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, 2016 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28 * Copyright (c) 2013 Steven Hartland. All rights reserved.
29 * Copyright (c) 2014 Integros [integros.com]
30 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
31 * Copyright 2016 Nexenta Systems, Inc.
41 #include <libnvpair.h>
53 #include <sys/debug.h>
55 #include <sys/mkdev.h>
56 #include <sys/mntent.h>
57 #include <sys/mnttab.h>
58 #include <sys/mount.h>
60 #include <sys/fs/zfs.h>
61 #include <sys/types.h>
65 #include <libzfs_core.h>
67 #include <zfs_deleg.h>
70 #include <directory.h>
76 #include "zfs_comutil.h"
78 libzfs_handle_t
*g_zfs
;
80 static FILE *mnttab_file
;
81 static char history_str
[HIS_MAX_RECORD_LEN
];
82 static boolean_t log_history
= B_TRUE
;
84 static int zfs_do_clone(int argc
, char **argv
);
85 static int zfs_do_create(int argc
, char **argv
);
86 static int zfs_do_destroy(int argc
, char **argv
);
87 static int zfs_do_get(int argc
, char **argv
);
88 static int zfs_do_inherit(int argc
, char **argv
);
89 static int zfs_do_list(int argc
, char **argv
);
90 static int zfs_do_mount(int argc
, char **argv
);
91 static int zfs_do_rename(int argc
, char **argv
);
92 static int zfs_do_rollback(int argc
, char **argv
);
93 static int zfs_do_set(int argc
, char **argv
);
94 static int zfs_do_upgrade(int argc
, char **argv
);
95 static int zfs_do_snapshot(int argc
, char **argv
);
96 static int zfs_do_unmount(int argc
, char **argv
);
97 static int zfs_do_share(int argc
, char **argv
);
98 static int zfs_do_unshare(int argc
, char **argv
);
99 static int zfs_do_send(int argc
, char **argv
);
100 static int zfs_do_receive(int argc
, char **argv
);
101 static int zfs_do_promote(int argc
, char **argv
);
102 static int zfs_do_userspace(int argc
, char **argv
);
103 static int zfs_do_allow(int argc
, char **argv
);
104 static int zfs_do_unallow(int argc
, char **argv
);
105 static int zfs_do_hold(int argc
, char **argv
);
106 static int zfs_do_holds(int argc
, char **argv
);
107 static int zfs_do_release(int argc
, char **argv
);
108 static int zfs_do_diff(int argc
, char **argv
);
109 static int zfs_do_bookmark(int argc
, char **argv
);
110 static int zfs_do_remap(int argc
, char **argv
);
111 static int zfs_do_channel_program(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 */
160 HELP_CHANNEL_PROGRAM
,
163 typedef struct zfs_command
{
165 int (*func
)(int argc
, char **argv
);
170 * Master command table. Each ZFS command has a name, associated function, and
171 * usage message. The usage messages need to be internationalized, so we have
172 * to have a function to return the usage message based on a command index.
174 * These commands are organized according to how they are displayed in the usage
175 * message. An empty command (one with a NULL name) indicates an empty line in
176 * the generic usage message.
178 static zfs_command_t command_table
[] = {
179 { "create", zfs_do_create
, HELP_CREATE
},
180 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
182 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
183 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
184 { "clone", zfs_do_clone
, HELP_CLONE
},
185 { "promote", zfs_do_promote
, HELP_PROMOTE
},
186 { "rename", zfs_do_rename
, HELP_RENAME
},
187 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
188 { "program", zfs_do_channel_program
, HELP_CHANNEL_PROGRAM
},
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 { "remap", zfs_do_remap
, HELP_REMAP
},
218 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
220 zfs_command_t
*current_command
;
223 get_usage(zfs_help_t idx
)
227 return (gettext("\tclone [-p] [-o property=value] ... "
228 "<snapshot> <filesystem|volume>\n"));
230 return (gettext("\tcreate [-p] [-o property=value] ... "
232 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
233 "-V <size> <volume>\n"));
235 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
236 "\tdestroy [-dnpRrv] "
237 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
238 "\tdestroy <filesystem|volume>#<bookmark>\n"));
240 return (gettext("\tget [-rHp] [-d max] "
241 "[-o \"all\" | field[,...]]\n"
242 "\t [-t type[,...]] [-s source[,...]]\n"
243 "\t <\"all\" | property[,...]> "
244 "[filesystem|volume|snapshot|bookmark] ...\n"));
246 return (gettext("\tinherit [-rS] <property> "
247 "<filesystem|volume|snapshot> ...\n"));
249 return (gettext("\tupgrade [-v]\n"
250 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
252 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
253 "[-s property]...\n\t [-S property]... [-t type[,...]] "
254 "[filesystem|volume|snapshot] ...\n"));
256 return (gettext("\tmount\n"
257 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
259 return (gettext("\tpromote <clone-filesystem>\n"));
261 return (gettext("\treceive [-vnsFu] <filesystem|volume|"
263 "\treceive [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
265 "\treceive -A <filesystem|volume>\n"));
267 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
268 "<filesystem|volume|snapshot>\n"
269 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
270 "\trename -r <snapshot> <snapshot>\n"));
272 return (gettext("\trollback [-rRf] <snapshot>\n"));
274 return (gettext("\tsend [-DnPpRvLec] [-[iI] snapshot] "
276 "\tsend [-Le] [-i snapshot|bookmark] "
277 "<filesystem|volume|snapshot>\n"
278 "\tsend [-nvPe] -t <receive_resume_token>\n"));
280 return (gettext("\tset <property=value> ... "
281 "<filesystem|volume|snapshot> ...\n"));
283 return (gettext("\tshare <-a | filesystem>\n"));
285 return (gettext("\tsnapshot [-r] [-o property=value] ... "
286 "<filesystem|volume>@<snap> ...\n"));
288 return (gettext("\tunmount [-f] "
289 "<-a | filesystem|mountpoint>\n"));
291 return (gettext("\tunshare "
292 "<-a | filesystem|mountpoint>\n"));
294 return (gettext("\tallow <filesystem|volume>\n"
296 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
297 "\t <filesystem|volume>\n"
298 "\tallow [-ld] -e <perm|@setname>[,...] "
299 "<filesystem|volume>\n"
300 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
301 "\tallow -s @setname <perm|@setname>[,...] "
302 "<filesystem|volume>\n"));
304 return (gettext("\tunallow [-rldug] "
305 "<\"everyone\"|user|group>[,...]\n"
306 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
307 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
308 "<filesystem|volume>\n"
309 "\tunallow [-r] -c [<perm|@setname>[,...]] "
310 "<filesystem|volume>\n"
311 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
312 "<filesystem|volume>\n"));
314 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
316 "\t [-S field] ... [-t type[,...]] "
317 "<filesystem|snapshot>\n"));
318 case HELP_GROUPSPACE
:
319 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
321 "\t [-S field] ... [-t type[,...]] "
322 "<filesystem|snapshot>\n"));
324 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
326 return (gettext("\tholds [-r] <snapshot> ...\n"));
328 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
330 return (gettext("\tdiff [-FHt] <snapshot> "
331 "[snapshot|filesystem]\n"));
333 return (gettext("\tremap <filesystem | volume>\n"));
335 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
336 case HELP_CHANNEL_PROGRAM
:
337 return (gettext("\tprogram [-n] [-t <instruction limit>] "
338 "[-m <memory limit (b)>] <pool> <program file> "
349 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
354 * Utility function to guarantee malloc() success.
358 safe_malloc(size_t size
)
362 if ((data
= calloc(1, size
)) == NULL
)
369 safe_realloc(void *data
, size_t size
)
372 if ((newp
= realloc(data
, size
)) == NULL
) {
381 safe_strdup(char *str
)
383 char *dupstr
= strdup(str
);
392 * Callback routine that will print out information for each of
396 usage_prop_cb(int prop
, void *cb
)
400 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
402 if (zfs_prop_readonly(prop
))
403 (void) fprintf(fp
, " NO ");
405 (void) fprintf(fp
, "YES ");
407 if (zfs_prop_inheritable(prop
))
408 (void) fprintf(fp
, " YES ");
410 (void) fprintf(fp
, " NO ");
412 if (zfs_prop_values(prop
) == NULL
)
413 (void) fprintf(fp
, "-\n");
415 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
421 * Display usage message. If we're inside a command, display only the usage for
422 * that command. Otherwise, iterate over the entire command table and display
423 * a complete usage message.
426 usage(boolean_t requested
)
429 boolean_t show_properties
= B_FALSE
;
430 FILE *fp
= requested
? stdout
: stderr
;
432 if (current_command
== NULL
) {
434 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
436 gettext("where 'command' is one of the following:\n\n"));
438 for (i
= 0; i
< NCOMMAND
; i
++) {
439 if (command_table
[i
].name
== NULL
)
440 (void) fprintf(fp
, "\n");
442 (void) fprintf(fp
, "%s",
443 get_usage(command_table
[i
].usage
));
446 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
447 "pool/[dataset/]*dataset[@name]\n"));
449 (void) fprintf(fp
, gettext("usage:\n"));
450 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
453 if (current_command
!= NULL
&&
454 (strcmp(current_command
->name
, "set") == 0 ||
455 strcmp(current_command
->name
, "get") == 0 ||
456 strcmp(current_command
->name
, "inherit") == 0 ||
457 strcmp(current_command
->name
, "list") == 0))
458 show_properties
= B_TRUE
;
460 if (show_properties
) {
462 gettext("\nThe following properties are supported:\n"));
464 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
465 "PROPERTY", "EDIT", "INHERIT", "VALUES");
467 /* Iterate over all properties */
468 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
471 (void) fprintf(fp
, "\t%-15s ", "userused@...");
472 (void) fprintf(fp
, " NO NO <size>\n");
473 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
474 (void) fprintf(fp
, " NO NO <size>\n");
475 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
476 (void) fprintf(fp
, "YES NO <size> | none\n");
477 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
478 (void) fprintf(fp
, "YES NO <size> | none\n");
479 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
480 (void) fprintf(fp
, " NO NO <size>\n");
482 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
483 "with standard units such as K, M, G, etc.\n"));
484 (void) fprintf(fp
, gettext("\nUser-defined properties can "
485 "be specified by using a name containing a colon (:).\n"));
486 (void) fprintf(fp
, gettext("\nThe {user|group}{used|quota}@ "
487 "properties must be appended with\n"
488 "a user or group specifier of one of these forms:\n"
489 " POSIX name (eg: \"matt\")\n"
490 " POSIX id (eg: \"126829\")\n"
491 " SMB name@domain (eg: \"matt@sun\")\n"
492 " SMB SID (eg: \"S-1-234-567-89\")\n"));
495 gettext("\nFor the property list, run: %s\n"),
498 gettext("\nFor the delegated permission list, run: %s\n"),
499 "zfs allow|unallow");
503 * See comments at end of main().
505 if (getenv("ZFS_ABORT") != NULL
) {
506 (void) printf("dumping core by request\n");
510 exit(requested
? 0 : 2);
514 * Take a property=value argument string and add it to the given nvlist.
515 * Modifies the argument inplace.
518 parseprop(nvlist_t
*props
, char *propname
)
520 char *propval
, *strval
;
522 if ((propval
= strchr(propname
, '=')) == NULL
) {
523 (void) fprintf(stderr
, gettext("missing "
524 "'=' for property=value argument\n"));
529 if (nvlist_lookup_string(props
, propname
, &strval
) == 0) {
530 (void) fprintf(stderr
, gettext("property '%s' "
531 "specified multiple times\n"), propname
);
534 if (nvlist_add_string(props
, propname
, propval
) != 0)
540 parse_depth(char *opt
, int *flags
)
545 depth
= (int)strtol(opt
, &tmp
, 0);
547 (void) fprintf(stderr
,
548 gettext("%s is not an integer\n"), optarg
);
552 (void) fprintf(stderr
,
553 gettext("Depth can not be negative.\n"));
556 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
560 #define PROGRESS_DELAY 2 /* seconds */
562 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";
563 static time_t pt_begin
;
564 static char *pt_header
= NULL
;
565 static boolean_t pt_shown
;
568 start_progress_timer(void)
570 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
575 set_progress_header(char *header
)
577 assert(pt_header
== NULL
);
578 pt_header
= safe_strdup(header
);
580 (void) printf("%s: ", header
);
581 (void) fflush(stdout
);
586 update_progress(char *update
)
588 if (!pt_shown
&& time(NULL
) > pt_begin
) {
589 int len
= strlen(update
);
591 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
593 (void) fflush(stdout
);
595 } else if (pt_shown
) {
596 int len
= strlen(update
);
598 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
599 (void) fflush(stdout
);
604 finish_progress(char *done
)
607 (void) printf("%s\n", done
);
608 (void) fflush(stdout
);
615 * Check if the dataset is mountable and should be automatically mounted.
618 should_auto_mount(zfs_handle_t
*zhp
)
620 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, zfs_get_type(zhp
)))
622 return (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
);
626 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
628 * Given an existing dataset, create a writable copy whose initial contents
629 * are the same as the source. The newly created dataset maintains a
630 * dependency on the original; the original cannot be destroyed so long as
633 * The '-p' flag creates all the non-existing ancestors of the target first.
636 zfs_do_clone(int argc
, char **argv
)
638 zfs_handle_t
*zhp
= NULL
;
639 boolean_t parents
= B_FALSE
;
644 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
648 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
651 if (parseprop(props
, optarg
) != 0)
658 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
667 /* check number of arguments */
669 (void) fprintf(stderr
, gettext("missing source dataset "
674 (void) fprintf(stderr
, gettext("missing target dataset "
679 (void) fprintf(stderr
, gettext("too many arguments\n"));
683 /* open the source dataset */
684 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
687 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
690 * Now create the ancestors of the target dataset. If the
691 * target already exists and '-p' option was used we should not
694 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
697 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
702 ret
= zfs_clone(zhp
, argv
[1], props
);
704 /* create the mountpoint if necessary */
708 clone
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
711 * If the user doesn't want the dataset
712 * automatically mounted, then skip the mount/share
715 if (should_auto_mount(clone
)) {
716 if ((ret
= zfs_mount(clone
, NULL
, 0)) != 0) {
717 (void) fprintf(stderr
, gettext("clone "
718 "successfully created, "
719 "but not mounted\n"));
720 } else if ((ret
= zfs_share(clone
)) != 0) {
721 (void) fprintf(stderr
, gettext("clone "
722 "successfully created, "
723 "but not shared\n"));
744 * zfs create [-p] [-o prop=value] ... fs
745 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
747 * Create a new dataset. This command can be used to create filesystems
748 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
749 * For volumes, the user must specify a size to be used.
751 * The '-s' flag applies only to volumes, and indicates that we should not try
752 * to set the reservation for this volume. By default we set a reservation
753 * equal to the size for any volume. For pools with SPA_VERSION >=
754 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
756 * The '-p' flag creates all the non-existing ancestors of the target first.
759 zfs_do_create(int argc
, char **argv
)
761 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
762 zfs_handle_t
*zhp
= NULL
;
763 uint64_t volsize
= 0;
765 boolean_t noreserve
= B_FALSE
;
766 boolean_t bflag
= B_FALSE
;
767 boolean_t parents
= B_FALSE
;
772 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
776 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
779 type
= ZFS_TYPE_VOLUME
;
780 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
781 (void) fprintf(stderr
, gettext("bad volume "
782 "size '%s': %s\n"), optarg
,
783 libzfs_error_description(g_zfs
));
787 if (nvlist_add_uint64(props
,
788 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
797 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
798 (void) fprintf(stderr
, gettext("bad volume "
799 "block size '%s': %s\n"), optarg
,
800 libzfs_error_description(g_zfs
));
804 if (nvlist_add_uint64(props
,
805 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
810 if (parseprop(props
, optarg
) != 0)
817 (void) fprintf(stderr
, gettext("missing size "
821 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
827 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
828 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
829 "used when creating a volume\n"));
836 /* check number of arguments */
838 (void) fprintf(stderr
, gettext("missing %s argument\n"),
839 zfs_type_to_name(type
));
843 (void) fprintf(stderr
, gettext("too many arguments\n"));
847 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
848 zpool_handle_t
*zpool_handle
;
849 nvlist_t
*real_props
= NULL
;
850 uint64_t spa_version
;
852 zfs_prop_t resv_prop
;
856 if ((p
= strchr(argv
[0], '/')) != NULL
)
858 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
861 if (zpool_handle
== NULL
)
863 spa_version
= zpool_get_prop_int(zpool_handle
,
864 ZPOOL_PROP_VERSION
, NULL
);
865 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
866 resv_prop
= ZFS_PROP_REFRESERVATION
;
868 resv_prop
= ZFS_PROP_RESERVATION
;
870 (void) snprintf(msg
, sizeof (msg
),
871 gettext("cannot create '%s'"), argv
[0]);
872 if (props
&& (real_props
= zfs_valid_proplist(g_zfs
, type
,
873 props
, 0, NULL
, zpool_handle
, msg
)) == NULL
) {
874 zpool_close(zpool_handle
);
877 zpool_close(zpool_handle
);
879 volsize
= zvol_volsize_to_reservation(volsize
, real_props
);
880 nvlist_free(real_props
);
882 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
884 if (nvlist_add_uint64(props
,
885 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
892 if (parents
&& zfs_name_valid(argv
[0], type
)) {
894 * Now create the ancestors of target dataset. If the target
895 * already exists and '-p' option was used we should not
898 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
902 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
907 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
910 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
916 * Mount and/or share the new filesystem as appropriate. We provide a
917 * verbose error message to let the user know that their filesystem was
918 * in fact created, even if we failed to mount or share it.
919 * If the user doesn't want the dataset automatically mounted,
920 * then skip the mount/share step altogether.
922 if (should_auto_mount(zhp
)) {
923 if (zfs_mount(zhp
, NULL
, 0) != 0) {
924 (void) fprintf(stderr
, gettext("filesystem "
925 "successfully created, but not mounted\n"));
927 } else if (zfs_share(zhp
) != 0) {
928 (void) fprintf(stderr
, gettext("filesystem "
929 "successfully created, but not shared\n"));
946 * zfs destroy [-rRf] <fs, vol>
947 * zfs destroy [-rRd] <snap>
949 * -r Recursively destroy all children
950 * -R Recursively destroy all dependents, including clones
951 * -f Force unmounting of any dependents
952 * -d If we can't destroy now, mark for deferred destruction
954 * Destroys the given dataset. By default, it will unmount any filesystems,
955 * and refuse to destroy a dataset that has any dependents. A dependent can
956 * either be a child, or a clone of a child.
958 typedef struct destroy_cbdata
{
961 boolean_t cb_recurse
;
963 boolean_t cb_doclones
;
964 zfs_handle_t
*cb_target
;
965 boolean_t cb_defer_destroy
;
966 boolean_t cb_verbose
;
967 boolean_t cb_parsable
;
970 nvlist_t
*cb_batchedsnaps
;
972 /* first snap in contiguous run */
974 /* previous snap in contiguous run */
982 * Check for any dependents based on the '-r' or '-R' flags.
985 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
987 destroy_cbdata_t
*cbp
= data
;
988 const char *tname
= zfs_get_name(cbp
->cb_target
);
989 const char *name
= zfs_get_name(zhp
);
991 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
992 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
994 * This is a direct descendant, not a clone somewhere else in
1000 if (cbp
->cb_first
) {
1001 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1002 "%s has children\n"),
1003 zfs_get_name(cbp
->cb_target
),
1004 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1005 (void) fprintf(stderr
, gettext("use '-r' to destroy "
1006 "the following datasets:\n"));
1007 cbp
->cb_first
= B_FALSE
;
1008 cbp
->cb_error
= B_TRUE
;
1011 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1014 * This is a clone. We only want to report this if the '-r'
1015 * wasn't specified, or the target is a snapshot.
1017 if (!cbp
->cb_recurse
&&
1018 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
1021 if (cbp
->cb_first
) {
1022 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1023 "%s has dependent clones\n"),
1024 zfs_get_name(cbp
->cb_target
),
1025 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1026 (void) fprintf(stderr
, gettext("use '-R' to destroy "
1027 "the following datasets:\n"));
1028 cbp
->cb_first
= B_FALSE
;
1029 cbp
->cb_error
= B_TRUE
;
1030 cbp
->cb_dryrun
= B_TRUE
;
1033 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1042 destroy_callback(zfs_handle_t
*zhp
, void *data
)
1044 destroy_cbdata_t
*cb
= data
;
1045 const char *name
= zfs_get_name(zhp
);
1047 if (cb
->cb_verbose
) {
1048 if (cb
->cb_parsable
) {
1049 (void) printf("destroy\t%s\n", name
);
1050 } else if (cb
->cb_dryrun
) {
1051 (void) printf(gettext("would destroy %s\n"),
1054 (void) printf(gettext("will destroy %s\n"),
1060 * Ignore pools (which we've already flagged as an error before getting
1063 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1064 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1068 if (cb
->cb_dryrun
) {
1074 * We batch up all contiguous snapshots (even of different
1075 * filesystems) and destroy them with one ioctl. We can't
1076 * simply do all snap deletions and then all fs deletions,
1077 * because we must delete a clone before its origin.
1079 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1080 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1082 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1083 cb
->cb_batchedsnaps
, B_FALSE
);
1084 fnvlist_free(cb
->cb_batchedsnaps
);
1085 cb
->cb_batchedsnaps
= fnvlist_alloc();
1088 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1089 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1100 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1102 destroy_cbdata_t
*cb
= arg
;
1103 const char *name
= zfs_get_name(zhp
);
1106 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1107 if (cb
->cb_firstsnap
== NULL
)
1108 cb
->cb_firstsnap
= strdup(name
);
1109 if (cb
->cb_prevsnap
!= NULL
)
1110 free(cb
->cb_prevsnap
);
1111 /* this snap continues the current range */
1112 cb
->cb_prevsnap
= strdup(name
);
1113 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1115 if (cb
->cb_verbose
) {
1116 if (cb
->cb_parsable
) {
1117 (void) printf("destroy\t%s\n", name
);
1118 } else if (cb
->cb_dryrun
) {
1119 (void) printf(gettext("would destroy %s\n"),
1122 (void) printf(gettext("will destroy %s\n"),
1126 } else if (cb
->cb_firstsnap
!= NULL
) {
1127 /* end of this range */
1129 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1130 cb
->cb_prevsnap
, &used
);
1131 cb
->cb_snapused
+= used
;
1132 free(cb
->cb_firstsnap
);
1133 cb
->cb_firstsnap
= NULL
;
1134 free(cb
->cb_prevsnap
);
1135 cb
->cb_prevsnap
= NULL
;
1142 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1145 assert(cb
->cb_firstsnap
== NULL
);
1146 assert(cb
->cb_prevsnap
== NULL
);
1147 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1148 if (cb
->cb_firstsnap
!= NULL
) {
1151 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1152 cb
->cb_prevsnap
, &used
);
1154 cb
->cb_snapused
+= used
;
1155 free(cb
->cb_firstsnap
);
1156 cb
->cb_firstsnap
= NULL
;
1157 free(cb
->cb_prevsnap
);
1158 cb
->cb_prevsnap
= NULL
;
1164 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1166 destroy_cbdata_t
*cb
= arg
;
1169 /* Check for clones. */
1170 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1171 cb
->cb_target
= zhp
;
1172 cb
->cb_first
= B_TRUE
;
1173 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1174 destroy_check_dependent
, cb
);
1178 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1186 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1188 destroy_cbdata_t
*cb
= arg
;
1191 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1197 if (cb
->cb_verbose
) {
1198 err
= destroy_print_snapshots(zhp
, cb
);
1204 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1212 destroy_clones(destroy_cbdata_t
*cb
)
1215 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1217 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1218 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1221 boolean_t defer
= cb
->cb_defer_destroy
;
1225 * We can't defer destroy non-snapshots, so set it to
1226 * false while destroying the clones.
1228 cb
->cb_defer_destroy
= B_FALSE
;
1229 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1230 destroy_callback
, cb
);
1231 cb
->cb_defer_destroy
= defer
;
1241 zfs_do_destroy(int argc
, char **argv
)
1243 destroy_cbdata_t cb
= { 0 };
1247 zfs_handle_t
*zhp
= NULL
;
1249 zfs_type_t type
= ZFS_TYPE_DATASET
;
1252 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1255 cb
.cb_verbose
= B_TRUE
;
1258 cb
.cb_verbose
= B_TRUE
;
1259 cb
.cb_parsable
= B_TRUE
;
1262 cb
.cb_dryrun
= B_TRUE
;
1265 cb
.cb_defer_destroy
= B_TRUE
;
1266 type
= ZFS_TYPE_SNAPSHOT
;
1269 cb
.cb_force
= B_TRUE
;
1272 cb
.cb_recurse
= B_TRUE
;
1275 cb
.cb_recurse
= B_TRUE
;
1276 cb
.cb_doclones
= B_TRUE
;
1280 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1289 /* check number of arguments */
1291 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1295 (void) fprintf(stderr
, gettext("too many arguments\n"));
1299 at
= strchr(argv
[0], '@');
1300 pound
= strchr(argv
[0], '#');
1303 /* Build the list of snaps to destroy in cb_nvl. */
1304 cb
.cb_nvl
= fnvlist_alloc();
1307 zhp
= zfs_open(g_zfs
, argv
[0],
1308 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1312 cb
.cb_snapspec
= at
+ 1;
1313 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1319 if (nvlist_empty(cb
.cb_nvl
)) {
1320 (void) fprintf(stderr
, gettext("could not find any "
1321 "snapshots to destroy; check snapshot names.\n"));
1326 if (cb
.cb_verbose
) {
1328 zfs_nicenum(cb
.cb_snapused
, buf
, sizeof (buf
));
1329 if (cb
.cb_parsable
) {
1330 (void) printf("reclaim\t%llu\n",
1332 } else if (cb
.cb_dryrun
) {
1333 (void) printf(gettext("would reclaim %s\n"),
1336 (void) printf(gettext("will reclaim %s\n"),
1341 if (!cb
.cb_dryrun
) {
1342 if (cb
.cb_doclones
) {
1343 cb
.cb_batchedsnaps
= fnvlist_alloc();
1344 err
= destroy_clones(&cb
);
1346 err
= zfs_destroy_snaps_nvl(g_zfs
,
1347 cb
.cb_batchedsnaps
, B_FALSE
);
1355 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1356 cb
.cb_defer_destroy
);
1362 } else if (pound
!= NULL
) {
1367 (void) fprintf(stderr
,
1368 "dryrun is not supported with bookmark\n");
1372 if (cb
.cb_defer_destroy
) {
1373 (void) fprintf(stderr
,
1374 "defer destroy is not supported with bookmark\n");
1378 if (cb
.cb_recurse
) {
1379 (void) fprintf(stderr
,
1380 "recursive is not supported with bookmark\n");
1384 if (!zfs_bookmark_exists(argv
[0])) {
1385 (void) fprintf(stderr
, gettext("bookmark '%s' "
1386 "does not exist.\n"), argv
[0]);
1390 nvl
= fnvlist_alloc();
1391 fnvlist_add_boolean(nvl
, argv
[0]);
1393 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1395 (void) zfs_standard_error(g_zfs
, err
,
1396 "cannot destroy bookmark");
1399 nvlist_free(cb
.cb_nvl
);
1403 /* Open the given dataset */
1404 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1410 * Perform an explicit check for pools before going any further.
1412 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1413 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1414 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1415 "operation does not apply to pools\n"),
1417 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1418 "%s' to destroy all datasets in the pool\n"),
1420 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1421 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1427 * Check for any dependents and/or clones.
1429 cb
.cb_first
= B_TRUE
;
1430 if (!cb
.cb_doclones
&&
1431 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1442 cb
.cb_batchedsnaps
= fnvlist_alloc();
1443 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1450 * Do the real thing. The callback will close the
1451 * handle regardless of whether it succeeds or not.
1453 err
= destroy_callback(zhp
, &cb
);
1456 err
= zfs_destroy_snaps_nvl(g_zfs
,
1457 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1464 fnvlist_free(cb
.cb_batchedsnaps
);
1465 fnvlist_free(cb
.cb_nvl
);
1472 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1475 zfs_get_column_t col
;
1477 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1478 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1479 if (col
== GET_COL_RECVD
)
1485 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1486 * < all | property[,property]... > < fs | snap | vol > ...
1488 * -r recurse over any child datasets
1489 * -H scripted mode. Headers are stripped, and fields are separated
1490 * by tabs instead of spaces.
1491 * -o Set of fields to display. One of "name,property,value,
1492 * received,source". Default is "name,property,value,source".
1493 * "all" is an alias for all five.
1494 * -s Set of sources to allow. One of
1495 * "local,default,inherited,received,temporary,none". Default is
1497 * -p Display values in parsable (literal) format.
1499 * Prints properties for the given datasets. The user can control which
1500 * columns to display as well as which property types to allow.
1504 * Invoked to display the properties for a single dataset.
1507 get_callback(zfs_handle_t
*zhp
, void *data
)
1509 char buf
[ZFS_MAXPROPLEN
];
1510 char rbuf
[ZFS_MAXPROPLEN
];
1511 zprop_source_t sourcetype
;
1512 char source
[ZFS_MAX_DATASET_NAME_LEN
];
1513 zprop_get_cbdata_t
*cbp
= data
;
1514 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1515 zprop_list_t
*pl
= cbp
->cb_proplist
;
1519 boolean_t received
= is_recvd_column(cbp
);
1521 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1522 char *recvdval
= NULL
;
1524 * Skip the special fake placeholder. This will also skip over
1525 * the name property when 'all' is specified.
1527 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1528 pl
== cbp
->cb_proplist
)
1531 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1532 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1533 sizeof (buf
), &sourcetype
, source
,
1535 cbp
->cb_literal
) != 0) {
1538 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1539 ZFS_TYPE_DATASET
)) {
1540 (void) fprintf(stderr
,
1541 gettext("No such property '%s'\n"),
1542 zfs_prop_to_name(pl
->pl_prop
));
1545 sourcetype
= ZPROP_SRC_NONE
;
1546 (void) strlcpy(buf
, "-", sizeof (buf
));
1549 if (received
&& (zfs_prop_get_recvd(zhp
,
1550 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1551 cbp
->cb_literal
) == 0))
1554 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1555 zfs_prop_to_name(pl
->pl_prop
),
1556 buf
, sourcetype
, source
, recvdval
);
1557 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1558 sourcetype
= ZPROP_SRC_LOCAL
;
1560 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1561 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1562 sourcetype
= ZPROP_SRC_NONE
;
1563 (void) strlcpy(buf
, "-", sizeof (buf
));
1566 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1567 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1568 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1569 sourcetype
= ZPROP_SRC_LOCAL
;
1571 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1572 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1573 sourcetype
= ZPROP_SRC_NONE
;
1574 (void) strlcpy(buf
, "-", sizeof (buf
));
1577 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1578 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1580 if (nvlist_lookup_nvlist(user_props
,
1581 pl
->pl_user_prop
, &propval
) != 0) {
1584 sourcetype
= ZPROP_SRC_NONE
;
1587 verify(nvlist_lookup_string(propval
,
1588 ZPROP_VALUE
, &strval
) == 0);
1589 verify(nvlist_lookup_string(propval
,
1590 ZPROP_SOURCE
, &sourceval
) == 0);
1592 if (strcmp(sourceval
,
1593 zfs_get_name(zhp
)) == 0) {
1594 sourcetype
= ZPROP_SRC_LOCAL
;
1595 } else if (strcmp(sourceval
,
1596 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1597 sourcetype
= ZPROP_SRC_RECEIVED
;
1599 sourcetype
= ZPROP_SRC_INHERITED
;
1600 (void) strlcpy(source
,
1601 sourceval
, sizeof (source
));
1605 if (received
&& (zfs_prop_get_recvd(zhp
,
1606 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1607 cbp
->cb_literal
) == 0))
1610 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1611 pl
->pl_user_prop
, strval
, sourcetype
,
1620 zfs_do_get(int argc
, char **argv
)
1622 zprop_get_cbdata_t cb
= { 0 };
1623 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1624 int types
= ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
;
1625 char *value
, *fields
;
1628 zprop_list_t fake_name
= { 0 };
1631 * Set up default columns and sources.
1633 cb
.cb_sources
= ZPROP_SRC_ALL
;
1634 cb
.cb_columns
[0] = GET_COL_NAME
;
1635 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1636 cb
.cb_columns
[2] = GET_COL_VALUE
;
1637 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1638 cb
.cb_type
= ZFS_TYPE_DATASET
;
1641 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1644 cb
.cb_literal
= B_TRUE
;
1647 limit
= parse_depth(optarg
, &flags
);
1650 flags
|= ZFS_ITER_RECURSE
;
1653 cb
.cb_scripted
= B_TRUE
;
1656 (void) fprintf(stderr
, gettext("missing argument for "
1657 "'%c' option\n"), optopt
);
1662 * Process the set of columns to display. We zero out
1663 * the structure to give us a blank slate.
1665 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1667 while (*optarg
!= '\0') {
1668 static char *col_subopts
[] =
1669 { "name", "property", "value", "received",
1670 "source", "all", NULL
};
1672 if (i
== ZFS_GET_NCOLS
) {
1673 (void) fprintf(stderr
, gettext("too "
1674 "many fields given to -o "
1679 switch (getsubopt(&optarg
, col_subopts
,
1682 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1685 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1688 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1691 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1692 flags
|= ZFS_ITER_RECVD_PROPS
;
1695 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1699 (void) fprintf(stderr
,
1700 gettext("\"all\" conflicts "
1701 "with specific fields "
1702 "given to -o option\n"));
1705 cb
.cb_columns
[0] = GET_COL_NAME
;
1706 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1707 cb
.cb_columns
[2] = GET_COL_VALUE
;
1708 cb
.cb_columns
[3] = GET_COL_RECVD
;
1709 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1710 flags
|= ZFS_ITER_RECVD_PROPS
;
1714 (void) fprintf(stderr
,
1715 gettext("invalid column name "
1724 while (*optarg
!= '\0') {
1725 static char *source_subopts
[] = {
1726 "local", "default", "inherited",
1727 "received", "temporary", "none",
1730 switch (getsubopt(&optarg
, source_subopts
,
1733 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1736 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1739 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1742 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1745 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1748 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1751 (void) fprintf(stderr
,
1752 gettext("invalid source "
1761 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1762 while (*optarg
!= '\0') {
1763 static char *type_subopts
[] = { "filesystem",
1764 "volume", "snapshot", "bookmark",
1767 switch (getsubopt(&optarg
, type_subopts
,
1770 types
|= ZFS_TYPE_FILESYSTEM
;
1773 types
|= ZFS_TYPE_VOLUME
;
1776 types
|= ZFS_TYPE_SNAPSHOT
;
1779 types
|= ZFS_TYPE_BOOKMARK
;
1782 types
= ZFS_TYPE_DATASET
|
1787 (void) fprintf(stderr
,
1788 gettext("invalid type '%s'\n"),
1796 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1806 (void) fprintf(stderr
, gettext("missing property "
1813 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1821 * As part of zfs_expand_proplist(), we keep track of the maximum column
1822 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1823 * need to know the maximum name length. However, the user likely did
1824 * not specify 'name' as one of the properties to fetch, so we need to
1825 * make sure we always include at least this property for
1826 * print_get_headers() to work properly.
1828 if (cb
.cb_proplist
!= NULL
) {
1829 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1830 fake_name
.pl_width
= strlen(gettext("NAME"));
1831 fake_name
.pl_next
= cb
.cb_proplist
;
1832 cb
.cb_proplist
= &fake_name
;
1835 cb
.cb_first
= B_TRUE
;
1837 /* run for each object */
1838 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1839 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1841 if (cb
.cb_proplist
== &fake_name
)
1842 zprop_free_list(fake_name
.pl_next
);
1844 zprop_free_list(cb
.cb_proplist
);
1850 * inherit [-rS] <property> <fs|vol> ...
1852 * -r Recurse over all children
1853 * -S Revert to received value, if any
1855 * For each dataset specified on the command line, inherit the given property
1856 * from its parent. Inheriting a property at the pool level will cause it to
1857 * use the default value. The '-r' flag will recurse over all children, and is
1858 * useful for setting a property on a hierarchy-wide basis, regardless of any
1859 * local modifications for each dataset.
1862 typedef struct inherit_cbdata
{
1863 const char *cb_propname
;
1864 boolean_t cb_received
;
1868 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1870 inherit_cbdata_t
*cb
= data
;
1871 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1874 * If we're doing it recursively, then ignore properties that
1875 * are not valid for this type of dataset.
1877 if (prop
!= ZPROP_INVAL
&&
1878 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
)))
1881 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1885 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1887 inherit_cbdata_t
*cb
= data
;
1889 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1893 zfs_do_inherit(int argc
, char **argv
)
1897 inherit_cbdata_t cb
= { 0 };
1901 boolean_t received
= B_FALSE
;
1904 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1907 flags
|= ZFS_ITER_RECURSE
;
1914 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1923 /* check number of arguments */
1925 (void) fprintf(stderr
, gettext("missing property argument\n"));
1929 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1937 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1938 if (zfs_prop_readonly(prop
)) {
1939 (void) fprintf(stderr
, gettext(
1940 "%s property is read-only\n"),
1944 if (!zfs_prop_inheritable(prop
) && !received
) {
1945 (void) fprintf(stderr
, gettext("'%s' property cannot "
1946 "be inherited\n"), propname
);
1947 if (prop
== ZFS_PROP_QUOTA
||
1948 prop
== ZFS_PROP_RESERVATION
||
1949 prop
== ZFS_PROP_REFQUOTA
||
1950 prop
== ZFS_PROP_REFRESERVATION
) {
1951 (void) fprintf(stderr
, gettext("use 'zfs set "
1952 "%s=none' to clear\n"), propname
);
1953 (void) fprintf(stderr
, gettext("use 'zfs "
1954 "inherit -S %s' to revert to received "
1955 "value\n"), propname
);
1959 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1960 prop
== ZFS_PROP_VERSION
)) {
1961 (void) fprintf(stderr
, gettext("'%s' property cannot "
1962 "be reverted to a received value\n"), propname
);
1965 } else if (!zfs_prop_user(propname
)) {
1966 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1971 cb
.cb_propname
= propname
;
1972 cb
.cb_received
= received
;
1974 if (flags
& ZFS_ITER_RECURSE
) {
1975 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1976 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1978 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1979 NULL
, NULL
, 0, inherit_cb
, &cb
);
1985 typedef struct upgrade_cbdata
{
1986 uint64_t cb_numupgraded
;
1987 uint64_t cb_numsamegraded
;
1988 uint64_t cb_numfailed
;
1989 uint64_t cb_version
;
1991 boolean_t cb_foundone
;
1992 char cb_lastfs
[ZFS_MAX_DATASET_NAME_LEN
];
1996 same_pool(zfs_handle_t
*zhp
, const char *name
)
1998 int len1
= strcspn(name
, "/@");
1999 const char *zhname
= zfs_get_name(zhp
);
2000 int len2
= strcspn(zhname
, "/@");
2004 return (strncmp(name
, zhname
, len1
) == 0);
2008 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
2010 upgrade_cbdata_t
*cb
= data
;
2011 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2013 /* list if it's old/new */
2014 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
2015 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
2018 str
= gettext("The following filesystems are "
2019 "formatted using a newer software version and\n"
2020 "cannot be accessed on the current system.\n\n");
2022 str
= gettext("The following filesystems are "
2023 "out of date, and can be upgraded. After being\n"
2024 "upgraded, these filesystems (and any 'zfs send' "
2025 "streams generated from\n"
2026 "subsequent snapshots) will no longer be "
2027 "accessible by older software versions.\n\n");
2030 if (!cb
->cb_foundone
) {
2032 (void) printf(gettext("VER FILESYSTEM\n"));
2033 (void) printf(gettext("--- ------------\n"));
2034 cb
->cb_foundone
= B_TRUE
;
2037 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
2044 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2046 upgrade_cbdata_t
*cb
= data
;
2047 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2048 int needed_spa_version
;
2051 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2054 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2056 if (needed_spa_version
< 0)
2059 if (spa_version
< needed_spa_version
) {
2061 (void) printf(gettext("%s: can not be "
2062 "upgraded; the pool version needs to first "
2063 "be upgraded\nto version %d\n\n"),
2064 zfs_get_name(zhp
), needed_spa_version
);
2070 if (version
< cb
->cb_version
) {
2072 (void) snprintf(verstr
, sizeof (verstr
),
2073 "%llu", cb
->cb_version
);
2074 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2076 * If they did "zfs upgrade -a", then we could
2077 * be doing ioctls to different pools. We need
2078 * to log this history once to each pool, and bypass
2079 * the normal history logging that happens in main().
2081 (void) zpool_log_history(g_zfs
, history_str
);
2082 log_history
= B_FALSE
;
2084 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2085 cb
->cb_numupgraded
++;
2088 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2089 } else if (version
> cb
->cb_version
) {
2090 /* can't downgrade */
2091 (void) printf(gettext("%s: can not be downgraded; "
2092 "it is already at version %u\n"),
2093 zfs_get_name(zhp
), version
);
2096 cb
->cb_numsamegraded
++;
2104 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2107 zfs_do_upgrade(int argc
, char **argv
)
2109 boolean_t all
= B_FALSE
;
2110 boolean_t showversions
= B_FALSE
;
2112 upgrade_cbdata_t cb
= { 0 };
2114 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2117 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2120 flags
|= ZFS_ITER_RECURSE
;
2123 showversions
= B_TRUE
;
2126 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2127 optarg
, &cb
.cb_version
) != 0) {
2128 (void) fprintf(stderr
,
2129 gettext("invalid version %s\n"), optarg
);
2138 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2147 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2149 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2150 cb
.cb_version
|| argc
))
2152 if ((all
|| argc
) && (showversions
))
2158 /* Show info on available versions. */
2159 (void) printf(gettext("The following filesystem versions are "
2161 (void) printf(gettext("VER DESCRIPTION\n"));
2162 (void) printf("--- -----------------------------------------"
2163 "---------------\n");
2164 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2165 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2166 (void) printf(gettext(" 3 Case insensitive and filesystem "
2167 "user identifier (FUID)\n"));
2168 (void) printf(gettext(" 4 userquota, groupquota "
2170 (void) printf(gettext(" 5 System attributes\n"));
2171 (void) printf(gettext("\nFor more information on a particular "
2172 "version, including supported releases,\n"));
2173 (void) printf("see the ZFS Administration Guide.\n\n");
2175 } else if (argc
|| all
) {
2176 /* Upgrade filesystems */
2177 if (cb
.cb_version
== 0)
2178 cb
.cb_version
= ZPL_VERSION
;
2179 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2180 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2181 (void) printf(gettext("%llu filesystems upgraded\n"),
2183 if (cb
.cb_numsamegraded
) {
2184 (void) printf(gettext("%llu filesystems already at "
2186 cb
.cb_numsamegraded
);
2188 if (cb
.cb_numfailed
!= 0)
2191 /* List old-version filesystems */
2193 (void) printf(gettext("This system is currently running "
2194 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2196 flags
|= ZFS_ITER_RECURSE
;
2197 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2198 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2200 found
= cb
.cb_foundone
;
2201 cb
.cb_foundone
= B_FALSE
;
2202 cb
.cb_newer
= B_TRUE
;
2204 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2205 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2207 if (!cb
.cb_foundone
&& !found
) {
2208 (void) printf(gettext("All filesystems are "
2209 "formatted with the current version.\n"));
2217 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2218 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2219 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2220 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2222 * -H Scripted mode; elide headers and separate columns by tabs.
2223 * -i Translate SID to POSIX ID.
2224 * -n Print numeric ID instead of user/group name.
2225 * -o Control which fields to display.
2226 * -p Use exact (parsable) numeric output.
2227 * -s Specify sort columns, descending order.
2228 * -S Specify sort columns, ascending order.
2229 * -t Control which object types to display.
2231 * Displays space consumed by, and quotas on, each user in the specified
2232 * filesystem or snapshot.
2235 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2236 enum us_field_types
{
2242 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA" };
2243 static char *us_field_names
[] = { "type", "name", "used", "quota" };
2244 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2246 #define USTYPE_PSX_GRP (1 << 0)
2247 #define USTYPE_PSX_USR (1 << 1)
2248 #define USTYPE_SMB_GRP (1 << 2)
2249 #define USTYPE_SMB_USR (1 << 3)
2250 #define USTYPE_ALL \
2251 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2253 static int us_type_bits
[] = {
2260 static char *us_type_names
[] = { "posixgroup", "posixuser", "smbgroup",
2263 typedef struct us_node
{
2265 uu_avl_node_t usn_avlnode
;
2266 uu_list_node_t usn_listnode
;
2269 typedef struct us_cbdata
{
2271 uu_avl_pool_t
*cb_avl_pool
;
2273 boolean_t cb_numname
;
2274 boolean_t cb_nicenum
;
2275 boolean_t cb_sid2posix
;
2276 zfs_userquota_prop_t cb_prop
;
2277 zfs_sort_column_t
*cb_sortcol
;
2278 size_t cb_width
[USFIELD_LAST
];
2281 static boolean_t us_populated
= B_FALSE
;
2284 zfs_sort_column_t
*si_sortcol
;
2285 boolean_t si_numname
;
2289 us_field_index(char *field
)
2293 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2294 if (strcmp(field
, us_field_names
[i
]) == 0)
2302 us_compare(const void *larg
, const void *rarg
, void *unused
)
2304 const us_node_t
*l
= larg
;
2305 const us_node_t
*r
= rarg
;
2306 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2307 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2308 boolean_t numname
= si
->si_numname
;
2309 nvlist_t
*lnvl
= l
->usn_nvl
;
2310 nvlist_t
*rnvl
= r
->usn_nvl
;
2314 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2321 zfs_prop_t prop
= sortcol
->sc_prop
;
2322 const char *propname
= NULL
;
2323 boolean_t reverse
= sortcol
->sc_reverse
;
2328 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2329 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2331 rc
= (rv32
< lv32
) ? 1 : -1;
2336 (void) nvlist_lookup_uint64(lnvl
, propname
,
2338 (void) nvlist_lookup_uint64(rnvl
, propname
,
2341 rc
= (rv64
< lv64
) ? 1 : -1;
2343 (void) nvlist_lookup_string(lnvl
, propname
,
2345 (void) nvlist_lookup_string(rnvl
, propname
,
2347 rc
= strcmp(lvstr
, rvstr
);
2351 case ZFS_PROP_QUOTA
:
2354 if (prop
== ZFS_PROP_USED
)
2358 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2359 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2361 rc
= (rv64
< lv64
) ? 1 : -1;
2370 return (reverse
? 1 : -1);
2372 return (reverse
? -1 : 1);
2377 * If entries still seem to be the same, check if they are of the same
2378 * type (smbentity is added only if we are doing SID to POSIX ID
2379 * translation where we can have duplicate type/name combinations).
2381 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2382 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2384 return (lvb
< rvb
? -1 : 1);
2389 static inline const char *
2390 us_type2str(unsigned field_type
)
2392 switch (field_type
) {
2393 case USTYPE_PSX_USR
:
2394 return ("POSIX User");
2395 case USTYPE_PSX_GRP
:
2396 return ("POSIX Group");
2397 case USTYPE_SMB_USR
:
2398 return ("SMB User");
2399 case USTYPE_SMB_GRP
:
2400 return ("SMB Group");
2402 return ("Undefined");
2407 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2409 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2410 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2415 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2416 uu_avl_t
*avl
= cb
->cb_avl
;
2420 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2422 const char *typestr
;
2426 int typeidx
, nameidx
, sizeidx
;
2427 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2428 boolean_t smbentity
= B_FALSE
;
2430 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2432 node
= safe_malloc(sizeof (us_node_t
));
2433 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2434 node
->usn_nvl
= props
;
2436 if (domain
!= NULL
&& domain
[0] != '\0') {
2438 char sid
[MAXNAMELEN
+ 32];
2441 int flag
= IDMAP_REQ_FLG_USE_CACHE
;
2445 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2447 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2448 type
= USTYPE_SMB_GRP
;
2449 err
= sid_to_id(sid
, B_FALSE
, &id
);
2451 type
= USTYPE_SMB_USR
;
2452 err
= sid_to_id(sid
, B_TRUE
, &id
);
2457 if (!cb
->cb_sid2posix
) {
2458 if (type
== USTYPE_SMB_USR
) {
2459 (void) idmap_getwinnamebyuid(rid
, flag
,
2462 (void) idmap_getwinnamebygid(rid
, flag
,
2471 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2473 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2474 type
= USTYPE_PSX_GRP
;
2475 if (!cb
->cb_numname
) {
2478 if ((g
= getgrgid(rid
)) != NULL
)
2482 type
= USTYPE_PSX_USR
;
2483 if (!cb
->cb_numname
) {
2486 if ((p
= getpwuid(rid
)) != NULL
)
2493 * Make sure that the type/name combination is unique when doing
2494 * SID to POSIX ID translation (hence changing the type from SMB to
2497 if (cb
->cb_sid2posix
&&
2498 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2501 /* Calculate/update width of TYPE field */
2502 typestr
= us_type2str(type
);
2503 typelen
= strlen(gettext(typestr
));
2504 typeidx
= us_field_index("type");
2505 if (typelen
> cb
->cb_width
[typeidx
])
2506 cb
->cb_width
[typeidx
] = typelen
;
2507 if (nvlist_add_uint32(props
, "type", type
) != 0)
2510 /* Calculate/update width of NAME field */
2511 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2512 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2514 namelen
= snprintf(NULL
, 0, "%u", rid
);
2516 if (nvlist_add_string(props
, "name", name
) != 0)
2518 namelen
= strlen(name
);
2520 nameidx
= us_field_index("name");
2521 if (namelen
> cb
->cb_width
[nameidx
])
2522 cb
->cb_width
[nameidx
] = namelen
;
2525 * Check if this type/name combination is in the list and update it;
2526 * otherwise add new node to the list.
2528 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2529 uu_avl_insert(avl
, node
, idx
);
2534 props
= node
->usn_nvl
;
2537 /* Calculate/update width of USED/QUOTA fields */
2539 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2541 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu", space
);
2542 sizelen
= strlen(sizebuf
);
2543 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
) {
2545 if (!nvlist_exists(props
, "quota"))
2546 (void) nvlist_add_uint64(props
, "quota", 0);
2549 if (!nvlist_exists(props
, "used"))
2550 (void) nvlist_add_uint64(props
, "used", 0);
2552 sizeidx
= us_field_index(propname
);
2553 if (sizelen
> cb
->cb_width
[sizeidx
])
2554 cb
->cb_width
[sizeidx
] = sizelen
;
2556 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2563 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2564 size_t *width
, us_node_t
*node
)
2566 nvlist_t
*nvl
= node
->usn_nvl
;
2567 char valstr
[MAXNAMELEN
];
2568 boolean_t first
= B_TRUE
;
2574 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2575 if (!(ustype
& types
))
2578 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2579 nvpair_t
*nvp
= NULL
;
2583 char *strval
= NULL
;
2585 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2586 if (strcmp(nvpair_name(nvp
),
2587 us_field_names
[field
]) == 0)
2591 type
= nvpair_type(nvp
);
2593 case DATA_TYPE_UINT32
:
2594 (void) nvpair_value_uint32(nvp
, &val32
);
2596 case DATA_TYPE_UINT64
:
2597 (void) nvpair_value_uint64(nvp
, &val64
);
2599 case DATA_TYPE_STRING
:
2600 (void) nvpair_value_string(nvp
, &strval
);
2603 (void) fprintf(stderr
, "invalid data type\n");
2608 strval
= (char *)us_type2str(val32
);
2611 if (type
== DATA_TYPE_UINT64
) {
2612 (void) sprintf(valstr
, "%llu", val64
);
2618 if (type
== DATA_TYPE_UINT64
) {
2620 (void) sprintf(valstr
, "%llu", val64
);
2622 zfs_nicenum(val64
, valstr
,
2625 if (field
== USFIELD_QUOTA
&&
2626 strcmp(valstr
, "0") == 0)
2636 (void) printf("\t");
2641 (void) printf("%s", strval
);
2642 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2643 (void) printf("%-*s", width
[field
], strval
);
2645 (void) printf("%*s", width
[field
], strval
);
2651 (void) printf("\n");
2655 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2656 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2664 boolean_t first
= B_TRUE
;
2666 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2667 col
= gettext(us_field_hdr
[field
]);
2668 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2669 (void) printf(first
? "%-*s" : " %-*s",
2672 (void) printf(first
? "%*s" : " %*s",
2678 (void) printf("\n");
2681 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2682 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2684 nvlist_free(node
->usn_nvl
);
2689 zfs_do_userspace(int argc
, char **argv
)
2692 zfs_userquota_prop_t p
;
2693 uu_avl_pool_t
*avl_pool
;
2695 uu_avl_walk_t
*walk
;
2697 char deffields
[] = "type,name,used,quota";
2698 char *ofield
= NULL
;
2699 char *tfield
= NULL
;
2703 boolean_t scripted
= B_FALSE
;
2704 boolean_t prtnum
= B_FALSE
;
2705 boolean_t parsable
= B_FALSE
;
2706 boolean_t sid2posix
= B_FALSE
;
2709 zfs_sort_column_t
*sortcol
= NULL
;
2710 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2714 uu_list_pool_t
*listpool
;
2716 uu_avl_index_t idx
= 0;
2717 uu_list_index_t idx2
= 0;
2722 if (strcmp(argv
[0], "groupspace") == 0)
2723 /* Toggle default group types */
2724 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2726 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2742 if (zfs_add_sort_column(&sortcol
, optarg
,
2743 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
2744 (void) fprintf(stderr
,
2745 gettext("invalid field '%s'\n"), optarg
);
2756 (void) fprintf(stderr
, gettext("missing argument for "
2757 "'%c' option\n"), optopt
);
2761 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2771 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2775 (void) fprintf(stderr
, gettext("too many arguments\n"));
2779 /* Use default output fields if not specified using -o */
2783 if ((delim
= strchr(ofield
, ',')) != NULL
)
2785 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
2786 (void) fprintf(stderr
, gettext("invalid type '%s' "
2787 "for -o option\n"), ofield
);
2792 } while (delim
!= NULL
);
2793 fields
[cfield
] = USFIELD_LAST
;
2795 /* Override output types (-t option) */
2796 if (tfield
!= NULL
) {
2800 boolean_t found
= B_FALSE
;
2802 if ((delim
= strchr(tfield
, ',')) != NULL
)
2804 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
2806 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
2808 types
|= us_type_bits
[i
];
2813 (void) fprintf(stderr
, gettext("invalid type "
2814 "'%s' for -t option\n"), tfield
);
2819 } while (delim
!= NULL
);
2822 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
2825 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
2826 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
2828 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
2831 /* Always add default sorting columns */
2832 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
2833 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
2835 cb
.cb_sortcol
= sortcol
;
2836 cb
.cb_numname
= prtnum
;
2837 cb
.cb_nicenum
= !parsable
;
2838 cb
.cb_avl_pool
= avl_pool
;
2839 cb
.cb_avl
= avl_tree
;
2840 cb
.cb_sid2posix
= sid2posix
;
2842 for (i
= 0; i
< USFIELD_LAST
; i
++)
2843 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
2845 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
2846 if (((p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
) &&
2847 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
2848 ((p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
) &&
2849 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))))
2852 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
2857 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
2860 us_populated
= B_TRUE
;
2862 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
2863 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
2864 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
2865 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
2867 while (node
!= NULL
) {
2869 node
= uu_avl_next(avl_tree
, node
);
2870 uu_avl_remove(avl_tree
, rmnode
);
2871 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
2872 uu_list_insert(list
, rmnode
, idx2
);
2875 for (node
= uu_list_first(list
); node
!= NULL
;
2876 node
= uu_list_next(list
, node
)) {
2877 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
2879 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
2880 uu_avl_insert(avl_tree
, node
, idx
);
2883 uu_list_destroy(list
);
2884 uu_list_pool_destroy(listpool
);
2886 /* Print and free node nvlist memory */
2887 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
2890 zfs_free_sort_columns(sortcol
);
2892 /* Clean up the AVL tree */
2893 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
2896 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
2897 uu_avl_remove(cb
.cb_avl
, node
);
2901 uu_avl_walk_end(walk
);
2902 uu_avl_destroy(avl_tree
);
2903 uu_avl_pool_destroy(avl_pool
);
2909 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2910 * [-t type[,...]] [filesystem|volume|snapshot] ...
2912 * -H Scripted mode; elide headers and separate columns by tabs.
2913 * -p Display values in parsable (literal) format.
2914 * -r Recurse over all children.
2915 * -d Limit recursion by depth.
2916 * -o Control which fields to display.
2917 * -s Specify sort columns, descending order.
2918 * -S Specify sort columns, ascending order.
2919 * -t Control which object types to display.
2921 * When given no arguments, list all filesystems in the system.
2922 * Otherwise, list the specified datasets, optionally recursing down them if
2923 * '-r' is specified.
2925 typedef struct list_cbdata
{
2927 boolean_t cb_literal
;
2928 boolean_t cb_scripted
;
2929 zprop_list_t
*cb_proplist
;
2933 * Given a list of columns to display, output appropriate headers for each one.
2936 print_header(list_cbdata_t
*cb
)
2938 zprop_list_t
*pl
= cb
->cb_proplist
;
2939 char headerbuf
[ZFS_MAXPROPLEN
];
2942 boolean_t first
= B_TRUE
;
2943 boolean_t right_justify
;
2945 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2952 right_justify
= B_FALSE
;
2953 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2954 header
= zfs_prop_column_name(pl
->pl_prop
);
2955 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2957 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2958 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2959 headerbuf
[i
] = '\0';
2963 if (pl
->pl_next
== NULL
&& !right_justify
)
2964 (void) printf("%s", header
);
2965 else if (right_justify
)
2966 (void) printf("%*s", pl
->pl_width
, header
);
2968 (void) printf("%-*s", pl
->pl_width
, header
);
2971 (void) printf("\n");
2975 * Given a dataset and a list of fields, print out all the properties according
2976 * to the described layout.
2979 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
2981 zprop_list_t
*pl
= cb
->cb_proplist
;
2982 boolean_t first
= B_TRUE
;
2983 char property
[ZFS_MAXPROPLEN
];
2984 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
2987 boolean_t right_justify
;
2989 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2991 if (cb
->cb_scripted
)
2992 (void) printf("\t");
2999 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
3000 (void) strlcpy(property
, zfs_get_name(zhp
),
3003 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3004 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
3005 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
3006 sizeof (property
), NULL
, NULL
, 0,
3007 cb
->cb_literal
) != 0)
3011 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3012 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
3013 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
3014 property
, sizeof (property
), cb
->cb_literal
) != 0)
3018 right_justify
= B_TRUE
;
3019 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
3020 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
3021 property
, sizeof (property
), cb
->cb_literal
) != 0)
3025 right_justify
= B_TRUE
;
3027 if (nvlist_lookup_nvlist(userprops
,
3028 pl
->pl_user_prop
, &propval
) != 0)
3031 verify(nvlist_lookup_string(propval
,
3032 ZPROP_VALUE
, &propstr
) == 0);
3033 right_justify
= B_FALSE
;
3037 * If this is being called in scripted mode, or if this is the
3038 * last column and it is left-justified, don't include a width
3041 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3042 (void) printf("%s", propstr
);
3043 else if (right_justify
)
3044 (void) printf("%*s", pl
->pl_width
, propstr
);
3046 (void) printf("%-*s", pl
->pl_width
, propstr
);
3049 (void) printf("\n");
3053 * Generic callback function to list a dataset or snapshot.
3056 list_callback(zfs_handle_t
*zhp
, void *data
)
3058 list_cbdata_t
*cbp
= data
;
3060 if (cbp
->cb_first
) {
3061 if (!cbp
->cb_scripted
)
3063 cbp
->cb_first
= B_FALSE
;
3066 print_dataset(zhp
, cbp
);
3072 zfs_do_list(int argc
, char **argv
)
3075 static char default_fields
[] =
3076 "name,used,available,referenced,mountpoint";
3077 int types
= ZFS_TYPE_DATASET
;
3078 boolean_t types_specified
= B_FALSE
;
3079 char *fields
= NULL
;
3080 list_cbdata_t cb
= { 0 };
3084 zfs_sort_column_t
*sortcol
= NULL
;
3085 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3088 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3094 cb
.cb_literal
= B_TRUE
;
3095 flags
|= ZFS_ITER_LITERAL_PROPS
;
3098 limit
= parse_depth(optarg
, &flags
);
3101 flags
|= ZFS_ITER_RECURSE
;
3104 cb
.cb_scripted
= B_TRUE
;
3107 if (zfs_add_sort_column(&sortcol
, optarg
,
3109 (void) fprintf(stderr
,
3110 gettext("invalid property '%s'\n"), optarg
);
3115 if (zfs_add_sort_column(&sortcol
, optarg
,
3117 (void) fprintf(stderr
,
3118 gettext("invalid property '%s'\n"), optarg
);
3124 types_specified
= B_TRUE
;
3125 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3126 while (*optarg
!= '\0') {
3127 static char *type_subopts
[] = { "filesystem",
3128 "volume", "snapshot", "snap", "bookmark",
3131 switch (getsubopt(&optarg
, type_subopts
,
3134 types
|= ZFS_TYPE_FILESYSTEM
;
3137 types
|= ZFS_TYPE_VOLUME
;
3141 types
|= ZFS_TYPE_SNAPSHOT
;
3144 types
|= ZFS_TYPE_BOOKMARK
;
3147 types
= ZFS_TYPE_DATASET
|
3151 (void) fprintf(stderr
,
3152 gettext("invalid type '%s'\n"),
3159 (void) fprintf(stderr
, gettext("missing argument for "
3160 "'%c' option\n"), optopt
);
3164 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3174 fields
= default_fields
;
3177 * If we are only going to list snapshot names and sort by name,
3178 * then we can use faster version.
3180 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3181 flags
|= ZFS_ITER_SIMPLE
;
3184 * If "-o space" and no types were specified, don't display snapshots.
3186 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3187 types
&= ~ZFS_TYPE_SNAPSHOT
;
3190 * If the user specifies '-o all', the zprop_get_list() doesn't
3191 * normally include the name of the dataset. For 'zfs list', we always
3192 * want this property to be first.
3194 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3198 cb
.cb_first
= B_TRUE
;
3200 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3201 limit
, list_callback
, &cb
);
3203 zprop_free_list(cb
.cb_proplist
);
3204 zfs_free_sort_columns(sortcol
);
3206 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3207 (void) printf(gettext("no datasets available\n"));
3213 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3214 * zfs rename [-f] -p <fs | vol> <fs | vol>
3215 * zfs rename -r <snap> <snap>
3217 * Renames the given dataset to another of the same type.
3219 * The '-p' flag creates all the non-existing ancestors of the target first.
3223 zfs_do_rename(int argc
, char **argv
)
3228 boolean_t recurse
= B_FALSE
;
3229 boolean_t parents
= B_FALSE
;
3230 boolean_t force_unmount
= B_FALSE
;
3233 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3242 force_unmount
= B_TRUE
;
3246 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3255 /* check number of arguments */
3257 (void) fprintf(stderr
, gettext("missing source dataset "
3262 (void) fprintf(stderr
, gettext("missing target dataset "
3267 (void) fprintf(stderr
, gettext("too many arguments\n"));
3271 if (recurse
&& parents
) {
3272 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3277 if (recurse
&& strchr(argv
[0], '@') == 0) {
3278 (void) fprintf(stderr
, gettext("source dataset for recursive "
3279 "rename must be a snapshot\n"));
3283 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3284 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3287 /* If we were asked and the name looks good, try to create ancestors. */
3288 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3289 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3294 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3303 * Promotes the given clone fs to be the parent
3307 zfs_do_promote(int argc
, char **argv
)
3313 if (argc
> 1 && argv
[1][0] == '-') {
3314 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3319 /* check number of arguments */
3321 (void) fprintf(stderr
, gettext("missing clone filesystem"
3326 (void) fprintf(stderr
, gettext("too many arguments\n"));
3330 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3334 ret
= (zfs_promote(zhp
) != 0);
3342 * zfs rollback [-rRf] <snapshot>
3344 * -r Delete any intervening snapshots before doing rollback
3345 * -R Delete any snapshots and their clones
3346 * -f ignored for backwards compatability
3348 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3349 * since then and making it the active dataset. If more recent snapshots exist,
3350 * the command will complain unless the '-r' flag is given.
3352 typedef struct rollback_cbdata
{
3358 boolean_t cb_recurse
;
3359 } rollback_cbdata_t
;
3362 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3364 rollback_cbdata_t
*cbp
= data
;
3366 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3367 (void) fprintf(stderr
, gettext("cannot rollback to "
3368 "'%s': clones of previous snapshots exist\n"),
3370 (void) fprintf(stderr
, gettext("use '-R' to "
3371 "force deletion of the following clones and "
3377 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3384 * Report any snapshots more recent than the one specified. Used when '-r' is
3385 * not specified. We reuse this same callback for the snapshot dependents - if
3386 * 'cb_dependent' is set, then this is a dependent and we should report it
3387 * without checking the transaction group.
3390 rollback_check(zfs_handle_t
*zhp
, void *data
)
3392 rollback_cbdata_t
*cbp
= data
;
3394 if (cbp
->cb_doclones
) {
3399 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3400 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3401 (void) fprintf(stderr
, gettext("cannot "
3402 "rollback to '%s': more recent snapshots "
3403 "or bookmarks exist\n"),
3405 (void) fprintf(stderr
, gettext("use '-r' to "
3406 "force deletion of the following "
3407 "snapshots and bookmarks:\n"));
3412 if (cbp
->cb_recurse
) {
3413 if (zfs_iter_dependents(zhp
, B_TRUE
,
3414 rollback_check_dependent
, cbp
) != 0) {
3419 (void) fprintf(stderr
, "%s\n",
3428 zfs_do_rollback(int argc
, char **argv
)
3432 boolean_t force
= B_FALSE
;
3433 rollback_cbdata_t cb
= { 0 };
3434 zfs_handle_t
*zhp
, *snap
;
3435 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3439 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3452 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3461 /* check number of arguments */
3463 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3467 (void) fprintf(stderr
, gettext("too many arguments\n"));
3471 /* open the snapshot */
3472 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3475 /* open the parent dataset */
3476 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3477 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3479 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3485 * Check for more recent snapshots and/or clones based on the presence
3488 cb
.cb_target
= argv
[0];
3489 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3490 cb
.cb_first
= B_TRUE
;
3492 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, rollback_check
, &cb
)) != 0)
3494 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
3497 if ((ret
= cb
.cb_error
) != 0)
3501 * Rollback parent to the given snapshot.
3503 ret
= zfs_rollback(zhp
, snap
, force
);
3516 * zfs set property=value ... { fs | snap | vol } ...
3518 * Sets the given properties for all datasets specified on the command line.
3522 set_callback(zfs_handle_t
*zhp
, void *data
)
3524 nvlist_t
*props
= data
;
3526 if (zfs_prop_set_list(zhp
, props
) != 0) {
3527 switch (libzfs_errno(g_zfs
)) {
3528 case EZFS_MOUNTFAILED
:
3529 (void) fprintf(stderr
, gettext("property may be set "
3530 "but unable to remount filesystem\n"));
3532 case EZFS_SHARENFSFAILED
:
3533 (void) fprintf(stderr
, gettext("property may be set "
3534 "but unable to reshare filesystem\n"));
3543 zfs_do_set(int argc
, char **argv
)
3545 nvlist_t
*props
= NULL
;
3546 int ds_start
= -1; /* argv idx of first dataset arg */
3549 /* check for options */
3550 if (argc
> 1 && argv
[1][0] == '-') {
3551 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3556 /* check number of arguments */
3558 (void) fprintf(stderr
, gettext("missing arguments\n"));
3562 if (strchr(argv
[1], '=') == NULL
) {
3563 (void) fprintf(stderr
, gettext("missing property=value "
3566 (void) fprintf(stderr
, gettext("missing dataset "
3572 /* validate argument order: prop=val args followed by dataset args */
3573 for (int i
= 1; i
< argc
; i
++) {
3574 if (strchr(argv
[i
], '=') != NULL
) {
3576 /* out-of-order prop=val argument */
3577 (void) fprintf(stderr
, gettext("invalid "
3578 "argument order\n"), i
);
3581 } else if (ds_start
< 0) {
3586 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
3590 /* Populate a list of property settings */
3591 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3593 for (int i
= 1; i
< ds_start
; i
++) {
3594 if ((ret
= parseprop(props
, argv
[i
])) != 0)
3598 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
3599 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, props
);
3606 typedef struct snap_cbdata
{
3608 boolean_t sd_recursive
;
3609 const char *sd_snapname
;
3613 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3615 snap_cbdata_t
*sd
= arg
;
3620 if (sd
->sd_recursive
&&
3621 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
3626 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3629 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3632 if (sd
->sd_recursive
)
3633 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3639 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3641 * Creates a snapshot with the given name. While functionally equivalent to
3642 * 'zfs create', it is a separate command to differentiate intent.
3645 zfs_do_snapshot(int argc
, char **argv
)
3650 snap_cbdata_t sd
= { 0 };
3651 boolean_t multiple_snaps
= B_FALSE
;
3653 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3655 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3659 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3662 if (parseprop(props
, optarg
) != 0)
3666 sd
.sd_recursive
= B_TRUE
;
3667 multiple_snaps
= B_TRUE
;
3670 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3679 /* check number of arguments */
3681 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3686 multiple_snaps
= B_TRUE
;
3687 for (; argc
> 0; argc
--, argv
++) {
3691 atp
= strchr(argv
[0], '@');
3695 sd
.sd_snapname
= atp
+ 1;
3696 zhp
= zfs_open(g_zfs
, argv
[0],
3697 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3700 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3704 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3705 nvlist_free(sd
.sd_nvl
);
3707 if (ret
!= 0 && multiple_snaps
)
3708 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3712 nvlist_free(sd
.sd_nvl
);
3719 * Send a backup stream to stdout.
3722 zfs_do_send(int argc
, char **argv
)
3724 char *fromname
= NULL
;
3725 char *toname
= NULL
;
3726 char *resume_token
= NULL
;
3729 sendflags_t flags
= { 0 };
3731 nvlist_t
*dbgnv
= NULL
;
3732 boolean_t extraverbose
= B_FALSE
;
3734 struct option long_options
[] = {
3735 {"replicate", no_argument
, NULL
, 'R'},
3736 {"props", no_argument
, NULL
, 'p'},
3737 {"parsable", no_argument
, NULL
, 'P'},
3738 {"dedup", no_argument
, NULL
, 'D'},
3739 {"verbose", no_argument
, NULL
, 'v'},
3740 {"dryrun", no_argument
, NULL
, 'n'},
3741 {"large-block", no_argument
, NULL
, 'L'},
3742 {"embed", no_argument
, NULL
, 'e'},
3743 {"resume", required_argument
, NULL
, 't'},
3744 {"compressed", no_argument
, NULL
, 'c'},
3749 while ((c
= getopt_long(argc
, argv
, ":i:I:RbDpvnPLet:c", long_options
,
3761 flags
.doall
= B_TRUE
;
3764 flags
.replicate
= B_TRUE
;
3767 flags
.props
= B_TRUE
;
3770 flags
.parsable
= B_TRUE
;
3771 flags
.verbose
= B_TRUE
;
3775 extraverbose
= B_TRUE
;
3776 flags
.verbose
= B_TRUE
;
3777 flags
.progress
= B_TRUE
;
3780 flags
.dedup
= B_TRUE
;
3783 flags
.dryrun
= B_TRUE
;
3786 flags
.largeblock
= B_TRUE
;
3789 flags
.embed_data
= B_TRUE
;
3792 resume_token
= optarg
;
3795 flags
.compress
= B_TRUE
;
3799 * If a parameter was not passed, optopt contains the
3800 * value that would normally lead us into the
3801 * appropriate case statement. If it's > 256, then this
3802 * must be a longopt and we should look at argv to get
3803 * the string. Otherwise it's just the character, so we
3804 * should use it directly.
3806 if (optopt
<= UINT8_MAX
) {
3807 (void) fprintf(stderr
,
3808 gettext("missing argument for '%c' "
3809 "option\n"), optopt
);
3811 (void) fprintf(stderr
,
3812 gettext("missing argument for '%s' "
3813 "option\n"), argv
[optind
- 1]);
3821 * If an invalid flag was passed, optopt contains the
3822 * character if it was a short flag, or 0 if it was a
3826 (void) fprintf(stderr
,
3827 gettext("invalid option '%c'\n"), optopt
);
3829 (void) fprintf(stderr
,
3830 gettext("invalid option '%s'\n"),
3841 if (resume_token
!= NULL
) {
3842 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
3844 (void) fprintf(stderr
,
3845 gettext("invalid flags combined with -t\n"));
3849 (void) fprintf(stderr
, gettext("no additional "
3850 "arguments are permitted with -t\n"));
3855 (void) fprintf(stderr
,
3856 gettext("missing snapshot argument\n"));
3860 (void) fprintf(stderr
, gettext("too many arguments\n"));
3865 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
3866 (void) fprintf(stderr
,
3867 gettext("Error: Stream can not be written to a terminal.\n"
3868 "You must redirect standard output.\n"));
3872 if (resume_token
!= NULL
) {
3873 return (zfs_send_resume(g_zfs
, &flags
, STDOUT_FILENO
,
3878 * Special case sending a filesystem, or from a bookmark.
3880 if (strchr(argv
[0], '@') == NULL
||
3881 (fromname
&& strchr(fromname
, '#') != NULL
)) {
3882 char frombuf
[ZFS_MAX_DATASET_NAME_LEN
];
3883 enum lzc_send_flags lzc_flags
= 0;
3885 if (flags
.replicate
|| flags
.doall
|| flags
.props
||
3886 flags
.dedup
|| flags
.dryrun
|| flags
.verbose
||
3888 (void) fprintf(stderr
,
3890 "Unsupported flag with filesystem or bookmark.\n"));
3894 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
3898 if (flags
.largeblock
)
3899 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
3900 if (flags
.embed_data
)
3901 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
3903 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
3905 if (fromname
!= NULL
&&
3906 (fromname
[0] == '#' || fromname
[0] == '@')) {
3908 * Incremental source name begins with # or @.
3909 * Default to same fs as target.
3911 (void) strncpy(frombuf
, argv
[0], sizeof (frombuf
));
3912 cp
= strchr(frombuf
, '@');
3915 (void) strlcat(frombuf
, fromname
, sizeof (frombuf
));
3918 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, lzc_flags
);
3923 cp
= strchr(argv
[0], '@');
3926 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3931 * If they specified the full path to the snapshot, chop off
3932 * everything except the short name of the snapshot, but special
3933 * case if they specify the origin.
3935 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
3936 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
3939 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
3940 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
3942 if (strcmp(origin
, fromname
) == 0) {
3944 flags
.fromorigin
= B_TRUE
;
3947 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
3948 (void) fprintf(stderr
,
3949 gettext("incremental source must be "
3950 "in same filesystem\n"));
3954 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
3955 (void) fprintf(stderr
,
3956 gettext("invalid incremental source\n"));
3962 if (flags
.replicate
&& fromname
== NULL
)
3963 flags
.doall
= B_TRUE
;
3965 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
3966 extraverbose
? &dbgnv
: NULL
);
3968 if (extraverbose
&& dbgnv
!= NULL
) {
3970 * dump_nvlist prints to stdout, but that's been
3971 * redirected to a file. Make it print to stderr
3974 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
3975 dump_nvlist(dbgnv
, 0);
3984 * Restore a backup stream from stdin.
3987 zfs_do_receive(int argc
, char **argv
)
3990 recvflags_t flags
= { 0 };
3991 boolean_t abort_resumable
= B_FALSE
;
3994 nvpair_t
*nvp
= NULL
;
3996 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
4000 while ((c
= getopt(argc
, argv
, ":o:denuvFsA")) != -1) {
4003 if (parseprop(props
, optarg
) != 0)
4007 flags
.isprefix
= B_TRUE
;
4010 flags
.isprefix
= B_TRUE
;
4011 flags
.istail
= B_TRUE
;
4014 flags
.dryrun
= B_TRUE
;
4017 flags
.nomount
= B_TRUE
;
4020 flags
.verbose
= B_TRUE
;
4023 flags
.resumable
= B_TRUE
;
4026 flags
.force
= B_TRUE
;
4029 abort_resumable
= B_TRUE
;
4032 (void) fprintf(stderr
, gettext("missing argument for "
4033 "'%c' option\n"), optopt
);
4037 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4046 /* check number of arguments */
4048 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4052 (void) fprintf(stderr
, gettext("too many arguments\n"));
4056 while ((nvp
= nvlist_next_nvpair(props
, nvp
))) {
4057 if (strcmp(nvpair_name(nvp
), "origin") != 0) {
4058 (void) fprintf(stderr
, gettext("invalid option"));
4063 if (abort_resumable
) {
4064 if (flags
.isprefix
|| flags
.istail
|| flags
.dryrun
||
4065 flags
.resumable
|| flags
.nomount
) {
4066 (void) fprintf(stderr
, gettext("invalid option"));
4070 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
4071 (void) snprintf(namebuf
, sizeof (namebuf
),
4072 "%s/%%recv", argv
[0]);
4074 if (zfs_dataset_exists(g_zfs
, namebuf
,
4075 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) {
4076 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4077 namebuf
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4080 err
= zfs_destroy(zhp
, B_FALSE
);
4082 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4083 argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4086 if (!zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) ||
4087 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
4088 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == -1) {
4089 (void) fprintf(stderr
,
4090 gettext("'%s' does not have any "
4091 "resumable receive state to abort\n"),
4095 err
= zfs_destroy(zhp
, B_FALSE
);
4101 if (isatty(STDIN_FILENO
)) {
4102 (void) fprintf(stderr
,
4103 gettext("Error: Backup stream can not be read "
4104 "from a terminal.\n"
4105 "You must redirect standard input.\n"));
4108 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
4114 * allow/unallow stuff
4116 /* copied from zfs/sys/dsl_deleg.h */
4117 #define ZFS_DELEG_PERM_CREATE "create"
4118 #define ZFS_DELEG_PERM_DESTROY "destroy"
4119 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4120 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4121 #define ZFS_DELEG_PERM_CLONE "clone"
4122 #define ZFS_DELEG_PERM_PROMOTE "promote"
4123 #define ZFS_DELEG_PERM_RENAME "rename"
4124 #define ZFS_DELEG_PERM_MOUNT "mount"
4125 #define ZFS_DELEG_PERM_SHARE "share"
4126 #define ZFS_DELEG_PERM_SEND "send"
4127 #define ZFS_DELEG_PERM_RECEIVE "receive"
4128 #define ZFS_DELEG_PERM_ALLOW "allow"
4129 #define ZFS_DELEG_PERM_USERPROP "userprop"
4130 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4131 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4132 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4133 #define ZFS_DELEG_PERM_USERUSED "userused"
4134 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4135 #define ZFS_DELEG_PERM_HOLD "hold"
4136 #define ZFS_DELEG_PERM_RELEASE "release"
4137 #define ZFS_DELEG_PERM_DIFF "diff"
4138 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4139 #define ZFS_DELEG_PERM_REMAP "remap"
4141 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4143 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
4144 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
4145 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
4146 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
4147 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
4148 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
4149 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
4150 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
4151 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
4152 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
4153 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
4154 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
4155 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
4156 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
4157 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
4158 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
4159 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
4160 { ZFS_DELEG_PERM_REMAP
, ZFS_DELEG_NOTE_REMAP
},
4162 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
4163 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
4164 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
4165 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
4166 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
4167 { NULL
, ZFS_DELEG_NOTE_NONE
}
4170 /* permission structure */
4171 typedef struct deleg_perm
{
4172 zfs_deleg_who_type_t dp_who_type
;
4173 const char *dp_name
;
4175 boolean_t dp_descend
;
4179 typedef struct deleg_perm_node
{
4180 deleg_perm_t dpn_perm
;
4182 uu_avl_node_t dpn_avl_node
;
4183 } deleg_perm_node_t
;
4185 typedef struct fs_perm fs_perm_t
;
4187 /* permissions set */
4188 typedef struct who_perm
{
4189 zfs_deleg_who_type_t who_type
;
4190 const char *who_name
; /* id */
4191 char who_ug_name
[256]; /* user/group name */
4192 fs_perm_t
*who_fsperm
; /* uplink */
4194 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
4198 typedef struct who_perm_node
{
4199 who_perm_t who_perm
;
4200 uu_avl_node_t who_avl_node
;
4203 typedef struct fs_perm_set fs_perm_set_t
;
4204 /* fs permissions */
4206 const char *fsp_name
;
4208 uu_avl_t
*fsp_sc_avl
; /* sets,create */
4209 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
4211 fs_perm_set_t
*fsp_set
; /* uplink */
4215 typedef struct fs_perm_node
{
4216 fs_perm_t fspn_fsperm
;
4219 uu_list_node_t fspn_list_node
;
4222 /* top level structure */
4223 struct fs_perm_set
{
4224 uu_list_pool_t
*fsps_list_pool
;
4225 uu_list_t
*fsps_list
; /* list of fs_perms */
4227 uu_avl_pool_t
*fsps_named_set_avl_pool
;
4228 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
4229 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
4232 static inline const char *
4233 deleg_perm_type(zfs_deleg_note_t note
)
4239 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4240 case ZFS_DELEG_NOTE_GROUPUSED
:
4241 case ZFS_DELEG_NOTE_USERPROP
:
4242 case ZFS_DELEG_NOTE_USERQUOTA
:
4243 case ZFS_DELEG_NOTE_USERUSED
:
4245 return (gettext("other"));
4247 return (gettext("subcommand"));
4252 who_type2weight(zfs_deleg_who_type_t who_type
)
4256 case ZFS_DELEG_NAMED_SET_SETS
:
4257 case ZFS_DELEG_NAMED_SET
:
4260 case ZFS_DELEG_CREATE_SETS
:
4261 case ZFS_DELEG_CREATE
:
4264 case ZFS_DELEG_USER_SETS
:
4265 case ZFS_DELEG_USER
:
4268 case ZFS_DELEG_GROUP_SETS
:
4269 case ZFS_DELEG_GROUP
:
4272 case ZFS_DELEG_EVERYONE_SETS
:
4273 case ZFS_DELEG_EVERYONE
:
4285 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4287 const who_perm_node_t
*l
= larg
;
4288 const who_perm_node_t
*r
= rarg
;
4289 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
4290 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
4291 int lweight
= who_type2weight(ltype
);
4292 int rweight
= who_type2weight(rtype
);
4293 int res
= lweight
- rweight
;
4295 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
4296 ZFS_MAX_DELEG_NAME
-1);
4308 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4310 const deleg_perm_node_t
*l
= larg
;
4311 const deleg_perm_node_t
*r
= rarg
;
4312 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
4313 ZFS_MAX_DELEG_NAME
-1);
4325 fs_perm_set_init(fs_perm_set_t
*fspset
)
4327 bzero(fspset
, sizeof (fs_perm_set_t
));
4329 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4330 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4331 NULL
, UU_DEFAULT
)) == NULL
)
4333 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4334 UU_DEFAULT
)) == NULL
)
4337 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4338 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4339 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4340 UU_DEFAULT
)) == NULL
)
4343 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4344 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4345 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4346 UU_DEFAULT
)) == NULL
)
4349 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4350 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4351 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4356 static inline void fs_perm_fini(fs_perm_t
*);
4357 static inline void who_perm_fini(who_perm_t
*);
4360 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4362 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4364 while (node
!= NULL
) {
4365 fs_perm_node_t
*next_node
=
4366 uu_list_next(fspset
->fsps_list
, node
);
4367 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4368 fs_perm_fini(fsperm
);
4369 uu_list_remove(fspset
->fsps_list
, node
);
4374 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4375 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4376 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4380 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4383 deleg_perm
->dp_who_type
= type
;
4384 deleg_perm
->dp_name
= name
;
4388 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4389 zfs_deleg_who_type_t type
, const char *name
)
4391 uu_avl_pool_t
*pool
;
4392 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4394 bzero(who_perm
, sizeof (who_perm_t
));
4396 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4397 UU_DEFAULT
)) == NULL
)
4400 who_perm
->who_type
= type
;
4401 who_perm
->who_name
= name
;
4402 who_perm
->who_fsperm
= fsperm
;
4406 who_perm_fini(who_perm_t
*who_perm
)
4408 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4410 while (node
!= NULL
) {
4411 deleg_perm_node_t
*next_node
=
4412 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4414 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4419 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4423 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4425 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4426 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4428 bzero(fsperm
, sizeof (fs_perm_t
));
4430 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4434 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4438 fsperm
->fsp_set
= fspset
;
4439 fsperm
->fsp_name
= fsname
;
4443 fs_perm_fini(fs_perm_t
*fsperm
)
4445 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4446 while (node
!= NULL
) {
4447 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4449 who_perm_t
*who_perm
= &node
->who_perm
;
4450 who_perm_fini(who_perm
);
4451 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4456 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4457 while (node
!= NULL
) {
4458 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4460 who_perm_t
*who_perm
= &node
->who_perm
;
4461 who_perm_fini(who_perm
);
4462 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4467 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4468 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4472 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4473 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4475 uu_avl_index_t idx
= 0;
4477 deleg_perm_node_t
*found_node
= NULL
;
4478 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4480 deleg_perm_init(deleg_perm
, who_type
, name
);
4482 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4484 uu_avl_insert(avl
, node
, idx
);
4487 deleg_perm
= &node
->dpn_perm
;
4492 case ZFS_DELEG_LOCAL
:
4493 deleg_perm
->dp_local
= B_TRUE
;
4495 case ZFS_DELEG_DESCENDENT
:
4496 deleg_perm
->dp_descend
= B_TRUE
;
4501 assert(B_FALSE
); /* invalid locality */
4506 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4508 nvpair_t
*nvp
= NULL
;
4509 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4510 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4511 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4513 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4514 const char *name
= nvpair_name(nvp
);
4515 data_type_t type
= nvpair_type(nvp
);
4516 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4517 deleg_perm_node_t
*node
=
4518 safe_malloc(sizeof (deleg_perm_node_t
));
4520 assert(type
== DATA_TYPE_BOOLEAN
);
4522 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4523 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4530 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4532 nvpair_t
*nvp
= NULL
;
4533 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4535 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4536 nvlist_t
*nvl2
= NULL
;
4537 const char *name
= nvpair_name(nvp
);
4538 uu_avl_t
*avl
= NULL
;
4539 uu_avl_pool_t
*avl_pool
= NULL
;
4540 zfs_deleg_who_type_t perm_type
= name
[0];
4541 char perm_locality
= name
[1];
4542 const char *perm_name
= name
+ 3;
4543 boolean_t is_set
= B_TRUE
;
4544 who_perm_t
*who_perm
= NULL
;
4546 assert('$' == name
[2]);
4548 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4551 switch (perm_type
) {
4552 case ZFS_DELEG_CREATE
:
4553 case ZFS_DELEG_CREATE_SETS
:
4554 case ZFS_DELEG_NAMED_SET
:
4555 case ZFS_DELEG_NAMED_SET_SETS
:
4556 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4557 avl
= fsperm
->fsp_sc_avl
;
4559 case ZFS_DELEG_USER
:
4560 case ZFS_DELEG_USER_SETS
:
4561 case ZFS_DELEG_GROUP
:
4562 case ZFS_DELEG_GROUP_SETS
:
4563 case ZFS_DELEG_EVERYONE
:
4564 case ZFS_DELEG_EVERYONE_SETS
:
4565 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4566 avl
= fsperm
->fsp_uge_avl
;
4570 assert(!"unhandled zfs_deleg_who_type_t");
4574 who_perm_node_t
*found_node
= NULL
;
4575 who_perm_node_t
*node
= safe_malloc(
4576 sizeof (who_perm_node_t
));
4577 who_perm
= &node
->who_perm
;
4578 uu_avl_index_t idx
= 0;
4580 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4581 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4583 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4585 if (avl
== fsperm
->fsp_uge_avl
) {
4587 struct passwd
*p
= NULL
;
4588 struct group
*g
= NULL
;
4589 const char *nice_name
= NULL
;
4591 switch (perm_type
) {
4592 case ZFS_DELEG_USER_SETS
:
4593 case ZFS_DELEG_USER
:
4594 rid
= atoi(perm_name
);
4597 nice_name
= p
->pw_name
;
4599 case ZFS_DELEG_GROUP_SETS
:
4600 case ZFS_DELEG_GROUP
:
4601 rid
= atoi(perm_name
);
4604 nice_name
= g
->gr_name
;
4611 if (nice_name
!= NULL
)
4613 node
->who_perm
.who_ug_name
,
4617 uu_avl_insert(avl
, node
, idx
);
4620 who_perm
= &node
->who_perm
;
4624 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4631 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4633 nvpair_t
*nvp
= NULL
;
4634 uu_avl_index_t idx
= 0;
4636 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4637 nvlist_t
*nvl2
= NULL
;
4638 const char *fsname
= nvpair_name(nvp
);
4639 data_type_t type
= nvpair_type(nvp
);
4640 fs_perm_t
*fsperm
= NULL
;
4641 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4645 fsperm
= &node
->fspn_fsperm
;
4647 assert(DATA_TYPE_NVLIST
== type
);
4649 uu_list_node_init(node
, &node
->fspn_list_node
,
4650 fspset
->fsps_list_pool
);
4652 idx
= uu_list_numnodes(fspset
->fsps_list
);
4653 fs_perm_init(fsperm
, fspset
, fsname
);
4655 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4658 (void) parse_fs_perm(fsperm
, nvl2
);
4660 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4666 static inline const char *
4667 deleg_perm_comment(zfs_deleg_note_t note
)
4669 const char *str
= "";
4674 case ZFS_DELEG_NOTE_ALLOW
:
4675 str
= gettext("Must also have the permission that is being"
4676 "\n\t\t\t\tallowed");
4678 case ZFS_DELEG_NOTE_CLONE
:
4679 str
= gettext("Must also have the 'create' ability and 'mount'"
4680 "\n\t\t\t\tability in the origin file system");
4682 case ZFS_DELEG_NOTE_CREATE
:
4683 str
= gettext("Must also have the 'mount' ability");
4685 case ZFS_DELEG_NOTE_DESTROY
:
4686 str
= gettext("Must also have the 'mount' ability");
4688 case ZFS_DELEG_NOTE_DIFF
:
4689 str
= gettext("Allows lookup of paths within a dataset;"
4690 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4691 "\n\t\t\t\tin order to use zfs diff");
4693 case ZFS_DELEG_NOTE_HOLD
:
4694 str
= gettext("Allows adding a user hold to a snapshot");
4696 case ZFS_DELEG_NOTE_MOUNT
:
4697 str
= gettext("Allows mount/umount of ZFS datasets");
4699 case ZFS_DELEG_NOTE_PROMOTE
:
4700 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4701 " 'promote' ability in the origin file system");
4703 case ZFS_DELEG_NOTE_RECEIVE
:
4704 str
= gettext("Must also have the 'mount' and 'create'"
4707 case ZFS_DELEG_NOTE_RELEASE
:
4708 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4709 "might destroy the snapshot");
4711 case ZFS_DELEG_NOTE_RENAME
:
4712 str
= gettext("Must also have the 'mount' and 'create'"
4713 "\n\t\t\t\tability in the new parent");
4715 case ZFS_DELEG_NOTE_ROLLBACK
:
4718 case ZFS_DELEG_NOTE_SEND
:
4721 case ZFS_DELEG_NOTE_SHARE
:
4722 str
= gettext("Allows sharing file systems over NFS or SMB"
4723 "\n\t\t\t\tprotocols");
4725 case ZFS_DELEG_NOTE_SNAPSHOT
:
4729 * case ZFS_DELEG_NOTE_VSCAN:
4730 * str = gettext("");
4734 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4735 str
= gettext("Allows accessing any groupquota@... property");
4737 case ZFS_DELEG_NOTE_GROUPUSED
:
4738 str
= gettext("Allows reading any groupused@... property");
4740 case ZFS_DELEG_NOTE_USERPROP
:
4741 str
= gettext("Allows changing any user property");
4743 case ZFS_DELEG_NOTE_USERQUOTA
:
4744 str
= gettext("Allows accessing any userquota@... property");
4746 case ZFS_DELEG_NOTE_USERUSED
:
4747 str
= gettext("Allows reading any userused@... property");
4765 boolean_t recursive
; /* unallow only */
4766 boolean_t prt_usage
;
4768 boolean_t prt_perms
;
4771 const char *dataset
;
4775 prop_cmp(const void *a
, const void *b
)
4777 const char *str1
= *(const char **)a
;
4778 const char *str2
= *(const char **)b
;
4779 return (strcmp(str1
, str2
));
4783 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
4785 const char *opt_desc
[] = {
4786 "-h", gettext("show this help message and exit"),
4787 "-l", gettext("set permission locally"),
4788 "-d", gettext("set permission for descents"),
4789 "-u", gettext("set permission for user"),
4790 "-g", gettext("set permission for group"),
4791 "-e", gettext("set permission for everyone"),
4792 "-c", gettext("set create time permission"),
4793 "-s", gettext("define permission set"),
4795 "-r", gettext("remove permissions recursively"),
4797 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
4798 size_t allow_size
= unallow_size
- 2;
4799 const char *props
[ZFS_NUM_PROPS
];
4802 FILE *fp
= requested
? stdout
: stderr
;
4803 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
4804 const char *fmt
= gettext("%-16s %-14s\t%s\n");
4806 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
4808 (void) fprintf(fp
, gettext("Options:\n"));
4809 for (int i
= 0; i
< (un
? unallow_size
: allow_size
); i
++) {
4810 const char *opt
= opt_desc
[i
++];
4811 const char *optdsc
= opt_desc
[i
];
4812 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
4815 (void) fprintf(fp
, gettext("\nThe following permissions are "
4817 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
4819 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
4820 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
4821 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
4822 const char *perm_type
= deleg_perm_type(perm_note
);
4823 const char *perm_comment
= deleg_perm_comment(perm_note
);
4824 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
4827 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
4828 zprop_desc_t
*pd
= &pdtbl
[i
];
4829 if (pd
->pd_visible
!= B_TRUE
)
4832 if (pd
->pd_attr
== PROP_READONLY
)
4835 props
[count
++] = pd
->pd_name
;
4837 props
[count
] = NULL
;
4839 qsort(props
, count
, sizeof (char *), prop_cmp
);
4841 for (i
= 0; i
< count
; i
++)
4842 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
4845 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
4847 exit(requested
? 0 : 2);
4850 static inline const char *
4851 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
4854 if (un
&& argc
== expected_argc
- 1)
4856 else if (argc
== expected_argc
)
4857 *permsp
= argv
[argc
- 2];
4859 allow_usage(un
, B_FALSE
,
4860 gettext("wrong number of parameters\n"));
4862 return (argv
[argc
- 1]);
4866 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
4868 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
4869 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
4870 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
4871 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
4874 allow_usage(un
, B_FALSE
,
4875 gettext("-u, -g, and -e are mutually exclusive\n"));
4877 if (opts
->prt_usage
) {
4878 if (argc
== 0 && all_sum
== 0)
4879 allow_usage(un
, B_TRUE
, NULL
);
4886 allow_usage(un
, B_FALSE
,
4887 gettext("invalid options combined with -s\n"));
4889 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4890 if (argv
[0][0] != '@')
4891 allow_usage(un
, B_FALSE
,
4892 gettext("invalid set name: missing '@' prefix\n"));
4893 opts
->who
= argv
[0];
4894 } else if (opts
->create
) {
4895 if (ldcsuge_sum
> 1)
4896 allow_usage(un
, B_FALSE
,
4897 gettext("invalid options combined with -c\n"));
4898 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4899 } else if (opts
->everyone
) {
4901 allow_usage(un
, B_FALSE
,
4902 gettext("invalid options combined with -e\n"));
4903 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4904 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
4906 opts
->everyone
= B_TRUE
;
4909 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4910 } else if (argc
== 1 && !un
) {
4911 opts
->prt_perms
= B_TRUE
;
4912 opts
->dataset
= argv
[argc
-1];
4914 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4915 opts
->who
= argv
[0];
4918 if (!opts
->local
&& !opts
->descend
) {
4919 opts
->local
= B_TRUE
;
4920 opts
->descend
= B_TRUE
;
4925 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
4926 const char *who
, char *perms
, nvlist_t
*top_nvl
)
4929 char ld
[2] = { '\0', '\0' };
4930 char who_buf
[MAXNAMELEN
+ 32];
4931 char base_type
= '\0';
4932 char set_type
= '\0';
4933 nvlist_t
*base_nvl
= NULL
;
4934 nvlist_t
*set_nvl
= NULL
;
4937 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4939 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4943 case ZFS_DELEG_NAMED_SET_SETS
:
4944 case ZFS_DELEG_NAMED_SET
:
4945 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
4946 base_type
= ZFS_DELEG_NAMED_SET
;
4947 ld
[0] = ZFS_DELEG_NA
;
4949 case ZFS_DELEG_CREATE_SETS
:
4950 case ZFS_DELEG_CREATE
:
4951 set_type
= ZFS_DELEG_CREATE_SETS
;
4952 base_type
= ZFS_DELEG_CREATE
;
4953 ld
[0] = ZFS_DELEG_NA
;
4955 case ZFS_DELEG_USER_SETS
:
4956 case ZFS_DELEG_USER
:
4957 set_type
= ZFS_DELEG_USER_SETS
;
4958 base_type
= ZFS_DELEG_USER
;
4960 ld
[0] = ZFS_DELEG_LOCAL
;
4962 ld
[1] = ZFS_DELEG_DESCENDENT
;
4964 case ZFS_DELEG_GROUP_SETS
:
4965 case ZFS_DELEG_GROUP
:
4966 set_type
= ZFS_DELEG_GROUP_SETS
;
4967 base_type
= ZFS_DELEG_GROUP
;
4969 ld
[0] = ZFS_DELEG_LOCAL
;
4971 ld
[1] = ZFS_DELEG_DESCENDENT
;
4973 case ZFS_DELEG_EVERYONE_SETS
:
4974 case ZFS_DELEG_EVERYONE
:
4975 set_type
= ZFS_DELEG_EVERYONE_SETS
;
4976 base_type
= ZFS_DELEG_EVERYONE
;
4978 ld
[0] = ZFS_DELEG_LOCAL
;
4980 ld
[1] = ZFS_DELEG_DESCENDENT
;
4984 assert(set_type
!= '\0' && base_type
!= '\0');
4987 if (perms
!= NULL
) {
4989 char *end
= curr
+ strlen(perms
);
4991 while (curr
< end
) {
4992 char *delim
= strchr(curr
, ',');
5003 (void) nvlist_add_boolean(nvl
, curr
);
5009 for (i
= 0; i
< 2; i
++) {
5010 char locality
= ld
[i
];
5014 if (!nvlist_empty(base_nvl
)) {
5016 (void) snprintf(who_buf
,
5017 sizeof (who_buf
), "%c%c$%s",
5018 base_type
, locality
, who
);
5020 (void) snprintf(who_buf
,
5021 sizeof (who_buf
), "%c%c$",
5022 base_type
, locality
);
5024 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5029 if (!nvlist_empty(set_nvl
)) {
5031 (void) snprintf(who_buf
,
5032 sizeof (who_buf
), "%c%c$%s",
5033 set_type
, locality
, who
);
5035 (void) snprintf(who_buf
,
5036 sizeof (who_buf
), "%c%c$",
5037 set_type
, locality
);
5039 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5044 for (i
= 0; i
< 2; i
++) {
5045 char locality
= ld
[i
];
5050 (void) snprintf(who_buf
, sizeof (who_buf
),
5051 "%c%c$%s", base_type
, locality
, who
);
5053 (void) snprintf(who_buf
, sizeof (who_buf
),
5054 "%c%c$", base_type
, locality
);
5055 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5058 (void) snprintf(who_buf
, sizeof (who_buf
),
5059 "%c%c$%s", set_type
, locality
, who
);
5061 (void) snprintf(who_buf
, sizeof (who_buf
),
5062 "%c%c$", set_type
, locality
);
5063 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5069 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
5071 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
5075 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
5076 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
5077 } else if (opts
->create
) {
5078 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
5079 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5080 } else if (opts
->everyone
) {
5081 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
5082 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5084 char *curr
= opts
->who
;
5085 char *end
= curr
+ strlen(curr
);
5087 while (curr
< end
) {
5089 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
5091 char *delim
= strchr(curr
, ',');
5094 struct passwd
*p
= NULL
;
5095 struct group
*g
= NULL
;
5103 rid
= (uid_t
)strtol(curr
, &endch
, 0);
5105 who_type
= ZFS_DELEG_USER
;
5114 (void) snprintf(errbuf
, 256, gettext(
5115 "invalid user %s"), curr
);
5116 allow_usage(un
, B_TRUE
, errbuf
);
5118 } else if (opts
->group
) {
5119 who_type
= ZFS_DELEG_GROUP
;
5128 (void) snprintf(errbuf
, 256, gettext(
5129 "invalid group %s"), curr
);
5130 allow_usage(un
, B_TRUE
, errbuf
);
5133 if (*endch
!= '\0') {
5140 if (*endch
!= '\0') {
5148 who_type
= ZFS_DELEG_USER
;
5150 } else if (g
!= NULL
) {
5151 who_type
= ZFS_DELEG_GROUP
;
5154 (void) snprintf(errbuf
, 256, gettext(
5155 "invalid user/group %s"), curr
);
5156 allow_usage(un
, B_TRUE
, errbuf
);
5160 (void) sprintf(id
, "%u", rid
);
5163 store_allow_perm(who_type
, opts
->local
,
5164 opts
->descend
, who
, opts
->perms
, *nvlp
);
5173 print_set_creat_perms(uu_avl_t
*who_avl
)
5175 const char *sc_title
[] = {
5176 gettext("Permission sets:\n"),
5177 gettext("Create time permissions:\n"),
5180 const char **title_ptr
= sc_title
;
5181 who_perm_node_t
*who_node
= NULL
;
5182 int prev_weight
= -1;
5184 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
5185 who_node
= uu_avl_next(who_avl
, who_node
)) {
5186 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5187 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5188 const char *who_name
= who_node
->who_perm
.who_name
;
5189 int weight
= who_type2weight(who_type
);
5190 boolean_t first
= B_TRUE
;
5191 deleg_perm_node_t
*deleg_node
;
5193 if (prev_weight
!= weight
) {
5194 (void) printf(*title_ptr
++);
5195 prev_weight
= weight
;
5198 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
5199 (void) printf("\t");
5201 (void) printf("\t%s ", who_name
);
5203 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
5204 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5207 deleg_node
->dpn_perm
.dp_name
);
5210 (void) printf(",%s",
5211 deleg_node
->dpn_perm
.dp_name
);
5214 (void) printf("\n");
5219 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
5222 who_perm_node_t
*who_node
= NULL
;
5223 boolean_t prt_title
= B_TRUE
;
5224 uu_avl_walk_t
*walk
;
5226 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
5229 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
5230 const char *who_name
= who_node
->who_perm
.who_name
;
5231 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
5232 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5233 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5235 deleg_perm_node_t
*deleg_node
;
5236 boolean_t prt_who
= B_TRUE
;
5238 for (deleg_node
= uu_avl_first(avl
);
5240 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5241 if (local
!= deleg_node
->dpn_perm
.dp_local
||
5242 descend
!= deleg_node
->dpn_perm
.dp_descend
)
5246 const char *who
= NULL
;
5248 prt_title
= B_FALSE
;
5249 (void) printf(title
);
5253 case ZFS_DELEG_USER_SETS
:
5254 case ZFS_DELEG_USER
:
5255 who
= gettext("user");
5257 who_name
= nice_who_name
;
5259 case ZFS_DELEG_GROUP_SETS
:
5260 case ZFS_DELEG_GROUP
:
5261 who
= gettext("group");
5263 who_name
= nice_who_name
;
5265 case ZFS_DELEG_EVERYONE_SETS
:
5266 case ZFS_DELEG_EVERYONE
:
5267 who
= gettext("everyone");
5272 assert(who
!= NULL
);
5276 if (who_name
== NULL
)
5277 (void) printf("\t%s", who
);
5279 (void) printf("\t%s %s", who
, who_name
);
5282 (void) printf("%c%s", delim
,
5283 deleg_node
->dpn_perm
.dp_name
);
5288 (void) printf("\n");
5291 uu_avl_walk_end(walk
);
5295 print_fs_perms(fs_perm_set_t
*fspset
)
5297 fs_perm_node_t
*node
= NULL
;
5298 char buf
[MAXNAMELEN
+ 32];
5299 const char *dsname
= buf
;
5301 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
5302 node
= uu_list_next(fspset
->fsps_list
, node
)) {
5303 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
5304 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
5307 (void) snprintf(buf
, sizeof (buf
),
5308 gettext("---- Permissions on %s "),
5309 node
->fspn_fsperm
.fsp_name
);
5310 (void) printf(dsname
);
5311 left
= 70 - strlen(buf
);
5314 (void) printf("\n");
5316 print_set_creat_perms(sc_avl
);
5317 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
5318 gettext("Local permissions:\n"));
5319 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
5320 gettext("Descendent permissions:\n"));
5321 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
5322 gettext("Local+Descendent permissions:\n"));
5326 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5328 struct deleg_perms
{
5334 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5336 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5337 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5339 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5342 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5346 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5349 nvlist_t
*perm_nvl
= NULL
;
5350 nvlist_t
*update_perm_nvl
= NULL
;
5353 struct allow_opts opts
= { 0 };
5355 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5358 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5361 opts
.local
= B_TRUE
;
5364 opts
.descend
= B_TRUE
;
5370 opts
.group
= B_TRUE
;
5373 opts
.everyone
= B_TRUE
;
5379 opts
.create
= B_TRUE
;
5382 opts
.recursive
= B_TRUE
;
5385 (void) fprintf(stderr
, gettext("missing argument for "
5386 "'%c' option\n"), optopt
);
5390 opts
.prt_usage
= B_TRUE
;
5393 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5402 /* check arguments */
5403 parse_allow_args(argc
, argv
, un
, &opts
);
5405 /* try to open the dataset */
5406 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5407 ZFS_TYPE_VOLUME
)) == NULL
) {
5408 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5413 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5416 fs_perm_set_init(&fs_perm_set
);
5417 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5418 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5423 print_fs_perms(&fs_perm_set
);
5425 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5426 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5429 if (un
&& opts
.recursive
) {
5430 struct deleg_perms data
= { un
, update_perm_nvl
};
5431 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5440 nvlist_free(perm_nvl
);
5441 nvlist_free(update_perm_nvl
);
5443 fs_perm_set_fini(&fs_perm_set
);
5451 zfs_do_allow(int argc
, char **argv
)
5453 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5457 zfs_do_unallow(int argc
, char **argv
)
5459 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5463 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5468 boolean_t recursive
= B_FALSE
;
5469 const char *opts
= holding
? "rt" : "r";
5473 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5479 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5488 /* check number of arguments */
5496 if (holding
&& tag
[0] == '.') {
5497 /* tags starting with '.' are reserved for libzfs */
5498 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5502 for (i
= 0; i
< argc
; ++i
) {
5504 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
5506 char *path
= argv
[i
];
5508 delim
= strchr(path
, '@');
5509 if (delim
== NULL
) {
5510 (void) fprintf(stderr
,
5511 gettext("'%s' is not a snapshot\n"), path
);
5515 (void) strncpy(parent
, path
, delim
- path
);
5516 parent
[delim
- path
] = '\0';
5518 zhp
= zfs_open(g_zfs
, parent
,
5519 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5525 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
5528 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5534 return (errors
!= 0);
5538 * zfs hold [-r] [-t] <tag> <snap> ...
5540 * -r Recursively hold
5542 * Apply a user-hold with the given tag to the list of snapshots.
5545 zfs_do_hold(int argc
, char **argv
)
5547 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5551 * zfs release [-r] <tag> <snap> ...
5553 * -r Recursively release
5555 * Release a user-hold with the given tag from the list of snapshots.
5558 zfs_do_release(int argc
, char **argv
)
5560 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5563 typedef struct holds_cbdata
{
5564 boolean_t cb_recursive
;
5565 const char *cb_snapname
;
5567 size_t cb_max_namelen
;
5568 size_t cb_max_taglen
;
5571 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5572 #define DATETIME_BUF_LEN (32)
5577 print_holds(boolean_t scripted
, size_t nwidth
, size_t tagwidth
, nvlist_t
*nvl
)
5580 nvpair_t
*nvp
= NULL
;
5581 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5585 for (i
= 0; i
< 3; i
++) {
5586 col
= gettext(hdr_cols
[i
]);
5588 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5591 (void) printf("%s\n", col
);
5595 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5596 char *zname
= nvpair_name(nvp
);
5598 nvpair_t
*nvp2
= NULL
;
5599 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5600 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5601 char tsbuf
[DATETIME_BUF_LEN
];
5602 char *tagname
= nvpair_name(nvp2
);
5607 (void) nvpair_value_uint64(nvp2
, &val
);
5609 (void) localtime_r(&time
, &t
);
5610 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5611 gettext(STRFTIME_FMT_STR
), &t
);
5614 (void) printf("%s\t%s\t%s\n", zname
,
5617 (void) printf("%-*s %-*s %s\n", nwidth
,
5618 zname
, tagwidth
, tagname
, tsbuf
);
5625 * Generic callback function to list a dataset or snapshot.
5628 holds_callback(zfs_handle_t
*zhp
, void *data
)
5630 holds_cbdata_t
*cbp
= data
;
5631 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5632 nvlist_t
*nvl
= NULL
;
5633 nvpair_t
*nvp
= NULL
;
5634 const char *zname
= zfs_get_name(zhp
);
5635 size_t znamelen
= strlen(zname
);
5637 if (cbp
->cb_recursive
) {
5638 const char *snapname
;
5639 char *delim
= strchr(zname
, '@');
5643 snapname
= delim
+ 1;
5644 if (strcmp(cbp
->cb_snapname
, snapname
))
5648 if (zfs_get_holds(zhp
, &nvl
) != 0)
5651 if (znamelen
> cbp
->cb_max_namelen
)
5652 cbp
->cb_max_namelen
= znamelen
;
5654 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5655 const char *tag
= nvpair_name(nvp
);
5656 size_t taglen
= strlen(tag
);
5657 if (taglen
> cbp
->cb_max_taglen
)
5658 cbp
->cb_max_taglen
= taglen
;
5661 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5665 * zfs holds [-r] <snap> ...
5667 * -r Recursively hold
5670 zfs_do_holds(int argc
, char **argv
)
5675 boolean_t scripted
= B_FALSE
;
5676 boolean_t recursive
= B_FALSE
;
5677 const char *opts
= "rH";
5680 int types
= ZFS_TYPE_SNAPSHOT
;
5681 holds_cbdata_t cb
= { 0 };
5688 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5697 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5704 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
5705 flags
|= ZFS_ITER_RECURSE
;
5711 /* check number of arguments */
5715 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5718 for (i
= 0; i
< argc
; ++i
) {
5719 char *snapshot
= argv
[i
];
5721 const char *snapname
;
5723 delim
= strchr(snapshot
, '@');
5724 if (delim
== NULL
) {
5725 (void) fprintf(stderr
,
5726 gettext("'%s' is not a snapshot\n"), snapshot
);
5730 snapname
= delim
+ 1;
5732 snapshot
[delim
- snapshot
] = '\0';
5734 cb
.cb_recursive
= recursive
;
5735 cb
.cb_snapname
= snapname
;
5739 * 1. collect holds data, set format options
5741 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
5742 holds_callback
, &cb
);
5748 * 2. print holds data
5750 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
5752 if (nvlist_empty(nvl
))
5753 (void) printf(gettext("no datasets available\n"));
5757 return (0 != errors
);
5760 #define CHECK_SPINNER 30
5761 #define SPINNER_TIME 3 /* seconds */
5762 #define MOUNT_TIME 5 /* seconds */
5765 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
5767 static char *spin
[] = { "-", "\\", "|", "/" };
5768 static int spinval
= 0;
5769 static int spincheck
= 0;
5770 static time_t last_spin_time
= (time_t)0;
5771 get_all_cb_t
*cbp
= data
;
5772 zfs_type_t type
= zfs_get_type(zhp
);
5774 if (cbp
->cb_verbose
) {
5775 if (--spincheck
< 0) {
5776 time_t now
= time(NULL
);
5777 if (last_spin_time
+ SPINNER_TIME
< now
) {
5778 update_progress(spin
[spinval
++ % 4]);
5779 last_spin_time
= now
;
5781 spincheck
= CHECK_SPINNER
;
5786 * Interate over any nested datasets.
5788 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
5794 * Skip any datasets whose type does not match.
5796 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
5800 libzfs_add_handle(cbp
, zhp
);
5801 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
5807 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
5809 get_all_cb_t cb
= { 0 };
5810 cb
.cb_verbose
= verbose
;
5811 cb
.cb_getone
= get_one_dataset
;
5814 set_progress_header(gettext("Reading ZFS config"));
5815 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
5817 *dslist
= cb
.cb_handles
;
5818 *count
= cb
.cb_used
;
5821 finish_progress(gettext("done."));
5825 * Generic callback for sharing or mounting filesystems. Because the code is so
5826 * similar, we have a common function with an extra parameter to determine which
5827 * mode we are using.
5829 #define OP_SHARE 0x1
5830 #define OP_MOUNT 0x2
5833 * Share or mount a dataset.
5836 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
5837 boolean_t
explicit, const char *options
)
5839 char mountpoint
[ZFS_MAXPROPLEN
];
5840 char shareopts
[ZFS_MAXPROPLEN
];
5841 char smbshareopts
[ZFS_MAXPROPLEN
];
5842 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
5844 uint64_t zoned
, canmount
;
5845 boolean_t shared_nfs
, shared_smb
;
5847 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
5850 * Check to make sure we can mount/share this dataset. If we
5851 * are in the global zone and the filesystem is exported to a
5852 * local zone, or if we are in a local zone and the
5853 * filesystem is not exported, then it is an error.
5855 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
5857 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
5861 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5862 "dataset is exported to a local zone\n"), cmdname
,
5866 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
5870 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5871 "permission denied\n"), cmdname
,
5877 * Ignore any filesystems which don't apply to us. This
5878 * includes those with a legacy mountpoint, or those with
5879 * legacy share options.
5881 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
5882 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
5883 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
5884 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5885 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
5886 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5888 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
5889 strcmp(smbshareopts
, "off") == 0) {
5893 (void) fprintf(stderr
, gettext("cannot share '%s': "
5894 "legacy share\n"), zfs_get_name(zhp
));
5895 (void) fprintf(stderr
, gettext("use share(1M) to "
5896 "share this filesystem, or set "
5897 "sharenfs property on\n"));
5902 * We cannot share or mount legacy filesystems. If the
5903 * shareopts is non-legacy but the mountpoint is legacy, we
5904 * treat it as a legacy share.
5906 if (strcmp(mountpoint
, "legacy") == 0) {
5910 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5911 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
5912 (void) fprintf(stderr
, gettext("use %s(1M) to "
5913 "%s this filesystem\n"), cmdname
, cmdname
);
5917 if (strcmp(mountpoint
, "none") == 0) {
5921 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
5922 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
5927 * canmount explicit outcome
5928 * on no pass through
5929 * on yes pass through
5931 * off yes display error, return 1
5932 * noauto no return 0
5933 * noauto yes pass through
5935 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
5936 if (canmount
== ZFS_CANMOUNT_OFF
) {
5940 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5941 "'canmount' property is set to 'off'\n"), cmdname
,
5944 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
5949 * If this filesystem is inconsistent and has a receive resume
5950 * token, we can not mount it.
5952 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
5953 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
5954 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
5958 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5959 "Contains partially-completed state from "
5960 "\"zfs receive -r\", which can be resumed with "
5961 "\"zfs send -t\"\n"),
5962 cmdname
, zfs_get_name(zhp
));
5967 * At this point, we have verified that the mountpoint and/or
5968 * shareopts are appropriate for auto management. If the
5969 * filesystem is already mounted or shared, return (failing
5970 * for explicit requests); otherwise mount or share the
5976 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
5977 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
5979 if ((shared_nfs
&& shared_smb
) ||
5980 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
5981 strcmp(smbshareopts
, "off") == 0) ||
5982 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
5983 strcmp(shareopts
, "off") == 0)) {
5987 (void) fprintf(stderr
, gettext("cannot share "
5988 "'%s': filesystem already shared\n"),
5993 if (!zfs_is_mounted(zhp
, NULL
) &&
5994 zfs_mount(zhp
, NULL
, 0) != 0)
5997 if (protocol
== NULL
) {
5998 if (zfs_shareall(zhp
) != 0)
6000 } else if (strcmp(protocol
, "nfs") == 0) {
6001 if (zfs_share_nfs(zhp
))
6003 } else if (strcmp(protocol
, "smb") == 0) {
6004 if (zfs_share_smb(zhp
))
6007 (void) fprintf(stderr
, gettext("cannot share "
6008 "'%s': invalid share type '%s' "
6010 zfs_get_name(zhp
), protocol
);
6017 if (options
== NULL
)
6018 mnt
.mnt_mntopts
= "";
6020 mnt
.mnt_mntopts
= (char *)options
;
6022 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
6023 zfs_is_mounted(zhp
, NULL
)) {
6027 (void) fprintf(stderr
, gettext("cannot mount "
6028 "'%s': filesystem already mounted\n"),
6033 if (zfs_mount(zhp
, options
, flags
) != 0)
6042 * Reports progress in the form "(current/total)". Not thread-safe.
6045 report_mount_progress(int current
, int total
)
6047 static time_t last_progress_time
= 0;
6048 time_t now
= time(NULL
);
6051 /* report 1..n instead of 0..n-1 */
6054 /* display header if we're here for the first time */
6056 set_progress_header(gettext("Mounting ZFS filesystems"));
6057 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
6058 /* too soon to report again */
6062 last_progress_time
= now
;
6064 (void) sprintf(info
, "(%d/%d)", current
, total
);
6066 if (current
== total
)
6067 finish_progress(info
);
6069 update_progress(info
);
6073 append_options(char *mntopts
, char *newopts
)
6075 int len
= strlen(mntopts
);
6077 /* original length plus new string to append plus 1 for the comma */
6078 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
6079 (void) fprintf(stderr
, gettext("the opts argument for "
6080 "'%c' option is too long (more than %d chars)\n"),
6081 "-o", MNT_LINE_MAX
);
6086 mntopts
[len
++] = ',';
6088 (void) strcpy(&mntopts
[len
], newopts
);
6092 share_mount(int op
, int argc
, char **argv
)
6095 boolean_t verbose
= B_FALSE
;
6097 char *options
= NULL
;
6101 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
6111 if (*optarg
== '\0') {
6112 (void) fprintf(stderr
, gettext("empty mount "
6113 "options (-o) specified\n"));
6117 if (options
== NULL
)
6118 options
= safe_malloc(MNT_LINE_MAX
+ 1);
6120 /* option validation is done later */
6121 append_options(options
, optarg
);
6125 flags
|= MS_OVERLAY
;
6128 (void) fprintf(stderr
, gettext("missing argument for "
6129 "'%c' option\n"), optopt
);
6133 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6142 /* check number of arguments */
6144 zfs_handle_t
**dslist
= NULL
;
6145 size_t i
, count
= 0;
6146 char *protocol
= NULL
;
6148 if (op
== OP_SHARE
&& argc
> 0) {
6149 if (strcmp(argv
[0], "nfs") != 0 &&
6150 strcmp(argv
[0], "smb") != 0) {
6151 (void) fprintf(stderr
, gettext("share type "
6152 "must be 'nfs' or 'smb'\n"));
6161 (void) fprintf(stderr
, gettext("too many arguments\n"));
6165 start_progress_timer();
6166 get_all_datasets(&dslist
, &count
, verbose
);
6171 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
6172 sa_init_selective_arg_t sharearg
;
6173 sharearg
.zhandle_arr
= dslist
;
6174 sharearg
.zhandle_len
= count
;
6175 if ((ret
= zfs_init_libshare_arg(zfs_get_handle(dslist
[0]),
6176 SA_INIT_SHARE_API_SELECTIVE
, &sharearg
)) != SA_OK
) {
6177 (void) fprintf(stderr
,
6178 gettext("Could not initialize libshare, %d"), ret
);
6182 for (i
= 0; i
< count
; i
++) {
6184 report_mount_progress(i
, count
);
6186 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
6187 B_FALSE
, options
) != 0)
6189 zfs_close(dslist
[i
]);
6193 } else if (argc
== 0) {
6194 struct mnttab entry
;
6196 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
6197 (void) fprintf(stderr
, gettext("missing filesystem "
6198 "argument (specify -a for all)\n"));
6203 * When mount is given no arguments, go through /etc/mnttab and
6204 * display any active ZFS mounts. We hide any snapshots, since
6205 * they are controlled automatically.
6207 rewind(mnttab_file
);
6208 while (getmntent(mnttab_file
, &entry
) == 0) {
6209 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
6210 strchr(entry
.mnt_special
, '@') != NULL
)
6213 (void) printf("%-30s %s\n", entry
.mnt_special
,
6221 (void) fprintf(stderr
,
6222 gettext("too many arguments\n"));
6226 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6227 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6230 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
6240 * zfs mount -a [nfs]
6241 * zfs mount filesystem
6243 * Mount all filesystems, or mount the given filesystem.
6246 zfs_do_mount(int argc
, char **argv
)
6248 return (share_mount(OP_MOUNT
, argc
, argv
));
6252 * zfs share -a [nfs | smb]
6253 * zfs share filesystem
6255 * Share all filesystems, or share the given filesystem.
6258 zfs_do_share(int argc
, char **argv
)
6260 return (share_mount(OP_SHARE
, argc
, argv
));
6263 typedef struct unshare_unmount_node
{
6264 zfs_handle_t
*un_zhp
;
6266 uu_avl_node_t un_avlnode
;
6267 } unshare_unmount_node_t
;
6271 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
6273 const unshare_unmount_node_t
*l
= larg
;
6274 const unshare_unmount_node_t
*r
= rarg
;
6276 return (strcmp(l
->un_mountp
, r
->un_mountp
));
6280 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6281 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6282 * and unmount it appropriately.
6285 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
6289 struct stat64 statbuf
;
6290 struct extmnttab entry
;
6291 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
6295 * Search for the path in /etc/mnttab. Rather than looking for the
6296 * specific path, which can be fooled by non-standard paths (i.e. ".."
6297 * or "//"), we stat() the path and search for the corresponding
6298 * (major,minor) device pair.
6300 if (stat64(path
, &statbuf
) != 0) {
6301 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6302 cmdname
, path
, strerror(errno
));
6305 path_inode
= statbuf
.st_ino
;
6308 * Search for the given (major,minor) pair in the mount table.
6310 rewind(mnttab_file
);
6311 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
6312 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
6313 entry
.mnt_minor
== minor(statbuf
.st_dev
))
6317 if (op
== OP_SHARE
) {
6318 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
6319 "currently mounted\n"), cmdname
, path
);
6322 (void) fprintf(stderr
, gettext("warning: %s not in mnttab\n"),
6324 if ((ret
= umount2(path
, flags
)) != 0)
6325 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
6330 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
6331 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
6332 "filesystem\n"), cmdname
, path
);
6336 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6337 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6341 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
6342 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6343 cmdname
, path
, strerror(errno
));
6345 } else if (statbuf
.st_ino
!= path_inode
) {
6346 (void) fprintf(stderr
, gettext("cannot "
6347 "%s '%s': not a mountpoint\n"), cmdname
, path
);
6351 if (op
== OP_SHARE
) {
6352 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6353 char smbshare_prop
[ZFS_MAXPROPLEN
];
6355 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
6356 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6357 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
6358 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6360 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6361 strcmp(smbshare_prop
, "off") == 0) {
6362 (void) fprintf(stderr
, gettext("cannot unshare "
6363 "'%s': legacy share\n"), path
);
6364 (void) fprintf(stderr
, gettext("use "
6365 "unshare(1M) to unshare this filesystem\n"));
6366 } else if (!zfs_is_shared(zhp
)) {
6367 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6368 "not currently shared\n"), path
);
6370 ret
= zfs_unshareall_bypath(zhp
, path
);
6373 char mtpt_prop
[ZFS_MAXPROPLEN
];
6375 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6376 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6379 ret
= zfs_unmount(zhp
, NULL
, flags
);
6380 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6381 (void) fprintf(stderr
, gettext("cannot unmount "
6382 "'%s': legacy mountpoint\n"),
6384 (void) fprintf(stderr
, gettext("use umount(1M) "
6385 "to unmount this filesystem\n"));
6387 ret
= zfs_unmountall(zhp
, flags
);
6398 * Generic callback for unsharing or unmounting a filesystem.
6401 unshare_unmount(int op
, int argc
, char **argv
)
6408 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6409 char sharesmb
[ZFS_MAXPROPLEN
];
6412 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
6421 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6432 * We could make use of zfs_for_each() to walk all datasets in
6433 * the system, but this would be very inefficient, especially
6434 * since we would have to linearly search /etc/mnttab for each
6435 * one. Instead, do one pass through /etc/mnttab looking for
6436 * zfs entries and call zfs_unmount() for each one.
6438 * Things get a little tricky if the administrator has created
6439 * mountpoints beneath other ZFS filesystems. In this case, we
6440 * have to unmount the deepest filesystems first. To accomplish
6441 * this, we place all the mountpoints in an AVL tree sorted by
6442 * the special type (dataset name), and walk the result in
6443 * reverse to make sure to get any snapshots first.
6445 struct mnttab entry
;
6446 uu_avl_pool_t
*pool
;
6447 uu_avl_t
*tree
= NULL
;
6448 unshare_unmount_node_t
*node
;
6450 uu_avl_walk_t
*walk
;
6453 (void) fprintf(stderr
, gettext("too many arguments\n"));
6457 if (((pool
= uu_avl_pool_create("unmount_pool",
6458 sizeof (unshare_unmount_node_t
),
6459 offsetof(unshare_unmount_node_t
, un_avlnode
),
6460 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6461 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6464 rewind(mnttab_file
);
6465 while (getmntent(mnttab_file
, &entry
) == 0) {
6467 /* ignore non-ZFS entries */
6468 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6471 /* ignore snapshots */
6472 if (strchr(entry
.mnt_special
, '@') != NULL
)
6475 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6476 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6482 * Ignore datasets that are excluded/restricted by
6485 if (zpool_skip_pool(zfs_get_pool_name(zhp
))) {
6492 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6494 sizeof (nfs_mnt_prop
),
6495 NULL
, NULL
, 0, B_FALSE
) == 0);
6496 if (strcmp(nfs_mnt_prop
, "off") != 0)
6498 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6500 sizeof (nfs_mnt_prop
),
6501 NULL
, NULL
, 0, B_FALSE
) == 0);
6502 if (strcmp(nfs_mnt_prop
, "off") == 0)
6506 /* Ignore legacy mounts */
6507 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6509 sizeof (nfs_mnt_prop
),
6510 NULL
, NULL
, 0, B_FALSE
) == 0);
6511 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6513 /* Ignore canmount=noauto mounts */
6514 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6515 ZFS_CANMOUNT_NOAUTO
)
6521 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6523 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6525 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6527 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6528 uu_avl_insert(tree
, node
, idx
);
6530 zfs_close(node
->un_zhp
);
6531 free(node
->un_mountp
);
6537 * Walk the AVL tree in reverse, unmounting each filesystem and
6538 * removing it from the AVL tree in the process.
6540 if ((walk
= uu_avl_walk_start(tree
,
6541 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6544 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6545 uu_avl_remove(tree
, node
);
6549 if (zfs_unshareall_bypath(node
->un_zhp
,
6550 node
->un_mountp
) != 0)
6555 if (zfs_unmount(node
->un_zhp
,
6556 node
->un_mountp
, flags
) != 0)
6561 zfs_close(node
->un_zhp
);
6562 free(node
->un_mountp
);
6566 uu_avl_walk_end(walk
);
6567 uu_avl_destroy(tree
);
6568 uu_avl_pool_destroy(pool
);
6573 (void) fprintf(stderr
,
6574 gettext("missing filesystem argument\n"));
6576 (void) fprintf(stderr
,
6577 gettext("too many arguments\n"));
6582 * We have an argument, but it may be a full path or a ZFS
6583 * filesystem. Pass full paths off to unmount_path() (shared by
6584 * manual_unmount), otherwise open the filesystem and pass to
6587 if (argv
[0][0] == '/')
6588 return (unshare_unmount_path(op
, argv
[0],
6591 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6592 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6595 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
6596 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
6597 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
6598 NULL
, 0, B_FALSE
) == 0);
6602 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6604 sizeof (nfs_mnt_prop
),
6605 NULL
, NULL
, 0, B_FALSE
) == 0);
6606 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6607 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
6610 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6611 strcmp(sharesmb
, "off") == 0) {
6612 (void) fprintf(stderr
, gettext("cannot "
6613 "unshare '%s': legacy share\n"),
6615 (void) fprintf(stderr
, gettext("use "
6616 "unshare(1M) to unshare this "
6619 } else if (!zfs_is_shared(zhp
)) {
6620 (void) fprintf(stderr
, gettext("cannot "
6621 "unshare '%s': not currently "
6622 "shared\n"), zfs_get_name(zhp
));
6624 } else if (zfs_unshareall(zhp
) != 0) {
6630 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
6631 (void) fprintf(stderr
, gettext("cannot "
6632 "unmount '%s': legacy "
6633 "mountpoint\n"), zfs_get_name(zhp
));
6634 (void) fprintf(stderr
, gettext("use "
6635 "umount(1M) to unmount this "
6638 } else if (!zfs_is_mounted(zhp
, NULL
)) {
6639 (void) fprintf(stderr
, gettext("cannot "
6640 "unmount '%s': not currently "
6644 } else if (zfs_unmountall(zhp
, flags
) != 0) {
6658 * zfs unmount filesystem
6660 * Unmount all filesystems, or a specific ZFS filesystem.
6663 zfs_do_unmount(int argc
, char **argv
)
6665 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
6670 * zfs unshare filesystem
6672 * Unshare all filesystems, or a specific ZFS filesystem.
6675 zfs_do_unshare(int argc
, char **argv
)
6677 return (unshare_unmount(OP_SHARE
, argc
, argv
));
6681 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6682 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6685 manual_mount(int argc
, char **argv
)
6688 char mountpoint
[ZFS_MAXPROPLEN
];
6689 char mntopts
[MNT_LINE_MAX
] = { '\0' };
6693 char *dataset
, *path
;
6696 while ((c
= getopt(argc
, argv
, ":mo:O")) != -1) {
6699 (void) strlcpy(mntopts
, optarg
, sizeof (mntopts
));
6702 flags
|= MS_OVERLAY
;
6705 flags
|= MS_NOMNTTAB
;
6708 (void) fprintf(stderr
, gettext("missing argument for "
6709 "'%c' option\n"), optopt
);
6713 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6715 (void) fprintf(stderr
, gettext("usage: mount [-o opts] "
6724 /* check that we only have two arguments */
6727 (void) fprintf(stderr
, gettext("missing dataset "
6730 (void) fprintf(stderr
,
6731 gettext("missing mountpoint argument\n"));
6733 (void) fprintf(stderr
, gettext("too many arguments\n"));
6734 (void) fprintf(stderr
, "usage: mount <dataset> <mountpoint>\n");
6741 /* try to open the dataset */
6742 if ((zhp
= zfs_open(g_zfs
, dataset
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6745 (void) zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
6746 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
);
6748 /* check for legacy mountpoint and complain appropriately */
6750 if (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) == 0) {
6751 if (mount(dataset
, path
, MS_OPTIONSTR
| flags
, MNTTYPE_ZFS
,
6752 NULL
, 0, mntopts
, sizeof (mntopts
)) != 0) {
6753 (void) fprintf(stderr
, gettext("mount failed: %s\n"),
6758 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
6759 "mounted using 'mount -F zfs'\n"), dataset
);
6760 (void) fprintf(stderr
, gettext("Use 'zfs set mountpoint=%s' "
6761 "instead.\n"), path
);
6762 (void) fprintf(stderr
, gettext("If you must use 'mount -F zfs' "
6763 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
6764 (void) fprintf(stderr
, gettext("See zfs(1M) for more "
6773 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6774 * unmounts of non-legacy filesystems, as this is the dominant administrative
6778 manual_unmount(int argc
, char **argv
)
6784 while ((c
= getopt(argc
, argv
, "f")) != -1) {
6790 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6792 (void) fprintf(stderr
, gettext("usage: unmount [-f] "
6801 /* check arguments */
6804 (void) fprintf(stderr
, gettext("missing path "
6807 (void) fprintf(stderr
, gettext("too many arguments\n"));
6808 (void) fprintf(stderr
, gettext("usage: unmount [-f] <path>\n"));
6812 return (unshare_unmount_path(OP_MOUNT
, argv
[0], flags
, B_TRUE
));
6816 find_command_idx(char *command
, int *idx
)
6820 for (i
= 0; i
< NCOMMAND
; i
++) {
6821 if (command_table
[i
].name
== NULL
)
6824 if (strcmp(command
, command_table
[i
].name
) == 0) {
6833 zfs_do_diff(int argc
, char **argv
)
6837 char *tosnap
= NULL
;
6838 char *fromsnap
= NULL
;
6843 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
6846 flags
|= ZFS_DIFF_CLASSIFY
;
6849 flags
|= ZFS_DIFF_PARSEABLE
;
6852 flags
|= ZFS_DIFF_TIMESTAMP
;
6855 (void) fprintf(stderr
,
6856 gettext("invalid option '%c'\n"), optopt
);
6865 (void) fprintf(stderr
,
6866 gettext("must provide at least one snapshot name\n"));
6871 (void) fprintf(stderr
, gettext("too many arguments\n"));
6876 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
6879 if (*fromsnap
!= '@')
6880 copy
= strdup(fromsnap
);
6882 copy
= strdup(tosnap
);
6886 if ((atp
= strchr(copy
, '@')) != NULL
)
6889 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6895 * Ignore SIGPIPE so that the library can give us
6896 * information on any failure
6898 (void) sigignore(SIGPIPE
);
6900 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
6908 zfs_do_remap(int argc
, char **argv
)
6913 (void) fprintf(stderr
, gettext("wrong number of arguments\n"));
6918 err
= zfs_remap_indirects(g_zfs
, fsname
);
6924 * zfs bookmark <fs@snap> <fs#bmark>
6926 * Creates a bookmark with the given name from the given snapshot.
6929 zfs_do_bookmark(int argc
, char **argv
)
6931 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
6938 while ((c
= getopt(argc
, argv
, "")) != -1) {
6941 (void) fprintf(stderr
,
6942 gettext("invalid option '%c'\n"), optopt
);
6950 /* check number of arguments */
6952 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
6956 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
6960 if (strchr(argv
[1], '#') == NULL
) {
6961 (void) fprintf(stderr
,
6962 gettext("invalid bookmark name '%s' -- "
6963 "must contain a '#'\n"), argv
[1]);
6967 if (argv
[0][0] == '@') {
6969 * Snapshot name begins with @.
6970 * Default to same fs as bookmark.
6972 (void) strncpy(snapname
, argv
[1], sizeof (snapname
));
6973 *strchr(snapname
, '#') = '\0';
6974 (void) strlcat(snapname
, argv
[0], sizeof (snapname
));
6976 (void) strncpy(snapname
, argv
[0], sizeof (snapname
));
6978 zhp
= zfs_open(g_zfs
, snapname
, ZFS_TYPE_SNAPSHOT
);
6984 nvl
= fnvlist_alloc();
6985 fnvlist_add_string(nvl
, argv
[1], snapname
);
6986 ret
= lzc_bookmark(nvl
, NULL
);
6990 const char *err_msg
;
6993 (void) snprintf(errbuf
, sizeof (errbuf
),
6994 dgettext(TEXT_DOMAIN
,
6995 "cannot create bookmark '%s'"), argv
[1]);
6999 err_msg
= "bookmark is in a different pool";
7002 err_msg
= "bookmark exists";
7005 err_msg
= "invalid argument";
7008 err_msg
= "bookmark feature not enabled";
7011 err_msg
= "out of space";
7014 err_msg
= "unknown error";
7017 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
7018 dgettext(TEXT_DOMAIN
, err_msg
));
7029 zfs_do_channel_program(int argc
, char **argv
)
7033 char *progbuf
, *filename
, *poolname
;
7034 size_t progsize
, progread
;
7036 uint64_t instrlimit
= ZCP_DEFAULT_INSTRLIMIT
;
7037 uint64_t memlimit
= ZCP_DEFAULT_MEMLIMIT
;
7038 boolean_t sync_flag
= B_TRUE
;
7039 zpool_handle_t
*zhp
;
7043 (c
= getopt(argc
, argv
, "nt:(instr-limit)m:(memory-limit)"))) {
7051 arg
= strtoull(optarg
, &endp
, 0);
7052 if (errno
!= 0 || *endp
!= '\0') {
7053 (void) fprintf(stderr
, gettext(
7055 "'%s': expected integer\n"), optarg
);
7060 if (arg
> ZCP_MAX_INSTRLIMIT
|| arg
== 0) {
7061 (void) fprintf(stderr
, gettext(
7062 "Invalid instruction limit: "
7069 ASSERT3U(c
, ==, 'm');
7070 if (arg
> ZCP_MAX_MEMLIMIT
|| arg
== 0) {
7071 (void) fprintf(stderr
, gettext(
7072 "Invalid memory limit: "
7082 sync_flag
= B_FALSE
;
7086 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7096 (void) fprintf(stderr
,
7097 gettext("invalid number of arguments\n"));
7103 if (strcmp(filename
, "-") == 0) {
7105 filename
= "standard input";
7106 } else if ((fd
= open(filename
, O_RDONLY
)) < 0) {
7107 (void) fprintf(stderr
, gettext("cannot open '%s': %s\n"),
7108 filename
, strerror(errno
));
7112 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
7113 (void) fprintf(stderr
, gettext("cannot open pool '%s'"),
7120 * Read in the channel program, expanding the program buffer as
7125 progbuf
= safe_malloc(progsize
);
7127 ret
= read(fd
, progbuf
+ progread
, progsize
- progread
);
7129 if (progread
== progsize
&& ret
> 0) {
7131 progbuf
= safe_realloc(progbuf
, progsize
);
7139 (void) fprintf(stderr
,
7140 gettext("cannot read '%s': %s\n"),
7141 filename
, strerror(errno
));
7144 progbuf
[progread
] = '\0';
7147 * Any remaining arguments are passed as arguments to the lua script as
7150 * "argv" -> [ "arg 1", ... "arg n" ],
7153 nvlist_t
*argnvl
= fnvlist_alloc();
7154 fnvlist_add_string_array(argnvl
, ZCP_ARG_CLIARGV
, argv
+ 2, argc
- 2);
7157 ret
= lzc_channel_program(poolname
, progbuf
,
7158 instrlimit
, memlimit
, argnvl
, &outnvl
);
7160 ret
= lzc_channel_program_nosync(poolname
, progbuf
,
7161 instrlimit
, memlimit
, argnvl
, &outnvl
);
7166 * On error, report the error message handed back by lua if one
7167 * exists. Otherwise, generate an appropriate error message,
7168 * falling back on strerror() for an unexpected return code.
7170 char *errstring
= NULL
;
7171 if (nvlist_exists(outnvl
, ZCP_RET_ERROR
)) {
7172 (void) nvlist_lookup_string(outnvl
,
7173 ZCP_RET_ERROR
, &errstring
);
7174 if (errstring
== NULL
)
7175 errstring
= strerror(ret
);
7180 "Invalid instruction or memory limit.";
7183 errstring
= "Return value too large.";
7186 errstring
= "Memory limit exhausted.";
7189 errstring
= "Timed out.";
7192 errstring
= "Permission denied. Channel "
7193 "programs must be run as root.";
7196 errstring
= strerror(ret
);
7199 (void) fprintf(stderr
,
7200 gettext("Channel program execution failed:\n%s\n"),
7203 (void) printf("Channel program fully executed ");
7204 if (nvlist_empty(outnvl
)) {
7205 (void) printf("with no return value.\n");
7207 (void) printf("with return value:\n");
7208 dump_nvlist(outnvl
, 4);
7213 fnvlist_free(outnvl
);
7214 fnvlist_free(argnvl
);
7223 main(int argc
, char **argv
)
7230 (void) setlocale(LC_ALL
, "");
7231 (void) textdomain(TEXT_DOMAIN
);
7235 if ((g_zfs
= libzfs_init()) == NULL
) {
7236 (void) fprintf(stderr
, gettext("internal error: failed to "
7237 "initialize ZFS library\n"));
7241 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
7243 libzfs_print_on_error(g_zfs
, B_TRUE
);
7245 if ((mnttab_file
= fopen(MNTTAB
, "r")) == NULL
) {
7246 (void) fprintf(stderr
, gettext("internal error: unable to "
7247 "open %s\n"), MNTTAB
);
7252 * This command also doubles as the /etc/fs mount and unmount program.
7253 * Determine if we should take this behavior based on argv[0].
7255 progname
= basename(argv
[0]);
7256 if (strcmp(progname
, "mount") == 0) {
7257 ret
= manual_mount(argc
, argv
);
7258 } else if (strcmp(progname
, "umount") == 0) {
7259 ret
= manual_unmount(argc
, argv
);
7262 * Make sure the user has specified some command.
7265 (void) fprintf(stderr
, gettext("missing command\n"));
7272 * The 'umount' command is an alias for 'unmount'
7274 if (strcmp(cmdname
, "umount") == 0)
7275 cmdname
= "unmount";
7278 * The 'recv' command is an alias for 'receive'
7280 if (strcmp(cmdname
, "recv") == 0)
7281 cmdname
= "receive";
7284 * The 'snap' command is an alias for 'snapshot'
7286 if (strcmp(cmdname
, "snap") == 0)
7287 cmdname
= "snapshot";
7292 if (strcmp(cmdname
, "-?") == 0)
7296 * Run the appropriate command.
7298 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
7299 if (find_command_idx(cmdname
, &i
) == 0) {
7300 current_command
= &command_table
[i
];
7301 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
7302 } else if (strchr(cmdname
, '=') != NULL
) {
7303 verify(find_command_idx("set", &i
) == 0);
7304 current_command
= &command_table
[i
];
7305 ret
= command_table
[i
].func(argc
, argv
);
7307 (void) fprintf(stderr
, gettext("unrecognized "
7308 "command '%s'\n"), cmdname
);
7311 libzfs_mnttab_cache(g_zfs
, B_FALSE
);
7314 (void) fclose(mnttab_file
);
7316 if (ret
== 0 && log_history
)
7317 (void) zpool_log_history(g_zfs
, history_str
);
7322 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7323 * for the purposes of running ::findleaks.
7325 if (getenv("ZFS_ABORT") != NULL
) {
7326 (void) printf("dumping core by request\n");