4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
29 * Copyright 2016 Nexenta Systems, Inc.
30 * Copyright (c) 2018 Datto Inc.
40 #include <libnvpair.h>
52 #include <sys/debug.h>
54 #include <sys/mkdev.h>
55 #include <sys/mntent.h>
56 #include <sys/mnttab.h>
57 #include <sys/mount.h>
59 #include <sys/fs/zfs.h>
60 #include <sys/systeminfo.h>
61 #include <sys/types.h>
63 #include <sys/zfs_project.h>
66 #include <libzfs_core.h>
68 #include <zfs_deleg.h>
73 #include <directory.h>
74 #endif /* HAVE_IDMAP */
78 #include "zfs_comutil.h"
79 #include "libzfs_impl.h"
80 #include "zfs_projectutil.h"
82 libzfs_handle_t
*g_zfs
;
84 static FILE *mnttab_file
;
85 static char history_str
[HIS_MAX_RECORD_LEN
];
86 static boolean_t log_history
= B_TRUE
;
88 static int zfs_do_clone(int argc
, char **argv
);
89 static int zfs_do_create(int argc
, char **argv
);
90 static int zfs_do_destroy(int argc
, char **argv
);
91 static int zfs_do_get(int argc
, char **argv
);
92 static int zfs_do_inherit(int argc
, char **argv
);
93 static int zfs_do_list(int argc
, char **argv
);
94 static int zfs_do_mount(int argc
, char **argv
);
95 static int zfs_do_rename(int argc
, char **argv
);
96 static int zfs_do_rollback(int argc
, char **argv
);
97 static int zfs_do_set(int argc
, char **argv
);
98 static int zfs_do_upgrade(int argc
, char **argv
);
99 static int zfs_do_snapshot(int argc
, char **argv
);
100 static int zfs_do_unmount(int argc
, char **argv
);
101 static int zfs_do_share(int argc
, char **argv
);
102 static int zfs_do_unshare(int argc
, char **argv
);
103 static int zfs_do_send(int argc
, char **argv
);
104 static int zfs_do_receive(int argc
, char **argv
);
105 static int zfs_do_promote(int argc
, char **argv
);
106 static int zfs_do_userspace(int argc
, char **argv
);
107 static int zfs_do_allow(int argc
, char **argv
);
108 static int zfs_do_unallow(int argc
, char **argv
);
109 static int zfs_do_hold(int argc
, char **argv
);
110 static int zfs_do_holds(int argc
, char **argv
);
111 static int zfs_do_release(int argc
, char **argv
);
112 static int zfs_do_diff(int argc
, char **argv
);
113 static int zfs_do_bookmark(int argc
, char **argv
);
114 static int zfs_do_channel_program(int argc
, char **argv
);
115 static int zfs_do_remap(int argc
, char **argv
);
116 static int zfs_do_load_key(int argc
, char **argv
);
117 static int zfs_do_unload_key(int argc
, char **argv
);
118 static int zfs_do_change_key(int argc
, char **argv
);
119 static int zfs_do_project(int argc
, char **argv
);
122 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
127 _umem_debug_init(void)
129 return ("default,verbose"); /* $UMEM_DEBUG setting */
133 _umem_logging_init(void)
135 return ("fail,contents"); /* $UMEM_LOGGING setting */
170 HELP_CHANNEL_PROGRAM
,
176 typedef struct zfs_command
{
178 int (*func
)(int argc
, char **argv
);
183 * Master command table. Each ZFS command has a name, associated function, and
184 * usage message. The usage messages need to be internationalized, so we have
185 * to have a function to return the usage message based on a command index.
187 * These commands are organized according to how they are displayed in the usage
188 * message. An empty command (one with a NULL name) indicates an empty line in
189 * the generic usage message.
191 static zfs_command_t command_table
[] = {
192 { "create", zfs_do_create
, HELP_CREATE
},
193 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
195 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
196 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
197 { "clone", zfs_do_clone
, HELP_CLONE
},
198 { "promote", zfs_do_promote
, HELP_PROMOTE
},
199 { "rename", zfs_do_rename
, HELP_RENAME
},
200 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
201 { "program", zfs_do_channel_program
, HELP_CHANNEL_PROGRAM
},
203 { "list", zfs_do_list
, HELP_LIST
},
205 { "set", zfs_do_set
, HELP_SET
},
206 { "get", zfs_do_get
, HELP_GET
},
207 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
208 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
210 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
211 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
212 { "projectspace", zfs_do_userspace
, HELP_PROJECTSPACE
},
214 { "project", zfs_do_project
, HELP_PROJECT
},
216 { "mount", zfs_do_mount
, HELP_MOUNT
},
217 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
218 { "share", zfs_do_share
, HELP_SHARE
},
219 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
221 { "send", zfs_do_send
, HELP_SEND
},
222 { "receive", zfs_do_receive
, HELP_RECEIVE
},
224 { "allow", zfs_do_allow
, HELP_ALLOW
},
226 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
228 { "hold", zfs_do_hold
, HELP_HOLD
},
229 { "holds", zfs_do_holds
, HELP_HOLDS
},
230 { "release", zfs_do_release
, HELP_RELEASE
},
231 { "diff", zfs_do_diff
, HELP_DIFF
},
232 { "remap", zfs_do_remap
, HELP_REMAP
},
233 { "load-key", zfs_do_load_key
, HELP_LOAD_KEY
},
234 { "unload-key", zfs_do_unload_key
, HELP_UNLOAD_KEY
},
235 { "change-key", zfs_do_change_key
, HELP_CHANGE_KEY
},
238 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
240 zfs_command_t
*current_command
;
243 get_usage(zfs_help_t idx
)
247 return (gettext("\tclone [-p] [-o property=value] ... "
248 "<snapshot> <filesystem|volume>\n"));
250 return (gettext("\tcreate [-p] [-o property=value] ... "
252 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
253 "-V <size> <volume>\n"));
255 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
256 "\tdestroy [-dnpRrv] "
257 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
258 "\tdestroy <filesystem|volume>#<bookmark>\n"));
260 return (gettext("\tget [-rHp] [-d max] "
261 "[-o \"all\" | field[,...]]\n"
262 "\t [-t type[,...]] [-s source[,...]]\n"
263 "\t <\"all\" | property[,...]> "
264 "[filesystem|volume|snapshot|bookmark] ...\n"));
266 return (gettext("\tinherit [-rS] <property> "
267 "<filesystem|volume|snapshot> ...\n"));
269 return (gettext("\tupgrade [-v]\n"
270 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
272 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
273 "[-s property]...\n\t [-S property]... [-t type[,...]] "
274 "[filesystem|volume|snapshot] ...\n"));
276 return (gettext("\tmount\n"
277 "\tmount [-lvO] [-o opts] <-a | filesystem>\n"));
279 return (gettext("\tpromote <clone-filesystem>\n"));
281 return (gettext("\treceive [-vnsFu] "
282 "[-o <property>=<value>] ... [-x <property>] ...\n"
283 "\t <filesystem|volume|snapshot>\n"
284 "\treceive [-vnsFu] [-o <property>=<value>] ... "
285 "[-x <property>] ... \n"
286 "\t [-d | -e] <filesystem>\n"
287 "\treceive -A <filesystem|volume>\n"));
289 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
290 "<filesystem|volume|snapshot>\n"
291 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
292 "\trename -r <snapshot> <snapshot>\n"));
294 return (gettext("\trollback [-rRf] <snapshot>\n"));
296 return (gettext("\tsend [-DnPpRvLecwb] [-[i|I] snapshot] "
298 "\tsend [-nvPLecw] [-i snapshot|bookmark] "
299 "<filesystem|volume|snapshot>\n"
300 "\tsend [-nvPe] -t <receive_resume_token>\n"));
302 return (gettext("\tset <property=value> ... "
303 "<filesystem|volume|snapshot> ...\n"));
305 return (gettext("\tshare [-l] <-a [nfs|smb] | filesystem>\n"));
307 return (gettext("\tsnapshot [-r] [-o property=value] ... "
308 "<filesystem|volume>@<snap> ...\n"));
310 return (gettext("\tunmount [-f] "
311 "<-a | filesystem|mountpoint>\n"));
313 return (gettext("\tunshare "
314 "<-a [nfs|smb] | filesystem|mountpoint>\n"));
316 return (gettext("\tallow <filesystem|volume>\n"
318 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
319 "\t <filesystem|volume>\n"
320 "\tallow [-ld] -e <perm|@setname>[,...] "
321 "<filesystem|volume>\n"
322 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
323 "\tallow -s @setname <perm|@setname>[,...] "
324 "<filesystem|volume>\n"));
326 return (gettext("\tunallow [-rldug] "
327 "<\"everyone\"|user|group>[,...]\n"
328 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
329 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
330 "<filesystem|volume>\n"
331 "\tunallow [-r] -c [<perm|@setname>[,...]] "
332 "<filesystem|volume>\n"
333 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
334 "<filesystem|volume>\n"));
336 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
338 "\t [-S field] ... [-t type[,...]] "
339 "<filesystem|snapshot>\n"));
340 case HELP_GROUPSPACE
:
341 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
343 "\t [-S field] ... [-t type[,...]] "
344 "<filesystem|snapshot>\n"));
345 case HELP_PROJECTSPACE
:
346 return (gettext("\tprojectspace [-Hp] [-o field[,...]] "
348 "\t [-S field] ... <filesystem|snapshot>\n"));
350 return (gettext("\tproject [-d|-r] <directory|file ...>\n"
351 "\tproject -c [-0] [-d|-r] [-p id] <directory|file ...>\n"
352 "\tproject -C [-k] [-r] <directory ...>\n"
353 "\tproject [-p id] [-r] [-s] <directory ...>\n"));
355 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
357 return (gettext("\tholds [-rH] <snapshot> ...\n"));
359 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
361 return (gettext("\tdiff [-FHt] <snapshot> "
362 "[snapshot|filesystem]\n"));
364 return (gettext("\tremap <filesystem | volume>\n"));
366 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
367 case HELP_CHANNEL_PROGRAM
:
368 return (gettext("\tprogram [-jn] [-t <instruction limit>] "
369 "[-m <memory limit (b)>] <pool> <program file> "
372 return (gettext("\tload-key [-rn] [-L <keylocation>] "
373 "<-a | filesystem|volume>\n"));
374 case HELP_UNLOAD_KEY
:
375 return (gettext("\tunload-key [-r] "
376 "<-a | filesystem|volume>\n"));
377 case HELP_CHANGE_KEY
:
378 return (gettext("\tchange-key [-l] [-o keyformat=<value>]\n"
379 "\t [-o keylocation=<value>] [-o pbkfd2iters=<value>]\n"
380 "\t <filesystem|volume>\n"
381 "\tchange-key -i [-l] <filesystem|volume>\n"));
391 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
396 * Utility function to guarantee malloc() success.
400 safe_malloc(size_t size
)
404 if ((data
= calloc(1, size
)) == NULL
)
411 safe_realloc(void *data
, size_t size
)
414 if ((newp
= realloc(data
, size
)) == NULL
) {
423 safe_strdup(char *str
)
425 char *dupstr
= strdup(str
);
434 * Callback routine that will print out information for each of
438 usage_prop_cb(int prop
, void *cb
)
442 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
444 if (zfs_prop_readonly(prop
))
445 (void) fprintf(fp
, " NO ");
447 (void) fprintf(fp
, "YES ");
449 if (zfs_prop_inheritable(prop
))
450 (void) fprintf(fp
, " YES ");
452 (void) fprintf(fp
, " NO ");
454 if (zfs_prop_values(prop
) == NULL
)
455 (void) fprintf(fp
, "-\n");
457 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
463 * Display usage message. If we're inside a command, display only the usage for
464 * that command. Otherwise, iterate over the entire command table and display
465 * a complete usage message.
468 usage(boolean_t requested
)
471 boolean_t show_properties
= B_FALSE
;
472 FILE *fp
= requested
? stdout
: stderr
;
474 if (current_command
== NULL
) {
476 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
478 gettext("where 'command' is one of the following:\n\n"));
480 for (i
= 0; i
< NCOMMAND
; i
++) {
481 if (command_table
[i
].name
== NULL
)
482 (void) fprintf(fp
, "\n");
484 (void) fprintf(fp
, "%s",
485 get_usage(command_table
[i
].usage
));
488 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
489 "pool/[dataset/]*dataset[@name]\n"));
491 (void) fprintf(fp
, gettext("usage:\n"));
492 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
495 if (current_command
!= NULL
&&
496 (strcmp(current_command
->name
, "set") == 0 ||
497 strcmp(current_command
->name
, "get") == 0 ||
498 strcmp(current_command
->name
, "inherit") == 0 ||
499 strcmp(current_command
->name
, "list") == 0))
500 show_properties
= B_TRUE
;
502 if (show_properties
) {
504 gettext("\nThe following properties are supported:\n"));
506 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
507 "PROPERTY", "EDIT", "INHERIT", "VALUES");
509 /* Iterate over all properties */
510 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
513 (void) fprintf(fp
, "\t%-15s ", "userused@...");
514 (void) fprintf(fp
, " NO NO <size>\n");
515 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
516 (void) fprintf(fp
, " NO NO <size>\n");
517 (void) fprintf(fp
, "\t%-15s ", "projectused@...");
518 (void) fprintf(fp
, " NO NO <size>\n");
519 (void) fprintf(fp
, "\t%-15s ", "userobjused@...");
520 (void) fprintf(fp
, " NO NO <size>\n");
521 (void) fprintf(fp
, "\t%-15s ", "groupobjused@...");
522 (void) fprintf(fp
, " NO NO <size>\n");
523 (void) fprintf(fp
, "\t%-15s ", "projectobjused@...");
524 (void) fprintf(fp
, " NO NO <size>\n");
525 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
526 (void) fprintf(fp
, "YES NO <size> | none\n");
527 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
528 (void) fprintf(fp
, "YES NO <size> | none\n");
529 (void) fprintf(fp
, "\t%-15s ", "projectquota@...");
530 (void) fprintf(fp
, "YES NO <size> | none\n");
531 (void) fprintf(fp
, "\t%-15s ", "userobjquota@...");
532 (void) fprintf(fp
, "YES NO <size> | none\n");
533 (void) fprintf(fp
, "\t%-15s ", "groupobjquota@...");
534 (void) fprintf(fp
, "YES NO <size> | none\n");
535 (void) fprintf(fp
, "\t%-15s ", "projectobjquota@...");
536 (void) fprintf(fp
, "YES NO <size> | none\n");
537 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
538 (void) fprintf(fp
, " NO NO <size>\n");
540 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
541 "with standard units such as K, M, G, etc.\n"));
542 (void) fprintf(fp
, gettext("\nUser-defined properties can "
543 "be specified by using a name containing a colon (:).\n"));
544 (void) fprintf(fp
, gettext("\nThe {user|group|project}"
545 "[obj]{used|quota}@ properties must be appended with\n"
546 "a user|group|project specifier of one of these forms:\n"
547 " POSIX name (eg: \"matt\")\n"
548 " POSIX id (eg: \"126829\")\n"
549 " SMB name@domain (eg: \"matt@sun\")\n"
550 " SMB SID (eg: \"S-1-234-567-89\")\n"));
553 gettext("\nFor the property list, run: %s\n"),
556 gettext("\nFor the delegated permission list, run: %s\n"),
557 "zfs allow|unallow");
561 * See comments at end of main().
563 if (getenv("ZFS_ABORT") != NULL
) {
564 (void) printf("dumping core by request\n");
568 exit(requested
? 0 : 2);
572 * Take a property=value argument string and add it to the given nvlist.
573 * Modifies the argument inplace.
576 parseprop(nvlist_t
*props
, char *propname
)
580 if ((propval
= strchr(propname
, '=')) == NULL
) {
581 (void) fprintf(stderr
, gettext("missing "
582 "'=' for property=value argument\n"));
587 if (nvlist_exists(props
, propname
)) {
588 (void) fprintf(stderr
, gettext("property '%s' "
589 "specified multiple times\n"), propname
);
592 if (nvlist_add_string(props
, propname
, propval
) != 0)
598 * Take a property name argument and add it to the given nvlist.
599 * Modifies the argument inplace.
602 parsepropname(nvlist_t
*props
, char *propname
)
604 if (strchr(propname
, '=') != NULL
) {
605 (void) fprintf(stderr
, gettext("invalid character "
606 "'=' in property argument\n"));
609 if (nvlist_exists(props
, propname
)) {
610 (void) fprintf(stderr
, gettext("property '%s' "
611 "specified multiple times\n"), propname
);
614 if (nvlist_add_boolean(props
, propname
) != 0)
620 parse_depth(char *opt
, int *flags
)
625 depth
= (int)strtol(opt
, &tmp
, 0);
627 (void) fprintf(stderr
,
628 gettext("%s is not an integer\n"), optarg
);
632 (void) fprintf(stderr
,
633 gettext("Depth can not be negative.\n"));
636 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
640 #define PROGRESS_DELAY 2 /* seconds */
642 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";
643 static time_t pt_begin
;
644 static char *pt_header
= NULL
;
645 static boolean_t pt_shown
;
648 start_progress_timer(void)
650 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
655 set_progress_header(char *header
)
657 assert(pt_header
== NULL
);
658 pt_header
= safe_strdup(header
);
660 (void) printf("%s: ", header
);
661 (void) fflush(stdout
);
666 update_progress(char *update
)
668 if (!pt_shown
&& time(NULL
) > pt_begin
) {
669 int len
= strlen(update
);
671 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
673 (void) fflush(stdout
);
675 } else if (pt_shown
) {
676 int len
= strlen(update
);
678 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
679 (void) fflush(stdout
);
684 finish_progress(char *done
)
687 (void) printf("%s\n", done
);
688 (void) fflush(stdout
);
695 zfs_mount_and_share(libzfs_handle_t
*hdl
, const char *dataset
, zfs_type_t type
)
697 zfs_handle_t
*zhp
= NULL
;
700 zhp
= zfs_open(hdl
, dataset
, type
);
705 * Volumes may neither be mounted or shared. Potentially in the
706 * future filesystems detected on these volumes could be mounted.
708 if (zfs_get_type(zhp
) == ZFS_TYPE_VOLUME
) {
714 * Mount and/or share the new filesystem as appropriate. We provide a
715 * verbose error message to let the user know that their filesystem was
716 * in fact created, even if we failed to mount or share it.
718 * If the user doesn't want the dataset automatically mounted, then
719 * skip the mount/share step
721 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
, B_FALSE
) &&
722 zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
) {
723 if (geteuid() != 0) {
724 (void) fprintf(stderr
, gettext("filesystem "
725 "successfully created, but it may only be "
726 "mounted by root\n"));
728 } else if (zfs_mount(zhp
, NULL
, 0) != 0) {
729 (void) fprintf(stderr
, gettext("filesystem "
730 "successfully created, but not mounted\n"));
732 } else if (zfs_share(zhp
) != 0) {
733 (void) fprintf(stderr
, gettext("filesystem "
734 "successfully created, but not shared\n"));
745 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
747 * Given an existing dataset, create a writable copy whose initial contents
748 * are the same as the source. The newly created dataset maintains a
749 * dependency on the original; the original cannot be destroyed so long as
752 * The '-p' flag creates all the non-existing ancestors of the target first.
755 zfs_do_clone(int argc
, char **argv
)
757 zfs_handle_t
*zhp
= NULL
;
758 boolean_t parents
= B_FALSE
;
763 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
767 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
770 if (!parseprop(props
, optarg
)) {
779 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
788 /* check number of arguments */
790 (void) fprintf(stderr
, gettext("missing source dataset "
795 (void) fprintf(stderr
, gettext("missing target dataset "
800 (void) fprintf(stderr
, gettext("too many arguments\n"));
804 /* open the source dataset */
805 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
) {
810 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
813 * Now create the ancestors of the target dataset. If the
814 * target already exists and '-p' option was used we should not
817 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
823 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
831 ret
= zfs_clone(zhp
, argv
[1], props
);
833 /* create the mountpoint if necessary */
836 (void) zpool_log_history(g_zfs
, history_str
);
837 log_history
= B_FALSE
;
840 ret
= zfs_mount_and_share(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
849 ASSERT3P(zhp
, ==, NULL
);
856 * zfs create [-p] [-o prop=value] ... fs
857 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
859 * Create a new dataset. This command can be used to create filesystems
860 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
861 * For volumes, the user must specify a size to be used.
863 * The '-s' flag applies only to volumes, and indicates that we should not try
864 * to set the reservation for this volume. By default we set a reservation
865 * equal to the size for any volume. For pools with SPA_VERSION >=
866 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
868 * The '-p' flag creates all the non-existing ancestors of the target first.
871 zfs_do_create(int argc
, char **argv
)
873 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
874 uint64_t volsize
= 0;
876 boolean_t noreserve
= B_FALSE
;
877 boolean_t bflag
= B_FALSE
;
878 boolean_t parents
= B_FALSE
;
883 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
887 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
890 type
= ZFS_TYPE_VOLUME
;
891 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
892 (void) fprintf(stderr
, gettext("bad volume "
893 "size '%s': %s\n"), optarg
,
894 libzfs_error_description(g_zfs
));
898 if (nvlist_add_uint64(props
,
899 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
908 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
909 (void) fprintf(stderr
, gettext("bad volume "
910 "block size '%s': %s\n"), optarg
,
911 libzfs_error_description(g_zfs
));
915 if (nvlist_add_uint64(props
,
916 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
921 if (!parseprop(props
, optarg
))
928 (void) fprintf(stderr
, gettext("missing size "
932 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
938 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
939 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
940 "used when creating a volume\n"));
947 /* check number of arguments */
949 (void) fprintf(stderr
, gettext("missing %s argument\n"),
950 zfs_type_to_name(type
));
954 (void) fprintf(stderr
, gettext("too many arguments\n"));
958 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
959 zpool_handle_t
*zpool_handle
;
960 nvlist_t
*real_props
= NULL
;
961 uint64_t spa_version
;
963 zfs_prop_t resv_prop
;
967 if ((p
= strchr(argv
[0], '/')) != NULL
)
969 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
972 if (zpool_handle
== NULL
)
974 spa_version
= zpool_get_prop_int(zpool_handle
,
975 ZPOOL_PROP_VERSION
, NULL
);
976 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
977 resv_prop
= ZFS_PROP_REFRESERVATION
;
979 resv_prop
= ZFS_PROP_RESERVATION
;
981 (void) snprintf(msg
, sizeof (msg
),
982 gettext("cannot create '%s'"), argv
[0]);
983 if (props
&& (real_props
= zfs_valid_proplist(g_zfs
, type
,
984 props
, 0, NULL
, zpool_handle
, B_TRUE
, msg
)) == NULL
) {
985 zpool_close(zpool_handle
);
988 zpool_close(zpool_handle
);
990 volsize
= zvol_volsize_to_reservation(volsize
, real_props
);
991 nvlist_free(real_props
);
993 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
995 if (nvlist_add_uint64(props
,
996 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
1003 if (parents
&& zfs_name_valid(argv
[0], type
)) {
1005 * Now create the ancestors of target dataset. If the target
1006 * already exists and '-p' option was used we should not
1009 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
1013 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
1017 /* pass to libzfs */
1018 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
1022 (void) zpool_log_history(g_zfs
, history_str
);
1023 log_history
= B_FALSE
;
1026 ret
= zfs_mount_and_share(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
1037 * zfs destroy [-rRf] <fs, vol>
1038 * zfs destroy [-rRd] <snap>
1040 * -r Recursively destroy all children
1041 * -R Recursively destroy all dependents, including clones
1042 * -f Force unmounting of any dependents
1043 * -d If we can't destroy now, mark for deferred destruction
1045 * Destroys the given dataset. By default, it will unmount any filesystems,
1046 * and refuse to destroy a dataset that has any dependents. A dependent can
1047 * either be a child, or a clone of a child.
1049 typedef struct destroy_cbdata
{
1052 boolean_t cb_recurse
;
1054 boolean_t cb_doclones
;
1055 zfs_handle_t
*cb_target
;
1056 boolean_t cb_defer_destroy
;
1057 boolean_t cb_verbose
;
1058 boolean_t cb_parsable
;
1059 boolean_t cb_dryrun
;
1061 nvlist_t
*cb_batchedsnaps
;
1063 /* first snap in contiguous run */
1065 /* previous snap in contiguous run */
1067 int64_t cb_snapused
;
1070 uint64_t cb_snap_count
;
1074 * Check for any dependents based on the '-r' or '-R' flags.
1077 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
1079 destroy_cbdata_t
*cbp
= data
;
1080 const char *tname
= zfs_get_name(cbp
->cb_target
);
1081 const char *name
= zfs_get_name(zhp
);
1083 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
1084 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
1086 * This is a direct descendant, not a clone somewhere else in
1089 if (cbp
->cb_recurse
)
1092 if (cbp
->cb_first
) {
1093 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1094 "%s has children\n"),
1095 zfs_get_name(cbp
->cb_target
),
1096 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1097 (void) fprintf(stderr
, gettext("use '-r' to destroy "
1098 "the following datasets:\n"));
1099 cbp
->cb_first
= B_FALSE
;
1100 cbp
->cb_error
= B_TRUE
;
1103 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1106 * This is a clone. We only want to report this if the '-r'
1107 * wasn't specified, or the target is a snapshot.
1109 if (!cbp
->cb_recurse
&&
1110 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
1113 if (cbp
->cb_first
) {
1114 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1115 "%s has dependent clones\n"),
1116 zfs_get_name(cbp
->cb_target
),
1117 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1118 (void) fprintf(stderr
, gettext("use '-R' to destroy "
1119 "the following datasets:\n"));
1120 cbp
->cb_first
= B_FALSE
;
1121 cbp
->cb_error
= B_TRUE
;
1122 cbp
->cb_dryrun
= B_TRUE
;
1125 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1134 destroy_batched(destroy_cbdata_t
*cb
)
1136 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1137 cb
->cb_batchedsnaps
, B_FALSE
);
1138 fnvlist_free(cb
->cb_batchedsnaps
);
1139 cb
->cb_batchedsnaps
= fnvlist_alloc();
1144 destroy_callback(zfs_handle_t
*zhp
, void *data
)
1146 destroy_cbdata_t
*cb
= data
;
1147 const char *name
= zfs_get_name(zhp
);
1150 if (cb
->cb_verbose
) {
1151 if (cb
->cb_parsable
) {
1152 (void) printf("destroy\t%s\n", name
);
1153 } else if (cb
->cb_dryrun
) {
1154 (void) printf(gettext("would destroy %s\n"),
1157 (void) printf(gettext("will destroy %s\n"),
1163 * Ignore pools (which we've already flagged as an error before getting
1166 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1167 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1171 if (cb
->cb_dryrun
) {
1177 * We batch up all contiguous snapshots (even of different
1178 * filesystems) and destroy them with one ioctl. We can't
1179 * simply do all snap deletions and then all fs deletions,
1180 * because we must delete a clone before its origin.
1182 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1183 cb
->cb_snap_count
++;
1184 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1185 if (cb
->cb_snap_count
% 10 == 0 && cb
->cb_defer_destroy
)
1186 error
= destroy_batched(cb
);
1188 error
= destroy_batched(cb
);
1190 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1191 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1194 * When performing a recursive destroy we ignore errors
1195 * so that the recursive destroy could continue
1196 * destroying past problem datasets
1198 if (cb
->cb_recurse
) {
1199 cb
->cb_error
= B_TRUE
;
1211 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1213 destroy_cbdata_t
*cb
= arg
;
1214 const char *name
= zfs_get_name(zhp
);
1217 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1218 if (cb
->cb_firstsnap
== NULL
)
1219 cb
->cb_firstsnap
= strdup(name
);
1220 if (cb
->cb_prevsnap
!= NULL
)
1221 free(cb
->cb_prevsnap
);
1222 /* this snap continues the current range */
1223 cb
->cb_prevsnap
= strdup(name
);
1224 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1226 if (cb
->cb_verbose
) {
1227 if (cb
->cb_parsable
) {
1228 (void) printf("destroy\t%s\n", name
);
1229 } else if (cb
->cb_dryrun
) {
1230 (void) printf(gettext("would destroy %s\n"),
1233 (void) printf(gettext("will destroy %s\n"),
1237 } else if (cb
->cb_firstsnap
!= NULL
) {
1238 /* end of this range */
1240 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1241 cb
->cb_prevsnap
, &used
);
1242 cb
->cb_snapused
+= used
;
1243 free(cb
->cb_firstsnap
);
1244 cb
->cb_firstsnap
= NULL
;
1245 free(cb
->cb_prevsnap
);
1246 cb
->cb_prevsnap
= NULL
;
1253 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1256 assert(cb
->cb_firstsnap
== NULL
);
1257 assert(cb
->cb_prevsnap
== NULL
);
1258 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1259 if (cb
->cb_firstsnap
!= NULL
) {
1262 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1263 cb
->cb_prevsnap
, &used
);
1265 cb
->cb_snapused
+= used
;
1266 free(cb
->cb_firstsnap
);
1267 cb
->cb_firstsnap
= NULL
;
1268 free(cb
->cb_prevsnap
);
1269 cb
->cb_prevsnap
= NULL
;
1275 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1277 destroy_cbdata_t
*cb
= arg
;
1280 /* Check for clones. */
1281 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1282 cb
->cb_target
= zhp
;
1283 cb
->cb_first
= B_TRUE
;
1284 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1285 destroy_check_dependent
, cb
);
1289 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1297 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1299 destroy_cbdata_t
*cb
= arg
;
1302 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1308 if (cb
->cb_verbose
) {
1309 err
= destroy_print_snapshots(zhp
, cb
);
1315 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1323 destroy_clones(destroy_cbdata_t
*cb
)
1326 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1328 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1329 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1332 boolean_t defer
= cb
->cb_defer_destroy
;
1336 * We can't defer destroy non-snapshots, so set it to
1337 * false while destroying the clones.
1339 cb
->cb_defer_destroy
= B_FALSE
;
1340 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1341 destroy_callback
, cb
);
1342 cb
->cb_defer_destroy
= defer
;
1352 zfs_do_destroy(int argc
, char **argv
)
1354 destroy_cbdata_t cb
= { 0 };
1358 zfs_handle_t
*zhp
= NULL
;
1360 zfs_type_t type
= ZFS_TYPE_DATASET
;
1363 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1366 cb
.cb_verbose
= B_TRUE
;
1369 cb
.cb_verbose
= B_TRUE
;
1370 cb
.cb_parsable
= B_TRUE
;
1373 cb
.cb_dryrun
= B_TRUE
;
1376 cb
.cb_defer_destroy
= B_TRUE
;
1377 type
= ZFS_TYPE_SNAPSHOT
;
1380 cb
.cb_force
= B_TRUE
;
1383 cb
.cb_recurse
= B_TRUE
;
1386 cb
.cb_recurse
= B_TRUE
;
1387 cb
.cb_doclones
= B_TRUE
;
1391 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1400 /* check number of arguments */
1402 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1406 (void) fprintf(stderr
, gettext("too many arguments\n"));
1410 at
= strchr(argv
[0], '@');
1411 pound
= strchr(argv
[0], '#');
1414 /* Build the list of snaps to destroy in cb_nvl. */
1415 cb
.cb_nvl
= fnvlist_alloc();
1418 zhp
= zfs_open(g_zfs
, argv
[0],
1419 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1421 nvlist_free(cb
.cb_nvl
);
1425 cb
.cb_snapspec
= at
+ 1;
1426 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1432 if (nvlist_empty(cb
.cb_nvl
)) {
1433 (void) fprintf(stderr
, gettext("could not find any "
1434 "snapshots to destroy; check snapshot names.\n"));
1439 if (cb
.cb_verbose
) {
1441 zfs_nicebytes(cb
.cb_snapused
, buf
, sizeof (buf
));
1442 if (cb
.cb_parsable
) {
1443 (void) printf("reclaim\t%llu\n",
1444 (u_longlong_t
)cb
.cb_snapused
);
1445 } else if (cb
.cb_dryrun
) {
1446 (void) printf(gettext("would reclaim %s\n"),
1449 (void) printf(gettext("will reclaim %s\n"),
1454 if (!cb
.cb_dryrun
) {
1455 if (cb
.cb_doclones
) {
1456 cb
.cb_batchedsnaps
= fnvlist_alloc();
1457 err
= destroy_clones(&cb
);
1459 err
= zfs_destroy_snaps_nvl(g_zfs
,
1460 cb
.cb_batchedsnaps
, B_FALSE
);
1468 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1469 cb
.cb_defer_destroy
);
1475 } else if (pound
!= NULL
) {
1480 (void) fprintf(stderr
,
1481 "dryrun is not supported with bookmark\n");
1485 if (cb
.cb_defer_destroy
) {
1486 (void) fprintf(stderr
,
1487 "defer destroy is not supported with bookmark\n");
1491 if (cb
.cb_recurse
) {
1492 (void) fprintf(stderr
,
1493 "recursive is not supported with bookmark\n");
1497 if (!zfs_bookmark_exists(argv
[0])) {
1498 (void) fprintf(stderr
, gettext("bookmark '%s' "
1499 "does not exist.\n"), argv
[0]);
1503 nvl
= fnvlist_alloc();
1504 fnvlist_add_boolean(nvl
, argv
[0]);
1506 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1508 (void) zfs_standard_error(g_zfs
, err
,
1509 "cannot destroy bookmark");
1516 /* Open the given dataset */
1517 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1523 * Perform an explicit check for pools before going any further.
1525 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1526 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1527 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1528 "operation does not apply to pools\n"),
1530 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1531 "%s' to destroy all datasets in the pool\n"),
1533 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1534 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1540 * Check for any dependents and/or clones.
1542 cb
.cb_first
= B_TRUE
;
1543 if (!cb
.cb_doclones
&&
1544 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1554 cb
.cb_batchedsnaps
= fnvlist_alloc();
1555 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1562 * Do the real thing. The callback will close the
1563 * handle regardless of whether it succeeds or not.
1565 err
= destroy_callback(zhp
, &cb
);
1568 err
= zfs_destroy_snaps_nvl(g_zfs
,
1569 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1571 if (err
!= 0 || cb
.cb_error
== B_TRUE
)
1576 fnvlist_free(cb
.cb_batchedsnaps
);
1577 fnvlist_free(cb
.cb_nvl
);
1584 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1587 zfs_get_column_t col
;
1589 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1590 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1591 if (col
== GET_COL_RECVD
)
1597 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1598 * < all | property[,property]... > < fs | snap | vol > ...
1600 * -r recurse over any child datasets
1601 * -H scripted mode. Headers are stripped, and fields are separated
1602 * by tabs instead of spaces.
1603 * -o Set of fields to display. One of "name,property,value,
1604 * received,source". Default is "name,property,value,source".
1605 * "all" is an alias for all five.
1606 * -s Set of sources to allow. One of
1607 * "local,default,inherited,received,temporary,none". Default is
1609 * -p Display values in parsable (literal) format.
1611 * Prints properties for the given datasets. The user can control which
1612 * columns to display as well as which property types to allow.
1616 * Invoked to display the properties for a single dataset.
1619 get_callback(zfs_handle_t
*zhp
, void *data
)
1621 char buf
[ZFS_MAXPROPLEN
];
1622 char rbuf
[ZFS_MAXPROPLEN
];
1623 zprop_source_t sourcetype
;
1624 char source
[ZFS_MAX_DATASET_NAME_LEN
];
1625 zprop_get_cbdata_t
*cbp
= data
;
1626 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1627 zprop_list_t
*pl
= cbp
->cb_proplist
;
1631 boolean_t received
= is_recvd_column(cbp
);
1633 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1634 char *recvdval
= NULL
;
1636 * Skip the special fake placeholder. This will also skip over
1637 * the name property when 'all' is specified.
1639 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1640 pl
== cbp
->cb_proplist
)
1643 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1644 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1645 sizeof (buf
), &sourcetype
, source
,
1647 cbp
->cb_literal
) != 0) {
1650 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1651 ZFS_TYPE_DATASET
, B_FALSE
)) {
1652 (void) fprintf(stderr
,
1653 gettext("No such property '%s'\n"),
1654 zfs_prop_to_name(pl
->pl_prop
));
1657 sourcetype
= ZPROP_SRC_NONE
;
1658 (void) strlcpy(buf
, "-", sizeof (buf
));
1661 if (received
&& (zfs_prop_get_recvd(zhp
,
1662 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1663 cbp
->cb_literal
) == 0))
1666 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1667 zfs_prop_to_name(pl
->pl_prop
),
1668 buf
, sourcetype
, source
, recvdval
);
1669 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1670 sourcetype
= ZPROP_SRC_LOCAL
;
1672 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1673 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1674 sourcetype
= ZPROP_SRC_NONE
;
1675 (void) strlcpy(buf
, "-", sizeof (buf
));
1678 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1679 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1680 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1681 sourcetype
= ZPROP_SRC_LOCAL
;
1683 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1684 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1685 sourcetype
= ZPROP_SRC_NONE
;
1686 (void) strlcpy(buf
, "-", sizeof (buf
));
1689 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1690 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1692 if (nvlist_lookup_nvlist(user_props
,
1693 pl
->pl_user_prop
, &propval
) != 0) {
1696 sourcetype
= ZPROP_SRC_NONE
;
1699 verify(nvlist_lookup_string(propval
,
1700 ZPROP_VALUE
, &strval
) == 0);
1701 verify(nvlist_lookup_string(propval
,
1702 ZPROP_SOURCE
, &sourceval
) == 0);
1704 if (strcmp(sourceval
,
1705 zfs_get_name(zhp
)) == 0) {
1706 sourcetype
= ZPROP_SRC_LOCAL
;
1707 } else if (strcmp(sourceval
,
1708 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1709 sourcetype
= ZPROP_SRC_RECEIVED
;
1711 sourcetype
= ZPROP_SRC_INHERITED
;
1712 (void) strlcpy(source
,
1713 sourceval
, sizeof (source
));
1717 if (received
&& (zfs_prop_get_recvd(zhp
,
1718 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1719 cbp
->cb_literal
) == 0))
1722 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1723 pl
->pl_user_prop
, strval
, sourcetype
,
1732 zfs_do_get(int argc
, char **argv
)
1734 zprop_get_cbdata_t cb
= { 0 };
1735 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1736 int types
= ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
;
1737 char *value
, *fields
;
1740 zprop_list_t fake_name
= { 0 };
1743 * Set up default columns and sources.
1745 cb
.cb_sources
= ZPROP_SRC_ALL
;
1746 cb
.cb_columns
[0] = GET_COL_NAME
;
1747 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1748 cb
.cb_columns
[2] = GET_COL_VALUE
;
1749 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1750 cb
.cb_type
= ZFS_TYPE_DATASET
;
1753 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1756 cb
.cb_literal
= B_TRUE
;
1759 limit
= parse_depth(optarg
, &flags
);
1762 flags
|= ZFS_ITER_RECURSE
;
1765 cb
.cb_scripted
= B_TRUE
;
1768 (void) fprintf(stderr
, gettext("missing argument for "
1769 "'%c' option\n"), optopt
);
1774 * Process the set of columns to display. We zero out
1775 * the structure to give us a blank slate.
1777 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1779 while (*optarg
!= '\0') {
1780 static char *col_subopts
[] =
1781 { "name", "property", "value", "received",
1782 "source", "all", NULL
};
1784 if (i
== ZFS_GET_NCOLS
) {
1785 (void) fprintf(stderr
, gettext("too "
1786 "many fields given to -o "
1791 switch (getsubopt(&optarg
, col_subopts
,
1794 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1797 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1800 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1803 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1804 flags
|= ZFS_ITER_RECVD_PROPS
;
1807 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1811 (void) fprintf(stderr
,
1812 gettext("\"all\" conflicts "
1813 "with specific fields "
1814 "given to -o option\n"));
1817 cb
.cb_columns
[0] = GET_COL_NAME
;
1818 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1819 cb
.cb_columns
[2] = GET_COL_VALUE
;
1820 cb
.cb_columns
[3] = GET_COL_RECVD
;
1821 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1822 flags
|= ZFS_ITER_RECVD_PROPS
;
1826 (void) fprintf(stderr
,
1827 gettext("invalid column name "
1836 while (*optarg
!= '\0') {
1837 static char *source_subopts
[] = {
1838 "local", "default", "inherited",
1839 "received", "temporary", "none",
1842 switch (getsubopt(&optarg
, source_subopts
,
1845 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1848 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1851 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1854 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1857 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1860 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1863 (void) fprintf(stderr
,
1864 gettext("invalid source "
1873 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1874 while (*optarg
!= '\0') {
1875 static char *type_subopts
[] = { "filesystem",
1876 "volume", "snapshot", "bookmark",
1879 switch (getsubopt(&optarg
, type_subopts
,
1882 types
|= ZFS_TYPE_FILESYSTEM
;
1885 types
|= ZFS_TYPE_VOLUME
;
1888 types
|= ZFS_TYPE_SNAPSHOT
;
1891 types
|= ZFS_TYPE_BOOKMARK
;
1894 types
= ZFS_TYPE_DATASET
|
1899 (void) fprintf(stderr
,
1900 gettext("invalid type '%s'\n"),
1908 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1918 (void) fprintf(stderr
, gettext("missing property "
1925 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1933 * As part of zfs_expand_proplist(), we keep track of the maximum column
1934 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1935 * need to know the maximum name length. However, the user likely did
1936 * not specify 'name' as one of the properties to fetch, so we need to
1937 * make sure we always include at least this property for
1938 * print_get_headers() to work properly.
1940 if (cb
.cb_proplist
!= NULL
) {
1941 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1942 fake_name
.pl_width
= strlen(gettext("NAME"));
1943 fake_name
.pl_next
= cb
.cb_proplist
;
1944 cb
.cb_proplist
= &fake_name
;
1947 cb
.cb_first
= B_TRUE
;
1949 /* run for each object */
1950 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1951 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1953 if (cb
.cb_proplist
== &fake_name
)
1954 zprop_free_list(fake_name
.pl_next
);
1956 zprop_free_list(cb
.cb_proplist
);
1962 * inherit [-rS] <property> <fs|vol> ...
1964 * -r Recurse over all children
1965 * -S Revert to received value, if any
1967 * For each dataset specified on the command line, inherit the given property
1968 * from its parent. Inheriting a property at the pool level will cause it to
1969 * use the default value. The '-r' flag will recurse over all children, and is
1970 * useful for setting a property on a hierarchy-wide basis, regardless of any
1971 * local modifications for each dataset.
1974 typedef struct inherit_cbdata
{
1975 const char *cb_propname
;
1976 boolean_t cb_received
;
1980 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1982 inherit_cbdata_t
*cb
= data
;
1983 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1986 * If we're doing it recursively, then ignore properties that
1987 * are not valid for this type of dataset.
1989 if (prop
!= ZPROP_INVAL
&&
1990 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
), B_FALSE
))
1993 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1997 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1999 inherit_cbdata_t
*cb
= data
;
2001 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
2005 zfs_do_inherit(int argc
, char **argv
)
2009 inherit_cbdata_t cb
= { 0 };
2013 boolean_t received
= B_FALSE
;
2016 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
2019 flags
|= ZFS_ITER_RECURSE
;
2026 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2035 /* check number of arguments */
2037 (void) fprintf(stderr
, gettext("missing property argument\n"));
2041 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
2049 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
2050 if (zfs_prop_readonly(prop
)) {
2051 (void) fprintf(stderr
, gettext(
2052 "%s property is read-only\n"),
2056 if (!zfs_prop_inheritable(prop
) && !received
) {
2057 (void) fprintf(stderr
, gettext("'%s' property cannot "
2058 "be inherited\n"), propname
);
2059 if (prop
== ZFS_PROP_QUOTA
||
2060 prop
== ZFS_PROP_RESERVATION
||
2061 prop
== ZFS_PROP_REFQUOTA
||
2062 prop
== ZFS_PROP_REFRESERVATION
) {
2063 (void) fprintf(stderr
, gettext("use 'zfs set "
2064 "%s=none' to clear\n"), propname
);
2065 (void) fprintf(stderr
, gettext("use 'zfs "
2066 "inherit -S %s' to revert to received "
2067 "value\n"), propname
);
2071 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
2072 prop
== ZFS_PROP_VERSION
)) {
2073 (void) fprintf(stderr
, gettext("'%s' property cannot "
2074 "be reverted to a received value\n"), propname
);
2077 } else if (!zfs_prop_user(propname
)) {
2078 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
2083 cb
.cb_propname
= propname
;
2084 cb
.cb_received
= received
;
2086 if (flags
& ZFS_ITER_RECURSE
) {
2087 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
2088 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
2090 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
2091 NULL
, NULL
, 0, inherit_cb
, &cb
);
2097 typedef struct upgrade_cbdata
{
2098 uint64_t cb_numupgraded
;
2099 uint64_t cb_numsamegraded
;
2100 uint64_t cb_numfailed
;
2101 uint64_t cb_version
;
2103 boolean_t cb_foundone
;
2104 char cb_lastfs
[ZFS_MAX_DATASET_NAME_LEN
];
2108 same_pool(zfs_handle_t
*zhp
, const char *name
)
2110 int len1
= strcspn(name
, "/@");
2111 const char *zhname
= zfs_get_name(zhp
);
2112 int len2
= strcspn(zhname
, "/@");
2116 return (strncmp(name
, zhname
, len1
) == 0);
2120 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
2122 upgrade_cbdata_t
*cb
= data
;
2123 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2125 /* list if it's old/new */
2126 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
2127 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
2130 str
= gettext("The following filesystems are "
2131 "formatted using a newer software version and\n"
2132 "cannot be accessed on the current system.\n\n");
2134 str
= gettext("The following filesystems are "
2135 "out of date, and can be upgraded. After being\n"
2136 "upgraded, these filesystems (and any 'zfs send' "
2137 "streams generated from\n"
2138 "subsequent snapshots) will no longer be "
2139 "accessible by older software versions.\n\n");
2142 if (!cb
->cb_foundone
) {
2144 (void) printf(gettext("VER FILESYSTEM\n"));
2145 (void) printf(gettext("--- ------------\n"));
2146 cb
->cb_foundone
= B_TRUE
;
2149 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
2156 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2158 upgrade_cbdata_t
*cb
= data
;
2159 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2160 int needed_spa_version
;
2163 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2166 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2168 if (needed_spa_version
< 0)
2171 if (spa_version
< needed_spa_version
) {
2173 (void) printf(gettext("%s: can not be "
2174 "upgraded; the pool version needs to first "
2175 "be upgraded\nto version %d\n\n"),
2176 zfs_get_name(zhp
), needed_spa_version
);
2182 if (version
< cb
->cb_version
) {
2184 (void) snprintf(verstr
, sizeof (verstr
),
2185 "%llu", (u_longlong_t
)cb
->cb_version
);
2186 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2188 * If they did "zfs upgrade -a", then we could
2189 * be doing ioctls to different pools. We need
2190 * to log this history once to each pool, and bypass
2191 * the normal history logging that happens in main().
2193 (void) zpool_log_history(g_zfs
, history_str
);
2194 log_history
= B_FALSE
;
2196 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2197 cb
->cb_numupgraded
++;
2200 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2201 } else if (version
> cb
->cb_version
) {
2202 /* can't downgrade */
2203 (void) printf(gettext("%s: can not be downgraded; "
2204 "it is already at version %u\n"),
2205 zfs_get_name(zhp
), version
);
2208 cb
->cb_numsamegraded
++;
2216 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2219 zfs_do_upgrade(int argc
, char **argv
)
2221 boolean_t all
= B_FALSE
;
2222 boolean_t showversions
= B_FALSE
;
2224 upgrade_cbdata_t cb
= { 0 };
2226 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2229 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2232 flags
|= ZFS_ITER_RECURSE
;
2235 showversions
= B_TRUE
;
2238 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2239 optarg
, &cb
.cb_version
) != 0) {
2240 (void) fprintf(stderr
,
2241 gettext("invalid version %s\n"), optarg
);
2250 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2259 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2261 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2262 cb
.cb_version
|| argc
))
2264 if ((all
|| argc
) && (showversions
))
2270 /* Show info on available versions. */
2271 (void) printf(gettext("The following filesystem versions are "
2273 (void) printf(gettext("VER DESCRIPTION\n"));
2274 (void) printf("--- -----------------------------------------"
2275 "---------------\n");
2276 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2277 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2278 (void) printf(gettext(" 3 Case insensitive and filesystem "
2279 "user identifier (FUID)\n"));
2280 (void) printf(gettext(" 4 userquota, groupquota "
2282 (void) printf(gettext(" 5 System attributes\n"));
2283 (void) printf(gettext("\nFor more information on a particular "
2284 "version, including supported releases,\n"));
2285 (void) printf("see the ZFS Administration Guide.\n\n");
2287 } else if (argc
|| all
) {
2288 /* Upgrade filesystems */
2289 if (cb
.cb_version
== 0)
2290 cb
.cb_version
= ZPL_VERSION
;
2291 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2292 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2293 (void) printf(gettext("%llu filesystems upgraded\n"),
2294 (u_longlong_t
)cb
.cb_numupgraded
);
2295 if (cb
.cb_numsamegraded
) {
2296 (void) printf(gettext("%llu filesystems already at "
2298 (u_longlong_t
)cb
.cb_numsamegraded
);
2300 if (cb
.cb_numfailed
!= 0)
2303 /* List old-version filesystems */
2305 (void) printf(gettext("This system is currently running "
2306 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2308 flags
|= ZFS_ITER_RECURSE
;
2309 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2310 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2312 found
= cb
.cb_foundone
;
2313 cb
.cb_foundone
= B_FALSE
;
2314 cb
.cb_newer
= B_TRUE
;
2316 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2317 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2319 if (!cb
.cb_foundone
&& !found
) {
2320 (void) printf(gettext("All filesystems are "
2321 "formatted with the current version.\n"));
2329 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2330 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2331 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2332 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2333 * zfs projectspace [-Hp] [-o field[,...]] [-s field [-s field]...]
2334 * [-S field [-S field]...] filesystem | snapshot
2336 * -H Scripted mode; elide headers and separate columns by tabs.
2337 * -i Translate SID to POSIX ID.
2338 * -n Print numeric ID instead of user/group name.
2339 * -o Control which fields to display.
2340 * -p Use exact (parsable) numeric output.
2341 * -s Specify sort columns, descending order.
2342 * -S Specify sort columns, ascending order.
2343 * -t Control which object types to display.
2345 * Displays space consumed by, and quotas on, each user in the specified
2346 * filesystem or snapshot.
2349 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2350 enum us_field_types
{
2358 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA",
2359 "OBJUSED", "OBJQUOTA" };
2360 static char *us_field_names
[] = { "type", "name", "used", "quota",
2361 "objused", "objquota" };
2362 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2364 #define USTYPE_PSX_GRP (1 << 0)
2365 #define USTYPE_PSX_USR (1 << 1)
2366 #define USTYPE_SMB_GRP (1 << 2)
2367 #define USTYPE_SMB_USR (1 << 3)
2368 #define USTYPE_PROJ (1 << 4)
2369 #define USTYPE_ALL \
2370 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR | \
2373 static int us_type_bits
[] = {
2380 static char *us_type_names
[] = { "posixgroup", "posixuser", "smbgroup",
2383 typedef struct us_node
{
2385 uu_avl_node_t usn_avlnode
;
2386 uu_list_node_t usn_listnode
;
2389 typedef struct us_cbdata
{
2391 uu_avl_pool_t
*cb_avl_pool
;
2393 boolean_t cb_numname
;
2394 boolean_t cb_nicenum
;
2395 boolean_t cb_sid2posix
;
2396 zfs_userquota_prop_t cb_prop
;
2397 zfs_sort_column_t
*cb_sortcol
;
2398 size_t cb_width
[USFIELD_LAST
];
2401 static boolean_t us_populated
= B_FALSE
;
2404 zfs_sort_column_t
*si_sortcol
;
2405 boolean_t si_numname
;
2409 us_field_index(char *field
)
2413 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2414 if (strcmp(field
, us_field_names
[i
]) == 0)
2422 us_compare(const void *larg
, const void *rarg
, void *unused
)
2424 const us_node_t
*l
= larg
;
2425 const us_node_t
*r
= rarg
;
2426 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2427 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2428 boolean_t numname
= si
->si_numname
;
2429 nvlist_t
*lnvl
= l
->usn_nvl
;
2430 nvlist_t
*rnvl
= r
->usn_nvl
;
2434 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2441 zfs_prop_t prop
= sortcol
->sc_prop
;
2442 const char *propname
= NULL
;
2443 boolean_t reverse
= sortcol
->sc_reverse
;
2448 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2449 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2451 rc
= (rv32
< lv32
) ? 1 : -1;
2457 (void) nvlist_lookup_uint64(lnvl
, propname
,
2459 (void) nvlist_lookup_uint64(rnvl
, propname
,
2462 rc
= (rv64
< lv64
) ? 1 : -1;
2464 if ((nvlist_lookup_string(lnvl
, propname
,
2465 &lvstr
) == ENOENT
) ||
2466 (nvlist_lookup_string(rnvl
, propname
,
2467 &rvstr
) == ENOENT
)) {
2470 rc
= strcmp(lvstr
, rvstr
);
2474 case ZFS_PROP_QUOTA
:
2477 if (prop
== ZFS_PROP_USED
)
2481 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2482 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2484 rc
= (rv64
< lv64
) ? 1 : -1;
2493 return (reverse
? 1 : -1);
2495 return (reverse
? -1 : 1);
2500 * If entries still seem to be the same, check if they are of the same
2501 * type (smbentity is added only if we are doing SID to POSIX ID
2502 * translation where we can have duplicate type/name combinations).
2504 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2505 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2507 return (lvb
< rvb
? -1 : 1);
2513 zfs_prop_is_user(unsigned p
)
2515 return (p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
||
2516 p
== ZFS_PROP_USEROBJUSED
|| p
== ZFS_PROP_USEROBJQUOTA
);
2520 zfs_prop_is_group(unsigned p
)
2522 return (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
||
2523 p
== ZFS_PROP_GROUPOBJUSED
|| p
== ZFS_PROP_GROUPOBJQUOTA
);
2527 zfs_prop_is_project(unsigned p
)
2529 return (p
== ZFS_PROP_PROJECTUSED
|| p
== ZFS_PROP_PROJECTQUOTA
||
2530 p
== ZFS_PROP_PROJECTOBJUSED
|| p
== ZFS_PROP_PROJECTOBJQUOTA
);
2533 static inline const char *
2534 us_type2str(unsigned field_type
)
2536 switch (field_type
) {
2537 case USTYPE_PSX_USR
:
2538 return ("POSIX User");
2539 case USTYPE_PSX_GRP
:
2540 return ("POSIX Group");
2541 case USTYPE_SMB_USR
:
2542 return ("SMB User");
2543 case USTYPE_SMB_GRP
:
2544 return ("SMB Group");
2548 return ("Undefined");
2553 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2555 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2556 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2561 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2562 uu_avl_t
*avl
= cb
->cb_avl
;
2566 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2568 const char *typestr
;
2572 int typeidx
, nameidx
, sizeidx
;
2573 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2574 boolean_t smbentity
= B_FALSE
;
2576 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2578 node
= safe_malloc(sizeof (us_node_t
));
2579 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2580 node
->usn_nvl
= props
;
2582 if (domain
!= NULL
&& domain
[0] != '\0') {
2585 char sid
[MAXNAMELEN
+ 32];
2589 directory_error_t e
;
2593 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2595 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2596 type
= USTYPE_SMB_GRP
;
2597 err
= sid_to_id(sid
, B_FALSE
, &id
);
2599 type
= USTYPE_SMB_USR
;
2600 err
= sid_to_id(sid
, B_TRUE
, &id
);
2605 if (!cb
->cb_sid2posix
) {
2606 e
= directory_name_from_sid(NULL
, sid
, &name
,
2609 directory_error_free(e
);
2619 #endif /* HAVE_IDMAP */
2622 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2624 if (zfs_prop_is_group(prop
)) {
2625 type
= USTYPE_PSX_GRP
;
2626 if (!cb
->cb_numname
) {
2629 if ((g
= getgrgid(rid
)) != NULL
)
2632 } else if (zfs_prop_is_user(prop
)) {
2633 type
= USTYPE_PSX_USR
;
2634 if (!cb
->cb_numname
) {
2637 if ((p
= getpwuid(rid
)) != NULL
)
2646 * Make sure that the type/name combination is unique when doing
2647 * SID to POSIX ID translation (hence changing the type from SMB to
2650 if (cb
->cb_sid2posix
&&
2651 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2654 /* Calculate/update width of TYPE field */
2655 typestr
= us_type2str(type
);
2656 typelen
= strlen(gettext(typestr
));
2657 typeidx
= us_field_index("type");
2658 if (typelen
> cb
->cb_width
[typeidx
])
2659 cb
->cb_width
[typeidx
] = typelen
;
2660 if (nvlist_add_uint32(props
, "type", type
) != 0)
2663 /* Calculate/update width of NAME field */
2664 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2665 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2667 namelen
= snprintf(NULL
, 0, "%u", rid
);
2669 if (nvlist_add_string(props
, "name", name
) != 0)
2671 namelen
= strlen(name
);
2673 nameidx
= us_field_index("name");
2674 if (nameidx
>= 0 && namelen
> cb
->cb_width
[nameidx
])
2675 cb
->cb_width
[nameidx
] = namelen
;
2678 * Check if this type/name combination is in the list and update it;
2679 * otherwise add new node to the list.
2681 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2682 uu_avl_insert(avl
, node
, idx
);
2687 props
= node
->usn_nvl
;
2690 /* Calculate/update width of USED/QUOTA fields */
2691 if (cb
->cb_nicenum
) {
2692 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
||
2693 prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
||
2694 prop
== ZFS_PROP_PROJECTUSED
||
2695 prop
== ZFS_PROP_PROJECTQUOTA
) {
2696 zfs_nicebytes(space
, sizebuf
, sizeof (sizebuf
));
2698 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2701 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu",
2702 (u_longlong_t
)space
);
2704 sizelen
= strlen(sizebuf
);
2705 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
||
2706 prop
== ZFS_PROP_PROJECTUSED
) {
2708 if (!nvlist_exists(props
, "quota"))
2709 (void) nvlist_add_uint64(props
, "quota", 0);
2710 } else if (prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
||
2711 prop
== ZFS_PROP_PROJECTQUOTA
) {
2713 if (!nvlist_exists(props
, "used"))
2714 (void) nvlist_add_uint64(props
, "used", 0);
2715 } else if (prop
== ZFS_PROP_USEROBJUSED
||
2716 prop
== ZFS_PROP_GROUPOBJUSED
|| prop
== ZFS_PROP_PROJECTOBJUSED
) {
2717 propname
= "objused";
2718 if (!nvlist_exists(props
, "objquota"))
2719 (void) nvlist_add_uint64(props
, "objquota", 0);
2720 } else if (prop
== ZFS_PROP_USEROBJQUOTA
||
2721 prop
== ZFS_PROP_GROUPOBJQUOTA
||
2722 prop
== ZFS_PROP_PROJECTOBJQUOTA
) {
2723 propname
= "objquota";
2724 if (!nvlist_exists(props
, "objused"))
2725 (void) nvlist_add_uint64(props
, "objused", 0);
2729 sizeidx
= us_field_index(propname
);
2730 if (sizeidx
>= 0 && sizelen
> cb
->cb_width
[sizeidx
])
2731 cb
->cb_width
[sizeidx
] = sizelen
;
2733 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2740 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2741 size_t *width
, us_node_t
*node
)
2743 nvlist_t
*nvl
= node
->usn_nvl
;
2744 char valstr
[MAXNAMELEN
];
2745 boolean_t first
= B_TRUE
;
2751 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2752 if (!(ustype
& types
))
2755 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2756 nvpair_t
*nvp
= NULL
;
2762 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2763 if (strcmp(nvpair_name(nvp
),
2764 us_field_names
[field
]) == 0)
2768 type
= nvp
== NULL
? DATA_TYPE_UNKNOWN
: nvpair_type(nvp
);
2770 case DATA_TYPE_UINT32
:
2771 (void) nvpair_value_uint32(nvp
, &val32
);
2773 case DATA_TYPE_UINT64
:
2774 (void) nvpair_value_uint64(nvp
, &val64
);
2776 case DATA_TYPE_STRING
:
2777 (void) nvpair_value_string(nvp
, &strval
);
2779 case DATA_TYPE_UNKNOWN
:
2782 (void) fprintf(stderr
, "invalid data type\n");
2787 if (type
== DATA_TYPE_UINT32
)
2788 strval
= (char *)us_type2str(val32
);
2791 if (type
== DATA_TYPE_UINT64
) {
2792 (void) sprintf(valstr
, "%llu",
2793 (u_longlong_t
)val64
);
2799 if (type
== DATA_TYPE_UINT64
) {
2801 (void) sprintf(valstr
, "%llu",
2802 (u_longlong_t
)val64
);
2804 } else if (field
== USFIELD_QUOTA
&&
2808 zfs_nicebytes(val64
, valstr
,
2814 case USFIELD_OBJUSED
:
2815 case USFIELD_OBJQUOTA
:
2816 if (type
== DATA_TYPE_UINT64
) {
2818 (void) sprintf(valstr
, "%llu",
2819 (u_longlong_t
)val64
);
2821 } else if (field
== USFIELD_OBJQUOTA
&&
2825 zfs_nicenum(val64
, valstr
,
2835 (void) printf("\t");
2840 (void) printf("%s", strval
);
2841 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2842 (void) printf("%-*s", (int)width
[field
], strval
);
2844 (void) printf("%*s", (int)width
[field
], strval
);
2850 (void) printf("\n");
2854 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2855 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2863 boolean_t first
= B_TRUE
;
2865 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2866 col
= gettext(us_field_hdr
[field
]);
2867 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2868 (void) printf(first
? "%-*s" : " %-*s",
2869 (int)width
[field
], col
);
2871 (void) printf(first
? "%*s" : " %*s",
2872 (int)width
[field
], col
);
2877 (void) printf("\n");
2880 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2881 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2883 nvlist_free(node
->usn_nvl
);
2888 zfs_do_userspace(int argc
, char **argv
)
2891 zfs_userquota_prop_t p
;
2892 uu_avl_pool_t
*avl_pool
;
2894 uu_avl_walk_t
*walk
;
2896 char deffields
[] = "type,name,used,quota,objused,objquota";
2897 char *ofield
= NULL
;
2898 char *tfield
= NULL
;
2902 boolean_t scripted
= B_FALSE
;
2903 boolean_t prtnum
= B_FALSE
;
2904 boolean_t parsable
= B_FALSE
;
2905 boolean_t sid2posix
= B_FALSE
;
2908 zfs_sort_column_t
*sortcol
= NULL
;
2909 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2913 uu_list_pool_t
*listpool
;
2915 uu_avl_index_t idx
= 0;
2916 uu_list_index_t idx2
= 0;
2921 if (strcmp(argv
[0], "groupspace") == 0) {
2922 /* Toggle default group types */
2923 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2924 } else if (strcmp(argv
[0], "projectspace") == 0) {
2925 types
= USTYPE_PROJ
;
2929 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2932 if (types
== USTYPE_PROJ
) {
2933 (void) fprintf(stderr
,
2934 gettext("invalid option 'n'\n"));
2950 if (zfs_add_sort_column(&sortcol
, optarg
,
2951 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
2952 (void) fprintf(stderr
,
2953 gettext("invalid field '%s'\n"), optarg
);
2958 if (types
== USTYPE_PROJ
) {
2959 (void) fprintf(stderr
,
2960 gettext("invalid option 't'\n"));
2966 if (types
== USTYPE_PROJ
) {
2967 (void) fprintf(stderr
,
2968 gettext("invalid option 'i'\n"));
2974 (void) fprintf(stderr
, gettext("missing argument for "
2975 "'%c' option\n"), optopt
);
2979 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2989 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2993 (void) fprintf(stderr
, gettext("too many arguments\n"));
2997 /* Use default output fields if not specified using -o */
3001 if ((delim
= strchr(ofield
, ',')) != NULL
)
3003 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
3004 (void) fprintf(stderr
, gettext("invalid type '%s' "
3005 "for -o option\n"), ofield
);
3010 } while (delim
!= NULL
);
3011 fields
[cfield
] = USFIELD_LAST
;
3013 /* Override output types (-t option) */
3014 if (tfield
!= NULL
) {
3018 boolean_t found
= B_FALSE
;
3020 if ((delim
= strchr(tfield
, ',')) != NULL
)
3022 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
3024 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
3026 types
|= us_type_bits
[i
];
3031 (void) fprintf(stderr
, gettext("invalid type "
3032 "'%s' for -t option\n"), tfield
);
3037 } while (delim
!= NULL
);
3040 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
3043 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
3044 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
3046 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
3049 /* Always add default sorting columns */
3050 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
3051 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
3053 cb
.cb_sortcol
= sortcol
;
3054 cb
.cb_numname
= prtnum
;
3055 cb
.cb_nicenum
= !parsable
;
3056 cb
.cb_avl_pool
= avl_pool
;
3057 cb
.cb_avl
= avl_tree
;
3058 cb
.cb_sid2posix
= sid2posix
;
3060 for (i
= 0; i
< USFIELD_LAST
; i
++)
3061 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
3063 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
3064 if ((zfs_prop_is_user(p
) &&
3065 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
3066 (zfs_prop_is_group(p
) &&
3067 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))) ||
3068 (zfs_prop_is_project(p
) && types
!= USTYPE_PROJ
))
3072 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
3077 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
3080 us_populated
= B_TRUE
;
3082 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
3083 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
3084 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
3085 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
3087 while (node
!= NULL
) {
3089 node
= uu_avl_next(avl_tree
, node
);
3090 uu_avl_remove(avl_tree
, rmnode
);
3091 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
3092 uu_list_insert(list
, rmnode
, idx2
);
3095 for (node
= uu_list_first(list
); node
!= NULL
;
3096 node
= uu_list_next(list
, node
)) {
3097 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
3099 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
3100 uu_avl_insert(avl_tree
, node
, idx
);
3103 uu_list_destroy(list
);
3104 uu_list_pool_destroy(listpool
);
3106 /* Print and free node nvlist memory */
3107 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
3110 zfs_free_sort_columns(sortcol
);
3112 /* Clean up the AVL tree */
3113 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
3116 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
3117 uu_avl_remove(cb
.cb_avl
, node
);
3121 uu_avl_walk_end(walk
);
3122 uu_avl_destroy(avl_tree
);
3123 uu_avl_pool_destroy(avl_pool
);
3129 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
3130 * [-t type[,...]] [filesystem|volume|snapshot] ...
3132 * -H Scripted mode; elide headers and separate columns by tabs
3133 * -p Display values in parsable (literal) format.
3134 * -r Recurse over all children
3135 * -d Limit recursion by depth.
3136 * -o Control which fields to display.
3137 * -s Specify sort columns, descending order.
3138 * -S Specify sort columns, ascending order.
3139 * -t Control which object types to display.
3141 * When given no arguments, list all filesystems in the system.
3142 * Otherwise, list the specified datasets, optionally recursing down them if
3143 * '-r' is specified.
3145 typedef struct list_cbdata
{
3147 boolean_t cb_literal
;
3148 boolean_t cb_scripted
;
3149 zprop_list_t
*cb_proplist
;
3153 * Given a list of columns to display, output appropriate headers for each one.
3156 print_header(list_cbdata_t
*cb
)
3158 zprop_list_t
*pl
= cb
->cb_proplist
;
3159 char headerbuf
[ZFS_MAXPROPLEN
];
3162 boolean_t first
= B_TRUE
;
3163 boolean_t right_justify
;
3165 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3172 right_justify
= B_FALSE
;
3173 if (pl
->pl_prop
!= ZPROP_INVAL
) {
3174 header
= zfs_prop_column_name(pl
->pl_prop
);
3175 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3177 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
3178 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
3179 headerbuf
[i
] = '\0';
3183 if (pl
->pl_next
== NULL
&& !right_justify
)
3184 (void) printf("%s", header
);
3185 else if (right_justify
)
3186 (void) printf("%*s", (int)pl
->pl_width
, header
);
3188 (void) printf("%-*s", (int)pl
->pl_width
, header
);
3191 (void) printf("\n");
3195 * Given a dataset and a list of fields, print out all the properties according
3196 * to the described layout.
3199 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
3201 zprop_list_t
*pl
= cb
->cb_proplist
;
3202 boolean_t first
= B_TRUE
;
3203 char property
[ZFS_MAXPROPLEN
];
3204 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
3207 boolean_t right_justify
;
3209 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3211 if (cb
->cb_scripted
)
3212 (void) printf("\t");
3219 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
3220 (void) strlcpy(property
, zfs_get_name(zhp
),
3223 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3224 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
3225 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
3226 sizeof (property
), NULL
, NULL
, 0,
3227 cb
->cb_literal
) != 0)
3231 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3232 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
3233 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
3234 property
, sizeof (property
), cb
->cb_literal
) != 0)
3238 right_justify
= B_TRUE
;
3239 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
3240 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
3241 property
, sizeof (property
), cb
->cb_literal
) != 0)
3245 right_justify
= B_TRUE
;
3247 if (nvlist_lookup_nvlist(userprops
,
3248 pl
->pl_user_prop
, &propval
) != 0)
3251 verify(nvlist_lookup_string(propval
,
3252 ZPROP_VALUE
, &propstr
) == 0);
3253 right_justify
= B_FALSE
;
3257 * If this is being called in scripted mode, or if this is the
3258 * last column and it is left-justified, don't include a width
3261 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3262 (void) printf("%s", propstr
);
3263 else if (right_justify
)
3264 (void) printf("%*s", (int)pl
->pl_width
, propstr
);
3266 (void) printf("%-*s", (int)pl
->pl_width
, propstr
);
3269 (void) printf("\n");
3273 * Generic callback function to list a dataset or snapshot.
3276 list_callback(zfs_handle_t
*zhp
, void *data
)
3278 list_cbdata_t
*cbp
= data
;
3280 if (cbp
->cb_first
) {
3281 if (!cbp
->cb_scripted
)
3283 cbp
->cb_first
= B_FALSE
;
3286 print_dataset(zhp
, cbp
);
3292 zfs_do_list(int argc
, char **argv
)
3295 static char default_fields
[] =
3296 "name,used,available,referenced,mountpoint";
3297 int types
= ZFS_TYPE_DATASET
;
3298 boolean_t types_specified
= B_FALSE
;
3299 char *fields
= NULL
;
3300 list_cbdata_t cb
= { 0 };
3304 zfs_sort_column_t
*sortcol
= NULL
;
3305 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3308 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3314 cb
.cb_literal
= B_TRUE
;
3315 flags
|= ZFS_ITER_LITERAL_PROPS
;
3318 limit
= parse_depth(optarg
, &flags
);
3321 flags
|= ZFS_ITER_RECURSE
;
3324 cb
.cb_scripted
= B_TRUE
;
3327 if (zfs_add_sort_column(&sortcol
, optarg
,
3329 (void) fprintf(stderr
,
3330 gettext("invalid property '%s'\n"), optarg
);
3335 if (zfs_add_sort_column(&sortcol
, optarg
,
3337 (void) fprintf(stderr
,
3338 gettext("invalid property '%s'\n"), optarg
);
3344 types_specified
= B_TRUE
;
3345 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3346 while (*optarg
!= '\0') {
3347 static char *type_subopts
[] = { "filesystem",
3348 "volume", "snapshot", "snap", "bookmark",
3351 switch (getsubopt(&optarg
, type_subopts
,
3354 types
|= ZFS_TYPE_FILESYSTEM
;
3357 types
|= ZFS_TYPE_VOLUME
;
3361 types
|= ZFS_TYPE_SNAPSHOT
;
3364 types
|= ZFS_TYPE_BOOKMARK
;
3367 types
= ZFS_TYPE_DATASET
|
3371 (void) fprintf(stderr
,
3372 gettext("invalid type '%s'\n"),
3379 (void) fprintf(stderr
, gettext("missing argument for "
3380 "'%c' option\n"), optopt
);
3384 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3394 fields
= default_fields
;
3397 * If we are only going to list snapshot names and sort by name,
3398 * then we can use faster version.
3400 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3401 flags
|= ZFS_ITER_SIMPLE
;
3404 * If "-o space" and no types were specified, don't display snapshots.
3406 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3407 types
&= ~ZFS_TYPE_SNAPSHOT
;
3410 * If the user specifies '-o all', the zprop_get_list() doesn't
3411 * normally include the name of the dataset. For 'zfs list', we always
3412 * want this property to be first.
3414 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3418 cb
.cb_first
= B_TRUE
;
3420 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3421 limit
, list_callback
, &cb
);
3423 zprop_free_list(cb
.cb_proplist
);
3424 zfs_free_sort_columns(sortcol
);
3426 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3427 (void) fprintf(stderr
, gettext("no datasets available\n"));
3433 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3434 * zfs rename [-f] -p <fs | vol> <fs | vol>
3435 * zfs rename -r <snap> <snap>
3437 * Renames the given dataset to another of the same type.
3439 * The '-p' flag creates all the non-existing ancestors of the target first.
3443 zfs_do_rename(int argc
, char **argv
)
3448 boolean_t recurse
= B_FALSE
;
3449 boolean_t parents
= B_FALSE
;
3450 boolean_t force_unmount
= B_FALSE
;
3453 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3462 force_unmount
= B_TRUE
;
3466 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3475 /* check number of arguments */
3477 (void) fprintf(stderr
, gettext("missing source dataset "
3482 (void) fprintf(stderr
, gettext("missing target dataset "
3487 (void) fprintf(stderr
, gettext("too many arguments\n"));
3491 if (recurse
&& parents
) {
3492 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3497 if (recurse
&& strchr(argv
[0], '@') == 0) {
3498 (void) fprintf(stderr
, gettext("source dataset for recursive "
3499 "rename must be a snapshot\n"));
3503 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3504 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3507 /* If we were asked and the name looks good, try to create ancestors. */
3508 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3509 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3514 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3523 * Promotes the given clone fs to be the parent
3527 zfs_do_promote(int argc
, char **argv
)
3533 if (argc
> 1 && argv
[1][0] == '-') {
3534 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3539 /* check number of arguments */
3541 (void) fprintf(stderr
, gettext("missing clone filesystem"
3546 (void) fprintf(stderr
, gettext("too many arguments\n"));
3550 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3554 ret
= (zfs_promote(zhp
) != 0);
3562 * zfs rollback [-rRf] <snapshot>
3564 * -r Delete any intervening snapshots before doing rollback
3565 * -R Delete any snapshots and their clones
3566 * -f ignored for backwards compatibility
3568 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3569 * since then and making it the active dataset. If more recent snapshots exist,
3570 * the command will complain unless the '-r' flag is given.
3572 typedef struct rollback_cbdata
{
3578 boolean_t cb_recurse
;
3579 } rollback_cbdata_t
;
3582 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3584 rollback_cbdata_t
*cbp
= data
;
3586 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3587 (void) fprintf(stderr
, gettext("cannot rollback to "
3588 "'%s': clones of previous snapshots exist\n"),
3590 (void) fprintf(stderr
, gettext("use '-R' to "
3591 "force deletion of the following clones and "
3597 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3605 * Report any snapshots more recent than the one specified. Used when '-r' is
3606 * not specified. We reuse this same callback for the snapshot dependents - if
3607 * 'cb_dependent' is set, then this is a dependent and we should report it
3608 * without checking the transaction group.
3611 rollback_check(zfs_handle_t
*zhp
, void *data
)
3613 rollback_cbdata_t
*cbp
= data
;
3615 if (cbp
->cb_doclones
) {
3620 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3621 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3622 (void) fprintf(stderr
, gettext("cannot "
3623 "rollback to '%s': more recent snapshots "
3624 "or bookmarks exist\n"),
3626 (void) fprintf(stderr
, gettext("use '-r' to "
3627 "force deletion of the following "
3628 "snapshots and bookmarks:\n"));
3633 if (cbp
->cb_recurse
) {
3634 if (zfs_iter_dependents(zhp
, B_TRUE
,
3635 rollback_check_dependent
, cbp
) != 0) {
3640 (void) fprintf(stderr
, "%s\n",
3649 zfs_do_rollback(int argc
, char **argv
)
3653 boolean_t force
= B_FALSE
;
3654 rollback_cbdata_t cb
= { 0 };
3655 zfs_handle_t
*zhp
, *snap
;
3656 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3660 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3673 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3682 /* check number of arguments */
3684 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3688 (void) fprintf(stderr
, gettext("too many arguments\n"));
3692 /* open the snapshot */
3693 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3696 /* open the parent dataset */
3697 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3698 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3700 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3706 * Check for more recent snapshots and/or clones based on the presence
3709 cb
.cb_target
= argv
[0];
3710 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3711 cb
.cb_first
= B_TRUE
;
3713 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, rollback_check
, &cb
)) != 0)
3715 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
3718 if ((ret
= cb
.cb_error
) != 0)
3722 * Rollback parent to the given snapshot.
3724 ret
= zfs_rollback(zhp
, snap
, force
);
3737 * zfs set property=value ... { fs | snap | vol } ...
3739 * Sets the given properties for all datasets specified on the command line.
3743 set_callback(zfs_handle_t
*zhp
, void *data
)
3745 nvlist_t
*props
= data
;
3747 if (zfs_prop_set_list(zhp
, props
) != 0) {
3748 switch (libzfs_errno(g_zfs
)) {
3749 case EZFS_MOUNTFAILED
:
3750 (void) fprintf(stderr
, gettext("property may be set "
3751 "but unable to remount filesystem\n"));
3753 case EZFS_SHARENFSFAILED
:
3754 (void) fprintf(stderr
, gettext("property may be set "
3755 "but unable to reshare filesystem\n"));
3764 zfs_do_set(int argc
, char **argv
)
3766 nvlist_t
*props
= NULL
;
3767 int ds_start
= -1; /* argv idx of first dataset arg */
3771 /* check for options */
3772 if (argc
> 1 && argv
[1][0] == '-') {
3773 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3778 /* check number of arguments */
3780 (void) fprintf(stderr
, gettext("missing arguments\n"));
3784 if (strchr(argv
[1], '=') == NULL
) {
3785 (void) fprintf(stderr
, gettext("missing property=value "
3788 (void) fprintf(stderr
, gettext("missing dataset "
3794 /* validate argument order: prop=val args followed by dataset args */
3795 for (i
= 1; i
< argc
; i
++) {
3796 if (strchr(argv
[i
], '=') != NULL
) {
3798 /* out-of-order prop=val argument */
3799 (void) fprintf(stderr
, gettext("invalid "
3800 "argument order\n"));
3803 } else if (ds_start
< 0) {
3808 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
3812 /* Populate a list of property settings */
3813 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3815 for (i
= 1; i
< ds_start
; i
++) {
3816 if (!parseprop(props
, argv
[i
])) {
3822 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
3823 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, props
);
3830 typedef struct snap_cbdata
{
3832 boolean_t sd_recursive
;
3833 const char *sd_snapname
;
3837 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3839 snap_cbdata_t
*sd
= arg
;
3844 if (sd
->sd_recursive
&&
3845 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
3850 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3853 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3856 if (sd
->sd_recursive
)
3857 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3863 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3865 * Creates a snapshot with the given name. While functionally equivalent to
3866 * 'zfs create', it is a separate command to differentiate intent.
3869 zfs_do_snapshot(int argc
, char **argv
)
3874 snap_cbdata_t sd
= { 0 };
3875 boolean_t multiple_snaps
= B_FALSE
;
3877 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3879 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3883 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3886 if (!parseprop(props
, optarg
)) {
3887 nvlist_free(sd
.sd_nvl
);
3893 sd
.sd_recursive
= B_TRUE
;
3894 multiple_snaps
= B_TRUE
;
3897 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3906 /* check number of arguments */
3908 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3913 multiple_snaps
= B_TRUE
;
3914 for (; argc
> 0; argc
--, argv
++) {
3918 atp
= strchr(argv
[0], '@');
3922 sd
.sd_snapname
= atp
+ 1;
3923 zhp
= zfs_open(g_zfs
, argv
[0],
3924 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3927 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3931 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3932 nvlist_free(sd
.sd_nvl
);
3934 if (ret
!= 0 && multiple_snaps
)
3935 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3939 nvlist_free(sd
.sd_nvl
);
3946 * Send a backup stream to stdout.
3949 zfs_do_send(int argc
, char **argv
)
3951 char *fromname
= NULL
;
3952 char *toname
= NULL
;
3953 char *resume_token
= NULL
;
3956 sendflags_t flags
= { 0 };
3958 nvlist_t
*dbgnv
= NULL
;
3959 boolean_t extraverbose
= B_FALSE
;
3961 struct option long_options
[] = {
3962 {"replicate", no_argument
, NULL
, 'R'},
3963 {"props", no_argument
, NULL
, 'p'},
3964 {"parsable", no_argument
, NULL
, 'P'},
3965 {"dedup", no_argument
, NULL
, 'D'},
3966 {"verbose", no_argument
, NULL
, 'v'},
3967 {"dryrun", no_argument
, NULL
, 'n'},
3968 {"large-block", no_argument
, NULL
, 'L'},
3969 {"embed", no_argument
, NULL
, 'e'},
3970 {"resume", required_argument
, NULL
, 't'},
3971 {"compressed", no_argument
, NULL
, 'c'},
3972 {"raw", no_argument
, NULL
, 'w'},
3973 {"backup", no_argument
, NULL
, 'b'},
3978 while ((c
= getopt_long(argc
, argv
, ":i:I:RDpvnPLet:cwb", long_options
,
3990 flags
.doall
= B_TRUE
;
3993 flags
.replicate
= B_TRUE
;
3996 flags
.props
= B_TRUE
;
3999 flags
.backup
= B_TRUE
;
4002 flags
.parsable
= B_TRUE
;
4003 flags
.verbose
= B_TRUE
;
4007 extraverbose
= B_TRUE
;
4008 flags
.verbose
= B_TRUE
;
4009 flags
.progress
= B_TRUE
;
4012 flags
.dedup
= B_TRUE
;
4015 flags
.dryrun
= B_TRUE
;
4018 flags
.largeblock
= B_TRUE
;
4021 flags
.embed_data
= B_TRUE
;
4024 resume_token
= optarg
;
4027 flags
.compress
= B_TRUE
;
4031 flags
.compress
= B_TRUE
;
4032 flags
.embed_data
= B_TRUE
;
4033 flags
.largeblock
= B_TRUE
;
4037 * If a parameter was not passed, optopt contains the
4038 * value that would normally lead us into the
4039 * appropriate case statement. If it's > 256, then this
4040 * must be a longopt and we should look at argv to get
4041 * the string. Otherwise it's just the character, so we
4042 * should use it directly.
4044 if (optopt
<= UINT8_MAX
) {
4045 (void) fprintf(stderr
,
4046 gettext("missing argument for '%c' "
4047 "option\n"), optopt
);
4049 (void) fprintf(stderr
,
4050 gettext("missing argument for '%s' "
4051 "option\n"), argv
[optind
- 1]);
4059 * If an invalid flag was passed, optopt contains the
4060 * character if it was a short flag, or 0 if it was a
4064 (void) fprintf(stderr
,
4065 gettext("invalid option '%c'\n"), optopt
);
4067 (void) fprintf(stderr
,
4068 gettext("invalid option '%s'\n"),
4079 if (resume_token
!= NULL
) {
4080 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
4081 flags
.backup
|| flags
.dedup
) {
4082 (void) fprintf(stderr
,
4083 gettext("invalid flags combined with -t\n"));
4087 (void) fprintf(stderr
, gettext("no additional "
4088 "arguments are permitted with -t\n"));
4093 (void) fprintf(stderr
,
4094 gettext("missing snapshot argument\n"));
4098 (void) fprintf(stderr
, gettext("too many arguments\n"));
4103 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
4104 (void) fprintf(stderr
,
4105 gettext("Error: Stream can not be written to a terminal.\n"
4106 "You must redirect standard output.\n"));
4110 if (resume_token
!= NULL
) {
4111 return (zfs_send_resume(g_zfs
, &flags
, STDOUT_FILENO
,
4116 * Special case sending a filesystem, or from a bookmark.
4118 if (strchr(argv
[0], '@') == NULL
||
4119 (fromname
&& strchr(fromname
, '#') != NULL
)) {
4120 char frombuf
[ZFS_MAX_DATASET_NAME_LEN
];
4122 if (flags
.replicate
|| flags
.doall
|| flags
.props
||
4123 flags
.backup
|| flags
.dedup
||
4124 (strchr(argv
[0], '@') == NULL
&&
4125 (flags
.dryrun
|| flags
.verbose
|| flags
.progress
))) {
4126 (void) fprintf(stderr
, gettext("Error: "
4127 "Unsupported flag with filesystem or bookmark.\n"));
4131 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
4135 if (fromname
!= NULL
&&
4136 (fromname
[0] == '#' || fromname
[0] == '@')) {
4138 * Incremental source name begins with # or @.
4139 * Default to same fs as target.
4141 (void) strlcpy(frombuf
, argv
[0], sizeof (frombuf
));
4142 cp
= strchr(frombuf
, '@');
4145 (void) strlcat(frombuf
, fromname
, sizeof (frombuf
));
4148 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, flags
);
4153 cp
= strchr(argv
[0], '@');
4156 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4161 * If they specified the full path to the snapshot, chop off
4162 * everything except the short name of the snapshot, but special
4163 * case if they specify the origin.
4165 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
4166 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
4169 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
4170 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
4172 if (strcmp(origin
, fromname
) == 0) {
4174 flags
.fromorigin
= B_TRUE
;
4177 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
4178 (void) fprintf(stderr
,
4179 gettext("incremental source must be "
4180 "in same filesystem\n"));
4184 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
4185 (void) fprintf(stderr
,
4186 gettext("invalid incremental source\n"));
4192 if (flags
.replicate
&& fromname
== NULL
)
4193 flags
.doall
= B_TRUE
;
4195 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
4196 extraverbose
? &dbgnv
: NULL
);
4198 if (extraverbose
&& dbgnv
!= NULL
) {
4200 * dump_nvlist prints to stdout, but that's been
4201 * redirected to a file. Make it print to stderr
4204 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
4205 dump_nvlist(dbgnv
, 0);
4214 * Restore a backup stream from stdin.
4217 zfs_do_receive(int argc
, char **argv
)
4220 recvflags_t flags
= { 0 };
4221 boolean_t abort_resumable
= B_FALSE
;
4224 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
4228 while ((c
= getopt(argc
, argv
, ":o:x:denuvFsA")) != -1) {
4231 if (!parseprop(props
, optarg
)) {
4237 if (!parsepropname(props
, optarg
)) {
4244 (void) fprintf(stderr
, gettext("invalid option "
4245 "combination: -d and -e are mutually "
4249 flags
.isprefix
= B_TRUE
;
4252 if (flags
.isprefix
) {
4253 (void) fprintf(stderr
, gettext("invalid option "
4254 "combination: -d and -e are mutually "
4258 flags
.istail
= B_TRUE
;
4261 flags
.dryrun
= B_TRUE
;
4264 flags
.nomount
= B_TRUE
;
4267 flags
.verbose
= B_TRUE
;
4270 flags
.resumable
= B_TRUE
;
4273 flags
.force
= B_TRUE
;
4276 abort_resumable
= B_TRUE
;
4279 (void) fprintf(stderr
, gettext("missing argument for "
4280 "'%c' option\n"), optopt
);
4284 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4293 /* zfs recv -e (use "tail" name) implies -d (remove dataset "head") */
4295 flags
.isprefix
= B_TRUE
;
4297 /* check number of arguments */
4299 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4303 (void) fprintf(stderr
, gettext("too many arguments\n"));
4307 if (abort_resumable
) {
4308 if (flags
.isprefix
|| flags
.istail
|| flags
.dryrun
||
4309 flags
.resumable
|| flags
.nomount
) {
4310 (void) fprintf(stderr
, gettext("invalid option\n"));
4314 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
4315 (void) snprintf(namebuf
, sizeof (namebuf
),
4316 "%s/%%recv", argv
[0]);
4318 if (zfs_dataset_exists(g_zfs
, namebuf
,
4319 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) {
4320 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4321 namebuf
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4326 err
= zfs_destroy(zhp
, B_FALSE
);
4329 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4330 argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4333 if (!zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) ||
4334 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
4335 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == -1) {
4336 (void) fprintf(stderr
,
4337 gettext("'%s' does not have any "
4338 "resumable receive state to abort\n"),
4344 err
= zfs_destroy(zhp
, B_FALSE
);
4351 if (isatty(STDIN_FILENO
)) {
4352 (void) fprintf(stderr
,
4353 gettext("Error: Backup stream can not be read "
4354 "from a terminal.\n"
4355 "You must redirect standard input.\n"));
4359 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
4366 * allow/unallow stuff
4368 /* copied from zfs/sys/dsl_deleg.h */
4369 #define ZFS_DELEG_PERM_CREATE "create"
4370 #define ZFS_DELEG_PERM_DESTROY "destroy"
4371 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4372 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4373 #define ZFS_DELEG_PERM_CLONE "clone"
4374 #define ZFS_DELEG_PERM_PROMOTE "promote"
4375 #define ZFS_DELEG_PERM_RENAME "rename"
4376 #define ZFS_DELEG_PERM_MOUNT "mount"
4377 #define ZFS_DELEG_PERM_SHARE "share"
4378 #define ZFS_DELEG_PERM_SEND "send"
4379 #define ZFS_DELEG_PERM_RECEIVE "receive"
4380 #define ZFS_DELEG_PERM_ALLOW "allow"
4381 #define ZFS_DELEG_PERM_USERPROP "userprop"
4382 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4383 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4384 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4385 #define ZFS_DELEG_PERM_USERUSED "userused"
4386 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4387 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
4388 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
4389 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
4390 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
4392 #define ZFS_DELEG_PERM_HOLD "hold"
4393 #define ZFS_DELEG_PERM_RELEASE "release"
4394 #define ZFS_DELEG_PERM_DIFF "diff"
4395 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4396 #define ZFS_DELEG_PERM_REMAP "remap"
4397 #define ZFS_DELEG_PERM_LOAD_KEY "load-key"
4398 #define ZFS_DELEG_PERM_CHANGE_KEY "change-key"
4400 #define ZFS_DELEG_PERM_PROJECTUSED "projectused"
4401 #define ZFS_DELEG_PERM_PROJECTQUOTA "projectquota"
4402 #define ZFS_DELEG_PERM_PROJECTOBJUSED "projectobjused"
4403 #define ZFS_DELEG_PERM_PROJECTOBJQUOTA "projectobjquota"
4405 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4407 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
4408 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
4409 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
4410 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
4411 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
4412 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
4413 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
4414 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
4415 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
4416 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
4417 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
4418 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
4419 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
4420 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
4421 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
4422 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
4423 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
4424 { ZFS_DELEG_PERM_REMAP
, ZFS_DELEG_NOTE_REMAP
},
4425 { ZFS_DELEG_PERM_LOAD_KEY
, ZFS_DELEG_NOTE_LOAD_KEY
},
4426 { ZFS_DELEG_PERM_CHANGE_KEY
, ZFS_DELEG_NOTE_CHANGE_KEY
},
4428 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
4429 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
4430 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
4431 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
4432 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
4433 { ZFS_DELEG_PERM_USEROBJQUOTA
, ZFS_DELEG_NOTE_USEROBJQUOTA
},
4434 { ZFS_DELEG_PERM_USEROBJUSED
, ZFS_DELEG_NOTE_USEROBJUSED
},
4435 { ZFS_DELEG_PERM_GROUPOBJQUOTA
, ZFS_DELEG_NOTE_GROUPOBJQUOTA
},
4436 { ZFS_DELEG_PERM_GROUPOBJUSED
, ZFS_DELEG_NOTE_GROUPOBJUSED
},
4437 { ZFS_DELEG_PERM_PROJECTUSED
, ZFS_DELEG_NOTE_PROJECTUSED
},
4438 { ZFS_DELEG_PERM_PROJECTQUOTA
, ZFS_DELEG_NOTE_PROJECTQUOTA
},
4439 { ZFS_DELEG_PERM_PROJECTOBJUSED
, ZFS_DELEG_NOTE_PROJECTOBJUSED
},
4440 { ZFS_DELEG_PERM_PROJECTOBJQUOTA
, ZFS_DELEG_NOTE_PROJECTOBJQUOTA
},
4441 { NULL
, ZFS_DELEG_NOTE_NONE
}
4444 /* permission structure */
4445 typedef struct deleg_perm
{
4446 zfs_deleg_who_type_t dp_who_type
;
4447 const char *dp_name
;
4449 boolean_t dp_descend
;
4453 typedef struct deleg_perm_node
{
4454 deleg_perm_t dpn_perm
;
4456 uu_avl_node_t dpn_avl_node
;
4457 } deleg_perm_node_t
;
4459 typedef struct fs_perm fs_perm_t
;
4461 /* permissions set */
4462 typedef struct who_perm
{
4463 zfs_deleg_who_type_t who_type
;
4464 const char *who_name
; /* id */
4465 char who_ug_name
[256]; /* user/group name */
4466 fs_perm_t
*who_fsperm
; /* uplink */
4468 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
4472 typedef struct who_perm_node
{
4473 who_perm_t who_perm
;
4474 uu_avl_node_t who_avl_node
;
4477 typedef struct fs_perm_set fs_perm_set_t
;
4478 /* fs permissions */
4480 const char *fsp_name
;
4482 uu_avl_t
*fsp_sc_avl
; /* sets,create */
4483 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
4485 fs_perm_set_t
*fsp_set
; /* uplink */
4489 typedef struct fs_perm_node
{
4490 fs_perm_t fspn_fsperm
;
4493 uu_list_node_t fspn_list_node
;
4496 /* top level structure */
4497 struct fs_perm_set
{
4498 uu_list_pool_t
*fsps_list_pool
;
4499 uu_list_t
*fsps_list
; /* list of fs_perms */
4501 uu_avl_pool_t
*fsps_named_set_avl_pool
;
4502 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
4503 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
4506 static inline const char *
4507 deleg_perm_type(zfs_deleg_note_t note
)
4513 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4514 case ZFS_DELEG_NOTE_GROUPUSED
:
4515 case ZFS_DELEG_NOTE_USERPROP
:
4516 case ZFS_DELEG_NOTE_USERQUOTA
:
4517 case ZFS_DELEG_NOTE_USERUSED
:
4518 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
4519 case ZFS_DELEG_NOTE_USEROBJUSED
:
4520 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
4521 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
4522 case ZFS_DELEG_NOTE_PROJECTUSED
:
4523 case ZFS_DELEG_NOTE_PROJECTQUOTA
:
4524 case ZFS_DELEG_NOTE_PROJECTOBJUSED
:
4525 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA
:
4527 return (gettext("other"));
4529 return (gettext("subcommand"));
4534 who_type2weight(zfs_deleg_who_type_t who_type
)
4538 case ZFS_DELEG_NAMED_SET_SETS
:
4539 case ZFS_DELEG_NAMED_SET
:
4542 case ZFS_DELEG_CREATE_SETS
:
4543 case ZFS_DELEG_CREATE
:
4546 case ZFS_DELEG_USER_SETS
:
4547 case ZFS_DELEG_USER
:
4550 case ZFS_DELEG_GROUP_SETS
:
4551 case ZFS_DELEG_GROUP
:
4554 case ZFS_DELEG_EVERYONE_SETS
:
4555 case ZFS_DELEG_EVERYONE
:
4567 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4569 const who_perm_node_t
*l
= larg
;
4570 const who_perm_node_t
*r
= rarg
;
4571 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
4572 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
4573 int lweight
= who_type2weight(ltype
);
4574 int rweight
= who_type2weight(rtype
);
4575 int res
= lweight
- rweight
;
4577 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
4578 ZFS_MAX_DELEG_NAME
-1);
4590 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4592 const deleg_perm_node_t
*l
= larg
;
4593 const deleg_perm_node_t
*r
= rarg
;
4594 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
4595 ZFS_MAX_DELEG_NAME
-1);
4607 fs_perm_set_init(fs_perm_set_t
*fspset
)
4609 bzero(fspset
, sizeof (fs_perm_set_t
));
4611 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4612 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4613 NULL
, UU_DEFAULT
)) == NULL
)
4615 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4616 UU_DEFAULT
)) == NULL
)
4619 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4620 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4621 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4622 UU_DEFAULT
)) == NULL
)
4625 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4626 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4627 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4628 UU_DEFAULT
)) == NULL
)
4631 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4632 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4633 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4638 static inline void fs_perm_fini(fs_perm_t
*);
4639 static inline void who_perm_fini(who_perm_t
*);
4642 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4644 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4646 while (node
!= NULL
) {
4647 fs_perm_node_t
*next_node
=
4648 uu_list_next(fspset
->fsps_list
, node
);
4649 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4650 fs_perm_fini(fsperm
);
4651 uu_list_remove(fspset
->fsps_list
, node
);
4656 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4657 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4658 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4662 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4665 deleg_perm
->dp_who_type
= type
;
4666 deleg_perm
->dp_name
= name
;
4670 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4671 zfs_deleg_who_type_t type
, const char *name
)
4673 uu_avl_pool_t
*pool
;
4674 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4676 bzero(who_perm
, sizeof (who_perm_t
));
4678 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4679 UU_DEFAULT
)) == NULL
)
4682 who_perm
->who_type
= type
;
4683 who_perm
->who_name
= name
;
4684 who_perm
->who_fsperm
= fsperm
;
4688 who_perm_fini(who_perm_t
*who_perm
)
4690 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4692 while (node
!= NULL
) {
4693 deleg_perm_node_t
*next_node
=
4694 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4696 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4701 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4705 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4707 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4708 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4710 bzero(fsperm
, sizeof (fs_perm_t
));
4712 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4716 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4720 fsperm
->fsp_set
= fspset
;
4721 fsperm
->fsp_name
= fsname
;
4725 fs_perm_fini(fs_perm_t
*fsperm
)
4727 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4728 while (node
!= NULL
) {
4729 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4731 who_perm_t
*who_perm
= &node
->who_perm
;
4732 who_perm_fini(who_perm
);
4733 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4738 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4739 while (node
!= NULL
) {
4740 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4742 who_perm_t
*who_perm
= &node
->who_perm
;
4743 who_perm_fini(who_perm
);
4744 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4749 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4750 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4754 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4755 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4757 uu_avl_index_t idx
= 0;
4759 deleg_perm_node_t
*found_node
= NULL
;
4760 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4762 deleg_perm_init(deleg_perm
, who_type
, name
);
4764 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4766 uu_avl_insert(avl
, node
, idx
);
4769 deleg_perm
= &node
->dpn_perm
;
4774 case ZFS_DELEG_LOCAL
:
4775 deleg_perm
->dp_local
= B_TRUE
;
4777 case ZFS_DELEG_DESCENDENT
:
4778 deleg_perm
->dp_descend
= B_TRUE
;
4783 assert(B_FALSE
); /* invalid locality */
4788 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4790 nvpair_t
*nvp
= NULL
;
4791 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4792 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4793 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4795 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4796 const char *name
= nvpair_name(nvp
);
4797 data_type_t type
= nvpair_type(nvp
);
4798 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4799 deleg_perm_node_t
*node
=
4800 safe_malloc(sizeof (deleg_perm_node_t
));
4802 VERIFY(type
== DATA_TYPE_BOOLEAN
);
4804 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4805 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4812 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4814 nvpair_t
*nvp
= NULL
;
4815 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4817 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4818 nvlist_t
*nvl2
= NULL
;
4819 const char *name
= nvpair_name(nvp
);
4820 uu_avl_t
*avl
= NULL
;
4821 uu_avl_pool_t
*avl_pool
= NULL
;
4822 zfs_deleg_who_type_t perm_type
= name
[0];
4823 char perm_locality
= name
[1];
4824 const char *perm_name
= name
+ 3;
4825 boolean_t is_set
= B_TRUE
;
4826 who_perm_t
*who_perm
= NULL
;
4828 assert('$' == name
[2]);
4830 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4833 switch (perm_type
) {
4834 case ZFS_DELEG_CREATE
:
4835 case ZFS_DELEG_CREATE_SETS
:
4836 case ZFS_DELEG_NAMED_SET
:
4837 case ZFS_DELEG_NAMED_SET_SETS
:
4838 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4839 avl
= fsperm
->fsp_sc_avl
;
4841 case ZFS_DELEG_USER
:
4842 case ZFS_DELEG_USER_SETS
:
4843 case ZFS_DELEG_GROUP
:
4844 case ZFS_DELEG_GROUP_SETS
:
4845 case ZFS_DELEG_EVERYONE
:
4846 case ZFS_DELEG_EVERYONE_SETS
:
4847 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4848 avl
= fsperm
->fsp_uge_avl
;
4852 assert(!"unhandled zfs_deleg_who_type_t");
4856 who_perm_node_t
*found_node
= NULL
;
4857 who_perm_node_t
*node
= safe_malloc(
4858 sizeof (who_perm_node_t
));
4859 who_perm
= &node
->who_perm
;
4860 uu_avl_index_t idx
= 0;
4862 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4863 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4865 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4867 if (avl
== fsperm
->fsp_uge_avl
) {
4869 struct passwd
*p
= NULL
;
4870 struct group
*g
= NULL
;
4871 const char *nice_name
= NULL
;
4873 switch (perm_type
) {
4874 case ZFS_DELEG_USER_SETS
:
4875 case ZFS_DELEG_USER
:
4876 rid
= atoi(perm_name
);
4879 nice_name
= p
->pw_name
;
4881 case ZFS_DELEG_GROUP_SETS
:
4882 case ZFS_DELEG_GROUP
:
4883 rid
= atoi(perm_name
);
4886 nice_name
= g
->gr_name
;
4893 if (nice_name
!= NULL
)
4895 node
->who_perm
.who_ug_name
,
4899 uu_avl_insert(avl
, node
, idx
);
4902 who_perm
= &node
->who_perm
;
4905 VERIFY3P(who_perm
, !=, NULL
);
4906 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4913 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4915 nvpair_t
*nvp
= NULL
;
4916 uu_avl_index_t idx
= 0;
4918 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4919 nvlist_t
*nvl2
= NULL
;
4920 const char *fsname
= nvpair_name(nvp
);
4921 data_type_t type
= nvpair_type(nvp
);
4922 fs_perm_t
*fsperm
= NULL
;
4923 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4927 fsperm
= &node
->fspn_fsperm
;
4929 VERIFY(DATA_TYPE_NVLIST
== type
);
4931 uu_list_node_init(node
, &node
->fspn_list_node
,
4932 fspset
->fsps_list_pool
);
4934 idx
= uu_list_numnodes(fspset
->fsps_list
);
4935 fs_perm_init(fsperm
, fspset
, fsname
);
4937 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4940 (void) parse_fs_perm(fsperm
, nvl2
);
4942 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4948 static inline const char *
4949 deleg_perm_comment(zfs_deleg_note_t note
)
4951 const char *str
= "";
4956 case ZFS_DELEG_NOTE_ALLOW
:
4957 str
= gettext("Must also have the permission that is being"
4958 "\n\t\t\t\tallowed");
4960 case ZFS_DELEG_NOTE_CLONE
:
4961 str
= gettext("Must also have the 'create' ability and 'mount'"
4962 "\n\t\t\t\tability in the origin file system");
4964 case ZFS_DELEG_NOTE_CREATE
:
4965 str
= gettext("Must also have the 'mount' ability");
4967 case ZFS_DELEG_NOTE_DESTROY
:
4968 str
= gettext("Must also have the 'mount' ability");
4970 case ZFS_DELEG_NOTE_DIFF
:
4971 str
= gettext("Allows lookup of paths within a dataset;"
4972 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4973 "\n\t\t\t\tin order to use zfs diff");
4975 case ZFS_DELEG_NOTE_HOLD
:
4976 str
= gettext("Allows adding a user hold to a snapshot");
4978 case ZFS_DELEG_NOTE_MOUNT
:
4979 str
= gettext("Allows mount/umount of ZFS datasets");
4981 case ZFS_DELEG_NOTE_PROMOTE
:
4982 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4983 " 'promote' ability in the origin file system");
4985 case ZFS_DELEG_NOTE_RECEIVE
:
4986 str
= gettext("Must also have the 'mount' and 'create'"
4989 case ZFS_DELEG_NOTE_RELEASE
:
4990 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4991 "might destroy the snapshot");
4993 case ZFS_DELEG_NOTE_RENAME
:
4994 str
= gettext("Must also have the 'mount' and 'create'"
4995 "\n\t\t\t\tability in the new parent");
4997 case ZFS_DELEG_NOTE_ROLLBACK
:
5000 case ZFS_DELEG_NOTE_SEND
:
5003 case ZFS_DELEG_NOTE_SHARE
:
5004 str
= gettext("Allows sharing file systems over NFS or SMB"
5005 "\n\t\t\t\tprotocols");
5007 case ZFS_DELEG_NOTE_SNAPSHOT
:
5010 case ZFS_DELEG_NOTE_LOAD_KEY
:
5011 str
= gettext("Allows loading or unloading an encryption key");
5013 case ZFS_DELEG_NOTE_CHANGE_KEY
:
5014 str
= gettext("Allows changing or adding an encryption key");
5017 * case ZFS_DELEG_NOTE_VSCAN:
5018 * str = gettext("");
5022 case ZFS_DELEG_NOTE_GROUPQUOTA
:
5023 str
= gettext("Allows accessing any groupquota@... property");
5025 case ZFS_DELEG_NOTE_GROUPUSED
:
5026 str
= gettext("Allows reading any groupused@... property");
5028 case ZFS_DELEG_NOTE_USERPROP
:
5029 str
= gettext("Allows changing any user property");
5031 case ZFS_DELEG_NOTE_USERQUOTA
:
5032 str
= gettext("Allows accessing any userquota@... property");
5034 case ZFS_DELEG_NOTE_USERUSED
:
5035 str
= gettext("Allows reading any userused@... property");
5037 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
5038 str
= gettext("Allows accessing any userobjquota@... property");
5040 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
5041 str
= gettext("Allows accessing any \n\t\t\t\t"
5042 "groupobjquota@... property");
5044 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
5045 str
= gettext("Allows reading any groupobjused@... property");
5047 case ZFS_DELEG_NOTE_USEROBJUSED
:
5048 str
= gettext("Allows reading any userobjused@... property");
5050 case ZFS_DELEG_NOTE_PROJECTQUOTA
:
5051 str
= gettext("Allows accessing any projectquota@... property");
5053 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA
:
5054 str
= gettext("Allows accessing any \n\t\t\t\t"
5055 "projectobjquota@... property");
5057 case ZFS_DELEG_NOTE_PROJECTUSED
:
5058 str
= gettext("Allows reading any projectused@... property");
5060 case ZFS_DELEG_NOTE_PROJECTOBJUSED
:
5061 str
= gettext("Allows accessing any \n\t\t\t\t"
5062 "projectobjused@... property");
5080 boolean_t recursive
; /* unallow only */
5081 boolean_t prt_usage
;
5083 boolean_t prt_perms
;
5086 const char *dataset
;
5090 prop_cmp(const void *a
, const void *b
)
5092 const char *str1
= *(const char **)a
;
5093 const char *str2
= *(const char **)b
;
5094 return (strcmp(str1
, str2
));
5098 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
5100 const char *opt_desc
[] = {
5101 "-h", gettext("show this help message and exit"),
5102 "-l", gettext("set permission locally"),
5103 "-d", gettext("set permission for descents"),
5104 "-u", gettext("set permission for user"),
5105 "-g", gettext("set permission for group"),
5106 "-e", gettext("set permission for everyone"),
5107 "-c", gettext("set create time permission"),
5108 "-s", gettext("define permission set"),
5110 "-r", gettext("remove permissions recursively"),
5112 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
5113 size_t allow_size
= unallow_size
- 2;
5114 const char *props
[ZFS_NUM_PROPS
];
5117 FILE *fp
= requested
? stdout
: stderr
;
5118 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
5119 const char *fmt
= gettext("%-16s %-14s\t%s\n");
5121 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
5123 (void) fprintf(fp
, gettext("Options:\n"));
5124 for (i
= 0; i
< (un
? unallow_size
: allow_size
); i
+= 2) {
5125 const char *opt
= opt_desc
[i
];
5126 const char *optdsc
= opt_desc
[i
+ 1];
5127 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
5130 (void) fprintf(fp
, gettext("\nThe following permissions are "
5132 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
5134 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
5135 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
5136 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
5137 const char *perm_type
= deleg_perm_type(perm_note
);
5138 const char *perm_comment
= deleg_perm_comment(perm_note
);
5139 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
5142 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
5143 zprop_desc_t
*pd
= &pdtbl
[i
];
5144 if (pd
->pd_visible
!= B_TRUE
)
5147 if (pd
->pd_attr
== PROP_READONLY
)
5150 props
[count
++] = pd
->pd_name
;
5152 props
[count
] = NULL
;
5154 qsort(props
, count
, sizeof (char *), prop_cmp
);
5156 for (i
= 0; i
< count
; i
++)
5157 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
5160 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
5162 exit(requested
? 0 : 2);
5165 static inline const char *
5166 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
5169 if (un
&& argc
== expected_argc
- 1)
5171 else if (argc
== expected_argc
)
5172 *permsp
= argv
[argc
- 2];
5174 allow_usage(un
, B_FALSE
,
5175 gettext("wrong number of parameters\n"));
5177 return (argv
[argc
- 1]);
5181 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
5183 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
5184 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
5185 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
5186 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
5189 allow_usage(un
, B_FALSE
,
5190 gettext("-u, -g, and -e are mutually exclusive\n"));
5192 if (opts
->prt_usage
) {
5193 if (argc
== 0 && all_sum
== 0)
5194 allow_usage(un
, B_TRUE
, NULL
);
5201 allow_usage(un
, B_FALSE
,
5202 gettext("invalid options combined with -s\n"));
5204 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
5205 if (argv
[0][0] != '@')
5206 allow_usage(un
, B_FALSE
,
5207 gettext("invalid set name: missing '@' prefix\n"));
5208 opts
->who
= argv
[0];
5209 } else if (opts
->create
) {
5210 if (ldcsuge_sum
> 1)
5211 allow_usage(un
, B_FALSE
,
5212 gettext("invalid options combined with -c\n"));
5213 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5214 } else if (opts
->everyone
) {
5216 allow_usage(un
, B_FALSE
,
5217 gettext("invalid options combined with -e\n"));
5218 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5219 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
5221 opts
->everyone
= B_TRUE
;
5224 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5225 } else if (argc
== 1 && !un
) {
5226 opts
->prt_perms
= B_TRUE
;
5227 opts
->dataset
= argv
[argc
-1];
5229 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
5230 opts
->who
= argv
[0];
5233 if (!opts
->local
&& !opts
->descend
) {
5234 opts
->local
= B_TRUE
;
5235 opts
->descend
= B_TRUE
;
5240 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
5241 const char *who
, char *perms
, nvlist_t
*top_nvl
)
5244 char ld
[2] = { '\0', '\0' };
5245 char who_buf
[MAXNAMELEN
+ 32];
5246 char base_type
= '\0';
5247 char set_type
= '\0';
5248 nvlist_t
*base_nvl
= NULL
;
5249 nvlist_t
*set_nvl
= NULL
;
5252 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
5254 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
5258 case ZFS_DELEG_NAMED_SET_SETS
:
5259 case ZFS_DELEG_NAMED_SET
:
5260 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
5261 base_type
= ZFS_DELEG_NAMED_SET
;
5262 ld
[0] = ZFS_DELEG_NA
;
5264 case ZFS_DELEG_CREATE_SETS
:
5265 case ZFS_DELEG_CREATE
:
5266 set_type
= ZFS_DELEG_CREATE_SETS
;
5267 base_type
= ZFS_DELEG_CREATE
;
5268 ld
[0] = ZFS_DELEG_NA
;
5270 case ZFS_DELEG_USER_SETS
:
5271 case ZFS_DELEG_USER
:
5272 set_type
= ZFS_DELEG_USER_SETS
;
5273 base_type
= ZFS_DELEG_USER
;
5275 ld
[0] = ZFS_DELEG_LOCAL
;
5277 ld
[1] = ZFS_DELEG_DESCENDENT
;
5279 case ZFS_DELEG_GROUP_SETS
:
5280 case ZFS_DELEG_GROUP
:
5281 set_type
= ZFS_DELEG_GROUP_SETS
;
5282 base_type
= ZFS_DELEG_GROUP
;
5284 ld
[0] = ZFS_DELEG_LOCAL
;
5286 ld
[1] = ZFS_DELEG_DESCENDENT
;
5288 case ZFS_DELEG_EVERYONE_SETS
:
5289 case ZFS_DELEG_EVERYONE
:
5290 set_type
= ZFS_DELEG_EVERYONE_SETS
;
5291 base_type
= ZFS_DELEG_EVERYONE
;
5293 ld
[0] = ZFS_DELEG_LOCAL
;
5295 ld
[1] = ZFS_DELEG_DESCENDENT
;
5299 assert(set_type
!= '\0' && base_type
!= '\0');
5302 if (perms
!= NULL
) {
5304 char *end
= curr
+ strlen(perms
);
5306 while (curr
< end
) {
5307 char *delim
= strchr(curr
, ',');
5318 (void) nvlist_add_boolean(nvl
, curr
);
5324 for (i
= 0; i
< 2; i
++) {
5325 char locality
= ld
[i
];
5329 if (!nvlist_empty(base_nvl
)) {
5331 (void) snprintf(who_buf
,
5332 sizeof (who_buf
), "%c%c$%s",
5333 base_type
, locality
, who
);
5335 (void) snprintf(who_buf
,
5336 sizeof (who_buf
), "%c%c$",
5337 base_type
, locality
);
5339 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5344 if (!nvlist_empty(set_nvl
)) {
5346 (void) snprintf(who_buf
,
5347 sizeof (who_buf
), "%c%c$%s",
5348 set_type
, locality
, who
);
5350 (void) snprintf(who_buf
,
5351 sizeof (who_buf
), "%c%c$",
5352 set_type
, locality
);
5354 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5359 for (i
= 0; i
< 2; i
++) {
5360 char locality
= ld
[i
];
5365 (void) snprintf(who_buf
, sizeof (who_buf
),
5366 "%c%c$%s", base_type
, locality
, who
);
5368 (void) snprintf(who_buf
, sizeof (who_buf
),
5369 "%c%c$", base_type
, locality
);
5370 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5373 (void) snprintf(who_buf
, sizeof (who_buf
),
5374 "%c%c$%s", set_type
, locality
, who
);
5376 (void) snprintf(who_buf
, sizeof (who_buf
),
5377 "%c%c$", set_type
, locality
);
5378 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5384 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
5386 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
5390 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
5391 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
5392 } else if (opts
->create
) {
5393 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
5394 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5395 } else if (opts
->everyone
) {
5396 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
5397 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5399 char *curr
= opts
->who
;
5400 char *end
= curr
+ strlen(curr
);
5402 while (curr
< end
) {
5404 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
5406 char *delim
= strchr(curr
, ',');
5409 struct passwd
*p
= NULL
;
5410 struct group
*g
= NULL
;
5418 rid
= (uid_t
)strtol(curr
, &endch
, 0);
5420 who_type
= ZFS_DELEG_USER
;
5429 (void) snprintf(errbuf
, 256, gettext(
5430 "invalid user %s"), curr
);
5431 allow_usage(un
, B_TRUE
, errbuf
);
5433 } else if (opts
->group
) {
5434 who_type
= ZFS_DELEG_GROUP
;
5443 (void) snprintf(errbuf
, 256, gettext(
5444 "invalid group %s"), curr
);
5445 allow_usage(un
, B_TRUE
, errbuf
);
5448 if (*endch
!= '\0') {
5455 if (*endch
!= '\0') {
5463 who_type
= ZFS_DELEG_USER
;
5465 } else if (g
!= NULL
) {
5466 who_type
= ZFS_DELEG_GROUP
;
5469 (void) snprintf(errbuf
, 256, gettext(
5470 "invalid user/group %s"), curr
);
5471 allow_usage(un
, B_TRUE
, errbuf
);
5475 (void) sprintf(id
, "%u", rid
);
5478 store_allow_perm(who_type
, opts
->local
,
5479 opts
->descend
, who
, opts
->perms
, *nvlp
);
5488 print_set_creat_perms(uu_avl_t
*who_avl
)
5490 const char *sc_title
[] = {
5491 gettext("Permission sets:\n"),
5492 gettext("Create time permissions:\n"),
5495 who_perm_node_t
*who_node
= NULL
;
5496 int prev_weight
= -1;
5498 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
5499 who_node
= uu_avl_next(who_avl
, who_node
)) {
5500 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5501 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5502 const char *who_name
= who_node
->who_perm
.who_name
;
5503 int weight
= who_type2weight(who_type
);
5504 boolean_t first
= B_TRUE
;
5505 deleg_perm_node_t
*deleg_node
;
5507 if (prev_weight
!= weight
) {
5508 (void) printf("%s", sc_title
[weight
]);
5509 prev_weight
= weight
;
5512 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
5513 (void) printf("\t");
5515 (void) printf("\t%s ", who_name
);
5517 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
5518 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5521 deleg_node
->dpn_perm
.dp_name
);
5524 (void) printf(",%s",
5525 deleg_node
->dpn_perm
.dp_name
);
5528 (void) printf("\n");
5533 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
5536 who_perm_node_t
*who_node
= NULL
;
5537 boolean_t prt_title
= B_TRUE
;
5538 uu_avl_walk_t
*walk
;
5540 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
5543 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
5544 const char *who_name
= who_node
->who_perm
.who_name
;
5545 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
5546 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5547 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5549 deleg_perm_node_t
*deleg_node
;
5550 boolean_t prt_who
= B_TRUE
;
5552 for (deleg_node
= uu_avl_first(avl
);
5554 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5555 if (local
!= deleg_node
->dpn_perm
.dp_local
||
5556 descend
!= deleg_node
->dpn_perm
.dp_descend
)
5560 const char *who
= NULL
;
5562 prt_title
= B_FALSE
;
5563 (void) printf("%s", title
);
5567 case ZFS_DELEG_USER_SETS
:
5568 case ZFS_DELEG_USER
:
5569 who
= gettext("user");
5571 who_name
= nice_who_name
;
5573 case ZFS_DELEG_GROUP_SETS
:
5574 case ZFS_DELEG_GROUP
:
5575 who
= gettext("group");
5577 who_name
= nice_who_name
;
5579 case ZFS_DELEG_EVERYONE_SETS
:
5580 case ZFS_DELEG_EVERYONE
:
5581 who
= gettext("everyone");
5586 assert(who
!= NULL
);
5590 if (who_name
== NULL
)
5591 (void) printf("\t%s", who
);
5593 (void) printf("\t%s %s", who
, who_name
);
5596 (void) printf("%c%s", delim
,
5597 deleg_node
->dpn_perm
.dp_name
);
5602 (void) printf("\n");
5605 uu_avl_walk_end(walk
);
5609 print_fs_perms(fs_perm_set_t
*fspset
)
5611 fs_perm_node_t
*node
= NULL
;
5612 char buf
[MAXNAMELEN
+ 32];
5613 const char *dsname
= buf
;
5615 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
5616 node
= uu_list_next(fspset
->fsps_list
, node
)) {
5617 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
5618 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
5621 (void) snprintf(buf
, sizeof (buf
),
5622 gettext("---- Permissions on %s "),
5623 node
->fspn_fsperm
.fsp_name
);
5624 (void) printf("%s", dsname
);
5625 left
= 70 - strlen(buf
);
5628 (void) printf("\n");
5630 print_set_creat_perms(sc_avl
);
5631 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
5632 gettext("Local permissions:\n"));
5633 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
5634 gettext("Descendent permissions:\n"));
5635 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
5636 gettext("Local+Descendent permissions:\n"));
5640 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5642 struct deleg_perms
{
5648 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5650 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5651 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5653 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5656 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5660 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5663 nvlist_t
*perm_nvl
= NULL
;
5664 nvlist_t
*update_perm_nvl
= NULL
;
5667 struct allow_opts opts
= { 0 };
5669 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5672 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5675 opts
.local
= B_TRUE
;
5678 opts
.descend
= B_TRUE
;
5684 opts
.group
= B_TRUE
;
5687 opts
.everyone
= B_TRUE
;
5693 opts
.create
= B_TRUE
;
5696 opts
.recursive
= B_TRUE
;
5699 (void) fprintf(stderr
, gettext("missing argument for "
5700 "'%c' option\n"), optopt
);
5704 opts
.prt_usage
= B_TRUE
;
5707 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5716 /* check arguments */
5717 parse_allow_args(argc
, argv
, un
, &opts
);
5719 /* try to open the dataset */
5720 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5721 ZFS_TYPE_VOLUME
)) == NULL
) {
5722 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5727 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5730 fs_perm_set_init(&fs_perm_set
);
5731 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5732 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5737 print_fs_perms(&fs_perm_set
);
5739 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5740 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5743 if (un
&& opts
.recursive
) {
5744 struct deleg_perms data
= { un
, update_perm_nvl
};
5745 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5754 nvlist_free(perm_nvl
);
5755 nvlist_free(update_perm_nvl
);
5757 fs_perm_set_fini(&fs_perm_set
);
5765 zfs_do_allow(int argc
, char **argv
)
5767 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5771 zfs_do_unallow(int argc
, char **argv
)
5773 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5777 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5782 boolean_t recursive
= B_FALSE
;
5783 const char *opts
= holding
? "rt" : "r";
5787 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5793 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5802 /* check number of arguments */
5810 if (holding
&& tag
[0] == '.') {
5811 /* tags starting with '.' are reserved for libzfs */
5812 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5816 for (i
= 0; i
< argc
; ++i
) {
5818 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
5820 char *path
= argv
[i
];
5822 delim
= strchr(path
, '@');
5823 if (delim
== NULL
) {
5824 (void) fprintf(stderr
,
5825 gettext("'%s' is not a snapshot\n"), path
);
5829 (void) strncpy(parent
, path
, delim
- path
);
5830 parent
[delim
- path
] = '\0';
5832 zhp
= zfs_open(g_zfs
, parent
,
5833 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5839 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
5842 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5848 return (errors
!= 0);
5852 * zfs hold [-r] [-t] <tag> <snap> ...
5854 * -r Recursively hold
5856 * Apply a user-hold with the given tag to the list of snapshots.
5859 zfs_do_hold(int argc
, char **argv
)
5861 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5865 * zfs release [-r] <tag> <snap> ...
5867 * -r Recursively release
5869 * Release a user-hold with the given tag from the list of snapshots.
5872 zfs_do_release(int argc
, char **argv
)
5874 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5877 typedef struct holds_cbdata
{
5878 boolean_t cb_recursive
;
5879 const char *cb_snapname
;
5881 size_t cb_max_namelen
;
5882 size_t cb_max_taglen
;
5885 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5886 #define DATETIME_BUF_LEN (32)
5891 print_holds(boolean_t scripted
, int nwidth
, int tagwidth
, nvlist_t
*nvl
)
5894 nvpair_t
*nvp
= NULL
;
5895 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5899 for (i
= 0; i
< 3; i
++) {
5900 col
= gettext(hdr_cols
[i
]);
5902 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5905 (void) printf("%s\n", col
);
5909 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5910 char *zname
= nvpair_name(nvp
);
5912 nvpair_t
*nvp2
= NULL
;
5913 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5914 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5915 char tsbuf
[DATETIME_BUF_LEN
];
5916 char *tagname
= nvpair_name(nvp2
);
5921 (void) nvpair_value_uint64(nvp2
, &val
);
5923 (void) localtime_r(&time
, &t
);
5924 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5925 gettext(STRFTIME_FMT_STR
), &t
);
5928 (void) printf("%s\t%s\t%s\n", zname
,
5931 (void) printf("%-*s %-*s %s\n", nwidth
,
5932 zname
, tagwidth
, tagname
, tsbuf
);
5939 * Generic callback function to list a dataset or snapshot.
5942 holds_callback(zfs_handle_t
*zhp
, void *data
)
5944 holds_cbdata_t
*cbp
= data
;
5945 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5946 nvlist_t
*nvl
= NULL
;
5947 nvpair_t
*nvp
= NULL
;
5948 const char *zname
= zfs_get_name(zhp
);
5949 size_t znamelen
= strlen(zname
);
5951 if (cbp
->cb_recursive
) {
5952 const char *snapname
;
5953 char *delim
= strchr(zname
, '@');
5957 snapname
= delim
+ 1;
5958 if (strcmp(cbp
->cb_snapname
, snapname
))
5962 if (zfs_get_holds(zhp
, &nvl
) != 0)
5965 if (znamelen
> cbp
->cb_max_namelen
)
5966 cbp
->cb_max_namelen
= znamelen
;
5968 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5969 const char *tag
= nvpair_name(nvp
);
5970 size_t taglen
= strlen(tag
);
5971 if (taglen
> cbp
->cb_max_taglen
)
5972 cbp
->cb_max_taglen
= taglen
;
5975 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5979 * zfs holds [-rH] <snap> ...
5981 * -r Lists holds that are set on the named snapshots recursively.
5982 * -H Scripted mode; elide headers and separate columns by tabs.
5985 zfs_do_holds(int argc
, char **argv
)
5990 boolean_t scripted
= B_FALSE
;
5991 boolean_t recursive
= B_FALSE
;
5992 const char *opts
= "rH";
5995 int types
= ZFS_TYPE_SNAPSHOT
;
5996 holds_cbdata_t cb
= { 0 };
6003 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
6012 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6019 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
6020 flags
|= ZFS_ITER_RECURSE
;
6026 /* check number of arguments */
6030 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
6033 for (i
= 0; i
< argc
; ++i
) {
6034 char *snapshot
= argv
[i
];
6036 const char *snapname
;
6038 delim
= strchr(snapshot
, '@');
6039 if (delim
== NULL
) {
6040 (void) fprintf(stderr
,
6041 gettext("'%s' is not a snapshot\n"), snapshot
);
6045 snapname
= delim
+ 1;
6047 snapshot
[delim
- snapshot
] = '\0';
6049 cb
.cb_recursive
= recursive
;
6050 cb
.cb_snapname
= snapname
;
6054 * 1. collect holds data, set format options
6056 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
6057 holds_callback
, &cb
);
6063 * 2. print holds data
6065 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
6067 if (nvlist_empty(nvl
))
6068 (void) fprintf(stderr
, gettext("no datasets available\n"));
6072 return (0 != errors
);
6075 #define CHECK_SPINNER 30
6076 #define SPINNER_TIME 3 /* seconds */
6077 #define MOUNT_TIME 1 /* seconds */
6079 typedef struct get_all_state
{
6080 boolean_t ga_verbose
;
6081 get_all_cb_t
*ga_cbp
;
6085 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
6087 static char *spin
[] = { "-", "\\", "|", "/" };
6088 static int spinval
= 0;
6089 static int spincheck
= 0;
6090 static time_t last_spin_time
= (time_t)0;
6091 get_all_state_t
*state
= data
;
6092 zfs_type_t type
= zfs_get_type(zhp
);
6094 if (state
->ga_verbose
) {
6095 if (--spincheck
< 0) {
6096 time_t now
= time(NULL
);
6097 if (last_spin_time
+ SPINNER_TIME
< now
) {
6098 update_progress(spin
[spinval
++ % 4]);
6099 last_spin_time
= now
;
6101 spincheck
= CHECK_SPINNER
;
6106 * Iterate over any nested datasets.
6108 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
6114 * Skip any datasets whose type does not match.
6116 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
6120 libzfs_add_handle(state
->ga_cbp
, zhp
);
6121 assert(state
->ga_cbp
->cb_used
<= state
->ga_cbp
->cb_alloc
);
6127 get_all_datasets(get_all_cb_t
*cbp
, boolean_t verbose
)
6129 get_all_state_t state
= {
6130 .ga_verbose
= verbose
,
6135 set_progress_header(gettext("Reading ZFS config"));
6136 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &state
);
6139 finish_progress(gettext("done."));
6143 * Generic callback for sharing or mounting filesystems. Because the code is so
6144 * similar, we have a common function with an extra parameter to determine which
6145 * mode we are using.
6147 typedef enum { OP_SHARE
, OP_MOUNT
} share_mount_op_t
;
6149 typedef struct share_mount_state
{
6150 share_mount_op_t sm_op
;
6151 boolean_t sm_verbose
;
6154 char *sm_proto
; /* only valid for OP_SHARE */
6155 pthread_mutex_t sm_lock
; /* protects the remaining fields */
6156 uint_t sm_total
; /* number of filesystems to process */
6157 uint_t sm_done
; /* number of filesystems processed */
6158 int sm_status
; /* -1 if any of the share/mount operations failed */
6159 } share_mount_state_t
;
6162 * Share or mount a dataset.
6165 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
6166 boolean_t
explicit, const char *options
)
6168 char mountpoint
[ZFS_MAXPROPLEN
];
6169 char shareopts
[ZFS_MAXPROPLEN
];
6170 char smbshareopts
[ZFS_MAXPROPLEN
];
6171 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
6173 uint64_t zoned
, canmount
;
6174 boolean_t shared_nfs
, shared_smb
;
6176 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
6179 * Check to make sure we can mount/share this dataset. If we
6180 * are in the global zone and the filesystem is exported to a
6181 * local zone, or if we are in a local zone and the
6182 * filesystem is not exported, then it is an error.
6184 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
6186 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
6190 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6191 "dataset is exported to a local zone\n"), cmdname
,
6195 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
6199 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6200 "permission denied\n"), cmdname
,
6206 * Ignore any filesystems which don't apply to us. This
6207 * includes those with a legacy mountpoint, or those with
6208 * legacy share options.
6210 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
6211 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
6212 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
6213 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
6214 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
6215 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
6217 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
6218 strcmp(smbshareopts
, "off") == 0) {
6222 (void) fprintf(stderr
, gettext("cannot share '%s': "
6223 "legacy share\n"), zfs_get_name(zhp
));
6224 (void) fprintf(stderr
, gettext("use share(1M) to "
6225 "share this filesystem, or set "
6226 "sharenfs property on\n"));
6231 * We cannot share or mount legacy filesystems. If the
6232 * shareopts is non-legacy but the mountpoint is legacy, we
6233 * treat it as a legacy share.
6235 if (strcmp(mountpoint
, "legacy") == 0) {
6239 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6240 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
6241 (void) fprintf(stderr
, gettext("use %s(1M) to "
6242 "%s this filesystem\n"), cmdname
, cmdname
);
6246 if (strcmp(mountpoint
, "none") == 0) {
6250 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
6251 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
6256 * canmount explicit outcome
6257 * on no pass through
6258 * on yes pass through
6260 * off yes display error, return 1
6261 * noauto no return 0
6262 * noauto yes pass through
6264 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
6265 if (canmount
== ZFS_CANMOUNT_OFF
) {
6269 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6270 "'canmount' property is set to 'off'\n"), cmdname
,
6273 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
6278 * If this filesystem is encrypted and does not have
6279 * a loaded key, we can not mount it.
6281 if ((flags
& MS_CRYPT
) == 0 &&
6282 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) != ZIO_CRYPT_OFF
&&
6283 zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
) ==
6284 ZFS_KEYSTATUS_UNAVAILABLE
) {
6288 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6289 "encryption key not loaded\n"), cmdname
, zfs_get_name(zhp
));
6294 * If this filesystem is inconsistent and has a receive resume
6295 * token, we can not mount it.
6297 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
6298 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
6299 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
6303 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6304 "Contains partially-completed state from "
6305 "\"zfs receive -s\", which can be resumed with "
6306 "\"zfs send -t\"\n"),
6307 cmdname
, zfs_get_name(zhp
));
6312 * At this point, we have verified that the mountpoint and/or
6313 * shareopts are appropriate for auto management. If the
6314 * filesystem is already mounted or shared, return (failing
6315 * for explicit requests); otherwise mount or share the
6321 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
6322 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
6324 if ((shared_nfs
&& shared_smb
) ||
6325 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
6326 strcmp(smbshareopts
, "off") == 0) ||
6327 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
6328 strcmp(shareopts
, "off") == 0)) {
6332 (void) fprintf(stderr
, gettext("cannot share "
6333 "'%s': filesystem already shared\n"),
6338 if (!zfs_is_mounted(zhp
, NULL
) &&
6339 zfs_mount(zhp
, NULL
, flags
) != 0)
6342 if (protocol
== NULL
) {
6343 if (zfs_shareall(zhp
) != 0)
6345 } else if (strcmp(protocol
, "nfs") == 0) {
6346 if (zfs_share_nfs(zhp
))
6348 } else if (strcmp(protocol
, "smb") == 0) {
6349 if (zfs_share_smb(zhp
))
6352 (void) fprintf(stderr
, gettext("cannot share "
6353 "'%s': invalid share type '%s' "
6355 zfs_get_name(zhp
), protocol
);
6362 if (options
== NULL
)
6363 mnt
.mnt_mntopts
= "";
6365 mnt
.mnt_mntopts
= (char *)options
;
6367 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
6368 zfs_is_mounted(zhp
, NULL
)) {
6372 (void) fprintf(stderr
, gettext("cannot mount "
6373 "'%s': filesystem already mounted\n"),
6378 if (zfs_mount(zhp
, options
, flags
) != 0)
6387 * Reports progress in the form "(current/total)". Not thread-safe.
6390 report_mount_progress(int current
, int total
)
6392 static time_t last_progress_time
= 0;
6393 time_t now
= time(NULL
);
6396 /* report 1..n instead of 0..n-1 */
6399 /* display header if we're here for the first time */
6401 set_progress_header(gettext("Mounting ZFS filesystems"));
6402 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
6403 /* too soon to report again */
6407 last_progress_time
= now
;
6409 (void) sprintf(info
, "(%d/%d)", current
, total
);
6411 if (current
== total
)
6412 finish_progress(info
);
6414 update_progress(info
);
6418 * zfs_foreach_mountpoint() callback that mounts or shares one filesystem and
6419 * updates the progress meter.
6422 share_mount_one_cb(zfs_handle_t
*zhp
, void *arg
)
6424 share_mount_state_t
*sms
= arg
;
6427 ret
= share_mount_one(zhp
, sms
->sm_op
, sms
->sm_flags
, sms
->sm_proto
,
6428 B_FALSE
, sms
->sm_options
);
6430 pthread_mutex_lock(&sms
->sm_lock
);
6432 sms
->sm_status
= ret
;
6434 if (sms
->sm_verbose
)
6435 report_mount_progress(sms
->sm_done
, sms
->sm_total
);
6436 pthread_mutex_unlock(&sms
->sm_lock
);
6441 append_options(char *mntopts
, char *newopts
)
6443 int len
= strlen(mntopts
);
6445 /* original length plus new string to append plus 1 for the comma */
6446 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
6447 (void) fprintf(stderr
, gettext("the opts argument for "
6448 "'%s' option is too long (more than %d chars)\n"),
6449 "-o", MNT_LINE_MAX
);
6454 mntopts
[len
++] = ',';
6456 (void) strcpy(&mntopts
[len
], newopts
);
6460 share_mount(int op
, int argc
, char **argv
)
6463 boolean_t verbose
= B_FALSE
;
6465 char *options
= NULL
;
6469 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":alvo:O" : "al"))
6482 if (*optarg
== '\0') {
6483 (void) fprintf(stderr
, gettext("empty mount "
6484 "options (-o) specified\n"));
6488 if (options
== NULL
)
6489 options
= safe_malloc(MNT_LINE_MAX
+ 1);
6491 /* option validation is done later */
6492 append_options(options
, optarg
);
6495 flags
|= MS_OVERLAY
;
6498 (void) fprintf(stderr
, gettext("missing argument for "
6499 "'%c' option\n"), optopt
);
6503 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6512 /* check number of arguments */
6514 char *protocol
= NULL
;
6516 if (op
== OP_SHARE
&& argc
> 0) {
6517 if (strcmp(argv
[0], "nfs") != 0 &&
6518 strcmp(argv
[0], "smb") != 0) {
6519 (void) fprintf(stderr
, gettext("share type "
6520 "must be 'nfs' or 'smb'\n"));
6529 (void) fprintf(stderr
, gettext("too many arguments\n"));
6533 start_progress_timer();
6534 get_all_cb_t cb
= { 0 };
6535 get_all_datasets(&cb
, verbose
);
6537 if (cb
.cb_used
== 0) {
6538 if (options
!= NULL
)
6543 share_mount_state_t share_mount_state
= { 0 };
6544 share_mount_state
.sm_op
= op
;
6545 share_mount_state
.sm_verbose
= verbose
;
6546 share_mount_state
.sm_flags
= flags
;
6547 share_mount_state
.sm_options
= options
;
6548 share_mount_state
.sm_proto
= protocol
;
6549 share_mount_state
.sm_total
= cb
.cb_used
;
6550 pthread_mutex_init(&share_mount_state
.sm_lock
, NULL
);
6553 * libshare isn't mt-safe, so only do the operation in parallel
6554 * if we're mounting.
6556 zfs_foreach_mountpoint(g_zfs
, cb
.cb_handles
, cb
.cb_used
,
6557 share_mount_one_cb
, &share_mount_state
, op
== OP_MOUNT
);
6558 ret
= share_mount_state
.sm_status
;
6560 for (int i
= 0; i
< cb
.cb_used
; i
++)
6561 zfs_close(cb
.cb_handles
[i
]);
6562 free(cb
.cb_handles
);
6563 } else if (argc
== 0) {
6564 struct mnttab entry
;
6566 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
6567 (void) fprintf(stderr
, gettext("missing filesystem "
6568 "argument (specify -a for all)\n"));
6573 * When mount is given no arguments, go through
6574 * /proc/self/mounts and display any active ZFS mounts.
6575 * We hide any snapshots, since they are controlled
6579 /* Reopen MNTTAB to prevent reading stale data from open file */
6580 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
) {
6581 if (options
!= NULL
)
6586 while (getmntent(mnttab_file
, &entry
) == 0) {
6587 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
6588 strchr(entry
.mnt_special
, '@') != NULL
)
6591 (void) printf("%-30s %s\n", entry
.mnt_special
,
6599 (void) fprintf(stderr
,
6600 gettext("too many arguments\n"));
6604 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6605 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6608 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
6614 if (options
!= NULL
)
6621 * zfs mount -a [nfs]
6622 * zfs mount filesystem
6624 * Mount all filesystems, or mount the given filesystem.
6627 zfs_do_mount(int argc
, char **argv
)
6629 return (share_mount(OP_MOUNT
, argc
, argv
));
6633 * zfs share -a [nfs | smb]
6634 * zfs share filesystem
6636 * Share all filesystems, or share the given filesystem.
6639 zfs_do_share(int argc
, char **argv
)
6641 return (share_mount(OP_SHARE
, argc
, argv
));
6644 typedef struct unshare_unmount_node
{
6645 zfs_handle_t
*un_zhp
;
6647 uu_avl_node_t un_avlnode
;
6648 } unshare_unmount_node_t
;
6652 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
6654 const unshare_unmount_node_t
*l
= larg
;
6655 const unshare_unmount_node_t
*r
= rarg
;
6657 return (strcmp(l
->un_mountp
, r
->un_mountp
));
6661 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6662 * absolute path, find the entry /proc/self/mounts, verify that its a
6663 * ZFS filesystems, and unmount it appropriately.
6666 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
6670 struct stat64 statbuf
;
6671 struct extmnttab entry
;
6672 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
6676 * Search for the path in /proc/self/mounts. Rather than looking for the
6677 * specific path, which can be fooled by non-standard paths (i.e. ".."
6678 * or "//"), we stat() the path and search for the corresponding
6679 * (major,minor) device pair.
6681 if (stat64(path
, &statbuf
) != 0) {
6682 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6683 cmdname
, path
, strerror(errno
));
6686 path_inode
= statbuf
.st_ino
;
6689 * Search for the given (major,minor) pair in the mount table.
6692 /* Reopen MNTTAB to prevent reading stale data from open file */
6693 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6696 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
6697 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
6698 entry
.mnt_minor
== minor(statbuf
.st_dev
))
6702 if (op
== OP_SHARE
) {
6703 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
6704 "currently mounted\n"), cmdname
, path
);
6707 (void) fprintf(stderr
, gettext("warning: %s not in"
6708 "/proc/self/mounts\n"), path
);
6709 if ((ret
= umount2(path
, flags
)) != 0)
6710 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
6715 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
6716 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
6717 "filesystem\n"), cmdname
, path
);
6721 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6722 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6726 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
6727 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6728 cmdname
, path
, strerror(errno
));
6730 } else if (statbuf
.st_ino
!= path_inode
) {
6731 (void) fprintf(stderr
, gettext("cannot "
6732 "%s '%s': not a mountpoint\n"), cmdname
, path
);
6736 if (op
== OP_SHARE
) {
6737 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6738 char smbshare_prop
[ZFS_MAXPROPLEN
];
6740 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
6741 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6742 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
6743 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6745 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6746 strcmp(smbshare_prop
, "off") == 0) {
6747 (void) fprintf(stderr
, gettext("cannot unshare "
6748 "'%s': legacy share\n"), path
);
6749 (void) fprintf(stderr
, gettext("use exportfs(8) "
6750 "or smbcontrol(1) to unshare this filesystem\n"));
6751 } else if (!zfs_is_shared(zhp
)) {
6752 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6753 "not currently shared\n"), path
);
6755 ret
= zfs_unshareall_bypath(zhp
, path
);
6758 char mtpt_prop
[ZFS_MAXPROPLEN
];
6760 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6761 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6764 ret
= zfs_unmount(zhp
, NULL
, flags
);
6765 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6766 (void) fprintf(stderr
, gettext("cannot unmount "
6767 "'%s': legacy mountpoint\n"),
6769 (void) fprintf(stderr
, gettext("use umount(8) "
6770 "to unmount this filesystem\n"));
6772 ret
= zfs_unmountall(zhp
, flags
);
6783 * Generic callback for unsharing or unmounting a filesystem.
6786 unshare_unmount(int op
, int argc
, char **argv
)
6793 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6794 char sharesmb
[ZFS_MAXPROPLEN
];
6797 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? ":a" : "af")) != -1) {
6806 (void) fprintf(stderr
, gettext("missing argument for "
6807 "'%c' option\n"), optopt
);
6811 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6822 * We could make use of zfs_for_each() to walk all datasets in
6823 * the system, but this would be very inefficient, especially
6824 * since we would have to linearly search /proc/self/mounts for
6825 * each one. Instead, do one pass through /proc/self/mounts
6826 * looking for zfs entries and call zfs_unmount() for each one.
6828 * Things get a little tricky if the administrator has created
6829 * mountpoints beneath other ZFS filesystems. In this case, we
6830 * have to unmount the deepest filesystems first. To accomplish
6831 * this, we place all the mountpoints in an AVL tree sorted by
6832 * the special type (dataset name), and walk the result in
6833 * reverse to make sure to get any snapshots first.
6835 struct mnttab entry
;
6836 uu_avl_pool_t
*pool
;
6837 uu_avl_t
*tree
= NULL
;
6838 unshare_unmount_node_t
*node
;
6840 uu_avl_walk_t
*walk
;
6841 char *protocol
= NULL
;
6843 if (op
== OP_SHARE
&& argc
> 0) {
6844 if (strcmp(argv
[0], "nfs") != 0 &&
6845 strcmp(argv
[0], "smb") != 0) {
6846 (void) fprintf(stderr
, gettext("share type "
6847 "must be 'nfs' or 'smb'\n"));
6856 (void) fprintf(stderr
, gettext("too many arguments\n"));
6860 if (((pool
= uu_avl_pool_create("unmount_pool",
6861 sizeof (unshare_unmount_node_t
),
6862 offsetof(unshare_unmount_node_t
, un_avlnode
),
6863 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6864 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6867 /* Reopen MNTTAB to prevent reading stale data from open file */
6868 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6871 while (getmntent(mnttab_file
, &entry
) == 0) {
6873 /* ignore non-ZFS entries */
6874 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6877 /* ignore snapshots */
6878 if (strchr(entry
.mnt_special
, '@') != NULL
)
6881 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6882 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6888 * Ignore datasets that are excluded/restricted by
6891 if (zpool_skip_pool(zfs_get_pool_name(zhp
))) {
6898 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6900 sizeof (nfs_mnt_prop
),
6901 NULL
, NULL
, 0, B_FALSE
) == 0);
6902 if (strcmp(nfs_mnt_prop
, "off") != 0)
6904 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6906 sizeof (nfs_mnt_prop
),
6907 NULL
, NULL
, 0, B_FALSE
) == 0);
6908 if (strcmp(nfs_mnt_prop
, "off") == 0)
6912 /* Ignore legacy mounts */
6913 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6915 sizeof (nfs_mnt_prop
),
6916 NULL
, NULL
, 0, B_FALSE
) == 0);
6917 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6919 /* Ignore canmount=noauto mounts */
6920 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6921 ZFS_CANMOUNT_NOAUTO
)
6927 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6929 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6931 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6933 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6934 uu_avl_insert(tree
, node
, idx
);
6936 zfs_close(node
->un_zhp
);
6937 free(node
->un_mountp
);
6943 * Walk the AVL tree in reverse, unmounting each filesystem and
6944 * removing it from the AVL tree in the process.
6946 if ((walk
= uu_avl_walk_start(tree
,
6947 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6950 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6951 uu_avl_remove(tree
, node
);
6955 if (zfs_unshareall_bytype(node
->un_zhp
,
6956 node
->un_mountp
, protocol
) != 0)
6961 if (zfs_unmount(node
->un_zhp
,
6962 node
->un_zhp
->zfs_name
, flags
) != 0)
6967 zfs_close(node
->un_zhp
);
6968 free(node
->un_mountp
);
6972 uu_avl_walk_end(walk
);
6973 uu_avl_destroy(tree
);
6974 uu_avl_pool_destroy(pool
);
6979 (void) fprintf(stderr
,
6980 gettext("missing filesystem argument\n"));
6982 (void) fprintf(stderr
,
6983 gettext("too many arguments\n"));
6988 * We have an argument, but it may be a full path or a ZFS
6989 * filesystem. Pass full paths off to unmount_path() (shared by
6990 * manual_unmount), otherwise open the filesystem and pass to
6993 if (argv
[0][0] == '/')
6994 return (unshare_unmount_path(op
, argv
[0],
6997 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6998 ZFS_TYPE_FILESYSTEM
)) == NULL
)
7001 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
7002 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
7003 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
7004 NULL
, 0, B_FALSE
) == 0);
7008 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
7010 sizeof (nfs_mnt_prop
),
7011 NULL
, NULL
, 0, B_FALSE
) == 0);
7012 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
7013 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
7016 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
7017 strcmp(sharesmb
, "off") == 0) {
7018 (void) fprintf(stderr
, gettext("cannot "
7019 "unshare '%s': legacy share\n"),
7021 (void) fprintf(stderr
, gettext("use "
7022 "unshare(1M) to unshare this "
7025 } else if (!zfs_is_shared(zhp
)) {
7026 (void) fprintf(stderr
, gettext("cannot "
7027 "unshare '%s': not currently "
7028 "shared\n"), zfs_get_name(zhp
));
7030 } else if (zfs_unshareall(zhp
) != 0) {
7036 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
7037 (void) fprintf(stderr
, gettext("cannot "
7038 "unmount '%s': legacy "
7039 "mountpoint\n"), zfs_get_name(zhp
));
7040 (void) fprintf(stderr
, gettext("use "
7041 "umount(1M) to unmount this "
7044 } else if (!zfs_is_mounted(zhp
, NULL
)) {
7045 (void) fprintf(stderr
, gettext("cannot "
7046 "unmount '%s': not currently "
7050 } else if (zfs_unmountall(zhp
, flags
) != 0) {
7064 * zfs unmount filesystem
7066 * Unmount all filesystems, or a specific ZFS filesystem.
7069 zfs_do_unmount(int argc
, char **argv
)
7071 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
7076 * zfs unshare filesystem
7078 * Unshare all filesystems, or a specific ZFS filesystem.
7081 zfs_do_unshare(int argc
, char **argv
)
7083 return (unshare_unmount(OP_SHARE
, argc
, argv
));
7087 disable_command_idx(char *command
)
7089 for (int i
= 0; i
< NCOMMAND
; i
++) {
7090 if (command_table
[i
].name
== NULL
)
7093 if (strcmp(command
, command_table
[i
].name
) == 0) {
7094 command_table
[i
].name
= NULL
;
7102 find_command_idx(char *command
, int *idx
)
7106 for (i
= 0; i
< NCOMMAND
; i
++) {
7107 if (command_table
[i
].name
== NULL
)
7110 if (strcmp(command
, command_table
[i
].name
) == 0) {
7119 zfs_do_diff(int argc
, char **argv
)
7123 char *tosnap
= NULL
;
7124 char *fromsnap
= NULL
;
7129 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
7132 flags
|= ZFS_DIFF_CLASSIFY
;
7135 flags
|= ZFS_DIFF_PARSEABLE
;
7138 flags
|= ZFS_DIFF_TIMESTAMP
;
7141 (void) fprintf(stderr
,
7142 gettext("invalid option '%c'\n"), optopt
);
7151 (void) fprintf(stderr
,
7152 gettext("must provide at least one snapshot name\n"));
7157 (void) fprintf(stderr
, gettext("too many arguments\n"));
7162 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
7165 if (*fromsnap
!= '@')
7166 copy
= strdup(fromsnap
);
7168 copy
= strdup(tosnap
);
7172 if ((atp
= strchr(copy
, '@')) != NULL
)
7175 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
) {
7182 * Ignore SIGPIPE so that the library can give us
7183 * information on any failure
7185 (void) sigignore(SIGPIPE
);
7187 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
7196 * zfs remap <filesystem | volume>
7198 * N.B. The remap command has been disabled and may be removed in the future.
7200 * Remap the indirect blocks in the given filesystem or volume so that they no
7201 * longer reference blocks on previously removed vdevs and we can eventually
7202 * shrink the size of the indirect mapping objects for the previously removed
7203 * vdevs. Note that remapping all blocks might not be possible and that
7204 * references from snapshots will still exist and cannot be remapped.
7206 * This functionality is no longer particularly useful now that the removal
7207 * code can map large chunks. Furthermore, explaining what this command
7208 * does and why it may be useful requires a detailed understanding of the
7209 * internals of device removal. These are details users should not be
7210 * bothered with. If required, the remap command can be re-enabled by
7211 * setting the ZFS_REMAP_ENABLED environment variable.
7213 * > ZFS_REMAP_ENABLED=yes zfs remap <filesystem | volume>
7216 zfs_do_remap(int argc
, char **argv
)
7223 while ((c
= getopt(argc
, argv
, "")) != -1) {
7226 (void) fprintf(stderr
,
7227 gettext("invalid option '%c'\n"), optopt
);
7233 (void) fprintf(stderr
, gettext("wrong number of arguments\n"));
7238 err
= zfs_remap_indirects(g_zfs
, fsname
);
7244 * zfs bookmark <fs@snap> <fs#bmark>
7246 * Creates a bookmark with the given name from the given snapshot.
7249 zfs_do_bookmark(int argc
, char **argv
)
7251 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
7252 char bookname
[ZFS_MAX_DATASET_NAME_LEN
];
7259 while ((c
= getopt(argc
, argv
, "")) != -1) {
7262 (void) fprintf(stderr
,
7263 gettext("invalid option '%c'\n"), optopt
);
7271 /* check number of arguments */
7273 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
7277 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
7281 if (strchr(argv
[0], '@') == NULL
) {
7282 (void) fprintf(stderr
,
7283 gettext("invalid snapshot name '%s': "
7284 "must contain a '@'\n"), argv
[0]);
7287 if (strchr(argv
[1], '#') == NULL
) {
7288 (void) fprintf(stderr
,
7289 gettext("invalid bookmark name '%s': "
7290 "must contain a '#'\n"), argv
[1]);
7294 if (argv
[0][0] == '@') {
7296 * Snapshot name begins with @.
7297 * Default to same fs as bookmark.
7299 (void) strlcpy(snapname
, argv
[1], sizeof (snapname
));
7300 *strchr(snapname
, '#') = '\0';
7301 (void) strlcat(snapname
, argv
[0], sizeof (snapname
));
7303 (void) strlcpy(snapname
, argv
[0], sizeof (snapname
));
7305 if (argv
[1][0] == '#') {
7307 * Bookmark name begins with #.
7308 * Default to same fs as snapshot.
7310 (void) strlcpy(bookname
, argv
[0], sizeof (bookname
));
7311 *strchr(bookname
, '@') = '\0';
7312 (void) strlcat(bookname
, argv
[1], sizeof (bookname
));
7314 (void) strlcpy(bookname
, argv
[1], sizeof (bookname
));
7317 zhp
= zfs_open(g_zfs
, snapname
, ZFS_TYPE_SNAPSHOT
);
7323 nvl
= fnvlist_alloc();
7324 fnvlist_add_string(nvl
, bookname
, snapname
);
7325 ret
= lzc_bookmark(nvl
, NULL
);
7329 const char *err_msg
= NULL
;
7332 (void) snprintf(errbuf
, sizeof (errbuf
),
7333 dgettext(TEXT_DOMAIN
,
7334 "cannot create bookmark '%s'"), bookname
);
7338 err_msg
= "bookmark is in a different pool";
7341 err_msg
= "bookmark exists";
7344 err_msg
= "invalid argument";
7347 err_msg
= "bookmark feature not enabled";
7350 err_msg
= "out of space";
7353 err_msg
= "dataset does not exist";
7356 (void) zfs_standard_error(g_zfs
, ret
, errbuf
);
7359 if (err_msg
!= NULL
) {
7360 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
7361 dgettext(TEXT_DOMAIN
, err_msg
));
7373 zfs_do_channel_program(int argc
, char **argv
)
7376 char *progbuf
, *filename
, *poolname
;
7377 size_t progsize
, progread
;
7378 nvlist_t
*outnvl
= NULL
;
7379 uint64_t instrlimit
= ZCP_DEFAULT_INSTRLIMIT
;
7380 uint64_t memlimit
= ZCP_DEFAULT_MEMLIMIT
;
7381 boolean_t sync_flag
= B_TRUE
, json_output
= B_FALSE
;
7382 zpool_handle_t
*zhp
;
7385 while ((c
= getopt(argc
, argv
, "nt:m:j")) != -1) {
7393 arg
= strtoull(optarg
, &endp
, 0);
7394 if (errno
!= 0 || *endp
!= '\0') {
7395 (void) fprintf(stderr
, gettext(
7397 "'%s': expected integer\n"), optarg
);
7404 ASSERT3U(c
, ==, 'm');
7410 sync_flag
= B_FALSE
;
7414 json_output
= B_TRUE
;
7418 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7428 (void) fprintf(stderr
,
7429 gettext("invalid number of arguments\n"));
7435 if (strcmp(filename
, "-") == 0) {
7437 filename
= "standard input";
7438 } else if ((fd
= open(filename
, O_RDONLY
)) < 0) {
7439 (void) fprintf(stderr
, gettext("cannot open '%s': %s\n"),
7440 filename
, strerror(errno
));
7444 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
7445 (void) fprintf(stderr
, gettext("cannot open pool '%s'"),
7454 * Read in the channel program, expanding the program buffer as
7459 progbuf
= safe_malloc(progsize
);
7461 ret
= read(fd
, progbuf
+ progread
, progsize
- progread
);
7463 if (progread
== progsize
&& ret
> 0) {
7465 progbuf
= safe_realloc(progbuf
, progsize
);
7473 (void) fprintf(stderr
,
7474 gettext("cannot read '%s': %s\n"),
7475 filename
, strerror(errno
));
7478 progbuf
[progread
] = '\0';
7481 * Any remaining arguments are passed as arguments to the lua script as
7484 * "argv" -> [ "arg 1", ... "arg n" ],
7487 nvlist_t
*argnvl
= fnvlist_alloc();
7488 fnvlist_add_string_array(argnvl
, ZCP_ARG_CLIARGV
, argv
+ 2, argc
- 2);
7491 ret
= lzc_channel_program(poolname
, progbuf
,
7492 instrlimit
, memlimit
, argnvl
, &outnvl
);
7494 ret
= lzc_channel_program_nosync(poolname
, progbuf
,
7495 instrlimit
, memlimit
, argnvl
, &outnvl
);
7500 * On error, report the error message handed back by lua if one
7501 * exists. Otherwise, generate an appropriate error message,
7502 * falling back on strerror() for an unexpected return code.
7504 char *errstring
= NULL
;
7505 const char *msg
= gettext("Channel program execution failed");
7506 uint64_t instructions
= 0;
7507 if (outnvl
!= NULL
&& nvlist_exists(outnvl
, ZCP_RET_ERROR
)) {
7508 (void) nvlist_lookup_string(outnvl
,
7509 ZCP_RET_ERROR
, &errstring
);
7510 if (errstring
== NULL
)
7511 errstring
= strerror(ret
);
7513 (void) nvlist_lookup_uint64(outnvl
,
7514 ZCP_ARG_INSTRLIMIT
, &instructions
);
7520 "Invalid instruction or memory limit.";
7523 errstring
= "Return value too large.";
7526 errstring
= "Memory limit exhausted.";
7529 errstring
= "Timed out.";
7532 errstring
= "Permission denied. Channel "
7533 "programs must be run as root.";
7536 (void) zfs_standard_error(g_zfs
, ret
, msg
);
7539 if (errstring
!= NULL
)
7540 (void) fprintf(stderr
, "%s:\n%s\n", msg
, errstring
);
7542 if (ret
== ETIME
&& instructions
!= 0)
7543 (void) fprintf(stderr
,
7544 gettext("%llu Lua instructions\n"),
7545 (u_longlong_t
)instructions
);
7548 (void) nvlist_print_json(stdout
, outnvl
);
7549 } else if (nvlist_empty(outnvl
)) {
7550 (void) fprintf(stdout
, gettext("Channel program fully "
7551 "executed and did not produce output.\n"));
7553 (void) fprintf(stdout
, gettext("Channel program fully "
7554 "executed and produced output:\n"));
7555 dump_nvlist(outnvl
, 4);
7560 fnvlist_free(outnvl
);
7561 fnvlist_free(argnvl
);
7570 typedef struct loadkey_cbdata
{
7571 boolean_t cb_loadkey
;
7572 boolean_t cb_recursive
;
7574 char *cb_keylocation
;
7575 uint64_t cb_numfailed
;
7576 uint64_t cb_numattempted
;
7580 load_key_callback(zfs_handle_t
*zhp
, void *data
)
7583 boolean_t is_encroot
;
7584 loadkey_cbdata_t
*cb
= data
;
7585 uint64_t keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
7588 * If we are working recursively, we want to skip loading / unloading
7589 * keys for non-encryption roots and datasets whose keys are already
7590 * in the desired end-state.
7592 if (cb
->cb_recursive
) {
7593 ret
= zfs_crypto_get_encryption_root(zhp
, &is_encroot
, NULL
);
7599 if ((cb
->cb_loadkey
&& keystatus
== ZFS_KEYSTATUS_AVAILABLE
) ||
7600 (!cb
->cb_loadkey
&& keystatus
== ZFS_KEYSTATUS_UNAVAILABLE
))
7604 cb
->cb_numattempted
++;
7607 ret
= zfs_crypto_load_key(zhp
, cb
->cb_noop
, cb
->cb_keylocation
);
7609 ret
= zfs_crypto_unload_key(zhp
);
7620 load_unload_keys(int argc
, char **argv
, boolean_t loadkey
)
7622 int c
, ret
= 0, flags
= 0;
7623 boolean_t do_all
= B_FALSE
;
7624 loadkey_cbdata_t cb
= { 0 };
7626 cb
.cb_loadkey
= loadkey
;
7628 while ((c
= getopt(argc
, argv
, "anrL:")) != -1) {
7629 /* noop and alternate keylocations only apply to zfs load-key */
7633 cb
.cb_noop
= B_TRUE
;
7636 cb
.cb_keylocation
= optarg
;
7646 cb
.cb_recursive
= B_TRUE
;
7649 flags
|= ZFS_ITER_RECURSE
;
7650 cb
.cb_recursive
= B_TRUE
;
7653 (void) fprintf(stderr
,
7654 gettext("invalid option '%c'\n"), optopt
);
7662 if (!do_all
&& argc
== 0) {
7663 (void) fprintf(stderr
,
7664 gettext("Missing dataset argument or -a option\n"));
7668 if (do_all
&& argc
!= 0) {
7669 (void) fprintf(stderr
,
7670 gettext("Cannot specify dataset with -a option\n"));
7674 if (cb
.cb_recursive
&& cb
.cb_keylocation
!= NULL
&&
7675 strcmp(cb
.cb_keylocation
, "prompt") != 0) {
7676 (void) fprintf(stderr
, gettext("alternate keylocation may only "
7677 "be 'prompt' with -r or -a\n"));
7681 ret
= zfs_for_each(argc
, argv
, flags
,
7682 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
, NULL
, NULL
, 0,
7683 load_key_callback
, &cb
);
7685 if (cb
.cb_noop
|| (cb
.cb_recursive
&& cb
.cb_numattempted
!= 0)) {
7686 (void) printf(gettext("%llu / %llu key(s) successfully %s\n"),
7687 (u_longlong_t
)(cb
.cb_numattempted
- cb
.cb_numfailed
),
7688 (u_longlong_t
)cb
.cb_numattempted
,
7689 loadkey
? (cb
.cb_noop
? "verified" : "loaded") :
7693 if (cb
.cb_numfailed
!= 0)
7700 zfs_do_load_key(int argc
, char **argv
)
7702 return (load_unload_keys(argc
, argv
, B_TRUE
));
7707 zfs_do_unload_key(int argc
, char **argv
)
7709 return (load_unload_keys(argc
, argv
, B_FALSE
));
7713 zfs_do_change_key(int argc
, char **argv
)
7717 boolean_t loadkey
= B_FALSE
, inheritkey
= B_FALSE
;
7718 zfs_handle_t
*zhp
= NULL
;
7719 nvlist_t
*props
= fnvlist_alloc();
7721 while ((c
= getopt(argc
, argv
, "lio:")) != -1) {
7727 inheritkey
= B_TRUE
;
7730 if (!parseprop(props
, optarg
)) {
7736 (void) fprintf(stderr
,
7737 gettext("invalid option '%c'\n"), optopt
);
7742 if (inheritkey
&& !nvlist_empty(props
)) {
7743 (void) fprintf(stderr
,
7744 gettext("Properties not allowed for inheriting\n"));
7752 (void) fprintf(stderr
, gettext("Missing dataset argument\n"));
7757 (void) fprintf(stderr
, gettext("Too many arguments\n"));
7761 zhp
= zfs_open(g_zfs
, argv
[argc
- 1],
7762 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
7767 keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
7768 if (keystatus
!= ZFS_KEYSTATUS_AVAILABLE
) {
7769 ret
= zfs_crypto_load_key(zhp
, B_FALSE
, NULL
);
7777 /* refresh the properties so the new keystatus is visible */
7778 zfs_refresh_properties(zhp
);
7781 ret
= zfs_crypto_rewrap(zhp
, props
, inheritkey
);
7794 * 1) zfs project [-d|-r] <file|directory ...>
7795 * List project ID and inherit flag of file(s) or directories.
7796 * -d: List the directory itself, not its children.
7797 * -r: List subdirectories recursively.
7799 * 2) zfs project -C [-k] [-r] <file|directory ...>
7800 * Clear project inherit flag and/or ID on the file(s) or directories.
7801 * -k: Keep the project ID unchanged. If not specified, the project ID
7802 * will be reset as zero.
7803 * -r: Clear on subdirectories recursively.
7805 * 3) zfs project -c [-0] [-d|-r] [-p id] <file|directory ...>
7806 * Check project ID and inherit flag on the file(s) or directories,
7807 * report the outliers.
7808 * -0: Print file name followed by a NUL instead of newline.
7809 * -d: Check the directory itself, not its children.
7810 * -p: Specify the referenced ID for comparing with the target file(s)
7811 * or directories' project IDs. If not specified, the target (top)
7812 * directory's project ID will be used as the referenced one.
7813 * -r: Check subdirectories recursively.
7815 * 4) zfs project [-p id] [-r] [-s] <file|directory ...>
7816 * Set project ID and/or inherit flag on the file(s) or directories.
7817 * -p: Set the project ID as the given id.
7818 * -r: Set on subdirectorie recursively. If not specify "-p" option,
7819 * it will use top-level directory's project ID as the given id,
7820 * then set both project ID and inherit flag on all descendants
7821 * of the top-level directory.
7822 * -s: Set project inherit flag.
7825 zfs_do_project(int argc
, char **argv
)
7827 zfs_project_control_t zpc
= {
7828 .zpc_expected_projid
= ZFS_INVALID_PROJID
,
7829 .zpc_op
= ZFS_PROJECT_OP_DEFAULT
,
7830 .zpc_dironly
= B_FALSE
,
7831 .zpc_keep_projid
= B_FALSE
,
7832 .zpc_newline
= B_TRUE
,
7833 .zpc_recursive
= B_FALSE
,
7834 .zpc_set_flag
= B_FALSE
,
7841 while ((c
= getopt(argc
, argv
, "0Ccdkp:rs")) != -1) {
7844 zpc
.zpc_newline
= B_FALSE
;
7847 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
7848 (void) fprintf(stderr
, gettext("cannot "
7849 "specify '-C' '-c' '-s' together\n"));
7853 zpc
.zpc_op
= ZFS_PROJECT_OP_CLEAR
;
7856 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
7857 (void) fprintf(stderr
, gettext("cannot "
7858 "specify '-C' '-c' '-s' together\n"));
7862 zpc
.zpc_op
= ZFS_PROJECT_OP_CHECK
;
7865 zpc
.zpc_dironly
= B_TRUE
;
7866 /* overwrite "-r" option */
7867 zpc
.zpc_recursive
= B_FALSE
;
7870 zpc
.zpc_keep_projid
= B_TRUE
;
7876 zpc
.zpc_expected_projid
= strtoull(optarg
, &endptr
, 0);
7877 if (errno
!= 0 || *endptr
!= '\0') {
7878 (void) fprintf(stderr
,
7879 gettext("project ID must be less than "
7880 "%u\n"), UINT32_MAX
);
7883 if (zpc
.zpc_expected_projid
>= UINT32_MAX
) {
7884 (void) fprintf(stderr
,
7885 gettext("invalid project ID\n"));
7891 zpc
.zpc_recursive
= B_TRUE
;
7892 /* overwrite "-d" option */
7893 zpc
.zpc_dironly
= B_FALSE
;
7896 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
7897 (void) fprintf(stderr
, gettext("cannot "
7898 "specify '-C' '-c' '-s' together\n"));
7902 zpc
.zpc_set_flag
= B_TRUE
;
7903 zpc
.zpc_op
= ZFS_PROJECT_OP_SET
;
7906 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7912 if (zpc
.zpc_op
== ZFS_PROJECT_OP_DEFAULT
) {
7913 if (zpc
.zpc_expected_projid
!= ZFS_INVALID_PROJID
)
7914 zpc
.zpc_op
= ZFS_PROJECT_OP_SET
;
7916 zpc
.zpc_op
= ZFS_PROJECT_OP_LIST
;
7919 switch (zpc
.zpc_op
) {
7920 case ZFS_PROJECT_OP_LIST
:
7921 if (zpc
.zpc_keep_projid
) {
7922 (void) fprintf(stderr
,
7923 gettext("'-k' is only valid together with '-C'\n"));
7926 if (!zpc
.zpc_newline
) {
7927 (void) fprintf(stderr
,
7928 gettext("'-0' is only valid together with '-c'\n"));
7932 case ZFS_PROJECT_OP_CHECK
:
7933 if (zpc
.zpc_keep_projid
) {
7934 (void) fprintf(stderr
,
7935 gettext("'-k' is only valid together with '-C'\n"));
7939 case ZFS_PROJECT_OP_CLEAR
:
7940 if (zpc
.zpc_dironly
) {
7941 (void) fprintf(stderr
,
7942 gettext("'-d' is useless together with '-C'\n"));
7945 if (!zpc
.zpc_newline
) {
7946 (void) fprintf(stderr
,
7947 gettext("'-0' is only valid together with '-c'\n"));
7950 if (zpc
.zpc_expected_projid
!= ZFS_INVALID_PROJID
) {
7951 (void) fprintf(stderr
,
7952 gettext("'-p' is useless together with '-C'\n"));
7956 case ZFS_PROJECT_OP_SET
:
7957 if (zpc
.zpc_dironly
) {
7958 (void) fprintf(stderr
,
7959 gettext("'-d' is useless for set project ID and/or "
7963 if (zpc
.zpc_keep_projid
) {
7964 (void) fprintf(stderr
,
7965 gettext("'-k' is only valid together with '-C'\n"));
7968 if (!zpc
.zpc_newline
) {
7969 (void) fprintf(stderr
,
7970 gettext("'-0' is only valid together with '-c'\n"));
7982 (void) fprintf(stderr
,
7983 gettext("missing file or directory target(s)\n"));
7987 for (int i
= 0; i
< argc
; i
++) {
7990 err
= zfs_project_handle(argv
[i
], &zpc
);
7999 main(int argc
, char **argv
)
8006 (void) setlocale(LC_ALL
, "");
8007 (void) textdomain(TEXT_DOMAIN
);
8012 * Make sure the user has specified some command.
8015 (void) fprintf(stderr
, gettext("missing command\n"));
8022 * The 'umount' command is an alias for 'unmount'
8024 if (strcmp(cmdname
, "umount") == 0)
8025 cmdname
= "unmount";
8028 * The 'recv' command is an alias for 'receive'
8030 if (strcmp(cmdname
, "recv") == 0)
8031 cmdname
= "receive";
8034 * The 'snap' command is an alias for 'snapshot'
8036 if (strcmp(cmdname
, "snap") == 0)
8037 cmdname
= "snapshot";
8040 * The 'remap' command has been disabled and may be removed in the
8041 * future. See the comment above zfs_do_remap() for details.
8043 if (!libzfs_envvar_is_set("ZFS_REMAP_ENABLED"))
8044 disable_command_idx("remap");
8049 if ((strcmp(cmdname
, "-?") == 0) ||
8050 (strcmp(cmdname
, "--help") == 0))
8053 if ((g_zfs
= libzfs_init()) == NULL
) {
8054 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
8058 mnttab_file
= g_zfs
->libzfs_mnttab
;
8060 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
8062 libzfs_print_on_error(g_zfs
, B_TRUE
);
8065 * Many commands modify input strings for string parsing reasons.
8066 * We create a copy to protect the original argv.
8068 newargv
= malloc((argc
+ 1) * sizeof (newargv
[0]));
8069 for (i
= 0; i
< argc
; i
++)
8070 newargv
[i
] = strdup(argv
[i
]);
8071 newargv
[argc
] = NULL
;
8074 * Run the appropriate command.
8076 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
8077 if (find_command_idx(cmdname
, &i
) == 0) {
8078 current_command
= &command_table
[i
];
8079 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
8080 } else if (strchr(cmdname
, '=') != NULL
) {
8081 verify(find_command_idx("set", &i
) == 0);
8082 current_command
= &command_table
[i
];
8083 ret
= command_table
[i
].func(argc
, newargv
);
8085 (void) fprintf(stderr
, gettext("unrecognized "
8086 "command '%s'\n"), cmdname
);
8091 for (i
= 0; i
< argc
; i
++)
8095 if (ret
== 0 && log_history
)
8096 (void) zpool_log_history(g_zfs
, history_str
);
8101 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8102 * for the purposes of running ::findleaks.
8104 if (getenv("ZFS_ABORT") != NULL
) {
8105 (void) printf("dumping core by request\n");