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) 2013 by Delphix. All rights reserved.
25 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2013 Steven Hartland. All rights reserved.
27 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
36 #include <libnvpair.h>
49 #include <sys/mkdev.h>
50 #include <sys/mntent.h>
51 #include <sys/mnttab.h>
52 #include <sys/mount.h>
54 #include <sys/fs/zfs.h>
55 #include <sys/types.h>
59 #include <libzfs_core.h>
61 #include <zfs_deleg.h>
65 #include <directory.h>
66 #endif /* HAVE_IDMAP */
70 #include "zfs_comutil.h"
71 #include "libzfs_impl.h"
73 libzfs_handle_t
*g_zfs
;
75 static FILE *mnttab_file
;
76 static char history_str
[HIS_MAX_RECORD_LEN
];
77 static boolean_t log_history
= B_TRUE
;
79 static int zfs_do_clone(int argc
, char **argv
);
80 static int zfs_do_create(int argc
, char **argv
);
81 static int zfs_do_destroy(int argc
, char **argv
);
82 static int zfs_do_get(int argc
, char **argv
);
83 static int zfs_do_inherit(int argc
, char **argv
);
84 static int zfs_do_list(int argc
, char **argv
);
85 static int zfs_do_mount(int argc
, char **argv
);
86 static int zfs_do_rename(int argc
, char **argv
);
87 static int zfs_do_rollback(int argc
, char **argv
);
88 static int zfs_do_set(int argc
, char **argv
);
89 static int zfs_do_upgrade(int argc
, char **argv
);
90 static int zfs_do_snapshot(int argc
, char **argv
);
91 static int zfs_do_unmount(int argc
, char **argv
);
92 static int zfs_do_share(int argc
, char **argv
);
93 static int zfs_do_unshare(int argc
, char **argv
);
94 static int zfs_do_send(int argc
, char **argv
);
95 static int zfs_do_receive(int argc
, char **argv
);
96 static int zfs_do_promote(int argc
, char **argv
);
97 static int zfs_do_userspace(int argc
, char **argv
);
98 static int zfs_do_allow(int argc
, char **argv
);
99 static int zfs_do_unallow(int argc
, char **argv
);
100 static int zfs_do_hold(int argc
, char **argv
);
101 static int zfs_do_holds(int argc
, char **argv
);
102 static int zfs_do_release(int argc
, char **argv
);
103 static int zfs_do_diff(int argc
, char **argv
);
104 static int zfs_do_bookmark(int argc
, char **argv
);
107 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
112 _umem_debug_init(void)
114 return ("default,verbose"); /* $UMEM_DEBUG setting */
118 _umem_logging_init(void)
120 return ("fail,contents"); /* $UMEM_LOGGING setting */
154 typedef struct zfs_command
{
156 int (*func
)(int argc
, char **argv
);
161 * Master command table. Each ZFS command has a name, associated function, and
162 * usage message. The usage messages need to be internationalized, so we have
163 * to have a function to return the usage message based on a command index.
165 * These commands are organized according to how they are displayed in the usage
166 * message. An empty command (one with a NULL name) indicates an empty line in
167 * the generic usage message.
169 static zfs_command_t command_table
[] = {
170 { "create", zfs_do_create
, HELP_CREATE
},
171 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
173 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
174 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
175 { "clone", zfs_do_clone
, HELP_CLONE
},
176 { "promote", zfs_do_promote
, HELP_PROMOTE
},
177 { "rename", zfs_do_rename
, HELP_RENAME
},
178 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
180 { "list", zfs_do_list
, HELP_LIST
},
182 { "set", zfs_do_set
, HELP_SET
},
183 { "get", zfs_do_get
, HELP_GET
},
184 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
185 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
186 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
187 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
189 { "mount", zfs_do_mount
, HELP_MOUNT
},
190 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
191 { "share", zfs_do_share
, HELP_SHARE
},
192 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
194 { "send", zfs_do_send
, HELP_SEND
},
195 { "receive", zfs_do_receive
, HELP_RECEIVE
},
197 { "allow", zfs_do_allow
, HELP_ALLOW
},
199 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
201 { "hold", zfs_do_hold
, HELP_HOLD
},
202 { "holds", zfs_do_holds
, HELP_HOLDS
},
203 { "release", zfs_do_release
, HELP_RELEASE
},
204 { "diff", zfs_do_diff
, HELP_DIFF
},
207 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
209 zfs_command_t
*current_command
;
212 get_usage(zfs_help_t idx
)
216 return (gettext("\tclone [-p] [-o property=value] ... "
217 "<snapshot> <filesystem|volume>\n"));
219 return (gettext("\tcreate [-p] [-o property=value] ... "
221 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
222 "-V <size> <volume>\n"));
224 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
225 "\tdestroy [-dnpRrv] "
226 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
227 "\tdestroy <filesystem|volume>#<bookmark>\n"));
229 return (gettext("\tget [-rHp] [-d max] "
230 "[-o \"all\" | field[,...]]\n"
231 "\t [-t type[,...]] [-s source[,...]]\n"
232 "\t <\"all\" | property[,...]> "
233 "[filesystem|volume|snapshot] ...\n"));
235 return (gettext("\tinherit [-rS] <property> "
236 "<filesystem|volume|snapshot> ...\n"));
238 return (gettext("\tupgrade [-v]\n"
239 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
241 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
242 "[-s property]...\n\t [-S property]... [-t type[,...]] "
243 "[filesystem|volume|snapshot] ...\n"));
245 return (gettext("\tmount\n"
246 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
248 return (gettext("\tpromote <clone-filesystem>\n"));
250 return (gettext("\treceive [-vnFu] <filesystem|volume|"
252 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
254 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
255 "<filesystem|volume|snapshot>\n"
256 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
257 "\trename -r <snapshot> <snapshot>\n"));
259 return (gettext("\trollback [-rRf] <snapshot>\n"));
261 return (gettext("\tsend [-DnPpRrve] [-[iI] snapshot] "
263 "\tsend [-e] [-i snapshot|bookmark] "
264 "<filesystem|volume|snapshot>\n"));
266 return (gettext("\tset <property=value> "
267 "<filesystem|volume|snapshot> ...\n"));
269 return (gettext("\tshare <-a | filesystem>\n"));
271 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
272 "<filesystem|volume>@<snap> ...\n"));
274 return (gettext("\tunmount [-f] "
275 "<-a | filesystem|mountpoint>\n"));
277 return (gettext("\tunshare "
278 "<-a | filesystem|mountpoint>\n"));
280 return (gettext("\tallow <filesystem|volume>\n"
282 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
283 "\t <filesystem|volume>\n"
284 "\tallow [-ld] -e <perm|@setname>[,...] "
285 "<filesystem|volume>\n"
286 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
287 "\tallow -s @setname <perm|@setname>[,...] "
288 "<filesystem|volume>\n"));
290 return (gettext("\tunallow [-rldug] "
291 "<\"everyone\"|user|group>[,...]\n"
292 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
293 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
294 "<filesystem|volume>\n"
295 "\tunallow [-r] -c [<perm|@setname>[,...]] "
296 "<filesystem|volume>\n"
297 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
298 "<filesystem|volume>\n"));
300 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
302 "\t [-S field] ... [-t type[,...]] "
303 "<filesystem|snapshot>\n"));
304 case HELP_GROUPSPACE
:
305 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
307 "\t [-S field] ... [-t type[,...]] "
308 "<filesystem|snapshot>\n"));
310 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
312 return (gettext("\tholds [-r] <snapshot> ...\n"));
314 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
316 return (gettext("\tdiff [-FHt] <snapshot> "
317 "[snapshot|filesystem]\n"));
319 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
329 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
334 * Utility function to guarantee malloc() success.
338 safe_malloc(size_t size
)
342 if ((data
= calloc(1, size
)) == NULL
)
349 safe_strdup(char *str
)
351 char *dupstr
= strdup(str
);
360 * Callback routine that will print out information for each of
364 usage_prop_cb(int prop
, void *cb
)
368 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
370 if (zfs_prop_readonly(prop
))
371 (void) fprintf(fp
, " NO ");
373 (void) fprintf(fp
, "YES ");
375 if (zfs_prop_inheritable(prop
))
376 (void) fprintf(fp
, " YES ");
378 (void) fprintf(fp
, " NO ");
380 if (zfs_prop_values(prop
) == NULL
)
381 (void) fprintf(fp
, "-\n");
383 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
389 * Display usage message. If we're inside a command, display only the usage for
390 * that command. Otherwise, iterate over the entire command table and display
391 * a complete usage message.
394 usage(boolean_t requested
)
397 boolean_t show_properties
= B_FALSE
;
398 FILE *fp
= requested
? stdout
: stderr
;
400 if (current_command
== NULL
) {
402 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
404 gettext("where 'command' is one of the following:\n\n"));
406 for (i
= 0; i
< NCOMMAND
; i
++) {
407 if (command_table
[i
].name
== NULL
)
408 (void) fprintf(fp
, "\n");
410 (void) fprintf(fp
, "%s",
411 get_usage(command_table
[i
].usage
));
414 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
415 "pool/[dataset/]*dataset[@name]\n"));
417 (void) fprintf(fp
, gettext("usage:\n"));
418 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
421 if (current_command
!= NULL
&&
422 (strcmp(current_command
->name
, "set") == 0 ||
423 strcmp(current_command
->name
, "get") == 0 ||
424 strcmp(current_command
->name
, "inherit") == 0 ||
425 strcmp(current_command
->name
, "list") == 0))
426 show_properties
= B_TRUE
;
428 if (show_properties
) {
430 gettext("\nThe following properties are supported:\n"));
432 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
433 "PROPERTY", "EDIT", "INHERIT", "VALUES");
435 /* Iterate over all properties */
436 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
439 (void) fprintf(fp
, "\t%-15s ", "userused@...");
440 (void) fprintf(fp
, " NO NO <size>\n");
441 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
442 (void) fprintf(fp
, " NO NO <size>\n");
443 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
444 (void) fprintf(fp
, "YES NO <size> | none\n");
445 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
446 (void) fprintf(fp
, "YES NO <size> | none\n");
447 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
448 (void) fprintf(fp
, " NO NO <size>\n");
450 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
451 "with standard units such as K, M, G, etc.\n"));
452 (void) fprintf(fp
, gettext("\nUser-defined properties can "
453 "be specified by using a name containing a colon (:).\n"));
454 (void) fprintf(fp
, gettext("\nThe {user|group}{used|quota}@ "
455 "properties must be appended with\n"
456 "a user or group specifier of one of these forms:\n"
457 " POSIX name (eg: \"matt\")\n"
458 " POSIX id (eg: \"126829\")\n"
459 " SMB name@domain (eg: \"matt@sun\")\n"
460 " SMB SID (eg: \"S-1-234-567-89\")\n"));
463 gettext("\nFor the property list, run: %s\n"),
466 gettext("\nFor the delegated permission list, run: %s\n"),
467 "zfs allow|unallow");
471 * See comments at end of main().
473 if (getenv("ZFS_ABORT") != NULL
) {
474 (void) printf("dumping core by request\n");
478 exit(requested
? 0 : 2);
482 parseprop(nvlist_t
*props
)
484 char *propname
= optarg
;
485 char *propval
, *strval
;
487 if ((propval
= strchr(propname
, '=')) == NULL
) {
488 (void) fprintf(stderr
, gettext("missing "
489 "'=' for -o option\n"));
494 if (nvlist_lookup_string(props
, propname
, &strval
) == 0) {
495 (void) fprintf(stderr
, gettext("property '%s' "
496 "specified multiple times\n"), propname
);
499 if (nvlist_add_string(props
, propname
, propval
) != 0)
505 parse_depth(char *opt
, int *flags
)
510 depth
= (int)strtol(opt
, &tmp
, 0);
512 (void) fprintf(stderr
,
513 gettext("%s is not an integer\n"), optarg
);
517 (void) fprintf(stderr
,
518 gettext("Depth can not be negative.\n"));
521 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
525 #define PROGRESS_DELAY 2 /* seconds */
527 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";
528 static time_t pt_begin
;
529 static char *pt_header
= NULL
;
530 static boolean_t pt_shown
;
533 start_progress_timer(void)
535 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
540 set_progress_header(char *header
)
542 assert(pt_header
== NULL
);
543 pt_header
= safe_strdup(header
);
545 (void) printf("%s: ", header
);
546 (void) fflush(stdout
);
551 update_progress(char *update
)
553 if (!pt_shown
&& time(NULL
) > pt_begin
) {
554 int len
= strlen(update
);
556 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
558 (void) fflush(stdout
);
560 } else if (pt_shown
) {
561 int len
= strlen(update
);
563 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
564 (void) fflush(stdout
);
569 finish_progress(char *done
)
572 (void) printf("%s\n", done
);
573 (void) fflush(stdout
);
580 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
582 * Given an existing dataset, create a writable copy whose initial contents
583 * are the same as the source. The newly created dataset maintains a
584 * dependency on the original; the original cannot be destroyed so long as
587 * The '-p' flag creates all the non-existing ancestors of the target first.
590 zfs_do_clone(int argc
, char **argv
)
592 zfs_handle_t
*zhp
= NULL
;
593 boolean_t parents
= B_FALSE
;
598 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
602 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
605 if (parseprop(props
))
612 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
621 /* check number of arguments */
623 (void) fprintf(stderr
, gettext("missing source dataset "
628 (void) fprintf(stderr
, gettext("missing target dataset "
633 (void) fprintf(stderr
, gettext("too many arguments\n"));
637 /* open the source dataset */
638 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
641 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
644 * Now create the ancestors of the target dataset. If the
645 * target already exists and '-p' option was used we should not
648 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
651 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
656 ret
= zfs_clone(zhp
, argv
[1], props
);
658 /* create the mountpoint if necessary */
661 int canmount
= ZFS_CANMOUNT_OFF
;
664 (void) zpool_log_history(g_zfs
, history_str
);
665 log_history
= B_FALSE
;
668 clone
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
671 * if the user doesn't want the dataset automatically
672 * mounted, then skip the mount/share step.
674 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
,
675 zfs_get_type(clone
), B_FALSE
))
676 canmount
= zfs_prop_get_int(clone
,
679 if (zfs_get_type(clone
) != ZFS_TYPE_VOLUME
&&
680 canmount
== ZFS_CANMOUNT_ON
)
681 if ((ret
= zfs_mount(clone
, NULL
, 0)) == 0)
682 ret
= zfs_share(clone
);
701 * zfs create [-p] [-o prop=value] ... fs
702 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
704 * Create a new dataset. This command can be used to create filesystems
705 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
706 * For volumes, the user must specify a size to be used.
708 * The '-s' flag applies only to volumes, and indicates that we should not try
709 * to set the reservation for this volume. By default we set a reservation
710 * equal to the size for any volume. For pools with SPA_VERSION >=
711 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
713 * The '-p' flag creates all the non-existing ancestors of the target first.
716 zfs_do_create(int argc
, char **argv
)
718 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
719 zfs_handle_t
*zhp
= NULL
;
720 uint64_t volsize
= 0;
722 boolean_t noreserve
= B_FALSE
;
723 boolean_t bflag
= B_FALSE
;
724 boolean_t parents
= B_FALSE
;
728 int canmount
= ZFS_CANMOUNT_OFF
;
730 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
734 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
737 type
= ZFS_TYPE_VOLUME
;
738 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
739 (void) fprintf(stderr
, gettext("bad volume "
740 "size '%s': %s\n"), optarg
,
741 libzfs_error_description(g_zfs
));
745 if (nvlist_add_uint64(props
,
746 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
755 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
756 (void) fprintf(stderr
, gettext("bad volume "
757 "block size '%s': %s\n"), optarg
,
758 libzfs_error_description(g_zfs
));
762 if (nvlist_add_uint64(props
,
763 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
768 if (parseprop(props
))
775 (void) fprintf(stderr
, gettext("missing size "
780 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
786 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
787 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
788 "used when creating a volume\n"));
795 /* check number of arguments */
797 (void) fprintf(stderr
, gettext("missing %s argument\n"),
798 zfs_type_to_name(type
));
802 (void) fprintf(stderr
, gettext("too many arguments\n"));
806 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
807 zpool_handle_t
*zpool_handle
;
808 uint64_t spa_version
;
810 zfs_prop_t resv_prop
;
813 if ((p
= strchr(argv
[0], '/')))
815 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
818 if (zpool_handle
== NULL
)
820 spa_version
= zpool_get_prop_int(zpool_handle
,
821 ZPOOL_PROP_VERSION
, NULL
);
822 zpool_close(zpool_handle
);
823 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
824 resv_prop
= ZFS_PROP_REFRESERVATION
;
826 resv_prop
= ZFS_PROP_RESERVATION
;
827 volsize
= zvol_volsize_to_reservation(volsize
, props
);
829 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
831 if (nvlist_add_uint64(props
,
832 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
839 if (parents
&& zfs_name_valid(argv
[0], type
)) {
841 * Now create the ancestors of target dataset. If the target
842 * already exists and '-p' option was used we should not
845 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
849 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
854 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
858 (void) zpool_log_history(g_zfs
, history_str
);
859 log_history
= B_FALSE
;
862 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
867 * if the user doesn't want the dataset automatically mounted,
868 * then skip the mount/share step
870 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
, B_FALSE
))
871 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
874 * Mount and/or share the new filesystem as appropriate. We provide a
875 * verbose error message to let the user know that their filesystem was
876 * in fact created, even if we failed to mount or share it.
878 if (canmount
== ZFS_CANMOUNT_ON
) {
879 if (zfs_mount(zhp
, NULL
, 0) != 0) {
880 (void) fprintf(stderr
, gettext("filesystem "
881 "successfully created, but not mounted\n"));
883 } else if (zfs_share(zhp
) != 0) {
884 (void) fprintf(stderr
, gettext("filesystem "
885 "successfully created, but not shared\n"));
902 * zfs destroy [-rRf] <fs, vol>
903 * zfs destroy [-rRd] <snap>
905 * -r Recursively destroy all children
906 * -R Recursively destroy all dependents, including clones
907 * -f Force unmounting of any dependents
908 * -d If we can't destroy now, mark for deferred destruction
910 * Destroys the given dataset. By default, it will unmount any filesystems,
911 * and refuse to destroy a dataset that has any dependents. A dependent can
912 * either be a child, or a clone of a child.
914 typedef struct destroy_cbdata
{
917 boolean_t cb_recurse
;
919 boolean_t cb_doclones
;
920 zfs_handle_t
*cb_target
;
921 boolean_t cb_defer_destroy
;
922 boolean_t cb_verbose
;
923 boolean_t cb_parsable
;
926 nvlist_t
*cb_batchedsnaps
;
928 /* first snap in contiguous run */
930 /* previous snap in contiguous run */
938 * Check for any dependents based on the '-r' or '-R' flags.
941 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
943 destroy_cbdata_t
*cbp
= data
;
944 const char *tname
= zfs_get_name(cbp
->cb_target
);
945 const char *name
= zfs_get_name(zhp
);
947 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
948 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
950 * This is a direct descendant, not a clone somewhere else in
957 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
958 "%s has children\n"),
959 zfs_get_name(cbp
->cb_target
),
960 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
961 (void) fprintf(stderr
, gettext("use '-r' to destroy "
962 "the following datasets:\n"));
963 cbp
->cb_first
= B_FALSE
;
964 cbp
->cb_error
= B_TRUE
;
967 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
970 * This is a clone. We only want to report this if the '-r'
971 * wasn't specified, or the target is a snapshot.
973 if (!cbp
->cb_recurse
&&
974 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
978 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
979 "%s has dependent clones\n"),
980 zfs_get_name(cbp
->cb_target
),
981 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
982 (void) fprintf(stderr
, gettext("use '-R' to destroy "
983 "the following datasets:\n"));
984 cbp
->cb_first
= B_FALSE
;
985 cbp
->cb_error
= B_TRUE
;
986 cbp
->cb_dryrun
= B_TRUE
;
989 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
998 destroy_callback(zfs_handle_t
*zhp
, void *data
)
1000 destroy_cbdata_t
*cb
= data
;
1001 const char *name
= zfs_get_name(zhp
);
1003 if (cb
->cb_verbose
) {
1004 if (cb
->cb_parsable
) {
1005 (void) printf("destroy\t%s\n", name
);
1006 } else if (cb
->cb_dryrun
) {
1007 (void) printf(gettext("would destroy %s\n"),
1010 (void) printf(gettext("will destroy %s\n"),
1016 * Ignore pools (which we've already flagged as an error before getting
1019 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1020 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1024 if (cb
->cb_dryrun
) {
1030 * We batch up all contiguous snapshots (even of different
1031 * filesystems) and destroy them with one ioctl. We can't
1032 * simply do all snap deletions and then all fs deletions,
1033 * because we must delete a clone before its origin.
1035 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1036 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1038 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1039 cb
->cb_batchedsnaps
, B_FALSE
);
1040 fnvlist_free(cb
->cb_batchedsnaps
);
1041 cb
->cb_batchedsnaps
= fnvlist_alloc();
1044 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1045 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1056 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1058 destroy_cbdata_t
*cb
= arg
;
1059 const char *name
= zfs_get_name(zhp
);
1062 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1063 if (cb
->cb_firstsnap
== NULL
)
1064 cb
->cb_firstsnap
= strdup(name
);
1065 if (cb
->cb_prevsnap
!= NULL
)
1066 free(cb
->cb_prevsnap
);
1067 /* this snap continues the current range */
1068 cb
->cb_prevsnap
= strdup(name
);
1069 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1071 if (cb
->cb_verbose
) {
1072 if (cb
->cb_parsable
) {
1073 (void) printf("destroy\t%s\n", name
);
1074 } else if (cb
->cb_dryrun
) {
1075 (void) printf(gettext("would destroy %s\n"),
1078 (void) printf(gettext("will destroy %s\n"),
1082 } else if (cb
->cb_firstsnap
!= NULL
) {
1083 /* end of this range */
1085 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1086 cb
->cb_prevsnap
, &used
);
1087 cb
->cb_snapused
+= used
;
1088 free(cb
->cb_firstsnap
);
1089 cb
->cb_firstsnap
= NULL
;
1090 free(cb
->cb_prevsnap
);
1091 cb
->cb_prevsnap
= NULL
;
1098 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1101 assert(cb
->cb_firstsnap
== NULL
);
1102 assert(cb
->cb_prevsnap
== NULL
);
1103 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1104 if (cb
->cb_firstsnap
!= NULL
) {
1107 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1108 cb
->cb_prevsnap
, &used
);
1110 cb
->cb_snapused
+= used
;
1111 free(cb
->cb_firstsnap
);
1112 cb
->cb_firstsnap
= NULL
;
1113 free(cb
->cb_prevsnap
);
1114 cb
->cb_prevsnap
= NULL
;
1120 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1122 destroy_cbdata_t
*cb
= arg
;
1125 /* Check for clones. */
1126 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1127 cb
->cb_target
= zhp
;
1128 cb
->cb_first
= B_TRUE
;
1129 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1130 destroy_check_dependent
, cb
);
1134 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1142 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1144 destroy_cbdata_t
*cb
= arg
;
1147 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1153 if (cb
->cb_verbose
) {
1154 err
= destroy_print_snapshots(zhp
, cb
);
1160 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1168 destroy_clones(destroy_cbdata_t
*cb
)
1171 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1173 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1174 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1177 boolean_t defer
= cb
->cb_defer_destroy
;
1181 * We can't defer destroy non-snapshots, so set it to
1182 * false while destroying the clones.
1184 cb
->cb_defer_destroy
= B_FALSE
;
1185 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1186 destroy_callback
, cb
);
1187 cb
->cb_defer_destroy
= defer
;
1197 zfs_do_destroy(int argc
, char **argv
)
1199 destroy_cbdata_t cb
= { 0 };
1203 zfs_handle_t
*zhp
= NULL
;
1205 zfs_type_t type
= ZFS_TYPE_DATASET
;
1208 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1211 cb
.cb_verbose
= B_TRUE
;
1214 cb
.cb_verbose
= B_TRUE
;
1215 cb
.cb_parsable
= B_TRUE
;
1218 cb
.cb_dryrun
= B_TRUE
;
1221 cb
.cb_defer_destroy
= B_TRUE
;
1222 type
= ZFS_TYPE_SNAPSHOT
;
1225 cb
.cb_force
= B_TRUE
;
1228 cb
.cb_recurse
= B_TRUE
;
1231 cb
.cb_recurse
= B_TRUE
;
1232 cb
.cb_doclones
= B_TRUE
;
1236 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1245 /* check number of arguments */
1247 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1251 (void) fprintf(stderr
, gettext("too many arguments\n"));
1255 at
= strchr(argv
[0], '@');
1256 pound
= strchr(argv
[0], '#');
1259 /* Build the list of snaps to destroy in cb_nvl. */
1260 cb
.cb_nvl
= fnvlist_alloc();
1263 zhp
= zfs_open(g_zfs
, argv
[0],
1264 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1268 cb
.cb_snapspec
= at
+ 1;
1269 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1275 if (nvlist_empty(cb
.cb_nvl
)) {
1276 (void) fprintf(stderr
, gettext("could not find any "
1277 "snapshots to destroy; check snapshot names.\n"));
1282 if (cb
.cb_verbose
) {
1284 zfs_nicenum(cb
.cb_snapused
, buf
, sizeof (buf
));
1285 if (cb
.cb_parsable
) {
1286 (void) printf("reclaim\t%llu\n",
1287 (u_longlong_t
)cb
.cb_snapused
);
1288 } else if (cb
.cb_dryrun
) {
1289 (void) printf(gettext("would reclaim %s\n"),
1292 (void) printf(gettext("will reclaim %s\n"),
1297 if (!cb
.cb_dryrun
) {
1298 if (cb
.cb_doclones
) {
1299 cb
.cb_batchedsnaps
= fnvlist_alloc();
1300 err
= destroy_clones(&cb
);
1302 err
= zfs_destroy_snaps_nvl(g_zfs
,
1303 cb
.cb_batchedsnaps
, B_FALSE
);
1311 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1312 cb
.cb_defer_destroy
);
1318 } else if (pound
!= NULL
) {
1323 (void) fprintf(stderr
,
1324 "dryrun is not supported with bookmark\n");
1328 if (cb
.cb_defer_destroy
) {
1329 (void) fprintf(stderr
,
1330 "defer destroy is not supported with bookmark\n");
1334 if (cb
.cb_recurse
) {
1335 (void) fprintf(stderr
,
1336 "recursive is not supported with bookmark\n");
1340 if (!zfs_bookmark_exists(argv
[0])) {
1341 (void) fprintf(stderr
, gettext("bookmark '%s' "
1342 "does not exist.\n"), argv
[0]);
1346 nvl
= fnvlist_alloc();
1347 fnvlist_add_boolean(nvl
, argv
[0]);
1349 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1351 (void) zfs_standard_error(g_zfs
, err
,
1352 "cannot destroy bookmark");
1355 nvlist_free(cb
.cb_nvl
);
1359 /* Open the given dataset */
1360 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1366 * Perform an explicit check for pools before going any further.
1368 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1369 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1370 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1371 "operation does not apply to pools\n"),
1373 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1374 "%s' to destroy all datasets in the pool\n"),
1376 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1377 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1383 * Check for any dependents and/or clones.
1385 cb
.cb_first
= B_TRUE
;
1386 if (!cb
.cb_doclones
&&
1387 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1398 cb
.cb_batchedsnaps
= fnvlist_alloc();
1399 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1406 * Do the real thing. The callback will close the
1407 * handle regardless of whether it succeeds or not.
1409 err
= destroy_callback(zhp
, &cb
);
1412 err
= zfs_destroy_snaps_nvl(g_zfs
,
1413 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1420 fnvlist_free(cb
.cb_batchedsnaps
);
1421 fnvlist_free(cb
.cb_nvl
);
1428 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1431 zfs_get_column_t col
;
1433 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1434 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1435 if (col
== GET_COL_RECVD
)
1441 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1442 * < all | property[,property]... > < fs | snap | vol > ...
1444 * -r recurse over any child datasets
1445 * -H scripted mode. Headers are stripped, and fields are separated
1446 * by tabs instead of spaces.
1447 * -o Set of fields to display. One of "name,property,value,
1448 * received,source". Default is "name,property,value,source".
1449 * "all" is an alias for all five.
1450 * -s Set of sources to allow. One of
1451 * "local,default,inherited,received,temporary,none". Default is
1453 * -p Display values in parsable (literal) format.
1455 * Prints properties for the given datasets. The user can control which
1456 * columns to display as well as which property types to allow.
1460 * Invoked to display the properties for a single dataset.
1463 get_callback(zfs_handle_t
*zhp
, void *data
)
1465 char buf
[ZFS_MAXPROPLEN
];
1466 char rbuf
[ZFS_MAXPROPLEN
];
1467 zprop_source_t sourcetype
;
1468 char source
[ZFS_MAXNAMELEN
];
1469 zprop_get_cbdata_t
*cbp
= data
;
1470 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1471 zprop_list_t
*pl
= cbp
->cb_proplist
;
1475 boolean_t received
= is_recvd_column(cbp
);
1477 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1478 char *recvdval
= NULL
;
1480 * Skip the special fake placeholder. This will also skip over
1481 * the name property when 'all' is specified.
1483 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1484 pl
== cbp
->cb_proplist
)
1487 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1488 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1489 sizeof (buf
), &sourcetype
, source
,
1491 cbp
->cb_literal
) != 0) {
1494 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1495 ZFS_TYPE_DATASET
, B_FALSE
)) {
1496 (void) fprintf(stderr
,
1497 gettext("No such property '%s'\n"),
1498 zfs_prop_to_name(pl
->pl_prop
));
1501 sourcetype
= ZPROP_SRC_NONE
;
1502 (void) strlcpy(buf
, "-", sizeof (buf
));
1505 if (received
&& (zfs_prop_get_recvd(zhp
,
1506 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1507 cbp
->cb_literal
) == 0))
1510 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1511 zfs_prop_to_name(pl
->pl_prop
),
1512 buf
, sourcetype
, source
, recvdval
);
1513 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1514 sourcetype
= ZPROP_SRC_LOCAL
;
1516 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1517 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1518 sourcetype
= ZPROP_SRC_NONE
;
1519 (void) strlcpy(buf
, "-", sizeof (buf
));
1522 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1523 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1524 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1525 sourcetype
= ZPROP_SRC_LOCAL
;
1527 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1528 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1529 sourcetype
= ZPROP_SRC_NONE
;
1530 (void) strlcpy(buf
, "-", sizeof (buf
));
1533 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1534 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1536 if (nvlist_lookup_nvlist(user_props
,
1537 pl
->pl_user_prop
, &propval
) != 0) {
1540 sourcetype
= ZPROP_SRC_NONE
;
1543 verify(nvlist_lookup_string(propval
,
1544 ZPROP_VALUE
, &strval
) == 0);
1545 verify(nvlist_lookup_string(propval
,
1546 ZPROP_SOURCE
, &sourceval
) == 0);
1548 if (strcmp(sourceval
,
1549 zfs_get_name(zhp
)) == 0) {
1550 sourcetype
= ZPROP_SRC_LOCAL
;
1551 } else if (strcmp(sourceval
,
1552 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1553 sourcetype
= ZPROP_SRC_RECEIVED
;
1555 sourcetype
= ZPROP_SRC_INHERITED
;
1556 (void) strlcpy(source
,
1557 sourceval
, sizeof (source
));
1561 if (received
&& (zfs_prop_get_recvd(zhp
,
1562 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1563 cbp
->cb_literal
) == 0))
1566 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1567 pl
->pl_user_prop
, strval
, sourcetype
,
1576 zfs_do_get(int argc
, char **argv
)
1578 zprop_get_cbdata_t cb
= { 0 };
1579 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1580 int types
= ZFS_TYPE_DATASET
;
1581 char *value
, *fields
;
1584 zprop_list_t fake_name
= { 0 };
1587 * Set up default columns and sources.
1589 cb
.cb_sources
= ZPROP_SRC_ALL
;
1590 cb
.cb_columns
[0] = GET_COL_NAME
;
1591 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1592 cb
.cb_columns
[2] = GET_COL_VALUE
;
1593 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1594 cb
.cb_type
= ZFS_TYPE_DATASET
;
1597 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1600 cb
.cb_literal
= B_TRUE
;
1603 limit
= parse_depth(optarg
, &flags
);
1606 flags
|= ZFS_ITER_RECURSE
;
1609 cb
.cb_scripted
= B_TRUE
;
1612 (void) fprintf(stderr
, gettext("missing argument for "
1613 "'%c' option\n"), optopt
);
1618 * Process the set of columns to display. We zero out
1619 * the structure to give us a blank slate.
1621 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1623 while (*optarg
!= '\0') {
1624 static char *col_subopts
[] =
1625 { "name", "property", "value", "received",
1626 "source", "all", NULL
};
1628 if (i
== ZFS_GET_NCOLS
) {
1629 (void) fprintf(stderr
, gettext("too "
1630 "many fields given to -o "
1635 switch (getsubopt(&optarg
, col_subopts
,
1638 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1641 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1644 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1647 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1648 flags
|= ZFS_ITER_RECVD_PROPS
;
1651 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1655 (void) fprintf(stderr
,
1656 gettext("\"all\" conflicts "
1657 "with specific fields "
1658 "given to -o option\n"));
1661 cb
.cb_columns
[0] = GET_COL_NAME
;
1662 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1663 cb
.cb_columns
[2] = GET_COL_VALUE
;
1664 cb
.cb_columns
[3] = GET_COL_RECVD
;
1665 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1666 flags
|= ZFS_ITER_RECVD_PROPS
;
1670 (void) fprintf(stderr
,
1671 gettext("invalid column name "
1680 while (*optarg
!= '\0') {
1681 static char *source_subopts
[] = {
1682 "local", "default", "inherited",
1683 "received", "temporary", "none",
1686 switch (getsubopt(&optarg
, source_subopts
,
1689 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1692 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1695 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1698 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1701 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1704 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1707 (void) fprintf(stderr
,
1708 gettext("invalid source "
1717 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1718 while (*optarg
!= '\0') {
1719 static char *type_subopts
[] = { "filesystem",
1720 "volume", "snapshot", "bookmark",
1723 switch (getsubopt(&optarg
, type_subopts
,
1726 types
|= ZFS_TYPE_FILESYSTEM
;
1729 types
|= ZFS_TYPE_VOLUME
;
1732 types
|= ZFS_TYPE_SNAPSHOT
;
1735 types
|= ZFS_TYPE_BOOKMARK
;
1738 types
= ZFS_TYPE_DATASET
|
1743 (void) fprintf(stderr
,
1744 gettext("invalid type '%s'\n"),
1752 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1762 (void) fprintf(stderr
, gettext("missing property "
1769 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1777 * As part of zfs_expand_proplist(), we keep track of the maximum column
1778 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1779 * need to know the maximum name length. However, the user likely did
1780 * not specify 'name' as one of the properties to fetch, so we need to
1781 * make sure we always include at least this property for
1782 * print_get_headers() to work properly.
1784 if (cb
.cb_proplist
!= NULL
) {
1785 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1786 fake_name
.pl_width
= strlen(gettext("NAME"));
1787 fake_name
.pl_next
= cb
.cb_proplist
;
1788 cb
.cb_proplist
= &fake_name
;
1791 cb
.cb_first
= B_TRUE
;
1793 /* run for each object */
1794 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1795 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1797 if (cb
.cb_proplist
== &fake_name
)
1798 zprop_free_list(fake_name
.pl_next
);
1800 zprop_free_list(cb
.cb_proplist
);
1806 * inherit [-rS] <property> <fs|vol> ...
1808 * -r Recurse over all children
1809 * -S Revert to received value, if any
1811 * For each dataset specified on the command line, inherit the given property
1812 * from its parent. Inheriting a property at the pool level will cause it to
1813 * use the default value. The '-r' flag will recurse over all children, and is
1814 * useful for setting a property on a hierarchy-wide basis, regardless of any
1815 * local modifications for each dataset.
1818 typedef struct inherit_cbdata
{
1819 const char *cb_propname
;
1820 boolean_t cb_received
;
1824 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1826 inherit_cbdata_t
*cb
= data
;
1827 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1830 * If we're doing it recursively, then ignore properties that
1831 * are not valid for this type of dataset.
1833 if (prop
!= ZPROP_INVAL
&&
1834 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
), B_FALSE
))
1837 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1841 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1843 inherit_cbdata_t
*cb
= data
;
1845 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1849 zfs_do_inherit(int argc
, char **argv
)
1853 inherit_cbdata_t cb
= { 0 };
1857 boolean_t received
= B_FALSE
;
1860 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1863 flags
|= ZFS_ITER_RECURSE
;
1870 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1879 /* check number of arguments */
1881 (void) fprintf(stderr
, gettext("missing property argument\n"));
1885 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1893 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1894 if (zfs_prop_readonly(prop
)) {
1895 (void) fprintf(stderr
, gettext(
1896 "%s property is read-only\n"),
1900 if (!zfs_prop_inheritable(prop
) && !received
) {
1901 (void) fprintf(stderr
, gettext("'%s' property cannot "
1902 "be inherited\n"), propname
);
1903 if (prop
== ZFS_PROP_QUOTA
||
1904 prop
== ZFS_PROP_RESERVATION
||
1905 prop
== ZFS_PROP_REFQUOTA
||
1906 prop
== ZFS_PROP_REFRESERVATION
)
1907 (void) fprintf(stderr
, gettext("use 'zfs set "
1908 "%s=none' to clear\n"), propname
);
1911 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1912 prop
== ZFS_PROP_VERSION
)) {
1913 (void) fprintf(stderr
, gettext("'%s' property cannot "
1914 "be reverted to a received value\n"), propname
);
1917 } else if (!zfs_prop_user(propname
)) {
1918 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1923 cb
.cb_propname
= propname
;
1924 cb
.cb_received
= received
;
1926 if (flags
& ZFS_ITER_RECURSE
) {
1927 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1928 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1930 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1931 NULL
, NULL
, 0, inherit_cb
, &cb
);
1937 typedef struct upgrade_cbdata
{
1938 uint64_t cb_numupgraded
;
1939 uint64_t cb_numsamegraded
;
1940 uint64_t cb_numfailed
;
1941 uint64_t cb_version
;
1943 boolean_t cb_foundone
;
1944 char cb_lastfs
[ZFS_MAXNAMELEN
];
1948 same_pool(zfs_handle_t
*zhp
, const char *name
)
1950 int len1
= strcspn(name
, "/@");
1951 const char *zhname
= zfs_get_name(zhp
);
1952 int len2
= strcspn(zhname
, "/@");
1956 return (strncmp(name
, zhname
, len1
) == 0);
1960 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
1962 upgrade_cbdata_t
*cb
= data
;
1963 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1965 /* list if it's old/new */
1966 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
1967 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
1970 str
= gettext("The following filesystems are "
1971 "formatted using a newer software version and\n"
1972 "cannot be accessed on the current system.\n\n");
1974 str
= gettext("The following filesystems are "
1975 "out of date, and can be upgraded. After being\n"
1976 "upgraded, these filesystems (and any 'zfs send' "
1977 "streams generated from\n"
1978 "subsequent snapshots) will no longer be "
1979 "accessible by older software versions.\n\n");
1982 if (!cb
->cb_foundone
) {
1984 (void) printf(gettext("VER FILESYSTEM\n"));
1985 (void) printf(gettext("--- ------------\n"));
1986 cb
->cb_foundone
= B_TRUE
;
1989 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
1996 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
1998 upgrade_cbdata_t
*cb
= data
;
1999 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2000 int needed_spa_version
;
2003 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2006 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2008 if (needed_spa_version
< 0)
2011 if (spa_version
< needed_spa_version
) {
2013 (void) printf(gettext("%s: can not be "
2014 "upgraded; the pool version needs to first "
2015 "be upgraded\nto version %d\n\n"),
2016 zfs_get_name(zhp
), needed_spa_version
);
2022 if (version
< cb
->cb_version
) {
2024 (void) snprintf(verstr
, sizeof (verstr
),
2025 "%llu", (u_longlong_t
)cb
->cb_version
);
2026 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2028 * If they did "zfs upgrade -a", then we could
2029 * be doing ioctls to different pools. We need
2030 * to log this history once to each pool, and bypass
2031 * the normal history logging that happens in main().
2033 (void) zpool_log_history(g_zfs
, history_str
);
2034 log_history
= B_FALSE
;
2036 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2037 cb
->cb_numupgraded
++;
2040 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2041 } else if (version
> cb
->cb_version
) {
2042 /* can't downgrade */
2043 (void) printf(gettext("%s: can not be downgraded; "
2044 "it is already at version %u\n"),
2045 zfs_get_name(zhp
), version
);
2048 cb
->cb_numsamegraded
++;
2056 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2059 zfs_do_upgrade(int argc
, char **argv
)
2061 boolean_t all
= B_FALSE
;
2062 boolean_t showversions
= B_FALSE
;
2064 upgrade_cbdata_t cb
= { 0 };
2066 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2069 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2072 flags
|= ZFS_ITER_RECURSE
;
2075 showversions
= B_TRUE
;
2078 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2079 optarg
, &cb
.cb_version
) != 0) {
2080 (void) fprintf(stderr
,
2081 gettext("invalid version %s\n"), optarg
);
2090 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2099 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2101 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2102 cb
.cb_version
|| argc
))
2104 if ((all
|| argc
) && (showversions
))
2110 /* Show info on available versions. */
2111 (void) printf(gettext("The following filesystem versions are "
2113 (void) printf(gettext("VER DESCRIPTION\n"));
2114 (void) printf("--- -----------------------------------------"
2115 "---------------\n");
2116 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2117 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2118 (void) printf(gettext(" 3 Case insensitive and filesystem "
2119 "user identifier (FUID)\n"));
2120 (void) printf(gettext(" 4 userquota, groupquota "
2122 (void) printf(gettext(" 5 System attributes\n"));
2123 (void) printf(gettext("\nFor more information on a particular "
2124 "version, including supported releases,\n"));
2125 (void) printf("see the ZFS Administration Guide.\n\n");
2127 } else if (argc
|| all
) {
2128 /* Upgrade filesystems */
2129 if (cb
.cb_version
== 0)
2130 cb
.cb_version
= ZPL_VERSION
;
2131 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2132 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2133 (void) printf(gettext("%llu filesystems upgraded\n"),
2134 (u_longlong_t
)cb
.cb_numupgraded
);
2135 if (cb
.cb_numsamegraded
) {
2136 (void) printf(gettext("%llu filesystems already at "
2138 (u_longlong_t
)cb
.cb_numsamegraded
);
2140 if (cb
.cb_numfailed
!= 0)
2143 /* List old-version filesytems */
2145 (void) printf(gettext("This system is currently running "
2146 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2148 flags
|= ZFS_ITER_RECURSE
;
2149 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2150 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2152 found
= cb
.cb_foundone
;
2153 cb
.cb_foundone
= B_FALSE
;
2154 cb
.cb_newer
= B_TRUE
;
2156 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2157 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2159 if (!cb
.cb_foundone
&& !found
) {
2160 (void) printf(gettext("All filesystems are "
2161 "formatted with the current version.\n"));
2169 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2170 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2171 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2172 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2174 * -H Scripted mode; elide headers and separate columns by tabs.
2175 * -i Translate SID to POSIX ID.
2176 * -n Print numeric ID instead of user/group name.
2177 * -o Control which fields to display.
2178 * -p Use exact (parsable) numeric output.
2179 * -s Specify sort columns, descending order.
2180 * -S Specify sort columns, ascending order.
2181 * -t Control which object types to display.
2183 * Displays space consumed by, and quotas on, each user in the specified
2184 * filesystem or snapshot.
2187 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2188 enum us_field_types
{
2194 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA" };
2195 static char *us_field_names
[] = { "type", "name", "used", "quota" };
2196 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2198 #define USTYPE_PSX_GRP (1 << 0)
2199 #define USTYPE_PSX_USR (1 << 1)
2200 #define USTYPE_SMB_GRP (1 << 2)
2201 #define USTYPE_SMB_USR (1 << 3)
2202 #define USTYPE_ALL \
2203 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2205 static int us_type_bits
[] = {
2212 static char *us_type_names
[] = { "posixgroup", "posixuser", "smbgroup",
2215 typedef struct us_node
{
2217 uu_avl_node_t usn_avlnode
;
2218 uu_list_node_t usn_listnode
;
2221 typedef struct us_cbdata
{
2223 uu_avl_pool_t
*cb_avl_pool
;
2225 boolean_t cb_numname
;
2226 boolean_t cb_nicenum
;
2227 boolean_t cb_sid2posix
;
2228 zfs_userquota_prop_t cb_prop
;
2229 zfs_sort_column_t
*cb_sortcol
;
2230 size_t cb_width
[USFIELD_LAST
];
2233 static boolean_t us_populated
= B_FALSE
;
2236 zfs_sort_column_t
*si_sortcol
;
2237 boolean_t si_numname
;
2241 us_field_index(char *field
)
2245 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2246 if (strcmp(field
, us_field_names
[i
]) == 0)
2254 us_compare(const void *larg
, const void *rarg
, void *unused
)
2256 const us_node_t
*l
= larg
;
2257 const us_node_t
*r
= rarg
;
2258 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2259 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2260 boolean_t numname
= si
->si_numname
;
2261 nvlist_t
*lnvl
= l
->usn_nvl
;
2262 nvlist_t
*rnvl
= r
->usn_nvl
;
2266 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2273 zfs_prop_t prop
= sortcol
->sc_prop
;
2274 const char *propname
= NULL
;
2275 boolean_t reverse
= sortcol
->sc_reverse
;
2280 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2281 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2283 rc
= (rv32
< lv32
) ? 1 : -1;
2288 (void) nvlist_lookup_uint64(lnvl
, propname
,
2290 (void) nvlist_lookup_uint64(rnvl
, propname
,
2293 rc
= (rv64
< lv64
) ? 1 : -1;
2295 (void) nvlist_lookup_string(lnvl
, propname
,
2297 (void) nvlist_lookup_string(rnvl
, propname
,
2299 rc
= strcmp(lvstr
, rvstr
);
2303 case ZFS_PROP_QUOTA
:
2306 if (prop
== ZFS_PROP_USED
)
2310 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2311 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2313 rc
= (rv64
< lv64
) ? 1 : -1;
2321 return (reverse
? 1 : -1);
2323 return (reverse
? -1 : 1);
2328 * If entries still seem to be the same, check if they are of the same
2329 * type (smbentity is added only if we are doing SID to POSIX ID
2330 * translation where we can have duplicate type/name combinations).
2332 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2333 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2335 return (lvb
< rvb
? -1 : 1);
2340 static inline const char *
2341 us_type2str(unsigned field_type
)
2343 switch (field_type
) {
2344 case USTYPE_PSX_USR
:
2345 return ("POSIX User");
2346 case USTYPE_PSX_GRP
:
2347 return ("POSIX Group");
2348 case USTYPE_SMB_USR
:
2349 return ("SMB User");
2350 case USTYPE_SMB_GRP
:
2351 return ("SMB Group");
2353 return ("Undefined");
2358 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2360 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2361 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2366 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2367 uu_avl_t
*avl
= cb
->cb_avl
;
2371 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2373 const char *typestr
;
2377 int typeidx
, nameidx
, sizeidx
;
2378 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2379 boolean_t smbentity
= B_FALSE
;
2381 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2383 node
= safe_malloc(sizeof (us_node_t
));
2384 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2385 node
->usn_nvl
= props
;
2387 if (domain
!= NULL
&& domain
[0] != '\0') {
2390 char sid
[ZFS_MAXNAMELEN
+ 32];
2394 directory_error_t e
;
2398 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2400 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2401 type
= USTYPE_SMB_GRP
;
2402 err
= sid_to_id(sid
, B_FALSE
, &id
);
2404 type
= USTYPE_SMB_USR
;
2405 err
= sid_to_id(sid
, B_TRUE
, &id
);
2410 if (!cb
->cb_sid2posix
) {
2411 e
= directory_name_from_sid(NULL
, sid
, &name
,
2414 directory_error_free(e
);
2424 #endif /* HAVE_IDMAP */
2427 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2429 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2430 type
= USTYPE_PSX_GRP
;
2431 if (!cb
->cb_numname
) {
2434 if ((g
= getgrgid(rid
)) != NULL
)
2438 type
= USTYPE_PSX_USR
;
2439 if (!cb
->cb_numname
) {
2442 if ((p
= getpwuid(rid
)) != NULL
)
2449 * Make sure that the type/name combination is unique when doing
2450 * SID to POSIX ID translation (hence changing the type from SMB to
2453 if (cb
->cb_sid2posix
&&
2454 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2457 /* Calculate/update width of TYPE field */
2458 typestr
= us_type2str(type
);
2459 typelen
= strlen(gettext(typestr
));
2460 typeidx
= us_field_index("type");
2461 if (typelen
> cb
->cb_width
[typeidx
])
2462 cb
->cb_width
[typeidx
] = typelen
;
2463 if (nvlist_add_uint32(props
, "type", type
) != 0)
2466 /* Calculate/update width of NAME field */
2467 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2468 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2470 namelen
= snprintf(NULL
, 0, "%u", rid
);
2472 if (nvlist_add_string(props
, "name", name
) != 0)
2474 namelen
= strlen(name
);
2476 nameidx
= us_field_index("name");
2477 if (namelen
> cb
->cb_width
[nameidx
])
2478 cb
->cb_width
[nameidx
] = namelen
;
2481 * Check if this type/name combination is in the list and update it;
2482 * otherwise add new node to the list.
2484 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2485 uu_avl_insert(avl
, node
, idx
);
2490 props
= node
->usn_nvl
;
2493 /* Calculate/update width of USED/QUOTA fields */
2495 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2497 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu",
2498 (u_longlong_t
)space
);
2499 sizelen
= strlen(sizebuf
);
2500 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
) {
2502 if (!nvlist_exists(props
, "quota"))
2503 (void) nvlist_add_uint64(props
, "quota", 0);
2506 if (!nvlist_exists(props
, "used"))
2507 (void) nvlist_add_uint64(props
, "used", 0);
2509 sizeidx
= us_field_index(propname
);
2510 if (sizelen
> cb
->cb_width
[sizeidx
])
2511 cb
->cb_width
[sizeidx
] = sizelen
;
2513 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2520 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2521 size_t *width
, us_node_t
*node
)
2523 nvlist_t
*nvl
= node
->usn_nvl
;
2524 char valstr
[ZFS_MAXNAMELEN
];
2525 boolean_t first
= B_TRUE
;
2531 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2532 if (!(ustype
& types
))
2535 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2536 nvpair_t
*nvp
= NULL
;
2540 char *strval
= NULL
;
2542 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2543 if (strcmp(nvpair_name(nvp
),
2544 us_field_names
[field
]) == 0)
2548 type
= nvpair_type(nvp
);
2550 case DATA_TYPE_UINT32
:
2551 (void) nvpair_value_uint32(nvp
, &val32
);
2553 case DATA_TYPE_UINT64
:
2554 (void) nvpair_value_uint64(nvp
, &val64
);
2556 case DATA_TYPE_STRING
:
2557 (void) nvpair_value_string(nvp
, &strval
);
2560 (void) fprintf(stderr
, "invalid data type\n");
2565 strval
= (char *)us_type2str(val32
);
2568 if (type
== DATA_TYPE_UINT64
) {
2569 (void) sprintf(valstr
, "%llu",
2570 (u_longlong_t
) val64
);
2576 if (type
== DATA_TYPE_UINT64
) {
2578 (void) sprintf(valstr
, "%llu",
2579 (u_longlong_t
) val64
);
2581 zfs_nicenum(val64
, valstr
,
2584 if (field
== USFIELD_QUOTA
&&
2585 strcmp(valstr
, "0") == 0)
2595 (void) printf("\t");
2600 (void) printf("%s", strval
);
2601 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2602 (void) printf("%-*s", (int) width
[field
], strval
);
2604 (void) printf("%*s", (int) width
[field
], strval
);
2610 (void) printf("\n");
2614 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2615 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2623 boolean_t first
= B_TRUE
;
2625 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2626 col
= gettext(us_field_hdr
[field
]);
2627 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2628 (void) printf(first
? "%-*s" : " %-*s",
2629 (int) width
[field
], col
);
2631 (void) printf(first
? "%*s" : " %*s",
2632 (int) width
[field
], col
);
2637 (void) printf("\n");
2640 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2641 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2643 nvlist_free(node
->usn_nvl
);
2648 zfs_do_userspace(int argc
, char **argv
)
2651 zfs_userquota_prop_t p
;
2652 uu_avl_pool_t
*avl_pool
;
2654 uu_avl_walk_t
*walk
;
2656 char deffields
[] = "type,name,used,quota";
2657 char *ofield
= NULL
;
2658 char *tfield
= NULL
;
2662 boolean_t scripted
= B_FALSE
;
2663 boolean_t prtnum
= B_FALSE
;
2664 boolean_t parsable
= B_FALSE
;
2665 boolean_t sid2posix
= B_FALSE
;
2668 zfs_sort_column_t
*sortcol
= NULL
;
2669 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2673 uu_list_pool_t
*listpool
;
2675 uu_avl_index_t idx
= 0;
2676 uu_list_index_t idx2
= 0;
2681 if (strcmp(argv
[0], "groupspace") == 0)
2682 /* Toggle default group types */
2683 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2685 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2701 if (zfs_add_sort_column(&sortcol
, optarg
,
2702 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
2703 (void) fprintf(stderr
,
2704 gettext("invalid field '%s'\n"), optarg
);
2715 (void) fprintf(stderr
, gettext("missing argument for "
2716 "'%c' option\n"), optopt
);
2720 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2730 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2734 (void) fprintf(stderr
, gettext("too many arguments\n"));
2738 /* Use default output fields if not specified using -o */
2742 if ((delim
= strchr(ofield
, ',')) != NULL
)
2744 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
2745 (void) fprintf(stderr
, gettext("invalid type '%s' "
2746 "for -o option\n"), ofield
);
2751 } while (delim
!= NULL
);
2752 fields
[cfield
] = USFIELD_LAST
;
2754 /* Override output types (-t option) */
2755 if (tfield
!= NULL
) {
2759 boolean_t found
= B_FALSE
;
2761 if ((delim
= strchr(tfield
, ',')) != NULL
)
2763 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
2765 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
2767 types
|= us_type_bits
[i
];
2772 (void) fprintf(stderr
, gettext("invalid type "
2773 "'%s' for -t option\n"), tfield
);
2778 } while (delim
!= NULL
);
2781 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
2784 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
2785 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
2787 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
2790 /* Always add default sorting columns */
2791 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
2792 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
2794 cb
.cb_sortcol
= sortcol
;
2795 cb
.cb_numname
= prtnum
;
2796 cb
.cb_nicenum
= !parsable
;
2797 cb
.cb_avl_pool
= avl_pool
;
2798 cb
.cb_avl
= avl_tree
;
2799 cb
.cb_sid2posix
= sid2posix
;
2801 for (i
= 0; i
< USFIELD_LAST
; i
++)
2802 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
2804 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
2805 if (((p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
) &&
2806 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
2807 ((p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
) &&
2808 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))))
2811 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
2816 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
2819 us_populated
= B_TRUE
;
2821 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
2822 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
2823 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
2824 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
2826 while (node
!= NULL
) {
2828 node
= uu_avl_next(avl_tree
, node
);
2829 uu_avl_remove(avl_tree
, rmnode
);
2830 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
2831 uu_list_insert(list
, rmnode
, idx2
);
2834 for (node
= uu_list_first(list
); node
!= NULL
;
2835 node
= uu_list_next(list
, node
)) {
2836 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
2838 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
2839 uu_avl_insert(avl_tree
, node
, idx
);
2842 uu_list_destroy(list
);
2843 uu_list_pool_destroy(listpool
);
2845 /* Print and free node nvlist memory */
2846 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
2849 zfs_free_sort_columns(sortcol
);
2851 /* Clean up the AVL tree */
2852 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
2855 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
2856 uu_avl_remove(cb
.cb_avl
, node
);
2860 uu_avl_walk_end(walk
);
2861 uu_avl_destroy(avl_tree
);
2862 uu_avl_pool_destroy(avl_pool
);
2868 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
2869 * [-t type[,...]] [filesystem|volume|snapshot] ...
2871 * -H Scripted mode; elide headers and separate columns by tabs
2872 * -p Display values in parsable (literal) format.
2873 * -r Recurse over all children
2874 * -d Limit recursion by depth.
2875 * -o Control which fields to display.
2876 * -s Specify sort columns, descending order.
2877 * -S Specify sort columns, ascending order.
2878 * -t Control which object types to display.
2880 * When given no arguments, list all filesystems in the system.
2881 * Otherwise, list the specified datasets, optionally recursing down them if
2882 * '-r' is specified.
2884 typedef struct list_cbdata
{
2886 boolean_t cb_literal
;
2887 boolean_t cb_scripted
;
2888 zprop_list_t
*cb_proplist
;
2892 * Given a list of columns to display, output appropriate headers for each one.
2895 print_header(list_cbdata_t
*cb
)
2897 zprop_list_t
*pl
= cb
->cb_proplist
;
2898 char headerbuf
[ZFS_MAXPROPLEN
];
2901 boolean_t first
= B_TRUE
;
2902 boolean_t right_justify
;
2904 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2911 right_justify
= B_FALSE
;
2912 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2913 header
= zfs_prop_column_name(pl
->pl_prop
);
2914 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2916 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2917 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2918 headerbuf
[i
] = '\0';
2922 if (pl
->pl_next
== NULL
&& !right_justify
)
2923 (void) printf("%s", header
);
2924 else if (right_justify
)
2925 (void) printf("%*s", (int)pl
->pl_width
, header
);
2927 (void) printf("%-*s", (int)pl
->pl_width
, header
);
2930 (void) printf("\n");
2934 * Given a dataset and a list of fields, print out all the properties according
2935 * to the described layout.
2938 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
2940 zprop_list_t
*pl
= cb
->cb_proplist
;
2941 boolean_t first
= B_TRUE
;
2942 char property
[ZFS_MAXPROPLEN
];
2943 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
2946 boolean_t right_justify
;
2948 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2950 if (cb
->cb_scripted
)
2951 (void) printf("\t");
2958 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
2959 (void) strlcpy(property
, zfs_get_name(zhp
),
2962 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2963 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
2964 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
2965 sizeof (property
), NULL
, NULL
, 0,
2966 cb
->cb_literal
) != 0)
2970 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2971 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
2972 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
2973 property
, sizeof (property
), cb
->cb_literal
) != 0)
2977 right_justify
= B_TRUE
;
2978 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
2979 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
2980 property
, sizeof (property
), cb
->cb_literal
) != 0)
2984 right_justify
= B_TRUE
;
2986 if (nvlist_lookup_nvlist(userprops
,
2987 pl
->pl_user_prop
, &propval
) != 0)
2990 verify(nvlist_lookup_string(propval
,
2991 ZPROP_VALUE
, &propstr
) == 0);
2992 right_justify
= B_FALSE
;
2996 * If this is being called in scripted mode, or if this is the
2997 * last column and it is left-justified, don't include a width
3000 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3001 (void) printf("%s", propstr
);
3002 else if (right_justify
)
3003 (void) printf("%*s", (int)pl
->pl_width
, propstr
);
3005 (void) printf("%-*s", (int)pl
->pl_width
, propstr
);
3008 (void) printf("\n");
3012 * Generic callback function to list a dataset or snapshot.
3015 list_callback(zfs_handle_t
*zhp
, void *data
)
3017 list_cbdata_t
*cbp
= data
;
3019 if (cbp
->cb_first
) {
3020 if (!cbp
->cb_scripted
)
3022 cbp
->cb_first
= B_FALSE
;
3025 print_dataset(zhp
, cbp
);
3031 zfs_do_list(int argc
, char **argv
)
3034 static char default_fields
[] =
3035 "name,used,available,referenced,mountpoint";
3036 int types
= ZFS_TYPE_DATASET
;
3037 boolean_t types_specified
= B_FALSE
;
3038 char *fields
= NULL
;
3039 list_cbdata_t cb
= { 0 };
3043 zfs_sort_column_t
*sortcol
= NULL
;
3044 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3047 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3053 cb
.cb_literal
= B_TRUE
;
3054 flags
|= ZFS_ITER_LITERAL_PROPS
;
3057 limit
= parse_depth(optarg
, &flags
);
3060 flags
|= ZFS_ITER_RECURSE
;
3063 cb
.cb_scripted
= B_TRUE
;
3066 if (zfs_add_sort_column(&sortcol
, optarg
,
3068 (void) fprintf(stderr
,
3069 gettext("invalid property '%s'\n"), optarg
);
3074 if (zfs_add_sort_column(&sortcol
, optarg
,
3076 (void) fprintf(stderr
,
3077 gettext("invalid property '%s'\n"), optarg
);
3083 types_specified
= B_TRUE
;
3084 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3085 while (*optarg
!= '\0') {
3086 static char *type_subopts
[] = { "filesystem",
3087 "volume", "snapshot", "snap", "bookmark",
3090 switch (getsubopt(&optarg
, type_subopts
,
3093 types
|= ZFS_TYPE_FILESYSTEM
;
3096 types
|= ZFS_TYPE_VOLUME
;
3100 types
|= ZFS_TYPE_SNAPSHOT
;
3103 types
|= ZFS_TYPE_BOOKMARK
;
3106 types
= ZFS_TYPE_DATASET
|
3110 (void) fprintf(stderr
,
3111 gettext("invalid type '%s'\n"),
3118 (void) fprintf(stderr
, gettext("missing argument for "
3119 "'%c' option\n"), optopt
);
3123 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3133 fields
= default_fields
;
3136 * If we are only going to list snapshot names and sort by name,
3137 * then we can use faster version.
3139 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3140 flags
|= ZFS_ITER_SIMPLE
;
3143 * If "-o space" and no types were specified, don't display snapshots.
3145 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3146 types
&= ~ZFS_TYPE_SNAPSHOT
;
3149 * If the user specifies '-o all', the zprop_get_list() doesn't
3150 * normally include the name of the dataset. For 'zfs list', we always
3151 * want this property to be first.
3153 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3157 cb
.cb_first
= B_TRUE
;
3159 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3160 limit
, list_callback
, &cb
);
3162 zprop_free_list(cb
.cb_proplist
);
3163 zfs_free_sort_columns(sortcol
);
3165 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3166 (void) fprintf(stderr
, gettext("no datasets available\n"));
3172 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3173 * zfs rename [-f] -p <fs | vol> <fs | vol>
3174 * zfs rename -r <snap> <snap>
3176 * Renames the given dataset to another of the same type.
3178 * The '-p' flag creates all the non-existing ancestors of the target first.
3182 zfs_do_rename(int argc
, char **argv
)
3187 boolean_t recurse
= B_FALSE
;
3188 boolean_t parents
= B_FALSE
;
3189 boolean_t force_unmount
= B_FALSE
;
3192 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3201 force_unmount
= B_TRUE
;
3205 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3214 /* check number of arguments */
3216 (void) fprintf(stderr
, gettext("missing source dataset "
3221 (void) fprintf(stderr
, gettext("missing target dataset "
3226 (void) fprintf(stderr
, gettext("too many arguments\n"));
3230 if (recurse
&& parents
) {
3231 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3236 if (recurse
&& strchr(argv
[0], '@') == 0) {
3237 (void) fprintf(stderr
, gettext("source dataset for recursive "
3238 "rename must be a snapshot\n"));
3242 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3243 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3246 /* If we were asked and the name looks good, try to create ancestors. */
3247 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3248 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3253 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3262 * Promotes the given clone fs to be the parent
3266 zfs_do_promote(int argc
, char **argv
)
3272 if (argc
> 1 && argv
[1][0] == '-') {
3273 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3278 /* check number of arguments */
3280 (void) fprintf(stderr
, gettext("missing clone filesystem"
3285 (void) fprintf(stderr
, gettext("too many arguments\n"));
3289 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3293 ret
= (zfs_promote(zhp
) != 0);
3301 * zfs rollback [-rRf] <snapshot>
3303 * -r Delete any intervening snapshots before doing rollback
3304 * -R Delete any snapshots and their clones
3305 * -f ignored for backwards compatability
3307 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3308 * since then and making it the active dataset. If more recent snapshots exist,
3309 * the command will complain unless the '-r' flag is given.
3311 typedef struct rollback_cbdata
{
3317 boolean_t cb_recurse
;
3318 } rollback_cbdata_t
;
3321 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3323 rollback_cbdata_t
*cbp
= data
;
3325 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3326 (void) fprintf(stderr
, gettext("cannot rollback to "
3327 "'%s': clones of previous snapshots exist\n"),
3329 (void) fprintf(stderr
, gettext("use '-R' to "
3330 "force deletion of the following clones and "
3336 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3344 * Report any snapshots more recent than the one specified. Used when '-r' is
3345 * not specified. We reuse this same callback for the snapshot dependents - if
3346 * 'cb_dependent' is set, then this is a dependent and we should report it
3347 * without checking the transaction group.
3350 rollback_check(zfs_handle_t
*zhp
, void *data
)
3352 rollback_cbdata_t
*cbp
= data
;
3354 if (cbp
->cb_doclones
) {
3359 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3360 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3361 (void) fprintf(stderr
, gettext("cannot "
3362 "rollback to '%s': more recent snapshots "
3363 "or bookmarks exist\n"),
3365 (void) fprintf(stderr
, gettext("use '-r' to "
3366 "force deletion of the following "
3367 "snapshots and bookmarks:\n"));
3372 if (cbp
->cb_recurse
) {
3373 if (zfs_iter_dependents(zhp
, B_TRUE
,
3374 rollback_check_dependent
, cbp
) != 0) {
3379 (void) fprintf(stderr
, "%s\n",
3388 zfs_do_rollback(int argc
, char **argv
)
3392 boolean_t force
= B_FALSE
;
3393 rollback_cbdata_t cb
= { 0 };
3394 zfs_handle_t
*zhp
, *snap
;
3395 char parentname
[ZFS_MAXNAMELEN
];
3399 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3412 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3421 /* check number of arguments */
3423 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3427 (void) fprintf(stderr
, gettext("too many arguments\n"));
3431 /* open the snapshot */
3432 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3435 /* open the parent dataset */
3436 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3437 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3439 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3445 * Check for more recent snapshots and/or clones based on the presence
3448 cb
.cb_target
= argv
[0];
3449 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3450 cb
.cb_first
= B_TRUE
;
3452 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, rollback_check
, &cb
)) != 0)
3454 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
3457 if ((ret
= cb
.cb_error
) != 0)
3461 * Rollback parent to the given snapshot.
3463 ret
= zfs_rollback(zhp
, snap
, force
);
3476 * zfs set property=value { fs | snap | vol } ...
3478 * Sets the given property for all datasets specified on the command line.
3480 typedef struct set_cbdata
{
3486 set_callback(zfs_handle_t
*zhp
, void *data
)
3488 set_cbdata_t
*cbp
= data
;
3490 if (zfs_prop_set(zhp
, cbp
->cb_propname
, cbp
->cb_value
) != 0) {
3491 switch (libzfs_errno(g_zfs
)) {
3492 case EZFS_MOUNTFAILED
:
3493 (void) fprintf(stderr
, gettext("property may be set "
3494 "but unable to remount filesystem\n"));
3496 case EZFS_SHARENFSFAILED
:
3497 (void) fprintf(stderr
, gettext("property may be set "
3498 "but unable to reshare filesystem\n"));
3507 zfs_do_set(int argc
, char **argv
)
3512 /* check for options */
3513 if (argc
> 1 && argv
[1][0] == '-') {
3514 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3519 /* check number of arguments */
3521 (void) fprintf(stderr
, gettext("missing property=value "
3526 (void) fprintf(stderr
, gettext("missing dataset name\n"));
3530 /* validate property=value argument */
3531 cb
.cb_propname
= argv
[1];
3532 if (((cb
.cb_value
= strchr(cb
.cb_propname
, '=')) == NULL
) ||
3533 (cb
.cb_value
[1] == '\0')) {
3534 (void) fprintf(stderr
, gettext("missing value in "
3535 "property=value argument\n"));
3539 *cb
.cb_value
= '\0';
3542 if (*cb
.cb_propname
== '\0') {
3543 (void) fprintf(stderr
,
3544 gettext("missing property in property=value argument\n"));
3548 ret
= zfs_for_each(argc
- 2, argv
+ 2, 0,
3549 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, &cb
);
3554 typedef struct snap_cbdata
{
3556 boolean_t sd_recursive
;
3557 const char *sd_snapname
;
3561 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3563 snap_cbdata_t
*sd
= arg
;
3568 if (sd
->sd_recursive
&&
3569 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
3574 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3577 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3580 if (sd
->sd_recursive
)
3581 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3587 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3589 * Creates a snapshot with the given name. While functionally equivalent to
3590 * 'zfs create', it is a separate command to differentiate intent.
3593 zfs_do_snapshot(int argc
, char **argv
)
3598 snap_cbdata_t sd
= { 0 };
3599 boolean_t multiple_snaps
= B_FALSE
;
3601 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3603 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3607 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3610 if (parseprop(props
))
3614 sd
.sd_recursive
= B_TRUE
;
3615 multiple_snaps
= B_TRUE
;
3618 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3627 /* check number of arguments */
3629 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3634 multiple_snaps
= B_TRUE
;
3635 for (; argc
> 0; argc
--, argv
++) {
3639 atp
= strchr(argv
[0], '@');
3643 sd
.sd_snapname
= atp
+ 1;
3644 zhp
= zfs_open(g_zfs
, argv
[0],
3645 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3648 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3652 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3653 nvlist_free(sd
.sd_nvl
);
3655 if (ret
!= 0 && multiple_snaps
)
3656 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3660 nvlist_free(sd
.sd_nvl
);
3667 * Send a backup stream to stdout.
3670 zfs_do_send(int argc
, char **argv
)
3672 char *fromname
= NULL
;
3673 char *toname
= NULL
;
3676 sendflags_t flags
= { 0 };
3678 nvlist_t
*dbgnv
= NULL
;
3679 boolean_t extraverbose
= B_FALSE
;
3682 while ((c
= getopt(argc
, argv
, ":i:I:RDpvnPe")) != -1) {
3693 flags
.doall
= B_TRUE
;
3696 flags
.replicate
= B_TRUE
;
3699 flags
.props
= B_TRUE
;
3702 flags
.parsable
= B_TRUE
;
3703 flags
.verbose
= B_TRUE
;
3707 extraverbose
= B_TRUE
;
3708 flags
.verbose
= B_TRUE
;
3709 flags
.progress
= B_TRUE
;
3712 flags
.dedup
= B_TRUE
;
3715 flags
.dryrun
= B_TRUE
;
3718 flags
.embed_data
= B_TRUE
;
3721 (void) fprintf(stderr
, gettext("missing argument for "
3722 "'%c' option\n"), optopt
);
3726 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3735 /* check number of arguments */
3737 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3741 (void) fprintf(stderr
, gettext("too many arguments\n"));
3745 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
3746 (void) fprintf(stderr
,
3747 gettext("Error: Stream can not be written to a terminal.\n"
3748 "You must redirect standard output.\n"));
3753 * Special case sending a filesystem, or from a bookmark.
3755 if (strchr(argv
[0], '@') == NULL
||
3756 (fromname
&& strchr(fromname
, '#') != NULL
)) {
3757 char frombuf
[ZFS_MAXNAMELEN
];
3758 enum lzc_send_flags lzc_flags
= 0;
3760 if (flags
.replicate
|| flags
.doall
|| flags
.props
||
3761 flags
.dedup
|| flags
.dryrun
|| flags
.verbose
||
3763 (void) fprintf(stderr
,
3765 "Unsupported flag with filesystem or bookmark.\n"));
3769 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
3773 if (flags
.embed_data
)
3774 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
3776 if (fromname
!= NULL
&&
3777 (fromname
[0] == '#' || fromname
[0] == '@')) {
3779 * Incremental source name begins with # or @.
3780 * Default to same fs as target.
3782 (void) strncpy(frombuf
, argv
[0], sizeof (frombuf
));
3783 cp
= strchr(frombuf
, '@');
3786 (void) strlcat(frombuf
, fromname
, sizeof (frombuf
));
3789 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, lzc_flags
);
3794 cp
= strchr(argv
[0], '@');
3797 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3802 * If they specified the full path to the snapshot, chop off
3803 * everything except the short name of the snapshot, but special
3804 * case if they specify the origin.
3806 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
3807 char origin
[ZFS_MAXNAMELEN
];
3810 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
3811 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
3813 if (strcmp(origin
, fromname
) == 0) {
3815 flags
.fromorigin
= B_TRUE
;
3818 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
3819 (void) fprintf(stderr
,
3820 gettext("incremental source must be "
3821 "in same filesystem\n"));
3825 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
3826 (void) fprintf(stderr
,
3827 gettext("invalid incremental source\n"));
3833 if (flags
.replicate
&& fromname
== NULL
)
3834 flags
.doall
= B_TRUE
;
3836 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
3837 extraverbose
? &dbgnv
: NULL
);
3839 if (extraverbose
&& dbgnv
!= NULL
) {
3841 * dump_nvlist prints to stdout, but that's been
3842 * redirected to a file. Make it print to stderr
3845 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
3846 dump_nvlist(dbgnv
, 0);
3855 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3857 * Restore a backup stream from stdin.
3860 zfs_do_receive(int argc
, char **argv
)
3863 recvflags_t flags
= { 0 };
3866 while ((c
= getopt(argc
, argv
, ":denuvF")) != -1) {
3869 flags
.isprefix
= B_TRUE
;
3872 flags
.isprefix
= B_TRUE
;
3873 flags
.istail
= B_TRUE
;
3876 flags
.dryrun
= B_TRUE
;
3879 flags
.nomount
= B_TRUE
;
3882 flags
.verbose
= B_TRUE
;
3885 flags
.force
= B_TRUE
;
3888 (void) fprintf(stderr
, gettext("missing argument for "
3889 "'%c' option\n"), optopt
);
3893 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3902 /* check number of arguments */
3904 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3908 (void) fprintf(stderr
, gettext("too many arguments\n"));
3912 if (isatty(STDIN_FILENO
)) {
3913 (void) fprintf(stderr
,
3914 gettext("Error: Backup stream can not be read "
3915 "from a terminal.\n"
3916 "You must redirect standard input.\n"));
3920 err
= zfs_receive(g_zfs
, argv
[0], &flags
, STDIN_FILENO
, NULL
);
3926 * allow/unallow stuff
3928 /* copied from zfs/sys/dsl_deleg.h */
3929 #define ZFS_DELEG_PERM_CREATE "create"
3930 #define ZFS_DELEG_PERM_DESTROY "destroy"
3931 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3932 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3933 #define ZFS_DELEG_PERM_CLONE "clone"
3934 #define ZFS_DELEG_PERM_PROMOTE "promote"
3935 #define ZFS_DELEG_PERM_RENAME "rename"
3936 #define ZFS_DELEG_PERM_MOUNT "mount"
3937 #define ZFS_DELEG_PERM_SHARE "share"
3938 #define ZFS_DELEG_PERM_SEND "send"
3939 #define ZFS_DELEG_PERM_RECEIVE "receive"
3940 #define ZFS_DELEG_PERM_ALLOW "allow"
3941 #define ZFS_DELEG_PERM_USERPROP "userprop"
3942 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3943 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3944 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3945 #define ZFS_DELEG_PERM_USERUSED "userused"
3946 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3947 #define ZFS_DELEG_PERM_HOLD "hold"
3948 #define ZFS_DELEG_PERM_RELEASE "release"
3949 #define ZFS_DELEG_PERM_DIFF "diff"
3950 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
3952 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3954 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
3955 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
3956 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
3957 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
3958 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
3959 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
3960 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
3961 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
3962 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
3963 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
3964 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
3965 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
3966 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
3967 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
3968 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
3969 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
3970 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
3972 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
3973 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
3974 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
3975 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
3976 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
3977 { NULL
, ZFS_DELEG_NOTE_NONE
}
3980 /* permission structure */
3981 typedef struct deleg_perm
{
3982 zfs_deleg_who_type_t dp_who_type
;
3983 const char *dp_name
;
3985 boolean_t dp_descend
;
3989 typedef struct deleg_perm_node
{
3990 deleg_perm_t dpn_perm
;
3992 uu_avl_node_t dpn_avl_node
;
3993 } deleg_perm_node_t
;
3995 typedef struct fs_perm fs_perm_t
;
3997 /* permissions set */
3998 typedef struct who_perm
{
3999 zfs_deleg_who_type_t who_type
;
4000 const char *who_name
; /* id */
4001 char who_ug_name
[256]; /* user/group name */
4002 fs_perm_t
*who_fsperm
; /* uplink */
4004 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
4008 typedef struct who_perm_node
{
4009 who_perm_t who_perm
;
4010 uu_avl_node_t who_avl_node
;
4013 typedef struct fs_perm_set fs_perm_set_t
;
4014 /* fs permissions */
4016 const char *fsp_name
;
4018 uu_avl_t
*fsp_sc_avl
; /* sets,create */
4019 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
4021 fs_perm_set_t
*fsp_set
; /* uplink */
4025 typedef struct fs_perm_node
{
4026 fs_perm_t fspn_fsperm
;
4029 uu_list_node_t fspn_list_node
;
4032 /* top level structure */
4033 struct fs_perm_set
{
4034 uu_list_pool_t
*fsps_list_pool
;
4035 uu_list_t
*fsps_list
; /* list of fs_perms */
4037 uu_avl_pool_t
*fsps_named_set_avl_pool
;
4038 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
4039 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
4042 static inline const char *
4043 deleg_perm_type(zfs_deleg_note_t note
)
4049 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4050 case ZFS_DELEG_NOTE_GROUPUSED
:
4051 case ZFS_DELEG_NOTE_USERPROP
:
4052 case ZFS_DELEG_NOTE_USERQUOTA
:
4053 case ZFS_DELEG_NOTE_USERUSED
:
4055 return (gettext("other"));
4057 return (gettext("subcommand"));
4062 who_type2weight(zfs_deleg_who_type_t who_type
)
4066 case ZFS_DELEG_NAMED_SET_SETS
:
4067 case ZFS_DELEG_NAMED_SET
:
4070 case ZFS_DELEG_CREATE_SETS
:
4071 case ZFS_DELEG_CREATE
:
4074 case ZFS_DELEG_USER_SETS
:
4075 case ZFS_DELEG_USER
:
4078 case ZFS_DELEG_GROUP_SETS
:
4079 case ZFS_DELEG_GROUP
:
4082 case ZFS_DELEG_EVERYONE_SETS
:
4083 case ZFS_DELEG_EVERYONE
:
4095 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4097 const who_perm_node_t
*l
= larg
;
4098 const who_perm_node_t
*r
= rarg
;
4099 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
4100 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
4101 int lweight
= who_type2weight(ltype
);
4102 int rweight
= who_type2weight(rtype
);
4103 int res
= lweight
- rweight
;
4105 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
4106 ZFS_MAX_DELEG_NAME
-1);
4118 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4120 const deleg_perm_node_t
*l
= larg
;
4121 const deleg_perm_node_t
*r
= rarg
;
4122 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
4123 ZFS_MAX_DELEG_NAME
-1);
4135 fs_perm_set_init(fs_perm_set_t
*fspset
)
4137 bzero(fspset
, sizeof (fs_perm_set_t
));
4139 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4140 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4141 NULL
, UU_DEFAULT
)) == NULL
)
4143 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4144 UU_DEFAULT
)) == NULL
)
4147 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4148 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4149 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4150 UU_DEFAULT
)) == NULL
)
4153 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4154 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4155 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4156 UU_DEFAULT
)) == NULL
)
4159 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4160 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4161 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4166 static inline void fs_perm_fini(fs_perm_t
*);
4167 static inline void who_perm_fini(who_perm_t
*);
4170 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4172 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4174 while (node
!= NULL
) {
4175 fs_perm_node_t
*next_node
=
4176 uu_list_next(fspset
->fsps_list
, node
);
4177 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4178 fs_perm_fini(fsperm
);
4179 uu_list_remove(fspset
->fsps_list
, node
);
4184 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4185 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4186 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4190 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4193 deleg_perm
->dp_who_type
= type
;
4194 deleg_perm
->dp_name
= name
;
4198 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4199 zfs_deleg_who_type_t type
, const char *name
)
4201 uu_avl_pool_t
*pool
;
4202 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4204 bzero(who_perm
, sizeof (who_perm_t
));
4206 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4207 UU_DEFAULT
)) == NULL
)
4210 who_perm
->who_type
= type
;
4211 who_perm
->who_name
= name
;
4212 who_perm
->who_fsperm
= fsperm
;
4216 who_perm_fini(who_perm_t
*who_perm
)
4218 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4220 while (node
!= NULL
) {
4221 deleg_perm_node_t
*next_node
=
4222 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4224 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4229 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4233 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4235 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4236 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4238 bzero(fsperm
, sizeof (fs_perm_t
));
4240 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4244 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4248 fsperm
->fsp_set
= fspset
;
4249 fsperm
->fsp_name
= fsname
;
4253 fs_perm_fini(fs_perm_t
*fsperm
)
4255 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4256 while (node
!= NULL
) {
4257 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4259 who_perm_t
*who_perm
= &node
->who_perm
;
4260 who_perm_fini(who_perm
);
4261 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4266 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4267 while (node
!= NULL
) {
4268 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4270 who_perm_t
*who_perm
= &node
->who_perm
;
4271 who_perm_fini(who_perm
);
4272 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4277 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4278 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4282 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4283 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4285 uu_avl_index_t idx
= 0;
4287 deleg_perm_node_t
*found_node
= NULL
;
4288 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4290 deleg_perm_init(deleg_perm
, who_type
, name
);
4292 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4294 uu_avl_insert(avl
, node
, idx
);
4297 deleg_perm
= &node
->dpn_perm
;
4302 case ZFS_DELEG_LOCAL
:
4303 deleg_perm
->dp_local
= B_TRUE
;
4305 case ZFS_DELEG_DESCENDENT
:
4306 deleg_perm
->dp_descend
= B_TRUE
;
4311 assert(B_FALSE
); /* invalid locality */
4316 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4318 nvpair_t
*nvp
= NULL
;
4319 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4320 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4321 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4323 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4324 const char *name
= nvpair_name(nvp
);
4325 data_type_t type
= nvpair_type(nvp
);
4326 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4327 deleg_perm_node_t
*node
=
4328 safe_malloc(sizeof (deleg_perm_node_t
));
4330 VERIFY(type
== DATA_TYPE_BOOLEAN
);
4332 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4333 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4340 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4342 nvpair_t
*nvp
= NULL
;
4343 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4345 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4346 nvlist_t
*nvl2
= NULL
;
4347 const char *name
= nvpair_name(nvp
);
4348 uu_avl_t
*avl
= NULL
;
4349 uu_avl_pool_t
*avl_pool
= NULL
;
4350 zfs_deleg_who_type_t perm_type
= name
[0];
4351 char perm_locality
= name
[1];
4352 const char *perm_name
= name
+ 3;
4353 boolean_t is_set
= B_TRUE
;
4354 who_perm_t
*who_perm
= NULL
;
4356 assert('$' == name
[2]);
4358 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4361 switch (perm_type
) {
4362 case ZFS_DELEG_CREATE
:
4363 case ZFS_DELEG_CREATE_SETS
:
4364 case ZFS_DELEG_NAMED_SET
:
4365 case ZFS_DELEG_NAMED_SET_SETS
:
4366 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4367 avl
= fsperm
->fsp_sc_avl
;
4369 case ZFS_DELEG_USER
:
4370 case ZFS_DELEG_USER_SETS
:
4371 case ZFS_DELEG_GROUP
:
4372 case ZFS_DELEG_GROUP_SETS
:
4373 case ZFS_DELEG_EVERYONE
:
4374 case ZFS_DELEG_EVERYONE_SETS
:
4375 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4376 avl
= fsperm
->fsp_uge_avl
;
4383 who_perm_node_t
*found_node
= NULL
;
4384 who_perm_node_t
*node
= safe_malloc(
4385 sizeof (who_perm_node_t
));
4386 who_perm
= &node
->who_perm
;
4387 uu_avl_index_t idx
= 0;
4389 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4390 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4392 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4394 if (avl
== fsperm
->fsp_uge_avl
) {
4396 struct passwd
*p
= NULL
;
4397 struct group
*g
= NULL
;
4398 const char *nice_name
= NULL
;
4400 switch (perm_type
) {
4401 case ZFS_DELEG_USER_SETS
:
4402 case ZFS_DELEG_USER
:
4403 rid
= atoi(perm_name
);
4406 nice_name
= p
->pw_name
;
4408 case ZFS_DELEG_GROUP_SETS
:
4409 case ZFS_DELEG_GROUP
:
4410 rid
= atoi(perm_name
);
4413 nice_name
= g
->gr_name
;
4419 if (nice_name
!= NULL
)
4421 node
->who_perm
.who_ug_name
,
4425 uu_avl_insert(avl
, node
, idx
);
4428 who_perm
= &node
->who_perm
;
4432 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4439 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4441 nvpair_t
*nvp
= NULL
;
4442 uu_avl_index_t idx
= 0;
4444 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4445 nvlist_t
*nvl2
= NULL
;
4446 const char *fsname
= nvpair_name(nvp
);
4447 data_type_t type
= nvpair_type(nvp
);
4448 fs_perm_t
*fsperm
= NULL
;
4449 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4453 fsperm
= &node
->fspn_fsperm
;
4455 VERIFY(DATA_TYPE_NVLIST
== type
);
4457 uu_list_node_init(node
, &node
->fspn_list_node
,
4458 fspset
->fsps_list_pool
);
4460 idx
= uu_list_numnodes(fspset
->fsps_list
);
4461 fs_perm_init(fsperm
, fspset
, fsname
);
4463 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4466 (void) parse_fs_perm(fsperm
, nvl2
);
4468 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4474 static inline const char *
4475 deleg_perm_comment(zfs_deleg_note_t note
)
4477 const char *str
= "";
4482 case ZFS_DELEG_NOTE_ALLOW
:
4483 str
= gettext("Must also have the permission that is being"
4484 "\n\t\t\t\tallowed");
4486 case ZFS_DELEG_NOTE_CLONE
:
4487 str
= gettext("Must also have the 'create' ability and 'mount'"
4488 "\n\t\t\t\tability in the origin file system");
4490 case ZFS_DELEG_NOTE_CREATE
:
4491 str
= gettext("Must also have the 'mount' ability");
4493 case ZFS_DELEG_NOTE_DESTROY
:
4494 str
= gettext("Must also have the 'mount' ability");
4496 case ZFS_DELEG_NOTE_DIFF
:
4497 str
= gettext("Allows lookup of paths within a dataset;"
4498 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4499 "\n\t\t\t\tin order to use zfs diff");
4501 case ZFS_DELEG_NOTE_HOLD
:
4502 str
= gettext("Allows adding a user hold to a snapshot");
4504 case ZFS_DELEG_NOTE_MOUNT
:
4505 str
= gettext("Allows mount/umount of ZFS datasets");
4507 case ZFS_DELEG_NOTE_PROMOTE
:
4508 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4509 " 'promote' ability in the origin file system");
4511 case ZFS_DELEG_NOTE_RECEIVE
:
4512 str
= gettext("Must also have the 'mount' and 'create'"
4515 case ZFS_DELEG_NOTE_RELEASE
:
4516 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4517 "might destroy the snapshot");
4519 case ZFS_DELEG_NOTE_RENAME
:
4520 str
= gettext("Must also have the 'mount' and 'create'"
4521 "\n\t\t\t\tability in the new parent");
4523 case ZFS_DELEG_NOTE_ROLLBACK
:
4526 case ZFS_DELEG_NOTE_SEND
:
4529 case ZFS_DELEG_NOTE_SHARE
:
4530 str
= gettext("Allows sharing file systems over NFS or SMB"
4531 "\n\t\t\t\tprotocols");
4533 case ZFS_DELEG_NOTE_SNAPSHOT
:
4537 * case ZFS_DELEG_NOTE_VSCAN:
4538 * str = gettext("");
4542 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4543 str
= gettext("Allows accessing any groupquota@... property");
4545 case ZFS_DELEG_NOTE_GROUPUSED
:
4546 str
= gettext("Allows reading any groupused@... property");
4548 case ZFS_DELEG_NOTE_USERPROP
:
4549 str
= gettext("Allows changing any user property");
4551 case ZFS_DELEG_NOTE_USERQUOTA
:
4552 str
= gettext("Allows accessing any userquota@... property");
4554 case ZFS_DELEG_NOTE_USERUSED
:
4555 str
= gettext("Allows reading any userused@... property");
4573 boolean_t recursive
; /* unallow only */
4574 boolean_t prt_usage
;
4576 boolean_t prt_perms
;
4579 const char *dataset
;
4583 prop_cmp(const void *a
, const void *b
)
4585 const char *str1
= *(const char **)a
;
4586 const char *str2
= *(const char **)b
;
4587 return (strcmp(str1
, str2
));
4591 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
4593 const char *opt_desc
[] = {
4594 "-h", gettext("show this help message and exit"),
4595 "-l", gettext("set permission locally"),
4596 "-d", gettext("set permission for descents"),
4597 "-u", gettext("set permission for user"),
4598 "-g", gettext("set permission for group"),
4599 "-e", gettext("set permission for everyone"),
4600 "-c", gettext("set create time permission"),
4601 "-s", gettext("define permission set"),
4603 "-r", gettext("remove permissions recursively"),
4605 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
4606 size_t allow_size
= unallow_size
- 2;
4607 const char *props
[ZFS_NUM_PROPS
];
4610 FILE *fp
= requested
? stdout
: stderr
;
4611 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
4612 const char *fmt
= gettext("%-16s %-14s\t%s\n");
4614 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
4616 (void) fprintf(fp
, gettext("Options:\n"));
4617 for (i
= 0; i
< (un
? unallow_size
: allow_size
); i
++) {
4618 const char *opt
= opt_desc
[i
++];
4619 const char *optdsc
= opt_desc
[i
];
4620 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
4623 (void) fprintf(fp
, gettext("\nThe following permissions are "
4625 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
4627 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
4628 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
4629 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
4630 const char *perm_type
= deleg_perm_type(perm_note
);
4631 const char *perm_comment
= deleg_perm_comment(perm_note
);
4632 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
4635 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
4636 zprop_desc_t
*pd
= &pdtbl
[i
];
4637 if (pd
->pd_visible
!= B_TRUE
)
4640 if (pd
->pd_attr
== PROP_READONLY
)
4643 props
[count
++] = pd
->pd_name
;
4645 props
[count
] = NULL
;
4647 qsort(props
, count
, sizeof (char *), prop_cmp
);
4649 for (i
= 0; i
< count
; i
++)
4650 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
4653 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
4655 exit(requested
? 0 : 2);
4658 static inline const char *
4659 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
4662 if (un
&& argc
== expected_argc
- 1)
4664 else if (argc
== expected_argc
)
4665 *permsp
= argv
[argc
- 2];
4667 allow_usage(un
, B_FALSE
,
4668 gettext("wrong number of parameters\n"));
4670 return (argv
[argc
- 1]);
4674 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
4676 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
4677 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
4678 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
4679 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
4682 allow_usage(un
, B_FALSE
,
4683 gettext("-u, -g, and -e are mutually exclusive\n"));
4685 if (opts
->prt_usage
) {
4686 if (argc
== 0 && all_sum
== 0)
4687 allow_usage(un
, B_TRUE
, NULL
);
4694 allow_usage(un
, B_FALSE
,
4695 gettext("invalid options combined with -s\n"));
4697 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4698 if (argv
[0][0] != '@')
4699 allow_usage(un
, B_FALSE
,
4700 gettext("invalid set name: missing '@' prefix\n"));
4701 opts
->who
= argv
[0];
4702 } else if (opts
->create
) {
4703 if (ldcsuge_sum
> 1)
4704 allow_usage(un
, B_FALSE
,
4705 gettext("invalid options combined with -c\n"));
4706 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4707 } else if (opts
->everyone
) {
4709 allow_usage(un
, B_FALSE
,
4710 gettext("invalid options combined with -e\n"));
4711 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4712 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
4714 opts
->everyone
= B_TRUE
;
4717 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4718 } else if (argc
== 1 && !un
) {
4719 opts
->prt_perms
= B_TRUE
;
4720 opts
->dataset
= argv
[argc
-1];
4722 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4723 opts
->who
= argv
[0];
4726 if (!opts
->local
&& !opts
->descend
) {
4727 opts
->local
= B_TRUE
;
4728 opts
->descend
= B_TRUE
;
4733 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
4734 const char *who
, char *perms
, nvlist_t
*top_nvl
)
4737 char ld
[2] = { '\0', '\0' };
4738 char who_buf
[ZFS_MAXNAMELEN
+32];
4739 char base_type
= ZFS_DELEG_WHO_UNKNOWN
;
4740 char set_type
= ZFS_DELEG_WHO_UNKNOWN
;
4741 nvlist_t
*base_nvl
= NULL
;
4742 nvlist_t
*set_nvl
= NULL
;
4745 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4747 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4751 case ZFS_DELEG_NAMED_SET_SETS
:
4752 case ZFS_DELEG_NAMED_SET
:
4753 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
4754 base_type
= ZFS_DELEG_NAMED_SET
;
4755 ld
[0] = ZFS_DELEG_NA
;
4757 case ZFS_DELEG_CREATE_SETS
:
4758 case ZFS_DELEG_CREATE
:
4759 set_type
= ZFS_DELEG_CREATE_SETS
;
4760 base_type
= ZFS_DELEG_CREATE
;
4761 ld
[0] = ZFS_DELEG_NA
;
4763 case ZFS_DELEG_USER_SETS
:
4764 case ZFS_DELEG_USER
:
4765 set_type
= ZFS_DELEG_USER_SETS
;
4766 base_type
= ZFS_DELEG_USER
;
4768 ld
[0] = ZFS_DELEG_LOCAL
;
4770 ld
[1] = ZFS_DELEG_DESCENDENT
;
4772 case ZFS_DELEG_GROUP_SETS
:
4773 case ZFS_DELEG_GROUP
:
4774 set_type
= ZFS_DELEG_GROUP_SETS
;
4775 base_type
= ZFS_DELEG_GROUP
;
4777 ld
[0] = ZFS_DELEG_LOCAL
;
4779 ld
[1] = ZFS_DELEG_DESCENDENT
;
4781 case ZFS_DELEG_EVERYONE_SETS
:
4782 case ZFS_DELEG_EVERYONE
:
4783 set_type
= ZFS_DELEG_EVERYONE_SETS
;
4784 base_type
= ZFS_DELEG_EVERYONE
;
4786 ld
[0] = ZFS_DELEG_LOCAL
;
4788 ld
[1] = ZFS_DELEG_DESCENDENT
;
4793 if (perms
!= NULL
) {
4795 char *end
= curr
+ strlen(perms
);
4797 while (curr
< end
) {
4798 char *delim
= strchr(curr
, ',');
4809 (void) nvlist_add_boolean(nvl
, curr
);
4815 for (i
= 0; i
< 2; i
++) {
4816 char locality
= ld
[i
];
4820 if (!nvlist_empty(base_nvl
)) {
4822 (void) snprintf(who_buf
,
4823 sizeof (who_buf
), "%c%c$%s",
4824 base_type
, locality
, who
);
4826 (void) snprintf(who_buf
,
4827 sizeof (who_buf
), "%c%c$",
4828 base_type
, locality
);
4830 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
4835 if (!nvlist_empty(set_nvl
)) {
4837 (void) snprintf(who_buf
,
4838 sizeof (who_buf
), "%c%c$%s",
4839 set_type
, locality
, who
);
4841 (void) snprintf(who_buf
,
4842 sizeof (who_buf
), "%c%c$",
4843 set_type
, locality
);
4845 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
4850 for (i
= 0; i
< 2; i
++) {
4851 char locality
= ld
[i
];
4856 (void) snprintf(who_buf
, sizeof (who_buf
),
4857 "%c%c$%s", base_type
, locality
, who
);
4859 (void) snprintf(who_buf
, sizeof (who_buf
),
4860 "%c%c$", base_type
, locality
);
4861 (void) nvlist_add_boolean(top_nvl
, who_buf
);
4864 (void) snprintf(who_buf
, sizeof (who_buf
),
4865 "%c%c$%s", set_type
, locality
, who
);
4867 (void) snprintf(who_buf
, sizeof (who_buf
),
4868 "%c%c$", set_type
, locality
);
4869 (void) nvlist_add_boolean(top_nvl
, who_buf
);
4875 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
4877 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
4881 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
4882 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
4883 } else if (opts
->create
) {
4884 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
4885 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
4886 } else if (opts
->everyone
) {
4887 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
4888 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
4890 char *curr
= opts
->who
;
4891 char *end
= curr
+ strlen(curr
);
4893 while (curr
< end
) {
4895 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
4897 char *delim
= strchr(curr
, ',');
4900 struct passwd
*p
= NULL
;
4901 struct group
*g
= NULL
;
4909 rid
= (uid_t
)strtol(curr
, &endch
, 0);
4911 who_type
= ZFS_DELEG_USER
;
4920 (void) snprintf(errbuf
, 256, gettext(
4921 "invalid user %s"), curr
);
4922 allow_usage(un
, B_TRUE
, errbuf
);
4924 } else if (opts
->group
) {
4925 who_type
= ZFS_DELEG_GROUP
;
4934 (void) snprintf(errbuf
, 256, gettext(
4935 "invalid group %s"), curr
);
4936 allow_usage(un
, B_TRUE
, errbuf
);
4939 if (*endch
!= '\0') {
4946 if (*endch
!= '\0') {
4954 who_type
= ZFS_DELEG_USER
;
4956 } else if (g
!= NULL
) {
4957 who_type
= ZFS_DELEG_GROUP
;
4960 (void) snprintf(errbuf
, 256, gettext(
4961 "invalid user/group %s"), curr
);
4962 allow_usage(un
, B_TRUE
, errbuf
);
4966 (void) sprintf(id
, "%u", rid
);
4969 store_allow_perm(who_type
, opts
->local
,
4970 opts
->descend
, who
, opts
->perms
, *nvlp
);
4979 print_set_creat_perms(uu_avl_t
*who_avl
)
4981 const char *sc_title
[] = {
4982 gettext("Permission sets:\n"),
4983 gettext("Create time permissions:\n"),
4986 const char **title_ptr
= sc_title
;
4987 who_perm_node_t
*who_node
= NULL
;
4988 int prev_weight
= -1;
4990 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
4991 who_node
= uu_avl_next(who_avl
, who_node
)) {
4992 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
4993 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
4994 const char *who_name
= who_node
->who_perm
.who_name
;
4995 int weight
= who_type2weight(who_type
);
4996 boolean_t first
= B_TRUE
;
4997 deleg_perm_node_t
*deleg_node
;
4999 if (prev_weight
!= weight
) {
5000 (void) printf("%s", *title_ptr
++);
5001 prev_weight
= weight
;
5004 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
5005 (void) printf("\t");
5007 (void) printf("\t%s ", who_name
);
5009 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
5010 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5013 deleg_node
->dpn_perm
.dp_name
);
5016 (void) printf(",%s",
5017 deleg_node
->dpn_perm
.dp_name
);
5020 (void) printf("\n");
5025 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
5028 who_perm_node_t
*who_node
= NULL
;
5029 boolean_t prt_title
= B_TRUE
;
5030 uu_avl_walk_t
*walk
;
5032 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
5035 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
5036 const char *who_name
= who_node
->who_perm
.who_name
;
5037 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
5038 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5039 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5041 deleg_perm_node_t
*deleg_node
;
5042 boolean_t prt_who
= B_TRUE
;
5044 for (deleg_node
= uu_avl_first(avl
);
5046 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5047 if (local
!= deleg_node
->dpn_perm
.dp_local
||
5048 descend
!= deleg_node
->dpn_perm
.dp_descend
)
5052 const char *who
= NULL
;
5054 prt_title
= B_FALSE
;
5055 (void) printf("%s", title
);
5059 case ZFS_DELEG_USER_SETS
:
5060 case ZFS_DELEG_USER
:
5061 who
= gettext("user");
5063 who_name
= nice_who_name
;
5065 case ZFS_DELEG_GROUP_SETS
:
5066 case ZFS_DELEG_GROUP
:
5067 who
= gettext("group");
5069 who_name
= nice_who_name
;
5071 case ZFS_DELEG_EVERYONE_SETS
:
5072 case ZFS_DELEG_EVERYONE
:
5073 who
= gettext("everyone");
5080 if (who_name
== NULL
)
5081 (void) printf("\t%s", who
);
5083 (void) printf("\t%s %s", who
, who_name
);
5086 (void) printf("%c%s", delim
,
5087 deleg_node
->dpn_perm
.dp_name
);
5092 (void) printf("\n");
5095 uu_avl_walk_end(walk
);
5099 print_fs_perms(fs_perm_set_t
*fspset
)
5101 fs_perm_node_t
*node
= NULL
;
5102 char buf
[ZFS_MAXNAMELEN
+32];
5103 const char *dsname
= buf
;
5105 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
5106 node
= uu_list_next(fspset
->fsps_list
, node
)) {
5107 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
5108 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
5111 (void) snprintf(buf
, ZFS_MAXNAMELEN
+32,
5112 gettext("---- Permissions on %s "),
5113 node
->fspn_fsperm
.fsp_name
);
5114 (void) printf("%s", dsname
);
5115 left
= 70 - strlen(buf
);
5118 (void) printf("\n");
5120 print_set_creat_perms(sc_avl
);
5121 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
5122 gettext("Local permissions:\n"));
5123 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
5124 gettext("Descendent permissions:\n"));
5125 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
5126 gettext("Local+Descendent permissions:\n"));
5130 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5132 struct deleg_perms
{
5138 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5140 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5141 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5143 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5146 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5150 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5153 nvlist_t
*perm_nvl
= NULL
;
5154 nvlist_t
*update_perm_nvl
= NULL
;
5157 struct allow_opts opts
= { 0 };
5159 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5162 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5165 opts
.local
= B_TRUE
;
5168 opts
.descend
= B_TRUE
;
5174 opts
.group
= B_TRUE
;
5177 opts
.everyone
= B_TRUE
;
5183 opts
.create
= B_TRUE
;
5186 opts
.recursive
= B_TRUE
;
5189 (void) fprintf(stderr
, gettext("missing argument for "
5190 "'%c' option\n"), optopt
);
5194 opts
.prt_usage
= B_TRUE
;
5197 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5206 /* check arguments */
5207 parse_allow_args(argc
, argv
, un
, &opts
);
5209 /* try to open the dataset */
5210 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5211 ZFS_TYPE_VOLUME
)) == NULL
) {
5212 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5217 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5220 fs_perm_set_init(&fs_perm_set
);
5221 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5222 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5227 print_fs_perms(&fs_perm_set
);
5229 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5230 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5233 if (un
&& opts
.recursive
) {
5234 struct deleg_perms data
= { un
, update_perm_nvl
};
5235 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5244 nvlist_free(perm_nvl
);
5245 if (update_perm_nvl
!= NULL
)
5246 nvlist_free(update_perm_nvl
);
5248 fs_perm_set_fini(&fs_perm_set
);
5256 zfs_do_allow(int argc
, char **argv
)
5258 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5262 zfs_do_unallow(int argc
, char **argv
)
5264 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5268 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5273 boolean_t recursive
= B_FALSE
;
5274 const char *opts
= holding
? "rt" : "r";
5278 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5284 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5293 /* check number of arguments */
5301 if (holding
&& tag
[0] == '.') {
5302 /* tags starting with '.' are reserved for libzfs */
5303 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5307 for (i
= 0; i
< argc
; ++i
) {
5309 char parent
[ZFS_MAXNAMELEN
];
5311 char *path
= argv
[i
];
5313 delim
= strchr(path
, '@');
5314 if (delim
== NULL
) {
5315 (void) fprintf(stderr
,
5316 gettext("'%s' is not a snapshot\n"), path
);
5320 (void) strncpy(parent
, path
, delim
- path
);
5321 parent
[delim
- path
] = '\0';
5323 zhp
= zfs_open(g_zfs
, parent
,
5324 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5330 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
5333 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5339 return (errors
!= 0);
5343 * zfs hold [-r] [-t] <tag> <snap> ...
5345 * -r Recursively hold
5347 * Apply a user-hold with the given tag to the list of snapshots.
5350 zfs_do_hold(int argc
, char **argv
)
5352 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5356 * zfs release [-r] <tag> <snap> ...
5358 * -r Recursively release
5360 * Release a user-hold with the given tag from the list of snapshots.
5363 zfs_do_release(int argc
, char **argv
)
5365 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5368 typedef struct holds_cbdata
{
5369 boolean_t cb_recursive
;
5370 const char *cb_snapname
;
5372 size_t cb_max_namelen
;
5373 size_t cb_max_taglen
;
5376 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5377 #define DATETIME_BUF_LEN (32)
5382 print_holds(boolean_t scripted
, int nwidth
, int tagwidth
, nvlist_t
*nvl
)
5385 nvpair_t
*nvp
= NULL
;
5386 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5390 for (i
= 0; i
< 3; i
++) {
5391 col
= gettext(hdr_cols
[i
]);
5393 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5396 (void) printf("%s\n", col
);
5400 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5401 char *zname
= nvpair_name(nvp
);
5403 nvpair_t
*nvp2
= NULL
;
5404 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5405 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5406 char tsbuf
[DATETIME_BUF_LEN
];
5407 char *tagname
= nvpair_name(nvp2
);
5411 char sep
= scripted
? '\t' : ' ';
5412 int sepnum
= scripted
? 1 : 2;
5414 (void) nvpair_value_uint64(nvp2
, &val
);
5416 (void) localtime_r(&time
, &t
);
5417 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5418 gettext(STRFTIME_FMT_STR
), &t
);
5420 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth
, zname
,
5421 sepnum
, sep
, tagwidth
, tagname
, sepnum
, sep
, tsbuf
);
5427 * Generic callback function to list a dataset or snapshot.
5430 holds_callback(zfs_handle_t
*zhp
, void *data
)
5432 holds_cbdata_t
*cbp
= data
;
5433 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5434 nvlist_t
*nvl
= NULL
;
5435 nvpair_t
*nvp
= NULL
;
5436 const char *zname
= zfs_get_name(zhp
);
5437 size_t znamelen
= strnlen(zname
, ZFS_MAXNAMELEN
);
5439 if (cbp
->cb_recursive
) {
5440 const char *snapname
;
5441 char *delim
= strchr(zname
, '@');
5445 snapname
= delim
+ 1;
5446 if (strcmp(cbp
->cb_snapname
, snapname
))
5450 if (zfs_get_holds(zhp
, &nvl
) != 0)
5453 if (znamelen
> cbp
->cb_max_namelen
)
5454 cbp
->cb_max_namelen
= znamelen
;
5456 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5457 const char *tag
= nvpair_name(nvp
);
5458 size_t taglen
= strnlen(tag
, MAXNAMELEN
);
5459 if (taglen
> cbp
->cb_max_taglen
)
5460 cbp
->cb_max_taglen
= taglen
;
5463 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5467 * zfs holds [-r] <snap> ...
5469 * -r Recursively hold
5472 zfs_do_holds(int argc
, char **argv
)
5477 boolean_t scripted
= B_FALSE
;
5478 boolean_t recursive
= B_FALSE
;
5479 const char *opts
= "rH";
5482 int types
= ZFS_TYPE_SNAPSHOT
;
5483 holds_cbdata_t cb
= { 0 };
5490 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5499 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5506 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
5507 flags
|= ZFS_ITER_RECURSE
;
5513 /* check number of arguments */
5517 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5520 for (i
= 0; i
< argc
; ++i
) {
5521 char *snapshot
= argv
[i
];
5523 const char *snapname
;
5525 delim
= strchr(snapshot
, '@');
5526 if (delim
== NULL
) {
5527 (void) fprintf(stderr
,
5528 gettext("'%s' is not a snapshot\n"), snapshot
);
5532 snapname
= delim
+ 1;
5534 snapshot
[delim
- snapshot
] = '\0';
5536 cb
.cb_recursive
= recursive
;
5537 cb
.cb_snapname
= snapname
;
5541 * 1. collect holds data, set format options
5543 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
5544 holds_callback
, &cb
);
5550 * 2. print holds data
5552 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
5554 if (nvlist_empty(nvl
))
5555 (void) fprintf(stderr
, gettext("no datasets available\n"));
5559 return (0 != errors
);
5562 #define CHECK_SPINNER 30
5563 #define SPINNER_TIME 3 /* seconds */
5564 #define MOUNT_TIME 5 /* seconds */
5567 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
5569 static char *spin
[] = { "-", "\\", "|", "/" };
5570 static int spinval
= 0;
5571 static int spincheck
= 0;
5572 static time_t last_spin_time
= (time_t)0;
5573 get_all_cb_t
*cbp
= data
;
5574 zfs_type_t type
= zfs_get_type(zhp
);
5576 if (cbp
->cb_verbose
) {
5577 if (--spincheck
< 0) {
5578 time_t now
= time(NULL
);
5579 if (last_spin_time
+ SPINNER_TIME
< now
) {
5580 update_progress(spin
[spinval
++ % 4]);
5581 last_spin_time
= now
;
5583 spincheck
= CHECK_SPINNER
;
5588 * Iterate over any nested datasets.
5590 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
5596 * Skip any datasets whose type does not match.
5598 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
5602 libzfs_add_handle(cbp
, zhp
);
5603 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
5609 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
5611 get_all_cb_t cb
= { 0 };
5612 cb
.cb_verbose
= verbose
;
5613 cb
.cb_getone
= get_one_dataset
;
5616 set_progress_header(gettext("Reading ZFS config"));
5617 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
5619 *dslist
= cb
.cb_handles
;
5620 *count
= cb
.cb_used
;
5623 finish_progress(gettext("done."));
5627 * Generic callback for sharing or mounting filesystems. Because the code is so
5628 * similar, we have a common function with an extra parameter to determine which
5629 * mode we are using.
5631 #define OP_SHARE 0x1
5632 #define OP_MOUNT 0x2
5635 * Share or mount a dataset.
5638 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
5639 boolean_t
explicit, const char *options
)
5641 char mountpoint
[ZFS_MAXPROPLEN
];
5642 char shareopts
[ZFS_MAXPROPLEN
];
5643 char smbshareopts
[ZFS_MAXPROPLEN
];
5644 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
5646 uint64_t zoned
, canmount
;
5647 boolean_t shared_nfs
, shared_smb
;
5649 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
5652 * Check to make sure we can mount/share this dataset. If we
5653 * are in the global zone and the filesystem is exported to a
5654 * local zone, or if we are in a local zone and the
5655 * filesystem is not exported, then it is an error.
5657 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
5659 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
5663 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5664 "dataset is exported to a local zone\n"), cmdname
,
5668 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
5672 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5673 "permission denied\n"), cmdname
,
5679 * Ignore any filesystems which don't apply to us. This
5680 * includes those with a legacy mountpoint, or those with
5681 * legacy share options.
5683 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
5684 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
5685 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
5686 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5687 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
5688 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5690 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
5691 strcmp(smbshareopts
, "off") == 0) {
5695 (void) fprintf(stderr
, gettext("cannot share '%s': "
5696 "legacy share\n"), zfs_get_name(zhp
));
5697 (void) fprintf(stderr
, gettext("use share(1M) to "
5698 "share this filesystem, or set "
5699 "sharenfs property on\n"));
5704 * We cannot share or mount legacy filesystems. If the
5705 * shareopts is non-legacy but the mountpoint is legacy, we
5706 * treat it as a legacy share.
5708 if (strcmp(mountpoint
, "legacy") == 0) {
5712 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5713 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
5714 (void) fprintf(stderr
, gettext("use %s(1M) to "
5715 "%s this filesystem\n"), cmdname
, cmdname
);
5719 if (strcmp(mountpoint
, "none") == 0) {
5723 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
5724 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
5729 * canmount explicit outcome
5730 * on no pass through
5731 * on yes pass through
5733 * off yes display error, return 1
5734 * noauto no return 0
5735 * noauto yes pass through
5737 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
5738 if (canmount
== ZFS_CANMOUNT_OFF
) {
5742 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5743 "'canmount' property is set to 'off'\n"), cmdname
,
5746 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
5751 * At this point, we have verified that the mountpoint and/or
5752 * shareopts are appropriate for auto management. If the
5753 * filesystem is already mounted or shared, return (failing
5754 * for explicit requests); otherwise mount or share the
5760 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
5761 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
5763 if ((shared_nfs
&& shared_smb
) ||
5764 ((shared_nfs
&& strcmp(shareopts
, "on") == 0) &&
5765 (strcmp(smbshareopts
, "off") == 0)) ||
5766 ((shared_smb
&& strcmp(smbshareopts
, "on") == 0) &&
5767 (strcmp(shareopts
, "off") == 0))) {
5771 (void) fprintf(stderr
, gettext("cannot share "
5772 "'%s': filesystem already shared\n"),
5777 if (!zfs_is_mounted(zhp
, NULL
) &&
5778 zfs_mount(zhp
, NULL
, 0) != 0)
5781 if (protocol
== NULL
) {
5782 if (zfs_shareall(zhp
) != 0)
5784 } else if (strcmp(protocol
, "nfs") == 0) {
5785 if (zfs_share_nfs(zhp
))
5787 } else if (strcmp(protocol
, "smb") == 0) {
5788 if (zfs_share_smb(zhp
))
5791 (void) fprintf(stderr
, gettext("cannot share "
5792 "'%s': invalid share type '%s' "
5794 zfs_get_name(zhp
), protocol
);
5801 if (options
== NULL
)
5802 mnt
.mnt_mntopts
= "";
5804 mnt
.mnt_mntopts
= (char *)options
;
5806 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
5807 zfs_is_mounted(zhp
, NULL
)) {
5811 (void) fprintf(stderr
, gettext("cannot mount "
5812 "'%s': filesystem already mounted\n"),
5817 if (zfs_mount(zhp
, options
, flags
) != 0)
5826 * Reports progress in the form "(current/total)". Not thread-safe.
5829 report_mount_progress(int current
, int total
)
5831 static time_t last_progress_time
= 0;
5832 time_t now
= time(NULL
);
5835 /* report 1..n instead of 0..n-1 */
5838 /* display header if we're here for the first time */
5840 set_progress_header(gettext("Mounting ZFS filesystems"));
5841 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
5842 /* too soon to report again */
5846 last_progress_time
= now
;
5848 (void) sprintf(info
, "(%d/%d)", current
, total
);
5850 if (current
== total
)
5851 finish_progress(info
);
5853 update_progress(info
);
5857 append_options(char *mntopts
, char *newopts
)
5859 int len
= strlen(mntopts
);
5861 /* original length plus new string to append plus 1 for the comma */
5862 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
5863 (void) fprintf(stderr
, gettext("the opts argument for "
5864 "'%s' option is too long (more than %d chars)\n"),
5865 "-o", MNT_LINE_MAX
);
5870 mntopts
[len
++] = ',';
5872 (void) strcpy(&mntopts
[len
], newopts
);
5876 share_mount(int op
, int argc
, char **argv
)
5879 boolean_t verbose
= B_FALSE
;
5881 char *options
= NULL
;
5885 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
5895 if (*optarg
== '\0') {
5896 (void) fprintf(stderr
, gettext("empty mount "
5897 "options (-o) specified\n"));
5901 if (options
== NULL
)
5902 options
= safe_malloc(MNT_LINE_MAX
+ 1);
5904 /* option validation is done later */
5905 append_options(options
, optarg
);
5908 flags
|= MS_OVERLAY
;
5911 (void) fprintf(stderr
, gettext("missing argument for "
5912 "'%c' option\n"), optopt
);
5916 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5925 /* check number of arguments */
5927 zfs_handle_t
**dslist
= NULL
;
5928 size_t i
, count
= 0;
5929 char *protocol
= NULL
;
5931 if (op
== OP_SHARE
&& argc
> 0) {
5932 if (strcmp(argv
[0], "nfs") != 0 &&
5933 strcmp(argv
[0], "smb") != 0) {
5934 (void) fprintf(stderr
, gettext("share type "
5935 "must be 'nfs' or 'smb'\n"));
5944 (void) fprintf(stderr
, gettext("too many arguments\n"));
5948 start_progress_timer();
5949 get_all_datasets(&dslist
, &count
, verbose
);
5954 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
5956 for (i
= 0; i
< count
; i
++) {
5958 report_mount_progress(i
, count
);
5960 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
5961 B_FALSE
, options
) != 0)
5963 zfs_close(dslist
[i
]);
5967 } else if (argc
== 0) {
5968 struct mnttab entry
;
5970 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
5971 (void) fprintf(stderr
, gettext("missing filesystem "
5972 "argument (specify -a for all)\n"));
5977 * When mount is given no arguments, go through /etc/mtab and
5978 * display any active ZFS mounts. We hide any snapshots, since
5979 * they are controlled automatically.
5982 /* Reopen MNTTAB to prevent reading stale data from open file */
5983 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
5986 while (getmntent(mnttab_file
, &entry
) == 0) {
5987 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
5988 strchr(entry
.mnt_special
, '@') != NULL
)
5991 (void) printf("%-30s %s\n", entry
.mnt_special
,
5999 (void) fprintf(stderr
,
6000 gettext("too many arguments\n"));
6004 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6005 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6008 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
6018 * zfs mount -a [nfs]
6019 * zfs mount filesystem
6021 * Mount all filesystems, or mount the given filesystem.
6024 zfs_do_mount(int argc
, char **argv
)
6026 return (share_mount(OP_MOUNT
, argc
, argv
));
6030 * zfs share -a [nfs | smb]
6031 * zfs share filesystem
6033 * Share all filesystems, or share the given filesystem.
6036 zfs_do_share(int argc
, char **argv
)
6038 return (share_mount(OP_SHARE
, argc
, argv
));
6041 typedef struct unshare_unmount_node
{
6042 zfs_handle_t
*un_zhp
;
6044 uu_avl_node_t un_avlnode
;
6045 } unshare_unmount_node_t
;
6049 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
6051 const unshare_unmount_node_t
*l
= larg
;
6052 const unshare_unmount_node_t
*r
= rarg
;
6054 return (strcmp(l
->un_mountp
, r
->un_mountp
));
6058 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6059 * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
6060 * and unmount it appropriately.
6063 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
6067 struct stat64 statbuf
;
6068 struct extmnttab entry
;
6069 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
6073 * Search for the path in /etc/mtab. Rather than looking for the
6074 * specific path, which can be fooled by non-standard paths (i.e. ".."
6075 * or "//"), we stat() the path and search for the corresponding
6076 * (major,minor) device pair.
6078 if (stat64(path
, &statbuf
) != 0) {
6079 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6080 cmdname
, path
, strerror(errno
));
6083 path_inode
= statbuf
.st_ino
;
6086 * Search for the given (major,minor) pair in the mount table.
6089 /* Reopen MNTTAB to prevent reading stale data from open file */
6090 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6093 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
6094 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
6095 entry
.mnt_minor
== minor(statbuf
.st_dev
))
6099 if (op
== OP_SHARE
) {
6100 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
6101 "currently mounted\n"), cmdname
, path
);
6104 (void) fprintf(stderr
, gettext("warning: %s not in mtab\n"),
6106 if ((ret
= umount2(path
, flags
)) != 0)
6107 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
6112 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
6113 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
6114 "filesystem\n"), cmdname
, path
);
6118 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6119 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6123 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
6124 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6125 cmdname
, path
, strerror(errno
));
6127 } else if (statbuf
.st_ino
!= path_inode
) {
6128 (void) fprintf(stderr
, gettext("cannot "
6129 "%s '%s': not a mountpoint\n"), cmdname
, path
);
6133 if (op
== OP_SHARE
) {
6134 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6135 char smbshare_prop
[ZFS_MAXPROPLEN
];
6137 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
6138 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6139 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
6140 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6142 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6143 strcmp(smbshare_prop
, "off") == 0) {
6144 (void) fprintf(stderr
, gettext("cannot unshare "
6145 "'%s': legacy share\n"), path
);
6146 (void) fprintf(stderr
, gettext("use exportfs(8) "
6147 "or smbcontrol(1) to unshare this filesystem\n"));
6148 } else if (!zfs_is_shared(zhp
)) {
6149 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6150 "not currently shared\n"), path
);
6152 ret
= zfs_unshareall_bypath(zhp
, path
);
6155 char mtpt_prop
[ZFS_MAXPROPLEN
];
6157 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6158 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6161 ret
= zfs_unmount(zhp
, NULL
, flags
);
6162 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6163 (void) fprintf(stderr
, gettext("cannot unmount "
6164 "'%s': legacy mountpoint\n"),
6166 (void) fprintf(stderr
, gettext("use umount(8) "
6167 "to unmount this filesystem\n"));
6169 ret
= zfs_unmountall(zhp
, flags
);
6180 * Generic callback for unsharing or unmounting a filesystem.
6183 unshare_unmount(int op
, int argc
, char **argv
)
6190 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6191 char sharesmb
[ZFS_MAXPROPLEN
];
6194 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
6203 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6214 * We could make use of zfs_for_each() to walk all datasets in
6215 * the system, but this would be very inefficient, especially
6216 * since we would have to linearly search /etc/mtab for each
6217 * one. Instead, do one pass through /etc/mtab looking for
6218 * zfs entries and call zfs_unmount() for each one.
6220 * Things get a little tricky if the administrator has created
6221 * mountpoints beneath other ZFS filesystems. In this case, we
6222 * have to unmount the deepest filesystems first. To accomplish
6223 * this, we place all the mountpoints in an AVL tree sorted by
6224 * the special type (dataset name), and walk the result in
6225 * reverse to make sure to get any snapshots first.
6227 struct mnttab entry
;
6228 uu_avl_pool_t
*pool
;
6229 uu_avl_t
*tree
= NULL
;
6230 unshare_unmount_node_t
*node
;
6232 uu_avl_walk_t
*walk
;
6235 (void) fprintf(stderr
, gettext("too many arguments\n"));
6239 if (((pool
= uu_avl_pool_create("unmount_pool",
6240 sizeof (unshare_unmount_node_t
),
6241 offsetof(unshare_unmount_node_t
, un_avlnode
),
6242 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6243 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6246 /* Reopen MNTTAB to prevent reading stale data from open file */
6247 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6250 while (getmntent(mnttab_file
, &entry
) == 0) {
6252 /* ignore non-ZFS entries */
6253 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6256 /* ignore snapshots */
6257 if (strchr(entry
.mnt_special
, '@') != NULL
)
6260 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6261 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6268 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6270 sizeof (nfs_mnt_prop
),
6271 NULL
, NULL
, 0, B_FALSE
) == 0);
6272 if (strcmp(nfs_mnt_prop
, "off") != 0)
6274 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6276 sizeof (nfs_mnt_prop
),
6277 NULL
, NULL
, 0, B_FALSE
) == 0);
6278 if (strcmp(nfs_mnt_prop
, "off") == 0)
6282 /* Ignore legacy mounts */
6283 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6285 sizeof (nfs_mnt_prop
),
6286 NULL
, NULL
, 0, B_FALSE
) == 0);
6287 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6289 /* Ignore canmount=noauto mounts */
6290 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6291 ZFS_CANMOUNT_NOAUTO
)
6297 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6299 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6301 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6303 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6304 uu_avl_insert(tree
, node
, idx
);
6306 zfs_close(node
->un_zhp
);
6307 free(node
->un_mountp
);
6313 * Walk the AVL tree in reverse, unmounting each filesystem and
6314 * removing it from the AVL tree in the process.
6316 if ((walk
= uu_avl_walk_start(tree
,
6317 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6320 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6321 uu_avl_remove(tree
, node
);
6325 if (zfs_unshareall_bypath(node
->un_zhp
,
6326 node
->un_mountp
) != 0)
6331 if (zfs_unmount(node
->un_zhp
,
6332 node
->un_mountp
, flags
) != 0)
6337 zfs_close(node
->un_zhp
);
6338 free(node
->un_mountp
);
6342 uu_avl_walk_end(walk
);
6343 uu_avl_destroy(tree
);
6344 uu_avl_pool_destroy(pool
);
6349 (void) fprintf(stderr
,
6350 gettext("missing filesystem argument\n"));
6352 (void) fprintf(stderr
,
6353 gettext("too many arguments\n"));
6358 * We have an argument, but it may be a full path or a ZFS
6359 * filesystem. Pass full paths off to unmount_path() (shared by
6360 * manual_unmount), otherwise open the filesystem and pass to
6363 if (argv
[0][0] == '/')
6364 return (unshare_unmount_path(op
, argv
[0],
6367 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6368 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6371 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
6372 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
6373 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
6374 NULL
, 0, B_FALSE
) == 0);
6378 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6380 sizeof (nfs_mnt_prop
),
6381 NULL
, NULL
, 0, B_FALSE
) == 0);
6382 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6383 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
6386 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6387 strcmp(sharesmb
, "off") == 0) {
6388 (void) fprintf(stderr
, gettext("cannot "
6389 "unshare '%s': legacy share\n"),
6391 (void) fprintf(stderr
, gettext("use "
6392 "unshare(1M) to unshare this "
6395 } else if (!zfs_is_shared(zhp
)) {
6396 (void) fprintf(stderr
, gettext("cannot "
6397 "unshare '%s': not currently "
6398 "shared\n"), zfs_get_name(zhp
));
6400 } else if (zfs_unshareall(zhp
) != 0) {
6406 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
6407 (void) fprintf(stderr
, gettext("cannot "
6408 "unmount '%s': legacy "
6409 "mountpoint\n"), zfs_get_name(zhp
));
6410 (void) fprintf(stderr
, gettext("use "
6411 "umount(1M) to unmount this "
6414 } else if (!zfs_is_mounted(zhp
, NULL
)) {
6415 (void) fprintf(stderr
, gettext("cannot "
6416 "unmount '%s': not currently "
6420 } else if (zfs_unmountall(zhp
, flags
) != 0) {
6434 * zfs unmount filesystem
6436 * Unmount all filesystems, or a specific ZFS filesystem.
6439 zfs_do_unmount(int argc
, char **argv
)
6441 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
6446 * zfs unshare filesystem
6448 * Unshare all filesystems, or a specific ZFS filesystem.
6451 zfs_do_unshare(int argc
, char **argv
)
6453 return (unshare_unmount(OP_SHARE
, argc
, argv
));
6457 find_command_idx(char *command
, int *idx
)
6461 for (i
= 0; i
< NCOMMAND
; i
++) {
6462 if (command_table
[i
].name
== NULL
)
6465 if (strcmp(command
, command_table
[i
].name
) == 0) {
6474 zfs_do_diff(int argc
, char **argv
)
6478 char *tosnap
= NULL
;
6479 char *fromsnap
= NULL
;
6484 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
6487 flags
|= ZFS_DIFF_CLASSIFY
;
6490 flags
|= ZFS_DIFF_PARSEABLE
;
6493 flags
|= ZFS_DIFF_TIMESTAMP
;
6496 (void) fprintf(stderr
,
6497 gettext("invalid option '%c'\n"), optopt
);
6506 (void) fprintf(stderr
,
6507 gettext("must provide at least one snapshot name\n"));
6512 (void) fprintf(stderr
, gettext("too many arguments\n"));
6517 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
6520 if (*fromsnap
!= '@')
6521 copy
= strdup(fromsnap
);
6523 copy
= strdup(tosnap
);
6527 if ((atp
= strchr(copy
, '@')))
6530 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6536 * Ignore SIGPIPE so that the library can give us
6537 * information on any failure
6539 (void) sigignore(SIGPIPE
);
6541 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
6549 * zfs bookmark <fs@snap> <fs#bmark>
6551 * Creates a bookmark with the given name from the given snapshot.
6554 zfs_do_bookmark(int argc
, char **argv
)
6556 char snapname
[ZFS_MAXNAMELEN
];
6563 while ((c
= getopt(argc
, argv
, "")) != -1) {
6566 (void) fprintf(stderr
,
6567 gettext("invalid option '%c'\n"), optopt
);
6575 /* check number of arguments */
6577 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
6581 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
6585 if (strchr(argv
[1], '#') == NULL
) {
6586 (void) fprintf(stderr
,
6587 gettext("invalid bookmark name '%s' -- "
6588 "must contain a '#'\n"), argv
[1]);
6592 if (argv
[0][0] == '@') {
6594 * Snapshot name begins with @.
6595 * Default to same fs as bookmark.
6597 (void) strncpy(snapname
, argv
[1], sizeof (snapname
));
6598 *strchr(snapname
, '#') = '\0';
6599 (void) strlcat(snapname
, argv
[0], sizeof (snapname
));
6601 (void) strncpy(snapname
, argv
[0], sizeof (snapname
));
6603 zhp
= zfs_open(g_zfs
, snapname
, ZFS_TYPE_SNAPSHOT
);
6609 nvl
= fnvlist_alloc();
6610 fnvlist_add_string(nvl
, argv
[1], snapname
);
6611 ret
= lzc_bookmark(nvl
, NULL
);
6615 const char *err_msg
;
6618 (void) snprintf(errbuf
, sizeof (errbuf
),
6619 dgettext(TEXT_DOMAIN
,
6620 "cannot create bookmark '%s'"), argv
[1]);
6624 err_msg
= "bookmark is in a different pool";
6627 err_msg
= "bookmark exists";
6630 err_msg
= "invalid argument";
6633 err_msg
= "bookmark feature not enabled";
6636 err_msg
= "unknown error";
6639 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
6640 dgettext(TEXT_DOMAIN
, err_msg
));
6651 main(int argc
, char **argv
)
6657 (void) setlocale(LC_ALL
, "");
6658 (void) textdomain(TEXT_DOMAIN
);
6663 * Make sure the user has specified some command.
6666 (void) fprintf(stderr
, gettext("missing command\n"));
6673 * The 'umount' command is an alias for 'unmount'
6675 if (strcmp(cmdname
, "umount") == 0)
6676 cmdname
= "unmount";
6679 * The 'recv' command is an alias for 'receive'
6681 if (strcmp(cmdname
, "recv") == 0)
6682 cmdname
= "receive";
6685 * The 'snap' command is an alias for 'snapshot'
6687 if (strcmp(cmdname
, "snap") == 0)
6688 cmdname
= "snapshot";
6693 if ((strcmp(cmdname
, "-?") == 0) ||
6694 (strcmp(cmdname
, "--help") == 0))
6697 if ((g_zfs
= libzfs_init()) == NULL
)
6700 mnttab_file
= g_zfs
->libzfs_mnttab
;
6702 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
6704 libzfs_print_on_error(g_zfs
, B_TRUE
);
6707 * Run the appropriate command.
6709 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
6710 if (find_command_idx(cmdname
, &i
) == 0) {
6711 current_command
= &command_table
[i
];
6712 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
6713 } else if (strchr(cmdname
, '=') != NULL
) {
6714 verify(find_command_idx("set", &i
) == 0);
6715 current_command
= &command_table
[i
];
6716 ret
= command_table
[i
].func(argc
, argv
);
6718 (void) fprintf(stderr
, gettext("unrecognized "
6719 "command '%s'\n"), cmdname
);
6724 if (ret
== 0 && log_history
)
6725 (void) zpool_log_history(g_zfs
, history_str
);
6730 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6731 * for the purposes of running ::findleaks.
6733 if (getenv("ZFS_ABORT") != NULL
) {
6734 (void) printf("dumping core by request\n");