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, 2020 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
29 * Copyright 2016 Nexenta Systems, Inc.
30 * Copyright (c) 2019 Datto Inc.
31 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
32 * Copyright 2019 Joyent, Inc.
33 * Copyright (c) 2019, 2020 by Christian Schwarz. All rights reserved.
38 #include <sys/debug.h>
44 #include <libnvpair.h>
56 #include <sys/debug.h>
58 #include <sys/mkdev.h>
59 #include <sys/mntent.h>
60 #include <sys/mnttab.h>
61 #include <sys/mount.h>
63 #include <sys/fs/zfs.h>
64 #include <sys/systeminfo.h>
65 #include <sys/types.h>
67 #include <sys/zfs_project.h>
70 #include <libzfs_core.h>
72 #include <zfs_deleg.h>
77 #include <directory.h>
78 #endif /* HAVE_IDMAP */
82 #include "zfs_comutil.h"
83 #include "libzfs_impl.h"
84 #include "zfs_projectutil.h"
86 libzfs_handle_t *g_zfs;
88 static FILE *mnttab_file;
89 static char history_str[HIS_MAX_RECORD_LEN];
90 static boolean_t log_history = B_TRUE;
92 static int zfs_do_clone(int argc, char **argv);
93 static int zfs_do_create(int argc, char **argv);
94 static int zfs_do_destroy(int argc, char **argv);
95 static int zfs_do_get(int argc, char **argv);
96 static int zfs_do_inherit(int argc, char **argv);
97 static int zfs_do_list(int argc, char **argv);
98 static int zfs_do_mount(int argc, char **argv);
99 static int zfs_do_rename(int argc, char **argv);
100 static int zfs_do_rollback(int argc, char **argv);
101 static int zfs_do_set(int argc, char **argv);
102 static int zfs_do_upgrade(int argc, char **argv);
103 static int zfs_do_snapshot(int argc, char **argv);
104 static int zfs_do_unmount(int argc, char **argv);
105 static int zfs_do_share(int argc, char **argv);
106 static int zfs_do_unshare(int argc, char **argv);
107 static int zfs_do_send(int argc, char **argv);
108 static int zfs_do_receive(int argc, char **argv);
109 static int zfs_do_promote(int argc, char **argv);
110 static int zfs_do_userspace(int argc, char **argv);
111 static int zfs_do_allow(int argc, char **argv);
112 static int zfs_do_unallow(int argc, char **argv);
113 static int zfs_do_hold(int argc, char **argv);
114 static int zfs_do_holds(int argc, char **argv);
115 static int zfs_do_release(int argc, char **argv);
116 static int zfs_do_diff(int argc, char **argv);
117 static int zfs_do_bookmark(int argc, char **argv);
118 static int zfs_do_channel_program(int argc, char **argv);
119 static int zfs_do_load_key(int argc, char **argv);
120 static int zfs_do_unload_key(int argc, char **argv);
121 static int zfs_do_change_key(int argc, char **argv);
122 static int zfs_do_project(int argc, char **argv);
123 static int zfs_do_version(int argc, char **argv);
124 static int zfs_do_redact(int argc, char **argv);
125 static int zfs_do_wait(int argc, char **argv);
128 static int zfs_do_jail(int argc, char **argv);
129 static int zfs_do_unjail(int argc, char **argv);
133 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
138 _umem_debug_init(void)
140 return ("default,verbose"); /* $UMEM_DEBUG setting */
144 _umem_logging_init(void)
146 return ("fail,contents"); /* $UMEM_LOGGING setting */
180 HELP_CHANNEL_PROGRAM,
191 typedef struct zfs_command {
193 int (*func)(int argc, char **argv);
198 * Master command table. Each ZFS command has a name, associated function, and
199 * usage message. The usage messages need to be internationalized, so we have
200 * to have a function to return the usage message based on a command index.
202 * These commands are organized according to how they are displayed in the usage
203 * message. An empty command (one with a NULL name) indicates an empty line in
204 * the generic usage message.
206 static zfs_command_t command_table[] = {
207 { "version", zfs_do_version, HELP_VERSION },
209 { "create", zfs_do_create, HELP_CREATE },
210 { "destroy", zfs_do_destroy, HELP_DESTROY },
212 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
213 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
214 { "clone", zfs_do_clone, HELP_CLONE },
215 { "promote", zfs_do_promote, HELP_PROMOTE },
216 { "rename", zfs_do_rename, HELP_RENAME },
217 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
218 { "program", zfs_do_channel_program, HELP_CHANNEL_PROGRAM },
220 { "list", zfs_do_list, HELP_LIST },
222 { "set", zfs_do_set, HELP_SET },
223 { "get", zfs_do_get, HELP_GET },
224 { "inherit", zfs_do_inherit, HELP_INHERIT },
225 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
227 { "userspace", zfs_do_userspace, HELP_USERSPACE },
228 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
229 { "projectspace", zfs_do_userspace, HELP_PROJECTSPACE },
231 { "project", zfs_do_project, HELP_PROJECT },
233 { "mount", zfs_do_mount, HELP_MOUNT },
234 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
235 { "share", zfs_do_share, HELP_SHARE },
236 { "unshare", zfs_do_unshare, HELP_UNSHARE },
238 { "send", zfs_do_send, HELP_SEND },
239 { "receive", zfs_do_receive, HELP_RECEIVE },
241 { "allow", zfs_do_allow, HELP_ALLOW },
243 { "unallow", zfs_do_unallow, HELP_UNALLOW },
245 { "hold", zfs_do_hold, HELP_HOLD },
246 { "holds", zfs_do_holds, HELP_HOLDS },
247 { "release", zfs_do_release, HELP_RELEASE },
248 { "diff", zfs_do_diff, HELP_DIFF },
249 { "load-key", zfs_do_load_key, HELP_LOAD_KEY },
250 { "unload-key", zfs_do_unload_key, HELP_UNLOAD_KEY },
251 { "change-key", zfs_do_change_key, HELP_CHANGE_KEY },
252 { "redact", zfs_do_redact, HELP_REDACT },
253 { "wait", zfs_do_wait, HELP_WAIT },
256 { "jail", zfs_do_jail, HELP_JAIL },
257 { "unjail", zfs_do_unjail, HELP_UNJAIL },
261 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
263 zfs_command_t *current_command;
266 get_usage(zfs_help_t idx)
270 return (gettext("\tclone [-p] [-o property=value] ... "
271 "<snapshot> <filesystem|volume>\n"));
273 return (gettext("\tcreate [-Pnpv] [-o property=value] ... "
275 "\tcreate [-Pnpsv] [-b blocksize] [-o property=value] ... "
276 "-V <size> <volume>\n"));
278 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
279 "\tdestroy [-dnpRrv] "
280 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
281 "\tdestroy <filesystem|volume>#<bookmark>\n"));
283 return (gettext("\tget [-rHp] [-d max] "
284 "[-o \"all\" | field[,...]]\n"
285 "\t [-t type[,...]] [-s source[,...]]\n"
286 "\t <\"all\" | property[,...]> "
287 "[filesystem|volume|snapshot|bookmark] ...\n"));
289 return (gettext("\tinherit [-rS] <property> "
290 "<filesystem|volume|snapshot> ...\n"));
292 return (gettext("\tupgrade [-v]\n"
293 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
295 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
296 "[-s property]...\n\t [-S property]... [-t type[,...]] "
297 "[filesystem|volume|snapshot] ...\n"));
299 return (gettext("\tmount\n"
300 "\tmount [-flvO] [-o opts] <-a | filesystem>\n"));
302 return (gettext("\tpromote <clone-filesystem>\n"));
304 return (gettext("\treceive [-vMnsFhu] "
305 "[-o <property>=<value>] ... [-x <property>] ...\n"
306 "\t <filesystem|volume|snapshot>\n"
307 "\treceive [-vMnsFhu] [-o <property>=<value>] ... "
308 "[-x <property>] ... \n"
309 "\t [-d | -e] <filesystem>\n"
310 "\treceive -A <filesystem|volume>\n"));
312 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
313 "<filesystem|volume|snapshot>\n"
314 "\trename -p [-f] <filesystem|volume> <filesystem|volume>\n"
315 "\trename -u [-f] <filesystem> <filesystem>\n"
316 "\trename -r <snapshot> <snapshot>\n"));
318 return (gettext("\trollback [-rRf] <snapshot>\n"));
320 return (gettext("\tsend [-DnPpRvLecwhb] [-[i|I] snapshot] "
322 "\tsend [-nvPLecw] [-i snapshot|bookmark] "
323 "<filesystem|volume|snapshot>\n"
324 "\tsend [-DnPpvLec] [-i bookmark|snapshot] "
325 "--redact <bookmark> <snapshot>\n"
326 "\tsend [-nvPe] -t <receive_resume_token>\n"
327 "\tsend [-Pnv] --saved filesystem\n"));
329 return (gettext("\tset <property=value> ... "
330 "<filesystem|volume|snapshot> ...\n"));
332 return (gettext("\tshare [-l] <-a [nfs|smb] | filesystem>\n"));
334 return (gettext("\tsnapshot [-r] [-o property=value] ... "
335 "<filesystem|volume>@<snap> ...\n"));
337 return (gettext("\tunmount [-fu] "
338 "<-a | filesystem|mountpoint>\n"));
340 return (gettext("\tunshare "
341 "<-a [nfs|smb] | filesystem|mountpoint>\n"));
343 return (gettext("\tallow <filesystem|volume>\n"
345 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
346 "\t <filesystem|volume>\n"
347 "\tallow [-ld] -e <perm|@setname>[,...] "
348 "<filesystem|volume>\n"
349 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
350 "\tallow -s @setname <perm|@setname>[,...] "
351 "<filesystem|volume>\n"));
353 return (gettext("\tunallow [-rldug] "
354 "<\"everyone\"|user|group>[,...]\n"
355 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
356 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
357 "<filesystem|volume>\n"
358 "\tunallow [-r] -c [<perm|@setname>[,...]] "
359 "<filesystem|volume>\n"
360 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
361 "<filesystem|volume>\n"));
363 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
365 "\t [-S field] ... [-t type[,...]] "
366 "<filesystem|snapshot>\n"));
367 case HELP_GROUPSPACE:
368 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
370 "\t [-S field] ... [-t type[,...]] "
371 "<filesystem|snapshot>\n"));
372 case HELP_PROJECTSPACE:
373 return (gettext("\tprojectspace [-Hp] [-o field[,...]] "
375 "\t [-S field] ... <filesystem|snapshot>\n"));
377 return (gettext("\tproject [-d|-r] <directory|file ...>\n"
378 "\tproject -c [-0] [-d|-r] [-p id] <directory|file ...>\n"
379 "\tproject -C [-k] [-r] <directory ...>\n"
380 "\tproject [-p id] [-r] [-s] <directory ...>\n"));
382 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
384 return (gettext("\tholds [-rH] <snapshot> ...\n"));
386 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
388 return (gettext("\tdiff [-FHt] <snapshot> "
389 "[snapshot|filesystem]\n"));
391 return (gettext("\tbookmark <snapshot|bookmark> "
393 case HELP_CHANNEL_PROGRAM:
394 return (gettext("\tprogram [-jn] [-t <instruction limit>] "
395 "[-m <memory limit (b)>]\n"
396 "\t <pool> <program file> [lua args...]\n"));
398 return (gettext("\tload-key [-rn] [-L <keylocation>] "
399 "<-a | filesystem|volume>\n"));
400 case HELP_UNLOAD_KEY:
401 return (gettext("\tunload-key [-r] "
402 "<-a | filesystem|volume>\n"));
403 case HELP_CHANGE_KEY:
404 return (gettext("\tchange-key [-l] [-o keyformat=<value>]\n"
405 "\t [-o keylocation=<value>] [-o pbkdf2iters=<value>]\n"
406 "\t <filesystem|volume>\n"
407 "\tchange-key -i [-l] <filesystem|volume>\n"));
409 return (gettext("\tversion\n"));
411 return (gettext("\tredact <snapshot> <bookmark> "
412 "<redaction_snapshot> ...\n"));
414 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
416 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
418 return (gettext("\twait [-t <activity>] <filesystem>\n"));
428 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
433 * Utility function to guarantee malloc() success.
437 safe_malloc(size_t size)
441 if ((data = calloc(1, size)) == NULL)
448 safe_realloc(void *data, size_t size)
451 if ((newp = realloc(data, size)) == NULL) {
460 safe_strdup(char *str)
462 char *dupstr = strdup(str);
471 * Callback routine that will print out information for each of
475 usage_prop_cb(int prop, void *cb)
479 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
481 if (zfs_prop_readonly(prop))
482 (void) fprintf(fp, " NO ");
484 (void) fprintf(fp, "YES ");
486 if (zfs_prop_inheritable(prop))
487 (void) fprintf(fp, " YES ");
489 (void) fprintf(fp, " NO ");
491 if (zfs_prop_values(prop) == NULL)
492 (void) fprintf(fp, "-\n");
494 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
500 * Display usage message. If we're inside a command, display only the usage for
501 * that command. Otherwise, iterate over the entire command table and display
502 * a complete usage message.
505 usage(boolean_t requested)
508 boolean_t show_properties = B_FALSE;
509 FILE *fp = requested ? stdout : stderr;
511 if (current_command == NULL) {
513 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
515 gettext("where 'command' is one of the following:\n\n"));
517 for (i = 0; i < NCOMMAND; i++) {
518 if (command_table[i].name == NULL)
519 (void) fprintf(fp, "\n");
521 (void) fprintf(fp, "%s",
522 get_usage(command_table[i].usage));
525 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
526 "pool/[dataset/]*dataset[@name]\n"));
528 (void) fprintf(fp, gettext("usage:\n"));
529 (void) fprintf(fp, "%s", get_usage(current_command->usage));
532 if (current_command != NULL &&
533 (strcmp(current_command->name, "set") == 0 ||
534 strcmp(current_command->name, "get") == 0 ||
535 strcmp(current_command->name, "inherit") == 0 ||
536 strcmp(current_command->name, "list") == 0))
537 show_properties = B_TRUE;
539 if (show_properties) {
541 gettext("\nThe following properties are supported:\n"));
543 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
544 "PROPERTY", "EDIT", "INHERIT", "VALUES");
546 /* Iterate over all properties */
547 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
550 (void) fprintf(fp, "\t%-15s ", "userused@...");
551 (void) fprintf(fp, " NO NO <size>\n");
552 (void) fprintf(fp, "\t%-15s ", "groupused@...");
553 (void) fprintf(fp, " NO NO <size>\n");
554 (void) fprintf(fp, "\t%-15s ", "projectused@...");
555 (void) fprintf(fp, " NO NO <size>\n");
556 (void) fprintf(fp, "\t%-15s ", "userobjused@...");
557 (void) fprintf(fp, " NO NO <size>\n");
558 (void) fprintf(fp, "\t%-15s ", "groupobjused@...");
559 (void) fprintf(fp, " NO NO <size>\n");
560 (void) fprintf(fp, "\t%-15s ", "projectobjused@...");
561 (void) fprintf(fp, " NO NO <size>\n");
562 (void) fprintf(fp, "\t%-15s ", "userquota@...");
563 (void) fprintf(fp, "YES NO <size> | none\n");
564 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
565 (void) fprintf(fp, "YES NO <size> | none\n");
566 (void) fprintf(fp, "\t%-15s ", "projectquota@...");
567 (void) fprintf(fp, "YES NO <size> | none\n");
568 (void) fprintf(fp, "\t%-15s ", "userobjquota@...");
569 (void) fprintf(fp, "YES NO <size> | none\n");
570 (void) fprintf(fp, "\t%-15s ", "groupobjquota@...");
571 (void) fprintf(fp, "YES NO <size> | none\n");
572 (void) fprintf(fp, "\t%-15s ", "projectobjquota@...");
573 (void) fprintf(fp, "YES NO <size> | none\n");
574 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
575 (void) fprintf(fp, " NO NO <size>\n");
576 (void) fprintf(fp, "\t%-15s ", "written#<bookmark>");
577 (void) fprintf(fp, " NO NO <size>\n");
579 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
580 "with standard units such as K, M, G, etc.\n"));
581 (void) fprintf(fp, gettext("\nUser-defined properties can "
582 "be specified by using a name containing a colon (:).\n"));
583 (void) fprintf(fp, gettext("\nThe {user|group|project}"
584 "[obj]{used|quota}@ properties must be appended with\n"
585 "a user|group|project specifier of one of these forms:\n"
586 " POSIX name (eg: \"matt\")\n"
587 " POSIX id (eg: \"126829\")\n"
588 " SMB name@domain (eg: \"matt@sun\")\n"
589 " SMB SID (eg: \"S-1-234-567-89\")\n"));
592 gettext("\nFor the property list, run: %s\n"),
595 gettext("\nFor the delegated permission list, run: %s\n"),
596 "zfs allow|unallow");
600 * See comments at end of main().
602 if (getenv("ZFS_ABORT") != NULL) {
603 (void) printf("dumping core by request\n");
607 exit(requested ? 0 : 2);
611 * Take a property=value argument string and add it to the given nvlist.
612 * Modifies the argument inplace.
615 parseprop(nvlist_t *props, char *propname)
619 if ((propval = strchr(propname, '=')) == NULL) {
620 (void) fprintf(stderr, gettext("missing "
621 "'=' for property=value argument\n"));
626 if (nvlist_exists(props, propname)) {
627 (void) fprintf(stderr, gettext("property '%s' "
628 "specified multiple times\n"), propname);
631 if (nvlist_add_string(props, propname, propval) != 0)
637 * Take a property name argument and add it to the given nvlist.
638 * Modifies the argument inplace.
641 parsepropname(nvlist_t *props, char *propname)
643 if (strchr(propname, '=') != NULL) {
644 (void) fprintf(stderr, gettext("invalid character "
645 "'=' in property argument\n"));
648 if (nvlist_exists(props, propname)) {
649 (void) fprintf(stderr, gettext("property '%s' "
650 "specified multiple times\n"), propname);
653 if (nvlist_add_boolean(props, propname) != 0)
659 parse_depth(char *opt, int *flags)
664 depth = (int)strtol(opt, &tmp, 0);
666 (void) fprintf(stderr,
667 gettext("%s is not an integer\n"), optarg);
671 (void) fprintf(stderr,
672 gettext("Depth can not be negative.\n"));
675 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
679 #define PROGRESS_DELAY 2 /* seconds */
681 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";
682 static time_t pt_begin;
683 static char *pt_header = NULL;
684 static boolean_t pt_shown;
687 start_progress_timer(void)
689 pt_begin = time(NULL) + PROGRESS_DELAY;
694 set_progress_header(char *header)
696 assert(pt_header == NULL);
697 pt_header = safe_strdup(header);
699 (void) printf("%s: ", header);
700 (void) fflush(stdout);
705 update_progress(char *update)
707 if (!pt_shown && time(NULL) > pt_begin) {
708 int len = strlen(update);
710 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
712 (void) fflush(stdout);
714 } else if (pt_shown) {
715 int len = strlen(update);
717 (void) printf("%s%*.*s", update, len, len, pt_reverse);
718 (void) fflush(stdout);
723 finish_progress(char *done)
726 (void) printf("%s\n", done);
727 (void) fflush(stdout);
734 zfs_mount_and_share(libzfs_handle_t *hdl, const char *dataset, zfs_type_t type)
736 zfs_handle_t *zhp = NULL;
739 zhp = zfs_open(hdl, dataset, type);
744 * Volumes may neither be mounted or shared. Potentially in the
745 * future filesystems detected on these volumes could be mounted.
747 if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
753 * Mount and/or share the new filesystem as appropriate. We provide a
754 * verbose error message to let the user know that their filesystem was
755 * in fact created, even if we failed to mount or share it.
757 * If the user doesn't want the dataset automatically mounted, then
758 * skip the mount/share step
760 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type, B_FALSE) &&
761 zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON) {
762 if (zfs_mount_delegation_check()) {
763 (void) fprintf(stderr, gettext("filesystem "
764 "successfully created, but it may only be "
765 "mounted by root\n"));
767 } else if (zfs_mount(zhp, NULL, 0) != 0) {
768 (void) fprintf(stderr, gettext("filesystem "
769 "successfully created, but not mounted\n"));
771 } else if (zfs_share(zhp) != 0) {
772 (void) fprintf(stderr, gettext("filesystem "
773 "successfully created, but not shared\n"));
776 zfs_commit_all_shares();
785 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
787 * Given an existing dataset, create a writable copy whose initial contents
788 * are the same as the source. The newly created dataset maintains a
789 * dependency on the original; the original cannot be destroyed so long as
792 * The '-p' flag creates all the non-existing ancestors of the target first.
795 zfs_do_clone(int argc, char **argv)
797 zfs_handle_t *zhp = NULL;
798 boolean_t parents = B_FALSE;
803 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
807 while ((c = getopt(argc, argv, "o:p")) != -1) {
810 if (!parseprop(props, optarg)) {
819 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
828 /* check number of arguments */
830 (void) fprintf(stderr, gettext("missing source dataset "
835 (void) fprintf(stderr, gettext("missing target dataset "
840 (void) fprintf(stderr, gettext("too many arguments\n"));
844 /* open the source dataset */
845 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) {
850 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
853 * Now create the ancestors of the target dataset. If the
854 * target already exists and '-p' option was used we should not
857 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
863 if (zfs_create_ancestors(g_zfs, argv[1]) != 0) {
871 ret = zfs_clone(zhp, argv[1], props);
873 /* create the mountpoint if necessary */
876 (void) zpool_log_history(g_zfs, history_str);
877 log_history = B_FALSE;
880 ret = zfs_mount_and_share(g_zfs, argv[1], ZFS_TYPE_DATASET);
889 ASSERT3P(zhp, ==, NULL);
896 * zfs create [-Pnpv] [-o prop=value] ... fs
897 * zfs create [-Pnpsv] [-b blocksize] [-o prop=value] ... -V vol size
899 * Create a new dataset. This command can be used to create filesystems
900 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
901 * For volumes, the user must specify a size to be used.
903 * The '-s' flag applies only to volumes, and indicates that we should not try
904 * to set the reservation for this volume. By default we set a reservation
905 * equal to the size for any volume. For pools with SPA_VERSION >=
906 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
908 * The '-p' flag creates all the non-existing ancestors of the target first.
910 * The '-n' flag is no-op (dry run) mode. This will perform a user-space sanity
911 * check of arguments and properties, but does not check for permissions,
912 * available space, etc.
914 * The '-v' flag is for verbose output.
916 * The '-P' flag is used for parseable output. It implies '-v'.
919 zfs_do_create(int argc, char **argv)
921 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
922 zpool_handle_t *zpool_handle = NULL;
923 nvlist_t *real_props = NULL;
924 uint64_t volsize = 0;
926 boolean_t noreserve = B_FALSE;
927 boolean_t bflag = B_FALSE;
928 boolean_t parents = B_FALSE;
929 boolean_t dryrun = B_FALSE;
930 boolean_t verbose = B_FALSE;
931 boolean_t parseable = B_FALSE;
936 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
940 while ((c = getopt(argc, argv, ":PV:b:nso:pv")) != -1) {
943 type = ZFS_TYPE_VOLUME;
944 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
945 (void) fprintf(stderr, gettext("bad volume "
946 "size '%s': %s\n"), optarg,
947 libzfs_error_description(g_zfs));
951 if (nvlist_add_uint64(props,
952 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
965 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
966 (void) fprintf(stderr, gettext("bad volume "
967 "block size '%s': %s\n"), optarg,
968 libzfs_error_description(g_zfs));
972 if (nvlist_add_uint64(props,
973 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
981 if (!parseprop(props, optarg))
991 (void) fprintf(stderr, gettext("missing size "
995 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1001 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
1002 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
1003 "used when creating a volume\n"));
1010 /* check number of arguments */
1012 (void) fprintf(stderr, gettext("missing %s argument\n"),
1013 zfs_type_to_name(type));
1017 (void) fprintf(stderr, gettext("too many arguments\n"));
1021 if (dryrun || (type == ZFS_TYPE_VOLUME && !noreserve)) {
1022 char msg[ZFS_MAX_DATASET_NAME_LEN * 2];
1025 if ((p = strchr(argv[0], '/')) != NULL)
1027 zpool_handle = zpool_open(g_zfs, argv[0]);
1030 if (zpool_handle == NULL)
1033 (void) snprintf(msg, sizeof (msg),
1034 dryrun ? gettext("cannot verify '%s'") :
1035 gettext("cannot create '%s'"), argv[0]);
1036 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
1037 props, 0, NULL, zpool_handle, B_TRUE, msg)) == NULL) {
1038 zpool_close(zpool_handle);
1044 * if volsize is not a multiple of volblocksize, round it up to the
1045 * nearest multiple of the volblocksize
1047 if (type == ZFS_TYPE_VOLUME) {
1048 uint64_t volblocksize;
1050 if (nvlist_lookup_uint64(props,
1051 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1052 &volblocksize) != 0)
1053 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
1055 if (volsize % volblocksize) {
1056 volsize = P2ROUNDUP_TYPED(volsize, volblocksize,
1059 if (nvlist_add_uint64(props,
1060 zfs_prop_to_name(ZFS_PROP_VOLSIZE), volsize) != 0) {
1068 if (type == ZFS_TYPE_VOLUME && !noreserve) {
1069 uint64_t spa_version;
1070 zfs_prop_t resv_prop;
1073 spa_version = zpool_get_prop_int(zpool_handle,
1074 ZPOOL_PROP_VERSION, NULL);
1075 if (spa_version >= SPA_VERSION_REFRESERVATION)
1076 resv_prop = ZFS_PROP_REFRESERVATION;
1078 resv_prop = ZFS_PROP_RESERVATION;
1080 volsize = zvol_volsize_to_reservation(zpool_handle, volsize,
1083 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
1085 if (nvlist_add_uint64(props,
1086 zfs_prop_to_name(resv_prop), volsize) != 0) {
1092 if (zpool_handle != NULL) {
1093 zpool_close(zpool_handle);
1094 nvlist_free(real_props);
1097 if (parents && zfs_name_valid(argv[0], type)) {
1099 * Now create the ancestors of target dataset. If the target
1100 * already exists and '-p' option was used we should not
1103 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
1108 (void) printf(parseable ? "create_ancestors\t%s\n" :
1109 dryrun ? "would create ancestors of %s\n" :
1110 "create ancestors of %s\n", argv[0]);
1113 if (zfs_create_ancestors(g_zfs, argv[0]) != 0) {
1120 nvpair_t *nvp = NULL;
1121 (void) printf(parseable ? "create\t%s\n" :
1122 dryrun ? "would create %s\n" : "create %s\n", argv[0]);
1123 while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) {
1127 switch (nvpair_type(nvp)) {
1128 case DATA_TYPE_UINT64:
1129 VERIFY0(nvpair_value_uint64(nvp, &uval));
1130 (void) printf(parseable ?
1131 "property\t%s\t%llu\n" : "\t%s=%llu\n",
1132 nvpair_name(nvp), (u_longlong_t)uval);
1134 case DATA_TYPE_STRING:
1135 VERIFY0(nvpair_value_string(nvp, &sval));
1136 (void) printf(parseable ?
1137 "property\t%s\t%s\n" : "\t%s=%s\n",
1138 nvpair_name(nvp), sval);
1141 (void) fprintf(stderr, "property '%s' "
1142 "has illegal type %d\n",
1143 nvpair_name(nvp), nvpair_type(nvp));
1153 /* pass to libzfs */
1154 if (zfs_create(g_zfs, argv[0], type, props) != 0)
1158 (void) zpool_log_history(g_zfs, history_str);
1159 log_history = B_FALSE;
1162 ret = zfs_mount_and_share(g_zfs, argv[0], ZFS_TYPE_DATASET);
1173 * zfs destroy [-rRf] <fs, vol>
1174 * zfs destroy [-rRd] <snap>
1176 * -r Recursively destroy all children
1177 * -R Recursively destroy all dependents, including clones
1178 * -f Force unmounting of any dependents
1179 * -d If we can't destroy now, mark for deferred destruction
1181 * Destroys the given dataset. By default, it will unmount any filesystems,
1182 * and refuse to destroy a dataset that has any dependents. A dependent can
1183 * either be a child, or a clone of a child.
1185 typedef struct destroy_cbdata {
1188 boolean_t cb_recurse;
1190 boolean_t cb_doclones;
1191 zfs_handle_t *cb_target;
1192 boolean_t cb_defer_destroy;
1193 boolean_t cb_verbose;
1194 boolean_t cb_parsable;
1195 boolean_t cb_dryrun;
1197 nvlist_t *cb_batchedsnaps;
1199 /* first snap in contiguous run */
1201 /* previous snap in contiguous run */
1203 int64_t cb_snapused;
1206 uint64_t cb_snap_count;
1210 * Check for any dependents based on the '-r' or '-R' flags.
1213 destroy_check_dependent(zfs_handle_t *zhp, void *data)
1215 destroy_cbdata_t *cbp = data;
1216 const char *tname = zfs_get_name(cbp->cb_target);
1217 const char *name = zfs_get_name(zhp);
1219 if (strncmp(tname, name, strlen(tname)) == 0 &&
1220 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
1222 * This is a direct descendant, not a clone somewhere else in
1225 if (cbp->cb_recurse)
1228 if (cbp->cb_first) {
1229 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1230 "%s has children\n"),
1231 zfs_get_name(cbp->cb_target),
1232 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1233 (void) fprintf(stderr, gettext("use '-r' to destroy "
1234 "the following datasets:\n"));
1235 cbp->cb_first = B_FALSE;
1236 cbp->cb_error = B_TRUE;
1239 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1242 * This is a clone. We only want to report this if the '-r'
1243 * wasn't specified, or the target is a snapshot.
1245 if (!cbp->cb_recurse &&
1246 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1249 if (cbp->cb_first) {
1250 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1251 "%s has dependent clones\n"),
1252 zfs_get_name(cbp->cb_target),
1253 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1254 (void) fprintf(stderr, gettext("use '-R' to destroy "
1255 "the following datasets:\n"));
1256 cbp->cb_first = B_FALSE;
1257 cbp->cb_error = B_TRUE;
1258 cbp->cb_dryrun = B_TRUE;
1261 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1270 destroy_batched(destroy_cbdata_t *cb)
1272 int error = zfs_destroy_snaps_nvl(g_zfs,
1273 cb->cb_batchedsnaps, B_FALSE);
1274 fnvlist_free(cb->cb_batchedsnaps);
1275 cb->cb_batchedsnaps = fnvlist_alloc();
1280 destroy_callback(zfs_handle_t *zhp, void *data)
1282 destroy_cbdata_t *cb = data;
1283 const char *name = zfs_get_name(zhp);
1286 if (cb->cb_verbose) {
1287 if (cb->cb_parsable) {
1288 (void) printf("destroy\t%s\n", name);
1289 } else if (cb->cb_dryrun) {
1290 (void) printf(gettext("would destroy %s\n"),
1293 (void) printf(gettext("will destroy %s\n"),
1299 * Ignore pools (which we've already flagged as an error before getting
1302 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1303 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1307 if (cb->cb_dryrun) {
1313 * We batch up all contiguous snapshots (even of different
1314 * filesystems) and destroy them with one ioctl. We can't
1315 * simply do all snap deletions and then all fs deletions,
1316 * because we must delete a clone before its origin.
1318 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1319 cb->cb_snap_count++;
1320 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1321 if (cb->cb_snap_count % 10 == 0 && cb->cb_defer_destroy)
1322 error = destroy_batched(cb);
1324 error = destroy_batched(cb);
1326 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1327 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1330 * When performing a recursive destroy we ignore errors
1331 * so that the recursive destroy could continue
1332 * destroying past problem datasets
1334 if (cb->cb_recurse) {
1335 cb->cb_error = B_TRUE;
1347 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1349 destroy_cbdata_t *cb = arg;
1350 const char *name = zfs_get_name(zhp);
1353 if (nvlist_exists(cb->cb_nvl, name)) {
1354 if (cb->cb_firstsnap == NULL)
1355 cb->cb_firstsnap = strdup(name);
1356 if (cb->cb_prevsnap != NULL)
1357 free(cb->cb_prevsnap);
1358 /* this snap continues the current range */
1359 cb->cb_prevsnap = strdup(name);
1360 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1362 if (cb->cb_verbose) {
1363 if (cb->cb_parsable) {
1364 (void) printf("destroy\t%s\n", name);
1365 } else if (cb->cb_dryrun) {
1366 (void) printf(gettext("would destroy %s\n"),
1369 (void) printf(gettext("will destroy %s\n"),
1373 } else if (cb->cb_firstsnap != NULL) {
1374 /* end of this range */
1376 err = lzc_snaprange_space(cb->cb_firstsnap,
1377 cb->cb_prevsnap, &used);
1378 cb->cb_snapused += used;
1379 free(cb->cb_firstsnap);
1380 cb->cb_firstsnap = NULL;
1381 free(cb->cb_prevsnap);
1382 cb->cb_prevsnap = NULL;
1389 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1392 assert(cb->cb_firstsnap == NULL);
1393 assert(cb->cb_prevsnap == NULL);
1394 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb, 0, 0);
1395 if (cb->cb_firstsnap != NULL) {
1398 err = lzc_snaprange_space(cb->cb_firstsnap,
1399 cb->cb_prevsnap, &used);
1401 cb->cb_snapused += used;
1402 free(cb->cb_firstsnap);
1403 cb->cb_firstsnap = NULL;
1404 free(cb->cb_prevsnap);
1405 cb->cb_prevsnap = NULL;
1411 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1413 destroy_cbdata_t *cb = arg;
1416 /* Check for clones. */
1417 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1418 cb->cb_target = zhp;
1419 cb->cb_first = B_TRUE;
1420 err = zfs_iter_dependents(zhp, B_TRUE,
1421 destroy_check_dependent, cb);
1425 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1433 gather_snapshots(zfs_handle_t *zhp, void *arg)
1435 destroy_cbdata_t *cb = arg;
1438 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1444 if (cb->cb_verbose) {
1445 err = destroy_print_snapshots(zhp, cb);
1451 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1459 destroy_clones(destroy_cbdata_t *cb)
1462 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1464 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1465 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1468 boolean_t defer = cb->cb_defer_destroy;
1472 * We can't defer destroy non-snapshots, so set it to
1473 * false while destroying the clones.
1475 cb->cb_defer_destroy = B_FALSE;
1476 err = zfs_iter_dependents(zhp, B_FALSE,
1477 destroy_callback, cb);
1478 cb->cb_defer_destroy = defer;
1488 zfs_do_destroy(int argc, char **argv)
1490 destroy_cbdata_t cb = { 0 };
1494 zfs_handle_t *zhp = NULL;
1496 zfs_type_t type = ZFS_TYPE_DATASET;
1499 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1502 cb.cb_verbose = B_TRUE;
1505 cb.cb_verbose = B_TRUE;
1506 cb.cb_parsable = B_TRUE;
1509 cb.cb_dryrun = B_TRUE;
1512 cb.cb_defer_destroy = B_TRUE;
1513 type = ZFS_TYPE_SNAPSHOT;
1516 cb.cb_force = B_TRUE;
1519 cb.cb_recurse = B_TRUE;
1522 cb.cb_recurse = B_TRUE;
1523 cb.cb_doclones = B_TRUE;
1527 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1536 /* check number of arguments */
1538 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1542 (void) fprintf(stderr, gettext("too many arguments\n"));
1546 at = strchr(argv[0], '@');
1547 pound = strchr(argv[0], '#');
1550 /* Build the list of snaps to destroy in cb_nvl. */
1551 cb.cb_nvl = fnvlist_alloc();
1554 zhp = zfs_open(g_zfs, argv[0],
1555 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1557 nvlist_free(cb.cb_nvl);
1561 cb.cb_snapspec = at + 1;
1562 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1568 if (nvlist_empty(cb.cb_nvl)) {
1569 (void) fprintf(stderr, gettext("could not find any "
1570 "snapshots to destroy; check snapshot names.\n"));
1575 if (cb.cb_verbose) {
1577 zfs_nicebytes(cb.cb_snapused, buf, sizeof (buf));
1578 if (cb.cb_parsable) {
1579 (void) printf("reclaim\t%llu\n",
1580 (u_longlong_t)cb.cb_snapused);
1581 } else if (cb.cb_dryrun) {
1582 (void) printf(gettext("would reclaim %s\n"),
1585 (void) printf(gettext("will reclaim %s\n"),
1590 if (!cb.cb_dryrun) {
1591 if (cb.cb_doclones) {
1592 cb.cb_batchedsnaps = fnvlist_alloc();
1593 err = destroy_clones(&cb);
1595 err = zfs_destroy_snaps_nvl(g_zfs,
1596 cb.cb_batchedsnaps, B_FALSE);
1604 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1605 cb.cb_defer_destroy);
1611 } else if (pound != NULL) {
1616 (void) fprintf(stderr,
1617 "dryrun is not supported with bookmark\n");
1621 if (cb.cb_defer_destroy) {
1622 (void) fprintf(stderr,
1623 "defer destroy is not supported with bookmark\n");
1627 if (cb.cb_recurse) {
1628 (void) fprintf(stderr,
1629 "recursive is not supported with bookmark\n");
1634 * Unfortunately, zfs_bookmark() doesn't honor the
1635 * casesensitivity setting. However, we can't simply
1636 * remove this check, because lzc_destroy_bookmarks()
1637 * ignores non-existent bookmarks, so this is necessary
1638 * to get a proper error message.
1640 if (!zfs_bookmark_exists(argv[0])) {
1641 (void) fprintf(stderr, gettext("bookmark '%s' "
1642 "does not exist.\n"), argv[0]);
1646 nvl = fnvlist_alloc();
1647 fnvlist_add_boolean(nvl, argv[0]);
1649 err = lzc_destroy_bookmarks(nvl, NULL);
1651 (void) zfs_standard_error(g_zfs, err,
1652 "cannot destroy bookmark");
1659 /* Open the given dataset */
1660 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1666 * Perform an explicit check for pools before going any further.
1668 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1669 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1670 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1671 "operation does not apply to pools\n"),
1673 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1674 "%s' to destroy all datasets in the pool\n"),
1676 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1677 "to destroy the pool itself\n"), zfs_get_name(zhp));
1683 * Check for any dependents and/or clones.
1685 cb.cb_first = B_TRUE;
1686 if (!cb.cb_doclones &&
1687 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1697 cb.cb_batchedsnaps = fnvlist_alloc();
1698 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1705 * Do the real thing. The callback will close the
1706 * handle regardless of whether it succeeds or not.
1708 err = destroy_callback(zhp, &cb);
1711 err = zfs_destroy_snaps_nvl(g_zfs,
1712 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1714 if (err != 0 || cb.cb_error == B_TRUE)
1719 fnvlist_free(cb.cb_batchedsnaps);
1720 fnvlist_free(cb.cb_nvl);
1727 is_recvd_column(zprop_get_cbdata_t *cbp)
1730 zfs_get_column_t col;
1732 for (i = 0; i < ZFS_GET_NCOLS &&
1733 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1734 if (col == GET_COL_RECVD)
1740 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1741 * < all | property[,property]... > < fs | snap | vol > ...
1743 * -r recurse over any child datasets
1744 * -H scripted mode. Headers are stripped, and fields are separated
1745 * by tabs instead of spaces.
1746 * -o Set of fields to display. One of "name,property,value,
1747 * received,source". Default is "name,property,value,source".
1748 * "all" is an alias for all five.
1749 * -s Set of sources to allow. One of
1750 * "local,default,inherited,received,temporary,none". Default is
1752 * -p Display values in parsable (literal) format.
1754 * Prints properties for the given datasets. The user can control which
1755 * columns to display as well as which property types to allow.
1759 * Invoked to display the properties for a single dataset.
1762 get_callback(zfs_handle_t *zhp, void *data)
1764 char buf[ZFS_MAXPROPLEN];
1765 char rbuf[ZFS_MAXPROPLEN];
1766 zprop_source_t sourcetype;
1767 char source[ZFS_MAX_DATASET_NAME_LEN];
1768 zprop_get_cbdata_t *cbp = data;
1769 nvlist_t *user_props = zfs_get_user_props(zhp);
1770 zprop_list_t *pl = cbp->cb_proplist;
1774 boolean_t received = is_recvd_column(cbp);
1776 for (; pl != NULL; pl = pl->pl_next) {
1777 char *recvdval = NULL;
1779 * Skip the special fake placeholder. This will also skip over
1780 * the name property when 'all' is specified.
1782 if (pl->pl_prop == ZFS_PROP_NAME &&
1783 pl == cbp->cb_proplist)
1786 if (pl->pl_prop != ZPROP_INVAL) {
1787 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1788 sizeof (buf), &sourcetype, source,
1790 cbp->cb_literal) != 0) {
1793 if (!zfs_prop_valid_for_type(pl->pl_prop,
1794 ZFS_TYPE_DATASET, B_FALSE)) {
1795 (void) fprintf(stderr,
1796 gettext("No such property '%s'\n"),
1797 zfs_prop_to_name(pl->pl_prop));
1800 sourcetype = ZPROP_SRC_NONE;
1801 (void) strlcpy(buf, "-", sizeof (buf));
1804 if (received && (zfs_prop_get_recvd(zhp,
1805 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1806 cbp->cb_literal) == 0))
1809 zprop_print_one_property(zfs_get_name(zhp), cbp,
1810 zfs_prop_to_name(pl->pl_prop),
1811 buf, sourcetype, source, recvdval);
1812 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1813 sourcetype = ZPROP_SRC_LOCAL;
1815 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1816 buf, sizeof (buf), cbp->cb_literal) != 0) {
1817 sourcetype = ZPROP_SRC_NONE;
1818 (void) strlcpy(buf, "-", sizeof (buf));
1821 zprop_print_one_property(zfs_get_name(zhp), cbp,
1822 pl->pl_user_prop, buf, sourcetype, source, NULL);
1823 } else if (zfs_prop_written(pl->pl_user_prop)) {
1824 sourcetype = ZPROP_SRC_LOCAL;
1826 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1827 buf, sizeof (buf), cbp->cb_literal) != 0) {
1828 sourcetype = ZPROP_SRC_NONE;
1829 (void) strlcpy(buf, "-", sizeof (buf));
1832 zprop_print_one_property(zfs_get_name(zhp), cbp,
1833 pl->pl_user_prop, buf, sourcetype, source, NULL);
1835 if (nvlist_lookup_nvlist(user_props,
1836 pl->pl_user_prop, &propval) != 0) {
1839 sourcetype = ZPROP_SRC_NONE;
1842 verify(nvlist_lookup_string(propval,
1843 ZPROP_VALUE, &strval) == 0);
1844 verify(nvlist_lookup_string(propval,
1845 ZPROP_SOURCE, &sourceval) == 0);
1847 if (strcmp(sourceval,
1848 zfs_get_name(zhp)) == 0) {
1849 sourcetype = ZPROP_SRC_LOCAL;
1850 } else if (strcmp(sourceval,
1851 ZPROP_SOURCE_VAL_RECVD) == 0) {
1852 sourcetype = ZPROP_SRC_RECEIVED;
1854 sourcetype = ZPROP_SRC_INHERITED;
1855 (void) strlcpy(source,
1856 sourceval, sizeof (source));
1860 if (received && (zfs_prop_get_recvd(zhp,
1861 pl->pl_user_prop, rbuf, sizeof (rbuf),
1862 cbp->cb_literal) == 0))
1865 zprop_print_one_property(zfs_get_name(zhp), cbp,
1866 pl->pl_user_prop, strval, sourcetype,
1875 zfs_do_get(int argc, char **argv)
1877 zprop_get_cbdata_t cb = { 0 };
1878 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1879 int types = ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK;
1880 char *value, *fields;
1883 zprop_list_t fake_name = { 0 };
1886 * Set up default columns and sources.
1888 cb.cb_sources = ZPROP_SRC_ALL;
1889 cb.cb_columns[0] = GET_COL_NAME;
1890 cb.cb_columns[1] = GET_COL_PROPERTY;
1891 cb.cb_columns[2] = GET_COL_VALUE;
1892 cb.cb_columns[3] = GET_COL_SOURCE;
1893 cb.cb_type = ZFS_TYPE_DATASET;
1896 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1899 cb.cb_literal = B_TRUE;
1902 limit = parse_depth(optarg, &flags);
1905 flags |= ZFS_ITER_RECURSE;
1908 cb.cb_scripted = B_TRUE;
1911 (void) fprintf(stderr, gettext("missing argument for "
1912 "'%c' option\n"), optopt);
1917 * Process the set of columns to display. We zero out
1918 * the structure to give us a blank slate.
1920 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1922 while (*optarg != '\0') {
1923 static char *col_subopts[] =
1924 { "name", "property", "value", "received",
1925 "source", "all", NULL };
1927 if (i == ZFS_GET_NCOLS) {
1928 (void) fprintf(stderr, gettext("too "
1929 "many fields given to -o "
1934 switch (getsubopt(&optarg, col_subopts,
1937 cb.cb_columns[i++] = GET_COL_NAME;
1940 cb.cb_columns[i++] = GET_COL_PROPERTY;
1943 cb.cb_columns[i++] = GET_COL_VALUE;
1946 cb.cb_columns[i++] = GET_COL_RECVD;
1947 flags |= ZFS_ITER_RECVD_PROPS;
1950 cb.cb_columns[i++] = GET_COL_SOURCE;
1954 (void) fprintf(stderr,
1955 gettext("\"all\" conflicts "
1956 "with specific fields "
1957 "given to -o option\n"));
1960 cb.cb_columns[0] = GET_COL_NAME;
1961 cb.cb_columns[1] = GET_COL_PROPERTY;
1962 cb.cb_columns[2] = GET_COL_VALUE;
1963 cb.cb_columns[3] = GET_COL_RECVD;
1964 cb.cb_columns[4] = GET_COL_SOURCE;
1965 flags |= ZFS_ITER_RECVD_PROPS;
1969 (void) fprintf(stderr,
1970 gettext("invalid column name "
1979 while (*optarg != '\0') {
1980 static char *source_subopts[] = {
1981 "local", "default", "inherited",
1982 "received", "temporary", "none",
1985 switch (getsubopt(&optarg, source_subopts,
1988 cb.cb_sources |= ZPROP_SRC_LOCAL;
1991 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1994 cb.cb_sources |= ZPROP_SRC_INHERITED;
1997 cb.cb_sources |= ZPROP_SRC_RECEIVED;
2000 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
2003 cb.cb_sources |= ZPROP_SRC_NONE;
2006 (void) fprintf(stderr,
2007 gettext("invalid source "
2016 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
2017 while (*optarg != '\0') {
2018 static char *type_subopts[] = { "filesystem",
2019 "volume", "snapshot", "snap", "bookmark",
2022 switch (getsubopt(&optarg, type_subopts,
2025 types |= ZFS_TYPE_FILESYSTEM;
2028 types |= ZFS_TYPE_VOLUME;
2032 types |= ZFS_TYPE_SNAPSHOT;
2035 types |= ZFS_TYPE_BOOKMARK;
2038 types = ZFS_TYPE_DATASET |
2043 (void) fprintf(stderr,
2044 gettext("invalid type '%s'\n"),
2052 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2062 (void) fprintf(stderr, gettext("missing property "
2070 * Handle users who want to get all snapshots or bookmarks
2071 * of a dataset (ex. 'zfs get -t snapshot refer <dataset>').
2073 if ((types == ZFS_TYPE_SNAPSHOT || types == ZFS_TYPE_BOOKMARK) &&
2074 argc > 1 && (flags & ZFS_ITER_RECURSE) == 0 && limit == 0) {
2075 flags |= (ZFS_ITER_DEPTH_LIMIT | ZFS_ITER_RECURSE);
2079 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
2087 * As part of zfs_expand_proplist(), we keep track of the maximum column
2088 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
2089 * need to know the maximum name length. However, the user likely did
2090 * not specify 'name' as one of the properties to fetch, so we need to
2091 * make sure we always include at least this property for
2092 * print_get_headers() to work properly.
2094 if (cb.cb_proplist != NULL) {
2095 fake_name.pl_prop = ZFS_PROP_NAME;
2096 fake_name.pl_width = strlen(gettext("NAME"));
2097 fake_name.pl_next = cb.cb_proplist;
2098 cb.cb_proplist = &fake_name;
2101 cb.cb_first = B_TRUE;
2103 /* run for each object */
2104 ret = zfs_for_each(argc, argv, flags, types, NULL,
2105 &cb.cb_proplist, limit, get_callback, &cb);
2107 if (cb.cb_proplist == &fake_name)
2108 zprop_free_list(fake_name.pl_next);
2110 zprop_free_list(cb.cb_proplist);
2116 * inherit [-rS] <property> <fs|vol> ...
2118 * -r Recurse over all children
2119 * -S Revert to received value, if any
2121 * For each dataset specified on the command line, inherit the given property
2122 * from its parent. Inheriting a property at the pool level will cause it to
2123 * use the default value. The '-r' flag will recurse over all children, and is
2124 * useful for setting a property on a hierarchy-wide basis, regardless of any
2125 * local modifications for each dataset.
2128 typedef struct inherit_cbdata {
2129 const char *cb_propname;
2130 boolean_t cb_received;
2134 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
2136 inherit_cbdata_t *cb = data;
2137 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
2140 * If we're doing it recursively, then ignore properties that
2141 * are not valid for this type of dataset.
2143 if (prop != ZPROP_INVAL &&
2144 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp), B_FALSE))
2147 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
2151 inherit_cb(zfs_handle_t *zhp, void *data)
2153 inherit_cbdata_t *cb = data;
2155 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
2159 zfs_do_inherit(int argc, char **argv)
2163 inherit_cbdata_t cb = { 0 };
2167 boolean_t received = B_FALSE;
2170 while ((c = getopt(argc, argv, "rS")) != -1) {
2173 flags |= ZFS_ITER_RECURSE;
2180 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2189 /* check number of arguments */
2191 (void) fprintf(stderr, gettext("missing property argument\n"));
2195 (void) fprintf(stderr, gettext("missing dataset argument\n"));
2203 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
2204 if (zfs_prop_readonly(prop)) {
2205 (void) fprintf(stderr, gettext(
2206 "%s property is read-only\n"),
2210 if (!zfs_prop_inheritable(prop) && !received) {
2211 (void) fprintf(stderr, gettext("'%s' property cannot "
2212 "be inherited\n"), propname);
2213 if (prop == ZFS_PROP_QUOTA ||
2214 prop == ZFS_PROP_RESERVATION ||
2215 prop == ZFS_PROP_REFQUOTA ||
2216 prop == ZFS_PROP_REFRESERVATION) {
2217 (void) fprintf(stderr, gettext("use 'zfs set "
2218 "%s=none' to clear\n"), propname);
2219 (void) fprintf(stderr, gettext("use 'zfs "
2220 "inherit -S %s' to revert to received "
2221 "value\n"), propname);
2225 if (received && (prop == ZFS_PROP_VOLSIZE ||
2226 prop == ZFS_PROP_VERSION)) {
2227 (void) fprintf(stderr, gettext("'%s' property cannot "
2228 "be reverted to a received value\n"), propname);
2231 } else if (!zfs_prop_user(propname)) {
2232 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
2237 cb.cb_propname = propname;
2238 cb.cb_received = received;
2240 if (flags & ZFS_ITER_RECURSE) {
2241 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2242 NULL, NULL, 0, inherit_recurse_cb, &cb);
2244 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2245 NULL, NULL, 0, inherit_cb, &cb);
2251 typedef struct upgrade_cbdata {
2252 uint64_t cb_numupgraded;
2253 uint64_t cb_numsamegraded;
2254 uint64_t cb_numfailed;
2255 uint64_t cb_version;
2257 boolean_t cb_foundone;
2258 char cb_lastfs[ZFS_MAX_DATASET_NAME_LEN];
2262 same_pool(zfs_handle_t *zhp, const char *name)
2264 int len1 = strcspn(name, "/@");
2265 const char *zhname = zfs_get_name(zhp);
2266 int len2 = strcspn(zhname, "/@");
2270 return (strncmp(name, zhname, len1) == 0);
2274 upgrade_list_callback(zfs_handle_t *zhp, void *data)
2276 upgrade_cbdata_t *cb = data;
2277 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2279 /* list if it's old/new */
2280 if ((!cb->cb_newer && version < ZPL_VERSION) ||
2281 (cb->cb_newer && version > ZPL_VERSION)) {
2284 str = gettext("The following filesystems are "
2285 "formatted using a newer software version and\n"
2286 "cannot be accessed on the current system.\n\n");
2288 str = gettext("The following filesystems are "
2289 "out of date, and can be upgraded. After being\n"
2290 "upgraded, these filesystems (and any 'zfs send' "
2291 "streams generated from\n"
2292 "subsequent snapshots) will no longer be "
2293 "accessible by older software versions.\n\n");
2296 if (!cb->cb_foundone) {
2298 (void) printf(gettext("VER FILESYSTEM\n"));
2299 (void) printf(gettext("--- ------------\n"));
2300 cb->cb_foundone = B_TRUE;
2303 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2310 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2312 upgrade_cbdata_t *cb = data;
2313 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2314 int needed_spa_version;
2317 if (zfs_spa_version(zhp, &spa_version) < 0)
2320 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2322 if (needed_spa_version < 0)
2325 if (spa_version < needed_spa_version) {
2327 (void) printf(gettext("%s: can not be "
2328 "upgraded; the pool version needs to first "
2329 "be upgraded\nto version %d\n\n"),
2330 zfs_get_name(zhp), needed_spa_version);
2336 if (version < cb->cb_version) {
2338 (void) snprintf(verstr, sizeof (verstr),
2339 "%llu", (u_longlong_t)cb->cb_version);
2340 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2342 * If they did "zfs upgrade -a", then we could
2343 * be doing ioctls to different pools. We need
2344 * to log this history once to each pool, and bypass
2345 * the normal history logging that happens in main().
2347 (void) zpool_log_history(g_zfs, history_str);
2348 log_history = B_FALSE;
2350 if (zfs_prop_set(zhp, "version", verstr) == 0)
2351 cb->cb_numupgraded++;
2354 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2355 } else if (version > cb->cb_version) {
2356 /* can't downgrade */
2357 (void) printf(gettext("%s: can not be downgraded; "
2358 "it is already at version %u\n"),
2359 zfs_get_name(zhp), version);
2362 cb->cb_numsamegraded++;
2370 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2373 zfs_do_upgrade(int argc, char **argv)
2375 boolean_t all = B_FALSE;
2376 boolean_t showversions = B_FALSE;
2378 upgrade_cbdata_t cb = { 0 };
2380 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2383 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2386 flags |= ZFS_ITER_RECURSE;
2389 showversions = B_TRUE;
2392 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2393 optarg, &cb.cb_version) != 0) {
2394 (void) fprintf(stderr,
2395 gettext("invalid version %s\n"), optarg);
2404 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2413 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2415 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2416 cb.cb_version || argc))
2418 if ((all || argc) && (showversions))
2424 /* Show info on available versions. */
2425 (void) printf(gettext("The following filesystem versions are "
2427 (void) printf(gettext("VER DESCRIPTION\n"));
2428 (void) printf("--- -----------------------------------------"
2429 "---------------\n");
2430 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2431 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2432 (void) printf(gettext(" 3 Case insensitive and filesystem "
2433 "user identifier (FUID)\n"));
2434 (void) printf(gettext(" 4 userquota, groupquota "
2436 (void) printf(gettext(" 5 System attributes\n"));
2437 (void) printf(gettext("\nFor more information on a particular "
2438 "version, including supported releases,\n"));
2439 (void) printf("see the ZFS Administration Guide.\n\n");
2441 } else if (argc || all) {
2442 /* Upgrade filesystems */
2443 if (cb.cb_version == 0)
2444 cb.cb_version = ZPL_VERSION;
2445 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2446 NULL, NULL, 0, upgrade_set_callback, &cb);
2447 (void) printf(gettext("%llu filesystems upgraded\n"),
2448 (u_longlong_t)cb.cb_numupgraded);
2449 if (cb.cb_numsamegraded) {
2450 (void) printf(gettext("%llu filesystems already at "
2452 (u_longlong_t)cb.cb_numsamegraded);
2454 if (cb.cb_numfailed != 0)
2457 /* List old-version filesystems */
2459 (void) printf(gettext("This system is currently running "
2460 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2462 flags |= ZFS_ITER_RECURSE;
2463 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2464 NULL, NULL, 0, upgrade_list_callback, &cb);
2466 found = cb.cb_foundone;
2467 cb.cb_foundone = B_FALSE;
2468 cb.cb_newer = B_TRUE;
2470 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2471 NULL, NULL, 0, upgrade_list_callback, &cb);
2473 if (!cb.cb_foundone && !found) {
2474 (void) printf(gettext("All filesystems are "
2475 "formatted with the current version.\n"));
2483 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2484 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2485 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2486 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2487 * zfs projectspace [-Hp] [-o field[,...]] [-s field [-s field]...]
2488 * [-S field [-S field]...] filesystem | snapshot
2490 * -H Scripted mode; elide headers and separate columns by tabs.
2491 * -i Translate SID to POSIX ID.
2492 * -n Print numeric ID instead of user/group name.
2493 * -o Control which fields to display.
2494 * -p Use exact (parsable) numeric output.
2495 * -s Specify sort columns, descending order.
2496 * -S Specify sort columns, ascending order.
2497 * -t Control which object types to display.
2499 * Displays space consumed by, and quotas on, each user in the specified
2500 * filesystem or snapshot.
2503 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2504 enum us_field_types {
2512 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA",
2513 "OBJUSED", "OBJQUOTA" };
2514 static char *us_field_names[] = { "type", "name", "used", "quota",
2515 "objused", "objquota" };
2516 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2518 #define USTYPE_PSX_GRP (1 << 0)
2519 #define USTYPE_PSX_USR (1 << 1)
2520 #define USTYPE_SMB_GRP (1 << 2)
2521 #define USTYPE_SMB_USR (1 << 3)
2522 #define USTYPE_PROJ (1 << 4)
2523 #define USTYPE_ALL \
2524 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR | \
2527 static int us_type_bits[] = {
2534 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2537 typedef struct us_node {
2539 uu_avl_node_t usn_avlnode;
2540 uu_list_node_t usn_listnode;
2543 typedef struct us_cbdata {
2545 uu_avl_pool_t *cb_avl_pool;
2547 boolean_t cb_numname;
2548 boolean_t cb_nicenum;
2549 boolean_t cb_sid2posix;
2550 zfs_userquota_prop_t cb_prop;
2551 zfs_sort_column_t *cb_sortcol;
2552 size_t cb_width[USFIELD_LAST];
2555 static boolean_t us_populated = B_FALSE;
2558 zfs_sort_column_t *si_sortcol;
2559 boolean_t si_numname;
2563 us_field_index(char *field)
2567 for (i = 0; i < USFIELD_LAST; i++) {
2568 if (strcmp(field, us_field_names[i]) == 0)
2576 us_compare(const void *larg, const void *rarg, void *unused)
2578 const us_node_t *l = larg;
2579 const us_node_t *r = rarg;
2580 us_sort_info_t *si = (us_sort_info_t *)unused;
2581 zfs_sort_column_t *sortcol = si->si_sortcol;
2582 boolean_t numname = si->si_numname;
2583 nvlist_t *lnvl = l->usn_nvl;
2584 nvlist_t *rnvl = r->usn_nvl;
2588 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2595 zfs_prop_t prop = sortcol->sc_prop;
2596 const char *propname = NULL;
2597 boolean_t reverse = sortcol->sc_reverse;
2602 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2603 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2605 rc = (rv32 < lv32) ? 1 : -1;
2611 (void) nvlist_lookup_uint64(lnvl, propname,
2613 (void) nvlist_lookup_uint64(rnvl, propname,
2616 rc = (rv64 < lv64) ? 1 : -1;
2618 if ((nvlist_lookup_string(lnvl, propname,
2619 &lvstr) == ENOENT) ||
2620 (nvlist_lookup_string(rnvl, propname,
2621 &rvstr) == ENOENT)) {
2624 rc = strcmp(lvstr, rvstr);
2628 case ZFS_PROP_QUOTA:
2631 if (prop == ZFS_PROP_USED)
2635 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2636 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2638 rc = (rv64 < lv64) ? 1 : -1;
2647 return (reverse ? 1 : -1);
2649 return (reverse ? -1 : 1);
2654 * If entries still seem to be the same, check if they are of the same
2655 * type (smbentity is added only if we are doing SID to POSIX ID
2656 * translation where we can have duplicate type/name combinations).
2658 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2659 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2661 return (lvb < rvb ? -1 : 1);
2667 zfs_prop_is_user(unsigned p)
2669 return (p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA ||
2670 p == ZFS_PROP_USEROBJUSED || p == ZFS_PROP_USEROBJQUOTA);
2674 zfs_prop_is_group(unsigned p)
2676 return (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA ||
2677 p == ZFS_PROP_GROUPOBJUSED || p == ZFS_PROP_GROUPOBJQUOTA);
2681 zfs_prop_is_project(unsigned p)
2683 return (p == ZFS_PROP_PROJECTUSED || p == ZFS_PROP_PROJECTQUOTA ||
2684 p == ZFS_PROP_PROJECTOBJUSED || p == ZFS_PROP_PROJECTOBJQUOTA);
2687 static inline const char *
2688 us_type2str(unsigned field_type)
2690 switch (field_type) {
2691 case USTYPE_PSX_USR:
2692 return ("POSIX User");
2693 case USTYPE_PSX_GRP:
2694 return ("POSIX Group");
2695 case USTYPE_SMB_USR:
2696 return ("SMB User");
2697 case USTYPE_SMB_GRP:
2698 return ("SMB Group");
2702 return ("Undefined");
2707 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2709 us_cbdata_t *cb = (us_cbdata_t *)arg;
2710 zfs_userquota_prop_t prop = cb->cb_prop;
2715 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2716 uu_avl_t *avl = cb->cb_avl;
2720 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2722 const char *typestr;
2726 int typeidx, nameidx, sizeidx;
2727 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2728 boolean_t smbentity = B_FALSE;
2730 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2732 node = safe_malloc(sizeof (us_node_t));
2733 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2734 node->usn_nvl = props;
2736 if (domain != NULL && domain[0] != '\0') {
2739 char sid[MAXNAMELEN + 32];
2743 directory_error_t e;
2747 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2749 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2750 type = USTYPE_SMB_GRP;
2751 err = sid_to_id(sid, B_FALSE, &id);
2753 type = USTYPE_SMB_USR;
2754 err = sid_to_id(sid, B_TRUE, &id);
2759 if (!cb->cb_sid2posix) {
2760 e = directory_name_from_sid(NULL, sid, &name,
2763 directory_error_free(e);
2773 #endif /* HAVE_IDMAP */
2776 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2778 if (zfs_prop_is_group(prop)) {
2779 type = USTYPE_PSX_GRP;
2780 if (!cb->cb_numname) {
2783 if ((g = getgrgid(rid)) != NULL)
2786 } else if (zfs_prop_is_user(prop)) {
2787 type = USTYPE_PSX_USR;
2788 if (!cb->cb_numname) {
2791 if ((p = getpwuid(rid)) != NULL)
2800 * Make sure that the type/name combination is unique when doing
2801 * SID to POSIX ID translation (hence changing the type from SMB to
2804 if (cb->cb_sid2posix &&
2805 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2808 /* Calculate/update width of TYPE field */
2809 typestr = us_type2str(type);
2810 typelen = strlen(gettext(typestr));
2811 typeidx = us_field_index("type");
2812 if (typelen > cb->cb_width[typeidx])
2813 cb->cb_width[typeidx] = typelen;
2814 if (nvlist_add_uint32(props, "type", type) != 0)
2817 /* Calculate/update width of NAME field */
2818 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2819 if (nvlist_add_uint64(props, "name", rid) != 0)
2821 namelen = snprintf(NULL, 0, "%u", rid);
2823 if (nvlist_add_string(props, "name", name) != 0)
2825 namelen = strlen(name);
2827 nameidx = us_field_index("name");
2828 if (nameidx >= 0 && namelen > cb->cb_width[nameidx])
2829 cb->cb_width[nameidx] = namelen;
2832 * Check if this type/name combination is in the list and update it;
2833 * otherwise add new node to the list.
2835 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2836 uu_avl_insert(avl, node, idx);
2841 props = node->usn_nvl;
2844 /* Calculate/update width of USED/QUOTA fields */
2845 if (cb->cb_nicenum) {
2846 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED ||
2847 prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA ||
2848 prop == ZFS_PROP_PROJECTUSED ||
2849 prop == ZFS_PROP_PROJECTQUOTA) {
2850 zfs_nicebytes(space, sizebuf, sizeof (sizebuf));
2852 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2855 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu",
2856 (u_longlong_t)space);
2858 sizelen = strlen(sizebuf);
2859 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED ||
2860 prop == ZFS_PROP_PROJECTUSED) {
2862 if (!nvlist_exists(props, "quota"))
2863 (void) nvlist_add_uint64(props, "quota", 0);
2864 } else if (prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA ||
2865 prop == ZFS_PROP_PROJECTQUOTA) {
2867 if (!nvlist_exists(props, "used"))
2868 (void) nvlist_add_uint64(props, "used", 0);
2869 } else if (prop == ZFS_PROP_USEROBJUSED ||
2870 prop == ZFS_PROP_GROUPOBJUSED || prop == ZFS_PROP_PROJECTOBJUSED) {
2871 propname = "objused";
2872 if (!nvlist_exists(props, "objquota"))
2873 (void) nvlist_add_uint64(props, "objquota", 0);
2874 } else if (prop == ZFS_PROP_USEROBJQUOTA ||
2875 prop == ZFS_PROP_GROUPOBJQUOTA ||
2876 prop == ZFS_PROP_PROJECTOBJQUOTA) {
2877 propname = "objquota";
2878 if (!nvlist_exists(props, "objused"))
2879 (void) nvlist_add_uint64(props, "objused", 0);
2883 sizeidx = us_field_index(propname);
2884 if (sizeidx >= 0 && sizelen > cb->cb_width[sizeidx])
2885 cb->cb_width[sizeidx] = sizelen;
2887 if (nvlist_add_uint64(props, propname, space) != 0)
2894 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2895 size_t *width, us_node_t *node)
2897 nvlist_t *nvl = node->usn_nvl;
2898 char valstr[MAXNAMELEN];
2899 boolean_t first = B_TRUE;
2905 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2906 if (!(ustype & types))
2909 while ((field = fields[cfield]) != USFIELD_LAST) {
2910 nvpair_t *nvp = NULL;
2916 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2917 if (strcmp(nvpair_name(nvp),
2918 us_field_names[field]) == 0)
2922 type = nvp == NULL ? DATA_TYPE_UNKNOWN : nvpair_type(nvp);
2924 case DATA_TYPE_UINT32:
2925 (void) nvpair_value_uint32(nvp, &val32);
2927 case DATA_TYPE_UINT64:
2928 (void) nvpair_value_uint64(nvp, &val64);
2930 case DATA_TYPE_STRING:
2931 (void) nvpair_value_string(nvp, &strval);
2933 case DATA_TYPE_UNKNOWN:
2936 (void) fprintf(stderr, "invalid data type\n");
2941 if (type == DATA_TYPE_UINT32)
2942 strval = (char *)us_type2str(val32);
2945 if (type == DATA_TYPE_UINT64) {
2946 (void) sprintf(valstr, "%llu",
2947 (u_longlong_t)val64);
2953 if (type == DATA_TYPE_UINT64) {
2955 (void) sprintf(valstr, "%llu",
2956 (u_longlong_t)val64);
2958 } else if (field == USFIELD_QUOTA &&
2962 zfs_nicebytes(val64, valstr,
2968 case USFIELD_OBJUSED:
2969 case USFIELD_OBJQUOTA:
2970 if (type == DATA_TYPE_UINT64) {
2972 (void) sprintf(valstr, "%llu",
2973 (u_longlong_t)val64);
2975 } else if (field == USFIELD_OBJQUOTA &&
2979 zfs_nicenum(val64, valstr,
2989 (void) printf("\t");
2994 (void) printf("%s", strval);
2995 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2996 (void) printf("%-*s", (int)width[field], strval);
2998 (void) printf("%*s", (int)width[field], strval);
3004 (void) printf("\n");
3008 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
3009 size_t *width, boolean_t rmnode, uu_avl_t *avl)
3017 boolean_t first = B_TRUE;
3019 while ((field = fields[cfield]) != USFIELD_LAST) {
3020 col = gettext(us_field_hdr[field]);
3021 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
3022 (void) printf(first ? "%-*s" : " %-*s",
3023 (int)width[field], col);
3025 (void) printf(first ? "%*s" : " %*s",
3026 (int)width[field], col);
3031 (void) printf("\n");
3034 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
3035 print_us_node(scripted, parsable, fields, types, width, node);
3037 nvlist_free(node->usn_nvl);
3042 zfs_do_userspace(int argc, char **argv)
3045 zfs_userquota_prop_t p;
3046 uu_avl_pool_t *avl_pool;
3048 uu_avl_walk_t *walk;
3050 char deffields[] = "type,name,used,quota,objused,objquota";
3051 char *ofield = NULL;
3052 char *tfield = NULL;
3056 boolean_t scripted = B_FALSE;
3057 boolean_t prtnum = B_FALSE;
3058 boolean_t parsable = B_FALSE;
3059 boolean_t sid2posix = B_FALSE;
3062 zfs_sort_column_t *sortcol = NULL;
3063 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
3067 uu_list_pool_t *listpool;
3069 uu_avl_index_t idx = 0;
3070 uu_list_index_t idx2 = 0;
3075 if (strcmp(argv[0], "groupspace") == 0) {
3076 /* Toggle default group types */
3077 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
3078 } else if (strcmp(argv[0], "projectspace") == 0) {
3079 types = USTYPE_PROJ;
3083 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
3086 if (types == USTYPE_PROJ) {
3087 (void) fprintf(stderr,
3088 gettext("invalid option 'n'\n"));
3104 if (zfs_add_sort_column(&sortcol, optarg,
3105 c == 's' ? B_FALSE : B_TRUE) != 0) {
3106 (void) fprintf(stderr,
3107 gettext("invalid field '%s'\n"), optarg);
3112 if (types == USTYPE_PROJ) {
3113 (void) fprintf(stderr,
3114 gettext("invalid option 't'\n"));
3120 if (types == USTYPE_PROJ) {
3121 (void) fprintf(stderr,
3122 gettext("invalid option 'i'\n"));
3128 (void) fprintf(stderr, gettext("missing argument for "
3129 "'%c' option\n"), optopt);
3133 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3143 (void) fprintf(stderr, gettext("missing dataset name\n"));
3147 (void) fprintf(stderr, gettext("too many arguments\n"));
3151 /* Use default output fields if not specified using -o */
3155 if ((delim = strchr(ofield, ',')) != NULL)
3157 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
3158 (void) fprintf(stderr, gettext("invalid type '%s' "
3159 "for -o option\n"), ofield);
3164 } while (delim != NULL);
3165 fields[cfield] = USFIELD_LAST;
3167 /* Override output types (-t option) */
3168 if (tfield != NULL) {
3172 boolean_t found = B_FALSE;
3174 if ((delim = strchr(tfield, ',')) != NULL)
3176 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
3178 if (strcmp(tfield, us_type_names[i]) == 0) {
3180 types |= us_type_bits[i];
3185 (void) fprintf(stderr, gettext("invalid type "
3186 "'%s' for -t option\n"), tfield);
3191 } while (delim != NULL);
3194 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM |
3195 ZFS_TYPE_SNAPSHOT)) == NULL)
3197 if (zhp->zfs_head_type != ZFS_TYPE_FILESYSTEM) {
3198 (void) fprintf(stderr, gettext("operation is only applicable "
3199 "to filesystems and their snapshots\n"));
3204 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
3205 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
3207 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
3210 /* Always add default sorting columns */
3211 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
3212 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
3214 cb.cb_sortcol = sortcol;
3215 cb.cb_numname = prtnum;
3216 cb.cb_nicenum = !parsable;
3217 cb.cb_avl_pool = avl_pool;
3218 cb.cb_avl = avl_tree;
3219 cb.cb_sid2posix = sid2posix;
3221 for (i = 0; i < USFIELD_LAST; i++)
3222 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
3224 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
3225 if ((zfs_prop_is_user(p) &&
3226 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
3227 (zfs_prop_is_group(p) &&
3228 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))) ||
3229 (zfs_prop_is_project(p) && types != USTYPE_PROJ))
3233 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0) {
3241 if ((node = uu_avl_first(avl_tree)) == NULL)
3244 us_populated = B_TRUE;
3246 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
3247 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
3248 list = uu_list_create(listpool, NULL, UU_DEFAULT);
3249 uu_list_node_init(node, &node->usn_listnode, listpool);
3251 while (node != NULL) {
3253 node = uu_avl_next(avl_tree, node);
3254 uu_avl_remove(avl_tree, rmnode);
3255 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
3256 uu_list_insert(list, rmnode, idx2);
3259 for (node = uu_list_first(list); node != NULL;
3260 node = uu_list_next(list, node)) {
3261 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
3263 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
3264 uu_avl_insert(avl_tree, node, idx);
3267 uu_list_destroy(list);
3268 uu_list_pool_destroy(listpool);
3270 /* Print and free node nvlist memory */
3271 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
3274 zfs_free_sort_columns(sortcol);
3276 /* Clean up the AVL tree */
3277 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
3280 while ((node = uu_avl_walk_next(walk)) != NULL) {
3281 uu_avl_remove(cb.cb_avl, node);
3285 uu_avl_walk_end(walk);
3286 uu_avl_destroy(avl_tree);
3287 uu_avl_pool_destroy(avl_pool);
3293 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
3294 * [-t type[,...]] [filesystem|volume|snapshot] ...
3296 * -H Scripted mode; elide headers and separate columns by tabs
3297 * -p Display values in parsable (literal) format.
3298 * -r Recurse over all children
3299 * -d Limit recursion by depth.
3300 * -o Control which fields to display.
3301 * -s Specify sort columns, descending order.
3302 * -S Specify sort columns, ascending order.
3303 * -t Control which object types to display.
3305 * When given no arguments, list all filesystems in the system.
3306 * Otherwise, list the specified datasets, optionally recursing down them if
3307 * '-r' is specified.
3309 typedef struct list_cbdata {
3311 boolean_t cb_literal;
3312 boolean_t cb_scripted;
3313 zprop_list_t *cb_proplist;
3317 * Given a list of columns to display, output appropriate headers for each one.
3320 print_header(list_cbdata_t *cb)
3322 zprop_list_t *pl = cb->cb_proplist;
3323 char headerbuf[ZFS_MAXPROPLEN];
3326 boolean_t first = B_TRUE;
3327 boolean_t right_justify;
3329 for (; pl != NULL; pl = pl->pl_next) {
3336 right_justify = B_FALSE;
3337 if (pl->pl_prop != ZPROP_INVAL) {
3338 header = zfs_prop_column_name(pl->pl_prop);
3339 right_justify = zfs_prop_align_right(pl->pl_prop);
3341 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
3342 headerbuf[i] = toupper(pl->pl_user_prop[i]);
3343 headerbuf[i] = '\0';
3347 if (pl->pl_next == NULL && !right_justify)
3348 (void) printf("%s", header);
3349 else if (right_justify)
3350 (void) printf("%*s", (int)pl->pl_width, header);
3352 (void) printf("%-*s", (int)pl->pl_width, header);
3355 (void) printf("\n");
3359 * Given a dataset and a list of fields, print out all the properties according
3360 * to the described layout.
3363 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
3365 zprop_list_t *pl = cb->cb_proplist;
3366 boolean_t first = B_TRUE;
3367 char property[ZFS_MAXPROPLEN];
3368 nvlist_t *userprops = zfs_get_user_props(zhp);
3371 boolean_t right_justify;
3373 for (; pl != NULL; pl = pl->pl_next) {
3375 if (cb->cb_scripted)
3376 (void) printf("\t");
3383 if (pl->pl_prop == ZFS_PROP_NAME) {
3384 (void) strlcpy(property, zfs_get_name(zhp),
3387 right_justify = zfs_prop_align_right(pl->pl_prop);
3388 } else if (pl->pl_prop != ZPROP_INVAL) {
3389 if (zfs_prop_get(zhp, pl->pl_prop, property,
3390 sizeof (property), NULL, NULL, 0,
3391 cb->cb_literal) != 0)
3395 right_justify = zfs_prop_align_right(pl->pl_prop);
3396 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3397 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3398 property, sizeof (property), cb->cb_literal) != 0)
3402 right_justify = B_TRUE;
3403 } else if (zfs_prop_written(pl->pl_user_prop)) {
3404 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3405 property, sizeof (property), cb->cb_literal) != 0)
3409 right_justify = B_TRUE;
3411 if (nvlist_lookup_nvlist(userprops,
3412 pl->pl_user_prop, &propval) != 0)
3415 verify(nvlist_lookup_string(propval,
3416 ZPROP_VALUE, &propstr) == 0);
3417 right_justify = B_FALSE;
3421 * If this is being called in scripted mode, or if this is the
3422 * last column and it is left-justified, don't include a width
3425 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3426 (void) printf("%s", propstr);
3427 else if (right_justify)
3428 (void) printf("%*s", (int)pl->pl_width, propstr);
3430 (void) printf("%-*s", (int)pl->pl_width, propstr);
3433 (void) printf("\n");
3437 * Generic callback function to list a dataset or snapshot.
3440 list_callback(zfs_handle_t *zhp, void *data)
3442 list_cbdata_t *cbp = data;
3444 if (cbp->cb_first) {
3445 if (!cbp->cb_scripted)
3447 cbp->cb_first = B_FALSE;
3450 print_dataset(zhp, cbp);
3456 zfs_do_list(int argc, char **argv)
3459 static char default_fields[] =
3460 "name,used,available,referenced,mountpoint";
3461 int types = ZFS_TYPE_DATASET;
3462 boolean_t types_specified = B_FALSE;
3463 char *fields = NULL;
3464 list_cbdata_t cb = { 0 };
3468 zfs_sort_column_t *sortcol = NULL;
3469 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3472 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3478 cb.cb_literal = B_TRUE;
3479 flags |= ZFS_ITER_LITERAL_PROPS;
3482 limit = parse_depth(optarg, &flags);
3485 flags |= ZFS_ITER_RECURSE;
3488 cb.cb_scripted = B_TRUE;
3491 if (zfs_add_sort_column(&sortcol, optarg,
3493 (void) fprintf(stderr,
3494 gettext("invalid property '%s'\n"), optarg);
3499 if (zfs_add_sort_column(&sortcol, optarg,
3501 (void) fprintf(stderr,
3502 gettext("invalid property '%s'\n"), optarg);
3508 types_specified = B_TRUE;
3509 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3510 while (*optarg != '\0') {
3511 static char *type_subopts[] = { "filesystem",
3512 "volume", "snapshot", "snap", "bookmark",
3515 switch (getsubopt(&optarg, type_subopts,
3518 types |= ZFS_TYPE_FILESYSTEM;
3521 types |= ZFS_TYPE_VOLUME;
3525 types |= ZFS_TYPE_SNAPSHOT;
3528 types |= ZFS_TYPE_BOOKMARK;
3531 types = ZFS_TYPE_DATASET |
3535 (void) fprintf(stderr,
3536 gettext("invalid type '%s'\n"),
3543 (void) fprintf(stderr, gettext("missing argument for "
3544 "'%c' option\n"), optopt);
3548 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3558 fields = default_fields;
3561 * If we are only going to list snapshot names and sort by name,
3562 * then we can use faster version.
3564 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3565 flags |= ZFS_ITER_SIMPLE;
3568 * If "-o space" and no types were specified, don't display snapshots.
3570 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3571 types &= ~ZFS_TYPE_SNAPSHOT;
3574 * Handle users who want to list all snapshots or bookmarks
3575 * of the current dataset (ex. 'zfs list -t snapshot <dataset>').
3577 if ((types == ZFS_TYPE_SNAPSHOT || types == ZFS_TYPE_BOOKMARK) &&
3578 argc > 0 && (flags & ZFS_ITER_RECURSE) == 0 && limit == 0) {
3579 flags |= (ZFS_ITER_DEPTH_LIMIT | ZFS_ITER_RECURSE);
3584 * If the user specifies '-o all', the zprop_get_list() doesn't
3585 * normally include the name of the dataset. For 'zfs list', we always
3586 * want this property to be first.
3588 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3592 cb.cb_first = B_TRUE;
3594 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3595 limit, list_callback, &cb);
3597 zprop_free_list(cb.cb_proplist);
3598 zfs_free_sort_columns(sortcol);
3600 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3601 (void) fprintf(stderr, gettext("no datasets available\n"));
3607 * zfs rename [-fu] <fs | snap | vol> <fs | snap | vol>
3608 * zfs rename [-f] -p <fs | vol> <fs | vol>
3609 * zfs rename [-u] -r <snap> <snap>
3611 * Renames the given dataset to another of the same type.
3613 * The '-p' flag creates all the non-existing ancestors of the target first.
3614 * The '-u' flag prevents file systems from being remounted during rename.
3618 zfs_do_rename(int argc, char **argv)
3621 renameflags_t flags = { 0 };
3625 boolean_t parents = B_FALSE;
3628 while ((c = getopt(argc, argv, "pruf")) != -1) {
3634 flags.recursive = B_TRUE;
3637 flags.nounmount = B_TRUE;
3640 flags.forceunmount = B_TRUE;
3644 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3653 /* check number of arguments */
3655 (void) fprintf(stderr, gettext("missing source dataset "
3660 (void) fprintf(stderr, gettext("missing target dataset "
3665 (void) fprintf(stderr, gettext("too many arguments\n"));
3669 if (flags.recursive && parents) {
3670 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3675 if (flags.nounmount && parents) {
3676 (void) fprintf(stderr, gettext("-u and -p options are mutually "
3681 if (flags.recursive && strchr(argv[0], '@') == 0) {
3682 (void) fprintf(stderr, gettext("source dataset for recursive "
3683 "rename must be a snapshot\n"));
3687 if (flags.nounmount)
3688 types = ZFS_TYPE_FILESYSTEM;
3690 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3692 types = ZFS_TYPE_DATASET;
3694 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3697 /* If we were asked and the name looks good, try to create ancestors. */
3698 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3699 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3704 ret = (zfs_rename(zhp, argv[1], flags) != 0);
3713 * Promotes the given clone fs to be the parent
3717 zfs_do_promote(int argc, char **argv)
3723 if (argc > 1 && argv[1][0] == '-') {
3724 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3729 /* check number of arguments */
3731 (void) fprintf(stderr, gettext("missing clone filesystem"
3736 (void) fprintf(stderr, gettext("too many arguments\n"));
3740 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3744 ret = (zfs_promote(zhp) != 0);
3752 zfs_do_redact(int argc, char **argv)
3755 char *bookname = NULL;
3756 char **rsnaps = NULL;
3761 (void) fprintf(stderr, gettext("too few arguments\n"));
3768 numrsnaps = argc - 2;
3770 nvlist_t *rsnapnv = fnvlist_alloc();
3772 for (int i = 0; i < numrsnaps; i++) {
3773 fnvlist_add_boolean(rsnapnv, rsnaps[i]);
3776 int err = lzc_redact(snap, bookname, rsnapnv);
3777 fnvlist_free(rsnapnv);
3783 (void) fprintf(stderr,
3784 gettext("provided snapshot %s does not exist\n"), snap);
3787 (void) fprintf(stderr, gettext("specified redaction bookmark "
3788 "(%s) provided already exists\n"), bookname);
3791 (void) fprintf(stderr, gettext("provided bookmark name cannot "
3792 "be used, final name would be too long\n"));
3795 (void) fprintf(stderr, gettext("too many redaction snapshots "
3799 if (strchr(bookname, '#') != NULL)
3800 (void) fprintf(stderr, gettext(
3801 "redaction bookmark name must not contain '#'\n"));
3803 (void) fprintf(stderr, gettext(
3804 "redaction snapshot must be descendent of "
3805 "snapshot being redacted\n"));
3808 (void) fprintf(stderr, gettext("attempted to redact redacted "
3809 "dataset or with respect to redacted dataset\n"));
3812 (void) fprintf(stderr, gettext("redaction bookmarks feature "
3816 (void) fprintf(stderr, gettext("potentially invalid redaction "
3817 "snapshot; full dataset names required\n"));
3820 (void) fprintf(stderr, gettext("internal error: %s\n"),
3828 * zfs rollback [-rRf] <snapshot>
3830 * -r Delete any intervening snapshots before doing rollback
3831 * -R Delete any snapshots and their clones
3832 * -f ignored for backwards compatibility
3834 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3835 * since then and making it the active dataset. If more recent snapshots exist,
3836 * the command will complain unless the '-r' flag is given.
3838 typedef struct rollback_cbdata {
3840 uint8_t cb_younger_ds_printed;
3845 boolean_t cb_recurse;
3846 } rollback_cbdata_t;
3849 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3851 rollback_cbdata_t *cbp = data;
3853 if (cbp->cb_first && cbp->cb_recurse) {
3854 (void) fprintf(stderr, gettext("cannot rollback to "
3855 "'%s': clones of previous snapshots exist\n"),
3857 (void) fprintf(stderr, gettext("use '-R' to "
3858 "force deletion of the following clones and "
3864 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3872 * Report some snapshots/bookmarks more recent than the one specified.
3873 * Used when '-r' is not specified. We reuse this same callback for the
3874 * snapshot dependents - if 'cb_dependent' is set, then this is a
3875 * dependent and we should report it without checking the transaction group.
3878 rollback_check(zfs_handle_t *zhp, void *data)
3880 rollback_cbdata_t *cbp = data;
3882 * Max number of younger snapshots and/or bookmarks to display before
3883 * we stop the iteration.
3885 const uint8_t max_younger = 32;
3887 if (cbp->cb_doclones) {
3892 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3893 if (cbp->cb_first && !cbp->cb_recurse) {
3894 (void) fprintf(stderr, gettext("cannot "
3895 "rollback to '%s': more recent snapshots "
3896 "or bookmarks exist\n"),
3898 (void) fprintf(stderr, gettext("use '-r' to "
3899 "force deletion of the following "
3900 "snapshots and bookmarks:\n"));
3905 if (cbp->cb_recurse) {
3906 if (zfs_iter_dependents(zhp, B_TRUE,
3907 rollback_check_dependent, cbp) != 0) {
3912 (void) fprintf(stderr, "%s\n",
3914 cbp->cb_younger_ds_printed++;
3919 if (cbp->cb_younger_ds_printed == max_younger) {
3921 * This non-recursive rollback is going to fail due to the
3922 * presence of snapshots and/or bookmarks that are younger than
3923 * the rollback target.
3924 * We printed some of the offending objects, now we stop
3925 * zfs_iter_snapshot/bookmark iteration so we can fail fast and
3926 * avoid iterating over the rest of the younger objects
3928 (void) fprintf(stderr, gettext("Output limited to %d "
3929 "snapshots/bookmarks\n"), max_younger);
3936 zfs_do_rollback(int argc, char **argv)
3940 boolean_t force = B_FALSE;
3941 rollback_cbdata_t cb = { 0 };
3942 zfs_handle_t *zhp, *snap;
3943 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3945 uint64_t min_txg = 0;
3948 while ((c = getopt(argc, argv, "rRf")) != -1) {
3961 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3970 /* check number of arguments */
3972 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3976 (void) fprintf(stderr, gettext("too many arguments\n"));
3980 /* open the snapshot */
3981 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3984 /* open the parent dataset */
3985 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3986 verify((delim = strrchr(parentname, '@')) != NULL);
3988 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3994 * Check for more recent snapshots and/or clones based on the presence
3997 cb.cb_target = argv[0];
3998 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3999 cb.cb_first = B_TRUE;
4002 if (cb.cb_create > 0)
4003 min_txg = cb.cb_create;
4005 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb,
4008 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
4011 if ((ret = cb.cb_error) != 0)
4015 * Rollback parent to the given snapshot.
4017 ret = zfs_rollback(zhp, snap, force);
4030 * zfs set property=value ... { fs | snap | vol } ...
4032 * Sets the given properties for all datasets specified on the command line.
4036 set_callback(zfs_handle_t *zhp, void *data)
4038 nvlist_t *props = data;
4040 if (zfs_prop_set_list(zhp, props) != 0) {
4041 switch (libzfs_errno(g_zfs)) {
4042 case EZFS_MOUNTFAILED:
4043 (void) fprintf(stderr, gettext("property may be set "
4044 "but unable to remount filesystem\n"));
4046 case EZFS_SHARENFSFAILED:
4047 (void) fprintf(stderr, gettext("property may be set "
4048 "but unable to reshare filesystem\n"));
4057 zfs_do_set(int argc, char **argv)
4059 nvlist_t *props = NULL;
4060 int ds_start = -1; /* argv idx of first dataset arg */
4064 /* check for options */
4065 if (argc > 1 && argv[1][0] == '-') {
4066 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4071 /* check number of arguments */
4073 (void) fprintf(stderr, gettext("missing arguments\n"));
4077 if (strchr(argv[1], '=') == NULL) {
4078 (void) fprintf(stderr, gettext("missing property=value "
4081 (void) fprintf(stderr, gettext("missing dataset "
4087 /* validate argument order: prop=val args followed by dataset args */
4088 for (i = 1; i < argc; i++) {
4089 if (strchr(argv[i], '=') != NULL) {
4091 /* out-of-order prop=val argument */
4092 (void) fprintf(stderr, gettext("invalid "
4093 "argument order\n"));
4096 } else if (ds_start < 0) {
4101 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
4105 /* Populate a list of property settings */
4106 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4108 for (i = 1; i < ds_start; i++) {
4109 if (!parseprop(props, argv[i])) {
4115 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
4116 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
4123 typedef struct snap_cbdata {
4125 boolean_t sd_recursive;
4126 const char *sd_snapname;
4130 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
4132 snap_cbdata_t *sd = arg;
4137 if (sd->sd_recursive &&
4138 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
4143 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
4146 fnvlist_add_boolean(sd->sd_nvl, name);
4149 if (sd->sd_recursive)
4150 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
4156 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
4158 * Creates a snapshot with the given name. While functionally equivalent to
4159 * 'zfs create', it is a separate command to differentiate intent.
4162 zfs_do_snapshot(int argc, char **argv)
4167 snap_cbdata_t sd = { 0 };
4168 boolean_t multiple_snaps = B_FALSE;
4170 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4172 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
4176 while ((c = getopt(argc, argv, "ro:")) != -1) {
4179 if (!parseprop(props, optarg)) {
4180 nvlist_free(sd.sd_nvl);
4186 sd.sd_recursive = B_TRUE;
4187 multiple_snaps = B_TRUE;
4190 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4199 /* check number of arguments */
4201 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4206 multiple_snaps = B_TRUE;
4207 for (; argc > 0; argc--, argv++) {
4211 atp = strchr(argv[0], '@');
4215 sd.sd_snapname = atp + 1;
4216 zhp = zfs_open(g_zfs, argv[0],
4217 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4220 if (zfs_snapshot_cb(zhp, &sd) != 0)
4224 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
4225 nvlist_free(sd.sd_nvl);
4227 if (ret != 0 && multiple_snaps)
4228 (void) fprintf(stderr, gettext("no snapshots were created\n"));
4232 nvlist_free(sd.sd_nvl);
4240 * Send a backup stream to stdout.
4243 zfs_do_send(int argc, char **argv)
4245 char *fromname = NULL;
4246 char *toname = NULL;
4247 char *resume_token = NULL;
4250 sendflags_t flags = { 0 };
4252 nvlist_t *dbgnv = NULL;
4253 char *redactbook = NULL;
4255 struct option long_options[] = {
4256 {"replicate", no_argument, NULL, 'R'},
4257 {"redact", required_argument, NULL, 'd'},
4258 {"props", no_argument, NULL, 'p'},
4259 {"parsable", no_argument, NULL, 'P'},
4260 {"dedup", no_argument, NULL, 'D'},
4261 {"verbose", no_argument, NULL, 'v'},
4262 {"dryrun", no_argument, NULL, 'n'},
4263 {"large-block", no_argument, NULL, 'L'},
4264 {"embed", no_argument, NULL, 'e'},
4265 {"resume", required_argument, NULL, 't'},
4266 {"compressed", no_argument, NULL, 'c'},
4267 {"raw", no_argument, NULL, 'w'},
4268 {"backup", no_argument, NULL, 'b'},
4269 {"holds", no_argument, NULL, 'h'},
4270 {"saved", no_argument, NULL, 'S'},
4275 while ((c = getopt_long(argc, argv, ":i:I:RDpvnPLeht:cwbd:S",
4276 long_options, NULL)) != -1) {
4287 flags.doall = B_TRUE;
4290 flags.replicate = B_TRUE;
4293 redactbook = optarg;
4296 flags.props = B_TRUE;
4299 flags.backup = B_TRUE;
4302 flags.holds = B_TRUE;
4305 flags.parsable = B_TRUE;
4309 flags.progress = B_TRUE;
4312 (void) fprintf(stderr,
4313 gettext("WARNING: deduplicated send is no "
4314 "longer supported. A regular,\n"
4315 "non-deduplicated stream will be generated.\n\n"));
4318 flags.dryrun = B_TRUE;
4321 flags.largeblock = B_TRUE;
4324 flags.embed_data = B_TRUE;
4327 resume_token = optarg;
4330 flags.compress = B_TRUE;
4334 flags.compress = B_TRUE;
4335 flags.embed_data = B_TRUE;
4336 flags.largeblock = B_TRUE;
4339 flags.saved = B_TRUE;
4343 * If a parameter was not passed, optopt contains the
4344 * value that would normally lead us into the
4345 * appropriate case statement. If it's > 256, then this
4346 * must be a longopt and we should look at argv to get
4347 * the string. Otherwise it's just the character, so we
4348 * should use it directly.
4350 if (optopt <= UINT8_MAX) {
4351 (void) fprintf(stderr,
4352 gettext("missing argument for '%c' "
4353 "option\n"), optopt);
4355 (void) fprintf(stderr,
4356 gettext("missing argument for '%s' "
4357 "option\n"), argv[optind - 1]);
4365 * If an invalid flag was passed, optopt contains the
4366 * character if it was a short flag, or 0 if it was a
4370 (void) fprintf(stderr,
4371 gettext("invalid option '%c'\n"), optopt);
4373 (void) fprintf(stderr,
4374 gettext("invalid option '%s'\n"),
4382 if (flags.parsable && flags.verbosity == 0)
4383 flags.verbosity = 1;
4388 if (resume_token != NULL) {
4389 if (fromname != NULL || flags.replicate || flags.props ||
4390 flags.backup || flags.holds ||
4391 flags.saved || redactbook != NULL) {
4392 (void) fprintf(stderr,
4393 gettext("invalid flags combined with -t\n"));
4397 (void) fprintf(stderr, gettext("too many arguments\n"));
4402 (void) fprintf(stderr,
4403 gettext("missing snapshot argument\n"));
4407 (void) fprintf(stderr, gettext("too many arguments\n"));
4413 if (fromname != NULL || flags.replicate || flags.props ||
4414 flags.doall || flags.backup ||
4415 flags.holds || flags.largeblock || flags.embed_data ||
4416 flags.compress || flags.raw || redactbook != NULL) {
4417 (void) fprintf(stderr, gettext("incompatible flags "
4418 "combined with saved send flag\n"));
4421 if (strchr(argv[0], '@') != NULL) {
4422 (void) fprintf(stderr, gettext("saved send must "
4423 "specify the dataset with partially-received "
4429 if (flags.raw && redactbook != NULL) {
4430 (void) fprintf(stderr,
4431 gettext("Error: raw sends may not be redacted.\n"));
4435 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
4436 (void) fprintf(stderr,
4437 gettext("Error: Stream can not be written to a terminal.\n"
4438 "You must redirect standard output.\n"));
4443 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
4447 err = zfs_send_saved(zhp, &flags, STDOUT_FILENO,
4451 } else if (resume_token != NULL) {
4452 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
4457 * For everything except -R and -I, use the new, cleaner code path.
4459 if (!(flags.replicate || flags.doall)) {
4460 char frombuf[ZFS_MAX_DATASET_NAME_LEN];
4462 if (fromname != NULL && (strchr(fromname, '#') == NULL &&
4463 strchr(fromname, '@') == NULL)) {
4465 * Neither bookmark or snapshot was specified. Print a
4466 * warning, and assume snapshot.
4468 (void) fprintf(stderr, "Warning: incremental source "
4469 "didn't specify type, assuming snapshot. Use '@' "
4470 "or '#' prefix to avoid ambiguity.\n");
4471 (void) snprintf(frombuf, sizeof (frombuf), "@%s",
4475 if (fromname != NULL &&
4476 (fromname[0] == '#' || fromname[0] == '@')) {
4478 * Incremental source name begins with # or @.
4479 * Default to same fs as target.
4481 char tmpbuf[ZFS_MAX_DATASET_NAME_LEN];
4482 (void) strlcpy(tmpbuf, fromname, sizeof (tmpbuf));
4483 (void) strlcpy(frombuf, argv[0], sizeof (frombuf));
4484 cp = strchr(frombuf, '@');
4487 (void) strlcat(frombuf, tmpbuf, sizeof (frombuf));
4491 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
4494 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, &flags,
4500 if (fromname != NULL && strchr(fromname, '#')) {
4501 (void) fprintf(stderr,
4502 gettext("Error: multiple snapshots cannot be "
4503 "sent from a bookmark.\n"));
4507 if (redactbook != NULL) {
4508 (void) fprintf(stderr, gettext("Error: multiple snapshots "
4509 "cannot be sent redacted.\n"));
4513 if ((cp = strchr(argv[0], '@')) == NULL) {
4514 (void) fprintf(stderr, gettext("Error: "
4515 "Unsupported flag with filesystem or bookmark.\n"));
4520 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4525 * If they specified the full path to the snapshot, chop off
4526 * everything except the short name of the snapshot, but special
4527 * case if they specify the origin.
4529 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
4530 char origin[ZFS_MAX_DATASET_NAME_LEN];
4533 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
4534 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
4536 if (strcmp(origin, fromname) == 0) {
4538 flags.fromorigin = B_TRUE;
4541 if (cp != fromname && strcmp(argv[0], fromname)) {
4542 (void) fprintf(stderr,
4543 gettext("incremental source must be "
4544 "in same filesystem\n"));
4548 if (strchr(fromname, '@') || strchr(fromname, '/')) {
4549 (void) fprintf(stderr,
4550 gettext("invalid incremental source\n"));
4556 if (flags.replicate && fromname == NULL)
4557 flags.doall = B_TRUE;
4559 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
4560 flags.verbosity >= 3 ? &dbgnv : NULL);
4562 if (flags.verbosity >= 3 && dbgnv != NULL) {
4564 * dump_nvlist prints to stdout, but that's been
4565 * redirected to a file. Make it print to stderr
4568 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
4569 dump_nvlist(dbgnv, 0);
4578 * Restore a backup stream from stdin.
4581 zfs_do_receive(int argc, char **argv)
4584 recvflags_t flags = { 0 };
4585 boolean_t abort_resumable = B_FALSE;
4588 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4592 while ((c = getopt(argc, argv, ":o:x:dehMnuvFsA")) != -1) {
4595 if (!parseprop(props, optarg)) {
4601 if (!parsepropname(props, optarg)) {
4608 (void) fprintf(stderr, gettext("invalid option "
4609 "combination: -d and -e are mutually "
4613 flags.isprefix = B_TRUE;
4616 if (flags.isprefix) {
4617 (void) fprintf(stderr, gettext("invalid option "
4618 "combination: -d and -e are mutually "
4622 flags.istail = B_TRUE;
4625 flags.skipholds = B_TRUE;
4628 flags.forceunmount = B_TRUE;
4631 flags.dryrun = B_TRUE;
4634 flags.nomount = B_TRUE;
4637 flags.verbose = B_TRUE;
4640 flags.resumable = B_TRUE;
4643 flags.force = B_TRUE;
4646 abort_resumable = B_TRUE;
4649 (void) fprintf(stderr, gettext("missing argument for "
4650 "'%c' option\n"), optopt);
4654 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4663 /* zfs recv -e (use "tail" name) implies -d (remove dataset "head") */
4665 flags.isprefix = B_TRUE;
4667 /* check number of arguments */
4669 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4673 (void) fprintf(stderr, gettext("too many arguments\n"));
4677 if (abort_resumable) {
4678 if (flags.isprefix || flags.istail || flags.dryrun ||
4679 flags.resumable || flags.nomount) {
4680 (void) fprintf(stderr, gettext("invalid option\n"));
4684 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4685 (void) snprintf(namebuf, sizeof (namebuf),
4686 "%s/%%recv", argv[0]);
4688 if (zfs_dataset_exists(g_zfs, namebuf,
4689 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4690 zfs_handle_t *zhp = zfs_open(g_zfs,
4691 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4696 err = zfs_destroy(zhp, B_FALSE);
4699 zfs_handle_t *zhp = zfs_open(g_zfs,
4700 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4703 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4704 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4705 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4706 (void) fprintf(stderr,
4707 gettext("'%s' does not have any "
4708 "resumable receive state to abort\n"),
4714 err = zfs_destroy(zhp, B_FALSE);
4721 if (isatty(STDIN_FILENO)) {
4722 (void) fprintf(stderr,
4723 gettext("Error: Backup stream can not be read "
4724 "from a terminal.\n"
4725 "You must redirect standard input.\n"));
4729 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4736 * allow/unallow stuff
4738 /* copied from zfs/sys/dsl_deleg.h */
4739 #define ZFS_DELEG_PERM_CREATE "create"
4740 #define ZFS_DELEG_PERM_DESTROY "destroy"
4741 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4742 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4743 #define ZFS_DELEG_PERM_CLONE "clone"
4744 #define ZFS_DELEG_PERM_PROMOTE "promote"
4745 #define ZFS_DELEG_PERM_RENAME "rename"
4746 #define ZFS_DELEG_PERM_MOUNT "mount"
4747 #define ZFS_DELEG_PERM_SHARE "share"
4748 #define ZFS_DELEG_PERM_SEND "send"
4749 #define ZFS_DELEG_PERM_RECEIVE "receive"
4750 #define ZFS_DELEG_PERM_ALLOW "allow"
4751 #define ZFS_DELEG_PERM_USERPROP "userprop"
4752 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4753 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4754 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4755 #define ZFS_DELEG_PERM_USERUSED "userused"
4756 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4757 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
4758 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
4759 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
4760 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
4762 #define ZFS_DELEG_PERM_HOLD "hold"
4763 #define ZFS_DELEG_PERM_RELEASE "release"
4764 #define ZFS_DELEG_PERM_DIFF "diff"
4765 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4766 #define ZFS_DELEG_PERM_LOAD_KEY "load-key"
4767 #define ZFS_DELEG_PERM_CHANGE_KEY "change-key"
4769 #define ZFS_DELEG_PERM_PROJECTUSED "projectused"
4770 #define ZFS_DELEG_PERM_PROJECTQUOTA "projectquota"
4771 #define ZFS_DELEG_PERM_PROJECTOBJUSED "projectobjused"
4772 #define ZFS_DELEG_PERM_PROJECTOBJQUOTA "projectobjquota"
4774 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4776 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4777 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4778 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4779 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4780 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4781 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4782 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4783 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4784 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4785 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4786 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4787 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4788 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4789 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4790 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4791 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4792 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4793 { ZFS_DELEG_PERM_LOAD_KEY, ZFS_DELEG_NOTE_LOAD_KEY },
4794 { ZFS_DELEG_PERM_CHANGE_KEY, ZFS_DELEG_NOTE_CHANGE_KEY },
4796 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4797 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4798 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4799 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4800 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4801 { ZFS_DELEG_PERM_USEROBJQUOTA, ZFS_DELEG_NOTE_USEROBJQUOTA },
4802 { ZFS_DELEG_PERM_USEROBJUSED, ZFS_DELEG_NOTE_USEROBJUSED },
4803 { ZFS_DELEG_PERM_GROUPOBJQUOTA, ZFS_DELEG_NOTE_GROUPOBJQUOTA },
4804 { ZFS_DELEG_PERM_GROUPOBJUSED, ZFS_DELEG_NOTE_GROUPOBJUSED },
4805 { ZFS_DELEG_PERM_PROJECTUSED, ZFS_DELEG_NOTE_PROJECTUSED },
4806 { ZFS_DELEG_PERM_PROJECTQUOTA, ZFS_DELEG_NOTE_PROJECTQUOTA },
4807 { ZFS_DELEG_PERM_PROJECTOBJUSED, ZFS_DELEG_NOTE_PROJECTOBJUSED },
4808 { ZFS_DELEG_PERM_PROJECTOBJQUOTA, ZFS_DELEG_NOTE_PROJECTOBJQUOTA },
4809 { NULL, ZFS_DELEG_NOTE_NONE }
4812 /* permission structure */
4813 typedef struct deleg_perm {
4814 zfs_deleg_who_type_t dp_who_type;
4815 const char *dp_name;
4817 boolean_t dp_descend;
4821 typedef struct deleg_perm_node {
4822 deleg_perm_t dpn_perm;
4824 uu_avl_node_t dpn_avl_node;
4825 } deleg_perm_node_t;
4827 typedef struct fs_perm fs_perm_t;
4829 /* permissions set */
4830 typedef struct who_perm {
4831 zfs_deleg_who_type_t who_type;
4832 const char *who_name; /* id */
4833 char who_ug_name[256]; /* user/group name */
4834 fs_perm_t *who_fsperm; /* uplink */
4836 uu_avl_t *who_deleg_perm_avl; /* permissions */
4840 typedef struct who_perm_node {
4841 who_perm_t who_perm;
4842 uu_avl_node_t who_avl_node;
4845 typedef struct fs_perm_set fs_perm_set_t;
4846 /* fs permissions */
4848 const char *fsp_name;
4850 uu_avl_t *fsp_sc_avl; /* sets,create */
4851 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4853 fs_perm_set_t *fsp_set; /* uplink */
4857 typedef struct fs_perm_node {
4858 fs_perm_t fspn_fsperm;
4861 uu_list_node_t fspn_list_node;
4864 /* top level structure */
4865 struct fs_perm_set {
4866 uu_list_pool_t *fsps_list_pool;
4867 uu_list_t *fsps_list; /* list of fs_perms */
4869 uu_avl_pool_t *fsps_named_set_avl_pool;
4870 uu_avl_pool_t *fsps_who_perm_avl_pool;
4871 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4874 static inline const char *
4875 deleg_perm_type(zfs_deleg_note_t note)
4881 case ZFS_DELEG_NOTE_GROUPQUOTA:
4882 case ZFS_DELEG_NOTE_GROUPUSED:
4883 case ZFS_DELEG_NOTE_USERPROP:
4884 case ZFS_DELEG_NOTE_USERQUOTA:
4885 case ZFS_DELEG_NOTE_USERUSED:
4886 case ZFS_DELEG_NOTE_USEROBJQUOTA:
4887 case ZFS_DELEG_NOTE_USEROBJUSED:
4888 case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
4889 case ZFS_DELEG_NOTE_GROUPOBJUSED:
4890 case ZFS_DELEG_NOTE_PROJECTUSED:
4891 case ZFS_DELEG_NOTE_PROJECTQUOTA:
4892 case ZFS_DELEG_NOTE_PROJECTOBJUSED:
4893 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA:
4895 return (gettext("other"));
4897 return (gettext("subcommand"));
4902 who_type2weight(zfs_deleg_who_type_t who_type)
4906 case ZFS_DELEG_NAMED_SET_SETS:
4907 case ZFS_DELEG_NAMED_SET:
4910 case ZFS_DELEG_CREATE_SETS:
4911 case ZFS_DELEG_CREATE:
4914 case ZFS_DELEG_USER_SETS:
4915 case ZFS_DELEG_USER:
4918 case ZFS_DELEG_GROUP_SETS:
4919 case ZFS_DELEG_GROUP:
4922 case ZFS_DELEG_EVERYONE_SETS:
4923 case ZFS_DELEG_EVERYONE:
4935 who_perm_compare(const void *larg, const void *rarg, void *unused)
4937 const who_perm_node_t *l = larg;
4938 const who_perm_node_t *r = rarg;
4939 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4940 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4941 int lweight = who_type2weight(ltype);
4942 int rweight = who_type2weight(rtype);
4943 int res = lweight - rweight;
4945 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4946 ZFS_MAX_DELEG_NAME-1);
4958 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4960 const deleg_perm_node_t *l = larg;
4961 const deleg_perm_node_t *r = rarg;
4962 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4963 ZFS_MAX_DELEG_NAME-1);
4975 fs_perm_set_init(fs_perm_set_t *fspset)
4977 bzero(fspset, sizeof (fs_perm_set_t));
4979 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4980 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4981 NULL, UU_DEFAULT)) == NULL)
4983 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4984 UU_DEFAULT)) == NULL)
4987 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4988 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4989 who_perm_node_t, who_avl_node), who_perm_compare,
4990 UU_DEFAULT)) == NULL)
4993 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4994 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4995 who_perm_node_t, who_avl_node), who_perm_compare,
4996 UU_DEFAULT)) == NULL)
4999 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
5000 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
5001 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
5006 static inline void fs_perm_fini(fs_perm_t *);
5007 static inline void who_perm_fini(who_perm_t *);
5010 fs_perm_set_fini(fs_perm_set_t *fspset)
5012 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
5014 while (node != NULL) {
5015 fs_perm_node_t *next_node =
5016 uu_list_next(fspset->fsps_list, node);
5017 fs_perm_t *fsperm = &node->fspn_fsperm;
5018 fs_perm_fini(fsperm);
5019 uu_list_remove(fspset->fsps_list, node);
5024 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
5025 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
5026 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
5030 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
5033 deleg_perm->dp_who_type = type;
5034 deleg_perm->dp_name = name;
5038 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
5039 zfs_deleg_who_type_t type, const char *name)
5041 uu_avl_pool_t *pool;
5042 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
5044 bzero(who_perm, sizeof (who_perm_t));
5046 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
5047 UU_DEFAULT)) == NULL)
5050 who_perm->who_type = type;
5051 who_perm->who_name = name;
5052 who_perm->who_fsperm = fsperm;
5056 who_perm_fini(who_perm_t *who_perm)
5058 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
5060 while (node != NULL) {
5061 deleg_perm_node_t *next_node =
5062 uu_avl_next(who_perm->who_deleg_perm_avl, node);
5064 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
5069 uu_avl_destroy(who_perm->who_deleg_perm_avl);
5073 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
5075 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
5076 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
5078 bzero(fsperm, sizeof (fs_perm_t));
5080 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
5084 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
5088 fsperm->fsp_set = fspset;
5089 fsperm->fsp_name = fsname;
5093 fs_perm_fini(fs_perm_t *fsperm)
5095 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
5096 while (node != NULL) {
5097 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
5099 who_perm_t *who_perm = &node->who_perm;
5100 who_perm_fini(who_perm);
5101 uu_avl_remove(fsperm->fsp_sc_avl, node);
5106 node = uu_avl_first(fsperm->fsp_uge_avl);
5107 while (node != NULL) {
5108 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
5110 who_perm_t *who_perm = &node->who_perm;
5111 who_perm_fini(who_perm);
5112 uu_avl_remove(fsperm->fsp_uge_avl, node);
5117 uu_avl_destroy(fsperm->fsp_sc_avl);
5118 uu_avl_destroy(fsperm->fsp_uge_avl);
5122 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
5123 zfs_deleg_who_type_t who_type, const char *name, char locality)
5125 uu_avl_index_t idx = 0;
5127 deleg_perm_node_t *found_node = NULL;
5128 deleg_perm_t *deleg_perm = &node->dpn_perm;
5130 deleg_perm_init(deleg_perm, who_type, name);
5132 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
5134 uu_avl_insert(avl, node, idx);
5137 deleg_perm = &node->dpn_perm;
5142 case ZFS_DELEG_LOCAL:
5143 deleg_perm->dp_local = B_TRUE;
5145 case ZFS_DELEG_DESCENDENT:
5146 deleg_perm->dp_descend = B_TRUE;
5151 assert(B_FALSE); /* invalid locality */
5156 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
5158 nvpair_t *nvp = NULL;
5159 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
5160 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
5161 zfs_deleg_who_type_t who_type = who_perm->who_type;
5163 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5164 const char *name = nvpair_name(nvp);
5165 data_type_t type = nvpair_type(nvp);
5166 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
5167 deleg_perm_node_t *node =
5168 safe_malloc(sizeof (deleg_perm_node_t));
5170 VERIFY(type == DATA_TYPE_BOOLEAN);
5172 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
5173 set_deleg_perm_node(avl, node, who_type, name, locality);
5180 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
5182 nvpair_t *nvp = NULL;
5183 fs_perm_set_t *fspset = fsperm->fsp_set;
5185 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5186 nvlist_t *nvl2 = NULL;
5187 const char *name = nvpair_name(nvp);
5188 uu_avl_t *avl = NULL;
5189 uu_avl_pool_t *avl_pool = NULL;
5190 zfs_deleg_who_type_t perm_type = name[0];
5191 char perm_locality = name[1];
5192 const char *perm_name = name + 3;
5193 who_perm_t *who_perm = NULL;
5195 assert('$' == name[2]);
5197 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
5200 switch (perm_type) {
5201 case ZFS_DELEG_CREATE:
5202 case ZFS_DELEG_CREATE_SETS:
5203 case ZFS_DELEG_NAMED_SET:
5204 case ZFS_DELEG_NAMED_SET_SETS:
5205 avl_pool = fspset->fsps_named_set_avl_pool;
5206 avl = fsperm->fsp_sc_avl;
5208 case ZFS_DELEG_USER:
5209 case ZFS_DELEG_USER_SETS:
5210 case ZFS_DELEG_GROUP:
5211 case ZFS_DELEG_GROUP_SETS:
5212 case ZFS_DELEG_EVERYONE:
5213 case ZFS_DELEG_EVERYONE_SETS:
5214 avl_pool = fspset->fsps_who_perm_avl_pool;
5215 avl = fsperm->fsp_uge_avl;
5219 assert(!"unhandled zfs_deleg_who_type_t");
5222 who_perm_node_t *found_node = NULL;
5223 who_perm_node_t *node = safe_malloc(
5224 sizeof (who_perm_node_t));
5225 who_perm = &node->who_perm;
5226 uu_avl_index_t idx = 0;
5228 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
5229 who_perm_init(who_perm, fsperm, perm_type, perm_name);
5231 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
5233 if (avl == fsperm->fsp_uge_avl) {
5235 struct passwd *p = NULL;
5236 struct group *g = NULL;
5237 const char *nice_name = NULL;
5239 switch (perm_type) {
5240 case ZFS_DELEG_USER_SETS:
5241 case ZFS_DELEG_USER:
5242 rid = atoi(perm_name);
5245 nice_name = p->pw_name;
5247 case ZFS_DELEG_GROUP_SETS:
5248 case ZFS_DELEG_GROUP:
5249 rid = atoi(perm_name);
5252 nice_name = g->gr_name;
5259 if (nice_name != NULL) {
5261 node->who_perm.who_ug_name,
5264 /* User or group unknown */
5266 node->who_perm.who_ug_name,
5267 sizeof (node->who_perm.who_ug_name),
5268 "(unknown: %d)", rid);
5272 uu_avl_insert(avl, node, idx);
5275 who_perm = &node->who_perm;
5278 assert(who_perm != NULL);
5279 (void) parse_who_perm(who_perm, nvl2, perm_locality);
5286 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
5288 nvpair_t *nvp = NULL;
5289 uu_avl_index_t idx = 0;
5291 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5292 nvlist_t *nvl2 = NULL;
5293 const char *fsname = nvpair_name(nvp);
5294 data_type_t type = nvpair_type(nvp);
5295 fs_perm_t *fsperm = NULL;
5296 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
5300 fsperm = &node->fspn_fsperm;
5302 VERIFY(DATA_TYPE_NVLIST == type);
5304 uu_list_node_init(node, &node->fspn_list_node,
5305 fspset->fsps_list_pool);
5307 idx = uu_list_numnodes(fspset->fsps_list);
5308 fs_perm_init(fsperm, fspset, fsname);
5310 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
5313 (void) parse_fs_perm(fsperm, nvl2);
5315 uu_list_insert(fspset->fsps_list, node, idx);
5321 static inline const char *
5322 deleg_perm_comment(zfs_deleg_note_t note)
5324 const char *str = "";
5329 case ZFS_DELEG_NOTE_ALLOW:
5330 str = gettext("Must also have the permission that is being"
5331 "\n\t\t\t\tallowed");
5333 case ZFS_DELEG_NOTE_CLONE:
5334 str = gettext("Must also have the 'create' ability and 'mount'"
5335 "\n\t\t\t\tability in the origin file system");
5337 case ZFS_DELEG_NOTE_CREATE:
5338 str = gettext("Must also have the 'mount' ability");
5340 case ZFS_DELEG_NOTE_DESTROY:
5341 str = gettext("Must also have the 'mount' ability");
5343 case ZFS_DELEG_NOTE_DIFF:
5344 str = gettext("Allows lookup of paths within a dataset;"
5345 "\n\t\t\t\tgiven an object number. Ordinary users need this"
5346 "\n\t\t\t\tin order to use zfs diff");
5348 case ZFS_DELEG_NOTE_HOLD:
5349 str = gettext("Allows adding a user hold to a snapshot");
5351 case ZFS_DELEG_NOTE_MOUNT:
5352 str = gettext("Allows mount/umount of ZFS datasets");
5354 case ZFS_DELEG_NOTE_PROMOTE:
5355 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
5356 " 'promote' ability in the origin file system");
5358 case ZFS_DELEG_NOTE_RECEIVE:
5359 str = gettext("Must also have the 'mount' and 'create'"
5362 case ZFS_DELEG_NOTE_RELEASE:
5363 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
5364 "might destroy the snapshot");
5366 case ZFS_DELEG_NOTE_RENAME:
5367 str = gettext("Must also have the 'mount' and 'create'"
5368 "\n\t\t\t\tability in the new parent");
5370 case ZFS_DELEG_NOTE_ROLLBACK:
5373 case ZFS_DELEG_NOTE_SEND:
5376 case ZFS_DELEG_NOTE_SHARE:
5377 str = gettext("Allows sharing file systems over NFS or SMB"
5378 "\n\t\t\t\tprotocols");
5380 case ZFS_DELEG_NOTE_SNAPSHOT:
5383 case ZFS_DELEG_NOTE_LOAD_KEY:
5384 str = gettext("Allows loading or unloading an encryption key");
5386 case ZFS_DELEG_NOTE_CHANGE_KEY:
5387 str = gettext("Allows changing or adding an encryption key");
5390 * case ZFS_DELEG_NOTE_VSCAN:
5391 * str = gettext("");
5395 case ZFS_DELEG_NOTE_GROUPQUOTA:
5396 str = gettext("Allows accessing any groupquota@... property");
5398 case ZFS_DELEG_NOTE_GROUPUSED:
5399 str = gettext("Allows reading any groupused@... property");
5401 case ZFS_DELEG_NOTE_USERPROP:
5402 str = gettext("Allows changing any user property");
5404 case ZFS_DELEG_NOTE_USERQUOTA:
5405 str = gettext("Allows accessing any userquota@... property");
5407 case ZFS_DELEG_NOTE_USERUSED:
5408 str = gettext("Allows reading any userused@... property");
5410 case ZFS_DELEG_NOTE_USEROBJQUOTA:
5411 str = gettext("Allows accessing any userobjquota@... property");
5413 case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
5414 str = gettext("Allows accessing any \n\t\t\t\t"
5415 "groupobjquota@... property");
5417 case ZFS_DELEG_NOTE_GROUPOBJUSED:
5418 str = gettext("Allows reading any groupobjused@... property");
5420 case ZFS_DELEG_NOTE_USEROBJUSED:
5421 str = gettext("Allows reading any userobjused@... property");
5423 case ZFS_DELEG_NOTE_PROJECTQUOTA:
5424 str = gettext("Allows accessing any projectquota@... property");
5426 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA:
5427 str = gettext("Allows accessing any \n\t\t\t\t"
5428 "projectobjquota@... property");
5430 case ZFS_DELEG_NOTE_PROJECTUSED:
5431 str = gettext("Allows reading any projectused@... property");
5433 case ZFS_DELEG_NOTE_PROJECTOBJUSED:
5434 str = gettext("Allows accessing any \n\t\t\t\t"
5435 "projectobjused@... property");
5453 boolean_t recursive; /* unallow only */
5454 boolean_t prt_usage;
5456 boolean_t prt_perms;
5459 const char *dataset;
5463 prop_cmp(const void *a, const void *b)
5465 const char *str1 = *(const char **)a;
5466 const char *str2 = *(const char **)b;
5467 return (strcmp(str1, str2));
5471 allow_usage(boolean_t un, boolean_t requested, const char *msg)
5473 const char *opt_desc[] = {
5474 "-h", gettext("show this help message and exit"),
5475 "-l", gettext("set permission locally"),
5476 "-d", gettext("set permission for descents"),
5477 "-u", gettext("set permission for user"),
5478 "-g", gettext("set permission for group"),
5479 "-e", gettext("set permission for everyone"),
5480 "-c", gettext("set create time permission"),
5481 "-s", gettext("define permission set"),
5483 "-r", gettext("remove permissions recursively"),
5485 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
5486 size_t allow_size = unallow_size - 2;
5487 const char *props[ZFS_NUM_PROPS];
5490 FILE *fp = requested ? stdout : stderr;
5491 zprop_desc_t *pdtbl = zfs_prop_get_table();
5492 const char *fmt = gettext("%-16s %-14s\t%s\n");
5494 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
5496 (void) fprintf(fp, gettext("Options:\n"));
5497 for (i = 0; i < (un ? unallow_size : allow_size); i += 2) {
5498 const char *opt = opt_desc[i];
5499 const char *optdsc = opt_desc[i + 1];
5500 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
5503 (void) fprintf(fp, gettext("\nThe following permissions are "
5505 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
5507 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
5508 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
5509 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
5510 const char *perm_type = deleg_perm_type(perm_note);
5511 const char *perm_comment = deleg_perm_comment(perm_note);
5512 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
5515 for (i = 0; i < ZFS_NUM_PROPS; i++) {
5516 zprop_desc_t *pd = &pdtbl[i];
5517 if (pd->pd_visible != B_TRUE)
5520 if (pd->pd_attr == PROP_READONLY)
5523 props[count++] = pd->pd_name;
5525 props[count] = NULL;
5527 qsort(props, count, sizeof (char *), prop_cmp);
5529 for (i = 0; i < count; i++)
5530 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
5533 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
5535 exit(requested ? 0 : 2);
5538 static inline const char *
5539 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
5542 if (un && argc == expected_argc - 1)
5544 else if (argc == expected_argc)
5545 *permsp = argv[argc - 2];
5547 allow_usage(un, B_FALSE,
5548 gettext("wrong number of parameters\n"));
5550 return (argv[argc - 1]);
5554 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
5556 int uge_sum = opts->user + opts->group + opts->everyone;
5557 int csuge_sum = opts->create + opts->set + uge_sum;
5558 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
5559 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
5562 allow_usage(un, B_FALSE,
5563 gettext("-u, -g, and -e are mutually exclusive\n"));
5565 if (opts->prt_usage) {
5566 if (argc == 0 && all_sum == 0)
5567 allow_usage(un, B_TRUE, NULL);
5574 allow_usage(un, B_FALSE,
5575 gettext("invalid options combined with -s\n"));
5577 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
5578 if (argv[0][0] != '@')
5579 allow_usage(un, B_FALSE,
5580 gettext("invalid set name: missing '@' prefix\n"));
5581 opts->who = argv[0];
5582 } else if (opts->create) {
5583 if (ldcsuge_sum > 1)
5584 allow_usage(un, B_FALSE,
5585 gettext("invalid options combined with -c\n"));
5586 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5587 } else if (opts->everyone) {
5589 allow_usage(un, B_FALSE,
5590 gettext("invalid options combined with -e\n"));
5591 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5592 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
5594 opts->everyone = B_TRUE;
5597 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5598 } else if (argc == 1 && !un) {
5599 opts->prt_perms = B_TRUE;
5600 opts->dataset = argv[argc-1];
5602 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
5603 opts->who = argv[0];
5606 if (!opts->local && !opts->descend) {
5607 opts->local = B_TRUE;
5608 opts->descend = B_TRUE;
5613 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
5614 const char *who, char *perms, nvlist_t *top_nvl)
5617 char ld[2] = { '\0', '\0' };
5618 char who_buf[MAXNAMELEN + 32];
5619 char base_type = '\0';
5620 char set_type = '\0';
5621 nvlist_t *base_nvl = NULL;
5622 nvlist_t *set_nvl = NULL;
5625 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
5627 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
5631 case ZFS_DELEG_NAMED_SET_SETS:
5632 case ZFS_DELEG_NAMED_SET:
5633 set_type = ZFS_DELEG_NAMED_SET_SETS;
5634 base_type = ZFS_DELEG_NAMED_SET;
5635 ld[0] = ZFS_DELEG_NA;
5637 case ZFS_DELEG_CREATE_SETS:
5638 case ZFS_DELEG_CREATE:
5639 set_type = ZFS_DELEG_CREATE_SETS;
5640 base_type = ZFS_DELEG_CREATE;
5641 ld[0] = ZFS_DELEG_NA;
5643 case ZFS_DELEG_USER_SETS:
5644 case ZFS_DELEG_USER:
5645 set_type = ZFS_DELEG_USER_SETS;
5646 base_type = ZFS_DELEG_USER;
5648 ld[0] = ZFS_DELEG_LOCAL;
5650 ld[1] = ZFS_DELEG_DESCENDENT;
5652 case ZFS_DELEG_GROUP_SETS:
5653 case ZFS_DELEG_GROUP:
5654 set_type = ZFS_DELEG_GROUP_SETS;
5655 base_type = ZFS_DELEG_GROUP;
5657 ld[0] = ZFS_DELEG_LOCAL;
5659 ld[1] = ZFS_DELEG_DESCENDENT;
5661 case ZFS_DELEG_EVERYONE_SETS:
5662 case ZFS_DELEG_EVERYONE:
5663 set_type = ZFS_DELEG_EVERYONE_SETS;
5664 base_type = ZFS_DELEG_EVERYONE;
5666 ld[0] = ZFS_DELEG_LOCAL;
5668 ld[1] = ZFS_DELEG_DESCENDENT;
5672 assert(set_type != '\0' && base_type != '\0');
5675 if (perms != NULL) {
5677 char *end = curr + strlen(perms);
5679 while (curr < end) {
5680 char *delim = strchr(curr, ',');
5691 (void) nvlist_add_boolean(nvl, curr);
5697 for (i = 0; i < 2; i++) {
5698 char locality = ld[i];
5702 if (!nvlist_empty(base_nvl)) {
5704 (void) snprintf(who_buf,
5705 sizeof (who_buf), "%c%c$%s",
5706 base_type, locality, who);
5708 (void) snprintf(who_buf,
5709 sizeof (who_buf), "%c%c$",
5710 base_type, locality);
5712 (void) nvlist_add_nvlist(top_nvl, who_buf,
5717 if (!nvlist_empty(set_nvl)) {
5719 (void) snprintf(who_buf,
5720 sizeof (who_buf), "%c%c$%s",
5721 set_type, locality, who);
5723 (void) snprintf(who_buf,
5724 sizeof (who_buf), "%c%c$",
5725 set_type, locality);
5727 (void) nvlist_add_nvlist(top_nvl, who_buf,
5732 for (i = 0; i < 2; i++) {
5733 char locality = ld[i];
5738 (void) snprintf(who_buf, sizeof (who_buf),
5739 "%c%c$%s", base_type, locality, who);
5741 (void) snprintf(who_buf, sizeof (who_buf),
5742 "%c%c$", base_type, locality);
5743 (void) nvlist_add_boolean(top_nvl, who_buf);
5746 (void) snprintf(who_buf, sizeof (who_buf),
5747 "%c%c$%s", set_type, locality, who);
5749 (void) snprintf(who_buf, sizeof (who_buf),
5750 "%c%c$", set_type, locality);
5751 (void) nvlist_add_boolean(top_nvl, who_buf);
5757 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5759 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5763 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5764 opts->descend, opts->who, opts->perms, *nvlp);
5765 } else if (opts->create) {
5766 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5767 opts->descend, NULL, opts->perms, *nvlp);
5768 } else if (opts->everyone) {
5769 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5770 opts->descend, NULL, opts->perms, *nvlp);
5772 char *curr = opts->who;
5773 char *end = curr + strlen(curr);
5775 while (curr < end) {
5777 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
5779 char *delim = strchr(curr, ',');
5782 struct passwd *p = NULL;
5783 struct group *g = NULL;
5791 rid = (uid_t)strtol(curr, &endch, 0);
5793 who_type = ZFS_DELEG_USER;
5801 else if (*endch != '\0') {
5802 (void) snprintf(errbuf, 256, gettext(
5803 "invalid user %s\n"), curr);
5804 allow_usage(un, B_TRUE, errbuf);
5806 } else if (opts->group) {
5807 who_type = ZFS_DELEG_GROUP;
5815 else if (*endch != '\0') {
5816 (void) snprintf(errbuf, 256, gettext(
5817 "invalid group %s\n"), curr);
5818 allow_usage(un, B_TRUE, errbuf);
5821 if (*endch != '\0') {
5828 if (*endch != '\0') {
5836 who_type = ZFS_DELEG_USER;
5838 } else if (g != NULL) {
5839 who_type = ZFS_DELEG_GROUP;
5842 (void) snprintf(errbuf, 256, gettext(
5843 "invalid user/group %s\n"), curr);
5844 allow_usage(un, B_TRUE, errbuf);
5848 (void) sprintf(id, "%u", rid);
5851 store_allow_perm(who_type, opts->local,
5852 opts->descend, who, opts->perms, *nvlp);
5861 print_set_creat_perms(uu_avl_t *who_avl)
5863 const char *sc_title[] = {
5864 gettext("Permission sets:\n"),
5865 gettext("Create time permissions:\n"),
5868 who_perm_node_t *who_node = NULL;
5869 int prev_weight = -1;
5871 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5872 who_node = uu_avl_next(who_avl, who_node)) {
5873 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5874 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5875 const char *who_name = who_node->who_perm.who_name;
5876 int weight = who_type2weight(who_type);
5877 boolean_t first = B_TRUE;
5878 deleg_perm_node_t *deleg_node;
5880 if (prev_weight != weight) {
5881 (void) printf("%s", sc_title[weight]);
5882 prev_weight = weight;
5885 if (who_name == NULL || strnlen(who_name, 1) == 0)
5886 (void) printf("\t");
5888 (void) printf("\t%s ", who_name);
5890 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5891 deleg_node = uu_avl_next(avl, deleg_node)) {
5894 deleg_node->dpn_perm.dp_name);
5897 (void) printf(",%s",
5898 deleg_node->dpn_perm.dp_name);
5901 (void) printf("\n");
5906 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5909 who_perm_node_t *who_node = NULL;
5910 boolean_t prt_title = B_TRUE;
5911 uu_avl_walk_t *walk;
5913 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5916 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5917 const char *who_name = who_node->who_perm.who_name;
5918 const char *nice_who_name = who_node->who_perm.who_ug_name;
5919 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5920 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5922 deleg_perm_node_t *deleg_node;
5923 boolean_t prt_who = B_TRUE;
5925 for (deleg_node = uu_avl_first(avl);
5927 deleg_node = uu_avl_next(avl, deleg_node)) {
5928 if (local != deleg_node->dpn_perm.dp_local ||
5929 descend != deleg_node->dpn_perm.dp_descend)
5933 const char *who = NULL;
5935 prt_title = B_FALSE;
5936 (void) printf("%s", title);
5940 case ZFS_DELEG_USER_SETS:
5941 case ZFS_DELEG_USER:
5942 who = gettext("user");
5944 who_name = nice_who_name;
5946 case ZFS_DELEG_GROUP_SETS:
5947 case ZFS_DELEG_GROUP:
5948 who = gettext("group");
5950 who_name = nice_who_name;
5952 case ZFS_DELEG_EVERYONE_SETS:
5953 case ZFS_DELEG_EVERYONE:
5954 who = gettext("everyone");
5959 assert(who != NULL);
5963 if (who_name == NULL)
5964 (void) printf("\t%s", who);
5966 (void) printf("\t%s %s", who, who_name);
5969 (void) printf("%c%s", delim,
5970 deleg_node->dpn_perm.dp_name);
5975 (void) printf("\n");
5978 uu_avl_walk_end(walk);
5982 print_fs_perms(fs_perm_set_t *fspset)
5984 fs_perm_node_t *node = NULL;
5985 char buf[MAXNAMELEN + 32];
5986 const char *dsname = buf;
5988 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5989 node = uu_list_next(fspset->fsps_list, node)) {
5990 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5991 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5994 (void) snprintf(buf, sizeof (buf),
5995 gettext("---- Permissions on %s "),
5996 node->fspn_fsperm.fsp_name);
5997 (void) printf("%s", dsname);
5998 left = 70 - strlen(buf);
6001 (void) printf("\n");
6003 print_set_creat_perms(sc_avl);
6004 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
6005 gettext("Local permissions:\n"));
6006 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
6007 gettext("Descendent permissions:\n"));
6008 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
6009 gettext("Local+Descendent permissions:\n"));
6013 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
6015 struct deleg_perms {
6021 set_deleg_perms(zfs_handle_t *zhp, void *data)
6023 struct deleg_perms *perms = (struct deleg_perms *)data;
6024 zfs_type_t zfs_type = zfs_get_type(zhp);
6026 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
6029 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
6033 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
6036 nvlist_t *perm_nvl = NULL;
6037 nvlist_t *update_perm_nvl = NULL;
6040 struct allow_opts opts = { 0 };
6042 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
6045 while ((c = getopt(argc, argv, optstr)) != -1) {
6048 opts.local = B_TRUE;
6051 opts.descend = B_TRUE;
6057 opts.group = B_TRUE;
6060 opts.everyone = B_TRUE;
6066 opts.create = B_TRUE;
6069 opts.recursive = B_TRUE;
6072 (void) fprintf(stderr, gettext("missing argument for "
6073 "'%c' option\n"), optopt);
6077 opts.prt_usage = B_TRUE;
6080 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6089 /* check arguments */
6090 parse_allow_args(argc, argv, un, &opts);
6092 /* try to open the dataset */
6093 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
6094 ZFS_TYPE_VOLUME)) == NULL) {
6095 (void) fprintf(stderr, "Failed to open dataset: %s\n",
6100 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
6103 fs_perm_set_init(&fs_perm_set);
6104 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
6105 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
6110 print_fs_perms(&fs_perm_set);
6112 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
6113 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
6116 if (un && opts.recursive) {
6117 struct deleg_perms data = { un, update_perm_nvl };
6118 if (zfs_iter_filesystems(zhp, set_deleg_perms,
6127 nvlist_free(perm_nvl);
6128 nvlist_free(update_perm_nvl);
6130 fs_perm_set_fini(&fs_perm_set);
6138 zfs_do_allow(int argc, char **argv)
6140 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
6144 zfs_do_unallow(int argc, char **argv)
6146 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
6150 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
6155 boolean_t recursive = B_FALSE;
6156 const char *opts = holding ? "rt" : "r";
6160 while ((c = getopt(argc, argv, opts)) != -1) {
6166 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6175 /* check number of arguments */
6183 if (holding && tag[0] == '.') {
6184 /* tags starting with '.' are reserved for libzfs */
6185 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
6189 for (i = 0; i < argc; ++i) {
6191 char parent[ZFS_MAX_DATASET_NAME_LEN];
6193 char *path = argv[i];
6195 delim = strchr(path, '@');
6196 if (delim == NULL) {
6197 (void) fprintf(stderr,
6198 gettext("'%s' is not a snapshot\n"), path);
6202 (void) strncpy(parent, path, delim - path);
6203 parent[delim - path] = '\0';
6205 zhp = zfs_open(g_zfs, parent,
6206 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
6212 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
6215 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
6221 return (errors != 0);
6225 * zfs hold [-r] [-t] <tag> <snap> ...
6227 * -r Recursively hold
6229 * Apply a user-hold with the given tag to the list of snapshots.
6232 zfs_do_hold(int argc, char **argv)
6234 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
6238 * zfs release [-r] <tag> <snap> ...
6240 * -r Recursively release
6242 * Release a user-hold with the given tag from the list of snapshots.
6245 zfs_do_release(int argc, char **argv)
6247 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
6250 typedef struct holds_cbdata {
6251 boolean_t cb_recursive;
6252 const char *cb_snapname;
6254 size_t cb_max_namelen;
6255 size_t cb_max_taglen;
6258 #define STRFTIME_FMT_STR "%a %b %e %H:%M %Y"
6259 #define DATETIME_BUF_LEN (32)
6264 print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl)
6267 nvpair_t *nvp = NULL;
6268 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
6272 for (i = 0; i < 3; i++) {
6273 col = gettext(hdr_cols[i]);
6275 (void) printf("%-*s ", i ? tagwidth : nwidth,
6278 (void) printf("%s\n", col);
6282 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
6283 char *zname = nvpair_name(nvp);
6285 nvpair_t *nvp2 = NULL;
6286 (void) nvpair_value_nvlist(nvp, &nvl2);
6287 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
6288 char tsbuf[DATETIME_BUF_LEN];
6289 char *tagname = nvpair_name(nvp2);
6294 (void) nvpair_value_uint64(nvp2, &val);
6296 (void) localtime_r(&time, &t);
6297 (void) strftime(tsbuf, DATETIME_BUF_LEN,
6298 gettext(STRFTIME_FMT_STR), &t);
6301 (void) printf("%s\t%s\t%s\n", zname,
6304 (void) printf("%-*s %-*s %s\n", nwidth,
6305 zname, tagwidth, tagname, tsbuf);
6312 * Generic callback function to list a dataset or snapshot.
6315 holds_callback(zfs_handle_t *zhp, void *data)
6317 holds_cbdata_t *cbp = data;
6318 nvlist_t *top_nvl = *cbp->cb_nvlp;
6319 nvlist_t *nvl = NULL;
6320 nvpair_t *nvp = NULL;
6321 const char *zname = zfs_get_name(zhp);
6322 size_t znamelen = strlen(zname);
6324 if (cbp->cb_recursive) {
6325 const char *snapname;
6326 char *delim = strchr(zname, '@');
6330 snapname = delim + 1;
6331 if (strcmp(cbp->cb_snapname, snapname))
6335 if (zfs_get_holds(zhp, &nvl) != 0)
6338 if (znamelen > cbp->cb_max_namelen)
6339 cbp->cb_max_namelen = znamelen;
6341 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
6342 const char *tag = nvpair_name(nvp);
6343 size_t taglen = strlen(tag);
6344 if (taglen > cbp->cb_max_taglen)
6345 cbp->cb_max_taglen = taglen;
6348 return (nvlist_add_nvlist(top_nvl, zname, nvl));
6352 * zfs holds [-rH] <snap> ...
6354 * -r Lists holds that are set on the named snapshots recursively.
6355 * -H Scripted mode; elide headers and separate columns by tabs.
6358 zfs_do_holds(int argc, char **argv)
6363 boolean_t scripted = B_FALSE;
6364 boolean_t recursive = B_FALSE;
6365 const char *opts = "rH";
6368 int types = ZFS_TYPE_SNAPSHOT;
6369 holds_cbdata_t cb = { 0 };
6376 while ((c = getopt(argc, argv, opts)) != -1) {
6385 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6392 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
6393 flags |= ZFS_ITER_RECURSE;
6399 /* check number of arguments */
6403 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
6406 for (i = 0; i < argc; ++i) {
6407 char *snapshot = argv[i];
6409 const char *snapname;
6411 delim = strchr(snapshot, '@');
6412 if (delim == NULL) {
6413 (void) fprintf(stderr,
6414 gettext("'%s' is not a snapshot\n"), snapshot);
6418 snapname = delim + 1;
6420 snapshot[delim - snapshot] = '\0';
6422 cb.cb_recursive = recursive;
6423 cb.cb_snapname = snapname;
6427 * 1. collect holds data, set format options
6429 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
6430 holds_callback, &cb);
6436 * 2. print holds data
6438 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
6440 if (nvlist_empty(nvl))
6441 (void) fprintf(stderr, gettext("no datasets available\n"));
6445 return (0 != errors);
6448 #define CHECK_SPINNER 30
6449 #define SPINNER_TIME 3 /* seconds */
6450 #define MOUNT_TIME 1 /* seconds */
6452 typedef struct get_all_state {
6453 boolean_t ga_verbose;
6454 get_all_cb_t *ga_cbp;
6458 get_one_dataset(zfs_handle_t *zhp, void *data)
6460 static char *spin[] = { "-", "\\", "|", "/" };
6461 static int spinval = 0;
6462 static int spincheck = 0;
6463 static time_t last_spin_time = (time_t)0;
6464 get_all_state_t *state = data;
6465 zfs_type_t type = zfs_get_type(zhp);
6467 if (state->ga_verbose) {
6468 if (--spincheck < 0) {
6469 time_t now = time(NULL);
6470 if (last_spin_time + SPINNER_TIME < now) {
6471 update_progress(spin[spinval++ % 4]);
6472 last_spin_time = now;
6474 spincheck = CHECK_SPINNER;
6479 * Iterate over any nested datasets.
6481 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
6487 * Skip any datasets whose type does not match.
6489 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
6493 libzfs_add_handle(state->ga_cbp, zhp);
6494 assert(state->ga_cbp->cb_used <= state->ga_cbp->cb_alloc);
6500 get_all_datasets(get_all_cb_t *cbp, boolean_t verbose)
6502 get_all_state_t state = {
6503 .ga_verbose = verbose,
6508 set_progress_header(gettext("Reading ZFS config"));
6509 (void) zfs_iter_root(g_zfs, get_one_dataset, &state);
6512 finish_progress(gettext("done."));
6516 * Generic callback for sharing or mounting filesystems. Because the code is so
6517 * similar, we have a common function with an extra parameter to determine which
6518 * mode we are using.
6520 typedef enum { OP_SHARE, OP_MOUNT } share_mount_op_t;
6522 typedef struct share_mount_state {
6523 share_mount_op_t sm_op;
6524 boolean_t sm_verbose;
6527 char *sm_proto; /* only valid for OP_SHARE */
6528 pthread_mutex_t sm_lock; /* protects the remaining fields */
6529 uint_t sm_total; /* number of filesystems to process */
6530 uint_t sm_done; /* number of filesystems processed */
6531 int sm_status; /* -1 if any of the share/mount operations failed */
6532 } share_mount_state_t;
6535 * Share or mount a dataset.
6538 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
6539 boolean_t explicit, const char *options)
6541 char mountpoint[ZFS_MAXPROPLEN];
6542 char shareopts[ZFS_MAXPROPLEN];
6543 char smbshareopts[ZFS_MAXPROPLEN];
6544 const char *cmdname = op == OP_SHARE ? "share" : "mount";
6546 uint64_t zoned, canmount;
6547 boolean_t shared_nfs, shared_smb;
6549 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
6552 * Check to make sure we can mount/share this dataset. If we
6553 * are in the global zone and the filesystem is exported to a
6554 * local zone, or if we are in a local zone and the
6555 * filesystem is not exported, then it is an error.
6557 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
6559 if (zoned && getzoneid() == GLOBAL_ZONEID) {
6563 (void) fprintf(stderr, gettext("cannot %s '%s': "
6564 "dataset is exported to a local zone\n"), cmdname,
6568 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
6572 (void) fprintf(stderr, gettext("cannot %s '%s': "
6573 "permission denied\n"), cmdname,
6579 * Ignore any filesystems which don't apply to us. This
6580 * includes those with a legacy mountpoint, or those with
6581 * legacy share options.
6583 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6584 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
6585 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
6586 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
6587 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
6588 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
6590 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
6591 strcmp(smbshareopts, "off") == 0) {
6595 (void) fprintf(stderr, gettext("cannot share '%s': "
6596 "legacy share\n"), zfs_get_name(zhp));
6597 (void) fprintf(stderr, gettext("use share(1M) to "
6598 "share this filesystem, or set "
6599 "sharenfs property on\n"));
6604 * We cannot share or mount legacy filesystems. If the
6605 * shareopts is non-legacy but the mountpoint is legacy, we
6606 * treat it as a legacy share.
6608 if (strcmp(mountpoint, "legacy") == 0) {
6612 (void) fprintf(stderr, gettext("cannot %s '%s': "
6613 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
6614 (void) fprintf(stderr, gettext("use %s(1M) to "
6615 "%s this filesystem\n"), cmdname, cmdname);
6619 if (strcmp(mountpoint, "none") == 0) {
6623 (void) fprintf(stderr, gettext("cannot %s '%s': no "
6624 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
6629 * canmount explicit outcome
6630 * on no pass through
6631 * on yes pass through
6633 * off yes display error, return 1
6634 * noauto no return 0
6635 * noauto yes pass through
6637 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
6638 if (canmount == ZFS_CANMOUNT_OFF) {
6642 (void) fprintf(stderr, gettext("cannot %s '%s': "
6643 "'canmount' property is set to 'off'\n"), cmdname,
6646 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
6648 * When performing a 'zfs mount -a', we skip any mounts for
6649 * datasets that have 'noauto' set. Sharing a dataset with
6650 * 'noauto' set is only allowed if it's mounted.
6654 if (op == OP_SHARE && !zfs_is_mounted(zhp, NULL)) {
6655 /* also purge it from existing exports */
6656 zfs_unshareall_bypath(zhp, mountpoint);
6662 * If this filesystem is encrypted and does not have
6663 * a loaded key, we can not mount it.
6665 if ((flags & MS_CRYPT) == 0 &&
6666 zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF &&
6667 zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
6668 ZFS_KEYSTATUS_UNAVAILABLE) {
6672 (void) fprintf(stderr, gettext("cannot %s '%s': "
6673 "encryption key not loaded\n"), cmdname, zfs_get_name(zhp));
6678 * If this filesystem is inconsistent and has a receive resume
6679 * token, we can not mount it.
6681 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
6682 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
6683 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
6687 (void) fprintf(stderr, gettext("cannot %s '%s': "
6688 "Contains partially-completed state from "
6689 "\"zfs receive -s\", which can be resumed with "
6690 "\"zfs send -t\"\n"),
6691 cmdname, zfs_get_name(zhp));
6695 if (zfs_prop_get_int(zhp, ZFS_PROP_REDACTED) && !(flags & MS_FORCE)) {
6699 (void) fprintf(stderr, gettext("cannot %s '%s': "
6700 "Dataset is not complete, was created by receiving "
6701 "a redacted zfs send stream.\n"), cmdname,
6707 * At this point, we have verified that the mountpoint and/or
6708 * shareopts are appropriate for auto management. If the
6709 * filesystem is already mounted or shared, return (failing
6710 * for explicit requests); otherwise mount or share the
6716 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
6717 shared_smb = zfs_is_shared_smb(zhp, NULL);
6719 if ((shared_nfs && shared_smb) ||
6720 (shared_nfs && strcmp(shareopts, "on") == 0 &&
6721 strcmp(smbshareopts, "off") == 0) ||
6722 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
6723 strcmp(shareopts, "off") == 0)) {
6727 (void) fprintf(stderr, gettext("cannot share "
6728 "'%s': filesystem already shared\n"),
6733 if (!zfs_is_mounted(zhp, NULL) &&
6734 zfs_mount(zhp, NULL, flags) != 0)
6737 if (protocol == NULL) {
6738 if (zfs_shareall(zhp) != 0)
6740 } else if (strcmp(protocol, "nfs") == 0) {
6741 if (zfs_share_nfs(zhp))
6743 } else if (strcmp(protocol, "smb") == 0) {
6744 if (zfs_share_smb(zhp))
6747 (void) fprintf(stderr, gettext("cannot share "
6748 "'%s': invalid share type '%s' "
6750 zfs_get_name(zhp), protocol);
6757 if (options == NULL)
6758 mnt.mnt_mntopts = "";
6760 mnt.mnt_mntopts = (char *)options;
6762 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
6763 zfs_is_mounted(zhp, NULL)) {
6767 (void) fprintf(stderr, gettext("cannot mount "
6768 "'%s': filesystem already mounted\n"),
6773 if (zfs_mount(zhp, options, flags) != 0)
6782 * Reports progress in the form "(current/total)". Not thread-safe.
6785 report_mount_progress(int current, int total)
6787 static time_t last_progress_time = 0;
6788 time_t now = time(NULL);
6791 /* report 1..n instead of 0..n-1 */
6794 /* display header if we're here for the first time */
6796 set_progress_header(gettext("Mounting ZFS filesystems"));
6797 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6798 /* too soon to report again */
6802 last_progress_time = now;
6804 (void) sprintf(info, "(%d/%d)", current, total);
6806 if (current == total)
6807 finish_progress(info);
6809 update_progress(info);
6813 * zfs_foreach_mountpoint() callback that mounts or shares one filesystem and
6814 * updates the progress meter.
6817 share_mount_one_cb(zfs_handle_t *zhp, void *arg)
6819 share_mount_state_t *sms = arg;
6822 ret = share_mount_one(zhp, sms->sm_op, sms->sm_flags, sms->sm_proto,
6823 B_FALSE, sms->sm_options);
6825 pthread_mutex_lock(&sms->sm_lock);
6827 sms->sm_status = ret;
6829 if (sms->sm_verbose)
6830 report_mount_progress(sms->sm_done, sms->sm_total);
6831 pthread_mutex_unlock(&sms->sm_lock);
6836 append_options(char *mntopts, char *newopts)
6838 int len = strlen(mntopts);
6840 /* original length plus new string to append plus 1 for the comma */
6841 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6842 (void) fprintf(stderr, gettext("the opts argument for "
6843 "'%s' option is too long (more than %d chars)\n"),
6844 "-o", MNT_LINE_MAX);
6849 mntopts[len++] = ',';
6851 (void) strcpy(&mntopts[len], newopts);
6855 share_mount(int op, int argc, char **argv)
6858 boolean_t verbose = B_FALSE;
6860 char *options = NULL;
6864 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":alvo:Of" : "al"))
6877 if (*optarg == '\0') {
6878 (void) fprintf(stderr, gettext("empty mount "
6879 "options (-o) specified\n"));
6883 if (options == NULL)
6884 options = safe_malloc(MNT_LINE_MAX + 1);
6886 /* option validation is done later */
6887 append_options(options, optarg);
6890 flags |= MS_OVERLAY;
6896 (void) fprintf(stderr, gettext("missing argument for "
6897 "'%c' option\n"), optopt);
6901 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6910 /* check number of arguments */
6912 char *protocol = NULL;
6914 if (op == OP_SHARE && argc > 0) {
6915 if (strcmp(argv[0], "nfs") != 0 &&
6916 strcmp(argv[0], "smb") != 0) {
6917 (void) fprintf(stderr, gettext("share type "
6918 "must be 'nfs' or 'smb'\n"));
6927 (void) fprintf(stderr, gettext("too many arguments\n"));
6931 start_progress_timer();
6932 get_all_cb_t cb = { 0 };
6933 get_all_datasets(&cb, verbose);
6935 if (cb.cb_used == 0) {
6936 if (options != NULL)
6941 share_mount_state_t share_mount_state = { 0 };
6942 share_mount_state.sm_op = op;
6943 share_mount_state.sm_verbose = verbose;
6944 share_mount_state.sm_flags = flags;
6945 share_mount_state.sm_options = options;
6946 share_mount_state.sm_proto = protocol;
6947 share_mount_state.sm_total = cb.cb_used;
6948 pthread_mutex_init(&share_mount_state.sm_lock, NULL);
6951 * libshare isn't mt-safe, so only do the operation in parallel
6952 * if we're mounting. Additionally, the key-loading option must
6953 * be serialized so that we can prompt the user for their keys
6954 * in a consistent manner.
6956 zfs_foreach_mountpoint(g_zfs, cb.cb_handles, cb.cb_used,
6957 share_mount_one_cb, &share_mount_state,
6958 op == OP_MOUNT && !(flags & MS_CRYPT));
6959 zfs_commit_all_shares();
6961 ret = share_mount_state.sm_status;
6963 for (int i = 0; i < cb.cb_used; i++)
6964 zfs_close(cb.cb_handles[i]);
6965 free(cb.cb_handles);
6966 } else if (argc == 0) {
6967 struct mnttab entry;
6969 if ((op == OP_SHARE) || (options != NULL)) {
6970 (void) fprintf(stderr, gettext("missing filesystem "
6971 "argument (specify -a for all)\n"));
6976 * When mount is given no arguments, go through
6977 * /proc/self/mounts and display any active ZFS mounts.
6978 * We hide any snapshots, since they are controlled
6982 /* Reopen MNTTAB to prevent reading stale data from open file */
6983 if (freopen(MNTTAB, "r", mnttab_file) == NULL) {
6984 if (options != NULL)
6989 while (getmntent(mnttab_file, &entry) == 0) {
6990 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6991 strchr(entry.mnt_special, '@') != NULL)
6994 (void) printf("%-30s %s\n", entry.mnt_special,
7002 (void) fprintf(stderr,
7003 gettext("too many arguments\n"));
7007 if ((zhp = zfs_open(g_zfs, argv[0],
7008 ZFS_TYPE_FILESYSTEM)) == NULL) {
7011 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
7013 zfs_commit_all_shares();
7018 if (options != NULL)
7025 * zfs mount -a [nfs]
7026 * zfs mount filesystem
7028 * Mount all filesystems, or mount the given filesystem.
7031 zfs_do_mount(int argc, char **argv)
7033 return (share_mount(OP_MOUNT, argc, argv));
7037 * zfs share -a [nfs | smb]
7038 * zfs share filesystem
7040 * Share all filesystems, or share the given filesystem.
7043 zfs_do_share(int argc, char **argv)
7045 return (share_mount(OP_SHARE, argc, argv));
7048 typedef struct unshare_unmount_node {
7049 zfs_handle_t *un_zhp;
7051 uu_avl_node_t un_avlnode;
7052 } unshare_unmount_node_t;
7056 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
7058 const unshare_unmount_node_t *l = larg;
7059 const unshare_unmount_node_t *r = rarg;
7061 return (strcmp(l->un_mountp, r->un_mountp));
7065 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
7066 * absolute path, find the entry /proc/self/mounts, verify that it's a
7067 * ZFS filesystem, and unmount it appropriately.
7070 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
7074 struct stat64 statbuf;
7075 struct extmnttab entry;
7076 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
7080 * Search for the given (major,minor) pair in the mount table.
7083 /* Reopen MNTTAB to prevent reading stale data from open file */
7084 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
7087 if (getextmntent(path, &entry, &statbuf) != 0) {
7088 if (op == OP_SHARE) {
7089 (void) fprintf(stderr, gettext("cannot %s '%s': not "
7090 "currently mounted\n"), cmdname, path);
7093 (void) fprintf(stderr, gettext("warning: %s not in"
7094 "/proc/self/mounts\n"), path);
7095 if ((ret = umount2(path, flags)) != 0)
7096 (void) fprintf(stderr, gettext("%s: %s\n"), path,
7100 path_inode = statbuf.st_ino;
7102 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
7103 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
7104 "filesystem\n"), cmdname, path);
7108 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
7109 ZFS_TYPE_FILESYSTEM)) == NULL)
7113 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
7114 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
7115 cmdname, path, strerror(errno));
7117 } else if (statbuf.st_ino != path_inode) {
7118 (void) fprintf(stderr, gettext("cannot "
7119 "%s '%s': not a mountpoint\n"), cmdname, path);
7123 if (op == OP_SHARE) {
7124 char nfs_mnt_prop[ZFS_MAXPROPLEN];
7125 char smbshare_prop[ZFS_MAXPROPLEN];
7127 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
7128 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
7129 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
7130 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
7132 if (strcmp(nfs_mnt_prop, "off") == 0 &&
7133 strcmp(smbshare_prop, "off") == 0) {
7134 (void) fprintf(stderr, gettext("cannot unshare "
7135 "'%s': legacy share\n"), path);
7136 (void) fprintf(stderr, gettext("use exportfs(8) "
7137 "or smbcontrol(1) to unshare this filesystem\n"));
7138 } else if (!zfs_is_shared(zhp)) {
7139 (void) fprintf(stderr, gettext("cannot unshare '%s': "
7140 "not currently shared\n"), path);
7142 ret = zfs_unshareall_bypath(zhp, path);
7143 zfs_commit_all_shares();
7146 char mtpt_prop[ZFS_MAXPROPLEN];
7148 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
7149 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
7152 ret = zfs_unmount(zhp, NULL, flags);
7153 } else if (strcmp(mtpt_prop, "legacy") == 0) {
7154 (void) fprintf(stderr, gettext("cannot unmount "
7155 "'%s': legacy mountpoint\n"),
7157 (void) fprintf(stderr, gettext("use umount(8) "
7158 "to unmount this filesystem\n"));
7160 ret = zfs_unmountall(zhp, flags);
7171 * Generic callback for unsharing or unmounting a filesystem.
7174 unshare_unmount(int op, int argc, char **argv)
7181 char nfs_mnt_prop[ZFS_MAXPROPLEN];
7182 char sharesmb[ZFS_MAXPROPLEN];
7185 while ((c = getopt(argc, argv, op == OP_SHARE ? ":a" : "afu")) != -1) {
7197 (void) fprintf(stderr, gettext("missing argument for "
7198 "'%c' option\n"), optopt);
7202 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7213 * We could make use of zfs_for_each() to walk all datasets in
7214 * the system, but this would be very inefficient, especially
7215 * since we would have to linearly search /proc/self/mounts for
7216 * each one. Instead, do one pass through /proc/self/mounts
7217 * looking for zfs entries and call zfs_unmount() for each one.
7219 * Things get a little tricky if the administrator has created
7220 * mountpoints beneath other ZFS filesystems. In this case, we
7221 * have to unmount the deepest filesystems first. To accomplish
7222 * this, we place all the mountpoints in an AVL tree sorted by
7223 * the special type (dataset name), and walk the result in
7224 * reverse to make sure to get any snapshots first.
7226 struct mnttab entry;
7227 uu_avl_pool_t *pool;
7228 uu_avl_t *tree = NULL;
7229 unshare_unmount_node_t *node;
7231 uu_avl_walk_t *walk;
7232 char *protocol = NULL;
7234 if (op == OP_SHARE && argc > 0) {
7235 if (strcmp(argv[0], "nfs") != 0 &&
7236 strcmp(argv[0], "smb") != 0) {
7237 (void) fprintf(stderr, gettext("share type "
7238 "must be 'nfs' or 'smb'\n"));
7247 (void) fprintf(stderr, gettext("too many arguments\n"));
7251 if (((pool = uu_avl_pool_create("unmount_pool",
7252 sizeof (unshare_unmount_node_t),
7253 offsetof(unshare_unmount_node_t, un_avlnode),
7254 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
7255 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
7258 /* Reopen MNTTAB to prevent reading stale data from open file */
7259 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
7262 while (getmntent(mnttab_file, &entry) == 0) {
7264 /* ignore non-ZFS entries */
7265 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
7268 /* ignore snapshots */
7269 if (strchr(entry.mnt_special, '@') != NULL)
7272 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
7273 ZFS_TYPE_FILESYSTEM)) == NULL) {
7279 * Ignore datasets that are excluded/restricted by
7282 if (zpool_skip_pool(zfs_get_pool_name(zhp))) {
7289 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
7291 sizeof (nfs_mnt_prop),
7292 NULL, NULL, 0, B_FALSE) == 0);
7293 if (strcmp(nfs_mnt_prop, "off") != 0)
7295 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
7297 sizeof (nfs_mnt_prop),
7298 NULL, NULL, 0, B_FALSE) == 0);
7299 if (strcmp(nfs_mnt_prop, "off") == 0)
7303 /* Ignore legacy mounts */
7304 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
7306 sizeof (nfs_mnt_prop),
7307 NULL, NULL, 0, B_FALSE) == 0);
7308 if (strcmp(nfs_mnt_prop, "legacy") == 0)
7310 /* Ignore canmount=noauto mounts */
7311 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
7312 ZFS_CANMOUNT_NOAUTO)
7318 node = safe_malloc(sizeof (unshare_unmount_node_t));
7320 node->un_mountp = safe_strdup(entry.mnt_mountp);
7322 uu_avl_node_init(node, &node->un_avlnode, pool);
7324 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
7325 uu_avl_insert(tree, node, idx);
7327 zfs_close(node->un_zhp);
7328 free(node->un_mountp);
7334 * Walk the AVL tree in reverse, unmounting each filesystem and
7335 * removing it from the AVL tree in the process.
7337 if ((walk = uu_avl_walk_start(tree,
7338 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
7341 while ((node = uu_avl_walk_next(walk)) != NULL) {
7342 const char *mntarg = NULL;
7344 uu_avl_remove(tree, node);
7347 if (zfs_unshareall_bytype(node->un_zhp,
7348 node->un_mountp, protocol) != 0)
7353 if (zfs_unmount(node->un_zhp,
7354 mntarg, flags) != 0)
7359 zfs_close(node->un_zhp);
7360 free(node->un_mountp);
7365 zfs_commit_shares(protocol);
7367 uu_avl_walk_end(walk);
7368 uu_avl_destroy(tree);
7369 uu_avl_pool_destroy(pool);
7374 (void) fprintf(stderr,
7375 gettext("missing filesystem argument\n"));
7377 (void) fprintf(stderr,
7378 gettext("too many arguments\n"));
7383 * We have an argument, but it may be a full path or a ZFS
7384 * filesystem. Pass full paths off to unmount_path() (shared by
7385 * manual_unmount), otherwise open the filesystem and pass to
7388 if (argv[0][0] == '/')
7389 return (unshare_unmount_path(op, argv[0],
7392 if ((zhp = zfs_open(g_zfs, argv[0],
7393 ZFS_TYPE_FILESYSTEM)) == NULL)
7396 verify(zfs_prop_get(zhp, op == OP_SHARE ?
7397 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
7398 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
7399 NULL, 0, B_FALSE) == 0);
7403 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
7405 sizeof (nfs_mnt_prop),
7406 NULL, NULL, 0, B_FALSE) == 0);
7407 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
7408 sharesmb, sizeof (sharesmb), NULL, NULL,
7411 if (strcmp(nfs_mnt_prop, "off") == 0 &&
7412 strcmp(sharesmb, "off") == 0) {
7413 (void) fprintf(stderr, gettext("cannot "
7414 "unshare '%s': legacy share\n"),
7416 (void) fprintf(stderr, gettext("use "
7417 "unshare(1M) to unshare this "
7420 } else if (!zfs_is_shared(zhp)) {
7421 (void) fprintf(stderr, gettext("cannot "
7422 "unshare '%s': not currently "
7423 "shared\n"), zfs_get_name(zhp));
7425 } else if (zfs_unshareall(zhp) != 0) {
7431 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
7432 (void) fprintf(stderr, gettext("cannot "
7433 "unmount '%s': legacy "
7434 "mountpoint\n"), zfs_get_name(zhp));
7435 (void) fprintf(stderr, gettext("use "
7436 "umount(1M) to unmount this "
7439 } else if (!zfs_is_mounted(zhp, NULL)) {
7440 (void) fprintf(stderr, gettext("cannot "
7441 "unmount '%s': not currently "
7445 } else if (zfs_unmountall(zhp, flags) != 0) {
7458 * zfs unmount [-fu] -a
7459 * zfs unmount [-fu] filesystem
7461 * Unmount all filesystems, or a specific ZFS filesystem.
7464 zfs_do_unmount(int argc, char **argv)
7466 return (unshare_unmount(OP_MOUNT, argc, argv));
7471 * zfs unshare filesystem
7473 * Unshare all filesystems, or a specific ZFS filesystem.
7476 zfs_do_unshare(int argc, char **argv)
7478 return (unshare_unmount(OP_SHARE, argc, argv));
7482 find_command_idx(char *command, int *idx)
7486 for (i = 0; i < NCOMMAND; i++) {
7487 if (command_table[i].name == NULL)
7490 if (strcmp(command, command_table[i].name) == 0) {
7499 zfs_do_diff(int argc, char **argv)
7503 char *tosnap = NULL;
7504 char *fromsnap = NULL;
7508 struct sigaction sa;
7510 while ((c = getopt(argc, argv, "FHt")) != -1) {
7513 flags |= ZFS_DIFF_CLASSIFY;
7516 flags |= ZFS_DIFF_PARSEABLE;
7519 flags |= ZFS_DIFF_TIMESTAMP;
7522 (void) fprintf(stderr,
7523 gettext("invalid option '%c'\n"), optopt);
7532 (void) fprintf(stderr,
7533 gettext("must provide at least one snapshot name\n"));
7538 (void) fprintf(stderr, gettext("too many arguments\n"));
7543 tosnap = (argc == 2) ? argv[1] : NULL;
7546 if (*fromsnap != '@')
7547 copy = strdup(fromsnap);
7549 copy = strdup(tosnap);
7553 if ((atp = strchr(copy, '@')) != NULL)
7556 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL) {
7563 * Ignore SIGPIPE so that the library can give us
7564 * information on any failure
7566 if (sigemptyset(&sa.sa_mask) == -1) {
7571 sa.sa_handler = SIG_IGN;
7572 if (sigaction(SIGPIPE, &sa, NULL) == -1) {
7577 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
7585 * zfs bookmark <fs@source>|<fs#source> <fs#bookmark>
7587 * Creates a bookmark with the given name from the source snapshot
7588 * or creates a copy of an existing source bookmark.
7591 zfs_do_bookmark(int argc, char **argv)
7593 char *source, *bookname;
7594 char expbuf[ZFS_MAX_DATASET_NAME_LEN];
7601 while ((c = getopt(argc, argv, "")) != -1) {
7604 (void) fprintf(stderr,
7605 gettext("invalid option '%c'\n"), optopt);
7613 /* check number of arguments */
7615 (void) fprintf(stderr, gettext("missing source argument\n"));
7619 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
7626 if (strchr(source, '@') == NULL && strchr(source, '#') == NULL) {
7627 (void) fprintf(stderr,
7628 gettext("invalid source name '%s': "
7629 "must contain a '@' or '#'\n"), source);
7632 if (strchr(bookname, '#') == NULL) {
7633 (void) fprintf(stderr,
7634 gettext("invalid bookmark name '%s': "
7635 "must contain a '#'\n"), bookname);
7640 * expand source or bookname to full path:
7641 * one of them may be specified as short name
7645 char *source_short, *bookname_short;
7646 source_short = strpbrk(source, "@#");
7647 bookname_short = strpbrk(bookname, "#");
7648 if (source_short == source &&
7649 bookname_short == bookname) {
7650 (void) fprintf(stderr, gettext(
7651 "either source or bookmark must be specified as "
7652 "full dataset paths"));
7654 } else if (source_short != source &&
7655 bookname_short != bookname) {
7657 } else if (source_short != source) {
7658 strlcpy(expbuf, source, sizeof (expbuf));
7660 } else if (bookname_short != bookname) {
7661 strlcpy(expbuf, bookname, sizeof (expbuf));
7666 if (expand != NULL) {
7667 *strpbrk(expbuf, "@#") = '\0'; /* dataset name in buf */
7668 (void) strlcat(expbuf, *expand, sizeof (expbuf));
7673 /* determine source type */
7674 switch (*strpbrk(source, "@#")) {
7675 case '@': source_type = ZFS_TYPE_SNAPSHOT; break;
7676 case '#': source_type = ZFS_TYPE_BOOKMARK; break;
7680 /* test the source exists */
7682 zhp = zfs_open(g_zfs, source, source_type);
7687 nvl = fnvlist_alloc();
7688 fnvlist_add_string(nvl, bookname, source);
7689 ret = lzc_bookmark(nvl, NULL);
7693 const char *err_msg = NULL;
7696 (void) snprintf(errbuf, sizeof (errbuf),
7697 dgettext(TEXT_DOMAIN,
7698 "cannot create bookmark '%s'"), bookname);
7702 err_msg = "bookmark is in a different pool";
7704 case ZFS_ERR_BOOKMARK_SOURCE_NOT_ANCESTOR:
7705 err_msg = "source is not an ancestor of the "
7706 "new bookmark's dataset";
7709 err_msg = "bookmark exists";
7712 err_msg = "invalid argument";
7715 err_msg = "bookmark feature not enabled";
7718 err_msg = "out of space";
7721 err_msg = "dataset does not exist";
7724 (void) zfs_standard_error(g_zfs, ret, errbuf);
7727 if (err_msg != NULL) {
7728 (void) fprintf(stderr, "%s: %s\n", errbuf,
7729 dgettext(TEXT_DOMAIN, err_msg));
7741 zfs_do_channel_program(int argc, char **argv)
7744 char *progbuf, *filename, *poolname;
7745 size_t progsize, progread;
7746 nvlist_t *outnvl = NULL;
7747 uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT;
7748 uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT;
7749 boolean_t sync_flag = B_TRUE, json_output = B_FALSE;
7750 zpool_handle_t *zhp;
7753 while ((c = getopt(argc, argv, "nt:m:j")) != -1) {
7761 arg = strtoull(optarg, &endp, 0);
7762 if (errno != 0 || *endp != '\0') {
7763 (void) fprintf(stderr, gettext(
7765 "'%s': expected integer\n"), optarg);
7772 ASSERT3U(c, ==, 'm');
7778 sync_flag = B_FALSE;
7782 json_output = B_TRUE;
7786 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7796 (void) fprintf(stderr,
7797 gettext("invalid number of arguments\n"));
7803 if (strcmp(filename, "-") == 0) {
7805 filename = "standard input";
7806 } else if ((fd = open(filename, O_RDONLY)) < 0) {
7807 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
7808 filename, strerror(errno));
7812 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
7813 (void) fprintf(stderr, gettext("cannot open pool '%s'\n"),
7822 * Read in the channel program, expanding the program buffer as
7827 progbuf = safe_malloc(progsize);
7829 ret = read(fd, progbuf + progread, progsize - progread);
7831 if (progread == progsize && ret > 0) {
7833 progbuf = safe_realloc(progbuf, progsize);
7841 (void) fprintf(stderr,
7842 gettext("cannot read '%s': %s\n"),
7843 filename, strerror(errno));
7846 progbuf[progread] = '\0';
7849 * Any remaining arguments are passed as arguments to the lua script as
7852 * "argv" -> [ "arg 1", ... "arg n" ],
7855 nvlist_t *argnvl = fnvlist_alloc();
7856 fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV, argv + 2, argc - 2);
7859 ret = lzc_channel_program(poolname, progbuf,
7860 instrlimit, memlimit, argnvl, &outnvl);
7862 ret = lzc_channel_program_nosync(poolname, progbuf,
7863 instrlimit, memlimit, argnvl, &outnvl);
7868 * On error, report the error message handed back by lua if one
7869 * exists. Otherwise, generate an appropriate error message,
7870 * falling back on strerror() for an unexpected return code.
7872 char *errstring = NULL;
7873 const char *msg = gettext("Channel program execution failed");
7874 uint64_t instructions = 0;
7875 if (outnvl != NULL && nvlist_exists(outnvl, ZCP_RET_ERROR)) {
7876 (void) nvlist_lookup_string(outnvl,
7877 ZCP_RET_ERROR, &errstring);
7878 if (errstring == NULL)
7879 errstring = strerror(ret);
7881 (void) nvlist_lookup_uint64(outnvl,
7882 ZCP_ARG_INSTRLIMIT, &instructions);
7888 "Invalid instruction or memory limit.";
7891 errstring = "Return value too large.";
7894 errstring = "Memory limit exhausted.";
7897 errstring = "Timed out.";
7900 errstring = "Permission denied. Channel "
7901 "programs must be run as root.";
7904 (void) zfs_standard_error(g_zfs, ret, msg);
7907 if (errstring != NULL)
7908 (void) fprintf(stderr, "%s:\n%s\n", msg, errstring);
7910 if (ret == ETIME && instructions != 0)
7911 (void) fprintf(stderr,
7912 gettext("%llu Lua instructions\n"),
7913 (u_longlong_t)instructions);
7916 (void) nvlist_print_json(stdout, outnvl);
7917 } else if (nvlist_empty(outnvl)) {
7918 (void) fprintf(stdout, gettext("Channel program fully "
7919 "executed and did not produce output.\n"));
7921 (void) fprintf(stdout, gettext("Channel program fully "
7922 "executed and produced output:\n"));
7923 dump_nvlist(outnvl, 4);
7928 fnvlist_free(outnvl);
7929 fnvlist_free(argnvl);
7938 typedef struct loadkey_cbdata {
7939 boolean_t cb_loadkey;
7940 boolean_t cb_recursive;
7942 char *cb_keylocation;
7943 uint64_t cb_numfailed;
7944 uint64_t cb_numattempted;
7948 load_key_callback(zfs_handle_t *zhp, void *data)
7951 boolean_t is_encroot;
7952 loadkey_cbdata_t *cb = data;
7953 uint64_t keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
7956 * If we are working recursively, we want to skip loading / unloading
7957 * keys for non-encryption roots and datasets whose keys are already
7958 * in the desired end-state.
7960 if (cb->cb_recursive) {
7961 ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
7967 if ((cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_AVAILABLE) ||
7968 (!cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_UNAVAILABLE))
7972 cb->cb_numattempted++;
7975 ret = zfs_crypto_load_key(zhp, cb->cb_noop, cb->cb_keylocation);
7977 ret = zfs_crypto_unload_key(zhp);
7988 load_unload_keys(int argc, char **argv, boolean_t loadkey)
7990 int c, ret = 0, flags = 0;
7991 boolean_t do_all = B_FALSE;
7992 loadkey_cbdata_t cb = { 0 };
7994 cb.cb_loadkey = loadkey;
7996 while ((c = getopt(argc, argv, "anrL:")) != -1) {
7997 /* noop and alternate keylocations only apply to zfs load-key */
8001 cb.cb_noop = B_TRUE;
8004 cb.cb_keylocation = optarg;
8014 cb.cb_recursive = B_TRUE;
8017 flags |= ZFS_ITER_RECURSE;
8018 cb.cb_recursive = B_TRUE;
8021 (void) fprintf(stderr,
8022 gettext("invalid option '%c'\n"), optopt);
8030 if (!do_all && argc == 0) {
8031 (void) fprintf(stderr,
8032 gettext("Missing dataset argument or -a option\n"));
8036 if (do_all && argc != 0) {
8037 (void) fprintf(stderr,
8038 gettext("Cannot specify dataset with -a option\n"));
8042 if (cb.cb_recursive && cb.cb_keylocation != NULL &&
8043 strcmp(cb.cb_keylocation, "prompt") != 0) {
8044 (void) fprintf(stderr, gettext("alternate keylocation may only "
8045 "be 'prompt' with -r or -a\n"));
8049 ret = zfs_for_each(argc, argv, flags,
8050 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, NULL, NULL, 0,
8051 load_key_callback, &cb);
8053 if (cb.cb_noop || (cb.cb_recursive && cb.cb_numattempted != 0)) {
8054 (void) printf(gettext("%llu / %llu key(s) successfully %s\n"),
8055 (u_longlong_t)(cb.cb_numattempted - cb.cb_numfailed),
8056 (u_longlong_t)cb.cb_numattempted,
8057 loadkey ? (cb.cb_noop ? "verified" : "loaded") :
8061 if (cb.cb_numfailed != 0)
8068 zfs_do_load_key(int argc, char **argv)
8070 return (load_unload_keys(argc, argv, B_TRUE));
8075 zfs_do_unload_key(int argc, char **argv)
8077 return (load_unload_keys(argc, argv, B_FALSE));
8081 zfs_do_change_key(int argc, char **argv)
8085 boolean_t loadkey = B_FALSE, inheritkey = B_FALSE;
8086 zfs_handle_t *zhp = NULL;
8087 nvlist_t *props = fnvlist_alloc();
8089 while ((c = getopt(argc, argv, "lio:")) != -1) {
8095 inheritkey = B_TRUE;
8098 if (!parseprop(props, optarg)) {
8104 (void) fprintf(stderr,
8105 gettext("invalid option '%c'\n"), optopt);
8110 if (inheritkey && !nvlist_empty(props)) {
8111 (void) fprintf(stderr,
8112 gettext("Properties not allowed for inheriting\n"));
8120 (void) fprintf(stderr, gettext("Missing dataset argument\n"));
8125 (void) fprintf(stderr, gettext("Too many arguments\n"));
8129 zhp = zfs_open(g_zfs, argv[argc - 1],
8130 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
8135 keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
8136 if (keystatus != ZFS_KEYSTATUS_AVAILABLE) {
8137 ret = zfs_crypto_load_key(zhp, B_FALSE, NULL);
8145 /* refresh the properties so the new keystatus is visible */
8146 zfs_refresh_properties(zhp);
8149 ret = zfs_crypto_rewrap(zhp, props, inheritkey);
8162 * 1) zfs project [-d|-r] <file|directory ...>
8163 * List project ID and inherit flag of file(s) or directories.
8164 * -d: List the directory itself, not its children.
8165 * -r: List subdirectories recursively.
8167 * 2) zfs project -C [-k] [-r] <file|directory ...>
8168 * Clear project inherit flag and/or ID on the file(s) or directories.
8169 * -k: Keep the project ID unchanged. If not specified, the project ID
8170 * will be reset as zero.
8171 * -r: Clear on subdirectories recursively.
8173 * 3) zfs project -c [-0] [-d|-r] [-p id] <file|directory ...>
8174 * Check project ID and inherit flag on the file(s) or directories,
8175 * report the outliers.
8176 * -0: Print file name followed by a NUL instead of newline.
8177 * -d: Check the directory itself, not its children.
8178 * -p: Specify the referenced ID for comparing with the target file(s)
8179 * or directories' project IDs. If not specified, the target (top)
8180 * directory's project ID will be used as the referenced one.
8181 * -r: Check subdirectories recursively.
8183 * 4) zfs project [-p id] [-r] [-s] <file|directory ...>
8184 * Set project ID and/or inherit flag on the file(s) or directories.
8185 * -p: Set the project ID as the given id.
8186 * -r: Set on subdirectories recursively. If not specify "-p" option,
8187 * it will use top-level directory's project ID as the given id,
8188 * then set both project ID and inherit flag on all descendants
8189 * of the top-level directory.
8190 * -s: Set project inherit flag.
8193 zfs_do_project(int argc, char **argv)
8195 zfs_project_control_t zpc = {
8196 .zpc_expected_projid = ZFS_INVALID_PROJID,
8197 .zpc_op = ZFS_PROJECT_OP_DEFAULT,
8198 .zpc_dironly = B_FALSE,
8199 .zpc_keep_projid = B_FALSE,
8200 .zpc_newline = B_TRUE,
8201 .zpc_recursive = B_FALSE,
8202 .zpc_set_flag = B_FALSE,
8209 while ((c = getopt(argc, argv, "0Ccdkp:rs")) != -1) {
8212 zpc.zpc_newline = B_FALSE;
8215 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
8216 (void) fprintf(stderr, gettext("cannot "
8217 "specify '-C' '-c' '-s' together\n"));
8221 zpc.zpc_op = ZFS_PROJECT_OP_CLEAR;
8224 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
8225 (void) fprintf(stderr, gettext("cannot "
8226 "specify '-C' '-c' '-s' together\n"));
8230 zpc.zpc_op = ZFS_PROJECT_OP_CHECK;
8233 zpc.zpc_dironly = B_TRUE;
8234 /* overwrite "-r" option */
8235 zpc.zpc_recursive = B_FALSE;
8238 zpc.zpc_keep_projid = B_TRUE;
8244 zpc.zpc_expected_projid = strtoull(optarg, &endptr, 0);
8245 if (errno != 0 || *endptr != '\0') {
8246 (void) fprintf(stderr,
8247 gettext("project ID must be less than "
8248 "%u\n"), UINT32_MAX);
8251 if (zpc.zpc_expected_projid >= UINT32_MAX) {
8252 (void) fprintf(stderr,
8253 gettext("invalid project ID\n"));
8259 zpc.zpc_recursive = B_TRUE;
8260 /* overwrite "-d" option */
8261 zpc.zpc_dironly = B_FALSE;
8264 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
8265 (void) fprintf(stderr, gettext("cannot "
8266 "specify '-C' '-c' '-s' together\n"));
8270 zpc.zpc_set_flag = B_TRUE;
8271 zpc.zpc_op = ZFS_PROJECT_OP_SET;
8274 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8280 if (zpc.zpc_op == ZFS_PROJECT_OP_DEFAULT) {
8281 if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID)
8282 zpc.zpc_op = ZFS_PROJECT_OP_SET;
8284 zpc.zpc_op = ZFS_PROJECT_OP_LIST;
8287 switch (zpc.zpc_op) {
8288 case ZFS_PROJECT_OP_LIST:
8289 if (zpc.zpc_keep_projid) {
8290 (void) fprintf(stderr,
8291 gettext("'-k' is only valid together with '-C'\n"));
8294 if (!zpc.zpc_newline) {
8295 (void) fprintf(stderr,
8296 gettext("'-0' is only valid together with '-c'\n"));
8300 case ZFS_PROJECT_OP_CHECK:
8301 if (zpc.zpc_keep_projid) {
8302 (void) fprintf(stderr,
8303 gettext("'-k' is only valid together with '-C'\n"));
8307 case ZFS_PROJECT_OP_CLEAR:
8308 if (zpc.zpc_dironly) {
8309 (void) fprintf(stderr,
8310 gettext("'-d' is useless together with '-C'\n"));
8313 if (!zpc.zpc_newline) {
8314 (void) fprintf(stderr,
8315 gettext("'-0' is only valid together with '-c'\n"));
8318 if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID) {
8319 (void) fprintf(stderr,
8320 gettext("'-p' is useless together with '-C'\n"));
8324 case ZFS_PROJECT_OP_SET:
8325 if (zpc.zpc_dironly) {
8326 (void) fprintf(stderr,
8327 gettext("'-d' is useless for set project ID and/or "
8331 if (zpc.zpc_keep_projid) {
8332 (void) fprintf(stderr,
8333 gettext("'-k' is only valid together with '-C'\n"));
8336 if (!zpc.zpc_newline) {
8337 (void) fprintf(stderr,
8338 gettext("'-0' is only valid together with '-c'\n"));
8350 (void) fprintf(stderr,
8351 gettext("missing file or directory target(s)\n"));
8355 for (int i = 0; i < argc; i++) {
8358 err = zfs_project_handle(argv[i], &zpc);
8367 zfs_do_wait(int argc, char **argv)
8369 boolean_t enabled[ZFS_WAIT_NUM_ACTIVITIES];
8373 /* By default, wait for all types of activity. */
8374 for (i = 0; i < ZFS_WAIT_NUM_ACTIVITIES; i++)
8375 enabled[i] = B_TRUE;
8377 while ((c = getopt(argc, argv, "t:")) != -1) {
8381 static char *col_subopts[] = { "deleteq", NULL };
8384 /* Reset activities array */
8385 bzero(&enabled, sizeof (enabled));
8386 while (*optarg != '\0') {
8387 int activity = getsubopt(&optarg, col_subopts,
8391 (void) fprintf(stderr,
8392 gettext("invalid activity '%s'\n"),
8397 enabled[activity] = B_TRUE;
8402 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8411 (void) fprintf(stderr, gettext("missing 'filesystem' "
8416 (void) fprintf(stderr, gettext("too many arguments\n"));
8420 zfs_handle_t *zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM);
8425 boolean_t missing = B_FALSE;
8426 boolean_t any_waited = B_FALSE;
8428 for (int i = 0; i < ZFS_WAIT_NUM_ACTIVITIES; i++) {
8434 error = zfs_wait_status(zhp, i, &missing, &waited);
8435 if (error != 0 || missing)
8438 any_waited = (any_waited || waited);
8441 if (error != 0 || missing || !any_waited)
8451 * Display version message
8454 zfs_do_version(int argc, char **argv)
8456 if (zfs_version_print() == -1)
8463 main(int argc, char **argv)
8470 (void) setlocale(LC_ALL, "");
8471 (void) textdomain(TEXT_DOMAIN);
8476 * Make sure the user has specified some command.
8479 (void) fprintf(stderr, gettext("missing command\n"));
8486 * The 'umount' command is an alias for 'unmount'
8488 if (strcmp(cmdname, "umount") == 0)
8489 cmdname = "unmount";
8492 * The 'recv' command is an alias for 'receive'
8494 if (strcmp(cmdname, "recv") == 0)
8495 cmdname = "receive";
8498 * The 'snap' command is an alias for 'snapshot'
8500 if (strcmp(cmdname, "snap") == 0)
8501 cmdname = "snapshot";
8506 if ((strcmp(cmdname, "-?") == 0) ||
8507 (strcmp(cmdname, "--help") == 0))
8511 * Special case '-V|--version'
8513 if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0))
8514 return (zfs_do_version(argc, argv));
8516 if ((g_zfs = libzfs_init()) == NULL) {
8517 (void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
8521 mnttab_file = g_zfs->libzfs_mnttab;
8523 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
8525 libzfs_print_on_error(g_zfs, B_TRUE);
8528 * Many commands modify input strings for string parsing reasons.
8529 * We create a copy to protect the original argv.
8531 newargv = malloc((argc + 1) * sizeof (newargv[0]));
8532 for (i = 0; i < argc; i++)
8533 newargv[i] = strdup(argv[i]);
8534 newargv[argc] = NULL;
8537 * Run the appropriate command.
8539 libzfs_mnttab_cache(g_zfs, B_TRUE);
8540 if (find_command_idx(cmdname, &i) == 0) {
8541 current_command = &command_table[i];
8542 ret = command_table[i].func(argc - 1, newargv + 1);
8543 } else if (strchr(cmdname, '=') != NULL) {
8544 verify(find_command_idx("set", &i) == 0);
8545 current_command = &command_table[i];
8546 ret = command_table[i].func(argc, newargv);
8548 (void) fprintf(stderr, gettext("unrecognized "
8549 "command '%s'\n"), cmdname);
8554 for (i = 0; i < argc; i++)
8558 if (ret == 0 && log_history)
8559 (void) zpool_log_history(g_zfs, history_str);
8564 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8565 * for the purposes of running ::findleaks.
8567 if (getenv("ZFS_ABORT") != NULL) {
8568 (void) printf("dumping core by request\n");
8576 #include <sys/jail.h>
8579 * Attach/detach the given dataset to/from the given jail
8583 zfs_do_jail_impl(int argc, char **argv, boolean_t attach)
8588 /* check number of arguments */
8590 (void) fprintf(stderr, gettext("missing argument(s)\n"));
8594 (void) fprintf(stderr, gettext("too many arguments\n"));
8598 jailid = jail_getid(argv[1]);
8600 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
8604 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
8608 ret = (zfs_jail(zhp, jailid, attach) != 0);
8615 * zfs jail jailid filesystem
8617 * Attach the given dataset to the given jail
8621 zfs_do_jail(int argc, char **argv)
8623 return (zfs_do_jail_impl(argc, argv, B_TRUE));
8627 * zfs unjail jailid filesystem
8629 * Detach the given dataset from the given jail
8633 zfs_do_unjail(int argc, char **argv)
8635 return (zfs_do_jail_impl(argc, argv, B_FALSE));