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, 2015 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
29 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
38 #include <libnvpair.h>
51 #include <sys/mkdev.h>
52 #include <sys/mntent.h>
53 #include <sys/mnttab.h>
54 #include <sys/mount.h>
56 #include <sys/fs/zfs.h>
57 #include <sys/types.h>
61 #include <libzfs_core.h>
63 #include <zfs_deleg.h>
67 #include <directory.h>
68 #endif /* HAVE_IDMAP */
72 #include "zfs_comutil.h"
73 #include "libzfs_impl.h"
75 libzfs_handle_t
*g_zfs
;
77 static FILE *mnttab_file
;
78 static char history_str
[HIS_MAX_RECORD_LEN
];
79 static boolean_t log_history
= B_TRUE
;
81 static int zfs_do_clone(int argc
, char **argv
);
82 static int zfs_do_create(int argc
, char **argv
);
83 static int zfs_do_destroy(int argc
, char **argv
);
84 static int zfs_do_get(int argc
, char **argv
);
85 static int zfs_do_inherit(int argc
, char **argv
);
86 static int zfs_do_list(int argc
, char **argv
);
87 static int zfs_do_mount(int argc
, char **argv
);
88 static int zfs_do_rename(int argc
, char **argv
);
89 static int zfs_do_rollback(int argc
, char **argv
);
90 static int zfs_do_set(int argc
, char **argv
);
91 static int zfs_do_upgrade(int argc
, char **argv
);
92 static int zfs_do_snapshot(int argc
, char **argv
);
93 static int zfs_do_unmount(int argc
, char **argv
);
94 static int zfs_do_share(int argc
, char **argv
);
95 static int zfs_do_unshare(int argc
, char **argv
);
96 static int zfs_do_send(int argc
, char **argv
);
97 static int zfs_do_receive(int argc
, char **argv
);
98 static int zfs_do_promote(int argc
, char **argv
);
99 static int zfs_do_userspace(int argc
, char **argv
);
100 static int zfs_do_allow(int argc
, char **argv
);
101 static int zfs_do_unallow(int argc
, char **argv
);
102 static int zfs_do_hold(int argc
, char **argv
);
103 static int zfs_do_holds(int argc
, char **argv
);
104 static int zfs_do_release(int argc
, char **argv
);
105 static int zfs_do_diff(int argc
, char **argv
);
106 static int zfs_do_bookmark(int argc
, char **argv
);
109 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
114 _umem_debug_init(void)
116 return ("default,verbose"); /* $UMEM_DEBUG setting */
120 _umem_logging_init(void)
122 return ("fail,contents"); /* $UMEM_LOGGING setting */
156 typedef struct zfs_command
{
158 int (*func
)(int argc
, char **argv
);
163 * Master command table. Each ZFS command has a name, associated function, and
164 * usage message. The usage messages need to be internationalized, so we have
165 * to have a function to return the usage message based on a command index.
167 * These commands are organized according to how they are displayed in the usage
168 * message. An empty command (one with a NULL name) indicates an empty line in
169 * the generic usage message.
171 static zfs_command_t command_table
[] = {
172 { "create", zfs_do_create
, HELP_CREATE
},
173 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
175 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
176 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
177 { "clone", zfs_do_clone
, HELP_CLONE
},
178 { "promote", zfs_do_promote
, HELP_PROMOTE
},
179 { "rename", zfs_do_rename
, HELP_RENAME
},
180 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
182 { "list", zfs_do_list
, HELP_LIST
},
184 { "set", zfs_do_set
, HELP_SET
},
185 { "get", zfs_do_get
, HELP_GET
},
186 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
187 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
188 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
189 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
191 { "mount", zfs_do_mount
, HELP_MOUNT
},
192 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
193 { "share", zfs_do_share
, HELP_SHARE
},
194 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
196 { "send", zfs_do_send
, HELP_SEND
},
197 { "receive", zfs_do_receive
, HELP_RECEIVE
},
199 { "allow", zfs_do_allow
, HELP_ALLOW
},
201 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
203 { "hold", zfs_do_hold
, HELP_HOLD
},
204 { "holds", zfs_do_holds
, HELP_HOLDS
},
205 { "release", zfs_do_release
, HELP_RELEASE
},
206 { "diff", zfs_do_diff
, HELP_DIFF
},
209 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
211 zfs_command_t
*current_command
;
214 get_usage(zfs_help_t idx
)
218 return (gettext("\tclone [-p] [-o property=value] ... "
219 "<snapshot> <filesystem|volume>\n"));
221 return (gettext("\tcreate [-p] [-o property=value] ... "
223 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
224 "-V <size> <volume>\n"));
226 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
227 "\tdestroy [-dnpRrv] "
228 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
229 "\tdestroy <filesystem|volume>#<bookmark>\n"));
231 return (gettext("\tget [-rHp] [-d max] "
232 "[-o \"all\" | field[,...]]\n"
233 "\t [-t type[,...]] [-s source[,...]]\n"
234 "\t <\"all\" | property[,...]> "
235 "[filesystem|volume|snapshot] ...\n"));
237 return (gettext("\tinherit [-rS] <property> "
238 "<filesystem|volume|snapshot> ...\n"));
240 return (gettext("\tupgrade [-v]\n"
241 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
243 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
244 "[-s property]...\n\t [-S property]... [-t type[,...]] "
245 "[filesystem|volume|snapshot] ...\n"));
247 return (gettext("\tmount\n"
248 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
250 return (gettext("\tpromote <clone-filesystem>\n"));
252 return (gettext("\treceive [-vnsFu] <filesystem|volume|"
254 "\treceive [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
256 "\treceive -A <filesystem|volume>\n"));
258 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
259 "<filesystem|volume|snapshot>\n"
260 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
261 "\trename -r <snapshot> <snapshot>\n"));
263 return (gettext("\trollback [-rRf] <snapshot>\n"));
265 return (gettext("\tsend [-DnPpRvLec] [-[iI] snapshot] "
267 "\tsend [-Le] [-i snapshot|bookmark] "
268 "<filesystem|volume|snapshot>\n"
269 "\tsend [-nvPe] -t <receive_resume_token>\n"));
271 return (gettext("\tset <property=value> ... "
272 "<filesystem|volume|snapshot> ...\n"));
274 return (gettext("\tshare <-a [nfs|smb] | filesystem>\n"));
276 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
277 "<filesystem|volume>@<snap> ...\n"));
279 return (gettext("\tunmount [-f] "
280 "<-a | filesystem|mountpoint>\n"));
282 return (gettext("\tunshare "
283 "<-a [nfs|smb] | filesystem|mountpoint>\n"));
285 return (gettext("\tallow <filesystem|volume>\n"
287 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
288 "\t <filesystem|volume>\n"
289 "\tallow [-ld] -e <perm|@setname>[,...] "
290 "<filesystem|volume>\n"
291 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
292 "\tallow -s @setname <perm|@setname>[,...] "
293 "<filesystem|volume>\n"));
295 return (gettext("\tunallow [-rldug] "
296 "<\"everyone\"|user|group>[,...]\n"
297 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
298 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
299 "<filesystem|volume>\n"
300 "\tunallow [-r] -c [<perm|@setname>[,...]] "
301 "<filesystem|volume>\n"
302 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
303 "<filesystem|volume>\n"));
305 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
307 "\t [-S field] ... [-t type[,...]] "
308 "<filesystem|snapshot>\n"));
309 case HELP_GROUPSPACE
:
310 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
312 "\t [-S field] ... [-t type[,...]] "
313 "<filesystem|snapshot>\n"));
315 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
317 return (gettext("\tholds [-r] <snapshot> ...\n"));
319 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
321 return (gettext("\tdiff [-FHt] <snapshot> "
322 "[snapshot|filesystem]\n"));
324 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
334 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
339 * Utility function to guarantee malloc() success.
343 safe_malloc(size_t size
)
347 if ((data
= calloc(1, size
)) == NULL
)
354 safe_strdup(char *str
)
356 char *dupstr
= strdup(str
);
365 * Callback routine that will print out information for each of
369 usage_prop_cb(int prop
, void *cb
)
373 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
375 if (zfs_prop_readonly(prop
))
376 (void) fprintf(fp
, " NO ");
378 (void) fprintf(fp
, "YES ");
380 if (zfs_prop_inheritable(prop
))
381 (void) fprintf(fp
, " YES ");
383 (void) fprintf(fp
, " NO ");
385 if (zfs_prop_values(prop
) == NULL
)
386 (void) fprintf(fp
, "-\n");
388 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
394 * Display usage message. If we're inside a command, display only the usage for
395 * that command. Otherwise, iterate over the entire command table and display
396 * a complete usage message.
399 usage(boolean_t requested
)
402 boolean_t show_properties
= B_FALSE
;
403 FILE *fp
= requested
? stdout
: stderr
;
405 if (current_command
== NULL
) {
407 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
409 gettext("where 'command' is one of the following:\n\n"));
411 for (i
= 0; i
< NCOMMAND
; i
++) {
412 if (command_table
[i
].name
== NULL
)
413 (void) fprintf(fp
, "\n");
415 (void) fprintf(fp
, "%s",
416 get_usage(command_table
[i
].usage
));
419 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
420 "pool/[dataset/]*dataset[@name]\n"));
422 (void) fprintf(fp
, gettext("usage:\n"));
423 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
426 if (current_command
!= NULL
&&
427 (strcmp(current_command
->name
, "set") == 0 ||
428 strcmp(current_command
->name
, "get") == 0 ||
429 strcmp(current_command
->name
, "inherit") == 0 ||
430 strcmp(current_command
->name
, "list") == 0))
431 show_properties
= B_TRUE
;
433 if (show_properties
) {
435 gettext("\nThe following properties are supported:\n"));
437 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
438 "PROPERTY", "EDIT", "INHERIT", "VALUES");
440 /* Iterate over all properties */
441 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
444 (void) fprintf(fp
, "\t%-15s ", "userused@...");
445 (void) fprintf(fp
, " NO NO <size>\n");
446 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
447 (void) fprintf(fp
, " NO NO <size>\n");
448 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
449 (void) fprintf(fp
, "YES NO <size> | none\n");
450 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
451 (void) fprintf(fp
, "YES NO <size> | none\n");
452 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
453 (void) fprintf(fp
, " NO NO <size>\n");
455 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
456 "with standard units such as K, M, G, etc.\n"));
457 (void) fprintf(fp
, gettext("\nUser-defined properties can "
458 "be specified by using a name containing a colon (:).\n"));
459 (void) fprintf(fp
, gettext("\nThe {user|group}{used|quota}@ "
460 "properties must be appended with\n"
461 "a user or group specifier of one of these forms:\n"
462 " POSIX name (eg: \"matt\")\n"
463 " POSIX id (eg: \"126829\")\n"
464 " SMB name@domain (eg: \"matt@sun\")\n"
465 " SMB SID (eg: \"S-1-234-567-89\")\n"));
468 gettext("\nFor the property list, run: %s\n"),
471 gettext("\nFor the delegated permission list, run: %s\n"),
472 "zfs allow|unallow");
476 * See comments at end of main().
478 if (getenv("ZFS_ABORT") != NULL
) {
479 (void) printf("dumping core by request\n");
483 exit(requested
? 0 : 2);
487 * Take a property=value argument string and add it to the given nvlist.
488 * Modifies the argument inplace.
491 parseprop(nvlist_t
*props
, char *propname
)
493 char *propval
, *strval
;
495 if ((propval
= strchr(propname
, '=')) == NULL
) {
496 (void) fprintf(stderr
, gettext("missing "
497 "'=' for property=value argument\n"));
502 if (nvlist_lookup_string(props
, propname
, &strval
) == 0) {
503 (void) fprintf(stderr
, gettext("property '%s' "
504 "specified multiple times\n"), propname
);
507 if (nvlist_add_string(props
, propname
, propval
) != 0)
513 parse_depth(char *opt
, int *flags
)
518 depth
= (int)strtol(opt
, &tmp
, 0);
520 (void) fprintf(stderr
,
521 gettext("%s is not an integer\n"), optarg
);
525 (void) fprintf(stderr
,
526 gettext("Depth can not be negative.\n"));
529 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
533 #define PROGRESS_DELAY 2 /* seconds */
535 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";
536 static time_t pt_begin
;
537 static char *pt_header
= NULL
;
538 static boolean_t pt_shown
;
541 start_progress_timer(void)
543 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
548 set_progress_header(char *header
)
550 assert(pt_header
== NULL
);
551 pt_header
= safe_strdup(header
);
553 (void) printf("%s: ", header
);
554 (void) fflush(stdout
);
559 update_progress(char *update
)
561 if (!pt_shown
&& time(NULL
) > pt_begin
) {
562 int len
= strlen(update
);
564 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
566 (void) fflush(stdout
);
568 } else if (pt_shown
) {
569 int len
= strlen(update
);
571 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
572 (void) fflush(stdout
);
577 finish_progress(char *done
)
580 (void) printf("%s\n", done
);
581 (void) fflush(stdout
);
588 zfs_mount_and_share(libzfs_handle_t
*hdl
, const char *dataset
, zfs_type_t type
)
590 zfs_handle_t
*zhp
= NULL
;
593 zhp
= zfs_open(hdl
, dataset
, type
);
598 * Volumes may neither be mounted or shared. Potentially in the
599 * future filesystems detected on these volumes could be mounted.
601 if (zfs_get_type(zhp
) == ZFS_TYPE_VOLUME
) {
607 * Mount and/or share the new filesystem as appropriate. We provide a
608 * verbose error message to let the user know that their filesystem was
609 * in fact created, even if we failed to mount or share it.
611 * If the user doesn't want the dataset automatically mounted, then
612 * skip the mount/share step
614 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
, B_FALSE
) &&
615 zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
) {
616 if (geteuid() != 0) {
617 (void) fprintf(stderr
, gettext("filesystem "
618 "successfully created, but it may only be "
619 "mounted by root\n"));
621 } else if (zfs_mount(zhp
, NULL
, 0) != 0) {
622 (void) fprintf(stderr
, gettext("filesystem "
623 "successfully created, but not mounted\n"));
625 } else if (zfs_share(zhp
) != 0) {
626 (void) fprintf(stderr
, gettext("filesystem "
627 "successfully created, but not shared\n"));
638 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
640 * Given an existing dataset, create a writable copy whose initial contents
641 * are the same as the source. The newly created dataset maintains a
642 * dependency on the original; the original cannot be destroyed so long as
645 * The '-p' flag creates all the non-existing ancestors of the target first.
648 zfs_do_clone(int argc
, char **argv
)
650 zfs_handle_t
*zhp
= NULL
;
651 boolean_t parents
= B_FALSE
;
656 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
660 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
663 if (parseprop(props
, optarg
) != 0) {
672 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
681 /* check number of arguments */
683 (void) fprintf(stderr
, gettext("missing source dataset "
688 (void) fprintf(stderr
, gettext("missing target dataset "
693 (void) fprintf(stderr
, gettext("too many arguments\n"));
697 /* open the source dataset */
698 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
) {
703 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
706 * Now create the ancestors of the target dataset. If the
707 * target already exists and '-p' option was used we should not
710 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
716 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
724 ret
= zfs_clone(zhp
, argv
[1], props
);
726 /* create the mountpoint if necessary */
729 (void) zpool_log_history(g_zfs
, history_str
);
730 log_history
= B_FALSE
;
733 ret
= zfs_mount_and_share(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
750 * zfs create [-p] [-o prop=value] ... fs
751 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
753 * Create a new dataset. This command can be used to create filesystems
754 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
755 * For volumes, the user must specify a size to be used.
757 * The '-s' flag applies only to volumes, and indicates that we should not try
758 * to set the reservation for this volume. By default we set a reservation
759 * equal to the size for any volume. For pools with SPA_VERSION >=
760 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
762 * The '-p' flag creates all the non-existing ancestors of the target first.
765 zfs_do_create(int argc
, char **argv
)
767 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
768 uint64_t volsize
= 0;
770 boolean_t noreserve
= B_FALSE
;
771 boolean_t bflag
= B_FALSE
;
772 boolean_t parents
= B_FALSE
;
777 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
781 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
784 type
= ZFS_TYPE_VOLUME
;
785 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
786 (void) fprintf(stderr
, gettext("bad volume "
787 "size '%s': %s\n"), optarg
,
788 libzfs_error_description(g_zfs
));
792 if (nvlist_add_uint64(props
,
793 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
802 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
803 (void) fprintf(stderr
, gettext("bad volume "
804 "block size '%s': %s\n"), optarg
,
805 libzfs_error_description(g_zfs
));
809 if (nvlist_add_uint64(props
,
810 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
815 if (parseprop(props
, optarg
) != 0)
822 (void) fprintf(stderr
, gettext("missing size "
826 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
832 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
833 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
834 "used when creating a volume\n"));
841 /* check number of arguments */
843 (void) fprintf(stderr
, gettext("missing %s argument\n"),
844 zfs_type_to_name(type
));
848 (void) fprintf(stderr
, gettext("too many arguments\n"));
852 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
853 zpool_handle_t
*zpool_handle
;
854 nvlist_t
*real_props
= NULL
;
855 uint64_t spa_version
;
857 zfs_prop_t resv_prop
;
861 if ((p
= strchr(argv
[0], '/')) != NULL
)
863 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
866 if (zpool_handle
== NULL
)
868 spa_version
= zpool_get_prop_int(zpool_handle
,
869 ZPOOL_PROP_VERSION
, NULL
);
870 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
871 resv_prop
= ZFS_PROP_REFRESERVATION
;
873 resv_prop
= ZFS_PROP_RESERVATION
;
875 (void) snprintf(msg
, sizeof (msg
),
876 gettext("cannot create '%s'"), argv
[0]);
877 if (props
&& (real_props
= zfs_valid_proplist(g_zfs
, type
,
878 props
, 0, NULL
, zpool_handle
, msg
)) == NULL
) {
879 zpool_close(zpool_handle
);
882 zpool_close(zpool_handle
);
884 volsize
= zvol_volsize_to_reservation(volsize
, real_props
);
885 nvlist_free(real_props
);
887 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
889 if (nvlist_add_uint64(props
,
890 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
897 if (parents
&& zfs_name_valid(argv
[0], type
)) {
899 * Now create the ancestors of target dataset. If the target
900 * already exists and '-p' option was used we should not
903 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
907 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
912 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
916 (void) zpool_log_history(g_zfs
, history_str
);
917 log_history
= B_FALSE
;
920 ret
= zfs_mount_and_share(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
931 * zfs destroy [-rRf] <fs, vol>
932 * zfs destroy [-rRd] <snap>
934 * -r Recursively destroy all children
935 * -R Recursively destroy all dependents, including clones
936 * -f Force unmounting of any dependents
937 * -d If we can't destroy now, mark for deferred destruction
939 * Destroys the given dataset. By default, it will unmount any filesystems,
940 * and refuse to destroy a dataset that has any dependents. A dependent can
941 * either be a child, or a clone of a child.
943 typedef struct destroy_cbdata
{
946 boolean_t cb_recurse
;
948 boolean_t cb_doclones
;
949 zfs_handle_t
*cb_target
;
950 boolean_t cb_defer_destroy
;
951 boolean_t cb_verbose
;
952 boolean_t cb_parsable
;
955 nvlist_t
*cb_batchedsnaps
;
957 /* first snap in contiguous run */
959 /* previous snap in contiguous run */
967 * Check for any dependents based on the '-r' or '-R' flags.
970 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
972 destroy_cbdata_t
*cbp
= data
;
973 const char *tname
= zfs_get_name(cbp
->cb_target
);
974 const char *name
= zfs_get_name(zhp
);
976 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
977 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
979 * This is a direct descendant, not a clone somewhere else in
986 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
987 "%s has children\n"),
988 zfs_get_name(cbp
->cb_target
),
989 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
990 (void) fprintf(stderr
, gettext("use '-r' to destroy "
991 "the following datasets:\n"));
992 cbp
->cb_first
= B_FALSE
;
993 cbp
->cb_error
= B_TRUE
;
996 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
999 * This is a clone. We only want to report this if the '-r'
1000 * wasn't specified, or the target is a snapshot.
1002 if (!cbp
->cb_recurse
&&
1003 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
1006 if (cbp
->cb_first
) {
1007 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1008 "%s has dependent clones\n"),
1009 zfs_get_name(cbp
->cb_target
),
1010 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1011 (void) fprintf(stderr
, gettext("use '-R' to destroy "
1012 "the following datasets:\n"));
1013 cbp
->cb_first
= B_FALSE
;
1014 cbp
->cb_error
= B_TRUE
;
1015 cbp
->cb_dryrun
= B_TRUE
;
1018 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1027 destroy_callback(zfs_handle_t
*zhp
, void *data
)
1029 destroy_cbdata_t
*cb
= data
;
1030 const char *name
= zfs_get_name(zhp
);
1032 if (cb
->cb_verbose
) {
1033 if (cb
->cb_parsable
) {
1034 (void) printf("destroy\t%s\n", name
);
1035 } else if (cb
->cb_dryrun
) {
1036 (void) printf(gettext("would destroy %s\n"),
1039 (void) printf(gettext("will destroy %s\n"),
1045 * Ignore pools (which we've already flagged as an error before getting
1048 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1049 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1053 if (cb
->cb_dryrun
) {
1059 * We batch up all contiguous snapshots (even of different
1060 * filesystems) and destroy them with one ioctl. We can't
1061 * simply do all snap deletions and then all fs deletions,
1062 * because we must delete a clone before its origin.
1064 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1065 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1067 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1068 cb
->cb_batchedsnaps
, B_FALSE
);
1069 fnvlist_free(cb
->cb_batchedsnaps
);
1070 cb
->cb_batchedsnaps
= fnvlist_alloc();
1073 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1074 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1085 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1087 destroy_cbdata_t
*cb
= arg
;
1088 const char *name
= zfs_get_name(zhp
);
1091 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1092 if (cb
->cb_firstsnap
== NULL
)
1093 cb
->cb_firstsnap
= strdup(name
);
1094 if (cb
->cb_prevsnap
!= NULL
)
1095 free(cb
->cb_prevsnap
);
1096 /* this snap continues the current range */
1097 cb
->cb_prevsnap
= strdup(name
);
1098 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1100 if (cb
->cb_verbose
) {
1101 if (cb
->cb_parsable
) {
1102 (void) printf("destroy\t%s\n", name
);
1103 } else if (cb
->cb_dryrun
) {
1104 (void) printf(gettext("would destroy %s\n"),
1107 (void) printf(gettext("will destroy %s\n"),
1111 } else if (cb
->cb_firstsnap
!= NULL
) {
1112 /* end of this range */
1114 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1115 cb
->cb_prevsnap
, &used
);
1116 cb
->cb_snapused
+= used
;
1117 free(cb
->cb_firstsnap
);
1118 cb
->cb_firstsnap
= NULL
;
1119 free(cb
->cb_prevsnap
);
1120 cb
->cb_prevsnap
= NULL
;
1127 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1130 assert(cb
->cb_firstsnap
== NULL
);
1131 assert(cb
->cb_prevsnap
== NULL
);
1132 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1133 if (cb
->cb_firstsnap
!= NULL
) {
1136 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1137 cb
->cb_prevsnap
, &used
);
1139 cb
->cb_snapused
+= used
;
1140 free(cb
->cb_firstsnap
);
1141 cb
->cb_firstsnap
= NULL
;
1142 free(cb
->cb_prevsnap
);
1143 cb
->cb_prevsnap
= NULL
;
1149 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1151 destroy_cbdata_t
*cb
= arg
;
1154 /* Check for clones. */
1155 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1156 cb
->cb_target
= zhp
;
1157 cb
->cb_first
= B_TRUE
;
1158 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1159 destroy_check_dependent
, cb
);
1163 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1171 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1173 destroy_cbdata_t
*cb
= arg
;
1176 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1182 if (cb
->cb_verbose
) {
1183 err
= destroy_print_snapshots(zhp
, cb
);
1189 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1197 destroy_clones(destroy_cbdata_t
*cb
)
1200 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1202 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1203 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1206 boolean_t defer
= cb
->cb_defer_destroy
;
1210 * We can't defer destroy non-snapshots, so set it to
1211 * false while destroying the clones.
1213 cb
->cb_defer_destroy
= B_FALSE
;
1214 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1215 destroy_callback
, cb
);
1216 cb
->cb_defer_destroy
= defer
;
1226 zfs_do_destroy(int argc
, char **argv
)
1228 destroy_cbdata_t cb
= { 0 };
1232 zfs_handle_t
*zhp
= NULL
;
1234 zfs_type_t type
= ZFS_TYPE_DATASET
;
1237 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1240 cb
.cb_verbose
= B_TRUE
;
1243 cb
.cb_verbose
= B_TRUE
;
1244 cb
.cb_parsable
= B_TRUE
;
1247 cb
.cb_dryrun
= B_TRUE
;
1250 cb
.cb_defer_destroy
= B_TRUE
;
1251 type
= ZFS_TYPE_SNAPSHOT
;
1254 cb
.cb_force
= B_TRUE
;
1257 cb
.cb_recurse
= B_TRUE
;
1260 cb
.cb_recurse
= B_TRUE
;
1261 cb
.cb_doclones
= B_TRUE
;
1265 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1274 /* check number of arguments */
1276 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1280 (void) fprintf(stderr
, gettext("too many arguments\n"));
1284 at
= strchr(argv
[0], '@');
1285 pound
= strchr(argv
[0], '#');
1288 /* Build the list of snaps to destroy in cb_nvl. */
1289 cb
.cb_nvl
= fnvlist_alloc();
1292 zhp
= zfs_open(g_zfs
, argv
[0],
1293 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1295 nvlist_free(cb
.cb_nvl
);
1299 cb
.cb_snapspec
= at
+ 1;
1300 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1306 if (nvlist_empty(cb
.cb_nvl
)) {
1307 (void) fprintf(stderr
, gettext("could not find any "
1308 "snapshots to destroy; check snapshot names.\n"));
1313 if (cb
.cb_verbose
) {
1315 zfs_nicenum(cb
.cb_snapused
, buf
, sizeof (buf
));
1316 if (cb
.cb_parsable
) {
1317 (void) printf("reclaim\t%llu\n",
1318 (u_longlong_t
)cb
.cb_snapused
);
1319 } else if (cb
.cb_dryrun
) {
1320 (void) printf(gettext("would reclaim %s\n"),
1323 (void) printf(gettext("will reclaim %s\n"),
1328 if (!cb
.cb_dryrun
) {
1329 if (cb
.cb_doclones
) {
1330 cb
.cb_batchedsnaps
= fnvlist_alloc();
1331 err
= destroy_clones(&cb
);
1333 err
= zfs_destroy_snaps_nvl(g_zfs
,
1334 cb
.cb_batchedsnaps
, B_FALSE
);
1342 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1343 cb
.cb_defer_destroy
);
1349 } else if (pound
!= NULL
) {
1354 (void) fprintf(stderr
,
1355 "dryrun is not supported with bookmark\n");
1359 if (cb
.cb_defer_destroy
) {
1360 (void) fprintf(stderr
,
1361 "defer destroy is not supported with bookmark\n");
1365 if (cb
.cb_recurse
) {
1366 (void) fprintf(stderr
,
1367 "recursive is not supported with bookmark\n");
1371 if (!zfs_bookmark_exists(argv
[0])) {
1372 (void) fprintf(stderr
, gettext("bookmark '%s' "
1373 "does not exist.\n"), argv
[0]);
1377 nvl
= fnvlist_alloc();
1378 fnvlist_add_boolean(nvl
, argv
[0]);
1380 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1382 (void) zfs_standard_error(g_zfs
, err
,
1383 "cannot destroy bookmark");
1390 /* Open the given dataset */
1391 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1397 * Perform an explicit check for pools before going any further.
1399 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1400 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1401 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1402 "operation does not apply to pools\n"),
1404 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1405 "%s' to destroy all datasets in the pool\n"),
1407 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1408 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1414 * Check for any dependents and/or clones.
1416 cb
.cb_first
= B_TRUE
;
1417 if (!cb
.cb_doclones
&&
1418 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1429 cb
.cb_batchedsnaps
= fnvlist_alloc();
1430 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1437 * Do the real thing. The callback will close the
1438 * handle regardless of whether it succeeds or not.
1440 err
= destroy_callback(zhp
, &cb
);
1443 err
= zfs_destroy_snaps_nvl(g_zfs
,
1444 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1451 fnvlist_free(cb
.cb_batchedsnaps
);
1452 fnvlist_free(cb
.cb_nvl
);
1459 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1462 zfs_get_column_t col
;
1464 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1465 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1466 if (col
== GET_COL_RECVD
)
1472 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1473 * < all | property[,property]... > < fs | snap | vol > ...
1475 * -r recurse over any child datasets
1476 * -H scripted mode. Headers are stripped, and fields are separated
1477 * by tabs instead of spaces.
1478 * -o Set of fields to display. One of "name,property,value,
1479 * received,source". Default is "name,property,value,source".
1480 * "all" is an alias for all five.
1481 * -s Set of sources to allow. One of
1482 * "local,default,inherited,received,temporary,none". Default is
1484 * -p Display values in parsable (literal) format.
1486 * Prints properties for the given datasets. The user can control which
1487 * columns to display as well as which property types to allow.
1491 * Invoked to display the properties for a single dataset.
1494 get_callback(zfs_handle_t
*zhp
, void *data
)
1496 char buf
[ZFS_MAXPROPLEN
];
1497 char rbuf
[ZFS_MAXPROPLEN
];
1498 zprop_source_t sourcetype
;
1499 char source
[ZFS_MAX_DATASET_NAME_LEN
];
1500 zprop_get_cbdata_t
*cbp
= data
;
1501 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1502 zprop_list_t
*pl
= cbp
->cb_proplist
;
1506 boolean_t received
= is_recvd_column(cbp
);
1508 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1509 char *recvdval
= NULL
;
1511 * Skip the special fake placeholder. This will also skip over
1512 * the name property when 'all' is specified.
1514 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1515 pl
== cbp
->cb_proplist
)
1518 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1519 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1520 sizeof (buf
), &sourcetype
, source
,
1522 cbp
->cb_literal
) != 0) {
1525 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1526 ZFS_TYPE_DATASET
, B_FALSE
)) {
1527 (void) fprintf(stderr
,
1528 gettext("No such property '%s'\n"),
1529 zfs_prop_to_name(pl
->pl_prop
));
1532 sourcetype
= ZPROP_SRC_NONE
;
1533 (void) strlcpy(buf
, "-", sizeof (buf
));
1536 if (received
&& (zfs_prop_get_recvd(zhp
,
1537 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1538 cbp
->cb_literal
) == 0))
1541 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1542 zfs_prop_to_name(pl
->pl_prop
),
1543 buf
, sourcetype
, source
, recvdval
);
1544 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1545 sourcetype
= ZPROP_SRC_LOCAL
;
1547 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1548 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1549 sourcetype
= ZPROP_SRC_NONE
;
1550 (void) strlcpy(buf
, "-", sizeof (buf
));
1553 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1554 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1555 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1556 sourcetype
= ZPROP_SRC_LOCAL
;
1558 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1559 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1560 sourcetype
= ZPROP_SRC_NONE
;
1561 (void) strlcpy(buf
, "-", sizeof (buf
));
1564 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1565 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1567 if (nvlist_lookup_nvlist(user_props
,
1568 pl
->pl_user_prop
, &propval
) != 0) {
1571 sourcetype
= ZPROP_SRC_NONE
;
1574 verify(nvlist_lookup_string(propval
,
1575 ZPROP_VALUE
, &strval
) == 0);
1576 verify(nvlist_lookup_string(propval
,
1577 ZPROP_SOURCE
, &sourceval
) == 0);
1579 if (strcmp(sourceval
,
1580 zfs_get_name(zhp
)) == 0) {
1581 sourcetype
= ZPROP_SRC_LOCAL
;
1582 } else if (strcmp(sourceval
,
1583 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1584 sourcetype
= ZPROP_SRC_RECEIVED
;
1586 sourcetype
= ZPROP_SRC_INHERITED
;
1587 (void) strlcpy(source
,
1588 sourceval
, sizeof (source
));
1592 if (received
&& (zfs_prop_get_recvd(zhp
,
1593 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1594 cbp
->cb_literal
) == 0))
1597 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1598 pl
->pl_user_prop
, strval
, sourcetype
,
1607 zfs_do_get(int argc
, char **argv
)
1609 zprop_get_cbdata_t cb
= { 0 };
1610 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1611 int types
= ZFS_TYPE_DATASET
;
1612 char *value
, *fields
;
1615 zprop_list_t fake_name
= { 0 };
1618 * Set up default columns and sources.
1620 cb
.cb_sources
= ZPROP_SRC_ALL
;
1621 cb
.cb_columns
[0] = GET_COL_NAME
;
1622 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1623 cb
.cb_columns
[2] = GET_COL_VALUE
;
1624 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1625 cb
.cb_type
= ZFS_TYPE_DATASET
;
1628 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1631 cb
.cb_literal
= B_TRUE
;
1634 limit
= parse_depth(optarg
, &flags
);
1637 flags
|= ZFS_ITER_RECURSE
;
1640 cb
.cb_scripted
= B_TRUE
;
1643 (void) fprintf(stderr
, gettext("missing argument for "
1644 "'%c' option\n"), optopt
);
1649 * Process the set of columns to display. We zero out
1650 * the structure to give us a blank slate.
1652 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1654 while (*optarg
!= '\0') {
1655 static char *col_subopts
[] =
1656 { "name", "property", "value", "received",
1657 "source", "all", NULL
};
1659 if (i
== ZFS_GET_NCOLS
) {
1660 (void) fprintf(stderr
, gettext("too "
1661 "many fields given to -o "
1666 switch (getsubopt(&optarg
, col_subopts
,
1669 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1672 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1675 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1678 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1679 flags
|= ZFS_ITER_RECVD_PROPS
;
1682 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1686 (void) fprintf(stderr
,
1687 gettext("\"all\" conflicts "
1688 "with specific fields "
1689 "given to -o option\n"));
1692 cb
.cb_columns
[0] = GET_COL_NAME
;
1693 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1694 cb
.cb_columns
[2] = GET_COL_VALUE
;
1695 cb
.cb_columns
[3] = GET_COL_RECVD
;
1696 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1697 flags
|= ZFS_ITER_RECVD_PROPS
;
1701 (void) fprintf(stderr
,
1702 gettext("invalid column name "
1711 while (*optarg
!= '\0') {
1712 static char *source_subopts
[] = {
1713 "local", "default", "inherited",
1714 "received", "temporary", "none",
1717 switch (getsubopt(&optarg
, source_subopts
,
1720 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1723 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1726 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1729 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1732 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1735 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1738 (void) fprintf(stderr
,
1739 gettext("invalid source "
1748 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1749 while (*optarg
!= '\0') {
1750 static char *type_subopts
[] = { "filesystem",
1751 "volume", "snapshot", "bookmark",
1754 switch (getsubopt(&optarg
, type_subopts
,
1757 types
|= ZFS_TYPE_FILESYSTEM
;
1760 types
|= ZFS_TYPE_VOLUME
;
1763 types
|= ZFS_TYPE_SNAPSHOT
;
1766 types
|= ZFS_TYPE_BOOKMARK
;
1769 types
= ZFS_TYPE_DATASET
|
1774 (void) fprintf(stderr
,
1775 gettext("invalid type '%s'\n"),
1783 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1793 (void) fprintf(stderr
, gettext("missing property "
1800 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1808 * As part of zfs_expand_proplist(), we keep track of the maximum column
1809 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1810 * need to know the maximum name length. However, the user likely did
1811 * not specify 'name' as one of the properties to fetch, so we need to
1812 * make sure we always include at least this property for
1813 * print_get_headers() to work properly.
1815 if (cb
.cb_proplist
!= NULL
) {
1816 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1817 fake_name
.pl_width
= strlen(gettext("NAME"));
1818 fake_name
.pl_next
= cb
.cb_proplist
;
1819 cb
.cb_proplist
= &fake_name
;
1822 cb
.cb_first
= B_TRUE
;
1824 /* run for each object */
1825 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1826 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1828 if (cb
.cb_proplist
== &fake_name
)
1829 zprop_free_list(fake_name
.pl_next
);
1831 zprop_free_list(cb
.cb_proplist
);
1837 * inherit [-rS] <property> <fs|vol> ...
1839 * -r Recurse over all children
1840 * -S Revert to received value, if any
1842 * For each dataset specified on the command line, inherit the given property
1843 * from its parent. Inheriting a property at the pool level will cause it to
1844 * use the default value. The '-r' flag will recurse over all children, and is
1845 * useful for setting a property on a hierarchy-wide basis, regardless of any
1846 * local modifications for each dataset.
1849 typedef struct inherit_cbdata
{
1850 const char *cb_propname
;
1851 boolean_t cb_received
;
1855 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1857 inherit_cbdata_t
*cb
= data
;
1858 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1861 * If we're doing it recursively, then ignore properties that
1862 * are not valid for this type of dataset.
1864 if (prop
!= ZPROP_INVAL
&&
1865 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
), B_FALSE
))
1868 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1872 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1874 inherit_cbdata_t
*cb
= data
;
1876 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1880 zfs_do_inherit(int argc
, char **argv
)
1884 inherit_cbdata_t cb
= { 0 };
1888 boolean_t received
= B_FALSE
;
1891 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1894 flags
|= ZFS_ITER_RECURSE
;
1901 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1910 /* check number of arguments */
1912 (void) fprintf(stderr
, gettext("missing property argument\n"));
1916 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1924 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1925 if (zfs_prop_readonly(prop
)) {
1926 (void) fprintf(stderr
, gettext(
1927 "%s property is read-only\n"),
1931 if (!zfs_prop_inheritable(prop
) && !received
) {
1932 (void) fprintf(stderr
, gettext("'%s' property cannot "
1933 "be inherited\n"), propname
);
1934 if (prop
== ZFS_PROP_QUOTA
||
1935 prop
== ZFS_PROP_RESERVATION
||
1936 prop
== ZFS_PROP_REFQUOTA
||
1937 prop
== ZFS_PROP_REFRESERVATION
) {
1938 (void) fprintf(stderr
, gettext("use 'zfs set "
1939 "%s=none' to clear\n"), propname
);
1940 (void) fprintf(stderr
, gettext("use 'zfs "
1941 "inherit -S %s' to revert to received "
1942 "value\n"), propname
);
1946 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1947 prop
== ZFS_PROP_VERSION
)) {
1948 (void) fprintf(stderr
, gettext("'%s' property cannot "
1949 "be reverted to a received value\n"), propname
);
1952 } else if (!zfs_prop_user(propname
)) {
1953 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1958 cb
.cb_propname
= propname
;
1959 cb
.cb_received
= received
;
1961 if (flags
& ZFS_ITER_RECURSE
) {
1962 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1963 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1965 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1966 NULL
, NULL
, 0, inherit_cb
, &cb
);
1972 typedef struct upgrade_cbdata
{
1973 uint64_t cb_numupgraded
;
1974 uint64_t cb_numsamegraded
;
1975 uint64_t cb_numfailed
;
1976 uint64_t cb_version
;
1978 boolean_t cb_foundone
;
1979 char cb_lastfs
[ZFS_MAX_DATASET_NAME_LEN
];
1983 same_pool(zfs_handle_t
*zhp
, const char *name
)
1985 int len1
= strcspn(name
, "/@");
1986 const char *zhname
= zfs_get_name(zhp
);
1987 int len2
= strcspn(zhname
, "/@");
1991 return (strncmp(name
, zhname
, len1
) == 0);
1995 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
1997 upgrade_cbdata_t
*cb
= data
;
1998 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2000 /* list if it's old/new */
2001 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
2002 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
2005 str
= gettext("The following filesystems are "
2006 "formatted using a newer software version and\n"
2007 "cannot be accessed on the current system.\n\n");
2009 str
= gettext("The following filesystems are "
2010 "out of date, and can be upgraded. After being\n"
2011 "upgraded, these filesystems (and any 'zfs send' "
2012 "streams generated from\n"
2013 "subsequent snapshots) will no longer be "
2014 "accessible by older software versions.\n\n");
2017 if (!cb
->cb_foundone
) {
2019 (void) printf(gettext("VER FILESYSTEM\n"));
2020 (void) printf(gettext("--- ------------\n"));
2021 cb
->cb_foundone
= B_TRUE
;
2024 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
2031 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2033 upgrade_cbdata_t
*cb
= data
;
2034 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2035 int needed_spa_version
;
2038 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2041 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2043 if (needed_spa_version
< 0)
2046 if (spa_version
< needed_spa_version
) {
2048 (void) printf(gettext("%s: can not be "
2049 "upgraded; the pool version needs to first "
2050 "be upgraded\nto version %d\n\n"),
2051 zfs_get_name(zhp
), needed_spa_version
);
2057 if (version
< cb
->cb_version
) {
2059 (void) snprintf(verstr
, sizeof (verstr
),
2060 "%llu", (u_longlong_t
)cb
->cb_version
);
2061 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2063 * If they did "zfs upgrade -a", then we could
2064 * be doing ioctls to different pools. We need
2065 * to log this history once to each pool, and bypass
2066 * the normal history logging that happens in main().
2068 (void) zpool_log_history(g_zfs
, history_str
);
2069 log_history
= B_FALSE
;
2071 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2072 cb
->cb_numupgraded
++;
2075 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2076 } else if (version
> cb
->cb_version
) {
2077 /* can't downgrade */
2078 (void) printf(gettext("%s: can not be downgraded; "
2079 "it is already at version %u\n"),
2080 zfs_get_name(zhp
), version
);
2083 cb
->cb_numsamegraded
++;
2091 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2094 zfs_do_upgrade(int argc
, char **argv
)
2096 boolean_t all
= B_FALSE
;
2097 boolean_t showversions
= B_FALSE
;
2099 upgrade_cbdata_t cb
= { 0 };
2101 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2104 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2107 flags
|= ZFS_ITER_RECURSE
;
2110 showversions
= B_TRUE
;
2113 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2114 optarg
, &cb
.cb_version
) != 0) {
2115 (void) fprintf(stderr
,
2116 gettext("invalid version %s\n"), optarg
);
2125 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2134 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2136 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2137 cb
.cb_version
|| argc
))
2139 if ((all
|| argc
) && (showversions
))
2145 /* Show info on available versions. */
2146 (void) printf(gettext("The following filesystem versions are "
2148 (void) printf(gettext("VER DESCRIPTION\n"));
2149 (void) printf("--- -----------------------------------------"
2150 "---------------\n");
2151 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2152 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2153 (void) printf(gettext(" 3 Case insensitive and filesystem "
2154 "user identifier (FUID)\n"));
2155 (void) printf(gettext(" 4 userquota, groupquota "
2157 (void) printf(gettext(" 5 System attributes\n"));
2158 (void) printf(gettext("\nFor more information on a particular "
2159 "version, including supported releases,\n"));
2160 (void) printf("see the ZFS Administration Guide.\n\n");
2162 } else if (argc
|| all
) {
2163 /* Upgrade filesystems */
2164 if (cb
.cb_version
== 0)
2165 cb
.cb_version
= ZPL_VERSION
;
2166 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2167 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2168 (void) printf(gettext("%llu filesystems upgraded\n"),
2169 (u_longlong_t
)cb
.cb_numupgraded
);
2170 if (cb
.cb_numsamegraded
) {
2171 (void) printf(gettext("%llu filesystems already at "
2173 (u_longlong_t
)cb
.cb_numsamegraded
);
2175 if (cb
.cb_numfailed
!= 0)
2178 /* List old-version filesystems */
2180 (void) printf(gettext("This system is currently running "
2181 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2183 flags
|= ZFS_ITER_RECURSE
;
2184 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2185 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2187 found
= cb
.cb_foundone
;
2188 cb
.cb_foundone
= B_FALSE
;
2189 cb
.cb_newer
= B_TRUE
;
2191 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2192 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2194 if (!cb
.cb_foundone
&& !found
) {
2195 (void) printf(gettext("All filesystems are "
2196 "formatted with the current version.\n"));
2204 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2205 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2206 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2207 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2209 * -H Scripted mode; elide headers and separate columns by tabs.
2210 * -i Translate SID to POSIX ID.
2211 * -n Print numeric ID instead of user/group name.
2212 * -o Control which fields to display.
2213 * -p Use exact (parsable) numeric output.
2214 * -s Specify sort columns, descending order.
2215 * -S Specify sort columns, ascending order.
2216 * -t Control which object types to display.
2218 * Displays space consumed by, and quotas on, each user in the specified
2219 * filesystem or snapshot.
2222 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2223 enum us_field_types
{
2231 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA",
2232 "OBJUSED", "OBJQUOTA" };
2233 static char *us_field_names
[] = { "type", "name", "used", "quota",
2234 "objused", "objquota" };
2235 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2237 #define USTYPE_PSX_GRP (1 << 0)
2238 #define USTYPE_PSX_USR (1 << 1)
2239 #define USTYPE_SMB_GRP (1 << 2)
2240 #define USTYPE_SMB_USR (1 << 3)
2241 #define USTYPE_ALL \
2242 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2244 static int us_type_bits
[] = {
2251 static char *us_type_names
[] = { "posixgroup", "posixuser", "smbgroup",
2254 typedef struct us_node
{
2256 uu_avl_node_t usn_avlnode
;
2257 uu_list_node_t usn_listnode
;
2260 typedef struct us_cbdata
{
2262 uu_avl_pool_t
*cb_avl_pool
;
2264 boolean_t cb_numname
;
2265 boolean_t cb_nicenum
;
2266 boolean_t cb_sid2posix
;
2267 zfs_userquota_prop_t cb_prop
;
2268 zfs_sort_column_t
*cb_sortcol
;
2269 size_t cb_width
[USFIELD_LAST
];
2272 static boolean_t us_populated
= B_FALSE
;
2275 zfs_sort_column_t
*si_sortcol
;
2276 boolean_t si_numname
;
2280 us_field_index(char *field
)
2284 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2285 if (strcmp(field
, us_field_names
[i
]) == 0)
2293 us_compare(const void *larg
, const void *rarg
, void *unused
)
2295 const us_node_t
*l
= larg
;
2296 const us_node_t
*r
= rarg
;
2297 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2298 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2299 boolean_t numname
= si
->si_numname
;
2300 nvlist_t
*lnvl
= l
->usn_nvl
;
2301 nvlist_t
*rnvl
= r
->usn_nvl
;
2305 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2312 zfs_prop_t prop
= sortcol
->sc_prop
;
2313 const char *propname
= NULL
;
2314 boolean_t reverse
= sortcol
->sc_reverse
;
2319 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2320 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2322 rc
= (rv32
< lv32
) ? 1 : -1;
2328 (void) nvlist_lookup_uint64(lnvl
, propname
,
2330 (void) nvlist_lookup_uint64(rnvl
, propname
,
2333 rc
= (rv64
< lv64
) ? 1 : -1;
2335 if ((nvlist_lookup_string(lnvl
, propname
,
2336 &lvstr
) == ENOENT
) ||
2337 (nvlist_lookup_string(rnvl
, propname
,
2338 &rvstr
) == ENOENT
)) {
2341 rc
= strcmp(lvstr
, rvstr
);
2345 case ZFS_PROP_QUOTA
:
2348 if (prop
== ZFS_PROP_USED
)
2352 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2353 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2355 rc
= (rv64
< lv64
) ? 1 : -1;
2364 return (reverse
? 1 : -1);
2366 return (reverse
? -1 : 1);
2371 * If entries still seem to be the same, check if they are of the same
2372 * type (smbentity is added only if we are doing SID to POSIX ID
2373 * translation where we can have duplicate type/name combinations).
2375 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2376 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2378 return (lvb
< rvb
? -1 : 1);
2384 zfs_prop_is_user(unsigned p
)
2386 return (p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
||
2387 p
== ZFS_PROP_USEROBJUSED
|| p
== ZFS_PROP_USEROBJQUOTA
);
2391 zfs_prop_is_group(unsigned p
)
2393 return (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
||
2394 p
== ZFS_PROP_GROUPOBJUSED
|| p
== ZFS_PROP_GROUPOBJQUOTA
);
2397 static inline const char *
2398 us_type2str(unsigned field_type
)
2400 switch (field_type
) {
2401 case USTYPE_PSX_USR
:
2402 return ("POSIX User");
2403 case USTYPE_PSX_GRP
:
2404 return ("POSIX Group");
2405 case USTYPE_SMB_USR
:
2406 return ("SMB User");
2407 case USTYPE_SMB_GRP
:
2408 return ("SMB Group");
2410 return ("Undefined");
2415 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2417 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2418 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2423 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2424 uu_avl_t
*avl
= cb
->cb_avl
;
2428 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2430 const char *typestr
;
2434 int typeidx
, nameidx
, sizeidx
;
2435 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2436 boolean_t smbentity
= B_FALSE
;
2438 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2440 node
= safe_malloc(sizeof (us_node_t
));
2441 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2442 node
->usn_nvl
= props
;
2444 if (domain
!= NULL
&& domain
[0] != '\0') {
2447 char sid
[MAXNAMELEN
+ 32];
2451 directory_error_t e
;
2455 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2457 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2458 type
= USTYPE_SMB_GRP
;
2459 err
= sid_to_id(sid
, B_FALSE
, &id
);
2461 type
= USTYPE_SMB_USR
;
2462 err
= sid_to_id(sid
, B_TRUE
, &id
);
2467 if (!cb
->cb_sid2posix
) {
2468 e
= directory_name_from_sid(NULL
, sid
, &name
,
2471 directory_error_free(e
);
2481 #endif /* HAVE_IDMAP */
2484 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2486 if (zfs_prop_is_group(prop
)) {
2487 type
= USTYPE_PSX_GRP
;
2488 if (!cb
->cb_numname
) {
2491 if ((g
= getgrgid(rid
)) != NULL
)
2495 type
= USTYPE_PSX_USR
;
2496 if (!cb
->cb_numname
) {
2499 if ((p
= getpwuid(rid
)) != NULL
)
2506 * Make sure that the type/name combination is unique when doing
2507 * SID to POSIX ID translation (hence changing the type from SMB to
2510 if (cb
->cb_sid2posix
&&
2511 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2514 /* Calculate/update width of TYPE field */
2515 typestr
= us_type2str(type
);
2516 typelen
= strlen(gettext(typestr
));
2517 typeidx
= us_field_index("type");
2518 if (typelen
> cb
->cb_width
[typeidx
])
2519 cb
->cb_width
[typeidx
] = typelen
;
2520 if (nvlist_add_uint32(props
, "type", type
) != 0)
2523 /* Calculate/update width of NAME field */
2524 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2525 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2527 namelen
= snprintf(NULL
, 0, "%u", rid
);
2529 if (nvlist_add_string(props
, "name", name
) != 0)
2531 namelen
= strlen(name
);
2533 nameidx
= us_field_index("name");
2534 if (nameidx
>= 0 && namelen
> cb
->cb_width
[nameidx
])
2535 cb
->cb_width
[nameidx
] = namelen
;
2538 * Check if this type/name combination is in the list and update it;
2539 * otherwise add new node to the list.
2541 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2542 uu_avl_insert(avl
, node
, idx
);
2547 props
= node
->usn_nvl
;
2550 /* Calculate/update width of USED/QUOTA fields */
2552 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2554 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu",
2555 (u_longlong_t
)space
);
2556 sizelen
= strlen(sizebuf
);
2557 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
) {
2559 if (!nvlist_exists(props
, "quota"))
2560 (void) nvlist_add_uint64(props
, "quota", 0);
2561 } else if (prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2563 if (!nvlist_exists(props
, "used"))
2564 (void) nvlist_add_uint64(props
, "used", 0);
2565 } else if (prop
== ZFS_PROP_USEROBJUSED
||
2566 prop
== ZFS_PROP_GROUPOBJUSED
) {
2567 propname
= "objused";
2568 if (!nvlist_exists(props
, "objquota"))
2569 (void) nvlist_add_uint64(props
, "objquota", 0);
2570 } else if (prop
== ZFS_PROP_USEROBJQUOTA
||
2571 prop
== ZFS_PROP_GROUPOBJQUOTA
) {
2572 propname
= "objquota";
2573 if (!nvlist_exists(props
, "objused"))
2574 (void) nvlist_add_uint64(props
, "objused", 0);
2578 sizeidx
= us_field_index(propname
);
2579 if (sizeidx
>= 0 && sizelen
> cb
->cb_width
[sizeidx
])
2580 cb
->cb_width
[sizeidx
] = sizelen
;
2582 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2589 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2590 size_t *width
, us_node_t
*node
)
2592 nvlist_t
*nvl
= node
->usn_nvl
;
2593 char valstr
[MAXNAMELEN
];
2594 boolean_t first
= B_TRUE
;
2600 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2601 if (!(ustype
& types
))
2604 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2605 nvpair_t
*nvp
= NULL
;
2611 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2612 if (strcmp(nvpair_name(nvp
),
2613 us_field_names
[field
]) == 0)
2617 type
= nvp
== NULL
? DATA_TYPE_UNKNOWN
: nvpair_type(nvp
);
2619 case DATA_TYPE_UINT32
:
2620 (void) nvpair_value_uint32(nvp
, &val32
);
2622 case DATA_TYPE_UINT64
:
2623 (void) nvpair_value_uint64(nvp
, &val64
);
2625 case DATA_TYPE_STRING
:
2626 (void) nvpair_value_string(nvp
, &strval
);
2628 case DATA_TYPE_UNKNOWN
:
2631 (void) fprintf(stderr
, "invalid data type\n");
2636 if (type
== DATA_TYPE_UINT32
)
2637 strval
= (char *)us_type2str(val32
);
2640 if (type
== DATA_TYPE_UINT64
) {
2641 (void) sprintf(valstr
, "%llu",
2642 (u_longlong_t
)val64
);
2648 case USFIELD_OBJUSED
:
2649 case USFIELD_OBJQUOTA
:
2650 if (type
== DATA_TYPE_UINT64
) {
2652 (void) sprintf(valstr
, "%llu",
2653 (u_longlong_t
)val64
);
2655 zfs_nicenum(val64
, valstr
,
2658 if ((field
== USFIELD_QUOTA
||
2659 field
== USFIELD_OBJQUOTA
) &&
2660 strcmp(valstr
, "0") == 0)
2670 (void) printf("\t");
2675 (void) printf("%s", strval
);
2676 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2677 (void) printf("%-*s", (int)width
[field
], strval
);
2679 (void) printf("%*s", (int)width
[field
], strval
);
2685 (void) printf("\n");
2689 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2690 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2698 boolean_t first
= B_TRUE
;
2700 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2701 col
= gettext(us_field_hdr
[field
]);
2702 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2703 (void) printf(first
? "%-*s" : " %-*s",
2704 (int)width
[field
], col
);
2706 (void) printf(first
? "%*s" : " %*s",
2707 (int)width
[field
], col
);
2712 (void) printf("\n");
2715 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2716 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2718 nvlist_free(node
->usn_nvl
);
2723 zfs_do_userspace(int argc
, char **argv
)
2726 zfs_userquota_prop_t p
;
2727 uu_avl_pool_t
*avl_pool
;
2729 uu_avl_walk_t
*walk
;
2731 char deffields
[] = "type,name,used,quota,objused,objquota";
2732 char *ofield
= NULL
;
2733 char *tfield
= NULL
;
2737 boolean_t scripted
= B_FALSE
;
2738 boolean_t prtnum
= B_FALSE
;
2739 boolean_t parsable
= B_FALSE
;
2740 boolean_t sid2posix
= B_FALSE
;
2743 zfs_sort_column_t
*sortcol
= NULL
;
2744 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2748 uu_list_pool_t
*listpool
;
2750 uu_avl_index_t idx
= 0;
2751 uu_list_index_t idx2
= 0;
2756 if (strcmp(argv
[0], "groupspace") == 0)
2757 /* Toggle default group types */
2758 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2760 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2776 if (zfs_add_sort_column(&sortcol
, optarg
,
2777 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
2778 (void) fprintf(stderr
,
2779 gettext("invalid field '%s'\n"), optarg
);
2790 (void) fprintf(stderr
, gettext("missing argument for "
2791 "'%c' option\n"), optopt
);
2795 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2805 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2809 (void) fprintf(stderr
, gettext("too many arguments\n"));
2813 /* Use default output fields if not specified using -o */
2817 if ((delim
= strchr(ofield
, ',')) != NULL
)
2819 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
2820 (void) fprintf(stderr
, gettext("invalid type '%s' "
2821 "for -o option\n"), ofield
);
2826 } while (delim
!= NULL
);
2827 fields
[cfield
] = USFIELD_LAST
;
2829 /* Override output types (-t option) */
2830 if (tfield
!= NULL
) {
2834 boolean_t found
= B_FALSE
;
2836 if ((delim
= strchr(tfield
, ',')) != NULL
)
2838 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
2840 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
2842 types
|= us_type_bits
[i
];
2847 (void) fprintf(stderr
, gettext("invalid type "
2848 "'%s' for -t option\n"), tfield
);
2853 } while (delim
!= NULL
);
2856 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
2859 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
2860 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
2862 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
2865 /* Always add default sorting columns */
2866 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
2867 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
2869 cb
.cb_sortcol
= sortcol
;
2870 cb
.cb_numname
= prtnum
;
2871 cb
.cb_nicenum
= !parsable
;
2872 cb
.cb_avl_pool
= avl_pool
;
2873 cb
.cb_avl
= avl_tree
;
2874 cb
.cb_sid2posix
= sid2posix
;
2876 for (i
= 0; i
< USFIELD_LAST
; i
++)
2877 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
2879 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
2880 if ((zfs_prop_is_user(p
) &&
2881 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
2882 (zfs_prop_is_group(p
) &&
2883 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))))
2887 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
2892 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
2895 us_populated
= B_TRUE
;
2897 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
2898 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
2899 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
2900 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
2902 while (node
!= NULL
) {
2904 node
= uu_avl_next(avl_tree
, node
);
2905 uu_avl_remove(avl_tree
, rmnode
);
2906 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
2907 uu_list_insert(list
, rmnode
, idx2
);
2910 for (node
= uu_list_first(list
); node
!= NULL
;
2911 node
= uu_list_next(list
, node
)) {
2912 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
2914 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
2915 uu_avl_insert(avl_tree
, node
, idx
);
2918 uu_list_destroy(list
);
2919 uu_list_pool_destroy(listpool
);
2921 /* Print and free node nvlist memory */
2922 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
2925 zfs_free_sort_columns(sortcol
);
2927 /* Clean up the AVL tree */
2928 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
2931 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
2932 uu_avl_remove(cb
.cb_avl
, node
);
2936 uu_avl_walk_end(walk
);
2937 uu_avl_destroy(avl_tree
);
2938 uu_avl_pool_destroy(avl_pool
);
2944 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
2945 * [-t type[,...]] [filesystem|volume|snapshot] ...
2947 * -H Scripted mode; elide headers and separate columns by tabs
2948 * -p Display values in parsable (literal) format.
2949 * -r Recurse over all children
2950 * -d Limit recursion by depth.
2951 * -o Control which fields to display.
2952 * -s Specify sort columns, descending order.
2953 * -S Specify sort columns, ascending order.
2954 * -t Control which object types to display.
2956 * When given no arguments, list all filesystems in the system.
2957 * Otherwise, list the specified datasets, optionally recursing down them if
2958 * '-r' is specified.
2960 typedef struct list_cbdata
{
2962 boolean_t cb_literal
;
2963 boolean_t cb_scripted
;
2964 zprop_list_t
*cb_proplist
;
2968 * Given a list of columns to display, output appropriate headers for each one.
2971 print_header(list_cbdata_t
*cb
)
2973 zprop_list_t
*pl
= cb
->cb_proplist
;
2974 char headerbuf
[ZFS_MAXPROPLEN
];
2977 boolean_t first
= B_TRUE
;
2978 boolean_t right_justify
;
2980 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2987 right_justify
= B_FALSE
;
2988 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2989 header
= zfs_prop_column_name(pl
->pl_prop
);
2990 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2992 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2993 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2994 headerbuf
[i
] = '\0';
2998 if (pl
->pl_next
== NULL
&& !right_justify
)
2999 (void) printf("%s", header
);
3000 else if (right_justify
)
3001 (void) printf("%*s", (int)pl
->pl_width
, header
);
3003 (void) printf("%-*s", (int)pl
->pl_width
, header
);
3006 (void) printf("\n");
3010 * Given a dataset and a list of fields, print out all the properties according
3011 * to the described layout.
3014 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
3016 zprop_list_t
*pl
= cb
->cb_proplist
;
3017 boolean_t first
= B_TRUE
;
3018 char property
[ZFS_MAXPROPLEN
];
3019 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
3022 boolean_t right_justify
;
3024 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3026 if (cb
->cb_scripted
)
3027 (void) printf("\t");
3034 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
3035 (void) strlcpy(property
, zfs_get_name(zhp
),
3038 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3039 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
3040 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
3041 sizeof (property
), NULL
, NULL
, 0,
3042 cb
->cb_literal
) != 0)
3046 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3047 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
3048 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
3049 property
, sizeof (property
), cb
->cb_literal
) != 0)
3053 right_justify
= B_TRUE
;
3054 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
3055 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
3056 property
, sizeof (property
), cb
->cb_literal
) != 0)
3060 right_justify
= B_TRUE
;
3062 if (nvlist_lookup_nvlist(userprops
,
3063 pl
->pl_user_prop
, &propval
) != 0)
3066 verify(nvlist_lookup_string(propval
,
3067 ZPROP_VALUE
, &propstr
) == 0);
3068 right_justify
= B_FALSE
;
3072 * If this is being called in scripted mode, or if this is the
3073 * last column and it is left-justified, don't include a width
3076 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3077 (void) printf("%s", propstr
);
3078 else if (right_justify
)
3079 (void) printf("%*s", (int)pl
->pl_width
, propstr
);
3081 (void) printf("%-*s", (int)pl
->pl_width
, propstr
);
3084 (void) printf("\n");
3088 * Generic callback function to list a dataset or snapshot.
3091 list_callback(zfs_handle_t
*zhp
, void *data
)
3093 list_cbdata_t
*cbp
= data
;
3095 if (cbp
->cb_first
) {
3096 if (!cbp
->cb_scripted
)
3098 cbp
->cb_first
= B_FALSE
;
3101 print_dataset(zhp
, cbp
);
3107 zfs_do_list(int argc
, char **argv
)
3110 static char default_fields
[] =
3111 "name,used,available,referenced,mountpoint";
3112 int types
= ZFS_TYPE_DATASET
;
3113 boolean_t types_specified
= B_FALSE
;
3114 char *fields
= NULL
;
3115 list_cbdata_t cb
= { 0 };
3119 zfs_sort_column_t
*sortcol
= NULL
;
3120 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3123 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3129 cb
.cb_literal
= B_TRUE
;
3130 flags
|= ZFS_ITER_LITERAL_PROPS
;
3133 limit
= parse_depth(optarg
, &flags
);
3136 flags
|= ZFS_ITER_RECURSE
;
3139 cb
.cb_scripted
= B_TRUE
;
3142 if (zfs_add_sort_column(&sortcol
, optarg
,
3144 (void) fprintf(stderr
,
3145 gettext("invalid property '%s'\n"), optarg
);
3150 if (zfs_add_sort_column(&sortcol
, optarg
,
3152 (void) fprintf(stderr
,
3153 gettext("invalid property '%s'\n"), optarg
);
3159 types_specified
= B_TRUE
;
3160 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3161 while (*optarg
!= '\0') {
3162 static char *type_subopts
[] = { "filesystem",
3163 "volume", "snapshot", "snap", "bookmark",
3166 switch (getsubopt(&optarg
, type_subopts
,
3169 types
|= ZFS_TYPE_FILESYSTEM
;
3172 types
|= ZFS_TYPE_VOLUME
;
3176 types
|= ZFS_TYPE_SNAPSHOT
;
3179 types
|= ZFS_TYPE_BOOKMARK
;
3182 types
= ZFS_TYPE_DATASET
|
3186 (void) fprintf(stderr
,
3187 gettext("invalid type '%s'\n"),
3194 (void) fprintf(stderr
, gettext("missing argument for "
3195 "'%c' option\n"), optopt
);
3199 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3209 fields
= default_fields
;
3212 * If we are only going to list snapshot names and sort by name,
3213 * then we can use faster version.
3215 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3216 flags
|= ZFS_ITER_SIMPLE
;
3219 * If "-o space" and no types were specified, don't display snapshots.
3221 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3222 types
&= ~ZFS_TYPE_SNAPSHOT
;
3225 * If the user specifies '-o all', the zprop_get_list() doesn't
3226 * normally include the name of the dataset. For 'zfs list', we always
3227 * want this property to be first.
3229 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3233 cb
.cb_first
= B_TRUE
;
3235 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3236 limit
, list_callback
, &cb
);
3238 zprop_free_list(cb
.cb_proplist
);
3239 zfs_free_sort_columns(sortcol
);
3241 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3242 (void) fprintf(stderr
, gettext("no datasets available\n"));
3248 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3249 * zfs rename [-f] -p <fs | vol> <fs | vol>
3250 * zfs rename -r <snap> <snap>
3252 * Renames the given dataset to another of the same type.
3254 * The '-p' flag creates all the non-existing ancestors of the target first.
3258 zfs_do_rename(int argc
, char **argv
)
3263 boolean_t recurse
= B_FALSE
;
3264 boolean_t parents
= B_FALSE
;
3265 boolean_t force_unmount
= B_FALSE
;
3268 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3277 force_unmount
= B_TRUE
;
3281 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3290 /* check number of arguments */
3292 (void) fprintf(stderr
, gettext("missing source dataset "
3297 (void) fprintf(stderr
, gettext("missing target dataset "
3302 (void) fprintf(stderr
, gettext("too many arguments\n"));
3306 if (recurse
&& parents
) {
3307 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3312 if (recurse
&& strchr(argv
[0], '@') == 0) {
3313 (void) fprintf(stderr
, gettext("source dataset for recursive "
3314 "rename must be a snapshot\n"));
3318 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3319 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3322 /* If we were asked and the name looks good, try to create ancestors. */
3323 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3324 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3329 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3338 * Promotes the given clone fs to be the parent
3342 zfs_do_promote(int argc
, char **argv
)
3348 if (argc
> 1 && argv
[1][0] == '-') {
3349 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3354 /* check number of arguments */
3356 (void) fprintf(stderr
, gettext("missing clone filesystem"
3361 (void) fprintf(stderr
, gettext("too many arguments\n"));
3365 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3369 ret
= (zfs_promote(zhp
) != 0);
3377 * zfs rollback [-rRf] <snapshot>
3379 * -r Delete any intervening snapshots before doing rollback
3380 * -R Delete any snapshots and their clones
3381 * -f ignored for backwards compatibility
3383 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3384 * since then and making it the active dataset. If more recent snapshots exist,
3385 * the command will complain unless the '-r' flag is given.
3387 typedef struct rollback_cbdata
{
3393 boolean_t cb_recurse
;
3394 } rollback_cbdata_t
;
3397 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3399 rollback_cbdata_t
*cbp
= data
;
3401 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3402 (void) fprintf(stderr
, gettext("cannot rollback to "
3403 "'%s': clones of previous snapshots exist\n"),
3405 (void) fprintf(stderr
, gettext("use '-R' to "
3406 "force deletion of the following clones and "
3412 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3420 * Report any snapshots more recent than the one specified. Used when '-r' is
3421 * not specified. We reuse this same callback for the snapshot dependents - if
3422 * 'cb_dependent' is set, then this is a dependent and we should report it
3423 * without checking the transaction group.
3426 rollback_check(zfs_handle_t
*zhp
, void *data
)
3428 rollback_cbdata_t
*cbp
= data
;
3430 if (cbp
->cb_doclones
) {
3435 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3436 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3437 (void) fprintf(stderr
, gettext("cannot "
3438 "rollback to '%s': more recent snapshots "
3439 "or bookmarks exist\n"),
3441 (void) fprintf(stderr
, gettext("use '-r' to "
3442 "force deletion of the following "
3443 "snapshots and bookmarks:\n"));
3448 if (cbp
->cb_recurse
) {
3449 if (zfs_iter_dependents(zhp
, B_TRUE
,
3450 rollback_check_dependent
, cbp
) != 0) {
3455 (void) fprintf(stderr
, "%s\n",
3464 zfs_do_rollback(int argc
, char **argv
)
3468 boolean_t force
= B_FALSE
;
3469 rollback_cbdata_t cb
= { 0 };
3470 zfs_handle_t
*zhp
, *snap
;
3471 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3475 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3488 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3497 /* check number of arguments */
3499 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3503 (void) fprintf(stderr
, gettext("too many arguments\n"));
3507 /* open the snapshot */
3508 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3511 /* open the parent dataset */
3512 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3513 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3515 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3521 * Check for more recent snapshots and/or clones based on the presence
3524 cb
.cb_target
= argv
[0];
3525 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3526 cb
.cb_first
= B_TRUE
;
3528 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, rollback_check
, &cb
)) != 0)
3530 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
3533 if ((ret
= cb
.cb_error
) != 0)
3537 * Rollback parent to the given snapshot.
3539 ret
= zfs_rollback(zhp
, snap
, force
);
3552 * zfs set property=value ... { fs | snap | vol } ...
3554 * Sets the given properties for all datasets specified on the command line.
3558 set_callback(zfs_handle_t
*zhp
, void *data
)
3560 nvlist_t
*props
= data
;
3562 if (zfs_prop_set_list(zhp
, props
) != 0) {
3563 switch (libzfs_errno(g_zfs
)) {
3564 case EZFS_MOUNTFAILED
:
3565 (void) fprintf(stderr
, gettext("property may be set "
3566 "but unable to remount filesystem\n"));
3568 case EZFS_SHARENFSFAILED
:
3569 (void) fprintf(stderr
, gettext("property may be set "
3570 "but unable to reshare filesystem\n"));
3579 zfs_do_set(int argc
, char **argv
)
3581 nvlist_t
*props
= NULL
;
3582 int ds_start
= -1; /* argv idx of first dataset arg */
3586 /* check for options */
3587 if (argc
> 1 && argv
[1][0] == '-') {
3588 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3593 /* check number of arguments */
3595 (void) fprintf(stderr
, gettext("missing arguments\n"));
3599 if (strchr(argv
[1], '=') == NULL
) {
3600 (void) fprintf(stderr
, gettext("missing property=value "
3603 (void) fprintf(stderr
, gettext("missing dataset "
3609 /* validate argument order: prop=val args followed by dataset args */
3610 for (i
= 1; i
< argc
; i
++) {
3611 if (strchr(argv
[i
], '=') != NULL
) {
3613 /* out-of-order prop=val argument */
3614 (void) fprintf(stderr
, gettext("invalid "
3615 "argument order\n"));
3618 } else if (ds_start
< 0) {
3623 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
3627 /* Populate a list of property settings */
3628 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3630 for (i
= 1; i
< ds_start
; i
++) {
3631 if ((ret
= parseprop(props
, argv
[i
])) != 0)
3635 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
3636 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, props
);
3643 typedef struct snap_cbdata
{
3645 boolean_t sd_recursive
;
3646 const char *sd_snapname
;
3650 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3652 snap_cbdata_t
*sd
= arg
;
3657 if (sd
->sd_recursive
&&
3658 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
3663 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3666 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3669 if (sd
->sd_recursive
)
3670 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3676 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3678 * Creates a snapshot with the given name. While functionally equivalent to
3679 * 'zfs create', it is a separate command to differentiate intent.
3682 zfs_do_snapshot(int argc
, char **argv
)
3687 snap_cbdata_t sd
= { 0 };
3688 boolean_t multiple_snaps
= B_FALSE
;
3690 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3692 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3696 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3699 if (parseprop(props
, optarg
) != 0) {
3700 nvlist_free(sd
.sd_nvl
);
3706 sd
.sd_recursive
= B_TRUE
;
3707 multiple_snaps
= B_TRUE
;
3710 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3719 /* check number of arguments */
3721 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3726 multiple_snaps
= B_TRUE
;
3727 for (; argc
> 0; argc
--, argv
++) {
3731 atp
= strchr(argv
[0], '@');
3735 sd
.sd_snapname
= atp
+ 1;
3736 zhp
= zfs_open(g_zfs
, argv
[0],
3737 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3740 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3744 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3745 nvlist_free(sd
.sd_nvl
);
3747 if (ret
!= 0 && multiple_snaps
)
3748 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3752 nvlist_free(sd
.sd_nvl
);
3759 * Send a backup stream to stdout.
3762 zfs_do_send(int argc
, char **argv
)
3764 char *fromname
= NULL
;
3765 char *toname
= NULL
;
3766 char *resume_token
= NULL
;
3769 sendflags_t flags
= { 0 };
3771 nvlist_t
*dbgnv
= NULL
;
3772 boolean_t extraverbose
= B_FALSE
;
3775 while ((c
= getopt(argc
, argv
, ":i:I:RDpvnPLet:c")) != -1) {
3786 flags
.doall
= B_TRUE
;
3789 flags
.replicate
= B_TRUE
;
3792 flags
.props
= B_TRUE
;
3795 flags
.parsable
= B_TRUE
;
3796 flags
.verbose
= B_TRUE
;
3800 extraverbose
= B_TRUE
;
3801 flags
.verbose
= B_TRUE
;
3802 flags
.progress
= B_TRUE
;
3805 flags
.dedup
= B_TRUE
;
3808 flags
.dryrun
= B_TRUE
;
3811 flags
.largeblock
= B_TRUE
;
3814 flags
.embed_data
= B_TRUE
;
3817 resume_token
= optarg
;
3820 flags
.compress
= B_TRUE
;
3823 (void) fprintf(stderr
, gettext("missing argument for "
3824 "'%c' option\n"), optopt
);
3828 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3837 if (resume_token
!= NULL
) {
3838 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
3840 (void) fprintf(stderr
,
3841 gettext("invalid flags combined with -t\n"));
3845 (void) fprintf(stderr
, gettext("no additional "
3846 "arguments are permitted with -t\n"));
3851 (void) fprintf(stderr
,
3852 gettext("missing snapshot argument\n"));
3856 (void) fprintf(stderr
, gettext("too many arguments\n"));
3861 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
3862 (void) fprintf(stderr
,
3863 gettext("Error: Stream can not be written to a terminal.\n"
3864 "You must redirect standard output.\n"));
3868 if (resume_token
!= NULL
) {
3869 return (zfs_send_resume(g_zfs
, &flags
, STDOUT_FILENO
,
3874 * Special case sending a filesystem, or from a bookmark.
3876 if (strchr(argv
[0], '@') == NULL
||
3877 (fromname
&& strchr(fromname
, '#') != NULL
)) {
3878 char frombuf
[ZFS_MAX_DATASET_NAME_LEN
];
3879 enum lzc_send_flags lzc_flags
= 0;
3881 if (flags
.replicate
|| flags
.doall
|| flags
.props
||
3882 flags
.dedup
|| flags
.dryrun
|| flags
.verbose
||
3884 (void) fprintf(stderr
,
3886 "Unsupported flag with filesystem or bookmark.\n"));
3890 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
3894 if (flags
.largeblock
)
3895 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
3896 if (flags
.embed_data
)
3897 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
3899 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
3901 if (fromname
!= NULL
&&
3902 (fromname
[0] == '#' || fromname
[0] == '@')) {
3904 * Incremental source name begins with # or @.
3905 * Default to same fs as target.
3907 (void) strlcpy(frombuf
, argv
[0], sizeof (frombuf
));
3908 cp
= strchr(frombuf
, '@');
3911 (void) strlcat(frombuf
, fromname
, sizeof (frombuf
));
3914 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, lzc_flags
);
3919 cp
= strchr(argv
[0], '@');
3922 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3927 * If they specified the full path to the snapshot, chop off
3928 * everything except the short name of the snapshot, but special
3929 * case if they specify the origin.
3931 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
3932 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
3935 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
3936 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
3938 if (strcmp(origin
, fromname
) == 0) {
3940 flags
.fromorigin
= B_TRUE
;
3943 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
3944 (void) fprintf(stderr
,
3945 gettext("incremental source must be "
3946 "in same filesystem\n"));
3950 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
3951 (void) fprintf(stderr
,
3952 gettext("invalid incremental source\n"));
3958 if (flags
.replicate
&& fromname
== NULL
)
3959 flags
.doall
= B_TRUE
;
3961 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
3962 extraverbose
? &dbgnv
: NULL
);
3964 if (extraverbose
&& dbgnv
!= NULL
) {
3966 * dump_nvlist prints to stdout, but that's been
3967 * redirected to a file. Make it print to stderr
3970 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
3971 dump_nvlist(dbgnv
, 0);
3980 * Restore a backup stream from stdin.
3983 zfs_do_receive(int argc
, char **argv
)
3986 recvflags_t flags
= { 0 };
3987 boolean_t abort_resumable
= B_FALSE
;
3990 nvpair_t
*nvp
= NULL
;
3992 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3996 while ((c
= getopt(argc
, argv
, ":o:denuvFsA")) != -1) {
3999 if (parseprop(props
, optarg
) != 0) {
4005 flags
.isprefix
= B_TRUE
;
4008 flags
.isprefix
= B_TRUE
;
4009 flags
.istail
= B_TRUE
;
4012 flags
.dryrun
= B_TRUE
;
4015 flags
.nomount
= B_TRUE
;
4018 flags
.verbose
= B_TRUE
;
4021 flags
.resumable
= B_TRUE
;
4024 flags
.force
= B_TRUE
;
4027 abort_resumable
= B_TRUE
;
4030 (void) fprintf(stderr
, gettext("missing argument for "
4031 "'%c' option\n"), optopt
);
4035 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4044 /* check number of arguments */
4046 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4050 (void) fprintf(stderr
, gettext("too many arguments\n"));
4054 while ((nvp
= nvlist_next_nvpair(props
, nvp
))) {
4055 if (strcmp(nvpair_name(nvp
), "origin") != 0) {
4056 (void) fprintf(stderr
, gettext("invalid option"));
4061 if (abort_resumable
) {
4062 if (flags
.isprefix
|| flags
.istail
|| flags
.dryrun
||
4063 flags
.resumable
|| flags
.nomount
) {
4064 (void) fprintf(stderr
, gettext("invalid option"));
4068 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
4069 (void) snprintf(namebuf
, sizeof (namebuf
),
4070 "%s/%%recv", argv
[0]);
4072 if (zfs_dataset_exists(g_zfs
, namebuf
,
4073 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) {
4074 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4075 namebuf
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4080 err
= zfs_destroy(zhp
, B_FALSE
);
4083 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4084 argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4087 if (!zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) ||
4088 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
4089 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == -1) {
4090 (void) fprintf(stderr
,
4091 gettext("'%s' does not have any "
4092 "resumable receive state to abort\n"),
4098 err
= zfs_destroy(zhp
, B_FALSE
);
4105 if (isatty(STDIN_FILENO
)) {
4106 (void) fprintf(stderr
,
4107 gettext("Error: Backup stream can not be read "
4108 "from a terminal.\n"
4109 "You must redirect standard input.\n"));
4113 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
4120 * allow/unallow stuff
4122 /* copied from zfs/sys/dsl_deleg.h */
4123 #define ZFS_DELEG_PERM_CREATE "create"
4124 #define ZFS_DELEG_PERM_DESTROY "destroy"
4125 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4126 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4127 #define ZFS_DELEG_PERM_CLONE "clone"
4128 #define ZFS_DELEG_PERM_PROMOTE "promote"
4129 #define ZFS_DELEG_PERM_RENAME "rename"
4130 #define ZFS_DELEG_PERM_MOUNT "mount"
4131 #define ZFS_DELEG_PERM_SHARE "share"
4132 #define ZFS_DELEG_PERM_SEND "send"
4133 #define ZFS_DELEG_PERM_RECEIVE "receive"
4134 #define ZFS_DELEG_PERM_ALLOW "allow"
4135 #define ZFS_DELEG_PERM_USERPROP "userprop"
4136 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4137 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4138 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4139 #define ZFS_DELEG_PERM_USERUSED "userused"
4140 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4141 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
4142 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
4143 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
4144 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
4146 #define ZFS_DELEG_PERM_HOLD "hold"
4147 #define ZFS_DELEG_PERM_RELEASE "release"
4148 #define ZFS_DELEG_PERM_DIFF "diff"
4149 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4151 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4153 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
4154 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
4155 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
4156 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
4157 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
4158 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
4159 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
4160 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
4161 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
4162 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
4163 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
4164 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
4165 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
4166 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
4167 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
4168 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
4169 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
4171 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
4172 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
4173 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
4174 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
4175 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
4176 { ZFS_DELEG_PERM_USEROBJQUOTA
, ZFS_DELEG_NOTE_USEROBJQUOTA
},
4177 { ZFS_DELEG_PERM_USEROBJUSED
, ZFS_DELEG_NOTE_USEROBJUSED
},
4178 { ZFS_DELEG_PERM_GROUPOBJQUOTA
, ZFS_DELEG_NOTE_GROUPOBJQUOTA
},
4179 { ZFS_DELEG_PERM_GROUPOBJUSED
, ZFS_DELEG_NOTE_GROUPOBJUSED
},
4180 { NULL
, ZFS_DELEG_NOTE_NONE
}
4183 /* permission structure */
4184 typedef struct deleg_perm
{
4185 zfs_deleg_who_type_t dp_who_type
;
4186 const char *dp_name
;
4188 boolean_t dp_descend
;
4192 typedef struct deleg_perm_node
{
4193 deleg_perm_t dpn_perm
;
4195 uu_avl_node_t dpn_avl_node
;
4196 } deleg_perm_node_t
;
4198 typedef struct fs_perm fs_perm_t
;
4200 /* permissions set */
4201 typedef struct who_perm
{
4202 zfs_deleg_who_type_t who_type
;
4203 const char *who_name
; /* id */
4204 char who_ug_name
[256]; /* user/group name */
4205 fs_perm_t
*who_fsperm
; /* uplink */
4207 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
4211 typedef struct who_perm_node
{
4212 who_perm_t who_perm
;
4213 uu_avl_node_t who_avl_node
;
4216 typedef struct fs_perm_set fs_perm_set_t
;
4217 /* fs permissions */
4219 const char *fsp_name
;
4221 uu_avl_t
*fsp_sc_avl
; /* sets,create */
4222 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
4224 fs_perm_set_t
*fsp_set
; /* uplink */
4228 typedef struct fs_perm_node
{
4229 fs_perm_t fspn_fsperm
;
4232 uu_list_node_t fspn_list_node
;
4235 /* top level structure */
4236 struct fs_perm_set
{
4237 uu_list_pool_t
*fsps_list_pool
;
4238 uu_list_t
*fsps_list
; /* list of fs_perms */
4240 uu_avl_pool_t
*fsps_named_set_avl_pool
;
4241 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
4242 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
4245 static inline const char *
4246 deleg_perm_type(zfs_deleg_note_t note
)
4252 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4253 case ZFS_DELEG_NOTE_GROUPUSED
:
4254 case ZFS_DELEG_NOTE_USERPROP
:
4255 case ZFS_DELEG_NOTE_USERQUOTA
:
4256 case ZFS_DELEG_NOTE_USERUSED
:
4257 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
4258 case ZFS_DELEG_NOTE_USEROBJUSED
:
4259 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
4260 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
4262 return (gettext("other"));
4264 return (gettext("subcommand"));
4269 who_type2weight(zfs_deleg_who_type_t who_type
)
4273 case ZFS_DELEG_NAMED_SET_SETS
:
4274 case ZFS_DELEG_NAMED_SET
:
4277 case ZFS_DELEG_CREATE_SETS
:
4278 case ZFS_DELEG_CREATE
:
4281 case ZFS_DELEG_USER_SETS
:
4282 case ZFS_DELEG_USER
:
4285 case ZFS_DELEG_GROUP_SETS
:
4286 case ZFS_DELEG_GROUP
:
4289 case ZFS_DELEG_EVERYONE_SETS
:
4290 case ZFS_DELEG_EVERYONE
:
4302 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4304 const who_perm_node_t
*l
= larg
;
4305 const who_perm_node_t
*r
= rarg
;
4306 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
4307 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
4308 int lweight
= who_type2weight(ltype
);
4309 int rweight
= who_type2weight(rtype
);
4310 int res
= lweight
- rweight
;
4312 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
4313 ZFS_MAX_DELEG_NAME
-1);
4325 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4327 const deleg_perm_node_t
*l
= larg
;
4328 const deleg_perm_node_t
*r
= rarg
;
4329 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
4330 ZFS_MAX_DELEG_NAME
-1);
4342 fs_perm_set_init(fs_perm_set_t
*fspset
)
4344 bzero(fspset
, sizeof (fs_perm_set_t
));
4346 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4347 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4348 NULL
, UU_DEFAULT
)) == NULL
)
4350 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4351 UU_DEFAULT
)) == NULL
)
4354 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4355 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4356 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4357 UU_DEFAULT
)) == NULL
)
4360 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4361 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4362 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4363 UU_DEFAULT
)) == NULL
)
4366 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4367 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4368 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4373 static inline void fs_perm_fini(fs_perm_t
*);
4374 static inline void who_perm_fini(who_perm_t
*);
4377 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4379 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4381 while (node
!= NULL
) {
4382 fs_perm_node_t
*next_node
=
4383 uu_list_next(fspset
->fsps_list
, node
);
4384 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4385 fs_perm_fini(fsperm
);
4386 uu_list_remove(fspset
->fsps_list
, node
);
4391 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4392 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4393 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4397 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4400 deleg_perm
->dp_who_type
= type
;
4401 deleg_perm
->dp_name
= name
;
4405 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4406 zfs_deleg_who_type_t type
, const char *name
)
4408 uu_avl_pool_t
*pool
;
4409 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4411 bzero(who_perm
, sizeof (who_perm_t
));
4413 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4414 UU_DEFAULT
)) == NULL
)
4417 who_perm
->who_type
= type
;
4418 who_perm
->who_name
= name
;
4419 who_perm
->who_fsperm
= fsperm
;
4423 who_perm_fini(who_perm_t
*who_perm
)
4425 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4427 while (node
!= NULL
) {
4428 deleg_perm_node_t
*next_node
=
4429 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4431 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4436 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4440 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4442 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4443 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4445 bzero(fsperm
, sizeof (fs_perm_t
));
4447 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4451 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4455 fsperm
->fsp_set
= fspset
;
4456 fsperm
->fsp_name
= fsname
;
4460 fs_perm_fini(fs_perm_t
*fsperm
)
4462 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4463 while (node
!= NULL
) {
4464 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4466 who_perm_t
*who_perm
= &node
->who_perm
;
4467 who_perm_fini(who_perm
);
4468 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4473 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4474 while (node
!= NULL
) {
4475 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4477 who_perm_t
*who_perm
= &node
->who_perm
;
4478 who_perm_fini(who_perm
);
4479 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4484 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4485 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4489 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4490 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4492 uu_avl_index_t idx
= 0;
4494 deleg_perm_node_t
*found_node
= NULL
;
4495 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4497 deleg_perm_init(deleg_perm
, who_type
, name
);
4499 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4501 uu_avl_insert(avl
, node
, idx
);
4504 deleg_perm
= &node
->dpn_perm
;
4509 case ZFS_DELEG_LOCAL
:
4510 deleg_perm
->dp_local
= B_TRUE
;
4512 case ZFS_DELEG_DESCENDENT
:
4513 deleg_perm
->dp_descend
= B_TRUE
;
4518 assert(B_FALSE
); /* invalid locality */
4523 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4525 nvpair_t
*nvp
= NULL
;
4526 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4527 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4528 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4530 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4531 const char *name
= nvpair_name(nvp
);
4532 data_type_t type
= nvpair_type(nvp
);
4533 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4534 deleg_perm_node_t
*node
=
4535 safe_malloc(sizeof (deleg_perm_node_t
));
4537 VERIFY(type
== DATA_TYPE_BOOLEAN
);
4539 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4540 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4547 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4549 nvpair_t
*nvp
= NULL
;
4550 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4552 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4553 nvlist_t
*nvl2
= NULL
;
4554 const char *name
= nvpair_name(nvp
);
4555 uu_avl_t
*avl
= NULL
;
4556 uu_avl_pool_t
*avl_pool
= NULL
;
4557 zfs_deleg_who_type_t perm_type
= name
[0];
4558 char perm_locality
= name
[1];
4559 const char *perm_name
= name
+ 3;
4560 boolean_t is_set
= B_TRUE
;
4561 who_perm_t
*who_perm
= NULL
;
4563 assert('$' == name
[2]);
4565 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4568 switch (perm_type
) {
4569 case ZFS_DELEG_CREATE
:
4570 case ZFS_DELEG_CREATE_SETS
:
4571 case ZFS_DELEG_NAMED_SET
:
4572 case ZFS_DELEG_NAMED_SET_SETS
:
4573 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4574 avl
= fsperm
->fsp_sc_avl
;
4576 case ZFS_DELEG_USER
:
4577 case ZFS_DELEG_USER_SETS
:
4578 case ZFS_DELEG_GROUP
:
4579 case ZFS_DELEG_GROUP_SETS
:
4580 case ZFS_DELEG_EVERYONE
:
4581 case ZFS_DELEG_EVERYONE_SETS
:
4582 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4583 avl
= fsperm
->fsp_uge_avl
;
4587 assert(!"unhandled zfs_deleg_who_type_t");
4591 who_perm_node_t
*found_node
= NULL
;
4592 who_perm_node_t
*node
= safe_malloc(
4593 sizeof (who_perm_node_t
));
4594 who_perm
= &node
->who_perm
;
4595 uu_avl_index_t idx
= 0;
4597 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4598 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4600 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4602 if (avl
== fsperm
->fsp_uge_avl
) {
4604 struct passwd
*p
= NULL
;
4605 struct group
*g
= NULL
;
4606 const char *nice_name
= NULL
;
4608 switch (perm_type
) {
4609 case ZFS_DELEG_USER_SETS
:
4610 case ZFS_DELEG_USER
:
4611 rid
= atoi(perm_name
);
4614 nice_name
= p
->pw_name
;
4616 case ZFS_DELEG_GROUP_SETS
:
4617 case ZFS_DELEG_GROUP
:
4618 rid
= atoi(perm_name
);
4621 nice_name
= g
->gr_name
;
4628 if (nice_name
!= NULL
)
4630 node
->who_perm
.who_ug_name
,
4634 uu_avl_insert(avl
, node
, idx
);
4637 who_perm
= &node
->who_perm
;
4640 VERIFY3P(who_perm
, !=, NULL
);
4641 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4648 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4650 nvpair_t
*nvp
= NULL
;
4651 uu_avl_index_t idx
= 0;
4653 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4654 nvlist_t
*nvl2
= NULL
;
4655 const char *fsname
= nvpair_name(nvp
);
4656 data_type_t type
= nvpair_type(nvp
);
4657 fs_perm_t
*fsperm
= NULL
;
4658 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4662 fsperm
= &node
->fspn_fsperm
;
4664 VERIFY(DATA_TYPE_NVLIST
== type
);
4666 uu_list_node_init(node
, &node
->fspn_list_node
,
4667 fspset
->fsps_list_pool
);
4669 idx
= uu_list_numnodes(fspset
->fsps_list
);
4670 fs_perm_init(fsperm
, fspset
, fsname
);
4672 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4675 (void) parse_fs_perm(fsperm
, nvl2
);
4677 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4683 static inline const char *
4684 deleg_perm_comment(zfs_deleg_note_t note
)
4686 const char *str
= "";
4691 case ZFS_DELEG_NOTE_ALLOW
:
4692 str
= gettext("Must also have the permission that is being"
4693 "\n\t\t\t\tallowed");
4695 case ZFS_DELEG_NOTE_CLONE
:
4696 str
= gettext("Must also have the 'create' ability and 'mount'"
4697 "\n\t\t\t\tability in the origin file system");
4699 case ZFS_DELEG_NOTE_CREATE
:
4700 str
= gettext("Must also have the 'mount' ability");
4702 case ZFS_DELEG_NOTE_DESTROY
:
4703 str
= gettext("Must also have the 'mount' ability");
4705 case ZFS_DELEG_NOTE_DIFF
:
4706 str
= gettext("Allows lookup of paths within a dataset;"
4707 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4708 "\n\t\t\t\tin order to use zfs diff");
4710 case ZFS_DELEG_NOTE_HOLD
:
4711 str
= gettext("Allows adding a user hold to a snapshot");
4713 case ZFS_DELEG_NOTE_MOUNT
:
4714 str
= gettext("Allows mount/umount of ZFS datasets");
4716 case ZFS_DELEG_NOTE_PROMOTE
:
4717 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4718 " 'promote' ability in the origin file system");
4720 case ZFS_DELEG_NOTE_RECEIVE
:
4721 str
= gettext("Must also have the 'mount' and 'create'"
4724 case ZFS_DELEG_NOTE_RELEASE
:
4725 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4726 "might destroy the snapshot");
4728 case ZFS_DELEG_NOTE_RENAME
:
4729 str
= gettext("Must also have the 'mount' and 'create'"
4730 "\n\t\t\t\tability in the new parent");
4732 case ZFS_DELEG_NOTE_ROLLBACK
:
4735 case ZFS_DELEG_NOTE_SEND
:
4738 case ZFS_DELEG_NOTE_SHARE
:
4739 str
= gettext("Allows sharing file systems over NFS or SMB"
4740 "\n\t\t\t\tprotocols");
4742 case ZFS_DELEG_NOTE_SNAPSHOT
:
4746 * case ZFS_DELEG_NOTE_VSCAN:
4747 * str = gettext("");
4751 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4752 str
= gettext("Allows accessing any groupquota@... property");
4754 case ZFS_DELEG_NOTE_GROUPUSED
:
4755 str
= gettext("Allows reading any groupused@... property");
4757 case ZFS_DELEG_NOTE_USERPROP
:
4758 str
= gettext("Allows changing any user property");
4760 case ZFS_DELEG_NOTE_USERQUOTA
:
4761 str
= gettext("Allows accessing any userquota@... property");
4763 case ZFS_DELEG_NOTE_USERUSED
:
4764 str
= gettext("Allows reading any userused@... property");
4766 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
4767 str
= gettext("Allows accessing any userobjquota@... property");
4769 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
4770 str
= gettext("Allows accessing any \n\t\t\t\t"
4771 "groupobjquota@... property");
4773 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
4774 str
= gettext("Allows reading any groupobjused@... property");
4776 case ZFS_DELEG_NOTE_USEROBJUSED
:
4777 str
= gettext("Allows reading any userobjused@... property");
4795 boolean_t recursive
; /* unallow only */
4796 boolean_t prt_usage
;
4798 boolean_t prt_perms
;
4801 const char *dataset
;
4805 prop_cmp(const void *a
, const void *b
)
4807 const char *str1
= *(const char **)a
;
4808 const char *str2
= *(const char **)b
;
4809 return (strcmp(str1
, str2
));
4813 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
4815 const char *opt_desc
[] = {
4816 "-h", gettext("show this help message and exit"),
4817 "-l", gettext("set permission locally"),
4818 "-d", gettext("set permission for descents"),
4819 "-u", gettext("set permission for user"),
4820 "-g", gettext("set permission for group"),
4821 "-e", gettext("set permission for everyone"),
4822 "-c", gettext("set create time permission"),
4823 "-s", gettext("define permission set"),
4825 "-r", gettext("remove permissions recursively"),
4827 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
4828 size_t allow_size
= unallow_size
- 2;
4829 const char *props
[ZFS_NUM_PROPS
];
4832 FILE *fp
= requested
? stdout
: stderr
;
4833 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
4834 const char *fmt
= gettext("%-16s %-14s\t%s\n");
4836 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
4838 (void) fprintf(fp
, gettext("Options:\n"));
4839 for (i
= 0; i
< (un
? unallow_size
: allow_size
); i
+= 2) {
4840 const char *opt
= opt_desc
[i
];
4841 const char *optdsc
= opt_desc
[i
+ 1];
4842 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
4845 (void) fprintf(fp
, gettext("\nThe following permissions are "
4847 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
4849 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
4850 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
4851 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
4852 const char *perm_type
= deleg_perm_type(perm_note
);
4853 const char *perm_comment
= deleg_perm_comment(perm_note
);
4854 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
4857 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
4858 zprop_desc_t
*pd
= &pdtbl
[i
];
4859 if (pd
->pd_visible
!= B_TRUE
)
4862 if (pd
->pd_attr
== PROP_READONLY
)
4865 props
[count
++] = pd
->pd_name
;
4867 props
[count
] = NULL
;
4869 qsort(props
, count
, sizeof (char *), prop_cmp
);
4871 for (i
= 0; i
< count
; i
++)
4872 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
4875 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
4877 exit(requested
? 0 : 2);
4880 static inline const char *
4881 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
4884 if (un
&& argc
== expected_argc
- 1)
4886 else if (argc
== expected_argc
)
4887 *permsp
= argv
[argc
- 2];
4889 allow_usage(un
, B_FALSE
,
4890 gettext("wrong number of parameters\n"));
4892 return (argv
[argc
- 1]);
4896 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
4898 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
4899 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
4900 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
4901 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
4904 allow_usage(un
, B_FALSE
,
4905 gettext("-u, -g, and -e are mutually exclusive\n"));
4907 if (opts
->prt_usage
) {
4908 if (argc
== 0 && all_sum
== 0)
4909 allow_usage(un
, B_TRUE
, NULL
);
4916 allow_usage(un
, B_FALSE
,
4917 gettext("invalid options combined with -s\n"));
4919 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4920 if (argv
[0][0] != '@')
4921 allow_usage(un
, B_FALSE
,
4922 gettext("invalid set name: missing '@' prefix\n"));
4923 opts
->who
= argv
[0];
4924 } else if (opts
->create
) {
4925 if (ldcsuge_sum
> 1)
4926 allow_usage(un
, B_FALSE
,
4927 gettext("invalid options combined with -c\n"));
4928 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4929 } else if (opts
->everyone
) {
4931 allow_usage(un
, B_FALSE
,
4932 gettext("invalid options combined with -e\n"));
4933 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4934 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
4936 opts
->everyone
= B_TRUE
;
4939 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4940 } else if (argc
== 1 && !un
) {
4941 opts
->prt_perms
= B_TRUE
;
4942 opts
->dataset
= argv
[argc
-1];
4944 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4945 opts
->who
= argv
[0];
4948 if (!opts
->local
&& !opts
->descend
) {
4949 opts
->local
= B_TRUE
;
4950 opts
->descend
= B_TRUE
;
4955 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
4956 const char *who
, char *perms
, nvlist_t
*top_nvl
)
4959 char ld
[2] = { '\0', '\0' };
4960 char who_buf
[MAXNAMELEN
+ 32];
4961 char base_type
= '\0';
4962 char set_type
= '\0';
4963 nvlist_t
*base_nvl
= NULL
;
4964 nvlist_t
*set_nvl
= NULL
;
4967 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4969 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4973 case ZFS_DELEG_NAMED_SET_SETS
:
4974 case ZFS_DELEG_NAMED_SET
:
4975 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
4976 base_type
= ZFS_DELEG_NAMED_SET
;
4977 ld
[0] = ZFS_DELEG_NA
;
4979 case ZFS_DELEG_CREATE_SETS
:
4980 case ZFS_DELEG_CREATE
:
4981 set_type
= ZFS_DELEG_CREATE_SETS
;
4982 base_type
= ZFS_DELEG_CREATE
;
4983 ld
[0] = ZFS_DELEG_NA
;
4985 case ZFS_DELEG_USER_SETS
:
4986 case ZFS_DELEG_USER
:
4987 set_type
= ZFS_DELEG_USER_SETS
;
4988 base_type
= ZFS_DELEG_USER
;
4990 ld
[0] = ZFS_DELEG_LOCAL
;
4992 ld
[1] = ZFS_DELEG_DESCENDENT
;
4994 case ZFS_DELEG_GROUP_SETS
:
4995 case ZFS_DELEG_GROUP
:
4996 set_type
= ZFS_DELEG_GROUP_SETS
;
4997 base_type
= ZFS_DELEG_GROUP
;
4999 ld
[0] = ZFS_DELEG_LOCAL
;
5001 ld
[1] = ZFS_DELEG_DESCENDENT
;
5003 case ZFS_DELEG_EVERYONE_SETS
:
5004 case ZFS_DELEG_EVERYONE
:
5005 set_type
= ZFS_DELEG_EVERYONE_SETS
;
5006 base_type
= ZFS_DELEG_EVERYONE
;
5008 ld
[0] = ZFS_DELEG_LOCAL
;
5010 ld
[1] = ZFS_DELEG_DESCENDENT
;
5014 assert(set_type
!= '\0' && base_type
!= '\0');
5017 if (perms
!= NULL
) {
5019 char *end
= curr
+ strlen(perms
);
5021 while (curr
< end
) {
5022 char *delim
= strchr(curr
, ',');
5033 (void) nvlist_add_boolean(nvl
, curr
);
5039 for (i
= 0; i
< 2; i
++) {
5040 char locality
= ld
[i
];
5044 if (!nvlist_empty(base_nvl
)) {
5046 (void) snprintf(who_buf
,
5047 sizeof (who_buf
), "%c%c$%s",
5048 base_type
, locality
, who
);
5050 (void) snprintf(who_buf
,
5051 sizeof (who_buf
), "%c%c$",
5052 base_type
, locality
);
5054 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5059 if (!nvlist_empty(set_nvl
)) {
5061 (void) snprintf(who_buf
,
5062 sizeof (who_buf
), "%c%c$%s",
5063 set_type
, locality
, who
);
5065 (void) snprintf(who_buf
,
5066 sizeof (who_buf
), "%c%c$",
5067 set_type
, locality
);
5069 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5074 for (i
= 0; i
< 2; i
++) {
5075 char locality
= ld
[i
];
5080 (void) snprintf(who_buf
, sizeof (who_buf
),
5081 "%c%c$%s", base_type
, locality
, who
);
5083 (void) snprintf(who_buf
, sizeof (who_buf
),
5084 "%c%c$", base_type
, locality
);
5085 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5088 (void) snprintf(who_buf
, sizeof (who_buf
),
5089 "%c%c$%s", set_type
, locality
, who
);
5091 (void) snprintf(who_buf
, sizeof (who_buf
),
5092 "%c%c$", set_type
, locality
);
5093 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5099 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
5101 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
5105 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
5106 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
5107 } else if (opts
->create
) {
5108 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
5109 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5110 } else if (opts
->everyone
) {
5111 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
5112 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5114 char *curr
= opts
->who
;
5115 char *end
= curr
+ strlen(curr
);
5117 while (curr
< end
) {
5119 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
5121 char *delim
= strchr(curr
, ',');
5124 struct passwd
*p
= NULL
;
5125 struct group
*g
= NULL
;
5133 rid
= (uid_t
)strtol(curr
, &endch
, 0);
5135 who_type
= ZFS_DELEG_USER
;
5144 (void) snprintf(errbuf
, 256, gettext(
5145 "invalid user %s"), curr
);
5146 allow_usage(un
, B_TRUE
, errbuf
);
5148 } else if (opts
->group
) {
5149 who_type
= ZFS_DELEG_GROUP
;
5158 (void) snprintf(errbuf
, 256, gettext(
5159 "invalid group %s"), curr
);
5160 allow_usage(un
, B_TRUE
, errbuf
);
5163 if (*endch
!= '\0') {
5170 if (*endch
!= '\0') {
5178 who_type
= ZFS_DELEG_USER
;
5180 } else if (g
!= NULL
) {
5181 who_type
= ZFS_DELEG_GROUP
;
5184 (void) snprintf(errbuf
, 256, gettext(
5185 "invalid user/group %s"), curr
);
5186 allow_usage(un
, B_TRUE
, errbuf
);
5190 (void) sprintf(id
, "%u", rid
);
5193 store_allow_perm(who_type
, opts
->local
,
5194 opts
->descend
, who
, opts
->perms
, *nvlp
);
5203 print_set_creat_perms(uu_avl_t
*who_avl
)
5205 const char *sc_title
[] = {
5206 gettext("Permission sets:\n"),
5207 gettext("Create time permissions:\n"),
5210 const char **title_ptr
= sc_title
;
5211 who_perm_node_t
*who_node
= NULL
;
5212 int prev_weight
= -1;
5214 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
5215 who_node
= uu_avl_next(who_avl
, who_node
)) {
5216 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5217 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5218 const char *who_name
= who_node
->who_perm
.who_name
;
5219 int weight
= who_type2weight(who_type
);
5220 boolean_t first
= B_TRUE
;
5221 deleg_perm_node_t
*deleg_node
;
5223 if (prev_weight
!= weight
) {
5224 (void) printf("%s", *title_ptr
++);
5225 prev_weight
= weight
;
5228 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
5229 (void) printf("\t");
5231 (void) printf("\t%s ", who_name
);
5233 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
5234 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5237 deleg_node
->dpn_perm
.dp_name
);
5240 (void) printf(",%s",
5241 deleg_node
->dpn_perm
.dp_name
);
5244 (void) printf("\n");
5249 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
5252 who_perm_node_t
*who_node
= NULL
;
5253 boolean_t prt_title
= B_TRUE
;
5254 uu_avl_walk_t
*walk
;
5256 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
5259 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
5260 const char *who_name
= who_node
->who_perm
.who_name
;
5261 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
5262 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5263 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5265 deleg_perm_node_t
*deleg_node
;
5266 boolean_t prt_who
= B_TRUE
;
5268 for (deleg_node
= uu_avl_first(avl
);
5270 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5271 if (local
!= deleg_node
->dpn_perm
.dp_local
||
5272 descend
!= deleg_node
->dpn_perm
.dp_descend
)
5276 const char *who
= NULL
;
5278 prt_title
= B_FALSE
;
5279 (void) printf("%s", title
);
5283 case ZFS_DELEG_USER_SETS
:
5284 case ZFS_DELEG_USER
:
5285 who
= gettext("user");
5287 who_name
= nice_who_name
;
5289 case ZFS_DELEG_GROUP_SETS
:
5290 case ZFS_DELEG_GROUP
:
5291 who
= gettext("group");
5293 who_name
= nice_who_name
;
5295 case ZFS_DELEG_EVERYONE_SETS
:
5296 case ZFS_DELEG_EVERYONE
:
5297 who
= gettext("everyone");
5302 assert(who
!= NULL
);
5306 if (who_name
== NULL
)
5307 (void) printf("\t%s", who
);
5309 (void) printf("\t%s %s", who
, who_name
);
5312 (void) printf("%c%s", delim
,
5313 deleg_node
->dpn_perm
.dp_name
);
5318 (void) printf("\n");
5321 uu_avl_walk_end(walk
);
5325 print_fs_perms(fs_perm_set_t
*fspset
)
5327 fs_perm_node_t
*node
= NULL
;
5328 char buf
[MAXNAMELEN
+ 32];
5329 const char *dsname
= buf
;
5331 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
5332 node
= uu_list_next(fspset
->fsps_list
, node
)) {
5333 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
5334 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
5337 (void) snprintf(buf
, sizeof (buf
),
5338 gettext("---- Permissions on %s "),
5339 node
->fspn_fsperm
.fsp_name
);
5340 (void) printf("%s", dsname
);
5341 left
= 70 - strlen(buf
);
5344 (void) printf("\n");
5346 print_set_creat_perms(sc_avl
);
5347 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
5348 gettext("Local permissions:\n"));
5349 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
5350 gettext("Descendent permissions:\n"));
5351 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
5352 gettext("Local+Descendent permissions:\n"));
5356 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5358 struct deleg_perms
{
5364 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5366 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5367 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5369 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5372 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5376 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5379 nvlist_t
*perm_nvl
= NULL
;
5380 nvlist_t
*update_perm_nvl
= NULL
;
5383 struct allow_opts opts
= { 0 };
5385 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5388 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5391 opts
.local
= B_TRUE
;
5394 opts
.descend
= B_TRUE
;
5400 opts
.group
= B_TRUE
;
5403 opts
.everyone
= B_TRUE
;
5409 opts
.create
= B_TRUE
;
5412 opts
.recursive
= B_TRUE
;
5415 (void) fprintf(stderr
, gettext("missing argument for "
5416 "'%c' option\n"), optopt
);
5420 opts
.prt_usage
= B_TRUE
;
5423 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5432 /* check arguments */
5433 parse_allow_args(argc
, argv
, un
, &opts
);
5435 /* try to open the dataset */
5436 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5437 ZFS_TYPE_VOLUME
)) == NULL
) {
5438 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5443 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5446 fs_perm_set_init(&fs_perm_set
);
5447 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5448 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5453 print_fs_perms(&fs_perm_set
);
5455 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5456 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5459 if (un
&& opts
.recursive
) {
5460 struct deleg_perms data
= { un
, update_perm_nvl
};
5461 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5470 nvlist_free(perm_nvl
);
5471 nvlist_free(update_perm_nvl
);
5473 fs_perm_set_fini(&fs_perm_set
);
5481 zfs_do_allow(int argc
, char **argv
)
5483 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5487 zfs_do_unallow(int argc
, char **argv
)
5489 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5493 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5498 boolean_t recursive
= B_FALSE
;
5499 const char *opts
= holding
? "rt" : "r";
5503 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5509 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5518 /* check number of arguments */
5526 if (holding
&& tag
[0] == '.') {
5527 /* tags starting with '.' are reserved for libzfs */
5528 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5532 for (i
= 0; i
< argc
; ++i
) {
5534 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
5536 char *path
= argv
[i
];
5538 delim
= strchr(path
, '@');
5539 if (delim
== NULL
) {
5540 (void) fprintf(stderr
,
5541 gettext("'%s' is not a snapshot\n"), path
);
5545 (void) strncpy(parent
, path
, delim
- path
);
5546 parent
[delim
- path
] = '\0';
5548 zhp
= zfs_open(g_zfs
, parent
,
5549 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5555 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
5558 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5564 return (errors
!= 0);
5568 * zfs hold [-r] [-t] <tag> <snap> ...
5570 * -r Recursively hold
5572 * Apply a user-hold with the given tag to the list of snapshots.
5575 zfs_do_hold(int argc
, char **argv
)
5577 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5581 * zfs release [-r] <tag> <snap> ...
5583 * -r Recursively release
5585 * Release a user-hold with the given tag from the list of snapshots.
5588 zfs_do_release(int argc
, char **argv
)
5590 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5593 typedef struct holds_cbdata
{
5594 boolean_t cb_recursive
;
5595 const char *cb_snapname
;
5597 size_t cb_max_namelen
;
5598 size_t cb_max_taglen
;
5601 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5602 #define DATETIME_BUF_LEN (32)
5607 print_holds(boolean_t scripted
, int nwidth
, int tagwidth
, nvlist_t
*nvl
)
5610 nvpair_t
*nvp
= NULL
;
5611 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5615 for (i
= 0; i
< 3; i
++) {
5616 col
= gettext(hdr_cols
[i
]);
5618 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5621 (void) printf("%s\n", col
);
5625 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5626 char *zname
= nvpair_name(nvp
);
5628 nvpair_t
*nvp2
= NULL
;
5629 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5630 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5631 char tsbuf
[DATETIME_BUF_LEN
];
5632 char *tagname
= nvpair_name(nvp2
);
5636 char sep
= scripted
? '\t' : ' ';
5637 int sepnum
= scripted
? 1 : 2;
5639 (void) nvpair_value_uint64(nvp2
, &val
);
5641 (void) localtime_r(&time
, &t
);
5642 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5643 gettext(STRFTIME_FMT_STR
), &t
);
5645 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth
, zname
,
5646 sepnum
, sep
, tagwidth
, tagname
, sepnum
, sep
, tsbuf
);
5652 * Generic callback function to list a dataset or snapshot.
5655 holds_callback(zfs_handle_t
*zhp
, void *data
)
5657 holds_cbdata_t
*cbp
= data
;
5658 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5659 nvlist_t
*nvl
= NULL
;
5660 nvpair_t
*nvp
= NULL
;
5661 const char *zname
= zfs_get_name(zhp
);
5662 size_t znamelen
= strlen(zname
);
5664 if (cbp
->cb_recursive
) {
5665 const char *snapname
;
5666 char *delim
= strchr(zname
, '@');
5670 snapname
= delim
+ 1;
5671 if (strcmp(cbp
->cb_snapname
, snapname
))
5675 if (zfs_get_holds(zhp
, &nvl
) != 0)
5678 if (znamelen
> cbp
->cb_max_namelen
)
5679 cbp
->cb_max_namelen
= znamelen
;
5681 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5682 const char *tag
= nvpair_name(nvp
);
5683 size_t taglen
= strlen(tag
);
5684 if (taglen
> cbp
->cb_max_taglen
)
5685 cbp
->cb_max_taglen
= taglen
;
5688 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5692 * zfs holds [-r] <snap> ...
5694 * -r Recursively hold
5697 zfs_do_holds(int argc
, char **argv
)
5702 boolean_t scripted
= B_FALSE
;
5703 boolean_t recursive
= B_FALSE
;
5704 const char *opts
= "rH";
5707 int types
= ZFS_TYPE_SNAPSHOT
;
5708 holds_cbdata_t cb
= { 0 };
5715 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5724 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5731 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
5732 flags
|= ZFS_ITER_RECURSE
;
5738 /* check number of arguments */
5742 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5745 for (i
= 0; i
< argc
; ++i
) {
5746 char *snapshot
= argv
[i
];
5748 const char *snapname
;
5750 delim
= strchr(snapshot
, '@');
5751 if (delim
== NULL
) {
5752 (void) fprintf(stderr
,
5753 gettext("'%s' is not a snapshot\n"), snapshot
);
5757 snapname
= delim
+ 1;
5759 snapshot
[delim
- snapshot
] = '\0';
5761 cb
.cb_recursive
= recursive
;
5762 cb
.cb_snapname
= snapname
;
5766 * 1. collect holds data, set format options
5768 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
5769 holds_callback
, &cb
);
5775 * 2. print holds data
5777 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
5779 if (nvlist_empty(nvl
))
5780 (void) fprintf(stderr
, gettext("no datasets available\n"));
5784 return (0 != errors
);
5787 #define CHECK_SPINNER 30
5788 #define SPINNER_TIME 3 /* seconds */
5789 #define MOUNT_TIME 5 /* seconds */
5792 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
5794 static char *spin
[] = { "-", "\\", "|", "/" };
5795 static int spinval
= 0;
5796 static int spincheck
= 0;
5797 static time_t last_spin_time
= (time_t)0;
5798 get_all_cb_t
*cbp
= data
;
5799 zfs_type_t type
= zfs_get_type(zhp
);
5801 if (cbp
->cb_verbose
) {
5802 if (--spincheck
< 0) {
5803 time_t now
= time(NULL
);
5804 if (last_spin_time
+ SPINNER_TIME
< now
) {
5805 update_progress(spin
[spinval
++ % 4]);
5806 last_spin_time
= now
;
5808 spincheck
= CHECK_SPINNER
;
5813 * Iterate over any nested datasets.
5815 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
5821 * Skip any datasets whose type does not match.
5823 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
5827 libzfs_add_handle(cbp
, zhp
);
5828 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
5834 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
5836 get_all_cb_t cb
= { 0 };
5837 cb
.cb_verbose
= verbose
;
5838 cb
.cb_getone
= get_one_dataset
;
5841 set_progress_header(gettext("Reading ZFS config"));
5842 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
5844 *dslist
= cb
.cb_handles
;
5845 *count
= cb
.cb_used
;
5848 finish_progress(gettext("done."));
5852 * Generic callback for sharing or mounting filesystems. Because the code is so
5853 * similar, we have a common function with an extra parameter to determine which
5854 * mode we are using.
5856 #define OP_SHARE 0x1
5857 #define OP_MOUNT 0x2
5860 * Share or mount a dataset.
5863 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
5864 boolean_t
explicit, const char *options
)
5866 char mountpoint
[ZFS_MAXPROPLEN
];
5867 char shareopts
[ZFS_MAXPROPLEN
];
5868 char smbshareopts
[ZFS_MAXPROPLEN
];
5869 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
5871 uint64_t zoned
, canmount
;
5872 boolean_t shared_nfs
, shared_smb
;
5874 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
5877 * Check to make sure we can mount/share this dataset. If we
5878 * are in the global zone and the filesystem is exported to a
5879 * local zone, or if we are in a local zone and the
5880 * filesystem is not exported, then it is an error.
5882 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
5884 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
5888 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5889 "dataset is exported to a local zone\n"), cmdname
,
5893 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
5897 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5898 "permission denied\n"), cmdname
,
5904 * Ignore any filesystems which don't apply to us. This
5905 * includes those with a legacy mountpoint, or those with
5906 * legacy share options.
5908 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
5909 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
5910 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
5911 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5912 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
5913 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5915 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
5916 strcmp(smbshareopts
, "off") == 0) {
5920 (void) fprintf(stderr
, gettext("cannot share '%s': "
5921 "legacy share\n"), zfs_get_name(zhp
));
5922 (void) fprintf(stderr
, gettext("use share(1M) to "
5923 "share this filesystem, or set "
5924 "sharenfs property on\n"));
5929 * We cannot share or mount legacy filesystems. If the
5930 * shareopts is non-legacy but the mountpoint is legacy, we
5931 * treat it as a legacy share.
5933 if (strcmp(mountpoint
, "legacy") == 0) {
5937 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5938 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
5939 (void) fprintf(stderr
, gettext("use %s(1M) to "
5940 "%s this filesystem\n"), cmdname
, cmdname
);
5944 if (strcmp(mountpoint
, "none") == 0) {
5948 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
5949 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
5954 * canmount explicit outcome
5955 * on no pass through
5956 * on yes pass through
5958 * off yes display error, return 1
5959 * noauto no return 0
5960 * noauto yes pass through
5962 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
5963 if (canmount
== ZFS_CANMOUNT_OFF
) {
5967 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5968 "'canmount' property is set to 'off'\n"), cmdname
,
5971 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
5976 * If this filesystem is inconsistent and has a receive resume
5977 * token, we can not mount it.
5979 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
5980 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
5981 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
5985 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5986 "Contains partially-completed state from "
5987 "\"zfs receive -r\", which can be resumed with "
5988 "\"zfs send -t\"\n"),
5989 cmdname
, zfs_get_name(zhp
));
5994 * At this point, we have verified that the mountpoint and/or
5995 * shareopts are appropriate for auto management. If the
5996 * filesystem is already mounted or shared, return (failing
5997 * for explicit requests); otherwise mount or share the
6003 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
6004 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
6006 if ((shared_nfs
&& shared_smb
) ||
6007 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
6008 strcmp(smbshareopts
, "off") == 0) ||
6009 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
6010 strcmp(shareopts
, "off") == 0)) {
6014 (void) fprintf(stderr
, gettext("cannot share "
6015 "'%s': filesystem already shared\n"),
6020 if (!zfs_is_mounted(zhp
, NULL
) &&
6021 zfs_mount(zhp
, NULL
, 0) != 0)
6024 if (protocol
== NULL
) {
6025 if (zfs_shareall(zhp
) != 0)
6027 } else if (strcmp(protocol
, "nfs") == 0) {
6028 if (zfs_share_nfs(zhp
))
6030 } else if (strcmp(protocol
, "smb") == 0) {
6031 if (zfs_share_smb(zhp
))
6034 (void) fprintf(stderr
, gettext("cannot share "
6035 "'%s': invalid share type '%s' "
6037 zfs_get_name(zhp
), protocol
);
6044 if (options
== NULL
)
6045 mnt
.mnt_mntopts
= "";
6047 mnt
.mnt_mntopts
= (char *)options
;
6049 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
6050 zfs_is_mounted(zhp
, NULL
)) {
6054 (void) fprintf(stderr
, gettext("cannot mount "
6055 "'%s': filesystem already mounted\n"),
6060 if (zfs_mount(zhp
, options
, flags
) != 0)
6069 * Reports progress in the form "(current/total)". Not thread-safe.
6072 report_mount_progress(int current
, int total
)
6074 static time_t last_progress_time
= 0;
6075 time_t now
= time(NULL
);
6078 /* report 1..n instead of 0..n-1 */
6081 /* display header if we're here for the first time */
6083 set_progress_header(gettext("Mounting ZFS filesystems"));
6084 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
6085 /* too soon to report again */
6089 last_progress_time
= now
;
6091 (void) sprintf(info
, "(%d/%d)", current
, total
);
6093 if (current
== total
)
6094 finish_progress(info
);
6096 update_progress(info
);
6100 append_options(char *mntopts
, char *newopts
)
6102 int len
= strlen(mntopts
);
6104 /* original length plus new string to append plus 1 for the comma */
6105 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
6106 (void) fprintf(stderr
, gettext("the opts argument for "
6107 "'%s' option is too long (more than %d chars)\n"),
6108 "-o", MNT_LINE_MAX
);
6113 mntopts
[len
++] = ',';
6115 (void) strcpy(&mntopts
[len
], newopts
);
6119 share_mount(int op
, int argc
, char **argv
)
6122 boolean_t verbose
= B_FALSE
;
6124 char *options
= NULL
;
6128 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
6138 if (*optarg
== '\0') {
6139 (void) fprintf(stderr
, gettext("empty mount "
6140 "options (-o) specified\n"));
6144 if (options
== NULL
)
6145 options
= safe_malloc(MNT_LINE_MAX
+ 1);
6147 /* option validation is done later */
6148 append_options(options
, optarg
);
6151 flags
|= MS_OVERLAY
;
6154 (void) fprintf(stderr
, gettext("missing argument for "
6155 "'%c' option\n"), optopt
);
6159 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6168 /* check number of arguments */
6170 zfs_handle_t
**dslist
= NULL
;
6171 size_t i
, count
= 0;
6172 char *protocol
= NULL
;
6174 if (op
== OP_SHARE
&& argc
> 0) {
6175 if (strcmp(argv
[0], "nfs") != 0 &&
6176 strcmp(argv
[0], "smb") != 0) {
6177 (void) fprintf(stderr
, gettext("share type "
6178 "must be 'nfs' or 'smb'\n"));
6187 (void) fprintf(stderr
, gettext("too many arguments\n"));
6191 start_progress_timer();
6192 get_all_datasets(&dslist
, &count
, verbose
);
6195 if (options
!= NULL
)
6200 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
6202 for (i
= 0; i
< count
; i
++) {
6204 report_mount_progress(i
, count
);
6206 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
6207 B_FALSE
, options
) != 0)
6209 zfs_close(dslist
[i
]);
6213 } else if (argc
== 0) {
6214 struct mnttab entry
;
6216 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
6217 (void) fprintf(stderr
, gettext("missing filesystem "
6218 "argument (specify -a for all)\n"));
6223 * When mount is given no arguments, go through
6224 * /proc/self/mounts and display any active ZFS mounts.
6225 * We hide any snapshots, since they are controlled
6229 /* Reopen MNTTAB to prevent reading stale data from open file */
6230 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
) {
6231 if (options
!= NULL
)
6236 while (getmntent(mnttab_file
, &entry
) == 0) {
6237 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
6238 strchr(entry
.mnt_special
, '@') != NULL
)
6241 (void) printf("%-30s %s\n", entry
.mnt_special
,
6249 (void) fprintf(stderr
,
6250 gettext("too many arguments\n"));
6254 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6255 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6258 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
6264 if (options
!= NULL
)
6271 * zfs mount -a [nfs]
6272 * zfs mount filesystem
6274 * Mount all filesystems, or mount the given filesystem.
6277 zfs_do_mount(int argc
, char **argv
)
6279 return (share_mount(OP_MOUNT
, argc
, argv
));
6283 * zfs share -a [nfs | smb]
6284 * zfs share filesystem
6286 * Share all filesystems, or share the given filesystem.
6289 zfs_do_share(int argc
, char **argv
)
6291 return (share_mount(OP_SHARE
, argc
, argv
));
6294 typedef struct unshare_unmount_node
{
6295 zfs_handle_t
*un_zhp
;
6297 uu_avl_node_t un_avlnode
;
6298 } unshare_unmount_node_t
;
6302 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
6304 const unshare_unmount_node_t
*l
= larg
;
6305 const unshare_unmount_node_t
*r
= rarg
;
6307 return (strcmp(l
->un_mountp
, r
->un_mountp
));
6311 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6312 * absolute path, find the entry /proc/self/mounts, verify that its a
6313 * ZFS filesystems, and unmount it appropriately.
6316 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
6320 struct stat64 statbuf
;
6321 struct extmnttab entry
;
6322 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
6326 * Search for the path in /proc/self/mounts. Rather than looking for the
6327 * specific path, which can be fooled by non-standard paths (i.e. ".."
6328 * or "//"), we stat() the path and search for the corresponding
6329 * (major,minor) device pair.
6331 if (stat64(path
, &statbuf
) != 0) {
6332 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6333 cmdname
, path
, strerror(errno
));
6336 path_inode
= statbuf
.st_ino
;
6339 * Search for the given (major,minor) pair in the mount table.
6342 /* Reopen MNTTAB to prevent reading stale data from open file */
6343 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6346 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
6347 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
6348 entry
.mnt_minor
== minor(statbuf
.st_dev
))
6352 if (op
== OP_SHARE
) {
6353 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
6354 "currently mounted\n"), cmdname
, path
);
6357 (void) fprintf(stderr
, gettext("warning: %s not in"
6358 "/proc/self/mounts\n"), path
);
6359 if ((ret
= umount2(path
, flags
)) != 0)
6360 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
6365 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
6366 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
6367 "filesystem\n"), cmdname
, path
);
6371 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6372 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6376 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
6377 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6378 cmdname
, path
, strerror(errno
));
6380 } else if (statbuf
.st_ino
!= path_inode
) {
6381 (void) fprintf(stderr
, gettext("cannot "
6382 "%s '%s': not a mountpoint\n"), cmdname
, path
);
6386 if (op
== OP_SHARE
) {
6387 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6388 char smbshare_prop
[ZFS_MAXPROPLEN
];
6390 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
6391 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6392 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
6393 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6395 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6396 strcmp(smbshare_prop
, "off") == 0) {
6397 (void) fprintf(stderr
, gettext("cannot unshare "
6398 "'%s': legacy share\n"), path
);
6399 (void) fprintf(stderr
, gettext("use exportfs(8) "
6400 "or smbcontrol(1) to unshare this filesystem\n"));
6401 } else if (!zfs_is_shared(zhp
)) {
6402 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6403 "not currently shared\n"), path
);
6405 ret
= zfs_unshareall_bypath(zhp
, path
);
6408 char mtpt_prop
[ZFS_MAXPROPLEN
];
6410 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6411 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6414 ret
= zfs_unmount(zhp
, NULL
, flags
);
6415 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6416 (void) fprintf(stderr
, gettext("cannot unmount "
6417 "'%s': legacy mountpoint\n"),
6419 (void) fprintf(stderr
, gettext("use umount(8) "
6420 "to unmount this filesystem\n"));
6422 ret
= zfs_unmountall(zhp
, flags
);
6433 * Generic callback for unsharing or unmounting a filesystem.
6436 unshare_unmount(int op
, int argc
, char **argv
)
6443 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6444 char sharesmb
[ZFS_MAXPROPLEN
];
6447 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? ":a" : "af")) != -1) {
6456 (void) fprintf(stderr
, gettext("missing argument for "
6457 "'%c' option\n"), optopt
);
6461 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6472 * We could make use of zfs_for_each() to walk all datasets in
6473 * the system, but this would be very inefficient, especially
6474 * since we would have to linearly search /proc/self/mounts for
6475 * each one. Instead, do one pass through /proc/self/mounts
6476 * looking for zfs entries and call zfs_unmount() for each one.
6478 * Things get a little tricky if the administrator has created
6479 * mountpoints beneath other ZFS filesystems. In this case, we
6480 * have to unmount the deepest filesystems first. To accomplish
6481 * this, we place all the mountpoints in an AVL tree sorted by
6482 * the special type (dataset name), and walk the result in
6483 * reverse to make sure to get any snapshots first.
6485 struct mnttab entry
;
6486 uu_avl_pool_t
*pool
;
6487 uu_avl_t
*tree
= NULL
;
6488 unshare_unmount_node_t
*node
;
6490 uu_avl_walk_t
*walk
;
6491 char *protocol
= NULL
;
6493 if (op
== OP_SHARE
&& argc
> 0) {
6494 if (strcmp(argv
[0], "nfs") != 0 &&
6495 strcmp(argv
[0], "smb") != 0) {
6496 (void) fprintf(stderr
, gettext("share type "
6497 "must be 'nfs' or 'smb'\n"));
6506 (void) fprintf(stderr
, gettext("too many arguments\n"));
6510 if (((pool
= uu_avl_pool_create("unmount_pool",
6511 sizeof (unshare_unmount_node_t
),
6512 offsetof(unshare_unmount_node_t
, un_avlnode
),
6513 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6514 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6517 /* Reopen MNTTAB to prevent reading stale data from open file */
6518 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6521 while (getmntent(mnttab_file
, &entry
) == 0) {
6523 /* ignore non-ZFS entries */
6524 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6527 /* ignore snapshots */
6528 if (strchr(entry
.mnt_special
, '@') != NULL
)
6531 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6532 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6539 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6541 sizeof (nfs_mnt_prop
),
6542 NULL
, NULL
, 0, B_FALSE
) == 0);
6543 if (strcmp(nfs_mnt_prop
, "off") != 0)
6545 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6547 sizeof (nfs_mnt_prop
),
6548 NULL
, NULL
, 0, B_FALSE
) == 0);
6549 if (strcmp(nfs_mnt_prop
, "off") == 0)
6553 /* Ignore legacy mounts */
6554 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6556 sizeof (nfs_mnt_prop
),
6557 NULL
, NULL
, 0, B_FALSE
) == 0);
6558 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6560 /* Ignore canmount=noauto mounts */
6561 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6562 ZFS_CANMOUNT_NOAUTO
)
6568 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6570 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6572 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6574 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6575 uu_avl_insert(tree
, node
, idx
);
6577 zfs_close(node
->un_zhp
);
6578 free(node
->un_mountp
);
6584 * Walk the AVL tree in reverse, unmounting each filesystem and
6585 * removing it from the AVL tree in the process.
6587 if ((walk
= uu_avl_walk_start(tree
,
6588 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6591 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6592 uu_avl_remove(tree
, node
);
6596 if (zfs_unshareall_bytype(node
->un_zhp
,
6597 node
->un_mountp
, protocol
) != 0)
6602 if (zfs_unmount(node
->un_zhp
,
6603 node
->un_mountp
, flags
) != 0)
6608 zfs_close(node
->un_zhp
);
6609 free(node
->un_mountp
);
6613 uu_avl_walk_end(walk
);
6614 uu_avl_destroy(tree
);
6615 uu_avl_pool_destroy(pool
);
6620 (void) fprintf(stderr
,
6621 gettext("missing filesystem argument\n"));
6623 (void) fprintf(stderr
,
6624 gettext("too many arguments\n"));
6629 * We have an argument, but it may be a full path or a ZFS
6630 * filesystem. Pass full paths off to unmount_path() (shared by
6631 * manual_unmount), otherwise open the filesystem and pass to
6634 if (argv
[0][0] == '/')
6635 return (unshare_unmount_path(op
, argv
[0],
6638 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6639 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6642 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
6643 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
6644 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
6645 NULL
, 0, B_FALSE
) == 0);
6649 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6651 sizeof (nfs_mnt_prop
),
6652 NULL
, NULL
, 0, B_FALSE
) == 0);
6653 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6654 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
6657 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6658 strcmp(sharesmb
, "off") == 0) {
6659 (void) fprintf(stderr
, gettext("cannot "
6660 "unshare '%s': legacy share\n"),
6662 (void) fprintf(stderr
, gettext("use "
6663 "unshare(1M) to unshare this "
6666 } else if (!zfs_is_shared(zhp
)) {
6667 (void) fprintf(stderr
, gettext("cannot "
6668 "unshare '%s': not currently "
6669 "shared\n"), zfs_get_name(zhp
));
6671 } else if (zfs_unshareall(zhp
) != 0) {
6677 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
6678 (void) fprintf(stderr
, gettext("cannot "
6679 "unmount '%s': legacy "
6680 "mountpoint\n"), zfs_get_name(zhp
));
6681 (void) fprintf(stderr
, gettext("use "
6682 "umount(1M) to unmount this "
6685 } else if (!zfs_is_mounted(zhp
, NULL
)) {
6686 (void) fprintf(stderr
, gettext("cannot "
6687 "unmount '%s': not currently "
6691 } else if (zfs_unmountall(zhp
, flags
) != 0) {
6705 * zfs unmount filesystem
6707 * Unmount all filesystems, or a specific ZFS filesystem.
6710 zfs_do_unmount(int argc
, char **argv
)
6712 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
6717 * zfs unshare filesystem
6719 * Unshare all filesystems, or a specific ZFS filesystem.
6722 zfs_do_unshare(int argc
, char **argv
)
6724 return (unshare_unmount(OP_SHARE
, argc
, argv
));
6728 find_command_idx(char *command
, int *idx
)
6732 for (i
= 0; i
< NCOMMAND
; i
++) {
6733 if (command_table
[i
].name
== NULL
)
6736 if (strcmp(command
, command_table
[i
].name
) == 0) {
6745 zfs_do_diff(int argc
, char **argv
)
6749 char *tosnap
= NULL
;
6750 char *fromsnap
= NULL
;
6755 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
6758 flags
|= ZFS_DIFF_CLASSIFY
;
6761 flags
|= ZFS_DIFF_PARSEABLE
;
6764 flags
|= ZFS_DIFF_TIMESTAMP
;
6767 (void) fprintf(stderr
,
6768 gettext("invalid option '%c'\n"), optopt
);
6777 (void) fprintf(stderr
,
6778 gettext("must provide at least one snapshot name\n"));
6783 (void) fprintf(stderr
, gettext("too many arguments\n"));
6788 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
6791 if (*fromsnap
!= '@')
6792 copy
= strdup(fromsnap
);
6794 copy
= strdup(tosnap
);
6798 if ((atp
= strchr(copy
, '@')) != NULL
)
6801 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6808 * Ignore SIGPIPE so that the library can give us
6809 * information on any failure
6811 (void) sigignore(SIGPIPE
);
6813 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
6821 * zfs bookmark <fs@snap> <fs#bmark>
6823 * Creates a bookmark with the given name from the given snapshot.
6826 zfs_do_bookmark(int argc
, char **argv
)
6828 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
6835 while ((c
= getopt(argc
, argv
, "")) != -1) {
6838 (void) fprintf(stderr
,
6839 gettext("invalid option '%c'\n"), optopt
);
6847 /* check number of arguments */
6849 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
6853 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
6857 if (strchr(argv
[1], '#') == NULL
) {
6858 (void) fprintf(stderr
,
6859 gettext("invalid bookmark name '%s' -- "
6860 "must contain a '#'\n"), argv
[1]);
6864 if (argv
[0][0] == '@') {
6866 * Snapshot name begins with @.
6867 * Default to same fs as bookmark.
6869 (void) strlcpy(snapname
, argv
[1], sizeof (snapname
));
6870 *strchr(snapname
, '#') = '\0';
6871 (void) strlcat(snapname
, argv
[0], sizeof (snapname
));
6873 (void) strlcpy(snapname
, argv
[0], sizeof (snapname
));
6875 zhp
= zfs_open(g_zfs
, snapname
, ZFS_TYPE_SNAPSHOT
);
6881 nvl
= fnvlist_alloc();
6882 fnvlist_add_string(nvl
, argv
[1], snapname
);
6883 ret
= lzc_bookmark(nvl
, NULL
);
6887 const char *err_msg
;
6890 (void) snprintf(errbuf
, sizeof (errbuf
),
6891 dgettext(TEXT_DOMAIN
,
6892 "cannot create bookmark '%s'"), argv
[1]);
6896 err_msg
= "bookmark is in a different pool";
6899 err_msg
= "bookmark exists";
6902 err_msg
= "invalid argument";
6905 err_msg
= "bookmark feature not enabled";
6908 err_msg
= "out of space";
6911 err_msg
= "unknown error";
6914 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
6915 dgettext(TEXT_DOMAIN
, err_msg
));
6926 main(int argc
, char **argv
)
6932 (void) setlocale(LC_ALL
, "");
6933 (void) textdomain(TEXT_DOMAIN
);
6935 dprintf_setup(&argc
, argv
);
6940 * Make sure the user has specified some command.
6943 (void) fprintf(stderr
, gettext("missing command\n"));
6950 * The 'umount' command is an alias for 'unmount'
6952 if (strcmp(cmdname
, "umount") == 0)
6953 cmdname
= "unmount";
6956 * The 'recv' command is an alias for 'receive'
6958 if (strcmp(cmdname
, "recv") == 0)
6959 cmdname
= "receive";
6962 * The 'snap' command is an alias for 'snapshot'
6964 if (strcmp(cmdname
, "snap") == 0)
6965 cmdname
= "snapshot";
6970 if ((strcmp(cmdname
, "-?") == 0) ||
6971 (strcmp(cmdname
, "--help") == 0))
6974 if ((g_zfs
= libzfs_init()) == NULL
) {
6975 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
6979 mnttab_file
= g_zfs
->libzfs_mnttab
;
6981 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
6983 libzfs_print_on_error(g_zfs
, B_TRUE
);
6986 * Run the appropriate command.
6988 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
6989 if (find_command_idx(cmdname
, &i
) == 0) {
6990 current_command
= &command_table
[i
];
6991 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
6992 } else if (strchr(cmdname
, '=') != NULL
) {
6993 verify(find_command_idx("set", &i
) == 0);
6994 current_command
= &command_table
[i
];
6995 ret
= command_table
[i
].func(argc
, argv
);
6997 (void) fprintf(stderr
, gettext("unrecognized "
6998 "command '%s'\n"), cmdname
);
7003 if (ret
== 0 && log_history
)
7004 (void) zpool_log_history(g_zfs
, history_str
);
7009 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7010 * for the purposes of running ::findleaks.
7012 if (getenv("ZFS_ABORT") != NULL
) {
7013 (void) printf("dumping core by request\n");