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 2019 Joyent, Inc.
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.
32 * Copyright (c) 2018 Datto Inc.
33 * Copyright 2021 RackTop Systems, Inc.
43 #include <libnvpair.h>
55 #include <sys/debug.h>
57 #include <sys/sysmacros.h>
58 #include <sys/mkdev.h>
59 #include <sys/mntent.h>
60 #include <sys/mnttab.h>
61 #include <sys/mount.h>
63 #include <sys/fs/zfs.h>
64 #include <sys/types.h>
66 #include <sys/zfs_project.h>
70 #include <libzfs_core.h>
72 #include <zfs_deleg.h>
76 #include <directory.h>
82 #include "zfs_comutil.h"
83 #include "zfs_projectutil.h"
85 libzfs_handle_t
*g_zfs
;
87 static FILE *mnttab_file
;
88 static char history_str
[HIS_MAX_RECORD_LEN
];
89 static boolean_t log_history
= B_TRUE
;
91 static int zfs_do_clone(int argc
, char **argv
);
92 static int zfs_do_create(int argc
, char **argv
);
93 static int zfs_do_destroy(int argc
, char **argv
);
94 static int zfs_do_get(int argc
, char **argv
);
95 static int zfs_do_inherit(int argc
, char **argv
);
96 static int zfs_do_list(int argc
, char **argv
);
97 static int zfs_do_mount(int argc
, char **argv
);
98 static int zfs_do_rename(int argc
, char **argv
);
99 static int zfs_do_rollback(int argc
, char **argv
);
100 static int zfs_do_set(int argc
, char **argv
);
101 static int zfs_do_upgrade(int argc
, char **argv
);
102 static int zfs_do_snapshot(int argc
, char **argv
);
103 static int zfs_do_unmount(int argc
, char **argv
);
104 static int zfs_do_share(int argc
, char **argv
);
105 static int zfs_do_unshare(int argc
, char **argv
);
106 static int zfs_do_send(int argc
, char **argv
);
107 static int zfs_do_receive(int argc
, char **argv
);
108 static int zfs_do_promote(int argc
, char **argv
);
109 static int zfs_do_userspace(int argc
, char **argv
);
110 static int zfs_do_allow(int argc
, char **argv
);
111 static int zfs_do_unallow(int argc
, char **argv
);
112 static int zfs_do_hold(int argc
, char **argv
);
113 static int zfs_do_holds(int argc
, char **argv
);
114 static int zfs_do_release(int argc
, char **argv
);
115 static int zfs_do_diff(int argc
, char **argv
);
116 static int zfs_do_bookmark(int argc
, char **argv
);
117 static int zfs_do_remap(int argc
, char **argv
);
118 static int zfs_do_channel_program(int argc
, char **argv
);
119 static int zfs_do_load_key(int argc
, char **argv
);
120 static int zfs_do_unload_key(int argc
, char **argv
);
121 static int zfs_do_change_key(int argc
, char **argv
);
122 static int zfs_do_project(int argc
, char **argv
);
125 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
130 _umem_debug_init(void)
132 return ("default,verbose"); /* $UMEM_DEBUG setting */
136 _umem_logging_init(void)
138 return ("fail,contents"); /* $UMEM_LOGGING setting */
173 HELP_CHANNEL_PROGRAM
,
179 typedef struct zfs_command
{
181 int (*func
)(int argc
, char **argv
);
186 * Master command table. Each ZFS command has a name, associated function, and
187 * usage message. The usage messages need to be internationalized, so we have
188 * to have a function to return the usage message based on a command index.
190 * These commands are organized according to how they are displayed in the usage
191 * message. An empty command (one with a NULL name) indicates an empty line in
192 * the generic usage message.
194 static zfs_command_t command_table
[] = {
195 { "create", zfs_do_create
, HELP_CREATE
},
196 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
198 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
199 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
200 { "clone", zfs_do_clone
, HELP_CLONE
},
201 { "promote", zfs_do_promote
, HELP_PROMOTE
},
202 { "rename", zfs_do_rename
, HELP_RENAME
},
203 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
204 { "program", zfs_do_channel_program
, HELP_CHANNEL_PROGRAM
},
206 { "list", zfs_do_list
, HELP_LIST
},
208 { "set", zfs_do_set
, HELP_SET
},
209 { "get", zfs_do_get
, HELP_GET
},
210 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
211 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
213 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
214 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
215 { "projectspace", zfs_do_userspace
, HELP_PROJECTSPACE
},
217 { "project", zfs_do_project
, HELP_PROJECT
},
219 { "mount", zfs_do_mount
, HELP_MOUNT
},
220 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
221 { "share", zfs_do_share
, HELP_SHARE
},
222 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
224 { "send", zfs_do_send
, HELP_SEND
},
225 { "receive", zfs_do_receive
, HELP_RECEIVE
},
227 { "allow", zfs_do_allow
, HELP_ALLOW
},
229 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
231 { "hold", zfs_do_hold
, HELP_HOLD
},
232 { "holds", zfs_do_holds
, HELP_HOLDS
},
233 { "release", zfs_do_release
, HELP_RELEASE
},
234 { "diff", zfs_do_diff
, HELP_DIFF
},
235 { "remap", zfs_do_remap
, HELP_REMAP
},
236 { "load-key", zfs_do_load_key
, HELP_LOAD_KEY
},
237 { "unload-key", zfs_do_unload_key
, HELP_UNLOAD_KEY
},
238 { "change-key", zfs_do_change_key
, HELP_CHANGE_KEY
},
241 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
243 zfs_command_t
*current_command
;
246 get_usage(zfs_help_t idx
)
250 return (gettext("\tclone [-p] [-o property=value] ... "
251 "<snapshot> <filesystem|volume>\n"));
253 return (gettext("\tcreate [-Pnpv] [-o property=value] ... "
255 "\tcreate [-Pnpsv] [-b blocksize] [-o property=value] ... "
256 "-V <size> <volume>\n"));
258 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
259 "\tdestroy [-dnpRrv] "
260 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
261 "\tdestroy <filesystem|volume>#<bookmark>\n"));
263 return (gettext("\tget [-rHp] [-d max] "
264 "[-o \"all\" | field[,...]]\n"
265 "\t [-t type[,...]] [-s source[,...]]\n"
266 "\t <\"all\" | property[,...]> "
267 "[filesystem|volume|snapshot|bookmark] ...\n"));
269 return (gettext("\tinherit [-rS] <property> "
270 "<filesystem|volume|snapshot> ...\n"));
272 return (gettext("\tupgrade [-v]\n"
273 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
275 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
276 "[-s property]...\n\t [-S property]... [-t type[,...]] "
277 "[filesystem|volume|snapshot] ...\n"));
279 return (gettext("\tmount\n"
280 "\tmount [-lvO] [-o opts] <-a | filesystem>\n"));
282 return (gettext("\tpromote <clone-filesystem>\n"));
284 return (gettext("\treceive [-vnsFhu] "
285 "[-o <property>=<value>] ... [-x <property>] ...\n"
286 "\t <filesystem|volume|snapshot>\n"
287 "\treceive [-vnsFhu] [-o <property>=<value>] ... "
288 "[-x <property>] ... \n"
289 "\t [-d | -e] <filesystem>\n"
290 "\treceive -A <filesystem|volume>\n"));
292 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
293 "<filesystem|volume|snapshot>\n"
294 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
295 "\trename -r <snapshot> <snapshot>\n"));
297 return (gettext("\trollback [-rRf] <snapshot>\n"));
299 return (gettext("\tsend [-DnPpRvLecwhb] [-[iI] snapshot] "
301 "\tsend [-nvPLecw] [-i snapshot|bookmark] "
302 "<filesystem|volume|snapshot>\n"
303 "\tsend [-nvPe] -t <receive_resume_token>\n"));
305 return (gettext("\tset <property=value> ... "
306 "<filesystem|volume|snapshot> ...\n"));
308 return (gettext("\tshare [-l] <-a | filesystem>\n"));
310 return (gettext("\tsnapshot [-r] [-o property=value] ... "
311 "<filesystem|volume>@<snap> ...\n"));
313 return (gettext("\tunmount [-f] "
314 "<-a | filesystem|mountpoint>\n"));
316 return (gettext("\tunshare "
317 "<-a | filesystem|mountpoint>\n"));
319 return (gettext("\tallow <filesystem|volume>\n"
321 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
322 "\t <filesystem|volume>\n"
323 "\tallow [-ld] -e <perm|@setname>[,...] "
324 "<filesystem|volume>\n"
325 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
326 "\tallow -s @setname <perm|@setname>[,...] "
327 "<filesystem|volume>\n"));
329 return (gettext("\tunallow [-rldug] "
330 "<\"everyone\"|user|group>[,...]\n"
331 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
332 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
333 "<filesystem|volume>\n"
334 "\tunallow [-r] -c [<perm|@setname>[,...]] "
335 "<filesystem|volume>\n"
336 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
337 "<filesystem|volume>\n"));
339 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
341 "\t [-S field] ... [-t type[,...]] "
342 "<filesystem|snapshot>\n"));
343 case HELP_GROUPSPACE
:
344 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
346 "\t [-S field] ... [-t type[,...]] "
347 "<filesystem|snapshot>\n"));
348 case HELP_PROJECTSPACE
:
349 return (gettext("\tprojectspace [-Hp] [-o field[,...]] "
351 "\t [-S field] ... <filesystem|snapshot>\n"));
353 return (gettext("\tproject [-d|-r] <directory|file ...>\n"
354 "\tproject -c [-0] [-d|-r] [-p id] <directory|file ...>\n"
355 "\tproject -C [-k] [-r] <directory ...>\n"
356 "\tproject [-p id] [-r] [-s] <directory ...>\n"));
358 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
360 return (gettext("\tholds [-r] <snapshot> ...\n"));
362 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
364 return (gettext("\tdiff [-FHt] <snapshot> "
365 "[snapshot|filesystem]\n"));
367 return (gettext("\tremap <filesystem | volume>\n"));
369 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
370 case HELP_CHANNEL_PROGRAM
:
371 return (gettext("\tprogram [-jn] [-t <instruction limit>] "
372 "[-m <memory limit (b)>] <pool> <program file> "
375 return (gettext("\tload-key [-rn] [-L <keylocation>] "
376 "<-a | filesystem|volume>\n"));
377 case HELP_UNLOAD_KEY
:
378 return (gettext("\tunload-key [-r] "
379 "<-a | filesystem|volume>\n"));
380 case HELP_CHANGE_KEY
:
381 return (gettext("\tchange-key [-l] [-o keyformat=<value>]\n"
382 "\t [-o keylocation=<value>] [-o pbkfd2iters=<value>]\n"
383 "\t <filesystem|volume>\n"
384 "\tchange-key -i [-l] <filesystem|volume>\n"));
394 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
399 * Utility function to guarantee malloc() success.
403 safe_malloc(size_t size
)
407 if ((data
= calloc(1, size
)) == NULL
)
414 safe_realloc(void *data
, size_t size
)
417 if ((newp
= realloc(data
, size
)) == NULL
) {
426 safe_strdup(char *str
)
428 char *dupstr
= strdup(str
);
437 * Callback routine that will print out information for each of
441 usage_prop_cb(int prop
, void *cb
)
445 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
447 if (zfs_prop_readonly(prop
))
448 (void) fprintf(fp
, " NO ");
450 (void) fprintf(fp
, "YES ");
452 if (zfs_prop_inheritable(prop
))
453 (void) fprintf(fp
, " YES ");
455 (void) fprintf(fp
, " NO ");
457 if (zfs_prop_values(prop
) == NULL
)
458 (void) fprintf(fp
, "-\n");
460 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
466 * Display usage message. If we're inside a command, display only the usage for
467 * that command. Otherwise, iterate over the entire command table and display
468 * a complete usage message.
471 usage(boolean_t requested
)
474 boolean_t show_properties
= B_FALSE
;
475 FILE *fp
= requested
? stdout
: stderr
;
477 if (current_command
== NULL
) {
479 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
481 gettext("where 'command' is one of the following:\n\n"));
483 for (i
= 0; i
< NCOMMAND
; i
++) {
484 if (command_table
[i
].name
== NULL
)
485 (void) fprintf(fp
, "\n");
487 (void) fprintf(fp
, "%s",
488 get_usage(command_table
[i
].usage
));
491 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
492 "pool/[dataset/]*dataset[@name]\n"));
494 (void) fprintf(fp
, gettext("usage:\n"));
495 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
498 if (current_command
!= NULL
&&
499 (strcmp(current_command
->name
, "set") == 0 ||
500 strcmp(current_command
->name
, "get") == 0 ||
501 strcmp(current_command
->name
, "inherit") == 0 ||
502 strcmp(current_command
->name
, "list") == 0))
503 show_properties
= B_TRUE
;
505 if (show_properties
) {
507 gettext("\nThe following properties are supported:\n"));
509 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
510 "PROPERTY", "EDIT", "INHERIT", "VALUES");
512 /* Iterate over all properties */
513 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
516 (void) fprintf(fp
, "\t%-15s ", "userused@...");
517 (void) fprintf(fp
, " NO NO <size>\n");
518 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
519 (void) fprintf(fp
, " NO NO <size>\n");
520 (void) fprintf(fp
, "\t%-15s ", "projectused@...");
521 (void) fprintf(fp
, " NO NO <size>\n");
522 (void) fprintf(fp
, "\t%-15s ", "userobjused@...");
523 (void) fprintf(fp
, " NO NO <size>\n");
524 (void) fprintf(fp
, "\t%-15s ", "groupobjused@...");
525 (void) fprintf(fp
, " NO NO <size>\n");
526 (void) fprintf(fp
, "\t%-15s ", "projectobjused@...");
527 (void) fprintf(fp
, " NO NO <size>\n");
528 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
529 (void) fprintf(fp
, "YES NO <size> | none\n");
530 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
531 (void) fprintf(fp
, "YES NO <size> | none\n");
532 (void) fprintf(fp
, "\t%-15s ", "projectquota@...");
533 (void) fprintf(fp
, "YES NO <size> | none\n");
534 (void) fprintf(fp
, "\t%-15s ", "userobjquota@...");
535 (void) fprintf(fp
, "YES NO <size> | none\n");
536 (void) fprintf(fp
, "\t%-15s ", "groupobjquota@...");
537 (void) fprintf(fp
, "YES NO <size> | none\n");
538 (void) fprintf(fp
, "\t%-15s ", "projectobjquota@...");
539 (void) fprintf(fp
, "YES NO <size> | none\n");
540 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
541 (void) fprintf(fp
, " NO NO <size>\n");
543 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
544 "with standard units such as K, M, G, etc.\n"));
545 (void) fprintf(fp
, gettext("\nUser-defined properties can "
546 "be specified by using a name containing a colon (:).\n"));
547 (void) fprintf(fp
, gettext("\nThe {user|group|project}"
548 "[obj]{used|quota}@ properties must be appended with\n"
549 "a user|group|project specifier of one of these forms:\n"
550 " POSIX name (eg: \"matt\")\n"
551 " POSIX id (eg: \"126829\")\n"
552 " SMB name@domain (eg: \"matt@sun\")\n"
553 " SMB SID (eg: \"S-1-234-567-89\")\n"));
556 gettext("\nFor the property list, run: %s\n"),
559 gettext("\nFor the delegated permission list, run: %s\n"),
560 "zfs allow|unallow");
564 * See comments at end of main().
566 if (getenv("ZFS_ABORT") != NULL
) {
567 (void) printf("dumping core by request\n");
571 exit(requested
? 0 : 2);
575 * Take a property=value argument string and add it to the given nvlist.
576 * Modifies the argument inplace.
579 parseprop(nvlist_t
*props
, char *propname
)
583 if ((propval
= strchr(propname
, '=')) == NULL
) {
584 (void) fprintf(stderr
, gettext("missing "
585 "'=' for property=value argument\n"));
590 if (nvlist_exists(props
, propname
)) {
591 (void) fprintf(stderr
, gettext("property '%s' "
592 "specified multiple times\n"), propname
);
595 if (nvlist_add_string(props
, propname
, propval
) != 0)
601 * Take a property name argument and add it to the given nvlist.
602 * Modifies the argument inplace.
605 parsepropname(nvlist_t
*props
, char *propname
)
607 if (strchr(propname
, '=') != NULL
) {
608 (void) fprintf(stderr
, gettext("invalid character "
609 "'=' in property argument\n"));
612 if (nvlist_exists(props
, propname
)) {
613 (void) fprintf(stderr
, gettext("property '%s' "
614 "specified multiple times\n"), propname
);
617 if (nvlist_add_boolean(props
, propname
) != 0)
623 parse_depth(char *opt
, int *flags
)
628 depth
= (int)strtol(opt
, &tmp
, 0);
630 (void) fprintf(stderr
,
631 gettext("%s is not an integer\n"), optarg
);
635 (void) fprintf(stderr
,
636 gettext("Depth can not be negative.\n"));
639 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
643 #define PROGRESS_DELAY 2 /* seconds */
645 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";
646 static time_t pt_begin
;
647 static char *pt_header
= NULL
;
648 static boolean_t pt_shown
;
651 start_progress_timer(void)
653 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
658 set_progress_header(char *header
)
660 assert(pt_header
== NULL
);
661 pt_header
= safe_strdup(header
);
663 (void) printf("%s: ", header
);
664 (void) fflush(stdout
);
669 update_progress(char *update
)
671 if (!pt_shown
&& time(NULL
) > pt_begin
) {
672 int len
= strlen(update
);
674 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
676 (void) fflush(stdout
);
678 } else if (pt_shown
) {
679 int len
= strlen(update
);
681 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
682 (void) fflush(stdout
);
687 finish_progress(char *done
)
690 (void) printf("%s\n", done
);
691 (void) fflush(stdout
);
698 zfs_mount_and_share(libzfs_handle_t
*hdl
, const char *dataset
, zfs_type_t type
)
700 zfs_handle_t
*zhp
= NULL
;
703 zhp
= zfs_open(hdl
, dataset
, type
);
708 * Volumes may neither be mounted or shared. Potentially in the
709 * future filesystems detected on these volumes could be mounted.
711 if (zfs_get_type(zhp
) == ZFS_TYPE_VOLUME
) {
717 * Mount and/or share the new filesystem as appropriate. We provide a
718 * verbose error message to let the user know that their filesystem was
719 * in fact created, even if we failed to mount or share it.
721 * If the user doesn't want the dataset automatically mounted, then
722 * skip the mount/share step
724 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
, B_FALSE
) &&
725 zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
) {
726 if (zfs_mount(zhp
, NULL
, 0) != 0) {
727 (void) fprintf(stderr
, gettext("filesystem "
728 "successfully created, but not mounted\n"));
730 } else if (zfs_share(zhp
) != 0) {
731 (void) fprintf(stderr
, gettext("filesystem "
732 "successfully created, but not shared\n"));
743 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
745 * Given an existing dataset, create a writable copy whose initial contents
746 * are the same as the source. The newly created dataset maintains a
747 * dependency on the original; the original cannot be destroyed so long as
750 * The '-p' flag creates all the non-existing ancestors of the target first.
753 zfs_do_clone(int argc
, char **argv
)
755 zfs_handle_t
*zhp
= NULL
;
756 boolean_t parents
= B_FALSE
;
761 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
765 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
768 if (!parseprop(props
, optarg
)) {
777 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
786 /* check number of arguments */
788 (void) fprintf(stderr
, gettext("missing source dataset "
793 (void) fprintf(stderr
, gettext("missing target dataset "
798 (void) fprintf(stderr
, gettext("too many arguments\n"));
802 /* open the source dataset */
803 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
806 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
809 * Now create the ancestors of the target dataset. If the
810 * target already exists and '-p' option was used we should not
813 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
816 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
821 ret
= zfs_clone(zhp
, argv
[1], props
);
823 /* create the mountpoint if necessary */
826 (void) zpool_log_history(g_zfs
, history_str
);
827 log_history
= B_FALSE
;
830 ret
= zfs_mount_and_share(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
847 * zfs create [-Pnpv] [-o prop=value] ... fs
848 * zfs create [-Pnpsv] [-b blocksize] [-o prop=value] ... -V vol size
850 * Create a new dataset. This command can be used to create filesystems
851 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
852 * For volumes, the user must specify a size to be used.
854 * The '-s' flag applies only to volumes, and indicates that we should not try
855 * to set the reservation for this volume. By default we set a reservation
856 * equal to the size for any volume. For pools with SPA_VERSION >=
857 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
859 * The '-p' flag creates all the non-existing ancestors of the target first.
861 * The '-n' flag is no-op (dry run) mode. This will perform a user-space sanity
862 * check of arguments and properties, but does not check for permissions,
863 * available space, etc.
865 * The '-v' flag is for verbose output.
867 * The '-P' flag is used for parseable output. It implies '-v'.
870 zfs_do_create(int argc
, char **argv
)
872 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
873 zpool_handle_t
*zpool_handle
= NULL
;
874 nvlist_t
*real_props
= NULL
;
875 uint64_t volsize
= 0;
877 boolean_t noreserve
= B_FALSE
;
878 boolean_t bflag
= B_FALSE
;
879 boolean_t parents
= B_FALSE
;
880 boolean_t dryrun
= B_FALSE
;
881 boolean_t verbose
= B_FALSE
;
882 boolean_t parseable
= B_FALSE
;
887 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
891 while ((c
= getopt(argc
, argv
, ":PV:b:nso:pv")) != -1) {
894 type
= ZFS_TYPE_VOLUME
;
895 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
896 (void) fprintf(stderr
, gettext("bad volume "
897 "size '%s': %s\n"), optarg
,
898 libzfs_error_description(g_zfs
));
902 if (nvlist_add_uint64(props
,
903 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
916 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
917 (void) fprintf(stderr
, gettext("bad volume "
918 "block size '%s': %s\n"), optarg
,
919 libzfs_error_description(g_zfs
));
923 if (nvlist_add_uint64(props
,
924 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
932 if (!parseprop(props
, optarg
))
942 (void) fprintf(stderr
, gettext("missing size "
946 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
952 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
953 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
954 "used when creating a volume\n"));
961 /* check number of arguments */
963 (void) fprintf(stderr
, gettext("missing %s argument\n"),
964 zfs_type_to_name(type
));
968 (void) fprintf(stderr
, gettext("too many arguments\n"));
972 if (dryrun
|| (type
== ZFS_TYPE_VOLUME
&& !noreserve
)) {
973 char msg
[ZFS_MAX_DATASET_NAME_LEN
* 2];
976 if ((p
= strchr(argv
[0], '/')) != NULL
)
978 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
981 if (zpool_handle
== NULL
)
984 (void) snprintf(msg
, sizeof (msg
),
985 dryrun
? gettext("cannot verify '%s'") :
986 gettext("cannot create '%s'"), argv
[0]);
987 if (props
&& (real_props
= zfs_valid_proplist(g_zfs
, type
,
988 props
, 0, NULL
, zpool_handle
, B_TRUE
, msg
)) == NULL
) {
989 zpool_close(zpool_handle
);
995 * if volsize is not a multiple of volblocksize, round it up to the
996 * nearest multiple of the volblocksize
998 if (type
== ZFS_TYPE_VOLUME
) {
999 uint64_t volblocksize
;
1001 if (nvlist_lookup_uint64(props
,
1002 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1003 &volblocksize
) != 0)
1004 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
1006 if (volsize
% volblocksize
) {
1007 volsize
= P2ROUNDUP_TYPED(volsize
, volblocksize
,
1010 if (nvlist_add_uint64(props
,
1011 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), volsize
) != 0) {
1019 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
1020 uint64_t spa_version
;
1021 zfs_prop_t resv_prop
;
1024 spa_version
= zpool_get_prop_int(zpool_handle
,
1025 ZPOOL_PROP_VERSION
, NULL
);
1026 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
1027 resv_prop
= ZFS_PROP_REFRESERVATION
;
1029 resv_prop
= ZFS_PROP_RESERVATION
;
1031 volsize
= zvol_volsize_to_reservation(zpool_handle
, volsize
,
1034 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
1036 if (nvlist_add_uint64(props
,
1037 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
1043 if (zpool_handle
!= NULL
) {
1044 zpool_close(zpool_handle
);
1045 nvlist_free(real_props
);
1048 if (parents
&& zfs_name_valid(argv
[0], type
)) {
1050 * Now create the ancestors of target dataset. If the target
1051 * already exists and '-p' option was used we should not
1054 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
1059 (void) printf(parseable
? "create_ancestors\t%s\n" :
1060 dryrun
? "would create ancestors of %s\n" :
1061 "create ancestors of %s\n", argv
[0]);
1064 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0) {
1071 nvpair_t
*nvp
= NULL
;
1072 (void) printf(parseable
? "create\t%s\n" :
1073 dryrun
? "would create %s\n" : "create %s\n", argv
[0]);
1074 while ((nvp
= nvlist_next_nvpair(props
, nvp
)) != NULL
) {
1078 switch (nvpair_type(nvp
)) {
1079 case DATA_TYPE_UINT64
:
1080 VERIFY0(nvpair_value_uint64(nvp
, &uval
));
1081 (void) printf(parseable
?
1082 "property\t%s\t%llu\n" : "\t%s=%llu\n",
1083 nvpair_name(nvp
), (u_longlong_t
)uval
);
1085 case DATA_TYPE_STRING
:
1086 VERIFY0(nvpair_value_string(nvp
, &sval
));
1087 (void) printf(parseable
?
1088 "property\t%s\t%s\n" : "\t%s=%s\n",
1089 nvpair_name(nvp
), sval
);
1092 (void) fprintf(stderr
, "property '%s' "
1093 "has illegal type %d\n",
1094 nvpair_name(nvp
), nvpair_type(nvp
));
1104 /* pass to libzfs */
1105 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
1109 (void) zpool_log_history(g_zfs
, history_str
);
1110 log_history
= B_FALSE
;
1113 ret
= zfs_mount_and_share(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
1124 * zfs destroy [-rRf] <fs, vol>
1125 * zfs destroy [-rRd] <snap>
1127 * -r Recursively destroy all children
1128 * -R Recursively destroy all dependents, including clones
1129 * -f Force unmounting of any dependents
1130 * -d If we can't destroy now, mark for deferred destruction
1132 * Destroys the given dataset. By default, it will unmount any filesystems,
1133 * and refuse to destroy a dataset that has any dependents. A dependent can
1134 * either be a child, or a clone of a child.
1136 typedef struct destroy_cbdata
{
1139 boolean_t cb_recurse
;
1141 boolean_t cb_doclones
;
1142 zfs_handle_t
*cb_target
;
1143 boolean_t cb_defer_destroy
;
1144 boolean_t cb_verbose
;
1145 boolean_t cb_parsable
;
1146 boolean_t cb_dryrun
;
1148 nvlist_t
*cb_batchedsnaps
;
1150 /* first snap in contiguous run */
1152 /* previous snap in contiguous run */
1154 int64_t cb_snapused
;
1160 * Check for any dependents based on the '-r' or '-R' flags.
1163 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
1165 destroy_cbdata_t
*cbp
= data
;
1166 const char *tname
= zfs_get_name(cbp
->cb_target
);
1167 const char *name
= zfs_get_name(zhp
);
1169 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
1170 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
1172 * This is a direct descendant, not a clone somewhere else in
1175 if (cbp
->cb_recurse
)
1178 if (cbp
->cb_first
) {
1179 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1180 "%s has children\n"),
1181 zfs_get_name(cbp
->cb_target
),
1182 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1183 (void) fprintf(stderr
, gettext("use '-r' to destroy "
1184 "the following datasets:\n"));
1185 cbp
->cb_first
= B_FALSE
;
1186 cbp
->cb_error
= B_TRUE
;
1189 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1192 * This is a clone. We only want to report this if the '-r'
1193 * wasn't specified, or the target is a snapshot.
1195 if (!cbp
->cb_recurse
&&
1196 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
1199 if (cbp
->cb_first
) {
1200 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1201 "%s has dependent clones\n"),
1202 zfs_get_name(cbp
->cb_target
),
1203 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1204 (void) fprintf(stderr
, gettext("use '-R' to destroy "
1205 "the following datasets:\n"));
1206 cbp
->cb_first
= B_FALSE
;
1207 cbp
->cb_error
= B_TRUE
;
1208 cbp
->cb_dryrun
= B_TRUE
;
1211 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1220 destroy_callback(zfs_handle_t
*zhp
, void *data
)
1222 destroy_cbdata_t
*cb
= data
;
1223 const char *name
= zfs_get_name(zhp
);
1225 if (cb
->cb_verbose
) {
1226 if (cb
->cb_parsable
) {
1227 (void) printf("destroy\t%s\n", name
);
1228 } else if (cb
->cb_dryrun
) {
1229 (void) printf(gettext("would destroy %s\n"),
1232 (void) printf(gettext("will destroy %s\n"),
1238 * Ignore pools (which we've already flagged as an error before getting
1241 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1242 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1246 if (cb
->cb_dryrun
) {
1252 * We batch up all contiguous snapshots (even of different
1253 * filesystems) and destroy them with one ioctl. We can't
1254 * simply do all snap deletions and then all fs deletions,
1255 * because we must delete a clone before its origin.
1257 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1258 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1260 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1261 cb
->cb_batchedsnaps
, B_FALSE
);
1262 fnvlist_free(cb
->cb_batchedsnaps
);
1263 cb
->cb_batchedsnaps
= fnvlist_alloc();
1266 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1267 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1278 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1280 destroy_cbdata_t
*cb
= arg
;
1281 const char *name
= zfs_get_name(zhp
);
1284 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1285 if (cb
->cb_firstsnap
== NULL
)
1286 cb
->cb_firstsnap
= strdup(name
);
1287 if (cb
->cb_prevsnap
!= NULL
)
1288 free(cb
->cb_prevsnap
);
1289 /* this snap continues the current range */
1290 cb
->cb_prevsnap
= strdup(name
);
1291 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1293 if (cb
->cb_verbose
) {
1294 if (cb
->cb_parsable
) {
1295 (void) printf("destroy\t%s\n", name
);
1296 } else if (cb
->cb_dryrun
) {
1297 (void) printf(gettext("would destroy %s\n"),
1300 (void) printf(gettext("will destroy %s\n"),
1304 } else if (cb
->cb_firstsnap
!= NULL
) {
1305 /* end of this range */
1307 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1308 cb
->cb_prevsnap
, &used
);
1309 cb
->cb_snapused
+= used
;
1310 free(cb
->cb_firstsnap
);
1311 cb
->cb_firstsnap
= NULL
;
1312 free(cb
->cb_prevsnap
);
1313 cb
->cb_prevsnap
= NULL
;
1320 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1323 assert(cb
->cb_firstsnap
== NULL
);
1324 assert(cb
->cb_prevsnap
== NULL
);
1325 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
);
1326 if (cb
->cb_firstsnap
!= NULL
) {
1329 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1330 cb
->cb_prevsnap
, &used
);
1332 cb
->cb_snapused
+= used
;
1333 free(cb
->cb_firstsnap
);
1334 cb
->cb_firstsnap
= NULL
;
1335 free(cb
->cb_prevsnap
);
1336 cb
->cb_prevsnap
= NULL
;
1342 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1344 destroy_cbdata_t
*cb
= arg
;
1347 /* Check for clones. */
1348 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1349 cb
->cb_target
= zhp
;
1350 cb
->cb_first
= B_TRUE
;
1351 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1352 destroy_check_dependent
, cb
);
1356 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1364 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1366 destroy_cbdata_t
*cb
= arg
;
1369 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1375 if (cb
->cb_verbose
) {
1376 err
= destroy_print_snapshots(zhp
, cb
);
1382 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1390 destroy_clones(destroy_cbdata_t
*cb
)
1393 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1395 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1396 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1399 boolean_t defer
= cb
->cb_defer_destroy
;
1403 * We can't defer destroy non-snapshots, so set it to
1404 * false while destroying the clones.
1406 cb
->cb_defer_destroy
= B_FALSE
;
1407 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1408 destroy_callback
, cb
);
1409 cb
->cb_defer_destroy
= defer
;
1419 zfs_do_destroy(int argc
, char **argv
)
1421 destroy_cbdata_t cb
= { 0 };
1425 zfs_handle_t
*zhp
= NULL
;
1427 zfs_type_t type
= ZFS_TYPE_DATASET
;
1430 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1433 cb
.cb_verbose
= B_TRUE
;
1436 cb
.cb_verbose
= B_TRUE
;
1437 cb
.cb_parsable
= B_TRUE
;
1440 cb
.cb_dryrun
= B_TRUE
;
1443 cb
.cb_defer_destroy
= B_TRUE
;
1444 type
= ZFS_TYPE_SNAPSHOT
;
1447 cb
.cb_force
= B_TRUE
;
1450 cb
.cb_recurse
= B_TRUE
;
1453 cb
.cb_recurse
= B_TRUE
;
1454 cb
.cb_doclones
= B_TRUE
;
1458 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1467 /* check number of arguments */
1469 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1473 (void) fprintf(stderr
, gettext("too many arguments\n"));
1477 at
= strchr(argv
[0], '@');
1478 pound
= strchr(argv
[0], '#');
1481 /* Build the list of snaps to destroy in cb_nvl. */
1482 cb
.cb_nvl
= fnvlist_alloc();
1485 zhp
= zfs_open(g_zfs
, argv
[0],
1486 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1490 cb
.cb_snapspec
= at
+ 1;
1491 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1497 if (nvlist_empty(cb
.cb_nvl
)) {
1498 (void) fprintf(stderr
, gettext("could not find any "
1499 "snapshots to destroy; check snapshot names.\n"));
1504 if (cb
.cb_verbose
) {
1506 zfs_nicebytes(cb
.cb_snapused
, buf
, sizeof (buf
));
1507 if (cb
.cb_parsable
) {
1508 (void) printf("reclaim\t%llu\n",
1510 } else if (cb
.cb_dryrun
) {
1511 (void) printf(gettext("would reclaim %s\n"),
1514 (void) printf(gettext("will reclaim %s\n"),
1519 if (!cb
.cb_dryrun
) {
1520 if (cb
.cb_doclones
) {
1521 cb
.cb_batchedsnaps
= fnvlist_alloc();
1522 err
= destroy_clones(&cb
);
1524 err
= zfs_destroy_snaps_nvl(g_zfs
,
1525 cb
.cb_batchedsnaps
, B_FALSE
);
1533 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1534 cb
.cb_defer_destroy
);
1540 } else if (pound
!= NULL
) {
1545 (void) fprintf(stderr
,
1546 "dryrun is not supported with bookmark\n");
1550 if (cb
.cb_defer_destroy
) {
1551 (void) fprintf(stderr
,
1552 "defer destroy is not supported with bookmark\n");
1556 if (cb
.cb_recurse
) {
1557 (void) fprintf(stderr
,
1558 "recursive is not supported with bookmark\n");
1562 if (!zfs_bookmark_exists(argv
[0])) {
1563 (void) fprintf(stderr
, gettext("bookmark '%s' "
1564 "does not exist.\n"), argv
[0]);
1568 nvl
= fnvlist_alloc();
1569 fnvlist_add_boolean(nvl
, argv
[0]);
1571 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1573 (void) zfs_standard_error(g_zfs
, err
,
1574 "cannot destroy bookmark");
1577 nvlist_free(cb
.cb_nvl
);
1581 /* Open the given dataset */
1582 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1588 * Perform an explicit check for pools before going any further.
1590 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1591 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1592 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1593 "operation does not apply to pools\n"),
1595 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1596 "%s' to destroy all datasets in the pool\n"),
1598 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1599 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1605 * Check for any dependents and/or clones.
1607 cb
.cb_first
= B_TRUE
;
1608 if (!cb
.cb_doclones
&&
1609 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1620 cb
.cb_batchedsnaps
= fnvlist_alloc();
1621 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1628 * Do the real thing. The callback will close the
1629 * handle regardless of whether it succeeds or not.
1631 err
= destroy_callback(zhp
, &cb
);
1634 err
= zfs_destroy_snaps_nvl(g_zfs
,
1635 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1642 fnvlist_free(cb
.cb_batchedsnaps
);
1643 fnvlist_free(cb
.cb_nvl
);
1650 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1653 zfs_get_column_t col
;
1655 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1656 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1657 if (col
== GET_COL_RECVD
)
1663 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1664 * < all | property[,property]... > < fs | snap | vol > ...
1666 * -r recurse over any child datasets
1667 * -H scripted mode. Headers are stripped, and fields are separated
1668 * by tabs instead of spaces.
1669 * -o Set of fields to display. One of "name,property,value,
1670 * received,source". Default is "name,property,value,source".
1671 * "all" is an alias for all five.
1672 * -s Set of sources to allow. One of
1673 * "local,default,inherited,received,temporary,none". Default is
1675 * -p Display values in parsable (literal) format.
1677 * Prints properties for the given datasets. The user can control which
1678 * columns to display as well as which property types to allow.
1682 * Invoked to display the properties for a single dataset.
1685 get_callback(zfs_handle_t
*zhp
, void *data
)
1687 char buf
[ZFS_MAXPROPLEN
];
1688 char rbuf
[ZFS_MAXPROPLEN
];
1689 zprop_source_t sourcetype
;
1690 char source
[ZFS_MAX_DATASET_NAME_LEN
];
1691 zprop_get_cbdata_t
*cbp
= data
;
1692 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1693 zprop_list_t
*pl
= cbp
->cb_proplist
;
1697 boolean_t received
= is_recvd_column(cbp
);
1699 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1700 char *recvdval
= NULL
;
1702 * Skip the special fake placeholder. This will also skip over
1703 * the name property when 'all' is specified.
1705 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1706 pl
== cbp
->cb_proplist
)
1709 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1710 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1711 sizeof (buf
), &sourcetype
, source
,
1713 cbp
->cb_literal
) != 0) {
1716 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1717 ZFS_TYPE_DATASET
, B_FALSE
)) {
1718 (void) fprintf(stderr
,
1719 gettext("No such property '%s'\n"),
1720 zfs_prop_to_name(pl
->pl_prop
));
1723 sourcetype
= ZPROP_SRC_NONE
;
1724 (void) strlcpy(buf
, "-", sizeof (buf
));
1727 if (received
&& (zfs_prop_get_recvd(zhp
,
1728 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1729 cbp
->cb_literal
) == 0))
1732 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1733 zfs_prop_to_name(pl
->pl_prop
),
1734 buf
, sourcetype
, source
, recvdval
);
1735 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1736 sourcetype
= ZPROP_SRC_LOCAL
;
1738 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1739 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1740 sourcetype
= ZPROP_SRC_NONE
;
1741 (void) strlcpy(buf
, "-", sizeof (buf
));
1744 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1745 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1746 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1747 sourcetype
= ZPROP_SRC_LOCAL
;
1749 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1750 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1751 sourcetype
= ZPROP_SRC_NONE
;
1752 (void) strlcpy(buf
, "-", sizeof (buf
));
1755 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1756 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1758 if (nvlist_lookup_nvlist(user_props
,
1759 pl
->pl_user_prop
, &propval
) != 0) {
1762 sourcetype
= ZPROP_SRC_NONE
;
1765 verify(nvlist_lookup_string(propval
,
1766 ZPROP_VALUE
, &strval
) == 0);
1767 verify(nvlist_lookup_string(propval
,
1768 ZPROP_SOURCE
, &sourceval
) == 0);
1770 if (strcmp(sourceval
,
1771 zfs_get_name(zhp
)) == 0) {
1772 sourcetype
= ZPROP_SRC_LOCAL
;
1773 } else if (strcmp(sourceval
,
1774 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1775 sourcetype
= ZPROP_SRC_RECEIVED
;
1777 sourcetype
= ZPROP_SRC_INHERITED
;
1778 (void) strlcpy(source
,
1779 sourceval
, sizeof (source
));
1783 if (received
&& (zfs_prop_get_recvd(zhp
,
1784 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1785 cbp
->cb_literal
) == 0))
1788 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1789 pl
->pl_user_prop
, strval
, sourcetype
,
1798 zfs_do_get(int argc
, char **argv
)
1800 zprop_get_cbdata_t cb
= { 0 };
1801 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1802 int types
= ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
;
1803 char *value
, *fields
;
1806 zprop_list_t fake_name
= { 0 };
1809 * Set up default columns and sources.
1811 cb
.cb_sources
= ZPROP_SRC_ALL
;
1812 cb
.cb_columns
[0] = GET_COL_NAME
;
1813 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1814 cb
.cb_columns
[2] = GET_COL_VALUE
;
1815 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1816 cb
.cb_type
= ZFS_TYPE_DATASET
;
1819 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1822 cb
.cb_literal
= B_TRUE
;
1825 limit
= parse_depth(optarg
, &flags
);
1828 flags
|= ZFS_ITER_RECURSE
;
1831 cb
.cb_scripted
= B_TRUE
;
1834 (void) fprintf(stderr
, gettext("missing argument for "
1835 "'%c' option\n"), optopt
);
1840 * Process the set of columns to display. We zero out
1841 * the structure to give us a blank slate.
1843 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1845 while (*optarg
!= '\0') {
1846 static char *col_subopts
[] =
1847 { "name", "property", "value", "received",
1848 "source", "all", NULL
};
1850 if (i
== ZFS_GET_NCOLS
) {
1851 (void) fprintf(stderr
, gettext("too "
1852 "many fields given to -o "
1857 switch (getsubopt(&optarg
, col_subopts
,
1860 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1863 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1866 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1869 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1870 flags
|= ZFS_ITER_RECVD_PROPS
;
1873 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1877 (void) fprintf(stderr
,
1878 gettext("\"all\" conflicts "
1879 "with specific fields "
1880 "given to -o option\n"));
1883 cb
.cb_columns
[0] = GET_COL_NAME
;
1884 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1885 cb
.cb_columns
[2] = GET_COL_VALUE
;
1886 cb
.cb_columns
[3] = GET_COL_RECVD
;
1887 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1888 flags
|= ZFS_ITER_RECVD_PROPS
;
1892 (void) fprintf(stderr
,
1893 gettext("invalid column name "
1902 while (*optarg
!= '\0') {
1903 static char *source_subopts
[] = {
1904 "local", "default", "inherited",
1905 "received", "temporary", "none",
1908 switch (getsubopt(&optarg
, source_subopts
,
1911 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1914 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1917 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1920 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1923 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1926 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1929 (void) fprintf(stderr
,
1930 gettext("invalid source "
1939 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1940 while (*optarg
!= '\0') {
1941 static char *type_subopts
[] = { "filesystem",
1942 "volume", "snapshot", "bookmark",
1945 switch (getsubopt(&optarg
, type_subopts
,
1948 types
|= ZFS_TYPE_FILESYSTEM
;
1951 types
|= ZFS_TYPE_VOLUME
;
1954 types
|= ZFS_TYPE_SNAPSHOT
;
1957 types
|= ZFS_TYPE_BOOKMARK
;
1960 types
= ZFS_TYPE_DATASET
|
1965 (void) fprintf(stderr
,
1966 gettext("invalid type '%s'\n"),
1974 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1984 (void) fprintf(stderr
, gettext("missing property "
1991 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1999 * As part of zfs_expand_proplist(), we keep track of the maximum column
2000 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
2001 * need to know the maximum name length. However, the user likely did
2002 * not specify 'name' as one of the properties to fetch, so we need to
2003 * make sure we always include at least this property for
2004 * print_get_headers() to work properly.
2006 if (cb
.cb_proplist
!= NULL
) {
2007 fake_name
.pl_prop
= ZFS_PROP_NAME
;
2008 fake_name
.pl_width
= strlen(gettext("NAME"));
2009 fake_name
.pl_next
= cb
.cb_proplist
;
2010 cb
.cb_proplist
= &fake_name
;
2013 cb
.cb_first
= B_TRUE
;
2015 /* run for each object */
2016 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
2017 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
2019 if (cb
.cb_proplist
== &fake_name
)
2020 zprop_free_list(fake_name
.pl_next
);
2022 zprop_free_list(cb
.cb_proplist
);
2028 * inherit [-rS] <property> <fs|vol> ...
2030 * -r Recurse over all children
2031 * -S Revert to received value, if any
2033 * For each dataset specified on the command line, inherit the given property
2034 * from its parent. Inheriting a property at the pool level will cause it to
2035 * use the default value. The '-r' flag will recurse over all children, and is
2036 * useful for setting a property on a hierarchy-wide basis, regardless of any
2037 * local modifications for each dataset.
2040 typedef struct inherit_cbdata
{
2041 const char *cb_propname
;
2042 boolean_t cb_received
;
2046 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
2048 inherit_cbdata_t
*cb
= data
;
2049 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
2052 * If we're doing it recursively, then ignore properties that
2053 * are not valid for this type of dataset.
2055 if (prop
!= ZPROP_INVAL
&&
2056 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
), B_FALSE
))
2059 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
2063 inherit_cb(zfs_handle_t
*zhp
, void *data
)
2065 inherit_cbdata_t
*cb
= data
;
2067 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
2071 zfs_do_inherit(int argc
, char **argv
)
2075 inherit_cbdata_t cb
= { 0 };
2079 boolean_t received
= B_FALSE
;
2082 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
2085 flags
|= ZFS_ITER_RECURSE
;
2092 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2101 /* check number of arguments */
2103 (void) fprintf(stderr
, gettext("missing property argument\n"));
2107 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
2115 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
2116 if (zfs_prop_readonly(prop
)) {
2117 (void) fprintf(stderr
, gettext(
2118 "%s property is read-only\n"),
2122 if (!zfs_prop_inheritable(prop
) && !received
) {
2123 (void) fprintf(stderr
, gettext("'%s' property cannot "
2124 "be inherited\n"), propname
);
2125 if (prop
== ZFS_PROP_QUOTA
||
2126 prop
== ZFS_PROP_RESERVATION
||
2127 prop
== ZFS_PROP_REFQUOTA
||
2128 prop
== ZFS_PROP_REFRESERVATION
) {
2129 (void) fprintf(stderr
, gettext("use 'zfs set "
2130 "%s=none' to clear\n"), propname
);
2131 (void) fprintf(stderr
, gettext("use 'zfs "
2132 "inherit -S %s' to revert to received "
2133 "value\n"), propname
);
2137 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
2138 prop
== ZFS_PROP_VERSION
)) {
2139 (void) fprintf(stderr
, gettext("'%s' property cannot "
2140 "be reverted to a received value\n"), propname
);
2143 } else if (!zfs_prop_user(propname
)) {
2144 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
2149 cb
.cb_propname
= propname
;
2150 cb
.cb_received
= received
;
2152 if (flags
& ZFS_ITER_RECURSE
) {
2153 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
2154 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
2156 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
2157 NULL
, NULL
, 0, inherit_cb
, &cb
);
2163 typedef struct upgrade_cbdata
{
2164 uint64_t cb_numupgraded
;
2165 uint64_t cb_numsamegraded
;
2166 uint64_t cb_numfailed
;
2167 uint64_t cb_version
;
2169 boolean_t cb_foundone
;
2170 char cb_lastfs
[ZFS_MAX_DATASET_NAME_LEN
];
2174 same_pool(zfs_handle_t
*zhp
, const char *name
)
2176 int len1
= strcspn(name
, "/@");
2177 const char *zhname
= zfs_get_name(zhp
);
2178 int len2
= strcspn(zhname
, "/@");
2182 return (strncmp(name
, zhname
, len1
) == 0);
2186 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
2188 upgrade_cbdata_t
*cb
= data
;
2189 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2191 /* list if it's old/new */
2192 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
2193 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
2196 str
= gettext("The following filesystems are "
2197 "formatted using a newer software version and\n"
2198 "cannot be accessed on the current system.\n\n");
2200 str
= gettext("The following filesystems are "
2201 "out of date, and can be upgraded. After being\n"
2202 "upgraded, these filesystems (and any 'zfs send' "
2203 "streams generated from\n"
2204 "subsequent snapshots) will no longer be "
2205 "accessible by older software versions.\n\n");
2208 if (!cb
->cb_foundone
) {
2210 (void) printf(gettext("VER FILESYSTEM\n"));
2211 (void) printf(gettext("--- ------------\n"));
2212 cb
->cb_foundone
= B_TRUE
;
2215 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
2222 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2224 upgrade_cbdata_t
*cb
= data
;
2225 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2226 int needed_spa_version
;
2229 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2232 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2234 if (needed_spa_version
< 0)
2237 if (spa_version
< needed_spa_version
) {
2239 (void) printf(gettext("%s: can not be "
2240 "upgraded; the pool version needs to first "
2241 "be upgraded\nto version %d\n\n"),
2242 zfs_get_name(zhp
), needed_spa_version
);
2248 if (version
< cb
->cb_version
) {
2250 (void) snprintf(verstr
, sizeof (verstr
),
2251 "%llu", cb
->cb_version
);
2252 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2254 * If they did "zfs upgrade -a", then we could
2255 * be doing ioctls to different pools. We need
2256 * to log this history once to each pool, and bypass
2257 * the normal history logging that happens in main().
2259 (void) zpool_log_history(g_zfs
, history_str
);
2260 log_history
= B_FALSE
;
2262 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2263 cb
->cb_numupgraded
++;
2266 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2267 } else if (version
> cb
->cb_version
) {
2268 /* can't downgrade */
2269 (void) printf(gettext("%s: can not be downgraded; "
2270 "it is already at version %u\n"),
2271 zfs_get_name(zhp
), version
);
2274 cb
->cb_numsamegraded
++;
2282 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2285 zfs_do_upgrade(int argc
, char **argv
)
2287 boolean_t all
= B_FALSE
;
2288 boolean_t showversions
= B_FALSE
;
2290 upgrade_cbdata_t cb
= { 0 };
2292 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2295 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2298 flags
|= ZFS_ITER_RECURSE
;
2301 showversions
= B_TRUE
;
2304 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2305 optarg
, &cb
.cb_version
) != 0) {
2306 (void) fprintf(stderr
,
2307 gettext("invalid version %s\n"), optarg
);
2316 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2325 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2327 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2328 cb
.cb_version
|| argc
))
2330 if ((all
|| argc
) && (showversions
))
2336 /* Show info on available versions. */
2337 (void) printf(gettext("The following filesystem versions are "
2339 (void) printf(gettext("VER DESCRIPTION\n"));
2340 (void) printf("--- -----------------------------------------"
2341 "---------------\n");
2342 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2343 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2344 (void) printf(gettext(" 3 Case insensitive and filesystem "
2345 "user identifier (FUID)\n"));
2346 (void) printf(gettext(" 4 userquota, groupquota "
2348 (void) printf(gettext(" 5 System attributes\n"));
2349 (void) printf(gettext("\nFor more information on a particular "
2350 "version, including supported releases,\n"));
2351 (void) printf("see the ZFS Administration Guide.\n\n");
2353 } else if (argc
|| all
) {
2354 /* Upgrade filesystems */
2355 if (cb
.cb_version
== 0)
2356 cb
.cb_version
= ZPL_VERSION
;
2357 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2358 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2359 (void) printf(gettext("%llu filesystems upgraded\n"),
2361 if (cb
.cb_numsamegraded
) {
2362 (void) printf(gettext("%llu filesystems already at "
2364 cb
.cb_numsamegraded
);
2366 if (cb
.cb_numfailed
!= 0)
2369 /* List old-version filesystems */
2371 (void) printf(gettext("This system is currently running "
2372 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2374 flags
|= ZFS_ITER_RECURSE
;
2375 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2376 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2378 found
= cb
.cb_foundone
;
2379 cb
.cb_foundone
= B_FALSE
;
2380 cb
.cb_newer
= B_TRUE
;
2382 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2383 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2385 if (!cb
.cb_foundone
&& !found
) {
2386 (void) printf(gettext("All filesystems are "
2387 "formatted with the current version.\n"));
2395 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2396 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2397 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2398 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2399 * zfs projectspace [-Hp] [-o field[,...]] [-s field [-s field]...]
2400 * [-S field [-S field]...] filesystem | snapshot
2402 * -H Scripted mode; elide headers and separate columns by tabs.
2403 * -i Translate SID to POSIX ID.
2404 * -n Print numeric ID instead of user/group name.
2405 * -o Control which fields to display.
2406 * -p Use exact (parsable) numeric output.
2407 * -s Specify sort columns, descending order.
2408 * -S Specify sort columns, ascending order.
2409 * -t Control which object types to display.
2411 * Displays space consumed by, and quotas on, each user in the specified
2412 * filesystem or snapshot.
2415 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2416 enum us_field_types
{
2424 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA",
2425 "OBJUSED", "OBJQUOTA" };
2426 static char *us_field_names
[] = { "type", "name", "used", "quota",
2427 "objused", "objquota" };
2428 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2430 #define USTYPE_PSX_GRP (1 << 0)
2431 #define USTYPE_PSX_USR (1 << 1)
2432 #define USTYPE_SMB_GRP (1 << 2)
2433 #define USTYPE_SMB_USR (1 << 3)
2434 #define USTYPE_PROJ (1 << 4)
2435 #define USTYPE_ALL \
2436 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR | \
2439 static int us_type_bits
[] = {
2446 static char *us_type_names
[] = { "posixgroup", "posixuser", "smbgroup",
2449 typedef struct us_node
{
2451 uu_avl_node_t usn_avlnode
;
2452 uu_list_node_t usn_listnode
;
2455 typedef struct us_cbdata
{
2457 uu_avl_pool_t
*cb_avl_pool
;
2459 boolean_t cb_numname
;
2460 boolean_t cb_nicenum
;
2461 boolean_t cb_sid2posix
;
2462 zfs_userquota_prop_t cb_prop
;
2463 zfs_sort_column_t
*cb_sortcol
;
2464 size_t cb_width
[USFIELD_LAST
];
2467 static boolean_t us_populated
= B_FALSE
;
2470 zfs_sort_column_t
*si_sortcol
;
2471 boolean_t si_numname
;
2475 us_field_index(char *field
)
2479 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2480 if (strcmp(field
, us_field_names
[i
]) == 0)
2488 us_compare(const void *larg
, const void *rarg
, void *unused
)
2490 const us_node_t
*l
= larg
;
2491 const us_node_t
*r
= rarg
;
2492 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2493 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2494 boolean_t numname
= si
->si_numname
;
2495 nvlist_t
*lnvl
= l
->usn_nvl
;
2496 nvlist_t
*rnvl
= r
->usn_nvl
;
2500 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2507 zfs_prop_t prop
= sortcol
->sc_prop
;
2508 const char *propname
= NULL
;
2509 boolean_t reverse
= sortcol
->sc_reverse
;
2514 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2515 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2517 rc
= (rv32
< lv32
) ? 1 : -1;
2522 (void) nvlist_lookup_uint64(lnvl
, propname
,
2524 (void) nvlist_lookup_uint64(rnvl
, propname
,
2527 rc
= (rv64
< lv64
) ? 1 : -1;
2529 (void) nvlist_lookup_string(lnvl
, propname
,
2531 (void) nvlist_lookup_string(rnvl
, propname
,
2533 rc
= strcmp(lvstr
, rvstr
);
2537 case ZFS_PROP_QUOTA
:
2540 if (prop
== ZFS_PROP_USED
)
2544 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2545 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2547 rc
= (rv64
< lv64
) ? 1 : -1;
2556 return (reverse
? 1 : -1);
2558 return (reverse
? -1 : 1);
2563 * If entries still seem to be the same, check if they are of the same
2564 * type (smbentity is added only if we are doing SID to POSIX ID
2565 * translation where we can have duplicate type/name combinations).
2567 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2568 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2570 return (lvb
< rvb
? -1 : 1);
2576 zfs_prop_is_user(unsigned p
)
2578 return (p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
||
2579 p
== ZFS_PROP_USEROBJUSED
|| p
== ZFS_PROP_USEROBJQUOTA
);
2583 zfs_prop_is_group(unsigned p
)
2585 return (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
||
2586 p
== ZFS_PROP_GROUPOBJUSED
|| p
== ZFS_PROP_GROUPOBJQUOTA
);
2590 zfs_prop_is_project(unsigned p
)
2592 return (p
== ZFS_PROP_PROJECTUSED
|| p
== ZFS_PROP_PROJECTQUOTA
||
2593 p
== ZFS_PROP_PROJECTOBJUSED
|| p
== ZFS_PROP_PROJECTOBJQUOTA
);
2596 static inline const char *
2597 us_type2str(unsigned field_type
)
2599 switch (field_type
) {
2600 case USTYPE_PSX_USR
:
2601 return ("POSIX User");
2602 case USTYPE_PSX_GRP
:
2603 return ("POSIX Group");
2604 case USTYPE_SMB_USR
:
2605 return ("SMB User");
2606 case USTYPE_SMB_GRP
:
2607 return ("SMB Group");
2611 return ("Undefined");
2616 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2618 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2619 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2624 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2625 uu_avl_t
*avl
= cb
->cb_avl
;
2629 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2631 const char *typestr
;
2635 int typeidx
, nameidx
, sizeidx
;
2636 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2637 boolean_t smbentity
= B_FALSE
;
2639 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2641 node
= safe_malloc(sizeof (us_node_t
));
2642 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2643 node
->usn_nvl
= props
;
2645 if (domain
!= NULL
&& domain
[0] != '\0') {
2647 char sid
[MAXNAMELEN
+ 32];
2650 int flag
= IDMAP_REQ_FLG_USE_CACHE
;
2654 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2656 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2657 type
= USTYPE_SMB_GRP
;
2658 err
= sid_to_id(sid
, B_FALSE
, &id
);
2660 type
= USTYPE_SMB_USR
;
2661 err
= sid_to_id(sid
, B_TRUE
, &id
);
2666 if (!cb
->cb_sid2posix
) {
2667 if (type
== USTYPE_SMB_USR
) {
2668 (void) idmap_getwinnamebyuid(rid
, flag
,
2671 (void) idmap_getwinnamebygid(rid
, flag
,
2680 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2682 if (zfs_prop_is_group(prop
)) {
2683 type
= USTYPE_PSX_GRP
;
2684 if (!cb
->cb_numname
) {
2687 if ((g
= getgrgid(rid
)) != NULL
)
2690 } else if (zfs_prop_is_user(prop
)) {
2691 type
= USTYPE_PSX_USR
;
2692 if (!cb
->cb_numname
) {
2695 if ((p
= getpwuid(rid
)) != NULL
)
2704 * Make sure that the type/name combination is unique when doing
2705 * SID to POSIX ID translation (hence changing the type from SMB to
2708 if (cb
->cb_sid2posix
&&
2709 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2712 /* Calculate/update width of TYPE field */
2713 typestr
= us_type2str(type
);
2714 typelen
= strlen(gettext(typestr
));
2715 typeidx
= us_field_index("type");
2716 if (typelen
> cb
->cb_width
[typeidx
])
2717 cb
->cb_width
[typeidx
] = typelen
;
2718 if (nvlist_add_uint32(props
, "type", type
) != 0)
2721 /* Calculate/update width of NAME field */
2722 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2723 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2725 namelen
= snprintf(NULL
, 0, "%u", rid
);
2727 if (nvlist_add_string(props
, "name", name
) != 0)
2729 namelen
= strlen(name
);
2731 nameidx
= us_field_index("name");
2732 if (namelen
> cb
->cb_width
[nameidx
])
2733 cb
->cb_width
[nameidx
] = namelen
;
2736 * Check if this type/name combination is in the list and update it;
2737 * otherwise add new node to the list.
2739 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2740 uu_avl_insert(avl
, node
, idx
);
2745 props
= node
->usn_nvl
;
2748 /* Calculate/update width of USED/QUOTA fields */
2749 if (cb
->cb_nicenum
) {
2750 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
||
2751 prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
||
2752 prop
== ZFS_PROP_PROJECTUSED
||
2753 prop
== ZFS_PROP_PROJECTQUOTA
) {
2754 zfs_nicebytes(space
, sizebuf
, sizeof (sizebuf
));
2756 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2759 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu", space
);
2761 sizelen
= strlen(sizebuf
);
2762 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
||
2763 prop
== ZFS_PROP_PROJECTUSED
) {
2765 if (!nvlist_exists(props
, "quota"))
2766 (void) nvlist_add_uint64(props
, "quota", 0);
2767 } else if (prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
||
2768 prop
== ZFS_PROP_PROJECTQUOTA
) {
2770 if (!nvlist_exists(props
, "used"))
2771 (void) nvlist_add_uint64(props
, "used", 0);
2772 } else if (prop
== ZFS_PROP_USEROBJUSED
||
2773 prop
== ZFS_PROP_GROUPOBJUSED
|| prop
== ZFS_PROP_PROJECTOBJUSED
) {
2774 propname
= "objused";
2775 if (!nvlist_exists(props
, "objquota"))
2776 (void) nvlist_add_uint64(props
, "objquota", 0);
2777 } else if (prop
== ZFS_PROP_USEROBJQUOTA
||
2778 prop
== ZFS_PROP_GROUPOBJQUOTA
||
2779 prop
== ZFS_PROP_PROJECTOBJQUOTA
) {
2780 propname
= "objquota";
2781 if (!nvlist_exists(props
, "objused"))
2782 (void) nvlist_add_uint64(props
, "objused", 0);
2786 sizeidx
= us_field_index(propname
);
2787 if (sizelen
> cb
->cb_width
[sizeidx
])
2788 cb
->cb_width
[sizeidx
] = sizelen
;
2790 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2797 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2798 size_t *width
, us_node_t
*node
)
2800 nvlist_t
*nvl
= node
->usn_nvl
;
2801 char valstr
[MAXNAMELEN
];
2802 boolean_t first
= B_TRUE
;
2808 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2809 if (!(ustype
& types
))
2812 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2813 nvpair_t
*nvp
= NULL
;
2819 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2820 if (strcmp(nvpair_name(nvp
),
2821 us_field_names
[field
]) == 0)
2825 type
= nvp
== NULL
? DATA_TYPE_UNKNOWN
: nvpair_type(nvp
);
2827 case DATA_TYPE_UINT32
:
2828 (void) nvpair_value_uint32(nvp
, &val32
);
2830 case DATA_TYPE_UINT64
:
2831 (void) nvpair_value_uint64(nvp
, &val64
);
2833 case DATA_TYPE_STRING
:
2834 (void) nvpair_value_string(nvp
, &strval
);
2836 case DATA_TYPE_UNKNOWN
:
2839 (void) fprintf(stderr
, "invalid data type\n");
2844 if (type
== DATA_TYPE_UINT32
)
2845 strval
= (char *)us_type2str(val32
);
2848 if (type
== DATA_TYPE_UINT64
) {
2849 (void) sprintf(valstr
, "%llu", val64
);
2855 if (type
== DATA_TYPE_UINT64
) {
2857 (void) sprintf(valstr
, "%llu", val64
);
2859 } else if (field
== USFIELD_QUOTA
&&
2863 zfs_nicebytes(val64
, valstr
,
2869 case USFIELD_OBJUSED
:
2870 case USFIELD_OBJQUOTA
:
2871 if (type
== DATA_TYPE_UINT64
) {
2873 (void) sprintf(valstr
, "%llu", val64
);
2875 } else if (field
== USFIELD_OBJQUOTA
&&
2879 zfs_nicenum(val64
, valstr
,
2889 (void) printf("\t");
2894 (void) printf("%s", strval
);
2895 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2896 (void) printf("%-*s", width
[field
], strval
);
2898 (void) printf("%*s", width
[field
], strval
);
2904 (void) printf("\n");
2908 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2909 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
2917 boolean_t first
= B_TRUE
;
2919 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2920 col
= gettext(us_field_hdr
[field
]);
2921 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
2922 (void) printf(first
? "%-*s" : " %-*s",
2925 (void) printf(first
? "%*s" : " %*s",
2931 (void) printf("\n");
2934 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
2935 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
2937 nvlist_free(node
->usn_nvl
);
2942 zfs_do_userspace(int argc
, char **argv
)
2945 zfs_userquota_prop_t p
;
2946 uu_avl_pool_t
*avl_pool
;
2948 uu_avl_walk_t
*walk
;
2950 char deffields
[] = "type,name,used,quota,objused,objquota";
2951 char *ofield
= NULL
;
2952 char *tfield
= NULL
;
2956 boolean_t scripted
= B_FALSE
;
2957 boolean_t prtnum
= B_FALSE
;
2958 boolean_t parsable
= B_FALSE
;
2959 boolean_t sid2posix
= B_FALSE
;
2962 zfs_sort_column_t
*sortcol
= NULL
;
2963 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
2967 uu_list_pool_t
*listpool
;
2969 uu_avl_index_t idx
= 0;
2970 uu_list_index_t idx2
= 0;
2975 if (strcmp(argv
[0], "groupspace") == 0) {
2976 /* Toggle default group types */
2977 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
2978 } else if (strcmp(argv
[0], "projectspace") == 0) {
2979 types
= USTYPE_PROJ
;
2983 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
2986 if (types
== USTYPE_PROJ
) {
2987 (void) fprintf(stderr
,
2988 gettext("invalid option 'n'\n"));
3004 if (zfs_add_sort_column(&sortcol
, optarg
,
3005 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
3006 (void) fprintf(stderr
,
3007 gettext("invalid field '%s'\n"), optarg
);
3012 if (types
== USTYPE_PROJ
) {
3013 (void) fprintf(stderr
,
3014 gettext("invalid option 't'\n"));
3020 if (types
== USTYPE_PROJ
) {
3021 (void) fprintf(stderr
,
3022 gettext("invalid option 'i'\n"));
3028 (void) fprintf(stderr
, gettext("missing argument for "
3029 "'%c' option\n"), optopt
);
3033 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3043 (void) fprintf(stderr
, gettext("missing dataset name\n"));
3047 (void) fprintf(stderr
, gettext("too many arguments\n"));
3051 /* Use default output fields if not specified using -o */
3055 if ((delim
= strchr(ofield
, ',')) != NULL
)
3057 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
3058 (void) fprintf(stderr
, gettext("invalid type '%s' "
3059 "for -o option\n"), ofield
);
3064 } while (delim
!= NULL
);
3065 fields
[cfield
] = USFIELD_LAST
;
3067 /* Override output types (-t option) */
3068 if (tfield
!= NULL
) {
3072 boolean_t found
= B_FALSE
;
3074 if ((delim
= strchr(tfield
, ',')) != NULL
)
3076 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
3078 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
3080 types
|= us_type_bits
[i
];
3085 (void) fprintf(stderr
, gettext("invalid type "
3086 "'%s' for -t option\n"), tfield
);
3091 } while (delim
!= NULL
);
3094 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
3097 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
3098 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
3100 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
3103 /* Always add default sorting columns */
3104 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
3105 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
3107 cb
.cb_sortcol
= sortcol
;
3108 cb
.cb_numname
= prtnum
;
3109 cb
.cb_nicenum
= !parsable
;
3110 cb
.cb_avl_pool
= avl_pool
;
3111 cb
.cb_avl
= avl_tree
;
3112 cb
.cb_sid2posix
= sid2posix
;
3114 for (i
= 0; i
< USFIELD_LAST
; i
++)
3115 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
3117 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
3118 if ((zfs_prop_is_user(p
) &&
3119 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
3120 (zfs_prop_is_group(p
) &&
3121 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))) ||
3122 (zfs_prop_is_project(p
) && types
!= USTYPE_PROJ
))
3126 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0)
3131 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
3134 us_populated
= B_TRUE
;
3136 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
3137 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
3138 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
3139 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
3141 while (node
!= NULL
) {
3143 node
= uu_avl_next(avl_tree
, node
);
3144 uu_avl_remove(avl_tree
, rmnode
);
3145 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
3146 uu_list_insert(list
, rmnode
, idx2
);
3149 for (node
= uu_list_first(list
); node
!= NULL
;
3150 node
= uu_list_next(list
, node
)) {
3151 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
3153 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
3154 uu_avl_insert(avl_tree
, node
, idx
);
3157 uu_list_destroy(list
);
3158 uu_list_pool_destroy(listpool
);
3160 /* Print and free node nvlist memory */
3161 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
3164 zfs_free_sort_columns(sortcol
);
3166 /* Clean up the AVL tree */
3167 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
3170 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
3171 uu_avl_remove(cb
.cb_avl
, node
);
3175 uu_avl_walk_end(walk
);
3176 uu_avl_destroy(avl_tree
);
3177 uu_avl_pool_destroy(avl_pool
);
3183 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
3184 * [-t type[,...]] [filesystem|volume|snapshot] ...
3186 * -H Scripted mode; elide headers and separate columns by tabs.
3187 * -p Display values in parsable (literal) format.
3188 * -r Recurse over all children.
3189 * -d Limit recursion by depth.
3190 * -o Control which fields to display.
3191 * -s Specify sort columns, descending order.
3192 * -S Specify sort columns, ascending order.
3193 * -t Control which object types to display.
3195 * When given no arguments, list all filesystems in the system.
3196 * Otherwise, list the specified datasets, optionally recursing down them if
3197 * '-r' is specified.
3199 typedef struct list_cbdata
{
3201 boolean_t cb_literal
;
3202 boolean_t cb_scripted
;
3203 zprop_list_t
*cb_proplist
;
3207 * Given a list of columns to display, output appropriate headers for each one.
3210 print_header(list_cbdata_t
*cb
)
3212 zprop_list_t
*pl
= cb
->cb_proplist
;
3213 char headerbuf
[ZFS_MAXPROPLEN
];
3216 boolean_t first
= B_TRUE
;
3217 boolean_t right_justify
;
3219 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3226 right_justify
= B_FALSE
;
3227 if (pl
->pl_prop
!= ZPROP_INVAL
) {
3228 header
= zfs_prop_column_name(pl
->pl_prop
);
3229 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3231 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
3232 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
3233 headerbuf
[i
] = '\0';
3237 if (pl
->pl_next
== NULL
&& !right_justify
)
3238 (void) printf("%s", header
);
3239 else if (right_justify
)
3240 (void) printf("%*s", pl
->pl_width
, header
);
3242 (void) printf("%-*s", pl
->pl_width
, header
);
3245 (void) printf("\n");
3249 * Given a dataset and a list of fields, print out all the properties according
3250 * to the described layout.
3253 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
3255 zprop_list_t
*pl
= cb
->cb_proplist
;
3256 boolean_t first
= B_TRUE
;
3257 char property
[ZFS_MAXPROPLEN
];
3258 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
3261 boolean_t right_justify
;
3263 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3265 if (cb
->cb_scripted
)
3266 (void) printf("\t");
3273 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
3274 (void) strlcpy(property
, zfs_get_name(zhp
),
3277 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3278 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
3279 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
3280 sizeof (property
), NULL
, NULL
, 0,
3281 cb
->cb_literal
) != 0)
3285 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3286 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
3287 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
3288 property
, sizeof (property
), cb
->cb_literal
) != 0)
3292 right_justify
= B_TRUE
;
3293 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
3294 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
3295 property
, sizeof (property
), cb
->cb_literal
) != 0)
3299 right_justify
= B_TRUE
;
3301 if (nvlist_lookup_nvlist(userprops
,
3302 pl
->pl_user_prop
, &propval
) != 0)
3305 verify(nvlist_lookup_string(propval
,
3306 ZPROP_VALUE
, &propstr
) == 0);
3307 right_justify
= B_FALSE
;
3311 * If this is being called in scripted mode, or if this is the
3312 * last column and it is left-justified, don't include a width
3315 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3316 (void) printf("%s", propstr
);
3317 else if (right_justify
)
3318 (void) printf("%*s", pl
->pl_width
, propstr
);
3320 (void) printf("%-*s", pl
->pl_width
, propstr
);
3323 (void) printf("\n");
3327 * Generic callback function to list a dataset or snapshot.
3330 list_callback(zfs_handle_t
*zhp
, void *data
)
3332 list_cbdata_t
*cbp
= data
;
3334 if (cbp
->cb_first
) {
3335 if (!cbp
->cb_scripted
)
3337 cbp
->cb_first
= B_FALSE
;
3340 print_dataset(zhp
, cbp
);
3346 zfs_do_list(int argc
, char **argv
)
3349 static char default_fields
[] =
3350 "name,used,available,referenced,mountpoint";
3351 int types
= ZFS_TYPE_DATASET
;
3352 boolean_t types_specified
= B_FALSE
;
3353 char *fields
= NULL
;
3354 list_cbdata_t cb
= { 0 };
3358 zfs_sort_column_t
*sortcol
= NULL
;
3359 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3362 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3368 cb
.cb_literal
= B_TRUE
;
3369 flags
|= ZFS_ITER_LITERAL_PROPS
;
3372 limit
= parse_depth(optarg
, &flags
);
3375 flags
|= ZFS_ITER_RECURSE
;
3378 cb
.cb_scripted
= B_TRUE
;
3381 if (zfs_add_sort_column(&sortcol
, optarg
,
3383 (void) fprintf(stderr
,
3384 gettext("invalid property '%s'\n"), optarg
);
3389 if (zfs_add_sort_column(&sortcol
, optarg
,
3391 (void) fprintf(stderr
,
3392 gettext("invalid property '%s'\n"), optarg
);
3398 types_specified
= B_TRUE
;
3399 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3400 while (*optarg
!= '\0') {
3401 static char *type_subopts
[] = { "filesystem",
3402 "volume", "snapshot", "snap", "bookmark",
3405 switch (getsubopt(&optarg
, type_subopts
,
3408 types
|= ZFS_TYPE_FILESYSTEM
;
3411 types
|= ZFS_TYPE_VOLUME
;
3415 types
|= ZFS_TYPE_SNAPSHOT
;
3418 types
|= ZFS_TYPE_BOOKMARK
;
3421 types
= ZFS_TYPE_DATASET
|
3425 (void) fprintf(stderr
,
3426 gettext("invalid type '%s'\n"),
3433 (void) fprintf(stderr
, gettext("missing argument for "
3434 "'%c' option\n"), optopt
);
3438 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3448 fields
= default_fields
;
3451 * If we are only going to list snapshot names and sort by name,
3452 * then we can use faster version.
3454 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3455 flags
|= ZFS_ITER_SIMPLE
;
3458 * If "-o space" and no types were specified, don't display snapshots.
3460 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3461 types
&= ~ZFS_TYPE_SNAPSHOT
;
3464 * If the user specifies '-o all', the zprop_get_list() doesn't
3465 * normally include the name of the dataset. For 'zfs list', we always
3466 * want this property to be first.
3468 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3472 cb
.cb_first
= B_TRUE
;
3474 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3475 limit
, list_callback
, &cb
);
3477 zprop_free_list(cb
.cb_proplist
);
3478 zfs_free_sort_columns(sortcol
);
3480 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3481 (void) printf(gettext("no datasets available\n"));
3487 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3488 * zfs rename [-f] -p <fs | vol> <fs | vol>
3489 * zfs rename -r <snap> <snap>
3491 * Renames the given dataset to another of the same type.
3493 * The '-p' flag creates all the non-existing ancestors of the target first.
3497 zfs_do_rename(int argc
, char **argv
)
3502 boolean_t recurse
= B_FALSE
;
3503 boolean_t parents
= B_FALSE
;
3504 boolean_t force_unmount
= B_FALSE
;
3507 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3516 force_unmount
= B_TRUE
;
3520 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3529 /* check number of arguments */
3531 (void) fprintf(stderr
, gettext("missing source dataset "
3536 (void) fprintf(stderr
, gettext("missing target dataset "
3541 (void) fprintf(stderr
, gettext("too many arguments\n"));
3545 if (recurse
&& parents
) {
3546 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3551 if (recurse
&& strchr(argv
[0], '@') == 0) {
3552 (void) fprintf(stderr
, gettext("source dataset for recursive "
3553 "rename must be a snapshot\n"));
3557 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3558 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3561 /* If we were asked and the name looks good, try to create ancestors. */
3562 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3563 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3568 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3577 * Promotes the given clone fs to be the parent
3581 zfs_do_promote(int argc
, char **argv
)
3587 if (argc
> 1 && argv
[1][0] == '-') {
3588 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3593 /* check number of arguments */
3595 (void) fprintf(stderr
, gettext("missing clone filesystem"
3600 (void) fprintf(stderr
, gettext("too many arguments\n"));
3604 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3608 ret
= (zfs_promote(zhp
) != 0);
3616 * zfs rollback [-rRf] <snapshot>
3618 * -r Delete any intervening snapshots before doing rollback
3619 * -R Delete any snapshots and their clones
3620 * -f ignored for backwards compatability
3622 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3623 * since then and making it the active dataset. If more recent snapshots exist,
3624 * the command will complain unless the '-r' flag is given.
3626 typedef struct rollback_cbdata
{
3632 boolean_t cb_recurse
;
3633 } rollback_cbdata_t
;
3636 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3638 rollback_cbdata_t
*cbp
= data
;
3640 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3641 (void) fprintf(stderr
, gettext("cannot rollback to "
3642 "'%s': clones of previous snapshots exist\n"),
3644 (void) fprintf(stderr
, gettext("use '-R' to "
3645 "force deletion of the following clones and "
3651 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3658 * Report any snapshots more recent than the one specified. Used when '-r' is
3659 * not specified. We reuse this same callback for the snapshot dependents - if
3660 * 'cb_dependent' is set, then this is a dependent and we should report it
3661 * without checking the transaction group.
3664 rollback_check(zfs_handle_t
*zhp
, void *data
)
3666 rollback_cbdata_t
*cbp
= data
;
3668 if (cbp
->cb_doclones
) {
3673 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3674 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3675 (void) fprintf(stderr
, gettext("cannot "
3676 "rollback to '%s': more recent snapshots "
3677 "or bookmarks exist\n"),
3679 (void) fprintf(stderr
, gettext("use '-r' to "
3680 "force deletion of the following "
3681 "snapshots and bookmarks:\n"));
3686 if (cbp
->cb_recurse
) {
3687 if (zfs_iter_dependents(zhp
, B_TRUE
,
3688 rollback_check_dependent
, cbp
) != 0) {
3693 (void) fprintf(stderr
, "%s\n",
3702 zfs_do_rollback(int argc
, char **argv
)
3706 boolean_t force
= B_FALSE
;
3707 rollback_cbdata_t cb
= { 0 };
3708 zfs_handle_t
*zhp
, *snap
;
3709 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3713 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3726 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3735 /* check number of arguments */
3737 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3741 (void) fprintf(stderr
, gettext("too many arguments\n"));
3745 /* open the snapshot */
3746 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3749 /* open the parent dataset */
3750 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3751 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3753 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3759 * Check for more recent snapshots and/or clones based on the presence
3762 cb
.cb_target
= argv
[0];
3763 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3764 cb
.cb_first
= B_TRUE
;
3766 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, rollback_check
, &cb
)) != 0)
3768 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
3771 if ((ret
= cb
.cb_error
) != 0)
3775 * Rollback parent to the given snapshot.
3777 ret
= zfs_rollback(zhp
, snap
, force
);
3790 * zfs set property=value ... { fs | snap | vol } ...
3792 * Sets the given properties for all datasets specified on the command line.
3796 set_callback(zfs_handle_t
*zhp
, void *data
)
3798 nvlist_t
*props
= data
;
3800 if (zfs_prop_set_list(zhp
, props
) != 0) {
3801 switch (libzfs_errno(g_zfs
)) {
3802 case EZFS_MOUNTFAILED
:
3803 (void) fprintf(stderr
, gettext("property may be set "
3804 "but unable to remount filesystem\n"));
3806 case EZFS_SHARENFSFAILED
:
3807 (void) fprintf(stderr
, gettext("property may be set "
3808 "but unable to reshare filesystem\n"));
3817 zfs_do_set(int argc
, char **argv
)
3819 nvlist_t
*props
= NULL
;
3820 int ds_start
= -1; /* argv idx of first dataset arg */
3823 /* check for options */
3824 if (argc
> 1 && argv
[1][0] == '-') {
3825 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3830 /* check number of arguments */
3832 (void) fprintf(stderr
, gettext("missing arguments\n"));
3836 if (strchr(argv
[1], '=') == NULL
) {
3837 (void) fprintf(stderr
, gettext("missing property=value "
3840 (void) fprintf(stderr
, gettext("missing dataset "
3846 /* validate argument order: prop=val args followed by dataset args */
3847 for (int i
= 1; i
< argc
; i
++) {
3848 if (strchr(argv
[i
], '=') != NULL
) {
3850 /* out-of-order prop=val argument */
3851 (void) fprintf(stderr
, gettext("invalid "
3852 "argument order\n"), i
);
3855 } else if (ds_start
< 0) {
3860 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
3864 /* Populate a list of property settings */
3865 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3867 for (int i
= 1; i
< ds_start
; i
++) {
3868 if (!parseprop(props
, argv
[i
])) {
3874 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
3875 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, props
);
3882 typedef struct snap_cbdata
{
3884 boolean_t sd_recursive
;
3885 const char *sd_snapname
;
3889 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3891 snap_cbdata_t
*sd
= arg
;
3896 if (sd
->sd_recursive
&&
3897 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
3902 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3905 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3908 if (sd
->sd_recursive
)
3909 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3915 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3917 * Creates a snapshot with the given name. While functionally equivalent to
3918 * 'zfs create', it is a separate command to differentiate intent.
3921 zfs_do_snapshot(int argc
, char **argv
)
3926 snap_cbdata_t sd
= { 0 };
3927 boolean_t multiple_snaps
= B_FALSE
;
3929 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3931 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
3935 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
3938 if (!parseprop(props
, optarg
)) {
3939 nvlist_free(sd
.sd_nvl
);
3945 sd
.sd_recursive
= B_TRUE
;
3946 multiple_snaps
= B_TRUE
;
3949 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3958 /* check number of arguments */
3960 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
3965 multiple_snaps
= B_TRUE
;
3966 for (; argc
> 0; argc
--, argv
++) {
3970 atp
= strchr(argv
[0], '@');
3974 sd
.sd_snapname
= atp
+ 1;
3975 zhp
= zfs_open(g_zfs
, argv
[0],
3976 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3979 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
3983 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
3984 nvlist_free(sd
.sd_nvl
);
3986 if (ret
!= 0 && multiple_snaps
)
3987 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
3991 nvlist_free(sd
.sd_nvl
);
3998 * Send a backup stream to stdout.
4001 zfs_do_send(int argc
, char **argv
)
4003 char *fromname
= NULL
;
4004 char *toname
= NULL
;
4005 char *resume_token
= NULL
;
4008 sendflags_t flags
= { 0 };
4010 nvlist_t
*dbgnv
= NULL
;
4011 boolean_t extraverbose
= B_FALSE
;
4013 struct option long_options
[] = {
4014 {"replicate", no_argument
, NULL
, 'R'},
4015 {"props", no_argument
, NULL
, 'p'},
4016 {"parsable", no_argument
, NULL
, 'P'},
4017 {"dedup", no_argument
, NULL
, 'D'},
4018 {"verbose", no_argument
, NULL
, 'v'},
4019 {"dryrun", no_argument
, NULL
, 'n'},
4020 {"large-block", no_argument
, NULL
, 'L'},
4021 {"embed", no_argument
, NULL
, 'e'},
4022 {"resume", required_argument
, NULL
, 't'},
4023 {"compressed", no_argument
, NULL
, 'c'},
4024 {"raw", no_argument
, NULL
, 'w'},
4025 {"backup", no_argument
, NULL
, 'b'},
4026 {"holds", no_argument
, NULL
, 'h'},
4031 while ((c
= getopt_long(argc
, argv
, ":i:I:RDpvnPLeht:cwb", long_options
,
4043 flags
.doall
= B_TRUE
;
4046 flags
.replicate
= B_TRUE
;
4049 flags
.props
= B_TRUE
;
4052 flags
.backup
= B_TRUE
;
4055 flags
.holds
= B_TRUE
;
4058 flags
.parsable
= B_TRUE
;
4059 flags
.verbose
= B_TRUE
;
4063 extraverbose
= B_TRUE
;
4064 flags
.verbose
= B_TRUE
;
4065 flags
.progress
= B_TRUE
;
4068 flags
.dedup
= B_TRUE
;
4071 flags
.dryrun
= B_TRUE
;
4074 flags
.largeblock
= B_TRUE
;
4077 flags
.embed_data
= B_TRUE
;
4080 resume_token
= optarg
;
4083 flags
.compress
= B_TRUE
;
4087 flags
.compress
= B_TRUE
;
4088 flags
.embed_data
= B_TRUE
;
4089 flags
.largeblock
= B_TRUE
;
4093 * If a parameter was not passed, optopt contains the
4094 * value that would normally lead us into the
4095 * appropriate case statement. If it's > 256, then this
4096 * must be a longopt and we should look at argv to get
4097 * the string. Otherwise it's just the character, so we
4098 * should use it directly.
4100 if (optopt
<= UINT8_MAX
) {
4101 (void) fprintf(stderr
,
4102 gettext("missing argument for '%c' "
4103 "option\n"), optopt
);
4105 (void) fprintf(stderr
,
4106 gettext("missing argument for '%s' "
4107 "option\n"), argv
[optind
- 1]);
4115 * If an invalid flag was passed, optopt contains the
4116 * character if it was a short flag, or 0 if it was a
4120 (void) fprintf(stderr
,
4121 gettext("invalid option '%c'\n"), optopt
);
4123 (void) fprintf(stderr
,
4124 gettext("invalid option '%s'\n"),
4135 if (resume_token
!= NULL
) {
4136 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
4137 flags
.backup
|| flags
.dedup
) {
4138 (void) fprintf(stderr
,
4139 gettext("invalid flags combined with -t\n"));
4143 (void) fprintf(stderr
, gettext("no additional "
4144 "arguments are permitted with -t\n"));
4149 (void) fprintf(stderr
,
4150 gettext("missing snapshot argument\n"));
4154 (void) fprintf(stderr
, gettext("too many arguments\n"));
4159 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
4160 (void) fprintf(stderr
,
4161 gettext("Error: Stream can not be written to a terminal.\n"
4162 "You must redirect standard output.\n"));
4166 if (resume_token
!= NULL
) {
4167 return (zfs_send_resume(g_zfs
, &flags
, STDOUT_FILENO
,
4172 * Special case sending a filesystem, or from a bookmark.
4174 if (strchr(argv
[0], '@') == NULL
||
4175 (fromname
&& strchr(fromname
, '#') != NULL
)) {
4176 char frombuf
[ZFS_MAX_DATASET_NAME_LEN
];
4177 enum lzc_send_flags lzc_flags
= 0;
4179 if (flags
.replicate
|| flags
.doall
|| flags
.props
||
4180 flags
.backup
|| flags
.dedup
|| flags
.holds
||
4181 flags
.dryrun
|| flags
.verbose
|| flags
.progress
) {
4182 (void) fprintf(stderr
,
4184 "Unsupported flag with filesystem or bookmark.\n"));
4188 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
4192 if (flags
.largeblock
)
4193 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
4194 if (flags
.embed_data
)
4195 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
4197 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
4199 lzc_flags
|= LZC_SEND_FLAG_RAW
;
4201 if (fromname
!= NULL
&&
4202 (fromname
[0] == '#' || fromname
[0] == '@')) {
4204 * Incremental source name begins with # or @.
4205 * Default to same fs as target.
4207 (void) strlcpy(frombuf
, argv
[0], sizeof (frombuf
));
4208 cp
= strchr(frombuf
, '@');
4211 (void) strlcat(frombuf
, fromname
, sizeof (frombuf
));
4214 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, lzc_flags
);
4219 cp
= strchr(argv
[0], '@');
4222 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4227 * If they specified the full path to the snapshot, chop off
4228 * everything except the short name of the snapshot, but special
4229 * case if they specify the origin.
4231 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
4232 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
4235 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
4236 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
4238 if (strcmp(origin
, fromname
) == 0) {
4240 flags
.fromorigin
= B_TRUE
;
4243 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
4244 (void) fprintf(stderr
,
4245 gettext("incremental source must be "
4246 "in same filesystem\n"));
4250 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
4251 (void) fprintf(stderr
,
4252 gettext("invalid incremental source\n"));
4258 if (flags
.replicate
&& fromname
== NULL
)
4259 flags
.doall
= B_TRUE
;
4261 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
4262 extraverbose
? &dbgnv
: NULL
);
4264 if (extraverbose
&& dbgnv
!= NULL
) {
4266 * dump_nvlist prints to stdout, but that's been
4267 * redirected to a file. Make it print to stderr
4270 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
4271 dump_nvlist(dbgnv
, 0);
4280 * Restore a backup stream from stdin.
4283 zfs_do_receive(int argc
, char **argv
)
4286 recvflags_t flags
= { 0 };
4287 boolean_t abort_resumable
= B_FALSE
;
4290 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
4294 while ((c
= getopt(argc
, argv
, ":o:x:dehnuvFsA")) != -1) {
4297 if (!parseprop(props
, optarg
)) {
4303 if (!parsepropname(props
, optarg
)) {
4309 flags
.isprefix
= B_TRUE
;
4312 flags
.isprefix
= B_TRUE
;
4313 flags
.istail
= B_TRUE
;
4316 flags
.skipholds
= B_TRUE
;
4319 flags
.dryrun
= B_TRUE
;
4322 flags
.nomount
= B_TRUE
;
4325 flags
.verbose
= B_TRUE
;
4328 flags
.resumable
= B_TRUE
;
4331 flags
.force
= B_TRUE
;
4334 abort_resumable
= B_TRUE
;
4337 (void) fprintf(stderr
, gettext("missing argument for "
4338 "'%c' option\n"), optopt
);
4342 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4351 /* check number of arguments */
4353 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4357 (void) fprintf(stderr
, gettext("too many arguments\n"));
4361 if (abort_resumable
) {
4362 if (flags
.isprefix
|| flags
.istail
|| flags
.dryrun
||
4363 flags
.resumable
|| flags
.nomount
) {
4364 (void) fprintf(stderr
, gettext("invalid option"));
4368 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
4369 (void) snprintf(namebuf
, sizeof (namebuf
),
4370 "%s/%%recv", argv
[0]);
4372 if (zfs_dataset_exists(g_zfs
, namebuf
,
4373 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) {
4374 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4375 namebuf
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4378 err
= zfs_destroy(zhp
, B_FALSE
);
4380 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4381 argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4384 if (!zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) ||
4385 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
4386 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == -1) {
4387 (void) fprintf(stderr
,
4388 gettext("'%s' does not have any "
4389 "resumable receive state to abort\n"),
4393 err
= zfs_destroy(zhp
, B_FALSE
);
4399 if (isatty(STDIN_FILENO
)) {
4400 (void) fprintf(stderr
,
4401 gettext("Error: Backup stream can not be read "
4402 "from a terminal.\n"
4403 "You must redirect standard input.\n"));
4406 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
4412 * allow/unallow stuff
4414 /* copied from zfs/sys/dsl_deleg.h */
4415 #define ZFS_DELEG_PERM_CREATE "create"
4416 #define ZFS_DELEG_PERM_DESTROY "destroy"
4417 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4418 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4419 #define ZFS_DELEG_PERM_CLONE "clone"
4420 #define ZFS_DELEG_PERM_PROMOTE "promote"
4421 #define ZFS_DELEG_PERM_RENAME "rename"
4422 #define ZFS_DELEG_PERM_MOUNT "mount"
4423 #define ZFS_DELEG_PERM_SHARE "share"
4424 #define ZFS_DELEG_PERM_SEND "send"
4425 #define ZFS_DELEG_PERM_RECEIVE "receive"
4426 #define ZFS_DELEG_PERM_ALLOW "allow"
4427 #define ZFS_DELEG_PERM_USERPROP "userprop"
4428 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4429 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4430 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4431 #define ZFS_DELEG_PERM_USERUSED "userused"
4432 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4433 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
4434 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
4435 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
4436 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
4438 #define ZFS_DELEG_PERM_HOLD "hold"
4439 #define ZFS_DELEG_PERM_RELEASE "release"
4440 #define ZFS_DELEG_PERM_DIFF "diff"
4441 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4442 #define ZFS_DELEG_PERM_REMAP "remap"
4443 #define ZFS_DELEG_PERM_LOAD_KEY "load-key"
4444 #define ZFS_DELEG_PERM_CHANGE_KEY "change-key"
4446 #define ZFS_DELEG_PERM_PROJECTUSED "projectused"
4447 #define ZFS_DELEG_PERM_PROJECTQUOTA "projectquota"
4448 #define ZFS_DELEG_PERM_PROJECTOBJUSED "projectobjused"
4449 #define ZFS_DELEG_PERM_PROJECTOBJQUOTA "projectobjquota"
4451 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4453 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
4454 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
4455 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
4456 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
4457 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
4458 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
4459 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
4460 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
4461 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
4462 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
4463 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
4464 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
4465 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
4466 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
4467 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
4468 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
4469 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
4470 { ZFS_DELEG_PERM_REMAP
, ZFS_DELEG_NOTE_REMAP
},
4471 { ZFS_DELEG_PERM_LOAD_KEY
, ZFS_DELEG_NOTE_LOAD_KEY
},
4472 { ZFS_DELEG_PERM_CHANGE_KEY
, ZFS_DELEG_NOTE_CHANGE_KEY
},
4474 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
4475 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
4476 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
4477 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
4478 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
4479 { ZFS_DELEG_PERM_USEROBJQUOTA
, ZFS_DELEG_NOTE_USEROBJQUOTA
},
4480 { ZFS_DELEG_PERM_USEROBJUSED
, ZFS_DELEG_NOTE_USEROBJUSED
},
4481 { ZFS_DELEG_PERM_GROUPOBJQUOTA
, ZFS_DELEG_NOTE_GROUPOBJQUOTA
},
4482 { ZFS_DELEG_PERM_GROUPOBJUSED
, ZFS_DELEG_NOTE_GROUPOBJUSED
},
4483 { ZFS_DELEG_PERM_PROJECTUSED
, ZFS_DELEG_NOTE_PROJECTUSED
},
4484 { ZFS_DELEG_PERM_PROJECTQUOTA
, ZFS_DELEG_NOTE_PROJECTQUOTA
},
4485 { ZFS_DELEG_PERM_PROJECTOBJUSED
, ZFS_DELEG_NOTE_PROJECTOBJUSED
},
4486 { ZFS_DELEG_PERM_PROJECTOBJQUOTA
, ZFS_DELEG_NOTE_PROJECTOBJQUOTA
},
4487 { NULL
, ZFS_DELEG_NOTE_NONE
}
4490 /* permission structure */
4491 typedef struct deleg_perm
{
4492 zfs_deleg_who_type_t dp_who_type
;
4493 const char *dp_name
;
4495 boolean_t dp_descend
;
4499 typedef struct deleg_perm_node
{
4500 deleg_perm_t dpn_perm
;
4502 uu_avl_node_t dpn_avl_node
;
4503 } deleg_perm_node_t
;
4505 typedef struct fs_perm fs_perm_t
;
4507 /* permissions set */
4508 typedef struct who_perm
{
4509 zfs_deleg_who_type_t who_type
;
4510 const char *who_name
; /* id */
4511 char who_ug_name
[256]; /* user/group name */
4512 fs_perm_t
*who_fsperm
; /* uplink */
4514 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
4518 typedef struct who_perm_node
{
4519 who_perm_t who_perm
;
4520 uu_avl_node_t who_avl_node
;
4523 typedef struct fs_perm_set fs_perm_set_t
;
4524 /* fs permissions */
4526 const char *fsp_name
;
4528 uu_avl_t
*fsp_sc_avl
; /* sets,create */
4529 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
4531 fs_perm_set_t
*fsp_set
; /* uplink */
4535 typedef struct fs_perm_node
{
4536 fs_perm_t fspn_fsperm
;
4539 uu_list_node_t fspn_list_node
;
4542 /* top level structure */
4543 struct fs_perm_set
{
4544 uu_list_pool_t
*fsps_list_pool
;
4545 uu_list_t
*fsps_list
; /* list of fs_perms */
4547 uu_avl_pool_t
*fsps_named_set_avl_pool
;
4548 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
4549 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
4552 static inline const char *
4553 deleg_perm_type(zfs_deleg_note_t note
)
4559 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4560 case ZFS_DELEG_NOTE_GROUPUSED
:
4561 case ZFS_DELEG_NOTE_USERPROP
:
4562 case ZFS_DELEG_NOTE_USERQUOTA
:
4563 case ZFS_DELEG_NOTE_USERUSED
:
4564 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
4565 case ZFS_DELEG_NOTE_USEROBJUSED
:
4566 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
4567 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
4568 case ZFS_DELEG_NOTE_PROJECTUSED
:
4569 case ZFS_DELEG_NOTE_PROJECTQUOTA
:
4570 case ZFS_DELEG_NOTE_PROJECTOBJUSED
:
4571 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA
:
4573 return (gettext("other"));
4575 return (gettext("subcommand"));
4580 who_type2weight(zfs_deleg_who_type_t who_type
)
4584 case ZFS_DELEG_NAMED_SET_SETS
:
4585 case ZFS_DELEG_NAMED_SET
:
4588 case ZFS_DELEG_CREATE_SETS
:
4589 case ZFS_DELEG_CREATE
:
4592 case ZFS_DELEG_USER_SETS
:
4593 case ZFS_DELEG_USER
:
4596 case ZFS_DELEG_GROUP_SETS
:
4597 case ZFS_DELEG_GROUP
:
4600 case ZFS_DELEG_EVERYONE_SETS
:
4601 case ZFS_DELEG_EVERYONE
:
4613 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4615 const who_perm_node_t
*l
= larg
;
4616 const who_perm_node_t
*r
= rarg
;
4617 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
4618 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
4619 int lweight
= who_type2weight(ltype
);
4620 int rweight
= who_type2weight(rtype
);
4621 int res
= lweight
- rweight
;
4623 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
4624 ZFS_MAX_DELEG_NAME
-1);
4636 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4638 const deleg_perm_node_t
*l
= larg
;
4639 const deleg_perm_node_t
*r
= rarg
;
4640 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
4641 ZFS_MAX_DELEG_NAME
-1);
4653 fs_perm_set_init(fs_perm_set_t
*fspset
)
4655 bzero(fspset
, sizeof (fs_perm_set_t
));
4657 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4658 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4659 NULL
, UU_DEFAULT
)) == NULL
)
4661 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4662 UU_DEFAULT
)) == NULL
)
4665 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4666 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4667 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4668 UU_DEFAULT
)) == NULL
)
4671 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4672 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4673 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4674 UU_DEFAULT
)) == NULL
)
4677 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4678 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4679 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4684 static inline void fs_perm_fini(fs_perm_t
*);
4685 static inline void who_perm_fini(who_perm_t
*);
4688 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4690 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4692 while (node
!= NULL
) {
4693 fs_perm_node_t
*next_node
=
4694 uu_list_next(fspset
->fsps_list
, node
);
4695 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
4696 fs_perm_fini(fsperm
);
4697 uu_list_remove(fspset
->fsps_list
, node
);
4702 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
4703 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
4704 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
4708 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
4711 deleg_perm
->dp_who_type
= type
;
4712 deleg_perm
->dp_name
= name
;
4716 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
4717 zfs_deleg_who_type_t type
, const char *name
)
4719 uu_avl_pool_t
*pool
;
4720 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
4722 bzero(who_perm
, sizeof (who_perm_t
));
4724 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
4725 UU_DEFAULT
)) == NULL
)
4728 who_perm
->who_type
= type
;
4729 who_perm
->who_name
= name
;
4730 who_perm
->who_fsperm
= fsperm
;
4734 who_perm_fini(who_perm_t
*who_perm
)
4736 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
4738 while (node
!= NULL
) {
4739 deleg_perm_node_t
*next_node
=
4740 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
4742 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
4747 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
4751 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
4753 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
4754 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
4756 bzero(fsperm
, sizeof (fs_perm_t
));
4758 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
4762 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
4766 fsperm
->fsp_set
= fspset
;
4767 fsperm
->fsp_name
= fsname
;
4771 fs_perm_fini(fs_perm_t
*fsperm
)
4773 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
4774 while (node
!= NULL
) {
4775 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
4777 who_perm_t
*who_perm
= &node
->who_perm
;
4778 who_perm_fini(who_perm
);
4779 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
4784 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
4785 while (node
!= NULL
) {
4786 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
4788 who_perm_t
*who_perm
= &node
->who_perm
;
4789 who_perm_fini(who_perm
);
4790 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
4795 uu_avl_destroy(fsperm
->fsp_sc_avl
);
4796 uu_avl_destroy(fsperm
->fsp_uge_avl
);
4800 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
4801 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
4803 uu_avl_index_t idx
= 0;
4805 deleg_perm_node_t
*found_node
= NULL
;
4806 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
4808 deleg_perm_init(deleg_perm
, who_type
, name
);
4810 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4812 uu_avl_insert(avl
, node
, idx
);
4815 deleg_perm
= &node
->dpn_perm
;
4820 case ZFS_DELEG_LOCAL
:
4821 deleg_perm
->dp_local
= B_TRUE
;
4823 case ZFS_DELEG_DESCENDENT
:
4824 deleg_perm
->dp_descend
= B_TRUE
;
4829 assert(B_FALSE
); /* invalid locality */
4834 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
4836 nvpair_t
*nvp
= NULL
;
4837 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
4838 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
4839 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
4841 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4842 const char *name
= nvpair_name(nvp
);
4843 data_type_t type
= nvpair_type(nvp
);
4844 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
4845 deleg_perm_node_t
*node
=
4846 safe_malloc(sizeof (deleg_perm_node_t
));
4848 assert(type
== DATA_TYPE_BOOLEAN
);
4850 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
4851 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
4858 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
4860 nvpair_t
*nvp
= NULL
;
4861 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
4863 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4864 nvlist_t
*nvl2
= NULL
;
4865 const char *name
= nvpair_name(nvp
);
4866 uu_avl_t
*avl
= NULL
;
4867 uu_avl_pool_t
*avl_pool
= NULL
;
4868 zfs_deleg_who_type_t perm_type
= name
[0];
4869 char perm_locality
= name
[1];
4870 const char *perm_name
= name
+ 3;
4871 boolean_t is_set
= B_TRUE
;
4872 who_perm_t
*who_perm
= NULL
;
4874 assert('$' == name
[2]);
4876 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4879 switch (perm_type
) {
4880 case ZFS_DELEG_CREATE
:
4881 case ZFS_DELEG_CREATE_SETS
:
4882 case ZFS_DELEG_NAMED_SET
:
4883 case ZFS_DELEG_NAMED_SET_SETS
:
4884 avl_pool
= fspset
->fsps_named_set_avl_pool
;
4885 avl
= fsperm
->fsp_sc_avl
;
4887 case ZFS_DELEG_USER
:
4888 case ZFS_DELEG_USER_SETS
:
4889 case ZFS_DELEG_GROUP
:
4890 case ZFS_DELEG_GROUP_SETS
:
4891 case ZFS_DELEG_EVERYONE
:
4892 case ZFS_DELEG_EVERYONE_SETS
:
4893 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
4894 avl
= fsperm
->fsp_uge_avl
;
4898 assert(!"unhandled zfs_deleg_who_type_t");
4902 who_perm_node_t
*found_node
= NULL
;
4903 who_perm_node_t
*node
= safe_malloc(
4904 sizeof (who_perm_node_t
));
4905 who_perm
= &node
->who_perm
;
4906 uu_avl_index_t idx
= 0;
4908 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
4909 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
4911 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
4913 if (avl
== fsperm
->fsp_uge_avl
) {
4915 struct passwd
*p
= NULL
;
4916 struct group
*g
= NULL
;
4917 const char *nice_name
= NULL
;
4919 switch (perm_type
) {
4920 case ZFS_DELEG_USER_SETS
:
4921 case ZFS_DELEG_USER
:
4922 rid
= atoi(perm_name
);
4925 nice_name
= p
->pw_name
;
4927 case ZFS_DELEG_GROUP_SETS
:
4928 case ZFS_DELEG_GROUP
:
4929 rid
= atoi(perm_name
);
4932 nice_name
= g
->gr_name
;
4939 if (nice_name
!= NULL
)
4941 node
->who_perm
.who_ug_name
,
4945 uu_avl_insert(avl
, node
, idx
);
4948 who_perm
= &node
->who_perm
;
4952 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
4959 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
4961 nvpair_t
*nvp
= NULL
;
4962 uu_avl_index_t idx
= 0;
4964 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
4965 nvlist_t
*nvl2
= NULL
;
4966 const char *fsname
= nvpair_name(nvp
);
4967 data_type_t type
= nvpair_type(nvp
);
4968 fs_perm_t
*fsperm
= NULL
;
4969 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
4973 fsperm
= &node
->fspn_fsperm
;
4975 assert(DATA_TYPE_NVLIST
== type
);
4977 uu_list_node_init(node
, &node
->fspn_list_node
,
4978 fspset
->fsps_list_pool
);
4980 idx
= uu_list_numnodes(fspset
->fsps_list
);
4981 fs_perm_init(fsperm
, fspset
, fsname
);
4983 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
4986 (void) parse_fs_perm(fsperm
, nvl2
);
4988 uu_list_insert(fspset
->fsps_list
, node
, idx
);
4994 static inline const char *
4995 deleg_perm_comment(zfs_deleg_note_t note
)
4997 const char *str
= "";
5002 case ZFS_DELEG_NOTE_ALLOW
:
5003 str
= gettext("Must also have the permission that is being"
5004 "\n\t\t\t\tallowed");
5006 case ZFS_DELEG_NOTE_CLONE
:
5007 str
= gettext("Must also have the 'create' ability and 'mount'"
5008 "\n\t\t\t\tability in the origin file system");
5010 case ZFS_DELEG_NOTE_CREATE
:
5011 str
= gettext("Must also have the 'mount' ability");
5013 case ZFS_DELEG_NOTE_DESTROY
:
5014 str
= gettext("Must also have the 'mount' ability");
5016 case ZFS_DELEG_NOTE_DIFF
:
5017 str
= gettext("Allows lookup of paths within a dataset;"
5018 "\n\t\t\t\tgiven an object number. Ordinary users need this"
5019 "\n\t\t\t\tin order to use zfs diff");
5021 case ZFS_DELEG_NOTE_HOLD
:
5022 str
= gettext("Allows adding a user hold to a snapshot");
5024 case ZFS_DELEG_NOTE_MOUNT
:
5025 str
= gettext("Allows mount/umount of ZFS datasets");
5027 case ZFS_DELEG_NOTE_PROMOTE
:
5028 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
5029 " 'promote' ability in the origin file system");
5031 case ZFS_DELEG_NOTE_RECEIVE
:
5032 str
= gettext("Must also have the 'mount' and 'create'"
5035 case ZFS_DELEG_NOTE_RELEASE
:
5036 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
5037 "might destroy the snapshot");
5039 case ZFS_DELEG_NOTE_RENAME
:
5040 str
= gettext("Must also have the 'mount' and 'create'"
5041 "\n\t\t\t\tability in the new parent");
5043 case ZFS_DELEG_NOTE_ROLLBACK
:
5046 case ZFS_DELEG_NOTE_SEND
:
5049 case ZFS_DELEG_NOTE_SHARE
:
5050 str
= gettext("Allows sharing file systems over NFS or SMB"
5051 "\n\t\t\t\tprotocols");
5053 case ZFS_DELEG_NOTE_SNAPSHOT
:
5056 case ZFS_DELEG_NOTE_LOAD_KEY
:
5057 str
= gettext("Allows loading or unloading an encryption key");
5059 case ZFS_DELEG_NOTE_CHANGE_KEY
:
5060 str
= gettext("Allows changing or adding an encryption key");
5063 * case ZFS_DELEG_NOTE_VSCAN:
5064 * str = gettext("");
5068 case ZFS_DELEG_NOTE_GROUPQUOTA
:
5069 str
= gettext("Allows accessing any groupquota@... property");
5071 case ZFS_DELEG_NOTE_GROUPUSED
:
5072 str
= gettext("Allows reading any groupused@... property");
5074 case ZFS_DELEG_NOTE_USERPROP
:
5075 str
= gettext("Allows changing any user property");
5077 case ZFS_DELEG_NOTE_USERQUOTA
:
5078 str
= gettext("Allows accessing any userquota@... property");
5080 case ZFS_DELEG_NOTE_USERUSED
:
5081 str
= gettext("Allows reading any userused@... property");
5083 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
5084 str
= gettext("Allows accessing any userobjquota@... property");
5086 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
5087 str
= gettext("Allows accessing any \n\t\t\t\t"
5088 "groupobjquota@... property");
5090 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
5091 str
= gettext("Allows reading any groupobjused@... property");
5093 case ZFS_DELEG_NOTE_USEROBJUSED
:
5094 str
= gettext("Allows reading any userobjused@... property");
5096 case ZFS_DELEG_NOTE_PROJECTQUOTA
:
5097 str
= gettext("Allows accessing any projectquota@... property");
5099 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA
:
5100 str
= gettext("Allows accessing any \n\t\t\t\t"
5101 "projectobjquota@... property");
5103 case ZFS_DELEG_NOTE_PROJECTUSED
:
5104 str
= gettext("Allows reading any projectused@... property");
5106 case ZFS_DELEG_NOTE_PROJECTOBJUSED
:
5107 str
= gettext("Allows accessing any \n\t\t\t\t"
5108 "projectobjused@... property");
5126 boolean_t recursive
; /* unallow only */
5127 boolean_t prt_usage
;
5129 boolean_t prt_perms
;
5132 const char *dataset
;
5136 prop_cmp(const void *a
, const void *b
)
5138 const char *str1
= *(const char **)a
;
5139 const char *str2
= *(const char **)b
;
5140 return (strcmp(str1
, str2
));
5144 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
5146 const char *opt_desc
[] = {
5147 "-h", gettext("show this help message and exit"),
5148 "-l", gettext("set permission locally"),
5149 "-d", gettext("set permission for descents"),
5150 "-u", gettext("set permission for user"),
5151 "-g", gettext("set permission for group"),
5152 "-e", gettext("set permission for everyone"),
5153 "-c", gettext("set create time permission"),
5154 "-s", gettext("define permission set"),
5156 "-r", gettext("remove permissions recursively"),
5158 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
5159 size_t allow_size
= unallow_size
- 2;
5160 const char *props
[ZFS_NUM_PROPS
];
5163 FILE *fp
= requested
? stdout
: stderr
;
5164 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
5165 const char *fmt
= gettext("%-16s %-14s\t%s\n");
5167 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
5169 (void) fprintf(fp
, gettext("Options:\n"));
5170 for (int i
= 0; i
< (un
? unallow_size
: allow_size
); i
++) {
5171 const char *opt
= opt_desc
[i
++];
5172 const char *optdsc
= opt_desc
[i
];
5173 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
5176 (void) fprintf(fp
, gettext("\nThe following permissions are "
5178 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
5180 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
5181 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
5182 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
5183 const char *perm_type
= deleg_perm_type(perm_note
);
5184 const char *perm_comment
= deleg_perm_comment(perm_note
);
5185 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
5188 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
5189 zprop_desc_t
*pd
= &pdtbl
[i
];
5190 if (pd
->pd_visible
!= B_TRUE
)
5193 if (pd
->pd_attr
== PROP_READONLY
)
5196 props
[count
++] = pd
->pd_name
;
5198 props
[count
] = NULL
;
5200 qsort(props
, count
, sizeof (char *), prop_cmp
);
5202 for (i
= 0; i
< count
; i
++)
5203 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
5206 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
5208 exit(requested
? 0 : 2);
5211 static inline const char *
5212 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
5215 if (un
&& argc
== expected_argc
- 1)
5217 else if (argc
== expected_argc
)
5218 *permsp
= argv
[argc
- 2];
5220 allow_usage(un
, B_FALSE
,
5221 gettext("wrong number of parameters\n"));
5223 return (argv
[argc
- 1]);
5227 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
5229 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
5230 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
5231 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
5232 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
5235 allow_usage(un
, B_FALSE
,
5236 gettext("-u, -g, and -e are mutually exclusive\n"));
5238 if (opts
->prt_usage
) {
5239 if (argc
== 0 && all_sum
== 0)
5240 allow_usage(un
, B_TRUE
, NULL
);
5247 allow_usage(un
, B_FALSE
,
5248 gettext("invalid options combined with -s\n"));
5250 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
5251 if (argv
[0][0] != '@')
5252 allow_usage(un
, B_FALSE
,
5253 gettext("invalid set name: missing '@' prefix\n"));
5254 opts
->who
= argv
[0];
5255 } else if (opts
->create
) {
5256 if (ldcsuge_sum
> 1)
5257 allow_usage(un
, B_FALSE
,
5258 gettext("invalid options combined with -c\n"));
5259 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5260 } else if (opts
->everyone
) {
5262 allow_usage(un
, B_FALSE
,
5263 gettext("invalid options combined with -e\n"));
5264 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5265 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
5267 opts
->everyone
= B_TRUE
;
5270 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5271 } else if (argc
== 1 && !un
) {
5272 opts
->prt_perms
= B_TRUE
;
5273 opts
->dataset
= argv
[argc
-1];
5275 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
5276 opts
->who
= argv
[0];
5279 if (!opts
->local
&& !opts
->descend
) {
5280 opts
->local
= B_TRUE
;
5281 opts
->descend
= B_TRUE
;
5286 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
5287 const char *who
, char *perms
, nvlist_t
*top_nvl
)
5290 char ld
[2] = { '\0', '\0' };
5291 char who_buf
[MAXNAMELEN
+ 32];
5292 char base_type
= '\0';
5293 char set_type
= '\0';
5294 nvlist_t
*base_nvl
= NULL
;
5295 nvlist_t
*set_nvl
= NULL
;
5298 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
5300 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
5304 case ZFS_DELEG_NAMED_SET_SETS
:
5305 case ZFS_DELEG_NAMED_SET
:
5306 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
5307 base_type
= ZFS_DELEG_NAMED_SET
;
5308 ld
[0] = ZFS_DELEG_NA
;
5310 case ZFS_DELEG_CREATE_SETS
:
5311 case ZFS_DELEG_CREATE
:
5312 set_type
= ZFS_DELEG_CREATE_SETS
;
5313 base_type
= ZFS_DELEG_CREATE
;
5314 ld
[0] = ZFS_DELEG_NA
;
5316 case ZFS_DELEG_USER_SETS
:
5317 case ZFS_DELEG_USER
:
5318 set_type
= ZFS_DELEG_USER_SETS
;
5319 base_type
= ZFS_DELEG_USER
;
5321 ld
[0] = ZFS_DELEG_LOCAL
;
5323 ld
[1] = ZFS_DELEG_DESCENDENT
;
5325 case ZFS_DELEG_GROUP_SETS
:
5326 case ZFS_DELEG_GROUP
:
5327 set_type
= ZFS_DELEG_GROUP_SETS
;
5328 base_type
= ZFS_DELEG_GROUP
;
5330 ld
[0] = ZFS_DELEG_LOCAL
;
5332 ld
[1] = ZFS_DELEG_DESCENDENT
;
5334 case ZFS_DELEG_EVERYONE_SETS
:
5335 case ZFS_DELEG_EVERYONE
:
5336 set_type
= ZFS_DELEG_EVERYONE_SETS
;
5337 base_type
= ZFS_DELEG_EVERYONE
;
5339 ld
[0] = ZFS_DELEG_LOCAL
;
5341 ld
[1] = ZFS_DELEG_DESCENDENT
;
5345 assert(set_type
!= '\0' && base_type
!= '\0');
5348 if (perms
!= NULL
) {
5350 char *end
= curr
+ strlen(perms
);
5352 while (curr
< end
) {
5353 char *delim
= strchr(curr
, ',');
5364 (void) nvlist_add_boolean(nvl
, curr
);
5370 for (i
= 0; i
< 2; i
++) {
5371 char locality
= ld
[i
];
5375 if (!nvlist_empty(base_nvl
)) {
5377 (void) snprintf(who_buf
,
5378 sizeof (who_buf
), "%c%c$%s",
5379 base_type
, locality
, who
);
5381 (void) snprintf(who_buf
,
5382 sizeof (who_buf
), "%c%c$",
5383 base_type
, locality
);
5385 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5390 if (!nvlist_empty(set_nvl
)) {
5392 (void) snprintf(who_buf
,
5393 sizeof (who_buf
), "%c%c$%s",
5394 set_type
, locality
, who
);
5396 (void) snprintf(who_buf
,
5397 sizeof (who_buf
), "%c%c$",
5398 set_type
, locality
);
5400 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5405 for (i
= 0; i
< 2; i
++) {
5406 char locality
= ld
[i
];
5411 (void) snprintf(who_buf
, sizeof (who_buf
),
5412 "%c%c$%s", base_type
, locality
, who
);
5414 (void) snprintf(who_buf
, sizeof (who_buf
),
5415 "%c%c$", base_type
, locality
);
5416 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5419 (void) snprintf(who_buf
, sizeof (who_buf
),
5420 "%c%c$%s", set_type
, locality
, who
);
5422 (void) snprintf(who_buf
, sizeof (who_buf
),
5423 "%c%c$", set_type
, locality
);
5424 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5430 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
5432 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
5436 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
5437 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
5438 } else if (opts
->create
) {
5439 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
5440 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5441 } else if (opts
->everyone
) {
5442 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
5443 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5445 char *curr
= opts
->who
;
5446 char *end
= curr
+ strlen(curr
);
5448 while (curr
< end
) {
5450 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
5452 char *delim
= strchr(curr
, ',');
5455 struct passwd
*p
= NULL
;
5456 struct group
*g
= NULL
;
5464 rid
= (uid_t
)strtol(curr
, &endch
, 0);
5466 who_type
= ZFS_DELEG_USER
;
5475 (void) snprintf(errbuf
, 256, gettext(
5476 "invalid user %s"), curr
);
5477 allow_usage(un
, B_TRUE
, errbuf
);
5479 } else if (opts
->group
) {
5480 who_type
= ZFS_DELEG_GROUP
;
5489 (void) snprintf(errbuf
, 256, gettext(
5490 "invalid group %s"), curr
);
5491 allow_usage(un
, B_TRUE
, errbuf
);
5494 if (*endch
!= '\0') {
5501 if (*endch
!= '\0') {
5509 who_type
= ZFS_DELEG_USER
;
5511 } else if (g
!= NULL
) {
5512 who_type
= ZFS_DELEG_GROUP
;
5515 (void) snprintf(errbuf
, 256, gettext(
5516 "invalid user/group %s"), curr
);
5517 allow_usage(un
, B_TRUE
, errbuf
);
5521 (void) sprintf(id
, "%u", rid
);
5524 store_allow_perm(who_type
, opts
->local
,
5525 opts
->descend
, who
, opts
->perms
, *nvlp
);
5534 print_set_creat_perms(uu_avl_t
*who_avl
)
5536 const char *sc_title
[] = {
5537 gettext("Permission sets:\n"),
5538 gettext("Create time permissions:\n"),
5541 who_perm_node_t
*who_node
= NULL
;
5542 int prev_weight
= -1;
5544 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
5545 who_node
= uu_avl_next(who_avl
, who_node
)) {
5546 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5547 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5548 const char *who_name
= who_node
->who_perm
.who_name
;
5549 int weight
= who_type2weight(who_type
);
5550 boolean_t first
= B_TRUE
;
5551 deleg_perm_node_t
*deleg_node
;
5553 if (prev_weight
!= weight
) {
5554 VERIFY3S(weight
, >=, 0);
5555 VERIFY3S(weight
, <=, 1);
5556 (void) printf(sc_title
[weight
]);
5557 prev_weight
= weight
;
5560 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
5561 (void) printf("\t");
5563 (void) printf("\t%s ", who_name
);
5565 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
5566 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5569 deleg_node
->dpn_perm
.dp_name
);
5572 (void) printf(",%s",
5573 deleg_node
->dpn_perm
.dp_name
);
5576 (void) printf("\n");
5581 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
5584 who_perm_node_t
*who_node
= NULL
;
5585 boolean_t prt_title
= B_TRUE
;
5586 uu_avl_walk_t
*walk
;
5588 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
5591 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
5592 const char *who_name
= who_node
->who_perm
.who_name
;
5593 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
5594 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5595 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5597 deleg_perm_node_t
*deleg_node
;
5598 boolean_t prt_who
= B_TRUE
;
5600 for (deleg_node
= uu_avl_first(avl
);
5602 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5603 if (local
!= deleg_node
->dpn_perm
.dp_local
||
5604 descend
!= deleg_node
->dpn_perm
.dp_descend
)
5608 const char *who
= NULL
;
5610 prt_title
= B_FALSE
;
5611 (void) printf(title
);
5615 case ZFS_DELEG_USER_SETS
:
5616 case ZFS_DELEG_USER
:
5617 who
= gettext("user");
5619 who_name
= nice_who_name
;
5621 case ZFS_DELEG_GROUP_SETS
:
5622 case ZFS_DELEG_GROUP
:
5623 who
= gettext("group");
5625 who_name
= nice_who_name
;
5627 case ZFS_DELEG_EVERYONE_SETS
:
5628 case ZFS_DELEG_EVERYONE
:
5629 who
= gettext("everyone");
5634 assert(who
!= NULL
);
5638 if (who_name
== NULL
)
5639 (void) printf("\t%s", who
);
5641 (void) printf("\t%s %s", who
, who_name
);
5644 (void) printf("%c%s", delim
,
5645 deleg_node
->dpn_perm
.dp_name
);
5650 (void) printf("\n");
5653 uu_avl_walk_end(walk
);
5657 print_fs_perms(fs_perm_set_t
*fspset
)
5659 fs_perm_node_t
*node
= NULL
;
5660 char buf
[MAXNAMELEN
+ 32];
5661 const char *dsname
= buf
;
5663 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
5664 node
= uu_list_next(fspset
->fsps_list
, node
)) {
5665 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
5666 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
5669 (void) snprintf(buf
, sizeof (buf
),
5670 gettext("---- Permissions on %s "),
5671 node
->fspn_fsperm
.fsp_name
);
5672 (void) printf(dsname
);
5673 left
= 70 - strlen(buf
);
5676 (void) printf("\n");
5678 print_set_creat_perms(sc_avl
);
5679 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
5680 gettext("Local permissions:\n"));
5681 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
5682 gettext("Descendent permissions:\n"));
5683 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
5684 gettext("Local+Descendent permissions:\n"));
5688 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5690 struct deleg_perms
{
5696 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
5698 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
5699 zfs_type_t zfs_type
= zfs_get_type(zhp
);
5701 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
5704 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
5708 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
5711 nvlist_t
*perm_nvl
= NULL
;
5712 nvlist_t
*update_perm_nvl
= NULL
;
5715 struct allow_opts opts
= { 0 };
5717 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
5720 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
5723 opts
.local
= B_TRUE
;
5726 opts
.descend
= B_TRUE
;
5732 opts
.group
= B_TRUE
;
5735 opts
.everyone
= B_TRUE
;
5741 opts
.create
= B_TRUE
;
5744 opts
.recursive
= B_TRUE
;
5747 (void) fprintf(stderr
, gettext("missing argument for "
5748 "'%c' option\n"), optopt
);
5752 opts
.prt_usage
= B_TRUE
;
5755 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5764 /* check arguments */
5765 parse_allow_args(argc
, argv
, un
, &opts
);
5767 /* try to open the dataset */
5768 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
5769 ZFS_TYPE_VOLUME
)) == NULL
) {
5770 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
5775 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
5778 fs_perm_set_init(&fs_perm_set
);
5779 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
5780 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
5785 print_fs_perms(&fs_perm_set
);
5787 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
5788 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
5791 if (un
&& opts
.recursive
) {
5792 struct deleg_perms data
= { un
, update_perm_nvl
};
5793 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
5802 nvlist_free(perm_nvl
);
5803 nvlist_free(update_perm_nvl
);
5805 fs_perm_set_fini(&fs_perm_set
);
5813 zfs_do_allow(int argc
, char **argv
)
5815 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
5819 zfs_do_unallow(int argc
, char **argv
)
5821 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
5825 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
5830 boolean_t recursive
= B_FALSE
;
5831 const char *opts
= holding
? "rt" : "r";
5835 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
5841 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5850 /* check number of arguments */
5858 if (holding
&& tag
[0] == '.') {
5859 /* tags starting with '.' are reserved for libzfs */
5860 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
5864 for (i
= 0; i
< argc
; ++i
) {
5866 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
5868 char *path
= argv
[i
];
5870 delim
= strchr(path
, '@');
5871 if (delim
== NULL
) {
5872 (void) fprintf(stderr
,
5873 gettext("'%s' is not a snapshot\n"), path
);
5877 (void) strncpy(parent
, path
, delim
- path
);
5878 parent
[delim
- path
] = '\0';
5880 zhp
= zfs_open(g_zfs
, parent
,
5881 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5887 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
5890 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
5896 return (errors
!= 0);
5900 * zfs hold [-r] [-t] <tag> <snap> ...
5902 * -r Recursively hold
5904 * Apply a user-hold with the given tag to the list of snapshots.
5907 zfs_do_hold(int argc
, char **argv
)
5909 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
5913 * zfs release [-r] <tag> <snap> ...
5915 * -r Recursively release
5917 * Release a user-hold with the given tag from the list of snapshots.
5920 zfs_do_release(int argc
, char **argv
)
5922 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
5925 typedef struct holds_cbdata
{
5926 boolean_t cb_recursive
;
5927 const char *cb_snapname
;
5929 size_t cb_max_namelen
;
5930 size_t cb_max_taglen
;
5933 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5934 #define DATETIME_BUF_LEN (32)
5939 print_holds(boolean_t scripted
, size_t nwidth
, size_t tagwidth
, nvlist_t
*nvl
)
5942 nvpair_t
*nvp
= NULL
;
5943 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
5947 for (i
= 0; i
< 3; i
++) {
5948 col
= gettext(hdr_cols
[i
]);
5950 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
5953 (void) printf("%s\n", col
);
5957 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5958 char *zname
= nvpair_name(nvp
);
5960 nvpair_t
*nvp2
= NULL
;
5961 (void) nvpair_value_nvlist(nvp
, &nvl2
);
5962 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
5963 char tsbuf
[DATETIME_BUF_LEN
];
5964 char *tagname
= nvpair_name(nvp2
);
5969 (void) nvpair_value_uint64(nvp2
, &val
);
5971 (void) localtime_r(&time
, &t
);
5972 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
5973 gettext(STRFTIME_FMT_STR
), &t
);
5976 (void) printf("%s\t%s\t%s\n", zname
,
5979 (void) printf("%-*s %-*s %s\n", nwidth
,
5980 zname
, tagwidth
, tagname
, tsbuf
);
5987 * Generic callback function to list a dataset or snapshot.
5990 holds_callback(zfs_handle_t
*zhp
, void *data
)
5992 holds_cbdata_t
*cbp
= data
;
5993 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
5994 nvlist_t
*nvl
= NULL
;
5995 nvpair_t
*nvp
= NULL
;
5996 const char *zname
= zfs_get_name(zhp
);
5997 size_t znamelen
= strlen(zname
);
5999 if (cbp
->cb_recursive
) {
6000 const char *snapname
;
6001 char *delim
= strchr(zname
, '@');
6005 snapname
= delim
+ 1;
6006 if (strcmp(cbp
->cb_snapname
, snapname
))
6010 if (zfs_get_holds(zhp
, &nvl
) != 0)
6013 if (znamelen
> cbp
->cb_max_namelen
)
6014 cbp
->cb_max_namelen
= znamelen
;
6016 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
6017 const char *tag
= nvpair_name(nvp
);
6018 size_t taglen
= strlen(tag
);
6019 if (taglen
> cbp
->cb_max_taglen
)
6020 cbp
->cb_max_taglen
= taglen
;
6023 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
6027 * zfs holds [-r] <snap> ...
6029 * -r Recursively hold
6032 zfs_do_holds(int argc
, char **argv
)
6037 boolean_t scripted
= B_FALSE
;
6038 boolean_t recursive
= B_FALSE
;
6039 const char *opts
= "rH";
6042 int types
= ZFS_TYPE_SNAPSHOT
;
6043 holds_cbdata_t cb
= { 0 };
6050 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
6059 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6066 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
6067 flags
|= ZFS_ITER_RECURSE
;
6073 /* check number of arguments */
6077 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
6080 for (i
= 0; i
< argc
; ++i
) {
6081 char *snapshot
= argv
[i
];
6083 const char *snapname
;
6085 delim
= strchr(snapshot
, '@');
6086 if (delim
== NULL
) {
6087 (void) fprintf(stderr
,
6088 gettext("'%s' is not a snapshot\n"), snapshot
);
6092 snapname
= delim
+ 1;
6094 snapshot
[delim
- snapshot
] = '\0';
6096 cb
.cb_recursive
= recursive
;
6097 cb
.cb_snapname
= snapname
;
6101 * 1. collect holds data, set format options
6103 ret
= zfs_for_each(1, &argv
[i
], flags
, types
, NULL
, NULL
, limit
,
6104 holds_callback
, &cb
);
6110 * 2. print holds data
6112 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
6116 return (0 != errors
);
6119 #define CHECK_SPINNER 30
6120 #define SPINNER_TIME 3 /* seconds */
6121 #define MOUNT_TIME 1 /* seconds */
6123 typedef struct get_all_state
{
6124 boolean_t ga_verbose
;
6125 get_all_cb_t
*ga_cbp
;
6129 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
6131 static char *spin
[] = { "-", "\\", "|", "/" };
6132 static int spinval
= 0;
6133 static int spincheck
= 0;
6134 static time_t last_spin_time
= (time_t)0;
6135 get_all_state_t
*state
= data
;
6136 zfs_type_t type
= zfs_get_type(zhp
);
6138 if (state
->ga_verbose
) {
6139 if (--spincheck
< 0) {
6140 time_t now
= time(NULL
);
6141 if (last_spin_time
+ SPINNER_TIME
< now
) {
6142 update_progress(spin
[spinval
++ % 4]);
6143 last_spin_time
= now
;
6145 spincheck
= CHECK_SPINNER
;
6150 * Interate over any nested datasets.
6152 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
6158 * Skip any datasets whose type does not match.
6160 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
6164 libzfs_add_handle(state
->ga_cbp
, zhp
);
6165 assert(state
->ga_cbp
->cb_used
<= state
->ga_cbp
->cb_alloc
);
6171 get_all_datasets(get_all_cb_t
*cbp
, boolean_t verbose
)
6173 get_all_state_t state
= {
6174 .ga_verbose
= verbose
,
6179 set_progress_header(gettext("Reading ZFS config"));
6180 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &state
);
6183 finish_progress(gettext("done."));
6187 * Generic callback for sharing or mounting filesystems. Because the code is so
6188 * similar, we have a common function with an extra parameter to determine which
6189 * mode we are using.
6191 typedef enum { OP_SHARE
, OP_MOUNT
} share_mount_op_t
;
6193 typedef struct share_mount_state
{
6194 share_mount_op_t sm_op
;
6195 boolean_t sm_verbose
;
6198 char *sm_proto
; /* only valid for OP_SHARE */
6199 mutex_t sm_lock
; /* protects the remaining fields */
6200 uint_t sm_total
; /* number of filesystems to process */
6201 uint_t sm_done
; /* number of filesystems processed */
6202 int sm_status
; /* -1 if any of the share/mount operations failed */
6203 } share_mount_state_t
;
6206 * Share or mount a dataset.
6209 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
6210 boolean_t
explicit, const char *options
)
6212 char mountpoint
[ZFS_MAXPROPLEN
];
6213 char shareopts
[ZFS_MAXPROPLEN
];
6214 char smbshareopts
[ZFS_MAXPROPLEN
];
6215 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
6217 uint64_t zoned
, canmount
;
6218 boolean_t shared_nfs
, shared_smb
;
6220 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
6223 * Check to make sure we can mount/share this dataset. If we
6224 * are in the global zone and the filesystem is exported to a
6225 * local zone, or if we are in a local zone and the
6226 * filesystem is not exported, then it is an error.
6228 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
6230 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
6234 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6235 "dataset is exported to a local zone\n"), cmdname
,
6239 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
6243 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6244 "permission denied\n"), cmdname
,
6250 * Ignore any filesystems which don't apply to us. This
6251 * includes those with a legacy mountpoint, or those with
6252 * legacy share options.
6254 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
6255 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
6256 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
6257 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
6258 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
6259 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
6261 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
6262 strcmp(smbshareopts
, "off") == 0) {
6266 (void) fprintf(stderr
, gettext("cannot share '%s': "
6267 "legacy share\n"), zfs_get_name(zhp
));
6268 (void) fprintf(stderr
, gettext("use share(8) to "
6269 "share this filesystem, or set "
6270 "sharenfs property on\n"));
6275 * We cannot share or mount legacy filesystems. If the
6276 * shareopts is non-legacy but the mountpoint is legacy, we
6277 * treat it as a legacy share.
6279 if (strcmp(mountpoint
, "legacy") == 0) {
6283 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6284 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
6285 (void) fprintf(stderr
, gettext("use %s(8) to "
6286 "%s this filesystem\n"), cmdname
, cmdname
);
6290 if (strcmp(mountpoint
, "none") == 0) {
6294 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
6295 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
6300 * canmount explicit outcome
6301 * on no pass through
6302 * on yes pass through
6304 * off yes display error, return 1
6305 * noauto no return 0
6306 * noauto yes pass through
6308 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
6309 if (canmount
== ZFS_CANMOUNT_OFF
) {
6313 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6314 "'canmount' property is set to 'off'\n"), cmdname
,
6317 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
6322 * If this filesystem is encrypted and does not have
6323 * a loaded key, we can not mount it.
6325 if ((flags
& MS_CRYPT
) == 0 &&
6326 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) != ZIO_CRYPT_OFF
&&
6327 zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
) ==
6328 ZFS_KEYSTATUS_UNAVAILABLE
) {
6332 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6333 "encryption key not loaded\n"), cmdname
, zfs_get_name(zhp
));
6338 * If this filesystem is inconsistent and has a receive resume
6339 * token, we can not mount it.
6341 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
6342 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
6343 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
6347 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6348 "Contains partially-completed state from "
6349 "\"zfs receive -s\", which can be resumed with "
6350 "\"zfs send -t\"\n"),
6351 cmdname
, zfs_get_name(zhp
));
6356 * At this point, we have verified that the mountpoint and/or
6357 * shareopts are appropriate for auto management. If the
6358 * filesystem is already mounted or shared, return (failing
6359 * for explicit requests); otherwise mount or share the
6365 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
6366 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
6368 if ((shared_nfs
&& shared_smb
) ||
6369 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
6370 strcmp(smbshareopts
, "off") == 0) ||
6371 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
6372 strcmp(shareopts
, "off") == 0)) {
6376 (void) fprintf(stderr
, gettext("cannot share "
6377 "'%s': filesystem already shared\n"),
6382 if (!zfs_is_mounted(zhp
, NULL
) &&
6383 zfs_mount(zhp
, NULL
, flags
) != 0)
6386 if (protocol
== NULL
) {
6387 if (zfs_shareall(zhp
) != 0)
6389 } else if (strcmp(protocol
, "nfs") == 0) {
6390 if (zfs_share_nfs(zhp
))
6392 } else if (strcmp(protocol
, "smb") == 0) {
6393 if (zfs_share_smb(zhp
))
6396 (void) fprintf(stderr
, gettext("cannot share "
6397 "'%s': invalid share type '%s' "
6399 zfs_get_name(zhp
), protocol
);
6406 if (options
== NULL
)
6407 mnt
.mnt_mntopts
= "";
6409 mnt
.mnt_mntopts
= (char *)options
;
6411 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
6412 zfs_is_mounted(zhp
, NULL
)) {
6416 (void) fprintf(stderr
, gettext("cannot mount "
6417 "'%s': filesystem already mounted\n"),
6422 if (zfs_mount(zhp
, options
, flags
) != 0)
6431 * Reports progress in the form "(current/total)". Not thread-safe.
6434 report_mount_progress(int current
, int total
)
6436 static time_t last_progress_time
= 0;
6437 time_t now
= time(NULL
);
6440 /* display header if we're here for the first time */
6442 set_progress_header(gettext("Mounting ZFS filesystems"));
6443 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
6444 /* too soon to report again */
6448 last_progress_time
= now
;
6450 (void) sprintf(info
, "(%d/%d)", current
, total
);
6452 if (current
== total
)
6453 finish_progress(info
);
6455 update_progress(info
);
6459 * zfs_foreach_mountpoint() callback that mounts or shares one filesystem and
6460 * updates the progress meter.
6463 share_mount_one_cb(zfs_handle_t
*zhp
, void *arg
)
6465 share_mount_state_t
*sms
= arg
;
6468 ret
= share_mount_one(zhp
, sms
->sm_op
, sms
->sm_flags
, sms
->sm_proto
,
6469 B_FALSE
, sms
->sm_options
);
6471 mutex_enter(&sms
->sm_lock
);
6473 sms
->sm_status
= ret
;
6475 if (sms
->sm_verbose
)
6476 report_mount_progress(sms
->sm_done
, sms
->sm_total
);
6477 mutex_exit(&sms
->sm_lock
);
6482 append_options(char *mntopts
, char *newopts
)
6484 int len
= strlen(mntopts
);
6486 /* original length plus new string to append plus 1 for the comma */
6487 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
6488 (void) fprintf(stderr
, gettext("the opts argument for "
6489 "'%c' option is too long (more than %d chars)\n"),
6490 "-o", MNT_LINE_MAX
);
6495 mntopts
[len
++] = ',';
6497 (void) strcpy(&mntopts
[len
], newopts
);
6501 share_mount(int op
, int argc
, char **argv
)
6504 boolean_t verbose
= B_FALSE
;
6506 char *options
= NULL
;
6510 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":alvo:O" : "al"))
6523 if (*optarg
== '\0') {
6524 (void) fprintf(stderr
, gettext("empty mount "
6525 "options (-o) specified\n"));
6529 if (options
== NULL
)
6530 options
= safe_malloc(MNT_LINE_MAX
+ 1);
6532 /* option validation is done later */
6533 append_options(options
, optarg
);
6537 flags
|= MS_OVERLAY
;
6540 (void) fprintf(stderr
, gettext("missing argument for "
6541 "'%c' option\n"), optopt
);
6545 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6554 /* check number of arguments */
6556 char *protocol
= NULL
;
6558 if (op
== OP_SHARE
&& argc
> 0) {
6559 if (strcmp(argv
[0], "nfs") != 0 &&
6560 strcmp(argv
[0], "smb") != 0) {
6561 (void) fprintf(stderr
, gettext("share type "
6562 "must be 'nfs' or 'smb'\n"));
6571 (void) fprintf(stderr
, gettext("too many arguments\n"));
6575 start_progress_timer();
6576 get_all_cb_t cb
= { 0 };
6577 get_all_datasets(&cb
, verbose
);
6579 if (cb
.cb_used
== 0)
6582 if (op
== OP_SHARE
) {
6583 sa_init_selective_arg_t sharearg
;
6584 sharearg
.zhandle_arr
= cb
.cb_handles
;
6585 sharearg
.zhandle_len
= cb
.cb_used
;
6586 if ((ret
= zfs_init_libshare_arg(g_zfs
,
6587 SA_INIT_SHARE_API_SELECTIVE
, &sharearg
)) != SA_OK
) {
6588 (void) fprintf(stderr
, gettext(
6589 "Could not initialize libshare, %d"), ret
);
6594 share_mount_state_t share_mount_state
= { 0 };
6595 share_mount_state
.sm_op
= op
;
6596 share_mount_state
.sm_verbose
= verbose
;
6597 share_mount_state
.sm_flags
= flags
;
6598 share_mount_state
.sm_options
= options
;
6599 share_mount_state
.sm_proto
= protocol
;
6600 share_mount_state
.sm_total
= cb
.cb_used
;
6601 (void) mutex_init(&share_mount_state
.sm_lock
,
6602 LOCK_NORMAL
| LOCK_ERRORCHECK
, NULL
);
6604 * libshare isn't mt-safe, so only do the operation in parallel
6605 * if we're mounting.
6607 zfs_foreach_mountpoint(g_zfs
, cb
.cb_handles
, cb
.cb_used
,
6608 share_mount_one_cb
, &share_mount_state
, op
== OP_MOUNT
);
6609 ret
= share_mount_state
.sm_status
;
6611 for (int i
= 0; i
< cb
.cb_used
; i
++)
6612 zfs_close(cb
.cb_handles
[i
]);
6613 free(cb
.cb_handles
);
6614 } else if (argc
== 0) {
6615 struct mnttab entry
;
6617 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
6618 (void) fprintf(stderr
, gettext("missing filesystem "
6619 "argument (specify -a for all)\n"));
6624 * When mount is given no arguments, go through /etc/mnttab and
6625 * display any active ZFS mounts. We hide any snapshots, since
6626 * they are controlled automatically.
6628 rewind(mnttab_file
);
6629 while (getmntent(mnttab_file
, &entry
) == 0) {
6630 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
6631 strchr(entry
.mnt_special
, '@') != NULL
)
6634 (void) printf("%-30s %s\n", entry
.mnt_special
,
6642 (void) fprintf(stderr
,
6643 gettext("too many arguments\n"));
6647 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6648 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6651 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
6661 * zfs mount -a [nfs]
6662 * zfs mount filesystem
6664 * Mount all filesystems, or mount the given filesystem.
6667 zfs_do_mount(int argc
, char **argv
)
6669 return (share_mount(OP_MOUNT
, argc
, argv
));
6673 * zfs share -a [nfs | smb]
6674 * zfs share filesystem
6676 * Share all filesystems, or share the given filesystem.
6679 zfs_do_share(int argc
, char **argv
)
6681 return (share_mount(OP_SHARE
, argc
, argv
));
6684 typedef struct unshare_unmount_node
{
6685 zfs_handle_t
*un_zhp
;
6687 uu_avl_node_t un_avlnode
;
6688 } unshare_unmount_node_t
;
6692 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
6694 const unshare_unmount_node_t
*l
= larg
;
6695 const unshare_unmount_node_t
*r
= rarg
;
6697 return (strcmp(l
->un_mountp
, r
->un_mountp
));
6701 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6702 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6703 * and unmount it appropriately.
6706 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
6710 struct stat64 statbuf
;
6711 struct extmnttab entry
;
6712 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
6716 * Search for the path in /etc/mnttab. Rather than looking for the
6717 * specific path, which can be fooled by non-standard paths (i.e. ".."
6718 * or "//"), we stat() the path and search for the corresponding
6719 * (major,minor) device pair.
6721 if (stat64(path
, &statbuf
) != 0) {
6722 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6723 cmdname
, path
, strerror(errno
));
6726 path_inode
= statbuf
.st_ino
;
6729 * Search for the given (major,minor) pair in the mount table.
6731 rewind(mnttab_file
);
6732 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
6733 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
6734 entry
.mnt_minor
== minor(statbuf
.st_dev
))
6738 if (op
== OP_SHARE
) {
6739 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
6740 "currently mounted\n"), cmdname
, path
);
6743 (void) fprintf(stderr
, gettext("warning: %s not in mnttab\n"),
6745 if ((ret
= umount2(path
, flags
)) != 0)
6746 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
6751 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
6752 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
6753 "filesystem\n"), cmdname
, path
);
6757 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6758 ZFS_TYPE_FILESYSTEM
)) == NULL
)
6762 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
6763 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
6764 cmdname
, path
, strerror(errno
));
6766 } else if (statbuf
.st_ino
!= path_inode
) {
6767 (void) fprintf(stderr
, gettext("cannot "
6768 "%s '%s': not a mountpoint\n"), cmdname
, path
);
6772 if (op
== OP_SHARE
) {
6773 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6774 char smbshare_prop
[ZFS_MAXPROPLEN
];
6776 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
6777 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6778 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
6779 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6781 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
6782 strcmp(smbshare_prop
, "off") == 0) {
6783 (void) fprintf(stderr
, gettext("cannot unshare "
6784 "'%s': legacy share\n"), path
);
6785 (void) fprintf(stderr
, gettext("use "
6786 "unshare(8) to unshare this filesystem\n"));
6787 } else if (!zfs_is_shared(zhp
)) {
6788 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
6789 "not currently shared\n"), path
);
6791 ret
= zfs_unshareall_bypath(zhp
, path
);
6794 char mtpt_prop
[ZFS_MAXPROPLEN
];
6796 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
6797 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
6800 ret
= zfs_unmount(zhp
, NULL
, flags
);
6801 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
6802 (void) fprintf(stderr
, gettext("cannot unmount "
6803 "'%s': legacy mountpoint\n"),
6805 (void) fprintf(stderr
, gettext("use umount(8) "
6806 "to unmount this filesystem\n"));
6808 ret
= zfs_unmountall(zhp
, flags
);
6819 * Generic callback for unsharing or unmounting a filesystem.
6822 unshare_unmount(int op
, int argc
, char **argv
)
6829 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
6830 char sharesmb
[ZFS_MAXPROPLEN
];
6833 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
6842 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6853 * We could make use of zfs_for_each() to walk all datasets in
6854 * the system, but this would be very inefficient, especially
6855 * since we would have to linearly search /etc/mnttab for each
6856 * one. Instead, do one pass through /etc/mnttab looking for
6857 * zfs entries and call zfs_unmount() for each one.
6859 * Things get a little tricky if the administrator has created
6860 * mountpoints beneath other ZFS filesystems. In this case, we
6861 * have to unmount the deepest filesystems first. To accomplish
6862 * this, we place all the mountpoints in an AVL tree sorted by
6863 * the special type (dataset name), and walk the result in
6864 * reverse to make sure to get any snapshots first.
6866 struct mnttab entry
;
6867 uu_avl_pool_t
*pool
;
6868 uu_avl_t
*tree
= NULL
;
6869 unshare_unmount_node_t
*node
;
6871 uu_avl_walk_t
*walk
;
6874 (void) fprintf(stderr
, gettext("too many arguments\n"));
6878 if (((pool
= uu_avl_pool_create("unmount_pool",
6879 sizeof (unshare_unmount_node_t
),
6880 offsetof(unshare_unmount_node_t
, un_avlnode
),
6881 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
6882 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
6885 rewind(mnttab_file
);
6886 while (getmntent(mnttab_file
, &entry
) == 0) {
6888 /* ignore non-ZFS entries */
6889 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
6892 /* ignore snapshots */
6893 if (strchr(entry
.mnt_special
, '@') != NULL
)
6896 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
6897 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6903 * Ignore datasets that are excluded/restricted by
6906 if (zpool_skip_pool(zfs_get_pool_name(zhp
))) {
6913 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
6915 sizeof (nfs_mnt_prop
),
6916 NULL
, NULL
, 0, B_FALSE
) == 0);
6917 if (strcmp(nfs_mnt_prop
, "off") != 0)
6919 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
6921 sizeof (nfs_mnt_prop
),
6922 NULL
, NULL
, 0, B_FALSE
) == 0);
6923 if (strcmp(nfs_mnt_prop
, "off") == 0)
6927 /* Ignore legacy mounts */
6928 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
6930 sizeof (nfs_mnt_prop
),
6931 NULL
, NULL
, 0, B_FALSE
) == 0);
6932 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
6934 /* Ignore canmount=noauto mounts */
6935 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
6936 ZFS_CANMOUNT_NOAUTO
)
6942 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
6944 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
6946 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
6948 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
6949 uu_avl_insert(tree
, node
, idx
);
6951 zfs_close(node
->un_zhp
);
6952 free(node
->un_mountp
);
6958 * Initialize libshare SA_INIT_SHARE_API_SELECTIVE here
6959 * to avoid unnecessary load/unload of the libshare API
6960 * per shared dataset downstream.
6962 if (op
== OP_SHARE
) {
6963 get_all_cb_t dslist
= { 0 };
6964 get_all_datasets(&dslist
, B_FALSE
);
6966 if (dslist
.cb_used
!= 0) {
6967 sa_init_selective_arg_t sharearg
;
6968 sharearg
.zhandle_arr
= dslist
.cb_handles
;
6969 sharearg
.zhandle_len
= dslist
.cb_used
;
6970 if ((ret
= zfs_init_libshare_arg(g_zfs
,
6971 SA_INIT_SHARE_API_SELECTIVE
, &sharearg
)) !=
6973 (void) fprintf(stderr
, gettext(
6974 "Could not initialize libshare, "
6982 * Walk the AVL tree in reverse, unmounting each filesystem and
6983 * removing it from the AVL tree in the process.
6985 if ((walk
= uu_avl_walk_start(tree
,
6986 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
6989 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
6990 uu_avl_remove(tree
, node
);
6994 if (zfs_unshareall_bypath(node
->un_zhp
,
6995 node
->un_mountp
) != 0)
7000 if (zfs_unmount(node
->un_zhp
,
7001 node
->un_mountp
, flags
) != 0)
7006 zfs_close(node
->un_zhp
);
7007 free(node
->un_mountp
);
7011 uu_avl_walk_end(walk
);
7012 uu_avl_destroy(tree
);
7013 uu_avl_pool_destroy(pool
);
7018 (void) fprintf(stderr
,
7019 gettext("missing filesystem argument\n"));
7021 (void) fprintf(stderr
,
7022 gettext("too many arguments\n"));
7027 * We have an argument, but it may be a full path or a ZFS
7028 * filesystem. Pass full paths off to unmount_path() (shared by
7029 * manual_unmount), otherwise open the filesystem and pass to
7032 if (argv
[0][0] == '/')
7033 return (unshare_unmount_path(op
, argv
[0],
7036 if ((zhp
= zfs_open(g_zfs
, argv
[0],
7037 ZFS_TYPE_FILESYSTEM
)) == NULL
)
7040 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
7041 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
7042 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
7043 NULL
, 0, B_FALSE
) == 0);
7047 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
7049 sizeof (nfs_mnt_prop
),
7050 NULL
, NULL
, 0, B_FALSE
) == 0);
7051 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
7052 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
7055 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
7056 strcmp(sharesmb
, "off") == 0) {
7057 (void) fprintf(stderr
, gettext("cannot "
7058 "unshare '%s': legacy share\n"),
7060 (void) fprintf(stderr
, gettext("use "
7061 "unshare(8) to unshare this "
7064 } else if (!zfs_is_shared(zhp
)) {
7065 (void) fprintf(stderr
, gettext("cannot "
7066 "unshare '%s': not currently "
7067 "shared\n"), zfs_get_name(zhp
));
7069 } else if (zfs_unshareall(zhp
) != 0) {
7075 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
7076 (void) fprintf(stderr
, gettext("cannot "
7077 "unmount '%s': legacy "
7078 "mountpoint\n"), zfs_get_name(zhp
));
7079 (void) fprintf(stderr
, gettext("use "
7080 "umount(8) to unmount this "
7083 } else if (!zfs_is_mounted(zhp
, NULL
)) {
7084 (void) fprintf(stderr
, gettext("cannot "
7085 "unmount '%s': not currently "
7089 } else if (zfs_unmountall(zhp
, flags
) != 0) {
7103 * zfs unmount filesystem
7105 * Unmount all filesystems, or a specific ZFS filesystem.
7108 zfs_do_unmount(int argc
, char **argv
)
7110 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
7115 * zfs unshare filesystem
7117 * Unshare all filesystems, or a specific ZFS filesystem.
7120 zfs_do_unshare(int argc
, char **argv
)
7122 return (unshare_unmount(OP_SHARE
, argc
, argv
));
7126 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
7127 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
7130 manual_mount(int argc
, char **argv
)
7133 char mountpoint
[ZFS_MAXPROPLEN
];
7134 char mntopts
[MNT_LINE_MAX
] = { '\0' };
7138 char *dataset
, *path
;
7141 while ((c
= getopt(argc
, argv
, ":mo:O")) != -1) {
7144 (void) strlcpy(mntopts
, optarg
, sizeof (mntopts
));
7147 flags
|= MS_OVERLAY
;
7150 flags
|= MS_NOMNTTAB
;
7153 (void) fprintf(stderr
, gettext("missing argument for "
7154 "'%c' option\n"), optopt
);
7158 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7160 (void) fprintf(stderr
, gettext("usage: mount [-o opts] "
7169 /* check that we only have two arguments */
7172 (void) fprintf(stderr
, gettext("missing dataset "
7175 (void) fprintf(stderr
,
7176 gettext("missing mountpoint argument\n"));
7178 (void) fprintf(stderr
, gettext("too many arguments\n"));
7179 (void) fprintf(stderr
, "usage: mount <dataset> <mountpoint>\n");
7186 /* try to open the dataset */
7187 if ((zhp
= zfs_open(g_zfs
, dataset
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
7190 (void) zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
7191 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
);
7193 /* check for legacy mountpoint and complain appropriately */
7195 if (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) == 0) {
7196 if (mount(dataset
, path
, MS_OPTIONSTR
| flags
, MNTTYPE_ZFS
,
7197 NULL
, 0, mntopts
, sizeof (mntopts
)) != 0) {
7198 (void) fprintf(stderr
, gettext("mount failed: %s\n"),
7203 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
7204 "mounted using 'mount -F zfs'\n"), dataset
);
7205 (void) fprintf(stderr
, gettext("Use 'zfs set mountpoint=%s' "
7206 "instead.\n"), path
);
7207 (void) fprintf(stderr
, gettext("If you must use 'mount -F zfs' "
7208 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
7209 (void) fprintf(stderr
, gettext("See zfs(8) for more "
7218 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
7219 * unmounts of non-legacy filesystems, as this is the dominant administrative
7223 manual_unmount(int argc
, char **argv
)
7229 while ((c
= getopt(argc
, argv
, "f")) != -1) {
7235 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7237 (void) fprintf(stderr
, gettext("usage: unmount [-f] "
7246 /* check arguments */
7249 (void) fprintf(stderr
, gettext("missing path "
7252 (void) fprintf(stderr
, gettext("too many arguments\n"));
7253 (void) fprintf(stderr
, gettext("usage: unmount [-f] <path>\n"));
7257 return (unshare_unmount_path(OP_MOUNT
, argv
[0], flags
, B_TRUE
));
7261 find_command_idx(char *command
, int *idx
)
7265 for (i
= 0; i
< NCOMMAND
; i
++) {
7266 if (command_table
[i
].name
== NULL
)
7269 if (strcmp(command
, command_table
[i
].name
) == 0) {
7278 zfs_do_diff(int argc
, char **argv
)
7282 char *tosnap
= NULL
;
7283 char *fromsnap
= NULL
;
7288 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
7291 flags
|= ZFS_DIFF_CLASSIFY
;
7294 flags
|= ZFS_DIFF_PARSEABLE
;
7297 flags
|= ZFS_DIFF_TIMESTAMP
;
7300 (void) fprintf(stderr
,
7301 gettext("invalid option '%c'\n"), optopt
);
7310 (void) fprintf(stderr
,
7311 gettext("must provide at least one snapshot name\n"));
7316 (void) fprintf(stderr
, gettext("too many arguments\n"));
7321 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
7324 if (*fromsnap
!= '@')
7325 copy
= strdup(fromsnap
);
7327 copy
= strdup(tosnap
);
7331 if ((atp
= strchr(copy
, '@')) != NULL
)
7334 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
) {
7341 * Ignore SIGPIPE so that the library can give us
7342 * information on any failure
7344 (void) sigignore(SIGPIPE
);
7346 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
7354 * zfs remap <filesystem | volume>
7356 * Remap the indirect blocks in the given fileystem or volume.
7359 zfs_do_remap(int argc
, char **argv
)
7366 while ((c
= getopt(argc
, argv
, "")) != -1) {
7369 (void) fprintf(stderr
,
7370 gettext("invalid option '%c'\n"), optopt
);
7376 (void) fprintf(stderr
, gettext("wrong number of arguments\n"));
7381 err
= zfs_remap_indirects(g_zfs
, fsname
);
7387 * zfs bookmark <fs@snap> <fs#bmark>
7389 * Creates a bookmark with the given name from the given snapshot.
7392 zfs_do_bookmark(int argc
, char **argv
)
7394 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
7401 while ((c
= getopt(argc
, argv
, "")) != -1) {
7404 (void) fprintf(stderr
,
7405 gettext("invalid option '%c'\n"), optopt
);
7413 /* check number of arguments */
7415 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
7419 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
7423 if (strchr(argv
[1], '#') == NULL
) {
7424 (void) fprintf(stderr
,
7425 gettext("invalid bookmark name '%s' -- "
7426 "must contain a '#'\n"), argv
[1]);
7430 if (argv
[0][0] == '@') {
7432 * Snapshot name begins with @.
7433 * Default to same fs as bookmark.
7435 (void) strncpy(snapname
, argv
[1], sizeof (snapname
));
7436 *strchr(snapname
, '#') = '\0';
7437 (void) strlcat(snapname
, argv
[0], sizeof (snapname
));
7439 (void) strlcpy(snapname
, argv
[0], sizeof (snapname
));
7441 zhp
= zfs_open(g_zfs
, snapname
, ZFS_TYPE_SNAPSHOT
);
7447 nvl
= fnvlist_alloc();
7448 fnvlist_add_string(nvl
, argv
[1], snapname
);
7449 ret
= lzc_bookmark(nvl
, NULL
);
7453 const char *err_msg
= NULL
;
7456 (void) snprintf(errbuf
, sizeof (errbuf
),
7457 dgettext(TEXT_DOMAIN
,
7458 "cannot create bookmark '%s'"), argv
[1]);
7462 err_msg
= "bookmark is in a different pool";
7465 err_msg
= "bookmark exists";
7468 err_msg
= "invalid argument";
7471 err_msg
= "bookmark feature not enabled";
7474 err_msg
= "out of space";
7477 (void) zfs_standard_error(g_zfs
, ret
, errbuf
);
7480 if (err_msg
!= NULL
) {
7481 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
7482 dgettext(TEXT_DOMAIN
, err_msg
));
7494 zfs_do_channel_program(int argc
, char **argv
)
7498 char *progbuf
, *filename
, *poolname
;
7499 size_t progsize
, progread
;
7500 nvlist_t
*outnvl
= NULL
;
7501 uint64_t instrlimit
= ZCP_DEFAULT_INSTRLIMIT
;
7502 uint64_t memlimit
= ZCP_DEFAULT_MEMLIMIT
;
7503 boolean_t sync_flag
= B_TRUE
, json_output
= B_FALSE
;
7504 zpool_handle_t
*zhp
;
7508 (c
= getopt(argc
, argv
, "jnt:(instr-limit)m:(memory-limit)"))) {
7516 arg
= strtoull(optarg
, &endp
, 0);
7517 if (errno
!= 0 || *endp
!= '\0') {
7518 (void) fprintf(stderr
, gettext(
7520 "'%s': expected integer\n"), optarg
);
7525 if (arg
> ZCP_MAX_INSTRLIMIT
|| arg
== 0) {
7526 (void) fprintf(stderr
, gettext(
7527 "Invalid instruction limit: "
7534 ASSERT3U(c
, ==, 'm');
7535 if (arg
> ZCP_MAX_MEMLIMIT
|| arg
== 0) {
7536 (void) fprintf(stderr
, gettext(
7537 "Invalid memory limit: "
7547 sync_flag
= B_FALSE
;
7551 json_output
= B_TRUE
;
7555 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7565 (void) fprintf(stderr
,
7566 gettext("invalid number of arguments\n"));
7572 if (strcmp(filename
, "-") == 0) {
7574 filename
= "standard input";
7575 } else if ((fd
= open(filename
, O_RDONLY
)) < 0) {
7576 (void) fprintf(stderr
, gettext("cannot open '%s': %s\n"),
7577 filename
, strerror(errno
));
7581 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
7582 (void) fprintf(stderr
, gettext("cannot open pool '%s'"),
7589 * Read in the channel program, expanding the program buffer as
7594 progbuf
= safe_malloc(progsize
);
7596 ret
= read(fd
, progbuf
+ progread
, progsize
- progread
);
7598 if (progread
== progsize
&& ret
> 0) {
7600 progbuf
= safe_realloc(progbuf
, progsize
);
7608 (void) fprintf(stderr
,
7609 gettext("cannot read '%s': %s\n"),
7610 filename
, strerror(errno
));
7613 progbuf
[progread
] = '\0';
7616 * Any remaining arguments are passed as arguments to the lua script as
7619 * "argv" -> [ "arg 1", ... "arg n" ],
7622 nvlist_t
*argnvl
= fnvlist_alloc();
7623 fnvlist_add_string_array(argnvl
, ZCP_ARG_CLIARGV
, argv
+ 2, argc
- 2);
7626 ret
= lzc_channel_program(poolname
, progbuf
,
7627 instrlimit
, memlimit
, argnvl
, &outnvl
);
7629 ret
= lzc_channel_program_nosync(poolname
, progbuf
,
7630 instrlimit
, memlimit
, argnvl
, &outnvl
);
7635 * On error, report the error message handed back by lua if one
7636 * exists. Otherwise, generate an appropriate error message,
7637 * falling back on strerror() for an unexpected return code.
7639 char *errstring
= NULL
;
7640 const char *msg
= gettext("Channel program execution failed");
7641 if (outnvl
!= NULL
&& nvlist_exists(outnvl
, ZCP_RET_ERROR
)) {
7642 (void) nvlist_lookup_string(outnvl
,
7643 ZCP_RET_ERROR
, &errstring
);
7644 if (errstring
== NULL
)
7645 errstring
= strerror(ret
);
7650 "Invalid instruction or memory limit.";
7653 errstring
= "Return value too large.";
7656 errstring
= "Memory limit exhausted.";
7659 errstring
= "Timed out.";
7662 errstring
= "Permission denied. Channel "
7663 "programs must be run as root.";
7666 (void) zfs_standard_error(g_zfs
, ret
, msg
);
7669 if (errstring
!= NULL
)
7670 (void) fprintf(stderr
, "%s:\n%s\n", msg
, errstring
);
7673 (void) nvlist_print_json(stdout
, outnvl
);
7674 } else if (nvlist_empty(outnvl
)) {
7675 (void) fprintf(stdout
, gettext("Channel program fully "
7676 "executed and did not produce output.\n"));
7678 (void) fprintf(stdout
, gettext("Channel program fully "
7679 "executed and produced output:\n"));
7680 dump_nvlist(outnvl
, 4);
7685 fnvlist_free(outnvl
);
7686 fnvlist_free(argnvl
);
7694 typedef struct loadkey_cbdata
{
7695 boolean_t cb_loadkey
;
7696 boolean_t cb_recursive
;
7698 char *cb_keylocation
;
7699 uint64_t cb_numfailed
;
7700 uint64_t cb_numattempted
;
7704 load_key_callback(zfs_handle_t
*zhp
, void *data
)
7707 boolean_t is_encroot
;
7708 loadkey_cbdata_t
*cb
= data
;
7709 uint64_t keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
7712 * If we are working recursively, we want to skip loading / unloading
7713 * keys for non-encryption roots and datasets whose keys are already
7714 * in the desired end-state.
7716 if (cb
->cb_recursive
) {
7717 ret
= zfs_crypto_get_encryption_root(zhp
, &is_encroot
, NULL
);
7723 if ((cb
->cb_loadkey
&& keystatus
== ZFS_KEYSTATUS_AVAILABLE
) ||
7724 (!cb
->cb_loadkey
&& keystatus
== ZFS_KEYSTATUS_UNAVAILABLE
))
7728 cb
->cb_numattempted
++;
7731 ret
= zfs_crypto_load_key(zhp
, cb
->cb_noop
, cb
->cb_keylocation
);
7733 ret
= zfs_crypto_unload_key(zhp
);
7744 load_unload_keys(int argc
, char **argv
, boolean_t loadkey
)
7746 int c
, ret
= 0, flags
= 0;
7747 boolean_t do_all
= B_FALSE
;
7748 loadkey_cbdata_t cb
= { 0 };
7750 cb
.cb_loadkey
= loadkey
;
7752 while ((c
= getopt(argc
, argv
, "anrL:")) != -1) {
7753 /* noop and alternate keylocations only apply to zfs load-key */
7757 cb
.cb_noop
= B_TRUE
;
7760 cb
.cb_keylocation
= optarg
;
7770 cb
.cb_recursive
= B_TRUE
;
7773 flags
|= ZFS_ITER_RECURSE
;
7774 cb
.cb_recursive
= B_TRUE
;
7777 (void) fprintf(stderr
,
7778 gettext("invalid option '%c'\n"), optopt
);
7786 if (!do_all
&& argc
== 0) {
7787 (void) fprintf(stderr
,
7788 gettext("Missing dataset argument or -a option\n"));
7792 if (do_all
&& argc
!= 0) {
7793 (void) fprintf(stderr
,
7794 gettext("Cannot specify dataset with -a option\n"));
7798 if (cb
.cb_recursive
&& cb
.cb_keylocation
!= NULL
&&
7799 strcmp(cb
.cb_keylocation
, "prompt") != 0) {
7800 (void) fprintf(stderr
, gettext("alternate keylocation may only "
7801 "be 'prompt' with -r or -a\n"));
7805 ret
= zfs_for_each(argc
, argv
, flags
,
7806 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
, NULL
, NULL
, 0,
7807 load_key_callback
, &cb
);
7809 if (cb
.cb_noop
|| (cb
.cb_recursive
&& cb
.cb_numattempted
!= 0)) {
7810 (void) printf(gettext("%llu / %llu key(s) successfully %s\n"),
7811 (u_longlong_t
)(cb
.cb_numattempted
- cb
.cb_numfailed
),
7812 (u_longlong_t
)cb
.cb_numattempted
,
7813 loadkey
? (cb
.cb_noop
? "verified" : "loaded") :
7817 if (cb
.cb_numfailed
!= 0)
7824 zfs_do_load_key(int argc
, char **argv
)
7826 return (load_unload_keys(argc
, argv
, B_TRUE
));
7831 zfs_do_unload_key(int argc
, char **argv
)
7833 return (load_unload_keys(argc
, argv
, B_FALSE
));
7837 zfs_do_change_key(int argc
, char **argv
)
7841 boolean_t loadkey
= B_FALSE
, inheritkey
= B_FALSE
;
7842 zfs_handle_t
*zhp
= NULL
;
7843 nvlist_t
*props
= fnvlist_alloc();
7845 while ((c
= getopt(argc
, argv
, "lio:")) != -1) {
7851 inheritkey
= B_TRUE
;
7854 if (!parseprop(props
, optarg
)) {
7860 (void) fprintf(stderr
,
7861 gettext("invalid option '%c'\n"), optopt
);
7866 if (inheritkey
&& !nvlist_empty(props
)) {
7867 (void) fprintf(stderr
,
7868 gettext("Properties not allowed for inheriting\n"));
7876 (void) fprintf(stderr
, gettext("Missing dataset argument\n"));
7881 (void) fprintf(stderr
, gettext("Too many arguments\n"));
7885 zhp
= zfs_open(g_zfs
, argv
[argc
- 1],
7886 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
7891 keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
7892 if (keystatus
!= ZFS_KEYSTATUS_AVAILABLE
) {
7893 ret
= zfs_crypto_load_key(zhp
, B_FALSE
, NULL
);
7901 /* refresh the properties so the new keystatus is visible */
7902 zfs_refresh_properties(zhp
);
7905 ret
= zfs_crypto_rewrap(zhp
, props
, inheritkey
);
7918 * 1) zfs project [-d|-r] <file|directory ...>
7919 * List project ID and inherit flag of file(s) or directories.
7920 * -d: List the directory itself, not its children.
7921 * -r: List subdirectories recursively.
7923 * 2) zfs project -C [-k] [-r] <file|directory ...>
7924 * Clear project inherit flag and/or ID on the file(s) or directories.
7925 * -k: Keep the project ID unchanged. If not specified, the project ID
7926 * will be reset as zero.
7927 * -r: Clear on subdirectories recursively.
7929 * 3) zfs project -c [-0] [-d|-r] [-p id] <file|directory ...>
7930 * Check project ID and inherit flag on the file(s) or directories,
7931 * report the outliers.
7932 * -0: Print file name followed by a NUL instead of newline.
7933 * -d: Check the directory itself, not its children.
7934 * -p: Specify the referenced ID for comparing with the target file(s)
7935 * or directories' project IDs. If not specified, the target (top)
7936 * directory's project ID will be used as the referenced one.
7937 * -r: Check subdirectories recursively.
7939 * 4) zfs project [-p id] [-r] [-s] <file|directory ...>
7940 * Set project ID and/or inherit flag on the file(s) or directories.
7941 * -p: Set the project ID as the given id.
7942 * -r: Set on subdirectory recursively. If not specify "-p" option,
7943 * it will use top-level directory's project ID as the given id,
7944 * then set both project ID and inherit flag on all descendants
7945 * of the top-level directory.
7946 * -s: Set project inherit flag.
7949 zfs_do_project(int argc
, char **argv
)
7951 zfs_project_control_t zpc
= {
7952 .zpc_expected_projid
= ZFS_INVALID_PROJID
,
7953 .zpc_op
= ZFS_PROJECT_OP_DEFAULT
,
7954 .zpc_dironly
= B_FALSE
,
7955 .zpc_keep_projid
= B_FALSE
,
7956 .zpc_newline
= B_TRUE
,
7957 .zpc_recursive
= B_FALSE
,
7958 .zpc_set_flag
= B_FALSE
,
7965 while ((c
= getopt(argc
, argv
, "0Ccdkp:rs")) != -1) {
7968 zpc
.zpc_newline
= B_FALSE
;
7971 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
7972 (void) fprintf(stderr
, gettext("cannot "
7973 "specify '-C' '-c' '-s' together\n"));
7977 zpc
.zpc_op
= ZFS_PROJECT_OP_CLEAR
;
7980 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
7981 (void) fprintf(stderr
, gettext("cannot "
7982 "specify '-C' '-c' '-s' together\n"));
7986 zpc
.zpc_op
= ZFS_PROJECT_OP_CHECK
;
7989 zpc
.zpc_dironly
= B_TRUE
;
7990 /* overwrite "-r" option */
7991 zpc
.zpc_recursive
= B_FALSE
;
7994 zpc
.zpc_keep_projid
= B_TRUE
;
8000 zpc
.zpc_expected_projid
= strtoull(optarg
, &endptr
, 0);
8001 if (errno
!= 0 || *endptr
!= '\0') {
8002 (void) fprintf(stderr
,
8003 gettext("project ID must be less than "
8004 "%u\n"), UINT32_MAX
);
8007 if (zpc
.zpc_expected_projid
>= UINT32_MAX
) {
8008 (void) fprintf(stderr
,
8009 gettext("invalid project ID\n"));
8015 zpc
.zpc_recursive
= B_TRUE
;
8016 /* overwrite "-d" option */
8017 zpc
.zpc_dironly
= B_FALSE
;
8020 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
8021 (void) fprintf(stderr
, gettext("cannot "
8022 "specify '-C' '-c' '-s' together\n"));
8026 zpc
.zpc_set_flag
= B_TRUE
;
8027 zpc
.zpc_op
= ZFS_PROJECT_OP_SET
;
8030 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8036 if (zpc
.zpc_op
== ZFS_PROJECT_OP_DEFAULT
) {
8037 if (zpc
.zpc_expected_projid
!= ZFS_INVALID_PROJID
)
8038 zpc
.zpc_op
= ZFS_PROJECT_OP_SET
;
8040 zpc
.zpc_op
= ZFS_PROJECT_OP_LIST
;
8043 switch (zpc
.zpc_op
) {
8044 case ZFS_PROJECT_OP_LIST
:
8045 if (zpc
.zpc_keep_projid
) {
8046 (void) fprintf(stderr
,
8047 gettext("'-k' is only valid together with '-C'\n"));
8050 if (!zpc
.zpc_newline
) {
8051 (void) fprintf(stderr
,
8052 gettext("'-0' is only valid together with '-c'\n"));
8056 case ZFS_PROJECT_OP_CHECK
:
8057 if (zpc
.zpc_keep_projid
) {
8058 (void) fprintf(stderr
,
8059 gettext("'-k' is only valid together with '-C'\n"));
8063 case ZFS_PROJECT_OP_CLEAR
:
8064 if (zpc
.zpc_dironly
) {
8065 (void) fprintf(stderr
,
8066 gettext("'-d' is useless together with '-C'\n"));
8069 if (!zpc
.zpc_newline
) {
8070 (void) fprintf(stderr
,
8071 gettext("'-0' is only valid together with '-c'\n"));
8074 if (zpc
.zpc_expected_projid
!= ZFS_INVALID_PROJID
) {
8075 (void) fprintf(stderr
,
8076 gettext("'-p' is useless together with '-C'\n"));
8080 case ZFS_PROJECT_OP_SET
:
8081 if (zpc
.zpc_dironly
) {
8082 (void) fprintf(stderr
,
8083 gettext("'-d' is useless for set project ID and/or "
8087 if (zpc
.zpc_keep_projid
) {
8088 (void) fprintf(stderr
,
8089 gettext("'-k' is only valid together with '-C'\n"));
8092 if (!zpc
.zpc_newline
) {
8093 (void) fprintf(stderr
,
8094 gettext("'-0' is only valid together with '-c'\n"));
8106 (void) fprintf(stderr
,
8107 gettext("missing file or directory target(s)\n"));
8111 for (int i
= 0; i
< argc
; i
++) {
8114 err
= zfs_project_handle(argv
[i
], &zpc
);
8123 main(int argc
, char **argv
)
8130 (void) setlocale(LC_ALL
, "");
8131 (void) textdomain(TEXT_DOMAIN
);
8135 if ((g_zfs
= libzfs_init()) == NULL
) {
8136 (void) fprintf(stderr
, gettext("internal error: failed to "
8137 "initialize ZFS library\n"));
8141 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
8143 libzfs_print_on_error(g_zfs
, B_TRUE
);
8145 if ((mnttab_file
= fopen(MNTTAB
, "r")) == NULL
) {
8146 (void) fprintf(stderr
, gettext("internal error: unable to "
8147 "open %s\n"), MNTTAB
);
8152 * This command also doubles as the /etc/fs mount and unmount program.
8153 * Determine if we should take this behavior based on argv[0].
8155 progname
= basename(argv
[0]);
8156 if (strcmp(progname
, "mount") == 0) {
8157 ret
= manual_mount(argc
, argv
);
8158 } else if (strcmp(progname
, "umount") == 0) {
8159 ret
= manual_unmount(argc
, argv
);
8162 * Make sure the user has specified some command.
8165 (void) fprintf(stderr
, gettext("missing command\n"));
8172 * The 'umount' command is an alias for 'unmount'
8174 if (strcmp(cmdname
, "umount") == 0)
8175 cmdname
= "unmount";
8178 * The 'recv' command is an alias for 'receive'
8180 if (strcmp(cmdname
, "recv") == 0)
8181 cmdname
= "receive";
8184 * The 'snap' command is an alias for 'snapshot'
8186 if (strcmp(cmdname
, "snap") == 0)
8187 cmdname
= "snapshot";
8192 if (strcmp(cmdname
, "-?") == 0)
8196 * Run the appropriate command.
8198 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
8199 if (find_command_idx(cmdname
, &i
) == 0) {
8200 current_command
= &command_table
[i
];
8201 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
8202 } else if (strchr(cmdname
, '=') != NULL
) {
8203 verify(find_command_idx("set", &i
) == 0);
8204 current_command
= &command_table
[i
];
8205 ret
= command_table
[i
].func(argc
, argv
);
8207 (void) fprintf(stderr
, gettext("unrecognized "
8208 "command '%s'\n"), cmdname
);
8211 libzfs_mnttab_cache(g_zfs
, B_FALSE
);
8214 (void) fclose(mnttab_file
);
8216 if (ret
== 0 && log_history
)
8217 (void) zpool_log_history(g_zfs
, history_str
);
8222 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8223 * for the purposes of running ::findleaks.
8225 if (getenv("ZFS_ABORT") != NULL
) {
8226 (void) printf("dumping core by request\n");