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) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28 * Copyright (c) 2013 Steven Hartland. All rights reserved.
29 * Copyright (c) 2014 Integros [integros.com]
30 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
31 * Copyright 2016 Nexenta Systems, Inc.
41 #include <libnvpair.h>
54 #include <sys/mkdev.h>
55 #include <sys/mntent.h>
56 #include <sys/mnttab.h>
57 #include <sys/mount.h>
59 #include <sys/fs/zfs.h>
60 #include <sys/types.h>
64 #include <libzfs_core.h>
66 #include <zfs_deleg.h>
69 #include <directory.h>
75 #include "zfs_comutil.h"
77 libzfs_handle_t
*g_zfs
;
79 static FILE *mnttab_file
;
80 static char history_str
[HIS_MAX_RECORD_LEN
];
81 static boolean_t log_history
= B_TRUE
;
83 static int zfs_do_clone(int argc
, char **argv
);
84 static int zfs_do_create(int argc
, char **argv
);
85 static int zfs_do_destroy(int argc
, char **argv
);
86 static int zfs_do_get(int argc
, char **argv
);
87 static int zfs_do_inherit(int argc
, char **argv
);
88 static int zfs_do_list(int argc
, char **argv
);
89 static int zfs_do_mount(int argc
, char **argv
);
90 static int zfs_do_rename(int argc
, char **argv
);
91 static int zfs_do_rollback(int argc
, char **argv
);
92 static int zfs_do_set(int argc
, char **argv
);
93 static int zfs_do_upgrade(int argc
, char **argv
);
94 static int zfs_do_snapshot(int argc
, char **argv
);
95 static int zfs_do_unmount(int argc
, char **argv
);
96 static int zfs_do_share(int argc
, char **argv
);
97 static int zfs_do_unshare(int argc
, char **argv
);
98 static int zfs_do_send(int argc
, char **argv
);
99 static int zfs_do_receive(int argc
, char **argv
);
100 static int zfs_do_promote(int argc
, char **argv
);
101 static int zfs_do_userspace(int argc
, char **argv
);
102 static int zfs_do_allow(int argc
, char **argv
);
103 static int zfs_do_unallow(int argc
, char **argv
);
104 static int zfs_do_hold(int argc
, char **argv
);
105 static int zfs_do_holds(int argc
, char **argv
);
106 static int zfs_do_release(int argc
, char **argv
);
107 static int zfs_do_diff(int argc
, char **argv
);
108 static int zfs_do_bookmark(int argc
, char **argv
);
111 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
116 _umem_debug_init(void)
118 return ("default,verbose"); /* $UMEM_DEBUG setting */
122 _umem_logging_init(void)
124 return ("fail,contents"); /* $UMEM_LOGGING setting */
158 typedef struct zfs_command
{
160 int (*func
)(int argc
, char **argv
);
165 * Master command table. Each ZFS command has a name, associated function, and
166 * usage message. The usage messages need to be internationalized, so we have
167 * to have a function to return the usage message based on a command index.
169 * These commands are organized according to how they are displayed in the usage
170 * message. An empty command (one with a NULL name) indicates an empty line in
171 * the generic usage message.
173 static zfs_command_t command_table
[] = {
174 { "create", zfs_do_create
, HELP_CREATE
},
175 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
177 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
178 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
179 { "clone", zfs_do_clone
, HELP_CLONE
},
180 { "promote", zfs_do_promote
, HELP_PROMOTE
},
181 { "rename", zfs_do_rename
, HELP_RENAME
},
182 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
184 { "list", zfs_do_list
, HELP_LIST
},
186 { "set", zfs_do_set
, HELP_SET
},
187 { "get", zfs_do_get
, HELP_GET
},
188 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
189 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
190 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
191 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
193 { "mount", zfs_do_mount
, HELP_MOUNT
},
194 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
195 { "share", zfs_do_share
, HELP_SHARE
},
196 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
198 { "send", zfs_do_send
, HELP_SEND
},
199 { "receive", zfs_do_receive
, HELP_RECEIVE
},
201 { "allow", zfs_do_allow
, HELP_ALLOW
},
203 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
205 { "hold", zfs_do_hold
, HELP_HOLD
},
206 { "holds", zfs_do_holds
, HELP_HOLDS
},
207 { "release", zfs_do_release
, HELP_RELEASE
},
208 { "diff", zfs_do_diff
, HELP_DIFF
},
211 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
213 zfs_command_t
*current_command
;
216 get_usage(zfs_help_t idx
)
220 return (gettext("\tclone [-p] [-o property=value] ... "
221 "<snapshot> <filesystem|volume>\n"));
223 return (gettext("\tcreate [-p] [-o property=value] ... "
225 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
226 "-V <size> <volume>\n"));
228 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
229 "\tdestroy [-dnpRrv] "
230 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
231 "\tdestroy <filesystem|volume>#<bookmark>\n"));
233 return (gettext("\tget [-rHp] [-d max] "
234 "[-o \"all\" | field[,...]]\n"
235 "\t [-t type[,...]] [-s source[,...]]\n"
236 "\t <\"all\" | property[,...]> "
237 "[filesystem|volume|snapshot|bookmark] ...\n"));
239 return (gettext("\tinherit [-rS] <property> "
240 "<filesystem|volume|snapshot> ...\n"));
242 return (gettext("\tupgrade [-v]\n"
243 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
245 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
246 "[-s property]...\n\t [-S property]... [-t type[,...]] "
247 "[filesystem|volume|snapshot] ...\n"));
249 return (gettext("\tmount\n"
250 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
252 return (gettext("\tpromote <clone-filesystem>\n"));
254 return (gettext("\treceive [-vnsFu] <filesystem|volume|"
256 "\treceive [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
258 "\treceive -A <filesystem|volume>\n"));
260 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
261 "<filesystem|volume|snapshot>\n"
262 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
263 "\trename -r <snapshot> <snapshot>\n"));
265 return (gettext("\trollback [-rRf] <snapshot>\n"));
267 return (gettext("\tsend [-DnPpRvLec] [-[iI] snapshot] "
269 "\tsend [-Le] [-i snapshot|bookmark] "
270 "<filesystem|volume|snapshot>\n"
271 "\tsend [-nvPe] -t <receive_resume_token>\n"));
273 return (gettext("\tset <property=value> ... "
274 "<filesystem|volume|snapshot> ...\n"));
276 return (gettext("\tshare <-a | filesystem>\n"));
278 return (gettext("\tsnapshot [-r] [-o property=value] ... "
279 "<filesystem|volume>@<snap> ...\n"));
281 return (gettext("\tunmount [-f] "
282 "<-a | filesystem|mountpoint>\n"));
284 return (gettext("\tunshare "
285 "<-a | filesystem|mountpoint>\n"));
287 return (gettext("\tallow <filesystem|volume>\n"
289 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
290 "\t <filesystem|volume>\n"
291 "\tallow [-ld] -e <perm|@setname>[,...] "
292 "<filesystem|volume>\n"
293 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
294 "\tallow -s @setname <perm|@setname>[,...] "
295 "<filesystem|volume>\n"));
297 return (gettext("\tunallow [-rldug] "
298 "<\"everyone\"|user|group>[,...]\n"
299 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
300 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
301 "<filesystem|volume>\n"
302 "\tunallow [-r] -c [<perm|@setname>[,...]] "
303 "<filesystem|volume>\n"
304 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
305 "<filesystem|volume>\n"));
307 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
309 "\t [-S field] ... [-t type[,...]] "
310 "<filesystem|snapshot>\n"));
311 case HELP_GROUPSPACE
:
312 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
314 "\t [-S field] ... [-t type[,...]] "
315 "<filesystem|snapshot>\n"));
317 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
319 return (gettext("\tholds [-r] <snapshot> ...\n"));
321 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
323 return (gettext("\tdiff [-FHt] <snapshot> "
324 "[snapshot|filesystem]\n"));
326 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
336 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
341 * Utility function to guarantee malloc() success.
345 safe_malloc(size_t size
)
349 if ((data
= calloc(1, size
)) == NULL
)
356 safe_strdup(char *str
)
358 char *dupstr
= strdup(str
);
367 * Callback routine that will print out information for each of
371 usage_prop_cb(int prop
, void *cb
)
375 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
377 if (zfs_prop_readonly(prop
))
378 (void) fprintf(fp
, " NO ");
380 (void) fprintf(fp
, "YES ");
382 if (zfs_prop_inheritable(prop
))
383 (void) fprintf(fp
, " YES ");
385 (void) fprintf(fp
, " NO ");
387 if (zfs_prop_values(prop
) == NULL
)
388 (void) fprintf(fp
, "-\n");
390 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
396 * Display usage message. If we're inside a command, display only the usage for
397 * that command. Otherwise, iterate over the entire command table and display
398 * a complete usage message.
401 usage(boolean_t requested
)
404 boolean_t show_properties
= B_FALSE
;
405 FILE *fp
= requested
? stdout
: stderr
;
407 if (current_command
== NULL
) {
409 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
411 gettext("where 'command' is one of the following:\n\n"));
413 for (i
= 0; i
< NCOMMAND
; i
++) {
414 if (command_table
[i
].name
== NULL
)
415 (void) fprintf(fp
, "\n");
417 (void) fprintf(fp
, "%s",
418 get_usage(command_table
[i
].usage
));
421 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
422 "pool/[dataset/]*dataset[@name]\n"));
424 (void) fprintf(fp
, gettext("usage:\n"));
425 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
428 if (current_command
!= NULL
&&
429 (strcmp(current_command
->name
, "set") == 0 ||
430 strcmp(current_command
->name
, "get") == 0 ||
431 strcmp(current_command
->name
, "inherit") == 0 ||
432 strcmp(current_command
->name
, "list") == 0))
433 show_properties
= B_TRUE
;
435 if (show_properties
) {
437 gettext("\nThe following properties are supported:\n"));
439 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
440 "PROPERTY", "EDIT", "INHERIT", "VALUES");
442 /* Iterate over all properties */
443 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
446 (void) fprintf(fp
, "\t%-15s ", "userused@...");
447 (void) fprintf(fp
, " NO NO <size>\n");
448 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
449 (void) fprintf(fp
, " NO NO <size>\n");
450 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
451 (void) fprintf(fp
, "YES NO <size> | none\n");
452 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
453 (void) fprintf(fp
, "YES NO <size> | none\n");
454 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
455 (void) fprintf(fp
, " NO NO <size>\n");
457 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
458 "with standard units such as K, M, G, etc.\n"));
459 (void) fprintf(fp
, gettext("\nUser-defined properties can "
460 "be specified by using a name containing a colon (:).\n"));
461 (void) fprintf(fp
, gettext("\nThe {user|group}{used|quota}@ "
462 "properties must be appended with\n"
463 "a user or group specifier of one of these forms:\n"
464 " POSIX name (eg: \"matt\")\n"
465 " POSIX id (eg: \"126829\")\n"
466 " SMB name@domain (eg: \"matt@sun\")\n"
467 " SMB SID (eg: \"S-1-234-567-89\")\n"));
470 gettext("\nFor the property list, run: %s\n"),
473 gettext("\nFor the delegated permission list, run: %s\n"),
474 "zfs allow|unallow");
478 * See comments at end of main().
480 if (getenv("ZFS_ABORT") != NULL
) {
481 (void) printf("dumping core by request\n");
485 exit(requested
? 0 : 2);
489 * Take a property=value argument string and add it to the given nvlist.
490 * Modifies the argument inplace.
493 parseprop(nvlist_t
*props
, char *propname
)
495 char *propval
, *strval
;
497 if ((propval
= strchr(propname
, '=')) == NULL
) {
498 (void) fprintf(stderr
, gettext("missing "
499 "'=' for property=value argument\n"));
504 if (nvlist_lookup_string(props
, propname
, &strval
) == 0) {
505 (void) fprintf(stderr
, gettext("property '%s' "
506 "specified multiple times\n"), propname
);
509 if (nvlist_add_string(props
, propname
, propval
) != 0)
515 parse_depth(char *opt
, int *flags
)
520 depth
= (int)strtol(opt
, &tmp
, 0);
522 (void) fprintf(stderr
,
523 gettext("%s is not an integer\n"), optarg
);
527 (void) fprintf(stderr
,
528 gettext("Depth can not be negative.\n"));
531 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
535 #define PROGRESS_DELAY 2 /* seconds */
537 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";
538 static time_t pt_begin
;
539 static char *pt_header
= NULL
;
540 static boolean_t pt_shown
;
543 start_progress_timer(void)
545 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
550 set_progress_header(char *header
)
552 assert(pt_header
== NULL
);
553 pt_header
= safe_strdup(header
);
555 (void) printf("%s: ", header
);
556 (void) fflush(stdout
);
561 update_progress(char *update
)
563 if (!pt_shown
&& time(NULL
) > pt_begin
) {
564 int len
= strlen(update
);
566 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
568 (void) fflush(stdout
);
570 } else if (pt_shown
) {
571 int len
= strlen(update
);
573 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
574 (void) fflush(stdout
);
579 finish_progress(char *done
)
582 (void) printf("%s\n", done
);
583 (void) fflush(stdout
);
590 * Check if the dataset is mountable and should be automatically mounted.
593 should_auto_mount(zfs_handle_t
*zhp
)
595 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, zfs_get_type(zhp
)))
597 return (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
);
601 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
603 * Given an existing dataset, create a writable copy whose initial contents
604 * are the same as the source. The newly created dataset maintains a
605 * dependency on the original; the original cannot be destroyed so long as
608 * The '-p' flag creates all the non-existing ancestors of the target first.
611 zfs_do_clone(int argc
, char **argv
)
613 zfs_handle_t
*zhp
= NULL
;
614 boolean_t parents
= B_FALSE
;
619 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
623 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
626 if (parseprop(props
, optarg
) != 0)
633 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
642 /* check number of arguments */
644 (void) fprintf(stderr
, gettext("missing source dataset "
649 (void) fprintf(stderr
, gettext("missing target dataset "
654 (void) fprintf(stderr
, gettext("too many arguments\n"));
658 /* open the source dataset */
659 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
662 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
665 * Now create the ancestors of the target dataset. If the
666 * target already exists and '-p' option was used we should not
669 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
672 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
677 ret
= zfs_clone(zhp
, argv
[1], props
);
679 /* create the mountpoint if necessary */
683 clone
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
686 * If the user doesn't want the dataset
687 * automatically mounted, then skip the mount/share
690 if (should_auto_mount(clone
)) {
691 if ((ret
= zfs_mount(clone
, NULL
, 0)) != 0) {
692 (void) fprintf(stderr
, gettext("clone "
693 "successfully created, "
694 "but not mounted\n"));
695 } else if ((ret
= zfs_share(clone
)) != 0) {
696 (void) fprintf(stderr
, gettext("clone "
697 "successfully created, "
698 "but not shared\n"));
719 * zfs create [-p] [-o prop=value] ... fs
720 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
722 * Create a new dataset. This command can be used to create filesystems
723 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
724 * For volumes, the user must specify a size to be used.
726 * The '-s' flag applies only to volumes, and indicates that we should not try
727 * to set the reservation for this volume. By default we set a reservation
728 * equal to the size for any volume. For pools with SPA_VERSION >=
729 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
731 * The '-p' flag creates all the non-existing ancestors of the target first.
734 zfs_do_create(int argc
, char **argv
)
736 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
737 zfs_handle_t
*zhp
= NULL
;
738 uint64_t volsize
= 0;
740 boolean_t noreserve
= B_FALSE
;
741 boolean_t bflag
= B_FALSE
;
742 boolean_t parents
= B_FALSE
;
747 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
751 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
754 type
= ZFS_TYPE_VOLUME
;
755 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
756 (void) fprintf(stderr
, gettext("bad volume "
757 "size '%s': %s\n"), optarg
,
758 libzfs_error_description(g_zfs
));
762 if (nvlist_add_uint64(props
,
763 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
772 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
773 (void) fprintf(stderr
, gettext("bad volume "
774 "block size '%s': %s\n"), optarg
,
775 libzfs_error_description(g_zfs
));
779 if (nvlist_add_uint64(props
,
780 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
785 if (parseprop(props
, optarg
) != 0)
792 (void) fprintf(stderr
, gettext("missing size "
796 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
802 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
803 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
804 "used when creating a volume\n"));
811 /* check number of arguments */
813 (void) fprintf(stderr
, gettext("missing %s argument\n"),
814 zfs_type_to_name(type
));
818 (void) fprintf(stderr
, gettext("too many arguments\n"));
822 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
823 zpool_handle_t
*zpool_handle
;
824 nvlist_t
*real_props
= NULL
;
825 uint64_t spa_version
;
827 zfs_prop_t resv_prop
;
831 if ((p
= strchr(argv
[0], '/')) != NULL
)
833 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
836 if (zpool_handle
== NULL
)
838 spa_version
= zpool_get_prop_int(zpool_handle
,
839 ZPOOL_PROP_VERSION
, NULL
);
840 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
841 resv_prop
= ZFS_PROP_REFRESERVATION
;
843 resv_prop
= ZFS_PROP_RESERVATION
;
845 (void) snprintf(msg
, sizeof (msg
),
846 gettext("cannot create '%s'"), argv
[0]);
847 if (props
&& (real_props
= zfs_valid_proplist(g_zfs
, type
,
848 props
, 0, NULL
, zpool_handle
, msg
)) == NULL
) {
849 zpool_close(zpool_handle
);
852 zpool_close(zpool_handle
);
854 volsize
= zvol_volsize_to_reservation(volsize
, real_props
);
855 nvlist_free(real_props
);
857 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
859 if (nvlist_add_uint64(props
,
860 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
867 if (parents
&& zfs_name_valid(argv
[0], type
)) {
869 * Now create the ancestors of target dataset. If the target
870 * already exists and '-p' option was used we should not
873 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
877 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
882 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
885 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
891 * Mount and/or share the new filesystem as appropriate. We provide a
892 * verbose error message to let the user know that their filesystem was
893 * in fact created, even if we failed to mount or share it.
894 * If the user doesn't want the dataset automatically mounted,
895 * then skip the mount/share step altogether.
897 if (should_auto_mount(zhp
)) {
898 if (zfs_mount(zhp
, NULL
, 0) != 0) {
899 (void) fprintf(stderr
, gettext("filesystem "
900 "successfully created, but not mounted\n"));
902 } else if (zfs_share(zhp
) != 0) {
903 (void) fprintf(stderr
, gettext("filesystem "
904 "successfully created, but not shared\n"));
921 * zfs destroy [-rRf] <fs, vol>
922 * zfs destroy [-rRd] <snap>
924 * -r Recursively destroy all children
925 * -R Recursively destroy all dependents, including clones
926 * -f Force unmounting of any dependents
927 * -d If we can't destroy now, mark for deferred destruction
929 * Destroys the given dataset. By default, it will unmount any filesystems,
930 * and refuse to destroy a dataset that has any dependents. A dependent can
931 * either be a child, or a clone of a child.
933 typedef struct destroy_cbdata
{
936 boolean_t cb_recurse
;
938 boolean_t cb_doclones
;
939 zfs_handle_t
*cb_target
;
940 boolean_t cb_defer_destroy
;
941 boolean_t cb_verbose
;
942 boolean_t cb_parsable
;
945 nvlist_t
*cb_batchedsnaps
;
947 /* first snap in contiguous run */
949 /* previous snap in contiguous run */
957 * Check for any dependents based on the '-r' or '-R' flags.
960 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
962 destroy_cbdata_t
*cbp
= data
;
963 const char *tname
= zfs_get_name(cbp
->cb_target
);
964 const char *name
= zfs_get_name(zhp
);
966 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
967 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
969 * This is a direct descendant, not a clone somewhere else in
976 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
977 "%s has children\n"),
978 zfs_get_name(cbp
->cb_target
),
979 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
980 (void) fprintf(stderr
, gettext("use '-r' to destroy "
981 "the following datasets:\n"));
982 cbp
->cb_first
= B_FALSE
;
983 cbp
->cb_error
= B_TRUE
;
986 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
989 * This is a clone. We only want to report this if the '-r'
990 * wasn't specified, or the target is a snapshot.
992 if (!cbp
->cb_recurse
&&
993 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
997 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
998 "%s has dependent clones\n"),
999 zfs_get_name(cbp
->cb_target
),
1000 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1001 (void) fprintf(stderr
, gettext("use '-R' to destroy "
1002 "the following datasets:\n"));
1003 cbp
->cb_first
= B_FALSE
;
1004 cbp
->cb_error
= B_TRUE
;
1005 cbp
->cb_dryrun
= B_TRUE
;
1008 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1017 destroy_callback(zfs_handle_t
*zhp
, void *data
)
1019 destroy_cbdata_t
*cb
= data
;
1020 const char *name
= zfs_get_name(zhp
);
1022 if (cb
->cb_verbose
) {
1023 if (cb
->cb_parsable
) {
1024 (void) printf("destroy\t%s\n", name
);
1025 } else if (cb
->cb_dryrun
) {
1026 (void) printf(gettext("would destroy %s\n"),
1029 (void) printf(gettext("will destroy %s\n"),
1035 * Ignore pools (which we've already flagged as an error before getting
1038 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1039 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1043 if (cb
->cb_dryrun
) {
1049 * We batch up all contiguous snapshots (even of different
1050 * filesystems) and destroy them with one ioctl. We can't
1051 * simply do all snap deletions and then all fs deletions,
1052 * because we must delete a clone before its origin.
1054 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1055 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1057 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1058 cb
->cb_batchedsnaps
, B_FALSE
);
1059 fnvlist_free(cb
->cb_batchedsnaps
);
1060 cb
->cb_batchedsnaps
= fnvlist_alloc();
1063 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1064 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1075 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1077 destroy_cbdata_t
*cb
= arg
;
1078 const char *name
= zfs_get_name(zhp
);
1081 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1082 if (cb
->cb_firstsnap
== NULL
)
1083 cb
->cb_firstsnap
= strdup(name
);
1084 if (cb
->cb_prevsnap
!= NULL
)
1085 free(cb
->cb_prevsnap
);
1086 /* this snap continues the current range */
1087 cb
->cb_prevsnap
= strdup(name
);
1088 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1090 if (cb
->cb_verbose
) {
1091 if (cb
->cb_parsable
) {
1092 (void) printf("destroy\t%s\n", name
);
1093 } else if (cb
->cb_dryrun
) {
1094 (void) printf(gettext("would destroy %s\n"),
1097 (void) printf(gettext("will destroy %s\n"),
1101 } else if (cb
->cb_firstsnap
!= NULL
) {
1102 /* end of this range */
1104 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1105 cb
->cb_prevsnap
, &used
);
1106 cb
->cb_snapused
+= used
;
1107 free(cb
->cb_firstsnap
);
1108 cb
->cb_firstsnap
= NULL
;
1109 free(cb
->cb_prevsnap
);
1110 cb
->cb_prevsnap
= NULL
;
1117 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1120 assert(cb
->cb_firstsnap
== NULL
);
1121 assert(cb
->cb_prevsnap
== NULL
);
1122 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1123 if (cb
->cb_firstsnap
!= NULL
) {
1126 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1127 cb
->cb_prevsnap
, &used
);
1129 cb
->cb_snapused
+= used
;
1130 free(cb
->cb_firstsnap
);
1131 cb
->cb_firstsnap
= NULL
;
1132 free(cb
->cb_prevsnap
);
1133 cb
->cb_prevsnap
= NULL
;
1139 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1141 destroy_cbdata_t
*cb
= arg
;
1144 /* Check for clones. */
1145 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1146 cb
->cb_target
= zhp
;
1147 cb
->cb_first
= B_TRUE
;
1148 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1149 destroy_check_dependent
, cb
);
1153 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1161 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1163 destroy_cbdata_t
*cb
= arg
;
1166 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1172 if (cb
->cb_verbose
) {
1173 err
= destroy_print_snapshots(zhp
, cb
);
1179 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1187 destroy_clones(destroy_cbdata_t
*cb
)
1190 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1192 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1193 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1196 boolean_t defer
= cb
->cb_defer_destroy
;
1200 * We can't defer destroy non-snapshots, so set it to
1201 * false while destroying the clones.
1203 cb
->cb_defer_destroy
= B_FALSE
;
1204 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1205 destroy_callback
, cb
);
1206 cb
->cb_defer_destroy
= defer
;
1216 zfs_do_destroy(int argc
, char **argv
)
1218 destroy_cbdata_t cb
= { 0 };
1222 zfs_handle_t
*zhp
= NULL
;
1224 zfs_type_t type
= ZFS_TYPE_DATASET
;
1227 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1230 cb
.cb_verbose
= B_TRUE
;
1233 cb
.cb_verbose
= B_TRUE
;
1234 cb
.cb_parsable
= B_TRUE
;
1237 cb
.cb_dryrun
= B_TRUE
;
1240 cb
.cb_defer_destroy
= B_TRUE
;
1241 type
= ZFS_TYPE_SNAPSHOT
;
1244 cb
.cb_force
= B_TRUE
;
1247 cb
.cb_recurse
= B_TRUE
;
1250 cb
.cb_recurse
= B_TRUE
;
1251 cb
.cb_doclones
= B_TRUE
;
1255 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1264 /* check number of arguments */
1266 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1270 (void) fprintf(stderr
, gettext("too many arguments\n"));
1274 at
= strchr(argv
[0], '@');
1275 pound
= strchr(argv
[0], '#');
1278 /* Build the list of snaps to destroy in cb_nvl. */
1279 cb
.cb_nvl
= fnvlist_alloc();
1282 zhp
= zfs_open(g_zfs
, argv
[0],
1283 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1287 cb
.cb_snapspec
= at
+ 1;
1288 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1294 if (nvlist_empty(cb
.cb_nvl
)) {
1295 (void) fprintf(stderr
, gettext("could not find any "
1296 "snapshots to destroy; check snapshot names.\n"));
1301 if (cb
.cb_verbose
) {
1303 zfs_nicenum(cb
.cb_snapused
, buf
, sizeof (buf
));
1304 if (cb
.cb_parsable
) {
1305 (void) printf("reclaim\t%llu\n",
1307 } else if (cb
.cb_dryrun
) {
1308 (void) printf(gettext("would reclaim %s\n"),
1311 (void) printf(gettext("will reclaim %s\n"),
1316 if (!cb
.cb_dryrun
) {
1317 if (cb
.cb_doclones
) {
1318 cb
.cb_batchedsnaps
= fnvlist_alloc();
1319 err
= destroy_clones(&cb
);
1321 err
= zfs_destroy_snaps_nvl(g_zfs
,
1322 cb
.cb_batchedsnaps
, B_FALSE
);
1330 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1331 cb
.cb_defer_destroy
);
1337 } else if (pound
!= NULL
) {
1342 (void) fprintf(stderr
,
1343 "dryrun is not supported with bookmark\n");
1347 if (cb
.cb_defer_destroy
) {
1348 (void) fprintf(stderr
,
1349 "defer destroy is not supported with bookmark\n");
1353 if (cb
.cb_recurse
) {
1354 (void) fprintf(stderr
,
1355 "recursive is not supported with bookmark\n");
1359 if (!zfs_bookmark_exists(argv
[0])) {
1360 (void) fprintf(stderr
, gettext("bookmark '%s' "
1361 "does not exist.\n"), argv
[0]);
1365 nvl
= fnvlist_alloc();
1366 fnvlist_add_boolean(nvl
, argv
[0]);
1368 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1370 (void) zfs_standard_error(g_zfs
, err
,
1371 "cannot destroy bookmark");
1374 nvlist_free(cb
.cb_nvl
);
1378 /* Open the given dataset */
1379 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1385 * Perform an explicit check for pools before going any further.
1387 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1388 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1389 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1390 "operation does not apply to pools\n"),
1392 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1393 "%s' to destroy all datasets in the pool\n"),
1395 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1396 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1402 * Check for any dependents and/or clones.
1404 cb
.cb_first
= B_TRUE
;
1405 if (!cb
.cb_doclones
&&
1406 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1417 cb
.cb_batchedsnaps
= fnvlist_alloc();
1418 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1425 * Do the real thing. The callback will close the
1426 * handle regardless of whether it succeeds or not.
1428 err
= destroy_callback(zhp
, &cb
);
1431 err
= zfs_destroy_snaps_nvl(g_zfs
,
1432 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1439 fnvlist_free(cb
.cb_batchedsnaps
);
1440 fnvlist_free(cb
.cb_nvl
);
1447 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1450 zfs_get_column_t col
;
1452 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1453 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1454 if (col
== GET_COL_RECVD
)
1460 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1461 * < all | property[,property]... > < fs | snap | vol > ...
1463 * -r recurse over any child datasets
1464 * -H scripted mode. Headers are stripped, and fields are separated
1465 * by tabs instead of spaces.
1466 * -o Set of fields to display. One of "name,property,value,
1467 * received,source". Default is "name,property,value,source".
1468 * "all" is an alias for all five.
1469 * -s Set of sources to allow. One of
1470 * "local,default,inherited,received,temporary,none". Default is
1472 * -p Display values in parsable (literal) format.
1474 * Prints properties for the given datasets. The user can control which
1475 * columns to display as well as which property types to allow.
1479 * Invoked to display the properties for a single dataset.
1482 get_callback(zfs_handle_t
*zhp
, void *data
)
1484 char buf
[ZFS_MAXPROPLEN
];
1485 char rbuf
[ZFS_MAXPROPLEN
];
1486 zprop_source_t sourcetype
;
1487 char source
[ZFS_MAX_DATASET_NAME_LEN
];
1488 zprop_get_cbdata_t
*cbp
= data
;
1489 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1490 zprop_list_t
*pl
= cbp
->cb_proplist
;
1494 boolean_t received
= is_recvd_column(cbp
);
1496 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1497 char *recvdval
= NULL
;
1499 * Skip the special fake placeholder. This will also skip over
1500 * the name property when 'all' is specified.
1502 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1503 pl
== cbp
->cb_proplist
)
1506 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1507 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1508 sizeof (buf
), &sourcetype
, source
,
1510 cbp
->cb_literal
) != 0) {
1513 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1514 ZFS_TYPE_DATASET
)) {
1515 (void) fprintf(stderr
,
1516 gettext("No such property '%s'\n"),
1517 zfs_prop_to_name(pl
->pl_prop
));
1520 sourcetype
= ZPROP_SRC_NONE
;
1521 (void) strlcpy(buf
, "-", sizeof (buf
));
1524 if (received
&& (zfs_prop_get_recvd(zhp
,
1525 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1526 cbp
->cb_literal
) == 0))
1529 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1530 zfs_prop_to_name(pl
->pl_prop
),
1531 buf
, sourcetype
, source
, recvdval
);
1532 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1533 sourcetype
= ZPROP_SRC_LOCAL
;
1535 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1536 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1537 sourcetype
= ZPROP_SRC_NONE
;
1538 (void) strlcpy(buf
, "-", sizeof (buf
));
1541 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1542 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1543 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1544 sourcetype
= ZPROP_SRC_LOCAL
;
1546 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1547 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1548 sourcetype
= ZPROP_SRC_NONE
;
1549 (void) strlcpy(buf
, "-", sizeof (buf
));
1552 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1553 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1555 if (nvlist_lookup_nvlist(user_props
,
1556 pl
->pl_user_prop
, &propval
) != 0) {
1559 sourcetype
= ZPROP_SRC_NONE
;
1562 verify(nvlist_lookup_string(propval
,
1563 ZPROP_VALUE
, &strval
) == 0);
1564 verify(nvlist_lookup_string(propval
,
1565 ZPROP_SOURCE
, &sourceval
) == 0);
1567 if (strcmp(sourceval
,
1568 zfs_get_name(zhp
)) == 0) {
1569 sourcetype
= ZPROP_SRC_LOCAL
;
1570 } else if (strcmp(sourceval
,
1571 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1572 sourcetype
= ZPROP_SRC_RECEIVED
;
1574 sourcetype
= ZPROP_SRC_INHERITED
;
1575 (void) strlcpy(source
,
1576 sourceval
, sizeof (source
));
1580 if (received
&& (zfs_prop_get_recvd(zhp
,
1581 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1582 cbp
->cb_literal
) == 0))
1585 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1586 pl
->pl_user_prop
, strval
, sourcetype
,
1595 zfs_do_get(int argc
, char **argv
)
1597 zprop_get_cbdata_t cb
= { 0 };
1598 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1599 int types
= ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
;
1600 char *value
, *fields
;
1603 zprop_list_t fake_name
= { 0 };
1606 * Set up default columns and sources.
1608 cb
.cb_sources
= ZPROP_SRC_ALL
;
1609 cb
.cb_columns
[0] = GET_COL_NAME
;
1610 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1611 cb
.cb_columns
[2] = GET_COL_VALUE
;
1612 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1613 cb
.cb_type
= ZFS_TYPE_DATASET
;
1616 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1619 cb
.cb_literal
= B_TRUE
;
1622 limit
= parse_depth(optarg
, &flags
);
1625 flags
|= ZFS_ITER_RECURSE
;
1628 cb
.cb_scripted
= B_TRUE
;
1631 (void) fprintf(stderr
, gettext("missing argument for "
1632 "'%c' option\n"), optopt
);
1637 * Process the set of columns to display. We zero out
1638 * the structure to give us a blank slate.
1640 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1642 while (*optarg
!= '\0') {
1643 static char *col_subopts
[] =
1644 { "name", "property", "value", "received",
1645 "source", "all", NULL
};
1647 if (i
== ZFS_GET_NCOLS
) {
1648 (void) fprintf(stderr
, gettext("too "
1649 "many fields given to -o "
1654 switch (getsubopt(&optarg
, col_subopts
,
1657 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1660 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1663 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1666 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1667 flags
|= ZFS_ITER_RECVD_PROPS
;
1670 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1674 (void) fprintf(stderr
,
1675 gettext("\"all\" conflicts "
1676 "with specific fields "
1677 "given to -o option\n"));
1680 cb
.cb_columns
[0] = GET_COL_NAME
;
1681 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1682 cb
.cb_columns
[2] = GET_COL_VALUE
;
1683 cb
.cb_columns
[3] = GET_COL_RECVD
;
1684 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1685 flags
|= ZFS_ITER_RECVD_PROPS
;
1689 (void) fprintf(stderr
,
1690 gettext("invalid column name "
1699 while (*optarg
!= '\0') {
1700 static char *source_subopts
[] = {
1701 "local", "default", "inherited",
1702 "received", "temporary", "none",
1705 switch (getsubopt(&optarg
, source_subopts
,
1708 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1711 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1714 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1717 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1720 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1723 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1726 (void) fprintf(stderr
,
1727 gettext("invalid source "
1736 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1737 while (*optarg
!= '\0') {
1738 static char *type_subopts
[] = { "filesystem",
1739 "volume", "snapshot", "bookmark",
1742 switch (getsubopt(&optarg
, type_subopts
,
1745 types
|= ZFS_TYPE_FILESYSTEM
;
1748 types
|= ZFS_TYPE_VOLUME
;
1751 types
|= ZFS_TYPE_SNAPSHOT
;
1754 types
|= ZFS_TYPE_BOOKMARK
;
1757 types
= ZFS_TYPE_DATASET
|
1762 (void) fprintf(stderr
,
1763 gettext("invalid type '%s'\n"),
1771 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1781 (void) fprintf(stderr
, gettext("missing property "
1788 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1796 * As part of zfs_expand_proplist(), we keep track of the maximum column
1797 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1798 * need to know the maximum name length. However, the user likely did
1799 * not specify 'name' as one of the properties to fetch, so we need to
1800 * make sure we always include at least this property for
1801 * print_get_headers() to work properly.
1803 if (cb
.cb_proplist
!= NULL
) {
1804 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1805 fake_name
.pl_width
= strlen(gettext("NAME"));
1806 fake_name
.pl_next
= cb
.cb_proplist
;
1807 cb
.cb_proplist
= &fake_name
;
1810 cb
.cb_first
= B_TRUE
;
1812 /* run for each object */
1813 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1814 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1816 if (cb
.cb_proplist
== &fake_name
)
1817 zprop_free_list(fake_name
.pl_next
);
1819 zprop_free_list(cb
.cb_proplist
);
1825 * inherit [-rS] <property> <fs|vol> ...
1827 * -r Recurse over all children
1828 * -S Revert to received value, if any
1830 * For each dataset specified on the command line, inherit the given property
1831 * from its parent. Inheriting a property at the pool level will cause it to
1832 * use the default value. The '-r' flag will recurse over all children, and is
1833 * useful for setting a property on a hierarchy-wide basis, regardless of any
1834 * local modifications for each dataset.
1837 typedef struct inherit_cbdata
{
1838 const char *cb_propname
;
1839 boolean_t cb_received
;
1843 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1845 inherit_cbdata_t
*cb
= data
;
1846 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1849 * If we're doing it recursively, then ignore properties that
1850 * are not valid for this type of dataset.
1852 if (prop
!= ZPROP_INVAL
&&
1853 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
)))
1856 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1860 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1862 inherit_cbdata_t
*cb
= data
;
1864 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1868 zfs_do_inherit(int argc
, char **argv
)
1872 inherit_cbdata_t cb
= { 0 };
1876 boolean_t received
= B_FALSE
;
1879 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1882 flags
|= ZFS_ITER_RECURSE
;
1889 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1898 /* check number of arguments */
1900 (void) fprintf(stderr
, gettext("missing property argument\n"));
1904 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1912 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1913 if (zfs_prop_readonly(prop
)) {
1914 (void) fprintf(stderr
, gettext(
1915 "%s property is read-only\n"),
1919 if (!zfs_prop_inheritable(prop
) && !received
) {
1920 (void) fprintf(stderr
, gettext("'%s' property cannot "
1921 "be inherited\n"), propname
);
1922 if (prop
== ZFS_PROP_QUOTA
||
1923 prop
== ZFS_PROP_RESERVATION
||
1924 prop
== ZFS_PROP_REFQUOTA
||
1925 prop
== ZFS_PROP_REFRESERVATION
) {
1926 (void) fprintf(stderr
, gettext("use 'zfs set "
1927 "%s=none' to clear\n"), propname
);
1928 (void) fprintf(stderr
, gettext("use 'zfs "
1929 "inherit -S %s' to revert to received "
1930 "value\n"), propname
);
1934 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1935 prop
== ZFS_PROP_VERSION
)) {
1936 (void) fprintf(stderr
, gettext("'%s' property cannot "
1937 "be reverted to a received value\n"), propname
);
1940 } else if (!zfs_prop_user(propname
)) {
1941 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1946 cb
.cb_propname
= propname
;
1947 cb
.cb_received
= received
;
1949 if (flags
& ZFS_ITER_RECURSE
) {
1950 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1951 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1953 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1954 NULL
, NULL
, 0, inherit_cb
, &cb
);
1960 typedef struct upgrade_cbdata
{
1961 uint64_t cb_numupgraded
;
1962 uint64_t cb_numsamegraded
;
1963 uint64_t cb_numfailed
;
1964 uint64_t cb_version
;
1966 boolean_t cb_foundone
;
1967 char cb_lastfs
[ZFS_MAX_DATASET_NAME_LEN
];
1971 same_pool(zfs_handle_t
*zhp
, const char *name
)
1973 int len1
= strcspn(name
, "/@");
1974 const char *zhname
= zfs_get_name(zhp
);
1975 int len2
= strcspn(zhname
, "/@");
1979 return (strncmp(name
, zhname
, len1
) == 0);
1983 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
1985 upgrade_cbdata_t
*cb
= data
;
1986 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1988 /* list if it's old/new */
1989 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
1990 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
1993 str
= gettext("The following filesystems are "
1994 "formatted using a newer software version and\n"
1995 "cannot be accessed on the current system.\n\n");
1997 str
= gettext("The following filesystems are "
1998 "out of date, and can be upgraded. After being\n"
1999 "upgraded, these filesystems (and any 'zfs send' "
2000 "streams generated from\n"
2001 "subsequent snapshots) will no longer be "
2002 "accessible by older software versions.\n\n");
2005 if (!cb
->cb_foundone
) {
2007 (void) printf(gettext("VER FILESYSTEM\n"));
2008 (void) printf(gettext("--- ------------\n"));
2009 cb
->cb_foundone
= B_TRUE
;
2012 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
2019 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2021 upgrade_cbdata_t
*cb
= data
;
2022 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2023 int needed_spa_version
;
2026 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2029 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2031 if (needed_spa_version
< 0)
2034 if (spa_version
< needed_spa_version
) {
2036 (void) printf(gettext("%s: can not be "
2037 "upgraded; the pool version needs to first "
2038 "be upgraded\nto version %d\n\n"),
2039 zfs_get_name(zhp
), needed_spa_version
);
2045 if (version
< cb
->cb_version
) {
2047 (void) snprintf(verstr
, sizeof (verstr
),
2048 "%llu", cb
->cb_version
);
2049 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2051 * If they did "zfs upgrade -a", then we could
2052 * be doing ioctls to different pools. We need
2053 * to log this history once to each pool, and bypass
2054 * the normal history logging that happens in main().
2056 (void) zpool_log_history(g_zfs
, history_str
);
2057 log_history
= B_FALSE
;
2059 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2060 cb
->cb_numupgraded
++;
2063 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2064 } else if (version
> cb
->cb_version
) {
2065 /* can't downgrade */
2066 (void) printf(gettext("%s: can not be downgraded; "
2067 "it is already at version %u\n"),
2068 zfs_get_name(zhp
), version
);
2071 cb
->cb_numsamegraded
++;
2079 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2082 zfs_do_upgrade(int argc
, char **argv
)
2084 boolean_t all
= B_FALSE
;
2085 boolean_t showversions
= B_FALSE
;
2087 upgrade_cbdata_t cb
= { 0 };
2089 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2092 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2095 flags
|= ZFS_ITER_RECURSE
;
2098 showversions
= B_TRUE
;
2101 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2102 optarg
, &cb
.cb_version
) != 0) {
2103 (void) fprintf(stderr
,
2104 gettext("invalid version %s\n"), optarg
);
2113 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2122 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2124 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2125 cb
.cb_version
|| argc
))
2127 if ((all
|| argc
) && (showversions
))
2133 /* Show info on available versions. */
2134 (void) printf(gettext("The following filesystem versions are "
2136 (void) printf(gettext("VER DESCRIPTION\n"));
2137 (void) printf("--- -----------------------------------------"
2138 "---------------\n");
2139 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2140 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2141 (void) printf(gettext(" 3 Case insensitive and filesystem "
2142 "user identifier (FUID)\n"));
2143 (void) printf(gettext(" 4 userquota, groupquota "
2145 (void) printf(gettext(" 5 System attributes\n"));
2146 (void) printf(gettext("\nFor more information on a particular "
2147 "version, including supported releases,\n"));
2148 (void) printf("see the ZFS Administration Guide.\n\n");
2150 } else if (argc
|| all
) {
2151 /* Upgrade filesystems */
2152 if (cb
.cb_version
== 0)
2153 cb
.cb_version
= ZPL_VERSION
;
2154 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2155 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2156 (void) printf(gettext("%llu filesystems upgraded\n"),
2158 if (cb
.cb_numsamegraded
) {
2159 (void) printf(gettext("%llu filesystems already at "
2161 cb
.cb_numsamegraded
);
2163 if (cb
.cb_numfailed
!= 0)
2166 /* List old-version filesytems */
2168 (void) printf(gettext("This system is currently running "
2169 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2171 flags
|= ZFS_ITER_RECURSE
;
2172 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2173 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2175 found
= cb
.cb_foundone
;
2176 cb
.cb_foundone
= B_FALSE
;
2177 cb
.cb_newer
= B_TRUE
;
2179 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2180 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2182 if (!cb
.cb_foundone
&& !found
) {
2183 (void) printf(gettext("All filesystems are "
2184 "formatted with the current version.\n"));
2192 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2193 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2194 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2195 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2197 * -H Scripted mode; elide headers and separate columns by tabs.
2198 * -i Translate SID to POSIX ID.
2199 * -n Print numeric ID instead of user/group name.
2200 * -o Control which fields to display.
2201 * -p Use exact (parsable) numeric output.
2202 * -s Specify sort columns, descending order.
2203 * -S Specify sort columns, ascending order.
2204 * -t Control which object types to display.
2206 * Displays space consumed by, and quotas on, each user in the specified
2207 * filesystem or snapshot.
2210 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2211 enum us_field_types
{
2217 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA" };
2218 static char *us_field_names
[] = { "type", "name", "used", "quota" };
2219 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2221 #define USTYPE_PSX_GRP (1 << 0)
2222 #define USTYPE_PSX_USR (1 << 1)
2223 #define USTYPE_SMB_GRP (1 << 2)
2224 #define USTYPE_SMB_USR (1 << 3)
2225 #define USTYPE_ALL \
2226 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2228 static int us_type_bits
[] = {
2235 static char *us_type_names
[] = { "posixgroup", "posixuser", "smbgroup",
2238 typedef struct us_node
{
2240 uu_avl_node_t usn_avlnode
;
2241 uu_list_node_t usn_listnode
;
2244 typedef struct us_cbdata
{
2246 uu_avl_pool_t
*cb_avl_pool
;
2248 boolean_t cb_numname
;
2249 boolean_t cb_nicenum
;
2250 boolean_t cb_sid2posix
;
2251 zfs_userquota_prop_t cb_prop
;
2252 zfs_sort_column_t
*cb_sortcol
;
2253 size_t cb_width
[USFIELD_LAST
];
2256 static boolean_t us_populated
= B_FALSE
;
2259 zfs_sort_column_t
*si_sortcol
;
2260 boolean_t si_numname
;
2264 us_field_index(char *field
)
2268 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2269 if (strcmp(field
, us_field_names
[i
]) == 0)
2277 us_compare(const void *larg
, const void *rarg
, void *unused
)
2279 const us_node_t
*l
= larg
;
2280 const us_node_t
*r
= rarg
;
2281 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2282 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2283 boolean_t numname
= si
->si_numname
;
2284 nvlist_t
*lnvl
= l
->usn_nvl
;
2285 nvlist_t
*rnvl
= r
->usn_nvl
;
2289 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2296 zfs_prop_t prop
= sortcol
->sc_prop
;
2297 const char *propname
= NULL
;
2298 boolean_t reverse
= sortcol
->sc_reverse
;
2303 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2304 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2306 rc
= (rv32
< lv32
) ? 1 : -1;
2311 (void) nvlist_lookup_uint64(lnvl
, propname
,
2313 (void) nvlist_lookup_uint64(rnvl
, propname
,
2316 rc
= (rv64
< lv64
) ? 1 : -1;
2318 (void) nvlist_lookup_string(lnvl
, propname
,
2320 (void) nvlist_lookup_string(rnvl
, propname
,
2322 rc
= strcmp(lvstr
, rvstr
);
2326 case ZFS_PROP_QUOTA
:
2329 if (prop
== ZFS_PROP_USED
)
2333 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2334 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2336 rc
= (rv64
< lv64
) ? 1 : -1;
2345 return (reverse
? 1 : -1);
2347 return (reverse
? -1 : 1);
2352 * If entries still seem to be the same, check if they are of the same
2353 * type (smbentity is added only if we are doing SID to POSIX ID
2354 * translation where we can have duplicate type/name combinations).
2356 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2357 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2359 return (lvb
< rvb
? -1 : 1);
2364 static inline const char *
2365 us_type2str(unsigned field_type
)
2367 switch (field_type
) {
2368 case USTYPE_PSX_USR
:
2369 return ("POSIX User");
2370 case USTYPE_PSX_GRP
:
2371 return ("POSIX Group");
2372 case USTYPE_SMB_USR
:
2373 return ("SMB User");
2374 case USTYPE_SMB_GRP
:
2375 return ("SMB Group");
2377 return ("Undefined");
2382 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2384 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2385 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2390 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2391 uu_avl_t
*avl
= cb
->cb_avl
;
2395 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2397 const char *typestr
;
2401 int typeidx
, nameidx
, sizeidx
;
2402 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2403 boolean_t smbentity
= B_FALSE
;
2405 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2407 node
= safe_malloc(sizeof (us_node_t
));
2408 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2409 node
->usn_nvl
= props
;
2411 if (domain
!= NULL
&& domain
[0] != '\0') {
2413 char sid
[MAXNAMELEN
+ 32];
2416 int flag
= IDMAP_REQ_FLG_USE_CACHE
;
2420 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2422 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2423 type
= USTYPE_SMB_GRP
;
2424 err
= sid_to_id(sid
, B_FALSE
, &id
);
2426 type
= USTYPE_SMB_USR
;
2427 err
= sid_to_id(sid
, B_TRUE
, &id
);
2432 if (!cb
->cb_sid2posix
) {
2433 if (type
== USTYPE_SMB_USR
) {
2434 (void) idmap_getwinnamebyuid(rid
, flag
,
2437 (void) idmap_getwinnamebygid(rid
, flag
,
2446 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2448 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2449 type
= USTYPE_PSX_GRP
;
2450 if (!cb
->cb_numname
) {
2453 if ((g
= getgrgid(rid
)) != NULL
)
2457 type
= USTYPE_PSX_USR
;
2458 if (!cb
->cb_numname
) {
2461 if ((p
= getpwuid(rid
)) != NULL
)
2468 * Make sure that the type/name combination is unique when doing
2469 * SID to POSIX ID translation (hence changing the type from SMB to
2472 if (cb
->cb_sid2posix
&&
2473 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2476 /* Calculate/update width of TYPE field */
2477 typestr
= us_type2str(type
);
2478 typelen
= strlen(gettext(typestr
));
2479 typeidx
= us_field_index("type");
2480 if (typelen
> cb
->cb_width
[typeidx
])
2481 cb
->cb_width
[typeidx
] = typelen
;
2482 if (nvlist_add_uint32(props
, "type", type
) != 0)
2485 /* Calculate/update width of NAME field */
2486 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2487 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2489 namelen
= snprintf(NULL
, 0, "%u", rid
);
2491 if (nvlist_add_string(props
, "name", name
) != 0)
2493 namelen
= strlen(name
);
2495 nameidx
= us_field_index("name");
2496 if (namelen
> cb
->cb_width
[nameidx
])
2497 cb
->cb_width
[nameidx
] = namelen
;
2500 * Check if this type/name combination is in the list and update it;
2501 * otherwise add new node to the list.
2503 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2504 uu_avl_insert(avl
, node
, idx
);
2509 props
= node
->usn_nvl
;
2512 /* Calculate/update width of USED/QUOTA fields */
2514 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2516 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu", space
);
2517 sizelen
= strlen(sizebuf
);
2518 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
) {
2520 if (!nvlist_exists(props
, "quota"))
2521 (void) nvlist_add_uint64(props
, "quota", 0);
2524 if (!nvlist_exists(props
, "used"))
2525 (void) nvlist_add_uint64(props
, "used", 0);
2527 sizeidx
= us_field_index(propname
);
2528 if (sizelen
> cb
->cb_width
[sizeidx
])
2529 cb
->cb_width
[sizeidx
] = sizelen
;
2531 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2538 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2539 size_t *width
, us_node_t
*node
)
2541 nvlist_t
*nvl
= node
->usn_nvl
;
2542 char valstr
[MAXNAMELEN
];
2543 boolean_t first
= B_TRUE
;
2549 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2550 if (!(ustype
& types
))
2553 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2554 nvpair_t
*nvp
= NULL
;
2558 char *strval
= NULL
;
2560 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2561 if (strcmp(nvpair_name(nvp
),
2562 us_field_names
[field
]) == 0)
2566 type
= nvpair_type(nvp
);
2568 case DATA_TYPE_UINT32
:
2569 (void) nvpair_value_uint32(nvp
, &val32
);
2571 case DATA_TYPE_UINT64
:
2572 (void) nvpair_value_uint64(nvp
, &val64
);
2574 case DATA_TYPE_STRING
:
2575 (void) nvpair_value_string(nvp
, &strval
);
2578 (void) fprintf(stderr
, "invalid data type\n");
2583 strval
= (char *)us_type2str(val32
);
2586 if (type
== DATA_TYPE_UINT64
) {
2587 (void) sprintf(valstr
, "%llu", val64
);
2593 if (type
== DATA_TYPE_UINT64
) {
2595 (void) sprintf(valstr
, "%llu", val64
);
2597 zfs_nicenum(val64
, valstr
,
2600 if (field
== USFIELD_QUOTA
&&
2601 strcmp(valstr
, "0") == 0)
2611 (void) printf("\t");
2616 (void) printf("%s", strval
);
2617 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2618 (void) printf("%-*s", width
[field
], strval
);
2620 (void) printf("%*s", width
[field
], strval
);
2626 (void) printf("\n");
2630 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2631 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2639 boolean_t first
= B_TRUE
;
2641 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2642 col
= gettext(us_field_hdr
[field
]);
2643 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2644 (void) printf(first
? "%-*s" : " %-*s",
2647 (void) printf(first
? "%*s" : " %*s",
2653 (void) printf("\n");
2656 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2657 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2659 nvlist_free(node
->usn_nvl
);
2664 zfs_do_userspace(int argc
, char **argv
)
2667 zfs_userquota_prop_t p
;
2668 uu_avl_pool_t
*avl_pool
;
2670 uu_avl_walk_t
*walk
;
2672 char deffields
[] = "type,name,used,quota";
2673 char *ofield
= NULL
;
2674 char *tfield
= NULL
;
2678 boolean_t scripted
= B_FALSE
;
2679 boolean_t prtnum
= B_FALSE
;
2680 boolean_t parsable
= B_FALSE
;
2681 boolean_t sid2posix
= B_FALSE
;
2684 zfs_sort_column_t
*sortcol
= NULL
;
2685 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2689 uu_list_pool_t
*listpool
;
2691 uu_avl_index_t idx
= 0;
2692 uu_list_index_t idx2
= 0;
2697 if (strcmp(argv
[0], "groupspace") == 0)
2698 /* Toggle default group types */
2699 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2701 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2717 if (zfs_add_sort_column(&sortcol
, optarg
,
2718 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
2719 (void) fprintf(stderr
,
2720 gettext("invalid field '%s'\n"), optarg
);
2731 (void) fprintf(stderr
, gettext("missing argument for "
2732 "'%c' option\n"), optopt
);
2736 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2746 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2750 (void) fprintf(stderr
, gettext("too many arguments\n"));
2754 /* Use default output fields if not specified using -o */
2758 if ((delim
= strchr(ofield
, ',')) != NULL
)
2760 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
2761 (void) fprintf(stderr
, gettext("invalid type '%s' "
2762 "for -o option\n"), ofield
);
2767 } while (delim
!= NULL
);
2768 fields
[cfield
] = USFIELD_LAST
;
2770 /* Override output types (-t option) */
2771 if (tfield
!= NULL
) {
2775 boolean_t found
= B_FALSE
;
2777 if ((delim
= strchr(tfield
, ',')) != NULL
)
2779 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
2781 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
2783 types
|= us_type_bits
[i
];
2788 (void) fprintf(stderr
, gettext("invalid type "
2789 "'%s' for -t option\n"), tfield
);
2794 } while (delim
!= NULL
);
2797 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
2800 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
2801 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
2803 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
2806 /* Always add default sorting columns */
2807 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
2808 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
2810 cb
.cb_sortcol
= sortcol
;
2811 cb
.cb_numname
= prtnum
;
2812 cb
.cb_nicenum
= !parsable
;
2813 cb
.cb_avl_pool
= avl_pool
;
2814 cb
.cb_avl
= avl_tree
;
2815 cb
.cb_sid2posix
= sid2posix
;
2817 for (i
= 0; i
< USFIELD_LAST
; i
++)
2818 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
2820 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
2821 if (((p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
) &&
2822 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
2823 ((p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
) &&
2824 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))))
2827 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
2832 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
2835 us_populated
= B_TRUE
;
2837 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
2838 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
2839 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
2840 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
2842 while (node
!= NULL
) {
2844 node
= uu_avl_next(avl_tree
, node
);
2845 uu_avl_remove(avl_tree
, rmnode
);
2846 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
2847 uu_list_insert(list
, rmnode
, idx2
);
2850 for (node
= uu_list_first(list
); node
!= NULL
;
2851 node
= uu_list_next(list
, node
)) {
2852 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
2854 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
2855 uu_avl_insert(avl_tree
, node
, idx
);
2858 uu_list_destroy(list
);
2859 uu_list_pool_destroy(listpool
);
2861 /* Print and free node nvlist memory */
2862 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
2865 zfs_free_sort_columns(sortcol
);
2867 /* Clean up the AVL tree */
2868 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
2871 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
2872 uu_avl_remove(cb
.cb_avl
, node
);
2876 uu_avl_walk_end(walk
);
2877 uu_avl_destroy(avl_tree
);
2878 uu_avl_pool_destroy(avl_pool
);
2884 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2885 * [-t type[,...]] [filesystem|volume|snapshot] ...
2887 * -H Scripted mode; elide headers and separate columns by tabs.
2888 * -p Display values in parsable (literal) format.
2889 * -r Recurse over all children.
2890 * -d Limit recursion by depth.
2891 * -o Control which fields to display.
2892 * -s Specify sort columns, descending order.
2893 * -S Specify sort columns, ascending order.
2894 * -t Control which object types to display.
2896 * When given no arguments, list all filesystems in the system.
2897 * Otherwise, list the specified datasets, optionally recursing down them if
2898 * '-r' is specified.
2900 typedef struct list_cbdata
{
2902 boolean_t cb_literal
;
2903 boolean_t cb_scripted
;
2904 zprop_list_t
*cb_proplist
;
2908 * Given a list of columns to display, output appropriate headers for each one.
2911 print_header(list_cbdata_t
*cb
)
2913 zprop_list_t
*pl
= cb
->cb_proplist
;
2914 char headerbuf
[ZFS_MAXPROPLEN
];
2917 boolean_t first
= B_TRUE
;
2918 boolean_t right_justify
;
2920 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2927 right_justify
= B_FALSE
;
2928 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2929 header
= zfs_prop_column_name(pl
->pl_prop
);
2930 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2932 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2933 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2934 headerbuf
[i
] = '\0';
2938 if (pl
->pl_next
== NULL
&& !right_justify
)
2939 (void) printf("%s", header
);
2940 else if (right_justify
)
2941 (void) printf("%*s", pl
->pl_width
, header
);
2943 (void) printf("%-*s", pl
->pl_width
, header
);
2946 (void) printf("\n");
2950 * Given a dataset and a list of fields, print out all the properties according
2951 * to the described layout.
2954 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
2956 zprop_list_t
*pl
= cb
->cb_proplist
;
2957 boolean_t first
= B_TRUE
;
2958 char property
[ZFS_MAXPROPLEN
];
2959 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
2962 boolean_t right_justify
;
2964 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2966 if (cb
->cb_scripted
)
2967 (void) printf("\t");
2974 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
2975 (void) strlcpy(property
, zfs_get_name(zhp
),
2978 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2979 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
2980 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
2981 sizeof (property
), NULL
, NULL
, 0,
2982 cb
->cb_literal
) != 0)
2986 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2987 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
2988 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
2989 property
, sizeof (property
), cb
->cb_literal
) != 0)
2993 right_justify
= B_TRUE
;
2994 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
2995 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
2996 property
, sizeof (property
), cb
->cb_literal
) != 0)
3000 right_justify
= B_TRUE
;
3002 if (nvlist_lookup_nvlist(userprops
,
3003 pl
->pl_user_prop
, &propval
) != 0)
3006 verify(nvlist_lookup_string(propval
,
3007 ZPROP_VALUE
, &propstr
) == 0);
3008 right_justify
= B_FALSE
;
3012 * If this is being called in scripted mode, or if this is the
3013 * last column and it is left-justified, don't include a width
3016 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3017 (void) printf("%s", propstr
);
3018 else if (right_justify
)
3019 (void) printf("%*s", pl
->pl_width
, propstr
);
3021 (void) printf("%-*s", pl
->pl_width
, propstr
);
3024 (void) printf("\n");
3028 * Generic callback function to list a dataset or snapshot.
3031 list_callback(zfs_handle_t
*zhp
, void *data
)
3033 list_cbdata_t
*cbp
= data
;
3035 if (cbp
->cb_first
) {
3036 if (!cbp
->cb_scripted
)
3038 cbp
->cb_first
= B_FALSE
;
3041 print_dataset(zhp
, cbp
);
3047 zfs_do_list(int argc
, char **argv
)
3050 static char default_fields
[] =
3051 "name,used,available,referenced,mountpoint";
3052 int types
= ZFS_TYPE_DATASET
;
3053 boolean_t types_specified
= B_FALSE
;
3054 char *fields
= NULL
;
3055 list_cbdata_t cb
= { 0 };
3059 zfs_sort_column_t
*sortcol
= NULL
;
3060 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3063 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3069 cb
.cb_literal
= B_TRUE
;
3070 flags
|= ZFS_ITER_LITERAL_PROPS
;
3073 limit
= parse_depth(optarg
, &flags
);
3076 flags
|= ZFS_ITER_RECURSE
;
3079 cb
.cb_scripted
= B_TRUE
;
3082 if (zfs_add_sort_column(&sortcol
, optarg
,
3084 (void) fprintf(stderr
,
3085 gettext("invalid property '%s'\n"), optarg
);
3090 if (zfs_add_sort_column(&sortcol
, optarg
,
3092 (void) fprintf(stderr
,
3093 gettext("invalid property '%s'\n"), optarg
);
3099 types_specified
= B_TRUE
;
3100 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3101 while (*optarg
!= '\0') {
3102 static char *type_subopts
[] = { "filesystem",
3103 "volume", "snapshot", "snap", "bookmark",
3106 switch (getsubopt(&optarg
, type_subopts
,
3109 types
|= ZFS_TYPE_FILESYSTEM
;
3112 types
|= ZFS_TYPE_VOLUME
;
3116 types
|= ZFS_TYPE_SNAPSHOT
;
3119 types
|= ZFS_TYPE_BOOKMARK
;
3122 types
= ZFS_TYPE_DATASET
|
3126 (void) fprintf(stderr
,
3127 gettext("invalid type '%s'\n"),
3134 (void) fprintf(stderr
, gettext("missing argument for "
3135 "'%c' option\n"), optopt
);
3139 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3149 fields
= default_fields
;
3152 * If we are only going to list snapshot names and sort by name,
3153 * then we can use faster version.
3155 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3156 flags
|= ZFS_ITER_SIMPLE
;
3159 * If "-o space" and no types were specified, don't display snapshots.
3161 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3162 types
&= ~ZFS_TYPE_SNAPSHOT
;
3165 * If the user specifies '-o all', the zprop_get_list() doesn't
3166 * normally include the name of the dataset. For 'zfs list', we always
3167 * want this property to be first.
3169 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3173 cb
.cb_first
= B_TRUE
;
3175 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3176 limit
, list_callback
, &cb
);
3178 zprop_free_list(cb
.cb_proplist
);
3179 zfs_free_sort_columns(sortcol
);
3181 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3182 (void) printf(gettext("no datasets available\n"));
3188 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3189 * zfs rename [-f] -p <fs | vol> <fs | vol>
3190 * zfs rename -r <snap> <snap>
3192 * Renames the given dataset to another of the same type.
3194 * The '-p' flag creates all the non-existing ancestors of the target first.
3198 zfs_do_rename(int argc
, char **argv
)
3203 boolean_t recurse
= B_FALSE
;
3204 boolean_t parents
= B_FALSE
;
3205 boolean_t force_unmount
= B_FALSE
;
3208 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3217 force_unmount
= B_TRUE
;
3221 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3230 /* check number of arguments */
3232 (void) fprintf(stderr
, gettext("missing source dataset "
3237 (void) fprintf(stderr
, gettext("missing target dataset "
3242 (void) fprintf(stderr
, gettext("too many arguments\n"));
3246 if (recurse
&& parents
) {
3247 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3252 if (recurse
&& strchr(argv
[0], '@') == 0) {
3253 (void) fprintf(stderr
, gettext("source dataset for recursive "
3254 "rename must be a snapshot\n"));
3258 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3259 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3262 /* If we were asked and the name looks good, try to create ancestors. */
3263 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3264 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3269 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3278 * Promotes the given clone fs to be the parent
3282 zfs_do_promote(int argc
, char **argv
)
3288 if (argc
> 1 && argv
[1][0] == '-') {
3289 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3294 /* check number of arguments */
3296 (void) fprintf(stderr
, gettext("missing clone filesystem"
3301 (void) fprintf(stderr
, gettext("too many arguments\n"));
3305 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3309 ret
= (zfs_promote(zhp
) != 0);
3317 * zfs rollback [-rRf] <snapshot>
3319 * -r Delete any intervening snapshots before doing rollback
3320 * -R Delete any snapshots and their clones
3321 * -f ignored for backwards compatability
3323 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3324 * since then and making it the active dataset. If more recent snapshots exist,
3325 * the command will complain unless the '-r' flag is given.
3327 typedef struct rollback_cbdata
{
3333 boolean_t cb_recurse
;
3334 } rollback_cbdata_t
;
3337 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3339 rollback_cbdata_t
*cbp
= data
;
3341 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3342 (void) fprintf(stderr
, gettext("cannot rollback to "
3343 "'%s': clones of previous snapshots exist\n"),
3345 (void) fprintf(stderr
, gettext("use '-R' to "
3346 "force deletion of the following clones and "
3352 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3359 * Report any snapshots more recent than the one specified. Used when '-r' is
3360 * not specified. We reuse this same callback for the snapshot dependents - if
3361 * 'cb_dependent' is set, then this is a dependent and we should report it
3362 * without checking the transaction group.
3365 rollback_check(zfs_handle_t
*zhp
, void *data
)
3367 rollback_cbdata_t
*cbp
= data
;
3369 if (cbp
->cb_doclones
) {
3374 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3375 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3376 (void) fprintf(stderr
, gettext("cannot "
3377 "rollback to '%s': more recent snapshots "
3378 "or bookmarks exist\n"),
3380 (void) fprintf(stderr
, gettext("use '-r' to "
3381 "force deletion of the following "
3382 "snapshots and bookmarks:\n"));
3387 if (cbp
->cb_recurse
) {
3388 if (zfs_iter_dependents(zhp
, B_TRUE
,
3389 rollback_check_dependent
, cbp
) != 0) {
3394 (void) fprintf(stderr
, "%s\n",
3403 zfs_do_rollback(int argc
, char **argv
)
3407 boolean_t force
= B_FALSE
;
3408 rollback_cbdata_t cb
= { 0 };
3409 zfs_handle_t
*zhp
, *snap
;
3410 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3414 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3427 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3436 /* check number of arguments */
3438 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3442 (void) fprintf(stderr
, gettext("too many arguments\n"));
3446 /* open the snapshot */
3447 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3450 /* open the parent dataset */
3451 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3452 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3454 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3460 * Check for more recent snapshots and/or clones based on the presence
3463 cb
.cb_target
= argv
[0];
3464 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3465 cb
.cb_first
= B_TRUE
;
3467 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, rollback_check
, &cb
)) != 0)
3469 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
3472 if ((ret
= cb
.cb_error
) != 0)
3476 * Rollback parent to the given snapshot.
3478 ret
= zfs_rollback(zhp
, snap
, force
);
3491 * zfs set property=value ... { fs | snap | vol } ...
3493 * Sets the given properties for all datasets specified on the command line.
3497 set_callback(zfs_handle_t
*zhp
, void *data
)
3499 nvlist_t
*props
= data
;
3501 if (zfs_prop_set_list(zhp
, props
) != 0) {
3502 switch (libzfs_errno(g_zfs
)) {
3503 case EZFS_MOUNTFAILED
:
3504 (void) fprintf(stderr
, gettext("property may be set "
3505 "but unable to remount filesystem\n"));
3507 case EZFS_SHARENFSFAILED
:
3508 (void) fprintf(stderr
, gettext("property may be set "
3509 "but unable to reshare filesystem\n"));
3518 zfs_do_set(int argc
, char **argv
)
3520 nvlist_t
*props
= NULL
;
3521 int ds_start
= -1; /* argv idx of first dataset arg */
3524 /* check for options */
3525 if (argc
> 1 && argv
[1][0] == '-') {
3526 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3531 /* check number of arguments */
3533 (void) fprintf(stderr
, gettext("missing arguments\n"));
3537 if (strchr(argv
[1], '=') == NULL
) {
3538 (void) fprintf(stderr
, gettext("missing property=value "
3541 (void) fprintf(stderr
, gettext("missing dataset "
3547 /* validate argument order: prop=val args followed by dataset args */
3548 for (int i
= 1; i
< argc
; i
++) {
3549 if (strchr(argv
[i
], '=') != NULL
) {
3551 /* out-of-order prop=val argument */
3552 (void) fprintf(stderr
, gettext("invalid "
3553 "argument order\n"), i
);
3556 } else if (ds_start
< 0) {
3561 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
3565 /* Populate a list of property settings */
3566 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3568 for (int i
= 1; i
< ds_start
; i
++) {
3569 if ((ret
= parseprop(props
, argv
[i
])) != 0)
3573 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
3574 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, props
);
3581 typedef struct snap_cbdata
{
3583 boolean_t sd_recursive
;
3584 const char *sd_snapname
;
3588 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3590 snap_cbdata_t
*sd
= arg
;
3595 if (sd
->sd_recursive
&&
3596 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
3601 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3604 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3607 if (sd
->sd_recursive
)
3608 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3614 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3616 * Creates a snapshot with the given name. While functionally equivalent to
3617 * 'zfs create', it is a separate command to differentiate intent.
3620 zfs_do_snapshot(int argc
, char **argv
)
3625 snap_cbdata_t sd
= { 0 };
3626 boolean_t multiple_snaps
= B_FALSE
;
3628 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3630 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3634 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3637 if (parseprop(props
, optarg
) != 0)
3641 sd
.sd_recursive
= B_TRUE
;
3642 multiple_snaps
= B_TRUE
;
3645 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3654 /* check number of arguments */
3656 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3661 multiple_snaps
= B_TRUE
;
3662 for (; argc
> 0; argc
--, argv
++) {
3666 atp
= strchr(argv
[0], '@');
3670 sd
.sd_snapname
= atp
+ 1;
3671 zhp
= zfs_open(g_zfs
, argv
[0],
3672 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3675 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3679 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3680 nvlist_free(sd
.sd_nvl
);
3682 if (ret
!= 0 && multiple_snaps
)
3683 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3687 nvlist_free(sd
.sd_nvl
);
3694 * Send a backup stream to stdout.
3697 zfs_do_send(int argc
, char **argv
)
3699 char *fromname
= NULL
;
3700 char *toname
= NULL
;
3701 char *resume_token
= NULL
;
3704 sendflags_t flags
= { 0 };
3706 nvlist_t
*dbgnv
= NULL
;
3707 boolean_t extraverbose
= B_FALSE
;
3709 struct option long_options
[] = {
3710 {"replicate", no_argument
, NULL
, 'R'},
3711 {"props", no_argument
, NULL
, 'p'},
3712 {"parsable", no_argument
, NULL
, 'P'},
3713 {"dedup", no_argument
, NULL
, 'D'},
3714 {"verbose", no_argument
, NULL
, 'v'},
3715 {"dryrun", no_argument
, NULL
, 'n'},
3716 {"large-block", no_argument
, NULL
, 'L'},
3717 {"embed", no_argument
, NULL
, 'e'},
3718 {"resume", required_argument
, NULL
, 't'},
3719 {"compressed", no_argument
, NULL
, 'c'},
3724 while ((c
= getopt_long(argc
, argv
, ":i:I:RbDpvnPLet:c", long_options
,
3736 flags
.doall
= B_TRUE
;
3739 flags
.replicate
= B_TRUE
;
3742 flags
.props
= B_TRUE
;
3745 flags
.parsable
= B_TRUE
;
3746 flags
.verbose
= B_TRUE
;
3750 extraverbose
= B_TRUE
;
3751 flags
.verbose
= B_TRUE
;
3752 flags
.progress
= B_TRUE
;
3755 flags
.dedup
= B_TRUE
;
3758 flags
.dryrun
= B_TRUE
;
3761 flags
.largeblock
= B_TRUE
;
3764 flags
.embed_data
= B_TRUE
;
3767 resume_token
= optarg
;
3770 flags
.compress
= B_TRUE
;
3774 * If a parameter was not passed, optopt contains the
3775 * value that would normally lead us into the
3776 * appropriate case statement. If it's > 256, then this
3777 * must be a longopt and we should look at argv to get
3778 * the string. Otherwise it's just the character, so we
3779 * should use it directly.
3781 if (optopt
<= UINT8_MAX
) {
3782 (void) fprintf(stderr
,
3783 gettext("missing argument for '%c' "
3784 "option\n"), optopt
);
3786 (void) fprintf(stderr
,
3787 gettext("missing argument for '%s' "
3788 "option\n"), argv
[optind
- 1]);
3796 * If an invalid flag was passed, optopt contains the
3797 * character if it was a short flag, or 0 if it was a
3801 (void) fprintf(stderr
,
3802 gettext("invalid option '%c'\n"), optopt
);
3804 (void) fprintf(stderr
,
3805 gettext("invalid option '%s'\n"),
3816 if (resume_token
!= NULL
) {
3817 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
3819 (void) fprintf(stderr
,
3820 gettext("invalid flags combined with -t\n"));
3824 (void) fprintf(stderr
, gettext("no additional "
3825 "arguments are permitted with -t\n"));
3830 (void) fprintf(stderr
,
3831 gettext("missing snapshot argument\n"));
3835 (void) fprintf(stderr
, gettext("too many arguments\n"));
3840 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
3841 (void) fprintf(stderr
,
3842 gettext("Error: Stream can not be written to a terminal.\n"
3843 "You must redirect standard output.\n"));
3847 if (resume_token
!= NULL
) {
3848 return (zfs_send_resume(g_zfs
, &flags
, STDOUT_FILENO
,
3853 * Special case sending a filesystem, or from a bookmark.
3855 if (strchr(argv
[0], '@') == NULL
||
3856 (fromname
&& strchr(fromname
, '#') != NULL
)) {
3857 char frombuf
[ZFS_MAX_DATASET_NAME_LEN
];
3858 enum lzc_send_flags lzc_flags
= 0;
3860 if (flags
.replicate
|| flags
.doall
|| flags
.props
||
3861 flags
.dedup
|| flags
.dryrun
|| flags
.verbose
||
3863 (void) fprintf(stderr
,
3865 "Unsupported flag with filesystem or bookmark.\n"));
3869 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
3873 if (flags
.largeblock
)
3874 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
3875 if (flags
.embed_data
)
3876 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
3878 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
3880 if (fromname
!= NULL
&&
3881 (fromname
[0] == '#' || fromname
[0] == '@')) {
3883 * Incremental source name begins with # or @.
3884 * Default to same fs as target.
3886 (void) strncpy(frombuf
, argv
[0], sizeof (frombuf
));
3887 cp
= strchr(frombuf
, '@');
3890 (void) strlcat(frombuf
, fromname
, sizeof (frombuf
));
3893 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, lzc_flags
);
3898 cp
= strchr(argv
[0], '@');
3901 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3906 * If they specified the full path to the snapshot, chop off
3907 * everything except the short name of the snapshot, but special
3908 * case if they specify the origin.
3910 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
3911 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
3914 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
3915 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
3917 if (strcmp(origin
, fromname
) == 0) {
3919 flags
.fromorigin
= B_TRUE
;
3922 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
3923 (void) fprintf(stderr
,
3924 gettext("incremental source must be "
3925 "in same filesystem\n"));
3929 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
3930 (void) fprintf(stderr
,
3931 gettext("invalid incremental source\n"));
3937 if (flags
.replicate
&& fromname
== NULL
)
3938 flags
.doall
= B_TRUE
;
3940 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
3941 extraverbose
? &dbgnv
: NULL
);
3943 if (extraverbose
&& dbgnv
!= NULL
) {
3945 * dump_nvlist prints to stdout, but that's been
3946 * redirected to a file. Make it print to stderr
3949 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
3950 dump_nvlist(dbgnv
, 0);
3959 * Restore a backup stream from stdin.
3962 zfs_do_receive(int argc
, char **argv
)
3965 recvflags_t flags
= { 0 };
3966 boolean_t abort_resumable
= B_FALSE
;
3969 nvpair_t
*nvp
= NULL
;
3971 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3975 while ((c
= getopt(argc
, argv
, ":o:denuvFsA")) != -1) {
3978 if (parseprop(props
, optarg
) != 0)
3982 flags
.isprefix
= B_TRUE
;
3985 flags
.isprefix
= B_TRUE
;
3986 flags
.istail
= B_TRUE
;
3989 flags
.dryrun
= B_TRUE
;
3992 flags
.nomount
= B_TRUE
;
3995 flags
.verbose
= B_TRUE
;
3998 flags
.resumable
= B_TRUE
;
4001 flags
.force
= B_TRUE
;
4004 abort_resumable
= B_TRUE
;
4007 (void) fprintf(stderr
, gettext("missing argument for "
4008 "'%c' option\n"), optopt
);
4012 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4021 /* check number of arguments */
4023 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4027 (void) fprintf(stderr
, gettext("too many arguments\n"));
4031 while ((nvp
= nvlist_next_nvpair(props
, nvp
))) {
4032 if (strcmp(nvpair_name(nvp
), "origin") != 0) {
4033 (void) fprintf(stderr
, gettext("invalid option"));
4038 if (abort_resumable
) {
4039 if (flags
.isprefix
|| flags
.istail
|| flags
.dryrun
||
4040 flags
.resumable
|| flags
.nomount
) {
4041 (void) fprintf(stderr
, gettext("invalid option"));
4045 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
4046 (void) snprintf(namebuf
, sizeof (namebuf
),
4047 "%s/%%recv", argv
[0]);
4049 if (zfs_dataset_exists(g_zfs
, namebuf
,
4050 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) {
4051 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4052 namebuf
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4055 err
= zfs_destroy(zhp
, B_FALSE
);
4057 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4058 argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4061 if (!zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) ||
4062 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
4063 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == -1) {
4064 (void) fprintf(stderr
,
4065 gettext("'%s' does not have any "
4066 "resumable receive state to abort\n"),
4070 err
= zfs_destroy(zhp
, B_FALSE
);
4076 if (isatty(STDIN_FILENO
)) {
4077 (void) fprintf(stderr
,
4078 gettext("Error: Backup stream can not be read "
4079 "from a terminal.\n"
4080 "You must redirect standard input.\n"));
4083 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
4089 * allow/unallow stuff
4091 /* copied from zfs/sys/dsl_deleg.h */
4092 #define ZFS_DELEG_PERM_CREATE "create"
4093 #define ZFS_DELEG_PERM_DESTROY "destroy"
4094 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4095 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4096 #define ZFS_DELEG_PERM_CLONE "clone"
4097 #define ZFS_DELEG_PERM_PROMOTE "promote"
4098 #define ZFS_DELEG_PERM_RENAME "rename"
4099 #define ZFS_DELEG_PERM_MOUNT "mount"
4100 #define ZFS_DELEG_PERM_SHARE "share"
4101 #define ZFS_DELEG_PERM_SEND "send"
4102 #define ZFS_DELEG_PERM_RECEIVE "receive"
4103 #define ZFS_DELEG_PERM_ALLOW "allow"
4104 #define ZFS_DELEG_PERM_USERPROP "userprop"
4105 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4106 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4107 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4108 #define ZFS_DELEG_PERM_USERUSED "userused"
4109 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4110 #define ZFS_DELEG_PERM_HOLD "hold"
4111 #define ZFS_DELEG_PERM_RELEASE "release"
4112 #define ZFS_DELEG_PERM_DIFF "diff"
4113 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4115 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4117 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
4118 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
4119 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
4120 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
4121 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
4122 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
4123 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
4124 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
4125 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
4126 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
4127 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
4128 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
4129 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
4130 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
4131 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
4132 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
4133 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
4135 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
4136 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
4137 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
4138 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
4139 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
4140 { NULL
, ZFS_DELEG_NOTE_NONE
}
4143 /* permission structure */
4144 typedef struct deleg_perm
{
4145 zfs_deleg_who_type_t dp_who_type
;
4146 const char *dp_name
;
4148 boolean_t dp_descend
;
4152 typedef struct deleg_perm_node
{
4153 deleg_perm_t dpn_perm
;
4155 uu_avl_node_t dpn_avl_node
;
4156 } deleg_perm_node_t
;
4158 typedef struct fs_perm fs_perm_t
;
4160 /* permissions set */
4161 typedef struct who_perm
{
4162 zfs_deleg_who_type_t who_type
;
4163 const char *who_name
; /* id */
4164 char who_ug_name
[256]; /* user/group name */
4165 fs_perm_t
*who_fsperm
; /* uplink */
4167 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
4171 typedef struct who_perm_node
{
4172 who_perm_t who_perm
;
4173 uu_avl_node_t who_avl_node
;
4176 typedef struct fs_perm_set fs_perm_set_t
;
4177 /* fs permissions */
4179 const char *fsp_name
;
4181 uu_avl_t
*fsp_sc_avl
; /* sets,create */
4182 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
4184 fs_perm_set_t
*fsp_set
; /* uplink */
4188 typedef struct fs_perm_node
{
4189 fs_perm_t fspn_fsperm
;
4192 uu_list_node_t fspn_list_node
;
4195 /* top level structure */
4196 struct fs_perm_set
{
4197 uu_list_pool_t
*fsps_list_pool
;
4198 uu_list_t
*fsps_list
; /* list of fs_perms */
4200 uu_avl_pool_t
*fsps_named_set_avl_pool
;
4201 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
4202 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
4205 static inline const char *
4206 deleg_perm_type(zfs_deleg_note_t note
)
4212 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4213 case ZFS_DELEG_NOTE_GROUPUSED
:
4214 case ZFS_DELEG_NOTE_USERPROP
:
4215 case ZFS_DELEG_NOTE_USERQUOTA
:
4216 case ZFS_DELEG_NOTE_USERUSED
:
4218 return (gettext("other"));
4220 return (gettext("subcommand"));
4225 who_type2weight(zfs_deleg_who_type_t who_type
)
4229 case ZFS_DELEG_NAMED_SET_SETS
:
4230 case ZFS_DELEG_NAMED_SET
:
4233 case ZFS_DELEG_CREATE_SETS
:
4234 case ZFS_DELEG_CREATE
:
4237 case ZFS_DELEG_USER_SETS
:
4238 case ZFS_DELEG_USER
:
4241 case ZFS_DELEG_GROUP_SETS
:
4242 case ZFS_DELEG_GROUP
:
4245 case ZFS_DELEG_EVERYONE_SETS
:
4246 case ZFS_DELEG_EVERYONE
:
4258 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4260 const who_perm_node_t
*l
= larg
;
4261 const who_perm_node_t
*r
= rarg
;
4262 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
4263 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
4264 int lweight
= who_type2weight(ltype
);
4265 int rweight
= who_type2weight(rtype
);
4266 int res
= lweight
- rweight
;
4268 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
4269 ZFS_MAX_DELEG_NAME
-1);
4281 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4283 const deleg_perm_node_t
*l
= larg
;
4284 const deleg_perm_node_t
*r
= rarg
;
4285 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
4286 ZFS_MAX_DELEG_NAME
-1);
4298 fs_perm_set_init(fs_perm_set_t
*fspset
)
4300 bzero(fspset
, sizeof (fs_perm_set_t
));
4302 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4303 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4304 NULL
, UU_DEFAULT
)) == NULL
)
4306 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4307 UU_DEFAULT
)) == NULL
)
4310 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4311 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4312 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4313 UU_DEFAULT
)) == NULL
)
4316 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4317 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4318 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4319 UU_DEFAULT
)) == NULL
)
4322 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4323 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4324 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4329 static inline void fs_perm_fini(fs_perm_t
*);
4330 static inline void who_perm_fini(who_perm_t
*);
4333 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4335 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4337 while (node
!= NULL
) {
4338 fs_perm_node_t
*next_node
=
4339 uu_list_next(fspset
->fsps_list
, node
);
4340 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4341 fs_perm_fini(fsperm
);
4342 uu_list_remove(fspset
->fsps_list
, node
);
4347 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4348 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4349 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4353 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4356 deleg_perm
->dp_who_type
= type
;
4357 deleg_perm
->dp_name
= name
;
4361 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4362 zfs_deleg_who_type_t type
, const char *name
)
4364 uu_avl_pool_t
*pool
;
4365 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4367 bzero(who_perm
, sizeof (who_perm_t
));
4369 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4370 UU_DEFAULT
)) == NULL
)
4373 who_perm
->who_type
= type
;
4374 who_perm
->who_name
= name
;
4375 who_perm
->who_fsperm
= fsperm
;
4379 who_perm_fini(who_perm_t
*who_perm
)
4381 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4383 while (node
!= NULL
) {
4384 deleg_perm_node_t
*next_node
=
4385 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4387 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4392 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4396 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4398 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4399 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4401 bzero(fsperm
, sizeof (fs_perm_t
));
4403 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4407 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4411 fsperm
->fsp_set
= fspset
;
4412 fsperm
->fsp_name
= fsname
;
4416 fs_perm_fini(fs_perm_t
*fsperm
)
4418 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4419 while (node
!= NULL
) {
4420 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4422 who_perm_t
*who_perm
= &node
->who_perm
;
4423 who_perm_fini(who_perm
);
4424 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4429 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4430 while (node
!= NULL
) {
4431 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4433 who_perm_t
*who_perm
= &node
->who_perm
;
4434 who_perm_fini(who_perm
);
4435 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4440 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4441 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4445 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4446 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4448 uu_avl_index_t idx
= 0;
4450 deleg_perm_node_t
*found_node
= NULL
;
4451 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4453 deleg_perm_init(deleg_perm
, who_type
, name
);
4455 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4457 uu_avl_insert(avl
, node
, idx
);
4460 deleg_perm
= &node
->dpn_perm
;
4465 case ZFS_DELEG_LOCAL
:
4466 deleg_perm
->dp_local
= B_TRUE
;
4468 case ZFS_DELEG_DESCENDENT
:
4469 deleg_perm
->dp_descend
= B_TRUE
;
4474 assert(B_FALSE
); /* invalid locality */
4479 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4481 nvpair_t
*nvp
= NULL
;
4482 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4483 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4484 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4486 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4487 const char *name
= nvpair_name(nvp
);
4488 data_type_t type
= nvpair_type(nvp
);
4489 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4490 deleg_perm_node_t
*node
=
4491 safe_malloc(sizeof (deleg_perm_node_t
));
4493 assert(type
== DATA_TYPE_BOOLEAN
);
4495 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4496 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4503 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4505 nvpair_t
*nvp
= NULL
;
4506 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4508 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4509 nvlist_t
*nvl2
= NULL
;
4510 const char *name
= nvpair_name(nvp
);
4511 uu_avl_t
*avl
= NULL
;
4512 uu_avl_pool_t
*avl_pool
= NULL
;
4513 zfs_deleg_who_type_t perm_type
= name
[0];
4514 char perm_locality
= name
[1];
4515 const char *perm_name
= name
+ 3;
4516 boolean_t is_set
= B_TRUE
;
4517 who_perm_t
*who_perm
= NULL
;
4519 assert('$' == name
[2]);
4521 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4524 switch (perm_type
) {
4525 case ZFS_DELEG_CREATE
:
4526 case ZFS_DELEG_CREATE_SETS
:
4527 case ZFS_DELEG_NAMED_SET
:
4528 case ZFS_DELEG_NAMED_SET_SETS
:
4529 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4530 avl
= fsperm
->fsp_sc_avl
;
4532 case ZFS_DELEG_USER
:
4533 case ZFS_DELEG_USER_SETS
:
4534 case ZFS_DELEG_GROUP
:
4535 case ZFS_DELEG_GROUP_SETS
:
4536 case ZFS_DELEG_EVERYONE
:
4537 case ZFS_DELEG_EVERYONE_SETS
:
4538 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4539 avl
= fsperm
->fsp_uge_avl
;
4543 assert(!"unhandled zfs_deleg_who_type_t");
4547 who_perm_node_t
*found_node
= NULL
;
4548 who_perm_node_t
*node
= safe_malloc(
4549 sizeof (who_perm_node_t
));
4550 who_perm
= &node
->who_perm
;
4551 uu_avl_index_t idx
= 0;
4553 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4554 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4556 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4558 if (avl
== fsperm
->fsp_uge_avl
) {
4560 struct passwd
*p
= NULL
;
4561 struct group
*g
= NULL
;
4562 const char *nice_name
= NULL
;
4564 switch (perm_type
) {
4565 case ZFS_DELEG_USER_SETS
:
4566 case ZFS_DELEG_USER
:
4567 rid
= atoi(perm_name
);
4570 nice_name
= p
->pw_name
;
4572 case ZFS_DELEG_GROUP_SETS
:
4573 case ZFS_DELEG_GROUP
:
4574 rid
= atoi(perm_name
);
4577 nice_name
= g
->gr_name
;
4584 if (nice_name
!= NULL
)
4586 node
->who_perm
.who_ug_name
,
4590 uu_avl_insert(avl
, node
, idx
);
4593 who_perm
= &node
->who_perm
;
4597 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4604 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4606 nvpair_t
*nvp
= NULL
;
4607 uu_avl_index_t idx
= 0;
4609 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4610 nvlist_t
*nvl2
= NULL
;
4611 const char *fsname
= nvpair_name(nvp
);
4612 data_type_t type
= nvpair_type(nvp
);
4613 fs_perm_t
*fsperm
= NULL
;
4614 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4618 fsperm
= &node
->fspn_fsperm
;
4620 assert(DATA_TYPE_NVLIST
== type
);
4622 uu_list_node_init(node
, &node
->fspn_list_node
,
4623 fspset
->fsps_list_pool
);
4625 idx
= uu_list_numnodes(fspset
->fsps_list
);
4626 fs_perm_init(fsperm
, fspset
, fsname
);
4628 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4631 (void) parse_fs_perm(fsperm
, nvl2
);
4633 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4639 static inline const char *
4640 deleg_perm_comment(zfs_deleg_note_t note
)
4642 const char *str
= "";
4647 case ZFS_DELEG_NOTE_ALLOW
:
4648 str
= gettext("Must also have the permission that is being"
4649 "\n\t\t\t\tallowed");
4651 case ZFS_DELEG_NOTE_CLONE
:
4652 str
= gettext("Must also have the 'create' ability and 'mount'"
4653 "\n\t\t\t\tability in the origin file system");
4655 case ZFS_DELEG_NOTE_CREATE
:
4656 str
= gettext("Must also have the 'mount' ability");
4658 case ZFS_DELEG_NOTE_DESTROY
:
4659 str
= gettext("Must also have the 'mount' ability");
4661 case ZFS_DELEG_NOTE_DIFF
:
4662 str
= gettext("Allows lookup of paths within a dataset;"
4663 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4664 "\n\t\t\t\tin order to use zfs diff");
4666 case ZFS_DELEG_NOTE_HOLD
:
4667 str
= gettext("Allows adding a user hold to a snapshot");
4669 case ZFS_DELEG_NOTE_MOUNT
:
4670 str
= gettext("Allows mount/umount of ZFS datasets");
4672 case ZFS_DELEG_NOTE_PROMOTE
:
4673 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4674 " 'promote' ability in the origin file system");
4676 case ZFS_DELEG_NOTE_RECEIVE
:
4677 str
= gettext("Must also have the 'mount' and 'create'"
4680 case ZFS_DELEG_NOTE_RELEASE
:
4681 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4682 "might destroy the snapshot");
4684 case ZFS_DELEG_NOTE_RENAME
:
4685 str
= gettext("Must also have the 'mount' and 'create'"
4686 "\n\t\t\t\tability in the new parent");
4688 case ZFS_DELEG_NOTE_ROLLBACK
:
4691 case ZFS_DELEG_NOTE_SEND
:
4694 case ZFS_DELEG_NOTE_SHARE
:
4695 str
= gettext("Allows sharing file systems over NFS or SMB"
4696 "\n\t\t\t\tprotocols");
4698 case ZFS_DELEG_NOTE_SNAPSHOT
:
4702 * case ZFS_DELEG_NOTE_VSCAN:
4703 * str = gettext("");
4707 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4708 str
= gettext("Allows accessing any groupquota@... property");
4710 case ZFS_DELEG_NOTE_GROUPUSED
:
4711 str
= gettext("Allows reading any groupused@... property");
4713 case ZFS_DELEG_NOTE_USERPROP
:
4714 str
= gettext("Allows changing any user property");
4716 case ZFS_DELEG_NOTE_USERQUOTA
:
4717 str
= gettext("Allows accessing any userquota@... property");
4719 case ZFS_DELEG_NOTE_USERUSED
:
4720 str
= gettext("Allows reading any userused@... property");
4738 boolean_t recursive
; /* unallow only */
4739 boolean_t prt_usage
;
4741 boolean_t prt_perms
;
4744 const char *dataset
;
4748 prop_cmp(const void *a
, const void *b
)
4750 const char *str1
= *(const char **)a
;
4751 const char *str2
= *(const char **)b
;
4752 return (strcmp(str1
, str2
));
4756 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
4758 const char *opt_desc
[] = {
4759 "-h", gettext("show this help message and exit"),
4760 "-l", gettext("set permission locally"),
4761 "-d", gettext("set permission for descents"),
4762 "-u", gettext("set permission for user"),
4763 "-g", gettext("set permission for group"),
4764 "-e", gettext("set permission for everyone"),
4765 "-c", gettext("set create time permission"),
4766 "-s", gettext("define permission set"),
4768 "-r", gettext("remove permissions recursively"),
4770 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
4771 size_t allow_size
= unallow_size
- 2;
4772 const char *props
[ZFS_NUM_PROPS
];
4775 FILE *fp
= requested
? stdout
: stderr
;
4776 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
4777 const char *fmt
= gettext("%-16s %-14s\t%s\n");
4779 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
4781 (void) fprintf(fp
, gettext("Options:\n"));
4782 for (int i
= 0; i
< (un
? unallow_size
: allow_size
); i
++) {
4783 const char *opt
= opt_desc
[i
++];
4784 const char *optdsc
= opt_desc
[i
];
4785 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
4788 (void) fprintf(fp
, gettext("\nThe following permissions are "
4790 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
4792 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
4793 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
4794 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
4795 const char *perm_type
= deleg_perm_type(perm_note
);
4796 const char *perm_comment
= deleg_perm_comment(perm_note
);
4797 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
4800 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
4801 zprop_desc_t
*pd
= &pdtbl
[i
];
4802 if (pd
->pd_visible
!= B_TRUE
)
4805 if (pd
->pd_attr
== PROP_READONLY
)
4808 props
[count
++] = pd
->pd_name
;
4810 props
[count
] = NULL
;
4812 qsort(props
, count
, sizeof (char *), prop_cmp
);
4814 for (i
= 0; i
< count
; i
++)
4815 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
4818 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
4820 exit(requested
? 0 : 2);
4823 static inline const char *
4824 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
4827 if (un
&& argc
== expected_argc
- 1)
4829 else if (argc
== expected_argc
)
4830 *permsp
= argv
[argc
- 2];
4832 allow_usage(un
, B_FALSE
,
4833 gettext("wrong number of parameters\n"));
4835 return (argv
[argc
- 1]);
4839 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
4841 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
4842 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
4843 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
4844 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
4847 allow_usage(un
, B_FALSE
,
4848 gettext("-u, -g, and -e are mutually exclusive\n"));
4850 if (opts
->prt_usage
) {
4851 if (argc
== 0 && all_sum
== 0)
4852 allow_usage(un
, B_TRUE
, NULL
);
4859 allow_usage(un
, B_FALSE
,
4860 gettext("invalid options combined with -s\n"));
4862 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4863 if (argv
[0][0] != '@')
4864 allow_usage(un
, B_FALSE
,
4865 gettext("invalid set name: missing '@' prefix\n"));
4866 opts
->who
= argv
[0];
4867 } else if (opts
->create
) {
4868 if (ldcsuge_sum
> 1)
4869 allow_usage(un
, B_FALSE
,
4870 gettext("invalid options combined with -c\n"));
4871 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4872 } else if (opts
->everyone
) {
4874 allow_usage(un
, B_FALSE
,
4875 gettext("invalid options combined with -e\n"));
4876 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4877 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
4879 opts
->everyone
= B_TRUE
;
4882 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4883 } else if (argc
== 1 && !un
) {
4884 opts
->prt_perms
= B_TRUE
;
4885 opts
->dataset
= argv
[argc
-1];
4887 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4888 opts
->who
= argv
[0];
4891 if (!opts
->local
&& !opts
->descend
) {
4892 opts
->local
= B_TRUE
;
4893 opts
->descend
= B_TRUE
;
4898 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
4899 const char *who
, char *perms
, nvlist_t
*top_nvl
)
4902 char ld
[2] = { '\0', '\0' };
4903 char who_buf
[MAXNAMELEN
+ 32];
4904 char base_type
= '\0';
4905 char set_type
= '\0';
4906 nvlist_t
*base_nvl
= NULL
;
4907 nvlist_t
*set_nvl
= NULL
;
4910 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4912 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4916 case ZFS_DELEG_NAMED_SET_SETS
:
4917 case ZFS_DELEG_NAMED_SET
:
4918 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
4919 base_type
= ZFS_DELEG_NAMED_SET
;
4920 ld
[0] = ZFS_DELEG_NA
;
4922 case ZFS_DELEG_CREATE_SETS
:
4923 case ZFS_DELEG_CREATE
:
4924 set_type
= ZFS_DELEG_CREATE_SETS
;
4925 base_type
= ZFS_DELEG_CREATE
;
4926 ld
[0] = ZFS_DELEG_NA
;
4928 case ZFS_DELEG_USER_SETS
:
4929 case ZFS_DELEG_USER
:
4930 set_type
= ZFS_DELEG_USER_SETS
;
4931 base_type
= ZFS_DELEG_USER
;
4933 ld
[0] = ZFS_DELEG_LOCAL
;
4935 ld
[1] = ZFS_DELEG_DESCENDENT
;
4937 case ZFS_DELEG_GROUP_SETS
:
4938 case ZFS_DELEG_GROUP
:
4939 set_type
= ZFS_DELEG_GROUP_SETS
;
4940 base_type
= ZFS_DELEG_GROUP
;
4942 ld
[0] = ZFS_DELEG_LOCAL
;
4944 ld
[1] = ZFS_DELEG_DESCENDENT
;
4946 case ZFS_DELEG_EVERYONE_SETS
:
4947 case ZFS_DELEG_EVERYONE
:
4948 set_type
= ZFS_DELEG_EVERYONE_SETS
;
4949 base_type
= ZFS_DELEG_EVERYONE
;
4951 ld
[0] = ZFS_DELEG_LOCAL
;
4953 ld
[1] = ZFS_DELEG_DESCENDENT
;
4957 assert(set_type
!= '\0' && base_type
!= '\0');
4960 if (perms
!= NULL
) {
4962 char *end
= curr
+ strlen(perms
);
4964 while (curr
< end
) {
4965 char *delim
= strchr(curr
, ',');
4976 (void) nvlist_add_boolean(nvl
, curr
);
4982 for (i
= 0; i
< 2; i
++) {
4983 char locality
= ld
[i
];
4987 if (!nvlist_empty(base_nvl
)) {
4989 (void) snprintf(who_buf
,
4990 sizeof (who_buf
), "%c%c$%s",
4991 base_type
, locality
, who
);
4993 (void) snprintf(who_buf
,
4994 sizeof (who_buf
), "%c%c$",
4995 base_type
, locality
);
4997 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5002 if (!nvlist_empty(set_nvl
)) {
5004 (void) snprintf(who_buf
,
5005 sizeof (who_buf
), "%c%c$%s",
5006 set_type
, locality
, who
);
5008 (void) snprintf(who_buf
,
5009 sizeof (who_buf
), "%c%c$",
5010 set_type
, locality
);
5012 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5017 for (i
= 0; i
< 2; i
++) {
5018 char locality
= ld
[i
];
5023 (void) snprintf(who_buf
, sizeof (who_buf
),
5024 "%c%c$%s", base_type
, locality
, who
);
5026 (void) snprintf(who_buf
, sizeof (who_buf
),
5027 "%c%c$", base_type
, locality
);
5028 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5031 (void) snprintf(who_buf
, sizeof (who_buf
),
5032 "%c%c$%s", set_type
, locality
, who
);
5034 (void) snprintf(who_buf
, sizeof (who_buf
),
5035 "%c%c$", set_type
, locality
);
5036 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5042 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
5044 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
5048 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
5049 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
5050 } else if (opts
->create
) {
5051 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
5052 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5053 } else if (opts
->everyone
) {
5054 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
5055 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5057 char *curr
= opts
->who
;
5058 char *end
= curr
+ strlen(curr
);
5060 while (curr
< end
) {
5062 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
5064 char *delim
= strchr(curr
, ',');
5067 struct passwd
*p
= NULL
;
5068 struct group
*g
= NULL
;
5076 rid
= (uid_t
)strtol(curr
, &endch
, 0);
5078 who_type
= ZFS_DELEG_USER
;
5087 (void) snprintf(errbuf
, 256, gettext(
5088 "invalid user %s"), curr
);
5089 allow_usage(un
, B_TRUE
, errbuf
);
5091 } else if (opts
->group
) {
5092 who_type
= ZFS_DELEG_GROUP
;
5101 (void) snprintf(errbuf
, 256, gettext(
5102 "invalid group %s"), curr
);
5103 allow_usage(un
, B_TRUE
, errbuf
);
5106 if (*endch
!= '\0') {
5113 if (*endch
!= '\0') {
5121 who_type
= ZFS_DELEG_USER
;
5123 } else if (g
!= NULL
) {
5124 who_type
= ZFS_DELEG_GROUP
;
5127 (void) snprintf(errbuf
, 256, gettext(
5128 "invalid user/group %s"), curr
);
5129 allow_usage(un
, B_TRUE
, errbuf
);
5133 (void) sprintf(id
, "%u", rid
);
5136 store_allow_perm(who_type
, opts
->local
,
5137 opts
->descend
, who
, opts
->perms
, *nvlp
);
5146 print_set_creat_perms(uu_avl_t
*who_avl
)
5148 const char *sc_title
[] = {
5149 gettext("Permission sets:\n"),
5150 gettext("Create time permissions:\n"),
5153 const char **title_ptr
= sc_title
;
5154 who_perm_node_t
*who_node
= NULL
;
5155 int prev_weight
= -1;
5157 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
5158 who_node
= uu_avl_next(who_avl
, who_node
)) {
5159 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5160 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5161 const char *who_name
= who_node
->who_perm
.who_name
;
5162 int weight
= who_type2weight(who_type
);
5163 boolean_t first
= B_TRUE
;
5164 deleg_perm_node_t
*deleg_node
;
5166 if (prev_weight
!= weight
) {
5167 (void) printf(*title_ptr
++);
5168 prev_weight
= weight
;
5171 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
5172 (void) printf("\t");
5174 (void) printf("\t%s ", who_name
);
5176 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
5177 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5180 deleg_node
->dpn_perm
.dp_name
);
5183 (void) printf(",%s",
5184 deleg_node
->dpn_perm
.dp_name
);
5187 (void) printf("\n");
5192 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
5195 who_perm_node_t
*who_node
= NULL
;
5196 boolean_t prt_title
= B_TRUE
;
5197 uu_avl_walk_t
*walk
;
5199 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
5202 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
5203 const char *who_name
= who_node
->who_perm
.who_name
;
5204 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
5205 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5206 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5208 deleg_perm_node_t
*deleg_node
;
5209 boolean_t prt_who
= B_TRUE
;
5211 for (deleg_node
= uu_avl_first(avl
);
5213 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5214 if (local
!= deleg_node
->dpn_perm
.dp_local
||
5215 descend
!= deleg_node
->dpn_perm
.dp_descend
)
5219 const char *who
= NULL
;
5221 prt_title
= B_FALSE
;
5222 (void) printf(title
);
5226 case ZFS_DELEG_USER_SETS
:
5227 case ZFS_DELEG_USER
:
5228 who
= gettext("user");
5230 who_name
= nice_who_name
;
5232 case ZFS_DELEG_GROUP_SETS
:
5233 case ZFS_DELEG_GROUP
:
5234 who
= gettext("group");
5236 who_name
= nice_who_name
;
5238 case ZFS_DELEG_EVERYONE_SETS
:
5239 case ZFS_DELEG_EVERYONE
:
5240 who
= gettext("everyone");
5245 assert(who
!= NULL
);
5249 if (who_name
== NULL
)
5250 (void) printf("\t%s", who
);
5252 (void) printf("\t%s %s", who
, who_name
);
5255 (void) printf("%c%s", delim
,
5256 deleg_node
->dpn_perm
.dp_name
);
5261 (void) printf("\n");
5264 uu_avl_walk_end(walk
);
5268 print_fs_perms(fs_perm_set_t
*fspset
)
5270 fs_perm_node_t
*node
= NULL
;
5271 char buf
[MAXNAMELEN
+ 32];
5272 const char *dsname
= buf
;
5274 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
5275 node
= uu_list_next(fspset
->fsps_list
, node
)) {
5276 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
5277 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
5280 (void) snprintf(buf
, sizeof (buf
),
5281 gettext("---- Permissions on %s "),
5282 node
->fspn_fsperm
.fsp_name
);
5283 (void) printf(dsname
);
5284 left
= 70 - strlen(buf
);
5287 (void) printf("\n");
5289 print_set_creat_perms(sc_avl
);
5290 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
5291 gettext("Local permissions:\n"));
5292 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
5293 gettext("Descendent permissions:\n"));
5294 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
5295 gettext("Local+Descendent permissions:\n"));
5299 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5301 struct deleg_perms
{
5307 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5309 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5310 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5312 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5315 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5319 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5322 nvlist_t
*perm_nvl
= NULL
;
5323 nvlist_t
*update_perm_nvl
= NULL
;
5326 struct allow_opts opts
= { 0 };
5328 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5331 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5334 opts
.local
= B_TRUE
;
5337 opts
.descend
= B_TRUE
;
5343 opts
.group
= B_TRUE
;
5346 opts
.everyone
= B_TRUE
;
5352 opts
.create
= B_TRUE
;
5355 opts
.recursive
= B_TRUE
;
5358 (void) fprintf(stderr
, gettext("missing argument for "
5359 "'%c' option\n"), optopt
);
5363 opts
.prt_usage
= B_TRUE
;
5366 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5375 /* check arguments */
5376 parse_allow_args(argc
, argv
, un
, &opts
);
5378 /* try to open the dataset */
5379 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5380 ZFS_TYPE_VOLUME
)) == NULL
) {
5381 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5386 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5389 fs_perm_set_init(&fs_perm_set
);
5390 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5391 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5396 print_fs_perms(&fs_perm_set
);
5398 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5399 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5402 if (un
&& opts
.recursive
) {
5403 struct deleg_perms data
= { un
, update_perm_nvl
};
5404 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5413 nvlist_free(perm_nvl
);
5414 nvlist_free(update_perm_nvl
);
5416 fs_perm_set_fini(&fs_perm_set
);
5424 zfs_do_allow(int argc
, char **argv
)
5426 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5430 zfs_do_unallow(int argc
, char **argv
)
5432 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5436 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5441 boolean_t recursive
= B_FALSE
;
5442 const char *opts
= holding
? "rt" : "r";
5446 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5452 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5461 /* check number of arguments */
5469 if (holding
&& tag
[0] == '.') {
5470 /* tags starting with '.' are reserved for libzfs */
5471 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5475 for (i
= 0; i
< argc
; ++i
) {
5477 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
5479 char *path
= argv
[i
];
5481 delim
= strchr(path
, '@');
5482 if (delim
== NULL
) {
5483 (void) fprintf(stderr
,
5484 gettext("'%s' is not a snapshot\n"), path
);
5488 (void) strncpy(parent
, path
, delim
- path
);
5489 parent
[delim
- path
] = '\0';
5491 zhp
= zfs_open(g_zfs
, parent
,
5492 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5498 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
5501 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5507 return (errors
!= 0);
5511 * zfs hold [-r] [-t] <tag> <snap> ...
5513 * -r Recursively hold
5515 * Apply a user-hold with the given tag to the list of snapshots.
5518 zfs_do_hold(int argc
, char **argv
)
5520 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5524 * zfs release [-r] <tag> <snap> ...
5526 * -r Recursively release
5528 * Release a user-hold with the given tag from the list of snapshots.
5531 zfs_do_release(int argc
, char **argv
)
5533 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5536 typedef struct holds_cbdata
{
5537 boolean_t cb_recursive
;
5538 const char *cb_snapname
;
5540 size_t cb_max_namelen
;
5541 size_t cb_max_taglen
;
5544 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5545 #define DATETIME_BUF_LEN (32)
5550 print_holds(boolean_t scripted
, size_t nwidth
, size_t tagwidth
, nvlist_t
*nvl
)
5553 nvpair_t
*nvp
= NULL
;
5554 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5558 for (i
= 0; i
< 3; i
++) {
5559 col
= gettext(hdr_cols
[i
]);
5561 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5564 (void) printf("%s\n", col
);
5568 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5569 char *zname
= nvpair_name(nvp
);
5571 nvpair_t
*nvp2
= NULL
;
5572 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5573 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5574 char tsbuf
[DATETIME_BUF_LEN
];
5575 char *tagname
= nvpair_name(nvp2
);
5579 char sep
= scripted
? '\t' : ' ';
5580 size_t sepnum
= scripted
? 1 : 2;
5582 (void) nvpair_value_uint64(nvp2
, &val
);
5584 (void) localtime_r(&time
, &t
);
5585 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5586 gettext(STRFTIME_FMT_STR
), &t
);
5588 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth
, zname
,
5589 sepnum
, sep
, tagwidth
, tagname
, sepnum
, sep
, tsbuf
);
5595 * Generic callback function to list a dataset or snapshot.
5598 holds_callback(zfs_handle_t
*zhp
, void *data
)
5600 holds_cbdata_t
*cbp
= data
;
5601 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5602 nvlist_t
*nvl
= NULL
;
5603 nvpair_t
*nvp
= NULL
;
5604 const char *zname
= zfs_get_name(zhp
);
5605 size_t znamelen
= strlen(zname
);
5607 if (cbp
->cb_recursive
) {
5608 const char *snapname
;
5609 char *delim
= strchr(zname
, '@');
5613 snapname
= delim
+ 1;
5614 if (strcmp(cbp
->cb_snapname
, snapname
))
5618 if (zfs_get_holds(zhp
, &nvl
) != 0)
5621 if (znamelen
> cbp
->cb_max_namelen
)
5622 cbp
->cb_max_namelen
= znamelen
;
5624 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5625 const char *tag
= nvpair_name(nvp
);
5626 size_t taglen
= strlen(tag
);
5627 if (taglen
> cbp
->cb_max_taglen
)
5628 cbp
->cb_max_taglen
= taglen
;
5631 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5635 * zfs holds [-r] <snap> ...
5637 * -r Recursively hold
5640 zfs_do_holds(int argc
, char **argv
)
5645 boolean_t scripted
= B_FALSE
;
5646 boolean_t recursive
= B_FALSE
;
5647 const char *opts
= "rH";
5650 int types
= ZFS_TYPE_SNAPSHOT
;
5651 holds_cbdata_t cb
= { 0 };
5658 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5667 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5674 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
5675 flags
|= ZFS_ITER_RECURSE
;
5681 /* check number of arguments */
5685 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5688 for (i
= 0; i
< argc
; ++i
) {
5689 char *snapshot
= argv
[i
];
5691 const char *snapname
;
5693 delim
= strchr(snapshot
, '@');
5694 if (delim
== NULL
) {
5695 (void) fprintf(stderr
,
5696 gettext("'%s' is not a snapshot\n"), snapshot
);
5700 snapname
= delim
+ 1;
5702 snapshot
[delim
- snapshot
] = '\0';
5704 cb
.cb_recursive
= recursive
;
5705 cb
.cb_snapname
= snapname
;
5709 * 1. collect holds data, set format options
5711 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
5712 holds_callback
, &cb
);
5718 * 2. print holds data
5720 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
5722 if (nvlist_empty(nvl
))
5723 (void) printf(gettext("no datasets available\n"));
5727 return (0 != errors
);
5730 #define CHECK_SPINNER 30
5731 #define SPINNER_TIME 3 /* seconds */
5732 #define MOUNT_TIME 5 /* seconds */
5735 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
5737 static char *spin
[] = { "-", "\\", "|", "/" };
5738 static int spinval
= 0;
5739 static int spincheck
= 0;
5740 static time_t last_spin_time
= (time_t)0;
5741 get_all_cb_t
*cbp
= data
;
5742 zfs_type_t type
= zfs_get_type(zhp
);
5744 if (cbp
->cb_verbose
) {
5745 if (--spincheck
< 0) {
5746 time_t now
= time(NULL
);
5747 if (last_spin_time
+ SPINNER_TIME
< now
) {
5748 update_progress(spin
[spinval
++ % 4]);
5749 last_spin_time
= now
;
5751 spincheck
= CHECK_SPINNER
;
5756 * Interate over any nested datasets.
5758 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
5764 * Skip any datasets whose type does not match.
5766 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
5770 libzfs_add_handle(cbp
, zhp
);
5771 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
5777 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
5779 get_all_cb_t cb
= { 0 };
5780 cb
.cb_verbose
= verbose
;
5781 cb
.cb_getone
= get_one_dataset
;
5784 set_progress_header(gettext("Reading ZFS config"));
5785 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
5787 *dslist
= cb
.cb_handles
;
5788 *count
= cb
.cb_used
;
5791 finish_progress(gettext("done."));
5795 * Generic callback for sharing or mounting filesystems. Because the code is so
5796 * similar, we have a common function with an extra parameter to determine which
5797 * mode we are using.
5799 #define OP_SHARE 0x1
5800 #define OP_MOUNT 0x2
5803 * Share or mount a dataset.
5806 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
5807 boolean_t
explicit, const char *options
)
5809 char mountpoint
[ZFS_MAXPROPLEN
];
5810 char shareopts
[ZFS_MAXPROPLEN
];
5811 char smbshareopts
[ZFS_MAXPROPLEN
];
5812 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
5814 uint64_t zoned
, canmount
;
5815 boolean_t shared_nfs
, shared_smb
;
5817 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
5820 * Check to make sure we can mount/share this dataset. If we
5821 * are in the global zone and the filesystem is exported to a
5822 * local zone, or if we are in a local zone and the
5823 * filesystem is not exported, then it is an error.
5825 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
5827 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
5831 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5832 "dataset is exported to a local zone\n"), cmdname
,
5836 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
5840 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5841 "permission denied\n"), cmdname
,
5847 * Ignore any filesystems which don't apply to us. This
5848 * includes those with a legacy mountpoint, or those with
5849 * legacy share options.
5851 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
5852 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
5853 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
5854 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5855 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
5856 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5858 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
5859 strcmp(smbshareopts
, "off") == 0) {
5863 (void) fprintf(stderr
, gettext("cannot share '%s': "
5864 "legacy share\n"), zfs_get_name(zhp
));
5865 (void) fprintf(stderr
, gettext("use share(1M) to "
5866 "share this filesystem, or set "
5867 "sharenfs property on\n"));
5872 * We cannot share or mount legacy filesystems. If the
5873 * shareopts is non-legacy but the mountpoint is legacy, we
5874 * treat it as a legacy share.
5876 if (strcmp(mountpoint
, "legacy") == 0) {
5880 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5881 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
5882 (void) fprintf(stderr
, gettext("use %s(1M) to "
5883 "%s this filesystem\n"), cmdname
, cmdname
);
5887 if (strcmp(mountpoint
, "none") == 0) {
5891 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
5892 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
5897 * canmount explicit outcome
5898 * on no pass through
5899 * on yes pass through
5901 * off yes display error, return 1
5902 * noauto no return 0
5903 * noauto yes pass through
5905 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
5906 if (canmount
== ZFS_CANMOUNT_OFF
) {
5910 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5911 "'canmount' property is set to 'off'\n"), cmdname
,
5914 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
5919 * If this filesystem is inconsistent and has a receive resume
5920 * token, we can not mount it.
5922 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
5923 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
5924 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
5928 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5929 "Contains partially-completed state from "
5930 "\"zfs receive -r\", which can be resumed with "
5931 "\"zfs send -t\"\n"),
5932 cmdname
, zfs_get_name(zhp
));
5937 * At this point, we have verified that the mountpoint and/or
5938 * shareopts are appropriate for auto management. If the
5939 * filesystem is already mounted or shared, return (failing
5940 * for explicit requests); otherwise mount or share the
5946 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
5947 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
5949 if ((shared_nfs
&& shared_smb
) ||
5950 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
5951 strcmp(smbshareopts
, "off") == 0) ||
5952 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
5953 strcmp(shareopts
, "off") == 0)) {
5957 (void) fprintf(stderr
, gettext("cannot share "
5958 "'%s': filesystem already shared\n"),
5963 if (!zfs_is_mounted(zhp
, NULL
) &&
5964 zfs_mount(zhp
, NULL
, 0) != 0)
5967 if (protocol
== NULL
) {
5968 if (zfs_shareall(zhp
) != 0)
5970 } else if (strcmp(protocol
, "nfs") == 0) {
5971 if (zfs_share_nfs(zhp
))
5973 } else if (strcmp(protocol
, "smb") == 0) {
5974 if (zfs_share_smb(zhp
))
5977 (void) fprintf(stderr
, gettext("cannot share "
5978 "'%s': invalid share type '%s' "
5980 zfs_get_name(zhp
), protocol
);
5987 if (options
== NULL
)
5988 mnt
.mnt_mntopts
= "";
5990 mnt
.mnt_mntopts
= (char *)options
;
5992 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
5993 zfs_is_mounted(zhp
, NULL
)) {
5997 (void) fprintf(stderr
, gettext("cannot mount "
5998 "'%s': filesystem already mounted\n"),
6003 if (zfs_mount(zhp
, options
, flags
) != 0)
6012 * Reports progress in the form "(current/total)". Not thread-safe.
6015 report_mount_progress(int current
, int total
)
6017 static time_t last_progress_time
= 0;
6018 time_t now
= time(NULL
);
6021 /* report 1..n instead of 0..n-1 */
6024 /* display header if we're here for the first time */
6026 set_progress_header(gettext("Mounting ZFS filesystems"));
6027 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
6028 /* too soon to report again */
6032 last_progress_time
= now
;
6034 (void) sprintf(info
, "(%d/%d)", current
, total
);
6036 if (current
== total
)
6037 finish_progress(info
);
6039 update_progress(info
);
6043 append_options(char *mntopts
, char *newopts
)
6045 int len
= strlen(mntopts
);
6047 /* original length plus new string to append plus 1 for the comma */
6048 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
6049 (void) fprintf(stderr
, gettext("the opts argument for "
6050 "'%c' option is too long (more than %d chars)\n"),
6051 "-o", MNT_LINE_MAX
);
6056 mntopts
[len
++] = ',';
6058 (void) strcpy(&mntopts
[len
], newopts
);
6062 share_mount(int op
, int argc
, char **argv
)
6065 boolean_t verbose
= B_FALSE
;
6067 char *options
= NULL
;
6071 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
6081 if (*optarg
== '\0') {
6082 (void) fprintf(stderr
, gettext("empty mount "
6083 "options (-o) specified\n"));
6087 if (options
== NULL
)
6088 options
= safe_malloc(MNT_LINE_MAX
+ 1);
6090 /* option validation is done later */
6091 append_options(options
, optarg
);
6095 flags
|= MS_OVERLAY
;
6098 (void) fprintf(stderr
, gettext("missing argument for "
6099 "'%c' option\n"), optopt
);
6103 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6112 /* check number of arguments */
6114 zfs_handle_t
**dslist
= NULL
;
6115 size_t i
, count
= 0;
6116 char *protocol
= NULL
;
6118 if (op
== OP_SHARE
&& argc
> 0) {
6119 if (strcmp(argv
[0], "nfs") != 0 &&
6120 strcmp(argv
[0], "smb") != 0) {
6121 (void) fprintf(stderr
, gettext("share type "
6122 "must be 'nfs' or 'smb'\n"));
6131 (void) fprintf(stderr
, gettext("too many arguments\n"));
6135 start_progress_timer();
6136 get_all_datasets(&dslist
, &count
, verbose
);
6141 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
6142 sa_init_selective_arg_t sharearg
;
6143 sharearg
.zhandle_arr
= dslist
;
6144 sharearg
.zhandle_len
= count
;
6145 if ((ret
= zfs_init_libshare_arg(zfs_get_handle(dslist
[0]),
6146 SA_INIT_SHARE_API_SELECTIVE
, &sharearg
)) != SA_OK
) {
6147 (void) fprintf(stderr
,
6148 gettext("Could not initialize libshare, %d"), ret
);
6152 for (i
= 0; i
< count
; i
++) {
6154 report_mount_progress(i
, count
);
6156 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
6157 B_FALSE
, options
) != 0)
6159 zfs_close(dslist
[i
]);
6163 } else if (argc
== 0) {
6164 struct mnttab entry
;
6166 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
6167 (void) fprintf(stderr
, gettext("missing filesystem "
6168 "argument (specify -a for all)\n"));
6173 * When mount is given no arguments, go through /etc/mnttab and
6174 * display any active ZFS mounts. We hide any snapshots, since
6175 * they are controlled automatically.
6177 rewind(mnttab_file
);
6178 while (getmntent(mnttab_file
, &entry
) == 0) {
6179 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
6180 strchr(entry
.mnt_special
, '@') != NULL
)
6183 (void) printf("%-30s %s\n", entry
.mnt_special
,
6191 (void) fprintf(stderr
,
6192 gettext("too many arguments\n"));
6196 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6197 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6200 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
6210 * zfs mount -a [nfs]
6211 * zfs mount filesystem
6213 * Mount all filesystems, or mount the given filesystem.
6216 zfs_do_mount(int argc
, char **argv
)
6218 return (share_mount(OP_MOUNT
, argc
, argv
));
6222 * zfs share -a [nfs | smb]
6223 * zfs share filesystem
6225 * Share all filesystems, or share the given filesystem.
6228 zfs_do_share(int argc
, char **argv
)
6230 return (share_mount(OP_SHARE
, argc
, argv
));
6233 typedef struct unshare_unmount_node
{
6234 zfs_handle_t
*un_zhp
;
6236 uu_avl_node_t un_avlnode
;
6237 } unshare_unmount_node_t
;
6241 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
6243 const unshare_unmount_node_t
*l
= larg
;
6244 const unshare_unmount_node_t
*r
= rarg
;
6246 return (strcmp(l
->un_mountp
, r
->un_mountp
));
6250 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6251 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6252 * and unmount it appropriately.
6255 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
6259 struct stat64 statbuf
;
6260 struct extmnttab entry
;
6261 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
6265 * Search for the path in /etc/mnttab. Rather than looking for the
6266 * specific path, which can be fooled by non-standard paths (i.e. ".."
6267 * or "//"), we stat() the path and search for the corresponding
6268 * (major,minor) device pair.
6270 if (stat64(path
, &statbuf
) != 0) {
6271 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6272 cmdname
, path
, strerror(errno
));
6275 path_inode
= statbuf
.st_ino
;
6278 * Search for the given (major,minor) pair in the mount table.
6280 rewind(mnttab_file
);
6281 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
6282 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
6283 entry
.mnt_minor
== minor(statbuf
.st_dev
))
6287 if (op
== OP_SHARE
) {
6288 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
6289 "currently mounted\n"), cmdname
, path
);
6292 (void) fprintf(stderr
, gettext("warning: %s not in mnttab\n"),
6294 if ((ret
= umount2(path
, flags
)) != 0)
6295 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
6300 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
6301 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
6302 "filesystem\n"), cmdname
, path
);
6306 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6307 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6311 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
6312 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6313 cmdname
, path
, strerror(errno
));
6315 } else if (statbuf
.st_ino
!= path_inode
) {
6316 (void) fprintf(stderr
, gettext("cannot "
6317 "%s '%s': not a mountpoint\n"), cmdname
, path
);
6321 if (op
== OP_SHARE
) {
6322 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6323 char smbshare_prop
[ZFS_MAXPROPLEN
];
6325 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
6326 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6327 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
6328 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6330 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6331 strcmp(smbshare_prop
, "off") == 0) {
6332 (void) fprintf(stderr
, gettext("cannot unshare "
6333 "'%s': legacy share\n"), path
);
6334 (void) fprintf(stderr
, gettext("use "
6335 "unshare(1M) to unshare this filesystem\n"));
6336 } else if (!zfs_is_shared(zhp
)) {
6337 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6338 "not currently shared\n"), path
);
6340 ret
= zfs_unshareall_bypath(zhp
, path
);
6343 char mtpt_prop
[ZFS_MAXPROPLEN
];
6345 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6346 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6349 ret
= zfs_unmount(zhp
, NULL
, flags
);
6350 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6351 (void) fprintf(stderr
, gettext("cannot unmount "
6352 "'%s': legacy mountpoint\n"),
6354 (void) fprintf(stderr
, gettext("use umount(1M) "
6355 "to unmount this filesystem\n"));
6357 ret
= zfs_unmountall(zhp
, flags
);
6368 * Generic callback for unsharing or unmounting a filesystem.
6371 unshare_unmount(int op
, int argc
, char **argv
)
6378 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6379 char sharesmb
[ZFS_MAXPROPLEN
];
6382 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
6391 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6402 * We could make use of zfs_for_each() to walk all datasets in
6403 * the system, but this would be very inefficient, especially
6404 * since we would have to linearly search /etc/mnttab for each
6405 * one. Instead, do one pass through /etc/mnttab looking for
6406 * zfs entries and call zfs_unmount() for each one.
6408 * Things get a little tricky if the administrator has created
6409 * mountpoints beneath other ZFS filesystems. In this case, we
6410 * have to unmount the deepest filesystems first. To accomplish
6411 * this, we place all the mountpoints in an AVL tree sorted by
6412 * the special type (dataset name), and walk the result in
6413 * reverse to make sure to get any snapshots first.
6415 struct mnttab entry
;
6416 uu_avl_pool_t
*pool
;
6417 uu_avl_t
*tree
= NULL
;
6418 unshare_unmount_node_t
*node
;
6420 uu_avl_walk_t
*walk
;
6423 (void) fprintf(stderr
, gettext("too many arguments\n"));
6427 if (((pool
= uu_avl_pool_create("unmount_pool",
6428 sizeof (unshare_unmount_node_t
),
6429 offsetof(unshare_unmount_node_t
, un_avlnode
),
6430 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6431 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6434 rewind(mnttab_file
);
6435 while (getmntent(mnttab_file
, &entry
) == 0) {
6437 /* ignore non-ZFS entries */
6438 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6441 /* ignore snapshots */
6442 if (strchr(entry
.mnt_special
, '@') != NULL
)
6445 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6446 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6452 * Ignore datasets that are excluded/restricted by
6455 if (zpool_skip_pool(zfs_get_pool_name(zhp
))) {
6462 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6464 sizeof (nfs_mnt_prop
),
6465 NULL
, NULL
, 0, B_FALSE
) == 0);
6466 if (strcmp(nfs_mnt_prop
, "off") != 0)
6468 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6470 sizeof (nfs_mnt_prop
),
6471 NULL
, NULL
, 0, B_FALSE
) == 0);
6472 if (strcmp(nfs_mnt_prop
, "off") == 0)
6476 /* Ignore legacy mounts */
6477 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6479 sizeof (nfs_mnt_prop
),
6480 NULL
, NULL
, 0, B_FALSE
) == 0);
6481 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6483 /* Ignore canmount=noauto mounts */
6484 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6485 ZFS_CANMOUNT_NOAUTO
)
6491 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6493 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6495 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6497 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6498 uu_avl_insert(tree
, node
, idx
);
6500 zfs_close(node
->un_zhp
);
6501 free(node
->un_mountp
);
6507 * Walk the AVL tree in reverse, unmounting each filesystem and
6508 * removing it from the AVL tree in the process.
6510 if ((walk
= uu_avl_walk_start(tree
,
6511 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6514 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6515 uu_avl_remove(tree
, node
);
6519 if (zfs_unshareall_bypath(node
->un_zhp
,
6520 node
->un_mountp
) != 0)
6525 if (zfs_unmount(node
->un_zhp
,
6526 node
->un_mountp
, flags
) != 0)
6531 zfs_close(node
->un_zhp
);
6532 free(node
->un_mountp
);
6536 uu_avl_walk_end(walk
);
6537 uu_avl_destroy(tree
);
6538 uu_avl_pool_destroy(pool
);
6543 (void) fprintf(stderr
,
6544 gettext("missing filesystem argument\n"));
6546 (void) fprintf(stderr
,
6547 gettext("too many arguments\n"));
6552 * We have an argument, but it may be a full path or a ZFS
6553 * filesystem. Pass full paths off to unmount_path() (shared by
6554 * manual_unmount), otherwise open the filesystem and pass to
6557 if (argv
[0][0] == '/')
6558 return (unshare_unmount_path(op
, argv
[0],
6561 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6562 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6565 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
6566 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
6567 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
6568 NULL
, 0, B_FALSE
) == 0);
6572 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6574 sizeof (nfs_mnt_prop
),
6575 NULL
, NULL
, 0, B_FALSE
) == 0);
6576 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6577 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
6580 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6581 strcmp(sharesmb
, "off") == 0) {
6582 (void) fprintf(stderr
, gettext("cannot "
6583 "unshare '%s': legacy share\n"),
6585 (void) fprintf(stderr
, gettext("use "
6586 "unshare(1M) to unshare this "
6589 } else if (!zfs_is_shared(zhp
)) {
6590 (void) fprintf(stderr
, gettext("cannot "
6591 "unshare '%s': not currently "
6592 "shared\n"), zfs_get_name(zhp
));
6594 } else if (zfs_unshareall(zhp
) != 0) {
6600 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
6601 (void) fprintf(stderr
, gettext("cannot "
6602 "unmount '%s': legacy "
6603 "mountpoint\n"), zfs_get_name(zhp
));
6604 (void) fprintf(stderr
, gettext("use "
6605 "umount(1M) to unmount this "
6608 } else if (!zfs_is_mounted(zhp
, NULL
)) {
6609 (void) fprintf(stderr
, gettext("cannot "
6610 "unmount '%s': not currently "
6614 } else if (zfs_unmountall(zhp
, flags
) != 0) {
6628 * zfs unmount filesystem
6630 * Unmount all filesystems, or a specific ZFS filesystem.
6633 zfs_do_unmount(int argc
, char **argv
)
6635 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
6640 * zfs unshare filesystem
6642 * Unshare all filesystems, or a specific ZFS filesystem.
6645 zfs_do_unshare(int argc
, char **argv
)
6647 return (unshare_unmount(OP_SHARE
, argc
, argv
));
6651 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6652 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6655 manual_mount(int argc
, char **argv
)
6658 char mountpoint
[ZFS_MAXPROPLEN
];
6659 char mntopts
[MNT_LINE_MAX
] = { '\0' };
6663 char *dataset
, *path
;
6666 while ((c
= getopt(argc
, argv
, ":mo:O")) != -1) {
6669 (void) strlcpy(mntopts
, optarg
, sizeof (mntopts
));
6672 flags
|= MS_OVERLAY
;
6675 flags
|= MS_NOMNTTAB
;
6678 (void) fprintf(stderr
, gettext("missing argument for "
6679 "'%c' option\n"), optopt
);
6683 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6685 (void) fprintf(stderr
, gettext("usage: mount [-o opts] "
6694 /* check that we only have two arguments */
6697 (void) fprintf(stderr
, gettext("missing dataset "
6700 (void) fprintf(stderr
,
6701 gettext("missing mountpoint argument\n"));
6703 (void) fprintf(stderr
, gettext("too many arguments\n"));
6704 (void) fprintf(stderr
, "usage: mount <dataset> <mountpoint>\n");
6711 /* try to open the dataset */
6712 if ((zhp
= zfs_open(g_zfs
, dataset
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6715 (void) zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
6716 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
);
6718 /* check for legacy mountpoint and complain appropriately */
6720 if (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) == 0) {
6721 if (mount(dataset
, path
, MS_OPTIONSTR
| flags
, MNTTYPE_ZFS
,
6722 NULL
, 0, mntopts
, sizeof (mntopts
)) != 0) {
6723 (void) fprintf(stderr
, gettext("mount failed: %s\n"),
6728 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
6729 "mounted using 'mount -F zfs'\n"), dataset
);
6730 (void) fprintf(stderr
, gettext("Use 'zfs set mountpoint=%s' "
6731 "instead.\n"), path
);
6732 (void) fprintf(stderr
, gettext("If you must use 'mount -F zfs' "
6733 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
6734 (void) fprintf(stderr
, gettext("See zfs(1M) for more "
6743 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6744 * unmounts of non-legacy filesystems, as this is the dominant administrative
6748 manual_unmount(int argc
, char **argv
)
6754 while ((c
= getopt(argc
, argv
, "f")) != -1) {
6760 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6762 (void) fprintf(stderr
, gettext("usage: unmount [-f] "
6771 /* check arguments */
6774 (void) fprintf(stderr
, gettext("missing path "
6777 (void) fprintf(stderr
, gettext("too many arguments\n"));
6778 (void) fprintf(stderr
, gettext("usage: unmount [-f] <path>\n"));
6782 return (unshare_unmount_path(OP_MOUNT
, argv
[0], flags
, B_TRUE
));
6786 find_command_idx(char *command
, int *idx
)
6790 for (i
= 0; i
< NCOMMAND
; i
++) {
6791 if (command_table
[i
].name
== NULL
)
6794 if (strcmp(command
, command_table
[i
].name
) == 0) {
6803 zfs_do_diff(int argc
, char **argv
)
6807 char *tosnap
= NULL
;
6808 char *fromsnap
= NULL
;
6813 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
6816 flags
|= ZFS_DIFF_CLASSIFY
;
6819 flags
|= ZFS_DIFF_PARSEABLE
;
6822 flags
|= ZFS_DIFF_TIMESTAMP
;
6825 (void) fprintf(stderr
,
6826 gettext("invalid option '%c'\n"), optopt
);
6835 (void) fprintf(stderr
,
6836 gettext("must provide at least one snapshot name\n"));
6841 (void) fprintf(stderr
, gettext("too many arguments\n"));
6846 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
6849 if (*fromsnap
!= '@')
6850 copy
= strdup(fromsnap
);
6852 copy
= strdup(tosnap
);
6856 if ((atp
= strchr(copy
, '@')) != NULL
)
6859 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6865 * Ignore SIGPIPE so that the library can give us
6866 * information on any failure
6868 (void) sigignore(SIGPIPE
);
6870 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
6878 * zfs bookmark <fs@snap> <fs#bmark>
6880 * Creates a bookmark with the given name from the given snapshot.
6883 zfs_do_bookmark(int argc
, char **argv
)
6885 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
6892 while ((c
= getopt(argc
, argv
, "")) != -1) {
6895 (void) fprintf(stderr
,
6896 gettext("invalid option '%c'\n"), optopt
);
6904 /* check number of arguments */
6906 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
6910 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
6914 if (strchr(argv
[1], '#') == NULL
) {
6915 (void) fprintf(stderr
,
6916 gettext("invalid bookmark name '%s' -- "
6917 "must contain a '#'\n"), argv
[1]);
6921 if (argv
[0][0] == '@') {
6923 * Snapshot name begins with @.
6924 * Default to same fs as bookmark.
6926 (void) strncpy(snapname
, argv
[1], sizeof (snapname
));
6927 *strchr(snapname
, '#') = '\0';
6928 (void) strlcat(snapname
, argv
[0], sizeof (snapname
));
6930 (void) strncpy(snapname
, argv
[0], sizeof (snapname
));
6932 zhp
= zfs_open(g_zfs
, snapname
, ZFS_TYPE_SNAPSHOT
);
6938 nvl
= fnvlist_alloc();
6939 fnvlist_add_string(nvl
, argv
[1], snapname
);
6940 ret
= lzc_bookmark(nvl
, NULL
);
6944 const char *err_msg
;
6947 (void) snprintf(errbuf
, sizeof (errbuf
),
6948 dgettext(TEXT_DOMAIN
,
6949 "cannot create bookmark '%s'"), argv
[1]);
6953 err_msg
= "bookmark is in a different pool";
6956 err_msg
= "bookmark exists";
6959 err_msg
= "invalid argument";
6962 err_msg
= "bookmark feature not enabled";
6965 err_msg
= "out of space";
6968 err_msg
= "unknown error";
6971 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
6972 dgettext(TEXT_DOMAIN
, err_msg
));
6983 main(int argc
, char **argv
)
6990 (void) setlocale(LC_ALL
, "");
6991 (void) textdomain(TEXT_DOMAIN
);
6995 if ((g_zfs
= libzfs_init()) == NULL
) {
6996 (void) fprintf(stderr
, gettext("internal error: failed to "
6997 "initialize ZFS library\n"));
7001 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
7003 libzfs_print_on_error(g_zfs
, B_TRUE
);
7005 if ((mnttab_file
= fopen(MNTTAB
, "r")) == NULL
) {
7006 (void) fprintf(stderr
, gettext("internal error: unable to "
7007 "open %s\n"), MNTTAB
);
7012 * This command also doubles as the /etc/fs mount and unmount program.
7013 * Determine if we should take this behavior based on argv[0].
7015 progname
= basename(argv
[0]);
7016 if (strcmp(progname
, "mount") == 0) {
7017 ret
= manual_mount(argc
, argv
);
7018 } else if (strcmp(progname
, "umount") == 0) {
7019 ret
= manual_unmount(argc
, argv
);
7022 * Make sure the user has specified some command.
7025 (void) fprintf(stderr
, gettext("missing command\n"));
7032 * The 'umount' command is an alias for 'unmount'
7034 if (strcmp(cmdname
, "umount") == 0)
7035 cmdname
= "unmount";
7038 * The 'recv' command is an alias for 'receive'
7040 if (strcmp(cmdname
, "recv") == 0)
7041 cmdname
= "receive";
7044 * The 'snap' command is an alias for 'snapshot'
7046 if (strcmp(cmdname
, "snap") == 0)
7047 cmdname
= "snapshot";
7052 if (strcmp(cmdname
, "-?") == 0)
7056 * Run the appropriate command.
7058 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
7059 if (find_command_idx(cmdname
, &i
) == 0) {
7060 current_command
= &command_table
[i
];
7061 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
7062 } else if (strchr(cmdname
, '=') != NULL
) {
7063 verify(find_command_idx("set", &i
) == 0);
7064 current_command
= &command_table
[i
];
7065 ret
= command_table
[i
].func(argc
, argv
);
7067 (void) fprintf(stderr
, gettext("unrecognized "
7068 "command '%s'\n"), cmdname
);
7071 libzfs_mnttab_cache(g_zfs
, B_FALSE
);
7074 (void) fclose(mnttab_file
);
7076 if (ret
== 0 && log_history
)
7077 (void) zpool_log_history(g_zfs
, history_str
);
7082 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7083 * for the purposes of running ::findleaks.
7085 if (getenv("ZFS_ABORT") != NULL
) {
7086 (void) printf("dumping core by request\n");