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 2012 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
35 #include <libnvpair.h>
48 #include <sys/mkdev.h>
49 #include <sys/mntent.h>
50 #include <sys/mnttab.h>
51 #include <sys/mount.h>
53 #include <sys/fs/zfs.h>
54 #include <sys/types.h>
58 #include <libzfs_core.h>
60 #include <zfs_deleg.h>
64 #include <directory.h>
65 #endif /* HAVE_IDMAP */
69 #include "zfs_comutil.h"
70 #include "libzfs_impl.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
);
105 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
110 _umem_debug_init(void)
112 return ("default,verbose"); /* $UMEM_DEBUG setting */
116 _umem_logging_init(void)
118 return ("fail,contents"); /* $UMEM_LOGGING setting */
151 typedef struct zfs_command
{
153 int (*func
)(int argc
, char **argv
);
158 * Master command table. Each ZFS command has a name, associated function, and
159 * usage message. The usage messages need to be internationalized, so we have
160 * to have a function to return the usage message based on a command index.
162 * These commands are organized according to how they are displayed in the usage
163 * message. An empty command (one with a NULL name) indicates an empty line in
164 * the generic usage message.
166 static zfs_command_t command_table
[] = {
167 { "create", zfs_do_create
, HELP_CREATE
},
168 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
170 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
171 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
172 { "clone", zfs_do_clone
, HELP_CLONE
},
173 { "promote", zfs_do_promote
, HELP_PROMOTE
},
174 { "rename", zfs_do_rename
, HELP_RENAME
},
176 { "list", zfs_do_list
, HELP_LIST
},
178 { "set", zfs_do_set
, HELP_SET
},
179 { "get", zfs_do_get
, HELP_GET
},
180 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
181 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
182 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
183 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
185 { "mount", zfs_do_mount
, HELP_MOUNT
},
186 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
187 { "share", zfs_do_share
, HELP_SHARE
},
188 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
190 { "send", zfs_do_send
, HELP_SEND
},
191 { "receive", zfs_do_receive
, HELP_RECEIVE
},
193 { "allow", zfs_do_allow
, HELP_ALLOW
},
195 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
197 { "hold", zfs_do_hold
, HELP_HOLD
},
198 { "holds", zfs_do_holds
, HELP_HOLDS
},
199 { "release", zfs_do_release
, HELP_RELEASE
},
200 { "diff", zfs_do_diff
, HELP_DIFF
},
203 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
205 zfs_command_t
*current_command
;
208 get_usage(zfs_help_t idx
)
212 return (gettext("\tclone [-p] [-o property=value] ... "
213 "<snapshot> <filesystem|volume>\n"));
215 return (gettext("\tcreate [-p] [-o property=value] ... "
217 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
218 "-V <size> <volume>\n"));
220 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
221 "\tdestroy [-dnpRrv] "
222 "<filesystem|volume>@<snap>[%<snap>][,...]\n"));
224 return (gettext("\tget [-rHp] [-d max] "
225 "[-o \"all\" | field[,...]] [-t type[,...]] "
226 "[-s source[,...]]\n"
227 "\t <\"all\" | property[,...]> "
228 "[filesystem|volume|snapshot] ...\n"));
230 return (gettext("\tinherit [-rS] <property> "
231 "<filesystem|volume|snapshot> ...\n"));
233 return (gettext("\tupgrade [-v]\n"
234 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
236 return (gettext("\tlist [-rH][-d max] "
237 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
238 "\t [-S property] ... "
239 "[filesystem|volume|snapshot] ...\n"));
241 return (gettext("\tmount\n"
242 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
244 return (gettext("\tpromote <clone-filesystem>\n"));
246 return (gettext("\treceive [-vnFu] <filesystem|volume|"
248 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
250 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
251 "<filesystem|volume|snapshot>\n"
252 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
253 "\trename -r <snapshot> <snapshot>"));
255 return (gettext("\trollback [-rRf] <snapshot>\n"));
257 return (gettext("\tsend [-DnPpRrv] [-[iI] snapshot] "
260 return (gettext("\tset <property=value> "
261 "<filesystem|volume|snapshot> ...\n"));
263 return (gettext("\tshare <-a | filesystem>\n"));
265 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
266 "<filesystem@snapname|volume@snapname> ...\n"));
268 return (gettext("\tunmount [-f] "
269 "<-a | filesystem|mountpoint>\n"));
271 return (gettext("\tunshare "
272 "<-a | filesystem|mountpoint>\n"));
274 return (gettext("\tallow <filesystem|volume>\n"
276 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
277 "\t <filesystem|volume>\n"
278 "\tallow [-ld] -e <perm|@setname>[,...] "
279 "<filesystem|volume>\n"
280 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
281 "\tallow -s @setname <perm|@setname>[,...] "
282 "<filesystem|volume>\n"));
284 return (gettext("\tunallow [-rldug] "
285 "<\"everyone\"|user|group>[,...]\n"
286 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
287 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
288 "<filesystem|volume>\n"
289 "\tunallow [-r] -c [<perm|@setname>[,...]] "
290 "<filesystem|volume>\n"
291 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
292 "<filesystem|volume>\n"));
294 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
295 "[-s field] ...\n\t[-S field] ... "
296 "[-t type[,...]] <filesystem|snapshot>\n"));
297 case HELP_GROUPSPACE
:
298 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
299 "[-s field] ...\n\t[-S field] ... "
300 "[-t type[,...]] <filesystem|snapshot>\n"));
302 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
304 return (gettext("\tholds [-r] <snapshot> ...\n"));
306 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
308 return (gettext("\tdiff [-FHt] <snapshot> "
309 "[snapshot|filesystem]\n"));
319 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
324 * Utility function to guarantee malloc() success.
328 safe_malloc(size_t size
)
332 if ((data
= calloc(1, size
)) == NULL
)
339 safe_strdup(char *str
)
341 char *dupstr
= strdup(str
);
350 * Callback routine that will print out information for each of
354 usage_prop_cb(int prop
, void *cb
)
358 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
360 if (zfs_prop_readonly(prop
))
361 (void) fprintf(fp
, " NO ");
363 (void) fprintf(fp
, "YES ");
365 if (zfs_prop_inheritable(prop
))
366 (void) fprintf(fp
, " YES ");
368 (void) fprintf(fp
, " NO ");
370 if (zfs_prop_values(prop
) == NULL
)
371 (void) fprintf(fp
, "-\n");
373 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
379 * Display usage message. If we're inside a command, display only the usage for
380 * that command. Otherwise, iterate over the entire command table and display
381 * a complete usage message.
384 usage(boolean_t requested
)
387 boolean_t show_properties
= B_FALSE
;
388 FILE *fp
= requested
? stdout
: stderr
;
390 if (current_command
== NULL
) {
392 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
394 gettext("where 'command' is one of the following:\n\n"));
396 for (i
= 0; i
< NCOMMAND
; i
++) {
397 if (command_table
[i
].name
== NULL
)
398 (void) fprintf(fp
, "\n");
400 (void) fprintf(fp
, "%s",
401 get_usage(command_table
[i
].usage
));
404 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
405 "pool/[dataset/]*dataset[@name]\n"));
407 (void) fprintf(fp
, gettext("usage:\n"));
408 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
411 if (current_command
!= NULL
&&
412 (strcmp(current_command
->name
, "set") == 0 ||
413 strcmp(current_command
->name
, "get") == 0 ||
414 strcmp(current_command
->name
, "inherit") == 0 ||
415 strcmp(current_command
->name
, "list") == 0))
416 show_properties
= B_TRUE
;
418 if (show_properties
) {
420 gettext("\nThe following properties are supported:\n"));
422 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
423 "PROPERTY", "EDIT", "INHERIT", "VALUES");
425 /* Iterate over all properties */
426 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
429 (void) fprintf(fp
, "\t%-15s ", "userused@...");
430 (void) fprintf(fp
, " NO NO <size>\n");
431 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
432 (void) fprintf(fp
, " NO NO <size>\n");
433 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
434 (void) fprintf(fp
, "YES NO <size> | none\n");
435 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
436 (void) fprintf(fp
, "YES NO <size> | none\n");
437 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
438 (void) fprintf(fp
, " NO NO <size>\n");
440 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
441 "with standard units such as K, M, G, etc.\n"));
442 (void) fprintf(fp
, gettext("\nUser-defined properties can "
443 "be specified by using a name containing a colon (:).\n"));
444 (void) fprintf(fp
, gettext("\nThe {user|group}{used|quota}@ "
445 "properties must be appended with\n"
446 "a user or group specifier of one of these forms:\n"
447 " POSIX name (eg: \"matt\")\n"
448 " POSIX id (eg: \"126829\")\n"
449 " SMB name@domain (eg: \"matt@sun\")\n"
450 " SMB SID (eg: \"S-1-234-567-89\")\n"));
453 gettext("\nFor the property list, run: %s\n"),
456 gettext("\nFor the delegated permission list, run: %s\n"),
457 "zfs allow|unallow");
461 * See comments at end of main().
463 if (getenv("ZFS_ABORT") != NULL
) {
464 (void) printf("dumping core by request\n");
468 exit(requested
? 0 : 2);
472 parseprop(nvlist_t
*props
)
474 char *propname
= optarg
;
475 char *propval
, *strval
;
477 if ((propval
= strchr(propname
, '=')) == NULL
) {
478 (void) fprintf(stderr
, gettext("missing "
479 "'=' for -o option\n"));
484 if (nvlist_lookup_string(props
, propname
, &strval
) == 0) {
485 (void) fprintf(stderr
, gettext("property '%s' "
486 "specified multiple times\n"), propname
);
489 if (nvlist_add_string(props
, propname
, propval
) != 0)
495 parse_depth(char *opt
, int *flags
)
500 depth
= (int)strtol(opt
, &tmp
, 0);
502 (void) fprintf(stderr
,
503 gettext("%s is not an integer\n"), optarg
);
507 (void) fprintf(stderr
,
508 gettext("Depth can not be negative.\n"));
511 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
515 #define PROGRESS_DELAY 2 /* seconds */
517 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";
518 static time_t pt_begin
;
519 static char *pt_header
= NULL
;
520 static boolean_t pt_shown
;
523 start_progress_timer(void)
525 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
530 set_progress_header(char *header
)
532 assert(pt_header
== NULL
);
533 pt_header
= safe_strdup(header
);
535 (void) printf("%s: ", header
);
536 (void) fflush(stdout
);
541 update_progress(char *update
)
543 if (!pt_shown
&& time(NULL
) > pt_begin
) {
544 int len
= strlen(update
);
546 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
548 (void) fflush(stdout
);
550 } else if (pt_shown
) {
551 int len
= strlen(update
);
553 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
554 (void) fflush(stdout
);
559 finish_progress(char *done
)
562 (void) printf("%s\n", done
);
563 (void) fflush(stdout
);
570 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
572 * Given an existing dataset, create a writable copy whose initial contents
573 * are the same as the source. The newly created dataset maintains a
574 * dependency on the original; the original cannot be destroyed so long as
577 * The '-p' flag creates all the non-existing ancestors of the target first.
580 zfs_do_clone(int argc
, char **argv
)
582 zfs_handle_t
*zhp
= NULL
;
583 boolean_t parents
= B_FALSE
;
588 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
592 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
595 if (parseprop(props
))
602 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
611 /* check number of arguments */
613 (void) fprintf(stderr
, gettext("missing source dataset "
618 (void) fprintf(stderr
, gettext("missing target dataset "
623 (void) fprintf(stderr
, gettext("too many arguments\n"));
627 /* open the source dataset */
628 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
631 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
634 * Now create the ancestors of the target dataset. If the
635 * target already exists and '-p' option was used we should not
638 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
641 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
646 ret
= zfs_clone(zhp
, argv
[1], props
);
648 /* create the mountpoint if necessary */
652 clone
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
654 if (zfs_get_type(clone
) != ZFS_TYPE_VOLUME
)
655 if ((ret
= zfs_mount(clone
, NULL
, 0)) == 0)
656 ret
= zfs_share(clone
);
675 * zfs create [-p] [-o prop=value] ... fs
676 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
678 * Create a new dataset. This command can be used to create filesystems
679 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
680 * For volumes, the user must specify a size to be used.
682 * The '-s' flag applies only to volumes, and indicates that we should not try
683 * to set the reservation for this volume. By default we set a reservation
684 * equal to the size for any volume. For pools with SPA_VERSION >=
685 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
687 * The '-p' flag creates all the non-existing ancestors of the target first.
690 zfs_do_create(int argc
, char **argv
)
692 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
693 zfs_handle_t
*zhp
= NULL
;
694 uint64_t volsize
= 0;
696 boolean_t noreserve
= B_FALSE
;
697 boolean_t bflag
= B_FALSE
;
698 boolean_t parents
= B_FALSE
;
702 int canmount
= ZFS_CANMOUNT_OFF
;
704 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
708 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
711 type
= ZFS_TYPE_VOLUME
;
712 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
713 (void) fprintf(stderr
, gettext("bad volume "
714 "size '%s': %s\n"), optarg
,
715 libzfs_error_description(g_zfs
));
719 if (nvlist_add_uint64(props
,
720 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
729 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
730 (void) fprintf(stderr
, gettext("bad volume "
731 "block size '%s': %s\n"), optarg
,
732 libzfs_error_description(g_zfs
));
736 if (nvlist_add_uint64(props
,
737 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
742 if (parseprop(props
))
749 (void) fprintf(stderr
, gettext("missing size "
754 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
760 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
761 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
762 "used when creating a volume\n"));
769 /* check number of arguments */
771 (void) fprintf(stderr
, gettext("missing %s argument\n"),
772 zfs_type_to_name(type
));
776 (void) fprintf(stderr
, gettext("too many arguments\n"));
780 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
781 zpool_handle_t
*zpool_handle
;
782 uint64_t spa_version
;
784 zfs_prop_t resv_prop
;
787 if ((p
= strchr(argv
[0], '/')))
789 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
792 if (zpool_handle
== NULL
)
794 spa_version
= zpool_get_prop_int(zpool_handle
,
795 ZPOOL_PROP_VERSION
, NULL
);
796 zpool_close(zpool_handle
);
797 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
798 resv_prop
= ZFS_PROP_REFRESERVATION
;
800 resv_prop
= ZFS_PROP_RESERVATION
;
801 volsize
= zvol_volsize_to_reservation(volsize
, props
);
803 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
805 if (nvlist_add_uint64(props
,
806 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
813 if (parents
&& zfs_name_valid(argv
[0], type
)) {
815 * Now create the ancestors of target dataset. If the target
816 * already exists and '-p' option was used we should not
819 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
823 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
828 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
831 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
836 * if the user doesn't want the dataset automatically mounted,
837 * then skip the mount/share step
839 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
))
840 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
843 * Mount and/or share the new filesystem as appropriate. We provide a
844 * verbose error message to let the user know that their filesystem was
845 * in fact created, even if we failed to mount or share it.
847 if (canmount
== ZFS_CANMOUNT_ON
) {
848 if (zfs_mount(zhp
, NULL
, 0) != 0) {
849 (void) fprintf(stderr
, gettext("filesystem "
850 "successfully created, but not mounted\n"));
852 } else if (zfs_share(zhp
) != 0) {
853 (void) fprintf(stderr
, gettext("filesystem "
854 "successfully created, but not shared\n"));
871 * zfs destroy [-rRf] <fs, vol>
872 * zfs destroy [-rRd] <snap>
874 * -r Recursively destroy all children
875 * -R Recursively destroy all dependents, including clones
876 * -f Force unmounting of any dependents
877 * -d If we can't destroy now, mark for deferred destruction
879 * Destroys the given dataset. By default, it will unmount any filesystems,
880 * and refuse to destroy a dataset that has any dependents. A dependent can
881 * either be a child, or a clone of a child.
883 typedef struct destroy_cbdata
{
886 boolean_t cb_recurse
;
888 boolean_t cb_doclones
;
889 zfs_handle_t
*cb_target
;
890 boolean_t cb_defer_destroy
;
891 boolean_t cb_verbose
;
892 boolean_t cb_parsable
;
895 nvlist_t
*cb_batchedsnaps
;
897 /* first snap in contiguous run */
899 /* previous snap in contiguous run */
906 * Check for any dependents based on the '-r' or '-R' flags.
909 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
911 destroy_cbdata_t
*cbp
= data
;
912 const char *tname
= zfs_get_name(cbp
->cb_target
);
913 const char *name
= zfs_get_name(zhp
);
915 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
916 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
918 * This is a direct descendant, not a clone somewhere else in
925 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
926 "%s has children\n"),
927 zfs_get_name(cbp
->cb_target
),
928 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
929 (void) fprintf(stderr
, gettext("use '-r' to destroy "
930 "the following datasets:\n"));
931 cbp
->cb_first
= B_FALSE
;
932 cbp
->cb_error
= B_TRUE
;
935 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
938 * This is a clone. We only want to report this if the '-r'
939 * wasn't specified, or the target is a snapshot.
941 if (!cbp
->cb_recurse
&&
942 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
946 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
947 "%s has dependent clones\n"),
948 zfs_get_name(cbp
->cb_target
),
949 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
950 (void) fprintf(stderr
, gettext("use '-R' to destroy "
951 "the following datasets:\n"));
952 cbp
->cb_first
= B_FALSE
;
953 cbp
->cb_error
= B_TRUE
;
954 cbp
->cb_dryrun
= B_TRUE
;
957 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
966 destroy_callback(zfs_handle_t
*zhp
, void *data
)
968 destroy_cbdata_t
*cb
= data
;
969 const char *name
= zfs_get_name(zhp
);
971 if (cb
->cb_verbose
) {
972 if (cb
->cb_parsable
) {
973 (void) printf("destroy\t%s\n", name
);
974 } else if (cb
->cb_dryrun
) {
975 (void) printf(gettext("would destroy %s\n"),
978 (void) printf(gettext("will destroy %s\n"),
984 * Ignore pools (which we've already flagged as an error before getting
987 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
988 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
998 * We batch up all contiguous snapshots (even of different
999 * filesystems) and destroy them with one ioctl. We can't
1000 * simply do all snap deletions and then all fs deletions,
1001 * because we must delete a clone before its origin.
1003 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1004 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1006 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1007 cb
->cb_batchedsnaps
, B_FALSE
);
1008 fnvlist_free(cb
->cb_batchedsnaps
);
1009 cb
->cb_batchedsnaps
= fnvlist_alloc();
1012 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1013 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1024 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1026 destroy_cbdata_t
*cb
= arg
;
1027 const char *name
= zfs_get_name(zhp
);
1030 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1031 if (cb
->cb_firstsnap
== NULL
)
1032 cb
->cb_firstsnap
= strdup(name
);
1033 if (cb
->cb_prevsnap
!= NULL
)
1034 free(cb
->cb_prevsnap
);
1035 /* this snap continues the current range */
1036 cb
->cb_prevsnap
= strdup(name
);
1037 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1039 if (cb
->cb_verbose
) {
1040 if (cb
->cb_parsable
) {
1041 (void) printf("destroy\t%s\n", name
);
1042 } else if (cb
->cb_dryrun
) {
1043 (void) printf(gettext("would destroy %s\n"),
1046 (void) printf(gettext("will destroy %s\n"),
1050 } else if (cb
->cb_firstsnap
!= NULL
) {
1051 /* end of this range */
1053 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1054 cb
->cb_prevsnap
, &used
);
1055 cb
->cb_snapused
+= used
;
1056 free(cb
->cb_firstsnap
);
1057 cb
->cb_firstsnap
= NULL
;
1058 free(cb
->cb_prevsnap
);
1059 cb
->cb_prevsnap
= NULL
;
1066 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1069 assert(cb
->cb_firstsnap
== NULL
);
1070 assert(cb
->cb_prevsnap
== NULL
);
1071 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1072 if (cb
->cb_firstsnap
!= NULL
) {
1075 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1076 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
;
1088 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1090 destroy_cbdata_t
*cb
= arg
;
1093 /* Check for clones. */
1094 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1095 cb
->cb_target
= zhp
;
1096 cb
->cb_first
= B_TRUE
;
1097 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1098 destroy_check_dependent
, cb
);
1102 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1110 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1112 destroy_cbdata_t
*cb
= arg
;
1115 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1121 if (cb
->cb_verbose
) {
1122 err
= destroy_print_snapshots(zhp
, cb
);
1128 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1136 destroy_clones(destroy_cbdata_t
*cb
)
1139 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1141 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1142 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1145 boolean_t defer
= cb
->cb_defer_destroy
;
1149 * We can't defer destroy non-snapshots, so set it to
1150 * false while destroying the clones.
1152 cb
->cb_defer_destroy
= B_FALSE
;
1153 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1154 destroy_callback
, cb
);
1155 cb
->cb_defer_destroy
= defer
;
1165 zfs_do_destroy(int argc
, char **argv
)
1167 destroy_cbdata_t cb
= { 0 };
1171 zfs_handle_t
*zhp
= NULL
;
1173 zfs_type_t type
= ZFS_TYPE_DATASET
;
1176 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1179 cb
.cb_verbose
= B_TRUE
;
1182 cb
.cb_verbose
= B_TRUE
;
1183 cb
.cb_parsable
= B_TRUE
;
1186 cb
.cb_dryrun
= B_TRUE
;
1189 cb
.cb_defer_destroy
= B_TRUE
;
1190 type
= ZFS_TYPE_SNAPSHOT
;
1193 cb
.cb_force
= B_TRUE
;
1196 cb
.cb_recurse
= B_TRUE
;
1199 cb
.cb_recurse
= B_TRUE
;
1200 cb
.cb_doclones
= B_TRUE
;
1204 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1213 /* check number of arguments */
1215 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1219 (void) fprintf(stderr
, gettext("too many arguments\n"));
1223 at
= strchr(argv
[0], '@');
1226 /* Build the list of snaps to destroy in cb_nvl. */
1227 cb
.cb_nvl
= fnvlist_alloc();
1230 zhp
= zfs_open(g_zfs
, argv
[0],
1231 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1235 cb
.cb_snapspec
= at
+ 1;
1236 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1242 if (nvlist_empty(cb
.cb_nvl
)) {
1243 (void) fprintf(stderr
, gettext("could not find any "
1244 "snapshots to destroy; check snapshot names.\n"));
1249 if (cb
.cb_verbose
) {
1251 zfs_nicenum(cb
.cb_snapused
, buf
, sizeof (buf
));
1252 if (cb
.cb_parsable
) {
1253 (void) printf("reclaim\t%llu\n",
1254 (u_longlong_t
)cb
.cb_snapused
);
1255 } else if (cb
.cb_dryrun
) {
1256 (void) printf(gettext("would reclaim %s\n"),
1259 (void) printf(gettext("will reclaim %s\n"),
1264 if (!cb
.cb_dryrun
) {
1265 if (cb
.cb_doclones
) {
1266 cb
.cb_batchedsnaps
= fnvlist_alloc();
1267 err
= destroy_clones(&cb
);
1269 err
= zfs_destroy_snaps_nvl(g_zfs
,
1270 cb
.cb_batchedsnaps
, B_FALSE
);
1278 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1279 cb
.cb_defer_destroy
);
1286 /* Open the given dataset */
1287 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1293 * Perform an explicit check for pools before going any further.
1295 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1296 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1297 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1298 "operation does not apply to pools\n"),
1300 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1301 "%s' to destroy all datasets in the pool\n"),
1303 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1304 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1310 * Check for any dependents and/or clones.
1312 cb
.cb_first
= B_TRUE
;
1313 if (!cb
.cb_doclones
&&
1314 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1325 cb
.cb_batchedsnaps
= fnvlist_alloc();
1326 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1333 * Do the real thing. The callback will close the
1334 * handle regardless of whether it succeeds or not.
1336 err
= destroy_callback(zhp
, &cb
);
1339 err
= zfs_destroy_snaps_nvl(g_zfs
,
1340 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1347 fnvlist_free(cb
.cb_batchedsnaps
);
1348 fnvlist_free(cb
.cb_nvl
);
1355 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1358 zfs_get_column_t col
;
1360 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1361 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1362 if (col
== GET_COL_RECVD
)
1368 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1369 * < all | property[,property]... > < fs | snap | vol > ...
1371 * -r recurse over any child datasets
1372 * -H scripted mode. Headers are stripped, and fields are separated
1373 * by tabs instead of spaces.
1374 * -o Set of fields to display. One of "name,property,value,
1375 * received,source". Default is "name,property,value,source".
1376 * "all" is an alias for all five.
1377 * -s Set of sources to allow. One of
1378 * "local,default,inherited,received,temporary,none". Default is
1380 * -p Display values in parsable (literal) format.
1382 * Prints properties for the given datasets. The user can control which
1383 * columns to display as well as which property types to allow.
1387 * Invoked to display the properties for a single dataset.
1390 get_callback(zfs_handle_t
*zhp
, void *data
)
1392 char buf
[ZFS_MAXPROPLEN
];
1393 char rbuf
[ZFS_MAXPROPLEN
];
1394 zprop_source_t sourcetype
;
1395 char source
[ZFS_MAXNAMELEN
];
1396 zprop_get_cbdata_t
*cbp
= data
;
1397 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1398 zprop_list_t
*pl
= cbp
->cb_proplist
;
1402 boolean_t received
= is_recvd_column(cbp
);
1404 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1405 char *recvdval
= NULL
;
1407 * Skip the special fake placeholder. This will also skip over
1408 * the name property when 'all' is specified.
1410 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1411 pl
== cbp
->cb_proplist
)
1414 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1415 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1416 sizeof (buf
), &sourcetype
, source
,
1418 cbp
->cb_literal
) != 0) {
1421 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1422 ZFS_TYPE_DATASET
)) {
1423 (void) fprintf(stderr
,
1424 gettext("No such property '%s'\n"),
1425 zfs_prop_to_name(pl
->pl_prop
));
1428 sourcetype
= ZPROP_SRC_NONE
;
1429 (void) strlcpy(buf
, "-", sizeof (buf
));
1432 if (received
&& (zfs_prop_get_recvd(zhp
,
1433 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1434 cbp
->cb_literal
) == 0))
1437 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1438 zfs_prop_to_name(pl
->pl_prop
),
1439 buf
, sourcetype
, source
, recvdval
);
1440 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1441 sourcetype
= ZPROP_SRC_LOCAL
;
1443 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1444 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1445 sourcetype
= ZPROP_SRC_NONE
;
1446 (void) strlcpy(buf
, "-", sizeof (buf
));
1449 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1450 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1451 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1452 sourcetype
= ZPROP_SRC_LOCAL
;
1454 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1455 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1456 sourcetype
= ZPROP_SRC_NONE
;
1457 (void) strlcpy(buf
, "-", sizeof (buf
));
1460 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1461 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1463 if (nvlist_lookup_nvlist(user_props
,
1464 pl
->pl_user_prop
, &propval
) != 0) {
1467 sourcetype
= ZPROP_SRC_NONE
;
1470 verify(nvlist_lookup_string(propval
,
1471 ZPROP_VALUE
, &strval
) == 0);
1472 verify(nvlist_lookup_string(propval
,
1473 ZPROP_SOURCE
, &sourceval
) == 0);
1475 if (strcmp(sourceval
,
1476 zfs_get_name(zhp
)) == 0) {
1477 sourcetype
= ZPROP_SRC_LOCAL
;
1478 } else if (strcmp(sourceval
,
1479 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1480 sourcetype
= ZPROP_SRC_RECEIVED
;
1482 sourcetype
= ZPROP_SRC_INHERITED
;
1483 (void) strlcpy(source
,
1484 sourceval
, sizeof (source
));
1488 if (received
&& (zfs_prop_get_recvd(zhp
,
1489 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1490 cbp
->cb_literal
) == 0))
1493 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1494 pl
->pl_user_prop
, strval
, sourcetype
,
1503 zfs_do_get(int argc
, char **argv
)
1505 zprop_get_cbdata_t cb
= { 0 };
1506 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1507 int types
= ZFS_TYPE_DATASET
;
1508 char *value
, *fields
;
1511 zprop_list_t fake_name
= { 0 };
1514 * Set up default columns and sources.
1516 cb
.cb_sources
= ZPROP_SRC_ALL
;
1517 cb
.cb_columns
[0] = GET_COL_NAME
;
1518 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1519 cb
.cb_columns
[2] = GET_COL_VALUE
;
1520 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1521 cb
.cb_type
= ZFS_TYPE_DATASET
;
1524 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1527 cb
.cb_literal
= B_TRUE
;
1530 limit
= parse_depth(optarg
, &flags
);
1533 flags
|= ZFS_ITER_RECURSE
;
1536 cb
.cb_scripted
= B_TRUE
;
1539 (void) fprintf(stderr
, gettext("missing argument for "
1540 "'%c' option\n"), optopt
);
1545 * Process the set of columns to display. We zero out
1546 * the structure to give us a blank slate.
1548 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1550 while (*optarg
!= '\0') {
1551 static char *col_subopts
[] =
1552 { "name", "property", "value", "received",
1553 "source", "all", NULL
};
1555 if (i
== ZFS_GET_NCOLS
) {
1556 (void) fprintf(stderr
, gettext("too "
1557 "many fields given to -o "
1562 switch (getsubopt(&optarg
, col_subopts
,
1565 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1568 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1571 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1574 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1575 flags
|= ZFS_ITER_RECVD_PROPS
;
1578 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1582 (void) fprintf(stderr
,
1583 gettext("\"all\" conflicts "
1584 "with specific fields "
1585 "given to -o option\n"));
1588 cb
.cb_columns
[0] = GET_COL_NAME
;
1589 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1590 cb
.cb_columns
[2] = GET_COL_VALUE
;
1591 cb
.cb_columns
[3] = GET_COL_RECVD
;
1592 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1593 flags
|= ZFS_ITER_RECVD_PROPS
;
1597 (void) fprintf(stderr
,
1598 gettext("invalid column name "
1607 while (*optarg
!= '\0') {
1608 static char *source_subopts
[] = {
1609 "local", "default", "inherited",
1610 "received", "temporary", "none",
1613 switch (getsubopt(&optarg
, source_subopts
,
1616 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1619 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1622 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1625 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1628 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1631 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1634 (void) fprintf(stderr
,
1635 gettext("invalid source "
1644 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1645 while (*optarg
!= '\0') {
1646 static char *type_subopts
[] = { "filesystem",
1647 "volume", "snapshot", "all", NULL
};
1649 switch (getsubopt(&optarg
, type_subopts
,
1652 types
|= ZFS_TYPE_FILESYSTEM
;
1655 types
|= ZFS_TYPE_VOLUME
;
1658 types
|= ZFS_TYPE_SNAPSHOT
;
1661 types
= ZFS_TYPE_DATASET
;
1665 (void) fprintf(stderr
,
1666 gettext("invalid type '%s'\n"),
1674 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1684 (void) fprintf(stderr
, gettext("missing property "
1691 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1699 * As part of zfs_expand_proplist(), we keep track of the maximum column
1700 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1701 * need to know the maximum name length. However, the user likely did
1702 * not specify 'name' as one of the properties to fetch, so we need to
1703 * make sure we always include at least this property for
1704 * print_get_headers() to work properly.
1706 if (cb
.cb_proplist
!= NULL
) {
1707 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1708 fake_name
.pl_width
= strlen(gettext("NAME"));
1709 fake_name
.pl_next
= cb
.cb_proplist
;
1710 cb
.cb_proplist
= &fake_name
;
1713 cb
.cb_first
= B_TRUE
;
1715 /* run for each object */
1716 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1717 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1719 if (cb
.cb_proplist
== &fake_name
)
1720 zprop_free_list(fake_name
.pl_next
);
1722 zprop_free_list(cb
.cb_proplist
);
1728 * inherit [-rS] <property> <fs|vol> ...
1730 * -r Recurse over all children
1731 * -S Revert to received value, if any
1733 * For each dataset specified on the command line, inherit the given property
1734 * from its parent. Inheriting a property at the pool level will cause it to
1735 * use the default value. The '-r' flag will recurse over all children, and is
1736 * useful for setting a property on a hierarchy-wide basis, regardless of any
1737 * local modifications for each dataset.
1740 typedef struct inherit_cbdata
{
1741 const char *cb_propname
;
1742 boolean_t cb_received
;
1746 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1748 inherit_cbdata_t
*cb
= data
;
1749 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1752 * If we're doing it recursively, then ignore properties that
1753 * are not valid for this type of dataset.
1755 if (prop
!= ZPROP_INVAL
&&
1756 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
)))
1759 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1763 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1765 inherit_cbdata_t
*cb
= data
;
1767 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1771 zfs_do_inherit(int argc
, char **argv
)
1775 inherit_cbdata_t cb
= { 0 };
1779 boolean_t received
= B_FALSE
;
1782 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1785 flags
|= ZFS_ITER_RECURSE
;
1792 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1801 /* check number of arguments */
1803 (void) fprintf(stderr
, gettext("missing property argument\n"));
1807 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1815 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1816 if (zfs_prop_readonly(prop
)) {
1817 (void) fprintf(stderr
, gettext(
1818 "%s property is read-only\n"),
1822 if (!zfs_prop_inheritable(prop
) && !received
) {
1823 (void) fprintf(stderr
, gettext("'%s' property cannot "
1824 "be inherited\n"), propname
);
1825 if (prop
== ZFS_PROP_QUOTA
||
1826 prop
== ZFS_PROP_RESERVATION
||
1827 prop
== ZFS_PROP_REFQUOTA
||
1828 prop
== ZFS_PROP_REFRESERVATION
)
1829 (void) fprintf(stderr
, gettext("use 'zfs set "
1830 "%s=none' to clear\n"), propname
);
1833 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1834 prop
== ZFS_PROP_VERSION
)) {
1835 (void) fprintf(stderr
, gettext("'%s' property cannot "
1836 "be reverted to a received value\n"), propname
);
1839 } else if (!zfs_prop_user(propname
)) {
1840 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1845 cb
.cb_propname
= propname
;
1846 cb
.cb_received
= received
;
1848 if (flags
& ZFS_ITER_RECURSE
) {
1849 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1850 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1852 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1853 NULL
, NULL
, 0, inherit_cb
, &cb
);
1859 typedef struct upgrade_cbdata
{
1860 uint64_t cb_numupgraded
;
1861 uint64_t cb_numsamegraded
;
1862 uint64_t cb_numfailed
;
1863 uint64_t cb_version
;
1865 boolean_t cb_foundone
;
1866 char cb_lastfs
[ZFS_MAXNAMELEN
];
1870 same_pool(zfs_handle_t
*zhp
, const char *name
)
1872 int len1
= strcspn(name
, "/@");
1873 const char *zhname
= zfs_get_name(zhp
);
1874 int len2
= strcspn(zhname
, "/@");
1878 return (strncmp(name
, zhname
, len1
) == 0);
1882 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
1884 upgrade_cbdata_t
*cb
= data
;
1885 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1887 /* list if it's old/new */
1888 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
1889 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
1892 str
= gettext("The following filesystems are "
1893 "formatted using a newer software version and\n"
1894 "cannot be accessed on the current system.\n\n");
1896 str
= gettext("The following filesystems are "
1897 "out of date, and can be upgraded. After being\n"
1898 "upgraded, these filesystems (and any 'zfs send' "
1899 "streams generated from\n"
1900 "subsequent snapshots) will no longer be "
1901 "accessible by older software versions.\n\n");
1904 if (!cb
->cb_foundone
) {
1906 (void) printf(gettext("VER FILESYSTEM\n"));
1907 (void) printf(gettext("--- ------------\n"));
1908 cb
->cb_foundone
= B_TRUE
;
1911 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
1918 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
1920 upgrade_cbdata_t
*cb
= data
;
1921 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1922 int needed_spa_version
;
1925 if (zfs_spa_version(zhp
, &spa_version
) < 0)
1928 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
1930 if (needed_spa_version
< 0)
1933 if (spa_version
< needed_spa_version
) {
1935 (void) printf(gettext("%s: can not be "
1936 "upgraded; the pool version needs to first "
1937 "be upgraded\nto version %d\n\n"),
1938 zfs_get_name(zhp
), needed_spa_version
);
1944 if (version
< cb
->cb_version
) {
1946 (void) snprintf(verstr
, sizeof (verstr
),
1947 "%llu", (u_longlong_t
)cb
->cb_version
);
1948 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
1950 * If they did "zfs upgrade -a", then we could
1951 * be doing ioctls to different pools. We need
1952 * to log this history once to each pool, and bypass
1953 * the normal history logging that happens in main().
1955 (void) zpool_log_history(g_zfs
, history_str
);
1956 log_history
= B_FALSE
;
1958 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
1959 cb
->cb_numupgraded
++;
1962 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
1963 } else if (version
> cb
->cb_version
) {
1964 /* can't downgrade */
1965 (void) printf(gettext("%s: can not be downgraded; "
1966 "it is already at version %u\n"),
1967 zfs_get_name(zhp
), version
);
1970 cb
->cb_numsamegraded
++;
1978 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1981 zfs_do_upgrade(int argc
, char **argv
)
1983 boolean_t all
= B_FALSE
;
1984 boolean_t showversions
= B_FALSE
;
1986 upgrade_cbdata_t cb
= { 0 };
1988 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1991 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
1994 flags
|= ZFS_ITER_RECURSE
;
1997 showversions
= B_TRUE
;
2000 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2001 optarg
, &cb
.cb_version
) != 0) {
2002 (void) fprintf(stderr
,
2003 gettext("invalid version %s\n"), optarg
);
2012 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2021 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2023 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2024 cb
.cb_version
|| argc
))
2026 if ((all
|| argc
) && (showversions
))
2032 /* Show info on available versions. */
2033 (void) printf(gettext("The following filesystem versions are "
2035 (void) printf(gettext("VER DESCRIPTION\n"));
2036 (void) printf("--- -----------------------------------------"
2037 "---------------\n");
2038 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2039 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2040 (void) printf(gettext(" 3 Case insensitive and filesystem "
2041 "user identifier (FUID)\n"));
2042 (void) printf(gettext(" 4 userquota, groupquota "
2044 (void) printf(gettext(" 5 System attributes\n"));
2045 (void) printf(gettext("\nFor more information on a particular "
2046 "version, including supported releases,\n"));
2047 (void) printf("see the ZFS Administration Guide.\n\n");
2049 } else if (argc
|| all
) {
2050 /* Upgrade filesystems */
2051 if (cb
.cb_version
== 0)
2052 cb
.cb_version
= ZPL_VERSION
;
2053 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2054 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2055 (void) printf(gettext("%llu filesystems upgraded\n"),
2056 (u_longlong_t
)cb
.cb_numupgraded
);
2057 if (cb
.cb_numsamegraded
) {
2058 (void) printf(gettext("%llu filesystems already at "
2060 (u_longlong_t
)cb
.cb_numsamegraded
);
2062 if (cb
.cb_numfailed
!= 0)
2065 /* List old-version filesytems */
2067 (void) printf(gettext("This system is currently running "
2068 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2070 flags
|= ZFS_ITER_RECURSE
;
2071 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2072 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2074 found
= cb
.cb_foundone
;
2075 cb
.cb_foundone
= B_FALSE
;
2076 cb
.cb_newer
= B_TRUE
;
2078 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2079 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2081 if (!cb
.cb_foundone
&& !found
) {
2082 (void) printf(gettext("All filesystems are "
2083 "formatted with the current version.\n"));
2091 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2092 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2093 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2094 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2096 * -H Scripted mode; elide headers and separate columns by tabs.
2097 * -i Translate SID to POSIX ID.
2098 * -n Print numeric ID instead of user/group name.
2099 * -o Control which fields to display.
2100 * -p Use exact (parseable) numeric output.
2101 * -s Specify sort columns, descending order.
2102 * -S Specify sort columns, ascending order.
2103 * -t Control which object types to display.
2105 * Displays space consumed by, and quotas on, each user in the specified
2106 * filesystem or snapshot.
2109 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2110 enum us_field_types
{
2116 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA" };
2117 static char *us_field_names
[] = { "type", "name", "used", "quota" };
2118 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2120 #define USTYPE_PSX_GRP (1 << 0)
2121 #define USTYPE_PSX_USR (1 << 1)
2122 #define USTYPE_SMB_GRP (1 << 2)
2123 #define USTYPE_SMB_USR (1 << 3)
2124 #define USTYPE_ALL \
2125 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2127 static int us_type_bits
[] = {
2134 static char *us_type_names
[] = { "posixgroup", "posxiuser", "smbgroup",
2137 typedef struct us_node
{
2139 uu_avl_node_t usn_avlnode
;
2140 uu_list_node_t usn_listnode
;
2143 typedef struct us_cbdata
{
2145 uu_avl_pool_t
*cb_avl_pool
;
2147 boolean_t cb_numname
;
2148 boolean_t cb_nicenum
;
2149 boolean_t cb_sid2posix
;
2150 zfs_userquota_prop_t cb_prop
;
2151 zfs_sort_column_t
*cb_sortcol
;
2152 size_t cb_width
[USFIELD_LAST
];
2155 static boolean_t us_populated
= B_FALSE
;
2158 zfs_sort_column_t
*si_sortcol
;
2159 boolean_t si_numname
;
2163 us_field_index(char *field
)
2167 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2168 if (strcmp(field
, us_field_names
[i
]) == 0)
2176 us_compare(const void *larg
, const void *rarg
, void *unused
)
2178 const us_node_t
*l
= larg
;
2179 const us_node_t
*r
= rarg
;
2180 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2181 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2182 boolean_t numname
= si
->si_numname
;
2183 nvlist_t
*lnvl
= l
->usn_nvl
;
2184 nvlist_t
*rnvl
= r
->usn_nvl
;
2188 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2195 zfs_prop_t prop
= sortcol
->sc_prop
;
2196 const char *propname
= NULL
;
2197 boolean_t reverse
= sortcol
->sc_reverse
;
2202 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2203 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2205 rc
= (rv32
< lv32
) ? 1 : -1;
2210 (void) nvlist_lookup_uint64(lnvl
, propname
,
2212 (void) nvlist_lookup_uint64(rnvl
, propname
,
2215 rc
= (rv64
< lv64
) ? 1 : -1;
2217 (void) nvlist_lookup_string(lnvl
, propname
,
2219 (void) nvlist_lookup_string(rnvl
, propname
,
2221 rc
= strcmp(lvstr
, rvstr
);
2225 case ZFS_PROP_QUOTA
:
2228 if (prop
== ZFS_PROP_USED
)
2232 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2233 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2235 rc
= (rv64
< lv64
) ? 1 : -1;
2243 return (reverse
? 1 : -1);
2245 return (reverse
? -1 : 1);
2250 * If entries still seem to be the same, check if they are of the same
2251 * type (smbentity is added only if we are doing SID to POSIX ID
2252 * translation where we can have duplicate type/name combinations).
2254 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2255 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2257 return (lvb
< rvb
? -1 : 1);
2262 static inline const char *
2263 us_type2str(unsigned field_type
)
2265 switch (field_type
) {
2266 case USTYPE_PSX_USR
:
2267 return ("POSIX User");
2268 case USTYPE_PSX_GRP
:
2269 return ("POSIX Group");
2270 case USTYPE_SMB_USR
:
2271 return ("SMB User");
2272 case USTYPE_SMB_GRP
:
2273 return ("SMB Group");
2275 return ("Undefined");
2280 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2282 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2283 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2288 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2289 uu_avl_t
*avl
= cb
->cb_avl
;
2293 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2295 const char *typestr
;
2299 int typeidx
, nameidx
, sizeidx
;
2300 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2301 boolean_t smbentity
= B_FALSE
;
2303 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2305 node
= safe_malloc(sizeof (us_node_t
));
2306 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2307 node
->usn_nvl
= props
;
2309 if (domain
!= NULL
&& domain
[0] != '\0') {
2312 char sid
[ZFS_MAXNAMELEN
+ 32];
2316 directory_error_t e
;
2320 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2322 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2323 type
= USTYPE_SMB_GRP
;
2324 err
= sid_to_id(sid
, B_FALSE
, &id
);
2326 type
= USTYPE_SMB_USR
;
2327 err
= sid_to_id(sid
, B_TRUE
, &id
);
2332 if (!cb
->cb_sid2posix
) {
2333 e
= directory_name_from_sid(NULL
, sid
, &name
,
2336 directory_error_free(e
);
2346 #endif /* HAVE_IDMAP */
2349 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2351 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2352 type
= USTYPE_PSX_GRP
;
2353 if (!cb
->cb_numname
) {
2356 if ((g
= getgrgid(rid
)) != NULL
)
2360 type
= USTYPE_PSX_USR
;
2361 if (!cb
->cb_numname
) {
2364 if ((p
= getpwuid(rid
)) != NULL
)
2371 * Make sure that the type/name combination is unique when doing
2372 * SID to POSIX ID translation (hence changing the type from SMB to
2375 if (cb
->cb_sid2posix
&&
2376 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2379 /* Calculate/update width of TYPE field */
2380 typestr
= us_type2str(type
);
2381 typelen
= strlen(gettext(typestr
));
2382 typeidx
= us_field_index("type");
2383 if (typelen
> cb
->cb_width
[typeidx
])
2384 cb
->cb_width
[typeidx
] = typelen
;
2385 if (nvlist_add_uint32(props
, "type", type
) != 0)
2388 /* Calculate/update width of NAME field */
2389 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2390 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2392 namelen
= snprintf(NULL
, 0, "%u", rid
);
2394 if (nvlist_add_string(props
, "name", name
) != 0)
2396 namelen
= strlen(name
);
2398 nameidx
= us_field_index("name");
2399 if (namelen
> cb
->cb_width
[nameidx
])
2400 cb
->cb_width
[nameidx
] = namelen
;
2403 * Check if this type/name combination is in the list and update it;
2404 * otherwise add new node to the list.
2406 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2407 uu_avl_insert(avl
, node
, idx
);
2412 props
= node
->usn_nvl
;
2415 /* Calculate/update width of USED/QUOTA fields */
2417 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2419 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu",
2420 (u_longlong_t
)space
);
2421 sizelen
= strlen(sizebuf
);
2422 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
) {
2424 if (!nvlist_exists(props
, "quota"))
2425 (void) nvlist_add_uint64(props
, "quota", 0);
2428 if (!nvlist_exists(props
, "used"))
2429 (void) nvlist_add_uint64(props
, "used", 0);
2431 sizeidx
= us_field_index(propname
);
2432 if (sizelen
> cb
->cb_width
[sizeidx
])
2433 cb
->cb_width
[sizeidx
] = sizelen
;
2435 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2442 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2443 size_t *width
, us_node_t
*node
)
2445 nvlist_t
*nvl
= node
->usn_nvl
;
2446 char valstr
[ZFS_MAXNAMELEN
];
2447 boolean_t first
= B_TRUE
;
2453 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2454 if (!(ustype
& types
))
2457 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2458 nvpair_t
*nvp
= NULL
;
2462 char *strval
= NULL
;
2464 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2465 if (strcmp(nvpair_name(nvp
),
2466 us_field_names
[field
]) == 0)
2470 type
= nvpair_type(nvp
);
2472 case DATA_TYPE_UINT32
:
2473 (void) nvpair_value_uint32(nvp
, &val32
);
2475 case DATA_TYPE_UINT64
:
2476 (void) nvpair_value_uint64(nvp
, &val64
);
2478 case DATA_TYPE_STRING
:
2479 (void) nvpair_value_string(nvp
, &strval
);
2482 (void) fprintf(stderr
, "invalid data type\n");
2487 strval
= (char *)us_type2str(val32
);
2490 if (type
== DATA_TYPE_UINT64
) {
2491 (void) sprintf(valstr
, "%llu",
2492 (u_longlong_t
) val64
);
2498 if (type
== DATA_TYPE_UINT64
) {
2500 (void) sprintf(valstr
, "%llu",
2501 (u_longlong_t
) val64
);
2503 zfs_nicenum(val64
, valstr
,
2506 if (field
== USFIELD_QUOTA
&&
2507 strcmp(valstr
, "0") == 0)
2517 (void) printf("\t");
2522 (void) printf("%s", strval
);
2523 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2524 (void) printf("%-*s", (int) width
[field
], strval
);
2526 (void) printf("%*s", (int) width
[field
], strval
);
2532 (void) printf("\n");
2536 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2537 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2545 boolean_t first
= B_TRUE
;
2547 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2548 col
= gettext(us_field_hdr
[field
]);
2549 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2550 (void) printf(first
? "%-*s" : " %-*s",
2551 (int) width
[field
], col
);
2553 (void) printf(first
? "%*s" : " %*s",
2554 (int) width
[field
], col
);
2559 (void) printf("\n");
2562 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2563 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2565 nvlist_free(node
->usn_nvl
);
2570 zfs_do_userspace(int argc
, char **argv
)
2573 zfs_userquota_prop_t p
;
2574 uu_avl_pool_t
*avl_pool
;
2576 uu_avl_walk_t
*walk
;
2578 char deffields
[] = "type,name,used,quota";
2579 char *ofield
= NULL
;
2580 char *tfield
= NULL
;
2584 boolean_t scripted
= B_FALSE
;
2585 boolean_t prtnum
= B_FALSE
;
2586 boolean_t parsable
= B_FALSE
;
2587 boolean_t sid2posix
= B_FALSE
;
2590 zfs_sort_column_t
*sortcol
= NULL
;
2591 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2595 uu_list_pool_t
*listpool
;
2597 uu_avl_index_t idx
= 0;
2598 uu_list_index_t idx2
= 0;
2603 if (strcmp(argv
[0], "groupspace") == 0)
2604 /* Toggle default group types */
2605 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2607 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2623 if (zfs_add_sort_column(&sortcol
, optarg
,
2624 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
2625 (void) fprintf(stderr
,
2626 gettext("invalid field '%s'\n"), optarg
);
2637 (void) fprintf(stderr
, gettext("missing argument for "
2638 "'%c' option\n"), optopt
);
2642 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2652 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2656 (void) fprintf(stderr
, gettext("too many arguments\n"));
2660 /* Use default output fields if not specified using -o */
2664 if ((delim
= strchr(ofield
, ',')) != NULL
)
2666 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
2667 (void) fprintf(stderr
, gettext("invalid type '%s' "
2668 "for -o option\n"), ofield
);
2673 } while (delim
!= NULL
);
2674 fields
[cfield
] = USFIELD_LAST
;
2676 /* Override output types (-t option) */
2677 if (tfield
!= NULL
) {
2681 boolean_t found
= B_FALSE
;
2683 if ((delim
= strchr(tfield
, ',')) != NULL
)
2685 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
2687 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
2689 types
|= us_type_bits
[i
];
2694 (void) fprintf(stderr
, gettext("invalid type "
2695 "'%s' for -t option\n"), tfield
);
2700 } while (delim
!= NULL
);
2703 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
2706 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
2707 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
2709 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
2712 /* Always add default sorting columns */
2713 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
2714 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
2716 cb
.cb_sortcol
= sortcol
;
2717 cb
.cb_numname
= prtnum
;
2718 cb
.cb_nicenum
= !parsable
;
2719 cb
.cb_avl_pool
= avl_pool
;
2720 cb
.cb_avl
= avl_tree
;
2721 cb
.cb_sid2posix
= sid2posix
;
2723 for (i
= 0; i
< USFIELD_LAST
; i
++)
2724 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
2726 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
2727 if (((p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
) &&
2728 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
2729 ((p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
) &&
2730 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))))
2733 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
2738 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
2741 us_populated
= B_TRUE
;
2743 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
2744 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
2745 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
2746 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
2748 while (node
!= NULL
) {
2750 node
= uu_avl_next(avl_tree
, node
);
2751 uu_avl_remove(avl_tree
, rmnode
);
2752 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
2753 uu_list_insert(list
, rmnode
, idx2
);
2756 for (node
= uu_list_first(list
); node
!= NULL
;
2757 node
= uu_list_next(list
, node
)) {
2758 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
2760 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
2761 uu_avl_insert(avl_tree
, node
, idx
);
2764 uu_list_destroy(list
);
2765 uu_list_pool_destroy(listpool
);
2767 /* Print and free node nvlist memory */
2768 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
2771 zfs_free_sort_columns(sortcol
);
2773 /* Clean up the AVL tree */
2774 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
2777 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
2778 uu_avl_remove(cb
.cb_avl
, node
);
2782 uu_avl_walk_end(walk
);
2783 uu_avl_destroy(avl_tree
);
2784 uu_avl_pool_destroy(avl_pool
);
2790 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
2791 * [-s property [-s property]...] [-S property [-S property]...]
2794 * -r Recurse over all children
2795 * -d Limit recursion by depth.
2796 * -H Scripted mode; elide headers and separate columns by tabs
2797 * -o Control which fields to display.
2798 * -t Control which object types to display.
2799 * -s Specify sort columns, descending order.
2800 * -S Specify sort columns, ascending order.
2802 * When given no arguments, lists all filesystems in the system.
2803 * Otherwise, list the specified datasets, optionally recursing down them if
2804 * '-r' is specified.
2806 typedef struct list_cbdata
{
2808 boolean_t cb_scripted
;
2809 zprop_list_t
*cb_proplist
;
2813 * Given a list of columns to display, output appropriate headers for each one.
2816 print_header(zprop_list_t
*pl
)
2818 char headerbuf
[ZFS_MAXPROPLEN
];
2821 boolean_t first
= B_TRUE
;
2822 boolean_t right_justify
;
2824 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2831 right_justify
= B_FALSE
;
2832 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2833 header
= zfs_prop_column_name(pl
->pl_prop
);
2834 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2836 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2837 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2838 headerbuf
[i
] = '\0';
2842 if (pl
->pl_next
== NULL
&& !right_justify
)
2843 (void) printf("%s", header
);
2844 else if (right_justify
)
2845 (void) printf("%*s", (int)pl
->pl_width
, header
);
2847 (void) printf("%-*s", (int)pl
->pl_width
, header
);
2850 (void) printf("\n");
2854 * Given a dataset and a list of fields, print out all the properties according
2855 * to the described layout.
2858 print_dataset(zfs_handle_t
*zhp
, zprop_list_t
*pl
, boolean_t scripted
)
2860 boolean_t first
= B_TRUE
;
2861 char property
[ZFS_MAXPROPLEN
];
2862 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
2865 boolean_t right_justify
;
2868 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2871 (void) printf("\t");
2878 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
2879 (void) strlcpy(property
, zfs_get_name(zhp
),
2882 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2883 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
2884 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
2885 sizeof (property
), NULL
, NULL
, 0, B_FALSE
) != 0)
2890 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2891 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
2892 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
2893 property
, sizeof (property
), B_FALSE
) != 0)
2897 right_justify
= B_TRUE
;
2898 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
2899 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
2900 property
, sizeof (property
), B_FALSE
) != 0)
2904 right_justify
= B_TRUE
;
2906 if (nvlist_lookup_nvlist(userprops
,
2907 pl
->pl_user_prop
, &propval
) != 0)
2910 verify(nvlist_lookup_string(propval
,
2911 ZPROP_VALUE
, &propstr
) == 0);
2912 right_justify
= B_FALSE
;
2915 width
= pl
->pl_width
;
2918 * If this is being called in scripted mode, or if this is the
2919 * last column and it is left-justified, don't include a width
2922 if (scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
2923 (void) printf("%s", propstr
);
2924 else if (right_justify
)
2925 (void) printf("%*s", width
, propstr
);
2927 (void) printf("%-*s", width
, propstr
);
2930 (void) printf("\n");
2934 * Generic callback function to list a dataset or snapshot.
2937 list_callback(zfs_handle_t
*zhp
, void *data
)
2939 list_cbdata_t
*cbp
= data
;
2941 if (cbp
->cb_first
) {
2942 if (!cbp
->cb_scripted
)
2943 print_header(cbp
->cb_proplist
);
2944 cbp
->cb_first
= B_FALSE
;
2947 print_dataset(zhp
, cbp
->cb_proplist
, cbp
->cb_scripted
);
2953 zfs_do_list(int argc
, char **argv
)
2956 boolean_t scripted
= B_FALSE
;
2957 static char default_fields
[] =
2958 "name,used,available,referenced,mountpoint";
2959 int types
= ZFS_TYPE_DATASET
;
2960 boolean_t types_specified
= B_FALSE
;
2961 char *fields
= NULL
;
2962 list_cbdata_t cb
= { 0 };
2966 zfs_sort_column_t
*sortcol
= NULL
;
2967 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
2970 while ((c
= getopt(argc
, argv
, ":d:o:rt:Hs:S:")) != -1) {
2976 limit
= parse_depth(optarg
, &flags
);
2979 flags
|= ZFS_ITER_RECURSE
;
2985 if (zfs_add_sort_column(&sortcol
, optarg
,
2987 (void) fprintf(stderr
,
2988 gettext("invalid property '%s'\n"), optarg
);
2993 if (zfs_add_sort_column(&sortcol
, optarg
,
2995 (void) fprintf(stderr
,
2996 gettext("invalid property '%s'\n"), optarg
);
3002 types_specified
= B_TRUE
;
3003 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3004 while (*optarg
!= '\0') {
3005 static char *type_subopts
[] = { "filesystem",
3006 "volume", "snapshot", "snap", "all", NULL
};
3008 switch (getsubopt(&optarg
, type_subopts
,
3011 types
|= ZFS_TYPE_FILESYSTEM
;
3014 types
|= ZFS_TYPE_VOLUME
;
3018 types
|= ZFS_TYPE_SNAPSHOT
;
3021 types
= ZFS_TYPE_DATASET
;
3025 (void) fprintf(stderr
,
3026 gettext("invalid type '%s'\n"),
3033 (void) fprintf(stderr
, gettext("missing argument for "
3034 "'%c' option\n"), optopt
);
3038 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3048 fields
= default_fields
;
3051 * If we are only going to list snapshot names and sort by name,
3052 * then we can use faster version.
3054 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3055 flags
|= ZFS_ITER_SIMPLE
;
3058 * If "-o space" and no types were specified, don't display snapshots.
3060 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3061 types
&= ~ZFS_TYPE_SNAPSHOT
;
3064 * If the user specifies '-o all', the zprop_get_list() doesn't
3065 * normally include the name of the dataset. For 'zfs list', we always
3066 * want this property to be first.
3068 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3072 cb
.cb_scripted
= scripted
;
3073 cb
.cb_first
= B_TRUE
;
3075 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3076 limit
, list_callback
, &cb
);
3078 zprop_free_list(cb
.cb_proplist
);
3079 zfs_free_sort_columns(sortcol
);
3081 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3082 (void) fprintf(stderr
, gettext("no datasets available\n"));
3088 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3089 * zfs rename [-f] -p <fs | vol> <fs | vol>
3090 * zfs rename -r <snap> <snap>
3092 * Renames the given dataset to another of the same type.
3094 * The '-p' flag creates all the non-existing ancestors of the target first.
3098 zfs_do_rename(int argc
, char **argv
)
3103 boolean_t recurse
= B_FALSE
;
3104 boolean_t parents
= B_FALSE
;
3105 boolean_t force_unmount
= B_FALSE
;
3108 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3117 force_unmount
= B_TRUE
;
3121 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3130 /* check number of arguments */
3132 (void) fprintf(stderr
, gettext("missing source dataset "
3137 (void) fprintf(stderr
, gettext("missing target dataset "
3142 (void) fprintf(stderr
, gettext("too many arguments\n"));
3146 if (recurse
&& parents
) {
3147 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3152 if (recurse
&& strchr(argv
[0], '@') == 0) {
3153 (void) fprintf(stderr
, gettext("source dataset for recursive "
3154 "rename must be a snapshot\n"));
3158 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3159 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3162 /* If we were asked and the name looks good, try to create ancestors. */
3163 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3164 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3169 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3178 * Promotes the given clone fs to be the parent
3182 zfs_do_promote(int argc
, char **argv
)
3188 if (argc
> 1 && argv
[1][0] == '-') {
3189 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3194 /* check number of arguments */
3196 (void) fprintf(stderr
, gettext("missing clone filesystem"
3201 (void) fprintf(stderr
, gettext("too many arguments\n"));
3205 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3209 ret
= (zfs_promote(zhp
) != 0);
3217 * zfs rollback [-rRf] <snapshot>
3219 * -r Delete any intervening snapshots before doing rollback
3220 * -R Delete any snapshots and their clones
3221 * -f ignored for backwards compatability
3223 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3224 * since then and making it the active dataset. If more recent snapshots exist,
3225 * the command will complain unless the '-r' flag is given.
3227 typedef struct rollback_cbdata
{
3233 boolean_t cb_recurse
;
3234 boolean_t cb_dependent
;
3235 } rollback_cbdata_t
;
3238 * Report any snapshots more recent than the one specified. Used when '-r' is
3239 * not specified. We reuse this same callback for the snapshot dependents - if
3240 * 'cb_dependent' is set, then this is a dependent and we should report it
3241 * without checking the transaction group.
3244 rollback_check(zfs_handle_t
*zhp
, void *data
)
3246 rollback_cbdata_t
*cbp
= data
;
3248 if (cbp
->cb_doclones
) {
3253 if (!cbp
->cb_dependent
) {
3254 if (strcmp(zfs_get_name(zhp
), cbp
->cb_target
) != 0 &&
3255 zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
3256 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) >
3259 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3260 (void) fprintf(stderr
, gettext("cannot "
3261 "rollback to '%s': more recent snapshots "
3264 (void) fprintf(stderr
, gettext("use '-r' to "
3265 "force deletion of the following "
3271 if (cbp
->cb_recurse
) {
3272 cbp
->cb_dependent
= B_TRUE
;
3273 if (zfs_iter_dependents(zhp
, B_TRUE
,
3274 rollback_check
, cbp
) != 0) {
3278 cbp
->cb_dependent
= B_FALSE
;
3280 (void) fprintf(stderr
, "%s\n",
3285 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3286 (void) fprintf(stderr
, gettext("cannot rollback to "
3287 "'%s': clones of previous snapshots exist\n"),
3289 (void) fprintf(stderr
, gettext("use '-R' to "
3290 "force deletion of the following clones and "
3296 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3304 zfs_do_rollback(int argc
, char **argv
)
3308 boolean_t force
= B_FALSE
;
3309 rollback_cbdata_t cb
= { 0 };
3310 zfs_handle_t
*zhp
, *snap
;
3311 char parentname
[ZFS_MAXNAMELEN
];
3315 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3328 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3337 /* check number of arguments */
3339 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3343 (void) fprintf(stderr
, gettext("too many arguments\n"));
3347 /* open the snapshot */
3348 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3351 /* open the parent dataset */
3352 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3353 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3355 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3361 * Check for more recent snapshots and/or clones based on the presence
3364 cb
.cb_target
= argv
[0];
3365 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3366 cb
.cb_first
= B_TRUE
;
3368 if ((ret
= zfs_iter_children(zhp
, rollback_check
, &cb
)) != 0)
3371 if ((ret
= cb
.cb_error
) != 0)
3375 * Rollback parent to the given snapshot.
3377 ret
= zfs_rollback(zhp
, snap
, force
);
3390 * zfs set property=value { fs | snap | vol } ...
3392 * Sets the given property for all datasets specified on the command line.
3394 typedef struct set_cbdata
{
3400 set_callback(zfs_handle_t
*zhp
, void *data
)
3402 set_cbdata_t
*cbp
= data
;
3404 if (zfs_prop_set(zhp
, cbp
->cb_propname
, cbp
->cb_value
) != 0) {
3405 switch (libzfs_errno(g_zfs
)) {
3406 case EZFS_MOUNTFAILED
:
3407 (void) fprintf(stderr
, gettext("property may be set "
3408 "but unable to remount filesystem\n"));
3410 case EZFS_SHARENFSFAILED
:
3411 (void) fprintf(stderr
, gettext("property may be set "
3412 "but unable to reshare filesystem\n"));
3421 zfs_do_set(int argc
, char **argv
)
3426 /* check for options */
3427 if (argc
> 1 && argv
[1][0] == '-') {
3428 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3433 /* check number of arguments */
3435 (void) fprintf(stderr
, gettext("missing property=value "
3440 (void) fprintf(stderr
, gettext("missing dataset name\n"));
3444 /* validate property=value argument */
3445 cb
.cb_propname
= argv
[1];
3446 if (((cb
.cb_value
= strchr(cb
.cb_propname
, '=')) == NULL
) ||
3447 (cb
.cb_value
[1] == '\0')) {
3448 (void) fprintf(stderr
, gettext("missing value in "
3449 "property=value argument\n"));
3453 *cb
.cb_value
= '\0';
3456 if (*cb
.cb_propname
== '\0') {
3457 (void) fprintf(stderr
,
3458 gettext("missing property in property=value argument\n"));
3462 ret
= zfs_for_each(argc
- 2, argv
+ 2, 0,
3463 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, &cb
);
3468 typedef struct snap_cbdata
{
3470 boolean_t sd_recursive
;
3471 const char *sd_snapname
;
3475 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3477 snap_cbdata_t
*sd
= arg
;
3482 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3485 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3488 if (sd
->sd_recursive
)
3489 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3495 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3497 * Creates a snapshot with the given name. While functionally equivalent to
3498 * 'zfs create', it is a separate command to differentiate intent.
3501 zfs_do_snapshot(int argc
, char **argv
)
3506 snap_cbdata_t sd
= { 0 };
3507 boolean_t multiple_snaps
= B_FALSE
;
3509 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3511 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3515 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3518 if (parseprop(props
))
3522 sd
.sd_recursive
= B_TRUE
;
3523 multiple_snaps
= B_TRUE
;
3526 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3535 /* check number of arguments */
3537 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3542 multiple_snaps
= B_TRUE
;
3543 for (; argc
> 0; argc
--, argv
++) {
3547 atp
= strchr(argv
[0], '@');
3551 sd
.sd_snapname
= atp
+ 1;
3552 zhp
= zfs_open(g_zfs
, argv
[0],
3553 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3556 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3560 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3561 nvlist_free(sd
.sd_nvl
);
3563 if (ret
!= 0 && multiple_snaps
)
3564 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3568 nvlist_free(sd
.sd_nvl
);
3575 * Send a backup stream to stdout.
3578 zfs_do_send(int argc
, char **argv
)
3580 char *fromname
= NULL
;
3581 char *toname
= NULL
;
3584 sendflags_t flags
= { 0 };
3586 nvlist_t
*dbgnv
= NULL
;
3587 boolean_t extraverbose
= B_FALSE
;
3590 while ((c
= getopt(argc
, argv
, ":i:I:RDpvnP")) != -1) {
3601 flags
.doall
= B_TRUE
;
3604 flags
.replicate
= B_TRUE
;
3607 flags
.props
= B_TRUE
;
3610 flags
.parsable
= B_TRUE
;
3611 flags
.verbose
= B_TRUE
;
3615 extraverbose
= B_TRUE
;
3616 flags
.verbose
= B_TRUE
;
3617 flags
.progress
= B_TRUE
;
3620 flags
.dedup
= B_TRUE
;
3623 flags
.dryrun
= B_TRUE
;
3626 (void) fprintf(stderr
, gettext("missing argument for "
3627 "'%c' option\n"), optopt
);
3631 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3640 /* check number of arguments */
3642 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3646 (void) fprintf(stderr
, gettext("too many arguments\n"));
3650 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
3651 (void) fprintf(stderr
,
3652 gettext("Error: Stream can not be written to a terminal.\n"
3653 "You must redirect standard output.\n"));
3657 cp
= strchr(argv
[0], '@');
3659 (void) fprintf(stderr
,
3660 gettext("argument must be a snapshot\n"));
3665 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3670 * If they specified the full path to the snapshot, chop off
3671 * everything except the short name of the snapshot, but special
3672 * case if they specify the origin.
3674 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
3675 char origin
[ZFS_MAXNAMELEN
];
3678 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
3679 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
3681 if (strcmp(origin
, fromname
) == 0) {
3683 flags
.fromorigin
= B_TRUE
;
3686 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
3687 (void) fprintf(stderr
,
3688 gettext("incremental source must be "
3689 "in same filesystem\n"));
3693 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
3694 (void) fprintf(stderr
,
3695 gettext("invalid incremental source\n"));
3701 if (flags
.replicate
&& fromname
== NULL
)
3702 flags
.doall
= B_TRUE
;
3704 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
3705 extraverbose
? &dbgnv
: NULL
);
3707 if (extraverbose
&& dbgnv
!= NULL
) {
3709 * dump_nvlist prints to stdout, but that's been
3710 * redirected to a file. Make it print to stderr
3713 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
3714 dump_nvlist(dbgnv
, 0);
3723 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3725 * Restore a backup stream from stdin.
3728 zfs_do_receive(int argc
, char **argv
)
3731 recvflags_t flags
= { 0 };
3734 while ((c
= getopt(argc
, argv
, ":denuvF")) != -1) {
3737 flags
.isprefix
= B_TRUE
;
3740 flags
.isprefix
= B_TRUE
;
3741 flags
.istail
= B_TRUE
;
3744 flags
.dryrun
= B_TRUE
;
3747 flags
.nomount
= B_TRUE
;
3750 flags
.verbose
= B_TRUE
;
3753 flags
.force
= B_TRUE
;
3756 (void) fprintf(stderr
, gettext("missing argument for "
3757 "'%c' option\n"), optopt
);
3761 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3770 /* check number of arguments */
3772 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3776 (void) fprintf(stderr
, gettext("too many arguments\n"));
3780 if (isatty(STDIN_FILENO
)) {
3781 (void) fprintf(stderr
,
3782 gettext("Error: Backup stream can not be read "
3783 "from a terminal.\n"
3784 "You must redirect standard input.\n"));
3788 err
= zfs_receive(g_zfs
, argv
[0], &flags
, STDIN_FILENO
, NULL
);
3794 * allow/unallow stuff
3796 /* copied from zfs/sys/dsl_deleg.h */
3797 #define ZFS_DELEG_PERM_CREATE "create"
3798 #define ZFS_DELEG_PERM_DESTROY "destroy"
3799 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3800 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3801 #define ZFS_DELEG_PERM_CLONE "clone"
3802 #define ZFS_DELEG_PERM_PROMOTE "promote"
3803 #define ZFS_DELEG_PERM_RENAME "rename"
3804 #define ZFS_DELEG_PERM_MOUNT "mount"
3805 #define ZFS_DELEG_PERM_SHARE "share"
3806 #define ZFS_DELEG_PERM_SEND "send"
3807 #define ZFS_DELEG_PERM_RECEIVE "receive"
3808 #define ZFS_DELEG_PERM_ALLOW "allow"
3809 #define ZFS_DELEG_PERM_USERPROP "userprop"
3810 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3811 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3812 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3813 #define ZFS_DELEG_PERM_USERUSED "userused"
3814 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3815 #define ZFS_DELEG_PERM_HOLD "hold"
3816 #define ZFS_DELEG_PERM_RELEASE "release"
3817 #define ZFS_DELEG_PERM_DIFF "diff"
3819 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3821 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
3822 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
3823 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
3824 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
3825 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
3826 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
3827 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
3828 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
3829 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
3830 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
3831 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
3832 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
3833 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
3834 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
3835 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
3836 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
3838 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
3839 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
3840 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
3841 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
3842 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
3843 { NULL
, ZFS_DELEG_NOTE_NONE
}
3846 /* permission structure */
3847 typedef struct deleg_perm
{
3848 zfs_deleg_who_type_t dp_who_type
;
3849 const char *dp_name
;
3851 boolean_t dp_descend
;
3855 typedef struct deleg_perm_node
{
3856 deleg_perm_t dpn_perm
;
3858 uu_avl_node_t dpn_avl_node
;
3859 } deleg_perm_node_t
;
3861 typedef struct fs_perm fs_perm_t
;
3863 /* permissions set */
3864 typedef struct who_perm
{
3865 zfs_deleg_who_type_t who_type
;
3866 const char *who_name
; /* id */
3867 char who_ug_name
[256]; /* user/group name */
3868 fs_perm_t
*who_fsperm
; /* uplink */
3870 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
3874 typedef struct who_perm_node
{
3875 who_perm_t who_perm
;
3876 uu_avl_node_t who_avl_node
;
3879 typedef struct fs_perm_set fs_perm_set_t
;
3880 /* fs permissions */
3882 const char *fsp_name
;
3884 uu_avl_t
*fsp_sc_avl
; /* sets,create */
3885 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
3887 fs_perm_set_t
*fsp_set
; /* uplink */
3891 typedef struct fs_perm_node
{
3892 fs_perm_t fspn_fsperm
;
3895 uu_list_node_t fspn_list_node
;
3898 /* top level structure */
3899 struct fs_perm_set
{
3900 uu_list_pool_t
*fsps_list_pool
;
3901 uu_list_t
*fsps_list
; /* list of fs_perms */
3903 uu_avl_pool_t
*fsps_named_set_avl_pool
;
3904 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
3905 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
3908 static inline const char *
3909 deleg_perm_type(zfs_deleg_note_t note
)
3915 case ZFS_DELEG_NOTE_GROUPQUOTA
:
3916 case ZFS_DELEG_NOTE_GROUPUSED
:
3917 case ZFS_DELEG_NOTE_USERPROP
:
3918 case ZFS_DELEG_NOTE_USERQUOTA
:
3919 case ZFS_DELEG_NOTE_USERUSED
:
3921 return (gettext("other"));
3923 return (gettext("subcommand"));
3928 who_type2weight(zfs_deleg_who_type_t who_type
)
3932 case ZFS_DELEG_NAMED_SET_SETS
:
3933 case ZFS_DELEG_NAMED_SET
:
3936 case ZFS_DELEG_CREATE_SETS
:
3937 case ZFS_DELEG_CREATE
:
3940 case ZFS_DELEG_USER_SETS
:
3941 case ZFS_DELEG_USER
:
3944 case ZFS_DELEG_GROUP_SETS
:
3945 case ZFS_DELEG_GROUP
:
3948 case ZFS_DELEG_EVERYONE_SETS
:
3949 case ZFS_DELEG_EVERYONE
:
3961 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
3963 const who_perm_node_t
*l
= larg
;
3964 const who_perm_node_t
*r
= rarg
;
3965 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
3966 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
3967 int lweight
= who_type2weight(ltype
);
3968 int rweight
= who_type2weight(rtype
);
3969 int res
= lweight
- rweight
;
3971 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
3972 ZFS_MAX_DELEG_NAME
-1);
3984 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
3986 const deleg_perm_node_t
*l
= larg
;
3987 const deleg_perm_node_t
*r
= rarg
;
3988 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
3989 ZFS_MAX_DELEG_NAME
-1);
4001 fs_perm_set_init(fs_perm_set_t
*fspset
)
4003 bzero(fspset
, sizeof (fs_perm_set_t
));
4005 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4006 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4007 NULL
, UU_DEFAULT
)) == NULL
)
4009 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4010 UU_DEFAULT
)) == NULL
)
4013 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4014 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4015 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4016 UU_DEFAULT
)) == NULL
)
4019 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4020 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4021 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4022 UU_DEFAULT
)) == NULL
)
4025 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4026 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4027 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4032 static inline void fs_perm_fini(fs_perm_t
*);
4033 static inline void who_perm_fini(who_perm_t
*);
4036 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4038 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4040 while (node
!= NULL
) {
4041 fs_perm_node_t
*next_node
=
4042 uu_list_next(fspset
->fsps_list
, node
);
4043 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4044 fs_perm_fini(fsperm
);
4045 uu_list_remove(fspset
->fsps_list
, node
);
4050 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4051 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4052 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4056 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4059 deleg_perm
->dp_who_type
= type
;
4060 deleg_perm
->dp_name
= name
;
4064 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4065 zfs_deleg_who_type_t type
, const char *name
)
4067 uu_avl_pool_t
*pool
;
4068 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4070 bzero(who_perm
, sizeof (who_perm_t
));
4072 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4073 UU_DEFAULT
)) == NULL
)
4076 who_perm
->who_type
= type
;
4077 who_perm
->who_name
= name
;
4078 who_perm
->who_fsperm
= fsperm
;
4082 who_perm_fini(who_perm_t
*who_perm
)
4084 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4086 while (node
!= NULL
) {
4087 deleg_perm_node_t
*next_node
=
4088 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4090 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4095 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4099 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4101 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4102 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4104 bzero(fsperm
, sizeof (fs_perm_t
));
4106 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4110 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4114 fsperm
->fsp_set
= fspset
;
4115 fsperm
->fsp_name
= fsname
;
4119 fs_perm_fini(fs_perm_t
*fsperm
)
4121 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4122 while (node
!= NULL
) {
4123 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4125 who_perm_t
*who_perm
= &node
->who_perm
;
4126 who_perm_fini(who_perm
);
4127 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4132 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4133 while (node
!= NULL
) {
4134 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4136 who_perm_t
*who_perm
= &node
->who_perm
;
4137 who_perm_fini(who_perm
);
4138 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4143 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4144 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4148 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4149 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4151 uu_avl_index_t idx
= 0;
4153 deleg_perm_node_t
*found_node
= NULL
;
4154 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4156 deleg_perm_init(deleg_perm
, who_type
, name
);
4158 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4160 uu_avl_insert(avl
, node
, idx
);
4163 deleg_perm
= &node
->dpn_perm
;
4168 case ZFS_DELEG_LOCAL
:
4169 deleg_perm
->dp_local
= B_TRUE
;
4171 case ZFS_DELEG_DESCENDENT
:
4172 deleg_perm
->dp_descend
= B_TRUE
;
4177 assert(B_FALSE
); /* invalid locality */
4182 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4184 nvpair_t
*nvp
= NULL
;
4185 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4186 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4187 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4189 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4190 const char *name
= nvpair_name(nvp
);
4191 data_type_t type
= nvpair_type(nvp
);
4192 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4193 deleg_perm_node_t
*node
=
4194 safe_malloc(sizeof (deleg_perm_node_t
));
4196 VERIFY(type
== DATA_TYPE_BOOLEAN
);
4198 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4199 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4206 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4208 nvpair_t
*nvp
= NULL
;
4209 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4211 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4212 nvlist_t
*nvl2
= NULL
;
4213 const char *name
= nvpair_name(nvp
);
4214 uu_avl_t
*avl
= NULL
;
4215 uu_avl_pool_t
*avl_pool
= NULL
;
4216 zfs_deleg_who_type_t perm_type
= name
[0];
4217 char perm_locality
= name
[1];
4218 const char *perm_name
= name
+ 3;
4219 boolean_t is_set
= B_TRUE
;
4220 who_perm_t
*who_perm
= NULL
;
4222 assert('$' == name
[2]);
4224 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4227 switch (perm_type
) {
4228 case ZFS_DELEG_CREATE
:
4229 case ZFS_DELEG_CREATE_SETS
:
4230 case ZFS_DELEG_NAMED_SET
:
4231 case ZFS_DELEG_NAMED_SET_SETS
:
4232 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4233 avl
= fsperm
->fsp_sc_avl
;
4235 case ZFS_DELEG_USER
:
4236 case ZFS_DELEG_USER_SETS
:
4237 case ZFS_DELEG_GROUP
:
4238 case ZFS_DELEG_GROUP_SETS
:
4239 case ZFS_DELEG_EVERYONE
:
4240 case ZFS_DELEG_EVERYONE_SETS
:
4241 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4242 avl
= fsperm
->fsp_uge_avl
;
4249 who_perm_node_t
*found_node
= NULL
;
4250 who_perm_node_t
*node
= safe_malloc(
4251 sizeof (who_perm_node_t
));
4252 who_perm
= &node
->who_perm
;
4253 uu_avl_index_t idx
= 0;
4255 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4256 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4258 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4260 if (avl
== fsperm
->fsp_uge_avl
) {
4262 struct passwd
*p
= NULL
;
4263 struct group
*g
= NULL
;
4264 const char *nice_name
= NULL
;
4266 switch (perm_type
) {
4267 case ZFS_DELEG_USER_SETS
:
4268 case ZFS_DELEG_USER
:
4269 rid
= atoi(perm_name
);
4272 nice_name
= p
->pw_name
;
4274 case ZFS_DELEG_GROUP_SETS
:
4275 case ZFS_DELEG_GROUP
:
4276 rid
= atoi(perm_name
);
4279 nice_name
= g
->gr_name
;
4285 if (nice_name
!= NULL
)
4287 node
->who_perm
.who_ug_name
,
4291 uu_avl_insert(avl
, node
, idx
);
4294 who_perm
= &node
->who_perm
;
4298 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4305 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4307 nvpair_t
*nvp
= NULL
;
4308 uu_avl_index_t idx
= 0;
4310 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4311 nvlist_t
*nvl2
= NULL
;
4312 const char *fsname
= nvpair_name(nvp
);
4313 data_type_t type
= nvpair_type(nvp
);
4314 fs_perm_t
*fsperm
= NULL
;
4315 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4319 fsperm
= &node
->fspn_fsperm
;
4321 VERIFY(DATA_TYPE_NVLIST
== type
);
4323 uu_list_node_init(node
, &node
->fspn_list_node
,
4324 fspset
->fsps_list_pool
);
4326 idx
= uu_list_numnodes(fspset
->fsps_list
);
4327 fs_perm_init(fsperm
, fspset
, fsname
);
4329 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4332 (void) parse_fs_perm(fsperm
, nvl2
);
4334 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4340 static inline const char *
4341 deleg_perm_comment(zfs_deleg_note_t note
)
4343 const char *str
= "";
4348 case ZFS_DELEG_NOTE_ALLOW
:
4349 str
= gettext("Must also have the permission that is being"
4350 "\n\t\t\t\tallowed");
4352 case ZFS_DELEG_NOTE_CLONE
:
4353 str
= gettext("Must also have the 'create' ability and 'mount'"
4354 "\n\t\t\t\tability in the origin file system");
4356 case ZFS_DELEG_NOTE_CREATE
:
4357 str
= gettext("Must also have the 'mount' ability");
4359 case ZFS_DELEG_NOTE_DESTROY
:
4360 str
= gettext("Must also have the 'mount' ability");
4362 case ZFS_DELEG_NOTE_DIFF
:
4363 str
= gettext("Allows lookup of paths within a dataset;"
4364 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4365 "\n\t\t\t\tin order to use zfs diff");
4367 case ZFS_DELEG_NOTE_HOLD
:
4368 str
= gettext("Allows adding a user hold to a snapshot");
4370 case ZFS_DELEG_NOTE_MOUNT
:
4371 str
= gettext("Allows mount/umount of ZFS datasets");
4373 case ZFS_DELEG_NOTE_PROMOTE
:
4374 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4375 " 'promote' ability in the origin file system");
4377 case ZFS_DELEG_NOTE_RECEIVE
:
4378 str
= gettext("Must also have the 'mount' and 'create'"
4381 case ZFS_DELEG_NOTE_RELEASE
:
4382 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4383 "might destroy the snapshot");
4385 case ZFS_DELEG_NOTE_RENAME
:
4386 str
= gettext("Must also have the 'mount' and 'create'"
4387 "\n\t\t\t\tability in the new parent");
4389 case ZFS_DELEG_NOTE_ROLLBACK
:
4392 case ZFS_DELEG_NOTE_SEND
:
4395 case ZFS_DELEG_NOTE_SHARE
:
4396 str
= gettext("Allows sharing file systems over NFS or SMB"
4397 "\n\t\t\t\tprotocols");
4399 case ZFS_DELEG_NOTE_SNAPSHOT
:
4403 * case ZFS_DELEG_NOTE_VSCAN:
4404 * str = gettext("");
4408 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4409 str
= gettext("Allows accessing any groupquota@... property");
4411 case ZFS_DELEG_NOTE_GROUPUSED
:
4412 str
= gettext("Allows reading any groupused@... property");
4414 case ZFS_DELEG_NOTE_USERPROP
:
4415 str
= gettext("Allows changing any user property");
4417 case ZFS_DELEG_NOTE_USERQUOTA
:
4418 str
= gettext("Allows accessing any userquota@... property");
4420 case ZFS_DELEG_NOTE_USERUSED
:
4421 str
= gettext("Allows reading any userused@... property");
4439 boolean_t recursive
; /* unallow only */
4440 boolean_t prt_usage
;
4442 boolean_t prt_perms
;
4445 const char *dataset
;
4449 prop_cmp(const void *a
, const void *b
)
4451 const char *str1
= *(const char **)a
;
4452 const char *str2
= *(const char **)b
;
4453 return (strcmp(str1
, str2
));
4457 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
4459 const char *opt_desc
[] = {
4460 "-h", gettext("show this help message and exit"),
4461 "-l", gettext("set permission locally"),
4462 "-d", gettext("set permission for descents"),
4463 "-u", gettext("set permission for user"),
4464 "-g", gettext("set permission for group"),
4465 "-e", gettext("set permission for everyone"),
4466 "-c", gettext("set create time permission"),
4467 "-s", gettext("define permission set"),
4469 "-r", gettext("remove permissions recursively"),
4471 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
4472 size_t allow_size
= unallow_size
- 2;
4473 const char *props
[ZFS_NUM_PROPS
];
4476 FILE *fp
= requested
? stdout
: stderr
;
4477 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
4478 const char *fmt
= gettext("%-16s %-14s\t%s\n");
4480 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
4482 (void) fprintf(fp
, gettext("Options:\n"));
4483 for (i
= 0; i
< (un
? unallow_size
: allow_size
); i
++) {
4484 const char *opt
= opt_desc
[i
++];
4485 const char *optdsc
= opt_desc
[i
];
4486 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
4489 (void) fprintf(fp
, gettext("\nThe following permissions are "
4491 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
4493 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
4494 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
4495 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
4496 const char *perm_type
= deleg_perm_type(perm_note
);
4497 const char *perm_comment
= deleg_perm_comment(perm_note
);
4498 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
4501 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
4502 zprop_desc_t
*pd
= &pdtbl
[i
];
4503 if (pd
->pd_visible
!= B_TRUE
)
4506 if (pd
->pd_attr
== PROP_READONLY
)
4509 props
[count
++] = pd
->pd_name
;
4511 props
[count
] = NULL
;
4513 qsort(props
, count
, sizeof (char *), prop_cmp
);
4515 for (i
= 0; i
< count
; i
++)
4516 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
4519 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
4521 exit(requested
? 0 : 2);
4524 static inline const char *
4525 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
4528 if (un
&& argc
== expected_argc
- 1)
4530 else if (argc
== expected_argc
)
4531 *permsp
= argv
[argc
- 2];
4533 allow_usage(un
, B_FALSE
,
4534 gettext("wrong number of parameters\n"));
4536 return (argv
[argc
- 1]);
4540 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
4542 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
4543 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
4544 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
4545 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
4548 allow_usage(un
, B_FALSE
,
4549 gettext("-u, -g, and -e are mutually exclusive\n"));
4551 if (opts
->prt_usage
) {
4552 if (argc
== 0 && all_sum
== 0)
4553 allow_usage(un
, B_TRUE
, NULL
);
4560 allow_usage(un
, B_FALSE
,
4561 gettext("invalid options combined with -s\n"));
4563 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4564 if (argv
[0][0] != '@')
4565 allow_usage(un
, B_FALSE
,
4566 gettext("invalid set name: missing '@' prefix\n"));
4567 opts
->who
= argv
[0];
4568 } else if (opts
->create
) {
4569 if (ldcsuge_sum
> 1)
4570 allow_usage(un
, B_FALSE
,
4571 gettext("invalid options combined with -c\n"));
4572 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4573 } else if (opts
->everyone
) {
4575 allow_usage(un
, B_FALSE
,
4576 gettext("invalid options combined with -e\n"));
4577 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4578 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
4580 opts
->everyone
= B_TRUE
;
4583 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4584 } else if (argc
== 1 && !un
) {
4585 opts
->prt_perms
= B_TRUE
;
4586 opts
->dataset
= argv
[argc
-1];
4588 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4589 opts
->who
= argv
[0];
4592 if (!opts
->local
&& !opts
->descend
) {
4593 opts
->local
= B_TRUE
;
4594 opts
->descend
= B_TRUE
;
4599 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
4600 const char *who
, char *perms
, nvlist_t
*top_nvl
)
4603 char ld
[2] = { '\0', '\0' };
4604 char who_buf
[ZFS_MAXNAMELEN
+32];
4605 char base_type
= ZFS_DELEG_WHO_UNKNOWN
;
4606 char set_type
= ZFS_DELEG_WHO_UNKNOWN
;
4607 nvlist_t
*base_nvl
= NULL
;
4608 nvlist_t
*set_nvl
= NULL
;
4611 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4613 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4617 case ZFS_DELEG_NAMED_SET_SETS
:
4618 case ZFS_DELEG_NAMED_SET
:
4619 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
4620 base_type
= ZFS_DELEG_NAMED_SET
;
4621 ld
[0] = ZFS_DELEG_NA
;
4623 case ZFS_DELEG_CREATE_SETS
:
4624 case ZFS_DELEG_CREATE
:
4625 set_type
= ZFS_DELEG_CREATE_SETS
;
4626 base_type
= ZFS_DELEG_CREATE
;
4627 ld
[0] = ZFS_DELEG_NA
;
4629 case ZFS_DELEG_USER_SETS
:
4630 case ZFS_DELEG_USER
:
4631 set_type
= ZFS_DELEG_USER_SETS
;
4632 base_type
= ZFS_DELEG_USER
;
4634 ld
[0] = ZFS_DELEG_LOCAL
;
4636 ld
[1] = ZFS_DELEG_DESCENDENT
;
4638 case ZFS_DELEG_GROUP_SETS
:
4639 case ZFS_DELEG_GROUP
:
4640 set_type
= ZFS_DELEG_GROUP_SETS
;
4641 base_type
= ZFS_DELEG_GROUP
;
4643 ld
[0] = ZFS_DELEG_LOCAL
;
4645 ld
[1] = ZFS_DELEG_DESCENDENT
;
4647 case ZFS_DELEG_EVERYONE_SETS
:
4648 case ZFS_DELEG_EVERYONE
:
4649 set_type
= ZFS_DELEG_EVERYONE_SETS
;
4650 base_type
= ZFS_DELEG_EVERYONE
;
4652 ld
[0] = ZFS_DELEG_LOCAL
;
4654 ld
[1] = ZFS_DELEG_DESCENDENT
;
4659 if (perms
!= NULL
) {
4661 char *end
= curr
+ strlen(perms
);
4663 while (curr
< end
) {
4664 char *delim
= strchr(curr
, ',');
4675 (void) nvlist_add_boolean(nvl
, curr
);
4681 for (i
= 0; i
< 2; i
++) {
4682 char locality
= ld
[i
];
4686 if (!nvlist_empty(base_nvl
)) {
4688 (void) snprintf(who_buf
,
4689 sizeof (who_buf
), "%c%c$%s",
4690 base_type
, locality
, who
);
4692 (void) snprintf(who_buf
,
4693 sizeof (who_buf
), "%c%c$",
4694 base_type
, locality
);
4696 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
4701 if (!nvlist_empty(set_nvl
)) {
4703 (void) snprintf(who_buf
,
4704 sizeof (who_buf
), "%c%c$%s",
4705 set_type
, locality
, who
);
4707 (void) snprintf(who_buf
,
4708 sizeof (who_buf
), "%c%c$",
4709 set_type
, locality
);
4711 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
4716 for (i
= 0; i
< 2; i
++) {
4717 char locality
= ld
[i
];
4722 (void) snprintf(who_buf
, sizeof (who_buf
),
4723 "%c%c$%s", base_type
, locality
, who
);
4725 (void) snprintf(who_buf
, sizeof (who_buf
),
4726 "%c%c$", base_type
, locality
);
4727 (void) nvlist_add_boolean(top_nvl
, who_buf
);
4730 (void) snprintf(who_buf
, sizeof (who_buf
),
4731 "%c%c$%s", set_type
, locality
, who
);
4733 (void) snprintf(who_buf
, sizeof (who_buf
),
4734 "%c%c$", set_type
, locality
);
4735 (void) nvlist_add_boolean(top_nvl
, who_buf
);
4741 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
4743 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
4747 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
4748 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
4749 } else if (opts
->create
) {
4750 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
4751 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
4752 } else if (opts
->everyone
) {
4753 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
4754 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
4756 char *curr
= opts
->who
;
4757 char *end
= curr
+ strlen(curr
);
4759 while (curr
< end
) {
4761 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
4763 char *delim
= strchr(curr
, ',');
4766 struct passwd
*p
= NULL
;
4767 struct group
*g
= NULL
;
4775 rid
= (uid_t
)strtol(curr
, &endch
, 0);
4777 who_type
= ZFS_DELEG_USER
;
4786 (void) snprintf(errbuf
, 256, gettext(
4787 "invalid user %s"), curr
);
4788 allow_usage(un
, B_TRUE
, errbuf
);
4790 } else if (opts
->group
) {
4791 who_type
= ZFS_DELEG_GROUP
;
4800 (void) snprintf(errbuf
, 256, gettext(
4801 "invalid group %s"), curr
);
4802 allow_usage(un
, B_TRUE
, errbuf
);
4805 if (*endch
!= '\0') {
4812 if (*endch
!= '\0') {
4820 who_type
= ZFS_DELEG_USER
;
4822 } else if (g
!= NULL
) {
4823 who_type
= ZFS_DELEG_GROUP
;
4826 (void) snprintf(errbuf
, 256, gettext(
4827 "invalid user/group %s"), curr
);
4828 allow_usage(un
, B_TRUE
, errbuf
);
4832 (void) sprintf(id
, "%u", rid
);
4835 store_allow_perm(who_type
, opts
->local
,
4836 opts
->descend
, who
, opts
->perms
, *nvlp
);
4845 print_set_creat_perms(uu_avl_t
*who_avl
)
4847 const char *sc_title
[] = {
4848 gettext("Permission sets:\n"),
4849 gettext("Create time permissions:\n"),
4852 const char **title_ptr
= sc_title
;
4853 who_perm_node_t
*who_node
= NULL
;
4854 int prev_weight
= -1;
4856 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
4857 who_node
= uu_avl_next(who_avl
, who_node
)) {
4858 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
4859 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
4860 const char *who_name
= who_node
->who_perm
.who_name
;
4861 int weight
= who_type2weight(who_type
);
4862 boolean_t first
= B_TRUE
;
4863 deleg_perm_node_t
*deleg_node
;
4865 if (prev_weight
!= weight
) {
4866 (void) printf("%s", *title_ptr
++);
4867 prev_weight
= weight
;
4870 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
4871 (void) printf("\t");
4873 (void) printf("\t%s ", who_name
);
4875 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
4876 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
4879 deleg_node
->dpn_perm
.dp_name
);
4882 (void) printf(",%s",
4883 deleg_node
->dpn_perm
.dp_name
);
4886 (void) printf("\n");
4891 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
4894 who_perm_node_t
*who_node
= NULL
;
4895 boolean_t prt_title
= B_TRUE
;
4896 uu_avl_walk_t
*walk
;
4898 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
4901 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
4902 const char *who_name
= who_node
->who_perm
.who_name
;
4903 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
4904 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
4905 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
4907 deleg_perm_node_t
*deleg_node
;
4908 boolean_t prt_who
= B_TRUE
;
4910 for (deleg_node
= uu_avl_first(avl
);
4912 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
4913 if (local
!= deleg_node
->dpn_perm
.dp_local
||
4914 descend
!= deleg_node
->dpn_perm
.dp_descend
)
4918 const char *who
= NULL
;
4920 prt_title
= B_FALSE
;
4921 (void) printf("%s", title
);
4925 case ZFS_DELEG_USER_SETS
:
4926 case ZFS_DELEG_USER
:
4927 who
= gettext("user");
4929 who_name
= nice_who_name
;
4931 case ZFS_DELEG_GROUP_SETS
:
4932 case ZFS_DELEG_GROUP
:
4933 who
= gettext("group");
4935 who_name
= nice_who_name
;
4937 case ZFS_DELEG_EVERYONE_SETS
:
4938 case ZFS_DELEG_EVERYONE
:
4939 who
= gettext("everyone");
4946 if (who_name
== NULL
)
4947 (void) printf("\t%s", who
);
4949 (void) printf("\t%s %s", who
, who_name
);
4952 (void) printf("%c%s", delim
,
4953 deleg_node
->dpn_perm
.dp_name
);
4958 (void) printf("\n");
4961 uu_avl_walk_end(walk
);
4965 print_fs_perms(fs_perm_set_t
*fspset
)
4967 fs_perm_node_t
*node
= NULL
;
4968 char buf
[ZFS_MAXNAMELEN
+32];
4969 const char *dsname
= buf
;
4971 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
4972 node
= uu_list_next(fspset
->fsps_list
, node
)) {
4973 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
4974 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
4977 (void) snprintf(buf
, ZFS_MAXNAMELEN
+32,
4978 gettext("---- Permissions on %s "),
4979 node
->fspn_fsperm
.fsp_name
);
4980 (void) printf("%s", dsname
);
4981 left
= 70 - strlen(buf
);
4984 (void) printf("\n");
4986 print_set_creat_perms(sc_avl
);
4987 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
4988 gettext("Local permissions:\n"));
4989 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
4990 gettext("Descendent permissions:\n"));
4991 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
4992 gettext("Local+Descendent permissions:\n"));
4996 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
4998 struct deleg_perms
{
5004 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5006 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5007 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5009 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5012 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5016 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5019 nvlist_t
*perm_nvl
= NULL
;
5020 nvlist_t
*update_perm_nvl
= NULL
;
5023 struct allow_opts opts
= { 0 };
5025 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5028 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5031 opts
.local
= B_TRUE
;
5034 opts
.descend
= B_TRUE
;
5040 opts
.group
= B_TRUE
;
5043 opts
.everyone
= B_TRUE
;
5049 opts
.create
= B_TRUE
;
5052 opts
.recursive
= B_TRUE
;
5055 (void) fprintf(stderr
, gettext("missing argument for "
5056 "'%c' option\n"), optopt
);
5060 opts
.prt_usage
= B_TRUE
;
5063 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5072 /* check arguments */
5073 parse_allow_args(argc
, argv
, un
, &opts
);
5075 /* try to open the dataset */
5076 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5077 ZFS_TYPE_VOLUME
)) == NULL
) {
5078 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5083 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5086 fs_perm_set_init(&fs_perm_set
);
5087 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5088 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5093 print_fs_perms(&fs_perm_set
);
5095 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5096 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5099 if (un
&& opts
.recursive
) {
5100 struct deleg_perms data
= { un
, update_perm_nvl
};
5101 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5110 nvlist_free(perm_nvl
);
5111 if (update_perm_nvl
!= NULL
)
5112 nvlist_free(update_perm_nvl
);
5114 fs_perm_set_fini(&fs_perm_set
);
5122 zfs_do_allow(int argc
, char **argv
)
5124 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5128 zfs_do_unallow(int argc
, char **argv
)
5130 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5134 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5139 boolean_t recursive
= B_FALSE
;
5140 const char *opts
= holding
? "rt" : "r";
5144 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5150 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5159 /* check number of arguments */
5167 if (holding
&& tag
[0] == '.') {
5168 /* tags starting with '.' are reserved for libzfs */
5169 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5173 for (i
= 0; i
< argc
; ++i
) {
5175 char parent
[ZFS_MAXNAMELEN
];
5177 char *path
= argv
[i
];
5179 delim
= strchr(path
, '@');
5180 if (delim
== NULL
) {
5181 (void) fprintf(stderr
,
5182 gettext("'%s' is not a snapshot\n"), path
);
5186 (void) strncpy(parent
, path
, delim
- path
);
5187 parent
[delim
- path
] = '\0';
5189 zhp
= zfs_open(g_zfs
, parent
,
5190 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5196 if (zfs_hold(zhp
, delim
+1, tag
, recursive
,
5200 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5206 return (errors
!= 0);
5210 * zfs hold [-r] [-t] <tag> <snap> ...
5212 * -r Recursively hold
5214 * Apply a user-hold with the given tag to the list of snapshots.
5217 zfs_do_hold(int argc
, char **argv
)
5219 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5223 * zfs release [-r] <tag> <snap> ...
5225 * -r Recursively release
5227 * Release a user-hold with the given tag from the list of snapshots.
5230 zfs_do_release(int argc
, char **argv
)
5232 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5235 typedef struct holds_cbdata
{
5236 boolean_t cb_recursive
;
5237 const char *cb_snapname
;
5239 size_t cb_max_namelen
;
5240 size_t cb_max_taglen
;
5243 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5244 #define DATETIME_BUF_LEN (32)
5249 print_holds(boolean_t scripted
, int nwidth
, int tagwidth
, nvlist_t
*nvl
)
5252 nvpair_t
*nvp
= NULL
;
5253 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5257 for (i
= 0; i
< 3; i
++) {
5258 col
= gettext(hdr_cols
[i
]);
5260 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5263 (void) printf("%s\n", col
);
5267 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5268 char *zname
= nvpair_name(nvp
);
5270 nvpair_t
*nvp2
= NULL
;
5271 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5272 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5273 char tsbuf
[DATETIME_BUF_LEN
];
5274 char *tagname
= nvpair_name(nvp2
);
5278 char sep
= scripted
? '\t' : ' ';
5279 int sepnum
= scripted
? 1 : 2;
5281 (void) nvpair_value_uint64(nvp2
, &val
);
5283 (void) localtime_r(&time
, &t
);
5284 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5285 gettext(STRFTIME_FMT_STR
), &t
);
5287 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth
, zname
,
5288 sepnum
, sep
, tagwidth
, tagname
, sepnum
, sep
, tsbuf
);
5294 * Generic callback function to list a dataset or snapshot.
5297 holds_callback(zfs_handle_t
*zhp
, void *data
)
5299 holds_cbdata_t
*cbp
= data
;
5300 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5301 nvlist_t
*nvl
= NULL
;
5302 nvpair_t
*nvp
= NULL
;
5303 const char *zname
= zfs_get_name(zhp
);
5304 size_t znamelen
= strnlen(zname
, ZFS_MAXNAMELEN
);
5306 if (cbp
->cb_recursive
) {
5307 const char *snapname
;
5308 char *delim
= strchr(zname
, '@');
5312 snapname
= delim
+ 1;
5313 if (strcmp(cbp
->cb_snapname
, snapname
))
5317 if (zfs_get_holds(zhp
, &nvl
) != 0)
5320 if (znamelen
> cbp
->cb_max_namelen
)
5321 cbp
->cb_max_namelen
= znamelen
;
5323 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5324 const char *tag
= nvpair_name(nvp
);
5325 size_t taglen
= strnlen(tag
, MAXNAMELEN
);
5326 if (taglen
> cbp
->cb_max_taglen
)
5327 cbp
->cb_max_taglen
= taglen
;
5330 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5334 * zfs holds [-r] <snap> ...
5336 * -r Recursively hold
5339 zfs_do_holds(int argc
, char **argv
)
5344 boolean_t scripted
= B_FALSE
;
5345 boolean_t recursive
= B_FALSE
;
5346 const char *opts
= "rH";
5349 int types
= ZFS_TYPE_SNAPSHOT
;
5350 holds_cbdata_t cb
= { 0 };
5357 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5366 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5373 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
5374 flags
|= ZFS_ITER_RECURSE
;
5380 /* check number of arguments */
5384 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5387 for (i
= 0; i
< argc
; ++i
) {
5388 char *snapshot
= argv
[i
];
5390 const char *snapname
;
5392 delim
= strchr(snapshot
, '@');
5393 if (delim
== NULL
) {
5394 (void) fprintf(stderr
,
5395 gettext("'%s' is not a snapshot\n"), snapshot
);
5399 snapname
= delim
+ 1;
5401 snapshot
[delim
- snapshot
] = '\0';
5403 cb
.cb_recursive
= recursive
;
5404 cb
.cb_snapname
= snapname
;
5408 * 1. collect holds data, set format options
5410 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
5411 holds_callback
, &cb
);
5417 * 2. print holds data
5419 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
5421 if (nvlist_empty(nvl
))
5422 (void) fprintf(stderr
, gettext("no datasets available\n"));
5426 return (0 != errors
);
5429 #define CHECK_SPINNER 30
5430 #define SPINNER_TIME 3 /* seconds */
5431 #define MOUNT_TIME 5 /* seconds */
5434 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
5436 static char *spin
[] = { "-", "\\", "|", "/" };
5437 static int spinval
= 0;
5438 static int spincheck
= 0;
5439 static time_t last_spin_time
= (time_t)0;
5440 get_all_cb_t
*cbp
= data
;
5441 zfs_type_t type
= zfs_get_type(zhp
);
5443 if (cbp
->cb_verbose
) {
5444 if (--spincheck
< 0) {
5445 time_t now
= time(NULL
);
5446 if (last_spin_time
+ SPINNER_TIME
< now
) {
5447 update_progress(spin
[spinval
++ % 4]);
5448 last_spin_time
= now
;
5450 spincheck
= CHECK_SPINNER
;
5455 * Iterate over any nested datasets.
5457 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
5463 * Skip any datasets whose type does not match.
5465 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
5469 libzfs_add_handle(cbp
, zhp
);
5470 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
5476 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
5478 get_all_cb_t cb
= { 0 };
5479 cb
.cb_verbose
= verbose
;
5480 cb
.cb_getone
= get_one_dataset
;
5483 set_progress_header(gettext("Reading ZFS config"));
5484 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
5486 *dslist
= cb
.cb_handles
;
5487 *count
= cb
.cb_used
;
5490 finish_progress(gettext("done."));
5494 * Generic callback for sharing or mounting filesystems. Because the code is so
5495 * similar, we have a common function with an extra parameter to determine which
5496 * mode we are using.
5498 #define OP_SHARE 0x1
5499 #define OP_MOUNT 0x2
5502 * Share or mount a dataset.
5505 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
5506 boolean_t
explicit, const char *options
)
5508 char mountpoint
[ZFS_MAXPROPLEN
];
5509 char shareopts
[ZFS_MAXPROPLEN
];
5510 char smbshareopts
[ZFS_MAXPROPLEN
];
5511 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
5513 uint64_t zoned
, canmount
;
5514 boolean_t shared_nfs
, shared_smb
;
5516 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
5519 * Check to make sure we can mount/share this dataset. If we
5520 * are in the global zone and the filesystem is exported to a
5521 * local zone, or if we are in a local zone and the
5522 * filesystem is not exported, then it is an error.
5524 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
5526 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
5530 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5531 "dataset is exported to a local zone\n"), cmdname
,
5535 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
5539 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5540 "permission denied\n"), cmdname
,
5546 * Ignore any filesystems which don't apply to us. This
5547 * includes those with a legacy mountpoint, or those with
5548 * legacy share options.
5550 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
5551 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
5552 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
5553 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5554 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
5555 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5557 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
5558 strcmp(smbshareopts
, "off") == 0) {
5562 (void) fprintf(stderr
, gettext("cannot share '%s': "
5563 "legacy share\n"), zfs_get_name(zhp
));
5564 (void) fprintf(stderr
, gettext("use share(1M) to "
5565 "share this filesystem, or set "
5566 "sharenfs property on\n"));
5571 * We cannot share or mount legacy filesystems. If the
5572 * shareopts is non-legacy but the mountpoint is legacy, we
5573 * treat it as a legacy share.
5575 if (strcmp(mountpoint
, "legacy") == 0) {
5579 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5580 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
5581 (void) fprintf(stderr
, gettext("use %s(1M) to "
5582 "%s this filesystem\n"), cmdname
, cmdname
);
5586 if (strcmp(mountpoint
, "none") == 0) {
5590 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
5591 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
5596 * canmount explicit outcome
5597 * on no pass through
5598 * on yes pass through
5600 * off yes display error, return 1
5601 * noauto no return 0
5602 * noauto yes pass through
5604 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
5605 if (canmount
== ZFS_CANMOUNT_OFF
) {
5609 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5610 "'canmount' property is set to 'off'\n"), cmdname
,
5613 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
5618 * At this point, we have verified that the mountpoint and/or
5619 * shareopts are appropriate for auto management. If the
5620 * filesystem is already mounted or shared, return (failing
5621 * for explicit requests); otherwise mount or share the
5627 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
5628 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
5630 if ((shared_nfs
&& shared_smb
) ||
5631 ((shared_nfs
&& strcmp(shareopts
, "on") == 0) &&
5632 (strcmp(smbshareopts
, "off") == 0)) ||
5633 ((shared_smb
&& strcmp(smbshareopts
, "on") == 0) &&
5634 (strcmp(shareopts
, "off") == 0))) {
5638 (void) fprintf(stderr
, gettext("cannot share "
5639 "'%s': filesystem already shared\n"),
5644 if (!zfs_is_mounted(zhp
, NULL
) &&
5645 zfs_mount(zhp
, NULL
, 0) != 0)
5648 if (protocol
== NULL
) {
5649 if (zfs_shareall(zhp
) != 0)
5651 } else if (strcmp(protocol
, "nfs") == 0) {
5652 if (zfs_share_nfs(zhp
))
5654 } else if (strcmp(protocol
, "smb") == 0) {
5655 if (zfs_share_smb(zhp
))
5658 (void) fprintf(stderr
, gettext("cannot share "
5659 "'%s': invalid share type '%s' "
5661 zfs_get_name(zhp
), protocol
);
5668 if (options
== NULL
)
5669 mnt
.mnt_mntopts
= "";
5671 mnt
.mnt_mntopts
= (char *)options
;
5673 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
5674 zfs_is_mounted(zhp
, NULL
)) {
5678 (void) fprintf(stderr
, gettext("cannot mount "
5679 "'%s': filesystem already mounted\n"),
5684 if (zfs_mount(zhp
, options
, flags
) != 0)
5693 * Reports progress in the form "(current/total)". Not thread-safe.
5696 report_mount_progress(int current
, int total
)
5698 static time_t last_progress_time
= 0;
5699 time_t now
= time(NULL
);
5702 /* report 1..n instead of 0..n-1 */
5705 /* display header if we're here for the first time */
5707 set_progress_header(gettext("Mounting ZFS filesystems"));
5708 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
5709 /* too soon to report again */
5713 last_progress_time
= now
;
5715 (void) sprintf(info
, "(%d/%d)", current
, total
);
5717 if (current
== total
)
5718 finish_progress(info
);
5720 update_progress(info
);
5724 append_options(char *mntopts
, char *newopts
)
5726 int len
= strlen(mntopts
);
5728 /* original length plus new string to append plus 1 for the comma */
5729 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
5730 (void) fprintf(stderr
, gettext("the opts argument for "
5731 "'%s' option is too long (more than %d chars)\n"),
5732 "-o", MNT_LINE_MAX
);
5737 mntopts
[len
++] = ',';
5739 (void) strcpy(&mntopts
[len
], newopts
);
5743 share_mount(int op
, int argc
, char **argv
)
5746 boolean_t verbose
= B_FALSE
;
5748 char *options
= NULL
;
5752 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
5762 if (*optarg
== '\0') {
5763 (void) fprintf(stderr
, gettext("empty mount "
5764 "options (-o) specified\n"));
5768 if (options
== NULL
)
5769 options
= safe_malloc(MNT_LINE_MAX
+ 1);
5771 /* option validation is done later */
5772 append_options(options
, optarg
);
5775 flags
|= MS_OVERLAY
;
5778 (void) fprintf(stderr
, gettext("missing argument for "
5779 "'%c' option\n"), optopt
);
5783 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5792 /* check number of arguments */
5794 zfs_handle_t
**dslist
= NULL
;
5795 size_t i
, count
= 0;
5796 char *protocol
= NULL
;
5798 if (op
== OP_SHARE
&& argc
> 0) {
5799 if (strcmp(argv
[0], "nfs") != 0 &&
5800 strcmp(argv
[0], "smb") != 0) {
5801 (void) fprintf(stderr
, gettext("share type "
5802 "must be 'nfs' or 'smb'\n"));
5811 (void) fprintf(stderr
, gettext("too many arguments\n"));
5815 start_progress_timer();
5816 get_all_datasets(&dslist
, &count
, verbose
);
5821 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
5823 for (i
= 0; i
< count
; i
++) {
5825 report_mount_progress(i
, count
);
5827 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
5828 B_FALSE
, options
) != 0)
5830 zfs_close(dslist
[i
]);
5834 } else if (argc
== 0) {
5835 struct mnttab entry
;
5837 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
5838 (void) fprintf(stderr
, gettext("missing filesystem "
5839 "argument (specify -a for all)\n"));
5844 * When mount is given no arguments, go through /etc/mtab and
5845 * display any active ZFS mounts. We hide any snapshots, since
5846 * they are controlled automatically.
5848 rewind(mnttab_file
);
5849 while (getmntent(mnttab_file
, &entry
) == 0) {
5850 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
5851 strchr(entry
.mnt_special
, '@') != NULL
)
5854 (void) printf("%-30s %s\n", entry
.mnt_special
,
5862 (void) fprintf(stderr
,
5863 gettext("too many arguments\n"));
5867 if ((zhp
= zfs_open(g_zfs
, argv
[0],
5868 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
5871 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
5881 * zfs mount -a [nfs]
5882 * zfs mount filesystem
5884 * Mount all filesystems, or mount the given filesystem.
5887 zfs_do_mount(int argc
, char **argv
)
5889 return (share_mount(OP_MOUNT
, argc
, argv
));
5893 * zfs share -a [nfs | smb]
5894 * zfs share filesystem
5896 * Share all filesystems, or share the given filesystem.
5899 zfs_do_share(int argc
, char **argv
)
5901 return (share_mount(OP_SHARE
, argc
, argv
));
5904 typedef struct unshare_unmount_node
{
5905 zfs_handle_t
*un_zhp
;
5907 uu_avl_node_t un_avlnode
;
5908 } unshare_unmount_node_t
;
5912 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
5914 const unshare_unmount_node_t
*l
= larg
;
5915 const unshare_unmount_node_t
*r
= rarg
;
5917 return (strcmp(l
->un_mountp
, r
->un_mountp
));
5921 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
5922 * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
5923 * and unmount it appropriately.
5926 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
5930 struct stat64 statbuf
;
5931 struct extmnttab entry
;
5932 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
5936 * Search for the path in /etc/mtab. Rather than looking for the
5937 * specific path, which can be fooled by non-standard paths (i.e. ".."
5938 * or "//"), we stat() the path and search for the corresponding
5939 * (major,minor) device pair.
5941 if (stat64(path
, &statbuf
) != 0) {
5942 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
5943 cmdname
, path
, strerror(errno
));
5946 path_inode
= statbuf
.st_ino
;
5949 * Search for the given (major,minor) pair in the mount table.
5951 rewind(mnttab_file
);
5952 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
5953 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
5954 entry
.mnt_minor
== minor(statbuf
.st_dev
))
5958 if (op
== OP_SHARE
) {
5959 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
5960 "currently mounted\n"), cmdname
, path
);
5963 (void) fprintf(stderr
, gettext("warning: %s not in mtab\n"),
5965 if ((ret
= umount2(path
, flags
)) != 0)
5966 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
5971 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
5972 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
5973 "filesystem\n"), cmdname
, path
);
5977 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
5978 ZFS_TYPE_FILESYSTEM
)) == NULL
)
5982 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
5983 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
5984 cmdname
, path
, strerror(errno
));
5986 } else if (statbuf
.st_ino
!= path_inode
) {
5987 (void) fprintf(stderr
, gettext("cannot "
5988 "%s '%s': not a mountpoint\n"), cmdname
, path
);
5992 if (op
== OP_SHARE
) {
5993 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
5994 char smbshare_prop
[ZFS_MAXPROPLEN
];
5996 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
5997 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
5998 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
5999 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6001 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6002 strcmp(smbshare_prop
, "off") == 0) {
6003 (void) fprintf(stderr
, gettext("cannot unshare "
6004 "'%s': legacy share\n"), path
);
6005 (void) fprintf(stderr
, gettext("use exportfs(8) "
6006 "or smbcontrol(1) to unshare this filesystem\n"));
6007 } else if (!zfs_is_shared(zhp
)) {
6008 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6009 "not currently shared\n"), path
);
6011 ret
= zfs_unshareall_bypath(zhp
, path
);
6014 char mtpt_prop
[ZFS_MAXPROPLEN
];
6016 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6017 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6020 ret
= zfs_unmount(zhp
, NULL
, flags
);
6021 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6022 (void) fprintf(stderr
, gettext("cannot unmount "
6023 "'%s': legacy mountpoint\n"),
6025 (void) fprintf(stderr
, gettext("use umount(8) "
6026 "to unmount this filesystem\n"));
6028 ret
= zfs_unmountall(zhp
, flags
);
6039 * Generic callback for unsharing or unmounting a filesystem.
6042 unshare_unmount(int op
, int argc
, char **argv
)
6049 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6050 char sharesmb
[ZFS_MAXPROPLEN
];
6053 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
6062 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6073 * We could make use of zfs_for_each() to walk all datasets in
6074 * the system, but this would be very inefficient, especially
6075 * since we would have to linearly search /etc/mtab for each
6076 * one. Instead, do one pass through /etc/mtab looking for
6077 * zfs entries and call zfs_unmount() for each one.
6079 * Things get a little tricky if the administrator has created
6080 * mountpoints beneath other ZFS filesystems. In this case, we
6081 * have to unmount the deepest filesystems first. To accomplish
6082 * this, we place all the mountpoints in an AVL tree sorted by
6083 * the special type (dataset name), and walk the result in
6084 * reverse to make sure to get any snapshots first.
6086 struct mnttab entry
;
6087 uu_avl_pool_t
*pool
;
6088 uu_avl_t
*tree
= NULL
;
6089 unshare_unmount_node_t
*node
;
6091 uu_avl_walk_t
*walk
;
6094 (void) fprintf(stderr
, gettext("too many arguments\n"));
6098 if (((pool
= uu_avl_pool_create("unmount_pool",
6099 sizeof (unshare_unmount_node_t
),
6100 offsetof(unshare_unmount_node_t
, un_avlnode
),
6101 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6102 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6105 rewind(mnttab_file
);
6106 while (getmntent(mnttab_file
, &entry
) == 0) {
6108 /* ignore non-ZFS entries */
6109 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6112 /* ignore snapshots */
6113 if (strchr(entry
.mnt_special
, '@') != NULL
)
6116 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6117 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6124 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6126 sizeof (nfs_mnt_prop
),
6127 NULL
, NULL
, 0, B_FALSE
) == 0);
6128 if (strcmp(nfs_mnt_prop
, "off") != 0)
6130 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6132 sizeof (nfs_mnt_prop
),
6133 NULL
, NULL
, 0, B_FALSE
) == 0);
6134 if (strcmp(nfs_mnt_prop
, "off") == 0)
6138 /* Ignore legacy mounts */
6139 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6141 sizeof (nfs_mnt_prop
),
6142 NULL
, NULL
, 0, B_FALSE
) == 0);
6143 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6145 /* Ignore canmount=noauto mounts */
6146 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6147 ZFS_CANMOUNT_NOAUTO
)
6153 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6155 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6157 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6159 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6160 uu_avl_insert(tree
, node
, idx
);
6162 zfs_close(node
->un_zhp
);
6163 free(node
->un_mountp
);
6169 * Walk the AVL tree in reverse, unmounting each filesystem and
6170 * removing it from the AVL tree in the process.
6172 if ((walk
= uu_avl_walk_start(tree
,
6173 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6176 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6177 uu_avl_remove(tree
, node
);
6181 if (zfs_unshareall_bypath(node
->un_zhp
,
6182 node
->un_mountp
) != 0)
6187 if (zfs_unmount(node
->un_zhp
,
6188 node
->un_mountp
, flags
) != 0)
6193 zfs_close(node
->un_zhp
);
6194 free(node
->un_mountp
);
6198 uu_avl_walk_end(walk
);
6199 uu_avl_destroy(tree
);
6200 uu_avl_pool_destroy(pool
);
6205 (void) fprintf(stderr
,
6206 gettext("missing filesystem argument\n"));
6208 (void) fprintf(stderr
,
6209 gettext("too many arguments\n"));
6214 * We have an argument, but it may be a full path or a ZFS
6215 * filesystem. Pass full paths off to unmount_path() (shared by
6216 * manual_unmount), otherwise open the filesystem and pass to
6219 if (argv
[0][0] == '/')
6220 return (unshare_unmount_path(op
, argv
[0],
6223 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6224 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6227 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
6228 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
6229 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
6230 NULL
, 0, B_FALSE
) == 0);
6234 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6236 sizeof (nfs_mnt_prop
),
6237 NULL
, NULL
, 0, B_FALSE
) == 0);
6238 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6239 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
6242 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6243 strcmp(sharesmb
, "off") == 0) {
6244 (void) fprintf(stderr
, gettext("cannot "
6245 "unshare '%s': legacy share\n"),
6247 (void) fprintf(stderr
, gettext("use "
6248 "unshare(1M) to unshare this "
6251 } else if (!zfs_is_shared(zhp
)) {
6252 (void) fprintf(stderr
, gettext("cannot "
6253 "unshare '%s': not currently "
6254 "shared\n"), zfs_get_name(zhp
));
6256 } else if (zfs_unshareall(zhp
) != 0) {
6262 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
6263 (void) fprintf(stderr
, gettext("cannot "
6264 "unmount '%s': legacy "
6265 "mountpoint\n"), zfs_get_name(zhp
));
6266 (void) fprintf(stderr
, gettext("use "
6267 "umount(1M) to unmount this "
6270 } else if (!zfs_is_mounted(zhp
, NULL
)) {
6271 (void) fprintf(stderr
, gettext("cannot "
6272 "unmount '%s': not currently "
6276 } else if (zfs_unmountall(zhp
, flags
) != 0) {
6290 * zfs unmount filesystem
6292 * Unmount all filesystems, or a specific ZFS filesystem.
6295 zfs_do_unmount(int argc
, char **argv
)
6297 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
6302 * zfs unshare filesystem
6304 * Unshare all filesystems, or a specific ZFS filesystem.
6307 zfs_do_unshare(int argc
, char **argv
)
6309 return (unshare_unmount(OP_SHARE
, argc
, argv
));
6313 find_command_idx(char *command
, int *idx
)
6317 for (i
= 0; i
< NCOMMAND
; i
++) {
6318 if (command_table
[i
].name
== NULL
)
6321 if (strcmp(command
, command_table
[i
].name
) == 0) {
6330 zfs_do_diff(int argc
, char **argv
)
6334 char *tosnap
= NULL
;
6335 char *fromsnap
= NULL
;
6340 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
6343 flags
|= ZFS_DIFF_CLASSIFY
;
6346 flags
|= ZFS_DIFF_PARSEABLE
;
6349 flags
|= ZFS_DIFF_TIMESTAMP
;
6352 (void) fprintf(stderr
,
6353 gettext("invalid option '%c'\n"), optopt
);
6362 (void) fprintf(stderr
,
6363 gettext("must provide at least one snapshot name\n"));
6368 (void) fprintf(stderr
, gettext("too many arguments\n"));
6373 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
6376 if (*fromsnap
!= '@')
6377 copy
= strdup(fromsnap
);
6379 copy
= strdup(tosnap
);
6383 if ((atp
= strchr(copy
, '@')))
6386 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6392 * Ignore SIGPIPE so that the library can give us
6393 * information on any failure
6395 (void) sigignore(SIGPIPE
);
6397 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
6405 main(int argc
, char **argv
)
6411 (void) setlocale(LC_ALL
, "");
6412 (void) textdomain(TEXT_DOMAIN
);
6417 * Make sure the user has specified some command.
6420 (void) fprintf(stderr
, gettext("missing command\n"));
6427 * The 'umount' command is an alias for 'unmount'
6429 if (strcmp(cmdname
, "umount") == 0)
6430 cmdname
= "unmount";
6433 * The 'recv' command is an alias for 'receive'
6435 if (strcmp(cmdname
, "recv") == 0)
6436 cmdname
= "receive";
6439 * The 'snap' command is an alias for 'snapshot'
6441 if (strcmp(cmdname
, "snap") == 0)
6442 cmdname
= "snapshot";
6447 if ((strcmp(cmdname
, "-?") == 0) ||
6448 (strcmp(cmdname
, "--help") == 0))
6451 if ((g_zfs
= libzfs_init()) == NULL
)
6454 mnttab_file
= g_zfs
->libzfs_mnttab
;
6456 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
6458 libzfs_print_on_error(g_zfs
, B_TRUE
);
6461 * Run the appropriate command.
6463 libzfs_mnttab_cache(g_zfs
, B_FALSE
);
6464 if (find_command_idx(cmdname
, &i
) == 0) {
6465 current_command
= &command_table
[i
];
6466 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
6467 } else if (strchr(cmdname
, '=') != NULL
) {
6468 verify(find_command_idx("set", &i
) == 0);
6469 current_command
= &command_table
[i
];
6470 ret
= command_table
[i
].func(argc
, argv
);
6472 (void) fprintf(stderr
, gettext("unrecognized "
6473 "command '%s'\n"), cmdname
);
6479 if (ret
== 0 && log_history
)
6480 (void) zpool_log_history(g_zfs
, history_str
);
6483 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6484 * for the purposes of running ::findleaks.
6486 if (getenv("ZFS_ABORT") != NULL
) {
6487 (void) printf("dumping core by request\n");