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.
34 #include <libnvpair.h>
47 #include <sys/mkdev.h>
48 #include <sys/mntent.h>
49 #include <sys/mnttab.h>
50 #include <sys/mount.h>
52 #include <sys/fs/zfs.h>
53 #include <sys/types.h>
58 #include <zfs_deleg.h>
62 #include <directory.h>
63 #endif /* HAVE_IDMAP */
67 #include "zfs_comutil.h"
69 libzfs_handle_t
*g_zfs
;
71 static FILE *mnttab_file
;
72 static char history_str
[HIS_MAX_RECORD_LEN
];
74 static int zfs_do_clone(int argc
, char **argv
);
75 static int zfs_do_create(int argc
, char **argv
);
76 static int zfs_do_destroy(int argc
, char **argv
);
77 static int zfs_do_get(int argc
, char **argv
);
78 static int zfs_do_inherit(int argc
, char **argv
);
79 static int zfs_do_list(int argc
, char **argv
);
80 static int zfs_do_mount(int argc
, char **argv
);
81 static int zfs_do_rename(int argc
, char **argv
);
82 static int zfs_do_rollback(int argc
, char **argv
);
83 static int zfs_do_set(int argc
, char **argv
);
84 static int zfs_do_upgrade(int argc
, char **argv
);
85 static int zfs_do_snapshot(int argc
, char **argv
);
86 static int zfs_do_unmount(int argc
, char **argv
);
87 static int zfs_do_share(int argc
, char **argv
);
88 static int zfs_do_unshare(int argc
, char **argv
);
89 static int zfs_do_send(int argc
, char **argv
);
90 static int zfs_do_receive(int argc
, char **argv
);
91 static int zfs_do_promote(int argc
, char **argv
);
92 static int zfs_do_userspace(int argc
, char **argv
);
93 static int zfs_do_allow(int argc
, char **argv
);
94 static int zfs_do_unallow(int argc
, char **argv
);
95 static int zfs_do_hold(int argc
, char **argv
);
96 static int zfs_do_holds(int argc
, char **argv
);
97 static int zfs_do_release(int argc
, char **argv
);
98 static int zfs_do_diff(int argc
, char **argv
);
101 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
106 _umem_debug_init(void)
108 return ("default,verbose"); /* $UMEM_DEBUG setting */
112 _umem_logging_init(void)
114 return ("fail,contents"); /* $UMEM_LOGGING setting */
147 typedef struct zfs_command
{
149 int (*func
)(int argc
, char **argv
);
154 * Master command table. Each ZFS command has a name, associated function, and
155 * usage message. The usage messages need to be internationalized, so we have
156 * to have a function to return the usage message based on a command index.
158 * These commands are organized according to how they are displayed in the usage
159 * message. An empty command (one with a NULL name) indicates an empty line in
160 * the generic usage message.
162 static zfs_command_t command_table
[] = {
163 { "create", zfs_do_create
, HELP_CREATE
},
164 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
166 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
167 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
168 { "clone", zfs_do_clone
, HELP_CLONE
},
169 { "promote", zfs_do_promote
, HELP_PROMOTE
},
170 { "rename", zfs_do_rename
, HELP_RENAME
},
172 { "list", zfs_do_list
, HELP_LIST
},
174 { "set", zfs_do_set
, HELP_SET
},
175 { "get", zfs_do_get
, HELP_GET
},
176 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
177 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
178 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
179 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
181 { "mount", zfs_do_mount
, HELP_MOUNT
},
182 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
183 { "share", zfs_do_share
, HELP_SHARE
},
184 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
186 { "send", zfs_do_send
, HELP_SEND
},
187 { "receive", zfs_do_receive
, HELP_RECEIVE
},
189 { "allow", zfs_do_allow
, HELP_ALLOW
},
191 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
193 { "hold", zfs_do_hold
, HELP_HOLD
},
194 { "holds", zfs_do_holds
, HELP_HOLDS
},
195 { "release", zfs_do_release
, HELP_RELEASE
},
196 { "diff", zfs_do_diff
, HELP_DIFF
},
199 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
201 zfs_command_t
*current_command
;
204 get_usage(zfs_help_t idx
)
208 return (gettext("\tclone [-p] [-o property=value] ... "
209 "<snapshot> <filesystem|volume>\n"));
211 return (gettext("\tcreate [-p] [-o property=value] ... "
213 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
214 "-V <size> <volume>\n"));
216 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
217 "\tdestroy [-dnpRrv] "
218 "<filesystem|volume>@<snap>[%<snap>][,...]\n"));
220 return (gettext("\tget [-rHp] [-d max] "
221 "[-o \"all\" | field[,...]] [-t type[,...]] "
222 "[-s source[,...]]\n"
223 "\t <\"all\" | property[,...]> "
224 "[filesystem|volume|snapshot] ...\n"));
226 return (gettext("\tinherit [-rS] <property> "
227 "<filesystem|volume|snapshot> ...\n"));
229 return (gettext("\tupgrade [-v]\n"
230 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
232 return (gettext("\tlist [-rH][-d max] "
233 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
234 "\t [-S property] ... "
235 "[filesystem|volume|snapshot|snap] ...\n"));
237 return (gettext("\tmount\n"
238 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
240 return (gettext("\tpromote <clone-filesystem>\n"));
242 return (gettext("\treceive [-vnFu] <filesystem|volume|"
244 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
246 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
247 "<filesystem|volume|snapshot>\n"
248 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
249 "\trename -r <snapshot> <snapshot>"));
251 return (gettext("\trollback [-rRf] <snapshot>\n"));
253 return (gettext("\tsend [-DnPpRrv] [-[iI] snapshot] "
256 return (gettext("\tset <property=value> "
257 "<filesystem|volume|snapshot> ...\n"));
259 return (gettext("\tshare <-a | filesystem>\n"));
261 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
262 "<filesystem@snapname|volume@snapname>\n"));
264 return (gettext("\tunmount [-f] "
265 "<-a | filesystem|mountpoint>\n"));
267 return (gettext("\tunshare "
268 "<-a | filesystem|mountpoint>\n"));
270 return (gettext("\tallow <filesystem|volume>\n"
272 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
273 "\t <filesystem|volume>\n"
274 "\tallow [-ld] -e <perm|@setname>[,...] "
275 "<filesystem|volume>\n"
276 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
277 "\tallow -s @setname <perm|@setname>[,...] "
278 "<filesystem|volume>\n"));
280 return (gettext("\tunallow [-rldug] "
281 "<\"everyone\"|user|group>[,...]\n"
282 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
283 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
284 "<filesystem|volume>\n"
285 "\tunallow [-r] -c [<perm|@setname>[,...]] "
286 "<filesystem|volume>\n"
287 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
288 "<filesystem|volume>\n"));
290 return (gettext("\tuserspace [-hniHp] [-o field[,...]] "
291 "[-sS field] ... [-t type[,...]]\n"
292 "\t <filesystem|snapshot>\n"));
293 case HELP_GROUPSPACE
:
294 return (gettext("\tgroupspace [-hniHpU] [-o field[,...]] "
295 "[-sS field] ... [-t type[,...]]\n"
296 "\t <filesystem|snapshot>\n"));
298 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
300 return (gettext("\tholds [-r] <snapshot> ...\n"));
302 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
304 return (gettext("\tdiff [-FHt] <snapshot> "
305 "[snapshot|filesystem]\n"));
315 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
320 * Utility function to guarantee malloc() success.
324 safe_malloc(size_t size
)
328 if ((data
= calloc(1, size
)) == NULL
)
335 safe_strdup(char *str
)
337 char *dupstr
= strdup(str
);
346 * Callback routine that will print out information for each of
350 usage_prop_cb(int prop
, void *cb
)
354 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
356 if (zfs_prop_readonly(prop
))
357 (void) fprintf(fp
, " NO ");
359 (void) fprintf(fp
, "YES ");
361 if (zfs_prop_inheritable(prop
))
362 (void) fprintf(fp
, " YES ");
364 (void) fprintf(fp
, " NO ");
366 if (zfs_prop_values(prop
) == NULL
)
367 (void) fprintf(fp
, "-\n");
369 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
375 * Display usage message. If we're inside a command, display only the usage for
376 * that command. Otherwise, iterate over the entire command table and display
377 * a complete usage message.
380 usage(boolean_t requested
)
383 boolean_t show_properties
= B_FALSE
;
384 FILE *fp
= requested
? stdout
: stderr
;
386 if (current_command
== NULL
) {
388 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
390 gettext("where 'command' is one of the following:\n\n"));
392 for (i
= 0; i
< NCOMMAND
; i
++) {
393 if (command_table
[i
].name
== NULL
)
394 (void) fprintf(fp
, "\n");
396 (void) fprintf(fp
, "%s",
397 get_usage(command_table
[i
].usage
));
400 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
401 "pool/[dataset/]*dataset[@name]\n"));
403 (void) fprintf(fp
, gettext("usage:\n"));
404 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
407 if (current_command
!= NULL
&&
408 (strcmp(current_command
->name
, "set") == 0 ||
409 strcmp(current_command
->name
, "get") == 0 ||
410 strcmp(current_command
->name
, "inherit") == 0 ||
411 strcmp(current_command
->name
, "list") == 0))
412 show_properties
= B_TRUE
;
414 if (show_properties
) {
416 gettext("\nThe following properties are supported:\n"));
418 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
419 "PROPERTY", "EDIT", "INHERIT", "VALUES");
421 /* Iterate over all properties */
422 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
425 (void) fprintf(fp
, "\t%-15s ", "userused@...");
426 (void) fprintf(fp
, " NO NO <size>\n");
427 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
428 (void) fprintf(fp
, " NO NO <size>\n");
429 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
430 (void) fprintf(fp
, "YES NO <size> | none\n");
431 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
432 (void) fprintf(fp
, "YES NO <size> | none\n");
433 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
434 (void) fprintf(fp
, " NO NO <size>\n");
436 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
437 "with standard units such as K, M, G, etc.\n"));
438 (void) fprintf(fp
, gettext("\nUser-defined properties can "
439 "be specified by using a name containing a colon (:).\n"));
440 (void) fprintf(fp
, gettext("\nThe {user|group}{used|quota}@ "
441 "properties must be appended with\n"
442 "a user or group specifier of one of these forms:\n"
443 " POSIX name (eg: \"matt\")\n"
444 " POSIX id (eg: \"126829\")\n"
445 " SMB name@domain (eg: \"matt@sun\")\n"
446 " SMB SID (eg: \"S-1-234-567-89\")\n"));
449 gettext("\nFor the property list, run: %s\n"),
452 gettext("\nFor the delegated permission list, run: %s\n"),
453 "zfs allow|unallow");
457 * See comments at end of main().
459 if (getenv("ZFS_ABORT") != NULL
) {
460 (void) printf("dumping core by request\n");
464 exit(requested
? 0 : 2);
468 parseprop(nvlist_t
*props
)
470 char *propname
= optarg
;
471 char *propval
, *strval
;
473 if ((propval
= strchr(propname
, '=')) == NULL
) {
474 (void) fprintf(stderr
, gettext("missing "
475 "'=' for -o option\n"));
480 if (nvlist_lookup_string(props
, propname
, &strval
) == 0) {
481 (void) fprintf(stderr
, gettext("property '%s' "
482 "specified multiple times\n"), propname
);
485 if (nvlist_add_string(props
, propname
, propval
) != 0)
491 parse_depth(char *opt
, int *flags
)
496 depth
= (int)strtol(opt
, &tmp
, 0);
498 (void) fprintf(stderr
,
499 gettext("%s is not an integer\n"), optarg
);
503 (void) fprintf(stderr
,
504 gettext("Depth can not be negative.\n"));
507 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
511 #define PROGRESS_DELAY 2 /* seconds */
513 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";
514 static time_t pt_begin
;
515 static char *pt_header
= NULL
;
516 static boolean_t pt_shown
;
519 start_progress_timer(void)
521 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
526 set_progress_header(char *header
)
528 assert(pt_header
== NULL
);
529 pt_header
= safe_strdup(header
);
531 (void) printf("%s: ", header
);
532 (void) fflush(stdout
);
537 update_progress(char *update
)
539 if (!pt_shown
&& time(NULL
) > pt_begin
) {
540 int len
= strlen(update
);
542 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
544 (void) fflush(stdout
);
546 } else if (pt_shown
) {
547 int len
= strlen(update
);
549 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
550 (void) fflush(stdout
);
555 finish_progress(char *done
)
558 (void) printf("%s\n", done
);
559 (void) fflush(stdout
);
566 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
568 * Given an existing dataset, create a writable copy whose initial contents
569 * are the same as the source. The newly created dataset maintains a
570 * dependency on the original; the original cannot be destroyed so long as
573 * The '-p' flag creates all the non-existing ancestors of the target first.
576 zfs_do_clone(int argc
, char **argv
)
578 zfs_handle_t
*zhp
= NULL
;
579 boolean_t parents
= B_FALSE
;
584 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
588 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
591 if (parseprop(props
))
598 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
607 /* check number of arguments */
609 (void) fprintf(stderr
, gettext("missing source dataset "
614 (void) fprintf(stderr
, gettext("missing target dataset "
619 (void) fprintf(stderr
, gettext("too many arguments\n"));
623 /* open the source dataset */
624 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
627 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
630 * Now create the ancestors of the target dataset. If the
631 * target already exists and '-p' option was used we should not
634 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
637 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
642 ret
= zfs_clone(zhp
, argv
[1], props
);
644 /* create the mountpoint if necessary */
648 clone
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
650 if (zfs_get_type(clone
) != ZFS_TYPE_VOLUME
)
651 if ((ret
= zfs_mount(clone
, NULL
, 0)) == 0)
652 ret
= zfs_share(clone
);
671 * zfs create [-p] [-o prop=value] ... fs
672 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
674 * Create a new dataset. This command can be used to create filesystems
675 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
676 * For volumes, the user must specify a size to be used.
678 * The '-s' flag applies only to volumes, and indicates that we should not try
679 * to set the reservation for this volume. By default we set a reservation
680 * equal to the size for any volume. For pools with SPA_VERSION >=
681 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
683 * The '-p' flag creates all the non-existing ancestors of the target first.
686 zfs_do_create(int argc
, char **argv
)
688 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
689 zfs_handle_t
*zhp
= NULL
;
690 uint64_t volsize
= 0;
692 boolean_t noreserve
= B_FALSE
;
693 boolean_t bflag
= B_FALSE
;
694 boolean_t parents
= B_FALSE
;
698 int canmount
= ZFS_CANMOUNT_OFF
;
700 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
704 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
707 type
= ZFS_TYPE_VOLUME
;
708 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
709 (void) fprintf(stderr
, gettext("bad volume "
710 "size '%s': %s\n"), optarg
,
711 libzfs_error_description(g_zfs
));
715 if (nvlist_add_uint64(props
,
716 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
725 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
726 (void) fprintf(stderr
, gettext("bad volume "
727 "block size '%s': %s\n"), optarg
,
728 libzfs_error_description(g_zfs
));
732 if (nvlist_add_uint64(props
,
733 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
738 if (parseprop(props
))
745 (void) fprintf(stderr
, gettext("missing size "
750 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
756 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
757 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
758 "used when creating a volume\n"));
765 /* check number of arguments */
767 (void) fprintf(stderr
, gettext("missing %s argument\n"),
768 zfs_type_to_name(type
));
772 (void) fprintf(stderr
, gettext("too many arguments\n"));
776 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
777 zpool_handle_t
*zpool_handle
;
778 uint64_t spa_version
;
780 zfs_prop_t resv_prop
;
783 if ((p
= strchr(argv
[0], '/')))
785 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
788 if (zpool_handle
== NULL
)
790 spa_version
= zpool_get_prop_int(zpool_handle
,
791 ZPOOL_PROP_VERSION
, NULL
);
792 zpool_close(zpool_handle
);
793 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
794 resv_prop
= ZFS_PROP_REFRESERVATION
;
796 resv_prop
= ZFS_PROP_RESERVATION
;
797 volsize
= zvol_volsize_to_reservation(volsize
, props
);
799 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
801 if (nvlist_add_uint64(props
,
802 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
809 if (parents
&& zfs_name_valid(argv
[0], type
)) {
811 * Now create the ancestors of target dataset. If the target
812 * already exists and '-p' option was used we should not
815 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
819 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
824 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
827 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
832 * if the user doesn't want the dataset automatically mounted,
833 * then skip the mount/share step
835 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
))
836 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
839 * Mount and/or share the new filesystem as appropriate. We provide a
840 * verbose error message to let the user know that their filesystem was
841 * in fact created, even if we failed to mount or share it.
843 if (canmount
== ZFS_CANMOUNT_ON
) {
844 if (zfs_mount(zhp
, NULL
, 0) != 0) {
845 (void) fprintf(stderr
, gettext("filesystem "
846 "successfully created, but not mounted\n"));
848 } else if (zfs_share(zhp
) != 0) {
849 (void) fprintf(stderr
, gettext("filesystem "
850 "successfully created, but not shared\n"));
867 * zfs destroy [-rRf] <fs, vol>
868 * zfs destroy [-rRd] <snap>
870 * -r Recursively destroy all children
871 * -R Recursively destroy all dependents, including clones
872 * -f Force unmounting of any dependents
873 * -d If we can't destroy now, mark for deferred destruction
875 * Destroys the given dataset. By default, it will unmount any filesystems,
876 * and refuse to destroy a dataset that has any dependents. A dependent can
877 * either be a child, or a clone of a child.
879 typedef struct destroy_cbdata
{
882 boolean_t cb_recurse
;
884 boolean_t cb_doclones
;
885 zfs_handle_t
*cb_target
;
886 boolean_t cb_defer_destroy
;
887 boolean_t cb_verbose
;
888 boolean_t cb_parsable
;
892 /* first snap in contiguous run */
893 zfs_handle_t
*cb_firstsnap
;
894 /* previous snap in contiguous run */
895 zfs_handle_t
*cb_prevsnap
;
901 * Check for any dependents based on the '-r' or '-R' flags.
904 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
906 destroy_cbdata_t
*cbp
= data
;
907 const char *tname
= zfs_get_name(cbp
->cb_target
);
908 const char *name
= zfs_get_name(zhp
);
910 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
911 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
913 * This is a direct descendant, not a clone somewhere else in
920 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
921 "%s has children\n"),
922 zfs_get_name(cbp
->cb_target
),
923 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
924 (void) fprintf(stderr
, gettext("use '-r' to destroy "
925 "the following datasets:\n"));
926 cbp
->cb_first
= B_FALSE
;
927 cbp
->cb_error
= B_TRUE
;
930 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
933 * This is a clone. We only want to report this if the '-r'
934 * wasn't specified, or the target is a snapshot.
936 if (!cbp
->cb_recurse
&&
937 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
941 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
942 "%s has dependent clones\n"),
943 zfs_get_name(cbp
->cb_target
),
944 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
945 (void) fprintf(stderr
, gettext("use '-R' to destroy "
946 "the following datasets:\n"));
947 cbp
->cb_first
= B_FALSE
;
948 cbp
->cb_error
= B_TRUE
;
949 cbp
->cb_dryrun
= B_TRUE
;
952 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
961 destroy_callback(zfs_handle_t
*zhp
, void *data
)
963 destroy_cbdata_t
*cb
= data
;
964 const char *name
= zfs_get_name(zhp
);
966 if (cb
->cb_verbose
) {
967 if (cb
->cb_parsable
) {
968 (void) printf("destroy\t%s\n", name
);
969 } else if (cb
->cb_dryrun
) {
970 (void) printf(gettext("would destroy %s\n"),
973 (void) printf(gettext("will destroy %s\n"),
979 * Ignore pools (which we've already flagged as an error before getting
982 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
983 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
988 if (!cb
->cb_dryrun
) {
989 if (zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
990 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1001 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1003 destroy_cbdata_t
*cb
= arg
;
1004 const char *name
= zfs_get_name(zhp
);
1007 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1008 if (cb
->cb_firstsnap
== NULL
)
1009 cb
->cb_firstsnap
= zfs_handle_dup(zhp
);
1010 if (cb
->cb_prevsnap
!= NULL
)
1011 zfs_close(cb
->cb_prevsnap
);
1012 /* this snap continues the current range */
1013 cb
->cb_prevsnap
= zfs_handle_dup(zhp
);
1014 if (cb
->cb_verbose
) {
1015 if (cb
->cb_parsable
) {
1016 (void) printf("destroy\t%s\n", name
);
1017 } else if (cb
->cb_dryrun
) {
1018 (void) printf(gettext("would destroy %s\n"),
1021 (void) printf(gettext("will destroy %s\n"),
1025 } else if (cb
->cb_firstsnap
!= NULL
) {
1026 /* end of this range */
1028 err
= zfs_get_snapused_int(cb
->cb_firstsnap
,
1029 cb
->cb_prevsnap
, &used
);
1030 cb
->cb_snapused
+= used
;
1031 zfs_close(cb
->cb_firstsnap
);
1032 cb
->cb_firstsnap
= NULL
;
1033 zfs_close(cb
->cb_prevsnap
);
1034 cb
->cb_prevsnap
= NULL
;
1041 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1044 assert(cb
->cb_firstsnap
== NULL
);
1045 assert(cb
->cb_prevsnap
== NULL
);
1046 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1047 if (cb
->cb_firstsnap
!= NULL
) {
1050 err
= zfs_get_snapused_int(cb
->cb_firstsnap
,
1051 cb
->cb_prevsnap
, &used
);
1053 cb
->cb_snapused
+= used
;
1054 zfs_close(cb
->cb_firstsnap
);
1055 cb
->cb_firstsnap
= NULL
;
1056 zfs_close(cb
->cb_prevsnap
);
1057 cb
->cb_prevsnap
= NULL
;
1063 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1065 destroy_cbdata_t
*cb
= arg
;
1068 /* Check for clones. */
1069 if (!cb
->cb_doclones
) {
1070 cb
->cb_target
= zhp
;
1071 cb
->cb_first
= B_TRUE
;
1072 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1073 destroy_check_dependent
, cb
);
1077 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1085 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1087 destroy_cbdata_t
*cb
= arg
;
1090 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1096 if (cb
->cb_verbose
) {
1097 err
= destroy_print_snapshots(zhp
, cb
);
1103 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1111 destroy_clones(destroy_cbdata_t
*cb
)
1114 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1116 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1117 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1120 boolean_t defer
= cb
->cb_defer_destroy
;
1124 * We can't defer destroy non-snapshots, so set it to
1125 * false while destroying the clones.
1127 cb
->cb_defer_destroy
= B_FALSE
;
1128 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1129 destroy_callback
, cb
);
1130 cb
->cb_defer_destroy
= defer
;
1140 zfs_do_destroy(int argc
, char **argv
)
1142 destroy_cbdata_t cb
= { 0 };
1146 zfs_type_t type
= ZFS_TYPE_DATASET
;
1149 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1152 cb
.cb_verbose
= B_TRUE
;
1155 cb
.cb_verbose
= B_TRUE
;
1156 cb
.cb_parsable
= B_TRUE
;
1159 cb
.cb_dryrun
= B_TRUE
;
1162 cb
.cb_defer_destroy
= B_TRUE
;
1163 type
= ZFS_TYPE_SNAPSHOT
;
1166 cb
.cb_force
= B_TRUE
;
1169 cb
.cb_recurse
= B_TRUE
;
1172 cb
.cb_recurse
= B_TRUE
;
1173 cb
.cb_doclones
= B_TRUE
;
1177 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1186 /* check number of arguments */
1188 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1192 (void) fprintf(stderr
, gettext("too many arguments\n"));
1196 at
= strchr(argv
[0], '@');
1200 /* Build the list of snaps to destroy in cb_nvl. */
1201 if (nvlist_alloc(&cb
.cb_nvl
, NV_UNIQUE_NAME
, 0) != 0)
1205 zhp
= zfs_open(g_zfs
, argv
[0],
1206 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1210 cb
.cb_snapspec
= at
+ 1;
1211 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1214 nvlist_free(cb
.cb_nvl
);
1218 if (nvlist_empty(cb
.cb_nvl
)) {
1219 (void) fprintf(stderr
, gettext("could not find any "
1220 "snapshots to destroy; check snapshot names.\n"));
1222 nvlist_free(cb
.cb_nvl
);
1226 if (cb
.cb_verbose
) {
1228 zfs_nicenum(cb
.cb_snapused
, buf
, sizeof (buf
));
1229 if (cb
.cb_parsable
) {
1230 (void) printf("reclaim\t%llu\n",
1231 (u_longlong_t
)cb
.cb_snapused
);
1232 } else if (cb
.cb_dryrun
) {
1233 (void) printf(gettext("would reclaim %s\n"),
1236 (void) printf(gettext("will reclaim %s\n"),
1241 if (!cb
.cb_dryrun
) {
1243 err
= destroy_clones(&cb
);
1245 err
= zfs_destroy_snaps_nvl(zhp
, cb
.cb_nvl
,
1246 cb
.cb_defer_destroy
);
1251 nvlist_free(cb
.cb_nvl
);
1255 /* Open the given dataset */
1256 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1262 * Perform an explicit check for pools before going any further.
1264 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1265 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1266 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1267 "operation does not apply to pools\n"),
1269 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1270 "%s' to destroy all datasets in the pool\n"),
1272 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1273 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1279 * Check for any dependents and/or clones.
1281 cb
.cb_first
= B_TRUE
;
1282 if (!cb
.cb_doclones
&&
1283 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1294 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1301 * Do the real thing. The callback will close the
1302 * handle regardless of whether it succeeds or not.
1304 if (destroy_callback(zhp
, &cb
) != 0)
1312 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1315 zfs_get_column_t col
;
1317 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1318 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1319 if (col
== GET_COL_RECVD
)
1325 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1326 * < all | property[,property]... > < fs | snap | vol > ...
1328 * -r recurse over any child datasets
1329 * -H scripted mode. Headers are stripped, and fields are separated
1330 * by tabs instead of spaces.
1331 * -o Set of fields to display. One of "name,property,value,
1332 * received,source". Default is "name,property,value,source".
1333 * "all" is an alias for all five.
1334 * -s Set of sources to allow. One of
1335 * "local,default,inherited,received,temporary,none". Default is
1337 * -p Display values in parsable (literal) format.
1339 * Prints properties for the given datasets. The user can control which
1340 * columns to display as well as which property types to allow.
1344 * Invoked to display the properties for a single dataset.
1347 get_callback(zfs_handle_t
*zhp
, void *data
)
1349 char buf
[ZFS_MAXPROPLEN
];
1350 char rbuf
[ZFS_MAXPROPLEN
];
1351 zprop_source_t sourcetype
;
1352 char source
[ZFS_MAXNAMELEN
];
1353 zprop_get_cbdata_t
*cbp
= data
;
1354 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1355 zprop_list_t
*pl
= cbp
->cb_proplist
;
1359 boolean_t received
= is_recvd_column(cbp
);
1361 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1362 char *recvdval
= NULL
;
1364 * Skip the special fake placeholder. This will also skip over
1365 * the name property when 'all' is specified.
1367 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1368 pl
== cbp
->cb_proplist
)
1371 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1372 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1373 sizeof (buf
), &sourcetype
, source
,
1375 cbp
->cb_literal
) != 0) {
1378 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1379 ZFS_TYPE_DATASET
)) {
1380 (void) fprintf(stderr
,
1381 gettext("No such property '%s'\n"),
1382 zfs_prop_to_name(pl
->pl_prop
));
1385 sourcetype
= ZPROP_SRC_NONE
;
1386 (void) strlcpy(buf
, "-", sizeof (buf
));
1389 if (received
&& (zfs_prop_get_recvd(zhp
,
1390 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1391 cbp
->cb_literal
) == 0))
1394 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1395 zfs_prop_to_name(pl
->pl_prop
),
1396 buf
, sourcetype
, source
, recvdval
);
1397 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1398 sourcetype
= ZPROP_SRC_LOCAL
;
1400 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1401 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1402 sourcetype
= ZPROP_SRC_NONE
;
1403 (void) strlcpy(buf
, "-", sizeof (buf
));
1406 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1407 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1408 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1409 sourcetype
= ZPROP_SRC_LOCAL
;
1411 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1412 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1413 sourcetype
= ZPROP_SRC_NONE
;
1414 (void) strlcpy(buf
, "-", sizeof (buf
));
1417 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1418 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1420 if (nvlist_lookup_nvlist(user_props
,
1421 pl
->pl_user_prop
, &propval
) != 0) {
1424 sourcetype
= ZPROP_SRC_NONE
;
1427 verify(nvlist_lookup_string(propval
,
1428 ZPROP_VALUE
, &strval
) == 0);
1429 verify(nvlist_lookup_string(propval
,
1430 ZPROP_SOURCE
, &sourceval
) == 0);
1432 if (strcmp(sourceval
,
1433 zfs_get_name(zhp
)) == 0) {
1434 sourcetype
= ZPROP_SRC_LOCAL
;
1435 } else if (strcmp(sourceval
,
1436 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1437 sourcetype
= ZPROP_SRC_RECEIVED
;
1439 sourcetype
= ZPROP_SRC_INHERITED
;
1440 (void) strlcpy(source
,
1441 sourceval
, sizeof (source
));
1445 if (received
&& (zfs_prop_get_recvd(zhp
,
1446 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1447 cbp
->cb_literal
) == 0))
1450 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1451 pl
->pl_user_prop
, strval
, sourcetype
,
1460 zfs_do_get(int argc
, char **argv
)
1462 zprop_get_cbdata_t cb
= { 0 };
1463 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1464 int types
= ZFS_TYPE_DATASET
;
1465 char *value
, *fields
;
1468 zprop_list_t fake_name
= { 0 };
1471 * Set up default columns and sources.
1473 cb
.cb_sources
= ZPROP_SRC_ALL
;
1474 cb
.cb_columns
[0] = GET_COL_NAME
;
1475 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1476 cb
.cb_columns
[2] = GET_COL_VALUE
;
1477 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1478 cb
.cb_type
= ZFS_TYPE_DATASET
;
1481 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1484 cb
.cb_literal
= B_TRUE
;
1487 limit
= parse_depth(optarg
, &flags
);
1490 flags
|= ZFS_ITER_RECURSE
;
1493 cb
.cb_scripted
= B_TRUE
;
1496 (void) fprintf(stderr
, gettext("missing argument for "
1497 "'%c' option\n"), optopt
);
1502 * Process the set of columns to display. We zero out
1503 * the structure to give us a blank slate.
1505 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1507 while (*optarg
!= '\0') {
1508 static char *col_subopts
[] =
1509 { "name", "property", "value", "received",
1510 "source", "all", NULL
};
1512 if (i
== ZFS_GET_NCOLS
) {
1513 (void) fprintf(stderr
, gettext("too "
1514 "many fields given to -o "
1519 switch (getsubopt(&optarg
, col_subopts
,
1522 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1525 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1528 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1531 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1532 flags
|= ZFS_ITER_RECVD_PROPS
;
1535 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1539 (void) fprintf(stderr
,
1540 gettext("\"all\" conflicts "
1541 "with specific fields "
1542 "given to -o option\n"));
1545 cb
.cb_columns
[0] = GET_COL_NAME
;
1546 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1547 cb
.cb_columns
[2] = GET_COL_VALUE
;
1548 cb
.cb_columns
[3] = GET_COL_RECVD
;
1549 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1550 flags
|= ZFS_ITER_RECVD_PROPS
;
1554 (void) fprintf(stderr
,
1555 gettext("invalid column name "
1564 while (*optarg
!= '\0') {
1565 static char *source_subopts
[] = {
1566 "local", "default", "inherited",
1567 "received", "temporary", "none",
1570 switch (getsubopt(&optarg
, source_subopts
,
1573 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1576 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1579 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1582 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1585 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1588 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1591 (void) fprintf(stderr
,
1592 gettext("invalid source "
1601 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1602 while (*optarg
!= '\0') {
1603 static char *type_subopts
[] = { "filesystem",
1604 "volume", "snapshot", "all", NULL
};
1606 switch (getsubopt(&optarg
, type_subopts
,
1609 types
|= ZFS_TYPE_FILESYSTEM
;
1612 types
|= ZFS_TYPE_VOLUME
;
1615 types
|= ZFS_TYPE_SNAPSHOT
;
1618 types
= ZFS_TYPE_DATASET
;
1622 (void) fprintf(stderr
,
1623 gettext("invalid type '%s'\n"),
1631 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1641 (void) fprintf(stderr
, gettext("missing property "
1648 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1656 * As part of zfs_expand_proplist(), we keep track of the maximum column
1657 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1658 * need to know the maximum name length. However, the user likely did
1659 * not specify 'name' as one of the properties to fetch, so we need to
1660 * make sure we always include at least this property for
1661 * print_get_headers() to work properly.
1663 if (cb
.cb_proplist
!= NULL
) {
1664 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1665 fake_name
.pl_width
= strlen(gettext("NAME"));
1666 fake_name
.pl_next
= cb
.cb_proplist
;
1667 cb
.cb_proplist
= &fake_name
;
1670 cb
.cb_first
= B_TRUE
;
1672 /* run for each object */
1673 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1674 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1676 if (cb
.cb_proplist
== &fake_name
)
1677 zprop_free_list(fake_name
.pl_next
);
1679 zprop_free_list(cb
.cb_proplist
);
1685 * inherit [-rS] <property> <fs|vol> ...
1687 * -r Recurse over all children
1688 * -S Revert to received value, if any
1690 * For each dataset specified on the command line, inherit the given property
1691 * from its parent. Inheriting a property at the pool level will cause it to
1692 * use the default value. The '-r' flag will recurse over all children, and is
1693 * useful for setting a property on a hierarchy-wide basis, regardless of any
1694 * local modifications for each dataset.
1697 typedef struct inherit_cbdata
{
1698 const char *cb_propname
;
1699 boolean_t cb_received
;
1703 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1705 inherit_cbdata_t
*cb
= data
;
1706 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1709 * If we're doing it recursively, then ignore properties that
1710 * are not valid for this type of dataset.
1712 if (prop
!= ZPROP_INVAL
&&
1713 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
)))
1716 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1720 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1722 inherit_cbdata_t
*cb
= data
;
1724 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1728 zfs_do_inherit(int argc
, char **argv
)
1732 inherit_cbdata_t cb
= { 0 };
1736 boolean_t received
= B_FALSE
;
1739 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1742 flags
|= ZFS_ITER_RECURSE
;
1749 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1758 /* check number of arguments */
1760 (void) fprintf(stderr
, gettext("missing property argument\n"));
1764 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1772 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1773 if (zfs_prop_readonly(prop
)) {
1774 (void) fprintf(stderr
, gettext(
1775 "%s property is read-only\n"),
1779 if (!zfs_prop_inheritable(prop
) && !received
) {
1780 (void) fprintf(stderr
, gettext("'%s' property cannot "
1781 "be inherited\n"), propname
);
1782 if (prop
== ZFS_PROP_QUOTA
||
1783 prop
== ZFS_PROP_RESERVATION
||
1784 prop
== ZFS_PROP_REFQUOTA
||
1785 prop
== ZFS_PROP_REFRESERVATION
)
1786 (void) fprintf(stderr
, gettext("use 'zfs set "
1787 "%s=none' to clear\n"), propname
);
1790 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1791 prop
== ZFS_PROP_VERSION
)) {
1792 (void) fprintf(stderr
, gettext("'%s' property cannot "
1793 "be reverted to a received value\n"), propname
);
1796 } else if (!zfs_prop_user(propname
)) {
1797 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1802 cb
.cb_propname
= propname
;
1803 cb
.cb_received
= received
;
1805 if (flags
& ZFS_ITER_RECURSE
) {
1806 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1807 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1809 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1810 NULL
, NULL
, 0, inherit_cb
, &cb
);
1816 typedef struct upgrade_cbdata
{
1817 uint64_t cb_numupgraded
;
1818 uint64_t cb_numsamegraded
;
1819 uint64_t cb_numfailed
;
1820 uint64_t cb_version
;
1822 boolean_t cb_foundone
;
1823 char cb_lastfs
[ZFS_MAXNAMELEN
];
1827 same_pool(zfs_handle_t
*zhp
, const char *name
)
1829 int len1
= strcspn(name
, "/@");
1830 const char *zhname
= zfs_get_name(zhp
);
1831 int len2
= strcspn(zhname
, "/@");
1835 return (strncmp(name
, zhname
, len1
) == 0);
1839 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
1841 upgrade_cbdata_t
*cb
= data
;
1842 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1844 /* list if it's old/new */
1845 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
1846 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
1849 str
= gettext("The following filesystems are "
1850 "formatted using a newer software version and\n"
1851 "cannot be accessed on the current system.\n\n");
1853 str
= gettext("The following filesystems are "
1854 "out of date, and can be upgraded. After being\n"
1855 "upgraded, these filesystems (and any 'zfs send' "
1856 "streams generated from\n"
1857 "subsequent snapshots) will no longer be "
1858 "accessible by older software versions.\n\n");
1861 if (!cb
->cb_foundone
) {
1863 (void) printf(gettext("VER FILESYSTEM\n"));
1864 (void) printf(gettext("--- ------------\n"));
1865 cb
->cb_foundone
= B_TRUE
;
1868 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
1875 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
1877 upgrade_cbdata_t
*cb
= data
;
1878 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1879 int needed_spa_version
;
1882 if (zfs_spa_version(zhp
, &spa_version
) < 0)
1885 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
1887 if (needed_spa_version
< 0)
1890 if (spa_version
< needed_spa_version
) {
1892 (void) printf(gettext("%s: can not be "
1893 "upgraded; the pool version needs to first "
1894 "be upgraded\nto version %d\n\n"),
1895 zfs_get_name(zhp
), needed_spa_version
);
1901 if (version
< cb
->cb_version
) {
1903 (void) snprintf(verstr
, sizeof (verstr
),
1904 "%llu", (u_longlong_t
)cb
->cb_version
);
1905 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
1907 * If they did "zfs upgrade -a", then we could
1908 * be doing ioctls to different pools. We need
1909 * to log this history once to each pool.
1911 verify(zpool_stage_history(g_zfs
, history_str
) == 0);
1913 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
1914 cb
->cb_numupgraded
++;
1917 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
1918 } else if (version
> cb
->cb_version
) {
1919 /* can't downgrade */
1920 (void) printf(gettext("%s: can not be downgraded; "
1921 "it is already at version %u\n"),
1922 zfs_get_name(zhp
), version
);
1925 cb
->cb_numsamegraded
++;
1933 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1936 zfs_do_upgrade(int argc
, char **argv
)
1938 boolean_t all
= B_FALSE
;
1939 boolean_t showversions
= B_FALSE
;
1941 upgrade_cbdata_t cb
= { 0 };
1943 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1946 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
1949 flags
|= ZFS_ITER_RECURSE
;
1952 showversions
= B_TRUE
;
1955 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
1956 optarg
, &cb
.cb_version
) != 0) {
1957 (void) fprintf(stderr
,
1958 gettext("invalid version %s\n"), optarg
);
1967 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1976 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
1978 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
1979 cb
.cb_version
|| argc
))
1981 if ((all
|| argc
) && (showversions
))
1987 /* Show info on available versions. */
1988 (void) printf(gettext("The following filesystem versions are "
1990 (void) printf(gettext("VER DESCRIPTION\n"));
1991 (void) printf("--- -----------------------------------------"
1992 "---------------\n");
1993 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1994 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1995 (void) printf(gettext(" 3 Case insensitive and filesystem "
1996 "user identifier (FUID)\n"));
1997 (void) printf(gettext(" 4 userquota, groupquota "
1999 (void) printf(gettext(" 5 System attributes\n"));
2000 (void) printf(gettext("\nFor more information on a particular "
2001 "version, including supported releases,\n"));
2002 (void) printf("see the ZFS Administration Guide.\n\n");
2004 } else if (argc
|| all
) {
2005 /* Upgrade filesystems */
2006 if (cb
.cb_version
== 0)
2007 cb
.cb_version
= ZPL_VERSION
;
2008 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2009 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2010 (void) printf(gettext("%llu filesystems upgraded\n"),
2011 (u_longlong_t
)cb
.cb_numupgraded
);
2012 if (cb
.cb_numsamegraded
) {
2013 (void) printf(gettext("%llu filesystems already at "
2015 (u_longlong_t
)cb
.cb_numsamegraded
);
2017 if (cb
.cb_numfailed
!= 0)
2020 /* List old-version filesytems */
2022 (void) printf(gettext("This system is currently running "
2023 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2025 flags
|= ZFS_ITER_RECURSE
;
2026 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2027 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2029 found
= cb
.cb_foundone
;
2030 cb
.cb_foundone
= B_FALSE
;
2031 cb
.cb_newer
= B_TRUE
;
2033 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2034 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2036 if (!cb
.cb_foundone
&& !found
) {
2037 (void) printf(gettext("All filesystems are "
2038 "formatted with the current version.\n"));
2045 #define USTYPE_USR_BIT (0)
2046 #define USTYPE_GRP_BIT (1)
2047 #define USTYPE_PSX_BIT (2)
2048 #define USTYPE_SMB_BIT (3)
2050 #define USTYPE_USR (1 << USTYPE_USR_BIT)
2051 #define USTYPE_GRP (1 << USTYPE_GRP_BIT)
2053 #define USTYPE_PSX (1 << USTYPE_PSX_BIT)
2054 #define USTYPE_SMB (1 << USTYPE_SMB_BIT)
2056 #define USTYPE_PSX_USR (USTYPE_PSX | USTYPE_USR)
2057 #define USTYPE_SMB_USR (USTYPE_SMB | USTYPE_USR)
2058 #define USTYPE_PSX_GRP (USTYPE_PSX | USTYPE_GRP)
2059 #define USTYPE_SMB_GRP (USTYPE_SMB | USTYPE_GRP)
2060 #define USTYPE_ALL (USTYPE_PSX_USR | USTYPE_SMB_USR \
2061 | USTYPE_PSX_GRP | USTYPE_SMB_GRP)
2064 #define USPROP_USED_BIT (0)
2065 #define USPROP_QUOTA_BIT (1)
2067 #define USPROP_USED (1 << USPROP_USED_BIT)
2068 #define USPROP_QUOTA (1 << USPROP_QUOTA_BIT)
2070 typedef struct us_node
{
2072 uu_avl_node_t usn_avlnode
;
2073 uu_list_node_t usn_listnode
;
2076 typedef struct us_cbdata
{
2078 uu_avl_pool_t
*cb_avl_pool
;
2080 boolean_t cb_numname
;
2081 boolean_t cb_nicenum
;
2082 boolean_t cb_sid2posix
;
2083 zfs_userquota_prop_t cb_prop
;
2084 zfs_sort_column_t
*cb_sortcol
;
2085 size_t cb_max_typelen
;
2086 size_t cb_max_namelen
;
2087 size_t cb_max_usedlen
;
2088 size_t cb_max_quotalen
;
2092 zfs_sort_column_t
*si_sortcol
;
2093 boolean_t si_num_name
;
2094 boolean_t si_parsable
;
2098 us_compare(const void *larg
, const void *rarg
, void *unused
)
2100 const us_node_t
*l
= larg
;
2101 const us_node_t
*r
= rarg
;
2103 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2104 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2105 boolean_t num_name
= si
->si_num_name
;
2106 nvlist_t
*lnvl
= l
->usn_nvl
;
2107 nvlist_t
*rnvl
= r
->usn_nvl
;
2109 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2116 zfs_prop_t prop
= sortcol
->sc_prop
;
2117 const char *propname
= NULL
;
2118 boolean_t reverse
= sortcol
->sc_reverse
;
2123 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2124 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2126 rc
= (rv32
> lv32
) ? 1 : -1;
2131 (void) nvlist_lookup_uint32(lnvl
, propname
,
2133 (void) nvlist_lookup_uint32(rnvl
, propname
,
2136 rc
= (rv32
> lv32
) ? 1 : -1;
2138 (void) nvlist_lookup_string(lnvl
, propname
,
2140 (void) nvlist_lookup_string(rnvl
, propname
,
2142 rc
= strcmp(lvstr
, rvstr
);
2147 case ZFS_PROP_QUOTA
:
2148 if (ZFS_PROP_USED
== prop
)
2152 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2153 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2155 rc
= (rv64
> lv64
) ? 1 : -1;
2162 return (reverse
? 1 : -1);
2164 return (reverse
? -1 : 1);
2171 static inline const char *
2172 us_type2str(unsigned field_type
)
2174 switch (field_type
) {
2175 case USTYPE_PSX_USR
:
2176 return ("POSIX User");
2177 case USTYPE_PSX_GRP
:
2178 return ("POSIX Group");
2179 case USTYPE_SMB_USR
:
2180 return ("SMB User");
2181 case USTYPE_SMB_GRP
:
2182 return ("SMB Group");
2184 return ("Undefined");
2192 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2194 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2195 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2201 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2202 uu_avl_t
*avl
= cb
->cb_avl
;
2206 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2208 const char *typestr
;
2212 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2214 if (domain
== NULL
|| domain
[0] == '\0') {
2216 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2217 type
= USTYPE_PSX_GRP
;
2218 struct group
*g
= getgrgid(rid
);
2222 type
= USTYPE_PSX_USR
;
2223 struct passwd
*p
= getpwuid(rid
);
2229 char sid
[ZFS_MAXNAMELEN
+32];
2233 directory_error_t e
;
2235 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2237 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2238 type
= USTYPE_SMB_GRP
;
2239 err
= sid_to_id(sid
, B_FALSE
, &id
);
2241 type
= USTYPE_SMB_USR
;
2242 err
= sid_to_id(sid
, B_TRUE
, &id
);
2248 e
= directory_name_from_sid(NULL
, sid
, &name
, &classes
);
2250 directory_error_free(e
);
2259 #endif /* HAVE_IDMAP */
2263 * if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA)
2269 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
)
2274 (void) snprintf(namebuf
, sizeof (namebuf
), "%u", rid
);
2279 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2281 (void) sprintf(sizebuf
, "%llu", (u_longlong_t
)space
);
2283 node
= safe_malloc(sizeof (us_node_t
));
2284 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2286 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
2291 if (nvlist_add_uint32(props
, "type", type
) != 0)
2294 if (cb
->cb_numname
) {
2295 if (nvlist_add_uint32(props
, "name", rid
) != 0)
2297 namelen
= strlen(namebuf
);
2299 if (nvlist_add_string(props
, "name", name
) != 0)
2301 namelen
= strlen(name
);
2304 typestr
= us_type2str(type
);
2305 typelen
= strlen(gettext(typestr
));
2306 if (typelen
> cb
->cb_max_typelen
)
2307 cb
->cb_max_typelen
= typelen
;
2309 if (namelen
> cb
->cb_max_namelen
)
2310 cb
->cb_max_namelen
= namelen
;
2312 sizelen
= strlen(sizebuf
);
2313 if (0 == strcmp(propname
, "used")) {
2314 if (sizelen
> cb
->cb_max_usedlen
)
2315 cb
->cb_max_usedlen
= sizelen
;
2317 if (sizelen
> cb
->cb_max_quotalen
)
2318 cb
->cb_max_quotalen
= sizelen
;
2321 node
->usn_nvl
= props
;
2323 n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
);
2325 uu_avl_insert(avl
, node
, idx
);
2330 props
= node
->usn_nvl
;
2333 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2339 static inline boolean_t
2340 usprop_check(zfs_userquota_prop_t p
, unsigned types
, unsigned props
)
2346 case ZFS_PROP_USERUSED
:
2350 case ZFS_PROP_USERQUOTA
:
2352 prop
= USPROP_QUOTA
;
2354 case ZFS_PROP_GROUPUSED
:
2358 case ZFS_PROP_GROUPQUOTA
:
2360 prop
= USPROP_QUOTA
;
2366 return (type
& types
&& prop
& props
);
2369 #define USFIELD_TYPE (1 << 0)
2370 #define USFIELD_NAME (1 << 1)
2371 #define USFIELD_USED (1 << 2)
2372 #define USFIELD_QUOTA (1 << 3)
2373 #define USFIELD_ALL (USFIELD_TYPE | USFIELD_NAME | USFIELD_USED | USFIELD_QUOTA)
2376 parsefields(unsigned *fieldsp
, char **names
, unsigned *bits
, size_t len
)
2378 char *field
= optarg
;
2383 boolean_t found
= B_FALSE
;
2384 delim
= strchr(field
, ',');
2388 for (i
= 0; i
< len
; i
++)
2389 if (0 == strcmp(field
, names
[i
])) {
2391 *fieldsp
|= bits
[i
];
2396 (void) fprintf(stderr
, gettext("invalid type '%s'"
2397 "for -t option\n"), field
);
2408 static char *type_names
[] = { "posixuser", "smbuser", "posixgroup", "smbgroup",
2410 static unsigned type_bits
[] = {
2418 static char *us_field_names
[] = { "type", "name", "used", "quota" };
2419 static unsigned us_field_bits
[] = {
2427 print_us_node(boolean_t scripted
, boolean_t parseable
, unsigned fields
,
2428 size_t type_width
, size_t name_width
, size_t used_width
,
2429 size_t quota_width
, us_node_t
*node
)
2431 nvlist_t
*nvl
= node
->usn_nvl
;
2432 nvpair_t
*nvp
= NULL
;
2433 char valstr
[ZFS_MAXNAMELEN
];
2434 boolean_t first
= B_TRUE
;
2435 boolean_t quota_found
= B_FALSE
;
2437 if (fields
& USFIELD_QUOTA
&& !nvlist_exists(nvl
, "quota"))
2438 if (nvlist_add_string(nvl
, "quota", "none") != 0)
2441 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2442 char *pname
= nvpair_name(nvp
);
2443 data_type_t type
= nvpair_type(nvp
);
2446 char *strval
= NULL
;
2450 for (i
= 0; i
< 4; i
++) {
2451 if (0 == strcmp(pname
, us_field_names
[i
])) {
2452 field
= us_field_bits
[i
];
2457 if (!(field
& fields
))
2461 case DATA_TYPE_UINT32
:
2462 (void) nvpair_value_uint32(nvp
, &val32
);
2464 case DATA_TYPE_UINT64
:
2465 (void) nvpair_value_uint64(nvp
, &val64
);
2467 case DATA_TYPE_STRING
:
2468 (void) nvpair_value_string(nvp
, &strval
);
2471 (void) fprintf(stderr
, "Invalid data type\n");
2476 (void) printf("\t");
2483 strval
= (char *)us_type2str(val32
);
2487 if (type
== DATA_TYPE_UINT64
) {
2488 (void) sprintf(valstr
, "%llu",
2489 (u_longlong_t
) val64
);
2496 if (type
== DATA_TYPE_UINT64
) {
2497 (void) nvpair_value_uint64(nvp
, &val64
);
2499 (void) sprintf(valstr
, "%llu",
2500 (u_longlong_t
) val64
);
2502 zfs_nicenum(val64
, valstr
,
2507 if (field
== USFIELD_USED
)
2510 quota_found
= B_FALSE
;
2511 width
= quota_width
;
2517 if (field
== USFIELD_QUOTA
&& !quota_found
)
2518 (void) printf("%*s", width
, strval
);
2520 if (type
== DATA_TYPE_STRING
)
2521 (void) printf("%-*s", width
, strval
);
2523 (void) printf("%*s", width
, strval
);
2530 (void) printf("\n");
2534 print_us(boolean_t scripted
, boolean_t parsable
, unsigned fields
,
2535 unsigned type_width
, unsigned name_width
, unsigned used_width
,
2536 unsigned quota_width
, boolean_t rmnode
, uu_avl_t
*avl
)
2538 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA" };
2542 int width
[4] = { type_width
, name_width
, used_width
, quota_width
};
2545 boolean_t first
= B_TRUE
;
2546 for (i
= 0; i
< 4; i
++) {
2547 unsigned field
= us_field_bits
[i
];
2548 if (!(field
& fields
))
2551 col
= gettext(us_field_hdr
[i
]);
2552 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2553 (void) printf(first
?"%-*s":" %-*s", width
[i
],
2556 (void) printf(first
?"%*s":" %*s", width
[i
],
2560 (void) printf("\n");
2563 for (node
= uu_avl_first(avl
); node
!= NULL
;
2564 node
= uu_avl_next(avl
, node
)) {
2565 print_us_node(scripted
, parsable
, fields
, type_width
,
2566 name_width
, used_width
, used_width
, node
);
2568 nvlist_free(node
->usn_nvl
);
2573 zfs_do_userspace(int argc
, char **argv
)
2576 zfs_userquota_prop_t p
;
2577 uu_avl_pool_t
*avl_pool
;
2579 uu_avl_walk_t
*walk
;
2582 boolean_t scripted
= B_FALSE
;
2583 boolean_t prtnum
= B_FALSE
;
2584 boolean_t parseable
= B_FALSE
;
2585 boolean_t sid2posix
= B_FALSE
;
2588 zfs_sort_column_t
*default_sortcol
= NULL
;
2589 zfs_sort_column_t
*sortcol
= NULL
;
2590 unsigned types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2591 unsigned fields
= 0;
2592 unsigned props
= USPROP_USED
| USPROP_QUOTA
;
2595 boolean_t resort_avl
= B_FALSE
;
2601 if (0 == strcmp(cmd
, "groupspace"))
2602 /* toggle default group types */
2603 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2606 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2618 if (parsefields(&fields
, us_field_names
, us_field_bits
,
2623 if (zfs_add_sort_column(&sortcol
, optarg
,
2625 (void) fprintf(stderr
,
2626 gettext("invalid property '%s'\n"), optarg
);
2631 if (zfs_add_sort_column(&sortcol
, optarg
,
2633 (void) fprintf(stderr
,
2634 gettext("invalid property '%s'\n"), optarg
);
2639 if (parsefields(&types
, type_names
, type_bits
, 5))
2646 (void) fprintf(stderr
, gettext("missing argument for "
2647 "'%c' option\n"), optopt
);
2651 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2660 /* ok, now we have sorted by default colums (type,name) avl tree */
2662 zfs_sort_column_t
*sc
;
2663 for (sc
= sortcol
; sc
; sc
= sc
->sc_next
) {
2664 if (sc
->sc_prop
== ZFS_PROP_QUOTA
) {
2665 resort_avl
= B_TRUE
;
2672 fields
= USFIELD_ALL
;
2674 if ((zhp
= zfs_open(g_zfs
, argv
[argc
-1], ZFS_TYPE_DATASET
)) == NULL
)
2677 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
2678 offsetof(us_node_t
, usn_avlnode
),
2679 us_compare
, UU_DEFAULT
)) == NULL
)
2681 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
2684 if (sortcol
&& !resort_avl
)
2685 cb
.cb_sortcol
= sortcol
;
2687 (void) zfs_add_sort_column(&default_sortcol
, "type", B_FALSE
);
2688 (void) zfs_add_sort_column(&default_sortcol
, "name", B_FALSE
);
2689 cb
.cb_sortcol
= default_sortcol
;
2691 cb
.cb_numname
= prtnum
;
2692 cb
.cb_nicenum
= !parseable
;
2693 cb
.cb_avl_pool
= avl_pool
;
2694 cb
.cb_avl
= avl_tree
;
2695 cb
.cb_sid2posix
= sid2posix
;
2696 cb
.cb_max_typelen
= strlen(gettext("TYPE"));
2697 cb
.cb_max_namelen
= strlen(gettext("NAME"));
2698 cb
.cb_max_usedlen
= strlen(gettext("USED"));
2699 cb
.cb_max_quotalen
= strlen(gettext("QUOTA"));
2701 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
2702 if (!usprop_check(p
, types
, props
))
2706 error
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
);
2715 uu_list_pool_t
*listpool
;
2717 uu_avl_index_t idx
= 0;
2718 uu_list_index_t idx2
= 0;
2719 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
2720 offsetof(us_node_t
, usn_listnode
), NULL
,
2722 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
2724 node
= uu_avl_first(avl_tree
);
2725 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
2726 while (node
!= NULL
) {
2728 node
= uu_avl_next(avl_tree
, node
);
2729 uu_avl_remove(avl_tree
, rmnode
);
2730 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
) {
2731 uu_list_insert(list
, rmnode
, idx2
);
2735 for (node
= uu_list_first(list
); node
!= NULL
;
2736 node
= uu_list_next(list
, node
)) {
2737 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
2738 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) ==
2740 uu_avl_insert(avl_tree
, node
, idx
);
2743 uu_list_destroy(list
);
2746 /* print & free node`s nvlist memory */
2747 print_us(scripted
, parseable
, fields
, cb
.cb_max_typelen
,
2748 cb
.cb_max_namelen
, cb
.cb_max_usedlen
,
2749 cb
.cb_max_quotalen
, B_TRUE
, cb
.cb_avl
);
2752 zfs_free_sort_columns(sortcol
);
2753 zfs_free_sort_columns(default_sortcol
);
2756 * Finally, clean up the AVL tree.
2758 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
2761 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
2762 uu_avl_remove(cb
.cb_avl
, node
);
2766 uu_avl_walk_end(walk
);
2767 uu_avl_destroy(avl_tree
);
2768 uu_avl_pool_destroy(avl_pool
);
2774 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
2775 * [-s property [-s property]...] [-S property [-S property]...]
2778 * -r Recurse over all children
2779 * -d Limit recursion by depth.
2780 * -H Scripted mode; elide headers and separate columns by tabs
2781 * -o Control which fields to display.
2782 * -t Control which object types to display.
2783 * -s Specify sort columns, descending order.
2784 * -S Specify sort columns, ascending order.
2786 * When given no arguments, lists all filesystems in the system.
2787 * Otherwise, list the specified datasets, optionally recursing down them if
2788 * '-r' is specified.
2790 typedef struct list_cbdata
{
2792 boolean_t cb_scripted
;
2793 zprop_list_t
*cb_proplist
;
2797 * Given a list of columns to display, output appropriate headers for each one.
2800 print_header(zprop_list_t
*pl
)
2802 char headerbuf
[ZFS_MAXPROPLEN
];
2805 boolean_t first
= B_TRUE
;
2806 boolean_t right_justify
;
2808 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2815 right_justify
= B_FALSE
;
2816 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2817 header
= zfs_prop_column_name(pl
->pl_prop
);
2818 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2820 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2821 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2822 headerbuf
[i
] = '\0';
2826 if (pl
->pl_next
== NULL
&& !right_justify
)
2827 (void) printf("%s", header
);
2828 else if (right_justify
)
2829 (void) printf("%*s", (int)pl
->pl_width
, header
);
2831 (void) printf("%-*s", (int)pl
->pl_width
, header
);
2834 (void) printf("\n");
2838 * Given a dataset and a list of fields, print out all the properties according
2839 * to the described layout.
2842 print_dataset(zfs_handle_t
*zhp
, zprop_list_t
*pl
, boolean_t scripted
)
2844 boolean_t first
= B_TRUE
;
2845 char property
[ZFS_MAXPROPLEN
];
2846 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
2849 boolean_t right_justify
;
2852 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2855 (void) printf("\t");
2862 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
2863 (void) strlcpy(property
, zfs_get_name(zhp
),
2866 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2867 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
2868 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
2869 sizeof (property
), NULL
, NULL
, 0, B_FALSE
) != 0)
2874 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2875 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
2876 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
2877 property
, sizeof (property
), B_FALSE
) != 0)
2881 right_justify
= B_TRUE
;
2882 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
2883 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
2884 property
, sizeof (property
), B_FALSE
) != 0)
2888 right_justify
= B_TRUE
;
2890 if (nvlist_lookup_nvlist(userprops
,
2891 pl
->pl_user_prop
, &propval
) != 0)
2894 verify(nvlist_lookup_string(propval
,
2895 ZPROP_VALUE
, &propstr
) == 0);
2896 right_justify
= B_FALSE
;
2899 width
= pl
->pl_width
;
2902 * If this is being called in scripted mode, or if this is the
2903 * last column and it is left-justified, don't include a width
2906 if (scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
2907 (void) printf("%s", propstr
);
2908 else if (right_justify
)
2909 (void) printf("%*s", width
, propstr
);
2911 (void) printf("%-*s", width
, propstr
);
2914 (void) printf("\n");
2918 * Generic callback function to list a dataset or snapshot.
2921 list_callback(zfs_handle_t
*zhp
, void *data
)
2923 list_cbdata_t
*cbp
= data
;
2925 if (cbp
->cb_first
) {
2926 if (!cbp
->cb_scripted
)
2927 print_header(cbp
->cb_proplist
);
2928 cbp
->cb_first
= B_FALSE
;
2931 print_dataset(zhp
, cbp
->cb_proplist
, cbp
->cb_scripted
);
2937 zfs_do_list(int argc
, char **argv
)
2940 boolean_t scripted
= B_FALSE
;
2941 static char default_fields
[] =
2942 "name,used,available,referenced,mountpoint";
2943 int types
= ZFS_TYPE_DATASET
;
2944 boolean_t types_specified
= B_FALSE
;
2945 char *fields
= NULL
;
2946 list_cbdata_t cb
= { 0 };
2950 zfs_sort_column_t
*sortcol
= NULL
;
2951 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
2954 while ((c
= getopt(argc
, argv
, ":d:o:rt:Hs:S:")) != -1) {
2960 limit
= parse_depth(optarg
, &flags
);
2963 flags
|= ZFS_ITER_RECURSE
;
2969 if (zfs_add_sort_column(&sortcol
, optarg
,
2971 (void) fprintf(stderr
,
2972 gettext("invalid property '%s'\n"), optarg
);
2977 if (zfs_add_sort_column(&sortcol
, optarg
,
2979 (void) fprintf(stderr
,
2980 gettext("invalid property '%s'\n"), optarg
);
2986 types_specified
= B_TRUE
;
2987 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
2988 while (*optarg
!= '\0') {
2989 static char *type_subopts
[] = { "filesystem",
2990 "volume", "snapshot", "snap", "all", NULL
};
2992 switch (getsubopt(&optarg
, type_subopts
,
2995 types
|= ZFS_TYPE_FILESYSTEM
;
2998 types
|= ZFS_TYPE_VOLUME
;
3002 types
|= ZFS_TYPE_SNAPSHOT
;
3005 types
= ZFS_TYPE_DATASET
;
3009 (void) fprintf(stderr
,
3010 gettext("invalid type '%s'\n"),
3017 (void) fprintf(stderr
, gettext("missing argument for "
3018 "'%c' option\n"), optopt
);
3022 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3032 fields
= default_fields
;
3035 * If we are only going to list snapshot names and sort by name,
3036 * then we can use faster version.
3038 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3039 flags
|= ZFS_ITER_SIMPLE
;
3042 * If "-o space" and no types were specified, don't display snapshots.
3044 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3045 types
&= ~ZFS_TYPE_SNAPSHOT
;
3048 * If the user specifies '-o all', the zprop_get_list() doesn't
3049 * normally include the name of the dataset. For 'zfs list', we always
3050 * want this property to be first.
3052 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3056 cb
.cb_scripted
= scripted
;
3057 cb
.cb_first
= B_TRUE
;
3059 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3060 limit
, list_callback
, &cb
);
3062 zprop_free_list(cb
.cb_proplist
);
3063 zfs_free_sort_columns(sortcol
);
3065 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3066 (void) fprintf(stderr
, gettext("no datasets available\n"));
3072 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3073 * zfs rename [-f] -p <fs | vol> <fs | vol>
3074 * zfs rename -r <snap> <snap>
3076 * Renames the given dataset to another of the same type.
3078 * The '-p' flag creates all the non-existing ancestors of the target first.
3082 zfs_do_rename(int argc
, char **argv
)
3087 boolean_t recurse
= B_FALSE
;
3088 boolean_t parents
= B_FALSE
;
3089 boolean_t force_unmount
= B_FALSE
;
3092 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3101 force_unmount
= B_TRUE
;
3105 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3114 /* check number of arguments */
3116 (void) fprintf(stderr
, gettext("missing source dataset "
3121 (void) fprintf(stderr
, gettext("missing target dataset "
3126 (void) fprintf(stderr
, gettext("too many arguments\n"));
3130 if (recurse
&& parents
) {
3131 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3136 if (recurse
&& strchr(argv
[0], '@') == 0) {
3137 (void) fprintf(stderr
, gettext("source dataset for recursive "
3138 "rename must be a snapshot\n"));
3142 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3143 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3146 /* If we were asked and the name looks good, try to create ancestors. */
3147 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3148 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3153 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3162 * Promotes the given clone fs to be the parent
3166 zfs_do_promote(int argc
, char **argv
)
3172 if (argc
> 1 && argv
[1][0] == '-') {
3173 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3178 /* check number of arguments */
3180 (void) fprintf(stderr
, gettext("missing clone filesystem"
3185 (void) fprintf(stderr
, gettext("too many arguments\n"));
3189 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3193 ret
= (zfs_promote(zhp
) != 0);
3201 * zfs rollback [-rRf] <snapshot>
3203 * -r Delete any intervening snapshots before doing rollback
3204 * -R Delete any snapshots and their clones
3205 * -f ignored for backwards compatability
3207 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3208 * since then and making it the active dataset. If more recent snapshots exist,
3209 * the command will complain unless the '-r' flag is given.
3211 typedef struct rollback_cbdata
{
3217 boolean_t cb_recurse
;
3218 boolean_t cb_dependent
;
3219 } rollback_cbdata_t
;
3222 * Report any snapshots more recent than the one specified. Used when '-r' is
3223 * not specified. We reuse this same callback for the snapshot dependents - if
3224 * 'cb_dependent' is set, then this is a dependent and we should report it
3225 * without checking the transaction group.
3228 rollback_check(zfs_handle_t
*zhp
, void *data
)
3230 rollback_cbdata_t
*cbp
= data
;
3232 if (cbp
->cb_doclones
) {
3237 if (!cbp
->cb_dependent
) {
3238 if (strcmp(zfs_get_name(zhp
), cbp
->cb_target
) != 0 &&
3239 zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
3240 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) >
3243 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3244 (void) fprintf(stderr
, gettext("cannot "
3245 "rollback to '%s': more recent snapshots "
3248 (void) fprintf(stderr
, gettext("use '-r' to "
3249 "force deletion of the following "
3255 if (cbp
->cb_recurse
) {
3256 cbp
->cb_dependent
= B_TRUE
;
3257 if (zfs_iter_dependents(zhp
, B_TRUE
,
3258 rollback_check
, cbp
) != 0) {
3262 cbp
->cb_dependent
= B_FALSE
;
3264 (void) fprintf(stderr
, "%s\n",
3269 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3270 (void) fprintf(stderr
, gettext("cannot rollback to "
3271 "'%s': clones of previous snapshots exist\n"),
3273 (void) fprintf(stderr
, gettext("use '-R' to "
3274 "force deletion of the following clones and "
3280 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3288 zfs_do_rollback(int argc
, char **argv
)
3292 boolean_t force
= B_FALSE
;
3293 rollback_cbdata_t cb
= { 0 };
3294 zfs_handle_t
*zhp
, *snap
;
3295 char parentname
[ZFS_MAXNAMELEN
];
3299 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3312 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3321 /* check number of arguments */
3323 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3327 (void) fprintf(stderr
, gettext("too many arguments\n"));
3331 /* open the snapshot */
3332 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3335 /* open the parent dataset */
3336 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3337 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3339 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3345 * Check for more recent snapshots and/or clones based on the presence
3348 cb
.cb_target
= argv
[0];
3349 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3350 cb
.cb_first
= B_TRUE
;
3352 if ((ret
= zfs_iter_children(zhp
, rollback_check
, &cb
)) != 0)
3355 if ((ret
= cb
.cb_error
) != 0)
3359 * Rollback parent to the given snapshot.
3361 ret
= zfs_rollback(zhp
, snap
, force
);
3374 * zfs set property=value { fs | snap | vol } ...
3376 * Sets the given property for all datasets specified on the command line.
3378 typedef struct set_cbdata
{
3384 set_callback(zfs_handle_t
*zhp
, void *data
)
3386 set_cbdata_t
*cbp
= data
;
3388 if (zfs_prop_set(zhp
, cbp
->cb_propname
, cbp
->cb_value
) != 0) {
3389 switch (libzfs_errno(g_zfs
)) {
3390 case EZFS_MOUNTFAILED
:
3391 (void) fprintf(stderr
, gettext("property may be set "
3392 "but unable to remount filesystem\n"));
3394 case EZFS_SHARENFSFAILED
:
3395 (void) fprintf(stderr
, gettext("property may be set "
3396 "but unable to reshare filesystem\n"));
3405 zfs_do_set(int argc
, char **argv
)
3410 /* check for options */
3411 if (argc
> 1 && argv
[1][0] == '-') {
3412 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3417 /* check number of arguments */
3419 (void) fprintf(stderr
, gettext("missing property=value "
3424 (void) fprintf(stderr
, gettext("missing dataset name\n"));
3428 /* validate property=value argument */
3429 cb
.cb_propname
= argv
[1];
3430 if (((cb
.cb_value
= strchr(cb
.cb_propname
, '=')) == NULL
) ||
3431 (cb
.cb_value
[1] == '\0')) {
3432 (void) fprintf(stderr
, gettext("missing value in "
3433 "property=value argument\n"));
3437 *cb
.cb_value
= '\0';
3440 if (*cb
.cb_propname
== '\0') {
3441 (void) fprintf(stderr
,
3442 gettext("missing property in property=value argument\n"));
3446 ret
= zfs_for_each(argc
- 2, argv
+ 2, 0,
3447 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, &cb
);
3453 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3455 * Creates a snapshot with the given name. While functionally equivalent to
3456 * 'zfs create', it is a separate command to differentiate intent.
3459 zfs_do_snapshot(int argc
, char **argv
)
3461 boolean_t recursive
= B_FALSE
;
3466 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3470 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3473 if (parseprop(props
))
3480 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3489 /* check number of arguments */
3491 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3495 (void) fprintf(stderr
, gettext("too many arguments\n"));
3499 ret
= zfs_snapshot(g_zfs
, argv
[0], recursive
, props
);
3501 if (ret
&& recursive
)
3502 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3512 * Send a backup stream to stdout.
3515 zfs_do_send(int argc
, char **argv
)
3517 char *fromname
= NULL
;
3518 char *toname
= NULL
;
3521 sendflags_t flags
= { 0 };
3523 nvlist_t
*dbgnv
= NULL
;
3524 boolean_t extraverbose
= B_FALSE
;
3527 while ((c
= getopt(argc
, argv
, ":i:I:RDpvnP")) != -1) {
3538 flags
.doall
= B_TRUE
;
3541 flags
.replicate
= B_TRUE
;
3544 flags
.props
= B_TRUE
;
3547 flags
.parsable
= B_TRUE
;
3548 flags
.verbose
= B_TRUE
;
3552 extraverbose
= B_TRUE
;
3553 flags
.verbose
= B_TRUE
;
3556 flags
.dedup
= B_TRUE
;
3559 flags
.dryrun
= B_TRUE
;
3562 (void) fprintf(stderr
, gettext("missing argument for "
3563 "'%c' option\n"), optopt
);
3567 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3576 /* check number of arguments */
3578 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3582 (void) fprintf(stderr
, gettext("too many arguments\n"));
3586 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
3587 (void) fprintf(stderr
,
3588 gettext("Error: Stream can not be written to a terminal.\n"
3589 "You must redirect standard output.\n"));
3593 cp
= strchr(argv
[0], '@');
3595 (void) fprintf(stderr
,
3596 gettext("argument must be a snapshot\n"));
3601 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3606 * If they specified the full path to the snapshot, chop off
3607 * everything except the short name of the snapshot, but special
3608 * case if they specify the origin.
3610 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
3611 char origin
[ZFS_MAXNAMELEN
];
3614 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
3615 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
3617 if (strcmp(origin
, fromname
) == 0) {
3619 flags
.fromorigin
= B_TRUE
;
3622 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
3623 (void) fprintf(stderr
,
3624 gettext("incremental source must be "
3625 "in same filesystem\n"));
3629 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
3630 (void) fprintf(stderr
,
3631 gettext("invalid incremental source\n"));
3637 if (flags
.replicate
&& fromname
== NULL
)
3638 flags
.doall
= B_TRUE
;
3640 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
3641 extraverbose
? &dbgnv
: NULL
);
3643 if (extraverbose
&& dbgnv
!= NULL
) {
3645 * dump_nvlist prints to stdout, but that's been
3646 * redirected to a file. Make it print to stderr
3649 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
3650 dump_nvlist(dbgnv
, 0);
3659 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3661 * Restore a backup stream from stdin.
3664 zfs_do_receive(int argc
, char **argv
)
3667 recvflags_t flags
= { 0 };
3670 while ((c
= getopt(argc
, argv
, ":denuvF")) != -1) {
3673 flags
.isprefix
= B_TRUE
;
3676 flags
.isprefix
= B_TRUE
;
3677 flags
.istail
= B_TRUE
;
3680 flags
.dryrun
= B_TRUE
;
3683 flags
.nomount
= B_TRUE
;
3686 flags
.verbose
= B_TRUE
;
3689 flags
.force
= B_TRUE
;
3692 (void) fprintf(stderr
, gettext("missing argument for "
3693 "'%c' option\n"), optopt
);
3697 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3706 /* check number of arguments */
3708 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3712 (void) fprintf(stderr
, gettext("too many arguments\n"));
3716 if (isatty(STDIN_FILENO
)) {
3717 (void) fprintf(stderr
,
3718 gettext("Error: Backup stream can not be read "
3719 "from a terminal.\n"
3720 "You must redirect standard input.\n"));
3724 err
= zfs_receive(g_zfs
, argv
[0], &flags
, STDIN_FILENO
, NULL
);
3730 * allow/unallow stuff
3732 /* copied from zfs/sys/dsl_deleg.h */
3733 #define ZFS_DELEG_PERM_CREATE "create"
3734 #define ZFS_DELEG_PERM_DESTROY "destroy"
3735 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3736 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3737 #define ZFS_DELEG_PERM_CLONE "clone"
3738 #define ZFS_DELEG_PERM_PROMOTE "promote"
3739 #define ZFS_DELEG_PERM_RENAME "rename"
3740 #define ZFS_DELEG_PERM_MOUNT "mount"
3741 #define ZFS_DELEG_PERM_SHARE "share"
3742 #define ZFS_DELEG_PERM_SEND "send"
3743 #define ZFS_DELEG_PERM_RECEIVE "receive"
3744 #define ZFS_DELEG_PERM_ALLOW "allow"
3745 #define ZFS_DELEG_PERM_USERPROP "userprop"
3746 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3747 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3748 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3749 #define ZFS_DELEG_PERM_USERUSED "userused"
3750 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3751 #define ZFS_DELEG_PERM_HOLD "hold"
3752 #define ZFS_DELEG_PERM_RELEASE "release"
3753 #define ZFS_DELEG_PERM_DIFF "diff"
3755 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3757 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
3758 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
3759 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
3760 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
3761 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
3762 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
3763 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
3764 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
3765 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
3766 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
3767 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
3768 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
3769 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
3770 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
3771 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
3772 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
3774 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
3775 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
3776 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
3777 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
3778 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
3779 { NULL
, ZFS_DELEG_NOTE_NONE
}
3782 /* permission structure */
3783 typedef struct deleg_perm
{
3784 zfs_deleg_who_type_t dp_who_type
;
3785 const char *dp_name
;
3787 boolean_t dp_descend
;
3791 typedef struct deleg_perm_node
{
3792 deleg_perm_t dpn_perm
;
3794 uu_avl_node_t dpn_avl_node
;
3795 } deleg_perm_node_t
;
3797 typedef struct fs_perm fs_perm_t
;
3799 /* permissions set */
3800 typedef struct who_perm
{
3801 zfs_deleg_who_type_t who_type
;
3802 const char *who_name
; /* id */
3803 char who_ug_name
[256]; /* user/group name */
3804 fs_perm_t
*who_fsperm
; /* uplink */
3806 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
3810 typedef struct who_perm_node
{
3811 who_perm_t who_perm
;
3812 uu_avl_node_t who_avl_node
;
3815 typedef struct fs_perm_set fs_perm_set_t
;
3816 /* fs permissions */
3818 const char *fsp_name
;
3820 uu_avl_t
*fsp_sc_avl
; /* sets,create */
3821 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
3823 fs_perm_set_t
*fsp_set
; /* uplink */
3827 typedef struct fs_perm_node
{
3828 fs_perm_t fspn_fsperm
;
3831 uu_list_node_t fspn_list_node
;
3834 /* top level structure */
3835 struct fs_perm_set
{
3836 uu_list_pool_t
*fsps_list_pool
;
3837 uu_list_t
*fsps_list
; /* list of fs_perms */
3839 uu_avl_pool_t
*fsps_named_set_avl_pool
;
3840 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
3841 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
3844 static inline const char *
3845 deleg_perm_type(zfs_deleg_note_t note
)
3851 case ZFS_DELEG_NOTE_GROUPQUOTA
:
3852 case ZFS_DELEG_NOTE_GROUPUSED
:
3853 case ZFS_DELEG_NOTE_USERPROP
:
3854 case ZFS_DELEG_NOTE_USERQUOTA
:
3855 case ZFS_DELEG_NOTE_USERUSED
:
3857 return (gettext("other"));
3859 return (gettext("subcommand"));
3864 who_type2weight(zfs_deleg_who_type_t who_type
)
3868 case ZFS_DELEG_NAMED_SET_SETS
:
3869 case ZFS_DELEG_NAMED_SET
:
3872 case ZFS_DELEG_CREATE_SETS
:
3873 case ZFS_DELEG_CREATE
:
3876 case ZFS_DELEG_USER_SETS
:
3877 case ZFS_DELEG_USER
:
3880 case ZFS_DELEG_GROUP_SETS
:
3881 case ZFS_DELEG_GROUP
:
3884 case ZFS_DELEG_EVERYONE_SETS
:
3885 case ZFS_DELEG_EVERYONE
:
3897 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
3899 const who_perm_node_t
*l
= larg
;
3900 const who_perm_node_t
*r
= rarg
;
3901 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
3902 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
3903 int lweight
= who_type2weight(ltype
);
3904 int rweight
= who_type2weight(rtype
);
3905 int res
= lweight
- rweight
;
3907 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
3908 ZFS_MAX_DELEG_NAME
-1);
3920 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
3922 const deleg_perm_node_t
*l
= larg
;
3923 const deleg_perm_node_t
*r
= rarg
;
3924 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
3925 ZFS_MAX_DELEG_NAME
-1);
3937 fs_perm_set_init(fs_perm_set_t
*fspset
)
3939 bzero(fspset
, sizeof (fs_perm_set_t
));
3941 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
3942 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
3943 NULL
, UU_DEFAULT
)) == NULL
)
3945 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
3946 UU_DEFAULT
)) == NULL
)
3949 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
3950 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
3951 who_perm_node_t
, who_avl_node
), who_perm_compare
,
3952 UU_DEFAULT
)) == NULL
)
3955 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
3956 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
3957 who_perm_node_t
, who_avl_node
), who_perm_compare
,
3958 UU_DEFAULT
)) == NULL
)
3961 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
3962 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
3963 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
3968 static inline void fs_perm_fini(fs_perm_t
*);
3969 static inline void who_perm_fini(who_perm_t
*);
3972 fs_perm_set_fini(fs_perm_set_t
*fspset
)
3974 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
3976 while (node
!= NULL
) {
3977 fs_perm_node_t
*next_node
=
3978 uu_list_next(fspset
->fsps_list
, node
);
3979 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
3980 fs_perm_fini(fsperm
);
3981 uu_list_remove(fspset
->fsps_list
, node
);
3986 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
3987 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
3988 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
3992 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
3995 deleg_perm
->dp_who_type
= type
;
3996 deleg_perm
->dp_name
= name
;
4000 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4001 zfs_deleg_who_type_t type
, const char *name
)
4003 uu_avl_pool_t
*pool
;
4004 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4006 bzero(who_perm
, sizeof (who_perm_t
));
4008 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4009 UU_DEFAULT
)) == NULL
)
4012 who_perm
->who_type
= type
;
4013 who_perm
->who_name
= name
;
4014 who_perm
->who_fsperm
= fsperm
;
4018 who_perm_fini(who_perm_t
*who_perm
)
4020 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4022 while (node
!= NULL
) {
4023 deleg_perm_node_t
*next_node
=
4024 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4026 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4031 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4035 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4037 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4038 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4040 bzero(fsperm
, sizeof (fs_perm_t
));
4042 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4046 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4050 fsperm
->fsp_set
= fspset
;
4051 fsperm
->fsp_name
= fsname
;
4055 fs_perm_fini(fs_perm_t
*fsperm
)
4057 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4058 while (node
!= NULL
) {
4059 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4061 who_perm_t
*who_perm
= &node
->who_perm
;
4062 who_perm_fini(who_perm
);
4063 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4068 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4069 while (node
!= NULL
) {
4070 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4072 who_perm_t
*who_perm
= &node
->who_perm
;
4073 who_perm_fini(who_perm
);
4074 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4079 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4080 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4084 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4085 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4087 uu_avl_index_t idx
= 0;
4089 deleg_perm_node_t
*found_node
= NULL
;
4090 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4092 deleg_perm_init(deleg_perm
, who_type
, name
);
4094 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4096 uu_avl_insert(avl
, node
, idx
);
4099 deleg_perm
= &node
->dpn_perm
;
4104 case ZFS_DELEG_LOCAL
:
4105 deleg_perm
->dp_local
= B_TRUE
;
4107 case ZFS_DELEG_DESCENDENT
:
4108 deleg_perm
->dp_descend
= B_TRUE
;
4113 assert(B_FALSE
); /* invalid locality */
4118 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4120 nvpair_t
*nvp
= NULL
;
4121 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4122 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4123 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4125 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4126 const char *name
= nvpair_name(nvp
);
4127 data_type_t type
= nvpair_type(nvp
);
4128 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4129 deleg_perm_node_t
*node
=
4130 safe_malloc(sizeof (deleg_perm_node_t
));
4132 VERIFY(type
== DATA_TYPE_BOOLEAN
);
4134 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4135 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4142 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4144 nvpair_t
*nvp
= NULL
;
4145 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4147 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4148 nvlist_t
*nvl2
= NULL
;
4149 const char *name
= nvpair_name(nvp
);
4150 uu_avl_t
*avl
= NULL
;
4151 uu_avl_pool_t
*avl_pool
= NULL
;
4152 zfs_deleg_who_type_t perm_type
= name
[0];
4153 char perm_locality
= name
[1];
4154 const char *perm_name
= name
+ 3;
4155 boolean_t is_set
= B_TRUE
;
4156 who_perm_t
*who_perm
= NULL
;
4158 assert('$' == name
[2]);
4160 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4163 switch (perm_type
) {
4164 case ZFS_DELEG_CREATE
:
4165 case ZFS_DELEG_CREATE_SETS
:
4166 case ZFS_DELEG_NAMED_SET
:
4167 case ZFS_DELEG_NAMED_SET_SETS
:
4168 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4169 avl
= fsperm
->fsp_sc_avl
;
4171 case ZFS_DELEG_USER
:
4172 case ZFS_DELEG_USER_SETS
:
4173 case ZFS_DELEG_GROUP
:
4174 case ZFS_DELEG_GROUP_SETS
:
4175 case ZFS_DELEG_EVERYONE
:
4176 case ZFS_DELEG_EVERYONE_SETS
:
4177 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4178 avl
= fsperm
->fsp_uge_avl
;
4185 who_perm_node_t
*found_node
= NULL
;
4186 who_perm_node_t
*node
= safe_malloc(
4187 sizeof (who_perm_node_t
));
4188 who_perm
= &node
->who_perm
;
4189 uu_avl_index_t idx
= 0;
4191 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4192 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4194 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4196 if (avl
== fsperm
->fsp_uge_avl
) {
4198 struct passwd
*p
= NULL
;
4199 struct group
*g
= NULL
;
4200 const char *nice_name
= NULL
;
4202 switch (perm_type
) {
4203 case ZFS_DELEG_USER_SETS
:
4204 case ZFS_DELEG_USER
:
4205 rid
= atoi(perm_name
);
4208 nice_name
= p
->pw_name
;
4210 case ZFS_DELEG_GROUP_SETS
:
4211 case ZFS_DELEG_GROUP
:
4212 rid
= atoi(perm_name
);
4215 nice_name
= g
->gr_name
;
4221 if (nice_name
!= NULL
)
4223 node
->who_perm
.who_ug_name
,
4227 uu_avl_insert(avl
, node
, idx
);
4230 who_perm
= &node
->who_perm
;
4234 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4241 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4243 nvpair_t
*nvp
= NULL
;
4244 uu_avl_index_t idx
= 0;
4246 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4247 nvlist_t
*nvl2
= NULL
;
4248 const char *fsname
= nvpair_name(nvp
);
4249 data_type_t type
= nvpair_type(nvp
);
4250 fs_perm_t
*fsperm
= NULL
;
4251 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4255 fsperm
= &node
->fspn_fsperm
;
4257 VERIFY(DATA_TYPE_NVLIST
== type
);
4259 uu_list_node_init(node
, &node
->fspn_list_node
,
4260 fspset
->fsps_list_pool
);
4262 idx
= uu_list_numnodes(fspset
->fsps_list
);
4263 fs_perm_init(fsperm
, fspset
, fsname
);
4265 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4268 (void) parse_fs_perm(fsperm
, nvl2
);
4270 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4276 static inline const char *
4277 deleg_perm_comment(zfs_deleg_note_t note
)
4279 const char *str
= "";
4284 case ZFS_DELEG_NOTE_ALLOW
:
4285 str
= gettext("Must also have the permission that is being"
4286 "\n\t\t\t\tallowed");
4288 case ZFS_DELEG_NOTE_CLONE
:
4289 str
= gettext("Must also have the 'create' ability and 'mount'"
4290 "\n\t\t\t\tability in the origin file system");
4292 case ZFS_DELEG_NOTE_CREATE
:
4293 str
= gettext("Must also have the 'mount' ability");
4295 case ZFS_DELEG_NOTE_DESTROY
:
4296 str
= gettext("Must also have the 'mount' ability");
4298 case ZFS_DELEG_NOTE_DIFF
:
4299 str
= gettext("Allows lookup of paths within a dataset;"
4300 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4301 "\n\t\t\t\tin order to use zfs diff");
4303 case ZFS_DELEG_NOTE_HOLD
:
4304 str
= gettext("Allows adding a user hold to a snapshot");
4306 case ZFS_DELEG_NOTE_MOUNT
:
4307 str
= gettext("Allows mount/umount of ZFS datasets");
4309 case ZFS_DELEG_NOTE_PROMOTE
:
4310 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4311 " 'promote' ability in the origin file system");
4313 case ZFS_DELEG_NOTE_RECEIVE
:
4314 str
= gettext("Must also have the 'mount' and 'create'"
4317 case ZFS_DELEG_NOTE_RELEASE
:
4318 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4319 "might destroy the snapshot");
4321 case ZFS_DELEG_NOTE_RENAME
:
4322 str
= gettext("Must also have the 'mount' and 'create'"
4323 "\n\t\t\t\tability in the new parent");
4325 case ZFS_DELEG_NOTE_ROLLBACK
:
4328 case ZFS_DELEG_NOTE_SEND
:
4331 case ZFS_DELEG_NOTE_SHARE
:
4332 str
= gettext("Allows sharing file systems over NFS or SMB"
4333 "\n\t\t\t\tprotocols");
4335 case ZFS_DELEG_NOTE_SNAPSHOT
:
4339 * case ZFS_DELEG_NOTE_VSCAN:
4340 * str = gettext("");
4344 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4345 str
= gettext("Allows accessing any groupquota@... property");
4347 case ZFS_DELEG_NOTE_GROUPUSED
:
4348 str
= gettext("Allows reading any groupused@... property");
4350 case ZFS_DELEG_NOTE_USERPROP
:
4351 str
= gettext("Allows changing any user property");
4353 case ZFS_DELEG_NOTE_USERQUOTA
:
4354 str
= gettext("Allows accessing any userquota@... property");
4356 case ZFS_DELEG_NOTE_USERUSED
:
4357 str
= gettext("Allows reading any userused@... property");
4375 boolean_t recursive
; /* unallow only */
4376 boolean_t prt_usage
;
4378 boolean_t prt_perms
;
4381 const char *dataset
;
4385 prop_cmp(const void *a
, const void *b
)
4387 const char *str1
= *(const char **)a
;
4388 const char *str2
= *(const char **)b
;
4389 return (strcmp(str1
, str2
));
4393 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
4395 const char *opt_desc
[] = {
4396 "-h", gettext("show this help message and exit"),
4397 "-l", gettext("set permission locally"),
4398 "-d", gettext("set permission for descents"),
4399 "-u", gettext("set permission for user"),
4400 "-g", gettext("set permission for group"),
4401 "-e", gettext("set permission for everyone"),
4402 "-c", gettext("set create time permission"),
4403 "-s", gettext("define permission set"),
4405 "-r", gettext("remove permissions recursively"),
4407 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
4408 size_t allow_size
= unallow_size
- 2;
4409 const char *props
[ZFS_NUM_PROPS
];
4412 FILE *fp
= requested
? stdout
: stderr
;
4413 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
4414 const char *fmt
= gettext("%-16s %-14s\t%s\n");
4416 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
4418 (void) fprintf(fp
, gettext("Options:\n"));
4419 for (i
= 0; i
< (un
? unallow_size
: allow_size
); i
++) {
4420 const char *opt
= opt_desc
[i
++];
4421 const char *optdsc
= opt_desc
[i
];
4422 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
4425 (void) fprintf(fp
, gettext("\nThe following permissions are "
4427 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
4429 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
4430 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
4431 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
4432 const char *perm_type
= deleg_perm_type(perm_note
);
4433 const char *perm_comment
= deleg_perm_comment(perm_note
);
4434 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
4437 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
4438 zprop_desc_t
*pd
= &pdtbl
[i
];
4439 if (pd
->pd_visible
!= B_TRUE
)
4442 if (pd
->pd_attr
== PROP_READONLY
)
4445 props
[count
++] = pd
->pd_name
;
4447 props
[count
] = NULL
;
4449 qsort(props
, count
, sizeof (char *), prop_cmp
);
4451 for (i
= 0; i
< count
; i
++)
4452 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
4455 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
4457 exit(requested
? 0 : 2);
4460 static inline const char *
4461 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
4464 if (un
&& argc
== expected_argc
- 1)
4466 else if (argc
== expected_argc
)
4467 *permsp
= argv
[argc
- 2];
4469 allow_usage(un
, B_FALSE
,
4470 gettext("wrong number of parameters\n"));
4472 return (argv
[argc
- 1]);
4476 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
4478 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
4479 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
4480 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
4481 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
4484 allow_usage(un
, B_FALSE
,
4485 gettext("-u, -g, and -e are mutually exclusive\n"));
4487 if (opts
->prt_usage
) {
4488 if (argc
== 0 && all_sum
== 0)
4489 allow_usage(un
, B_TRUE
, NULL
);
4496 allow_usage(un
, B_FALSE
,
4497 gettext("invalid options combined with -s\n"));
4499 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4500 if (argv
[0][0] != '@')
4501 allow_usage(un
, B_FALSE
,
4502 gettext("invalid set name: missing '@' prefix\n"));
4503 opts
->who
= argv
[0];
4504 } else if (opts
->create
) {
4505 if (ldcsuge_sum
> 1)
4506 allow_usage(un
, B_FALSE
,
4507 gettext("invalid options combined with -c\n"));
4508 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4509 } else if (opts
->everyone
) {
4511 allow_usage(un
, B_FALSE
,
4512 gettext("invalid options combined with -e\n"));
4513 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4514 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
4516 opts
->everyone
= B_TRUE
;
4519 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4520 } else if (argc
== 1 && !un
) {
4521 opts
->prt_perms
= B_TRUE
;
4522 opts
->dataset
= argv
[argc
-1];
4524 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4525 opts
->who
= argv
[0];
4528 if (!opts
->local
&& !opts
->descend
) {
4529 opts
->local
= B_TRUE
;
4530 opts
->descend
= B_TRUE
;
4535 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
4536 const char *who
, char *perms
, nvlist_t
*top_nvl
)
4539 char ld
[2] = { '\0', '\0' };
4540 char who_buf
[ZFS_MAXNAMELEN
+32];
4541 char base_type
= ZFS_DELEG_WHO_UNKNOWN
;
4542 char set_type
= ZFS_DELEG_WHO_UNKNOWN
;
4543 nvlist_t
*base_nvl
= NULL
;
4544 nvlist_t
*set_nvl
= NULL
;
4547 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4549 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4553 case ZFS_DELEG_NAMED_SET_SETS
:
4554 case ZFS_DELEG_NAMED_SET
:
4555 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
4556 base_type
= ZFS_DELEG_NAMED_SET
;
4557 ld
[0] = ZFS_DELEG_NA
;
4559 case ZFS_DELEG_CREATE_SETS
:
4560 case ZFS_DELEG_CREATE
:
4561 set_type
= ZFS_DELEG_CREATE_SETS
;
4562 base_type
= ZFS_DELEG_CREATE
;
4563 ld
[0] = ZFS_DELEG_NA
;
4565 case ZFS_DELEG_USER_SETS
:
4566 case ZFS_DELEG_USER
:
4567 set_type
= ZFS_DELEG_USER_SETS
;
4568 base_type
= ZFS_DELEG_USER
;
4570 ld
[0] = ZFS_DELEG_LOCAL
;
4572 ld
[1] = ZFS_DELEG_DESCENDENT
;
4574 case ZFS_DELEG_GROUP_SETS
:
4575 case ZFS_DELEG_GROUP
:
4576 set_type
= ZFS_DELEG_GROUP_SETS
;
4577 base_type
= ZFS_DELEG_GROUP
;
4579 ld
[0] = ZFS_DELEG_LOCAL
;
4581 ld
[1] = ZFS_DELEG_DESCENDENT
;
4583 case ZFS_DELEG_EVERYONE_SETS
:
4584 case ZFS_DELEG_EVERYONE
:
4585 set_type
= ZFS_DELEG_EVERYONE_SETS
;
4586 base_type
= ZFS_DELEG_EVERYONE
;
4588 ld
[0] = ZFS_DELEG_LOCAL
;
4590 ld
[1] = ZFS_DELEG_DESCENDENT
;
4595 if (perms
!= NULL
) {
4597 char *end
= curr
+ strlen(perms
);
4599 while (curr
< end
) {
4600 char *delim
= strchr(curr
, ',');
4611 (void) nvlist_add_boolean(nvl
, curr
);
4617 for (i
= 0; i
< 2; i
++) {
4618 char locality
= ld
[i
];
4622 if (!nvlist_empty(base_nvl
)) {
4624 (void) snprintf(who_buf
,
4625 sizeof (who_buf
), "%c%c$%s",
4626 base_type
, locality
, who
);
4628 (void) snprintf(who_buf
,
4629 sizeof (who_buf
), "%c%c$",
4630 base_type
, locality
);
4632 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
4637 if (!nvlist_empty(set_nvl
)) {
4639 (void) snprintf(who_buf
,
4640 sizeof (who_buf
), "%c%c$%s",
4641 set_type
, locality
, who
);
4643 (void) snprintf(who_buf
,
4644 sizeof (who_buf
), "%c%c$",
4645 set_type
, locality
);
4647 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
4652 for (i
= 0; i
< 2; i
++) {
4653 char locality
= ld
[i
];
4658 (void) snprintf(who_buf
, sizeof (who_buf
),
4659 "%c%c$%s", base_type
, locality
, who
);
4661 (void) snprintf(who_buf
, sizeof (who_buf
),
4662 "%c%c$", base_type
, locality
);
4663 (void) nvlist_add_boolean(top_nvl
, who_buf
);
4666 (void) snprintf(who_buf
, sizeof (who_buf
),
4667 "%c%c$%s", set_type
, locality
, who
);
4669 (void) snprintf(who_buf
, sizeof (who_buf
),
4670 "%c%c$", set_type
, locality
);
4671 (void) nvlist_add_boolean(top_nvl
, who_buf
);
4677 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
4679 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
4683 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
4684 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
4685 } else if (opts
->create
) {
4686 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
4687 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
4688 } else if (opts
->everyone
) {
4689 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
4690 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
4692 char *curr
= opts
->who
;
4693 char *end
= curr
+ strlen(curr
);
4695 while (curr
< end
) {
4697 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
4699 char *delim
= strchr(curr
, ',');
4702 struct passwd
*p
= NULL
;
4703 struct group
*g
= NULL
;
4711 rid
= (uid_t
)strtol(curr
, &endch
, 0);
4713 who_type
= ZFS_DELEG_USER
;
4722 (void) snprintf(errbuf
, 256, gettext(
4723 "invalid user %s"), curr
);
4724 allow_usage(un
, B_TRUE
, errbuf
);
4726 } else if (opts
->group
) {
4727 who_type
= ZFS_DELEG_GROUP
;
4736 (void) snprintf(errbuf
, 256, gettext(
4737 "invalid group %s"), curr
);
4738 allow_usage(un
, B_TRUE
, errbuf
);
4741 if (*endch
!= '\0') {
4748 if (*endch
!= '\0') {
4756 who_type
= ZFS_DELEG_USER
;
4758 } else if (g
!= NULL
) {
4759 who_type
= ZFS_DELEG_GROUP
;
4762 (void) snprintf(errbuf
, 256, gettext(
4763 "invalid user/group %s"), curr
);
4764 allow_usage(un
, B_TRUE
, errbuf
);
4768 (void) sprintf(id
, "%u", rid
);
4771 store_allow_perm(who_type
, opts
->local
,
4772 opts
->descend
, who
, opts
->perms
, *nvlp
);
4781 print_set_creat_perms(uu_avl_t
*who_avl
)
4783 const char *sc_title
[] = {
4784 gettext("Permission sets:\n"),
4785 gettext("Create time permissions:\n"),
4788 const char **title_ptr
= sc_title
;
4789 who_perm_node_t
*who_node
= NULL
;
4790 int prev_weight
= -1;
4792 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
4793 who_node
= uu_avl_next(who_avl
, who_node
)) {
4794 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
4795 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
4796 const char *who_name
= who_node
->who_perm
.who_name
;
4797 int weight
= who_type2weight(who_type
);
4798 boolean_t first
= B_TRUE
;
4799 deleg_perm_node_t
*deleg_node
;
4801 if (prev_weight
!= weight
) {
4802 (void) printf("%s", *title_ptr
++);
4803 prev_weight
= weight
;
4806 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
4807 (void) printf("\t");
4809 (void) printf("\t%s ", who_name
);
4811 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
4812 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
4815 deleg_node
->dpn_perm
.dp_name
);
4818 (void) printf(",%s",
4819 deleg_node
->dpn_perm
.dp_name
);
4822 (void) printf("\n");
4827 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
4830 who_perm_node_t
*who_node
= NULL
;
4831 boolean_t prt_title
= B_TRUE
;
4832 uu_avl_walk_t
*walk
;
4834 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
4837 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
4838 const char *who_name
= who_node
->who_perm
.who_name
;
4839 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
4840 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
4841 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
4843 deleg_perm_node_t
*deleg_node
;
4844 boolean_t prt_who
= B_TRUE
;
4846 for (deleg_node
= uu_avl_first(avl
);
4848 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
4849 if (local
!= deleg_node
->dpn_perm
.dp_local
||
4850 descend
!= deleg_node
->dpn_perm
.dp_descend
)
4854 const char *who
= NULL
;
4856 prt_title
= B_FALSE
;
4857 (void) printf("%s", title
);
4861 case ZFS_DELEG_USER_SETS
:
4862 case ZFS_DELEG_USER
:
4863 who
= gettext("user");
4865 who_name
= nice_who_name
;
4867 case ZFS_DELEG_GROUP_SETS
:
4868 case ZFS_DELEG_GROUP
:
4869 who
= gettext("group");
4871 who_name
= nice_who_name
;
4873 case ZFS_DELEG_EVERYONE_SETS
:
4874 case ZFS_DELEG_EVERYONE
:
4875 who
= gettext("everyone");
4882 if (who_name
== NULL
)
4883 (void) printf("\t%s", who
);
4885 (void) printf("\t%s %s", who
, who_name
);
4888 (void) printf("%c%s", delim
,
4889 deleg_node
->dpn_perm
.dp_name
);
4894 (void) printf("\n");
4897 uu_avl_walk_end(walk
);
4901 print_fs_perms(fs_perm_set_t
*fspset
)
4903 fs_perm_node_t
*node
= NULL
;
4904 char buf
[ZFS_MAXNAMELEN
+32];
4905 const char *dsname
= buf
;
4907 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
4908 node
= uu_list_next(fspset
->fsps_list
, node
)) {
4909 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
4910 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
4913 (void) snprintf(buf
, ZFS_MAXNAMELEN
+32,
4914 gettext("---- Permissions on %s "),
4915 node
->fspn_fsperm
.fsp_name
);
4916 (void) printf("%s", dsname
);
4917 left
= 70 - strlen(buf
);
4920 (void) printf("\n");
4922 print_set_creat_perms(sc_avl
);
4923 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
4924 gettext("Local permissions:\n"));
4925 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
4926 gettext("Descendent permissions:\n"));
4927 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
4928 gettext("Local+Descendent permissions:\n"));
4932 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
4934 struct deleg_perms
{
4940 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
4942 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
4943 zfs_type_t zfs_type
= zfs_get_type(zhp
);
4945 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
4948 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
4952 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
4955 nvlist_t
*perm_nvl
= NULL
;
4956 nvlist_t
*update_perm_nvl
= NULL
;
4959 struct allow_opts opts
= { 0 };
4961 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
4964 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
4967 opts
.local
= B_TRUE
;
4970 opts
.descend
= B_TRUE
;
4976 opts
.group
= B_TRUE
;
4979 opts
.everyone
= B_TRUE
;
4985 opts
.create
= B_TRUE
;
4988 opts
.recursive
= B_TRUE
;
4991 (void) fprintf(stderr
, gettext("missing argument for "
4992 "'%c' option\n"), optopt
);
4996 opts
.prt_usage
= B_TRUE
;
4999 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5008 /* check arguments */
5009 parse_allow_args(argc
, argv
, un
, &opts
);
5011 /* try to open the dataset */
5012 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5013 ZFS_TYPE_VOLUME
)) == NULL
) {
5014 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5019 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5022 fs_perm_set_init(&fs_perm_set
);
5023 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5024 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5029 print_fs_perms(&fs_perm_set
);
5031 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5032 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5035 if (un
&& opts
.recursive
) {
5036 struct deleg_perms data
= { un
, update_perm_nvl
};
5037 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5046 nvlist_free(perm_nvl
);
5047 if (update_perm_nvl
!= NULL
)
5048 nvlist_free(update_perm_nvl
);
5050 fs_perm_set_fini(&fs_perm_set
);
5058 * zfs allow [-r] [-t] <tag> <snap> ...
5060 * -r Recursively hold
5061 * -t Temporary hold (hidden option)
5063 * Apply a user-hold with the given tag to the list of snapshots.
5066 zfs_do_allow(int argc
, char **argv
)
5068 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5072 * zfs unallow [-r] [-t] <tag> <snap> ...
5074 * -r Recursively hold
5075 * -t Temporary hold (hidden option)
5077 * Apply a user-hold with the given tag to the list of snapshots.
5080 zfs_do_unallow(int argc
, char **argv
)
5082 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5086 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5091 boolean_t recursive
= B_FALSE
;
5092 boolean_t temphold
= B_FALSE
;
5093 const char *opts
= holding
? "rt" : "r";
5097 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5106 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5115 /* check number of arguments */
5123 if (holding
&& tag
[0] == '.') {
5124 /* tags starting with '.' are reserved for libzfs */
5125 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5129 for (i
= 0; i
< argc
; ++i
) {
5131 char parent
[ZFS_MAXNAMELEN
];
5133 char *path
= argv
[i
];
5135 delim
= strchr(path
, '@');
5136 if (delim
== NULL
) {
5137 (void) fprintf(stderr
,
5138 gettext("'%s' is not a snapshot\n"), path
);
5142 (void) strncpy(parent
, path
, delim
- path
);
5143 parent
[delim
- path
] = '\0';
5145 zhp
= zfs_open(g_zfs
, parent
,
5146 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5152 if (zfs_hold(zhp
, delim
+1, tag
, recursive
,
5153 temphold
, B_FALSE
, -1, 0, 0) != 0)
5156 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5162 return (errors
!= 0);
5166 * zfs hold [-r] [-t] <tag> <snap> ...
5168 * -r Recursively hold
5169 * -t Temporary hold (hidden option)
5171 * Apply a user-hold with the given tag to the list of snapshots.
5174 zfs_do_hold(int argc
, char **argv
)
5176 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5180 * zfs release [-r] <tag> <snap> ...
5182 * -r Recursively release
5184 * Release a user-hold with the given tag from the list of snapshots.
5187 zfs_do_release(int argc
, char **argv
)
5189 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5192 typedef struct holds_cbdata
{
5193 boolean_t cb_recursive
;
5194 const char *cb_snapname
;
5196 size_t cb_max_namelen
;
5197 size_t cb_max_taglen
;
5200 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5201 #define DATETIME_BUF_LEN (32)
5206 print_holds(boolean_t scripted
, int nwidth
, int tagwidth
, nvlist_t
*nvl
)
5209 nvpair_t
*nvp
= NULL
;
5210 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5214 for (i
= 0; i
< 3; i
++) {
5215 col
= gettext(hdr_cols
[i
]);
5217 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5220 (void) printf("%s\n", col
);
5224 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5225 char *zname
= nvpair_name(nvp
);
5227 nvpair_t
*nvp2
= NULL
;
5228 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5229 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5230 char tsbuf
[DATETIME_BUF_LEN
];
5231 char *tagname
= nvpair_name(nvp2
);
5235 char sep
= scripted
? '\t' : ' ';
5236 int sepnum
= scripted
? 1 : 2;
5238 (void) nvpair_value_uint64(nvp2
, &val
);
5240 (void) localtime_r(&time
, &t
);
5241 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5242 gettext(STRFTIME_FMT_STR
), &t
);
5244 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth
, zname
,
5245 sepnum
, sep
, tagwidth
, tagname
, sepnum
, sep
, tsbuf
);
5251 * Generic callback function to list a dataset or snapshot.
5254 holds_callback(zfs_handle_t
*zhp
, void *data
)
5256 holds_cbdata_t
*cbp
= data
;
5257 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5258 nvlist_t
*nvl
= NULL
;
5259 nvpair_t
*nvp
= NULL
;
5260 const char *zname
= zfs_get_name(zhp
);
5261 size_t znamelen
= strnlen(zname
, ZFS_MAXNAMELEN
);
5263 if (cbp
->cb_recursive
) {
5264 const char *snapname
;
5265 char *delim
= strchr(zname
, '@');
5269 snapname
= delim
+ 1;
5270 if (strcmp(cbp
->cb_snapname
, snapname
))
5274 if (zfs_get_holds(zhp
, &nvl
) != 0)
5277 if (znamelen
> cbp
->cb_max_namelen
)
5278 cbp
->cb_max_namelen
= znamelen
;
5280 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5281 const char *tag
= nvpair_name(nvp
);
5282 size_t taglen
= strnlen(tag
, MAXNAMELEN
);
5283 if (taglen
> cbp
->cb_max_taglen
)
5284 cbp
->cb_max_taglen
= taglen
;
5287 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5291 * zfs holds [-r] <snap> ...
5293 * -r Recursively hold
5296 zfs_do_holds(int argc
, char **argv
)
5301 boolean_t scripted
= B_FALSE
;
5302 boolean_t recursive
= B_FALSE
;
5303 const char *opts
= "rH";
5306 int types
= ZFS_TYPE_SNAPSHOT
;
5307 holds_cbdata_t cb
= { 0 };
5314 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5323 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5330 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
5331 flags
|= ZFS_ITER_RECURSE
;
5337 /* check number of arguments */
5341 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5344 for (i
= 0; i
< argc
; ++i
) {
5345 char *snapshot
= argv
[i
];
5347 const char *snapname
;
5349 delim
= strchr(snapshot
, '@');
5350 if (delim
== NULL
) {
5351 (void) fprintf(stderr
,
5352 gettext("'%s' is not a snapshot\n"), snapshot
);
5356 snapname
= delim
+ 1;
5358 snapshot
[delim
- snapshot
] = '\0';
5360 cb
.cb_recursive
= recursive
;
5361 cb
.cb_snapname
= snapname
;
5365 * 1. collect holds data, set format options
5367 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
5368 holds_callback
, &cb
);
5374 * 2. print holds data
5376 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
5378 if (nvlist_empty(nvl
))
5379 (void) fprintf(stderr
, gettext("no datasets available\n"));
5383 return (0 != errors
);
5386 #define CHECK_SPINNER 30
5387 #define SPINNER_TIME 3 /* seconds */
5388 #define MOUNT_TIME 5 /* seconds */
5391 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
5393 static char *spin
[] = { "-", "\\", "|", "/" };
5394 static int spinval
= 0;
5395 static int spincheck
= 0;
5396 static time_t last_spin_time
= (time_t)0;
5397 get_all_cb_t
*cbp
= data
;
5398 zfs_type_t type
= zfs_get_type(zhp
);
5400 if (cbp
->cb_verbose
) {
5401 if (--spincheck
< 0) {
5402 time_t now
= time(NULL
);
5403 if (last_spin_time
+ SPINNER_TIME
< now
) {
5404 update_progress(spin
[spinval
++ % 4]);
5405 last_spin_time
= now
;
5407 spincheck
= CHECK_SPINNER
;
5412 * Interate over any nested datasets.
5414 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
5420 * Skip any datasets whose type does not match.
5422 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
5426 libzfs_add_handle(cbp
, zhp
);
5427 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
5433 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
5435 get_all_cb_t cb
= { 0 };
5436 cb
.cb_verbose
= verbose
;
5437 cb
.cb_getone
= get_one_dataset
;
5440 set_progress_header(gettext("Reading ZFS config"));
5441 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
5443 *dslist
= cb
.cb_handles
;
5444 *count
= cb
.cb_used
;
5447 finish_progress(gettext("done."));
5451 * Generic callback for sharing or mounting filesystems. Because the code is so
5452 * similar, we have a common function with an extra parameter to determine which
5453 * mode we are using.
5455 #define OP_SHARE 0x1
5456 #define OP_MOUNT 0x2
5459 * Share or mount a dataset.
5462 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
5463 boolean_t
explicit, const char *options
)
5465 char mountpoint
[ZFS_MAXPROPLEN
];
5466 char shareopts
[ZFS_MAXPROPLEN
];
5467 char smbshareopts
[ZFS_MAXPROPLEN
];
5468 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
5470 uint64_t zoned
, canmount
;
5471 boolean_t shared_nfs
, shared_smb
;
5473 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
5476 * Check to make sure we can mount/share this dataset. If we
5477 * are in the global zone and the filesystem is exported to a
5478 * local zone, or if we are in a local zone and the
5479 * filesystem is not exported, then it is an error.
5481 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
5483 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
5487 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5488 "dataset is exported to a local zone\n"), cmdname
,
5492 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
5496 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5497 "permission denied\n"), cmdname
,
5503 * Ignore any filesystems which don't apply to us. This
5504 * includes those with a legacy mountpoint, or those with
5505 * legacy share options.
5507 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
5508 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
5509 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
5510 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5511 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
5512 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5514 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
5515 strcmp(smbshareopts
, "off") == 0) {
5519 (void) fprintf(stderr
, gettext("cannot share '%s': "
5520 "legacy share\n"), zfs_get_name(zhp
));
5521 (void) fprintf(stderr
, gettext("use share(1M) to "
5522 "share this filesystem, or set "
5523 "sharenfs property on\n"));
5528 * We cannot share or mount legacy filesystems. If the
5529 * shareopts is non-legacy but the mountpoint is legacy, we
5530 * treat it as a legacy share.
5532 if (strcmp(mountpoint
, "legacy") == 0) {
5536 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5537 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
5538 (void) fprintf(stderr
, gettext("use %s(1M) to "
5539 "%s this filesystem\n"), cmdname
, cmdname
);
5543 if (strcmp(mountpoint
, "none") == 0) {
5547 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
5548 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
5553 * canmount explicit outcome
5554 * on no pass through
5555 * on yes pass through
5557 * off yes display error, return 1
5558 * noauto no return 0
5559 * noauto yes pass through
5561 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
5562 if (canmount
== ZFS_CANMOUNT_OFF
) {
5566 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5567 "'canmount' property is set to 'off'\n"), cmdname
,
5570 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
5575 * At this point, we have verified that the mountpoint and/or
5576 * shareopts are appropriate for auto management. If the
5577 * filesystem is already mounted or shared, return (failing
5578 * for explicit requests); otherwise mount or share the
5584 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
5585 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
5587 if ((shared_nfs
&& shared_smb
) ||
5588 ((shared_nfs
&& strcmp(shareopts
, "on") == 0) &&
5589 (strcmp(smbshareopts
, "off") == 0)) ||
5590 ((shared_smb
&& strcmp(smbshareopts
, "on") == 0) &&
5591 (strcmp(shareopts
, "off") == 0))) {
5595 (void) fprintf(stderr
, gettext("cannot share "
5596 "'%s': filesystem already shared\n"),
5601 if (!zfs_is_mounted(zhp
, NULL
) &&
5602 zfs_mount(zhp
, NULL
, 0) != 0)
5605 if (protocol
== NULL
) {
5606 if (zfs_shareall(zhp
) != 0)
5608 } else if (strcmp(protocol
, "nfs") == 0) {
5609 if (zfs_share_nfs(zhp
))
5611 } else if (strcmp(protocol
, "smb") == 0) {
5612 if (zfs_share_smb(zhp
))
5615 (void) fprintf(stderr
, gettext("cannot share "
5616 "'%s': invalid share type '%s' "
5618 zfs_get_name(zhp
), protocol
);
5625 if (options
== NULL
)
5626 mnt
.mnt_mntopts
= "";
5628 mnt
.mnt_mntopts
= (char *)options
;
5630 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
5631 zfs_is_mounted(zhp
, NULL
)) {
5635 (void) fprintf(stderr
, gettext("cannot mount "
5636 "'%s': filesystem already mounted\n"),
5641 if (zfs_mount(zhp
, options
, flags
) != 0)
5650 * Reports progress in the form "(current/total)". Not thread-safe.
5653 report_mount_progress(int current
, int total
)
5655 static time_t last_progress_time
= 0;
5656 time_t now
= time(NULL
);
5659 /* report 1..n instead of 0..n-1 */
5662 /* display header if we're here for the first time */
5664 set_progress_header(gettext("Mounting ZFS filesystems"));
5665 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
5666 /* too soon to report again */
5670 last_progress_time
= now
;
5672 (void) sprintf(info
, "(%d/%d)", current
, total
);
5674 if (current
== total
)
5675 finish_progress(info
);
5677 update_progress(info
);
5681 append_options(char *mntopts
, char *newopts
)
5683 int len
= strlen(mntopts
);
5685 /* original length plus new string to append plus 1 for the comma */
5686 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
5687 (void) fprintf(stderr
, gettext("the opts argument for "
5688 "'%s' option is too long (more than %d chars)\n"),
5689 "-o", MNT_LINE_MAX
);
5694 mntopts
[len
++] = ',';
5696 (void) strcpy(&mntopts
[len
], newopts
);
5700 share_mount(int op
, int argc
, char **argv
)
5703 boolean_t verbose
= B_FALSE
;
5705 char *options
= NULL
;
5709 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
5719 if (*optarg
== '\0') {
5720 (void) fprintf(stderr
, gettext("empty mount "
5721 "options (-o) specified\n"));
5725 if (options
== NULL
)
5726 options
= safe_malloc(MNT_LINE_MAX
+ 1);
5728 /* option validation is done later */
5729 append_options(options
, optarg
);
5732 flags
|= MS_OVERLAY
;
5735 (void) fprintf(stderr
, gettext("missing argument for "
5736 "'%c' option\n"), optopt
);
5740 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5749 /* check number of arguments */
5751 zfs_handle_t
**dslist
= NULL
;
5752 size_t i
, count
= 0;
5753 char *protocol
= NULL
;
5755 if (op
== OP_SHARE
&& argc
> 0) {
5756 if (strcmp(argv
[0], "nfs") != 0 &&
5757 strcmp(argv
[0], "smb") != 0) {
5758 (void) fprintf(stderr
, gettext("share type "
5759 "must be 'nfs' or 'smb'\n"));
5768 (void) fprintf(stderr
, gettext("too many arguments\n"));
5772 start_progress_timer();
5773 get_all_datasets(&dslist
, &count
, verbose
);
5778 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
5780 for (i
= 0; i
< count
; i
++) {
5782 report_mount_progress(i
, count
);
5784 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
5785 B_FALSE
, options
) != 0)
5787 zfs_close(dslist
[i
]);
5791 } else if (argc
== 0) {
5792 struct mnttab entry
;
5794 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
5795 (void) fprintf(stderr
, gettext("missing filesystem "
5796 "argument (specify -a for all)\n"));
5801 * When mount is given no arguments, go through /etc/mtab and
5802 * display any active ZFS mounts. We hide any snapshots, since
5803 * they are controlled automatically.
5805 rewind(mnttab_file
);
5806 while (getmntent(mnttab_file
, &entry
) == 0) {
5807 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
5808 strchr(entry
.mnt_special
, '@') != NULL
)
5811 (void) printf("%-30s %s\n", entry
.mnt_special
,
5819 (void) fprintf(stderr
,
5820 gettext("too many arguments\n"));
5824 if ((zhp
= zfs_open(g_zfs
, argv
[0],
5825 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
5828 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
5838 * zfs mount -a [nfs]
5839 * zfs mount filesystem
5841 * Mount all filesystems, or mount the given filesystem.
5844 zfs_do_mount(int argc
, char **argv
)
5846 return (share_mount(OP_MOUNT
, argc
, argv
));
5850 * zfs share -a [nfs | smb]
5851 * zfs share filesystem
5853 * Share all filesystems, or share the given filesystem.
5856 zfs_do_share(int argc
, char **argv
)
5858 return (share_mount(OP_SHARE
, argc
, argv
));
5861 typedef struct unshare_unmount_node
{
5862 zfs_handle_t
*un_zhp
;
5864 uu_avl_node_t un_avlnode
;
5865 } unshare_unmount_node_t
;
5869 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
5871 const unshare_unmount_node_t
*l
= larg
;
5872 const unshare_unmount_node_t
*r
= rarg
;
5874 return (strcmp(l
->un_mountp
, r
->un_mountp
));
5878 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
5879 * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
5880 * and unmount it appropriately.
5883 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
5887 struct stat64 statbuf
;
5888 struct extmnttab entry
;
5889 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
5893 * Search for the path in /etc/mtab. Rather than looking for the
5894 * specific path, which can be fooled by non-standard paths (i.e. ".."
5895 * or "//"), we stat() the path and search for the corresponding
5896 * (major,minor) device pair.
5898 if (stat64(path
, &statbuf
) != 0) {
5899 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
5900 cmdname
, path
, strerror(errno
));
5903 path_inode
= statbuf
.st_ino
;
5906 * Search for the given (major,minor) pair in the mount table.
5908 rewind(mnttab_file
);
5909 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
5910 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
5911 entry
.mnt_minor
== minor(statbuf
.st_dev
))
5915 if (op
== OP_SHARE
) {
5916 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
5917 "currently mounted\n"), cmdname
, path
);
5920 (void) fprintf(stderr
, gettext("warning: %s not in mtab\n"),
5922 if ((ret
= umount2(path
, flags
)) != 0)
5923 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
5928 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
5929 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
5930 "filesystem\n"), cmdname
, path
);
5934 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
5935 ZFS_TYPE_FILESYSTEM
)) == NULL
)
5939 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
5940 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
5941 cmdname
, path
, strerror(errno
));
5943 } else if (statbuf
.st_ino
!= path_inode
) {
5944 (void) fprintf(stderr
, gettext("cannot "
5945 "%s '%s': not a mountpoint\n"), cmdname
, path
);
5949 if (op
== OP_SHARE
) {
5950 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
5951 char smbshare_prop
[ZFS_MAXPROPLEN
];
5953 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
5954 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
5955 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
5956 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
5958 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
5959 strcmp(smbshare_prop
, "off") == 0) {
5960 (void) fprintf(stderr
, gettext("cannot unshare "
5961 "'%s': legacy share\n"), path
);
5962 (void) fprintf(stderr
, gettext("use exportfs(8) "
5963 "or smbcontrol(1) to unshare this filesystem\n"));
5964 } else if (!zfs_is_shared(zhp
)) {
5965 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
5966 "not currently shared\n"), path
);
5968 ret
= zfs_unshareall_bypath(zhp
, path
);
5971 char mtpt_prop
[ZFS_MAXPROPLEN
];
5973 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
5974 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
5977 ret
= zfs_unmount(zhp
, NULL
, flags
);
5978 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
5979 (void) fprintf(stderr
, gettext("cannot unmount "
5980 "'%s': legacy mountpoint\n"),
5982 (void) fprintf(stderr
, gettext("use umount(8) "
5983 "to unmount this filesystem\n"));
5985 ret
= zfs_unmountall(zhp
, flags
);
5996 * Generic callback for unsharing or unmounting a filesystem.
5999 unshare_unmount(int op
, int argc
, char **argv
)
6006 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6007 char sharesmb
[ZFS_MAXPROPLEN
];
6010 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
6019 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6030 * We could make use of zfs_for_each() to walk all datasets in
6031 * the system, but this would be very inefficient, especially
6032 * since we would have to linearly search /etc/mtab for each
6033 * one. Instead, do one pass through /etc/mtab looking for
6034 * zfs entries and call zfs_unmount() for each one.
6036 * Things get a little tricky if the administrator has created
6037 * mountpoints beneath other ZFS filesystems. In this case, we
6038 * have to unmount the deepest filesystems first. To accomplish
6039 * this, we place all the mountpoints in an AVL tree sorted by
6040 * the special type (dataset name), and walk the result in
6041 * reverse to make sure to get any snapshots first.
6043 struct mnttab entry
;
6044 uu_avl_pool_t
*pool
;
6045 uu_avl_t
*tree
= NULL
;
6046 unshare_unmount_node_t
*node
;
6048 uu_avl_walk_t
*walk
;
6051 (void) fprintf(stderr
, gettext("too many arguments\n"));
6055 if (((pool
= uu_avl_pool_create("unmount_pool",
6056 sizeof (unshare_unmount_node_t
),
6057 offsetof(unshare_unmount_node_t
, un_avlnode
),
6058 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6059 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6062 rewind(mnttab_file
);
6063 while (getmntent(mnttab_file
, &entry
) == 0) {
6065 /* ignore non-ZFS entries */
6066 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6069 /* ignore snapshots */
6070 if (strchr(entry
.mnt_special
, '@') != NULL
)
6073 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6074 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6081 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6083 sizeof (nfs_mnt_prop
),
6084 NULL
, NULL
, 0, B_FALSE
) == 0);
6085 if (strcmp(nfs_mnt_prop
, "off") != 0)
6087 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6089 sizeof (nfs_mnt_prop
),
6090 NULL
, NULL
, 0, B_FALSE
) == 0);
6091 if (strcmp(nfs_mnt_prop
, "off") == 0)
6095 /* Ignore legacy mounts */
6096 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6098 sizeof (nfs_mnt_prop
),
6099 NULL
, NULL
, 0, B_FALSE
) == 0);
6100 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6102 /* Ignore canmount=noauto mounts */
6103 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6104 ZFS_CANMOUNT_NOAUTO
)
6110 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6112 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6114 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6116 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6117 uu_avl_insert(tree
, node
, idx
);
6119 zfs_close(node
->un_zhp
);
6120 free(node
->un_mountp
);
6126 * Walk the AVL tree in reverse, unmounting each filesystem and
6127 * removing it from the AVL tree in the process.
6129 if ((walk
= uu_avl_walk_start(tree
,
6130 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6133 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6134 uu_avl_remove(tree
, node
);
6138 if (zfs_unshareall_bypath(node
->un_zhp
,
6139 node
->un_mountp
) != 0)
6144 if (zfs_unmount(node
->un_zhp
,
6145 node
->un_mountp
, flags
) != 0)
6150 zfs_close(node
->un_zhp
);
6151 free(node
->un_mountp
);
6155 uu_avl_walk_end(walk
);
6156 uu_avl_destroy(tree
);
6157 uu_avl_pool_destroy(pool
);
6162 (void) fprintf(stderr
,
6163 gettext("missing filesystem argument\n"));
6165 (void) fprintf(stderr
,
6166 gettext("too many arguments\n"));
6171 * We have an argument, but it may be a full path or a ZFS
6172 * filesystem. Pass full paths off to unmount_path() (shared by
6173 * manual_unmount), otherwise open the filesystem and pass to
6176 if (argv
[0][0] == '/')
6177 return (unshare_unmount_path(op
, argv
[0],
6180 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6181 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6184 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
6185 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
6186 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
6187 NULL
, 0, B_FALSE
) == 0);
6191 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6193 sizeof (nfs_mnt_prop
),
6194 NULL
, NULL
, 0, B_FALSE
) == 0);
6195 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6196 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
6199 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6200 strcmp(sharesmb
, "off") == 0) {
6201 (void) fprintf(stderr
, gettext("cannot "
6202 "unshare '%s': legacy share\n"),
6204 (void) fprintf(stderr
, gettext("use "
6205 "unshare(1M) to unshare this "
6208 } else if (!zfs_is_shared(zhp
)) {
6209 (void) fprintf(stderr
, gettext("cannot "
6210 "unshare '%s': not currently "
6211 "shared\n"), zfs_get_name(zhp
));
6213 } else if (zfs_unshareall(zhp
) != 0) {
6219 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
6220 (void) fprintf(stderr
, gettext("cannot "
6221 "unmount '%s': legacy "
6222 "mountpoint\n"), zfs_get_name(zhp
));
6223 (void) fprintf(stderr
, gettext("use "
6224 "umount(1M) to unmount this "
6227 } else if (!zfs_is_mounted(zhp
, NULL
)) {
6228 (void) fprintf(stderr
, gettext("cannot "
6229 "unmount '%s': not currently "
6233 } else if (zfs_unmountall(zhp
, flags
) != 0) {
6247 * zfs unmount filesystem
6249 * Unmount all filesystems, or a specific ZFS filesystem.
6252 zfs_do_unmount(int argc
, char **argv
)
6254 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
6259 * zfs unshare filesystem
6261 * Unshare all filesystems, or a specific ZFS filesystem.
6264 zfs_do_unshare(int argc
, char **argv
)
6266 return (unshare_unmount(OP_SHARE
, argc
, argv
));
6270 find_command_idx(char *command
, int *idx
)
6274 for (i
= 0; i
< NCOMMAND
; i
++) {
6275 if (command_table
[i
].name
== NULL
)
6278 if (strcmp(command
, command_table
[i
].name
) == 0) {
6287 zfs_do_diff(int argc
, char **argv
)
6291 char *tosnap
= NULL
;
6292 char *fromsnap
= NULL
;
6297 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
6300 flags
|= ZFS_DIFF_CLASSIFY
;
6303 flags
|= ZFS_DIFF_PARSEABLE
;
6306 flags
|= ZFS_DIFF_TIMESTAMP
;
6309 (void) fprintf(stderr
,
6310 gettext("invalid option '%c'\n"), optopt
);
6319 (void) fprintf(stderr
,
6320 gettext("must provide at least one snapshot name\n"));
6325 (void) fprintf(stderr
, gettext("too many arguments\n"));
6330 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
6333 if (*fromsnap
!= '@')
6334 copy
= strdup(fromsnap
);
6336 copy
= strdup(tosnap
);
6340 if ((atp
= strchr(copy
, '@')))
6343 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6349 * Ignore SIGPIPE so that the library can give us
6350 * information on any failure
6352 (void) sigignore(SIGPIPE
);
6354 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
6362 main(int argc
, char **argv
)
6368 (void) setlocale(LC_ALL
, "");
6369 (void) textdomain(TEXT_DOMAIN
);
6373 if ((mnttab_file
= fopen(MNTTAB
, "r")) == NULL
) {
6374 (void) fprintf(stderr
, gettext("internal error: unable to "
6375 "open %s\n"), MNTTAB
);
6380 * Make sure the user has specified some command.
6383 (void) fprintf(stderr
, gettext("missing command\n"));
6390 * The 'umount' command is an alias for 'unmount'
6392 if (strcmp(cmdname
, "umount") == 0)
6393 cmdname
= "unmount";
6396 * The 'recv' command is an alias for 'receive'
6398 if (strcmp(cmdname
, "recv") == 0)
6399 cmdname
= "receive";
6402 * The 'snap' command is an alias for 'snapshot'
6404 if (strcmp(cmdname
, "snap") == 0)
6405 cmdname
= "snapshot";
6410 if ((strcmp(cmdname
, "-?") == 0) ||
6411 (strcmp(cmdname
, "--help") == 0))
6414 if ((g_zfs
= libzfs_init()) == NULL
)
6417 zpool_set_history_str("zfs", argc
, argv
, history_str
);
6418 verify(zpool_stage_history(g_zfs
, history_str
) == 0);
6420 libzfs_print_on_error(g_zfs
, B_TRUE
);
6423 * Run the appropriate command.
6425 libzfs_mnttab_cache(g_zfs
, B_FALSE
);
6426 if (find_command_idx(cmdname
, &i
) == 0) {
6427 current_command
= &command_table
[i
];
6428 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
6429 } else if (strchr(cmdname
, '=') != NULL
) {
6430 verify(find_command_idx("set", &i
) == 0);
6431 current_command
= &command_table
[i
];
6432 ret
= command_table
[i
].func(argc
, argv
);
6434 (void) fprintf(stderr
, gettext("unrecognized "
6435 "command '%s'\n"), cmdname
);
6441 (void) fclose(mnttab_file
);
6444 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6445 * for the purposes of running ::findleaks.
6447 if (getenv("ZFS_ABORT") != NULL
) {
6448 (void) printf("dumping core by request\n");