4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
29 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
38 #include <libnvpair.h>
51 #include <sys/mkdev.h>
52 #include <sys/mntent.h>
53 #include <sys/mnttab.h>
54 #include <sys/mount.h>
56 #include <sys/fs/zfs.h>
57 #include <sys/types.h>
61 #include <libzfs_core.h>
63 #include <zfs_deleg.h>
67 #include <directory.h>
68 #endif /* HAVE_IDMAP */
72 #include "zfs_comutil.h"
73 #include "libzfs_impl.h"
75 libzfs_handle_t
*g_zfs
;
77 static FILE *mnttab_file
;
78 static char history_str
[HIS_MAX_RECORD_LEN
];
79 static boolean_t log_history
= B_TRUE
;
81 static int zfs_do_clone(int argc
, char **argv
);
82 static int zfs_do_create(int argc
, char **argv
);
83 static int zfs_do_destroy(int argc
, char **argv
);
84 static int zfs_do_get(int argc
, char **argv
);
85 static int zfs_do_inherit(int argc
, char **argv
);
86 static int zfs_do_list(int argc
, char **argv
);
87 static int zfs_do_mount(int argc
, char **argv
);
88 static int zfs_do_rename(int argc
, char **argv
);
89 static int zfs_do_rollback(int argc
, char **argv
);
90 static int zfs_do_set(int argc
, char **argv
);
91 static int zfs_do_upgrade(int argc
, char **argv
);
92 static int zfs_do_snapshot(int argc
, char **argv
);
93 static int zfs_do_unmount(int argc
, char **argv
);
94 static int zfs_do_share(int argc
, char **argv
);
95 static int zfs_do_unshare(int argc
, char **argv
);
96 static int zfs_do_send(int argc
, char **argv
);
97 static int zfs_do_receive(int argc
, char **argv
);
98 static int zfs_do_promote(int argc
, char **argv
);
99 static int zfs_do_userspace(int argc
, char **argv
);
100 static int zfs_do_allow(int argc
, char **argv
);
101 static int zfs_do_unallow(int argc
, char **argv
);
102 static int zfs_do_hold(int argc
, char **argv
);
103 static int zfs_do_holds(int argc
, char **argv
);
104 static int zfs_do_release(int argc
, char **argv
);
105 static int zfs_do_diff(int argc
, char **argv
);
106 static int zfs_do_bookmark(int argc
, char **argv
);
109 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
114 _umem_debug_init(void)
116 return ("default,verbose"); /* $UMEM_DEBUG setting */
120 _umem_logging_init(void)
122 return ("fail,contents"); /* $UMEM_LOGGING setting */
156 typedef struct zfs_command
{
158 int (*func
)(int argc
, char **argv
);
163 * Master command table. Each ZFS command has a name, associated function, and
164 * usage message. The usage messages need to be internationalized, so we have
165 * to have a function to return the usage message based on a command index.
167 * These commands are organized according to how they are displayed in the usage
168 * message. An empty command (one with a NULL name) indicates an empty line in
169 * the generic usage message.
171 static zfs_command_t command_table
[] = {
172 { "create", zfs_do_create
, HELP_CREATE
},
173 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
175 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
176 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
177 { "clone", zfs_do_clone
, HELP_CLONE
},
178 { "promote", zfs_do_promote
, HELP_PROMOTE
},
179 { "rename", zfs_do_rename
, HELP_RENAME
},
180 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
182 { "list", zfs_do_list
, HELP_LIST
},
184 { "set", zfs_do_set
, HELP_SET
},
185 { "get", zfs_do_get
, HELP_GET
},
186 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
187 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
188 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
189 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
191 { "mount", zfs_do_mount
, HELP_MOUNT
},
192 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
193 { "share", zfs_do_share
, HELP_SHARE
},
194 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
196 { "send", zfs_do_send
, HELP_SEND
},
197 { "receive", zfs_do_receive
, HELP_RECEIVE
},
199 { "allow", zfs_do_allow
, HELP_ALLOW
},
201 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
203 { "hold", zfs_do_hold
, HELP_HOLD
},
204 { "holds", zfs_do_holds
, HELP_HOLDS
},
205 { "release", zfs_do_release
, HELP_RELEASE
},
206 { "diff", zfs_do_diff
, HELP_DIFF
},
209 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
211 zfs_command_t
*current_command
;
214 get_usage(zfs_help_t idx
)
218 return (gettext("\tclone [-p] [-o property=value] ... "
219 "<snapshot> <filesystem|volume>\n"));
221 return (gettext("\tcreate [-p] [-o property=value] ... "
223 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
224 "-V <size> <volume>\n"));
226 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
227 "\tdestroy [-dnpRrv] "
228 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
229 "\tdestroy <filesystem|volume>#<bookmark>\n"));
231 return (gettext("\tget [-rHp] [-d max] "
232 "[-o \"all\" | field[,...]]\n"
233 "\t [-t type[,...]] [-s source[,...]]\n"
234 "\t <\"all\" | property[,...]> "
235 "[filesystem|volume|snapshot|bookmark] ...\n"));
237 return (gettext("\tinherit [-rS] <property> "
238 "<filesystem|volume|snapshot> ...\n"));
240 return (gettext("\tupgrade [-v]\n"
241 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
243 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
244 "[-s property]...\n\t [-S property]... [-t type[,...]] "
245 "[filesystem|volume|snapshot] ...\n"));
247 return (gettext("\tmount\n"
248 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
250 return (gettext("\tpromote <clone-filesystem>\n"));
252 return (gettext("\treceive [-vnsFu] <filesystem|volume|"
254 "\treceive [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
256 "\treceive -A <filesystem|volume>\n"));
258 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
259 "<filesystem|volume|snapshot>\n"
260 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
261 "\trename -r <snapshot> <snapshot>\n"));
263 return (gettext("\trollback [-rRf] <snapshot>\n"));
265 return (gettext("\tsend [-DnPpRvLec] [-[iI] snapshot] "
267 "\tsend [-Le] [-i snapshot|bookmark] "
268 "<filesystem|volume|snapshot>\n"
269 "\tsend [-nvPe] -t <receive_resume_token>\n"));
271 return (gettext("\tset <property=value> ... "
272 "<filesystem|volume|snapshot> ...\n"));
274 return (gettext("\tshare <-a [nfs|smb] | filesystem>\n"));
276 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
277 "<filesystem|volume>@<snap> ...\n"));
279 return (gettext("\tunmount [-f] "
280 "<-a | filesystem|mountpoint>\n"));
282 return (gettext("\tunshare "
283 "<-a [nfs|smb] | filesystem|mountpoint>\n"));
285 return (gettext("\tallow <filesystem|volume>\n"
287 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
288 "\t <filesystem|volume>\n"
289 "\tallow [-ld] -e <perm|@setname>[,...] "
290 "<filesystem|volume>\n"
291 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
292 "\tallow -s @setname <perm|@setname>[,...] "
293 "<filesystem|volume>\n"));
295 return (gettext("\tunallow [-rldug] "
296 "<\"everyone\"|user|group>[,...]\n"
297 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
298 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
299 "<filesystem|volume>\n"
300 "\tunallow [-r] -c [<perm|@setname>[,...]] "
301 "<filesystem|volume>\n"
302 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
303 "<filesystem|volume>\n"));
305 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
307 "\t [-S field] ... [-t type[,...]] "
308 "<filesystem|snapshot>\n"));
309 case HELP_GROUPSPACE
:
310 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
312 "\t [-S field] ... [-t type[,...]] "
313 "<filesystem|snapshot>\n"));
315 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
317 return (gettext("\tholds [-r] <snapshot> ...\n"));
319 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
321 return (gettext("\tdiff [-FHt] <snapshot> "
322 "[snapshot|filesystem]\n"));
324 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
334 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
339 * Utility function to guarantee malloc() success.
343 safe_malloc(size_t size
)
347 if ((data
= calloc(1, size
)) == NULL
)
354 safe_strdup(char *str
)
356 char *dupstr
= strdup(str
);
365 * Callback routine that will print out information for each of
369 usage_prop_cb(int prop
, void *cb
)
373 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
375 if (zfs_prop_readonly(prop
))
376 (void) fprintf(fp
, " NO ");
378 (void) fprintf(fp
, "YES ");
380 if (zfs_prop_inheritable(prop
))
381 (void) fprintf(fp
, " YES ");
383 (void) fprintf(fp
, " NO ");
385 if (zfs_prop_values(prop
) == NULL
)
386 (void) fprintf(fp
, "-\n");
388 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
394 * Display usage message. If we're inside a command, display only the usage for
395 * that command. Otherwise, iterate over the entire command table and display
396 * a complete usage message.
399 usage(boolean_t requested
)
402 boolean_t show_properties
= B_FALSE
;
403 FILE *fp
= requested
? stdout
: stderr
;
405 if (current_command
== NULL
) {
407 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
409 gettext("where 'command' is one of the following:\n\n"));
411 for (i
= 0; i
< NCOMMAND
; i
++) {
412 if (command_table
[i
].name
== NULL
)
413 (void) fprintf(fp
, "\n");
415 (void) fprintf(fp
, "%s",
416 get_usage(command_table
[i
].usage
));
419 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
420 "pool/[dataset/]*dataset[@name]\n"));
422 (void) fprintf(fp
, gettext("usage:\n"));
423 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
426 if (current_command
!= NULL
&&
427 (strcmp(current_command
->name
, "set") == 0 ||
428 strcmp(current_command
->name
, "get") == 0 ||
429 strcmp(current_command
->name
, "inherit") == 0 ||
430 strcmp(current_command
->name
, "list") == 0))
431 show_properties
= B_TRUE
;
433 if (show_properties
) {
435 gettext("\nThe following properties are supported:\n"));
437 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
438 "PROPERTY", "EDIT", "INHERIT", "VALUES");
440 /* Iterate over all properties */
441 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
444 (void) fprintf(fp
, "\t%-15s ", "userused@...");
445 (void) fprintf(fp
, " NO NO <size>\n");
446 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
447 (void) fprintf(fp
, " NO NO <size>\n");
448 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
449 (void) fprintf(fp
, "YES NO <size> | none\n");
450 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
451 (void) fprintf(fp
, "YES NO <size> | none\n");
452 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
453 (void) fprintf(fp
, " NO NO <size>\n");
455 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
456 "with standard units such as K, M, G, etc.\n"));
457 (void) fprintf(fp
, gettext("\nUser-defined properties can "
458 "be specified by using a name containing a colon (:).\n"));
459 (void) fprintf(fp
, gettext("\nThe {user|group}{used|quota}@ "
460 "properties must be appended with\n"
461 "a user or group specifier of one of these forms:\n"
462 " POSIX name (eg: \"matt\")\n"
463 " POSIX id (eg: \"126829\")\n"
464 " SMB name@domain (eg: \"matt@sun\")\n"
465 " SMB SID (eg: \"S-1-234-567-89\")\n"));
468 gettext("\nFor the property list, run: %s\n"),
471 gettext("\nFor the delegated permission list, run: %s\n"),
472 "zfs allow|unallow");
476 * See comments at end of main().
478 if (getenv("ZFS_ABORT") != NULL
) {
479 (void) printf("dumping core by request\n");
483 exit(requested
? 0 : 2);
487 * Take a property=value argument string and add it to the given nvlist.
488 * Modifies the argument inplace.
491 parseprop(nvlist_t
*props
, char *propname
)
493 char *propval
, *strval
;
495 if ((propval
= strchr(propname
, '=')) == NULL
) {
496 (void) fprintf(stderr
, gettext("missing "
497 "'=' for property=value argument\n"));
502 if (nvlist_lookup_string(props
, propname
, &strval
) == 0) {
503 (void) fprintf(stderr
, gettext("property '%s' "
504 "specified multiple times\n"), propname
);
507 if (nvlist_add_string(props
, propname
, propval
) != 0)
513 parse_depth(char *opt
, int *flags
)
518 depth
= (int)strtol(opt
, &tmp
, 0);
520 (void) fprintf(stderr
,
521 gettext("%s is not an integer\n"), optarg
);
525 (void) fprintf(stderr
,
526 gettext("Depth can not be negative.\n"));
529 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
533 #define PROGRESS_DELAY 2 /* seconds */
535 static char *pt_reverse
= "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
536 static time_t pt_begin
;
537 static char *pt_header
= NULL
;
538 static boolean_t pt_shown
;
541 start_progress_timer(void)
543 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
548 set_progress_header(char *header
)
550 assert(pt_header
== NULL
);
551 pt_header
= safe_strdup(header
);
553 (void) printf("%s: ", header
);
554 (void) fflush(stdout
);
559 update_progress(char *update
)
561 if (!pt_shown
&& time(NULL
) > pt_begin
) {
562 int len
= strlen(update
);
564 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
566 (void) fflush(stdout
);
568 } else if (pt_shown
) {
569 int len
= strlen(update
);
571 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
572 (void) fflush(stdout
);
577 finish_progress(char *done
)
580 (void) printf("%s\n", done
);
581 (void) fflush(stdout
);
588 zfs_mount_and_share(libzfs_handle_t
*hdl
, const char *dataset
, zfs_type_t type
)
590 zfs_handle_t
*zhp
= NULL
;
593 zhp
= zfs_open(hdl
, dataset
, type
);
598 * Volumes may neither be mounted or shared. Potentially in the
599 * future filesystems detected on these volumes could be mounted.
601 if (zfs_get_type(zhp
) == ZFS_TYPE_VOLUME
) {
607 * Mount and/or share the new filesystem as appropriate. We provide a
608 * verbose error message to let the user know that their filesystem was
609 * in fact created, even if we failed to mount or share it.
611 * If the user doesn't want the dataset automatically mounted, then
612 * skip the mount/share step
614 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
, B_FALSE
) &&
615 zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
) {
616 if (geteuid() != 0) {
617 (void) fprintf(stderr
, gettext("filesystem "
618 "successfully created, but it may only be "
619 "mounted by root\n"));
621 } else if (zfs_mount(zhp
, NULL
, 0) != 0) {
622 (void) fprintf(stderr
, gettext("filesystem "
623 "successfully created, but not mounted\n"));
625 } else if (zfs_share(zhp
) != 0) {
626 (void) fprintf(stderr
, gettext("filesystem "
627 "successfully created, but not shared\n"));
638 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
640 * Given an existing dataset, create a writable copy whose initial contents
641 * are the same as the source. The newly created dataset maintains a
642 * dependency on the original; the original cannot be destroyed so long as
645 * The '-p' flag creates all the non-existing ancestors of the target first.
648 zfs_do_clone(int argc
, char **argv
)
650 zfs_handle_t
*zhp
= NULL
;
651 boolean_t parents
= B_FALSE
;
656 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
660 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
663 if (parseprop(props
, optarg
) != 0) {
672 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
681 /* check number of arguments */
683 (void) fprintf(stderr
, gettext("missing source dataset "
688 (void) fprintf(stderr
, gettext("missing target dataset "
693 (void) fprintf(stderr
, gettext("too many arguments\n"));
697 /* open the source dataset */
698 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
) {
703 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
706 * Now create the ancestors of the target dataset. If the
707 * target already exists and '-p' option was used we should not
710 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
716 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
724 ret
= zfs_clone(zhp
, argv
[1], props
);
726 /* create the mountpoint if necessary */
729 (void) zpool_log_history(g_zfs
, history_str
);
730 log_history
= B_FALSE
;
733 ret
= zfs_mount_and_share(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
750 * zfs create [-p] [-o prop=value] ... fs
751 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
753 * Create a new dataset. This command can be used to create filesystems
754 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
755 * For volumes, the user must specify a size to be used.
757 * The '-s' flag applies only to volumes, and indicates that we should not try
758 * to set the reservation for this volume. By default we set a reservation
759 * equal to the size for any volume. For pools with SPA_VERSION >=
760 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
762 * The '-p' flag creates all the non-existing ancestors of the target first.
765 zfs_do_create(int argc
, char **argv
)
767 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
768 uint64_t volsize
= 0;
770 boolean_t noreserve
= B_FALSE
;
771 boolean_t bflag
= B_FALSE
;
772 boolean_t parents
= B_FALSE
;
777 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
781 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
784 type
= ZFS_TYPE_VOLUME
;
785 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
786 (void) fprintf(stderr
, gettext("bad volume "
787 "size '%s': %s\n"), optarg
,
788 libzfs_error_description(g_zfs
));
792 if (nvlist_add_uint64(props
,
793 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
802 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
803 (void) fprintf(stderr
, gettext("bad volume "
804 "block size '%s': %s\n"), optarg
,
805 libzfs_error_description(g_zfs
));
809 if (nvlist_add_uint64(props
,
810 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
815 if (parseprop(props
, optarg
) != 0)
822 (void) fprintf(stderr
, gettext("missing size "
826 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
832 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
833 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
834 "used when creating a volume\n"));
841 /* check number of arguments */
843 (void) fprintf(stderr
, gettext("missing %s argument\n"),
844 zfs_type_to_name(type
));
848 (void) fprintf(stderr
, gettext("too many arguments\n"));
852 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
853 zpool_handle_t
*zpool_handle
;
854 nvlist_t
*real_props
= NULL
;
855 uint64_t spa_version
;
857 zfs_prop_t resv_prop
;
861 if ((p
= strchr(argv
[0], '/')) != NULL
)
863 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
866 if (zpool_handle
== NULL
)
868 spa_version
= zpool_get_prop_int(zpool_handle
,
869 ZPOOL_PROP_VERSION
, NULL
);
870 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
871 resv_prop
= ZFS_PROP_REFRESERVATION
;
873 resv_prop
= ZFS_PROP_RESERVATION
;
875 (void) snprintf(msg
, sizeof (msg
),
876 gettext("cannot create '%s'"), argv
[0]);
877 if (props
&& (real_props
= zfs_valid_proplist(g_zfs
, type
,
878 props
, 0, NULL
, zpool_handle
, msg
)) == NULL
) {
879 zpool_close(zpool_handle
);
882 zpool_close(zpool_handle
);
884 volsize
= zvol_volsize_to_reservation(volsize
, real_props
);
885 nvlist_free(real_props
);
887 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
889 if (nvlist_add_uint64(props
,
890 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
897 if (parents
&& zfs_name_valid(argv
[0], type
)) {
899 * Now create the ancestors of target dataset. If the target
900 * already exists and '-p' option was used we should not
903 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
907 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
912 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
916 (void) zpool_log_history(g_zfs
, history_str
);
917 log_history
= B_FALSE
;
920 ret
= zfs_mount_and_share(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
931 * zfs destroy [-rRf] <fs, vol>
932 * zfs destroy [-rRd] <snap>
934 * -r Recursively destroy all children
935 * -R Recursively destroy all dependents, including clones
936 * -f Force unmounting of any dependents
937 * -d If we can't destroy now, mark for deferred destruction
939 * Destroys the given dataset. By default, it will unmount any filesystems,
940 * and refuse to destroy a dataset that has any dependents. A dependent can
941 * either be a child, or a clone of a child.
943 typedef struct destroy_cbdata
{
946 boolean_t cb_recurse
;
948 boolean_t cb_doclones
;
949 zfs_handle_t
*cb_target
;
950 boolean_t cb_defer_destroy
;
951 boolean_t cb_verbose
;
952 boolean_t cb_parsable
;
955 nvlist_t
*cb_batchedsnaps
;
957 /* first snap in contiguous run */
959 /* previous snap in contiguous run */
967 * Check for any dependents based on the '-r' or '-R' flags.
970 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
972 destroy_cbdata_t
*cbp
= data
;
973 const char *tname
= zfs_get_name(cbp
->cb_target
);
974 const char *name
= zfs_get_name(zhp
);
976 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
977 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
979 * This is a direct descendant, not a clone somewhere else in
986 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
987 "%s has children\n"),
988 zfs_get_name(cbp
->cb_target
),
989 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
990 (void) fprintf(stderr
, gettext("use '-r' to destroy "
991 "the following datasets:\n"));
992 cbp
->cb_first
= B_FALSE
;
993 cbp
->cb_error
= B_TRUE
;
996 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
999 * This is a clone. We only want to report this if the '-r'
1000 * wasn't specified, or the target is a snapshot.
1002 if (!cbp
->cb_recurse
&&
1003 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
1006 if (cbp
->cb_first
) {
1007 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1008 "%s has dependent clones\n"),
1009 zfs_get_name(cbp
->cb_target
),
1010 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1011 (void) fprintf(stderr
, gettext("use '-R' to destroy "
1012 "the following datasets:\n"));
1013 cbp
->cb_first
= B_FALSE
;
1014 cbp
->cb_error
= B_TRUE
;
1015 cbp
->cb_dryrun
= B_TRUE
;
1018 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1027 destroy_callback(zfs_handle_t
*zhp
, void *data
)
1029 destroy_cbdata_t
*cb
= data
;
1030 const char *name
= zfs_get_name(zhp
);
1032 if (cb
->cb_verbose
) {
1033 if (cb
->cb_parsable
) {
1034 (void) printf("destroy\t%s\n", name
);
1035 } else if (cb
->cb_dryrun
) {
1036 (void) printf(gettext("would destroy %s\n"),
1039 (void) printf(gettext("will destroy %s\n"),
1045 * Ignore pools (which we've already flagged as an error before getting
1048 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1049 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1053 if (cb
->cb_dryrun
) {
1059 * We batch up all contiguous snapshots (even of different
1060 * filesystems) and destroy them with one ioctl. We can't
1061 * simply do all snap deletions and then all fs deletions,
1062 * because we must delete a clone before its origin.
1064 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1065 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1067 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1068 cb
->cb_batchedsnaps
, B_FALSE
);
1069 fnvlist_free(cb
->cb_batchedsnaps
);
1070 cb
->cb_batchedsnaps
= fnvlist_alloc();
1073 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1074 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1085 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1087 destroy_cbdata_t
*cb
= arg
;
1088 const char *name
= zfs_get_name(zhp
);
1091 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1092 if (cb
->cb_firstsnap
== NULL
)
1093 cb
->cb_firstsnap
= strdup(name
);
1094 if (cb
->cb_prevsnap
!= NULL
)
1095 free(cb
->cb_prevsnap
);
1096 /* this snap continues the current range */
1097 cb
->cb_prevsnap
= strdup(name
);
1098 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1100 if (cb
->cb_verbose
) {
1101 if (cb
->cb_parsable
) {
1102 (void) printf("destroy\t%s\n", name
);
1103 } else if (cb
->cb_dryrun
) {
1104 (void) printf(gettext("would destroy %s\n"),
1107 (void) printf(gettext("will destroy %s\n"),
1111 } else if (cb
->cb_firstsnap
!= NULL
) {
1112 /* end of this range */
1114 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1115 cb
->cb_prevsnap
, &used
);
1116 cb
->cb_snapused
+= used
;
1117 free(cb
->cb_firstsnap
);
1118 cb
->cb_firstsnap
= NULL
;
1119 free(cb
->cb_prevsnap
);
1120 cb
->cb_prevsnap
= NULL
;
1127 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1130 assert(cb
->cb_firstsnap
== NULL
);
1131 assert(cb
->cb_prevsnap
== NULL
);
1132 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1133 if (cb
->cb_firstsnap
!= NULL
) {
1136 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1137 cb
->cb_prevsnap
, &used
);
1139 cb
->cb_snapused
+= used
;
1140 free(cb
->cb_firstsnap
);
1141 cb
->cb_firstsnap
= NULL
;
1142 free(cb
->cb_prevsnap
);
1143 cb
->cb_prevsnap
= NULL
;
1149 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1151 destroy_cbdata_t
*cb
= arg
;
1154 /* Check for clones. */
1155 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1156 cb
->cb_target
= zhp
;
1157 cb
->cb_first
= B_TRUE
;
1158 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1159 destroy_check_dependent
, cb
);
1163 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1171 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1173 destroy_cbdata_t
*cb
= arg
;
1176 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1182 if (cb
->cb_verbose
) {
1183 err
= destroy_print_snapshots(zhp
, cb
);
1189 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1197 destroy_clones(destroy_cbdata_t
*cb
)
1200 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1202 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1203 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1206 boolean_t defer
= cb
->cb_defer_destroy
;
1210 * We can't defer destroy non-snapshots, so set it to
1211 * false while destroying the clones.
1213 cb
->cb_defer_destroy
= B_FALSE
;
1214 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1215 destroy_callback
, cb
);
1216 cb
->cb_defer_destroy
= defer
;
1226 zfs_do_destroy(int argc
, char **argv
)
1228 destroy_cbdata_t cb
= { 0 };
1232 zfs_handle_t
*zhp
= NULL
;
1234 zfs_type_t type
= ZFS_TYPE_DATASET
;
1237 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1240 cb
.cb_verbose
= B_TRUE
;
1243 cb
.cb_verbose
= B_TRUE
;
1244 cb
.cb_parsable
= B_TRUE
;
1247 cb
.cb_dryrun
= B_TRUE
;
1250 cb
.cb_defer_destroy
= B_TRUE
;
1251 type
= ZFS_TYPE_SNAPSHOT
;
1254 cb
.cb_force
= B_TRUE
;
1257 cb
.cb_recurse
= B_TRUE
;
1260 cb
.cb_recurse
= B_TRUE
;
1261 cb
.cb_doclones
= B_TRUE
;
1265 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1274 /* check number of arguments */
1276 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1280 (void) fprintf(stderr
, gettext("too many arguments\n"));
1284 at
= strchr(argv
[0], '@');
1285 pound
= strchr(argv
[0], '#');
1288 /* Build the list of snaps to destroy in cb_nvl. */
1289 cb
.cb_nvl
= fnvlist_alloc();
1292 zhp
= zfs_open(g_zfs
, argv
[0],
1293 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1295 nvlist_free(cb
.cb_nvl
);
1299 cb
.cb_snapspec
= at
+ 1;
1300 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1306 if (nvlist_empty(cb
.cb_nvl
)) {
1307 (void) fprintf(stderr
, gettext("could not find any "
1308 "snapshots to destroy; check snapshot names.\n"));
1313 if (cb
.cb_verbose
) {
1315 zfs_nicenum(cb
.cb_snapused
, buf
, sizeof (buf
));
1316 if (cb
.cb_parsable
) {
1317 (void) printf("reclaim\t%llu\n",
1318 (u_longlong_t
)cb
.cb_snapused
);
1319 } else if (cb
.cb_dryrun
) {
1320 (void) printf(gettext("would reclaim %s\n"),
1323 (void) printf(gettext("will reclaim %s\n"),
1328 if (!cb
.cb_dryrun
) {
1329 if (cb
.cb_doclones
) {
1330 cb
.cb_batchedsnaps
= fnvlist_alloc();
1331 err
= destroy_clones(&cb
);
1333 err
= zfs_destroy_snaps_nvl(g_zfs
,
1334 cb
.cb_batchedsnaps
, B_FALSE
);
1342 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1343 cb
.cb_defer_destroy
);
1349 } else if (pound
!= NULL
) {
1354 (void) fprintf(stderr
,
1355 "dryrun is not supported with bookmark\n");
1359 if (cb
.cb_defer_destroy
) {
1360 (void) fprintf(stderr
,
1361 "defer destroy is not supported with bookmark\n");
1365 if (cb
.cb_recurse
) {
1366 (void) fprintf(stderr
,
1367 "recursive is not supported with bookmark\n");
1371 if (!zfs_bookmark_exists(argv
[0])) {
1372 (void) fprintf(stderr
, gettext("bookmark '%s' "
1373 "does not exist.\n"), argv
[0]);
1377 nvl
= fnvlist_alloc();
1378 fnvlist_add_boolean(nvl
, argv
[0]);
1380 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1382 (void) zfs_standard_error(g_zfs
, err
,
1383 "cannot destroy bookmark");
1390 /* Open the given dataset */
1391 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1397 * Perform an explicit check for pools before going any further.
1399 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1400 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1401 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1402 "operation does not apply to pools\n"),
1404 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1405 "%s' to destroy all datasets in the pool\n"),
1407 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1408 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1414 * Check for any dependents and/or clones.
1416 cb
.cb_first
= B_TRUE
;
1417 if (!cb
.cb_doclones
&&
1418 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1429 cb
.cb_batchedsnaps
= fnvlist_alloc();
1430 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1437 * Do the real thing. The callback will close the
1438 * handle regardless of whether it succeeds or not.
1440 err
= destroy_callback(zhp
, &cb
);
1443 err
= zfs_destroy_snaps_nvl(g_zfs
,
1444 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1451 fnvlist_free(cb
.cb_batchedsnaps
);
1452 fnvlist_free(cb
.cb_nvl
);
1459 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1462 zfs_get_column_t col
;
1464 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1465 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1466 if (col
== GET_COL_RECVD
)
1472 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1473 * < all | property[,property]... > < fs | snap | vol > ...
1475 * -r recurse over any child datasets
1476 * -H scripted mode. Headers are stripped, and fields are separated
1477 * by tabs instead of spaces.
1478 * -o Set of fields to display. One of "name,property,value,
1479 * received,source". Default is "name,property,value,source".
1480 * "all" is an alias for all five.
1481 * -s Set of sources to allow. One of
1482 * "local,default,inherited,received,temporary,none". Default is
1484 * -p Display values in parsable (literal) format.
1486 * Prints properties for the given datasets. The user can control which
1487 * columns to display as well as which property types to allow.
1491 * Invoked to display the properties for a single dataset.
1494 get_callback(zfs_handle_t
*zhp
, void *data
)
1496 char buf
[ZFS_MAXPROPLEN
];
1497 char rbuf
[ZFS_MAXPROPLEN
];
1498 zprop_source_t sourcetype
;
1499 char source
[ZFS_MAX_DATASET_NAME_LEN
];
1500 zprop_get_cbdata_t
*cbp
= data
;
1501 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1502 zprop_list_t
*pl
= cbp
->cb_proplist
;
1506 boolean_t received
= is_recvd_column(cbp
);
1508 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1509 char *recvdval
= NULL
;
1511 * Skip the special fake placeholder. This will also skip over
1512 * the name property when 'all' is specified.
1514 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1515 pl
== cbp
->cb_proplist
)
1518 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1519 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1520 sizeof (buf
), &sourcetype
, source
,
1522 cbp
->cb_literal
) != 0) {
1525 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1526 ZFS_TYPE_DATASET
, B_FALSE
)) {
1527 (void) fprintf(stderr
,
1528 gettext("No such property '%s'\n"),
1529 zfs_prop_to_name(pl
->pl_prop
));
1532 sourcetype
= ZPROP_SRC_NONE
;
1533 (void) strlcpy(buf
, "-", sizeof (buf
));
1536 if (received
&& (zfs_prop_get_recvd(zhp
,
1537 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1538 cbp
->cb_literal
) == 0))
1541 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1542 zfs_prop_to_name(pl
->pl_prop
),
1543 buf
, sourcetype
, source
, recvdval
);
1544 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1545 sourcetype
= ZPROP_SRC_LOCAL
;
1547 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1548 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1549 sourcetype
= ZPROP_SRC_NONE
;
1550 (void) strlcpy(buf
, "-", sizeof (buf
));
1553 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1554 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1555 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1556 sourcetype
= ZPROP_SRC_LOCAL
;
1558 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1559 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1560 sourcetype
= ZPROP_SRC_NONE
;
1561 (void) strlcpy(buf
, "-", sizeof (buf
));
1564 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1565 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1567 if (nvlist_lookup_nvlist(user_props
,
1568 pl
->pl_user_prop
, &propval
) != 0) {
1571 sourcetype
= ZPROP_SRC_NONE
;
1574 verify(nvlist_lookup_string(propval
,
1575 ZPROP_VALUE
, &strval
) == 0);
1576 verify(nvlist_lookup_string(propval
,
1577 ZPROP_SOURCE
, &sourceval
) == 0);
1579 if (strcmp(sourceval
,
1580 zfs_get_name(zhp
)) == 0) {
1581 sourcetype
= ZPROP_SRC_LOCAL
;
1582 } else if (strcmp(sourceval
,
1583 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1584 sourcetype
= ZPROP_SRC_RECEIVED
;
1586 sourcetype
= ZPROP_SRC_INHERITED
;
1587 (void) strlcpy(source
,
1588 sourceval
, sizeof (source
));
1592 if (received
&& (zfs_prop_get_recvd(zhp
,
1593 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1594 cbp
->cb_literal
) == 0))
1597 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1598 pl
->pl_user_prop
, strval
, sourcetype
,
1607 zfs_do_get(int argc
, char **argv
)
1609 zprop_get_cbdata_t cb
= { 0 };
1610 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1611 int types
= ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
;
1612 char *value
, *fields
;
1615 zprop_list_t fake_name
= { 0 };
1618 * Set up default columns and sources.
1620 cb
.cb_sources
= ZPROP_SRC_ALL
;
1621 cb
.cb_columns
[0] = GET_COL_NAME
;
1622 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1623 cb
.cb_columns
[2] = GET_COL_VALUE
;
1624 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1625 cb
.cb_type
= ZFS_TYPE_DATASET
;
1628 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1631 cb
.cb_literal
= B_TRUE
;
1634 limit
= parse_depth(optarg
, &flags
);
1637 flags
|= ZFS_ITER_RECURSE
;
1640 cb
.cb_scripted
= B_TRUE
;
1643 (void) fprintf(stderr
, gettext("missing argument for "
1644 "'%c' option\n"), optopt
);
1649 * Process the set of columns to display. We zero out
1650 * the structure to give us a blank slate.
1652 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1654 while (*optarg
!= '\0') {
1655 static char *col_subopts
[] =
1656 { "name", "property", "value", "received",
1657 "source", "all", NULL
};
1659 if (i
== ZFS_GET_NCOLS
) {
1660 (void) fprintf(stderr
, gettext("too "
1661 "many fields given to -o "
1666 switch (getsubopt(&optarg
, col_subopts
,
1669 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1672 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1675 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1678 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1679 flags
|= ZFS_ITER_RECVD_PROPS
;
1682 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1686 (void) fprintf(stderr
,
1687 gettext("\"all\" conflicts "
1688 "with specific fields "
1689 "given to -o option\n"));
1692 cb
.cb_columns
[0] = GET_COL_NAME
;
1693 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1694 cb
.cb_columns
[2] = GET_COL_VALUE
;
1695 cb
.cb_columns
[3] = GET_COL_RECVD
;
1696 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1697 flags
|= ZFS_ITER_RECVD_PROPS
;
1701 (void) fprintf(stderr
,
1702 gettext("invalid column name "
1711 while (*optarg
!= '\0') {
1712 static char *source_subopts
[] = {
1713 "local", "default", "inherited",
1714 "received", "temporary", "none",
1717 switch (getsubopt(&optarg
, source_subopts
,
1720 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1723 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1726 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1729 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1732 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1735 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1738 (void) fprintf(stderr
,
1739 gettext("invalid source "
1748 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1749 while (*optarg
!= '\0') {
1750 static char *type_subopts
[] = { "filesystem",
1751 "volume", "snapshot", "bookmark",
1754 switch (getsubopt(&optarg
, type_subopts
,
1757 types
|= ZFS_TYPE_FILESYSTEM
;
1760 types
|= ZFS_TYPE_VOLUME
;
1763 types
|= ZFS_TYPE_SNAPSHOT
;
1766 types
|= ZFS_TYPE_BOOKMARK
;
1769 types
= ZFS_TYPE_DATASET
|
1774 (void) fprintf(stderr
,
1775 gettext("invalid type '%s'\n"),
1783 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1793 (void) fprintf(stderr
, gettext("missing property "
1800 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1808 * As part of zfs_expand_proplist(), we keep track of the maximum column
1809 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1810 * need to know the maximum name length. However, the user likely did
1811 * not specify 'name' as one of the properties to fetch, so we need to
1812 * make sure we always include at least this property for
1813 * print_get_headers() to work properly.
1815 if (cb
.cb_proplist
!= NULL
) {
1816 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1817 fake_name
.pl_width
= strlen(gettext("NAME"));
1818 fake_name
.pl_next
= cb
.cb_proplist
;
1819 cb
.cb_proplist
= &fake_name
;
1822 cb
.cb_first
= B_TRUE
;
1824 /* run for each object */
1825 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1826 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1828 if (cb
.cb_proplist
== &fake_name
)
1829 zprop_free_list(fake_name
.pl_next
);
1831 zprop_free_list(cb
.cb_proplist
);
1837 * inherit [-rS] <property> <fs|vol> ...
1839 * -r Recurse over all children
1840 * -S Revert to received value, if any
1842 * For each dataset specified on the command line, inherit the given property
1843 * from its parent. Inheriting a property at the pool level will cause it to
1844 * use the default value. The '-r' flag will recurse over all children, and is
1845 * useful for setting a property on a hierarchy-wide basis, regardless of any
1846 * local modifications for each dataset.
1849 typedef struct inherit_cbdata
{
1850 const char *cb_propname
;
1851 boolean_t cb_received
;
1855 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1857 inherit_cbdata_t
*cb
= data
;
1858 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1861 * If we're doing it recursively, then ignore properties that
1862 * are not valid for this type of dataset.
1864 if (prop
!= ZPROP_INVAL
&&
1865 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
), B_FALSE
))
1868 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1872 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1874 inherit_cbdata_t
*cb
= data
;
1876 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1880 zfs_do_inherit(int argc
, char **argv
)
1884 inherit_cbdata_t cb
= { 0 };
1888 boolean_t received
= B_FALSE
;
1891 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1894 flags
|= ZFS_ITER_RECURSE
;
1901 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1910 /* check number of arguments */
1912 (void) fprintf(stderr
, gettext("missing property argument\n"));
1916 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1924 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1925 if (zfs_prop_readonly(prop
)) {
1926 (void) fprintf(stderr
, gettext(
1927 "%s property is read-only\n"),
1931 if (!zfs_prop_inheritable(prop
) && !received
) {
1932 (void) fprintf(stderr
, gettext("'%s' property cannot "
1933 "be inherited\n"), propname
);
1934 if (prop
== ZFS_PROP_QUOTA
||
1935 prop
== ZFS_PROP_RESERVATION
||
1936 prop
== ZFS_PROP_REFQUOTA
||
1937 prop
== ZFS_PROP_REFRESERVATION
) {
1938 (void) fprintf(stderr
, gettext("use 'zfs set "
1939 "%s=none' to clear\n"), propname
);
1940 (void) fprintf(stderr
, gettext("use 'zfs "
1941 "inherit -S %s' to revert to received "
1942 "value\n"), propname
);
1946 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1947 prop
== ZFS_PROP_VERSION
)) {
1948 (void) fprintf(stderr
, gettext("'%s' property cannot "
1949 "be reverted to a received value\n"), propname
);
1952 } else if (!zfs_prop_user(propname
)) {
1953 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1958 cb
.cb_propname
= propname
;
1959 cb
.cb_received
= received
;
1961 if (flags
& ZFS_ITER_RECURSE
) {
1962 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1963 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1965 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1966 NULL
, NULL
, 0, inherit_cb
, &cb
);
1972 typedef struct upgrade_cbdata
{
1973 uint64_t cb_numupgraded
;
1974 uint64_t cb_numsamegraded
;
1975 uint64_t cb_numfailed
;
1976 uint64_t cb_version
;
1978 boolean_t cb_foundone
;
1979 char cb_lastfs
[ZFS_MAX_DATASET_NAME_LEN
];
1983 same_pool(zfs_handle_t
*zhp
, const char *name
)
1985 int len1
= strcspn(name
, "/@");
1986 const char *zhname
= zfs_get_name(zhp
);
1987 int len2
= strcspn(zhname
, "/@");
1991 return (strncmp(name
, zhname
, len1
) == 0);
1995 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
1997 upgrade_cbdata_t
*cb
= data
;
1998 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2000 /* list if it's old/new */
2001 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
2002 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
2005 str
= gettext("The following filesystems are "
2006 "formatted using a newer software version and\n"
2007 "cannot be accessed on the current system.\n\n");
2009 str
= gettext("The following filesystems are "
2010 "out of date, and can be upgraded. After being\n"
2011 "upgraded, these filesystems (and any 'zfs send' "
2012 "streams generated from\n"
2013 "subsequent snapshots) will no longer be "
2014 "accessible by older software versions.\n\n");
2017 if (!cb
->cb_foundone
) {
2019 (void) printf(gettext("VER FILESYSTEM\n"));
2020 (void) printf(gettext("--- ------------\n"));
2021 cb
->cb_foundone
= B_TRUE
;
2024 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
2031 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2033 upgrade_cbdata_t
*cb
= data
;
2034 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2035 int needed_spa_version
;
2038 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2041 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2043 if (needed_spa_version
< 0)
2046 if (spa_version
< needed_spa_version
) {
2048 (void) printf(gettext("%s: can not be "
2049 "upgraded; the pool version needs to first "
2050 "be upgraded\nto version %d\n\n"),
2051 zfs_get_name(zhp
), needed_spa_version
);
2057 if (version
< cb
->cb_version
) {
2059 (void) snprintf(verstr
, sizeof (verstr
),
2060 "%llu", (u_longlong_t
)cb
->cb_version
);
2061 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2063 * If they did "zfs upgrade -a", then we could
2064 * be doing ioctls to different pools. We need
2065 * to log this history once to each pool, and bypass
2066 * the normal history logging that happens in main().
2068 (void) zpool_log_history(g_zfs
, history_str
);
2069 log_history
= B_FALSE
;
2071 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2072 cb
->cb_numupgraded
++;
2075 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2076 } else if (version
> cb
->cb_version
) {
2077 /* can't downgrade */
2078 (void) printf(gettext("%s: can not be downgraded; "
2079 "it is already at version %u\n"),
2080 zfs_get_name(zhp
), version
);
2083 cb
->cb_numsamegraded
++;
2091 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2094 zfs_do_upgrade(int argc
, char **argv
)
2096 boolean_t all
= B_FALSE
;
2097 boolean_t showversions
= B_FALSE
;
2099 upgrade_cbdata_t cb
= { 0 };
2101 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2104 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2107 flags
|= ZFS_ITER_RECURSE
;
2110 showversions
= B_TRUE
;
2113 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2114 optarg
, &cb
.cb_version
) != 0) {
2115 (void) fprintf(stderr
,
2116 gettext("invalid version %s\n"), optarg
);
2125 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2134 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2136 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2137 cb
.cb_version
|| argc
))
2139 if ((all
|| argc
) && (showversions
))
2145 /* Show info on available versions. */
2146 (void) printf(gettext("The following filesystem versions are "
2148 (void) printf(gettext("VER DESCRIPTION\n"));
2149 (void) printf("--- -----------------------------------------"
2150 "---------------\n");
2151 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2152 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2153 (void) printf(gettext(" 3 Case insensitive and filesystem "
2154 "user identifier (FUID)\n"));
2155 (void) printf(gettext(" 4 userquota, groupquota "
2157 (void) printf(gettext(" 5 System attributes\n"));
2158 (void) printf(gettext("\nFor more information on a particular "
2159 "version, including supported releases,\n"));
2160 (void) printf("see the ZFS Administration Guide.\n\n");
2162 } else if (argc
|| all
) {
2163 /* Upgrade filesystems */
2164 if (cb
.cb_version
== 0)
2165 cb
.cb_version
= ZPL_VERSION
;
2166 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2167 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2168 (void) printf(gettext("%llu filesystems upgraded\n"),
2169 (u_longlong_t
)cb
.cb_numupgraded
);
2170 if (cb
.cb_numsamegraded
) {
2171 (void) printf(gettext("%llu filesystems already at "
2173 (u_longlong_t
)cb
.cb_numsamegraded
);
2175 if (cb
.cb_numfailed
!= 0)
2178 /* List old-version filesystems */
2180 (void) printf(gettext("This system is currently running "
2181 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2183 flags
|= ZFS_ITER_RECURSE
;
2184 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2185 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2187 found
= cb
.cb_foundone
;
2188 cb
.cb_foundone
= B_FALSE
;
2189 cb
.cb_newer
= B_TRUE
;
2191 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2192 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2194 if (!cb
.cb_foundone
&& !found
) {
2195 (void) printf(gettext("All filesystems are "
2196 "formatted with the current version.\n"));
2204 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2205 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2206 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2207 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2209 * -H Scripted mode; elide headers and separate columns by tabs.
2210 * -i Translate SID to POSIX ID.
2211 * -n Print numeric ID instead of user/group name.
2212 * -o Control which fields to display.
2213 * -p Use exact (parsable) numeric output.
2214 * -s Specify sort columns, descending order.
2215 * -S Specify sort columns, ascending order.
2216 * -t Control which object types to display.
2218 * Displays space consumed by, and quotas on, each user in the specified
2219 * filesystem or snapshot.
2222 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2223 enum us_field_types
{
2231 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA",
2232 "OBJUSED", "OBJQUOTA" };
2233 static char *us_field_names
[] = { "type", "name", "used", "quota",
2234 "objused", "objquota" };
2235 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2237 #define USTYPE_PSX_GRP (1 << 0)
2238 #define USTYPE_PSX_USR (1 << 1)
2239 #define USTYPE_SMB_GRP (1 << 2)
2240 #define USTYPE_SMB_USR (1 << 3)
2241 #define USTYPE_ALL \
2242 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2244 static int us_type_bits
[] = {
2251 static char *us_type_names
[] = { "posixgroup", "posixuser", "smbgroup",
2254 typedef struct us_node
{
2256 uu_avl_node_t usn_avlnode
;
2257 uu_list_node_t usn_listnode
;
2260 typedef struct us_cbdata
{
2262 uu_avl_pool_t
*cb_avl_pool
;
2264 boolean_t cb_numname
;
2265 boolean_t cb_nicenum
;
2266 boolean_t cb_sid2posix
;
2267 zfs_userquota_prop_t cb_prop
;
2268 zfs_sort_column_t
*cb_sortcol
;
2269 size_t cb_width
[USFIELD_LAST
];
2272 static boolean_t us_populated
= B_FALSE
;
2275 zfs_sort_column_t
*si_sortcol
;
2276 boolean_t si_numname
;
2280 us_field_index(char *field
)
2284 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2285 if (strcmp(field
, us_field_names
[i
]) == 0)
2293 us_compare(const void *larg
, const void *rarg
, void *unused
)
2295 const us_node_t
*l
= larg
;
2296 const us_node_t
*r
= rarg
;
2297 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2298 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2299 boolean_t numname
= si
->si_numname
;
2300 nvlist_t
*lnvl
= l
->usn_nvl
;
2301 nvlist_t
*rnvl
= r
->usn_nvl
;
2305 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2312 zfs_prop_t prop
= sortcol
->sc_prop
;
2313 const char *propname
= NULL
;
2314 boolean_t reverse
= sortcol
->sc_reverse
;
2319 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2320 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2322 rc
= (rv32
< lv32
) ? 1 : -1;
2328 (void) nvlist_lookup_uint64(lnvl
, propname
,
2330 (void) nvlist_lookup_uint64(rnvl
, propname
,
2333 rc
= (rv64
< lv64
) ? 1 : -1;
2335 if ((nvlist_lookup_string(lnvl
, propname
,
2336 &lvstr
) == ENOENT
) ||
2337 (nvlist_lookup_string(rnvl
, propname
,
2338 &rvstr
) == ENOENT
)) {
2341 rc
= strcmp(lvstr
, rvstr
);
2345 case ZFS_PROP_QUOTA
:
2348 if (prop
== ZFS_PROP_USED
)
2352 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2353 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2355 rc
= (rv64
< lv64
) ? 1 : -1;
2364 return (reverse
? 1 : -1);
2366 return (reverse
? -1 : 1);
2371 * If entries still seem to be the same, check if they are of the same
2372 * type (smbentity is added only if we are doing SID to POSIX ID
2373 * translation where we can have duplicate type/name combinations).
2375 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2376 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2378 return (lvb
< rvb
? -1 : 1);
2384 zfs_prop_is_user(unsigned p
)
2386 return (p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
||
2387 p
== ZFS_PROP_USEROBJUSED
|| p
== ZFS_PROP_USEROBJQUOTA
);
2391 zfs_prop_is_group(unsigned p
)
2393 return (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
||
2394 p
== ZFS_PROP_GROUPOBJUSED
|| p
== ZFS_PROP_GROUPOBJQUOTA
);
2397 static inline const char *
2398 us_type2str(unsigned field_type
)
2400 switch (field_type
) {
2401 case USTYPE_PSX_USR
:
2402 return ("POSIX User");
2403 case USTYPE_PSX_GRP
:
2404 return ("POSIX Group");
2405 case USTYPE_SMB_USR
:
2406 return ("SMB User");
2407 case USTYPE_SMB_GRP
:
2408 return ("SMB Group");
2410 return ("Undefined");
2415 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2417 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2418 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2423 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2424 uu_avl_t
*avl
= cb
->cb_avl
;
2428 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2430 const char *typestr
;
2434 int typeidx
, nameidx
, sizeidx
;
2435 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2436 boolean_t smbentity
= B_FALSE
;
2438 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2440 node
= safe_malloc(sizeof (us_node_t
));
2441 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2442 node
->usn_nvl
= props
;
2444 if (domain
!= NULL
&& domain
[0] != '\0') {
2447 char sid
[MAXNAMELEN
+ 32];
2451 directory_error_t e
;
2455 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2457 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2458 type
= USTYPE_SMB_GRP
;
2459 err
= sid_to_id(sid
, B_FALSE
, &id
);
2461 type
= USTYPE_SMB_USR
;
2462 err
= sid_to_id(sid
, B_TRUE
, &id
);
2467 if (!cb
->cb_sid2posix
) {
2468 e
= directory_name_from_sid(NULL
, sid
, &name
,
2471 directory_error_free(e
);
2481 #endif /* HAVE_IDMAP */
2484 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2486 if (zfs_prop_is_group(prop
)) {
2487 type
= USTYPE_PSX_GRP
;
2488 if (!cb
->cb_numname
) {
2491 if ((g
= getgrgid(rid
)) != NULL
)
2495 type
= USTYPE_PSX_USR
;
2496 if (!cb
->cb_numname
) {
2499 if ((p
= getpwuid(rid
)) != NULL
)
2506 * Make sure that the type/name combination is unique when doing
2507 * SID to POSIX ID translation (hence changing the type from SMB to
2510 if (cb
->cb_sid2posix
&&
2511 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2514 /* Calculate/update width of TYPE field */
2515 typestr
= us_type2str(type
);
2516 typelen
= strlen(gettext(typestr
));
2517 typeidx
= us_field_index("type");
2518 if (typelen
> cb
->cb_width
[typeidx
])
2519 cb
->cb_width
[typeidx
] = typelen
;
2520 if (nvlist_add_uint32(props
, "type", type
) != 0)
2523 /* Calculate/update width of NAME field */
2524 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2525 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2527 namelen
= snprintf(NULL
, 0, "%u", rid
);
2529 if (nvlist_add_string(props
, "name", name
) != 0)
2531 namelen
= strlen(name
);
2533 nameidx
= us_field_index("name");
2534 if (nameidx
>= 0 && namelen
> cb
->cb_width
[nameidx
])
2535 cb
->cb_width
[nameidx
] = namelen
;
2538 * Check if this type/name combination is in the list and update it;
2539 * otherwise add new node to the list.
2541 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2542 uu_avl_insert(avl
, node
, idx
);
2547 props
= node
->usn_nvl
;
2550 /* Calculate/update width of USED/QUOTA fields */
2552 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2554 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu",
2555 (u_longlong_t
)space
);
2556 sizelen
= strlen(sizebuf
);
2557 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
) {
2559 if (!nvlist_exists(props
, "quota"))
2560 (void) nvlist_add_uint64(props
, "quota", 0);
2561 } else if (prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2563 if (!nvlist_exists(props
, "used"))
2564 (void) nvlist_add_uint64(props
, "used", 0);
2565 } else if (prop
== ZFS_PROP_USEROBJUSED
||
2566 prop
== ZFS_PROP_GROUPOBJUSED
) {
2567 propname
= "objused";
2568 if (!nvlist_exists(props
, "objquota"))
2569 (void) nvlist_add_uint64(props
, "objquota", 0);
2570 } else if (prop
== ZFS_PROP_USEROBJQUOTA
||
2571 prop
== ZFS_PROP_GROUPOBJQUOTA
) {
2572 propname
= "objquota";
2573 if (!nvlist_exists(props
, "objused"))
2574 (void) nvlist_add_uint64(props
, "objused", 0);
2578 sizeidx
= us_field_index(propname
);
2579 if (sizeidx
>= 0 && sizelen
> cb
->cb_width
[sizeidx
])
2580 cb
->cb_width
[sizeidx
] = sizelen
;
2582 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2589 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2590 size_t *width
, us_node_t
*node
)
2592 nvlist_t
*nvl
= node
->usn_nvl
;
2593 char valstr
[MAXNAMELEN
];
2594 boolean_t first
= B_TRUE
;
2600 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2601 if (!(ustype
& types
))
2604 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2605 nvpair_t
*nvp
= NULL
;
2611 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2612 if (strcmp(nvpair_name(nvp
),
2613 us_field_names
[field
]) == 0)
2617 type
= nvp
== NULL
? DATA_TYPE_UNKNOWN
: nvpair_type(nvp
);
2619 case DATA_TYPE_UINT32
:
2620 (void) nvpair_value_uint32(nvp
, &val32
);
2622 case DATA_TYPE_UINT64
:
2623 (void) nvpair_value_uint64(nvp
, &val64
);
2625 case DATA_TYPE_STRING
:
2626 (void) nvpair_value_string(nvp
, &strval
);
2628 case DATA_TYPE_UNKNOWN
:
2631 (void) fprintf(stderr
, "invalid data type\n");
2636 if (type
== DATA_TYPE_UINT32
)
2637 strval
= (char *)us_type2str(val32
);
2640 if (type
== DATA_TYPE_UINT64
) {
2641 (void) sprintf(valstr
, "%llu",
2642 (u_longlong_t
)val64
);
2648 case USFIELD_OBJUSED
:
2649 case USFIELD_OBJQUOTA
:
2650 if (type
== DATA_TYPE_UINT64
) {
2652 (void) sprintf(valstr
, "%llu",
2653 (u_longlong_t
)val64
);
2655 zfs_nicenum(val64
, valstr
,
2658 if ((field
== USFIELD_QUOTA
||
2659 field
== USFIELD_OBJQUOTA
) &&
2660 strcmp(valstr
, "0") == 0)
2670 (void) printf("\t");
2675 (void) printf("%s", strval
);
2676 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2677 (void) printf("%-*s", (int)width
[field
], strval
);
2679 (void) printf("%*s", (int)width
[field
], strval
);
2685 (void) printf("\n");
2689 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2690 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2698 boolean_t first
= B_TRUE
;
2700 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2701 col
= gettext(us_field_hdr
[field
]);
2702 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2703 (void) printf(first
? "%-*s" : " %-*s",
2704 (int)width
[field
], col
);
2706 (void) printf(first
? "%*s" : " %*s",
2707 (int)width
[field
], col
);
2712 (void) printf("\n");
2715 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2716 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2718 nvlist_free(node
->usn_nvl
);
2723 zfs_do_userspace(int argc
, char **argv
)
2726 zfs_userquota_prop_t p
;
2727 uu_avl_pool_t
*avl_pool
;
2729 uu_avl_walk_t
*walk
;
2731 char deffields
[] = "type,name,used,quota,objused,objquota";
2732 char *ofield
= NULL
;
2733 char *tfield
= NULL
;
2737 boolean_t scripted
= B_FALSE
;
2738 boolean_t prtnum
= B_FALSE
;
2739 boolean_t parsable
= B_FALSE
;
2740 boolean_t sid2posix
= B_FALSE
;
2743 zfs_sort_column_t
*sortcol
= NULL
;
2744 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2748 uu_list_pool_t
*listpool
;
2750 uu_avl_index_t idx
= 0;
2751 uu_list_index_t idx2
= 0;
2756 if (strcmp(argv
[0], "groupspace") == 0)
2757 /* Toggle default group types */
2758 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2760 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2776 if (zfs_add_sort_column(&sortcol
, optarg
,
2777 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
2778 (void) fprintf(stderr
,
2779 gettext("invalid field '%s'\n"), optarg
);
2790 (void) fprintf(stderr
, gettext("missing argument for "
2791 "'%c' option\n"), optopt
);
2795 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2805 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2809 (void) fprintf(stderr
, gettext("too many arguments\n"));
2813 /* Use default output fields if not specified using -o */
2817 if ((delim
= strchr(ofield
, ',')) != NULL
)
2819 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
2820 (void) fprintf(stderr
, gettext("invalid type '%s' "
2821 "for -o option\n"), ofield
);
2826 } while (delim
!= NULL
);
2827 fields
[cfield
] = USFIELD_LAST
;
2829 /* Override output types (-t option) */
2830 if (tfield
!= NULL
) {
2834 boolean_t found
= B_FALSE
;
2836 if ((delim
= strchr(tfield
, ',')) != NULL
)
2838 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
2840 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
2842 types
|= us_type_bits
[i
];
2847 (void) fprintf(stderr
, gettext("invalid type "
2848 "'%s' for -t option\n"), tfield
);
2853 } while (delim
!= NULL
);
2856 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
2859 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
2860 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
2862 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
2865 /* Always add default sorting columns */
2866 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
2867 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
2869 cb
.cb_sortcol
= sortcol
;
2870 cb
.cb_numname
= prtnum
;
2871 cb
.cb_nicenum
= !parsable
;
2872 cb
.cb_avl_pool
= avl_pool
;
2873 cb
.cb_avl
= avl_tree
;
2874 cb
.cb_sid2posix
= sid2posix
;
2876 for (i
= 0; i
< USFIELD_LAST
; i
++)
2877 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
2879 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
2880 if ((zfs_prop_is_user(p
) &&
2881 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
2882 (zfs_prop_is_group(p
) &&
2883 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))))
2887 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
2892 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
2895 us_populated
= B_TRUE
;
2897 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
2898 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
2899 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
2900 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
2902 while (node
!= NULL
) {
2904 node
= uu_avl_next(avl_tree
, node
);
2905 uu_avl_remove(avl_tree
, rmnode
);
2906 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
2907 uu_list_insert(list
, rmnode
, idx2
);
2910 for (node
= uu_list_first(list
); node
!= NULL
;
2911 node
= uu_list_next(list
, node
)) {
2912 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
2914 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
2915 uu_avl_insert(avl_tree
, node
, idx
);
2918 uu_list_destroy(list
);
2919 uu_list_pool_destroy(listpool
);
2921 /* Print and free node nvlist memory */
2922 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
2925 zfs_free_sort_columns(sortcol
);
2927 /* Clean up the AVL tree */
2928 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
2931 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
2932 uu_avl_remove(cb
.cb_avl
, node
);
2936 uu_avl_walk_end(walk
);
2937 uu_avl_destroy(avl_tree
);
2938 uu_avl_pool_destroy(avl_pool
);
2944 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
2945 * [-t type[,...]] [filesystem|volume|snapshot] ...
2947 * -H Scripted mode; elide headers and separate columns by tabs
2948 * -p Display values in parsable (literal) format.
2949 * -r Recurse over all children
2950 * -d Limit recursion by depth.
2951 * -o Control which fields to display.
2952 * -s Specify sort columns, descending order.
2953 * -S Specify sort columns, ascending order.
2954 * -t Control which object types to display.
2956 * When given no arguments, list all filesystems in the system.
2957 * Otherwise, list the specified datasets, optionally recursing down them if
2958 * '-r' is specified.
2960 typedef struct list_cbdata
{
2962 boolean_t cb_literal
;
2963 boolean_t cb_scripted
;
2964 zprop_list_t
*cb_proplist
;
2968 * Given a list of columns to display, output appropriate headers for each one.
2971 print_header(list_cbdata_t
*cb
)
2973 zprop_list_t
*pl
= cb
->cb_proplist
;
2974 char headerbuf
[ZFS_MAXPROPLEN
];
2977 boolean_t first
= B_TRUE
;
2978 boolean_t right_justify
;
2980 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2987 right_justify
= B_FALSE
;
2988 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2989 header
= zfs_prop_column_name(pl
->pl_prop
);
2990 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2992 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2993 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2994 headerbuf
[i
] = '\0';
2998 if (pl
->pl_next
== NULL
&& !right_justify
)
2999 (void) printf("%s", header
);
3000 else if (right_justify
)
3001 (void) printf("%*s", (int)pl
->pl_width
, header
);
3003 (void) printf("%-*s", (int)pl
->pl_width
, header
);
3006 (void) printf("\n");
3010 * Given a dataset and a list of fields, print out all the properties according
3011 * to the described layout.
3014 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
3016 zprop_list_t
*pl
= cb
->cb_proplist
;
3017 boolean_t first
= B_TRUE
;
3018 char property
[ZFS_MAXPROPLEN
];
3019 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
3022 boolean_t right_justify
;
3024 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3026 if (cb
->cb_scripted
)
3027 (void) printf("\t");
3034 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
3035 (void) strlcpy(property
, zfs_get_name(zhp
),
3038 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3039 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
3040 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
3041 sizeof (property
), NULL
, NULL
, 0,
3042 cb
->cb_literal
) != 0)
3046 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3047 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
3048 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
3049 property
, sizeof (property
), cb
->cb_literal
) != 0)
3053 right_justify
= B_TRUE
;
3054 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
3055 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
3056 property
, sizeof (property
), cb
->cb_literal
) != 0)
3060 right_justify
= B_TRUE
;
3062 if (nvlist_lookup_nvlist(userprops
,
3063 pl
->pl_user_prop
, &propval
) != 0)
3066 verify(nvlist_lookup_string(propval
,
3067 ZPROP_VALUE
, &propstr
) == 0);
3068 right_justify
= B_FALSE
;
3072 * If this is being called in scripted mode, or if this is the
3073 * last column and it is left-justified, don't include a width
3076 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3077 (void) printf("%s", propstr
);
3078 else if (right_justify
)
3079 (void) printf("%*s", (int)pl
->pl_width
, propstr
);
3081 (void) printf("%-*s", (int)pl
->pl_width
, propstr
);
3084 (void) printf("\n");
3088 * Generic callback function to list a dataset or snapshot.
3091 list_callback(zfs_handle_t
*zhp
, void *data
)
3093 list_cbdata_t
*cbp
= data
;
3095 if (cbp
->cb_first
) {
3096 if (!cbp
->cb_scripted
)
3098 cbp
->cb_first
= B_FALSE
;
3101 print_dataset(zhp
, cbp
);
3107 zfs_do_list(int argc
, char **argv
)
3110 static char default_fields
[] =
3111 "name,used,available,referenced,mountpoint";
3112 int types
= ZFS_TYPE_DATASET
;
3113 boolean_t types_specified
= B_FALSE
;
3114 char *fields
= NULL
;
3115 list_cbdata_t cb
= { 0 };
3119 zfs_sort_column_t
*sortcol
= NULL
;
3120 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3123 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3129 cb
.cb_literal
= B_TRUE
;
3130 flags
|= ZFS_ITER_LITERAL_PROPS
;
3133 limit
= parse_depth(optarg
, &flags
);
3136 flags
|= ZFS_ITER_RECURSE
;
3139 cb
.cb_scripted
= B_TRUE
;
3142 if (zfs_add_sort_column(&sortcol
, optarg
,
3144 (void) fprintf(stderr
,
3145 gettext("invalid property '%s'\n"), optarg
);
3150 if (zfs_add_sort_column(&sortcol
, optarg
,
3152 (void) fprintf(stderr
,
3153 gettext("invalid property '%s'\n"), optarg
);
3159 types_specified
= B_TRUE
;
3160 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3161 while (*optarg
!= '\0') {
3162 static char *type_subopts
[] = { "filesystem",
3163 "volume", "snapshot", "snap", "bookmark",
3166 switch (getsubopt(&optarg
, type_subopts
,
3169 types
|= ZFS_TYPE_FILESYSTEM
;
3172 types
|= ZFS_TYPE_VOLUME
;
3176 types
|= ZFS_TYPE_SNAPSHOT
;
3179 types
|= ZFS_TYPE_BOOKMARK
;
3182 types
= ZFS_TYPE_DATASET
|
3186 (void) fprintf(stderr
,
3187 gettext("invalid type '%s'\n"),
3194 (void) fprintf(stderr
, gettext("missing argument for "
3195 "'%c' option\n"), optopt
);
3199 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3209 fields
= default_fields
;
3212 * If we are only going to list snapshot names and sort by name,
3213 * then we can use faster version.
3215 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3216 flags
|= ZFS_ITER_SIMPLE
;
3219 * If "-o space" and no types were specified, don't display snapshots.
3221 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3222 types
&= ~ZFS_TYPE_SNAPSHOT
;
3225 * If the user specifies '-o all', the zprop_get_list() doesn't
3226 * normally include the name of the dataset. For 'zfs list', we always
3227 * want this property to be first.
3229 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3233 cb
.cb_first
= B_TRUE
;
3235 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3236 limit
, list_callback
, &cb
);
3238 zprop_free_list(cb
.cb_proplist
);
3239 zfs_free_sort_columns(sortcol
);
3241 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3242 (void) fprintf(stderr
, gettext("no datasets available\n"));
3248 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3249 * zfs rename [-f] -p <fs | vol> <fs | vol>
3250 * zfs rename -r <snap> <snap>
3252 * Renames the given dataset to another of the same type.
3254 * The '-p' flag creates all the non-existing ancestors of the target first.
3258 zfs_do_rename(int argc
, char **argv
)
3263 boolean_t recurse
= B_FALSE
;
3264 boolean_t parents
= B_FALSE
;
3265 boolean_t force_unmount
= B_FALSE
;
3268 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3277 force_unmount
= B_TRUE
;
3281 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3290 /* check number of arguments */
3292 (void) fprintf(stderr
, gettext("missing source dataset "
3297 (void) fprintf(stderr
, gettext("missing target dataset "
3302 (void) fprintf(stderr
, gettext("too many arguments\n"));
3306 if (recurse
&& parents
) {
3307 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3312 if (recurse
&& strchr(argv
[0], '@') == 0) {
3313 (void) fprintf(stderr
, gettext("source dataset for recursive "
3314 "rename must be a snapshot\n"));
3318 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3319 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3322 /* If we were asked and the name looks good, try to create ancestors. */
3323 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3324 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3329 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3338 * Promotes the given clone fs to be the parent
3342 zfs_do_promote(int argc
, char **argv
)
3348 if (argc
> 1 && argv
[1][0] == '-') {
3349 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3354 /* check number of arguments */
3356 (void) fprintf(stderr
, gettext("missing clone filesystem"
3361 (void) fprintf(stderr
, gettext("too many arguments\n"));
3365 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3369 ret
= (zfs_promote(zhp
) != 0);
3377 * zfs rollback [-rRf] <snapshot>
3379 * -r Delete any intervening snapshots before doing rollback
3380 * -R Delete any snapshots and their clones
3381 * -f ignored for backwards compatibility
3383 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3384 * since then and making it the active dataset. If more recent snapshots exist,
3385 * the command will complain unless the '-r' flag is given.
3387 typedef struct rollback_cbdata
{
3393 boolean_t cb_recurse
;
3394 } rollback_cbdata_t
;
3397 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3399 rollback_cbdata_t
*cbp
= data
;
3401 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3402 (void) fprintf(stderr
, gettext("cannot rollback to "
3403 "'%s': clones of previous snapshots exist\n"),
3405 (void) fprintf(stderr
, gettext("use '-R' to "
3406 "force deletion of the following clones and "
3412 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3420 * Report any snapshots more recent than the one specified. Used when '-r' is
3421 * not specified. We reuse this same callback for the snapshot dependents - if
3422 * 'cb_dependent' is set, then this is a dependent and we should report it
3423 * without checking the transaction group.
3426 rollback_check(zfs_handle_t
*zhp
, void *data
)
3428 rollback_cbdata_t
*cbp
= data
;
3430 if (cbp
->cb_doclones
) {
3435 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3436 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3437 (void) fprintf(stderr
, gettext("cannot "
3438 "rollback to '%s': more recent snapshots "
3439 "or bookmarks exist\n"),
3441 (void) fprintf(stderr
, gettext("use '-r' to "
3442 "force deletion of the following "
3443 "snapshots and bookmarks:\n"));
3448 if (cbp
->cb_recurse
) {
3449 if (zfs_iter_dependents(zhp
, B_TRUE
,
3450 rollback_check_dependent
, cbp
) != 0) {
3455 (void) fprintf(stderr
, "%s\n",
3464 zfs_do_rollback(int argc
, char **argv
)
3468 boolean_t force
= B_FALSE
;
3469 rollback_cbdata_t cb
= { 0 };
3470 zfs_handle_t
*zhp
, *snap
;
3471 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3475 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3488 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3497 /* check number of arguments */
3499 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3503 (void) fprintf(stderr
, gettext("too many arguments\n"));
3507 /* open the snapshot */
3508 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3511 /* open the parent dataset */
3512 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3513 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3515 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3521 * Check for more recent snapshots and/or clones based on the presence
3524 cb
.cb_target
= argv
[0];
3525 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3526 cb
.cb_first
= B_TRUE
;
3528 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, rollback_check
, &cb
)) != 0)
3530 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
3533 if ((ret
= cb
.cb_error
) != 0)
3537 * Rollback parent to the given snapshot.
3539 ret
= zfs_rollback(zhp
, snap
, force
);
3552 * zfs set property=value ... { fs | snap | vol } ...
3554 * Sets the given properties for all datasets specified on the command line.
3558 set_callback(zfs_handle_t
*zhp
, void *data
)
3560 nvlist_t
*props
= data
;
3562 if (zfs_prop_set_list(zhp
, props
) != 0) {
3563 switch (libzfs_errno(g_zfs
)) {
3564 case EZFS_MOUNTFAILED
:
3565 (void) fprintf(stderr
, gettext("property may be set "
3566 "but unable to remount filesystem\n"));
3568 case EZFS_SHARENFSFAILED
:
3569 (void) fprintf(stderr
, gettext("property may be set "
3570 "but unable to reshare filesystem\n"));
3579 zfs_do_set(int argc
, char **argv
)
3581 nvlist_t
*props
= NULL
;
3582 int ds_start
= -1; /* argv idx of first dataset arg */
3586 /* check for options */
3587 if (argc
> 1 && argv
[1][0] == '-') {
3588 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3593 /* check number of arguments */
3595 (void) fprintf(stderr
, gettext("missing arguments\n"));
3599 if (strchr(argv
[1], '=') == NULL
) {
3600 (void) fprintf(stderr
, gettext("missing property=value "
3603 (void) fprintf(stderr
, gettext("missing dataset "
3609 /* validate argument order: prop=val args followed by dataset args */
3610 for (i
= 1; i
< argc
; i
++) {
3611 if (strchr(argv
[i
], '=') != NULL
) {
3613 /* out-of-order prop=val argument */
3614 (void) fprintf(stderr
, gettext("invalid "
3615 "argument order\n"));
3618 } else if (ds_start
< 0) {
3623 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
3627 /* Populate a list of property settings */
3628 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3630 for (i
= 1; i
< ds_start
; i
++) {
3631 if ((ret
= parseprop(props
, argv
[i
])) != 0)
3635 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
3636 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, props
);
3643 typedef struct snap_cbdata
{
3645 boolean_t sd_recursive
;
3646 const char *sd_snapname
;
3650 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3652 snap_cbdata_t
*sd
= arg
;
3657 if (sd
->sd_recursive
&&
3658 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
3663 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3666 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3669 if (sd
->sd_recursive
)
3670 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3676 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3678 * Creates a snapshot with the given name. While functionally equivalent to
3679 * 'zfs create', it is a separate command to differentiate intent.
3682 zfs_do_snapshot(int argc
, char **argv
)
3687 snap_cbdata_t sd
= { 0 };
3688 boolean_t multiple_snaps
= B_FALSE
;
3690 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3692 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3696 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3699 if (parseprop(props
, optarg
) != 0) {
3700 nvlist_free(sd
.sd_nvl
);
3706 sd
.sd_recursive
= B_TRUE
;
3707 multiple_snaps
= B_TRUE
;
3710 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3719 /* check number of arguments */
3721 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3726 multiple_snaps
= B_TRUE
;
3727 for (; argc
> 0; argc
--, argv
++) {
3731 atp
= strchr(argv
[0], '@');
3735 sd
.sd_snapname
= atp
+ 1;
3736 zhp
= zfs_open(g_zfs
, argv
[0],
3737 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3740 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3744 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3745 nvlist_free(sd
.sd_nvl
);
3747 if (ret
!= 0 && multiple_snaps
)
3748 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3752 nvlist_free(sd
.sd_nvl
);
3759 * Send a backup stream to stdout.
3762 zfs_do_send(int argc
, char **argv
)
3764 char *fromname
= NULL
;
3765 char *toname
= NULL
;
3766 char *resume_token
= NULL
;
3769 sendflags_t flags
= { 0 };
3771 nvlist_t
*dbgnv
= NULL
;
3772 boolean_t extraverbose
= B_FALSE
;
3775 while ((c
= getopt(argc
, argv
, ":i:I:RDpvnPLet:c")) != -1) {
3786 flags
.doall
= B_TRUE
;
3789 flags
.replicate
= B_TRUE
;
3792 flags
.props
= B_TRUE
;
3795 flags
.parsable
= B_TRUE
;
3796 flags
.verbose
= B_TRUE
;
3800 extraverbose
= B_TRUE
;
3801 flags
.verbose
= B_TRUE
;
3802 flags
.progress
= B_TRUE
;
3805 flags
.dedup
= B_TRUE
;
3808 flags
.dryrun
= B_TRUE
;
3811 flags
.largeblock
= B_TRUE
;
3814 flags
.embed_data
= B_TRUE
;
3817 resume_token
= optarg
;
3820 flags
.compress
= B_TRUE
;
3824 * If a parameter was not passed, optopt contains the
3825 * value that would normally lead us into the
3826 * appropriate case statement. If it's > 256, then this
3827 * must be a longopt and we should look at argv to get
3828 * the string. Otherwise it's just the character, so we
3829 * should use it directly.
3831 if (optopt
<= UINT8_MAX
) {
3832 (void) fprintf(stderr
,
3833 gettext("missing argument for '%c' "
3834 "option\n"), optopt
);
3836 (void) fprintf(stderr
,
3837 gettext("missing argument for '%s' "
3838 "option\n"), argv
[optind
- 1]);
3846 * If an invalid flag was passed, optopt contains the
3847 * character if it was a short flag, or 0 if it was a
3851 (void) fprintf(stderr
,
3852 gettext("invalid option '%c'\n"), optopt
);
3854 (void) fprintf(stderr
,
3855 gettext("invalid option '%s'\n"),
3866 if (resume_token
!= NULL
) {
3867 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
3869 (void) fprintf(stderr
,
3870 gettext("invalid flags combined with -t\n"));
3874 (void) fprintf(stderr
, gettext("no additional "
3875 "arguments are permitted with -t\n"));
3880 (void) fprintf(stderr
,
3881 gettext("missing snapshot argument\n"));
3885 (void) fprintf(stderr
, gettext("too many arguments\n"));
3890 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
3891 (void) fprintf(stderr
,
3892 gettext("Error: Stream can not be written to a terminal.\n"
3893 "You must redirect standard output.\n"));
3897 if (resume_token
!= NULL
) {
3898 return (zfs_send_resume(g_zfs
, &flags
, STDOUT_FILENO
,
3903 * Special case sending a filesystem, or from a bookmark.
3905 if (strchr(argv
[0], '@') == NULL
||
3906 (fromname
&& strchr(fromname
, '#') != NULL
)) {
3907 char frombuf
[ZFS_MAX_DATASET_NAME_LEN
];
3908 enum lzc_send_flags lzc_flags
= 0;
3910 if (flags
.replicate
|| flags
.doall
|| flags
.props
||
3911 flags
.dedup
|| flags
.dryrun
|| flags
.verbose
||
3913 (void) fprintf(stderr
,
3915 "Unsupported flag with filesystem or bookmark.\n"));
3919 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
3923 if (flags
.largeblock
)
3924 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
3925 if (flags
.embed_data
)
3926 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
3928 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
3930 if (fromname
!= NULL
&&
3931 (fromname
[0] == '#' || fromname
[0] == '@')) {
3933 * Incremental source name begins with # or @.
3934 * Default to same fs as target.
3936 (void) strlcpy(frombuf
, argv
[0], sizeof (frombuf
));
3937 cp
= strchr(frombuf
, '@');
3940 (void) strlcat(frombuf
, fromname
, sizeof (frombuf
));
3943 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, lzc_flags
);
3948 cp
= strchr(argv
[0], '@');
3951 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3956 * If they specified the full path to the snapshot, chop off
3957 * everything except the short name of the snapshot, but special
3958 * case if they specify the origin.
3960 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
3961 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
3964 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
3965 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
3967 if (strcmp(origin
, fromname
) == 0) {
3969 flags
.fromorigin
= B_TRUE
;
3972 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
3973 (void) fprintf(stderr
,
3974 gettext("incremental source must be "
3975 "in same filesystem\n"));
3979 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
3980 (void) fprintf(stderr
,
3981 gettext("invalid incremental source\n"));
3987 if (flags
.replicate
&& fromname
== NULL
)
3988 flags
.doall
= B_TRUE
;
3990 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
3991 extraverbose
? &dbgnv
: NULL
);
3993 if (extraverbose
&& dbgnv
!= NULL
) {
3995 * dump_nvlist prints to stdout, but that's been
3996 * redirected to a file. Make it print to stderr
3999 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
4000 dump_nvlist(dbgnv
, 0);
4009 * Restore a backup stream from stdin.
4012 zfs_do_receive(int argc
, char **argv
)
4015 recvflags_t flags
= { 0 };
4016 boolean_t abort_resumable
= B_FALSE
;
4019 nvpair_t
*nvp
= NULL
;
4021 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
4025 while ((c
= getopt(argc
, argv
, ":o:denuvFsA")) != -1) {
4028 if (parseprop(props
, optarg
) != 0) {
4034 flags
.isprefix
= B_TRUE
;
4037 flags
.isprefix
= B_TRUE
;
4038 flags
.istail
= B_TRUE
;
4041 flags
.dryrun
= B_TRUE
;
4044 flags
.nomount
= B_TRUE
;
4047 flags
.verbose
= B_TRUE
;
4050 flags
.resumable
= B_TRUE
;
4053 flags
.force
= B_TRUE
;
4056 abort_resumable
= B_TRUE
;
4059 (void) fprintf(stderr
, gettext("missing argument for "
4060 "'%c' option\n"), optopt
);
4064 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4073 /* check number of arguments */
4075 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4079 (void) fprintf(stderr
, gettext("too many arguments\n"));
4083 while ((nvp
= nvlist_next_nvpair(props
, nvp
))) {
4084 if (strcmp(nvpair_name(nvp
), "origin") != 0) {
4085 (void) fprintf(stderr
, gettext("invalid option"));
4090 if (abort_resumable
) {
4091 if (flags
.isprefix
|| flags
.istail
|| flags
.dryrun
||
4092 flags
.resumable
|| flags
.nomount
) {
4093 (void) fprintf(stderr
, gettext("invalid option"));
4097 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
4098 (void) snprintf(namebuf
, sizeof (namebuf
),
4099 "%s/%%recv", argv
[0]);
4101 if (zfs_dataset_exists(g_zfs
, namebuf
,
4102 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) {
4103 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4104 namebuf
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4109 err
= zfs_destroy(zhp
, B_FALSE
);
4112 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4113 argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4116 if (!zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) ||
4117 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
4118 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == -1) {
4119 (void) fprintf(stderr
,
4120 gettext("'%s' does not have any "
4121 "resumable receive state to abort\n"),
4127 err
= zfs_destroy(zhp
, B_FALSE
);
4134 if (isatty(STDIN_FILENO
)) {
4135 (void) fprintf(stderr
,
4136 gettext("Error: Backup stream can not be read "
4137 "from a terminal.\n"
4138 "You must redirect standard input.\n"));
4142 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
4149 * allow/unallow stuff
4151 /* copied from zfs/sys/dsl_deleg.h */
4152 #define ZFS_DELEG_PERM_CREATE "create"
4153 #define ZFS_DELEG_PERM_DESTROY "destroy"
4154 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4155 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4156 #define ZFS_DELEG_PERM_CLONE "clone"
4157 #define ZFS_DELEG_PERM_PROMOTE "promote"
4158 #define ZFS_DELEG_PERM_RENAME "rename"
4159 #define ZFS_DELEG_PERM_MOUNT "mount"
4160 #define ZFS_DELEG_PERM_SHARE "share"
4161 #define ZFS_DELEG_PERM_SEND "send"
4162 #define ZFS_DELEG_PERM_RECEIVE "receive"
4163 #define ZFS_DELEG_PERM_ALLOW "allow"
4164 #define ZFS_DELEG_PERM_USERPROP "userprop"
4165 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4166 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4167 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4168 #define ZFS_DELEG_PERM_USERUSED "userused"
4169 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4170 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
4171 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
4172 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
4173 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
4175 #define ZFS_DELEG_PERM_HOLD "hold"
4176 #define ZFS_DELEG_PERM_RELEASE "release"
4177 #define ZFS_DELEG_PERM_DIFF "diff"
4178 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4180 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4182 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
4183 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
4184 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
4185 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
4186 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
4187 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
4188 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
4189 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
4190 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
4191 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
4192 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
4193 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
4194 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
4195 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
4196 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
4197 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
4198 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
4200 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
4201 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
4202 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
4203 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
4204 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
4205 { ZFS_DELEG_PERM_USEROBJQUOTA
, ZFS_DELEG_NOTE_USEROBJQUOTA
},
4206 { ZFS_DELEG_PERM_USEROBJUSED
, ZFS_DELEG_NOTE_USEROBJUSED
},
4207 { ZFS_DELEG_PERM_GROUPOBJQUOTA
, ZFS_DELEG_NOTE_GROUPOBJQUOTA
},
4208 { ZFS_DELEG_PERM_GROUPOBJUSED
, ZFS_DELEG_NOTE_GROUPOBJUSED
},
4209 { NULL
, ZFS_DELEG_NOTE_NONE
}
4212 /* permission structure */
4213 typedef struct deleg_perm
{
4214 zfs_deleg_who_type_t dp_who_type
;
4215 const char *dp_name
;
4217 boolean_t dp_descend
;
4221 typedef struct deleg_perm_node
{
4222 deleg_perm_t dpn_perm
;
4224 uu_avl_node_t dpn_avl_node
;
4225 } deleg_perm_node_t
;
4227 typedef struct fs_perm fs_perm_t
;
4229 /* permissions set */
4230 typedef struct who_perm
{
4231 zfs_deleg_who_type_t who_type
;
4232 const char *who_name
; /* id */
4233 char who_ug_name
[256]; /* user/group name */
4234 fs_perm_t
*who_fsperm
; /* uplink */
4236 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
4240 typedef struct who_perm_node
{
4241 who_perm_t who_perm
;
4242 uu_avl_node_t who_avl_node
;
4245 typedef struct fs_perm_set fs_perm_set_t
;
4246 /* fs permissions */
4248 const char *fsp_name
;
4250 uu_avl_t
*fsp_sc_avl
; /* sets,create */
4251 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
4253 fs_perm_set_t
*fsp_set
; /* uplink */
4257 typedef struct fs_perm_node
{
4258 fs_perm_t fspn_fsperm
;
4261 uu_list_node_t fspn_list_node
;
4264 /* top level structure */
4265 struct fs_perm_set
{
4266 uu_list_pool_t
*fsps_list_pool
;
4267 uu_list_t
*fsps_list
; /* list of fs_perms */
4269 uu_avl_pool_t
*fsps_named_set_avl_pool
;
4270 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
4271 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
4274 static inline const char *
4275 deleg_perm_type(zfs_deleg_note_t note
)
4281 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4282 case ZFS_DELEG_NOTE_GROUPUSED
:
4283 case ZFS_DELEG_NOTE_USERPROP
:
4284 case ZFS_DELEG_NOTE_USERQUOTA
:
4285 case ZFS_DELEG_NOTE_USERUSED
:
4286 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
4287 case ZFS_DELEG_NOTE_USEROBJUSED
:
4288 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
4289 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
4291 return (gettext("other"));
4293 return (gettext("subcommand"));
4298 who_type2weight(zfs_deleg_who_type_t who_type
)
4302 case ZFS_DELEG_NAMED_SET_SETS
:
4303 case ZFS_DELEG_NAMED_SET
:
4306 case ZFS_DELEG_CREATE_SETS
:
4307 case ZFS_DELEG_CREATE
:
4310 case ZFS_DELEG_USER_SETS
:
4311 case ZFS_DELEG_USER
:
4314 case ZFS_DELEG_GROUP_SETS
:
4315 case ZFS_DELEG_GROUP
:
4318 case ZFS_DELEG_EVERYONE_SETS
:
4319 case ZFS_DELEG_EVERYONE
:
4331 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4333 const who_perm_node_t
*l
= larg
;
4334 const who_perm_node_t
*r
= rarg
;
4335 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
4336 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
4337 int lweight
= who_type2weight(ltype
);
4338 int rweight
= who_type2weight(rtype
);
4339 int res
= lweight
- rweight
;
4341 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
4342 ZFS_MAX_DELEG_NAME
-1);
4354 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4356 const deleg_perm_node_t
*l
= larg
;
4357 const deleg_perm_node_t
*r
= rarg
;
4358 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
4359 ZFS_MAX_DELEG_NAME
-1);
4371 fs_perm_set_init(fs_perm_set_t
*fspset
)
4373 bzero(fspset
, sizeof (fs_perm_set_t
));
4375 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4376 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4377 NULL
, UU_DEFAULT
)) == NULL
)
4379 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4380 UU_DEFAULT
)) == NULL
)
4383 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4384 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4385 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4386 UU_DEFAULT
)) == NULL
)
4389 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4390 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4391 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4392 UU_DEFAULT
)) == NULL
)
4395 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4396 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4397 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4402 static inline void fs_perm_fini(fs_perm_t
*);
4403 static inline void who_perm_fini(who_perm_t
*);
4406 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4408 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4410 while (node
!= NULL
) {
4411 fs_perm_node_t
*next_node
=
4412 uu_list_next(fspset
->fsps_list
, node
);
4413 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4414 fs_perm_fini(fsperm
);
4415 uu_list_remove(fspset
->fsps_list
, node
);
4420 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4421 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4422 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4426 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4429 deleg_perm
->dp_who_type
= type
;
4430 deleg_perm
->dp_name
= name
;
4434 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4435 zfs_deleg_who_type_t type
, const char *name
)
4437 uu_avl_pool_t
*pool
;
4438 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4440 bzero(who_perm
, sizeof (who_perm_t
));
4442 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4443 UU_DEFAULT
)) == NULL
)
4446 who_perm
->who_type
= type
;
4447 who_perm
->who_name
= name
;
4448 who_perm
->who_fsperm
= fsperm
;
4452 who_perm_fini(who_perm_t
*who_perm
)
4454 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4456 while (node
!= NULL
) {
4457 deleg_perm_node_t
*next_node
=
4458 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4460 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4465 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4469 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4471 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4472 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4474 bzero(fsperm
, sizeof (fs_perm_t
));
4476 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4480 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4484 fsperm
->fsp_set
= fspset
;
4485 fsperm
->fsp_name
= fsname
;
4489 fs_perm_fini(fs_perm_t
*fsperm
)
4491 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4492 while (node
!= NULL
) {
4493 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4495 who_perm_t
*who_perm
= &node
->who_perm
;
4496 who_perm_fini(who_perm
);
4497 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4502 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4503 while (node
!= NULL
) {
4504 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4506 who_perm_t
*who_perm
= &node
->who_perm
;
4507 who_perm_fini(who_perm
);
4508 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4513 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4514 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4518 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4519 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4521 uu_avl_index_t idx
= 0;
4523 deleg_perm_node_t
*found_node
= NULL
;
4524 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4526 deleg_perm_init(deleg_perm
, who_type
, name
);
4528 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4530 uu_avl_insert(avl
, node
, idx
);
4533 deleg_perm
= &node
->dpn_perm
;
4538 case ZFS_DELEG_LOCAL
:
4539 deleg_perm
->dp_local
= B_TRUE
;
4541 case ZFS_DELEG_DESCENDENT
:
4542 deleg_perm
->dp_descend
= B_TRUE
;
4547 assert(B_FALSE
); /* invalid locality */
4552 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4554 nvpair_t
*nvp
= NULL
;
4555 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4556 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4557 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4559 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4560 const char *name
= nvpair_name(nvp
);
4561 data_type_t type
= nvpair_type(nvp
);
4562 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4563 deleg_perm_node_t
*node
=
4564 safe_malloc(sizeof (deleg_perm_node_t
));
4566 VERIFY(type
== DATA_TYPE_BOOLEAN
);
4568 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4569 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4576 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4578 nvpair_t
*nvp
= NULL
;
4579 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4581 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4582 nvlist_t
*nvl2
= NULL
;
4583 const char *name
= nvpair_name(nvp
);
4584 uu_avl_t
*avl
= NULL
;
4585 uu_avl_pool_t
*avl_pool
= NULL
;
4586 zfs_deleg_who_type_t perm_type
= name
[0];
4587 char perm_locality
= name
[1];
4588 const char *perm_name
= name
+ 3;
4589 boolean_t is_set
= B_TRUE
;
4590 who_perm_t
*who_perm
= NULL
;
4592 assert('$' == name
[2]);
4594 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4597 switch (perm_type
) {
4598 case ZFS_DELEG_CREATE
:
4599 case ZFS_DELEG_CREATE_SETS
:
4600 case ZFS_DELEG_NAMED_SET
:
4601 case ZFS_DELEG_NAMED_SET_SETS
:
4602 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4603 avl
= fsperm
->fsp_sc_avl
;
4605 case ZFS_DELEG_USER
:
4606 case ZFS_DELEG_USER_SETS
:
4607 case ZFS_DELEG_GROUP
:
4608 case ZFS_DELEG_GROUP_SETS
:
4609 case ZFS_DELEG_EVERYONE
:
4610 case ZFS_DELEG_EVERYONE_SETS
:
4611 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4612 avl
= fsperm
->fsp_uge_avl
;
4616 assert(!"unhandled zfs_deleg_who_type_t");
4620 who_perm_node_t
*found_node
= NULL
;
4621 who_perm_node_t
*node
= safe_malloc(
4622 sizeof (who_perm_node_t
));
4623 who_perm
= &node
->who_perm
;
4624 uu_avl_index_t idx
= 0;
4626 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4627 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4629 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4631 if (avl
== fsperm
->fsp_uge_avl
) {
4633 struct passwd
*p
= NULL
;
4634 struct group
*g
= NULL
;
4635 const char *nice_name
= NULL
;
4637 switch (perm_type
) {
4638 case ZFS_DELEG_USER_SETS
:
4639 case ZFS_DELEG_USER
:
4640 rid
= atoi(perm_name
);
4643 nice_name
= p
->pw_name
;
4645 case ZFS_DELEG_GROUP_SETS
:
4646 case ZFS_DELEG_GROUP
:
4647 rid
= atoi(perm_name
);
4650 nice_name
= g
->gr_name
;
4657 if (nice_name
!= NULL
)
4659 node
->who_perm
.who_ug_name
,
4663 uu_avl_insert(avl
, node
, idx
);
4666 who_perm
= &node
->who_perm
;
4669 VERIFY3P(who_perm
, !=, NULL
);
4670 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4677 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4679 nvpair_t
*nvp
= NULL
;
4680 uu_avl_index_t idx
= 0;
4682 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4683 nvlist_t
*nvl2
= NULL
;
4684 const char *fsname
= nvpair_name(nvp
);
4685 data_type_t type
= nvpair_type(nvp
);
4686 fs_perm_t
*fsperm
= NULL
;
4687 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4691 fsperm
= &node
->fspn_fsperm
;
4693 VERIFY(DATA_TYPE_NVLIST
== type
);
4695 uu_list_node_init(node
, &node
->fspn_list_node
,
4696 fspset
->fsps_list_pool
);
4698 idx
= uu_list_numnodes(fspset
->fsps_list
);
4699 fs_perm_init(fsperm
, fspset
, fsname
);
4701 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4704 (void) parse_fs_perm(fsperm
, nvl2
);
4706 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4712 static inline const char *
4713 deleg_perm_comment(zfs_deleg_note_t note
)
4715 const char *str
= "";
4720 case ZFS_DELEG_NOTE_ALLOW
:
4721 str
= gettext("Must also have the permission that is being"
4722 "\n\t\t\t\tallowed");
4724 case ZFS_DELEG_NOTE_CLONE
:
4725 str
= gettext("Must also have the 'create' ability and 'mount'"
4726 "\n\t\t\t\tability in the origin file system");
4728 case ZFS_DELEG_NOTE_CREATE
:
4729 str
= gettext("Must also have the 'mount' ability");
4731 case ZFS_DELEG_NOTE_DESTROY
:
4732 str
= gettext("Must also have the 'mount' ability");
4734 case ZFS_DELEG_NOTE_DIFF
:
4735 str
= gettext("Allows lookup of paths within a dataset;"
4736 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4737 "\n\t\t\t\tin order to use zfs diff");
4739 case ZFS_DELEG_NOTE_HOLD
:
4740 str
= gettext("Allows adding a user hold to a snapshot");
4742 case ZFS_DELEG_NOTE_MOUNT
:
4743 str
= gettext("Allows mount/umount of ZFS datasets");
4745 case ZFS_DELEG_NOTE_PROMOTE
:
4746 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4747 " 'promote' ability in the origin file system");
4749 case ZFS_DELEG_NOTE_RECEIVE
:
4750 str
= gettext("Must also have the 'mount' and 'create'"
4753 case ZFS_DELEG_NOTE_RELEASE
:
4754 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4755 "might destroy the snapshot");
4757 case ZFS_DELEG_NOTE_RENAME
:
4758 str
= gettext("Must also have the 'mount' and 'create'"
4759 "\n\t\t\t\tability in the new parent");
4761 case ZFS_DELEG_NOTE_ROLLBACK
:
4764 case ZFS_DELEG_NOTE_SEND
:
4767 case ZFS_DELEG_NOTE_SHARE
:
4768 str
= gettext("Allows sharing file systems over NFS or SMB"
4769 "\n\t\t\t\tprotocols");
4771 case ZFS_DELEG_NOTE_SNAPSHOT
:
4775 * case ZFS_DELEG_NOTE_VSCAN:
4776 * str = gettext("");
4780 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4781 str
= gettext("Allows accessing any groupquota@... property");
4783 case ZFS_DELEG_NOTE_GROUPUSED
:
4784 str
= gettext("Allows reading any groupused@... property");
4786 case ZFS_DELEG_NOTE_USERPROP
:
4787 str
= gettext("Allows changing any user property");
4789 case ZFS_DELEG_NOTE_USERQUOTA
:
4790 str
= gettext("Allows accessing any userquota@... property");
4792 case ZFS_DELEG_NOTE_USERUSED
:
4793 str
= gettext("Allows reading any userused@... property");
4795 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
4796 str
= gettext("Allows accessing any userobjquota@... property");
4798 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
4799 str
= gettext("Allows accessing any \n\t\t\t\t"
4800 "groupobjquota@... property");
4802 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
4803 str
= gettext("Allows reading any groupobjused@... property");
4805 case ZFS_DELEG_NOTE_USEROBJUSED
:
4806 str
= gettext("Allows reading any userobjused@... property");
4824 boolean_t recursive
; /* unallow only */
4825 boolean_t prt_usage
;
4827 boolean_t prt_perms
;
4830 const char *dataset
;
4834 prop_cmp(const void *a
, const void *b
)
4836 const char *str1
= *(const char **)a
;
4837 const char *str2
= *(const char **)b
;
4838 return (strcmp(str1
, str2
));
4842 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
4844 const char *opt_desc
[] = {
4845 "-h", gettext("show this help message and exit"),
4846 "-l", gettext("set permission locally"),
4847 "-d", gettext("set permission for descents"),
4848 "-u", gettext("set permission for user"),
4849 "-g", gettext("set permission for group"),
4850 "-e", gettext("set permission for everyone"),
4851 "-c", gettext("set create time permission"),
4852 "-s", gettext("define permission set"),
4854 "-r", gettext("remove permissions recursively"),
4856 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
4857 size_t allow_size
= unallow_size
- 2;
4858 const char *props
[ZFS_NUM_PROPS
];
4861 FILE *fp
= requested
? stdout
: stderr
;
4862 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
4863 const char *fmt
= gettext("%-16s %-14s\t%s\n");
4865 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
4867 (void) fprintf(fp
, gettext("Options:\n"));
4868 for (i
= 0; i
< (un
? unallow_size
: allow_size
); i
+= 2) {
4869 const char *opt
= opt_desc
[i
];
4870 const char *optdsc
= opt_desc
[i
+ 1];
4871 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
4874 (void) fprintf(fp
, gettext("\nThe following permissions are "
4876 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
4878 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
4879 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
4880 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
4881 const char *perm_type
= deleg_perm_type(perm_note
);
4882 const char *perm_comment
= deleg_perm_comment(perm_note
);
4883 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
4886 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
4887 zprop_desc_t
*pd
= &pdtbl
[i
];
4888 if (pd
->pd_visible
!= B_TRUE
)
4891 if (pd
->pd_attr
== PROP_READONLY
)
4894 props
[count
++] = pd
->pd_name
;
4896 props
[count
] = NULL
;
4898 qsort(props
, count
, sizeof (char *), prop_cmp
);
4900 for (i
= 0; i
< count
; i
++)
4901 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
4904 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
4906 exit(requested
? 0 : 2);
4909 static inline const char *
4910 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
4913 if (un
&& argc
== expected_argc
- 1)
4915 else if (argc
== expected_argc
)
4916 *permsp
= argv
[argc
- 2];
4918 allow_usage(un
, B_FALSE
,
4919 gettext("wrong number of parameters\n"));
4921 return (argv
[argc
- 1]);
4925 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
4927 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
4928 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
4929 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
4930 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
4933 allow_usage(un
, B_FALSE
,
4934 gettext("-u, -g, and -e are mutually exclusive\n"));
4936 if (opts
->prt_usage
) {
4937 if (argc
== 0 && all_sum
== 0)
4938 allow_usage(un
, B_TRUE
, NULL
);
4945 allow_usage(un
, B_FALSE
,
4946 gettext("invalid options combined with -s\n"));
4948 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4949 if (argv
[0][0] != '@')
4950 allow_usage(un
, B_FALSE
,
4951 gettext("invalid set name: missing '@' prefix\n"));
4952 opts
->who
= argv
[0];
4953 } else if (opts
->create
) {
4954 if (ldcsuge_sum
> 1)
4955 allow_usage(un
, B_FALSE
,
4956 gettext("invalid options combined with -c\n"));
4957 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4958 } else if (opts
->everyone
) {
4960 allow_usage(un
, B_FALSE
,
4961 gettext("invalid options combined with -e\n"));
4962 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4963 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
4965 opts
->everyone
= B_TRUE
;
4968 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4969 } else if (argc
== 1 && !un
) {
4970 opts
->prt_perms
= B_TRUE
;
4971 opts
->dataset
= argv
[argc
-1];
4973 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4974 opts
->who
= argv
[0];
4977 if (!opts
->local
&& !opts
->descend
) {
4978 opts
->local
= B_TRUE
;
4979 opts
->descend
= B_TRUE
;
4984 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
4985 const char *who
, char *perms
, nvlist_t
*top_nvl
)
4988 char ld
[2] = { '\0', '\0' };
4989 char who_buf
[MAXNAMELEN
+ 32];
4990 char base_type
= '\0';
4991 char set_type
= '\0';
4992 nvlist_t
*base_nvl
= NULL
;
4993 nvlist_t
*set_nvl
= NULL
;
4996 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4998 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
5002 case ZFS_DELEG_NAMED_SET_SETS
:
5003 case ZFS_DELEG_NAMED_SET
:
5004 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
5005 base_type
= ZFS_DELEG_NAMED_SET
;
5006 ld
[0] = ZFS_DELEG_NA
;
5008 case ZFS_DELEG_CREATE_SETS
:
5009 case ZFS_DELEG_CREATE
:
5010 set_type
= ZFS_DELEG_CREATE_SETS
;
5011 base_type
= ZFS_DELEG_CREATE
;
5012 ld
[0] = ZFS_DELEG_NA
;
5014 case ZFS_DELEG_USER_SETS
:
5015 case ZFS_DELEG_USER
:
5016 set_type
= ZFS_DELEG_USER_SETS
;
5017 base_type
= ZFS_DELEG_USER
;
5019 ld
[0] = ZFS_DELEG_LOCAL
;
5021 ld
[1] = ZFS_DELEG_DESCENDENT
;
5023 case ZFS_DELEG_GROUP_SETS
:
5024 case ZFS_DELEG_GROUP
:
5025 set_type
= ZFS_DELEG_GROUP_SETS
;
5026 base_type
= ZFS_DELEG_GROUP
;
5028 ld
[0] = ZFS_DELEG_LOCAL
;
5030 ld
[1] = ZFS_DELEG_DESCENDENT
;
5032 case ZFS_DELEG_EVERYONE_SETS
:
5033 case ZFS_DELEG_EVERYONE
:
5034 set_type
= ZFS_DELEG_EVERYONE_SETS
;
5035 base_type
= ZFS_DELEG_EVERYONE
;
5037 ld
[0] = ZFS_DELEG_LOCAL
;
5039 ld
[1] = ZFS_DELEG_DESCENDENT
;
5043 assert(set_type
!= '\0' && base_type
!= '\0');
5046 if (perms
!= NULL
) {
5048 char *end
= curr
+ strlen(perms
);
5050 while (curr
< end
) {
5051 char *delim
= strchr(curr
, ',');
5062 (void) nvlist_add_boolean(nvl
, curr
);
5068 for (i
= 0; i
< 2; i
++) {
5069 char locality
= ld
[i
];
5073 if (!nvlist_empty(base_nvl
)) {
5075 (void) snprintf(who_buf
,
5076 sizeof (who_buf
), "%c%c$%s",
5077 base_type
, locality
, who
);
5079 (void) snprintf(who_buf
,
5080 sizeof (who_buf
), "%c%c$",
5081 base_type
, locality
);
5083 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5088 if (!nvlist_empty(set_nvl
)) {
5090 (void) snprintf(who_buf
,
5091 sizeof (who_buf
), "%c%c$%s",
5092 set_type
, locality
, who
);
5094 (void) snprintf(who_buf
,
5095 sizeof (who_buf
), "%c%c$",
5096 set_type
, locality
);
5098 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5103 for (i
= 0; i
< 2; i
++) {
5104 char locality
= ld
[i
];
5109 (void) snprintf(who_buf
, sizeof (who_buf
),
5110 "%c%c$%s", base_type
, locality
, who
);
5112 (void) snprintf(who_buf
, sizeof (who_buf
),
5113 "%c%c$", base_type
, locality
);
5114 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5117 (void) snprintf(who_buf
, sizeof (who_buf
),
5118 "%c%c$%s", set_type
, locality
, who
);
5120 (void) snprintf(who_buf
, sizeof (who_buf
),
5121 "%c%c$", set_type
, locality
);
5122 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5128 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
5130 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
5134 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
5135 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
5136 } else if (opts
->create
) {
5137 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
5138 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5139 } else if (opts
->everyone
) {
5140 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
5141 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5143 char *curr
= opts
->who
;
5144 char *end
= curr
+ strlen(curr
);
5146 while (curr
< end
) {
5148 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
5150 char *delim
= strchr(curr
, ',');
5153 struct passwd
*p
= NULL
;
5154 struct group
*g
= NULL
;
5162 rid
= (uid_t
)strtol(curr
, &endch
, 0);
5164 who_type
= ZFS_DELEG_USER
;
5173 (void) snprintf(errbuf
, 256, gettext(
5174 "invalid user %s"), curr
);
5175 allow_usage(un
, B_TRUE
, errbuf
);
5177 } else if (opts
->group
) {
5178 who_type
= ZFS_DELEG_GROUP
;
5187 (void) snprintf(errbuf
, 256, gettext(
5188 "invalid group %s"), curr
);
5189 allow_usage(un
, B_TRUE
, errbuf
);
5192 if (*endch
!= '\0') {
5199 if (*endch
!= '\0') {
5207 who_type
= ZFS_DELEG_USER
;
5209 } else if (g
!= NULL
) {
5210 who_type
= ZFS_DELEG_GROUP
;
5213 (void) snprintf(errbuf
, 256, gettext(
5214 "invalid user/group %s"), curr
);
5215 allow_usage(un
, B_TRUE
, errbuf
);
5219 (void) sprintf(id
, "%u", rid
);
5222 store_allow_perm(who_type
, opts
->local
,
5223 opts
->descend
, who
, opts
->perms
, *nvlp
);
5232 print_set_creat_perms(uu_avl_t
*who_avl
)
5234 const char *sc_title
[] = {
5235 gettext("Permission sets:\n"),
5236 gettext("Create time permissions:\n"),
5239 const char **title_ptr
= sc_title
;
5240 who_perm_node_t
*who_node
= NULL
;
5241 int prev_weight
= -1;
5243 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
5244 who_node
= uu_avl_next(who_avl
, who_node
)) {
5245 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5246 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5247 const char *who_name
= who_node
->who_perm
.who_name
;
5248 int weight
= who_type2weight(who_type
);
5249 boolean_t first
= B_TRUE
;
5250 deleg_perm_node_t
*deleg_node
;
5252 if (prev_weight
!= weight
) {
5253 (void) printf("%s", *title_ptr
++);
5254 prev_weight
= weight
;
5257 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
5258 (void) printf("\t");
5260 (void) printf("\t%s ", who_name
);
5262 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
5263 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5266 deleg_node
->dpn_perm
.dp_name
);
5269 (void) printf(",%s",
5270 deleg_node
->dpn_perm
.dp_name
);
5273 (void) printf("\n");
5278 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
5281 who_perm_node_t
*who_node
= NULL
;
5282 boolean_t prt_title
= B_TRUE
;
5283 uu_avl_walk_t
*walk
;
5285 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
5288 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
5289 const char *who_name
= who_node
->who_perm
.who_name
;
5290 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
5291 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5292 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5294 deleg_perm_node_t
*deleg_node
;
5295 boolean_t prt_who
= B_TRUE
;
5297 for (deleg_node
= uu_avl_first(avl
);
5299 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5300 if (local
!= deleg_node
->dpn_perm
.dp_local
||
5301 descend
!= deleg_node
->dpn_perm
.dp_descend
)
5305 const char *who
= NULL
;
5307 prt_title
= B_FALSE
;
5308 (void) printf("%s", title
);
5312 case ZFS_DELEG_USER_SETS
:
5313 case ZFS_DELEG_USER
:
5314 who
= gettext("user");
5316 who_name
= nice_who_name
;
5318 case ZFS_DELEG_GROUP_SETS
:
5319 case ZFS_DELEG_GROUP
:
5320 who
= gettext("group");
5322 who_name
= nice_who_name
;
5324 case ZFS_DELEG_EVERYONE_SETS
:
5325 case ZFS_DELEG_EVERYONE
:
5326 who
= gettext("everyone");
5331 assert(who
!= NULL
);
5335 if (who_name
== NULL
)
5336 (void) printf("\t%s", who
);
5338 (void) printf("\t%s %s", who
, who_name
);
5341 (void) printf("%c%s", delim
,
5342 deleg_node
->dpn_perm
.dp_name
);
5347 (void) printf("\n");
5350 uu_avl_walk_end(walk
);
5354 print_fs_perms(fs_perm_set_t
*fspset
)
5356 fs_perm_node_t
*node
= NULL
;
5357 char buf
[MAXNAMELEN
+ 32];
5358 const char *dsname
= buf
;
5360 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
5361 node
= uu_list_next(fspset
->fsps_list
, node
)) {
5362 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
5363 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
5366 (void) snprintf(buf
, sizeof (buf
),
5367 gettext("---- Permissions on %s "),
5368 node
->fspn_fsperm
.fsp_name
);
5369 (void) printf("%s", dsname
);
5370 left
= 70 - strlen(buf
);
5373 (void) printf("\n");
5375 print_set_creat_perms(sc_avl
);
5376 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
5377 gettext("Local permissions:\n"));
5378 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
5379 gettext("Descendent permissions:\n"));
5380 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
5381 gettext("Local+Descendent permissions:\n"));
5385 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5387 struct deleg_perms
{
5393 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5395 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5396 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5398 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5401 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5405 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5408 nvlist_t
*perm_nvl
= NULL
;
5409 nvlist_t
*update_perm_nvl
= NULL
;
5412 struct allow_opts opts
= { 0 };
5414 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5417 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5420 opts
.local
= B_TRUE
;
5423 opts
.descend
= B_TRUE
;
5429 opts
.group
= B_TRUE
;
5432 opts
.everyone
= B_TRUE
;
5438 opts
.create
= B_TRUE
;
5441 opts
.recursive
= B_TRUE
;
5444 (void) fprintf(stderr
, gettext("missing argument for "
5445 "'%c' option\n"), optopt
);
5449 opts
.prt_usage
= B_TRUE
;
5452 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5461 /* check arguments */
5462 parse_allow_args(argc
, argv
, un
, &opts
);
5464 /* try to open the dataset */
5465 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5466 ZFS_TYPE_VOLUME
)) == NULL
) {
5467 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5472 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5475 fs_perm_set_init(&fs_perm_set
);
5476 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5477 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5482 print_fs_perms(&fs_perm_set
);
5484 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5485 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5488 if (un
&& opts
.recursive
) {
5489 struct deleg_perms data
= { un
, update_perm_nvl
};
5490 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5499 nvlist_free(perm_nvl
);
5500 nvlist_free(update_perm_nvl
);
5502 fs_perm_set_fini(&fs_perm_set
);
5510 zfs_do_allow(int argc
, char **argv
)
5512 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5516 zfs_do_unallow(int argc
, char **argv
)
5518 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5522 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5527 boolean_t recursive
= B_FALSE
;
5528 const char *opts
= holding
? "rt" : "r";
5532 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5538 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5547 /* check number of arguments */
5555 if (holding
&& tag
[0] == '.') {
5556 /* tags starting with '.' are reserved for libzfs */
5557 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5561 for (i
= 0; i
< argc
; ++i
) {
5563 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
5565 char *path
= argv
[i
];
5567 delim
= strchr(path
, '@');
5568 if (delim
== NULL
) {
5569 (void) fprintf(stderr
,
5570 gettext("'%s' is not a snapshot\n"), path
);
5574 (void) strncpy(parent
, path
, delim
- path
);
5575 parent
[delim
- path
] = '\0';
5577 zhp
= zfs_open(g_zfs
, parent
,
5578 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5584 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
5587 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5593 return (errors
!= 0);
5597 * zfs hold [-r] [-t] <tag> <snap> ...
5599 * -r Recursively hold
5601 * Apply a user-hold with the given tag to the list of snapshots.
5604 zfs_do_hold(int argc
, char **argv
)
5606 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5610 * zfs release [-r] <tag> <snap> ...
5612 * -r Recursively release
5614 * Release a user-hold with the given tag from the list of snapshots.
5617 zfs_do_release(int argc
, char **argv
)
5619 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5622 typedef struct holds_cbdata
{
5623 boolean_t cb_recursive
;
5624 const char *cb_snapname
;
5626 size_t cb_max_namelen
;
5627 size_t cb_max_taglen
;
5630 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5631 #define DATETIME_BUF_LEN (32)
5636 print_holds(boolean_t scripted
, int nwidth
, int tagwidth
, nvlist_t
*nvl
)
5639 nvpair_t
*nvp
= NULL
;
5640 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5644 for (i
= 0; i
< 3; i
++) {
5645 col
= gettext(hdr_cols
[i
]);
5647 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5650 (void) printf("%s\n", col
);
5654 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5655 char *zname
= nvpair_name(nvp
);
5657 nvpair_t
*nvp2
= NULL
;
5658 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5659 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5660 char tsbuf
[DATETIME_BUF_LEN
];
5661 char *tagname
= nvpair_name(nvp2
);
5665 char sep
= scripted
? '\t' : ' ';
5666 int sepnum
= scripted
? 1 : 2;
5668 (void) nvpair_value_uint64(nvp2
, &val
);
5670 (void) localtime_r(&time
, &t
);
5671 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5672 gettext(STRFTIME_FMT_STR
), &t
);
5674 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth
, zname
,
5675 sepnum
, sep
, tagwidth
, tagname
, sepnum
, sep
, tsbuf
);
5681 * Generic callback function to list a dataset or snapshot.
5684 holds_callback(zfs_handle_t
*zhp
, void *data
)
5686 holds_cbdata_t
*cbp
= data
;
5687 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5688 nvlist_t
*nvl
= NULL
;
5689 nvpair_t
*nvp
= NULL
;
5690 const char *zname
= zfs_get_name(zhp
);
5691 size_t znamelen
= strlen(zname
);
5693 if (cbp
->cb_recursive
) {
5694 const char *snapname
;
5695 char *delim
= strchr(zname
, '@');
5699 snapname
= delim
+ 1;
5700 if (strcmp(cbp
->cb_snapname
, snapname
))
5704 if (zfs_get_holds(zhp
, &nvl
) != 0)
5707 if (znamelen
> cbp
->cb_max_namelen
)
5708 cbp
->cb_max_namelen
= znamelen
;
5710 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5711 const char *tag
= nvpair_name(nvp
);
5712 size_t taglen
= strlen(tag
);
5713 if (taglen
> cbp
->cb_max_taglen
)
5714 cbp
->cb_max_taglen
= taglen
;
5717 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5721 * zfs holds [-r] <snap> ...
5723 * -r Recursively hold
5726 zfs_do_holds(int argc
, char **argv
)
5731 boolean_t scripted
= B_FALSE
;
5732 boolean_t recursive
= B_FALSE
;
5733 const char *opts
= "rH";
5736 int types
= ZFS_TYPE_SNAPSHOT
;
5737 holds_cbdata_t cb
= { 0 };
5744 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5753 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5760 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
5761 flags
|= ZFS_ITER_RECURSE
;
5767 /* check number of arguments */
5771 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5774 for (i
= 0; i
< argc
; ++i
) {
5775 char *snapshot
= argv
[i
];
5777 const char *snapname
;
5779 delim
= strchr(snapshot
, '@');
5780 if (delim
== NULL
) {
5781 (void) fprintf(stderr
,
5782 gettext("'%s' is not a snapshot\n"), snapshot
);
5786 snapname
= delim
+ 1;
5788 snapshot
[delim
- snapshot
] = '\0';
5790 cb
.cb_recursive
= recursive
;
5791 cb
.cb_snapname
= snapname
;
5795 * 1. collect holds data, set format options
5797 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
5798 holds_callback
, &cb
);
5804 * 2. print holds data
5806 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
5808 if (nvlist_empty(nvl
))
5809 (void) fprintf(stderr
, gettext("no datasets available\n"));
5813 return (0 != errors
);
5816 #define CHECK_SPINNER 30
5817 #define SPINNER_TIME 3 /* seconds */
5818 #define MOUNT_TIME 5 /* seconds */
5821 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
5823 static char *spin
[] = { "-", "\\", "|", "/" };
5824 static int spinval
= 0;
5825 static int spincheck
= 0;
5826 static time_t last_spin_time
= (time_t)0;
5827 get_all_cb_t
*cbp
= data
;
5828 zfs_type_t type
= zfs_get_type(zhp
);
5830 if (cbp
->cb_verbose
) {
5831 if (--spincheck
< 0) {
5832 time_t now
= time(NULL
);
5833 if (last_spin_time
+ SPINNER_TIME
< now
) {
5834 update_progress(spin
[spinval
++ % 4]);
5835 last_spin_time
= now
;
5837 spincheck
= CHECK_SPINNER
;
5842 * Iterate over any nested datasets.
5844 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
5850 * Skip any datasets whose type does not match.
5852 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
5856 libzfs_add_handle(cbp
, zhp
);
5857 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
5863 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
5865 get_all_cb_t cb
= { 0 };
5866 cb
.cb_verbose
= verbose
;
5867 cb
.cb_getone
= get_one_dataset
;
5870 set_progress_header(gettext("Reading ZFS config"));
5871 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
5873 *dslist
= cb
.cb_handles
;
5874 *count
= cb
.cb_used
;
5877 finish_progress(gettext("done."));
5881 * Generic callback for sharing or mounting filesystems. Because the code is so
5882 * similar, we have a common function with an extra parameter to determine which
5883 * mode we are using.
5885 #define OP_SHARE 0x1
5886 #define OP_MOUNT 0x2
5889 * Share or mount a dataset.
5892 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
5893 boolean_t
explicit, const char *options
)
5895 char mountpoint
[ZFS_MAXPROPLEN
];
5896 char shareopts
[ZFS_MAXPROPLEN
];
5897 char smbshareopts
[ZFS_MAXPROPLEN
];
5898 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
5900 uint64_t zoned
, canmount
;
5901 boolean_t shared_nfs
, shared_smb
;
5903 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
5906 * Check to make sure we can mount/share this dataset. If we
5907 * are in the global zone and the filesystem is exported to a
5908 * local zone, or if we are in a local zone and the
5909 * filesystem is not exported, then it is an error.
5911 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
5913 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
5917 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5918 "dataset is exported to a local zone\n"), cmdname
,
5922 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
5926 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5927 "permission denied\n"), cmdname
,
5933 * Ignore any filesystems which don't apply to us. This
5934 * includes those with a legacy mountpoint, or those with
5935 * legacy share options.
5937 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
5938 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
5939 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
5940 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5941 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
5942 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5944 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
5945 strcmp(smbshareopts
, "off") == 0) {
5949 (void) fprintf(stderr
, gettext("cannot share '%s': "
5950 "legacy share\n"), zfs_get_name(zhp
));
5951 (void) fprintf(stderr
, gettext("use share(1M) to "
5952 "share this filesystem, or set "
5953 "sharenfs property on\n"));
5958 * We cannot share or mount legacy filesystems. If the
5959 * shareopts is non-legacy but the mountpoint is legacy, we
5960 * treat it as a legacy share.
5962 if (strcmp(mountpoint
, "legacy") == 0) {
5966 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5967 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
5968 (void) fprintf(stderr
, gettext("use %s(1M) to "
5969 "%s this filesystem\n"), cmdname
, cmdname
);
5973 if (strcmp(mountpoint
, "none") == 0) {
5977 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
5978 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
5983 * canmount explicit outcome
5984 * on no pass through
5985 * on yes pass through
5987 * off yes display error, return 1
5988 * noauto no return 0
5989 * noauto yes pass through
5991 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
5992 if (canmount
== ZFS_CANMOUNT_OFF
) {
5996 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5997 "'canmount' property is set to 'off'\n"), cmdname
,
6000 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
6005 * If this filesystem is inconsistent and has a receive resume
6006 * token, we can not mount it.
6008 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
6009 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
6010 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
6014 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6015 "Contains partially-completed state from "
6016 "\"zfs receive -r\", which can be resumed with "
6017 "\"zfs send -t\"\n"),
6018 cmdname
, zfs_get_name(zhp
));
6023 * At this point, we have verified that the mountpoint and/or
6024 * shareopts are appropriate for auto management. If the
6025 * filesystem is already mounted or shared, return (failing
6026 * for explicit requests); otherwise mount or share the
6032 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
6033 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
6035 if ((shared_nfs
&& shared_smb
) ||
6036 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
6037 strcmp(smbshareopts
, "off") == 0) ||
6038 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
6039 strcmp(shareopts
, "off") == 0)) {
6043 (void) fprintf(stderr
, gettext("cannot share "
6044 "'%s': filesystem already shared\n"),
6049 if (!zfs_is_mounted(zhp
, NULL
) &&
6050 zfs_mount(zhp
, NULL
, 0) != 0)
6053 if (protocol
== NULL
) {
6054 if (zfs_shareall(zhp
) != 0)
6056 } else if (strcmp(protocol
, "nfs") == 0) {
6057 if (zfs_share_nfs(zhp
))
6059 } else if (strcmp(protocol
, "smb") == 0) {
6060 if (zfs_share_smb(zhp
))
6063 (void) fprintf(stderr
, gettext("cannot share "
6064 "'%s': invalid share type '%s' "
6066 zfs_get_name(zhp
), protocol
);
6073 if (options
== NULL
)
6074 mnt
.mnt_mntopts
= "";
6076 mnt
.mnt_mntopts
= (char *)options
;
6078 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
6079 zfs_is_mounted(zhp
, NULL
)) {
6083 (void) fprintf(stderr
, gettext("cannot mount "
6084 "'%s': filesystem already mounted\n"),
6089 if (zfs_mount(zhp
, options
, flags
) != 0)
6098 * Reports progress in the form "(current/total)". Not thread-safe.
6101 report_mount_progress(int current
, int total
)
6103 static time_t last_progress_time
= 0;
6104 time_t now
= time(NULL
);
6107 /* report 1..n instead of 0..n-1 */
6110 /* display header if we're here for the first time */
6112 set_progress_header(gettext("Mounting ZFS filesystems"));
6113 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
6114 /* too soon to report again */
6118 last_progress_time
= now
;
6120 (void) sprintf(info
, "(%d/%d)", current
, total
);
6122 if (current
== total
)
6123 finish_progress(info
);
6125 update_progress(info
);
6129 append_options(char *mntopts
, char *newopts
)
6131 int len
= strlen(mntopts
);
6133 /* original length plus new string to append plus 1 for the comma */
6134 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
6135 (void) fprintf(stderr
, gettext("the opts argument for "
6136 "'%s' option is too long (more than %d chars)\n"),
6137 "-o", MNT_LINE_MAX
);
6142 mntopts
[len
++] = ',';
6144 (void) strcpy(&mntopts
[len
], newopts
);
6148 share_mount(int op
, int argc
, char **argv
)
6151 boolean_t verbose
= B_FALSE
;
6153 char *options
= NULL
;
6157 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
6167 if (*optarg
== '\0') {
6168 (void) fprintf(stderr
, gettext("empty mount "
6169 "options (-o) specified\n"));
6173 if (options
== NULL
)
6174 options
= safe_malloc(MNT_LINE_MAX
+ 1);
6176 /* option validation is done later */
6177 append_options(options
, optarg
);
6180 flags
|= MS_OVERLAY
;
6183 (void) fprintf(stderr
, gettext("missing argument for "
6184 "'%c' option\n"), optopt
);
6188 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6197 /* check number of arguments */
6199 zfs_handle_t
**dslist
= NULL
;
6200 size_t i
, count
= 0;
6201 char *protocol
= NULL
;
6203 if (op
== OP_SHARE
&& argc
> 0) {
6204 if (strcmp(argv
[0], "nfs") != 0 &&
6205 strcmp(argv
[0], "smb") != 0) {
6206 (void) fprintf(stderr
, gettext("share type "
6207 "must be 'nfs' or 'smb'\n"));
6216 (void) fprintf(stderr
, gettext("too many arguments\n"));
6220 start_progress_timer();
6221 get_all_datasets(&dslist
, &count
, verbose
);
6224 if (options
!= NULL
)
6229 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
6231 for (i
= 0; i
< count
; i
++) {
6233 report_mount_progress(i
, count
);
6235 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
6236 B_FALSE
, options
) != 0)
6238 zfs_close(dslist
[i
]);
6242 } else if (argc
== 0) {
6243 struct mnttab entry
;
6245 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
6246 (void) fprintf(stderr
, gettext("missing filesystem "
6247 "argument (specify -a for all)\n"));
6252 * When mount is given no arguments, go through
6253 * /proc/self/mounts and display any active ZFS mounts.
6254 * We hide any snapshots, since they are controlled
6258 /* Reopen MNTTAB to prevent reading stale data from open file */
6259 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
) {
6260 if (options
!= NULL
)
6265 while (getmntent(mnttab_file
, &entry
) == 0) {
6266 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
6267 strchr(entry
.mnt_special
, '@') != NULL
)
6270 (void) printf("%-30s %s\n", entry
.mnt_special
,
6278 (void) fprintf(stderr
,
6279 gettext("too many arguments\n"));
6283 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6284 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6287 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
6293 if (options
!= NULL
)
6300 * zfs mount -a [nfs]
6301 * zfs mount filesystem
6303 * Mount all filesystems, or mount the given filesystem.
6306 zfs_do_mount(int argc
, char **argv
)
6308 return (share_mount(OP_MOUNT
, argc
, argv
));
6312 * zfs share -a [nfs | smb]
6313 * zfs share filesystem
6315 * Share all filesystems, or share the given filesystem.
6318 zfs_do_share(int argc
, char **argv
)
6320 return (share_mount(OP_SHARE
, argc
, argv
));
6323 typedef struct unshare_unmount_node
{
6324 zfs_handle_t
*un_zhp
;
6326 uu_avl_node_t un_avlnode
;
6327 } unshare_unmount_node_t
;
6331 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
6333 const unshare_unmount_node_t
*l
= larg
;
6334 const unshare_unmount_node_t
*r
= rarg
;
6336 return (strcmp(l
->un_mountp
, r
->un_mountp
));
6340 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6341 * absolute path, find the entry /proc/self/mounts, verify that its a
6342 * ZFS filesystems, and unmount it appropriately.
6345 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
6349 struct stat64 statbuf
;
6350 struct extmnttab entry
;
6351 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
6355 * Search for the path in /proc/self/mounts. Rather than looking for the
6356 * specific path, which can be fooled by non-standard paths (i.e. ".."
6357 * or "//"), we stat() the path and search for the corresponding
6358 * (major,minor) device pair.
6360 if (stat64(path
, &statbuf
) != 0) {
6361 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6362 cmdname
, path
, strerror(errno
));
6365 path_inode
= statbuf
.st_ino
;
6368 * Search for the given (major,minor) pair in the mount table.
6371 /* Reopen MNTTAB to prevent reading stale data from open file */
6372 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6375 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
6376 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
6377 entry
.mnt_minor
== minor(statbuf
.st_dev
))
6381 if (op
== OP_SHARE
) {
6382 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
6383 "currently mounted\n"), cmdname
, path
);
6386 (void) fprintf(stderr
, gettext("warning: %s not in"
6387 "/proc/self/mounts\n"), path
);
6388 if ((ret
= umount2(path
, flags
)) != 0)
6389 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
6394 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
6395 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
6396 "filesystem\n"), cmdname
, path
);
6400 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6401 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6405 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
6406 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6407 cmdname
, path
, strerror(errno
));
6409 } else if (statbuf
.st_ino
!= path_inode
) {
6410 (void) fprintf(stderr
, gettext("cannot "
6411 "%s '%s': not a mountpoint\n"), cmdname
, path
);
6415 if (op
== OP_SHARE
) {
6416 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6417 char smbshare_prop
[ZFS_MAXPROPLEN
];
6419 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
6420 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6421 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
6422 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6424 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6425 strcmp(smbshare_prop
, "off") == 0) {
6426 (void) fprintf(stderr
, gettext("cannot unshare "
6427 "'%s': legacy share\n"), path
);
6428 (void) fprintf(stderr
, gettext("use exportfs(8) "
6429 "or smbcontrol(1) to unshare this filesystem\n"));
6430 } else if (!zfs_is_shared(zhp
)) {
6431 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6432 "not currently shared\n"), path
);
6434 ret
= zfs_unshareall_bypath(zhp
, path
);
6437 char mtpt_prop
[ZFS_MAXPROPLEN
];
6439 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6440 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6443 ret
= zfs_unmount(zhp
, NULL
, flags
);
6444 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6445 (void) fprintf(stderr
, gettext("cannot unmount "
6446 "'%s': legacy mountpoint\n"),
6448 (void) fprintf(stderr
, gettext("use umount(8) "
6449 "to unmount this filesystem\n"));
6451 ret
= zfs_unmountall(zhp
, flags
);
6462 * Generic callback for unsharing or unmounting a filesystem.
6465 unshare_unmount(int op
, int argc
, char **argv
)
6472 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6473 char sharesmb
[ZFS_MAXPROPLEN
];
6476 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? ":a" : "af")) != -1) {
6485 (void) fprintf(stderr
, gettext("missing argument for "
6486 "'%c' option\n"), optopt
);
6490 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6501 * We could make use of zfs_for_each() to walk all datasets in
6502 * the system, but this would be very inefficient, especially
6503 * since we would have to linearly search /proc/self/mounts for
6504 * each one. Instead, do one pass through /proc/self/mounts
6505 * looking for zfs entries and call zfs_unmount() for each one.
6507 * Things get a little tricky if the administrator has created
6508 * mountpoints beneath other ZFS filesystems. In this case, we
6509 * have to unmount the deepest filesystems first. To accomplish
6510 * this, we place all the mountpoints in an AVL tree sorted by
6511 * the special type (dataset name), and walk the result in
6512 * reverse to make sure to get any snapshots first.
6514 struct mnttab entry
;
6515 uu_avl_pool_t
*pool
;
6516 uu_avl_t
*tree
= NULL
;
6517 unshare_unmount_node_t
*node
;
6519 uu_avl_walk_t
*walk
;
6520 char *protocol
= NULL
;
6522 if (op
== OP_SHARE
&& argc
> 0) {
6523 if (strcmp(argv
[0], "nfs") != 0 &&
6524 strcmp(argv
[0], "smb") != 0) {
6525 (void) fprintf(stderr
, gettext("share type "
6526 "must be 'nfs' or 'smb'\n"));
6535 (void) fprintf(stderr
, gettext("too many arguments\n"));
6539 if (((pool
= uu_avl_pool_create("unmount_pool",
6540 sizeof (unshare_unmount_node_t
),
6541 offsetof(unshare_unmount_node_t
, un_avlnode
),
6542 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6543 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6546 /* Reopen MNTTAB to prevent reading stale data from open file */
6547 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6550 while (getmntent(mnttab_file
, &entry
) == 0) {
6552 /* ignore non-ZFS entries */
6553 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6556 /* ignore snapshots */
6557 if (strchr(entry
.mnt_special
, '@') != NULL
)
6560 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6561 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6568 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6570 sizeof (nfs_mnt_prop
),
6571 NULL
, NULL
, 0, B_FALSE
) == 0);
6572 if (strcmp(nfs_mnt_prop
, "off") != 0)
6574 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6576 sizeof (nfs_mnt_prop
),
6577 NULL
, NULL
, 0, B_FALSE
) == 0);
6578 if (strcmp(nfs_mnt_prop
, "off") == 0)
6582 /* Ignore legacy mounts */
6583 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6585 sizeof (nfs_mnt_prop
),
6586 NULL
, NULL
, 0, B_FALSE
) == 0);
6587 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6589 /* Ignore canmount=noauto mounts */
6590 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6591 ZFS_CANMOUNT_NOAUTO
)
6597 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6599 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6601 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6603 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6604 uu_avl_insert(tree
, node
, idx
);
6606 zfs_close(node
->un_zhp
);
6607 free(node
->un_mountp
);
6613 * Walk the AVL tree in reverse, unmounting each filesystem and
6614 * removing it from the AVL tree in the process.
6616 if ((walk
= uu_avl_walk_start(tree
,
6617 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6620 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6621 uu_avl_remove(tree
, node
);
6625 if (zfs_unshareall_bytype(node
->un_zhp
,
6626 node
->un_mountp
, protocol
) != 0)
6631 if (zfs_unmount(node
->un_zhp
,
6632 node
->un_mountp
, flags
) != 0)
6637 zfs_close(node
->un_zhp
);
6638 free(node
->un_mountp
);
6642 uu_avl_walk_end(walk
);
6643 uu_avl_destroy(tree
);
6644 uu_avl_pool_destroy(pool
);
6649 (void) fprintf(stderr
,
6650 gettext("missing filesystem argument\n"));
6652 (void) fprintf(stderr
,
6653 gettext("too many arguments\n"));
6658 * We have an argument, but it may be a full path or a ZFS
6659 * filesystem. Pass full paths off to unmount_path() (shared by
6660 * manual_unmount), otherwise open the filesystem and pass to
6663 if (argv
[0][0] == '/')
6664 return (unshare_unmount_path(op
, argv
[0],
6667 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6668 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6671 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
6672 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
6673 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
6674 NULL
, 0, B_FALSE
) == 0);
6678 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6680 sizeof (nfs_mnt_prop
),
6681 NULL
, NULL
, 0, B_FALSE
) == 0);
6682 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6683 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
6686 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6687 strcmp(sharesmb
, "off") == 0) {
6688 (void) fprintf(stderr
, gettext("cannot "
6689 "unshare '%s': legacy share\n"),
6691 (void) fprintf(stderr
, gettext("use "
6692 "unshare(1M) to unshare this "
6695 } else if (!zfs_is_shared(zhp
)) {
6696 (void) fprintf(stderr
, gettext("cannot "
6697 "unshare '%s': not currently "
6698 "shared\n"), zfs_get_name(zhp
));
6700 } else if (zfs_unshareall(zhp
) != 0) {
6706 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
6707 (void) fprintf(stderr
, gettext("cannot "
6708 "unmount '%s': legacy "
6709 "mountpoint\n"), zfs_get_name(zhp
));
6710 (void) fprintf(stderr
, gettext("use "
6711 "umount(1M) to unmount this "
6714 } else if (!zfs_is_mounted(zhp
, NULL
)) {
6715 (void) fprintf(stderr
, gettext("cannot "
6716 "unmount '%s': not currently "
6720 } else if (zfs_unmountall(zhp
, flags
) != 0) {
6734 * zfs unmount filesystem
6736 * Unmount all filesystems, or a specific ZFS filesystem.
6739 zfs_do_unmount(int argc
, char **argv
)
6741 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
6746 * zfs unshare filesystem
6748 * Unshare all filesystems, or a specific ZFS filesystem.
6751 zfs_do_unshare(int argc
, char **argv
)
6753 return (unshare_unmount(OP_SHARE
, argc
, argv
));
6757 find_command_idx(char *command
, int *idx
)
6761 for (i
= 0; i
< NCOMMAND
; i
++) {
6762 if (command_table
[i
].name
== NULL
)
6765 if (strcmp(command
, command_table
[i
].name
) == 0) {
6774 zfs_do_diff(int argc
, char **argv
)
6778 char *tosnap
= NULL
;
6779 char *fromsnap
= NULL
;
6784 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
6787 flags
|= ZFS_DIFF_CLASSIFY
;
6790 flags
|= ZFS_DIFF_PARSEABLE
;
6793 flags
|= ZFS_DIFF_TIMESTAMP
;
6796 (void) fprintf(stderr
,
6797 gettext("invalid option '%c'\n"), optopt
);
6806 (void) fprintf(stderr
,
6807 gettext("must provide at least one snapshot name\n"));
6812 (void) fprintf(stderr
, gettext("too many arguments\n"));
6817 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
6820 if (*fromsnap
!= '@')
6821 copy
= strdup(fromsnap
);
6823 copy
= strdup(tosnap
);
6827 if ((atp
= strchr(copy
, '@')) != NULL
)
6830 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6837 * Ignore SIGPIPE so that the library can give us
6838 * information on any failure
6840 (void) sigignore(SIGPIPE
);
6842 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
6850 * zfs bookmark <fs@snap> <fs#bmark>
6852 * Creates a bookmark with the given name from the given snapshot.
6855 zfs_do_bookmark(int argc
, char **argv
)
6857 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
6864 while ((c
= getopt(argc
, argv
, "")) != -1) {
6867 (void) fprintf(stderr
,
6868 gettext("invalid option '%c'\n"), optopt
);
6876 /* check number of arguments */
6878 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
6882 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
6886 if (strchr(argv
[1], '#') == NULL
) {
6887 (void) fprintf(stderr
,
6888 gettext("invalid bookmark name '%s' -- "
6889 "must contain a '#'\n"), argv
[1]);
6893 if (argv
[0][0] == '@') {
6895 * Snapshot name begins with @.
6896 * Default to same fs as bookmark.
6898 (void) strlcpy(snapname
, argv
[1], sizeof (snapname
));
6899 *strchr(snapname
, '#') = '\0';
6900 (void) strlcat(snapname
, argv
[0], sizeof (snapname
));
6902 (void) strlcpy(snapname
, argv
[0], sizeof (snapname
));
6904 zhp
= zfs_open(g_zfs
, snapname
, ZFS_TYPE_SNAPSHOT
);
6910 nvl
= fnvlist_alloc();
6911 fnvlist_add_string(nvl
, argv
[1], snapname
);
6912 ret
= lzc_bookmark(nvl
, NULL
);
6916 const char *err_msg
;
6919 (void) snprintf(errbuf
, sizeof (errbuf
),
6920 dgettext(TEXT_DOMAIN
,
6921 "cannot create bookmark '%s'"), argv
[1]);
6925 err_msg
= "bookmark is in a different pool";
6928 err_msg
= "bookmark exists";
6931 err_msg
= "invalid argument";
6934 err_msg
= "bookmark feature not enabled";
6937 err_msg
= "out of space";
6940 err_msg
= "unknown error";
6943 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
6944 dgettext(TEXT_DOMAIN
, err_msg
));
6955 main(int argc
, char **argv
)
6961 (void) setlocale(LC_ALL
, "");
6962 (void) textdomain(TEXT_DOMAIN
);
6964 dprintf_setup(&argc
, argv
);
6969 * Make sure the user has specified some command.
6972 (void) fprintf(stderr
, gettext("missing command\n"));
6979 * The 'umount' command is an alias for 'unmount'
6981 if (strcmp(cmdname
, "umount") == 0)
6982 cmdname
= "unmount";
6985 * The 'recv' command is an alias for 'receive'
6987 if (strcmp(cmdname
, "recv") == 0)
6988 cmdname
= "receive";
6991 * The 'snap' command is an alias for 'snapshot'
6993 if (strcmp(cmdname
, "snap") == 0)
6994 cmdname
= "snapshot";
6999 if ((strcmp(cmdname
, "-?") == 0) ||
7000 (strcmp(cmdname
, "--help") == 0))
7003 if ((g_zfs
= libzfs_init()) == NULL
) {
7004 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
7008 mnttab_file
= g_zfs
->libzfs_mnttab
;
7010 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
7012 libzfs_print_on_error(g_zfs
, B_TRUE
);
7015 * Run the appropriate command.
7017 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
7018 if (find_command_idx(cmdname
, &i
) == 0) {
7019 current_command
= &command_table
[i
];
7020 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
7021 } else if (strchr(cmdname
, '=') != NULL
) {
7022 verify(find_command_idx("set", &i
) == 0);
7023 current_command
= &command_table
[i
];
7024 ret
= command_table
[i
].func(argc
, argv
);
7026 (void) fprintf(stderr
, gettext("unrecognized "
7027 "command '%s'\n"), cmdname
);
7032 if (ret
== 0 && log_history
)
7033 (void) zpool_log_history(g_zfs
, history_str
);
7038 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7039 * for the purposes of running ::findleaks.
7041 if (getenv("ZFS_ABORT") != NULL
) {
7042 (void) printf("dumping core by request\n");