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 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
29 * Copyright 2016 Nexenta Systems, Inc.
39 #include <libnvpair.h>
52 #include <sys/mkdev.h>
53 #include <sys/mntent.h>
54 #include <sys/mnttab.h>
55 #include <sys/mount.h>
57 #include <sys/fs/zfs.h>
58 #include <sys/types.h>
62 #include <libzfs_core.h>
64 #include <zfs_deleg.h>
68 #include <directory.h>
69 #endif /* HAVE_IDMAP */
73 #include "zfs_comutil.h"
74 #include "libzfs_impl.h"
76 libzfs_handle_t
*g_zfs
;
78 static FILE *mnttab_file
;
79 static char history_str
[HIS_MAX_RECORD_LEN
];
80 static boolean_t log_history
= B_TRUE
;
82 static int zfs_do_clone(int argc
, char **argv
);
83 static int zfs_do_create(int argc
, char **argv
);
84 static int zfs_do_destroy(int argc
, char **argv
);
85 static int zfs_do_get(int argc
, char **argv
);
86 static int zfs_do_inherit(int argc
, char **argv
);
87 static int zfs_do_list(int argc
, char **argv
);
88 static int zfs_do_mount(int argc
, char **argv
);
89 static int zfs_do_rename(int argc
, char **argv
);
90 static int zfs_do_rollback(int argc
, char **argv
);
91 static int zfs_do_set(int argc
, char **argv
);
92 static int zfs_do_upgrade(int argc
, char **argv
);
93 static int zfs_do_snapshot(int argc
, char **argv
);
94 static int zfs_do_unmount(int argc
, char **argv
);
95 static int zfs_do_share(int argc
, char **argv
);
96 static int zfs_do_unshare(int argc
, char **argv
);
97 static int zfs_do_send(int argc
, char **argv
);
98 static int zfs_do_receive(int argc
, char **argv
);
99 static int zfs_do_promote(int argc
, char **argv
);
100 static int zfs_do_userspace(int argc
, char **argv
);
101 static int zfs_do_allow(int argc
, char **argv
);
102 static int zfs_do_unallow(int argc
, char **argv
);
103 static int zfs_do_hold(int argc
, char **argv
);
104 static int zfs_do_holds(int argc
, char **argv
);
105 static int zfs_do_release(int argc
, char **argv
);
106 static int zfs_do_diff(int argc
, char **argv
);
107 static int zfs_do_bookmark(int argc
, char **argv
);
110 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
115 _umem_debug_init(void)
117 return ("default,verbose"); /* $UMEM_DEBUG setting */
121 _umem_logging_init(void)
123 return ("fail,contents"); /* $UMEM_LOGGING setting */
157 typedef struct zfs_command
{
159 int (*func
)(int argc
, char **argv
);
164 * Master command table. Each ZFS command has a name, associated function, and
165 * usage message. The usage messages need to be internationalized, so we have
166 * to have a function to return the usage message based on a command index.
168 * These commands are organized according to how they are displayed in the usage
169 * message. An empty command (one with a NULL name) indicates an empty line in
170 * the generic usage message.
172 static zfs_command_t command_table
[] = {
173 { "create", zfs_do_create
, HELP_CREATE
},
174 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
176 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
177 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
178 { "clone", zfs_do_clone
, HELP_CLONE
},
179 { "promote", zfs_do_promote
, HELP_PROMOTE
},
180 { "rename", zfs_do_rename
, HELP_RENAME
},
181 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
183 { "list", zfs_do_list
, HELP_LIST
},
185 { "set", zfs_do_set
, HELP_SET
},
186 { "get", zfs_do_get
, HELP_GET
},
187 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
188 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
189 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
190 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
192 { "mount", zfs_do_mount
, HELP_MOUNT
},
193 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
194 { "share", zfs_do_share
, HELP_SHARE
},
195 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
197 { "send", zfs_do_send
, HELP_SEND
},
198 { "receive", zfs_do_receive
, HELP_RECEIVE
},
200 { "allow", zfs_do_allow
, HELP_ALLOW
},
202 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
204 { "hold", zfs_do_hold
, HELP_HOLD
},
205 { "holds", zfs_do_holds
, HELP_HOLDS
},
206 { "release", zfs_do_release
, HELP_RELEASE
},
207 { "diff", zfs_do_diff
, HELP_DIFF
},
210 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
212 zfs_command_t
*current_command
;
215 get_usage(zfs_help_t idx
)
219 return (gettext("\tclone [-p] [-o property=value] ... "
220 "<snapshot> <filesystem|volume>\n"));
222 return (gettext("\tcreate [-p] [-o property=value] ... "
224 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
225 "-V <size> <volume>\n"));
227 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
228 "\tdestroy [-dnpRrv] "
229 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
230 "\tdestroy <filesystem|volume>#<bookmark>\n"));
232 return (gettext("\tget [-rHp] [-d max] "
233 "[-o \"all\" | field[,...]]\n"
234 "\t [-t type[,...]] [-s source[,...]]\n"
235 "\t <\"all\" | property[,...]> "
236 "[filesystem|volume|snapshot|bookmark] ...\n"));
238 return (gettext("\tinherit [-rS] <property> "
239 "<filesystem|volume|snapshot> ...\n"));
241 return (gettext("\tupgrade [-v]\n"
242 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
244 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
245 "[-s property]...\n\t [-S property]... [-t type[,...]] "
246 "[filesystem|volume|snapshot] ...\n"));
248 return (gettext("\tmount\n"
249 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
251 return (gettext("\tpromote <clone-filesystem>\n"));
253 return (gettext("\treceive [-vnsFu] "
254 "[-o <property>=<value>] ... [-x <property>] ...\n"
255 "\t <filesystem|volume|snapshot>\n"
256 "\treceive [-vnsFu] [-o <property>=<value>] ... "
257 "[-x <property>] ... \n"
258 "\t [-d | -e] <filesystem>\n"
259 "\treceive -A <filesystem|volume>\n"));
261 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
262 "<filesystem|volume|snapshot>\n"
263 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
264 "\trename -r <snapshot> <snapshot>\n"));
266 return (gettext("\trollback [-rRf] <snapshot>\n"));
268 return (gettext("\tsend [-DnPpRvLec] [-[i|I] snapshot] "
270 "\tsend [-Lec] [-i snapshot|bookmark] "
271 "<filesystem|volume|snapshot>\n"
272 "\tsend [-nvPe] -t <receive_resume_token>\n"));
274 return (gettext("\tset <property=value> ... "
275 "<filesystem|volume|snapshot> ...\n"));
277 return (gettext("\tshare <-a [nfs|smb] | filesystem>\n"));
279 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
280 "<filesystem|volume>@<snap> ...\n"));
282 return (gettext("\tunmount [-f] "
283 "<-a | filesystem|mountpoint>\n"));
285 return (gettext("\tunshare "
286 "<-a [nfs|smb] | filesystem|mountpoint>\n"));
288 return (gettext("\tallow <filesystem|volume>\n"
290 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
291 "\t <filesystem|volume>\n"
292 "\tallow [-ld] -e <perm|@setname>[,...] "
293 "<filesystem|volume>\n"
294 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
295 "\tallow -s @setname <perm|@setname>[,...] "
296 "<filesystem|volume>\n"));
298 return (gettext("\tunallow [-rldug] "
299 "<\"everyone\"|user|group>[,...]\n"
300 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
301 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
302 "<filesystem|volume>\n"
303 "\tunallow [-r] -c [<perm|@setname>[,...]] "
304 "<filesystem|volume>\n"
305 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
306 "<filesystem|volume>\n"));
308 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
310 "\t [-S field] ... [-t type[,...]] "
311 "<filesystem|snapshot>\n"));
312 case HELP_GROUPSPACE
:
313 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
315 "\t [-S field] ... [-t type[,...]] "
316 "<filesystem|snapshot>\n"));
318 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
320 return (gettext("\tholds [-r] <snapshot> ...\n"));
322 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
324 return (gettext("\tdiff [-FHt] <snapshot> "
325 "[snapshot|filesystem]\n"));
327 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
337 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
342 * Utility function to guarantee malloc() success.
346 safe_malloc(size_t size
)
350 if ((data
= calloc(1, size
)) == NULL
)
357 safe_strdup(char *str
)
359 char *dupstr
= strdup(str
);
368 * Callback routine that will print out information for each of
372 usage_prop_cb(int prop
, void *cb
)
376 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
378 if (zfs_prop_readonly(prop
))
379 (void) fprintf(fp
, " NO ");
381 (void) fprintf(fp
, "YES ");
383 if (zfs_prop_inheritable(prop
))
384 (void) fprintf(fp
, " YES ");
386 (void) fprintf(fp
, " NO ");
388 if (zfs_prop_values(prop
) == NULL
)
389 (void) fprintf(fp
, "-\n");
391 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
397 * Display usage message. If we're inside a command, display only the usage for
398 * that command. Otherwise, iterate over the entire command table and display
399 * a complete usage message.
402 usage(boolean_t requested
)
405 boolean_t show_properties
= B_FALSE
;
406 FILE *fp
= requested
? stdout
: stderr
;
408 if (current_command
== NULL
) {
410 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
412 gettext("where 'command' is one of the following:\n\n"));
414 for (i
= 0; i
< NCOMMAND
; i
++) {
415 if (command_table
[i
].name
== NULL
)
416 (void) fprintf(fp
, "\n");
418 (void) fprintf(fp
, "%s",
419 get_usage(command_table
[i
].usage
));
422 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
423 "pool/[dataset/]*dataset[@name]\n"));
425 (void) fprintf(fp
, gettext("usage:\n"));
426 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
429 if (current_command
!= NULL
&&
430 (strcmp(current_command
->name
, "set") == 0 ||
431 strcmp(current_command
->name
, "get") == 0 ||
432 strcmp(current_command
->name
, "inherit") == 0 ||
433 strcmp(current_command
->name
, "list") == 0))
434 show_properties
= B_TRUE
;
436 if (show_properties
) {
438 gettext("\nThe following properties are supported:\n"));
440 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
441 "PROPERTY", "EDIT", "INHERIT", "VALUES");
443 /* Iterate over all properties */
444 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
447 (void) fprintf(fp
, "\t%-15s ", "userused@...");
448 (void) fprintf(fp
, " NO NO <size>\n");
449 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
450 (void) fprintf(fp
, " NO NO <size>\n");
451 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
452 (void) fprintf(fp
, "YES NO <size> | none\n");
453 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
454 (void) fprintf(fp
, "YES NO <size> | none\n");
455 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
456 (void) fprintf(fp
, " NO NO <size>\n");
458 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
459 "with standard units such as K, M, G, etc.\n"));
460 (void) fprintf(fp
, gettext("\nUser-defined properties can "
461 "be specified by using a name containing a colon (:).\n"));
462 (void) fprintf(fp
, gettext("\nThe {user|group}{used|quota}@ "
463 "properties must be appended with\n"
464 "a user or group specifier of one of these forms:\n"
465 " POSIX name (eg: \"matt\")\n"
466 " POSIX id (eg: \"126829\")\n"
467 " SMB name@domain (eg: \"matt@sun\")\n"
468 " SMB SID (eg: \"S-1-234-567-89\")\n"));
471 gettext("\nFor the property list, run: %s\n"),
474 gettext("\nFor the delegated permission list, run: %s\n"),
475 "zfs allow|unallow");
479 * See comments at end of main().
481 if (getenv("ZFS_ABORT") != NULL
) {
482 (void) printf("dumping core by request\n");
486 exit(requested
? 0 : 2);
490 * Take a property=value argument string and add it to the given nvlist.
491 * Modifies the argument inplace.
494 parseprop(nvlist_t
*props
, char *propname
)
498 if ((propval
= strchr(propname
, '=')) == NULL
) {
499 (void) fprintf(stderr
, gettext("missing "
500 "'=' for property=value argument\n"));
505 if (nvlist_exists(props
, propname
)) {
506 (void) fprintf(stderr
, gettext("property '%s' "
507 "specified multiple times\n"), propname
);
510 if (nvlist_add_string(props
, propname
, propval
) != 0)
516 * Take a property name argument and add it to the given nvlist.
517 * Modifies the argument inplace.
520 parsepropname(nvlist_t
*props
, char *propname
)
522 if (strchr(propname
, '=') != NULL
) {
523 (void) fprintf(stderr
, gettext("invalid character "
524 "'=' in property argument\n"));
527 if (nvlist_exists(props
, propname
)) {
528 (void) fprintf(stderr
, gettext("property '%s' "
529 "specified multiple times\n"), propname
);
532 if (nvlist_add_boolean(props
, propname
) != 0)
538 parse_depth(char *opt
, int *flags
)
543 depth
= (int)strtol(opt
, &tmp
, 0);
545 (void) fprintf(stderr
,
546 gettext("%s is not an integer\n"), optarg
);
550 (void) fprintf(stderr
,
551 gettext("Depth can not be negative.\n"));
554 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
558 #define PROGRESS_DELAY 2 /* seconds */
560 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";
561 static time_t pt_begin
;
562 static char *pt_header
= NULL
;
563 static boolean_t pt_shown
;
566 start_progress_timer(void)
568 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
573 set_progress_header(char *header
)
575 assert(pt_header
== NULL
);
576 pt_header
= safe_strdup(header
);
578 (void) printf("%s: ", header
);
579 (void) fflush(stdout
);
584 update_progress(char *update
)
586 if (!pt_shown
&& time(NULL
) > pt_begin
) {
587 int len
= strlen(update
);
589 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
591 (void) fflush(stdout
);
593 } else if (pt_shown
) {
594 int len
= strlen(update
);
596 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
597 (void) fflush(stdout
);
602 finish_progress(char *done
)
605 (void) printf("%s\n", done
);
606 (void) fflush(stdout
);
613 zfs_mount_and_share(libzfs_handle_t
*hdl
, const char *dataset
, zfs_type_t type
)
615 zfs_handle_t
*zhp
= NULL
;
618 zhp
= zfs_open(hdl
, dataset
, type
);
623 * Volumes may neither be mounted or shared. Potentially in the
624 * future filesystems detected on these volumes could be mounted.
626 if (zfs_get_type(zhp
) == ZFS_TYPE_VOLUME
) {
632 * Mount and/or share the new filesystem as appropriate. We provide a
633 * verbose error message to let the user know that their filesystem was
634 * in fact created, even if we failed to mount or share it.
636 * If the user doesn't want the dataset automatically mounted, then
637 * skip the mount/share step
639 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
, B_FALSE
) &&
640 zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
) {
641 if (geteuid() != 0) {
642 (void) fprintf(stderr
, gettext("filesystem "
643 "successfully created, but it may only be "
644 "mounted by root\n"));
646 } else if (zfs_mount(zhp
, NULL
, 0) != 0) {
647 (void) fprintf(stderr
, gettext("filesystem "
648 "successfully created, but not mounted\n"));
650 } else if (zfs_share(zhp
) != 0) {
651 (void) fprintf(stderr
, gettext("filesystem "
652 "successfully created, but not shared\n"));
663 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
665 * Given an existing dataset, create a writable copy whose initial contents
666 * are the same as the source. The newly created dataset maintains a
667 * dependency on the original; the original cannot be destroyed so long as
670 * The '-p' flag creates all the non-existing ancestors of the target first.
673 zfs_do_clone(int argc
, char **argv
)
675 zfs_handle_t
*zhp
= NULL
;
676 boolean_t parents
= B_FALSE
;
681 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
685 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
688 if (!parseprop(props
, optarg
)) {
697 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
706 /* check number of arguments */
708 (void) fprintf(stderr
, gettext("missing source dataset "
713 (void) fprintf(stderr
, gettext("missing target dataset "
718 (void) fprintf(stderr
, gettext("too many arguments\n"));
722 /* open the source dataset */
723 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
) {
728 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
731 * Now create the ancestors of the target dataset. If the
732 * target already exists and '-p' option was used we should not
735 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
741 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
749 ret
= zfs_clone(zhp
, argv
[1], props
);
751 /* create the mountpoint if necessary */
754 (void) zpool_log_history(g_zfs
, history_str
);
755 log_history
= B_FALSE
;
758 ret
= zfs_mount_and_share(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
775 * zfs create [-p] [-o prop=value] ... fs
776 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
778 * Create a new dataset. This command can be used to create filesystems
779 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
780 * For volumes, the user must specify a size to be used.
782 * The '-s' flag applies only to volumes, and indicates that we should not try
783 * to set the reservation for this volume. By default we set a reservation
784 * equal to the size for any volume. For pools with SPA_VERSION >=
785 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
787 * The '-p' flag creates all the non-existing ancestors of the target first.
790 zfs_do_create(int argc
, char **argv
)
792 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
793 uint64_t volsize
= 0;
795 boolean_t noreserve
= B_FALSE
;
796 boolean_t bflag
= B_FALSE
;
797 boolean_t parents
= B_FALSE
;
802 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
806 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
809 type
= ZFS_TYPE_VOLUME
;
810 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
811 (void) fprintf(stderr
, gettext("bad volume "
812 "size '%s': %s\n"), optarg
,
813 libzfs_error_description(g_zfs
));
817 if (nvlist_add_uint64(props
,
818 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
827 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
828 (void) fprintf(stderr
, gettext("bad volume "
829 "block size '%s': %s\n"), optarg
,
830 libzfs_error_description(g_zfs
));
834 if (nvlist_add_uint64(props
,
835 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
840 if (!parseprop(props
, optarg
))
847 (void) fprintf(stderr
, gettext("missing size "
851 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
857 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
858 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
859 "used when creating a volume\n"));
866 /* check number of arguments */
868 (void) fprintf(stderr
, gettext("missing %s argument\n"),
869 zfs_type_to_name(type
));
873 (void) fprintf(stderr
, gettext("too many arguments\n"));
877 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
878 zpool_handle_t
*zpool_handle
;
879 nvlist_t
*real_props
= NULL
;
880 uint64_t spa_version
;
882 zfs_prop_t resv_prop
;
886 if ((p
= strchr(argv
[0], '/')) != NULL
)
888 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
891 if (zpool_handle
== NULL
)
893 spa_version
= zpool_get_prop_int(zpool_handle
,
894 ZPOOL_PROP_VERSION
, NULL
);
895 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
896 resv_prop
= ZFS_PROP_REFRESERVATION
;
898 resv_prop
= ZFS_PROP_RESERVATION
;
900 (void) snprintf(msg
, sizeof (msg
),
901 gettext("cannot create '%s'"), argv
[0]);
902 if (props
&& (real_props
= zfs_valid_proplist(g_zfs
, type
,
903 props
, 0, NULL
, zpool_handle
, msg
)) == NULL
) {
904 zpool_close(zpool_handle
);
907 zpool_close(zpool_handle
);
909 volsize
= zvol_volsize_to_reservation(volsize
, real_props
);
910 nvlist_free(real_props
);
912 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
914 if (nvlist_add_uint64(props
,
915 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
922 if (parents
&& zfs_name_valid(argv
[0], type
)) {
924 * Now create the ancestors of target dataset. If the target
925 * already exists and '-p' option was used we should not
928 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
932 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
937 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
941 (void) zpool_log_history(g_zfs
, history_str
);
942 log_history
= B_FALSE
;
945 ret
= zfs_mount_and_share(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
956 * zfs destroy [-rRf] <fs, vol>
957 * zfs destroy [-rRd] <snap>
959 * -r Recursively destroy all children
960 * -R Recursively destroy all dependents, including clones
961 * -f Force unmounting of any dependents
962 * -d If we can't destroy now, mark for deferred destruction
964 * Destroys the given dataset. By default, it will unmount any filesystems,
965 * and refuse to destroy a dataset that has any dependents. A dependent can
966 * either be a child, or a clone of a child.
968 typedef struct destroy_cbdata
{
971 boolean_t cb_recurse
;
973 boolean_t cb_doclones
;
974 zfs_handle_t
*cb_target
;
975 boolean_t cb_defer_destroy
;
976 boolean_t cb_verbose
;
977 boolean_t cb_parsable
;
980 nvlist_t
*cb_batchedsnaps
;
982 /* first snap in contiguous run */
984 /* previous snap in contiguous run */
992 * Check for any dependents based on the '-r' or '-R' flags.
995 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
997 destroy_cbdata_t
*cbp
= data
;
998 const char *tname
= zfs_get_name(cbp
->cb_target
);
999 const char *name
= zfs_get_name(zhp
);
1001 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
1002 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
1004 * This is a direct descendant, not a clone somewhere else in
1007 if (cbp
->cb_recurse
)
1010 if (cbp
->cb_first
) {
1011 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1012 "%s has children\n"),
1013 zfs_get_name(cbp
->cb_target
),
1014 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1015 (void) fprintf(stderr
, gettext("use '-r' to destroy "
1016 "the following datasets:\n"));
1017 cbp
->cb_first
= B_FALSE
;
1018 cbp
->cb_error
= B_TRUE
;
1021 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1024 * This is a clone. We only want to report this if the '-r'
1025 * wasn't specified, or the target is a snapshot.
1027 if (!cbp
->cb_recurse
&&
1028 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
1031 if (cbp
->cb_first
) {
1032 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1033 "%s has dependent clones\n"),
1034 zfs_get_name(cbp
->cb_target
),
1035 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1036 (void) fprintf(stderr
, gettext("use '-R' to destroy "
1037 "the following datasets:\n"));
1038 cbp
->cb_first
= B_FALSE
;
1039 cbp
->cb_error
= B_TRUE
;
1040 cbp
->cb_dryrun
= B_TRUE
;
1043 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1052 destroy_callback(zfs_handle_t
*zhp
, void *data
)
1054 destroy_cbdata_t
*cb
= data
;
1055 const char *name
= zfs_get_name(zhp
);
1057 if (cb
->cb_verbose
) {
1058 if (cb
->cb_parsable
) {
1059 (void) printf("destroy\t%s\n", name
);
1060 } else if (cb
->cb_dryrun
) {
1061 (void) printf(gettext("would destroy %s\n"),
1064 (void) printf(gettext("will destroy %s\n"),
1070 * Ignore pools (which we've already flagged as an error before getting
1073 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1074 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1078 if (cb
->cb_dryrun
) {
1084 * We batch up all contiguous snapshots (even of different
1085 * filesystems) and destroy them with one ioctl. We can't
1086 * simply do all snap deletions and then all fs deletions,
1087 * because we must delete a clone before its origin.
1089 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1090 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1092 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1093 cb
->cb_batchedsnaps
, B_FALSE
);
1094 fnvlist_free(cb
->cb_batchedsnaps
);
1095 cb
->cb_batchedsnaps
= fnvlist_alloc();
1098 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1099 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1110 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1112 destroy_cbdata_t
*cb
= arg
;
1113 const char *name
= zfs_get_name(zhp
);
1116 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1117 if (cb
->cb_firstsnap
== NULL
)
1118 cb
->cb_firstsnap
= strdup(name
);
1119 if (cb
->cb_prevsnap
!= NULL
)
1120 free(cb
->cb_prevsnap
);
1121 /* this snap continues the current range */
1122 cb
->cb_prevsnap
= strdup(name
);
1123 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1125 if (cb
->cb_verbose
) {
1126 if (cb
->cb_parsable
) {
1127 (void) printf("destroy\t%s\n", name
);
1128 } else if (cb
->cb_dryrun
) {
1129 (void) printf(gettext("would destroy %s\n"),
1132 (void) printf(gettext("will destroy %s\n"),
1136 } else if (cb
->cb_firstsnap
!= NULL
) {
1137 /* end of this range */
1139 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1140 cb
->cb_prevsnap
, &used
);
1141 cb
->cb_snapused
+= used
;
1142 free(cb
->cb_firstsnap
);
1143 cb
->cb_firstsnap
= NULL
;
1144 free(cb
->cb_prevsnap
);
1145 cb
->cb_prevsnap
= NULL
;
1152 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1155 assert(cb
->cb_firstsnap
== NULL
);
1156 assert(cb
->cb_prevsnap
== NULL
);
1157 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1158 if (cb
->cb_firstsnap
!= NULL
) {
1161 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1162 cb
->cb_prevsnap
, &used
);
1164 cb
->cb_snapused
+= used
;
1165 free(cb
->cb_firstsnap
);
1166 cb
->cb_firstsnap
= NULL
;
1167 free(cb
->cb_prevsnap
);
1168 cb
->cb_prevsnap
= NULL
;
1174 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1176 destroy_cbdata_t
*cb
= arg
;
1179 /* Check for clones. */
1180 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1181 cb
->cb_target
= zhp
;
1182 cb
->cb_first
= B_TRUE
;
1183 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1184 destroy_check_dependent
, cb
);
1188 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1196 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1198 destroy_cbdata_t
*cb
= arg
;
1201 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1207 if (cb
->cb_verbose
) {
1208 err
= destroy_print_snapshots(zhp
, cb
);
1214 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1222 destroy_clones(destroy_cbdata_t
*cb
)
1225 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1227 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1228 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1231 boolean_t defer
= cb
->cb_defer_destroy
;
1235 * We can't defer destroy non-snapshots, so set it to
1236 * false while destroying the clones.
1238 cb
->cb_defer_destroy
= B_FALSE
;
1239 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1240 destroy_callback
, cb
);
1241 cb
->cb_defer_destroy
= defer
;
1251 zfs_do_destroy(int argc
, char **argv
)
1253 destroy_cbdata_t cb
= { 0 };
1257 zfs_handle_t
*zhp
= NULL
;
1259 zfs_type_t type
= ZFS_TYPE_DATASET
;
1262 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1265 cb
.cb_verbose
= B_TRUE
;
1268 cb
.cb_verbose
= B_TRUE
;
1269 cb
.cb_parsable
= B_TRUE
;
1272 cb
.cb_dryrun
= B_TRUE
;
1275 cb
.cb_defer_destroy
= B_TRUE
;
1276 type
= ZFS_TYPE_SNAPSHOT
;
1279 cb
.cb_force
= B_TRUE
;
1282 cb
.cb_recurse
= B_TRUE
;
1285 cb
.cb_recurse
= B_TRUE
;
1286 cb
.cb_doclones
= B_TRUE
;
1290 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1299 /* check number of arguments */
1301 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1305 (void) fprintf(stderr
, gettext("too many arguments\n"));
1309 at
= strchr(argv
[0], '@');
1310 pound
= strchr(argv
[0], '#');
1313 /* Build the list of snaps to destroy in cb_nvl. */
1314 cb
.cb_nvl
= fnvlist_alloc();
1317 zhp
= zfs_open(g_zfs
, argv
[0],
1318 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1320 nvlist_free(cb
.cb_nvl
);
1324 cb
.cb_snapspec
= at
+ 1;
1325 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1331 if (nvlist_empty(cb
.cb_nvl
)) {
1332 (void) fprintf(stderr
, gettext("could not find any "
1333 "snapshots to destroy; check snapshot names.\n"));
1338 if (cb
.cb_verbose
) {
1340 zfs_nicebytes(cb
.cb_snapused
, buf
, sizeof (buf
));
1341 if (cb
.cb_parsable
) {
1342 (void) printf("reclaim\t%llu\n",
1343 (u_longlong_t
)cb
.cb_snapused
);
1344 } else if (cb
.cb_dryrun
) {
1345 (void) printf(gettext("would reclaim %s\n"),
1348 (void) printf(gettext("will reclaim %s\n"),
1353 if (!cb
.cb_dryrun
) {
1354 if (cb
.cb_doclones
) {
1355 cb
.cb_batchedsnaps
= fnvlist_alloc();
1356 err
= destroy_clones(&cb
);
1358 err
= zfs_destroy_snaps_nvl(g_zfs
,
1359 cb
.cb_batchedsnaps
, B_FALSE
);
1367 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1368 cb
.cb_defer_destroy
);
1374 } else if (pound
!= NULL
) {
1379 (void) fprintf(stderr
,
1380 "dryrun is not supported with bookmark\n");
1384 if (cb
.cb_defer_destroy
) {
1385 (void) fprintf(stderr
,
1386 "defer destroy is not supported with bookmark\n");
1390 if (cb
.cb_recurse
) {
1391 (void) fprintf(stderr
,
1392 "recursive is not supported with bookmark\n");
1396 if (!zfs_bookmark_exists(argv
[0])) {
1397 (void) fprintf(stderr
, gettext("bookmark '%s' "
1398 "does not exist.\n"), argv
[0]);
1402 nvl
= fnvlist_alloc();
1403 fnvlist_add_boolean(nvl
, argv
[0]);
1405 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1407 (void) zfs_standard_error(g_zfs
, err
,
1408 "cannot destroy bookmark");
1415 /* Open the given dataset */
1416 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1422 * Perform an explicit check for pools before going any further.
1424 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1425 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1426 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1427 "operation does not apply to pools\n"),
1429 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1430 "%s' to destroy all datasets in the pool\n"),
1432 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1433 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1439 * Check for any dependents and/or clones.
1441 cb
.cb_first
= B_TRUE
;
1442 if (!cb
.cb_doclones
&&
1443 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1454 cb
.cb_batchedsnaps
= fnvlist_alloc();
1455 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1462 * Do the real thing. The callback will close the
1463 * handle regardless of whether it succeeds or not.
1465 err
= destroy_callback(zhp
, &cb
);
1468 err
= zfs_destroy_snaps_nvl(g_zfs
,
1469 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1476 fnvlist_free(cb
.cb_batchedsnaps
);
1477 fnvlist_free(cb
.cb_nvl
);
1484 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1487 zfs_get_column_t col
;
1489 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1490 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1491 if (col
== GET_COL_RECVD
)
1497 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1498 * < all | property[,property]... > < fs | snap | vol > ...
1500 * -r recurse over any child datasets
1501 * -H scripted mode. Headers are stripped, and fields are separated
1502 * by tabs instead of spaces.
1503 * -o Set of fields to display. One of "name,property,value,
1504 * received,source". Default is "name,property,value,source".
1505 * "all" is an alias for all five.
1506 * -s Set of sources to allow. One of
1507 * "local,default,inherited,received,temporary,none". Default is
1509 * -p Display values in parsable (literal) format.
1511 * Prints properties for the given datasets. The user can control which
1512 * columns to display as well as which property types to allow.
1516 * Invoked to display the properties for a single dataset.
1519 get_callback(zfs_handle_t
*zhp
, void *data
)
1521 char buf
[ZFS_MAXPROPLEN
];
1522 char rbuf
[ZFS_MAXPROPLEN
];
1523 zprop_source_t sourcetype
;
1524 char source
[ZFS_MAX_DATASET_NAME_LEN
];
1525 zprop_get_cbdata_t
*cbp
= data
;
1526 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1527 zprop_list_t
*pl
= cbp
->cb_proplist
;
1531 boolean_t received
= is_recvd_column(cbp
);
1533 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1534 char *recvdval
= NULL
;
1536 * Skip the special fake placeholder. This will also skip over
1537 * the name property when 'all' is specified.
1539 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1540 pl
== cbp
->cb_proplist
)
1543 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1544 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1545 sizeof (buf
), &sourcetype
, source
,
1547 cbp
->cb_literal
) != 0) {
1550 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1551 ZFS_TYPE_DATASET
, B_FALSE
)) {
1552 (void) fprintf(stderr
,
1553 gettext("No such property '%s'\n"),
1554 zfs_prop_to_name(pl
->pl_prop
));
1557 sourcetype
= ZPROP_SRC_NONE
;
1558 (void) strlcpy(buf
, "-", sizeof (buf
));
1561 if (received
&& (zfs_prop_get_recvd(zhp
,
1562 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1563 cbp
->cb_literal
) == 0))
1566 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1567 zfs_prop_to_name(pl
->pl_prop
),
1568 buf
, sourcetype
, source
, recvdval
);
1569 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1570 sourcetype
= ZPROP_SRC_LOCAL
;
1572 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1573 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1574 sourcetype
= ZPROP_SRC_NONE
;
1575 (void) strlcpy(buf
, "-", sizeof (buf
));
1578 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1579 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1580 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1581 sourcetype
= ZPROP_SRC_LOCAL
;
1583 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1584 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1585 sourcetype
= ZPROP_SRC_NONE
;
1586 (void) strlcpy(buf
, "-", sizeof (buf
));
1589 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1590 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1592 if (nvlist_lookup_nvlist(user_props
,
1593 pl
->pl_user_prop
, &propval
) != 0) {
1596 sourcetype
= ZPROP_SRC_NONE
;
1599 verify(nvlist_lookup_string(propval
,
1600 ZPROP_VALUE
, &strval
) == 0);
1601 verify(nvlist_lookup_string(propval
,
1602 ZPROP_SOURCE
, &sourceval
) == 0);
1604 if (strcmp(sourceval
,
1605 zfs_get_name(zhp
)) == 0) {
1606 sourcetype
= ZPROP_SRC_LOCAL
;
1607 } else if (strcmp(sourceval
,
1608 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1609 sourcetype
= ZPROP_SRC_RECEIVED
;
1611 sourcetype
= ZPROP_SRC_INHERITED
;
1612 (void) strlcpy(source
,
1613 sourceval
, sizeof (source
));
1617 if (received
&& (zfs_prop_get_recvd(zhp
,
1618 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1619 cbp
->cb_literal
) == 0))
1622 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1623 pl
->pl_user_prop
, strval
, sourcetype
,
1632 zfs_do_get(int argc
, char **argv
)
1634 zprop_get_cbdata_t cb
= { 0 };
1635 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1636 int types
= ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
;
1637 char *value
, *fields
;
1640 zprop_list_t fake_name
= { 0 };
1643 * Set up default columns and sources.
1645 cb
.cb_sources
= ZPROP_SRC_ALL
;
1646 cb
.cb_columns
[0] = GET_COL_NAME
;
1647 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1648 cb
.cb_columns
[2] = GET_COL_VALUE
;
1649 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1650 cb
.cb_type
= ZFS_TYPE_DATASET
;
1653 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1656 cb
.cb_literal
= B_TRUE
;
1659 limit
= parse_depth(optarg
, &flags
);
1662 flags
|= ZFS_ITER_RECURSE
;
1665 cb
.cb_scripted
= B_TRUE
;
1668 (void) fprintf(stderr
, gettext("missing argument for "
1669 "'%c' option\n"), optopt
);
1674 * Process the set of columns to display. We zero out
1675 * the structure to give us a blank slate.
1677 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1679 while (*optarg
!= '\0') {
1680 static char *col_subopts
[] =
1681 { "name", "property", "value", "received",
1682 "source", "all", NULL
};
1684 if (i
== ZFS_GET_NCOLS
) {
1685 (void) fprintf(stderr
, gettext("too "
1686 "many fields given to -o "
1691 switch (getsubopt(&optarg
, col_subopts
,
1694 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1697 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1700 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1703 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1704 flags
|= ZFS_ITER_RECVD_PROPS
;
1707 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1711 (void) fprintf(stderr
,
1712 gettext("\"all\" conflicts "
1713 "with specific fields "
1714 "given to -o option\n"));
1717 cb
.cb_columns
[0] = GET_COL_NAME
;
1718 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1719 cb
.cb_columns
[2] = GET_COL_VALUE
;
1720 cb
.cb_columns
[3] = GET_COL_RECVD
;
1721 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1722 flags
|= ZFS_ITER_RECVD_PROPS
;
1726 (void) fprintf(stderr
,
1727 gettext("invalid column name "
1736 while (*optarg
!= '\0') {
1737 static char *source_subopts
[] = {
1738 "local", "default", "inherited",
1739 "received", "temporary", "none",
1742 switch (getsubopt(&optarg
, source_subopts
,
1745 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1748 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1751 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1754 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1757 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1760 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1763 (void) fprintf(stderr
,
1764 gettext("invalid source "
1773 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1774 while (*optarg
!= '\0') {
1775 static char *type_subopts
[] = { "filesystem",
1776 "volume", "snapshot", "bookmark",
1779 switch (getsubopt(&optarg
, type_subopts
,
1782 types
|= ZFS_TYPE_FILESYSTEM
;
1785 types
|= ZFS_TYPE_VOLUME
;
1788 types
|= ZFS_TYPE_SNAPSHOT
;
1791 types
|= ZFS_TYPE_BOOKMARK
;
1794 types
= ZFS_TYPE_DATASET
|
1799 (void) fprintf(stderr
,
1800 gettext("invalid type '%s'\n"),
1808 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1818 (void) fprintf(stderr
, gettext("missing property "
1825 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1833 * As part of zfs_expand_proplist(), we keep track of the maximum column
1834 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1835 * need to know the maximum name length. However, the user likely did
1836 * not specify 'name' as one of the properties to fetch, so we need to
1837 * make sure we always include at least this property for
1838 * print_get_headers() to work properly.
1840 if (cb
.cb_proplist
!= NULL
) {
1841 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1842 fake_name
.pl_width
= strlen(gettext("NAME"));
1843 fake_name
.pl_next
= cb
.cb_proplist
;
1844 cb
.cb_proplist
= &fake_name
;
1847 cb
.cb_first
= B_TRUE
;
1849 /* run for each object */
1850 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1851 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1853 if (cb
.cb_proplist
== &fake_name
)
1854 zprop_free_list(fake_name
.pl_next
);
1856 zprop_free_list(cb
.cb_proplist
);
1862 * inherit [-rS] <property> <fs|vol> ...
1864 * -r Recurse over all children
1865 * -S Revert to received value, if any
1867 * For each dataset specified on the command line, inherit the given property
1868 * from its parent. Inheriting a property at the pool level will cause it to
1869 * use the default value. The '-r' flag will recurse over all children, and is
1870 * useful for setting a property on a hierarchy-wide basis, regardless of any
1871 * local modifications for each dataset.
1874 typedef struct inherit_cbdata
{
1875 const char *cb_propname
;
1876 boolean_t cb_received
;
1880 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1882 inherit_cbdata_t
*cb
= data
;
1883 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1886 * If we're doing it recursively, then ignore properties that
1887 * are not valid for this type of dataset.
1889 if (prop
!= ZPROP_INVAL
&&
1890 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
), B_FALSE
))
1893 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1897 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1899 inherit_cbdata_t
*cb
= data
;
1901 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1905 zfs_do_inherit(int argc
, char **argv
)
1909 inherit_cbdata_t cb
= { 0 };
1913 boolean_t received
= B_FALSE
;
1916 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1919 flags
|= ZFS_ITER_RECURSE
;
1926 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1935 /* check number of arguments */
1937 (void) fprintf(stderr
, gettext("missing property argument\n"));
1941 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1949 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1950 if (zfs_prop_readonly(prop
)) {
1951 (void) fprintf(stderr
, gettext(
1952 "%s property is read-only\n"),
1956 if (!zfs_prop_inheritable(prop
) && !received
) {
1957 (void) fprintf(stderr
, gettext("'%s' property cannot "
1958 "be inherited\n"), propname
);
1959 if (prop
== ZFS_PROP_QUOTA
||
1960 prop
== ZFS_PROP_RESERVATION
||
1961 prop
== ZFS_PROP_REFQUOTA
||
1962 prop
== ZFS_PROP_REFRESERVATION
) {
1963 (void) fprintf(stderr
, gettext("use 'zfs set "
1964 "%s=none' to clear\n"), propname
);
1965 (void) fprintf(stderr
, gettext("use 'zfs "
1966 "inherit -S %s' to revert to received "
1967 "value\n"), propname
);
1971 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1972 prop
== ZFS_PROP_VERSION
)) {
1973 (void) fprintf(stderr
, gettext("'%s' property cannot "
1974 "be reverted to a received value\n"), propname
);
1977 } else if (!zfs_prop_user(propname
)) {
1978 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1983 cb
.cb_propname
= propname
;
1984 cb
.cb_received
= received
;
1986 if (flags
& ZFS_ITER_RECURSE
) {
1987 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1988 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1990 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1991 NULL
, NULL
, 0, inherit_cb
, &cb
);
1997 typedef struct upgrade_cbdata
{
1998 uint64_t cb_numupgraded
;
1999 uint64_t cb_numsamegraded
;
2000 uint64_t cb_numfailed
;
2001 uint64_t cb_version
;
2003 boolean_t cb_foundone
;
2004 char cb_lastfs
[ZFS_MAX_DATASET_NAME_LEN
];
2008 same_pool(zfs_handle_t
*zhp
, const char *name
)
2010 int len1
= strcspn(name
, "/@");
2011 const char *zhname
= zfs_get_name(zhp
);
2012 int len2
= strcspn(zhname
, "/@");
2016 return (strncmp(name
, zhname
, len1
) == 0);
2020 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
2022 upgrade_cbdata_t
*cb
= data
;
2023 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2025 /* list if it's old/new */
2026 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
2027 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
2030 str
= gettext("The following filesystems are "
2031 "formatted using a newer software version and\n"
2032 "cannot be accessed on the current system.\n\n");
2034 str
= gettext("The following filesystems are "
2035 "out of date, and can be upgraded. After being\n"
2036 "upgraded, these filesystems (and any 'zfs send' "
2037 "streams generated from\n"
2038 "subsequent snapshots) will no longer be "
2039 "accessible by older software versions.\n\n");
2042 if (!cb
->cb_foundone
) {
2044 (void) printf(gettext("VER FILESYSTEM\n"));
2045 (void) printf(gettext("--- ------------\n"));
2046 cb
->cb_foundone
= B_TRUE
;
2049 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
2056 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2058 upgrade_cbdata_t
*cb
= data
;
2059 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2060 int needed_spa_version
;
2063 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2066 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2068 if (needed_spa_version
< 0)
2071 if (spa_version
< needed_spa_version
) {
2073 (void) printf(gettext("%s: can not be "
2074 "upgraded; the pool version needs to first "
2075 "be upgraded\nto version %d\n\n"),
2076 zfs_get_name(zhp
), needed_spa_version
);
2082 if (version
< cb
->cb_version
) {
2084 (void) snprintf(verstr
, sizeof (verstr
),
2085 "%llu", (u_longlong_t
)cb
->cb_version
);
2086 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2088 * If they did "zfs upgrade -a", then we could
2089 * be doing ioctls to different pools. We need
2090 * to log this history once to each pool, and bypass
2091 * the normal history logging that happens in main().
2093 (void) zpool_log_history(g_zfs
, history_str
);
2094 log_history
= B_FALSE
;
2096 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2097 cb
->cb_numupgraded
++;
2100 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2101 } else if (version
> cb
->cb_version
) {
2102 /* can't downgrade */
2103 (void) printf(gettext("%s: can not be downgraded; "
2104 "it is already at version %u\n"),
2105 zfs_get_name(zhp
), version
);
2108 cb
->cb_numsamegraded
++;
2116 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2119 zfs_do_upgrade(int argc
, char **argv
)
2121 boolean_t all
= B_FALSE
;
2122 boolean_t showversions
= B_FALSE
;
2124 upgrade_cbdata_t cb
= { 0 };
2126 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2129 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2132 flags
|= ZFS_ITER_RECURSE
;
2135 showversions
= B_TRUE
;
2138 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2139 optarg
, &cb
.cb_version
) != 0) {
2140 (void) fprintf(stderr
,
2141 gettext("invalid version %s\n"), optarg
);
2150 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2159 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2161 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2162 cb
.cb_version
|| argc
))
2164 if ((all
|| argc
) && (showversions
))
2170 /* Show info on available versions. */
2171 (void) printf(gettext("The following filesystem versions are "
2173 (void) printf(gettext("VER DESCRIPTION\n"));
2174 (void) printf("--- -----------------------------------------"
2175 "---------------\n");
2176 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2177 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2178 (void) printf(gettext(" 3 Case insensitive and filesystem "
2179 "user identifier (FUID)\n"));
2180 (void) printf(gettext(" 4 userquota, groupquota "
2182 (void) printf(gettext(" 5 System attributes\n"));
2183 (void) printf(gettext("\nFor more information on a particular "
2184 "version, including supported releases,\n"));
2185 (void) printf("see the ZFS Administration Guide.\n\n");
2187 } else if (argc
|| all
) {
2188 /* Upgrade filesystems */
2189 if (cb
.cb_version
== 0)
2190 cb
.cb_version
= ZPL_VERSION
;
2191 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2192 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2193 (void) printf(gettext("%llu filesystems upgraded\n"),
2194 (u_longlong_t
)cb
.cb_numupgraded
);
2195 if (cb
.cb_numsamegraded
) {
2196 (void) printf(gettext("%llu filesystems already at "
2198 (u_longlong_t
)cb
.cb_numsamegraded
);
2200 if (cb
.cb_numfailed
!= 0)
2203 /* List old-version filesystems */
2205 (void) printf(gettext("This system is currently running "
2206 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2208 flags
|= ZFS_ITER_RECURSE
;
2209 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2210 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2212 found
= cb
.cb_foundone
;
2213 cb
.cb_foundone
= B_FALSE
;
2214 cb
.cb_newer
= B_TRUE
;
2216 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2217 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2219 if (!cb
.cb_foundone
&& !found
) {
2220 (void) printf(gettext("All filesystems are "
2221 "formatted with the current version.\n"));
2229 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2230 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2231 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2232 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2234 * -H Scripted mode; elide headers and separate columns by tabs.
2235 * -i Translate SID to POSIX ID.
2236 * -n Print numeric ID instead of user/group name.
2237 * -o Control which fields to display.
2238 * -p Use exact (parsable) numeric output.
2239 * -s Specify sort columns, descending order.
2240 * -S Specify sort columns, ascending order.
2241 * -t Control which object types to display.
2243 * Displays space consumed by, and quotas on, each user in the specified
2244 * filesystem or snapshot.
2247 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2248 enum us_field_types
{
2256 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA",
2257 "OBJUSED", "OBJQUOTA" };
2258 static char *us_field_names
[] = { "type", "name", "used", "quota",
2259 "objused", "objquota" };
2260 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2262 #define USTYPE_PSX_GRP (1 << 0)
2263 #define USTYPE_PSX_USR (1 << 1)
2264 #define USTYPE_SMB_GRP (1 << 2)
2265 #define USTYPE_SMB_USR (1 << 3)
2266 #define USTYPE_ALL \
2267 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2269 static int us_type_bits
[] = {
2276 static char *us_type_names
[] = { "posixgroup", "posixuser", "smbgroup",
2279 typedef struct us_node
{
2281 uu_avl_node_t usn_avlnode
;
2282 uu_list_node_t usn_listnode
;
2285 typedef struct us_cbdata
{
2287 uu_avl_pool_t
*cb_avl_pool
;
2289 boolean_t cb_numname
;
2290 boolean_t cb_nicenum
;
2291 boolean_t cb_sid2posix
;
2292 zfs_userquota_prop_t cb_prop
;
2293 zfs_sort_column_t
*cb_sortcol
;
2294 size_t cb_width
[USFIELD_LAST
];
2297 static boolean_t us_populated
= B_FALSE
;
2300 zfs_sort_column_t
*si_sortcol
;
2301 boolean_t si_numname
;
2305 us_field_index(char *field
)
2309 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2310 if (strcmp(field
, us_field_names
[i
]) == 0)
2318 us_compare(const void *larg
, const void *rarg
, void *unused
)
2320 const us_node_t
*l
= larg
;
2321 const us_node_t
*r
= rarg
;
2322 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2323 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2324 boolean_t numname
= si
->si_numname
;
2325 nvlist_t
*lnvl
= l
->usn_nvl
;
2326 nvlist_t
*rnvl
= r
->usn_nvl
;
2330 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2337 zfs_prop_t prop
= sortcol
->sc_prop
;
2338 const char *propname
= NULL
;
2339 boolean_t reverse
= sortcol
->sc_reverse
;
2344 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2345 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2347 rc
= (rv32
< lv32
) ? 1 : -1;
2353 (void) nvlist_lookup_uint64(lnvl
, propname
,
2355 (void) nvlist_lookup_uint64(rnvl
, propname
,
2358 rc
= (rv64
< lv64
) ? 1 : -1;
2360 if ((nvlist_lookup_string(lnvl
, propname
,
2361 &lvstr
) == ENOENT
) ||
2362 (nvlist_lookup_string(rnvl
, propname
,
2363 &rvstr
) == ENOENT
)) {
2366 rc
= strcmp(lvstr
, rvstr
);
2370 case ZFS_PROP_QUOTA
:
2373 if (prop
== ZFS_PROP_USED
)
2377 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2378 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2380 rc
= (rv64
< lv64
) ? 1 : -1;
2389 return (reverse
? 1 : -1);
2391 return (reverse
? -1 : 1);
2396 * If entries still seem to be the same, check if they are of the same
2397 * type (smbentity is added only if we are doing SID to POSIX ID
2398 * translation where we can have duplicate type/name combinations).
2400 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2401 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2403 return (lvb
< rvb
? -1 : 1);
2409 zfs_prop_is_user(unsigned p
)
2411 return (p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
||
2412 p
== ZFS_PROP_USEROBJUSED
|| p
== ZFS_PROP_USEROBJQUOTA
);
2416 zfs_prop_is_group(unsigned p
)
2418 return (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
||
2419 p
== ZFS_PROP_GROUPOBJUSED
|| p
== ZFS_PROP_GROUPOBJQUOTA
);
2422 static inline const char *
2423 us_type2str(unsigned field_type
)
2425 switch (field_type
) {
2426 case USTYPE_PSX_USR
:
2427 return ("POSIX User");
2428 case USTYPE_PSX_GRP
:
2429 return ("POSIX Group");
2430 case USTYPE_SMB_USR
:
2431 return ("SMB User");
2432 case USTYPE_SMB_GRP
:
2433 return ("SMB Group");
2435 return ("Undefined");
2440 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2442 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2443 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2448 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2449 uu_avl_t
*avl
= cb
->cb_avl
;
2453 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2455 const char *typestr
;
2459 int typeidx
, nameidx
, sizeidx
;
2460 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2461 boolean_t smbentity
= B_FALSE
;
2463 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2465 node
= safe_malloc(sizeof (us_node_t
));
2466 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2467 node
->usn_nvl
= props
;
2469 if (domain
!= NULL
&& domain
[0] != '\0') {
2472 char sid
[MAXNAMELEN
+ 32];
2476 directory_error_t e
;
2480 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2482 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2483 type
= USTYPE_SMB_GRP
;
2484 err
= sid_to_id(sid
, B_FALSE
, &id
);
2486 type
= USTYPE_SMB_USR
;
2487 err
= sid_to_id(sid
, B_TRUE
, &id
);
2492 if (!cb
->cb_sid2posix
) {
2493 e
= directory_name_from_sid(NULL
, sid
, &name
,
2496 directory_error_free(e
);
2506 #endif /* HAVE_IDMAP */
2509 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2511 if (zfs_prop_is_group(prop
)) {
2512 type
= USTYPE_PSX_GRP
;
2513 if (!cb
->cb_numname
) {
2516 if ((g
= getgrgid(rid
)) != NULL
)
2520 type
= USTYPE_PSX_USR
;
2521 if (!cb
->cb_numname
) {
2524 if ((p
= getpwuid(rid
)) != NULL
)
2531 * Make sure that the type/name combination is unique when doing
2532 * SID to POSIX ID translation (hence changing the type from SMB to
2535 if (cb
->cb_sid2posix
&&
2536 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2539 /* Calculate/update width of TYPE field */
2540 typestr
= us_type2str(type
);
2541 typelen
= strlen(gettext(typestr
));
2542 typeidx
= us_field_index("type");
2543 if (typelen
> cb
->cb_width
[typeidx
])
2544 cb
->cb_width
[typeidx
] = typelen
;
2545 if (nvlist_add_uint32(props
, "type", type
) != 0)
2548 /* Calculate/update width of NAME field */
2549 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2550 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2552 namelen
= snprintf(NULL
, 0, "%u", rid
);
2554 if (nvlist_add_string(props
, "name", name
) != 0)
2556 namelen
= strlen(name
);
2558 nameidx
= us_field_index("name");
2559 if (nameidx
>= 0 && namelen
> cb
->cb_width
[nameidx
])
2560 cb
->cb_width
[nameidx
] = namelen
;
2563 * Check if this type/name combination is in the list and update it;
2564 * otherwise add new node to the list.
2566 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2567 uu_avl_insert(avl
, node
, idx
);
2572 props
= node
->usn_nvl
;
2575 /* Calculate/update width of USED/QUOTA fields */
2576 if (cb
->cb_nicenum
) {
2577 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
||
2578 prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2579 zfs_nicebytes(space
, sizebuf
, sizeof (sizebuf
));
2581 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2584 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu",
2585 (u_longlong_t
)space
);
2587 sizelen
= strlen(sizebuf
);
2588 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
) {
2590 if (!nvlist_exists(props
, "quota"))
2591 (void) nvlist_add_uint64(props
, "quota", 0);
2592 } else if (prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2594 if (!nvlist_exists(props
, "used"))
2595 (void) nvlist_add_uint64(props
, "used", 0);
2596 } else if (prop
== ZFS_PROP_USEROBJUSED
||
2597 prop
== ZFS_PROP_GROUPOBJUSED
) {
2598 propname
= "objused";
2599 if (!nvlist_exists(props
, "objquota"))
2600 (void) nvlist_add_uint64(props
, "objquota", 0);
2601 } else if (prop
== ZFS_PROP_USEROBJQUOTA
||
2602 prop
== ZFS_PROP_GROUPOBJQUOTA
) {
2603 propname
= "objquota";
2604 if (!nvlist_exists(props
, "objused"))
2605 (void) nvlist_add_uint64(props
, "objused", 0);
2609 sizeidx
= us_field_index(propname
);
2610 if (sizeidx
>= 0 && sizelen
> cb
->cb_width
[sizeidx
])
2611 cb
->cb_width
[sizeidx
] = sizelen
;
2613 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2620 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2621 size_t *width
, us_node_t
*node
)
2623 nvlist_t
*nvl
= node
->usn_nvl
;
2624 char valstr
[MAXNAMELEN
];
2625 boolean_t first
= B_TRUE
;
2631 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2632 if (!(ustype
& types
))
2635 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2636 nvpair_t
*nvp
= NULL
;
2642 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2643 if (strcmp(nvpair_name(nvp
),
2644 us_field_names
[field
]) == 0)
2648 type
= nvp
== NULL
? DATA_TYPE_UNKNOWN
: nvpair_type(nvp
);
2650 case DATA_TYPE_UINT32
:
2651 (void) nvpair_value_uint32(nvp
, &val32
);
2653 case DATA_TYPE_UINT64
:
2654 (void) nvpair_value_uint64(nvp
, &val64
);
2656 case DATA_TYPE_STRING
:
2657 (void) nvpair_value_string(nvp
, &strval
);
2659 case DATA_TYPE_UNKNOWN
:
2662 (void) fprintf(stderr
, "invalid data type\n");
2667 if (type
== DATA_TYPE_UINT32
)
2668 strval
= (char *)us_type2str(val32
);
2671 if (type
== DATA_TYPE_UINT64
) {
2672 (void) sprintf(valstr
, "%llu",
2673 (u_longlong_t
)val64
);
2679 if (type
== DATA_TYPE_UINT64
) {
2681 (void) sprintf(valstr
, "%llu",
2682 (u_longlong_t
)val64
);
2684 } else if (field
== USFIELD_QUOTA
&&
2688 zfs_nicebytes(val64
, valstr
,
2694 case USFIELD_OBJUSED
:
2695 case USFIELD_OBJQUOTA
:
2696 if (type
== DATA_TYPE_UINT64
) {
2698 (void) sprintf(valstr
, "%llu",
2699 (u_longlong_t
)val64
);
2701 } else if (field
== USFIELD_OBJQUOTA
&&
2705 zfs_nicenum(val64
, valstr
,
2715 (void) printf("\t");
2720 (void) printf("%s", strval
);
2721 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2722 (void) printf("%-*s", (int)width
[field
], strval
);
2724 (void) printf("%*s", (int)width
[field
], strval
);
2730 (void) printf("\n");
2734 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2735 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2743 boolean_t first
= B_TRUE
;
2745 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2746 col
= gettext(us_field_hdr
[field
]);
2747 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2748 (void) printf(first
? "%-*s" : " %-*s",
2749 (int)width
[field
], col
);
2751 (void) printf(first
? "%*s" : " %*s",
2752 (int)width
[field
], col
);
2757 (void) printf("\n");
2760 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2761 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2763 nvlist_free(node
->usn_nvl
);
2768 zfs_do_userspace(int argc
, char **argv
)
2771 zfs_userquota_prop_t p
;
2772 uu_avl_pool_t
*avl_pool
;
2774 uu_avl_walk_t
*walk
;
2776 char deffields
[] = "type,name,used,quota,objused,objquota";
2777 char *ofield
= NULL
;
2778 char *tfield
= NULL
;
2782 boolean_t scripted
= B_FALSE
;
2783 boolean_t prtnum
= B_FALSE
;
2784 boolean_t parsable
= B_FALSE
;
2785 boolean_t sid2posix
= B_FALSE
;
2788 zfs_sort_column_t
*sortcol
= NULL
;
2789 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2793 uu_list_pool_t
*listpool
;
2795 uu_avl_index_t idx
= 0;
2796 uu_list_index_t idx2
= 0;
2801 if (strcmp(argv
[0], "groupspace") == 0)
2802 /* Toggle default group types */
2803 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2805 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2821 if (zfs_add_sort_column(&sortcol
, optarg
,
2822 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
2823 (void) fprintf(stderr
,
2824 gettext("invalid field '%s'\n"), optarg
);
2835 (void) fprintf(stderr
, gettext("missing argument for "
2836 "'%c' option\n"), optopt
);
2840 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2850 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2854 (void) fprintf(stderr
, gettext("too many arguments\n"));
2858 /* Use default output fields if not specified using -o */
2862 if ((delim
= strchr(ofield
, ',')) != NULL
)
2864 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
2865 (void) fprintf(stderr
, gettext("invalid type '%s' "
2866 "for -o option\n"), ofield
);
2871 } while (delim
!= NULL
);
2872 fields
[cfield
] = USFIELD_LAST
;
2874 /* Override output types (-t option) */
2875 if (tfield
!= NULL
) {
2879 boolean_t found
= B_FALSE
;
2881 if ((delim
= strchr(tfield
, ',')) != NULL
)
2883 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
2885 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
2887 types
|= us_type_bits
[i
];
2892 (void) fprintf(stderr
, gettext("invalid type "
2893 "'%s' for -t option\n"), tfield
);
2898 } while (delim
!= NULL
);
2901 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
2904 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
2905 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
2907 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
2910 /* Always add default sorting columns */
2911 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
2912 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
2914 cb
.cb_sortcol
= sortcol
;
2915 cb
.cb_numname
= prtnum
;
2916 cb
.cb_nicenum
= !parsable
;
2917 cb
.cb_avl_pool
= avl_pool
;
2918 cb
.cb_avl
= avl_tree
;
2919 cb
.cb_sid2posix
= sid2posix
;
2921 for (i
= 0; i
< USFIELD_LAST
; i
++)
2922 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
2924 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
2925 if ((zfs_prop_is_user(p
) &&
2926 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
2927 (zfs_prop_is_group(p
) &&
2928 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))))
2932 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
2937 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
2940 us_populated
= B_TRUE
;
2942 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
2943 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
2944 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
2945 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
2947 while (node
!= NULL
) {
2949 node
= uu_avl_next(avl_tree
, node
);
2950 uu_avl_remove(avl_tree
, rmnode
);
2951 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
2952 uu_list_insert(list
, rmnode
, idx2
);
2955 for (node
= uu_list_first(list
); node
!= NULL
;
2956 node
= uu_list_next(list
, node
)) {
2957 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
2959 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
2960 uu_avl_insert(avl_tree
, node
, idx
);
2963 uu_list_destroy(list
);
2964 uu_list_pool_destroy(listpool
);
2966 /* Print and free node nvlist memory */
2967 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
2970 zfs_free_sort_columns(sortcol
);
2972 /* Clean up the AVL tree */
2973 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
2976 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
2977 uu_avl_remove(cb
.cb_avl
, node
);
2981 uu_avl_walk_end(walk
);
2982 uu_avl_destroy(avl_tree
);
2983 uu_avl_pool_destroy(avl_pool
);
2989 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
2990 * [-t type[,...]] [filesystem|volume|snapshot] ...
2992 * -H Scripted mode; elide headers and separate columns by tabs
2993 * -p Display values in parsable (literal) format.
2994 * -r Recurse over all children
2995 * -d Limit recursion by depth.
2996 * -o Control which fields to display.
2997 * -s Specify sort columns, descending order.
2998 * -S Specify sort columns, ascending order.
2999 * -t Control which object types to display.
3001 * When given no arguments, list all filesystems in the system.
3002 * Otherwise, list the specified datasets, optionally recursing down them if
3003 * '-r' is specified.
3005 typedef struct list_cbdata
{
3007 boolean_t cb_literal
;
3008 boolean_t cb_scripted
;
3009 zprop_list_t
*cb_proplist
;
3013 * Given a list of columns to display, output appropriate headers for each one.
3016 print_header(list_cbdata_t
*cb
)
3018 zprop_list_t
*pl
= cb
->cb_proplist
;
3019 char headerbuf
[ZFS_MAXPROPLEN
];
3022 boolean_t first
= B_TRUE
;
3023 boolean_t right_justify
;
3025 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3032 right_justify
= B_FALSE
;
3033 if (pl
->pl_prop
!= ZPROP_INVAL
) {
3034 header
= zfs_prop_column_name(pl
->pl_prop
);
3035 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3037 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
3038 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
3039 headerbuf
[i
] = '\0';
3043 if (pl
->pl_next
== NULL
&& !right_justify
)
3044 (void) printf("%s", header
);
3045 else if (right_justify
)
3046 (void) printf("%*s", (int)pl
->pl_width
, header
);
3048 (void) printf("%-*s", (int)pl
->pl_width
, header
);
3051 (void) printf("\n");
3055 * Given a dataset and a list of fields, print out all the properties according
3056 * to the described layout.
3059 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
3061 zprop_list_t
*pl
= cb
->cb_proplist
;
3062 boolean_t first
= B_TRUE
;
3063 char property
[ZFS_MAXPROPLEN
];
3064 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
3067 boolean_t right_justify
;
3069 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3071 if (cb
->cb_scripted
)
3072 (void) printf("\t");
3079 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
3080 (void) strlcpy(property
, zfs_get_name(zhp
),
3083 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3084 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
3085 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
3086 sizeof (property
), NULL
, NULL
, 0,
3087 cb
->cb_literal
) != 0)
3091 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3092 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
3093 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
3094 property
, sizeof (property
), cb
->cb_literal
) != 0)
3098 right_justify
= B_TRUE
;
3099 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
3100 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
3101 property
, sizeof (property
), cb
->cb_literal
) != 0)
3105 right_justify
= B_TRUE
;
3107 if (nvlist_lookup_nvlist(userprops
,
3108 pl
->pl_user_prop
, &propval
) != 0)
3111 verify(nvlist_lookup_string(propval
,
3112 ZPROP_VALUE
, &propstr
) == 0);
3113 right_justify
= B_FALSE
;
3117 * If this is being called in scripted mode, or if this is the
3118 * last column and it is left-justified, don't include a width
3121 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3122 (void) printf("%s", propstr
);
3123 else if (right_justify
)
3124 (void) printf("%*s", (int)pl
->pl_width
, propstr
);
3126 (void) printf("%-*s", (int)pl
->pl_width
, propstr
);
3129 (void) printf("\n");
3133 * Generic callback function to list a dataset or snapshot.
3136 list_callback(zfs_handle_t
*zhp
, void *data
)
3138 list_cbdata_t
*cbp
= data
;
3140 if (cbp
->cb_first
) {
3141 if (!cbp
->cb_scripted
)
3143 cbp
->cb_first
= B_FALSE
;
3146 print_dataset(zhp
, cbp
);
3152 zfs_do_list(int argc
, char **argv
)
3155 static char default_fields
[] =
3156 "name,used,available,referenced,mountpoint";
3157 int types
= ZFS_TYPE_DATASET
;
3158 boolean_t types_specified
= B_FALSE
;
3159 char *fields
= NULL
;
3160 list_cbdata_t cb
= { 0 };
3164 zfs_sort_column_t
*sortcol
= NULL
;
3165 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3168 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3174 cb
.cb_literal
= B_TRUE
;
3175 flags
|= ZFS_ITER_LITERAL_PROPS
;
3178 limit
= parse_depth(optarg
, &flags
);
3181 flags
|= ZFS_ITER_RECURSE
;
3184 cb
.cb_scripted
= B_TRUE
;
3187 if (zfs_add_sort_column(&sortcol
, optarg
,
3189 (void) fprintf(stderr
,
3190 gettext("invalid property '%s'\n"), optarg
);
3195 if (zfs_add_sort_column(&sortcol
, optarg
,
3197 (void) fprintf(stderr
,
3198 gettext("invalid property '%s'\n"), optarg
);
3204 types_specified
= B_TRUE
;
3205 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3206 while (*optarg
!= '\0') {
3207 static char *type_subopts
[] = { "filesystem",
3208 "volume", "snapshot", "snap", "bookmark",
3211 switch (getsubopt(&optarg
, type_subopts
,
3214 types
|= ZFS_TYPE_FILESYSTEM
;
3217 types
|= ZFS_TYPE_VOLUME
;
3221 types
|= ZFS_TYPE_SNAPSHOT
;
3224 types
|= ZFS_TYPE_BOOKMARK
;
3227 types
= ZFS_TYPE_DATASET
|
3231 (void) fprintf(stderr
,
3232 gettext("invalid type '%s'\n"),
3239 (void) fprintf(stderr
, gettext("missing argument for "
3240 "'%c' option\n"), optopt
);
3244 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3254 fields
= default_fields
;
3257 * If we are only going to list snapshot names and sort by name,
3258 * then we can use faster version.
3260 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3261 flags
|= ZFS_ITER_SIMPLE
;
3264 * If "-o space" and no types were specified, don't display snapshots.
3266 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3267 types
&= ~ZFS_TYPE_SNAPSHOT
;
3270 * If the user specifies '-o all', the zprop_get_list() doesn't
3271 * normally include the name of the dataset. For 'zfs list', we always
3272 * want this property to be first.
3274 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3278 cb
.cb_first
= B_TRUE
;
3280 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3281 limit
, list_callback
, &cb
);
3283 zprop_free_list(cb
.cb_proplist
);
3284 zfs_free_sort_columns(sortcol
);
3286 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3287 (void) fprintf(stderr
, gettext("no datasets available\n"));
3293 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3294 * zfs rename [-f] -p <fs | vol> <fs | vol>
3295 * zfs rename -r <snap> <snap>
3297 * Renames the given dataset to another of the same type.
3299 * The '-p' flag creates all the non-existing ancestors of the target first.
3303 zfs_do_rename(int argc
, char **argv
)
3308 boolean_t recurse
= B_FALSE
;
3309 boolean_t parents
= B_FALSE
;
3310 boolean_t force_unmount
= B_FALSE
;
3313 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3322 force_unmount
= B_TRUE
;
3326 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3335 /* check number of arguments */
3337 (void) fprintf(stderr
, gettext("missing source dataset "
3342 (void) fprintf(stderr
, gettext("missing target dataset "
3347 (void) fprintf(stderr
, gettext("too many arguments\n"));
3351 if (recurse
&& parents
) {
3352 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3357 if (recurse
&& strchr(argv
[0], '@') == 0) {
3358 (void) fprintf(stderr
, gettext("source dataset for recursive "
3359 "rename must be a snapshot\n"));
3363 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3364 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3367 /* If we were asked and the name looks good, try to create ancestors. */
3368 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3369 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3374 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3383 * Promotes the given clone fs to be the parent
3387 zfs_do_promote(int argc
, char **argv
)
3393 if (argc
> 1 && argv
[1][0] == '-') {
3394 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3399 /* check number of arguments */
3401 (void) fprintf(stderr
, gettext("missing clone filesystem"
3406 (void) fprintf(stderr
, gettext("too many arguments\n"));
3410 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3414 ret
= (zfs_promote(zhp
) != 0);
3422 * zfs rollback [-rRf] <snapshot>
3424 * -r Delete any intervening snapshots before doing rollback
3425 * -R Delete any snapshots and their clones
3426 * -f ignored for backwards compatibility
3428 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3429 * since then and making it the active dataset. If more recent snapshots exist,
3430 * the command will complain unless the '-r' flag is given.
3432 typedef struct rollback_cbdata
{
3438 boolean_t cb_recurse
;
3439 } rollback_cbdata_t
;
3442 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3444 rollback_cbdata_t
*cbp
= data
;
3446 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3447 (void) fprintf(stderr
, gettext("cannot rollback to "
3448 "'%s': clones of previous snapshots exist\n"),
3450 (void) fprintf(stderr
, gettext("use '-R' to "
3451 "force deletion of the following clones and "
3457 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3465 * Report any snapshots more recent than the one specified. Used when '-r' is
3466 * not specified. We reuse this same callback for the snapshot dependents - if
3467 * 'cb_dependent' is set, then this is a dependent and we should report it
3468 * without checking the transaction group.
3471 rollback_check(zfs_handle_t
*zhp
, void *data
)
3473 rollback_cbdata_t
*cbp
= data
;
3475 if (cbp
->cb_doclones
) {
3480 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3481 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3482 (void) fprintf(stderr
, gettext("cannot "
3483 "rollback to '%s': more recent snapshots "
3484 "or bookmarks exist\n"),
3486 (void) fprintf(stderr
, gettext("use '-r' to "
3487 "force deletion of the following "
3488 "snapshots and bookmarks:\n"));
3493 if (cbp
->cb_recurse
) {
3494 if (zfs_iter_dependents(zhp
, B_TRUE
,
3495 rollback_check_dependent
, cbp
) != 0) {
3500 (void) fprintf(stderr
, "%s\n",
3509 zfs_do_rollback(int argc
, char **argv
)
3513 boolean_t force
= B_FALSE
;
3514 rollback_cbdata_t cb
= { 0 };
3515 zfs_handle_t
*zhp
, *snap
;
3516 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3520 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3533 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3542 /* check number of arguments */
3544 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3548 (void) fprintf(stderr
, gettext("too many arguments\n"));
3552 /* open the snapshot */
3553 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3556 /* open the parent dataset */
3557 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3558 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3560 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3566 * Check for more recent snapshots and/or clones based on the presence
3569 cb
.cb_target
= argv
[0];
3570 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3571 cb
.cb_first
= B_TRUE
;
3573 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, rollback_check
, &cb
)) != 0)
3575 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
3578 if ((ret
= cb
.cb_error
) != 0)
3582 * Rollback parent to the given snapshot.
3584 ret
= zfs_rollback(zhp
, snap
, force
);
3597 * zfs set property=value ... { fs | snap | vol } ...
3599 * Sets the given properties for all datasets specified on the command line.
3603 set_callback(zfs_handle_t
*zhp
, void *data
)
3605 nvlist_t
*props
= data
;
3607 if (zfs_prop_set_list(zhp
, props
) != 0) {
3608 switch (libzfs_errno(g_zfs
)) {
3609 case EZFS_MOUNTFAILED
:
3610 (void) fprintf(stderr
, gettext("property may be set "
3611 "but unable to remount filesystem\n"));
3613 case EZFS_SHARENFSFAILED
:
3614 (void) fprintf(stderr
, gettext("property may be set "
3615 "but unable to reshare filesystem\n"));
3624 zfs_do_set(int argc
, char **argv
)
3626 nvlist_t
*props
= NULL
;
3627 int ds_start
= -1; /* argv idx of first dataset arg */
3631 /* check for options */
3632 if (argc
> 1 && argv
[1][0] == '-') {
3633 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3638 /* check number of arguments */
3640 (void) fprintf(stderr
, gettext("missing arguments\n"));
3644 if (strchr(argv
[1], '=') == NULL
) {
3645 (void) fprintf(stderr
, gettext("missing property=value "
3648 (void) fprintf(stderr
, gettext("missing dataset "
3654 /* validate argument order: prop=val args followed by dataset args */
3655 for (i
= 1; i
< argc
; i
++) {
3656 if (strchr(argv
[i
], '=') != NULL
) {
3658 /* out-of-order prop=val argument */
3659 (void) fprintf(stderr
, gettext("invalid "
3660 "argument order\n"));
3663 } else if (ds_start
< 0) {
3668 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
3672 /* Populate a list of property settings */
3673 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3675 for (i
= 1; i
< ds_start
; i
++) {
3676 if (!parseprop(props
, argv
[i
])) {
3682 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
3683 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, props
);
3690 typedef struct snap_cbdata
{
3692 boolean_t sd_recursive
;
3693 const char *sd_snapname
;
3697 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3699 snap_cbdata_t
*sd
= arg
;
3704 if (sd
->sd_recursive
&&
3705 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
3710 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3713 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3716 if (sd
->sd_recursive
)
3717 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3723 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3725 * Creates a snapshot with the given name. While functionally equivalent to
3726 * 'zfs create', it is a separate command to differentiate intent.
3729 zfs_do_snapshot(int argc
, char **argv
)
3734 snap_cbdata_t sd
= { 0 };
3735 boolean_t multiple_snaps
= B_FALSE
;
3737 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3739 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3743 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3746 if (!parseprop(props
, optarg
)) {
3747 nvlist_free(sd
.sd_nvl
);
3753 sd
.sd_recursive
= B_TRUE
;
3754 multiple_snaps
= B_TRUE
;
3757 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3766 /* check number of arguments */
3768 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3773 multiple_snaps
= B_TRUE
;
3774 for (; argc
> 0; argc
--, argv
++) {
3778 atp
= strchr(argv
[0], '@');
3782 sd
.sd_snapname
= atp
+ 1;
3783 zhp
= zfs_open(g_zfs
, argv
[0],
3784 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3787 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3791 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3792 nvlist_free(sd
.sd_nvl
);
3794 if (ret
!= 0 && multiple_snaps
)
3795 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3799 nvlist_free(sd
.sd_nvl
);
3806 * Send a backup stream to stdout.
3809 zfs_do_send(int argc
, char **argv
)
3811 char *fromname
= NULL
;
3812 char *toname
= NULL
;
3813 char *resume_token
= NULL
;
3816 sendflags_t flags
= { 0 };
3818 nvlist_t
*dbgnv
= NULL
;
3819 boolean_t extraverbose
= B_FALSE
;
3821 struct option long_options
[] = {
3822 {"replicate", no_argument
, NULL
, 'R'},
3823 {"props", no_argument
, NULL
, 'p'},
3824 {"parsable", no_argument
, NULL
, 'P'},
3825 {"dedup", no_argument
, NULL
, 'D'},
3826 {"verbose", no_argument
, NULL
, 'v'},
3827 {"dryrun", no_argument
, NULL
, 'n'},
3828 {"large-block", no_argument
, NULL
, 'L'},
3829 {"embed", no_argument
, NULL
, 'e'},
3830 {"resume", required_argument
, NULL
, 't'},
3831 {"compressed", no_argument
, NULL
, 'c'},
3836 while ((c
= getopt_long(argc
, argv
, ":i:I:RDpvnPLet:c", long_options
,
3848 flags
.doall
= B_TRUE
;
3851 flags
.replicate
= B_TRUE
;
3854 flags
.props
= B_TRUE
;
3857 flags
.parsable
= B_TRUE
;
3858 flags
.verbose
= B_TRUE
;
3862 extraverbose
= B_TRUE
;
3863 flags
.verbose
= B_TRUE
;
3864 flags
.progress
= B_TRUE
;
3867 flags
.dedup
= B_TRUE
;
3870 flags
.dryrun
= B_TRUE
;
3873 flags
.largeblock
= B_TRUE
;
3876 flags
.embed_data
= B_TRUE
;
3879 resume_token
= optarg
;
3882 flags
.compress
= B_TRUE
;
3886 * If a parameter was not passed, optopt contains the
3887 * value that would normally lead us into the
3888 * appropriate case statement. If it's > 256, then this
3889 * must be a longopt and we should look at argv to get
3890 * the string. Otherwise it's just the character, so we
3891 * should use it directly.
3893 if (optopt
<= UINT8_MAX
) {
3894 (void) fprintf(stderr
,
3895 gettext("missing argument for '%c' "
3896 "option\n"), optopt
);
3898 (void) fprintf(stderr
,
3899 gettext("missing argument for '%s' "
3900 "option\n"), argv
[optind
- 1]);
3908 * If an invalid flag was passed, optopt contains the
3909 * character if it was a short flag, or 0 if it was a
3913 (void) fprintf(stderr
,
3914 gettext("invalid option '%c'\n"), optopt
);
3916 (void) fprintf(stderr
,
3917 gettext("invalid option '%s'\n"),
3928 if (resume_token
!= NULL
) {
3929 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
3931 (void) fprintf(stderr
,
3932 gettext("invalid flags combined with -t\n"));
3936 (void) fprintf(stderr
, gettext("no additional "
3937 "arguments are permitted with -t\n"));
3942 (void) fprintf(stderr
,
3943 gettext("missing snapshot argument\n"));
3947 (void) fprintf(stderr
, gettext("too many arguments\n"));
3952 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
3953 (void) fprintf(stderr
,
3954 gettext("Error: Stream can not be written to a terminal.\n"
3955 "You must redirect standard output.\n"));
3959 if (resume_token
!= NULL
) {
3960 return (zfs_send_resume(g_zfs
, &flags
, STDOUT_FILENO
,
3965 * Special case sending a filesystem, or from a bookmark.
3967 if (strchr(argv
[0], '@') == NULL
||
3968 (fromname
&& strchr(fromname
, '#') != NULL
)) {
3969 char frombuf
[ZFS_MAX_DATASET_NAME_LEN
];
3970 enum lzc_send_flags lzc_flags
= 0;
3972 if (flags
.replicate
|| flags
.doall
|| flags
.props
||
3973 flags
.dedup
|| flags
.dryrun
|| flags
.verbose
||
3975 (void) fprintf(stderr
,
3977 "Unsupported flag with filesystem or bookmark.\n"));
3981 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
3985 if (flags
.largeblock
)
3986 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
3987 if (flags
.embed_data
)
3988 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
3990 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
3992 if (fromname
!= NULL
&&
3993 (fromname
[0] == '#' || fromname
[0] == '@')) {
3995 * Incremental source name begins with # or @.
3996 * Default to same fs as target.
3998 (void) strlcpy(frombuf
, argv
[0], sizeof (frombuf
));
3999 cp
= strchr(frombuf
, '@');
4002 (void) strlcat(frombuf
, fromname
, sizeof (frombuf
));
4005 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, lzc_flags
);
4010 cp
= strchr(argv
[0], '@');
4013 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4018 * If they specified the full path to the snapshot, chop off
4019 * everything except the short name of the snapshot, but special
4020 * case if they specify the origin.
4022 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
4023 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
4026 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
4027 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
4029 if (strcmp(origin
, fromname
) == 0) {
4031 flags
.fromorigin
= B_TRUE
;
4034 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
4035 (void) fprintf(stderr
,
4036 gettext("incremental source must be "
4037 "in same filesystem\n"));
4041 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
4042 (void) fprintf(stderr
,
4043 gettext("invalid incremental source\n"));
4049 if (flags
.replicate
&& fromname
== NULL
)
4050 flags
.doall
= B_TRUE
;
4052 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
4053 extraverbose
? &dbgnv
: NULL
);
4055 if (extraverbose
&& dbgnv
!= NULL
) {
4057 * dump_nvlist prints to stdout, but that's been
4058 * redirected to a file. Make it print to stderr
4061 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
4062 dump_nvlist(dbgnv
, 0);
4071 * Restore a backup stream from stdin.
4074 zfs_do_receive(int argc
, char **argv
)
4077 recvflags_t flags
= { 0 };
4078 boolean_t abort_resumable
= B_FALSE
;
4081 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
4085 while ((c
= getopt(argc
, argv
, ":o:x:denuvFsA")) != -1) {
4088 if (!parseprop(props
, optarg
)) {
4094 if (!parsepropname(props
, optarg
)) {
4100 flags
.isprefix
= B_TRUE
;
4103 flags
.isprefix
= B_TRUE
;
4104 flags
.istail
= B_TRUE
;
4107 flags
.dryrun
= B_TRUE
;
4110 flags
.nomount
= B_TRUE
;
4113 flags
.verbose
= B_TRUE
;
4116 flags
.resumable
= B_TRUE
;
4119 flags
.force
= B_TRUE
;
4122 abort_resumable
= B_TRUE
;
4125 (void) fprintf(stderr
, gettext("missing argument for "
4126 "'%c' option\n"), optopt
);
4130 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4139 /* check number of arguments */
4141 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4145 (void) fprintf(stderr
, gettext("too many arguments\n"));
4149 if (abort_resumable
) {
4150 if (flags
.isprefix
|| flags
.istail
|| flags
.dryrun
||
4151 flags
.resumable
|| flags
.nomount
) {
4152 (void) fprintf(stderr
, gettext("invalid option\n"));
4156 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
4157 (void) snprintf(namebuf
, sizeof (namebuf
),
4158 "%s/%%recv", argv
[0]);
4160 if (zfs_dataset_exists(g_zfs
, namebuf
,
4161 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) {
4162 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4163 namebuf
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4168 err
= zfs_destroy(zhp
, B_FALSE
);
4171 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4172 argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4175 if (!zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) ||
4176 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
4177 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == -1) {
4178 (void) fprintf(stderr
,
4179 gettext("'%s' does not have any "
4180 "resumable receive state to abort\n"),
4186 err
= zfs_destroy(zhp
, B_FALSE
);
4193 if (isatty(STDIN_FILENO
)) {
4194 (void) fprintf(stderr
,
4195 gettext("Error: Backup stream can not be read "
4196 "from a terminal.\n"
4197 "You must redirect standard input.\n"));
4201 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
4208 * allow/unallow stuff
4210 /* copied from zfs/sys/dsl_deleg.h */
4211 #define ZFS_DELEG_PERM_CREATE "create"
4212 #define ZFS_DELEG_PERM_DESTROY "destroy"
4213 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4214 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4215 #define ZFS_DELEG_PERM_CLONE "clone"
4216 #define ZFS_DELEG_PERM_PROMOTE "promote"
4217 #define ZFS_DELEG_PERM_RENAME "rename"
4218 #define ZFS_DELEG_PERM_MOUNT "mount"
4219 #define ZFS_DELEG_PERM_SHARE "share"
4220 #define ZFS_DELEG_PERM_SEND "send"
4221 #define ZFS_DELEG_PERM_RECEIVE "receive"
4222 #define ZFS_DELEG_PERM_ALLOW "allow"
4223 #define ZFS_DELEG_PERM_USERPROP "userprop"
4224 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4225 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4226 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4227 #define ZFS_DELEG_PERM_USERUSED "userused"
4228 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4229 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
4230 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
4231 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
4232 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
4234 #define ZFS_DELEG_PERM_HOLD "hold"
4235 #define ZFS_DELEG_PERM_RELEASE "release"
4236 #define ZFS_DELEG_PERM_DIFF "diff"
4237 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4239 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4241 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
4242 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
4243 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
4244 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
4245 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
4246 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
4247 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
4248 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
4249 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
4250 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
4251 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
4252 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
4253 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
4254 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
4255 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
4256 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
4257 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
4259 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
4260 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
4261 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
4262 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
4263 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
4264 { ZFS_DELEG_PERM_USEROBJQUOTA
, ZFS_DELEG_NOTE_USEROBJQUOTA
},
4265 { ZFS_DELEG_PERM_USEROBJUSED
, ZFS_DELEG_NOTE_USEROBJUSED
},
4266 { ZFS_DELEG_PERM_GROUPOBJQUOTA
, ZFS_DELEG_NOTE_GROUPOBJQUOTA
},
4267 { ZFS_DELEG_PERM_GROUPOBJUSED
, ZFS_DELEG_NOTE_GROUPOBJUSED
},
4268 { NULL
, ZFS_DELEG_NOTE_NONE
}
4271 /* permission structure */
4272 typedef struct deleg_perm
{
4273 zfs_deleg_who_type_t dp_who_type
;
4274 const char *dp_name
;
4276 boolean_t dp_descend
;
4280 typedef struct deleg_perm_node
{
4281 deleg_perm_t dpn_perm
;
4283 uu_avl_node_t dpn_avl_node
;
4284 } deleg_perm_node_t
;
4286 typedef struct fs_perm fs_perm_t
;
4288 /* permissions set */
4289 typedef struct who_perm
{
4290 zfs_deleg_who_type_t who_type
;
4291 const char *who_name
; /* id */
4292 char who_ug_name
[256]; /* user/group name */
4293 fs_perm_t
*who_fsperm
; /* uplink */
4295 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
4299 typedef struct who_perm_node
{
4300 who_perm_t who_perm
;
4301 uu_avl_node_t who_avl_node
;
4304 typedef struct fs_perm_set fs_perm_set_t
;
4305 /* fs permissions */
4307 const char *fsp_name
;
4309 uu_avl_t
*fsp_sc_avl
; /* sets,create */
4310 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
4312 fs_perm_set_t
*fsp_set
; /* uplink */
4316 typedef struct fs_perm_node
{
4317 fs_perm_t fspn_fsperm
;
4320 uu_list_node_t fspn_list_node
;
4323 /* top level structure */
4324 struct fs_perm_set
{
4325 uu_list_pool_t
*fsps_list_pool
;
4326 uu_list_t
*fsps_list
; /* list of fs_perms */
4328 uu_avl_pool_t
*fsps_named_set_avl_pool
;
4329 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
4330 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
4333 static inline const char *
4334 deleg_perm_type(zfs_deleg_note_t note
)
4340 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4341 case ZFS_DELEG_NOTE_GROUPUSED
:
4342 case ZFS_DELEG_NOTE_USERPROP
:
4343 case ZFS_DELEG_NOTE_USERQUOTA
:
4344 case ZFS_DELEG_NOTE_USERUSED
:
4345 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
4346 case ZFS_DELEG_NOTE_USEROBJUSED
:
4347 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
4348 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
4350 return (gettext("other"));
4352 return (gettext("subcommand"));
4357 who_type2weight(zfs_deleg_who_type_t who_type
)
4361 case ZFS_DELEG_NAMED_SET_SETS
:
4362 case ZFS_DELEG_NAMED_SET
:
4365 case ZFS_DELEG_CREATE_SETS
:
4366 case ZFS_DELEG_CREATE
:
4369 case ZFS_DELEG_USER_SETS
:
4370 case ZFS_DELEG_USER
:
4373 case ZFS_DELEG_GROUP_SETS
:
4374 case ZFS_DELEG_GROUP
:
4377 case ZFS_DELEG_EVERYONE_SETS
:
4378 case ZFS_DELEG_EVERYONE
:
4390 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4392 const who_perm_node_t
*l
= larg
;
4393 const who_perm_node_t
*r
= rarg
;
4394 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
4395 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
4396 int lweight
= who_type2weight(ltype
);
4397 int rweight
= who_type2weight(rtype
);
4398 int res
= lweight
- rweight
;
4400 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
4401 ZFS_MAX_DELEG_NAME
-1);
4413 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4415 const deleg_perm_node_t
*l
= larg
;
4416 const deleg_perm_node_t
*r
= rarg
;
4417 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
4418 ZFS_MAX_DELEG_NAME
-1);
4430 fs_perm_set_init(fs_perm_set_t
*fspset
)
4432 bzero(fspset
, sizeof (fs_perm_set_t
));
4434 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4435 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4436 NULL
, UU_DEFAULT
)) == NULL
)
4438 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4439 UU_DEFAULT
)) == NULL
)
4442 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4443 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4444 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4445 UU_DEFAULT
)) == NULL
)
4448 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4449 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4450 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4451 UU_DEFAULT
)) == NULL
)
4454 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4455 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4456 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4461 static inline void fs_perm_fini(fs_perm_t
*);
4462 static inline void who_perm_fini(who_perm_t
*);
4465 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4467 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4469 while (node
!= NULL
) {
4470 fs_perm_node_t
*next_node
=
4471 uu_list_next(fspset
->fsps_list
, node
);
4472 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4473 fs_perm_fini(fsperm
);
4474 uu_list_remove(fspset
->fsps_list
, node
);
4479 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4480 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4481 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4485 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4488 deleg_perm
->dp_who_type
= type
;
4489 deleg_perm
->dp_name
= name
;
4493 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4494 zfs_deleg_who_type_t type
, const char *name
)
4496 uu_avl_pool_t
*pool
;
4497 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4499 bzero(who_perm
, sizeof (who_perm_t
));
4501 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4502 UU_DEFAULT
)) == NULL
)
4505 who_perm
->who_type
= type
;
4506 who_perm
->who_name
= name
;
4507 who_perm
->who_fsperm
= fsperm
;
4511 who_perm_fini(who_perm_t
*who_perm
)
4513 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4515 while (node
!= NULL
) {
4516 deleg_perm_node_t
*next_node
=
4517 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4519 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4524 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4528 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4530 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4531 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4533 bzero(fsperm
, sizeof (fs_perm_t
));
4535 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4539 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4543 fsperm
->fsp_set
= fspset
;
4544 fsperm
->fsp_name
= fsname
;
4548 fs_perm_fini(fs_perm_t
*fsperm
)
4550 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4551 while (node
!= NULL
) {
4552 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4554 who_perm_t
*who_perm
= &node
->who_perm
;
4555 who_perm_fini(who_perm
);
4556 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4561 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4562 while (node
!= NULL
) {
4563 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4565 who_perm_t
*who_perm
= &node
->who_perm
;
4566 who_perm_fini(who_perm
);
4567 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4572 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4573 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4577 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4578 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4580 uu_avl_index_t idx
= 0;
4582 deleg_perm_node_t
*found_node
= NULL
;
4583 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4585 deleg_perm_init(deleg_perm
, who_type
, name
);
4587 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4589 uu_avl_insert(avl
, node
, idx
);
4592 deleg_perm
= &node
->dpn_perm
;
4597 case ZFS_DELEG_LOCAL
:
4598 deleg_perm
->dp_local
= B_TRUE
;
4600 case ZFS_DELEG_DESCENDENT
:
4601 deleg_perm
->dp_descend
= B_TRUE
;
4606 assert(B_FALSE
); /* invalid locality */
4611 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4613 nvpair_t
*nvp
= NULL
;
4614 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4615 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4616 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4618 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4619 const char *name
= nvpair_name(nvp
);
4620 data_type_t type
= nvpair_type(nvp
);
4621 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4622 deleg_perm_node_t
*node
=
4623 safe_malloc(sizeof (deleg_perm_node_t
));
4625 VERIFY(type
== DATA_TYPE_BOOLEAN
);
4627 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4628 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4635 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4637 nvpair_t
*nvp
= NULL
;
4638 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4640 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4641 nvlist_t
*nvl2
= NULL
;
4642 const char *name
= nvpair_name(nvp
);
4643 uu_avl_t
*avl
= NULL
;
4644 uu_avl_pool_t
*avl_pool
= NULL
;
4645 zfs_deleg_who_type_t perm_type
= name
[0];
4646 char perm_locality
= name
[1];
4647 const char *perm_name
= name
+ 3;
4648 boolean_t is_set
= B_TRUE
;
4649 who_perm_t
*who_perm
= NULL
;
4651 assert('$' == name
[2]);
4653 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4656 switch (perm_type
) {
4657 case ZFS_DELEG_CREATE
:
4658 case ZFS_DELEG_CREATE_SETS
:
4659 case ZFS_DELEG_NAMED_SET
:
4660 case ZFS_DELEG_NAMED_SET_SETS
:
4661 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4662 avl
= fsperm
->fsp_sc_avl
;
4664 case ZFS_DELEG_USER
:
4665 case ZFS_DELEG_USER_SETS
:
4666 case ZFS_DELEG_GROUP
:
4667 case ZFS_DELEG_GROUP_SETS
:
4668 case ZFS_DELEG_EVERYONE
:
4669 case ZFS_DELEG_EVERYONE_SETS
:
4670 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4671 avl
= fsperm
->fsp_uge_avl
;
4675 assert(!"unhandled zfs_deleg_who_type_t");
4679 who_perm_node_t
*found_node
= NULL
;
4680 who_perm_node_t
*node
= safe_malloc(
4681 sizeof (who_perm_node_t
));
4682 who_perm
= &node
->who_perm
;
4683 uu_avl_index_t idx
= 0;
4685 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4686 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4688 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4690 if (avl
== fsperm
->fsp_uge_avl
) {
4692 struct passwd
*p
= NULL
;
4693 struct group
*g
= NULL
;
4694 const char *nice_name
= NULL
;
4696 switch (perm_type
) {
4697 case ZFS_DELEG_USER_SETS
:
4698 case ZFS_DELEG_USER
:
4699 rid
= atoi(perm_name
);
4702 nice_name
= p
->pw_name
;
4704 case ZFS_DELEG_GROUP_SETS
:
4705 case ZFS_DELEG_GROUP
:
4706 rid
= atoi(perm_name
);
4709 nice_name
= g
->gr_name
;
4716 if (nice_name
!= NULL
)
4718 node
->who_perm
.who_ug_name
,
4722 uu_avl_insert(avl
, node
, idx
);
4725 who_perm
= &node
->who_perm
;
4728 VERIFY3P(who_perm
, !=, NULL
);
4729 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4736 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4738 nvpair_t
*nvp
= NULL
;
4739 uu_avl_index_t idx
= 0;
4741 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4742 nvlist_t
*nvl2
= NULL
;
4743 const char *fsname
= nvpair_name(nvp
);
4744 data_type_t type
= nvpair_type(nvp
);
4745 fs_perm_t
*fsperm
= NULL
;
4746 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4750 fsperm
= &node
->fspn_fsperm
;
4752 VERIFY(DATA_TYPE_NVLIST
== type
);
4754 uu_list_node_init(node
, &node
->fspn_list_node
,
4755 fspset
->fsps_list_pool
);
4757 idx
= uu_list_numnodes(fspset
->fsps_list
);
4758 fs_perm_init(fsperm
, fspset
, fsname
);
4760 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4763 (void) parse_fs_perm(fsperm
, nvl2
);
4765 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4771 static inline const char *
4772 deleg_perm_comment(zfs_deleg_note_t note
)
4774 const char *str
= "";
4779 case ZFS_DELEG_NOTE_ALLOW
:
4780 str
= gettext("Must also have the permission that is being"
4781 "\n\t\t\t\tallowed");
4783 case ZFS_DELEG_NOTE_CLONE
:
4784 str
= gettext("Must also have the 'create' ability and 'mount'"
4785 "\n\t\t\t\tability in the origin file system");
4787 case ZFS_DELEG_NOTE_CREATE
:
4788 str
= gettext("Must also have the 'mount' ability");
4790 case ZFS_DELEG_NOTE_DESTROY
:
4791 str
= gettext("Must also have the 'mount' ability");
4793 case ZFS_DELEG_NOTE_DIFF
:
4794 str
= gettext("Allows lookup of paths within a dataset;"
4795 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4796 "\n\t\t\t\tin order to use zfs diff");
4798 case ZFS_DELEG_NOTE_HOLD
:
4799 str
= gettext("Allows adding a user hold to a snapshot");
4801 case ZFS_DELEG_NOTE_MOUNT
:
4802 str
= gettext("Allows mount/umount of ZFS datasets");
4804 case ZFS_DELEG_NOTE_PROMOTE
:
4805 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4806 " 'promote' ability in the origin file system");
4808 case ZFS_DELEG_NOTE_RECEIVE
:
4809 str
= gettext("Must also have the 'mount' and 'create'"
4812 case ZFS_DELEG_NOTE_RELEASE
:
4813 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4814 "might destroy the snapshot");
4816 case ZFS_DELEG_NOTE_RENAME
:
4817 str
= gettext("Must also have the 'mount' and 'create'"
4818 "\n\t\t\t\tability in the new parent");
4820 case ZFS_DELEG_NOTE_ROLLBACK
:
4823 case ZFS_DELEG_NOTE_SEND
:
4826 case ZFS_DELEG_NOTE_SHARE
:
4827 str
= gettext("Allows sharing file systems over NFS or SMB"
4828 "\n\t\t\t\tprotocols");
4830 case ZFS_DELEG_NOTE_SNAPSHOT
:
4834 * case ZFS_DELEG_NOTE_VSCAN:
4835 * str = gettext("");
4839 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4840 str
= gettext("Allows accessing any groupquota@... property");
4842 case ZFS_DELEG_NOTE_GROUPUSED
:
4843 str
= gettext("Allows reading any groupused@... property");
4845 case ZFS_DELEG_NOTE_USERPROP
:
4846 str
= gettext("Allows changing any user property");
4848 case ZFS_DELEG_NOTE_USERQUOTA
:
4849 str
= gettext("Allows accessing any userquota@... property");
4851 case ZFS_DELEG_NOTE_USERUSED
:
4852 str
= gettext("Allows reading any userused@... property");
4854 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
4855 str
= gettext("Allows accessing any userobjquota@... property");
4857 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
4858 str
= gettext("Allows accessing any \n\t\t\t\t"
4859 "groupobjquota@... property");
4861 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
4862 str
= gettext("Allows reading any groupobjused@... property");
4864 case ZFS_DELEG_NOTE_USEROBJUSED
:
4865 str
= gettext("Allows reading any userobjused@... property");
4883 boolean_t recursive
; /* unallow only */
4884 boolean_t prt_usage
;
4886 boolean_t prt_perms
;
4889 const char *dataset
;
4893 prop_cmp(const void *a
, const void *b
)
4895 const char *str1
= *(const char **)a
;
4896 const char *str2
= *(const char **)b
;
4897 return (strcmp(str1
, str2
));
4901 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
4903 const char *opt_desc
[] = {
4904 "-h", gettext("show this help message and exit"),
4905 "-l", gettext("set permission locally"),
4906 "-d", gettext("set permission for descents"),
4907 "-u", gettext("set permission for user"),
4908 "-g", gettext("set permission for group"),
4909 "-e", gettext("set permission for everyone"),
4910 "-c", gettext("set create time permission"),
4911 "-s", gettext("define permission set"),
4913 "-r", gettext("remove permissions recursively"),
4915 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
4916 size_t allow_size
= unallow_size
- 2;
4917 const char *props
[ZFS_NUM_PROPS
];
4920 FILE *fp
= requested
? stdout
: stderr
;
4921 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
4922 const char *fmt
= gettext("%-16s %-14s\t%s\n");
4924 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
4926 (void) fprintf(fp
, gettext("Options:\n"));
4927 for (i
= 0; i
< (un
? unallow_size
: allow_size
); i
+= 2) {
4928 const char *opt
= opt_desc
[i
];
4929 const char *optdsc
= opt_desc
[i
+ 1];
4930 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
4933 (void) fprintf(fp
, gettext("\nThe following permissions are "
4935 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
4937 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
4938 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
4939 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
4940 const char *perm_type
= deleg_perm_type(perm_note
);
4941 const char *perm_comment
= deleg_perm_comment(perm_note
);
4942 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
4945 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
4946 zprop_desc_t
*pd
= &pdtbl
[i
];
4947 if (pd
->pd_visible
!= B_TRUE
)
4950 if (pd
->pd_attr
== PROP_READONLY
)
4953 props
[count
++] = pd
->pd_name
;
4955 props
[count
] = NULL
;
4957 qsort(props
, count
, sizeof (char *), prop_cmp
);
4959 for (i
= 0; i
< count
; i
++)
4960 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
4963 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
4965 exit(requested
? 0 : 2);
4968 static inline const char *
4969 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
4972 if (un
&& argc
== expected_argc
- 1)
4974 else if (argc
== expected_argc
)
4975 *permsp
= argv
[argc
- 2];
4977 allow_usage(un
, B_FALSE
,
4978 gettext("wrong number of parameters\n"));
4980 return (argv
[argc
- 1]);
4984 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
4986 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
4987 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
4988 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
4989 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
4992 allow_usage(un
, B_FALSE
,
4993 gettext("-u, -g, and -e are mutually exclusive\n"));
4995 if (opts
->prt_usage
) {
4996 if (argc
== 0 && all_sum
== 0)
4997 allow_usage(un
, B_TRUE
, NULL
);
5004 allow_usage(un
, B_FALSE
,
5005 gettext("invalid options combined with -s\n"));
5007 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
5008 if (argv
[0][0] != '@')
5009 allow_usage(un
, B_FALSE
,
5010 gettext("invalid set name: missing '@' prefix\n"));
5011 opts
->who
= argv
[0];
5012 } else if (opts
->create
) {
5013 if (ldcsuge_sum
> 1)
5014 allow_usage(un
, B_FALSE
,
5015 gettext("invalid options combined with -c\n"));
5016 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5017 } else if (opts
->everyone
) {
5019 allow_usage(un
, B_FALSE
,
5020 gettext("invalid options combined with -e\n"));
5021 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5022 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
5024 opts
->everyone
= B_TRUE
;
5027 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5028 } else if (argc
== 1 && !un
) {
5029 opts
->prt_perms
= B_TRUE
;
5030 opts
->dataset
= argv
[argc
-1];
5032 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
5033 opts
->who
= argv
[0];
5036 if (!opts
->local
&& !opts
->descend
) {
5037 opts
->local
= B_TRUE
;
5038 opts
->descend
= B_TRUE
;
5043 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
5044 const char *who
, char *perms
, nvlist_t
*top_nvl
)
5047 char ld
[2] = { '\0', '\0' };
5048 char who_buf
[MAXNAMELEN
+ 32];
5049 char base_type
= '\0';
5050 char set_type
= '\0';
5051 nvlist_t
*base_nvl
= NULL
;
5052 nvlist_t
*set_nvl
= NULL
;
5055 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
5057 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
5061 case ZFS_DELEG_NAMED_SET_SETS
:
5062 case ZFS_DELEG_NAMED_SET
:
5063 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
5064 base_type
= ZFS_DELEG_NAMED_SET
;
5065 ld
[0] = ZFS_DELEG_NA
;
5067 case ZFS_DELEG_CREATE_SETS
:
5068 case ZFS_DELEG_CREATE
:
5069 set_type
= ZFS_DELEG_CREATE_SETS
;
5070 base_type
= ZFS_DELEG_CREATE
;
5071 ld
[0] = ZFS_DELEG_NA
;
5073 case ZFS_DELEG_USER_SETS
:
5074 case ZFS_DELEG_USER
:
5075 set_type
= ZFS_DELEG_USER_SETS
;
5076 base_type
= ZFS_DELEG_USER
;
5078 ld
[0] = ZFS_DELEG_LOCAL
;
5080 ld
[1] = ZFS_DELEG_DESCENDENT
;
5082 case ZFS_DELEG_GROUP_SETS
:
5083 case ZFS_DELEG_GROUP
:
5084 set_type
= ZFS_DELEG_GROUP_SETS
;
5085 base_type
= ZFS_DELEG_GROUP
;
5087 ld
[0] = ZFS_DELEG_LOCAL
;
5089 ld
[1] = ZFS_DELEG_DESCENDENT
;
5091 case ZFS_DELEG_EVERYONE_SETS
:
5092 case ZFS_DELEG_EVERYONE
:
5093 set_type
= ZFS_DELEG_EVERYONE_SETS
;
5094 base_type
= ZFS_DELEG_EVERYONE
;
5096 ld
[0] = ZFS_DELEG_LOCAL
;
5098 ld
[1] = ZFS_DELEG_DESCENDENT
;
5102 assert(set_type
!= '\0' && base_type
!= '\0');
5105 if (perms
!= NULL
) {
5107 char *end
= curr
+ strlen(perms
);
5109 while (curr
< end
) {
5110 char *delim
= strchr(curr
, ',');
5121 (void) nvlist_add_boolean(nvl
, curr
);
5127 for (i
= 0; i
< 2; i
++) {
5128 char locality
= ld
[i
];
5132 if (!nvlist_empty(base_nvl
)) {
5134 (void) snprintf(who_buf
,
5135 sizeof (who_buf
), "%c%c$%s",
5136 base_type
, locality
, who
);
5138 (void) snprintf(who_buf
,
5139 sizeof (who_buf
), "%c%c$",
5140 base_type
, locality
);
5142 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5147 if (!nvlist_empty(set_nvl
)) {
5149 (void) snprintf(who_buf
,
5150 sizeof (who_buf
), "%c%c$%s",
5151 set_type
, locality
, who
);
5153 (void) snprintf(who_buf
,
5154 sizeof (who_buf
), "%c%c$",
5155 set_type
, locality
);
5157 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5162 for (i
= 0; i
< 2; i
++) {
5163 char locality
= ld
[i
];
5168 (void) snprintf(who_buf
, sizeof (who_buf
),
5169 "%c%c$%s", base_type
, locality
, who
);
5171 (void) snprintf(who_buf
, sizeof (who_buf
),
5172 "%c%c$", base_type
, locality
);
5173 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5176 (void) snprintf(who_buf
, sizeof (who_buf
),
5177 "%c%c$%s", set_type
, locality
, who
);
5179 (void) snprintf(who_buf
, sizeof (who_buf
),
5180 "%c%c$", set_type
, locality
);
5181 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5187 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
5189 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
5193 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
5194 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
5195 } else if (opts
->create
) {
5196 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
5197 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5198 } else if (opts
->everyone
) {
5199 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
5200 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5202 char *curr
= opts
->who
;
5203 char *end
= curr
+ strlen(curr
);
5205 while (curr
< end
) {
5207 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
5209 char *delim
= strchr(curr
, ',');
5212 struct passwd
*p
= NULL
;
5213 struct group
*g
= NULL
;
5221 rid
= (uid_t
)strtol(curr
, &endch
, 0);
5223 who_type
= ZFS_DELEG_USER
;
5232 (void) snprintf(errbuf
, 256, gettext(
5233 "invalid user %s"), curr
);
5234 allow_usage(un
, B_TRUE
, errbuf
);
5236 } else if (opts
->group
) {
5237 who_type
= ZFS_DELEG_GROUP
;
5246 (void) snprintf(errbuf
, 256, gettext(
5247 "invalid group %s"), curr
);
5248 allow_usage(un
, B_TRUE
, errbuf
);
5251 if (*endch
!= '\0') {
5258 if (*endch
!= '\0') {
5266 who_type
= ZFS_DELEG_USER
;
5268 } else if (g
!= NULL
) {
5269 who_type
= ZFS_DELEG_GROUP
;
5272 (void) snprintf(errbuf
, 256, gettext(
5273 "invalid user/group %s"), curr
);
5274 allow_usage(un
, B_TRUE
, errbuf
);
5278 (void) sprintf(id
, "%u", rid
);
5281 store_allow_perm(who_type
, opts
->local
,
5282 opts
->descend
, who
, opts
->perms
, *nvlp
);
5291 print_set_creat_perms(uu_avl_t
*who_avl
)
5293 const char *sc_title
[] = {
5294 gettext("Permission sets:\n"),
5295 gettext("Create time permissions:\n"),
5298 const char **title_ptr
= sc_title
;
5299 who_perm_node_t
*who_node
= NULL
;
5300 int prev_weight
= -1;
5302 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
5303 who_node
= uu_avl_next(who_avl
, who_node
)) {
5304 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5305 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5306 const char *who_name
= who_node
->who_perm
.who_name
;
5307 int weight
= who_type2weight(who_type
);
5308 boolean_t first
= B_TRUE
;
5309 deleg_perm_node_t
*deleg_node
;
5311 if (prev_weight
!= weight
) {
5312 (void) printf("%s", *title_ptr
++);
5313 prev_weight
= weight
;
5316 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
5317 (void) printf("\t");
5319 (void) printf("\t%s ", who_name
);
5321 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
5322 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5325 deleg_node
->dpn_perm
.dp_name
);
5328 (void) printf(",%s",
5329 deleg_node
->dpn_perm
.dp_name
);
5332 (void) printf("\n");
5337 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
5340 who_perm_node_t
*who_node
= NULL
;
5341 boolean_t prt_title
= B_TRUE
;
5342 uu_avl_walk_t
*walk
;
5344 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
5347 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
5348 const char *who_name
= who_node
->who_perm
.who_name
;
5349 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
5350 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5351 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5353 deleg_perm_node_t
*deleg_node
;
5354 boolean_t prt_who
= B_TRUE
;
5356 for (deleg_node
= uu_avl_first(avl
);
5358 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5359 if (local
!= deleg_node
->dpn_perm
.dp_local
||
5360 descend
!= deleg_node
->dpn_perm
.dp_descend
)
5364 const char *who
= NULL
;
5366 prt_title
= B_FALSE
;
5367 (void) printf("%s", title
);
5371 case ZFS_DELEG_USER_SETS
:
5372 case ZFS_DELEG_USER
:
5373 who
= gettext("user");
5375 who_name
= nice_who_name
;
5377 case ZFS_DELEG_GROUP_SETS
:
5378 case ZFS_DELEG_GROUP
:
5379 who
= gettext("group");
5381 who_name
= nice_who_name
;
5383 case ZFS_DELEG_EVERYONE_SETS
:
5384 case ZFS_DELEG_EVERYONE
:
5385 who
= gettext("everyone");
5390 assert(who
!= NULL
);
5394 if (who_name
== NULL
)
5395 (void) printf("\t%s", who
);
5397 (void) printf("\t%s %s", who
, who_name
);
5400 (void) printf("%c%s", delim
,
5401 deleg_node
->dpn_perm
.dp_name
);
5406 (void) printf("\n");
5409 uu_avl_walk_end(walk
);
5413 print_fs_perms(fs_perm_set_t
*fspset
)
5415 fs_perm_node_t
*node
= NULL
;
5416 char buf
[MAXNAMELEN
+ 32];
5417 const char *dsname
= buf
;
5419 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
5420 node
= uu_list_next(fspset
->fsps_list
, node
)) {
5421 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
5422 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
5425 (void) snprintf(buf
, sizeof (buf
),
5426 gettext("---- Permissions on %s "),
5427 node
->fspn_fsperm
.fsp_name
);
5428 (void) printf("%s", dsname
);
5429 left
= 70 - strlen(buf
);
5432 (void) printf("\n");
5434 print_set_creat_perms(sc_avl
);
5435 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
5436 gettext("Local permissions:\n"));
5437 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
5438 gettext("Descendent permissions:\n"));
5439 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
5440 gettext("Local+Descendent permissions:\n"));
5444 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5446 struct deleg_perms
{
5452 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5454 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5455 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5457 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5460 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5464 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5467 nvlist_t
*perm_nvl
= NULL
;
5468 nvlist_t
*update_perm_nvl
= NULL
;
5471 struct allow_opts opts
= { 0 };
5473 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5476 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5479 opts
.local
= B_TRUE
;
5482 opts
.descend
= B_TRUE
;
5488 opts
.group
= B_TRUE
;
5491 opts
.everyone
= B_TRUE
;
5497 opts
.create
= B_TRUE
;
5500 opts
.recursive
= B_TRUE
;
5503 (void) fprintf(stderr
, gettext("missing argument for "
5504 "'%c' option\n"), optopt
);
5508 opts
.prt_usage
= B_TRUE
;
5511 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5520 /* check arguments */
5521 parse_allow_args(argc
, argv
, un
, &opts
);
5523 /* try to open the dataset */
5524 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5525 ZFS_TYPE_VOLUME
)) == NULL
) {
5526 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5531 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5534 fs_perm_set_init(&fs_perm_set
);
5535 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5536 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5541 print_fs_perms(&fs_perm_set
);
5543 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5544 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5547 if (un
&& opts
.recursive
) {
5548 struct deleg_perms data
= { un
, update_perm_nvl
};
5549 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5558 nvlist_free(perm_nvl
);
5559 nvlist_free(update_perm_nvl
);
5561 fs_perm_set_fini(&fs_perm_set
);
5569 zfs_do_allow(int argc
, char **argv
)
5571 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5575 zfs_do_unallow(int argc
, char **argv
)
5577 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5581 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5586 boolean_t recursive
= B_FALSE
;
5587 const char *opts
= holding
? "rt" : "r";
5591 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5597 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5606 /* check number of arguments */
5614 if (holding
&& tag
[0] == '.') {
5615 /* tags starting with '.' are reserved for libzfs */
5616 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5620 for (i
= 0; i
< argc
; ++i
) {
5622 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
5624 char *path
= argv
[i
];
5626 delim
= strchr(path
, '@');
5627 if (delim
== NULL
) {
5628 (void) fprintf(stderr
,
5629 gettext("'%s' is not a snapshot\n"), path
);
5633 (void) strncpy(parent
, path
, delim
- path
);
5634 parent
[delim
- path
] = '\0';
5636 zhp
= zfs_open(g_zfs
, parent
,
5637 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5643 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
5646 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5652 return (errors
!= 0);
5656 * zfs hold [-r] [-t] <tag> <snap> ...
5658 * -r Recursively hold
5660 * Apply a user-hold with the given tag to the list of snapshots.
5663 zfs_do_hold(int argc
, char **argv
)
5665 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5669 * zfs release [-r] <tag> <snap> ...
5671 * -r Recursively release
5673 * Release a user-hold with the given tag from the list of snapshots.
5676 zfs_do_release(int argc
, char **argv
)
5678 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5681 typedef struct holds_cbdata
{
5682 boolean_t cb_recursive
;
5683 const char *cb_snapname
;
5685 size_t cb_max_namelen
;
5686 size_t cb_max_taglen
;
5689 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5690 #define DATETIME_BUF_LEN (32)
5695 print_holds(boolean_t scripted
, int nwidth
, int tagwidth
, nvlist_t
*nvl
)
5698 nvpair_t
*nvp
= NULL
;
5699 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5703 for (i
= 0; i
< 3; i
++) {
5704 col
= gettext(hdr_cols
[i
]);
5706 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5709 (void) printf("%s\n", col
);
5713 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5714 char *zname
= nvpair_name(nvp
);
5716 nvpair_t
*nvp2
= NULL
;
5717 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5718 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5719 char tsbuf
[DATETIME_BUF_LEN
];
5720 char *tagname
= nvpair_name(nvp2
);
5724 char sep
= scripted
? '\t' : ' ';
5725 int sepnum
= scripted
? 1 : 2;
5727 (void) nvpair_value_uint64(nvp2
, &val
);
5729 (void) localtime_r(&time
, &t
);
5730 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5731 gettext(STRFTIME_FMT_STR
), &t
);
5733 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth
, zname
,
5734 sepnum
, sep
, tagwidth
, tagname
, sepnum
, sep
, tsbuf
);
5740 * Generic callback function to list a dataset or snapshot.
5743 holds_callback(zfs_handle_t
*zhp
, void *data
)
5745 holds_cbdata_t
*cbp
= data
;
5746 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5747 nvlist_t
*nvl
= NULL
;
5748 nvpair_t
*nvp
= NULL
;
5749 const char *zname
= zfs_get_name(zhp
);
5750 size_t znamelen
= strlen(zname
);
5752 if (cbp
->cb_recursive
) {
5753 const char *snapname
;
5754 char *delim
= strchr(zname
, '@');
5758 snapname
= delim
+ 1;
5759 if (strcmp(cbp
->cb_snapname
, snapname
))
5763 if (zfs_get_holds(zhp
, &nvl
) != 0)
5766 if (znamelen
> cbp
->cb_max_namelen
)
5767 cbp
->cb_max_namelen
= znamelen
;
5769 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5770 const char *tag
= nvpair_name(nvp
);
5771 size_t taglen
= strlen(tag
);
5772 if (taglen
> cbp
->cb_max_taglen
)
5773 cbp
->cb_max_taglen
= taglen
;
5776 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5780 * zfs holds [-r] <snap> ...
5782 * -r Recursively hold
5785 zfs_do_holds(int argc
, char **argv
)
5790 boolean_t scripted
= B_FALSE
;
5791 boolean_t recursive
= B_FALSE
;
5792 const char *opts
= "rH";
5795 int types
= ZFS_TYPE_SNAPSHOT
;
5796 holds_cbdata_t cb
= { 0 };
5803 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5812 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5819 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
5820 flags
|= ZFS_ITER_RECURSE
;
5826 /* check number of arguments */
5830 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5833 for (i
= 0; i
< argc
; ++i
) {
5834 char *snapshot
= argv
[i
];
5836 const char *snapname
;
5838 delim
= strchr(snapshot
, '@');
5839 if (delim
== NULL
) {
5840 (void) fprintf(stderr
,
5841 gettext("'%s' is not a snapshot\n"), snapshot
);
5845 snapname
= delim
+ 1;
5847 snapshot
[delim
- snapshot
] = '\0';
5849 cb
.cb_recursive
= recursive
;
5850 cb
.cb_snapname
= snapname
;
5854 * 1. collect holds data, set format options
5856 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
5857 holds_callback
, &cb
);
5863 * 2. print holds data
5865 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
5867 if (nvlist_empty(nvl
))
5868 (void) fprintf(stderr
, gettext("no datasets available\n"));
5872 return (0 != errors
);
5875 #define CHECK_SPINNER 30
5876 #define SPINNER_TIME 3 /* seconds */
5877 #define MOUNT_TIME 5 /* seconds */
5880 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
5882 static char *spin
[] = { "-", "\\", "|", "/" };
5883 static int spinval
= 0;
5884 static int spincheck
= 0;
5885 static time_t last_spin_time
= (time_t)0;
5886 get_all_cb_t
*cbp
= data
;
5887 zfs_type_t type
= zfs_get_type(zhp
);
5889 if (cbp
->cb_verbose
) {
5890 if (--spincheck
< 0) {
5891 time_t now
= time(NULL
);
5892 if (last_spin_time
+ SPINNER_TIME
< now
) {
5893 update_progress(spin
[spinval
++ % 4]);
5894 last_spin_time
= now
;
5896 spincheck
= CHECK_SPINNER
;
5901 * Iterate over any nested datasets.
5903 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
5909 * Skip any datasets whose type does not match.
5911 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
5915 libzfs_add_handle(cbp
, zhp
);
5916 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
5922 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
5924 get_all_cb_t cb
= { 0 };
5925 cb
.cb_verbose
= verbose
;
5926 cb
.cb_getone
= get_one_dataset
;
5929 set_progress_header(gettext("Reading ZFS config"));
5930 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
5932 *dslist
= cb
.cb_handles
;
5933 *count
= cb
.cb_used
;
5936 finish_progress(gettext("done."));
5940 * Generic callback for sharing or mounting filesystems. Because the code is so
5941 * similar, we have a common function with an extra parameter to determine which
5942 * mode we are using.
5944 #define OP_SHARE 0x1
5945 #define OP_MOUNT 0x2
5948 * Share or mount a dataset.
5951 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
5952 boolean_t
explicit, const char *options
)
5954 char mountpoint
[ZFS_MAXPROPLEN
];
5955 char shareopts
[ZFS_MAXPROPLEN
];
5956 char smbshareopts
[ZFS_MAXPROPLEN
];
5957 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
5959 uint64_t zoned
, canmount
;
5960 boolean_t shared_nfs
, shared_smb
;
5962 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
5965 * Check to make sure we can mount/share this dataset. If we
5966 * are in the global zone and the filesystem is exported to a
5967 * local zone, or if we are in a local zone and the
5968 * filesystem is not exported, then it is an error.
5970 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
5972 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
5976 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5977 "dataset is exported to a local zone\n"), cmdname
,
5981 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
5985 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5986 "permission denied\n"), cmdname
,
5992 * Ignore any filesystems which don't apply to us. This
5993 * includes those with a legacy mountpoint, or those with
5994 * legacy share options.
5996 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
5997 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
5998 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
5999 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
6000 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
6001 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
6003 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
6004 strcmp(smbshareopts
, "off") == 0) {
6008 (void) fprintf(stderr
, gettext("cannot share '%s': "
6009 "legacy share\n"), zfs_get_name(zhp
));
6010 (void) fprintf(stderr
, gettext("use share(1M) to "
6011 "share this filesystem, or set "
6012 "sharenfs property on\n"));
6017 * We cannot share or mount legacy filesystems. If the
6018 * shareopts is non-legacy but the mountpoint is legacy, we
6019 * treat it as a legacy share.
6021 if (strcmp(mountpoint
, "legacy") == 0) {
6025 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6026 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
6027 (void) fprintf(stderr
, gettext("use %s(1M) to "
6028 "%s this filesystem\n"), cmdname
, cmdname
);
6032 if (strcmp(mountpoint
, "none") == 0) {
6036 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
6037 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
6042 * canmount explicit outcome
6043 * on no pass through
6044 * on yes pass through
6046 * off yes display error, return 1
6047 * noauto no return 0
6048 * noauto yes pass through
6050 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
6051 if (canmount
== ZFS_CANMOUNT_OFF
) {
6055 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6056 "'canmount' property is set to 'off'\n"), cmdname
,
6059 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
6064 * If this filesystem is inconsistent and has a receive resume
6065 * token, we can not mount it.
6067 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
6068 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
6069 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
6073 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6074 "Contains partially-completed state from "
6075 "\"zfs receive -s\", which can be resumed with "
6076 "\"zfs send -t\"\n"),
6077 cmdname
, zfs_get_name(zhp
));
6082 * At this point, we have verified that the mountpoint and/or
6083 * shareopts are appropriate for auto management. If the
6084 * filesystem is already mounted or shared, return (failing
6085 * for explicit requests); otherwise mount or share the
6091 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
6092 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
6094 if ((shared_nfs
&& shared_smb
) ||
6095 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
6096 strcmp(smbshareopts
, "off") == 0) ||
6097 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
6098 strcmp(shareopts
, "off") == 0)) {
6102 (void) fprintf(stderr
, gettext("cannot share "
6103 "'%s': filesystem already shared\n"),
6108 if (!zfs_is_mounted(zhp
, NULL
) &&
6109 zfs_mount(zhp
, NULL
, 0) != 0)
6112 if (protocol
== NULL
) {
6113 if (zfs_shareall(zhp
) != 0)
6115 } else if (strcmp(protocol
, "nfs") == 0) {
6116 if (zfs_share_nfs(zhp
))
6118 } else if (strcmp(protocol
, "smb") == 0) {
6119 if (zfs_share_smb(zhp
))
6122 (void) fprintf(stderr
, gettext("cannot share "
6123 "'%s': invalid share type '%s' "
6125 zfs_get_name(zhp
), protocol
);
6132 if (options
== NULL
)
6133 mnt
.mnt_mntopts
= "";
6135 mnt
.mnt_mntopts
= (char *)options
;
6137 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
6138 zfs_is_mounted(zhp
, NULL
)) {
6142 (void) fprintf(stderr
, gettext("cannot mount "
6143 "'%s': filesystem already mounted\n"),
6148 if (zfs_mount(zhp
, options
, flags
) != 0)
6157 * Reports progress in the form "(current/total)". Not thread-safe.
6160 report_mount_progress(int current
, int total
)
6162 static time_t last_progress_time
= 0;
6163 time_t now
= time(NULL
);
6166 /* report 1..n instead of 0..n-1 */
6169 /* display header if we're here for the first time */
6171 set_progress_header(gettext("Mounting ZFS filesystems"));
6172 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
6173 /* too soon to report again */
6177 last_progress_time
= now
;
6179 (void) sprintf(info
, "(%d/%d)", current
, total
);
6181 if (current
== total
)
6182 finish_progress(info
);
6184 update_progress(info
);
6188 append_options(char *mntopts
, char *newopts
)
6190 int len
= strlen(mntopts
);
6192 /* original length plus new string to append plus 1 for the comma */
6193 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
6194 (void) fprintf(stderr
, gettext("the opts argument for "
6195 "'%s' option is too long (more than %d chars)\n"),
6196 "-o", MNT_LINE_MAX
);
6201 mntopts
[len
++] = ',';
6203 (void) strcpy(&mntopts
[len
], newopts
);
6207 share_mount(int op
, int argc
, char **argv
)
6210 boolean_t verbose
= B_FALSE
;
6212 char *options
= NULL
;
6216 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
6226 if (*optarg
== '\0') {
6227 (void) fprintf(stderr
, gettext("empty mount "
6228 "options (-o) specified\n"));
6232 if (options
== NULL
)
6233 options
= safe_malloc(MNT_LINE_MAX
+ 1);
6235 /* option validation is done later */
6236 append_options(options
, optarg
);
6239 flags
|= MS_OVERLAY
;
6242 (void) fprintf(stderr
, gettext("missing argument for "
6243 "'%c' option\n"), optopt
);
6247 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6256 /* check number of arguments */
6258 zfs_handle_t
**dslist
= NULL
;
6259 size_t i
, count
= 0;
6260 char *protocol
= NULL
;
6262 if (op
== OP_SHARE
&& argc
> 0) {
6263 if (strcmp(argv
[0], "nfs") != 0 &&
6264 strcmp(argv
[0], "smb") != 0) {
6265 (void) fprintf(stderr
, gettext("share type "
6266 "must be 'nfs' or 'smb'\n"));
6275 (void) fprintf(stderr
, gettext("too many arguments\n"));
6279 start_progress_timer();
6280 get_all_datasets(&dslist
, &count
, verbose
);
6283 if (options
!= NULL
)
6288 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
6290 for (i
= 0; i
< count
; i
++) {
6292 report_mount_progress(i
, count
);
6294 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
6295 B_FALSE
, options
) != 0)
6297 zfs_close(dslist
[i
]);
6301 } else if (argc
== 0) {
6302 struct mnttab entry
;
6304 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
6305 (void) fprintf(stderr
, gettext("missing filesystem "
6306 "argument (specify -a for all)\n"));
6311 * When mount is given no arguments, go through
6312 * /proc/self/mounts and display any active ZFS mounts.
6313 * We hide any snapshots, since they are controlled
6317 /* Reopen MNTTAB to prevent reading stale data from open file */
6318 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
) {
6319 if (options
!= NULL
)
6324 while (getmntent(mnttab_file
, &entry
) == 0) {
6325 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
6326 strchr(entry
.mnt_special
, '@') != NULL
)
6329 (void) printf("%-30s %s\n", entry
.mnt_special
,
6337 (void) fprintf(stderr
,
6338 gettext("too many arguments\n"));
6342 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6343 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6346 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
6352 if (options
!= NULL
)
6359 * zfs mount -a [nfs]
6360 * zfs mount filesystem
6362 * Mount all filesystems, or mount the given filesystem.
6365 zfs_do_mount(int argc
, char **argv
)
6367 return (share_mount(OP_MOUNT
, argc
, argv
));
6371 * zfs share -a [nfs | smb]
6372 * zfs share filesystem
6374 * Share all filesystems, or share the given filesystem.
6377 zfs_do_share(int argc
, char **argv
)
6379 return (share_mount(OP_SHARE
, argc
, argv
));
6382 typedef struct unshare_unmount_node
{
6383 zfs_handle_t
*un_zhp
;
6385 uu_avl_node_t un_avlnode
;
6386 } unshare_unmount_node_t
;
6390 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
6392 const unshare_unmount_node_t
*l
= larg
;
6393 const unshare_unmount_node_t
*r
= rarg
;
6395 return (strcmp(l
->un_mountp
, r
->un_mountp
));
6399 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6400 * absolute path, find the entry /proc/self/mounts, verify that its a
6401 * ZFS filesystems, and unmount it appropriately.
6404 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
6408 struct stat64 statbuf
;
6409 struct extmnttab entry
;
6410 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
6414 * Search for the path in /proc/self/mounts. Rather than looking for the
6415 * specific path, which can be fooled by non-standard paths (i.e. ".."
6416 * or "//"), we stat() the path and search for the corresponding
6417 * (major,minor) device pair.
6419 if (stat64(path
, &statbuf
) != 0) {
6420 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6421 cmdname
, path
, strerror(errno
));
6424 path_inode
= statbuf
.st_ino
;
6427 * Search for the given (major,minor) pair in the mount table.
6430 /* Reopen MNTTAB to prevent reading stale data from open file */
6431 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6434 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
6435 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
6436 entry
.mnt_minor
== minor(statbuf
.st_dev
))
6440 if (op
== OP_SHARE
) {
6441 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
6442 "currently mounted\n"), cmdname
, path
);
6445 (void) fprintf(stderr
, gettext("warning: %s not in"
6446 "/proc/self/mounts\n"), path
);
6447 if ((ret
= umount2(path
, flags
)) != 0)
6448 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
6453 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
6454 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
6455 "filesystem\n"), cmdname
, path
);
6459 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6460 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6464 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
6465 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6466 cmdname
, path
, strerror(errno
));
6468 } else if (statbuf
.st_ino
!= path_inode
) {
6469 (void) fprintf(stderr
, gettext("cannot "
6470 "%s '%s': not a mountpoint\n"), cmdname
, path
);
6474 if (op
== OP_SHARE
) {
6475 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6476 char smbshare_prop
[ZFS_MAXPROPLEN
];
6478 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
6479 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6480 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
6481 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6483 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6484 strcmp(smbshare_prop
, "off") == 0) {
6485 (void) fprintf(stderr
, gettext("cannot unshare "
6486 "'%s': legacy share\n"), path
);
6487 (void) fprintf(stderr
, gettext("use exportfs(8) "
6488 "or smbcontrol(1) to unshare this filesystem\n"));
6489 } else if (!zfs_is_shared(zhp
)) {
6490 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6491 "not currently shared\n"), path
);
6493 ret
= zfs_unshareall_bypath(zhp
, path
);
6496 char mtpt_prop
[ZFS_MAXPROPLEN
];
6498 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6499 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6502 ret
= zfs_unmount(zhp
, NULL
, flags
);
6503 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6504 (void) fprintf(stderr
, gettext("cannot unmount "
6505 "'%s': legacy mountpoint\n"),
6507 (void) fprintf(stderr
, gettext("use umount(8) "
6508 "to unmount this filesystem\n"));
6510 ret
= zfs_unmountall(zhp
, flags
);
6521 * Generic callback for unsharing or unmounting a filesystem.
6524 unshare_unmount(int op
, int argc
, char **argv
)
6531 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6532 char sharesmb
[ZFS_MAXPROPLEN
];
6535 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? ":a" : "af")) != -1) {
6544 (void) fprintf(stderr
, gettext("missing argument for "
6545 "'%c' option\n"), optopt
);
6549 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6560 * We could make use of zfs_for_each() to walk all datasets in
6561 * the system, but this would be very inefficient, especially
6562 * since we would have to linearly search /proc/self/mounts for
6563 * each one. Instead, do one pass through /proc/self/mounts
6564 * looking for zfs entries and call zfs_unmount() for each one.
6566 * Things get a little tricky if the administrator has created
6567 * mountpoints beneath other ZFS filesystems. In this case, we
6568 * have to unmount the deepest filesystems first. To accomplish
6569 * this, we place all the mountpoints in an AVL tree sorted by
6570 * the special type (dataset name), and walk the result in
6571 * reverse to make sure to get any snapshots first.
6573 struct mnttab entry
;
6574 uu_avl_pool_t
*pool
;
6575 uu_avl_t
*tree
= NULL
;
6576 unshare_unmount_node_t
*node
;
6578 uu_avl_walk_t
*walk
;
6579 char *protocol
= NULL
;
6581 if (op
== OP_SHARE
&& argc
> 0) {
6582 if (strcmp(argv
[0], "nfs") != 0 &&
6583 strcmp(argv
[0], "smb") != 0) {
6584 (void) fprintf(stderr
, gettext("share type "
6585 "must be 'nfs' or 'smb'\n"));
6594 (void) fprintf(stderr
, gettext("too many arguments\n"));
6598 if (((pool
= uu_avl_pool_create("unmount_pool",
6599 sizeof (unshare_unmount_node_t
),
6600 offsetof(unshare_unmount_node_t
, un_avlnode
),
6601 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6602 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6605 /* Reopen MNTTAB to prevent reading stale data from open file */
6606 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6609 while (getmntent(mnttab_file
, &entry
) == 0) {
6611 /* ignore non-ZFS entries */
6612 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6615 /* ignore snapshots */
6616 if (strchr(entry
.mnt_special
, '@') != NULL
)
6619 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6620 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6626 * Ignore datasets that are excluded/restricted by
6629 if (zpool_skip_pool(zfs_get_pool_name(zhp
))) {
6636 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6638 sizeof (nfs_mnt_prop
),
6639 NULL
, NULL
, 0, B_FALSE
) == 0);
6640 if (strcmp(nfs_mnt_prop
, "off") != 0)
6642 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6644 sizeof (nfs_mnt_prop
),
6645 NULL
, NULL
, 0, B_FALSE
) == 0);
6646 if (strcmp(nfs_mnt_prop
, "off") == 0)
6650 /* Ignore legacy mounts */
6651 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6653 sizeof (nfs_mnt_prop
),
6654 NULL
, NULL
, 0, B_FALSE
) == 0);
6655 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6657 /* Ignore canmount=noauto mounts */
6658 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6659 ZFS_CANMOUNT_NOAUTO
)
6665 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6667 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6669 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6671 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6672 uu_avl_insert(tree
, node
, idx
);
6674 zfs_close(node
->un_zhp
);
6675 free(node
->un_mountp
);
6681 * Walk the AVL tree in reverse, unmounting each filesystem and
6682 * removing it from the AVL tree in the process.
6684 if ((walk
= uu_avl_walk_start(tree
,
6685 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6688 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6689 uu_avl_remove(tree
, node
);
6693 if (zfs_unshareall_bytype(node
->un_zhp
,
6694 node
->un_mountp
, protocol
) != 0)
6699 if (zfs_unmount(node
->un_zhp
,
6700 node
->un_mountp
, flags
) != 0)
6705 zfs_close(node
->un_zhp
);
6706 free(node
->un_mountp
);
6710 uu_avl_walk_end(walk
);
6711 uu_avl_destroy(tree
);
6712 uu_avl_pool_destroy(pool
);
6717 (void) fprintf(stderr
,
6718 gettext("missing filesystem argument\n"));
6720 (void) fprintf(stderr
,
6721 gettext("too many arguments\n"));
6726 * We have an argument, but it may be a full path or a ZFS
6727 * filesystem. Pass full paths off to unmount_path() (shared by
6728 * manual_unmount), otherwise open the filesystem and pass to
6731 if (argv
[0][0] == '/')
6732 return (unshare_unmount_path(op
, argv
[0],
6735 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6736 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6739 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
6740 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
6741 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
6742 NULL
, 0, B_FALSE
) == 0);
6746 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6748 sizeof (nfs_mnt_prop
),
6749 NULL
, NULL
, 0, B_FALSE
) == 0);
6750 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6751 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
6754 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6755 strcmp(sharesmb
, "off") == 0) {
6756 (void) fprintf(stderr
, gettext("cannot "
6757 "unshare '%s': legacy share\n"),
6759 (void) fprintf(stderr
, gettext("use "
6760 "unshare(1M) to unshare this "
6763 } else if (!zfs_is_shared(zhp
)) {
6764 (void) fprintf(stderr
, gettext("cannot "
6765 "unshare '%s': not currently "
6766 "shared\n"), zfs_get_name(zhp
));
6768 } else if (zfs_unshareall(zhp
) != 0) {
6774 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
6775 (void) fprintf(stderr
, gettext("cannot "
6776 "unmount '%s': legacy "
6777 "mountpoint\n"), zfs_get_name(zhp
));
6778 (void) fprintf(stderr
, gettext("use "
6779 "umount(1M) to unmount this "
6782 } else if (!zfs_is_mounted(zhp
, NULL
)) {
6783 (void) fprintf(stderr
, gettext("cannot "
6784 "unmount '%s': not currently "
6788 } else if (zfs_unmountall(zhp
, flags
) != 0) {
6802 * zfs unmount filesystem
6804 * Unmount all filesystems, or a specific ZFS filesystem.
6807 zfs_do_unmount(int argc
, char **argv
)
6809 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
6814 * zfs unshare filesystem
6816 * Unshare all filesystems, or a specific ZFS filesystem.
6819 zfs_do_unshare(int argc
, char **argv
)
6821 return (unshare_unmount(OP_SHARE
, argc
, argv
));
6825 find_command_idx(char *command
, int *idx
)
6829 for (i
= 0; i
< NCOMMAND
; i
++) {
6830 if (command_table
[i
].name
== NULL
)
6833 if (strcmp(command
, command_table
[i
].name
) == 0) {
6842 zfs_do_diff(int argc
, char **argv
)
6846 char *tosnap
= NULL
;
6847 char *fromsnap
= NULL
;
6852 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
6855 flags
|= ZFS_DIFF_CLASSIFY
;
6858 flags
|= ZFS_DIFF_PARSEABLE
;
6861 flags
|= ZFS_DIFF_TIMESTAMP
;
6864 (void) fprintf(stderr
,
6865 gettext("invalid option '%c'\n"), optopt
);
6874 (void) fprintf(stderr
,
6875 gettext("must provide at least one snapshot name\n"));
6880 (void) fprintf(stderr
, gettext("too many arguments\n"));
6885 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
6888 if (*fromsnap
!= '@')
6889 copy
= strdup(fromsnap
);
6891 copy
= strdup(tosnap
);
6895 if ((atp
= strchr(copy
, '@')) != NULL
)
6898 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6905 * Ignore SIGPIPE so that the library can give us
6906 * information on any failure
6908 (void) sigignore(SIGPIPE
);
6910 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
6918 * zfs bookmark <fs@snap> <fs#bmark>
6920 * Creates a bookmark with the given name from the given snapshot.
6923 zfs_do_bookmark(int argc
, char **argv
)
6925 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
6926 char bookname
[ZFS_MAX_DATASET_NAME_LEN
];
6933 while ((c
= getopt(argc
, argv
, "")) != -1) {
6936 (void) fprintf(stderr
,
6937 gettext("invalid option '%c'\n"), optopt
);
6945 /* check number of arguments */
6947 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
6951 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
6955 if (strchr(argv
[1], '#') == NULL
) {
6956 (void) fprintf(stderr
,
6957 gettext("invalid bookmark name '%s': "
6958 "must contain a '#'\n"), argv
[1]);
6962 if (argv
[0][0] == '@') {
6964 * Snapshot name begins with @.
6965 * Default to same fs as bookmark.
6967 (void) strlcpy(snapname
, argv
[1], sizeof (snapname
));
6968 *strchr(snapname
, '#') = '\0';
6969 (void) strlcat(snapname
, argv
[0], sizeof (snapname
));
6971 (void) strlcpy(snapname
, argv
[0], sizeof (snapname
));
6973 if (argv
[1][0] == '#') {
6975 * Bookmark name begins with #.
6976 * Default to same fs as snapshot.
6978 (void) strlcpy(bookname
, argv
[0], sizeof (bookname
));
6979 *strchr(bookname
, '@') = '\0';
6980 (void) strlcat(bookname
, argv
[1], sizeof (bookname
));
6982 (void) strlcpy(bookname
, argv
[1], sizeof (bookname
));
6985 zhp
= zfs_open(g_zfs
, snapname
, ZFS_TYPE_SNAPSHOT
);
6991 nvl
= fnvlist_alloc();
6992 fnvlist_add_string(nvl
, bookname
, snapname
);
6993 ret
= lzc_bookmark(nvl
, NULL
);
6997 const char *err_msg
;
7000 (void) snprintf(errbuf
, sizeof (errbuf
),
7001 dgettext(TEXT_DOMAIN
,
7002 "cannot create bookmark '%s'"), bookname
);
7006 err_msg
= "bookmark is in a different pool";
7009 err_msg
= "bookmark exists";
7012 err_msg
= "invalid argument";
7015 err_msg
= "bookmark feature not enabled";
7018 err_msg
= "out of space";
7021 err_msg
= "dataset does not exist";
7024 err_msg
= "unknown error";
7027 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
7028 dgettext(TEXT_DOMAIN
, err_msg
));
7039 main(int argc
, char **argv
)
7046 (void) setlocale(LC_ALL
, "");
7047 (void) textdomain(TEXT_DOMAIN
);
7049 dprintf_setup(&argc
, argv
);
7054 * Make sure the user has specified some command.
7057 (void) fprintf(stderr
, gettext("missing command\n"));
7064 * The 'umount' command is an alias for 'unmount'
7066 if (strcmp(cmdname
, "umount") == 0)
7067 cmdname
= "unmount";
7070 * The 'recv' command is an alias for 'receive'
7072 if (strcmp(cmdname
, "recv") == 0)
7073 cmdname
= "receive";
7076 * The 'snap' command is an alias for 'snapshot'
7078 if (strcmp(cmdname
, "snap") == 0)
7079 cmdname
= "snapshot";
7084 if ((strcmp(cmdname
, "-?") == 0) ||
7085 (strcmp(cmdname
, "--help") == 0))
7088 if ((g_zfs
= libzfs_init()) == NULL
) {
7089 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
7093 mnttab_file
= g_zfs
->libzfs_mnttab
;
7095 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
7097 libzfs_print_on_error(g_zfs
, B_TRUE
);
7100 * Many commands modify input strings for string parsing reasons.
7101 * We create a copy to protect the original argv.
7103 newargv
= malloc((argc
+ 1) * sizeof (newargv
[0]));
7104 for (i
= 0; i
< argc
; i
++)
7105 newargv
[i
] = strdup(argv
[i
]);
7106 newargv
[argc
] = NULL
;
7109 * Run the appropriate command.
7111 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
7112 if (find_command_idx(cmdname
, &i
) == 0) {
7113 current_command
= &command_table
[i
];
7114 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
7115 } else if (strchr(cmdname
, '=') != NULL
) {
7116 verify(find_command_idx("set", &i
) == 0);
7117 current_command
= &command_table
[i
];
7118 ret
= command_table
[i
].func(argc
, newargv
);
7120 (void) fprintf(stderr
, gettext("unrecognized "
7121 "command '%s'\n"), cmdname
);
7126 for (i
= 0; i
< argc
; i
++)
7130 if (ret
== 0 && log_history
)
7131 (void) zpool_log_history(g_zfs
, history_str
);
7136 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7137 * for the purposes of running ::findleaks.
7139 if (getenv("ZFS_ABORT") != NULL
) {
7140 (void) printf("dumping core by request\n");