4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2014 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 [-DnPpRvLe] [-[iI] snapshot] "
263 "\tsend [-Le] [-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 * Take a property=value argument string and add it to the given nvlist.
483 * Modifies the argument inplace.
486 parseprop(nvlist_t
*props
, char *propname
)
488 char *propval
, *strval
;
490 if ((propval
= strchr(propname
, '=')) == NULL
) {
491 (void) fprintf(stderr
, gettext("missing "
492 "'=' for property=value argument\n"));
497 if (nvlist_lookup_string(props
, propname
, &strval
) == 0) {
498 (void) fprintf(stderr
, gettext("property '%s' "
499 "specified multiple times\n"), propname
);
502 if (nvlist_add_string(props
, propname
, propval
) != 0)
508 parse_depth(char *opt
, int *flags
)
513 depth
= (int)strtol(opt
, &tmp
, 0);
515 (void) fprintf(stderr
,
516 gettext("%s is not an integer\n"), optarg
);
520 (void) fprintf(stderr
,
521 gettext("Depth can not be negative.\n"));
524 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
528 #define PROGRESS_DELAY 2 /* seconds */
530 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";
531 static time_t pt_begin
;
532 static char *pt_header
= NULL
;
533 static boolean_t pt_shown
;
536 start_progress_timer(void)
538 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
543 set_progress_header(char *header
)
545 assert(pt_header
== NULL
);
546 pt_header
= safe_strdup(header
);
548 (void) printf("%s: ", header
);
549 (void) fflush(stdout
);
554 update_progress(char *update
)
556 if (!pt_shown
&& time(NULL
) > pt_begin
) {
557 int len
= strlen(update
);
559 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
561 (void) fflush(stdout
);
563 } else if (pt_shown
) {
564 int len
= strlen(update
);
566 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
567 (void) fflush(stdout
);
572 finish_progress(char *done
)
575 (void) printf("%s\n", done
);
576 (void) fflush(stdout
);
583 zfs_mount_and_share(libzfs_handle_t
*hdl
, const char *dataset
, zfs_type_t type
)
585 zfs_handle_t
*zhp
= NULL
;
588 zhp
= zfs_open(hdl
, dataset
, type
);
593 * Volumes may neither be mounted or shared. Potentially in the
594 * future filesystems detected on these volumes could be mounted.
596 if (zfs_get_type(zhp
) == ZFS_TYPE_VOLUME
) {
602 * Mount and/or share the new filesystem as appropriate. We provide a
603 * verbose error message to let the user know that their filesystem was
604 * in fact created, even if we failed to mount or share it.
606 * If the user doesn't want the dataset automatically mounted, then
607 * skip the mount/share step
609 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
, B_FALSE
) &&
610 zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
) {
611 if (zfs_mount(zhp
, NULL
, 0) != 0) {
612 (void) fprintf(stderr
, gettext("filesystem "
613 "successfully created, but not mounted\n"));
615 } else if (zfs_share(zhp
) != 0) {
616 (void) fprintf(stderr
, gettext("filesystem "
617 "successfully created, but not shared\n"));
628 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
630 * Given an existing dataset, create a writable copy whose initial contents
631 * are the same as the source. The newly created dataset maintains a
632 * dependency on the original; the original cannot be destroyed so long as
635 * The '-p' flag creates all the non-existing ancestors of the target first.
638 zfs_do_clone(int argc
, char **argv
)
640 zfs_handle_t
*zhp
= NULL
;
641 boolean_t parents
= B_FALSE
;
646 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
650 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
653 if (parseprop(props
, optarg
) != 0)
660 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
669 /* check number of arguments */
671 (void) fprintf(stderr
, gettext("missing source dataset "
676 (void) fprintf(stderr
, gettext("missing target dataset "
681 (void) fprintf(stderr
, gettext("too many arguments\n"));
685 /* open the source dataset */
686 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
689 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
692 * Now create the ancestors of the target dataset. If the
693 * target already exists and '-p' option was used we should not
696 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
699 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
704 ret
= zfs_clone(zhp
, argv
[1], props
);
706 /* create the mountpoint if necessary */
709 (void) zpool_log_history(g_zfs
, history_str
);
710 log_history
= B_FALSE
;
713 ret
= zfs_mount_and_share(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
730 * zfs create [-p] [-o prop=value] ... fs
731 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
733 * Create a new dataset. This command can be used to create filesystems
734 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
735 * For volumes, the user must specify a size to be used.
737 * The '-s' flag applies only to volumes, and indicates that we should not try
738 * to set the reservation for this volume. By default we set a reservation
739 * equal to the size for any volume. For pools with SPA_VERSION >=
740 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
742 * The '-p' flag creates all the non-existing ancestors of the target first.
745 zfs_do_create(int argc
, char **argv
)
747 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
748 uint64_t volsize
= 0;
750 boolean_t noreserve
= B_FALSE
;
751 boolean_t bflag
= B_FALSE
;
752 boolean_t parents
= B_FALSE
;
757 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
761 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
764 type
= ZFS_TYPE_VOLUME
;
765 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
766 (void) fprintf(stderr
, gettext("bad volume "
767 "size '%s': %s\n"), optarg
,
768 libzfs_error_description(g_zfs
));
772 if (nvlist_add_uint64(props
,
773 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
782 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
783 (void) fprintf(stderr
, gettext("bad volume "
784 "block size '%s': %s\n"), optarg
,
785 libzfs_error_description(g_zfs
));
789 if (nvlist_add_uint64(props
,
790 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
795 if (parseprop(props
, optarg
))
802 (void) fprintf(stderr
, gettext("missing size "
807 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
813 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
814 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
815 "used when creating a volume\n"));
822 /* check number of arguments */
824 (void) fprintf(stderr
, gettext("missing %s argument\n"),
825 zfs_type_to_name(type
));
829 (void) fprintf(stderr
, gettext("too many arguments\n"));
833 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
834 zpool_handle_t
*zpool_handle
;
835 uint64_t spa_version
;
837 zfs_prop_t resv_prop
;
840 if ((p
= strchr(argv
[0], '/')))
842 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
845 if (zpool_handle
== NULL
)
847 spa_version
= zpool_get_prop_int(zpool_handle
,
848 ZPOOL_PROP_VERSION
, NULL
);
849 zpool_close(zpool_handle
);
850 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
851 resv_prop
= ZFS_PROP_REFRESERVATION
;
853 resv_prop
= ZFS_PROP_RESERVATION
;
854 volsize
= zvol_volsize_to_reservation(volsize
, props
);
856 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
858 if (nvlist_add_uint64(props
,
859 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
866 if (parents
&& zfs_name_valid(argv
[0], type
)) {
868 * Now create the ancestors of target dataset. If the target
869 * already exists and '-p' option was used we should not
872 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
876 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
881 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
885 (void) zpool_log_history(g_zfs
, history_str
);
886 log_history
= B_FALSE
;
889 ret
= zfs_mount_and_share(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
900 * zfs destroy [-rRf] <fs, vol>
901 * zfs destroy [-rRd] <snap>
903 * -r Recursively destroy all children
904 * -R Recursively destroy all dependents, including clones
905 * -f Force unmounting of any dependents
906 * -d If we can't destroy now, mark for deferred destruction
908 * Destroys the given dataset. By default, it will unmount any filesystems,
909 * and refuse to destroy a dataset that has any dependents. A dependent can
910 * either be a child, or a clone of a child.
912 typedef struct destroy_cbdata
{
915 boolean_t cb_recurse
;
917 boolean_t cb_doclones
;
918 zfs_handle_t
*cb_target
;
919 boolean_t cb_defer_destroy
;
920 boolean_t cb_verbose
;
921 boolean_t cb_parsable
;
924 nvlist_t
*cb_batchedsnaps
;
926 /* first snap in contiguous run */
928 /* previous snap in contiguous run */
936 * Check for any dependents based on the '-r' or '-R' flags.
939 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
941 destroy_cbdata_t
*cbp
= data
;
942 const char *tname
= zfs_get_name(cbp
->cb_target
);
943 const char *name
= zfs_get_name(zhp
);
945 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
946 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
948 * This is a direct descendant, not a clone somewhere else in
955 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
956 "%s has children\n"),
957 zfs_get_name(cbp
->cb_target
),
958 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
959 (void) fprintf(stderr
, gettext("use '-r' to destroy "
960 "the following datasets:\n"));
961 cbp
->cb_first
= B_FALSE
;
962 cbp
->cb_error
= B_TRUE
;
965 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
968 * This is a clone. We only want to report this if the '-r'
969 * wasn't specified, or the target is a snapshot.
971 if (!cbp
->cb_recurse
&&
972 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
976 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
977 "%s has dependent clones\n"),
978 zfs_get_name(cbp
->cb_target
),
979 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
980 (void) fprintf(stderr
, gettext("use '-R' to destroy "
981 "the following datasets:\n"));
982 cbp
->cb_first
= B_FALSE
;
983 cbp
->cb_error
= B_TRUE
;
984 cbp
->cb_dryrun
= B_TRUE
;
987 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
996 destroy_callback(zfs_handle_t
*zhp
, void *data
)
998 destroy_cbdata_t
*cb
= data
;
999 const char *name
= zfs_get_name(zhp
);
1001 if (cb
->cb_verbose
) {
1002 if (cb
->cb_parsable
) {
1003 (void) printf("destroy\t%s\n", name
);
1004 } else if (cb
->cb_dryrun
) {
1005 (void) printf(gettext("would destroy %s\n"),
1008 (void) printf(gettext("will destroy %s\n"),
1014 * Ignore pools (which we've already flagged as an error before getting
1017 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1018 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1022 if (cb
->cb_dryrun
) {
1028 * We batch up all contiguous snapshots (even of different
1029 * filesystems) and destroy them with one ioctl. We can't
1030 * simply do all snap deletions and then all fs deletions,
1031 * because we must delete a clone before its origin.
1033 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1034 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1036 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1037 cb
->cb_batchedsnaps
, B_FALSE
);
1038 fnvlist_free(cb
->cb_batchedsnaps
);
1039 cb
->cb_batchedsnaps
= fnvlist_alloc();
1042 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1043 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1054 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1056 destroy_cbdata_t
*cb
= arg
;
1057 const char *name
= zfs_get_name(zhp
);
1060 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1061 if (cb
->cb_firstsnap
== NULL
)
1062 cb
->cb_firstsnap
= strdup(name
);
1063 if (cb
->cb_prevsnap
!= NULL
)
1064 free(cb
->cb_prevsnap
);
1065 /* this snap continues the current range */
1066 cb
->cb_prevsnap
= strdup(name
);
1067 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1069 if (cb
->cb_verbose
) {
1070 if (cb
->cb_parsable
) {
1071 (void) printf("destroy\t%s\n", name
);
1072 } else if (cb
->cb_dryrun
) {
1073 (void) printf(gettext("would destroy %s\n"),
1076 (void) printf(gettext("will destroy %s\n"),
1080 } else if (cb
->cb_firstsnap
!= NULL
) {
1081 /* end of this range */
1083 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1084 cb
->cb_prevsnap
, &used
);
1085 cb
->cb_snapused
+= used
;
1086 free(cb
->cb_firstsnap
);
1087 cb
->cb_firstsnap
= NULL
;
1088 free(cb
->cb_prevsnap
);
1089 cb
->cb_prevsnap
= NULL
;
1096 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1099 assert(cb
->cb_firstsnap
== NULL
);
1100 assert(cb
->cb_prevsnap
== NULL
);
1101 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1102 if (cb
->cb_firstsnap
!= NULL
) {
1105 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1106 cb
->cb_prevsnap
, &used
);
1108 cb
->cb_snapused
+= used
;
1109 free(cb
->cb_firstsnap
);
1110 cb
->cb_firstsnap
= NULL
;
1111 free(cb
->cb_prevsnap
);
1112 cb
->cb_prevsnap
= NULL
;
1118 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1120 destroy_cbdata_t
*cb
= arg
;
1123 /* Check for clones. */
1124 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1125 cb
->cb_target
= zhp
;
1126 cb
->cb_first
= B_TRUE
;
1127 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1128 destroy_check_dependent
, cb
);
1132 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1140 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1142 destroy_cbdata_t
*cb
= arg
;
1145 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1151 if (cb
->cb_verbose
) {
1152 err
= destroy_print_snapshots(zhp
, cb
);
1158 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1166 destroy_clones(destroy_cbdata_t
*cb
)
1169 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1171 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1172 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1175 boolean_t defer
= cb
->cb_defer_destroy
;
1179 * We can't defer destroy non-snapshots, so set it to
1180 * false while destroying the clones.
1182 cb
->cb_defer_destroy
= B_FALSE
;
1183 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1184 destroy_callback
, cb
);
1185 cb
->cb_defer_destroy
= defer
;
1195 zfs_do_destroy(int argc
, char **argv
)
1197 destroy_cbdata_t cb
= { 0 };
1201 zfs_handle_t
*zhp
= NULL
;
1203 zfs_type_t type
= ZFS_TYPE_DATASET
;
1206 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1209 cb
.cb_verbose
= B_TRUE
;
1212 cb
.cb_verbose
= B_TRUE
;
1213 cb
.cb_parsable
= B_TRUE
;
1216 cb
.cb_dryrun
= B_TRUE
;
1219 cb
.cb_defer_destroy
= B_TRUE
;
1220 type
= ZFS_TYPE_SNAPSHOT
;
1223 cb
.cb_force
= B_TRUE
;
1226 cb
.cb_recurse
= B_TRUE
;
1229 cb
.cb_recurse
= B_TRUE
;
1230 cb
.cb_doclones
= B_TRUE
;
1234 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1243 /* check number of arguments */
1245 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1249 (void) fprintf(stderr
, gettext("too many arguments\n"));
1253 at
= strchr(argv
[0], '@');
1254 pound
= strchr(argv
[0], '#');
1257 /* Build the list of snaps to destroy in cb_nvl. */
1258 cb
.cb_nvl
= fnvlist_alloc();
1261 zhp
= zfs_open(g_zfs
, argv
[0],
1262 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1266 cb
.cb_snapspec
= at
+ 1;
1267 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1273 if (nvlist_empty(cb
.cb_nvl
)) {
1274 (void) fprintf(stderr
, gettext("could not find any "
1275 "snapshots to destroy; check snapshot names.\n"));
1280 if (cb
.cb_verbose
) {
1282 zfs_nicenum(cb
.cb_snapused
, buf
, sizeof (buf
));
1283 if (cb
.cb_parsable
) {
1284 (void) printf("reclaim\t%llu\n",
1285 (u_longlong_t
)cb
.cb_snapused
);
1286 } else if (cb
.cb_dryrun
) {
1287 (void) printf(gettext("would reclaim %s\n"),
1290 (void) printf(gettext("will reclaim %s\n"),
1295 if (!cb
.cb_dryrun
) {
1296 if (cb
.cb_doclones
) {
1297 cb
.cb_batchedsnaps
= fnvlist_alloc();
1298 err
= destroy_clones(&cb
);
1300 err
= zfs_destroy_snaps_nvl(g_zfs
,
1301 cb
.cb_batchedsnaps
, B_FALSE
);
1309 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1310 cb
.cb_defer_destroy
);
1316 } else if (pound
!= NULL
) {
1321 (void) fprintf(stderr
,
1322 "dryrun is not supported with bookmark\n");
1326 if (cb
.cb_defer_destroy
) {
1327 (void) fprintf(stderr
,
1328 "defer destroy is not supported with bookmark\n");
1332 if (cb
.cb_recurse
) {
1333 (void) fprintf(stderr
,
1334 "recursive is not supported with bookmark\n");
1338 if (!zfs_bookmark_exists(argv
[0])) {
1339 (void) fprintf(stderr
, gettext("bookmark '%s' "
1340 "does not exist.\n"), argv
[0]);
1344 nvl
= fnvlist_alloc();
1345 fnvlist_add_boolean(nvl
, argv
[0]);
1347 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1349 (void) zfs_standard_error(g_zfs
, err
,
1350 "cannot destroy bookmark");
1353 nvlist_free(cb
.cb_nvl
);
1357 /* Open the given dataset */
1358 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1364 * Perform an explicit check for pools before going any further.
1366 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1367 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1368 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1369 "operation does not apply to pools\n"),
1371 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1372 "%s' to destroy all datasets in the pool\n"),
1374 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1375 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1381 * Check for any dependents and/or clones.
1383 cb
.cb_first
= B_TRUE
;
1384 if (!cb
.cb_doclones
&&
1385 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1396 cb
.cb_batchedsnaps
= fnvlist_alloc();
1397 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1404 * Do the real thing. The callback will close the
1405 * handle regardless of whether it succeeds or not.
1407 err
= destroy_callback(zhp
, &cb
);
1410 err
= zfs_destroy_snaps_nvl(g_zfs
,
1411 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1418 fnvlist_free(cb
.cb_batchedsnaps
);
1419 fnvlist_free(cb
.cb_nvl
);
1426 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1429 zfs_get_column_t col
;
1431 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1432 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1433 if (col
== GET_COL_RECVD
)
1439 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1440 * < all | property[,property]... > < fs | snap | vol > ...
1442 * -r recurse over any child datasets
1443 * -H scripted mode. Headers are stripped, and fields are separated
1444 * by tabs instead of spaces.
1445 * -o Set of fields to display. One of "name,property,value,
1446 * received,source". Default is "name,property,value,source".
1447 * "all" is an alias for all five.
1448 * -s Set of sources to allow. One of
1449 * "local,default,inherited,received,temporary,none". Default is
1451 * -p Display values in parsable (literal) format.
1453 * Prints properties for the given datasets. The user can control which
1454 * columns to display as well as which property types to allow.
1458 * Invoked to display the properties for a single dataset.
1461 get_callback(zfs_handle_t
*zhp
, void *data
)
1463 char buf
[ZFS_MAXPROPLEN
];
1464 char rbuf
[ZFS_MAXPROPLEN
];
1465 zprop_source_t sourcetype
;
1466 char source
[ZFS_MAXNAMELEN
];
1467 zprop_get_cbdata_t
*cbp
= data
;
1468 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1469 zprop_list_t
*pl
= cbp
->cb_proplist
;
1473 boolean_t received
= is_recvd_column(cbp
);
1475 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1476 char *recvdval
= NULL
;
1478 * Skip the special fake placeholder. This will also skip over
1479 * the name property when 'all' is specified.
1481 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1482 pl
== cbp
->cb_proplist
)
1485 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1486 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1487 sizeof (buf
), &sourcetype
, source
,
1489 cbp
->cb_literal
) != 0) {
1492 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1493 ZFS_TYPE_DATASET
, B_FALSE
)) {
1494 (void) fprintf(stderr
,
1495 gettext("No such property '%s'\n"),
1496 zfs_prop_to_name(pl
->pl_prop
));
1499 sourcetype
= ZPROP_SRC_NONE
;
1500 (void) strlcpy(buf
, "-", sizeof (buf
));
1503 if (received
&& (zfs_prop_get_recvd(zhp
,
1504 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1505 cbp
->cb_literal
) == 0))
1508 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1509 zfs_prop_to_name(pl
->pl_prop
),
1510 buf
, sourcetype
, source
, recvdval
);
1511 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1512 sourcetype
= ZPROP_SRC_LOCAL
;
1514 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1515 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1516 sourcetype
= ZPROP_SRC_NONE
;
1517 (void) strlcpy(buf
, "-", sizeof (buf
));
1520 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1521 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1522 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1523 sourcetype
= ZPROP_SRC_LOCAL
;
1525 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1526 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1527 sourcetype
= ZPROP_SRC_NONE
;
1528 (void) strlcpy(buf
, "-", sizeof (buf
));
1531 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1532 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1534 if (nvlist_lookup_nvlist(user_props
,
1535 pl
->pl_user_prop
, &propval
) != 0) {
1538 sourcetype
= ZPROP_SRC_NONE
;
1541 verify(nvlist_lookup_string(propval
,
1542 ZPROP_VALUE
, &strval
) == 0);
1543 verify(nvlist_lookup_string(propval
,
1544 ZPROP_SOURCE
, &sourceval
) == 0);
1546 if (strcmp(sourceval
,
1547 zfs_get_name(zhp
)) == 0) {
1548 sourcetype
= ZPROP_SRC_LOCAL
;
1549 } else if (strcmp(sourceval
,
1550 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1551 sourcetype
= ZPROP_SRC_RECEIVED
;
1553 sourcetype
= ZPROP_SRC_INHERITED
;
1554 (void) strlcpy(source
,
1555 sourceval
, sizeof (source
));
1559 if (received
&& (zfs_prop_get_recvd(zhp
,
1560 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1561 cbp
->cb_literal
) == 0))
1564 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1565 pl
->pl_user_prop
, strval
, sourcetype
,
1574 zfs_do_get(int argc
, char **argv
)
1576 zprop_get_cbdata_t cb
= { 0 };
1577 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1578 int types
= ZFS_TYPE_DATASET
;
1579 char *value
, *fields
;
1582 zprop_list_t fake_name
= { 0 };
1585 * Set up default columns and sources.
1587 cb
.cb_sources
= ZPROP_SRC_ALL
;
1588 cb
.cb_columns
[0] = GET_COL_NAME
;
1589 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1590 cb
.cb_columns
[2] = GET_COL_VALUE
;
1591 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1592 cb
.cb_type
= ZFS_TYPE_DATASET
;
1595 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1598 cb
.cb_literal
= B_TRUE
;
1601 limit
= parse_depth(optarg
, &flags
);
1604 flags
|= ZFS_ITER_RECURSE
;
1607 cb
.cb_scripted
= B_TRUE
;
1610 (void) fprintf(stderr
, gettext("missing argument for "
1611 "'%c' option\n"), optopt
);
1616 * Process the set of columns to display. We zero out
1617 * the structure to give us a blank slate.
1619 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1621 while (*optarg
!= '\0') {
1622 static char *col_subopts
[] =
1623 { "name", "property", "value", "received",
1624 "source", "all", NULL
};
1626 if (i
== ZFS_GET_NCOLS
) {
1627 (void) fprintf(stderr
, gettext("too "
1628 "many fields given to -o "
1633 switch (getsubopt(&optarg
, col_subopts
,
1636 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1639 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1642 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1645 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1646 flags
|= ZFS_ITER_RECVD_PROPS
;
1649 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1653 (void) fprintf(stderr
,
1654 gettext("\"all\" conflicts "
1655 "with specific fields "
1656 "given to -o option\n"));
1659 cb
.cb_columns
[0] = GET_COL_NAME
;
1660 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1661 cb
.cb_columns
[2] = GET_COL_VALUE
;
1662 cb
.cb_columns
[3] = GET_COL_RECVD
;
1663 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1664 flags
|= ZFS_ITER_RECVD_PROPS
;
1668 (void) fprintf(stderr
,
1669 gettext("invalid column name "
1678 while (*optarg
!= '\0') {
1679 static char *source_subopts
[] = {
1680 "local", "default", "inherited",
1681 "received", "temporary", "none",
1684 switch (getsubopt(&optarg
, source_subopts
,
1687 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1690 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1693 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1696 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1699 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1702 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1705 (void) fprintf(stderr
,
1706 gettext("invalid source "
1715 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1716 while (*optarg
!= '\0') {
1717 static char *type_subopts
[] = { "filesystem",
1718 "volume", "snapshot", "bookmark",
1721 switch (getsubopt(&optarg
, type_subopts
,
1724 types
|= ZFS_TYPE_FILESYSTEM
;
1727 types
|= ZFS_TYPE_VOLUME
;
1730 types
|= ZFS_TYPE_SNAPSHOT
;
1733 types
|= ZFS_TYPE_BOOKMARK
;
1736 types
= ZFS_TYPE_DATASET
|
1741 (void) fprintf(stderr
,
1742 gettext("invalid type '%s'\n"),
1750 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1760 (void) fprintf(stderr
, gettext("missing property "
1767 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1775 * As part of zfs_expand_proplist(), we keep track of the maximum column
1776 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1777 * need to know the maximum name length. However, the user likely did
1778 * not specify 'name' as one of the properties to fetch, so we need to
1779 * make sure we always include at least this property for
1780 * print_get_headers() to work properly.
1782 if (cb
.cb_proplist
!= NULL
) {
1783 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1784 fake_name
.pl_width
= strlen(gettext("NAME"));
1785 fake_name
.pl_next
= cb
.cb_proplist
;
1786 cb
.cb_proplist
= &fake_name
;
1789 cb
.cb_first
= B_TRUE
;
1791 /* run for each object */
1792 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1793 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1795 if (cb
.cb_proplist
== &fake_name
)
1796 zprop_free_list(fake_name
.pl_next
);
1798 zprop_free_list(cb
.cb_proplist
);
1804 * inherit [-rS] <property> <fs|vol> ...
1806 * -r Recurse over all children
1807 * -S Revert to received value, if any
1809 * For each dataset specified on the command line, inherit the given property
1810 * from its parent. Inheriting a property at the pool level will cause it to
1811 * use the default value. The '-r' flag will recurse over all children, and is
1812 * useful for setting a property on a hierarchy-wide basis, regardless of any
1813 * local modifications for each dataset.
1816 typedef struct inherit_cbdata
{
1817 const char *cb_propname
;
1818 boolean_t cb_received
;
1822 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1824 inherit_cbdata_t
*cb
= data
;
1825 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1828 * If we're doing it recursively, then ignore properties that
1829 * are not valid for this type of dataset.
1831 if (prop
!= ZPROP_INVAL
&&
1832 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
), B_FALSE
))
1835 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1839 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1841 inherit_cbdata_t
*cb
= data
;
1843 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1847 zfs_do_inherit(int argc
, char **argv
)
1851 inherit_cbdata_t cb
= { 0 };
1855 boolean_t received
= B_FALSE
;
1858 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1861 flags
|= ZFS_ITER_RECURSE
;
1868 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1877 /* check number of arguments */
1879 (void) fprintf(stderr
, gettext("missing property argument\n"));
1883 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1891 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1892 if (zfs_prop_readonly(prop
)) {
1893 (void) fprintf(stderr
, gettext(
1894 "%s property is read-only\n"),
1898 if (!zfs_prop_inheritable(prop
) && !received
) {
1899 (void) fprintf(stderr
, gettext("'%s' property cannot "
1900 "be inherited\n"), propname
);
1901 if (prop
== ZFS_PROP_QUOTA
||
1902 prop
== ZFS_PROP_RESERVATION
||
1903 prop
== ZFS_PROP_REFQUOTA
||
1904 prop
== ZFS_PROP_REFRESERVATION
) {
1905 (void) fprintf(stderr
, gettext("use 'zfs set "
1906 "%s=none' to clear\n"), propname
);
1907 (void) fprintf(stderr
, gettext("use 'zfs "
1908 "inherit -S %s' to revert to received "
1909 "value\n"), propname
);
1913 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1914 prop
== ZFS_PROP_VERSION
)) {
1915 (void) fprintf(stderr
, gettext("'%s' property cannot "
1916 "be reverted to a received value\n"), propname
);
1919 } else if (!zfs_prop_user(propname
)) {
1920 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1925 cb
.cb_propname
= propname
;
1926 cb
.cb_received
= received
;
1928 if (flags
& ZFS_ITER_RECURSE
) {
1929 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1930 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1932 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1933 NULL
, NULL
, 0, inherit_cb
, &cb
);
1939 typedef struct upgrade_cbdata
{
1940 uint64_t cb_numupgraded
;
1941 uint64_t cb_numsamegraded
;
1942 uint64_t cb_numfailed
;
1943 uint64_t cb_version
;
1945 boolean_t cb_foundone
;
1946 char cb_lastfs
[ZFS_MAXNAMELEN
];
1950 same_pool(zfs_handle_t
*zhp
, const char *name
)
1952 int len1
= strcspn(name
, "/@");
1953 const char *zhname
= zfs_get_name(zhp
);
1954 int len2
= strcspn(zhname
, "/@");
1958 return (strncmp(name
, zhname
, len1
) == 0);
1962 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
1964 upgrade_cbdata_t
*cb
= data
;
1965 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1967 /* list if it's old/new */
1968 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
1969 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
1972 str
= gettext("The following filesystems are "
1973 "formatted using a newer software version and\n"
1974 "cannot be accessed on the current system.\n\n");
1976 str
= gettext("The following filesystems are "
1977 "out of date, and can be upgraded. After being\n"
1978 "upgraded, these filesystems (and any 'zfs send' "
1979 "streams generated from\n"
1980 "subsequent snapshots) will no longer be "
1981 "accessible by older software versions.\n\n");
1984 if (!cb
->cb_foundone
) {
1986 (void) printf(gettext("VER FILESYSTEM\n"));
1987 (void) printf(gettext("--- ------------\n"));
1988 cb
->cb_foundone
= B_TRUE
;
1991 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
1998 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2000 upgrade_cbdata_t
*cb
= data
;
2001 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2002 int needed_spa_version
;
2005 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2008 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2010 if (needed_spa_version
< 0)
2013 if (spa_version
< needed_spa_version
) {
2015 (void) printf(gettext("%s: can not be "
2016 "upgraded; the pool version needs to first "
2017 "be upgraded\nto version %d\n\n"),
2018 zfs_get_name(zhp
), needed_spa_version
);
2024 if (version
< cb
->cb_version
) {
2026 (void) snprintf(verstr
, sizeof (verstr
),
2027 "%llu", (u_longlong_t
)cb
->cb_version
);
2028 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2030 * If they did "zfs upgrade -a", then we could
2031 * be doing ioctls to different pools. We need
2032 * to log this history once to each pool, and bypass
2033 * the normal history logging that happens in main().
2035 (void) zpool_log_history(g_zfs
, history_str
);
2036 log_history
= B_FALSE
;
2038 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2039 cb
->cb_numupgraded
++;
2042 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2043 } else if (version
> cb
->cb_version
) {
2044 /* can't downgrade */
2045 (void) printf(gettext("%s: can not be downgraded; "
2046 "it is already at version %u\n"),
2047 zfs_get_name(zhp
), version
);
2050 cb
->cb_numsamegraded
++;
2058 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2061 zfs_do_upgrade(int argc
, char **argv
)
2063 boolean_t all
= B_FALSE
;
2064 boolean_t showversions
= B_FALSE
;
2066 upgrade_cbdata_t cb
= { 0 };
2068 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2071 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2074 flags
|= ZFS_ITER_RECURSE
;
2077 showversions
= B_TRUE
;
2080 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2081 optarg
, &cb
.cb_version
) != 0) {
2082 (void) fprintf(stderr
,
2083 gettext("invalid version %s\n"), optarg
);
2092 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2101 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2103 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2104 cb
.cb_version
|| argc
))
2106 if ((all
|| argc
) && (showversions
))
2112 /* Show info on available versions. */
2113 (void) printf(gettext("The following filesystem versions are "
2115 (void) printf(gettext("VER DESCRIPTION\n"));
2116 (void) printf("--- -----------------------------------------"
2117 "---------------\n");
2118 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2119 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2120 (void) printf(gettext(" 3 Case insensitive and filesystem "
2121 "user identifier (FUID)\n"));
2122 (void) printf(gettext(" 4 userquota, groupquota "
2124 (void) printf(gettext(" 5 System attributes\n"));
2125 (void) printf(gettext("\nFor more information on a particular "
2126 "version, including supported releases,\n"));
2127 (void) printf("see the ZFS Administration Guide.\n\n");
2129 } else if (argc
|| all
) {
2130 /* Upgrade filesystems */
2131 if (cb
.cb_version
== 0)
2132 cb
.cb_version
= ZPL_VERSION
;
2133 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2134 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2135 (void) printf(gettext("%llu filesystems upgraded\n"),
2136 (u_longlong_t
)cb
.cb_numupgraded
);
2137 if (cb
.cb_numsamegraded
) {
2138 (void) printf(gettext("%llu filesystems already at "
2140 (u_longlong_t
)cb
.cb_numsamegraded
);
2142 if (cb
.cb_numfailed
!= 0)
2145 /* List old-version filesytems */
2147 (void) printf(gettext("This system is currently running "
2148 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2150 flags
|= ZFS_ITER_RECURSE
;
2151 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2152 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2154 found
= cb
.cb_foundone
;
2155 cb
.cb_foundone
= B_FALSE
;
2156 cb
.cb_newer
= B_TRUE
;
2158 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2159 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2161 if (!cb
.cb_foundone
&& !found
) {
2162 (void) printf(gettext("All filesystems are "
2163 "formatted with the current version.\n"));
2171 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2172 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2173 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2174 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2176 * -H Scripted mode; elide headers and separate columns by tabs.
2177 * -i Translate SID to POSIX ID.
2178 * -n Print numeric ID instead of user/group name.
2179 * -o Control which fields to display.
2180 * -p Use exact (parsable) numeric output.
2181 * -s Specify sort columns, descending order.
2182 * -S Specify sort columns, ascending order.
2183 * -t Control which object types to display.
2185 * Displays space consumed by, and quotas on, each user in the specified
2186 * filesystem or snapshot.
2189 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2190 enum us_field_types
{
2196 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA" };
2197 static char *us_field_names
[] = { "type", "name", "used", "quota" };
2198 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2200 #define USTYPE_PSX_GRP (1 << 0)
2201 #define USTYPE_PSX_USR (1 << 1)
2202 #define USTYPE_SMB_GRP (1 << 2)
2203 #define USTYPE_SMB_USR (1 << 3)
2204 #define USTYPE_ALL \
2205 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2207 static int us_type_bits
[] = {
2214 static char *us_type_names
[] = { "posixgroup", "posixuser", "smbgroup",
2217 typedef struct us_node
{
2219 uu_avl_node_t usn_avlnode
;
2220 uu_list_node_t usn_listnode
;
2223 typedef struct us_cbdata
{
2225 uu_avl_pool_t
*cb_avl_pool
;
2227 boolean_t cb_numname
;
2228 boolean_t cb_nicenum
;
2229 boolean_t cb_sid2posix
;
2230 zfs_userquota_prop_t cb_prop
;
2231 zfs_sort_column_t
*cb_sortcol
;
2232 size_t cb_width
[USFIELD_LAST
];
2235 static boolean_t us_populated
= B_FALSE
;
2238 zfs_sort_column_t
*si_sortcol
;
2239 boolean_t si_numname
;
2243 us_field_index(char *field
)
2247 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2248 if (strcmp(field
, us_field_names
[i
]) == 0)
2256 us_compare(const void *larg
, const void *rarg
, void *unused
)
2258 const us_node_t
*l
= larg
;
2259 const us_node_t
*r
= rarg
;
2260 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2261 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2262 boolean_t numname
= si
->si_numname
;
2263 nvlist_t
*lnvl
= l
->usn_nvl
;
2264 nvlist_t
*rnvl
= r
->usn_nvl
;
2268 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2275 zfs_prop_t prop
= sortcol
->sc_prop
;
2276 const char *propname
= NULL
;
2277 boolean_t reverse
= sortcol
->sc_reverse
;
2282 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2283 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2285 rc
= (rv32
< lv32
) ? 1 : -1;
2290 (void) nvlist_lookup_uint64(lnvl
, propname
,
2292 (void) nvlist_lookup_uint64(rnvl
, propname
,
2295 rc
= (rv64
< lv64
) ? 1 : -1;
2297 (void) nvlist_lookup_string(lnvl
, propname
,
2299 (void) nvlist_lookup_string(rnvl
, propname
,
2301 rc
= strcmp(lvstr
, rvstr
);
2305 case ZFS_PROP_QUOTA
:
2308 if (prop
== ZFS_PROP_USED
)
2312 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2313 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2315 rc
= (rv64
< lv64
) ? 1 : -1;
2323 return (reverse
? 1 : -1);
2325 return (reverse
? -1 : 1);
2330 * If entries still seem to be the same, check if they are of the same
2331 * type (smbentity is added only if we are doing SID to POSIX ID
2332 * translation where we can have duplicate type/name combinations).
2334 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2335 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2337 return (lvb
< rvb
? -1 : 1);
2342 static inline const char *
2343 us_type2str(unsigned field_type
)
2345 switch (field_type
) {
2346 case USTYPE_PSX_USR
:
2347 return ("POSIX User");
2348 case USTYPE_PSX_GRP
:
2349 return ("POSIX Group");
2350 case USTYPE_SMB_USR
:
2351 return ("SMB User");
2352 case USTYPE_SMB_GRP
:
2353 return ("SMB Group");
2355 return ("Undefined");
2360 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2362 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2363 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2368 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2369 uu_avl_t
*avl
= cb
->cb_avl
;
2373 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2375 const char *typestr
;
2379 int typeidx
, nameidx
, sizeidx
;
2380 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2381 boolean_t smbentity
= B_FALSE
;
2383 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2385 node
= safe_malloc(sizeof (us_node_t
));
2386 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2387 node
->usn_nvl
= props
;
2389 if (domain
!= NULL
&& domain
[0] != '\0') {
2392 char sid
[ZFS_MAXNAMELEN
+ 32];
2396 directory_error_t e
;
2400 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2402 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2403 type
= USTYPE_SMB_GRP
;
2404 err
= sid_to_id(sid
, B_FALSE
, &id
);
2406 type
= USTYPE_SMB_USR
;
2407 err
= sid_to_id(sid
, B_TRUE
, &id
);
2412 if (!cb
->cb_sid2posix
) {
2413 e
= directory_name_from_sid(NULL
, sid
, &name
,
2416 directory_error_free(e
);
2426 #endif /* HAVE_IDMAP */
2429 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2431 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2432 type
= USTYPE_PSX_GRP
;
2433 if (!cb
->cb_numname
) {
2436 if ((g
= getgrgid(rid
)) != NULL
)
2440 type
= USTYPE_PSX_USR
;
2441 if (!cb
->cb_numname
) {
2444 if ((p
= getpwuid(rid
)) != NULL
)
2451 * Make sure that the type/name combination is unique when doing
2452 * SID to POSIX ID translation (hence changing the type from SMB to
2455 if (cb
->cb_sid2posix
&&
2456 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2459 /* Calculate/update width of TYPE field */
2460 typestr
= us_type2str(type
);
2461 typelen
= strlen(gettext(typestr
));
2462 typeidx
= us_field_index("type");
2463 if (typelen
> cb
->cb_width
[typeidx
])
2464 cb
->cb_width
[typeidx
] = typelen
;
2465 if (nvlist_add_uint32(props
, "type", type
) != 0)
2468 /* Calculate/update width of NAME field */
2469 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2470 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2472 namelen
= snprintf(NULL
, 0, "%u", rid
);
2474 if (nvlist_add_string(props
, "name", name
) != 0)
2476 namelen
= strlen(name
);
2478 nameidx
= us_field_index("name");
2479 if (namelen
> cb
->cb_width
[nameidx
])
2480 cb
->cb_width
[nameidx
] = namelen
;
2483 * Check if this type/name combination is in the list and update it;
2484 * otherwise add new node to the list.
2486 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2487 uu_avl_insert(avl
, node
, idx
);
2492 props
= node
->usn_nvl
;
2495 /* Calculate/update width of USED/QUOTA fields */
2497 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2499 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu",
2500 (u_longlong_t
)space
);
2501 sizelen
= strlen(sizebuf
);
2502 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
) {
2504 if (!nvlist_exists(props
, "quota"))
2505 (void) nvlist_add_uint64(props
, "quota", 0);
2508 if (!nvlist_exists(props
, "used"))
2509 (void) nvlist_add_uint64(props
, "used", 0);
2511 sizeidx
= us_field_index(propname
);
2512 if (sizelen
> cb
->cb_width
[sizeidx
])
2513 cb
->cb_width
[sizeidx
] = sizelen
;
2515 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2522 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2523 size_t *width
, us_node_t
*node
)
2525 nvlist_t
*nvl
= node
->usn_nvl
;
2526 char valstr
[ZFS_MAXNAMELEN
];
2527 boolean_t first
= B_TRUE
;
2533 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2534 if (!(ustype
& types
))
2537 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2538 nvpair_t
*nvp
= NULL
;
2542 char *strval
= NULL
;
2544 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2545 if (strcmp(nvpair_name(nvp
),
2546 us_field_names
[field
]) == 0)
2550 type
= nvpair_type(nvp
);
2552 case DATA_TYPE_UINT32
:
2553 (void) nvpair_value_uint32(nvp
, &val32
);
2555 case DATA_TYPE_UINT64
:
2556 (void) nvpair_value_uint64(nvp
, &val64
);
2558 case DATA_TYPE_STRING
:
2559 (void) nvpair_value_string(nvp
, &strval
);
2562 (void) fprintf(stderr
, "invalid data type\n");
2567 strval
= (char *)us_type2str(val32
);
2570 if (type
== DATA_TYPE_UINT64
) {
2571 (void) sprintf(valstr
, "%llu",
2572 (u_longlong_t
) val64
);
2578 if (type
== DATA_TYPE_UINT64
) {
2580 (void) sprintf(valstr
, "%llu",
2581 (u_longlong_t
) val64
);
2583 zfs_nicenum(val64
, valstr
,
2586 if (field
== USFIELD_QUOTA
&&
2587 strcmp(valstr
, "0") == 0)
2597 (void) printf("\t");
2602 (void) printf("%s", strval
);
2603 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2604 (void) printf("%-*s", (int) width
[field
], strval
);
2606 (void) printf("%*s", (int) width
[field
], strval
);
2612 (void) printf("\n");
2616 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2617 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2625 boolean_t first
= B_TRUE
;
2627 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2628 col
= gettext(us_field_hdr
[field
]);
2629 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2630 (void) printf(first
? "%-*s" : " %-*s",
2631 (int) width
[field
], col
);
2633 (void) printf(first
? "%*s" : " %*s",
2634 (int) width
[field
], col
);
2639 (void) printf("\n");
2642 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2643 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2645 nvlist_free(node
->usn_nvl
);
2650 zfs_do_userspace(int argc
, char **argv
)
2653 zfs_userquota_prop_t p
;
2654 uu_avl_pool_t
*avl_pool
;
2656 uu_avl_walk_t
*walk
;
2658 char deffields
[] = "type,name,used,quota";
2659 char *ofield
= NULL
;
2660 char *tfield
= NULL
;
2664 boolean_t scripted
= B_FALSE
;
2665 boolean_t prtnum
= B_FALSE
;
2666 boolean_t parsable
= B_FALSE
;
2667 boolean_t sid2posix
= B_FALSE
;
2670 zfs_sort_column_t
*sortcol
= NULL
;
2671 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2675 uu_list_pool_t
*listpool
;
2677 uu_avl_index_t idx
= 0;
2678 uu_list_index_t idx2
= 0;
2683 if (strcmp(argv
[0], "groupspace") == 0)
2684 /* Toggle default group types */
2685 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2687 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2703 if (zfs_add_sort_column(&sortcol
, optarg
,
2704 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
2705 (void) fprintf(stderr
,
2706 gettext("invalid field '%s'\n"), optarg
);
2717 (void) fprintf(stderr
, gettext("missing argument for "
2718 "'%c' option\n"), optopt
);
2722 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2732 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2736 (void) fprintf(stderr
, gettext("too many arguments\n"));
2740 /* Use default output fields if not specified using -o */
2744 if ((delim
= strchr(ofield
, ',')) != NULL
)
2746 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
2747 (void) fprintf(stderr
, gettext("invalid type '%s' "
2748 "for -o option\n"), ofield
);
2753 } while (delim
!= NULL
);
2754 fields
[cfield
] = USFIELD_LAST
;
2756 /* Override output types (-t option) */
2757 if (tfield
!= NULL
) {
2761 boolean_t found
= B_FALSE
;
2763 if ((delim
= strchr(tfield
, ',')) != NULL
)
2765 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
2767 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
2769 types
|= us_type_bits
[i
];
2774 (void) fprintf(stderr
, gettext("invalid type "
2775 "'%s' for -t option\n"), tfield
);
2780 } while (delim
!= NULL
);
2783 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
2786 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
2787 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
2789 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
2792 /* Always add default sorting columns */
2793 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
2794 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
2796 cb
.cb_sortcol
= sortcol
;
2797 cb
.cb_numname
= prtnum
;
2798 cb
.cb_nicenum
= !parsable
;
2799 cb
.cb_avl_pool
= avl_pool
;
2800 cb
.cb_avl
= avl_tree
;
2801 cb
.cb_sid2posix
= sid2posix
;
2803 for (i
= 0; i
< USFIELD_LAST
; i
++)
2804 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
2806 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
2807 if (((p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
) &&
2808 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
2809 ((p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
) &&
2810 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))))
2813 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
2818 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
2821 us_populated
= B_TRUE
;
2823 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
2824 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
2825 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
2826 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
2828 while (node
!= NULL
) {
2830 node
= uu_avl_next(avl_tree
, node
);
2831 uu_avl_remove(avl_tree
, rmnode
);
2832 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
2833 uu_list_insert(list
, rmnode
, idx2
);
2836 for (node
= uu_list_first(list
); node
!= NULL
;
2837 node
= uu_list_next(list
, node
)) {
2838 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
2840 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
2841 uu_avl_insert(avl_tree
, node
, idx
);
2844 uu_list_destroy(list
);
2845 uu_list_pool_destroy(listpool
);
2847 /* Print and free node nvlist memory */
2848 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
2851 zfs_free_sort_columns(sortcol
);
2853 /* Clean up the AVL tree */
2854 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
2857 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
2858 uu_avl_remove(cb
.cb_avl
, node
);
2862 uu_avl_walk_end(walk
);
2863 uu_avl_destroy(avl_tree
);
2864 uu_avl_pool_destroy(avl_pool
);
2870 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
2871 * [-t type[,...]] [filesystem|volume|snapshot] ...
2873 * -H Scripted mode; elide headers and separate columns by tabs
2874 * -p Display values in parsable (literal) format.
2875 * -r Recurse over all children
2876 * -d Limit recursion by depth.
2877 * -o Control which fields to display.
2878 * -s Specify sort columns, descending order.
2879 * -S Specify sort columns, ascending order.
2880 * -t Control which object types to display.
2882 * When given no arguments, list all filesystems in the system.
2883 * Otherwise, list the specified datasets, optionally recursing down them if
2884 * '-r' is specified.
2886 typedef struct list_cbdata
{
2888 boolean_t cb_literal
;
2889 boolean_t cb_scripted
;
2890 zprop_list_t
*cb_proplist
;
2894 * Given a list of columns to display, output appropriate headers for each one.
2897 print_header(list_cbdata_t
*cb
)
2899 zprop_list_t
*pl
= cb
->cb_proplist
;
2900 char headerbuf
[ZFS_MAXPROPLEN
];
2903 boolean_t first
= B_TRUE
;
2904 boolean_t right_justify
;
2906 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2913 right_justify
= B_FALSE
;
2914 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2915 header
= zfs_prop_column_name(pl
->pl_prop
);
2916 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2918 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2919 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2920 headerbuf
[i
] = '\0';
2924 if (pl
->pl_next
== NULL
&& !right_justify
)
2925 (void) printf("%s", header
);
2926 else if (right_justify
)
2927 (void) printf("%*s", (int)pl
->pl_width
, header
);
2929 (void) printf("%-*s", (int)pl
->pl_width
, header
);
2932 (void) printf("\n");
2936 * Given a dataset and a list of fields, print out all the properties according
2937 * to the described layout.
2940 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
2942 zprop_list_t
*pl
= cb
->cb_proplist
;
2943 boolean_t first
= B_TRUE
;
2944 char property
[ZFS_MAXPROPLEN
];
2945 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
2948 boolean_t right_justify
;
2950 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2952 if (cb
->cb_scripted
)
2953 (void) printf("\t");
2960 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
2961 (void) strlcpy(property
, zfs_get_name(zhp
),
2964 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2965 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
2966 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
2967 sizeof (property
), NULL
, NULL
, 0,
2968 cb
->cb_literal
) != 0)
2972 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2973 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
2974 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
2975 property
, sizeof (property
), cb
->cb_literal
) != 0)
2979 right_justify
= B_TRUE
;
2980 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
2981 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
2982 property
, sizeof (property
), cb
->cb_literal
) != 0)
2986 right_justify
= B_TRUE
;
2988 if (nvlist_lookup_nvlist(userprops
,
2989 pl
->pl_user_prop
, &propval
) != 0)
2992 verify(nvlist_lookup_string(propval
,
2993 ZPROP_VALUE
, &propstr
) == 0);
2994 right_justify
= B_FALSE
;
2998 * If this is being called in scripted mode, or if this is the
2999 * last column and it is left-justified, don't include a width
3002 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3003 (void) printf("%s", propstr
);
3004 else if (right_justify
)
3005 (void) printf("%*s", (int)pl
->pl_width
, propstr
);
3007 (void) printf("%-*s", (int)pl
->pl_width
, propstr
);
3010 (void) printf("\n");
3014 * Generic callback function to list a dataset or snapshot.
3017 list_callback(zfs_handle_t
*zhp
, void *data
)
3019 list_cbdata_t
*cbp
= data
;
3021 if (cbp
->cb_first
) {
3022 if (!cbp
->cb_scripted
)
3024 cbp
->cb_first
= B_FALSE
;
3027 print_dataset(zhp
, cbp
);
3033 zfs_do_list(int argc
, char **argv
)
3036 static char default_fields
[] =
3037 "name,used,available,referenced,mountpoint";
3038 int types
= ZFS_TYPE_DATASET
;
3039 boolean_t types_specified
= B_FALSE
;
3040 char *fields
= NULL
;
3041 list_cbdata_t cb
= { 0 };
3045 zfs_sort_column_t
*sortcol
= NULL
;
3046 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3049 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3055 cb
.cb_literal
= B_TRUE
;
3056 flags
|= ZFS_ITER_LITERAL_PROPS
;
3059 limit
= parse_depth(optarg
, &flags
);
3062 flags
|= ZFS_ITER_RECURSE
;
3065 cb
.cb_scripted
= B_TRUE
;
3068 if (zfs_add_sort_column(&sortcol
, optarg
,
3070 (void) fprintf(stderr
,
3071 gettext("invalid property '%s'\n"), optarg
);
3076 if (zfs_add_sort_column(&sortcol
, optarg
,
3078 (void) fprintf(stderr
,
3079 gettext("invalid property '%s'\n"), optarg
);
3085 types_specified
= B_TRUE
;
3086 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3087 while (*optarg
!= '\0') {
3088 static char *type_subopts
[] = { "filesystem",
3089 "volume", "snapshot", "snap", "bookmark",
3092 switch (getsubopt(&optarg
, type_subopts
,
3095 types
|= ZFS_TYPE_FILESYSTEM
;
3098 types
|= ZFS_TYPE_VOLUME
;
3102 types
|= ZFS_TYPE_SNAPSHOT
;
3105 types
|= ZFS_TYPE_BOOKMARK
;
3108 types
= ZFS_TYPE_DATASET
|
3112 (void) fprintf(stderr
,
3113 gettext("invalid type '%s'\n"),
3120 (void) fprintf(stderr
, gettext("missing argument for "
3121 "'%c' option\n"), optopt
);
3125 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3135 fields
= default_fields
;
3138 * If we are only going to list snapshot names and sort by name,
3139 * then we can use faster version.
3141 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3142 flags
|= ZFS_ITER_SIMPLE
;
3145 * If "-o space" and no types were specified, don't display snapshots.
3147 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3148 types
&= ~ZFS_TYPE_SNAPSHOT
;
3151 * If the user specifies '-o all', the zprop_get_list() doesn't
3152 * normally include the name of the dataset. For 'zfs list', we always
3153 * want this property to be first.
3155 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3159 cb
.cb_first
= B_TRUE
;
3161 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3162 limit
, list_callback
, &cb
);
3164 zprop_free_list(cb
.cb_proplist
);
3165 zfs_free_sort_columns(sortcol
);
3167 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3168 (void) fprintf(stderr
, gettext("no datasets available\n"));
3174 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3175 * zfs rename [-f] -p <fs | vol> <fs | vol>
3176 * zfs rename -r <snap> <snap>
3178 * Renames the given dataset to another of the same type.
3180 * The '-p' flag creates all the non-existing ancestors of the target first.
3184 zfs_do_rename(int argc
, char **argv
)
3189 boolean_t recurse
= B_FALSE
;
3190 boolean_t parents
= B_FALSE
;
3191 boolean_t force_unmount
= B_FALSE
;
3194 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3203 force_unmount
= B_TRUE
;
3207 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3216 /* check number of arguments */
3218 (void) fprintf(stderr
, gettext("missing source dataset "
3223 (void) fprintf(stderr
, gettext("missing target dataset "
3228 (void) fprintf(stderr
, gettext("too many arguments\n"));
3232 if (recurse
&& parents
) {
3233 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3238 if (recurse
&& strchr(argv
[0], '@') == 0) {
3239 (void) fprintf(stderr
, gettext("source dataset for recursive "
3240 "rename must be a snapshot\n"));
3244 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3245 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3248 /* If we were asked and the name looks good, try to create ancestors. */
3249 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3250 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3255 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3264 * Promotes the given clone fs to be the parent
3268 zfs_do_promote(int argc
, char **argv
)
3274 if (argc
> 1 && argv
[1][0] == '-') {
3275 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3280 /* check number of arguments */
3282 (void) fprintf(stderr
, gettext("missing clone filesystem"
3287 (void) fprintf(stderr
, gettext("too many arguments\n"));
3291 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3295 ret
= (zfs_promote(zhp
) != 0);
3303 * zfs rollback [-rRf] <snapshot>
3305 * -r Delete any intervening snapshots before doing rollback
3306 * -R Delete any snapshots and their clones
3307 * -f ignored for backwards compatability
3309 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3310 * since then and making it the active dataset. If more recent snapshots exist,
3311 * the command will complain unless the '-r' flag is given.
3313 typedef struct rollback_cbdata
{
3319 boolean_t cb_recurse
;
3320 } rollback_cbdata_t
;
3323 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3325 rollback_cbdata_t
*cbp
= data
;
3327 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3328 (void) fprintf(stderr
, gettext("cannot rollback to "
3329 "'%s': clones of previous snapshots exist\n"),
3331 (void) fprintf(stderr
, gettext("use '-R' to "
3332 "force deletion of the following clones and "
3338 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3346 * Report any snapshots more recent than the one specified. Used when '-r' is
3347 * not specified. We reuse this same callback for the snapshot dependents - if
3348 * 'cb_dependent' is set, then this is a dependent and we should report it
3349 * without checking the transaction group.
3352 rollback_check(zfs_handle_t
*zhp
, void *data
)
3354 rollback_cbdata_t
*cbp
= data
;
3356 if (cbp
->cb_doclones
) {
3361 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3362 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3363 (void) fprintf(stderr
, gettext("cannot "
3364 "rollback to '%s': more recent snapshots "
3365 "or bookmarks exist\n"),
3367 (void) fprintf(stderr
, gettext("use '-r' to "
3368 "force deletion of the following "
3369 "snapshots and bookmarks:\n"));
3374 if (cbp
->cb_recurse
) {
3375 if (zfs_iter_dependents(zhp
, B_TRUE
,
3376 rollback_check_dependent
, cbp
) != 0) {
3381 (void) fprintf(stderr
, "%s\n",
3390 zfs_do_rollback(int argc
, char **argv
)
3394 boolean_t force
= B_FALSE
;
3395 rollback_cbdata_t cb
= { 0 };
3396 zfs_handle_t
*zhp
, *snap
;
3397 char parentname
[ZFS_MAXNAMELEN
];
3401 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3414 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3423 /* check number of arguments */
3425 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3429 (void) fprintf(stderr
, gettext("too many arguments\n"));
3433 /* open the snapshot */
3434 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3437 /* open the parent dataset */
3438 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3439 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3441 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3447 * Check for more recent snapshots and/or clones based on the presence
3450 cb
.cb_target
= argv
[0];
3451 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3452 cb
.cb_first
= B_TRUE
;
3454 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, rollback_check
, &cb
)) != 0)
3456 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
3459 if ((ret
= cb
.cb_error
) != 0)
3463 * Rollback parent to the given snapshot.
3465 ret
= zfs_rollback(zhp
, snap
, force
);
3478 * zfs set property=value ... { fs | snap | vol } ...
3480 * Sets the given properties for all datasets specified on the command line.
3484 set_callback(zfs_handle_t
*zhp
, void *data
)
3486 nvlist_t
*props
= data
;
3488 if (zfs_prop_set_list(zhp
, props
) != 0) {
3489 switch (libzfs_errno(g_zfs
)) {
3490 case EZFS_MOUNTFAILED
:
3491 (void) fprintf(stderr
, gettext("property may be set "
3492 "but unable to remount filesystem\n"));
3494 case EZFS_SHARENFSFAILED
:
3495 (void) fprintf(stderr
, gettext("property may be set "
3496 "but unable to reshare filesystem\n"));
3505 zfs_do_set(int argc
, char **argv
)
3507 nvlist_t
*props
= NULL
;
3508 int ds_start
= -1; /* argv idx of first dataset arg */
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 arguments\n"));
3525 if (strchr(argv
[1], '=') == NULL
) {
3526 (void) fprintf(stderr
, gettext("missing property=value "
3529 (void) fprintf(stderr
, gettext("missing dataset "
3535 /* validate argument order: prop=val args followed by dataset args */
3536 for (i
= 1; i
< argc
; i
++) {
3537 if (strchr(argv
[i
], '=') != NULL
) {
3539 /* out-of-order prop=val argument */
3540 (void) fprintf(stderr
, gettext("invalid "
3541 "argument order\n"));
3544 } else if (ds_start
< 0) {
3549 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
3553 /* Populate a list of property settings */
3554 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3556 for (i
= 1; i
< ds_start
; i
++) {
3557 if ((ret
= parseprop(props
, argv
[i
])) != 0)
3561 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
3562 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, props
);
3569 typedef struct snap_cbdata
{
3571 boolean_t sd_recursive
;
3572 const char *sd_snapname
;
3576 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3578 snap_cbdata_t
*sd
= arg
;
3583 if (sd
->sd_recursive
&&
3584 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
3589 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3592 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3595 if (sd
->sd_recursive
)
3596 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3602 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3604 * Creates a snapshot with the given name. While functionally equivalent to
3605 * 'zfs create', it is a separate command to differentiate intent.
3608 zfs_do_snapshot(int argc
, char **argv
)
3613 snap_cbdata_t sd
= { 0 };
3614 boolean_t multiple_snaps
= B_FALSE
;
3616 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3618 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3622 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3625 if (parseprop(props
, optarg
))
3629 sd
.sd_recursive
= B_TRUE
;
3630 multiple_snaps
= B_TRUE
;
3633 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3642 /* check number of arguments */
3644 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3649 multiple_snaps
= B_TRUE
;
3650 for (; argc
> 0; argc
--, argv
++) {
3654 atp
= strchr(argv
[0], '@');
3658 sd
.sd_snapname
= atp
+ 1;
3659 zhp
= zfs_open(g_zfs
, argv
[0],
3660 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3663 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3667 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3668 nvlist_free(sd
.sd_nvl
);
3670 if (ret
!= 0 && multiple_snaps
)
3671 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3675 nvlist_free(sd
.sd_nvl
);
3682 * Send a backup stream to stdout.
3685 zfs_do_send(int argc
, char **argv
)
3687 char *fromname
= NULL
;
3688 char *toname
= NULL
;
3691 sendflags_t flags
= { 0 };
3693 nvlist_t
*dbgnv
= NULL
;
3694 boolean_t extraverbose
= B_FALSE
;
3697 while ((c
= getopt(argc
, argv
, ":i:I:RDpvnPLe")) != -1) {
3708 flags
.doall
= B_TRUE
;
3711 flags
.replicate
= B_TRUE
;
3714 flags
.props
= B_TRUE
;
3717 flags
.parsable
= B_TRUE
;
3718 flags
.verbose
= B_TRUE
;
3722 extraverbose
= B_TRUE
;
3723 flags
.verbose
= B_TRUE
;
3724 flags
.progress
= B_TRUE
;
3727 flags
.dedup
= B_TRUE
;
3730 flags
.dryrun
= B_TRUE
;
3733 flags
.largeblock
= B_TRUE
;
3736 flags
.embed_data
= B_TRUE
;
3739 (void) fprintf(stderr
, gettext("missing argument for "
3740 "'%c' option\n"), optopt
);
3744 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3753 /* check number of arguments */
3755 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3759 (void) fprintf(stderr
, gettext("too many arguments\n"));
3763 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
3764 (void) fprintf(stderr
,
3765 gettext("Error: Stream can not be written to a terminal.\n"
3766 "You must redirect standard output.\n"));
3771 * Special case sending a filesystem, or from a bookmark.
3773 if (strchr(argv
[0], '@') == NULL
||
3774 (fromname
&& strchr(fromname
, '#') != NULL
)) {
3775 char frombuf
[ZFS_MAXNAMELEN
];
3776 enum lzc_send_flags lzc_flags
= 0;
3778 if (flags
.replicate
|| flags
.doall
|| flags
.props
||
3779 flags
.dedup
|| flags
.dryrun
|| flags
.verbose
||
3781 (void) fprintf(stderr
,
3783 "Unsupported flag with filesystem or bookmark.\n"));
3787 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
3791 if (flags
.largeblock
)
3792 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
3793 if (flags
.embed_data
)
3794 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
3796 if (fromname
!= NULL
&&
3797 (fromname
[0] == '#' || fromname
[0] == '@')) {
3799 * Incremental source name begins with # or @.
3800 * Default to same fs as target.
3802 (void) strncpy(frombuf
, argv
[0], sizeof (frombuf
));
3803 cp
= strchr(frombuf
, '@');
3806 (void) strlcat(frombuf
, fromname
, sizeof (frombuf
));
3809 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, lzc_flags
);
3814 cp
= strchr(argv
[0], '@');
3817 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3822 * If they specified the full path to the snapshot, chop off
3823 * everything except the short name of the snapshot, but special
3824 * case if they specify the origin.
3826 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
3827 char origin
[ZFS_MAXNAMELEN
];
3830 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
3831 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
3833 if (strcmp(origin
, fromname
) == 0) {
3835 flags
.fromorigin
= B_TRUE
;
3838 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
3839 (void) fprintf(stderr
,
3840 gettext("incremental source must be "
3841 "in same filesystem\n"));
3845 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
3846 (void) fprintf(stderr
,
3847 gettext("invalid incremental source\n"));
3853 if (flags
.replicate
&& fromname
== NULL
)
3854 flags
.doall
= B_TRUE
;
3856 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
3857 extraverbose
? &dbgnv
: NULL
);
3859 if (extraverbose
&& dbgnv
!= NULL
) {
3861 * dump_nvlist prints to stdout, but that's been
3862 * redirected to a file. Make it print to stderr
3865 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
3866 dump_nvlist(dbgnv
, 0);
3875 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3877 * Restore a backup stream from stdin.
3880 zfs_do_receive(int argc
, char **argv
)
3883 recvflags_t flags
= { 0 };
3886 while ((c
= getopt(argc
, argv
, ":denuvF")) != -1) {
3889 flags
.isprefix
= B_TRUE
;
3892 flags
.isprefix
= B_TRUE
;
3893 flags
.istail
= B_TRUE
;
3896 flags
.dryrun
= B_TRUE
;
3899 flags
.nomount
= B_TRUE
;
3902 flags
.verbose
= B_TRUE
;
3905 flags
.force
= B_TRUE
;
3908 (void) fprintf(stderr
, gettext("missing argument for "
3909 "'%c' option\n"), optopt
);
3913 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3922 /* check number of arguments */
3924 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3928 (void) fprintf(stderr
, gettext("too many arguments\n"));
3932 if (isatty(STDIN_FILENO
)) {
3933 (void) fprintf(stderr
,
3934 gettext("Error: Backup stream can not be read "
3935 "from a terminal.\n"
3936 "You must redirect standard input.\n"));
3940 err
= zfs_receive(g_zfs
, argv
[0], &flags
, STDIN_FILENO
, NULL
);
3946 * allow/unallow stuff
3948 /* copied from zfs/sys/dsl_deleg.h */
3949 #define ZFS_DELEG_PERM_CREATE "create"
3950 #define ZFS_DELEG_PERM_DESTROY "destroy"
3951 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3952 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3953 #define ZFS_DELEG_PERM_CLONE "clone"
3954 #define ZFS_DELEG_PERM_PROMOTE "promote"
3955 #define ZFS_DELEG_PERM_RENAME "rename"
3956 #define ZFS_DELEG_PERM_MOUNT "mount"
3957 #define ZFS_DELEG_PERM_SHARE "share"
3958 #define ZFS_DELEG_PERM_SEND "send"
3959 #define ZFS_DELEG_PERM_RECEIVE "receive"
3960 #define ZFS_DELEG_PERM_ALLOW "allow"
3961 #define ZFS_DELEG_PERM_USERPROP "userprop"
3962 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3963 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3964 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3965 #define ZFS_DELEG_PERM_USERUSED "userused"
3966 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3967 #define ZFS_DELEG_PERM_HOLD "hold"
3968 #define ZFS_DELEG_PERM_RELEASE "release"
3969 #define ZFS_DELEG_PERM_DIFF "diff"
3970 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
3972 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3974 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
3975 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
3976 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
3977 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
3978 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
3979 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
3980 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
3981 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
3982 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
3983 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
3984 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
3985 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
3986 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
3987 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
3988 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
3989 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
3990 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
3992 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
3993 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
3994 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
3995 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
3996 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
3997 { NULL
, ZFS_DELEG_NOTE_NONE
}
4000 /* permission structure */
4001 typedef struct deleg_perm
{
4002 zfs_deleg_who_type_t dp_who_type
;
4003 const char *dp_name
;
4005 boolean_t dp_descend
;
4009 typedef struct deleg_perm_node
{
4010 deleg_perm_t dpn_perm
;
4012 uu_avl_node_t dpn_avl_node
;
4013 } deleg_perm_node_t
;
4015 typedef struct fs_perm fs_perm_t
;
4017 /* permissions set */
4018 typedef struct who_perm
{
4019 zfs_deleg_who_type_t who_type
;
4020 const char *who_name
; /* id */
4021 char who_ug_name
[256]; /* user/group name */
4022 fs_perm_t
*who_fsperm
; /* uplink */
4024 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
4028 typedef struct who_perm_node
{
4029 who_perm_t who_perm
;
4030 uu_avl_node_t who_avl_node
;
4033 typedef struct fs_perm_set fs_perm_set_t
;
4034 /* fs permissions */
4036 const char *fsp_name
;
4038 uu_avl_t
*fsp_sc_avl
; /* sets,create */
4039 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
4041 fs_perm_set_t
*fsp_set
; /* uplink */
4045 typedef struct fs_perm_node
{
4046 fs_perm_t fspn_fsperm
;
4049 uu_list_node_t fspn_list_node
;
4052 /* top level structure */
4053 struct fs_perm_set
{
4054 uu_list_pool_t
*fsps_list_pool
;
4055 uu_list_t
*fsps_list
; /* list of fs_perms */
4057 uu_avl_pool_t
*fsps_named_set_avl_pool
;
4058 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
4059 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
4062 static inline const char *
4063 deleg_perm_type(zfs_deleg_note_t note
)
4069 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4070 case ZFS_DELEG_NOTE_GROUPUSED
:
4071 case ZFS_DELEG_NOTE_USERPROP
:
4072 case ZFS_DELEG_NOTE_USERQUOTA
:
4073 case ZFS_DELEG_NOTE_USERUSED
:
4075 return (gettext("other"));
4077 return (gettext("subcommand"));
4082 who_type2weight(zfs_deleg_who_type_t who_type
)
4086 case ZFS_DELEG_NAMED_SET_SETS
:
4087 case ZFS_DELEG_NAMED_SET
:
4090 case ZFS_DELEG_CREATE_SETS
:
4091 case ZFS_DELEG_CREATE
:
4094 case ZFS_DELEG_USER_SETS
:
4095 case ZFS_DELEG_USER
:
4098 case ZFS_DELEG_GROUP_SETS
:
4099 case ZFS_DELEG_GROUP
:
4102 case ZFS_DELEG_EVERYONE_SETS
:
4103 case ZFS_DELEG_EVERYONE
:
4115 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4117 const who_perm_node_t
*l
= larg
;
4118 const who_perm_node_t
*r
= rarg
;
4119 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
4120 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
4121 int lweight
= who_type2weight(ltype
);
4122 int rweight
= who_type2weight(rtype
);
4123 int res
= lweight
- rweight
;
4125 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
4126 ZFS_MAX_DELEG_NAME
-1);
4138 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4140 const deleg_perm_node_t
*l
= larg
;
4141 const deleg_perm_node_t
*r
= rarg
;
4142 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
4143 ZFS_MAX_DELEG_NAME
-1);
4155 fs_perm_set_init(fs_perm_set_t
*fspset
)
4157 bzero(fspset
, sizeof (fs_perm_set_t
));
4159 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4160 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4161 NULL
, UU_DEFAULT
)) == NULL
)
4163 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4164 UU_DEFAULT
)) == NULL
)
4167 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4168 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4169 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4170 UU_DEFAULT
)) == NULL
)
4173 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4174 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4175 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4176 UU_DEFAULT
)) == NULL
)
4179 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4180 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4181 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4186 static inline void fs_perm_fini(fs_perm_t
*);
4187 static inline void who_perm_fini(who_perm_t
*);
4190 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4192 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4194 while (node
!= NULL
) {
4195 fs_perm_node_t
*next_node
=
4196 uu_list_next(fspset
->fsps_list
, node
);
4197 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4198 fs_perm_fini(fsperm
);
4199 uu_list_remove(fspset
->fsps_list
, node
);
4204 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4205 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4206 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4210 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4213 deleg_perm
->dp_who_type
= type
;
4214 deleg_perm
->dp_name
= name
;
4218 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4219 zfs_deleg_who_type_t type
, const char *name
)
4221 uu_avl_pool_t
*pool
;
4222 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4224 bzero(who_perm
, sizeof (who_perm_t
));
4226 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4227 UU_DEFAULT
)) == NULL
)
4230 who_perm
->who_type
= type
;
4231 who_perm
->who_name
= name
;
4232 who_perm
->who_fsperm
= fsperm
;
4236 who_perm_fini(who_perm_t
*who_perm
)
4238 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4240 while (node
!= NULL
) {
4241 deleg_perm_node_t
*next_node
=
4242 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4244 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4249 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4253 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4255 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4256 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4258 bzero(fsperm
, sizeof (fs_perm_t
));
4260 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4264 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4268 fsperm
->fsp_set
= fspset
;
4269 fsperm
->fsp_name
= fsname
;
4273 fs_perm_fini(fs_perm_t
*fsperm
)
4275 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4276 while (node
!= NULL
) {
4277 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4279 who_perm_t
*who_perm
= &node
->who_perm
;
4280 who_perm_fini(who_perm
);
4281 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4286 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4287 while (node
!= NULL
) {
4288 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4290 who_perm_t
*who_perm
= &node
->who_perm
;
4291 who_perm_fini(who_perm
);
4292 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4297 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4298 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4302 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4303 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4305 uu_avl_index_t idx
= 0;
4307 deleg_perm_node_t
*found_node
= NULL
;
4308 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4310 deleg_perm_init(deleg_perm
, who_type
, name
);
4312 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4314 uu_avl_insert(avl
, node
, idx
);
4317 deleg_perm
= &node
->dpn_perm
;
4322 case ZFS_DELEG_LOCAL
:
4323 deleg_perm
->dp_local
= B_TRUE
;
4325 case ZFS_DELEG_DESCENDENT
:
4326 deleg_perm
->dp_descend
= B_TRUE
;
4331 assert(B_FALSE
); /* invalid locality */
4336 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4338 nvpair_t
*nvp
= NULL
;
4339 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4340 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4341 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4343 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4344 const char *name
= nvpair_name(nvp
);
4345 data_type_t type
= nvpair_type(nvp
);
4346 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4347 deleg_perm_node_t
*node
=
4348 safe_malloc(sizeof (deleg_perm_node_t
));
4350 VERIFY(type
== DATA_TYPE_BOOLEAN
);
4352 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4353 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4360 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4362 nvpair_t
*nvp
= NULL
;
4363 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4365 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4366 nvlist_t
*nvl2
= NULL
;
4367 const char *name
= nvpair_name(nvp
);
4368 uu_avl_t
*avl
= NULL
;
4369 uu_avl_pool_t
*avl_pool
= NULL
;
4370 zfs_deleg_who_type_t perm_type
= name
[0];
4371 char perm_locality
= name
[1];
4372 const char *perm_name
= name
+ 3;
4373 boolean_t is_set
= B_TRUE
;
4374 who_perm_t
*who_perm
= NULL
;
4376 assert('$' == name
[2]);
4378 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4381 switch (perm_type
) {
4382 case ZFS_DELEG_CREATE
:
4383 case ZFS_DELEG_CREATE_SETS
:
4384 case ZFS_DELEG_NAMED_SET
:
4385 case ZFS_DELEG_NAMED_SET_SETS
:
4386 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4387 avl
= fsperm
->fsp_sc_avl
;
4389 case ZFS_DELEG_USER
:
4390 case ZFS_DELEG_USER_SETS
:
4391 case ZFS_DELEG_GROUP
:
4392 case ZFS_DELEG_GROUP_SETS
:
4393 case ZFS_DELEG_EVERYONE
:
4394 case ZFS_DELEG_EVERYONE_SETS
:
4395 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4396 avl
= fsperm
->fsp_uge_avl
;
4403 who_perm_node_t
*found_node
= NULL
;
4404 who_perm_node_t
*node
= safe_malloc(
4405 sizeof (who_perm_node_t
));
4406 who_perm
= &node
->who_perm
;
4407 uu_avl_index_t idx
= 0;
4409 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4410 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4412 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4414 if (avl
== fsperm
->fsp_uge_avl
) {
4416 struct passwd
*p
= NULL
;
4417 struct group
*g
= NULL
;
4418 const char *nice_name
= NULL
;
4420 switch (perm_type
) {
4421 case ZFS_DELEG_USER_SETS
:
4422 case ZFS_DELEG_USER
:
4423 rid
= atoi(perm_name
);
4426 nice_name
= p
->pw_name
;
4428 case ZFS_DELEG_GROUP_SETS
:
4429 case ZFS_DELEG_GROUP
:
4430 rid
= atoi(perm_name
);
4433 nice_name
= g
->gr_name
;
4439 if (nice_name
!= NULL
)
4441 node
->who_perm
.who_ug_name
,
4445 uu_avl_insert(avl
, node
, idx
);
4448 who_perm
= &node
->who_perm
;
4452 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4459 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4461 nvpair_t
*nvp
= NULL
;
4462 uu_avl_index_t idx
= 0;
4464 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4465 nvlist_t
*nvl2
= NULL
;
4466 const char *fsname
= nvpair_name(nvp
);
4467 data_type_t type
= nvpair_type(nvp
);
4468 fs_perm_t
*fsperm
= NULL
;
4469 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4473 fsperm
= &node
->fspn_fsperm
;
4475 VERIFY(DATA_TYPE_NVLIST
== type
);
4477 uu_list_node_init(node
, &node
->fspn_list_node
,
4478 fspset
->fsps_list_pool
);
4480 idx
= uu_list_numnodes(fspset
->fsps_list
);
4481 fs_perm_init(fsperm
, fspset
, fsname
);
4483 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4486 (void) parse_fs_perm(fsperm
, nvl2
);
4488 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4494 static inline const char *
4495 deleg_perm_comment(zfs_deleg_note_t note
)
4497 const char *str
= "";
4502 case ZFS_DELEG_NOTE_ALLOW
:
4503 str
= gettext("Must also have the permission that is being"
4504 "\n\t\t\t\tallowed");
4506 case ZFS_DELEG_NOTE_CLONE
:
4507 str
= gettext("Must also have the 'create' ability and 'mount'"
4508 "\n\t\t\t\tability in the origin file system");
4510 case ZFS_DELEG_NOTE_CREATE
:
4511 str
= gettext("Must also have the 'mount' ability");
4513 case ZFS_DELEG_NOTE_DESTROY
:
4514 str
= gettext("Must also have the 'mount' ability");
4516 case ZFS_DELEG_NOTE_DIFF
:
4517 str
= gettext("Allows lookup of paths within a dataset;"
4518 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4519 "\n\t\t\t\tin order to use zfs diff");
4521 case ZFS_DELEG_NOTE_HOLD
:
4522 str
= gettext("Allows adding a user hold to a snapshot");
4524 case ZFS_DELEG_NOTE_MOUNT
:
4525 str
= gettext("Allows mount/umount of ZFS datasets");
4527 case ZFS_DELEG_NOTE_PROMOTE
:
4528 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4529 " 'promote' ability in the origin file system");
4531 case ZFS_DELEG_NOTE_RECEIVE
:
4532 str
= gettext("Must also have the 'mount' and 'create'"
4535 case ZFS_DELEG_NOTE_RELEASE
:
4536 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4537 "might destroy the snapshot");
4539 case ZFS_DELEG_NOTE_RENAME
:
4540 str
= gettext("Must also have the 'mount' and 'create'"
4541 "\n\t\t\t\tability in the new parent");
4543 case ZFS_DELEG_NOTE_ROLLBACK
:
4546 case ZFS_DELEG_NOTE_SEND
:
4549 case ZFS_DELEG_NOTE_SHARE
:
4550 str
= gettext("Allows sharing file systems over NFS or SMB"
4551 "\n\t\t\t\tprotocols");
4553 case ZFS_DELEG_NOTE_SNAPSHOT
:
4557 * case ZFS_DELEG_NOTE_VSCAN:
4558 * str = gettext("");
4562 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4563 str
= gettext("Allows accessing any groupquota@... property");
4565 case ZFS_DELEG_NOTE_GROUPUSED
:
4566 str
= gettext("Allows reading any groupused@... property");
4568 case ZFS_DELEG_NOTE_USERPROP
:
4569 str
= gettext("Allows changing any user property");
4571 case ZFS_DELEG_NOTE_USERQUOTA
:
4572 str
= gettext("Allows accessing any userquota@... property");
4574 case ZFS_DELEG_NOTE_USERUSED
:
4575 str
= gettext("Allows reading any userused@... property");
4593 boolean_t recursive
; /* unallow only */
4594 boolean_t prt_usage
;
4596 boolean_t prt_perms
;
4599 const char *dataset
;
4603 prop_cmp(const void *a
, const void *b
)
4605 const char *str1
= *(const char **)a
;
4606 const char *str2
= *(const char **)b
;
4607 return (strcmp(str1
, str2
));
4611 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
4613 const char *opt_desc
[] = {
4614 "-h", gettext("show this help message and exit"),
4615 "-l", gettext("set permission locally"),
4616 "-d", gettext("set permission for descents"),
4617 "-u", gettext("set permission for user"),
4618 "-g", gettext("set permission for group"),
4619 "-e", gettext("set permission for everyone"),
4620 "-c", gettext("set create time permission"),
4621 "-s", gettext("define permission set"),
4623 "-r", gettext("remove permissions recursively"),
4625 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
4626 size_t allow_size
= unallow_size
- 2;
4627 const char *props
[ZFS_NUM_PROPS
];
4630 FILE *fp
= requested
? stdout
: stderr
;
4631 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
4632 const char *fmt
= gettext("%-16s %-14s\t%s\n");
4634 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
4636 (void) fprintf(fp
, gettext("Options:\n"));
4637 for (i
= 0; i
< (un
? unallow_size
: allow_size
); i
++) {
4638 const char *opt
= opt_desc
[i
++];
4639 const char *optdsc
= opt_desc
[i
];
4640 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
4643 (void) fprintf(fp
, gettext("\nThe following permissions are "
4645 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
4647 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
4648 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
4649 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
4650 const char *perm_type
= deleg_perm_type(perm_note
);
4651 const char *perm_comment
= deleg_perm_comment(perm_note
);
4652 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
4655 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
4656 zprop_desc_t
*pd
= &pdtbl
[i
];
4657 if (pd
->pd_visible
!= B_TRUE
)
4660 if (pd
->pd_attr
== PROP_READONLY
)
4663 props
[count
++] = pd
->pd_name
;
4665 props
[count
] = NULL
;
4667 qsort(props
, count
, sizeof (char *), prop_cmp
);
4669 for (i
= 0; i
< count
; i
++)
4670 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
4673 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
4675 exit(requested
? 0 : 2);
4678 static inline const char *
4679 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
4682 if (un
&& argc
== expected_argc
- 1)
4684 else if (argc
== expected_argc
)
4685 *permsp
= argv
[argc
- 2];
4687 allow_usage(un
, B_FALSE
,
4688 gettext("wrong number of parameters\n"));
4690 return (argv
[argc
- 1]);
4694 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
4696 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
4697 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
4698 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
4699 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
4702 allow_usage(un
, B_FALSE
,
4703 gettext("-u, -g, and -e are mutually exclusive\n"));
4705 if (opts
->prt_usage
) {
4706 if (argc
== 0 && all_sum
== 0)
4707 allow_usage(un
, B_TRUE
, NULL
);
4714 allow_usage(un
, B_FALSE
,
4715 gettext("invalid options combined with -s\n"));
4717 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4718 if (argv
[0][0] != '@')
4719 allow_usage(un
, B_FALSE
,
4720 gettext("invalid set name: missing '@' prefix\n"));
4721 opts
->who
= argv
[0];
4722 } else if (opts
->create
) {
4723 if (ldcsuge_sum
> 1)
4724 allow_usage(un
, B_FALSE
,
4725 gettext("invalid options combined with -c\n"));
4726 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4727 } else if (opts
->everyone
) {
4729 allow_usage(un
, B_FALSE
,
4730 gettext("invalid options combined with -e\n"));
4731 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4732 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
4734 opts
->everyone
= B_TRUE
;
4737 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4738 } else if (argc
== 1 && !un
) {
4739 opts
->prt_perms
= B_TRUE
;
4740 opts
->dataset
= argv
[argc
-1];
4742 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4743 opts
->who
= argv
[0];
4746 if (!opts
->local
&& !opts
->descend
) {
4747 opts
->local
= B_TRUE
;
4748 opts
->descend
= B_TRUE
;
4753 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
4754 const char *who
, char *perms
, nvlist_t
*top_nvl
)
4757 char ld
[2] = { '\0', '\0' };
4758 char who_buf
[ZFS_MAXNAMELEN
+32];
4759 char base_type
= ZFS_DELEG_WHO_UNKNOWN
;
4760 char set_type
= ZFS_DELEG_WHO_UNKNOWN
;
4761 nvlist_t
*base_nvl
= NULL
;
4762 nvlist_t
*set_nvl
= NULL
;
4765 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4767 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4771 case ZFS_DELEG_NAMED_SET_SETS
:
4772 case ZFS_DELEG_NAMED_SET
:
4773 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
4774 base_type
= ZFS_DELEG_NAMED_SET
;
4775 ld
[0] = ZFS_DELEG_NA
;
4777 case ZFS_DELEG_CREATE_SETS
:
4778 case ZFS_DELEG_CREATE
:
4779 set_type
= ZFS_DELEG_CREATE_SETS
;
4780 base_type
= ZFS_DELEG_CREATE
;
4781 ld
[0] = ZFS_DELEG_NA
;
4783 case ZFS_DELEG_USER_SETS
:
4784 case ZFS_DELEG_USER
:
4785 set_type
= ZFS_DELEG_USER_SETS
;
4786 base_type
= ZFS_DELEG_USER
;
4788 ld
[0] = ZFS_DELEG_LOCAL
;
4790 ld
[1] = ZFS_DELEG_DESCENDENT
;
4792 case ZFS_DELEG_GROUP_SETS
:
4793 case ZFS_DELEG_GROUP
:
4794 set_type
= ZFS_DELEG_GROUP_SETS
;
4795 base_type
= ZFS_DELEG_GROUP
;
4797 ld
[0] = ZFS_DELEG_LOCAL
;
4799 ld
[1] = ZFS_DELEG_DESCENDENT
;
4801 case ZFS_DELEG_EVERYONE_SETS
:
4802 case ZFS_DELEG_EVERYONE
:
4803 set_type
= ZFS_DELEG_EVERYONE_SETS
;
4804 base_type
= ZFS_DELEG_EVERYONE
;
4806 ld
[0] = ZFS_DELEG_LOCAL
;
4808 ld
[1] = ZFS_DELEG_DESCENDENT
;
4813 if (perms
!= NULL
) {
4815 char *end
= curr
+ strlen(perms
);
4817 while (curr
< end
) {
4818 char *delim
= strchr(curr
, ',');
4829 (void) nvlist_add_boolean(nvl
, curr
);
4835 for (i
= 0; i
< 2; i
++) {
4836 char locality
= ld
[i
];
4840 if (!nvlist_empty(base_nvl
)) {
4842 (void) snprintf(who_buf
,
4843 sizeof (who_buf
), "%c%c$%s",
4844 base_type
, locality
, who
);
4846 (void) snprintf(who_buf
,
4847 sizeof (who_buf
), "%c%c$",
4848 base_type
, locality
);
4850 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
4855 if (!nvlist_empty(set_nvl
)) {
4857 (void) snprintf(who_buf
,
4858 sizeof (who_buf
), "%c%c$%s",
4859 set_type
, locality
, who
);
4861 (void) snprintf(who_buf
,
4862 sizeof (who_buf
), "%c%c$",
4863 set_type
, locality
);
4865 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
4870 for (i
= 0; i
< 2; i
++) {
4871 char locality
= ld
[i
];
4876 (void) snprintf(who_buf
, sizeof (who_buf
),
4877 "%c%c$%s", base_type
, locality
, who
);
4879 (void) snprintf(who_buf
, sizeof (who_buf
),
4880 "%c%c$", base_type
, locality
);
4881 (void) nvlist_add_boolean(top_nvl
, who_buf
);
4884 (void) snprintf(who_buf
, sizeof (who_buf
),
4885 "%c%c$%s", set_type
, locality
, who
);
4887 (void) snprintf(who_buf
, sizeof (who_buf
),
4888 "%c%c$", set_type
, locality
);
4889 (void) nvlist_add_boolean(top_nvl
, who_buf
);
4895 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
4897 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
4901 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
4902 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
4903 } else if (opts
->create
) {
4904 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
4905 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
4906 } else if (opts
->everyone
) {
4907 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
4908 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
4910 char *curr
= opts
->who
;
4911 char *end
= curr
+ strlen(curr
);
4913 while (curr
< end
) {
4915 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
4917 char *delim
= strchr(curr
, ',');
4920 struct passwd
*p
= NULL
;
4921 struct group
*g
= NULL
;
4929 rid
= (uid_t
)strtol(curr
, &endch
, 0);
4931 who_type
= ZFS_DELEG_USER
;
4940 (void) snprintf(errbuf
, 256, gettext(
4941 "invalid user %s"), curr
);
4942 allow_usage(un
, B_TRUE
, errbuf
);
4944 } else if (opts
->group
) {
4945 who_type
= ZFS_DELEG_GROUP
;
4954 (void) snprintf(errbuf
, 256, gettext(
4955 "invalid group %s"), curr
);
4956 allow_usage(un
, B_TRUE
, errbuf
);
4959 if (*endch
!= '\0') {
4966 if (*endch
!= '\0') {
4974 who_type
= ZFS_DELEG_USER
;
4976 } else if (g
!= NULL
) {
4977 who_type
= ZFS_DELEG_GROUP
;
4980 (void) snprintf(errbuf
, 256, gettext(
4981 "invalid user/group %s"), curr
);
4982 allow_usage(un
, B_TRUE
, errbuf
);
4986 (void) sprintf(id
, "%u", rid
);
4989 store_allow_perm(who_type
, opts
->local
,
4990 opts
->descend
, who
, opts
->perms
, *nvlp
);
4999 print_set_creat_perms(uu_avl_t
*who_avl
)
5001 const char *sc_title
[] = {
5002 gettext("Permission sets:\n"),
5003 gettext("Create time permissions:\n"),
5006 const char **title_ptr
= sc_title
;
5007 who_perm_node_t
*who_node
= NULL
;
5008 int prev_weight
= -1;
5010 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
5011 who_node
= uu_avl_next(who_avl
, who_node
)) {
5012 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5013 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5014 const char *who_name
= who_node
->who_perm
.who_name
;
5015 int weight
= who_type2weight(who_type
);
5016 boolean_t first
= B_TRUE
;
5017 deleg_perm_node_t
*deleg_node
;
5019 if (prev_weight
!= weight
) {
5020 (void) printf("%s", *title_ptr
++);
5021 prev_weight
= weight
;
5024 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
5025 (void) printf("\t");
5027 (void) printf("\t%s ", who_name
);
5029 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
5030 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5033 deleg_node
->dpn_perm
.dp_name
);
5036 (void) printf(",%s",
5037 deleg_node
->dpn_perm
.dp_name
);
5040 (void) printf("\n");
5045 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
5048 who_perm_node_t
*who_node
= NULL
;
5049 boolean_t prt_title
= B_TRUE
;
5050 uu_avl_walk_t
*walk
;
5052 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
5055 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
5056 const char *who_name
= who_node
->who_perm
.who_name
;
5057 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
5058 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5059 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5061 deleg_perm_node_t
*deleg_node
;
5062 boolean_t prt_who
= B_TRUE
;
5064 for (deleg_node
= uu_avl_first(avl
);
5066 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5067 if (local
!= deleg_node
->dpn_perm
.dp_local
||
5068 descend
!= deleg_node
->dpn_perm
.dp_descend
)
5072 const char *who
= NULL
;
5074 prt_title
= B_FALSE
;
5075 (void) printf("%s", title
);
5079 case ZFS_DELEG_USER_SETS
:
5080 case ZFS_DELEG_USER
:
5081 who
= gettext("user");
5083 who_name
= nice_who_name
;
5085 case ZFS_DELEG_GROUP_SETS
:
5086 case ZFS_DELEG_GROUP
:
5087 who
= gettext("group");
5089 who_name
= nice_who_name
;
5091 case ZFS_DELEG_EVERYONE_SETS
:
5092 case ZFS_DELEG_EVERYONE
:
5093 who
= gettext("everyone");
5100 if (who_name
== NULL
)
5101 (void) printf("\t%s", who
);
5103 (void) printf("\t%s %s", who
, who_name
);
5106 (void) printf("%c%s", delim
,
5107 deleg_node
->dpn_perm
.dp_name
);
5112 (void) printf("\n");
5115 uu_avl_walk_end(walk
);
5119 print_fs_perms(fs_perm_set_t
*fspset
)
5121 fs_perm_node_t
*node
= NULL
;
5122 char buf
[ZFS_MAXNAMELEN
+32];
5123 const char *dsname
= buf
;
5125 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
5126 node
= uu_list_next(fspset
->fsps_list
, node
)) {
5127 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
5128 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
5131 (void) snprintf(buf
, ZFS_MAXNAMELEN
+32,
5132 gettext("---- Permissions on %s "),
5133 node
->fspn_fsperm
.fsp_name
);
5134 (void) printf("%s", dsname
);
5135 left
= 70 - strlen(buf
);
5138 (void) printf("\n");
5140 print_set_creat_perms(sc_avl
);
5141 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
5142 gettext("Local permissions:\n"));
5143 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
5144 gettext("Descendent permissions:\n"));
5145 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
5146 gettext("Local+Descendent permissions:\n"));
5150 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5152 struct deleg_perms
{
5158 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5160 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5161 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5163 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5166 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5170 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5173 nvlist_t
*perm_nvl
= NULL
;
5174 nvlist_t
*update_perm_nvl
= NULL
;
5177 struct allow_opts opts
= { 0 };
5179 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5182 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5185 opts
.local
= B_TRUE
;
5188 opts
.descend
= B_TRUE
;
5194 opts
.group
= B_TRUE
;
5197 opts
.everyone
= B_TRUE
;
5203 opts
.create
= B_TRUE
;
5206 opts
.recursive
= B_TRUE
;
5209 (void) fprintf(stderr
, gettext("missing argument for "
5210 "'%c' option\n"), optopt
);
5214 opts
.prt_usage
= B_TRUE
;
5217 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5226 /* check arguments */
5227 parse_allow_args(argc
, argv
, un
, &opts
);
5229 /* try to open the dataset */
5230 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5231 ZFS_TYPE_VOLUME
)) == NULL
) {
5232 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5237 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5240 fs_perm_set_init(&fs_perm_set
);
5241 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5242 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5247 print_fs_perms(&fs_perm_set
);
5249 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5250 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5253 if (un
&& opts
.recursive
) {
5254 struct deleg_perms data
= { un
, update_perm_nvl
};
5255 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5264 nvlist_free(perm_nvl
);
5265 if (update_perm_nvl
!= NULL
)
5266 nvlist_free(update_perm_nvl
);
5268 fs_perm_set_fini(&fs_perm_set
);
5276 zfs_do_allow(int argc
, char **argv
)
5278 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5282 zfs_do_unallow(int argc
, char **argv
)
5284 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5288 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5293 boolean_t recursive
= B_FALSE
;
5294 const char *opts
= holding
? "rt" : "r";
5298 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5304 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5313 /* check number of arguments */
5321 if (holding
&& tag
[0] == '.') {
5322 /* tags starting with '.' are reserved for libzfs */
5323 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5327 for (i
= 0; i
< argc
; ++i
) {
5329 char parent
[ZFS_MAXNAMELEN
];
5331 char *path
= argv
[i
];
5333 delim
= strchr(path
, '@');
5334 if (delim
== NULL
) {
5335 (void) fprintf(stderr
,
5336 gettext("'%s' is not a snapshot\n"), path
);
5340 (void) strncpy(parent
, path
, delim
- path
);
5341 parent
[delim
- path
] = '\0';
5343 zhp
= zfs_open(g_zfs
, parent
,
5344 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5350 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
5353 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5359 return (errors
!= 0);
5363 * zfs hold [-r] [-t] <tag> <snap> ...
5365 * -r Recursively hold
5367 * Apply a user-hold with the given tag to the list of snapshots.
5370 zfs_do_hold(int argc
, char **argv
)
5372 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5376 * zfs release [-r] <tag> <snap> ...
5378 * -r Recursively release
5380 * Release a user-hold with the given tag from the list of snapshots.
5383 zfs_do_release(int argc
, char **argv
)
5385 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5388 typedef struct holds_cbdata
{
5389 boolean_t cb_recursive
;
5390 const char *cb_snapname
;
5392 size_t cb_max_namelen
;
5393 size_t cb_max_taglen
;
5396 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5397 #define DATETIME_BUF_LEN (32)
5402 print_holds(boolean_t scripted
, int nwidth
, int tagwidth
, nvlist_t
*nvl
)
5405 nvpair_t
*nvp
= NULL
;
5406 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5410 for (i
= 0; i
< 3; i
++) {
5411 col
= gettext(hdr_cols
[i
]);
5413 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5416 (void) printf("%s\n", col
);
5420 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5421 char *zname
= nvpair_name(nvp
);
5423 nvpair_t
*nvp2
= NULL
;
5424 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5425 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5426 char tsbuf
[DATETIME_BUF_LEN
];
5427 char *tagname
= nvpair_name(nvp2
);
5431 char sep
= scripted
? '\t' : ' ';
5432 int sepnum
= scripted
? 1 : 2;
5434 (void) nvpair_value_uint64(nvp2
, &val
);
5436 (void) localtime_r(&time
, &t
);
5437 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5438 gettext(STRFTIME_FMT_STR
), &t
);
5440 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth
, zname
,
5441 sepnum
, sep
, tagwidth
, tagname
, sepnum
, sep
, tsbuf
);
5447 * Generic callback function to list a dataset or snapshot.
5450 holds_callback(zfs_handle_t
*zhp
, void *data
)
5452 holds_cbdata_t
*cbp
= data
;
5453 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5454 nvlist_t
*nvl
= NULL
;
5455 nvpair_t
*nvp
= NULL
;
5456 const char *zname
= zfs_get_name(zhp
);
5457 size_t znamelen
= strnlen(zname
, ZFS_MAXNAMELEN
);
5459 if (cbp
->cb_recursive
) {
5460 const char *snapname
;
5461 char *delim
= strchr(zname
, '@');
5465 snapname
= delim
+ 1;
5466 if (strcmp(cbp
->cb_snapname
, snapname
))
5470 if (zfs_get_holds(zhp
, &nvl
) != 0)
5473 if (znamelen
> cbp
->cb_max_namelen
)
5474 cbp
->cb_max_namelen
= znamelen
;
5476 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5477 const char *tag
= nvpair_name(nvp
);
5478 size_t taglen
= strnlen(tag
, MAXNAMELEN
);
5479 if (taglen
> cbp
->cb_max_taglen
)
5480 cbp
->cb_max_taglen
= taglen
;
5483 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5487 * zfs holds [-r] <snap> ...
5489 * -r Recursively hold
5492 zfs_do_holds(int argc
, char **argv
)
5497 boolean_t scripted
= B_FALSE
;
5498 boolean_t recursive
= B_FALSE
;
5499 const char *opts
= "rH";
5502 int types
= ZFS_TYPE_SNAPSHOT
;
5503 holds_cbdata_t cb
= { 0 };
5510 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5519 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5526 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
5527 flags
|= ZFS_ITER_RECURSE
;
5533 /* check number of arguments */
5537 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5540 for (i
= 0; i
< argc
; ++i
) {
5541 char *snapshot
= argv
[i
];
5543 const char *snapname
;
5545 delim
= strchr(snapshot
, '@');
5546 if (delim
== NULL
) {
5547 (void) fprintf(stderr
,
5548 gettext("'%s' is not a snapshot\n"), snapshot
);
5552 snapname
= delim
+ 1;
5554 snapshot
[delim
- snapshot
] = '\0';
5556 cb
.cb_recursive
= recursive
;
5557 cb
.cb_snapname
= snapname
;
5561 * 1. collect holds data, set format options
5563 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
5564 holds_callback
, &cb
);
5570 * 2. print holds data
5572 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
5574 if (nvlist_empty(nvl
))
5575 (void) fprintf(stderr
, gettext("no datasets available\n"));
5579 return (0 != errors
);
5582 #define CHECK_SPINNER 30
5583 #define SPINNER_TIME 3 /* seconds */
5584 #define MOUNT_TIME 5 /* seconds */
5587 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
5589 static char *spin
[] = { "-", "\\", "|", "/" };
5590 static int spinval
= 0;
5591 static int spincheck
= 0;
5592 static time_t last_spin_time
= (time_t)0;
5593 get_all_cb_t
*cbp
= data
;
5594 zfs_type_t type
= zfs_get_type(zhp
);
5596 if (cbp
->cb_verbose
) {
5597 if (--spincheck
< 0) {
5598 time_t now
= time(NULL
);
5599 if (last_spin_time
+ SPINNER_TIME
< now
) {
5600 update_progress(spin
[spinval
++ % 4]);
5601 last_spin_time
= now
;
5603 spincheck
= CHECK_SPINNER
;
5608 * Iterate over any nested datasets.
5610 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
5616 * Skip any datasets whose type does not match.
5618 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
5622 libzfs_add_handle(cbp
, zhp
);
5623 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
5629 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
5631 get_all_cb_t cb
= { 0 };
5632 cb
.cb_verbose
= verbose
;
5633 cb
.cb_getone
= get_one_dataset
;
5636 set_progress_header(gettext("Reading ZFS config"));
5637 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
5639 *dslist
= cb
.cb_handles
;
5640 *count
= cb
.cb_used
;
5643 finish_progress(gettext("done."));
5647 * Generic callback for sharing or mounting filesystems. Because the code is so
5648 * similar, we have a common function with an extra parameter to determine which
5649 * mode we are using.
5651 #define OP_SHARE 0x1
5652 #define OP_MOUNT 0x2
5655 * Share or mount a dataset.
5658 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
5659 boolean_t
explicit, const char *options
)
5661 char mountpoint
[ZFS_MAXPROPLEN
];
5662 char shareopts
[ZFS_MAXPROPLEN
];
5663 char smbshareopts
[ZFS_MAXPROPLEN
];
5664 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
5666 uint64_t zoned
, canmount
;
5667 boolean_t shared_nfs
, shared_smb
;
5669 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
5672 * Check to make sure we can mount/share this dataset. If we
5673 * are in the global zone and the filesystem is exported to a
5674 * local zone, or if we are in a local zone and the
5675 * filesystem is not exported, then it is an error.
5677 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
5679 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
5683 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5684 "dataset is exported to a local zone\n"), cmdname
,
5688 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
5692 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5693 "permission denied\n"), cmdname
,
5699 * Ignore any filesystems which don't apply to us. This
5700 * includes those with a legacy mountpoint, or those with
5701 * legacy share options.
5703 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
5704 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
5705 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
5706 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5707 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
5708 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5710 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
5711 strcmp(smbshareopts
, "off") == 0) {
5715 (void) fprintf(stderr
, gettext("cannot share '%s': "
5716 "legacy share\n"), zfs_get_name(zhp
));
5717 (void) fprintf(stderr
, gettext("use share(1M) to "
5718 "share this filesystem, or set "
5719 "sharenfs property on\n"));
5724 * We cannot share or mount legacy filesystems. If the
5725 * shareopts is non-legacy but the mountpoint is legacy, we
5726 * treat it as a legacy share.
5728 if (strcmp(mountpoint
, "legacy") == 0) {
5732 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5733 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
5734 (void) fprintf(stderr
, gettext("use %s(1M) to "
5735 "%s this filesystem\n"), cmdname
, cmdname
);
5739 if (strcmp(mountpoint
, "none") == 0) {
5743 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
5744 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
5749 * canmount explicit outcome
5750 * on no pass through
5751 * on yes pass through
5753 * off yes display error, return 1
5754 * noauto no return 0
5755 * noauto yes pass through
5757 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
5758 if (canmount
== ZFS_CANMOUNT_OFF
) {
5762 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5763 "'canmount' property is set to 'off'\n"), cmdname
,
5766 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
5771 * At this point, we have verified that the mountpoint and/or
5772 * shareopts are appropriate for auto management. If the
5773 * filesystem is already mounted or shared, return (failing
5774 * for explicit requests); otherwise mount or share the
5780 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
5781 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
5783 if ((shared_nfs
&& shared_smb
) ||
5784 ((shared_nfs
&& strcmp(shareopts
, "on") == 0) &&
5785 (strcmp(smbshareopts
, "off") == 0)) ||
5786 ((shared_smb
&& strcmp(smbshareopts
, "on") == 0) &&
5787 (strcmp(shareopts
, "off") == 0))) {
5791 (void) fprintf(stderr
, gettext("cannot share "
5792 "'%s': filesystem already shared\n"),
5797 if (!zfs_is_mounted(zhp
, NULL
) &&
5798 zfs_mount(zhp
, NULL
, 0) != 0)
5801 if (protocol
== NULL
) {
5802 if (zfs_shareall(zhp
) != 0)
5804 } else if (strcmp(protocol
, "nfs") == 0) {
5805 if (zfs_share_nfs(zhp
))
5807 } else if (strcmp(protocol
, "smb") == 0) {
5808 if (zfs_share_smb(zhp
))
5811 (void) fprintf(stderr
, gettext("cannot share "
5812 "'%s': invalid share type '%s' "
5814 zfs_get_name(zhp
), protocol
);
5821 if (options
== NULL
)
5822 mnt
.mnt_mntopts
= "";
5824 mnt
.mnt_mntopts
= (char *)options
;
5826 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
5827 zfs_is_mounted(zhp
, NULL
)) {
5831 (void) fprintf(stderr
, gettext("cannot mount "
5832 "'%s': filesystem already mounted\n"),
5837 if (zfs_mount(zhp
, options
, flags
) != 0)
5846 * Reports progress in the form "(current/total)". Not thread-safe.
5849 report_mount_progress(int current
, int total
)
5851 static time_t last_progress_time
= 0;
5852 time_t now
= time(NULL
);
5855 /* report 1..n instead of 0..n-1 */
5858 /* display header if we're here for the first time */
5860 set_progress_header(gettext("Mounting ZFS filesystems"));
5861 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
5862 /* too soon to report again */
5866 last_progress_time
= now
;
5868 (void) sprintf(info
, "(%d/%d)", current
, total
);
5870 if (current
== total
)
5871 finish_progress(info
);
5873 update_progress(info
);
5877 append_options(char *mntopts
, char *newopts
)
5879 int len
= strlen(mntopts
);
5881 /* original length plus new string to append plus 1 for the comma */
5882 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
5883 (void) fprintf(stderr
, gettext("the opts argument for "
5884 "'%s' option is too long (more than %d chars)\n"),
5885 "-o", MNT_LINE_MAX
);
5890 mntopts
[len
++] = ',';
5892 (void) strcpy(&mntopts
[len
], newopts
);
5896 share_mount(int op
, int argc
, char **argv
)
5899 boolean_t verbose
= B_FALSE
;
5901 char *options
= NULL
;
5905 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
5915 if (*optarg
== '\0') {
5916 (void) fprintf(stderr
, gettext("empty mount "
5917 "options (-o) specified\n"));
5921 if (options
== NULL
)
5922 options
= safe_malloc(MNT_LINE_MAX
+ 1);
5924 /* option validation is done later */
5925 append_options(options
, optarg
);
5928 flags
|= MS_OVERLAY
;
5931 (void) fprintf(stderr
, gettext("missing argument for "
5932 "'%c' option\n"), optopt
);
5936 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5945 /* check number of arguments */
5947 zfs_handle_t
**dslist
= NULL
;
5948 size_t i
, count
= 0;
5949 char *protocol
= NULL
;
5951 if (op
== OP_SHARE
&& argc
> 0) {
5952 if (strcmp(argv
[0], "nfs") != 0 &&
5953 strcmp(argv
[0], "smb") != 0) {
5954 (void) fprintf(stderr
, gettext("share type "
5955 "must be 'nfs' or 'smb'\n"));
5964 (void) fprintf(stderr
, gettext("too many arguments\n"));
5968 start_progress_timer();
5969 get_all_datasets(&dslist
, &count
, verbose
);
5974 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
5976 for (i
= 0; i
< count
; i
++) {
5978 report_mount_progress(i
, count
);
5980 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
5981 B_FALSE
, options
) != 0)
5983 zfs_close(dslist
[i
]);
5987 } else if (argc
== 0) {
5988 struct mnttab entry
;
5990 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
5991 (void) fprintf(stderr
, gettext("missing filesystem "
5992 "argument (specify -a for all)\n"));
5997 * When mount is given no arguments, go through /etc/mtab and
5998 * display any active ZFS mounts. We hide any snapshots, since
5999 * they are controlled automatically.
6002 /* Reopen MNTTAB to prevent reading stale data from open file */
6003 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6006 while (getmntent(mnttab_file
, &entry
) == 0) {
6007 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
6008 strchr(entry
.mnt_special
, '@') != NULL
)
6011 (void) printf("%-30s %s\n", entry
.mnt_special
,
6019 (void) fprintf(stderr
,
6020 gettext("too many arguments\n"));
6024 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6025 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6028 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
6038 * zfs mount -a [nfs]
6039 * zfs mount filesystem
6041 * Mount all filesystems, or mount the given filesystem.
6044 zfs_do_mount(int argc
, char **argv
)
6046 return (share_mount(OP_MOUNT
, argc
, argv
));
6050 * zfs share -a [nfs | smb]
6051 * zfs share filesystem
6053 * Share all filesystems, or share the given filesystem.
6056 zfs_do_share(int argc
, char **argv
)
6058 return (share_mount(OP_SHARE
, argc
, argv
));
6061 typedef struct unshare_unmount_node
{
6062 zfs_handle_t
*un_zhp
;
6064 uu_avl_node_t un_avlnode
;
6065 } unshare_unmount_node_t
;
6069 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
6071 const unshare_unmount_node_t
*l
= larg
;
6072 const unshare_unmount_node_t
*r
= rarg
;
6074 return (strcmp(l
->un_mountp
, r
->un_mountp
));
6078 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6079 * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
6080 * and unmount it appropriately.
6083 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
6087 struct stat64 statbuf
;
6088 struct extmnttab entry
;
6089 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
6093 * Search for the path in /etc/mtab. Rather than looking for the
6094 * specific path, which can be fooled by non-standard paths (i.e. ".."
6095 * or "//"), we stat() the path and search for the corresponding
6096 * (major,minor) device pair.
6098 if (stat64(path
, &statbuf
) != 0) {
6099 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6100 cmdname
, path
, strerror(errno
));
6103 path_inode
= statbuf
.st_ino
;
6106 * Search for the given (major,minor) pair in the mount table.
6109 /* Reopen MNTTAB to prevent reading stale data from open file */
6110 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6113 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
6114 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
6115 entry
.mnt_minor
== minor(statbuf
.st_dev
))
6119 if (op
== OP_SHARE
) {
6120 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
6121 "currently mounted\n"), cmdname
, path
);
6124 (void) fprintf(stderr
, gettext("warning: %s not in mtab\n"),
6126 if ((ret
= umount2(path
, flags
)) != 0)
6127 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
6132 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
6133 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
6134 "filesystem\n"), cmdname
, path
);
6138 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6139 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6143 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
6144 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6145 cmdname
, path
, strerror(errno
));
6147 } else if (statbuf
.st_ino
!= path_inode
) {
6148 (void) fprintf(stderr
, gettext("cannot "
6149 "%s '%s': not a mountpoint\n"), cmdname
, path
);
6153 if (op
== OP_SHARE
) {
6154 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6155 char smbshare_prop
[ZFS_MAXPROPLEN
];
6157 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
6158 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6159 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
6160 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6162 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6163 strcmp(smbshare_prop
, "off") == 0) {
6164 (void) fprintf(stderr
, gettext("cannot unshare "
6165 "'%s': legacy share\n"), path
);
6166 (void) fprintf(stderr
, gettext("use exportfs(8) "
6167 "or smbcontrol(1) to unshare this filesystem\n"));
6168 } else if (!zfs_is_shared(zhp
)) {
6169 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6170 "not currently shared\n"), path
);
6172 ret
= zfs_unshareall_bypath(zhp
, path
);
6175 char mtpt_prop
[ZFS_MAXPROPLEN
];
6177 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6178 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6181 ret
= zfs_unmount(zhp
, NULL
, flags
);
6182 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6183 (void) fprintf(stderr
, gettext("cannot unmount "
6184 "'%s': legacy mountpoint\n"),
6186 (void) fprintf(stderr
, gettext("use umount(8) "
6187 "to unmount this filesystem\n"));
6189 ret
= zfs_unmountall(zhp
, flags
);
6200 * Generic callback for unsharing or unmounting a filesystem.
6203 unshare_unmount(int op
, int argc
, char **argv
)
6210 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6211 char sharesmb
[ZFS_MAXPROPLEN
];
6214 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
6223 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6234 * We could make use of zfs_for_each() to walk all datasets in
6235 * the system, but this would be very inefficient, especially
6236 * since we would have to linearly search /etc/mtab for each
6237 * one. Instead, do one pass through /etc/mtab looking for
6238 * zfs entries and call zfs_unmount() for each one.
6240 * Things get a little tricky if the administrator has created
6241 * mountpoints beneath other ZFS filesystems. In this case, we
6242 * have to unmount the deepest filesystems first. To accomplish
6243 * this, we place all the mountpoints in an AVL tree sorted by
6244 * the special type (dataset name), and walk the result in
6245 * reverse to make sure to get any snapshots first.
6247 struct mnttab entry
;
6248 uu_avl_pool_t
*pool
;
6249 uu_avl_t
*tree
= NULL
;
6250 unshare_unmount_node_t
*node
;
6252 uu_avl_walk_t
*walk
;
6255 (void) fprintf(stderr
, gettext("too many arguments\n"));
6259 if (((pool
= uu_avl_pool_create("unmount_pool",
6260 sizeof (unshare_unmount_node_t
),
6261 offsetof(unshare_unmount_node_t
, un_avlnode
),
6262 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6263 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6266 /* Reopen MNTTAB to prevent reading stale data from open file */
6267 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6270 while (getmntent(mnttab_file
, &entry
) == 0) {
6272 /* ignore non-ZFS entries */
6273 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6276 /* ignore snapshots */
6277 if (strchr(entry
.mnt_special
, '@') != NULL
)
6280 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6281 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6288 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6290 sizeof (nfs_mnt_prop
),
6291 NULL
, NULL
, 0, B_FALSE
) == 0);
6292 if (strcmp(nfs_mnt_prop
, "off") != 0)
6294 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6296 sizeof (nfs_mnt_prop
),
6297 NULL
, NULL
, 0, B_FALSE
) == 0);
6298 if (strcmp(nfs_mnt_prop
, "off") == 0)
6302 /* Ignore legacy mounts */
6303 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6305 sizeof (nfs_mnt_prop
),
6306 NULL
, NULL
, 0, B_FALSE
) == 0);
6307 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6309 /* Ignore canmount=noauto mounts */
6310 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6311 ZFS_CANMOUNT_NOAUTO
)
6317 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6319 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6321 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6323 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6324 uu_avl_insert(tree
, node
, idx
);
6326 zfs_close(node
->un_zhp
);
6327 free(node
->un_mountp
);
6333 * Walk the AVL tree in reverse, unmounting each filesystem and
6334 * removing it from the AVL tree in the process.
6336 if ((walk
= uu_avl_walk_start(tree
,
6337 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6340 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6341 uu_avl_remove(tree
, node
);
6345 if (zfs_unshareall_bypath(node
->un_zhp
,
6346 node
->un_mountp
) != 0)
6351 if (zfs_unmount(node
->un_zhp
,
6352 node
->un_mountp
, flags
) != 0)
6357 zfs_close(node
->un_zhp
);
6358 free(node
->un_mountp
);
6362 uu_avl_walk_end(walk
);
6363 uu_avl_destroy(tree
);
6364 uu_avl_pool_destroy(pool
);
6369 (void) fprintf(stderr
,
6370 gettext("missing filesystem argument\n"));
6372 (void) fprintf(stderr
,
6373 gettext("too many arguments\n"));
6378 * We have an argument, but it may be a full path or a ZFS
6379 * filesystem. Pass full paths off to unmount_path() (shared by
6380 * manual_unmount), otherwise open the filesystem and pass to
6383 if (argv
[0][0] == '/')
6384 return (unshare_unmount_path(op
, argv
[0],
6387 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6388 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6391 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
6392 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
6393 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
6394 NULL
, 0, B_FALSE
) == 0);
6398 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6400 sizeof (nfs_mnt_prop
),
6401 NULL
, NULL
, 0, B_FALSE
) == 0);
6402 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6403 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
6406 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6407 strcmp(sharesmb
, "off") == 0) {
6408 (void) fprintf(stderr
, gettext("cannot "
6409 "unshare '%s': legacy share\n"),
6411 (void) fprintf(stderr
, gettext("use "
6412 "unshare(1M) to unshare this "
6415 } else if (!zfs_is_shared(zhp
)) {
6416 (void) fprintf(stderr
, gettext("cannot "
6417 "unshare '%s': not currently "
6418 "shared\n"), zfs_get_name(zhp
));
6420 } else if (zfs_unshareall(zhp
) != 0) {
6426 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
6427 (void) fprintf(stderr
, gettext("cannot "
6428 "unmount '%s': legacy "
6429 "mountpoint\n"), zfs_get_name(zhp
));
6430 (void) fprintf(stderr
, gettext("use "
6431 "umount(1M) to unmount this "
6434 } else if (!zfs_is_mounted(zhp
, NULL
)) {
6435 (void) fprintf(stderr
, gettext("cannot "
6436 "unmount '%s': not currently "
6440 } else if (zfs_unmountall(zhp
, flags
) != 0) {
6454 * zfs unmount filesystem
6456 * Unmount all filesystems, or a specific ZFS filesystem.
6459 zfs_do_unmount(int argc
, char **argv
)
6461 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
6466 * zfs unshare filesystem
6468 * Unshare all filesystems, or a specific ZFS filesystem.
6471 zfs_do_unshare(int argc
, char **argv
)
6473 return (unshare_unmount(OP_SHARE
, argc
, argv
));
6477 find_command_idx(char *command
, int *idx
)
6481 for (i
= 0; i
< NCOMMAND
; i
++) {
6482 if (command_table
[i
].name
== NULL
)
6485 if (strcmp(command
, command_table
[i
].name
) == 0) {
6494 zfs_do_diff(int argc
, char **argv
)
6498 char *tosnap
= NULL
;
6499 char *fromsnap
= NULL
;
6504 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
6507 flags
|= ZFS_DIFF_CLASSIFY
;
6510 flags
|= ZFS_DIFF_PARSEABLE
;
6513 flags
|= ZFS_DIFF_TIMESTAMP
;
6516 (void) fprintf(stderr
,
6517 gettext("invalid option '%c'\n"), optopt
);
6526 (void) fprintf(stderr
,
6527 gettext("must provide at least one snapshot name\n"));
6532 (void) fprintf(stderr
, gettext("too many arguments\n"));
6537 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
6540 if (*fromsnap
!= '@')
6541 copy
= strdup(fromsnap
);
6543 copy
= strdup(tosnap
);
6547 if ((atp
= strchr(copy
, '@')))
6550 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6556 * Ignore SIGPIPE so that the library can give us
6557 * information on any failure
6559 (void) sigignore(SIGPIPE
);
6561 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
6569 * zfs bookmark <fs@snap> <fs#bmark>
6571 * Creates a bookmark with the given name from the given snapshot.
6574 zfs_do_bookmark(int argc
, char **argv
)
6576 char snapname
[ZFS_MAXNAMELEN
];
6583 while ((c
= getopt(argc
, argv
, "")) != -1) {
6586 (void) fprintf(stderr
,
6587 gettext("invalid option '%c'\n"), optopt
);
6595 /* check number of arguments */
6597 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
6601 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
6605 if (strchr(argv
[1], '#') == NULL
) {
6606 (void) fprintf(stderr
,
6607 gettext("invalid bookmark name '%s' -- "
6608 "must contain a '#'\n"), argv
[1]);
6612 if (argv
[0][0] == '@') {
6614 * Snapshot name begins with @.
6615 * Default to same fs as bookmark.
6617 (void) strncpy(snapname
, argv
[1], sizeof (snapname
));
6618 *strchr(snapname
, '#') = '\0';
6619 (void) strlcat(snapname
, argv
[0], sizeof (snapname
));
6621 (void) strncpy(snapname
, argv
[0], sizeof (snapname
));
6623 zhp
= zfs_open(g_zfs
, snapname
, ZFS_TYPE_SNAPSHOT
);
6629 nvl
= fnvlist_alloc();
6630 fnvlist_add_string(nvl
, argv
[1], snapname
);
6631 ret
= lzc_bookmark(nvl
, NULL
);
6635 const char *err_msg
;
6638 (void) snprintf(errbuf
, sizeof (errbuf
),
6639 dgettext(TEXT_DOMAIN
,
6640 "cannot create bookmark '%s'"), argv
[1]);
6644 err_msg
= "bookmark is in a different pool";
6647 err_msg
= "bookmark exists";
6650 err_msg
= "invalid argument";
6653 err_msg
= "bookmark feature not enabled";
6656 err_msg
= "out of space";
6659 err_msg
= "unknown error";
6662 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
6663 dgettext(TEXT_DOMAIN
, err_msg
));
6674 main(int argc
, char **argv
)
6680 (void) setlocale(LC_ALL
, "");
6681 (void) textdomain(TEXT_DOMAIN
);
6683 dprintf_setup(&argc
, argv
);
6688 * Make sure the user has specified some command.
6691 (void) fprintf(stderr
, gettext("missing command\n"));
6698 * The 'umount' command is an alias for 'unmount'
6700 if (strcmp(cmdname
, "umount") == 0)
6701 cmdname
= "unmount";
6704 * The 'recv' command is an alias for 'receive'
6706 if (strcmp(cmdname
, "recv") == 0)
6707 cmdname
= "receive";
6710 * The 'snap' command is an alias for 'snapshot'
6712 if (strcmp(cmdname
, "snap") == 0)
6713 cmdname
= "snapshot";
6718 if ((strcmp(cmdname
, "-?") == 0) ||
6719 (strcmp(cmdname
, "--help") == 0))
6722 if ((g_zfs
= libzfs_init()) == NULL
) {
6723 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
6727 mnttab_file
= g_zfs
->libzfs_mnttab
;
6729 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
6731 libzfs_print_on_error(g_zfs
, B_TRUE
);
6734 * Run the appropriate command.
6736 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
6737 if (find_command_idx(cmdname
, &i
) == 0) {
6738 current_command
= &command_table
[i
];
6739 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
6740 } else if (strchr(cmdname
, '=') != NULL
) {
6741 verify(find_command_idx("set", &i
) == 0);
6742 current_command
= &command_table
[i
];
6743 ret
= command_table
[i
].func(argc
, argv
);
6745 (void) fprintf(stderr
, gettext("unrecognized "
6746 "command '%s'\n"), cmdname
);
6751 if (ret
== 0 && log_history
)
6752 (void) zpool_log_history(g_zfs
, history_str
);
6757 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6758 * for the purposes of running ::findleaks.
6760 if (getenv("ZFS_ABORT") != NULL
) {
6761 (void) printf("dumping core by request\n");