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 https://opensource.org/licenses/CDDL-1.0.
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, 2020 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
29 * Copyright 2016 Nexenta Systems, Inc.
30 * Copyright (c) 2019 Datto Inc.
31 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
32 * Copyright 2019 Joyent, Inc.
33 * Copyright (c) 2019, 2020 by Christian Schwarz. All rights reserved.
38 #include <sys/debug.h>
44 #include <libnvpair.h>
59 #include <sys/mkdev.h>
60 #include <sys/mntent.h>
61 #include <sys/mnttab.h>
62 #include <sys/mount.h>
64 #include <sys/fs/zfs.h>
65 #include <sys/systeminfo.h>
66 #include <sys/types.h>
68 #include <sys/zfs_project.h>
71 #include <libzfs_core.h>
73 #include <zfs_deleg.h>
77 #include <directory.h>
78 #endif /* HAVE_IDMAP */
82 #include "zfs_comutil.h"
83 #include "zfs_projectutil.h"
85 libzfs_handle_t
*g_zfs
;
87 static char history_str
[HIS_MAX_RECORD_LEN
];
88 static boolean_t log_history
= B_TRUE
;
90 static int zfs_do_clone(int argc
, char **argv
);
91 static int zfs_do_create(int argc
, char **argv
);
92 static int zfs_do_destroy(int argc
, char **argv
);
93 static int zfs_do_get(int argc
, char **argv
);
94 static int zfs_do_inherit(int argc
, char **argv
);
95 static int zfs_do_list(int argc
, char **argv
);
96 static int zfs_do_mount(int argc
, char **argv
);
97 static int zfs_do_rename(int argc
, char **argv
);
98 static int zfs_do_rollback(int argc
, char **argv
);
99 static int zfs_do_set(int argc
, char **argv
);
100 static int zfs_do_upgrade(int argc
, char **argv
);
101 static int zfs_do_snapshot(int argc
, char **argv
);
102 static int zfs_do_unmount(int argc
, char **argv
);
103 static int zfs_do_share(int argc
, char **argv
);
104 static int zfs_do_unshare(int argc
, char **argv
);
105 static int zfs_do_send(int argc
, char **argv
);
106 static int zfs_do_receive(int argc
, char **argv
);
107 static int zfs_do_promote(int argc
, char **argv
);
108 static int zfs_do_userspace(int argc
, char **argv
);
109 static int zfs_do_allow(int argc
, char **argv
);
110 static int zfs_do_unallow(int argc
, char **argv
);
111 static int zfs_do_hold(int argc
, char **argv
);
112 static int zfs_do_holds(int argc
, char **argv
);
113 static int zfs_do_release(int argc
, char **argv
);
114 static int zfs_do_diff(int argc
, char **argv
);
115 static int zfs_do_bookmark(int argc
, char **argv
);
116 static int zfs_do_channel_program(int argc
, char **argv
);
117 static int zfs_do_load_key(int argc
, char **argv
);
118 static int zfs_do_unload_key(int argc
, char **argv
);
119 static int zfs_do_change_key(int argc
, char **argv
);
120 static int zfs_do_project(int argc
, char **argv
);
121 static int zfs_do_version(int argc
, char **argv
);
122 static int zfs_do_redact(int argc
, char **argv
);
123 static int zfs_do_wait(int argc
, char **argv
);
126 static int zfs_do_jail(int argc
, char **argv
);
127 static int zfs_do_unjail(int argc
, char **argv
);
131 static int zfs_do_zone(int argc
, char **argv
);
132 static int zfs_do_unzone(int argc
, char **argv
);
135 static int zfs_do_help(int argc
, char **argv
);
138 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
143 _umem_debug_init(void)
145 return ("default,verbose"); /* $UMEM_DEBUG setting */
149 _umem_logging_init(void)
151 return ("fail,contents"); /* $UMEM_LOGGING setting */
185 HELP_CHANNEL_PROGRAM
,
198 typedef struct zfs_command
{
200 int (*func
)(int argc
, char **argv
);
205 * Master command table. Each ZFS command has a name, associated function, and
206 * usage message. The usage messages need to be internationalized, so we have
207 * to have a function to return the usage message based on a command index.
209 * These commands are organized according to how they are displayed in the usage
210 * message. An empty command (one with a NULL name) indicates an empty line in
211 * the generic usage message.
213 static zfs_command_t command_table
[] = {
214 { "version", zfs_do_version
, HELP_VERSION
},
216 { "create", zfs_do_create
, HELP_CREATE
},
217 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
219 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
220 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
221 { "clone", zfs_do_clone
, HELP_CLONE
},
222 { "promote", zfs_do_promote
, HELP_PROMOTE
},
223 { "rename", zfs_do_rename
, HELP_RENAME
},
224 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
225 { "program", zfs_do_channel_program
, HELP_CHANNEL_PROGRAM
},
227 { "list", zfs_do_list
, HELP_LIST
},
229 { "set", zfs_do_set
, HELP_SET
},
230 { "get", zfs_do_get
, HELP_GET
},
231 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
232 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
234 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
235 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
236 { "projectspace", zfs_do_userspace
, HELP_PROJECTSPACE
},
238 { "project", zfs_do_project
, HELP_PROJECT
},
240 { "mount", zfs_do_mount
, HELP_MOUNT
},
241 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
242 { "share", zfs_do_share
, HELP_SHARE
},
243 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
245 { "send", zfs_do_send
, HELP_SEND
},
246 { "receive", zfs_do_receive
, HELP_RECEIVE
},
248 { "allow", zfs_do_allow
, HELP_ALLOW
},
250 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
252 { "hold", zfs_do_hold
, HELP_HOLD
},
253 { "holds", zfs_do_holds
, HELP_HOLDS
},
254 { "release", zfs_do_release
, HELP_RELEASE
},
255 { "diff", zfs_do_diff
, HELP_DIFF
},
256 { "load-key", zfs_do_load_key
, HELP_LOAD_KEY
},
257 { "unload-key", zfs_do_unload_key
, HELP_UNLOAD_KEY
},
258 { "change-key", zfs_do_change_key
, HELP_CHANGE_KEY
},
259 { "redact", zfs_do_redact
, HELP_REDACT
},
260 { "wait", zfs_do_wait
, HELP_WAIT
},
263 { "jail", zfs_do_jail
, HELP_JAIL
},
264 { "unjail", zfs_do_unjail
, HELP_UNJAIL
},
268 { "zone", zfs_do_zone
, HELP_ZONE
},
269 { "unzone", zfs_do_unzone
, HELP_UNZONE
},
273 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
275 zfs_command_t
*current_command
;
278 get_usage(zfs_help_t idx
)
282 return (gettext("\tclone [-p] [-o property=value] ... "
283 "<snapshot> <filesystem|volume>\n"));
285 return (gettext("\tcreate [-Pnpuv] [-o property=value] ... "
287 "\tcreate [-Pnpsv] [-b blocksize] [-o property=value] ... "
288 "-V <size> <volume>\n"));
290 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
291 "\tdestroy [-dnpRrv] "
292 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
293 "\tdestroy <filesystem|volume>#<bookmark>\n"));
295 return (gettext("\tget [-rHp] [-d max] "
296 "[-o \"all\" | field[,...]]\n"
297 "\t [-t type[,...]] [-s source[,...]]\n"
298 "\t <\"all\" | property[,...]> "
299 "[filesystem|volume|snapshot|bookmark] ...\n"));
301 return (gettext("\tinherit [-rS] <property> "
302 "<filesystem|volume|snapshot> ...\n"));
304 return (gettext("\tupgrade [-v]\n"
305 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
307 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
308 "[-s property]...\n\t [-S property]... [-t type[,...]] "
309 "[filesystem|volume|snapshot] ...\n"));
311 return (gettext("\tmount\n"
312 "\tmount [-flvO] [-o opts] <-a|-R filesystem|"
315 return (gettext("\tpromote <clone-filesystem>\n"));
317 return (gettext("\treceive [-vMnsFhu] "
318 "[-o <property>=<value>] ... [-x <property>] ...\n"
319 "\t <filesystem|volume|snapshot>\n"
320 "\treceive [-vMnsFhu] [-o <property>=<value>] ... "
321 "[-x <property>] ... \n"
322 "\t [-d | -e] <filesystem>\n"
323 "\treceive -A <filesystem|volume>\n"));
325 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
326 "<filesystem|volume|snapshot>\n"
327 "\trename -p [-f] <filesystem|volume> <filesystem|volume>\n"
328 "\trename -u [-f] <filesystem> <filesystem>\n"
329 "\trename -r <snapshot> <snapshot>\n"));
331 return (gettext("\trollback [-rRf] <snapshot>\n"));
333 return (gettext("\tsend [-DLPbcehnpsVvw] "
335 "\t [-R [-X dataset[,dataset]...]] <snapshot>\n"
336 "\tsend [-DnVvPLecw] [-i snapshot|bookmark] "
337 "<filesystem|volume|snapshot>\n"
338 "\tsend [-DnPpVvLec] [-i bookmark|snapshot] "
339 "--redact <bookmark> <snapshot>\n"
340 "\tsend [-nVvPe] -t <receive_resume_token>\n"
341 "\tsend [-PnVv] --saved filesystem\n"));
343 return (gettext("\tset [-u] <property=value> ... "
344 "<filesystem|volume|snapshot> ...\n"));
346 return (gettext("\tshare [-l] <-a [nfs|smb] | filesystem>\n"));
348 return (gettext("\tsnapshot [-r] [-o property=value] ... "
349 "<filesystem|volume>@<snap> ...\n"));
351 return (gettext("\tunmount [-fu] "
352 "<-a | filesystem|mountpoint>\n"));
354 return (gettext("\tunshare "
355 "<-a [nfs|smb] | filesystem|mountpoint>\n"));
357 return (gettext("\tallow <filesystem|volume>\n"
359 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
360 "\t <filesystem|volume>\n"
361 "\tallow [-ld] -e <perm|@setname>[,...] "
362 "<filesystem|volume>\n"
363 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
364 "\tallow -s @setname <perm|@setname>[,...] "
365 "<filesystem|volume>\n"));
367 return (gettext("\tunallow [-rldug] "
368 "<\"everyone\"|user|group>[,...]\n"
369 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
370 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
371 "<filesystem|volume>\n"
372 "\tunallow [-r] -c [<perm|@setname>[,...]] "
373 "<filesystem|volume>\n"
374 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
375 "<filesystem|volume>\n"));
377 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
379 "\t [-S field] ... [-t type[,...]] "
380 "<filesystem|snapshot|path>\n"));
381 case HELP_GROUPSPACE
:
382 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
384 "\t [-S field] ... [-t type[,...]] "
385 "<filesystem|snapshot|path>\n"));
386 case HELP_PROJECTSPACE
:
387 return (gettext("\tprojectspace [-Hp] [-o field[,...]] "
389 "\t [-S field] ... <filesystem|snapshot|path>\n"));
391 return (gettext("\tproject [-d|-r] <directory|file ...>\n"
392 "\tproject -c [-0] [-d|-r] [-p id] <directory|file ...>\n"
393 "\tproject -C [-k] [-r] <directory ...>\n"
394 "\tproject [-p id] [-r] [-s] <directory ...>\n"));
396 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
398 return (gettext("\tholds [-rHp] <snapshot> ...\n"));
400 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
402 return (gettext("\tdiff [-FHth] <snapshot> "
403 "[snapshot|filesystem]\n"));
405 return (gettext("\tbookmark <snapshot|bookmark> "
407 case HELP_CHANNEL_PROGRAM
:
408 return (gettext("\tprogram [-jn] [-t <instruction limit>] "
409 "[-m <memory limit (b)>]\n"
410 "\t <pool> <program file> [lua args...]\n"));
412 return (gettext("\tload-key [-rn] [-L <keylocation>] "
413 "<-a | filesystem|volume>\n"));
414 case HELP_UNLOAD_KEY
:
415 return (gettext("\tunload-key [-r] "
416 "<-a | filesystem|volume>\n"));
417 case HELP_CHANGE_KEY
:
418 return (gettext("\tchange-key [-l] [-o keyformat=<value>]\n"
419 "\t [-o keylocation=<value>] [-o pbkdf2iters=<value>]\n"
420 "\t <filesystem|volume>\n"
421 "\tchange-key -i [-l] <filesystem|volume>\n"));
423 return (gettext("\tversion\n"));
425 return (gettext("\tredact <snapshot> <bookmark> "
426 "<redaction_snapshot> ...\n"));
428 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
430 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
432 return (gettext("\twait [-t <activity>] <filesystem>\n"));
434 return (gettext("\tzone <nsfile> <filesystem>\n"));
436 return (gettext("\tunzone <nsfile> <filesystem>\n"));
438 __builtin_unreachable();
445 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
450 * Utility function to guarantee malloc() success.
454 safe_malloc(size_t size
)
458 if ((data
= calloc(1, size
)) == NULL
)
465 safe_realloc(void *data
, size_t size
)
468 if ((newp
= realloc(data
, size
)) == NULL
) {
477 safe_strdup(const char *str
)
479 char *dupstr
= strdup(str
);
488 * Callback routine that will print out information for each of
492 usage_prop_cb(int prop
, void *cb
)
496 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
498 if (zfs_prop_readonly(prop
))
499 (void) fprintf(fp
, " NO ");
501 (void) fprintf(fp
, "YES ");
503 if (zfs_prop_inheritable(prop
))
504 (void) fprintf(fp
, " YES ");
506 (void) fprintf(fp
, " NO ");
508 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
) ?: "-");
514 * Display usage message. If we're inside a command, display only the usage for
515 * that command. Otherwise, iterate over the entire command table and display
516 * a complete usage message.
518 static __attribute__((noreturn
)) void
519 usage(boolean_t requested
)
522 boolean_t show_properties
= B_FALSE
;
523 FILE *fp
= requested
? stdout
: stderr
;
525 if (current_command
== NULL
) {
527 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
529 gettext("where 'command' is one of the following:\n\n"));
531 for (i
= 0; i
< NCOMMAND
; i
++) {
532 if (command_table
[i
].name
== NULL
)
533 (void) fprintf(fp
, "\n");
535 (void) fprintf(fp
, "%s",
536 get_usage(command_table
[i
].usage
));
539 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
540 "pool/[dataset/]*dataset[@name]\n"));
542 (void) fprintf(fp
, gettext("usage:\n"));
543 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
546 if (current_command
!= NULL
&&
547 (strcmp(current_command
->name
, "set") == 0 ||
548 strcmp(current_command
->name
, "get") == 0 ||
549 strcmp(current_command
->name
, "inherit") == 0 ||
550 strcmp(current_command
->name
, "list") == 0))
551 show_properties
= B_TRUE
;
553 if (show_properties
) {
554 (void) fprintf(fp
, "%s",
555 gettext("\nThe following properties are supported:\n"));
557 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
558 "PROPERTY", "EDIT", "INHERIT", "VALUES");
560 /* Iterate over all properties */
561 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
564 (void) fprintf(fp
, "\t%-15s ", "userused@...");
565 (void) fprintf(fp
, " NO NO <size>\n");
566 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
567 (void) fprintf(fp
, " NO NO <size>\n");
568 (void) fprintf(fp
, "\t%-15s ", "projectused@...");
569 (void) fprintf(fp
, " NO NO <size>\n");
570 (void) fprintf(fp
, "\t%-15s ", "userobjused@...");
571 (void) fprintf(fp
, " NO NO <size>\n");
572 (void) fprintf(fp
, "\t%-15s ", "groupobjused@...");
573 (void) fprintf(fp
, " NO NO <size>\n");
574 (void) fprintf(fp
, "\t%-15s ", "projectobjused@...");
575 (void) fprintf(fp
, " NO NO <size>\n");
576 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
577 (void) fprintf(fp
, "YES NO <size> | none\n");
578 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
579 (void) fprintf(fp
, "YES NO <size> | none\n");
580 (void) fprintf(fp
, "\t%-15s ", "projectquota@...");
581 (void) fprintf(fp
, "YES NO <size> | none\n");
582 (void) fprintf(fp
, "\t%-15s ", "userobjquota@...");
583 (void) fprintf(fp
, "YES NO <size> | none\n");
584 (void) fprintf(fp
, "\t%-15s ", "groupobjquota@...");
585 (void) fprintf(fp
, "YES NO <size> | none\n");
586 (void) fprintf(fp
, "\t%-15s ", "projectobjquota@...");
587 (void) fprintf(fp
, "YES NO <size> | none\n");
588 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
589 (void) fprintf(fp
, " NO NO <size>\n");
590 (void) fprintf(fp
, "\t%-15s ", "written#<bookmark>");
591 (void) fprintf(fp
, " NO NO <size>\n");
593 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
594 "with standard units such as K, M, G, etc.\n"));
595 (void) fprintf(fp
, "%s", gettext("\nUser-defined properties "
596 "can be specified by using a name containing a colon "
598 (void) fprintf(fp
, gettext("\nThe {user|group|project}"
599 "[obj]{used|quota}@ properties must be appended with\n"
600 "a user|group|project specifier of one of these forms:\n"
601 " POSIX name (eg: \"matt\")\n"
602 " POSIX id (eg: \"126829\")\n"
603 " SMB name@domain (eg: \"matt@sun\")\n"
604 " SMB SID (eg: \"S-1-234-567-89\")\n"));
607 gettext("\nFor the property list, run: %s\n"),
610 gettext("\nFor the delegated permission list, run: %s\n"),
611 "zfs allow|unallow");
613 gettext("\nFor further help on a command or topic, "
614 "run: %s\n"), "zfs help [<topic>]");
618 * See comments at end of main().
620 if (getenv("ZFS_ABORT") != NULL
) {
621 (void) printf("dumping core by request\n");
625 exit(requested
? 0 : 2);
629 * Take a property=value argument string and add it to the given nvlist.
630 * Modifies the argument inplace.
633 parseprop(nvlist_t
*props
, char *propname
)
637 if ((propval
= strchr(propname
, '=')) == NULL
) {
638 (void) fprintf(stderr
, gettext("missing "
639 "'=' for property=value argument\n"));
644 if (nvlist_exists(props
, propname
)) {
645 (void) fprintf(stderr
, gettext("property '%s' "
646 "specified multiple times\n"), propname
);
649 if (nvlist_add_string(props
, propname
, propval
) != 0)
655 * Take a property name argument and add it to the given nvlist.
656 * Modifies the argument inplace.
659 parsepropname(nvlist_t
*props
, char *propname
)
661 if (strchr(propname
, '=') != NULL
) {
662 (void) fprintf(stderr
, gettext("invalid character "
663 "'=' in property argument\n"));
666 if (nvlist_exists(props
, propname
)) {
667 (void) fprintf(stderr
, gettext("property '%s' "
668 "specified multiple times\n"), propname
);
671 if (nvlist_add_boolean(props
, propname
) != 0)
677 parse_depth(char *opt
, int *flags
)
682 depth
= (int)strtol(opt
, &tmp
, 0);
684 (void) fprintf(stderr
,
685 gettext("%s is not an integer\n"), optarg
);
689 (void) fprintf(stderr
,
690 gettext("Depth can not be negative.\n"));
693 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
697 #define PROGRESS_DELAY 2 /* seconds */
699 static const char *pt_reverse
=
700 "\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";
701 static time_t pt_begin
;
702 static char *pt_header
= NULL
;
703 static boolean_t pt_shown
;
706 start_progress_timer(void)
708 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
713 set_progress_header(const char *header
)
715 assert(pt_header
== NULL
);
716 pt_header
= safe_strdup(header
);
718 (void) printf("%s: ", header
);
719 (void) fflush(stdout
);
724 update_progress(const char *update
)
726 if (!pt_shown
&& time(NULL
) > pt_begin
) {
727 int len
= strlen(update
);
729 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
731 (void) fflush(stdout
);
733 } else if (pt_shown
) {
734 int len
= strlen(update
);
736 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
737 (void) fflush(stdout
);
742 finish_progress(const char *done
)
746 (void) fflush(stdout
);
753 zfs_mount_and_share(libzfs_handle_t
*hdl
, const char *dataset
, zfs_type_t type
)
755 zfs_handle_t
*zhp
= NULL
;
758 zhp
= zfs_open(hdl
, dataset
, type
);
763 * Volumes may neither be mounted or shared. Potentially in the
764 * future filesystems detected on these volumes could be mounted.
766 if (zfs_get_type(zhp
) == ZFS_TYPE_VOLUME
) {
772 * Mount and/or share the new filesystem as appropriate. We provide a
773 * verbose error message to let the user know that their filesystem was
774 * in fact created, even if we failed to mount or share it.
776 * If the user doesn't want the dataset automatically mounted, then
777 * skip the mount/share step
779 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
, B_FALSE
) &&
780 zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
) {
781 if (zfs_mount_delegation_check()) {
782 (void) fprintf(stderr
, gettext("filesystem "
783 "successfully created, but it may only be "
784 "mounted by root\n"));
786 } else if (zfs_mount(zhp
, NULL
, 0) != 0) {
787 (void) fprintf(stderr
, gettext("filesystem "
788 "successfully created, but not mounted\n"));
790 } else if (zfs_share(zhp
, NULL
) != 0) {
791 (void) fprintf(stderr
, gettext("filesystem "
792 "successfully created, but not shared\n"));
795 zfs_commit_shares(NULL
);
804 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
806 * Given an existing dataset, create a writable copy whose initial contents
807 * are the same as the source. The newly created dataset maintains a
808 * dependency on the original; the original cannot be destroyed so long as
811 * The '-p' flag creates all the non-existing ancestors of the target first.
814 zfs_do_clone(int argc
, char **argv
)
816 zfs_handle_t
*zhp
= NULL
;
817 boolean_t parents
= B_FALSE
;
822 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
826 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
829 if (!parseprop(props
, optarg
)) {
838 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
847 /* check number of arguments */
849 (void) fprintf(stderr
, gettext("missing source dataset "
854 (void) fprintf(stderr
, gettext("missing target dataset "
859 (void) fprintf(stderr
, gettext("too many arguments\n"));
863 /* open the source dataset */
864 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
) {
869 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
872 * Now create the ancestors of the target dataset. If the
873 * target already exists and '-p' option was used we should not
876 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
882 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
890 ret
= zfs_clone(zhp
, argv
[1], props
);
892 /* create the mountpoint if necessary */
895 (void) zpool_log_history(g_zfs
, history_str
);
896 log_history
= B_FALSE
;
899 ret
= zfs_mount_and_share(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
908 ASSERT3P(zhp
, ==, NULL
);
915 * Return a default volblocksize for the pool which always uses more than
916 * half of the data sectors. This primarily applies to dRAID which always
917 * writes full stripe widths.
920 default_volblocksize(zpool_handle_t
*zhp
, nvlist_t
*props
)
922 uint64_t volblocksize
, asize
= SPA_MINBLOCKSIZE
;
923 nvlist_t
*tree
, **vdevs
;
926 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
928 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
) != 0 ||
929 nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
,
930 &vdevs
, &nvdevs
) != 0) {
931 return (ZVOL_DEFAULT_BLOCKSIZE
);
934 for (int i
= 0; i
< nvdevs
; i
++) {
935 nvlist_t
*nv
= vdevs
[i
];
936 uint64_t ashift
, ndata
, nparity
;
938 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASHIFT
, &ashift
) != 0)
941 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DRAID_NDATA
,
943 /* dRAID minimum allocation width */
944 asize
= MAX(asize
, ndata
* (1ULL << ashift
));
945 } else if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
947 /* raidz minimum allocation width */
949 asize
= MAX(asize
, 2 * (1ULL << ashift
));
951 asize
= MAX(asize
, 4 * (1ULL << ashift
));
953 /* mirror or (non-redundant) leaf vdev */
954 asize
= MAX(asize
, 1ULL << ashift
);
959 * Calculate the target volblocksize such that more than half
960 * of the asize is used. The following table is for 4k sectors.
962 * n asize blksz used | n asize blksz used
963 * -------------------------+---------------------------------
964 * 1 4,096 8,192 100% | 9 36,864 32,768 88%
965 * 2 8,192 8,192 100% | 10 40,960 32,768 80%
966 * 3 12,288 8,192 66% | 11 45,056 32,768 72%
967 * 4 16,384 16,384 100% | 12 49,152 32,768 66%
968 * 5 20,480 16,384 80% | 13 53,248 32,768 61%
969 * 6 24,576 16,384 66% | 14 57,344 32,768 57%
970 * 7 28,672 16,384 57% | 15 61,440 32,768 53%
971 * 8 32,768 32,768 100% | 16 65,536 65,636 100%
973 * This is primarily a concern for dRAID which always allocates
974 * a full stripe width. For dRAID the default stripe width is
975 * n=8 in which case the volblocksize is set to 32k. Ignoring
976 * compression there are no unused sectors. This same reasoning
977 * applies to raidz[2,3] so target 4 sectors to minimize waste.
979 uint64_t tgt_volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
980 while (tgt_volblocksize
* 2 <= asize
)
981 tgt_volblocksize
*= 2;
983 const char *prop
= zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
);
984 if (nvlist_lookup_uint64(props
, prop
, &volblocksize
) == 0) {
986 /* Issue a warning when a non-optimal size is requested. */
987 if (volblocksize
< ZVOL_DEFAULT_BLOCKSIZE
) {
988 (void) fprintf(stderr
, gettext("Warning: "
989 "volblocksize (%llu) is less than the default "
990 "minimum block size (%llu).\nTo reduce wasted "
991 "space a volblocksize of %llu is recommended.\n"),
992 (u_longlong_t
)volblocksize
,
993 (u_longlong_t
)ZVOL_DEFAULT_BLOCKSIZE
,
994 (u_longlong_t
)tgt_volblocksize
);
995 } else if (volblocksize
< tgt_volblocksize
) {
996 (void) fprintf(stderr
, gettext("Warning: "
997 "volblocksize (%llu) is much less than the "
998 "minimum allocation\nunit (%llu), which wastes "
999 "at least %llu%% of space. To reduce wasted "
1000 "space,\nuse a larger volblocksize (%llu is "
1001 "recommended), fewer dRAID data disks\n"
1002 "per group, or smaller sector size (ashift).\n"),
1003 (u_longlong_t
)volblocksize
, (u_longlong_t
)asize
,
1004 (u_longlong_t
)((100 * (asize
- volblocksize
)) /
1005 asize
), (u_longlong_t
)tgt_volblocksize
);
1008 volblocksize
= tgt_volblocksize
;
1009 fnvlist_add_uint64(props
, prop
, volblocksize
);
1012 return (volblocksize
);
1016 * zfs create [-Pnpv] [-o prop=value] ... fs
1017 * zfs create [-Pnpsv] [-b blocksize] [-o prop=value] ... -V vol size
1019 * Create a new dataset. This command can be used to create filesystems
1020 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
1021 * For volumes, the user must specify a size to be used.
1023 * The '-s' flag applies only to volumes, and indicates that we should not try
1024 * to set the reservation for this volume. By default we set a reservation
1025 * equal to the size for any volume. For pools with SPA_VERSION >=
1026 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
1028 * The '-p' flag creates all the non-existing ancestors of the target first.
1030 * The '-n' flag is no-op (dry run) mode. This will perform a user-space sanity
1031 * check of arguments and properties, but does not check for permissions,
1032 * available space, etc.
1034 * The '-u' flag prevents the newly created file system from being mounted.
1036 * The '-v' flag is for verbose output.
1038 * The '-P' flag is used for parseable output. It implies '-v'.
1041 zfs_do_create(int argc
, char **argv
)
1043 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
1044 zpool_handle_t
*zpool_handle
= NULL
;
1045 nvlist_t
*real_props
= NULL
;
1046 uint64_t volsize
= 0;
1048 boolean_t noreserve
= B_FALSE
;
1049 boolean_t bflag
= B_FALSE
;
1050 boolean_t parents
= B_FALSE
;
1051 boolean_t dryrun
= B_FALSE
;
1052 boolean_t nomount
= B_FALSE
;
1053 boolean_t verbose
= B_FALSE
;
1054 boolean_t parseable
= B_FALSE
;
1060 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
1064 while ((c
= getopt(argc
, argv
, ":PV:b:nso:puv")) != -1) {
1067 type
= ZFS_TYPE_VOLUME
;
1068 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
1069 (void) fprintf(stderr
, gettext("bad volume "
1070 "size '%s': %s\n"), optarg
,
1071 libzfs_error_description(g_zfs
));
1075 if (nvlist_add_uint64(props
,
1076 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
1089 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
1090 (void) fprintf(stderr
, gettext("bad volume "
1091 "block size '%s': %s\n"), optarg
,
1092 libzfs_error_description(g_zfs
));
1096 if (nvlist_add_uint64(props
,
1097 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1105 if (!parseprop(props
, optarg
))
1118 (void) fprintf(stderr
, gettext("missing size "
1122 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1128 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
1129 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
1130 "used when creating a volume\n"));
1133 if (nomount
&& type
!= ZFS_TYPE_FILESYSTEM
) {
1134 (void) fprintf(stderr
, gettext("'-u' can only be "
1135 "used when creating a filesystem\n"));
1142 /* check number of arguments */
1144 (void) fprintf(stderr
, gettext("missing %s argument\n"),
1145 zfs_type_to_name(type
));
1149 (void) fprintf(stderr
, gettext("too many arguments\n"));
1153 if (dryrun
|| type
== ZFS_TYPE_VOLUME
) {
1154 char msg
[ZFS_MAX_DATASET_NAME_LEN
* 2];
1157 if ((p
= strchr(argv
[0], '/')) != NULL
)
1159 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
1162 if (zpool_handle
== NULL
)
1165 (void) snprintf(msg
, sizeof (msg
),
1166 dryrun
? gettext("cannot verify '%s'") :
1167 gettext("cannot create '%s'"), argv
[0]);
1168 if (props
&& (real_props
= zfs_valid_proplist(g_zfs
, type
,
1169 props
, 0, NULL
, zpool_handle
, B_TRUE
, msg
)) == NULL
) {
1170 zpool_close(zpool_handle
);
1175 if (type
== ZFS_TYPE_VOLUME
) {
1176 const char *prop
= zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
);
1177 uint64_t volblocksize
= default_volblocksize(zpool_handle
,
1180 if (volblocksize
!= ZVOL_DEFAULT_BLOCKSIZE
&&
1181 nvlist_lookup_string(props
, prop
, &strval
) != 0) {
1183 if (asprintf(&tmp
, "%llu",
1184 (u_longlong_t
)volblocksize
) == -1)
1186 nvlist_add_string(props
, prop
, tmp
);
1191 * If volsize is not a multiple of volblocksize, round it
1192 * up to the nearest multiple of the volblocksize.
1194 if (volsize
% volblocksize
) {
1195 volsize
= P2ROUNDUP_TYPED(volsize
, volblocksize
,
1198 if (nvlist_add_uint64(props
,
1199 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), volsize
) != 0) {
1206 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
1207 uint64_t spa_version
;
1208 zfs_prop_t resv_prop
;
1210 spa_version
= zpool_get_prop_int(zpool_handle
,
1211 ZPOOL_PROP_VERSION
, NULL
);
1212 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
1213 resv_prop
= ZFS_PROP_REFRESERVATION
;
1215 resv_prop
= ZFS_PROP_RESERVATION
;
1217 volsize
= zvol_volsize_to_reservation(zpool_handle
, volsize
,
1220 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
1222 if (nvlist_add_uint64(props
,
1223 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
1229 if (zpool_handle
!= NULL
) {
1230 zpool_close(zpool_handle
);
1231 nvlist_free(real_props
);
1234 if (parents
&& zfs_name_valid(argv
[0], type
)) {
1236 * Now create the ancestors of target dataset. If the target
1237 * already exists and '-p' option was used we should not
1240 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
1245 (void) printf(parseable
? "create_ancestors\t%s\n" :
1246 dryrun
? "would create ancestors of %s\n" :
1247 "create ancestors of %s\n", argv
[0]);
1250 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0) {
1257 nvpair_t
*nvp
= NULL
;
1258 (void) printf(parseable
? "create\t%s\n" :
1259 dryrun
? "would create %s\n" : "create %s\n", argv
[0]);
1260 while ((nvp
= nvlist_next_nvpair(props
, nvp
)) != NULL
) {
1264 switch (nvpair_type(nvp
)) {
1265 case DATA_TYPE_UINT64
:
1266 VERIFY0(nvpair_value_uint64(nvp
, &uval
));
1267 (void) printf(parseable
?
1268 "property\t%s\t%llu\n" : "\t%s=%llu\n",
1269 nvpair_name(nvp
), (u_longlong_t
)uval
);
1271 case DATA_TYPE_STRING
:
1272 VERIFY0(nvpair_value_string(nvp
, &sval
));
1273 (void) printf(parseable
?
1274 "property\t%s\t%s\n" : "\t%s=%s\n",
1275 nvpair_name(nvp
), sval
);
1278 (void) fprintf(stderr
, "property '%s' "
1279 "has illegal type %d\n",
1280 nvpair_name(nvp
), nvpair_type(nvp
));
1290 /* pass to libzfs */
1291 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
1295 (void) zpool_log_history(g_zfs
, history_str
);
1296 log_history
= B_FALSE
;
1304 ret
= zfs_mount_and_share(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
1315 * zfs destroy [-rRf] <fs, vol>
1316 * zfs destroy [-rRd] <snap>
1318 * -r Recursively destroy all children
1319 * -R Recursively destroy all dependents, including clones
1320 * -f Force unmounting of any dependents
1321 * -d If we can't destroy now, mark for deferred destruction
1323 * Destroys the given dataset. By default, it will unmount any filesystems,
1324 * and refuse to destroy a dataset that has any dependents. A dependent can
1325 * either be a child, or a clone of a child.
1327 typedef struct destroy_cbdata
{
1330 boolean_t cb_recurse
;
1332 boolean_t cb_doclones
;
1333 zfs_handle_t
*cb_target
;
1334 boolean_t cb_defer_destroy
;
1335 boolean_t cb_verbose
;
1336 boolean_t cb_parsable
;
1337 boolean_t cb_dryrun
;
1339 nvlist_t
*cb_batchedsnaps
;
1341 /* first snap in contiguous run */
1343 /* previous snap in contiguous run */
1345 int64_t cb_snapused
;
1348 uint64_t cb_snap_count
;
1352 * Check for any dependents based on the '-r' or '-R' flags.
1355 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
1357 destroy_cbdata_t
*cbp
= data
;
1358 const char *tname
= zfs_get_name(cbp
->cb_target
);
1359 const char *name
= zfs_get_name(zhp
);
1361 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
1362 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
1364 * This is a direct descendant, not a clone somewhere else in
1367 if (cbp
->cb_recurse
)
1370 if (cbp
->cb_first
) {
1371 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1372 "%s has children\n"),
1373 zfs_get_name(cbp
->cb_target
),
1374 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1375 (void) fprintf(stderr
, gettext("use '-r' to destroy "
1376 "the following datasets:\n"));
1377 cbp
->cb_first
= B_FALSE
;
1378 cbp
->cb_error
= B_TRUE
;
1381 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1384 * This is a clone. We only want to report this if the '-r'
1385 * wasn't specified, or the target is a snapshot.
1387 if (!cbp
->cb_recurse
&&
1388 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
1391 if (cbp
->cb_first
) {
1392 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1393 "%s has dependent clones\n"),
1394 zfs_get_name(cbp
->cb_target
),
1395 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1396 (void) fprintf(stderr
, gettext("use '-R' to destroy "
1397 "the following datasets:\n"));
1398 cbp
->cb_first
= B_FALSE
;
1399 cbp
->cb_error
= B_TRUE
;
1400 cbp
->cb_dryrun
= B_TRUE
;
1403 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1412 destroy_batched(destroy_cbdata_t
*cb
)
1414 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1415 cb
->cb_batchedsnaps
, B_FALSE
);
1416 fnvlist_free(cb
->cb_batchedsnaps
);
1417 cb
->cb_batchedsnaps
= fnvlist_alloc();
1422 destroy_callback(zfs_handle_t
*zhp
, void *data
)
1424 destroy_cbdata_t
*cb
= data
;
1425 const char *name
= zfs_get_name(zhp
);
1428 if (cb
->cb_verbose
) {
1429 if (cb
->cb_parsable
) {
1430 (void) printf("destroy\t%s\n", name
);
1431 } else if (cb
->cb_dryrun
) {
1432 (void) printf(gettext("would destroy %s\n"),
1435 (void) printf(gettext("will destroy %s\n"),
1441 * Ignore pools (which we've already flagged as an error before getting
1444 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1445 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1449 if (cb
->cb_dryrun
) {
1455 * We batch up all contiguous snapshots (even of different
1456 * filesystems) and destroy them with one ioctl. We can't
1457 * simply do all snap deletions and then all fs deletions,
1458 * because we must delete a clone before its origin.
1460 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1461 cb
->cb_snap_count
++;
1462 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1463 if (cb
->cb_snap_count
% 10 == 0 && cb
->cb_defer_destroy
) {
1464 error
= destroy_batched(cb
);
1471 error
= destroy_batched(cb
);
1473 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1474 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1477 * When performing a recursive destroy we ignore errors
1478 * so that the recursive destroy could continue
1479 * destroying past problem datasets
1481 if (cb
->cb_recurse
) {
1482 cb
->cb_error
= B_TRUE
;
1494 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1496 destroy_cbdata_t
*cb
= arg
;
1497 const char *name
= zfs_get_name(zhp
);
1500 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1501 if (cb
->cb_firstsnap
== NULL
)
1502 cb
->cb_firstsnap
= strdup(name
);
1503 if (cb
->cb_prevsnap
!= NULL
)
1504 free(cb
->cb_prevsnap
);
1505 /* this snap continues the current range */
1506 cb
->cb_prevsnap
= strdup(name
);
1507 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1509 if (cb
->cb_verbose
) {
1510 if (cb
->cb_parsable
) {
1511 (void) printf("destroy\t%s\n", name
);
1512 } else if (cb
->cb_dryrun
) {
1513 (void) printf(gettext("would destroy %s\n"),
1516 (void) printf(gettext("will destroy %s\n"),
1520 } else if (cb
->cb_firstsnap
!= NULL
) {
1521 /* end of this range */
1523 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1524 cb
->cb_prevsnap
, &used
);
1525 cb
->cb_snapused
+= used
;
1526 free(cb
->cb_firstsnap
);
1527 cb
->cb_firstsnap
= NULL
;
1528 free(cb
->cb_prevsnap
);
1529 cb
->cb_prevsnap
= NULL
;
1536 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1539 assert(cb
->cb_firstsnap
== NULL
);
1540 assert(cb
->cb_prevsnap
== NULL
);
1541 err
= zfs_iter_snapshots_sorted_v2(fs_zhp
, 0, destroy_print_cb
, cb
, 0,
1543 if (cb
->cb_firstsnap
!= NULL
) {
1546 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1547 cb
->cb_prevsnap
, &used
);
1549 cb
->cb_snapused
+= used
;
1550 free(cb
->cb_firstsnap
);
1551 cb
->cb_firstsnap
= NULL
;
1552 free(cb
->cb_prevsnap
);
1553 cb
->cb_prevsnap
= NULL
;
1559 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1561 destroy_cbdata_t
*cb
= arg
;
1564 /* Check for clones. */
1565 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1566 cb
->cb_target
= zhp
;
1567 cb
->cb_first
= B_TRUE
;
1568 err
= zfs_iter_dependents_v2(zhp
, 0, B_TRUE
,
1569 destroy_check_dependent
, cb
);
1573 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1581 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1583 destroy_cbdata_t
*cb
= arg
;
1586 err
= zfs_iter_snapspec_v2(zhp
, 0, cb
->cb_snapspec
,
1587 snapshot_to_nvl_cb
, cb
);
1593 if (cb
->cb_verbose
) {
1594 err
= destroy_print_snapshots(zhp
, cb
);
1600 err
= zfs_iter_filesystems_v2(zhp
, 0, gather_snapshots
, cb
);
1608 destroy_clones(destroy_cbdata_t
*cb
)
1611 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1613 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1614 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1617 boolean_t defer
= cb
->cb_defer_destroy
;
1621 * We can't defer destroy non-snapshots, so set it to
1622 * false while destroying the clones.
1624 cb
->cb_defer_destroy
= B_FALSE
;
1625 err
= zfs_iter_dependents_v2(zhp
, 0, B_FALSE
,
1626 destroy_callback
, cb
);
1627 cb
->cb_defer_destroy
= defer
;
1637 zfs_do_destroy(int argc
, char **argv
)
1639 destroy_cbdata_t cb
= { 0 };
1643 zfs_handle_t
*zhp
= NULL
;
1645 zfs_type_t type
= ZFS_TYPE_DATASET
;
1648 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1651 cb
.cb_verbose
= B_TRUE
;
1654 cb
.cb_verbose
= B_TRUE
;
1655 cb
.cb_parsable
= B_TRUE
;
1658 cb
.cb_dryrun
= B_TRUE
;
1661 cb
.cb_defer_destroy
= B_TRUE
;
1662 type
= ZFS_TYPE_SNAPSHOT
;
1665 cb
.cb_force
= B_TRUE
;
1668 cb
.cb_recurse
= B_TRUE
;
1671 cb
.cb_recurse
= B_TRUE
;
1672 cb
.cb_doclones
= B_TRUE
;
1676 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1685 /* check number of arguments */
1687 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1691 (void) fprintf(stderr
, gettext("too many arguments\n"));
1695 at
= strchr(argv
[0], '@');
1696 pound
= strchr(argv
[0], '#');
1699 /* Build the list of snaps to destroy in cb_nvl. */
1700 cb
.cb_nvl
= fnvlist_alloc();
1703 zhp
= zfs_open(g_zfs
, argv
[0],
1704 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1706 nvlist_free(cb
.cb_nvl
);
1710 cb
.cb_snapspec
= at
+ 1;
1711 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1717 if (nvlist_empty(cb
.cb_nvl
)) {
1718 (void) fprintf(stderr
, gettext("could not find any "
1719 "snapshots to destroy; check snapshot names.\n"));
1724 if (cb
.cb_verbose
) {
1726 zfs_nicebytes(cb
.cb_snapused
, buf
, sizeof (buf
));
1727 if (cb
.cb_parsable
) {
1728 (void) printf("reclaim\t%llu\n",
1729 (u_longlong_t
)cb
.cb_snapused
);
1730 } else if (cb
.cb_dryrun
) {
1731 (void) printf(gettext("would reclaim %s\n"),
1734 (void) printf(gettext("will reclaim %s\n"),
1739 if (!cb
.cb_dryrun
) {
1740 if (cb
.cb_doclones
) {
1741 cb
.cb_batchedsnaps
= fnvlist_alloc();
1742 err
= destroy_clones(&cb
);
1744 err
= zfs_destroy_snaps_nvl(g_zfs
,
1745 cb
.cb_batchedsnaps
, B_FALSE
);
1753 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1754 cb
.cb_defer_destroy
);
1760 } else if (pound
!= NULL
) {
1765 (void) fprintf(stderr
,
1766 "dryrun is not supported with bookmark\n");
1770 if (cb
.cb_defer_destroy
) {
1771 (void) fprintf(stderr
,
1772 "defer destroy is not supported with bookmark\n");
1776 if (cb
.cb_recurse
) {
1777 (void) fprintf(stderr
,
1778 "recursive is not supported with bookmark\n");
1783 * Unfortunately, zfs_bookmark() doesn't honor the
1784 * casesensitivity setting. However, we can't simply
1785 * remove this check, because lzc_destroy_bookmarks()
1786 * ignores non-existent bookmarks, so this is necessary
1787 * to get a proper error message.
1789 if (!zfs_bookmark_exists(argv
[0])) {
1790 (void) fprintf(stderr
, gettext("bookmark '%s' "
1791 "does not exist.\n"), argv
[0]);
1795 nvl
= fnvlist_alloc();
1796 fnvlist_add_boolean(nvl
, argv
[0]);
1798 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1800 (void) zfs_standard_error(g_zfs
, err
,
1801 "cannot destroy bookmark");
1808 /* Open the given dataset */
1809 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1815 * Perform an explicit check for pools before going any further.
1817 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1818 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1819 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1820 "operation does not apply to pools\n"),
1822 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1823 "%s' to destroy all datasets in the pool\n"),
1825 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1826 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1832 * Check for any dependents and/or clones.
1834 cb
.cb_first
= B_TRUE
;
1835 if (!cb
.cb_doclones
&& zfs_iter_dependents_v2(zhp
, 0, B_TRUE
,
1836 destroy_check_dependent
, &cb
) != 0) {
1845 cb
.cb_batchedsnaps
= fnvlist_alloc();
1846 if (zfs_iter_dependents_v2(zhp
, 0, B_FALSE
, destroy_callback
,
1853 * Do the real thing. The callback will close the
1854 * handle regardless of whether it succeeds or not.
1856 err
= destroy_callback(zhp
, &cb
);
1859 err
= zfs_destroy_snaps_nvl(g_zfs
,
1860 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1862 if (err
!= 0 || cb
.cb_error
== B_TRUE
)
1867 fnvlist_free(cb
.cb_batchedsnaps
);
1868 fnvlist_free(cb
.cb_nvl
);
1875 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1878 zfs_get_column_t col
;
1880 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1881 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1882 if (col
== GET_COL_RECVD
)
1888 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1889 * < all | property[,property]... > < fs | snap | vol > ...
1891 * -r recurse over any child datasets
1892 * -H scripted mode. Headers are stripped, and fields are separated
1893 * by tabs instead of spaces.
1894 * -o Set of fields to display. One of "name,property,value,
1895 * received,source". Default is "name,property,value,source".
1896 * "all" is an alias for all five.
1897 * -s Set of sources to allow. One of
1898 * "local,default,inherited,received,temporary,none". Default is
1900 * -p Display values in parsable (literal) format.
1902 * Prints properties for the given datasets. The user can control which
1903 * columns to display as well as which property types to allow.
1907 * Invoked to display the properties for a single dataset.
1910 get_callback(zfs_handle_t
*zhp
, void *data
)
1912 char buf
[ZFS_MAXPROPLEN
];
1913 char rbuf
[ZFS_MAXPROPLEN
];
1914 zprop_source_t sourcetype
;
1915 char source
[ZFS_MAX_DATASET_NAME_LEN
];
1916 zprop_get_cbdata_t
*cbp
= data
;
1917 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1918 zprop_list_t
*pl
= cbp
->cb_proplist
;
1921 const char *sourceval
;
1922 boolean_t received
= is_recvd_column(cbp
);
1924 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1925 char *recvdval
= NULL
;
1927 * Skip the special fake placeholder. This will also skip over
1928 * the name property when 'all' is specified.
1930 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1931 pl
== cbp
->cb_proplist
)
1934 if (pl
->pl_prop
!= ZPROP_USERPROP
) {
1935 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1936 sizeof (buf
), &sourcetype
, source
,
1938 cbp
->cb_literal
) != 0) {
1941 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1942 ZFS_TYPE_DATASET
, B_FALSE
)) {
1943 (void) fprintf(stderr
,
1944 gettext("No such property '%s'\n"),
1945 zfs_prop_to_name(pl
->pl_prop
));
1948 sourcetype
= ZPROP_SRC_NONE
;
1949 (void) strlcpy(buf
, "-", sizeof (buf
));
1952 if (received
&& (zfs_prop_get_recvd(zhp
,
1953 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1954 cbp
->cb_literal
) == 0))
1957 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1958 zfs_prop_to_name(pl
->pl_prop
),
1959 buf
, sourcetype
, source
, recvdval
);
1960 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1961 sourcetype
= ZPROP_SRC_LOCAL
;
1963 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1964 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1965 sourcetype
= ZPROP_SRC_NONE
;
1966 (void) strlcpy(buf
, "-", sizeof (buf
));
1969 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1970 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1971 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1972 sourcetype
= ZPROP_SRC_LOCAL
;
1974 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1975 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1976 sourcetype
= ZPROP_SRC_NONE
;
1977 (void) strlcpy(buf
, "-", sizeof (buf
));
1980 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1981 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1983 if (nvlist_lookup_nvlist(user_props
,
1984 pl
->pl_user_prop
, &propval
) != 0) {
1987 sourcetype
= ZPROP_SRC_NONE
;
1990 strval
= fnvlist_lookup_string(propval
,
1992 sourceval
= fnvlist_lookup_string(propval
,
1995 if (strcmp(sourceval
,
1996 zfs_get_name(zhp
)) == 0) {
1997 sourcetype
= ZPROP_SRC_LOCAL
;
1998 } else if (strcmp(sourceval
,
1999 ZPROP_SOURCE_VAL_RECVD
) == 0) {
2000 sourcetype
= ZPROP_SRC_RECEIVED
;
2002 sourcetype
= ZPROP_SRC_INHERITED
;
2003 (void) strlcpy(source
,
2004 sourceval
, sizeof (source
));
2008 if (received
&& (zfs_prop_get_recvd(zhp
,
2009 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
2010 cbp
->cb_literal
) == 0))
2013 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
2014 pl
->pl_user_prop
, strval
, sourcetype
,
2023 zfs_do_get(int argc
, char **argv
)
2025 zprop_get_cbdata_t cb
= { 0 };
2026 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2027 int types
= ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
;
2031 zprop_list_t fake_name
= { 0 };
2034 * Set up default columns and sources.
2036 cb
.cb_sources
= ZPROP_SRC_ALL
;
2037 cb
.cb_columns
[0] = GET_COL_NAME
;
2038 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
2039 cb
.cb_columns
[2] = GET_COL_VALUE
;
2040 cb
.cb_columns
[3] = GET_COL_SOURCE
;
2041 cb
.cb_type
= ZFS_TYPE_DATASET
;
2044 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
2047 cb
.cb_literal
= B_TRUE
;
2050 limit
= parse_depth(optarg
, &flags
);
2053 flags
|= ZFS_ITER_RECURSE
;
2056 cb
.cb_scripted
= B_TRUE
;
2059 (void) fprintf(stderr
, gettext("missing argument for "
2060 "'%c' option\n"), optopt
);
2065 * Process the set of columns to display. We zero out
2066 * the structure to give us a blank slate.
2068 memset(&cb
.cb_columns
, 0, sizeof (cb
.cb_columns
));
2071 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
2072 static const char *const col_subopts
[] =
2073 { "name", "property", "value",
2074 "received", "source", "all" };
2075 static const zfs_get_column_t col_subopt_col
[] =
2076 { GET_COL_NAME
, GET_COL_PROPERTY
, GET_COL_VALUE
,
2077 GET_COL_RECVD
, GET_COL_SOURCE
};
2078 static const int col_subopt_flags
[] =
2079 { 0, 0, 0, ZFS_ITER_RECVD_PROPS
, 0 };
2081 if (i
== ZFS_GET_NCOLS
) {
2082 (void) fprintf(stderr
, gettext("too "
2083 "many fields given to -o "
2088 for (c
= 0; c
< ARRAY_SIZE(col_subopts
); ++c
)
2089 if (strcmp(tok
, col_subopts
[c
]) == 0)
2092 (void) fprintf(stderr
,
2093 gettext("invalid column name '%s'\n"), tok
);
2099 (void) fprintf(stderr
,
2100 gettext("\"all\" conflicts "
2101 "with specific fields "
2102 "given to -o option\n"));
2106 memcpy(cb
.cb_columns
, col_subopt_col
,
2107 sizeof (col_subopt_col
));
2108 flags
|= ZFS_ITER_RECVD_PROPS
;
2111 cb
.cb_columns
[i
++] = col_subopt_col
[c
];
2112 flags
|= col_subopt_flags
[c
];
2120 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
2121 static const char *const source_opt
[] = {
2123 "inherited", "received",
2124 "temporary", "none" };
2125 static const int source_flg
[] = {
2126 ZPROP_SRC_LOCAL
, ZPROP_SRC_DEFAULT
,
2127 ZPROP_SRC_INHERITED
, ZPROP_SRC_RECEIVED
,
2128 ZPROP_SRC_TEMPORARY
, ZPROP_SRC_NONE
};
2130 for (i
= 0; i
< ARRAY_SIZE(source_opt
); ++i
)
2131 if (strcmp(tok
, source_opt
[i
]) == 0) {
2132 cb
.cb_sources
|= source_flg
[i
];
2136 (void) fprintf(stderr
,
2137 gettext("invalid source '%s'\n"), tok
);
2145 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
2147 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
2148 static const char *const type_opts
[] = {
2158 static const int type_types
[] = {
2159 ZFS_TYPE_FILESYSTEM
,
2160 ZFS_TYPE_FILESYSTEM
,
2166 ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
2169 for (i
= 0; i
< ARRAY_SIZE(type_opts
); ++i
)
2170 if (strcmp(tok
, type_opts
[i
]) == 0) {
2171 types
|= type_types
[i
];
2175 (void) fprintf(stderr
,
2176 gettext("invalid type '%s'\n"), tok
);
2183 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2193 (void) fprintf(stderr
, gettext("missing property "
2201 * Handle users who want to get all snapshots or bookmarks
2202 * of a dataset (ex. 'zfs get -t snapshot refer <dataset>').
2204 if ((types
== ZFS_TYPE_SNAPSHOT
|| types
== ZFS_TYPE_BOOKMARK
) &&
2205 argc
> 1 && (flags
& ZFS_ITER_RECURSE
) == 0 && limit
== 0) {
2206 flags
|= (ZFS_ITER_DEPTH_LIMIT
| ZFS_ITER_RECURSE
);
2210 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
2218 * As part of zfs_expand_proplist(), we keep track of the maximum column
2219 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
2220 * need to know the maximum name length. However, the user likely did
2221 * not specify 'name' as one of the properties to fetch, so we need to
2222 * make sure we always include at least this property for
2223 * print_get_headers() to work properly.
2225 if (cb
.cb_proplist
!= NULL
) {
2226 fake_name
.pl_prop
= ZFS_PROP_NAME
;
2227 fake_name
.pl_width
= strlen(gettext("NAME"));
2228 fake_name
.pl_next
= cb
.cb_proplist
;
2229 cb
.cb_proplist
= &fake_name
;
2232 cb
.cb_first
= B_TRUE
;
2234 /* run for each object */
2235 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
2236 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
2238 if (cb
.cb_proplist
== &fake_name
)
2239 zprop_free_list(fake_name
.pl_next
);
2241 zprop_free_list(cb
.cb_proplist
);
2247 * inherit [-rS] <property> <fs|vol> ...
2249 * -r Recurse over all children
2250 * -S Revert to received value, if any
2252 * For each dataset specified on the command line, inherit the given property
2253 * from its parent. Inheriting a property at the pool level will cause it to
2254 * use the default value. The '-r' flag will recurse over all children, and is
2255 * useful for setting a property on a hierarchy-wide basis, regardless of any
2256 * local modifications for each dataset.
2259 typedef struct inherit_cbdata
{
2260 const char *cb_propname
;
2261 boolean_t cb_received
;
2265 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
2267 inherit_cbdata_t
*cb
= data
;
2268 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
2271 * If we're doing it recursively, then ignore properties that
2272 * are not valid for this type of dataset.
2274 if (prop
!= ZPROP_INVAL
&&
2275 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
), B_FALSE
))
2278 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
2282 inherit_cb(zfs_handle_t
*zhp
, void *data
)
2284 inherit_cbdata_t
*cb
= data
;
2286 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
2290 zfs_do_inherit(int argc
, char **argv
)
2294 inherit_cbdata_t cb
= { 0 };
2298 boolean_t received
= B_FALSE
;
2301 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
2304 flags
|= ZFS_ITER_RECURSE
;
2311 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2320 /* check number of arguments */
2322 (void) fprintf(stderr
, gettext("missing property argument\n"));
2326 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
2334 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_USERPROP
) {
2335 if (zfs_prop_readonly(prop
)) {
2336 (void) fprintf(stderr
, gettext(
2337 "%s property is read-only\n"),
2341 if (!zfs_prop_inheritable(prop
) && !received
) {
2342 (void) fprintf(stderr
, gettext("'%s' property cannot "
2343 "be inherited\n"), propname
);
2344 if (prop
== ZFS_PROP_QUOTA
||
2345 prop
== ZFS_PROP_RESERVATION
||
2346 prop
== ZFS_PROP_REFQUOTA
||
2347 prop
== ZFS_PROP_REFRESERVATION
) {
2348 (void) fprintf(stderr
, gettext("use 'zfs set "
2349 "%s=none' to clear\n"), propname
);
2350 (void) fprintf(stderr
, gettext("use 'zfs "
2351 "inherit -S %s' to revert to received "
2352 "value\n"), propname
);
2356 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
2357 prop
== ZFS_PROP_VERSION
)) {
2358 (void) fprintf(stderr
, gettext("'%s' property cannot "
2359 "be reverted to a received value\n"), propname
);
2362 } else if (!zfs_prop_user(propname
)) {
2363 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
2368 cb
.cb_propname
= propname
;
2369 cb
.cb_received
= received
;
2371 if (flags
& ZFS_ITER_RECURSE
) {
2372 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
2373 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
2375 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
2376 NULL
, NULL
, 0, inherit_cb
, &cb
);
2382 typedef struct upgrade_cbdata
{
2383 uint64_t cb_numupgraded
;
2384 uint64_t cb_numsamegraded
;
2385 uint64_t cb_numfailed
;
2386 uint64_t cb_version
;
2388 boolean_t cb_foundone
;
2389 char cb_lastfs
[ZFS_MAX_DATASET_NAME_LEN
];
2393 same_pool(zfs_handle_t
*zhp
, const char *name
)
2395 int len1
= strcspn(name
, "/@");
2396 const char *zhname
= zfs_get_name(zhp
);
2397 int len2
= strcspn(zhname
, "/@");
2401 return (strncmp(name
, zhname
, len1
) == 0);
2405 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
2407 upgrade_cbdata_t
*cb
= data
;
2408 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2410 /* list if it's old/new */
2411 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
2412 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
2415 str
= gettext("The following filesystems are "
2416 "formatted using a newer software version and\n"
2417 "cannot be accessed on the current system.\n\n");
2419 str
= gettext("The following filesystems are "
2420 "out of date, and can be upgraded. After being\n"
2421 "upgraded, these filesystems (and any 'zfs send' "
2422 "streams generated from\n"
2423 "subsequent snapshots) will no longer be "
2424 "accessible by older software versions.\n\n");
2427 if (!cb
->cb_foundone
) {
2429 (void) printf(gettext("VER FILESYSTEM\n"));
2430 (void) printf(gettext("--- ------------\n"));
2431 cb
->cb_foundone
= B_TRUE
;
2434 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
2441 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2443 upgrade_cbdata_t
*cb
= data
;
2444 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2445 int needed_spa_version
;
2448 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2451 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2453 if (needed_spa_version
< 0)
2456 if (spa_version
< needed_spa_version
) {
2458 (void) printf(gettext("%s: can not be "
2459 "upgraded; the pool version needs to first "
2460 "be upgraded\nto version %d\n\n"),
2461 zfs_get_name(zhp
), needed_spa_version
);
2467 if (version
< cb
->cb_version
) {
2469 (void) snprintf(verstr
, sizeof (verstr
),
2470 "%llu", (u_longlong_t
)cb
->cb_version
);
2471 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2473 * If they did "zfs upgrade -a", then we could
2474 * be doing ioctls to different pools. We need
2475 * to log this history once to each pool, and bypass
2476 * the normal history logging that happens in main().
2478 (void) zpool_log_history(g_zfs
, history_str
);
2479 log_history
= B_FALSE
;
2481 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2482 cb
->cb_numupgraded
++;
2485 (void) strlcpy(cb
->cb_lastfs
, zfs_get_name(zhp
),
2486 sizeof (cb
->cb_lastfs
));
2487 } else if (version
> cb
->cb_version
) {
2488 /* can't downgrade */
2489 (void) printf(gettext("%s: can not be downgraded; "
2490 "it is already at version %u\n"),
2491 zfs_get_name(zhp
), version
);
2494 cb
->cb_numsamegraded
++;
2502 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2505 zfs_do_upgrade(int argc
, char **argv
)
2507 boolean_t all
= B_FALSE
;
2508 boolean_t showversions
= B_FALSE
;
2510 upgrade_cbdata_t cb
= { 0 };
2512 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2515 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2518 flags
|= ZFS_ITER_RECURSE
;
2521 showversions
= B_TRUE
;
2524 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2525 optarg
, &cb
.cb_version
) != 0) {
2526 (void) fprintf(stderr
,
2527 gettext("invalid version %s\n"), optarg
);
2536 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2545 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2547 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2548 cb
.cb_version
|| argc
))
2550 if ((all
|| argc
) && (showversions
))
2556 /* Show info on available versions. */
2557 (void) printf(gettext("The following filesystem versions are "
2559 (void) printf(gettext("VER DESCRIPTION\n"));
2560 (void) printf("--- -----------------------------------------"
2561 "---------------\n");
2562 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2563 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2564 (void) printf(gettext(" 3 Case insensitive and filesystem "
2565 "user identifier (FUID)\n"));
2566 (void) printf(gettext(" 4 userquota, groupquota "
2568 (void) printf(gettext(" 5 System attributes\n"));
2569 (void) printf(gettext("\nFor more information on a particular "
2570 "version, including supported releases,\n"));
2571 (void) printf("see the ZFS Administration Guide.\n\n");
2573 } else if (argc
|| all
) {
2574 /* Upgrade filesystems */
2575 if (cb
.cb_version
== 0)
2576 cb
.cb_version
= ZPL_VERSION
;
2577 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2578 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2579 (void) printf(gettext("%llu filesystems upgraded\n"),
2580 (u_longlong_t
)cb
.cb_numupgraded
);
2581 if (cb
.cb_numsamegraded
) {
2582 (void) printf(gettext("%llu filesystems already at "
2584 (u_longlong_t
)cb
.cb_numsamegraded
);
2586 if (cb
.cb_numfailed
!= 0)
2589 /* List old-version filesystems */
2591 (void) printf(gettext("This system is currently running "
2592 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2594 flags
|= ZFS_ITER_RECURSE
;
2595 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2596 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2598 found
= cb
.cb_foundone
;
2599 cb
.cb_foundone
= B_FALSE
;
2600 cb
.cb_newer
= B_TRUE
;
2602 ret
|= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2603 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2605 if (!cb
.cb_foundone
&& !found
) {
2606 (void) printf(gettext("All filesystems are "
2607 "formatted with the current version.\n"));
2615 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2616 * [-S field [-S field]...] [-t type[,...]]
2617 * filesystem | snapshot | path
2618 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2619 * [-S field [-S field]...] [-t type[,...]]
2620 * filesystem | snapshot | path
2621 * zfs projectspace [-Hp] [-o field[,...]] [-s field [-s field]...]
2622 * [-S field [-S field]...] filesystem | snapshot | path
2624 * -H Scripted mode; elide headers and separate columns by tabs.
2625 * -i Translate SID to POSIX ID.
2626 * -n Print numeric ID instead of user/group name.
2627 * -o Control which fields to display.
2628 * -p Use exact (parsable) numeric output.
2629 * -s Specify sort columns, descending order.
2630 * -S Specify sort columns, ascending order.
2631 * -t Control which object types to display.
2633 * Displays space consumed by, and quotas on, each user in the specified
2634 * filesystem or snapshot.
2637 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2638 enum us_field_types
{
2646 static const char *const us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA",
2647 "OBJUSED", "OBJQUOTA" };
2648 static const char *const us_field_names
[] = { "type", "name", "used", "quota",
2649 "objused", "objquota" };
2650 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2652 #define USTYPE_PSX_GRP (1 << 0)
2653 #define USTYPE_PSX_USR (1 << 1)
2654 #define USTYPE_SMB_GRP (1 << 2)
2655 #define USTYPE_SMB_USR (1 << 3)
2656 #define USTYPE_PROJ (1 << 4)
2657 #define USTYPE_ALL \
2658 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR | \
2661 static int us_type_bits
[] = {
2668 static const char *const us_type_names
[] = { "posixgroup", "posixuser",
2669 "smbgroup", "smbuser", "all" };
2671 typedef struct us_node
{
2673 uu_avl_node_t usn_avlnode
;
2674 uu_list_node_t usn_listnode
;
2677 typedef struct us_cbdata
{
2679 uu_avl_pool_t
*cb_avl_pool
;
2681 boolean_t cb_numname
;
2682 boolean_t cb_nicenum
;
2683 boolean_t cb_sid2posix
;
2684 zfs_userquota_prop_t cb_prop
;
2685 zfs_sort_column_t
*cb_sortcol
;
2686 size_t cb_width
[USFIELD_LAST
];
2689 static boolean_t us_populated
= B_FALSE
;
2692 zfs_sort_column_t
*si_sortcol
;
2693 boolean_t si_numname
;
2697 us_field_index(const char *field
)
2699 for (int i
= 0; i
< USFIELD_LAST
; i
++) {
2700 if (strcmp(field
, us_field_names
[i
]) == 0)
2708 us_compare(const void *larg
, const void *rarg
, void *unused
)
2710 const us_node_t
*l
= larg
;
2711 const us_node_t
*r
= rarg
;
2712 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2713 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2714 boolean_t numname
= si
->si_numname
;
2715 nvlist_t
*lnvl
= l
->usn_nvl
;
2716 nvlist_t
*rnvl
= r
->usn_nvl
;
2720 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2721 const char *lvstr
= "";
2722 const char *rvstr
= "";
2727 zfs_prop_t prop
= sortcol
->sc_prop
;
2728 const char *propname
= NULL
;
2729 boolean_t reverse
= sortcol
->sc_reverse
;
2734 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2735 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2737 rc
= (rv32
< lv32
) ? 1 : -1;
2743 (void) nvlist_lookup_uint64(lnvl
, propname
,
2745 (void) nvlist_lookup_uint64(rnvl
, propname
,
2748 rc
= (rv64
< lv64
) ? 1 : -1;
2750 if ((nvlist_lookup_string(lnvl
, propname
,
2751 &lvstr
) == ENOENT
) ||
2752 (nvlist_lookup_string(rnvl
, propname
,
2753 &rvstr
) == ENOENT
)) {
2756 rc
= strcmp(lvstr
, rvstr
);
2760 case ZFS_PROP_QUOTA
:
2763 if (prop
== ZFS_PROP_USED
)
2767 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2768 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2770 rc
= (rv64
< lv64
) ? 1 : -1;
2779 return (reverse
? 1 : -1);
2781 return (reverse
? -1 : 1);
2786 * If entries still seem to be the same, check if they are of the same
2787 * type (smbentity is added only if we are doing SID to POSIX ID
2788 * translation where we can have duplicate type/name combinations).
2790 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2791 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2793 return (lvb
< rvb
? -1 : 1);
2799 zfs_prop_is_user(unsigned p
)
2801 return (p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
||
2802 p
== ZFS_PROP_USEROBJUSED
|| p
== ZFS_PROP_USEROBJQUOTA
);
2806 zfs_prop_is_group(unsigned p
)
2808 return (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
||
2809 p
== ZFS_PROP_GROUPOBJUSED
|| p
== ZFS_PROP_GROUPOBJQUOTA
);
2813 zfs_prop_is_project(unsigned p
)
2815 return (p
== ZFS_PROP_PROJECTUSED
|| p
== ZFS_PROP_PROJECTQUOTA
||
2816 p
== ZFS_PROP_PROJECTOBJUSED
|| p
== ZFS_PROP_PROJECTOBJQUOTA
);
2819 static inline const char *
2820 us_type2str(unsigned field_type
)
2822 switch (field_type
) {
2823 case USTYPE_PSX_USR
:
2824 return ("POSIX User");
2825 case USTYPE_PSX_GRP
:
2826 return ("POSIX Group");
2827 case USTYPE_SMB_USR
:
2828 return ("SMB User");
2829 case USTYPE_SMB_GRP
:
2830 return ("SMB Group");
2834 return ("Undefined");
2839 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2841 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2842 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2844 const char *propname
;
2847 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2848 uu_avl_t
*avl
= cb
->cb_avl
;
2852 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2854 const char *typestr
;
2858 int typeidx
, nameidx
, sizeidx
;
2859 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2860 boolean_t smbentity
= B_FALSE
;
2862 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2864 node
= safe_malloc(sizeof (us_node_t
));
2865 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2866 node
->usn_nvl
= props
;
2868 if (domain
!= NULL
&& domain
[0] != '\0') {
2871 char sid
[MAXNAMELEN
+ 32];
2875 directory_error_t e
;
2879 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2881 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2882 type
= USTYPE_SMB_GRP
;
2883 err
= sid_to_id(sid
, B_FALSE
, &id
);
2885 type
= USTYPE_SMB_USR
;
2886 err
= sid_to_id(sid
, B_TRUE
, &id
);
2891 if (!cb
->cb_sid2posix
) {
2892 e
= directory_name_from_sid(NULL
, sid
, &name
,
2895 directory_error_free(e
);
2905 #endif /* HAVE_IDMAP */
2908 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2910 if (zfs_prop_is_group(prop
)) {
2911 type
= USTYPE_PSX_GRP
;
2912 if (!cb
->cb_numname
) {
2915 if ((g
= getgrgid(rid
)) != NULL
)
2918 } else if (zfs_prop_is_user(prop
)) {
2919 type
= USTYPE_PSX_USR
;
2920 if (!cb
->cb_numname
) {
2923 if ((p
= getpwuid(rid
)) != NULL
)
2932 * Make sure that the type/name combination is unique when doing
2933 * SID to POSIX ID translation (hence changing the type from SMB to
2936 if (cb
->cb_sid2posix
&&
2937 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2940 /* Calculate/update width of TYPE field */
2941 typestr
= us_type2str(type
);
2942 typelen
= strlen(gettext(typestr
));
2943 typeidx
= us_field_index("type");
2944 if (typelen
> cb
->cb_width
[typeidx
])
2945 cb
->cb_width
[typeidx
] = typelen
;
2946 if (nvlist_add_uint32(props
, "type", type
) != 0)
2949 /* Calculate/update width of NAME field */
2950 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2951 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2953 namelen
= snprintf(NULL
, 0, "%u", rid
);
2955 if (nvlist_add_string(props
, "name", name
) != 0)
2957 namelen
= strlen(name
);
2959 nameidx
= us_field_index("name");
2960 if (nameidx
>= 0 && namelen
> cb
->cb_width
[nameidx
])
2961 cb
->cb_width
[nameidx
] = namelen
;
2964 * Check if this type/name combination is in the list and update it;
2965 * otherwise add new node to the list.
2967 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2968 uu_avl_insert(avl
, node
, idx
);
2973 props
= node
->usn_nvl
;
2976 /* Calculate/update width of USED/QUOTA fields */
2977 if (cb
->cb_nicenum
) {
2978 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
||
2979 prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
||
2980 prop
== ZFS_PROP_PROJECTUSED
||
2981 prop
== ZFS_PROP_PROJECTQUOTA
) {
2982 zfs_nicebytes(space
, sizebuf
, sizeof (sizebuf
));
2984 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2987 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu",
2988 (u_longlong_t
)space
);
2990 sizelen
= strlen(sizebuf
);
2991 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
||
2992 prop
== ZFS_PROP_PROJECTUSED
) {
2994 if (!nvlist_exists(props
, "quota"))
2995 (void) nvlist_add_uint64(props
, "quota", 0);
2996 } else if (prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
||
2997 prop
== ZFS_PROP_PROJECTQUOTA
) {
2999 if (!nvlist_exists(props
, "used"))
3000 (void) nvlist_add_uint64(props
, "used", 0);
3001 } else if (prop
== ZFS_PROP_USEROBJUSED
||
3002 prop
== ZFS_PROP_GROUPOBJUSED
|| prop
== ZFS_PROP_PROJECTOBJUSED
) {
3003 propname
= "objused";
3004 if (!nvlist_exists(props
, "objquota"))
3005 (void) nvlist_add_uint64(props
, "objquota", 0);
3006 } else if (prop
== ZFS_PROP_USEROBJQUOTA
||
3007 prop
== ZFS_PROP_GROUPOBJQUOTA
||
3008 prop
== ZFS_PROP_PROJECTOBJQUOTA
) {
3009 propname
= "objquota";
3010 if (!nvlist_exists(props
, "objused"))
3011 (void) nvlist_add_uint64(props
, "objused", 0);
3015 sizeidx
= us_field_index(propname
);
3016 if (sizeidx
>= 0 && sizelen
> cb
->cb_width
[sizeidx
])
3017 cb
->cb_width
[sizeidx
] = sizelen
;
3019 if (nvlist_add_uint64(props
, propname
, space
) != 0)
3026 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
3027 size_t *width
, us_node_t
*node
)
3029 nvlist_t
*nvl
= node
->usn_nvl
;
3030 char valstr
[MAXNAMELEN
];
3031 boolean_t first
= B_TRUE
;
3037 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
3038 if (!(ustype
& types
))
3041 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
3042 nvpair_t
*nvp
= NULL
;
3044 uint32_t val32
= -1;
3045 uint64_t val64
= -1;
3046 const char *strval
= "-";
3048 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
)
3049 if (strcmp(nvpair_name(nvp
),
3050 us_field_names
[field
]) == 0)
3053 type
= nvp
== NULL
? DATA_TYPE_UNKNOWN
: nvpair_type(nvp
);
3055 case DATA_TYPE_UINT32
:
3056 val32
= fnvpair_value_uint32(nvp
);
3058 case DATA_TYPE_UINT64
:
3059 val64
= fnvpair_value_uint64(nvp
);
3061 case DATA_TYPE_STRING
:
3062 strval
= fnvpair_value_string(nvp
);
3064 case DATA_TYPE_UNKNOWN
:
3067 (void) fprintf(stderr
, "invalid data type\n");
3072 if (type
== DATA_TYPE_UINT32
)
3073 strval
= us_type2str(val32
);
3076 if (type
== DATA_TYPE_UINT64
) {
3077 (void) sprintf(valstr
, "%llu",
3078 (u_longlong_t
)val64
);
3084 if (type
== DATA_TYPE_UINT64
) {
3086 (void) sprintf(valstr
, "%llu",
3087 (u_longlong_t
)val64
);
3089 } else if (field
== USFIELD_QUOTA
&&
3093 zfs_nicebytes(val64
, valstr
,
3099 case USFIELD_OBJUSED
:
3100 case USFIELD_OBJQUOTA
:
3101 if (type
== DATA_TYPE_UINT64
) {
3103 (void) sprintf(valstr
, "%llu",
3104 (u_longlong_t
)val64
);
3106 } else if (field
== USFIELD_OBJQUOTA
&&
3110 zfs_nicenum(val64
, valstr
,
3120 (void) putchar('\t');
3122 (void) fputs(" ", stdout
);
3125 (void) fputs(strval
, stdout
);
3126 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
3127 (void) printf("%-*s", (int)width
[field
], strval
);
3129 (void) printf("%*s", (int)width
[field
], strval
);
3135 (void) putchar('\n');
3139 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
3140 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
3148 boolean_t first
= B_TRUE
;
3150 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
3151 col
= gettext(us_field_hdr
[field
]);
3152 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
3153 (void) printf(first
? "%-*s" : " %-*s",
3154 (int)width
[field
], col
);
3156 (void) printf(first
? "%*s" : " %*s",
3157 (int)width
[field
], col
);
3162 (void) printf("\n");
3165 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
3166 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
3168 nvlist_free(node
->usn_nvl
);
3173 zfs_do_userspace(int argc
, char **argv
)
3176 zfs_userquota_prop_t p
;
3177 uu_avl_pool_t
*avl_pool
;
3179 uu_avl_walk_t
*walk
;
3181 char deffields
[] = "type,name,used,quota,objused,objquota";
3182 char *ofield
= NULL
;
3183 char *tfield
= NULL
;
3187 boolean_t scripted
= B_FALSE
;
3188 boolean_t prtnum
= B_FALSE
;
3189 boolean_t parsable
= B_FALSE
;
3190 boolean_t sid2posix
= B_FALSE
;
3193 zfs_sort_column_t
*sortcol
= NULL
;
3194 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
3198 uu_list_pool_t
*listpool
;
3200 uu_avl_index_t idx
= 0;
3201 uu_list_index_t idx2
= 0;
3206 if (strcmp(argv
[0], "groupspace") == 0) {
3207 /* Toggle default group types */
3208 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
3209 } else if (strcmp(argv
[0], "projectspace") == 0) {
3210 types
= USTYPE_PROJ
;
3214 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
3217 if (types
== USTYPE_PROJ
) {
3218 (void) fprintf(stderr
,
3219 gettext("invalid option 'n'\n"));
3235 if (zfs_add_sort_column(&sortcol
, optarg
,
3236 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
3237 (void) fprintf(stderr
,
3238 gettext("invalid field '%s'\n"), optarg
);
3243 if (types
== USTYPE_PROJ
) {
3244 (void) fprintf(stderr
,
3245 gettext("invalid option 't'\n"));
3251 if (types
== USTYPE_PROJ
) {
3252 (void) fprintf(stderr
,
3253 gettext("invalid option 'i'\n"));
3259 (void) fprintf(stderr
, gettext("missing argument for "
3260 "'%c' option\n"), optopt
);
3264 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3274 (void) fprintf(stderr
, gettext("missing dataset name\n"));
3278 (void) fprintf(stderr
, gettext("too many arguments\n"));
3282 /* Use default output fields if not specified using -o */
3286 if ((delim
= strchr(ofield
, ',')) != NULL
)
3288 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
3289 (void) fprintf(stderr
, gettext("invalid type '%s' "
3290 "for -o option\n"), ofield
);
3295 } while (delim
!= NULL
);
3296 fields
[cfield
] = USFIELD_LAST
;
3298 /* Override output types (-t option) */
3299 if (tfield
!= NULL
) {
3303 boolean_t found
= B_FALSE
;
3305 if ((delim
= strchr(tfield
, ',')) != NULL
)
3307 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
3309 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
3311 types
|= us_type_bits
[i
];
3316 (void) fprintf(stderr
, gettext("invalid type "
3317 "'%s' for -t option\n"), tfield
);
3322 } while (delim
!= NULL
);
3325 if ((zhp
= zfs_path_to_zhandle(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
|
3326 ZFS_TYPE_SNAPSHOT
)) == NULL
)
3328 if (zfs_get_underlying_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3329 (void) fprintf(stderr
, gettext("operation is only applicable "
3330 "to filesystems and their snapshots\n"));
3335 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
3336 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
3338 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
3341 /* Always add default sorting columns */
3342 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
3343 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
3345 cb
.cb_sortcol
= sortcol
;
3346 cb
.cb_numname
= prtnum
;
3347 cb
.cb_nicenum
= !parsable
;
3348 cb
.cb_avl_pool
= avl_pool
;
3349 cb
.cb_avl
= avl_tree
;
3350 cb
.cb_sid2posix
= sid2posix
;
3352 for (i
= 0; i
< USFIELD_LAST
; i
++)
3353 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
3355 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
3356 if ((zfs_prop_is_user(p
) &&
3357 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
3358 (zfs_prop_is_group(p
) &&
3359 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))) ||
3360 (zfs_prop_is_project(p
) && types
!= USTYPE_PROJ
))
3364 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0) {
3372 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
3375 us_populated
= B_TRUE
;
3377 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
3378 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
3379 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
3380 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
3382 while (node
!= NULL
) {
3384 node
= uu_avl_next(avl_tree
, node
);
3385 uu_avl_remove(avl_tree
, rmnode
);
3386 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
3387 uu_list_insert(list
, rmnode
, idx2
);
3390 for (node
= uu_list_first(list
); node
!= NULL
;
3391 node
= uu_list_next(list
, node
)) {
3392 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
3394 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
3395 uu_avl_insert(avl_tree
, node
, idx
);
3398 uu_list_destroy(list
);
3399 uu_list_pool_destroy(listpool
);
3401 /* Print and free node nvlist memory */
3402 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
3405 zfs_free_sort_columns(sortcol
);
3407 /* Clean up the AVL tree */
3408 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
3411 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
3412 uu_avl_remove(cb
.cb_avl
, node
);
3416 uu_avl_walk_end(walk
);
3417 uu_avl_destroy(avl_tree
);
3418 uu_avl_pool_destroy(avl_pool
);
3424 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
3425 * [-t type[,...]] [filesystem|volume|snapshot] ...
3427 * -H Scripted mode; elide headers and separate columns by tabs
3428 * -p Display values in parsable (literal) format.
3429 * -r Recurse over all children
3430 * -d Limit recursion by depth.
3431 * -o Control which fields to display.
3432 * -s Specify sort columns, descending order.
3433 * -S Specify sort columns, ascending order.
3434 * -t Control which object types to display.
3436 * When given no arguments, list all filesystems in the system.
3437 * Otherwise, list the specified datasets, optionally recursing down them if
3438 * '-r' is specified.
3440 typedef struct list_cbdata
{
3442 boolean_t cb_literal
;
3443 boolean_t cb_scripted
;
3444 zprop_list_t
*cb_proplist
;
3448 * Given a list of columns to display, output appropriate headers for each one.
3451 print_header(list_cbdata_t
*cb
)
3453 zprop_list_t
*pl
= cb
->cb_proplist
;
3454 char headerbuf
[ZFS_MAXPROPLEN
];
3457 boolean_t first
= B_TRUE
;
3458 boolean_t right_justify
;
3460 color_start(ANSI_BOLD
);
3462 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3469 right_justify
= B_FALSE
;
3470 if (pl
->pl_prop
!= ZPROP_USERPROP
) {
3471 header
= zfs_prop_column_name(pl
->pl_prop
);
3472 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3474 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
3475 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
3476 headerbuf
[i
] = '\0';
3480 if (pl
->pl_next
== NULL
&& !right_justify
)
3481 (void) printf("%s", header
);
3482 else if (right_justify
)
3483 (void) printf("%*s", (int)pl
->pl_width
, header
);
3485 (void) printf("%-*s", (int)pl
->pl_width
, header
);
3490 (void) printf("\n");
3494 * Decides on the color that the avail value should be printed in.
3495 * > 80% used = yellow
3499 zfs_list_avail_color(zfs_handle_t
*zhp
)
3501 uint64_t used
= zfs_prop_get_int(zhp
, ZFS_PROP_USED
);
3502 uint64_t avail
= zfs_prop_get_int(zhp
, ZFS_PROP_AVAILABLE
);
3503 int percentage
= (int)((double)avail
/ MAX(avail
+ used
, 1) * 100);
3505 if (percentage
> 20)
3507 else if (percentage
> 10)
3508 return (ANSI_YELLOW
);
3514 * Given a dataset and a list of fields, print out all the properties according
3515 * to the described layout.
3518 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
3520 zprop_list_t
*pl
= cb
->cb_proplist
;
3521 boolean_t first
= B_TRUE
;
3522 char property
[ZFS_MAXPROPLEN
];
3523 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
3525 const char *propstr
;
3526 boolean_t right_justify
;
3528 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3530 if (cb
->cb_scripted
)
3531 (void) putchar('\t');
3533 (void) fputs(" ", stdout
);
3538 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
3539 (void) strlcpy(property
, zfs_get_name(zhp
),
3542 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3543 } else if (pl
->pl_prop
!= ZPROP_USERPROP
) {
3544 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
3545 sizeof (property
), NULL
, NULL
, 0,
3546 cb
->cb_literal
) != 0)
3550 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3551 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
3552 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
3553 property
, sizeof (property
), cb
->cb_literal
) != 0)
3557 right_justify
= B_TRUE
;
3558 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
3559 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
3560 property
, sizeof (property
), cb
->cb_literal
) != 0)
3564 right_justify
= B_TRUE
;
3566 if (nvlist_lookup_nvlist(userprops
,
3567 pl
->pl_user_prop
, &propval
) != 0)
3570 propstr
= fnvlist_lookup_string(propval
,
3572 right_justify
= B_FALSE
;
3576 * zfs_list_avail_color() needs ZFS_PROP_AVAILABLE + USED
3577 * - so we need another for() search for the USED part
3578 * - when no colors wanted, we can skip the whole thing
3580 if (use_color() && pl
->pl_prop
== ZFS_PROP_AVAILABLE
) {
3581 zprop_list_t
*pl2
= cb
->cb_proplist
;
3582 for (; pl2
!= NULL
; pl2
= pl2
->pl_next
) {
3583 if (pl2
->pl_prop
== ZFS_PROP_USED
) {
3584 color_start(zfs_list_avail_color(zhp
));
3585 /* found it, no need for more loops */
3592 * If this is being called in scripted mode, or if this is the
3593 * last column and it is left-justified, don't include a width
3596 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3597 (void) fputs(propstr
, stdout
);
3598 else if (right_justify
)
3599 (void) printf("%*s", (int)pl
->pl_width
, propstr
);
3601 (void) printf("%-*s", (int)pl
->pl_width
, propstr
);
3603 if (pl
->pl_prop
== ZFS_PROP_AVAILABLE
)
3607 (void) putchar('\n');
3611 * Generic callback function to list a dataset or snapshot.
3614 list_callback(zfs_handle_t
*zhp
, void *data
)
3616 list_cbdata_t
*cbp
= data
;
3618 if (cbp
->cb_first
) {
3619 if (!cbp
->cb_scripted
)
3621 cbp
->cb_first
= B_FALSE
;
3624 print_dataset(zhp
, cbp
);
3630 zfs_do_list(int argc
, char **argv
)
3633 char default_fields
[] =
3634 "name,used,available,referenced,mountpoint";
3635 int types
= ZFS_TYPE_DATASET
;
3636 boolean_t types_specified
= B_FALSE
;
3637 char *fields
= default_fields
;
3638 list_cbdata_t cb
= { 0 };
3641 zfs_sort_column_t
*sortcol
= NULL
;
3642 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3645 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3651 cb
.cb_literal
= B_TRUE
;
3652 flags
|= ZFS_ITER_LITERAL_PROPS
;
3655 limit
= parse_depth(optarg
, &flags
);
3658 flags
|= ZFS_ITER_RECURSE
;
3661 cb
.cb_scripted
= B_TRUE
;
3664 if (zfs_add_sort_column(&sortcol
, optarg
,
3666 (void) fprintf(stderr
,
3667 gettext("invalid property '%s'\n"), optarg
);
3672 if (zfs_add_sort_column(&sortcol
, optarg
,
3674 (void) fprintf(stderr
,
3675 gettext("invalid property '%s'\n"), optarg
);
3681 types_specified
= B_TRUE
;
3682 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3684 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
3685 static const char *const type_subopts
[] = {
3695 static const int type_types
[] = {
3696 ZFS_TYPE_FILESYSTEM
,
3697 ZFS_TYPE_FILESYSTEM
,
3703 ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
3706 for (c
= 0; c
< ARRAY_SIZE(type_subopts
); ++c
)
3707 if (strcmp(tok
, type_subopts
[c
]) == 0) {
3708 types
|= type_types
[c
];
3712 (void) fprintf(stderr
,
3713 gettext("invalid type '%s'\n"), tok
);
3719 (void) fprintf(stderr
, gettext("missing argument for "
3720 "'%c' option\n"), optopt
);
3724 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3734 * If "-o space" and no types were specified, don't display snapshots.
3736 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3737 types
&= ~ZFS_TYPE_SNAPSHOT
;
3740 * Handle users who want to list all snapshots or bookmarks
3741 * of the current dataset (ex. 'zfs list -t snapshot <dataset>').
3743 if ((types
== ZFS_TYPE_SNAPSHOT
|| types
== ZFS_TYPE_BOOKMARK
) &&
3744 argc
> 0 && (flags
& ZFS_ITER_RECURSE
) == 0 && limit
== 0) {
3745 flags
|= (ZFS_ITER_DEPTH_LIMIT
| ZFS_ITER_RECURSE
);
3750 * If the user specifies '-o all', the zprop_get_list() doesn't
3751 * normally include the name of the dataset. For 'zfs list', we always
3752 * want this property to be first.
3754 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3758 cb
.cb_first
= B_TRUE
;
3761 * If we are only going to list and sort by properties that are "fast"
3762 * then we can use "simple" mode and avoid populating the properties
3765 if (zfs_list_only_by_fast(cb
.cb_proplist
) &&
3766 zfs_sort_only_by_fast(sortcol
))
3767 flags
|= ZFS_ITER_SIMPLE
;
3769 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3770 limit
, list_callback
, &cb
);
3772 zprop_free_list(cb
.cb_proplist
);
3773 zfs_free_sort_columns(sortcol
);
3775 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3776 (void) fprintf(stderr
, gettext("no datasets available\n"));
3782 * zfs rename [-fu] <fs | snap | vol> <fs | snap | vol>
3783 * zfs rename [-f] -p <fs | vol> <fs | vol>
3784 * zfs rename [-u] -r <snap> <snap>
3786 * Renames the given dataset to another of the same type.
3788 * The '-p' flag creates all the non-existing ancestors of the target first.
3789 * The '-u' flag prevents file systems from being remounted during rename.
3792 zfs_do_rename(int argc
, char **argv
)
3795 renameflags_t flags
= { 0 };
3799 boolean_t parents
= B_FALSE
;
3802 while ((c
= getopt(argc
, argv
, "pruf")) != -1) {
3808 flags
.recursive
= B_TRUE
;
3811 flags
.nounmount
= B_TRUE
;
3814 flags
.forceunmount
= B_TRUE
;
3818 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3827 /* check number of arguments */
3829 (void) fprintf(stderr
, gettext("missing source dataset "
3834 (void) fprintf(stderr
, gettext("missing target dataset "
3839 (void) fprintf(stderr
, gettext("too many arguments\n"));
3843 if (flags
.recursive
&& parents
) {
3844 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3849 if (flags
.nounmount
&& parents
) {
3850 (void) fprintf(stderr
, gettext("-u and -p options are mutually "
3855 if (flags
.recursive
&& strchr(argv
[0], '@') == 0) {
3856 (void) fprintf(stderr
, gettext("source dataset for recursive "
3857 "rename must be a snapshot\n"));
3861 if (flags
.nounmount
)
3862 types
= ZFS_TYPE_FILESYSTEM
;
3864 types
= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
3866 types
= ZFS_TYPE_DATASET
;
3868 if ((zhp
= zfs_open(g_zfs
, argv
[0], types
)) == NULL
)
3871 /* If we were asked and the name looks good, try to create ancestors. */
3872 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3873 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3878 ret
= (zfs_rename(zhp
, argv
[1], flags
) != 0);
3887 * Promotes the given clone fs to be the parent
3890 zfs_do_promote(int argc
, char **argv
)
3896 if (argc
> 1 && argv
[1][0] == '-') {
3897 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3902 /* check number of arguments */
3904 (void) fprintf(stderr
, gettext("missing clone filesystem"
3909 (void) fprintf(stderr
, gettext("too many arguments\n"));
3913 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3917 ret
= (zfs_promote(zhp
) != 0);
3925 zfs_do_redact(int argc
, char **argv
)
3928 char *bookname
= NULL
;
3929 char **rsnaps
= NULL
;
3934 (void) fprintf(stderr
, gettext("too few arguments\n"));
3941 numrsnaps
= argc
- 2;
3943 nvlist_t
*rsnapnv
= fnvlist_alloc();
3945 for (int i
= 0; i
< numrsnaps
; i
++) {
3946 fnvlist_add_boolean(rsnapnv
, rsnaps
[i
]);
3949 int err
= lzc_redact(snap
, bookname
, rsnapnv
);
3950 fnvlist_free(rsnapnv
);
3956 zfs_handle_t
*zhp
= zfs_open(g_zfs
, snap
, ZFS_TYPE_SNAPSHOT
);
3958 (void) fprintf(stderr
, gettext("provided snapshot %s "
3959 "does not exist\n"), snap
);
3963 for (int i
= 0; i
< numrsnaps
; i
++) {
3964 zhp
= zfs_open(g_zfs
, rsnaps
[i
], ZFS_TYPE_SNAPSHOT
);
3966 (void) fprintf(stderr
, gettext("provided "
3967 "snapshot %s does not exist\n"), rsnaps
[i
]);
3975 (void) fprintf(stderr
, gettext("specified redaction bookmark "
3976 "(%s) provided already exists\n"), bookname
);
3979 (void) fprintf(stderr
, gettext("provided bookmark name cannot "
3980 "be used, final name would be too long\n"));
3983 (void) fprintf(stderr
, gettext("too many redaction snapshots "
3987 if (strchr(bookname
, '#') != NULL
)
3988 (void) fprintf(stderr
, gettext(
3989 "redaction bookmark name must not contain '#'\n"));
3991 (void) fprintf(stderr
, gettext(
3992 "redaction snapshot must be descendent of "
3993 "snapshot being redacted\n"));
3996 (void) fprintf(stderr
, gettext("attempted to redact redacted "
3997 "dataset or with respect to redacted dataset\n"));
4000 (void) fprintf(stderr
, gettext("redaction bookmarks feature "
4004 (void) fprintf(stderr
, gettext("potentially invalid redaction "
4005 "snapshot; full dataset names required\n"));
4008 (void) fprintf(stderr
, gettext("attempted to resume redaction "
4009 " with a mismatched redaction list\n"));
4012 (void) fprintf(stderr
, gettext("internal error: %s\n"),
4020 * zfs rollback [-rRf] <snapshot>
4022 * -r Delete any intervening snapshots before doing rollback
4023 * -R Delete any snapshots and their clones
4024 * -f ignored for backwards compatibility
4026 * Given a filesystem, rollback to a specific snapshot, discarding any changes
4027 * since then and making it the active dataset. If more recent snapshots exist,
4028 * the command will complain unless the '-r' flag is given.
4030 typedef struct rollback_cbdata
{
4032 uint8_t cb_younger_ds_printed
;
4037 boolean_t cb_recurse
;
4038 } rollback_cbdata_t
;
4041 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
4043 rollback_cbdata_t
*cbp
= data
;
4045 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
4046 (void) fprintf(stderr
, gettext("cannot rollback to "
4047 "'%s': clones of previous snapshots exist\n"),
4049 (void) fprintf(stderr
, gettext("use '-R' to "
4050 "force deletion of the following clones and "
4056 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
4064 * Report some snapshots/bookmarks more recent than the one specified.
4065 * Used when '-r' is not specified. We reuse this same callback for the
4066 * snapshot dependents - if 'cb_dependent' is set, then this is a
4067 * dependent and we should report it without checking the transaction group.
4070 rollback_check(zfs_handle_t
*zhp
, void *data
)
4072 rollback_cbdata_t
*cbp
= data
;
4074 * Max number of younger snapshots and/or bookmarks to display before
4075 * we stop the iteration.
4077 const uint8_t max_younger
= 32;
4079 if (cbp
->cb_doclones
) {
4084 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
4085 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
4086 (void) fprintf(stderr
, gettext("cannot "
4087 "rollback to '%s': more recent snapshots "
4088 "or bookmarks exist\n"),
4090 (void) fprintf(stderr
, gettext("use '-r' to "
4091 "force deletion of the following "
4092 "snapshots and bookmarks:\n"));
4097 if (cbp
->cb_recurse
) {
4098 if (zfs_iter_dependents_v2(zhp
, 0, B_TRUE
,
4099 rollback_check_dependent
, cbp
) != 0) {
4104 (void) fprintf(stderr
, "%s\n",
4106 cbp
->cb_younger_ds_printed
++;
4111 if (cbp
->cb_younger_ds_printed
== max_younger
) {
4113 * This non-recursive rollback is going to fail due to the
4114 * presence of snapshots and/or bookmarks that are younger than
4115 * the rollback target.
4116 * We printed some of the offending objects, now we stop
4117 * zfs_iter_snapshot/bookmark iteration so we can fail fast and
4118 * avoid iterating over the rest of the younger objects
4120 (void) fprintf(stderr
, gettext("Output limited to %d "
4121 "snapshots/bookmarks\n"), max_younger
);
4128 zfs_do_rollback(int argc
, char **argv
)
4132 boolean_t force
= B_FALSE
;
4133 rollback_cbdata_t cb
= { 0 };
4134 zfs_handle_t
*zhp
, *snap
;
4135 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
4137 uint64_t min_txg
= 0;
4140 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
4153 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4162 /* check number of arguments */
4164 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
4168 (void) fprintf(stderr
, gettext("too many arguments\n"));
4172 /* open the snapshot */
4173 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
4176 /* open the parent dataset */
4177 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
4178 verify((delim
= strrchr(parentname
, '@')) != NULL
);
4180 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
4186 * Check for more recent snapshots and/or clones based on the presence
4189 cb
.cb_target
= argv
[0];
4190 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
4191 cb
.cb_first
= B_TRUE
;
4194 if (cb
.cb_create
> 0)
4195 min_txg
= cb
.cb_create
;
4197 if ((ret
= zfs_iter_snapshots_v2(zhp
, 0, rollback_check
, &cb
,
4200 if ((ret
= zfs_iter_bookmarks_v2(zhp
, 0, rollback_check
, &cb
)) != 0)
4203 if ((ret
= cb
.cb_error
) != 0)
4207 * Rollback parent to the given snapshot.
4209 ret
= zfs_rollback(zhp
, snap
, force
);
4222 * zfs set property=value ... { fs | snap | vol } ...
4224 * Sets the given properties for all datasets specified on the command line.
4228 set_callback(zfs_handle_t
*zhp
, void *data
)
4230 zprop_set_cbdata_t
*cb
= data
;
4231 int ret
= zfs_prop_set_list_flags(zhp
, cb
->cb_proplist
, cb
->cb_flags
);
4233 if (ret
!= 0 || libzfs_errno(g_zfs
) != EZFS_SUCCESS
) {
4234 switch (libzfs_errno(g_zfs
)) {
4235 case EZFS_MOUNTFAILED
:
4236 (void) fprintf(stderr
, gettext("property may be set "
4237 "but unable to remount filesystem\n"));
4239 case EZFS_SHARENFSFAILED
:
4240 (void) fprintf(stderr
, gettext("property may be set "
4241 "but unable to reshare filesystem\n"));
4249 zfs_do_set(int argc
, char **argv
)
4251 zprop_set_cbdata_t cb
= { 0 };
4252 int ds_start
= -1; /* argv idx of first dataset arg */
4257 while ((c
= getopt(argc
, argv
, "u")) != -1) {
4260 cb
.cb_flags
|= ZFS_SET_NOMOUNT
;
4264 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4273 /* check number of arguments */
4275 (void) fprintf(stderr
, gettext("missing arguments\n"));
4279 if (strchr(argv
[0], '=') == NULL
) {
4280 (void) fprintf(stderr
, gettext("missing property=value "
4283 (void) fprintf(stderr
, gettext("missing dataset "
4289 /* validate argument order: prop=val args followed by dataset args */
4290 for (i
= 0; i
< argc
; i
++) {
4291 if (strchr(argv
[i
], '=') != NULL
) {
4293 /* out-of-order prop=val argument */
4294 (void) fprintf(stderr
, gettext("invalid "
4295 "argument order\n"));
4298 } else if (ds_start
< 0) {
4303 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
4307 /* Populate a list of property settings */
4308 if (nvlist_alloc(&cb
.cb_proplist
, NV_UNIQUE_NAME
, 0) != 0)
4310 for (i
= 0; i
< ds_start
; i
++) {
4311 if (!parseprop(cb
.cb_proplist
, argv
[i
])) {
4317 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
4318 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, &cb
);
4321 nvlist_free(cb
.cb_proplist
);
4325 typedef struct snap_cbdata
{
4327 boolean_t sd_recursive
;
4328 const char *sd_snapname
;
4332 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
4334 snap_cbdata_t
*sd
= arg
;
4339 if (sd
->sd_recursive
&&
4340 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
4345 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
4348 fnvlist_add_boolean(sd
->sd_nvl
, name
);
4351 if (sd
->sd_recursive
)
4352 rv
= zfs_iter_filesystems_v2(zhp
, 0, zfs_snapshot_cb
, sd
);
4358 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
4360 * Creates a snapshot with the given name. While functionally equivalent to
4361 * 'zfs create', it is a separate command to differentiate intent.
4364 zfs_do_snapshot(int argc
, char **argv
)
4369 snap_cbdata_t sd
= { 0 };
4370 boolean_t multiple_snaps
= B_FALSE
;
4372 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
4374 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4378 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
4381 if (!parseprop(props
, optarg
)) {
4382 nvlist_free(sd
.sd_nvl
);
4388 sd
.sd_recursive
= B_TRUE
;
4389 multiple_snaps
= B_TRUE
;
4392 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4401 /* check number of arguments */
4403 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4408 multiple_snaps
= B_TRUE
;
4409 for (; argc
> 0; argc
--, argv
++) {
4413 atp
= strchr(argv
[0], '@');
4417 sd
.sd_snapname
= atp
+ 1;
4418 zhp
= zfs_open(g_zfs
, argv
[0],
4419 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4422 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
4426 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
4427 nvlist_free(sd
.sd_nvl
);
4429 if (ret
!= 0 && multiple_snaps
)
4430 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
4434 nvlist_free(sd
.sd_nvl
);
4441 * Array of prefixes to exclude –
4442 * a linear search, even if executed for each dataset,
4443 * is plenty good enough.
4445 typedef struct zfs_send_exclude_arg
{
4448 } zfs_send_exclude_arg_t
;
4451 zfs_do_send_exclude(zfs_handle_t
*zhp
, void *context
)
4453 zfs_send_exclude_arg_t
*excludes
= context
;
4454 const char *name
= zfs_get_name(zhp
);
4456 for (size_t i
= 0; i
< excludes
->count
; ++i
) {
4457 size_t len
= strlen(excludes
->list
[i
]);
4458 if (strncmp(name
, excludes
->list
[i
], len
) == 0 &&
4459 memchr("/@", name
[len
], sizeof ("/@")))
4467 * Send a backup stream to stdout.
4470 zfs_do_send(int argc
, char **argv
)
4472 char *fromname
= NULL
;
4473 char *toname
= NULL
;
4474 char *resume_token
= NULL
;
4477 sendflags_t flags
= { 0 };
4479 nvlist_t
*dbgnv
= NULL
;
4480 char *redactbook
= NULL
;
4481 zfs_send_exclude_arg_t excludes
= { 0 };
4483 struct option long_options
[] = {
4484 {"replicate", no_argument
, NULL
, 'R'},
4485 {"skip-missing", no_argument
, NULL
, 's'},
4486 {"redact", required_argument
, NULL
, 'd'},
4487 {"props", no_argument
, NULL
, 'p'},
4488 {"parsable", no_argument
, NULL
, 'P'},
4489 {"dedup", no_argument
, NULL
, 'D'},
4490 {"proctitle", no_argument
, NULL
, 'V'},
4491 {"verbose", no_argument
, NULL
, 'v'},
4492 {"dryrun", no_argument
, NULL
, 'n'},
4493 {"large-block", no_argument
, NULL
, 'L'},
4494 {"embed", no_argument
, NULL
, 'e'},
4495 {"resume", required_argument
, NULL
, 't'},
4496 {"compressed", no_argument
, NULL
, 'c'},
4497 {"raw", no_argument
, NULL
, 'w'},
4498 {"backup", no_argument
, NULL
, 'b'},
4499 {"holds", no_argument
, NULL
, 'h'},
4500 {"saved", no_argument
, NULL
, 'S'},
4501 {"exclude", required_argument
, NULL
, 'X'},
4506 while ((c
= getopt_long(argc
, argv
, ":i:I:RsDpVvnPLeht:cwbd:SX:",
4507 long_options
, NULL
)) != -1) {
4510 for (char *ds
; (ds
= strsep(&optarg
, ",")) != NULL
; ) {
4511 if (!zfs_name_valid(ds
, ZFS_TYPE_DATASET
) ||
4512 strchr(ds
, '/') == NULL
) {
4513 (void) fprintf(stderr
, gettext("-X %s: "
4514 "not a valid non-root dataset name"
4518 excludes
.list
= safe_realloc(excludes
.list
,
4519 sizeof (char *) * (excludes
.count
+ 1));
4520 excludes
.list
[excludes
.count
++] = ds
;
4532 flags
.doall
= B_TRUE
;
4535 flags
.replicate
= B_TRUE
;
4538 flags
.skipmissing
= B_TRUE
;
4541 redactbook
= optarg
;
4544 flags
.props
= B_TRUE
;
4547 flags
.backup
= B_TRUE
;
4550 flags
.holds
= B_TRUE
;
4553 flags
.parsable
= B_TRUE
;
4556 flags
.progressastitle
= B_TRUE
;
4560 flags
.progress
= B_TRUE
;
4563 (void) fprintf(stderr
,
4564 gettext("WARNING: deduplicated send is no "
4565 "longer supported. A regular,\n"
4566 "non-deduplicated stream will be generated.\n\n"));
4569 flags
.dryrun
= B_TRUE
;
4572 flags
.largeblock
= B_TRUE
;
4575 flags
.embed_data
= B_TRUE
;
4578 resume_token
= optarg
;
4581 flags
.compress
= B_TRUE
;
4585 flags
.compress
= B_TRUE
;
4586 flags
.embed_data
= B_TRUE
;
4587 flags
.largeblock
= B_TRUE
;
4590 flags
.saved
= B_TRUE
;
4594 * If a parameter was not passed, optopt contains the
4595 * value that would normally lead us into the
4596 * appropriate case statement. If it's > 256, then this
4597 * must be a longopt and we should look at argv to get
4598 * the string. Otherwise it's just the character, so we
4599 * should use it directly.
4601 if (optopt
<= UINT8_MAX
) {
4602 (void) fprintf(stderr
,
4603 gettext("missing argument for '%c' "
4604 "option\n"), optopt
);
4606 (void) fprintf(stderr
,
4607 gettext("missing argument for '%s' "
4608 "option\n"), argv
[optind
- 1]);
4610 free(excludes
.list
);
4616 * If an invalid flag was passed, optopt contains the
4617 * character if it was a short flag, or 0 if it was a
4621 (void) fprintf(stderr
,
4622 gettext("invalid option '%c'\n"), optopt
);
4624 (void) fprintf(stderr
,
4625 gettext("invalid option '%s'\n"),
4629 free(excludes
.list
);
4634 if ((flags
.parsable
|| flags
.progressastitle
) && flags
.verbosity
== 0)
4635 flags
.verbosity
= 1;
4637 if (excludes
.count
> 0 && !flags
.replicate
) {
4638 free(excludes
.list
);
4639 (void) fprintf(stderr
, gettext("Cannot specify "
4640 "dataset exclusion (-X) on a non-recursive "
4648 if (resume_token
!= NULL
) {
4649 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
4650 flags
.backup
|| flags
.holds
||
4651 flags
.saved
|| redactbook
!= NULL
) {
4652 free(excludes
.list
);
4653 (void) fprintf(stderr
,
4654 gettext("invalid flags combined with -t\n"));
4658 free(excludes
.list
);
4659 (void) fprintf(stderr
, gettext("too many arguments\n"));
4664 free(excludes
.list
);
4665 (void) fprintf(stderr
,
4666 gettext("missing snapshot argument\n"));
4670 free(excludes
.list
);
4671 (void) fprintf(stderr
, gettext("too many arguments\n"));
4677 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
4678 flags
.doall
|| flags
.backup
||
4679 flags
.holds
|| flags
.largeblock
|| flags
.embed_data
||
4680 flags
.compress
|| flags
.raw
|| redactbook
!= NULL
) {
4681 free(excludes
.list
);
4683 (void) fprintf(stderr
, gettext("incompatible flags "
4684 "combined with saved send flag\n"));
4687 if (strchr(argv
[0], '@') != NULL
) {
4688 free(excludes
.list
);
4690 (void) fprintf(stderr
, gettext("saved send must "
4691 "specify the dataset with partially-received "
4697 if (flags
.raw
&& redactbook
!= NULL
) {
4698 free(excludes
.list
);
4699 (void) fprintf(stderr
,
4700 gettext("Error: raw sends may not be redacted.\n"));
4704 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
4705 free(excludes
.list
);
4706 (void) fprintf(stderr
,
4707 gettext("Error: Stream can not be written to a terminal.\n"
4708 "You must redirect standard output.\n"));
4713 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
4715 free(excludes
.list
);
4719 err
= zfs_send_saved(zhp
, &flags
, STDOUT_FILENO
,
4721 free(excludes
.list
);
4724 } else if (resume_token
!= NULL
) {
4725 free(excludes
.list
);
4726 return (zfs_send_resume(g_zfs
, &flags
, STDOUT_FILENO
,
4730 if (flags
.skipmissing
&& !flags
.replicate
) {
4731 free(excludes
.list
);
4732 (void) fprintf(stderr
,
4733 gettext("skip-missing flag can only be used in "
4734 "conjunction with replicate\n"));
4739 * For everything except -R and -I, use the new, cleaner code path.
4741 if (!(flags
.replicate
|| flags
.doall
)) {
4742 char frombuf
[ZFS_MAX_DATASET_NAME_LEN
];
4744 if (fromname
!= NULL
&& (strchr(fromname
, '#') == NULL
&&
4745 strchr(fromname
, '@') == NULL
)) {
4747 * Neither bookmark or snapshot was specified. Print a
4748 * warning, and assume snapshot.
4750 (void) fprintf(stderr
, "Warning: incremental source "
4751 "didn't specify type, assuming snapshot. Use '@' "
4752 "or '#' prefix to avoid ambiguity.\n");
4753 (void) snprintf(frombuf
, sizeof (frombuf
), "@%s",
4757 if (fromname
!= NULL
&&
4758 (fromname
[0] == '#' || fromname
[0] == '@')) {
4760 * Incremental source name begins with # or @.
4761 * Default to same fs as target.
4763 char tmpbuf
[ZFS_MAX_DATASET_NAME_LEN
];
4764 (void) strlcpy(tmpbuf
, fromname
, sizeof (tmpbuf
));
4765 (void) strlcpy(frombuf
, argv
[0], sizeof (frombuf
));
4766 cp
= strchr(frombuf
, '@');
4769 (void) strlcat(frombuf
, tmpbuf
, sizeof (frombuf
));
4773 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
4775 free(excludes
.list
);
4778 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, &flags
,
4781 free(excludes
.list
);
4786 if (fromname
!= NULL
&& strchr(fromname
, '#')) {
4787 (void) fprintf(stderr
,
4788 gettext("Error: multiple snapshots cannot be "
4789 "sent from a bookmark.\n"));
4790 free(excludes
.list
);
4794 if (redactbook
!= NULL
) {
4795 (void) fprintf(stderr
, gettext("Error: multiple snapshots "
4796 "cannot be sent redacted.\n"));
4797 free(excludes
.list
);
4801 if ((cp
= strchr(argv
[0], '@')) == NULL
) {
4802 (void) fprintf(stderr
, gettext("Error: "
4803 "Unsupported flag with filesystem or bookmark.\n"));
4804 free(excludes
.list
);
4809 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4811 free(excludes
.list
);
4816 * If they specified the full path to the snapshot, chop off
4817 * everything except the short name of the snapshot, but special
4818 * case if they specify the origin.
4820 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
4821 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
4824 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
4825 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
4827 if (strcmp(origin
, fromname
) == 0) {
4829 flags
.fromorigin
= B_TRUE
;
4832 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
4834 free(excludes
.list
);
4835 (void) fprintf(stderr
,
4836 gettext("incremental source must be "
4837 "in same filesystem\n"));
4841 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
4843 free(excludes
.list
);
4844 (void) fprintf(stderr
,
4845 gettext("invalid incremental source\n"));
4851 if (flags
.replicate
&& fromname
== NULL
)
4852 flags
.doall
= B_TRUE
;
4854 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
,
4855 excludes
.count
> 0 ? zfs_do_send_exclude
: NULL
,
4856 &excludes
, flags
.verbosity
>= 3 ? &dbgnv
: NULL
);
4858 if (flags
.verbosity
>= 3 && dbgnv
!= NULL
) {
4860 * dump_nvlist prints to stdout, but that's been
4861 * redirected to a file. Make it print to stderr
4864 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
4865 dump_nvlist(dbgnv
, 0);
4870 free(excludes
.list
);
4875 * Restore a backup stream from stdin.
4878 zfs_do_receive(int argc
, char **argv
)
4881 recvflags_t flags
= { 0 };
4882 boolean_t abort_resumable
= B_FALSE
;
4885 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
4889 while ((c
= getopt(argc
, argv
, ":o:x:dehMnuvFsAc")) != -1) {
4892 if (!parseprop(props
, optarg
)) {
4898 if (!parsepropname(props
, optarg
)) {
4905 (void) fprintf(stderr
, gettext("invalid option "
4906 "combination: -d and -e are mutually "
4910 flags
.isprefix
= B_TRUE
;
4913 if (flags
.isprefix
) {
4914 (void) fprintf(stderr
, gettext("invalid option "
4915 "combination: -d and -e are mutually "
4919 flags
.istail
= B_TRUE
;
4922 flags
.skipholds
= B_TRUE
;
4925 flags
.forceunmount
= B_TRUE
;
4928 flags
.dryrun
= B_TRUE
;
4931 flags
.nomount
= B_TRUE
;
4934 flags
.verbose
= B_TRUE
;
4937 flags
.resumable
= B_TRUE
;
4940 flags
.force
= B_TRUE
;
4943 abort_resumable
= B_TRUE
;
4946 flags
.heal
= B_TRUE
;
4949 (void) fprintf(stderr
, gettext("missing argument for "
4950 "'%c' option\n"), optopt
);
4954 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4963 /* zfs recv -e (use "tail" name) implies -d (remove dataset "head") */
4965 flags
.isprefix
= B_TRUE
;
4967 /* check number of arguments */
4969 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4973 (void) fprintf(stderr
, gettext("too many arguments\n"));
4977 if (abort_resumable
) {
4978 if (flags
.isprefix
|| flags
.istail
|| flags
.dryrun
||
4979 flags
.resumable
|| flags
.nomount
) {
4980 (void) fprintf(stderr
, gettext("invalid option\n"));
4984 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
4985 (void) snprintf(namebuf
, sizeof (namebuf
),
4986 "%s/%%recv", argv
[0]);
4988 if (zfs_dataset_exists(g_zfs
, namebuf
,
4989 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) {
4990 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4991 namebuf
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4996 err
= zfs_destroy(zhp
, B_FALSE
);
4999 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
5000 argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5003 if (!zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) ||
5004 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
5005 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == -1) {
5006 (void) fprintf(stderr
,
5007 gettext("'%s' does not have any "
5008 "resumable receive state to abort\n"),
5014 err
= zfs_destroy(zhp
, B_FALSE
);
5021 if (isatty(STDIN_FILENO
)) {
5022 (void) fprintf(stderr
,
5023 gettext("Error: Backup stream can not be read "
5024 "from a terminal.\n"
5025 "You must redirect standard input.\n"));
5029 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
5036 * allow/unallow stuff
5038 /* copied from zfs/sys/dsl_deleg.h */
5039 #define ZFS_DELEG_PERM_CREATE "create"
5040 #define ZFS_DELEG_PERM_DESTROY "destroy"
5041 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
5042 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
5043 #define ZFS_DELEG_PERM_CLONE "clone"
5044 #define ZFS_DELEG_PERM_PROMOTE "promote"
5045 #define ZFS_DELEG_PERM_RENAME "rename"
5046 #define ZFS_DELEG_PERM_MOUNT "mount"
5047 #define ZFS_DELEG_PERM_SHARE "share"
5048 #define ZFS_DELEG_PERM_SEND "send"
5049 #define ZFS_DELEG_PERM_RECEIVE "receive"
5050 #define ZFS_DELEG_PERM_ALLOW "allow"
5051 #define ZFS_DELEG_PERM_USERPROP "userprop"
5052 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
5053 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
5054 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
5055 #define ZFS_DELEG_PERM_USERUSED "userused"
5056 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
5057 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
5058 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
5059 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
5060 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
5062 #define ZFS_DELEG_PERM_HOLD "hold"
5063 #define ZFS_DELEG_PERM_RELEASE "release"
5064 #define ZFS_DELEG_PERM_DIFF "diff"
5065 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
5066 #define ZFS_DELEG_PERM_LOAD_KEY "load-key"
5067 #define ZFS_DELEG_PERM_CHANGE_KEY "change-key"
5069 #define ZFS_DELEG_PERM_PROJECTUSED "projectused"
5070 #define ZFS_DELEG_PERM_PROJECTQUOTA "projectquota"
5071 #define ZFS_DELEG_PERM_PROJECTOBJUSED "projectobjused"
5072 #define ZFS_DELEG_PERM_PROJECTOBJQUOTA "projectobjquota"
5074 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
5076 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
5077 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
5078 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
5079 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
5080 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
5081 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
5082 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
5083 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
5084 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
5085 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
5086 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
5087 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
5088 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
5089 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
5090 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
5091 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
5092 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
5093 { ZFS_DELEG_PERM_LOAD_KEY
, ZFS_DELEG_NOTE_LOAD_KEY
},
5094 { ZFS_DELEG_PERM_CHANGE_KEY
, ZFS_DELEG_NOTE_CHANGE_KEY
},
5096 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
5097 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
5098 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
5099 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
5100 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
5101 { ZFS_DELEG_PERM_USEROBJQUOTA
, ZFS_DELEG_NOTE_USEROBJQUOTA
},
5102 { ZFS_DELEG_PERM_USEROBJUSED
, ZFS_DELEG_NOTE_USEROBJUSED
},
5103 { ZFS_DELEG_PERM_GROUPOBJQUOTA
, ZFS_DELEG_NOTE_GROUPOBJQUOTA
},
5104 { ZFS_DELEG_PERM_GROUPOBJUSED
, ZFS_DELEG_NOTE_GROUPOBJUSED
},
5105 { ZFS_DELEG_PERM_PROJECTUSED
, ZFS_DELEG_NOTE_PROJECTUSED
},
5106 { ZFS_DELEG_PERM_PROJECTQUOTA
, ZFS_DELEG_NOTE_PROJECTQUOTA
},
5107 { ZFS_DELEG_PERM_PROJECTOBJUSED
, ZFS_DELEG_NOTE_PROJECTOBJUSED
},
5108 { ZFS_DELEG_PERM_PROJECTOBJQUOTA
, ZFS_DELEG_NOTE_PROJECTOBJQUOTA
},
5109 { NULL
, ZFS_DELEG_NOTE_NONE
}
5112 /* permission structure */
5113 typedef struct deleg_perm
{
5114 zfs_deleg_who_type_t dp_who_type
;
5115 const char *dp_name
;
5117 boolean_t dp_descend
;
5121 typedef struct deleg_perm_node
{
5122 deleg_perm_t dpn_perm
;
5124 uu_avl_node_t dpn_avl_node
;
5125 } deleg_perm_node_t
;
5127 typedef struct fs_perm fs_perm_t
;
5129 /* permissions set */
5130 typedef struct who_perm
{
5131 zfs_deleg_who_type_t who_type
;
5132 const char *who_name
; /* id */
5133 char who_ug_name
[256]; /* user/group name */
5134 fs_perm_t
*who_fsperm
; /* uplink */
5136 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
5140 typedef struct who_perm_node
{
5141 who_perm_t who_perm
;
5142 uu_avl_node_t who_avl_node
;
5145 typedef struct fs_perm_set fs_perm_set_t
;
5146 /* fs permissions */
5148 const char *fsp_name
;
5150 uu_avl_t
*fsp_sc_avl
; /* sets,create */
5151 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
5153 fs_perm_set_t
*fsp_set
; /* uplink */
5157 typedef struct fs_perm_node
{
5158 fs_perm_t fspn_fsperm
;
5161 uu_list_node_t fspn_list_node
;
5164 /* top level structure */
5165 struct fs_perm_set
{
5166 uu_list_pool_t
*fsps_list_pool
;
5167 uu_list_t
*fsps_list
; /* list of fs_perms */
5169 uu_avl_pool_t
*fsps_named_set_avl_pool
;
5170 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
5171 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
5174 static inline const char *
5175 deleg_perm_type(zfs_deleg_note_t note
)
5181 case ZFS_DELEG_NOTE_GROUPQUOTA
:
5182 case ZFS_DELEG_NOTE_GROUPUSED
:
5183 case ZFS_DELEG_NOTE_USERPROP
:
5184 case ZFS_DELEG_NOTE_USERQUOTA
:
5185 case ZFS_DELEG_NOTE_USERUSED
:
5186 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
5187 case ZFS_DELEG_NOTE_USEROBJUSED
:
5188 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
5189 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
5190 case ZFS_DELEG_NOTE_PROJECTUSED
:
5191 case ZFS_DELEG_NOTE_PROJECTQUOTA
:
5192 case ZFS_DELEG_NOTE_PROJECTOBJUSED
:
5193 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA
:
5195 return (gettext("other"));
5197 return (gettext("subcommand"));
5202 who_type2weight(zfs_deleg_who_type_t who_type
)
5206 case ZFS_DELEG_NAMED_SET_SETS
:
5207 case ZFS_DELEG_NAMED_SET
:
5210 case ZFS_DELEG_CREATE_SETS
:
5211 case ZFS_DELEG_CREATE
:
5214 case ZFS_DELEG_USER_SETS
:
5215 case ZFS_DELEG_USER
:
5218 case ZFS_DELEG_GROUP_SETS
:
5219 case ZFS_DELEG_GROUP
:
5222 case ZFS_DELEG_EVERYONE_SETS
:
5223 case ZFS_DELEG_EVERYONE
:
5234 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
5237 const who_perm_node_t
*l
= larg
;
5238 const who_perm_node_t
*r
= rarg
;
5239 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
5240 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
5241 int lweight
= who_type2weight(ltype
);
5242 int rweight
= who_type2weight(rtype
);
5243 int res
= lweight
- rweight
;
5245 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
5246 ZFS_MAX_DELEG_NAME
-1);
5257 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
5260 const deleg_perm_node_t
*l
= larg
;
5261 const deleg_perm_node_t
*r
= rarg
;
5262 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
5263 ZFS_MAX_DELEG_NAME
-1);
5275 fs_perm_set_init(fs_perm_set_t
*fspset
)
5277 memset(fspset
, 0, sizeof (fs_perm_set_t
));
5279 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
5280 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
5281 NULL
, UU_DEFAULT
)) == NULL
)
5283 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
5284 UU_DEFAULT
)) == NULL
)
5287 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
5288 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
5289 who_perm_node_t
, who_avl_node
), who_perm_compare
,
5290 UU_DEFAULT
)) == NULL
)
5293 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
5294 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
5295 who_perm_node_t
, who_avl_node
), who_perm_compare
,
5296 UU_DEFAULT
)) == NULL
)
5299 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
5300 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
5301 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
5306 static inline void fs_perm_fini(fs_perm_t
*);
5307 static inline void who_perm_fini(who_perm_t
*);
5310 fs_perm_set_fini(fs_perm_set_t
*fspset
)
5312 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
5314 while (node
!= NULL
) {
5315 fs_perm_node_t
*next_node
=
5316 uu_list_next(fspset
->fsps_list
, node
);
5317 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
5318 fs_perm_fini(fsperm
);
5319 uu_list_remove(fspset
->fsps_list
, node
);
5324 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
5325 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
5326 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
5330 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
5333 deleg_perm
->dp_who_type
= type
;
5334 deleg_perm
->dp_name
= name
;
5338 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
5339 zfs_deleg_who_type_t type
, const char *name
)
5341 uu_avl_pool_t
*pool
;
5342 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
5344 memset(who_perm
, 0, sizeof (who_perm_t
));
5346 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
5347 UU_DEFAULT
)) == NULL
)
5350 who_perm
->who_type
= type
;
5351 who_perm
->who_name
= name
;
5352 who_perm
->who_fsperm
= fsperm
;
5356 who_perm_fini(who_perm_t
*who_perm
)
5358 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
5360 while (node
!= NULL
) {
5361 deleg_perm_node_t
*next_node
=
5362 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
5364 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
5369 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
5373 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
5375 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
5376 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
5378 memset(fsperm
, 0, sizeof (fs_perm_t
));
5380 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
5384 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
5388 fsperm
->fsp_set
= fspset
;
5389 fsperm
->fsp_name
= fsname
;
5393 fs_perm_fini(fs_perm_t
*fsperm
)
5395 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
5396 while (node
!= NULL
) {
5397 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
5399 who_perm_t
*who_perm
= &node
->who_perm
;
5400 who_perm_fini(who_perm
);
5401 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
5406 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
5407 while (node
!= NULL
) {
5408 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
5410 who_perm_t
*who_perm
= &node
->who_perm
;
5411 who_perm_fini(who_perm
);
5412 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
5417 uu_avl_destroy(fsperm
->fsp_sc_avl
);
5418 uu_avl_destroy(fsperm
->fsp_uge_avl
);
5422 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
5423 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
5425 uu_avl_index_t idx
= 0;
5427 deleg_perm_node_t
*found_node
= NULL
;
5428 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
5430 deleg_perm_init(deleg_perm
, who_type
, name
);
5432 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
5434 uu_avl_insert(avl
, node
, idx
);
5437 deleg_perm
= &node
->dpn_perm
;
5442 case ZFS_DELEG_LOCAL
:
5443 deleg_perm
->dp_local
= B_TRUE
;
5445 case ZFS_DELEG_DESCENDENT
:
5446 deleg_perm
->dp_descend
= B_TRUE
;
5451 assert(B_FALSE
); /* invalid locality */
5456 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
5458 nvpair_t
*nvp
= NULL
;
5459 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
5460 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
5461 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
5463 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5464 const char *name
= nvpair_name(nvp
);
5465 data_type_t type
= nvpair_type(nvp
);
5466 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
5467 deleg_perm_node_t
*node
=
5468 safe_malloc(sizeof (deleg_perm_node_t
));
5470 VERIFY(type
== DATA_TYPE_BOOLEAN
);
5472 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
5473 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
5480 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
5482 nvpair_t
*nvp
= NULL
;
5483 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
5485 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5486 nvlist_t
*nvl2
= NULL
;
5487 const char *name
= nvpair_name(nvp
);
5488 uu_avl_t
*avl
= NULL
;
5489 uu_avl_pool_t
*avl_pool
= NULL
;
5490 zfs_deleg_who_type_t perm_type
= name
[0];
5491 char perm_locality
= name
[1];
5492 const char *perm_name
= name
+ 3;
5493 who_perm_t
*who_perm
= NULL
;
5495 assert('$' == name
[2]);
5497 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
5500 switch (perm_type
) {
5501 case ZFS_DELEG_CREATE
:
5502 case ZFS_DELEG_CREATE_SETS
:
5503 case ZFS_DELEG_NAMED_SET
:
5504 case ZFS_DELEG_NAMED_SET_SETS
:
5505 avl_pool
= fspset
->fsps_named_set_avl_pool
;
5506 avl
= fsperm
->fsp_sc_avl
;
5508 case ZFS_DELEG_USER
:
5509 case ZFS_DELEG_USER_SETS
:
5510 case ZFS_DELEG_GROUP
:
5511 case ZFS_DELEG_GROUP_SETS
:
5512 case ZFS_DELEG_EVERYONE
:
5513 case ZFS_DELEG_EVERYONE_SETS
:
5514 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
5515 avl
= fsperm
->fsp_uge_avl
;
5519 assert(!"unhandled zfs_deleg_who_type_t");
5522 who_perm_node_t
*found_node
= NULL
;
5523 who_perm_node_t
*node
= safe_malloc(
5524 sizeof (who_perm_node_t
));
5525 who_perm
= &node
->who_perm
;
5526 uu_avl_index_t idx
= 0;
5528 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
5529 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
5531 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
5533 if (avl
== fsperm
->fsp_uge_avl
) {
5535 struct passwd
*p
= NULL
;
5536 struct group
*g
= NULL
;
5537 const char *nice_name
= NULL
;
5539 switch (perm_type
) {
5540 case ZFS_DELEG_USER_SETS
:
5541 case ZFS_DELEG_USER
:
5542 rid
= atoi(perm_name
);
5545 nice_name
= p
->pw_name
;
5547 case ZFS_DELEG_GROUP_SETS
:
5548 case ZFS_DELEG_GROUP
:
5549 rid
= atoi(perm_name
);
5552 nice_name
= g
->gr_name
;
5559 if (nice_name
!= NULL
) {
5561 node
->who_perm
.who_ug_name
,
5564 /* User or group unknown */
5566 node
->who_perm
.who_ug_name
,
5567 sizeof (node
->who_perm
.who_ug_name
),
5568 "(unknown: %d)", rid
);
5572 uu_avl_insert(avl
, node
, idx
);
5575 who_perm
= &node
->who_perm
;
5578 assert(who_perm
!= NULL
);
5579 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
5586 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
5588 nvpair_t
*nvp
= NULL
;
5589 uu_avl_index_t idx
= 0;
5591 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5592 nvlist_t
*nvl2
= NULL
;
5593 const char *fsname
= nvpair_name(nvp
);
5594 data_type_t type
= nvpair_type(nvp
);
5595 fs_perm_t
*fsperm
= NULL
;
5596 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
5598 fsperm
= &node
->fspn_fsperm
;
5600 VERIFY(DATA_TYPE_NVLIST
== type
);
5602 uu_list_node_init(node
, &node
->fspn_list_node
,
5603 fspset
->fsps_list_pool
);
5605 idx
= uu_list_numnodes(fspset
->fsps_list
);
5606 fs_perm_init(fsperm
, fspset
, fsname
);
5608 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
5611 (void) parse_fs_perm(fsperm
, nvl2
);
5613 uu_list_insert(fspset
->fsps_list
, node
, idx
);
5619 static inline const char *
5620 deleg_perm_comment(zfs_deleg_note_t note
)
5622 const char *str
= "";
5627 case ZFS_DELEG_NOTE_ALLOW
:
5628 str
= gettext("Must also have the permission that is being"
5629 "\n\t\t\t\tallowed");
5631 case ZFS_DELEG_NOTE_CLONE
:
5632 str
= gettext("Must also have the 'create' ability and 'mount'"
5633 "\n\t\t\t\tability in the origin file system");
5635 case ZFS_DELEG_NOTE_CREATE
:
5636 str
= gettext("Must also have the 'mount' ability");
5638 case ZFS_DELEG_NOTE_DESTROY
:
5639 str
= gettext("Must also have the 'mount' ability");
5641 case ZFS_DELEG_NOTE_DIFF
:
5642 str
= gettext("Allows lookup of paths within a dataset;"
5643 "\n\t\t\t\tgiven an object number. Ordinary users need this"
5644 "\n\t\t\t\tin order to use zfs diff");
5646 case ZFS_DELEG_NOTE_HOLD
:
5647 str
= gettext("Allows adding a user hold to a snapshot");
5649 case ZFS_DELEG_NOTE_MOUNT
:
5650 str
= gettext("Allows mount/umount of ZFS datasets");
5652 case ZFS_DELEG_NOTE_PROMOTE
:
5653 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
5654 " 'promote' ability in the origin file system");
5656 case ZFS_DELEG_NOTE_RECEIVE
:
5657 str
= gettext("Must also have the 'mount' and 'create'"
5660 case ZFS_DELEG_NOTE_RELEASE
:
5661 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
5662 "might destroy the snapshot");
5664 case ZFS_DELEG_NOTE_RENAME
:
5665 str
= gettext("Must also have the 'mount' and 'create'"
5666 "\n\t\t\t\tability in the new parent");
5668 case ZFS_DELEG_NOTE_ROLLBACK
:
5671 case ZFS_DELEG_NOTE_SEND
:
5674 case ZFS_DELEG_NOTE_SHARE
:
5675 str
= gettext("Allows sharing file systems over NFS or SMB"
5676 "\n\t\t\t\tprotocols");
5678 case ZFS_DELEG_NOTE_SNAPSHOT
:
5681 case ZFS_DELEG_NOTE_LOAD_KEY
:
5682 str
= gettext("Allows loading or unloading an encryption key");
5684 case ZFS_DELEG_NOTE_CHANGE_KEY
:
5685 str
= gettext("Allows changing or adding an encryption key");
5688 * case ZFS_DELEG_NOTE_VSCAN:
5689 * str = gettext("");
5693 case ZFS_DELEG_NOTE_GROUPQUOTA
:
5694 str
= gettext("Allows accessing any groupquota@... property");
5696 case ZFS_DELEG_NOTE_GROUPUSED
:
5697 str
= gettext("Allows reading any groupused@... property");
5699 case ZFS_DELEG_NOTE_USERPROP
:
5700 str
= gettext("Allows changing any user property");
5702 case ZFS_DELEG_NOTE_USERQUOTA
:
5703 str
= gettext("Allows accessing any userquota@... property");
5705 case ZFS_DELEG_NOTE_USERUSED
:
5706 str
= gettext("Allows reading any userused@... property");
5708 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
5709 str
= gettext("Allows accessing any userobjquota@... property");
5711 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
5712 str
= gettext("Allows accessing any \n\t\t\t\t"
5713 "groupobjquota@... property");
5715 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
5716 str
= gettext("Allows reading any groupobjused@... property");
5718 case ZFS_DELEG_NOTE_USEROBJUSED
:
5719 str
= gettext("Allows reading any userobjused@... property");
5721 case ZFS_DELEG_NOTE_PROJECTQUOTA
:
5722 str
= gettext("Allows accessing any projectquota@... property");
5724 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA
:
5725 str
= gettext("Allows accessing any \n\t\t\t\t"
5726 "projectobjquota@... property");
5728 case ZFS_DELEG_NOTE_PROJECTUSED
:
5729 str
= gettext("Allows reading any projectused@... property");
5731 case ZFS_DELEG_NOTE_PROJECTOBJUSED
:
5732 str
= gettext("Allows accessing any \n\t\t\t\t"
5733 "projectobjused@... property");
5751 boolean_t recursive
; /* unallow only */
5752 boolean_t prt_usage
;
5754 boolean_t prt_perms
;
5757 const char *dataset
;
5761 prop_cmp(const void *a
, const void *b
)
5763 const char *str1
= *(const char **)a
;
5764 const char *str2
= *(const char **)b
;
5765 return (strcmp(str1
, str2
));
5769 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
5771 const char *opt_desc
[] = {
5772 "-h", gettext("show this help message and exit"),
5773 "-l", gettext("set permission locally"),
5774 "-d", gettext("set permission for descents"),
5775 "-u", gettext("set permission for user"),
5776 "-g", gettext("set permission for group"),
5777 "-e", gettext("set permission for everyone"),
5778 "-c", gettext("set create time permission"),
5779 "-s", gettext("define permission set"),
5781 "-r", gettext("remove permissions recursively"),
5783 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
5784 size_t allow_size
= unallow_size
- 2;
5785 const char *props
[ZFS_NUM_PROPS
];
5788 FILE *fp
= requested
? stdout
: stderr
;
5789 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
5790 const char *fmt
= gettext("%-16s %-14s\t%s\n");
5792 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
5794 (void) fprintf(fp
, gettext("Options:\n"));
5795 for (i
= 0; i
< (un
? unallow_size
: allow_size
); i
+= 2) {
5796 const char *opt
= opt_desc
[i
];
5797 const char *optdsc
= opt_desc
[i
+ 1];
5798 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
5801 (void) fprintf(fp
, gettext("\nThe following permissions are "
5803 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
5805 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
5806 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
5807 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
5808 const char *perm_type
= deleg_perm_type(perm_note
);
5809 const char *perm_comment
= deleg_perm_comment(perm_note
);
5810 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
5813 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
5814 zprop_desc_t
*pd
= &pdtbl
[i
];
5815 if (pd
->pd_visible
!= B_TRUE
)
5818 if (pd
->pd_attr
== PROP_READONLY
)
5821 props
[count
++] = pd
->pd_name
;
5823 props
[count
] = NULL
;
5825 qsort(props
, count
, sizeof (char *), prop_cmp
);
5827 for (i
= 0; i
< count
; i
++)
5828 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
5831 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
5833 exit(requested
? 0 : 2);
5836 static inline const char *
5837 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
5840 if (un
&& argc
== expected_argc
- 1)
5842 else if (argc
== expected_argc
)
5843 *permsp
= argv
[argc
- 2];
5845 allow_usage(un
, B_FALSE
,
5846 gettext("wrong number of parameters\n"));
5848 return (argv
[argc
- 1]);
5852 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
5854 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
5855 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
5856 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
5857 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
5860 allow_usage(un
, B_FALSE
,
5861 gettext("-u, -g, and -e are mutually exclusive\n"));
5863 if (opts
->prt_usage
) {
5864 if (argc
== 0 && all_sum
== 0)
5865 allow_usage(un
, B_TRUE
, NULL
);
5872 allow_usage(un
, B_FALSE
,
5873 gettext("invalid options combined with -s\n"));
5875 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
5876 if (argv
[0][0] != '@')
5877 allow_usage(un
, B_FALSE
,
5878 gettext("invalid set name: missing '@' prefix\n"));
5879 opts
->who
= argv
[0];
5880 } else if (opts
->create
) {
5881 if (ldcsuge_sum
> 1)
5882 allow_usage(un
, B_FALSE
,
5883 gettext("invalid options combined with -c\n"));
5884 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5885 } else if (opts
->everyone
) {
5887 allow_usage(un
, B_FALSE
,
5888 gettext("invalid options combined with -e\n"));
5889 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5890 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
5892 opts
->everyone
= B_TRUE
;
5895 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5896 } else if (argc
== 1 && !un
) {
5897 opts
->prt_perms
= B_TRUE
;
5898 opts
->dataset
= argv
[argc
-1];
5900 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
5901 opts
->who
= argv
[0];
5904 if (!opts
->local
&& !opts
->descend
) {
5905 opts
->local
= B_TRUE
;
5906 opts
->descend
= B_TRUE
;
5911 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
5912 const char *who
, char *perms
, nvlist_t
*top_nvl
)
5915 char ld
[2] = { '\0', '\0' };
5916 char who_buf
[MAXNAMELEN
+ 32];
5917 char base_type
= '\0';
5918 char set_type
= '\0';
5919 nvlist_t
*base_nvl
= NULL
;
5920 nvlist_t
*set_nvl
= NULL
;
5923 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
5925 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
5929 case ZFS_DELEG_NAMED_SET_SETS
:
5930 case ZFS_DELEG_NAMED_SET
:
5931 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
5932 base_type
= ZFS_DELEG_NAMED_SET
;
5933 ld
[0] = ZFS_DELEG_NA
;
5935 case ZFS_DELEG_CREATE_SETS
:
5936 case ZFS_DELEG_CREATE
:
5937 set_type
= ZFS_DELEG_CREATE_SETS
;
5938 base_type
= ZFS_DELEG_CREATE
;
5939 ld
[0] = ZFS_DELEG_NA
;
5941 case ZFS_DELEG_USER_SETS
:
5942 case ZFS_DELEG_USER
:
5943 set_type
= ZFS_DELEG_USER_SETS
;
5944 base_type
= ZFS_DELEG_USER
;
5946 ld
[0] = ZFS_DELEG_LOCAL
;
5948 ld
[1] = ZFS_DELEG_DESCENDENT
;
5950 case ZFS_DELEG_GROUP_SETS
:
5951 case ZFS_DELEG_GROUP
:
5952 set_type
= ZFS_DELEG_GROUP_SETS
;
5953 base_type
= ZFS_DELEG_GROUP
;
5955 ld
[0] = ZFS_DELEG_LOCAL
;
5957 ld
[1] = ZFS_DELEG_DESCENDENT
;
5959 case ZFS_DELEG_EVERYONE_SETS
:
5960 case ZFS_DELEG_EVERYONE
:
5961 set_type
= ZFS_DELEG_EVERYONE_SETS
;
5962 base_type
= ZFS_DELEG_EVERYONE
;
5964 ld
[0] = ZFS_DELEG_LOCAL
;
5966 ld
[1] = ZFS_DELEG_DESCENDENT
;
5970 assert(set_type
!= '\0' && base_type
!= '\0');
5973 if (perms
!= NULL
) {
5975 char *end
= curr
+ strlen(perms
);
5977 while (curr
< end
) {
5978 char *delim
= strchr(curr
, ',');
5989 (void) nvlist_add_boolean(nvl
, curr
);
5995 for (i
= 0; i
< 2; i
++) {
5996 char locality
= ld
[i
];
6000 if (!nvlist_empty(base_nvl
)) {
6002 (void) snprintf(who_buf
,
6003 sizeof (who_buf
), "%c%c$%s",
6004 base_type
, locality
, who
);
6006 (void) snprintf(who_buf
,
6007 sizeof (who_buf
), "%c%c$",
6008 base_type
, locality
);
6010 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
6015 if (!nvlist_empty(set_nvl
)) {
6017 (void) snprintf(who_buf
,
6018 sizeof (who_buf
), "%c%c$%s",
6019 set_type
, locality
, who
);
6021 (void) snprintf(who_buf
,
6022 sizeof (who_buf
), "%c%c$",
6023 set_type
, locality
);
6025 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
6030 for (i
= 0; i
< 2; i
++) {
6031 char locality
= ld
[i
];
6036 (void) snprintf(who_buf
, sizeof (who_buf
),
6037 "%c%c$%s", base_type
, locality
, who
);
6039 (void) snprintf(who_buf
, sizeof (who_buf
),
6040 "%c%c$", base_type
, locality
);
6041 (void) nvlist_add_boolean(top_nvl
, who_buf
);
6044 (void) snprintf(who_buf
, sizeof (who_buf
),
6045 "%c%c$%s", set_type
, locality
, who
);
6047 (void) snprintf(who_buf
, sizeof (who_buf
),
6048 "%c%c$", set_type
, locality
);
6049 (void) nvlist_add_boolean(top_nvl
, who_buf
);
6055 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
6057 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
6061 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
6062 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
6063 } else if (opts
->create
) {
6064 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
6065 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
6066 } else if (opts
->everyone
) {
6067 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
6068 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
6070 char *curr
= opts
->who
;
6071 char *end
= curr
+ strlen(curr
);
6073 while (curr
< end
) {
6075 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
6077 char *delim
= strchr(curr
, ',');
6080 struct passwd
*p
= NULL
;
6081 struct group
*g
= NULL
;
6089 rid
= (uid_t
)strtol(curr
, &endch
, 0);
6091 who_type
= ZFS_DELEG_USER
;
6099 else if (*endch
!= '\0') {
6100 (void) snprintf(errbuf
, sizeof (errbuf
),
6101 gettext("invalid user %s\n"), curr
);
6102 allow_usage(un
, B_TRUE
, errbuf
);
6104 } else if (opts
->group
) {
6105 who_type
= ZFS_DELEG_GROUP
;
6113 else if (*endch
!= '\0') {
6114 (void) snprintf(errbuf
, sizeof (errbuf
),
6115 gettext("invalid group %s\n"),
6117 allow_usage(un
, B_TRUE
, errbuf
);
6120 if (*endch
!= '\0') {
6127 if (*endch
!= '\0') {
6135 who_type
= ZFS_DELEG_USER
;
6137 } else if (g
!= NULL
) {
6138 who_type
= ZFS_DELEG_GROUP
;
6141 (void) snprintf(errbuf
, sizeof (errbuf
),
6142 gettext("invalid user/group %s\n"),
6144 allow_usage(un
, B_TRUE
, errbuf
);
6148 (void) sprintf(id
, "%u", rid
);
6151 store_allow_perm(who_type
, opts
->local
,
6152 opts
->descend
, who
, opts
->perms
, *nvlp
);
6161 print_set_creat_perms(uu_avl_t
*who_avl
)
6163 const char *sc_title
[] = {
6164 gettext("Permission sets:\n"),
6165 gettext("Create time permissions:\n"),
6168 who_perm_node_t
*who_node
= NULL
;
6169 int prev_weight
= -1;
6171 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
6172 who_node
= uu_avl_next(who_avl
, who_node
)) {
6173 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
6174 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
6175 const char *who_name
= who_node
->who_perm
.who_name
;
6176 int weight
= who_type2weight(who_type
);
6177 boolean_t first
= B_TRUE
;
6178 deleg_perm_node_t
*deleg_node
;
6180 if (prev_weight
!= weight
) {
6181 (void) printf("%s", sc_title
[weight
]);
6182 prev_weight
= weight
;
6185 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
6186 (void) printf("\t");
6188 (void) printf("\t%s ", who_name
);
6190 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
6191 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
6194 deleg_node
->dpn_perm
.dp_name
);
6197 (void) printf(",%s",
6198 deleg_node
->dpn_perm
.dp_name
);
6201 (void) printf("\n");
6206 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
6209 who_perm_node_t
*who_node
= NULL
;
6210 boolean_t prt_title
= B_TRUE
;
6211 uu_avl_walk_t
*walk
;
6213 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
6216 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
6217 const char *who_name
= who_node
->who_perm
.who_name
;
6218 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
6219 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
6220 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
6222 deleg_perm_node_t
*deleg_node
;
6223 boolean_t prt_who
= B_TRUE
;
6225 for (deleg_node
= uu_avl_first(avl
);
6227 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
6228 if (local
!= deleg_node
->dpn_perm
.dp_local
||
6229 descend
!= deleg_node
->dpn_perm
.dp_descend
)
6233 const char *who
= NULL
;
6235 prt_title
= B_FALSE
;
6236 (void) printf("%s", title
);
6240 case ZFS_DELEG_USER_SETS
:
6241 case ZFS_DELEG_USER
:
6242 who
= gettext("user");
6244 who_name
= nice_who_name
;
6246 case ZFS_DELEG_GROUP_SETS
:
6247 case ZFS_DELEG_GROUP
:
6248 who
= gettext("group");
6250 who_name
= nice_who_name
;
6252 case ZFS_DELEG_EVERYONE_SETS
:
6253 case ZFS_DELEG_EVERYONE
:
6254 who
= gettext("everyone");
6259 assert(who
!= NULL
);
6263 if (who_name
== NULL
)
6264 (void) printf("\t%s", who
);
6266 (void) printf("\t%s %s", who
, who_name
);
6269 (void) printf("%c%s", delim
,
6270 deleg_node
->dpn_perm
.dp_name
);
6275 (void) printf("\n");
6278 uu_avl_walk_end(walk
);
6282 print_fs_perms(fs_perm_set_t
*fspset
)
6284 fs_perm_node_t
*node
= NULL
;
6285 char buf
[MAXNAMELEN
+ 32];
6286 const char *dsname
= buf
;
6288 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
6289 node
= uu_list_next(fspset
->fsps_list
, node
)) {
6290 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
6291 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
6294 (void) snprintf(buf
, sizeof (buf
),
6295 gettext("---- Permissions on %s "),
6296 node
->fspn_fsperm
.fsp_name
);
6297 (void) printf("%s", dsname
);
6298 left
= 70 - strlen(buf
);
6301 (void) printf("\n");
6303 print_set_creat_perms(sc_avl
);
6304 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
6305 gettext("Local permissions:\n"));
6306 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
6307 gettext("Descendent permissions:\n"));
6308 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
6309 gettext("Local+Descendent permissions:\n"));
6313 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
6315 struct deleg_perms
{
6321 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
6323 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
6324 zfs_type_t zfs_type
= zfs_get_type(zhp
);
6326 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
6329 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
6333 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
6336 nvlist_t
*perm_nvl
= NULL
;
6337 nvlist_t
*update_perm_nvl
= NULL
;
6340 struct allow_opts opts
= { 0 };
6342 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
6345 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
6348 opts
.local
= B_TRUE
;
6351 opts
.descend
= B_TRUE
;
6357 opts
.group
= B_TRUE
;
6360 opts
.everyone
= B_TRUE
;
6366 opts
.create
= B_TRUE
;
6369 opts
.recursive
= B_TRUE
;
6372 (void) fprintf(stderr
, gettext("missing argument for "
6373 "'%c' option\n"), optopt
);
6377 opts
.prt_usage
= B_TRUE
;
6380 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6389 /* check arguments */
6390 parse_allow_args(argc
, argv
, un
, &opts
);
6392 /* try to open the dataset */
6393 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
6394 ZFS_TYPE_VOLUME
)) == NULL
) {
6395 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
6400 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
6403 fs_perm_set_init(&fs_perm_set
);
6404 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
6405 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
6410 print_fs_perms(&fs_perm_set
);
6412 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
6413 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
6416 if (un
&& opts
.recursive
) {
6417 struct deleg_perms data
= { un
, update_perm_nvl
};
6418 if (zfs_iter_filesystems_v2(zhp
, 0, set_deleg_perms
,
6427 nvlist_free(perm_nvl
);
6428 nvlist_free(update_perm_nvl
);
6430 fs_perm_set_fini(&fs_perm_set
);
6438 zfs_do_allow(int argc
, char **argv
)
6440 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
6444 zfs_do_unallow(int argc
, char **argv
)
6446 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
6450 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
6455 boolean_t recursive
= B_FALSE
;
6456 const char *opts
= holding
? "rt" : "r";
6460 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
6466 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6475 /* check number of arguments */
6483 if (holding
&& tag
[0] == '.') {
6484 /* tags starting with '.' are reserved for libzfs */
6485 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
6489 for (i
= 0; i
< argc
; ++i
) {
6491 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
6493 char *path
= argv
[i
];
6495 delim
= strchr(path
, '@');
6496 if (delim
== NULL
) {
6497 (void) fprintf(stderr
,
6498 gettext("'%s' is not a snapshot\n"), path
);
6502 (void) strlcpy(parent
, path
, MIN(sizeof (parent
),
6505 zhp
= zfs_open(g_zfs
, parent
,
6506 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
6512 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
6515 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
6521 return (errors
!= 0);
6525 * zfs hold [-r] [-t] <tag> <snap> ...
6527 * -r Recursively hold
6529 * Apply a user-hold with the given tag to the list of snapshots.
6532 zfs_do_hold(int argc
, char **argv
)
6534 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
6538 * zfs release [-r] <tag> <snap> ...
6540 * -r Recursively release
6542 * Release a user-hold with the given tag from the list of snapshots.
6545 zfs_do_release(int argc
, char **argv
)
6547 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
6550 typedef struct holds_cbdata
{
6551 boolean_t cb_recursive
;
6552 const char *cb_snapname
;
6554 size_t cb_max_namelen
;
6555 size_t cb_max_taglen
;
6558 #define STRFTIME_FMT_STR "%a %b %e %H:%M %Y"
6559 #define DATETIME_BUF_LEN (32)
6564 print_holds(boolean_t scripted
, int nwidth
, int tagwidth
, nvlist_t
*nvl
,
6568 nvpair_t
*nvp
= NULL
;
6569 const char *const hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
6573 for (i
= 0; i
< 3; i
++) {
6574 col
= gettext(hdr_cols
[i
]);
6576 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
6579 (void) printf("%s\n", col
);
6583 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
6584 const char *zname
= nvpair_name(nvp
);
6586 nvpair_t
*nvp2
= NULL
;
6587 (void) nvpair_value_nvlist(nvp
, &nvl2
);
6588 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
6589 char tsbuf
[DATETIME_BUF_LEN
];
6590 const char *tagname
= nvpair_name(nvp2
);
6595 (void) nvpair_value_uint64(nvp2
, &val
);
6597 (void) localtime_r(&time
, &t
);
6598 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
6599 gettext(STRFTIME_FMT_STR
), &t
);
6603 (void) printf("%s\t%s\t%ld\n", zname
,
6606 (void) printf("%s\t%s\t%s\n", zname
,
6611 (void) printf("%-*s %-*s %ld\n",
6612 nwidth
, zname
, tagwidth
,
6615 (void) printf("%-*s %-*s %s\n",
6616 nwidth
, zname
, tagwidth
,
6625 * Generic callback function to list a dataset or snapshot.
6628 holds_callback(zfs_handle_t
*zhp
, void *data
)
6630 holds_cbdata_t
*cbp
= data
;
6631 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
6632 nvlist_t
*nvl
= NULL
;
6633 nvpair_t
*nvp
= NULL
;
6634 const char *zname
= zfs_get_name(zhp
);
6635 size_t znamelen
= strlen(zname
);
6637 if (cbp
->cb_recursive
) {
6638 const char *snapname
;
6639 char *delim
= strchr(zname
, '@');
6643 snapname
= delim
+ 1;
6644 if (strcmp(cbp
->cb_snapname
, snapname
))
6648 if (zfs_get_holds(zhp
, &nvl
) != 0)
6651 if (znamelen
> cbp
->cb_max_namelen
)
6652 cbp
->cb_max_namelen
= znamelen
;
6654 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
6655 const char *tag
= nvpair_name(nvp
);
6656 size_t taglen
= strlen(tag
);
6657 if (taglen
> cbp
->cb_max_taglen
)
6658 cbp
->cb_max_taglen
= taglen
;
6661 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
6665 * zfs holds [-rHp] <snap> ...
6667 * -r Lists holds that are set on the named snapshots recursively.
6668 * -H Scripted mode; elide headers and separate columns by tabs.
6669 * -p Display values in parsable (literal) format.
6672 zfs_do_holds(int argc
, char **argv
)
6675 boolean_t errors
= B_FALSE
;
6676 boolean_t scripted
= B_FALSE
;
6677 boolean_t recursive
= B_FALSE
;
6678 boolean_t parsable
= B_FALSE
;
6680 int types
= ZFS_TYPE_SNAPSHOT
;
6681 holds_cbdata_t cb
= { 0 };
6688 while ((c
= getopt(argc
, argv
, "rHp")) != -1) {
6700 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6707 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
6708 flags
|= ZFS_ITER_RECURSE
;
6714 /* check number of arguments */
6718 nvlist_t
*nvl
= fnvlist_alloc();
6720 for (int i
= 0; i
< argc
; ++i
) {
6721 char *snapshot
= argv
[i
];
6723 const char *snapname
;
6725 delim
= strchr(snapshot
, '@');
6726 if (delim
== NULL
) {
6727 (void) fprintf(stderr
,
6728 gettext("'%s' is not a snapshot\n"), snapshot
);
6732 snapname
= delim
+ 1;
6734 snapshot
[delim
- snapshot
] = '\0';
6736 cb
.cb_recursive
= recursive
;
6737 cb
.cb_snapname
= snapname
;
6741 * 1. collect holds data, set format options
6743 ret
= zfs_for_each(1, argv
+ i
, flags
, types
, NULL
, NULL
, limit
,
6744 holds_callback
, &cb
);
6750 * 2. print holds data
6752 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
,
6755 if (nvlist_empty(nvl
))
6756 (void) fprintf(stderr
, gettext("no datasets available\n"));
6763 #define CHECK_SPINNER 30
6764 #define SPINNER_TIME 3 /* seconds */
6765 #define MOUNT_TIME 1 /* seconds */
6767 typedef struct get_all_state
{
6770 boolean_t ga_verbose
;
6771 get_all_cb_t
*ga_cbp
;
6775 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
6777 static const char *const spin
[] = { "-", "\\", "|", "/" };
6778 static int spinval
= 0;
6779 static int spincheck
= 0;
6780 static time_t last_spin_time
= (time_t)0;
6781 get_all_state_t
*state
= data
;
6782 zfs_type_t type
= zfs_get_type(zhp
);
6784 if (state
->ga_verbose
) {
6785 if (--spincheck
< 0) {
6786 time_t now
= time(NULL
);
6787 if (last_spin_time
+ SPINNER_TIME
< now
) {
6788 update_progress(spin
[spinval
++ % 4]);
6789 last_spin_time
= now
;
6791 spincheck
= CHECK_SPINNER
;
6796 * Iterate over any nested datasets.
6798 if (zfs_iter_filesystems_v2(zhp
, 0, get_one_dataset
, data
) != 0) {
6804 * Skip any datasets whose type does not match.
6806 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
6810 libzfs_add_handle(state
->ga_cbp
, zhp
);
6811 assert(state
->ga_cbp
->cb_used
<= state
->ga_cbp
->cb_alloc
);
6817 get_recursive_datasets(zfs_handle_t
*zhp
, void *data
)
6819 get_all_state_t
*state
= data
;
6820 int len
= strlen(zfs_get_name(zhp
));
6821 for (int i
= 0; i
< state
->ga_count
; ++i
) {
6822 if (strcmp(state
->ga_datasets
[i
], zfs_get_name(zhp
)) == 0)
6823 return (get_one_dataset(zhp
, data
));
6824 else if ((strncmp(state
->ga_datasets
[i
], zfs_get_name(zhp
),
6825 len
) == 0) && state
->ga_datasets
[i
][len
] == '/') {
6826 (void) zfs_iter_filesystems_v2(zhp
, 0,
6827 get_recursive_datasets
, data
);
6835 get_all_datasets(get_all_state_t
*state
)
6837 if (state
->ga_verbose
)
6838 set_progress_header(gettext("Reading ZFS config"));
6839 if (state
->ga_datasets
== NULL
)
6840 (void) zfs_iter_root(g_zfs
, get_one_dataset
, state
);
6842 (void) zfs_iter_root(g_zfs
, get_recursive_datasets
, state
);
6844 if (state
->ga_verbose
)
6845 finish_progress(gettext("done."));
6849 * Generic callback for sharing or mounting filesystems. Because the code is so
6850 * similar, we have a common function with an extra parameter to determine which
6851 * mode we are using.
6853 typedef enum { OP_SHARE
, OP_MOUNT
} share_mount_op_t
;
6855 typedef struct share_mount_state
{
6856 share_mount_op_t sm_op
;
6857 boolean_t sm_verbose
;
6860 enum sa_protocol sm_proto
; /* only valid for OP_SHARE */
6861 pthread_mutex_t sm_lock
; /* protects the remaining fields */
6862 uint_t sm_total
; /* number of filesystems to process */
6863 uint_t sm_done
; /* number of filesystems processed */
6864 int sm_status
; /* -1 if any of the share/mount operations failed */
6865 } share_mount_state_t
;
6868 * Share or mount a dataset.
6871 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, enum sa_protocol protocol
,
6872 boolean_t
explicit, const char *options
)
6874 char mountpoint
[ZFS_MAXPROPLEN
];
6875 char shareopts
[ZFS_MAXPROPLEN
];
6876 char smbshareopts
[ZFS_MAXPROPLEN
];
6877 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
6879 uint64_t zoned
, canmount
;
6880 boolean_t shared_nfs
, shared_smb
;
6882 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
6885 * Check to make sure we can mount/share this dataset. If we
6886 * are in the global zone and the filesystem is exported to a
6887 * local zone, or if we are in a local zone and the
6888 * filesystem is not exported, then it is an error.
6890 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
6892 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
6896 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6897 "dataset is exported to a local zone\n"), cmdname
,
6901 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
6905 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6906 "permission denied\n"), cmdname
,
6912 * Ignore any filesystems which don't apply to us. This
6913 * includes those with a legacy mountpoint, or those with
6914 * legacy share options.
6916 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
6917 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
6918 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
6919 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
6920 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
6921 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
6923 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
6924 strcmp(smbshareopts
, "off") == 0) {
6928 (void) fprintf(stderr
, gettext("cannot share '%s': "
6929 "legacy share\n"), zfs_get_name(zhp
));
6930 (void) fprintf(stderr
, gettext("use exports(5) or "
6931 "smb.conf(5) to share this filesystem, or set "
6932 "the sharenfs or sharesmb property\n"));
6937 * We cannot share or mount legacy filesystems. If the
6938 * shareopts is non-legacy but the mountpoint is legacy, we
6939 * treat it as a legacy share.
6941 if (strcmp(mountpoint
, "legacy") == 0) {
6945 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6946 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
6947 (void) fprintf(stderr
, gettext("use %s(8) to "
6948 "%s this filesystem\n"), cmdname
, cmdname
);
6952 if (strcmp(mountpoint
, "none") == 0) {
6956 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
6957 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
6962 * canmount explicit outcome
6963 * on no pass through
6964 * on yes pass through
6966 * off yes display error, return 1
6967 * noauto no return 0
6968 * noauto yes pass through
6970 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
6971 if (canmount
== ZFS_CANMOUNT_OFF
) {
6975 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6976 "'canmount' property is set to 'off'\n"), cmdname
,
6979 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
6981 * When performing a 'zfs mount -a', we skip any mounts for
6982 * datasets that have 'noauto' set. Sharing a dataset with
6983 * 'noauto' set is only allowed if it's mounted.
6987 if (op
== OP_SHARE
&& !zfs_is_mounted(zhp
, NULL
)) {
6988 /* also purge it from existing exports */
6989 zfs_unshare(zhp
, mountpoint
, NULL
);
6995 * If this filesystem is encrypted and does not have
6996 * a loaded key, we can not mount it.
6998 if ((flags
& MS_CRYPT
) == 0 &&
6999 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) != ZIO_CRYPT_OFF
&&
7000 zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
) ==
7001 ZFS_KEYSTATUS_UNAVAILABLE
) {
7005 (void) fprintf(stderr
, gettext("cannot %s '%s': "
7006 "encryption key not loaded\n"), cmdname
, zfs_get_name(zhp
));
7011 * If this filesystem is inconsistent and has a receive resume
7012 * token, we can not mount it.
7014 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
7015 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
7016 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
7020 (void) fprintf(stderr
, gettext("cannot %s '%s': "
7021 "Contains partially-completed state from "
7022 "\"zfs receive -s\", which can be resumed with "
7023 "\"zfs send -t\"\n"),
7024 cmdname
, zfs_get_name(zhp
));
7028 if (zfs_prop_get_int(zhp
, ZFS_PROP_REDACTED
) && !(flags
& MS_FORCE
)) {
7032 (void) fprintf(stderr
, gettext("cannot %s '%s': "
7033 "Dataset is not complete, was created by receiving "
7034 "a redacted zfs send stream.\n"), cmdname
,
7040 * At this point, we have verified that the mountpoint and/or
7041 * shareopts are appropriate for auto management. If the
7042 * filesystem is already mounted or shared, return (failing
7043 * for explicit requests); otherwise mount or share the
7048 enum sa_protocol prot
[] = {SA_PROTOCOL_NFS
, SA_NO_PROTOCOL
};
7049 shared_nfs
= zfs_is_shared(zhp
, NULL
, prot
);
7050 *prot
= SA_PROTOCOL_SMB
;
7051 shared_smb
= zfs_is_shared(zhp
, NULL
, prot
);
7053 if ((shared_nfs
&& shared_smb
) ||
7054 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
7055 strcmp(smbshareopts
, "off") == 0) ||
7056 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
7057 strcmp(shareopts
, "off") == 0)) {
7061 (void) fprintf(stderr
, gettext("cannot share "
7062 "'%s': filesystem already shared\n"),
7067 if (!zfs_is_mounted(zhp
, NULL
) &&
7068 zfs_mount(zhp
, NULL
, flags
) != 0)
7072 if (zfs_share(zhp
, protocol
== SA_NO_PROTOCOL
? NULL
: prot
))
7079 mnt
.mnt_mntopts
= (char *)(options
?: "");
7081 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
7082 zfs_is_mounted(zhp
, NULL
)) {
7086 (void) fprintf(stderr
, gettext("cannot mount "
7087 "'%s': filesystem already mounted\n"),
7092 if (zfs_mount(zhp
, options
, flags
) != 0)
7101 * Reports progress in the form "(current/total)". Not thread-safe.
7104 report_mount_progress(int current
, int total
)
7106 static time_t last_progress_time
= 0;
7107 time_t now
= time(NULL
);
7110 /* display header if we're here for the first time */
7112 set_progress_header(gettext("Mounting ZFS filesystems"));
7113 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
7114 /* too soon to report again */
7118 last_progress_time
= now
;
7120 (void) sprintf(info
, "(%d/%d)", current
, total
);
7122 if (current
== total
)
7123 finish_progress(info
);
7125 update_progress(info
);
7129 * zfs_foreach_mountpoint() callback that mounts or shares one filesystem and
7130 * updates the progress meter.
7133 share_mount_one_cb(zfs_handle_t
*zhp
, void *arg
)
7135 share_mount_state_t
*sms
= arg
;
7138 ret
= share_mount_one(zhp
, sms
->sm_op
, sms
->sm_flags
, sms
->sm_proto
,
7139 B_FALSE
, sms
->sm_options
);
7141 pthread_mutex_lock(&sms
->sm_lock
);
7143 sms
->sm_status
= ret
;
7145 if (sms
->sm_verbose
)
7146 report_mount_progress(sms
->sm_done
, sms
->sm_total
);
7147 pthread_mutex_unlock(&sms
->sm_lock
);
7152 append_options(char *mntopts
, char *newopts
)
7154 int len
= strlen(mntopts
);
7156 /* original length plus new string to append plus 1 for the comma */
7157 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
7158 (void) fprintf(stderr
, gettext("the opts argument for "
7159 "'%s' option is too long (more than %d chars)\n"),
7160 "-o", MNT_LINE_MAX
);
7165 mntopts
[len
++] = ',';
7167 (void) strcpy(&mntopts
[len
], newopts
);
7170 static enum sa_protocol
7171 sa_protocol_decode(const char *protocol
)
7173 for (enum sa_protocol i
= 0; i
< ARRAY_SIZE(sa_protocol_names
); ++i
)
7174 if (strcmp(protocol
, sa_protocol_names
[i
]) == 0)
7177 (void) fputs(gettext("share type must be one of: "), stderr
);
7178 for (enum sa_protocol i
= 0;
7179 i
< ARRAY_SIZE(sa_protocol_names
); ++i
)
7180 (void) fprintf(stderr
, "%s%s",
7181 i
!= 0 ? ", " : "", sa_protocol_names
[i
]);
7182 (void) fputc('\n', stderr
);
7187 share_mount(int op
, int argc
, char **argv
)
7191 boolean_t verbose
= B_FALSE
;
7193 char *options
= NULL
;
7195 const uint_t mount_nthr
= 512;
7199 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":aRlvo:Of" : "al"))
7215 if (*optarg
== '\0') {
7216 (void) fprintf(stderr
, gettext("empty mount "
7217 "options (-o) specified\n"));
7221 if (options
== NULL
)
7222 options
= safe_malloc(MNT_LINE_MAX
+ 1);
7224 /* option validation is done later */
7225 append_options(options
, optarg
);
7228 flags
|= MS_OVERLAY
;
7234 (void) fprintf(stderr
, gettext("missing argument for "
7235 "'%c' option\n"), optopt
);
7239 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7248 /* check number of arguments */
7249 if (do_all
|| recursive
) {
7250 enum sa_protocol protocol
= SA_NO_PROTOCOL
;
7252 if (op
== OP_SHARE
&& argc
> 0) {
7253 protocol
= sa_protocol_decode(argv
[0]);
7258 if (argc
!= 0 && do_all
) {
7259 (void) fprintf(stderr
, gettext("too many arguments\n"));
7263 if (argc
== 0 && recursive
) {
7264 (void) fprintf(stderr
,
7265 gettext("no dataset provided\n"));
7269 start_progress_timer();
7270 get_all_cb_t cb
= { 0 };
7271 get_all_state_t state
= { 0 };
7273 state
.ga_datasets
= NULL
;
7274 state
.ga_count
= -1;
7277 for (int i
= 0; i
< argc
; i
++) {
7278 zhp
= zfs_open(g_zfs
, argv
[i
],
7279 ZFS_TYPE_FILESYSTEM
);
7284 state
.ga_datasets
= argv
;
7285 state
.ga_count
= argc
;
7287 state
.ga_verbose
= verbose
;
7289 get_all_datasets(&state
);
7291 if (cb
.cb_used
== 0) {
7296 share_mount_state_t share_mount_state
= { 0 };
7297 share_mount_state
.sm_op
= op
;
7298 share_mount_state
.sm_verbose
= verbose
;
7299 share_mount_state
.sm_flags
= flags
;
7300 share_mount_state
.sm_options
= options
;
7301 share_mount_state
.sm_proto
= protocol
;
7302 share_mount_state
.sm_total
= cb
.cb_used
;
7303 pthread_mutex_init(&share_mount_state
.sm_lock
, NULL
);
7305 /* For a 'zfs share -a' operation start with a clean slate. */
7307 zfs_truncate_shares(NULL
);
7310 * libshare isn't mt-safe, so only do the operation in parallel
7311 * if we're mounting. Additionally, the key-loading option must
7312 * be serialized so that we can prompt the user for their keys
7313 * in a consistent manner.
7315 nthr
= op
== OP_MOUNT
&& !(flags
& MS_CRYPT
) ? mount_nthr
: 1;
7316 zfs_foreach_mountpoint(g_zfs
, cb
.cb_handles
, cb
.cb_used
,
7317 share_mount_one_cb
, &share_mount_state
, nthr
);
7318 zfs_commit_shares(NULL
);
7320 ret
= share_mount_state
.sm_status
;
7322 for (int i
= 0; i
< cb
.cb_used
; i
++)
7323 zfs_close(cb
.cb_handles
[i
]);
7324 free(cb
.cb_handles
);
7325 } else if (argc
== 0) {
7327 struct mnttab entry
;
7329 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
7330 (void) fprintf(stderr
, gettext("missing filesystem "
7331 "argument (specify -a for all)\n"));
7336 * When mount is given no arguments, go through
7337 * /proc/self/mounts and display any active ZFS mounts.
7338 * We hide any snapshots, since they are controlled
7342 if ((mnttab
= fopen(MNTTAB
, "re")) == NULL
) {
7347 while (getmntent(mnttab
, &entry
) == 0) {
7348 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
7349 strchr(entry
.mnt_special
, '@') != NULL
)
7352 (void) printf("%-30s %s\n", entry
.mnt_special
,
7356 (void) fclose(mnttab
);
7361 (void) fprintf(stderr
,
7362 gettext("too many arguments\n"));
7366 if ((zhp
= zfs_open(g_zfs
, argv
[0],
7367 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
7370 ret
= share_mount_one(zhp
, op
, flags
, SA_NO_PROTOCOL
,
7372 zfs_commit_shares(NULL
);
7383 * zfs mount filesystem
7385 * Mount all filesystems, or mount the given filesystem.
7388 zfs_do_mount(int argc
, char **argv
)
7390 return (share_mount(OP_MOUNT
, argc
, argv
));
7394 * zfs share -a [nfs | smb]
7395 * zfs share filesystem
7397 * Share all filesystems, or share the given filesystem.
7400 zfs_do_share(int argc
, char **argv
)
7402 return (share_mount(OP_SHARE
, argc
, argv
));
7405 typedef struct unshare_unmount_node
{
7406 zfs_handle_t
*un_zhp
;
7408 uu_avl_node_t un_avlnode
;
7409 } unshare_unmount_node_t
;
7412 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
7415 const unshare_unmount_node_t
*l
= larg
;
7416 const unshare_unmount_node_t
*r
= rarg
;
7418 return (strcmp(l
->un_mountp
, r
->un_mountp
));
7422 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
7423 * absolute path, find the entry /proc/self/mounts, verify that it's a
7424 * ZFS filesystem, and unmount it appropriately.
7427 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
7431 struct stat64 statbuf
;
7432 struct extmnttab entry
;
7433 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
7437 * Search for the given (major,minor) pair in the mount table.
7440 if (getextmntent(path
, &entry
, &statbuf
) != 0) {
7441 if (op
== OP_SHARE
) {
7442 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
7443 "currently mounted\n"), cmdname
, path
);
7446 (void) fprintf(stderr
, gettext("warning: %s not in"
7447 "/proc/self/mounts\n"), path
);
7448 if ((ret
= umount2(path
, flags
)) != 0)
7449 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
7453 path_inode
= statbuf
.st_ino
;
7455 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
7456 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
7457 "filesystem\n"), cmdname
, path
);
7461 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
7462 ZFS_TYPE_FILESYSTEM
)) == NULL
)
7466 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
7467 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
7468 cmdname
, path
, strerror(errno
));
7470 } else if (statbuf
.st_ino
!= path_inode
) {
7471 (void) fprintf(stderr
, gettext("cannot "
7472 "%s '%s': not a mountpoint\n"), cmdname
, path
);
7476 if (op
== OP_SHARE
) {
7477 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
7478 char smbshare_prop
[ZFS_MAXPROPLEN
];
7480 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
7481 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
7482 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
7483 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
7485 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
7486 strcmp(smbshare_prop
, "off") == 0) {
7487 (void) fprintf(stderr
, gettext("cannot unshare "
7488 "'%s': legacy share\n"), path
);
7489 (void) fprintf(stderr
, gettext("use exportfs(8) "
7490 "or smbcontrol(1) to unshare this filesystem\n"));
7491 } else if (!zfs_is_shared(zhp
, NULL
, NULL
)) {
7492 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
7493 "not currently shared\n"), path
);
7495 ret
= zfs_unshare(zhp
, path
, NULL
);
7496 zfs_commit_shares(NULL
);
7499 char mtpt_prop
[ZFS_MAXPROPLEN
];
7501 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
7502 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
7505 ret
= zfs_unmount(zhp
, NULL
, flags
);
7506 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
7507 (void) fprintf(stderr
, gettext("cannot unmount "
7508 "'%s': legacy mountpoint\n"),
7510 (void) fprintf(stderr
, gettext("use umount(8) "
7511 "to unmount this filesystem\n"));
7513 ret
= zfs_unmountall(zhp
, flags
);
7524 * Generic callback for unsharing or unmounting a filesystem.
7527 unshare_unmount(int op
, int argc
, char **argv
)
7534 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
7535 char sharesmb
[ZFS_MAXPROPLEN
];
7538 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? ":a" : "afu")) != -1) {
7550 (void) fprintf(stderr
, gettext("missing argument for "
7551 "'%c' option\n"), optopt
);
7555 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7566 * We could make use of zfs_for_each() to walk all datasets in
7567 * the system, but this would be very inefficient, especially
7568 * since we would have to linearly search /proc/self/mounts for
7569 * each one. Instead, do one pass through /proc/self/mounts
7570 * looking for zfs entries and call zfs_unmount() for each one.
7572 * Things get a little tricky if the administrator has created
7573 * mountpoints beneath other ZFS filesystems. In this case, we
7574 * have to unmount the deepest filesystems first. To accomplish
7575 * this, we place all the mountpoints in an AVL tree sorted by
7576 * the special type (dataset name), and walk the result in
7577 * reverse to make sure to get any snapshots first.
7580 struct mnttab entry
;
7581 uu_avl_pool_t
*pool
;
7582 uu_avl_t
*tree
= NULL
;
7583 unshare_unmount_node_t
*node
;
7585 uu_avl_walk_t
*walk
;
7586 enum sa_protocol
*protocol
= NULL
,
7587 single_protocol
[] = {SA_NO_PROTOCOL
, SA_NO_PROTOCOL
};
7589 if (op
== OP_SHARE
&& argc
> 0) {
7590 *single_protocol
= sa_protocol_decode(argv
[0]);
7591 protocol
= single_protocol
;
7597 (void) fprintf(stderr
, gettext("too many arguments\n"));
7601 if (((pool
= uu_avl_pool_create("unmount_pool",
7602 sizeof (unshare_unmount_node_t
),
7603 offsetof(unshare_unmount_node_t
, un_avlnode
),
7604 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
7605 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
7608 if ((mnttab
= fopen(MNTTAB
, "re")) == NULL
) {
7609 uu_avl_destroy(tree
);
7610 uu_avl_pool_destroy(pool
);
7614 while (getmntent(mnttab
, &entry
) == 0) {
7616 /* ignore non-ZFS entries */
7617 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
7620 /* ignore snapshots */
7621 if (strchr(entry
.mnt_special
, '@') != NULL
)
7624 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
7625 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
7631 * Ignore datasets that are excluded/restricted by
7634 if (zpool_skip_pool(zfs_get_pool_name(zhp
))) {
7641 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
7643 sizeof (nfs_mnt_prop
),
7644 NULL
, NULL
, 0, B_FALSE
) == 0);
7645 if (strcmp(nfs_mnt_prop
, "off") != 0)
7647 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
7649 sizeof (nfs_mnt_prop
),
7650 NULL
, NULL
, 0, B_FALSE
) == 0);
7651 if (strcmp(nfs_mnt_prop
, "off") == 0)
7655 /* Ignore legacy mounts */
7656 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
7658 sizeof (nfs_mnt_prop
),
7659 NULL
, NULL
, 0, B_FALSE
) == 0);
7660 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
7662 /* Ignore canmount=noauto mounts */
7663 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
7664 ZFS_CANMOUNT_NOAUTO
)
7671 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
7673 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
7675 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
7677 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
7678 uu_avl_insert(tree
, node
, idx
);
7680 zfs_close(node
->un_zhp
);
7681 free(node
->un_mountp
);
7685 (void) fclose(mnttab
);
7688 * Walk the AVL tree in reverse, unmounting each filesystem and
7689 * removing it from the AVL tree in the process.
7691 if ((walk
= uu_avl_walk_start(tree
,
7692 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
7695 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
7696 const char *mntarg
= NULL
;
7698 uu_avl_remove(tree
, node
);
7701 if (zfs_unshare(node
->un_zhp
,
7702 node
->un_mountp
, protocol
) != 0)
7707 if (zfs_unmount(node
->un_zhp
,
7708 mntarg
, flags
) != 0)
7713 zfs_close(node
->un_zhp
);
7714 free(node
->un_mountp
);
7719 zfs_commit_shares(protocol
);
7721 uu_avl_walk_end(walk
);
7722 uu_avl_destroy(tree
);
7723 uu_avl_pool_destroy(pool
);
7728 (void) fprintf(stderr
,
7729 gettext("missing filesystem argument\n"));
7731 (void) fprintf(stderr
,
7732 gettext("too many arguments\n"));
7737 * We have an argument, but it may be a full path or a ZFS
7738 * filesystem. Pass full paths off to unmount_path() (shared by
7739 * manual_unmount), otherwise open the filesystem and pass to
7742 if (argv
[0][0] == '/')
7743 return (unshare_unmount_path(op
, argv
[0],
7746 if ((zhp
= zfs_open(g_zfs
, argv
[0],
7747 ZFS_TYPE_FILESYSTEM
)) == NULL
)
7750 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
7751 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
7752 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
7753 NULL
, 0, B_FALSE
) == 0);
7757 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
7759 sizeof (nfs_mnt_prop
),
7760 NULL
, NULL
, 0, B_FALSE
) == 0);
7761 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
7762 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
7765 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
7766 strcmp(sharesmb
, "off") == 0) {
7767 (void) fprintf(stderr
, gettext("cannot "
7768 "unshare '%s': legacy share\n"),
7770 (void) fprintf(stderr
, gettext("use "
7771 "exports(5) or smb.conf(5) to unshare "
7772 "this filesystem\n"));
7774 } else if (!zfs_is_shared(zhp
, NULL
, NULL
)) {
7775 (void) fprintf(stderr
, gettext("cannot "
7776 "unshare '%s': not currently "
7777 "shared\n"), zfs_get_name(zhp
));
7779 } else if (zfs_unshareall(zhp
, NULL
) != 0) {
7785 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
7786 (void) fprintf(stderr
, gettext("cannot "
7787 "unmount '%s': legacy "
7788 "mountpoint\n"), zfs_get_name(zhp
));
7789 (void) fprintf(stderr
, gettext("use "
7790 "umount(8) to unmount this "
7793 } else if (!zfs_is_mounted(zhp
, NULL
)) {
7794 (void) fprintf(stderr
, gettext("cannot "
7795 "unmount '%s': not currently "
7799 } else if (zfs_unmountall(zhp
, flags
) != 0) {
7812 * zfs unmount [-fu] -a
7813 * zfs unmount [-fu] filesystem
7815 * Unmount all filesystems, or a specific ZFS filesystem.
7818 zfs_do_unmount(int argc
, char **argv
)
7820 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
7825 * zfs unshare filesystem
7827 * Unshare all filesystems, or a specific ZFS filesystem.
7830 zfs_do_unshare(int argc
, char **argv
)
7832 return (unshare_unmount(OP_SHARE
, argc
, argv
));
7836 find_command_idx(const char *command
, int *idx
)
7840 for (i
= 0; i
< NCOMMAND
; i
++) {
7841 if (command_table
[i
].name
== NULL
)
7844 if (strcmp(command
, command_table
[i
].name
) == 0) {
7853 zfs_do_diff(int argc
, char **argv
)
7857 char *tosnap
= NULL
;
7858 char *fromsnap
= NULL
;
7862 struct sigaction sa
;
7864 while ((c
= getopt(argc
, argv
, "FHth")) != -1) {
7867 flags
|= ZFS_DIFF_CLASSIFY
;
7870 flags
|= ZFS_DIFF_PARSEABLE
;
7873 flags
|= ZFS_DIFF_TIMESTAMP
;
7876 flags
|= ZFS_DIFF_NO_MANGLE
;
7879 (void) fprintf(stderr
,
7880 gettext("invalid option '%c'\n"), optopt
);
7889 (void) fprintf(stderr
,
7890 gettext("must provide at least one snapshot name\n"));
7895 (void) fprintf(stderr
, gettext("too many arguments\n"));
7900 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
7903 if (*fromsnap
!= '@')
7904 copy
= strdup(fromsnap
);
7906 copy
= strdup(tosnap
);
7910 if ((atp
= strchr(copy
, '@')) != NULL
)
7913 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
) {
7920 * Ignore SIGPIPE so that the library can give us
7921 * information on any failure
7923 if (sigemptyset(&sa
.sa_mask
) == -1) {
7928 sa
.sa_handler
= SIG_IGN
;
7929 if (sigaction(SIGPIPE
, &sa
, NULL
) == -1) {
7934 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
7942 * zfs bookmark <fs@source>|<fs#source> <fs#bookmark>
7944 * Creates a bookmark with the given name from the source snapshot
7945 * or creates a copy of an existing source bookmark.
7948 zfs_do_bookmark(int argc
, char **argv
)
7950 char *source
, *bookname
;
7951 char expbuf
[ZFS_MAX_DATASET_NAME_LEN
];
7958 while ((c
= getopt(argc
, argv
, "")) != -1) {
7961 (void) fprintf(stderr
,
7962 gettext("invalid option '%c'\n"), optopt
);
7970 /* check number of arguments */
7972 (void) fprintf(stderr
, gettext("missing source argument\n"));
7976 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
7983 if (strchr(source
, '@') == NULL
&& strchr(source
, '#') == NULL
) {
7984 (void) fprintf(stderr
,
7985 gettext("invalid source name '%s': "
7986 "must contain a '@' or '#'\n"), source
);
7989 if (strchr(bookname
, '#') == NULL
) {
7990 (void) fprintf(stderr
,
7991 gettext("invalid bookmark name '%s': "
7992 "must contain a '#'\n"), bookname
);
7997 * expand source or bookname to full path:
7998 * one of them may be specified as short name
8002 char *source_short
, *bookname_short
;
8003 source_short
= strpbrk(source
, "@#");
8004 bookname_short
= strpbrk(bookname
, "#");
8005 if (source_short
== source
&&
8006 bookname_short
== bookname
) {
8007 (void) fprintf(stderr
, gettext(
8008 "either source or bookmark must be specified as "
8009 "full dataset paths"));
8011 } else if (source_short
!= source
&&
8012 bookname_short
!= bookname
) {
8014 } else if (source_short
!= source
) {
8015 strlcpy(expbuf
, source
, sizeof (expbuf
));
8017 } else if (bookname_short
!= bookname
) {
8018 strlcpy(expbuf
, bookname
, sizeof (expbuf
));
8023 if (expand
!= NULL
) {
8024 *strpbrk(expbuf
, "@#") = '\0'; /* dataset name in buf */
8025 (void) strlcat(expbuf
, *expand
, sizeof (expbuf
));
8030 /* determine source type */
8031 switch (*strpbrk(source
, "@#")) {
8032 case '@': source_type
= ZFS_TYPE_SNAPSHOT
; break;
8033 case '#': source_type
= ZFS_TYPE_BOOKMARK
; break;
8037 /* test the source exists */
8039 zhp
= zfs_open(g_zfs
, source
, source_type
);
8044 nvl
= fnvlist_alloc();
8045 fnvlist_add_string(nvl
, bookname
, source
);
8046 ret
= lzc_bookmark(nvl
, NULL
);
8050 const char *err_msg
= NULL
;
8053 (void) snprintf(errbuf
, sizeof (errbuf
),
8054 dgettext(TEXT_DOMAIN
,
8055 "cannot create bookmark '%s'"), bookname
);
8059 err_msg
= "bookmark is in a different pool";
8061 case ZFS_ERR_BOOKMARK_SOURCE_NOT_ANCESTOR
:
8062 err_msg
= "source is not an ancestor of the "
8063 "new bookmark's dataset";
8066 err_msg
= "bookmark exists";
8069 err_msg
= "invalid argument";
8072 err_msg
= "bookmark feature not enabled";
8075 err_msg
= "out of space";
8078 err_msg
= "dataset does not exist";
8081 (void) zfs_standard_error(g_zfs
, ret
, errbuf
);
8084 if (err_msg
!= NULL
) {
8085 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
8086 dgettext(TEXT_DOMAIN
, err_msg
));
8098 zfs_do_channel_program(int argc
, char **argv
)
8101 size_t progsize
, progread
;
8102 nvlist_t
*outnvl
= NULL
;
8103 uint64_t instrlimit
= ZCP_DEFAULT_INSTRLIMIT
;
8104 uint64_t memlimit
= ZCP_DEFAULT_MEMLIMIT
;
8105 boolean_t sync_flag
= B_TRUE
, json_output
= B_FALSE
;
8106 zpool_handle_t
*zhp
;
8109 while ((c
= getopt(argc
, argv
, "nt:m:j")) != -1) {
8117 arg
= strtoull(optarg
, &endp
, 0);
8118 if (errno
!= 0 || *endp
!= '\0') {
8119 (void) fprintf(stderr
, gettext(
8121 "'%s': expected integer\n"), optarg
);
8128 ASSERT3U(c
, ==, 'm');
8134 sync_flag
= B_FALSE
;
8138 json_output
= B_TRUE
;
8142 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8152 (void) fprintf(stderr
,
8153 gettext("invalid number of arguments\n"));
8157 const char *poolname
= argv
[0];
8158 const char *filename
= argv
[1];
8159 if (strcmp(filename
, "-") == 0) {
8161 filename
= "standard input";
8162 } else if ((fd
= open(filename
, O_RDONLY
)) < 0) {
8163 (void) fprintf(stderr
, gettext("cannot open '%s': %s\n"),
8164 filename
, strerror(errno
));
8168 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
8169 (void) fprintf(stderr
, gettext("cannot open pool '%s'\n"),
8178 * Read in the channel program, expanding the program buffer as
8183 char *progbuf
= safe_malloc(progsize
);
8185 ret
= read(fd
, progbuf
+ progread
, progsize
- progread
);
8187 if (progread
== progsize
&& ret
> 0) {
8189 progbuf
= safe_realloc(progbuf
, progsize
);
8197 (void) fprintf(stderr
,
8198 gettext("cannot read '%s': %s\n"),
8199 filename
, strerror(errno
));
8202 progbuf
[progread
] = '\0';
8205 * Any remaining arguments are passed as arguments to the lua script as
8208 * "argv" -> [ "arg 1", ... "arg n" ],
8211 nvlist_t
*argnvl
= fnvlist_alloc();
8212 fnvlist_add_string_array(argnvl
, ZCP_ARG_CLIARGV
,
8213 (const char **)argv
+ 2, argc
- 2);
8216 ret
= lzc_channel_program(poolname
, progbuf
,
8217 instrlimit
, memlimit
, argnvl
, &outnvl
);
8219 ret
= lzc_channel_program_nosync(poolname
, progbuf
,
8220 instrlimit
, memlimit
, argnvl
, &outnvl
);
8225 * On error, report the error message handed back by lua if one
8226 * exists. Otherwise, generate an appropriate error message,
8227 * falling back on strerror() for an unexpected return code.
8229 const char *errstring
= NULL
;
8230 const char *msg
= gettext("Channel program execution failed");
8231 uint64_t instructions
= 0;
8232 if (outnvl
!= NULL
&& nvlist_exists(outnvl
, ZCP_RET_ERROR
)) {
8233 const char *es
= NULL
;
8234 (void) nvlist_lookup_string(outnvl
,
8235 ZCP_RET_ERROR
, &es
);
8237 errstring
= strerror(ret
);
8241 (void) nvlist_lookup_uint64(outnvl
,
8242 ZCP_ARG_INSTRLIMIT
, &instructions
);
8248 "Invalid instruction or memory limit.";
8251 errstring
= "Return value too large.";
8254 errstring
= "Memory limit exhausted.";
8257 errstring
= "Timed out.";
8260 errstring
= "Permission denied. Channel "
8261 "programs must be run as root.";
8264 (void) zfs_standard_error(g_zfs
, ret
, msg
);
8267 if (errstring
!= NULL
)
8268 (void) fprintf(stderr
, "%s:\n%s\n", msg
, errstring
);
8270 if (ret
== ETIME
&& instructions
!= 0)
8271 (void) fprintf(stderr
,
8272 gettext("%llu Lua instructions\n"),
8273 (u_longlong_t
)instructions
);
8276 (void) nvlist_print_json(stdout
, outnvl
);
8277 } else if (nvlist_empty(outnvl
)) {
8278 (void) fprintf(stdout
, gettext("Channel program fully "
8279 "executed and did not produce output.\n"));
8281 (void) fprintf(stdout
, gettext("Channel program fully "
8282 "executed and produced output:\n"));
8283 dump_nvlist(outnvl
, 4);
8288 fnvlist_free(outnvl
);
8289 fnvlist_free(argnvl
);
8298 typedef struct loadkey_cbdata
{
8299 boolean_t cb_loadkey
;
8300 boolean_t cb_recursive
;
8302 char *cb_keylocation
;
8303 uint64_t cb_numfailed
;
8304 uint64_t cb_numattempted
;
8308 load_key_callback(zfs_handle_t
*zhp
, void *data
)
8311 boolean_t is_encroot
;
8312 loadkey_cbdata_t
*cb
= data
;
8313 uint64_t keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
8316 * If we are working recursively, we want to skip loading / unloading
8317 * keys for non-encryption roots and datasets whose keys are already
8318 * in the desired end-state.
8320 if (cb
->cb_recursive
) {
8321 ret
= zfs_crypto_get_encryption_root(zhp
, &is_encroot
, NULL
);
8327 if ((cb
->cb_loadkey
&& keystatus
== ZFS_KEYSTATUS_AVAILABLE
) ||
8328 (!cb
->cb_loadkey
&& keystatus
== ZFS_KEYSTATUS_UNAVAILABLE
))
8332 cb
->cb_numattempted
++;
8335 ret
= zfs_crypto_load_key(zhp
, cb
->cb_noop
, cb
->cb_keylocation
);
8337 ret
= zfs_crypto_unload_key(zhp
);
8348 load_unload_keys(int argc
, char **argv
, boolean_t loadkey
)
8350 int c
, ret
= 0, flags
= 0;
8351 boolean_t do_all
= B_FALSE
;
8352 loadkey_cbdata_t cb
= { 0 };
8354 cb
.cb_loadkey
= loadkey
;
8356 while ((c
= getopt(argc
, argv
, "anrL:")) != -1) {
8357 /* noop and alternate keylocations only apply to zfs load-key */
8361 cb
.cb_noop
= B_TRUE
;
8364 cb
.cb_keylocation
= optarg
;
8374 cb
.cb_recursive
= B_TRUE
;
8377 flags
|= ZFS_ITER_RECURSE
;
8378 cb
.cb_recursive
= B_TRUE
;
8381 (void) fprintf(stderr
,
8382 gettext("invalid option '%c'\n"), optopt
);
8390 if (!do_all
&& argc
== 0) {
8391 (void) fprintf(stderr
,
8392 gettext("Missing dataset argument or -a option\n"));
8396 if (do_all
&& argc
!= 0) {
8397 (void) fprintf(stderr
,
8398 gettext("Cannot specify dataset with -a option\n"));
8402 if (cb
.cb_recursive
&& cb
.cb_keylocation
!= NULL
&&
8403 strcmp(cb
.cb_keylocation
, "prompt") != 0) {
8404 (void) fprintf(stderr
, gettext("alternate keylocation may only "
8405 "be 'prompt' with -r or -a\n"));
8409 ret
= zfs_for_each(argc
, argv
, flags
,
8410 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
, NULL
, NULL
, 0,
8411 load_key_callback
, &cb
);
8413 if (cb
.cb_noop
|| (cb
.cb_recursive
&& cb
.cb_numattempted
!= 0)) {
8414 (void) printf(gettext("%llu / %llu key(s) successfully %s\n"),
8415 (u_longlong_t
)(cb
.cb_numattempted
- cb
.cb_numfailed
),
8416 (u_longlong_t
)cb
.cb_numattempted
,
8417 loadkey
? (cb
.cb_noop
? "verified" : "loaded") :
8421 if (cb
.cb_numfailed
!= 0)
8428 zfs_do_load_key(int argc
, char **argv
)
8430 return (load_unload_keys(argc
, argv
, B_TRUE
));
8435 zfs_do_unload_key(int argc
, char **argv
)
8437 return (load_unload_keys(argc
, argv
, B_FALSE
));
8441 zfs_do_change_key(int argc
, char **argv
)
8445 boolean_t loadkey
= B_FALSE
, inheritkey
= B_FALSE
;
8446 zfs_handle_t
*zhp
= NULL
;
8447 nvlist_t
*props
= fnvlist_alloc();
8449 while ((c
= getopt(argc
, argv
, "lio:")) != -1) {
8455 inheritkey
= B_TRUE
;
8458 if (!parseprop(props
, optarg
)) {
8464 (void) fprintf(stderr
,
8465 gettext("invalid option '%c'\n"), optopt
);
8470 if (inheritkey
&& !nvlist_empty(props
)) {
8471 (void) fprintf(stderr
,
8472 gettext("Properties not allowed for inheriting\n"));
8480 (void) fprintf(stderr
, gettext("Missing dataset argument\n"));
8485 (void) fprintf(stderr
, gettext("Too many arguments\n"));
8489 zhp
= zfs_open(g_zfs
, argv
[argc
- 1],
8490 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
8495 keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
8496 if (keystatus
!= ZFS_KEYSTATUS_AVAILABLE
) {
8497 ret
= zfs_crypto_load_key(zhp
, B_FALSE
, NULL
);
8505 /* refresh the properties so the new keystatus is visible */
8506 zfs_refresh_properties(zhp
);
8509 ret
= zfs_crypto_rewrap(zhp
, props
, inheritkey
);
8522 * 1) zfs project [-d|-r] <file|directory ...>
8523 * List project ID and inherit flag of file(s) or directories.
8524 * -d: List the directory itself, not its children.
8525 * -r: List subdirectories recursively.
8527 * 2) zfs project -C [-k] [-r] <file|directory ...>
8528 * Clear project inherit flag and/or ID on the file(s) or directories.
8529 * -k: Keep the project ID unchanged. If not specified, the project ID
8530 * will be reset as zero.
8531 * -r: Clear on subdirectories recursively.
8533 * 3) zfs project -c [-0] [-d|-r] [-p id] <file|directory ...>
8534 * Check project ID and inherit flag on the file(s) or directories,
8535 * report the outliers.
8536 * -0: Print file name followed by a NUL instead of newline.
8537 * -d: Check the directory itself, not its children.
8538 * -p: Specify the referenced ID for comparing with the target file(s)
8539 * or directories' project IDs. If not specified, the target (top)
8540 * directory's project ID will be used as the referenced one.
8541 * -r: Check subdirectories recursively.
8543 * 4) zfs project [-p id] [-r] [-s] <file|directory ...>
8544 * Set project ID and/or inherit flag on the file(s) or directories.
8545 * -p: Set the project ID as the given id.
8546 * -r: Set on subdirectories recursively. If not specify "-p" option,
8547 * it will use top-level directory's project ID as the given id,
8548 * then set both project ID and inherit flag on all descendants
8549 * of the top-level directory.
8550 * -s: Set project inherit flag.
8553 zfs_do_project(int argc
, char **argv
)
8555 zfs_project_control_t zpc
= {
8556 .zpc_expected_projid
= ZFS_INVALID_PROJID
,
8557 .zpc_op
= ZFS_PROJECT_OP_DEFAULT
,
8558 .zpc_dironly
= B_FALSE
,
8559 .zpc_keep_projid
= B_FALSE
,
8560 .zpc_newline
= B_TRUE
,
8561 .zpc_recursive
= B_FALSE
,
8562 .zpc_set_flag
= B_FALSE
,
8569 while ((c
= getopt(argc
, argv
, "0Ccdkp:rs")) != -1) {
8572 zpc
.zpc_newline
= B_FALSE
;
8575 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
8576 (void) fprintf(stderr
, gettext("cannot "
8577 "specify '-C' '-c' '-s' together\n"));
8581 zpc
.zpc_op
= ZFS_PROJECT_OP_CLEAR
;
8584 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
8585 (void) fprintf(stderr
, gettext("cannot "
8586 "specify '-C' '-c' '-s' together\n"));
8590 zpc
.zpc_op
= ZFS_PROJECT_OP_CHECK
;
8593 zpc
.zpc_dironly
= B_TRUE
;
8594 /* overwrite "-r" option */
8595 zpc
.zpc_recursive
= B_FALSE
;
8598 zpc
.zpc_keep_projid
= B_TRUE
;
8604 zpc
.zpc_expected_projid
= strtoull(optarg
, &endptr
, 0);
8605 if (errno
!= 0 || *endptr
!= '\0') {
8606 (void) fprintf(stderr
,
8607 gettext("project ID must be less than "
8608 "%u\n"), UINT32_MAX
);
8611 if (zpc
.zpc_expected_projid
>= UINT32_MAX
) {
8612 (void) fprintf(stderr
,
8613 gettext("invalid project ID\n"));
8619 zpc
.zpc_recursive
= B_TRUE
;
8620 /* overwrite "-d" option */
8621 zpc
.zpc_dironly
= B_FALSE
;
8624 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
8625 (void) fprintf(stderr
, gettext("cannot "
8626 "specify '-C' '-c' '-s' together\n"));
8630 zpc
.zpc_set_flag
= B_TRUE
;
8631 zpc
.zpc_op
= ZFS_PROJECT_OP_SET
;
8634 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8640 if (zpc
.zpc_op
== ZFS_PROJECT_OP_DEFAULT
) {
8641 if (zpc
.zpc_expected_projid
!= ZFS_INVALID_PROJID
)
8642 zpc
.zpc_op
= ZFS_PROJECT_OP_SET
;
8644 zpc
.zpc_op
= ZFS_PROJECT_OP_LIST
;
8647 switch (zpc
.zpc_op
) {
8648 case ZFS_PROJECT_OP_LIST
:
8649 if (zpc
.zpc_keep_projid
) {
8650 (void) fprintf(stderr
,
8651 gettext("'-k' is only valid together with '-C'\n"));
8654 if (!zpc
.zpc_newline
) {
8655 (void) fprintf(stderr
,
8656 gettext("'-0' is only valid together with '-c'\n"));
8660 case ZFS_PROJECT_OP_CHECK
:
8661 if (zpc
.zpc_keep_projid
) {
8662 (void) fprintf(stderr
,
8663 gettext("'-k' is only valid together with '-C'\n"));
8667 case ZFS_PROJECT_OP_CLEAR
:
8668 if (zpc
.zpc_dironly
) {
8669 (void) fprintf(stderr
,
8670 gettext("'-d' is useless together with '-C'\n"));
8673 if (!zpc
.zpc_newline
) {
8674 (void) fprintf(stderr
,
8675 gettext("'-0' is only valid together with '-c'\n"));
8678 if (zpc
.zpc_expected_projid
!= ZFS_INVALID_PROJID
) {
8679 (void) fprintf(stderr
,
8680 gettext("'-p' is useless together with '-C'\n"));
8684 case ZFS_PROJECT_OP_SET
:
8685 if (zpc
.zpc_dironly
) {
8686 (void) fprintf(stderr
,
8687 gettext("'-d' is useless for set project ID and/or "
8691 if (zpc
.zpc_keep_projid
) {
8692 (void) fprintf(stderr
,
8693 gettext("'-k' is only valid together with '-C'\n"));
8696 if (!zpc
.zpc_newline
) {
8697 (void) fprintf(stderr
,
8698 gettext("'-0' is only valid together with '-c'\n"));
8710 (void) fprintf(stderr
,
8711 gettext("missing file or directory target(s)\n"));
8715 for (int i
= 0; i
< argc
; i
++) {
8718 err
= zfs_project_handle(argv
[i
], &zpc
);
8727 zfs_do_wait(int argc
, char **argv
)
8729 boolean_t enabled
[ZFS_WAIT_NUM_ACTIVITIES
];
8733 /* By default, wait for all types of activity. */
8734 for (i
= 0; i
< ZFS_WAIT_NUM_ACTIVITIES
; i
++)
8735 enabled
[i
] = B_TRUE
;
8737 while ((c
= getopt(argc
, argv
, "t:")) != -1) {
8740 /* Reset activities array */
8741 memset(&enabled
, 0, sizeof (enabled
));
8743 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
8744 static const char *const col_subopts
[
8745 ZFS_WAIT_NUM_ACTIVITIES
] = { "deleteq" };
8747 for (i
= 0; i
< ARRAY_SIZE(col_subopts
); ++i
)
8748 if (strcmp(tok
, col_subopts
[i
]) == 0) {
8749 enabled
[i
] = B_TRUE
;
8753 (void) fprintf(stderr
,
8754 gettext("invalid activity '%s'\n"), tok
);
8760 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8769 (void) fprintf(stderr
, gettext("missing 'filesystem' "
8774 (void) fprintf(stderr
, gettext("too many arguments\n"));
8778 zfs_handle_t
*zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
);
8783 boolean_t missing
= B_FALSE
;
8784 boolean_t any_waited
= B_FALSE
;
8786 for (int i
= 0; i
< ZFS_WAIT_NUM_ACTIVITIES
; i
++) {
8792 error
= zfs_wait_status(zhp
, i
, &missing
, &waited
);
8793 if (error
!= 0 || missing
)
8796 any_waited
= (any_waited
|| waited
);
8799 if (error
!= 0 || missing
|| !any_waited
)
8809 * Display version message
8812 zfs_do_version(int argc
, char **argv
)
8814 (void) argc
, (void) argv
;
8815 return (zfs_version_print() != 0);
8818 /* Display documentation */
8820 zfs_do_help(int argc
, char **argv
)
8822 char page
[MAXNAMELEN
];
8823 if (argc
< 3 || strcmp(argv
[2], "zfs") == 0)
8824 strcpy(page
, "zfs");
8825 else if (strcmp(argv
[2], "concepts") == 0 ||
8826 strcmp(argv
[2], "props") == 0)
8827 snprintf(page
, sizeof (page
), "zfs%s", argv
[2]);
8829 snprintf(page
, sizeof (page
), "zfs-%s", argv
[2]);
8831 execlp("man", "man", page
, NULL
);
8833 fprintf(stderr
, "couldn't run man program: %s", strerror(errno
));
8838 main(int argc
, char **argv
)
8842 const char *cmdname
;
8845 (void) setlocale(LC_ALL
, "");
8846 (void) setlocale(LC_NUMERIC
, "C");
8847 (void) textdomain(TEXT_DOMAIN
);
8852 * Make sure the user has specified some command.
8855 (void) fprintf(stderr
, gettext("missing command\n"));
8862 * The 'umount' command is an alias for 'unmount'
8864 if (strcmp(cmdname
, "umount") == 0)
8865 cmdname
= "unmount";
8868 * The 'recv' command is an alias for 'receive'
8870 if (strcmp(cmdname
, "recv") == 0)
8871 cmdname
= "receive";
8874 * The 'snap' command is an alias for 'snapshot'
8876 if (strcmp(cmdname
, "snap") == 0)
8877 cmdname
= "snapshot";
8882 if ((strcmp(cmdname
, "-?") == 0) ||
8883 (strcmp(cmdname
, "--help") == 0))
8887 * Special case '-V|--version'
8889 if ((strcmp(cmdname
, "-V") == 0) || (strcmp(cmdname
, "--version") == 0))
8890 return (zfs_do_version(argc
, argv
));
8893 * Special case 'help'
8895 if (strcmp(cmdname
, "help") == 0)
8896 return (zfs_do_help(argc
, argv
));
8898 if ((g_zfs
= libzfs_init()) == NULL
) {
8899 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
8903 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
8905 libzfs_print_on_error(g_zfs
, B_TRUE
);
8907 zfs_setproctitle_init(argc
, argv
, environ
);
8910 * Many commands modify input strings for string parsing reasons.
8911 * We create a copy to protect the original argv.
8913 newargv
= safe_malloc((argc
+ 1) * sizeof (newargv
[0]));
8914 for (i
= 0; i
< argc
; i
++)
8915 newargv
[i
] = strdup(argv
[i
]);
8916 newargv
[argc
] = NULL
;
8919 * Run the appropriate command.
8921 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
8922 if (find_command_idx(cmdname
, &i
) == 0) {
8923 current_command
= &command_table
[i
];
8924 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
8925 } else if (strchr(cmdname
, '=') != NULL
) {
8926 verify(find_command_idx("set", &i
) == 0);
8927 current_command
= &command_table
[i
];
8928 ret
= command_table
[i
].func(argc
, newargv
);
8930 (void) fprintf(stderr
, gettext("unrecognized "
8931 "command '%s'\n"), cmdname
);
8936 for (i
= 0; i
< argc
; i
++)
8940 if (ret
== 0 && log_history
)
8941 (void) zpool_log_history(g_zfs
, history_str
);
8946 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8947 * for the purposes of running ::findleaks.
8949 if (getenv("ZFS_ABORT") != NULL
) {
8950 (void) printf("dumping core by request\n");
8958 * zfs zone nsfile filesystem
8960 * Add or delete the given dataset to/from the namespace.
8964 zfs_do_zone_impl(int argc
, char **argv
, boolean_t attach
)
8970 (void) fprintf(stderr
, gettext("missing argument(s)\n"));
8974 (void) fprintf(stderr
, gettext("too many arguments\n"));
8978 zhp
= zfs_open(g_zfs
, argv
[2], ZFS_TYPE_FILESYSTEM
);
8982 ret
= (zfs_userns(zhp
, argv
[1], attach
) != 0);
8989 zfs_do_zone(int argc
, char **argv
)
8991 return (zfs_do_zone_impl(argc
, argv
, B_TRUE
));
8995 zfs_do_unzone(int argc
, char **argv
)
8997 return (zfs_do_zone_impl(argc
, argv
, B_FALSE
));
9002 #include <sys/jail.h>
9005 * Attach/detach the given dataset to/from the given jail
9008 zfs_do_jail_impl(int argc
, char **argv
, boolean_t attach
)
9013 /* check number of arguments */
9015 (void) fprintf(stderr
, gettext("missing argument(s)\n"));
9019 (void) fprintf(stderr
, gettext("too many arguments\n"));
9023 jailid
= jail_getid(argv
[1]);
9025 (void) fprintf(stderr
, gettext("invalid jail id or name\n"));
9029 zhp
= zfs_open(g_zfs
, argv
[2], ZFS_TYPE_FILESYSTEM
);
9033 ret
= (zfs_jail(zhp
, jailid
, attach
) != 0);
9040 * zfs jail jailid filesystem
9042 * Attach the given dataset to the given jail
9045 zfs_do_jail(int argc
, char **argv
)
9047 return (zfs_do_jail_impl(argc
, argv
, B_TRUE
));
9051 * zfs unjail jailid filesystem
9053 * Detach the given dataset from the given jail
9056 zfs_do_unjail(int argc
, char **argv
)
9058 return (zfs_do_jail_impl(argc
, argv
, B_FALSE
));