4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28 * Copyright (c) 2013 Steven Hartland. All rights reserved.
29 * Copyright (c) 2014 Integros [integros.com]
30 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
31 * Copyright 2016 Nexenta Systems, Inc.
41 #include <libnvpair.h>
53 #include <sys/debug.h>
55 #include <sys/mkdev.h>
56 #include <sys/mntent.h>
57 #include <sys/mnttab.h>
58 #include <sys/mount.h>
60 #include <sys/fs/zfs.h>
61 #include <sys/types.h>
66 #include <libzfs_core.h>
68 #include <zfs_deleg.h>
71 #include <directory.h>
77 #include "zfs_comutil.h"
79 libzfs_handle_t
*g_zfs
;
81 static FILE *mnttab_file
;
82 static char history_str
[HIS_MAX_RECORD_LEN
];
83 static boolean_t log_history
= B_TRUE
;
85 static int zfs_do_clone(int argc
, char **argv
);
86 static int zfs_do_create(int argc
, char **argv
);
87 static int zfs_do_destroy(int argc
, char **argv
);
88 static int zfs_do_get(int argc
, char **argv
);
89 static int zfs_do_inherit(int argc
, char **argv
);
90 static int zfs_do_list(int argc
, char **argv
);
91 static int zfs_do_mount(int argc
, char **argv
);
92 static int zfs_do_rename(int argc
, char **argv
);
93 static int zfs_do_rollback(int argc
, char **argv
);
94 static int zfs_do_set(int argc
, char **argv
);
95 static int zfs_do_upgrade(int argc
, char **argv
);
96 static int zfs_do_snapshot(int argc
, char **argv
);
97 static int zfs_do_unmount(int argc
, char **argv
);
98 static int zfs_do_share(int argc
, char **argv
);
99 static int zfs_do_unshare(int argc
, char **argv
);
100 static int zfs_do_send(int argc
, char **argv
);
101 static int zfs_do_receive(int argc
, char **argv
);
102 static int zfs_do_promote(int argc
, char **argv
);
103 static int zfs_do_userspace(int argc
, char **argv
);
104 static int zfs_do_allow(int argc
, char **argv
);
105 static int zfs_do_unallow(int argc
, char **argv
);
106 static int zfs_do_hold(int argc
, char **argv
);
107 static int zfs_do_holds(int argc
, char **argv
);
108 static int zfs_do_release(int argc
, char **argv
);
109 static int zfs_do_diff(int argc
, char **argv
);
110 static int zfs_do_bookmark(int argc
, char **argv
);
111 static int zfs_do_remap(int argc
, char **argv
);
112 static int zfs_do_channel_program(int argc
, char **argv
);
115 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
120 _umem_debug_init(void)
122 return ("default,verbose"); /* $UMEM_DEBUG setting */
126 _umem_logging_init(void)
128 return ("fail,contents"); /* $UMEM_LOGGING setting */
161 HELP_CHANNEL_PROGRAM
,
164 typedef struct zfs_command
{
166 int (*func
)(int argc
, char **argv
);
171 * Master command table. Each ZFS command has a name, associated function, and
172 * usage message. The usage messages need to be internationalized, so we have
173 * to have a function to return the usage message based on a command index.
175 * These commands are organized according to how they are displayed in the usage
176 * message. An empty command (one with a NULL name) indicates an empty line in
177 * the generic usage message.
179 static zfs_command_t command_table
[] = {
180 { "create", zfs_do_create
, HELP_CREATE
},
181 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
183 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
184 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
185 { "clone", zfs_do_clone
, HELP_CLONE
},
186 { "promote", zfs_do_promote
, HELP_PROMOTE
},
187 { "rename", zfs_do_rename
, HELP_RENAME
},
188 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
189 { "program", zfs_do_channel_program
, HELP_CHANNEL_PROGRAM
},
191 { "list", zfs_do_list
, HELP_LIST
},
193 { "set", zfs_do_set
, HELP_SET
},
194 { "get", zfs_do_get
, HELP_GET
},
195 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
196 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
197 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
198 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
200 { "mount", zfs_do_mount
, HELP_MOUNT
},
201 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
202 { "share", zfs_do_share
, HELP_SHARE
},
203 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
205 { "send", zfs_do_send
, HELP_SEND
},
206 { "receive", zfs_do_receive
, HELP_RECEIVE
},
208 { "allow", zfs_do_allow
, HELP_ALLOW
},
210 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
212 { "hold", zfs_do_hold
, HELP_HOLD
},
213 { "holds", zfs_do_holds
, HELP_HOLDS
},
214 { "release", zfs_do_release
, HELP_RELEASE
},
215 { "diff", zfs_do_diff
, HELP_DIFF
},
216 { "remap", zfs_do_remap
, HELP_REMAP
},
219 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
221 zfs_command_t
*current_command
;
224 get_usage(zfs_help_t idx
)
228 return (gettext("\tclone [-p] [-o property=value] ... "
229 "<snapshot> <filesystem|volume>\n"));
231 return (gettext("\tcreate [-p] [-o property=value] ... "
233 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
234 "-V <size> <volume>\n"));
236 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
237 "\tdestroy [-dnpRrv] "
238 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
239 "\tdestroy <filesystem|volume>#<bookmark>\n"));
241 return (gettext("\tget [-rHp] [-d max] "
242 "[-o \"all\" | field[,...]]\n"
243 "\t [-t type[,...]] [-s source[,...]]\n"
244 "\t <\"all\" | property[,...]> "
245 "[filesystem|volume|snapshot|bookmark] ...\n"));
247 return (gettext("\tinherit [-rS] <property> "
248 "<filesystem|volume|snapshot> ...\n"));
250 return (gettext("\tupgrade [-v]\n"
251 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
253 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
254 "[-s property]...\n\t [-S property]... [-t type[,...]] "
255 "[filesystem|volume|snapshot] ...\n"));
257 return (gettext("\tmount\n"
258 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
260 return (gettext("\tpromote <clone-filesystem>\n"));
262 return (gettext("\treceive [-vnsFu] <filesystem|volume|"
264 "\treceive [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
266 "\treceive -A <filesystem|volume>\n"));
268 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
269 "<filesystem|volume|snapshot>\n"
270 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
271 "\trename -r <snapshot> <snapshot>\n"));
273 return (gettext("\trollback [-rRf] <snapshot>\n"));
275 return (gettext("\tsend [-DnPpRvLec] [-[iI] snapshot] "
277 "\tsend [-Le] [-i snapshot|bookmark] "
278 "<filesystem|volume|snapshot>\n"
279 "\tsend [-nvPe] -t <receive_resume_token>\n"));
281 return (gettext("\tset <property=value> ... "
282 "<filesystem|volume|snapshot> ...\n"));
284 return (gettext("\tshare <-a | filesystem>\n"));
286 return (gettext("\tsnapshot [-r] [-o property=value] ... "
287 "<filesystem|volume>@<snap> ...\n"));
289 return (gettext("\tunmount [-f] "
290 "<-a | filesystem|mountpoint>\n"));
292 return (gettext("\tunshare "
293 "<-a | filesystem|mountpoint>\n"));
295 return (gettext("\tallow <filesystem|volume>\n"
297 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
298 "\t <filesystem|volume>\n"
299 "\tallow [-ld] -e <perm|@setname>[,...] "
300 "<filesystem|volume>\n"
301 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
302 "\tallow -s @setname <perm|@setname>[,...] "
303 "<filesystem|volume>\n"));
305 return (gettext("\tunallow [-rldug] "
306 "<\"everyone\"|user|group>[,...]\n"
307 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
308 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
309 "<filesystem|volume>\n"
310 "\tunallow [-r] -c [<perm|@setname>[,...]] "
311 "<filesystem|volume>\n"
312 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
313 "<filesystem|volume>\n"));
315 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
317 "\t [-S field] ... [-t type[,...]] "
318 "<filesystem|snapshot>\n"));
319 case HELP_GROUPSPACE
:
320 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
322 "\t [-S field] ... [-t type[,...]] "
323 "<filesystem|snapshot>\n"));
325 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
327 return (gettext("\tholds [-r] <snapshot> ...\n"));
329 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
331 return (gettext("\tdiff [-FHt] <snapshot> "
332 "[snapshot|filesystem]\n"));
334 return (gettext("\tremap <filesystem | volume>\n"));
336 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
337 case HELP_CHANNEL_PROGRAM
:
338 return (gettext("\tprogram [-n] [-t <instruction limit>] "
339 "[-m <memory limit (b)>] <pool> <program file> "
350 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
355 * Utility function to guarantee malloc() success.
359 safe_malloc(size_t size
)
363 if ((data
= calloc(1, size
)) == NULL
)
370 safe_realloc(void *data
, size_t size
)
373 if ((newp
= realloc(data
, size
)) == NULL
) {
382 safe_strdup(char *str
)
384 char *dupstr
= strdup(str
);
393 * Callback routine that will print out information for each of
397 usage_prop_cb(int prop
, void *cb
)
401 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
403 if (zfs_prop_readonly(prop
))
404 (void) fprintf(fp
, " NO ");
406 (void) fprintf(fp
, "YES ");
408 if (zfs_prop_inheritable(prop
))
409 (void) fprintf(fp
, " YES ");
411 (void) fprintf(fp
, " NO ");
413 if (zfs_prop_values(prop
) == NULL
)
414 (void) fprintf(fp
, "-\n");
416 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
422 * Display usage message. If we're inside a command, display only the usage for
423 * that command. Otherwise, iterate over the entire command table and display
424 * a complete usage message.
427 usage(boolean_t requested
)
430 boolean_t show_properties
= B_FALSE
;
431 FILE *fp
= requested
? stdout
: stderr
;
433 if (current_command
== NULL
) {
435 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
437 gettext("where 'command' is one of the following:\n\n"));
439 for (i
= 0; i
< NCOMMAND
; i
++) {
440 if (command_table
[i
].name
== NULL
)
441 (void) fprintf(fp
, "\n");
443 (void) fprintf(fp
, "%s",
444 get_usage(command_table
[i
].usage
));
447 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
448 "pool/[dataset/]*dataset[@name]\n"));
450 (void) fprintf(fp
, gettext("usage:\n"));
451 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
454 if (current_command
!= NULL
&&
455 (strcmp(current_command
->name
, "set") == 0 ||
456 strcmp(current_command
->name
, "get") == 0 ||
457 strcmp(current_command
->name
, "inherit") == 0 ||
458 strcmp(current_command
->name
, "list") == 0))
459 show_properties
= B_TRUE
;
461 if (show_properties
) {
463 gettext("\nThe following properties are supported:\n"));
465 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
466 "PROPERTY", "EDIT", "INHERIT", "VALUES");
468 /* Iterate over all properties */
469 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
472 (void) fprintf(fp
, "\t%-15s ", "userused@...");
473 (void) fprintf(fp
, " NO NO <size>\n");
474 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
475 (void) fprintf(fp
, " NO NO <size>\n");
476 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
477 (void) fprintf(fp
, "YES NO <size> | none\n");
478 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
479 (void) fprintf(fp
, "YES NO <size> | none\n");
480 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
481 (void) fprintf(fp
, " NO NO <size>\n");
483 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
484 "with standard units such as K, M, G, etc.\n"));
485 (void) fprintf(fp
, gettext("\nUser-defined properties can "
486 "be specified by using a name containing a colon (:).\n"));
487 (void) fprintf(fp
, gettext("\nThe {user|group}{used|quota}@ "
488 "properties must be appended with\n"
489 "a user or group specifier of one of these forms:\n"
490 " POSIX name (eg: \"matt\")\n"
491 " POSIX id (eg: \"126829\")\n"
492 " SMB name@domain (eg: \"matt@sun\")\n"
493 " SMB SID (eg: \"S-1-234-567-89\")\n"));
496 gettext("\nFor the property list, run: %s\n"),
499 gettext("\nFor the delegated permission list, run: %s\n"),
500 "zfs allow|unallow");
504 * See comments at end of main().
506 if (getenv("ZFS_ABORT") != NULL
) {
507 (void) printf("dumping core by request\n");
511 exit(requested
? 0 : 2);
515 * Take a property=value argument string and add it to the given nvlist.
516 * Modifies the argument inplace.
519 parseprop(nvlist_t
*props
, char *propname
)
521 char *propval
, *strval
;
523 if ((propval
= strchr(propname
, '=')) == NULL
) {
524 (void) fprintf(stderr
, gettext("missing "
525 "'=' for property=value argument\n"));
530 if (nvlist_lookup_string(props
, propname
, &strval
) == 0) {
531 (void) fprintf(stderr
, gettext("property '%s' "
532 "specified multiple times\n"), propname
);
535 if (nvlist_add_string(props
, propname
, propval
) != 0)
541 parse_depth(char *opt
, int *flags
)
546 depth
= (int)strtol(opt
, &tmp
, 0);
548 (void) fprintf(stderr
,
549 gettext("%s is not an integer\n"), optarg
);
553 (void) fprintf(stderr
,
554 gettext("Depth can not be negative.\n"));
557 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
561 #define PROGRESS_DELAY 2 /* seconds */
563 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";
564 static time_t pt_begin
;
565 static char *pt_header
= NULL
;
566 static boolean_t pt_shown
;
569 start_progress_timer(void)
571 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
576 set_progress_header(char *header
)
578 assert(pt_header
== NULL
);
579 pt_header
= safe_strdup(header
);
581 (void) printf("%s: ", header
);
582 (void) fflush(stdout
);
587 update_progress(char *update
)
589 if (!pt_shown
&& time(NULL
) > pt_begin
) {
590 int len
= strlen(update
);
592 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
594 (void) fflush(stdout
);
596 } else if (pt_shown
) {
597 int len
= strlen(update
);
599 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
600 (void) fflush(stdout
);
605 finish_progress(char *done
)
608 (void) printf("%s\n", done
);
609 (void) fflush(stdout
);
616 * Check if the dataset is mountable and should be automatically mounted.
619 should_auto_mount(zfs_handle_t
*zhp
)
621 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, zfs_get_type(zhp
)))
623 return (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
);
627 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
629 * Given an existing dataset, create a writable copy whose initial contents
630 * are the same as the source. The newly created dataset maintains a
631 * dependency on the original; the original cannot be destroyed so long as
634 * The '-p' flag creates all the non-existing ancestors of the target first.
637 zfs_do_clone(int argc
, char **argv
)
639 zfs_handle_t
*zhp
= NULL
;
640 boolean_t parents
= B_FALSE
;
645 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
649 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
652 if (parseprop(props
, optarg
) != 0)
659 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
668 /* check number of arguments */
670 (void) fprintf(stderr
, gettext("missing source dataset "
675 (void) fprintf(stderr
, gettext("missing target dataset "
680 (void) fprintf(stderr
, gettext("too many arguments\n"));
684 /* open the source dataset */
685 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
688 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
691 * Now create the ancestors of the target dataset. If the
692 * target already exists and '-p' option was used we should not
695 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
698 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
703 ret
= zfs_clone(zhp
, argv
[1], props
);
705 /* create the mountpoint if necessary */
709 clone
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
712 * If the user doesn't want the dataset
713 * automatically mounted, then skip the mount/share
716 if (should_auto_mount(clone
)) {
717 if ((ret
= zfs_mount(clone
, NULL
, 0)) != 0) {
718 (void) fprintf(stderr
, gettext("clone "
719 "successfully created, "
720 "but not mounted\n"));
721 } else if ((ret
= zfs_share(clone
)) != 0) {
722 (void) fprintf(stderr
, gettext("clone "
723 "successfully created, "
724 "but not shared\n"));
745 * zfs create [-p] [-o prop=value] ... fs
746 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
748 * Create a new dataset. This command can be used to create filesystems
749 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
750 * For volumes, the user must specify a size to be used.
752 * The '-s' flag applies only to volumes, and indicates that we should not try
753 * to set the reservation for this volume. By default we set a reservation
754 * equal to the size for any volume. For pools with SPA_VERSION >=
755 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
757 * The '-p' flag creates all the non-existing ancestors of the target first.
760 zfs_do_create(int argc
, char **argv
)
762 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
763 zfs_handle_t
*zhp
= NULL
;
764 uint64_t volsize
= 0;
766 boolean_t noreserve
= B_FALSE
;
767 boolean_t bflag
= B_FALSE
;
768 boolean_t parents
= B_FALSE
;
773 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
777 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
780 type
= ZFS_TYPE_VOLUME
;
781 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
782 (void) fprintf(stderr
, gettext("bad volume "
783 "size '%s': %s\n"), optarg
,
784 libzfs_error_description(g_zfs
));
788 if (nvlist_add_uint64(props
,
789 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
798 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
799 (void) fprintf(stderr
, gettext("bad volume "
800 "block size '%s': %s\n"), optarg
,
801 libzfs_error_description(g_zfs
));
805 if (nvlist_add_uint64(props
,
806 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
811 if (parseprop(props
, optarg
) != 0)
818 (void) fprintf(stderr
, gettext("missing size "
822 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
828 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
829 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
830 "used when creating a volume\n"));
837 /* check number of arguments */
839 (void) fprintf(stderr
, gettext("missing %s argument\n"),
840 zfs_type_to_name(type
));
844 (void) fprintf(stderr
, gettext("too many arguments\n"));
848 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
849 zpool_handle_t
*zpool_handle
;
850 nvlist_t
*real_props
= NULL
;
851 uint64_t spa_version
;
853 zfs_prop_t resv_prop
;
857 if ((p
= strchr(argv
[0], '/')) != NULL
)
859 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
862 if (zpool_handle
== NULL
)
864 spa_version
= zpool_get_prop_int(zpool_handle
,
865 ZPOOL_PROP_VERSION
, NULL
);
866 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
867 resv_prop
= ZFS_PROP_REFRESERVATION
;
869 resv_prop
= ZFS_PROP_RESERVATION
;
871 (void) snprintf(msg
, sizeof (msg
),
872 gettext("cannot create '%s'"), argv
[0]);
873 if (props
&& (real_props
= zfs_valid_proplist(g_zfs
, type
,
874 props
, 0, NULL
, zpool_handle
, msg
)) == NULL
) {
875 zpool_close(zpool_handle
);
878 zpool_close(zpool_handle
);
880 volsize
= zvol_volsize_to_reservation(volsize
, real_props
);
881 nvlist_free(real_props
);
883 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
885 if (nvlist_add_uint64(props
,
886 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
893 if (parents
&& zfs_name_valid(argv
[0], type
)) {
895 * Now create the ancestors of target dataset. If the target
896 * already exists and '-p' option was used we should not
899 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
903 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
908 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
911 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
917 * Mount and/or share the new filesystem as appropriate. We provide a
918 * verbose error message to let the user know that their filesystem was
919 * in fact created, even if we failed to mount or share it.
920 * If the user doesn't want the dataset automatically mounted,
921 * then skip the mount/share step altogether.
923 if (should_auto_mount(zhp
)) {
924 if (zfs_mount(zhp
, NULL
, 0) != 0) {
925 (void) fprintf(stderr
, gettext("filesystem "
926 "successfully created, but not mounted\n"));
928 } else if (zfs_share(zhp
) != 0) {
929 (void) fprintf(stderr
, gettext("filesystem "
930 "successfully created, but not shared\n"));
947 * zfs destroy [-rRf] <fs, vol>
948 * zfs destroy [-rRd] <snap>
950 * -r Recursively destroy all children
951 * -R Recursively destroy all dependents, including clones
952 * -f Force unmounting of any dependents
953 * -d If we can't destroy now, mark for deferred destruction
955 * Destroys the given dataset. By default, it will unmount any filesystems,
956 * and refuse to destroy a dataset that has any dependents. A dependent can
957 * either be a child, or a clone of a child.
959 typedef struct destroy_cbdata
{
962 boolean_t cb_recurse
;
964 boolean_t cb_doclones
;
965 zfs_handle_t
*cb_target
;
966 boolean_t cb_defer_destroy
;
967 boolean_t cb_verbose
;
968 boolean_t cb_parsable
;
971 nvlist_t
*cb_batchedsnaps
;
973 /* first snap in contiguous run */
975 /* previous snap in contiguous run */
983 * Check for any dependents based on the '-r' or '-R' flags.
986 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
988 destroy_cbdata_t
*cbp
= data
;
989 const char *tname
= zfs_get_name(cbp
->cb_target
);
990 const char *name
= zfs_get_name(zhp
);
992 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
993 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
995 * This is a direct descendant, not a clone somewhere else in
1001 if (cbp
->cb_first
) {
1002 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1003 "%s has children\n"),
1004 zfs_get_name(cbp
->cb_target
),
1005 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1006 (void) fprintf(stderr
, gettext("use '-r' to destroy "
1007 "the following datasets:\n"));
1008 cbp
->cb_first
= B_FALSE
;
1009 cbp
->cb_error
= B_TRUE
;
1012 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1015 * This is a clone. We only want to report this if the '-r'
1016 * wasn't specified, or the target is a snapshot.
1018 if (!cbp
->cb_recurse
&&
1019 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
1022 if (cbp
->cb_first
) {
1023 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1024 "%s has dependent clones\n"),
1025 zfs_get_name(cbp
->cb_target
),
1026 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1027 (void) fprintf(stderr
, gettext("use '-R' to destroy "
1028 "the following datasets:\n"));
1029 cbp
->cb_first
= B_FALSE
;
1030 cbp
->cb_error
= B_TRUE
;
1031 cbp
->cb_dryrun
= B_TRUE
;
1034 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1043 destroy_callback(zfs_handle_t
*zhp
, void *data
)
1045 destroy_cbdata_t
*cb
= data
;
1046 const char *name
= zfs_get_name(zhp
);
1048 if (cb
->cb_verbose
) {
1049 if (cb
->cb_parsable
) {
1050 (void) printf("destroy\t%s\n", name
);
1051 } else if (cb
->cb_dryrun
) {
1052 (void) printf(gettext("would destroy %s\n"),
1055 (void) printf(gettext("will destroy %s\n"),
1061 * Ignore pools (which we've already flagged as an error before getting
1064 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1065 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1069 if (cb
->cb_dryrun
) {
1075 * We batch up all contiguous snapshots (even of different
1076 * filesystems) and destroy them with one ioctl. We can't
1077 * simply do all snap deletions and then all fs deletions,
1078 * because we must delete a clone before its origin.
1080 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1081 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1083 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1084 cb
->cb_batchedsnaps
, B_FALSE
);
1085 fnvlist_free(cb
->cb_batchedsnaps
);
1086 cb
->cb_batchedsnaps
= fnvlist_alloc();
1089 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1090 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1101 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1103 destroy_cbdata_t
*cb
= arg
;
1104 const char *name
= zfs_get_name(zhp
);
1107 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1108 if (cb
->cb_firstsnap
== NULL
)
1109 cb
->cb_firstsnap
= strdup(name
);
1110 free(cb
->cb_prevsnap
);
1111 /* this snap continues the current range */
1112 cb
->cb_prevsnap
= strdup(name
);
1113 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1115 if (cb
->cb_verbose
) {
1116 if (cb
->cb_parsable
) {
1117 (void) printf("destroy\t%s\n", name
);
1118 } else if (cb
->cb_dryrun
) {
1119 (void) printf(gettext("would destroy %s\n"),
1122 (void) printf(gettext("will destroy %s\n"),
1126 } else if (cb
->cb_firstsnap
!= NULL
) {
1127 /* end of this range */
1129 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1130 cb
->cb_prevsnap
, &used
);
1131 cb
->cb_snapused
+= used
;
1132 free(cb
->cb_firstsnap
);
1133 cb
->cb_firstsnap
= NULL
;
1134 free(cb
->cb_prevsnap
);
1135 cb
->cb_prevsnap
= NULL
;
1142 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1145 assert(cb
->cb_firstsnap
== NULL
);
1146 assert(cb
->cb_prevsnap
== NULL
);
1147 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1148 if (cb
->cb_firstsnap
!= NULL
) {
1151 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1152 cb
->cb_prevsnap
, &used
);
1154 cb
->cb_snapused
+= used
;
1155 free(cb
->cb_firstsnap
);
1156 cb
->cb_firstsnap
= NULL
;
1157 free(cb
->cb_prevsnap
);
1158 cb
->cb_prevsnap
= NULL
;
1164 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1166 destroy_cbdata_t
*cb
= arg
;
1169 /* Check for clones. */
1170 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1171 cb
->cb_target
= zhp
;
1172 cb
->cb_first
= B_TRUE
;
1173 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1174 destroy_check_dependent
, cb
);
1178 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1186 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1188 destroy_cbdata_t
*cb
= arg
;
1191 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1197 if (cb
->cb_verbose
) {
1198 err
= destroy_print_snapshots(zhp
, cb
);
1204 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1212 destroy_clones(destroy_cbdata_t
*cb
)
1215 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1217 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1218 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1221 boolean_t defer
= cb
->cb_defer_destroy
;
1225 * We can't defer destroy non-snapshots, so set it to
1226 * false while destroying the clones.
1228 cb
->cb_defer_destroy
= B_FALSE
;
1229 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1230 destroy_callback
, cb
);
1231 cb
->cb_defer_destroy
= defer
;
1241 zfs_do_destroy(int argc
, char **argv
)
1243 destroy_cbdata_t cb
= { 0 };
1247 zfs_handle_t
*zhp
= NULL
;
1249 zfs_type_t type
= ZFS_TYPE_DATASET
;
1252 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1255 cb
.cb_verbose
= B_TRUE
;
1258 cb
.cb_verbose
= B_TRUE
;
1259 cb
.cb_parsable
= B_TRUE
;
1262 cb
.cb_dryrun
= B_TRUE
;
1265 cb
.cb_defer_destroy
= B_TRUE
;
1266 type
= ZFS_TYPE_SNAPSHOT
;
1269 cb
.cb_force
= B_TRUE
;
1272 cb
.cb_recurse
= B_TRUE
;
1275 cb
.cb_recurse
= B_TRUE
;
1276 cb
.cb_doclones
= B_TRUE
;
1280 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1289 /* check number of arguments */
1291 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1295 (void) fprintf(stderr
, gettext("too many arguments\n"));
1299 at
= strchr(argv
[0], '@');
1300 pound
= strchr(argv
[0], '#');
1303 /* Build the list of snaps to destroy in cb_nvl. */
1304 cb
.cb_nvl
= fnvlist_alloc();
1307 zhp
= zfs_open(g_zfs
, argv
[0],
1308 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1312 cb
.cb_snapspec
= at
+ 1;
1313 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1319 if (nvlist_empty(cb
.cb_nvl
)) {
1320 (void) fprintf(stderr
, gettext("could not find any "
1321 "snapshots to destroy; check snapshot names.\n"));
1326 if (cb
.cb_verbose
) {
1328 zfs_nicenum(cb
.cb_snapused
, buf
, sizeof (buf
));
1329 if (cb
.cb_parsable
) {
1330 (void) printf("reclaim\t%llu\n",
1332 } else if (cb
.cb_dryrun
) {
1333 (void) printf(gettext("would reclaim %s\n"),
1336 (void) printf(gettext("will reclaim %s\n"),
1341 if (!cb
.cb_dryrun
) {
1342 if (cb
.cb_doclones
) {
1343 cb
.cb_batchedsnaps
= fnvlist_alloc();
1344 err
= destroy_clones(&cb
);
1346 err
= zfs_destroy_snaps_nvl(g_zfs
,
1347 cb
.cb_batchedsnaps
, B_FALSE
);
1355 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1356 cb
.cb_defer_destroy
);
1362 } else if (pound
!= NULL
) {
1367 (void) fprintf(stderr
,
1368 "dryrun is not supported with bookmark\n");
1372 if (cb
.cb_defer_destroy
) {
1373 (void) fprintf(stderr
,
1374 "defer destroy is not supported with bookmark\n");
1378 if (cb
.cb_recurse
) {
1379 (void) fprintf(stderr
,
1380 "recursive is not supported with bookmark\n");
1384 if (!zfs_bookmark_exists(argv
[0])) {
1385 (void) fprintf(stderr
, gettext("bookmark '%s' "
1386 "does not exist.\n"), argv
[0]);
1390 nvl
= fnvlist_alloc();
1391 fnvlist_add_boolean(nvl
, argv
[0]);
1393 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1395 (void) zfs_standard_error(g_zfs
, err
,
1396 "cannot destroy bookmark");
1399 nvlist_free(cb
.cb_nvl
);
1403 /* Open the given dataset */
1404 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1410 * Perform an explicit check for pools before going any further.
1412 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1413 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1414 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1415 "operation does not apply to pools\n"),
1417 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1418 "%s' to destroy all datasets in the pool\n"),
1420 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1421 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1427 * Check for any dependents and/or clones.
1429 cb
.cb_first
= B_TRUE
;
1430 if (!cb
.cb_doclones
&&
1431 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1442 cb
.cb_batchedsnaps
= fnvlist_alloc();
1443 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1450 * Do the real thing. The callback will close the
1451 * handle regardless of whether it succeeds or not.
1453 err
= destroy_callback(zhp
, &cb
);
1456 err
= zfs_destroy_snaps_nvl(g_zfs
,
1457 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1464 fnvlist_free(cb
.cb_batchedsnaps
);
1465 fnvlist_free(cb
.cb_nvl
);
1472 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1475 zfs_get_column_t col
;
1477 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1478 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1479 if (col
== GET_COL_RECVD
)
1485 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1486 * < all | property[,property]... > < fs | snap | vol > ...
1488 * -r recurse over any child datasets
1489 * -H scripted mode. Headers are stripped, and fields are separated
1490 * by tabs instead of spaces.
1491 * -o Set of fields to display. One of "name,property,value,
1492 * received,source". Default is "name,property,value,source".
1493 * "all" is an alias for all five.
1494 * -s Set of sources to allow. One of
1495 * "local,default,inherited,received,temporary,none". Default is
1497 * -p Display values in parsable (literal) format.
1499 * Prints properties for the given datasets. The user can control which
1500 * columns to display as well as which property types to allow.
1504 * Invoked to display the properties for a single dataset.
1507 get_callback(zfs_handle_t
*zhp
, void *data
)
1509 char buf
[ZFS_MAXPROPLEN
];
1510 char rbuf
[ZFS_MAXPROPLEN
];
1511 zprop_source_t sourcetype
;
1512 char source
[ZFS_MAX_DATASET_NAME_LEN
];
1513 zprop_get_cbdata_t
*cbp
= data
;
1514 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1515 zprop_list_t
*pl
= cbp
->cb_proplist
;
1519 boolean_t received
= is_recvd_column(cbp
);
1521 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1522 char *recvdval
= NULL
;
1524 * Skip the special fake placeholder. This will also skip over
1525 * the name property when 'all' is specified.
1527 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1528 pl
== cbp
->cb_proplist
)
1531 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1532 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1533 sizeof (buf
), &sourcetype
, source
,
1535 cbp
->cb_literal
) != 0) {
1538 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1539 ZFS_TYPE_DATASET
)) {
1540 (void) fprintf(stderr
,
1541 gettext("No such property '%s'\n"),
1542 zfs_prop_to_name(pl
->pl_prop
));
1545 sourcetype
= ZPROP_SRC_NONE
;
1546 (void) strlcpy(buf
, "-", sizeof (buf
));
1549 if (received
&& (zfs_prop_get_recvd(zhp
,
1550 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1551 cbp
->cb_literal
) == 0))
1554 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1555 zfs_prop_to_name(pl
->pl_prop
),
1556 buf
, sourcetype
, source
, recvdval
);
1557 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1558 sourcetype
= ZPROP_SRC_LOCAL
;
1560 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1561 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1562 sourcetype
= ZPROP_SRC_NONE
;
1563 (void) strlcpy(buf
, "-", sizeof (buf
));
1566 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1567 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1568 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1569 sourcetype
= ZPROP_SRC_LOCAL
;
1571 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1572 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1573 sourcetype
= ZPROP_SRC_NONE
;
1574 (void) strlcpy(buf
, "-", sizeof (buf
));
1577 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1578 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1580 if (nvlist_lookup_nvlist(user_props
,
1581 pl
->pl_user_prop
, &propval
) != 0) {
1584 sourcetype
= ZPROP_SRC_NONE
;
1587 verify(nvlist_lookup_string(propval
,
1588 ZPROP_VALUE
, &strval
) == 0);
1589 verify(nvlist_lookup_string(propval
,
1590 ZPROP_SOURCE
, &sourceval
) == 0);
1592 if (strcmp(sourceval
,
1593 zfs_get_name(zhp
)) == 0) {
1594 sourcetype
= ZPROP_SRC_LOCAL
;
1595 } else if (strcmp(sourceval
,
1596 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1597 sourcetype
= ZPROP_SRC_RECEIVED
;
1599 sourcetype
= ZPROP_SRC_INHERITED
;
1600 (void) strlcpy(source
,
1601 sourceval
, sizeof (source
));
1605 if (received
&& (zfs_prop_get_recvd(zhp
,
1606 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1607 cbp
->cb_literal
) == 0))
1610 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1611 pl
->pl_user_prop
, strval
, sourcetype
,
1620 zfs_do_get(int argc
, char **argv
)
1622 zprop_get_cbdata_t cb
= { 0 };
1623 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1624 int types
= ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
;
1625 char *value
, *fields
;
1628 zprop_list_t fake_name
= { 0 };
1631 * Set up default columns and sources.
1633 cb
.cb_sources
= ZPROP_SRC_ALL
;
1634 cb
.cb_columns
[0] = GET_COL_NAME
;
1635 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1636 cb
.cb_columns
[2] = GET_COL_VALUE
;
1637 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1638 cb
.cb_type
= ZFS_TYPE_DATASET
;
1641 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1644 cb
.cb_literal
= B_TRUE
;
1647 limit
= parse_depth(optarg
, &flags
);
1650 flags
|= ZFS_ITER_RECURSE
;
1653 cb
.cb_scripted
= B_TRUE
;
1656 (void) fprintf(stderr
, gettext("missing argument for "
1657 "'%c' option\n"), optopt
);
1662 * Process the set of columns to display. We zero out
1663 * the structure to give us a blank slate.
1665 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1667 while (*optarg
!= '\0') {
1668 static char *col_subopts
[] =
1669 { "name", "property", "value", "received",
1670 "source", "all", NULL
};
1672 if (i
== ZFS_GET_NCOLS
) {
1673 (void) fprintf(stderr
, gettext("too "
1674 "many fields given to -o "
1679 switch (getsubopt(&optarg
, col_subopts
,
1682 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1685 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1688 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1691 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1692 flags
|= ZFS_ITER_RECVD_PROPS
;
1695 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1699 (void) fprintf(stderr
,
1700 gettext("\"all\" conflicts "
1701 "with specific fields "
1702 "given to -o option\n"));
1705 cb
.cb_columns
[0] = GET_COL_NAME
;
1706 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1707 cb
.cb_columns
[2] = GET_COL_VALUE
;
1708 cb
.cb_columns
[3] = GET_COL_RECVD
;
1709 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1710 flags
|= ZFS_ITER_RECVD_PROPS
;
1714 (void) fprintf(stderr
,
1715 gettext("invalid column name "
1724 while (*optarg
!= '\0') {
1725 static char *source_subopts
[] = {
1726 "local", "default", "inherited",
1727 "received", "temporary", "none",
1730 switch (getsubopt(&optarg
, source_subopts
,
1733 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1736 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1739 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1742 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1745 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1748 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1751 (void) fprintf(stderr
,
1752 gettext("invalid source "
1761 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1762 while (*optarg
!= '\0') {
1763 static char *type_subopts
[] = { "filesystem",
1764 "volume", "snapshot", "bookmark",
1767 switch (getsubopt(&optarg
, type_subopts
,
1770 types
|= ZFS_TYPE_FILESYSTEM
;
1773 types
|= ZFS_TYPE_VOLUME
;
1776 types
|= ZFS_TYPE_SNAPSHOT
;
1779 types
|= ZFS_TYPE_BOOKMARK
;
1782 types
= ZFS_TYPE_DATASET
|
1787 (void) fprintf(stderr
,
1788 gettext("invalid type '%s'\n"),
1796 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1806 (void) fprintf(stderr
, gettext("missing property "
1813 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1821 * As part of zfs_expand_proplist(), we keep track of the maximum column
1822 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1823 * need to know the maximum name length. However, the user likely did
1824 * not specify 'name' as one of the properties to fetch, so we need to
1825 * make sure we always include at least this property for
1826 * print_get_headers() to work properly.
1828 if (cb
.cb_proplist
!= NULL
) {
1829 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1830 fake_name
.pl_width
= strlen(gettext("NAME"));
1831 fake_name
.pl_next
= cb
.cb_proplist
;
1832 cb
.cb_proplist
= &fake_name
;
1835 cb
.cb_first
= B_TRUE
;
1837 /* run for each object */
1838 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1839 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1841 if (cb
.cb_proplist
== &fake_name
)
1842 zprop_free_list(fake_name
.pl_next
);
1844 zprop_free_list(cb
.cb_proplist
);
1850 * inherit [-rS] <property> <fs|vol> ...
1852 * -r Recurse over all children
1853 * -S Revert to received value, if any
1855 * For each dataset specified on the command line, inherit the given property
1856 * from its parent. Inheriting a property at the pool level will cause it to
1857 * use the default value. The '-r' flag will recurse over all children, and is
1858 * useful for setting a property on a hierarchy-wide basis, regardless of any
1859 * local modifications for each dataset.
1862 typedef struct inherit_cbdata
{
1863 const char *cb_propname
;
1864 boolean_t cb_received
;
1868 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1870 inherit_cbdata_t
*cb
= data
;
1871 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1874 * If we're doing it recursively, then ignore properties that
1875 * are not valid for this type of dataset.
1877 if (prop
!= ZPROP_INVAL
&&
1878 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
)))
1881 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1885 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1887 inherit_cbdata_t
*cb
= data
;
1889 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1893 zfs_do_inherit(int argc
, char **argv
)
1897 inherit_cbdata_t cb
= { 0 };
1901 boolean_t received
= B_FALSE
;
1904 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1907 flags
|= ZFS_ITER_RECURSE
;
1914 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1923 /* check number of arguments */
1925 (void) fprintf(stderr
, gettext("missing property argument\n"));
1929 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1937 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1938 if (zfs_prop_readonly(prop
)) {
1939 (void) fprintf(stderr
, gettext(
1940 "%s property is read-only\n"),
1944 if (!zfs_prop_inheritable(prop
) && !received
) {
1945 (void) fprintf(stderr
, gettext("'%s' property cannot "
1946 "be inherited\n"), propname
);
1947 if (prop
== ZFS_PROP_QUOTA
||
1948 prop
== ZFS_PROP_RESERVATION
||
1949 prop
== ZFS_PROP_REFQUOTA
||
1950 prop
== ZFS_PROP_REFRESERVATION
) {
1951 (void) fprintf(stderr
, gettext("use 'zfs set "
1952 "%s=none' to clear\n"), propname
);
1953 (void) fprintf(stderr
, gettext("use 'zfs "
1954 "inherit -S %s' to revert to received "
1955 "value\n"), propname
);
1959 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1960 prop
== ZFS_PROP_VERSION
)) {
1961 (void) fprintf(stderr
, gettext("'%s' property cannot "
1962 "be reverted to a received value\n"), propname
);
1965 } else if (!zfs_prop_user(propname
)) {
1966 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1971 cb
.cb_propname
= propname
;
1972 cb
.cb_received
= received
;
1974 if (flags
& ZFS_ITER_RECURSE
) {
1975 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1976 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1978 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1979 NULL
, NULL
, 0, inherit_cb
, &cb
);
1985 typedef struct upgrade_cbdata
{
1986 uint64_t cb_numupgraded
;
1987 uint64_t cb_numsamegraded
;
1988 uint64_t cb_numfailed
;
1989 uint64_t cb_version
;
1991 boolean_t cb_foundone
;
1992 char cb_lastfs
[ZFS_MAX_DATASET_NAME_LEN
];
1996 same_pool(zfs_handle_t
*zhp
, const char *name
)
1998 int len1
= strcspn(name
, "/@");
1999 const char *zhname
= zfs_get_name(zhp
);
2000 int len2
= strcspn(zhname
, "/@");
2004 return (strncmp(name
, zhname
, len1
) == 0);
2008 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
2010 upgrade_cbdata_t
*cb
= data
;
2011 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2013 /* list if it's old/new */
2014 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
2015 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
2018 str
= gettext("The following filesystems are "
2019 "formatted using a newer software version and\n"
2020 "cannot be accessed on the current system.\n\n");
2022 str
= gettext("The following filesystems are "
2023 "out of date, and can be upgraded. After being\n"
2024 "upgraded, these filesystems (and any 'zfs send' "
2025 "streams generated from\n"
2026 "subsequent snapshots) will no longer be "
2027 "accessible by older software versions.\n\n");
2030 if (!cb
->cb_foundone
) {
2032 (void) printf(gettext("VER FILESYSTEM\n"));
2033 (void) printf(gettext("--- ------------\n"));
2034 cb
->cb_foundone
= B_TRUE
;
2037 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
2044 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2046 upgrade_cbdata_t
*cb
= data
;
2047 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2048 int needed_spa_version
;
2051 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2054 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2056 if (needed_spa_version
< 0)
2059 if (spa_version
< needed_spa_version
) {
2061 (void) printf(gettext("%s: can not be "
2062 "upgraded; the pool version needs to first "
2063 "be upgraded\nto version %d\n\n"),
2064 zfs_get_name(zhp
), needed_spa_version
);
2070 if (version
< cb
->cb_version
) {
2072 (void) snprintf(verstr
, sizeof (verstr
),
2073 "%llu", cb
->cb_version
);
2074 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2076 * If they did "zfs upgrade -a", then we could
2077 * be doing ioctls to different pools. We need
2078 * to log this history once to each pool, and bypass
2079 * the normal history logging that happens in main().
2081 (void) zpool_log_history(g_zfs
, history_str
);
2082 log_history
= B_FALSE
;
2084 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2085 cb
->cb_numupgraded
++;
2088 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2089 } else if (version
> cb
->cb_version
) {
2090 /* can't downgrade */
2091 (void) printf(gettext("%s: can not be downgraded; "
2092 "it is already at version %u\n"),
2093 zfs_get_name(zhp
), version
);
2096 cb
->cb_numsamegraded
++;
2104 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2107 zfs_do_upgrade(int argc
, char **argv
)
2109 boolean_t all
= B_FALSE
;
2110 boolean_t showversions
= B_FALSE
;
2112 upgrade_cbdata_t cb
= { 0 };
2114 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2117 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2120 flags
|= ZFS_ITER_RECURSE
;
2123 showversions
= B_TRUE
;
2126 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2127 optarg
, &cb
.cb_version
) != 0) {
2128 (void) fprintf(stderr
,
2129 gettext("invalid version %s\n"), optarg
);
2138 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2147 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2149 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2150 cb
.cb_version
|| argc
))
2152 if ((all
|| argc
) && (showversions
))
2158 /* Show info on available versions. */
2159 (void) printf(gettext("The following filesystem versions are "
2161 (void) printf(gettext("VER DESCRIPTION\n"));
2162 (void) printf("--- -----------------------------------------"
2163 "---------------\n");
2164 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2165 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2166 (void) printf(gettext(" 3 Case insensitive and filesystem "
2167 "user identifier (FUID)\n"));
2168 (void) printf(gettext(" 4 userquota, groupquota "
2170 (void) printf(gettext(" 5 System attributes\n"));
2171 (void) printf(gettext("\nFor more information on a particular "
2172 "version, including supported releases,\n"));
2173 (void) printf("see the ZFS Administration Guide.\n\n");
2175 } else if (argc
|| all
) {
2176 /* Upgrade filesystems */
2177 if (cb
.cb_version
== 0)
2178 cb
.cb_version
= ZPL_VERSION
;
2179 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2180 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2181 (void) printf(gettext("%llu filesystems upgraded\n"),
2183 if (cb
.cb_numsamegraded
) {
2184 (void) printf(gettext("%llu filesystems already at "
2186 cb
.cb_numsamegraded
);
2188 if (cb
.cb_numfailed
!= 0)
2191 /* List old-version filesystems */
2193 (void) printf(gettext("This system is currently running "
2194 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2196 flags
|= ZFS_ITER_RECURSE
;
2197 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2198 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2200 found
= cb
.cb_foundone
;
2201 cb
.cb_foundone
= B_FALSE
;
2202 cb
.cb_newer
= B_TRUE
;
2204 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2205 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2207 if (!cb
.cb_foundone
&& !found
) {
2208 (void) printf(gettext("All filesystems are "
2209 "formatted with the current version.\n"));
2217 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2218 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2219 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2220 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2222 * -H Scripted mode; elide headers and separate columns by tabs.
2223 * -i Translate SID to POSIX ID.
2224 * -n Print numeric ID instead of user/group name.
2225 * -o Control which fields to display.
2226 * -p Use exact (parsable) numeric output.
2227 * -s Specify sort columns, descending order.
2228 * -S Specify sort columns, ascending order.
2229 * -t Control which object types to display.
2231 * Displays space consumed by, and quotas on, each user in the specified
2232 * filesystem or snapshot.
2235 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2236 enum us_field_types
{
2242 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA" };
2243 static char *us_field_names
[] = { "type", "name", "used", "quota" };
2244 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2246 #define USTYPE_PSX_GRP (1 << 0)
2247 #define USTYPE_PSX_USR (1 << 1)
2248 #define USTYPE_SMB_GRP (1 << 2)
2249 #define USTYPE_SMB_USR (1 << 3)
2250 #define USTYPE_ALL \
2251 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2253 static int us_type_bits
[] = {
2260 static char *us_type_names
[] = { "posixgroup", "posixuser", "smbgroup",
2263 typedef struct us_node
{
2265 uu_avl_node_t usn_avlnode
;
2266 uu_list_node_t usn_listnode
;
2269 typedef struct us_cbdata
{
2271 uu_avl_pool_t
*cb_avl_pool
;
2273 boolean_t cb_numname
;
2274 boolean_t cb_nicenum
;
2275 boolean_t cb_sid2posix
;
2276 zfs_userquota_prop_t cb_prop
;
2277 zfs_sort_column_t
*cb_sortcol
;
2278 size_t cb_width
[USFIELD_LAST
];
2281 static boolean_t us_populated
= B_FALSE
;
2284 zfs_sort_column_t
*si_sortcol
;
2285 boolean_t si_numname
;
2289 us_field_index(char *field
)
2293 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2294 if (strcmp(field
, us_field_names
[i
]) == 0)
2302 us_compare(const void *larg
, const void *rarg
, void *unused
)
2304 const us_node_t
*l
= larg
;
2305 const us_node_t
*r
= rarg
;
2306 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2307 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2308 boolean_t numname
= si
->si_numname
;
2309 nvlist_t
*lnvl
= l
->usn_nvl
;
2310 nvlist_t
*rnvl
= r
->usn_nvl
;
2314 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2321 zfs_prop_t prop
= sortcol
->sc_prop
;
2322 const char *propname
= NULL
;
2323 boolean_t reverse
= sortcol
->sc_reverse
;
2328 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2329 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2331 rc
= (rv32
< lv32
) ? 1 : -1;
2336 (void) nvlist_lookup_uint64(lnvl
, propname
,
2338 (void) nvlist_lookup_uint64(rnvl
, propname
,
2341 rc
= (rv64
< lv64
) ? 1 : -1;
2343 (void) nvlist_lookup_string(lnvl
, propname
,
2345 (void) nvlist_lookup_string(rnvl
, propname
,
2347 rc
= strcmp(lvstr
, rvstr
);
2351 case ZFS_PROP_QUOTA
:
2354 if (prop
== ZFS_PROP_USED
)
2358 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2359 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2361 rc
= (rv64
< lv64
) ? 1 : -1;
2370 return (reverse
? 1 : -1);
2372 return (reverse
? -1 : 1);
2377 * If entries still seem to be the same, check if they are of the same
2378 * type (smbentity is added only if we are doing SID to POSIX ID
2379 * translation where we can have duplicate type/name combinations).
2381 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2382 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2384 return (lvb
< rvb
? -1 : 1);
2389 static inline const char *
2390 us_type2str(unsigned field_type
)
2392 switch (field_type
) {
2393 case USTYPE_PSX_USR
:
2394 return ("POSIX User");
2395 case USTYPE_PSX_GRP
:
2396 return ("POSIX Group");
2397 case USTYPE_SMB_USR
:
2398 return ("SMB User");
2399 case USTYPE_SMB_GRP
:
2400 return ("SMB Group");
2402 return ("Undefined");
2407 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2409 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2410 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2415 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2416 uu_avl_t
*avl
= cb
->cb_avl
;
2420 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2422 const char *typestr
;
2426 int typeidx
, nameidx
, sizeidx
;
2427 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2428 boolean_t smbentity
= B_FALSE
;
2430 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2432 node
= safe_malloc(sizeof (us_node_t
));
2433 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2434 node
->usn_nvl
= props
;
2436 if (domain
!= NULL
&& domain
[0] != '\0') {
2438 char sid
[MAXNAMELEN
+ 32];
2441 int flag
= IDMAP_REQ_FLG_USE_CACHE
;
2445 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2447 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2448 type
= USTYPE_SMB_GRP
;
2449 err
= sid_to_id(sid
, B_FALSE
, &id
);
2451 type
= USTYPE_SMB_USR
;
2452 err
= sid_to_id(sid
, B_TRUE
, &id
);
2457 if (!cb
->cb_sid2posix
) {
2458 if (type
== USTYPE_SMB_USR
) {
2459 (void) idmap_getwinnamebyuid(rid
, flag
,
2462 (void) idmap_getwinnamebygid(rid
, flag
,
2471 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2473 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2474 type
= USTYPE_PSX_GRP
;
2475 if (!cb
->cb_numname
) {
2478 if ((g
= getgrgid(rid
)) != NULL
)
2482 type
= USTYPE_PSX_USR
;
2483 if (!cb
->cb_numname
) {
2486 if ((p
= getpwuid(rid
)) != NULL
)
2493 * Make sure that the type/name combination is unique when doing
2494 * SID to POSIX ID translation (hence changing the type from SMB to
2497 if (cb
->cb_sid2posix
&&
2498 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2501 /* Calculate/update width of TYPE field */
2502 typestr
= us_type2str(type
);
2503 typelen
= strlen(gettext(typestr
));
2504 typeidx
= us_field_index("type");
2505 if (typelen
> cb
->cb_width
[typeidx
])
2506 cb
->cb_width
[typeidx
] = typelen
;
2507 if (nvlist_add_uint32(props
, "type", type
) != 0)
2510 /* Calculate/update width of NAME field */
2511 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2512 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2514 namelen
= snprintf(NULL
, 0, "%u", rid
);
2516 if (nvlist_add_string(props
, "name", name
) != 0)
2518 namelen
= strlen(name
);
2520 nameidx
= us_field_index("name");
2521 if (namelen
> cb
->cb_width
[nameidx
])
2522 cb
->cb_width
[nameidx
] = namelen
;
2525 * Check if this type/name combination is in the list and update it;
2526 * otherwise add new node to the list.
2528 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2529 uu_avl_insert(avl
, node
, idx
);
2534 props
= node
->usn_nvl
;
2537 /* Calculate/update width of USED/QUOTA fields */
2539 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2541 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu", space
);
2542 sizelen
= strlen(sizebuf
);
2543 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
) {
2545 if (!nvlist_exists(props
, "quota"))
2546 (void) nvlist_add_uint64(props
, "quota", 0);
2549 if (!nvlist_exists(props
, "used"))
2550 (void) nvlist_add_uint64(props
, "used", 0);
2552 sizeidx
= us_field_index(propname
);
2553 if (sizelen
> cb
->cb_width
[sizeidx
])
2554 cb
->cb_width
[sizeidx
] = sizelen
;
2556 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2563 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2564 size_t *width
, us_node_t
*node
)
2566 nvlist_t
*nvl
= node
->usn_nvl
;
2567 char valstr
[MAXNAMELEN
];
2568 boolean_t first
= B_TRUE
;
2574 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2575 if (!(ustype
& types
))
2578 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2579 nvpair_t
*nvp
= NULL
;
2583 char *strval
= NULL
;
2585 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2586 if (strcmp(nvpair_name(nvp
),
2587 us_field_names
[field
]) == 0)
2591 type
= nvpair_type(nvp
);
2593 case DATA_TYPE_UINT32
:
2594 (void) nvpair_value_uint32(nvp
, &val32
);
2596 case DATA_TYPE_UINT64
:
2597 (void) nvpair_value_uint64(nvp
, &val64
);
2599 case DATA_TYPE_STRING
:
2600 (void) nvpair_value_string(nvp
, &strval
);
2603 (void) fprintf(stderr
, "invalid data type\n");
2608 strval
= (char *)us_type2str(val32
);
2611 if (type
== DATA_TYPE_UINT64
) {
2612 (void) sprintf(valstr
, "%llu", val64
);
2618 if (type
== DATA_TYPE_UINT64
) {
2620 (void) sprintf(valstr
, "%llu", val64
);
2622 zfs_nicenum(val64
, valstr
,
2625 if (field
== USFIELD_QUOTA
&&
2626 strcmp(valstr
, "0") == 0)
2636 (void) printf("\t");
2641 (void) printf("%s", strval
);
2642 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2643 (void) printf("%-*s", width
[field
], strval
);
2645 (void) printf("%*s", width
[field
], strval
);
2651 (void) printf("\n");
2655 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2656 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2664 boolean_t first
= B_TRUE
;
2666 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2667 col
= gettext(us_field_hdr
[field
]);
2668 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2669 (void) printf(first
? "%-*s" : " %-*s",
2672 (void) printf(first
? "%*s" : " %*s",
2678 (void) printf("\n");
2681 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2682 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2684 nvlist_free(node
->usn_nvl
);
2689 zfs_do_userspace(int argc
, char **argv
)
2692 zfs_userquota_prop_t p
;
2693 uu_avl_pool_t
*avl_pool
;
2695 uu_avl_walk_t
*walk
;
2697 char deffields
[] = "type,name,used,quota";
2698 char *ofield
= NULL
;
2699 char *tfield
= NULL
;
2703 boolean_t scripted
= B_FALSE
;
2704 boolean_t prtnum
= B_FALSE
;
2705 boolean_t parsable
= B_FALSE
;
2706 boolean_t sid2posix
= B_FALSE
;
2709 zfs_sort_column_t
*sortcol
= NULL
;
2710 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2714 uu_list_pool_t
*listpool
;
2716 uu_avl_index_t idx
= 0;
2717 uu_list_index_t idx2
= 0;
2722 if (strcmp(argv
[0], "groupspace") == 0)
2723 /* Toggle default group types */
2724 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2726 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2742 if (zfs_add_sort_column(&sortcol
, optarg
,
2743 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
2744 (void) fprintf(stderr
,
2745 gettext("invalid field '%s'\n"), optarg
);
2756 (void) fprintf(stderr
, gettext("missing argument for "
2757 "'%c' option\n"), optopt
);
2761 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2771 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2775 (void) fprintf(stderr
, gettext("too many arguments\n"));
2779 /* Use default output fields if not specified using -o */
2783 if ((delim
= strchr(ofield
, ',')) != NULL
)
2785 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
2786 (void) fprintf(stderr
, gettext("invalid type '%s' "
2787 "for -o option\n"), ofield
);
2792 } while (delim
!= NULL
);
2793 fields
[cfield
] = USFIELD_LAST
;
2795 /* Override output types (-t option) */
2796 if (tfield
!= NULL
) {
2800 boolean_t found
= B_FALSE
;
2802 if ((delim
= strchr(tfield
, ',')) != NULL
)
2804 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
2806 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
2808 types
|= us_type_bits
[i
];
2813 (void) fprintf(stderr
, gettext("invalid type "
2814 "'%s' for -t option\n"), tfield
);
2819 } while (delim
!= NULL
);
2822 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
2825 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
2826 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
2828 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
2831 /* Always add default sorting columns */
2832 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
2833 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
2835 cb
.cb_sortcol
= sortcol
;
2836 cb
.cb_numname
= prtnum
;
2837 cb
.cb_nicenum
= !parsable
;
2838 cb
.cb_avl_pool
= avl_pool
;
2839 cb
.cb_avl
= avl_tree
;
2840 cb
.cb_sid2posix
= sid2posix
;
2842 for (i
= 0; i
< USFIELD_LAST
; i
++)
2843 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
2845 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
2846 if (((p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
) &&
2847 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
2848 ((p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
) &&
2849 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))))
2852 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
2857 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
2860 us_populated
= B_TRUE
;
2862 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
2863 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
2864 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
2865 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
2867 while (node
!= NULL
) {
2869 node
= uu_avl_next(avl_tree
, node
);
2870 uu_avl_remove(avl_tree
, rmnode
);
2871 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
2872 uu_list_insert(list
, rmnode
, idx2
);
2875 for (node
= uu_list_first(list
); node
!= NULL
;
2876 node
= uu_list_next(list
, node
)) {
2877 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
2879 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
2880 uu_avl_insert(avl_tree
, node
, idx
);
2883 uu_list_destroy(list
);
2884 uu_list_pool_destroy(listpool
);
2886 /* Print and free node nvlist memory */
2887 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
2890 zfs_free_sort_columns(sortcol
);
2892 /* Clean up the AVL tree */
2893 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
2896 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
2897 uu_avl_remove(cb
.cb_avl
, node
);
2901 uu_avl_walk_end(walk
);
2902 uu_avl_destroy(avl_tree
);
2903 uu_avl_pool_destroy(avl_pool
);
2909 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2910 * [-t type[,...]] [filesystem|volume|snapshot] ...
2912 * -H Scripted mode; elide headers and separate columns by tabs.
2913 * -p Display values in parsable (literal) format.
2914 * -r Recurse over all children.
2915 * -d Limit recursion by depth.
2916 * -o Control which fields to display.
2917 * -s Specify sort columns, descending order.
2918 * -S Specify sort columns, ascending order.
2919 * -t Control which object types to display.
2921 * When given no arguments, list all filesystems in the system.
2922 * Otherwise, list the specified datasets, optionally recursing down them if
2923 * '-r' is specified.
2925 typedef struct list_cbdata
{
2927 boolean_t cb_literal
;
2928 boolean_t cb_scripted
;
2929 zprop_list_t
*cb_proplist
;
2933 * Given a list of columns to display, output appropriate headers for each one.
2936 print_header(list_cbdata_t
*cb
)
2938 zprop_list_t
*pl
= cb
->cb_proplist
;
2939 char headerbuf
[ZFS_MAXPROPLEN
];
2942 boolean_t first
= B_TRUE
;
2943 boolean_t right_justify
;
2945 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2952 right_justify
= B_FALSE
;
2953 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2954 header
= zfs_prop_column_name(pl
->pl_prop
);
2955 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2957 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2958 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2959 headerbuf
[i
] = '\0';
2963 if (pl
->pl_next
== NULL
&& !right_justify
)
2964 (void) printf("%s", header
);
2965 else if (right_justify
)
2966 (void) printf("%*s", pl
->pl_width
, header
);
2968 (void) printf("%-*s", pl
->pl_width
, header
);
2971 (void) printf("\n");
2975 * Given a dataset and a list of fields, print out all the properties according
2976 * to the described layout.
2979 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
2981 zprop_list_t
*pl
= cb
->cb_proplist
;
2982 boolean_t first
= B_TRUE
;
2983 char property
[ZFS_MAXPROPLEN
];
2984 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
2987 boolean_t right_justify
;
2989 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2991 if (cb
->cb_scripted
)
2992 (void) printf("\t");
2999 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
3000 (void) strlcpy(property
, zfs_get_name(zhp
),
3003 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3004 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
3005 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
3006 sizeof (property
), NULL
, NULL
, 0,
3007 cb
->cb_literal
) != 0)
3011 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3012 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
3013 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
3014 property
, sizeof (property
), cb
->cb_literal
) != 0)
3018 right_justify
= B_TRUE
;
3019 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
3020 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
3021 property
, sizeof (property
), cb
->cb_literal
) != 0)
3025 right_justify
= B_TRUE
;
3027 if (nvlist_lookup_nvlist(userprops
,
3028 pl
->pl_user_prop
, &propval
) != 0)
3031 verify(nvlist_lookup_string(propval
,
3032 ZPROP_VALUE
, &propstr
) == 0);
3033 right_justify
= B_FALSE
;
3037 * If this is being called in scripted mode, or if this is the
3038 * last column and it is left-justified, don't include a width
3041 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3042 (void) printf("%s", propstr
);
3043 else if (right_justify
)
3044 (void) printf("%*s", pl
->pl_width
, propstr
);
3046 (void) printf("%-*s", pl
->pl_width
, propstr
);
3049 (void) printf("\n");
3053 * Generic callback function to list a dataset or snapshot.
3056 list_callback(zfs_handle_t
*zhp
, void *data
)
3058 list_cbdata_t
*cbp
= data
;
3060 if (cbp
->cb_first
) {
3061 if (!cbp
->cb_scripted
)
3063 cbp
->cb_first
= B_FALSE
;
3066 print_dataset(zhp
, cbp
);
3072 zfs_do_list(int argc
, char **argv
)
3075 static char default_fields
[] =
3076 "name,used,available,referenced,mountpoint";
3077 int types
= ZFS_TYPE_DATASET
;
3078 boolean_t types_specified
= B_FALSE
;
3079 char *fields
= NULL
;
3080 list_cbdata_t cb
= { 0 };
3084 zfs_sort_column_t
*sortcol
= NULL
;
3085 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3088 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3094 cb
.cb_literal
= B_TRUE
;
3095 flags
|= ZFS_ITER_LITERAL_PROPS
;
3098 limit
= parse_depth(optarg
, &flags
);
3101 flags
|= ZFS_ITER_RECURSE
;
3104 cb
.cb_scripted
= B_TRUE
;
3107 if (zfs_add_sort_column(&sortcol
, optarg
,
3109 (void) fprintf(stderr
,
3110 gettext("invalid property '%s'\n"), optarg
);
3115 if (zfs_add_sort_column(&sortcol
, optarg
,
3117 (void) fprintf(stderr
,
3118 gettext("invalid property '%s'\n"), optarg
);
3124 types_specified
= B_TRUE
;
3125 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3126 while (*optarg
!= '\0') {
3127 static char *type_subopts
[] = { "filesystem",
3128 "volume", "snapshot", "snap", "bookmark",
3131 switch (getsubopt(&optarg
, type_subopts
,
3134 types
|= ZFS_TYPE_FILESYSTEM
;
3137 types
|= ZFS_TYPE_VOLUME
;
3141 types
|= ZFS_TYPE_SNAPSHOT
;
3144 types
|= ZFS_TYPE_BOOKMARK
;
3147 types
= ZFS_TYPE_DATASET
|
3151 (void) fprintf(stderr
,
3152 gettext("invalid type '%s'\n"),
3159 (void) fprintf(stderr
, gettext("missing argument for "
3160 "'%c' option\n"), optopt
);
3164 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3174 fields
= default_fields
;
3177 * If we are only going to list snapshot names and sort by name,
3178 * then we can use faster version.
3180 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3181 flags
|= ZFS_ITER_SIMPLE
;
3184 * If "-o space" and no types were specified, don't display snapshots.
3186 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3187 types
&= ~ZFS_TYPE_SNAPSHOT
;
3190 * If the user specifies '-o all', the zprop_get_list() doesn't
3191 * normally include the name of the dataset. For 'zfs list', we always
3192 * want this property to be first.
3194 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3198 cb
.cb_first
= B_TRUE
;
3200 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3201 limit
, list_callback
, &cb
);
3203 zprop_free_list(cb
.cb_proplist
);
3204 zfs_free_sort_columns(sortcol
);
3206 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3207 (void) printf(gettext("no datasets available\n"));
3213 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3214 * zfs rename [-f] -p <fs | vol> <fs | vol>
3215 * zfs rename -r <snap> <snap>
3217 * Renames the given dataset to another of the same type.
3219 * The '-p' flag creates all the non-existing ancestors of the target first.
3223 zfs_do_rename(int argc
, char **argv
)
3228 boolean_t recurse
= B_FALSE
;
3229 boolean_t parents
= B_FALSE
;
3230 boolean_t force_unmount
= B_FALSE
;
3233 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3242 force_unmount
= B_TRUE
;
3246 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3255 /* check number of arguments */
3257 (void) fprintf(stderr
, gettext("missing source dataset "
3262 (void) fprintf(stderr
, gettext("missing target dataset "
3267 (void) fprintf(stderr
, gettext("too many arguments\n"));
3271 if (recurse
&& parents
) {
3272 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3277 if (recurse
&& strchr(argv
[0], '@') == 0) {
3278 (void) fprintf(stderr
, gettext("source dataset for recursive "
3279 "rename must be a snapshot\n"));
3283 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3284 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3287 /* If we were asked and the name looks good, try to create ancestors. */
3288 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3289 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3294 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3303 * Promotes the given clone fs to be the parent
3307 zfs_do_promote(int argc
, char **argv
)
3313 if (argc
> 1 && argv
[1][0] == '-') {
3314 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3319 /* check number of arguments */
3321 (void) fprintf(stderr
, gettext("missing clone filesystem"
3326 (void) fprintf(stderr
, gettext("too many arguments\n"));
3330 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3334 ret
= (zfs_promote(zhp
) != 0);
3342 * zfs rollback [-rRf] <snapshot>
3344 * -r Delete any intervening snapshots before doing rollback
3345 * -R Delete any snapshots and their clones
3346 * -f ignored for backwards compatability
3348 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3349 * since then and making it the active dataset. If more recent snapshots exist,
3350 * the command will complain unless the '-r' flag is given.
3352 typedef struct rollback_cbdata
{
3358 boolean_t cb_recurse
;
3359 } rollback_cbdata_t
;
3362 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3364 rollback_cbdata_t
*cbp
= data
;
3366 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3367 (void) fprintf(stderr
, gettext("cannot rollback to "
3368 "'%s': clones of previous snapshots exist\n"),
3370 (void) fprintf(stderr
, gettext("use '-R' to "
3371 "force deletion of the following clones and "
3377 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3384 * Report any snapshots more recent than the one specified. Used when '-r' is
3385 * not specified. We reuse this same callback for the snapshot dependents - if
3386 * 'cb_dependent' is set, then this is a dependent and we should report it
3387 * without checking the transaction group.
3390 rollback_check(zfs_handle_t
*zhp
, void *data
)
3392 rollback_cbdata_t
*cbp
= data
;
3394 if (cbp
->cb_doclones
) {
3399 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3400 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3401 (void) fprintf(stderr
, gettext("cannot "
3402 "rollback to '%s': more recent snapshots "
3403 "or bookmarks exist\n"),
3405 (void) fprintf(stderr
, gettext("use '-r' to "
3406 "force deletion of the following "
3407 "snapshots and bookmarks:\n"));
3412 if (cbp
->cb_recurse
) {
3413 if (zfs_iter_dependents(zhp
, B_TRUE
,
3414 rollback_check_dependent
, cbp
) != 0) {
3419 (void) fprintf(stderr
, "%s\n",
3428 zfs_do_rollback(int argc
, char **argv
)
3432 boolean_t force
= B_FALSE
;
3433 rollback_cbdata_t cb
= { 0 };
3434 zfs_handle_t
*zhp
, *snap
;
3435 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3439 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3452 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3461 /* check number of arguments */
3463 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3467 (void) fprintf(stderr
, gettext("too many arguments\n"));
3471 /* open the snapshot */
3472 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3475 /* open the parent dataset */
3476 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3477 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3479 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3485 * Check for more recent snapshots and/or clones based on the presence
3488 cb
.cb_target
= argv
[0];
3489 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3490 cb
.cb_first
= B_TRUE
;
3492 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, rollback_check
, &cb
)) != 0)
3494 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
3497 if ((ret
= cb
.cb_error
) != 0)
3501 * Rollback parent to the given snapshot.
3503 ret
= zfs_rollback(zhp
, snap
, force
);
3516 * zfs set property=value ... { fs | snap | vol } ...
3518 * Sets the given properties for all datasets specified on the command line.
3522 set_callback(zfs_handle_t
*zhp
, void *data
)
3524 nvlist_t
*props
= data
;
3526 if (zfs_prop_set_list(zhp
, props
) != 0) {
3527 switch (libzfs_errno(g_zfs
)) {
3528 case EZFS_MOUNTFAILED
:
3529 (void) fprintf(stderr
, gettext("property may be set "
3530 "but unable to remount filesystem\n"));
3532 case EZFS_SHARENFSFAILED
:
3533 (void) fprintf(stderr
, gettext("property may be set "
3534 "but unable to reshare filesystem\n"));
3543 zfs_do_set(int argc
, char **argv
)
3545 nvlist_t
*props
= NULL
;
3546 int ds_start
= -1; /* argv idx of first dataset arg */
3549 /* check for options */
3550 if (argc
> 1 && argv
[1][0] == '-') {
3551 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3556 /* check number of arguments */
3558 (void) fprintf(stderr
, gettext("missing arguments\n"));
3562 if (strchr(argv
[1], '=') == NULL
) {
3563 (void) fprintf(stderr
, gettext("missing property=value "
3566 (void) fprintf(stderr
, gettext("missing dataset "
3572 /* validate argument order: prop=val args followed by dataset args */
3573 for (int i
= 1; i
< argc
; i
++) {
3574 if (strchr(argv
[i
], '=') != NULL
) {
3576 /* out-of-order prop=val argument */
3577 (void) fprintf(stderr
, gettext("invalid "
3578 "argument order\n"), i
);
3581 } else if (ds_start
< 0) {
3586 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
3590 /* Populate a list of property settings */
3591 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3593 for (int i
= 1; i
< ds_start
; i
++) {
3594 if ((ret
= parseprop(props
, argv
[i
])) != 0)
3598 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
3599 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, props
);
3606 typedef struct snap_cbdata
{
3608 boolean_t sd_recursive
;
3609 const char *sd_snapname
;
3613 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3615 snap_cbdata_t
*sd
= arg
;
3620 if (sd
->sd_recursive
&&
3621 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
3626 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3629 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3632 if (sd
->sd_recursive
)
3633 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3639 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3641 * Creates a snapshot with the given name. While functionally equivalent to
3642 * 'zfs create', it is a separate command to differentiate intent.
3645 zfs_do_snapshot(int argc
, char **argv
)
3650 snap_cbdata_t sd
= { 0 };
3651 boolean_t multiple_snaps
= B_FALSE
;
3653 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3655 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3659 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3662 if (parseprop(props
, optarg
) != 0)
3666 sd
.sd_recursive
= B_TRUE
;
3667 multiple_snaps
= B_TRUE
;
3670 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3679 /* check number of arguments */
3681 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3686 multiple_snaps
= B_TRUE
;
3687 for (; argc
> 0; argc
--, argv
++) {
3691 atp
= strchr(argv
[0], '@');
3695 sd
.sd_snapname
= atp
+ 1;
3696 zhp
= zfs_open(g_zfs
, argv
[0],
3697 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3700 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3704 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3705 nvlist_free(sd
.sd_nvl
);
3707 if (ret
!= 0 && multiple_snaps
)
3708 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3712 nvlist_free(sd
.sd_nvl
);
3719 * Send a backup stream to stdout.
3722 zfs_do_send(int argc
, char **argv
)
3724 char *fromname
= NULL
;
3725 char *toname
= NULL
;
3726 char *resume_token
= NULL
;
3729 sendflags_t flags
= { 0 };
3731 nvlist_t
*dbgnv
= NULL
;
3732 boolean_t extraverbose
= B_FALSE
;
3734 struct option long_options
[] = {
3735 {"replicate", no_argument
, NULL
, 'R'},
3736 {"props", no_argument
, NULL
, 'p'},
3737 {"parsable", no_argument
, NULL
, 'P'},
3738 {"dedup", no_argument
, NULL
, 'D'},
3739 {"verbose", no_argument
, NULL
, 'v'},
3740 {"dryrun", no_argument
, NULL
, 'n'},
3741 {"large-block", no_argument
, NULL
, 'L'},
3742 {"embed", no_argument
, NULL
, 'e'},
3743 {"resume", required_argument
, NULL
, 't'},
3744 {"compressed", no_argument
, NULL
, 'c'},
3749 while ((c
= getopt_long(argc
, argv
, ":i:I:RbDpvnPLet:c", long_options
,
3761 flags
.doall
= B_TRUE
;
3764 flags
.replicate
= B_TRUE
;
3767 flags
.props
= B_TRUE
;
3770 flags
.parsable
= B_TRUE
;
3771 flags
.verbose
= B_TRUE
;
3775 extraverbose
= B_TRUE
;
3776 flags
.verbose
= B_TRUE
;
3777 flags
.progress
= B_TRUE
;
3780 flags
.dedup
= B_TRUE
;
3783 flags
.dryrun
= B_TRUE
;
3786 flags
.largeblock
= B_TRUE
;
3789 flags
.embed_data
= B_TRUE
;
3792 resume_token
= optarg
;
3795 flags
.compress
= B_TRUE
;
3799 * If a parameter was not passed, optopt contains the
3800 * value that would normally lead us into the
3801 * appropriate case statement. If it's > 256, then this
3802 * must be a longopt and we should look at argv to get
3803 * the string. Otherwise it's just the character, so we
3804 * should use it directly.
3806 if (optopt
<= UINT8_MAX
) {
3807 (void) fprintf(stderr
,
3808 gettext("missing argument for '%c' "
3809 "option\n"), optopt
);
3811 (void) fprintf(stderr
,
3812 gettext("missing argument for '%s' "
3813 "option\n"), argv
[optind
- 1]);
3821 * If an invalid flag was passed, optopt contains the
3822 * character if it was a short flag, or 0 if it was a
3826 (void) fprintf(stderr
,
3827 gettext("invalid option '%c'\n"), optopt
);
3829 (void) fprintf(stderr
,
3830 gettext("invalid option '%s'\n"),
3841 if (resume_token
!= NULL
) {
3842 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
3844 (void) fprintf(stderr
,
3845 gettext("invalid flags combined with -t\n"));
3849 (void) fprintf(stderr
, gettext("no additional "
3850 "arguments are permitted with -t\n"));
3855 (void) fprintf(stderr
,
3856 gettext("missing snapshot argument\n"));
3860 (void) fprintf(stderr
, gettext("too many arguments\n"));
3865 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
3866 (void) fprintf(stderr
,
3867 gettext("Error: Stream can not be written to a terminal.\n"
3868 "You must redirect standard output.\n"));
3872 if (resume_token
!= NULL
) {
3873 return (zfs_send_resume(g_zfs
, &flags
, STDOUT_FILENO
,
3878 * Special case sending a filesystem, or from a bookmark.
3880 if (strchr(argv
[0], '@') == NULL
||
3881 (fromname
&& strchr(fromname
, '#') != NULL
)) {
3882 char frombuf
[ZFS_MAX_DATASET_NAME_LEN
];
3883 enum lzc_send_flags lzc_flags
= 0;
3885 if (flags
.replicate
|| flags
.doall
|| flags
.props
||
3886 flags
.dedup
|| flags
.dryrun
|| flags
.verbose
||
3888 (void) fprintf(stderr
,
3890 "Unsupported flag with filesystem or bookmark.\n"));
3894 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
3898 if (flags
.largeblock
)
3899 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
3900 if (flags
.embed_data
)
3901 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
3903 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
3905 if (fromname
!= NULL
&&
3906 (fromname
[0] == '#' || fromname
[0] == '@')) {
3908 * Incremental source name begins with # or @.
3909 * Default to same fs as target.
3911 (void) strncpy(frombuf
, argv
[0], sizeof (frombuf
));
3912 cp
= strchr(frombuf
, '@');
3915 (void) strlcat(frombuf
, fromname
, sizeof (frombuf
));
3918 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, lzc_flags
);
3923 cp
= strchr(argv
[0], '@');
3926 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3931 * If they specified the full path to the snapshot, chop off
3932 * everything except the short name of the snapshot, but special
3933 * case if they specify the origin.
3935 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
3936 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
3939 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
3940 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
3942 if (strcmp(origin
, fromname
) == 0) {
3944 flags
.fromorigin
= B_TRUE
;
3947 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
3948 (void) fprintf(stderr
,
3949 gettext("incremental source must be "
3950 "in same filesystem\n"));
3954 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
3955 (void) fprintf(stderr
,
3956 gettext("invalid incremental source\n"));
3962 if (flags
.replicate
&& fromname
== NULL
)
3963 flags
.doall
= B_TRUE
;
3965 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
3966 extraverbose
? &dbgnv
: NULL
);
3968 if (extraverbose
&& dbgnv
!= NULL
) {
3970 * dump_nvlist prints to stdout, but that's been
3971 * redirected to a file. Make it print to stderr
3974 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
3975 dump_nvlist(dbgnv
, 0);
3984 * Restore a backup stream from stdin.
3987 zfs_do_receive(int argc
, char **argv
)
3990 recvflags_t flags
= { 0 };
3991 boolean_t abort_resumable
= B_FALSE
;
3994 nvpair_t
*nvp
= NULL
;
3996 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
4000 while ((c
= getopt(argc
, argv
, ":o:denuvFsA")) != -1) {
4003 if (parseprop(props
, optarg
) != 0)
4007 flags
.isprefix
= B_TRUE
;
4010 flags
.isprefix
= B_TRUE
;
4011 flags
.istail
= B_TRUE
;
4014 flags
.dryrun
= B_TRUE
;
4017 flags
.nomount
= B_TRUE
;
4020 flags
.verbose
= B_TRUE
;
4023 flags
.resumable
= B_TRUE
;
4026 flags
.force
= B_TRUE
;
4029 abort_resumable
= B_TRUE
;
4032 (void) fprintf(stderr
, gettext("missing argument for "
4033 "'%c' option\n"), optopt
);
4037 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4046 /* check number of arguments */
4048 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4052 (void) fprintf(stderr
, gettext("too many arguments\n"));
4056 while ((nvp
= nvlist_next_nvpair(props
, nvp
))) {
4057 if (strcmp(nvpair_name(nvp
), "origin") != 0) {
4058 (void) fprintf(stderr
, gettext("invalid option"));
4063 if (abort_resumable
) {
4064 if (flags
.isprefix
|| flags
.istail
|| flags
.dryrun
||
4065 flags
.resumable
|| flags
.nomount
) {
4066 (void) fprintf(stderr
, gettext("invalid option"));
4070 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
4071 (void) snprintf(namebuf
, sizeof (namebuf
),
4072 "%s/%%recv", argv
[0]);
4074 if (zfs_dataset_exists(g_zfs
, namebuf
,
4075 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) {
4076 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4077 namebuf
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4080 err
= zfs_destroy(zhp
, B_FALSE
);
4082 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4083 argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4086 if (!zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) ||
4087 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
4088 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == -1) {
4089 (void) fprintf(stderr
,
4090 gettext("'%s' does not have any "
4091 "resumable receive state to abort\n"),
4095 err
= zfs_destroy(zhp
, B_FALSE
);
4101 if (isatty(STDIN_FILENO
)) {
4102 (void) fprintf(stderr
,
4103 gettext("Error: Backup stream can not be read "
4104 "from a terminal.\n"
4105 "You must redirect standard input.\n"));
4108 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
4114 * allow/unallow stuff
4116 /* copied from zfs/sys/dsl_deleg.h */
4117 #define ZFS_DELEG_PERM_CREATE "create"
4118 #define ZFS_DELEG_PERM_DESTROY "destroy"
4119 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4120 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4121 #define ZFS_DELEG_PERM_CLONE "clone"
4122 #define ZFS_DELEG_PERM_PROMOTE "promote"
4123 #define ZFS_DELEG_PERM_RENAME "rename"
4124 #define ZFS_DELEG_PERM_MOUNT "mount"
4125 #define ZFS_DELEG_PERM_SHARE "share"
4126 #define ZFS_DELEG_PERM_SEND "send"
4127 #define ZFS_DELEG_PERM_RECEIVE "receive"
4128 #define ZFS_DELEG_PERM_ALLOW "allow"
4129 #define ZFS_DELEG_PERM_USERPROP "userprop"
4130 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4131 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4132 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4133 #define ZFS_DELEG_PERM_USERUSED "userused"
4134 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4135 #define ZFS_DELEG_PERM_HOLD "hold"
4136 #define ZFS_DELEG_PERM_RELEASE "release"
4137 #define ZFS_DELEG_PERM_DIFF "diff"
4138 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4139 #define ZFS_DELEG_PERM_REMAP "remap"
4141 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4143 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
4144 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
4145 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
4146 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
4147 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
4148 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
4149 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
4150 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
4151 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
4152 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
4153 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
4154 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
4155 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
4156 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
4157 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
4158 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
4159 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
4160 { ZFS_DELEG_PERM_REMAP
, ZFS_DELEG_NOTE_REMAP
},
4162 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
4163 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
4164 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
4165 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
4166 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
4167 { NULL
, ZFS_DELEG_NOTE_NONE
}
4170 /* permission structure */
4171 typedef struct deleg_perm
{
4172 zfs_deleg_who_type_t dp_who_type
;
4173 const char *dp_name
;
4175 boolean_t dp_descend
;
4179 typedef struct deleg_perm_node
{
4180 deleg_perm_t dpn_perm
;
4182 uu_avl_node_t dpn_avl_node
;
4183 } deleg_perm_node_t
;
4185 typedef struct fs_perm fs_perm_t
;
4187 /* permissions set */
4188 typedef struct who_perm
{
4189 zfs_deleg_who_type_t who_type
;
4190 const char *who_name
; /* id */
4191 char who_ug_name
[256]; /* user/group name */
4192 fs_perm_t
*who_fsperm
; /* uplink */
4194 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
4198 typedef struct who_perm_node
{
4199 who_perm_t who_perm
;
4200 uu_avl_node_t who_avl_node
;
4203 typedef struct fs_perm_set fs_perm_set_t
;
4204 /* fs permissions */
4206 const char *fsp_name
;
4208 uu_avl_t
*fsp_sc_avl
; /* sets,create */
4209 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
4211 fs_perm_set_t
*fsp_set
; /* uplink */
4215 typedef struct fs_perm_node
{
4216 fs_perm_t fspn_fsperm
;
4219 uu_list_node_t fspn_list_node
;
4222 /* top level structure */
4223 struct fs_perm_set
{
4224 uu_list_pool_t
*fsps_list_pool
;
4225 uu_list_t
*fsps_list
; /* list of fs_perms */
4227 uu_avl_pool_t
*fsps_named_set_avl_pool
;
4228 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
4229 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
4232 static inline const char *
4233 deleg_perm_type(zfs_deleg_note_t note
)
4239 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4240 case ZFS_DELEG_NOTE_GROUPUSED
:
4241 case ZFS_DELEG_NOTE_USERPROP
:
4242 case ZFS_DELEG_NOTE_USERQUOTA
:
4243 case ZFS_DELEG_NOTE_USERUSED
:
4245 return (gettext("other"));
4247 return (gettext("subcommand"));
4252 who_type2weight(zfs_deleg_who_type_t who_type
)
4256 case ZFS_DELEG_NAMED_SET_SETS
:
4257 case ZFS_DELEG_NAMED_SET
:
4260 case ZFS_DELEG_CREATE_SETS
:
4261 case ZFS_DELEG_CREATE
:
4264 case ZFS_DELEG_USER_SETS
:
4265 case ZFS_DELEG_USER
:
4268 case ZFS_DELEG_GROUP_SETS
:
4269 case ZFS_DELEG_GROUP
:
4272 case ZFS_DELEG_EVERYONE_SETS
:
4273 case ZFS_DELEG_EVERYONE
:
4285 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4287 const who_perm_node_t
*l
= larg
;
4288 const who_perm_node_t
*r
= rarg
;
4289 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
4290 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
4291 int lweight
= who_type2weight(ltype
);
4292 int rweight
= who_type2weight(rtype
);
4293 int res
= lweight
- rweight
;
4295 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
4296 ZFS_MAX_DELEG_NAME
-1);
4308 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4310 const deleg_perm_node_t
*l
= larg
;
4311 const deleg_perm_node_t
*r
= rarg
;
4312 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
4313 ZFS_MAX_DELEG_NAME
-1);
4325 fs_perm_set_init(fs_perm_set_t
*fspset
)
4327 bzero(fspset
, sizeof (fs_perm_set_t
));
4329 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4330 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4331 NULL
, UU_DEFAULT
)) == NULL
)
4333 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4334 UU_DEFAULT
)) == NULL
)
4337 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4338 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4339 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4340 UU_DEFAULT
)) == NULL
)
4343 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4344 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4345 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4346 UU_DEFAULT
)) == NULL
)
4349 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4350 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4351 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4356 static inline void fs_perm_fini(fs_perm_t
*);
4357 static inline void who_perm_fini(who_perm_t
*);
4360 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4362 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4364 while (node
!= NULL
) {
4365 fs_perm_node_t
*next_node
=
4366 uu_list_next(fspset
->fsps_list
, node
);
4367 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4368 fs_perm_fini(fsperm
);
4369 uu_list_remove(fspset
->fsps_list
, node
);
4374 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4375 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4376 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4380 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4383 deleg_perm
->dp_who_type
= type
;
4384 deleg_perm
->dp_name
= name
;
4388 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4389 zfs_deleg_who_type_t type
, const char *name
)
4391 uu_avl_pool_t
*pool
;
4392 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4394 bzero(who_perm
, sizeof (who_perm_t
));
4396 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4397 UU_DEFAULT
)) == NULL
)
4400 who_perm
->who_type
= type
;
4401 who_perm
->who_name
= name
;
4402 who_perm
->who_fsperm
= fsperm
;
4406 who_perm_fini(who_perm_t
*who_perm
)
4408 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4410 while (node
!= NULL
) {
4411 deleg_perm_node_t
*next_node
=
4412 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4414 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4419 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4423 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4425 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4426 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4428 bzero(fsperm
, sizeof (fs_perm_t
));
4430 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4434 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4438 fsperm
->fsp_set
= fspset
;
4439 fsperm
->fsp_name
= fsname
;
4443 fs_perm_fini(fs_perm_t
*fsperm
)
4445 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4446 while (node
!= NULL
) {
4447 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4449 who_perm_t
*who_perm
= &node
->who_perm
;
4450 who_perm_fini(who_perm
);
4451 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4456 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4457 while (node
!= NULL
) {
4458 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4460 who_perm_t
*who_perm
= &node
->who_perm
;
4461 who_perm_fini(who_perm
);
4462 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4467 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4468 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4472 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4473 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4475 uu_avl_index_t idx
= 0;
4477 deleg_perm_node_t
*found_node
= NULL
;
4478 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4480 deleg_perm_init(deleg_perm
, who_type
, name
);
4482 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4484 uu_avl_insert(avl
, node
, idx
);
4487 deleg_perm
= &node
->dpn_perm
;
4492 case ZFS_DELEG_LOCAL
:
4493 deleg_perm
->dp_local
= B_TRUE
;
4495 case ZFS_DELEG_DESCENDENT
:
4496 deleg_perm
->dp_descend
= B_TRUE
;
4501 assert(B_FALSE
); /* invalid locality */
4506 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4508 nvpair_t
*nvp
= NULL
;
4509 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4510 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4511 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4513 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4514 const char *name
= nvpair_name(nvp
);
4515 data_type_t type
= nvpair_type(nvp
);
4516 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4517 deleg_perm_node_t
*node
=
4518 safe_malloc(sizeof (deleg_perm_node_t
));
4520 assert(type
== DATA_TYPE_BOOLEAN
);
4522 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4523 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4530 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4532 nvpair_t
*nvp
= NULL
;
4533 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4535 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4536 nvlist_t
*nvl2
= NULL
;
4537 const char *name
= nvpair_name(nvp
);
4538 uu_avl_t
*avl
= NULL
;
4539 uu_avl_pool_t
*avl_pool
= NULL
;
4540 zfs_deleg_who_type_t perm_type
= name
[0];
4541 char perm_locality
= name
[1];
4542 const char *perm_name
= name
+ 3;
4543 boolean_t is_set
= B_TRUE
;
4544 who_perm_t
*who_perm
= NULL
;
4546 assert('$' == name
[2]);
4548 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4551 switch (perm_type
) {
4552 case ZFS_DELEG_CREATE
:
4553 case ZFS_DELEG_CREATE_SETS
:
4554 case ZFS_DELEG_NAMED_SET
:
4555 case ZFS_DELEG_NAMED_SET_SETS
:
4556 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4557 avl
= fsperm
->fsp_sc_avl
;
4559 case ZFS_DELEG_USER
:
4560 case ZFS_DELEG_USER_SETS
:
4561 case ZFS_DELEG_GROUP
:
4562 case ZFS_DELEG_GROUP_SETS
:
4563 case ZFS_DELEG_EVERYONE
:
4564 case ZFS_DELEG_EVERYONE_SETS
:
4565 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4566 avl
= fsperm
->fsp_uge_avl
;
4570 assert(!"unhandled zfs_deleg_who_type_t");
4574 who_perm_node_t
*found_node
= NULL
;
4575 who_perm_node_t
*node
= safe_malloc(
4576 sizeof (who_perm_node_t
));
4577 who_perm
= &node
->who_perm
;
4578 uu_avl_index_t idx
= 0;
4580 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4581 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4583 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4585 if (avl
== fsperm
->fsp_uge_avl
) {
4587 struct passwd
*p
= NULL
;
4588 struct group
*g
= NULL
;
4589 const char *nice_name
= NULL
;
4591 switch (perm_type
) {
4592 case ZFS_DELEG_USER_SETS
:
4593 case ZFS_DELEG_USER
:
4594 rid
= atoi(perm_name
);
4597 nice_name
= p
->pw_name
;
4599 case ZFS_DELEG_GROUP_SETS
:
4600 case ZFS_DELEG_GROUP
:
4601 rid
= atoi(perm_name
);
4604 nice_name
= g
->gr_name
;
4611 if (nice_name
!= NULL
)
4613 node
->who_perm
.who_ug_name
,
4617 uu_avl_insert(avl
, node
, idx
);
4620 who_perm
= &node
->who_perm
;
4624 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4631 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4633 nvpair_t
*nvp
= NULL
;
4634 uu_avl_index_t idx
= 0;
4636 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4637 nvlist_t
*nvl2
= NULL
;
4638 const char *fsname
= nvpair_name(nvp
);
4639 data_type_t type
= nvpair_type(nvp
);
4640 fs_perm_t
*fsperm
= NULL
;
4641 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4645 fsperm
= &node
->fspn_fsperm
;
4647 assert(DATA_TYPE_NVLIST
== type
);
4649 uu_list_node_init(node
, &node
->fspn_list_node
,
4650 fspset
->fsps_list_pool
);
4652 idx
= uu_list_numnodes(fspset
->fsps_list
);
4653 fs_perm_init(fsperm
, fspset
, fsname
);
4655 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4658 (void) parse_fs_perm(fsperm
, nvl2
);
4660 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4666 static inline const char *
4667 deleg_perm_comment(zfs_deleg_note_t note
)
4669 const char *str
= "";
4674 case ZFS_DELEG_NOTE_ALLOW
:
4675 str
= gettext("Must also have the permission that is being"
4676 "\n\t\t\t\tallowed");
4678 case ZFS_DELEG_NOTE_CLONE
:
4679 str
= gettext("Must also have the 'create' ability and 'mount'"
4680 "\n\t\t\t\tability in the origin file system");
4682 case ZFS_DELEG_NOTE_CREATE
:
4683 str
= gettext("Must also have the 'mount' ability");
4685 case ZFS_DELEG_NOTE_DESTROY
:
4686 str
= gettext("Must also have the 'mount' ability");
4688 case ZFS_DELEG_NOTE_DIFF
:
4689 str
= gettext("Allows lookup of paths within a dataset;"
4690 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4691 "\n\t\t\t\tin order to use zfs diff");
4693 case ZFS_DELEG_NOTE_HOLD
:
4694 str
= gettext("Allows adding a user hold to a snapshot");
4696 case ZFS_DELEG_NOTE_MOUNT
:
4697 str
= gettext("Allows mount/umount of ZFS datasets");
4699 case ZFS_DELEG_NOTE_PROMOTE
:
4700 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4701 " 'promote' ability in the origin file system");
4703 case ZFS_DELEG_NOTE_RECEIVE
:
4704 str
= gettext("Must also have the 'mount' and 'create'"
4707 case ZFS_DELEG_NOTE_RELEASE
:
4708 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4709 "might destroy the snapshot");
4711 case ZFS_DELEG_NOTE_RENAME
:
4712 str
= gettext("Must also have the 'mount' and 'create'"
4713 "\n\t\t\t\tability in the new parent");
4715 case ZFS_DELEG_NOTE_ROLLBACK
:
4718 case ZFS_DELEG_NOTE_SEND
:
4721 case ZFS_DELEG_NOTE_SHARE
:
4722 str
= gettext("Allows sharing file systems over NFS or SMB"
4723 "\n\t\t\t\tprotocols");
4725 case ZFS_DELEG_NOTE_SNAPSHOT
:
4729 * case ZFS_DELEG_NOTE_VSCAN:
4730 * str = gettext("");
4734 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4735 str
= gettext("Allows accessing any groupquota@... property");
4737 case ZFS_DELEG_NOTE_GROUPUSED
:
4738 str
= gettext("Allows reading any groupused@... property");
4740 case ZFS_DELEG_NOTE_USERPROP
:
4741 str
= gettext("Allows changing any user property");
4743 case ZFS_DELEG_NOTE_USERQUOTA
:
4744 str
= gettext("Allows accessing any userquota@... property");
4746 case ZFS_DELEG_NOTE_USERUSED
:
4747 str
= gettext("Allows reading any userused@... property");
4765 boolean_t recursive
; /* unallow only */
4766 boolean_t prt_usage
;
4768 boolean_t prt_perms
;
4771 const char *dataset
;
4775 prop_cmp(const void *a
, const void *b
)
4777 const char *str1
= *(const char **)a
;
4778 const char *str2
= *(const char **)b
;
4779 return (strcmp(str1
, str2
));
4783 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
4785 const char *opt_desc
[] = {
4786 "-h", gettext("show this help message and exit"),
4787 "-l", gettext("set permission locally"),
4788 "-d", gettext("set permission for descents"),
4789 "-u", gettext("set permission for user"),
4790 "-g", gettext("set permission for group"),
4791 "-e", gettext("set permission for everyone"),
4792 "-c", gettext("set create time permission"),
4793 "-s", gettext("define permission set"),
4795 "-r", gettext("remove permissions recursively"),
4797 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
4798 size_t allow_size
= unallow_size
- 2;
4799 const char *props
[ZFS_NUM_PROPS
];
4802 FILE *fp
= requested
? stdout
: stderr
;
4803 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
4804 const char *fmt
= gettext("%-16s %-14s\t%s\n");
4806 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
4808 (void) fprintf(fp
, gettext("Options:\n"));
4809 for (int i
= 0; i
< (un
? unallow_size
: allow_size
); i
++) {
4810 const char *opt
= opt_desc
[i
++];
4811 const char *optdsc
= opt_desc
[i
];
4812 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
4815 (void) fprintf(fp
, gettext("\nThe following permissions are "
4817 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
4819 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
4820 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
4821 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
4822 const char *perm_type
= deleg_perm_type(perm_note
);
4823 const char *perm_comment
= deleg_perm_comment(perm_note
);
4824 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
4827 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
4828 zprop_desc_t
*pd
= &pdtbl
[i
];
4829 if (pd
->pd_visible
!= B_TRUE
)
4832 if (pd
->pd_attr
== PROP_READONLY
)
4835 props
[count
++] = pd
->pd_name
;
4837 props
[count
] = NULL
;
4839 qsort(props
, count
, sizeof (char *), prop_cmp
);
4841 for (i
= 0; i
< count
; i
++)
4842 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
4845 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
4847 exit(requested
? 0 : 2);
4850 static inline const char *
4851 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
4854 if (un
&& argc
== expected_argc
- 1)
4856 else if (argc
== expected_argc
)
4857 *permsp
= argv
[argc
- 2];
4859 allow_usage(un
, B_FALSE
,
4860 gettext("wrong number of parameters\n"));
4862 return (argv
[argc
- 1]);
4866 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
4868 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
4869 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
4870 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
4871 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
4874 allow_usage(un
, B_FALSE
,
4875 gettext("-u, -g, and -e are mutually exclusive\n"));
4877 if (opts
->prt_usage
) {
4878 if (argc
== 0 && all_sum
== 0)
4879 allow_usage(un
, B_TRUE
, NULL
);
4886 allow_usage(un
, B_FALSE
,
4887 gettext("invalid options combined with -s\n"));
4889 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4890 if (argv
[0][0] != '@')
4891 allow_usage(un
, B_FALSE
,
4892 gettext("invalid set name: missing '@' prefix\n"));
4893 opts
->who
= argv
[0];
4894 } else if (opts
->create
) {
4895 if (ldcsuge_sum
> 1)
4896 allow_usage(un
, B_FALSE
,
4897 gettext("invalid options combined with -c\n"));
4898 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4899 } else if (opts
->everyone
) {
4901 allow_usage(un
, B_FALSE
,
4902 gettext("invalid options combined with -e\n"));
4903 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4904 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
4906 opts
->everyone
= B_TRUE
;
4909 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4910 } else if (argc
== 1 && !un
) {
4911 opts
->prt_perms
= B_TRUE
;
4912 opts
->dataset
= argv
[argc
-1];
4914 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4915 opts
->who
= argv
[0];
4918 if (!opts
->local
&& !opts
->descend
) {
4919 opts
->local
= B_TRUE
;
4920 opts
->descend
= B_TRUE
;
4925 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
4926 const char *who
, char *perms
, nvlist_t
*top_nvl
)
4929 char ld
[2] = { '\0', '\0' };
4930 char who_buf
[MAXNAMELEN
+ 32];
4931 char base_type
= '\0';
4932 char set_type
= '\0';
4933 nvlist_t
*base_nvl
= NULL
;
4934 nvlist_t
*set_nvl
= NULL
;
4937 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4939 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4943 case ZFS_DELEG_NAMED_SET_SETS
:
4944 case ZFS_DELEG_NAMED_SET
:
4945 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
4946 base_type
= ZFS_DELEG_NAMED_SET
;
4947 ld
[0] = ZFS_DELEG_NA
;
4949 case ZFS_DELEG_CREATE_SETS
:
4950 case ZFS_DELEG_CREATE
:
4951 set_type
= ZFS_DELEG_CREATE_SETS
;
4952 base_type
= ZFS_DELEG_CREATE
;
4953 ld
[0] = ZFS_DELEG_NA
;
4955 case ZFS_DELEG_USER_SETS
:
4956 case ZFS_DELEG_USER
:
4957 set_type
= ZFS_DELEG_USER_SETS
;
4958 base_type
= ZFS_DELEG_USER
;
4960 ld
[0] = ZFS_DELEG_LOCAL
;
4962 ld
[1] = ZFS_DELEG_DESCENDENT
;
4964 case ZFS_DELEG_GROUP_SETS
:
4965 case ZFS_DELEG_GROUP
:
4966 set_type
= ZFS_DELEG_GROUP_SETS
;
4967 base_type
= ZFS_DELEG_GROUP
;
4969 ld
[0] = ZFS_DELEG_LOCAL
;
4971 ld
[1] = ZFS_DELEG_DESCENDENT
;
4973 case ZFS_DELEG_EVERYONE_SETS
:
4974 case ZFS_DELEG_EVERYONE
:
4975 set_type
= ZFS_DELEG_EVERYONE_SETS
;
4976 base_type
= ZFS_DELEG_EVERYONE
;
4978 ld
[0] = ZFS_DELEG_LOCAL
;
4980 ld
[1] = ZFS_DELEG_DESCENDENT
;
4984 assert(set_type
!= '\0' && base_type
!= '\0');
4987 if (perms
!= NULL
) {
4989 char *end
= curr
+ strlen(perms
);
4991 while (curr
< end
) {
4992 char *delim
= strchr(curr
, ',');
5003 (void) nvlist_add_boolean(nvl
, curr
);
5009 for (i
= 0; i
< 2; i
++) {
5010 char locality
= ld
[i
];
5014 if (!nvlist_empty(base_nvl
)) {
5016 (void) snprintf(who_buf
,
5017 sizeof (who_buf
), "%c%c$%s",
5018 base_type
, locality
, who
);
5020 (void) snprintf(who_buf
,
5021 sizeof (who_buf
), "%c%c$",
5022 base_type
, locality
);
5024 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5029 if (!nvlist_empty(set_nvl
)) {
5031 (void) snprintf(who_buf
,
5032 sizeof (who_buf
), "%c%c$%s",
5033 set_type
, locality
, who
);
5035 (void) snprintf(who_buf
,
5036 sizeof (who_buf
), "%c%c$",
5037 set_type
, locality
);
5039 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5044 for (i
= 0; i
< 2; i
++) {
5045 char locality
= ld
[i
];
5050 (void) snprintf(who_buf
, sizeof (who_buf
),
5051 "%c%c$%s", base_type
, locality
, who
);
5053 (void) snprintf(who_buf
, sizeof (who_buf
),
5054 "%c%c$", base_type
, locality
);
5055 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5058 (void) snprintf(who_buf
, sizeof (who_buf
),
5059 "%c%c$%s", set_type
, locality
, who
);
5061 (void) snprintf(who_buf
, sizeof (who_buf
),
5062 "%c%c$", set_type
, locality
);
5063 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5069 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
5071 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
5075 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
5076 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
5077 } else if (opts
->create
) {
5078 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
5079 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5080 } else if (opts
->everyone
) {
5081 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
5082 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5084 char *curr
= opts
->who
;
5085 char *end
= curr
+ strlen(curr
);
5087 while (curr
< end
) {
5089 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
5091 char *delim
= strchr(curr
, ',');
5094 struct passwd
*p
= NULL
;
5095 struct group
*g
= NULL
;
5103 rid
= (uid_t
)strtol(curr
, &endch
, 0);
5105 who_type
= ZFS_DELEG_USER
;
5114 (void) snprintf(errbuf
, 256, gettext(
5115 "invalid user %s"), curr
);
5116 allow_usage(un
, B_TRUE
, errbuf
);
5118 } else if (opts
->group
) {
5119 who_type
= ZFS_DELEG_GROUP
;
5128 (void) snprintf(errbuf
, 256, gettext(
5129 "invalid group %s"), curr
);
5130 allow_usage(un
, B_TRUE
, errbuf
);
5133 if (*endch
!= '\0') {
5140 if (*endch
!= '\0') {
5148 who_type
= ZFS_DELEG_USER
;
5150 } else if (g
!= NULL
) {
5151 who_type
= ZFS_DELEG_GROUP
;
5154 (void) snprintf(errbuf
, 256, gettext(
5155 "invalid user/group %s"), curr
);
5156 allow_usage(un
, B_TRUE
, errbuf
);
5160 (void) sprintf(id
, "%u", rid
);
5163 store_allow_perm(who_type
, opts
->local
,
5164 opts
->descend
, who
, opts
->perms
, *nvlp
);
5173 print_set_creat_perms(uu_avl_t
*who_avl
)
5175 const char *sc_title
[] = {
5176 gettext("Permission sets:\n"),
5177 gettext("Create time permissions:\n"),
5180 const char **title_ptr
= sc_title
;
5181 who_perm_node_t
*who_node
= NULL
;
5182 int prev_weight
= -1;
5184 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
5185 who_node
= uu_avl_next(who_avl
, who_node
)) {
5186 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5187 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5188 const char *who_name
= who_node
->who_perm
.who_name
;
5189 int weight
= who_type2weight(who_type
);
5190 boolean_t first
= B_TRUE
;
5191 deleg_perm_node_t
*deleg_node
;
5193 if (prev_weight
!= weight
) {
5194 (void) printf(*title_ptr
++);
5195 prev_weight
= weight
;
5198 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
5199 (void) printf("\t");
5201 (void) printf("\t%s ", who_name
);
5203 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
5204 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5207 deleg_node
->dpn_perm
.dp_name
);
5210 (void) printf(",%s",
5211 deleg_node
->dpn_perm
.dp_name
);
5214 (void) printf("\n");
5219 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
5222 who_perm_node_t
*who_node
= NULL
;
5223 boolean_t prt_title
= B_TRUE
;
5224 uu_avl_walk_t
*walk
;
5226 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
5229 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
5230 const char *who_name
= who_node
->who_perm
.who_name
;
5231 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
5232 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5233 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5235 deleg_perm_node_t
*deleg_node
;
5236 boolean_t prt_who
= B_TRUE
;
5238 for (deleg_node
= uu_avl_first(avl
);
5240 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5241 if (local
!= deleg_node
->dpn_perm
.dp_local
||
5242 descend
!= deleg_node
->dpn_perm
.dp_descend
)
5246 const char *who
= NULL
;
5248 prt_title
= B_FALSE
;
5249 (void) printf(title
);
5253 case ZFS_DELEG_USER_SETS
:
5254 case ZFS_DELEG_USER
:
5255 who
= gettext("user");
5257 who_name
= nice_who_name
;
5259 case ZFS_DELEG_GROUP_SETS
:
5260 case ZFS_DELEG_GROUP
:
5261 who
= gettext("group");
5263 who_name
= nice_who_name
;
5265 case ZFS_DELEG_EVERYONE_SETS
:
5266 case ZFS_DELEG_EVERYONE
:
5267 who
= gettext("everyone");
5272 assert(who
!= NULL
);
5276 if (who_name
== NULL
)
5277 (void) printf("\t%s", who
);
5279 (void) printf("\t%s %s", who
, who_name
);
5282 (void) printf("%c%s", delim
,
5283 deleg_node
->dpn_perm
.dp_name
);
5288 (void) printf("\n");
5291 uu_avl_walk_end(walk
);
5295 print_fs_perms(fs_perm_set_t
*fspset
)
5297 fs_perm_node_t
*node
= NULL
;
5298 char buf
[MAXNAMELEN
+ 32];
5299 const char *dsname
= buf
;
5301 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
5302 node
= uu_list_next(fspset
->fsps_list
, node
)) {
5303 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
5304 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
5307 (void) snprintf(buf
, sizeof (buf
),
5308 gettext("---- Permissions on %s "),
5309 node
->fspn_fsperm
.fsp_name
);
5310 (void) printf(dsname
);
5311 left
= 70 - strlen(buf
);
5314 (void) printf("\n");
5316 print_set_creat_perms(sc_avl
);
5317 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
5318 gettext("Local permissions:\n"));
5319 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
5320 gettext("Descendent permissions:\n"));
5321 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
5322 gettext("Local+Descendent permissions:\n"));
5326 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5328 struct deleg_perms
{
5334 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5336 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5337 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5339 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5342 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5346 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5349 nvlist_t
*perm_nvl
= NULL
;
5350 nvlist_t
*update_perm_nvl
= NULL
;
5353 struct allow_opts opts
= { 0 };
5355 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5358 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5361 opts
.local
= B_TRUE
;
5364 opts
.descend
= B_TRUE
;
5370 opts
.group
= B_TRUE
;
5373 opts
.everyone
= B_TRUE
;
5379 opts
.create
= B_TRUE
;
5382 opts
.recursive
= B_TRUE
;
5385 (void) fprintf(stderr
, gettext("missing argument for "
5386 "'%c' option\n"), optopt
);
5390 opts
.prt_usage
= B_TRUE
;
5393 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5402 /* check arguments */
5403 parse_allow_args(argc
, argv
, un
, &opts
);
5405 /* try to open the dataset */
5406 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5407 ZFS_TYPE_VOLUME
)) == NULL
) {
5408 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5413 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5416 fs_perm_set_init(&fs_perm_set
);
5417 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5418 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5423 print_fs_perms(&fs_perm_set
);
5425 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5426 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5429 if (un
&& opts
.recursive
) {
5430 struct deleg_perms data
= { un
, update_perm_nvl
};
5431 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5440 nvlist_free(perm_nvl
);
5441 nvlist_free(update_perm_nvl
);
5443 fs_perm_set_fini(&fs_perm_set
);
5451 zfs_do_allow(int argc
, char **argv
)
5453 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5457 zfs_do_unallow(int argc
, char **argv
)
5459 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5463 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5468 boolean_t recursive
= B_FALSE
;
5469 const char *opts
= holding
? "rt" : "r";
5473 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5479 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5488 /* check number of arguments */
5496 if (holding
&& tag
[0] == '.') {
5497 /* tags starting with '.' are reserved for libzfs */
5498 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5502 for (i
= 0; i
< argc
; ++i
) {
5504 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
5506 char *path
= argv
[i
];
5508 delim
= strchr(path
, '@');
5509 if (delim
== NULL
) {
5510 (void) fprintf(stderr
,
5511 gettext("'%s' is not a snapshot\n"), path
);
5515 (void) strncpy(parent
, path
, delim
- path
);
5516 parent
[delim
- path
] = '\0';
5518 zhp
= zfs_open(g_zfs
, parent
,
5519 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5525 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
5528 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5534 return (errors
!= 0);
5538 * zfs hold [-r] [-t] <tag> <snap> ...
5540 * -r Recursively hold
5542 * Apply a user-hold with the given tag to the list of snapshots.
5545 zfs_do_hold(int argc
, char **argv
)
5547 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5551 * zfs release [-r] <tag> <snap> ...
5553 * -r Recursively release
5555 * Release a user-hold with the given tag from the list of snapshots.
5558 zfs_do_release(int argc
, char **argv
)
5560 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5563 typedef struct holds_cbdata
{
5564 boolean_t cb_recursive
;
5565 const char *cb_snapname
;
5567 size_t cb_max_namelen
;
5568 size_t cb_max_taglen
;
5571 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5572 #define DATETIME_BUF_LEN (32)
5577 print_holds(boolean_t scripted
, size_t nwidth
, size_t tagwidth
, nvlist_t
*nvl
)
5580 nvpair_t
*nvp
= NULL
;
5581 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5585 for (i
= 0; i
< 3; i
++) {
5586 col
= gettext(hdr_cols
[i
]);
5588 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5591 (void) printf("%s\n", col
);
5595 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5596 char *zname
= nvpair_name(nvp
);
5598 nvpair_t
*nvp2
= NULL
;
5599 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5600 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5601 char tsbuf
[DATETIME_BUF_LEN
];
5602 char *tagname
= nvpair_name(nvp2
);
5607 (void) nvpair_value_uint64(nvp2
, &val
);
5609 (void) localtime_r(&time
, &t
);
5610 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5611 gettext(STRFTIME_FMT_STR
), &t
);
5614 (void) printf("%s\t%s\t%s\n", zname
,
5617 (void) printf("%-*s %-*s %s\n", nwidth
,
5618 zname
, tagwidth
, tagname
, tsbuf
);
5625 * Generic callback function to list a dataset or snapshot.
5628 holds_callback(zfs_handle_t
*zhp
, void *data
)
5630 holds_cbdata_t
*cbp
= data
;
5631 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5632 nvlist_t
*nvl
= NULL
;
5633 nvpair_t
*nvp
= NULL
;
5634 const char *zname
= zfs_get_name(zhp
);
5635 size_t znamelen
= strlen(zname
);
5637 if (cbp
->cb_recursive
) {
5638 const char *snapname
;
5639 char *delim
= strchr(zname
, '@');
5643 snapname
= delim
+ 1;
5644 if (strcmp(cbp
->cb_snapname
, snapname
))
5648 if (zfs_get_holds(zhp
, &nvl
) != 0)
5651 if (znamelen
> cbp
->cb_max_namelen
)
5652 cbp
->cb_max_namelen
= znamelen
;
5654 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5655 const char *tag
= nvpair_name(nvp
);
5656 size_t taglen
= strlen(tag
);
5657 if (taglen
> cbp
->cb_max_taglen
)
5658 cbp
->cb_max_taglen
= taglen
;
5661 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5665 * zfs holds [-r] <snap> ...
5667 * -r Recursively hold
5670 zfs_do_holds(int argc
, char **argv
)
5675 boolean_t scripted
= B_FALSE
;
5676 boolean_t recursive
= B_FALSE
;
5677 const char *opts
= "rH";
5680 int types
= ZFS_TYPE_SNAPSHOT
;
5681 holds_cbdata_t cb
= { 0 };
5688 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5697 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5704 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
5705 flags
|= ZFS_ITER_RECURSE
;
5711 /* check number of arguments */
5715 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5718 for (i
= 0; i
< argc
; ++i
) {
5719 char *snapshot
= argv
[i
];
5721 const char *snapname
;
5723 delim
= strchr(snapshot
, '@');
5724 if (delim
== NULL
) {
5725 (void) fprintf(stderr
,
5726 gettext("'%s' is not a snapshot\n"), snapshot
);
5730 snapname
= delim
+ 1;
5732 snapshot
[delim
- snapshot
] = '\0';
5734 cb
.cb_recursive
= recursive
;
5735 cb
.cb_snapname
= snapname
;
5739 * 1. collect holds data, set format options
5741 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
5742 holds_callback
, &cb
);
5748 * 2. print holds data
5750 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
5752 if (nvlist_empty(nvl
))
5753 (void) printf(gettext("no datasets available\n"));
5757 return (0 != errors
);
5760 #define CHECK_SPINNER 30
5761 #define SPINNER_TIME 3 /* seconds */
5762 #define MOUNT_TIME 1 /* seconds */
5764 typedef struct get_all_state
{
5765 boolean_t ga_verbose
;
5766 get_all_cb_t
*ga_cbp
;
5770 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
5772 static char *spin
[] = { "-", "\\", "|", "/" };
5773 static int spinval
= 0;
5774 static int spincheck
= 0;
5775 static time_t last_spin_time
= (time_t)0;
5776 get_all_state_t
*state
= data
;
5777 zfs_type_t type
= zfs_get_type(zhp
);
5779 if (state
->ga_verbose
) {
5780 if (--spincheck
< 0) {
5781 time_t now
= time(NULL
);
5782 if (last_spin_time
+ SPINNER_TIME
< now
) {
5783 update_progress(spin
[spinval
++ % 4]);
5784 last_spin_time
= now
;
5786 spincheck
= CHECK_SPINNER
;
5791 * Interate over any nested datasets.
5793 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
5799 * Skip any datasets whose type does not match.
5801 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
5805 libzfs_add_handle(state
->ga_cbp
, zhp
);
5806 assert(state
->ga_cbp
->cb_used
<= state
->ga_cbp
->cb_alloc
);
5812 get_all_datasets(get_all_cb_t
*cbp
, boolean_t verbose
)
5814 get_all_state_t state
= {
5815 .ga_verbose
= verbose
,
5820 set_progress_header(gettext("Reading ZFS config"));
5821 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &state
);
5824 finish_progress(gettext("done."));
5828 * Generic callback for sharing or mounting filesystems. Because the code is so
5829 * similar, we have a common function with an extra parameter to determine which
5830 * mode we are using.
5832 typedef enum { OP_SHARE
, OP_MOUNT
} share_mount_op_t
;
5834 typedef struct share_mount_state
{
5835 share_mount_op_t sm_op
;
5836 boolean_t sm_verbose
;
5839 char *sm_proto
; /* only valid for OP_SHARE */
5840 mutex_t sm_lock
; /* protects the remaining fields */
5841 uint_t sm_total
; /* number of filesystems to process */
5842 uint_t sm_done
; /* number of filesystems processed */
5843 int sm_status
; /* -1 if any of the share/mount operations failed */
5844 } share_mount_state_t
;
5847 * Share or mount a dataset.
5850 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
5851 boolean_t
explicit, const char *options
)
5853 char mountpoint
[ZFS_MAXPROPLEN
];
5854 char shareopts
[ZFS_MAXPROPLEN
];
5855 char smbshareopts
[ZFS_MAXPROPLEN
];
5856 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
5858 uint64_t zoned
, canmount
;
5859 boolean_t shared_nfs
, shared_smb
;
5861 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
5864 * Check to make sure we can mount/share this dataset. If we
5865 * are in the global zone and the filesystem is exported to a
5866 * local zone, or if we are in a local zone and the
5867 * filesystem is not exported, then it is an error.
5869 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
5871 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
5875 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5876 "dataset is exported to a local zone\n"), cmdname
,
5880 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
5884 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5885 "permission denied\n"), cmdname
,
5891 * Ignore any filesystems which don't apply to us. This
5892 * includes those with a legacy mountpoint, or those with
5893 * legacy share options.
5895 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
5896 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
5897 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
5898 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5899 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
5900 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5902 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
5903 strcmp(smbshareopts
, "off") == 0) {
5907 (void) fprintf(stderr
, gettext("cannot share '%s': "
5908 "legacy share\n"), zfs_get_name(zhp
));
5909 (void) fprintf(stderr
, gettext("use share(8) to "
5910 "share this filesystem, or set "
5911 "sharenfs property on\n"));
5916 * We cannot share or mount legacy filesystems. If the
5917 * shareopts is non-legacy but the mountpoint is legacy, we
5918 * treat it as a legacy share.
5920 if (strcmp(mountpoint
, "legacy") == 0) {
5924 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5925 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
5926 (void) fprintf(stderr
, gettext("use %s(8) to "
5927 "%s this filesystem\n"), cmdname
, cmdname
);
5931 if (strcmp(mountpoint
, "none") == 0) {
5935 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
5936 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
5941 * canmount explicit outcome
5942 * on no pass through
5943 * on yes pass through
5945 * off yes display error, return 1
5946 * noauto no return 0
5947 * noauto yes pass through
5949 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
5950 if (canmount
== ZFS_CANMOUNT_OFF
) {
5954 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5955 "'canmount' property is set to 'off'\n"), cmdname
,
5958 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
5963 * If this filesystem is inconsistent and has a receive resume
5964 * token, we can not mount it.
5966 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
5967 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
5968 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
5972 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5973 "Contains partially-completed state from "
5974 "\"zfs receive -r\", which can be resumed with "
5975 "\"zfs send -t\"\n"),
5976 cmdname
, zfs_get_name(zhp
));
5981 * At this point, we have verified that the mountpoint and/or
5982 * shareopts are appropriate for auto management. If the
5983 * filesystem is already mounted or shared, return (failing
5984 * for explicit requests); otherwise mount or share the
5990 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
5991 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
5993 if ((shared_nfs
&& shared_smb
) ||
5994 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
5995 strcmp(smbshareopts
, "off") == 0) ||
5996 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
5997 strcmp(shareopts
, "off") == 0)) {
6001 (void) fprintf(stderr
, gettext("cannot share "
6002 "'%s': filesystem already shared\n"),
6007 if (!zfs_is_mounted(zhp
, NULL
) &&
6008 zfs_mount(zhp
, NULL
, 0) != 0)
6011 if (protocol
== NULL
) {
6012 if (zfs_shareall(zhp
) != 0)
6014 } else if (strcmp(protocol
, "nfs") == 0) {
6015 if (zfs_share_nfs(zhp
))
6017 } else if (strcmp(protocol
, "smb") == 0) {
6018 if (zfs_share_smb(zhp
))
6021 (void) fprintf(stderr
, gettext("cannot share "
6022 "'%s': invalid share type '%s' "
6024 zfs_get_name(zhp
), protocol
);
6031 if (options
== NULL
)
6032 mnt
.mnt_mntopts
= "";
6034 mnt
.mnt_mntopts
= (char *)options
;
6036 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
6037 zfs_is_mounted(zhp
, NULL
)) {
6041 (void) fprintf(stderr
, gettext("cannot mount "
6042 "'%s': filesystem already mounted\n"),
6047 if (zfs_mount(zhp
, options
, flags
) != 0)
6056 * Reports progress in the form "(current/total)". Not thread-safe.
6059 report_mount_progress(int current
, int total
)
6061 static time_t last_progress_time
= 0;
6062 time_t now
= time(NULL
);
6065 /* report 1..n instead of 0..n-1 */
6068 /* display header if we're here for the first time */
6070 set_progress_header(gettext("Mounting ZFS filesystems"));
6071 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
6072 /* too soon to report again */
6076 last_progress_time
= now
;
6078 (void) sprintf(info
, "(%d/%d)", current
, total
);
6080 if (current
== total
)
6081 finish_progress(info
);
6083 update_progress(info
);
6087 * zfs_foreach_mountpoint() callback that mounts or shares one filesystem and
6088 * updates the progress meter.
6091 share_mount_one_cb(zfs_handle_t
*zhp
, void *arg
)
6093 share_mount_state_t
*sms
= arg
;
6096 ret
= share_mount_one(zhp
, sms
->sm_op
, sms
->sm_flags
, sms
->sm_proto
,
6097 B_FALSE
, sms
->sm_options
);
6099 mutex_enter(&sms
->sm_lock
);
6101 sms
->sm_status
= ret
;
6103 if (sms
->sm_verbose
)
6104 report_mount_progress(sms
->sm_done
, sms
->sm_total
);
6105 mutex_exit(&sms
->sm_lock
);
6110 append_options(char *mntopts
, char *newopts
)
6112 int len
= strlen(mntopts
);
6114 /* original length plus new string to append plus 1 for the comma */
6115 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
6116 (void) fprintf(stderr
, gettext("the opts argument for "
6117 "'%c' option is too long (more than %d chars)\n"),
6118 "-o", MNT_LINE_MAX
);
6123 mntopts
[len
++] = ',';
6125 (void) strcpy(&mntopts
[len
], newopts
);
6129 share_mount(int op
, int argc
, char **argv
)
6132 boolean_t verbose
= B_FALSE
;
6134 char *options
= NULL
;
6138 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
6148 if (*optarg
== '\0') {
6149 (void) fprintf(stderr
, gettext("empty mount "
6150 "options (-o) specified\n"));
6154 if (options
== NULL
)
6155 options
= safe_malloc(MNT_LINE_MAX
+ 1);
6157 /* option validation is done later */
6158 append_options(options
, optarg
);
6162 flags
|= MS_OVERLAY
;
6165 (void) fprintf(stderr
, gettext("missing argument for "
6166 "'%c' option\n"), optopt
);
6170 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6179 /* check number of arguments */
6181 char *protocol
= NULL
;
6183 if (op
== OP_SHARE
&& argc
> 0) {
6184 if (strcmp(argv
[0], "nfs") != 0 &&
6185 strcmp(argv
[0], "smb") != 0) {
6186 (void) fprintf(stderr
, gettext("share type "
6187 "must be 'nfs' or 'smb'\n"));
6196 (void) fprintf(stderr
, gettext("too many arguments\n"));
6200 start_progress_timer();
6201 get_all_cb_t cb
= { 0 };
6202 get_all_datasets(&cb
, verbose
);
6204 if (cb
.cb_used
== 0)
6207 if (op
== OP_SHARE
) {
6208 sa_init_selective_arg_t sharearg
;
6209 sharearg
.zhandle_arr
= cb
.cb_handles
;
6210 sharearg
.zhandle_len
= cb
.cb_used
;
6211 if ((ret
= zfs_init_libshare_arg(g_zfs
,
6212 SA_INIT_SHARE_API_SELECTIVE
, &sharearg
)) != SA_OK
) {
6213 (void) fprintf(stderr
, gettext(
6214 "Could not initialize libshare, %d"), ret
);
6219 share_mount_state_t share_mount_state
= { 0 };
6220 share_mount_state
.sm_op
= op
;
6221 share_mount_state
.sm_verbose
= verbose
;
6222 share_mount_state
.sm_flags
= flags
;
6223 share_mount_state
.sm_options
= options
;
6224 share_mount_state
.sm_proto
= protocol
;
6225 share_mount_state
.sm_total
= cb
.cb_used
;
6226 (void) mutex_init(&share_mount_state
.sm_lock
,
6227 LOCK_NORMAL
| LOCK_ERRORCHECK
, NULL
);
6229 * libshare isn't mt-safe, so only do the operation in parallel
6230 * if we're mounting.
6232 zfs_foreach_mountpoint(g_zfs
, cb
.cb_handles
, cb
.cb_used
,
6233 share_mount_one_cb
, &share_mount_state
, op
== OP_MOUNT
);
6234 ret
= share_mount_state
.sm_status
;
6236 for (int i
= 0; i
< cb
.cb_used
; i
++)
6237 zfs_close(cb
.cb_handles
[i
]);
6238 free(cb
.cb_handles
);
6239 } else if (argc
== 0) {
6240 struct mnttab entry
;
6242 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
6243 (void) fprintf(stderr
, gettext("missing filesystem "
6244 "argument (specify -a for all)\n"));
6249 * When mount is given no arguments, go through /etc/mnttab and
6250 * display any active ZFS mounts. We hide any snapshots, since
6251 * they are controlled automatically.
6253 rewind(mnttab_file
);
6254 while (getmntent(mnttab_file
, &entry
) == 0) {
6255 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
6256 strchr(entry
.mnt_special
, '@') != NULL
)
6259 (void) printf("%-30s %s\n", entry
.mnt_special
,
6267 (void) fprintf(stderr
,
6268 gettext("too many arguments\n"));
6272 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6273 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6276 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
6286 * zfs mount -a [nfs]
6287 * zfs mount filesystem
6289 * Mount all filesystems, or mount the given filesystem.
6292 zfs_do_mount(int argc
, char **argv
)
6294 return (share_mount(OP_MOUNT
, argc
, argv
));
6298 * zfs share -a [nfs | smb]
6299 * zfs share filesystem
6301 * Share all filesystems, or share the given filesystem.
6304 zfs_do_share(int argc
, char **argv
)
6306 return (share_mount(OP_SHARE
, argc
, argv
));
6309 typedef struct unshare_unmount_node
{
6310 zfs_handle_t
*un_zhp
;
6312 uu_avl_node_t un_avlnode
;
6313 } unshare_unmount_node_t
;
6317 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
6319 const unshare_unmount_node_t
*l
= larg
;
6320 const unshare_unmount_node_t
*r
= rarg
;
6322 return (strcmp(l
->un_mountp
, r
->un_mountp
));
6326 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6327 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6328 * and unmount it appropriately.
6331 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
6335 struct stat64 statbuf
;
6336 struct extmnttab entry
;
6337 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
6341 * Search for the path in /etc/mnttab. Rather than looking for the
6342 * specific path, which can be fooled by non-standard paths (i.e. ".."
6343 * or "//"), we stat() the path and search for the corresponding
6344 * (major,minor) device pair.
6346 if (stat64(path
, &statbuf
) != 0) {
6347 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6348 cmdname
, path
, strerror(errno
));
6351 path_inode
= statbuf
.st_ino
;
6354 * Search for the given (major,minor) pair in the mount table.
6356 rewind(mnttab_file
);
6357 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
6358 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
6359 entry
.mnt_minor
== minor(statbuf
.st_dev
))
6363 if (op
== OP_SHARE
) {
6364 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
6365 "currently mounted\n"), cmdname
, path
);
6368 (void) fprintf(stderr
, gettext("warning: %s not in mnttab\n"),
6370 if ((ret
= umount2(path
, flags
)) != 0)
6371 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
6376 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
6377 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
6378 "filesystem\n"), cmdname
, path
);
6382 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6383 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6387 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
6388 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6389 cmdname
, path
, strerror(errno
));
6391 } else if (statbuf
.st_ino
!= path_inode
) {
6392 (void) fprintf(stderr
, gettext("cannot "
6393 "%s '%s': not a mountpoint\n"), cmdname
, path
);
6397 if (op
== OP_SHARE
) {
6398 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6399 char smbshare_prop
[ZFS_MAXPROPLEN
];
6401 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
6402 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6403 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
6404 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6406 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6407 strcmp(smbshare_prop
, "off") == 0) {
6408 (void) fprintf(stderr
, gettext("cannot unshare "
6409 "'%s': legacy share\n"), path
);
6410 (void) fprintf(stderr
, gettext("use "
6411 "unshare(8) to unshare this filesystem\n"));
6412 } else if (!zfs_is_shared(zhp
)) {
6413 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6414 "not currently shared\n"), path
);
6416 ret
= zfs_unshareall_bypath(zhp
, path
);
6419 char mtpt_prop
[ZFS_MAXPROPLEN
];
6421 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6422 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6425 ret
= zfs_unmount(zhp
, NULL
, flags
);
6426 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6427 (void) fprintf(stderr
, gettext("cannot unmount "
6428 "'%s': legacy mountpoint\n"),
6430 (void) fprintf(stderr
, gettext("use umount(8) "
6431 "to unmount this filesystem\n"));
6433 ret
= zfs_unmountall(zhp
, flags
);
6444 * Generic callback for unsharing or unmounting a filesystem.
6447 unshare_unmount(int op
, int argc
, char **argv
)
6454 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6455 char sharesmb
[ZFS_MAXPROPLEN
];
6458 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
6467 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6478 * We could make use of zfs_for_each() to walk all datasets in
6479 * the system, but this would be very inefficient, especially
6480 * since we would have to linearly search /etc/mnttab for each
6481 * one. Instead, do one pass through /etc/mnttab looking for
6482 * zfs entries and call zfs_unmount() for each one.
6484 * Things get a little tricky if the administrator has created
6485 * mountpoints beneath other ZFS filesystems. In this case, we
6486 * have to unmount the deepest filesystems first. To accomplish
6487 * this, we place all the mountpoints in an AVL tree sorted by
6488 * the special type (dataset name), and walk the result in
6489 * reverse to make sure to get any snapshots first.
6491 struct mnttab entry
;
6492 uu_avl_pool_t
*pool
;
6493 uu_avl_t
*tree
= NULL
;
6494 unshare_unmount_node_t
*node
;
6496 uu_avl_walk_t
*walk
;
6499 (void) fprintf(stderr
, gettext("too many arguments\n"));
6503 if (((pool
= uu_avl_pool_create("unmount_pool",
6504 sizeof (unshare_unmount_node_t
),
6505 offsetof(unshare_unmount_node_t
, un_avlnode
),
6506 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6507 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6510 rewind(mnttab_file
);
6511 while (getmntent(mnttab_file
, &entry
) == 0) {
6513 /* ignore non-ZFS entries */
6514 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6517 /* ignore snapshots */
6518 if (strchr(entry
.mnt_special
, '@') != NULL
)
6521 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6522 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6528 * Ignore datasets that are excluded/restricted by
6531 if (zpool_skip_pool(zfs_get_pool_name(zhp
))) {
6538 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6540 sizeof (nfs_mnt_prop
),
6541 NULL
, NULL
, 0, B_FALSE
) == 0);
6542 if (strcmp(nfs_mnt_prop
, "off") != 0)
6544 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6546 sizeof (nfs_mnt_prop
),
6547 NULL
, NULL
, 0, B_FALSE
) == 0);
6548 if (strcmp(nfs_mnt_prop
, "off") == 0)
6552 /* Ignore legacy mounts */
6553 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6555 sizeof (nfs_mnt_prop
),
6556 NULL
, NULL
, 0, B_FALSE
) == 0);
6557 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6559 /* Ignore canmount=noauto mounts */
6560 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6561 ZFS_CANMOUNT_NOAUTO
)
6567 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6569 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6571 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6573 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6574 uu_avl_insert(tree
, node
, idx
);
6576 zfs_close(node
->un_zhp
);
6577 free(node
->un_mountp
);
6583 * Walk the AVL tree in reverse, unmounting each filesystem and
6584 * removing it from the AVL tree in the process.
6586 if ((walk
= uu_avl_walk_start(tree
,
6587 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6590 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6591 uu_avl_remove(tree
, node
);
6595 if (zfs_unshareall_bypath(node
->un_zhp
,
6596 node
->un_mountp
) != 0)
6601 if (zfs_unmount(node
->un_zhp
,
6602 node
->un_mountp
, flags
) != 0)
6607 zfs_close(node
->un_zhp
);
6608 free(node
->un_mountp
);
6612 uu_avl_walk_end(walk
);
6613 uu_avl_destroy(tree
);
6614 uu_avl_pool_destroy(pool
);
6619 (void) fprintf(stderr
,
6620 gettext("missing filesystem argument\n"));
6622 (void) fprintf(stderr
,
6623 gettext("too many arguments\n"));
6628 * We have an argument, but it may be a full path or a ZFS
6629 * filesystem. Pass full paths off to unmount_path() (shared by
6630 * manual_unmount), otherwise open the filesystem and pass to
6633 if (argv
[0][0] == '/')
6634 return (unshare_unmount_path(op
, argv
[0],
6637 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6638 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6641 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
6642 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
6643 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
6644 NULL
, 0, B_FALSE
) == 0);
6648 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6650 sizeof (nfs_mnt_prop
),
6651 NULL
, NULL
, 0, B_FALSE
) == 0);
6652 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6653 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
6656 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6657 strcmp(sharesmb
, "off") == 0) {
6658 (void) fprintf(stderr
, gettext("cannot "
6659 "unshare '%s': legacy share\n"),
6661 (void) fprintf(stderr
, gettext("use "
6662 "unshare(8) to unshare this "
6665 } else if (!zfs_is_shared(zhp
)) {
6666 (void) fprintf(stderr
, gettext("cannot "
6667 "unshare '%s': not currently "
6668 "shared\n"), zfs_get_name(zhp
));
6670 } else if (zfs_unshareall(zhp
) != 0) {
6676 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
6677 (void) fprintf(stderr
, gettext("cannot "
6678 "unmount '%s': legacy "
6679 "mountpoint\n"), zfs_get_name(zhp
));
6680 (void) fprintf(stderr
, gettext("use "
6681 "umount(8) to unmount this "
6684 } else if (!zfs_is_mounted(zhp
, NULL
)) {
6685 (void) fprintf(stderr
, gettext("cannot "
6686 "unmount '%s': not currently "
6690 } else if (zfs_unmountall(zhp
, flags
) != 0) {
6704 * zfs unmount filesystem
6706 * Unmount all filesystems, or a specific ZFS filesystem.
6709 zfs_do_unmount(int argc
, char **argv
)
6711 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
6716 * zfs unshare filesystem
6718 * Unshare all filesystems, or a specific ZFS filesystem.
6721 zfs_do_unshare(int argc
, char **argv
)
6723 return (unshare_unmount(OP_SHARE
, argc
, argv
));
6727 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6728 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6731 manual_mount(int argc
, char **argv
)
6734 char mountpoint
[ZFS_MAXPROPLEN
];
6735 char mntopts
[MNT_LINE_MAX
] = { '\0' };
6739 char *dataset
, *path
;
6742 while ((c
= getopt(argc
, argv
, ":mo:O")) != -1) {
6745 (void) strlcpy(mntopts
, optarg
, sizeof (mntopts
));
6748 flags
|= MS_OVERLAY
;
6751 flags
|= MS_NOMNTTAB
;
6754 (void) fprintf(stderr
, gettext("missing argument for "
6755 "'%c' option\n"), optopt
);
6759 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6761 (void) fprintf(stderr
, gettext("usage: mount [-o opts] "
6770 /* check that we only have two arguments */
6773 (void) fprintf(stderr
, gettext("missing dataset "
6776 (void) fprintf(stderr
,
6777 gettext("missing mountpoint argument\n"));
6779 (void) fprintf(stderr
, gettext("too many arguments\n"));
6780 (void) fprintf(stderr
, "usage: mount <dataset> <mountpoint>\n");
6787 /* try to open the dataset */
6788 if ((zhp
= zfs_open(g_zfs
, dataset
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6791 (void) zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
6792 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
);
6794 /* check for legacy mountpoint and complain appropriately */
6796 if (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) == 0) {
6797 if (mount(dataset
, path
, MS_OPTIONSTR
| flags
, MNTTYPE_ZFS
,
6798 NULL
, 0, mntopts
, sizeof (mntopts
)) != 0) {
6799 (void) fprintf(stderr
, gettext("mount failed: %s\n"),
6804 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
6805 "mounted using 'mount -F zfs'\n"), dataset
);
6806 (void) fprintf(stderr
, gettext("Use 'zfs set mountpoint=%s' "
6807 "instead.\n"), path
);
6808 (void) fprintf(stderr
, gettext("If you must use 'mount -F zfs' "
6809 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
6810 (void) fprintf(stderr
, gettext("See zfs(8) for more "
6819 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6820 * unmounts of non-legacy filesystems, as this is the dominant administrative
6824 manual_unmount(int argc
, char **argv
)
6830 while ((c
= getopt(argc
, argv
, "f")) != -1) {
6836 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6838 (void) fprintf(stderr
, gettext("usage: unmount [-f] "
6847 /* check arguments */
6850 (void) fprintf(stderr
, gettext("missing path "
6853 (void) fprintf(stderr
, gettext("too many arguments\n"));
6854 (void) fprintf(stderr
, gettext("usage: unmount [-f] <path>\n"));
6858 return (unshare_unmount_path(OP_MOUNT
, argv
[0], flags
, B_TRUE
));
6862 find_command_idx(char *command
, int *idx
)
6866 for (i
= 0; i
< NCOMMAND
; i
++) {
6867 if (command_table
[i
].name
== NULL
)
6870 if (strcmp(command
, command_table
[i
].name
) == 0) {
6879 zfs_do_diff(int argc
, char **argv
)
6883 char *tosnap
= NULL
;
6884 char *fromsnap
= NULL
;
6889 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
6892 flags
|= ZFS_DIFF_CLASSIFY
;
6895 flags
|= ZFS_DIFF_PARSEABLE
;
6898 flags
|= ZFS_DIFF_TIMESTAMP
;
6901 (void) fprintf(stderr
,
6902 gettext("invalid option '%c'\n"), optopt
);
6911 (void) fprintf(stderr
,
6912 gettext("must provide at least one snapshot name\n"));
6917 (void) fprintf(stderr
, gettext("too many arguments\n"));
6922 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
6925 if (*fromsnap
!= '@')
6926 copy
= strdup(fromsnap
);
6928 copy
= strdup(tosnap
);
6932 if ((atp
= strchr(copy
, '@')) != NULL
)
6935 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6941 * Ignore SIGPIPE so that the library can give us
6942 * information on any failure
6944 (void) sigignore(SIGPIPE
);
6946 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
6954 * zfs remap <filesystem | volume>
6956 * Remap the indirect blocks in the given fileystem or volume.
6959 zfs_do_remap(int argc
, char **argv
)
6966 while ((c
= getopt(argc
, argv
, "")) != -1) {
6969 (void) fprintf(stderr
,
6970 gettext("invalid option '%c'\n"), optopt
);
6976 (void) fprintf(stderr
, gettext("wrong number of arguments\n"));
6981 err
= zfs_remap_indirects(g_zfs
, fsname
);
6987 * zfs bookmark <fs@snap> <fs#bmark>
6989 * Creates a bookmark with the given name from the given snapshot.
6992 zfs_do_bookmark(int argc
, char **argv
)
6994 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
7001 while ((c
= getopt(argc
, argv
, "")) != -1) {
7004 (void) fprintf(stderr
,
7005 gettext("invalid option '%c'\n"), optopt
);
7013 /* check number of arguments */
7015 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
7019 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
7023 if (strchr(argv
[1], '#') == NULL
) {
7024 (void) fprintf(stderr
,
7025 gettext("invalid bookmark name '%s' -- "
7026 "must contain a '#'\n"), argv
[1]);
7030 if (argv
[0][0] == '@') {
7032 * Snapshot name begins with @.
7033 * Default to same fs as bookmark.
7035 (void) strncpy(snapname
, argv
[1], sizeof (snapname
));
7036 *strchr(snapname
, '#') = '\0';
7037 (void) strlcat(snapname
, argv
[0], sizeof (snapname
));
7039 (void) strncpy(snapname
, argv
[0], sizeof (snapname
));
7041 zhp
= zfs_open(g_zfs
, snapname
, ZFS_TYPE_SNAPSHOT
);
7047 nvl
= fnvlist_alloc();
7048 fnvlist_add_string(nvl
, argv
[1], snapname
);
7049 ret
= lzc_bookmark(nvl
, NULL
);
7053 const char *err_msg
;
7056 (void) snprintf(errbuf
, sizeof (errbuf
),
7057 dgettext(TEXT_DOMAIN
,
7058 "cannot create bookmark '%s'"), argv
[1]);
7062 err_msg
= "bookmark is in a different pool";
7065 err_msg
= "bookmark exists";
7068 err_msg
= "invalid argument";
7071 err_msg
= "bookmark feature not enabled";
7074 err_msg
= "out of space";
7077 err_msg
= "unknown error";
7080 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
7081 dgettext(TEXT_DOMAIN
, err_msg
));
7092 zfs_do_channel_program(int argc
, char **argv
)
7096 char *progbuf
, *filename
, *poolname
;
7097 size_t progsize
, progread
;
7099 uint64_t instrlimit
= ZCP_DEFAULT_INSTRLIMIT
;
7100 uint64_t memlimit
= ZCP_DEFAULT_MEMLIMIT
;
7101 boolean_t sync_flag
= B_TRUE
;
7102 zpool_handle_t
*zhp
;
7106 (c
= getopt(argc
, argv
, "nt:(instr-limit)m:(memory-limit)"))) {
7114 arg
= strtoull(optarg
, &endp
, 0);
7115 if (errno
!= 0 || *endp
!= '\0') {
7116 (void) fprintf(stderr
, gettext(
7118 "'%s': expected integer\n"), optarg
);
7123 if (arg
> ZCP_MAX_INSTRLIMIT
|| arg
== 0) {
7124 (void) fprintf(stderr
, gettext(
7125 "Invalid instruction limit: "
7132 ASSERT3U(c
, ==, 'm');
7133 if (arg
> ZCP_MAX_MEMLIMIT
|| arg
== 0) {
7134 (void) fprintf(stderr
, gettext(
7135 "Invalid memory limit: "
7145 sync_flag
= B_FALSE
;
7149 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7159 (void) fprintf(stderr
,
7160 gettext("invalid number of arguments\n"));
7166 if (strcmp(filename
, "-") == 0) {
7168 filename
= "standard input";
7169 } else if ((fd
= open(filename
, O_RDONLY
)) < 0) {
7170 (void) fprintf(stderr
, gettext("cannot open '%s': %s\n"),
7171 filename
, strerror(errno
));
7175 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
7176 (void) fprintf(stderr
, gettext("cannot open pool '%s'"),
7183 * Read in the channel program, expanding the program buffer as
7188 progbuf
= safe_malloc(progsize
);
7190 ret
= read(fd
, progbuf
+ progread
, progsize
- progread
);
7192 if (progread
== progsize
&& ret
> 0) {
7194 progbuf
= safe_realloc(progbuf
, progsize
);
7202 (void) fprintf(stderr
,
7203 gettext("cannot read '%s': %s\n"),
7204 filename
, strerror(errno
));
7207 progbuf
[progread
] = '\0';
7210 * Any remaining arguments are passed as arguments to the lua script as
7213 * "argv" -> [ "arg 1", ... "arg n" ],
7216 nvlist_t
*argnvl
= fnvlist_alloc();
7217 fnvlist_add_string_array(argnvl
, ZCP_ARG_CLIARGV
, argv
+ 2, argc
- 2);
7220 ret
= lzc_channel_program(poolname
, progbuf
,
7221 instrlimit
, memlimit
, argnvl
, &outnvl
);
7223 ret
= lzc_channel_program_nosync(poolname
, progbuf
,
7224 instrlimit
, memlimit
, argnvl
, &outnvl
);
7229 * On error, report the error message handed back by lua if one
7230 * exists. Otherwise, generate an appropriate error message,
7231 * falling back on strerror() for an unexpected return code.
7233 char *errstring
= NULL
;
7234 if (nvlist_exists(outnvl
, ZCP_RET_ERROR
)) {
7235 (void) nvlist_lookup_string(outnvl
,
7236 ZCP_RET_ERROR
, &errstring
);
7237 if (errstring
== NULL
)
7238 errstring
= strerror(ret
);
7243 "Invalid instruction or memory limit.";
7246 errstring
= "Return value too large.";
7249 errstring
= "Memory limit exhausted.";
7252 errstring
= "Timed out.";
7255 errstring
= "Permission denied. Channel "
7256 "programs must be run as root.";
7259 errstring
= strerror(ret
);
7262 (void) fprintf(stderr
,
7263 gettext("Channel program execution failed:\n%s\n"),
7266 (void) printf("Channel program fully executed ");
7267 if (nvlist_empty(outnvl
)) {
7268 (void) printf("with no return value.\n");
7270 (void) printf("with return value:\n");
7271 dump_nvlist(outnvl
, 4);
7276 fnvlist_free(outnvl
);
7277 fnvlist_free(argnvl
);
7286 main(int argc
, char **argv
)
7293 (void) setlocale(LC_ALL
, "");
7294 (void) textdomain(TEXT_DOMAIN
);
7298 if ((g_zfs
= libzfs_init()) == NULL
) {
7299 (void) fprintf(stderr
, gettext("internal error: failed to "
7300 "initialize ZFS library\n"));
7304 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
7306 libzfs_print_on_error(g_zfs
, B_TRUE
);
7308 if ((mnttab_file
= fopen(MNTTAB
, "r")) == NULL
) {
7309 (void) fprintf(stderr
, gettext("internal error: unable to "
7310 "open %s\n"), MNTTAB
);
7315 * This command also doubles as the /etc/fs mount and unmount program.
7316 * Determine if we should take this behavior based on argv[0].
7318 progname
= basename(argv
[0]);
7319 if (strcmp(progname
, "mount") == 0) {
7320 ret
= manual_mount(argc
, argv
);
7321 } else if (strcmp(progname
, "umount") == 0) {
7322 ret
= manual_unmount(argc
, argv
);
7325 * Make sure the user has specified some command.
7328 (void) fprintf(stderr
, gettext("missing command\n"));
7335 * The 'umount' command is an alias for 'unmount'
7337 if (strcmp(cmdname
, "umount") == 0)
7338 cmdname
= "unmount";
7341 * The 'recv' command is an alias for 'receive'
7343 if (strcmp(cmdname
, "recv") == 0)
7344 cmdname
= "receive";
7347 * The 'snap' command is an alias for 'snapshot'
7349 if (strcmp(cmdname
, "snap") == 0)
7350 cmdname
= "snapshot";
7355 if (strcmp(cmdname
, "-?") == 0)
7359 * Run the appropriate command.
7361 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
7362 if (find_command_idx(cmdname
, &i
) == 0) {
7363 current_command
= &command_table
[i
];
7364 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
7365 } else if (strchr(cmdname
, '=') != NULL
) {
7366 verify(find_command_idx("set", &i
) == 0);
7367 current_command
= &command_table
[i
];
7368 ret
= command_table
[i
].func(argc
, argv
);
7370 (void) fprintf(stderr
, gettext("unrecognized "
7371 "command '%s'\n"), cmdname
);
7374 libzfs_mnttab_cache(g_zfs
, B_FALSE
);
7377 (void) fclose(mnttab_file
);
7379 if (ret
== 0 && log_history
)
7380 (void) zpool_log_history(g_zfs
, history_str
);
7385 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7386 * for the purposes of running ::findleaks.
7388 if (getenv("ZFS_ABORT") != NULL
) {
7389 (void) printf("dumping core by request\n");