4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
37 #include <libnvpair.h>
50 #include <sys/mkdev.h>
51 #include <sys/mntent.h>
52 #include <sys/mnttab.h>
53 #include <sys/mount.h>
55 #include <sys/fs/zfs.h>
56 #include <sys/types.h>
60 #include <libzfs_core.h>
62 #include <zfs_deleg.h>
66 #include <directory.h>
67 #endif /* HAVE_IDMAP */
71 #include "zfs_comutil.h"
72 #include "libzfs_impl.h"
74 libzfs_handle_t
*g_zfs
;
76 static FILE *mnttab_file
;
77 static char history_str
[HIS_MAX_RECORD_LEN
];
78 static boolean_t log_history
= B_TRUE
;
80 static int zfs_do_clone(int argc
, char **argv
);
81 static int zfs_do_create(int argc
, char **argv
);
82 static int zfs_do_destroy(int argc
, char **argv
);
83 static int zfs_do_get(int argc
, char **argv
);
84 static int zfs_do_inherit(int argc
, char **argv
);
85 static int zfs_do_list(int argc
, char **argv
);
86 static int zfs_do_mount(int argc
, char **argv
);
87 static int zfs_do_rename(int argc
, char **argv
);
88 static int zfs_do_rollback(int argc
, char **argv
);
89 static int zfs_do_set(int argc
, char **argv
);
90 static int zfs_do_upgrade(int argc
, char **argv
);
91 static int zfs_do_snapshot(int argc
, char **argv
);
92 static int zfs_do_unmount(int argc
, char **argv
);
93 static int zfs_do_share(int argc
, char **argv
);
94 static int zfs_do_unshare(int argc
, char **argv
);
95 static int zfs_do_send(int argc
, char **argv
);
96 static int zfs_do_receive(int argc
, char **argv
);
97 static int zfs_do_promote(int argc
, char **argv
);
98 static int zfs_do_userspace(int argc
, char **argv
);
99 static int zfs_do_allow(int argc
, char **argv
);
100 static int zfs_do_unallow(int argc
, char **argv
);
101 static int zfs_do_hold(int argc
, char **argv
);
102 static int zfs_do_holds(int argc
, char **argv
);
103 static int zfs_do_release(int argc
, char **argv
);
104 static int zfs_do_diff(int argc
, char **argv
);
105 static int zfs_do_bookmark(int argc
, char **argv
);
108 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
113 _umem_debug_init(void)
115 return ("default,verbose"); /* $UMEM_DEBUG setting */
119 _umem_logging_init(void)
121 return ("fail,contents"); /* $UMEM_LOGGING setting */
155 typedef struct zfs_command
{
157 int (*func
)(int argc
, char **argv
);
162 * Master command table. Each ZFS command has a name, associated function, and
163 * usage message. The usage messages need to be internationalized, so we have
164 * to have a function to return the usage message based on a command index.
166 * These commands are organized according to how they are displayed in the usage
167 * message. An empty command (one with a NULL name) indicates an empty line in
168 * the generic usage message.
170 static zfs_command_t command_table
[] = {
171 { "create", zfs_do_create
, HELP_CREATE
},
172 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
174 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
175 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
176 { "clone", zfs_do_clone
, HELP_CLONE
},
177 { "promote", zfs_do_promote
, HELP_PROMOTE
},
178 { "rename", zfs_do_rename
, HELP_RENAME
},
179 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
181 { "list", zfs_do_list
, HELP_LIST
},
183 { "set", zfs_do_set
, HELP_SET
},
184 { "get", zfs_do_get
, HELP_GET
},
185 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
186 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
187 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
188 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
190 { "mount", zfs_do_mount
, HELP_MOUNT
},
191 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
192 { "share", zfs_do_share
, HELP_SHARE
},
193 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
195 { "send", zfs_do_send
, HELP_SEND
},
196 { "receive", zfs_do_receive
, HELP_RECEIVE
},
198 { "allow", zfs_do_allow
, HELP_ALLOW
},
200 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
202 { "hold", zfs_do_hold
, HELP_HOLD
},
203 { "holds", zfs_do_holds
, HELP_HOLDS
},
204 { "release", zfs_do_release
, HELP_RELEASE
},
205 { "diff", zfs_do_diff
, HELP_DIFF
},
208 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
210 zfs_command_t
*current_command
;
213 get_usage(zfs_help_t idx
)
217 return (gettext("\tclone [-p] [-o property=value] ... "
218 "<snapshot> <filesystem|volume>\n"));
220 return (gettext("\tcreate [-p] [-o property=value] ... "
222 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
223 "-V <size> <volume>\n"));
225 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
226 "\tdestroy [-dnpRrv] "
227 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
228 "\tdestroy <filesystem|volume>#<bookmark>\n"));
230 return (gettext("\tget [-rHp] [-d max] "
231 "[-o \"all\" | field[,...]]\n"
232 "\t [-t type[,...]] [-s source[,...]]\n"
233 "\t <\"all\" | property[,...]> "
234 "[filesystem|volume|snapshot] ...\n"));
236 return (gettext("\tinherit [-rS] <property> "
237 "<filesystem|volume|snapshot> ...\n"));
239 return (gettext("\tupgrade [-v]\n"
240 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
242 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
243 "[-s property]...\n\t [-S property]... [-t type[,...]] "
244 "[filesystem|volume|snapshot] ...\n"));
246 return (gettext("\tmount\n"
247 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
249 return (gettext("\tpromote <clone-filesystem>\n"));
251 return (gettext("\treceive [-vnsFu] <filesystem|volume|"
253 "\treceive [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
255 "\treceive -A <filesystem|volume>\n"));
257 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
258 "<filesystem|volume|snapshot>\n"
259 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
260 "\trename -r <snapshot> <snapshot>\n"));
262 return (gettext("\trollback [-rRf] <snapshot>\n"));
264 return (gettext("\tsend [-DnPpRvLe] [-[iI] snapshot] "
266 "\tsend [-Le] [-i snapshot|bookmark] "
267 "<filesystem|volume|snapshot>\n"
268 "\tsend [-nvPe] -t <receive_resume_token>\n"));
270 return (gettext("\tset <property=value> ... "
271 "<filesystem|volume|snapshot> ...\n"));
273 return (gettext("\tshare <-a | filesystem>\n"));
275 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
276 "<filesystem|volume>@<snap> ...\n"));
278 return (gettext("\tunmount [-f] "
279 "<-a | filesystem|mountpoint>\n"));
281 return (gettext("\tunshare "
282 "<-a | filesystem|mountpoint>\n"));
284 return (gettext("\tallow <filesystem|volume>\n"
286 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
287 "\t <filesystem|volume>\n"
288 "\tallow [-ld] -e <perm|@setname>[,...] "
289 "<filesystem|volume>\n"
290 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
291 "\tallow -s @setname <perm|@setname>[,...] "
292 "<filesystem|volume>\n"));
294 return (gettext("\tunallow [-rldug] "
295 "<\"everyone\"|user|group>[,...]\n"
296 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
297 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
298 "<filesystem|volume>\n"
299 "\tunallow [-r] -c [<perm|@setname>[,...]] "
300 "<filesystem|volume>\n"
301 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
302 "<filesystem|volume>\n"));
304 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
306 "\t [-S field] ... [-t type[,...]] "
307 "<filesystem|snapshot>\n"));
308 case HELP_GROUPSPACE
:
309 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
311 "\t [-S field] ... [-t type[,...]] "
312 "<filesystem|snapshot>\n"));
314 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
316 return (gettext("\tholds [-r] <snapshot> ...\n"));
318 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
320 return (gettext("\tdiff [-FHt] <snapshot> "
321 "[snapshot|filesystem]\n"));
323 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
333 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
338 * Utility function to guarantee malloc() success.
342 safe_malloc(size_t size
)
346 if ((data
= calloc(1, size
)) == NULL
)
353 safe_strdup(char *str
)
355 char *dupstr
= strdup(str
);
364 * Callback routine that will print out information for each of
368 usage_prop_cb(int prop
, void *cb
)
372 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
374 if (zfs_prop_readonly(prop
))
375 (void) fprintf(fp
, " NO ");
377 (void) fprintf(fp
, "YES ");
379 if (zfs_prop_inheritable(prop
))
380 (void) fprintf(fp
, " YES ");
382 (void) fprintf(fp
, " NO ");
384 if (zfs_prop_values(prop
) == NULL
)
385 (void) fprintf(fp
, "-\n");
387 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
393 * Display usage message. If we're inside a command, display only the usage for
394 * that command. Otherwise, iterate over the entire command table and display
395 * a complete usage message.
398 usage(boolean_t requested
)
401 boolean_t show_properties
= B_FALSE
;
402 FILE *fp
= requested
? stdout
: stderr
;
404 if (current_command
== NULL
) {
406 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
408 gettext("where 'command' is one of the following:\n\n"));
410 for (i
= 0; i
< NCOMMAND
; i
++) {
411 if (command_table
[i
].name
== NULL
)
412 (void) fprintf(fp
, "\n");
414 (void) fprintf(fp
, "%s",
415 get_usage(command_table
[i
].usage
));
418 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
419 "pool/[dataset/]*dataset[@name]\n"));
421 (void) fprintf(fp
, gettext("usage:\n"));
422 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
425 if (current_command
!= NULL
&&
426 (strcmp(current_command
->name
, "set") == 0 ||
427 strcmp(current_command
->name
, "get") == 0 ||
428 strcmp(current_command
->name
, "inherit") == 0 ||
429 strcmp(current_command
->name
, "list") == 0))
430 show_properties
= B_TRUE
;
432 if (show_properties
) {
434 gettext("\nThe following properties are supported:\n"));
436 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
437 "PROPERTY", "EDIT", "INHERIT", "VALUES");
439 /* Iterate over all properties */
440 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
443 (void) fprintf(fp
, "\t%-15s ", "userused@...");
444 (void) fprintf(fp
, " NO NO <size>\n");
445 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
446 (void) fprintf(fp
, " NO NO <size>\n");
447 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
448 (void) fprintf(fp
, "YES NO <size> | none\n");
449 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
450 (void) fprintf(fp
, "YES NO <size> | none\n");
451 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
452 (void) fprintf(fp
, " NO NO <size>\n");
454 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
455 "with standard units such as K, M, G, etc.\n"));
456 (void) fprintf(fp
, gettext("\nUser-defined properties can "
457 "be specified by using a name containing a colon (:).\n"));
458 (void) fprintf(fp
, gettext("\nThe {user|group}{used|quota}@ "
459 "properties must be appended with\n"
460 "a user or group specifier of one of these forms:\n"
461 " POSIX name (eg: \"matt\")\n"
462 " POSIX id (eg: \"126829\")\n"
463 " SMB name@domain (eg: \"matt@sun\")\n"
464 " SMB SID (eg: \"S-1-234-567-89\")\n"));
467 gettext("\nFor the property list, run: %s\n"),
470 gettext("\nFor the delegated permission list, run: %s\n"),
471 "zfs allow|unallow");
475 * See comments at end of main().
477 if (getenv("ZFS_ABORT") != NULL
) {
478 (void) printf("dumping core by request\n");
482 exit(requested
? 0 : 2);
486 * Take a property=value argument string and add it to the given nvlist.
487 * Modifies the argument inplace.
490 parseprop(nvlist_t
*props
, char *propname
)
492 char *propval
, *strval
;
494 if ((propval
= strchr(propname
, '=')) == NULL
) {
495 (void) fprintf(stderr
, gettext("missing "
496 "'=' for property=value argument\n"));
501 if (nvlist_lookup_string(props
, propname
, &strval
) == 0) {
502 (void) fprintf(stderr
, gettext("property '%s' "
503 "specified multiple times\n"), propname
);
506 if (nvlist_add_string(props
, propname
, propval
) != 0)
512 parse_depth(char *opt
, int *flags
)
517 depth
= (int)strtol(opt
, &tmp
, 0);
519 (void) fprintf(stderr
,
520 gettext("%s is not an integer\n"), optarg
);
524 (void) fprintf(stderr
,
525 gettext("Depth can not be negative.\n"));
528 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
532 #define PROGRESS_DELAY 2 /* seconds */
534 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";
535 static time_t pt_begin
;
536 static char *pt_header
= NULL
;
537 static boolean_t pt_shown
;
540 start_progress_timer(void)
542 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
547 set_progress_header(char *header
)
549 assert(pt_header
== NULL
);
550 pt_header
= safe_strdup(header
);
552 (void) printf("%s: ", header
);
553 (void) fflush(stdout
);
558 update_progress(char *update
)
560 if (!pt_shown
&& time(NULL
) > pt_begin
) {
561 int len
= strlen(update
);
563 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
565 (void) fflush(stdout
);
567 } else if (pt_shown
) {
568 int len
= strlen(update
);
570 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
571 (void) fflush(stdout
);
576 finish_progress(char *done
)
579 (void) printf("%s\n", done
);
580 (void) fflush(stdout
);
587 zfs_mount_and_share(libzfs_handle_t
*hdl
, const char *dataset
, zfs_type_t type
)
589 zfs_handle_t
*zhp
= NULL
;
592 zhp
= zfs_open(hdl
, dataset
, type
);
597 * Volumes may neither be mounted or shared. Potentially in the
598 * future filesystems detected on these volumes could be mounted.
600 if (zfs_get_type(zhp
) == ZFS_TYPE_VOLUME
) {
606 * Mount and/or share the new filesystem as appropriate. We provide a
607 * verbose error message to let the user know that their filesystem was
608 * in fact created, even if we failed to mount or share it.
610 * If the user doesn't want the dataset automatically mounted, then
611 * skip the mount/share step
613 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
, B_FALSE
) &&
614 zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
) {
615 if (geteuid() != 0) {
616 (void) fprintf(stderr
, gettext("filesystem "
617 "successfully created, but it may only be "
618 "mounted by root\n"));
620 } else if (zfs_mount(zhp
, NULL
, 0) != 0) {
621 (void) fprintf(stderr
, gettext("filesystem "
622 "successfully created, but not mounted\n"));
624 } else if (zfs_share(zhp
) != 0) {
625 (void) fprintf(stderr
, gettext("filesystem "
626 "successfully created, but not shared\n"));
637 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
639 * Given an existing dataset, create a writable copy whose initial contents
640 * are the same as the source. The newly created dataset maintains a
641 * dependency on the original; the original cannot be destroyed so long as
644 * The '-p' flag creates all the non-existing ancestors of the target first.
647 zfs_do_clone(int argc
, char **argv
)
649 zfs_handle_t
*zhp
= NULL
;
650 boolean_t parents
= B_FALSE
;
655 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
659 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
662 if (parseprop(props
, optarg
) != 0)
669 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
678 /* check number of arguments */
680 (void) fprintf(stderr
, gettext("missing source dataset "
685 (void) fprintf(stderr
, gettext("missing target dataset "
690 (void) fprintf(stderr
, gettext("too many arguments\n"));
694 /* open the source dataset */
695 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
698 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
701 * Now create the ancestors of the target dataset. If the
702 * target already exists and '-p' option was used we should not
705 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
708 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
713 ret
= zfs_clone(zhp
, argv
[1], props
);
715 /* create the mountpoint if necessary */
718 (void) zpool_log_history(g_zfs
, history_str
);
719 log_history
= B_FALSE
;
722 ret
= zfs_mount_and_share(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
739 * zfs create [-p] [-o prop=value] ... fs
740 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
742 * Create a new dataset. This command can be used to create filesystems
743 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
744 * For volumes, the user must specify a size to be used.
746 * The '-s' flag applies only to volumes, and indicates that we should not try
747 * to set the reservation for this volume. By default we set a reservation
748 * equal to the size for any volume. For pools with SPA_VERSION >=
749 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
751 * The '-p' flag creates all the non-existing ancestors of the target first.
754 zfs_do_create(int argc
, char **argv
)
756 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
757 uint64_t volsize
= 0;
759 boolean_t noreserve
= B_FALSE
;
760 boolean_t bflag
= B_FALSE
;
761 boolean_t parents
= B_FALSE
;
766 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
770 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
773 type
= ZFS_TYPE_VOLUME
;
774 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
775 (void) fprintf(stderr
, gettext("bad volume "
776 "size '%s': %s\n"), optarg
,
777 libzfs_error_description(g_zfs
));
781 if (nvlist_add_uint64(props
,
782 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
791 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
792 (void) fprintf(stderr
, gettext("bad volume "
793 "block size '%s': %s\n"), optarg
,
794 libzfs_error_description(g_zfs
));
798 if (nvlist_add_uint64(props
,
799 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
804 if (parseprop(props
, optarg
) != 0)
811 (void) fprintf(stderr
, gettext("missing size "
815 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
821 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
822 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
823 "used when creating a volume\n"));
830 /* check number of arguments */
832 (void) fprintf(stderr
, gettext("missing %s argument\n"),
833 zfs_type_to_name(type
));
837 (void) fprintf(stderr
, gettext("too many arguments\n"));
841 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
842 zpool_handle_t
*zpool_handle
;
843 nvlist_t
*real_props
= NULL
;
844 uint64_t spa_version
;
846 zfs_prop_t resv_prop
;
850 if ((p
= strchr(argv
[0], '/')))
852 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
855 if (zpool_handle
== NULL
)
857 spa_version
= zpool_get_prop_int(zpool_handle
,
858 ZPOOL_PROP_VERSION
, NULL
);
859 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
860 resv_prop
= ZFS_PROP_REFRESERVATION
;
862 resv_prop
= ZFS_PROP_RESERVATION
;
864 (void) snprintf(msg
, sizeof (msg
),
865 gettext("cannot create '%s'"), argv
[0]);
866 if (props
&& (real_props
= zfs_valid_proplist(g_zfs
, type
,
867 props
, 0, NULL
, zpool_handle
, msg
)) == NULL
) {
868 zpool_close(zpool_handle
);
871 zpool_close(zpool_handle
);
873 volsize
= zvol_volsize_to_reservation(volsize
, real_props
);
874 nvlist_free(real_props
);
876 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
878 if (nvlist_add_uint64(props
,
879 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
886 if (parents
&& zfs_name_valid(argv
[0], type
)) {
888 * Now create the ancestors of target dataset. If the target
889 * already exists and '-p' option was used we should not
892 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
896 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
901 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
905 (void) zpool_log_history(g_zfs
, history_str
);
906 log_history
= B_FALSE
;
909 ret
= zfs_mount_and_share(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
920 * zfs destroy [-rRf] <fs, vol>
921 * zfs destroy [-rRd] <snap>
923 * -r Recursively destroy all children
924 * -R Recursively destroy all dependents, including clones
925 * -f Force unmounting of any dependents
926 * -d If we can't destroy now, mark for deferred destruction
928 * Destroys the given dataset. By default, it will unmount any filesystems,
929 * and refuse to destroy a dataset that has any dependents. A dependent can
930 * either be a child, or a clone of a child.
932 typedef struct destroy_cbdata
{
935 boolean_t cb_recurse
;
937 boolean_t cb_doclones
;
938 zfs_handle_t
*cb_target
;
939 boolean_t cb_defer_destroy
;
940 boolean_t cb_verbose
;
941 boolean_t cb_parsable
;
944 nvlist_t
*cb_batchedsnaps
;
946 /* first snap in contiguous run */
948 /* previous snap in contiguous run */
956 * Check for any dependents based on the '-r' or '-R' flags.
959 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
961 destroy_cbdata_t
*cbp
= data
;
962 const char *tname
= zfs_get_name(cbp
->cb_target
);
963 const char *name
= zfs_get_name(zhp
);
965 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
966 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
968 * This is a direct descendant, not a clone somewhere else in
975 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
976 "%s has children\n"),
977 zfs_get_name(cbp
->cb_target
),
978 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
979 (void) fprintf(stderr
, gettext("use '-r' to destroy "
980 "the following datasets:\n"));
981 cbp
->cb_first
= B_FALSE
;
982 cbp
->cb_error
= B_TRUE
;
985 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
988 * This is a clone. We only want to report this if the '-r'
989 * wasn't specified, or the target is a snapshot.
991 if (!cbp
->cb_recurse
&&
992 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
996 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
997 "%s has dependent clones\n"),
998 zfs_get_name(cbp
->cb_target
),
999 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1000 (void) fprintf(stderr
, gettext("use '-R' to destroy "
1001 "the following datasets:\n"));
1002 cbp
->cb_first
= B_FALSE
;
1003 cbp
->cb_error
= B_TRUE
;
1004 cbp
->cb_dryrun
= B_TRUE
;
1007 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1016 destroy_callback(zfs_handle_t
*zhp
, void *data
)
1018 destroy_cbdata_t
*cb
= data
;
1019 const char *name
= zfs_get_name(zhp
);
1021 if (cb
->cb_verbose
) {
1022 if (cb
->cb_parsable
) {
1023 (void) printf("destroy\t%s\n", name
);
1024 } else if (cb
->cb_dryrun
) {
1025 (void) printf(gettext("would destroy %s\n"),
1028 (void) printf(gettext("will destroy %s\n"),
1034 * Ignore pools (which we've already flagged as an error before getting
1037 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1038 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1042 if (cb
->cb_dryrun
) {
1048 * We batch up all contiguous snapshots (even of different
1049 * filesystems) and destroy them with one ioctl. We can't
1050 * simply do all snap deletions and then all fs deletions,
1051 * because we must delete a clone before its origin.
1053 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1054 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1056 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1057 cb
->cb_batchedsnaps
, B_FALSE
);
1058 fnvlist_free(cb
->cb_batchedsnaps
);
1059 cb
->cb_batchedsnaps
= fnvlist_alloc();
1062 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1063 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1074 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1076 destroy_cbdata_t
*cb
= arg
;
1077 const char *name
= zfs_get_name(zhp
);
1080 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1081 if (cb
->cb_firstsnap
== NULL
)
1082 cb
->cb_firstsnap
= strdup(name
);
1083 if (cb
->cb_prevsnap
!= NULL
)
1084 free(cb
->cb_prevsnap
);
1085 /* this snap continues the current range */
1086 cb
->cb_prevsnap
= strdup(name
);
1087 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1089 if (cb
->cb_verbose
) {
1090 if (cb
->cb_parsable
) {
1091 (void) printf("destroy\t%s\n", name
);
1092 } else if (cb
->cb_dryrun
) {
1093 (void) printf(gettext("would destroy %s\n"),
1096 (void) printf(gettext("will destroy %s\n"),
1100 } else if (cb
->cb_firstsnap
!= NULL
) {
1101 /* end of this range */
1103 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1104 cb
->cb_prevsnap
, &used
);
1105 cb
->cb_snapused
+= used
;
1106 free(cb
->cb_firstsnap
);
1107 cb
->cb_firstsnap
= NULL
;
1108 free(cb
->cb_prevsnap
);
1109 cb
->cb_prevsnap
= NULL
;
1116 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1119 assert(cb
->cb_firstsnap
== NULL
);
1120 assert(cb
->cb_prevsnap
== NULL
);
1121 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1122 if (cb
->cb_firstsnap
!= NULL
) {
1125 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1126 cb
->cb_prevsnap
, &used
);
1128 cb
->cb_snapused
+= used
;
1129 free(cb
->cb_firstsnap
);
1130 cb
->cb_firstsnap
= NULL
;
1131 free(cb
->cb_prevsnap
);
1132 cb
->cb_prevsnap
= NULL
;
1138 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1140 destroy_cbdata_t
*cb
= arg
;
1143 /* Check for clones. */
1144 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1145 cb
->cb_target
= zhp
;
1146 cb
->cb_first
= B_TRUE
;
1147 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1148 destroy_check_dependent
, cb
);
1152 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1160 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1162 destroy_cbdata_t
*cb
= arg
;
1165 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1171 if (cb
->cb_verbose
) {
1172 err
= destroy_print_snapshots(zhp
, cb
);
1178 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1186 destroy_clones(destroy_cbdata_t
*cb
)
1189 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1191 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1192 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1195 boolean_t defer
= cb
->cb_defer_destroy
;
1199 * We can't defer destroy non-snapshots, so set it to
1200 * false while destroying the clones.
1202 cb
->cb_defer_destroy
= B_FALSE
;
1203 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1204 destroy_callback
, cb
);
1205 cb
->cb_defer_destroy
= defer
;
1215 zfs_do_destroy(int argc
, char **argv
)
1217 destroy_cbdata_t cb
= { 0 };
1221 zfs_handle_t
*zhp
= NULL
;
1223 zfs_type_t type
= ZFS_TYPE_DATASET
;
1226 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1229 cb
.cb_verbose
= B_TRUE
;
1232 cb
.cb_verbose
= B_TRUE
;
1233 cb
.cb_parsable
= B_TRUE
;
1236 cb
.cb_dryrun
= B_TRUE
;
1239 cb
.cb_defer_destroy
= B_TRUE
;
1240 type
= ZFS_TYPE_SNAPSHOT
;
1243 cb
.cb_force
= B_TRUE
;
1246 cb
.cb_recurse
= B_TRUE
;
1249 cb
.cb_recurse
= B_TRUE
;
1250 cb
.cb_doclones
= B_TRUE
;
1254 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1263 /* check number of arguments */
1265 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1269 (void) fprintf(stderr
, gettext("too many arguments\n"));
1273 at
= strchr(argv
[0], '@');
1274 pound
= strchr(argv
[0], '#');
1277 /* Build the list of snaps to destroy in cb_nvl. */
1278 cb
.cb_nvl
= fnvlist_alloc();
1281 zhp
= zfs_open(g_zfs
, argv
[0],
1282 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1286 cb
.cb_snapspec
= at
+ 1;
1287 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1293 if (nvlist_empty(cb
.cb_nvl
)) {
1294 (void) fprintf(stderr
, gettext("could not find any "
1295 "snapshots to destroy; check snapshot names.\n"));
1300 if (cb
.cb_verbose
) {
1302 zfs_nicenum(cb
.cb_snapused
, buf
, sizeof (buf
));
1303 if (cb
.cb_parsable
) {
1304 (void) printf("reclaim\t%llu\n",
1305 (u_longlong_t
)cb
.cb_snapused
);
1306 } else if (cb
.cb_dryrun
) {
1307 (void) printf(gettext("would reclaim %s\n"),
1310 (void) printf(gettext("will reclaim %s\n"),
1315 if (!cb
.cb_dryrun
) {
1316 if (cb
.cb_doclones
) {
1317 cb
.cb_batchedsnaps
= fnvlist_alloc();
1318 err
= destroy_clones(&cb
);
1320 err
= zfs_destroy_snaps_nvl(g_zfs
,
1321 cb
.cb_batchedsnaps
, B_FALSE
);
1329 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1330 cb
.cb_defer_destroy
);
1336 } else if (pound
!= NULL
) {
1341 (void) fprintf(stderr
,
1342 "dryrun is not supported with bookmark\n");
1346 if (cb
.cb_defer_destroy
) {
1347 (void) fprintf(stderr
,
1348 "defer destroy is not supported with bookmark\n");
1352 if (cb
.cb_recurse
) {
1353 (void) fprintf(stderr
,
1354 "recursive is not supported with bookmark\n");
1358 if (!zfs_bookmark_exists(argv
[0])) {
1359 (void) fprintf(stderr
, gettext("bookmark '%s' "
1360 "does not exist.\n"), argv
[0]);
1364 nvl
= fnvlist_alloc();
1365 fnvlist_add_boolean(nvl
, argv
[0]);
1367 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1369 (void) zfs_standard_error(g_zfs
, err
,
1370 "cannot destroy bookmark");
1373 nvlist_free(cb
.cb_nvl
);
1377 /* Open the given dataset */
1378 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1384 * Perform an explicit check for pools before going any further.
1386 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1387 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1388 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1389 "operation does not apply to pools\n"),
1391 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1392 "%s' to destroy all datasets in the pool\n"),
1394 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1395 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1401 * Check for any dependents and/or clones.
1403 cb
.cb_first
= B_TRUE
;
1404 if (!cb
.cb_doclones
&&
1405 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1416 cb
.cb_batchedsnaps
= fnvlist_alloc();
1417 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1424 * Do the real thing. The callback will close the
1425 * handle regardless of whether it succeeds or not.
1427 err
= destroy_callback(zhp
, &cb
);
1430 err
= zfs_destroy_snaps_nvl(g_zfs
,
1431 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1438 fnvlist_free(cb
.cb_batchedsnaps
);
1439 fnvlist_free(cb
.cb_nvl
);
1446 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1449 zfs_get_column_t col
;
1451 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1452 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1453 if (col
== GET_COL_RECVD
)
1459 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1460 * < all | property[,property]... > < fs | snap | vol > ...
1462 * -r recurse over any child datasets
1463 * -H scripted mode. Headers are stripped, and fields are separated
1464 * by tabs instead of spaces.
1465 * -o Set of fields to display. One of "name,property,value,
1466 * received,source". Default is "name,property,value,source".
1467 * "all" is an alias for all five.
1468 * -s Set of sources to allow. One of
1469 * "local,default,inherited,received,temporary,none". Default is
1471 * -p Display values in parsable (literal) format.
1473 * Prints properties for the given datasets. The user can control which
1474 * columns to display as well as which property types to allow.
1478 * Invoked to display the properties for a single dataset.
1481 get_callback(zfs_handle_t
*zhp
, void *data
)
1483 char buf
[ZFS_MAXPROPLEN
];
1484 char rbuf
[ZFS_MAXPROPLEN
];
1485 zprop_source_t sourcetype
;
1486 char source
[ZFS_MAXNAMELEN
];
1487 zprop_get_cbdata_t
*cbp
= data
;
1488 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1489 zprop_list_t
*pl
= cbp
->cb_proplist
;
1493 boolean_t received
= is_recvd_column(cbp
);
1495 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1496 char *recvdval
= NULL
;
1498 * Skip the special fake placeholder. This will also skip over
1499 * the name property when 'all' is specified.
1501 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1502 pl
== cbp
->cb_proplist
)
1505 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1506 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1507 sizeof (buf
), &sourcetype
, source
,
1509 cbp
->cb_literal
) != 0) {
1512 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1513 ZFS_TYPE_DATASET
, B_FALSE
)) {
1514 (void) fprintf(stderr
,
1515 gettext("No such property '%s'\n"),
1516 zfs_prop_to_name(pl
->pl_prop
));
1519 sourcetype
= ZPROP_SRC_NONE
;
1520 (void) strlcpy(buf
, "-", sizeof (buf
));
1523 if (received
&& (zfs_prop_get_recvd(zhp
,
1524 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1525 cbp
->cb_literal
) == 0))
1528 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1529 zfs_prop_to_name(pl
->pl_prop
),
1530 buf
, sourcetype
, source
, recvdval
);
1531 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1532 sourcetype
= ZPROP_SRC_LOCAL
;
1534 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1535 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1536 sourcetype
= ZPROP_SRC_NONE
;
1537 (void) strlcpy(buf
, "-", sizeof (buf
));
1540 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1541 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1542 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1543 sourcetype
= ZPROP_SRC_LOCAL
;
1545 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1546 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1547 sourcetype
= ZPROP_SRC_NONE
;
1548 (void) strlcpy(buf
, "-", sizeof (buf
));
1551 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1552 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1554 if (nvlist_lookup_nvlist(user_props
,
1555 pl
->pl_user_prop
, &propval
) != 0) {
1558 sourcetype
= ZPROP_SRC_NONE
;
1561 verify(nvlist_lookup_string(propval
,
1562 ZPROP_VALUE
, &strval
) == 0);
1563 verify(nvlist_lookup_string(propval
,
1564 ZPROP_SOURCE
, &sourceval
) == 0);
1566 if (strcmp(sourceval
,
1567 zfs_get_name(zhp
)) == 0) {
1568 sourcetype
= ZPROP_SRC_LOCAL
;
1569 } else if (strcmp(sourceval
,
1570 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1571 sourcetype
= ZPROP_SRC_RECEIVED
;
1573 sourcetype
= ZPROP_SRC_INHERITED
;
1574 (void) strlcpy(source
,
1575 sourceval
, sizeof (source
));
1579 if (received
&& (zfs_prop_get_recvd(zhp
,
1580 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1581 cbp
->cb_literal
) == 0))
1584 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1585 pl
->pl_user_prop
, strval
, sourcetype
,
1594 zfs_do_get(int argc
, char **argv
)
1596 zprop_get_cbdata_t cb
= { 0 };
1597 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1598 int types
= ZFS_TYPE_DATASET
;
1599 char *value
, *fields
;
1602 zprop_list_t fake_name
= { 0 };
1605 * Set up default columns and sources.
1607 cb
.cb_sources
= ZPROP_SRC_ALL
;
1608 cb
.cb_columns
[0] = GET_COL_NAME
;
1609 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1610 cb
.cb_columns
[2] = GET_COL_VALUE
;
1611 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1612 cb
.cb_type
= ZFS_TYPE_DATASET
;
1615 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1618 cb
.cb_literal
= B_TRUE
;
1621 limit
= parse_depth(optarg
, &flags
);
1624 flags
|= ZFS_ITER_RECURSE
;
1627 cb
.cb_scripted
= B_TRUE
;
1630 (void) fprintf(stderr
, gettext("missing argument for "
1631 "'%c' option\n"), optopt
);
1636 * Process the set of columns to display. We zero out
1637 * the structure to give us a blank slate.
1639 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1641 while (*optarg
!= '\0') {
1642 static char *col_subopts
[] =
1643 { "name", "property", "value", "received",
1644 "source", "all", NULL
};
1646 if (i
== ZFS_GET_NCOLS
) {
1647 (void) fprintf(stderr
, gettext("too "
1648 "many fields given to -o "
1653 switch (getsubopt(&optarg
, col_subopts
,
1656 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1659 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1662 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1665 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1666 flags
|= ZFS_ITER_RECVD_PROPS
;
1669 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1673 (void) fprintf(stderr
,
1674 gettext("\"all\" conflicts "
1675 "with specific fields "
1676 "given to -o option\n"));
1679 cb
.cb_columns
[0] = GET_COL_NAME
;
1680 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1681 cb
.cb_columns
[2] = GET_COL_VALUE
;
1682 cb
.cb_columns
[3] = GET_COL_RECVD
;
1683 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1684 flags
|= ZFS_ITER_RECVD_PROPS
;
1688 (void) fprintf(stderr
,
1689 gettext("invalid column name "
1698 while (*optarg
!= '\0') {
1699 static char *source_subopts
[] = {
1700 "local", "default", "inherited",
1701 "received", "temporary", "none",
1704 switch (getsubopt(&optarg
, source_subopts
,
1707 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1710 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1713 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1716 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1719 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1722 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1725 (void) fprintf(stderr
,
1726 gettext("invalid source "
1735 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1736 while (*optarg
!= '\0') {
1737 static char *type_subopts
[] = { "filesystem",
1738 "volume", "snapshot", "bookmark",
1741 switch (getsubopt(&optarg
, type_subopts
,
1744 types
|= ZFS_TYPE_FILESYSTEM
;
1747 types
|= ZFS_TYPE_VOLUME
;
1750 types
|= ZFS_TYPE_SNAPSHOT
;
1753 types
|= ZFS_TYPE_BOOKMARK
;
1756 types
= ZFS_TYPE_DATASET
|
1761 (void) fprintf(stderr
,
1762 gettext("invalid type '%s'\n"),
1770 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1780 (void) fprintf(stderr
, gettext("missing property "
1787 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1795 * As part of zfs_expand_proplist(), we keep track of the maximum column
1796 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1797 * need to know the maximum name length. However, the user likely did
1798 * not specify 'name' as one of the properties to fetch, so we need to
1799 * make sure we always include at least this property for
1800 * print_get_headers() to work properly.
1802 if (cb
.cb_proplist
!= NULL
) {
1803 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1804 fake_name
.pl_width
= strlen(gettext("NAME"));
1805 fake_name
.pl_next
= cb
.cb_proplist
;
1806 cb
.cb_proplist
= &fake_name
;
1809 cb
.cb_first
= B_TRUE
;
1811 /* run for each object */
1812 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1813 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1815 if (cb
.cb_proplist
== &fake_name
)
1816 zprop_free_list(fake_name
.pl_next
);
1818 zprop_free_list(cb
.cb_proplist
);
1824 * inherit [-rS] <property> <fs|vol> ...
1826 * -r Recurse over all children
1827 * -S Revert to received value, if any
1829 * For each dataset specified on the command line, inherit the given property
1830 * from its parent. Inheriting a property at the pool level will cause it to
1831 * use the default value. The '-r' flag will recurse over all children, and is
1832 * useful for setting a property on a hierarchy-wide basis, regardless of any
1833 * local modifications for each dataset.
1836 typedef struct inherit_cbdata
{
1837 const char *cb_propname
;
1838 boolean_t cb_received
;
1842 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1844 inherit_cbdata_t
*cb
= data
;
1845 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1848 * If we're doing it recursively, then ignore properties that
1849 * are not valid for this type of dataset.
1851 if (prop
!= ZPROP_INVAL
&&
1852 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
), B_FALSE
))
1855 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1859 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1861 inherit_cbdata_t
*cb
= data
;
1863 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1867 zfs_do_inherit(int argc
, char **argv
)
1871 inherit_cbdata_t cb
= { 0 };
1875 boolean_t received
= B_FALSE
;
1878 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1881 flags
|= ZFS_ITER_RECURSE
;
1888 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1897 /* check number of arguments */
1899 (void) fprintf(stderr
, gettext("missing property argument\n"));
1903 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1911 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1912 if (zfs_prop_readonly(prop
)) {
1913 (void) fprintf(stderr
, gettext(
1914 "%s property is read-only\n"),
1918 if (!zfs_prop_inheritable(prop
) && !received
) {
1919 (void) fprintf(stderr
, gettext("'%s' property cannot "
1920 "be inherited\n"), propname
);
1921 if (prop
== ZFS_PROP_QUOTA
||
1922 prop
== ZFS_PROP_RESERVATION
||
1923 prop
== ZFS_PROP_REFQUOTA
||
1924 prop
== ZFS_PROP_REFRESERVATION
) {
1925 (void) fprintf(stderr
, gettext("use 'zfs set "
1926 "%s=none' to clear\n"), propname
);
1927 (void) fprintf(stderr
, gettext("use 'zfs "
1928 "inherit -S %s' to revert to received "
1929 "value\n"), propname
);
1933 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1934 prop
== ZFS_PROP_VERSION
)) {
1935 (void) fprintf(stderr
, gettext("'%s' property cannot "
1936 "be reverted to a received value\n"), propname
);
1939 } else if (!zfs_prop_user(propname
)) {
1940 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1945 cb
.cb_propname
= propname
;
1946 cb
.cb_received
= received
;
1948 if (flags
& ZFS_ITER_RECURSE
) {
1949 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1950 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1952 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1953 NULL
, NULL
, 0, inherit_cb
, &cb
);
1959 typedef struct upgrade_cbdata
{
1960 uint64_t cb_numupgraded
;
1961 uint64_t cb_numsamegraded
;
1962 uint64_t cb_numfailed
;
1963 uint64_t cb_version
;
1965 boolean_t cb_foundone
;
1966 char cb_lastfs
[ZFS_MAXNAMELEN
];
1970 same_pool(zfs_handle_t
*zhp
, const char *name
)
1972 int len1
= strcspn(name
, "/@");
1973 const char *zhname
= zfs_get_name(zhp
);
1974 int len2
= strcspn(zhname
, "/@");
1978 return (strncmp(name
, zhname
, len1
) == 0);
1982 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
1984 upgrade_cbdata_t
*cb
= data
;
1985 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1987 /* list if it's old/new */
1988 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
1989 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
1992 str
= gettext("The following filesystems are "
1993 "formatted using a newer software version and\n"
1994 "cannot be accessed on the current system.\n\n");
1996 str
= gettext("The following filesystems are "
1997 "out of date, and can be upgraded. After being\n"
1998 "upgraded, these filesystems (and any 'zfs send' "
1999 "streams generated from\n"
2000 "subsequent snapshots) will no longer be "
2001 "accessible by older software versions.\n\n");
2004 if (!cb
->cb_foundone
) {
2006 (void) printf(gettext("VER FILESYSTEM\n"));
2007 (void) printf(gettext("--- ------------\n"));
2008 cb
->cb_foundone
= B_TRUE
;
2011 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
2018 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2020 upgrade_cbdata_t
*cb
= data
;
2021 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2022 int needed_spa_version
;
2025 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2028 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2030 if (needed_spa_version
< 0)
2033 if (spa_version
< needed_spa_version
) {
2035 (void) printf(gettext("%s: can not be "
2036 "upgraded; the pool version needs to first "
2037 "be upgraded\nto version %d\n\n"),
2038 zfs_get_name(zhp
), needed_spa_version
);
2044 if (version
< cb
->cb_version
) {
2046 (void) snprintf(verstr
, sizeof (verstr
),
2047 "%llu", (u_longlong_t
)cb
->cb_version
);
2048 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2050 * If they did "zfs upgrade -a", then we could
2051 * be doing ioctls to different pools. We need
2052 * to log this history once to each pool, and bypass
2053 * the normal history logging that happens in main().
2055 (void) zpool_log_history(g_zfs
, history_str
);
2056 log_history
= B_FALSE
;
2058 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2059 cb
->cb_numupgraded
++;
2062 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2063 } else if (version
> cb
->cb_version
) {
2064 /* can't downgrade */
2065 (void) printf(gettext("%s: can not be downgraded; "
2066 "it is already at version %u\n"),
2067 zfs_get_name(zhp
), version
);
2070 cb
->cb_numsamegraded
++;
2078 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2081 zfs_do_upgrade(int argc
, char **argv
)
2083 boolean_t all
= B_FALSE
;
2084 boolean_t showversions
= B_FALSE
;
2086 upgrade_cbdata_t cb
= { 0 };
2088 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2091 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2094 flags
|= ZFS_ITER_RECURSE
;
2097 showversions
= B_TRUE
;
2100 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2101 optarg
, &cb
.cb_version
) != 0) {
2102 (void) fprintf(stderr
,
2103 gettext("invalid version %s\n"), optarg
);
2112 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2121 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2123 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2124 cb
.cb_version
|| argc
))
2126 if ((all
|| argc
) && (showversions
))
2132 /* Show info on available versions. */
2133 (void) printf(gettext("The following filesystem versions are "
2135 (void) printf(gettext("VER DESCRIPTION\n"));
2136 (void) printf("--- -----------------------------------------"
2137 "---------------\n");
2138 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2139 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2140 (void) printf(gettext(" 3 Case insensitive and filesystem "
2141 "user identifier (FUID)\n"));
2142 (void) printf(gettext(" 4 userquota, groupquota "
2144 (void) printf(gettext(" 5 System attributes\n"));
2145 (void) printf(gettext("\nFor more information on a particular "
2146 "version, including supported releases,\n"));
2147 (void) printf("see the ZFS Administration Guide.\n\n");
2149 } else if (argc
|| all
) {
2150 /* Upgrade filesystems */
2151 if (cb
.cb_version
== 0)
2152 cb
.cb_version
= ZPL_VERSION
;
2153 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2154 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2155 (void) printf(gettext("%llu filesystems upgraded\n"),
2156 (u_longlong_t
)cb
.cb_numupgraded
);
2157 if (cb
.cb_numsamegraded
) {
2158 (void) printf(gettext("%llu filesystems already at "
2160 (u_longlong_t
)cb
.cb_numsamegraded
);
2162 if (cb
.cb_numfailed
!= 0)
2165 /* List old-version filesytems */
2167 (void) printf(gettext("This system is currently running "
2168 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2170 flags
|= ZFS_ITER_RECURSE
;
2171 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2172 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2174 found
= cb
.cb_foundone
;
2175 cb
.cb_foundone
= B_FALSE
;
2176 cb
.cb_newer
= B_TRUE
;
2178 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2179 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2181 if (!cb
.cb_foundone
&& !found
) {
2182 (void) printf(gettext("All filesystems are "
2183 "formatted with the current version.\n"));
2191 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2192 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2193 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2194 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2196 * -H Scripted mode; elide headers and separate columns by tabs.
2197 * -i Translate SID to POSIX ID.
2198 * -n Print numeric ID instead of user/group name.
2199 * -o Control which fields to display.
2200 * -p Use exact (parsable) numeric output.
2201 * -s Specify sort columns, descending order.
2202 * -S Specify sort columns, ascending order.
2203 * -t Control which object types to display.
2205 * Displays space consumed by, and quotas on, each user in the specified
2206 * filesystem or snapshot.
2209 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2210 enum us_field_types
{
2216 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA" };
2217 static char *us_field_names
[] = { "type", "name", "used", "quota" };
2218 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2220 #define USTYPE_PSX_GRP (1 << 0)
2221 #define USTYPE_PSX_USR (1 << 1)
2222 #define USTYPE_SMB_GRP (1 << 2)
2223 #define USTYPE_SMB_USR (1 << 3)
2224 #define USTYPE_ALL \
2225 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2227 static int us_type_bits
[] = {
2234 static char *us_type_names
[] = { "posixgroup", "posixuser", "smbgroup",
2237 typedef struct us_node
{
2239 uu_avl_node_t usn_avlnode
;
2240 uu_list_node_t usn_listnode
;
2243 typedef struct us_cbdata
{
2245 uu_avl_pool_t
*cb_avl_pool
;
2247 boolean_t cb_numname
;
2248 boolean_t cb_nicenum
;
2249 boolean_t cb_sid2posix
;
2250 zfs_userquota_prop_t cb_prop
;
2251 zfs_sort_column_t
*cb_sortcol
;
2252 size_t cb_width
[USFIELD_LAST
];
2255 static boolean_t us_populated
= B_FALSE
;
2258 zfs_sort_column_t
*si_sortcol
;
2259 boolean_t si_numname
;
2263 us_field_index(char *field
)
2267 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2268 if (strcmp(field
, us_field_names
[i
]) == 0)
2276 us_compare(const void *larg
, const void *rarg
, void *unused
)
2278 const us_node_t
*l
= larg
;
2279 const us_node_t
*r
= rarg
;
2280 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2281 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2282 boolean_t numname
= si
->si_numname
;
2283 nvlist_t
*lnvl
= l
->usn_nvl
;
2284 nvlist_t
*rnvl
= r
->usn_nvl
;
2288 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2295 zfs_prop_t prop
= sortcol
->sc_prop
;
2296 const char *propname
= NULL
;
2297 boolean_t reverse
= sortcol
->sc_reverse
;
2302 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2303 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2305 rc
= (rv32
< lv32
) ? 1 : -1;
2311 (void) nvlist_lookup_uint64(lnvl
, propname
,
2313 (void) nvlist_lookup_uint64(rnvl
, propname
,
2316 rc
= (rv64
< lv64
) ? 1 : -1;
2318 if ((nvlist_lookup_string(lnvl
, propname
,
2319 &lvstr
) == ENOENT
) ||
2320 (nvlist_lookup_string(rnvl
, propname
,
2321 &rvstr
) == ENOENT
)) {
2324 rc
= strcmp(lvstr
, rvstr
);
2328 case ZFS_PROP_QUOTA
:
2331 if (prop
== ZFS_PROP_USED
)
2335 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2336 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2338 rc
= (rv64
< lv64
) ? 1 : -1;
2346 return (reverse
? 1 : -1);
2348 return (reverse
? -1 : 1);
2353 * If entries still seem to be the same, check if they are of the same
2354 * type (smbentity is added only if we are doing SID to POSIX ID
2355 * translation where we can have duplicate type/name combinations).
2357 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2358 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2360 return (lvb
< rvb
? -1 : 1);
2365 static inline const char *
2366 us_type2str(unsigned field_type
)
2368 switch (field_type
) {
2369 case USTYPE_PSX_USR
:
2370 return ("POSIX User");
2371 case USTYPE_PSX_GRP
:
2372 return ("POSIX Group");
2373 case USTYPE_SMB_USR
:
2374 return ("SMB User");
2375 case USTYPE_SMB_GRP
:
2376 return ("SMB Group");
2378 return ("Undefined");
2383 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2385 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2386 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2391 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2392 uu_avl_t
*avl
= cb
->cb_avl
;
2396 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2398 const char *typestr
;
2402 int typeidx
, nameidx
, sizeidx
;
2403 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2404 boolean_t smbentity
= B_FALSE
;
2406 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2408 node
= safe_malloc(sizeof (us_node_t
));
2409 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2410 node
->usn_nvl
= props
;
2412 if (domain
!= NULL
&& domain
[0] != '\0') {
2415 char sid
[ZFS_MAXNAMELEN
+ 32];
2419 directory_error_t e
;
2423 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2425 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2426 type
= USTYPE_SMB_GRP
;
2427 err
= sid_to_id(sid
, B_FALSE
, &id
);
2429 type
= USTYPE_SMB_USR
;
2430 err
= sid_to_id(sid
, B_TRUE
, &id
);
2435 if (!cb
->cb_sid2posix
) {
2436 e
= directory_name_from_sid(NULL
, sid
, &name
,
2439 directory_error_free(e
);
2449 #endif /* HAVE_IDMAP */
2452 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2454 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2455 type
= USTYPE_PSX_GRP
;
2456 if (!cb
->cb_numname
) {
2459 if ((g
= getgrgid(rid
)) != NULL
)
2463 type
= USTYPE_PSX_USR
;
2464 if (!cb
->cb_numname
) {
2467 if ((p
= getpwuid(rid
)) != NULL
)
2474 * Make sure that the type/name combination is unique when doing
2475 * SID to POSIX ID translation (hence changing the type from SMB to
2478 if (cb
->cb_sid2posix
&&
2479 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2482 /* Calculate/update width of TYPE field */
2483 typestr
= us_type2str(type
);
2484 typelen
= strlen(gettext(typestr
));
2485 typeidx
= us_field_index("type");
2486 if (typelen
> cb
->cb_width
[typeidx
])
2487 cb
->cb_width
[typeidx
] = typelen
;
2488 if (nvlist_add_uint32(props
, "type", type
) != 0)
2491 /* Calculate/update width of NAME field */
2492 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2493 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2495 namelen
= snprintf(NULL
, 0, "%u", rid
);
2497 if (nvlist_add_string(props
, "name", name
) != 0)
2499 namelen
= strlen(name
);
2501 nameidx
= us_field_index("name");
2502 if (namelen
> cb
->cb_width
[nameidx
])
2503 cb
->cb_width
[nameidx
] = namelen
;
2506 * Check if this type/name combination is in the list and update it;
2507 * otherwise add new node to the list.
2509 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2510 uu_avl_insert(avl
, node
, idx
);
2515 props
= node
->usn_nvl
;
2518 /* Calculate/update width of USED/QUOTA fields */
2520 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2522 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu",
2523 (u_longlong_t
)space
);
2524 sizelen
= strlen(sizebuf
);
2525 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
) {
2527 if (!nvlist_exists(props
, "quota"))
2528 (void) nvlist_add_uint64(props
, "quota", 0);
2531 if (!nvlist_exists(props
, "used"))
2532 (void) nvlist_add_uint64(props
, "used", 0);
2534 sizeidx
= us_field_index(propname
);
2535 if (sizelen
> cb
->cb_width
[sizeidx
])
2536 cb
->cb_width
[sizeidx
] = sizelen
;
2538 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2545 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2546 size_t *width
, us_node_t
*node
)
2548 nvlist_t
*nvl
= node
->usn_nvl
;
2549 char valstr
[ZFS_MAXNAMELEN
];
2550 boolean_t first
= B_TRUE
;
2556 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2557 if (!(ustype
& types
))
2560 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2561 nvpair_t
*nvp
= NULL
;
2565 char *strval
= NULL
;
2567 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2568 if (strcmp(nvpair_name(nvp
),
2569 us_field_names
[field
]) == 0)
2573 type
= nvpair_type(nvp
);
2575 case DATA_TYPE_UINT32
:
2576 (void) nvpair_value_uint32(nvp
, &val32
);
2578 case DATA_TYPE_UINT64
:
2579 (void) nvpair_value_uint64(nvp
, &val64
);
2581 case DATA_TYPE_STRING
:
2582 (void) nvpair_value_string(nvp
, &strval
);
2585 (void) fprintf(stderr
, "invalid data type\n");
2590 strval
= (char *)us_type2str(val32
);
2593 if (type
== DATA_TYPE_UINT64
) {
2594 (void) sprintf(valstr
, "%llu",
2595 (u_longlong_t
) val64
);
2601 if (type
== DATA_TYPE_UINT64
) {
2603 (void) sprintf(valstr
, "%llu",
2604 (u_longlong_t
) val64
);
2606 zfs_nicenum(val64
, valstr
,
2609 if (field
== USFIELD_QUOTA
&&
2610 strcmp(valstr
, "0") == 0)
2620 (void) printf("\t");
2625 (void) printf("%s", strval
);
2626 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2627 (void) printf("%-*s", (int) width
[field
], strval
);
2629 (void) printf("%*s", (int) width
[field
], strval
);
2635 (void) printf("\n");
2639 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2640 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2648 boolean_t first
= B_TRUE
;
2650 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2651 col
= gettext(us_field_hdr
[field
]);
2652 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2653 (void) printf(first
? "%-*s" : " %-*s",
2654 (int) width
[field
], col
);
2656 (void) printf(first
? "%*s" : " %*s",
2657 (int) width
[field
], col
);
2662 (void) printf("\n");
2665 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2666 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2668 nvlist_free(node
->usn_nvl
);
2673 zfs_do_userspace(int argc
, char **argv
)
2676 zfs_userquota_prop_t p
;
2677 uu_avl_pool_t
*avl_pool
;
2679 uu_avl_walk_t
*walk
;
2681 char deffields
[] = "type,name,used,quota";
2682 char *ofield
= NULL
;
2683 char *tfield
= NULL
;
2687 boolean_t scripted
= B_FALSE
;
2688 boolean_t prtnum
= B_FALSE
;
2689 boolean_t parsable
= B_FALSE
;
2690 boolean_t sid2posix
= B_FALSE
;
2693 zfs_sort_column_t
*sortcol
= NULL
;
2694 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2698 uu_list_pool_t
*listpool
;
2700 uu_avl_index_t idx
= 0;
2701 uu_list_index_t idx2
= 0;
2706 if (strcmp(argv
[0], "groupspace") == 0)
2707 /* Toggle default group types */
2708 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2710 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2726 if (zfs_add_sort_column(&sortcol
, optarg
,
2727 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
2728 (void) fprintf(stderr
,
2729 gettext("invalid field '%s'\n"), optarg
);
2740 (void) fprintf(stderr
, gettext("missing argument for "
2741 "'%c' option\n"), optopt
);
2745 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2755 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2759 (void) fprintf(stderr
, gettext("too many arguments\n"));
2763 /* Use default output fields if not specified using -o */
2767 if ((delim
= strchr(ofield
, ',')) != NULL
)
2769 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
2770 (void) fprintf(stderr
, gettext("invalid type '%s' "
2771 "for -o option\n"), ofield
);
2776 } while (delim
!= NULL
);
2777 fields
[cfield
] = USFIELD_LAST
;
2779 /* Override output types (-t option) */
2780 if (tfield
!= NULL
) {
2784 boolean_t found
= B_FALSE
;
2786 if ((delim
= strchr(tfield
, ',')) != NULL
)
2788 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
2790 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
2792 types
|= us_type_bits
[i
];
2797 (void) fprintf(stderr
, gettext("invalid type "
2798 "'%s' for -t option\n"), tfield
);
2803 } while (delim
!= NULL
);
2806 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
2809 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
2810 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
2812 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
2815 /* Always add default sorting columns */
2816 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
2817 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
2819 cb
.cb_sortcol
= sortcol
;
2820 cb
.cb_numname
= prtnum
;
2821 cb
.cb_nicenum
= !parsable
;
2822 cb
.cb_avl_pool
= avl_pool
;
2823 cb
.cb_avl
= avl_tree
;
2824 cb
.cb_sid2posix
= sid2posix
;
2826 for (i
= 0; i
< USFIELD_LAST
; i
++)
2827 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
2829 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
2830 if (((p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
) &&
2831 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
2832 ((p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
) &&
2833 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))))
2836 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
2841 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
2844 us_populated
= B_TRUE
;
2846 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
2847 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
2848 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
2849 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
2851 while (node
!= NULL
) {
2853 node
= uu_avl_next(avl_tree
, node
);
2854 uu_avl_remove(avl_tree
, rmnode
);
2855 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
2856 uu_list_insert(list
, rmnode
, idx2
);
2859 for (node
= uu_list_first(list
); node
!= NULL
;
2860 node
= uu_list_next(list
, node
)) {
2861 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
2863 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
2864 uu_avl_insert(avl_tree
, node
, idx
);
2867 uu_list_destroy(list
);
2868 uu_list_pool_destroy(listpool
);
2870 /* Print and free node nvlist memory */
2871 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
2874 zfs_free_sort_columns(sortcol
);
2876 /* Clean up the AVL tree */
2877 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
2880 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
2881 uu_avl_remove(cb
.cb_avl
, node
);
2885 uu_avl_walk_end(walk
);
2886 uu_avl_destroy(avl_tree
);
2887 uu_avl_pool_destroy(avl_pool
);
2893 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
2894 * [-t type[,...]] [filesystem|volume|snapshot] ...
2896 * -H Scripted mode; elide headers and separate columns by tabs
2897 * -p Display values in parsable (literal) format.
2898 * -r Recurse over all children
2899 * -d Limit recursion by depth.
2900 * -o Control which fields to display.
2901 * -s Specify sort columns, descending order.
2902 * -S Specify sort columns, ascending order.
2903 * -t Control which object types to display.
2905 * When given no arguments, list all filesystems in the system.
2906 * Otherwise, list the specified datasets, optionally recursing down them if
2907 * '-r' is specified.
2909 typedef struct list_cbdata
{
2911 boolean_t cb_literal
;
2912 boolean_t cb_scripted
;
2913 zprop_list_t
*cb_proplist
;
2917 * Given a list of columns to display, output appropriate headers for each one.
2920 print_header(list_cbdata_t
*cb
)
2922 zprop_list_t
*pl
= cb
->cb_proplist
;
2923 char headerbuf
[ZFS_MAXPROPLEN
];
2926 boolean_t first
= B_TRUE
;
2927 boolean_t right_justify
;
2929 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2936 right_justify
= B_FALSE
;
2937 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2938 header
= zfs_prop_column_name(pl
->pl_prop
);
2939 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2941 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2942 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2943 headerbuf
[i
] = '\0';
2947 if (pl
->pl_next
== NULL
&& !right_justify
)
2948 (void) printf("%s", header
);
2949 else if (right_justify
)
2950 (void) printf("%*s", (int)pl
->pl_width
, header
);
2952 (void) printf("%-*s", (int)pl
->pl_width
, header
);
2955 (void) printf("\n");
2959 * Given a dataset and a list of fields, print out all the properties according
2960 * to the described layout.
2963 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
2965 zprop_list_t
*pl
= cb
->cb_proplist
;
2966 boolean_t first
= B_TRUE
;
2967 char property
[ZFS_MAXPROPLEN
];
2968 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
2971 boolean_t right_justify
;
2973 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2975 if (cb
->cb_scripted
)
2976 (void) printf("\t");
2983 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
2984 (void) strlcpy(property
, zfs_get_name(zhp
),
2987 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2988 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
2989 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
2990 sizeof (property
), NULL
, NULL
, 0,
2991 cb
->cb_literal
) != 0)
2995 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2996 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
2997 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
2998 property
, sizeof (property
), cb
->cb_literal
) != 0)
3002 right_justify
= B_TRUE
;
3003 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
3004 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
3005 property
, sizeof (property
), cb
->cb_literal
) != 0)
3009 right_justify
= B_TRUE
;
3011 if (nvlist_lookup_nvlist(userprops
,
3012 pl
->pl_user_prop
, &propval
) != 0)
3015 verify(nvlist_lookup_string(propval
,
3016 ZPROP_VALUE
, &propstr
) == 0);
3017 right_justify
= B_FALSE
;
3021 * If this is being called in scripted mode, or if this is the
3022 * last column and it is left-justified, don't include a width
3025 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3026 (void) printf("%s", propstr
);
3027 else if (right_justify
)
3028 (void) printf("%*s", (int)pl
->pl_width
, propstr
);
3030 (void) printf("%-*s", (int)pl
->pl_width
, propstr
);
3033 (void) printf("\n");
3037 * Generic callback function to list a dataset or snapshot.
3040 list_callback(zfs_handle_t
*zhp
, void *data
)
3042 list_cbdata_t
*cbp
= data
;
3044 if (cbp
->cb_first
) {
3045 if (!cbp
->cb_scripted
)
3047 cbp
->cb_first
= B_FALSE
;
3050 print_dataset(zhp
, cbp
);
3056 zfs_do_list(int argc
, char **argv
)
3059 static char default_fields
[] =
3060 "name,used,available,referenced,mountpoint";
3061 int types
= ZFS_TYPE_DATASET
;
3062 boolean_t types_specified
= B_FALSE
;
3063 char *fields
= NULL
;
3064 list_cbdata_t cb
= { 0 };
3068 zfs_sort_column_t
*sortcol
= NULL
;
3069 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3072 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3078 cb
.cb_literal
= B_TRUE
;
3079 flags
|= ZFS_ITER_LITERAL_PROPS
;
3082 limit
= parse_depth(optarg
, &flags
);
3085 flags
|= ZFS_ITER_RECURSE
;
3088 cb
.cb_scripted
= B_TRUE
;
3091 if (zfs_add_sort_column(&sortcol
, optarg
,
3093 (void) fprintf(stderr
,
3094 gettext("invalid property '%s'\n"), optarg
);
3099 if (zfs_add_sort_column(&sortcol
, optarg
,
3101 (void) fprintf(stderr
,
3102 gettext("invalid property '%s'\n"), optarg
);
3108 types_specified
= B_TRUE
;
3109 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3110 while (*optarg
!= '\0') {
3111 static char *type_subopts
[] = { "filesystem",
3112 "volume", "snapshot", "snap", "bookmark",
3115 switch (getsubopt(&optarg
, type_subopts
,
3118 types
|= ZFS_TYPE_FILESYSTEM
;
3121 types
|= ZFS_TYPE_VOLUME
;
3125 types
|= ZFS_TYPE_SNAPSHOT
;
3128 types
|= ZFS_TYPE_BOOKMARK
;
3131 types
= ZFS_TYPE_DATASET
|
3135 (void) fprintf(stderr
,
3136 gettext("invalid type '%s'\n"),
3143 (void) fprintf(stderr
, gettext("missing argument for "
3144 "'%c' option\n"), optopt
);
3148 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3158 fields
= default_fields
;
3161 * If we are only going to list snapshot names and sort by name,
3162 * then we can use faster version.
3164 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3165 flags
|= ZFS_ITER_SIMPLE
;
3168 * If "-o space" and no types were specified, don't display snapshots.
3170 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3171 types
&= ~ZFS_TYPE_SNAPSHOT
;
3174 * If the user specifies '-o all', the zprop_get_list() doesn't
3175 * normally include the name of the dataset. For 'zfs list', we always
3176 * want this property to be first.
3178 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3182 cb
.cb_first
= B_TRUE
;
3184 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3185 limit
, list_callback
, &cb
);
3187 zprop_free_list(cb
.cb_proplist
);
3188 zfs_free_sort_columns(sortcol
);
3190 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3191 (void) fprintf(stderr
, gettext("no datasets available\n"));
3197 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3198 * zfs rename [-f] -p <fs | vol> <fs | vol>
3199 * zfs rename -r <snap> <snap>
3201 * Renames the given dataset to another of the same type.
3203 * The '-p' flag creates all the non-existing ancestors of the target first.
3207 zfs_do_rename(int argc
, char **argv
)
3212 boolean_t recurse
= B_FALSE
;
3213 boolean_t parents
= B_FALSE
;
3214 boolean_t force_unmount
= B_FALSE
;
3217 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3226 force_unmount
= B_TRUE
;
3230 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3239 /* check number of arguments */
3241 (void) fprintf(stderr
, gettext("missing source dataset "
3246 (void) fprintf(stderr
, gettext("missing target dataset "
3251 (void) fprintf(stderr
, gettext("too many arguments\n"));
3255 if (recurse
&& parents
) {
3256 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3261 if (recurse
&& strchr(argv
[0], '@') == 0) {
3262 (void) fprintf(stderr
, gettext("source dataset for recursive "
3263 "rename must be a snapshot\n"));
3267 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3268 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3271 /* If we were asked and the name looks good, try to create ancestors. */
3272 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3273 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3278 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3287 * Promotes the given clone fs to be the parent
3291 zfs_do_promote(int argc
, char **argv
)
3297 if (argc
> 1 && argv
[1][0] == '-') {
3298 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3303 /* check number of arguments */
3305 (void) fprintf(stderr
, gettext("missing clone filesystem"
3310 (void) fprintf(stderr
, gettext("too many arguments\n"));
3314 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3318 ret
= (zfs_promote(zhp
) != 0);
3326 * zfs rollback [-rRf] <snapshot>
3328 * -r Delete any intervening snapshots before doing rollback
3329 * -R Delete any snapshots and their clones
3330 * -f ignored for backwards compatability
3332 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3333 * since then and making it the active dataset. If more recent snapshots exist,
3334 * the command will complain unless the '-r' flag is given.
3336 typedef struct rollback_cbdata
{
3342 boolean_t cb_recurse
;
3343 } rollback_cbdata_t
;
3346 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3348 rollback_cbdata_t
*cbp
= data
;
3350 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3351 (void) fprintf(stderr
, gettext("cannot rollback to "
3352 "'%s': clones of previous snapshots exist\n"),
3354 (void) fprintf(stderr
, gettext("use '-R' to "
3355 "force deletion of the following clones and "
3361 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3369 * Report any snapshots more recent than the one specified. Used when '-r' is
3370 * not specified. We reuse this same callback for the snapshot dependents - if
3371 * 'cb_dependent' is set, then this is a dependent and we should report it
3372 * without checking the transaction group.
3375 rollback_check(zfs_handle_t
*zhp
, void *data
)
3377 rollback_cbdata_t
*cbp
= data
;
3379 if (cbp
->cb_doclones
) {
3384 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3385 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3386 (void) fprintf(stderr
, gettext("cannot "
3387 "rollback to '%s': more recent snapshots "
3388 "or bookmarks exist\n"),
3390 (void) fprintf(stderr
, gettext("use '-r' to "
3391 "force deletion of the following "
3392 "snapshots and bookmarks:\n"));
3397 if (cbp
->cb_recurse
) {
3398 if (zfs_iter_dependents(zhp
, B_TRUE
,
3399 rollback_check_dependent
, cbp
) != 0) {
3404 (void) fprintf(stderr
, "%s\n",
3413 zfs_do_rollback(int argc
, char **argv
)
3417 boolean_t force
= B_FALSE
;
3418 rollback_cbdata_t cb
= { 0 };
3419 zfs_handle_t
*zhp
, *snap
;
3420 char parentname
[ZFS_MAXNAMELEN
];
3424 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3437 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3446 /* check number of arguments */
3448 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3452 (void) fprintf(stderr
, gettext("too many arguments\n"));
3456 /* open the snapshot */
3457 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3460 /* open the parent dataset */
3461 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3462 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3464 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3470 * Check for more recent snapshots and/or clones based on the presence
3473 cb
.cb_target
= argv
[0];
3474 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3475 cb
.cb_first
= B_TRUE
;
3477 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, rollback_check
, &cb
)) != 0)
3479 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
3482 if ((ret
= cb
.cb_error
) != 0)
3486 * Rollback parent to the given snapshot.
3488 ret
= zfs_rollback(zhp
, snap
, force
);
3501 * zfs set property=value ... { fs | snap | vol } ...
3503 * Sets the given properties for all datasets specified on the command line.
3507 set_callback(zfs_handle_t
*zhp
, void *data
)
3509 nvlist_t
*props
= data
;
3511 if (zfs_prop_set_list(zhp
, props
) != 0) {
3512 switch (libzfs_errno(g_zfs
)) {
3513 case EZFS_MOUNTFAILED
:
3514 (void) fprintf(stderr
, gettext("property may be set "
3515 "but unable to remount filesystem\n"));
3517 case EZFS_SHARENFSFAILED
:
3518 (void) fprintf(stderr
, gettext("property may be set "
3519 "but unable to reshare filesystem\n"));
3528 zfs_do_set(int argc
, char **argv
)
3530 nvlist_t
*props
= NULL
;
3531 int ds_start
= -1; /* argv idx of first dataset arg */
3535 /* check for options */
3536 if (argc
> 1 && argv
[1][0] == '-') {
3537 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3542 /* check number of arguments */
3544 (void) fprintf(stderr
, gettext("missing arguments\n"));
3548 if (strchr(argv
[1], '=') == NULL
) {
3549 (void) fprintf(stderr
, gettext("missing property=value "
3552 (void) fprintf(stderr
, gettext("missing dataset "
3558 /* validate argument order: prop=val args followed by dataset args */
3559 for (i
= 1; i
< argc
; i
++) {
3560 if (strchr(argv
[i
], '=') != NULL
) {
3562 /* out-of-order prop=val argument */
3563 (void) fprintf(stderr
, gettext("invalid "
3564 "argument order\n"));
3567 } else if (ds_start
< 0) {
3572 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
3576 /* Populate a list of property settings */
3577 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3579 for (i
= 1; i
< ds_start
; i
++) {
3580 if ((ret
= parseprop(props
, argv
[i
])) != 0)
3584 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
3585 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, props
);
3592 typedef struct snap_cbdata
{
3594 boolean_t sd_recursive
;
3595 const char *sd_snapname
;
3599 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3601 snap_cbdata_t
*sd
= arg
;
3606 if (sd
->sd_recursive
&&
3607 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
3612 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3615 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3618 if (sd
->sd_recursive
)
3619 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3625 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3627 * Creates a snapshot with the given name. While functionally equivalent to
3628 * 'zfs create', it is a separate command to differentiate intent.
3631 zfs_do_snapshot(int argc
, char **argv
)
3636 snap_cbdata_t sd
= { 0 };
3637 boolean_t multiple_snaps
= B_FALSE
;
3639 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3641 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3645 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3648 if (parseprop(props
, optarg
) != 0)
3652 sd
.sd_recursive
= B_TRUE
;
3653 multiple_snaps
= B_TRUE
;
3656 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3665 /* check number of arguments */
3667 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3672 multiple_snaps
= B_TRUE
;
3673 for (; argc
> 0; argc
--, argv
++) {
3677 atp
= strchr(argv
[0], '@');
3681 sd
.sd_snapname
= atp
+ 1;
3682 zhp
= zfs_open(g_zfs
, argv
[0],
3683 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3686 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3690 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3691 nvlist_free(sd
.sd_nvl
);
3693 if (ret
!= 0 && multiple_snaps
)
3694 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3698 nvlist_free(sd
.sd_nvl
);
3705 * Send a backup stream to stdout.
3708 zfs_do_send(int argc
, char **argv
)
3710 char *fromname
= NULL
;
3711 char *toname
= NULL
;
3712 char *resume_token
= NULL
;
3715 sendflags_t flags
= { 0 };
3717 nvlist_t
*dbgnv
= NULL
;
3718 boolean_t extraverbose
= B_FALSE
;
3721 while ((c
= getopt(argc
, argv
, ":i:I:RDpvnPLet:")) != -1) {
3732 flags
.doall
= B_TRUE
;
3735 flags
.replicate
= B_TRUE
;
3738 flags
.props
= B_TRUE
;
3741 flags
.parsable
= B_TRUE
;
3742 flags
.verbose
= B_TRUE
;
3746 extraverbose
= B_TRUE
;
3747 flags
.verbose
= B_TRUE
;
3748 flags
.progress
= B_TRUE
;
3751 flags
.dedup
= B_TRUE
;
3754 flags
.dryrun
= B_TRUE
;
3757 flags
.largeblock
= B_TRUE
;
3760 flags
.embed_data
= B_TRUE
;
3763 resume_token
= optarg
;
3766 (void) fprintf(stderr
, gettext("missing argument for "
3767 "'%c' option\n"), optopt
);
3771 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3780 if (resume_token
!= NULL
) {
3781 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
3783 (void) fprintf(stderr
,
3784 gettext("invalid flags combined with -t\n"));
3788 (void) fprintf(stderr
, gettext("no additional "
3789 "arguments are permitted with -t\n"));
3794 (void) fprintf(stderr
,
3795 gettext("missing snapshot argument\n"));
3799 (void) fprintf(stderr
, gettext("too many arguments\n"));
3804 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
3805 (void) fprintf(stderr
,
3806 gettext("Error: Stream can not be written to a terminal.\n"
3807 "You must redirect standard output.\n"));
3811 if (resume_token
!= NULL
) {
3812 return (zfs_send_resume(g_zfs
, &flags
, STDOUT_FILENO
,
3817 * Special case sending a filesystem, or from a bookmark.
3819 if (strchr(argv
[0], '@') == NULL
||
3820 (fromname
&& strchr(fromname
, '#') != NULL
)) {
3821 char frombuf
[ZFS_MAXNAMELEN
];
3822 enum lzc_send_flags lzc_flags
= 0;
3824 if (flags
.replicate
|| flags
.doall
|| flags
.props
||
3825 flags
.dedup
|| flags
.dryrun
|| flags
.verbose
||
3827 (void) fprintf(stderr
,
3829 "Unsupported flag with filesystem or bookmark.\n"));
3833 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
3837 if (flags
.largeblock
)
3838 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
3839 if (flags
.embed_data
)
3840 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
3842 if (fromname
!= NULL
&&
3843 (fromname
[0] == '#' || fromname
[0] == '@')) {
3845 * Incremental source name begins with # or @.
3846 * Default to same fs as target.
3848 (void) strncpy(frombuf
, argv
[0], sizeof (frombuf
));
3849 cp
= strchr(frombuf
, '@');
3852 (void) strlcat(frombuf
, fromname
, sizeof (frombuf
));
3855 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, lzc_flags
);
3860 cp
= strchr(argv
[0], '@');
3863 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3868 * If they specified the full path to the snapshot, chop off
3869 * everything except the short name of the snapshot, but special
3870 * case if they specify the origin.
3872 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
3873 char origin
[ZFS_MAXNAMELEN
];
3876 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
3877 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
3879 if (strcmp(origin
, fromname
) == 0) {
3881 flags
.fromorigin
= B_TRUE
;
3884 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
3885 (void) fprintf(stderr
,
3886 gettext("incremental source must be "
3887 "in same filesystem\n"));
3891 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
3892 (void) fprintf(stderr
,
3893 gettext("invalid incremental source\n"));
3899 if (flags
.replicate
&& fromname
== NULL
)
3900 flags
.doall
= B_TRUE
;
3902 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
3903 extraverbose
? &dbgnv
: NULL
);
3905 if (extraverbose
&& dbgnv
!= NULL
) {
3907 * dump_nvlist prints to stdout, but that's been
3908 * redirected to a file. Make it print to stderr
3911 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
3912 dump_nvlist(dbgnv
, 0);
3921 * Restore a backup stream from stdin.
3924 zfs_do_receive(int argc
, char **argv
)
3927 recvflags_t flags
= { 0 };
3928 boolean_t abort_resumable
= B_FALSE
;
3931 nvpair_t
*nvp
= NULL
;
3933 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3937 while ((c
= getopt(argc
, argv
, ":o:denuvFsA")) != -1) {
3940 if (parseprop(props
, optarg
) != 0)
3944 flags
.isprefix
= B_TRUE
;
3947 flags
.isprefix
= B_TRUE
;
3948 flags
.istail
= B_TRUE
;
3951 flags
.dryrun
= B_TRUE
;
3954 flags
.nomount
= B_TRUE
;
3957 flags
.verbose
= B_TRUE
;
3960 flags
.resumable
= B_TRUE
;
3963 flags
.force
= B_TRUE
;
3966 abort_resumable
= B_TRUE
;
3969 (void) fprintf(stderr
, gettext("missing argument for "
3970 "'%c' option\n"), optopt
);
3974 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3983 /* check number of arguments */
3985 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3989 (void) fprintf(stderr
, gettext("too many arguments\n"));
3993 while ((nvp
= nvlist_next_nvpair(props
, nvp
))) {
3994 if (strcmp(nvpair_name(nvp
), "origin") != 0) {
3995 (void) fprintf(stderr
, gettext("invalid option"));
4000 if (abort_resumable
) {
4001 if (flags
.isprefix
|| flags
.istail
|| flags
.dryrun
||
4002 flags
.resumable
|| flags
.nomount
) {
4003 (void) fprintf(stderr
, gettext("invalid option"));
4007 char namebuf
[ZFS_MAXNAMELEN
];
4008 (void) snprintf(namebuf
, sizeof (namebuf
),
4009 "%s/%%recv", argv
[0]);
4011 if (zfs_dataset_exists(g_zfs
, namebuf
,
4012 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) {
4013 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4014 namebuf
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4017 err
= zfs_destroy(zhp
, B_FALSE
);
4019 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4020 argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4023 if (!zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) ||
4024 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
4025 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == -1) {
4026 (void) fprintf(stderr
,
4027 gettext("'%s' does not have any "
4028 "resumable receive state to abort\n"),
4032 err
= zfs_destroy(zhp
, B_FALSE
);
4038 if (isatty(STDIN_FILENO
)) {
4039 (void) fprintf(stderr
,
4040 gettext("Error: Backup stream can not be read "
4041 "from a terminal.\n"
4042 "You must redirect standard input.\n"));
4045 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
4051 * allow/unallow stuff
4053 /* copied from zfs/sys/dsl_deleg.h */
4054 #define ZFS_DELEG_PERM_CREATE "create"
4055 #define ZFS_DELEG_PERM_DESTROY "destroy"
4056 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4057 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4058 #define ZFS_DELEG_PERM_CLONE "clone"
4059 #define ZFS_DELEG_PERM_PROMOTE "promote"
4060 #define ZFS_DELEG_PERM_RENAME "rename"
4061 #define ZFS_DELEG_PERM_MOUNT "mount"
4062 #define ZFS_DELEG_PERM_SHARE "share"
4063 #define ZFS_DELEG_PERM_SEND "send"
4064 #define ZFS_DELEG_PERM_RECEIVE "receive"
4065 #define ZFS_DELEG_PERM_ALLOW "allow"
4066 #define ZFS_DELEG_PERM_USERPROP "userprop"
4067 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4068 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4069 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4070 #define ZFS_DELEG_PERM_USERUSED "userused"
4071 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4072 #define ZFS_DELEG_PERM_HOLD "hold"
4073 #define ZFS_DELEG_PERM_RELEASE "release"
4074 #define ZFS_DELEG_PERM_DIFF "diff"
4075 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4077 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4079 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
4080 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
4081 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
4082 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
4083 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
4084 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
4085 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
4086 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
4087 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
4088 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
4089 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
4090 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
4091 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
4092 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
4093 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
4094 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
4095 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
4097 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
4098 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
4099 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
4100 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
4101 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
4102 { NULL
, ZFS_DELEG_NOTE_NONE
}
4105 /* permission structure */
4106 typedef struct deleg_perm
{
4107 zfs_deleg_who_type_t dp_who_type
;
4108 const char *dp_name
;
4110 boolean_t dp_descend
;
4114 typedef struct deleg_perm_node
{
4115 deleg_perm_t dpn_perm
;
4117 uu_avl_node_t dpn_avl_node
;
4118 } deleg_perm_node_t
;
4120 typedef struct fs_perm fs_perm_t
;
4122 /* permissions set */
4123 typedef struct who_perm
{
4124 zfs_deleg_who_type_t who_type
;
4125 const char *who_name
; /* id */
4126 char who_ug_name
[256]; /* user/group name */
4127 fs_perm_t
*who_fsperm
; /* uplink */
4129 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
4133 typedef struct who_perm_node
{
4134 who_perm_t who_perm
;
4135 uu_avl_node_t who_avl_node
;
4138 typedef struct fs_perm_set fs_perm_set_t
;
4139 /* fs permissions */
4141 const char *fsp_name
;
4143 uu_avl_t
*fsp_sc_avl
; /* sets,create */
4144 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
4146 fs_perm_set_t
*fsp_set
; /* uplink */
4150 typedef struct fs_perm_node
{
4151 fs_perm_t fspn_fsperm
;
4154 uu_list_node_t fspn_list_node
;
4157 /* top level structure */
4158 struct fs_perm_set
{
4159 uu_list_pool_t
*fsps_list_pool
;
4160 uu_list_t
*fsps_list
; /* list of fs_perms */
4162 uu_avl_pool_t
*fsps_named_set_avl_pool
;
4163 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
4164 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
4167 static inline const char *
4168 deleg_perm_type(zfs_deleg_note_t note
)
4174 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4175 case ZFS_DELEG_NOTE_GROUPUSED
:
4176 case ZFS_DELEG_NOTE_USERPROP
:
4177 case ZFS_DELEG_NOTE_USERQUOTA
:
4178 case ZFS_DELEG_NOTE_USERUSED
:
4180 return (gettext("other"));
4182 return (gettext("subcommand"));
4187 who_type2weight(zfs_deleg_who_type_t who_type
)
4191 case ZFS_DELEG_NAMED_SET_SETS
:
4192 case ZFS_DELEG_NAMED_SET
:
4195 case ZFS_DELEG_CREATE_SETS
:
4196 case ZFS_DELEG_CREATE
:
4199 case ZFS_DELEG_USER_SETS
:
4200 case ZFS_DELEG_USER
:
4203 case ZFS_DELEG_GROUP_SETS
:
4204 case ZFS_DELEG_GROUP
:
4207 case ZFS_DELEG_EVERYONE_SETS
:
4208 case ZFS_DELEG_EVERYONE
:
4220 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4222 const who_perm_node_t
*l
= larg
;
4223 const who_perm_node_t
*r
= rarg
;
4224 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
4225 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
4226 int lweight
= who_type2weight(ltype
);
4227 int rweight
= who_type2weight(rtype
);
4228 int res
= lweight
- rweight
;
4230 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
4231 ZFS_MAX_DELEG_NAME
-1);
4243 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4245 const deleg_perm_node_t
*l
= larg
;
4246 const deleg_perm_node_t
*r
= rarg
;
4247 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
4248 ZFS_MAX_DELEG_NAME
-1);
4260 fs_perm_set_init(fs_perm_set_t
*fspset
)
4262 bzero(fspset
, sizeof (fs_perm_set_t
));
4264 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4265 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4266 NULL
, UU_DEFAULT
)) == NULL
)
4268 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4269 UU_DEFAULT
)) == NULL
)
4272 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4273 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4274 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4275 UU_DEFAULT
)) == NULL
)
4278 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4279 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4280 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4281 UU_DEFAULT
)) == NULL
)
4284 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4285 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4286 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4291 static inline void fs_perm_fini(fs_perm_t
*);
4292 static inline void who_perm_fini(who_perm_t
*);
4295 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4297 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4299 while (node
!= NULL
) {
4300 fs_perm_node_t
*next_node
=
4301 uu_list_next(fspset
->fsps_list
, node
);
4302 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4303 fs_perm_fini(fsperm
);
4304 uu_list_remove(fspset
->fsps_list
, node
);
4309 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4310 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4311 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4315 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4318 deleg_perm
->dp_who_type
= type
;
4319 deleg_perm
->dp_name
= name
;
4323 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4324 zfs_deleg_who_type_t type
, const char *name
)
4326 uu_avl_pool_t
*pool
;
4327 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4329 bzero(who_perm
, sizeof (who_perm_t
));
4331 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4332 UU_DEFAULT
)) == NULL
)
4335 who_perm
->who_type
= type
;
4336 who_perm
->who_name
= name
;
4337 who_perm
->who_fsperm
= fsperm
;
4341 who_perm_fini(who_perm_t
*who_perm
)
4343 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4345 while (node
!= NULL
) {
4346 deleg_perm_node_t
*next_node
=
4347 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4349 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4354 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4358 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4360 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4361 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4363 bzero(fsperm
, sizeof (fs_perm_t
));
4365 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4369 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4373 fsperm
->fsp_set
= fspset
;
4374 fsperm
->fsp_name
= fsname
;
4378 fs_perm_fini(fs_perm_t
*fsperm
)
4380 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4381 while (node
!= NULL
) {
4382 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4384 who_perm_t
*who_perm
= &node
->who_perm
;
4385 who_perm_fini(who_perm
);
4386 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4391 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4392 while (node
!= NULL
) {
4393 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4395 who_perm_t
*who_perm
= &node
->who_perm
;
4396 who_perm_fini(who_perm
);
4397 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4402 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4403 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4407 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4408 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4410 uu_avl_index_t idx
= 0;
4412 deleg_perm_node_t
*found_node
= NULL
;
4413 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4415 deleg_perm_init(deleg_perm
, who_type
, name
);
4417 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4419 uu_avl_insert(avl
, node
, idx
);
4422 deleg_perm
= &node
->dpn_perm
;
4427 case ZFS_DELEG_LOCAL
:
4428 deleg_perm
->dp_local
= B_TRUE
;
4430 case ZFS_DELEG_DESCENDENT
:
4431 deleg_perm
->dp_descend
= B_TRUE
;
4436 assert(B_FALSE
); /* invalid locality */
4441 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4443 nvpair_t
*nvp
= NULL
;
4444 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4445 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4446 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4448 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4449 const char *name
= nvpair_name(nvp
);
4450 data_type_t type
= nvpair_type(nvp
);
4451 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4452 deleg_perm_node_t
*node
=
4453 safe_malloc(sizeof (deleg_perm_node_t
));
4455 VERIFY(type
== DATA_TYPE_BOOLEAN
);
4457 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4458 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4465 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4467 nvpair_t
*nvp
= NULL
;
4468 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4470 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4471 nvlist_t
*nvl2
= NULL
;
4472 const char *name
= nvpair_name(nvp
);
4473 uu_avl_t
*avl
= NULL
;
4474 uu_avl_pool_t
*avl_pool
= NULL
;
4475 zfs_deleg_who_type_t perm_type
= name
[0];
4476 char perm_locality
= name
[1];
4477 const char *perm_name
= name
+ 3;
4478 boolean_t is_set
= B_TRUE
;
4479 who_perm_t
*who_perm
= NULL
;
4481 assert('$' == name
[2]);
4483 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4486 switch (perm_type
) {
4487 case ZFS_DELEG_CREATE
:
4488 case ZFS_DELEG_CREATE_SETS
:
4489 case ZFS_DELEG_NAMED_SET
:
4490 case ZFS_DELEG_NAMED_SET_SETS
:
4491 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4492 avl
= fsperm
->fsp_sc_avl
;
4494 case ZFS_DELEG_USER
:
4495 case ZFS_DELEG_USER_SETS
:
4496 case ZFS_DELEG_GROUP
:
4497 case ZFS_DELEG_GROUP_SETS
:
4498 case ZFS_DELEG_EVERYONE
:
4499 case ZFS_DELEG_EVERYONE_SETS
:
4500 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4501 avl
= fsperm
->fsp_uge_avl
;
4508 who_perm_node_t
*found_node
= NULL
;
4509 who_perm_node_t
*node
= safe_malloc(
4510 sizeof (who_perm_node_t
));
4511 who_perm
= &node
->who_perm
;
4512 uu_avl_index_t idx
= 0;
4514 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4515 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4517 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4519 if (avl
== fsperm
->fsp_uge_avl
) {
4521 struct passwd
*p
= NULL
;
4522 struct group
*g
= NULL
;
4523 const char *nice_name
= NULL
;
4525 switch (perm_type
) {
4526 case ZFS_DELEG_USER_SETS
:
4527 case ZFS_DELEG_USER
:
4528 rid
= atoi(perm_name
);
4531 nice_name
= p
->pw_name
;
4533 case ZFS_DELEG_GROUP_SETS
:
4534 case ZFS_DELEG_GROUP
:
4535 rid
= atoi(perm_name
);
4538 nice_name
= g
->gr_name
;
4544 if (nice_name
!= NULL
)
4546 node
->who_perm
.who_ug_name
,
4550 uu_avl_insert(avl
, node
, idx
);
4553 who_perm
= &node
->who_perm
;
4557 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4564 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4566 nvpair_t
*nvp
= NULL
;
4567 uu_avl_index_t idx
= 0;
4569 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4570 nvlist_t
*nvl2
= NULL
;
4571 const char *fsname
= nvpair_name(nvp
);
4572 data_type_t type
= nvpair_type(nvp
);
4573 fs_perm_t
*fsperm
= NULL
;
4574 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4578 fsperm
= &node
->fspn_fsperm
;
4580 VERIFY(DATA_TYPE_NVLIST
== type
);
4582 uu_list_node_init(node
, &node
->fspn_list_node
,
4583 fspset
->fsps_list_pool
);
4585 idx
= uu_list_numnodes(fspset
->fsps_list
);
4586 fs_perm_init(fsperm
, fspset
, fsname
);
4588 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4591 (void) parse_fs_perm(fsperm
, nvl2
);
4593 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4599 static inline const char *
4600 deleg_perm_comment(zfs_deleg_note_t note
)
4602 const char *str
= "";
4607 case ZFS_DELEG_NOTE_ALLOW
:
4608 str
= gettext("Must also have the permission that is being"
4609 "\n\t\t\t\tallowed");
4611 case ZFS_DELEG_NOTE_CLONE
:
4612 str
= gettext("Must also have the 'create' ability and 'mount'"
4613 "\n\t\t\t\tability in the origin file system");
4615 case ZFS_DELEG_NOTE_CREATE
:
4616 str
= gettext("Must also have the 'mount' ability");
4618 case ZFS_DELEG_NOTE_DESTROY
:
4619 str
= gettext("Must also have the 'mount' ability");
4621 case ZFS_DELEG_NOTE_DIFF
:
4622 str
= gettext("Allows lookup of paths within a dataset;"
4623 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4624 "\n\t\t\t\tin order to use zfs diff");
4626 case ZFS_DELEG_NOTE_HOLD
:
4627 str
= gettext("Allows adding a user hold to a snapshot");
4629 case ZFS_DELEG_NOTE_MOUNT
:
4630 str
= gettext("Allows mount/umount of ZFS datasets");
4632 case ZFS_DELEG_NOTE_PROMOTE
:
4633 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4634 " 'promote' ability in the origin file system");
4636 case ZFS_DELEG_NOTE_RECEIVE
:
4637 str
= gettext("Must also have the 'mount' and 'create'"
4640 case ZFS_DELEG_NOTE_RELEASE
:
4641 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4642 "might destroy the snapshot");
4644 case ZFS_DELEG_NOTE_RENAME
:
4645 str
= gettext("Must also have the 'mount' and 'create'"
4646 "\n\t\t\t\tability in the new parent");
4648 case ZFS_DELEG_NOTE_ROLLBACK
:
4651 case ZFS_DELEG_NOTE_SEND
:
4654 case ZFS_DELEG_NOTE_SHARE
:
4655 str
= gettext("Allows sharing file systems over NFS or SMB"
4656 "\n\t\t\t\tprotocols");
4658 case ZFS_DELEG_NOTE_SNAPSHOT
:
4662 * case ZFS_DELEG_NOTE_VSCAN:
4663 * str = gettext("");
4667 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4668 str
= gettext("Allows accessing any groupquota@... property");
4670 case ZFS_DELEG_NOTE_GROUPUSED
:
4671 str
= gettext("Allows reading any groupused@... property");
4673 case ZFS_DELEG_NOTE_USERPROP
:
4674 str
= gettext("Allows changing any user property");
4676 case ZFS_DELEG_NOTE_USERQUOTA
:
4677 str
= gettext("Allows accessing any userquota@... property");
4679 case ZFS_DELEG_NOTE_USERUSED
:
4680 str
= gettext("Allows reading any userused@... property");
4698 boolean_t recursive
; /* unallow only */
4699 boolean_t prt_usage
;
4701 boolean_t prt_perms
;
4704 const char *dataset
;
4708 prop_cmp(const void *a
, const void *b
)
4710 const char *str1
= *(const char **)a
;
4711 const char *str2
= *(const char **)b
;
4712 return (strcmp(str1
, str2
));
4716 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
4718 const char *opt_desc
[] = {
4719 "-h", gettext("show this help message and exit"),
4720 "-l", gettext("set permission locally"),
4721 "-d", gettext("set permission for descents"),
4722 "-u", gettext("set permission for user"),
4723 "-g", gettext("set permission for group"),
4724 "-e", gettext("set permission for everyone"),
4725 "-c", gettext("set create time permission"),
4726 "-s", gettext("define permission set"),
4728 "-r", gettext("remove permissions recursively"),
4730 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
4731 size_t allow_size
= unallow_size
- 2;
4732 const char *props
[ZFS_NUM_PROPS
];
4735 FILE *fp
= requested
? stdout
: stderr
;
4736 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
4737 const char *fmt
= gettext("%-16s %-14s\t%s\n");
4739 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
4741 (void) fprintf(fp
, gettext("Options:\n"));
4742 for (i
= 0; i
< (un
? unallow_size
: allow_size
); i
++) {
4743 const char *opt
= opt_desc
[i
++];
4744 const char *optdsc
= opt_desc
[i
];
4745 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
4748 (void) fprintf(fp
, gettext("\nThe following permissions are "
4750 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
4752 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
4753 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
4754 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
4755 const char *perm_type
= deleg_perm_type(perm_note
);
4756 const char *perm_comment
= deleg_perm_comment(perm_note
);
4757 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
4760 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
4761 zprop_desc_t
*pd
= &pdtbl
[i
];
4762 if (pd
->pd_visible
!= B_TRUE
)
4765 if (pd
->pd_attr
== PROP_READONLY
)
4768 props
[count
++] = pd
->pd_name
;
4770 props
[count
] = NULL
;
4772 qsort(props
, count
, sizeof (char *), prop_cmp
);
4774 for (i
= 0; i
< count
; i
++)
4775 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
4778 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
4780 exit(requested
? 0 : 2);
4783 static inline const char *
4784 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
4787 if (un
&& argc
== expected_argc
- 1)
4789 else if (argc
== expected_argc
)
4790 *permsp
= argv
[argc
- 2];
4792 allow_usage(un
, B_FALSE
,
4793 gettext("wrong number of parameters\n"));
4795 return (argv
[argc
- 1]);
4799 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
4801 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
4802 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
4803 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
4804 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
4807 allow_usage(un
, B_FALSE
,
4808 gettext("-u, -g, and -e are mutually exclusive\n"));
4810 if (opts
->prt_usage
) {
4811 if (argc
== 0 && all_sum
== 0)
4812 allow_usage(un
, B_TRUE
, NULL
);
4819 allow_usage(un
, B_FALSE
,
4820 gettext("invalid options combined with -s\n"));
4822 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4823 if (argv
[0][0] != '@')
4824 allow_usage(un
, B_FALSE
,
4825 gettext("invalid set name: missing '@' prefix\n"));
4826 opts
->who
= argv
[0];
4827 } else if (opts
->create
) {
4828 if (ldcsuge_sum
> 1)
4829 allow_usage(un
, B_FALSE
,
4830 gettext("invalid options combined with -c\n"));
4831 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4832 } else if (opts
->everyone
) {
4834 allow_usage(un
, B_FALSE
,
4835 gettext("invalid options combined with -e\n"));
4836 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4837 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
4839 opts
->everyone
= B_TRUE
;
4842 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4843 } else if (argc
== 1 && !un
) {
4844 opts
->prt_perms
= B_TRUE
;
4845 opts
->dataset
= argv
[argc
-1];
4847 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4848 opts
->who
= argv
[0];
4851 if (!opts
->local
&& !opts
->descend
) {
4852 opts
->local
= B_TRUE
;
4853 opts
->descend
= B_TRUE
;
4858 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
4859 const char *who
, char *perms
, nvlist_t
*top_nvl
)
4862 char ld
[2] = { '\0', '\0' };
4863 char who_buf
[ZFS_MAXNAMELEN
+32];
4864 char base_type
= ZFS_DELEG_WHO_UNKNOWN
;
4865 char set_type
= ZFS_DELEG_WHO_UNKNOWN
;
4866 nvlist_t
*base_nvl
= NULL
;
4867 nvlist_t
*set_nvl
= NULL
;
4870 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4872 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4876 case ZFS_DELEG_NAMED_SET_SETS
:
4877 case ZFS_DELEG_NAMED_SET
:
4878 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
4879 base_type
= ZFS_DELEG_NAMED_SET
;
4880 ld
[0] = ZFS_DELEG_NA
;
4882 case ZFS_DELEG_CREATE_SETS
:
4883 case ZFS_DELEG_CREATE
:
4884 set_type
= ZFS_DELEG_CREATE_SETS
;
4885 base_type
= ZFS_DELEG_CREATE
;
4886 ld
[0] = ZFS_DELEG_NA
;
4888 case ZFS_DELEG_USER_SETS
:
4889 case ZFS_DELEG_USER
:
4890 set_type
= ZFS_DELEG_USER_SETS
;
4891 base_type
= ZFS_DELEG_USER
;
4893 ld
[0] = ZFS_DELEG_LOCAL
;
4895 ld
[1] = ZFS_DELEG_DESCENDENT
;
4897 case ZFS_DELEG_GROUP_SETS
:
4898 case ZFS_DELEG_GROUP
:
4899 set_type
= ZFS_DELEG_GROUP_SETS
;
4900 base_type
= ZFS_DELEG_GROUP
;
4902 ld
[0] = ZFS_DELEG_LOCAL
;
4904 ld
[1] = ZFS_DELEG_DESCENDENT
;
4906 case ZFS_DELEG_EVERYONE_SETS
:
4907 case ZFS_DELEG_EVERYONE
:
4908 set_type
= ZFS_DELEG_EVERYONE_SETS
;
4909 base_type
= ZFS_DELEG_EVERYONE
;
4911 ld
[0] = ZFS_DELEG_LOCAL
;
4913 ld
[1] = ZFS_DELEG_DESCENDENT
;
4918 if (perms
!= NULL
) {
4920 char *end
= curr
+ strlen(perms
);
4922 while (curr
< end
) {
4923 char *delim
= strchr(curr
, ',');
4934 (void) nvlist_add_boolean(nvl
, curr
);
4940 for (i
= 0; i
< 2; i
++) {
4941 char locality
= ld
[i
];
4945 if (!nvlist_empty(base_nvl
)) {
4947 (void) snprintf(who_buf
,
4948 sizeof (who_buf
), "%c%c$%s",
4949 base_type
, locality
, who
);
4951 (void) snprintf(who_buf
,
4952 sizeof (who_buf
), "%c%c$",
4953 base_type
, locality
);
4955 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
4960 if (!nvlist_empty(set_nvl
)) {
4962 (void) snprintf(who_buf
,
4963 sizeof (who_buf
), "%c%c$%s",
4964 set_type
, locality
, who
);
4966 (void) snprintf(who_buf
,
4967 sizeof (who_buf
), "%c%c$",
4968 set_type
, locality
);
4970 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
4975 for (i
= 0; i
< 2; i
++) {
4976 char locality
= ld
[i
];
4981 (void) snprintf(who_buf
, sizeof (who_buf
),
4982 "%c%c$%s", base_type
, locality
, who
);
4984 (void) snprintf(who_buf
, sizeof (who_buf
),
4985 "%c%c$", base_type
, locality
);
4986 (void) nvlist_add_boolean(top_nvl
, who_buf
);
4989 (void) snprintf(who_buf
, sizeof (who_buf
),
4990 "%c%c$%s", set_type
, locality
, who
);
4992 (void) snprintf(who_buf
, sizeof (who_buf
),
4993 "%c%c$", set_type
, locality
);
4994 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5000 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
5002 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
5006 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
5007 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
5008 } else if (opts
->create
) {
5009 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
5010 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5011 } else if (opts
->everyone
) {
5012 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
5013 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5015 char *curr
= opts
->who
;
5016 char *end
= curr
+ strlen(curr
);
5018 while (curr
< end
) {
5020 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
5022 char *delim
= strchr(curr
, ',');
5025 struct passwd
*p
= NULL
;
5026 struct group
*g
= NULL
;
5034 rid
= (uid_t
)strtol(curr
, &endch
, 0);
5036 who_type
= ZFS_DELEG_USER
;
5045 (void) snprintf(errbuf
, 256, gettext(
5046 "invalid user %s"), curr
);
5047 allow_usage(un
, B_TRUE
, errbuf
);
5049 } else if (opts
->group
) {
5050 who_type
= ZFS_DELEG_GROUP
;
5059 (void) snprintf(errbuf
, 256, gettext(
5060 "invalid group %s"), curr
);
5061 allow_usage(un
, B_TRUE
, errbuf
);
5064 if (*endch
!= '\0') {
5071 if (*endch
!= '\0') {
5079 who_type
= ZFS_DELEG_USER
;
5081 } else if (g
!= NULL
) {
5082 who_type
= ZFS_DELEG_GROUP
;
5085 (void) snprintf(errbuf
, 256, gettext(
5086 "invalid user/group %s"), curr
);
5087 allow_usage(un
, B_TRUE
, errbuf
);
5091 (void) sprintf(id
, "%u", rid
);
5094 store_allow_perm(who_type
, opts
->local
,
5095 opts
->descend
, who
, opts
->perms
, *nvlp
);
5104 print_set_creat_perms(uu_avl_t
*who_avl
)
5106 const char *sc_title
[] = {
5107 gettext("Permission sets:\n"),
5108 gettext("Create time permissions:\n"),
5111 const char **title_ptr
= sc_title
;
5112 who_perm_node_t
*who_node
= NULL
;
5113 int prev_weight
= -1;
5115 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
5116 who_node
= uu_avl_next(who_avl
, who_node
)) {
5117 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5118 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5119 const char *who_name
= who_node
->who_perm
.who_name
;
5120 int weight
= who_type2weight(who_type
);
5121 boolean_t first
= B_TRUE
;
5122 deleg_perm_node_t
*deleg_node
;
5124 if (prev_weight
!= weight
) {
5125 (void) printf("%s", *title_ptr
++);
5126 prev_weight
= weight
;
5129 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
5130 (void) printf("\t");
5132 (void) printf("\t%s ", who_name
);
5134 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
5135 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5138 deleg_node
->dpn_perm
.dp_name
);
5141 (void) printf(",%s",
5142 deleg_node
->dpn_perm
.dp_name
);
5145 (void) printf("\n");
5150 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
5153 who_perm_node_t
*who_node
= NULL
;
5154 boolean_t prt_title
= B_TRUE
;
5155 uu_avl_walk_t
*walk
;
5157 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
5160 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
5161 const char *who_name
= who_node
->who_perm
.who_name
;
5162 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
5163 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5164 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5166 deleg_perm_node_t
*deleg_node
;
5167 boolean_t prt_who
= B_TRUE
;
5169 for (deleg_node
= uu_avl_first(avl
);
5171 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5172 if (local
!= deleg_node
->dpn_perm
.dp_local
||
5173 descend
!= deleg_node
->dpn_perm
.dp_descend
)
5177 const char *who
= NULL
;
5179 prt_title
= B_FALSE
;
5180 (void) printf("%s", title
);
5184 case ZFS_DELEG_USER_SETS
:
5185 case ZFS_DELEG_USER
:
5186 who
= gettext("user");
5188 who_name
= nice_who_name
;
5190 case ZFS_DELEG_GROUP_SETS
:
5191 case ZFS_DELEG_GROUP
:
5192 who
= gettext("group");
5194 who_name
= nice_who_name
;
5196 case ZFS_DELEG_EVERYONE_SETS
:
5197 case ZFS_DELEG_EVERYONE
:
5198 who
= gettext("everyone");
5205 if (who_name
== NULL
)
5206 (void) printf("\t%s", who
);
5208 (void) printf("\t%s %s", who
, who_name
);
5211 (void) printf("%c%s", delim
,
5212 deleg_node
->dpn_perm
.dp_name
);
5217 (void) printf("\n");
5220 uu_avl_walk_end(walk
);
5224 print_fs_perms(fs_perm_set_t
*fspset
)
5226 fs_perm_node_t
*node
= NULL
;
5227 char buf
[ZFS_MAXNAMELEN
+32];
5228 const char *dsname
= buf
;
5230 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
5231 node
= uu_list_next(fspset
->fsps_list
, node
)) {
5232 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
5233 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
5236 (void) snprintf(buf
, ZFS_MAXNAMELEN
+32,
5237 gettext("---- Permissions on %s "),
5238 node
->fspn_fsperm
.fsp_name
);
5239 (void) printf("%s", dsname
);
5240 left
= 70 - strlen(buf
);
5243 (void) printf("\n");
5245 print_set_creat_perms(sc_avl
);
5246 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
5247 gettext("Local permissions:\n"));
5248 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
5249 gettext("Descendent permissions:\n"));
5250 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
5251 gettext("Local+Descendent permissions:\n"));
5255 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5257 struct deleg_perms
{
5263 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5265 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5266 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5268 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5271 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5275 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5278 nvlist_t
*perm_nvl
= NULL
;
5279 nvlist_t
*update_perm_nvl
= NULL
;
5282 struct allow_opts opts
= { 0 };
5284 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5287 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5290 opts
.local
= B_TRUE
;
5293 opts
.descend
= B_TRUE
;
5299 opts
.group
= B_TRUE
;
5302 opts
.everyone
= B_TRUE
;
5308 opts
.create
= B_TRUE
;
5311 opts
.recursive
= B_TRUE
;
5314 (void) fprintf(stderr
, gettext("missing argument for "
5315 "'%c' option\n"), optopt
);
5319 opts
.prt_usage
= B_TRUE
;
5322 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5331 /* check arguments */
5332 parse_allow_args(argc
, argv
, un
, &opts
);
5334 /* try to open the dataset */
5335 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5336 ZFS_TYPE_VOLUME
)) == NULL
) {
5337 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5342 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5345 fs_perm_set_init(&fs_perm_set
);
5346 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5347 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5352 print_fs_perms(&fs_perm_set
);
5354 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5355 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5358 if (un
&& opts
.recursive
) {
5359 struct deleg_perms data
= { un
, update_perm_nvl
};
5360 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5369 nvlist_free(perm_nvl
);
5370 nvlist_free(update_perm_nvl
);
5372 fs_perm_set_fini(&fs_perm_set
);
5380 zfs_do_allow(int argc
, char **argv
)
5382 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5386 zfs_do_unallow(int argc
, char **argv
)
5388 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5392 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5397 boolean_t recursive
= B_FALSE
;
5398 const char *opts
= holding
? "rt" : "r";
5402 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5408 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5417 /* check number of arguments */
5425 if (holding
&& tag
[0] == '.') {
5426 /* tags starting with '.' are reserved for libzfs */
5427 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5431 for (i
= 0; i
< argc
; ++i
) {
5433 char parent
[ZFS_MAXNAMELEN
];
5435 char *path
= argv
[i
];
5437 delim
= strchr(path
, '@');
5438 if (delim
== NULL
) {
5439 (void) fprintf(stderr
,
5440 gettext("'%s' is not a snapshot\n"), path
);
5444 (void) strncpy(parent
, path
, delim
- path
);
5445 parent
[delim
- path
] = '\0';
5447 zhp
= zfs_open(g_zfs
, parent
,
5448 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5454 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
5457 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5463 return (errors
!= 0);
5467 * zfs hold [-r] [-t] <tag> <snap> ...
5469 * -r Recursively hold
5471 * Apply a user-hold with the given tag to the list of snapshots.
5474 zfs_do_hold(int argc
, char **argv
)
5476 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5480 * zfs release [-r] <tag> <snap> ...
5482 * -r Recursively release
5484 * Release a user-hold with the given tag from the list of snapshots.
5487 zfs_do_release(int argc
, char **argv
)
5489 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5492 typedef struct holds_cbdata
{
5493 boolean_t cb_recursive
;
5494 const char *cb_snapname
;
5496 size_t cb_max_namelen
;
5497 size_t cb_max_taglen
;
5500 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5501 #define DATETIME_BUF_LEN (32)
5506 print_holds(boolean_t scripted
, int nwidth
, int tagwidth
, nvlist_t
*nvl
)
5509 nvpair_t
*nvp
= NULL
;
5510 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5514 for (i
= 0; i
< 3; i
++) {
5515 col
= gettext(hdr_cols
[i
]);
5517 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5520 (void) printf("%s\n", col
);
5524 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5525 char *zname
= nvpair_name(nvp
);
5527 nvpair_t
*nvp2
= NULL
;
5528 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5529 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5530 char tsbuf
[DATETIME_BUF_LEN
];
5531 char *tagname
= nvpair_name(nvp2
);
5535 char sep
= scripted
? '\t' : ' ';
5536 int sepnum
= scripted
? 1 : 2;
5538 (void) nvpair_value_uint64(nvp2
, &val
);
5540 (void) localtime_r(&time
, &t
);
5541 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5542 gettext(STRFTIME_FMT_STR
), &t
);
5544 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth
, zname
,
5545 sepnum
, sep
, tagwidth
, tagname
, sepnum
, sep
, tsbuf
);
5551 * Generic callback function to list a dataset or snapshot.
5554 holds_callback(zfs_handle_t
*zhp
, void *data
)
5556 holds_cbdata_t
*cbp
= data
;
5557 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5558 nvlist_t
*nvl
= NULL
;
5559 nvpair_t
*nvp
= NULL
;
5560 const char *zname
= zfs_get_name(zhp
);
5561 size_t znamelen
= strnlen(zname
, ZFS_MAXNAMELEN
);
5563 if (cbp
->cb_recursive
) {
5564 const char *snapname
;
5565 char *delim
= strchr(zname
, '@');
5569 snapname
= delim
+ 1;
5570 if (strcmp(cbp
->cb_snapname
, snapname
))
5574 if (zfs_get_holds(zhp
, &nvl
) != 0)
5577 if (znamelen
> cbp
->cb_max_namelen
)
5578 cbp
->cb_max_namelen
= znamelen
;
5580 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5581 const char *tag
= nvpair_name(nvp
);
5582 size_t taglen
= strnlen(tag
, MAXNAMELEN
);
5583 if (taglen
> cbp
->cb_max_taglen
)
5584 cbp
->cb_max_taglen
= taglen
;
5587 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5591 * zfs holds [-r] <snap> ...
5593 * -r Recursively hold
5596 zfs_do_holds(int argc
, char **argv
)
5601 boolean_t scripted
= B_FALSE
;
5602 boolean_t recursive
= B_FALSE
;
5603 const char *opts
= "rH";
5606 int types
= ZFS_TYPE_SNAPSHOT
;
5607 holds_cbdata_t cb
= { 0 };
5614 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5623 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5630 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
5631 flags
|= ZFS_ITER_RECURSE
;
5637 /* check number of arguments */
5641 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5644 for (i
= 0; i
< argc
; ++i
) {
5645 char *snapshot
= argv
[i
];
5647 const char *snapname
;
5649 delim
= strchr(snapshot
, '@');
5650 if (delim
== NULL
) {
5651 (void) fprintf(stderr
,
5652 gettext("'%s' is not a snapshot\n"), snapshot
);
5656 snapname
= delim
+ 1;
5658 snapshot
[delim
- snapshot
] = '\0';
5660 cb
.cb_recursive
= recursive
;
5661 cb
.cb_snapname
= snapname
;
5665 * 1. collect holds data, set format options
5667 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
5668 holds_callback
, &cb
);
5674 * 2. print holds data
5676 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
5678 if (nvlist_empty(nvl
))
5679 (void) fprintf(stderr
, gettext("no datasets available\n"));
5683 return (0 != errors
);
5686 #define CHECK_SPINNER 30
5687 #define SPINNER_TIME 3 /* seconds */
5688 #define MOUNT_TIME 5 /* seconds */
5691 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
5693 static char *spin
[] = { "-", "\\", "|", "/" };
5694 static int spinval
= 0;
5695 static int spincheck
= 0;
5696 static time_t last_spin_time
= (time_t)0;
5697 get_all_cb_t
*cbp
= data
;
5698 zfs_type_t type
= zfs_get_type(zhp
);
5700 if (cbp
->cb_verbose
) {
5701 if (--spincheck
< 0) {
5702 time_t now
= time(NULL
);
5703 if (last_spin_time
+ SPINNER_TIME
< now
) {
5704 update_progress(spin
[spinval
++ % 4]);
5705 last_spin_time
= now
;
5707 spincheck
= CHECK_SPINNER
;
5712 * Iterate over any nested datasets.
5714 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
5720 * Skip any datasets whose type does not match.
5722 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
5726 libzfs_add_handle(cbp
, zhp
);
5727 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
5733 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
5735 get_all_cb_t cb
= { 0 };
5736 cb
.cb_verbose
= verbose
;
5737 cb
.cb_getone
= get_one_dataset
;
5740 set_progress_header(gettext("Reading ZFS config"));
5741 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
5743 *dslist
= cb
.cb_handles
;
5744 *count
= cb
.cb_used
;
5747 finish_progress(gettext("done."));
5751 * Generic callback for sharing or mounting filesystems. Because the code is so
5752 * similar, we have a common function with an extra parameter to determine which
5753 * mode we are using.
5755 #define OP_SHARE 0x1
5756 #define OP_MOUNT 0x2
5759 * Share or mount a dataset.
5762 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
5763 boolean_t
explicit, const char *options
)
5765 char mountpoint
[ZFS_MAXPROPLEN
];
5766 char shareopts
[ZFS_MAXPROPLEN
];
5767 char smbshareopts
[ZFS_MAXPROPLEN
];
5768 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
5770 uint64_t zoned
, canmount
;
5771 boolean_t shared_nfs
, shared_smb
;
5773 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
5776 * Check to make sure we can mount/share this dataset. If we
5777 * are in the global zone and the filesystem is exported to a
5778 * local zone, or if we are in a local zone and the
5779 * filesystem is not exported, then it is an error.
5781 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
5783 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
5787 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5788 "dataset is exported to a local zone\n"), cmdname
,
5792 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
5796 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5797 "permission denied\n"), cmdname
,
5803 * Ignore any filesystems which don't apply to us. This
5804 * includes those with a legacy mountpoint, or those with
5805 * legacy share options.
5807 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
5808 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
5809 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
5810 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5811 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
5812 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5814 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
5815 strcmp(smbshareopts
, "off") == 0) {
5819 (void) fprintf(stderr
, gettext("cannot share '%s': "
5820 "legacy share\n"), zfs_get_name(zhp
));
5821 (void) fprintf(stderr
, gettext("use share(1M) to "
5822 "share this filesystem, or set "
5823 "sharenfs property on\n"));
5828 * We cannot share or mount legacy filesystems. If the
5829 * shareopts is non-legacy but the mountpoint is legacy, we
5830 * treat it as a legacy share.
5832 if (strcmp(mountpoint
, "legacy") == 0) {
5836 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5837 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
5838 (void) fprintf(stderr
, gettext("use %s(1M) to "
5839 "%s this filesystem\n"), cmdname
, cmdname
);
5843 if (strcmp(mountpoint
, "none") == 0) {
5847 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
5848 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
5853 * canmount explicit outcome
5854 * on no pass through
5855 * on yes pass through
5857 * off yes display error, return 1
5858 * noauto no return 0
5859 * noauto yes pass through
5861 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
5862 if (canmount
== ZFS_CANMOUNT_OFF
) {
5866 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5867 "'canmount' property is set to 'off'\n"), cmdname
,
5870 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
5875 * If this filesystem is inconsistent and has a receive resume
5876 * token, we can not mount it.
5878 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
5879 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
5880 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
5884 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5885 "Contains partially-completed state from "
5886 "\"zfs receive -r\", which can be resumed with "
5887 "\"zfs send -t\"\n"),
5888 cmdname
, zfs_get_name(zhp
));
5893 * At this point, we have verified that the mountpoint and/or
5894 * shareopts are appropriate for auto management. If the
5895 * filesystem is already mounted or shared, return (failing
5896 * for explicit requests); otherwise mount or share the
5902 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
5903 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
5905 if ((shared_nfs
&& shared_smb
) ||
5906 ((shared_nfs
&& strcmp(shareopts
, "on") == 0) &&
5907 (strcmp(smbshareopts
, "off") == 0)) ||
5908 ((shared_smb
&& strcmp(smbshareopts
, "on") == 0) &&
5909 (strcmp(shareopts
, "off") == 0))) {
5913 (void) fprintf(stderr
, gettext("cannot share "
5914 "'%s': filesystem already shared\n"),
5919 if (!zfs_is_mounted(zhp
, NULL
) &&
5920 zfs_mount(zhp
, NULL
, 0) != 0)
5923 if (protocol
== NULL
) {
5924 if (zfs_shareall(zhp
) != 0)
5926 } else if (strcmp(protocol
, "nfs") == 0) {
5927 if (zfs_share_nfs(zhp
))
5929 } else if (strcmp(protocol
, "smb") == 0) {
5930 if (zfs_share_smb(zhp
))
5933 (void) fprintf(stderr
, gettext("cannot share "
5934 "'%s': invalid share type '%s' "
5936 zfs_get_name(zhp
), protocol
);
5943 if (options
== NULL
)
5944 mnt
.mnt_mntopts
= "";
5946 mnt
.mnt_mntopts
= (char *)options
;
5948 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
5949 zfs_is_mounted(zhp
, NULL
)) {
5953 (void) fprintf(stderr
, gettext("cannot mount "
5954 "'%s': filesystem already mounted\n"),
5959 if (zfs_mount(zhp
, options
, flags
) != 0)
5968 * Reports progress in the form "(current/total)". Not thread-safe.
5971 report_mount_progress(int current
, int total
)
5973 static time_t last_progress_time
= 0;
5974 time_t now
= time(NULL
);
5977 /* report 1..n instead of 0..n-1 */
5980 /* display header if we're here for the first time */
5982 set_progress_header(gettext("Mounting ZFS filesystems"));
5983 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
5984 /* too soon to report again */
5988 last_progress_time
= now
;
5990 (void) sprintf(info
, "(%d/%d)", current
, total
);
5992 if (current
== total
)
5993 finish_progress(info
);
5995 update_progress(info
);
5999 append_options(char *mntopts
, char *newopts
)
6001 int len
= strlen(mntopts
);
6003 /* original length plus new string to append plus 1 for the comma */
6004 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
6005 (void) fprintf(stderr
, gettext("the opts argument for "
6006 "'%s' option is too long (more than %d chars)\n"),
6007 "-o", MNT_LINE_MAX
);
6012 mntopts
[len
++] = ',';
6014 (void) strcpy(&mntopts
[len
], newopts
);
6018 share_mount(int op
, int argc
, char **argv
)
6021 boolean_t verbose
= B_FALSE
;
6023 char *options
= NULL
;
6027 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
6037 if (*optarg
== '\0') {
6038 (void) fprintf(stderr
, gettext("empty mount "
6039 "options (-o) specified\n"));
6043 if (options
== NULL
)
6044 options
= safe_malloc(MNT_LINE_MAX
+ 1);
6046 /* option validation is done later */
6047 append_options(options
, optarg
);
6050 flags
|= MS_OVERLAY
;
6053 (void) fprintf(stderr
, gettext("missing argument for "
6054 "'%c' option\n"), optopt
);
6058 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6067 /* check number of arguments */
6069 zfs_handle_t
**dslist
= NULL
;
6070 size_t i
, count
= 0;
6071 char *protocol
= NULL
;
6073 if (op
== OP_SHARE
&& argc
> 0) {
6074 if (strcmp(argv
[0], "nfs") != 0 &&
6075 strcmp(argv
[0], "smb") != 0) {
6076 (void) fprintf(stderr
, gettext("share type "
6077 "must be 'nfs' or 'smb'\n"));
6086 (void) fprintf(stderr
, gettext("too many arguments\n"));
6090 start_progress_timer();
6091 get_all_datasets(&dslist
, &count
, verbose
);
6096 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
6098 for (i
= 0; i
< count
; i
++) {
6100 report_mount_progress(i
, count
);
6102 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
6103 B_FALSE
, options
) != 0)
6105 zfs_close(dslist
[i
]);
6109 } else if (argc
== 0) {
6110 struct mnttab entry
;
6112 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
6113 (void) fprintf(stderr
, gettext("missing filesystem "
6114 "argument (specify -a for all)\n"));
6119 * When mount is given no arguments, go through /etc/mtab and
6120 * display any active ZFS mounts. We hide any snapshots, since
6121 * they are controlled automatically.
6124 /* Reopen MNTTAB to prevent reading stale data from open file */
6125 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6128 while (getmntent(mnttab_file
, &entry
) == 0) {
6129 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
6130 strchr(entry
.mnt_special
, '@') != NULL
)
6133 (void) printf("%-30s %s\n", entry
.mnt_special
,
6141 (void) fprintf(stderr
,
6142 gettext("too many arguments\n"));
6146 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6147 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6150 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
6160 * zfs mount -a [nfs]
6161 * zfs mount filesystem
6163 * Mount all filesystems, or mount the given filesystem.
6166 zfs_do_mount(int argc
, char **argv
)
6168 return (share_mount(OP_MOUNT
, argc
, argv
));
6172 * zfs share -a [nfs | smb]
6173 * zfs share filesystem
6175 * Share all filesystems, or share the given filesystem.
6178 zfs_do_share(int argc
, char **argv
)
6180 return (share_mount(OP_SHARE
, argc
, argv
));
6183 typedef struct unshare_unmount_node
{
6184 zfs_handle_t
*un_zhp
;
6186 uu_avl_node_t un_avlnode
;
6187 } unshare_unmount_node_t
;
6191 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
6193 const unshare_unmount_node_t
*l
= larg
;
6194 const unshare_unmount_node_t
*r
= rarg
;
6196 return (strcmp(l
->un_mountp
, r
->un_mountp
));
6200 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6201 * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
6202 * and unmount it appropriately.
6205 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
6209 struct stat64 statbuf
;
6210 struct extmnttab entry
;
6211 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
6215 * Search for the path in /etc/mtab. Rather than looking for the
6216 * specific path, which can be fooled by non-standard paths (i.e. ".."
6217 * or "//"), we stat() the path and search for the corresponding
6218 * (major,minor) device pair.
6220 if (stat64(path
, &statbuf
) != 0) {
6221 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6222 cmdname
, path
, strerror(errno
));
6225 path_inode
= statbuf
.st_ino
;
6228 * Search for the given (major,minor) pair in the mount table.
6231 /* Reopen MNTTAB to prevent reading stale data from open file */
6232 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6235 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
6236 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
6237 entry
.mnt_minor
== minor(statbuf
.st_dev
))
6241 if (op
== OP_SHARE
) {
6242 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
6243 "currently mounted\n"), cmdname
, path
);
6246 (void) fprintf(stderr
, gettext("warning: %s not in mtab\n"),
6248 if ((ret
= umount2(path
, flags
)) != 0)
6249 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
6254 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
6255 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
6256 "filesystem\n"), cmdname
, path
);
6260 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6261 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6265 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
6266 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6267 cmdname
, path
, strerror(errno
));
6269 } else if (statbuf
.st_ino
!= path_inode
) {
6270 (void) fprintf(stderr
, gettext("cannot "
6271 "%s '%s': not a mountpoint\n"), cmdname
, path
);
6275 if (op
== OP_SHARE
) {
6276 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6277 char smbshare_prop
[ZFS_MAXPROPLEN
];
6279 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
6280 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6281 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
6282 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6284 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6285 strcmp(smbshare_prop
, "off") == 0) {
6286 (void) fprintf(stderr
, gettext("cannot unshare "
6287 "'%s': legacy share\n"), path
);
6288 (void) fprintf(stderr
, gettext("use exportfs(8) "
6289 "or smbcontrol(1) to unshare this filesystem\n"));
6290 } else if (!zfs_is_shared(zhp
)) {
6291 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6292 "not currently shared\n"), path
);
6294 ret
= zfs_unshareall_bypath(zhp
, path
);
6297 char mtpt_prop
[ZFS_MAXPROPLEN
];
6299 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6300 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6303 ret
= zfs_unmount(zhp
, NULL
, flags
);
6304 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6305 (void) fprintf(stderr
, gettext("cannot unmount "
6306 "'%s': legacy mountpoint\n"),
6308 (void) fprintf(stderr
, gettext("use umount(8) "
6309 "to unmount this filesystem\n"));
6311 ret
= zfs_unmountall(zhp
, flags
);
6322 * Generic callback for unsharing or unmounting a filesystem.
6325 unshare_unmount(int op
, int argc
, char **argv
)
6332 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6333 char sharesmb
[ZFS_MAXPROPLEN
];
6336 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
6345 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6356 * We could make use of zfs_for_each() to walk all datasets in
6357 * the system, but this would be very inefficient, especially
6358 * since we would have to linearly search /etc/mtab for each
6359 * one. Instead, do one pass through /etc/mtab looking for
6360 * zfs entries and call zfs_unmount() for each one.
6362 * Things get a little tricky if the administrator has created
6363 * mountpoints beneath other ZFS filesystems. In this case, we
6364 * have to unmount the deepest filesystems first. To accomplish
6365 * this, we place all the mountpoints in an AVL tree sorted by
6366 * the special type (dataset name), and walk the result in
6367 * reverse to make sure to get any snapshots first.
6369 struct mnttab entry
;
6370 uu_avl_pool_t
*pool
;
6371 uu_avl_t
*tree
= NULL
;
6372 unshare_unmount_node_t
*node
;
6374 uu_avl_walk_t
*walk
;
6377 (void) fprintf(stderr
, gettext("too many arguments\n"));
6381 if (((pool
= uu_avl_pool_create("unmount_pool",
6382 sizeof (unshare_unmount_node_t
),
6383 offsetof(unshare_unmount_node_t
, un_avlnode
),
6384 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6385 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6388 /* Reopen MNTTAB to prevent reading stale data from open file */
6389 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6392 while (getmntent(mnttab_file
, &entry
) == 0) {
6394 /* ignore non-ZFS entries */
6395 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6398 /* ignore snapshots */
6399 if (strchr(entry
.mnt_special
, '@') != NULL
)
6402 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6403 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6410 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6412 sizeof (nfs_mnt_prop
),
6413 NULL
, NULL
, 0, B_FALSE
) == 0);
6414 if (strcmp(nfs_mnt_prop
, "off") != 0)
6416 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6418 sizeof (nfs_mnt_prop
),
6419 NULL
, NULL
, 0, B_FALSE
) == 0);
6420 if (strcmp(nfs_mnt_prop
, "off") == 0)
6424 /* Ignore legacy mounts */
6425 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6427 sizeof (nfs_mnt_prop
),
6428 NULL
, NULL
, 0, B_FALSE
) == 0);
6429 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6431 /* Ignore canmount=noauto mounts */
6432 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6433 ZFS_CANMOUNT_NOAUTO
)
6439 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6441 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6443 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6445 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6446 uu_avl_insert(tree
, node
, idx
);
6448 zfs_close(node
->un_zhp
);
6449 free(node
->un_mountp
);
6455 * Walk the AVL tree in reverse, unmounting each filesystem and
6456 * removing it from the AVL tree in the process.
6458 if ((walk
= uu_avl_walk_start(tree
,
6459 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6462 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6463 uu_avl_remove(tree
, node
);
6467 if (zfs_unshareall_bypath(node
->un_zhp
,
6468 node
->un_mountp
) != 0)
6473 if (zfs_unmount(node
->un_zhp
,
6474 node
->un_mountp
, flags
) != 0)
6479 zfs_close(node
->un_zhp
);
6480 free(node
->un_mountp
);
6484 uu_avl_walk_end(walk
);
6485 uu_avl_destroy(tree
);
6486 uu_avl_pool_destroy(pool
);
6491 (void) fprintf(stderr
,
6492 gettext("missing filesystem argument\n"));
6494 (void) fprintf(stderr
,
6495 gettext("too many arguments\n"));
6500 * We have an argument, but it may be a full path or a ZFS
6501 * filesystem. Pass full paths off to unmount_path() (shared by
6502 * manual_unmount), otherwise open the filesystem and pass to
6505 if (argv
[0][0] == '/')
6506 return (unshare_unmount_path(op
, argv
[0],
6509 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6510 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6513 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
6514 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
6515 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
6516 NULL
, 0, B_FALSE
) == 0);
6520 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6522 sizeof (nfs_mnt_prop
),
6523 NULL
, NULL
, 0, B_FALSE
) == 0);
6524 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6525 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
6528 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6529 strcmp(sharesmb
, "off") == 0) {
6530 (void) fprintf(stderr
, gettext("cannot "
6531 "unshare '%s': legacy share\n"),
6533 (void) fprintf(stderr
, gettext("use "
6534 "unshare(1M) to unshare this "
6537 } else if (!zfs_is_shared(zhp
)) {
6538 (void) fprintf(stderr
, gettext("cannot "
6539 "unshare '%s': not currently "
6540 "shared\n"), zfs_get_name(zhp
));
6542 } else if (zfs_unshareall(zhp
) != 0) {
6548 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
6549 (void) fprintf(stderr
, gettext("cannot "
6550 "unmount '%s': legacy "
6551 "mountpoint\n"), zfs_get_name(zhp
));
6552 (void) fprintf(stderr
, gettext("use "
6553 "umount(1M) to unmount this "
6556 } else if (!zfs_is_mounted(zhp
, NULL
)) {
6557 (void) fprintf(stderr
, gettext("cannot "
6558 "unmount '%s': not currently "
6562 } else if (zfs_unmountall(zhp
, flags
) != 0) {
6576 * zfs unmount filesystem
6578 * Unmount all filesystems, or a specific ZFS filesystem.
6581 zfs_do_unmount(int argc
, char **argv
)
6583 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
6588 * zfs unshare filesystem
6590 * Unshare all filesystems, or a specific ZFS filesystem.
6593 zfs_do_unshare(int argc
, char **argv
)
6595 return (unshare_unmount(OP_SHARE
, argc
, argv
));
6599 find_command_idx(char *command
, int *idx
)
6603 for (i
= 0; i
< NCOMMAND
; i
++) {
6604 if (command_table
[i
].name
== NULL
)
6607 if (strcmp(command
, command_table
[i
].name
) == 0) {
6616 zfs_do_diff(int argc
, char **argv
)
6620 char *tosnap
= NULL
;
6621 char *fromsnap
= NULL
;
6626 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
6629 flags
|= ZFS_DIFF_CLASSIFY
;
6632 flags
|= ZFS_DIFF_PARSEABLE
;
6635 flags
|= ZFS_DIFF_TIMESTAMP
;
6638 (void) fprintf(stderr
,
6639 gettext("invalid option '%c'\n"), optopt
);
6648 (void) fprintf(stderr
,
6649 gettext("must provide at least one snapshot name\n"));
6654 (void) fprintf(stderr
, gettext("too many arguments\n"));
6659 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
6662 if (*fromsnap
!= '@')
6663 copy
= strdup(fromsnap
);
6665 copy
= strdup(tosnap
);
6669 if ((atp
= strchr(copy
, '@')))
6672 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6678 * Ignore SIGPIPE so that the library can give us
6679 * information on any failure
6681 (void) sigignore(SIGPIPE
);
6683 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
6691 * zfs bookmark <fs@snap> <fs#bmark>
6693 * Creates a bookmark with the given name from the given snapshot.
6696 zfs_do_bookmark(int argc
, char **argv
)
6698 char snapname
[ZFS_MAXNAMELEN
];
6705 while ((c
= getopt(argc
, argv
, "")) != -1) {
6708 (void) fprintf(stderr
,
6709 gettext("invalid option '%c'\n"), optopt
);
6717 /* check number of arguments */
6719 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
6723 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
6727 if (strchr(argv
[1], '#') == NULL
) {
6728 (void) fprintf(stderr
,
6729 gettext("invalid bookmark name '%s' -- "
6730 "must contain a '#'\n"), argv
[1]);
6734 if (argv
[0][0] == '@') {
6736 * Snapshot name begins with @.
6737 * Default to same fs as bookmark.
6739 (void) strncpy(snapname
, argv
[1], sizeof (snapname
));
6740 *strchr(snapname
, '#') = '\0';
6741 (void) strlcat(snapname
, argv
[0], sizeof (snapname
));
6743 (void) strncpy(snapname
, argv
[0], sizeof (snapname
));
6745 zhp
= zfs_open(g_zfs
, snapname
, ZFS_TYPE_SNAPSHOT
);
6751 nvl
= fnvlist_alloc();
6752 fnvlist_add_string(nvl
, argv
[1], snapname
);
6753 ret
= lzc_bookmark(nvl
, NULL
);
6757 const char *err_msg
;
6760 (void) snprintf(errbuf
, sizeof (errbuf
),
6761 dgettext(TEXT_DOMAIN
,
6762 "cannot create bookmark '%s'"), argv
[1]);
6766 err_msg
= "bookmark is in a different pool";
6769 err_msg
= "bookmark exists";
6772 err_msg
= "invalid argument";
6775 err_msg
= "bookmark feature not enabled";
6778 err_msg
= "out of space";
6781 err_msg
= "unknown error";
6784 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
6785 dgettext(TEXT_DOMAIN
, err_msg
));
6796 main(int argc
, char **argv
)
6802 (void) setlocale(LC_ALL
, "");
6803 (void) textdomain(TEXT_DOMAIN
);
6805 dprintf_setup(&argc
, argv
);
6810 * Make sure the user has specified some command.
6813 (void) fprintf(stderr
, gettext("missing command\n"));
6820 * The 'umount' command is an alias for 'unmount'
6822 if (strcmp(cmdname
, "umount") == 0)
6823 cmdname
= "unmount";
6826 * The 'recv' command is an alias for 'receive'
6828 if (strcmp(cmdname
, "recv") == 0)
6829 cmdname
= "receive";
6832 * The 'snap' command is an alias for 'snapshot'
6834 if (strcmp(cmdname
, "snap") == 0)
6835 cmdname
= "snapshot";
6840 if ((strcmp(cmdname
, "-?") == 0) ||
6841 (strcmp(cmdname
, "--help") == 0))
6844 if ((g_zfs
= libzfs_init()) == NULL
) {
6845 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
6849 mnttab_file
= g_zfs
->libzfs_mnttab
;
6851 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
6853 libzfs_print_on_error(g_zfs
, B_TRUE
);
6856 * Run the appropriate command.
6858 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
6859 if (find_command_idx(cmdname
, &i
) == 0) {
6860 current_command
= &command_table
[i
];
6861 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
6862 } else if (strchr(cmdname
, '=') != NULL
) {
6863 verify(find_command_idx("set", &i
) == 0);
6864 current_command
= &command_table
[i
];
6865 ret
= command_table
[i
].func(argc
, argv
);
6867 (void) fprintf(stderr
, gettext("unrecognized "
6868 "command '%s'\n"), cmdname
);
6873 if (ret
== 0 && log_history
)
6874 (void) zpool_log_history(g_zfs
, history_str
);
6879 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6880 * for the purposes of running ::findleaks.
6882 if (getenv("ZFS_ABORT") != NULL
) {
6883 (void) printf("dumping core by request\n");