4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
29 * Copyright 2016 Nexenta Systems, Inc.
30 * Copyright (c) 2018 Datto Inc.
40 #include <libnvpair.h>
52 #include <sys/debug.h>
54 #include <sys/mkdev.h>
55 #include <sys/mntent.h>
56 #include <sys/mnttab.h>
57 #include <sys/mount.h>
59 #include <sys/fs/zfs.h>
60 #include <sys/systeminfo.h>
61 #include <sys/types.h>
63 #include <sys/zfs_project.h>
66 #include <libzfs_core.h>
68 #include <zfs_deleg.h>
72 #include <directory.h>
73 #endif /* HAVE_IDMAP */
77 #include "zfs_comutil.h"
78 #include "libzfs_impl.h"
79 #include "zfs_projectutil.h"
81 libzfs_handle_t
*g_zfs
;
83 static FILE *mnttab_file
;
84 static char history_str
[HIS_MAX_RECORD_LEN
];
85 static boolean_t log_history
= B_TRUE
;
87 static int zfs_do_clone(int argc
, char **argv
);
88 static int zfs_do_create(int argc
, char **argv
);
89 static int zfs_do_destroy(int argc
, char **argv
);
90 static int zfs_do_get(int argc
, char **argv
);
91 static int zfs_do_inherit(int argc
, char **argv
);
92 static int zfs_do_list(int argc
, char **argv
);
93 static int zfs_do_mount(int argc
, char **argv
);
94 static int zfs_do_rename(int argc
, char **argv
);
95 static int zfs_do_rollback(int argc
, char **argv
);
96 static int zfs_do_set(int argc
, char **argv
);
97 static int zfs_do_upgrade(int argc
, char **argv
);
98 static int zfs_do_snapshot(int argc
, char **argv
);
99 static int zfs_do_unmount(int argc
, char **argv
);
100 static int zfs_do_share(int argc
, char **argv
);
101 static int zfs_do_unshare(int argc
, char **argv
);
102 static int zfs_do_send(int argc
, char **argv
);
103 static int zfs_do_receive(int argc
, char **argv
);
104 static int zfs_do_promote(int argc
, char **argv
);
105 static int zfs_do_userspace(int argc
, char **argv
);
106 static int zfs_do_allow(int argc
, char **argv
);
107 static int zfs_do_unallow(int argc
, char **argv
);
108 static int zfs_do_hold(int argc
, char **argv
);
109 static int zfs_do_holds(int argc
, char **argv
);
110 static int zfs_do_release(int argc
, char **argv
);
111 static int zfs_do_diff(int argc
, char **argv
);
112 static int zfs_do_bookmark(int argc
, char **argv
);
113 static int zfs_do_channel_program(int argc
, char **argv
);
114 static int zfs_do_remap(int argc
, char **argv
);
115 static int zfs_do_load_key(int argc
, char **argv
);
116 static int zfs_do_unload_key(int argc
, char **argv
);
117 static int zfs_do_change_key(int argc
, char **argv
);
118 static int zfs_do_project(int argc
, char **argv
);
121 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
126 _umem_debug_init(void)
128 return ("default,verbose"); /* $UMEM_DEBUG setting */
132 _umem_logging_init(void)
134 return ("fail,contents"); /* $UMEM_LOGGING setting */
169 HELP_CHANNEL_PROGRAM
,
175 typedef struct zfs_command
{
177 int (*func
)(int argc
, char **argv
);
182 * Master command table. Each ZFS command has a name, associated function, and
183 * usage message. The usage messages need to be internationalized, so we have
184 * to have a function to return the usage message based on a command index.
186 * These commands are organized according to how they are displayed in the usage
187 * message. An empty command (one with a NULL name) indicates an empty line in
188 * the generic usage message.
190 static zfs_command_t command_table
[] = {
191 { "create", zfs_do_create
, HELP_CREATE
},
192 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
194 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
195 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
196 { "clone", zfs_do_clone
, HELP_CLONE
},
197 { "promote", zfs_do_promote
, HELP_PROMOTE
},
198 { "rename", zfs_do_rename
, HELP_RENAME
},
199 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
200 { "program", zfs_do_channel_program
, HELP_CHANNEL_PROGRAM
},
202 { "list", zfs_do_list
, HELP_LIST
},
204 { "set", zfs_do_set
, HELP_SET
},
205 { "get", zfs_do_get
, HELP_GET
},
206 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
207 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
209 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
210 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
211 { "projectspace", zfs_do_userspace
, HELP_PROJECTSPACE
},
213 { "project", zfs_do_project
, HELP_PROJECT
},
215 { "mount", zfs_do_mount
, HELP_MOUNT
},
216 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
217 { "share", zfs_do_share
, HELP_SHARE
},
218 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
220 { "send", zfs_do_send
, HELP_SEND
},
221 { "receive", zfs_do_receive
, HELP_RECEIVE
},
223 { "allow", zfs_do_allow
, HELP_ALLOW
},
225 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
227 { "hold", zfs_do_hold
, HELP_HOLD
},
228 { "holds", zfs_do_holds
, HELP_HOLDS
},
229 { "release", zfs_do_release
, HELP_RELEASE
},
230 { "diff", zfs_do_diff
, HELP_DIFF
},
231 { "remap", zfs_do_remap
, HELP_REMAP
},
232 { "load-key", zfs_do_load_key
, HELP_LOAD_KEY
},
233 { "unload-key", zfs_do_unload_key
, HELP_UNLOAD_KEY
},
234 { "change-key", zfs_do_change_key
, HELP_CHANGE_KEY
},
237 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
239 zfs_command_t
*current_command
;
242 get_usage(zfs_help_t idx
)
246 return (gettext("\tclone [-p] [-o property=value] ... "
247 "<snapshot> <filesystem|volume>\n"));
249 return (gettext("\tcreate [-p] [-o property=value] ... "
251 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
252 "-V <size> <volume>\n"));
254 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
255 "\tdestroy [-dnpRrv] "
256 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
257 "\tdestroy <filesystem|volume>#<bookmark>\n"));
259 return (gettext("\tget [-rHp] [-d max] "
260 "[-o \"all\" | field[,...]]\n"
261 "\t [-t type[,...]] [-s source[,...]]\n"
262 "\t <\"all\" | property[,...]> "
263 "[filesystem|volume|snapshot|bookmark] ...\n"));
265 return (gettext("\tinherit [-rS] <property> "
266 "<filesystem|volume|snapshot> ...\n"));
268 return (gettext("\tupgrade [-v]\n"
269 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
271 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
272 "[-s property]...\n\t [-S property]... [-t type[,...]] "
273 "[filesystem|volume|snapshot] ...\n"));
275 return (gettext("\tmount\n"
276 "\tmount [-lvO] [-o opts] <-a | filesystem>\n"));
278 return (gettext("\tpromote <clone-filesystem>\n"));
280 return (gettext("\treceive [-vnsFu] "
281 "[-o <property>=<value>] ... [-x <property>] ...\n"
282 "\t <filesystem|volume|snapshot>\n"
283 "\treceive [-vnsFu] [-o <property>=<value>] ... "
284 "[-x <property>] ... \n"
285 "\t [-d | -e] <filesystem>\n"
286 "\treceive -A <filesystem|volume>\n"));
288 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
289 "<filesystem|volume|snapshot>\n"
290 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
291 "\trename -r <snapshot> <snapshot>\n"));
293 return (gettext("\trollback [-rRf] <snapshot>\n"));
295 return (gettext("\tsend [-DnPpRvLecwb] [-[i|I] snapshot] "
297 "\tsend [-nvPLecw] [-i snapshot|bookmark] "
298 "<filesystem|volume|snapshot>\n"
299 "\tsend [-nvPe] -t <receive_resume_token>\n"));
301 return (gettext("\tset <property=value> ... "
302 "<filesystem|volume|snapshot> ...\n"));
304 return (gettext("\tshare [-l] <-a [nfs|smb] | filesystem>\n"));
306 return (gettext("\tsnapshot [-r] [-o property=value] ... "
307 "<filesystem|volume>@<snap> ...\n"));
309 return (gettext("\tunmount [-f] "
310 "<-a | filesystem|mountpoint>\n"));
312 return (gettext("\tunshare "
313 "<-a [nfs|smb] | filesystem|mountpoint>\n"));
315 return (gettext("\tallow <filesystem|volume>\n"
317 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
318 "\t <filesystem|volume>\n"
319 "\tallow [-ld] -e <perm|@setname>[,...] "
320 "<filesystem|volume>\n"
321 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
322 "\tallow -s @setname <perm|@setname>[,...] "
323 "<filesystem|volume>\n"));
325 return (gettext("\tunallow [-rldug] "
326 "<\"everyone\"|user|group>[,...]\n"
327 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
328 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
329 "<filesystem|volume>\n"
330 "\tunallow [-r] -c [<perm|@setname>[,...]] "
331 "<filesystem|volume>\n"
332 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
333 "<filesystem|volume>\n"));
335 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
337 "\t [-S field] ... [-t type[,...]] "
338 "<filesystem|snapshot>\n"));
339 case HELP_GROUPSPACE
:
340 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
342 "\t [-S field] ... [-t type[,...]] "
343 "<filesystem|snapshot>\n"));
344 case HELP_PROJECTSPACE
:
345 return (gettext("\tprojectspace [-Hp] [-o field[,...]] "
347 "\t [-S field] ... <filesystem|snapshot>\n"));
349 return (gettext("\tproject [-d|-r] <directory|file ...>\n"
350 "\tproject -c [-0] [-d|-r] [-p id] <directory|file ...>\n"
351 "\tproject -C [-k] [-r] <directory ...>\n"
352 "\tproject [-p id] [-r] [-s] <directory ...>\n"));
354 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
356 return (gettext("\tholds [-rH] <snapshot> ...\n"));
358 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
360 return (gettext("\tdiff [-FHt] <snapshot> "
361 "[snapshot|filesystem]\n"));
363 return (gettext("\tremap <filesystem | volume>\n"));
365 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
366 case HELP_CHANNEL_PROGRAM
:
367 return (gettext("\tprogram [-jn] [-t <instruction limit>] "
368 "[-m <memory limit (b)>] <pool> <program file> "
371 return (gettext("\tload-key [-rn] [-L <keylocation>] "
372 "<-a | filesystem|volume>\n"));
373 case HELP_UNLOAD_KEY
:
374 return (gettext("\tunload-key [-r] "
375 "<-a | filesystem|volume>\n"));
376 case HELP_CHANGE_KEY
:
377 return (gettext("\tchange-key [-l] [-o keyformat=<value>]\n"
378 "\t [-o keylocation=<value>] [-o pbkfd2iters=<value>]\n"
379 "\t <filesystem|volume>\n"
380 "\tchange-key -i [-l] <filesystem|volume>\n"));
390 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
395 * Utility function to guarantee malloc() success.
399 safe_malloc(size_t size
)
403 if ((data
= calloc(1, size
)) == NULL
)
410 safe_realloc(void *data
, size_t size
)
413 if ((newp
= realloc(data
, size
)) == NULL
) {
422 safe_strdup(char *str
)
424 char *dupstr
= strdup(str
);
433 * Callback routine that will print out information for each of
437 usage_prop_cb(int prop
, void *cb
)
441 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
443 if (zfs_prop_readonly(prop
))
444 (void) fprintf(fp
, " NO ");
446 (void) fprintf(fp
, "YES ");
448 if (zfs_prop_inheritable(prop
))
449 (void) fprintf(fp
, " YES ");
451 (void) fprintf(fp
, " NO ");
453 if (zfs_prop_values(prop
) == NULL
)
454 (void) fprintf(fp
, "-\n");
456 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
462 * Display usage message. If we're inside a command, display only the usage for
463 * that command. Otherwise, iterate over the entire command table and display
464 * a complete usage message.
467 usage(boolean_t requested
)
470 boolean_t show_properties
= B_FALSE
;
471 FILE *fp
= requested
? stdout
: stderr
;
473 if (current_command
== NULL
) {
475 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
477 gettext("where 'command' is one of the following:\n\n"));
479 for (i
= 0; i
< NCOMMAND
; i
++) {
480 if (command_table
[i
].name
== NULL
)
481 (void) fprintf(fp
, "\n");
483 (void) fprintf(fp
, "%s",
484 get_usage(command_table
[i
].usage
));
487 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
488 "pool/[dataset/]*dataset[@name]\n"));
490 (void) fprintf(fp
, gettext("usage:\n"));
491 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
494 if (current_command
!= NULL
&&
495 (strcmp(current_command
->name
, "set") == 0 ||
496 strcmp(current_command
->name
, "get") == 0 ||
497 strcmp(current_command
->name
, "inherit") == 0 ||
498 strcmp(current_command
->name
, "list") == 0))
499 show_properties
= B_TRUE
;
501 if (show_properties
) {
503 gettext("\nThe following properties are supported:\n"));
505 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
506 "PROPERTY", "EDIT", "INHERIT", "VALUES");
508 /* Iterate over all properties */
509 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
512 (void) fprintf(fp
, "\t%-15s ", "userused@...");
513 (void) fprintf(fp
, " NO NO <size>\n");
514 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
515 (void) fprintf(fp
, " NO NO <size>\n");
516 (void) fprintf(fp
, "\t%-15s ", "projectused@...");
517 (void) fprintf(fp
, " NO NO <size>\n");
518 (void) fprintf(fp
, "\t%-15s ", "userobjused@...");
519 (void) fprintf(fp
, " NO NO <size>\n");
520 (void) fprintf(fp
, "\t%-15s ", "groupobjused@...");
521 (void) fprintf(fp
, " NO NO <size>\n");
522 (void) fprintf(fp
, "\t%-15s ", "projectobjused@...");
523 (void) fprintf(fp
, " NO NO <size>\n");
524 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
525 (void) fprintf(fp
, "YES NO <size> | none\n");
526 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
527 (void) fprintf(fp
, "YES NO <size> | none\n");
528 (void) fprintf(fp
, "\t%-15s ", "projectquota@...");
529 (void) fprintf(fp
, "YES NO <size> | none\n");
530 (void) fprintf(fp
, "\t%-15s ", "userobjquota@...");
531 (void) fprintf(fp
, "YES NO <size> | none\n");
532 (void) fprintf(fp
, "\t%-15s ", "groupobjquota@...");
533 (void) fprintf(fp
, "YES NO <size> | none\n");
534 (void) fprintf(fp
, "\t%-15s ", "projectobjquota@...");
535 (void) fprintf(fp
, "YES NO <size> | none\n");
536 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
537 (void) fprintf(fp
, " NO NO <size>\n");
539 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
540 "with standard units such as K, M, G, etc.\n"));
541 (void) fprintf(fp
, gettext("\nUser-defined properties can "
542 "be specified by using a name containing a colon (:).\n"));
543 (void) fprintf(fp
, gettext("\nThe {user|group|project}"
544 "[obj]{used|quota}@ properties must be appended with\n"
545 "a user|group|project specifier of one of these forms:\n"
546 " POSIX name (eg: \"matt\")\n"
547 " POSIX id (eg: \"126829\")\n"
548 " SMB name@domain (eg: \"matt@sun\")\n"
549 " SMB SID (eg: \"S-1-234-567-89\")\n"));
552 gettext("\nFor the property list, run: %s\n"),
555 gettext("\nFor the delegated permission list, run: %s\n"),
556 "zfs allow|unallow");
560 * See comments at end of main().
562 if (getenv("ZFS_ABORT") != NULL
) {
563 (void) printf("dumping core by request\n");
567 exit(requested
? 0 : 2);
571 * Take a property=value argument string and add it to the given nvlist.
572 * Modifies the argument inplace.
575 parseprop(nvlist_t
*props
, char *propname
)
579 if ((propval
= strchr(propname
, '=')) == NULL
) {
580 (void) fprintf(stderr
, gettext("missing "
581 "'=' for property=value argument\n"));
586 if (nvlist_exists(props
, propname
)) {
587 (void) fprintf(stderr
, gettext("property '%s' "
588 "specified multiple times\n"), propname
);
591 if (nvlist_add_string(props
, propname
, propval
) != 0)
597 * Take a property name argument and add it to the given nvlist.
598 * Modifies the argument inplace.
601 parsepropname(nvlist_t
*props
, char *propname
)
603 if (strchr(propname
, '=') != NULL
) {
604 (void) fprintf(stderr
, gettext("invalid character "
605 "'=' in property argument\n"));
608 if (nvlist_exists(props
, propname
)) {
609 (void) fprintf(stderr
, gettext("property '%s' "
610 "specified multiple times\n"), propname
);
613 if (nvlist_add_boolean(props
, propname
) != 0)
619 parse_depth(char *opt
, int *flags
)
624 depth
= (int)strtol(opt
, &tmp
, 0);
626 (void) fprintf(stderr
,
627 gettext("%s is not an integer\n"), optarg
);
631 (void) fprintf(stderr
,
632 gettext("Depth can not be negative.\n"));
635 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
639 #define PROGRESS_DELAY 2 /* seconds */
641 static char *pt_reverse
= "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
642 static time_t pt_begin
;
643 static char *pt_header
= NULL
;
644 static boolean_t pt_shown
;
647 start_progress_timer(void)
649 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
654 set_progress_header(char *header
)
656 assert(pt_header
== NULL
);
657 pt_header
= safe_strdup(header
);
659 (void) printf("%s: ", header
);
660 (void) fflush(stdout
);
665 update_progress(char *update
)
667 if (!pt_shown
&& time(NULL
) > pt_begin
) {
668 int len
= strlen(update
);
670 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
672 (void) fflush(stdout
);
674 } else if (pt_shown
) {
675 int len
= strlen(update
);
677 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
678 (void) fflush(stdout
);
683 finish_progress(char *done
)
686 (void) printf("%s\n", done
);
687 (void) fflush(stdout
);
694 zfs_mount_and_share(libzfs_handle_t
*hdl
, const char *dataset
, zfs_type_t type
)
696 zfs_handle_t
*zhp
= NULL
;
699 zhp
= zfs_open(hdl
, dataset
, type
);
704 * Volumes may neither be mounted or shared. Potentially in the
705 * future filesystems detected on these volumes could be mounted.
707 if (zfs_get_type(zhp
) == ZFS_TYPE_VOLUME
) {
713 * Mount and/or share the new filesystem as appropriate. We provide a
714 * verbose error message to let the user know that their filesystem was
715 * in fact created, even if we failed to mount or share it.
717 * If the user doesn't want the dataset automatically mounted, then
718 * skip the mount/share step
720 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
, B_FALSE
) &&
721 zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
) {
722 if (geteuid() != 0) {
723 (void) fprintf(stderr
, gettext("filesystem "
724 "successfully created, but it may only be "
725 "mounted by root\n"));
727 } else if (zfs_mount(zhp
, NULL
, 0) != 0) {
728 (void) fprintf(stderr
, gettext("filesystem "
729 "successfully created, but not mounted\n"));
731 } else if (zfs_share(zhp
) != 0) {
732 (void) fprintf(stderr
, gettext("filesystem "
733 "successfully created, but not shared\n"));
744 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
746 * Given an existing dataset, create a writable copy whose initial contents
747 * are the same as the source. The newly created dataset maintains a
748 * dependency on the original; the original cannot be destroyed so long as
751 * The '-p' flag creates all the non-existing ancestors of the target first.
754 zfs_do_clone(int argc
, char **argv
)
756 zfs_handle_t
*zhp
= NULL
;
757 boolean_t parents
= B_FALSE
;
762 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
766 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
769 if (!parseprop(props
, optarg
)) {
778 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
787 /* check number of arguments */
789 (void) fprintf(stderr
, gettext("missing source dataset "
794 (void) fprintf(stderr
, gettext("missing target dataset "
799 (void) fprintf(stderr
, gettext("too many arguments\n"));
803 /* open the source dataset */
804 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
) {
809 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
812 * Now create the ancestors of the target dataset. If the
813 * target already exists and '-p' option was used we should not
816 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
822 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
830 ret
= zfs_clone(zhp
, argv
[1], props
);
832 /* create the mountpoint if necessary */
835 (void) zpool_log_history(g_zfs
, history_str
);
836 log_history
= B_FALSE
;
839 ret
= zfs_mount_and_share(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
848 ASSERT3P(zhp
, ==, NULL
);
855 * zfs create [-p] [-o prop=value] ... fs
856 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
858 * Create a new dataset. This command can be used to create filesystems
859 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
860 * For volumes, the user must specify a size to be used.
862 * The '-s' flag applies only to volumes, and indicates that we should not try
863 * to set the reservation for this volume. By default we set a reservation
864 * equal to the size for any volume. For pools with SPA_VERSION >=
865 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
867 * The '-p' flag creates all the non-existing ancestors of the target first.
870 zfs_do_create(int argc
, char **argv
)
872 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
873 uint64_t volsize
= 0;
875 boolean_t noreserve
= B_FALSE
;
876 boolean_t bflag
= B_FALSE
;
877 boolean_t parents
= B_FALSE
;
882 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
886 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
889 type
= ZFS_TYPE_VOLUME
;
890 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
891 (void) fprintf(stderr
, gettext("bad volume "
892 "size '%s': %s\n"), optarg
,
893 libzfs_error_description(g_zfs
));
897 if (nvlist_add_uint64(props
,
898 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
907 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
908 (void) fprintf(stderr
, gettext("bad volume "
909 "block size '%s': %s\n"), optarg
,
910 libzfs_error_description(g_zfs
));
914 if (nvlist_add_uint64(props
,
915 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
920 if (!parseprop(props
, optarg
))
927 (void) fprintf(stderr
, gettext("missing size "
931 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
937 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
938 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
939 "used when creating a volume\n"));
946 /* check number of arguments */
948 (void) fprintf(stderr
, gettext("missing %s argument\n"),
949 zfs_type_to_name(type
));
953 (void) fprintf(stderr
, gettext("too many arguments\n"));
957 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
958 zpool_handle_t
*zpool_handle
;
959 nvlist_t
*real_props
= NULL
;
960 uint64_t spa_version
;
962 zfs_prop_t resv_prop
;
966 if ((p
= strchr(argv
[0], '/')) != NULL
)
968 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
971 if (zpool_handle
== NULL
)
973 spa_version
= zpool_get_prop_int(zpool_handle
,
974 ZPOOL_PROP_VERSION
, NULL
);
975 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
976 resv_prop
= ZFS_PROP_REFRESERVATION
;
978 resv_prop
= ZFS_PROP_RESERVATION
;
980 (void) snprintf(msg
, sizeof (msg
),
981 gettext("cannot create '%s'"), argv
[0]);
982 if (props
&& (real_props
= zfs_valid_proplist(g_zfs
, type
,
983 props
, 0, NULL
, zpool_handle
, B_TRUE
, msg
)) == NULL
) {
984 zpool_close(zpool_handle
);
987 zpool_close(zpool_handle
);
989 volsize
= zvol_volsize_to_reservation(volsize
, real_props
);
990 nvlist_free(real_props
);
992 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
994 if (nvlist_add_uint64(props
,
995 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
1002 if (parents
&& zfs_name_valid(argv
[0], type
)) {
1004 * Now create the ancestors of target dataset. If the target
1005 * already exists and '-p' option was used we should not
1008 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
1012 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
1016 /* pass to libzfs */
1017 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
1021 (void) zpool_log_history(g_zfs
, history_str
);
1022 log_history
= B_FALSE
;
1025 ret
= zfs_mount_and_share(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
1036 * zfs destroy [-rRf] <fs, vol>
1037 * zfs destroy [-rRd] <snap>
1039 * -r Recursively destroy all children
1040 * -R Recursively destroy all dependents, including clones
1041 * -f Force unmounting of any dependents
1042 * -d If we can't destroy now, mark for deferred destruction
1044 * Destroys the given dataset. By default, it will unmount any filesystems,
1045 * and refuse to destroy a dataset that has any dependents. A dependent can
1046 * either be a child, or a clone of a child.
1048 typedef struct destroy_cbdata
{
1051 boolean_t cb_recurse
;
1053 boolean_t cb_doclones
;
1054 zfs_handle_t
*cb_target
;
1055 boolean_t cb_defer_destroy
;
1056 boolean_t cb_verbose
;
1057 boolean_t cb_parsable
;
1058 boolean_t cb_dryrun
;
1060 nvlist_t
*cb_batchedsnaps
;
1062 /* first snap in contiguous run */
1064 /* previous snap in contiguous run */
1066 int64_t cb_snapused
;
1069 uint64_t cb_snap_count
;
1073 * Check for any dependents based on the '-r' or '-R' flags.
1076 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
1078 destroy_cbdata_t
*cbp
= data
;
1079 const char *tname
= zfs_get_name(cbp
->cb_target
);
1080 const char *name
= zfs_get_name(zhp
);
1082 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
1083 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
1085 * This is a direct descendant, not a clone somewhere else in
1088 if (cbp
->cb_recurse
)
1091 if (cbp
->cb_first
) {
1092 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1093 "%s has children\n"),
1094 zfs_get_name(cbp
->cb_target
),
1095 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1096 (void) fprintf(stderr
, gettext("use '-r' to destroy "
1097 "the following datasets:\n"));
1098 cbp
->cb_first
= B_FALSE
;
1099 cbp
->cb_error
= B_TRUE
;
1102 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1105 * This is a clone. We only want to report this if the '-r'
1106 * wasn't specified, or the target is a snapshot.
1108 if (!cbp
->cb_recurse
&&
1109 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
1112 if (cbp
->cb_first
) {
1113 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1114 "%s has dependent clones\n"),
1115 zfs_get_name(cbp
->cb_target
),
1116 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1117 (void) fprintf(stderr
, gettext("use '-R' to destroy "
1118 "the following datasets:\n"));
1119 cbp
->cb_first
= B_FALSE
;
1120 cbp
->cb_error
= B_TRUE
;
1121 cbp
->cb_dryrun
= B_TRUE
;
1124 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1133 destroy_batched(destroy_cbdata_t
*cb
)
1135 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1136 cb
->cb_batchedsnaps
, B_FALSE
);
1137 fnvlist_free(cb
->cb_batchedsnaps
);
1138 cb
->cb_batchedsnaps
= fnvlist_alloc();
1143 destroy_callback(zfs_handle_t
*zhp
, void *data
)
1145 destroy_cbdata_t
*cb
= data
;
1146 const char *name
= zfs_get_name(zhp
);
1149 if (cb
->cb_verbose
) {
1150 if (cb
->cb_parsable
) {
1151 (void) printf("destroy\t%s\n", name
);
1152 } else if (cb
->cb_dryrun
) {
1153 (void) printf(gettext("would destroy %s\n"),
1156 (void) printf(gettext("will destroy %s\n"),
1162 * Ignore pools (which we've already flagged as an error before getting
1165 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1166 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1170 if (cb
->cb_dryrun
) {
1176 * We batch up all contiguous snapshots (even of different
1177 * filesystems) and destroy them with one ioctl. We can't
1178 * simply do all snap deletions and then all fs deletions,
1179 * because we must delete a clone before its origin.
1181 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1182 cb
->cb_snap_count
++;
1183 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1184 if (cb
->cb_snap_count
% 10 == 0 && cb
->cb_defer_destroy
)
1185 error
= destroy_batched(cb
);
1187 error
= destroy_batched(cb
);
1189 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1190 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1193 * When performing a recursive destroy we ignore errors
1194 * so that the recursive destroy could continue
1195 * destroying past problem datasets
1197 if (cb
->cb_recurse
) {
1198 cb
->cb_error
= B_TRUE
;
1210 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1212 destroy_cbdata_t
*cb
= arg
;
1213 const char *name
= zfs_get_name(zhp
);
1216 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1217 if (cb
->cb_firstsnap
== NULL
)
1218 cb
->cb_firstsnap
= strdup(name
);
1219 if (cb
->cb_prevsnap
!= NULL
)
1220 free(cb
->cb_prevsnap
);
1221 /* this snap continues the current range */
1222 cb
->cb_prevsnap
= strdup(name
);
1223 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1225 if (cb
->cb_verbose
) {
1226 if (cb
->cb_parsable
) {
1227 (void) printf("destroy\t%s\n", name
);
1228 } else if (cb
->cb_dryrun
) {
1229 (void) printf(gettext("would destroy %s\n"),
1232 (void) printf(gettext("will destroy %s\n"),
1236 } else if (cb
->cb_firstsnap
!= NULL
) {
1237 /* end of this range */
1239 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1240 cb
->cb_prevsnap
, &used
);
1241 cb
->cb_snapused
+= used
;
1242 free(cb
->cb_firstsnap
);
1243 cb
->cb_firstsnap
= NULL
;
1244 free(cb
->cb_prevsnap
);
1245 cb
->cb_prevsnap
= NULL
;
1252 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1255 assert(cb
->cb_firstsnap
== NULL
);
1256 assert(cb
->cb_prevsnap
== NULL
);
1257 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1258 if (cb
->cb_firstsnap
!= NULL
) {
1261 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1262 cb
->cb_prevsnap
, &used
);
1264 cb
->cb_snapused
+= used
;
1265 free(cb
->cb_firstsnap
);
1266 cb
->cb_firstsnap
= NULL
;
1267 free(cb
->cb_prevsnap
);
1268 cb
->cb_prevsnap
= NULL
;
1274 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1276 destroy_cbdata_t
*cb
= arg
;
1279 /* Check for clones. */
1280 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1281 cb
->cb_target
= zhp
;
1282 cb
->cb_first
= B_TRUE
;
1283 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1284 destroy_check_dependent
, cb
);
1288 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1296 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1298 destroy_cbdata_t
*cb
= arg
;
1301 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1307 if (cb
->cb_verbose
) {
1308 err
= destroy_print_snapshots(zhp
, cb
);
1314 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1322 destroy_clones(destroy_cbdata_t
*cb
)
1325 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1327 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1328 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1331 boolean_t defer
= cb
->cb_defer_destroy
;
1335 * We can't defer destroy non-snapshots, so set it to
1336 * false while destroying the clones.
1338 cb
->cb_defer_destroy
= B_FALSE
;
1339 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1340 destroy_callback
, cb
);
1341 cb
->cb_defer_destroy
= defer
;
1351 zfs_do_destroy(int argc
, char **argv
)
1353 destroy_cbdata_t cb
= { 0 };
1357 zfs_handle_t
*zhp
= NULL
;
1359 zfs_type_t type
= ZFS_TYPE_DATASET
;
1362 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1365 cb
.cb_verbose
= B_TRUE
;
1368 cb
.cb_verbose
= B_TRUE
;
1369 cb
.cb_parsable
= B_TRUE
;
1372 cb
.cb_dryrun
= B_TRUE
;
1375 cb
.cb_defer_destroy
= B_TRUE
;
1376 type
= ZFS_TYPE_SNAPSHOT
;
1379 cb
.cb_force
= B_TRUE
;
1382 cb
.cb_recurse
= B_TRUE
;
1385 cb
.cb_recurse
= B_TRUE
;
1386 cb
.cb_doclones
= B_TRUE
;
1390 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1399 /* check number of arguments */
1401 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1405 (void) fprintf(stderr
, gettext("too many arguments\n"));
1409 at
= strchr(argv
[0], '@');
1410 pound
= strchr(argv
[0], '#');
1413 /* Build the list of snaps to destroy in cb_nvl. */
1414 cb
.cb_nvl
= fnvlist_alloc();
1417 zhp
= zfs_open(g_zfs
, argv
[0],
1418 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1420 nvlist_free(cb
.cb_nvl
);
1424 cb
.cb_snapspec
= at
+ 1;
1425 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1431 if (nvlist_empty(cb
.cb_nvl
)) {
1432 (void) fprintf(stderr
, gettext("could not find any "
1433 "snapshots to destroy; check snapshot names.\n"));
1438 if (cb
.cb_verbose
) {
1440 zfs_nicebytes(cb
.cb_snapused
, buf
, sizeof (buf
));
1441 if (cb
.cb_parsable
) {
1442 (void) printf("reclaim\t%llu\n",
1443 (u_longlong_t
)cb
.cb_snapused
);
1444 } else if (cb
.cb_dryrun
) {
1445 (void) printf(gettext("would reclaim %s\n"),
1448 (void) printf(gettext("will reclaim %s\n"),
1453 if (!cb
.cb_dryrun
) {
1454 if (cb
.cb_doclones
) {
1455 cb
.cb_batchedsnaps
= fnvlist_alloc();
1456 err
= destroy_clones(&cb
);
1458 err
= zfs_destroy_snaps_nvl(g_zfs
,
1459 cb
.cb_batchedsnaps
, B_FALSE
);
1467 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1468 cb
.cb_defer_destroy
);
1474 } else if (pound
!= NULL
) {
1479 (void) fprintf(stderr
,
1480 "dryrun is not supported with bookmark\n");
1484 if (cb
.cb_defer_destroy
) {
1485 (void) fprintf(stderr
,
1486 "defer destroy is not supported with bookmark\n");
1490 if (cb
.cb_recurse
) {
1491 (void) fprintf(stderr
,
1492 "recursive is not supported with bookmark\n");
1496 if (!zfs_bookmark_exists(argv
[0])) {
1497 (void) fprintf(stderr
, gettext("bookmark '%s' "
1498 "does not exist.\n"), argv
[0]);
1502 nvl
= fnvlist_alloc();
1503 fnvlist_add_boolean(nvl
, argv
[0]);
1505 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1507 (void) zfs_standard_error(g_zfs
, err
,
1508 "cannot destroy bookmark");
1515 /* Open the given dataset */
1516 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1522 * Perform an explicit check for pools before going any further.
1524 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1525 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1526 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1527 "operation does not apply to pools\n"),
1529 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1530 "%s' to destroy all datasets in the pool\n"),
1532 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1533 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1539 * Check for any dependents and/or clones.
1541 cb
.cb_first
= B_TRUE
;
1542 if (!cb
.cb_doclones
&&
1543 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1553 cb
.cb_batchedsnaps
= fnvlist_alloc();
1554 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1561 * Do the real thing. The callback will close the
1562 * handle regardless of whether it succeeds or not.
1564 err
= destroy_callback(zhp
, &cb
);
1567 err
= zfs_destroy_snaps_nvl(g_zfs
,
1568 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1570 if (err
!= 0 || cb
.cb_error
== B_TRUE
)
1575 fnvlist_free(cb
.cb_batchedsnaps
);
1576 fnvlist_free(cb
.cb_nvl
);
1583 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1586 zfs_get_column_t col
;
1588 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1589 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1590 if (col
== GET_COL_RECVD
)
1596 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1597 * < all | property[,property]... > < fs | snap | vol > ...
1599 * -r recurse over any child datasets
1600 * -H scripted mode. Headers are stripped, and fields are separated
1601 * by tabs instead of spaces.
1602 * -o Set of fields to display. One of "name,property,value,
1603 * received,source". Default is "name,property,value,source".
1604 * "all" is an alias for all five.
1605 * -s Set of sources to allow. One of
1606 * "local,default,inherited,received,temporary,none". Default is
1608 * -p Display values in parsable (literal) format.
1610 * Prints properties for the given datasets. The user can control which
1611 * columns to display as well as which property types to allow.
1615 * Invoked to display the properties for a single dataset.
1618 get_callback(zfs_handle_t
*zhp
, void *data
)
1620 char buf
[ZFS_MAXPROPLEN
];
1621 char rbuf
[ZFS_MAXPROPLEN
];
1622 zprop_source_t sourcetype
;
1623 char source
[ZFS_MAX_DATASET_NAME_LEN
];
1624 zprop_get_cbdata_t
*cbp
= data
;
1625 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1626 zprop_list_t
*pl
= cbp
->cb_proplist
;
1630 boolean_t received
= is_recvd_column(cbp
);
1632 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1633 char *recvdval
= NULL
;
1635 * Skip the special fake placeholder. This will also skip over
1636 * the name property when 'all' is specified.
1638 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1639 pl
== cbp
->cb_proplist
)
1642 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1643 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1644 sizeof (buf
), &sourcetype
, source
,
1646 cbp
->cb_literal
) != 0) {
1649 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1650 ZFS_TYPE_DATASET
, B_FALSE
)) {
1651 (void) fprintf(stderr
,
1652 gettext("No such property '%s'\n"),
1653 zfs_prop_to_name(pl
->pl_prop
));
1656 sourcetype
= ZPROP_SRC_NONE
;
1657 (void) strlcpy(buf
, "-", sizeof (buf
));
1660 if (received
&& (zfs_prop_get_recvd(zhp
,
1661 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1662 cbp
->cb_literal
) == 0))
1665 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1666 zfs_prop_to_name(pl
->pl_prop
),
1667 buf
, sourcetype
, source
, recvdval
);
1668 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1669 sourcetype
= ZPROP_SRC_LOCAL
;
1671 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1672 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1673 sourcetype
= ZPROP_SRC_NONE
;
1674 (void) strlcpy(buf
, "-", sizeof (buf
));
1677 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1678 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1679 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1680 sourcetype
= ZPROP_SRC_LOCAL
;
1682 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1683 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1684 sourcetype
= ZPROP_SRC_NONE
;
1685 (void) strlcpy(buf
, "-", sizeof (buf
));
1688 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1689 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1691 if (nvlist_lookup_nvlist(user_props
,
1692 pl
->pl_user_prop
, &propval
) != 0) {
1695 sourcetype
= ZPROP_SRC_NONE
;
1698 verify(nvlist_lookup_string(propval
,
1699 ZPROP_VALUE
, &strval
) == 0);
1700 verify(nvlist_lookup_string(propval
,
1701 ZPROP_SOURCE
, &sourceval
) == 0);
1703 if (strcmp(sourceval
,
1704 zfs_get_name(zhp
)) == 0) {
1705 sourcetype
= ZPROP_SRC_LOCAL
;
1706 } else if (strcmp(sourceval
,
1707 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1708 sourcetype
= ZPROP_SRC_RECEIVED
;
1710 sourcetype
= ZPROP_SRC_INHERITED
;
1711 (void) strlcpy(source
,
1712 sourceval
, sizeof (source
));
1716 if (received
&& (zfs_prop_get_recvd(zhp
,
1717 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1718 cbp
->cb_literal
) == 0))
1721 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1722 pl
->pl_user_prop
, strval
, sourcetype
,
1731 zfs_do_get(int argc
, char **argv
)
1733 zprop_get_cbdata_t cb
= { 0 };
1734 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1735 int types
= ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
;
1736 char *value
, *fields
;
1739 zprop_list_t fake_name
= { 0 };
1742 * Set up default columns and sources.
1744 cb
.cb_sources
= ZPROP_SRC_ALL
;
1745 cb
.cb_columns
[0] = GET_COL_NAME
;
1746 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1747 cb
.cb_columns
[2] = GET_COL_VALUE
;
1748 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1749 cb
.cb_type
= ZFS_TYPE_DATASET
;
1752 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1755 cb
.cb_literal
= B_TRUE
;
1758 limit
= parse_depth(optarg
, &flags
);
1761 flags
|= ZFS_ITER_RECURSE
;
1764 cb
.cb_scripted
= B_TRUE
;
1767 (void) fprintf(stderr
, gettext("missing argument for "
1768 "'%c' option\n"), optopt
);
1773 * Process the set of columns to display. We zero out
1774 * the structure to give us a blank slate.
1776 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1778 while (*optarg
!= '\0') {
1779 static char *col_subopts
[] =
1780 { "name", "property", "value", "received",
1781 "source", "all", NULL
};
1783 if (i
== ZFS_GET_NCOLS
) {
1784 (void) fprintf(stderr
, gettext("too "
1785 "many fields given to -o "
1790 switch (getsubopt(&optarg
, col_subopts
,
1793 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1796 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1799 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1802 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1803 flags
|= ZFS_ITER_RECVD_PROPS
;
1806 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1810 (void) fprintf(stderr
,
1811 gettext("\"all\" conflicts "
1812 "with specific fields "
1813 "given to -o option\n"));
1816 cb
.cb_columns
[0] = GET_COL_NAME
;
1817 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1818 cb
.cb_columns
[2] = GET_COL_VALUE
;
1819 cb
.cb_columns
[3] = GET_COL_RECVD
;
1820 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1821 flags
|= ZFS_ITER_RECVD_PROPS
;
1825 (void) fprintf(stderr
,
1826 gettext("invalid column name "
1835 while (*optarg
!= '\0') {
1836 static char *source_subopts
[] = {
1837 "local", "default", "inherited",
1838 "received", "temporary", "none",
1841 switch (getsubopt(&optarg
, source_subopts
,
1844 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1847 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1850 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1853 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1856 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1859 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1862 (void) fprintf(stderr
,
1863 gettext("invalid source "
1872 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1873 while (*optarg
!= '\0') {
1874 static char *type_subopts
[] = { "filesystem",
1875 "volume", "snapshot", "bookmark",
1878 switch (getsubopt(&optarg
, type_subopts
,
1881 types
|= ZFS_TYPE_FILESYSTEM
;
1884 types
|= ZFS_TYPE_VOLUME
;
1887 types
|= ZFS_TYPE_SNAPSHOT
;
1890 types
|= ZFS_TYPE_BOOKMARK
;
1893 types
= ZFS_TYPE_DATASET
|
1898 (void) fprintf(stderr
,
1899 gettext("invalid type '%s'\n"),
1907 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1917 (void) fprintf(stderr
, gettext("missing property "
1924 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1932 * As part of zfs_expand_proplist(), we keep track of the maximum column
1933 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1934 * need to know the maximum name length. However, the user likely did
1935 * not specify 'name' as one of the properties to fetch, so we need to
1936 * make sure we always include at least this property for
1937 * print_get_headers() to work properly.
1939 if (cb
.cb_proplist
!= NULL
) {
1940 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1941 fake_name
.pl_width
= strlen(gettext("NAME"));
1942 fake_name
.pl_next
= cb
.cb_proplist
;
1943 cb
.cb_proplist
= &fake_name
;
1946 cb
.cb_first
= B_TRUE
;
1948 /* run for each object */
1949 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1950 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1952 if (cb
.cb_proplist
== &fake_name
)
1953 zprop_free_list(fake_name
.pl_next
);
1955 zprop_free_list(cb
.cb_proplist
);
1961 * inherit [-rS] <property> <fs|vol> ...
1963 * -r Recurse over all children
1964 * -S Revert to received value, if any
1966 * For each dataset specified on the command line, inherit the given property
1967 * from its parent. Inheriting a property at the pool level will cause it to
1968 * use the default value. The '-r' flag will recurse over all children, and is
1969 * useful for setting a property on a hierarchy-wide basis, regardless of any
1970 * local modifications for each dataset.
1973 typedef struct inherit_cbdata
{
1974 const char *cb_propname
;
1975 boolean_t cb_received
;
1979 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1981 inherit_cbdata_t
*cb
= data
;
1982 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1985 * If we're doing it recursively, then ignore properties that
1986 * are not valid for this type of dataset.
1988 if (prop
!= ZPROP_INVAL
&&
1989 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
), B_FALSE
))
1992 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1996 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1998 inherit_cbdata_t
*cb
= data
;
2000 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
2004 zfs_do_inherit(int argc
, char **argv
)
2008 inherit_cbdata_t cb
= { 0 };
2012 boolean_t received
= B_FALSE
;
2015 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
2018 flags
|= ZFS_ITER_RECURSE
;
2025 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2034 /* check number of arguments */
2036 (void) fprintf(stderr
, gettext("missing property argument\n"));
2040 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
2048 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
2049 if (zfs_prop_readonly(prop
)) {
2050 (void) fprintf(stderr
, gettext(
2051 "%s property is read-only\n"),
2055 if (!zfs_prop_inheritable(prop
) && !received
) {
2056 (void) fprintf(stderr
, gettext("'%s' property cannot "
2057 "be inherited\n"), propname
);
2058 if (prop
== ZFS_PROP_QUOTA
||
2059 prop
== ZFS_PROP_RESERVATION
||
2060 prop
== ZFS_PROP_REFQUOTA
||
2061 prop
== ZFS_PROP_REFRESERVATION
) {
2062 (void) fprintf(stderr
, gettext("use 'zfs set "
2063 "%s=none' to clear\n"), propname
);
2064 (void) fprintf(stderr
, gettext("use 'zfs "
2065 "inherit -S %s' to revert to received "
2066 "value\n"), propname
);
2070 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
2071 prop
== ZFS_PROP_VERSION
)) {
2072 (void) fprintf(stderr
, gettext("'%s' property cannot "
2073 "be reverted to a received value\n"), propname
);
2076 } else if (!zfs_prop_user(propname
)) {
2077 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
2082 cb
.cb_propname
= propname
;
2083 cb
.cb_received
= received
;
2085 if (flags
& ZFS_ITER_RECURSE
) {
2086 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
2087 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
2089 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
2090 NULL
, NULL
, 0, inherit_cb
, &cb
);
2096 typedef struct upgrade_cbdata
{
2097 uint64_t cb_numupgraded
;
2098 uint64_t cb_numsamegraded
;
2099 uint64_t cb_numfailed
;
2100 uint64_t cb_version
;
2102 boolean_t cb_foundone
;
2103 char cb_lastfs
[ZFS_MAX_DATASET_NAME_LEN
];
2107 same_pool(zfs_handle_t
*zhp
, const char *name
)
2109 int len1
= strcspn(name
, "/@");
2110 const char *zhname
= zfs_get_name(zhp
);
2111 int len2
= strcspn(zhname
, "/@");
2115 return (strncmp(name
, zhname
, len1
) == 0);
2119 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
2121 upgrade_cbdata_t
*cb
= data
;
2122 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2124 /* list if it's old/new */
2125 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
2126 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
2129 str
= gettext("The following filesystems are "
2130 "formatted using a newer software version and\n"
2131 "cannot be accessed on the current system.\n\n");
2133 str
= gettext("The following filesystems are "
2134 "out of date, and can be upgraded. After being\n"
2135 "upgraded, these filesystems (and any 'zfs send' "
2136 "streams generated from\n"
2137 "subsequent snapshots) will no longer be "
2138 "accessible by older software versions.\n\n");
2141 if (!cb
->cb_foundone
) {
2143 (void) printf(gettext("VER FILESYSTEM\n"));
2144 (void) printf(gettext("--- ------------\n"));
2145 cb
->cb_foundone
= B_TRUE
;
2148 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
2155 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2157 upgrade_cbdata_t
*cb
= data
;
2158 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2159 int needed_spa_version
;
2162 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2165 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2167 if (needed_spa_version
< 0)
2170 if (spa_version
< needed_spa_version
) {
2172 (void) printf(gettext("%s: can not be "
2173 "upgraded; the pool version needs to first "
2174 "be upgraded\nto version %d\n\n"),
2175 zfs_get_name(zhp
), needed_spa_version
);
2181 if (version
< cb
->cb_version
) {
2183 (void) snprintf(verstr
, sizeof (verstr
),
2184 "%llu", (u_longlong_t
)cb
->cb_version
);
2185 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2187 * If they did "zfs upgrade -a", then we could
2188 * be doing ioctls to different pools. We need
2189 * to log this history once to each pool, and bypass
2190 * the normal history logging that happens in main().
2192 (void) zpool_log_history(g_zfs
, history_str
);
2193 log_history
= B_FALSE
;
2195 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2196 cb
->cb_numupgraded
++;
2199 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2200 } else if (version
> cb
->cb_version
) {
2201 /* can't downgrade */
2202 (void) printf(gettext("%s: can not be downgraded; "
2203 "it is already at version %u\n"),
2204 zfs_get_name(zhp
), version
);
2207 cb
->cb_numsamegraded
++;
2215 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2218 zfs_do_upgrade(int argc
, char **argv
)
2220 boolean_t all
= B_FALSE
;
2221 boolean_t showversions
= B_FALSE
;
2223 upgrade_cbdata_t cb
= { 0 };
2225 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2228 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2231 flags
|= ZFS_ITER_RECURSE
;
2234 showversions
= B_TRUE
;
2237 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2238 optarg
, &cb
.cb_version
) != 0) {
2239 (void) fprintf(stderr
,
2240 gettext("invalid version %s\n"), optarg
);
2249 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2258 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2260 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2261 cb
.cb_version
|| argc
))
2263 if ((all
|| argc
) && (showversions
))
2269 /* Show info on available versions. */
2270 (void) printf(gettext("The following filesystem versions are "
2272 (void) printf(gettext("VER DESCRIPTION\n"));
2273 (void) printf("--- -----------------------------------------"
2274 "---------------\n");
2275 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2276 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2277 (void) printf(gettext(" 3 Case insensitive and filesystem "
2278 "user identifier (FUID)\n"));
2279 (void) printf(gettext(" 4 userquota, groupquota "
2281 (void) printf(gettext(" 5 System attributes\n"));
2282 (void) printf(gettext("\nFor more information on a particular "
2283 "version, including supported releases,\n"));
2284 (void) printf("see the ZFS Administration Guide.\n\n");
2286 } else if (argc
|| all
) {
2287 /* Upgrade filesystems */
2288 if (cb
.cb_version
== 0)
2289 cb
.cb_version
= ZPL_VERSION
;
2290 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2291 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2292 (void) printf(gettext("%llu filesystems upgraded\n"),
2293 (u_longlong_t
)cb
.cb_numupgraded
);
2294 if (cb
.cb_numsamegraded
) {
2295 (void) printf(gettext("%llu filesystems already at "
2297 (u_longlong_t
)cb
.cb_numsamegraded
);
2299 if (cb
.cb_numfailed
!= 0)
2302 /* List old-version filesystems */
2304 (void) printf(gettext("This system is currently running "
2305 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2307 flags
|= ZFS_ITER_RECURSE
;
2308 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2309 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2311 found
= cb
.cb_foundone
;
2312 cb
.cb_foundone
= B_FALSE
;
2313 cb
.cb_newer
= B_TRUE
;
2315 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2316 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2318 if (!cb
.cb_foundone
&& !found
) {
2319 (void) printf(gettext("All filesystems are "
2320 "formatted with the current version.\n"));
2328 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2329 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2330 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2331 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2332 * zfs projectspace [-Hp] [-o field[,...]] [-s field [-s field]...]
2333 * [-S field [-S field]...] filesystem | snapshot
2335 * -H Scripted mode; elide headers and separate columns by tabs.
2336 * -i Translate SID to POSIX ID.
2337 * -n Print numeric ID instead of user/group name.
2338 * -o Control which fields to display.
2339 * -p Use exact (parsable) numeric output.
2340 * -s Specify sort columns, descending order.
2341 * -S Specify sort columns, ascending order.
2342 * -t Control which object types to display.
2344 * Displays space consumed by, and quotas on, each user in the specified
2345 * filesystem or snapshot.
2348 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2349 enum us_field_types
{
2357 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA",
2358 "OBJUSED", "OBJQUOTA" };
2359 static char *us_field_names
[] = { "type", "name", "used", "quota",
2360 "objused", "objquota" };
2361 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2363 #define USTYPE_PSX_GRP (1 << 0)
2364 #define USTYPE_PSX_USR (1 << 1)
2365 #define USTYPE_SMB_GRP (1 << 2)
2366 #define USTYPE_SMB_USR (1 << 3)
2367 #define USTYPE_PROJ (1 << 4)
2368 #define USTYPE_ALL \
2369 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR | \
2372 static int us_type_bits
[] = {
2379 static char *us_type_names
[] = { "posixgroup", "posixuser", "smbgroup",
2382 typedef struct us_node
{
2384 uu_avl_node_t usn_avlnode
;
2385 uu_list_node_t usn_listnode
;
2388 typedef struct us_cbdata
{
2390 uu_avl_pool_t
*cb_avl_pool
;
2392 boolean_t cb_numname
;
2393 boolean_t cb_nicenum
;
2394 boolean_t cb_sid2posix
;
2395 zfs_userquota_prop_t cb_prop
;
2396 zfs_sort_column_t
*cb_sortcol
;
2397 size_t cb_width
[USFIELD_LAST
];
2400 static boolean_t us_populated
= B_FALSE
;
2403 zfs_sort_column_t
*si_sortcol
;
2404 boolean_t si_numname
;
2408 us_field_index(char *field
)
2412 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2413 if (strcmp(field
, us_field_names
[i
]) == 0)
2421 us_compare(const void *larg
, const void *rarg
, void *unused
)
2423 const us_node_t
*l
= larg
;
2424 const us_node_t
*r
= rarg
;
2425 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2426 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2427 boolean_t numname
= si
->si_numname
;
2428 nvlist_t
*lnvl
= l
->usn_nvl
;
2429 nvlist_t
*rnvl
= r
->usn_nvl
;
2433 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2440 zfs_prop_t prop
= sortcol
->sc_prop
;
2441 const char *propname
= NULL
;
2442 boolean_t reverse
= sortcol
->sc_reverse
;
2447 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2448 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2450 rc
= (rv32
< lv32
) ? 1 : -1;
2456 (void) nvlist_lookup_uint64(lnvl
, propname
,
2458 (void) nvlist_lookup_uint64(rnvl
, propname
,
2461 rc
= (rv64
< lv64
) ? 1 : -1;
2463 if ((nvlist_lookup_string(lnvl
, propname
,
2464 &lvstr
) == ENOENT
) ||
2465 (nvlist_lookup_string(rnvl
, propname
,
2466 &rvstr
) == ENOENT
)) {
2469 rc
= strcmp(lvstr
, rvstr
);
2473 case ZFS_PROP_QUOTA
:
2476 if (prop
== ZFS_PROP_USED
)
2480 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2481 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2483 rc
= (rv64
< lv64
) ? 1 : -1;
2492 return (reverse
? 1 : -1);
2494 return (reverse
? -1 : 1);
2499 * If entries still seem to be the same, check if they are of the same
2500 * type (smbentity is added only if we are doing SID to POSIX ID
2501 * translation where we can have duplicate type/name combinations).
2503 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2504 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2506 return (lvb
< rvb
? -1 : 1);
2512 zfs_prop_is_user(unsigned p
)
2514 return (p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
||
2515 p
== ZFS_PROP_USEROBJUSED
|| p
== ZFS_PROP_USEROBJQUOTA
);
2519 zfs_prop_is_group(unsigned p
)
2521 return (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
||
2522 p
== ZFS_PROP_GROUPOBJUSED
|| p
== ZFS_PROP_GROUPOBJQUOTA
);
2526 zfs_prop_is_project(unsigned p
)
2528 return (p
== ZFS_PROP_PROJECTUSED
|| p
== ZFS_PROP_PROJECTQUOTA
||
2529 p
== ZFS_PROP_PROJECTOBJUSED
|| p
== ZFS_PROP_PROJECTOBJQUOTA
);
2532 static inline const char *
2533 us_type2str(unsigned field_type
)
2535 switch (field_type
) {
2536 case USTYPE_PSX_USR
:
2537 return ("POSIX User");
2538 case USTYPE_PSX_GRP
:
2539 return ("POSIX Group");
2540 case USTYPE_SMB_USR
:
2541 return ("SMB User");
2542 case USTYPE_SMB_GRP
:
2543 return ("SMB Group");
2547 return ("Undefined");
2552 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2554 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2555 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2560 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2561 uu_avl_t
*avl
= cb
->cb_avl
;
2565 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2567 const char *typestr
;
2571 int typeidx
, nameidx
, sizeidx
;
2572 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2573 boolean_t smbentity
= B_FALSE
;
2575 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2577 node
= safe_malloc(sizeof (us_node_t
));
2578 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2579 node
->usn_nvl
= props
;
2581 if (domain
!= NULL
&& domain
[0] != '\0') {
2584 char sid
[MAXNAMELEN
+ 32];
2588 directory_error_t e
;
2592 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2594 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2595 type
= USTYPE_SMB_GRP
;
2596 err
= sid_to_id(sid
, B_FALSE
, &id
);
2598 type
= USTYPE_SMB_USR
;
2599 err
= sid_to_id(sid
, B_TRUE
, &id
);
2604 if (!cb
->cb_sid2posix
) {
2605 e
= directory_name_from_sid(NULL
, sid
, &name
,
2608 directory_error_free(e
);
2618 #endif /* HAVE_IDMAP */
2621 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2623 if (zfs_prop_is_group(prop
)) {
2624 type
= USTYPE_PSX_GRP
;
2625 if (!cb
->cb_numname
) {
2628 if ((g
= getgrgid(rid
)) != NULL
)
2631 } else if (zfs_prop_is_user(prop
)) {
2632 type
= USTYPE_PSX_USR
;
2633 if (!cb
->cb_numname
) {
2636 if ((p
= getpwuid(rid
)) != NULL
)
2645 * Make sure that the type/name combination is unique when doing
2646 * SID to POSIX ID translation (hence changing the type from SMB to
2649 if (cb
->cb_sid2posix
&&
2650 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2653 /* Calculate/update width of TYPE field */
2654 typestr
= us_type2str(type
);
2655 typelen
= strlen(gettext(typestr
));
2656 typeidx
= us_field_index("type");
2657 if (typelen
> cb
->cb_width
[typeidx
])
2658 cb
->cb_width
[typeidx
] = typelen
;
2659 if (nvlist_add_uint32(props
, "type", type
) != 0)
2662 /* Calculate/update width of NAME field */
2663 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2664 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2666 namelen
= snprintf(NULL
, 0, "%u", rid
);
2668 if (nvlist_add_string(props
, "name", name
) != 0)
2670 namelen
= strlen(name
);
2672 nameidx
= us_field_index("name");
2673 if (nameidx
>= 0 && namelen
> cb
->cb_width
[nameidx
])
2674 cb
->cb_width
[nameidx
] = namelen
;
2677 * Check if this type/name combination is in the list and update it;
2678 * otherwise add new node to the list.
2680 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2681 uu_avl_insert(avl
, node
, idx
);
2686 props
= node
->usn_nvl
;
2689 /* Calculate/update width of USED/QUOTA fields */
2690 if (cb
->cb_nicenum
) {
2691 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
||
2692 prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
||
2693 prop
== ZFS_PROP_PROJECTUSED
||
2694 prop
== ZFS_PROP_PROJECTQUOTA
) {
2695 zfs_nicebytes(space
, sizebuf
, sizeof (sizebuf
));
2697 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2700 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu",
2701 (u_longlong_t
)space
);
2703 sizelen
= strlen(sizebuf
);
2704 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
||
2705 prop
== ZFS_PROP_PROJECTUSED
) {
2707 if (!nvlist_exists(props
, "quota"))
2708 (void) nvlist_add_uint64(props
, "quota", 0);
2709 } else if (prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
||
2710 prop
== ZFS_PROP_PROJECTQUOTA
) {
2712 if (!nvlist_exists(props
, "used"))
2713 (void) nvlist_add_uint64(props
, "used", 0);
2714 } else if (prop
== ZFS_PROP_USEROBJUSED
||
2715 prop
== ZFS_PROP_GROUPOBJUSED
|| prop
== ZFS_PROP_PROJECTOBJUSED
) {
2716 propname
= "objused";
2717 if (!nvlist_exists(props
, "objquota"))
2718 (void) nvlist_add_uint64(props
, "objquota", 0);
2719 } else if (prop
== ZFS_PROP_USEROBJQUOTA
||
2720 prop
== ZFS_PROP_GROUPOBJQUOTA
||
2721 prop
== ZFS_PROP_PROJECTOBJQUOTA
) {
2722 propname
= "objquota";
2723 if (!nvlist_exists(props
, "objused"))
2724 (void) nvlist_add_uint64(props
, "objused", 0);
2728 sizeidx
= us_field_index(propname
);
2729 if (sizeidx
>= 0 && sizelen
> cb
->cb_width
[sizeidx
])
2730 cb
->cb_width
[sizeidx
] = sizelen
;
2732 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2739 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2740 size_t *width
, us_node_t
*node
)
2742 nvlist_t
*nvl
= node
->usn_nvl
;
2743 char valstr
[MAXNAMELEN
];
2744 boolean_t first
= B_TRUE
;
2750 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2751 if (!(ustype
& types
))
2754 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2755 nvpair_t
*nvp
= NULL
;
2761 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2762 if (strcmp(nvpair_name(nvp
),
2763 us_field_names
[field
]) == 0)
2767 type
= nvp
== NULL
? DATA_TYPE_UNKNOWN
: nvpair_type(nvp
);
2769 case DATA_TYPE_UINT32
:
2770 (void) nvpair_value_uint32(nvp
, &val32
);
2772 case DATA_TYPE_UINT64
:
2773 (void) nvpair_value_uint64(nvp
, &val64
);
2775 case DATA_TYPE_STRING
:
2776 (void) nvpair_value_string(nvp
, &strval
);
2778 case DATA_TYPE_UNKNOWN
:
2781 (void) fprintf(stderr
, "invalid data type\n");
2786 if (type
== DATA_TYPE_UINT32
)
2787 strval
= (char *)us_type2str(val32
);
2790 if (type
== DATA_TYPE_UINT64
) {
2791 (void) sprintf(valstr
, "%llu",
2792 (u_longlong_t
)val64
);
2798 if (type
== DATA_TYPE_UINT64
) {
2800 (void) sprintf(valstr
, "%llu",
2801 (u_longlong_t
)val64
);
2803 } else if (field
== USFIELD_QUOTA
&&
2807 zfs_nicebytes(val64
, valstr
,
2813 case USFIELD_OBJUSED
:
2814 case USFIELD_OBJQUOTA
:
2815 if (type
== DATA_TYPE_UINT64
) {
2817 (void) sprintf(valstr
, "%llu",
2818 (u_longlong_t
)val64
);
2820 } else if (field
== USFIELD_OBJQUOTA
&&
2824 zfs_nicenum(val64
, valstr
,
2834 (void) printf("\t");
2839 (void) printf("%s", strval
);
2840 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2841 (void) printf("%-*s", (int)width
[field
], strval
);
2843 (void) printf("%*s", (int)width
[field
], strval
);
2849 (void) printf("\n");
2853 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2854 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2862 boolean_t first
= B_TRUE
;
2864 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2865 col
= gettext(us_field_hdr
[field
]);
2866 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2867 (void) printf(first
? "%-*s" : " %-*s",
2868 (int)width
[field
], col
);
2870 (void) printf(first
? "%*s" : " %*s",
2871 (int)width
[field
], col
);
2876 (void) printf("\n");
2879 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2880 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2882 nvlist_free(node
->usn_nvl
);
2887 zfs_do_userspace(int argc
, char **argv
)
2890 zfs_userquota_prop_t p
;
2891 uu_avl_pool_t
*avl_pool
;
2893 uu_avl_walk_t
*walk
;
2895 char deffields
[] = "type,name,used,quota,objused,objquota";
2896 char *ofield
= NULL
;
2897 char *tfield
= NULL
;
2901 boolean_t scripted
= B_FALSE
;
2902 boolean_t prtnum
= B_FALSE
;
2903 boolean_t parsable
= B_FALSE
;
2904 boolean_t sid2posix
= B_FALSE
;
2907 zfs_sort_column_t
*sortcol
= NULL
;
2908 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2912 uu_list_pool_t
*listpool
;
2914 uu_avl_index_t idx
= 0;
2915 uu_list_index_t idx2
= 0;
2920 if (strcmp(argv
[0], "groupspace") == 0) {
2921 /* Toggle default group types */
2922 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2923 } else if (strcmp(argv
[0], "projectspace") == 0) {
2924 types
= USTYPE_PROJ
;
2928 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2931 if (types
== USTYPE_PROJ
) {
2932 (void) fprintf(stderr
,
2933 gettext("invalid option 'n'\n"));
2949 if (zfs_add_sort_column(&sortcol
, optarg
,
2950 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
2951 (void) fprintf(stderr
,
2952 gettext("invalid field '%s'\n"), optarg
);
2957 if (types
== USTYPE_PROJ
) {
2958 (void) fprintf(stderr
,
2959 gettext("invalid option 't'\n"));
2965 if (types
== USTYPE_PROJ
) {
2966 (void) fprintf(stderr
,
2967 gettext("invalid option 'i'\n"));
2973 (void) fprintf(stderr
, gettext("missing argument for "
2974 "'%c' option\n"), optopt
);
2978 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2988 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2992 (void) fprintf(stderr
, gettext("too many arguments\n"));
2996 /* Use default output fields if not specified using -o */
3000 if ((delim
= strchr(ofield
, ',')) != NULL
)
3002 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
3003 (void) fprintf(stderr
, gettext("invalid type '%s' "
3004 "for -o option\n"), ofield
);
3009 } while (delim
!= NULL
);
3010 fields
[cfield
] = USFIELD_LAST
;
3012 /* Override output types (-t option) */
3013 if (tfield
!= NULL
) {
3017 boolean_t found
= B_FALSE
;
3019 if ((delim
= strchr(tfield
, ',')) != NULL
)
3021 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
3023 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
3025 types
|= us_type_bits
[i
];
3030 (void) fprintf(stderr
, gettext("invalid type "
3031 "'%s' for -t option\n"), tfield
);
3036 } while (delim
!= NULL
);
3039 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
3042 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
3043 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
3045 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
3048 /* Always add default sorting columns */
3049 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
3050 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
3052 cb
.cb_sortcol
= sortcol
;
3053 cb
.cb_numname
= prtnum
;
3054 cb
.cb_nicenum
= !parsable
;
3055 cb
.cb_avl_pool
= avl_pool
;
3056 cb
.cb_avl
= avl_tree
;
3057 cb
.cb_sid2posix
= sid2posix
;
3059 for (i
= 0; i
< USFIELD_LAST
; i
++)
3060 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
3062 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
3063 if ((zfs_prop_is_user(p
) &&
3064 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
3065 (zfs_prop_is_group(p
) &&
3066 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))) ||
3067 (zfs_prop_is_project(p
) && types
!= USTYPE_PROJ
))
3071 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
3076 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
3079 us_populated
= B_TRUE
;
3081 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
3082 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
3083 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
3084 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
3086 while (node
!= NULL
) {
3088 node
= uu_avl_next(avl_tree
, node
);
3089 uu_avl_remove(avl_tree
, rmnode
);
3090 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
3091 uu_list_insert(list
, rmnode
, idx2
);
3094 for (node
= uu_list_first(list
); node
!= NULL
;
3095 node
= uu_list_next(list
, node
)) {
3096 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
3098 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
3099 uu_avl_insert(avl_tree
, node
, idx
);
3102 uu_list_destroy(list
);
3103 uu_list_pool_destroy(listpool
);
3105 /* Print and free node nvlist memory */
3106 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
3109 zfs_free_sort_columns(sortcol
);
3111 /* Clean up the AVL tree */
3112 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
3115 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
3116 uu_avl_remove(cb
.cb_avl
, node
);
3120 uu_avl_walk_end(walk
);
3121 uu_avl_destroy(avl_tree
);
3122 uu_avl_pool_destroy(avl_pool
);
3128 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
3129 * [-t type[,...]] [filesystem|volume|snapshot] ...
3131 * -H Scripted mode; elide headers and separate columns by tabs
3132 * -p Display values in parsable (literal) format.
3133 * -r Recurse over all children
3134 * -d Limit recursion by depth.
3135 * -o Control which fields to display.
3136 * -s Specify sort columns, descending order.
3137 * -S Specify sort columns, ascending order.
3138 * -t Control which object types to display.
3140 * When given no arguments, list all filesystems in the system.
3141 * Otherwise, list the specified datasets, optionally recursing down them if
3142 * '-r' is specified.
3144 typedef struct list_cbdata
{
3146 boolean_t cb_literal
;
3147 boolean_t cb_scripted
;
3148 zprop_list_t
*cb_proplist
;
3152 * Given a list of columns to display, output appropriate headers for each one.
3155 print_header(list_cbdata_t
*cb
)
3157 zprop_list_t
*pl
= cb
->cb_proplist
;
3158 char headerbuf
[ZFS_MAXPROPLEN
];
3161 boolean_t first
= B_TRUE
;
3162 boolean_t right_justify
;
3164 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3171 right_justify
= B_FALSE
;
3172 if (pl
->pl_prop
!= ZPROP_INVAL
) {
3173 header
= zfs_prop_column_name(pl
->pl_prop
);
3174 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3176 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
3177 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
3178 headerbuf
[i
] = '\0';
3182 if (pl
->pl_next
== NULL
&& !right_justify
)
3183 (void) printf("%s", header
);
3184 else if (right_justify
)
3185 (void) printf("%*s", (int)pl
->pl_width
, header
);
3187 (void) printf("%-*s", (int)pl
->pl_width
, header
);
3190 (void) printf("\n");
3194 * Given a dataset and a list of fields, print out all the properties according
3195 * to the described layout.
3198 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
3200 zprop_list_t
*pl
= cb
->cb_proplist
;
3201 boolean_t first
= B_TRUE
;
3202 char property
[ZFS_MAXPROPLEN
];
3203 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
3206 boolean_t right_justify
;
3208 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3210 if (cb
->cb_scripted
)
3211 (void) printf("\t");
3218 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
3219 (void) strlcpy(property
, zfs_get_name(zhp
),
3222 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3223 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
3224 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
3225 sizeof (property
), NULL
, NULL
, 0,
3226 cb
->cb_literal
) != 0)
3230 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3231 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
3232 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
3233 property
, sizeof (property
), cb
->cb_literal
) != 0)
3237 right_justify
= B_TRUE
;
3238 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
3239 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
3240 property
, sizeof (property
), cb
->cb_literal
) != 0)
3244 right_justify
= B_TRUE
;
3246 if (nvlist_lookup_nvlist(userprops
,
3247 pl
->pl_user_prop
, &propval
) != 0)
3250 verify(nvlist_lookup_string(propval
,
3251 ZPROP_VALUE
, &propstr
) == 0);
3252 right_justify
= B_FALSE
;
3256 * If this is being called in scripted mode, or if this is the
3257 * last column and it is left-justified, don't include a width
3260 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3261 (void) printf("%s", propstr
);
3262 else if (right_justify
)
3263 (void) printf("%*s", (int)pl
->pl_width
, propstr
);
3265 (void) printf("%-*s", (int)pl
->pl_width
, propstr
);
3268 (void) printf("\n");
3272 * Generic callback function to list a dataset or snapshot.
3275 list_callback(zfs_handle_t
*zhp
, void *data
)
3277 list_cbdata_t
*cbp
= data
;
3279 if (cbp
->cb_first
) {
3280 if (!cbp
->cb_scripted
)
3282 cbp
->cb_first
= B_FALSE
;
3285 print_dataset(zhp
, cbp
);
3291 zfs_do_list(int argc
, char **argv
)
3294 static char default_fields
[] =
3295 "name,used,available,referenced,mountpoint";
3296 int types
= ZFS_TYPE_DATASET
;
3297 boolean_t types_specified
= B_FALSE
;
3298 char *fields
= NULL
;
3299 list_cbdata_t cb
= { 0 };
3303 zfs_sort_column_t
*sortcol
= NULL
;
3304 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3307 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3313 cb
.cb_literal
= B_TRUE
;
3314 flags
|= ZFS_ITER_LITERAL_PROPS
;
3317 limit
= parse_depth(optarg
, &flags
);
3320 flags
|= ZFS_ITER_RECURSE
;
3323 cb
.cb_scripted
= B_TRUE
;
3326 if (zfs_add_sort_column(&sortcol
, optarg
,
3328 (void) fprintf(stderr
,
3329 gettext("invalid property '%s'\n"), optarg
);
3334 if (zfs_add_sort_column(&sortcol
, optarg
,
3336 (void) fprintf(stderr
,
3337 gettext("invalid property '%s'\n"), optarg
);
3343 types_specified
= B_TRUE
;
3344 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3345 while (*optarg
!= '\0') {
3346 static char *type_subopts
[] = { "filesystem",
3347 "volume", "snapshot", "snap", "bookmark",
3350 switch (getsubopt(&optarg
, type_subopts
,
3353 types
|= ZFS_TYPE_FILESYSTEM
;
3356 types
|= ZFS_TYPE_VOLUME
;
3360 types
|= ZFS_TYPE_SNAPSHOT
;
3363 types
|= ZFS_TYPE_BOOKMARK
;
3366 types
= ZFS_TYPE_DATASET
|
3370 (void) fprintf(stderr
,
3371 gettext("invalid type '%s'\n"),
3378 (void) fprintf(stderr
, gettext("missing argument for "
3379 "'%c' option\n"), optopt
);
3383 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3393 fields
= default_fields
;
3396 * If we are only going to list snapshot names and sort by name,
3397 * then we can use faster version.
3399 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3400 flags
|= ZFS_ITER_SIMPLE
;
3403 * If "-o space" and no types were specified, don't display snapshots.
3405 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3406 types
&= ~ZFS_TYPE_SNAPSHOT
;
3409 * If the user specifies '-o all', the zprop_get_list() doesn't
3410 * normally include the name of the dataset. For 'zfs list', we always
3411 * want this property to be first.
3413 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3417 cb
.cb_first
= B_TRUE
;
3419 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3420 limit
, list_callback
, &cb
);
3422 zprop_free_list(cb
.cb_proplist
);
3423 zfs_free_sort_columns(sortcol
);
3425 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3426 (void) fprintf(stderr
, gettext("no datasets available\n"));
3432 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3433 * zfs rename [-f] -p <fs | vol> <fs | vol>
3434 * zfs rename -r <snap> <snap>
3436 * Renames the given dataset to another of the same type.
3438 * The '-p' flag creates all the non-existing ancestors of the target first.
3442 zfs_do_rename(int argc
, char **argv
)
3447 boolean_t recurse
= B_FALSE
;
3448 boolean_t parents
= B_FALSE
;
3449 boolean_t force_unmount
= B_FALSE
;
3452 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3461 force_unmount
= B_TRUE
;
3465 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3474 /* check number of arguments */
3476 (void) fprintf(stderr
, gettext("missing source dataset "
3481 (void) fprintf(stderr
, gettext("missing target dataset "
3486 (void) fprintf(stderr
, gettext("too many arguments\n"));
3490 if (recurse
&& parents
) {
3491 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3496 if (recurse
&& strchr(argv
[0], '@') == 0) {
3497 (void) fprintf(stderr
, gettext("source dataset for recursive "
3498 "rename must be a snapshot\n"));
3502 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3503 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3506 /* If we were asked and the name looks good, try to create ancestors. */
3507 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3508 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3513 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3522 * Promotes the given clone fs to be the parent
3526 zfs_do_promote(int argc
, char **argv
)
3532 if (argc
> 1 && argv
[1][0] == '-') {
3533 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3538 /* check number of arguments */
3540 (void) fprintf(stderr
, gettext("missing clone filesystem"
3545 (void) fprintf(stderr
, gettext("too many arguments\n"));
3549 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3553 ret
= (zfs_promote(zhp
) != 0);
3561 * zfs rollback [-rRf] <snapshot>
3563 * -r Delete any intervening snapshots before doing rollback
3564 * -R Delete any snapshots and their clones
3565 * -f ignored for backwards compatibility
3567 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3568 * since then and making it the active dataset. If more recent snapshots exist,
3569 * the command will complain unless the '-r' flag is given.
3571 typedef struct rollback_cbdata
{
3577 boolean_t cb_recurse
;
3578 } rollback_cbdata_t
;
3581 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3583 rollback_cbdata_t
*cbp
= data
;
3585 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3586 (void) fprintf(stderr
, gettext("cannot rollback to "
3587 "'%s': clones of previous snapshots exist\n"),
3589 (void) fprintf(stderr
, gettext("use '-R' to "
3590 "force deletion of the following clones and "
3596 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3604 * Report any snapshots more recent than the one specified. Used when '-r' is
3605 * not specified. We reuse this same callback for the snapshot dependents - if
3606 * 'cb_dependent' is set, then this is a dependent and we should report it
3607 * without checking the transaction group.
3610 rollback_check(zfs_handle_t
*zhp
, void *data
)
3612 rollback_cbdata_t
*cbp
= data
;
3614 if (cbp
->cb_doclones
) {
3619 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3620 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3621 (void) fprintf(stderr
, gettext("cannot "
3622 "rollback to '%s': more recent snapshots "
3623 "or bookmarks exist\n"),
3625 (void) fprintf(stderr
, gettext("use '-r' to "
3626 "force deletion of the following "
3627 "snapshots and bookmarks:\n"));
3632 if (cbp
->cb_recurse
) {
3633 if (zfs_iter_dependents(zhp
, B_TRUE
,
3634 rollback_check_dependent
, cbp
) != 0) {
3639 (void) fprintf(stderr
, "%s\n",
3648 zfs_do_rollback(int argc
, char **argv
)
3652 boolean_t force
= B_FALSE
;
3653 rollback_cbdata_t cb
= { 0 };
3654 zfs_handle_t
*zhp
, *snap
;
3655 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3659 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3672 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3681 /* check number of arguments */
3683 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3687 (void) fprintf(stderr
, gettext("too many arguments\n"));
3691 /* open the snapshot */
3692 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3695 /* open the parent dataset */
3696 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3697 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3699 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3705 * Check for more recent snapshots and/or clones based on the presence
3708 cb
.cb_target
= argv
[0];
3709 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3710 cb
.cb_first
= B_TRUE
;
3712 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, rollback_check
, &cb
)) != 0)
3714 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
3717 if ((ret
= cb
.cb_error
) != 0)
3721 * Rollback parent to the given snapshot.
3723 ret
= zfs_rollback(zhp
, snap
, force
);
3736 * zfs set property=value ... { fs | snap | vol } ...
3738 * Sets the given properties for all datasets specified on the command line.
3742 set_callback(zfs_handle_t
*zhp
, void *data
)
3744 nvlist_t
*props
= data
;
3746 if (zfs_prop_set_list(zhp
, props
) != 0) {
3747 switch (libzfs_errno(g_zfs
)) {
3748 case EZFS_MOUNTFAILED
:
3749 (void) fprintf(stderr
, gettext("property may be set "
3750 "but unable to remount filesystem\n"));
3752 case EZFS_SHARENFSFAILED
:
3753 (void) fprintf(stderr
, gettext("property may be set "
3754 "but unable to reshare filesystem\n"));
3763 zfs_do_set(int argc
, char **argv
)
3765 nvlist_t
*props
= NULL
;
3766 int ds_start
= -1; /* argv idx of first dataset arg */
3770 /* check for options */
3771 if (argc
> 1 && argv
[1][0] == '-') {
3772 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3777 /* check number of arguments */
3779 (void) fprintf(stderr
, gettext("missing arguments\n"));
3783 if (strchr(argv
[1], '=') == NULL
) {
3784 (void) fprintf(stderr
, gettext("missing property=value "
3787 (void) fprintf(stderr
, gettext("missing dataset "
3793 /* validate argument order: prop=val args followed by dataset args */
3794 for (i
= 1; i
< argc
; i
++) {
3795 if (strchr(argv
[i
], '=') != NULL
) {
3797 /* out-of-order prop=val argument */
3798 (void) fprintf(stderr
, gettext("invalid "
3799 "argument order\n"));
3802 } else if (ds_start
< 0) {
3807 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
3811 /* Populate a list of property settings */
3812 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3814 for (i
= 1; i
< ds_start
; i
++) {
3815 if (!parseprop(props
, argv
[i
])) {
3821 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
3822 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, props
);
3829 typedef struct snap_cbdata
{
3831 boolean_t sd_recursive
;
3832 const char *sd_snapname
;
3836 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3838 snap_cbdata_t
*sd
= arg
;
3843 if (sd
->sd_recursive
&&
3844 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
3849 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3852 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3855 if (sd
->sd_recursive
)
3856 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3862 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3864 * Creates a snapshot with the given name. While functionally equivalent to
3865 * 'zfs create', it is a separate command to differentiate intent.
3868 zfs_do_snapshot(int argc
, char **argv
)
3873 snap_cbdata_t sd
= { 0 };
3874 boolean_t multiple_snaps
= B_FALSE
;
3876 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3878 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3882 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3885 if (!parseprop(props
, optarg
)) {
3886 nvlist_free(sd
.sd_nvl
);
3892 sd
.sd_recursive
= B_TRUE
;
3893 multiple_snaps
= B_TRUE
;
3896 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3905 /* check number of arguments */
3907 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3912 multiple_snaps
= B_TRUE
;
3913 for (; argc
> 0; argc
--, argv
++) {
3917 atp
= strchr(argv
[0], '@');
3921 sd
.sd_snapname
= atp
+ 1;
3922 zhp
= zfs_open(g_zfs
, argv
[0],
3923 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3926 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3930 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3931 nvlist_free(sd
.sd_nvl
);
3933 if (ret
!= 0 && multiple_snaps
)
3934 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3938 nvlist_free(sd
.sd_nvl
);
3945 * Send a backup stream to stdout.
3948 zfs_do_send(int argc
, char **argv
)
3950 char *fromname
= NULL
;
3951 char *toname
= NULL
;
3952 char *resume_token
= NULL
;
3955 sendflags_t flags
= { 0 };
3957 nvlist_t
*dbgnv
= NULL
;
3958 boolean_t extraverbose
= B_FALSE
;
3960 struct option long_options
[] = {
3961 {"replicate", no_argument
, NULL
, 'R'},
3962 {"props", no_argument
, NULL
, 'p'},
3963 {"parsable", no_argument
, NULL
, 'P'},
3964 {"dedup", no_argument
, NULL
, 'D'},
3965 {"verbose", no_argument
, NULL
, 'v'},
3966 {"dryrun", no_argument
, NULL
, 'n'},
3967 {"large-block", no_argument
, NULL
, 'L'},
3968 {"embed", no_argument
, NULL
, 'e'},
3969 {"resume", required_argument
, NULL
, 't'},
3970 {"compressed", no_argument
, NULL
, 'c'},
3971 {"raw", no_argument
, NULL
, 'w'},
3972 {"backup", no_argument
, NULL
, 'b'},
3977 while ((c
= getopt_long(argc
, argv
, ":i:I:RDpvnPLet:cwb", long_options
,
3989 flags
.doall
= B_TRUE
;
3992 flags
.replicate
= B_TRUE
;
3995 flags
.props
= B_TRUE
;
3998 flags
.backup
= B_TRUE
;
4001 flags
.parsable
= B_TRUE
;
4002 flags
.verbose
= B_TRUE
;
4006 extraverbose
= B_TRUE
;
4007 flags
.verbose
= B_TRUE
;
4008 flags
.progress
= B_TRUE
;
4011 flags
.dedup
= B_TRUE
;
4014 flags
.dryrun
= B_TRUE
;
4017 flags
.largeblock
= B_TRUE
;
4020 flags
.embed_data
= B_TRUE
;
4023 resume_token
= optarg
;
4026 flags
.compress
= B_TRUE
;
4030 flags
.compress
= B_TRUE
;
4031 flags
.embed_data
= B_TRUE
;
4032 flags
.largeblock
= B_TRUE
;
4036 * If a parameter was not passed, optopt contains the
4037 * value that would normally lead us into the
4038 * appropriate case statement. If it's > 256, then this
4039 * must be a longopt and we should look at argv to get
4040 * the string. Otherwise it's just the character, so we
4041 * should use it directly.
4043 if (optopt
<= UINT8_MAX
) {
4044 (void) fprintf(stderr
,
4045 gettext("missing argument for '%c' "
4046 "option\n"), optopt
);
4048 (void) fprintf(stderr
,
4049 gettext("missing argument for '%s' "
4050 "option\n"), argv
[optind
- 1]);
4058 * If an invalid flag was passed, optopt contains the
4059 * character if it was a short flag, or 0 if it was a
4063 (void) fprintf(stderr
,
4064 gettext("invalid option '%c'\n"), optopt
);
4066 (void) fprintf(stderr
,
4067 gettext("invalid option '%s'\n"),
4078 if (resume_token
!= NULL
) {
4079 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
4080 flags
.backup
|| flags
.dedup
) {
4081 (void) fprintf(stderr
,
4082 gettext("invalid flags combined with -t\n"));
4086 (void) fprintf(stderr
, gettext("no additional "
4087 "arguments are permitted with -t\n"));
4092 (void) fprintf(stderr
,
4093 gettext("missing snapshot argument\n"));
4097 (void) fprintf(stderr
, gettext("too many arguments\n"));
4102 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
4103 (void) fprintf(stderr
,
4104 gettext("Error: Stream can not be written to a terminal.\n"
4105 "You must redirect standard output.\n"));
4109 if (resume_token
!= NULL
) {
4110 return (zfs_send_resume(g_zfs
, &flags
, STDOUT_FILENO
,
4115 * Special case sending a filesystem, or from a bookmark.
4117 if (strchr(argv
[0], '@') == NULL
||
4118 (fromname
&& strchr(fromname
, '#') != NULL
)) {
4119 char frombuf
[ZFS_MAX_DATASET_NAME_LEN
];
4121 if (flags
.replicate
|| flags
.doall
|| flags
.props
||
4122 flags
.backup
|| flags
.dedup
||
4123 (strchr(argv
[0], '@') == NULL
&&
4124 (flags
.dryrun
|| flags
.verbose
|| flags
.progress
))) {
4125 (void) fprintf(stderr
, gettext("Error: "
4126 "Unsupported flag with filesystem or bookmark.\n"));
4130 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
4134 if (fromname
!= NULL
&&
4135 (fromname
[0] == '#' || fromname
[0] == '@')) {
4137 * Incremental source name begins with # or @.
4138 * Default to same fs as target.
4140 (void) strlcpy(frombuf
, argv
[0], sizeof (frombuf
));
4141 cp
= strchr(frombuf
, '@');
4144 (void) strlcat(frombuf
, fromname
, sizeof (frombuf
));
4147 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, flags
);
4152 cp
= strchr(argv
[0], '@');
4155 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4160 * If they specified the full path to the snapshot, chop off
4161 * everything except the short name of the snapshot, but special
4162 * case if they specify the origin.
4164 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
4165 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
4168 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
4169 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
4171 if (strcmp(origin
, fromname
) == 0) {
4173 flags
.fromorigin
= B_TRUE
;
4176 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
4177 (void) fprintf(stderr
,
4178 gettext("incremental source must be "
4179 "in same filesystem\n"));
4183 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
4184 (void) fprintf(stderr
,
4185 gettext("invalid incremental source\n"));
4191 if (flags
.replicate
&& fromname
== NULL
)
4192 flags
.doall
= B_TRUE
;
4194 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
4195 extraverbose
? &dbgnv
: NULL
);
4197 if (extraverbose
&& dbgnv
!= NULL
) {
4199 * dump_nvlist prints to stdout, but that's been
4200 * redirected to a file. Make it print to stderr
4203 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
4204 dump_nvlist(dbgnv
, 0);
4213 * Restore a backup stream from stdin.
4216 zfs_do_receive(int argc
, char **argv
)
4219 recvflags_t flags
= { 0 };
4220 boolean_t abort_resumable
= B_FALSE
;
4223 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
4227 while ((c
= getopt(argc
, argv
, ":o:x:denuvFsA")) != -1) {
4230 if (!parseprop(props
, optarg
)) {
4236 if (!parsepropname(props
, optarg
)) {
4242 flags
.isprefix
= B_TRUE
;
4245 flags
.isprefix
= B_TRUE
;
4246 flags
.istail
= B_TRUE
;
4249 flags
.dryrun
= B_TRUE
;
4252 flags
.nomount
= B_TRUE
;
4255 flags
.verbose
= B_TRUE
;
4258 flags
.resumable
= B_TRUE
;
4261 flags
.force
= B_TRUE
;
4264 abort_resumable
= B_TRUE
;
4267 (void) fprintf(stderr
, gettext("missing argument for "
4268 "'%c' option\n"), optopt
);
4272 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4281 /* check number of arguments */
4283 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4287 (void) fprintf(stderr
, gettext("too many arguments\n"));
4291 if (abort_resumable
) {
4292 if (flags
.isprefix
|| flags
.istail
|| flags
.dryrun
||
4293 flags
.resumable
|| flags
.nomount
) {
4294 (void) fprintf(stderr
, gettext("invalid option\n"));
4298 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
4299 (void) snprintf(namebuf
, sizeof (namebuf
),
4300 "%s/%%recv", argv
[0]);
4302 if (zfs_dataset_exists(g_zfs
, namebuf
,
4303 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) {
4304 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4305 namebuf
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4310 err
= zfs_destroy(zhp
, B_FALSE
);
4313 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4314 argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4317 if (!zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) ||
4318 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
4319 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == -1) {
4320 (void) fprintf(stderr
,
4321 gettext("'%s' does not have any "
4322 "resumable receive state to abort\n"),
4328 err
= zfs_destroy(zhp
, B_FALSE
);
4335 if (isatty(STDIN_FILENO
)) {
4336 (void) fprintf(stderr
,
4337 gettext("Error: Backup stream can not be read "
4338 "from a terminal.\n"
4339 "You must redirect standard input.\n"));
4343 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
4350 * allow/unallow stuff
4352 /* copied from zfs/sys/dsl_deleg.h */
4353 #define ZFS_DELEG_PERM_CREATE "create"
4354 #define ZFS_DELEG_PERM_DESTROY "destroy"
4355 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4356 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4357 #define ZFS_DELEG_PERM_CLONE "clone"
4358 #define ZFS_DELEG_PERM_PROMOTE "promote"
4359 #define ZFS_DELEG_PERM_RENAME "rename"
4360 #define ZFS_DELEG_PERM_MOUNT "mount"
4361 #define ZFS_DELEG_PERM_SHARE "share"
4362 #define ZFS_DELEG_PERM_SEND "send"
4363 #define ZFS_DELEG_PERM_RECEIVE "receive"
4364 #define ZFS_DELEG_PERM_ALLOW "allow"
4365 #define ZFS_DELEG_PERM_USERPROP "userprop"
4366 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4367 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4368 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4369 #define ZFS_DELEG_PERM_USERUSED "userused"
4370 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4371 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
4372 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
4373 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
4374 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
4376 #define ZFS_DELEG_PERM_HOLD "hold"
4377 #define ZFS_DELEG_PERM_RELEASE "release"
4378 #define ZFS_DELEG_PERM_DIFF "diff"
4379 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4380 #define ZFS_DELEG_PERM_REMAP "remap"
4381 #define ZFS_DELEG_PERM_LOAD_KEY "load-key"
4382 #define ZFS_DELEG_PERM_CHANGE_KEY "change-key"
4384 #define ZFS_DELEG_PERM_PROJECTUSED "projectused"
4385 #define ZFS_DELEG_PERM_PROJECTQUOTA "projectquota"
4386 #define ZFS_DELEG_PERM_PROJECTOBJUSED "projectobjused"
4387 #define ZFS_DELEG_PERM_PROJECTOBJQUOTA "projectobjquota"
4389 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4391 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
4392 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
4393 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
4394 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
4395 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
4396 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
4397 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
4398 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
4399 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
4400 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
4401 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
4402 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
4403 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
4404 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
4405 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
4406 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
4407 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
4408 { ZFS_DELEG_PERM_REMAP
, ZFS_DELEG_NOTE_REMAP
},
4409 { ZFS_DELEG_PERM_LOAD_KEY
, ZFS_DELEG_NOTE_LOAD_KEY
},
4410 { ZFS_DELEG_PERM_CHANGE_KEY
, ZFS_DELEG_NOTE_CHANGE_KEY
},
4412 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
4413 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
4414 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
4415 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
4416 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
4417 { ZFS_DELEG_PERM_USEROBJQUOTA
, ZFS_DELEG_NOTE_USEROBJQUOTA
},
4418 { ZFS_DELEG_PERM_USEROBJUSED
, ZFS_DELEG_NOTE_USEROBJUSED
},
4419 { ZFS_DELEG_PERM_GROUPOBJQUOTA
, ZFS_DELEG_NOTE_GROUPOBJQUOTA
},
4420 { ZFS_DELEG_PERM_GROUPOBJUSED
, ZFS_DELEG_NOTE_GROUPOBJUSED
},
4421 { ZFS_DELEG_PERM_PROJECTUSED
, ZFS_DELEG_NOTE_PROJECTUSED
},
4422 { ZFS_DELEG_PERM_PROJECTQUOTA
, ZFS_DELEG_NOTE_PROJECTQUOTA
},
4423 { ZFS_DELEG_PERM_PROJECTOBJUSED
, ZFS_DELEG_NOTE_PROJECTOBJUSED
},
4424 { ZFS_DELEG_PERM_PROJECTOBJQUOTA
, ZFS_DELEG_NOTE_PROJECTOBJQUOTA
},
4425 { NULL
, ZFS_DELEG_NOTE_NONE
}
4428 /* permission structure */
4429 typedef struct deleg_perm
{
4430 zfs_deleg_who_type_t dp_who_type
;
4431 const char *dp_name
;
4433 boolean_t dp_descend
;
4437 typedef struct deleg_perm_node
{
4438 deleg_perm_t dpn_perm
;
4440 uu_avl_node_t dpn_avl_node
;
4441 } deleg_perm_node_t
;
4443 typedef struct fs_perm fs_perm_t
;
4445 /* permissions set */
4446 typedef struct who_perm
{
4447 zfs_deleg_who_type_t who_type
;
4448 const char *who_name
; /* id */
4449 char who_ug_name
[256]; /* user/group name */
4450 fs_perm_t
*who_fsperm
; /* uplink */
4452 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
4456 typedef struct who_perm_node
{
4457 who_perm_t who_perm
;
4458 uu_avl_node_t who_avl_node
;
4461 typedef struct fs_perm_set fs_perm_set_t
;
4462 /* fs permissions */
4464 const char *fsp_name
;
4466 uu_avl_t
*fsp_sc_avl
; /* sets,create */
4467 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
4469 fs_perm_set_t
*fsp_set
; /* uplink */
4473 typedef struct fs_perm_node
{
4474 fs_perm_t fspn_fsperm
;
4477 uu_list_node_t fspn_list_node
;
4480 /* top level structure */
4481 struct fs_perm_set
{
4482 uu_list_pool_t
*fsps_list_pool
;
4483 uu_list_t
*fsps_list
; /* list of fs_perms */
4485 uu_avl_pool_t
*fsps_named_set_avl_pool
;
4486 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
4487 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
4490 static inline const char *
4491 deleg_perm_type(zfs_deleg_note_t note
)
4497 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4498 case ZFS_DELEG_NOTE_GROUPUSED
:
4499 case ZFS_DELEG_NOTE_USERPROP
:
4500 case ZFS_DELEG_NOTE_USERQUOTA
:
4501 case ZFS_DELEG_NOTE_USERUSED
:
4502 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
4503 case ZFS_DELEG_NOTE_USEROBJUSED
:
4504 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
4505 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
4506 case ZFS_DELEG_NOTE_PROJECTUSED
:
4507 case ZFS_DELEG_NOTE_PROJECTQUOTA
:
4508 case ZFS_DELEG_NOTE_PROJECTOBJUSED
:
4509 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA
:
4511 return (gettext("other"));
4513 return (gettext("subcommand"));
4518 who_type2weight(zfs_deleg_who_type_t who_type
)
4522 case ZFS_DELEG_NAMED_SET_SETS
:
4523 case ZFS_DELEG_NAMED_SET
:
4526 case ZFS_DELEG_CREATE_SETS
:
4527 case ZFS_DELEG_CREATE
:
4530 case ZFS_DELEG_USER_SETS
:
4531 case ZFS_DELEG_USER
:
4534 case ZFS_DELEG_GROUP_SETS
:
4535 case ZFS_DELEG_GROUP
:
4538 case ZFS_DELEG_EVERYONE_SETS
:
4539 case ZFS_DELEG_EVERYONE
:
4551 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4553 const who_perm_node_t
*l
= larg
;
4554 const who_perm_node_t
*r
= rarg
;
4555 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
4556 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
4557 int lweight
= who_type2weight(ltype
);
4558 int rweight
= who_type2weight(rtype
);
4559 int res
= lweight
- rweight
;
4561 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
4562 ZFS_MAX_DELEG_NAME
-1);
4574 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4576 const deleg_perm_node_t
*l
= larg
;
4577 const deleg_perm_node_t
*r
= rarg
;
4578 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
4579 ZFS_MAX_DELEG_NAME
-1);
4591 fs_perm_set_init(fs_perm_set_t
*fspset
)
4593 bzero(fspset
, sizeof (fs_perm_set_t
));
4595 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4596 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4597 NULL
, UU_DEFAULT
)) == NULL
)
4599 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4600 UU_DEFAULT
)) == NULL
)
4603 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4604 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4605 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4606 UU_DEFAULT
)) == NULL
)
4609 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4610 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4611 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4612 UU_DEFAULT
)) == NULL
)
4615 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4616 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4617 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4622 static inline void fs_perm_fini(fs_perm_t
*);
4623 static inline void who_perm_fini(who_perm_t
*);
4626 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4628 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4630 while (node
!= NULL
) {
4631 fs_perm_node_t
*next_node
=
4632 uu_list_next(fspset
->fsps_list
, node
);
4633 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4634 fs_perm_fini(fsperm
);
4635 uu_list_remove(fspset
->fsps_list
, node
);
4640 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4641 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4642 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4646 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4649 deleg_perm
->dp_who_type
= type
;
4650 deleg_perm
->dp_name
= name
;
4654 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4655 zfs_deleg_who_type_t type
, const char *name
)
4657 uu_avl_pool_t
*pool
;
4658 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4660 bzero(who_perm
, sizeof (who_perm_t
));
4662 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4663 UU_DEFAULT
)) == NULL
)
4666 who_perm
->who_type
= type
;
4667 who_perm
->who_name
= name
;
4668 who_perm
->who_fsperm
= fsperm
;
4672 who_perm_fini(who_perm_t
*who_perm
)
4674 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4676 while (node
!= NULL
) {
4677 deleg_perm_node_t
*next_node
=
4678 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4680 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4685 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4689 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4691 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4692 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4694 bzero(fsperm
, sizeof (fs_perm_t
));
4696 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4700 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4704 fsperm
->fsp_set
= fspset
;
4705 fsperm
->fsp_name
= fsname
;
4709 fs_perm_fini(fs_perm_t
*fsperm
)
4711 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4712 while (node
!= NULL
) {
4713 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4715 who_perm_t
*who_perm
= &node
->who_perm
;
4716 who_perm_fini(who_perm
);
4717 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4722 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4723 while (node
!= NULL
) {
4724 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4726 who_perm_t
*who_perm
= &node
->who_perm
;
4727 who_perm_fini(who_perm
);
4728 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4733 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4734 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4738 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4739 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4741 uu_avl_index_t idx
= 0;
4743 deleg_perm_node_t
*found_node
= NULL
;
4744 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4746 deleg_perm_init(deleg_perm
, who_type
, name
);
4748 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4750 uu_avl_insert(avl
, node
, idx
);
4753 deleg_perm
= &node
->dpn_perm
;
4758 case ZFS_DELEG_LOCAL
:
4759 deleg_perm
->dp_local
= B_TRUE
;
4761 case ZFS_DELEG_DESCENDENT
:
4762 deleg_perm
->dp_descend
= B_TRUE
;
4767 assert(B_FALSE
); /* invalid locality */
4772 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4774 nvpair_t
*nvp
= NULL
;
4775 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4776 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4777 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4779 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4780 const char *name
= nvpair_name(nvp
);
4781 data_type_t type
= nvpair_type(nvp
);
4782 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4783 deleg_perm_node_t
*node
=
4784 safe_malloc(sizeof (deleg_perm_node_t
));
4786 VERIFY(type
== DATA_TYPE_BOOLEAN
);
4788 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4789 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4796 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4798 nvpair_t
*nvp
= NULL
;
4799 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4801 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4802 nvlist_t
*nvl2
= NULL
;
4803 const char *name
= nvpair_name(nvp
);
4804 uu_avl_t
*avl
= NULL
;
4805 uu_avl_pool_t
*avl_pool
= NULL
;
4806 zfs_deleg_who_type_t perm_type
= name
[0];
4807 char perm_locality
= name
[1];
4808 const char *perm_name
= name
+ 3;
4809 boolean_t is_set
= B_TRUE
;
4810 who_perm_t
*who_perm
= NULL
;
4812 assert('$' == name
[2]);
4814 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4817 switch (perm_type
) {
4818 case ZFS_DELEG_CREATE
:
4819 case ZFS_DELEG_CREATE_SETS
:
4820 case ZFS_DELEG_NAMED_SET
:
4821 case ZFS_DELEG_NAMED_SET_SETS
:
4822 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4823 avl
= fsperm
->fsp_sc_avl
;
4825 case ZFS_DELEG_USER
:
4826 case ZFS_DELEG_USER_SETS
:
4827 case ZFS_DELEG_GROUP
:
4828 case ZFS_DELEG_GROUP_SETS
:
4829 case ZFS_DELEG_EVERYONE
:
4830 case ZFS_DELEG_EVERYONE_SETS
:
4831 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4832 avl
= fsperm
->fsp_uge_avl
;
4836 assert(!"unhandled zfs_deleg_who_type_t");
4840 who_perm_node_t
*found_node
= NULL
;
4841 who_perm_node_t
*node
= safe_malloc(
4842 sizeof (who_perm_node_t
));
4843 who_perm
= &node
->who_perm
;
4844 uu_avl_index_t idx
= 0;
4846 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4847 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4849 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4851 if (avl
== fsperm
->fsp_uge_avl
) {
4853 struct passwd
*p
= NULL
;
4854 struct group
*g
= NULL
;
4855 const char *nice_name
= NULL
;
4857 switch (perm_type
) {
4858 case ZFS_DELEG_USER_SETS
:
4859 case ZFS_DELEG_USER
:
4860 rid
= atoi(perm_name
);
4863 nice_name
= p
->pw_name
;
4865 case ZFS_DELEG_GROUP_SETS
:
4866 case ZFS_DELEG_GROUP
:
4867 rid
= atoi(perm_name
);
4870 nice_name
= g
->gr_name
;
4877 if (nice_name
!= NULL
)
4879 node
->who_perm
.who_ug_name
,
4883 uu_avl_insert(avl
, node
, idx
);
4886 who_perm
= &node
->who_perm
;
4889 VERIFY3P(who_perm
, !=, NULL
);
4890 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4897 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4899 nvpair_t
*nvp
= NULL
;
4900 uu_avl_index_t idx
= 0;
4902 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4903 nvlist_t
*nvl2
= NULL
;
4904 const char *fsname
= nvpair_name(nvp
);
4905 data_type_t type
= nvpair_type(nvp
);
4906 fs_perm_t
*fsperm
= NULL
;
4907 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4911 fsperm
= &node
->fspn_fsperm
;
4913 VERIFY(DATA_TYPE_NVLIST
== type
);
4915 uu_list_node_init(node
, &node
->fspn_list_node
,
4916 fspset
->fsps_list_pool
);
4918 idx
= uu_list_numnodes(fspset
->fsps_list
);
4919 fs_perm_init(fsperm
, fspset
, fsname
);
4921 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4924 (void) parse_fs_perm(fsperm
, nvl2
);
4926 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4932 static inline const char *
4933 deleg_perm_comment(zfs_deleg_note_t note
)
4935 const char *str
= "";
4940 case ZFS_DELEG_NOTE_ALLOW
:
4941 str
= gettext("Must also have the permission that is being"
4942 "\n\t\t\t\tallowed");
4944 case ZFS_DELEG_NOTE_CLONE
:
4945 str
= gettext("Must also have the 'create' ability and 'mount'"
4946 "\n\t\t\t\tability in the origin file system");
4948 case ZFS_DELEG_NOTE_CREATE
:
4949 str
= gettext("Must also have the 'mount' ability");
4951 case ZFS_DELEG_NOTE_DESTROY
:
4952 str
= gettext("Must also have the 'mount' ability");
4954 case ZFS_DELEG_NOTE_DIFF
:
4955 str
= gettext("Allows lookup of paths within a dataset;"
4956 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4957 "\n\t\t\t\tin order to use zfs diff");
4959 case ZFS_DELEG_NOTE_HOLD
:
4960 str
= gettext("Allows adding a user hold to a snapshot");
4962 case ZFS_DELEG_NOTE_MOUNT
:
4963 str
= gettext("Allows mount/umount of ZFS datasets");
4965 case ZFS_DELEG_NOTE_PROMOTE
:
4966 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4967 " 'promote' ability in the origin file system");
4969 case ZFS_DELEG_NOTE_RECEIVE
:
4970 str
= gettext("Must also have the 'mount' and 'create'"
4973 case ZFS_DELEG_NOTE_RELEASE
:
4974 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4975 "might destroy the snapshot");
4977 case ZFS_DELEG_NOTE_RENAME
:
4978 str
= gettext("Must also have the 'mount' and 'create'"
4979 "\n\t\t\t\tability in the new parent");
4981 case ZFS_DELEG_NOTE_ROLLBACK
:
4984 case ZFS_DELEG_NOTE_SEND
:
4987 case ZFS_DELEG_NOTE_SHARE
:
4988 str
= gettext("Allows sharing file systems over NFS or SMB"
4989 "\n\t\t\t\tprotocols");
4991 case ZFS_DELEG_NOTE_SNAPSHOT
:
4994 case ZFS_DELEG_NOTE_LOAD_KEY
:
4995 str
= gettext("Allows loading or unloading an encryption key");
4997 case ZFS_DELEG_NOTE_CHANGE_KEY
:
4998 str
= gettext("Allows changing or adding an encryption key");
5001 * case ZFS_DELEG_NOTE_VSCAN:
5002 * str = gettext("");
5006 case ZFS_DELEG_NOTE_GROUPQUOTA
:
5007 str
= gettext("Allows accessing any groupquota@... property");
5009 case ZFS_DELEG_NOTE_GROUPUSED
:
5010 str
= gettext("Allows reading any groupused@... property");
5012 case ZFS_DELEG_NOTE_USERPROP
:
5013 str
= gettext("Allows changing any user property");
5015 case ZFS_DELEG_NOTE_USERQUOTA
:
5016 str
= gettext("Allows accessing any userquota@... property");
5018 case ZFS_DELEG_NOTE_USERUSED
:
5019 str
= gettext("Allows reading any userused@... property");
5021 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
5022 str
= gettext("Allows accessing any userobjquota@... property");
5024 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
5025 str
= gettext("Allows accessing any \n\t\t\t\t"
5026 "groupobjquota@... property");
5028 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
5029 str
= gettext("Allows reading any groupobjused@... property");
5031 case ZFS_DELEG_NOTE_USEROBJUSED
:
5032 str
= gettext("Allows reading any userobjused@... property");
5034 case ZFS_DELEG_NOTE_PROJECTQUOTA
:
5035 str
= gettext("Allows accessing any projectquota@... property");
5037 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA
:
5038 str
= gettext("Allows accessing any \n\t\t\t\t"
5039 "projectobjquota@... property");
5041 case ZFS_DELEG_NOTE_PROJECTUSED
:
5042 str
= gettext("Allows reading any projectused@... property");
5044 case ZFS_DELEG_NOTE_PROJECTOBJUSED
:
5045 str
= gettext("Allows accessing any \n\t\t\t\t"
5046 "projectobjused@... property");
5064 boolean_t recursive
; /* unallow only */
5065 boolean_t prt_usage
;
5067 boolean_t prt_perms
;
5070 const char *dataset
;
5074 prop_cmp(const void *a
, const void *b
)
5076 const char *str1
= *(const char **)a
;
5077 const char *str2
= *(const char **)b
;
5078 return (strcmp(str1
, str2
));
5082 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
5084 const char *opt_desc
[] = {
5085 "-h", gettext("show this help message and exit"),
5086 "-l", gettext("set permission locally"),
5087 "-d", gettext("set permission for descents"),
5088 "-u", gettext("set permission for user"),
5089 "-g", gettext("set permission for group"),
5090 "-e", gettext("set permission for everyone"),
5091 "-c", gettext("set create time permission"),
5092 "-s", gettext("define permission set"),
5094 "-r", gettext("remove permissions recursively"),
5096 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
5097 size_t allow_size
= unallow_size
- 2;
5098 const char *props
[ZFS_NUM_PROPS
];
5101 FILE *fp
= requested
? stdout
: stderr
;
5102 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
5103 const char *fmt
= gettext("%-16s %-14s\t%s\n");
5105 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
5107 (void) fprintf(fp
, gettext("Options:\n"));
5108 for (i
= 0; i
< (un
? unallow_size
: allow_size
); i
+= 2) {
5109 const char *opt
= opt_desc
[i
];
5110 const char *optdsc
= opt_desc
[i
+ 1];
5111 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
5114 (void) fprintf(fp
, gettext("\nThe following permissions are "
5116 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
5118 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
5119 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
5120 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
5121 const char *perm_type
= deleg_perm_type(perm_note
);
5122 const char *perm_comment
= deleg_perm_comment(perm_note
);
5123 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
5126 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
5127 zprop_desc_t
*pd
= &pdtbl
[i
];
5128 if (pd
->pd_visible
!= B_TRUE
)
5131 if (pd
->pd_attr
== PROP_READONLY
)
5134 props
[count
++] = pd
->pd_name
;
5136 props
[count
] = NULL
;
5138 qsort(props
, count
, sizeof (char *), prop_cmp
);
5140 for (i
= 0; i
< count
; i
++)
5141 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
5144 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
5146 exit(requested
? 0 : 2);
5149 static inline const char *
5150 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
5153 if (un
&& argc
== expected_argc
- 1)
5155 else if (argc
== expected_argc
)
5156 *permsp
= argv
[argc
- 2];
5158 allow_usage(un
, B_FALSE
,
5159 gettext("wrong number of parameters\n"));
5161 return (argv
[argc
- 1]);
5165 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
5167 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
5168 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
5169 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
5170 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
5173 allow_usage(un
, B_FALSE
,
5174 gettext("-u, -g, and -e are mutually exclusive\n"));
5176 if (opts
->prt_usage
) {
5177 if (argc
== 0 && all_sum
== 0)
5178 allow_usage(un
, B_TRUE
, NULL
);
5185 allow_usage(un
, B_FALSE
,
5186 gettext("invalid options combined with -s\n"));
5188 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
5189 if (argv
[0][0] != '@')
5190 allow_usage(un
, B_FALSE
,
5191 gettext("invalid set name: missing '@' prefix\n"));
5192 opts
->who
= argv
[0];
5193 } else if (opts
->create
) {
5194 if (ldcsuge_sum
> 1)
5195 allow_usage(un
, B_FALSE
,
5196 gettext("invalid options combined with -c\n"));
5197 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5198 } else if (opts
->everyone
) {
5200 allow_usage(un
, B_FALSE
,
5201 gettext("invalid options combined with -e\n"));
5202 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5203 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
5205 opts
->everyone
= B_TRUE
;
5208 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5209 } else if (argc
== 1 && !un
) {
5210 opts
->prt_perms
= B_TRUE
;
5211 opts
->dataset
= argv
[argc
-1];
5213 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
5214 opts
->who
= argv
[0];
5217 if (!opts
->local
&& !opts
->descend
) {
5218 opts
->local
= B_TRUE
;
5219 opts
->descend
= B_TRUE
;
5224 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
5225 const char *who
, char *perms
, nvlist_t
*top_nvl
)
5228 char ld
[2] = { '\0', '\0' };
5229 char who_buf
[MAXNAMELEN
+ 32];
5230 char base_type
= '\0';
5231 char set_type
= '\0';
5232 nvlist_t
*base_nvl
= NULL
;
5233 nvlist_t
*set_nvl
= NULL
;
5236 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
5238 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
5242 case ZFS_DELEG_NAMED_SET_SETS
:
5243 case ZFS_DELEG_NAMED_SET
:
5244 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
5245 base_type
= ZFS_DELEG_NAMED_SET
;
5246 ld
[0] = ZFS_DELEG_NA
;
5248 case ZFS_DELEG_CREATE_SETS
:
5249 case ZFS_DELEG_CREATE
:
5250 set_type
= ZFS_DELEG_CREATE_SETS
;
5251 base_type
= ZFS_DELEG_CREATE
;
5252 ld
[0] = ZFS_DELEG_NA
;
5254 case ZFS_DELEG_USER_SETS
:
5255 case ZFS_DELEG_USER
:
5256 set_type
= ZFS_DELEG_USER_SETS
;
5257 base_type
= ZFS_DELEG_USER
;
5259 ld
[0] = ZFS_DELEG_LOCAL
;
5261 ld
[1] = ZFS_DELEG_DESCENDENT
;
5263 case ZFS_DELEG_GROUP_SETS
:
5264 case ZFS_DELEG_GROUP
:
5265 set_type
= ZFS_DELEG_GROUP_SETS
;
5266 base_type
= ZFS_DELEG_GROUP
;
5268 ld
[0] = ZFS_DELEG_LOCAL
;
5270 ld
[1] = ZFS_DELEG_DESCENDENT
;
5272 case ZFS_DELEG_EVERYONE_SETS
:
5273 case ZFS_DELEG_EVERYONE
:
5274 set_type
= ZFS_DELEG_EVERYONE_SETS
;
5275 base_type
= ZFS_DELEG_EVERYONE
;
5277 ld
[0] = ZFS_DELEG_LOCAL
;
5279 ld
[1] = ZFS_DELEG_DESCENDENT
;
5283 assert(set_type
!= '\0' && base_type
!= '\0');
5286 if (perms
!= NULL
) {
5288 char *end
= curr
+ strlen(perms
);
5290 while (curr
< end
) {
5291 char *delim
= strchr(curr
, ',');
5302 (void) nvlist_add_boolean(nvl
, curr
);
5308 for (i
= 0; i
< 2; i
++) {
5309 char locality
= ld
[i
];
5313 if (!nvlist_empty(base_nvl
)) {
5315 (void) snprintf(who_buf
,
5316 sizeof (who_buf
), "%c%c$%s",
5317 base_type
, locality
, who
);
5319 (void) snprintf(who_buf
,
5320 sizeof (who_buf
), "%c%c$",
5321 base_type
, locality
);
5323 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5328 if (!nvlist_empty(set_nvl
)) {
5330 (void) snprintf(who_buf
,
5331 sizeof (who_buf
), "%c%c$%s",
5332 set_type
, locality
, who
);
5334 (void) snprintf(who_buf
,
5335 sizeof (who_buf
), "%c%c$",
5336 set_type
, locality
);
5338 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5343 for (i
= 0; i
< 2; i
++) {
5344 char locality
= ld
[i
];
5349 (void) snprintf(who_buf
, sizeof (who_buf
),
5350 "%c%c$%s", base_type
, locality
, who
);
5352 (void) snprintf(who_buf
, sizeof (who_buf
),
5353 "%c%c$", base_type
, locality
);
5354 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5357 (void) snprintf(who_buf
, sizeof (who_buf
),
5358 "%c%c$%s", set_type
, locality
, who
);
5360 (void) snprintf(who_buf
, sizeof (who_buf
),
5361 "%c%c$", set_type
, locality
);
5362 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5368 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
5370 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
5374 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
5375 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
5376 } else if (opts
->create
) {
5377 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
5378 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5379 } else if (opts
->everyone
) {
5380 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
5381 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5383 char *curr
= opts
->who
;
5384 char *end
= curr
+ strlen(curr
);
5386 while (curr
< end
) {
5388 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
5390 char *delim
= strchr(curr
, ',');
5393 struct passwd
*p
= NULL
;
5394 struct group
*g
= NULL
;
5402 rid
= (uid_t
)strtol(curr
, &endch
, 0);
5404 who_type
= ZFS_DELEG_USER
;
5413 (void) snprintf(errbuf
, 256, gettext(
5414 "invalid user %s"), curr
);
5415 allow_usage(un
, B_TRUE
, errbuf
);
5417 } else if (opts
->group
) {
5418 who_type
= ZFS_DELEG_GROUP
;
5427 (void) snprintf(errbuf
, 256, gettext(
5428 "invalid group %s"), curr
);
5429 allow_usage(un
, B_TRUE
, errbuf
);
5432 if (*endch
!= '\0') {
5439 if (*endch
!= '\0') {
5447 who_type
= ZFS_DELEG_USER
;
5449 } else if (g
!= NULL
) {
5450 who_type
= ZFS_DELEG_GROUP
;
5453 (void) snprintf(errbuf
, 256, gettext(
5454 "invalid user/group %s"), curr
);
5455 allow_usage(un
, B_TRUE
, errbuf
);
5459 (void) sprintf(id
, "%u", rid
);
5462 store_allow_perm(who_type
, opts
->local
,
5463 opts
->descend
, who
, opts
->perms
, *nvlp
);
5472 print_set_creat_perms(uu_avl_t
*who_avl
)
5474 const char *sc_title
[] = {
5475 gettext("Permission sets:\n"),
5476 gettext("Create time permissions:\n"),
5479 const char **title_ptr
= sc_title
;
5480 who_perm_node_t
*who_node
= NULL
;
5481 int prev_weight
= -1;
5483 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
5484 who_node
= uu_avl_next(who_avl
, who_node
)) {
5485 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5486 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5487 const char *who_name
= who_node
->who_perm
.who_name
;
5488 int weight
= who_type2weight(who_type
);
5489 boolean_t first
= B_TRUE
;
5490 deleg_perm_node_t
*deleg_node
;
5492 if (prev_weight
!= weight
) {
5493 (void) printf("%s", *title_ptr
++);
5494 prev_weight
= weight
;
5497 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
5498 (void) printf("\t");
5500 (void) printf("\t%s ", who_name
);
5502 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
5503 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5506 deleg_node
->dpn_perm
.dp_name
);
5509 (void) printf(",%s",
5510 deleg_node
->dpn_perm
.dp_name
);
5513 (void) printf("\n");
5518 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
5521 who_perm_node_t
*who_node
= NULL
;
5522 boolean_t prt_title
= B_TRUE
;
5523 uu_avl_walk_t
*walk
;
5525 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
5528 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
5529 const char *who_name
= who_node
->who_perm
.who_name
;
5530 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
5531 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5532 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5534 deleg_perm_node_t
*deleg_node
;
5535 boolean_t prt_who
= B_TRUE
;
5537 for (deleg_node
= uu_avl_first(avl
);
5539 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5540 if (local
!= deleg_node
->dpn_perm
.dp_local
||
5541 descend
!= deleg_node
->dpn_perm
.dp_descend
)
5545 const char *who
= NULL
;
5547 prt_title
= B_FALSE
;
5548 (void) printf("%s", title
);
5552 case ZFS_DELEG_USER_SETS
:
5553 case ZFS_DELEG_USER
:
5554 who
= gettext("user");
5556 who_name
= nice_who_name
;
5558 case ZFS_DELEG_GROUP_SETS
:
5559 case ZFS_DELEG_GROUP
:
5560 who
= gettext("group");
5562 who_name
= nice_who_name
;
5564 case ZFS_DELEG_EVERYONE_SETS
:
5565 case ZFS_DELEG_EVERYONE
:
5566 who
= gettext("everyone");
5571 assert(who
!= NULL
);
5575 if (who_name
== NULL
)
5576 (void) printf("\t%s", who
);
5578 (void) printf("\t%s %s", who
, who_name
);
5581 (void) printf("%c%s", delim
,
5582 deleg_node
->dpn_perm
.dp_name
);
5587 (void) printf("\n");
5590 uu_avl_walk_end(walk
);
5594 print_fs_perms(fs_perm_set_t
*fspset
)
5596 fs_perm_node_t
*node
= NULL
;
5597 char buf
[MAXNAMELEN
+ 32];
5598 const char *dsname
= buf
;
5600 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
5601 node
= uu_list_next(fspset
->fsps_list
, node
)) {
5602 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
5603 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
5606 (void) snprintf(buf
, sizeof (buf
),
5607 gettext("---- Permissions on %s "),
5608 node
->fspn_fsperm
.fsp_name
);
5609 (void) printf("%s", dsname
);
5610 left
= 70 - strlen(buf
);
5613 (void) printf("\n");
5615 print_set_creat_perms(sc_avl
);
5616 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
5617 gettext("Local permissions:\n"));
5618 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
5619 gettext("Descendent permissions:\n"));
5620 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
5621 gettext("Local+Descendent permissions:\n"));
5625 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5627 struct deleg_perms
{
5633 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5635 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5636 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5638 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5641 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5645 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5648 nvlist_t
*perm_nvl
= NULL
;
5649 nvlist_t
*update_perm_nvl
= NULL
;
5652 struct allow_opts opts
= { 0 };
5654 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5657 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5660 opts
.local
= B_TRUE
;
5663 opts
.descend
= B_TRUE
;
5669 opts
.group
= B_TRUE
;
5672 opts
.everyone
= B_TRUE
;
5678 opts
.create
= B_TRUE
;
5681 opts
.recursive
= B_TRUE
;
5684 (void) fprintf(stderr
, gettext("missing argument for "
5685 "'%c' option\n"), optopt
);
5689 opts
.prt_usage
= B_TRUE
;
5692 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5701 /* check arguments */
5702 parse_allow_args(argc
, argv
, un
, &opts
);
5704 /* try to open the dataset */
5705 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5706 ZFS_TYPE_VOLUME
)) == NULL
) {
5707 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5712 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5715 fs_perm_set_init(&fs_perm_set
);
5716 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5717 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5722 print_fs_perms(&fs_perm_set
);
5724 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5725 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5728 if (un
&& opts
.recursive
) {
5729 struct deleg_perms data
= { un
, update_perm_nvl
};
5730 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5739 nvlist_free(perm_nvl
);
5740 nvlist_free(update_perm_nvl
);
5742 fs_perm_set_fini(&fs_perm_set
);
5750 zfs_do_allow(int argc
, char **argv
)
5752 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5756 zfs_do_unallow(int argc
, char **argv
)
5758 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5762 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5767 boolean_t recursive
= B_FALSE
;
5768 const char *opts
= holding
? "rt" : "r";
5772 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5778 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5787 /* check number of arguments */
5795 if (holding
&& tag
[0] == '.') {
5796 /* tags starting with '.' are reserved for libzfs */
5797 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5801 for (i
= 0; i
< argc
; ++i
) {
5803 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
5805 char *path
= argv
[i
];
5807 delim
= strchr(path
, '@');
5808 if (delim
== NULL
) {
5809 (void) fprintf(stderr
,
5810 gettext("'%s' is not a snapshot\n"), path
);
5814 (void) strncpy(parent
, path
, delim
- path
);
5815 parent
[delim
- path
] = '\0';
5817 zhp
= zfs_open(g_zfs
, parent
,
5818 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5824 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
5827 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5833 return (errors
!= 0);
5837 * zfs hold [-r] [-t] <tag> <snap> ...
5839 * -r Recursively hold
5841 * Apply a user-hold with the given tag to the list of snapshots.
5844 zfs_do_hold(int argc
, char **argv
)
5846 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5850 * zfs release [-r] <tag> <snap> ...
5852 * -r Recursively release
5854 * Release a user-hold with the given tag from the list of snapshots.
5857 zfs_do_release(int argc
, char **argv
)
5859 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5862 typedef struct holds_cbdata
{
5863 boolean_t cb_recursive
;
5864 const char *cb_snapname
;
5866 size_t cb_max_namelen
;
5867 size_t cb_max_taglen
;
5870 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5871 #define DATETIME_BUF_LEN (32)
5876 print_holds(boolean_t scripted
, int nwidth
, int tagwidth
, nvlist_t
*nvl
)
5879 nvpair_t
*nvp
= NULL
;
5880 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5884 for (i
= 0; i
< 3; i
++) {
5885 col
= gettext(hdr_cols
[i
]);
5887 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5890 (void) printf("%s\n", col
);
5894 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5895 char *zname
= nvpair_name(nvp
);
5897 nvpair_t
*nvp2
= NULL
;
5898 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5899 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5900 char tsbuf
[DATETIME_BUF_LEN
];
5901 char *tagname
= nvpair_name(nvp2
);
5906 (void) nvpair_value_uint64(nvp2
, &val
);
5908 (void) localtime_r(&time
, &t
);
5909 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5910 gettext(STRFTIME_FMT_STR
), &t
);
5913 (void) printf("%s\t%s\t%s\n", zname
,
5916 (void) printf("%-*s %-*s %s\n", nwidth
,
5917 zname
, tagwidth
, tagname
, tsbuf
);
5924 * Generic callback function to list a dataset or snapshot.
5927 holds_callback(zfs_handle_t
*zhp
, void *data
)
5929 holds_cbdata_t
*cbp
= data
;
5930 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5931 nvlist_t
*nvl
= NULL
;
5932 nvpair_t
*nvp
= NULL
;
5933 const char *zname
= zfs_get_name(zhp
);
5934 size_t znamelen
= strlen(zname
);
5936 if (cbp
->cb_recursive
) {
5937 const char *snapname
;
5938 char *delim
= strchr(zname
, '@');
5942 snapname
= delim
+ 1;
5943 if (strcmp(cbp
->cb_snapname
, snapname
))
5947 if (zfs_get_holds(zhp
, &nvl
) != 0)
5950 if (znamelen
> cbp
->cb_max_namelen
)
5951 cbp
->cb_max_namelen
= znamelen
;
5953 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5954 const char *tag
= nvpair_name(nvp
);
5955 size_t taglen
= strlen(tag
);
5956 if (taglen
> cbp
->cb_max_taglen
)
5957 cbp
->cb_max_taglen
= taglen
;
5960 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5964 * zfs holds [-rH] <snap> ...
5966 * -r Lists holds that are set on the named snapshots recursively.
5967 * -H Scripted mode; elide headers and separate columns by tabs.
5970 zfs_do_holds(int argc
, char **argv
)
5975 boolean_t scripted
= B_FALSE
;
5976 boolean_t recursive
= B_FALSE
;
5977 const char *opts
= "rH";
5980 int types
= ZFS_TYPE_SNAPSHOT
;
5981 holds_cbdata_t cb
= { 0 };
5988 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5997 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6004 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
6005 flags
|= ZFS_ITER_RECURSE
;
6011 /* check number of arguments */
6015 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
6018 for (i
= 0; i
< argc
; ++i
) {
6019 char *snapshot
= argv
[i
];
6021 const char *snapname
;
6023 delim
= strchr(snapshot
, '@');
6024 if (delim
== NULL
) {
6025 (void) fprintf(stderr
,
6026 gettext("'%s' is not a snapshot\n"), snapshot
);
6030 snapname
= delim
+ 1;
6032 snapshot
[delim
- snapshot
] = '\0';
6034 cb
.cb_recursive
= recursive
;
6035 cb
.cb_snapname
= snapname
;
6039 * 1. collect holds data, set format options
6041 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
6042 holds_callback
, &cb
);
6048 * 2. print holds data
6050 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
6052 if (nvlist_empty(nvl
))
6053 (void) fprintf(stderr
, gettext("no datasets available\n"));
6057 return (0 != errors
);
6060 #define CHECK_SPINNER 30
6061 #define SPINNER_TIME 3 /* seconds */
6062 #define MOUNT_TIME 5 /* seconds */
6065 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
6067 static char *spin
[] = { "-", "\\", "|", "/" };
6068 static int spinval
= 0;
6069 static int spincheck
= 0;
6070 static time_t last_spin_time
= (time_t)0;
6071 get_all_cb_t
*cbp
= data
;
6072 zfs_type_t type
= zfs_get_type(zhp
);
6074 if (cbp
->cb_verbose
) {
6075 if (--spincheck
< 0) {
6076 time_t now
= time(NULL
);
6077 if (last_spin_time
+ SPINNER_TIME
< now
) {
6078 update_progress(spin
[spinval
++ % 4]);
6079 last_spin_time
= now
;
6081 spincheck
= CHECK_SPINNER
;
6086 * Iterate over any nested datasets.
6088 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
6094 * Skip any datasets whose type does not match.
6096 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
6100 libzfs_add_handle(cbp
, zhp
);
6101 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
6107 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
6109 get_all_cb_t cb
= { 0 };
6110 cb
.cb_verbose
= verbose
;
6111 cb
.cb_getone
= get_one_dataset
;
6114 set_progress_header(gettext("Reading ZFS config"));
6115 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
6117 *dslist
= cb
.cb_handles
;
6118 *count
= cb
.cb_used
;
6121 finish_progress(gettext("done."));
6125 * Generic callback for sharing or mounting filesystems. Because the code is so
6126 * similar, we have a common function with an extra parameter to determine which
6127 * mode we are using.
6129 #define OP_SHARE 0x1
6130 #define OP_MOUNT 0x2
6133 * Share or mount a dataset.
6136 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
6137 boolean_t
explicit, const char *options
)
6139 char mountpoint
[ZFS_MAXPROPLEN
];
6140 char shareopts
[ZFS_MAXPROPLEN
];
6141 char smbshareopts
[ZFS_MAXPROPLEN
];
6142 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
6144 uint64_t zoned
, canmount
;
6145 boolean_t shared_nfs
, shared_smb
;
6147 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
6150 * Check to make sure we can mount/share this dataset. If we
6151 * are in the global zone and the filesystem is exported to a
6152 * local zone, or if we are in a local zone and the
6153 * filesystem is not exported, then it is an error.
6155 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
6157 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
6161 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6162 "dataset is exported to a local zone\n"), cmdname
,
6166 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
6170 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6171 "permission denied\n"), cmdname
,
6177 * Ignore any filesystems which don't apply to us. This
6178 * includes those with a legacy mountpoint, or those with
6179 * legacy share options.
6181 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
6182 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
6183 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
6184 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
6185 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
6186 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
6188 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
6189 strcmp(smbshareopts
, "off") == 0) {
6193 (void) fprintf(stderr
, gettext("cannot share '%s': "
6194 "legacy share\n"), zfs_get_name(zhp
));
6195 (void) fprintf(stderr
, gettext("use share(1M) to "
6196 "share this filesystem, or set "
6197 "sharenfs property on\n"));
6202 * We cannot share or mount legacy filesystems. If the
6203 * shareopts is non-legacy but the mountpoint is legacy, we
6204 * treat it as a legacy share.
6206 if (strcmp(mountpoint
, "legacy") == 0) {
6210 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6211 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
6212 (void) fprintf(stderr
, gettext("use %s(1M) to "
6213 "%s this filesystem\n"), cmdname
, cmdname
);
6217 if (strcmp(mountpoint
, "none") == 0) {
6221 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
6222 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
6227 * canmount explicit outcome
6228 * on no pass through
6229 * on yes pass through
6231 * off yes display error, return 1
6232 * noauto no return 0
6233 * noauto yes pass through
6235 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
6236 if (canmount
== ZFS_CANMOUNT_OFF
) {
6240 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6241 "'canmount' property is set to 'off'\n"), cmdname
,
6244 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
6249 * If this filesystem is encrypted and does not have
6250 * a loaded key, we can not mount it.
6252 if ((flags
& MS_CRYPT
) == 0 &&
6253 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) != ZIO_CRYPT_OFF
&&
6254 zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
) ==
6255 ZFS_KEYSTATUS_UNAVAILABLE
) {
6259 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6260 "encryption key not loaded\n"), cmdname
, zfs_get_name(zhp
));
6265 * If this filesystem is inconsistent and has a receive resume
6266 * token, we can not mount it.
6268 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
6269 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
6270 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
6274 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6275 "Contains partially-completed state from "
6276 "\"zfs receive -s\", which can be resumed with "
6277 "\"zfs send -t\"\n"),
6278 cmdname
, zfs_get_name(zhp
));
6283 * At this point, we have verified that the mountpoint and/or
6284 * shareopts are appropriate for auto management. If the
6285 * filesystem is already mounted or shared, return (failing
6286 * for explicit requests); otherwise mount or share the
6292 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
6293 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
6295 if ((shared_nfs
&& shared_smb
) ||
6296 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
6297 strcmp(smbshareopts
, "off") == 0) ||
6298 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
6299 strcmp(shareopts
, "off") == 0)) {
6303 (void) fprintf(stderr
, gettext("cannot share "
6304 "'%s': filesystem already shared\n"),
6309 if (!zfs_is_mounted(zhp
, NULL
) &&
6310 zfs_mount(zhp
, NULL
, flags
) != 0)
6313 if (protocol
== NULL
) {
6314 if (zfs_shareall(zhp
) != 0)
6316 } else if (strcmp(protocol
, "nfs") == 0) {
6317 if (zfs_share_nfs(zhp
))
6319 } else if (strcmp(protocol
, "smb") == 0) {
6320 if (zfs_share_smb(zhp
))
6323 (void) fprintf(stderr
, gettext("cannot share "
6324 "'%s': invalid share type '%s' "
6326 zfs_get_name(zhp
), protocol
);
6333 if (options
== NULL
)
6334 mnt
.mnt_mntopts
= "";
6336 mnt
.mnt_mntopts
= (char *)options
;
6338 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
6339 zfs_is_mounted(zhp
, NULL
)) {
6343 (void) fprintf(stderr
, gettext("cannot mount "
6344 "'%s': filesystem already mounted\n"),
6349 if (zfs_mount(zhp
, options
, flags
) != 0)
6358 * Reports progress in the form "(current/total)". Not thread-safe.
6361 report_mount_progress(int current
, int total
)
6363 static time_t last_progress_time
= 0;
6364 time_t now
= time(NULL
);
6367 /* report 1..n instead of 0..n-1 */
6370 /* display header if we're here for the first time */
6372 set_progress_header(gettext("Mounting ZFS filesystems"));
6373 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
6374 /* too soon to report again */
6378 last_progress_time
= now
;
6380 (void) sprintf(info
, "(%d/%d)", current
, total
);
6382 if (current
== total
)
6383 finish_progress(info
);
6385 update_progress(info
);
6389 append_options(char *mntopts
, char *newopts
)
6391 int len
= strlen(mntopts
);
6393 /* original length plus new string to append plus 1 for the comma */
6394 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
6395 (void) fprintf(stderr
, gettext("the opts argument for "
6396 "'%s' option is too long (more than %d chars)\n"),
6397 "-o", MNT_LINE_MAX
);
6402 mntopts
[len
++] = ',';
6404 (void) strcpy(&mntopts
[len
], newopts
);
6408 share_mount(int op
, int argc
, char **argv
)
6411 boolean_t verbose
= B_FALSE
;
6413 char *options
= NULL
;
6417 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":alvo:O" : "al"))
6430 if (*optarg
== '\0') {
6431 (void) fprintf(stderr
, gettext("empty mount "
6432 "options (-o) specified\n"));
6436 if (options
== NULL
)
6437 options
= safe_malloc(MNT_LINE_MAX
+ 1);
6439 /* option validation is done later */
6440 append_options(options
, optarg
);
6443 flags
|= MS_OVERLAY
;
6446 (void) fprintf(stderr
, gettext("missing argument for "
6447 "'%c' option\n"), optopt
);
6451 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6460 /* check number of arguments */
6462 zfs_handle_t
**dslist
= NULL
;
6463 size_t i
, count
= 0;
6464 char *protocol
= NULL
;
6466 if (op
== OP_SHARE
&& argc
> 0) {
6467 if (strcmp(argv
[0], "nfs") != 0 &&
6468 strcmp(argv
[0], "smb") != 0) {
6469 (void) fprintf(stderr
, gettext("share type "
6470 "must be 'nfs' or 'smb'\n"));
6479 (void) fprintf(stderr
, gettext("too many arguments\n"));
6483 start_progress_timer();
6484 get_all_datasets(&dslist
, &count
, verbose
);
6487 if (options
!= NULL
)
6492 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
6494 for (i
= 0; i
< count
; i
++) {
6496 report_mount_progress(i
, count
);
6498 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
6499 B_FALSE
, options
) != 0)
6501 zfs_close(dslist
[i
]);
6505 } else if (argc
== 0) {
6506 struct mnttab entry
;
6508 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
6509 (void) fprintf(stderr
, gettext("missing filesystem "
6510 "argument (specify -a for all)\n"));
6515 * When mount is given no arguments, go through
6516 * /proc/self/mounts and display any active ZFS mounts.
6517 * We hide any snapshots, since they are controlled
6521 /* Reopen MNTTAB to prevent reading stale data from open file */
6522 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
) {
6523 if (options
!= NULL
)
6528 while (getmntent(mnttab_file
, &entry
) == 0) {
6529 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
6530 strchr(entry
.mnt_special
, '@') != NULL
)
6533 (void) printf("%-30s %s\n", entry
.mnt_special
,
6541 (void) fprintf(stderr
,
6542 gettext("too many arguments\n"));
6546 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6547 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6550 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
6556 if (options
!= NULL
)
6563 * zfs mount -a [nfs]
6564 * zfs mount filesystem
6566 * Mount all filesystems, or mount the given filesystem.
6569 zfs_do_mount(int argc
, char **argv
)
6571 return (share_mount(OP_MOUNT
, argc
, argv
));
6575 * zfs share -a [nfs | smb]
6576 * zfs share filesystem
6578 * Share all filesystems, or share the given filesystem.
6581 zfs_do_share(int argc
, char **argv
)
6583 return (share_mount(OP_SHARE
, argc
, argv
));
6586 typedef struct unshare_unmount_node
{
6587 zfs_handle_t
*un_zhp
;
6589 uu_avl_node_t un_avlnode
;
6590 } unshare_unmount_node_t
;
6594 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
6596 const unshare_unmount_node_t
*l
= larg
;
6597 const unshare_unmount_node_t
*r
= rarg
;
6599 return (strcmp(l
->un_mountp
, r
->un_mountp
));
6603 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6604 * absolute path, find the entry /proc/self/mounts, verify that its a
6605 * ZFS filesystems, and unmount it appropriately.
6608 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
6612 struct stat64 statbuf
;
6613 struct extmnttab entry
;
6614 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
6618 * Search for the path in /proc/self/mounts. Rather than looking for the
6619 * specific path, which can be fooled by non-standard paths (i.e. ".."
6620 * or "//"), we stat() the path and search for the corresponding
6621 * (major,minor) device pair.
6623 if (stat64(path
, &statbuf
) != 0) {
6624 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6625 cmdname
, path
, strerror(errno
));
6628 path_inode
= statbuf
.st_ino
;
6631 * Search for the given (major,minor) pair in the mount table.
6634 /* Reopen MNTTAB to prevent reading stale data from open file */
6635 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6638 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
6639 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
6640 entry
.mnt_minor
== minor(statbuf
.st_dev
))
6644 if (op
== OP_SHARE
) {
6645 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
6646 "currently mounted\n"), cmdname
, path
);
6649 (void) fprintf(stderr
, gettext("warning: %s not in"
6650 "/proc/self/mounts\n"), path
);
6651 if ((ret
= umount2(path
, flags
)) != 0)
6652 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
6657 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
6658 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
6659 "filesystem\n"), cmdname
, path
);
6663 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6664 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6668 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
6669 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6670 cmdname
, path
, strerror(errno
));
6672 } else if (statbuf
.st_ino
!= path_inode
) {
6673 (void) fprintf(stderr
, gettext("cannot "
6674 "%s '%s': not a mountpoint\n"), cmdname
, path
);
6678 if (op
== OP_SHARE
) {
6679 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6680 char smbshare_prop
[ZFS_MAXPROPLEN
];
6682 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
6683 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6684 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
6685 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6687 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6688 strcmp(smbshare_prop
, "off") == 0) {
6689 (void) fprintf(stderr
, gettext("cannot unshare "
6690 "'%s': legacy share\n"), path
);
6691 (void) fprintf(stderr
, gettext("use exportfs(8) "
6692 "or smbcontrol(1) to unshare this filesystem\n"));
6693 } else if (!zfs_is_shared(zhp
)) {
6694 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6695 "not currently shared\n"), path
);
6697 ret
= zfs_unshareall_bypath(zhp
, path
);
6700 char mtpt_prop
[ZFS_MAXPROPLEN
];
6702 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6703 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6706 ret
= zfs_unmount(zhp
, NULL
, flags
);
6707 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6708 (void) fprintf(stderr
, gettext("cannot unmount "
6709 "'%s': legacy mountpoint\n"),
6711 (void) fprintf(stderr
, gettext("use umount(8) "
6712 "to unmount this filesystem\n"));
6714 ret
= zfs_unmountall(zhp
, flags
);
6725 * Generic callback for unsharing or unmounting a filesystem.
6728 unshare_unmount(int op
, int argc
, char **argv
)
6735 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6736 char sharesmb
[ZFS_MAXPROPLEN
];
6739 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? ":a" : "af")) != -1) {
6748 (void) fprintf(stderr
, gettext("missing argument for "
6749 "'%c' option\n"), optopt
);
6753 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6764 * We could make use of zfs_for_each() to walk all datasets in
6765 * the system, but this would be very inefficient, especially
6766 * since we would have to linearly search /proc/self/mounts for
6767 * each one. Instead, do one pass through /proc/self/mounts
6768 * looking for zfs entries and call zfs_unmount() for each one.
6770 * Things get a little tricky if the administrator has created
6771 * mountpoints beneath other ZFS filesystems. In this case, we
6772 * have to unmount the deepest filesystems first. To accomplish
6773 * this, we place all the mountpoints in an AVL tree sorted by
6774 * the special type (dataset name), and walk the result in
6775 * reverse to make sure to get any snapshots first.
6777 struct mnttab entry
;
6778 uu_avl_pool_t
*pool
;
6779 uu_avl_t
*tree
= NULL
;
6780 unshare_unmount_node_t
*node
;
6782 uu_avl_walk_t
*walk
;
6783 char *protocol
= NULL
;
6785 if (op
== OP_SHARE
&& argc
> 0) {
6786 if (strcmp(argv
[0], "nfs") != 0 &&
6787 strcmp(argv
[0], "smb") != 0) {
6788 (void) fprintf(stderr
, gettext("share type "
6789 "must be 'nfs' or 'smb'\n"));
6798 (void) fprintf(stderr
, gettext("too many arguments\n"));
6802 if (((pool
= uu_avl_pool_create("unmount_pool",
6803 sizeof (unshare_unmount_node_t
),
6804 offsetof(unshare_unmount_node_t
, un_avlnode
),
6805 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6806 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6809 /* Reopen MNTTAB to prevent reading stale data from open file */
6810 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6813 while (getmntent(mnttab_file
, &entry
) == 0) {
6815 /* ignore non-ZFS entries */
6816 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6819 /* ignore snapshots */
6820 if (strchr(entry
.mnt_special
, '@') != NULL
)
6823 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6824 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6830 * Ignore datasets that are excluded/restricted by
6833 if (zpool_skip_pool(zfs_get_pool_name(zhp
))) {
6840 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6842 sizeof (nfs_mnt_prop
),
6843 NULL
, NULL
, 0, B_FALSE
) == 0);
6844 if (strcmp(nfs_mnt_prop
, "off") != 0)
6846 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6848 sizeof (nfs_mnt_prop
),
6849 NULL
, NULL
, 0, B_FALSE
) == 0);
6850 if (strcmp(nfs_mnt_prop
, "off") == 0)
6854 /* Ignore legacy mounts */
6855 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6857 sizeof (nfs_mnt_prop
),
6858 NULL
, NULL
, 0, B_FALSE
) == 0);
6859 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6861 /* Ignore canmount=noauto mounts */
6862 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6863 ZFS_CANMOUNT_NOAUTO
)
6869 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6871 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6873 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6875 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6876 uu_avl_insert(tree
, node
, idx
);
6878 zfs_close(node
->un_zhp
);
6879 free(node
->un_mountp
);
6885 * Walk the AVL tree in reverse, unmounting each filesystem and
6886 * removing it from the AVL tree in the process.
6888 if ((walk
= uu_avl_walk_start(tree
,
6889 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6892 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6893 uu_avl_remove(tree
, node
);
6897 if (zfs_unshareall_bytype(node
->un_zhp
,
6898 node
->un_mountp
, protocol
) != 0)
6903 if (zfs_unmount(node
->un_zhp
,
6904 node
->un_zhp
->zfs_name
, flags
) != 0)
6909 zfs_close(node
->un_zhp
);
6910 free(node
->un_mountp
);
6914 uu_avl_walk_end(walk
);
6915 uu_avl_destroy(tree
);
6916 uu_avl_pool_destroy(pool
);
6921 (void) fprintf(stderr
,
6922 gettext("missing filesystem argument\n"));
6924 (void) fprintf(stderr
,
6925 gettext("too many arguments\n"));
6930 * We have an argument, but it may be a full path or a ZFS
6931 * filesystem. Pass full paths off to unmount_path() (shared by
6932 * manual_unmount), otherwise open the filesystem and pass to
6935 if (argv
[0][0] == '/')
6936 return (unshare_unmount_path(op
, argv
[0],
6939 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6940 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6943 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
6944 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
6945 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
6946 NULL
, 0, B_FALSE
) == 0);
6950 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6952 sizeof (nfs_mnt_prop
),
6953 NULL
, NULL
, 0, B_FALSE
) == 0);
6954 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6955 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
6958 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6959 strcmp(sharesmb
, "off") == 0) {
6960 (void) fprintf(stderr
, gettext("cannot "
6961 "unshare '%s': legacy share\n"),
6963 (void) fprintf(stderr
, gettext("use "
6964 "unshare(1M) to unshare this "
6967 } else if (!zfs_is_shared(zhp
)) {
6968 (void) fprintf(stderr
, gettext("cannot "
6969 "unshare '%s': not currently "
6970 "shared\n"), zfs_get_name(zhp
));
6972 } else if (zfs_unshareall(zhp
) != 0) {
6978 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
6979 (void) fprintf(stderr
, gettext("cannot "
6980 "unmount '%s': legacy "
6981 "mountpoint\n"), zfs_get_name(zhp
));
6982 (void) fprintf(stderr
, gettext("use "
6983 "umount(1M) to unmount this "
6986 } else if (!zfs_is_mounted(zhp
, NULL
)) {
6987 (void) fprintf(stderr
, gettext("cannot "
6988 "unmount '%s': not currently "
6992 } else if (zfs_unmountall(zhp
, flags
) != 0) {
7006 * zfs unmount filesystem
7008 * Unmount all filesystems, or a specific ZFS filesystem.
7011 zfs_do_unmount(int argc
, char **argv
)
7013 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
7018 * zfs unshare filesystem
7020 * Unshare all filesystems, or a specific ZFS filesystem.
7023 zfs_do_unshare(int argc
, char **argv
)
7025 return (unshare_unmount(OP_SHARE
, argc
, argv
));
7029 find_command_idx(char *command
, int *idx
)
7033 for (i
= 0; i
< NCOMMAND
; i
++) {
7034 if (command_table
[i
].name
== NULL
)
7037 if (strcmp(command
, command_table
[i
].name
) == 0) {
7046 zfs_do_diff(int argc
, char **argv
)
7050 char *tosnap
= NULL
;
7051 char *fromsnap
= NULL
;
7056 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
7059 flags
|= ZFS_DIFF_CLASSIFY
;
7062 flags
|= ZFS_DIFF_PARSEABLE
;
7065 flags
|= ZFS_DIFF_TIMESTAMP
;
7068 (void) fprintf(stderr
,
7069 gettext("invalid option '%c'\n"), optopt
);
7078 (void) fprintf(stderr
,
7079 gettext("must provide at least one snapshot name\n"));
7084 (void) fprintf(stderr
, gettext("too many arguments\n"));
7089 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
7092 if (*fromsnap
!= '@')
7093 copy
= strdup(fromsnap
);
7095 copy
= strdup(tosnap
);
7099 if ((atp
= strchr(copy
, '@')) != NULL
)
7102 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
) {
7109 * Ignore SIGPIPE so that the library can give us
7110 * information on any failure
7112 (void) sigignore(SIGPIPE
);
7114 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
7123 * zfs remap <filesystem | volume>
7125 * Remap the indirect blocks in the given fileystem or volume.
7128 zfs_do_remap(int argc
, char **argv
)
7135 while ((c
= getopt(argc
, argv
, "")) != -1) {
7138 (void) fprintf(stderr
,
7139 gettext("invalid option '%c'\n"), optopt
);
7145 (void) fprintf(stderr
, gettext("wrong number of arguments\n"));
7150 err
= zfs_remap_indirects(g_zfs
, fsname
);
7156 * zfs bookmark <fs@snap> <fs#bmark>
7158 * Creates a bookmark with the given name from the given snapshot.
7161 zfs_do_bookmark(int argc
, char **argv
)
7163 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
7164 char bookname
[ZFS_MAX_DATASET_NAME_LEN
];
7171 while ((c
= getopt(argc
, argv
, "")) != -1) {
7174 (void) fprintf(stderr
,
7175 gettext("invalid option '%c'\n"), optopt
);
7183 /* check number of arguments */
7185 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
7189 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
7193 if (strchr(argv
[0], '@') == NULL
) {
7194 (void) fprintf(stderr
,
7195 gettext("invalid snapshot name '%s': "
7196 "must contain a '@'\n"), argv
[0]);
7199 if (strchr(argv
[1], '#') == NULL
) {
7200 (void) fprintf(stderr
,
7201 gettext("invalid bookmark name '%s': "
7202 "must contain a '#'\n"), argv
[1]);
7206 if (argv
[0][0] == '@') {
7208 * Snapshot name begins with @.
7209 * Default to same fs as bookmark.
7211 (void) strlcpy(snapname
, argv
[1], sizeof (snapname
));
7212 *strchr(snapname
, '#') = '\0';
7213 (void) strlcat(snapname
, argv
[0], sizeof (snapname
));
7215 (void) strlcpy(snapname
, argv
[0], sizeof (snapname
));
7217 if (argv
[1][0] == '#') {
7219 * Bookmark name begins with #.
7220 * Default to same fs as snapshot.
7222 (void) strlcpy(bookname
, argv
[0], sizeof (bookname
));
7223 *strchr(bookname
, '@') = '\0';
7224 (void) strlcat(bookname
, argv
[1], sizeof (bookname
));
7226 (void) strlcpy(bookname
, argv
[1], sizeof (bookname
));
7229 zhp
= zfs_open(g_zfs
, snapname
, ZFS_TYPE_SNAPSHOT
);
7235 nvl
= fnvlist_alloc();
7236 fnvlist_add_string(nvl
, bookname
, snapname
);
7237 ret
= lzc_bookmark(nvl
, NULL
);
7241 const char *err_msg
;
7244 (void) snprintf(errbuf
, sizeof (errbuf
),
7245 dgettext(TEXT_DOMAIN
,
7246 "cannot create bookmark '%s'"), bookname
);
7250 err_msg
= "bookmark is in a different pool";
7253 err_msg
= "bookmark exists";
7256 err_msg
= "invalid argument";
7259 err_msg
= "bookmark feature not enabled";
7262 err_msg
= "out of space";
7265 err_msg
= "dataset does not exist";
7268 err_msg
= "unknown error";
7271 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
7272 dgettext(TEXT_DOMAIN
, err_msg
));
7283 zfs_do_channel_program(int argc
, char **argv
)
7286 char *progbuf
, *filename
, *poolname
;
7287 size_t progsize
, progread
;
7289 uint64_t instrlimit
= ZCP_DEFAULT_INSTRLIMIT
;
7290 uint64_t memlimit
= ZCP_DEFAULT_MEMLIMIT
;
7291 boolean_t sync_flag
= B_TRUE
, json_output
= B_FALSE
;
7292 zpool_handle_t
*zhp
;
7295 while ((c
= getopt(argc
, argv
, "nt:m:j")) != -1) {
7303 arg
= strtoull(optarg
, &endp
, 0);
7304 if (errno
!= 0 || *endp
!= '\0') {
7305 (void) fprintf(stderr
, gettext(
7307 "'%s': expected integer\n"), optarg
);
7314 ASSERT3U(c
, ==, 'm');
7320 sync_flag
= B_FALSE
;
7324 json_output
= B_TRUE
;
7328 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7338 (void) fprintf(stderr
,
7339 gettext("invalid number of arguments\n"));
7345 if (strcmp(filename
, "-") == 0) {
7347 filename
= "standard input";
7348 } else if ((fd
= open(filename
, O_RDONLY
)) < 0) {
7349 (void) fprintf(stderr
, gettext("cannot open '%s': %s\n"),
7350 filename
, strerror(errno
));
7354 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
7355 (void) fprintf(stderr
, gettext("cannot open pool '%s'"),
7364 * Read in the channel program, expanding the program buffer as
7369 progbuf
= safe_malloc(progsize
);
7371 ret
= read(fd
, progbuf
+ progread
, progsize
- progread
);
7373 if (progread
== progsize
&& ret
> 0) {
7375 progbuf
= safe_realloc(progbuf
, progsize
);
7383 (void) fprintf(stderr
,
7384 gettext("cannot read '%s': %s\n"),
7385 filename
, strerror(errno
));
7388 progbuf
[progread
] = '\0';
7391 * Any remaining arguments are passed as arguments to the lua script as
7394 * "argv" -> [ "arg 1", ... "arg n" ],
7397 nvlist_t
*argnvl
= fnvlist_alloc();
7398 fnvlist_add_string_array(argnvl
, ZCP_ARG_CLIARGV
, argv
+ 2, argc
- 2);
7401 ret
= lzc_channel_program(poolname
, progbuf
,
7402 instrlimit
, memlimit
, argnvl
, &outnvl
);
7404 ret
= lzc_channel_program_nosync(poolname
, progbuf
,
7405 instrlimit
, memlimit
, argnvl
, &outnvl
);
7410 * On error, report the error message handed back by lua if one
7411 * exists. Otherwise, generate an appropriate error message,
7412 * falling back on strerror() for an unexpected return code.
7414 char *errstring
= NULL
;
7415 uint64_t instructions
= 0;
7416 if (nvlist_exists(outnvl
, ZCP_RET_ERROR
)) {
7417 (void) nvlist_lookup_string(outnvl
,
7418 ZCP_RET_ERROR
, &errstring
);
7419 if (errstring
== NULL
)
7420 errstring
= strerror(ret
);
7422 (void) nvlist_lookup_uint64(outnvl
,
7423 ZCP_ARG_INSTRLIMIT
, &instructions
);
7429 "Invalid instruction or memory limit.";
7432 errstring
= "Return value too large.";
7435 errstring
= "Memory limit exhausted.";
7438 errstring
= "Timed out.";
7441 errstring
= "Permission denied. Channel "
7442 "programs must be run as root.";
7445 errstring
= strerror(ret
);
7448 (void) fprintf(stderr
,
7449 gettext("Channel program execution failed:\n%s\n"),
7451 if (ret
== ETIME
&& instructions
!= 0)
7452 (void) fprintf(stderr
,
7453 gettext("%llu Lua instructions\n"),
7454 (u_longlong_t
)instructions
);
7457 (void) nvlist_print_json(stdout
, outnvl
);
7458 } else if (nvlist_empty(outnvl
)) {
7459 (void) fprintf(stdout
, gettext("Channel program fully "
7460 "executed and did not produce output.\n"));
7462 (void) fprintf(stdout
, gettext("Channel program fully "
7463 "executed and produced output:\n"));
7464 dump_nvlist(outnvl
, 4);
7469 fnvlist_free(outnvl
);
7470 fnvlist_free(argnvl
);
7479 typedef struct loadkey_cbdata
{
7480 boolean_t cb_loadkey
;
7481 boolean_t cb_recursive
;
7483 char *cb_keylocation
;
7484 uint64_t cb_numfailed
;
7485 uint64_t cb_numattempted
;
7489 load_key_callback(zfs_handle_t
*zhp
, void *data
)
7492 boolean_t is_encroot
;
7493 loadkey_cbdata_t
*cb
= data
;
7494 uint64_t keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
7497 * If we are working recursively, we want to skip loading / unloading
7498 * keys for non-encryption roots and datasets whose keys are already
7499 * in the desired end-state.
7501 if (cb
->cb_recursive
) {
7502 ret
= zfs_crypto_get_encryption_root(zhp
, &is_encroot
, NULL
);
7508 if ((cb
->cb_loadkey
&& keystatus
== ZFS_KEYSTATUS_AVAILABLE
) ||
7509 (!cb
->cb_loadkey
&& keystatus
== ZFS_KEYSTATUS_UNAVAILABLE
))
7513 cb
->cb_numattempted
++;
7516 ret
= zfs_crypto_load_key(zhp
, cb
->cb_noop
, cb
->cb_keylocation
);
7518 ret
= zfs_crypto_unload_key(zhp
);
7529 load_unload_keys(int argc
, char **argv
, boolean_t loadkey
)
7531 int c
, ret
= 0, flags
= 0;
7532 boolean_t do_all
= B_FALSE
;
7533 loadkey_cbdata_t cb
= { 0 };
7535 cb
.cb_loadkey
= loadkey
;
7537 while ((c
= getopt(argc
, argv
, "anrL:")) != -1) {
7538 /* noop and alternate keylocations only apply to zfs load-key */
7542 cb
.cb_noop
= B_TRUE
;
7545 cb
.cb_keylocation
= optarg
;
7555 cb
.cb_recursive
= B_TRUE
;
7558 flags
|= ZFS_ITER_RECURSE
;
7559 cb
.cb_recursive
= B_TRUE
;
7562 (void) fprintf(stderr
,
7563 gettext("invalid option '%c'\n"), optopt
);
7571 if (!do_all
&& argc
== 0) {
7572 (void) fprintf(stderr
,
7573 gettext("Missing dataset argument or -a option\n"));
7577 if (do_all
&& argc
!= 0) {
7578 (void) fprintf(stderr
,
7579 gettext("Cannot specify dataset with -a option\n"));
7583 if (cb
.cb_recursive
&& cb
.cb_keylocation
!= NULL
&&
7584 strcmp(cb
.cb_keylocation
, "prompt") != 0) {
7585 (void) fprintf(stderr
, gettext("alternate keylocation may only "
7586 "be 'prompt' with -r or -a\n"));
7590 ret
= zfs_for_each(argc
, argv
, flags
,
7591 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
, NULL
, NULL
, 0,
7592 load_key_callback
, &cb
);
7594 if (cb
.cb_noop
|| (cb
.cb_recursive
&& cb
.cb_numattempted
!= 0)) {
7595 (void) printf(gettext("%llu / %llu key(s) successfully %s\n"),
7596 (u_longlong_t
)(cb
.cb_numattempted
- cb
.cb_numfailed
),
7597 (u_longlong_t
)cb
.cb_numattempted
,
7598 loadkey
? (cb
.cb_noop
? "verified" : "loaded") :
7602 if (cb
.cb_numfailed
!= 0)
7609 zfs_do_load_key(int argc
, char **argv
)
7611 return (load_unload_keys(argc
, argv
, B_TRUE
));
7616 zfs_do_unload_key(int argc
, char **argv
)
7618 return (load_unload_keys(argc
, argv
, B_FALSE
));
7622 zfs_do_change_key(int argc
, char **argv
)
7626 boolean_t loadkey
= B_FALSE
, inheritkey
= B_FALSE
;
7627 zfs_handle_t
*zhp
= NULL
;
7628 nvlist_t
*props
= fnvlist_alloc();
7630 while ((c
= getopt(argc
, argv
, "lio:")) != -1) {
7636 inheritkey
= B_TRUE
;
7639 if (!parseprop(props
, optarg
)) {
7645 (void) fprintf(stderr
,
7646 gettext("invalid option '%c'\n"), optopt
);
7651 if (inheritkey
&& !nvlist_empty(props
)) {
7652 (void) fprintf(stderr
,
7653 gettext("Properties not allowed for inheriting\n"));
7661 (void) fprintf(stderr
, gettext("Missing dataset argument\n"));
7666 (void) fprintf(stderr
, gettext("Too many arguments\n"));
7670 zhp
= zfs_open(g_zfs
, argv
[argc
- 1],
7671 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
7676 keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
7677 if (keystatus
!= ZFS_KEYSTATUS_AVAILABLE
) {
7678 ret
= zfs_crypto_load_key(zhp
, B_FALSE
, NULL
);
7686 /* refresh the properties so the new keystatus is visible */
7687 zfs_refresh_properties(zhp
);
7690 ret
= zfs_crypto_rewrap(zhp
, props
, inheritkey
);
7703 * 1) zfs project [-d|-r] <file|directory ...>
7704 * List project ID and inherit flag of file(s) or directories.
7705 * -d: List the directory itself, not its children.
7706 * -r: List subdirectories recursively.
7708 * 2) zfs project -C [-k] [-r] <file|directory ...>
7709 * Clear project inherit flag and/or ID on the file(s) or directories.
7710 * -k: Keep the project ID unchanged. If not specified, the project ID
7711 * will be reset as zero.
7712 * -r: Clear on subdirectories recursively.
7714 * 3) zfs project -c [-0] [-d|-r] [-p id] <file|directory ...>
7715 * Check project ID and inherit flag on the file(s) or directories,
7716 * report the outliers.
7717 * -0: Print file name followed by a NUL instead of newline.
7718 * -d: Check the directory itself, not its children.
7719 * -p: Specify the referenced ID for comparing with the target file(s)
7720 * or directories' project IDs. If not specified, the target (top)
7721 * directory's project ID will be used as the referenced one.
7722 * -r: Check subdirectories recursively.
7724 * 4) zfs project [-p id] [-r] [-s] <file|directory ...>
7725 * Set project ID and/or inherit flag on the file(s) or directories.
7726 * -p: Set the project ID as the given id.
7727 * -r: Set on subdirectorie recursively. If not specify "-p" option,
7728 * it will use top-level directory's project ID as the given id,
7729 * then set both project ID and inherit flag on all descendants
7730 * of the top-level directory.
7731 * -s: Set project inherit flag.
7734 zfs_do_project(int argc
, char **argv
)
7736 zfs_project_control_t zpc
= {
7737 .zpc_expected_projid
= ZFS_INVALID_PROJID
,
7738 .zpc_op
= ZFS_PROJECT_OP_DEFAULT
,
7739 .zpc_dironly
= B_FALSE
,
7740 .zpc_keep_projid
= B_FALSE
,
7741 .zpc_newline
= B_TRUE
,
7742 .zpc_recursive
= B_FALSE
,
7743 .zpc_set_flag
= B_FALSE
,
7750 while ((c
= getopt(argc
, argv
, "0Ccdkp:rs")) != -1) {
7753 zpc
.zpc_newline
= B_FALSE
;
7756 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
7757 (void) fprintf(stderr
, gettext("cannot "
7758 "specify '-C' '-c' '-s' together\n"));
7762 zpc
.zpc_op
= ZFS_PROJECT_OP_CLEAR
;
7765 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
7766 (void) fprintf(stderr
, gettext("cannot "
7767 "specify '-C' '-c' '-s' together\n"));
7771 zpc
.zpc_op
= ZFS_PROJECT_OP_CHECK
;
7774 zpc
.zpc_dironly
= B_TRUE
;
7775 /* overwrite "-r" option */
7776 zpc
.zpc_recursive
= B_FALSE
;
7779 zpc
.zpc_keep_projid
= B_TRUE
;
7785 zpc
.zpc_expected_projid
= strtoull(optarg
, &endptr
, 0);
7786 if (errno
!= 0 || *endptr
!= '\0') {
7787 (void) fprintf(stderr
,
7788 gettext("project ID must be less than "
7789 "%u\n"), UINT32_MAX
);
7792 if (zpc
.zpc_expected_projid
>= UINT32_MAX
) {
7793 (void) fprintf(stderr
,
7794 gettext("invalid project ID\n"));
7800 zpc
.zpc_recursive
= B_TRUE
;
7801 /* overwrite "-d" option */
7802 zpc
.zpc_dironly
= B_FALSE
;
7805 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
7806 (void) fprintf(stderr
, gettext("cannot "
7807 "specify '-C' '-c' '-s' together\n"));
7811 zpc
.zpc_set_flag
= B_TRUE
;
7812 zpc
.zpc_op
= ZFS_PROJECT_OP_SET
;
7815 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7821 if (zpc
.zpc_op
== ZFS_PROJECT_OP_DEFAULT
) {
7822 if (zpc
.zpc_expected_projid
!= ZFS_INVALID_PROJID
)
7823 zpc
.zpc_op
= ZFS_PROJECT_OP_SET
;
7825 zpc
.zpc_op
= ZFS_PROJECT_OP_LIST
;
7828 switch (zpc
.zpc_op
) {
7829 case ZFS_PROJECT_OP_LIST
:
7830 if (zpc
.zpc_keep_projid
) {
7831 (void) fprintf(stderr
,
7832 gettext("'-k' is only valid together with '-C'\n"));
7835 if (!zpc
.zpc_newline
) {
7836 (void) fprintf(stderr
,
7837 gettext("'-0' is only valid together with '-c'\n"));
7841 case ZFS_PROJECT_OP_CHECK
:
7842 if (zpc
.zpc_keep_projid
) {
7843 (void) fprintf(stderr
,
7844 gettext("'-k' is only valid together with '-C'\n"));
7848 case ZFS_PROJECT_OP_CLEAR
:
7849 if (zpc
.zpc_dironly
) {
7850 (void) fprintf(stderr
,
7851 gettext("'-d' is useless together with '-C'\n"));
7854 if (!zpc
.zpc_newline
) {
7855 (void) fprintf(stderr
,
7856 gettext("'-0' is only valid together with '-c'\n"));
7859 if (zpc
.zpc_expected_projid
!= ZFS_INVALID_PROJID
) {
7860 (void) fprintf(stderr
,
7861 gettext("'-p' is useless together with '-C'\n"));
7865 case ZFS_PROJECT_OP_SET
:
7866 if (zpc
.zpc_dironly
) {
7867 (void) fprintf(stderr
,
7868 gettext("'-d' is useless for set project ID and/or "
7872 if (zpc
.zpc_keep_projid
) {
7873 (void) fprintf(stderr
,
7874 gettext("'-k' is only valid together with '-C'\n"));
7877 if (!zpc
.zpc_newline
) {
7878 (void) fprintf(stderr
,
7879 gettext("'-0' is only valid together with '-c'\n"));
7891 (void) fprintf(stderr
,
7892 gettext("missing file or directory target(s)\n"));
7896 for (int i
= 0; i
< argc
; i
++) {
7899 err
= zfs_project_handle(argv
[i
], &zpc
);
7908 main(int argc
, char **argv
)
7914 (void) setlocale(LC_ALL
, "");
7915 (void) textdomain(TEXT_DOMAIN
);
7920 * Make sure the user has specified some command.
7923 (void) fprintf(stderr
, gettext("missing command\n"));
7930 * The 'umount' command is an alias for 'unmount'
7932 if (strcmp(cmdname
, "umount") == 0)
7933 cmdname
= "unmount";
7936 * The 'recv' command is an alias for 'receive'
7938 if (strcmp(cmdname
, "recv") == 0)
7939 cmdname
= "receive";
7942 * The 'snap' command is an alias for 'snapshot'
7944 if (strcmp(cmdname
, "snap") == 0)
7945 cmdname
= "snapshot";
7950 if ((strcmp(cmdname
, "-?") == 0) ||
7951 (strcmp(cmdname
, "--help") == 0))
7954 if ((g_zfs
= libzfs_init()) == NULL
) {
7955 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
7959 mnttab_file
= g_zfs
->libzfs_mnttab
;
7961 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
7963 libzfs_print_on_error(g_zfs
, B_TRUE
);
7966 * Run the appropriate command.
7968 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
7969 if (find_command_idx(cmdname
, &i
) == 0) {
7970 current_command
= &command_table
[i
];
7971 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
7972 } else if (strchr(cmdname
, '=') != NULL
) {
7973 verify(find_command_idx("set", &i
) == 0);
7974 current_command
= &command_table
[i
];
7975 ret
= command_table
[i
].func(argc
, argv
);
7977 (void) fprintf(stderr
, gettext("unrecognized "
7978 "command '%s'\n"), cmdname
);
7983 if (ret
== 0 && log_history
)
7984 (void) zpool_log_history(g_zfs
, history_str
);
7989 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7990 * for the purposes of running ::findleaks.
7992 if (getenv("ZFS_ABORT") != NULL
) {
7993 (void) printf("dumping core by request\n");