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 free(cb
->cb_prevsnap
);
1085 /* this snap continues the current range */
1086 cb
->cb_prevsnap
= strdup(name
);
1087 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1089 if (cb
->cb_verbose
) {
1090 if (cb
->cb_parsable
) {
1091 (void) printf("destroy\t%s\n", name
);
1092 } else if (cb
->cb_dryrun
) {
1093 (void) printf(gettext("would destroy %s\n"),
1096 (void) printf(gettext("will destroy %s\n"),
1100 } else if (cb
->cb_firstsnap
!= NULL
) {
1101 /* end of this range */
1103 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1104 cb
->cb_prevsnap
, &used
);
1105 cb
->cb_snapused
+= used
;
1106 free(cb
->cb_firstsnap
);
1107 cb
->cb_firstsnap
= NULL
;
1108 free(cb
->cb_prevsnap
);
1109 cb
->cb_prevsnap
= NULL
;
1116 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1119 assert(cb
->cb_firstsnap
== NULL
);
1120 assert(cb
->cb_prevsnap
== NULL
);
1121 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1122 if (cb
->cb_firstsnap
!= NULL
) {
1125 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1126 cb
->cb_prevsnap
, &used
);
1128 cb
->cb_snapused
+= used
;
1129 free(cb
->cb_firstsnap
);
1130 cb
->cb_firstsnap
= NULL
;
1131 free(cb
->cb_prevsnap
);
1132 cb
->cb_prevsnap
= NULL
;
1138 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1140 destroy_cbdata_t
*cb
= arg
;
1143 /* Check for clones. */
1144 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1145 cb
->cb_target
= zhp
;
1146 cb
->cb_first
= B_TRUE
;
1147 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1148 destroy_check_dependent
, cb
);
1152 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1160 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1162 destroy_cbdata_t
*cb
= arg
;
1165 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1171 if (cb
->cb_verbose
) {
1172 err
= destroy_print_snapshots(zhp
, cb
);
1178 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1186 destroy_clones(destroy_cbdata_t
*cb
)
1189 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1191 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1192 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1195 boolean_t defer
= cb
->cb_defer_destroy
;
1199 * We can't defer destroy non-snapshots, so set it to
1200 * false while destroying the clones.
1202 cb
->cb_defer_destroy
= B_FALSE
;
1203 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1204 destroy_callback
, cb
);
1205 cb
->cb_defer_destroy
= defer
;
1215 zfs_do_destroy(int argc
, char **argv
)
1217 destroy_cbdata_t cb
= { 0 };
1221 zfs_handle_t
*zhp
= NULL
;
1223 zfs_type_t type
= ZFS_TYPE_DATASET
;
1226 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1229 cb
.cb_verbose
= B_TRUE
;
1232 cb
.cb_verbose
= B_TRUE
;
1233 cb
.cb_parsable
= B_TRUE
;
1236 cb
.cb_dryrun
= B_TRUE
;
1239 cb
.cb_defer_destroy
= B_TRUE
;
1240 type
= ZFS_TYPE_SNAPSHOT
;
1243 cb
.cb_force
= B_TRUE
;
1246 cb
.cb_recurse
= B_TRUE
;
1249 cb
.cb_recurse
= B_TRUE
;
1250 cb
.cb_doclones
= B_TRUE
;
1254 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1263 /* check number of arguments */
1265 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1269 (void) fprintf(stderr
, gettext("too many arguments\n"));
1273 at
= strchr(argv
[0], '@');
1274 pound
= strchr(argv
[0], '#');
1277 /* Build the list of snaps to destroy in cb_nvl. */
1278 cb
.cb_nvl
= fnvlist_alloc();
1281 zhp
= zfs_open(g_zfs
, argv
[0],
1282 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1286 cb
.cb_snapspec
= at
+ 1;
1287 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1293 if (nvlist_empty(cb
.cb_nvl
)) {
1294 (void) fprintf(stderr
, gettext("could not find any "
1295 "snapshots to destroy; check snapshot names.\n"));
1300 if (cb
.cb_verbose
) {
1302 zfs_nicenum(cb
.cb_snapused
, buf
, sizeof (buf
));
1303 if (cb
.cb_parsable
) {
1304 (void) printf("reclaim\t%llu\n",
1306 } else if (cb
.cb_dryrun
) {
1307 (void) printf(gettext("would reclaim %s\n"),
1310 (void) printf(gettext("will reclaim %s\n"),
1315 if (!cb
.cb_dryrun
) {
1316 if (cb
.cb_doclones
) {
1317 cb
.cb_batchedsnaps
= fnvlist_alloc();
1318 err
= destroy_clones(&cb
);
1320 err
= zfs_destroy_snaps_nvl(g_zfs
,
1321 cb
.cb_batchedsnaps
, B_FALSE
);
1329 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1330 cb
.cb_defer_destroy
);
1336 } else if (pound
!= NULL
) {
1341 (void) fprintf(stderr
,
1342 "dryrun is not supported with bookmark\n");
1346 if (cb
.cb_defer_destroy
) {
1347 (void) fprintf(stderr
,
1348 "defer destroy is not supported with bookmark\n");
1352 if (cb
.cb_recurse
) {
1353 (void) fprintf(stderr
,
1354 "recursive is not supported with bookmark\n");
1358 if (!zfs_bookmark_exists(argv
[0])) {
1359 (void) fprintf(stderr
, gettext("bookmark '%s' "
1360 "does not exist.\n"), argv
[0]);
1364 nvl
= fnvlist_alloc();
1365 fnvlist_add_boolean(nvl
, argv
[0]);
1367 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1369 (void) zfs_standard_error(g_zfs
, err
,
1370 "cannot destroy bookmark");
1373 nvlist_free(cb
.cb_nvl
);
1377 /* Open the given dataset */
1378 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1384 * Perform an explicit check for pools before going any further.
1386 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1387 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1388 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1389 "operation does not apply to pools\n"),
1391 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1392 "%s' to destroy all datasets in the pool\n"),
1394 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1395 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1401 * Check for any dependents and/or clones.
1403 cb
.cb_first
= B_TRUE
;
1404 if (!cb
.cb_doclones
&&
1405 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1416 cb
.cb_batchedsnaps
= fnvlist_alloc();
1417 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1424 * Do the real thing. The callback will close the
1425 * handle regardless of whether it succeeds or not.
1427 err
= destroy_callback(zhp
, &cb
);
1430 err
= zfs_destroy_snaps_nvl(g_zfs
,
1431 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1438 fnvlist_free(cb
.cb_batchedsnaps
);
1439 fnvlist_free(cb
.cb_nvl
);
1446 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1449 zfs_get_column_t col
;
1451 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1452 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1453 if (col
== GET_COL_RECVD
)
1459 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1460 * < all | property[,property]... > < fs | snap | vol > ...
1462 * -r recurse over any child datasets
1463 * -H scripted mode. Headers are stripped, and fields are separated
1464 * by tabs instead of spaces.
1465 * -o Set of fields to display. One of "name,property,value,
1466 * received,source". Default is "name,property,value,source".
1467 * "all" is an alias for all five.
1468 * -s Set of sources to allow. One of
1469 * "local,default,inherited,received,temporary,none". Default is
1471 * -p Display values in parsable (literal) format.
1473 * Prints properties for the given datasets. The user can control which
1474 * columns to display as well as which property types to allow.
1478 * Invoked to display the properties for a single dataset.
1481 get_callback(zfs_handle_t
*zhp
, void *data
)
1483 char buf
[ZFS_MAXPROPLEN
];
1484 char rbuf
[ZFS_MAXPROPLEN
];
1485 zprop_source_t sourcetype
;
1486 char source
[ZFS_MAX_DATASET_NAME_LEN
];
1487 zprop_get_cbdata_t
*cbp
= data
;
1488 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1489 zprop_list_t
*pl
= cbp
->cb_proplist
;
1493 boolean_t received
= is_recvd_column(cbp
);
1495 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1496 char *recvdval
= NULL
;
1498 * Skip the special fake placeholder. This will also skip over
1499 * the name property when 'all' is specified.
1501 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1502 pl
== cbp
->cb_proplist
)
1505 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1506 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1507 sizeof (buf
), &sourcetype
, source
,
1509 cbp
->cb_literal
) != 0) {
1512 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1513 ZFS_TYPE_DATASET
)) {
1514 (void) fprintf(stderr
,
1515 gettext("No such property '%s'\n"),
1516 zfs_prop_to_name(pl
->pl_prop
));
1519 sourcetype
= ZPROP_SRC_NONE
;
1520 (void) strlcpy(buf
, "-", sizeof (buf
));
1523 if (received
&& (zfs_prop_get_recvd(zhp
,
1524 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1525 cbp
->cb_literal
) == 0))
1528 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1529 zfs_prop_to_name(pl
->pl_prop
),
1530 buf
, sourcetype
, source
, recvdval
);
1531 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1532 sourcetype
= ZPROP_SRC_LOCAL
;
1534 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1535 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1536 sourcetype
= ZPROP_SRC_NONE
;
1537 (void) strlcpy(buf
, "-", sizeof (buf
));
1540 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1541 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1542 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1543 sourcetype
= ZPROP_SRC_LOCAL
;
1545 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1546 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1547 sourcetype
= ZPROP_SRC_NONE
;
1548 (void) strlcpy(buf
, "-", sizeof (buf
));
1551 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1552 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1554 if (nvlist_lookup_nvlist(user_props
,
1555 pl
->pl_user_prop
, &propval
) != 0) {
1558 sourcetype
= ZPROP_SRC_NONE
;
1561 verify(nvlist_lookup_string(propval
,
1562 ZPROP_VALUE
, &strval
) == 0);
1563 verify(nvlist_lookup_string(propval
,
1564 ZPROP_SOURCE
, &sourceval
) == 0);
1566 if (strcmp(sourceval
,
1567 zfs_get_name(zhp
)) == 0) {
1568 sourcetype
= ZPROP_SRC_LOCAL
;
1569 } else if (strcmp(sourceval
,
1570 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1571 sourcetype
= ZPROP_SRC_RECEIVED
;
1573 sourcetype
= ZPROP_SRC_INHERITED
;
1574 (void) strlcpy(source
,
1575 sourceval
, sizeof (source
));
1579 if (received
&& (zfs_prop_get_recvd(zhp
,
1580 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1581 cbp
->cb_literal
) == 0))
1584 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1585 pl
->pl_user_prop
, strval
, sourcetype
,
1594 zfs_do_get(int argc
, char **argv
)
1596 zprop_get_cbdata_t cb
= { 0 };
1597 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1598 int types
= ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
;
1599 char *value
, *fields
;
1602 zprop_list_t fake_name
= { 0 };
1605 * Set up default columns and sources.
1607 cb
.cb_sources
= ZPROP_SRC_ALL
;
1608 cb
.cb_columns
[0] = GET_COL_NAME
;
1609 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1610 cb
.cb_columns
[2] = GET_COL_VALUE
;
1611 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1612 cb
.cb_type
= ZFS_TYPE_DATASET
;
1615 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1618 cb
.cb_literal
= B_TRUE
;
1621 limit
= parse_depth(optarg
, &flags
);
1624 flags
|= ZFS_ITER_RECURSE
;
1627 cb
.cb_scripted
= B_TRUE
;
1630 (void) fprintf(stderr
, gettext("missing argument for "
1631 "'%c' option\n"), optopt
);
1636 * Process the set of columns to display. We zero out
1637 * the structure to give us a blank slate.
1639 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1641 while (*optarg
!= '\0') {
1642 static char *col_subopts
[] =
1643 { "name", "property", "value", "received",
1644 "source", "all", NULL
};
1646 if (i
== ZFS_GET_NCOLS
) {
1647 (void) fprintf(stderr
, gettext("too "
1648 "many fields given to -o "
1653 switch (getsubopt(&optarg
, col_subopts
,
1656 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1659 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1662 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1665 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1666 flags
|= ZFS_ITER_RECVD_PROPS
;
1669 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1673 (void) fprintf(stderr
,
1674 gettext("\"all\" conflicts "
1675 "with specific fields "
1676 "given to -o option\n"));
1679 cb
.cb_columns
[0] = GET_COL_NAME
;
1680 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1681 cb
.cb_columns
[2] = GET_COL_VALUE
;
1682 cb
.cb_columns
[3] = GET_COL_RECVD
;
1683 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1684 flags
|= ZFS_ITER_RECVD_PROPS
;
1688 (void) fprintf(stderr
,
1689 gettext("invalid column name "
1698 while (*optarg
!= '\0') {
1699 static char *source_subopts
[] = {
1700 "local", "default", "inherited",
1701 "received", "temporary", "none",
1704 switch (getsubopt(&optarg
, source_subopts
,
1707 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1710 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1713 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1716 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1719 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1722 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1725 (void) fprintf(stderr
,
1726 gettext("invalid source "
1735 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1736 while (*optarg
!= '\0') {
1737 static char *type_subopts
[] = { "filesystem",
1738 "volume", "snapshot", "bookmark",
1741 switch (getsubopt(&optarg
, type_subopts
,
1744 types
|= ZFS_TYPE_FILESYSTEM
;
1747 types
|= ZFS_TYPE_VOLUME
;
1750 types
|= ZFS_TYPE_SNAPSHOT
;
1753 types
|= ZFS_TYPE_BOOKMARK
;
1756 types
= ZFS_TYPE_DATASET
|
1761 (void) fprintf(stderr
,
1762 gettext("invalid type '%s'\n"),
1770 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1780 (void) fprintf(stderr
, gettext("missing property "
1787 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1795 * As part of zfs_expand_proplist(), we keep track of the maximum column
1796 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1797 * need to know the maximum name length. However, the user likely did
1798 * not specify 'name' as one of the properties to fetch, so we need to
1799 * make sure we always include at least this property for
1800 * print_get_headers() to work properly.
1802 if (cb
.cb_proplist
!= NULL
) {
1803 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1804 fake_name
.pl_width
= strlen(gettext("NAME"));
1805 fake_name
.pl_next
= cb
.cb_proplist
;
1806 cb
.cb_proplist
= &fake_name
;
1809 cb
.cb_first
= B_TRUE
;
1811 /* run for each object */
1812 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1813 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1815 if (cb
.cb_proplist
== &fake_name
)
1816 zprop_free_list(fake_name
.pl_next
);
1818 zprop_free_list(cb
.cb_proplist
);
1824 * inherit [-rS] <property> <fs|vol> ...
1826 * -r Recurse over all children
1827 * -S Revert to received value, if any
1829 * For each dataset specified on the command line, inherit the given property
1830 * from its parent. Inheriting a property at the pool level will cause it to
1831 * use the default value. The '-r' flag will recurse over all children, and is
1832 * useful for setting a property on a hierarchy-wide basis, regardless of any
1833 * local modifications for each dataset.
1836 typedef struct inherit_cbdata
{
1837 const char *cb_propname
;
1838 boolean_t cb_received
;
1842 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1844 inherit_cbdata_t
*cb
= data
;
1845 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1848 * If we're doing it recursively, then ignore properties that
1849 * are not valid for this type of dataset.
1851 if (prop
!= ZPROP_INVAL
&&
1852 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
)))
1855 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1859 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1861 inherit_cbdata_t
*cb
= data
;
1863 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1867 zfs_do_inherit(int argc
, char **argv
)
1871 inherit_cbdata_t cb
= { 0 };
1875 boolean_t received
= B_FALSE
;
1878 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1881 flags
|= ZFS_ITER_RECURSE
;
1888 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1897 /* check number of arguments */
1899 (void) fprintf(stderr
, gettext("missing property argument\n"));
1903 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1911 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1912 if (zfs_prop_readonly(prop
)) {
1913 (void) fprintf(stderr
, gettext(
1914 "%s property is read-only\n"),
1918 if (!zfs_prop_inheritable(prop
) && !received
) {
1919 (void) fprintf(stderr
, gettext("'%s' property cannot "
1920 "be inherited\n"), propname
);
1921 if (prop
== ZFS_PROP_QUOTA
||
1922 prop
== ZFS_PROP_RESERVATION
||
1923 prop
== ZFS_PROP_REFQUOTA
||
1924 prop
== ZFS_PROP_REFRESERVATION
) {
1925 (void) fprintf(stderr
, gettext("use 'zfs set "
1926 "%s=none' to clear\n"), propname
);
1927 (void) fprintf(stderr
, gettext("use 'zfs "
1928 "inherit -S %s' to revert to received "
1929 "value\n"), propname
);
1933 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1934 prop
== ZFS_PROP_VERSION
)) {
1935 (void) fprintf(stderr
, gettext("'%s' property cannot "
1936 "be reverted to a received value\n"), propname
);
1939 } else if (!zfs_prop_user(propname
)) {
1940 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1945 cb
.cb_propname
= propname
;
1946 cb
.cb_received
= received
;
1948 if (flags
& ZFS_ITER_RECURSE
) {
1949 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1950 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1952 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1953 NULL
, NULL
, 0, inherit_cb
, &cb
);
1959 typedef struct upgrade_cbdata
{
1960 uint64_t cb_numupgraded
;
1961 uint64_t cb_numsamegraded
;
1962 uint64_t cb_numfailed
;
1963 uint64_t cb_version
;
1965 boolean_t cb_foundone
;
1966 char cb_lastfs
[ZFS_MAX_DATASET_NAME_LEN
];
1970 same_pool(zfs_handle_t
*zhp
, const char *name
)
1972 int len1
= strcspn(name
, "/@");
1973 const char *zhname
= zfs_get_name(zhp
);
1974 int len2
= strcspn(zhname
, "/@");
1978 return (strncmp(name
, zhname
, len1
) == 0);
1982 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
1984 upgrade_cbdata_t
*cb
= data
;
1985 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1987 /* list if it's old/new */
1988 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
1989 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
1992 str
= gettext("The following filesystems are "
1993 "formatted using a newer software version and\n"
1994 "cannot be accessed on the current system.\n\n");
1996 str
= gettext("The following filesystems are "
1997 "out of date, and can be upgraded. After being\n"
1998 "upgraded, these filesystems (and any 'zfs send' "
1999 "streams generated from\n"
2000 "subsequent snapshots) will no longer be "
2001 "accessible by older software versions.\n\n");
2004 if (!cb
->cb_foundone
) {
2006 (void) printf(gettext("VER FILESYSTEM\n"));
2007 (void) printf(gettext("--- ------------\n"));
2008 cb
->cb_foundone
= B_TRUE
;
2011 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
2018 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2020 upgrade_cbdata_t
*cb
= data
;
2021 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2022 int needed_spa_version
;
2025 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2028 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2030 if (needed_spa_version
< 0)
2033 if (spa_version
< needed_spa_version
) {
2035 (void) printf(gettext("%s: can not be "
2036 "upgraded; the pool version needs to first "
2037 "be upgraded\nto version %d\n\n"),
2038 zfs_get_name(zhp
), needed_spa_version
);
2044 if (version
< cb
->cb_version
) {
2046 (void) snprintf(verstr
, sizeof (verstr
),
2047 "%llu", cb
->cb_version
);
2048 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2050 * If they did "zfs upgrade -a", then we could
2051 * be doing ioctls to different pools. We need
2052 * to log this history once to each pool, and bypass
2053 * the normal history logging that happens in main().
2055 (void) zpool_log_history(g_zfs
, history_str
);
2056 log_history
= B_FALSE
;
2058 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2059 cb
->cb_numupgraded
++;
2062 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2063 } else if (version
> cb
->cb_version
) {
2064 /* can't downgrade */
2065 (void) printf(gettext("%s: can not be downgraded; "
2066 "it is already at version %u\n"),
2067 zfs_get_name(zhp
), version
);
2070 cb
->cb_numsamegraded
++;
2078 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2081 zfs_do_upgrade(int argc
, char **argv
)
2083 boolean_t all
= B_FALSE
;
2084 boolean_t showversions
= B_FALSE
;
2086 upgrade_cbdata_t cb
= { 0 };
2088 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2091 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2094 flags
|= ZFS_ITER_RECURSE
;
2097 showversions
= B_TRUE
;
2100 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2101 optarg
, &cb
.cb_version
) != 0) {
2102 (void) fprintf(stderr
,
2103 gettext("invalid version %s\n"), optarg
);
2112 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2121 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2123 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2124 cb
.cb_version
|| argc
))
2126 if ((all
|| argc
) && (showversions
))
2132 /* Show info on available versions. */
2133 (void) printf(gettext("The following filesystem versions are "
2135 (void) printf(gettext("VER DESCRIPTION\n"));
2136 (void) printf("--- -----------------------------------------"
2137 "---------------\n");
2138 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2139 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2140 (void) printf(gettext(" 3 Case insensitive and filesystem "
2141 "user identifier (FUID)\n"));
2142 (void) printf(gettext(" 4 userquota, groupquota "
2144 (void) printf(gettext(" 5 System attributes\n"));
2145 (void) printf(gettext("\nFor more information on a particular "
2146 "version, including supported releases,\n"));
2147 (void) printf("see the ZFS Administration Guide.\n\n");
2149 } else if (argc
|| all
) {
2150 /* Upgrade filesystems */
2151 if (cb
.cb_version
== 0)
2152 cb
.cb_version
= ZPL_VERSION
;
2153 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2154 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2155 (void) printf(gettext("%llu filesystems upgraded\n"),
2157 if (cb
.cb_numsamegraded
) {
2158 (void) printf(gettext("%llu filesystems already at "
2160 cb
.cb_numsamegraded
);
2162 if (cb
.cb_numfailed
!= 0)
2165 /* List old-version filesytems */
2167 (void) printf(gettext("This system is currently running "
2168 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2170 flags
|= ZFS_ITER_RECURSE
;
2171 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2172 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2174 found
= cb
.cb_foundone
;
2175 cb
.cb_foundone
= B_FALSE
;
2176 cb
.cb_newer
= B_TRUE
;
2178 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2179 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2181 if (!cb
.cb_foundone
&& !found
) {
2182 (void) printf(gettext("All filesystems are "
2183 "formatted with the current version.\n"));
2191 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2192 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2193 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2194 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2196 * -H Scripted mode; elide headers and separate columns by tabs.
2197 * -i Translate SID to POSIX ID.
2198 * -n Print numeric ID instead of user/group name.
2199 * -o Control which fields to display.
2200 * -p Use exact (parsable) numeric output.
2201 * -s Specify sort columns, descending order.
2202 * -S Specify sort columns, ascending order.
2203 * -t Control which object types to display.
2205 * Displays space consumed by, and quotas on, each user in the specified
2206 * filesystem or snapshot.
2209 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2210 enum us_field_types
{
2216 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA" };
2217 static char *us_field_names
[] = { "type", "name", "used", "quota" };
2218 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2220 #define USTYPE_PSX_GRP (1 << 0)
2221 #define USTYPE_PSX_USR (1 << 1)
2222 #define USTYPE_SMB_GRP (1 << 2)
2223 #define USTYPE_SMB_USR (1 << 3)
2224 #define USTYPE_ALL \
2225 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2227 static int us_type_bits
[] = {
2234 static char *us_type_names
[] = { "posixgroup", "posixuser", "smbgroup",
2237 typedef struct us_node
{
2239 uu_avl_node_t usn_avlnode
;
2240 uu_list_node_t usn_listnode
;
2243 typedef struct us_cbdata
{
2245 uu_avl_pool_t
*cb_avl_pool
;
2247 boolean_t cb_numname
;
2248 boolean_t cb_nicenum
;
2249 boolean_t cb_sid2posix
;
2250 zfs_userquota_prop_t cb_prop
;
2251 zfs_sort_column_t
*cb_sortcol
;
2252 size_t cb_width
[USFIELD_LAST
];
2255 static boolean_t us_populated
= B_FALSE
;
2258 zfs_sort_column_t
*si_sortcol
;
2259 boolean_t si_numname
;
2263 us_field_index(char *field
)
2267 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2268 if (strcmp(field
, us_field_names
[i
]) == 0)
2276 us_compare(const void *larg
, const void *rarg
, void *unused
)
2278 const us_node_t
*l
= larg
;
2279 const us_node_t
*r
= rarg
;
2280 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2281 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2282 boolean_t numname
= si
->si_numname
;
2283 nvlist_t
*lnvl
= l
->usn_nvl
;
2284 nvlist_t
*rnvl
= r
->usn_nvl
;
2288 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2295 zfs_prop_t prop
= sortcol
->sc_prop
;
2296 const char *propname
= NULL
;
2297 boolean_t reverse
= sortcol
->sc_reverse
;
2302 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2303 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2305 rc
= (rv32
< lv32
) ? 1 : -1;
2310 (void) nvlist_lookup_uint64(lnvl
, propname
,
2312 (void) nvlist_lookup_uint64(rnvl
, propname
,
2315 rc
= (rv64
< lv64
) ? 1 : -1;
2317 (void) nvlist_lookup_string(lnvl
, propname
,
2319 (void) nvlist_lookup_string(rnvl
, propname
,
2321 rc
= strcmp(lvstr
, rvstr
);
2325 case ZFS_PROP_QUOTA
:
2328 if (prop
== ZFS_PROP_USED
)
2332 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2333 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2335 rc
= (rv64
< lv64
) ? 1 : -1;
2344 return (reverse
? 1 : -1);
2346 return (reverse
? -1 : 1);
2351 * If entries still seem to be the same, check if they are of the same
2352 * type (smbentity is added only if we are doing SID to POSIX ID
2353 * translation where we can have duplicate type/name combinations).
2355 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2356 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2358 return (lvb
< rvb
? -1 : 1);
2363 static inline const char *
2364 us_type2str(unsigned field_type
)
2366 switch (field_type
) {
2367 case USTYPE_PSX_USR
:
2368 return ("POSIX User");
2369 case USTYPE_PSX_GRP
:
2370 return ("POSIX Group");
2371 case USTYPE_SMB_USR
:
2372 return ("SMB User");
2373 case USTYPE_SMB_GRP
:
2374 return ("SMB Group");
2376 return ("Undefined");
2381 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2383 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2384 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2389 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2390 uu_avl_t
*avl
= cb
->cb_avl
;
2394 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2396 const char *typestr
;
2400 int typeidx
, nameidx
, sizeidx
;
2401 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2402 boolean_t smbentity
= B_FALSE
;
2404 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2406 node
= safe_malloc(sizeof (us_node_t
));
2407 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2408 node
->usn_nvl
= props
;
2410 if (domain
!= NULL
&& domain
[0] != '\0') {
2412 char sid
[MAXNAMELEN
+ 32];
2415 int flag
= IDMAP_REQ_FLG_USE_CACHE
;
2419 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2421 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2422 type
= USTYPE_SMB_GRP
;
2423 err
= sid_to_id(sid
, B_FALSE
, &id
);
2425 type
= USTYPE_SMB_USR
;
2426 err
= sid_to_id(sid
, B_TRUE
, &id
);
2431 if (!cb
->cb_sid2posix
) {
2432 if (type
== USTYPE_SMB_USR
) {
2433 (void) idmap_getwinnamebyuid(rid
, flag
,
2436 (void) idmap_getwinnamebygid(rid
, flag
,
2445 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2447 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2448 type
= USTYPE_PSX_GRP
;
2449 if (!cb
->cb_numname
) {
2452 if ((g
= getgrgid(rid
)) != NULL
)
2456 type
= USTYPE_PSX_USR
;
2457 if (!cb
->cb_numname
) {
2460 if ((p
= getpwuid(rid
)) != NULL
)
2467 * Make sure that the type/name combination is unique when doing
2468 * SID to POSIX ID translation (hence changing the type from SMB to
2471 if (cb
->cb_sid2posix
&&
2472 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2475 /* Calculate/update width of TYPE field */
2476 typestr
= us_type2str(type
);
2477 typelen
= strlen(gettext(typestr
));
2478 typeidx
= us_field_index("type");
2479 if (typelen
> cb
->cb_width
[typeidx
])
2480 cb
->cb_width
[typeidx
] = typelen
;
2481 if (nvlist_add_uint32(props
, "type", type
) != 0)
2484 /* Calculate/update width of NAME field */
2485 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2486 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2488 namelen
= snprintf(NULL
, 0, "%u", rid
);
2490 if (nvlist_add_string(props
, "name", name
) != 0)
2492 namelen
= strlen(name
);
2494 nameidx
= us_field_index("name");
2495 if (namelen
> cb
->cb_width
[nameidx
])
2496 cb
->cb_width
[nameidx
] = namelen
;
2499 * Check if this type/name combination is in the list and update it;
2500 * otherwise add new node to the list.
2502 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2503 uu_avl_insert(avl
, node
, idx
);
2508 props
= node
->usn_nvl
;
2511 /* Calculate/update width of USED/QUOTA fields */
2513 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2515 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu", space
);
2516 sizelen
= strlen(sizebuf
);
2517 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
) {
2519 if (!nvlist_exists(props
, "quota"))
2520 (void) nvlist_add_uint64(props
, "quota", 0);
2523 if (!nvlist_exists(props
, "used"))
2524 (void) nvlist_add_uint64(props
, "used", 0);
2526 sizeidx
= us_field_index(propname
);
2527 if (sizelen
> cb
->cb_width
[sizeidx
])
2528 cb
->cb_width
[sizeidx
] = sizelen
;
2530 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2537 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2538 size_t *width
, us_node_t
*node
)
2540 nvlist_t
*nvl
= node
->usn_nvl
;
2541 char valstr
[MAXNAMELEN
];
2542 boolean_t first
= B_TRUE
;
2548 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2549 if (!(ustype
& types
))
2552 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2553 nvpair_t
*nvp
= NULL
;
2557 char *strval
= NULL
;
2559 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2560 if (strcmp(nvpair_name(nvp
),
2561 us_field_names
[field
]) == 0)
2565 type
= nvpair_type(nvp
);
2567 case DATA_TYPE_UINT32
:
2568 (void) nvpair_value_uint32(nvp
, &val32
);
2570 case DATA_TYPE_UINT64
:
2571 (void) nvpair_value_uint64(nvp
, &val64
);
2573 case DATA_TYPE_STRING
:
2574 (void) nvpair_value_string(nvp
, &strval
);
2577 (void) fprintf(stderr
, "invalid data type\n");
2582 strval
= (char *)us_type2str(val32
);
2585 if (type
== DATA_TYPE_UINT64
) {
2586 (void) sprintf(valstr
, "%llu", val64
);
2592 if (type
== DATA_TYPE_UINT64
) {
2594 (void) sprintf(valstr
, "%llu", val64
);
2596 zfs_nicenum(val64
, valstr
,
2599 if (field
== USFIELD_QUOTA
&&
2600 strcmp(valstr
, "0") == 0)
2610 (void) printf("\t");
2615 (void) printf("%s", strval
);
2616 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2617 (void) printf("%-*s", width
[field
], strval
);
2619 (void) printf("%*s", width
[field
], strval
);
2625 (void) printf("\n");
2629 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2630 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2638 boolean_t first
= B_TRUE
;
2640 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2641 col
= gettext(us_field_hdr
[field
]);
2642 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2643 (void) printf(first
? "%-*s" : " %-*s",
2646 (void) printf(first
? "%*s" : " %*s",
2652 (void) printf("\n");
2655 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2656 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2658 nvlist_free(node
->usn_nvl
);
2663 zfs_do_userspace(int argc
, char **argv
)
2666 zfs_userquota_prop_t p
;
2667 uu_avl_pool_t
*avl_pool
;
2669 uu_avl_walk_t
*walk
;
2671 char deffields
[] = "type,name,used,quota";
2672 char *ofield
= NULL
;
2673 char *tfield
= NULL
;
2677 boolean_t scripted
= B_FALSE
;
2678 boolean_t prtnum
= B_FALSE
;
2679 boolean_t parsable
= B_FALSE
;
2680 boolean_t sid2posix
= B_FALSE
;
2683 zfs_sort_column_t
*sortcol
= NULL
;
2684 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2688 uu_list_pool_t
*listpool
;
2690 uu_avl_index_t idx
= 0;
2691 uu_list_index_t idx2
= 0;
2696 if (strcmp(argv
[0], "groupspace") == 0)
2697 /* Toggle default group types */
2698 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2700 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2716 if (zfs_add_sort_column(&sortcol
, optarg
,
2717 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
2718 (void) fprintf(stderr
,
2719 gettext("invalid field '%s'\n"), optarg
);
2730 (void) fprintf(stderr
, gettext("missing argument for "
2731 "'%c' option\n"), optopt
);
2735 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2745 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2749 (void) fprintf(stderr
, gettext("too many arguments\n"));
2753 /* Use default output fields if not specified using -o */
2757 if ((delim
= strchr(ofield
, ',')) != NULL
)
2759 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
2760 (void) fprintf(stderr
, gettext("invalid type '%s' "
2761 "for -o option\n"), ofield
);
2766 } while (delim
!= NULL
);
2767 fields
[cfield
] = USFIELD_LAST
;
2769 /* Override output types (-t option) */
2770 if (tfield
!= NULL
) {
2774 boolean_t found
= B_FALSE
;
2776 if ((delim
= strchr(tfield
, ',')) != NULL
)
2778 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
2780 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
2782 types
|= us_type_bits
[i
];
2787 (void) fprintf(stderr
, gettext("invalid type "
2788 "'%s' for -t option\n"), tfield
);
2793 } while (delim
!= NULL
);
2796 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
2799 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
2800 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
2802 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
2805 /* Always add default sorting columns */
2806 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
2807 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
2809 cb
.cb_sortcol
= sortcol
;
2810 cb
.cb_numname
= prtnum
;
2811 cb
.cb_nicenum
= !parsable
;
2812 cb
.cb_avl_pool
= avl_pool
;
2813 cb
.cb_avl
= avl_tree
;
2814 cb
.cb_sid2posix
= sid2posix
;
2816 for (i
= 0; i
< USFIELD_LAST
; i
++)
2817 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
2819 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
2820 if (((p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
) &&
2821 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
2822 ((p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
) &&
2823 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))))
2826 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
2831 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
2834 us_populated
= B_TRUE
;
2836 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
2837 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
2838 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
2839 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
2841 while (node
!= NULL
) {
2843 node
= uu_avl_next(avl_tree
, node
);
2844 uu_avl_remove(avl_tree
, rmnode
);
2845 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
2846 uu_list_insert(list
, rmnode
, idx2
);
2849 for (node
= uu_list_first(list
); node
!= NULL
;
2850 node
= uu_list_next(list
, node
)) {
2851 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
2853 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
2854 uu_avl_insert(avl_tree
, node
, idx
);
2857 uu_list_destroy(list
);
2858 uu_list_pool_destroy(listpool
);
2860 /* Print and free node nvlist memory */
2861 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
2864 zfs_free_sort_columns(sortcol
);
2866 /* Clean up the AVL tree */
2867 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
2870 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
2871 uu_avl_remove(cb
.cb_avl
, node
);
2875 uu_avl_walk_end(walk
);
2876 uu_avl_destroy(avl_tree
);
2877 uu_avl_pool_destroy(avl_pool
);
2883 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2884 * [-t type[,...]] [filesystem|volume|snapshot] ...
2886 * -H Scripted mode; elide headers and separate columns by tabs.
2887 * -p Display values in parsable (literal) format.
2888 * -r Recurse over all children.
2889 * -d Limit recursion by depth.
2890 * -o Control which fields to display.
2891 * -s Specify sort columns, descending order.
2892 * -S Specify sort columns, ascending order.
2893 * -t Control which object types to display.
2895 * When given no arguments, list all filesystems in the system.
2896 * Otherwise, list the specified datasets, optionally recursing down them if
2897 * '-r' is specified.
2899 typedef struct list_cbdata
{
2901 boolean_t cb_literal
;
2902 boolean_t cb_scripted
;
2903 zprop_list_t
*cb_proplist
;
2907 * Given a list of columns to display, output appropriate headers for each one.
2910 print_header(list_cbdata_t
*cb
)
2912 zprop_list_t
*pl
= cb
->cb_proplist
;
2913 char headerbuf
[ZFS_MAXPROPLEN
];
2916 boolean_t first
= B_TRUE
;
2917 boolean_t right_justify
;
2919 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2926 right_justify
= B_FALSE
;
2927 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2928 header
= zfs_prop_column_name(pl
->pl_prop
);
2929 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2931 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2932 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2933 headerbuf
[i
] = '\0';
2937 if (pl
->pl_next
== NULL
&& !right_justify
)
2938 (void) printf("%s", header
);
2939 else if (right_justify
)
2940 (void) printf("%*s", pl
->pl_width
, header
);
2942 (void) printf("%-*s", pl
->pl_width
, header
);
2945 (void) printf("\n");
2949 * Given a dataset and a list of fields, print out all the properties according
2950 * to the described layout.
2953 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
2955 zprop_list_t
*pl
= cb
->cb_proplist
;
2956 boolean_t first
= B_TRUE
;
2957 char property
[ZFS_MAXPROPLEN
];
2958 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
2961 boolean_t right_justify
;
2963 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2965 if (cb
->cb_scripted
)
2966 (void) printf("\t");
2973 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
2974 (void) strlcpy(property
, zfs_get_name(zhp
),
2977 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2978 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
2979 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
2980 sizeof (property
), NULL
, NULL
, 0,
2981 cb
->cb_literal
) != 0)
2985 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2986 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
2987 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
2988 property
, sizeof (property
), cb
->cb_literal
) != 0)
2992 right_justify
= B_TRUE
;
2993 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
2994 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
2995 property
, sizeof (property
), cb
->cb_literal
) != 0)
2999 right_justify
= B_TRUE
;
3001 if (nvlist_lookup_nvlist(userprops
,
3002 pl
->pl_user_prop
, &propval
) != 0)
3005 verify(nvlist_lookup_string(propval
,
3006 ZPROP_VALUE
, &propstr
) == 0);
3007 right_justify
= B_FALSE
;
3011 * If this is being called in scripted mode, or if this is the
3012 * last column and it is left-justified, don't include a width
3015 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3016 (void) printf("%s", propstr
);
3017 else if (right_justify
)
3018 (void) printf("%*s", pl
->pl_width
, propstr
);
3020 (void) printf("%-*s", pl
->pl_width
, propstr
);
3023 (void) printf("\n");
3027 * Generic callback function to list a dataset or snapshot.
3030 list_callback(zfs_handle_t
*zhp
, void *data
)
3032 list_cbdata_t
*cbp
= data
;
3034 if (cbp
->cb_first
) {
3035 if (!cbp
->cb_scripted
)
3037 cbp
->cb_first
= B_FALSE
;
3040 print_dataset(zhp
, cbp
);
3046 zfs_do_list(int argc
, char **argv
)
3049 static char default_fields
[] =
3050 "name,used,available,referenced,mountpoint";
3051 int types
= ZFS_TYPE_DATASET
;
3052 boolean_t types_specified
= B_FALSE
;
3053 char *fields
= NULL
;
3054 list_cbdata_t cb
= { 0 };
3058 zfs_sort_column_t
*sortcol
= NULL
;
3059 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3062 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3068 cb
.cb_literal
= B_TRUE
;
3069 flags
|= ZFS_ITER_LITERAL_PROPS
;
3072 limit
= parse_depth(optarg
, &flags
);
3075 flags
|= ZFS_ITER_RECURSE
;
3078 cb
.cb_scripted
= B_TRUE
;
3081 if (zfs_add_sort_column(&sortcol
, optarg
,
3083 (void) fprintf(stderr
,
3084 gettext("invalid property '%s'\n"), optarg
);
3089 if (zfs_add_sort_column(&sortcol
, optarg
,
3091 (void) fprintf(stderr
,
3092 gettext("invalid property '%s'\n"), optarg
);
3098 types_specified
= B_TRUE
;
3099 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3100 while (*optarg
!= '\0') {
3101 static char *type_subopts
[] = { "filesystem",
3102 "volume", "snapshot", "snap", "bookmark",
3105 switch (getsubopt(&optarg
, type_subopts
,
3108 types
|= ZFS_TYPE_FILESYSTEM
;
3111 types
|= ZFS_TYPE_VOLUME
;
3115 types
|= ZFS_TYPE_SNAPSHOT
;
3118 types
|= ZFS_TYPE_BOOKMARK
;
3121 types
= ZFS_TYPE_DATASET
|
3125 (void) fprintf(stderr
,
3126 gettext("invalid type '%s'\n"),
3133 (void) fprintf(stderr
, gettext("missing argument for "
3134 "'%c' option\n"), optopt
);
3138 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3148 fields
= default_fields
;
3151 * If we are only going to list snapshot names and sort by name,
3152 * then we can use faster version.
3154 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3155 flags
|= ZFS_ITER_SIMPLE
;
3158 * If "-o space" and no types were specified, don't display snapshots.
3160 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3161 types
&= ~ZFS_TYPE_SNAPSHOT
;
3164 * If the user specifies '-o all', the zprop_get_list() doesn't
3165 * normally include the name of the dataset. For 'zfs list', we always
3166 * want this property to be first.
3168 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3172 cb
.cb_first
= B_TRUE
;
3174 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3175 limit
, list_callback
, &cb
);
3177 zprop_free_list(cb
.cb_proplist
);
3178 zfs_free_sort_columns(sortcol
);
3180 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3181 (void) printf(gettext("no datasets available\n"));
3187 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3188 * zfs rename [-f] -p <fs | vol> <fs | vol>
3189 * zfs rename -r <snap> <snap>
3191 * Renames the given dataset to another of the same type.
3193 * The '-p' flag creates all the non-existing ancestors of the target first.
3197 zfs_do_rename(int argc
, char **argv
)
3202 boolean_t recurse
= B_FALSE
;
3203 boolean_t parents
= B_FALSE
;
3204 boolean_t force_unmount
= B_FALSE
;
3207 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3216 force_unmount
= B_TRUE
;
3220 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3229 /* check number of arguments */
3231 (void) fprintf(stderr
, gettext("missing source dataset "
3236 (void) fprintf(stderr
, gettext("missing target dataset "
3241 (void) fprintf(stderr
, gettext("too many arguments\n"));
3245 if (recurse
&& parents
) {
3246 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3251 if (recurse
&& strchr(argv
[0], '@') == 0) {
3252 (void) fprintf(stderr
, gettext("source dataset for recursive "
3253 "rename must be a snapshot\n"));
3257 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3258 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3261 /* If we were asked and the name looks good, try to create ancestors. */
3262 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3263 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3268 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3277 * Promotes the given clone fs to be the parent
3281 zfs_do_promote(int argc
, char **argv
)
3287 if (argc
> 1 && argv
[1][0] == '-') {
3288 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3293 /* check number of arguments */
3295 (void) fprintf(stderr
, gettext("missing clone filesystem"
3300 (void) fprintf(stderr
, gettext("too many arguments\n"));
3304 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3308 ret
= (zfs_promote(zhp
) != 0);
3316 * zfs rollback [-rRf] <snapshot>
3318 * -r Delete any intervening snapshots before doing rollback
3319 * -R Delete any snapshots and their clones
3320 * -f ignored for backwards compatability
3322 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3323 * since then and making it the active dataset. If more recent snapshots exist,
3324 * the command will complain unless the '-r' flag is given.
3326 typedef struct rollback_cbdata
{
3332 boolean_t cb_recurse
;
3333 } rollback_cbdata_t
;
3336 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3338 rollback_cbdata_t
*cbp
= data
;
3340 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3341 (void) fprintf(stderr
, gettext("cannot rollback to "
3342 "'%s': clones of previous snapshots exist\n"),
3344 (void) fprintf(stderr
, gettext("use '-R' to "
3345 "force deletion of the following clones and "
3351 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3358 * Report any snapshots more recent than the one specified. Used when '-r' is
3359 * not specified. We reuse this same callback for the snapshot dependents - if
3360 * 'cb_dependent' is set, then this is a dependent and we should report it
3361 * without checking the transaction group.
3364 rollback_check(zfs_handle_t
*zhp
, void *data
)
3366 rollback_cbdata_t
*cbp
= data
;
3368 if (cbp
->cb_doclones
) {
3373 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3374 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3375 (void) fprintf(stderr
, gettext("cannot "
3376 "rollback to '%s': more recent snapshots "
3377 "or bookmarks exist\n"),
3379 (void) fprintf(stderr
, gettext("use '-r' to "
3380 "force deletion of the following "
3381 "snapshots and bookmarks:\n"));
3386 if (cbp
->cb_recurse
) {
3387 if (zfs_iter_dependents(zhp
, B_TRUE
,
3388 rollback_check_dependent
, cbp
) != 0) {
3393 (void) fprintf(stderr
, "%s\n",
3402 zfs_do_rollback(int argc
, char **argv
)
3406 boolean_t force
= B_FALSE
;
3407 rollback_cbdata_t cb
= { 0 };
3408 zfs_handle_t
*zhp
, *snap
;
3409 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3413 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3426 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3435 /* check number of arguments */
3437 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3441 (void) fprintf(stderr
, gettext("too many arguments\n"));
3445 /* open the snapshot */
3446 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3449 /* open the parent dataset */
3450 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3451 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3453 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3459 * Check for more recent snapshots and/or clones based on the presence
3462 cb
.cb_target
= argv
[0];
3463 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3464 cb
.cb_first
= B_TRUE
;
3466 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, rollback_check
, &cb
)) != 0)
3468 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
3471 if ((ret
= cb
.cb_error
) != 0)
3475 * Rollback parent to the given snapshot.
3477 ret
= zfs_rollback(zhp
, snap
, force
);
3490 * zfs set property=value ... { fs | snap | vol } ...
3492 * Sets the given properties for all datasets specified on the command line.
3496 set_callback(zfs_handle_t
*zhp
, void *data
)
3498 nvlist_t
*props
= data
;
3500 if (zfs_prop_set_list(zhp
, props
) != 0) {
3501 switch (libzfs_errno(g_zfs
)) {
3502 case EZFS_MOUNTFAILED
:
3503 (void) fprintf(stderr
, gettext("property may be set "
3504 "but unable to remount filesystem\n"));
3506 case EZFS_SHARENFSFAILED
:
3507 (void) fprintf(stderr
, gettext("property may be set "
3508 "but unable to reshare filesystem\n"));
3517 zfs_do_set(int argc
, char **argv
)
3519 nvlist_t
*props
= NULL
;
3520 int ds_start
= -1; /* argv idx of first dataset arg */
3523 /* check for options */
3524 if (argc
> 1 && argv
[1][0] == '-') {
3525 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3530 /* check number of arguments */
3532 (void) fprintf(stderr
, gettext("missing arguments\n"));
3536 if (strchr(argv
[1], '=') == NULL
) {
3537 (void) fprintf(stderr
, gettext("missing property=value "
3540 (void) fprintf(stderr
, gettext("missing dataset "
3546 /* validate argument order: prop=val args followed by dataset args */
3547 for (int i
= 1; i
< argc
; i
++) {
3548 if (strchr(argv
[i
], '=') != NULL
) {
3550 /* out-of-order prop=val argument */
3551 (void) fprintf(stderr
, gettext("invalid "
3552 "argument order\n"), i
);
3555 } else if (ds_start
< 0) {
3560 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
3564 /* Populate a list of property settings */
3565 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3567 for (int i
= 1; i
< ds_start
; i
++) {
3568 if ((ret
= parseprop(props
, argv
[i
])) != 0)
3572 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
3573 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, props
);
3580 typedef struct snap_cbdata
{
3582 boolean_t sd_recursive
;
3583 const char *sd_snapname
;
3587 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3589 snap_cbdata_t
*sd
= arg
;
3594 if (sd
->sd_recursive
&&
3595 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
3600 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3603 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3606 if (sd
->sd_recursive
)
3607 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3613 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3615 * Creates a snapshot with the given name. While functionally equivalent to
3616 * 'zfs create', it is a separate command to differentiate intent.
3619 zfs_do_snapshot(int argc
, char **argv
)
3624 snap_cbdata_t sd
= { 0 };
3625 boolean_t multiple_snaps
= B_FALSE
;
3627 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3629 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3633 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3636 if (parseprop(props
, optarg
) != 0)
3640 sd
.sd_recursive
= B_TRUE
;
3641 multiple_snaps
= B_TRUE
;
3644 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3653 /* check number of arguments */
3655 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3660 multiple_snaps
= B_TRUE
;
3661 for (; argc
> 0; argc
--, argv
++) {
3665 atp
= strchr(argv
[0], '@');
3669 sd
.sd_snapname
= atp
+ 1;
3670 zhp
= zfs_open(g_zfs
, argv
[0],
3671 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3674 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3678 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3679 nvlist_free(sd
.sd_nvl
);
3681 if (ret
!= 0 && multiple_snaps
)
3682 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3686 nvlist_free(sd
.sd_nvl
);
3693 * Send a backup stream to stdout.
3696 zfs_do_send(int argc
, char **argv
)
3698 char *fromname
= NULL
;
3699 char *toname
= NULL
;
3700 char *resume_token
= NULL
;
3703 sendflags_t flags
= { 0 };
3705 nvlist_t
*dbgnv
= NULL
;
3706 boolean_t extraverbose
= B_FALSE
;
3708 struct option long_options
[] = {
3709 {"replicate", no_argument
, NULL
, 'R'},
3710 {"props", no_argument
, NULL
, 'p'},
3711 {"parsable", no_argument
, NULL
, 'P'},
3712 {"dedup", no_argument
, NULL
, 'D'},
3713 {"verbose", no_argument
, NULL
, 'v'},
3714 {"dryrun", no_argument
, NULL
, 'n'},
3715 {"large-block", no_argument
, NULL
, 'L'},
3716 {"embed", no_argument
, NULL
, 'e'},
3717 {"resume", required_argument
, NULL
, 't'},
3718 {"compressed", no_argument
, NULL
, 'c'},
3723 while ((c
= getopt_long(argc
, argv
, ":i:I:RbDpvnPLet:c", long_options
,
3735 flags
.doall
= B_TRUE
;
3738 flags
.replicate
= B_TRUE
;
3741 flags
.props
= B_TRUE
;
3744 flags
.parsable
= B_TRUE
;
3745 flags
.verbose
= B_TRUE
;
3749 extraverbose
= B_TRUE
;
3750 flags
.verbose
= B_TRUE
;
3751 flags
.progress
= B_TRUE
;
3754 flags
.dedup
= B_TRUE
;
3757 flags
.dryrun
= B_TRUE
;
3760 flags
.largeblock
= B_TRUE
;
3763 flags
.embed_data
= B_TRUE
;
3766 resume_token
= optarg
;
3769 flags
.compress
= B_TRUE
;
3773 * If a parameter was not passed, optopt contains the
3774 * value that would normally lead us into the
3775 * appropriate case statement. If it's > 256, then this
3776 * must be a longopt and we should look at argv to get
3777 * the string. Otherwise it's just the character, so we
3778 * should use it directly.
3780 if (optopt
<= UINT8_MAX
) {
3781 (void) fprintf(stderr
,
3782 gettext("missing argument for '%c' "
3783 "option\n"), optopt
);
3785 (void) fprintf(stderr
,
3786 gettext("missing argument for '%s' "
3787 "option\n"), argv
[optind
- 1]);
3795 * If an invalid flag was passed, optopt contains the
3796 * character if it was a short flag, or 0 if it was a
3800 (void) fprintf(stderr
,
3801 gettext("invalid option '%c'\n"), optopt
);
3803 (void) fprintf(stderr
,
3804 gettext("invalid option '%s'\n"),
3815 if (resume_token
!= NULL
) {
3816 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
3818 (void) fprintf(stderr
,
3819 gettext("invalid flags combined with -t\n"));
3823 (void) fprintf(stderr
, gettext("no additional "
3824 "arguments are permitted with -t\n"));
3829 (void) fprintf(stderr
,
3830 gettext("missing snapshot argument\n"));
3834 (void) fprintf(stderr
, gettext("too many arguments\n"));
3839 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
3840 (void) fprintf(stderr
,
3841 gettext("Error: Stream can not be written to a terminal.\n"
3842 "You must redirect standard output.\n"));
3846 if (resume_token
!= NULL
) {
3847 return (zfs_send_resume(g_zfs
, &flags
, STDOUT_FILENO
,
3852 * Special case sending a filesystem, or from a bookmark.
3854 if (strchr(argv
[0], '@') == NULL
||
3855 (fromname
&& strchr(fromname
, '#') != NULL
)) {
3856 char frombuf
[ZFS_MAX_DATASET_NAME_LEN
];
3857 enum lzc_send_flags lzc_flags
= 0;
3859 if (flags
.replicate
|| flags
.doall
|| flags
.props
||
3860 flags
.dedup
|| flags
.dryrun
|| flags
.verbose
||
3862 (void) fprintf(stderr
,
3864 "Unsupported flag with filesystem or bookmark.\n"));
3868 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
3872 if (flags
.largeblock
)
3873 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
3874 if (flags
.embed_data
)
3875 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
3877 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
3879 if (fromname
!= NULL
&&
3880 (fromname
[0] == '#' || fromname
[0] == '@')) {
3882 * Incremental source name begins with # or @.
3883 * Default to same fs as target.
3885 (void) strncpy(frombuf
, argv
[0], sizeof (frombuf
));
3886 cp
= strchr(frombuf
, '@');
3889 (void) strlcat(frombuf
, fromname
, sizeof (frombuf
));
3892 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, lzc_flags
);
3897 cp
= strchr(argv
[0], '@');
3900 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3905 * If they specified the full path to the snapshot, chop off
3906 * everything except the short name of the snapshot, but special
3907 * case if they specify the origin.
3909 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
3910 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
3913 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
3914 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
3916 if (strcmp(origin
, fromname
) == 0) {
3918 flags
.fromorigin
= B_TRUE
;
3921 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
3922 (void) fprintf(stderr
,
3923 gettext("incremental source must be "
3924 "in same filesystem\n"));
3928 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
3929 (void) fprintf(stderr
,
3930 gettext("invalid incremental source\n"));
3936 if (flags
.replicate
&& fromname
== NULL
)
3937 flags
.doall
= B_TRUE
;
3939 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
3940 extraverbose
? &dbgnv
: NULL
);
3942 if (extraverbose
&& dbgnv
!= NULL
) {
3944 * dump_nvlist prints to stdout, but that's been
3945 * redirected to a file. Make it print to stderr
3948 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
3949 dump_nvlist(dbgnv
, 0);
3958 * Restore a backup stream from stdin.
3961 zfs_do_receive(int argc
, char **argv
)
3964 recvflags_t flags
= { 0 };
3965 boolean_t abort_resumable
= B_FALSE
;
3968 nvpair_t
*nvp
= NULL
;
3970 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3974 while ((c
= getopt(argc
, argv
, ":o:denuvFsA")) != -1) {
3977 if (parseprop(props
, optarg
) != 0)
3981 flags
.isprefix
= B_TRUE
;
3984 flags
.isprefix
= B_TRUE
;
3985 flags
.istail
= B_TRUE
;
3988 flags
.dryrun
= B_TRUE
;
3991 flags
.nomount
= B_TRUE
;
3994 flags
.verbose
= B_TRUE
;
3997 flags
.resumable
= B_TRUE
;
4000 flags
.force
= B_TRUE
;
4003 abort_resumable
= B_TRUE
;
4006 (void) fprintf(stderr
, gettext("missing argument for "
4007 "'%c' option\n"), optopt
);
4011 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4020 /* check number of arguments */
4022 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4026 (void) fprintf(stderr
, gettext("too many arguments\n"));
4030 while ((nvp
= nvlist_next_nvpair(props
, nvp
))) {
4031 if (strcmp(nvpair_name(nvp
), "origin") != 0) {
4032 (void) fprintf(stderr
, gettext("invalid option"));
4037 if (abort_resumable
) {
4038 if (flags
.isprefix
|| flags
.istail
|| flags
.dryrun
||
4039 flags
.resumable
|| flags
.nomount
) {
4040 (void) fprintf(stderr
, gettext("invalid option"));
4044 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
4045 (void) snprintf(namebuf
, sizeof (namebuf
),
4046 "%s/%%recv", argv
[0]);
4048 if (zfs_dataset_exists(g_zfs
, namebuf
,
4049 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) {
4050 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4051 namebuf
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4054 err
= zfs_destroy(zhp
, B_FALSE
);
4056 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4057 argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4060 if (!zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) ||
4061 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
4062 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == -1) {
4063 (void) fprintf(stderr
,
4064 gettext("'%s' does not have any "
4065 "resumable receive state to abort\n"),
4069 err
= zfs_destroy(zhp
, B_FALSE
);
4075 if (isatty(STDIN_FILENO
)) {
4076 (void) fprintf(stderr
,
4077 gettext("Error: Backup stream can not be read "
4078 "from a terminal.\n"
4079 "You must redirect standard input.\n"));
4082 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
4088 * allow/unallow stuff
4090 /* copied from zfs/sys/dsl_deleg.h */
4091 #define ZFS_DELEG_PERM_CREATE "create"
4092 #define ZFS_DELEG_PERM_DESTROY "destroy"
4093 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4094 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4095 #define ZFS_DELEG_PERM_CLONE "clone"
4096 #define ZFS_DELEG_PERM_PROMOTE "promote"
4097 #define ZFS_DELEG_PERM_RENAME "rename"
4098 #define ZFS_DELEG_PERM_MOUNT "mount"
4099 #define ZFS_DELEG_PERM_SHARE "share"
4100 #define ZFS_DELEG_PERM_SEND "send"
4101 #define ZFS_DELEG_PERM_RECEIVE "receive"
4102 #define ZFS_DELEG_PERM_ALLOW "allow"
4103 #define ZFS_DELEG_PERM_USERPROP "userprop"
4104 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4105 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4106 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4107 #define ZFS_DELEG_PERM_USERUSED "userused"
4108 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4109 #define ZFS_DELEG_PERM_HOLD "hold"
4110 #define ZFS_DELEG_PERM_RELEASE "release"
4111 #define ZFS_DELEG_PERM_DIFF "diff"
4112 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4114 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4116 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
4117 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
4118 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
4119 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
4120 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
4121 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
4122 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
4123 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
4124 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
4125 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
4126 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
4127 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
4128 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
4129 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
4130 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
4131 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
4132 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
4134 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
4135 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
4136 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
4137 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
4138 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
4139 { NULL
, ZFS_DELEG_NOTE_NONE
}
4142 /* permission structure */
4143 typedef struct deleg_perm
{
4144 zfs_deleg_who_type_t dp_who_type
;
4145 const char *dp_name
;
4147 boolean_t dp_descend
;
4151 typedef struct deleg_perm_node
{
4152 deleg_perm_t dpn_perm
;
4154 uu_avl_node_t dpn_avl_node
;
4155 } deleg_perm_node_t
;
4157 typedef struct fs_perm fs_perm_t
;
4159 /* permissions set */
4160 typedef struct who_perm
{
4161 zfs_deleg_who_type_t who_type
;
4162 const char *who_name
; /* id */
4163 char who_ug_name
[256]; /* user/group name */
4164 fs_perm_t
*who_fsperm
; /* uplink */
4166 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
4170 typedef struct who_perm_node
{
4171 who_perm_t who_perm
;
4172 uu_avl_node_t who_avl_node
;
4175 typedef struct fs_perm_set fs_perm_set_t
;
4176 /* fs permissions */
4178 const char *fsp_name
;
4180 uu_avl_t
*fsp_sc_avl
; /* sets,create */
4181 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
4183 fs_perm_set_t
*fsp_set
; /* uplink */
4187 typedef struct fs_perm_node
{
4188 fs_perm_t fspn_fsperm
;
4191 uu_list_node_t fspn_list_node
;
4194 /* top level structure */
4195 struct fs_perm_set
{
4196 uu_list_pool_t
*fsps_list_pool
;
4197 uu_list_t
*fsps_list
; /* list of fs_perms */
4199 uu_avl_pool_t
*fsps_named_set_avl_pool
;
4200 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
4201 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
4204 static inline const char *
4205 deleg_perm_type(zfs_deleg_note_t note
)
4211 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4212 case ZFS_DELEG_NOTE_GROUPUSED
:
4213 case ZFS_DELEG_NOTE_USERPROP
:
4214 case ZFS_DELEG_NOTE_USERQUOTA
:
4215 case ZFS_DELEG_NOTE_USERUSED
:
4217 return (gettext("other"));
4219 return (gettext("subcommand"));
4224 who_type2weight(zfs_deleg_who_type_t who_type
)
4228 case ZFS_DELEG_NAMED_SET_SETS
:
4229 case ZFS_DELEG_NAMED_SET
:
4232 case ZFS_DELEG_CREATE_SETS
:
4233 case ZFS_DELEG_CREATE
:
4236 case ZFS_DELEG_USER_SETS
:
4237 case ZFS_DELEG_USER
:
4240 case ZFS_DELEG_GROUP_SETS
:
4241 case ZFS_DELEG_GROUP
:
4244 case ZFS_DELEG_EVERYONE_SETS
:
4245 case ZFS_DELEG_EVERYONE
:
4257 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4259 const who_perm_node_t
*l
= larg
;
4260 const who_perm_node_t
*r
= rarg
;
4261 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
4262 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
4263 int lweight
= who_type2weight(ltype
);
4264 int rweight
= who_type2weight(rtype
);
4265 int res
= lweight
- rweight
;
4267 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
4268 ZFS_MAX_DELEG_NAME
-1);
4280 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4282 const deleg_perm_node_t
*l
= larg
;
4283 const deleg_perm_node_t
*r
= rarg
;
4284 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
4285 ZFS_MAX_DELEG_NAME
-1);
4297 fs_perm_set_init(fs_perm_set_t
*fspset
)
4299 bzero(fspset
, sizeof (fs_perm_set_t
));
4301 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4302 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4303 NULL
, UU_DEFAULT
)) == NULL
)
4305 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4306 UU_DEFAULT
)) == NULL
)
4309 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4310 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4311 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4312 UU_DEFAULT
)) == NULL
)
4315 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4316 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4317 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4318 UU_DEFAULT
)) == NULL
)
4321 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4322 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4323 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4328 static inline void fs_perm_fini(fs_perm_t
*);
4329 static inline void who_perm_fini(who_perm_t
*);
4332 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4334 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4336 while (node
!= NULL
) {
4337 fs_perm_node_t
*next_node
=
4338 uu_list_next(fspset
->fsps_list
, node
);
4339 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4340 fs_perm_fini(fsperm
);
4341 uu_list_remove(fspset
->fsps_list
, node
);
4346 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4347 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4348 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4352 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4355 deleg_perm
->dp_who_type
= type
;
4356 deleg_perm
->dp_name
= name
;
4360 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4361 zfs_deleg_who_type_t type
, const char *name
)
4363 uu_avl_pool_t
*pool
;
4364 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4366 bzero(who_perm
, sizeof (who_perm_t
));
4368 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4369 UU_DEFAULT
)) == NULL
)
4372 who_perm
->who_type
= type
;
4373 who_perm
->who_name
= name
;
4374 who_perm
->who_fsperm
= fsperm
;
4378 who_perm_fini(who_perm_t
*who_perm
)
4380 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4382 while (node
!= NULL
) {
4383 deleg_perm_node_t
*next_node
=
4384 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4386 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4391 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4395 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4397 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4398 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4400 bzero(fsperm
, sizeof (fs_perm_t
));
4402 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4406 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4410 fsperm
->fsp_set
= fspset
;
4411 fsperm
->fsp_name
= fsname
;
4415 fs_perm_fini(fs_perm_t
*fsperm
)
4417 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4418 while (node
!= NULL
) {
4419 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4421 who_perm_t
*who_perm
= &node
->who_perm
;
4422 who_perm_fini(who_perm
);
4423 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4428 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4429 while (node
!= NULL
) {
4430 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4432 who_perm_t
*who_perm
= &node
->who_perm
;
4433 who_perm_fini(who_perm
);
4434 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4439 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4440 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4444 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4445 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4447 uu_avl_index_t idx
= 0;
4449 deleg_perm_node_t
*found_node
= NULL
;
4450 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4452 deleg_perm_init(deleg_perm
, who_type
, name
);
4454 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4456 uu_avl_insert(avl
, node
, idx
);
4459 deleg_perm
= &node
->dpn_perm
;
4464 case ZFS_DELEG_LOCAL
:
4465 deleg_perm
->dp_local
= B_TRUE
;
4467 case ZFS_DELEG_DESCENDENT
:
4468 deleg_perm
->dp_descend
= B_TRUE
;
4473 assert(B_FALSE
); /* invalid locality */
4478 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4480 nvpair_t
*nvp
= NULL
;
4481 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4482 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4483 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4485 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4486 const char *name
= nvpair_name(nvp
);
4487 data_type_t type
= nvpair_type(nvp
);
4488 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4489 deleg_perm_node_t
*node
=
4490 safe_malloc(sizeof (deleg_perm_node_t
));
4492 assert(type
== DATA_TYPE_BOOLEAN
);
4494 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4495 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4502 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4504 nvpair_t
*nvp
= NULL
;
4505 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4507 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4508 nvlist_t
*nvl2
= NULL
;
4509 const char *name
= nvpair_name(nvp
);
4510 uu_avl_t
*avl
= NULL
;
4511 uu_avl_pool_t
*avl_pool
= NULL
;
4512 zfs_deleg_who_type_t perm_type
= name
[0];
4513 char perm_locality
= name
[1];
4514 const char *perm_name
= name
+ 3;
4515 boolean_t is_set
= B_TRUE
;
4516 who_perm_t
*who_perm
= NULL
;
4518 assert('$' == name
[2]);
4520 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4523 switch (perm_type
) {
4524 case ZFS_DELEG_CREATE
:
4525 case ZFS_DELEG_CREATE_SETS
:
4526 case ZFS_DELEG_NAMED_SET
:
4527 case ZFS_DELEG_NAMED_SET_SETS
:
4528 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4529 avl
= fsperm
->fsp_sc_avl
;
4531 case ZFS_DELEG_USER
:
4532 case ZFS_DELEG_USER_SETS
:
4533 case ZFS_DELEG_GROUP
:
4534 case ZFS_DELEG_GROUP_SETS
:
4535 case ZFS_DELEG_EVERYONE
:
4536 case ZFS_DELEG_EVERYONE_SETS
:
4537 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4538 avl
= fsperm
->fsp_uge_avl
;
4542 assert(!"unhandled zfs_deleg_who_type_t");
4546 who_perm_node_t
*found_node
= NULL
;
4547 who_perm_node_t
*node
= safe_malloc(
4548 sizeof (who_perm_node_t
));
4549 who_perm
= &node
->who_perm
;
4550 uu_avl_index_t idx
= 0;
4552 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4553 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4555 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4557 if (avl
== fsperm
->fsp_uge_avl
) {
4559 struct passwd
*p
= NULL
;
4560 struct group
*g
= NULL
;
4561 const char *nice_name
= NULL
;
4563 switch (perm_type
) {
4564 case ZFS_DELEG_USER_SETS
:
4565 case ZFS_DELEG_USER
:
4566 rid
= atoi(perm_name
);
4569 nice_name
= p
->pw_name
;
4571 case ZFS_DELEG_GROUP_SETS
:
4572 case ZFS_DELEG_GROUP
:
4573 rid
= atoi(perm_name
);
4576 nice_name
= g
->gr_name
;
4583 if (nice_name
!= NULL
)
4585 node
->who_perm
.who_ug_name
,
4589 uu_avl_insert(avl
, node
, idx
);
4592 who_perm
= &node
->who_perm
;
4596 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4603 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4605 nvpair_t
*nvp
= NULL
;
4606 uu_avl_index_t idx
= 0;
4608 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4609 nvlist_t
*nvl2
= NULL
;
4610 const char *fsname
= nvpair_name(nvp
);
4611 data_type_t type
= nvpair_type(nvp
);
4612 fs_perm_t
*fsperm
= NULL
;
4613 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4617 fsperm
= &node
->fspn_fsperm
;
4619 assert(DATA_TYPE_NVLIST
== type
);
4621 uu_list_node_init(node
, &node
->fspn_list_node
,
4622 fspset
->fsps_list_pool
);
4624 idx
= uu_list_numnodes(fspset
->fsps_list
);
4625 fs_perm_init(fsperm
, fspset
, fsname
);
4627 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4630 (void) parse_fs_perm(fsperm
, nvl2
);
4632 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4638 static inline const char *
4639 deleg_perm_comment(zfs_deleg_note_t note
)
4641 const char *str
= "";
4646 case ZFS_DELEG_NOTE_ALLOW
:
4647 str
= gettext("Must also have the permission that is being"
4648 "\n\t\t\t\tallowed");
4650 case ZFS_DELEG_NOTE_CLONE
:
4651 str
= gettext("Must also have the 'create' ability and 'mount'"
4652 "\n\t\t\t\tability in the origin file system");
4654 case ZFS_DELEG_NOTE_CREATE
:
4655 str
= gettext("Must also have the 'mount' ability");
4657 case ZFS_DELEG_NOTE_DESTROY
:
4658 str
= gettext("Must also have the 'mount' ability");
4660 case ZFS_DELEG_NOTE_DIFF
:
4661 str
= gettext("Allows lookup of paths within a dataset;"
4662 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4663 "\n\t\t\t\tin order to use zfs diff");
4665 case ZFS_DELEG_NOTE_HOLD
:
4666 str
= gettext("Allows adding a user hold to a snapshot");
4668 case ZFS_DELEG_NOTE_MOUNT
:
4669 str
= gettext("Allows mount/umount of ZFS datasets");
4671 case ZFS_DELEG_NOTE_PROMOTE
:
4672 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4673 " 'promote' ability in the origin file system");
4675 case ZFS_DELEG_NOTE_RECEIVE
:
4676 str
= gettext("Must also have the 'mount' and 'create'"
4679 case ZFS_DELEG_NOTE_RELEASE
:
4680 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4681 "might destroy the snapshot");
4683 case ZFS_DELEG_NOTE_RENAME
:
4684 str
= gettext("Must also have the 'mount' and 'create'"
4685 "\n\t\t\t\tability in the new parent");
4687 case ZFS_DELEG_NOTE_ROLLBACK
:
4690 case ZFS_DELEG_NOTE_SEND
:
4693 case ZFS_DELEG_NOTE_SHARE
:
4694 str
= gettext("Allows sharing file systems over NFS or SMB"
4695 "\n\t\t\t\tprotocols");
4697 case ZFS_DELEG_NOTE_SNAPSHOT
:
4701 * case ZFS_DELEG_NOTE_VSCAN:
4702 * str = gettext("");
4706 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4707 str
= gettext("Allows accessing any groupquota@... property");
4709 case ZFS_DELEG_NOTE_GROUPUSED
:
4710 str
= gettext("Allows reading any groupused@... property");
4712 case ZFS_DELEG_NOTE_USERPROP
:
4713 str
= gettext("Allows changing any user property");
4715 case ZFS_DELEG_NOTE_USERQUOTA
:
4716 str
= gettext("Allows accessing any userquota@... property");
4718 case ZFS_DELEG_NOTE_USERUSED
:
4719 str
= gettext("Allows reading any userused@... property");
4737 boolean_t recursive
; /* unallow only */
4738 boolean_t prt_usage
;
4740 boolean_t prt_perms
;
4743 const char *dataset
;
4747 prop_cmp(const void *a
, const void *b
)
4749 const char *str1
= *(const char **)a
;
4750 const char *str2
= *(const char **)b
;
4751 return (strcmp(str1
, str2
));
4755 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
4757 const char *opt_desc
[] = {
4758 "-h", gettext("show this help message and exit"),
4759 "-l", gettext("set permission locally"),
4760 "-d", gettext("set permission for descents"),
4761 "-u", gettext("set permission for user"),
4762 "-g", gettext("set permission for group"),
4763 "-e", gettext("set permission for everyone"),
4764 "-c", gettext("set create time permission"),
4765 "-s", gettext("define permission set"),
4767 "-r", gettext("remove permissions recursively"),
4769 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
4770 size_t allow_size
= unallow_size
- 2;
4771 const char *props
[ZFS_NUM_PROPS
];
4774 FILE *fp
= requested
? stdout
: stderr
;
4775 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
4776 const char *fmt
= gettext("%-16s %-14s\t%s\n");
4778 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
4780 (void) fprintf(fp
, gettext("Options:\n"));
4781 for (int i
= 0; i
< (un
? unallow_size
: allow_size
); i
++) {
4782 const char *opt
= opt_desc
[i
++];
4783 const char *optdsc
= opt_desc
[i
];
4784 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
4787 (void) fprintf(fp
, gettext("\nThe following permissions are "
4789 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
4791 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
4792 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
4793 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
4794 const char *perm_type
= deleg_perm_type(perm_note
);
4795 const char *perm_comment
= deleg_perm_comment(perm_note
);
4796 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
4799 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
4800 zprop_desc_t
*pd
= &pdtbl
[i
];
4801 if (pd
->pd_visible
!= B_TRUE
)
4804 if (pd
->pd_attr
== PROP_READONLY
)
4807 props
[count
++] = pd
->pd_name
;
4809 props
[count
] = NULL
;
4811 qsort(props
, count
, sizeof (char *), prop_cmp
);
4813 for (i
= 0; i
< count
; i
++)
4814 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
4817 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
4819 exit(requested
? 0 : 2);
4822 static inline const char *
4823 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
4826 if (un
&& argc
== expected_argc
- 1)
4828 else if (argc
== expected_argc
)
4829 *permsp
= argv
[argc
- 2];
4831 allow_usage(un
, B_FALSE
,
4832 gettext("wrong number of parameters\n"));
4834 return (argv
[argc
- 1]);
4838 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
4840 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
4841 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
4842 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
4843 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
4846 allow_usage(un
, B_FALSE
,
4847 gettext("-u, -g, and -e are mutually exclusive\n"));
4849 if (opts
->prt_usage
) {
4850 if (argc
== 0 && all_sum
== 0)
4851 allow_usage(un
, B_TRUE
, NULL
);
4858 allow_usage(un
, B_FALSE
,
4859 gettext("invalid options combined with -s\n"));
4861 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4862 if (argv
[0][0] != '@')
4863 allow_usage(un
, B_FALSE
,
4864 gettext("invalid set name: missing '@' prefix\n"));
4865 opts
->who
= argv
[0];
4866 } else if (opts
->create
) {
4867 if (ldcsuge_sum
> 1)
4868 allow_usage(un
, B_FALSE
,
4869 gettext("invalid options combined with -c\n"));
4870 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4871 } else if (opts
->everyone
) {
4873 allow_usage(un
, B_FALSE
,
4874 gettext("invalid options combined with -e\n"));
4875 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4876 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
4878 opts
->everyone
= B_TRUE
;
4881 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4882 } else if (argc
== 1 && !un
) {
4883 opts
->prt_perms
= B_TRUE
;
4884 opts
->dataset
= argv
[argc
-1];
4886 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4887 opts
->who
= argv
[0];
4890 if (!opts
->local
&& !opts
->descend
) {
4891 opts
->local
= B_TRUE
;
4892 opts
->descend
= B_TRUE
;
4897 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
4898 const char *who
, char *perms
, nvlist_t
*top_nvl
)
4901 char ld
[2] = { '\0', '\0' };
4902 char who_buf
[MAXNAMELEN
+ 32];
4903 char base_type
= '\0';
4904 char set_type
= '\0';
4905 nvlist_t
*base_nvl
= NULL
;
4906 nvlist_t
*set_nvl
= NULL
;
4909 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4911 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4915 case ZFS_DELEG_NAMED_SET_SETS
:
4916 case ZFS_DELEG_NAMED_SET
:
4917 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
4918 base_type
= ZFS_DELEG_NAMED_SET
;
4919 ld
[0] = ZFS_DELEG_NA
;
4921 case ZFS_DELEG_CREATE_SETS
:
4922 case ZFS_DELEG_CREATE
:
4923 set_type
= ZFS_DELEG_CREATE_SETS
;
4924 base_type
= ZFS_DELEG_CREATE
;
4925 ld
[0] = ZFS_DELEG_NA
;
4927 case ZFS_DELEG_USER_SETS
:
4928 case ZFS_DELEG_USER
:
4929 set_type
= ZFS_DELEG_USER_SETS
;
4930 base_type
= ZFS_DELEG_USER
;
4932 ld
[0] = ZFS_DELEG_LOCAL
;
4934 ld
[1] = ZFS_DELEG_DESCENDENT
;
4936 case ZFS_DELEG_GROUP_SETS
:
4937 case ZFS_DELEG_GROUP
:
4938 set_type
= ZFS_DELEG_GROUP_SETS
;
4939 base_type
= ZFS_DELEG_GROUP
;
4941 ld
[0] = ZFS_DELEG_LOCAL
;
4943 ld
[1] = ZFS_DELEG_DESCENDENT
;
4945 case ZFS_DELEG_EVERYONE_SETS
:
4946 case ZFS_DELEG_EVERYONE
:
4947 set_type
= ZFS_DELEG_EVERYONE_SETS
;
4948 base_type
= ZFS_DELEG_EVERYONE
;
4950 ld
[0] = ZFS_DELEG_LOCAL
;
4952 ld
[1] = ZFS_DELEG_DESCENDENT
;
4956 assert(set_type
!= '\0' && base_type
!= '\0');
4959 if (perms
!= NULL
) {
4961 char *end
= curr
+ strlen(perms
);
4963 while (curr
< end
) {
4964 char *delim
= strchr(curr
, ',');
4975 (void) nvlist_add_boolean(nvl
, curr
);
4981 for (i
= 0; i
< 2; i
++) {
4982 char locality
= ld
[i
];
4986 if (!nvlist_empty(base_nvl
)) {
4988 (void) snprintf(who_buf
,
4989 sizeof (who_buf
), "%c%c$%s",
4990 base_type
, locality
, who
);
4992 (void) snprintf(who_buf
,
4993 sizeof (who_buf
), "%c%c$",
4994 base_type
, locality
);
4996 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5001 if (!nvlist_empty(set_nvl
)) {
5003 (void) snprintf(who_buf
,
5004 sizeof (who_buf
), "%c%c$%s",
5005 set_type
, locality
, who
);
5007 (void) snprintf(who_buf
,
5008 sizeof (who_buf
), "%c%c$",
5009 set_type
, locality
);
5011 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5016 for (i
= 0; i
< 2; i
++) {
5017 char locality
= ld
[i
];
5022 (void) snprintf(who_buf
, sizeof (who_buf
),
5023 "%c%c$%s", base_type
, locality
, who
);
5025 (void) snprintf(who_buf
, sizeof (who_buf
),
5026 "%c%c$", base_type
, locality
);
5027 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5030 (void) snprintf(who_buf
, sizeof (who_buf
),
5031 "%c%c$%s", set_type
, locality
, who
);
5033 (void) snprintf(who_buf
, sizeof (who_buf
),
5034 "%c%c$", set_type
, locality
);
5035 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5041 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
5043 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
5047 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
5048 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
5049 } else if (opts
->create
) {
5050 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
5051 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5052 } else if (opts
->everyone
) {
5053 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
5054 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5056 char *curr
= opts
->who
;
5057 char *end
= curr
+ strlen(curr
);
5059 while (curr
< end
) {
5061 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
5063 char *delim
= strchr(curr
, ',');
5066 struct passwd
*p
= NULL
;
5067 struct group
*g
= NULL
;
5075 rid
= (uid_t
)strtol(curr
, &endch
, 0);
5077 who_type
= ZFS_DELEG_USER
;
5086 (void) snprintf(errbuf
, 256, gettext(
5087 "invalid user %s"), curr
);
5088 allow_usage(un
, B_TRUE
, errbuf
);
5090 } else if (opts
->group
) {
5091 who_type
= ZFS_DELEG_GROUP
;
5100 (void) snprintf(errbuf
, 256, gettext(
5101 "invalid group %s"), curr
);
5102 allow_usage(un
, B_TRUE
, errbuf
);
5105 if (*endch
!= '\0') {
5112 if (*endch
!= '\0') {
5120 who_type
= ZFS_DELEG_USER
;
5122 } else if (g
!= NULL
) {
5123 who_type
= ZFS_DELEG_GROUP
;
5126 (void) snprintf(errbuf
, 256, gettext(
5127 "invalid user/group %s"), curr
);
5128 allow_usage(un
, B_TRUE
, errbuf
);
5132 (void) sprintf(id
, "%u", rid
);
5135 store_allow_perm(who_type
, opts
->local
,
5136 opts
->descend
, who
, opts
->perms
, *nvlp
);
5145 print_set_creat_perms(uu_avl_t
*who_avl
)
5147 const char *sc_title
[] = {
5148 gettext("Permission sets:\n"),
5149 gettext("Create time permissions:\n"),
5152 const char **title_ptr
= sc_title
;
5153 who_perm_node_t
*who_node
= NULL
;
5154 int prev_weight
= -1;
5156 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
5157 who_node
= uu_avl_next(who_avl
, who_node
)) {
5158 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5159 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5160 const char *who_name
= who_node
->who_perm
.who_name
;
5161 int weight
= who_type2weight(who_type
);
5162 boolean_t first
= B_TRUE
;
5163 deleg_perm_node_t
*deleg_node
;
5165 if (prev_weight
!= weight
) {
5166 (void) printf(*title_ptr
++);
5167 prev_weight
= weight
;
5170 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
5171 (void) printf("\t");
5173 (void) printf("\t%s ", who_name
);
5175 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
5176 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5179 deleg_node
->dpn_perm
.dp_name
);
5182 (void) printf(",%s",
5183 deleg_node
->dpn_perm
.dp_name
);
5186 (void) printf("\n");
5191 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
5194 who_perm_node_t
*who_node
= NULL
;
5195 boolean_t prt_title
= B_TRUE
;
5196 uu_avl_walk_t
*walk
;
5198 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
5201 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
5202 const char *who_name
= who_node
->who_perm
.who_name
;
5203 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
5204 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5205 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5207 deleg_perm_node_t
*deleg_node
;
5208 boolean_t prt_who
= B_TRUE
;
5210 for (deleg_node
= uu_avl_first(avl
);
5212 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5213 if (local
!= deleg_node
->dpn_perm
.dp_local
||
5214 descend
!= deleg_node
->dpn_perm
.dp_descend
)
5218 const char *who
= NULL
;
5220 prt_title
= B_FALSE
;
5221 (void) printf(title
);
5225 case ZFS_DELEG_USER_SETS
:
5226 case ZFS_DELEG_USER
:
5227 who
= gettext("user");
5229 who_name
= nice_who_name
;
5231 case ZFS_DELEG_GROUP_SETS
:
5232 case ZFS_DELEG_GROUP
:
5233 who
= gettext("group");
5235 who_name
= nice_who_name
;
5237 case ZFS_DELEG_EVERYONE_SETS
:
5238 case ZFS_DELEG_EVERYONE
:
5239 who
= gettext("everyone");
5244 assert(who
!= NULL
);
5248 if (who_name
== NULL
)
5249 (void) printf("\t%s", who
);
5251 (void) printf("\t%s %s", who
, who_name
);
5254 (void) printf("%c%s", delim
,
5255 deleg_node
->dpn_perm
.dp_name
);
5260 (void) printf("\n");
5263 uu_avl_walk_end(walk
);
5267 print_fs_perms(fs_perm_set_t
*fspset
)
5269 fs_perm_node_t
*node
= NULL
;
5270 char buf
[MAXNAMELEN
+ 32];
5271 const char *dsname
= buf
;
5273 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
5274 node
= uu_list_next(fspset
->fsps_list
, node
)) {
5275 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
5276 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
5279 (void) snprintf(buf
, sizeof (buf
),
5280 gettext("---- Permissions on %s "),
5281 node
->fspn_fsperm
.fsp_name
);
5282 (void) printf(dsname
);
5283 left
= 70 - strlen(buf
);
5286 (void) printf("\n");
5288 print_set_creat_perms(sc_avl
);
5289 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
5290 gettext("Local permissions:\n"));
5291 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
5292 gettext("Descendent permissions:\n"));
5293 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
5294 gettext("Local+Descendent permissions:\n"));
5298 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5300 struct deleg_perms
{
5306 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5308 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5309 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5311 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5314 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5318 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5321 nvlist_t
*perm_nvl
= NULL
;
5322 nvlist_t
*update_perm_nvl
= NULL
;
5325 struct allow_opts opts
= { 0 };
5327 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5330 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5333 opts
.local
= B_TRUE
;
5336 opts
.descend
= B_TRUE
;
5342 opts
.group
= B_TRUE
;
5345 opts
.everyone
= B_TRUE
;
5351 opts
.create
= B_TRUE
;
5354 opts
.recursive
= B_TRUE
;
5357 (void) fprintf(stderr
, gettext("missing argument for "
5358 "'%c' option\n"), optopt
);
5362 opts
.prt_usage
= B_TRUE
;
5365 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5374 /* check arguments */
5375 parse_allow_args(argc
, argv
, un
, &opts
);
5377 /* try to open the dataset */
5378 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5379 ZFS_TYPE_VOLUME
)) == NULL
) {
5380 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5385 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5388 fs_perm_set_init(&fs_perm_set
);
5389 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5390 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5395 print_fs_perms(&fs_perm_set
);
5397 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5398 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5401 if (un
&& opts
.recursive
) {
5402 struct deleg_perms data
= { un
, update_perm_nvl
};
5403 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5412 nvlist_free(perm_nvl
);
5413 nvlist_free(update_perm_nvl
);
5415 fs_perm_set_fini(&fs_perm_set
);
5423 zfs_do_allow(int argc
, char **argv
)
5425 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5429 zfs_do_unallow(int argc
, char **argv
)
5431 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5435 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5440 boolean_t recursive
= B_FALSE
;
5441 const char *opts
= holding
? "rt" : "r";
5445 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5451 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5460 /* check number of arguments */
5468 if (holding
&& tag
[0] == '.') {
5469 /* tags starting with '.' are reserved for libzfs */
5470 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5474 for (i
= 0; i
< argc
; ++i
) {
5476 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
5478 char *path
= argv
[i
];
5480 delim
= strchr(path
, '@');
5481 if (delim
== NULL
) {
5482 (void) fprintf(stderr
,
5483 gettext("'%s' is not a snapshot\n"), path
);
5487 (void) strncpy(parent
, path
, delim
- path
);
5488 parent
[delim
- path
] = '\0';
5490 zhp
= zfs_open(g_zfs
, parent
,
5491 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5497 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
5500 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5506 return (errors
!= 0);
5510 * zfs hold [-r] [-t] <tag> <snap> ...
5512 * -r Recursively hold
5514 * Apply a user-hold with the given tag to the list of snapshots.
5517 zfs_do_hold(int argc
, char **argv
)
5519 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5523 * zfs release [-r] <tag> <snap> ...
5525 * -r Recursively release
5527 * Release a user-hold with the given tag from the list of snapshots.
5530 zfs_do_release(int argc
, char **argv
)
5532 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5535 typedef struct holds_cbdata
{
5536 boolean_t cb_recursive
;
5537 const char *cb_snapname
;
5539 size_t cb_max_namelen
;
5540 size_t cb_max_taglen
;
5543 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5544 #define DATETIME_BUF_LEN (32)
5549 print_holds(boolean_t scripted
, size_t nwidth
, size_t tagwidth
, nvlist_t
*nvl
)
5552 nvpair_t
*nvp
= NULL
;
5553 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5557 for (i
= 0; i
< 3; i
++) {
5558 col
= gettext(hdr_cols
[i
]);
5560 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5563 (void) printf("%s\n", col
);
5567 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5568 char *zname
= nvpair_name(nvp
);
5570 nvpair_t
*nvp2
= NULL
;
5571 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5572 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5573 char tsbuf
[DATETIME_BUF_LEN
];
5574 char *tagname
= nvpair_name(nvp2
);
5578 char sep
= scripted
? '\t' : ' ';
5579 size_t sepnum
= scripted
? 1 : 2;
5581 (void) nvpair_value_uint64(nvp2
, &val
);
5583 (void) localtime_r(&time
, &t
);
5584 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5585 gettext(STRFTIME_FMT_STR
), &t
);
5587 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth
, zname
,
5588 sepnum
, sep
, tagwidth
, tagname
, sepnum
, sep
, tsbuf
);
5594 * Generic callback function to list a dataset or snapshot.
5597 holds_callback(zfs_handle_t
*zhp
, void *data
)
5599 holds_cbdata_t
*cbp
= data
;
5600 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5601 nvlist_t
*nvl
= NULL
;
5602 nvpair_t
*nvp
= NULL
;
5603 const char *zname
= zfs_get_name(zhp
);
5604 size_t znamelen
= strlen(zname
);
5606 if (cbp
->cb_recursive
) {
5607 const char *snapname
;
5608 char *delim
= strchr(zname
, '@');
5612 snapname
= delim
+ 1;
5613 if (strcmp(cbp
->cb_snapname
, snapname
))
5617 if (zfs_get_holds(zhp
, &nvl
) != 0)
5620 if (znamelen
> cbp
->cb_max_namelen
)
5621 cbp
->cb_max_namelen
= znamelen
;
5623 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5624 const char *tag
= nvpair_name(nvp
);
5625 size_t taglen
= strlen(tag
);
5626 if (taglen
> cbp
->cb_max_taglen
)
5627 cbp
->cb_max_taglen
= taglen
;
5630 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5634 * zfs holds [-r] <snap> ...
5636 * -r Recursively hold
5639 zfs_do_holds(int argc
, char **argv
)
5644 boolean_t scripted
= B_FALSE
;
5645 boolean_t recursive
= B_FALSE
;
5646 const char *opts
= "rH";
5649 int types
= ZFS_TYPE_SNAPSHOT
;
5650 holds_cbdata_t cb
= { 0 };
5657 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5666 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5673 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
5674 flags
|= ZFS_ITER_RECURSE
;
5680 /* check number of arguments */
5684 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5687 for (i
= 0; i
< argc
; ++i
) {
5688 char *snapshot
= argv
[i
];
5690 const char *snapname
;
5692 delim
= strchr(snapshot
, '@');
5693 if (delim
== NULL
) {
5694 (void) fprintf(stderr
,
5695 gettext("'%s' is not a snapshot\n"), snapshot
);
5699 snapname
= delim
+ 1;
5701 snapshot
[delim
- snapshot
] = '\0';
5703 cb
.cb_recursive
= recursive
;
5704 cb
.cb_snapname
= snapname
;
5708 * 1. collect holds data, set format options
5710 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
5711 holds_callback
, &cb
);
5717 * 2. print holds data
5719 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
5721 if (nvlist_empty(nvl
))
5722 (void) printf(gettext("no datasets available\n"));
5726 return (0 != errors
);
5729 #define CHECK_SPINNER 30
5730 #define SPINNER_TIME 3 /* seconds */
5731 #define MOUNT_TIME 5 /* seconds */
5734 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
5736 static char *spin
[] = { "-", "\\", "|", "/" };
5737 static int spinval
= 0;
5738 static int spincheck
= 0;
5739 static time_t last_spin_time
= (time_t)0;
5740 get_all_cb_t
*cbp
= data
;
5741 zfs_type_t type
= zfs_get_type(zhp
);
5743 if (cbp
->cb_verbose
) {
5744 if (--spincheck
< 0) {
5745 time_t now
= time(NULL
);
5746 if (last_spin_time
+ SPINNER_TIME
< now
) {
5747 update_progress(spin
[spinval
++ % 4]);
5748 last_spin_time
= now
;
5750 spincheck
= CHECK_SPINNER
;
5755 * Interate over any nested datasets.
5757 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
5763 * Skip any datasets whose type does not match.
5765 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
5769 libzfs_add_handle(cbp
, zhp
);
5770 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
5776 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
5778 get_all_cb_t cb
= { 0 };
5779 cb
.cb_verbose
= verbose
;
5780 cb
.cb_getone
= get_one_dataset
;
5783 set_progress_header(gettext("Reading ZFS config"));
5784 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
5786 *dslist
= cb
.cb_handles
;
5787 *count
= cb
.cb_used
;
5790 finish_progress(gettext("done."));
5794 * Generic callback for sharing or mounting filesystems. Because the code is so
5795 * similar, we have a common function with an extra parameter to determine which
5796 * mode we are using.
5798 #define OP_SHARE 0x1
5799 #define OP_MOUNT 0x2
5802 * Share or mount a dataset.
5805 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
5806 boolean_t
explicit, const char *options
)
5808 char mountpoint
[ZFS_MAXPROPLEN
];
5809 char shareopts
[ZFS_MAXPROPLEN
];
5810 char smbshareopts
[ZFS_MAXPROPLEN
];
5811 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
5813 uint64_t zoned
, canmount
;
5814 boolean_t shared_nfs
, shared_smb
;
5816 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
5819 * Check to make sure we can mount/share this dataset. If we
5820 * are in the global zone and the filesystem is exported to a
5821 * local zone, or if we are in a local zone and the
5822 * filesystem is not exported, then it is an error.
5824 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
5826 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
5830 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5831 "dataset is exported to a local zone\n"), cmdname
,
5835 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
5839 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5840 "permission denied\n"), cmdname
,
5846 * Ignore any filesystems which don't apply to us. This
5847 * includes those with a legacy mountpoint, or those with
5848 * legacy share options.
5850 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
5851 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
5852 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
5853 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5854 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
5855 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5857 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
5858 strcmp(smbshareopts
, "off") == 0) {
5862 (void) fprintf(stderr
, gettext("cannot share '%s': "
5863 "legacy share\n"), zfs_get_name(zhp
));
5864 (void) fprintf(stderr
, gettext("use share(1M) to "
5865 "share this filesystem, or set "
5866 "sharenfs property on\n"));
5871 * We cannot share or mount legacy filesystems. If the
5872 * shareopts is non-legacy but the mountpoint is legacy, we
5873 * treat it as a legacy share.
5875 if (strcmp(mountpoint
, "legacy") == 0) {
5879 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5880 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
5881 (void) fprintf(stderr
, gettext("use %s(1M) to "
5882 "%s this filesystem\n"), cmdname
, cmdname
);
5886 if (strcmp(mountpoint
, "none") == 0) {
5890 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
5891 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
5896 * canmount explicit outcome
5897 * on no pass through
5898 * on yes pass through
5900 * off yes display error, return 1
5901 * noauto no return 0
5902 * noauto yes pass through
5904 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
5905 if (canmount
== ZFS_CANMOUNT_OFF
) {
5909 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5910 "'canmount' property is set to 'off'\n"), cmdname
,
5913 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
5918 * If this filesystem is inconsistent and has a receive resume
5919 * token, we can not mount it.
5921 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
5922 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
5923 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
5927 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5928 "Contains partially-completed state from "
5929 "\"zfs receive -r\", which can be resumed with "
5930 "\"zfs send -t\"\n"),
5931 cmdname
, zfs_get_name(zhp
));
5936 * At this point, we have verified that the mountpoint and/or
5937 * shareopts are appropriate for auto management. If the
5938 * filesystem is already mounted or shared, return (failing
5939 * for explicit requests); otherwise mount or share the
5945 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
5946 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
5948 if ((shared_nfs
&& shared_smb
) ||
5949 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
5950 strcmp(smbshareopts
, "off") == 0) ||
5951 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
5952 strcmp(shareopts
, "off") == 0)) {
5956 (void) fprintf(stderr
, gettext("cannot share "
5957 "'%s': filesystem already shared\n"),
5962 if (!zfs_is_mounted(zhp
, NULL
) &&
5963 zfs_mount(zhp
, NULL
, 0) != 0)
5966 if (protocol
== NULL
) {
5967 if (zfs_shareall(zhp
) != 0)
5969 } else if (strcmp(protocol
, "nfs") == 0) {
5970 if (zfs_share_nfs(zhp
))
5972 } else if (strcmp(protocol
, "smb") == 0) {
5973 if (zfs_share_smb(zhp
))
5976 (void) fprintf(stderr
, gettext("cannot share "
5977 "'%s': invalid share type '%s' "
5979 zfs_get_name(zhp
), protocol
);
5986 if (options
== NULL
)
5987 mnt
.mnt_mntopts
= "";
5989 mnt
.mnt_mntopts
= (char *)options
;
5991 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
5992 zfs_is_mounted(zhp
, NULL
)) {
5996 (void) fprintf(stderr
, gettext("cannot mount "
5997 "'%s': filesystem already mounted\n"),
6002 if (zfs_mount(zhp
, options
, flags
) != 0)
6011 * Reports progress in the form "(current/total)". Not thread-safe.
6014 report_mount_progress(int current
, int total
)
6016 static time_t last_progress_time
= 0;
6017 time_t now
= time(NULL
);
6020 /* report 1..n instead of 0..n-1 */
6023 /* display header if we're here for the first time */
6025 set_progress_header(gettext("Mounting ZFS filesystems"));
6026 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
6027 /* too soon to report again */
6031 last_progress_time
= now
;
6033 (void) sprintf(info
, "(%d/%d)", current
, total
);
6035 if (current
== total
)
6036 finish_progress(info
);
6038 update_progress(info
);
6042 append_options(char *mntopts
, char *newopts
)
6044 int len
= strlen(mntopts
);
6046 /* original length plus new string to append plus 1 for the comma */
6047 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
6048 (void) fprintf(stderr
, gettext("the opts argument for "
6049 "'%c' option is too long (more than %d chars)\n"),
6050 "-o", MNT_LINE_MAX
);
6055 mntopts
[len
++] = ',';
6057 (void) strcpy(&mntopts
[len
], newopts
);
6061 share_mount(int op
, int argc
, char **argv
)
6064 boolean_t verbose
= B_FALSE
;
6066 char *options
= NULL
;
6070 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
6080 if (*optarg
== '\0') {
6081 (void) fprintf(stderr
, gettext("empty mount "
6082 "options (-o) specified\n"));
6086 if (options
== NULL
)
6087 options
= safe_malloc(MNT_LINE_MAX
+ 1);
6089 /* option validation is done later */
6090 append_options(options
, optarg
);
6094 flags
|= MS_OVERLAY
;
6097 (void) fprintf(stderr
, gettext("missing argument for "
6098 "'%c' option\n"), optopt
);
6102 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6111 /* check number of arguments */
6113 zfs_handle_t
**dslist
= NULL
;
6114 size_t i
, count
= 0;
6115 char *protocol
= NULL
;
6117 if (op
== OP_SHARE
&& argc
> 0) {
6118 if (strcmp(argv
[0], "nfs") != 0 &&
6119 strcmp(argv
[0], "smb") != 0) {
6120 (void) fprintf(stderr
, gettext("share type "
6121 "must be 'nfs' or 'smb'\n"));
6130 (void) fprintf(stderr
, gettext("too many arguments\n"));
6134 start_progress_timer();
6135 get_all_datasets(&dslist
, &count
, verbose
);
6140 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
6141 sa_init_selective_arg_t sharearg
;
6142 sharearg
.zhandle_arr
= dslist
;
6143 sharearg
.zhandle_len
= count
;
6144 if ((ret
= zfs_init_libshare_arg(zfs_get_handle(dslist
[0]),
6145 SA_INIT_SHARE_API_SELECTIVE
, &sharearg
)) != SA_OK
) {
6146 (void) fprintf(stderr
,
6147 gettext("Could not initialize libshare, %d"), ret
);
6151 for (i
= 0; i
< count
; i
++) {
6153 report_mount_progress(i
, count
);
6155 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
6156 B_FALSE
, options
) != 0)
6158 zfs_close(dslist
[i
]);
6162 } else if (argc
== 0) {
6163 struct mnttab entry
;
6165 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
6166 (void) fprintf(stderr
, gettext("missing filesystem "
6167 "argument (specify -a for all)\n"));
6172 * When mount is given no arguments, go through /etc/mnttab and
6173 * display any active ZFS mounts. We hide any snapshots, since
6174 * they are controlled automatically.
6176 rewind(mnttab_file
);
6177 while (getmntent(mnttab_file
, &entry
) == 0) {
6178 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
6179 strchr(entry
.mnt_special
, '@') != NULL
)
6182 (void) printf("%-30s %s\n", entry
.mnt_special
,
6190 (void) fprintf(stderr
,
6191 gettext("too many arguments\n"));
6195 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6196 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6199 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
6209 * zfs mount -a [nfs]
6210 * zfs mount filesystem
6212 * Mount all filesystems, or mount the given filesystem.
6215 zfs_do_mount(int argc
, char **argv
)
6217 return (share_mount(OP_MOUNT
, argc
, argv
));
6221 * zfs share -a [nfs | smb]
6222 * zfs share filesystem
6224 * Share all filesystems, or share the given filesystem.
6227 zfs_do_share(int argc
, char **argv
)
6229 return (share_mount(OP_SHARE
, argc
, argv
));
6232 typedef struct unshare_unmount_node
{
6233 zfs_handle_t
*un_zhp
;
6235 uu_avl_node_t un_avlnode
;
6236 } unshare_unmount_node_t
;
6240 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
6242 const unshare_unmount_node_t
*l
= larg
;
6243 const unshare_unmount_node_t
*r
= rarg
;
6245 return (strcmp(l
->un_mountp
, r
->un_mountp
));
6249 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6250 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6251 * and unmount it appropriately.
6254 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
6258 struct stat64 statbuf
;
6259 struct extmnttab entry
;
6260 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
6264 * Search for the path in /etc/mnttab. Rather than looking for the
6265 * specific path, which can be fooled by non-standard paths (i.e. ".."
6266 * or "//"), we stat() the path and search for the corresponding
6267 * (major,minor) device pair.
6269 if (stat64(path
, &statbuf
) != 0) {
6270 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6271 cmdname
, path
, strerror(errno
));
6274 path_inode
= statbuf
.st_ino
;
6277 * Search for the given (major,minor) pair in the mount table.
6279 rewind(mnttab_file
);
6280 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
6281 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
6282 entry
.mnt_minor
== minor(statbuf
.st_dev
))
6286 if (op
== OP_SHARE
) {
6287 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
6288 "currently mounted\n"), cmdname
, path
);
6291 (void) fprintf(stderr
, gettext("warning: %s not in mnttab\n"),
6293 if ((ret
= umount2(path
, flags
)) != 0)
6294 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
6299 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
6300 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
6301 "filesystem\n"), cmdname
, path
);
6305 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6306 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6310 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
6311 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6312 cmdname
, path
, strerror(errno
));
6314 } else if (statbuf
.st_ino
!= path_inode
) {
6315 (void) fprintf(stderr
, gettext("cannot "
6316 "%s '%s': not a mountpoint\n"), cmdname
, path
);
6320 if (op
== OP_SHARE
) {
6321 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6322 char smbshare_prop
[ZFS_MAXPROPLEN
];
6324 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
6325 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6326 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
6327 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6329 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6330 strcmp(smbshare_prop
, "off") == 0) {
6331 (void) fprintf(stderr
, gettext("cannot unshare "
6332 "'%s': legacy share\n"), path
);
6333 (void) fprintf(stderr
, gettext("use "
6334 "unshare(1M) to unshare this filesystem\n"));
6335 } else if (!zfs_is_shared(zhp
)) {
6336 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6337 "not currently shared\n"), path
);
6339 ret
= zfs_unshareall_bypath(zhp
, path
);
6342 char mtpt_prop
[ZFS_MAXPROPLEN
];
6344 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6345 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6348 ret
= zfs_unmount(zhp
, NULL
, flags
);
6349 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6350 (void) fprintf(stderr
, gettext("cannot unmount "
6351 "'%s': legacy mountpoint\n"),
6353 (void) fprintf(stderr
, gettext("use umount(1M) "
6354 "to unmount this filesystem\n"));
6356 ret
= zfs_unmountall(zhp
, flags
);
6367 * Generic callback for unsharing or unmounting a filesystem.
6370 unshare_unmount(int op
, int argc
, char **argv
)
6377 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6378 char sharesmb
[ZFS_MAXPROPLEN
];
6381 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
6390 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6401 * We could make use of zfs_for_each() to walk all datasets in
6402 * the system, but this would be very inefficient, especially
6403 * since we would have to linearly search /etc/mnttab for each
6404 * one. Instead, do one pass through /etc/mnttab looking for
6405 * zfs entries and call zfs_unmount() for each one.
6407 * Things get a little tricky if the administrator has created
6408 * mountpoints beneath other ZFS filesystems. In this case, we
6409 * have to unmount the deepest filesystems first. To accomplish
6410 * this, we place all the mountpoints in an AVL tree sorted by
6411 * the special type (dataset name), and walk the result in
6412 * reverse to make sure to get any snapshots first.
6414 struct mnttab entry
;
6415 uu_avl_pool_t
*pool
;
6416 uu_avl_t
*tree
= NULL
;
6417 unshare_unmount_node_t
*node
;
6419 uu_avl_walk_t
*walk
;
6422 (void) fprintf(stderr
, gettext("too many arguments\n"));
6426 if (((pool
= uu_avl_pool_create("unmount_pool",
6427 sizeof (unshare_unmount_node_t
),
6428 offsetof(unshare_unmount_node_t
, un_avlnode
),
6429 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6430 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6433 rewind(mnttab_file
);
6434 while (getmntent(mnttab_file
, &entry
) == 0) {
6436 /* ignore non-ZFS entries */
6437 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6440 /* ignore snapshots */
6441 if (strchr(entry
.mnt_special
, '@') != NULL
)
6444 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6445 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6451 * Ignore datasets that are excluded/restricted by
6454 if (zpool_skip_pool(zfs_get_pool_name(zhp
))) {
6461 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6463 sizeof (nfs_mnt_prop
),
6464 NULL
, NULL
, 0, B_FALSE
) == 0);
6465 if (strcmp(nfs_mnt_prop
, "off") != 0)
6467 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6469 sizeof (nfs_mnt_prop
),
6470 NULL
, NULL
, 0, B_FALSE
) == 0);
6471 if (strcmp(nfs_mnt_prop
, "off") == 0)
6475 /* Ignore legacy mounts */
6476 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6478 sizeof (nfs_mnt_prop
),
6479 NULL
, NULL
, 0, B_FALSE
) == 0);
6480 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6482 /* Ignore canmount=noauto mounts */
6483 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6484 ZFS_CANMOUNT_NOAUTO
)
6490 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6492 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6494 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6496 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6497 uu_avl_insert(tree
, node
, idx
);
6499 zfs_close(node
->un_zhp
);
6500 free(node
->un_mountp
);
6506 * Walk the AVL tree in reverse, unmounting each filesystem and
6507 * removing it from the AVL tree in the process.
6509 if ((walk
= uu_avl_walk_start(tree
,
6510 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6513 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6514 uu_avl_remove(tree
, node
);
6518 if (zfs_unshareall_bypath(node
->un_zhp
,
6519 node
->un_mountp
) != 0)
6524 if (zfs_unmount(node
->un_zhp
,
6525 node
->un_mountp
, flags
) != 0)
6530 zfs_close(node
->un_zhp
);
6531 free(node
->un_mountp
);
6535 uu_avl_walk_end(walk
);
6536 uu_avl_destroy(tree
);
6537 uu_avl_pool_destroy(pool
);
6542 (void) fprintf(stderr
,
6543 gettext("missing filesystem argument\n"));
6545 (void) fprintf(stderr
,
6546 gettext("too many arguments\n"));
6551 * We have an argument, but it may be a full path or a ZFS
6552 * filesystem. Pass full paths off to unmount_path() (shared by
6553 * manual_unmount), otherwise open the filesystem and pass to
6556 if (argv
[0][0] == '/')
6557 return (unshare_unmount_path(op
, argv
[0],
6560 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6561 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6564 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
6565 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
6566 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
6567 NULL
, 0, B_FALSE
) == 0);
6571 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6573 sizeof (nfs_mnt_prop
),
6574 NULL
, NULL
, 0, B_FALSE
) == 0);
6575 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6576 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
6579 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6580 strcmp(sharesmb
, "off") == 0) {
6581 (void) fprintf(stderr
, gettext("cannot "
6582 "unshare '%s': legacy share\n"),
6584 (void) fprintf(stderr
, gettext("use "
6585 "unshare(1M) to unshare this "
6588 } else if (!zfs_is_shared(zhp
)) {
6589 (void) fprintf(stderr
, gettext("cannot "
6590 "unshare '%s': not currently "
6591 "shared\n"), zfs_get_name(zhp
));
6593 } else if (zfs_unshareall(zhp
) != 0) {
6599 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
6600 (void) fprintf(stderr
, gettext("cannot "
6601 "unmount '%s': legacy "
6602 "mountpoint\n"), zfs_get_name(zhp
));
6603 (void) fprintf(stderr
, gettext("use "
6604 "umount(1M) to unmount this "
6607 } else if (!zfs_is_mounted(zhp
, NULL
)) {
6608 (void) fprintf(stderr
, gettext("cannot "
6609 "unmount '%s': not currently "
6613 } else if (zfs_unmountall(zhp
, flags
) != 0) {
6627 * zfs unmount filesystem
6629 * Unmount all filesystems, or a specific ZFS filesystem.
6632 zfs_do_unmount(int argc
, char **argv
)
6634 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
6639 * zfs unshare filesystem
6641 * Unshare all filesystems, or a specific ZFS filesystem.
6644 zfs_do_unshare(int argc
, char **argv
)
6646 return (unshare_unmount(OP_SHARE
, argc
, argv
));
6650 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6651 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6654 manual_mount(int argc
, char **argv
)
6657 char mountpoint
[ZFS_MAXPROPLEN
];
6658 char mntopts
[MNT_LINE_MAX
] = { '\0' };
6662 char *dataset
, *path
;
6665 while ((c
= getopt(argc
, argv
, ":mo:O")) != -1) {
6668 (void) strlcpy(mntopts
, optarg
, sizeof (mntopts
));
6671 flags
|= MS_OVERLAY
;
6674 flags
|= MS_NOMNTTAB
;
6677 (void) fprintf(stderr
, gettext("missing argument for "
6678 "'%c' option\n"), optopt
);
6682 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6684 (void) fprintf(stderr
, gettext("usage: mount [-o opts] "
6693 /* check that we only have two arguments */
6696 (void) fprintf(stderr
, gettext("missing dataset "
6699 (void) fprintf(stderr
,
6700 gettext("missing mountpoint argument\n"));
6702 (void) fprintf(stderr
, gettext("too many arguments\n"));
6703 (void) fprintf(stderr
, "usage: mount <dataset> <mountpoint>\n");
6710 /* try to open the dataset */
6711 if ((zhp
= zfs_open(g_zfs
, dataset
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6714 (void) zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
6715 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
);
6717 /* check for legacy mountpoint and complain appropriately */
6719 if (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) == 0) {
6720 if (mount(dataset
, path
, MS_OPTIONSTR
| flags
, MNTTYPE_ZFS
,
6721 NULL
, 0, mntopts
, sizeof (mntopts
)) != 0) {
6722 (void) fprintf(stderr
, gettext("mount failed: %s\n"),
6727 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
6728 "mounted using 'mount -F zfs'\n"), dataset
);
6729 (void) fprintf(stderr
, gettext("Use 'zfs set mountpoint=%s' "
6730 "instead.\n"), path
);
6731 (void) fprintf(stderr
, gettext("If you must use 'mount -F zfs' "
6732 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
6733 (void) fprintf(stderr
, gettext("See zfs(1M) for more "
6742 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6743 * unmounts of non-legacy filesystems, as this is the dominant administrative
6747 manual_unmount(int argc
, char **argv
)
6753 while ((c
= getopt(argc
, argv
, "f")) != -1) {
6759 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6761 (void) fprintf(stderr
, gettext("usage: unmount [-f] "
6770 /* check arguments */
6773 (void) fprintf(stderr
, gettext("missing path "
6776 (void) fprintf(stderr
, gettext("too many arguments\n"));
6777 (void) fprintf(stderr
, gettext("usage: unmount [-f] <path>\n"));
6781 return (unshare_unmount_path(OP_MOUNT
, argv
[0], flags
, B_TRUE
));
6785 find_command_idx(char *command
, int *idx
)
6789 for (i
= 0; i
< NCOMMAND
; i
++) {
6790 if (command_table
[i
].name
== NULL
)
6793 if (strcmp(command
, command_table
[i
].name
) == 0) {
6802 zfs_do_diff(int argc
, char **argv
)
6806 char *tosnap
= NULL
;
6807 char *fromsnap
= NULL
;
6812 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
6815 flags
|= ZFS_DIFF_CLASSIFY
;
6818 flags
|= ZFS_DIFF_PARSEABLE
;
6821 flags
|= ZFS_DIFF_TIMESTAMP
;
6824 (void) fprintf(stderr
,
6825 gettext("invalid option '%c'\n"), optopt
);
6834 (void) fprintf(stderr
,
6835 gettext("must provide at least one snapshot name\n"));
6840 (void) fprintf(stderr
, gettext("too many arguments\n"));
6845 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
6848 if (*fromsnap
!= '@')
6849 copy
= strdup(fromsnap
);
6851 copy
= strdup(tosnap
);
6855 if ((atp
= strchr(copy
, '@')) != NULL
)
6858 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6864 * Ignore SIGPIPE so that the library can give us
6865 * information on any failure
6867 (void) sigignore(SIGPIPE
);
6869 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
6877 * zfs bookmark <fs@snap> <fs#bmark>
6879 * Creates a bookmark with the given name from the given snapshot.
6882 zfs_do_bookmark(int argc
, char **argv
)
6884 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
6891 while ((c
= getopt(argc
, argv
, "")) != -1) {
6894 (void) fprintf(stderr
,
6895 gettext("invalid option '%c'\n"), optopt
);
6903 /* check number of arguments */
6905 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
6909 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
6913 if (strchr(argv
[1], '#') == NULL
) {
6914 (void) fprintf(stderr
,
6915 gettext("invalid bookmark name '%s' -- "
6916 "must contain a '#'\n"), argv
[1]);
6920 if (argv
[0][0] == '@') {
6922 * Snapshot name begins with @.
6923 * Default to same fs as bookmark.
6925 (void) strncpy(snapname
, argv
[1], sizeof (snapname
));
6926 *strchr(snapname
, '#') = '\0';
6927 (void) strlcat(snapname
, argv
[0], sizeof (snapname
));
6929 (void) strncpy(snapname
, argv
[0], sizeof (snapname
));
6931 zhp
= zfs_open(g_zfs
, snapname
, ZFS_TYPE_SNAPSHOT
);
6937 nvl
= fnvlist_alloc();
6938 fnvlist_add_string(nvl
, argv
[1], snapname
);
6939 ret
= lzc_bookmark(nvl
, NULL
);
6943 const char *err_msg
;
6946 (void) snprintf(errbuf
, sizeof (errbuf
),
6947 dgettext(TEXT_DOMAIN
,
6948 "cannot create bookmark '%s'"), argv
[1]);
6952 err_msg
= "bookmark is in a different pool";
6955 err_msg
= "bookmark exists";
6958 err_msg
= "invalid argument";
6961 err_msg
= "bookmark feature not enabled";
6964 err_msg
= "out of space";
6967 err_msg
= "unknown error";
6970 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
6971 dgettext(TEXT_DOMAIN
, err_msg
));
6982 main(int argc
, char **argv
)
6989 (void) setlocale(LC_ALL
, "");
6990 (void) textdomain(TEXT_DOMAIN
);
6994 if ((g_zfs
= libzfs_init()) == NULL
) {
6995 (void) fprintf(stderr
, gettext("internal error: failed to "
6996 "initialize ZFS library\n"));
7000 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
7002 libzfs_print_on_error(g_zfs
, B_TRUE
);
7004 if ((mnttab_file
= fopen(MNTTAB
, "r")) == NULL
) {
7005 (void) fprintf(stderr
, gettext("internal error: unable to "
7006 "open %s\n"), MNTTAB
);
7011 * This command also doubles as the /etc/fs mount and unmount program.
7012 * Determine if we should take this behavior based on argv[0].
7014 progname
= basename(argv
[0]);
7015 if (strcmp(progname
, "mount") == 0) {
7016 ret
= manual_mount(argc
, argv
);
7017 } else if (strcmp(progname
, "umount") == 0) {
7018 ret
= manual_unmount(argc
, argv
);
7021 * Make sure the user has specified some command.
7024 (void) fprintf(stderr
, gettext("missing command\n"));
7031 * The 'umount' command is an alias for 'unmount'
7033 if (strcmp(cmdname
, "umount") == 0)
7034 cmdname
= "unmount";
7037 * The 'recv' command is an alias for 'receive'
7039 if (strcmp(cmdname
, "recv") == 0)
7040 cmdname
= "receive";
7043 * The 'snap' command is an alias for 'snapshot'
7045 if (strcmp(cmdname
, "snap") == 0)
7046 cmdname
= "snapshot";
7051 if (strcmp(cmdname
, "-?") == 0)
7055 * Run the appropriate command.
7057 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
7058 if (find_command_idx(cmdname
, &i
) == 0) {
7059 current_command
= &command_table
[i
];
7060 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
7061 } else if (strchr(cmdname
, '=') != NULL
) {
7062 verify(find_command_idx("set", &i
) == 0);
7063 current_command
= &command_table
[i
];
7064 ret
= command_table
[i
].func(argc
, argv
);
7066 (void) fprintf(stderr
, gettext("unrecognized "
7067 "command '%s'\n"), cmdname
);
7070 libzfs_mnttab_cache(g_zfs
, B_FALSE
);
7073 (void) fclose(mnttab_file
);
7075 if (ret
== 0 && log_history
)
7076 (void) zpool_log_history(g_zfs
, history_str
);
7081 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7082 * for the purposes of running ::findleaks.
7084 if (getenv("ZFS_ABORT") != NULL
) {
7085 (void) printf("dumping core by request\n");