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 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
37 #include <libnvpair.h>
50 #include <sys/mkdev.h>
51 #include <sys/mntent.h>
52 #include <sys/mnttab.h>
53 #include <sys/mount.h>
55 #include <sys/fs/zfs.h>
56 #include <sys/types.h>
60 #include <libzfs_core.h>
62 #include <zfs_deleg.h>
65 #include <directory.h>
70 #include "zfs_comutil.h"
72 libzfs_handle_t
*g_zfs
;
74 static FILE *mnttab_file
;
75 static char history_str
[HIS_MAX_RECORD_LEN
];
76 static boolean_t log_history
= B_TRUE
;
78 static int zfs_do_clone(int argc
, char **argv
);
79 static int zfs_do_create(int argc
, char **argv
);
80 static int zfs_do_destroy(int argc
, char **argv
);
81 static int zfs_do_get(int argc
, char **argv
);
82 static int zfs_do_inherit(int argc
, char **argv
);
83 static int zfs_do_list(int argc
, char **argv
);
84 static int zfs_do_mount(int argc
, char **argv
);
85 static int zfs_do_rename(int argc
, char **argv
);
86 static int zfs_do_rollback(int argc
, char **argv
);
87 static int zfs_do_set(int argc
, char **argv
);
88 static int zfs_do_upgrade(int argc
, char **argv
);
89 static int zfs_do_snapshot(int argc
, char **argv
);
90 static int zfs_do_unmount(int argc
, char **argv
);
91 static int zfs_do_share(int argc
, char **argv
);
92 static int zfs_do_unshare(int argc
, char **argv
);
93 static int zfs_do_send(int argc
, char **argv
);
94 static int zfs_do_receive(int argc
, char **argv
);
95 static int zfs_do_promote(int argc
, char **argv
);
96 static int zfs_do_userspace(int argc
, char **argv
);
97 static int zfs_do_allow(int argc
, char **argv
);
98 static int zfs_do_unallow(int argc
, char **argv
);
99 static int zfs_do_hold(int argc
, char **argv
);
100 static int zfs_do_holds(int argc
, char **argv
);
101 static int zfs_do_release(int argc
, char **argv
);
102 static int zfs_do_diff(int argc
, char **argv
);
103 static int zfs_do_bookmark(int argc
, char **argv
);
106 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
111 _umem_debug_init(void)
113 return ("default,verbose"); /* $UMEM_DEBUG setting */
117 _umem_logging_init(void)
119 return ("fail,contents"); /* $UMEM_LOGGING setting */
153 typedef struct zfs_command
{
155 int (*func
)(int argc
, char **argv
);
160 * Master command table. Each ZFS command has a name, associated function, and
161 * usage message. The usage messages need to be internationalized, so we have
162 * to have a function to return the usage message based on a command index.
164 * These commands are organized according to how they are displayed in the usage
165 * message. An empty command (one with a NULL name) indicates an empty line in
166 * the generic usage message.
168 static zfs_command_t command_table
[] = {
169 { "create", zfs_do_create
, HELP_CREATE
},
170 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
172 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
173 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
174 { "clone", zfs_do_clone
, HELP_CLONE
},
175 { "promote", zfs_do_promote
, HELP_PROMOTE
},
176 { "rename", zfs_do_rename
, HELP_RENAME
},
177 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
179 { "list", zfs_do_list
, HELP_LIST
},
181 { "set", zfs_do_set
, HELP_SET
},
182 { "get", zfs_do_get
, HELP_GET
},
183 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
184 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
185 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
186 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
188 { "mount", zfs_do_mount
, HELP_MOUNT
},
189 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
190 { "share", zfs_do_share
, HELP_SHARE
},
191 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
193 { "send", zfs_do_send
, HELP_SEND
},
194 { "receive", zfs_do_receive
, HELP_RECEIVE
},
196 { "allow", zfs_do_allow
, HELP_ALLOW
},
198 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
200 { "hold", zfs_do_hold
, HELP_HOLD
},
201 { "holds", zfs_do_holds
, HELP_HOLDS
},
202 { "release", zfs_do_release
, HELP_RELEASE
},
203 { "diff", zfs_do_diff
, HELP_DIFF
},
206 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
208 zfs_command_t
*current_command
;
211 get_usage(zfs_help_t idx
)
215 return (gettext("\tclone [-p] [-o property=value] ... "
216 "<snapshot> <filesystem|volume>\n"));
218 return (gettext("\tcreate [-p] [-o property=value] ... "
220 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
221 "-V <size> <volume>\n"));
223 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
224 "\tdestroy [-dnpRrv] "
225 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
226 "\tdestroy <filesystem|volume>#<bookmark>\n"));
228 return (gettext("\tget [-rHp] [-d max] "
229 "[-o \"all\" | field[,...]]\n"
230 "\t [-t type[,...]] [-s source[,...]]\n"
231 "\t <\"all\" | property[,...]> "
232 "[filesystem|volume|snapshot] ...\n"));
234 return (gettext("\tinherit [-rS] <property> "
235 "<filesystem|volume|snapshot> ...\n"));
237 return (gettext("\tupgrade [-v]\n"
238 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
240 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
241 "[-s property]...\n\t [-S property]... [-t type[,...]] "
242 "[filesystem|volume|snapshot] ...\n"));
244 return (gettext("\tmount\n"
245 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
247 return (gettext("\tpromote <clone-filesystem>\n"));
249 return (gettext("\treceive [-vnFu] <filesystem|volume|"
251 "\treceive [-vnFu] [-o origin=<snapshot>] [-d | -e] "
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 [-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 clone [-p] [-o prop=value] ... <snap> <fs | vol>
585 * Given an existing dataset, create a writable copy whose initial contents
586 * are the same as the source. The newly created dataset maintains a
587 * dependency on the original; the original cannot be destroyed so long as
590 * The '-p' flag creates all the non-existing ancestors of the target first.
593 zfs_do_clone(int argc
, char **argv
)
595 zfs_handle_t
*zhp
= NULL
;
596 boolean_t parents
= B_FALSE
;
601 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
605 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
608 if (parseprop(props
, optarg
) != 0)
615 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
624 /* check number of arguments */
626 (void) fprintf(stderr
, gettext("missing source dataset "
631 (void) fprintf(stderr
, gettext("missing target dataset "
636 (void) fprintf(stderr
, gettext("too many arguments\n"));
640 /* open the source dataset */
641 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
644 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
647 * Now create the ancestors of the target dataset. If the
648 * target already exists and '-p' option was used we should not
651 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
654 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
659 ret
= zfs_clone(zhp
, argv
[1], props
);
661 /* create the mountpoint if necessary */
665 clone
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
667 if (zfs_get_type(clone
) != ZFS_TYPE_VOLUME
)
668 if ((ret
= zfs_mount(clone
, NULL
, 0)) == 0)
669 ret
= zfs_share(clone
);
688 * zfs create [-p] [-o prop=value] ... fs
689 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
691 * Create a new dataset. This command can be used to create filesystems
692 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
693 * For volumes, the user must specify a size to be used.
695 * The '-s' flag applies only to volumes, and indicates that we should not try
696 * to set the reservation for this volume. By default we set a reservation
697 * equal to the size for any volume. For pools with SPA_VERSION >=
698 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
700 * The '-p' flag creates all the non-existing ancestors of the target first.
703 zfs_do_create(int argc
, char **argv
)
705 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
706 zfs_handle_t
*zhp
= NULL
;
709 boolean_t noreserve
= B_FALSE
;
710 boolean_t bflag
= B_FALSE
;
711 boolean_t parents
= B_FALSE
;
715 int canmount
= ZFS_CANMOUNT_OFF
;
717 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
721 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
724 type
= ZFS_TYPE_VOLUME
;
725 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
726 (void) fprintf(stderr
, gettext("bad volume "
727 "size '%s': %s\n"), optarg
,
728 libzfs_error_description(g_zfs
));
732 if (nvlist_add_uint64(props
,
733 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
742 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
743 (void) fprintf(stderr
, gettext("bad volume "
744 "block size '%s': %s\n"), optarg
,
745 libzfs_error_description(g_zfs
));
749 if (nvlist_add_uint64(props
,
750 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
755 if (parseprop(props
, optarg
) != 0)
762 (void) fprintf(stderr
, gettext("missing size "
766 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
772 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
773 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
774 "used when creating a volume\n"));
781 /* check number of arguments */
783 (void) fprintf(stderr
, gettext("missing %s argument\n"),
784 zfs_type_to_name(type
));
788 (void) fprintf(stderr
, gettext("too many arguments\n"));
792 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
793 zpool_handle_t
*zpool_handle
;
794 nvlist_t
*real_props
;
795 uint64_t spa_version
;
797 zfs_prop_t resv_prop
;
801 if (p
= strchr(argv
[0], '/'))
803 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
806 if (zpool_handle
== NULL
)
808 spa_version
= zpool_get_prop_int(zpool_handle
,
809 ZPOOL_PROP_VERSION
, NULL
);
810 zpool_close(zpool_handle
);
811 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
812 resv_prop
= ZFS_PROP_REFRESERVATION
;
814 resv_prop
= ZFS_PROP_RESERVATION
;
816 (void) snprintf(msg
, sizeof (msg
),
817 gettext("cannot create '%s'"), argv
[0]);
818 if (props
&& (real_props
= zfs_valid_proplist(g_zfs
, type
,
819 props
, 0, NULL
, msg
)) == NULL
)
822 volsize
= zvol_volsize_to_reservation(volsize
, real_props
);
823 nvlist_free(real_props
);
825 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
827 if (nvlist_add_uint64(props
,
828 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
835 if (parents
&& zfs_name_valid(argv
[0], type
)) {
837 * Now create the ancestors of target dataset. If the target
838 * already exists and '-p' option was used we should not
841 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
845 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
850 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
853 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
858 * if the user doesn't want the dataset automatically mounted,
859 * then skip the mount/share step
861 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
))
862 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
865 * Mount and/or share the new filesystem as appropriate. We provide a
866 * verbose error message to let the user know that their filesystem was
867 * in fact created, even if we failed to mount or share it.
869 if (canmount
== ZFS_CANMOUNT_ON
) {
870 if (zfs_mount(zhp
, NULL
, 0) != 0) {
871 (void) fprintf(stderr
, gettext("filesystem "
872 "successfully created, but not mounted\n"));
874 } else if (zfs_share(zhp
) != 0) {
875 (void) fprintf(stderr
, gettext("filesystem "
876 "successfully created, but not shared\n"));
893 * zfs destroy [-rRf] <fs, vol>
894 * zfs destroy [-rRd] <snap>
896 * -r Recursively destroy all children
897 * -R Recursively destroy all dependents, including clones
898 * -f Force unmounting of any dependents
899 * -d If we can't destroy now, mark for deferred destruction
901 * Destroys the given dataset. By default, it will unmount any filesystems,
902 * and refuse to destroy a dataset that has any dependents. A dependent can
903 * either be a child, or a clone of a child.
905 typedef struct destroy_cbdata
{
908 boolean_t cb_recurse
;
910 boolean_t cb_doclones
;
911 zfs_handle_t
*cb_target
;
912 boolean_t cb_defer_destroy
;
913 boolean_t cb_verbose
;
914 boolean_t cb_parsable
;
917 nvlist_t
*cb_batchedsnaps
;
919 /* first snap in contiguous run */
921 /* previous snap in contiguous run */
929 * Check for any dependents based on the '-r' or '-R' flags.
932 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
934 destroy_cbdata_t
*cbp
= data
;
935 const char *tname
= zfs_get_name(cbp
->cb_target
);
936 const char *name
= zfs_get_name(zhp
);
938 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
939 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
941 * This is a direct descendant, not a clone somewhere else in
948 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
949 "%s has children\n"),
950 zfs_get_name(cbp
->cb_target
),
951 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
952 (void) fprintf(stderr
, gettext("use '-r' to destroy "
953 "the following datasets:\n"));
954 cbp
->cb_first
= B_FALSE
;
955 cbp
->cb_error
= B_TRUE
;
958 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
961 * This is a clone. We only want to report this if the '-r'
962 * wasn't specified, or the target is a snapshot.
964 if (!cbp
->cb_recurse
&&
965 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
969 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
970 "%s has dependent clones\n"),
971 zfs_get_name(cbp
->cb_target
),
972 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
973 (void) fprintf(stderr
, gettext("use '-R' to destroy "
974 "the following datasets:\n"));
975 cbp
->cb_first
= B_FALSE
;
976 cbp
->cb_error
= B_TRUE
;
977 cbp
->cb_dryrun
= B_TRUE
;
980 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
989 destroy_callback(zfs_handle_t
*zhp
, void *data
)
991 destroy_cbdata_t
*cb
= data
;
992 const char *name
= zfs_get_name(zhp
);
994 if (cb
->cb_verbose
) {
995 if (cb
->cb_parsable
) {
996 (void) printf("destroy\t%s\n", name
);
997 } else if (cb
->cb_dryrun
) {
998 (void) printf(gettext("would destroy %s\n"),
1001 (void) printf(gettext("will destroy %s\n"),
1007 * Ignore pools (which we've already flagged as an error before getting
1010 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1011 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1015 if (cb
->cb_dryrun
) {
1021 * We batch up all contiguous snapshots (even of different
1022 * filesystems) and destroy them with one ioctl. We can't
1023 * simply do all snap deletions and then all fs deletions,
1024 * because we must delete a clone before its origin.
1026 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1027 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1029 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1030 cb
->cb_batchedsnaps
, B_FALSE
);
1031 fnvlist_free(cb
->cb_batchedsnaps
);
1032 cb
->cb_batchedsnaps
= fnvlist_alloc();
1035 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1036 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1047 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1049 destroy_cbdata_t
*cb
= arg
;
1050 const char *name
= zfs_get_name(zhp
);
1053 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1054 if (cb
->cb_firstsnap
== NULL
)
1055 cb
->cb_firstsnap
= strdup(name
);
1056 if (cb
->cb_prevsnap
!= NULL
)
1057 free(cb
->cb_prevsnap
);
1058 /* this snap continues the current range */
1059 cb
->cb_prevsnap
= strdup(name
);
1060 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1062 if (cb
->cb_verbose
) {
1063 if (cb
->cb_parsable
) {
1064 (void) printf("destroy\t%s\n", name
);
1065 } else if (cb
->cb_dryrun
) {
1066 (void) printf(gettext("would destroy %s\n"),
1069 (void) printf(gettext("will destroy %s\n"),
1073 } else if (cb
->cb_firstsnap
!= NULL
) {
1074 /* end of this range */
1076 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1077 cb
->cb_prevsnap
, &used
);
1078 cb
->cb_snapused
+= used
;
1079 free(cb
->cb_firstsnap
);
1080 cb
->cb_firstsnap
= NULL
;
1081 free(cb
->cb_prevsnap
);
1082 cb
->cb_prevsnap
= NULL
;
1089 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1092 assert(cb
->cb_firstsnap
== NULL
);
1093 assert(cb
->cb_prevsnap
== NULL
);
1094 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1095 if (cb
->cb_firstsnap
!= NULL
) {
1098 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1099 cb
->cb_prevsnap
, &used
);
1101 cb
->cb_snapused
+= used
;
1102 free(cb
->cb_firstsnap
);
1103 cb
->cb_firstsnap
= NULL
;
1104 free(cb
->cb_prevsnap
);
1105 cb
->cb_prevsnap
= NULL
;
1111 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1113 destroy_cbdata_t
*cb
= arg
;
1116 /* Check for clones. */
1117 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1118 cb
->cb_target
= zhp
;
1119 cb
->cb_first
= B_TRUE
;
1120 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1121 destroy_check_dependent
, cb
);
1125 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1133 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1135 destroy_cbdata_t
*cb
= arg
;
1138 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1144 if (cb
->cb_verbose
) {
1145 err
= destroy_print_snapshots(zhp
, cb
);
1151 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1159 destroy_clones(destroy_cbdata_t
*cb
)
1162 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1164 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1165 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1168 boolean_t defer
= cb
->cb_defer_destroy
;
1172 * We can't defer destroy non-snapshots, so set it to
1173 * false while destroying the clones.
1175 cb
->cb_defer_destroy
= B_FALSE
;
1176 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1177 destroy_callback
, cb
);
1178 cb
->cb_defer_destroy
= defer
;
1188 zfs_do_destroy(int argc
, char **argv
)
1190 destroy_cbdata_t cb
= { 0 };
1194 zfs_handle_t
*zhp
= NULL
;
1196 zfs_type_t type
= ZFS_TYPE_DATASET
;
1199 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1202 cb
.cb_verbose
= B_TRUE
;
1205 cb
.cb_verbose
= B_TRUE
;
1206 cb
.cb_parsable
= B_TRUE
;
1209 cb
.cb_dryrun
= B_TRUE
;
1212 cb
.cb_defer_destroy
= B_TRUE
;
1213 type
= ZFS_TYPE_SNAPSHOT
;
1216 cb
.cb_force
= B_TRUE
;
1219 cb
.cb_recurse
= B_TRUE
;
1222 cb
.cb_recurse
= B_TRUE
;
1223 cb
.cb_doclones
= B_TRUE
;
1227 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1236 /* check number of arguments */
1238 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1242 (void) fprintf(stderr
, gettext("too many arguments\n"));
1246 at
= strchr(argv
[0], '@');
1247 pound
= strchr(argv
[0], '#');
1250 /* Build the list of snaps to destroy in cb_nvl. */
1251 cb
.cb_nvl
= fnvlist_alloc();
1254 zhp
= zfs_open(g_zfs
, argv
[0],
1255 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1259 cb
.cb_snapspec
= at
+ 1;
1260 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1266 if (nvlist_empty(cb
.cb_nvl
)) {
1267 (void) fprintf(stderr
, gettext("could not find any "
1268 "snapshots to destroy; check snapshot names.\n"));
1273 if (cb
.cb_verbose
) {
1275 zfs_nicenum(cb
.cb_snapused
, buf
, sizeof (buf
));
1276 if (cb
.cb_parsable
) {
1277 (void) printf("reclaim\t%llu\n",
1279 } else if (cb
.cb_dryrun
) {
1280 (void) printf(gettext("would reclaim %s\n"),
1283 (void) printf(gettext("will reclaim %s\n"),
1288 if (!cb
.cb_dryrun
) {
1289 if (cb
.cb_doclones
) {
1290 cb
.cb_batchedsnaps
= fnvlist_alloc();
1291 err
= destroy_clones(&cb
);
1293 err
= zfs_destroy_snaps_nvl(g_zfs
,
1294 cb
.cb_batchedsnaps
, B_FALSE
);
1302 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1303 cb
.cb_defer_destroy
);
1309 } else if (pound
!= NULL
) {
1314 (void) fprintf(stderr
,
1315 "dryrun is not supported with bookmark\n");
1319 if (cb
.cb_defer_destroy
) {
1320 (void) fprintf(stderr
,
1321 "defer destroy is not supported with bookmark\n");
1325 if (cb
.cb_recurse
) {
1326 (void) fprintf(stderr
,
1327 "recursive is not supported with bookmark\n");
1331 if (!zfs_bookmark_exists(argv
[0])) {
1332 (void) fprintf(stderr
, gettext("bookmark '%s' "
1333 "does not exist.\n"), argv
[0]);
1337 nvl
= fnvlist_alloc();
1338 fnvlist_add_boolean(nvl
, argv
[0]);
1340 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1342 (void) zfs_standard_error(g_zfs
, err
,
1343 "cannot destroy bookmark");
1346 nvlist_free(cb
.cb_nvl
);
1350 /* Open the given dataset */
1351 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1357 * Perform an explicit check for pools before going any further.
1359 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1360 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1361 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1362 "operation does not apply to pools\n"),
1364 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1365 "%s' to destroy all datasets in the pool\n"),
1367 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1368 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1374 * Check for any dependents and/or clones.
1376 cb
.cb_first
= B_TRUE
;
1377 if (!cb
.cb_doclones
&&
1378 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1389 cb
.cb_batchedsnaps
= fnvlist_alloc();
1390 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1397 * Do the real thing. The callback will close the
1398 * handle regardless of whether it succeeds or not.
1400 err
= destroy_callback(zhp
, &cb
);
1403 err
= zfs_destroy_snaps_nvl(g_zfs
,
1404 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1411 fnvlist_free(cb
.cb_batchedsnaps
);
1412 fnvlist_free(cb
.cb_nvl
);
1419 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1422 zfs_get_column_t col
;
1424 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1425 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1426 if (col
== GET_COL_RECVD
)
1432 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1433 * < all | property[,property]... > < fs | snap | vol > ...
1435 * -r recurse over any child datasets
1436 * -H scripted mode. Headers are stripped, and fields are separated
1437 * by tabs instead of spaces.
1438 * -o Set of fields to display. One of "name,property,value,
1439 * received,source". Default is "name,property,value,source".
1440 * "all" is an alias for all five.
1441 * -s Set of sources to allow. One of
1442 * "local,default,inherited,received,temporary,none". Default is
1444 * -p Display values in parsable (literal) format.
1446 * Prints properties for the given datasets. The user can control which
1447 * columns to display as well as which property types to allow.
1451 * Invoked to display the properties for a single dataset.
1454 get_callback(zfs_handle_t
*zhp
, void *data
)
1456 char buf
[ZFS_MAXPROPLEN
];
1457 char rbuf
[ZFS_MAXPROPLEN
];
1458 zprop_source_t sourcetype
;
1459 char source
[ZFS_MAXNAMELEN
];
1460 zprop_get_cbdata_t
*cbp
= data
;
1461 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1462 zprop_list_t
*pl
= cbp
->cb_proplist
;
1466 boolean_t received
= is_recvd_column(cbp
);
1468 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1469 char *recvdval
= NULL
;
1471 * Skip the special fake placeholder. This will also skip over
1472 * the name property when 'all' is specified.
1474 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1475 pl
== cbp
->cb_proplist
)
1478 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1479 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1480 sizeof (buf
), &sourcetype
, source
,
1482 cbp
->cb_literal
) != 0) {
1485 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1486 ZFS_TYPE_DATASET
)) {
1487 (void) fprintf(stderr
,
1488 gettext("No such property '%s'\n"),
1489 zfs_prop_to_name(pl
->pl_prop
));
1492 sourcetype
= ZPROP_SRC_NONE
;
1493 (void) strlcpy(buf
, "-", sizeof (buf
));
1496 if (received
&& (zfs_prop_get_recvd(zhp
,
1497 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1498 cbp
->cb_literal
) == 0))
1501 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1502 zfs_prop_to_name(pl
->pl_prop
),
1503 buf
, sourcetype
, source
, recvdval
);
1504 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1505 sourcetype
= ZPROP_SRC_LOCAL
;
1507 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1508 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1509 sourcetype
= ZPROP_SRC_NONE
;
1510 (void) strlcpy(buf
, "-", sizeof (buf
));
1513 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1514 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1515 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1516 sourcetype
= ZPROP_SRC_LOCAL
;
1518 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1519 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1520 sourcetype
= ZPROP_SRC_NONE
;
1521 (void) strlcpy(buf
, "-", sizeof (buf
));
1524 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1525 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1527 if (nvlist_lookup_nvlist(user_props
,
1528 pl
->pl_user_prop
, &propval
) != 0) {
1531 sourcetype
= ZPROP_SRC_NONE
;
1534 verify(nvlist_lookup_string(propval
,
1535 ZPROP_VALUE
, &strval
) == 0);
1536 verify(nvlist_lookup_string(propval
,
1537 ZPROP_SOURCE
, &sourceval
) == 0);
1539 if (strcmp(sourceval
,
1540 zfs_get_name(zhp
)) == 0) {
1541 sourcetype
= ZPROP_SRC_LOCAL
;
1542 } else if (strcmp(sourceval
,
1543 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1544 sourcetype
= ZPROP_SRC_RECEIVED
;
1546 sourcetype
= ZPROP_SRC_INHERITED
;
1547 (void) strlcpy(source
,
1548 sourceval
, sizeof (source
));
1552 if (received
&& (zfs_prop_get_recvd(zhp
,
1553 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1554 cbp
->cb_literal
) == 0))
1557 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1558 pl
->pl_user_prop
, strval
, sourcetype
,
1567 zfs_do_get(int argc
, char **argv
)
1569 zprop_get_cbdata_t cb
= { 0 };
1570 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1571 int types
= ZFS_TYPE_DATASET
;
1572 char *value
, *fields
;
1575 zprop_list_t fake_name
= { 0 };
1578 * Set up default columns and sources.
1580 cb
.cb_sources
= ZPROP_SRC_ALL
;
1581 cb
.cb_columns
[0] = GET_COL_NAME
;
1582 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1583 cb
.cb_columns
[2] = GET_COL_VALUE
;
1584 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1585 cb
.cb_type
= ZFS_TYPE_DATASET
;
1588 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1591 cb
.cb_literal
= B_TRUE
;
1594 limit
= parse_depth(optarg
, &flags
);
1597 flags
|= ZFS_ITER_RECURSE
;
1600 cb
.cb_scripted
= B_TRUE
;
1603 (void) fprintf(stderr
, gettext("missing argument for "
1604 "'%c' option\n"), optopt
);
1609 * Process the set of columns to display. We zero out
1610 * the structure to give us a blank slate.
1612 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1614 while (*optarg
!= '\0') {
1615 static char *col_subopts
[] =
1616 { "name", "property", "value", "received",
1617 "source", "all", NULL
};
1619 if (i
== ZFS_GET_NCOLS
) {
1620 (void) fprintf(stderr
, gettext("too "
1621 "many fields given to -o "
1626 switch (getsubopt(&optarg
, col_subopts
,
1629 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1632 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1635 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1638 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1639 flags
|= ZFS_ITER_RECVD_PROPS
;
1642 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1646 (void) fprintf(stderr
,
1647 gettext("\"all\" conflicts "
1648 "with specific fields "
1649 "given to -o option\n"));
1652 cb
.cb_columns
[0] = GET_COL_NAME
;
1653 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1654 cb
.cb_columns
[2] = GET_COL_VALUE
;
1655 cb
.cb_columns
[3] = GET_COL_RECVD
;
1656 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1657 flags
|= ZFS_ITER_RECVD_PROPS
;
1661 (void) fprintf(stderr
,
1662 gettext("invalid column name "
1671 while (*optarg
!= '\0') {
1672 static char *source_subopts
[] = {
1673 "local", "default", "inherited",
1674 "received", "temporary", "none",
1677 switch (getsubopt(&optarg
, source_subopts
,
1680 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1683 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1686 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1689 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1692 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1695 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1698 (void) fprintf(stderr
,
1699 gettext("invalid source "
1708 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1709 while (*optarg
!= '\0') {
1710 static char *type_subopts
[] = { "filesystem",
1711 "volume", "snapshot", "bookmark",
1714 switch (getsubopt(&optarg
, type_subopts
,
1717 types
|= ZFS_TYPE_FILESYSTEM
;
1720 types
|= ZFS_TYPE_VOLUME
;
1723 types
|= ZFS_TYPE_SNAPSHOT
;
1726 types
|= ZFS_TYPE_BOOKMARK
;
1729 types
= ZFS_TYPE_DATASET
|
1734 (void) fprintf(stderr
,
1735 gettext("invalid type '%s'\n"),
1743 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1753 (void) fprintf(stderr
, gettext("missing property "
1760 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1768 * As part of zfs_expand_proplist(), we keep track of the maximum column
1769 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1770 * need to know the maximum name length. However, the user likely did
1771 * not specify 'name' as one of the properties to fetch, so we need to
1772 * make sure we always include at least this property for
1773 * print_get_headers() to work properly.
1775 if (cb
.cb_proplist
!= NULL
) {
1776 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1777 fake_name
.pl_width
= strlen(gettext("NAME"));
1778 fake_name
.pl_next
= cb
.cb_proplist
;
1779 cb
.cb_proplist
= &fake_name
;
1782 cb
.cb_first
= B_TRUE
;
1784 /* run for each object */
1785 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1786 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1788 if (cb
.cb_proplist
== &fake_name
)
1789 zprop_free_list(fake_name
.pl_next
);
1791 zprop_free_list(cb
.cb_proplist
);
1797 * inherit [-rS] <property> <fs|vol> ...
1799 * -r Recurse over all children
1800 * -S Revert to received value, if any
1802 * For each dataset specified on the command line, inherit the given property
1803 * from its parent. Inheriting a property at the pool level will cause it to
1804 * use the default value. The '-r' flag will recurse over all children, and is
1805 * useful for setting a property on a hierarchy-wide basis, regardless of any
1806 * local modifications for each dataset.
1809 typedef struct inherit_cbdata
{
1810 const char *cb_propname
;
1811 boolean_t cb_received
;
1815 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1817 inherit_cbdata_t
*cb
= data
;
1818 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1821 * If we're doing it recursively, then ignore properties that
1822 * are not valid for this type of dataset.
1824 if (prop
!= ZPROP_INVAL
&&
1825 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
)))
1828 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1832 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1834 inherit_cbdata_t
*cb
= data
;
1836 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1840 zfs_do_inherit(int argc
, char **argv
)
1844 inherit_cbdata_t cb
= { 0 };
1848 boolean_t received
= B_FALSE
;
1851 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1854 flags
|= ZFS_ITER_RECURSE
;
1861 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1870 /* check number of arguments */
1872 (void) fprintf(stderr
, gettext("missing property argument\n"));
1876 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1884 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1885 if (zfs_prop_readonly(prop
)) {
1886 (void) fprintf(stderr
, gettext(
1887 "%s property is read-only\n"),
1891 if (!zfs_prop_inheritable(prop
) && !received
) {
1892 (void) fprintf(stderr
, gettext("'%s' property cannot "
1893 "be inherited\n"), propname
);
1894 if (prop
== ZFS_PROP_QUOTA
||
1895 prop
== ZFS_PROP_RESERVATION
||
1896 prop
== ZFS_PROP_REFQUOTA
||
1897 prop
== ZFS_PROP_REFRESERVATION
) {
1898 (void) fprintf(stderr
, gettext("use 'zfs set "
1899 "%s=none' to clear\n"), propname
);
1900 (void) fprintf(stderr
, gettext("use 'zfs "
1901 "inherit -S %s' to revert to received "
1902 "value\n"), propname
);
1906 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1907 prop
== ZFS_PROP_VERSION
)) {
1908 (void) fprintf(stderr
, gettext("'%s' property cannot "
1909 "be reverted to a received value\n"), propname
);
1912 } else if (!zfs_prop_user(propname
)) {
1913 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1918 cb
.cb_propname
= propname
;
1919 cb
.cb_received
= received
;
1921 if (flags
& ZFS_ITER_RECURSE
) {
1922 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1923 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1925 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1926 NULL
, NULL
, 0, inherit_cb
, &cb
);
1932 typedef struct upgrade_cbdata
{
1933 uint64_t cb_numupgraded
;
1934 uint64_t cb_numsamegraded
;
1935 uint64_t cb_numfailed
;
1936 uint64_t cb_version
;
1938 boolean_t cb_foundone
;
1939 char cb_lastfs
[ZFS_MAXNAMELEN
];
1943 same_pool(zfs_handle_t
*zhp
, const char *name
)
1945 int len1
= strcspn(name
, "/@");
1946 const char *zhname
= zfs_get_name(zhp
);
1947 int len2
= strcspn(zhname
, "/@");
1951 return (strncmp(name
, zhname
, len1
) == 0);
1955 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
1957 upgrade_cbdata_t
*cb
= data
;
1958 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1960 /* list if it's old/new */
1961 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
1962 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
1965 str
= gettext("The following filesystems are "
1966 "formatted using a newer software version and\n"
1967 "cannot be accessed on the current system.\n\n");
1969 str
= gettext("The following filesystems are "
1970 "out of date, and can be upgraded. After being\n"
1971 "upgraded, these filesystems (and any 'zfs send' "
1972 "streams generated from\n"
1973 "subsequent snapshots) will no longer be "
1974 "accessible by older software versions.\n\n");
1977 if (!cb
->cb_foundone
) {
1979 (void) printf(gettext("VER FILESYSTEM\n"));
1980 (void) printf(gettext("--- ------------\n"));
1981 cb
->cb_foundone
= B_TRUE
;
1984 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
1991 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
1993 upgrade_cbdata_t
*cb
= data
;
1994 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1995 int needed_spa_version
;
1998 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2001 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2003 if (needed_spa_version
< 0)
2006 if (spa_version
< needed_spa_version
) {
2008 (void) printf(gettext("%s: can not be "
2009 "upgraded; the pool version needs to first "
2010 "be upgraded\nto version %d\n\n"),
2011 zfs_get_name(zhp
), needed_spa_version
);
2017 if (version
< cb
->cb_version
) {
2019 (void) snprintf(verstr
, sizeof (verstr
),
2020 "%llu", cb
->cb_version
);
2021 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2023 * If they did "zfs upgrade -a", then we could
2024 * be doing ioctls to different pools. We need
2025 * to log this history once to each pool, and bypass
2026 * the normal history logging that happens in main().
2028 (void) zpool_log_history(g_zfs
, history_str
);
2029 log_history
= B_FALSE
;
2031 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2032 cb
->cb_numupgraded
++;
2035 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2036 } else if (version
> cb
->cb_version
) {
2037 /* can't downgrade */
2038 (void) printf(gettext("%s: can not be downgraded; "
2039 "it is already at version %u\n"),
2040 zfs_get_name(zhp
), version
);
2043 cb
->cb_numsamegraded
++;
2051 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2054 zfs_do_upgrade(int argc
, char **argv
)
2056 boolean_t all
= B_FALSE
;
2057 boolean_t showversions
= B_FALSE
;
2059 upgrade_cbdata_t cb
= { 0 };
2061 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2064 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2067 flags
|= ZFS_ITER_RECURSE
;
2070 showversions
= B_TRUE
;
2073 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2074 optarg
, &cb
.cb_version
) != 0) {
2075 (void) fprintf(stderr
,
2076 gettext("invalid version %s\n"), optarg
);
2085 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2094 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2096 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2097 cb
.cb_version
|| argc
))
2099 if ((all
|| argc
) && (showversions
))
2105 /* Show info on available versions. */
2106 (void) printf(gettext("The following filesystem versions are "
2108 (void) printf(gettext("VER DESCRIPTION\n"));
2109 (void) printf("--- -----------------------------------------"
2110 "---------------\n");
2111 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2112 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2113 (void) printf(gettext(" 3 Case insensitive and filesystem "
2114 "user identifier (FUID)\n"));
2115 (void) printf(gettext(" 4 userquota, groupquota "
2117 (void) printf(gettext(" 5 System attributes\n"));
2118 (void) printf(gettext("\nFor more information on a particular "
2119 "version, including supported releases,\n"));
2120 (void) printf("see the ZFS Administration Guide.\n\n");
2122 } else if (argc
|| all
) {
2123 /* Upgrade filesystems */
2124 if (cb
.cb_version
== 0)
2125 cb
.cb_version
= ZPL_VERSION
;
2126 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2127 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2128 (void) printf(gettext("%llu filesystems upgraded\n"),
2130 if (cb
.cb_numsamegraded
) {
2131 (void) printf(gettext("%llu filesystems already at "
2133 cb
.cb_numsamegraded
);
2135 if (cb
.cb_numfailed
!= 0)
2138 /* List old-version filesytems */
2140 (void) printf(gettext("This system is currently running "
2141 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2143 flags
|= ZFS_ITER_RECURSE
;
2144 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2145 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2147 found
= cb
.cb_foundone
;
2148 cb
.cb_foundone
= B_FALSE
;
2149 cb
.cb_newer
= B_TRUE
;
2151 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2152 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2154 if (!cb
.cb_foundone
&& !found
) {
2155 (void) printf(gettext("All filesystems are "
2156 "formatted with the current version.\n"));
2164 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2165 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2166 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2167 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2169 * -H Scripted mode; elide headers and separate columns by tabs.
2170 * -i Translate SID to POSIX ID.
2171 * -n Print numeric ID instead of user/group name.
2172 * -o Control which fields to display.
2173 * -p Use exact (parsable) numeric output.
2174 * -s Specify sort columns, descending order.
2175 * -S Specify sort columns, ascending order.
2176 * -t Control which object types to display.
2178 * Displays space consumed by, and quotas on, each user in the specified
2179 * filesystem or snapshot.
2182 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2183 enum us_field_types
{
2189 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA" };
2190 static char *us_field_names
[] = { "type", "name", "used", "quota" };
2191 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2193 #define USTYPE_PSX_GRP (1 << 0)
2194 #define USTYPE_PSX_USR (1 << 1)
2195 #define USTYPE_SMB_GRP (1 << 2)
2196 #define USTYPE_SMB_USR (1 << 3)
2197 #define USTYPE_ALL \
2198 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2200 static int us_type_bits
[] = {
2207 static char *us_type_names
[] = { "posixgroup", "posixuser", "smbgroup",
2210 typedef struct us_node
{
2212 uu_avl_node_t usn_avlnode
;
2213 uu_list_node_t usn_listnode
;
2216 typedef struct us_cbdata
{
2218 uu_avl_pool_t
*cb_avl_pool
;
2220 boolean_t cb_numname
;
2221 boolean_t cb_nicenum
;
2222 boolean_t cb_sid2posix
;
2223 zfs_userquota_prop_t cb_prop
;
2224 zfs_sort_column_t
*cb_sortcol
;
2225 size_t cb_width
[USFIELD_LAST
];
2228 static boolean_t us_populated
= B_FALSE
;
2231 zfs_sort_column_t
*si_sortcol
;
2232 boolean_t si_numname
;
2236 us_field_index(char *field
)
2240 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2241 if (strcmp(field
, us_field_names
[i
]) == 0)
2249 us_compare(const void *larg
, const void *rarg
, void *unused
)
2251 const us_node_t
*l
= larg
;
2252 const us_node_t
*r
= rarg
;
2253 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2254 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2255 boolean_t numname
= si
->si_numname
;
2256 nvlist_t
*lnvl
= l
->usn_nvl
;
2257 nvlist_t
*rnvl
= r
->usn_nvl
;
2261 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2268 zfs_prop_t prop
= sortcol
->sc_prop
;
2269 const char *propname
= NULL
;
2270 boolean_t reverse
= sortcol
->sc_reverse
;
2275 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2276 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2278 rc
= (rv32
< lv32
) ? 1 : -1;
2283 (void) nvlist_lookup_uint64(lnvl
, propname
,
2285 (void) nvlist_lookup_uint64(rnvl
, propname
,
2288 rc
= (rv64
< lv64
) ? 1 : -1;
2290 (void) nvlist_lookup_string(lnvl
, propname
,
2292 (void) nvlist_lookup_string(rnvl
, propname
,
2294 rc
= strcmp(lvstr
, rvstr
);
2298 case ZFS_PROP_QUOTA
:
2301 if (prop
== ZFS_PROP_USED
)
2305 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2306 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2308 rc
= (rv64
< lv64
) ? 1 : -1;
2314 return (reverse
? 1 : -1);
2316 return (reverse
? -1 : 1);
2321 * If entries still seem to be the same, check if they are of the same
2322 * type (smbentity is added only if we are doing SID to POSIX ID
2323 * translation where we can have duplicate type/name combinations).
2325 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2326 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2328 return (lvb
< rvb
? -1 : 1);
2333 static inline const char *
2334 us_type2str(unsigned field_type
)
2336 switch (field_type
) {
2337 case USTYPE_PSX_USR
:
2338 return ("POSIX User");
2339 case USTYPE_PSX_GRP
:
2340 return ("POSIX Group");
2341 case USTYPE_SMB_USR
:
2342 return ("SMB User");
2343 case USTYPE_SMB_GRP
:
2344 return ("SMB Group");
2346 return ("Undefined");
2351 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2353 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2354 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2359 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2360 uu_avl_t
*avl
= cb
->cb_avl
;
2364 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2366 const char *typestr
;
2370 int typeidx
, nameidx
, sizeidx
;
2371 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2372 boolean_t smbentity
= B_FALSE
;
2374 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2376 node
= safe_malloc(sizeof (us_node_t
));
2377 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2378 node
->usn_nvl
= props
;
2380 if (domain
!= NULL
&& domain
[0] != '\0') {
2382 char sid
[ZFS_MAXNAMELEN
+ 32];
2385 int flag
= IDMAP_REQ_FLG_USE_CACHE
;
2389 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2391 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2392 type
= USTYPE_SMB_GRP
;
2393 err
= sid_to_id(sid
, B_FALSE
, &id
);
2395 type
= USTYPE_SMB_USR
;
2396 err
= sid_to_id(sid
, B_TRUE
, &id
);
2401 if (!cb
->cb_sid2posix
) {
2402 if (type
== USTYPE_SMB_USR
) {
2403 (void) idmap_getwinnamebyuid(rid
, flag
,
2406 (void) idmap_getwinnamebygid(rid
, flag
,
2415 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2417 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2418 type
= USTYPE_PSX_GRP
;
2419 if (!cb
->cb_numname
) {
2422 if ((g
= getgrgid(rid
)) != NULL
)
2426 type
= USTYPE_PSX_USR
;
2427 if (!cb
->cb_numname
) {
2430 if ((p
= getpwuid(rid
)) != NULL
)
2437 * Make sure that the type/name combination is unique when doing
2438 * SID to POSIX ID translation (hence changing the type from SMB to
2441 if (cb
->cb_sid2posix
&&
2442 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2445 /* Calculate/update width of TYPE field */
2446 typestr
= us_type2str(type
);
2447 typelen
= strlen(gettext(typestr
));
2448 typeidx
= us_field_index("type");
2449 if (typelen
> cb
->cb_width
[typeidx
])
2450 cb
->cb_width
[typeidx
] = typelen
;
2451 if (nvlist_add_uint32(props
, "type", type
) != 0)
2454 /* Calculate/update width of NAME field */
2455 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2456 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2458 namelen
= snprintf(NULL
, 0, "%u", rid
);
2460 if (nvlist_add_string(props
, "name", name
) != 0)
2462 namelen
= strlen(name
);
2464 nameidx
= us_field_index("name");
2465 if (namelen
> cb
->cb_width
[nameidx
])
2466 cb
->cb_width
[nameidx
] = namelen
;
2469 * Check if this type/name combination is in the list and update it;
2470 * otherwise add new node to the list.
2472 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2473 uu_avl_insert(avl
, node
, idx
);
2478 props
= node
->usn_nvl
;
2481 /* Calculate/update width of USED/QUOTA fields */
2483 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2485 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu", space
);
2486 sizelen
= strlen(sizebuf
);
2487 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
) {
2489 if (!nvlist_exists(props
, "quota"))
2490 (void) nvlist_add_uint64(props
, "quota", 0);
2493 if (!nvlist_exists(props
, "used"))
2494 (void) nvlist_add_uint64(props
, "used", 0);
2496 sizeidx
= us_field_index(propname
);
2497 if (sizelen
> cb
->cb_width
[sizeidx
])
2498 cb
->cb_width
[sizeidx
] = sizelen
;
2500 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2507 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2508 size_t *width
, us_node_t
*node
)
2510 nvlist_t
*nvl
= node
->usn_nvl
;
2511 char valstr
[ZFS_MAXNAMELEN
];
2512 boolean_t first
= B_TRUE
;
2518 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2519 if (!(ustype
& types
))
2522 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2523 nvpair_t
*nvp
= NULL
;
2527 char *strval
= NULL
;
2529 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2530 if (strcmp(nvpair_name(nvp
),
2531 us_field_names
[field
]) == 0)
2535 type
= nvpair_type(nvp
);
2537 case DATA_TYPE_UINT32
:
2538 (void) nvpair_value_uint32(nvp
, &val32
);
2540 case DATA_TYPE_UINT64
:
2541 (void) nvpair_value_uint64(nvp
, &val64
);
2543 case DATA_TYPE_STRING
:
2544 (void) nvpair_value_string(nvp
, &strval
);
2547 (void) fprintf(stderr
, "invalid data type\n");
2552 strval
= (char *)us_type2str(val32
);
2555 if (type
== DATA_TYPE_UINT64
) {
2556 (void) sprintf(valstr
, "%llu", val64
);
2562 if (type
== DATA_TYPE_UINT64
) {
2564 (void) sprintf(valstr
, "%llu", val64
);
2566 zfs_nicenum(val64
, valstr
,
2569 if (field
== USFIELD_QUOTA
&&
2570 strcmp(valstr
, "0") == 0)
2580 (void) printf("\t");
2585 (void) printf("%s", strval
);
2586 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2587 (void) printf("%-*s", width
[field
], strval
);
2589 (void) printf("%*s", width
[field
], strval
);
2595 (void) printf("\n");
2599 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2600 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2608 boolean_t first
= B_TRUE
;
2610 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2611 col
= gettext(us_field_hdr
[field
]);
2612 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2613 (void) printf(first
? "%-*s" : " %-*s",
2616 (void) printf(first
? "%*s" : " %*s",
2622 (void) printf("\n");
2625 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2626 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2628 nvlist_free(node
->usn_nvl
);
2633 zfs_do_userspace(int argc
, char **argv
)
2636 zfs_userquota_prop_t p
;
2637 uu_avl_pool_t
*avl_pool
;
2639 uu_avl_walk_t
*walk
;
2641 char deffields
[] = "type,name,used,quota";
2642 char *ofield
= NULL
;
2643 char *tfield
= NULL
;
2647 boolean_t scripted
= B_FALSE
;
2648 boolean_t prtnum
= B_FALSE
;
2649 boolean_t parsable
= B_FALSE
;
2650 boolean_t sid2posix
= B_FALSE
;
2653 zfs_sort_column_t
*sortcol
= NULL
;
2654 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2658 uu_list_pool_t
*listpool
;
2660 uu_avl_index_t idx
= 0;
2661 uu_list_index_t idx2
= 0;
2666 if (strcmp(argv
[0], "groupspace") == 0)
2667 /* Toggle default group types */
2668 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2670 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2686 if (zfs_add_sort_column(&sortcol
, optarg
,
2687 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
2688 (void) fprintf(stderr
,
2689 gettext("invalid field '%s'\n"), optarg
);
2700 (void) fprintf(stderr
, gettext("missing argument for "
2701 "'%c' option\n"), optopt
);
2705 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2715 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2719 (void) fprintf(stderr
, gettext("too many arguments\n"));
2723 /* Use default output fields if not specified using -o */
2727 if ((delim
= strchr(ofield
, ',')) != NULL
)
2729 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
2730 (void) fprintf(stderr
, gettext("invalid type '%s' "
2731 "for -o option\n"), ofield
);
2736 } while (delim
!= NULL
);
2737 fields
[cfield
] = USFIELD_LAST
;
2739 /* Override output types (-t option) */
2740 if (tfield
!= NULL
) {
2744 boolean_t found
= B_FALSE
;
2746 if ((delim
= strchr(tfield
, ',')) != NULL
)
2748 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
2750 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
2752 types
|= us_type_bits
[i
];
2757 (void) fprintf(stderr
, gettext("invalid type "
2758 "'%s' for -t option\n"), tfield
);
2763 } while (delim
!= NULL
);
2766 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
2769 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
2770 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
2772 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
2775 /* Always add default sorting columns */
2776 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
2777 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
2779 cb
.cb_sortcol
= sortcol
;
2780 cb
.cb_numname
= prtnum
;
2781 cb
.cb_nicenum
= !parsable
;
2782 cb
.cb_avl_pool
= avl_pool
;
2783 cb
.cb_avl
= avl_tree
;
2784 cb
.cb_sid2posix
= sid2posix
;
2786 for (i
= 0; i
< USFIELD_LAST
; i
++)
2787 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
2789 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
2790 if (((p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
) &&
2791 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
2792 ((p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
) &&
2793 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))))
2796 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
2801 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
2804 us_populated
= B_TRUE
;
2806 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
2807 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
2808 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
2809 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
2811 while (node
!= NULL
) {
2813 node
= uu_avl_next(avl_tree
, node
);
2814 uu_avl_remove(avl_tree
, rmnode
);
2815 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
2816 uu_list_insert(list
, rmnode
, idx2
);
2819 for (node
= uu_list_first(list
); node
!= NULL
;
2820 node
= uu_list_next(list
, node
)) {
2821 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
2823 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
2824 uu_avl_insert(avl_tree
, node
, idx
);
2827 uu_list_destroy(list
);
2828 uu_list_pool_destroy(listpool
);
2830 /* Print and free node nvlist memory */
2831 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
2834 zfs_free_sort_columns(sortcol
);
2836 /* Clean up the AVL tree */
2837 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
2840 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
2841 uu_avl_remove(cb
.cb_avl
, node
);
2845 uu_avl_walk_end(walk
);
2846 uu_avl_destroy(avl_tree
);
2847 uu_avl_pool_destroy(avl_pool
);
2853 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2854 * [-t type[,...]] [filesystem|volume|snapshot] ...
2856 * -H Scripted mode; elide headers and separate columns by tabs.
2857 * -p Display values in parsable (literal) format.
2858 * -r Recurse over all children.
2859 * -d Limit recursion by depth.
2860 * -o Control which fields to display.
2861 * -s Specify sort columns, descending order.
2862 * -S Specify sort columns, ascending order.
2863 * -t Control which object types to display.
2865 * When given no arguments, list all filesystems in the system.
2866 * Otherwise, list the specified datasets, optionally recursing down them if
2867 * '-r' is specified.
2869 typedef struct list_cbdata
{
2871 boolean_t cb_literal
;
2872 boolean_t cb_scripted
;
2873 zprop_list_t
*cb_proplist
;
2877 * Given a list of columns to display, output appropriate headers for each one.
2880 print_header(list_cbdata_t
*cb
)
2882 zprop_list_t
*pl
= cb
->cb_proplist
;
2883 char headerbuf
[ZFS_MAXPROPLEN
];
2886 boolean_t first
= B_TRUE
;
2887 boolean_t right_justify
;
2889 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2896 right_justify
= B_FALSE
;
2897 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2898 header
= zfs_prop_column_name(pl
->pl_prop
);
2899 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2901 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2902 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2903 headerbuf
[i
] = '\0';
2907 if (pl
->pl_next
== NULL
&& !right_justify
)
2908 (void) printf("%s", header
);
2909 else if (right_justify
)
2910 (void) printf("%*s", pl
->pl_width
, header
);
2912 (void) printf("%-*s", pl
->pl_width
, header
);
2915 (void) printf("\n");
2919 * Given a dataset and a list of fields, print out all the properties according
2920 * to the described layout.
2923 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
2925 zprop_list_t
*pl
= cb
->cb_proplist
;
2926 boolean_t first
= B_TRUE
;
2927 char property
[ZFS_MAXPROPLEN
];
2928 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
2931 boolean_t right_justify
;
2933 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2935 if (cb
->cb_scripted
)
2936 (void) printf("\t");
2943 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2944 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
2945 sizeof (property
), NULL
, NULL
, 0,
2946 cb
->cb_literal
) != 0)
2950 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2951 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
2952 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
2953 property
, sizeof (property
), cb
->cb_literal
) != 0)
2957 right_justify
= B_TRUE
;
2958 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
2959 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
2960 property
, sizeof (property
), cb
->cb_literal
) != 0)
2964 right_justify
= B_TRUE
;
2966 if (nvlist_lookup_nvlist(userprops
,
2967 pl
->pl_user_prop
, &propval
) != 0)
2970 verify(nvlist_lookup_string(propval
,
2971 ZPROP_VALUE
, &propstr
) == 0);
2972 right_justify
= B_FALSE
;
2976 * If this is being called in scripted mode, or if this is the
2977 * last column and it is left-justified, don't include a width
2980 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
2981 (void) printf("%s", propstr
);
2982 else if (right_justify
)
2983 (void) printf("%*s", pl
->pl_width
, propstr
);
2985 (void) printf("%-*s", pl
->pl_width
, propstr
);
2988 (void) printf("\n");
2992 * Generic callback function to list a dataset or snapshot.
2995 list_callback(zfs_handle_t
*zhp
, void *data
)
2997 list_cbdata_t
*cbp
= data
;
2999 if (cbp
->cb_first
) {
3000 if (!cbp
->cb_scripted
)
3002 cbp
->cb_first
= B_FALSE
;
3005 print_dataset(zhp
, cbp
);
3011 zfs_do_list(int argc
, char **argv
)
3014 static char default_fields
[] =
3015 "name,used,available,referenced,mountpoint";
3016 int types
= ZFS_TYPE_DATASET
;
3017 boolean_t types_specified
= B_FALSE
;
3018 char *fields
= NULL
;
3019 list_cbdata_t cb
= { 0 };
3023 zfs_sort_column_t
*sortcol
= NULL
;
3024 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3027 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3033 cb
.cb_literal
= B_TRUE
;
3034 flags
|= ZFS_ITER_LITERAL_PROPS
;
3037 limit
= parse_depth(optarg
, &flags
);
3040 flags
|= ZFS_ITER_RECURSE
;
3043 cb
.cb_scripted
= B_TRUE
;
3046 if (zfs_add_sort_column(&sortcol
, optarg
,
3048 (void) fprintf(stderr
,
3049 gettext("invalid property '%s'\n"), optarg
);
3054 if (zfs_add_sort_column(&sortcol
, optarg
,
3056 (void) fprintf(stderr
,
3057 gettext("invalid property '%s'\n"), optarg
);
3063 types_specified
= B_TRUE
;
3064 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3065 while (*optarg
!= '\0') {
3066 static char *type_subopts
[] = { "filesystem",
3067 "volume", "snapshot", "snap", "bookmark",
3070 switch (getsubopt(&optarg
, type_subopts
,
3073 types
|= ZFS_TYPE_FILESYSTEM
;
3076 types
|= ZFS_TYPE_VOLUME
;
3080 types
|= ZFS_TYPE_SNAPSHOT
;
3083 types
|= ZFS_TYPE_BOOKMARK
;
3086 types
= ZFS_TYPE_DATASET
|
3090 (void) fprintf(stderr
,
3091 gettext("invalid type '%s'\n"),
3098 (void) fprintf(stderr
, gettext("missing argument for "
3099 "'%c' option\n"), optopt
);
3103 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3113 fields
= default_fields
;
3116 * If "-o space" and no types were specified, don't display snapshots.
3118 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3119 types
&= ~ZFS_TYPE_SNAPSHOT
;
3122 * If the user specifies '-o all', the zprop_get_list() doesn't
3123 * normally include the name of the dataset. For 'zfs list', we always
3124 * want this property to be first.
3126 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3130 cb
.cb_first
= B_TRUE
;
3132 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3133 limit
, list_callback
, &cb
);
3135 zprop_free_list(cb
.cb_proplist
);
3136 zfs_free_sort_columns(sortcol
);
3138 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3139 (void) printf(gettext("no datasets available\n"));
3145 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3146 * zfs rename [-f] -p <fs | vol> <fs | vol>
3147 * zfs rename -r <snap> <snap>
3149 * Renames the given dataset to another of the same type.
3151 * The '-p' flag creates all the non-existing ancestors of the target first.
3155 zfs_do_rename(int argc
, char **argv
)
3160 boolean_t recurse
= B_FALSE
;
3161 boolean_t parents
= B_FALSE
;
3162 boolean_t force_unmount
= B_FALSE
;
3165 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3174 force_unmount
= B_TRUE
;
3178 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3187 /* check number of arguments */
3189 (void) fprintf(stderr
, gettext("missing source dataset "
3194 (void) fprintf(stderr
, gettext("missing target dataset "
3199 (void) fprintf(stderr
, gettext("too many arguments\n"));
3203 if (recurse
&& parents
) {
3204 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3209 if (recurse
&& strchr(argv
[0], '@') == 0) {
3210 (void) fprintf(stderr
, gettext("source dataset for recursive "
3211 "rename must be a snapshot\n"));
3215 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3216 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3219 /* If we were asked and the name looks good, try to create ancestors. */
3220 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3221 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3226 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3235 * Promotes the given clone fs to be the parent
3239 zfs_do_promote(int argc
, char **argv
)
3245 if (argc
> 1 && argv
[1][0] == '-') {
3246 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3251 /* check number of arguments */
3253 (void) fprintf(stderr
, gettext("missing clone filesystem"
3258 (void) fprintf(stderr
, gettext("too many arguments\n"));
3262 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3266 ret
= (zfs_promote(zhp
) != 0);
3274 * zfs rollback [-rRf] <snapshot>
3276 * -r Delete any intervening snapshots before doing rollback
3277 * -R Delete any snapshots and their clones
3278 * -f ignored for backwards compatability
3280 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3281 * since then and making it the active dataset. If more recent snapshots exist,
3282 * the command will complain unless the '-r' flag is given.
3284 typedef struct rollback_cbdata
{
3290 boolean_t cb_recurse
;
3291 } rollback_cbdata_t
;
3294 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3296 rollback_cbdata_t
*cbp
= data
;
3298 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3299 (void) fprintf(stderr
, gettext("cannot rollback to "
3300 "'%s': clones of previous snapshots exist\n"),
3302 (void) fprintf(stderr
, gettext("use '-R' to "
3303 "force deletion of the following clones and "
3309 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3316 * Report any snapshots more recent than the one specified. Used when '-r' is
3317 * not specified. We reuse this same callback for the snapshot dependents - if
3318 * 'cb_dependent' is set, then this is a dependent and we should report it
3319 * without checking the transaction group.
3322 rollback_check(zfs_handle_t
*zhp
, void *data
)
3324 rollback_cbdata_t
*cbp
= data
;
3326 if (cbp
->cb_doclones
) {
3331 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3332 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3333 (void) fprintf(stderr
, gettext("cannot "
3334 "rollback to '%s': more recent snapshots "
3335 "or bookmarks exist\n"),
3337 (void) fprintf(stderr
, gettext("use '-r' to "
3338 "force deletion of the following "
3339 "snapshots and bookmarks:\n"));
3344 if (cbp
->cb_recurse
) {
3345 if (zfs_iter_dependents(zhp
, B_TRUE
,
3346 rollback_check_dependent
, cbp
) != 0) {
3351 (void) fprintf(stderr
, "%s\n",
3360 zfs_do_rollback(int argc
, char **argv
)
3364 boolean_t force
= B_FALSE
;
3365 rollback_cbdata_t cb
= { 0 };
3366 zfs_handle_t
*zhp
, *snap
;
3367 char parentname
[ZFS_MAXNAMELEN
];
3371 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3384 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3393 /* check number of arguments */
3395 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3399 (void) fprintf(stderr
, gettext("too many arguments\n"));
3403 /* open the snapshot */
3404 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3407 /* open the parent dataset */
3408 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3409 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3411 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3417 * Check for more recent snapshots and/or clones based on the presence
3420 cb
.cb_target
= argv
[0];
3421 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3422 cb
.cb_first
= B_TRUE
;
3424 if ((ret
= zfs_iter_snapshots(zhp
, rollback_check
, &cb
)) != 0)
3426 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
3429 if ((ret
= cb
.cb_error
) != 0)
3433 * Rollback parent to the given snapshot.
3435 ret
= zfs_rollback(zhp
, snap
, force
);
3448 * zfs set property=value ... { fs | snap | vol } ...
3450 * Sets the given properties for all datasets specified on the command line.
3454 set_callback(zfs_handle_t
*zhp
, void *data
)
3456 nvlist_t
*props
= data
;
3458 if (zfs_prop_set_list(zhp
, props
) != 0) {
3459 switch (libzfs_errno(g_zfs
)) {
3460 case EZFS_MOUNTFAILED
:
3461 (void) fprintf(stderr
, gettext("property may be set "
3462 "but unable to remount filesystem\n"));
3464 case EZFS_SHARENFSFAILED
:
3465 (void) fprintf(stderr
, gettext("property may be set "
3466 "but unable to reshare filesystem\n"));
3475 zfs_do_set(int argc
, char **argv
)
3477 nvlist_t
*props
= NULL
;
3478 int ds_start
= -1; /* argv idx of first dataset arg */
3481 /* check for options */
3482 if (argc
> 1 && argv
[1][0] == '-') {
3483 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3488 /* check number of arguments */
3490 (void) fprintf(stderr
, gettext("missing arguments\n"));
3494 if (strchr(argv
[1], '=') == NULL
) {
3495 (void) fprintf(stderr
, gettext("missing property=value "
3498 (void) fprintf(stderr
, gettext("missing dataset "
3504 /* validate argument order: prop=val args followed by dataset args */
3505 for (int i
= 1; i
< argc
; i
++) {
3506 if (strchr(argv
[i
], '=') != NULL
) {
3508 /* out-of-order prop=val argument */
3509 (void) fprintf(stderr
, gettext("invalid "
3510 "argument order\n"), i
);
3513 } else if (ds_start
< 0) {
3518 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
3522 /* Populate a list of property settings */
3523 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3525 for (int i
= 1; i
< ds_start
; i
++) {
3526 if ((ret
= parseprop(props
, argv
[i
])) != 0)
3530 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
3531 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, props
);
3538 typedef struct snap_cbdata
{
3540 boolean_t sd_recursive
;
3541 const char *sd_snapname
;
3545 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3547 snap_cbdata_t
*sd
= arg
;
3552 if (sd
->sd_recursive
&&
3553 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
3558 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3561 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3564 if (sd
->sd_recursive
)
3565 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3571 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3573 * Creates a snapshot with the given name. While functionally equivalent to
3574 * 'zfs create', it is a separate command to differentiate intent.
3577 zfs_do_snapshot(int argc
, char **argv
)
3582 snap_cbdata_t sd
= { 0 };
3583 boolean_t multiple_snaps
= B_FALSE
;
3585 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3587 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3591 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3594 if (parseprop(props
, optarg
) != 0)
3598 sd
.sd_recursive
= B_TRUE
;
3599 multiple_snaps
= B_TRUE
;
3602 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3611 /* check number of arguments */
3613 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3618 multiple_snaps
= B_TRUE
;
3619 for (; argc
> 0; argc
--, argv
++) {
3623 atp
= strchr(argv
[0], '@');
3627 sd
.sd_snapname
= atp
+ 1;
3628 zhp
= zfs_open(g_zfs
, argv
[0],
3629 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3632 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3636 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3637 nvlist_free(sd
.sd_nvl
);
3639 if (ret
!= 0 && multiple_snaps
)
3640 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3644 nvlist_free(sd
.sd_nvl
);
3651 * Send a backup stream to stdout.
3654 zfs_do_send(int argc
, char **argv
)
3656 char *fromname
= NULL
;
3657 char *toname
= NULL
;
3660 sendflags_t flags
= { 0 };
3662 nvlist_t
*dbgnv
= NULL
;
3663 boolean_t extraverbose
= B_FALSE
;
3666 while ((c
= getopt(argc
, argv
, ":i:I:RDpvnPLe")) != -1) {
3677 flags
.doall
= B_TRUE
;
3680 flags
.replicate
= B_TRUE
;
3683 flags
.props
= B_TRUE
;
3686 flags
.parsable
= B_TRUE
;
3687 flags
.verbose
= B_TRUE
;
3691 extraverbose
= B_TRUE
;
3692 flags
.verbose
= B_TRUE
;
3693 flags
.progress
= B_TRUE
;
3696 flags
.dedup
= B_TRUE
;
3699 flags
.dryrun
= B_TRUE
;
3702 flags
.largeblock
= B_TRUE
;
3705 flags
.embed_data
= B_TRUE
;
3708 (void) fprintf(stderr
, gettext("missing argument for "
3709 "'%c' option\n"), optopt
);
3713 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3722 /* check number of arguments */
3724 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3728 (void) fprintf(stderr
, gettext("too many arguments\n"));
3732 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
3733 (void) fprintf(stderr
,
3734 gettext("Error: Stream can not be written to a terminal.\n"
3735 "You must redirect standard output.\n"));
3740 * Special case sending a filesystem, or from a bookmark.
3742 if (strchr(argv
[0], '@') == NULL
||
3743 (fromname
&& strchr(fromname
, '#') != NULL
)) {
3744 char frombuf
[ZFS_MAXNAMELEN
];
3745 enum lzc_send_flags lzc_flags
= 0;
3747 if (flags
.replicate
|| flags
.doall
|| flags
.props
||
3748 flags
.dedup
|| flags
.dryrun
|| flags
.verbose
||
3750 (void) fprintf(stderr
,
3752 "Unsupported flag with filesystem or bookmark.\n"));
3756 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
3760 if (flags
.largeblock
)
3761 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
3762 if (flags
.embed_data
)
3763 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
3765 if (fromname
!= NULL
&&
3766 (fromname
[0] == '#' || fromname
[0] == '@')) {
3768 * Incremental source name begins with # or @.
3769 * Default to same fs as target.
3771 (void) strncpy(frombuf
, argv
[0], sizeof (frombuf
));
3772 cp
= strchr(frombuf
, '@');
3775 (void) strlcat(frombuf
, fromname
, sizeof (frombuf
));
3778 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, lzc_flags
);
3783 cp
= strchr(argv
[0], '@');
3786 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3791 * If they specified the full path to the snapshot, chop off
3792 * everything except the short name of the snapshot, but special
3793 * case if they specify the origin.
3795 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
3796 char origin
[ZFS_MAXNAMELEN
];
3799 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
3800 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
3802 if (strcmp(origin
, fromname
) == 0) {
3804 flags
.fromorigin
= B_TRUE
;
3807 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
3808 (void) fprintf(stderr
,
3809 gettext("incremental source must be "
3810 "in same filesystem\n"));
3814 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
3815 (void) fprintf(stderr
,
3816 gettext("invalid incremental source\n"));
3822 if (flags
.replicate
&& fromname
== NULL
)
3823 flags
.doall
= B_TRUE
;
3825 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
3826 extraverbose
? &dbgnv
: NULL
);
3828 if (extraverbose
&& dbgnv
!= NULL
) {
3830 * dump_nvlist prints to stdout, but that's been
3831 * redirected to a file. Make it print to stderr
3834 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
3835 dump_nvlist(dbgnv
, 0);
3844 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3846 * Restore a backup stream from stdin.
3849 zfs_do_receive(int argc
, char **argv
)
3852 recvflags_t flags
= { 0 };
3854 nvpair_t
*nvp
= NULL
;
3856 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3860 while ((c
= getopt(argc
, argv
, ":o:denuvF")) != -1) {
3863 if (parseprop(props
, optarg
) != 0)
3867 flags
.isprefix
= B_TRUE
;
3870 flags
.isprefix
= B_TRUE
;
3871 flags
.istail
= B_TRUE
;
3874 flags
.dryrun
= B_TRUE
;
3877 flags
.nomount
= B_TRUE
;
3880 flags
.verbose
= B_TRUE
;
3883 flags
.force
= B_TRUE
;
3886 (void) fprintf(stderr
, gettext("missing argument for "
3887 "'%c' option\n"), optopt
);
3891 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3900 /* check number of arguments */
3902 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3906 (void) fprintf(stderr
, gettext("too many arguments\n"));
3910 while ((nvp
= nvlist_next_nvpair(props
, nvp
))) {
3911 if (strcmp(nvpair_name(nvp
), "origin") != 0) {
3912 (void) fprintf(stderr
, gettext("invalid option"));
3917 if (isatty(STDIN_FILENO
)) {
3918 (void) fprintf(stderr
,
3919 gettext("Error: Backup stream can not be read "
3920 "from a terminal.\n"
3921 "You must redirect standard input.\n"));
3925 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
3931 * allow/unallow stuff
3933 /* copied from zfs/sys/dsl_deleg.h */
3934 #define ZFS_DELEG_PERM_CREATE "create"
3935 #define ZFS_DELEG_PERM_DESTROY "destroy"
3936 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3937 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3938 #define ZFS_DELEG_PERM_CLONE "clone"
3939 #define ZFS_DELEG_PERM_PROMOTE "promote"
3940 #define ZFS_DELEG_PERM_RENAME "rename"
3941 #define ZFS_DELEG_PERM_MOUNT "mount"
3942 #define ZFS_DELEG_PERM_SHARE "share"
3943 #define ZFS_DELEG_PERM_SEND "send"
3944 #define ZFS_DELEG_PERM_RECEIVE "receive"
3945 #define ZFS_DELEG_PERM_ALLOW "allow"
3946 #define ZFS_DELEG_PERM_USERPROP "userprop"
3947 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3948 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3949 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3950 #define ZFS_DELEG_PERM_USERUSED "userused"
3951 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3952 #define ZFS_DELEG_PERM_HOLD "hold"
3953 #define ZFS_DELEG_PERM_RELEASE "release"
3954 #define ZFS_DELEG_PERM_DIFF "diff"
3955 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
3957 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3959 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
3960 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
3961 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
3962 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
3963 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
3964 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
3965 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
3966 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
3967 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
3968 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
3969 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
3970 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
3971 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
3972 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
3973 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
3974 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
3975 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
3977 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
3978 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
3979 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
3980 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
3981 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
3982 { NULL
, ZFS_DELEG_NOTE_NONE
}
3985 /* permission structure */
3986 typedef struct deleg_perm
{
3987 zfs_deleg_who_type_t dp_who_type
;
3988 const char *dp_name
;
3990 boolean_t dp_descend
;
3994 typedef struct deleg_perm_node
{
3995 deleg_perm_t dpn_perm
;
3997 uu_avl_node_t dpn_avl_node
;
3998 } deleg_perm_node_t
;
4000 typedef struct fs_perm fs_perm_t
;
4002 /* permissions set */
4003 typedef struct who_perm
{
4004 zfs_deleg_who_type_t who_type
;
4005 const char *who_name
; /* id */
4006 char who_ug_name
[256]; /* user/group name */
4007 fs_perm_t
*who_fsperm
; /* uplink */
4009 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
4013 typedef struct who_perm_node
{
4014 who_perm_t who_perm
;
4015 uu_avl_node_t who_avl_node
;
4018 typedef struct fs_perm_set fs_perm_set_t
;
4019 /* fs permissions */
4021 const char *fsp_name
;
4023 uu_avl_t
*fsp_sc_avl
; /* sets,create */
4024 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
4026 fs_perm_set_t
*fsp_set
; /* uplink */
4030 typedef struct fs_perm_node
{
4031 fs_perm_t fspn_fsperm
;
4034 uu_list_node_t fspn_list_node
;
4037 /* top level structure */
4038 struct fs_perm_set
{
4039 uu_list_pool_t
*fsps_list_pool
;
4040 uu_list_t
*fsps_list
; /* list of fs_perms */
4042 uu_avl_pool_t
*fsps_named_set_avl_pool
;
4043 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
4044 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
4047 static inline const char *
4048 deleg_perm_type(zfs_deleg_note_t note
)
4054 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4055 case ZFS_DELEG_NOTE_GROUPUSED
:
4056 case ZFS_DELEG_NOTE_USERPROP
:
4057 case ZFS_DELEG_NOTE_USERQUOTA
:
4058 case ZFS_DELEG_NOTE_USERUSED
:
4060 return (gettext("other"));
4062 return (gettext("subcommand"));
4067 who_type2weight(zfs_deleg_who_type_t who_type
)
4071 case ZFS_DELEG_NAMED_SET_SETS
:
4072 case ZFS_DELEG_NAMED_SET
:
4075 case ZFS_DELEG_CREATE_SETS
:
4076 case ZFS_DELEG_CREATE
:
4079 case ZFS_DELEG_USER_SETS
:
4080 case ZFS_DELEG_USER
:
4083 case ZFS_DELEG_GROUP_SETS
:
4084 case ZFS_DELEG_GROUP
:
4087 case ZFS_DELEG_EVERYONE_SETS
:
4088 case ZFS_DELEG_EVERYONE
:
4100 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4102 const who_perm_node_t
*l
= larg
;
4103 const who_perm_node_t
*r
= rarg
;
4104 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
4105 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
4106 int lweight
= who_type2weight(ltype
);
4107 int rweight
= who_type2weight(rtype
);
4108 int res
= lweight
- rweight
;
4110 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
4111 ZFS_MAX_DELEG_NAME
-1);
4123 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4125 const deleg_perm_node_t
*l
= larg
;
4126 const deleg_perm_node_t
*r
= rarg
;
4127 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
4128 ZFS_MAX_DELEG_NAME
-1);
4140 fs_perm_set_init(fs_perm_set_t
*fspset
)
4142 bzero(fspset
, sizeof (fs_perm_set_t
));
4144 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4145 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4146 NULL
, UU_DEFAULT
)) == NULL
)
4148 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4149 UU_DEFAULT
)) == NULL
)
4152 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4153 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4154 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4155 UU_DEFAULT
)) == NULL
)
4158 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4159 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4160 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4161 UU_DEFAULT
)) == NULL
)
4164 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4165 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4166 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4171 static inline void fs_perm_fini(fs_perm_t
*);
4172 static inline void who_perm_fini(who_perm_t
*);
4175 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4177 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4179 while (node
!= NULL
) {
4180 fs_perm_node_t
*next_node
=
4181 uu_list_next(fspset
->fsps_list
, node
);
4182 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4183 fs_perm_fini(fsperm
);
4184 uu_list_remove(fspset
->fsps_list
, node
);
4189 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4190 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4191 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4195 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4198 deleg_perm
->dp_who_type
= type
;
4199 deleg_perm
->dp_name
= name
;
4203 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4204 zfs_deleg_who_type_t type
, const char *name
)
4206 uu_avl_pool_t
*pool
;
4207 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4209 bzero(who_perm
, sizeof (who_perm_t
));
4211 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4212 UU_DEFAULT
)) == NULL
)
4215 who_perm
->who_type
= type
;
4216 who_perm
->who_name
= name
;
4217 who_perm
->who_fsperm
= fsperm
;
4221 who_perm_fini(who_perm_t
*who_perm
)
4223 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4225 while (node
!= NULL
) {
4226 deleg_perm_node_t
*next_node
=
4227 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4229 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4234 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4238 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4240 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4241 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4243 bzero(fsperm
, sizeof (fs_perm_t
));
4245 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4249 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4253 fsperm
->fsp_set
= fspset
;
4254 fsperm
->fsp_name
= fsname
;
4258 fs_perm_fini(fs_perm_t
*fsperm
)
4260 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4261 while (node
!= NULL
) {
4262 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4264 who_perm_t
*who_perm
= &node
->who_perm
;
4265 who_perm_fini(who_perm
);
4266 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4271 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4272 while (node
!= NULL
) {
4273 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4275 who_perm_t
*who_perm
= &node
->who_perm
;
4276 who_perm_fini(who_perm
);
4277 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4282 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4283 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4287 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4288 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4290 uu_avl_index_t idx
= 0;
4292 deleg_perm_node_t
*found_node
= NULL
;
4293 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4295 deleg_perm_init(deleg_perm
, who_type
, name
);
4297 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4299 uu_avl_insert(avl
, node
, idx
);
4302 deleg_perm
= &node
->dpn_perm
;
4307 case ZFS_DELEG_LOCAL
:
4308 deleg_perm
->dp_local
= B_TRUE
;
4310 case ZFS_DELEG_DESCENDENT
:
4311 deleg_perm
->dp_descend
= B_TRUE
;
4316 assert(B_FALSE
); /* invalid locality */
4321 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4323 nvpair_t
*nvp
= NULL
;
4324 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4325 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4326 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4328 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4329 const char *name
= nvpair_name(nvp
);
4330 data_type_t type
= nvpair_type(nvp
);
4331 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4332 deleg_perm_node_t
*node
=
4333 safe_malloc(sizeof (deleg_perm_node_t
));
4335 assert(type
== DATA_TYPE_BOOLEAN
);
4337 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4338 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4345 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4347 nvpair_t
*nvp
= NULL
;
4348 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4350 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4351 nvlist_t
*nvl2
= NULL
;
4352 const char *name
= nvpair_name(nvp
);
4353 uu_avl_t
*avl
= NULL
;
4354 uu_avl_pool_t
*avl_pool
;
4355 zfs_deleg_who_type_t perm_type
= name
[0];
4356 char perm_locality
= name
[1];
4357 const char *perm_name
= name
+ 3;
4358 boolean_t is_set
= B_TRUE
;
4359 who_perm_t
*who_perm
= NULL
;
4361 assert('$' == name
[2]);
4363 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4366 switch (perm_type
) {
4367 case ZFS_DELEG_CREATE
:
4368 case ZFS_DELEG_CREATE_SETS
:
4369 case ZFS_DELEG_NAMED_SET
:
4370 case ZFS_DELEG_NAMED_SET_SETS
:
4371 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4372 avl
= fsperm
->fsp_sc_avl
;
4374 case ZFS_DELEG_USER
:
4375 case ZFS_DELEG_USER_SETS
:
4376 case ZFS_DELEG_GROUP
:
4377 case ZFS_DELEG_GROUP_SETS
:
4378 case ZFS_DELEG_EVERYONE
:
4379 case ZFS_DELEG_EVERYONE_SETS
:
4380 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4381 avl
= fsperm
->fsp_uge_avl
;
4386 who_perm_node_t
*found_node
= NULL
;
4387 who_perm_node_t
*node
= safe_malloc(
4388 sizeof (who_perm_node_t
));
4389 who_perm
= &node
->who_perm
;
4390 uu_avl_index_t idx
= 0;
4392 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4393 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4395 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4397 if (avl
== fsperm
->fsp_uge_avl
) {
4399 struct passwd
*p
= NULL
;
4400 struct group
*g
= NULL
;
4401 const char *nice_name
= NULL
;
4403 switch (perm_type
) {
4404 case ZFS_DELEG_USER_SETS
:
4405 case ZFS_DELEG_USER
:
4406 rid
= atoi(perm_name
);
4409 nice_name
= p
->pw_name
;
4411 case ZFS_DELEG_GROUP_SETS
:
4412 case ZFS_DELEG_GROUP
:
4413 rid
= atoi(perm_name
);
4416 nice_name
= g
->gr_name
;
4420 if (nice_name
!= NULL
)
4422 node
->who_perm
.who_ug_name
,
4426 uu_avl_insert(avl
, node
, idx
);
4429 who_perm
= &node
->who_perm
;
4433 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4440 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4442 nvpair_t
*nvp
= NULL
;
4443 uu_avl_index_t idx
= 0;
4445 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4446 nvlist_t
*nvl2
= NULL
;
4447 const char *fsname
= nvpair_name(nvp
);
4448 data_type_t type
= nvpair_type(nvp
);
4449 fs_perm_t
*fsperm
= NULL
;
4450 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4454 fsperm
= &node
->fspn_fsperm
;
4456 assert(DATA_TYPE_NVLIST
== type
);
4458 uu_list_node_init(node
, &node
->fspn_list_node
,
4459 fspset
->fsps_list_pool
);
4461 idx
= uu_list_numnodes(fspset
->fsps_list
);
4462 fs_perm_init(fsperm
, fspset
, fsname
);
4464 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4467 (void) parse_fs_perm(fsperm
, nvl2
);
4469 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4475 static inline const char *
4476 deleg_perm_comment(zfs_deleg_note_t note
)
4478 const char *str
= "";
4483 case ZFS_DELEG_NOTE_ALLOW
:
4484 str
= gettext("Must also have the permission that is being"
4485 "\n\t\t\t\tallowed");
4487 case ZFS_DELEG_NOTE_CLONE
:
4488 str
= gettext("Must also have the 'create' ability and 'mount'"
4489 "\n\t\t\t\tability in the origin file system");
4491 case ZFS_DELEG_NOTE_CREATE
:
4492 str
= gettext("Must also have the 'mount' ability");
4494 case ZFS_DELEG_NOTE_DESTROY
:
4495 str
= gettext("Must also have the 'mount' ability");
4497 case ZFS_DELEG_NOTE_DIFF
:
4498 str
= gettext("Allows lookup of paths within a dataset;"
4499 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4500 "\n\t\t\t\tin order to use zfs diff");
4502 case ZFS_DELEG_NOTE_HOLD
:
4503 str
= gettext("Allows adding a user hold to a snapshot");
4505 case ZFS_DELEG_NOTE_MOUNT
:
4506 str
= gettext("Allows mount/umount of ZFS datasets");
4508 case ZFS_DELEG_NOTE_PROMOTE
:
4509 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4510 " 'promote' ability in the origin file system");
4512 case ZFS_DELEG_NOTE_RECEIVE
:
4513 str
= gettext("Must also have the 'mount' and 'create'"
4516 case ZFS_DELEG_NOTE_RELEASE
:
4517 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4518 "might destroy the snapshot");
4520 case ZFS_DELEG_NOTE_RENAME
:
4521 str
= gettext("Must also have the 'mount' and 'create'"
4522 "\n\t\t\t\tability in the new parent");
4524 case ZFS_DELEG_NOTE_ROLLBACK
:
4527 case ZFS_DELEG_NOTE_SEND
:
4530 case ZFS_DELEG_NOTE_SHARE
:
4531 str
= gettext("Allows sharing file systems over NFS or SMB"
4532 "\n\t\t\t\tprotocols");
4534 case ZFS_DELEG_NOTE_SNAPSHOT
:
4538 * case ZFS_DELEG_NOTE_VSCAN:
4539 * str = gettext("");
4543 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4544 str
= gettext("Allows accessing any groupquota@... property");
4546 case ZFS_DELEG_NOTE_GROUPUSED
:
4547 str
= gettext("Allows reading any groupused@... property");
4549 case ZFS_DELEG_NOTE_USERPROP
:
4550 str
= gettext("Allows changing any user property");
4552 case ZFS_DELEG_NOTE_USERQUOTA
:
4553 str
= gettext("Allows accessing any userquota@... property");
4555 case ZFS_DELEG_NOTE_USERUSED
:
4556 str
= gettext("Allows reading any userused@... property");
4574 boolean_t recursive
; /* unallow only */
4575 boolean_t prt_usage
;
4577 boolean_t prt_perms
;
4580 const char *dataset
;
4584 prop_cmp(const void *a
, const void *b
)
4586 const char *str1
= *(const char **)a
;
4587 const char *str2
= *(const char **)b
;
4588 return (strcmp(str1
, str2
));
4592 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
4594 const char *opt_desc
[] = {
4595 "-h", gettext("show this help message and exit"),
4596 "-l", gettext("set permission locally"),
4597 "-d", gettext("set permission for descents"),
4598 "-u", gettext("set permission for user"),
4599 "-g", gettext("set permission for group"),
4600 "-e", gettext("set permission for everyone"),
4601 "-c", gettext("set create time permission"),
4602 "-s", gettext("define permission set"),
4604 "-r", gettext("remove permissions recursively"),
4606 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
4607 size_t allow_size
= unallow_size
- 2;
4608 const char *props
[ZFS_NUM_PROPS
];
4611 FILE *fp
= requested
? stdout
: stderr
;
4612 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
4613 const char *fmt
= gettext("%-16s %-14s\t%s\n");
4615 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
4617 (void) fprintf(fp
, gettext("Options:\n"));
4618 for (int i
= 0; i
< (un
? unallow_size
: allow_size
); i
++) {
4619 const char *opt
= opt_desc
[i
++];
4620 const char *optdsc
= opt_desc
[i
];
4621 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
4624 (void) fprintf(fp
, gettext("\nThe following permissions are "
4626 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
4628 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
4629 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
4630 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
4631 const char *perm_type
= deleg_perm_type(perm_note
);
4632 const char *perm_comment
= deleg_perm_comment(perm_note
);
4633 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
4636 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
4637 zprop_desc_t
*pd
= &pdtbl
[i
];
4638 if (pd
->pd_visible
!= B_TRUE
)
4641 if (pd
->pd_attr
== PROP_READONLY
)
4644 props
[count
++] = pd
->pd_name
;
4646 props
[count
] = NULL
;
4648 qsort(props
, count
, sizeof (char *), prop_cmp
);
4650 for (i
= 0; i
< count
; i
++)
4651 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
4654 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
4656 exit(requested
? 0 : 2);
4659 static inline const char *
4660 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
4663 if (un
&& argc
== expected_argc
- 1)
4665 else if (argc
== expected_argc
)
4666 *permsp
= argv
[argc
- 2];
4668 allow_usage(un
, B_FALSE
,
4669 gettext("wrong number of parameters\n"));
4671 return (argv
[argc
- 1]);
4675 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
4677 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
4678 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
4679 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
4680 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
4683 allow_usage(un
, B_FALSE
,
4684 gettext("-u, -g, and -e are mutually exclusive\n"));
4686 if (opts
->prt_usage
)
4687 if (argc
== 0 && all_sum
== 0)
4688 allow_usage(un
, B_TRUE
, NULL
);
4694 allow_usage(un
, B_FALSE
,
4695 gettext("invalid options combined with -s\n"));
4697 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4698 if (argv
[0][0] != '@')
4699 allow_usage(un
, B_FALSE
,
4700 gettext("invalid set name: missing '@' prefix\n"));
4701 opts
->who
= argv
[0];
4702 } else if (opts
->create
) {
4703 if (ldcsuge_sum
> 1)
4704 allow_usage(un
, B_FALSE
,
4705 gettext("invalid options combined with -c\n"));
4706 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4707 } else if (opts
->everyone
) {
4709 allow_usage(un
, B_FALSE
,
4710 gettext("invalid options combined with -e\n"));
4711 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4712 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
4714 opts
->everyone
= B_TRUE
;
4717 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4718 } else if (argc
== 1 && !un
) {
4719 opts
->prt_perms
= B_TRUE
;
4720 opts
->dataset
= argv
[argc
-1];
4722 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4723 opts
->who
= argv
[0];
4726 if (!opts
->local
&& !opts
->descend
) {
4727 opts
->local
= B_TRUE
;
4728 opts
->descend
= B_TRUE
;
4733 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
4734 const char *who
, char *perms
, nvlist_t
*top_nvl
)
4737 char ld
[2] = { '\0', '\0' };
4738 char who_buf
[ZFS_MAXNAMELEN
+32];
4741 nvlist_t
*base_nvl
= NULL
;
4742 nvlist_t
*set_nvl
= NULL
;
4745 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4747 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4751 case ZFS_DELEG_NAMED_SET_SETS
:
4752 case ZFS_DELEG_NAMED_SET
:
4753 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
4754 base_type
= ZFS_DELEG_NAMED_SET
;
4755 ld
[0] = ZFS_DELEG_NA
;
4757 case ZFS_DELEG_CREATE_SETS
:
4758 case ZFS_DELEG_CREATE
:
4759 set_type
= ZFS_DELEG_CREATE_SETS
;
4760 base_type
= ZFS_DELEG_CREATE
;
4761 ld
[0] = ZFS_DELEG_NA
;
4763 case ZFS_DELEG_USER_SETS
:
4764 case ZFS_DELEG_USER
:
4765 set_type
= ZFS_DELEG_USER_SETS
;
4766 base_type
= ZFS_DELEG_USER
;
4768 ld
[0] = ZFS_DELEG_LOCAL
;
4770 ld
[1] = ZFS_DELEG_DESCENDENT
;
4772 case ZFS_DELEG_GROUP_SETS
:
4773 case ZFS_DELEG_GROUP
:
4774 set_type
= ZFS_DELEG_GROUP_SETS
;
4775 base_type
= ZFS_DELEG_GROUP
;
4777 ld
[0] = ZFS_DELEG_LOCAL
;
4779 ld
[1] = ZFS_DELEG_DESCENDENT
;
4781 case ZFS_DELEG_EVERYONE_SETS
:
4782 case ZFS_DELEG_EVERYONE
:
4783 set_type
= ZFS_DELEG_EVERYONE_SETS
;
4784 base_type
= ZFS_DELEG_EVERYONE
;
4786 ld
[0] = ZFS_DELEG_LOCAL
;
4788 ld
[1] = ZFS_DELEG_DESCENDENT
;
4791 if (perms
!= NULL
) {
4793 char *end
= curr
+ strlen(perms
);
4795 while (curr
< end
) {
4796 char *delim
= strchr(curr
, ',');
4807 (void) nvlist_add_boolean(nvl
, curr
);
4813 for (i
= 0; i
< 2; i
++) {
4814 char locality
= ld
[i
];
4818 if (!nvlist_empty(base_nvl
)) {
4820 (void) snprintf(who_buf
,
4821 sizeof (who_buf
), "%c%c$%s",
4822 base_type
, locality
, who
);
4824 (void) snprintf(who_buf
,
4825 sizeof (who_buf
), "%c%c$",
4826 base_type
, locality
);
4828 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
4833 if (!nvlist_empty(set_nvl
)) {
4835 (void) snprintf(who_buf
,
4836 sizeof (who_buf
), "%c%c$%s",
4837 set_type
, locality
, who
);
4839 (void) snprintf(who_buf
,
4840 sizeof (who_buf
), "%c%c$",
4841 set_type
, locality
);
4843 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
4848 for (i
= 0; i
< 2; i
++) {
4849 char locality
= ld
[i
];
4854 (void) snprintf(who_buf
, sizeof (who_buf
),
4855 "%c%c$%s", base_type
, locality
, who
);
4857 (void) snprintf(who_buf
, sizeof (who_buf
),
4858 "%c%c$", base_type
, locality
);
4859 (void) nvlist_add_boolean(top_nvl
, who_buf
);
4862 (void) snprintf(who_buf
, sizeof (who_buf
),
4863 "%c%c$%s", set_type
, locality
, who
);
4865 (void) snprintf(who_buf
, sizeof (who_buf
),
4866 "%c%c$", set_type
, locality
);
4867 (void) nvlist_add_boolean(top_nvl
, who_buf
);
4873 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
4875 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
4879 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
4880 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
4881 } else if (opts
->create
) {
4882 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
4883 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
4884 } else if (opts
->everyone
) {
4885 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
4886 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
4888 char *curr
= opts
->who
;
4889 char *end
= curr
+ strlen(curr
);
4891 while (curr
< end
) {
4893 zfs_deleg_who_type_t who_type
;
4895 char *delim
= strchr(curr
, ',');
4898 struct passwd
*p
= NULL
;
4899 struct group
*g
= NULL
;
4907 rid
= (uid_t
)strtol(curr
, &endch
, 0);
4909 who_type
= ZFS_DELEG_USER
;
4918 (void) snprintf(errbuf
, 256, gettext(
4919 "invalid user %s"), curr
);
4920 allow_usage(un
, B_TRUE
, errbuf
);
4922 } else if (opts
->group
) {
4923 who_type
= ZFS_DELEG_GROUP
;
4932 (void) snprintf(errbuf
, 256, gettext(
4933 "invalid group %s"), curr
);
4934 allow_usage(un
, B_TRUE
, errbuf
);
4937 if (*endch
!= '\0') {
4944 if (*endch
!= '\0') {
4951 who_type
= ZFS_DELEG_USER
;
4953 } else if (g
!= NULL
) {
4954 who_type
= ZFS_DELEG_GROUP
;
4957 (void) snprintf(errbuf
, 256, gettext(
4958 "invalid user/group %s"), curr
);
4959 allow_usage(un
, B_TRUE
, errbuf
);
4963 (void) sprintf(id
, "%u", rid
);
4966 store_allow_perm(who_type
, opts
->local
,
4967 opts
->descend
, who
, opts
->perms
, *nvlp
);
4976 print_set_creat_perms(uu_avl_t
*who_avl
)
4978 const char *sc_title
[] = {
4979 gettext("Permission sets:\n"),
4980 gettext("Create time permissions:\n"),
4983 const char **title_ptr
= sc_title
;
4984 who_perm_node_t
*who_node
= NULL
;
4985 int prev_weight
= -1;
4987 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
4988 who_node
= uu_avl_next(who_avl
, who_node
)) {
4989 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
4990 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
4991 const char *who_name
= who_node
->who_perm
.who_name
;
4992 int weight
= who_type2weight(who_type
);
4993 boolean_t first
= B_TRUE
;
4994 deleg_perm_node_t
*deleg_node
;
4996 if (prev_weight
!= weight
) {
4997 (void) printf(*title_ptr
++);
4998 prev_weight
= weight
;
5001 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
5002 (void) printf("\t");
5004 (void) printf("\t%s ", who_name
);
5006 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
5007 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5010 deleg_node
->dpn_perm
.dp_name
);
5013 (void) printf(",%s",
5014 deleg_node
->dpn_perm
.dp_name
);
5017 (void) printf("\n");
5022 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
5025 who_perm_node_t
*who_node
= NULL
;
5026 boolean_t prt_title
= B_TRUE
;
5027 uu_avl_walk_t
*walk
;
5029 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
5032 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
5033 const char *who_name
= who_node
->who_perm
.who_name
;
5034 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
5035 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5036 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5038 deleg_perm_node_t
*deleg_node
;
5039 boolean_t prt_who
= B_TRUE
;
5041 for (deleg_node
= uu_avl_first(avl
);
5043 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5044 if (local
!= deleg_node
->dpn_perm
.dp_local
||
5045 descend
!= deleg_node
->dpn_perm
.dp_descend
)
5049 const char *who
= NULL
;
5051 prt_title
= B_FALSE
;
5052 (void) printf(title
);
5056 case ZFS_DELEG_USER_SETS
:
5057 case ZFS_DELEG_USER
:
5058 who
= gettext("user");
5060 who_name
= nice_who_name
;
5062 case ZFS_DELEG_GROUP_SETS
:
5063 case ZFS_DELEG_GROUP
:
5064 who
= gettext("group");
5066 who_name
= nice_who_name
;
5068 case ZFS_DELEG_EVERYONE_SETS
:
5069 case ZFS_DELEG_EVERYONE
:
5070 who
= gettext("everyone");
5075 if (who_name
== NULL
)
5076 (void) printf("\t%s", who
);
5078 (void) printf("\t%s %s", who
, who_name
);
5081 (void) printf("%c%s", delim
,
5082 deleg_node
->dpn_perm
.dp_name
);
5087 (void) printf("\n");
5090 uu_avl_walk_end(walk
);
5094 print_fs_perms(fs_perm_set_t
*fspset
)
5096 fs_perm_node_t
*node
= NULL
;
5097 char buf
[ZFS_MAXNAMELEN
+32];
5098 const char *dsname
= buf
;
5100 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
5101 node
= uu_list_next(fspset
->fsps_list
, node
)) {
5102 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
5103 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
5106 (void) snprintf(buf
, ZFS_MAXNAMELEN
+32,
5107 gettext("---- Permissions on %s "),
5108 node
->fspn_fsperm
.fsp_name
);
5109 (void) printf(dsname
);
5110 left
= 70 - strlen(buf
);
5113 (void) printf("\n");
5115 print_set_creat_perms(sc_avl
);
5116 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
5117 gettext("Local permissions:\n"));
5118 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
5119 gettext("Descendent permissions:\n"));
5120 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
5121 gettext("Local+Descendent permissions:\n"));
5125 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5127 struct deleg_perms
{
5133 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5135 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5136 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5138 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5141 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5145 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5148 nvlist_t
*perm_nvl
= NULL
;
5149 nvlist_t
*update_perm_nvl
= NULL
;
5152 struct allow_opts opts
= { 0 };
5154 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5157 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5160 opts
.local
= B_TRUE
;
5163 opts
.descend
= B_TRUE
;
5169 opts
.group
= B_TRUE
;
5172 opts
.everyone
= B_TRUE
;
5178 opts
.create
= B_TRUE
;
5181 opts
.recursive
= B_TRUE
;
5184 (void) fprintf(stderr
, gettext("missing argument for "
5185 "'%c' option\n"), optopt
);
5189 opts
.prt_usage
= B_TRUE
;
5192 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5201 /* check arguments */
5202 parse_allow_args(argc
, argv
, un
, &opts
);
5204 /* try to open the dataset */
5205 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5206 ZFS_TYPE_VOLUME
)) == NULL
) {
5207 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5212 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5215 fs_perm_set_init(&fs_perm_set
);
5216 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5217 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5222 print_fs_perms(&fs_perm_set
);
5224 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5225 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5228 if (un
&& opts
.recursive
) {
5229 struct deleg_perms data
= { un
, update_perm_nvl
};
5230 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5239 nvlist_free(perm_nvl
);
5240 if (update_perm_nvl
!= NULL
)
5241 nvlist_free(update_perm_nvl
);
5243 fs_perm_set_fini(&fs_perm_set
);
5251 zfs_do_allow(int argc
, char **argv
)
5253 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5257 zfs_do_unallow(int argc
, char **argv
)
5259 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5263 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5268 boolean_t recursive
= B_FALSE
;
5269 const char *opts
= holding
? "rt" : "r";
5273 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5279 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5288 /* check number of arguments */
5296 if (holding
&& tag
[0] == '.') {
5297 /* tags starting with '.' are reserved for libzfs */
5298 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5302 for (i
= 0; i
< argc
; ++i
) {
5304 char parent
[ZFS_MAXNAMELEN
];
5306 char *path
= argv
[i
];
5308 delim
= strchr(path
, '@');
5309 if (delim
== NULL
) {
5310 (void) fprintf(stderr
,
5311 gettext("'%s' is not a snapshot\n"), path
);
5315 (void) strncpy(parent
, path
, delim
- path
);
5316 parent
[delim
- path
] = '\0';
5318 zhp
= zfs_open(g_zfs
, parent
,
5319 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5325 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
5328 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5334 return (errors
!= 0);
5338 * zfs hold [-r] [-t] <tag> <snap> ...
5340 * -r Recursively hold
5342 * Apply a user-hold with the given tag to the list of snapshots.
5345 zfs_do_hold(int argc
, char **argv
)
5347 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5351 * zfs release [-r] <tag> <snap> ...
5353 * -r Recursively release
5355 * Release a user-hold with the given tag from the list of snapshots.
5358 zfs_do_release(int argc
, char **argv
)
5360 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5363 typedef struct holds_cbdata
{
5364 boolean_t cb_recursive
;
5365 const char *cb_snapname
;
5367 size_t cb_max_namelen
;
5368 size_t cb_max_taglen
;
5371 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5372 #define DATETIME_BUF_LEN (32)
5377 print_holds(boolean_t scripted
, size_t nwidth
, size_t tagwidth
, nvlist_t
*nvl
)
5380 nvpair_t
*nvp
= NULL
;
5381 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5385 for (i
= 0; i
< 3; i
++) {
5386 col
= gettext(hdr_cols
[i
]);
5388 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5391 (void) printf("%s\n", col
);
5395 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5396 char *zname
= nvpair_name(nvp
);
5398 nvpair_t
*nvp2
= NULL
;
5399 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5400 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5401 char tsbuf
[DATETIME_BUF_LEN
];
5402 char *tagname
= nvpair_name(nvp2
);
5406 char sep
= scripted
? '\t' : ' ';
5407 size_t sepnum
= scripted
? 1 : 2;
5409 (void) nvpair_value_uint64(nvp2
, &val
);
5411 (void) localtime_r(&time
, &t
);
5412 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5413 gettext(STRFTIME_FMT_STR
), &t
);
5415 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth
, zname
,
5416 sepnum
, sep
, tagwidth
, tagname
, sepnum
, sep
, tsbuf
);
5422 * Generic callback function to list a dataset or snapshot.
5425 holds_callback(zfs_handle_t
*zhp
, void *data
)
5427 holds_cbdata_t
*cbp
= data
;
5428 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5429 nvlist_t
*nvl
= NULL
;
5430 nvpair_t
*nvp
= NULL
;
5431 const char *zname
= zfs_get_name(zhp
);
5432 size_t znamelen
= strnlen(zname
, ZFS_MAXNAMELEN
);
5434 if (cbp
->cb_recursive
) {
5435 const char *snapname
;
5436 char *delim
= strchr(zname
, '@');
5440 snapname
= delim
+ 1;
5441 if (strcmp(cbp
->cb_snapname
, snapname
))
5445 if (zfs_get_holds(zhp
, &nvl
) != 0)
5448 if (znamelen
> cbp
->cb_max_namelen
)
5449 cbp
->cb_max_namelen
= znamelen
;
5451 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5452 const char *tag
= nvpair_name(nvp
);
5453 size_t taglen
= strnlen(tag
, MAXNAMELEN
);
5454 if (taglen
> cbp
->cb_max_taglen
)
5455 cbp
->cb_max_taglen
= taglen
;
5458 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5462 * zfs holds [-r] <snap> ...
5464 * -r Recursively hold
5467 zfs_do_holds(int argc
, char **argv
)
5472 boolean_t scripted
= B_FALSE
;
5473 boolean_t recursive
= B_FALSE
;
5474 const char *opts
= "rH";
5477 int types
= ZFS_TYPE_SNAPSHOT
;
5478 holds_cbdata_t cb
= { 0 };
5485 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5494 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5501 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
5502 flags
|= ZFS_ITER_RECURSE
;
5508 /* check number of arguments */
5512 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5515 for (i
= 0; i
< argc
; ++i
) {
5516 char *snapshot
= argv
[i
];
5518 const char *snapname
;
5520 delim
= strchr(snapshot
, '@');
5521 if (delim
== NULL
) {
5522 (void) fprintf(stderr
,
5523 gettext("'%s' is not a snapshot\n"), snapshot
);
5527 snapname
= delim
+ 1;
5529 snapshot
[delim
- snapshot
] = '\0';
5531 cb
.cb_recursive
= recursive
;
5532 cb
.cb_snapname
= snapname
;
5536 * 1. collect holds data, set format options
5538 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
5539 holds_callback
, &cb
);
5545 * 2. print holds data
5547 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
5549 if (nvlist_empty(nvl
))
5550 (void) printf(gettext("no datasets available\n"));
5554 return (0 != errors
);
5557 #define CHECK_SPINNER 30
5558 #define SPINNER_TIME 3 /* seconds */
5559 #define MOUNT_TIME 5 /* seconds */
5562 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
5564 static char *spin
[] = { "-", "\\", "|", "/" };
5565 static int spinval
= 0;
5566 static int spincheck
= 0;
5567 static time_t last_spin_time
= (time_t)0;
5568 get_all_cb_t
*cbp
= data
;
5569 zfs_type_t type
= zfs_get_type(zhp
);
5571 if (cbp
->cb_verbose
) {
5572 if (--spincheck
< 0) {
5573 time_t now
= time(NULL
);
5574 if (last_spin_time
+ SPINNER_TIME
< now
) {
5575 update_progress(spin
[spinval
++ % 4]);
5576 last_spin_time
= now
;
5578 spincheck
= CHECK_SPINNER
;
5583 * Interate over any nested datasets.
5585 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
5591 * Skip any datasets whose type does not match.
5593 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
5597 libzfs_add_handle(cbp
, zhp
);
5598 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
5604 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
5606 get_all_cb_t cb
= { 0 };
5607 cb
.cb_verbose
= verbose
;
5608 cb
.cb_getone
= get_one_dataset
;
5611 set_progress_header(gettext("Reading ZFS config"));
5612 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
5614 *dslist
= cb
.cb_handles
;
5615 *count
= cb
.cb_used
;
5618 finish_progress(gettext("done."));
5622 * Generic callback for sharing or mounting filesystems. Because the code is so
5623 * similar, we have a common function with an extra parameter to determine which
5624 * mode we are using.
5626 #define OP_SHARE 0x1
5627 #define OP_MOUNT 0x2
5630 * Share or mount a dataset.
5633 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
5634 boolean_t
explicit, const char *options
)
5636 char mountpoint
[ZFS_MAXPROPLEN
];
5637 char shareopts
[ZFS_MAXPROPLEN
];
5638 char smbshareopts
[ZFS_MAXPROPLEN
];
5639 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
5641 uint64_t zoned
, canmount
;
5642 boolean_t shared_nfs
, shared_smb
;
5644 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
5647 * Check to make sure we can mount/share this dataset. If we
5648 * are in the global zone and the filesystem is exported to a
5649 * local zone, or if we are in a local zone and the
5650 * filesystem is not exported, then it is an error.
5652 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
5654 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
5658 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5659 "dataset is exported to a local zone\n"), cmdname
,
5663 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
5667 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5668 "permission denied\n"), cmdname
,
5674 * Ignore any filesystems which don't apply to us. This
5675 * includes those with a legacy mountpoint, or those with
5676 * legacy share options.
5678 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
5679 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
5680 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
5681 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5682 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
5683 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5685 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
5686 strcmp(smbshareopts
, "off") == 0) {
5690 (void) fprintf(stderr
, gettext("cannot share '%s': "
5691 "legacy share\n"), zfs_get_name(zhp
));
5692 (void) fprintf(stderr
, gettext("use share(1M) to "
5693 "share this filesystem, or set "
5694 "sharenfs property on\n"));
5699 * We cannot share or mount legacy filesystems. If the
5700 * shareopts is non-legacy but the mountpoint is legacy, we
5701 * treat it as a legacy share.
5703 if (strcmp(mountpoint
, "legacy") == 0) {
5707 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5708 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
5709 (void) fprintf(stderr
, gettext("use %s(1M) to "
5710 "%s this filesystem\n"), cmdname
, cmdname
);
5714 if (strcmp(mountpoint
, "none") == 0) {
5718 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
5719 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
5724 * canmount explicit outcome
5725 * on no pass through
5726 * on yes pass through
5728 * off yes display error, return 1
5729 * noauto no return 0
5730 * noauto yes pass through
5732 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
5733 if (canmount
== ZFS_CANMOUNT_OFF
) {
5737 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5738 "'canmount' property is set to 'off'\n"), cmdname
,
5741 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
5746 * At this point, we have verified that the mountpoint and/or
5747 * shareopts are appropriate for auto management. If the
5748 * filesystem is already mounted or shared, return (failing
5749 * for explicit requests); otherwise mount or share the
5755 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
5756 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
5758 if (shared_nfs
&& shared_smb
||
5759 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
5760 strcmp(smbshareopts
, "off") == 0) ||
5761 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
5762 strcmp(shareopts
, "off") == 0)) {
5766 (void) fprintf(stderr
, gettext("cannot share "
5767 "'%s': filesystem already shared\n"),
5772 if (!zfs_is_mounted(zhp
, NULL
) &&
5773 zfs_mount(zhp
, NULL
, 0) != 0)
5776 if (protocol
== NULL
) {
5777 if (zfs_shareall(zhp
) != 0)
5779 } else if (strcmp(protocol
, "nfs") == 0) {
5780 if (zfs_share_nfs(zhp
))
5782 } else if (strcmp(protocol
, "smb") == 0) {
5783 if (zfs_share_smb(zhp
))
5786 (void) fprintf(stderr
, gettext("cannot share "
5787 "'%s': invalid share type '%s' "
5789 zfs_get_name(zhp
), protocol
);
5796 if (options
== NULL
)
5797 mnt
.mnt_mntopts
= "";
5799 mnt
.mnt_mntopts
= (char *)options
;
5801 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
5802 zfs_is_mounted(zhp
, NULL
)) {
5806 (void) fprintf(stderr
, gettext("cannot mount "
5807 "'%s': filesystem already mounted\n"),
5812 if (zfs_mount(zhp
, options
, flags
) != 0)
5821 * Reports progress in the form "(current/total)". Not thread-safe.
5824 report_mount_progress(int current
, int total
)
5826 static time_t last_progress_time
= 0;
5827 time_t now
= time(NULL
);
5830 /* report 1..n instead of 0..n-1 */
5833 /* display header if we're here for the first time */
5835 set_progress_header(gettext("Mounting ZFS filesystems"));
5836 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
5837 /* too soon to report again */
5841 last_progress_time
= now
;
5843 (void) sprintf(info
, "(%d/%d)", current
, total
);
5845 if (current
== total
)
5846 finish_progress(info
);
5848 update_progress(info
);
5852 append_options(char *mntopts
, char *newopts
)
5854 int len
= strlen(mntopts
);
5856 /* original length plus new string to append plus 1 for the comma */
5857 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
5858 (void) fprintf(stderr
, gettext("the opts argument for "
5859 "'%c' option is too long (more than %d chars)\n"),
5860 "-o", MNT_LINE_MAX
);
5865 mntopts
[len
++] = ',';
5867 (void) strcpy(&mntopts
[len
], newopts
);
5871 share_mount(int op
, int argc
, char **argv
)
5874 boolean_t verbose
= B_FALSE
;
5876 char *options
= NULL
;
5880 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
5890 if (*optarg
== '\0') {
5891 (void) fprintf(stderr
, gettext("empty mount "
5892 "options (-o) specified\n"));
5896 if (options
== NULL
)
5897 options
= safe_malloc(MNT_LINE_MAX
+ 1);
5899 /* option validation is done later */
5900 append_options(options
, optarg
);
5904 flags
|= MS_OVERLAY
;
5907 (void) fprintf(stderr
, gettext("missing argument for "
5908 "'%c' option\n"), optopt
);
5912 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5921 /* check number of arguments */
5923 zfs_handle_t
**dslist
= NULL
;
5924 size_t i
, count
= 0;
5925 char *protocol
= NULL
;
5927 if (op
== OP_SHARE
&& argc
> 0) {
5928 if (strcmp(argv
[0], "nfs") != 0 &&
5929 strcmp(argv
[0], "smb") != 0) {
5930 (void) fprintf(stderr
, gettext("share type "
5931 "must be 'nfs' or 'smb'\n"));
5940 (void) fprintf(stderr
, gettext("too many arguments\n"));
5944 start_progress_timer();
5945 get_all_datasets(&dslist
, &count
, verbose
);
5950 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
5952 for (i
= 0; i
< count
; i
++) {
5954 report_mount_progress(i
, count
);
5956 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
5957 B_FALSE
, options
) != 0)
5959 zfs_close(dslist
[i
]);
5963 } else if (argc
== 0) {
5964 struct mnttab entry
;
5966 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
5967 (void) fprintf(stderr
, gettext("missing filesystem "
5968 "argument (specify -a for all)\n"));
5973 * When mount is given no arguments, go through /etc/mnttab and
5974 * display any active ZFS mounts. We hide any snapshots, since
5975 * they are controlled automatically.
5977 rewind(mnttab_file
);
5978 while (getmntent(mnttab_file
, &entry
) == 0) {
5979 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
5980 strchr(entry
.mnt_special
, '@') != NULL
)
5983 (void) printf("%-30s %s\n", entry
.mnt_special
,
5991 (void) fprintf(stderr
,
5992 gettext("too many arguments\n"));
5996 if ((zhp
= zfs_open(g_zfs
, argv
[0],
5997 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6000 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
6010 * zfs mount -a [nfs]
6011 * zfs mount filesystem
6013 * Mount all filesystems, or mount the given filesystem.
6016 zfs_do_mount(int argc
, char **argv
)
6018 return (share_mount(OP_MOUNT
, argc
, argv
));
6022 * zfs share -a [nfs | smb]
6023 * zfs share filesystem
6025 * Share all filesystems, or share the given filesystem.
6028 zfs_do_share(int argc
, char **argv
)
6030 return (share_mount(OP_SHARE
, argc
, argv
));
6033 typedef struct unshare_unmount_node
{
6034 zfs_handle_t
*un_zhp
;
6036 uu_avl_node_t un_avlnode
;
6037 } unshare_unmount_node_t
;
6041 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
6043 const unshare_unmount_node_t
*l
= larg
;
6044 const unshare_unmount_node_t
*r
= rarg
;
6046 return (strcmp(l
->un_mountp
, r
->un_mountp
));
6050 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6051 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6052 * and unmount it appropriately.
6055 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
6059 struct stat64 statbuf
;
6060 struct extmnttab entry
;
6061 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
6065 * Search for the path in /etc/mnttab. Rather than looking for the
6066 * specific path, which can be fooled by non-standard paths (i.e. ".."
6067 * or "//"), we stat() the path and search for the corresponding
6068 * (major,minor) device pair.
6070 if (stat64(path
, &statbuf
) != 0) {
6071 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6072 cmdname
, path
, strerror(errno
));
6075 path_inode
= statbuf
.st_ino
;
6078 * Search for the given (major,minor) pair in the mount table.
6080 rewind(mnttab_file
);
6081 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
6082 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
6083 entry
.mnt_minor
== minor(statbuf
.st_dev
))
6087 if (op
== OP_SHARE
) {
6088 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
6089 "currently mounted\n"), cmdname
, path
);
6092 (void) fprintf(stderr
, gettext("warning: %s not in mnttab\n"),
6094 if ((ret
= umount2(path
, flags
)) != 0)
6095 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
6100 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
6101 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
6102 "filesystem\n"), cmdname
, path
);
6106 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6107 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6111 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
6112 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6113 cmdname
, path
, strerror(errno
));
6115 } else if (statbuf
.st_ino
!= path_inode
) {
6116 (void) fprintf(stderr
, gettext("cannot "
6117 "%s '%s': not a mountpoint\n"), cmdname
, path
);
6121 if (op
== OP_SHARE
) {
6122 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6123 char smbshare_prop
[ZFS_MAXPROPLEN
];
6125 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
6126 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6127 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
6128 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6130 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6131 strcmp(smbshare_prop
, "off") == 0) {
6132 (void) fprintf(stderr
, gettext("cannot unshare "
6133 "'%s': legacy share\n"), path
);
6134 (void) fprintf(stderr
, gettext("use "
6135 "unshare(1M) to unshare this filesystem\n"));
6136 } else if (!zfs_is_shared(zhp
)) {
6137 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6138 "not currently shared\n"), path
);
6140 ret
= zfs_unshareall_bypath(zhp
, path
);
6143 char mtpt_prop
[ZFS_MAXPROPLEN
];
6145 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6146 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6149 ret
= zfs_unmount(zhp
, NULL
, flags
);
6150 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6151 (void) fprintf(stderr
, gettext("cannot unmount "
6152 "'%s': legacy mountpoint\n"),
6154 (void) fprintf(stderr
, gettext("use umount(1M) "
6155 "to unmount this filesystem\n"));
6157 ret
= zfs_unmountall(zhp
, flags
);
6168 * Generic callback for unsharing or unmounting a filesystem.
6171 unshare_unmount(int op
, int argc
, char **argv
)
6178 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6179 char sharesmb
[ZFS_MAXPROPLEN
];
6182 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
6191 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6202 * We could make use of zfs_for_each() to walk all datasets in
6203 * the system, but this would be very inefficient, especially
6204 * since we would have to linearly search /etc/mnttab for each
6205 * one. Instead, do one pass through /etc/mnttab looking for
6206 * zfs entries and call zfs_unmount() for each one.
6208 * Things get a little tricky if the administrator has created
6209 * mountpoints beneath other ZFS filesystems. In this case, we
6210 * have to unmount the deepest filesystems first. To accomplish
6211 * this, we place all the mountpoints in an AVL tree sorted by
6212 * the special type (dataset name), and walk the result in
6213 * reverse to make sure to get any snapshots first.
6215 struct mnttab entry
;
6216 uu_avl_pool_t
*pool
;
6218 unshare_unmount_node_t
*node
;
6220 uu_avl_walk_t
*walk
;
6223 (void) fprintf(stderr
, gettext("too many arguments\n"));
6227 if (((pool
= uu_avl_pool_create("unmount_pool",
6228 sizeof (unshare_unmount_node_t
),
6229 offsetof(unshare_unmount_node_t
, un_avlnode
),
6230 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6231 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6234 rewind(mnttab_file
);
6235 while (getmntent(mnttab_file
, &entry
) == 0) {
6237 /* ignore non-ZFS entries */
6238 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6241 /* ignore snapshots */
6242 if (strchr(entry
.mnt_special
, '@') != NULL
)
6245 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6246 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6253 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6255 sizeof (nfs_mnt_prop
),
6256 NULL
, NULL
, 0, B_FALSE
) == 0);
6257 if (strcmp(nfs_mnt_prop
, "off") != 0)
6259 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6261 sizeof (nfs_mnt_prop
),
6262 NULL
, NULL
, 0, B_FALSE
) == 0);
6263 if (strcmp(nfs_mnt_prop
, "off") == 0)
6267 /* Ignore legacy mounts */
6268 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6270 sizeof (nfs_mnt_prop
),
6271 NULL
, NULL
, 0, B_FALSE
) == 0);
6272 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6274 /* Ignore canmount=noauto mounts */
6275 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6276 ZFS_CANMOUNT_NOAUTO
)
6282 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6284 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6286 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6288 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6289 uu_avl_insert(tree
, node
, idx
);
6291 zfs_close(node
->un_zhp
);
6292 free(node
->un_mountp
);
6298 * Walk the AVL tree in reverse, unmounting each filesystem and
6299 * removing it from the AVL tree in the process.
6301 if ((walk
= uu_avl_walk_start(tree
,
6302 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6305 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6306 uu_avl_remove(tree
, node
);
6310 if (zfs_unshareall_bypath(node
->un_zhp
,
6311 node
->un_mountp
) != 0)
6316 if (zfs_unmount(node
->un_zhp
,
6317 node
->un_mountp
, flags
) != 0)
6322 zfs_close(node
->un_zhp
);
6323 free(node
->un_mountp
);
6327 uu_avl_walk_end(walk
);
6328 uu_avl_destroy(tree
);
6329 uu_avl_pool_destroy(pool
);
6334 (void) fprintf(stderr
,
6335 gettext("missing filesystem argument\n"));
6337 (void) fprintf(stderr
,
6338 gettext("too many arguments\n"));
6343 * We have an argument, but it may be a full path or a ZFS
6344 * filesystem. Pass full paths off to unmount_path() (shared by
6345 * manual_unmount), otherwise open the filesystem and pass to
6348 if (argv
[0][0] == '/')
6349 return (unshare_unmount_path(op
, argv
[0],
6352 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6353 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6356 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
6357 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
6358 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
6359 NULL
, 0, B_FALSE
) == 0);
6363 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6365 sizeof (nfs_mnt_prop
),
6366 NULL
, NULL
, 0, B_FALSE
) == 0);
6367 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6368 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
6371 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6372 strcmp(sharesmb
, "off") == 0) {
6373 (void) fprintf(stderr
, gettext("cannot "
6374 "unshare '%s': legacy share\n"),
6376 (void) fprintf(stderr
, gettext("use "
6377 "unshare(1M) to unshare this "
6380 } else if (!zfs_is_shared(zhp
)) {
6381 (void) fprintf(stderr
, gettext("cannot "
6382 "unshare '%s': not currently "
6383 "shared\n"), zfs_get_name(zhp
));
6385 } else if (zfs_unshareall(zhp
) != 0) {
6391 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
6392 (void) fprintf(stderr
, gettext("cannot "
6393 "unmount '%s': legacy "
6394 "mountpoint\n"), zfs_get_name(zhp
));
6395 (void) fprintf(stderr
, gettext("use "
6396 "umount(1M) to unmount this "
6399 } else if (!zfs_is_mounted(zhp
, NULL
)) {
6400 (void) fprintf(stderr
, gettext("cannot "
6401 "unmount '%s': not currently "
6405 } else if (zfs_unmountall(zhp
, flags
) != 0) {
6419 * zfs unmount filesystem
6421 * Unmount all filesystems, or a specific ZFS filesystem.
6424 zfs_do_unmount(int argc
, char **argv
)
6426 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
6431 * zfs unshare filesystem
6433 * Unshare all filesystems, or a specific ZFS filesystem.
6436 zfs_do_unshare(int argc
, char **argv
)
6438 return (unshare_unmount(OP_SHARE
, argc
, argv
));
6442 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6443 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6446 manual_mount(int argc
, char **argv
)
6449 char mountpoint
[ZFS_MAXPROPLEN
];
6450 char mntopts
[MNT_LINE_MAX
] = { '\0' };
6454 char *dataset
, *path
;
6457 while ((c
= getopt(argc
, argv
, ":mo:O")) != -1) {
6460 (void) strlcpy(mntopts
, optarg
, sizeof (mntopts
));
6463 flags
|= MS_OVERLAY
;
6466 flags
|= MS_NOMNTTAB
;
6469 (void) fprintf(stderr
, gettext("missing argument for "
6470 "'%c' option\n"), optopt
);
6474 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6476 (void) fprintf(stderr
, gettext("usage: mount [-o opts] "
6485 /* check that we only have two arguments */
6488 (void) fprintf(stderr
, gettext("missing dataset "
6491 (void) fprintf(stderr
,
6492 gettext("missing mountpoint argument\n"));
6494 (void) fprintf(stderr
, gettext("too many arguments\n"));
6495 (void) fprintf(stderr
, "usage: mount <dataset> <mountpoint>\n");
6502 /* try to open the dataset */
6503 if ((zhp
= zfs_open(g_zfs
, dataset
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6506 (void) zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
6507 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
);
6509 /* check for legacy mountpoint and complain appropriately */
6511 if (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) == 0) {
6512 if (mount(dataset
, path
, MS_OPTIONSTR
| flags
, MNTTYPE_ZFS
,
6513 NULL
, 0, mntopts
, sizeof (mntopts
)) != 0) {
6514 (void) fprintf(stderr
, gettext("mount failed: %s\n"),
6519 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
6520 "mounted using 'mount -F zfs'\n"), dataset
);
6521 (void) fprintf(stderr
, gettext("Use 'zfs set mountpoint=%s' "
6522 "instead.\n"), path
);
6523 (void) fprintf(stderr
, gettext("If you must use 'mount -F zfs' "
6524 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
6525 (void) fprintf(stderr
, gettext("See zfs(1M) for more "
6534 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6535 * unmounts of non-legacy filesystems, as this is the dominant administrative
6539 manual_unmount(int argc
, char **argv
)
6545 while ((c
= getopt(argc
, argv
, "f")) != -1) {
6551 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6553 (void) fprintf(stderr
, gettext("usage: unmount [-f] "
6562 /* check arguments */
6565 (void) fprintf(stderr
, gettext("missing path "
6568 (void) fprintf(stderr
, gettext("too many arguments\n"));
6569 (void) fprintf(stderr
, gettext("usage: unmount [-f] <path>\n"));
6573 return (unshare_unmount_path(OP_MOUNT
, argv
[0], flags
, B_TRUE
));
6577 find_command_idx(char *command
, int *idx
)
6581 for (i
= 0; i
< NCOMMAND
; i
++) {
6582 if (command_table
[i
].name
== NULL
)
6585 if (strcmp(command
, command_table
[i
].name
) == 0) {
6594 zfs_do_diff(int argc
, char **argv
)
6598 char *tosnap
= NULL
;
6599 char *fromsnap
= NULL
;
6604 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
6607 flags
|= ZFS_DIFF_CLASSIFY
;
6610 flags
|= ZFS_DIFF_PARSEABLE
;
6613 flags
|= ZFS_DIFF_TIMESTAMP
;
6616 (void) fprintf(stderr
,
6617 gettext("invalid option '%c'\n"), optopt
);
6626 (void) fprintf(stderr
,
6627 gettext("must provide at least one snapshot name\n"));
6632 (void) fprintf(stderr
, gettext("too many arguments\n"));
6637 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
6640 if (*fromsnap
!= '@')
6641 copy
= strdup(fromsnap
);
6643 copy
= strdup(tosnap
);
6647 if (atp
= strchr(copy
, '@'))
6650 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6656 * Ignore SIGPIPE so that the library can give us
6657 * information on any failure
6659 (void) sigignore(SIGPIPE
);
6661 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
6669 * zfs bookmark <fs@snap> <fs#bmark>
6671 * Creates a bookmark with the given name from the given snapshot.
6674 zfs_do_bookmark(int argc
, char **argv
)
6676 char snapname
[ZFS_MAXNAMELEN
];
6683 while ((c
= getopt(argc
, argv
, "")) != -1) {
6686 (void) fprintf(stderr
,
6687 gettext("invalid option '%c'\n"), optopt
);
6695 /* check number of arguments */
6697 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
6701 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
6705 if (strchr(argv
[1], '#') == NULL
) {
6706 (void) fprintf(stderr
,
6707 gettext("invalid bookmark name '%s' -- "
6708 "must contain a '#'\n"), argv
[1]);
6712 if (argv
[0][0] == '@') {
6714 * Snapshot name begins with @.
6715 * Default to same fs as bookmark.
6717 (void) strncpy(snapname
, argv
[1], sizeof (snapname
));
6718 *strchr(snapname
, '#') = '\0';
6719 (void) strlcat(snapname
, argv
[0], sizeof (snapname
));
6721 (void) strncpy(snapname
, argv
[0], sizeof (snapname
));
6723 zhp
= zfs_open(g_zfs
, snapname
, ZFS_TYPE_SNAPSHOT
);
6729 nvl
= fnvlist_alloc();
6730 fnvlist_add_string(nvl
, argv
[1], snapname
);
6731 ret
= lzc_bookmark(nvl
, NULL
);
6735 const char *err_msg
;
6738 (void) snprintf(errbuf
, sizeof (errbuf
),
6739 dgettext(TEXT_DOMAIN
,
6740 "cannot create bookmark '%s'"), argv
[1]);
6744 err_msg
= "bookmark is in a different pool";
6747 err_msg
= "bookmark exists";
6750 err_msg
= "invalid argument";
6753 err_msg
= "bookmark feature not enabled";
6756 err_msg
= "out of space";
6759 err_msg
= "unknown error";
6762 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
6763 dgettext(TEXT_DOMAIN
, err_msg
));
6774 main(int argc
, char **argv
)
6781 (void) setlocale(LC_ALL
, "");
6782 (void) textdomain(TEXT_DOMAIN
);
6786 if ((g_zfs
= libzfs_init()) == NULL
) {
6787 (void) fprintf(stderr
, gettext("internal error: failed to "
6788 "initialize ZFS library\n"));
6792 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
6794 libzfs_print_on_error(g_zfs
, B_TRUE
);
6796 if ((mnttab_file
= fopen(MNTTAB
, "r")) == NULL
) {
6797 (void) fprintf(stderr
, gettext("internal error: unable to "
6798 "open %s\n"), MNTTAB
);
6803 * This command also doubles as the /etc/fs mount and unmount program.
6804 * Determine if we should take this behavior based on argv[0].
6806 progname
= basename(argv
[0]);
6807 if (strcmp(progname
, "mount") == 0) {
6808 ret
= manual_mount(argc
, argv
);
6809 } else if (strcmp(progname
, "umount") == 0) {
6810 ret
= manual_unmount(argc
, argv
);
6813 * Make sure the user has specified some command.
6816 (void) fprintf(stderr
, gettext("missing command\n"));
6823 * The 'umount' command is an alias for 'unmount'
6825 if (strcmp(cmdname
, "umount") == 0)
6826 cmdname
= "unmount";
6829 * The 'recv' command is an alias for 'receive'
6831 if (strcmp(cmdname
, "recv") == 0)
6832 cmdname
= "receive";
6835 * The 'snap' command is an alias for 'snapshot'
6837 if (strcmp(cmdname
, "snap") == 0)
6838 cmdname
= "snapshot";
6843 if (strcmp(cmdname
, "-?") == 0)
6847 * Run the appropriate command.
6849 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
6850 if (find_command_idx(cmdname
, &i
) == 0) {
6851 current_command
= &command_table
[i
];
6852 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
6853 } else if (strchr(cmdname
, '=') != NULL
) {
6854 verify(find_command_idx("set", &i
) == 0);
6855 current_command
= &command_table
[i
];
6856 ret
= command_table
[i
].func(argc
, argv
);
6858 (void) fprintf(stderr
, gettext("unrecognized "
6859 "command '%s'\n"), cmdname
);
6862 libzfs_mnttab_cache(g_zfs
, B_FALSE
);
6865 (void) fclose(mnttab_file
);
6867 if (ret
== 0 && log_history
)
6868 (void) zpool_log_history(g_zfs
, history_str
);
6873 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6874 * for the purposes of running ::findleaks.
6876 if (getenv("ZFS_ABORT") != NULL
) {
6877 (void) printf("dumping core by request\n");