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.
27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
36 #include <libnvpair.h>
49 #include <sys/mkdev.h>
50 #include <sys/mntent.h>
51 #include <sys/mnttab.h>
52 #include <sys/mount.h>
54 #include <sys/fs/zfs.h>
55 #include <sys/types.h>
59 #include <libzfs_core.h>
61 #include <zfs_deleg.h>
65 #include <directory.h>
66 #endif /* HAVE_IDMAP */
70 #include "zfs_comutil.h"
71 #include "libzfs_impl.h"
73 libzfs_handle_t
*g_zfs
;
75 static FILE *mnttab_file
;
76 static char history_str
[HIS_MAX_RECORD_LEN
];
77 static boolean_t log_history
= B_TRUE
;
79 static int zfs_do_clone(int argc
, char **argv
);
80 static int zfs_do_create(int argc
, char **argv
);
81 static int zfs_do_destroy(int argc
, char **argv
);
82 static int zfs_do_get(int argc
, char **argv
);
83 static int zfs_do_inherit(int argc
, char **argv
);
84 static int zfs_do_list(int argc
, char **argv
);
85 static int zfs_do_mount(int argc
, char **argv
);
86 static int zfs_do_rename(int argc
, char **argv
);
87 static int zfs_do_rollback(int argc
, char **argv
);
88 static int zfs_do_set(int argc
, char **argv
);
89 static int zfs_do_upgrade(int argc
, char **argv
);
90 static int zfs_do_snapshot(int argc
, char **argv
);
91 static int zfs_do_unmount(int argc
, char **argv
);
92 static int zfs_do_share(int argc
, char **argv
);
93 static int zfs_do_unshare(int argc
, char **argv
);
94 static int zfs_do_send(int argc
, char **argv
);
95 static int zfs_do_receive(int argc
, char **argv
);
96 static int zfs_do_promote(int argc
, char **argv
);
97 static int zfs_do_userspace(int argc
, char **argv
);
98 static int zfs_do_allow(int argc
, char **argv
);
99 static int zfs_do_unallow(int argc
, char **argv
);
100 static int zfs_do_hold(int argc
, char **argv
);
101 static int zfs_do_holds(int argc
, char **argv
);
102 static int zfs_do_release(int argc
, char **argv
);
103 static int zfs_do_diff(int argc
, char **argv
);
106 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
111 _umem_debug_init(void)
113 return ("default,verbose"); /* $UMEM_DEBUG setting */
117 _umem_logging_init(void)
119 return ("fail,contents"); /* $UMEM_LOGGING setting */
152 typedef struct zfs_command
{
154 int (*func
)(int argc
, char **argv
);
159 * Master command table. Each ZFS command has a name, associated function, and
160 * usage message. The usage messages need to be internationalized, so we have
161 * to have a function to return the usage message based on a command index.
163 * These commands are organized according to how they are displayed in the usage
164 * message. An empty command (one with a NULL name) indicates an empty line in
165 * the generic usage message.
167 static zfs_command_t command_table
[] = {
168 { "create", zfs_do_create
, HELP_CREATE
},
169 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
171 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
172 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
173 { "clone", zfs_do_clone
, HELP_CLONE
},
174 { "promote", zfs_do_promote
, HELP_PROMOTE
},
175 { "rename", zfs_do_rename
, HELP_RENAME
},
177 { "list", zfs_do_list
, HELP_LIST
},
179 { "set", zfs_do_set
, HELP_SET
},
180 { "get", zfs_do_get
, HELP_GET
},
181 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
182 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
183 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
184 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
186 { "mount", zfs_do_mount
, HELP_MOUNT
},
187 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
188 { "share", zfs_do_share
, HELP_SHARE
},
189 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
191 { "send", zfs_do_send
, HELP_SEND
},
192 { "receive", zfs_do_receive
, HELP_RECEIVE
},
194 { "allow", zfs_do_allow
, HELP_ALLOW
},
196 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
198 { "hold", zfs_do_hold
, HELP_HOLD
},
199 { "holds", zfs_do_holds
, HELP_HOLDS
},
200 { "release", zfs_do_release
, HELP_RELEASE
},
201 { "diff", zfs_do_diff
, HELP_DIFF
},
204 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
206 zfs_command_t
*current_command
;
209 get_usage(zfs_help_t idx
)
213 return (gettext("\tclone [-p] [-o property=value] ... "
214 "<snapshot> <filesystem|volume>\n"));
216 return (gettext("\tcreate [-p] [-o property=value] ... "
218 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
219 "-V <size> <volume>\n"));
221 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
222 "\tdestroy [-dnpRrv] "
223 "<filesystem|volume>@<snap>[%<snap>][,...]\n"));
225 return (gettext("\tget [-rHp] [-d max] "
226 "[-o \"all\" | field[,...]] [-t type[,...]] "
227 "[-s source[,...]]\n"
228 "\t <\"all\" | property[,...]> "
229 "[filesystem|volume|snapshot] ...\n"));
231 return (gettext("\tinherit [-rS] <property> "
232 "<filesystem|volume|snapshot> ...\n"));
234 return (gettext("\tupgrade [-v]\n"
235 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
237 return (gettext("\tlist [-rH][-d max] "
238 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
239 "\t [-S property] ... "
240 "[filesystem|volume|snapshot] ...\n"));
242 return (gettext("\tmount\n"
243 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
245 return (gettext("\tpromote <clone-filesystem>\n"));
247 return (gettext("\treceive [-vnFu] <filesystem|volume|"
249 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
251 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
252 "<filesystem|volume|snapshot>\n"
253 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
254 "\trename -r <snapshot> <snapshot>"));
256 return (gettext("\trollback [-rRf] <snapshot>\n"));
258 return (gettext("\tsend [-DnPpRrv] [-[iI] snapshot] "
261 return (gettext("\tset <property=value> "
262 "<filesystem|volume|snapshot> ...\n"));
264 return (gettext("\tshare <-a | filesystem>\n"));
266 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
267 "<filesystem@snapname|volume@snapname> ...\n"));
269 return (gettext("\tunmount [-f] "
270 "<-a | filesystem|mountpoint>\n"));
272 return (gettext("\tunshare "
273 "<-a | filesystem|mountpoint>\n"));
275 return (gettext("\tallow <filesystem|volume>\n"
277 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
278 "\t <filesystem|volume>\n"
279 "\tallow [-ld] -e <perm|@setname>[,...] "
280 "<filesystem|volume>\n"
281 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
282 "\tallow -s @setname <perm|@setname>[,...] "
283 "<filesystem|volume>\n"));
285 return (gettext("\tunallow [-rldug] "
286 "<\"everyone\"|user|group>[,...]\n"
287 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
288 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
289 "<filesystem|volume>\n"
290 "\tunallow [-r] -c [<perm|@setname>[,...]] "
291 "<filesystem|volume>\n"
292 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
293 "<filesystem|volume>\n"));
295 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
296 "[-s field] ...\n\t[-S field] ... "
297 "[-t type[,...]] <filesystem|snapshot>\n"));
298 case HELP_GROUPSPACE
:
299 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
300 "[-s field] ...\n\t[-S field] ... "
301 "[-t type[,...]] <filesystem|snapshot>\n"));
303 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
305 return (gettext("\tholds [-r] <snapshot> ...\n"));
307 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
309 return (gettext("\tdiff [-FHt] <snapshot> "
310 "[snapshot|filesystem]\n"));
320 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
325 * Utility function to guarantee malloc() success.
329 safe_malloc(size_t size
)
333 if ((data
= calloc(1, size
)) == NULL
)
340 safe_strdup(char *str
)
342 char *dupstr
= strdup(str
);
351 * Callback routine that will print out information for each of
355 usage_prop_cb(int prop
, void *cb
)
359 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
361 if (zfs_prop_readonly(prop
))
362 (void) fprintf(fp
, " NO ");
364 (void) fprintf(fp
, "YES ");
366 if (zfs_prop_inheritable(prop
))
367 (void) fprintf(fp
, " YES ");
369 (void) fprintf(fp
, " NO ");
371 if (zfs_prop_values(prop
) == NULL
)
372 (void) fprintf(fp
, "-\n");
374 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
380 * Display usage message. If we're inside a command, display only the usage for
381 * that command. Otherwise, iterate over the entire command table and display
382 * a complete usage message.
385 usage(boolean_t requested
)
388 boolean_t show_properties
= B_FALSE
;
389 FILE *fp
= requested
? stdout
: stderr
;
391 if (current_command
== NULL
) {
393 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
395 gettext("where 'command' is one of the following:\n\n"));
397 for (i
= 0; i
< NCOMMAND
; i
++) {
398 if (command_table
[i
].name
== NULL
)
399 (void) fprintf(fp
, "\n");
401 (void) fprintf(fp
, "%s",
402 get_usage(command_table
[i
].usage
));
405 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
406 "pool/[dataset/]*dataset[@name]\n"));
408 (void) fprintf(fp
, gettext("usage:\n"));
409 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
412 if (current_command
!= NULL
&&
413 (strcmp(current_command
->name
, "set") == 0 ||
414 strcmp(current_command
->name
, "get") == 0 ||
415 strcmp(current_command
->name
, "inherit") == 0 ||
416 strcmp(current_command
->name
, "list") == 0))
417 show_properties
= B_TRUE
;
419 if (show_properties
) {
421 gettext("\nThe following properties are supported:\n"));
423 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
424 "PROPERTY", "EDIT", "INHERIT", "VALUES");
426 /* Iterate over all properties */
427 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
430 (void) fprintf(fp
, "\t%-15s ", "userused@...");
431 (void) fprintf(fp
, " NO NO <size>\n");
432 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
433 (void) fprintf(fp
, " NO NO <size>\n");
434 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
435 (void) fprintf(fp
, "YES NO <size> | none\n");
436 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
437 (void) fprintf(fp
, "YES NO <size> | none\n");
438 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
439 (void) fprintf(fp
, " NO NO <size>\n");
441 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
442 "with standard units such as K, M, G, etc.\n"));
443 (void) fprintf(fp
, gettext("\nUser-defined properties can "
444 "be specified by using a name containing a colon (:).\n"));
445 (void) fprintf(fp
, gettext("\nThe {user|group}{used|quota}@ "
446 "properties must be appended with\n"
447 "a user or group specifier of one of these forms:\n"
448 " POSIX name (eg: \"matt\")\n"
449 " POSIX id (eg: \"126829\")\n"
450 " SMB name@domain (eg: \"matt@sun\")\n"
451 " SMB SID (eg: \"S-1-234-567-89\")\n"));
454 gettext("\nFor the property list, run: %s\n"),
457 gettext("\nFor the delegated permission list, run: %s\n"),
458 "zfs allow|unallow");
462 * See comments at end of main().
464 if (getenv("ZFS_ABORT") != NULL
) {
465 (void) printf("dumping core by request\n");
469 exit(requested
? 0 : 2);
473 parseprop(nvlist_t
*props
)
475 char *propname
= optarg
;
476 char *propval
, *strval
;
478 if ((propval
= strchr(propname
, '=')) == NULL
) {
479 (void) fprintf(stderr
, gettext("missing "
480 "'=' for -o option\n"));
485 if (nvlist_lookup_string(props
, propname
, &strval
) == 0) {
486 (void) fprintf(stderr
, gettext("property '%s' "
487 "specified multiple times\n"), propname
);
490 if (nvlist_add_string(props
, propname
, propval
) != 0)
496 parse_depth(char *opt
, int *flags
)
501 depth
= (int)strtol(opt
, &tmp
, 0);
503 (void) fprintf(stderr
,
504 gettext("%s is not an integer\n"), optarg
);
508 (void) fprintf(stderr
,
509 gettext("Depth can not be negative.\n"));
512 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
516 #define PROGRESS_DELAY 2 /* seconds */
518 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";
519 static time_t pt_begin
;
520 static char *pt_header
= NULL
;
521 static boolean_t pt_shown
;
524 start_progress_timer(void)
526 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
531 set_progress_header(char *header
)
533 assert(pt_header
== NULL
);
534 pt_header
= safe_strdup(header
);
536 (void) printf("%s: ", header
);
537 (void) fflush(stdout
);
542 update_progress(char *update
)
544 if (!pt_shown
&& time(NULL
) > pt_begin
) {
545 int len
= strlen(update
);
547 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
549 (void) fflush(stdout
);
551 } else if (pt_shown
) {
552 int len
= strlen(update
);
554 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
555 (void) fflush(stdout
);
560 finish_progress(char *done
)
563 (void) printf("%s\n", done
);
564 (void) fflush(stdout
);
571 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
573 * Given an existing dataset, create a writable copy whose initial contents
574 * are the same as the source. The newly created dataset maintains a
575 * dependency on the original; the original cannot be destroyed so long as
578 * The '-p' flag creates all the non-existing ancestors of the target first.
581 zfs_do_clone(int argc
, char **argv
)
583 zfs_handle_t
*zhp
= NULL
;
584 boolean_t parents
= B_FALSE
;
589 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
593 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
596 if (parseprop(props
))
603 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
612 /* check number of arguments */
614 (void) fprintf(stderr
, gettext("missing source dataset "
619 (void) fprintf(stderr
, gettext("missing target dataset "
624 (void) fprintf(stderr
, gettext("too many arguments\n"));
628 /* open the source dataset */
629 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
632 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
635 * Now create the ancestors of the target dataset. If the
636 * target already exists and '-p' option was used we should not
639 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
642 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
647 ret
= zfs_clone(zhp
, argv
[1], props
);
649 /* create the mountpoint if necessary */
653 clone
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
655 if (zfs_get_type(clone
) != ZFS_TYPE_VOLUME
)
656 if ((ret
= zfs_mount(clone
, NULL
, 0)) == 0)
657 ret
= zfs_share(clone
);
676 * zfs create [-p] [-o prop=value] ... fs
677 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
679 * Create a new dataset. This command can be used to create filesystems
680 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
681 * For volumes, the user must specify a size to be used.
683 * The '-s' flag applies only to volumes, and indicates that we should not try
684 * to set the reservation for this volume. By default we set a reservation
685 * equal to the size for any volume. For pools with SPA_VERSION >=
686 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
688 * The '-p' flag creates all the non-existing ancestors of the target first.
691 zfs_do_create(int argc
, char **argv
)
693 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
694 zfs_handle_t
*zhp
= NULL
;
695 uint64_t volsize
= 0;
697 boolean_t noreserve
= B_FALSE
;
698 boolean_t bflag
= B_FALSE
;
699 boolean_t parents
= B_FALSE
;
703 int canmount
= ZFS_CANMOUNT_OFF
;
705 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
709 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
712 type
= ZFS_TYPE_VOLUME
;
713 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
714 (void) fprintf(stderr
, gettext("bad volume "
715 "size '%s': %s\n"), optarg
,
716 libzfs_error_description(g_zfs
));
720 if (nvlist_add_uint64(props
,
721 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
730 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
731 (void) fprintf(stderr
, gettext("bad volume "
732 "block size '%s': %s\n"), optarg
,
733 libzfs_error_description(g_zfs
));
737 if (nvlist_add_uint64(props
,
738 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
743 if (parseprop(props
))
750 (void) fprintf(stderr
, gettext("missing size "
755 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
761 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
762 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
763 "used when creating a volume\n"));
770 /* check number of arguments */
772 (void) fprintf(stderr
, gettext("missing %s argument\n"),
773 zfs_type_to_name(type
));
777 (void) fprintf(stderr
, gettext("too many arguments\n"));
781 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
782 zpool_handle_t
*zpool_handle
;
783 uint64_t spa_version
;
785 zfs_prop_t resv_prop
;
788 if ((p
= strchr(argv
[0], '/')))
790 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
793 if (zpool_handle
== NULL
)
795 spa_version
= zpool_get_prop_int(zpool_handle
,
796 ZPOOL_PROP_VERSION
, NULL
);
797 zpool_close(zpool_handle
);
798 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
799 resv_prop
= ZFS_PROP_REFRESERVATION
;
801 resv_prop
= ZFS_PROP_RESERVATION
;
802 volsize
= zvol_volsize_to_reservation(volsize
, props
);
804 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
806 if (nvlist_add_uint64(props
,
807 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
814 if (parents
&& zfs_name_valid(argv
[0], type
)) {
816 * Now create the ancestors of target dataset. If the target
817 * already exists and '-p' option was used we should not
820 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
824 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
829 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
832 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
837 * if the user doesn't want the dataset automatically mounted,
838 * then skip the mount/share step
840 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
))
841 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
844 * Mount and/or share the new filesystem as appropriate. We provide a
845 * verbose error message to let the user know that their filesystem was
846 * in fact created, even if we failed to mount or share it.
848 if (canmount
== ZFS_CANMOUNT_ON
) {
849 if (zfs_mount(zhp
, NULL
, 0) != 0) {
850 (void) fprintf(stderr
, gettext("filesystem "
851 "successfully created, but not mounted\n"));
853 } else if (zfs_share(zhp
) != 0) {
854 (void) fprintf(stderr
, gettext("filesystem "
855 "successfully created, but not shared\n"));
872 * zfs destroy [-rRf] <fs, vol>
873 * zfs destroy [-rRd] <snap>
875 * -r Recursively destroy all children
876 * -R Recursively destroy all dependents, including clones
877 * -f Force unmounting of any dependents
878 * -d If we can't destroy now, mark for deferred destruction
880 * Destroys the given dataset. By default, it will unmount any filesystems,
881 * and refuse to destroy a dataset that has any dependents. A dependent can
882 * either be a child, or a clone of a child.
884 typedef struct destroy_cbdata
{
887 boolean_t cb_recurse
;
889 boolean_t cb_doclones
;
890 zfs_handle_t
*cb_target
;
891 boolean_t cb_defer_destroy
;
892 boolean_t cb_verbose
;
893 boolean_t cb_parsable
;
896 nvlist_t
*cb_batchedsnaps
;
898 /* first snap in contiguous run */
900 /* previous snap in contiguous run */
907 * Check for any dependents based on the '-r' or '-R' flags.
910 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
912 destroy_cbdata_t
*cbp
= data
;
913 const char *tname
= zfs_get_name(cbp
->cb_target
);
914 const char *name
= zfs_get_name(zhp
);
916 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
917 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
919 * This is a direct descendant, not a clone somewhere else in
926 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
927 "%s has children\n"),
928 zfs_get_name(cbp
->cb_target
),
929 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
930 (void) fprintf(stderr
, gettext("use '-r' to destroy "
931 "the following datasets:\n"));
932 cbp
->cb_first
= B_FALSE
;
933 cbp
->cb_error
= B_TRUE
;
936 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
939 * This is a clone. We only want to report this if the '-r'
940 * wasn't specified, or the target is a snapshot.
942 if (!cbp
->cb_recurse
&&
943 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
947 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
948 "%s has dependent clones\n"),
949 zfs_get_name(cbp
->cb_target
),
950 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
951 (void) fprintf(stderr
, gettext("use '-R' to destroy "
952 "the following datasets:\n"));
953 cbp
->cb_first
= B_FALSE
;
954 cbp
->cb_error
= B_TRUE
;
955 cbp
->cb_dryrun
= B_TRUE
;
958 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
967 destroy_callback(zfs_handle_t
*zhp
, void *data
)
969 destroy_cbdata_t
*cb
= data
;
970 const char *name
= zfs_get_name(zhp
);
972 if (cb
->cb_verbose
) {
973 if (cb
->cb_parsable
) {
974 (void) printf("destroy\t%s\n", name
);
975 } else if (cb
->cb_dryrun
) {
976 (void) printf(gettext("would destroy %s\n"),
979 (void) printf(gettext("will destroy %s\n"),
985 * Ignore pools (which we've already flagged as an error before getting
988 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
989 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
999 * We batch up all contiguous snapshots (even of different
1000 * filesystems) and destroy them with one ioctl. We can't
1001 * simply do all snap deletions and then all fs deletions,
1002 * because we must delete a clone before its origin.
1004 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1005 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1007 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1008 cb
->cb_batchedsnaps
, B_FALSE
);
1009 fnvlist_free(cb
->cb_batchedsnaps
);
1010 cb
->cb_batchedsnaps
= fnvlist_alloc();
1013 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1014 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1025 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1027 destroy_cbdata_t
*cb
= arg
;
1028 const char *name
= zfs_get_name(zhp
);
1031 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1032 if (cb
->cb_firstsnap
== NULL
)
1033 cb
->cb_firstsnap
= strdup(name
);
1034 if (cb
->cb_prevsnap
!= NULL
)
1035 free(cb
->cb_prevsnap
);
1036 /* this snap continues the current range */
1037 cb
->cb_prevsnap
= strdup(name
);
1038 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1040 if (cb
->cb_verbose
) {
1041 if (cb
->cb_parsable
) {
1042 (void) printf("destroy\t%s\n", name
);
1043 } else if (cb
->cb_dryrun
) {
1044 (void) printf(gettext("would destroy %s\n"),
1047 (void) printf(gettext("will destroy %s\n"),
1051 } else if (cb
->cb_firstsnap
!= NULL
) {
1052 /* end of this range */
1054 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1055 cb
->cb_prevsnap
, &used
);
1056 cb
->cb_snapused
+= used
;
1057 free(cb
->cb_firstsnap
);
1058 cb
->cb_firstsnap
= NULL
;
1059 free(cb
->cb_prevsnap
);
1060 cb
->cb_prevsnap
= NULL
;
1067 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1070 assert(cb
->cb_firstsnap
== NULL
);
1071 assert(cb
->cb_prevsnap
== NULL
);
1072 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1073 if (cb
->cb_firstsnap
!= NULL
) {
1076 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1077 cb
->cb_prevsnap
, &used
);
1079 cb
->cb_snapused
+= used
;
1080 free(cb
->cb_firstsnap
);
1081 cb
->cb_firstsnap
= NULL
;
1082 free(cb
->cb_prevsnap
);
1083 cb
->cb_prevsnap
= NULL
;
1089 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1091 destroy_cbdata_t
*cb
= arg
;
1094 /* Check for clones. */
1095 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1096 cb
->cb_target
= zhp
;
1097 cb
->cb_first
= B_TRUE
;
1098 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1099 destroy_check_dependent
, cb
);
1103 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1111 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1113 destroy_cbdata_t
*cb
= arg
;
1116 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1122 if (cb
->cb_verbose
) {
1123 err
= destroy_print_snapshots(zhp
, cb
);
1129 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1137 destroy_clones(destroy_cbdata_t
*cb
)
1140 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1142 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1143 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1146 boolean_t defer
= cb
->cb_defer_destroy
;
1150 * We can't defer destroy non-snapshots, so set it to
1151 * false while destroying the clones.
1153 cb
->cb_defer_destroy
= B_FALSE
;
1154 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1155 destroy_callback
, cb
);
1156 cb
->cb_defer_destroy
= defer
;
1166 zfs_do_destroy(int argc
, char **argv
)
1168 destroy_cbdata_t cb
= { 0 };
1172 zfs_handle_t
*zhp
= NULL
;
1174 zfs_type_t type
= ZFS_TYPE_DATASET
;
1177 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1180 cb
.cb_verbose
= B_TRUE
;
1183 cb
.cb_verbose
= B_TRUE
;
1184 cb
.cb_parsable
= B_TRUE
;
1187 cb
.cb_dryrun
= B_TRUE
;
1190 cb
.cb_defer_destroy
= B_TRUE
;
1191 type
= ZFS_TYPE_SNAPSHOT
;
1194 cb
.cb_force
= B_TRUE
;
1197 cb
.cb_recurse
= B_TRUE
;
1200 cb
.cb_recurse
= B_TRUE
;
1201 cb
.cb_doclones
= B_TRUE
;
1205 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1214 /* check number of arguments */
1216 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1220 (void) fprintf(stderr
, gettext("too many arguments\n"));
1224 at
= strchr(argv
[0], '@');
1227 /* Build the list of snaps to destroy in cb_nvl. */
1228 cb
.cb_nvl
= fnvlist_alloc();
1231 zhp
= zfs_open(g_zfs
, argv
[0],
1232 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1236 cb
.cb_snapspec
= at
+ 1;
1237 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1243 if (nvlist_empty(cb
.cb_nvl
)) {
1244 (void) fprintf(stderr
, gettext("could not find any "
1245 "snapshots to destroy; check snapshot names.\n"));
1250 if (cb
.cb_verbose
) {
1252 zfs_nicenum(cb
.cb_snapused
, buf
, sizeof (buf
));
1253 if (cb
.cb_parsable
) {
1254 (void) printf("reclaim\t%llu\n",
1255 (u_longlong_t
)cb
.cb_snapused
);
1256 } else if (cb
.cb_dryrun
) {
1257 (void) printf(gettext("would reclaim %s\n"),
1260 (void) printf(gettext("will reclaim %s\n"),
1265 if (!cb
.cb_dryrun
) {
1266 if (cb
.cb_doclones
) {
1267 cb
.cb_batchedsnaps
= fnvlist_alloc();
1268 err
= destroy_clones(&cb
);
1270 err
= zfs_destroy_snaps_nvl(g_zfs
,
1271 cb
.cb_batchedsnaps
, B_FALSE
);
1279 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1280 cb
.cb_defer_destroy
);
1287 /* Open the given dataset */
1288 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1294 * Perform an explicit check for pools before going any further.
1296 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1297 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1298 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1299 "operation does not apply to pools\n"),
1301 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1302 "%s' to destroy all datasets in the pool\n"),
1304 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1305 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1311 * Check for any dependents and/or clones.
1313 cb
.cb_first
= B_TRUE
;
1314 if (!cb
.cb_doclones
&&
1315 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1326 cb
.cb_batchedsnaps
= fnvlist_alloc();
1327 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1334 * Do the real thing. The callback will close the
1335 * handle regardless of whether it succeeds or not.
1337 err
= destroy_callback(zhp
, &cb
);
1340 err
= zfs_destroy_snaps_nvl(g_zfs
,
1341 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1348 fnvlist_free(cb
.cb_batchedsnaps
);
1349 fnvlist_free(cb
.cb_nvl
);
1356 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1359 zfs_get_column_t col
;
1361 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1362 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1363 if (col
== GET_COL_RECVD
)
1369 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1370 * < all | property[,property]... > < fs | snap | vol > ...
1372 * -r recurse over any child datasets
1373 * -H scripted mode. Headers are stripped, and fields are separated
1374 * by tabs instead of spaces.
1375 * -o Set of fields to display. One of "name,property,value,
1376 * received,source". Default is "name,property,value,source".
1377 * "all" is an alias for all five.
1378 * -s Set of sources to allow. One of
1379 * "local,default,inherited,received,temporary,none". Default is
1381 * -p Display values in parsable (literal) format.
1383 * Prints properties for the given datasets. The user can control which
1384 * columns to display as well as which property types to allow.
1388 * Invoked to display the properties for a single dataset.
1391 get_callback(zfs_handle_t
*zhp
, void *data
)
1393 char buf
[ZFS_MAXPROPLEN
];
1394 char rbuf
[ZFS_MAXPROPLEN
];
1395 zprop_source_t sourcetype
;
1396 char source
[ZFS_MAXNAMELEN
];
1397 zprop_get_cbdata_t
*cbp
= data
;
1398 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1399 zprop_list_t
*pl
= cbp
->cb_proplist
;
1403 boolean_t received
= is_recvd_column(cbp
);
1405 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1406 char *recvdval
= NULL
;
1408 * Skip the special fake placeholder. This will also skip over
1409 * the name property when 'all' is specified.
1411 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1412 pl
== cbp
->cb_proplist
)
1415 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1416 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1417 sizeof (buf
), &sourcetype
, source
,
1419 cbp
->cb_literal
) != 0) {
1422 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1423 ZFS_TYPE_DATASET
)) {
1424 (void) fprintf(stderr
,
1425 gettext("No such property '%s'\n"),
1426 zfs_prop_to_name(pl
->pl_prop
));
1429 sourcetype
= ZPROP_SRC_NONE
;
1430 (void) strlcpy(buf
, "-", sizeof (buf
));
1433 if (received
&& (zfs_prop_get_recvd(zhp
,
1434 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1435 cbp
->cb_literal
) == 0))
1438 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1439 zfs_prop_to_name(pl
->pl_prop
),
1440 buf
, sourcetype
, source
, recvdval
);
1441 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1442 sourcetype
= ZPROP_SRC_LOCAL
;
1444 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1445 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1446 sourcetype
= ZPROP_SRC_NONE
;
1447 (void) strlcpy(buf
, "-", sizeof (buf
));
1450 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1451 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1452 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1453 sourcetype
= ZPROP_SRC_LOCAL
;
1455 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1456 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1457 sourcetype
= ZPROP_SRC_NONE
;
1458 (void) strlcpy(buf
, "-", sizeof (buf
));
1461 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1462 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1464 if (nvlist_lookup_nvlist(user_props
,
1465 pl
->pl_user_prop
, &propval
) != 0) {
1468 sourcetype
= ZPROP_SRC_NONE
;
1471 verify(nvlist_lookup_string(propval
,
1472 ZPROP_VALUE
, &strval
) == 0);
1473 verify(nvlist_lookup_string(propval
,
1474 ZPROP_SOURCE
, &sourceval
) == 0);
1476 if (strcmp(sourceval
,
1477 zfs_get_name(zhp
)) == 0) {
1478 sourcetype
= ZPROP_SRC_LOCAL
;
1479 } else if (strcmp(sourceval
,
1480 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1481 sourcetype
= ZPROP_SRC_RECEIVED
;
1483 sourcetype
= ZPROP_SRC_INHERITED
;
1484 (void) strlcpy(source
,
1485 sourceval
, sizeof (source
));
1489 if (received
&& (zfs_prop_get_recvd(zhp
,
1490 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1491 cbp
->cb_literal
) == 0))
1494 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1495 pl
->pl_user_prop
, strval
, sourcetype
,
1504 zfs_do_get(int argc
, char **argv
)
1506 zprop_get_cbdata_t cb
= { 0 };
1507 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1508 int types
= ZFS_TYPE_DATASET
;
1509 char *value
, *fields
;
1512 zprop_list_t fake_name
= { 0 };
1515 * Set up default columns and sources.
1517 cb
.cb_sources
= ZPROP_SRC_ALL
;
1518 cb
.cb_columns
[0] = GET_COL_NAME
;
1519 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1520 cb
.cb_columns
[2] = GET_COL_VALUE
;
1521 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1522 cb
.cb_type
= ZFS_TYPE_DATASET
;
1525 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1528 cb
.cb_literal
= B_TRUE
;
1531 limit
= parse_depth(optarg
, &flags
);
1534 flags
|= ZFS_ITER_RECURSE
;
1537 cb
.cb_scripted
= B_TRUE
;
1540 (void) fprintf(stderr
, gettext("missing argument for "
1541 "'%c' option\n"), optopt
);
1546 * Process the set of columns to display. We zero out
1547 * the structure to give us a blank slate.
1549 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1551 while (*optarg
!= '\0') {
1552 static char *col_subopts
[] =
1553 { "name", "property", "value", "received",
1554 "source", "all", NULL
};
1556 if (i
== ZFS_GET_NCOLS
) {
1557 (void) fprintf(stderr
, gettext("too "
1558 "many fields given to -o "
1563 switch (getsubopt(&optarg
, col_subopts
,
1566 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1569 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1572 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1575 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1576 flags
|= ZFS_ITER_RECVD_PROPS
;
1579 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1583 (void) fprintf(stderr
,
1584 gettext("\"all\" conflicts "
1585 "with specific fields "
1586 "given to -o option\n"));
1589 cb
.cb_columns
[0] = GET_COL_NAME
;
1590 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1591 cb
.cb_columns
[2] = GET_COL_VALUE
;
1592 cb
.cb_columns
[3] = GET_COL_RECVD
;
1593 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1594 flags
|= ZFS_ITER_RECVD_PROPS
;
1598 (void) fprintf(stderr
,
1599 gettext("invalid column name "
1608 while (*optarg
!= '\0') {
1609 static char *source_subopts
[] = {
1610 "local", "default", "inherited",
1611 "received", "temporary", "none",
1614 switch (getsubopt(&optarg
, source_subopts
,
1617 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1620 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1623 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1626 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1629 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1632 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1635 (void) fprintf(stderr
,
1636 gettext("invalid source "
1645 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1646 while (*optarg
!= '\0') {
1647 static char *type_subopts
[] = { "filesystem",
1648 "volume", "snapshot", "all", NULL
};
1650 switch (getsubopt(&optarg
, type_subopts
,
1653 types
|= ZFS_TYPE_FILESYSTEM
;
1656 types
|= ZFS_TYPE_VOLUME
;
1659 types
|= ZFS_TYPE_SNAPSHOT
;
1662 types
= ZFS_TYPE_DATASET
;
1666 (void) fprintf(stderr
,
1667 gettext("invalid type '%s'\n"),
1675 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1685 (void) fprintf(stderr
, gettext("missing property "
1692 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1700 * As part of zfs_expand_proplist(), we keep track of the maximum column
1701 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1702 * need to know the maximum name length. However, the user likely did
1703 * not specify 'name' as one of the properties to fetch, so we need to
1704 * make sure we always include at least this property for
1705 * print_get_headers() to work properly.
1707 if (cb
.cb_proplist
!= NULL
) {
1708 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1709 fake_name
.pl_width
= strlen(gettext("NAME"));
1710 fake_name
.pl_next
= cb
.cb_proplist
;
1711 cb
.cb_proplist
= &fake_name
;
1714 cb
.cb_first
= B_TRUE
;
1716 /* run for each object */
1717 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1718 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1720 if (cb
.cb_proplist
== &fake_name
)
1721 zprop_free_list(fake_name
.pl_next
);
1723 zprop_free_list(cb
.cb_proplist
);
1729 * inherit [-rS] <property> <fs|vol> ...
1731 * -r Recurse over all children
1732 * -S Revert to received value, if any
1734 * For each dataset specified on the command line, inherit the given property
1735 * from its parent. Inheriting a property at the pool level will cause it to
1736 * use the default value. The '-r' flag will recurse over all children, and is
1737 * useful for setting a property on a hierarchy-wide basis, regardless of any
1738 * local modifications for each dataset.
1741 typedef struct inherit_cbdata
{
1742 const char *cb_propname
;
1743 boolean_t cb_received
;
1747 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1749 inherit_cbdata_t
*cb
= data
;
1750 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1753 * If we're doing it recursively, then ignore properties that
1754 * are not valid for this type of dataset.
1756 if (prop
!= ZPROP_INVAL
&&
1757 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
)))
1760 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1764 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1766 inherit_cbdata_t
*cb
= data
;
1768 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1772 zfs_do_inherit(int argc
, char **argv
)
1776 inherit_cbdata_t cb
= { 0 };
1780 boolean_t received
= B_FALSE
;
1783 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1786 flags
|= ZFS_ITER_RECURSE
;
1793 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1802 /* check number of arguments */
1804 (void) fprintf(stderr
, gettext("missing property argument\n"));
1808 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1816 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1817 if (zfs_prop_readonly(prop
)) {
1818 (void) fprintf(stderr
, gettext(
1819 "%s property is read-only\n"),
1823 if (!zfs_prop_inheritable(prop
) && !received
) {
1824 (void) fprintf(stderr
, gettext("'%s' property cannot "
1825 "be inherited\n"), propname
);
1826 if (prop
== ZFS_PROP_QUOTA
||
1827 prop
== ZFS_PROP_RESERVATION
||
1828 prop
== ZFS_PROP_REFQUOTA
||
1829 prop
== ZFS_PROP_REFRESERVATION
)
1830 (void) fprintf(stderr
, gettext("use 'zfs set "
1831 "%s=none' to clear\n"), propname
);
1834 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1835 prop
== ZFS_PROP_VERSION
)) {
1836 (void) fprintf(stderr
, gettext("'%s' property cannot "
1837 "be reverted to a received value\n"), propname
);
1840 } else if (!zfs_prop_user(propname
)) {
1841 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1846 cb
.cb_propname
= propname
;
1847 cb
.cb_received
= received
;
1849 if (flags
& ZFS_ITER_RECURSE
) {
1850 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1851 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1853 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1854 NULL
, NULL
, 0, inherit_cb
, &cb
);
1860 typedef struct upgrade_cbdata
{
1861 uint64_t cb_numupgraded
;
1862 uint64_t cb_numsamegraded
;
1863 uint64_t cb_numfailed
;
1864 uint64_t cb_version
;
1866 boolean_t cb_foundone
;
1867 char cb_lastfs
[ZFS_MAXNAMELEN
];
1871 same_pool(zfs_handle_t
*zhp
, const char *name
)
1873 int len1
= strcspn(name
, "/@");
1874 const char *zhname
= zfs_get_name(zhp
);
1875 int len2
= strcspn(zhname
, "/@");
1879 return (strncmp(name
, zhname
, len1
) == 0);
1883 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
1885 upgrade_cbdata_t
*cb
= data
;
1886 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1888 /* list if it's old/new */
1889 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
1890 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
1893 str
= gettext("The following filesystems are "
1894 "formatted using a newer software version and\n"
1895 "cannot be accessed on the current system.\n\n");
1897 str
= gettext("The following filesystems are "
1898 "out of date, and can be upgraded. After being\n"
1899 "upgraded, these filesystems (and any 'zfs send' "
1900 "streams generated from\n"
1901 "subsequent snapshots) will no longer be "
1902 "accessible by older software versions.\n\n");
1905 if (!cb
->cb_foundone
) {
1907 (void) printf(gettext("VER FILESYSTEM\n"));
1908 (void) printf(gettext("--- ------------\n"));
1909 cb
->cb_foundone
= B_TRUE
;
1912 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
1919 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
1921 upgrade_cbdata_t
*cb
= data
;
1922 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1923 int needed_spa_version
;
1926 if (zfs_spa_version(zhp
, &spa_version
) < 0)
1929 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
1931 if (needed_spa_version
< 0)
1934 if (spa_version
< needed_spa_version
) {
1936 (void) printf(gettext("%s: can not be "
1937 "upgraded; the pool version needs to first "
1938 "be upgraded\nto version %d\n\n"),
1939 zfs_get_name(zhp
), needed_spa_version
);
1945 if (version
< cb
->cb_version
) {
1947 (void) snprintf(verstr
, sizeof (verstr
),
1948 "%llu", (u_longlong_t
)cb
->cb_version
);
1949 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
1951 * If they did "zfs upgrade -a", then we could
1952 * be doing ioctls to different pools. We need
1953 * to log this history once to each pool, and bypass
1954 * the normal history logging that happens in main().
1956 (void) zpool_log_history(g_zfs
, history_str
);
1957 log_history
= B_FALSE
;
1959 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
1960 cb
->cb_numupgraded
++;
1963 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
1964 } else if (version
> cb
->cb_version
) {
1965 /* can't downgrade */
1966 (void) printf(gettext("%s: can not be downgraded; "
1967 "it is already at version %u\n"),
1968 zfs_get_name(zhp
), version
);
1971 cb
->cb_numsamegraded
++;
1979 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1982 zfs_do_upgrade(int argc
, char **argv
)
1984 boolean_t all
= B_FALSE
;
1985 boolean_t showversions
= B_FALSE
;
1987 upgrade_cbdata_t cb
= { 0 };
1989 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1992 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
1995 flags
|= ZFS_ITER_RECURSE
;
1998 showversions
= B_TRUE
;
2001 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2002 optarg
, &cb
.cb_version
) != 0) {
2003 (void) fprintf(stderr
,
2004 gettext("invalid version %s\n"), optarg
);
2013 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2022 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2024 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2025 cb
.cb_version
|| argc
))
2027 if ((all
|| argc
) && (showversions
))
2033 /* Show info on available versions. */
2034 (void) printf(gettext("The following filesystem versions are "
2036 (void) printf(gettext("VER DESCRIPTION\n"));
2037 (void) printf("--- -----------------------------------------"
2038 "---------------\n");
2039 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2040 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2041 (void) printf(gettext(" 3 Case insensitive and filesystem "
2042 "user identifier (FUID)\n"));
2043 (void) printf(gettext(" 4 userquota, groupquota "
2045 (void) printf(gettext(" 5 System attributes\n"));
2046 (void) printf(gettext("\nFor more information on a particular "
2047 "version, including supported releases,\n"));
2048 (void) printf("see the ZFS Administration Guide.\n\n");
2050 } else if (argc
|| all
) {
2051 /* Upgrade filesystems */
2052 if (cb
.cb_version
== 0)
2053 cb
.cb_version
= ZPL_VERSION
;
2054 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2055 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2056 (void) printf(gettext("%llu filesystems upgraded\n"),
2057 (u_longlong_t
)cb
.cb_numupgraded
);
2058 if (cb
.cb_numsamegraded
) {
2059 (void) printf(gettext("%llu filesystems already at "
2061 (u_longlong_t
)cb
.cb_numsamegraded
);
2063 if (cb
.cb_numfailed
!= 0)
2066 /* List old-version filesytems */
2068 (void) printf(gettext("This system is currently running "
2069 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2071 flags
|= ZFS_ITER_RECURSE
;
2072 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2073 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2075 found
= cb
.cb_foundone
;
2076 cb
.cb_foundone
= B_FALSE
;
2077 cb
.cb_newer
= B_TRUE
;
2079 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2080 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2082 if (!cb
.cb_foundone
&& !found
) {
2083 (void) printf(gettext("All filesystems are "
2084 "formatted with the current version.\n"));
2092 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2093 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2094 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2095 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2097 * -H Scripted mode; elide headers and separate columns by tabs.
2098 * -i Translate SID to POSIX ID.
2099 * -n Print numeric ID instead of user/group name.
2100 * -o Control which fields to display.
2101 * -p Use exact (parseable) numeric output.
2102 * -s Specify sort columns, descending order.
2103 * -S Specify sort columns, ascending order.
2104 * -t Control which object types to display.
2106 * Displays space consumed by, and quotas on, each user in the specified
2107 * filesystem or snapshot.
2110 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2111 enum us_field_types
{
2117 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA" };
2118 static char *us_field_names
[] = { "type", "name", "used", "quota" };
2119 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2121 #define USTYPE_PSX_GRP (1 << 0)
2122 #define USTYPE_PSX_USR (1 << 1)
2123 #define USTYPE_SMB_GRP (1 << 2)
2124 #define USTYPE_SMB_USR (1 << 3)
2125 #define USTYPE_ALL \
2126 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2128 static int us_type_bits
[] = {
2135 static char *us_type_names
[] = { "posixgroup", "posxiuser", "smbgroup",
2138 typedef struct us_node
{
2140 uu_avl_node_t usn_avlnode
;
2141 uu_list_node_t usn_listnode
;
2144 typedef struct us_cbdata
{
2146 uu_avl_pool_t
*cb_avl_pool
;
2148 boolean_t cb_numname
;
2149 boolean_t cb_nicenum
;
2150 boolean_t cb_sid2posix
;
2151 zfs_userquota_prop_t cb_prop
;
2152 zfs_sort_column_t
*cb_sortcol
;
2153 size_t cb_width
[USFIELD_LAST
];
2156 static boolean_t us_populated
= B_FALSE
;
2159 zfs_sort_column_t
*si_sortcol
;
2160 boolean_t si_numname
;
2164 us_field_index(char *field
)
2168 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2169 if (strcmp(field
, us_field_names
[i
]) == 0)
2177 us_compare(const void *larg
, const void *rarg
, void *unused
)
2179 const us_node_t
*l
= larg
;
2180 const us_node_t
*r
= rarg
;
2181 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2182 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2183 boolean_t numname
= si
->si_numname
;
2184 nvlist_t
*lnvl
= l
->usn_nvl
;
2185 nvlist_t
*rnvl
= r
->usn_nvl
;
2189 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2196 zfs_prop_t prop
= sortcol
->sc_prop
;
2197 const char *propname
= NULL
;
2198 boolean_t reverse
= sortcol
->sc_reverse
;
2203 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2204 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2206 rc
= (rv32
< lv32
) ? 1 : -1;
2211 (void) nvlist_lookup_uint64(lnvl
, propname
,
2213 (void) nvlist_lookup_uint64(rnvl
, propname
,
2216 rc
= (rv64
< lv64
) ? 1 : -1;
2218 (void) nvlist_lookup_string(lnvl
, propname
,
2220 (void) nvlist_lookup_string(rnvl
, propname
,
2222 rc
= strcmp(lvstr
, rvstr
);
2226 case ZFS_PROP_QUOTA
:
2229 if (prop
== ZFS_PROP_USED
)
2233 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2234 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2236 rc
= (rv64
< lv64
) ? 1 : -1;
2244 return (reverse
? 1 : -1);
2246 return (reverse
? -1 : 1);
2251 * If entries still seem to be the same, check if they are of the same
2252 * type (smbentity is added only if we are doing SID to POSIX ID
2253 * translation where we can have duplicate type/name combinations).
2255 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2256 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2258 return (lvb
< rvb
? -1 : 1);
2263 static inline const char *
2264 us_type2str(unsigned field_type
)
2266 switch (field_type
) {
2267 case USTYPE_PSX_USR
:
2268 return ("POSIX User");
2269 case USTYPE_PSX_GRP
:
2270 return ("POSIX Group");
2271 case USTYPE_SMB_USR
:
2272 return ("SMB User");
2273 case USTYPE_SMB_GRP
:
2274 return ("SMB Group");
2276 return ("Undefined");
2281 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2283 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2284 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2289 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2290 uu_avl_t
*avl
= cb
->cb_avl
;
2294 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2296 const char *typestr
;
2300 int typeidx
, nameidx
, sizeidx
;
2301 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2302 boolean_t smbentity
= B_FALSE
;
2304 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2306 node
= safe_malloc(sizeof (us_node_t
));
2307 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2308 node
->usn_nvl
= props
;
2310 if (domain
!= NULL
&& domain
[0] != '\0') {
2313 char sid
[ZFS_MAXNAMELEN
+ 32];
2317 directory_error_t e
;
2321 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2323 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2324 type
= USTYPE_SMB_GRP
;
2325 err
= sid_to_id(sid
, B_FALSE
, &id
);
2327 type
= USTYPE_SMB_USR
;
2328 err
= sid_to_id(sid
, B_TRUE
, &id
);
2333 if (!cb
->cb_sid2posix
) {
2334 e
= directory_name_from_sid(NULL
, sid
, &name
,
2337 directory_error_free(e
);
2347 #endif /* HAVE_IDMAP */
2350 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2352 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2353 type
= USTYPE_PSX_GRP
;
2354 if (!cb
->cb_numname
) {
2357 if ((g
= getgrgid(rid
)) != NULL
)
2361 type
= USTYPE_PSX_USR
;
2362 if (!cb
->cb_numname
) {
2365 if ((p
= getpwuid(rid
)) != NULL
)
2372 * Make sure that the type/name combination is unique when doing
2373 * SID to POSIX ID translation (hence changing the type from SMB to
2376 if (cb
->cb_sid2posix
&&
2377 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2380 /* Calculate/update width of TYPE field */
2381 typestr
= us_type2str(type
);
2382 typelen
= strlen(gettext(typestr
));
2383 typeidx
= us_field_index("type");
2384 if (typelen
> cb
->cb_width
[typeidx
])
2385 cb
->cb_width
[typeidx
] = typelen
;
2386 if (nvlist_add_uint32(props
, "type", type
) != 0)
2389 /* Calculate/update width of NAME field */
2390 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2391 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2393 namelen
= snprintf(NULL
, 0, "%u", rid
);
2395 if (nvlist_add_string(props
, "name", name
) != 0)
2397 namelen
= strlen(name
);
2399 nameidx
= us_field_index("name");
2400 if (namelen
> cb
->cb_width
[nameidx
])
2401 cb
->cb_width
[nameidx
] = namelen
;
2404 * Check if this type/name combination is in the list and update it;
2405 * otherwise add new node to the list.
2407 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2408 uu_avl_insert(avl
, node
, idx
);
2413 props
= node
->usn_nvl
;
2416 /* Calculate/update width of USED/QUOTA fields */
2418 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2420 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu",
2421 (u_longlong_t
)space
);
2422 sizelen
= strlen(sizebuf
);
2423 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
) {
2425 if (!nvlist_exists(props
, "quota"))
2426 (void) nvlist_add_uint64(props
, "quota", 0);
2429 if (!nvlist_exists(props
, "used"))
2430 (void) nvlist_add_uint64(props
, "used", 0);
2432 sizeidx
= us_field_index(propname
);
2433 if (sizelen
> cb
->cb_width
[sizeidx
])
2434 cb
->cb_width
[sizeidx
] = sizelen
;
2436 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2443 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2444 size_t *width
, us_node_t
*node
)
2446 nvlist_t
*nvl
= node
->usn_nvl
;
2447 char valstr
[ZFS_MAXNAMELEN
];
2448 boolean_t first
= B_TRUE
;
2454 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2455 if (!(ustype
& types
))
2458 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2459 nvpair_t
*nvp
= NULL
;
2463 char *strval
= NULL
;
2465 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2466 if (strcmp(nvpair_name(nvp
),
2467 us_field_names
[field
]) == 0)
2471 type
= nvpair_type(nvp
);
2473 case DATA_TYPE_UINT32
:
2474 (void) nvpair_value_uint32(nvp
, &val32
);
2476 case DATA_TYPE_UINT64
:
2477 (void) nvpair_value_uint64(nvp
, &val64
);
2479 case DATA_TYPE_STRING
:
2480 (void) nvpair_value_string(nvp
, &strval
);
2483 (void) fprintf(stderr
, "invalid data type\n");
2488 strval
= (char *)us_type2str(val32
);
2491 if (type
== DATA_TYPE_UINT64
) {
2492 (void) sprintf(valstr
, "%llu",
2493 (u_longlong_t
) val64
);
2499 if (type
== DATA_TYPE_UINT64
) {
2501 (void) sprintf(valstr
, "%llu",
2502 (u_longlong_t
) val64
);
2504 zfs_nicenum(val64
, valstr
,
2507 if (field
== USFIELD_QUOTA
&&
2508 strcmp(valstr
, "0") == 0)
2518 (void) printf("\t");
2523 (void) printf("%s", strval
);
2524 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2525 (void) printf("%-*s", (int) width
[field
], strval
);
2527 (void) printf("%*s", (int) width
[field
], strval
);
2533 (void) printf("\n");
2537 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2538 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2546 boolean_t first
= B_TRUE
;
2548 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2549 col
= gettext(us_field_hdr
[field
]);
2550 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2551 (void) printf(first
? "%-*s" : " %-*s",
2552 (int) width
[field
], col
);
2554 (void) printf(first
? "%*s" : " %*s",
2555 (int) width
[field
], col
);
2560 (void) printf("\n");
2563 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2564 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2566 nvlist_free(node
->usn_nvl
);
2571 zfs_do_userspace(int argc
, char **argv
)
2574 zfs_userquota_prop_t p
;
2575 uu_avl_pool_t
*avl_pool
;
2577 uu_avl_walk_t
*walk
;
2579 char deffields
[] = "type,name,used,quota";
2580 char *ofield
= NULL
;
2581 char *tfield
= NULL
;
2585 boolean_t scripted
= B_FALSE
;
2586 boolean_t prtnum
= B_FALSE
;
2587 boolean_t parsable
= B_FALSE
;
2588 boolean_t sid2posix
= B_FALSE
;
2591 zfs_sort_column_t
*sortcol
= NULL
;
2592 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2596 uu_list_pool_t
*listpool
;
2598 uu_avl_index_t idx
= 0;
2599 uu_list_index_t idx2
= 0;
2604 if (strcmp(argv
[0], "groupspace") == 0)
2605 /* Toggle default group types */
2606 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2608 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2624 if (zfs_add_sort_column(&sortcol
, optarg
,
2625 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
2626 (void) fprintf(stderr
,
2627 gettext("invalid field '%s'\n"), optarg
);
2638 (void) fprintf(stderr
, gettext("missing argument for "
2639 "'%c' option\n"), optopt
);
2643 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2653 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2657 (void) fprintf(stderr
, gettext("too many arguments\n"));
2661 /* Use default output fields if not specified using -o */
2665 if ((delim
= strchr(ofield
, ',')) != NULL
)
2667 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
2668 (void) fprintf(stderr
, gettext("invalid type '%s' "
2669 "for -o option\n"), ofield
);
2674 } while (delim
!= NULL
);
2675 fields
[cfield
] = USFIELD_LAST
;
2677 /* Override output types (-t option) */
2678 if (tfield
!= NULL
) {
2682 boolean_t found
= B_FALSE
;
2684 if ((delim
= strchr(tfield
, ',')) != NULL
)
2686 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
2688 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
2690 types
|= us_type_bits
[i
];
2695 (void) fprintf(stderr
, gettext("invalid type "
2696 "'%s' for -t option\n"), tfield
);
2701 } while (delim
!= NULL
);
2704 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
2707 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
2708 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
2710 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
2713 /* Always add default sorting columns */
2714 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
2715 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
2717 cb
.cb_sortcol
= sortcol
;
2718 cb
.cb_numname
= prtnum
;
2719 cb
.cb_nicenum
= !parsable
;
2720 cb
.cb_avl_pool
= avl_pool
;
2721 cb
.cb_avl
= avl_tree
;
2722 cb
.cb_sid2posix
= sid2posix
;
2724 for (i
= 0; i
< USFIELD_LAST
; i
++)
2725 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
2727 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
2728 if (((p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
) &&
2729 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
2730 ((p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
) &&
2731 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))))
2734 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
2739 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
2742 us_populated
= B_TRUE
;
2744 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
2745 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
2746 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
2747 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
2749 while (node
!= NULL
) {
2751 node
= uu_avl_next(avl_tree
, node
);
2752 uu_avl_remove(avl_tree
, rmnode
);
2753 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
2754 uu_list_insert(list
, rmnode
, idx2
);
2757 for (node
= uu_list_first(list
); node
!= NULL
;
2758 node
= uu_list_next(list
, node
)) {
2759 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
2761 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
2762 uu_avl_insert(avl_tree
, node
, idx
);
2765 uu_list_destroy(list
);
2766 uu_list_pool_destroy(listpool
);
2768 /* Print and free node nvlist memory */
2769 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
2772 zfs_free_sort_columns(sortcol
);
2774 /* Clean up the AVL tree */
2775 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
2778 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
2779 uu_avl_remove(cb
.cb_avl
, node
);
2783 uu_avl_walk_end(walk
);
2784 uu_avl_destroy(avl_tree
);
2785 uu_avl_pool_destroy(avl_pool
);
2791 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
2792 * [-s property [-s property]...] [-S property [-S property]...]
2795 * -r Recurse over all children
2796 * -d Limit recursion by depth.
2797 * -H Scripted mode; elide headers and separate columns by tabs
2798 * -o Control which fields to display.
2799 * -t Control which object types to display.
2800 * -s Specify sort columns, descending order.
2801 * -S Specify sort columns, ascending order.
2803 * When given no arguments, lists all filesystems in the system.
2804 * Otherwise, list the specified datasets, optionally recursing down them if
2805 * '-r' is specified.
2807 typedef struct list_cbdata
{
2809 boolean_t cb_scripted
;
2810 zprop_list_t
*cb_proplist
;
2814 * Given a list of columns to display, output appropriate headers for each one.
2817 print_header(zprop_list_t
*pl
)
2819 char headerbuf
[ZFS_MAXPROPLEN
];
2822 boolean_t first
= B_TRUE
;
2823 boolean_t right_justify
;
2825 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2832 right_justify
= B_FALSE
;
2833 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2834 header
= zfs_prop_column_name(pl
->pl_prop
);
2835 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2837 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2838 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2839 headerbuf
[i
] = '\0';
2843 if (pl
->pl_next
== NULL
&& !right_justify
)
2844 (void) printf("%s", header
);
2845 else if (right_justify
)
2846 (void) printf("%*s", (int)pl
->pl_width
, header
);
2848 (void) printf("%-*s", (int)pl
->pl_width
, header
);
2851 (void) printf("\n");
2855 * Given a dataset and a list of fields, print out all the properties according
2856 * to the described layout.
2859 print_dataset(zfs_handle_t
*zhp
, zprop_list_t
*pl
, boolean_t scripted
)
2861 boolean_t first
= B_TRUE
;
2862 char property
[ZFS_MAXPROPLEN
];
2863 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
2866 boolean_t right_justify
;
2869 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2872 (void) printf("\t");
2879 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
2880 (void) strlcpy(property
, zfs_get_name(zhp
),
2883 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2884 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
2885 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
2886 sizeof (property
), NULL
, NULL
, 0, B_FALSE
) != 0)
2891 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2892 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
2893 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
2894 property
, sizeof (property
), B_FALSE
) != 0)
2898 right_justify
= B_TRUE
;
2899 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
2900 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
2901 property
, sizeof (property
), B_FALSE
) != 0)
2905 right_justify
= B_TRUE
;
2907 if (nvlist_lookup_nvlist(userprops
,
2908 pl
->pl_user_prop
, &propval
) != 0)
2911 verify(nvlist_lookup_string(propval
,
2912 ZPROP_VALUE
, &propstr
) == 0);
2913 right_justify
= B_FALSE
;
2916 width
= pl
->pl_width
;
2919 * If this is being called in scripted mode, or if this is the
2920 * last column and it is left-justified, don't include a width
2923 if (scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
2924 (void) printf("%s", propstr
);
2925 else if (right_justify
)
2926 (void) printf("%*s", width
, propstr
);
2928 (void) printf("%-*s", width
, propstr
);
2931 (void) printf("\n");
2935 * Generic callback function to list a dataset or snapshot.
2938 list_callback(zfs_handle_t
*zhp
, void *data
)
2940 list_cbdata_t
*cbp
= data
;
2942 if (cbp
->cb_first
) {
2943 if (!cbp
->cb_scripted
)
2944 print_header(cbp
->cb_proplist
);
2945 cbp
->cb_first
= B_FALSE
;
2948 print_dataset(zhp
, cbp
->cb_proplist
, cbp
->cb_scripted
);
2954 zfs_do_list(int argc
, char **argv
)
2957 boolean_t scripted
= B_FALSE
;
2958 static char default_fields
[] =
2959 "name,used,available,referenced,mountpoint";
2960 int types
= ZFS_TYPE_DATASET
;
2961 boolean_t types_specified
= B_FALSE
;
2962 char *fields
= NULL
;
2963 list_cbdata_t cb
= { 0 };
2967 zfs_sort_column_t
*sortcol
= NULL
;
2968 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
2971 while ((c
= getopt(argc
, argv
, ":d:o:rt:Hs:S:")) != -1) {
2977 limit
= parse_depth(optarg
, &flags
);
2980 flags
|= ZFS_ITER_RECURSE
;
2986 if (zfs_add_sort_column(&sortcol
, optarg
,
2988 (void) fprintf(stderr
,
2989 gettext("invalid property '%s'\n"), optarg
);
2994 if (zfs_add_sort_column(&sortcol
, optarg
,
2996 (void) fprintf(stderr
,
2997 gettext("invalid property '%s'\n"), optarg
);
3003 types_specified
= B_TRUE
;
3004 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3005 while (*optarg
!= '\0') {
3006 static char *type_subopts
[] = { "filesystem",
3007 "volume", "snapshot", "snap", "all", NULL
};
3009 switch (getsubopt(&optarg
, type_subopts
,
3012 types
|= ZFS_TYPE_FILESYSTEM
;
3015 types
|= ZFS_TYPE_VOLUME
;
3019 types
|= ZFS_TYPE_SNAPSHOT
;
3022 types
= ZFS_TYPE_DATASET
;
3026 (void) fprintf(stderr
,
3027 gettext("invalid type '%s'\n"),
3034 (void) fprintf(stderr
, gettext("missing argument for "
3035 "'%c' option\n"), optopt
);
3039 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3049 fields
= default_fields
;
3052 * If we are only going to list snapshot names and sort by name,
3053 * then we can use faster version.
3055 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3056 flags
|= ZFS_ITER_SIMPLE
;
3059 * If "-o space" and no types were specified, don't display snapshots.
3061 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3062 types
&= ~ZFS_TYPE_SNAPSHOT
;
3065 * If the user specifies '-o all', the zprop_get_list() doesn't
3066 * normally include the name of the dataset. For 'zfs list', we always
3067 * want this property to be first.
3069 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3073 cb
.cb_scripted
= scripted
;
3074 cb
.cb_first
= B_TRUE
;
3076 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3077 limit
, list_callback
, &cb
);
3079 zprop_free_list(cb
.cb_proplist
);
3080 zfs_free_sort_columns(sortcol
);
3082 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3083 (void) fprintf(stderr
, gettext("no datasets available\n"));
3089 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3090 * zfs rename [-f] -p <fs | vol> <fs | vol>
3091 * zfs rename -r <snap> <snap>
3093 * Renames the given dataset to another of the same type.
3095 * The '-p' flag creates all the non-existing ancestors of the target first.
3099 zfs_do_rename(int argc
, char **argv
)
3104 boolean_t recurse
= B_FALSE
;
3105 boolean_t parents
= B_FALSE
;
3106 boolean_t force_unmount
= B_FALSE
;
3109 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3118 force_unmount
= B_TRUE
;
3122 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3131 /* check number of arguments */
3133 (void) fprintf(stderr
, gettext("missing source dataset "
3138 (void) fprintf(stderr
, gettext("missing target dataset "
3143 (void) fprintf(stderr
, gettext("too many arguments\n"));
3147 if (recurse
&& parents
) {
3148 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3153 if (recurse
&& strchr(argv
[0], '@') == 0) {
3154 (void) fprintf(stderr
, gettext("source dataset for recursive "
3155 "rename must be a snapshot\n"));
3159 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3160 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3163 /* If we were asked and the name looks good, try to create ancestors. */
3164 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3165 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3170 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3179 * Promotes the given clone fs to be the parent
3183 zfs_do_promote(int argc
, char **argv
)
3189 if (argc
> 1 && argv
[1][0] == '-') {
3190 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3195 /* check number of arguments */
3197 (void) fprintf(stderr
, gettext("missing clone filesystem"
3202 (void) fprintf(stderr
, gettext("too many arguments\n"));
3206 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3210 ret
= (zfs_promote(zhp
) != 0);
3218 * zfs rollback [-rRf] <snapshot>
3220 * -r Delete any intervening snapshots before doing rollback
3221 * -R Delete any snapshots and their clones
3222 * -f ignored for backwards compatability
3224 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3225 * since then and making it the active dataset. If more recent snapshots exist,
3226 * the command will complain unless the '-r' flag is given.
3228 typedef struct rollback_cbdata
{
3234 boolean_t cb_recurse
;
3235 boolean_t cb_dependent
;
3236 } rollback_cbdata_t
;
3239 * Report any snapshots more recent than the one specified. Used when '-r' is
3240 * not specified. We reuse this same callback for the snapshot dependents - if
3241 * 'cb_dependent' is set, then this is a dependent and we should report it
3242 * without checking the transaction group.
3245 rollback_check(zfs_handle_t
*zhp
, void *data
)
3247 rollback_cbdata_t
*cbp
= data
;
3249 if (cbp
->cb_doclones
) {
3254 if (!cbp
->cb_dependent
) {
3255 if (strcmp(zfs_get_name(zhp
), cbp
->cb_target
) != 0 &&
3256 zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
3257 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) >
3260 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3261 (void) fprintf(stderr
, gettext("cannot "
3262 "rollback to '%s': more recent snapshots "
3265 (void) fprintf(stderr
, gettext("use '-r' to "
3266 "force deletion of the following "
3272 if (cbp
->cb_recurse
) {
3273 cbp
->cb_dependent
= B_TRUE
;
3274 if (zfs_iter_dependents(zhp
, B_TRUE
,
3275 rollback_check
, cbp
) != 0) {
3279 cbp
->cb_dependent
= B_FALSE
;
3281 (void) fprintf(stderr
, "%s\n",
3286 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3287 (void) fprintf(stderr
, gettext("cannot rollback to "
3288 "'%s': clones of previous snapshots exist\n"),
3290 (void) fprintf(stderr
, gettext("use '-R' to "
3291 "force deletion of the following clones and "
3297 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3305 zfs_do_rollback(int argc
, char **argv
)
3309 boolean_t force
= B_FALSE
;
3310 rollback_cbdata_t cb
= { 0 };
3311 zfs_handle_t
*zhp
, *snap
;
3312 char parentname
[ZFS_MAXNAMELEN
];
3316 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3329 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3338 /* check number of arguments */
3340 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3344 (void) fprintf(stderr
, gettext("too many arguments\n"));
3348 /* open the snapshot */
3349 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3352 /* open the parent dataset */
3353 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3354 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3356 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3362 * Check for more recent snapshots and/or clones based on the presence
3365 cb
.cb_target
= argv
[0];
3366 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3367 cb
.cb_first
= B_TRUE
;
3369 if ((ret
= zfs_iter_children(zhp
, rollback_check
, &cb
)) != 0)
3372 if ((ret
= cb
.cb_error
) != 0)
3376 * Rollback parent to the given snapshot.
3378 ret
= zfs_rollback(zhp
, snap
, force
);
3391 * zfs set property=value { fs | snap | vol } ...
3393 * Sets the given property for all datasets specified on the command line.
3395 typedef struct set_cbdata
{
3401 set_callback(zfs_handle_t
*zhp
, void *data
)
3403 set_cbdata_t
*cbp
= data
;
3405 if (zfs_prop_set(zhp
, cbp
->cb_propname
, cbp
->cb_value
) != 0) {
3406 switch (libzfs_errno(g_zfs
)) {
3407 case EZFS_MOUNTFAILED
:
3408 (void) fprintf(stderr
, gettext("property may be set "
3409 "but unable to remount filesystem\n"));
3411 case EZFS_SHARENFSFAILED
:
3412 (void) fprintf(stderr
, gettext("property may be set "
3413 "but unable to reshare filesystem\n"));
3422 zfs_do_set(int argc
, char **argv
)
3427 /* check for options */
3428 if (argc
> 1 && argv
[1][0] == '-') {
3429 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3434 /* check number of arguments */
3436 (void) fprintf(stderr
, gettext("missing property=value "
3441 (void) fprintf(stderr
, gettext("missing dataset name\n"));
3445 /* validate property=value argument */
3446 cb
.cb_propname
= argv
[1];
3447 if (((cb
.cb_value
= strchr(cb
.cb_propname
, '=')) == NULL
) ||
3448 (cb
.cb_value
[1] == '\0')) {
3449 (void) fprintf(stderr
, gettext("missing value in "
3450 "property=value argument\n"));
3454 *cb
.cb_value
= '\0';
3457 if (*cb
.cb_propname
== '\0') {
3458 (void) fprintf(stderr
,
3459 gettext("missing property in property=value argument\n"));
3463 ret
= zfs_for_each(argc
- 2, argv
+ 2, 0,
3464 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, &cb
);
3469 typedef struct snap_cbdata
{
3471 boolean_t sd_recursive
;
3472 const char *sd_snapname
;
3476 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3478 snap_cbdata_t
*sd
= arg
;
3483 if (sd
->sd_recursive
&&
3484 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
3489 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3492 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3495 if (sd
->sd_recursive
)
3496 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3502 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3504 * Creates a snapshot with the given name. While functionally equivalent to
3505 * 'zfs create', it is a separate command to differentiate intent.
3508 zfs_do_snapshot(int argc
, char **argv
)
3513 snap_cbdata_t sd
= { 0 };
3514 boolean_t multiple_snaps
= B_FALSE
;
3516 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3518 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3522 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3525 if (parseprop(props
))
3529 sd
.sd_recursive
= B_TRUE
;
3530 multiple_snaps
= B_TRUE
;
3533 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3542 /* check number of arguments */
3544 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3549 multiple_snaps
= B_TRUE
;
3550 for (; argc
> 0; argc
--, argv
++) {
3554 atp
= strchr(argv
[0], '@');
3558 sd
.sd_snapname
= atp
+ 1;
3559 zhp
= zfs_open(g_zfs
, argv
[0],
3560 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3563 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3567 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3568 nvlist_free(sd
.sd_nvl
);
3570 if (ret
!= 0 && multiple_snaps
)
3571 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3575 nvlist_free(sd
.sd_nvl
);
3582 * Send a backup stream to stdout.
3585 zfs_do_send(int argc
, char **argv
)
3587 char *fromname
= NULL
;
3588 char *toname
= NULL
;
3591 sendflags_t flags
= { 0 };
3593 nvlist_t
*dbgnv
= NULL
;
3594 boolean_t extraverbose
= B_FALSE
;
3597 while ((c
= getopt(argc
, argv
, ":i:I:RDpvnP")) != -1) {
3608 flags
.doall
= B_TRUE
;
3611 flags
.replicate
= B_TRUE
;
3614 flags
.props
= B_TRUE
;
3617 flags
.parsable
= B_TRUE
;
3618 flags
.verbose
= B_TRUE
;
3622 extraverbose
= B_TRUE
;
3623 flags
.verbose
= B_TRUE
;
3624 flags
.progress
= B_TRUE
;
3627 flags
.dedup
= B_TRUE
;
3630 flags
.dryrun
= B_TRUE
;
3633 (void) fprintf(stderr
, gettext("missing argument for "
3634 "'%c' option\n"), optopt
);
3638 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3647 /* check number of arguments */
3649 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3653 (void) fprintf(stderr
, gettext("too many arguments\n"));
3657 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
3658 (void) fprintf(stderr
,
3659 gettext("Error: Stream can not be written to a terminal.\n"
3660 "You must redirect standard output.\n"));
3664 cp
= strchr(argv
[0], '@');
3666 (void) fprintf(stderr
,
3667 gettext("argument must be a snapshot\n"));
3672 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3677 * If they specified the full path to the snapshot, chop off
3678 * everything except the short name of the snapshot, but special
3679 * case if they specify the origin.
3681 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
3682 char origin
[ZFS_MAXNAMELEN
];
3685 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
3686 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
3688 if (strcmp(origin
, fromname
) == 0) {
3690 flags
.fromorigin
= B_TRUE
;
3693 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
3694 (void) fprintf(stderr
,
3695 gettext("incremental source must be "
3696 "in same filesystem\n"));
3700 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
3701 (void) fprintf(stderr
,
3702 gettext("invalid incremental source\n"));
3708 if (flags
.replicate
&& fromname
== NULL
)
3709 flags
.doall
= B_TRUE
;
3711 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
3712 extraverbose
? &dbgnv
: NULL
);
3714 if (extraverbose
&& dbgnv
!= NULL
) {
3716 * dump_nvlist prints to stdout, but that's been
3717 * redirected to a file. Make it print to stderr
3720 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
3721 dump_nvlist(dbgnv
, 0);
3730 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3732 * Restore a backup stream from stdin.
3735 zfs_do_receive(int argc
, char **argv
)
3738 recvflags_t flags
= { 0 };
3741 while ((c
= getopt(argc
, argv
, ":denuvF")) != -1) {
3744 flags
.isprefix
= B_TRUE
;
3747 flags
.isprefix
= B_TRUE
;
3748 flags
.istail
= B_TRUE
;
3751 flags
.dryrun
= B_TRUE
;
3754 flags
.nomount
= B_TRUE
;
3757 flags
.verbose
= B_TRUE
;
3760 flags
.force
= B_TRUE
;
3763 (void) fprintf(stderr
, gettext("missing argument for "
3764 "'%c' option\n"), optopt
);
3768 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3777 /* check number of arguments */
3779 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3783 (void) fprintf(stderr
, gettext("too many arguments\n"));
3787 if (isatty(STDIN_FILENO
)) {
3788 (void) fprintf(stderr
,
3789 gettext("Error: Backup stream can not be read "
3790 "from a terminal.\n"
3791 "You must redirect standard input.\n"));
3795 err
= zfs_receive(g_zfs
, argv
[0], &flags
, STDIN_FILENO
, NULL
);
3801 * allow/unallow stuff
3803 /* copied from zfs/sys/dsl_deleg.h */
3804 #define ZFS_DELEG_PERM_CREATE "create"
3805 #define ZFS_DELEG_PERM_DESTROY "destroy"
3806 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3807 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3808 #define ZFS_DELEG_PERM_CLONE "clone"
3809 #define ZFS_DELEG_PERM_PROMOTE "promote"
3810 #define ZFS_DELEG_PERM_RENAME "rename"
3811 #define ZFS_DELEG_PERM_MOUNT "mount"
3812 #define ZFS_DELEG_PERM_SHARE "share"
3813 #define ZFS_DELEG_PERM_SEND "send"
3814 #define ZFS_DELEG_PERM_RECEIVE "receive"
3815 #define ZFS_DELEG_PERM_ALLOW "allow"
3816 #define ZFS_DELEG_PERM_USERPROP "userprop"
3817 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3818 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3819 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3820 #define ZFS_DELEG_PERM_USERUSED "userused"
3821 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3822 #define ZFS_DELEG_PERM_HOLD "hold"
3823 #define ZFS_DELEG_PERM_RELEASE "release"
3824 #define ZFS_DELEG_PERM_DIFF "diff"
3826 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3828 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
3829 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
3830 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
3831 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
3832 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
3833 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
3834 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
3835 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
3836 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
3837 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
3838 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
3839 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
3840 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
3841 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
3842 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
3843 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
3845 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
3846 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
3847 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
3848 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
3849 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
3850 { NULL
, ZFS_DELEG_NOTE_NONE
}
3853 /* permission structure */
3854 typedef struct deleg_perm
{
3855 zfs_deleg_who_type_t dp_who_type
;
3856 const char *dp_name
;
3858 boolean_t dp_descend
;
3862 typedef struct deleg_perm_node
{
3863 deleg_perm_t dpn_perm
;
3865 uu_avl_node_t dpn_avl_node
;
3866 } deleg_perm_node_t
;
3868 typedef struct fs_perm fs_perm_t
;
3870 /* permissions set */
3871 typedef struct who_perm
{
3872 zfs_deleg_who_type_t who_type
;
3873 const char *who_name
; /* id */
3874 char who_ug_name
[256]; /* user/group name */
3875 fs_perm_t
*who_fsperm
; /* uplink */
3877 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
3881 typedef struct who_perm_node
{
3882 who_perm_t who_perm
;
3883 uu_avl_node_t who_avl_node
;
3886 typedef struct fs_perm_set fs_perm_set_t
;
3887 /* fs permissions */
3889 const char *fsp_name
;
3891 uu_avl_t
*fsp_sc_avl
; /* sets,create */
3892 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
3894 fs_perm_set_t
*fsp_set
; /* uplink */
3898 typedef struct fs_perm_node
{
3899 fs_perm_t fspn_fsperm
;
3902 uu_list_node_t fspn_list_node
;
3905 /* top level structure */
3906 struct fs_perm_set
{
3907 uu_list_pool_t
*fsps_list_pool
;
3908 uu_list_t
*fsps_list
; /* list of fs_perms */
3910 uu_avl_pool_t
*fsps_named_set_avl_pool
;
3911 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
3912 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
3915 static inline const char *
3916 deleg_perm_type(zfs_deleg_note_t note
)
3922 case ZFS_DELEG_NOTE_GROUPQUOTA
:
3923 case ZFS_DELEG_NOTE_GROUPUSED
:
3924 case ZFS_DELEG_NOTE_USERPROP
:
3925 case ZFS_DELEG_NOTE_USERQUOTA
:
3926 case ZFS_DELEG_NOTE_USERUSED
:
3928 return (gettext("other"));
3930 return (gettext("subcommand"));
3935 who_type2weight(zfs_deleg_who_type_t who_type
)
3939 case ZFS_DELEG_NAMED_SET_SETS
:
3940 case ZFS_DELEG_NAMED_SET
:
3943 case ZFS_DELEG_CREATE_SETS
:
3944 case ZFS_DELEG_CREATE
:
3947 case ZFS_DELEG_USER_SETS
:
3948 case ZFS_DELEG_USER
:
3951 case ZFS_DELEG_GROUP_SETS
:
3952 case ZFS_DELEG_GROUP
:
3955 case ZFS_DELEG_EVERYONE_SETS
:
3956 case ZFS_DELEG_EVERYONE
:
3968 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
3970 const who_perm_node_t
*l
= larg
;
3971 const who_perm_node_t
*r
= rarg
;
3972 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
3973 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
3974 int lweight
= who_type2weight(ltype
);
3975 int rweight
= who_type2weight(rtype
);
3976 int res
= lweight
- rweight
;
3978 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
3979 ZFS_MAX_DELEG_NAME
-1);
3991 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
3993 const deleg_perm_node_t
*l
= larg
;
3994 const deleg_perm_node_t
*r
= rarg
;
3995 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
3996 ZFS_MAX_DELEG_NAME
-1);
4008 fs_perm_set_init(fs_perm_set_t
*fspset
)
4010 bzero(fspset
, sizeof (fs_perm_set_t
));
4012 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4013 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4014 NULL
, UU_DEFAULT
)) == NULL
)
4016 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4017 UU_DEFAULT
)) == NULL
)
4020 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4021 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4022 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4023 UU_DEFAULT
)) == NULL
)
4026 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4027 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4028 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4029 UU_DEFAULT
)) == NULL
)
4032 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4033 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4034 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4039 static inline void fs_perm_fini(fs_perm_t
*);
4040 static inline void who_perm_fini(who_perm_t
*);
4043 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4045 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4047 while (node
!= NULL
) {
4048 fs_perm_node_t
*next_node
=
4049 uu_list_next(fspset
->fsps_list
, node
);
4050 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4051 fs_perm_fini(fsperm
);
4052 uu_list_remove(fspset
->fsps_list
, node
);
4057 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4058 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4059 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4063 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4066 deleg_perm
->dp_who_type
= type
;
4067 deleg_perm
->dp_name
= name
;
4071 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4072 zfs_deleg_who_type_t type
, const char *name
)
4074 uu_avl_pool_t
*pool
;
4075 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4077 bzero(who_perm
, sizeof (who_perm_t
));
4079 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4080 UU_DEFAULT
)) == NULL
)
4083 who_perm
->who_type
= type
;
4084 who_perm
->who_name
= name
;
4085 who_perm
->who_fsperm
= fsperm
;
4089 who_perm_fini(who_perm_t
*who_perm
)
4091 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4093 while (node
!= NULL
) {
4094 deleg_perm_node_t
*next_node
=
4095 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4097 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4102 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4106 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4108 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4109 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4111 bzero(fsperm
, sizeof (fs_perm_t
));
4113 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4117 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4121 fsperm
->fsp_set
= fspset
;
4122 fsperm
->fsp_name
= fsname
;
4126 fs_perm_fini(fs_perm_t
*fsperm
)
4128 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4129 while (node
!= NULL
) {
4130 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4132 who_perm_t
*who_perm
= &node
->who_perm
;
4133 who_perm_fini(who_perm
);
4134 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4139 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4140 while (node
!= NULL
) {
4141 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4143 who_perm_t
*who_perm
= &node
->who_perm
;
4144 who_perm_fini(who_perm
);
4145 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4150 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4151 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4155 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4156 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4158 uu_avl_index_t idx
= 0;
4160 deleg_perm_node_t
*found_node
= NULL
;
4161 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4163 deleg_perm_init(deleg_perm
, who_type
, name
);
4165 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4167 uu_avl_insert(avl
, node
, idx
);
4170 deleg_perm
= &node
->dpn_perm
;
4175 case ZFS_DELEG_LOCAL
:
4176 deleg_perm
->dp_local
= B_TRUE
;
4178 case ZFS_DELEG_DESCENDENT
:
4179 deleg_perm
->dp_descend
= B_TRUE
;
4184 assert(B_FALSE
); /* invalid locality */
4189 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4191 nvpair_t
*nvp
= NULL
;
4192 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4193 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4194 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4196 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4197 const char *name
= nvpair_name(nvp
);
4198 data_type_t type
= nvpair_type(nvp
);
4199 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4200 deleg_perm_node_t
*node
=
4201 safe_malloc(sizeof (deleg_perm_node_t
));
4203 VERIFY(type
== DATA_TYPE_BOOLEAN
);
4205 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4206 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4213 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4215 nvpair_t
*nvp
= NULL
;
4216 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4218 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4219 nvlist_t
*nvl2
= NULL
;
4220 const char *name
= nvpair_name(nvp
);
4221 uu_avl_t
*avl
= NULL
;
4222 uu_avl_pool_t
*avl_pool
= NULL
;
4223 zfs_deleg_who_type_t perm_type
= name
[0];
4224 char perm_locality
= name
[1];
4225 const char *perm_name
= name
+ 3;
4226 boolean_t is_set
= B_TRUE
;
4227 who_perm_t
*who_perm
= NULL
;
4229 assert('$' == name
[2]);
4231 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4234 switch (perm_type
) {
4235 case ZFS_DELEG_CREATE
:
4236 case ZFS_DELEG_CREATE_SETS
:
4237 case ZFS_DELEG_NAMED_SET
:
4238 case ZFS_DELEG_NAMED_SET_SETS
:
4239 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4240 avl
= fsperm
->fsp_sc_avl
;
4242 case ZFS_DELEG_USER
:
4243 case ZFS_DELEG_USER_SETS
:
4244 case ZFS_DELEG_GROUP
:
4245 case ZFS_DELEG_GROUP_SETS
:
4246 case ZFS_DELEG_EVERYONE
:
4247 case ZFS_DELEG_EVERYONE_SETS
:
4248 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4249 avl
= fsperm
->fsp_uge_avl
;
4256 who_perm_node_t
*found_node
= NULL
;
4257 who_perm_node_t
*node
= safe_malloc(
4258 sizeof (who_perm_node_t
));
4259 who_perm
= &node
->who_perm
;
4260 uu_avl_index_t idx
= 0;
4262 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4263 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4265 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4267 if (avl
== fsperm
->fsp_uge_avl
) {
4269 struct passwd
*p
= NULL
;
4270 struct group
*g
= NULL
;
4271 const char *nice_name
= NULL
;
4273 switch (perm_type
) {
4274 case ZFS_DELEG_USER_SETS
:
4275 case ZFS_DELEG_USER
:
4276 rid
= atoi(perm_name
);
4279 nice_name
= p
->pw_name
;
4281 case ZFS_DELEG_GROUP_SETS
:
4282 case ZFS_DELEG_GROUP
:
4283 rid
= atoi(perm_name
);
4286 nice_name
= g
->gr_name
;
4292 if (nice_name
!= NULL
)
4294 node
->who_perm
.who_ug_name
,
4298 uu_avl_insert(avl
, node
, idx
);
4301 who_perm
= &node
->who_perm
;
4305 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4312 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4314 nvpair_t
*nvp
= NULL
;
4315 uu_avl_index_t idx
= 0;
4317 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4318 nvlist_t
*nvl2
= NULL
;
4319 const char *fsname
= nvpair_name(nvp
);
4320 data_type_t type
= nvpair_type(nvp
);
4321 fs_perm_t
*fsperm
= NULL
;
4322 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4326 fsperm
= &node
->fspn_fsperm
;
4328 VERIFY(DATA_TYPE_NVLIST
== type
);
4330 uu_list_node_init(node
, &node
->fspn_list_node
,
4331 fspset
->fsps_list_pool
);
4333 idx
= uu_list_numnodes(fspset
->fsps_list
);
4334 fs_perm_init(fsperm
, fspset
, fsname
);
4336 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4339 (void) parse_fs_perm(fsperm
, nvl2
);
4341 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4347 static inline const char *
4348 deleg_perm_comment(zfs_deleg_note_t note
)
4350 const char *str
= "";
4355 case ZFS_DELEG_NOTE_ALLOW
:
4356 str
= gettext("Must also have the permission that is being"
4357 "\n\t\t\t\tallowed");
4359 case ZFS_DELEG_NOTE_CLONE
:
4360 str
= gettext("Must also have the 'create' ability and 'mount'"
4361 "\n\t\t\t\tability in the origin file system");
4363 case ZFS_DELEG_NOTE_CREATE
:
4364 str
= gettext("Must also have the 'mount' ability");
4366 case ZFS_DELEG_NOTE_DESTROY
:
4367 str
= gettext("Must also have the 'mount' ability");
4369 case ZFS_DELEG_NOTE_DIFF
:
4370 str
= gettext("Allows lookup of paths within a dataset;"
4371 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4372 "\n\t\t\t\tin order to use zfs diff");
4374 case ZFS_DELEG_NOTE_HOLD
:
4375 str
= gettext("Allows adding a user hold to a snapshot");
4377 case ZFS_DELEG_NOTE_MOUNT
:
4378 str
= gettext("Allows mount/umount of ZFS datasets");
4380 case ZFS_DELEG_NOTE_PROMOTE
:
4381 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4382 " 'promote' ability in the origin file system");
4384 case ZFS_DELEG_NOTE_RECEIVE
:
4385 str
= gettext("Must also have the 'mount' and 'create'"
4388 case ZFS_DELEG_NOTE_RELEASE
:
4389 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4390 "might destroy the snapshot");
4392 case ZFS_DELEG_NOTE_RENAME
:
4393 str
= gettext("Must also have the 'mount' and 'create'"
4394 "\n\t\t\t\tability in the new parent");
4396 case ZFS_DELEG_NOTE_ROLLBACK
:
4399 case ZFS_DELEG_NOTE_SEND
:
4402 case ZFS_DELEG_NOTE_SHARE
:
4403 str
= gettext("Allows sharing file systems over NFS or SMB"
4404 "\n\t\t\t\tprotocols");
4406 case ZFS_DELEG_NOTE_SNAPSHOT
:
4410 * case ZFS_DELEG_NOTE_VSCAN:
4411 * str = gettext("");
4415 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4416 str
= gettext("Allows accessing any groupquota@... property");
4418 case ZFS_DELEG_NOTE_GROUPUSED
:
4419 str
= gettext("Allows reading any groupused@... property");
4421 case ZFS_DELEG_NOTE_USERPROP
:
4422 str
= gettext("Allows changing any user property");
4424 case ZFS_DELEG_NOTE_USERQUOTA
:
4425 str
= gettext("Allows accessing any userquota@... property");
4427 case ZFS_DELEG_NOTE_USERUSED
:
4428 str
= gettext("Allows reading any userused@... property");
4446 boolean_t recursive
; /* unallow only */
4447 boolean_t prt_usage
;
4449 boolean_t prt_perms
;
4452 const char *dataset
;
4456 prop_cmp(const void *a
, const void *b
)
4458 const char *str1
= *(const char **)a
;
4459 const char *str2
= *(const char **)b
;
4460 return (strcmp(str1
, str2
));
4464 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
4466 const char *opt_desc
[] = {
4467 "-h", gettext("show this help message and exit"),
4468 "-l", gettext("set permission locally"),
4469 "-d", gettext("set permission for descents"),
4470 "-u", gettext("set permission for user"),
4471 "-g", gettext("set permission for group"),
4472 "-e", gettext("set permission for everyone"),
4473 "-c", gettext("set create time permission"),
4474 "-s", gettext("define permission set"),
4476 "-r", gettext("remove permissions recursively"),
4478 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
4479 size_t allow_size
= unallow_size
- 2;
4480 const char *props
[ZFS_NUM_PROPS
];
4483 FILE *fp
= requested
? stdout
: stderr
;
4484 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
4485 const char *fmt
= gettext("%-16s %-14s\t%s\n");
4487 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
4489 (void) fprintf(fp
, gettext("Options:\n"));
4490 for (i
= 0; i
< (un
? unallow_size
: allow_size
); i
++) {
4491 const char *opt
= opt_desc
[i
++];
4492 const char *optdsc
= opt_desc
[i
];
4493 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
4496 (void) fprintf(fp
, gettext("\nThe following permissions are "
4498 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
4500 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
4501 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
4502 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
4503 const char *perm_type
= deleg_perm_type(perm_note
);
4504 const char *perm_comment
= deleg_perm_comment(perm_note
);
4505 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
4508 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
4509 zprop_desc_t
*pd
= &pdtbl
[i
];
4510 if (pd
->pd_visible
!= B_TRUE
)
4513 if (pd
->pd_attr
== PROP_READONLY
)
4516 props
[count
++] = pd
->pd_name
;
4518 props
[count
] = NULL
;
4520 qsort(props
, count
, sizeof (char *), prop_cmp
);
4522 for (i
= 0; i
< count
; i
++)
4523 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
4526 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
4528 exit(requested
? 0 : 2);
4531 static inline const char *
4532 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
4535 if (un
&& argc
== expected_argc
- 1)
4537 else if (argc
== expected_argc
)
4538 *permsp
= argv
[argc
- 2];
4540 allow_usage(un
, B_FALSE
,
4541 gettext("wrong number of parameters\n"));
4543 return (argv
[argc
- 1]);
4547 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
4549 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
4550 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
4551 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
4552 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
4555 allow_usage(un
, B_FALSE
,
4556 gettext("-u, -g, and -e are mutually exclusive\n"));
4558 if (opts
->prt_usage
) {
4559 if (argc
== 0 && all_sum
== 0)
4560 allow_usage(un
, B_TRUE
, NULL
);
4567 allow_usage(un
, B_FALSE
,
4568 gettext("invalid options combined with -s\n"));
4570 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4571 if (argv
[0][0] != '@')
4572 allow_usage(un
, B_FALSE
,
4573 gettext("invalid set name: missing '@' prefix\n"));
4574 opts
->who
= argv
[0];
4575 } else if (opts
->create
) {
4576 if (ldcsuge_sum
> 1)
4577 allow_usage(un
, B_FALSE
,
4578 gettext("invalid options combined with -c\n"));
4579 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4580 } else if (opts
->everyone
) {
4582 allow_usage(un
, B_FALSE
,
4583 gettext("invalid options combined with -e\n"));
4584 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4585 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
4587 opts
->everyone
= B_TRUE
;
4590 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4591 } else if (argc
== 1 && !un
) {
4592 opts
->prt_perms
= B_TRUE
;
4593 opts
->dataset
= argv
[argc
-1];
4595 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4596 opts
->who
= argv
[0];
4599 if (!opts
->local
&& !opts
->descend
) {
4600 opts
->local
= B_TRUE
;
4601 opts
->descend
= B_TRUE
;
4606 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
4607 const char *who
, char *perms
, nvlist_t
*top_nvl
)
4610 char ld
[2] = { '\0', '\0' };
4611 char who_buf
[ZFS_MAXNAMELEN
+32];
4612 char base_type
= ZFS_DELEG_WHO_UNKNOWN
;
4613 char set_type
= ZFS_DELEG_WHO_UNKNOWN
;
4614 nvlist_t
*base_nvl
= NULL
;
4615 nvlist_t
*set_nvl
= NULL
;
4618 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4620 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4624 case ZFS_DELEG_NAMED_SET_SETS
:
4625 case ZFS_DELEG_NAMED_SET
:
4626 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
4627 base_type
= ZFS_DELEG_NAMED_SET
;
4628 ld
[0] = ZFS_DELEG_NA
;
4630 case ZFS_DELEG_CREATE_SETS
:
4631 case ZFS_DELEG_CREATE
:
4632 set_type
= ZFS_DELEG_CREATE_SETS
;
4633 base_type
= ZFS_DELEG_CREATE
;
4634 ld
[0] = ZFS_DELEG_NA
;
4636 case ZFS_DELEG_USER_SETS
:
4637 case ZFS_DELEG_USER
:
4638 set_type
= ZFS_DELEG_USER_SETS
;
4639 base_type
= ZFS_DELEG_USER
;
4641 ld
[0] = ZFS_DELEG_LOCAL
;
4643 ld
[1] = ZFS_DELEG_DESCENDENT
;
4645 case ZFS_DELEG_GROUP_SETS
:
4646 case ZFS_DELEG_GROUP
:
4647 set_type
= ZFS_DELEG_GROUP_SETS
;
4648 base_type
= ZFS_DELEG_GROUP
;
4650 ld
[0] = ZFS_DELEG_LOCAL
;
4652 ld
[1] = ZFS_DELEG_DESCENDENT
;
4654 case ZFS_DELEG_EVERYONE_SETS
:
4655 case ZFS_DELEG_EVERYONE
:
4656 set_type
= ZFS_DELEG_EVERYONE_SETS
;
4657 base_type
= ZFS_DELEG_EVERYONE
;
4659 ld
[0] = ZFS_DELEG_LOCAL
;
4661 ld
[1] = ZFS_DELEG_DESCENDENT
;
4666 if (perms
!= NULL
) {
4668 char *end
= curr
+ strlen(perms
);
4670 while (curr
< end
) {
4671 char *delim
= strchr(curr
, ',');
4682 (void) nvlist_add_boolean(nvl
, curr
);
4688 for (i
= 0; i
< 2; i
++) {
4689 char locality
= ld
[i
];
4693 if (!nvlist_empty(base_nvl
)) {
4695 (void) snprintf(who_buf
,
4696 sizeof (who_buf
), "%c%c$%s",
4697 base_type
, locality
, who
);
4699 (void) snprintf(who_buf
,
4700 sizeof (who_buf
), "%c%c$",
4701 base_type
, locality
);
4703 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
4708 if (!nvlist_empty(set_nvl
)) {
4710 (void) snprintf(who_buf
,
4711 sizeof (who_buf
), "%c%c$%s",
4712 set_type
, locality
, who
);
4714 (void) snprintf(who_buf
,
4715 sizeof (who_buf
), "%c%c$",
4716 set_type
, locality
);
4718 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
4723 for (i
= 0; i
< 2; i
++) {
4724 char locality
= ld
[i
];
4729 (void) snprintf(who_buf
, sizeof (who_buf
),
4730 "%c%c$%s", base_type
, locality
, who
);
4732 (void) snprintf(who_buf
, sizeof (who_buf
),
4733 "%c%c$", base_type
, locality
);
4734 (void) nvlist_add_boolean(top_nvl
, who_buf
);
4737 (void) snprintf(who_buf
, sizeof (who_buf
),
4738 "%c%c$%s", set_type
, locality
, who
);
4740 (void) snprintf(who_buf
, sizeof (who_buf
),
4741 "%c%c$", set_type
, locality
);
4742 (void) nvlist_add_boolean(top_nvl
, who_buf
);
4748 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
4750 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
4754 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
4755 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
4756 } else if (opts
->create
) {
4757 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
4758 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
4759 } else if (opts
->everyone
) {
4760 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
4761 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
4763 char *curr
= opts
->who
;
4764 char *end
= curr
+ strlen(curr
);
4766 while (curr
< end
) {
4768 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
4770 char *delim
= strchr(curr
, ',');
4773 struct passwd
*p
= NULL
;
4774 struct group
*g
= NULL
;
4782 rid
= (uid_t
)strtol(curr
, &endch
, 0);
4784 who_type
= ZFS_DELEG_USER
;
4793 (void) snprintf(errbuf
, 256, gettext(
4794 "invalid user %s"), curr
);
4795 allow_usage(un
, B_TRUE
, errbuf
);
4797 } else if (opts
->group
) {
4798 who_type
= ZFS_DELEG_GROUP
;
4807 (void) snprintf(errbuf
, 256, gettext(
4808 "invalid group %s"), curr
);
4809 allow_usage(un
, B_TRUE
, errbuf
);
4812 if (*endch
!= '\0') {
4819 if (*endch
!= '\0') {
4827 who_type
= ZFS_DELEG_USER
;
4829 } else if (g
!= NULL
) {
4830 who_type
= ZFS_DELEG_GROUP
;
4833 (void) snprintf(errbuf
, 256, gettext(
4834 "invalid user/group %s"), curr
);
4835 allow_usage(un
, B_TRUE
, errbuf
);
4839 (void) sprintf(id
, "%u", rid
);
4842 store_allow_perm(who_type
, opts
->local
,
4843 opts
->descend
, who
, opts
->perms
, *nvlp
);
4852 print_set_creat_perms(uu_avl_t
*who_avl
)
4854 const char *sc_title
[] = {
4855 gettext("Permission sets:\n"),
4856 gettext("Create time permissions:\n"),
4859 const char **title_ptr
= sc_title
;
4860 who_perm_node_t
*who_node
= NULL
;
4861 int prev_weight
= -1;
4863 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
4864 who_node
= uu_avl_next(who_avl
, who_node
)) {
4865 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
4866 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
4867 const char *who_name
= who_node
->who_perm
.who_name
;
4868 int weight
= who_type2weight(who_type
);
4869 boolean_t first
= B_TRUE
;
4870 deleg_perm_node_t
*deleg_node
;
4872 if (prev_weight
!= weight
) {
4873 (void) printf("%s", *title_ptr
++);
4874 prev_weight
= weight
;
4877 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
4878 (void) printf("\t");
4880 (void) printf("\t%s ", who_name
);
4882 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
4883 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
4886 deleg_node
->dpn_perm
.dp_name
);
4889 (void) printf(",%s",
4890 deleg_node
->dpn_perm
.dp_name
);
4893 (void) printf("\n");
4898 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
4901 who_perm_node_t
*who_node
= NULL
;
4902 boolean_t prt_title
= B_TRUE
;
4903 uu_avl_walk_t
*walk
;
4905 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
4908 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
4909 const char *who_name
= who_node
->who_perm
.who_name
;
4910 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
4911 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
4912 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
4914 deleg_perm_node_t
*deleg_node
;
4915 boolean_t prt_who
= B_TRUE
;
4917 for (deleg_node
= uu_avl_first(avl
);
4919 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
4920 if (local
!= deleg_node
->dpn_perm
.dp_local
||
4921 descend
!= deleg_node
->dpn_perm
.dp_descend
)
4925 const char *who
= NULL
;
4927 prt_title
= B_FALSE
;
4928 (void) printf("%s", title
);
4932 case ZFS_DELEG_USER_SETS
:
4933 case ZFS_DELEG_USER
:
4934 who
= gettext("user");
4936 who_name
= nice_who_name
;
4938 case ZFS_DELEG_GROUP_SETS
:
4939 case ZFS_DELEG_GROUP
:
4940 who
= gettext("group");
4942 who_name
= nice_who_name
;
4944 case ZFS_DELEG_EVERYONE_SETS
:
4945 case ZFS_DELEG_EVERYONE
:
4946 who
= gettext("everyone");
4953 if (who_name
== NULL
)
4954 (void) printf("\t%s", who
);
4956 (void) printf("\t%s %s", who
, who_name
);
4959 (void) printf("%c%s", delim
,
4960 deleg_node
->dpn_perm
.dp_name
);
4965 (void) printf("\n");
4968 uu_avl_walk_end(walk
);
4972 print_fs_perms(fs_perm_set_t
*fspset
)
4974 fs_perm_node_t
*node
= NULL
;
4975 char buf
[ZFS_MAXNAMELEN
+32];
4976 const char *dsname
= buf
;
4978 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
4979 node
= uu_list_next(fspset
->fsps_list
, node
)) {
4980 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
4981 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
4984 (void) snprintf(buf
, ZFS_MAXNAMELEN
+32,
4985 gettext("---- Permissions on %s "),
4986 node
->fspn_fsperm
.fsp_name
);
4987 (void) printf("%s", dsname
);
4988 left
= 70 - strlen(buf
);
4991 (void) printf("\n");
4993 print_set_creat_perms(sc_avl
);
4994 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
4995 gettext("Local permissions:\n"));
4996 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
4997 gettext("Descendent permissions:\n"));
4998 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
4999 gettext("Local+Descendent permissions:\n"));
5003 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5005 struct deleg_perms
{
5011 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5013 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5014 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5016 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5019 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5023 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5026 nvlist_t
*perm_nvl
= NULL
;
5027 nvlist_t
*update_perm_nvl
= NULL
;
5030 struct allow_opts opts
= { 0 };
5032 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5035 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5038 opts
.local
= B_TRUE
;
5041 opts
.descend
= B_TRUE
;
5047 opts
.group
= B_TRUE
;
5050 opts
.everyone
= B_TRUE
;
5056 opts
.create
= B_TRUE
;
5059 opts
.recursive
= B_TRUE
;
5062 (void) fprintf(stderr
, gettext("missing argument for "
5063 "'%c' option\n"), optopt
);
5067 opts
.prt_usage
= B_TRUE
;
5070 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5079 /* check arguments */
5080 parse_allow_args(argc
, argv
, un
, &opts
);
5082 /* try to open the dataset */
5083 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5084 ZFS_TYPE_VOLUME
)) == NULL
) {
5085 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5090 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5093 fs_perm_set_init(&fs_perm_set
);
5094 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5095 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5100 print_fs_perms(&fs_perm_set
);
5102 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5103 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5106 if (un
&& opts
.recursive
) {
5107 struct deleg_perms data
= { un
, update_perm_nvl
};
5108 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5117 nvlist_free(perm_nvl
);
5118 if (update_perm_nvl
!= NULL
)
5119 nvlist_free(update_perm_nvl
);
5121 fs_perm_set_fini(&fs_perm_set
);
5129 zfs_do_allow(int argc
, char **argv
)
5131 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5135 zfs_do_unallow(int argc
, char **argv
)
5137 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5141 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5146 boolean_t recursive
= B_FALSE
;
5147 const char *opts
= holding
? "rt" : "r";
5151 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5157 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5166 /* check number of arguments */
5174 if (holding
&& tag
[0] == '.') {
5175 /* tags starting with '.' are reserved for libzfs */
5176 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5180 for (i
= 0; i
< argc
; ++i
) {
5182 char parent
[ZFS_MAXNAMELEN
];
5184 char *path
= argv
[i
];
5186 delim
= strchr(path
, '@');
5187 if (delim
== NULL
) {
5188 (void) fprintf(stderr
,
5189 gettext("'%s' is not a snapshot\n"), path
);
5193 (void) strncpy(parent
, path
, delim
- path
);
5194 parent
[delim
- path
] = '\0';
5196 zhp
= zfs_open(g_zfs
, parent
,
5197 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5203 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
5206 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5212 return (errors
!= 0);
5216 * zfs hold [-r] [-t] <tag> <snap> ...
5218 * -r Recursively hold
5220 * Apply a user-hold with the given tag to the list of snapshots.
5223 zfs_do_hold(int argc
, char **argv
)
5225 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5229 * zfs release [-r] <tag> <snap> ...
5231 * -r Recursively release
5233 * Release a user-hold with the given tag from the list of snapshots.
5236 zfs_do_release(int argc
, char **argv
)
5238 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5241 typedef struct holds_cbdata
{
5242 boolean_t cb_recursive
;
5243 const char *cb_snapname
;
5245 size_t cb_max_namelen
;
5246 size_t cb_max_taglen
;
5249 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5250 #define DATETIME_BUF_LEN (32)
5255 print_holds(boolean_t scripted
, int nwidth
, int tagwidth
, nvlist_t
*nvl
)
5258 nvpair_t
*nvp
= NULL
;
5259 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5263 for (i
= 0; i
< 3; i
++) {
5264 col
= gettext(hdr_cols
[i
]);
5266 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5269 (void) printf("%s\n", col
);
5273 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5274 char *zname
= nvpair_name(nvp
);
5276 nvpair_t
*nvp2
= NULL
;
5277 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5278 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5279 char tsbuf
[DATETIME_BUF_LEN
];
5280 char *tagname
= nvpair_name(nvp2
);
5284 char sep
= scripted
? '\t' : ' ';
5285 int sepnum
= scripted
? 1 : 2;
5287 (void) nvpair_value_uint64(nvp2
, &val
);
5289 (void) localtime_r(&time
, &t
);
5290 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5291 gettext(STRFTIME_FMT_STR
), &t
);
5293 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth
, zname
,
5294 sepnum
, sep
, tagwidth
, tagname
, sepnum
, sep
, tsbuf
);
5300 * Generic callback function to list a dataset or snapshot.
5303 holds_callback(zfs_handle_t
*zhp
, void *data
)
5305 holds_cbdata_t
*cbp
= data
;
5306 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5307 nvlist_t
*nvl
= NULL
;
5308 nvpair_t
*nvp
= NULL
;
5309 const char *zname
= zfs_get_name(zhp
);
5310 size_t znamelen
= strnlen(zname
, ZFS_MAXNAMELEN
);
5312 if (cbp
->cb_recursive
) {
5313 const char *snapname
;
5314 char *delim
= strchr(zname
, '@');
5318 snapname
= delim
+ 1;
5319 if (strcmp(cbp
->cb_snapname
, snapname
))
5323 if (zfs_get_holds(zhp
, &nvl
) != 0)
5326 if (znamelen
> cbp
->cb_max_namelen
)
5327 cbp
->cb_max_namelen
= znamelen
;
5329 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5330 const char *tag
= nvpair_name(nvp
);
5331 size_t taglen
= strnlen(tag
, MAXNAMELEN
);
5332 if (taglen
> cbp
->cb_max_taglen
)
5333 cbp
->cb_max_taglen
= taglen
;
5336 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5340 * zfs holds [-r] <snap> ...
5342 * -r Recursively hold
5345 zfs_do_holds(int argc
, char **argv
)
5350 boolean_t scripted
= B_FALSE
;
5351 boolean_t recursive
= B_FALSE
;
5352 const char *opts
= "rH";
5355 int types
= ZFS_TYPE_SNAPSHOT
;
5356 holds_cbdata_t cb
= { 0 };
5363 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5372 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5379 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
5380 flags
|= ZFS_ITER_RECURSE
;
5386 /* check number of arguments */
5390 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5393 for (i
= 0; i
< argc
; ++i
) {
5394 char *snapshot
= argv
[i
];
5396 const char *snapname
;
5398 delim
= strchr(snapshot
, '@');
5399 if (delim
== NULL
) {
5400 (void) fprintf(stderr
,
5401 gettext("'%s' is not a snapshot\n"), snapshot
);
5405 snapname
= delim
+ 1;
5407 snapshot
[delim
- snapshot
] = '\0';
5409 cb
.cb_recursive
= recursive
;
5410 cb
.cb_snapname
= snapname
;
5414 * 1. collect holds data, set format options
5416 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
5417 holds_callback
, &cb
);
5423 * 2. print holds data
5425 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
5427 if (nvlist_empty(nvl
))
5428 (void) fprintf(stderr
, gettext("no datasets available\n"));
5432 return (0 != errors
);
5435 #define CHECK_SPINNER 30
5436 #define SPINNER_TIME 3 /* seconds */
5437 #define MOUNT_TIME 5 /* seconds */
5440 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
5442 static char *spin
[] = { "-", "\\", "|", "/" };
5443 static int spinval
= 0;
5444 static int spincheck
= 0;
5445 static time_t last_spin_time
= (time_t)0;
5446 get_all_cb_t
*cbp
= data
;
5447 zfs_type_t type
= zfs_get_type(zhp
);
5449 if (cbp
->cb_verbose
) {
5450 if (--spincheck
< 0) {
5451 time_t now
= time(NULL
);
5452 if (last_spin_time
+ SPINNER_TIME
< now
) {
5453 update_progress(spin
[spinval
++ % 4]);
5454 last_spin_time
= now
;
5456 spincheck
= CHECK_SPINNER
;
5461 * Iterate over any nested datasets.
5463 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
5469 * Skip any datasets whose type does not match.
5471 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
5475 libzfs_add_handle(cbp
, zhp
);
5476 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
5482 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
5484 get_all_cb_t cb
= { 0 };
5485 cb
.cb_verbose
= verbose
;
5486 cb
.cb_getone
= get_one_dataset
;
5489 set_progress_header(gettext("Reading ZFS config"));
5490 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
5492 *dslist
= cb
.cb_handles
;
5493 *count
= cb
.cb_used
;
5496 finish_progress(gettext("done."));
5500 * Generic callback for sharing or mounting filesystems. Because the code is so
5501 * similar, we have a common function with an extra parameter to determine which
5502 * mode we are using.
5504 #define OP_SHARE 0x1
5505 #define OP_MOUNT 0x2
5508 * Share or mount a dataset.
5511 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
5512 boolean_t
explicit, const char *options
)
5514 char mountpoint
[ZFS_MAXPROPLEN
];
5515 char shareopts
[ZFS_MAXPROPLEN
];
5516 char smbshareopts
[ZFS_MAXPROPLEN
];
5517 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
5519 uint64_t zoned
, canmount
;
5520 boolean_t shared_nfs
, shared_smb
;
5522 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
5525 * Check to make sure we can mount/share this dataset. If we
5526 * are in the global zone and the filesystem is exported to a
5527 * local zone, or if we are in a local zone and the
5528 * filesystem is not exported, then it is an error.
5530 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
5532 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
5536 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5537 "dataset is exported to a local zone\n"), cmdname
,
5541 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
5545 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5546 "permission denied\n"), cmdname
,
5552 * Ignore any filesystems which don't apply to us. This
5553 * includes those with a legacy mountpoint, or those with
5554 * legacy share options.
5556 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
5557 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
5558 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
5559 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5560 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
5561 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5563 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
5564 strcmp(smbshareopts
, "off") == 0) {
5568 (void) fprintf(stderr
, gettext("cannot share '%s': "
5569 "legacy share\n"), zfs_get_name(zhp
));
5570 (void) fprintf(stderr
, gettext("use share(1M) to "
5571 "share this filesystem, or set "
5572 "sharenfs property on\n"));
5577 * We cannot share or mount legacy filesystems. If the
5578 * shareopts is non-legacy but the mountpoint is legacy, we
5579 * treat it as a legacy share.
5581 if (strcmp(mountpoint
, "legacy") == 0) {
5585 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5586 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
5587 (void) fprintf(stderr
, gettext("use %s(1M) to "
5588 "%s this filesystem\n"), cmdname
, cmdname
);
5592 if (strcmp(mountpoint
, "none") == 0) {
5596 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
5597 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
5602 * canmount explicit outcome
5603 * on no pass through
5604 * on yes pass through
5606 * off yes display error, return 1
5607 * noauto no return 0
5608 * noauto yes pass through
5610 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
5611 if (canmount
== ZFS_CANMOUNT_OFF
) {
5615 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5616 "'canmount' property is set to 'off'\n"), cmdname
,
5619 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
5624 * At this point, we have verified that the mountpoint and/or
5625 * shareopts are appropriate for auto management. If the
5626 * filesystem is already mounted or shared, return (failing
5627 * for explicit requests); otherwise mount or share the
5633 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
5634 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
5636 if ((shared_nfs
&& shared_smb
) ||
5637 ((shared_nfs
&& strcmp(shareopts
, "on") == 0) &&
5638 (strcmp(smbshareopts
, "off") == 0)) ||
5639 ((shared_smb
&& strcmp(smbshareopts
, "on") == 0) &&
5640 (strcmp(shareopts
, "off") == 0))) {
5644 (void) fprintf(stderr
, gettext("cannot share "
5645 "'%s': filesystem already shared\n"),
5650 if (!zfs_is_mounted(zhp
, NULL
) &&
5651 zfs_mount(zhp
, NULL
, 0) != 0)
5654 if (protocol
== NULL
) {
5655 if (zfs_shareall(zhp
) != 0)
5657 } else if (strcmp(protocol
, "nfs") == 0) {
5658 if (zfs_share_nfs(zhp
))
5660 } else if (strcmp(protocol
, "smb") == 0) {
5661 if (zfs_share_smb(zhp
))
5664 (void) fprintf(stderr
, gettext("cannot share "
5665 "'%s': invalid share type '%s' "
5667 zfs_get_name(zhp
), protocol
);
5674 if (options
== NULL
)
5675 mnt
.mnt_mntopts
= "";
5677 mnt
.mnt_mntopts
= (char *)options
;
5679 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
5680 zfs_is_mounted(zhp
, NULL
)) {
5684 (void) fprintf(stderr
, gettext("cannot mount "
5685 "'%s': filesystem already mounted\n"),
5690 if (zfs_mount(zhp
, options
, flags
) != 0)
5699 * Reports progress in the form "(current/total)". Not thread-safe.
5702 report_mount_progress(int current
, int total
)
5704 static time_t last_progress_time
= 0;
5705 time_t now
= time(NULL
);
5708 /* report 1..n instead of 0..n-1 */
5711 /* display header if we're here for the first time */
5713 set_progress_header(gettext("Mounting ZFS filesystems"));
5714 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
5715 /* too soon to report again */
5719 last_progress_time
= now
;
5721 (void) sprintf(info
, "(%d/%d)", current
, total
);
5723 if (current
== total
)
5724 finish_progress(info
);
5726 update_progress(info
);
5730 append_options(char *mntopts
, char *newopts
)
5732 int len
= strlen(mntopts
);
5734 /* original length plus new string to append plus 1 for the comma */
5735 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
5736 (void) fprintf(stderr
, gettext("the opts argument for "
5737 "'%s' option is too long (more than %d chars)\n"),
5738 "-o", MNT_LINE_MAX
);
5743 mntopts
[len
++] = ',';
5745 (void) strcpy(&mntopts
[len
], newopts
);
5749 share_mount(int op
, int argc
, char **argv
)
5752 boolean_t verbose
= B_FALSE
;
5754 char *options
= NULL
;
5758 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
5768 if (*optarg
== '\0') {
5769 (void) fprintf(stderr
, gettext("empty mount "
5770 "options (-o) specified\n"));
5774 if (options
== NULL
)
5775 options
= safe_malloc(MNT_LINE_MAX
+ 1);
5777 /* option validation is done later */
5778 append_options(options
, optarg
);
5781 flags
|= MS_OVERLAY
;
5784 (void) fprintf(stderr
, gettext("missing argument for "
5785 "'%c' option\n"), optopt
);
5789 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5798 /* check number of arguments */
5800 zfs_handle_t
**dslist
= NULL
;
5801 size_t i
, count
= 0;
5802 char *protocol
= NULL
;
5804 if (op
== OP_SHARE
&& argc
> 0) {
5805 if (strcmp(argv
[0], "nfs") != 0 &&
5806 strcmp(argv
[0], "smb") != 0) {
5807 (void) fprintf(stderr
, gettext("share type "
5808 "must be 'nfs' or 'smb'\n"));
5817 (void) fprintf(stderr
, gettext("too many arguments\n"));
5821 start_progress_timer();
5822 get_all_datasets(&dslist
, &count
, verbose
);
5827 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
5829 for (i
= 0; i
< count
; i
++) {
5831 report_mount_progress(i
, count
);
5833 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
5834 B_FALSE
, options
) != 0)
5836 zfs_close(dslist
[i
]);
5840 } else if (argc
== 0) {
5841 struct mnttab entry
;
5843 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
5844 (void) fprintf(stderr
, gettext("missing filesystem "
5845 "argument (specify -a for all)\n"));
5850 * When mount is given no arguments, go through /etc/mtab and
5851 * display any active ZFS mounts. We hide any snapshots, since
5852 * they are controlled automatically.
5854 rewind(mnttab_file
);
5855 while (getmntent(mnttab_file
, &entry
) == 0) {
5856 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
5857 strchr(entry
.mnt_special
, '@') != NULL
)
5860 (void) printf("%-30s %s\n", entry
.mnt_special
,
5868 (void) fprintf(stderr
,
5869 gettext("too many arguments\n"));
5873 if ((zhp
= zfs_open(g_zfs
, argv
[0],
5874 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
5877 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
5887 * zfs mount -a [nfs]
5888 * zfs mount filesystem
5890 * Mount all filesystems, or mount the given filesystem.
5893 zfs_do_mount(int argc
, char **argv
)
5895 return (share_mount(OP_MOUNT
, argc
, argv
));
5899 * zfs share -a [nfs | smb]
5900 * zfs share filesystem
5902 * Share all filesystems, or share the given filesystem.
5905 zfs_do_share(int argc
, char **argv
)
5907 return (share_mount(OP_SHARE
, argc
, argv
));
5910 typedef struct unshare_unmount_node
{
5911 zfs_handle_t
*un_zhp
;
5913 uu_avl_node_t un_avlnode
;
5914 } unshare_unmount_node_t
;
5918 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
5920 const unshare_unmount_node_t
*l
= larg
;
5921 const unshare_unmount_node_t
*r
= rarg
;
5923 return (strcmp(l
->un_mountp
, r
->un_mountp
));
5927 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
5928 * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
5929 * and unmount it appropriately.
5932 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
5936 struct stat64 statbuf
;
5937 struct extmnttab entry
;
5938 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
5942 * Search for the path in /etc/mtab. Rather than looking for the
5943 * specific path, which can be fooled by non-standard paths (i.e. ".."
5944 * or "//"), we stat() the path and search for the corresponding
5945 * (major,minor) device pair.
5947 if (stat64(path
, &statbuf
) != 0) {
5948 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
5949 cmdname
, path
, strerror(errno
));
5952 path_inode
= statbuf
.st_ino
;
5955 * Search for the given (major,minor) pair in the mount table.
5957 rewind(mnttab_file
);
5958 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
5959 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
5960 entry
.mnt_minor
== minor(statbuf
.st_dev
))
5964 if (op
== OP_SHARE
) {
5965 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
5966 "currently mounted\n"), cmdname
, path
);
5969 (void) fprintf(stderr
, gettext("warning: %s not in mtab\n"),
5971 if ((ret
= umount2(path
, flags
)) != 0)
5972 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
5977 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
5978 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
5979 "filesystem\n"), cmdname
, path
);
5983 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
5984 ZFS_TYPE_FILESYSTEM
)) == NULL
)
5988 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
5989 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
5990 cmdname
, path
, strerror(errno
));
5992 } else if (statbuf
.st_ino
!= path_inode
) {
5993 (void) fprintf(stderr
, gettext("cannot "
5994 "%s '%s': not a mountpoint\n"), cmdname
, path
);
5998 if (op
== OP_SHARE
) {
5999 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6000 char smbshare_prop
[ZFS_MAXPROPLEN
];
6002 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
6003 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6004 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
6005 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6007 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6008 strcmp(smbshare_prop
, "off") == 0) {
6009 (void) fprintf(stderr
, gettext("cannot unshare "
6010 "'%s': legacy share\n"), path
);
6011 (void) fprintf(stderr
, gettext("use exportfs(8) "
6012 "or smbcontrol(1) to unshare this filesystem\n"));
6013 } else if (!zfs_is_shared(zhp
)) {
6014 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6015 "not currently shared\n"), path
);
6017 ret
= zfs_unshareall_bypath(zhp
, path
);
6020 char mtpt_prop
[ZFS_MAXPROPLEN
];
6022 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6023 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6026 ret
= zfs_unmount(zhp
, NULL
, flags
);
6027 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6028 (void) fprintf(stderr
, gettext("cannot unmount "
6029 "'%s': legacy mountpoint\n"),
6031 (void) fprintf(stderr
, gettext("use umount(8) "
6032 "to unmount this filesystem\n"));
6034 ret
= zfs_unmountall(zhp
, flags
);
6045 * Generic callback for unsharing or unmounting a filesystem.
6048 unshare_unmount(int op
, int argc
, char **argv
)
6055 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6056 char sharesmb
[ZFS_MAXPROPLEN
];
6059 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
6068 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6079 * We could make use of zfs_for_each() to walk all datasets in
6080 * the system, but this would be very inefficient, especially
6081 * since we would have to linearly search /etc/mtab for each
6082 * one. Instead, do one pass through /etc/mtab looking for
6083 * zfs entries and call zfs_unmount() for each one.
6085 * Things get a little tricky if the administrator has created
6086 * mountpoints beneath other ZFS filesystems. In this case, we
6087 * have to unmount the deepest filesystems first. To accomplish
6088 * this, we place all the mountpoints in an AVL tree sorted by
6089 * the special type (dataset name), and walk the result in
6090 * reverse to make sure to get any snapshots first.
6092 struct mnttab entry
;
6093 uu_avl_pool_t
*pool
;
6094 uu_avl_t
*tree
= NULL
;
6095 unshare_unmount_node_t
*node
;
6097 uu_avl_walk_t
*walk
;
6100 (void) fprintf(stderr
, gettext("too many arguments\n"));
6104 if (((pool
= uu_avl_pool_create("unmount_pool",
6105 sizeof (unshare_unmount_node_t
),
6106 offsetof(unshare_unmount_node_t
, un_avlnode
),
6107 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6108 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6111 rewind(mnttab_file
);
6112 while (getmntent(mnttab_file
, &entry
) == 0) {
6114 /* ignore non-ZFS entries */
6115 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6118 /* ignore snapshots */
6119 if (strchr(entry
.mnt_special
, '@') != NULL
)
6122 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6123 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6130 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6132 sizeof (nfs_mnt_prop
),
6133 NULL
, NULL
, 0, B_FALSE
) == 0);
6134 if (strcmp(nfs_mnt_prop
, "off") != 0)
6136 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6138 sizeof (nfs_mnt_prop
),
6139 NULL
, NULL
, 0, B_FALSE
) == 0);
6140 if (strcmp(nfs_mnt_prop
, "off") == 0)
6144 /* Ignore legacy mounts */
6145 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6147 sizeof (nfs_mnt_prop
),
6148 NULL
, NULL
, 0, B_FALSE
) == 0);
6149 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6151 /* Ignore canmount=noauto mounts */
6152 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6153 ZFS_CANMOUNT_NOAUTO
)
6159 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6161 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6163 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6165 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6166 uu_avl_insert(tree
, node
, idx
);
6168 zfs_close(node
->un_zhp
);
6169 free(node
->un_mountp
);
6175 * Walk the AVL tree in reverse, unmounting each filesystem and
6176 * removing it from the AVL tree in the process.
6178 if ((walk
= uu_avl_walk_start(tree
,
6179 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6182 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6183 uu_avl_remove(tree
, node
);
6187 if (zfs_unshareall_bypath(node
->un_zhp
,
6188 node
->un_mountp
) != 0)
6193 if (zfs_unmount(node
->un_zhp
,
6194 node
->un_mountp
, flags
) != 0)
6199 zfs_close(node
->un_zhp
);
6200 free(node
->un_mountp
);
6204 uu_avl_walk_end(walk
);
6205 uu_avl_destroy(tree
);
6206 uu_avl_pool_destroy(pool
);
6211 (void) fprintf(stderr
,
6212 gettext("missing filesystem argument\n"));
6214 (void) fprintf(stderr
,
6215 gettext("too many arguments\n"));
6220 * We have an argument, but it may be a full path or a ZFS
6221 * filesystem. Pass full paths off to unmount_path() (shared by
6222 * manual_unmount), otherwise open the filesystem and pass to
6225 if (argv
[0][0] == '/')
6226 return (unshare_unmount_path(op
, argv
[0],
6229 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6230 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6233 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
6234 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
6235 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
6236 NULL
, 0, B_FALSE
) == 0);
6240 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6242 sizeof (nfs_mnt_prop
),
6243 NULL
, NULL
, 0, B_FALSE
) == 0);
6244 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6245 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
6248 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6249 strcmp(sharesmb
, "off") == 0) {
6250 (void) fprintf(stderr
, gettext("cannot "
6251 "unshare '%s': legacy share\n"),
6253 (void) fprintf(stderr
, gettext("use "
6254 "unshare(1M) to unshare this "
6257 } else if (!zfs_is_shared(zhp
)) {
6258 (void) fprintf(stderr
, gettext("cannot "
6259 "unshare '%s': not currently "
6260 "shared\n"), zfs_get_name(zhp
));
6262 } else if (zfs_unshareall(zhp
) != 0) {
6268 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
6269 (void) fprintf(stderr
, gettext("cannot "
6270 "unmount '%s': legacy "
6271 "mountpoint\n"), zfs_get_name(zhp
));
6272 (void) fprintf(stderr
, gettext("use "
6273 "umount(1M) to unmount this "
6276 } else if (!zfs_is_mounted(zhp
, NULL
)) {
6277 (void) fprintf(stderr
, gettext("cannot "
6278 "unmount '%s': not currently "
6282 } else if (zfs_unmountall(zhp
, flags
) != 0) {
6296 * zfs unmount filesystem
6298 * Unmount all filesystems, or a specific ZFS filesystem.
6301 zfs_do_unmount(int argc
, char **argv
)
6303 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
6308 * zfs unshare filesystem
6310 * Unshare all filesystems, or a specific ZFS filesystem.
6313 zfs_do_unshare(int argc
, char **argv
)
6315 return (unshare_unmount(OP_SHARE
, argc
, argv
));
6319 find_command_idx(char *command
, int *idx
)
6323 for (i
= 0; i
< NCOMMAND
; i
++) {
6324 if (command_table
[i
].name
== NULL
)
6327 if (strcmp(command
, command_table
[i
].name
) == 0) {
6336 zfs_do_diff(int argc
, char **argv
)
6340 char *tosnap
= NULL
;
6341 char *fromsnap
= NULL
;
6346 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
6349 flags
|= ZFS_DIFF_CLASSIFY
;
6352 flags
|= ZFS_DIFF_PARSEABLE
;
6355 flags
|= ZFS_DIFF_TIMESTAMP
;
6358 (void) fprintf(stderr
,
6359 gettext("invalid option '%c'\n"), optopt
);
6368 (void) fprintf(stderr
,
6369 gettext("must provide at least one snapshot name\n"));
6374 (void) fprintf(stderr
, gettext("too many arguments\n"));
6379 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
6382 if (*fromsnap
!= '@')
6383 copy
= strdup(fromsnap
);
6385 copy
= strdup(tosnap
);
6389 if ((atp
= strchr(copy
, '@')))
6392 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6398 * Ignore SIGPIPE so that the library can give us
6399 * information on any failure
6401 (void) sigignore(SIGPIPE
);
6403 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
6411 main(int argc
, char **argv
)
6417 (void) setlocale(LC_ALL
, "");
6418 (void) textdomain(TEXT_DOMAIN
);
6423 * Make sure the user has specified some command.
6426 (void) fprintf(stderr
, gettext("missing command\n"));
6433 * The 'umount' command is an alias for 'unmount'
6435 if (strcmp(cmdname
, "umount") == 0)
6436 cmdname
= "unmount";
6439 * The 'recv' command is an alias for 'receive'
6441 if (strcmp(cmdname
, "recv") == 0)
6442 cmdname
= "receive";
6445 * The 'snap' command is an alias for 'snapshot'
6447 if (strcmp(cmdname
, "snap") == 0)
6448 cmdname
= "snapshot";
6453 if ((strcmp(cmdname
, "-?") == 0) ||
6454 (strcmp(cmdname
, "--help") == 0))
6457 if ((g_zfs
= libzfs_init()) == NULL
)
6460 mnttab_file
= g_zfs
->libzfs_mnttab
;
6462 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
6464 libzfs_print_on_error(g_zfs
, B_TRUE
);
6467 * Run the appropriate command.
6469 libzfs_mnttab_cache(g_zfs
, B_FALSE
);
6470 if (find_command_idx(cmdname
, &i
) == 0) {
6471 current_command
= &command_table
[i
];
6472 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
6473 } else if (strchr(cmdname
, '=') != NULL
) {
6474 verify(find_command_idx("set", &i
) == 0);
6475 current_command
= &command_table
[i
];
6476 ret
= command_table
[i
].func(argc
, argv
);
6478 (void) fprintf(stderr
, gettext("unrecognized "
6479 "command '%s'\n"), cmdname
);
6485 if (ret
== 0 && log_history
)
6486 (void) zpool_log_history(g_zfs
, history_str
);
6489 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6490 * for the purposes of running ::findleaks.
6492 if (getenv("ZFS_ABORT") != NULL
) {
6493 (void) printf("dumping core by request\n");