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_channel_program(int argc
, char **argv
);
113 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
118 _umem_debug_init(void)
120 return ("default,verbose"); /* $UMEM_DEBUG setting */
124 _umem_logging_init(void)
126 return ("fail,contents"); /* $UMEM_LOGGING setting */
158 HELP_CHANNEL_PROGRAM
,
161 typedef struct zfs_command
{
163 int (*func
)(int argc
, char **argv
);
168 * Master command table. Each ZFS command has a name, associated function, and
169 * usage message. The usage messages need to be internationalized, so we have
170 * to have a function to return the usage message based on a command index.
172 * These commands are organized according to how they are displayed in the usage
173 * message. An empty command (one with a NULL name) indicates an empty line in
174 * the generic usage message.
176 static zfs_command_t command_table
[] = {
177 { "create", zfs_do_create
, HELP_CREATE
},
178 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
180 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
181 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
182 { "clone", zfs_do_clone
, HELP_CLONE
},
183 { "promote", zfs_do_promote
, HELP_PROMOTE
},
184 { "rename", zfs_do_rename
, HELP_RENAME
},
185 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
186 { "program", zfs_do_channel_program
, HELP_CHANNEL_PROGRAM
},
188 { "list", zfs_do_list
, HELP_LIST
},
190 { "set", zfs_do_set
, HELP_SET
},
191 { "get", zfs_do_get
, HELP_GET
},
192 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
193 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
194 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
195 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
197 { "mount", zfs_do_mount
, HELP_MOUNT
},
198 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
199 { "share", zfs_do_share
, HELP_SHARE
},
200 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
202 { "send", zfs_do_send
, HELP_SEND
},
203 { "receive", zfs_do_receive
, HELP_RECEIVE
},
205 { "allow", zfs_do_allow
, HELP_ALLOW
},
207 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
209 { "hold", zfs_do_hold
, HELP_HOLD
},
210 { "holds", zfs_do_holds
, HELP_HOLDS
},
211 { "release", zfs_do_release
, HELP_RELEASE
},
212 { "diff", zfs_do_diff
, HELP_DIFF
},
215 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
217 zfs_command_t
*current_command
;
220 get_usage(zfs_help_t idx
)
224 return (gettext("\tclone [-p] [-o property=value] ... "
225 "<snapshot> <filesystem|volume>\n"));
227 return (gettext("\tcreate [-p] [-o property=value] ... "
229 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
230 "-V <size> <volume>\n"));
232 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
233 "\tdestroy [-dnpRrv] "
234 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
235 "\tdestroy <filesystem|volume>#<bookmark>\n"));
237 return (gettext("\tget [-rHp] [-d max] "
238 "[-o \"all\" | field[,...]]\n"
239 "\t [-t type[,...]] [-s source[,...]]\n"
240 "\t <\"all\" | property[,...]> "
241 "[filesystem|volume|snapshot|bookmark] ...\n"));
243 return (gettext("\tinherit [-rS] <property> "
244 "<filesystem|volume|snapshot> ...\n"));
246 return (gettext("\tupgrade [-v]\n"
247 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
249 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
250 "[-s property]...\n\t [-S property]... [-t type[,...]] "
251 "[filesystem|volume|snapshot] ...\n"));
253 return (gettext("\tmount\n"
254 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
256 return (gettext("\tpromote <clone-filesystem>\n"));
258 return (gettext("\treceive [-vnsFu] <filesystem|volume|"
260 "\treceive [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
262 "\treceive -A <filesystem|volume>\n"));
264 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
265 "<filesystem|volume|snapshot>\n"
266 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
267 "\trename -r <snapshot> <snapshot>\n"));
269 return (gettext("\trollback [-rRf] <snapshot>\n"));
271 return (gettext("\tsend [-DnPpRvLec] [-[iI] snapshot] "
273 "\tsend [-Le] [-i snapshot|bookmark] "
274 "<filesystem|volume|snapshot>\n"
275 "\tsend [-nvPe] -t <receive_resume_token>\n"));
277 return (gettext("\tset <property=value> ... "
278 "<filesystem|volume|snapshot> ...\n"));
280 return (gettext("\tshare <-a | filesystem>\n"));
282 return (gettext("\tsnapshot [-r] [-o property=value] ... "
283 "<filesystem|volume>@<snap> ...\n"));
285 return (gettext("\tunmount [-f] "
286 "<-a | filesystem|mountpoint>\n"));
288 return (gettext("\tunshare "
289 "<-a | filesystem|mountpoint>\n"));
291 return (gettext("\tallow <filesystem|volume>\n"
293 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
294 "\t <filesystem|volume>\n"
295 "\tallow [-ld] -e <perm|@setname>[,...] "
296 "<filesystem|volume>\n"
297 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
298 "\tallow -s @setname <perm|@setname>[,...] "
299 "<filesystem|volume>\n"));
301 return (gettext("\tunallow [-rldug] "
302 "<\"everyone\"|user|group>[,...]\n"
303 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
304 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
305 "<filesystem|volume>\n"
306 "\tunallow [-r] -c [<perm|@setname>[,...]] "
307 "<filesystem|volume>\n"
308 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
309 "<filesystem|volume>\n"));
311 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
313 "\t [-S field] ... [-t type[,...]] "
314 "<filesystem|snapshot>\n"));
315 case HELP_GROUPSPACE
:
316 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
318 "\t [-S field] ... [-t type[,...]] "
319 "<filesystem|snapshot>\n"));
321 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
323 return (gettext("\tholds [-r] <snapshot> ...\n"));
325 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
327 return (gettext("\tdiff [-FHt] <snapshot> "
328 "[snapshot|filesystem]\n"));
330 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
331 case HELP_CHANNEL_PROGRAM
:
332 return (gettext("\tprogram [-t <instruction limit>] "
333 "[-m <memory limit (b)>] <pool> <program file> "
344 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
349 * Utility function to guarantee malloc() success.
353 safe_malloc(size_t size
)
357 if ((data
= calloc(1, size
)) == NULL
)
364 safe_realloc(void *data
, size_t size
)
367 if ((newp
= realloc(data
, size
)) == NULL
) {
376 safe_strdup(char *str
)
378 char *dupstr
= strdup(str
);
387 * Callback routine that will print out information for each of
391 usage_prop_cb(int prop
, void *cb
)
395 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
397 if (zfs_prop_readonly(prop
))
398 (void) fprintf(fp
, " NO ");
400 (void) fprintf(fp
, "YES ");
402 if (zfs_prop_inheritable(prop
))
403 (void) fprintf(fp
, " YES ");
405 (void) fprintf(fp
, " NO ");
407 if (zfs_prop_values(prop
) == NULL
)
408 (void) fprintf(fp
, "-\n");
410 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
416 * Display usage message. If we're inside a command, display only the usage for
417 * that command. Otherwise, iterate over the entire command table and display
418 * a complete usage message.
421 usage(boolean_t requested
)
424 boolean_t show_properties
= B_FALSE
;
425 FILE *fp
= requested
? stdout
: stderr
;
427 if (current_command
== NULL
) {
429 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
431 gettext("where 'command' is one of the following:\n\n"));
433 for (i
= 0; i
< NCOMMAND
; i
++) {
434 if (command_table
[i
].name
== NULL
)
435 (void) fprintf(fp
, "\n");
437 (void) fprintf(fp
, "%s",
438 get_usage(command_table
[i
].usage
));
441 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
442 "pool/[dataset/]*dataset[@name]\n"));
444 (void) fprintf(fp
, gettext("usage:\n"));
445 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
448 if (current_command
!= NULL
&&
449 (strcmp(current_command
->name
, "set") == 0 ||
450 strcmp(current_command
->name
, "get") == 0 ||
451 strcmp(current_command
->name
, "inherit") == 0 ||
452 strcmp(current_command
->name
, "list") == 0))
453 show_properties
= B_TRUE
;
455 if (show_properties
) {
457 gettext("\nThe following properties are supported:\n"));
459 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
460 "PROPERTY", "EDIT", "INHERIT", "VALUES");
462 /* Iterate over all properties */
463 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
466 (void) fprintf(fp
, "\t%-15s ", "userused@...");
467 (void) fprintf(fp
, " NO NO <size>\n");
468 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
469 (void) fprintf(fp
, " NO NO <size>\n");
470 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
471 (void) fprintf(fp
, "YES NO <size> | none\n");
472 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
473 (void) fprintf(fp
, "YES NO <size> | none\n");
474 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
475 (void) fprintf(fp
, " NO NO <size>\n");
477 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
478 "with standard units such as K, M, G, etc.\n"));
479 (void) fprintf(fp
, gettext("\nUser-defined properties can "
480 "be specified by using a name containing a colon (:).\n"));
481 (void) fprintf(fp
, gettext("\nThe {user|group}{used|quota}@ "
482 "properties must be appended with\n"
483 "a user or group specifier of one of these forms:\n"
484 " POSIX name (eg: \"matt\")\n"
485 " POSIX id (eg: \"126829\")\n"
486 " SMB name@domain (eg: \"matt@sun\")\n"
487 " SMB SID (eg: \"S-1-234-567-89\")\n"));
490 gettext("\nFor the property list, run: %s\n"),
493 gettext("\nFor the delegated permission list, run: %s\n"),
494 "zfs allow|unallow");
498 * See comments at end of main().
500 if (getenv("ZFS_ABORT") != NULL
) {
501 (void) printf("dumping core by request\n");
505 exit(requested
? 0 : 2);
509 * Take a property=value argument string and add it to the given nvlist.
510 * Modifies the argument inplace.
513 parseprop(nvlist_t
*props
, char *propname
)
515 char *propval
, *strval
;
517 if ((propval
= strchr(propname
, '=')) == NULL
) {
518 (void) fprintf(stderr
, gettext("missing "
519 "'=' for property=value argument\n"));
524 if (nvlist_lookup_string(props
, propname
, &strval
) == 0) {
525 (void) fprintf(stderr
, gettext("property '%s' "
526 "specified multiple times\n"), propname
);
529 if (nvlist_add_string(props
, propname
, propval
) != 0)
535 parse_depth(char *opt
, int *flags
)
540 depth
= (int)strtol(opt
, &tmp
, 0);
542 (void) fprintf(stderr
,
543 gettext("%s is not an integer\n"), optarg
);
547 (void) fprintf(stderr
,
548 gettext("Depth can not be negative.\n"));
551 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
555 #define PROGRESS_DELAY 2 /* seconds */
557 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";
558 static time_t pt_begin
;
559 static char *pt_header
= NULL
;
560 static boolean_t pt_shown
;
563 start_progress_timer(void)
565 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
570 set_progress_header(char *header
)
572 assert(pt_header
== NULL
);
573 pt_header
= safe_strdup(header
);
575 (void) printf("%s: ", header
);
576 (void) fflush(stdout
);
581 update_progress(char *update
)
583 if (!pt_shown
&& time(NULL
) > pt_begin
) {
584 int len
= strlen(update
);
586 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
588 (void) fflush(stdout
);
590 } else if (pt_shown
) {
591 int len
= strlen(update
);
593 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
594 (void) fflush(stdout
);
599 finish_progress(char *done
)
602 (void) printf("%s\n", done
);
603 (void) fflush(stdout
);
610 * Check if the dataset is mountable and should be automatically mounted.
613 should_auto_mount(zfs_handle_t
*zhp
)
615 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, zfs_get_type(zhp
)))
617 return (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
);
621 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
623 * Given an existing dataset, create a writable copy whose initial contents
624 * are the same as the source. The newly created dataset maintains a
625 * dependency on the original; the original cannot be destroyed so long as
628 * The '-p' flag creates all the non-existing ancestors of the target first.
631 zfs_do_clone(int argc
, char **argv
)
633 zfs_handle_t
*zhp
= NULL
;
634 boolean_t parents
= B_FALSE
;
639 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
643 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
646 if (parseprop(props
, optarg
) != 0)
653 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
662 /* check number of arguments */
664 (void) fprintf(stderr
, gettext("missing source dataset "
669 (void) fprintf(stderr
, gettext("missing target dataset "
674 (void) fprintf(stderr
, gettext("too many arguments\n"));
678 /* open the source dataset */
679 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
682 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
685 * Now create the ancestors of the target dataset. If the
686 * target already exists and '-p' option was used we should not
689 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
692 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
697 ret
= zfs_clone(zhp
, argv
[1], props
);
699 /* create the mountpoint if necessary */
703 clone
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
706 * If the user doesn't want the dataset
707 * automatically mounted, then skip the mount/share
710 if (should_auto_mount(clone
)) {
711 if ((ret
= zfs_mount(clone
, NULL
, 0)) != 0) {
712 (void) fprintf(stderr
, gettext("clone "
713 "successfully created, "
714 "but not mounted\n"));
715 } else if ((ret
= zfs_share(clone
)) != 0) {
716 (void) fprintf(stderr
, gettext("clone "
717 "successfully created, "
718 "but not shared\n"));
739 * zfs create [-p] [-o prop=value] ... fs
740 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
742 * Create a new dataset. This command can be used to create filesystems
743 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
744 * For volumes, the user must specify a size to be used.
746 * The '-s' flag applies only to volumes, and indicates that we should not try
747 * to set the reservation for this volume. By default we set a reservation
748 * equal to the size for any volume. For pools with SPA_VERSION >=
749 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
751 * The '-p' flag creates all the non-existing ancestors of the target first.
754 zfs_do_create(int argc
, char **argv
)
756 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
757 zfs_handle_t
*zhp
= NULL
;
758 uint64_t volsize
= 0;
760 boolean_t noreserve
= B_FALSE
;
761 boolean_t bflag
= B_FALSE
;
762 boolean_t parents
= B_FALSE
;
767 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
771 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
774 type
= ZFS_TYPE_VOLUME
;
775 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
776 (void) fprintf(stderr
, gettext("bad volume "
777 "size '%s': %s\n"), optarg
,
778 libzfs_error_description(g_zfs
));
782 if (nvlist_add_uint64(props
,
783 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
792 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
793 (void) fprintf(stderr
, gettext("bad volume "
794 "block size '%s': %s\n"), optarg
,
795 libzfs_error_description(g_zfs
));
799 if (nvlist_add_uint64(props
,
800 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
805 if (parseprop(props
, optarg
) != 0)
812 (void) fprintf(stderr
, gettext("missing size "
816 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
822 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
823 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
824 "used when creating a volume\n"));
831 /* check number of arguments */
833 (void) fprintf(stderr
, gettext("missing %s argument\n"),
834 zfs_type_to_name(type
));
838 (void) fprintf(stderr
, gettext("too many arguments\n"));
842 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
843 zpool_handle_t
*zpool_handle
;
844 nvlist_t
*real_props
= NULL
;
845 uint64_t spa_version
;
847 zfs_prop_t resv_prop
;
851 if ((p
= strchr(argv
[0], '/')) != NULL
)
853 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
856 if (zpool_handle
== NULL
)
858 spa_version
= zpool_get_prop_int(zpool_handle
,
859 ZPOOL_PROP_VERSION
, NULL
);
860 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
861 resv_prop
= ZFS_PROP_REFRESERVATION
;
863 resv_prop
= ZFS_PROP_RESERVATION
;
865 (void) snprintf(msg
, sizeof (msg
),
866 gettext("cannot create '%s'"), argv
[0]);
867 if (props
&& (real_props
= zfs_valid_proplist(g_zfs
, type
,
868 props
, 0, NULL
, zpool_handle
, msg
)) == NULL
) {
869 zpool_close(zpool_handle
);
872 zpool_close(zpool_handle
);
874 volsize
= zvol_volsize_to_reservation(volsize
, real_props
);
875 nvlist_free(real_props
);
877 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
879 if (nvlist_add_uint64(props
,
880 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
887 if (parents
&& zfs_name_valid(argv
[0], type
)) {
889 * Now create the ancestors of target dataset. If the target
890 * already exists and '-p' option was used we should not
893 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
897 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
902 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
905 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
911 * Mount and/or share the new filesystem as appropriate. We provide a
912 * verbose error message to let the user know that their filesystem was
913 * in fact created, even if we failed to mount or share it.
914 * If the user doesn't want the dataset automatically mounted,
915 * then skip the mount/share step altogether.
917 if (should_auto_mount(zhp
)) {
918 if (zfs_mount(zhp
, NULL
, 0) != 0) {
919 (void) fprintf(stderr
, gettext("filesystem "
920 "successfully created, but not mounted\n"));
922 } else if (zfs_share(zhp
) != 0) {
923 (void) fprintf(stderr
, gettext("filesystem "
924 "successfully created, but not shared\n"));
941 * zfs destroy [-rRf] <fs, vol>
942 * zfs destroy [-rRd] <snap>
944 * -r Recursively destroy all children
945 * -R Recursively destroy all dependents, including clones
946 * -f Force unmounting of any dependents
947 * -d If we can't destroy now, mark for deferred destruction
949 * Destroys the given dataset. By default, it will unmount any filesystems,
950 * and refuse to destroy a dataset that has any dependents. A dependent can
951 * either be a child, or a clone of a child.
953 typedef struct destroy_cbdata
{
956 boolean_t cb_recurse
;
958 boolean_t cb_doclones
;
959 zfs_handle_t
*cb_target
;
960 boolean_t cb_defer_destroy
;
961 boolean_t cb_verbose
;
962 boolean_t cb_parsable
;
965 nvlist_t
*cb_batchedsnaps
;
967 /* first snap in contiguous run */
969 /* previous snap in contiguous run */
977 * Check for any dependents based on the '-r' or '-R' flags.
980 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
982 destroy_cbdata_t
*cbp
= data
;
983 const char *tname
= zfs_get_name(cbp
->cb_target
);
984 const char *name
= zfs_get_name(zhp
);
986 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
987 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
989 * This is a direct descendant, not a clone somewhere else in
996 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
997 "%s has children\n"),
998 zfs_get_name(cbp
->cb_target
),
999 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1000 (void) fprintf(stderr
, gettext("use '-r' to destroy "
1001 "the following datasets:\n"));
1002 cbp
->cb_first
= B_FALSE
;
1003 cbp
->cb_error
= B_TRUE
;
1006 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1009 * This is a clone. We only want to report this if the '-r'
1010 * wasn't specified, or the target is a snapshot.
1012 if (!cbp
->cb_recurse
&&
1013 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
1016 if (cbp
->cb_first
) {
1017 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1018 "%s has dependent clones\n"),
1019 zfs_get_name(cbp
->cb_target
),
1020 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1021 (void) fprintf(stderr
, gettext("use '-R' to destroy "
1022 "the following datasets:\n"));
1023 cbp
->cb_first
= B_FALSE
;
1024 cbp
->cb_error
= B_TRUE
;
1025 cbp
->cb_dryrun
= B_TRUE
;
1028 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1037 destroy_callback(zfs_handle_t
*zhp
, void *data
)
1039 destroy_cbdata_t
*cb
= data
;
1040 const char *name
= zfs_get_name(zhp
);
1042 if (cb
->cb_verbose
) {
1043 if (cb
->cb_parsable
) {
1044 (void) printf("destroy\t%s\n", name
);
1045 } else if (cb
->cb_dryrun
) {
1046 (void) printf(gettext("would destroy %s\n"),
1049 (void) printf(gettext("will destroy %s\n"),
1055 * Ignore pools (which we've already flagged as an error before getting
1058 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1059 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1063 if (cb
->cb_dryrun
) {
1069 * We batch up all contiguous snapshots (even of different
1070 * filesystems) and destroy them with one ioctl. We can't
1071 * simply do all snap deletions and then all fs deletions,
1072 * because we must delete a clone before its origin.
1074 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1075 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1077 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1078 cb
->cb_batchedsnaps
, B_FALSE
);
1079 fnvlist_free(cb
->cb_batchedsnaps
);
1080 cb
->cb_batchedsnaps
= fnvlist_alloc();
1083 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1084 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1095 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1097 destroy_cbdata_t
*cb
= arg
;
1098 const char *name
= zfs_get_name(zhp
);
1101 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1102 if (cb
->cb_firstsnap
== NULL
)
1103 cb
->cb_firstsnap
= strdup(name
);
1104 free(cb
->cb_prevsnap
);
1105 /* this snap continues the current range */
1106 cb
->cb_prevsnap
= strdup(name
);
1107 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1109 if (cb
->cb_verbose
) {
1110 if (cb
->cb_parsable
) {
1111 (void) printf("destroy\t%s\n", name
);
1112 } else if (cb
->cb_dryrun
) {
1113 (void) printf(gettext("would destroy %s\n"),
1116 (void) printf(gettext("will destroy %s\n"),
1120 } else if (cb
->cb_firstsnap
!= NULL
) {
1121 /* end of this range */
1123 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1124 cb
->cb_prevsnap
, &used
);
1125 cb
->cb_snapused
+= used
;
1126 free(cb
->cb_firstsnap
);
1127 cb
->cb_firstsnap
= NULL
;
1128 free(cb
->cb_prevsnap
);
1129 cb
->cb_prevsnap
= NULL
;
1136 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1139 assert(cb
->cb_firstsnap
== NULL
);
1140 assert(cb
->cb_prevsnap
== NULL
);
1141 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1142 if (cb
->cb_firstsnap
!= NULL
) {
1145 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1146 cb
->cb_prevsnap
, &used
);
1148 cb
->cb_snapused
+= used
;
1149 free(cb
->cb_firstsnap
);
1150 cb
->cb_firstsnap
= NULL
;
1151 free(cb
->cb_prevsnap
);
1152 cb
->cb_prevsnap
= NULL
;
1158 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1160 destroy_cbdata_t
*cb
= arg
;
1163 /* Check for clones. */
1164 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1165 cb
->cb_target
= zhp
;
1166 cb
->cb_first
= B_TRUE
;
1167 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1168 destroy_check_dependent
, cb
);
1172 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1180 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1182 destroy_cbdata_t
*cb
= arg
;
1185 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1191 if (cb
->cb_verbose
) {
1192 err
= destroy_print_snapshots(zhp
, cb
);
1198 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1206 destroy_clones(destroy_cbdata_t
*cb
)
1209 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1211 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1212 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1215 boolean_t defer
= cb
->cb_defer_destroy
;
1219 * We can't defer destroy non-snapshots, so set it to
1220 * false while destroying the clones.
1222 cb
->cb_defer_destroy
= B_FALSE
;
1223 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1224 destroy_callback
, cb
);
1225 cb
->cb_defer_destroy
= defer
;
1235 zfs_do_destroy(int argc
, char **argv
)
1237 destroy_cbdata_t cb
= { 0 };
1241 zfs_handle_t
*zhp
= NULL
;
1243 zfs_type_t type
= ZFS_TYPE_DATASET
;
1246 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1249 cb
.cb_verbose
= B_TRUE
;
1252 cb
.cb_verbose
= B_TRUE
;
1253 cb
.cb_parsable
= B_TRUE
;
1256 cb
.cb_dryrun
= B_TRUE
;
1259 cb
.cb_defer_destroy
= B_TRUE
;
1260 type
= ZFS_TYPE_SNAPSHOT
;
1263 cb
.cb_force
= B_TRUE
;
1266 cb
.cb_recurse
= B_TRUE
;
1269 cb
.cb_recurse
= B_TRUE
;
1270 cb
.cb_doclones
= B_TRUE
;
1274 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1283 /* check number of arguments */
1285 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1289 (void) fprintf(stderr
, gettext("too many arguments\n"));
1293 at
= strchr(argv
[0], '@');
1294 pound
= strchr(argv
[0], '#');
1297 /* Build the list of snaps to destroy in cb_nvl. */
1298 cb
.cb_nvl
= fnvlist_alloc();
1301 zhp
= zfs_open(g_zfs
, argv
[0],
1302 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1306 cb
.cb_snapspec
= at
+ 1;
1307 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1313 if (nvlist_empty(cb
.cb_nvl
)) {
1314 (void) fprintf(stderr
, gettext("could not find any "
1315 "snapshots to destroy; check snapshot names.\n"));
1320 if (cb
.cb_verbose
) {
1322 zfs_nicenum(cb
.cb_snapused
, buf
, sizeof (buf
));
1323 if (cb
.cb_parsable
) {
1324 (void) printf("reclaim\t%llu\n",
1326 } else if (cb
.cb_dryrun
) {
1327 (void) printf(gettext("would reclaim %s\n"),
1330 (void) printf(gettext("will reclaim %s\n"),
1335 if (!cb
.cb_dryrun
) {
1336 if (cb
.cb_doclones
) {
1337 cb
.cb_batchedsnaps
= fnvlist_alloc();
1338 err
= destroy_clones(&cb
);
1340 err
= zfs_destroy_snaps_nvl(g_zfs
,
1341 cb
.cb_batchedsnaps
, B_FALSE
);
1349 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1350 cb
.cb_defer_destroy
);
1356 } else if (pound
!= NULL
) {
1361 (void) fprintf(stderr
,
1362 "dryrun is not supported with bookmark\n");
1366 if (cb
.cb_defer_destroy
) {
1367 (void) fprintf(stderr
,
1368 "defer destroy is not supported with bookmark\n");
1372 if (cb
.cb_recurse
) {
1373 (void) fprintf(stderr
,
1374 "recursive is not supported with bookmark\n");
1378 if (!zfs_bookmark_exists(argv
[0])) {
1379 (void) fprintf(stderr
, gettext("bookmark '%s' "
1380 "does not exist.\n"), argv
[0]);
1384 nvl
= fnvlist_alloc();
1385 fnvlist_add_boolean(nvl
, argv
[0]);
1387 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1389 (void) zfs_standard_error(g_zfs
, err
,
1390 "cannot destroy bookmark");
1393 nvlist_free(cb
.cb_nvl
);
1397 /* Open the given dataset */
1398 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1404 * Perform an explicit check for pools before going any further.
1406 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1407 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1408 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1409 "operation does not apply to pools\n"),
1411 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1412 "%s' to destroy all datasets in the pool\n"),
1414 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1415 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1421 * Check for any dependents and/or clones.
1423 cb
.cb_first
= B_TRUE
;
1424 if (!cb
.cb_doclones
&&
1425 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1436 cb
.cb_batchedsnaps
= fnvlist_alloc();
1437 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1444 * Do the real thing. The callback will close the
1445 * handle regardless of whether it succeeds or not.
1447 err
= destroy_callback(zhp
, &cb
);
1450 err
= zfs_destroy_snaps_nvl(g_zfs
,
1451 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1458 fnvlist_free(cb
.cb_batchedsnaps
);
1459 fnvlist_free(cb
.cb_nvl
);
1466 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1469 zfs_get_column_t col
;
1471 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1472 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1473 if (col
== GET_COL_RECVD
)
1479 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1480 * < all | property[,property]... > < fs | snap | vol > ...
1482 * -r recurse over any child datasets
1483 * -H scripted mode. Headers are stripped, and fields are separated
1484 * by tabs instead of spaces.
1485 * -o Set of fields to display. One of "name,property,value,
1486 * received,source". Default is "name,property,value,source".
1487 * "all" is an alias for all five.
1488 * -s Set of sources to allow. One of
1489 * "local,default,inherited,received,temporary,none". Default is
1491 * -p Display values in parsable (literal) format.
1493 * Prints properties for the given datasets. The user can control which
1494 * columns to display as well as which property types to allow.
1498 * Invoked to display the properties for a single dataset.
1501 get_callback(zfs_handle_t
*zhp
, void *data
)
1503 char buf
[ZFS_MAXPROPLEN
];
1504 char rbuf
[ZFS_MAXPROPLEN
];
1505 zprop_source_t sourcetype
;
1506 char source
[ZFS_MAX_DATASET_NAME_LEN
];
1507 zprop_get_cbdata_t
*cbp
= data
;
1508 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1509 zprop_list_t
*pl
= cbp
->cb_proplist
;
1513 boolean_t received
= is_recvd_column(cbp
);
1515 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1516 char *recvdval
= NULL
;
1518 * Skip the special fake placeholder. This will also skip over
1519 * the name property when 'all' is specified.
1521 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1522 pl
== cbp
->cb_proplist
)
1525 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1526 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1527 sizeof (buf
), &sourcetype
, source
,
1529 cbp
->cb_literal
) != 0) {
1532 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1533 ZFS_TYPE_DATASET
)) {
1534 (void) fprintf(stderr
,
1535 gettext("No such property '%s'\n"),
1536 zfs_prop_to_name(pl
->pl_prop
));
1539 sourcetype
= ZPROP_SRC_NONE
;
1540 (void) strlcpy(buf
, "-", sizeof (buf
));
1543 if (received
&& (zfs_prop_get_recvd(zhp
,
1544 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1545 cbp
->cb_literal
) == 0))
1548 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1549 zfs_prop_to_name(pl
->pl_prop
),
1550 buf
, sourcetype
, source
, recvdval
);
1551 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1552 sourcetype
= ZPROP_SRC_LOCAL
;
1554 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1555 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1556 sourcetype
= ZPROP_SRC_NONE
;
1557 (void) strlcpy(buf
, "-", sizeof (buf
));
1560 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1561 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1562 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1563 sourcetype
= ZPROP_SRC_LOCAL
;
1565 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1566 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1567 sourcetype
= ZPROP_SRC_NONE
;
1568 (void) strlcpy(buf
, "-", sizeof (buf
));
1571 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1572 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1574 if (nvlist_lookup_nvlist(user_props
,
1575 pl
->pl_user_prop
, &propval
) != 0) {
1578 sourcetype
= ZPROP_SRC_NONE
;
1581 verify(nvlist_lookup_string(propval
,
1582 ZPROP_VALUE
, &strval
) == 0);
1583 verify(nvlist_lookup_string(propval
,
1584 ZPROP_SOURCE
, &sourceval
) == 0);
1586 if (strcmp(sourceval
,
1587 zfs_get_name(zhp
)) == 0) {
1588 sourcetype
= ZPROP_SRC_LOCAL
;
1589 } else if (strcmp(sourceval
,
1590 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1591 sourcetype
= ZPROP_SRC_RECEIVED
;
1593 sourcetype
= ZPROP_SRC_INHERITED
;
1594 (void) strlcpy(source
,
1595 sourceval
, sizeof (source
));
1599 if (received
&& (zfs_prop_get_recvd(zhp
,
1600 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1601 cbp
->cb_literal
) == 0))
1604 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1605 pl
->pl_user_prop
, strval
, sourcetype
,
1614 zfs_do_get(int argc
, char **argv
)
1616 zprop_get_cbdata_t cb
= { 0 };
1617 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1618 int types
= ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
;
1619 char *value
, *fields
;
1622 zprop_list_t fake_name
= { 0 };
1625 * Set up default columns and sources.
1627 cb
.cb_sources
= ZPROP_SRC_ALL
;
1628 cb
.cb_columns
[0] = GET_COL_NAME
;
1629 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1630 cb
.cb_columns
[2] = GET_COL_VALUE
;
1631 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1632 cb
.cb_type
= ZFS_TYPE_DATASET
;
1635 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1638 cb
.cb_literal
= B_TRUE
;
1641 limit
= parse_depth(optarg
, &flags
);
1644 flags
|= ZFS_ITER_RECURSE
;
1647 cb
.cb_scripted
= B_TRUE
;
1650 (void) fprintf(stderr
, gettext("missing argument for "
1651 "'%c' option\n"), optopt
);
1656 * Process the set of columns to display. We zero out
1657 * the structure to give us a blank slate.
1659 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1661 while (*optarg
!= '\0') {
1662 static char *col_subopts
[] =
1663 { "name", "property", "value", "received",
1664 "source", "all", NULL
};
1666 if (i
== ZFS_GET_NCOLS
) {
1667 (void) fprintf(stderr
, gettext("too "
1668 "many fields given to -o "
1673 switch (getsubopt(&optarg
, col_subopts
,
1676 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1679 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1682 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1685 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1686 flags
|= ZFS_ITER_RECVD_PROPS
;
1689 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1693 (void) fprintf(stderr
,
1694 gettext("\"all\" conflicts "
1695 "with specific fields "
1696 "given to -o option\n"));
1699 cb
.cb_columns
[0] = GET_COL_NAME
;
1700 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1701 cb
.cb_columns
[2] = GET_COL_VALUE
;
1702 cb
.cb_columns
[3] = GET_COL_RECVD
;
1703 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1704 flags
|= ZFS_ITER_RECVD_PROPS
;
1708 (void) fprintf(stderr
,
1709 gettext("invalid column name "
1718 while (*optarg
!= '\0') {
1719 static char *source_subopts
[] = {
1720 "local", "default", "inherited",
1721 "received", "temporary", "none",
1724 switch (getsubopt(&optarg
, source_subopts
,
1727 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1730 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1733 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1736 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1739 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1742 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1745 (void) fprintf(stderr
,
1746 gettext("invalid source "
1755 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1756 while (*optarg
!= '\0') {
1757 static char *type_subopts
[] = { "filesystem",
1758 "volume", "snapshot", "bookmark",
1761 switch (getsubopt(&optarg
, type_subopts
,
1764 types
|= ZFS_TYPE_FILESYSTEM
;
1767 types
|= ZFS_TYPE_VOLUME
;
1770 types
|= ZFS_TYPE_SNAPSHOT
;
1773 types
|= ZFS_TYPE_BOOKMARK
;
1776 types
= ZFS_TYPE_DATASET
|
1781 (void) fprintf(stderr
,
1782 gettext("invalid type '%s'\n"),
1790 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1800 (void) fprintf(stderr
, gettext("missing property "
1807 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1815 * As part of zfs_expand_proplist(), we keep track of the maximum column
1816 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1817 * need to know the maximum name length. However, the user likely did
1818 * not specify 'name' as one of the properties to fetch, so we need to
1819 * make sure we always include at least this property for
1820 * print_get_headers() to work properly.
1822 if (cb
.cb_proplist
!= NULL
) {
1823 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1824 fake_name
.pl_width
= strlen(gettext("NAME"));
1825 fake_name
.pl_next
= cb
.cb_proplist
;
1826 cb
.cb_proplist
= &fake_name
;
1829 cb
.cb_first
= B_TRUE
;
1831 /* run for each object */
1832 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
1833 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1835 if (cb
.cb_proplist
== &fake_name
)
1836 zprop_free_list(fake_name
.pl_next
);
1838 zprop_free_list(cb
.cb_proplist
);
1844 * inherit [-rS] <property> <fs|vol> ...
1846 * -r Recurse over all children
1847 * -S Revert to received value, if any
1849 * For each dataset specified on the command line, inherit the given property
1850 * from its parent. Inheriting a property at the pool level will cause it to
1851 * use the default value. The '-r' flag will recurse over all children, and is
1852 * useful for setting a property on a hierarchy-wide basis, regardless of any
1853 * local modifications for each dataset.
1856 typedef struct inherit_cbdata
{
1857 const char *cb_propname
;
1858 boolean_t cb_received
;
1862 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1864 inherit_cbdata_t
*cb
= data
;
1865 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1868 * If we're doing it recursively, then ignore properties that
1869 * are not valid for this type of dataset.
1871 if (prop
!= ZPROP_INVAL
&&
1872 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
)))
1875 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1879 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1881 inherit_cbdata_t
*cb
= data
;
1883 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1887 zfs_do_inherit(int argc
, char **argv
)
1891 inherit_cbdata_t cb
= { 0 };
1895 boolean_t received
= B_FALSE
;
1898 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1901 flags
|= ZFS_ITER_RECURSE
;
1908 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1917 /* check number of arguments */
1919 (void) fprintf(stderr
, gettext("missing property argument\n"));
1923 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1931 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1932 if (zfs_prop_readonly(prop
)) {
1933 (void) fprintf(stderr
, gettext(
1934 "%s property is read-only\n"),
1938 if (!zfs_prop_inheritable(prop
) && !received
) {
1939 (void) fprintf(stderr
, gettext("'%s' property cannot "
1940 "be inherited\n"), propname
);
1941 if (prop
== ZFS_PROP_QUOTA
||
1942 prop
== ZFS_PROP_RESERVATION
||
1943 prop
== ZFS_PROP_REFQUOTA
||
1944 prop
== ZFS_PROP_REFRESERVATION
) {
1945 (void) fprintf(stderr
, gettext("use 'zfs set "
1946 "%s=none' to clear\n"), propname
);
1947 (void) fprintf(stderr
, gettext("use 'zfs "
1948 "inherit -S %s' to revert to received "
1949 "value\n"), propname
);
1953 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1954 prop
== ZFS_PROP_VERSION
)) {
1955 (void) fprintf(stderr
, gettext("'%s' property cannot "
1956 "be reverted to a received value\n"), propname
);
1959 } else if (!zfs_prop_user(propname
)) {
1960 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1965 cb
.cb_propname
= propname
;
1966 cb
.cb_received
= received
;
1968 if (flags
& ZFS_ITER_RECURSE
) {
1969 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1970 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1972 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1973 NULL
, NULL
, 0, inherit_cb
, &cb
);
1979 typedef struct upgrade_cbdata
{
1980 uint64_t cb_numupgraded
;
1981 uint64_t cb_numsamegraded
;
1982 uint64_t cb_numfailed
;
1983 uint64_t cb_version
;
1985 boolean_t cb_foundone
;
1986 char cb_lastfs
[ZFS_MAX_DATASET_NAME_LEN
];
1990 same_pool(zfs_handle_t
*zhp
, const char *name
)
1992 int len1
= strcspn(name
, "/@");
1993 const char *zhname
= zfs_get_name(zhp
);
1994 int len2
= strcspn(zhname
, "/@");
1998 return (strncmp(name
, zhname
, len1
) == 0);
2002 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
2004 upgrade_cbdata_t
*cb
= data
;
2005 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2007 /* list if it's old/new */
2008 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
2009 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
2012 str
= gettext("The following filesystems are "
2013 "formatted using a newer software version and\n"
2014 "cannot be accessed on the current system.\n\n");
2016 str
= gettext("The following filesystems are "
2017 "out of date, and can be upgraded. After being\n"
2018 "upgraded, these filesystems (and any 'zfs send' "
2019 "streams generated from\n"
2020 "subsequent snapshots) will no longer be "
2021 "accessible by older software versions.\n\n");
2024 if (!cb
->cb_foundone
) {
2026 (void) printf(gettext("VER FILESYSTEM\n"));
2027 (void) printf(gettext("--- ------------\n"));
2028 cb
->cb_foundone
= B_TRUE
;
2031 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
2038 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2040 upgrade_cbdata_t
*cb
= data
;
2041 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2042 int needed_spa_version
;
2045 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2048 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2050 if (needed_spa_version
< 0)
2053 if (spa_version
< needed_spa_version
) {
2055 (void) printf(gettext("%s: can not be "
2056 "upgraded; the pool version needs to first "
2057 "be upgraded\nto version %d\n\n"),
2058 zfs_get_name(zhp
), needed_spa_version
);
2064 if (version
< cb
->cb_version
) {
2066 (void) snprintf(verstr
, sizeof (verstr
),
2067 "%llu", cb
->cb_version
);
2068 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2070 * If they did "zfs upgrade -a", then we could
2071 * be doing ioctls to different pools. We need
2072 * to log this history once to each pool, and bypass
2073 * the normal history logging that happens in main().
2075 (void) zpool_log_history(g_zfs
, history_str
);
2076 log_history
= B_FALSE
;
2078 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2079 cb
->cb_numupgraded
++;
2082 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2083 } else if (version
> cb
->cb_version
) {
2084 /* can't downgrade */
2085 (void) printf(gettext("%s: can not be downgraded; "
2086 "it is already at version %u\n"),
2087 zfs_get_name(zhp
), version
);
2090 cb
->cb_numsamegraded
++;
2098 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2101 zfs_do_upgrade(int argc
, char **argv
)
2103 boolean_t all
= B_FALSE
;
2104 boolean_t showversions
= B_FALSE
;
2106 upgrade_cbdata_t cb
= { 0 };
2108 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2111 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2114 flags
|= ZFS_ITER_RECURSE
;
2117 showversions
= B_TRUE
;
2120 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2121 optarg
, &cb
.cb_version
) != 0) {
2122 (void) fprintf(stderr
,
2123 gettext("invalid version %s\n"), optarg
);
2132 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2141 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2143 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2144 cb
.cb_version
|| argc
))
2146 if ((all
|| argc
) && (showversions
))
2152 /* Show info on available versions. */
2153 (void) printf(gettext("The following filesystem versions are "
2155 (void) printf(gettext("VER DESCRIPTION\n"));
2156 (void) printf("--- -----------------------------------------"
2157 "---------------\n");
2158 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2159 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2160 (void) printf(gettext(" 3 Case insensitive and filesystem "
2161 "user identifier (FUID)\n"));
2162 (void) printf(gettext(" 4 userquota, groupquota "
2164 (void) printf(gettext(" 5 System attributes\n"));
2165 (void) printf(gettext("\nFor more information on a particular "
2166 "version, including supported releases,\n"));
2167 (void) printf("see the ZFS Administration Guide.\n\n");
2169 } else if (argc
|| all
) {
2170 /* Upgrade filesystems */
2171 if (cb
.cb_version
== 0)
2172 cb
.cb_version
= ZPL_VERSION
;
2173 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2174 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2175 (void) printf(gettext("%llu filesystems upgraded\n"),
2177 if (cb
.cb_numsamegraded
) {
2178 (void) printf(gettext("%llu filesystems already at "
2180 cb
.cb_numsamegraded
);
2182 if (cb
.cb_numfailed
!= 0)
2185 /* List old-version filesytems */
2187 (void) printf(gettext("This system is currently running "
2188 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2190 flags
|= ZFS_ITER_RECURSE
;
2191 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2192 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2194 found
= cb
.cb_foundone
;
2195 cb
.cb_foundone
= B_FALSE
;
2196 cb
.cb_newer
= B_TRUE
;
2198 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2199 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2201 if (!cb
.cb_foundone
&& !found
) {
2202 (void) printf(gettext("All filesystems are "
2203 "formatted with the current version.\n"));
2211 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2212 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2213 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2214 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2216 * -H Scripted mode; elide headers and separate columns by tabs.
2217 * -i Translate SID to POSIX ID.
2218 * -n Print numeric ID instead of user/group name.
2219 * -o Control which fields to display.
2220 * -p Use exact (parsable) numeric output.
2221 * -s Specify sort columns, descending order.
2222 * -S Specify sort columns, ascending order.
2223 * -t Control which object types to display.
2225 * Displays space consumed by, and quotas on, each user in the specified
2226 * filesystem or snapshot.
2229 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2230 enum us_field_types
{
2236 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA" };
2237 static char *us_field_names
[] = { "type", "name", "used", "quota" };
2238 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2240 #define USTYPE_PSX_GRP (1 << 0)
2241 #define USTYPE_PSX_USR (1 << 1)
2242 #define USTYPE_SMB_GRP (1 << 2)
2243 #define USTYPE_SMB_USR (1 << 3)
2244 #define USTYPE_ALL \
2245 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2247 static int us_type_bits
[] = {
2254 static char *us_type_names
[] = { "posixgroup", "posixuser", "smbgroup",
2257 typedef struct us_node
{
2259 uu_avl_node_t usn_avlnode
;
2260 uu_list_node_t usn_listnode
;
2263 typedef struct us_cbdata
{
2265 uu_avl_pool_t
*cb_avl_pool
;
2267 boolean_t cb_numname
;
2268 boolean_t cb_nicenum
;
2269 boolean_t cb_sid2posix
;
2270 zfs_userquota_prop_t cb_prop
;
2271 zfs_sort_column_t
*cb_sortcol
;
2272 size_t cb_width
[USFIELD_LAST
];
2275 static boolean_t us_populated
= B_FALSE
;
2278 zfs_sort_column_t
*si_sortcol
;
2279 boolean_t si_numname
;
2283 us_field_index(char *field
)
2287 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2288 if (strcmp(field
, us_field_names
[i
]) == 0)
2296 us_compare(const void *larg
, const void *rarg
, void *unused
)
2298 const us_node_t
*l
= larg
;
2299 const us_node_t
*r
= rarg
;
2300 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2301 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2302 boolean_t numname
= si
->si_numname
;
2303 nvlist_t
*lnvl
= l
->usn_nvl
;
2304 nvlist_t
*rnvl
= r
->usn_nvl
;
2308 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2315 zfs_prop_t prop
= sortcol
->sc_prop
;
2316 const char *propname
= NULL
;
2317 boolean_t reverse
= sortcol
->sc_reverse
;
2322 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2323 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2325 rc
= (rv32
< lv32
) ? 1 : -1;
2330 (void) nvlist_lookup_uint64(lnvl
, propname
,
2332 (void) nvlist_lookup_uint64(rnvl
, propname
,
2335 rc
= (rv64
< lv64
) ? 1 : -1;
2337 (void) nvlist_lookup_string(lnvl
, propname
,
2339 (void) nvlist_lookup_string(rnvl
, propname
,
2341 rc
= strcmp(lvstr
, rvstr
);
2345 case ZFS_PROP_QUOTA
:
2348 if (prop
== ZFS_PROP_USED
)
2352 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2353 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2355 rc
= (rv64
< lv64
) ? 1 : -1;
2364 return (reverse
? 1 : -1);
2366 return (reverse
? -1 : 1);
2371 * If entries still seem to be the same, check if they are of the same
2372 * type (smbentity is added only if we are doing SID to POSIX ID
2373 * translation where we can have duplicate type/name combinations).
2375 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2376 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2378 return (lvb
< rvb
? -1 : 1);
2383 static inline const char *
2384 us_type2str(unsigned field_type
)
2386 switch (field_type
) {
2387 case USTYPE_PSX_USR
:
2388 return ("POSIX User");
2389 case USTYPE_PSX_GRP
:
2390 return ("POSIX Group");
2391 case USTYPE_SMB_USR
:
2392 return ("SMB User");
2393 case USTYPE_SMB_GRP
:
2394 return ("SMB Group");
2396 return ("Undefined");
2401 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2403 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2404 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2409 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2410 uu_avl_t
*avl
= cb
->cb_avl
;
2414 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2416 const char *typestr
;
2420 int typeidx
, nameidx
, sizeidx
;
2421 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2422 boolean_t smbentity
= B_FALSE
;
2424 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2426 node
= safe_malloc(sizeof (us_node_t
));
2427 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2428 node
->usn_nvl
= props
;
2430 if (domain
!= NULL
&& domain
[0] != '\0') {
2432 char sid
[MAXNAMELEN
+ 32];
2435 int flag
= IDMAP_REQ_FLG_USE_CACHE
;
2439 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2441 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2442 type
= USTYPE_SMB_GRP
;
2443 err
= sid_to_id(sid
, B_FALSE
, &id
);
2445 type
= USTYPE_SMB_USR
;
2446 err
= sid_to_id(sid
, B_TRUE
, &id
);
2451 if (!cb
->cb_sid2posix
) {
2452 if (type
== USTYPE_SMB_USR
) {
2453 (void) idmap_getwinnamebyuid(rid
, flag
,
2456 (void) idmap_getwinnamebygid(rid
, flag
,
2465 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2467 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2468 type
= USTYPE_PSX_GRP
;
2469 if (!cb
->cb_numname
) {
2472 if ((g
= getgrgid(rid
)) != NULL
)
2476 type
= USTYPE_PSX_USR
;
2477 if (!cb
->cb_numname
) {
2480 if ((p
= getpwuid(rid
)) != NULL
)
2487 * Make sure that the type/name combination is unique when doing
2488 * SID to POSIX ID translation (hence changing the type from SMB to
2491 if (cb
->cb_sid2posix
&&
2492 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2495 /* Calculate/update width of TYPE field */
2496 typestr
= us_type2str(type
);
2497 typelen
= strlen(gettext(typestr
));
2498 typeidx
= us_field_index("type");
2499 if (typelen
> cb
->cb_width
[typeidx
])
2500 cb
->cb_width
[typeidx
] = typelen
;
2501 if (nvlist_add_uint32(props
, "type", type
) != 0)
2504 /* Calculate/update width of NAME field */
2505 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2506 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2508 namelen
= snprintf(NULL
, 0, "%u", rid
);
2510 if (nvlist_add_string(props
, "name", name
) != 0)
2512 namelen
= strlen(name
);
2514 nameidx
= us_field_index("name");
2515 if (namelen
> cb
->cb_width
[nameidx
])
2516 cb
->cb_width
[nameidx
] = namelen
;
2519 * Check if this type/name combination is in the list and update it;
2520 * otherwise add new node to the list.
2522 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2523 uu_avl_insert(avl
, node
, idx
);
2528 props
= node
->usn_nvl
;
2531 /* Calculate/update width of USED/QUOTA fields */
2533 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2535 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu", space
);
2536 sizelen
= strlen(sizebuf
);
2537 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
) {
2539 if (!nvlist_exists(props
, "quota"))
2540 (void) nvlist_add_uint64(props
, "quota", 0);
2543 if (!nvlist_exists(props
, "used"))
2544 (void) nvlist_add_uint64(props
, "used", 0);
2546 sizeidx
= us_field_index(propname
);
2547 if (sizelen
> cb
->cb_width
[sizeidx
])
2548 cb
->cb_width
[sizeidx
] = sizelen
;
2550 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2557 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2558 size_t *width
, us_node_t
*node
)
2560 nvlist_t
*nvl
= node
->usn_nvl
;
2561 char valstr
[MAXNAMELEN
];
2562 boolean_t first
= B_TRUE
;
2568 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2569 if (!(ustype
& types
))
2572 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2573 nvpair_t
*nvp
= NULL
;
2577 char *strval
= NULL
;
2579 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2580 if (strcmp(nvpair_name(nvp
),
2581 us_field_names
[field
]) == 0)
2585 type
= nvpair_type(nvp
);
2587 case DATA_TYPE_UINT32
:
2588 (void) nvpair_value_uint32(nvp
, &val32
);
2590 case DATA_TYPE_UINT64
:
2591 (void) nvpair_value_uint64(nvp
, &val64
);
2593 case DATA_TYPE_STRING
:
2594 (void) nvpair_value_string(nvp
, &strval
);
2597 (void) fprintf(stderr
, "invalid data type\n");
2602 strval
= (char *)us_type2str(val32
);
2605 if (type
== DATA_TYPE_UINT64
) {
2606 (void) sprintf(valstr
, "%llu", val64
);
2612 if (type
== DATA_TYPE_UINT64
) {
2614 (void) sprintf(valstr
, "%llu", val64
);
2616 zfs_nicenum(val64
, valstr
,
2619 if (field
== USFIELD_QUOTA
&&
2620 strcmp(valstr
, "0") == 0)
2630 (void) printf("\t");
2635 (void) printf("%s", strval
);
2636 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2637 (void) printf("%-*s", width
[field
], strval
);
2639 (void) printf("%*s", width
[field
], strval
);
2645 (void) printf("\n");
2649 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2650 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2658 boolean_t first
= B_TRUE
;
2660 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2661 col
= gettext(us_field_hdr
[field
]);
2662 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2663 (void) printf(first
? "%-*s" : " %-*s",
2666 (void) printf(first
? "%*s" : " %*s",
2672 (void) printf("\n");
2675 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2676 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2678 nvlist_free(node
->usn_nvl
);
2683 zfs_do_userspace(int argc
, char **argv
)
2686 zfs_userquota_prop_t p
;
2687 uu_avl_pool_t
*avl_pool
;
2689 uu_avl_walk_t
*walk
;
2691 char deffields
[] = "type,name,used,quota";
2692 char *ofield
= NULL
;
2693 char *tfield
= NULL
;
2697 boolean_t scripted
= B_FALSE
;
2698 boolean_t prtnum
= B_FALSE
;
2699 boolean_t parsable
= B_FALSE
;
2700 boolean_t sid2posix
= B_FALSE
;
2703 zfs_sort_column_t
*sortcol
= NULL
;
2704 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2708 uu_list_pool_t
*listpool
;
2710 uu_avl_index_t idx
= 0;
2711 uu_list_index_t idx2
= 0;
2716 if (strcmp(argv
[0], "groupspace") == 0)
2717 /* Toggle default group types */
2718 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2720 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2736 if (zfs_add_sort_column(&sortcol
, optarg
,
2737 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
2738 (void) fprintf(stderr
,
2739 gettext("invalid field '%s'\n"), optarg
);
2750 (void) fprintf(stderr
, gettext("missing argument for "
2751 "'%c' option\n"), optopt
);
2755 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2765 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2769 (void) fprintf(stderr
, gettext("too many arguments\n"));
2773 /* Use default output fields if not specified using -o */
2777 if ((delim
= strchr(ofield
, ',')) != NULL
)
2779 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
2780 (void) fprintf(stderr
, gettext("invalid type '%s' "
2781 "for -o option\n"), ofield
);
2786 } while (delim
!= NULL
);
2787 fields
[cfield
] = USFIELD_LAST
;
2789 /* Override output types (-t option) */
2790 if (tfield
!= NULL
) {
2794 boolean_t found
= B_FALSE
;
2796 if ((delim
= strchr(tfield
, ',')) != NULL
)
2798 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
2800 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
2802 types
|= us_type_bits
[i
];
2807 (void) fprintf(stderr
, gettext("invalid type "
2808 "'%s' for -t option\n"), tfield
);
2813 } while (delim
!= NULL
);
2816 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
2819 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
2820 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
2822 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
2825 /* Always add default sorting columns */
2826 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
2827 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
2829 cb
.cb_sortcol
= sortcol
;
2830 cb
.cb_numname
= prtnum
;
2831 cb
.cb_nicenum
= !parsable
;
2832 cb
.cb_avl_pool
= avl_pool
;
2833 cb
.cb_avl
= avl_tree
;
2834 cb
.cb_sid2posix
= sid2posix
;
2836 for (i
= 0; i
< USFIELD_LAST
; i
++)
2837 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
2839 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
2840 if (((p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
) &&
2841 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
2842 ((p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
) &&
2843 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))))
2846 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
2851 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
2854 us_populated
= B_TRUE
;
2856 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
2857 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
2858 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
2859 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
2861 while (node
!= NULL
) {
2863 node
= uu_avl_next(avl_tree
, node
);
2864 uu_avl_remove(avl_tree
, rmnode
);
2865 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
2866 uu_list_insert(list
, rmnode
, idx2
);
2869 for (node
= uu_list_first(list
); node
!= NULL
;
2870 node
= uu_list_next(list
, node
)) {
2871 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
2873 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
2874 uu_avl_insert(avl_tree
, node
, idx
);
2877 uu_list_destroy(list
);
2878 uu_list_pool_destroy(listpool
);
2880 /* Print and free node nvlist memory */
2881 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
2884 zfs_free_sort_columns(sortcol
);
2886 /* Clean up the AVL tree */
2887 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
2890 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
2891 uu_avl_remove(cb
.cb_avl
, node
);
2895 uu_avl_walk_end(walk
);
2896 uu_avl_destroy(avl_tree
);
2897 uu_avl_pool_destroy(avl_pool
);
2903 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2904 * [-t type[,...]] [filesystem|volume|snapshot] ...
2906 * -H Scripted mode; elide headers and separate columns by tabs.
2907 * -p Display values in parsable (literal) format.
2908 * -r Recurse over all children.
2909 * -d Limit recursion by depth.
2910 * -o Control which fields to display.
2911 * -s Specify sort columns, descending order.
2912 * -S Specify sort columns, ascending order.
2913 * -t Control which object types to display.
2915 * When given no arguments, list all filesystems in the system.
2916 * Otherwise, list the specified datasets, optionally recursing down them if
2917 * '-r' is specified.
2919 typedef struct list_cbdata
{
2921 boolean_t cb_literal
;
2922 boolean_t cb_scripted
;
2923 zprop_list_t
*cb_proplist
;
2927 * Given a list of columns to display, output appropriate headers for each one.
2930 print_header(list_cbdata_t
*cb
)
2932 zprop_list_t
*pl
= cb
->cb_proplist
;
2933 char headerbuf
[ZFS_MAXPROPLEN
];
2936 boolean_t first
= B_TRUE
;
2937 boolean_t right_justify
;
2939 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2946 right_justify
= B_FALSE
;
2947 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2948 header
= zfs_prop_column_name(pl
->pl_prop
);
2949 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2951 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2952 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2953 headerbuf
[i
] = '\0';
2957 if (pl
->pl_next
== NULL
&& !right_justify
)
2958 (void) printf("%s", header
);
2959 else if (right_justify
)
2960 (void) printf("%*s", pl
->pl_width
, header
);
2962 (void) printf("%-*s", pl
->pl_width
, header
);
2965 (void) printf("\n");
2969 * Given a dataset and a list of fields, print out all the properties according
2970 * to the described layout.
2973 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
2975 zprop_list_t
*pl
= cb
->cb_proplist
;
2976 boolean_t first
= B_TRUE
;
2977 char property
[ZFS_MAXPROPLEN
];
2978 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
2981 boolean_t right_justify
;
2983 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2985 if (cb
->cb_scripted
)
2986 (void) printf("\t");
2993 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
2994 (void) strlcpy(property
, zfs_get_name(zhp
),
2997 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2998 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
2999 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
3000 sizeof (property
), NULL
, NULL
, 0,
3001 cb
->cb_literal
) != 0)
3005 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3006 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
3007 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
3008 property
, sizeof (property
), cb
->cb_literal
) != 0)
3012 right_justify
= B_TRUE
;
3013 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
3014 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
3015 property
, sizeof (property
), cb
->cb_literal
) != 0)
3019 right_justify
= B_TRUE
;
3021 if (nvlist_lookup_nvlist(userprops
,
3022 pl
->pl_user_prop
, &propval
) != 0)
3025 verify(nvlist_lookup_string(propval
,
3026 ZPROP_VALUE
, &propstr
) == 0);
3027 right_justify
= B_FALSE
;
3031 * If this is being called in scripted mode, or if this is the
3032 * last column and it is left-justified, don't include a width
3035 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3036 (void) printf("%s", propstr
);
3037 else if (right_justify
)
3038 (void) printf("%*s", pl
->pl_width
, propstr
);
3040 (void) printf("%-*s", pl
->pl_width
, propstr
);
3043 (void) printf("\n");
3047 * Generic callback function to list a dataset or snapshot.
3050 list_callback(zfs_handle_t
*zhp
, void *data
)
3052 list_cbdata_t
*cbp
= data
;
3054 if (cbp
->cb_first
) {
3055 if (!cbp
->cb_scripted
)
3057 cbp
->cb_first
= B_FALSE
;
3060 print_dataset(zhp
, cbp
);
3066 zfs_do_list(int argc
, char **argv
)
3069 static char default_fields
[] =
3070 "name,used,available,referenced,mountpoint";
3071 int types
= ZFS_TYPE_DATASET
;
3072 boolean_t types_specified
= B_FALSE
;
3073 char *fields
= NULL
;
3074 list_cbdata_t cb
= { 0 };
3078 zfs_sort_column_t
*sortcol
= NULL
;
3079 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3082 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3088 cb
.cb_literal
= B_TRUE
;
3089 flags
|= ZFS_ITER_LITERAL_PROPS
;
3092 limit
= parse_depth(optarg
, &flags
);
3095 flags
|= ZFS_ITER_RECURSE
;
3098 cb
.cb_scripted
= B_TRUE
;
3101 if (zfs_add_sort_column(&sortcol
, optarg
,
3103 (void) fprintf(stderr
,
3104 gettext("invalid property '%s'\n"), optarg
);
3109 if (zfs_add_sort_column(&sortcol
, optarg
,
3111 (void) fprintf(stderr
,
3112 gettext("invalid property '%s'\n"), optarg
);
3118 types_specified
= B_TRUE
;
3119 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3120 while (*optarg
!= '\0') {
3121 static char *type_subopts
[] = { "filesystem",
3122 "volume", "snapshot", "snap", "bookmark",
3125 switch (getsubopt(&optarg
, type_subopts
,
3128 types
|= ZFS_TYPE_FILESYSTEM
;
3131 types
|= ZFS_TYPE_VOLUME
;
3135 types
|= ZFS_TYPE_SNAPSHOT
;
3138 types
|= ZFS_TYPE_BOOKMARK
;
3141 types
= ZFS_TYPE_DATASET
|
3145 (void) fprintf(stderr
,
3146 gettext("invalid type '%s'\n"),
3153 (void) fprintf(stderr
, gettext("missing argument for "
3154 "'%c' option\n"), optopt
);
3158 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3168 fields
= default_fields
;
3171 * If we are only going to list snapshot names and sort by name,
3172 * then we can use faster version.
3174 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3175 flags
|= ZFS_ITER_SIMPLE
;
3178 * If "-o space" and no types were specified, don't display snapshots.
3180 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3181 types
&= ~ZFS_TYPE_SNAPSHOT
;
3184 * If the user specifies '-o all', the zprop_get_list() doesn't
3185 * normally include the name of the dataset. For 'zfs list', we always
3186 * want this property to be first.
3188 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3192 cb
.cb_first
= B_TRUE
;
3194 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3195 limit
, list_callback
, &cb
);
3197 zprop_free_list(cb
.cb_proplist
);
3198 zfs_free_sort_columns(sortcol
);
3200 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3201 (void) printf(gettext("no datasets available\n"));
3207 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3208 * zfs rename [-f] -p <fs | vol> <fs | vol>
3209 * zfs rename -r <snap> <snap>
3211 * Renames the given dataset to another of the same type.
3213 * The '-p' flag creates all the non-existing ancestors of the target first.
3217 zfs_do_rename(int argc
, char **argv
)
3222 boolean_t recurse
= B_FALSE
;
3223 boolean_t parents
= B_FALSE
;
3224 boolean_t force_unmount
= B_FALSE
;
3227 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3236 force_unmount
= B_TRUE
;
3240 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3249 /* check number of arguments */
3251 (void) fprintf(stderr
, gettext("missing source dataset "
3256 (void) fprintf(stderr
, gettext("missing target dataset "
3261 (void) fprintf(stderr
, gettext("too many arguments\n"));
3265 if (recurse
&& parents
) {
3266 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3271 if (recurse
&& strchr(argv
[0], '@') == 0) {
3272 (void) fprintf(stderr
, gettext("source dataset for recursive "
3273 "rename must be a snapshot\n"));
3277 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3278 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3281 /* If we were asked and the name looks good, try to create ancestors. */
3282 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3283 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3288 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3297 * Promotes the given clone fs to be the parent
3301 zfs_do_promote(int argc
, char **argv
)
3307 if (argc
> 1 && argv
[1][0] == '-') {
3308 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3313 /* check number of arguments */
3315 (void) fprintf(stderr
, gettext("missing clone filesystem"
3320 (void) fprintf(stderr
, gettext("too many arguments\n"));
3324 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3328 ret
= (zfs_promote(zhp
) != 0);
3336 * zfs rollback [-rRf] <snapshot>
3338 * -r Delete any intervening snapshots before doing rollback
3339 * -R Delete any snapshots and their clones
3340 * -f ignored for backwards compatability
3342 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3343 * since then and making it the active dataset. If more recent snapshots exist,
3344 * the command will complain unless the '-r' flag is given.
3346 typedef struct rollback_cbdata
{
3352 boolean_t cb_recurse
;
3353 } rollback_cbdata_t
;
3356 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3358 rollback_cbdata_t
*cbp
= data
;
3360 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3361 (void) fprintf(stderr
, gettext("cannot rollback to "
3362 "'%s': clones of previous snapshots exist\n"),
3364 (void) fprintf(stderr
, gettext("use '-R' to "
3365 "force deletion of the following clones and "
3371 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3378 * Report any snapshots more recent than the one specified. Used when '-r' is
3379 * not specified. We reuse this same callback for the snapshot dependents - if
3380 * 'cb_dependent' is set, then this is a dependent and we should report it
3381 * without checking the transaction group.
3384 rollback_check(zfs_handle_t
*zhp
, void *data
)
3386 rollback_cbdata_t
*cbp
= data
;
3388 if (cbp
->cb_doclones
) {
3393 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3394 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3395 (void) fprintf(stderr
, gettext("cannot "
3396 "rollback to '%s': more recent snapshots "
3397 "or bookmarks exist\n"),
3399 (void) fprintf(stderr
, gettext("use '-r' to "
3400 "force deletion of the following "
3401 "snapshots and bookmarks:\n"));
3406 if (cbp
->cb_recurse
) {
3407 if (zfs_iter_dependents(zhp
, B_TRUE
,
3408 rollback_check_dependent
, cbp
) != 0) {
3413 (void) fprintf(stderr
, "%s\n",
3422 zfs_do_rollback(int argc
, char **argv
)
3426 boolean_t force
= B_FALSE
;
3427 rollback_cbdata_t cb
= { 0 };
3428 zfs_handle_t
*zhp
, *snap
;
3429 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3433 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3446 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3455 /* check number of arguments */
3457 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3461 (void) fprintf(stderr
, gettext("too many arguments\n"));
3465 /* open the snapshot */
3466 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3469 /* open the parent dataset */
3470 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3471 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3473 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3479 * Check for more recent snapshots and/or clones based on the presence
3482 cb
.cb_target
= argv
[0];
3483 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3484 cb
.cb_first
= B_TRUE
;
3486 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, rollback_check
, &cb
)) != 0)
3488 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
3491 if ((ret
= cb
.cb_error
) != 0)
3495 * Rollback parent to the given snapshot.
3497 ret
= zfs_rollback(zhp
, snap
, force
);
3510 * zfs set property=value ... { fs | snap | vol } ...
3512 * Sets the given properties for all datasets specified on the command line.
3516 set_callback(zfs_handle_t
*zhp
, void *data
)
3518 nvlist_t
*props
= data
;
3520 if (zfs_prop_set_list(zhp
, props
) != 0) {
3521 switch (libzfs_errno(g_zfs
)) {
3522 case EZFS_MOUNTFAILED
:
3523 (void) fprintf(stderr
, gettext("property may be set "
3524 "but unable to remount filesystem\n"));
3526 case EZFS_SHARENFSFAILED
:
3527 (void) fprintf(stderr
, gettext("property may be set "
3528 "but unable to reshare filesystem\n"));
3537 zfs_do_set(int argc
, char **argv
)
3539 nvlist_t
*props
= NULL
;
3540 int ds_start
= -1; /* argv idx of first dataset arg */
3543 /* check for options */
3544 if (argc
> 1 && argv
[1][0] == '-') {
3545 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3550 /* check number of arguments */
3552 (void) fprintf(stderr
, gettext("missing arguments\n"));
3556 if (strchr(argv
[1], '=') == NULL
) {
3557 (void) fprintf(stderr
, gettext("missing property=value "
3560 (void) fprintf(stderr
, gettext("missing dataset "
3566 /* validate argument order: prop=val args followed by dataset args */
3567 for (int i
= 1; i
< argc
; i
++) {
3568 if (strchr(argv
[i
], '=') != NULL
) {
3570 /* out-of-order prop=val argument */
3571 (void) fprintf(stderr
, gettext("invalid "
3572 "argument order\n"), i
);
3575 } else if (ds_start
< 0) {
3580 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
3584 /* Populate a list of property settings */
3585 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3587 for (int i
= 1; i
< ds_start
; i
++) {
3588 if ((ret
= parseprop(props
, argv
[i
])) != 0)
3592 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
3593 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, props
);
3600 typedef struct snap_cbdata
{
3602 boolean_t sd_recursive
;
3603 const char *sd_snapname
;
3607 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3609 snap_cbdata_t
*sd
= arg
;
3614 if (sd
->sd_recursive
&&
3615 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
3620 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3623 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3626 if (sd
->sd_recursive
)
3627 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3633 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3635 * Creates a snapshot with the given name. While functionally equivalent to
3636 * 'zfs create', it is a separate command to differentiate intent.
3639 zfs_do_snapshot(int argc
, char **argv
)
3644 snap_cbdata_t sd
= { 0 };
3645 boolean_t multiple_snaps
= B_FALSE
;
3647 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3649 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3653 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3656 if (parseprop(props
, optarg
) != 0)
3660 sd
.sd_recursive
= B_TRUE
;
3661 multiple_snaps
= B_TRUE
;
3664 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3673 /* check number of arguments */
3675 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3680 multiple_snaps
= B_TRUE
;
3681 for (; argc
> 0; argc
--, argv
++) {
3685 atp
= strchr(argv
[0], '@');
3689 sd
.sd_snapname
= atp
+ 1;
3690 zhp
= zfs_open(g_zfs
, argv
[0],
3691 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3694 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3698 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3699 nvlist_free(sd
.sd_nvl
);
3701 if (ret
!= 0 && multiple_snaps
)
3702 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3706 nvlist_free(sd
.sd_nvl
);
3713 * Send a backup stream to stdout.
3716 zfs_do_send(int argc
, char **argv
)
3718 char *fromname
= NULL
;
3719 char *toname
= NULL
;
3720 char *resume_token
= NULL
;
3723 sendflags_t flags
= { 0 };
3725 nvlist_t
*dbgnv
= NULL
;
3726 boolean_t extraverbose
= B_FALSE
;
3728 struct option long_options
[] = {
3729 {"replicate", no_argument
, NULL
, 'R'},
3730 {"props", no_argument
, NULL
, 'p'},
3731 {"parsable", no_argument
, NULL
, 'P'},
3732 {"dedup", no_argument
, NULL
, 'D'},
3733 {"verbose", no_argument
, NULL
, 'v'},
3734 {"dryrun", no_argument
, NULL
, 'n'},
3735 {"large-block", no_argument
, NULL
, 'L'},
3736 {"embed", no_argument
, NULL
, 'e'},
3737 {"resume", required_argument
, NULL
, 't'},
3738 {"compressed", no_argument
, NULL
, 'c'},
3743 while ((c
= getopt_long(argc
, argv
, ":i:I:RbDpvnPLet:c", long_options
,
3755 flags
.doall
= B_TRUE
;
3758 flags
.replicate
= B_TRUE
;
3761 flags
.props
= B_TRUE
;
3764 flags
.parsable
= B_TRUE
;
3765 flags
.verbose
= B_TRUE
;
3769 extraverbose
= B_TRUE
;
3770 flags
.verbose
= B_TRUE
;
3771 flags
.progress
= B_TRUE
;
3774 flags
.dedup
= B_TRUE
;
3777 flags
.dryrun
= B_TRUE
;
3780 flags
.largeblock
= B_TRUE
;
3783 flags
.embed_data
= B_TRUE
;
3786 resume_token
= optarg
;
3789 flags
.compress
= B_TRUE
;
3793 * If a parameter was not passed, optopt contains the
3794 * value that would normally lead us into the
3795 * appropriate case statement. If it's > 256, then this
3796 * must be a longopt and we should look at argv to get
3797 * the string. Otherwise it's just the character, so we
3798 * should use it directly.
3800 if (optopt
<= UINT8_MAX
) {
3801 (void) fprintf(stderr
,
3802 gettext("missing argument for '%c' "
3803 "option\n"), optopt
);
3805 (void) fprintf(stderr
,
3806 gettext("missing argument for '%s' "
3807 "option\n"), argv
[optind
- 1]);
3815 * If an invalid flag was passed, optopt contains the
3816 * character if it was a short flag, or 0 if it was a
3820 (void) fprintf(stderr
,
3821 gettext("invalid option '%c'\n"), optopt
);
3823 (void) fprintf(stderr
,
3824 gettext("invalid option '%s'\n"),
3835 if (resume_token
!= NULL
) {
3836 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
3838 (void) fprintf(stderr
,
3839 gettext("invalid flags combined with -t\n"));
3843 (void) fprintf(stderr
, gettext("no additional "
3844 "arguments are permitted with -t\n"));
3849 (void) fprintf(stderr
,
3850 gettext("missing snapshot argument\n"));
3854 (void) fprintf(stderr
, gettext("too many arguments\n"));
3859 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
3860 (void) fprintf(stderr
,
3861 gettext("Error: Stream can not be written to a terminal.\n"
3862 "You must redirect standard output.\n"));
3866 if (resume_token
!= NULL
) {
3867 return (zfs_send_resume(g_zfs
, &flags
, STDOUT_FILENO
,
3872 * Special case sending a filesystem, or from a bookmark.
3874 if (strchr(argv
[0], '@') == NULL
||
3875 (fromname
&& strchr(fromname
, '#') != NULL
)) {
3876 char frombuf
[ZFS_MAX_DATASET_NAME_LEN
];
3877 enum lzc_send_flags lzc_flags
= 0;
3879 if (flags
.replicate
|| flags
.doall
|| flags
.props
||
3880 flags
.dedup
|| flags
.dryrun
|| flags
.verbose
||
3882 (void) fprintf(stderr
,
3884 "Unsupported flag with filesystem or bookmark.\n"));
3888 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
3892 if (flags
.largeblock
)
3893 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
3894 if (flags
.embed_data
)
3895 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
3897 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
3899 if (fromname
!= NULL
&&
3900 (fromname
[0] == '#' || fromname
[0] == '@')) {
3902 * Incremental source name begins with # or @.
3903 * Default to same fs as target.
3905 (void) strncpy(frombuf
, argv
[0], sizeof (frombuf
));
3906 cp
= strchr(frombuf
, '@');
3909 (void) strlcat(frombuf
, fromname
, sizeof (frombuf
));
3912 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, lzc_flags
);
3917 cp
= strchr(argv
[0], '@');
3920 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3925 * If they specified the full path to the snapshot, chop off
3926 * everything except the short name of the snapshot, but special
3927 * case if they specify the origin.
3929 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
3930 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
3933 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
3934 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
3936 if (strcmp(origin
, fromname
) == 0) {
3938 flags
.fromorigin
= B_TRUE
;
3941 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
3942 (void) fprintf(stderr
,
3943 gettext("incremental source must be "
3944 "in same filesystem\n"));
3948 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
3949 (void) fprintf(stderr
,
3950 gettext("invalid incremental source\n"));
3956 if (flags
.replicate
&& fromname
== NULL
)
3957 flags
.doall
= B_TRUE
;
3959 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
3960 extraverbose
? &dbgnv
: NULL
);
3962 if (extraverbose
&& dbgnv
!= NULL
) {
3964 * dump_nvlist prints to stdout, but that's been
3965 * redirected to a file. Make it print to stderr
3968 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
3969 dump_nvlist(dbgnv
, 0);
3978 * Restore a backup stream from stdin.
3981 zfs_do_receive(int argc
, char **argv
)
3984 recvflags_t flags
= { 0 };
3985 boolean_t abort_resumable
= B_FALSE
;
3988 nvpair_t
*nvp
= NULL
;
3990 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3994 while ((c
= getopt(argc
, argv
, ":o:denuvFsA")) != -1) {
3997 if (parseprop(props
, optarg
) != 0)
4001 flags
.isprefix
= B_TRUE
;
4004 flags
.isprefix
= B_TRUE
;
4005 flags
.istail
= B_TRUE
;
4008 flags
.dryrun
= B_TRUE
;
4011 flags
.nomount
= B_TRUE
;
4014 flags
.verbose
= B_TRUE
;
4017 flags
.resumable
= B_TRUE
;
4020 flags
.force
= B_TRUE
;
4023 abort_resumable
= B_TRUE
;
4026 (void) fprintf(stderr
, gettext("missing argument for "
4027 "'%c' option\n"), optopt
);
4031 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4040 /* check number of arguments */
4042 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4046 (void) fprintf(stderr
, gettext("too many arguments\n"));
4050 while ((nvp
= nvlist_next_nvpair(props
, nvp
))) {
4051 if (strcmp(nvpair_name(nvp
), "origin") != 0) {
4052 (void) fprintf(stderr
, gettext("invalid option"));
4057 if (abort_resumable
) {
4058 if (flags
.isprefix
|| flags
.istail
|| flags
.dryrun
||
4059 flags
.resumable
|| flags
.nomount
) {
4060 (void) fprintf(stderr
, gettext("invalid option"));
4064 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
4065 (void) snprintf(namebuf
, sizeof (namebuf
),
4066 "%s/%%recv", argv
[0]);
4068 if (zfs_dataset_exists(g_zfs
, namebuf
,
4069 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) {
4070 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4071 namebuf
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4074 err
= zfs_destroy(zhp
, B_FALSE
);
4076 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4077 argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4080 if (!zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) ||
4081 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
4082 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == -1) {
4083 (void) fprintf(stderr
,
4084 gettext("'%s' does not have any "
4085 "resumable receive state to abort\n"),
4089 err
= zfs_destroy(zhp
, B_FALSE
);
4095 if (isatty(STDIN_FILENO
)) {
4096 (void) fprintf(stderr
,
4097 gettext("Error: Backup stream can not be read "
4098 "from a terminal.\n"
4099 "You must redirect standard input.\n"));
4102 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
4108 * allow/unallow stuff
4110 /* copied from zfs/sys/dsl_deleg.h */
4111 #define ZFS_DELEG_PERM_CREATE "create"
4112 #define ZFS_DELEG_PERM_DESTROY "destroy"
4113 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4114 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4115 #define ZFS_DELEG_PERM_CLONE "clone"
4116 #define ZFS_DELEG_PERM_PROMOTE "promote"
4117 #define ZFS_DELEG_PERM_RENAME "rename"
4118 #define ZFS_DELEG_PERM_MOUNT "mount"
4119 #define ZFS_DELEG_PERM_SHARE "share"
4120 #define ZFS_DELEG_PERM_SEND "send"
4121 #define ZFS_DELEG_PERM_RECEIVE "receive"
4122 #define ZFS_DELEG_PERM_ALLOW "allow"
4123 #define ZFS_DELEG_PERM_USERPROP "userprop"
4124 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4125 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4126 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4127 #define ZFS_DELEG_PERM_USERUSED "userused"
4128 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4129 #define ZFS_DELEG_PERM_HOLD "hold"
4130 #define ZFS_DELEG_PERM_RELEASE "release"
4131 #define ZFS_DELEG_PERM_DIFF "diff"
4132 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4134 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4136 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
4137 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
4138 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
4139 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
4140 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
4141 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
4142 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
4143 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
4144 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
4145 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
4146 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
4147 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
4148 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
4149 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
4150 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
4151 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
4152 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
4154 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
4155 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
4156 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
4157 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
4158 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
4159 { NULL
, ZFS_DELEG_NOTE_NONE
}
4162 /* permission structure */
4163 typedef struct deleg_perm
{
4164 zfs_deleg_who_type_t dp_who_type
;
4165 const char *dp_name
;
4167 boolean_t dp_descend
;
4171 typedef struct deleg_perm_node
{
4172 deleg_perm_t dpn_perm
;
4174 uu_avl_node_t dpn_avl_node
;
4175 } deleg_perm_node_t
;
4177 typedef struct fs_perm fs_perm_t
;
4179 /* permissions set */
4180 typedef struct who_perm
{
4181 zfs_deleg_who_type_t who_type
;
4182 const char *who_name
; /* id */
4183 char who_ug_name
[256]; /* user/group name */
4184 fs_perm_t
*who_fsperm
; /* uplink */
4186 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
4190 typedef struct who_perm_node
{
4191 who_perm_t who_perm
;
4192 uu_avl_node_t who_avl_node
;
4195 typedef struct fs_perm_set fs_perm_set_t
;
4196 /* fs permissions */
4198 const char *fsp_name
;
4200 uu_avl_t
*fsp_sc_avl
; /* sets,create */
4201 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
4203 fs_perm_set_t
*fsp_set
; /* uplink */
4207 typedef struct fs_perm_node
{
4208 fs_perm_t fspn_fsperm
;
4211 uu_list_node_t fspn_list_node
;
4214 /* top level structure */
4215 struct fs_perm_set
{
4216 uu_list_pool_t
*fsps_list_pool
;
4217 uu_list_t
*fsps_list
; /* list of fs_perms */
4219 uu_avl_pool_t
*fsps_named_set_avl_pool
;
4220 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
4221 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
4224 static inline const char *
4225 deleg_perm_type(zfs_deleg_note_t note
)
4231 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4232 case ZFS_DELEG_NOTE_GROUPUSED
:
4233 case ZFS_DELEG_NOTE_USERPROP
:
4234 case ZFS_DELEG_NOTE_USERQUOTA
:
4235 case ZFS_DELEG_NOTE_USERUSED
:
4237 return (gettext("other"));
4239 return (gettext("subcommand"));
4244 who_type2weight(zfs_deleg_who_type_t who_type
)
4248 case ZFS_DELEG_NAMED_SET_SETS
:
4249 case ZFS_DELEG_NAMED_SET
:
4252 case ZFS_DELEG_CREATE_SETS
:
4253 case ZFS_DELEG_CREATE
:
4256 case ZFS_DELEG_USER_SETS
:
4257 case ZFS_DELEG_USER
:
4260 case ZFS_DELEG_GROUP_SETS
:
4261 case ZFS_DELEG_GROUP
:
4264 case ZFS_DELEG_EVERYONE_SETS
:
4265 case ZFS_DELEG_EVERYONE
:
4277 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4279 const who_perm_node_t
*l
= larg
;
4280 const who_perm_node_t
*r
= rarg
;
4281 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
4282 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
4283 int lweight
= who_type2weight(ltype
);
4284 int rweight
= who_type2weight(rtype
);
4285 int res
= lweight
- rweight
;
4287 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
4288 ZFS_MAX_DELEG_NAME
-1);
4300 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4302 const deleg_perm_node_t
*l
= larg
;
4303 const deleg_perm_node_t
*r
= rarg
;
4304 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
4305 ZFS_MAX_DELEG_NAME
-1);
4317 fs_perm_set_init(fs_perm_set_t
*fspset
)
4319 bzero(fspset
, sizeof (fs_perm_set_t
));
4321 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4322 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4323 NULL
, UU_DEFAULT
)) == NULL
)
4325 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4326 UU_DEFAULT
)) == NULL
)
4329 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4330 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4331 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4332 UU_DEFAULT
)) == NULL
)
4335 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4336 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4337 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4338 UU_DEFAULT
)) == NULL
)
4341 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4342 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4343 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4348 static inline void fs_perm_fini(fs_perm_t
*);
4349 static inline void who_perm_fini(who_perm_t
*);
4352 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4354 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4356 while (node
!= NULL
) {
4357 fs_perm_node_t
*next_node
=
4358 uu_list_next(fspset
->fsps_list
, node
);
4359 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4360 fs_perm_fini(fsperm
);
4361 uu_list_remove(fspset
->fsps_list
, node
);
4366 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4367 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4368 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4372 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4375 deleg_perm
->dp_who_type
= type
;
4376 deleg_perm
->dp_name
= name
;
4380 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4381 zfs_deleg_who_type_t type
, const char *name
)
4383 uu_avl_pool_t
*pool
;
4384 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4386 bzero(who_perm
, sizeof (who_perm_t
));
4388 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4389 UU_DEFAULT
)) == NULL
)
4392 who_perm
->who_type
= type
;
4393 who_perm
->who_name
= name
;
4394 who_perm
->who_fsperm
= fsperm
;
4398 who_perm_fini(who_perm_t
*who_perm
)
4400 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4402 while (node
!= NULL
) {
4403 deleg_perm_node_t
*next_node
=
4404 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4406 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4411 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4415 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4417 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4418 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4420 bzero(fsperm
, sizeof (fs_perm_t
));
4422 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4426 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4430 fsperm
->fsp_set
= fspset
;
4431 fsperm
->fsp_name
= fsname
;
4435 fs_perm_fini(fs_perm_t
*fsperm
)
4437 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4438 while (node
!= NULL
) {
4439 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4441 who_perm_t
*who_perm
= &node
->who_perm
;
4442 who_perm_fini(who_perm
);
4443 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4448 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4449 while (node
!= NULL
) {
4450 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4452 who_perm_t
*who_perm
= &node
->who_perm
;
4453 who_perm_fini(who_perm
);
4454 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4459 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4460 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4464 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4465 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4467 uu_avl_index_t idx
= 0;
4469 deleg_perm_node_t
*found_node
= NULL
;
4470 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4472 deleg_perm_init(deleg_perm
, who_type
, name
);
4474 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4476 uu_avl_insert(avl
, node
, idx
);
4479 deleg_perm
= &node
->dpn_perm
;
4484 case ZFS_DELEG_LOCAL
:
4485 deleg_perm
->dp_local
= B_TRUE
;
4487 case ZFS_DELEG_DESCENDENT
:
4488 deleg_perm
->dp_descend
= B_TRUE
;
4493 assert(B_FALSE
); /* invalid locality */
4498 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4500 nvpair_t
*nvp
= NULL
;
4501 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4502 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4503 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4505 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4506 const char *name
= nvpair_name(nvp
);
4507 data_type_t type
= nvpair_type(nvp
);
4508 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4509 deleg_perm_node_t
*node
=
4510 safe_malloc(sizeof (deleg_perm_node_t
));
4512 assert(type
== DATA_TYPE_BOOLEAN
);
4514 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4515 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4522 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4524 nvpair_t
*nvp
= NULL
;
4525 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4527 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4528 nvlist_t
*nvl2
= NULL
;
4529 const char *name
= nvpair_name(nvp
);
4530 uu_avl_t
*avl
= NULL
;
4531 uu_avl_pool_t
*avl_pool
= NULL
;
4532 zfs_deleg_who_type_t perm_type
= name
[0];
4533 char perm_locality
= name
[1];
4534 const char *perm_name
= name
+ 3;
4535 boolean_t is_set
= B_TRUE
;
4536 who_perm_t
*who_perm
= NULL
;
4538 assert('$' == name
[2]);
4540 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4543 switch (perm_type
) {
4544 case ZFS_DELEG_CREATE
:
4545 case ZFS_DELEG_CREATE_SETS
:
4546 case ZFS_DELEG_NAMED_SET
:
4547 case ZFS_DELEG_NAMED_SET_SETS
:
4548 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4549 avl
= fsperm
->fsp_sc_avl
;
4551 case ZFS_DELEG_USER
:
4552 case ZFS_DELEG_USER_SETS
:
4553 case ZFS_DELEG_GROUP
:
4554 case ZFS_DELEG_GROUP_SETS
:
4555 case ZFS_DELEG_EVERYONE
:
4556 case ZFS_DELEG_EVERYONE_SETS
:
4557 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4558 avl
= fsperm
->fsp_uge_avl
;
4562 assert(!"unhandled zfs_deleg_who_type_t");
4566 who_perm_node_t
*found_node
= NULL
;
4567 who_perm_node_t
*node
= safe_malloc(
4568 sizeof (who_perm_node_t
));
4569 who_perm
= &node
->who_perm
;
4570 uu_avl_index_t idx
= 0;
4572 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4573 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4575 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4577 if (avl
== fsperm
->fsp_uge_avl
) {
4579 struct passwd
*p
= NULL
;
4580 struct group
*g
= NULL
;
4581 const char *nice_name
= NULL
;
4583 switch (perm_type
) {
4584 case ZFS_DELEG_USER_SETS
:
4585 case ZFS_DELEG_USER
:
4586 rid
= atoi(perm_name
);
4589 nice_name
= p
->pw_name
;
4591 case ZFS_DELEG_GROUP_SETS
:
4592 case ZFS_DELEG_GROUP
:
4593 rid
= atoi(perm_name
);
4596 nice_name
= g
->gr_name
;
4603 if (nice_name
!= NULL
)
4605 node
->who_perm
.who_ug_name
,
4609 uu_avl_insert(avl
, node
, idx
);
4612 who_perm
= &node
->who_perm
;
4616 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4623 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4625 nvpair_t
*nvp
= NULL
;
4626 uu_avl_index_t idx
= 0;
4628 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4629 nvlist_t
*nvl2
= NULL
;
4630 const char *fsname
= nvpair_name(nvp
);
4631 data_type_t type
= nvpair_type(nvp
);
4632 fs_perm_t
*fsperm
= NULL
;
4633 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4637 fsperm
= &node
->fspn_fsperm
;
4639 assert(DATA_TYPE_NVLIST
== type
);
4641 uu_list_node_init(node
, &node
->fspn_list_node
,
4642 fspset
->fsps_list_pool
);
4644 idx
= uu_list_numnodes(fspset
->fsps_list
);
4645 fs_perm_init(fsperm
, fspset
, fsname
);
4647 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4650 (void) parse_fs_perm(fsperm
, nvl2
);
4652 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4658 static inline const char *
4659 deleg_perm_comment(zfs_deleg_note_t note
)
4661 const char *str
= "";
4666 case ZFS_DELEG_NOTE_ALLOW
:
4667 str
= gettext("Must also have the permission that is being"
4668 "\n\t\t\t\tallowed");
4670 case ZFS_DELEG_NOTE_CLONE
:
4671 str
= gettext("Must also have the 'create' ability and 'mount'"
4672 "\n\t\t\t\tability in the origin file system");
4674 case ZFS_DELEG_NOTE_CREATE
:
4675 str
= gettext("Must also have the 'mount' ability");
4677 case ZFS_DELEG_NOTE_DESTROY
:
4678 str
= gettext("Must also have the 'mount' ability");
4680 case ZFS_DELEG_NOTE_DIFF
:
4681 str
= gettext("Allows lookup of paths within a dataset;"
4682 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4683 "\n\t\t\t\tin order to use zfs diff");
4685 case ZFS_DELEG_NOTE_HOLD
:
4686 str
= gettext("Allows adding a user hold to a snapshot");
4688 case ZFS_DELEG_NOTE_MOUNT
:
4689 str
= gettext("Allows mount/umount of ZFS datasets");
4691 case ZFS_DELEG_NOTE_PROMOTE
:
4692 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
4693 " 'promote' ability in the origin file system");
4695 case ZFS_DELEG_NOTE_RECEIVE
:
4696 str
= gettext("Must also have the 'mount' and 'create'"
4699 case ZFS_DELEG_NOTE_RELEASE
:
4700 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
4701 "might destroy the snapshot");
4703 case ZFS_DELEG_NOTE_RENAME
:
4704 str
= gettext("Must also have the 'mount' and 'create'"
4705 "\n\t\t\t\tability in the new parent");
4707 case ZFS_DELEG_NOTE_ROLLBACK
:
4710 case ZFS_DELEG_NOTE_SEND
:
4713 case ZFS_DELEG_NOTE_SHARE
:
4714 str
= gettext("Allows sharing file systems over NFS or SMB"
4715 "\n\t\t\t\tprotocols");
4717 case ZFS_DELEG_NOTE_SNAPSHOT
:
4721 * case ZFS_DELEG_NOTE_VSCAN:
4722 * str = gettext("");
4726 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4727 str
= gettext("Allows accessing any groupquota@... property");
4729 case ZFS_DELEG_NOTE_GROUPUSED
:
4730 str
= gettext("Allows reading any groupused@... property");
4732 case ZFS_DELEG_NOTE_USERPROP
:
4733 str
= gettext("Allows changing any user property");
4735 case ZFS_DELEG_NOTE_USERQUOTA
:
4736 str
= gettext("Allows accessing any userquota@... property");
4738 case ZFS_DELEG_NOTE_USERUSED
:
4739 str
= gettext("Allows reading any userused@... property");
4757 boolean_t recursive
; /* unallow only */
4758 boolean_t prt_usage
;
4760 boolean_t prt_perms
;
4763 const char *dataset
;
4767 prop_cmp(const void *a
, const void *b
)
4769 const char *str1
= *(const char **)a
;
4770 const char *str2
= *(const char **)b
;
4771 return (strcmp(str1
, str2
));
4775 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
4777 const char *opt_desc
[] = {
4778 "-h", gettext("show this help message and exit"),
4779 "-l", gettext("set permission locally"),
4780 "-d", gettext("set permission for descents"),
4781 "-u", gettext("set permission for user"),
4782 "-g", gettext("set permission for group"),
4783 "-e", gettext("set permission for everyone"),
4784 "-c", gettext("set create time permission"),
4785 "-s", gettext("define permission set"),
4787 "-r", gettext("remove permissions recursively"),
4789 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
4790 size_t allow_size
= unallow_size
- 2;
4791 const char *props
[ZFS_NUM_PROPS
];
4794 FILE *fp
= requested
? stdout
: stderr
;
4795 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
4796 const char *fmt
= gettext("%-16s %-14s\t%s\n");
4798 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
4800 (void) fprintf(fp
, gettext("Options:\n"));
4801 for (int i
= 0; i
< (un
? unallow_size
: allow_size
); i
++) {
4802 const char *opt
= opt_desc
[i
++];
4803 const char *optdsc
= opt_desc
[i
];
4804 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
4807 (void) fprintf(fp
, gettext("\nThe following permissions are "
4809 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
4811 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
4812 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
4813 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
4814 const char *perm_type
= deleg_perm_type(perm_note
);
4815 const char *perm_comment
= deleg_perm_comment(perm_note
);
4816 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
4819 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
4820 zprop_desc_t
*pd
= &pdtbl
[i
];
4821 if (pd
->pd_visible
!= B_TRUE
)
4824 if (pd
->pd_attr
== PROP_READONLY
)
4827 props
[count
++] = pd
->pd_name
;
4829 props
[count
] = NULL
;
4831 qsort(props
, count
, sizeof (char *), prop_cmp
);
4833 for (i
= 0; i
< count
; i
++)
4834 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
4837 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
4839 exit(requested
? 0 : 2);
4842 static inline const char *
4843 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
4846 if (un
&& argc
== expected_argc
- 1)
4848 else if (argc
== expected_argc
)
4849 *permsp
= argv
[argc
- 2];
4851 allow_usage(un
, B_FALSE
,
4852 gettext("wrong number of parameters\n"));
4854 return (argv
[argc
- 1]);
4858 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
4860 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
4861 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
4862 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
4863 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
4866 allow_usage(un
, B_FALSE
,
4867 gettext("-u, -g, and -e are mutually exclusive\n"));
4869 if (opts
->prt_usage
) {
4870 if (argc
== 0 && all_sum
== 0)
4871 allow_usage(un
, B_TRUE
, NULL
);
4878 allow_usage(un
, B_FALSE
,
4879 gettext("invalid options combined with -s\n"));
4881 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4882 if (argv
[0][0] != '@')
4883 allow_usage(un
, B_FALSE
,
4884 gettext("invalid set name: missing '@' prefix\n"));
4885 opts
->who
= argv
[0];
4886 } else if (opts
->create
) {
4887 if (ldcsuge_sum
> 1)
4888 allow_usage(un
, B_FALSE
,
4889 gettext("invalid options combined with -c\n"));
4890 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4891 } else if (opts
->everyone
) {
4893 allow_usage(un
, B_FALSE
,
4894 gettext("invalid options combined with -e\n"));
4895 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4896 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
4898 opts
->everyone
= B_TRUE
;
4901 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
4902 } else if (argc
== 1 && !un
) {
4903 opts
->prt_perms
= B_TRUE
;
4904 opts
->dataset
= argv
[argc
-1];
4906 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
4907 opts
->who
= argv
[0];
4910 if (!opts
->local
&& !opts
->descend
) {
4911 opts
->local
= B_TRUE
;
4912 opts
->descend
= B_TRUE
;
4917 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
4918 const char *who
, char *perms
, nvlist_t
*top_nvl
)
4921 char ld
[2] = { '\0', '\0' };
4922 char who_buf
[MAXNAMELEN
+ 32];
4923 char base_type
= '\0';
4924 char set_type
= '\0';
4925 nvlist_t
*base_nvl
= NULL
;
4926 nvlist_t
*set_nvl
= NULL
;
4929 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4931 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4935 case ZFS_DELEG_NAMED_SET_SETS
:
4936 case ZFS_DELEG_NAMED_SET
:
4937 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
4938 base_type
= ZFS_DELEG_NAMED_SET
;
4939 ld
[0] = ZFS_DELEG_NA
;
4941 case ZFS_DELEG_CREATE_SETS
:
4942 case ZFS_DELEG_CREATE
:
4943 set_type
= ZFS_DELEG_CREATE_SETS
;
4944 base_type
= ZFS_DELEG_CREATE
;
4945 ld
[0] = ZFS_DELEG_NA
;
4947 case ZFS_DELEG_USER_SETS
:
4948 case ZFS_DELEG_USER
:
4949 set_type
= ZFS_DELEG_USER_SETS
;
4950 base_type
= ZFS_DELEG_USER
;
4952 ld
[0] = ZFS_DELEG_LOCAL
;
4954 ld
[1] = ZFS_DELEG_DESCENDENT
;
4956 case ZFS_DELEG_GROUP_SETS
:
4957 case ZFS_DELEG_GROUP
:
4958 set_type
= ZFS_DELEG_GROUP_SETS
;
4959 base_type
= ZFS_DELEG_GROUP
;
4961 ld
[0] = ZFS_DELEG_LOCAL
;
4963 ld
[1] = ZFS_DELEG_DESCENDENT
;
4965 case ZFS_DELEG_EVERYONE_SETS
:
4966 case ZFS_DELEG_EVERYONE
:
4967 set_type
= ZFS_DELEG_EVERYONE_SETS
;
4968 base_type
= ZFS_DELEG_EVERYONE
;
4970 ld
[0] = ZFS_DELEG_LOCAL
;
4972 ld
[1] = ZFS_DELEG_DESCENDENT
;
4976 assert(set_type
!= '\0' && base_type
!= '\0');
4979 if (perms
!= NULL
) {
4981 char *end
= curr
+ strlen(perms
);
4983 while (curr
< end
) {
4984 char *delim
= strchr(curr
, ',');
4995 (void) nvlist_add_boolean(nvl
, curr
);
5001 for (i
= 0; i
< 2; i
++) {
5002 char locality
= ld
[i
];
5006 if (!nvlist_empty(base_nvl
)) {
5008 (void) snprintf(who_buf
,
5009 sizeof (who_buf
), "%c%c$%s",
5010 base_type
, locality
, who
);
5012 (void) snprintf(who_buf
,
5013 sizeof (who_buf
), "%c%c$",
5014 base_type
, locality
);
5016 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5021 if (!nvlist_empty(set_nvl
)) {
5023 (void) snprintf(who_buf
,
5024 sizeof (who_buf
), "%c%c$%s",
5025 set_type
, locality
, who
);
5027 (void) snprintf(who_buf
,
5028 sizeof (who_buf
), "%c%c$",
5029 set_type
, locality
);
5031 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5036 for (i
= 0; i
< 2; i
++) {
5037 char locality
= ld
[i
];
5042 (void) snprintf(who_buf
, sizeof (who_buf
),
5043 "%c%c$%s", base_type
, locality
, who
);
5045 (void) snprintf(who_buf
, sizeof (who_buf
),
5046 "%c%c$", base_type
, locality
);
5047 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5050 (void) snprintf(who_buf
, sizeof (who_buf
),
5051 "%c%c$%s", set_type
, locality
, who
);
5053 (void) snprintf(who_buf
, sizeof (who_buf
),
5054 "%c%c$", set_type
, locality
);
5055 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5061 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
5063 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
5067 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
5068 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
5069 } else if (opts
->create
) {
5070 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
5071 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5072 } else if (opts
->everyone
) {
5073 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
5074 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5076 char *curr
= opts
->who
;
5077 char *end
= curr
+ strlen(curr
);
5079 while (curr
< end
) {
5081 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
5083 char *delim
= strchr(curr
, ',');
5086 struct passwd
*p
= NULL
;
5087 struct group
*g
= NULL
;
5095 rid
= (uid_t
)strtol(curr
, &endch
, 0);
5097 who_type
= ZFS_DELEG_USER
;
5106 (void) snprintf(errbuf
, 256, gettext(
5107 "invalid user %s"), curr
);
5108 allow_usage(un
, B_TRUE
, errbuf
);
5110 } else if (opts
->group
) {
5111 who_type
= ZFS_DELEG_GROUP
;
5120 (void) snprintf(errbuf
, 256, gettext(
5121 "invalid group %s"), curr
);
5122 allow_usage(un
, B_TRUE
, errbuf
);
5125 if (*endch
!= '\0') {
5132 if (*endch
!= '\0') {
5140 who_type
= ZFS_DELEG_USER
;
5142 } else if (g
!= NULL
) {
5143 who_type
= ZFS_DELEG_GROUP
;
5146 (void) snprintf(errbuf
, 256, gettext(
5147 "invalid user/group %s"), curr
);
5148 allow_usage(un
, B_TRUE
, errbuf
);
5152 (void) sprintf(id
, "%u", rid
);
5155 store_allow_perm(who_type
, opts
->local
,
5156 opts
->descend
, who
, opts
->perms
, *nvlp
);
5165 print_set_creat_perms(uu_avl_t
*who_avl
)
5167 const char *sc_title
[] = {
5168 gettext("Permission sets:\n"),
5169 gettext("Create time permissions:\n"),
5172 const char **title_ptr
= sc_title
;
5173 who_perm_node_t
*who_node
= NULL
;
5174 int prev_weight
= -1;
5176 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
5177 who_node
= uu_avl_next(who_avl
, who_node
)) {
5178 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5179 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5180 const char *who_name
= who_node
->who_perm
.who_name
;
5181 int weight
= who_type2weight(who_type
);
5182 boolean_t first
= B_TRUE
;
5183 deleg_perm_node_t
*deleg_node
;
5185 if (prev_weight
!= weight
) {
5186 (void) printf(*title_ptr
++);
5187 prev_weight
= weight
;
5190 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
5191 (void) printf("\t");
5193 (void) printf("\t%s ", who_name
);
5195 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
5196 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5199 deleg_node
->dpn_perm
.dp_name
);
5202 (void) printf(",%s",
5203 deleg_node
->dpn_perm
.dp_name
);
5206 (void) printf("\n");
5211 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
5214 who_perm_node_t
*who_node
= NULL
;
5215 boolean_t prt_title
= B_TRUE
;
5216 uu_avl_walk_t
*walk
;
5218 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
5221 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
5222 const char *who_name
= who_node
->who_perm
.who_name
;
5223 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
5224 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5225 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5227 deleg_perm_node_t
*deleg_node
;
5228 boolean_t prt_who
= B_TRUE
;
5230 for (deleg_node
= uu_avl_first(avl
);
5232 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5233 if (local
!= deleg_node
->dpn_perm
.dp_local
||
5234 descend
!= deleg_node
->dpn_perm
.dp_descend
)
5238 const char *who
= NULL
;
5240 prt_title
= B_FALSE
;
5241 (void) printf(title
);
5245 case ZFS_DELEG_USER_SETS
:
5246 case ZFS_DELEG_USER
:
5247 who
= gettext("user");
5249 who_name
= nice_who_name
;
5251 case ZFS_DELEG_GROUP_SETS
:
5252 case ZFS_DELEG_GROUP
:
5253 who
= gettext("group");
5255 who_name
= nice_who_name
;
5257 case ZFS_DELEG_EVERYONE_SETS
:
5258 case ZFS_DELEG_EVERYONE
:
5259 who
= gettext("everyone");
5264 assert(who
!= NULL
);
5268 if (who_name
== NULL
)
5269 (void) printf("\t%s", who
);
5271 (void) printf("\t%s %s", who
, who_name
);
5274 (void) printf("%c%s", delim
,
5275 deleg_node
->dpn_perm
.dp_name
);
5280 (void) printf("\n");
5283 uu_avl_walk_end(walk
);
5287 print_fs_perms(fs_perm_set_t
*fspset
)
5289 fs_perm_node_t
*node
= NULL
;
5290 char buf
[MAXNAMELEN
+ 32];
5291 const char *dsname
= buf
;
5293 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
5294 node
= uu_list_next(fspset
->fsps_list
, node
)) {
5295 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
5296 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
5299 (void) snprintf(buf
, sizeof (buf
),
5300 gettext("---- Permissions on %s "),
5301 node
->fspn_fsperm
.fsp_name
);
5302 (void) printf(dsname
);
5303 left
= 70 - strlen(buf
);
5306 (void) printf("\n");
5308 print_set_creat_perms(sc_avl
);
5309 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
5310 gettext("Local permissions:\n"));
5311 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
5312 gettext("Descendent permissions:\n"));
5313 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
5314 gettext("Local+Descendent permissions:\n"));
5318 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5320 struct deleg_perms
{
5326 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5328 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5329 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5331 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5334 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5338 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5341 nvlist_t
*perm_nvl
= NULL
;
5342 nvlist_t
*update_perm_nvl
= NULL
;
5345 struct allow_opts opts
= { 0 };
5347 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5350 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5353 opts
.local
= B_TRUE
;
5356 opts
.descend
= B_TRUE
;
5362 opts
.group
= B_TRUE
;
5365 opts
.everyone
= B_TRUE
;
5371 opts
.create
= B_TRUE
;
5374 opts
.recursive
= B_TRUE
;
5377 (void) fprintf(stderr
, gettext("missing argument for "
5378 "'%c' option\n"), optopt
);
5382 opts
.prt_usage
= B_TRUE
;
5385 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5394 /* check arguments */
5395 parse_allow_args(argc
, argv
, un
, &opts
);
5397 /* try to open the dataset */
5398 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5399 ZFS_TYPE_VOLUME
)) == NULL
) {
5400 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5405 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5408 fs_perm_set_init(&fs_perm_set
);
5409 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5410 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5415 print_fs_perms(&fs_perm_set
);
5417 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5418 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5421 if (un
&& opts
.recursive
) {
5422 struct deleg_perms data
= { un
, update_perm_nvl
};
5423 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5432 nvlist_free(perm_nvl
);
5433 nvlist_free(update_perm_nvl
);
5435 fs_perm_set_fini(&fs_perm_set
);
5443 zfs_do_allow(int argc
, char **argv
)
5445 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5449 zfs_do_unallow(int argc
, char **argv
)
5451 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5455 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5460 boolean_t recursive
= B_FALSE
;
5461 const char *opts
= holding
? "rt" : "r";
5465 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5471 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5480 /* check number of arguments */
5488 if (holding
&& tag
[0] == '.') {
5489 /* tags starting with '.' are reserved for libzfs */
5490 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5494 for (i
= 0; i
< argc
; ++i
) {
5496 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
5498 char *path
= argv
[i
];
5500 delim
= strchr(path
, '@');
5501 if (delim
== NULL
) {
5502 (void) fprintf(stderr
,
5503 gettext("'%s' is not a snapshot\n"), path
);
5507 (void) strncpy(parent
, path
, delim
- path
);
5508 parent
[delim
- path
] = '\0';
5510 zhp
= zfs_open(g_zfs
, parent
,
5511 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5517 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
5520 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5526 return (errors
!= 0);
5530 * zfs hold [-r] [-t] <tag> <snap> ...
5532 * -r Recursively hold
5534 * Apply a user-hold with the given tag to the list of snapshots.
5537 zfs_do_hold(int argc
, char **argv
)
5539 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5543 * zfs release [-r] <tag> <snap> ...
5545 * -r Recursively release
5547 * Release a user-hold with the given tag from the list of snapshots.
5550 zfs_do_release(int argc
, char **argv
)
5552 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5555 typedef struct holds_cbdata
{
5556 boolean_t cb_recursive
;
5557 const char *cb_snapname
;
5559 size_t cb_max_namelen
;
5560 size_t cb_max_taglen
;
5563 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5564 #define DATETIME_BUF_LEN (32)
5569 print_holds(boolean_t scripted
, size_t nwidth
, size_t tagwidth
, nvlist_t
*nvl
)
5572 nvpair_t
*nvp
= NULL
;
5573 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5577 for (i
= 0; i
< 3; i
++) {
5578 col
= gettext(hdr_cols
[i
]);
5580 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5583 (void) printf("%s\n", col
);
5587 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5588 char *zname
= nvpair_name(nvp
);
5590 nvpair_t
*nvp2
= NULL
;
5591 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5592 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5593 char tsbuf
[DATETIME_BUF_LEN
];
5594 char *tagname
= nvpair_name(nvp2
);
5598 char sep
= scripted
? '\t' : ' ';
5599 size_t sepnum
= scripted
? 1 : 2;
5601 (void) nvpair_value_uint64(nvp2
, &val
);
5603 (void) localtime_r(&time
, &t
);
5604 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5605 gettext(STRFTIME_FMT_STR
), &t
);
5607 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth
, zname
,
5608 sepnum
, sep
, tagwidth
, tagname
, sepnum
, sep
, tsbuf
);
5614 * Generic callback function to list a dataset or snapshot.
5617 holds_callback(zfs_handle_t
*zhp
, void *data
)
5619 holds_cbdata_t
*cbp
= data
;
5620 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5621 nvlist_t
*nvl
= NULL
;
5622 nvpair_t
*nvp
= NULL
;
5623 const char *zname
= zfs_get_name(zhp
);
5624 size_t znamelen
= strlen(zname
);
5626 if (cbp
->cb_recursive
) {
5627 const char *snapname
;
5628 char *delim
= strchr(zname
, '@');
5632 snapname
= delim
+ 1;
5633 if (strcmp(cbp
->cb_snapname
, snapname
))
5637 if (zfs_get_holds(zhp
, &nvl
) != 0)
5640 if (znamelen
> cbp
->cb_max_namelen
)
5641 cbp
->cb_max_namelen
= znamelen
;
5643 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5644 const char *tag
= nvpair_name(nvp
);
5645 size_t taglen
= strlen(tag
);
5646 if (taglen
> cbp
->cb_max_taglen
)
5647 cbp
->cb_max_taglen
= taglen
;
5650 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
5654 * zfs holds [-r] <snap> ...
5656 * -r Recursively hold
5659 zfs_do_holds(int argc
, char **argv
)
5664 boolean_t scripted
= B_FALSE
;
5665 boolean_t recursive
= B_FALSE
;
5666 const char *opts
= "rH";
5669 int types
= ZFS_TYPE_SNAPSHOT
;
5670 holds_cbdata_t cb
= { 0 };
5677 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5686 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5693 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
5694 flags
|= ZFS_ITER_RECURSE
;
5700 /* check number of arguments */
5704 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5707 for (i
= 0; i
< argc
; ++i
) {
5708 char *snapshot
= argv
[i
];
5710 const char *snapname
;
5712 delim
= strchr(snapshot
, '@');
5713 if (delim
== NULL
) {
5714 (void) fprintf(stderr
,
5715 gettext("'%s' is not a snapshot\n"), snapshot
);
5719 snapname
= delim
+ 1;
5721 snapshot
[delim
- snapshot
] = '\0';
5723 cb
.cb_recursive
= recursive
;
5724 cb
.cb_snapname
= snapname
;
5728 * 1. collect holds data, set format options
5730 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
5731 holds_callback
, &cb
);
5737 * 2. print holds data
5739 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
5741 if (nvlist_empty(nvl
))
5742 (void) printf(gettext("no datasets available\n"));
5746 return (0 != errors
);
5749 #define CHECK_SPINNER 30
5750 #define SPINNER_TIME 3 /* seconds */
5751 #define MOUNT_TIME 5 /* seconds */
5754 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
5756 static char *spin
[] = { "-", "\\", "|", "/" };
5757 static int spinval
= 0;
5758 static int spincheck
= 0;
5759 static time_t last_spin_time
= (time_t)0;
5760 get_all_cb_t
*cbp
= data
;
5761 zfs_type_t type
= zfs_get_type(zhp
);
5763 if (cbp
->cb_verbose
) {
5764 if (--spincheck
< 0) {
5765 time_t now
= time(NULL
);
5766 if (last_spin_time
+ SPINNER_TIME
< now
) {
5767 update_progress(spin
[spinval
++ % 4]);
5768 last_spin_time
= now
;
5770 spincheck
= CHECK_SPINNER
;
5775 * Interate over any nested datasets.
5777 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
5783 * Skip any datasets whose type does not match.
5785 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
5789 libzfs_add_handle(cbp
, zhp
);
5790 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
5796 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
5798 get_all_cb_t cb
= { 0 };
5799 cb
.cb_verbose
= verbose
;
5800 cb
.cb_getone
= get_one_dataset
;
5803 set_progress_header(gettext("Reading ZFS config"));
5804 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
5806 *dslist
= cb
.cb_handles
;
5807 *count
= cb
.cb_used
;
5810 finish_progress(gettext("done."));
5814 * Generic callback for sharing or mounting filesystems. Because the code is so
5815 * similar, we have a common function with an extra parameter to determine which
5816 * mode we are using.
5818 #define OP_SHARE 0x1
5819 #define OP_MOUNT 0x2
5822 * Share or mount a dataset.
5825 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
5826 boolean_t
explicit, const char *options
)
5828 char mountpoint
[ZFS_MAXPROPLEN
];
5829 char shareopts
[ZFS_MAXPROPLEN
];
5830 char smbshareopts
[ZFS_MAXPROPLEN
];
5831 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
5833 uint64_t zoned
, canmount
;
5834 boolean_t shared_nfs
, shared_smb
;
5836 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
5839 * Check to make sure we can mount/share this dataset. If we
5840 * are in the global zone and the filesystem is exported to a
5841 * local zone, or if we are in a local zone and the
5842 * filesystem is not exported, then it is an error.
5844 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
5846 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
5850 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5851 "dataset is exported to a local zone\n"), cmdname
,
5855 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
5859 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5860 "permission denied\n"), cmdname
,
5866 * Ignore any filesystems which don't apply to us. This
5867 * includes those with a legacy mountpoint, or those with
5868 * legacy share options.
5870 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
5871 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
5872 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
5873 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5874 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
5875 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
5877 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
5878 strcmp(smbshareopts
, "off") == 0) {
5882 (void) fprintf(stderr
, gettext("cannot share '%s': "
5883 "legacy share\n"), zfs_get_name(zhp
));
5884 (void) fprintf(stderr
, gettext("use share(1M) to "
5885 "share this filesystem, or set "
5886 "sharenfs property on\n"));
5891 * We cannot share or mount legacy filesystems. If the
5892 * shareopts is non-legacy but the mountpoint is legacy, we
5893 * treat it as a legacy share.
5895 if (strcmp(mountpoint
, "legacy") == 0) {
5899 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5900 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
5901 (void) fprintf(stderr
, gettext("use %s(1M) to "
5902 "%s this filesystem\n"), cmdname
, cmdname
);
5906 if (strcmp(mountpoint
, "none") == 0) {
5910 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
5911 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
5916 * canmount explicit outcome
5917 * on no pass through
5918 * on yes pass through
5920 * off yes display error, return 1
5921 * noauto no return 0
5922 * noauto yes pass through
5924 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
5925 if (canmount
== ZFS_CANMOUNT_OFF
) {
5929 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5930 "'canmount' property is set to 'off'\n"), cmdname
,
5933 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
5938 * If this filesystem is inconsistent and has a receive resume
5939 * token, we can not mount it.
5941 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
5942 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
5943 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
5947 (void) fprintf(stderr
, gettext("cannot %s '%s': "
5948 "Contains partially-completed state from "
5949 "\"zfs receive -r\", which can be resumed with "
5950 "\"zfs send -t\"\n"),
5951 cmdname
, zfs_get_name(zhp
));
5956 * At this point, we have verified that the mountpoint and/or
5957 * shareopts are appropriate for auto management. If the
5958 * filesystem is already mounted or shared, return (failing
5959 * for explicit requests); otherwise mount or share the
5965 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
5966 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
5968 if ((shared_nfs
&& shared_smb
) ||
5969 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
5970 strcmp(smbshareopts
, "off") == 0) ||
5971 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
5972 strcmp(shareopts
, "off") == 0)) {
5976 (void) fprintf(stderr
, gettext("cannot share "
5977 "'%s': filesystem already shared\n"),
5982 if (!zfs_is_mounted(zhp
, NULL
) &&
5983 zfs_mount(zhp
, NULL
, 0) != 0)
5986 if (protocol
== NULL
) {
5987 if (zfs_shareall(zhp
) != 0)
5989 } else if (strcmp(protocol
, "nfs") == 0) {
5990 if (zfs_share_nfs(zhp
))
5992 } else if (strcmp(protocol
, "smb") == 0) {
5993 if (zfs_share_smb(zhp
))
5996 (void) fprintf(stderr
, gettext("cannot share "
5997 "'%s': invalid share type '%s' "
5999 zfs_get_name(zhp
), protocol
);
6006 if (options
== NULL
)
6007 mnt
.mnt_mntopts
= "";
6009 mnt
.mnt_mntopts
= (char *)options
;
6011 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
6012 zfs_is_mounted(zhp
, NULL
)) {
6016 (void) fprintf(stderr
, gettext("cannot mount "
6017 "'%s': filesystem already mounted\n"),
6022 if (zfs_mount(zhp
, options
, flags
) != 0)
6031 * Reports progress in the form "(current/total)". Not thread-safe.
6034 report_mount_progress(int current
, int total
)
6036 static time_t last_progress_time
= 0;
6037 time_t now
= time(NULL
);
6040 /* report 1..n instead of 0..n-1 */
6043 /* display header if we're here for the first time */
6045 set_progress_header(gettext("Mounting ZFS filesystems"));
6046 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
6047 /* too soon to report again */
6051 last_progress_time
= now
;
6053 (void) sprintf(info
, "(%d/%d)", current
, total
);
6055 if (current
== total
)
6056 finish_progress(info
);
6058 update_progress(info
);
6062 append_options(char *mntopts
, char *newopts
)
6064 int len
= strlen(mntopts
);
6066 /* original length plus new string to append plus 1 for the comma */
6067 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
6068 (void) fprintf(stderr
, gettext("the opts argument for "
6069 "'%c' option is too long (more than %d chars)\n"),
6070 "-o", MNT_LINE_MAX
);
6075 mntopts
[len
++] = ',';
6077 (void) strcpy(&mntopts
[len
], newopts
);
6081 share_mount(int op
, int argc
, char **argv
)
6084 boolean_t verbose
= B_FALSE
;
6086 char *options
= NULL
;
6090 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
6100 if (*optarg
== '\0') {
6101 (void) fprintf(stderr
, gettext("empty mount "
6102 "options (-o) specified\n"));
6106 if (options
== NULL
)
6107 options
= safe_malloc(MNT_LINE_MAX
+ 1);
6109 /* option validation is done later */
6110 append_options(options
, optarg
);
6114 flags
|= MS_OVERLAY
;
6117 (void) fprintf(stderr
, gettext("missing argument for "
6118 "'%c' option\n"), optopt
);
6122 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6131 /* check number of arguments */
6133 zfs_handle_t
**dslist
= NULL
;
6134 size_t i
, count
= 0;
6135 char *protocol
= NULL
;
6137 if (op
== OP_SHARE
&& argc
> 0) {
6138 if (strcmp(argv
[0], "nfs") != 0 &&
6139 strcmp(argv
[0], "smb") != 0) {
6140 (void) fprintf(stderr
, gettext("share type "
6141 "must be 'nfs' or 'smb'\n"));
6150 (void) fprintf(stderr
, gettext("too many arguments\n"));
6154 start_progress_timer();
6155 get_all_datasets(&dslist
, &count
, verbose
);
6160 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
6161 sa_init_selective_arg_t sharearg
;
6162 sharearg
.zhandle_arr
= dslist
;
6163 sharearg
.zhandle_len
= count
;
6164 if ((ret
= zfs_init_libshare_arg(zfs_get_handle(dslist
[0]),
6165 SA_INIT_SHARE_API_SELECTIVE
, &sharearg
)) != SA_OK
) {
6166 (void) fprintf(stderr
,
6167 gettext("Could not initialize libshare, %d"), ret
);
6171 for (i
= 0; i
< count
; i
++) {
6173 report_mount_progress(i
, count
);
6175 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
6176 B_FALSE
, options
) != 0)
6178 zfs_close(dslist
[i
]);
6182 } else if (argc
== 0) {
6183 struct mnttab entry
;
6185 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
6186 (void) fprintf(stderr
, gettext("missing filesystem "
6187 "argument (specify -a for all)\n"));
6192 * When mount is given no arguments, go through /etc/mnttab and
6193 * display any active ZFS mounts. We hide any snapshots, since
6194 * they are controlled automatically.
6196 rewind(mnttab_file
);
6197 while (getmntent(mnttab_file
, &entry
) == 0) {
6198 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
6199 strchr(entry
.mnt_special
, '@') != NULL
)
6202 (void) printf("%-30s %s\n", entry
.mnt_special
,
6210 (void) fprintf(stderr
,
6211 gettext("too many arguments\n"));
6215 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6216 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6219 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
6229 * zfs mount -a [nfs]
6230 * zfs mount filesystem
6232 * Mount all filesystems, or mount the given filesystem.
6235 zfs_do_mount(int argc
, char **argv
)
6237 return (share_mount(OP_MOUNT
, argc
, argv
));
6241 * zfs share -a [nfs | smb]
6242 * zfs share filesystem
6244 * Share all filesystems, or share the given filesystem.
6247 zfs_do_share(int argc
, char **argv
)
6249 return (share_mount(OP_SHARE
, argc
, argv
));
6252 typedef struct unshare_unmount_node
{
6253 zfs_handle_t
*un_zhp
;
6255 uu_avl_node_t un_avlnode
;
6256 } unshare_unmount_node_t
;
6260 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
6262 const unshare_unmount_node_t
*l
= larg
;
6263 const unshare_unmount_node_t
*r
= rarg
;
6265 return (strcmp(l
->un_mountp
, r
->un_mountp
));
6269 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6270 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6271 * and unmount it appropriately.
6274 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
6278 struct stat64 statbuf
;
6279 struct extmnttab entry
;
6280 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
6284 * Search for the path in /etc/mnttab. Rather than looking for the
6285 * specific path, which can be fooled by non-standard paths (i.e. ".."
6286 * or "//"), we stat() the path and search for the corresponding
6287 * (major,minor) device pair.
6289 if (stat64(path
, &statbuf
) != 0) {
6290 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6291 cmdname
, path
, strerror(errno
));
6294 path_inode
= statbuf
.st_ino
;
6297 * Search for the given (major,minor) pair in the mount table.
6299 rewind(mnttab_file
);
6300 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
6301 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
6302 entry
.mnt_minor
== minor(statbuf
.st_dev
))
6306 if (op
== OP_SHARE
) {
6307 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
6308 "currently mounted\n"), cmdname
, path
);
6311 (void) fprintf(stderr
, gettext("warning: %s not in mnttab\n"),
6313 if ((ret
= umount2(path
, flags
)) != 0)
6314 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
6319 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
6320 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
6321 "filesystem\n"), cmdname
, path
);
6325 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6326 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6330 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
6331 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6332 cmdname
, path
, strerror(errno
));
6334 } else if (statbuf
.st_ino
!= path_inode
) {
6335 (void) fprintf(stderr
, gettext("cannot "
6336 "%s '%s': not a mountpoint\n"), cmdname
, path
);
6340 if (op
== OP_SHARE
) {
6341 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6342 char smbshare_prop
[ZFS_MAXPROPLEN
];
6344 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
6345 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6346 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
6347 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6349 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6350 strcmp(smbshare_prop
, "off") == 0) {
6351 (void) fprintf(stderr
, gettext("cannot unshare "
6352 "'%s': legacy share\n"), path
);
6353 (void) fprintf(stderr
, gettext("use "
6354 "unshare(1M) to unshare this filesystem\n"));
6355 } else if (!zfs_is_shared(zhp
)) {
6356 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6357 "not currently shared\n"), path
);
6359 ret
= zfs_unshareall_bypath(zhp
, path
);
6362 char mtpt_prop
[ZFS_MAXPROPLEN
];
6364 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6365 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6368 ret
= zfs_unmount(zhp
, NULL
, flags
);
6369 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6370 (void) fprintf(stderr
, gettext("cannot unmount "
6371 "'%s': legacy mountpoint\n"),
6373 (void) fprintf(stderr
, gettext("use umount(1M) "
6374 "to unmount this filesystem\n"));
6376 ret
= zfs_unmountall(zhp
, flags
);
6387 * Generic callback for unsharing or unmounting a filesystem.
6390 unshare_unmount(int op
, int argc
, char **argv
)
6397 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6398 char sharesmb
[ZFS_MAXPROPLEN
];
6401 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
6410 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6421 * We could make use of zfs_for_each() to walk all datasets in
6422 * the system, but this would be very inefficient, especially
6423 * since we would have to linearly search /etc/mnttab for each
6424 * one. Instead, do one pass through /etc/mnttab looking for
6425 * zfs entries and call zfs_unmount() for each one.
6427 * Things get a little tricky if the administrator has created
6428 * mountpoints beneath other ZFS filesystems. In this case, we
6429 * have to unmount the deepest filesystems first. To accomplish
6430 * this, we place all the mountpoints in an AVL tree sorted by
6431 * the special type (dataset name), and walk the result in
6432 * reverse to make sure to get any snapshots first.
6434 struct mnttab entry
;
6435 uu_avl_pool_t
*pool
;
6436 uu_avl_t
*tree
= NULL
;
6437 unshare_unmount_node_t
*node
;
6439 uu_avl_walk_t
*walk
;
6442 (void) fprintf(stderr
, gettext("too many arguments\n"));
6446 if (((pool
= uu_avl_pool_create("unmount_pool",
6447 sizeof (unshare_unmount_node_t
),
6448 offsetof(unshare_unmount_node_t
, un_avlnode
),
6449 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6450 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6453 rewind(mnttab_file
);
6454 while (getmntent(mnttab_file
, &entry
) == 0) {
6456 /* ignore non-ZFS entries */
6457 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6460 /* ignore snapshots */
6461 if (strchr(entry
.mnt_special
, '@') != NULL
)
6464 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6465 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6471 * Ignore datasets that are excluded/restricted by
6474 if (zpool_skip_pool(zfs_get_pool_name(zhp
))) {
6481 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6483 sizeof (nfs_mnt_prop
),
6484 NULL
, NULL
, 0, B_FALSE
) == 0);
6485 if (strcmp(nfs_mnt_prop
, "off") != 0)
6487 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6489 sizeof (nfs_mnt_prop
),
6490 NULL
, NULL
, 0, B_FALSE
) == 0);
6491 if (strcmp(nfs_mnt_prop
, "off") == 0)
6495 /* Ignore legacy mounts */
6496 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6498 sizeof (nfs_mnt_prop
),
6499 NULL
, NULL
, 0, B_FALSE
) == 0);
6500 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6502 /* Ignore canmount=noauto mounts */
6503 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6504 ZFS_CANMOUNT_NOAUTO
)
6510 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6512 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6514 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6516 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6517 uu_avl_insert(tree
, node
, idx
);
6519 zfs_close(node
->un_zhp
);
6520 free(node
->un_mountp
);
6526 * Walk the AVL tree in reverse, unmounting each filesystem and
6527 * removing it from the AVL tree in the process.
6529 if ((walk
= uu_avl_walk_start(tree
,
6530 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6533 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6534 uu_avl_remove(tree
, node
);
6538 if (zfs_unshareall_bypath(node
->un_zhp
,
6539 node
->un_mountp
) != 0)
6544 if (zfs_unmount(node
->un_zhp
,
6545 node
->un_mountp
, flags
) != 0)
6550 zfs_close(node
->un_zhp
);
6551 free(node
->un_mountp
);
6555 uu_avl_walk_end(walk
);
6556 uu_avl_destroy(tree
);
6557 uu_avl_pool_destroy(pool
);
6562 (void) fprintf(stderr
,
6563 gettext("missing filesystem argument\n"));
6565 (void) fprintf(stderr
,
6566 gettext("too many arguments\n"));
6571 * We have an argument, but it may be a full path or a ZFS
6572 * filesystem. Pass full paths off to unmount_path() (shared by
6573 * manual_unmount), otherwise open the filesystem and pass to
6576 if (argv
[0][0] == '/')
6577 return (unshare_unmount_path(op
, argv
[0],
6580 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6581 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6584 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
6585 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
6586 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
6587 NULL
, 0, B_FALSE
) == 0);
6591 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6593 sizeof (nfs_mnt_prop
),
6594 NULL
, NULL
, 0, B_FALSE
) == 0);
6595 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6596 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
6599 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6600 strcmp(sharesmb
, "off") == 0) {
6601 (void) fprintf(stderr
, gettext("cannot "
6602 "unshare '%s': legacy share\n"),
6604 (void) fprintf(stderr
, gettext("use "
6605 "unshare(1M) to unshare this "
6608 } else if (!zfs_is_shared(zhp
)) {
6609 (void) fprintf(stderr
, gettext("cannot "
6610 "unshare '%s': not currently "
6611 "shared\n"), zfs_get_name(zhp
));
6613 } else if (zfs_unshareall(zhp
) != 0) {
6619 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
6620 (void) fprintf(stderr
, gettext("cannot "
6621 "unmount '%s': legacy "
6622 "mountpoint\n"), zfs_get_name(zhp
));
6623 (void) fprintf(stderr
, gettext("use "
6624 "umount(1M) to unmount this "
6627 } else if (!zfs_is_mounted(zhp
, NULL
)) {
6628 (void) fprintf(stderr
, gettext("cannot "
6629 "unmount '%s': not currently "
6633 } else if (zfs_unmountall(zhp
, flags
) != 0) {
6647 * zfs unmount filesystem
6649 * Unmount all filesystems, or a specific ZFS filesystem.
6652 zfs_do_unmount(int argc
, char **argv
)
6654 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
6659 * zfs unshare filesystem
6661 * Unshare all filesystems, or a specific ZFS filesystem.
6664 zfs_do_unshare(int argc
, char **argv
)
6666 return (unshare_unmount(OP_SHARE
, argc
, argv
));
6670 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6671 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6674 manual_mount(int argc
, char **argv
)
6677 char mountpoint
[ZFS_MAXPROPLEN
];
6678 char mntopts
[MNT_LINE_MAX
] = { '\0' };
6682 char *dataset
, *path
;
6685 while ((c
= getopt(argc
, argv
, ":mo:O")) != -1) {
6688 (void) strlcpy(mntopts
, optarg
, sizeof (mntopts
));
6691 flags
|= MS_OVERLAY
;
6694 flags
|= MS_NOMNTTAB
;
6697 (void) fprintf(stderr
, gettext("missing argument for "
6698 "'%c' option\n"), optopt
);
6702 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6704 (void) fprintf(stderr
, gettext("usage: mount [-o opts] "
6713 /* check that we only have two arguments */
6716 (void) fprintf(stderr
, gettext("missing dataset "
6719 (void) fprintf(stderr
,
6720 gettext("missing mountpoint argument\n"));
6722 (void) fprintf(stderr
, gettext("too many arguments\n"));
6723 (void) fprintf(stderr
, "usage: mount <dataset> <mountpoint>\n");
6730 /* try to open the dataset */
6731 if ((zhp
= zfs_open(g_zfs
, dataset
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6734 (void) zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
6735 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
);
6737 /* check for legacy mountpoint and complain appropriately */
6739 if (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) == 0) {
6740 if (mount(dataset
, path
, MS_OPTIONSTR
| flags
, MNTTYPE_ZFS
,
6741 NULL
, 0, mntopts
, sizeof (mntopts
)) != 0) {
6742 (void) fprintf(stderr
, gettext("mount failed: %s\n"),
6747 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
6748 "mounted using 'mount -F zfs'\n"), dataset
);
6749 (void) fprintf(stderr
, gettext("Use 'zfs set mountpoint=%s' "
6750 "instead.\n"), path
);
6751 (void) fprintf(stderr
, gettext("If you must use 'mount -F zfs' "
6752 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
6753 (void) fprintf(stderr
, gettext("See zfs(1M) for more "
6762 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6763 * unmounts of non-legacy filesystems, as this is the dominant administrative
6767 manual_unmount(int argc
, char **argv
)
6773 while ((c
= getopt(argc
, argv
, "f")) != -1) {
6779 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6781 (void) fprintf(stderr
, gettext("usage: unmount [-f] "
6790 /* check arguments */
6793 (void) fprintf(stderr
, gettext("missing path "
6796 (void) fprintf(stderr
, gettext("too many arguments\n"));
6797 (void) fprintf(stderr
, gettext("usage: unmount [-f] <path>\n"));
6801 return (unshare_unmount_path(OP_MOUNT
, argv
[0], flags
, B_TRUE
));
6805 find_command_idx(char *command
, int *idx
)
6809 for (i
= 0; i
< NCOMMAND
; i
++) {
6810 if (command_table
[i
].name
== NULL
)
6813 if (strcmp(command
, command_table
[i
].name
) == 0) {
6822 zfs_do_diff(int argc
, char **argv
)
6826 char *tosnap
= NULL
;
6827 char *fromsnap
= NULL
;
6832 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
6835 flags
|= ZFS_DIFF_CLASSIFY
;
6838 flags
|= ZFS_DIFF_PARSEABLE
;
6841 flags
|= ZFS_DIFF_TIMESTAMP
;
6844 (void) fprintf(stderr
,
6845 gettext("invalid option '%c'\n"), optopt
);
6854 (void) fprintf(stderr
,
6855 gettext("must provide at least one snapshot name\n"));
6860 (void) fprintf(stderr
, gettext("too many arguments\n"));
6865 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
6868 if (*fromsnap
!= '@')
6869 copy
= strdup(fromsnap
);
6871 copy
= strdup(tosnap
);
6875 if ((atp
= strchr(copy
, '@')) != NULL
)
6878 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
6884 * Ignore SIGPIPE so that the library can give us
6885 * information on any failure
6887 (void) sigignore(SIGPIPE
);
6889 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
6897 * zfs bookmark <fs@snap> <fs#bmark>
6899 * Creates a bookmark with the given name from the given snapshot.
6902 zfs_do_bookmark(int argc
, char **argv
)
6904 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
6911 while ((c
= getopt(argc
, argv
, "")) != -1) {
6914 (void) fprintf(stderr
,
6915 gettext("invalid option '%c'\n"), optopt
);
6923 /* check number of arguments */
6925 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
6929 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
6933 if (strchr(argv
[1], '#') == NULL
) {
6934 (void) fprintf(stderr
,
6935 gettext("invalid bookmark name '%s' -- "
6936 "must contain a '#'\n"), argv
[1]);
6940 if (argv
[0][0] == '@') {
6942 * Snapshot name begins with @.
6943 * Default to same fs as bookmark.
6945 (void) strncpy(snapname
, argv
[1], sizeof (snapname
));
6946 *strchr(snapname
, '#') = '\0';
6947 (void) strlcat(snapname
, argv
[0], sizeof (snapname
));
6949 (void) strncpy(snapname
, argv
[0], sizeof (snapname
));
6951 zhp
= zfs_open(g_zfs
, snapname
, ZFS_TYPE_SNAPSHOT
);
6957 nvl
= fnvlist_alloc();
6958 fnvlist_add_string(nvl
, argv
[1], snapname
);
6959 ret
= lzc_bookmark(nvl
, NULL
);
6963 const char *err_msg
;
6966 (void) snprintf(errbuf
, sizeof (errbuf
),
6967 dgettext(TEXT_DOMAIN
,
6968 "cannot create bookmark '%s'"), argv
[1]);
6972 err_msg
= "bookmark is in a different pool";
6975 err_msg
= "bookmark exists";
6978 err_msg
= "invalid argument";
6981 err_msg
= "bookmark feature not enabled";
6984 err_msg
= "out of space";
6987 err_msg
= "unknown error";
6990 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
6991 dgettext(TEXT_DOMAIN
, err_msg
));
7002 zfs_do_channel_program(int argc
, char **argv
)
7006 char *progbuf
, *filename
, *poolname
;
7007 size_t progsize
, progread
;
7009 uint64_t instrlimit
= ZCP_DEFAULT_INSTRLIMIT
;
7010 uint64_t memlimit
= ZCP_DEFAULT_MEMLIMIT
;
7011 zpool_handle_t
*zhp
;
7015 (c
= getopt(argc
, argv
, "t:(instr-limit)m:(memory-limit)"))) {
7023 arg
= strtoull(optarg
, &endp
, 0);
7024 if (errno
!= 0 || *endp
!= '\0') {
7025 (void) fprintf(stderr
, gettext(
7027 "'%s': expected integer\n"), optarg
);
7032 if (arg
> ZCP_MAX_INSTRLIMIT
|| arg
== 0) {
7033 (void) fprintf(stderr
, gettext(
7034 "Invalid instruction limit: "
7041 ASSERT3U(c
, ==, 'm');
7042 if (arg
> ZCP_MAX_MEMLIMIT
|| arg
== 0) {
7043 (void) fprintf(stderr
, gettext(
7044 "Invalid memory limit: "
7054 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7064 (void) fprintf(stderr
,
7065 gettext("invalid number of arguments\n"));
7071 if (strcmp(filename
, "-") == 0) {
7073 filename
= "standard input";
7074 } else if ((fd
= open(filename
, O_RDONLY
)) < 0) {
7075 (void) fprintf(stderr
, gettext("cannot open '%s': %s\n"),
7076 filename
, strerror(errno
));
7080 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
7081 (void) fprintf(stderr
, gettext("cannot open pool '%s'"),
7088 * Read in the channel program, expanding the program buffer as
7093 progbuf
= safe_malloc(progsize
);
7095 ret
= read(fd
, progbuf
+ progread
, progsize
- progread
);
7097 if (progread
== progsize
&& ret
> 0) {
7099 progbuf
= safe_realloc(progbuf
, progsize
);
7107 (void) fprintf(stderr
,
7108 gettext("cannot read '%s': %s\n"),
7109 filename
, strerror(errno
));
7112 progbuf
[progread
] = '\0';
7115 * Any remaining arguments are passed as arguments to the lua script as
7118 * "argv" -> [ "arg 1", ... "arg n" ],
7121 nvlist_t
*argnvl
= fnvlist_alloc();
7122 fnvlist_add_string_array(argnvl
, ZCP_ARG_CLIARGV
, argv
+ 2, argc
- 2);
7124 ret
= lzc_channel_program(poolname
, progbuf
, instrlimit
, memlimit
,
7129 * On error, report the error message handed back by lua if one
7130 * exists. Otherwise, generate an appropriate error message,
7131 * falling back on strerror() for an unexpected return code.
7133 char *errstring
= NULL
;
7134 if (nvlist_exists(outnvl
, ZCP_RET_ERROR
)) {
7135 (void) nvlist_lookup_string(outnvl
,
7136 ZCP_RET_ERROR
, &errstring
);
7137 if (errstring
== NULL
)
7138 errstring
= strerror(ret
);
7143 "Invalid instruction or memory limit.";
7146 errstring
= "Return value too large.";
7149 errstring
= "Memory limit exhausted.";
7152 errstring
= "Timed out.";
7155 errstring
= "Permission denied. Channel "
7156 "programs must be run as root.";
7159 errstring
= strerror(ret
);
7162 (void) fprintf(stderr
,
7163 gettext("Channel program execution failed:\n%s\n"),
7166 (void) printf("Channel program fully executed ");
7167 if (nvlist_empty(outnvl
)) {
7168 (void) printf("with no return value.\n");
7170 (void) printf("with return value:\n");
7171 dump_nvlist(outnvl
, 4);
7176 fnvlist_free(outnvl
);
7177 fnvlist_free(argnvl
);
7186 main(int argc
, char **argv
)
7193 (void) setlocale(LC_ALL
, "");
7194 (void) textdomain(TEXT_DOMAIN
);
7198 if ((g_zfs
= libzfs_init()) == NULL
) {
7199 (void) fprintf(stderr
, gettext("internal error: failed to "
7200 "initialize ZFS library\n"));
7204 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
7206 libzfs_print_on_error(g_zfs
, B_TRUE
);
7208 if ((mnttab_file
= fopen(MNTTAB
, "r")) == NULL
) {
7209 (void) fprintf(stderr
, gettext("internal error: unable to "
7210 "open %s\n"), MNTTAB
);
7215 * This command also doubles as the /etc/fs mount and unmount program.
7216 * Determine if we should take this behavior based on argv[0].
7218 progname
= basename(argv
[0]);
7219 if (strcmp(progname
, "mount") == 0) {
7220 ret
= manual_mount(argc
, argv
);
7221 } else if (strcmp(progname
, "umount") == 0) {
7222 ret
= manual_unmount(argc
, argv
);
7225 * Make sure the user has specified some command.
7228 (void) fprintf(stderr
, gettext("missing command\n"));
7235 * The 'umount' command is an alias for 'unmount'
7237 if (strcmp(cmdname
, "umount") == 0)
7238 cmdname
= "unmount";
7241 * The 'recv' command is an alias for 'receive'
7243 if (strcmp(cmdname
, "recv") == 0)
7244 cmdname
= "receive";
7247 * The 'snap' command is an alias for 'snapshot'
7249 if (strcmp(cmdname
, "snap") == 0)
7250 cmdname
= "snapshot";
7255 if (strcmp(cmdname
, "-?") == 0)
7259 * Run the appropriate command.
7261 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
7262 if (find_command_idx(cmdname
, &i
) == 0) {
7263 current_command
= &command_table
[i
];
7264 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
7265 } else if (strchr(cmdname
, '=') != NULL
) {
7266 verify(find_command_idx("set", &i
) == 0);
7267 current_command
= &command_table
[i
];
7268 ret
= command_table
[i
].func(argc
, argv
);
7270 (void) fprintf(stderr
, gettext("unrecognized "
7271 "command '%s'\n"), cmdname
);
7274 libzfs_mnttab_cache(g_zfs
, B_FALSE
);
7277 (void) fclose(mnttab_file
);
7279 if (ret
== 0 && log_history
)
7280 (void) zpool_log_history(g_zfs
, history_str
);
7285 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7286 * for the purposes of running ::findleaks.
7288 if (getenv("ZFS_ABORT") != NULL
) {
7289 (void) printf("dumping core by request\n");