4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
29 * Copyright 2016 Nexenta Systems, Inc.
39 #include <libnvpair.h>
52 #include <sys/mkdev.h>
53 #include <sys/mntent.h>
54 #include <sys/mnttab.h>
55 #include <sys/mount.h>
57 #include <sys/fs/zfs.h>
58 #include <sys/systeminfo.h>
59 #include <sys/types.h>
63 #include <libzfs_core.h>
65 #include <zfs_deleg.h>
69 #include <directory.h>
70 #endif /* HAVE_IDMAP */
74 #include "zfs_comutil.h"
75 #include "libzfs_impl.h"
77 libzfs_handle_t
*g_zfs
;
79 static FILE *mnttab_file
;
80 static char history_str
[HIS_MAX_RECORD_LEN
];
81 static boolean_t log_history
= B_TRUE
;
83 static int zfs_do_clone(int argc
, char **argv
);
84 static int zfs_do_create(int argc
, char **argv
);
85 static int zfs_do_destroy(int argc
, char **argv
);
86 static int zfs_do_get(int argc
, char **argv
);
87 static int zfs_do_inherit(int argc
, char **argv
);
88 static int zfs_do_list(int argc
, char **argv
);
89 static int zfs_do_mount(int argc
, char **argv
);
90 static int zfs_do_rename(int argc
, char **argv
);
91 static int zfs_do_rollback(int argc
, char **argv
);
92 static int zfs_do_set(int argc
, char **argv
);
93 static int zfs_do_upgrade(int argc
, char **argv
);
94 static int zfs_do_snapshot(int argc
, char **argv
);
95 static int zfs_do_unmount(int argc
, char **argv
);
96 static int zfs_do_share(int argc
, char **argv
);
97 static int zfs_do_unshare(int argc
, char **argv
);
98 static int zfs_do_send(int argc
, char **argv
);
99 static int zfs_do_receive(int argc
, char **argv
);
100 static int zfs_do_promote(int argc
, char **argv
);
101 static int zfs_do_userspace(int argc
, char **argv
);
102 static int zfs_do_allow(int argc
, char **argv
);
103 static int zfs_do_unallow(int argc
, char **argv
);
104 static int zfs_do_hold(int argc
, char **argv
);
105 static int zfs_do_holds(int argc
, char **argv
);
106 static int zfs_do_release(int argc
, char **argv
);
107 static int zfs_do_diff(int argc
, char **argv
);
108 static int zfs_do_bookmark(int argc
, char **argv
);
109 static int zfs_do_load_key(int argc
, char **argv
);
110 static int zfs_do_unload_key(int argc
, char **argv
);
111 static int zfs_do_change_key(int argc
, char **argv
);
114 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
119 _umem_debug_init(void)
121 return ("default,verbose"); /* $UMEM_DEBUG setting */
125 _umem_logging_init(void)
127 return ("fail,contents"); /* $UMEM_LOGGING setting */
164 typedef struct zfs_command
{
166 int (*func
)(int argc
, char **argv
);
171 * Master command table. Each ZFS command has a name, associated function, and
172 * usage message. The usage messages need to be internationalized, so we have
173 * to have a function to return the usage message based on a command index.
175 * These commands are organized according to how they are displayed in the usage
176 * message. An empty command (one with a NULL name) indicates an empty line in
177 * the generic usage message.
179 static zfs_command_t command_table
[] = {
180 { "create", zfs_do_create
, HELP_CREATE
},
181 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
183 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
184 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
185 { "clone", zfs_do_clone
, HELP_CLONE
},
186 { "promote", zfs_do_promote
, HELP_PROMOTE
},
187 { "rename", zfs_do_rename
, HELP_RENAME
},
188 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
190 { "list", zfs_do_list
, HELP_LIST
},
192 { "set", zfs_do_set
, HELP_SET
},
193 { "get", zfs_do_get
, HELP_GET
},
194 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
195 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
196 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
197 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
199 { "mount", zfs_do_mount
, HELP_MOUNT
},
200 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
201 { "share", zfs_do_share
, HELP_SHARE
},
202 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
204 { "send", zfs_do_send
, HELP_SEND
},
205 { "receive", zfs_do_receive
, HELP_RECEIVE
},
207 { "allow", zfs_do_allow
, HELP_ALLOW
},
209 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
211 { "hold", zfs_do_hold
, HELP_HOLD
},
212 { "holds", zfs_do_holds
, HELP_HOLDS
},
213 { "release", zfs_do_release
, HELP_RELEASE
},
214 { "diff", zfs_do_diff
, HELP_DIFF
},
215 { "load-key", zfs_do_load_key
, HELP_LOAD_KEY
},
216 { "unload-key", zfs_do_unload_key
, HELP_UNLOAD_KEY
},
217 { "change-key", zfs_do_change_key
, HELP_CHANGE_KEY
},
220 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
222 zfs_command_t
*current_command
;
225 get_usage(zfs_help_t idx
)
229 return (gettext("\tclone [-p] [-o property=value] ... "
230 "<snapshot> <filesystem|volume>\n"));
232 return (gettext("\tcreate [-p] [-o property=value] ... "
234 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
235 "-V <size> <volume>\n"));
237 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
238 "\tdestroy [-dnpRrv] "
239 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
240 "\tdestroy <filesystem|volume>#<bookmark>\n"));
242 return (gettext("\tget [-rHp] [-d max] "
243 "[-o \"all\" | field[,...]]\n"
244 "\t [-t type[,...]] [-s source[,...]]\n"
245 "\t <\"all\" | property[,...]> "
246 "[filesystem|volume|snapshot|bookmark] ...\n"));
248 return (gettext("\tinherit [-rS] <property> "
249 "<filesystem|volume|snapshot> ...\n"));
251 return (gettext("\tupgrade [-v]\n"
252 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
254 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
255 "[-s property]...\n\t [-S property]... [-t type[,...]] "
256 "[filesystem|volume|snapshot] ...\n"));
258 return (gettext("\tmount\n"
259 "\tmount [-lvO] [-o opts] <-a | filesystem>\n"));
261 return (gettext("\tpromote <clone-filesystem>\n"));
263 return (gettext("\treceive [-vnsFu] "
264 "[-o <property>=<value>] ... [-x <property>] ...\n"
265 "\t <filesystem|volume|snapshot>\n"
266 "\treceive [-vnsFu] [-o <property>=<value>] ... "
267 "[-x <property>] ... \n"
268 "\t [-d | -e] <filesystem>\n"
269 "\treceive -A <filesystem|volume>\n"));
271 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
272 "<filesystem|volume|snapshot>\n"
273 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
274 "\trename -r <snapshot> <snapshot>\n"));
276 return (gettext("\trollback [-rRf] <snapshot>\n"));
278 return (gettext("\tsend [-DnPpRvLecr] [-[i|I] snapshot] "
280 "\tsend [-Lecr] [-i snapshot|bookmark] "
281 "<filesystem|volume|snapshot>\n"
282 "\tsend [-nvPe] -t <receive_resume_token>\n"));
284 return (gettext("\tset <property=value> ... "
285 "<filesystem|volume|snapshot> ...\n"));
287 return (gettext("\tshare [-l] <-a [nfs|smb] | filesystem>\n"));
289 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
290 "<filesystem|volume>@<snap> ...\n"));
292 return (gettext("\tunmount [-f] "
293 "<-a | filesystem|mountpoint>\n"));
295 return (gettext("\tunshare "
296 "<-a [nfs|smb] | filesystem|mountpoint>\n"));
298 return (gettext("\tallow <filesystem|volume>\n"
300 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
301 "\t <filesystem|volume>\n"
302 "\tallow [-ld] -e <perm|@setname>[,...] "
303 "<filesystem|volume>\n"
304 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
305 "\tallow -s @setname <perm|@setname>[,...] "
306 "<filesystem|volume>\n"));
308 return (gettext("\tunallow [-rldug] "
309 "<\"everyone\"|user|group>[,...]\n"
310 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
311 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
312 "<filesystem|volume>\n"
313 "\tunallow [-r] -c [<perm|@setname>[,...]] "
314 "<filesystem|volume>\n"
315 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
316 "<filesystem|volume>\n"));
318 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
320 "\t [-S field] ... [-t type[,...]] "
321 "<filesystem|snapshot>\n"));
322 case HELP_GROUPSPACE
:
323 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
325 "\t [-S field] ... [-t type[,...]] "
326 "<filesystem|snapshot>\n"));
328 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
330 return (gettext("\tholds [-r] <snapshot> ...\n"));
332 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
334 return (gettext("\tdiff [-FHt] <snapshot> "
335 "[snapshot|filesystem]\n"));
337 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
339 return (gettext("\tload-key [-rn] [-L <keylocation>] "
340 "<-a | filesystem|volume>\n"));
341 case HELP_UNLOAD_KEY
:
342 return (gettext("\tunload-key [-r] "
343 "<-a | filesystem|volume>\n"));
344 case HELP_CHANGE_KEY
:
345 return (gettext("\tchange-key [-l] [-o keyformat=<value>]"
346 "\t [-o keylocation=<value>] [-o pbkfd2iters=<value>]"
347 "\t <filesystem|volume>\n"
348 "\tchange-key -i [-l] <filesystem|volume>\n"));
358 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
363 * Utility function to guarantee malloc() success.
367 safe_malloc(size_t size
)
371 if ((data
= calloc(1, size
)) == NULL
)
378 safe_strdup(char *str
)
380 char *dupstr
= strdup(str
);
389 * Callback routine that will print out information for each of
393 usage_prop_cb(int prop
, void *cb
)
397 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
399 if (zfs_prop_readonly(prop
))
400 (void) fprintf(fp
, " NO ");
402 (void) fprintf(fp
, "YES ");
404 if (zfs_prop_inheritable(prop
))
405 (void) fprintf(fp
, " YES ");
407 (void) fprintf(fp
, " NO ");
409 if (zfs_prop_values(prop
) == NULL
)
410 (void) fprintf(fp
, "-\n");
412 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
418 * Display usage message. If we're inside a command, display only the usage for
419 * that command. Otherwise, iterate over the entire command table and display
420 * a complete usage message.
423 usage(boolean_t requested
)
426 boolean_t show_properties
= B_FALSE
;
427 FILE *fp
= requested
? stdout
: stderr
;
429 if (current_command
== NULL
) {
431 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
433 gettext("where 'command' is one of the following:\n\n"));
435 for (i
= 0; i
< NCOMMAND
; i
++) {
436 if (command_table
[i
].name
== NULL
)
437 (void) fprintf(fp
, "\n");
439 (void) fprintf(fp
, "%s",
440 get_usage(command_table
[i
].usage
));
443 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
444 "pool/[dataset/]*dataset[@name]\n"));
446 (void) fprintf(fp
, gettext("usage:\n"));
447 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
450 if (current_command
!= NULL
&&
451 (strcmp(current_command
->name
, "set") == 0 ||
452 strcmp(current_command
->name
, "get") == 0 ||
453 strcmp(current_command
->name
, "inherit") == 0 ||
454 strcmp(current_command
->name
, "list") == 0))
455 show_properties
= B_TRUE
;
457 if (show_properties
) {
459 gettext("\nThe following properties are supported:\n"));
461 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
462 "PROPERTY", "EDIT", "INHERIT", "VALUES");
464 /* Iterate over all properties */
465 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
468 (void) fprintf(fp
, "\t%-15s ", "userused@...");
469 (void) fprintf(fp
, " NO NO <size>\n");
470 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
471 (void) fprintf(fp
, " NO NO <size>\n");
472 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
473 (void) fprintf(fp
, "YES NO <size> | none\n");
474 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
475 (void) fprintf(fp
, "YES NO <size> | none\n");
476 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
477 (void) fprintf(fp
, " NO NO <size>\n");
479 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
480 "with standard units such as K, M, G, etc.\n"));
481 (void) fprintf(fp
, gettext("\nUser-defined properties can "
482 "be specified by using a name containing a colon (:).\n"));
483 (void) fprintf(fp
, gettext("\nThe {user|group}{used|quota}@ "
484 "properties must be appended with\n"
485 "a user or group specifier of one of these forms:\n"
486 " POSIX name (eg: \"matt\")\n"
487 " POSIX id (eg: \"126829\")\n"
488 " SMB name@domain (eg: \"matt@sun\")\n"
489 " SMB SID (eg: \"S-1-234-567-89\")\n"));
492 gettext("\nFor the property list, run: %s\n"),
495 gettext("\nFor the delegated permission list, run: %s\n"),
496 "zfs allow|unallow");
500 * See comments at end of main().
502 if (getenv("ZFS_ABORT") != NULL
) {
503 (void) printf("dumping core by request\n");
507 exit(requested
? 0 : 2);
511 * Take a property=value argument string and add it to the given nvlist.
512 * Modifies the argument inplace.
515 parseprop(nvlist_t
*props
, char *propname
)
519 if ((propval
= strchr(propname
, '=')) == NULL
) {
520 (void) fprintf(stderr
, gettext("missing "
521 "'=' for property=value argument\n"));
526 if (nvlist_exists(props
, propname
)) {
527 (void) fprintf(stderr
, gettext("property '%s' "
528 "specified multiple times\n"), propname
);
531 if (nvlist_add_string(props
, propname
, propval
) != 0)
537 * Take a property name argument and add it to the given nvlist.
538 * Modifies the argument inplace.
541 parsepropname(nvlist_t
*props
, char *propname
)
543 if (strchr(propname
, '=') != NULL
) {
544 (void) fprintf(stderr
, gettext("invalid character "
545 "'=' in property argument\n"));
548 if (nvlist_exists(props
, propname
)) {
549 (void) fprintf(stderr
, gettext("property '%s' "
550 "specified multiple times\n"), propname
);
553 if (nvlist_add_boolean(props
, propname
) != 0)
559 parse_depth(char *opt
, int *flags
)
564 depth
= (int)strtol(opt
, &tmp
, 0);
566 (void) fprintf(stderr
,
567 gettext("%s is not an integer\n"), optarg
);
571 (void) fprintf(stderr
,
572 gettext("Depth can not be negative.\n"));
575 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
579 #define PROGRESS_DELAY 2 /* seconds */
581 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";
582 static time_t pt_begin
;
583 static char *pt_header
= NULL
;
584 static boolean_t pt_shown
;
587 start_progress_timer(void)
589 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
594 set_progress_header(char *header
)
596 assert(pt_header
== NULL
);
597 pt_header
= safe_strdup(header
);
599 (void) printf("%s: ", header
);
600 (void) fflush(stdout
);
605 update_progress(char *update
)
607 if (!pt_shown
&& time(NULL
) > pt_begin
) {
608 int len
= strlen(update
);
610 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
612 (void) fflush(stdout
);
614 } else if (pt_shown
) {
615 int len
= strlen(update
);
617 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
618 (void) fflush(stdout
);
623 finish_progress(char *done
)
626 (void) printf("%s\n", done
);
627 (void) fflush(stdout
);
634 zfs_mount_and_share(libzfs_handle_t
*hdl
, const char *dataset
, zfs_type_t type
)
636 zfs_handle_t
*zhp
= NULL
;
639 zhp
= zfs_open(hdl
, dataset
, type
);
644 * Volumes may neither be mounted or shared. Potentially in the
645 * future filesystems detected on these volumes could be mounted.
647 if (zfs_get_type(zhp
) == ZFS_TYPE_VOLUME
) {
653 * Mount and/or share the new filesystem as appropriate. We provide a
654 * verbose error message to let the user know that their filesystem was
655 * in fact created, even if we failed to mount or share it.
657 * If the user doesn't want the dataset automatically mounted, then
658 * skip the mount/share step
660 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
, B_FALSE
) &&
661 zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
) {
662 if (geteuid() != 0) {
663 (void) fprintf(stderr
, gettext("filesystem "
664 "successfully created, but it may only be "
665 "mounted by root\n"));
667 } else if (zfs_mount(zhp
, NULL
, 0) != 0) {
668 (void) fprintf(stderr
, gettext("filesystem "
669 "successfully created, but not mounted\n"));
671 } else if (zfs_share(zhp
) != 0) {
672 (void) fprintf(stderr
, gettext("filesystem "
673 "successfully created, but not shared\n"));
684 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
686 * Given an existing dataset, create a writable copy whose initial contents
687 * are the same as the source. The newly created dataset maintains a
688 * dependency on the original; the original cannot be destroyed so long as
691 * The '-p' flag creates all the non-existing ancestors of the target first.
694 zfs_do_clone(int argc
, char **argv
)
696 zfs_handle_t
*zhp
= NULL
;
697 boolean_t parents
= B_FALSE
;
702 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
706 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
709 if (!parseprop(props
, optarg
)) {
718 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
727 /* check number of arguments */
729 (void) fprintf(stderr
, gettext("missing source dataset "
734 (void) fprintf(stderr
, gettext("missing target dataset "
739 (void) fprintf(stderr
, gettext("too many arguments\n"));
743 /* open the source dataset */
744 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
) {
749 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
752 * Now create the ancestors of the target dataset. If the
753 * target already exists and '-p' option was used we should not
756 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
762 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
770 ret
= zfs_clone(zhp
, argv
[1], props
);
772 /* create the mountpoint if necessary */
775 (void) zpool_log_history(g_zfs
, history_str
);
776 log_history
= B_FALSE
;
779 ret
= zfs_mount_and_share(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
796 * zfs create [-p] [-o prop=value] ... fs
797 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
799 * Create a new dataset. This command can be used to create filesystems
800 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
801 * For volumes, the user must specify a size to be used.
803 * The '-s' flag applies only to volumes, and indicates that we should not try
804 * to set the reservation for this volume. By default we set a reservation
805 * equal to the size for any volume. For pools with SPA_VERSION >=
806 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
808 * The '-p' flag creates all the non-existing ancestors of the target first.
811 zfs_do_create(int argc
, char **argv
)
813 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
814 uint64_t volsize
= 0;
816 boolean_t noreserve
= B_FALSE
;
817 boolean_t bflag
= B_FALSE
;
818 boolean_t parents
= B_FALSE
;
823 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
827 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
830 type
= ZFS_TYPE_VOLUME
;
831 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
832 (void) fprintf(stderr
, gettext("bad volume "
833 "size '%s': %s\n"), optarg
,
834 libzfs_error_description(g_zfs
));
838 if (nvlist_add_uint64(props
,
839 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
848 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
849 (void) fprintf(stderr
, gettext("bad volume "
850 "block size '%s': %s\n"), optarg
,
851 libzfs_error_description(g_zfs
));
855 if (nvlist_add_uint64(props
,
856 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
861 if (!parseprop(props
, optarg
))
868 (void) fprintf(stderr
, gettext("missing size "
872 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
878 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
879 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
880 "used when creating a volume\n"));
887 /* check number of arguments */
889 (void) fprintf(stderr
, gettext("missing %s argument\n"),
890 zfs_type_to_name(type
));
894 (void) fprintf(stderr
, gettext("too many arguments\n"));
898 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
899 zpool_handle_t
*zpool_handle
;
900 nvlist_t
*real_props
= NULL
;
901 uint64_t spa_version
;
903 zfs_prop_t resv_prop
;
907 if ((p
= strchr(argv
[0], '/')) != NULL
)
909 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
912 if (zpool_handle
== NULL
)
914 spa_version
= zpool_get_prop_int(zpool_handle
,
915 ZPOOL_PROP_VERSION
, NULL
);
916 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
917 resv_prop
= ZFS_PROP_REFRESERVATION
;
919 resv_prop
= ZFS_PROP_RESERVATION
;
921 (void) snprintf(msg
, sizeof (msg
),
922 gettext("cannot create '%s'"), argv
[0]);
923 if (props
&& (real_props
= zfs_valid_proplist(g_zfs
, type
,
924 props
, 0, NULL
, zpool_handle
, B_TRUE
, msg
)) == NULL
) {
925 zpool_close(zpool_handle
);
928 zpool_close(zpool_handle
);
930 volsize
= zvol_volsize_to_reservation(volsize
, real_props
);
931 nvlist_free(real_props
);
933 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
935 if (nvlist_add_uint64(props
,
936 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
943 if (parents
&& zfs_name_valid(argv
[0], type
)) {
945 * Now create the ancestors of target dataset. If the target
946 * already exists and '-p' option was used we should not
949 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
953 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
958 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
962 (void) zpool_log_history(g_zfs
, history_str
);
963 log_history
= B_FALSE
;
966 ret
= zfs_mount_and_share(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
977 * zfs destroy [-rRf] <fs, vol>
978 * zfs destroy [-rRd] <snap>
980 * -r Recursively destroy all children
981 * -R Recursively destroy all dependents, including clones
982 * -f Force unmounting of any dependents
983 * -d If we can't destroy now, mark for deferred destruction
985 * Destroys the given dataset. By default, it will unmount any filesystems,
986 * and refuse to destroy a dataset that has any dependents. A dependent can
987 * either be a child, or a clone of a child.
989 typedef struct destroy_cbdata
{
992 boolean_t cb_recurse
;
994 boolean_t cb_doclones
;
995 zfs_handle_t
*cb_target
;
996 boolean_t cb_defer_destroy
;
997 boolean_t cb_verbose
;
998 boolean_t cb_parsable
;
1001 nvlist_t
*cb_batchedsnaps
;
1003 /* first snap in contiguous run */
1005 /* previous snap in contiguous run */
1007 int64_t cb_snapused
;
1013 * Check for any dependents based on the '-r' or '-R' flags.
1016 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
1018 destroy_cbdata_t
*cbp
= data
;
1019 const char *tname
= zfs_get_name(cbp
->cb_target
);
1020 const char *name
= zfs_get_name(zhp
);
1022 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
1023 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
1025 * This is a direct descendant, not a clone somewhere else in
1028 if (cbp
->cb_recurse
)
1031 if (cbp
->cb_first
) {
1032 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1033 "%s has children\n"),
1034 zfs_get_name(cbp
->cb_target
),
1035 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1036 (void) fprintf(stderr
, gettext("use '-r' to destroy "
1037 "the following datasets:\n"));
1038 cbp
->cb_first
= B_FALSE
;
1039 cbp
->cb_error
= B_TRUE
;
1042 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1045 * This is a clone. We only want to report this if the '-r'
1046 * wasn't specified, or the target is a snapshot.
1048 if (!cbp
->cb_recurse
&&
1049 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
1052 if (cbp
->cb_first
) {
1053 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1054 "%s has dependent clones\n"),
1055 zfs_get_name(cbp
->cb_target
),
1056 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1057 (void) fprintf(stderr
, gettext("use '-R' to destroy "
1058 "the following datasets:\n"));
1059 cbp
->cb_first
= B_FALSE
;
1060 cbp
->cb_error
= B_TRUE
;
1061 cbp
->cb_dryrun
= B_TRUE
;
1064 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1073 destroy_callback(zfs_handle_t
*zhp
, void *data
)
1075 destroy_cbdata_t
*cb
= data
;
1076 const char *name
= zfs_get_name(zhp
);
1078 if (cb
->cb_verbose
) {
1079 if (cb
->cb_parsable
) {
1080 (void) printf("destroy\t%s\n", name
);
1081 } else if (cb
->cb_dryrun
) {
1082 (void) printf(gettext("would destroy %s\n"),
1085 (void) printf(gettext("will destroy %s\n"),
1091 * Ignore pools (which we've already flagged as an error before getting
1094 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1095 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1099 if (cb
->cb_dryrun
) {
1105 * We batch up all contiguous snapshots (even of different
1106 * filesystems) and destroy them with one ioctl. We can't
1107 * simply do all snap deletions and then all fs deletions,
1108 * because we must delete a clone before its origin.
1110 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1111 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1113 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1114 cb
->cb_batchedsnaps
, B_FALSE
);
1115 fnvlist_free(cb
->cb_batchedsnaps
);
1116 cb
->cb_batchedsnaps
= fnvlist_alloc();
1119 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1120 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1131 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1133 destroy_cbdata_t
*cb
= arg
;
1134 const char *name
= zfs_get_name(zhp
);
1137 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1138 if (cb
->cb_firstsnap
== NULL
)
1139 cb
->cb_firstsnap
= strdup(name
);
1140 if (cb
->cb_prevsnap
!= NULL
)
1141 free(cb
->cb_prevsnap
);
1142 /* this snap continues the current range */
1143 cb
->cb_prevsnap
= strdup(name
);
1144 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1146 if (cb
->cb_verbose
) {
1147 if (cb
->cb_parsable
) {
1148 (void) printf("destroy\t%s\n", name
);
1149 } else if (cb
->cb_dryrun
) {
1150 (void) printf(gettext("would destroy %s\n"),
1153 (void) printf(gettext("will destroy %s\n"),
1157 } else if (cb
->cb_firstsnap
!= NULL
) {
1158 /* end of this range */
1160 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1161 cb
->cb_prevsnap
, &used
);
1162 cb
->cb_snapused
+= used
;
1163 free(cb
->cb_firstsnap
);
1164 cb
->cb_firstsnap
= NULL
;
1165 free(cb
->cb_prevsnap
);
1166 cb
->cb_prevsnap
= NULL
;
1173 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1176 assert(cb
->cb_firstsnap
== NULL
);
1177 assert(cb
->cb_prevsnap
== NULL
);
1178 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1179 if (cb
->cb_firstsnap
!= NULL
) {
1182 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1183 cb
->cb_prevsnap
, &used
);
1185 cb
->cb_snapused
+= used
;
1186 free(cb
->cb_firstsnap
);
1187 cb
->cb_firstsnap
= NULL
;
1188 free(cb
->cb_prevsnap
);
1189 cb
->cb_prevsnap
= NULL
;
1195 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1197 destroy_cbdata_t
*cb
= arg
;
1200 /* Check for clones. */
1201 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1202 cb
->cb_target
= zhp
;
1203 cb
->cb_first
= B_TRUE
;
1204 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1205 destroy_check_dependent
, cb
);
1209 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1217 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1219 destroy_cbdata_t
*cb
= arg
;
1222 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1228 if (cb
->cb_verbose
) {
1229 err
= destroy_print_snapshots(zhp
, cb
);
1235 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1243 destroy_clones(destroy_cbdata_t
*cb
)
1246 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1248 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1249 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1252 boolean_t defer
= cb
->cb_defer_destroy
;
1256 * We can't defer destroy non-snapshots, so set it to
1257 * false while destroying the clones.
1259 cb
->cb_defer_destroy
= B_FALSE
;
1260 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1261 destroy_callback
, cb
);
1262 cb
->cb_defer_destroy
= defer
;
1272 zfs_do_destroy(int argc
, char **argv
)
1274 destroy_cbdata_t cb
= { 0 };
1278 zfs_handle_t
*zhp
= NULL
;
1280 zfs_type_t type
= ZFS_TYPE_DATASET
;
1283 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1286 cb
.cb_verbose
= B_TRUE
;
1289 cb
.cb_verbose
= B_TRUE
;
1290 cb
.cb_parsable
= B_TRUE
;
1293 cb
.cb_dryrun
= B_TRUE
;
1296 cb
.cb_defer_destroy
= B_TRUE
;
1297 type
= ZFS_TYPE_SNAPSHOT
;
1300 cb
.cb_force
= B_TRUE
;
1303 cb
.cb_recurse
= B_TRUE
;
1306 cb
.cb_recurse
= B_TRUE
;
1307 cb
.cb_doclones
= B_TRUE
;
1311 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1320 /* check number of arguments */
1322 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1326 (void) fprintf(stderr
, gettext("too many arguments\n"));
1330 at
= strchr(argv
[0], '@');
1331 pound
= strchr(argv
[0], '#');
1334 /* Build the list of snaps to destroy in cb_nvl. */
1335 cb
.cb_nvl
= fnvlist_alloc();
1338 zhp
= zfs_open(g_zfs
, argv
[0],
1339 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1341 nvlist_free(cb
.cb_nvl
);
1345 cb
.cb_snapspec
= at
+ 1;
1346 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1352 if (nvlist_empty(cb
.cb_nvl
)) {
1353 (void) fprintf(stderr
, gettext("could not find any "
1354 "snapshots to destroy; check snapshot names.\n"));
1359 if (cb
.cb_verbose
) {
1361 zfs_nicebytes(cb
.cb_snapused
, buf
, sizeof (buf
));
1362 if (cb
.cb_parsable
) {
1363 (void) printf("reclaim\t%llu\n",
1364 (u_longlong_t
)cb
.cb_snapused
);
1365 } else if (cb
.cb_dryrun
) {
1366 (void) printf(gettext("would reclaim %s\n"),
1369 (void) printf(gettext("will reclaim %s\n"),
1374 if (!cb
.cb_dryrun
) {
1375 if (cb
.cb_doclones
) {
1376 cb
.cb_batchedsnaps
= fnvlist_alloc();
1377 err
= destroy_clones(&cb
);
1379 err
= zfs_destroy_snaps_nvl(g_zfs
,
1380 cb
.cb_batchedsnaps
, B_FALSE
);
1388 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1389 cb
.cb_defer_destroy
);
1395 } else if (pound
!= NULL
) {
1400 (void) fprintf(stderr
,
1401 "dryrun is not supported with bookmark\n");
1405 if (cb
.cb_defer_destroy
) {
1406 (void) fprintf(stderr
,
1407 "defer destroy is not supported with bookmark\n");
1411 if (cb
.cb_recurse
) {
1412 (void) fprintf(stderr
,
1413 "recursive is not supported with bookmark\n");
1417 if (!zfs_bookmark_exists(argv
[0])) {
1418 (void) fprintf(stderr
, gettext("bookmark '%s' "
1419 "does not exist.\n"), argv
[0]);
1423 nvl
= fnvlist_alloc();
1424 fnvlist_add_boolean(nvl
, argv
[0]);
1426 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1428 (void) zfs_standard_error(g_zfs
, err
,
1429 "cannot destroy bookmark");
1436 /* Open the given dataset */
1437 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1443 * Perform an explicit check for pools before going any further.
1445 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1446 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1447 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1448 "operation does not apply to pools\n"),
1450 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1451 "%s' to destroy all datasets in the pool\n"),
1453 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1454 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1460 * Check for any dependents and/or clones.
1462 cb
.cb_first
= B_TRUE
;
1463 if (!cb
.cb_doclones
&&
1464 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1475 cb
.cb_batchedsnaps
= fnvlist_alloc();
1476 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1483 * Do the real thing. The callback will close the
1484 * handle regardless of whether it succeeds or not.
1486 err
= destroy_callback(zhp
, &cb
);
1489 err
= zfs_destroy_snaps_nvl(g_zfs
,
1490 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1497 fnvlist_free(cb
.cb_batchedsnaps
);
1498 fnvlist_free(cb
.cb_nvl
);
1505 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1508 zfs_get_column_t col
;
1510 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1511 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1512 if (col
== GET_COL_RECVD
)
1518 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1519 * < all | property[,property]... > < fs | snap | vol > ...
1521 * -r recurse over any child datasets
1522 * -H scripted mode. Headers are stripped, and fields are separated
1523 * by tabs instead of spaces.
1524 * -o Set of fields to display. One of "name,property,value,
1525 * received,source". Default is "name,property,value,source".
1526 * "all" is an alias for all five.
1527 * -s Set of sources to allow. One of
1528 * "local,default,inherited,received,temporary,none". Default is
1530 * -p Display values in parsable (literal) format.
1532 * Prints properties for the given datasets. The user can control which
1533 * columns to display as well as which property types to allow.
1537 * Invoked to display the properties for a single dataset.
1540 get_callback(zfs_handle_t
*zhp
, void *data
)
1542 char buf
[ZFS_MAXPROPLEN
];
1543 char rbuf
[ZFS_MAXPROPLEN
];
1544 zprop_source_t sourcetype
;
1545 char source
[ZFS_MAX_DATASET_NAME_LEN
];
1546 zprop_get_cbdata_t
*cbp
= data
;
1547 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1548 zprop_list_t
*pl
= cbp
->cb_proplist
;
1552 boolean_t received
= is_recvd_column(cbp
);
1554 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1555 char *recvdval
= NULL
;
1557 * Skip the special fake placeholder. This will also skip over
1558 * the name property when 'all' is specified.
1560 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1561 pl
== cbp
->cb_proplist
)
1564 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1565 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1566 sizeof (buf
), &sourcetype
, source
,
1568 cbp
->cb_literal
) != 0) {
1571 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1572 ZFS_TYPE_DATASET
, B_FALSE
)) {
1573 (void) fprintf(stderr
,
1574 gettext("No such property '%s'\n"),
1575 zfs_prop_to_name(pl
->pl_prop
));
1578 sourcetype
= ZPROP_SRC_NONE
;
1579 (void) strlcpy(buf
, "-", sizeof (buf
));
1582 if (received
&& (zfs_prop_get_recvd(zhp
,
1583 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1584 cbp
->cb_literal
) == 0))
1587 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1588 zfs_prop_to_name(pl
->pl_prop
),
1589 buf
, sourcetype
, source
, recvdval
);
1590 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1591 sourcetype
= ZPROP_SRC_LOCAL
;
1593 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1594 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1595 sourcetype
= ZPROP_SRC_NONE
;
1596 (void) strlcpy(buf
, "-", sizeof (buf
));
1599 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1600 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1601 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1602 sourcetype
= ZPROP_SRC_LOCAL
;
1604 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1605 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1606 sourcetype
= ZPROP_SRC_NONE
;
1607 (void) strlcpy(buf
, "-", sizeof (buf
));
1610 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1611 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1613 if (nvlist_lookup_nvlist(user_props
,
1614 pl
->pl_user_prop
, &propval
) != 0) {
1617 sourcetype
= ZPROP_SRC_NONE
;
1620 verify(nvlist_lookup_string(propval
,
1621 ZPROP_VALUE
, &strval
) == 0);
1622 verify(nvlist_lookup_string(propval
,
1623 ZPROP_SOURCE
, &sourceval
) == 0);
1625 if (strcmp(sourceval
,
1626 zfs_get_name(zhp
)) == 0) {
1627 sourcetype
= ZPROP_SRC_LOCAL
;
1628 } else if (strcmp(sourceval
,
1629 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1630 sourcetype
= ZPROP_SRC_RECEIVED
;
1632 sourcetype
= ZPROP_SRC_INHERITED
;
1633 (void) strlcpy(source
,
1634 sourceval
, sizeof (source
));
1638 if (received
&& (zfs_prop_get_recvd(zhp
,
1639 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1640 cbp
->cb_literal
) == 0))
1643 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1644 pl
->pl_user_prop
, strval
, sourcetype
,
1653 zfs_do_get(int argc
, char **argv
)
1655 zprop_get_cbdata_t cb
= { 0 };
1656 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1657 int types
= ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
;
1658 char *value
, *fields
;
1661 zprop_list_t fake_name
= { 0 };
1664 * Set up default columns and sources.
1666 cb
.cb_sources
= ZPROP_SRC_ALL
;
1667 cb
.cb_columns
[0] = GET_COL_NAME
;
1668 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1669 cb
.cb_columns
[2] = GET_COL_VALUE
;
1670 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1671 cb
.cb_type
= ZFS_TYPE_DATASET
;
1674 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1677 cb
.cb_literal
= B_TRUE
;
1680 limit
= parse_depth(optarg
, &flags
);
1683 flags
|= ZFS_ITER_RECURSE
;
1686 cb
.cb_scripted
= B_TRUE
;
1689 (void) fprintf(stderr
, gettext("missing argument for "
1690 "'%c' option\n"), optopt
);
1695 * Process the set of columns to display. We zero out
1696 * the structure to give us a blank slate.
1698 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1700 while (*optarg
!= '\0') {
1701 static char *col_subopts
[] =
1702 { "name", "property", "value", "received",
1703 "source", "all", NULL
};
1705 if (i
== ZFS_GET_NCOLS
) {
1706 (void) fprintf(stderr
, gettext("too "
1707 "many fields given to -o "
1712 switch (getsubopt(&optarg
, col_subopts
,
1715 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1718 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1721 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1724 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1725 flags
|= ZFS_ITER_RECVD_PROPS
;
1728 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1732 (void) fprintf(stderr
,
1733 gettext("\"all\" conflicts "
1734 "with specific fields "
1735 "given to -o option\n"));
1738 cb
.cb_columns
[0] = GET_COL_NAME
;
1739 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1740 cb
.cb_columns
[2] = GET_COL_VALUE
;
1741 cb
.cb_columns
[3] = GET_COL_RECVD
;
1742 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1743 flags
|= ZFS_ITER_RECVD_PROPS
;
1747 (void) fprintf(stderr
,
1748 gettext("invalid column name "
1757 while (*optarg
!= '\0') {
1758 static char *source_subopts
[] = {
1759 "local", "default", "inherited",
1760 "received", "temporary", "none",
1763 switch (getsubopt(&optarg
, source_subopts
,
1766 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1769 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1772 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1775 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1778 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1781 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1784 (void) fprintf(stderr
,
1785 gettext("invalid source "
1794 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1795 while (*optarg
!= '\0') {
1796 static char *type_subopts
[] = { "filesystem",
1797 "volume", "snapshot", "bookmark",
1800 switch (getsubopt(&optarg
, type_subopts
,
1803 types
|= ZFS_TYPE_FILESYSTEM
;
1806 types
|= ZFS_TYPE_VOLUME
;
1809 types
|= ZFS_TYPE_SNAPSHOT
;
1812 types
|= ZFS_TYPE_BOOKMARK
;
1815 types
= ZFS_TYPE_DATASET
|
1820 (void) fprintf(stderr
,
1821 gettext("invalid type '%s'\n"),
1829 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1839 (void) fprintf(stderr
, gettext("missing property "
1846 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1854 * As part of zfs_expand_proplist(), we keep track of the maximum column
1855 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1856 * need to know the maximum name length. However, the user likely did
1857 * not specify 'name' as one of the properties to fetch, so we need to
1858 * make sure we always include at least this property for
1859 * print_get_headers() to work properly.
1861 if (cb
.cb_proplist
!= NULL
) {
1862 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1863 fake_name
.pl_width
= strlen(gettext("NAME"));
1864 fake_name
.pl_next
= cb
.cb_proplist
;
1865 cb
.cb_proplist
= &fake_name
;
1868 cb
.cb_first
= B_TRUE
;
1870 /* run for each object */
1871 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1872 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1874 if (cb
.cb_proplist
== &fake_name
)
1875 zprop_free_list(fake_name
.pl_next
);
1877 zprop_free_list(cb
.cb_proplist
);
1883 * inherit [-rS] <property> <fs|vol> ...
1885 * -r Recurse over all children
1886 * -S Revert to received value, if any
1888 * For each dataset specified on the command line, inherit the given property
1889 * from its parent. Inheriting a property at the pool level will cause it to
1890 * use the default value. The '-r' flag will recurse over all children, and is
1891 * useful for setting a property on a hierarchy-wide basis, regardless of any
1892 * local modifications for each dataset.
1895 typedef struct inherit_cbdata
{
1896 const char *cb_propname
;
1897 boolean_t cb_received
;
1901 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1903 inherit_cbdata_t
*cb
= data
;
1904 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1907 * If we're doing it recursively, then ignore properties that
1908 * are not valid for this type of dataset.
1910 if (prop
!= ZPROP_INVAL
&&
1911 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
), B_FALSE
))
1914 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1918 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1920 inherit_cbdata_t
*cb
= data
;
1922 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1926 zfs_do_inherit(int argc
, char **argv
)
1930 inherit_cbdata_t cb
= { 0 };
1934 boolean_t received
= B_FALSE
;
1937 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1940 flags
|= ZFS_ITER_RECURSE
;
1947 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1956 /* check number of arguments */
1958 (void) fprintf(stderr
, gettext("missing property argument\n"));
1962 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1970 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1971 if (zfs_prop_readonly(prop
)) {
1972 (void) fprintf(stderr
, gettext(
1973 "%s property is read-only\n"),
1977 if (!zfs_prop_inheritable(prop
) && !received
) {
1978 (void) fprintf(stderr
, gettext("'%s' property cannot "
1979 "be inherited\n"), propname
);
1980 if (prop
== ZFS_PROP_QUOTA
||
1981 prop
== ZFS_PROP_RESERVATION
||
1982 prop
== ZFS_PROP_REFQUOTA
||
1983 prop
== ZFS_PROP_REFRESERVATION
) {
1984 (void) fprintf(stderr
, gettext("use 'zfs set "
1985 "%s=none' to clear\n"), propname
);
1986 (void) fprintf(stderr
, gettext("use 'zfs "
1987 "inherit -S %s' to revert to received "
1988 "value\n"), propname
);
1992 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1993 prop
== ZFS_PROP_VERSION
)) {
1994 (void) fprintf(stderr
, gettext("'%s' property cannot "
1995 "be reverted to a received value\n"), propname
);
1998 } else if (!zfs_prop_user(propname
)) {
1999 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
2004 cb
.cb_propname
= propname
;
2005 cb
.cb_received
= received
;
2007 if (flags
& ZFS_ITER_RECURSE
) {
2008 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
2009 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
2011 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
2012 NULL
, NULL
, 0, inherit_cb
, &cb
);
2018 typedef struct upgrade_cbdata
{
2019 uint64_t cb_numupgraded
;
2020 uint64_t cb_numsamegraded
;
2021 uint64_t cb_numfailed
;
2022 uint64_t cb_version
;
2024 boolean_t cb_foundone
;
2025 char cb_lastfs
[ZFS_MAX_DATASET_NAME_LEN
];
2029 same_pool(zfs_handle_t
*zhp
, const char *name
)
2031 int len1
= strcspn(name
, "/@");
2032 const char *zhname
= zfs_get_name(zhp
);
2033 int len2
= strcspn(zhname
, "/@");
2037 return (strncmp(name
, zhname
, len1
) == 0);
2041 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
2043 upgrade_cbdata_t
*cb
= data
;
2044 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2046 /* list if it's old/new */
2047 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
2048 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
2051 str
= gettext("The following filesystems are "
2052 "formatted using a newer software version and\n"
2053 "cannot be accessed on the current system.\n\n");
2055 str
= gettext("The following filesystems are "
2056 "out of date, and can be upgraded. After being\n"
2057 "upgraded, these filesystems (and any 'zfs send' "
2058 "streams generated from\n"
2059 "subsequent snapshots) will no longer be "
2060 "accessible by older software versions.\n\n");
2063 if (!cb
->cb_foundone
) {
2065 (void) printf(gettext("VER FILESYSTEM\n"));
2066 (void) printf(gettext("--- ------------\n"));
2067 cb
->cb_foundone
= B_TRUE
;
2070 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
2077 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2079 upgrade_cbdata_t
*cb
= data
;
2080 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2081 int needed_spa_version
;
2084 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2087 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2089 if (needed_spa_version
< 0)
2092 if (spa_version
< needed_spa_version
) {
2094 (void) printf(gettext("%s: can not be "
2095 "upgraded; the pool version needs to first "
2096 "be upgraded\nto version %d\n\n"),
2097 zfs_get_name(zhp
), needed_spa_version
);
2103 if (version
< cb
->cb_version
) {
2105 (void) snprintf(verstr
, sizeof (verstr
),
2106 "%llu", (u_longlong_t
)cb
->cb_version
);
2107 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2109 * If they did "zfs upgrade -a", then we could
2110 * be doing ioctls to different pools. We need
2111 * to log this history once to each pool, and bypass
2112 * the normal history logging that happens in main().
2114 (void) zpool_log_history(g_zfs
, history_str
);
2115 log_history
= B_FALSE
;
2117 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2118 cb
->cb_numupgraded
++;
2121 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2122 } else if (version
> cb
->cb_version
) {
2123 /* can't downgrade */
2124 (void) printf(gettext("%s: can not be downgraded; "
2125 "it is already at version %u\n"),
2126 zfs_get_name(zhp
), version
);
2129 cb
->cb_numsamegraded
++;
2137 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2140 zfs_do_upgrade(int argc
, char **argv
)
2142 boolean_t all
= B_FALSE
;
2143 boolean_t showversions
= B_FALSE
;
2145 upgrade_cbdata_t cb
= { 0 };
2147 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2150 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2153 flags
|= ZFS_ITER_RECURSE
;
2156 showversions
= B_TRUE
;
2159 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2160 optarg
, &cb
.cb_version
) != 0) {
2161 (void) fprintf(stderr
,
2162 gettext("invalid version %s\n"), optarg
);
2171 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2180 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2182 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2183 cb
.cb_version
|| argc
))
2185 if ((all
|| argc
) && (showversions
))
2191 /* Show info on available versions. */
2192 (void) printf(gettext("The following filesystem versions are "
2194 (void) printf(gettext("VER DESCRIPTION\n"));
2195 (void) printf("--- -----------------------------------------"
2196 "---------------\n");
2197 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2198 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2199 (void) printf(gettext(" 3 Case insensitive and filesystem "
2200 "user identifier (FUID)\n"));
2201 (void) printf(gettext(" 4 userquota, groupquota "
2203 (void) printf(gettext(" 5 System attributes\n"));
2204 (void) printf(gettext("\nFor more information on a particular "
2205 "version, including supported releases,\n"));
2206 (void) printf("see the ZFS Administration Guide.\n\n");
2208 } else if (argc
|| all
) {
2209 /* Upgrade filesystems */
2210 if (cb
.cb_version
== 0)
2211 cb
.cb_version
= ZPL_VERSION
;
2212 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2213 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2214 (void) printf(gettext("%llu filesystems upgraded\n"),
2215 (u_longlong_t
)cb
.cb_numupgraded
);
2216 if (cb
.cb_numsamegraded
) {
2217 (void) printf(gettext("%llu filesystems already at "
2219 (u_longlong_t
)cb
.cb_numsamegraded
);
2221 if (cb
.cb_numfailed
!= 0)
2224 /* List old-version filesystems */
2226 (void) printf(gettext("This system is currently running "
2227 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2229 flags
|= ZFS_ITER_RECURSE
;
2230 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2231 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2233 found
= cb
.cb_foundone
;
2234 cb
.cb_foundone
= B_FALSE
;
2235 cb
.cb_newer
= B_TRUE
;
2237 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2238 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2240 if (!cb
.cb_foundone
&& !found
) {
2241 (void) printf(gettext("All filesystems are "
2242 "formatted with the current version.\n"));
2250 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2251 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2252 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2253 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2255 * -H Scripted mode; elide headers and separate columns by tabs.
2256 * -i Translate SID to POSIX ID.
2257 * -n Print numeric ID instead of user/group name.
2258 * -o Control which fields to display.
2259 * -p Use exact (parsable) numeric output.
2260 * -s Specify sort columns, descending order.
2261 * -S Specify sort columns, ascending order.
2262 * -t Control which object types to display.
2264 * Displays space consumed by, and quotas on, each user in the specified
2265 * filesystem or snapshot.
2268 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2269 enum us_field_types
{
2277 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA",
2278 "OBJUSED", "OBJQUOTA" };
2279 static char *us_field_names
[] = { "type", "name", "used", "quota",
2280 "objused", "objquota" };
2281 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2283 #define USTYPE_PSX_GRP (1 << 0)
2284 #define USTYPE_PSX_USR (1 << 1)
2285 #define USTYPE_SMB_GRP (1 << 2)
2286 #define USTYPE_SMB_USR (1 << 3)
2287 #define USTYPE_ALL \
2288 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2290 static int us_type_bits
[] = {
2297 static char *us_type_names
[] = { "posixgroup", "posixuser", "smbgroup",
2300 typedef struct us_node
{
2302 uu_avl_node_t usn_avlnode
;
2303 uu_list_node_t usn_listnode
;
2306 typedef struct us_cbdata
{
2308 uu_avl_pool_t
*cb_avl_pool
;
2310 boolean_t cb_numname
;
2311 boolean_t cb_nicenum
;
2312 boolean_t cb_sid2posix
;
2313 zfs_userquota_prop_t cb_prop
;
2314 zfs_sort_column_t
*cb_sortcol
;
2315 size_t cb_width
[USFIELD_LAST
];
2318 static boolean_t us_populated
= B_FALSE
;
2321 zfs_sort_column_t
*si_sortcol
;
2322 boolean_t si_numname
;
2326 us_field_index(char *field
)
2330 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2331 if (strcmp(field
, us_field_names
[i
]) == 0)
2339 us_compare(const void *larg
, const void *rarg
, void *unused
)
2341 const us_node_t
*l
= larg
;
2342 const us_node_t
*r
= rarg
;
2343 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2344 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2345 boolean_t numname
= si
->si_numname
;
2346 nvlist_t
*lnvl
= l
->usn_nvl
;
2347 nvlist_t
*rnvl
= r
->usn_nvl
;
2351 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2358 zfs_prop_t prop
= sortcol
->sc_prop
;
2359 const char *propname
= NULL
;
2360 boolean_t reverse
= sortcol
->sc_reverse
;
2365 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2366 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2368 rc
= (rv32
< lv32
) ? 1 : -1;
2374 (void) nvlist_lookup_uint64(lnvl
, propname
,
2376 (void) nvlist_lookup_uint64(rnvl
, propname
,
2379 rc
= (rv64
< lv64
) ? 1 : -1;
2381 if ((nvlist_lookup_string(lnvl
, propname
,
2382 &lvstr
) == ENOENT
) ||
2383 (nvlist_lookup_string(rnvl
, propname
,
2384 &rvstr
) == ENOENT
)) {
2387 rc
= strcmp(lvstr
, rvstr
);
2391 case ZFS_PROP_QUOTA
:
2394 if (prop
== ZFS_PROP_USED
)
2398 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2399 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2401 rc
= (rv64
< lv64
) ? 1 : -1;
2410 return (reverse
? 1 : -1);
2412 return (reverse
? -1 : 1);
2417 * If entries still seem to be the same, check if they are of the same
2418 * type (smbentity is added only if we are doing SID to POSIX ID
2419 * translation where we can have duplicate type/name combinations).
2421 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2422 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2424 return (lvb
< rvb
? -1 : 1);
2430 zfs_prop_is_user(unsigned p
)
2432 return (p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
||
2433 p
== ZFS_PROP_USEROBJUSED
|| p
== ZFS_PROP_USEROBJQUOTA
);
2437 zfs_prop_is_group(unsigned p
)
2439 return (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
||
2440 p
== ZFS_PROP_GROUPOBJUSED
|| p
== ZFS_PROP_GROUPOBJQUOTA
);
2443 static inline const char *
2444 us_type2str(unsigned field_type
)
2446 switch (field_type
) {
2447 case USTYPE_PSX_USR
:
2448 return ("POSIX User");
2449 case USTYPE_PSX_GRP
:
2450 return ("POSIX Group");
2451 case USTYPE_SMB_USR
:
2452 return ("SMB User");
2453 case USTYPE_SMB_GRP
:
2454 return ("SMB Group");
2456 return ("Undefined");
2461 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2463 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2464 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2469 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2470 uu_avl_t
*avl
= cb
->cb_avl
;
2474 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2476 const char *typestr
;
2480 int typeidx
, nameidx
, sizeidx
;
2481 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2482 boolean_t smbentity
= B_FALSE
;
2484 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2486 node
= safe_malloc(sizeof (us_node_t
));
2487 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2488 node
->usn_nvl
= props
;
2490 if (domain
!= NULL
&& domain
[0] != '\0') {
2493 char sid
[MAXNAMELEN
+ 32];
2497 directory_error_t e
;
2501 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2503 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2504 type
= USTYPE_SMB_GRP
;
2505 err
= sid_to_id(sid
, B_FALSE
, &id
);
2507 type
= USTYPE_SMB_USR
;
2508 err
= sid_to_id(sid
, B_TRUE
, &id
);
2513 if (!cb
->cb_sid2posix
) {
2514 e
= directory_name_from_sid(NULL
, sid
, &name
,
2517 directory_error_free(e
);
2527 #endif /* HAVE_IDMAP */
2530 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2532 if (zfs_prop_is_group(prop
)) {
2533 type
= USTYPE_PSX_GRP
;
2534 if (!cb
->cb_numname
) {
2537 if ((g
= getgrgid(rid
)) != NULL
)
2541 type
= USTYPE_PSX_USR
;
2542 if (!cb
->cb_numname
) {
2545 if ((p
= getpwuid(rid
)) != NULL
)
2552 * Make sure that the type/name combination is unique when doing
2553 * SID to POSIX ID translation (hence changing the type from SMB to
2556 if (cb
->cb_sid2posix
&&
2557 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2560 /* Calculate/update width of TYPE field */
2561 typestr
= us_type2str(type
);
2562 typelen
= strlen(gettext(typestr
));
2563 typeidx
= us_field_index("type");
2564 if (typelen
> cb
->cb_width
[typeidx
])
2565 cb
->cb_width
[typeidx
] = typelen
;
2566 if (nvlist_add_uint32(props
, "type", type
) != 0)
2569 /* Calculate/update width of NAME field */
2570 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2571 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2573 namelen
= snprintf(NULL
, 0, "%u", rid
);
2575 if (nvlist_add_string(props
, "name", name
) != 0)
2577 namelen
= strlen(name
);
2579 nameidx
= us_field_index("name");
2580 if (nameidx
>= 0 && namelen
> cb
->cb_width
[nameidx
])
2581 cb
->cb_width
[nameidx
] = namelen
;
2584 * Check if this type/name combination is in the list and update it;
2585 * otherwise add new node to the list.
2587 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2588 uu_avl_insert(avl
, node
, idx
);
2593 props
= node
->usn_nvl
;
2596 /* Calculate/update width of USED/QUOTA fields */
2597 if (cb
->cb_nicenum
) {
2598 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
||
2599 prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2600 zfs_nicebytes(space
, sizebuf
, sizeof (sizebuf
));
2602 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2605 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu",
2606 (u_longlong_t
)space
);
2608 sizelen
= strlen(sizebuf
);
2609 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
) {
2611 if (!nvlist_exists(props
, "quota"))
2612 (void) nvlist_add_uint64(props
, "quota", 0);
2613 } else if (prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2615 if (!nvlist_exists(props
, "used"))
2616 (void) nvlist_add_uint64(props
, "used", 0);
2617 } else if (prop
== ZFS_PROP_USEROBJUSED
||
2618 prop
== ZFS_PROP_GROUPOBJUSED
) {
2619 propname
= "objused";
2620 if (!nvlist_exists(props
, "objquota"))
2621 (void) nvlist_add_uint64(props
, "objquota", 0);
2622 } else if (prop
== ZFS_PROP_USEROBJQUOTA
||
2623 prop
== ZFS_PROP_GROUPOBJQUOTA
) {
2624 propname
= "objquota";
2625 if (!nvlist_exists(props
, "objused"))
2626 (void) nvlist_add_uint64(props
, "objused", 0);
2630 sizeidx
= us_field_index(propname
);
2631 if (sizeidx
>= 0 && sizelen
> cb
->cb_width
[sizeidx
])
2632 cb
->cb_width
[sizeidx
] = sizelen
;
2634 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2641 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2642 size_t *width
, us_node_t
*node
)
2644 nvlist_t
*nvl
= node
->usn_nvl
;
2645 char valstr
[MAXNAMELEN
];
2646 boolean_t first
= B_TRUE
;
2652 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2653 if (!(ustype
& types
))
2656 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2657 nvpair_t
*nvp
= NULL
;
2663 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2664 if (strcmp(nvpair_name(nvp
),
2665 us_field_names
[field
]) == 0)
2669 type
= nvp
== NULL
? DATA_TYPE_UNKNOWN
: nvpair_type(nvp
);
2671 case DATA_TYPE_UINT32
:
2672 (void) nvpair_value_uint32(nvp
, &val32
);
2674 case DATA_TYPE_UINT64
:
2675 (void) nvpair_value_uint64(nvp
, &val64
);
2677 case DATA_TYPE_STRING
:
2678 (void) nvpair_value_string(nvp
, &strval
);
2680 case DATA_TYPE_UNKNOWN
:
2683 (void) fprintf(stderr
, "invalid data type\n");
2688 if (type
== DATA_TYPE_UINT32
)
2689 strval
= (char *)us_type2str(val32
);
2692 if (type
== DATA_TYPE_UINT64
) {
2693 (void) sprintf(valstr
, "%llu",
2694 (u_longlong_t
)val64
);
2700 if (type
== DATA_TYPE_UINT64
) {
2702 (void) sprintf(valstr
, "%llu",
2703 (u_longlong_t
)val64
);
2705 } else if (field
== USFIELD_QUOTA
&&
2709 zfs_nicebytes(val64
, valstr
,
2715 case USFIELD_OBJUSED
:
2716 case USFIELD_OBJQUOTA
:
2717 if (type
== DATA_TYPE_UINT64
) {
2719 (void) sprintf(valstr
, "%llu",
2720 (u_longlong_t
)val64
);
2722 } else if (field
== USFIELD_OBJQUOTA
&&
2726 zfs_nicenum(val64
, valstr
,
2736 (void) printf("\t");
2741 (void) printf("%s", strval
);
2742 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2743 (void) printf("%-*s", (int)width
[field
], strval
);
2745 (void) printf("%*s", (int)width
[field
], strval
);
2751 (void) printf("\n");
2755 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2756 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2764 boolean_t first
= B_TRUE
;
2766 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2767 col
= gettext(us_field_hdr
[field
]);
2768 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2769 (void) printf(first
? "%-*s" : " %-*s",
2770 (int)width
[field
], col
);
2772 (void) printf(first
? "%*s" : " %*s",
2773 (int)width
[field
], col
);
2778 (void) printf("\n");
2781 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2782 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2784 nvlist_free(node
->usn_nvl
);
2789 zfs_do_userspace(int argc
, char **argv
)
2792 zfs_userquota_prop_t p
;
2793 uu_avl_pool_t
*avl_pool
;
2795 uu_avl_walk_t
*walk
;
2797 char deffields
[] = "type,name,used,quota,objused,objquota";
2798 char *ofield
= NULL
;
2799 char *tfield
= NULL
;
2803 boolean_t scripted
= B_FALSE
;
2804 boolean_t prtnum
= B_FALSE
;
2805 boolean_t parsable
= B_FALSE
;
2806 boolean_t sid2posix
= B_FALSE
;
2809 zfs_sort_column_t
*sortcol
= NULL
;
2810 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2814 uu_list_pool_t
*listpool
;
2816 uu_avl_index_t idx
= 0;
2817 uu_list_index_t idx2
= 0;
2822 if (strcmp(argv
[0], "groupspace") == 0)
2823 /* Toggle default group types */
2824 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2826 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2842 if (zfs_add_sort_column(&sortcol
, optarg
,
2843 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
2844 (void) fprintf(stderr
,
2845 gettext("invalid field '%s'\n"), optarg
);
2856 (void) fprintf(stderr
, gettext("missing argument for "
2857 "'%c' option\n"), optopt
);
2861 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2871 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2875 (void) fprintf(stderr
, gettext("too many arguments\n"));
2879 /* Use default output fields if not specified using -o */
2883 if ((delim
= strchr(ofield
, ',')) != NULL
)
2885 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
2886 (void) fprintf(stderr
, gettext("invalid type '%s' "
2887 "for -o option\n"), ofield
);
2892 } while (delim
!= NULL
);
2893 fields
[cfield
] = USFIELD_LAST
;
2895 /* Override output types (-t option) */
2896 if (tfield
!= NULL
) {
2900 boolean_t found
= B_FALSE
;
2902 if ((delim
= strchr(tfield
, ',')) != NULL
)
2904 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
2906 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
2908 types
|= us_type_bits
[i
];
2913 (void) fprintf(stderr
, gettext("invalid type "
2914 "'%s' for -t option\n"), tfield
);
2919 } while (delim
!= NULL
);
2922 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
2925 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
2926 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
2928 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
2931 /* Always add default sorting columns */
2932 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
2933 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
2935 cb
.cb_sortcol
= sortcol
;
2936 cb
.cb_numname
= prtnum
;
2937 cb
.cb_nicenum
= !parsable
;
2938 cb
.cb_avl_pool
= avl_pool
;
2939 cb
.cb_avl
= avl_tree
;
2940 cb
.cb_sid2posix
= sid2posix
;
2942 for (i
= 0; i
< USFIELD_LAST
; i
++)
2943 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
2945 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
2946 if ((zfs_prop_is_user(p
) &&
2947 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
2948 (zfs_prop_is_group(p
) &&
2949 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))))
2953 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
2958 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
2961 us_populated
= B_TRUE
;
2963 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
2964 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
2965 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
2966 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
2968 while (node
!= NULL
) {
2970 node
= uu_avl_next(avl_tree
, node
);
2971 uu_avl_remove(avl_tree
, rmnode
);
2972 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
2973 uu_list_insert(list
, rmnode
, idx2
);
2976 for (node
= uu_list_first(list
); node
!= NULL
;
2977 node
= uu_list_next(list
, node
)) {
2978 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
2980 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
2981 uu_avl_insert(avl_tree
, node
, idx
);
2984 uu_list_destroy(list
);
2985 uu_list_pool_destroy(listpool
);
2987 /* Print and free node nvlist memory */
2988 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
2991 zfs_free_sort_columns(sortcol
);
2993 /* Clean up the AVL tree */
2994 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
2997 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
2998 uu_avl_remove(cb
.cb_avl
, node
);
3002 uu_avl_walk_end(walk
);
3003 uu_avl_destroy(avl_tree
);
3004 uu_avl_pool_destroy(avl_pool
);
3010 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
3011 * [-t type[,...]] [filesystem|volume|snapshot] ...
3013 * -H Scripted mode; elide headers and separate columns by tabs
3014 * -p Display values in parsable (literal) format.
3015 * -r Recurse over all children
3016 * -d Limit recursion by depth.
3017 * -o Control which fields to display.
3018 * -s Specify sort columns, descending order.
3019 * -S Specify sort columns, ascending order.
3020 * -t Control which object types to display.
3022 * When given no arguments, list all filesystems in the system.
3023 * Otherwise, list the specified datasets, optionally recursing down them if
3024 * '-r' is specified.
3026 typedef struct list_cbdata
{
3028 boolean_t cb_literal
;
3029 boolean_t cb_scripted
;
3030 zprop_list_t
*cb_proplist
;
3034 * Given a list of columns to display, output appropriate headers for each one.
3037 print_header(list_cbdata_t
*cb
)
3039 zprop_list_t
*pl
= cb
->cb_proplist
;
3040 char headerbuf
[ZFS_MAXPROPLEN
];
3043 boolean_t first
= B_TRUE
;
3044 boolean_t right_justify
;
3046 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3053 right_justify
= B_FALSE
;
3054 if (pl
->pl_prop
!= ZPROP_INVAL
) {
3055 header
= zfs_prop_column_name(pl
->pl_prop
);
3056 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3058 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
3059 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
3060 headerbuf
[i
] = '\0';
3064 if (pl
->pl_next
== NULL
&& !right_justify
)
3065 (void) printf("%s", header
);
3066 else if (right_justify
)
3067 (void) printf("%*s", (int)pl
->pl_width
, header
);
3069 (void) printf("%-*s", (int)pl
->pl_width
, header
);
3072 (void) printf("\n");
3076 * Given a dataset and a list of fields, print out all the properties according
3077 * to the described layout.
3080 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
3082 zprop_list_t
*pl
= cb
->cb_proplist
;
3083 boolean_t first
= B_TRUE
;
3084 char property
[ZFS_MAXPROPLEN
];
3085 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
3088 boolean_t right_justify
;
3090 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3092 if (cb
->cb_scripted
)
3093 (void) printf("\t");
3100 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
3101 (void) strlcpy(property
, zfs_get_name(zhp
),
3104 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3105 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
3106 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
3107 sizeof (property
), NULL
, NULL
, 0,
3108 cb
->cb_literal
) != 0)
3112 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3113 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
3114 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
3115 property
, sizeof (property
), cb
->cb_literal
) != 0)
3119 right_justify
= B_TRUE
;
3120 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
3121 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
3122 property
, sizeof (property
), cb
->cb_literal
) != 0)
3126 right_justify
= B_TRUE
;
3128 if (nvlist_lookup_nvlist(userprops
,
3129 pl
->pl_user_prop
, &propval
) != 0)
3132 verify(nvlist_lookup_string(propval
,
3133 ZPROP_VALUE
, &propstr
) == 0);
3134 right_justify
= B_FALSE
;
3138 * If this is being called in scripted mode, or if this is the
3139 * last column and it is left-justified, don't include a width
3142 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3143 (void) printf("%s", propstr
);
3144 else if (right_justify
)
3145 (void) printf("%*s", (int)pl
->pl_width
, propstr
);
3147 (void) printf("%-*s", (int)pl
->pl_width
, propstr
);
3150 (void) printf("\n");
3154 * Generic callback function to list a dataset or snapshot.
3157 list_callback(zfs_handle_t
*zhp
, void *data
)
3159 list_cbdata_t
*cbp
= data
;
3161 if (cbp
->cb_first
) {
3162 if (!cbp
->cb_scripted
)
3164 cbp
->cb_first
= B_FALSE
;
3167 print_dataset(zhp
, cbp
);
3173 zfs_do_list(int argc
, char **argv
)
3176 static char default_fields
[] =
3177 "name,used,available,referenced,mountpoint";
3178 int types
= ZFS_TYPE_DATASET
;
3179 boolean_t types_specified
= B_FALSE
;
3180 char *fields
= NULL
;
3181 list_cbdata_t cb
= { 0 };
3185 zfs_sort_column_t
*sortcol
= NULL
;
3186 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3189 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3195 cb
.cb_literal
= B_TRUE
;
3196 flags
|= ZFS_ITER_LITERAL_PROPS
;
3199 limit
= parse_depth(optarg
, &flags
);
3202 flags
|= ZFS_ITER_RECURSE
;
3205 cb
.cb_scripted
= B_TRUE
;
3208 if (zfs_add_sort_column(&sortcol
, optarg
,
3210 (void) fprintf(stderr
,
3211 gettext("invalid property '%s'\n"), optarg
);
3216 if (zfs_add_sort_column(&sortcol
, optarg
,
3218 (void) fprintf(stderr
,
3219 gettext("invalid property '%s'\n"), optarg
);
3225 types_specified
= B_TRUE
;
3226 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3227 while (*optarg
!= '\0') {
3228 static char *type_subopts
[] = { "filesystem",
3229 "volume", "snapshot", "snap", "bookmark",
3232 switch (getsubopt(&optarg
, type_subopts
,
3235 types
|= ZFS_TYPE_FILESYSTEM
;
3238 types
|= ZFS_TYPE_VOLUME
;
3242 types
|= ZFS_TYPE_SNAPSHOT
;
3245 types
|= ZFS_TYPE_BOOKMARK
;
3248 types
= ZFS_TYPE_DATASET
|
3252 (void) fprintf(stderr
,
3253 gettext("invalid type '%s'\n"),
3260 (void) fprintf(stderr
, gettext("missing argument for "
3261 "'%c' option\n"), optopt
);
3265 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3275 fields
= default_fields
;
3278 * If we are only going to list snapshot names and sort by name,
3279 * then we can use faster version.
3281 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3282 flags
|= ZFS_ITER_SIMPLE
;
3285 * If "-o space" and no types were specified, don't display snapshots.
3287 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3288 types
&= ~ZFS_TYPE_SNAPSHOT
;
3291 * If the user specifies '-o all', the zprop_get_list() doesn't
3292 * normally include the name of the dataset. For 'zfs list', we always
3293 * want this property to be first.
3295 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3299 cb
.cb_first
= B_TRUE
;
3301 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3302 limit
, list_callback
, &cb
);
3304 zprop_free_list(cb
.cb_proplist
);
3305 zfs_free_sort_columns(sortcol
);
3307 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3308 (void) fprintf(stderr
, gettext("no datasets available\n"));
3314 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3315 * zfs rename [-f] -p <fs | vol> <fs | vol>
3316 * zfs rename -r <snap> <snap>
3318 * Renames the given dataset to another of the same type.
3320 * The '-p' flag creates all the non-existing ancestors of the target first.
3324 zfs_do_rename(int argc
, char **argv
)
3329 boolean_t recurse
= B_FALSE
;
3330 boolean_t parents
= B_FALSE
;
3331 boolean_t force_unmount
= B_FALSE
;
3334 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3343 force_unmount
= B_TRUE
;
3347 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3356 /* check number of arguments */
3358 (void) fprintf(stderr
, gettext("missing source dataset "
3363 (void) fprintf(stderr
, gettext("missing target dataset "
3368 (void) fprintf(stderr
, gettext("too many arguments\n"));
3372 if (recurse
&& parents
) {
3373 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3378 if (recurse
&& strchr(argv
[0], '@') == 0) {
3379 (void) fprintf(stderr
, gettext("source dataset for recursive "
3380 "rename must be a snapshot\n"));
3384 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3385 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3388 /* If we were asked and the name looks good, try to create ancestors. */
3389 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3390 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3395 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3404 * Promotes the given clone fs to be the parent
3408 zfs_do_promote(int argc
, char **argv
)
3414 if (argc
> 1 && argv
[1][0] == '-') {
3415 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3420 /* check number of arguments */
3422 (void) fprintf(stderr
, gettext("missing clone filesystem"
3427 (void) fprintf(stderr
, gettext("too many arguments\n"));
3431 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3435 ret
= (zfs_promote(zhp
) != 0);
3443 * zfs rollback [-rRf] <snapshot>
3445 * -r Delete any intervening snapshots before doing rollback
3446 * -R Delete any snapshots and their clones
3447 * -f ignored for backwards compatibility
3449 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3450 * since then and making it the active dataset. If more recent snapshots exist,
3451 * the command will complain unless the '-r' flag is given.
3453 typedef struct rollback_cbdata
{
3459 boolean_t cb_recurse
;
3460 } rollback_cbdata_t
;
3463 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3465 rollback_cbdata_t
*cbp
= data
;
3467 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3468 (void) fprintf(stderr
, gettext("cannot rollback to "
3469 "'%s': clones of previous snapshots exist\n"),
3471 (void) fprintf(stderr
, gettext("use '-R' to "
3472 "force deletion of the following clones and "
3478 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3486 * Report any snapshots more recent than the one specified. Used when '-r' is
3487 * not specified. We reuse this same callback for the snapshot dependents - if
3488 * 'cb_dependent' is set, then this is a dependent and we should report it
3489 * without checking the transaction group.
3492 rollback_check(zfs_handle_t
*zhp
, void *data
)
3494 rollback_cbdata_t
*cbp
= data
;
3496 if (cbp
->cb_doclones
) {
3501 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3502 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3503 (void) fprintf(stderr
, gettext("cannot "
3504 "rollback to '%s': more recent snapshots "
3505 "or bookmarks exist\n"),
3507 (void) fprintf(stderr
, gettext("use '-r' to "
3508 "force deletion of the following "
3509 "snapshots and bookmarks:\n"));
3514 if (cbp
->cb_recurse
) {
3515 if (zfs_iter_dependents(zhp
, B_TRUE
,
3516 rollback_check_dependent
, cbp
) != 0) {
3521 (void) fprintf(stderr
, "%s\n",
3530 zfs_do_rollback(int argc
, char **argv
)
3534 boolean_t force
= B_FALSE
;
3535 rollback_cbdata_t cb
= { 0 };
3536 zfs_handle_t
*zhp
, *snap
;
3537 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3541 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3554 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3563 /* check number of arguments */
3565 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3569 (void) fprintf(stderr
, gettext("too many arguments\n"));
3573 /* open the snapshot */
3574 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3577 /* open the parent dataset */
3578 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3579 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3581 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3587 * Check for more recent snapshots and/or clones based on the presence
3590 cb
.cb_target
= argv
[0];
3591 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3592 cb
.cb_first
= B_TRUE
;
3594 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, rollback_check
, &cb
)) != 0)
3596 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
3599 if ((ret
= cb
.cb_error
) != 0)
3603 * Rollback parent to the given snapshot.
3605 ret
= zfs_rollback(zhp
, snap
, force
);
3618 * zfs set property=value ... { fs | snap | vol } ...
3620 * Sets the given properties for all datasets specified on the command line.
3624 set_callback(zfs_handle_t
*zhp
, void *data
)
3626 nvlist_t
*props
= data
;
3628 if (zfs_prop_set_list(zhp
, props
) != 0) {
3629 switch (libzfs_errno(g_zfs
)) {
3630 case EZFS_MOUNTFAILED
:
3631 (void) fprintf(stderr
, gettext("property may be set "
3632 "but unable to remount filesystem\n"));
3634 case EZFS_SHARENFSFAILED
:
3635 (void) fprintf(stderr
, gettext("property may be set "
3636 "but unable to reshare filesystem\n"));
3645 zfs_do_set(int argc
, char **argv
)
3647 nvlist_t
*props
= NULL
;
3648 int ds_start
= -1; /* argv idx of first dataset arg */
3652 /* check for options */
3653 if (argc
> 1 && argv
[1][0] == '-') {
3654 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3659 /* check number of arguments */
3661 (void) fprintf(stderr
, gettext("missing arguments\n"));
3665 if (strchr(argv
[1], '=') == NULL
) {
3666 (void) fprintf(stderr
, gettext("missing property=value "
3669 (void) fprintf(stderr
, gettext("missing dataset "
3675 /* validate argument order: prop=val args followed by dataset args */
3676 for (i
= 1; i
< argc
; i
++) {
3677 if (strchr(argv
[i
], '=') != NULL
) {
3679 /* out-of-order prop=val argument */
3680 (void) fprintf(stderr
, gettext("invalid "
3681 "argument order\n"));
3684 } else if (ds_start
< 0) {
3689 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
3693 /* Populate a list of property settings */
3694 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3696 for (i
= 1; i
< ds_start
; i
++) {
3697 if (!parseprop(props
, argv
[i
])) {
3703 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
3704 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, props
);
3711 typedef struct snap_cbdata
{
3713 boolean_t sd_recursive
;
3714 const char *sd_snapname
;
3718 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3720 snap_cbdata_t
*sd
= arg
;
3725 if (sd
->sd_recursive
&&
3726 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
3731 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3734 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3737 if (sd
->sd_recursive
)
3738 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3744 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3746 * Creates a snapshot with the given name. While functionally equivalent to
3747 * 'zfs create', it is a separate command to differentiate intent.
3750 zfs_do_snapshot(int argc
, char **argv
)
3755 snap_cbdata_t sd
= { 0 };
3756 boolean_t multiple_snaps
= B_FALSE
;
3758 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3760 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3764 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3767 if (!parseprop(props
, optarg
)) {
3768 nvlist_free(sd
.sd_nvl
);
3774 sd
.sd_recursive
= B_TRUE
;
3775 multiple_snaps
= B_TRUE
;
3778 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3787 /* check number of arguments */
3789 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3794 multiple_snaps
= B_TRUE
;
3795 for (; argc
> 0; argc
--, argv
++) {
3799 atp
= strchr(argv
[0], '@');
3803 sd
.sd_snapname
= atp
+ 1;
3804 zhp
= zfs_open(g_zfs
, argv
[0],
3805 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3808 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3812 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3813 nvlist_free(sd
.sd_nvl
);
3815 if (ret
!= 0 && multiple_snaps
)
3816 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3820 nvlist_free(sd
.sd_nvl
);
3827 * Send a backup stream to stdout.
3830 zfs_do_send(int argc
, char **argv
)
3832 char *fromname
= NULL
;
3833 char *toname
= NULL
;
3834 char *resume_token
= NULL
;
3837 sendflags_t flags
= { 0 };
3839 nvlist_t
*dbgnv
= NULL
;
3840 boolean_t extraverbose
= B_FALSE
;
3842 struct option long_options
[] = {
3843 {"replicate", no_argument
, NULL
, 'R'},
3844 {"props", no_argument
, NULL
, 'p'},
3845 {"parsable", no_argument
, NULL
, 'P'},
3846 {"dedup", no_argument
, NULL
, 'D'},
3847 {"verbose", no_argument
, NULL
, 'v'},
3848 {"dryrun", no_argument
, NULL
, 'n'},
3849 {"large-block", no_argument
, NULL
, 'L'},
3850 {"embed", no_argument
, NULL
, 'e'},
3851 {"resume", required_argument
, NULL
, 't'},
3852 {"compressed", no_argument
, NULL
, 'c'},
3853 {"raw", no_argument
, NULL
, 'w'},
3858 while ((c
= getopt_long(argc
, argv
, ":i:I:RDpvnPLet:cw", long_options
,
3870 flags
.doall
= B_TRUE
;
3873 flags
.replicate
= B_TRUE
;
3876 flags
.props
= B_TRUE
;
3879 flags
.parsable
= B_TRUE
;
3880 flags
.verbose
= B_TRUE
;
3884 extraverbose
= B_TRUE
;
3885 flags
.verbose
= B_TRUE
;
3886 flags
.progress
= B_TRUE
;
3889 flags
.dedup
= B_TRUE
;
3892 flags
.dryrun
= B_TRUE
;
3895 flags
.largeblock
= B_TRUE
;
3898 flags
.embed_data
= B_TRUE
;
3901 resume_token
= optarg
;
3904 flags
.compress
= B_TRUE
;
3908 flags
.compress
= B_TRUE
;
3909 flags
.embed_data
= B_TRUE
;
3910 flags
.largeblock
= B_TRUE
;
3914 * If a parameter was not passed, optopt contains the
3915 * value that would normally lead us into the
3916 * appropriate case statement. If it's > 256, then this
3917 * must be a longopt and we should look at argv to get
3918 * the string. Otherwise it's just the character, so we
3919 * should use it directly.
3921 if (optopt
<= UINT8_MAX
) {
3922 (void) fprintf(stderr
,
3923 gettext("missing argument for '%c' "
3924 "option\n"), optopt
);
3926 (void) fprintf(stderr
,
3927 gettext("missing argument for '%s' "
3928 "option\n"), argv
[optind
- 1]);
3936 * If an invalid flag was passed, optopt contains the
3937 * character if it was a short flag, or 0 if it was a
3941 (void) fprintf(stderr
,
3942 gettext("invalid option '%c'\n"), optopt
);
3944 (void) fprintf(stderr
,
3945 gettext("invalid option '%s'\n"),
3956 if (resume_token
!= NULL
) {
3957 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
3959 (void) fprintf(stderr
,
3960 gettext("invalid flags combined with -t\n"));
3964 (void) fprintf(stderr
, gettext("no additional "
3965 "arguments are permitted with -t\n"));
3970 (void) fprintf(stderr
,
3971 gettext("missing snapshot argument\n"));
3975 (void) fprintf(stderr
, gettext("too many arguments\n"));
3980 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
3981 (void) fprintf(stderr
,
3982 gettext("Error: Stream can not be written to a terminal.\n"
3983 "You must redirect standard output.\n"));
3987 if (resume_token
!= NULL
) {
3988 return (zfs_send_resume(g_zfs
, &flags
, STDOUT_FILENO
,
3993 * Special case sending a filesystem, or from a bookmark.
3995 if (strchr(argv
[0], '@') == NULL
||
3996 (fromname
&& strchr(fromname
, '#') != NULL
)) {
3997 char frombuf
[ZFS_MAX_DATASET_NAME_LEN
];
3998 enum lzc_send_flags lzc_flags
= 0;
4000 if (flags
.replicate
|| flags
.doall
|| flags
.props
||
4001 flags
.dedup
|| flags
.dryrun
|| flags
.verbose
||
4003 (void) fprintf(stderr
,
4005 "Unsupported flag with filesystem or bookmark.\n"));
4009 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
4013 if (flags
.largeblock
)
4014 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
4015 if (flags
.embed_data
)
4016 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
4018 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
4020 lzc_flags
|= LZC_SEND_FLAG_RAW
;
4022 if (fromname
!= NULL
&&
4023 (fromname
[0] == '#' || fromname
[0] == '@')) {
4025 * Incremental source name begins with # or @.
4026 * Default to same fs as target.
4028 (void) strlcpy(frombuf
, argv
[0], sizeof (frombuf
));
4029 cp
= strchr(frombuf
, '@');
4032 (void) strlcat(frombuf
, fromname
, sizeof (frombuf
));
4035 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, lzc_flags
);
4040 cp
= strchr(argv
[0], '@');
4043 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4048 * If they specified the full path to the snapshot, chop off
4049 * everything except the short name of the snapshot, but special
4050 * case if they specify the origin.
4052 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
4053 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
4056 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
4057 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
4059 if (strcmp(origin
, fromname
) == 0) {
4061 flags
.fromorigin
= B_TRUE
;
4064 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
4065 (void) fprintf(stderr
,
4066 gettext("incremental source must be "
4067 "in same filesystem\n"));
4071 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
4072 (void) fprintf(stderr
,
4073 gettext("invalid incremental source\n"));
4079 if (flags
.replicate
&& fromname
== NULL
)
4080 flags
.doall
= B_TRUE
;
4082 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
4083 extraverbose
? &dbgnv
: NULL
);
4085 if (extraverbose
&& dbgnv
!= NULL
) {
4087 * dump_nvlist prints to stdout, but that's been
4088 * redirected to a file. Make it print to stderr
4091 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
4092 dump_nvlist(dbgnv
, 0);
4101 * Restore a backup stream from stdin.
4104 zfs_do_receive(int argc
, char **argv
)
4107 recvflags_t flags
= { 0 };
4108 boolean_t abort_resumable
= B_FALSE
;
4111 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
4115 while ((c
= getopt(argc
, argv
, ":o:x:denuvFsA")) != -1) {
4118 if (!parseprop(props
, optarg
)) {
4124 if (!parsepropname(props
, optarg
)) {
4130 flags
.isprefix
= B_TRUE
;
4133 flags
.isprefix
= B_TRUE
;
4134 flags
.istail
= B_TRUE
;
4137 flags
.dryrun
= B_TRUE
;
4140 flags
.nomount
= B_TRUE
;
4143 flags
.verbose
= B_TRUE
;
4146 flags
.resumable
= B_TRUE
;
4149 flags
.force
= B_TRUE
;
4152 abort_resumable
= B_TRUE
;
4155 (void) fprintf(stderr
, gettext("missing argument for "
4156 "'%c' option\n"), optopt
);
4160 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4169 /* check number of arguments */
4171 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4175 (void) fprintf(stderr
, gettext("too many arguments\n"));
4179 if (abort_resumable
) {
4180 if (flags
.isprefix
|| flags
.istail
|| flags
.dryrun
||
4181 flags
.resumable
|| flags
.nomount
) {
4182 (void) fprintf(stderr
, gettext("invalid option\n"));
4186 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
4187 (void) snprintf(namebuf
, sizeof (namebuf
),
4188 "%s/%%recv", argv
[0]);
4190 if (zfs_dataset_exists(g_zfs
, namebuf
,
4191 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) {
4192 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4193 namebuf
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4198 err
= zfs_destroy(zhp
, B_FALSE
);
4201 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4202 argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4205 if (!zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) ||
4206 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
4207 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == -1) {
4208 (void) fprintf(stderr
,
4209 gettext("'%s' does not have any "
4210 "resumable receive state to abort\n"),
4216 err
= zfs_destroy(zhp
, B_FALSE
);
4223 if (isatty(STDIN_FILENO
)) {
4224 (void) fprintf(stderr
,
4225 gettext("Error: Backup stream can not be read "
4226 "from a terminal.\n"
4227 "You must redirect standard input.\n"));
4231 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
4238 * allow/unallow stuff
4240 /* copied from zfs/sys/dsl_deleg.h */
4241 #define ZFS_DELEG_PERM_CREATE "create"
4242 #define ZFS_DELEG_PERM_DESTROY "destroy"
4243 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4244 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4245 #define ZFS_DELEG_PERM_CLONE "clone"
4246 #define ZFS_DELEG_PERM_PROMOTE "promote"
4247 #define ZFS_DELEG_PERM_RENAME "rename"
4248 #define ZFS_DELEG_PERM_MOUNT "mount"
4249 #define ZFS_DELEG_PERM_SHARE "share"
4250 #define ZFS_DELEG_PERM_SEND "send"
4251 #define ZFS_DELEG_PERM_RECEIVE "receive"
4252 #define ZFS_DELEG_PERM_ALLOW "allow"
4253 #define ZFS_DELEG_PERM_USERPROP "userprop"
4254 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4255 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4256 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4257 #define ZFS_DELEG_PERM_USERUSED "userused"
4258 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4259 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
4260 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
4261 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
4262 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
4264 #define ZFS_DELEG_PERM_HOLD "hold"
4265 #define ZFS_DELEG_PERM_RELEASE "release"
4266 #define ZFS_DELEG_PERM_DIFF "diff"
4267 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4268 #define ZFS_DELEG_PERM_LOAD_KEY "load-key"
4269 #define ZFS_DELEG_PERM_CHANGE_KEY "change-key"
4271 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4273 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
4274 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
4275 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
4276 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
4277 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
4278 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
4279 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
4280 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
4281 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
4282 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
4283 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
4284 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
4285 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
4286 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
4287 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
4288 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
4289 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
4290 { ZFS_DELEG_PERM_LOAD_KEY
, ZFS_DELEG_NOTE_LOAD_KEY
},
4291 { ZFS_DELEG_PERM_CHANGE_KEY
, ZFS_DELEG_NOTE_CHANGE_KEY
},
4293 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
4294 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
4295 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
4296 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
4297 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
4298 { ZFS_DELEG_PERM_USEROBJQUOTA
, ZFS_DELEG_NOTE_USEROBJQUOTA
},
4299 { ZFS_DELEG_PERM_USEROBJUSED
, ZFS_DELEG_NOTE_USEROBJUSED
},
4300 { ZFS_DELEG_PERM_GROUPOBJQUOTA
, ZFS_DELEG_NOTE_GROUPOBJQUOTA
},
4301 { ZFS_DELEG_PERM_GROUPOBJUSED
, ZFS_DELEG_NOTE_GROUPOBJUSED
},
4302 { NULL
, ZFS_DELEG_NOTE_NONE
}
4305 /* permission structure */
4306 typedef struct deleg_perm
{
4307 zfs_deleg_who_type_t dp_who_type
;
4308 const char *dp_name
;
4310 boolean_t dp_descend
;
4314 typedef struct deleg_perm_node
{
4315 deleg_perm_t dpn_perm
;
4317 uu_avl_node_t dpn_avl_node
;
4318 } deleg_perm_node_t
;
4320 typedef struct fs_perm fs_perm_t
;
4322 /* permissions set */
4323 typedef struct who_perm
{
4324 zfs_deleg_who_type_t who_type
;
4325 const char *who_name
; /* id */
4326 char who_ug_name
[256]; /* user/group name */
4327 fs_perm_t
*who_fsperm
; /* uplink */
4329 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
4333 typedef struct who_perm_node
{
4334 who_perm_t who_perm
;
4335 uu_avl_node_t who_avl_node
;
4338 typedef struct fs_perm_set fs_perm_set_t
;
4339 /* fs permissions */
4341 const char *fsp_name
;
4343 uu_avl_t
*fsp_sc_avl
; /* sets,create */
4344 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
4346 fs_perm_set_t
*fsp_set
; /* uplink */
4350 typedef struct fs_perm_node
{
4351 fs_perm_t fspn_fsperm
;
4354 uu_list_node_t fspn_list_node
;
4357 /* top level structure */
4358 struct fs_perm_set
{
4359 uu_list_pool_t
*fsps_list_pool
;
4360 uu_list_t
*fsps_list
; /* list of fs_perms */
4362 uu_avl_pool_t
*fsps_named_set_avl_pool
;
4363 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
4364 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
4367 static inline const char *
4368 deleg_perm_type(zfs_deleg_note_t note
)
4374 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4375 case ZFS_DELEG_NOTE_GROUPUSED
:
4376 case ZFS_DELEG_NOTE_USERPROP
:
4377 case ZFS_DELEG_NOTE_USERQUOTA
:
4378 case ZFS_DELEG_NOTE_USERUSED
:
4379 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
4380 case ZFS_DELEG_NOTE_USEROBJUSED
:
4381 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
4382 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
4384 return (gettext("other"));
4386 return (gettext("subcommand"));
4391 who_type2weight(zfs_deleg_who_type_t who_type
)
4395 case ZFS_DELEG_NAMED_SET_SETS
:
4396 case ZFS_DELEG_NAMED_SET
:
4399 case ZFS_DELEG_CREATE_SETS
:
4400 case ZFS_DELEG_CREATE
:
4403 case ZFS_DELEG_USER_SETS
:
4404 case ZFS_DELEG_USER
:
4407 case ZFS_DELEG_GROUP_SETS
:
4408 case ZFS_DELEG_GROUP
:
4411 case ZFS_DELEG_EVERYONE_SETS
:
4412 case ZFS_DELEG_EVERYONE
:
4424 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4426 const who_perm_node_t
*l
= larg
;
4427 const who_perm_node_t
*r
= rarg
;
4428 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
4429 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
4430 int lweight
= who_type2weight(ltype
);
4431 int rweight
= who_type2weight(rtype
);
4432 int res
= lweight
- rweight
;
4434 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
4435 ZFS_MAX_DELEG_NAME
-1);
4447 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4449 const deleg_perm_node_t
*l
= larg
;
4450 const deleg_perm_node_t
*r
= rarg
;
4451 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
4452 ZFS_MAX_DELEG_NAME
-1);
4464 fs_perm_set_init(fs_perm_set_t
*fspset
)
4466 bzero(fspset
, sizeof (fs_perm_set_t
));
4468 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4469 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4470 NULL
, UU_DEFAULT
)) == NULL
)
4472 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4473 UU_DEFAULT
)) == NULL
)
4476 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4477 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4478 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4479 UU_DEFAULT
)) == NULL
)
4482 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4483 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4484 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4485 UU_DEFAULT
)) == NULL
)
4488 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4489 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4490 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4495 static inline void fs_perm_fini(fs_perm_t
*);
4496 static inline void who_perm_fini(who_perm_t
*);
4499 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4501 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4503 while (node
!= NULL
) {
4504 fs_perm_node_t
*next_node
=
4505 uu_list_next(fspset
->fsps_list
, node
);
4506 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4507 fs_perm_fini(fsperm
);
4508 uu_list_remove(fspset
->fsps_list
, node
);
4513 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4514 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4515 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4519 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4522 deleg_perm
->dp_who_type
= type
;
4523 deleg_perm
->dp_name
= name
;
4527 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4528 zfs_deleg_who_type_t type
, const char *name
)
4530 uu_avl_pool_t
*pool
;
4531 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4533 bzero(who_perm
, sizeof (who_perm_t
));
4535 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4536 UU_DEFAULT
)) == NULL
)
4539 who_perm
->who_type
= type
;
4540 who_perm
->who_name
= name
;
4541 who_perm
->who_fsperm
= fsperm
;
4545 who_perm_fini(who_perm_t
*who_perm
)
4547 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4549 while (node
!= NULL
) {
4550 deleg_perm_node_t
*next_node
=
4551 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4553 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4558 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4562 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4564 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4565 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4567 bzero(fsperm
, sizeof (fs_perm_t
));
4569 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4573 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4577 fsperm
->fsp_set
= fspset
;
4578 fsperm
->fsp_name
= fsname
;
4582 fs_perm_fini(fs_perm_t
*fsperm
)
4584 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4585 while (node
!= NULL
) {
4586 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4588 who_perm_t
*who_perm
= &node
->who_perm
;
4589 who_perm_fini(who_perm
);
4590 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4595 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4596 while (node
!= NULL
) {
4597 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4599 who_perm_t
*who_perm
= &node
->who_perm
;
4600 who_perm_fini(who_perm
);
4601 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4606 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4607 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4611 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4612 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4614 uu_avl_index_t idx
= 0;
4616 deleg_perm_node_t
*found_node
= NULL
;
4617 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4619 deleg_perm_init(deleg_perm
, who_type
, name
);
4621 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4623 uu_avl_insert(avl
, node
, idx
);
4626 deleg_perm
= &node
->dpn_perm
;
4631 case ZFS_DELEG_LOCAL
:
4632 deleg_perm
->dp_local
= B_TRUE
;
4634 case ZFS_DELEG_DESCENDENT
:
4635 deleg_perm
->dp_descend
= B_TRUE
;
4640 assert(B_FALSE
); /* invalid locality */
4645 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4647 nvpair_t
*nvp
= NULL
;
4648 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4649 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4650 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4652 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4653 const char *name
= nvpair_name(nvp
);
4654 data_type_t type
= nvpair_type(nvp
);
4655 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4656 deleg_perm_node_t
*node
=
4657 safe_malloc(sizeof (deleg_perm_node_t
));
4659 VERIFY(type
== DATA_TYPE_BOOLEAN
);
4661 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4662 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4669 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4671 nvpair_t
*nvp
= NULL
;
4672 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4674 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4675 nvlist_t
*nvl2
= NULL
;
4676 const char *name
= nvpair_name(nvp
);
4677 uu_avl_t
*avl
= NULL
;
4678 uu_avl_pool_t
*avl_pool
= NULL
;
4679 zfs_deleg_who_type_t perm_type
= name
[0];
4680 char perm_locality
= name
[1];
4681 const char *perm_name
= name
+ 3;
4682 boolean_t is_set
= B_TRUE
;
4683 who_perm_t
*who_perm
= NULL
;
4685 assert('$' == name
[2]);
4687 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4690 switch (perm_type
) {
4691 case ZFS_DELEG_CREATE
:
4692 case ZFS_DELEG_CREATE_SETS
:
4693 case ZFS_DELEG_NAMED_SET
:
4694 case ZFS_DELEG_NAMED_SET_SETS
:
4695 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4696 avl
= fsperm
->fsp_sc_avl
;
4698 case ZFS_DELEG_USER
:
4699 case ZFS_DELEG_USER_SETS
:
4700 case ZFS_DELEG_GROUP
:
4701 case ZFS_DELEG_GROUP_SETS
:
4702 case ZFS_DELEG_EVERYONE
:
4703 case ZFS_DELEG_EVERYONE_SETS
:
4704 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4705 avl
= fsperm
->fsp_uge_avl
;
4709 assert(!"unhandled zfs_deleg_who_type_t");
4713 who_perm_node_t
*found_node
= NULL
;
4714 who_perm_node_t
*node
= safe_malloc(
4715 sizeof (who_perm_node_t
));
4716 who_perm
= &node
->who_perm
;
4717 uu_avl_index_t idx
= 0;
4719 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4720 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4722 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4724 if (avl
== fsperm
->fsp_uge_avl
) {
4726 struct passwd
*p
= NULL
;
4727 struct group
*g
= NULL
;
4728 const char *nice_name
= NULL
;
4730 switch (perm_type
) {
4731 case ZFS_DELEG_USER_SETS
:
4732 case ZFS_DELEG_USER
:
4733 rid
= atoi(perm_name
);
4736 nice_name
= p
->pw_name
;
4738 case ZFS_DELEG_GROUP_SETS
:
4739 case ZFS_DELEG_GROUP
:
4740 rid
= atoi(perm_name
);
4743 nice_name
= g
->gr_name
;
4750 if (nice_name
!= NULL
)
4752 node
->who_perm
.who_ug_name
,
4756 uu_avl_insert(avl
, node
, idx
);
4759 who_perm
= &node
->who_perm
;
4762 VERIFY3P(who_perm
, !=, NULL
);
4763 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4770 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4772 nvpair_t
*nvp
= NULL
;
4773 uu_avl_index_t idx
= 0;
4775 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4776 nvlist_t
*nvl2
= NULL
;
4777 const char *fsname
= nvpair_name(nvp
);
4778 data_type_t type
= nvpair_type(nvp
);
4779 fs_perm_t
*fsperm
= NULL
;
4780 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4784 fsperm
= &node
->fspn_fsperm
;
4786 VERIFY(DATA_TYPE_NVLIST
== type
);
4788 uu_list_node_init(node
, &node
->fspn_list_node
,
4789 fspset
->fsps_list_pool
);
4791 idx
= uu_list_numnodes(fspset
->fsps_list
);
4792 fs_perm_init(fsperm
, fspset
, fsname
);
4794 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4797 (void) parse_fs_perm(fsperm
, nvl2
);
4799 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4805 static inline const char *
4806 deleg_perm_comment(zfs_deleg_note_t note
)
4808 const char *str
= "";
4813 case ZFS_DELEG_NOTE_ALLOW
:
4814 str
= gettext("Must also have the permission that is being"
4815 "\n\t\t\t\tallowed");
4817 case ZFS_DELEG_NOTE_CLONE
:
4818 str
= gettext("Must also have the 'create' ability and 'mount'"
4819 "\n\t\t\t\tability in the origin file system");
4821 case ZFS_DELEG_NOTE_CREATE
:
4822 str
= gettext("Must also have the 'mount' ability");
4824 case ZFS_DELEG_NOTE_DESTROY
:
4825 str
= gettext("Must also have the 'mount' ability");
4827 case ZFS_DELEG_NOTE_DIFF
:
4828 str
= gettext("Allows lookup of paths within a dataset;"
4829 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4830 "\n\t\t\t\tin order to use zfs diff");
4832 case ZFS_DELEG_NOTE_HOLD
:
4833 str
= gettext("Allows adding a user hold to a snapshot");
4835 case ZFS_DELEG_NOTE_MOUNT
:
4836 str
= gettext("Allows mount/umount of ZFS datasets");
4838 case ZFS_DELEG_NOTE_PROMOTE
:
4839 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4840 " 'promote' ability in the origin file system");
4842 case ZFS_DELEG_NOTE_RECEIVE
:
4843 str
= gettext("Must also have the 'mount' and 'create'"
4846 case ZFS_DELEG_NOTE_RELEASE
:
4847 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4848 "might destroy the snapshot");
4850 case ZFS_DELEG_NOTE_RENAME
:
4851 str
= gettext("Must also have the 'mount' and 'create'"
4852 "\n\t\t\t\tability in the new parent");
4854 case ZFS_DELEG_NOTE_ROLLBACK
:
4857 case ZFS_DELEG_NOTE_SEND
:
4860 case ZFS_DELEG_NOTE_SHARE
:
4861 str
= gettext("Allows sharing file systems over NFS or SMB"
4862 "\n\t\t\t\tprotocols");
4864 case ZFS_DELEG_NOTE_SNAPSHOT
:
4867 case ZFS_DELEG_NOTE_LOAD_KEY
:
4868 str
= gettext("Allows loading or unloading an encryption key");
4870 case ZFS_DELEG_NOTE_CHANGE_KEY
:
4871 str
= gettext("Allows changing or adding an encryption key");
4874 * case ZFS_DELEG_NOTE_VSCAN:
4875 * str = gettext("");
4879 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4880 str
= gettext("Allows accessing any groupquota@... property");
4882 case ZFS_DELEG_NOTE_GROUPUSED
:
4883 str
= gettext("Allows reading any groupused@... property");
4885 case ZFS_DELEG_NOTE_USERPROP
:
4886 str
= gettext("Allows changing any user property");
4888 case ZFS_DELEG_NOTE_USERQUOTA
:
4889 str
= gettext("Allows accessing any userquota@... property");
4891 case ZFS_DELEG_NOTE_USERUSED
:
4892 str
= gettext("Allows reading any userused@... property");
4894 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
4895 str
= gettext("Allows accessing any userobjquota@... property");
4897 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
4898 str
= gettext("Allows accessing any \n\t\t\t\t"
4899 "groupobjquota@... property");
4901 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
4902 str
= gettext("Allows reading any groupobjused@... property");
4904 case ZFS_DELEG_NOTE_USEROBJUSED
:
4905 str
= gettext("Allows reading any userobjused@... property");
4923 boolean_t recursive
; /* unallow only */
4924 boolean_t prt_usage
;
4926 boolean_t prt_perms
;
4929 const char *dataset
;
4933 prop_cmp(const void *a
, const void *b
)
4935 const char *str1
= *(const char **)a
;
4936 const char *str2
= *(const char **)b
;
4937 return (strcmp(str1
, str2
));
4941 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
4943 const char *opt_desc
[] = {
4944 "-h", gettext("show this help message and exit"),
4945 "-l", gettext("set permission locally"),
4946 "-d", gettext("set permission for descents"),
4947 "-u", gettext("set permission for user"),
4948 "-g", gettext("set permission for group"),
4949 "-e", gettext("set permission for everyone"),
4950 "-c", gettext("set create time permission"),
4951 "-s", gettext("define permission set"),
4953 "-r", gettext("remove permissions recursively"),
4955 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
4956 size_t allow_size
= unallow_size
- 2;
4957 const char *props
[ZFS_NUM_PROPS
];
4960 FILE *fp
= requested
? stdout
: stderr
;
4961 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
4962 const char *fmt
= gettext("%-16s %-14s\t%s\n");
4964 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
4966 (void) fprintf(fp
, gettext("Options:\n"));
4967 for (i
= 0; i
< (un
? unallow_size
: allow_size
); i
+= 2) {
4968 const char *opt
= opt_desc
[i
];
4969 const char *optdsc
= opt_desc
[i
+ 1];
4970 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
4973 (void) fprintf(fp
, gettext("\nThe following permissions are "
4975 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
4977 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
4978 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
4979 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
4980 const char *perm_type
= deleg_perm_type(perm_note
);
4981 const char *perm_comment
= deleg_perm_comment(perm_note
);
4982 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
4985 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
4986 zprop_desc_t
*pd
= &pdtbl
[i
];
4987 if (pd
->pd_visible
!= B_TRUE
)
4990 if (pd
->pd_attr
== PROP_READONLY
)
4993 props
[count
++] = pd
->pd_name
;
4995 props
[count
] = NULL
;
4997 qsort(props
, count
, sizeof (char *), prop_cmp
);
4999 for (i
= 0; i
< count
; i
++)
5000 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
5003 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
5005 exit(requested
? 0 : 2);
5008 static inline const char *
5009 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
5012 if (un
&& argc
== expected_argc
- 1)
5014 else if (argc
== expected_argc
)
5015 *permsp
= argv
[argc
- 2];
5017 allow_usage(un
, B_FALSE
,
5018 gettext("wrong number of parameters\n"));
5020 return (argv
[argc
- 1]);
5024 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
5026 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
5027 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
5028 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
5029 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
5032 allow_usage(un
, B_FALSE
,
5033 gettext("-u, -g, and -e are mutually exclusive\n"));
5035 if (opts
->prt_usage
) {
5036 if (argc
== 0 && all_sum
== 0)
5037 allow_usage(un
, B_TRUE
, NULL
);
5044 allow_usage(un
, B_FALSE
,
5045 gettext("invalid options combined with -s\n"));
5047 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
5048 if (argv
[0][0] != '@')
5049 allow_usage(un
, B_FALSE
,
5050 gettext("invalid set name: missing '@' prefix\n"));
5051 opts
->who
= argv
[0];
5052 } else if (opts
->create
) {
5053 if (ldcsuge_sum
> 1)
5054 allow_usage(un
, B_FALSE
,
5055 gettext("invalid options combined with -c\n"));
5056 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5057 } else if (opts
->everyone
) {
5059 allow_usage(un
, B_FALSE
,
5060 gettext("invalid options combined with -e\n"));
5061 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5062 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
5064 opts
->everyone
= B_TRUE
;
5067 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5068 } else if (argc
== 1 && !un
) {
5069 opts
->prt_perms
= B_TRUE
;
5070 opts
->dataset
= argv
[argc
-1];
5072 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
5073 opts
->who
= argv
[0];
5076 if (!opts
->local
&& !opts
->descend
) {
5077 opts
->local
= B_TRUE
;
5078 opts
->descend
= B_TRUE
;
5083 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
5084 const char *who
, char *perms
, nvlist_t
*top_nvl
)
5087 char ld
[2] = { '\0', '\0' };
5088 char who_buf
[MAXNAMELEN
+ 32];
5089 char base_type
= '\0';
5090 char set_type
= '\0';
5091 nvlist_t
*base_nvl
= NULL
;
5092 nvlist_t
*set_nvl
= NULL
;
5095 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
5097 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
5101 case ZFS_DELEG_NAMED_SET_SETS
:
5102 case ZFS_DELEG_NAMED_SET
:
5103 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
5104 base_type
= ZFS_DELEG_NAMED_SET
;
5105 ld
[0] = ZFS_DELEG_NA
;
5107 case ZFS_DELEG_CREATE_SETS
:
5108 case ZFS_DELEG_CREATE
:
5109 set_type
= ZFS_DELEG_CREATE_SETS
;
5110 base_type
= ZFS_DELEG_CREATE
;
5111 ld
[0] = ZFS_DELEG_NA
;
5113 case ZFS_DELEG_USER_SETS
:
5114 case ZFS_DELEG_USER
:
5115 set_type
= ZFS_DELEG_USER_SETS
;
5116 base_type
= ZFS_DELEG_USER
;
5118 ld
[0] = ZFS_DELEG_LOCAL
;
5120 ld
[1] = ZFS_DELEG_DESCENDENT
;
5122 case ZFS_DELEG_GROUP_SETS
:
5123 case ZFS_DELEG_GROUP
:
5124 set_type
= ZFS_DELEG_GROUP_SETS
;
5125 base_type
= ZFS_DELEG_GROUP
;
5127 ld
[0] = ZFS_DELEG_LOCAL
;
5129 ld
[1] = ZFS_DELEG_DESCENDENT
;
5131 case ZFS_DELEG_EVERYONE_SETS
:
5132 case ZFS_DELEG_EVERYONE
:
5133 set_type
= ZFS_DELEG_EVERYONE_SETS
;
5134 base_type
= ZFS_DELEG_EVERYONE
;
5136 ld
[0] = ZFS_DELEG_LOCAL
;
5138 ld
[1] = ZFS_DELEG_DESCENDENT
;
5142 assert(set_type
!= '\0' && base_type
!= '\0');
5145 if (perms
!= NULL
) {
5147 char *end
= curr
+ strlen(perms
);
5149 while (curr
< end
) {
5150 char *delim
= strchr(curr
, ',');
5161 (void) nvlist_add_boolean(nvl
, curr
);
5167 for (i
= 0; i
< 2; i
++) {
5168 char locality
= ld
[i
];
5172 if (!nvlist_empty(base_nvl
)) {
5174 (void) snprintf(who_buf
,
5175 sizeof (who_buf
), "%c%c$%s",
5176 base_type
, locality
, who
);
5178 (void) snprintf(who_buf
,
5179 sizeof (who_buf
), "%c%c$",
5180 base_type
, locality
);
5182 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5187 if (!nvlist_empty(set_nvl
)) {
5189 (void) snprintf(who_buf
,
5190 sizeof (who_buf
), "%c%c$%s",
5191 set_type
, locality
, who
);
5193 (void) snprintf(who_buf
,
5194 sizeof (who_buf
), "%c%c$",
5195 set_type
, locality
);
5197 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5202 for (i
= 0; i
< 2; i
++) {
5203 char locality
= ld
[i
];
5208 (void) snprintf(who_buf
, sizeof (who_buf
),
5209 "%c%c$%s", base_type
, locality
, who
);
5211 (void) snprintf(who_buf
, sizeof (who_buf
),
5212 "%c%c$", base_type
, locality
);
5213 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5216 (void) snprintf(who_buf
, sizeof (who_buf
),
5217 "%c%c$%s", set_type
, locality
, who
);
5219 (void) snprintf(who_buf
, sizeof (who_buf
),
5220 "%c%c$", set_type
, locality
);
5221 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5227 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
5229 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
5233 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
5234 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
5235 } else if (opts
->create
) {
5236 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
5237 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5238 } else if (opts
->everyone
) {
5239 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
5240 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5242 char *curr
= opts
->who
;
5243 char *end
= curr
+ strlen(curr
);
5245 while (curr
< end
) {
5247 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
5249 char *delim
= strchr(curr
, ',');
5252 struct passwd
*p
= NULL
;
5253 struct group
*g
= NULL
;
5261 rid
= (uid_t
)strtol(curr
, &endch
, 0);
5263 who_type
= ZFS_DELEG_USER
;
5272 (void) snprintf(errbuf
, 256, gettext(
5273 "invalid user %s"), curr
);
5274 allow_usage(un
, B_TRUE
, errbuf
);
5276 } else if (opts
->group
) {
5277 who_type
= ZFS_DELEG_GROUP
;
5286 (void) snprintf(errbuf
, 256, gettext(
5287 "invalid group %s"), curr
);
5288 allow_usage(un
, B_TRUE
, errbuf
);
5291 if (*endch
!= '\0') {
5298 if (*endch
!= '\0') {
5306 who_type
= ZFS_DELEG_USER
;
5308 } else if (g
!= NULL
) {
5309 who_type
= ZFS_DELEG_GROUP
;
5312 (void) snprintf(errbuf
, 256, gettext(
5313 "invalid user/group %s"), curr
);
5314 allow_usage(un
, B_TRUE
, errbuf
);
5318 (void) sprintf(id
, "%u", rid
);
5321 store_allow_perm(who_type
, opts
->local
,
5322 opts
->descend
, who
, opts
->perms
, *nvlp
);
5331 print_set_creat_perms(uu_avl_t
*who_avl
)
5333 const char *sc_title
[] = {
5334 gettext("Permission sets:\n"),
5335 gettext("Create time permissions:\n"),
5338 const char **title_ptr
= sc_title
;
5339 who_perm_node_t
*who_node
= NULL
;
5340 int prev_weight
= -1;
5342 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
5343 who_node
= uu_avl_next(who_avl
, who_node
)) {
5344 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5345 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5346 const char *who_name
= who_node
->who_perm
.who_name
;
5347 int weight
= who_type2weight(who_type
);
5348 boolean_t first
= B_TRUE
;
5349 deleg_perm_node_t
*deleg_node
;
5351 if (prev_weight
!= weight
) {
5352 (void) printf("%s", *title_ptr
++);
5353 prev_weight
= weight
;
5356 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
5357 (void) printf("\t");
5359 (void) printf("\t%s ", who_name
);
5361 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
5362 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5365 deleg_node
->dpn_perm
.dp_name
);
5368 (void) printf(",%s",
5369 deleg_node
->dpn_perm
.dp_name
);
5372 (void) printf("\n");
5377 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
5380 who_perm_node_t
*who_node
= NULL
;
5381 boolean_t prt_title
= B_TRUE
;
5382 uu_avl_walk_t
*walk
;
5384 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
5387 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
5388 const char *who_name
= who_node
->who_perm
.who_name
;
5389 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
5390 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5391 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5393 deleg_perm_node_t
*deleg_node
;
5394 boolean_t prt_who
= B_TRUE
;
5396 for (deleg_node
= uu_avl_first(avl
);
5398 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5399 if (local
!= deleg_node
->dpn_perm
.dp_local
||
5400 descend
!= deleg_node
->dpn_perm
.dp_descend
)
5404 const char *who
= NULL
;
5406 prt_title
= B_FALSE
;
5407 (void) printf("%s", title
);
5411 case ZFS_DELEG_USER_SETS
:
5412 case ZFS_DELEG_USER
:
5413 who
= gettext("user");
5415 who_name
= nice_who_name
;
5417 case ZFS_DELEG_GROUP_SETS
:
5418 case ZFS_DELEG_GROUP
:
5419 who
= gettext("group");
5421 who_name
= nice_who_name
;
5423 case ZFS_DELEG_EVERYONE_SETS
:
5424 case ZFS_DELEG_EVERYONE
:
5425 who
= gettext("everyone");
5430 assert(who
!= NULL
);
5434 if (who_name
== NULL
)
5435 (void) printf("\t%s", who
);
5437 (void) printf("\t%s %s", who
, who_name
);
5440 (void) printf("%c%s", delim
,
5441 deleg_node
->dpn_perm
.dp_name
);
5446 (void) printf("\n");
5449 uu_avl_walk_end(walk
);
5453 print_fs_perms(fs_perm_set_t
*fspset
)
5455 fs_perm_node_t
*node
= NULL
;
5456 char buf
[MAXNAMELEN
+ 32];
5457 const char *dsname
= buf
;
5459 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
5460 node
= uu_list_next(fspset
->fsps_list
, node
)) {
5461 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
5462 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
5465 (void) snprintf(buf
, sizeof (buf
),
5466 gettext("---- Permissions on %s "),
5467 node
->fspn_fsperm
.fsp_name
);
5468 (void) printf("%s", dsname
);
5469 left
= 70 - strlen(buf
);
5472 (void) printf("\n");
5474 print_set_creat_perms(sc_avl
);
5475 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
5476 gettext("Local permissions:\n"));
5477 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
5478 gettext("Descendent permissions:\n"));
5479 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
5480 gettext("Local+Descendent permissions:\n"));
5484 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5486 struct deleg_perms
{
5492 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5494 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5495 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5497 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5500 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5504 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5507 nvlist_t
*perm_nvl
= NULL
;
5508 nvlist_t
*update_perm_nvl
= NULL
;
5511 struct allow_opts opts
= { 0 };
5513 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5516 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5519 opts
.local
= B_TRUE
;
5522 opts
.descend
= B_TRUE
;
5528 opts
.group
= B_TRUE
;
5531 opts
.everyone
= B_TRUE
;
5537 opts
.create
= B_TRUE
;
5540 opts
.recursive
= B_TRUE
;
5543 (void) fprintf(stderr
, gettext("missing argument for "
5544 "'%c' option\n"), optopt
);
5548 opts
.prt_usage
= B_TRUE
;
5551 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5560 /* check arguments */
5561 parse_allow_args(argc
, argv
, un
, &opts
);
5563 /* try to open the dataset */
5564 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5565 ZFS_TYPE_VOLUME
)) == NULL
) {
5566 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5571 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5574 fs_perm_set_init(&fs_perm_set
);
5575 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5576 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5581 print_fs_perms(&fs_perm_set
);
5583 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5584 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5587 if (un
&& opts
.recursive
) {
5588 struct deleg_perms data
= { un
, update_perm_nvl
};
5589 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5598 nvlist_free(perm_nvl
);
5599 nvlist_free(update_perm_nvl
);
5601 fs_perm_set_fini(&fs_perm_set
);
5609 zfs_do_allow(int argc
, char **argv
)
5611 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5615 zfs_do_unallow(int argc
, char **argv
)
5617 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5621 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5626 boolean_t recursive
= B_FALSE
;
5627 const char *opts
= holding
? "rt" : "r";
5631 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5637 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5646 /* check number of arguments */
5654 if (holding
&& tag
[0] == '.') {
5655 /* tags starting with '.' are reserved for libzfs */
5656 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5660 for (i
= 0; i
< argc
; ++i
) {
5662 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
5664 char *path
= argv
[i
];
5666 delim
= strchr(path
, '@');
5667 if (delim
== NULL
) {
5668 (void) fprintf(stderr
,
5669 gettext("'%s' is not a snapshot\n"), path
);
5673 (void) strncpy(parent
, path
, delim
- path
);
5674 parent
[delim
- path
] = '\0';
5676 zhp
= zfs_open(g_zfs
, parent
,
5677 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5683 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
5686 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5692 return (errors
!= 0);
5696 * zfs hold [-r] [-t] <tag> <snap> ...
5698 * -r Recursively hold
5700 * Apply a user-hold with the given tag to the list of snapshots.
5703 zfs_do_hold(int argc
, char **argv
)
5705 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5709 * zfs release [-r] <tag> <snap> ...
5711 * -r Recursively release
5713 * Release a user-hold with the given tag from the list of snapshots.
5716 zfs_do_release(int argc
, char **argv
)
5718 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5721 typedef struct holds_cbdata
{
5722 boolean_t cb_recursive
;
5723 const char *cb_snapname
;
5725 size_t cb_max_namelen
;
5726 size_t cb_max_taglen
;
5729 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5730 #define DATETIME_BUF_LEN (32)
5735 print_holds(boolean_t scripted
, int nwidth
, int tagwidth
, nvlist_t
*nvl
)
5738 nvpair_t
*nvp
= NULL
;
5739 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5743 for (i
= 0; i
< 3; i
++) {
5744 col
= gettext(hdr_cols
[i
]);
5746 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5749 (void) printf("%s\n", col
);
5753 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5754 char *zname
= nvpair_name(nvp
);
5756 nvpair_t
*nvp2
= NULL
;
5757 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5758 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5759 char tsbuf
[DATETIME_BUF_LEN
];
5760 char *tagname
= nvpair_name(nvp2
);
5764 char sep
= scripted
? '\t' : ' ';
5765 int sepnum
= scripted
? 1 : 2;
5767 (void) nvpair_value_uint64(nvp2
, &val
);
5769 (void) localtime_r(&time
, &t
);
5770 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5771 gettext(STRFTIME_FMT_STR
), &t
);
5773 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth
, zname
,
5774 sepnum
, sep
, tagwidth
, tagname
, sepnum
, sep
, tsbuf
);
5780 * Generic callback function to list a dataset or snapshot.
5783 holds_callback(zfs_handle_t
*zhp
, void *data
)
5785 holds_cbdata_t
*cbp
= data
;
5786 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5787 nvlist_t
*nvl
= NULL
;
5788 nvpair_t
*nvp
= NULL
;
5789 const char *zname
= zfs_get_name(zhp
);
5790 size_t znamelen
= strlen(zname
);
5792 if (cbp
->cb_recursive
) {
5793 const char *snapname
;
5794 char *delim
= strchr(zname
, '@');
5798 snapname
= delim
+ 1;
5799 if (strcmp(cbp
->cb_snapname
, snapname
))
5803 if (zfs_get_holds(zhp
, &nvl
) != 0)
5806 if (znamelen
> cbp
->cb_max_namelen
)
5807 cbp
->cb_max_namelen
= znamelen
;
5809 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5810 const char *tag
= nvpair_name(nvp
);
5811 size_t taglen
= strlen(tag
);
5812 if (taglen
> cbp
->cb_max_taglen
)
5813 cbp
->cb_max_taglen
= taglen
;
5816 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5820 * zfs holds [-r] <snap> ...
5822 * -r Recursively hold
5825 zfs_do_holds(int argc
, char **argv
)
5830 boolean_t scripted
= B_FALSE
;
5831 boolean_t recursive
= B_FALSE
;
5832 const char *opts
= "rH";
5835 int types
= ZFS_TYPE_SNAPSHOT
;
5836 holds_cbdata_t cb
= { 0 };
5843 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5852 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5859 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
5860 flags
|= ZFS_ITER_RECURSE
;
5866 /* check number of arguments */
5870 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5873 for (i
= 0; i
< argc
; ++i
) {
5874 char *snapshot
= argv
[i
];
5876 const char *snapname
;
5878 delim
= strchr(snapshot
, '@');
5879 if (delim
== NULL
) {
5880 (void) fprintf(stderr
,
5881 gettext("'%s' is not a snapshot\n"), snapshot
);
5885 snapname
= delim
+ 1;
5887 snapshot
[delim
- snapshot
] = '\0';
5889 cb
.cb_recursive
= recursive
;
5890 cb
.cb_snapname
= snapname
;
5894 * 1. collect holds data, set format options
5896 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
5897 holds_callback
, &cb
);
5903 * 2. print holds data
5905 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
5907 if (nvlist_empty(nvl
))
5908 (void) fprintf(stderr
, gettext("no datasets available\n"));
5912 return (0 != errors
);
5915 #define CHECK_SPINNER 30
5916 #define SPINNER_TIME 3 /* seconds */
5917 #define MOUNT_TIME 5 /* seconds */
5920 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
5922 static char *spin
[] = { "-", "\\", "|", "/" };
5923 static int spinval
= 0;
5924 static int spincheck
= 0;
5925 static time_t last_spin_time
= (time_t)0;
5926 get_all_cb_t
*cbp
= data
;
5927 zfs_type_t type
= zfs_get_type(zhp
);
5929 if (cbp
->cb_verbose
) {
5930 if (--spincheck
< 0) {
5931 time_t now
= time(NULL
);
5932 if (last_spin_time
+ SPINNER_TIME
< now
) {
5933 update_progress(spin
[spinval
++ % 4]);
5934 last_spin_time
= now
;
5936 spincheck
= CHECK_SPINNER
;
5941 * Iterate over any nested datasets.
5943 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
5949 * Skip any datasets whose type does not match.
5951 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
5955 libzfs_add_handle(cbp
, zhp
);
5956 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
5962 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
5964 get_all_cb_t cb
= { 0 };
5965 cb
.cb_verbose
= verbose
;
5966 cb
.cb_getone
= get_one_dataset
;
5969 set_progress_header(gettext("Reading ZFS config"));
5970 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
5972 *dslist
= cb
.cb_handles
;
5973 *count
= cb
.cb_used
;
5976 finish_progress(gettext("done."));
5980 * Generic callback for sharing or mounting filesystems. Because the code is so
5981 * similar, we have a common function with an extra parameter to determine which
5982 * mode we are using.
5984 #define OP_SHARE 0x1
5985 #define OP_MOUNT 0x2
5988 * Share or mount a dataset.
5991 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
5992 boolean_t
explicit, const char *options
)
5994 char mountpoint
[ZFS_MAXPROPLEN
];
5995 char shareopts
[ZFS_MAXPROPLEN
];
5996 char smbshareopts
[ZFS_MAXPROPLEN
];
5997 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
5999 uint64_t zoned
, canmount
;
6000 boolean_t shared_nfs
, shared_smb
;
6002 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
6005 * Check to make sure we can mount/share this dataset. If we
6006 * are in the global zone and the filesystem is exported to a
6007 * local zone, or if we are in a local zone and the
6008 * filesystem is not exported, then it is an error.
6010 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
6012 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
6016 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6017 "dataset is exported to a local zone\n"), cmdname
,
6021 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
6025 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6026 "permission denied\n"), cmdname
,
6032 * Ignore any filesystems which don't apply to us. This
6033 * includes those with a legacy mountpoint, or those with
6034 * legacy share options.
6036 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
6037 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
6038 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
6039 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
6040 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
6041 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
6043 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
6044 strcmp(smbshareopts
, "off") == 0) {
6048 (void) fprintf(stderr
, gettext("cannot share '%s': "
6049 "legacy share\n"), zfs_get_name(zhp
));
6050 (void) fprintf(stderr
, gettext("use share(1M) to "
6051 "share this filesystem, or set "
6052 "sharenfs property on\n"));
6057 * We cannot share or mount legacy filesystems. If the
6058 * shareopts is non-legacy but the mountpoint is legacy, we
6059 * treat it as a legacy share.
6061 if (strcmp(mountpoint
, "legacy") == 0) {
6065 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6066 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
6067 (void) fprintf(stderr
, gettext("use %s(1M) to "
6068 "%s this filesystem\n"), cmdname
, cmdname
);
6072 if (strcmp(mountpoint
, "none") == 0) {
6076 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
6077 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
6082 * canmount explicit outcome
6083 * on no pass through
6084 * on yes pass through
6086 * off yes display error, return 1
6087 * noauto no return 0
6088 * noauto yes pass through
6090 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
6091 if (canmount
== ZFS_CANMOUNT_OFF
) {
6095 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6096 "'canmount' property is set to 'off'\n"), cmdname
,
6099 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
6104 * If this filesystem is inconsistent and has a receive resume
6105 * token, we can not mount it.
6107 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
6108 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
6109 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
6113 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6114 "Contains partially-completed state from "
6115 "\"zfs receive -r\", which can be resumed with "
6116 "\"zfs send -t\"\n"),
6117 cmdname
, zfs_get_name(zhp
));
6122 * At this point, we have verified that the mountpoint and/or
6123 * shareopts are appropriate for auto management. If the
6124 * filesystem is already mounted or shared, return (failing
6125 * for explicit requests); otherwise mount or share the
6131 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
6132 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
6134 if ((shared_nfs
&& shared_smb
) ||
6135 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
6136 strcmp(smbshareopts
, "off") == 0) ||
6137 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
6138 strcmp(shareopts
, "off") == 0)) {
6142 (void) fprintf(stderr
, gettext("cannot share "
6143 "'%s': filesystem already shared\n"),
6148 if (!zfs_is_mounted(zhp
, NULL
) &&
6149 zfs_mount(zhp
, NULL
, flags
) != 0)
6152 if (protocol
== NULL
) {
6153 if (zfs_shareall(zhp
) != 0)
6155 } else if (strcmp(protocol
, "nfs") == 0) {
6156 if (zfs_share_nfs(zhp
))
6158 } else if (strcmp(protocol
, "smb") == 0) {
6159 if (zfs_share_smb(zhp
))
6162 (void) fprintf(stderr
, gettext("cannot share "
6163 "'%s': invalid share type '%s' "
6165 zfs_get_name(zhp
), protocol
);
6172 if (options
== NULL
)
6173 mnt
.mnt_mntopts
= "";
6175 mnt
.mnt_mntopts
= (char *)options
;
6177 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
6178 zfs_is_mounted(zhp
, NULL
)) {
6182 (void) fprintf(stderr
, gettext("cannot mount "
6183 "'%s': filesystem already mounted\n"),
6188 if (zfs_mount(zhp
, options
, flags
) != 0)
6197 * Reports progress in the form "(current/total)". Not thread-safe.
6200 report_mount_progress(int current
, int total
)
6202 static time_t last_progress_time
= 0;
6203 time_t now
= time(NULL
);
6206 /* report 1..n instead of 0..n-1 */
6209 /* display header if we're here for the first time */
6211 set_progress_header(gettext("Mounting ZFS filesystems"));
6212 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
6213 /* too soon to report again */
6217 last_progress_time
= now
;
6219 (void) sprintf(info
, "(%d/%d)", current
, total
);
6221 if (current
== total
)
6222 finish_progress(info
);
6224 update_progress(info
);
6228 append_options(char *mntopts
, char *newopts
)
6230 int len
= strlen(mntopts
);
6232 /* original length plus new string to append plus 1 for the comma */
6233 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
6234 (void) fprintf(stderr
, gettext("the opts argument for "
6235 "'%s' option is too long (more than %d chars)\n"),
6236 "-o", MNT_LINE_MAX
);
6241 mntopts
[len
++] = ',';
6243 (void) strcpy(&mntopts
[len
], newopts
);
6247 share_mount(int op
, int argc
, char **argv
)
6250 boolean_t verbose
= B_FALSE
;
6252 char *options
= NULL
;
6256 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":alvo:O" : "al"))
6269 if (*optarg
== '\0') {
6270 (void) fprintf(stderr
, gettext("empty mount "
6271 "options (-o) specified\n"));
6275 if (options
== NULL
)
6276 options
= safe_malloc(MNT_LINE_MAX
+ 1);
6278 /* option validation is done later */
6279 append_options(options
, optarg
);
6282 flags
|= MS_OVERLAY
;
6285 (void) fprintf(stderr
, gettext("missing argument for "
6286 "'%c' option\n"), optopt
);
6290 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6299 /* check number of arguments */
6301 zfs_handle_t
**dslist
= NULL
;
6302 size_t i
, count
= 0;
6303 char *protocol
= NULL
;
6305 if (op
== OP_SHARE
&& argc
> 0) {
6306 if (strcmp(argv
[0], "nfs") != 0 &&
6307 strcmp(argv
[0], "smb") != 0) {
6308 (void) fprintf(stderr
, gettext("share type "
6309 "must be 'nfs' or 'smb'\n"));
6318 (void) fprintf(stderr
, gettext("too many arguments\n"));
6322 start_progress_timer();
6323 get_all_datasets(&dslist
, &count
, verbose
);
6326 if (options
!= NULL
)
6331 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
6333 for (i
= 0; i
< count
; i
++) {
6335 report_mount_progress(i
, count
);
6337 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
6338 B_FALSE
, options
) != 0)
6340 zfs_close(dslist
[i
]);
6344 } else if (argc
== 0) {
6345 struct mnttab entry
;
6347 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
6348 (void) fprintf(stderr
, gettext("missing filesystem "
6349 "argument (specify -a for all)\n"));
6354 * When mount is given no arguments, go through
6355 * /proc/self/mounts and display any active ZFS mounts.
6356 * We hide any snapshots, since they are controlled
6360 /* Reopen MNTTAB to prevent reading stale data from open file */
6361 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
) {
6362 if (options
!= NULL
)
6367 while (getmntent(mnttab_file
, &entry
) == 0) {
6368 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
6369 strchr(entry
.mnt_special
, '@') != NULL
)
6372 (void) printf("%-30s %s\n", entry
.mnt_special
,
6380 (void) fprintf(stderr
,
6381 gettext("too many arguments\n"));
6385 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6386 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6389 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
6395 if (options
!= NULL
)
6402 * zfs mount -a [nfs]
6403 * zfs mount filesystem
6405 * Mount all filesystems, or mount the given filesystem.
6408 zfs_do_mount(int argc
, char **argv
)
6410 return (share_mount(OP_MOUNT
, argc
, argv
));
6414 * zfs share -a [nfs | smb]
6415 * zfs share filesystem
6417 * Share all filesystems, or share the given filesystem.
6420 zfs_do_share(int argc
, char **argv
)
6422 return (share_mount(OP_SHARE
, argc
, argv
));
6425 typedef struct unshare_unmount_node
{
6426 zfs_handle_t
*un_zhp
;
6428 uu_avl_node_t un_avlnode
;
6429 } unshare_unmount_node_t
;
6433 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
6435 const unshare_unmount_node_t
*l
= larg
;
6436 const unshare_unmount_node_t
*r
= rarg
;
6438 return (strcmp(l
->un_mountp
, r
->un_mountp
));
6442 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6443 * absolute path, find the entry /proc/self/mounts, verify that its a
6444 * ZFS filesystems, and unmount it appropriately.
6447 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
6451 struct stat64 statbuf
;
6452 struct extmnttab entry
;
6453 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
6457 * Search for the path in /proc/self/mounts. Rather than looking for the
6458 * specific path, which can be fooled by non-standard paths (i.e. ".."
6459 * or "//"), we stat() the path and search for the corresponding
6460 * (major,minor) device pair.
6462 if (stat64(path
, &statbuf
) != 0) {
6463 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6464 cmdname
, path
, strerror(errno
));
6467 path_inode
= statbuf
.st_ino
;
6470 * Search for the given (major,minor) pair in the mount table.
6473 /* Reopen MNTTAB to prevent reading stale data from open file */
6474 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6477 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
6478 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
6479 entry
.mnt_minor
== minor(statbuf
.st_dev
))
6483 if (op
== OP_SHARE
) {
6484 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
6485 "currently mounted\n"), cmdname
, path
);
6488 (void) fprintf(stderr
, gettext("warning: %s not in"
6489 "/proc/self/mounts\n"), path
);
6490 if ((ret
= umount2(path
, flags
)) != 0)
6491 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
6496 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
6497 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
6498 "filesystem\n"), cmdname
, path
);
6502 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6503 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6507 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
6508 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6509 cmdname
, path
, strerror(errno
));
6511 } else if (statbuf
.st_ino
!= path_inode
) {
6512 (void) fprintf(stderr
, gettext("cannot "
6513 "%s '%s': not a mountpoint\n"), cmdname
, path
);
6517 if (op
== OP_SHARE
) {
6518 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6519 char smbshare_prop
[ZFS_MAXPROPLEN
];
6521 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
6522 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6523 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
6524 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6526 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6527 strcmp(smbshare_prop
, "off") == 0) {
6528 (void) fprintf(stderr
, gettext("cannot unshare "
6529 "'%s': legacy share\n"), path
);
6530 (void) fprintf(stderr
, gettext("use exportfs(8) "
6531 "or smbcontrol(1) to unshare this filesystem\n"));
6532 } else if (!zfs_is_shared(zhp
)) {
6533 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6534 "not currently shared\n"), path
);
6536 ret
= zfs_unshareall_bypath(zhp
, path
);
6539 char mtpt_prop
[ZFS_MAXPROPLEN
];
6541 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6542 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6545 ret
= zfs_unmount(zhp
, NULL
, flags
);
6546 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6547 (void) fprintf(stderr
, gettext("cannot unmount "
6548 "'%s': legacy mountpoint\n"),
6550 (void) fprintf(stderr
, gettext("use umount(8) "
6551 "to unmount this filesystem\n"));
6553 ret
= zfs_unmountall(zhp
, flags
);
6564 * Generic callback for unsharing or unmounting a filesystem.
6567 unshare_unmount(int op
, int argc
, char **argv
)
6574 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6575 char sharesmb
[ZFS_MAXPROPLEN
];
6578 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? ":a" : "af")) != -1) {
6587 (void) fprintf(stderr
, gettext("missing argument for "
6588 "'%c' option\n"), optopt
);
6592 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6603 * We could make use of zfs_for_each() to walk all datasets in
6604 * the system, but this would be very inefficient, especially
6605 * since we would have to linearly search /proc/self/mounts for
6606 * each one. Instead, do one pass through /proc/self/mounts
6607 * looking for zfs entries and call zfs_unmount() for each one.
6609 * Things get a little tricky if the administrator has created
6610 * mountpoints beneath other ZFS filesystems. In this case, we
6611 * have to unmount the deepest filesystems first. To accomplish
6612 * this, we place all the mountpoints in an AVL tree sorted by
6613 * the special type (dataset name), and walk the result in
6614 * reverse to make sure to get any snapshots first.
6616 struct mnttab entry
;
6617 uu_avl_pool_t
*pool
;
6618 uu_avl_t
*tree
= NULL
;
6619 unshare_unmount_node_t
*node
;
6621 uu_avl_walk_t
*walk
;
6622 char *protocol
= NULL
;
6624 if (op
== OP_SHARE
&& argc
> 0) {
6625 if (strcmp(argv
[0], "nfs") != 0 &&
6626 strcmp(argv
[0], "smb") != 0) {
6627 (void) fprintf(stderr
, gettext("share type "
6628 "must be 'nfs' or 'smb'\n"));
6637 (void) fprintf(stderr
, gettext("too many arguments\n"));
6641 if (((pool
= uu_avl_pool_create("unmount_pool",
6642 sizeof (unshare_unmount_node_t
),
6643 offsetof(unshare_unmount_node_t
, un_avlnode
),
6644 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6645 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6648 /* Reopen MNTTAB to prevent reading stale data from open file */
6649 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
6652 while (getmntent(mnttab_file
, &entry
) == 0) {
6654 /* ignore non-ZFS entries */
6655 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6658 /* ignore snapshots */
6659 if (strchr(entry
.mnt_special
, '@') != NULL
)
6662 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6663 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6669 * Ignore datasets that are excluded/restricted by
6672 if (zpool_skip_pool(zfs_get_pool_name(zhp
))) {
6679 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6681 sizeof (nfs_mnt_prop
),
6682 NULL
, NULL
, 0, B_FALSE
) == 0);
6683 if (strcmp(nfs_mnt_prop
, "off") != 0)
6685 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6687 sizeof (nfs_mnt_prop
),
6688 NULL
, NULL
, 0, B_FALSE
) == 0);
6689 if (strcmp(nfs_mnt_prop
, "off") == 0)
6693 /* Ignore legacy mounts */
6694 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6696 sizeof (nfs_mnt_prop
),
6697 NULL
, NULL
, 0, B_FALSE
) == 0);
6698 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6700 /* Ignore canmount=noauto mounts */
6701 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6702 ZFS_CANMOUNT_NOAUTO
)
6708 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6710 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6712 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6714 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6715 uu_avl_insert(tree
, node
, idx
);
6717 zfs_close(node
->un_zhp
);
6718 free(node
->un_mountp
);
6724 * Walk the AVL tree in reverse, unmounting each filesystem and
6725 * removing it from the AVL tree in the process.
6727 if ((walk
= uu_avl_walk_start(tree
,
6728 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6731 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6732 uu_avl_remove(tree
, node
);
6736 if (zfs_unshareall_bytype(node
->un_zhp
,
6737 node
->un_mountp
, protocol
) != 0)
6742 if (zfs_unmount(node
->un_zhp
,
6743 node
->un_mountp
, flags
) != 0)
6748 zfs_close(node
->un_zhp
);
6749 free(node
->un_mountp
);
6753 uu_avl_walk_end(walk
);
6754 uu_avl_destroy(tree
);
6755 uu_avl_pool_destroy(pool
);
6760 (void) fprintf(stderr
,
6761 gettext("missing filesystem argument\n"));
6763 (void) fprintf(stderr
,
6764 gettext("too many arguments\n"));
6769 * We have an argument, but it may be a full path or a ZFS
6770 * filesystem. Pass full paths off to unmount_path() (shared by
6771 * manual_unmount), otherwise open the filesystem and pass to
6774 if (argv
[0][0] == '/')
6775 return (unshare_unmount_path(op
, argv
[0],
6778 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6779 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6782 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
6783 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
6784 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
6785 NULL
, 0, B_FALSE
) == 0);
6789 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6791 sizeof (nfs_mnt_prop
),
6792 NULL
, NULL
, 0, B_FALSE
) == 0);
6793 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6794 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
6797 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6798 strcmp(sharesmb
, "off") == 0) {
6799 (void) fprintf(stderr
, gettext("cannot "
6800 "unshare '%s': legacy share\n"),
6802 (void) fprintf(stderr
, gettext("use "
6803 "unshare(1M) to unshare this "
6806 } else if (!zfs_is_shared(zhp
)) {
6807 (void) fprintf(stderr
, gettext("cannot "
6808 "unshare '%s': not currently "
6809 "shared\n"), zfs_get_name(zhp
));
6811 } else if (zfs_unshareall(zhp
) != 0) {
6817 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
6818 (void) fprintf(stderr
, gettext("cannot "
6819 "unmount '%s': legacy "
6820 "mountpoint\n"), zfs_get_name(zhp
));
6821 (void) fprintf(stderr
, gettext("use "
6822 "umount(1M) to unmount this "
6825 } else if (!zfs_is_mounted(zhp
, NULL
)) {
6826 (void) fprintf(stderr
, gettext("cannot "
6827 "unmount '%s': not currently "
6831 } else if (zfs_unmountall(zhp
, flags
) != 0) {
6845 * zfs unmount filesystem
6847 * Unmount all filesystems, or a specific ZFS filesystem.
6850 zfs_do_unmount(int argc
, char **argv
)
6852 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
6857 * zfs unshare filesystem
6859 * Unshare all filesystems, or a specific ZFS filesystem.
6862 zfs_do_unshare(int argc
, char **argv
)
6864 return (unshare_unmount(OP_SHARE
, argc
, argv
));
6868 find_command_idx(char *command
, int *idx
)
6872 for (i
= 0; i
< NCOMMAND
; i
++) {
6873 if (command_table
[i
].name
== NULL
)
6876 if (strcmp(command
, command_table
[i
].name
) == 0) {
6885 zfs_do_diff(int argc
, char **argv
)
6889 char *tosnap
= NULL
;
6890 char *fromsnap
= NULL
;
6895 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
6898 flags
|= ZFS_DIFF_CLASSIFY
;
6901 flags
|= ZFS_DIFF_PARSEABLE
;
6904 flags
|= ZFS_DIFF_TIMESTAMP
;
6907 (void) fprintf(stderr
,
6908 gettext("invalid option '%c'\n"), optopt
);
6917 (void) fprintf(stderr
,
6918 gettext("must provide at least one snapshot name\n"));
6923 (void) fprintf(stderr
, gettext("too many arguments\n"));
6928 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
6931 if (*fromsnap
!= '@')
6932 copy
= strdup(fromsnap
);
6934 copy
= strdup(tosnap
);
6938 if ((atp
= strchr(copy
, '@')) != NULL
)
6941 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6948 * Ignore SIGPIPE so that the library can give us
6949 * information on any failure
6951 (void) sigignore(SIGPIPE
);
6953 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
6961 * zfs bookmark <fs@snap> <fs#bmark>
6963 * Creates a bookmark with the given name from the given snapshot.
6966 zfs_do_bookmark(int argc
, char **argv
)
6968 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
6969 char bookname
[ZFS_MAX_DATASET_NAME_LEN
];
6976 while ((c
= getopt(argc
, argv
, "")) != -1) {
6979 (void) fprintf(stderr
,
6980 gettext("invalid option '%c'\n"), optopt
);
6988 /* check number of arguments */
6990 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
6994 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
6998 if (strchr(argv
[1], '#') == NULL
) {
6999 (void) fprintf(stderr
,
7000 gettext("invalid bookmark name '%s': "
7001 "must contain a '#'\n"), argv
[1]);
7005 if (argv
[0][0] == '@') {
7007 * Snapshot name begins with @.
7008 * Default to same fs as bookmark.
7010 (void) strlcpy(snapname
, argv
[1], sizeof (snapname
));
7011 *strchr(snapname
, '#') = '\0';
7012 (void) strlcat(snapname
, argv
[0], sizeof (snapname
));
7014 (void) strlcpy(snapname
, argv
[0], sizeof (snapname
));
7016 if (argv
[1][0] == '#') {
7018 * Bookmark name begins with #.
7019 * Default to same fs as snapshot.
7021 (void) strlcpy(bookname
, argv
[0], sizeof (bookname
));
7022 *strchr(bookname
, '@') = '\0';
7023 (void) strlcat(bookname
, argv
[1], sizeof (bookname
));
7025 (void) strlcpy(bookname
, argv
[1], sizeof (bookname
));
7028 zhp
= zfs_open(g_zfs
, snapname
, ZFS_TYPE_SNAPSHOT
);
7034 nvl
= fnvlist_alloc();
7035 fnvlist_add_string(nvl
, bookname
, snapname
);
7036 ret
= lzc_bookmark(nvl
, NULL
);
7040 const char *err_msg
;
7043 (void) snprintf(errbuf
, sizeof (errbuf
),
7044 dgettext(TEXT_DOMAIN
,
7045 "cannot create bookmark '%s'"), bookname
);
7049 err_msg
= "bookmark is in a different pool";
7052 err_msg
= "bookmark exists";
7055 err_msg
= "invalid argument";
7058 err_msg
= "bookmark feature not enabled";
7061 err_msg
= "out of space";
7064 err_msg
= "dataset does not exist";
7067 err_msg
= "unknown error";
7070 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
7071 dgettext(TEXT_DOMAIN
, err_msg
));
7081 typedef struct loadkey_cbdata
{
7082 boolean_t cb_loadkey
;
7083 boolean_t cb_recursive
;
7085 char *cb_keylocation
;
7086 uint64_t cb_numfailed
;
7087 uint64_t cb_numattempted
;
7091 load_key_callback(zfs_handle_t
*zhp
, void *data
)
7094 boolean_t is_encroot
;
7095 loadkey_cbdata_t
*cb
= data
;
7096 uint64_t keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
7099 * If we are working recursively, we want to skip loading / unloading
7100 * keys for non-encryption roots and datasets whose keys are already
7101 * in the desired end-state.
7103 if (cb
->cb_recursive
) {
7104 ret
= zfs_crypto_get_encryption_root(zhp
, &is_encroot
, NULL
);
7110 if ((cb
->cb_loadkey
&& keystatus
== ZFS_KEYSTATUS_AVAILABLE
) ||
7111 (!cb
->cb_loadkey
&& keystatus
== ZFS_KEYSTATUS_UNAVAILABLE
))
7115 cb
->cb_numattempted
++;
7118 ret
= zfs_crypto_load_key(zhp
, cb
->cb_noop
, cb
->cb_keylocation
);
7120 ret
= zfs_crypto_unload_key(zhp
);
7131 load_unload_keys(int argc
, char **argv
, boolean_t loadkey
)
7133 int c
, ret
= 0, flags
= 0;
7134 boolean_t do_all
= B_FALSE
;
7135 loadkey_cbdata_t cb
= { 0 };
7137 cb
.cb_loadkey
= loadkey
;
7139 while ((c
= getopt(argc
, argv
, "anrL:")) != -1) {
7140 /* noop and alternate keylocations only apply to zfs load-key */
7144 cb
.cb_noop
= B_TRUE
;
7147 cb
.cb_keylocation
= optarg
;
7157 cb
.cb_recursive
= B_TRUE
;
7160 flags
|= ZFS_ITER_RECURSE
;
7161 cb
.cb_recursive
= B_TRUE
;
7164 (void) fprintf(stderr
,
7165 gettext("invalid option '%c'\n"), optopt
);
7173 if (!do_all
&& argc
== 0) {
7174 (void) fprintf(stderr
,
7175 gettext("Missing dataset argument or -a option\n"));
7179 if (do_all
&& argc
!= 0) {
7180 (void) fprintf(stderr
,
7181 gettext("Cannot specify dataset with -a option\n"));
7185 if (cb
.cb_recursive
&& cb
.cb_keylocation
!= NULL
&&
7186 strcmp(cb
.cb_keylocation
, "prompt") != 0) {
7187 (void) fprintf(stderr
, gettext("alternate keylocation may only "
7188 "be 'prompt' with -r or -a\n"));
7192 ret
= zfs_for_each(argc
, argv
, flags
,
7193 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
, NULL
, NULL
, 0,
7194 load_key_callback
, &cb
);
7196 if (cb
.cb_noop
|| (cb
.cb_recursive
&& cb
.cb_numattempted
!= 0)) {
7197 (void) printf(gettext("%llu / %llu key(s) successfully %s\n"),
7198 (u_longlong_t
)(cb
.cb_numattempted
- cb
.cb_numfailed
),
7199 (u_longlong_t
)cb
.cb_numattempted
,
7200 loadkey
? (cb
.cb_noop
? "verified" : "loaded") :
7204 if (cb
.cb_numfailed
!= 0)
7211 zfs_do_load_key(int argc
, char **argv
)
7213 return (load_unload_keys(argc
, argv
, B_TRUE
));
7218 zfs_do_unload_key(int argc
, char **argv
)
7220 return (load_unload_keys(argc
, argv
, B_FALSE
));
7224 zfs_do_change_key(int argc
, char **argv
)
7228 boolean_t loadkey
= B_FALSE
, inheritkey
= B_FALSE
;
7229 zfs_handle_t
*zhp
= NULL
;
7230 nvlist_t
*props
= fnvlist_alloc();
7232 while ((c
= getopt(argc
, argv
, "lio:")) != -1) {
7238 inheritkey
= B_TRUE
;
7241 if (!parseprop(props
, optarg
)) {
7247 (void) fprintf(stderr
,
7248 gettext("invalid option '%c'\n"), optopt
);
7253 if (inheritkey
&& !nvlist_empty(props
)) {
7254 (void) fprintf(stderr
,
7255 gettext("Properties not allowed for inheriting\n"));
7263 (void) fprintf(stderr
, gettext("Missing dataset argument\n"));
7268 (void) fprintf(stderr
, gettext("Too many arguments\n"));
7272 zhp
= zfs_open(g_zfs
, argv
[argc
- 1],
7273 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
7278 keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
7279 if (keystatus
!= ZFS_KEYSTATUS_AVAILABLE
) {
7280 ret
= zfs_crypto_load_key(zhp
, B_FALSE
, NULL
);
7285 /* refresh the properties so the new keystatus is visable */
7286 zfs_refresh_properties(zhp
);
7289 ret
= zfs_crypto_rewrap(zhp
, props
, inheritkey
);
7306 main(int argc
, char **argv
)
7312 (void) setlocale(LC_ALL
, "");
7313 (void) textdomain(TEXT_DOMAIN
);
7318 * Make sure the user has specified some command.
7321 (void) fprintf(stderr
, gettext("missing command\n"));
7328 * The 'umount' command is an alias for 'unmount'
7330 if (strcmp(cmdname
, "umount") == 0)
7331 cmdname
= "unmount";
7334 * The 'recv' command is an alias for 'receive'
7336 if (strcmp(cmdname
, "recv") == 0)
7337 cmdname
= "receive";
7340 * The 'snap' command is an alias for 'snapshot'
7342 if (strcmp(cmdname
, "snap") == 0)
7343 cmdname
= "snapshot";
7348 if ((strcmp(cmdname
, "-?") == 0) ||
7349 (strcmp(cmdname
, "--help") == 0))
7352 if ((g_zfs
= libzfs_init()) == NULL
) {
7353 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
7357 mnttab_file
= g_zfs
->libzfs_mnttab
;
7359 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
7361 libzfs_print_on_error(g_zfs
, B_TRUE
);
7364 * Run the appropriate command.
7366 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
7367 if (find_command_idx(cmdname
, &i
) == 0) {
7368 current_command
= &command_table
[i
];
7369 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
7370 } else if (strchr(cmdname
, '=') != NULL
) {
7371 verify(find_command_idx("set", &i
) == 0);
7372 current_command
= &command_table
[i
];
7373 ret
= command_table
[i
].func(argc
, argv
);
7375 (void) fprintf(stderr
, gettext("unrecognized "
7376 "command '%s'\n"), cmdname
);
7381 if (ret
== 0 && log_history
)
7382 (void) zpool_log_history(g_zfs
, history_str
);
7387 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7388 * for the purposes of running ::findleaks.
7390 if (getenv("ZFS_ABORT") != NULL
) {
7391 (void) printf("dumping core by request\n");