4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 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>
56 #include <sys/debug.h>
58 #include <sys/mkdev.h>
59 #include <sys/mntent.h>
60 #include <sys/mnttab.h>
61 #include <sys/mount.h>
63 #include <sys/fs/zfs.h>
64 #include <sys/systeminfo.h>
65 #include <sys/types.h>
67 #include <sys/zfs_project.h>
70 #include <libzfs_core.h>
72 #include <zfs_deleg.h>
77 #include <directory.h>
78 #endif /* HAVE_IDMAP */
82 #include "zfs_comutil.h"
83 #include "libzfs_impl.h"
84 #include "zfs_projectutil.h"
86 libzfs_handle_t
*g_zfs
;
88 static FILE *mnttab_file
;
89 static char history_str
[HIS_MAX_RECORD_LEN
];
90 static boolean_t log_history
= B_TRUE
;
92 static int zfs_do_clone(int argc
, char **argv
);
93 static int zfs_do_create(int argc
, char **argv
);
94 static int zfs_do_destroy(int argc
, char **argv
);
95 static int zfs_do_get(int argc
, char **argv
);
96 static int zfs_do_inherit(int argc
, char **argv
);
97 static int zfs_do_list(int argc
, char **argv
);
98 static int zfs_do_mount(int argc
, char **argv
);
99 static int zfs_do_rename(int argc
, char **argv
);
100 static int zfs_do_rollback(int argc
, char **argv
);
101 static int zfs_do_set(int argc
, char **argv
);
102 static int zfs_do_upgrade(int argc
, char **argv
);
103 static int zfs_do_snapshot(int argc
, char **argv
);
104 static int zfs_do_unmount(int argc
, char **argv
);
105 static int zfs_do_share(int argc
, char **argv
);
106 static int zfs_do_unshare(int argc
, char **argv
);
107 static int zfs_do_send(int argc
, char **argv
);
108 static int zfs_do_receive(int argc
, char **argv
);
109 static int zfs_do_promote(int argc
, char **argv
);
110 static int zfs_do_userspace(int argc
, char **argv
);
111 static int zfs_do_allow(int argc
, char **argv
);
112 static int zfs_do_unallow(int argc
, char **argv
);
113 static int zfs_do_hold(int argc
, char **argv
);
114 static int zfs_do_holds(int argc
, char **argv
);
115 static int zfs_do_release(int argc
, char **argv
);
116 static int zfs_do_diff(int argc
, char **argv
);
117 static int zfs_do_bookmark(int argc
, char **argv
);
118 static int zfs_do_channel_program(int argc
, char **argv
);
119 static int zfs_do_load_key(int argc
, char **argv
);
120 static int zfs_do_unload_key(int argc
, char **argv
);
121 static int zfs_do_change_key(int argc
, char **argv
);
122 static int zfs_do_project(int argc
, char **argv
);
123 static int zfs_do_version(int argc
, char **argv
);
124 static int zfs_do_redact(int argc
, char **argv
);
125 static int zfs_do_wait(int argc
, char **argv
);
128 static int zfs_do_jail(int argc
, char **argv
);
129 static int zfs_do_unjail(int argc
, char **argv
);
133 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
138 _umem_debug_init(void)
140 return ("default,verbose"); /* $UMEM_DEBUG setting */
144 _umem_logging_init(void)
146 return ("fail,contents"); /* $UMEM_LOGGING setting */
180 HELP_CHANNEL_PROGRAM
,
191 typedef struct zfs_command
{
193 int (*func
)(int argc
, char **argv
);
198 * Master command table. Each ZFS command has a name, associated function, and
199 * usage message. The usage messages need to be internationalized, so we have
200 * to have a function to return the usage message based on a command index.
202 * These commands are organized according to how they are displayed in the usage
203 * message. An empty command (one with a NULL name) indicates an empty line in
204 * the generic usage message.
206 static zfs_command_t command_table
[] = {
207 { "version", zfs_do_version
, HELP_VERSION
},
209 { "create", zfs_do_create
, HELP_CREATE
},
210 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
212 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
213 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
214 { "clone", zfs_do_clone
, HELP_CLONE
},
215 { "promote", zfs_do_promote
, HELP_PROMOTE
},
216 { "rename", zfs_do_rename
, HELP_RENAME
},
217 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
218 { "program", zfs_do_channel_program
, HELP_CHANNEL_PROGRAM
},
220 { "list", zfs_do_list
, HELP_LIST
},
222 { "set", zfs_do_set
, HELP_SET
},
223 { "get", zfs_do_get
, HELP_GET
},
224 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
225 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
227 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
228 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
229 { "projectspace", zfs_do_userspace
, HELP_PROJECTSPACE
},
231 { "project", zfs_do_project
, HELP_PROJECT
},
233 { "mount", zfs_do_mount
, HELP_MOUNT
},
234 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
235 { "share", zfs_do_share
, HELP_SHARE
},
236 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
238 { "send", zfs_do_send
, HELP_SEND
},
239 { "receive", zfs_do_receive
, HELP_RECEIVE
},
241 { "allow", zfs_do_allow
, HELP_ALLOW
},
243 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
245 { "hold", zfs_do_hold
, HELP_HOLD
},
246 { "holds", zfs_do_holds
, HELP_HOLDS
},
247 { "release", zfs_do_release
, HELP_RELEASE
},
248 { "diff", zfs_do_diff
, HELP_DIFF
},
249 { "load-key", zfs_do_load_key
, HELP_LOAD_KEY
},
250 { "unload-key", zfs_do_unload_key
, HELP_UNLOAD_KEY
},
251 { "change-key", zfs_do_change_key
, HELP_CHANGE_KEY
},
252 { "redact", zfs_do_redact
, HELP_REDACT
},
253 { "wait", zfs_do_wait
, HELP_WAIT
},
256 { "jail", zfs_do_jail
, HELP_JAIL
},
257 { "unjail", zfs_do_unjail
, HELP_UNJAIL
},
261 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
263 zfs_command_t
*current_command
;
266 get_usage(zfs_help_t idx
)
270 return (gettext("\tclone [-p] [-o property=value] ... "
271 "<snapshot> <filesystem|volume>\n"));
273 return (gettext("\tcreate [-Pnpv] [-o property=value] ... "
275 "\tcreate [-Pnpsv] [-b blocksize] [-o property=value] ... "
276 "-V <size> <volume>\n"));
278 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
279 "\tdestroy [-dnpRrv] "
280 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
281 "\tdestroy <filesystem|volume>#<bookmark>\n"));
283 return (gettext("\tget [-rHp] [-d max] "
284 "[-o \"all\" | field[,...]]\n"
285 "\t [-t type[,...]] [-s source[,...]]\n"
286 "\t <\"all\" | property[,...]> "
287 "[filesystem|volume|snapshot|bookmark] ...\n"));
289 return (gettext("\tinherit [-rS] <property> "
290 "<filesystem|volume|snapshot> ...\n"));
292 return (gettext("\tupgrade [-v]\n"
293 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
295 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
296 "[-s property]...\n\t [-S property]... [-t type[,...]] "
297 "[filesystem|volume|snapshot] ...\n"));
299 return (gettext("\tmount\n"
300 "\tmount [-flvO] [-o opts] <-a | filesystem>\n"));
302 return (gettext("\tpromote <clone-filesystem>\n"));
304 return (gettext("\treceive [-vMnsFhu] "
305 "[-o <property>=<value>] ... [-x <property>] ...\n"
306 "\t <filesystem|volume|snapshot>\n"
307 "\treceive [-vMnsFhu] [-o <property>=<value>] ... "
308 "[-x <property>] ... \n"
309 "\t [-d | -e] <filesystem>\n"
310 "\treceive -A <filesystem|volume>\n"));
312 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
313 "<filesystem|volume|snapshot>\n"
314 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
315 "\trename -r <snapshot> <snapshot>\n"));
317 return (gettext("\trollback [-rRf] <snapshot>\n"));
319 return (gettext("\tsend [-DnPpRvLecwhb] [-[i|I] snapshot] "
321 "\tsend [-nvPLecw] [-i snapshot|bookmark] "
322 "<filesystem|volume|snapshot>\n"
323 "\tsend [-DnPpvLec] [-i bookmark|snapshot] "
324 "--redact <bookmark> <snapshot>\n"
325 "\tsend [-nvPe] -t <receive_resume_token>\n"
326 "\tsend [-Pnv] --saved filesystem\n"));
328 return (gettext("\tset <property=value> ... "
329 "<filesystem|volume|snapshot> ...\n"));
331 return (gettext("\tshare [-l] <-a [nfs|smb] | filesystem>\n"));
333 return (gettext("\tsnapshot [-r] [-o property=value] ... "
334 "<filesystem|volume>@<snap> ...\n"));
336 return (gettext("\tunmount [-fu] "
337 "<-a | filesystem|mountpoint>\n"));
339 return (gettext("\tunshare "
340 "<-a [nfs|smb] | filesystem|mountpoint>\n"));
342 return (gettext("\tallow <filesystem|volume>\n"
344 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
345 "\t <filesystem|volume>\n"
346 "\tallow [-ld] -e <perm|@setname>[,...] "
347 "<filesystem|volume>\n"
348 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
349 "\tallow -s @setname <perm|@setname>[,...] "
350 "<filesystem|volume>\n"));
352 return (gettext("\tunallow [-rldug] "
353 "<\"everyone\"|user|group>[,...]\n"
354 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
355 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
356 "<filesystem|volume>\n"
357 "\tunallow [-r] -c [<perm|@setname>[,...]] "
358 "<filesystem|volume>\n"
359 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
360 "<filesystem|volume>\n"));
362 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
364 "\t [-S field] ... [-t type[,...]] "
365 "<filesystem|snapshot>\n"));
366 case HELP_GROUPSPACE
:
367 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
369 "\t [-S field] ... [-t type[,...]] "
370 "<filesystem|snapshot>\n"));
371 case HELP_PROJECTSPACE
:
372 return (gettext("\tprojectspace [-Hp] [-o field[,...]] "
374 "\t [-S field] ... <filesystem|snapshot>\n"));
376 return (gettext("\tproject [-d|-r] <directory|file ...>\n"
377 "\tproject -c [-0] [-d|-r] [-p id] <directory|file ...>\n"
378 "\tproject -C [-k] [-r] <directory ...>\n"
379 "\tproject [-p id] [-r] [-s] <directory ...>\n"));
381 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
383 return (gettext("\tholds [-rH] <snapshot> ...\n"));
385 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
387 return (gettext("\tdiff [-FHt] <snapshot> "
388 "[snapshot|filesystem]\n"));
390 return (gettext("\tbookmark <snapshot|bookmark> "
392 case HELP_CHANNEL_PROGRAM
:
393 return (gettext("\tprogram [-jn] [-t <instruction limit>] "
394 "[-m <memory limit (b)>]\n"
395 "\t <pool> <program file> [lua args...]\n"));
397 return (gettext("\tload-key [-rn] [-L <keylocation>] "
398 "<-a | filesystem|volume>\n"));
399 case HELP_UNLOAD_KEY
:
400 return (gettext("\tunload-key [-r] "
401 "<-a | filesystem|volume>\n"));
402 case HELP_CHANGE_KEY
:
403 return (gettext("\tchange-key [-l] [-o keyformat=<value>]\n"
404 "\t [-o keylocation=<value>] [-o pbkdf2iters=<value>]\n"
405 "\t <filesystem|volume>\n"
406 "\tchange-key -i [-l] <filesystem|volume>\n"));
408 return (gettext("\tversion\n"));
410 return (gettext("\tredact <snapshot> <bookmark> "
411 "<redaction_snapshot> ...\n"));
413 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
415 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
417 return (gettext("\twait [-t <activity>] <filesystem>\n"));
427 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
432 * Utility function to guarantee malloc() success.
436 safe_malloc(size_t size
)
440 if ((data
= calloc(1, size
)) == NULL
)
447 safe_realloc(void *data
, size_t size
)
450 if ((newp
= realloc(data
, size
)) == NULL
) {
459 safe_strdup(char *str
)
461 char *dupstr
= strdup(str
);
470 * Callback routine that will print out information for each of
474 usage_prop_cb(int prop
, void *cb
)
478 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
480 if (zfs_prop_readonly(prop
))
481 (void) fprintf(fp
, " NO ");
483 (void) fprintf(fp
, "YES ");
485 if (zfs_prop_inheritable(prop
))
486 (void) fprintf(fp
, " YES ");
488 (void) fprintf(fp
, " NO ");
490 if (zfs_prop_values(prop
) == NULL
)
491 (void) fprintf(fp
, "-\n");
493 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
499 * Display usage message. If we're inside a command, display only the usage for
500 * that command. Otherwise, iterate over the entire command table and display
501 * a complete usage message.
504 usage(boolean_t requested
)
507 boolean_t show_properties
= B_FALSE
;
508 FILE *fp
= requested
? stdout
: stderr
;
510 if (current_command
== NULL
) {
512 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
514 gettext("where 'command' is one of the following:\n\n"));
516 for (i
= 0; i
< NCOMMAND
; i
++) {
517 if (command_table
[i
].name
== NULL
)
518 (void) fprintf(fp
, "\n");
520 (void) fprintf(fp
, "%s",
521 get_usage(command_table
[i
].usage
));
524 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
525 "pool/[dataset/]*dataset[@name]\n"));
527 (void) fprintf(fp
, gettext("usage:\n"));
528 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
531 if (current_command
!= NULL
&&
532 (strcmp(current_command
->name
, "set") == 0 ||
533 strcmp(current_command
->name
, "get") == 0 ||
534 strcmp(current_command
->name
, "inherit") == 0 ||
535 strcmp(current_command
->name
, "list") == 0))
536 show_properties
= B_TRUE
;
538 if (show_properties
) {
540 gettext("\nThe following properties are supported:\n"));
542 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
543 "PROPERTY", "EDIT", "INHERIT", "VALUES");
545 /* Iterate over all properties */
546 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
549 (void) fprintf(fp
, "\t%-15s ", "userused@...");
550 (void) fprintf(fp
, " NO NO <size>\n");
551 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
552 (void) fprintf(fp
, " NO NO <size>\n");
553 (void) fprintf(fp
, "\t%-15s ", "projectused@...");
554 (void) fprintf(fp
, " NO NO <size>\n");
555 (void) fprintf(fp
, "\t%-15s ", "userobjused@...");
556 (void) fprintf(fp
, " NO NO <size>\n");
557 (void) fprintf(fp
, "\t%-15s ", "groupobjused@...");
558 (void) fprintf(fp
, " NO NO <size>\n");
559 (void) fprintf(fp
, "\t%-15s ", "projectobjused@...");
560 (void) fprintf(fp
, " NO NO <size>\n");
561 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
562 (void) fprintf(fp
, "YES NO <size> | none\n");
563 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
564 (void) fprintf(fp
, "YES NO <size> | none\n");
565 (void) fprintf(fp
, "\t%-15s ", "projectquota@...");
566 (void) fprintf(fp
, "YES NO <size> | none\n");
567 (void) fprintf(fp
, "\t%-15s ", "userobjquota@...");
568 (void) fprintf(fp
, "YES NO <size> | none\n");
569 (void) fprintf(fp
, "\t%-15s ", "groupobjquota@...");
570 (void) fprintf(fp
, "YES NO <size> | none\n");
571 (void) fprintf(fp
, "\t%-15s ", "projectobjquota@...");
572 (void) fprintf(fp
, "YES NO <size> | none\n");
573 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
574 (void) fprintf(fp
, " NO NO <size>\n");
575 (void) fprintf(fp
, "\t%-15s ", "written#<bookmark>");
576 (void) fprintf(fp
, " NO NO <size>\n");
578 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
579 "with standard units such as K, M, G, etc.\n"));
580 (void) fprintf(fp
, gettext("\nUser-defined properties can "
581 "be specified by using a name containing a colon (:).\n"));
582 (void) fprintf(fp
, gettext("\nThe {user|group|project}"
583 "[obj]{used|quota}@ properties must be appended with\n"
584 "a user|group|project specifier of one of these forms:\n"
585 " POSIX name (eg: \"matt\")\n"
586 " POSIX id (eg: \"126829\")\n"
587 " SMB name@domain (eg: \"matt@sun\")\n"
588 " SMB SID (eg: \"S-1-234-567-89\")\n"));
591 gettext("\nFor the property list, run: %s\n"),
594 gettext("\nFor the delegated permission list, run: %s\n"),
595 "zfs allow|unallow");
599 * See comments at end of main().
601 if (getenv("ZFS_ABORT") != NULL
) {
602 (void) printf("dumping core by request\n");
606 exit(requested
? 0 : 2);
610 * Take a property=value argument string and add it to the given nvlist.
611 * Modifies the argument inplace.
614 parseprop(nvlist_t
*props
, char *propname
)
618 if ((propval
= strchr(propname
, '=')) == NULL
) {
619 (void) fprintf(stderr
, gettext("missing "
620 "'=' for property=value argument\n"));
625 if (nvlist_exists(props
, propname
)) {
626 (void) fprintf(stderr
, gettext("property '%s' "
627 "specified multiple times\n"), propname
);
630 if (nvlist_add_string(props
, propname
, propval
) != 0)
636 * Take a property name argument and add it to the given nvlist.
637 * Modifies the argument inplace.
640 parsepropname(nvlist_t
*props
, char *propname
)
642 if (strchr(propname
, '=') != NULL
) {
643 (void) fprintf(stderr
, gettext("invalid character "
644 "'=' in property argument\n"));
647 if (nvlist_exists(props
, propname
)) {
648 (void) fprintf(stderr
, gettext("property '%s' "
649 "specified multiple times\n"), propname
);
652 if (nvlist_add_boolean(props
, propname
) != 0)
658 parse_depth(char *opt
, int *flags
)
663 depth
= (int)strtol(opt
, &tmp
, 0);
665 (void) fprintf(stderr
,
666 gettext("%s is not an integer\n"), optarg
);
670 (void) fprintf(stderr
,
671 gettext("Depth can not be negative.\n"));
674 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
678 #define PROGRESS_DELAY 2 /* seconds */
680 static char *pt_reverse
= "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
681 static time_t pt_begin
;
682 static char *pt_header
= NULL
;
683 static boolean_t pt_shown
;
686 start_progress_timer(void)
688 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
693 set_progress_header(char *header
)
695 assert(pt_header
== NULL
);
696 pt_header
= safe_strdup(header
);
698 (void) printf("%s: ", header
);
699 (void) fflush(stdout
);
704 update_progress(char *update
)
706 if (!pt_shown
&& time(NULL
) > pt_begin
) {
707 int len
= strlen(update
);
709 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
711 (void) fflush(stdout
);
713 } else if (pt_shown
) {
714 int len
= strlen(update
);
716 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
717 (void) fflush(stdout
);
722 finish_progress(char *done
)
725 (void) printf("%s\n", done
);
726 (void) fflush(stdout
);
733 zfs_mount_and_share(libzfs_handle_t
*hdl
, const char *dataset
, zfs_type_t type
)
735 zfs_handle_t
*zhp
= NULL
;
738 zhp
= zfs_open(hdl
, dataset
, type
);
743 * Volumes may neither be mounted or shared. Potentially in the
744 * future filesystems detected on these volumes could be mounted.
746 if (zfs_get_type(zhp
) == ZFS_TYPE_VOLUME
) {
752 * Mount and/or share the new filesystem as appropriate. We provide a
753 * verbose error message to let the user know that their filesystem was
754 * in fact created, even if we failed to mount or share it.
756 * If the user doesn't want the dataset automatically mounted, then
757 * skip the mount/share step
759 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
, B_FALSE
) &&
760 zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
) {
761 if (zfs_mount_delegation_check()) {
762 (void) fprintf(stderr
, gettext("filesystem "
763 "successfully created, but it may only be "
764 "mounted by root\n"));
766 } else if (zfs_mount(zhp
, NULL
, 0) != 0) {
767 (void) fprintf(stderr
, gettext("filesystem "
768 "successfully created, but not mounted\n"));
770 } else if (zfs_share(zhp
) != 0) {
771 (void) fprintf(stderr
, gettext("filesystem "
772 "successfully created, but not shared\n"));
775 zfs_commit_all_shares();
784 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
786 * Given an existing dataset, create a writable copy whose initial contents
787 * are the same as the source. The newly created dataset maintains a
788 * dependency on the original; the original cannot be destroyed so long as
791 * The '-p' flag creates all the non-existing ancestors of the target first.
794 zfs_do_clone(int argc
, char **argv
)
796 zfs_handle_t
*zhp
= NULL
;
797 boolean_t parents
= B_FALSE
;
802 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
806 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
809 if (!parseprop(props
, optarg
)) {
818 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
827 /* check number of arguments */
829 (void) fprintf(stderr
, gettext("missing source dataset "
834 (void) fprintf(stderr
, gettext("missing target dataset "
839 (void) fprintf(stderr
, gettext("too many arguments\n"));
843 /* open the source dataset */
844 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
) {
849 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
852 * Now create the ancestors of the target dataset. If the
853 * target already exists and '-p' option was used we should not
856 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
862 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
870 ret
= zfs_clone(zhp
, argv
[1], props
);
872 /* create the mountpoint if necessary */
875 (void) zpool_log_history(g_zfs
, history_str
);
876 log_history
= B_FALSE
;
879 ret
= zfs_mount_and_share(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
888 ASSERT3P(zhp
, ==, NULL
);
895 * zfs create [-Pnpv] [-o prop=value] ... fs
896 * zfs create [-Pnpsv] [-b blocksize] [-o prop=value] ... -V vol size
898 * Create a new dataset. This command can be used to create filesystems
899 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
900 * For volumes, the user must specify a size to be used.
902 * The '-s' flag applies only to volumes, and indicates that we should not try
903 * to set the reservation for this volume. By default we set a reservation
904 * equal to the size for any volume. For pools with SPA_VERSION >=
905 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
907 * The '-p' flag creates all the non-existing ancestors of the target first.
909 * The '-n' flag is no-op (dry run) mode. This will perform a user-space sanity
910 * check of arguments and properties, but does not check for permissions,
911 * available space, etc.
913 * The '-v' flag is for verbose output.
915 * The '-P' flag is used for parseable output. It implies '-v'.
918 zfs_do_create(int argc
, char **argv
)
920 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
921 zpool_handle_t
*zpool_handle
= NULL
;
922 nvlist_t
*real_props
= NULL
;
923 uint64_t volsize
= 0;
925 boolean_t noreserve
= B_FALSE
;
926 boolean_t bflag
= B_FALSE
;
927 boolean_t parents
= B_FALSE
;
928 boolean_t dryrun
= B_FALSE
;
929 boolean_t verbose
= B_FALSE
;
930 boolean_t parseable
= B_FALSE
;
935 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
939 while ((c
= getopt(argc
, argv
, ":PV:b:nso:pv")) != -1) {
942 type
= ZFS_TYPE_VOLUME
;
943 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
944 (void) fprintf(stderr
, gettext("bad volume "
945 "size '%s': %s\n"), optarg
,
946 libzfs_error_description(g_zfs
));
950 if (nvlist_add_uint64(props
,
951 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
964 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
965 (void) fprintf(stderr
, gettext("bad volume "
966 "block size '%s': %s\n"), optarg
,
967 libzfs_error_description(g_zfs
));
971 if (nvlist_add_uint64(props
,
972 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
980 if (!parseprop(props
, optarg
))
990 (void) fprintf(stderr
, gettext("missing size "
994 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1000 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
1001 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
1002 "used when creating a volume\n"));
1009 /* check number of arguments */
1011 (void) fprintf(stderr
, gettext("missing %s argument\n"),
1012 zfs_type_to_name(type
));
1016 (void) fprintf(stderr
, gettext("too many arguments\n"));
1020 if (dryrun
|| (type
== ZFS_TYPE_VOLUME
&& !noreserve
)) {
1021 char msg
[ZFS_MAX_DATASET_NAME_LEN
* 2];
1024 if ((p
= strchr(argv
[0], '/')) != NULL
)
1026 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
1029 if (zpool_handle
== NULL
)
1032 (void) snprintf(msg
, sizeof (msg
),
1033 dryrun
? gettext("cannot verify '%s'") :
1034 gettext("cannot create '%s'"), argv
[0]);
1035 if (props
&& (real_props
= zfs_valid_proplist(g_zfs
, type
,
1036 props
, 0, NULL
, zpool_handle
, B_TRUE
, msg
)) == NULL
) {
1037 zpool_close(zpool_handle
);
1043 * if volsize is not a multiple of volblocksize, round it up to the
1044 * nearest multiple of the volblocksize
1046 if (type
== ZFS_TYPE_VOLUME
) {
1047 uint64_t volblocksize
;
1049 if (nvlist_lookup_uint64(props
,
1050 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1051 &volblocksize
) != 0)
1052 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
1054 if (volsize
% volblocksize
) {
1055 volsize
= P2ROUNDUP_TYPED(volsize
, volblocksize
,
1058 if (nvlist_add_uint64(props
,
1059 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), volsize
) != 0) {
1067 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
1068 uint64_t spa_version
;
1069 zfs_prop_t resv_prop
;
1072 spa_version
= zpool_get_prop_int(zpool_handle
,
1073 ZPOOL_PROP_VERSION
, NULL
);
1074 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
1075 resv_prop
= ZFS_PROP_REFRESERVATION
;
1077 resv_prop
= ZFS_PROP_RESERVATION
;
1079 volsize
= zvol_volsize_to_reservation(zpool_handle
, volsize
,
1082 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
1084 if (nvlist_add_uint64(props
,
1085 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
1091 if (zpool_handle
!= NULL
) {
1092 zpool_close(zpool_handle
);
1093 nvlist_free(real_props
);
1096 if (parents
&& zfs_name_valid(argv
[0], type
)) {
1098 * Now create the ancestors of target dataset. If the target
1099 * already exists and '-p' option was used we should not
1102 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
1107 (void) printf(parseable
? "create_ancestors\t%s\n" :
1108 dryrun
? "would create ancestors of %s\n" :
1109 "create ancestors of %s\n", argv
[0]);
1112 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0) {
1119 nvpair_t
*nvp
= NULL
;
1120 (void) printf(parseable
? "create\t%s\n" :
1121 dryrun
? "would create %s\n" : "create %s\n", argv
[0]);
1122 while ((nvp
= nvlist_next_nvpair(props
, nvp
)) != NULL
) {
1126 switch (nvpair_type(nvp
)) {
1127 case DATA_TYPE_UINT64
:
1128 VERIFY0(nvpair_value_uint64(nvp
, &uval
));
1129 (void) printf(parseable
?
1130 "property\t%s\t%llu\n" : "\t%s=%llu\n",
1131 nvpair_name(nvp
), (u_longlong_t
)uval
);
1133 case DATA_TYPE_STRING
:
1134 VERIFY0(nvpair_value_string(nvp
, &sval
));
1135 (void) printf(parseable
?
1136 "property\t%s\t%s\n" : "\t%s=%s\n",
1137 nvpair_name(nvp
), sval
);
1140 (void) fprintf(stderr
, "property '%s' "
1141 "has illegal type %d\n",
1142 nvpair_name(nvp
), nvpair_type(nvp
));
1152 /* pass to libzfs */
1153 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
1157 (void) zpool_log_history(g_zfs
, history_str
);
1158 log_history
= B_FALSE
;
1161 ret
= zfs_mount_and_share(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
1172 * zfs destroy [-rRf] <fs, vol>
1173 * zfs destroy [-rRd] <snap>
1175 * -r Recursively destroy all children
1176 * -R Recursively destroy all dependents, including clones
1177 * -f Force unmounting of any dependents
1178 * -d If we can't destroy now, mark for deferred destruction
1180 * Destroys the given dataset. By default, it will unmount any filesystems,
1181 * and refuse to destroy a dataset that has any dependents. A dependent can
1182 * either be a child, or a clone of a child.
1184 typedef struct destroy_cbdata
{
1187 boolean_t cb_recurse
;
1189 boolean_t cb_doclones
;
1190 zfs_handle_t
*cb_target
;
1191 boolean_t cb_defer_destroy
;
1192 boolean_t cb_verbose
;
1193 boolean_t cb_parsable
;
1194 boolean_t cb_dryrun
;
1196 nvlist_t
*cb_batchedsnaps
;
1198 /* first snap in contiguous run */
1200 /* previous snap in contiguous run */
1202 int64_t cb_snapused
;
1205 uint64_t cb_snap_count
;
1209 * Check for any dependents based on the '-r' or '-R' flags.
1212 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
1214 destroy_cbdata_t
*cbp
= data
;
1215 const char *tname
= zfs_get_name(cbp
->cb_target
);
1216 const char *name
= zfs_get_name(zhp
);
1218 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
1219 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
1221 * This is a direct descendant, not a clone somewhere else in
1224 if (cbp
->cb_recurse
)
1227 if (cbp
->cb_first
) {
1228 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1229 "%s has children\n"),
1230 zfs_get_name(cbp
->cb_target
),
1231 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1232 (void) fprintf(stderr
, gettext("use '-r' to destroy "
1233 "the following datasets:\n"));
1234 cbp
->cb_first
= B_FALSE
;
1235 cbp
->cb_error
= B_TRUE
;
1238 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1241 * This is a clone. We only want to report this if the '-r'
1242 * wasn't specified, or the target is a snapshot.
1244 if (!cbp
->cb_recurse
&&
1245 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
1248 if (cbp
->cb_first
) {
1249 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1250 "%s has dependent clones\n"),
1251 zfs_get_name(cbp
->cb_target
),
1252 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1253 (void) fprintf(stderr
, gettext("use '-R' to destroy "
1254 "the following datasets:\n"));
1255 cbp
->cb_first
= B_FALSE
;
1256 cbp
->cb_error
= B_TRUE
;
1257 cbp
->cb_dryrun
= B_TRUE
;
1260 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1269 destroy_batched(destroy_cbdata_t
*cb
)
1271 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1272 cb
->cb_batchedsnaps
, B_FALSE
);
1273 fnvlist_free(cb
->cb_batchedsnaps
);
1274 cb
->cb_batchedsnaps
= fnvlist_alloc();
1279 destroy_callback(zfs_handle_t
*zhp
, void *data
)
1281 destroy_cbdata_t
*cb
= data
;
1282 const char *name
= zfs_get_name(zhp
);
1285 if (cb
->cb_verbose
) {
1286 if (cb
->cb_parsable
) {
1287 (void) printf("destroy\t%s\n", name
);
1288 } else if (cb
->cb_dryrun
) {
1289 (void) printf(gettext("would destroy %s\n"),
1292 (void) printf(gettext("will destroy %s\n"),
1298 * Ignore pools (which we've already flagged as an error before getting
1301 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1302 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1306 if (cb
->cb_dryrun
) {
1312 * We batch up all contiguous snapshots (even of different
1313 * filesystems) and destroy them with one ioctl. We can't
1314 * simply do all snap deletions and then all fs deletions,
1315 * because we must delete a clone before its origin.
1317 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1318 cb
->cb_snap_count
++;
1319 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1320 if (cb
->cb_snap_count
% 10 == 0 && cb
->cb_defer_destroy
)
1321 error
= destroy_batched(cb
);
1323 error
= destroy_batched(cb
);
1325 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1326 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1329 * When performing a recursive destroy we ignore errors
1330 * so that the recursive destroy could continue
1331 * destroying past problem datasets
1333 if (cb
->cb_recurse
) {
1334 cb
->cb_error
= B_TRUE
;
1346 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1348 destroy_cbdata_t
*cb
= arg
;
1349 const char *name
= zfs_get_name(zhp
);
1352 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1353 if (cb
->cb_firstsnap
== NULL
)
1354 cb
->cb_firstsnap
= strdup(name
);
1355 if (cb
->cb_prevsnap
!= NULL
)
1356 free(cb
->cb_prevsnap
);
1357 /* this snap continues the current range */
1358 cb
->cb_prevsnap
= strdup(name
);
1359 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1361 if (cb
->cb_verbose
) {
1362 if (cb
->cb_parsable
) {
1363 (void) printf("destroy\t%s\n", name
);
1364 } else if (cb
->cb_dryrun
) {
1365 (void) printf(gettext("would destroy %s\n"),
1368 (void) printf(gettext("will destroy %s\n"),
1372 } else if (cb
->cb_firstsnap
!= NULL
) {
1373 /* end of this range */
1375 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1376 cb
->cb_prevsnap
, &used
);
1377 cb
->cb_snapused
+= used
;
1378 free(cb
->cb_firstsnap
);
1379 cb
->cb_firstsnap
= NULL
;
1380 free(cb
->cb_prevsnap
);
1381 cb
->cb_prevsnap
= NULL
;
1388 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1391 assert(cb
->cb_firstsnap
== NULL
);
1392 assert(cb
->cb_prevsnap
== NULL
);
1393 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
, 0, 0);
1394 if (cb
->cb_firstsnap
!= NULL
) {
1397 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1398 cb
->cb_prevsnap
, &used
);
1400 cb
->cb_snapused
+= used
;
1401 free(cb
->cb_firstsnap
);
1402 cb
->cb_firstsnap
= NULL
;
1403 free(cb
->cb_prevsnap
);
1404 cb
->cb_prevsnap
= NULL
;
1410 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1412 destroy_cbdata_t
*cb
= arg
;
1415 /* Check for clones. */
1416 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1417 cb
->cb_target
= zhp
;
1418 cb
->cb_first
= B_TRUE
;
1419 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1420 destroy_check_dependent
, cb
);
1424 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1432 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1434 destroy_cbdata_t
*cb
= arg
;
1437 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1443 if (cb
->cb_verbose
) {
1444 err
= destroy_print_snapshots(zhp
, cb
);
1450 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1458 destroy_clones(destroy_cbdata_t
*cb
)
1461 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1463 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1464 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1467 boolean_t defer
= cb
->cb_defer_destroy
;
1471 * We can't defer destroy non-snapshots, so set it to
1472 * false while destroying the clones.
1474 cb
->cb_defer_destroy
= B_FALSE
;
1475 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1476 destroy_callback
, cb
);
1477 cb
->cb_defer_destroy
= defer
;
1487 zfs_do_destroy(int argc
, char **argv
)
1489 destroy_cbdata_t cb
= { 0 };
1493 zfs_handle_t
*zhp
= NULL
;
1495 zfs_type_t type
= ZFS_TYPE_DATASET
;
1498 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1501 cb
.cb_verbose
= B_TRUE
;
1504 cb
.cb_verbose
= B_TRUE
;
1505 cb
.cb_parsable
= B_TRUE
;
1508 cb
.cb_dryrun
= B_TRUE
;
1511 cb
.cb_defer_destroy
= B_TRUE
;
1512 type
= ZFS_TYPE_SNAPSHOT
;
1515 cb
.cb_force
= B_TRUE
;
1518 cb
.cb_recurse
= B_TRUE
;
1521 cb
.cb_recurse
= B_TRUE
;
1522 cb
.cb_doclones
= B_TRUE
;
1526 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1535 /* check number of arguments */
1537 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1541 (void) fprintf(stderr
, gettext("too many arguments\n"));
1545 at
= strchr(argv
[0], '@');
1546 pound
= strchr(argv
[0], '#');
1549 /* Build the list of snaps to destroy in cb_nvl. */
1550 cb
.cb_nvl
= fnvlist_alloc();
1553 zhp
= zfs_open(g_zfs
, argv
[0],
1554 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1556 nvlist_free(cb
.cb_nvl
);
1560 cb
.cb_snapspec
= at
+ 1;
1561 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1567 if (nvlist_empty(cb
.cb_nvl
)) {
1568 (void) fprintf(stderr
, gettext("could not find any "
1569 "snapshots to destroy; check snapshot names.\n"));
1574 if (cb
.cb_verbose
) {
1576 zfs_nicebytes(cb
.cb_snapused
, buf
, sizeof (buf
));
1577 if (cb
.cb_parsable
) {
1578 (void) printf("reclaim\t%llu\n",
1579 (u_longlong_t
)cb
.cb_snapused
);
1580 } else if (cb
.cb_dryrun
) {
1581 (void) printf(gettext("would reclaim %s\n"),
1584 (void) printf(gettext("will reclaim %s\n"),
1589 if (!cb
.cb_dryrun
) {
1590 if (cb
.cb_doclones
) {
1591 cb
.cb_batchedsnaps
= fnvlist_alloc();
1592 err
= destroy_clones(&cb
);
1594 err
= zfs_destroy_snaps_nvl(g_zfs
,
1595 cb
.cb_batchedsnaps
, B_FALSE
);
1603 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1604 cb
.cb_defer_destroy
);
1610 } else if (pound
!= NULL
) {
1615 (void) fprintf(stderr
,
1616 "dryrun is not supported with bookmark\n");
1620 if (cb
.cb_defer_destroy
) {
1621 (void) fprintf(stderr
,
1622 "defer destroy is not supported with bookmark\n");
1626 if (cb
.cb_recurse
) {
1627 (void) fprintf(stderr
,
1628 "recursive is not supported with bookmark\n");
1633 * Unfortunately, zfs_bookmark() doesn't honor the
1634 * casesensitivity setting. However, we can't simply
1635 * remove this check, because lzc_destroy_bookmarks()
1636 * ignores non-existent bookmarks, so this is necessary
1637 * to get a proper error message.
1639 if (!zfs_bookmark_exists(argv
[0])) {
1640 (void) fprintf(stderr
, gettext("bookmark '%s' "
1641 "does not exist.\n"), argv
[0]);
1645 nvl
= fnvlist_alloc();
1646 fnvlist_add_boolean(nvl
, argv
[0]);
1648 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1650 (void) zfs_standard_error(g_zfs
, err
,
1651 "cannot destroy bookmark");
1658 /* Open the given dataset */
1659 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1665 * Perform an explicit check for pools before going any further.
1667 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1668 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1669 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1670 "operation does not apply to pools\n"),
1672 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1673 "%s' to destroy all datasets in the pool\n"),
1675 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1676 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1682 * Check for any dependents and/or clones.
1684 cb
.cb_first
= B_TRUE
;
1685 if (!cb
.cb_doclones
&&
1686 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1696 cb
.cb_batchedsnaps
= fnvlist_alloc();
1697 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1704 * Do the real thing. The callback will close the
1705 * handle regardless of whether it succeeds or not.
1707 err
= destroy_callback(zhp
, &cb
);
1710 err
= zfs_destroy_snaps_nvl(g_zfs
,
1711 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1713 if (err
!= 0 || cb
.cb_error
== B_TRUE
)
1718 fnvlist_free(cb
.cb_batchedsnaps
);
1719 fnvlist_free(cb
.cb_nvl
);
1726 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1729 zfs_get_column_t col
;
1731 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1732 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1733 if (col
== GET_COL_RECVD
)
1739 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1740 * < all | property[,property]... > < fs | snap | vol > ...
1742 * -r recurse over any child datasets
1743 * -H scripted mode. Headers are stripped, and fields are separated
1744 * by tabs instead of spaces.
1745 * -o Set of fields to display. One of "name,property,value,
1746 * received,source". Default is "name,property,value,source".
1747 * "all" is an alias for all five.
1748 * -s Set of sources to allow. One of
1749 * "local,default,inherited,received,temporary,none". Default is
1751 * -p Display values in parsable (literal) format.
1753 * Prints properties for the given datasets. The user can control which
1754 * columns to display as well as which property types to allow.
1758 * Invoked to display the properties for a single dataset.
1761 get_callback(zfs_handle_t
*zhp
, void *data
)
1763 char buf
[ZFS_MAXPROPLEN
];
1764 char rbuf
[ZFS_MAXPROPLEN
];
1765 zprop_source_t sourcetype
;
1766 char source
[ZFS_MAX_DATASET_NAME_LEN
];
1767 zprop_get_cbdata_t
*cbp
= data
;
1768 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1769 zprop_list_t
*pl
= cbp
->cb_proplist
;
1773 boolean_t received
= is_recvd_column(cbp
);
1775 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1776 char *recvdval
= NULL
;
1778 * Skip the special fake placeholder. This will also skip over
1779 * the name property when 'all' is specified.
1781 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1782 pl
== cbp
->cb_proplist
)
1785 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1786 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1787 sizeof (buf
), &sourcetype
, source
,
1789 cbp
->cb_literal
) != 0) {
1792 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1793 ZFS_TYPE_DATASET
, B_FALSE
)) {
1794 (void) fprintf(stderr
,
1795 gettext("No such property '%s'\n"),
1796 zfs_prop_to_name(pl
->pl_prop
));
1799 sourcetype
= ZPROP_SRC_NONE
;
1800 (void) strlcpy(buf
, "-", sizeof (buf
));
1803 if (received
&& (zfs_prop_get_recvd(zhp
,
1804 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1805 cbp
->cb_literal
) == 0))
1808 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1809 zfs_prop_to_name(pl
->pl_prop
),
1810 buf
, sourcetype
, source
, recvdval
);
1811 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1812 sourcetype
= ZPROP_SRC_LOCAL
;
1814 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1815 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1816 sourcetype
= ZPROP_SRC_NONE
;
1817 (void) strlcpy(buf
, "-", sizeof (buf
));
1820 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1821 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1822 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1823 sourcetype
= ZPROP_SRC_LOCAL
;
1825 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1826 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1827 sourcetype
= ZPROP_SRC_NONE
;
1828 (void) strlcpy(buf
, "-", sizeof (buf
));
1831 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1832 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1834 if (nvlist_lookup_nvlist(user_props
,
1835 pl
->pl_user_prop
, &propval
) != 0) {
1838 sourcetype
= ZPROP_SRC_NONE
;
1841 verify(nvlist_lookup_string(propval
,
1842 ZPROP_VALUE
, &strval
) == 0);
1843 verify(nvlist_lookup_string(propval
,
1844 ZPROP_SOURCE
, &sourceval
) == 0);
1846 if (strcmp(sourceval
,
1847 zfs_get_name(zhp
)) == 0) {
1848 sourcetype
= ZPROP_SRC_LOCAL
;
1849 } else if (strcmp(sourceval
,
1850 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1851 sourcetype
= ZPROP_SRC_RECEIVED
;
1853 sourcetype
= ZPROP_SRC_INHERITED
;
1854 (void) strlcpy(source
,
1855 sourceval
, sizeof (source
));
1859 if (received
&& (zfs_prop_get_recvd(zhp
,
1860 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1861 cbp
->cb_literal
) == 0))
1864 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1865 pl
->pl_user_prop
, strval
, sourcetype
,
1874 zfs_do_get(int argc
, char **argv
)
1876 zprop_get_cbdata_t cb
= { 0 };
1877 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1878 int types
= ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
;
1879 char *value
, *fields
;
1882 zprop_list_t fake_name
= { 0 };
1885 * Set up default columns and sources.
1887 cb
.cb_sources
= ZPROP_SRC_ALL
;
1888 cb
.cb_columns
[0] = GET_COL_NAME
;
1889 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1890 cb
.cb_columns
[2] = GET_COL_VALUE
;
1891 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1892 cb
.cb_type
= ZFS_TYPE_DATASET
;
1895 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
1898 cb
.cb_literal
= B_TRUE
;
1901 limit
= parse_depth(optarg
, &flags
);
1904 flags
|= ZFS_ITER_RECURSE
;
1907 cb
.cb_scripted
= B_TRUE
;
1910 (void) fprintf(stderr
, gettext("missing argument for "
1911 "'%c' option\n"), optopt
);
1916 * Process the set of columns to display. We zero out
1917 * the structure to give us a blank slate.
1919 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1921 while (*optarg
!= '\0') {
1922 static char *col_subopts
[] =
1923 { "name", "property", "value", "received",
1924 "source", "all", NULL
};
1926 if (i
== ZFS_GET_NCOLS
) {
1927 (void) fprintf(stderr
, gettext("too "
1928 "many fields given to -o "
1933 switch (getsubopt(&optarg
, col_subopts
,
1936 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1939 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1942 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1945 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1946 flags
|= ZFS_ITER_RECVD_PROPS
;
1949 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1953 (void) fprintf(stderr
,
1954 gettext("\"all\" conflicts "
1955 "with specific fields "
1956 "given to -o option\n"));
1959 cb
.cb_columns
[0] = GET_COL_NAME
;
1960 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1961 cb
.cb_columns
[2] = GET_COL_VALUE
;
1962 cb
.cb_columns
[3] = GET_COL_RECVD
;
1963 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1964 flags
|= ZFS_ITER_RECVD_PROPS
;
1968 (void) fprintf(stderr
,
1969 gettext("invalid column name "
1978 while (*optarg
!= '\0') {
1979 static char *source_subopts
[] = {
1980 "local", "default", "inherited",
1981 "received", "temporary", "none",
1984 switch (getsubopt(&optarg
, source_subopts
,
1987 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1990 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1993 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1996 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1999 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
2002 cb
.cb_sources
|= ZPROP_SRC_NONE
;
2005 (void) fprintf(stderr
,
2006 gettext("invalid source "
2015 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
2016 while (*optarg
!= '\0') {
2017 static char *type_subopts
[] = { "filesystem",
2018 "volume", "snapshot", "snap", "bookmark",
2021 switch (getsubopt(&optarg
, type_subopts
,
2024 types
|= ZFS_TYPE_FILESYSTEM
;
2027 types
|= ZFS_TYPE_VOLUME
;
2031 types
|= ZFS_TYPE_SNAPSHOT
;
2034 types
|= ZFS_TYPE_BOOKMARK
;
2037 types
= ZFS_TYPE_DATASET
|
2042 (void) fprintf(stderr
,
2043 gettext("invalid type '%s'\n"),
2051 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2061 (void) fprintf(stderr
, gettext("missing property "
2069 * Handle users who want to get all snapshots or bookmarks
2070 * of a dataset (ex. 'zfs get -t snapshot refer <dataset>').
2072 if ((types
== ZFS_TYPE_SNAPSHOT
|| types
== ZFS_TYPE_BOOKMARK
) &&
2073 argc
> 1 && (flags
& ZFS_ITER_RECURSE
) == 0 && limit
== 0) {
2074 flags
|= (ZFS_ITER_DEPTH_LIMIT
| ZFS_ITER_RECURSE
);
2078 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
2086 * As part of zfs_expand_proplist(), we keep track of the maximum column
2087 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
2088 * need to know the maximum name length. However, the user likely did
2089 * not specify 'name' as one of the properties to fetch, so we need to
2090 * make sure we always include at least this property for
2091 * print_get_headers() to work properly.
2093 if (cb
.cb_proplist
!= NULL
) {
2094 fake_name
.pl_prop
= ZFS_PROP_NAME
;
2095 fake_name
.pl_width
= strlen(gettext("NAME"));
2096 fake_name
.pl_next
= cb
.cb_proplist
;
2097 cb
.cb_proplist
= &fake_name
;
2100 cb
.cb_first
= B_TRUE
;
2102 /* run for each object */
2103 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
2104 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
2106 if (cb
.cb_proplist
== &fake_name
)
2107 zprop_free_list(fake_name
.pl_next
);
2109 zprop_free_list(cb
.cb_proplist
);
2115 * inherit [-rS] <property> <fs|vol> ...
2117 * -r Recurse over all children
2118 * -S Revert to received value, if any
2120 * For each dataset specified on the command line, inherit the given property
2121 * from its parent. Inheriting a property at the pool level will cause it to
2122 * use the default value. The '-r' flag will recurse over all children, and is
2123 * useful for setting a property on a hierarchy-wide basis, regardless of any
2124 * local modifications for each dataset.
2127 typedef struct inherit_cbdata
{
2128 const char *cb_propname
;
2129 boolean_t cb_received
;
2133 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
2135 inherit_cbdata_t
*cb
= data
;
2136 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
2139 * If we're doing it recursively, then ignore properties that
2140 * are not valid for this type of dataset.
2142 if (prop
!= ZPROP_INVAL
&&
2143 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
), B_FALSE
))
2146 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
2150 inherit_cb(zfs_handle_t
*zhp
, void *data
)
2152 inherit_cbdata_t
*cb
= data
;
2154 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
2158 zfs_do_inherit(int argc
, char **argv
)
2162 inherit_cbdata_t cb
= { 0 };
2166 boolean_t received
= B_FALSE
;
2169 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
2172 flags
|= ZFS_ITER_RECURSE
;
2179 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2188 /* check number of arguments */
2190 (void) fprintf(stderr
, gettext("missing property argument\n"));
2194 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
2202 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
2203 if (zfs_prop_readonly(prop
)) {
2204 (void) fprintf(stderr
, gettext(
2205 "%s property is read-only\n"),
2209 if (!zfs_prop_inheritable(prop
) && !received
) {
2210 (void) fprintf(stderr
, gettext("'%s' property cannot "
2211 "be inherited\n"), propname
);
2212 if (prop
== ZFS_PROP_QUOTA
||
2213 prop
== ZFS_PROP_RESERVATION
||
2214 prop
== ZFS_PROP_REFQUOTA
||
2215 prop
== ZFS_PROP_REFRESERVATION
) {
2216 (void) fprintf(stderr
, gettext("use 'zfs set "
2217 "%s=none' to clear\n"), propname
);
2218 (void) fprintf(stderr
, gettext("use 'zfs "
2219 "inherit -S %s' to revert to received "
2220 "value\n"), propname
);
2224 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
2225 prop
== ZFS_PROP_VERSION
)) {
2226 (void) fprintf(stderr
, gettext("'%s' property cannot "
2227 "be reverted to a received value\n"), propname
);
2230 } else if (!zfs_prop_user(propname
)) {
2231 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
2236 cb
.cb_propname
= propname
;
2237 cb
.cb_received
= received
;
2239 if (flags
& ZFS_ITER_RECURSE
) {
2240 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
2241 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
2243 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
2244 NULL
, NULL
, 0, inherit_cb
, &cb
);
2250 typedef struct upgrade_cbdata
{
2251 uint64_t cb_numupgraded
;
2252 uint64_t cb_numsamegraded
;
2253 uint64_t cb_numfailed
;
2254 uint64_t cb_version
;
2256 boolean_t cb_foundone
;
2257 char cb_lastfs
[ZFS_MAX_DATASET_NAME_LEN
];
2261 same_pool(zfs_handle_t
*zhp
, const char *name
)
2263 int len1
= strcspn(name
, "/@");
2264 const char *zhname
= zfs_get_name(zhp
);
2265 int len2
= strcspn(zhname
, "/@");
2269 return (strncmp(name
, zhname
, len1
) == 0);
2273 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
2275 upgrade_cbdata_t
*cb
= data
;
2276 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2278 /* list if it's old/new */
2279 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
2280 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
2283 str
= gettext("The following filesystems are "
2284 "formatted using a newer software version and\n"
2285 "cannot be accessed on the current system.\n\n");
2287 str
= gettext("The following filesystems are "
2288 "out of date, and can be upgraded. After being\n"
2289 "upgraded, these filesystems (and any 'zfs send' "
2290 "streams generated from\n"
2291 "subsequent snapshots) will no longer be "
2292 "accessible by older software versions.\n\n");
2295 if (!cb
->cb_foundone
) {
2297 (void) printf(gettext("VER FILESYSTEM\n"));
2298 (void) printf(gettext("--- ------------\n"));
2299 cb
->cb_foundone
= B_TRUE
;
2302 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
2309 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2311 upgrade_cbdata_t
*cb
= data
;
2312 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2313 int needed_spa_version
;
2316 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2319 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2321 if (needed_spa_version
< 0)
2324 if (spa_version
< needed_spa_version
) {
2326 (void) printf(gettext("%s: can not be "
2327 "upgraded; the pool version needs to first "
2328 "be upgraded\nto version %d\n\n"),
2329 zfs_get_name(zhp
), needed_spa_version
);
2335 if (version
< cb
->cb_version
) {
2337 (void) snprintf(verstr
, sizeof (verstr
),
2338 "%llu", (u_longlong_t
)cb
->cb_version
);
2339 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2341 * If they did "zfs upgrade -a", then we could
2342 * be doing ioctls to different pools. We need
2343 * to log this history once to each pool, and bypass
2344 * the normal history logging that happens in main().
2346 (void) zpool_log_history(g_zfs
, history_str
);
2347 log_history
= B_FALSE
;
2349 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2350 cb
->cb_numupgraded
++;
2353 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2354 } else if (version
> cb
->cb_version
) {
2355 /* can't downgrade */
2356 (void) printf(gettext("%s: can not be downgraded; "
2357 "it is already at version %u\n"),
2358 zfs_get_name(zhp
), version
);
2361 cb
->cb_numsamegraded
++;
2369 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2372 zfs_do_upgrade(int argc
, char **argv
)
2374 boolean_t all
= B_FALSE
;
2375 boolean_t showversions
= B_FALSE
;
2377 upgrade_cbdata_t cb
= { 0 };
2379 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2382 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2385 flags
|= ZFS_ITER_RECURSE
;
2388 showversions
= B_TRUE
;
2391 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2392 optarg
, &cb
.cb_version
) != 0) {
2393 (void) fprintf(stderr
,
2394 gettext("invalid version %s\n"), optarg
);
2403 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2412 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2414 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2415 cb
.cb_version
|| argc
))
2417 if ((all
|| argc
) && (showversions
))
2423 /* Show info on available versions. */
2424 (void) printf(gettext("The following filesystem versions are "
2426 (void) printf(gettext("VER DESCRIPTION\n"));
2427 (void) printf("--- -----------------------------------------"
2428 "---------------\n");
2429 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2430 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2431 (void) printf(gettext(" 3 Case insensitive and filesystem "
2432 "user identifier (FUID)\n"));
2433 (void) printf(gettext(" 4 userquota, groupquota "
2435 (void) printf(gettext(" 5 System attributes\n"));
2436 (void) printf(gettext("\nFor more information on a particular "
2437 "version, including supported releases,\n"));
2438 (void) printf("see the ZFS Administration Guide.\n\n");
2440 } else if (argc
|| all
) {
2441 /* Upgrade filesystems */
2442 if (cb
.cb_version
== 0)
2443 cb
.cb_version
= ZPL_VERSION
;
2444 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2445 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2446 (void) printf(gettext("%llu filesystems upgraded\n"),
2447 (u_longlong_t
)cb
.cb_numupgraded
);
2448 if (cb
.cb_numsamegraded
) {
2449 (void) printf(gettext("%llu filesystems already at "
2451 (u_longlong_t
)cb
.cb_numsamegraded
);
2453 if (cb
.cb_numfailed
!= 0)
2456 /* List old-version filesystems */
2458 (void) printf(gettext("This system is currently running "
2459 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2461 flags
|= ZFS_ITER_RECURSE
;
2462 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2463 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2465 found
= cb
.cb_foundone
;
2466 cb
.cb_foundone
= B_FALSE
;
2467 cb
.cb_newer
= B_TRUE
;
2469 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2470 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2472 if (!cb
.cb_foundone
&& !found
) {
2473 (void) printf(gettext("All filesystems are "
2474 "formatted with the current version.\n"));
2482 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2483 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2484 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2485 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2486 * zfs projectspace [-Hp] [-o field[,...]] [-s field [-s field]...]
2487 * [-S field [-S field]...] filesystem | snapshot
2489 * -H Scripted mode; elide headers and separate columns by tabs.
2490 * -i Translate SID to POSIX ID.
2491 * -n Print numeric ID instead of user/group name.
2492 * -o Control which fields to display.
2493 * -p Use exact (parsable) numeric output.
2494 * -s Specify sort columns, descending order.
2495 * -S Specify sort columns, ascending order.
2496 * -t Control which object types to display.
2498 * Displays space consumed by, and quotas on, each user in the specified
2499 * filesystem or snapshot.
2502 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2503 enum us_field_types
{
2511 static char *us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA",
2512 "OBJUSED", "OBJQUOTA" };
2513 static char *us_field_names
[] = { "type", "name", "used", "quota",
2514 "objused", "objquota" };
2515 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2517 #define USTYPE_PSX_GRP (1 << 0)
2518 #define USTYPE_PSX_USR (1 << 1)
2519 #define USTYPE_SMB_GRP (1 << 2)
2520 #define USTYPE_SMB_USR (1 << 3)
2521 #define USTYPE_PROJ (1 << 4)
2522 #define USTYPE_ALL \
2523 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR | \
2526 static int us_type_bits
[] = {
2533 static char *us_type_names
[] = { "posixgroup", "posixuser", "smbgroup",
2536 typedef struct us_node
{
2538 uu_avl_node_t usn_avlnode
;
2539 uu_list_node_t usn_listnode
;
2542 typedef struct us_cbdata
{
2544 uu_avl_pool_t
*cb_avl_pool
;
2546 boolean_t cb_numname
;
2547 boolean_t cb_nicenum
;
2548 boolean_t cb_sid2posix
;
2549 zfs_userquota_prop_t cb_prop
;
2550 zfs_sort_column_t
*cb_sortcol
;
2551 size_t cb_width
[USFIELD_LAST
];
2554 static boolean_t us_populated
= B_FALSE
;
2557 zfs_sort_column_t
*si_sortcol
;
2558 boolean_t si_numname
;
2562 us_field_index(char *field
)
2566 for (i
= 0; i
< USFIELD_LAST
; i
++) {
2567 if (strcmp(field
, us_field_names
[i
]) == 0)
2575 us_compare(const void *larg
, const void *rarg
, void *unused
)
2577 const us_node_t
*l
= larg
;
2578 const us_node_t
*r
= rarg
;
2579 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2580 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2581 boolean_t numname
= si
->si_numname
;
2582 nvlist_t
*lnvl
= l
->usn_nvl
;
2583 nvlist_t
*rnvl
= r
->usn_nvl
;
2587 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2594 zfs_prop_t prop
= sortcol
->sc_prop
;
2595 const char *propname
= NULL
;
2596 boolean_t reverse
= sortcol
->sc_reverse
;
2601 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2602 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2604 rc
= (rv32
< lv32
) ? 1 : -1;
2610 (void) nvlist_lookup_uint64(lnvl
, propname
,
2612 (void) nvlist_lookup_uint64(rnvl
, propname
,
2615 rc
= (rv64
< lv64
) ? 1 : -1;
2617 if ((nvlist_lookup_string(lnvl
, propname
,
2618 &lvstr
) == ENOENT
) ||
2619 (nvlist_lookup_string(rnvl
, propname
,
2620 &rvstr
) == ENOENT
)) {
2623 rc
= strcmp(lvstr
, rvstr
);
2627 case ZFS_PROP_QUOTA
:
2630 if (prop
== ZFS_PROP_USED
)
2634 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2635 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2637 rc
= (rv64
< lv64
) ? 1 : -1;
2646 return (reverse
? 1 : -1);
2648 return (reverse
? -1 : 1);
2653 * If entries still seem to be the same, check if they are of the same
2654 * type (smbentity is added only if we are doing SID to POSIX ID
2655 * translation where we can have duplicate type/name combinations).
2657 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2658 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2660 return (lvb
< rvb
? -1 : 1);
2666 zfs_prop_is_user(unsigned p
)
2668 return (p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
||
2669 p
== ZFS_PROP_USEROBJUSED
|| p
== ZFS_PROP_USEROBJQUOTA
);
2673 zfs_prop_is_group(unsigned p
)
2675 return (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
||
2676 p
== ZFS_PROP_GROUPOBJUSED
|| p
== ZFS_PROP_GROUPOBJQUOTA
);
2680 zfs_prop_is_project(unsigned p
)
2682 return (p
== ZFS_PROP_PROJECTUSED
|| p
== ZFS_PROP_PROJECTQUOTA
||
2683 p
== ZFS_PROP_PROJECTOBJUSED
|| p
== ZFS_PROP_PROJECTOBJQUOTA
);
2686 static inline const char *
2687 us_type2str(unsigned field_type
)
2689 switch (field_type
) {
2690 case USTYPE_PSX_USR
:
2691 return ("POSIX User");
2692 case USTYPE_PSX_GRP
:
2693 return ("POSIX Group");
2694 case USTYPE_SMB_USR
:
2695 return ("SMB User");
2696 case USTYPE_SMB_GRP
:
2697 return ("SMB Group");
2701 return ("Undefined");
2706 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2708 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2709 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2714 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2715 uu_avl_t
*avl
= cb
->cb_avl
;
2719 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2721 const char *typestr
;
2725 int typeidx
, nameidx
, sizeidx
;
2726 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2727 boolean_t smbentity
= B_FALSE
;
2729 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2731 node
= safe_malloc(sizeof (us_node_t
));
2732 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2733 node
->usn_nvl
= props
;
2735 if (domain
!= NULL
&& domain
[0] != '\0') {
2738 char sid
[MAXNAMELEN
+ 32];
2742 directory_error_t e
;
2746 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2748 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2749 type
= USTYPE_SMB_GRP
;
2750 err
= sid_to_id(sid
, B_FALSE
, &id
);
2752 type
= USTYPE_SMB_USR
;
2753 err
= sid_to_id(sid
, B_TRUE
, &id
);
2758 if (!cb
->cb_sid2posix
) {
2759 e
= directory_name_from_sid(NULL
, sid
, &name
,
2762 directory_error_free(e
);
2772 #endif /* HAVE_IDMAP */
2775 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2777 if (zfs_prop_is_group(prop
)) {
2778 type
= USTYPE_PSX_GRP
;
2779 if (!cb
->cb_numname
) {
2782 if ((g
= getgrgid(rid
)) != NULL
)
2785 } else if (zfs_prop_is_user(prop
)) {
2786 type
= USTYPE_PSX_USR
;
2787 if (!cb
->cb_numname
) {
2790 if ((p
= getpwuid(rid
)) != NULL
)
2799 * Make sure that the type/name combination is unique when doing
2800 * SID to POSIX ID translation (hence changing the type from SMB to
2803 if (cb
->cb_sid2posix
&&
2804 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2807 /* Calculate/update width of TYPE field */
2808 typestr
= us_type2str(type
);
2809 typelen
= strlen(gettext(typestr
));
2810 typeidx
= us_field_index("type");
2811 if (typelen
> cb
->cb_width
[typeidx
])
2812 cb
->cb_width
[typeidx
] = typelen
;
2813 if (nvlist_add_uint32(props
, "type", type
) != 0)
2816 /* Calculate/update width of NAME field */
2817 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2818 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2820 namelen
= snprintf(NULL
, 0, "%u", rid
);
2822 if (nvlist_add_string(props
, "name", name
) != 0)
2824 namelen
= strlen(name
);
2826 nameidx
= us_field_index("name");
2827 if (nameidx
>= 0 && namelen
> cb
->cb_width
[nameidx
])
2828 cb
->cb_width
[nameidx
] = namelen
;
2831 * Check if this type/name combination is in the list and update it;
2832 * otherwise add new node to the list.
2834 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2835 uu_avl_insert(avl
, node
, idx
);
2840 props
= node
->usn_nvl
;
2843 /* Calculate/update width of USED/QUOTA fields */
2844 if (cb
->cb_nicenum
) {
2845 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
||
2846 prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
||
2847 prop
== ZFS_PROP_PROJECTUSED
||
2848 prop
== ZFS_PROP_PROJECTQUOTA
) {
2849 zfs_nicebytes(space
, sizebuf
, sizeof (sizebuf
));
2851 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2854 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu",
2855 (u_longlong_t
)space
);
2857 sizelen
= strlen(sizebuf
);
2858 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
||
2859 prop
== ZFS_PROP_PROJECTUSED
) {
2861 if (!nvlist_exists(props
, "quota"))
2862 (void) nvlist_add_uint64(props
, "quota", 0);
2863 } else if (prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
||
2864 prop
== ZFS_PROP_PROJECTQUOTA
) {
2866 if (!nvlist_exists(props
, "used"))
2867 (void) nvlist_add_uint64(props
, "used", 0);
2868 } else if (prop
== ZFS_PROP_USEROBJUSED
||
2869 prop
== ZFS_PROP_GROUPOBJUSED
|| prop
== ZFS_PROP_PROJECTOBJUSED
) {
2870 propname
= "objused";
2871 if (!nvlist_exists(props
, "objquota"))
2872 (void) nvlist_add_uint64(props
, "objquota", 0);
2873 } else if (prop
== ZFS_PROP_USEROBJQUOTA
||
2874 prop
== ZFS_PROP_GROUPOBJQUOTA
||
2875 prop
== ZFS_PROP_PROJECTOBJQUOTA
) {
2876 propname
= "objquota";
2877 if (!nvlist_exists(props
, "objused"))
2878 (void) nvlist_add_uint64(props
, "objused", 0);
2882 sizeidx
= us_field_index(propname
);
2883 if (sizeidx
>= 0 && sizelen
> cb
->cb_width
[sizeidx
])
2884 cb
->cb_width
[sizeidx
] = sizelen
;
2886 if (nvlist_add_uint64(props
, propname
, space
) != 0)
2893 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
2894 size_t *width
, us_node_t
*node
)
2896 nvlist_t
*nvl
= node
->usn_nvl
;
2897 char valstr
[MAXNAMELEN
];
2898 boolean_t first
= B_TRUE
;
2904 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
2905 if (!(ustype
& types
))
2908 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
2909 nvpair_t
*nvp
= NULL
;
2915 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
2916 if (strcmp(nvpair_name(nvp
),
2917 us_field_names
[field
]) == 0)
2921 type
= nvp
== NULL
? DATA_TYPE_UNKNOWN
: nvpair_type(nvp
);
2923 case DATA_TYPE_UINT32
:
2924 (void) nvpair_value_uint32(nvp
, &val32
);
2926 case DATA_TYPE_UINT64
:
2927 (void) nvpair_value_uint64(nvp
, &val64
);
2929 case DATA_TYPE_STRING
:
2930 (void) nvpair_value_string(nvp
, &strval
);
2932 case DATA_TYPE_UNKNOWN
:
2935 (void) fprintf(stderr
, "invalid data type\n");
2940 if (type
== DATA_TYPE_UINT32
)
2941 strval
= (char *)us_type2str(val32
);
2944 if (type
== DATA_TYPE_UINT64
) {
2945 (void) sprintf(valstr
, "%llu",
2946 (u_longlong_t
)val64
);
2952 if (type
== DATA_TYPE_UINT64
) {
2954 (void) sprintf(valstr
, "%llu",
2955 (u_longlong_t
)val64
);
2957 } else if (field
== USFIELD_QUOTA
&&
2961 zfs_nicebytes(val64
, valstr
,
2967 case USFIELD_OBJUSED
:
2968 case USFIELD_OBJQUOTA
:
2969 if (type
== DATA_TYPE_UINT64
) {
2971 (void) sprintf(valstr
, "%llu",
2972 (u_longlong_t
)val64
);
2974 } else if (field
== USFIELD_OBJQUOTA
&&
2978 zfs_nicenum(val64
, valstr
,
2988 (void) printf("\t");
2993 (void) printf("%s", strval
);
2994 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
2995 (void) printf("%-*s", (int)width
[field
], strval
);
2997 (void) printf("%*s", (int)width
[field
], strval
);
3003 (void) printf("\n");
3007 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
3008 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
3016 boolean_t first
= B_TRUE
;
3018 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
3019 col
= gettext(us_field_hdr
[field
]);
3020 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
3021 (void) printf(first
? "%-*s" : " %-*s",
3022 (int)width
[field
], col
);
3024 (void) printf(first
? "%*s" : " %*s",
3025 (int)width
[field
], col
);
3030 (void) printf("\n");
3033 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
3034 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
3036 nvlist_free(node
->usn_nvl
);
3041 zfs_do_userspace(int argc
, char **argv
)
3044 zfs_userquota_prop_t p
;
3045 uu_avl_pool_t
*avl_pool
;
3047 uu_avl_walk_t
*walk
;
3049 char deffields
[] = "type,name,used,quota,objused,objquota";
3050 char *ofield
= NULL
;
3051 char *tfield
= NULL
;
3055 boolean_t scripted
= B_FALSE
;
3056 boolean_t prtnum
= B_FALSE
;
3057 boolean_t parsable
= B_FALSE
;
3058 boolean_t sid2posix
= B_FALSE
;
3061 zfs_sort_column_t
*sortcol
= NULL
;
3062 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
3066 uu_list_pool_t
*listpool
;
3068 uu_avl_index_t idx
= 0;
3069 uu_list_index_t idx2
= 0;
3074 if (strcmp(argv
[0], "groupspace") == 0) {
3075 /* Toggle default group types */
3076 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
3077 } else if (strcmp(argv
[0], "projectspace") == 0) {
3078 types
= USTYPE_PROJ
;
3082 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
3085 if (types
== USTYPE_PROJ
) {
3086 (void) fprintf(stderr
,
3087 gettext("invalid option 'n'\n"));
3103 if (zfs_add_sort_column(&sortcol
, optarg
,
3104 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
3105 (void) fprintf(stderr
,
3106 gettext("invalid field '%s'\n"), optarg
);
3111 if (types
== USTYPE_PROJ
) {
3112 (void) fprintf(stderr
,
3113 gettext("invalid option 't'\n"));
3119 if (types
== USTYPE_PROJ
) {
3120 (void) fprintf(stderr
,
3121 gettext("invalid option 'i'\n"));
3127 (void) fprintf(stderr
, gettext("missing argument for "
3128 "'%c' option\n"), optopt
);
3132 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3142 (void) fprintf(stderr
, gettext("missing dataset name\n"));
3146 (void) fprintf(stderr
, gettext("too many arguments\n"));
3150 /* Use default output fields if not specified using -o */
3154 if ((delim
= strchr(ofield
, ',')) != NULL
)
3156 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
3157 (void) fprintf(stderr
, gettext("invalid type '%s' "
3158 "for -o option\n"), ofield
);
3163 } while (delim
!= NULL
);
3164 fields
[cfield
] = USFIELD_LAST
;
3166 /* Override output types (-t option) */
3167 if (tfield
!= NULL
) {
3171 boolean_t found
= B_FALSE
;
3173 if ((delim
= strchr(tfield
, ',')) != NULL
)
3175 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
3177 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
3179 types
|= us_type_bits
[i
];
3184 (void) fprintf(stderr
, gettext("invalid type "
3185 "'%s' for -t option\n"), tfield
);
3190 } while (delim
!= NULL
);
3193 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
|
3194 ZFS_TYPE_SNAPSHOT
)) == NULL
)
3196 if (zhp
->zfs_head_type
!= ZFS_TYPE_FILESYSTEM
) {
3197 (void) fprintf(stderr
, gettext("operation is only applicable "
3198 "to filesystems and their snapshots\n"));
3203 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
3204 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
3206 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
3209 /* Always add default sorting columns */
3210 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
3211 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
3213 cb
.cb_sortcol
= sortcol
;
3214 cb
.cb_numname
= prtnum
;
3215 cb
.cb_nicenum
= !parsable
;
3216 cb
.cb_avl_pool
= avl_pool
;
3217 cb
.cb_avl
= avl_tree
;
3218 cb
.cb_sid2posix
= sid2posix
;
3220 for (i
= 0; i
< USFIELD_LAST
; i
++)
3221 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
3223 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
3224 if ((zfs_prop_is_user(p
) &&
3225 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
3226 (zfs_prop_is_group(p
) &&
3227 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))) ||
3228 (zfs_prop_is_project(p
) && types
!= USTYPE_PROJ
))
3232 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0) {
3240 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
3243 us_populated
= B_TRUE
;
3245 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
3246 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
3247 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
3248 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
3250 while (node
!= NULL
) {
3252 node
= uu_avl_next(avl_tree
, node
);
3253 uu_avl_remove(avl_tree
, rmnode
);
3254 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
3255 uu_list_insert(list
, rmnode
, idx2
);
3258 for (node
= uu_list_first(list
); node
!= NULL
;
3259 node
= uu_list_next(list
, node
)) {
3260 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
3262 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
3263 uu_avl_insert(avl_tree
, node
, idx
);
3266 uu_list_destroy(list
);
3267 uu_list_pool_destroy(listpool
);
3269 /* Print and free node nvlist memory */
3270 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
3273 zfs_free_sort_columns(sortcol
);
3275 /* Clean up the AVL tree */
3276 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
3279 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
3280 uu_avl_remove(cb
.cb_avl
, node
);
3284 uu_avl_walk_end(walk
);
3285 uu_avl_destroy(avl_tree
);
3286 uu_avl_pool_destroy(avl_pool
);
3292 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
3293 * [-t type[,...]] [filesystem|volume|snapshot] ...
3295 * -H Scripted mode; elide headers and separate columns by tabs
3296 * -p Display values in parsable (literal) format.
3297 * -r Recurse over all children
3298 * -d Limit recursion by depth.
3299 * -o Control which fields to display.
3300 * -s Specify sort columns, descending order.
3301 * -S Specify sort columns, ascending order.
3302 * -t Control which object types to display.
3304 * When given no arguments, list all filesystems in the system.
3305 * Otherwise, list the specified datasets, optionally recursing down them if
3306 * '-r' is specified.
3308 typedef struct list_cbdata
{
3310 boolean_t cb_literal
;
3311 boolean_t cb_scripted
;
3312 zprop_list_t
*cb_proplist
;
3316 * Given a list of columns to display, output appropriate headers for each one.
3319 print_header(list_cbdata_t
*cb
)
3321 zprop_list_t
*pl
= cb
->cb_proplist
;
3322 char headerbuf
[ZFS_MAXPROPLEN
];
3325 boolean_t first
= B_TRUE
;
3326 boolean_t right_justify
;
3328 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3335 right_justify
= B_FALSE
;
3336 if (pl
->pl_prop
!= ZPROP_INVAL
) {
3337 header
= zfs_prop_column_name(pl
->pl_prop
);
3338 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3340 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
3341 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
3342 headerbuf
[i
] = '\0';
3346 if (pl
->pl_next
== NULL
&& !right_justify
)
3347 (void) printf("%s", header
);
3348 else if (right_justify
)
3349 (void) printf("%*s", (int)pl
->pl_width
, header
);
3351 (void) printf("%-*s", (int)pl
->pl_width
, header
);
3354 (void) printf("\n");
3358 * Given a dataset and a list of fields, print out all the properties according
3359 * to the described layout.
3362 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
3364 zprop_list_t
*pl
= cb
->cb_proplist
;
3365 boolean_t first
= B_TRUE
;
3366 char property
[ZFS_MAXPROPLEN
];
3367 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
3370 boolean_t right_justify
;
3372 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3374 if (cb
->cb_scripted
)
3375 (void) printf("\t");
3382 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
3383 (void) strlcpy(property
, zfs_get_name(zhp
),
3386 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3387 } else if (pl
->pl_prop
!= ZPROP_INVAL
) {
3388 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
3389 sizeof (property
), NULL
, NULL
, 0,
3390 cb
->cb_literal
) != 0)
3394 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3395 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
3396 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
3397 property
, sizeof (property
), cb
->cb_literal
) != 0)
3401 right_justify
= B_TRUE
;
3402 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
3403 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
3404 property
, sizeof (property
), cb
->cb_literal
) != 0)
3408 right_justify
= B_TRUE
;
3410 if (nvlist_lookup_nvlist(userprops
,
3411 pl
->pl_user_prop
, &propval
) != 0)
3414 verify(nvlist_lookup_string(propval
,
3415 ZPROP_VALUE
, &propstr
) == 0);
3416 right_justify
= B_FALSE
;
3420 * If this is being called in scripted mode, or if this is the
3421 * last column and it is left-justified, don't include a width
3424 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3425 (void) printf("%s", propstr
);
3426 else if (right_justify
)
3427 (void) printf("%*s", (int)pl
->pl_width
, propstr
);
3429 (void) printf("%-*s", (int)pl
->pl_width
, propstr
);
3432 (void) printf("\n");
3436 * Generic callback function to list a dataset or snapshot.
3439 list_callback(zfs_handle_t
*zhp
, void *data
)
3441 list_cbdata_t
*cbp
= data
;
3443 if (cbp
->cb_first
) {
3444 if (!cbp
->cb_scripted
)
3446 cbp
->cb_first
= B_FALSE
;
3449 print_dataset(zhp
, cbp
);
3455 zfs_do_list(int argc
, char **argv
)
3458 static char default_fields
[] =
3459 "name,used,available,referenced,mountpoint";
3460 int types
= ZFS_TYPE_DATASET
;
3461 boolean_t types_specified
= B_FALSE
;
3462 char *fields
= NULL
;
3463 list_cbdata_t cb
= { 0 };
3467 zfs_sort_column_t
*sortcol
= NULL
;
3468 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3471 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3477 cb
.cb_literal
= B_TRUE
;
3478 flags
|= ZFS_ITER_LITERAL_PROPS
;
3481 limit
= parse_depth(optarg
, &flags
);
3484 flags
|= ZFS_ITER_RECURSE
;
3487 cb
.cb_scripted
= B_TRUE
;
3490 if (zfs_add_sort_column(&sortcol
, optarg
,
3492 (void) fprintf(stderr
,
3493 gettext("invalid property '%s'\n"), optarg
);
3498 if (zfs_add_sort_column(&sortcol
, optarg
,
3500 (void) fprintf(stderr
,
3501 gettext("invalid property '%s'\n"), optarg
);
3507 types_specified
= B_TRUE
;
3508 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3509 while (*optarg
!= '\0') {
3510 static char *type_subopts
[] = { "filesystem",
3511 "volume", "snapshot", "snap", "bookmark",
3514 switch (getsubopt(&optarg
, type_subopts
,
3517 types
|= ZFS_TYPE_FILESYSTEM
;
3520 types
|= ZFS_TYPE_VOLUME
;
3524 types
|= ZFS_TYPE_SNAPSHOT
;
3527 types
|= ZFS_TYPE_BOOKMARK
;
3530 types
= ZFS_TYPE_DATASET
|
3534 (void) fprintf(stderr
,
3535 gettext("invalid type '%s'\n"),
3542 (void) fprintf(stderr
, gettext("missing argument for "
3543 "'%c' option\n"), optopt
);
3547 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3557 fields
= default_fields
;
3560 * If we are only going to list snapshot names and sort by name,
3561 * then we can use faster version.
3563 if (strcmp(fields
, "name") == 0 && zfs_sort_only_by_name(sortcol
))
3564 flags
|= ZFS_ITER_SIMPLE
;
3567 * If "-o space" and no types were specified, don't display snapshots.
3569 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3570 types
&= ~ZFS_TYPE_SNAPSHOT
;
3573 * Handle users who want to list all snapshots or bookmarks
3574 * of the current dataset (ex. 'zfs list -t snapshot <dataset>').
3576 if ((types
== ZFS_TYPE_SNAPSHOT
|| types
== ZFS_TYPE_BOOKMARK
) &&
3577 argc
> 0 && (flags
& ZFS_ITER_RECURSE
) == 0 && limit
== 0) {
3578 flags
|= (ZFS_ITER_DEPTH_LIMIT
| ZFS_ITER_RECURSE
);
3583 * If the user specifies '-o all', the zprop_get_list() doesn't
3584 * normally include the name of the dataset. For 'zfs list', we always
3585 * want this property to be first.
3587 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3591 cb
.cb_first
= B_TRUE
;
3593 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3594 limit
, list_callback
, &cb
);
3596 zprop_free_list(cb
.cb_proplist
);
3597 zfs_free_sort_columns(sortcol
);
3599 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3600 (void) fprintf(stderr
, gettext("no datasets available\n"));
3606 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3607 * zfs rename [-f] -p <fs | vol> <fs | vol>
3608 * zfs rename -r <snap> <snap>
3610 * Renames the given dataset to another of the same type.
3612 * The '-p' flag creates all the non-existing ancestors of the target first.
3616 zfs_do_rename(int argc
, char **argv
)
3621 boolean_t recurse
= B_FALSE
;
3622 boolean_t parents
= B_FALSE
;
3623 boolean_t force_unmount
= B_FALSE
;
3626 while ((c
= getopt(argc
, argv
, "prf")) != -1) {
3635 force_unmount
= B_TRUE
;
3639 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3648 /* check number of arguments */
3650 (void) fprintf(stderr
, gettext("missing source dataset "
3655 (void) fprintf(stderr
, gettext("missing target dataset "
3660 (void) fprintf(stderr
, gettext("too many arguments\n"));
3664 if (recurse
&& parents
) {
3665 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3670 if (recurse
&& strchr(argv
[0], '@') == 0) {
3671 (void) fprintf(stderr
, gettext("source dataset for recursive "
3672 "rename must be a snapshot\n"));
3676 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
3677 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
3680 /* If we were asked and the name looks good, try to create ancestors. */
3681 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3682 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3687 ret
= (zfs_rename(zhp
, argv
[1], recurse
, force_unmount
) != 0);
3696 * Promotes the given clone fs to be the parent
3700 zfs_do_promote(int argc
, char **argv
)
3706 if (argc
> 1 && argv
[1][0] == '-') {
3707 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3712 /* check number of arguments */
3714 (void) fprintf(stderr
, gettext("missing clone filesystem"
3719 (void) fprintf(stderr
, gettext("too many arguments\n"));
3723 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3727 ret
= (zfs_promote(zhp
) != 0);
3735 zfs_do_redact(int argc
, char **argv
)
3738 char *bookname
= NULL
;
3739 char **rsnaps
= NULL
;
3744 (void) fprintf(stderr
, gettext("too few arguments\n"));
3751 numrsnaps
= argc
- 2;
3753 nvlist_t
*rsnapnv
= fnvlist_alloc();
3755 for (int i
= 0; i
< numrsnaps
; i
++) {
3756 fnvlist_add_boolean(rsnapnv
, rsnaps
[i
]);
3759 int err
= lzc_redact(snap
, bookname
, rsnapnv
);
3760 fnvlist_free(rsnapnv
);
3766 (void) fprintf(stderr
,
3767 gettext("provided snapshot %s does not exist\n"), snap
);
3770 (void) fprintf(stderr
, gettext("specified redaction bookmark "
3771 "(%s) provided already exists\n"), bookname
);
3774 (void) fprintf(stderr
, gettext("provided bookmark name cannot "
3775 "be used, final name would be too long\n"));
3778 (void) fprintf(stderr
, gettext("too many redaction snapshots "
3782 if (strchr(bookname
, '#') != NULL
)
3783 (void) fprintf(stderr
, gettext(
3784 "redaction bookmark name must not contain '#'\n"));
3786 (void) fprintf(stderr
, gettext(
3787 "redaction snapshot must be descendent of "
3788 "snapshot being redacted\n"));
3791 (void) fprintf(stderr
, gettext("attempted to redact redacted "
3792 "dataset or with respect to redacted dataset\n"));
3795 (void) fprintf(stderr
, gettext("redaction bookmarks feature "
3799 (void) fprintf(stderr
, gettext("potentially invalid redaction "
3800 "snapshot; full dataset names required\n"));
3803 (void) fprintf(stderr
, gettext("internal error: %s\n"),
3811 * zfs rollback [-rRf] <snapshot>
3813 * -r Delete any intervening snapshots before doing rollback
3814 * -R Delete any snapshots and their clones
3815 * -f ignored for backwards compatibility
3817 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3818 * since then and making it the active dataset. If more recent snapshots exist,
3819 * the command will complain unless the '-r' flag is given.
3821 typedef struct rollback_cbdata
{
3823 uint8_t cb_younger_ds_printed
;
3828 boolean_t cb_recurse
;
3829 } rollback_cbdata_t
;
3832 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3834 rollback_cbdata_t
*cbp
= data
;
3836 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3837 (void) fprintf(stderr
, gettext("cannot rollback to "
3838 "'%s': clones of previous snapshots exist\n"),
3840 (void) fprintf(stderr
, gettext("use '-R' to "
3841 "force deletion of the following clones and "
3847 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3855 * Report some snapshots/bookmarks more recent than the one specified.
3856 * Used when '-r' is not specified. We reuse this same callback for the
3857 * snapshot dependents - if 'cb_dependent' is set, then this is a
3858 * dependent and we should report it without checking the transaction group.
3861 rollback_check(zfs_handle_t
*zhp
, void *data
)
3863 rollback_cbdata_t
*cbp
= data
;
3865 * Max number of younger snapshots and/or bookmarks to display before
3866 * we stop the iteration.
3868 const uint8_t max_younger
= 32;
3870 if (cbp
->cb_doclones
) {
3875 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3876 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3877 (void) fprintf(stderr
, gettext("cannot "
3878 "rollback to '%s': more recent snapshots "
3879 "or bookmarks exist\n"),
3881 (void) fprintf(stderr
, gettext("use '-r' to "
3882 "force deletion of the following "
3883 "snapshots and bookmarks:\n"));
3888 if (cbp
->cb_recurse
) {
3889 if (zfs_iter_dependents(zhp
, B_TRUE
,
3890 rollback_check_dependent
, cbp
) != 0) {
3895 (void) fprintf(stderr
, "%s\n",
3897 cbp
->cb_younger_ds_printed
++;
3902 if (cbp
->cb_younger_ds_printed
== max_younger
) {
3904 * This non-recursive rollback is going to fail due to the
3905 * presence of snapshots and/or bookmarks that are younger than
3906 * the rollback target.
3907 * We printed some of the offending objects, now we stop
3908 * zfs_iter_snapshot/bookmark iteration so we can fail fast and
3909 * avoid iterating over the rest of the younger objects
3911 (void) fprintf(stderr
, gettext("Output limited to %d "
3912 "snapshots/bookmarks\n"), max_younger
);
3919 zfs_do_rollback(int argc
, char **argv
)
3923 boolean_t force
= B_FALSE
;
3924 rollback_cbdata_t cb
= { 0 };
3925 zfs_handle_t
*zhp
, *snap
;
3926 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3928 uint64_t min_txg
= 0;
3931 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
3944 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3953 /* check number of arguments */
3955 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
3959 (void) fprintf(stderr
, gettext("too many arguments\n"));
3963 /* open the snapshot */
3964 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
3967 /* open the parent dataset */
3968 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
3969 verify((delim
= strrchr(parentname
, '@')) != NULL
);
3971 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
3977 * Check for more recent snapshots and/or clones based on the presence
3980 cb
.cb_target
= argv
[0];
3981 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3982 cb
.cb_first
= B_TRUE
;
3985 if (cb
.cb_create
> 0)
3986 min_txg
= cb
.cb_create
;
3988 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, rollback_check
, &cb
,
3991 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
3994 if ((ret
= cb
.cb_error
) != 0)
3998 * Rollback parent to the given snapshot.
4000 ret
= zfs_rollback(zhp
, snap
, force
);
4013 * zfs set property=value ... { fs | snap | vol } ...
4015 * Sets the given properties for all datasets specified on the command line.
4019 set_callback(zfs_handle_t
*zhp
, void *data
)
4021 nvlist_t
*props
= data
;
4023 if (zfs_prop_set_list(zhp
, props
) != 0) {
4024 switch (libzfs_errno(g_zfs
)) {
4025 case EZFS_MOUNTFAILED
:
4026 (void) fprintf(stderr
, gettext("property may be set "
4027 "but unable to remount filesystem\n"));
4029 case EZFS_SHARENFSFAILED
:
4030 (void) fprintf(stderr
, gettext("property may be set "
4031 "but unable to reshare filesystem\n"));
4040 zfs_do_set(int argc
, char **argv
)
4042 nvlist_t
*props
= NULL
;
4043 int ds_start
= -1; /* argv idx of first dataset arg */
4047 /* check for options */
4048 if (argc
> 1 && argv
[1][0] == '-') {
4049 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4054 /* check number of arguments */
4056 (void) fprintf(stderr
, gettext("missing arguments\n"));
4060 if (strchr(argv
[1], '=') == NULL
) {
4061 (void) fprintf(stderr
, gettext("missing property=value "
4064 (void) fprintf(stderr
, gettext("missing dataset "
4070 /* validate argument order: prop=val args followed by dataset args */
4071 for (i
= 1; i
< argc
; i
++) {
4072 if (strchr(argv
[i
], '=') != NULL
) {
4074 /* out-of-order prop=val argument */
4075 (void) fprintf(stderr
, gettext("invalid "
4076 "argument order\n"));
4079 } else if (ds_start
< 0) {
4084 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
4088 /* Populate a list of property settings */
4089 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
4091 for (i
= 1; i
< ds_start
; i
++) {
4092 if (!parseprop(props
, argv
[i
])) {
4098 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
4099 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, props
);
4106 typedef struct snap_cbdata
{
4108 boolean_t sd_recursive
;
4109 const char *sd_snapname
;
4113 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
4115 snap_cbdata_t
*sd
= arg
;
4120 if (sd
->sd_recursive
&&
4121 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
4126 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
4129 fnvlist_add_boolean(sd
->sd_nvl
, name
);
4132 if (sd
->sd_recursive
)
4133 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
4139 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
4141 * Creates a snapshot with the given name. While functionally equivalent to
4142 * 'zfs create', it is a separate command to differentiate intent.
4145 zfs_do_snapshot(int argc
, char **argv
)
4150 snap_cbdata_t sd
= { 0 };
4151 boolean_t multiple_snaps
= B_FALSE
;
4153 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
4155 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4159 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
4162 if (!parseprop(props
, optarg
)) {
4163 nvlist_free(sd
.sd_nvl
);
4169 sd
.sd_recursive
= B_TRUE
;
4170 multiple_snaps
= B_TRUE
;
4173 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4182 /* check number of arguments */
4184 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4189 multiple_snaps
= B_TRUE
;
4190 for (; argc
> 0; argc
--, argv
++) {
4194 atp
= strchr(argv
[0], '@');
4198 sd
.sd_snapname
= atp
+ 1;
4199 zhp
= zfs_open(g_zfs
, argv
[0],
4200 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4203 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
4207 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
4208 nvlist_free(sd
.sd_nvl
);
4210 if (ret
!= 0 && multiple_snaps
)
4211 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
4215 nvlist_free(sd
.sd_nvl
);
4223 * Send a backup stream to stdout.
4226 zfs_do_send(int argc
, char **argv
)
4228 char *fromname
= NULL
;
4229 char *toname
= NULL
;
4230 char *resume_token
= NULL
;
4233 sendflags_t flags
= { 0 };
4235 nvlist_t
*dbgnv
= NULL
;
4236 char *redactbook
= NULL
;
4238 struct option long_options
[] = {
4239 {"replicate", no_argument
, NULL
, 'R'},
4240 {"redact", required_argument
, NULL
, 'd'},
4241 {"props", no_argument
, NULL
, 'p'},
4242 {"parsable", no_argument
, NULL
, 'P'},
4243 {"dedup", no_argument
, NULL
, 'D'},
4244 {"verbose", no_argument
, NULL
, 'v'},
4245 {"dryrun", no_argument
, NULL
, 'n'},
4246 {"large-block", no_argument
, NULL
, 'L'},
4247 {"embed", no_argument
, NULL
, 'e'},
4248 {"resume", required_argument
, NULL
, 't'},
4249 {"compressed", no_argument
, NULL
, 'c'},
4250 {"raw", no_argument
, NULL
, 'w'},
4251 {"backup", no_argument
, NULL
, 'b'},
4252 {"holds", no_argument
, NULL
, 'h'},
4253 {"saved", no_argument
, NULL
, 'S'},
4258 while ((c
= getopt_long(argc
, argv
, ":i:I:RDpvnPLeht:cwbd:S",
4259 long_options
, NULL
)) != -1) {
4270 flags
.doall
= B_TRUE
;
4273 flags
.replicate
= B_TRUE
;
4276 redactbook
= optarg
;
4279 flags
.props
= B_TRUE
;
4282 flags
.backup
= B_TRUE
;
4285 flags
.holds
= B_TRUE
;
4288 flags
.parsable
= B_TRUE
;
4292 flags
.progress
= B_TRUE
;
4295 (void) fprintf(stderr
,
4296 gettext("WARNING: deduplicated send is no "
4297 "longer supported. A regular,\n"
4298 "non-deduplicated stream will be generated.\n\n"));
4301 flags
.dryrun
= B_TRUE
;
4304 flags
.largeblock
= B_TRUE
;
4307 flags
.embed_data
= B_TRUE
;
4310 resume_token
= optarg
;
4313 flags
.compress
= B_TRUE
;
4317 flags
.compress
= B_TRUE
;
4318 flags
.embed_data
= B_TRUE
;
4319 flags
.largeblock
= B_TRUE
;
4322 flags
.saved
= B_TRUE
;
4326 * If a parameter was not passed, optopt contains the
4327 * value that would normally lead us into the
4328 * appropriate case statement. If it's > 256, then this
4329 * must be a longopt and we should look at argv to get
4330 * the string. Otherwise it's just the character, so we
4331 * should use it directly.
4333 if (optopt
<= UINT8_MAX
) {
4334 (void) fprintf(stderr
,
4335 gettext("missing argument for '%c' "
4336 "option\n"), optopt
);
4338 (void) fprintf(stderr
,
4339 gettext("missing argument for '%s' "
4340 "option\n"), argv
[optind
- 1]);
4348 * If an invalid flag was passed, optopt contains the
4349 * character if it was a short flag, or 0 if it was a
4353 (void) fprintf(stderr
,
4354 gettext("invalid option '%c'\n"), optopt
);
4356 (void) fprintf(stderr
,
4357 gettext("invalid option '%s'\n"),
4365 if (flags
.parsable
&& flags
.verbosity
== 0)
4366 flags
.verbosity
= 1;
4371 if (resume_token
!= NULL
) {
4372 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
4373 flags
.backup
|| flags
.holds
||
4374 flags
.saved
|| redactbook
!= NULL
) {
4375 (void) fprintf(stderr
,
4376 gettext("invalid flags combined with -t\n"));
4380 (void) fprintf(stderr
, gettext("too many arguments\n"));
4385 (void) fprintf(stderr
,
4386 gettext("missing snapshot argument\n"));
4390 (void) fprintf(stderr
, gettext("too many arguments\n"));
4396 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
4397 flags
.doall
|| flags
.backup
||
4398 flags
.holds
|| flags
.largeblock
|| flags
.embed_data
||
4399 flags
.compress
|| flags
.raw
|| redactbook
!= NULL
) {
4400 (void) fprintf(stderr
, gettext("incompatible flags "
4401 "combined with saved send flag\n"));
4404 if (strchr(argv
[0], '@') != NULL
) {
4405 (void) fprintf(stderr
, gettext("saved send must "
4406 "specify the dataset with partially-received "
4412 if (flags
.raw
&& redactbook
!= NULL
) {
4413 (void) fprintf(stderr
,
4414 gettext("Error: raw sends may not be redacted.\n"));
4418 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
4419 (void) fprintf(stderr
,
4420 gettext("Error: Stream can not be written to a terminal.\n"
4421 "You must redirect standard output.\n"));
4426 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
4430 err
= zfs_send_saved(zhp
, &flags
, STDOUT_FILENO
,
4434 } else if (resume_token
!= NULL
) {
4435 return (zfs_send_resume(g_zfs
, &flags
, STDOUT_FILENO
,
4440 * For everything except -R and -I, use the new, cleaner code path.
4442 if (!(flags
.replicate
|| flags
.doall
)) {
4443 char frombuf
[ZFS_MAX_DATASET_NAME_LEN
];
4445 if (fromname
!= NULL
&& (strchr(fromname
, '#') == NULL
&&
4446 strchr(fromname
, '@') == NULL
)) {
4448 * Neither bookmark or snapshot was specified. Print a
4449 * warning, and assume snapshot.
4451 (void) fprintf(stderr
, "Warning: incremental source "
4452 "didn't specify type, assuming snapshot. Use '@' "
4453 "or '#' prefix to avoid ambiguity.\n");
4454 (void) snprintf(frombuf
, sizeof (frombuf
), "@%s",
4458 if (fromname
!= NULL
&&
4459 (fromname
[0] == '#' || fromname
[0] == '@')) {
4461 * Incremental source name begins with # or @.
4462 * Default to same fs as target.
4464 char tmpbuf
[ZFS_MAX_DATASET_NAME_LEN
];
4465 (void) strlcpy(tmpbuf
, fromname
, sizeof (tmpbuf
));
4466 (void) strlcpy(frombuf
, argv
[0], sizeof (frombuf
));
4467 cp
= strchr(frombuf
, '@');
4470 (void) strlcat(frombuf
, tmpbuf
, sizeof (frombuf
));
4474 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
4477 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, &flags
,
4483 if (fromname
!= NULL
&& strchr(fromname
, '#')) {
4484 (void) fprintf(stderr
,
4485 gettext("Error: multiple snapshots cannot be "
4486 "sent from a bookmark.\n"));
4490 if (redactbook
!= NULL
) {
4491 (void) fprintf(stderr
, gettext("Error: multiple snapshots "
4492 "cannot be sent redacted.\n"));
4496 if ((cp
= strchr(argv
[0], '@')) == NULL
) {
4497 (void) fprintf(stderr
, gettext("Error: "
4498 "Unsupported flag with filesystem or bookmark.\n"));
4503 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4508 * If they specified the full path to the snapshot, chop off
4509 * everything except the short name of the snapshot, but special
4510 * case if they specify the origin.
4512 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
4513 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
4516 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
4517 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
4519 if (strcmp(origin
, fromname
) == 0) {
4521 flags
.fromorigin
= B_TRUE
;
4524 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
4525 (void) fprintf(stderr
,
4526 gettext("incremental source must be "
4527 "in same filesystem\n"));
4531 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
4532 (void) fprintf(stderr
,
4533 gettext("invalid incremental source\n"));
4539 if (flags
.replicate
&& fromname
== NULL
)
4540 flags
.doall
= B_TRUE
;
4542 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
, NULL
, 0,
4543 flags
.verbosity
>= 3 ? &dbgnv
: NULL
);
4545 if (flags
.verbosity
>= 3 && dbgnv
!= NULL
) {
4547 * dump_nvlist prints to stdout, but that's been
4548 * redirected to a file. Make it print to stderr
4551 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
4552 dump_nvlist(dbgnv
, 0);
4561 * Restore a backup stream from stdin.
4564 zfs_do_receive(int argc
, char **argv
)
4567 recvflags_t flags
= { 0 };
4568 boolean_t abort_resumable
= B_FALSE
;
4571 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
4575 while ((c
= getopt(argc
, argv
, ":o:x:dehMnuvFsA")) != -1) {
4578 if (!parseprop(props
, optarg
)) {
4584 if (!parsepropname(props
, optarg
)) {
4591 (void) fprintf(stderr
, gettext("invalid option "
4592 "combination: -d and -e are mutually "
4596 flags
.isprefix
= B_TRUE
;
4599 if (flags
.isprefix
) {
4600 (void) fprintf(stderr
, gettext("invalid option "
4601 "combination: -d and -e are mutually "
4605 flags
.istail
= B_TRUE
;
4608 flags
.skipholds
= B_TRUE
;
4611 flags
.forceunmount
= B_TRUE
;
4614 flags
.dryrun
= B_TRUE
;
4617 flags
.nomount
= B_TRUE
;
4620 flags
.verbose
= B_TRUE
;
4623 flags
.resumable
= B_TRUE
;
4626 flags
.force
= B_TRUE
;
4629 abort_resumable
= B_TRUE
;
4632 (void) fprintf(stderr
, gettext("missing argument for "
4633 "'%c' option\n"), optopt
);
4637 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4646 /* zfs recv -e (use "tail" name) implies -d (remove dataset "head") */
4648 flags
.isprefix
= B_TRUE
;
4650 /* check number of arguments */
4652 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4656 (void) fprintf(stderr
, gettext("too many arguments\n"));
4660 if (abort_resumable
) {
4661 if (flags
.isprefix
|| flags
.istail
|| flags
.dryrun
||
4662 flags
.resumable
|| flags
.nomount
) {
4663 (void) fprintf(stderr
, gettext("invalid option\n"));
4667 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
4668 (void) snprintf(namebuf
, sizeof (namebuf
),
4669 "%s/%%recv", argv
[0]);
4671 if (zfs_dataset_exists(g_zfs
, namebuf
,
4672 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) {
4673 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4674 namebuf
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4679 err
= zfs_destroy(zhp
, B_FALSE
);
4682 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4683 argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4686 if (!zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) ||
4687 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
4688 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == -1) {
4689 (void) fprintf(stderr
,
4690 gettext("'%s' does not have any "
4691 "resumable receive state to abort\n"),
4697 err
= zfs_destroy(zhp
, B_FALSE
);
4704 if (isatty(STDIN_FILENO
)) {
4705 (void) fprintf(stderr
,
4706 gettext("Error: Backup stream can not be read "
4707 "from a terminal.\n"
4708 "You must redirect standard input.\n"));
4712 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
4719 * allow/unallow stuff
4721 /* copied from zfs/sys/dsl_deleg.h */
4722 #define ZFS_DELEG_PERM_CREATE "create"
4723 #define ZFS_DELEG_PERM_DESTROY "destroy"
4724 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4725 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4726 #define ZFS_DELEG_PERM_CLONE "clone"
4727 #define ZFS_DELEG_PERM_PROMOTE "promote"
4728 #define ZFS_DELEG_PERM_RENAME "rename"
4729 #define ZFS_DELEG_PERM_MOUNT "mount"
4730 #define ZFS_DELEG_PERM_SHARE "share"
4731 #define ZFS_DELEG_PERM_SEND "send"
4732 #define ZFS_DELEG_PERM_RECEIVE "receive"
4733 #define ZFS_DELEG_PERM_ALLOW "allow"
4734 #define ZFS_DELEG_PERM_USERPROP "userprop"
4735 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4736 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4737 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4738 #define ZFS_DELEG_PERM_USERUSED "userused"
4739 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4740 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
4741 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
4742 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
4743 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
4745 #define ZFS_DELEG_PERM_HOLD "hold"
4746 #define ZFS_DELEG_PERM_RELEASE "release"
4747 #define ZFS_DELEG_PERM_DIFF "diff"
4748 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4749 #define ZFS_DELEG_PERM_LOAD_KEY "load-key"
4750 #define ZFS_DELEG_PERM_CHANGE_KEY "change-key"
4752 #define ZFS_DELEG_PERM_PROJECTUSED "projectused"
4753 #define ZFS_DELEG_PERM_PROJECTQUOTA "projectquota"
4754 #define ZFS_DELEG_PERM_PROJECTOBJUSED "projectobjused"
4755 #define ZFS_DELEG_PERM_PROJECTOBJQUOTA "projectobjquota"
4757 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4759 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
4760 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
4761 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
4762 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
4763 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
4764 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
4765 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
4766 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
4767 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
4768 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
4769 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
4770 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
4771 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
4772 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
4773 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
4774 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
4775 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
4776 { ZFS_DELEG_PERM_LOAD_KEY
, ZFS_DELEG_NOTE_LOAD_KEY
},
4777 { ZFS_DELEG_PERM_CHANGE_KEY
, ZFS_DELEG_NOTE_CHANGE_KEY
},
4779 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
4780 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
4781 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
4782 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
4783 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
4784 { ZFS_DELEG_PERM_USEROBJQUOTA
, ZFS_DELEG_NOTE_USEROBJQUOTA
},
4785 { ZFS_DELEG_PERM_USEROBJUSED
, ZFS_DELEG_NOTE_USEROBJUSED
},
4786 { ZFS_DELEG_PERM_GROUPOBJQUOTA
, ZFS_DELEG_NOTE_GROUPOBJQUOTA
},
4787 { ZFS_DELEG_PERM_GROUPOBJUSED
, ZFS_DELEG_NOTE_GROUPOBJUSED
},
4788 { ZFS_DELEG_PERM_PROJECTUSED
, ZFS_DELEG_NOTE_PROJECTUSED
},
4789 { ZFS_DELEG_PERM_PROJECTQUOTA
, ZFS_DELEG_NOTE_PROJECTQUOTA
},
4790 { ZFS_DELEG_PERM_PROJECTOBJUSED
, ZFS_DELEG_NOTE_PROJECTOBJUSED
},
4791 { ZFS_DELEG_PERM_PROJECTOBJQUOTA
, ZFS_DELEG_NOTE_PROJECTOBJQUOTA
},
4792 { NULL
, ZFS_DELEG_NOTE_NONE
}
4795 /* permission structure */
4796 typedef struct deleg_perm
{
4797 zfs_deleg_who_type_t dp_who_type
;
4798 const char *dp_name
;
4800 boolean_t dp_descend
;
4804 typedef struct deleg_perm_node
{
4805 deleg_perm_t dpn_perm
;
4807 uu_avl_node_t dpn_avl_node
;
4808 } deleg_perm_node_t
;
4810 typedef struct fs_perm fs_perm_t
;
4812 /* permissions set */
4813 typedef struct who_perm
{
4814 zfs_deleg_who_type_t who_type
;
4815 const char *who_name
; /* id */
4816 char who_ug_name
[256]; /* user/group name */
4817 fs_perm_t
*who_fsperm
; /* uplink */
4819 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
4823 typedef struct who_perm_node
{
4824 who_perm_t who_perm
;
4825 uu_avl_node_t who_avl_node
;
4828 typedef struct fs_perm_set fs_perm_set_t
;
4829 /* fs permissions */
4831 const char *fsp_name
;
4833 uu_avl_t
*fsp_sc_avl
; /* sets,create */
4834 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
4836 fs_perm_set_t
*fsp_set
; /* uplink */
4840 typedef struct fs_perm_node
{
4841 fs_perm_t fspn_fsperm
;
4844 uu_list_node_t fspn_list_node
;
4847 /* top level structure */
4848 struct fs_perm_set
{
4849 uu_list_pool_t
*fsps_list_pool
;
4850 uu_list_t
*fsps_list
; /* list of fs_perms */
4852 uu_avl_pool_t
*fsps_named_set_avl_pool
;
4853 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
4854 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
4857 static inline const char *
4858 deleg_perm_type(zfs_deleg_note_t note
)
4864 case ZFS_DELEG_NOTE_GROUPQUOTA
:
4865 case ZFS_DELEG_NOTE_GROUPUSED
:
4866 case ZFS_DELEG_NOTE_USERPROP
:
4867 case ZFS_DELEG_NOTE_USERQUOTA
:
4868 case ZFS_DELEG_NOTE_USERUSED
:
4869 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
4870 case ZFS_DELEG_NOTE_USEROBJUSED
:
4871 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
4872 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
4873 case ZFS_DELEG_NOTE_PROJECTUSED
:
4874 case ZFS_DELEG_NOTE_PROJECTQUOTA
:
4875 case ZFS_DELEG_NOTE_PROJECTOBJUSED
:
4876 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA
:
4878 return (gettext("other"));
4880 return (gettext("subcommand"));
4885 who_type2weight(zfs_deleg_who_type_t who_type
)
4889 case ZFS_DELEG_NAMED_SET_SETS
:
4890 case ZFS_DELEG_NAMED_SET
:
4893 case ZFS_DELEG_CREATE_SETS
:
4894 case ZFS_DELEG_CREATE
:
4897 case ZFS_DELEG_USER_SETS
:
4898 case ZFS_DELEG_USER
:
4901 case ZFS_DELEG_GROUP_SETS
:
4902 case ZFS_DELEG_GROUP
:
4905 case ZFS_DELEG_EVERYONE_SETS
:
4906 case ZFS_DELEG_EVERYONE
:
4918 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4920 const who_perm_node_t
*l
= larg
;
4921 const who_perm_node_t
*r
= rarg
;
4922 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
4923 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
4924 int lweight
= who_type2weight(ltype
);
4925 int rweight
= who_type2weight(rtype
);
4926 int res
= lweight
- rweight
;
4928 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
4929 ZFS_MAX_DELEG_NAME
-1);
4941 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
4943 const deleg_perm_node_t
*l
= larg
;
4944 const deleg_perm_node_t
*r
= rarg
;
4945 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
4946 ZFS_MAX_DELEG_NAME
-1);
4958 fs_perm_set_init(fs_perm_set_t
*fspset
)
4960 bzero(fspset
, sizeof (fs_perm_set_t
));
4962 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
4963 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
4964 NULL
, UU_DEFAULT
)) == NULL
)
4966 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
4967 UU_DEFAULT
)) == NULL
)
4970 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
4971 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
4972 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4973 UU_DEFAULT
)) == NULL
)
4976 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
4977 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
4978 who_perm_node_t
, who_avl_node
), who_perm_compare
,
4979 UU_DEFAULT
)) == NULL
)
4982 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
4983 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
4984 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
4989 static inline void fs_perm_fini(fs_perm_t
*);
4990 static inline void who_perm_fini(who_perm_t
*);
4993 fs_perm_set_fini(fs_perm_set_t
*fspset
)
4995 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
4997 while (node
!= NULL
) {
4998 fs_perm_node_t
*next_node
=
4999 uu_list_next(fspset
->fsps_list
, node
);
5000 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
5001 fs_perm_fini(fsperm
);
5002 uu_list_remove(fspset
->fsps_list
, node
);
5007 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
5008 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
5009 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
5013 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
5016 deleg_perm
->dp_who_type
= type
;
5017 deleg_perm
->dp_name
= name
;
5021 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
5022 zfs_deleg_who_type_t type
, const char *name
)
5024 uu_avl_pool_t
*pool
;
5025 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
5027 bzero(who_perm
, sizeof (who_perm_t
));
5029 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
5030 UU_DEFAULT
)) == NULL
)
5033 who_perm
->who_type
= type
;
5034 who_perm
->who_name
= name
;
5035 who_perm
->who_fsperm
= fsperm
;
5039 who_perm_fini(who_perm_t
*who_perm
)
5041 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
5043 while (node
!= NULL
) {
5044 deleg_perm_node_t
*next_node
=
5045 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
5047 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
5052 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
5056 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
5058 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
5059 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
5061 bzero(fsperm
, sizeof (fs_perm_t
));
5063 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
5067 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
5071 fsperm
->fsp_set
= fspset
;
5072 fsperm
->fsp_name
= fsname
;
5076 fs_perm_fini(fs_perm_t
*fsperm
)
5078 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
5079 while (node
!= NULL
) {
5080 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
5082 who_perm_t
*who_perm
= &node
->who_perm
;
5083 who_perm_fini(who_perm
);
5084 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
5089 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
5090 while (node
!= NULL
) {
5091 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
5093 who_perm_t
*who_perm
= &node
->who_perm
;
5094 who_perm_fini(who_perm
);
5095 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
5100 uu_avl_destroy(fsperm
->fsp_sc_avl
);
5101 uu_avl_destroy(fsperm
->fsp_uge_avl
);
5105 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
5106 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
5108 uu_avl_index_t idx
= 0;
5110 deleg_perm_node_t
*found_node
= NULL
;
5111 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
5113 deleg_perm_init(deleg_perm
, who_type
, name
);
5115 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
5117 uu_avl_insert(avl
, node
, idx
);
5120 deleg_perm
= &node
->dpn_perm
;
5125 case ZFS_DELEG_LOCAL
:
5126 deleg_perm
->dp_local
= B_TRUE
;
5128 case ZFS_DELEG_DESCENDENT
:
5129 deleg_perm
->dp_descend
= B_TRUE
;
5134 assert(B_FALSE
); /* invalid locality */
5139 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
5141 nvpair_t
*nvp
= NULL
;
5142 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
5143 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
5144 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
5146 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5147 const char *name
= nvpair_name(nvp
);
5148 data_type_t type
= nvpair_type(nvp
);
5149 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
5150 deleg_perm_node_t
*node
=
5151 safe_malloc(sizeof (deleg_perm_node_t
));
5153 VERIFY(type
== DATA_TYPE_BOOLEAN
);
5155 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
5156 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
5163 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
5165 nvpair_t
*nvp
= NULL
;
5166 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
5168 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5169 nvlist_t
*nvl2
= NULL
;
5170 const char *name
= nvpair_name(nvp
);
5171 uu_avl_t
*avl
= NULL
;
5172 uu_avl_pool_t
*avl_pool
= NULL
;
5173 zfs_deleg_who_type_t perm_type
= name
[0];
5174 char perm_locality
= name
[1];
5175 const char *perm_name
= name
+ 3;
5176 who_perm_t
*who_perm
= NULL
;
5178 assert('$' == name
[2]);
5180 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
5183 switch (perm_type
) {
5184 case ZFS_DELEG_CREATE
:
5185 case ZFS_DELEG_CREATE_SETS
:
5186 case ZFS_DELEG_NAMED_SET
:
5187 case ZFS_DELEG_NAMED_SET_SETS
:
5188 avl_pool
= fspset
->fsps_named_set_avl_pool
;
5189 avl
= fsperm
->fsp_sc_avl
;
5191 case ZFS_DELEG_USER
:
5192 case ZFS_DELEG_USER_SETS
:
5193 case ZFS_DELEG_GROUP
:
5194 case ZFS_DELEG_GROUP_SETS
:
5195 case ZFS_DELEG_EVERYONE
:
5196 case ZFS_DELEG_EVERYONE_SETS
:
5197 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
5198 avl
= fsperm
->fsp_uge_avl
;
5202 assert(!"unhandled zfs_deleg_who_type_t");
5205 who_perm_node_t
*found_node
= NULL
;
5206 who_perm_node_t
*node
= safe_malloc(
5207 sizeof (who_perm_node_t
));
5208 who_perm
= &node
->who_perm
;
5209 uu_avl_index_t idx
= 0;
5211 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
5212 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
5214 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
5216 if (avl
== fsperm
->fsp_uge_avl
) {
5218 struct passwd
*p
= NULL
;
5219 struct group
*g
= NULL
;
5220 const char *nice_name
= NULL
;
5222 switch (perm_type
) {
5223 case ZFS_DELEG_USER_SETS
:
5224 case ZFS_DELEG_USER
:
5225 rid
= atoi(perm_name
);
5228 nice_name
= p
->pw_name
;
5230 case ZFS_DELEG_GROUP_SETS
:
5231 case ZFS_DELEG_GROUP
:
5232 rid
= atoi(perm_name
);
5235 nice_name
= g
->gr_name
;
5242 if (nice_name
!= NULL
) {
5244 node
->who_perm
.who_ug_name
,
5247 /* User or group unknown */
5249 node
->who_perm
.who_ug_name
,
5250 sizeof (node
->who_perm
.who_ug_name
),
5251 "(unknown: %d)", rid
);
5255 uu_avl_insert(avl
, node
, idx
);
5258 who_perm
= &node
->who_perm
;
5261 assert(who_perm
!= NULL
);
5262 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
5269 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
5271 nvpair_t
*nvp
= NULL
;
5272 uu_avl_index_t idx
= 0;
5274 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5275 nvlist_t
*nvl2
= NULL
;
5276 const char *fsname
= nvpair_name(nvp
);
5277 data_type_t type
= nvpair_type(nvp
);
5278 fs_perm_t
*fsperm
= NULL
;
5279 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
5283 fsperm
= &node
->fspn_fsperm
;
5285 VERIFY(DATA_TYPE_NVLIST
== type
);
5287 uu_list_node_init(node
, &node
->fspn_list_node
,
5288 fspset
->fsps_list_pool
);
5290 idx
= uu_list_numnodes(fspset
->fsps_list
);
5291 fs_perm_init(fsperm
, fspset
, fsname
);
5293 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
5296 (void) parse_fs_perm(fsperm
, nvl2
);
5298 uu_list_insert(fspset
->fsps_list
, node
, idx
);
5304 static inline const char *
5305 deleg_perm_comment(zfs_deleg_note_t note
)
5307 const char *str
= "";
5312 case ZFS_DELEG_NOTE_ALLOW
:
5313 str
= gettext("Must also have the permission that is being"
5314 "\n\t\t\t\tallowed");
5316 case ZFS_DELEG_NOTE_CLONE
:
5317 str
= gettext("Must also have the 'create' ability and 'mount'"
5318 "\n\t\t\t\tability in the origin file system");
5320 case ZFS_DELEG_NOTE_CREATE
:
5321 str
= gettext("Must also have the 'mount' ability");
5323 case ZFS_DELEG_NOTE_DESTROY
:
5324 str
= gettext("Must also have the 'mount' ability");
5326 case ZFS_DELEG_NOTE_DIFF
:
5327 str
= gettext("Allows lookup of paths within a dataset;"
5328 "\n\t\t\t\tgiven an object number. Ordinary users need this"
5329 "\n\t\t\t\tin order to use zfs diff");
5331 case ZFS_DELEG_NOTE_HOLD
:
5332 str
= gettext("Allows adding a user hold to a snapshot");
5334 case ZFS_DELEG_NOTE_MOUNT
:
5335 str
= gettext("Allows mount/umount of ZFS datasets");
5337 case ZFS_DELEG_NOTE_PROMOTE
:
5338 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
5339 " 'promote' ability in the origin file system");
5341 case ZFS_DELEG_NOTE_RECEIVE
:
5342 str
= gettext("Must also have the 'mount' and 'create'"
5345 case ZFS_DELEG_NOTE_RELEASE
:
5346 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
5347 "might destroy the snapshot");
5349 case ZFS_DELEG_NOTE_RENAME
:
5350 str
= gettext("Must also have the 'mount' and 'create'"
5351 "\n\t\t\t\tability in the new parent");
5353 case ZFS_DELEG_NOTE_ROLLBACK
:
5356 case ZFS_DELEG_NOTE_SEND
:
5359 case ZFS_DELEG_NOTE_SHARE
:
5360 str
= gettext("Allows sharing file systems over NFS or SMB"
5361 "\n\t\t\t\tprotocols");
5363 case ZFS_DELEG_NOTE_SNAPSHOT
:
5366 case ZFS_DELEG_NOTE_LOAD_KEY
:
5367 str
= gettext("Allows loading or unloading an encryption key");
5369 case ZFS_DELEG_NOTE_CHANGE_KEY
:
5370 str
= gettext("Allows changing or adding an encryption key");
5373 * case ZFS_DELEG_NOTE_VSCAN:
5374 * str = gettext("");
5378 case ZFS_DELEG_NOTE_GROUPQUOTA
:
5379 str
= gettext("Allows accessing any groupquota@... property");
5381 case ZFS_DELEG_NOTE_GROUPUSED
:
5382 str
= gettext("Allows reading any groupused@... property");
5384 case ZFS_DELEG_NOTE_USERPROP
:
5385 str
= gettext("Allows changing any user property");
5387 case ZFS_DELEG_NOTE_USERQUOTA
:
5388 str
= gettext("Allows accessing any userquota@... property");
5390 case ZFS_DELEG_NOTE_USERUSED
:
5391 str
= gettext("Allows reading any userused@... property");
5393 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
5394 str
= gettext("Allows accessing any userobjquota@... property");
5396 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
5397 str
= gettext("Allows accessing any \n\t\t\t\t"
5398 "groupobjquota@... property");
5400 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
5401 str
= gettext("Allows reading any groupobjused@... property");
5403 case ZFS_DELEG_NOTE_USEROBJUSED
:
5404 str
= gettext("Allows reading any userobjused@... property");
5406 case ZFS_DELEG_NOTE_PROJECTQUOTA
:
5407 str
= gettext("Allows accessing any projectquota@... property");
5409 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA
:
5410 str
= gettext("Allows accessing any \n\t\t\t\t"
5411 "projectobjquota@... property");
5413 case ZFS_DELEG_NOTE_PROJECTUSED
:
5414 str
= gettext("Allows reading any projectused@... property");
5416 case ZFS_DELEG_NOTE_PROJECTOBJUSED
:
5417 str
= gettext("Allows accessing any \n\t\t\t\t"
5418 "projectobjused@... property");
5436 boolean_t recursive
; /* unallow only */
5437 boolean_t prt_usage
;
5439 boolean_t prt_perms
;
5442 const char *dataset
;
5446 prop_cmp(const void *a
, const void *b
)
5448 const char *str1
= *(const char **)a
;
5449 const char *str2
= *(const char **)b
;
5450 return (strcmp(str1
, str2
));
5454 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
5456 const char *opt_desc
[] = {
5457 "-h", gettext("show this help message and exit"),
5458 "-l", gettext("set permission locally"),
5459 "-d", gettext("set permission for descents"),
5460 "-u", gettext("set permission for user"),
5461 "-g", gettext("set permission for group"),
5462 "-e", gettext("set permission for everyone"),
5463 "-c", gettext("set create time permission"),
5464 "-s", gettext("define permission set"),
5466 "-r", gettext("remove permissions recursively"),
5468 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
5469 size_t allow_size
= unallow_size
- 2;
5470 const char *props
[ZFS_NUM_PROPS
];
5473 FILE *fp
= requested
? stdout
: stderr
;
5474 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
5475 const char *fmt
= gettext("%-16s %-14s\t%s\n");
5477 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
5479 (void) fprintf(fp
, gettext("Options:\n"));
5480 for (i
= 0; i
< (un
? unallow_size
: allow_size
); i
+= 2) {
5481 const char *opt
= opt_desc
[i
];
5482 const char *optdsc
= opt_desc
[i
+ 1];
5483 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
5486 (void) fprintf(fp
, gettext("\nThe following permissions are "
5488 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
5490 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
5491 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
5492 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
5493 const char *perm_type
= deleg_perm_type(perm_note
);
5494 const char *perm_comment
= deleg_perm_comment(perm_note
);
5495 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
5498 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
5499 zprop_desc_t
*pd
= &pdtbl
[i
];
5500 if (pd
->pd_visible
!= B_TRUE
)
5503 if (pd
->pd_attr
== PROP_READONLY
)
5506 props
[count
++] = pd
->pd_name
;
5508 props
[count
] = NULL
;
5510 qsort(props
, count
, sizeof (char *), prop_cmp
);
5512 for (i
= 0; i
< count
; i
++)
5513 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
5516 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
5518 exit(requested
? 0 : 2);
5521 static inline const char *
5522 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
5525 if (un
&& argc
== expected_argc
- 1)
5527 else if (argc
== expected_argc
)
5528 *permsp
= argv
[argc
- 2];
5530 allow_usage(un
, B_FALSE
,
5531 gettext("wrong number of parameters\n"));
5533 return (argv
[argc
- 1]);
5537 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
5539 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
5540 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
5541 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
5542 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
5545 allow_usage(un
, B_FALSE
,
5546 gettext("-u, -g, and -e are mutually exclusive\n"));
5548 if (opts
->prt_usage
) {
5549 if (argc
== 0 && all_sum
== 0)
5550 allow_usage(un
, B_TRUE
, NULL
);
5557 allow_usage(un
, B_FALSE
,
5558 gettext("invalid options combined with -s\n"));
5560 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
5561 if (argv
[0][0] != '@')
5562 allow_usage(un
, B_FALSE
,
5563 gettext("invalid set name: missing '@' prefix\n"));
5564 opts
->who
= argv
[0];
5565 } else if (opts
->create
) {
5566 if (ldcsuge_sum
> 1)
5567 allow_usage(un
, B_FALSE
,
5568 gettext("invalid options combined with -c\n"));
5569 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5570 } else if (opts
->everyone
) {
5572 allow_usage(un
, B_FALSE
,
5573 gettext("invalid options combined with -e\n"));
5574 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5575 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
5577 opts
->everyone
= B_TRUE
;
5580 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5581 } else if (argc
== 1 && !un
) {
5582 opts
->prt_perms
= B_TRUE
;
5583 opts
->dataset
= argv
[argc
-1];
5585 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
5586 opts
->who
= argv
[0];
5589 if (!opts
->local
&& !opts
->descend
) {
5590 opts
->local
= B_TRUE
;
5591 opts
->descend
= B_TRUE
;
5596 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
5597 const char *who
, char *perms
, nvlist_t
*top_nvl
)
5600 char ld
[2] = { '\0', '\0' };
5601 char who_buf
[MAXNAMELEN
+ 32];
5602 char base_type
= '\0';
5603 char set_type
= '\0';
5604 nvlist_t
*base_nvl
= NULL
;
5605 nvlist_t
*set_nvl
= NULL
;
5608 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
5610 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
5614 case ZFS_DELEG_NAMED_SET_SETS
:
5615 case ZFS_DELEG_NAMED_SET
:
5616 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
5617 base_type
= ZFS_DELEG_NAMED_SET
;
5618 ld
[0] = ZFS_DELEG_NA
;
5620 case ZFS_DELEG_CREATE_SETS
:
5621 case ZFS_DELEG_CREATE
:
5622 set_type
= ZFS_DELEG_CREATE_SETS
;
5623 base_type
= ZFS_DELEG_CREATE
;
5624 ld
[0] = ZFS_DELEG_NA
;
5626 case ZFS_DELEG_USER_SETS
:
5627 case ZFS_DELEG_USER
:
5628 set_type
= ZFS_DELEG_USER_SETS
;
5629 base_type
= ZFS_DELEG_USER
;
5631 ld
[0] = ZFS_DELEG_LOCAL
;
5633 ld
[1] = ZFS_DELEG_DESCENDENT
;
5635 case ZFS_DELEG_GROUP_SETS
:
5636 case ZFS_DELEG_GROUP
:
5637 set_type
= ZFS_DELEG_GROUP_SETS
;
5638 base_type
= ZFS_DELEG_GROUP
;
5640 ld
[0] = ZFS_DELEG_LOCAL
;
5642 ld
[1] = ZFS_DELEG_DESCENDENT
;
5644 case ZFS_DELEG_EVERYONE_SETS
:
5645 case ZFS_DELEG_EVERYONE
:
5646 set_type
= ZFS_DELEG_EVERYONE_SETS
;
5647 base_type
= ZFS_DELEG_EVERYONE
;
5649 ld
[0] = ZFS_DELEG_LOCAL
;
5651 ld
[1] = ZFS_DELEG_DESCENDENT
;
5655 assert(set_type
!= '\0' && base_type
!= '\0');
5658 if (perms
!= NULL
) {
5660 char *end
= curr
+ strlen(perms
);
5662 while (curr
< end
) {
5663 char *delim
= strchr(curr
, ',');
5674 (void) nvlist_add_boolean(nvl
, curr
);
5680 for (i
= 0; i
< 2; i
++) {
5681 char locality
= ld
[i
];
5685 if (!nvlist_empty(base_nvl
)) {
5687 (void) snprintf(who_buf
,
5688 sizeof (who_buf
), "%c%c$%s",
5689 base_type
, locality
, who
);
5691 (void) snprintf(who_buf
,
5692 sizeof (who_buf
), "%c%c$",
5693 base_type
, locality
);
5695 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5700 if (!nvlist_empty(set_nvl
)) {
5702 (void) snprintf(who_buf
,
5703 sizeof (who_buf
), "%c%c$%s",
5704 set_type
, locality
, who
);
5706 (void) snprintf(who_buf
,
5707 sizeof (who_buf
), "%c%c$",
5708 set_type
, locality
);
5710 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5715 for (i
= 0; i
< 2; i
++) {
5716 char locality
= ld
[i
];
5721 (void) snprintf(who_buf
, sizeof (who_buf
),
5722 "%c%c$%s", base_type
, locality
, who
);
5724 (void) snprintf(who_buf
, sizeof (who_buf
),
5725 "%c%c$", base_type
, locality
);
5726 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5729 (void) snprintf(who_buf
, sizeof (who_buf
),
5730 "%c%c$%s", set_type
, locality
, who
);
5732 (void) snprintf(who_buf
, sizeof (who_buf
),
5733 "%c%c$", set_type
, locality
);
5734 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5740 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
5742 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
5746 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
5747 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
5748 } else if (opts
->create
) {
5749 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
5750 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5751 } else if (opts
->everyone
) {
5752 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
5753 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5755 char *curr
= opts
->who
;
5756 char *end
= curr
+ strlen(curr
);
5758 while (curr
< end
) {
5760 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
5762 char *delim
= strchr(curr
, ',');
5765 struct passwd
*p
= NULL
;
5766 struct group
*g
= NULL
;
5774 rid
= (uid_t
)strtol(curr
, &endch
, 0);
5776 who_type
= ZFS_DELEG_USER
;
5784 else if (*endch
!= '\0') {
5785 (void) snprintf(errbuf
, 256, gettext(
5786 "invalid user %s\n"), curr
);
5787 allow_usage(un
, B_TRUE
, errbuf
);
5789 } else if (opts
->group
) {
5790 who_type
= ZFS_DELEG_GROUP
;
5798 else if (*endch
!= '\0') {
5799 (void) snprintf(errbuf
, 256, gettext(
5800 "invalid group %s\n"), curr
);
5801 allow_usage(un
, B_TRUE
, errbuf
);
5804 if (*endch
!= '\0') {
5811 if (*endch
!= '\0') {
5819 who_type
= ZFS_DELEG_USER
;
5821 } else if (g
!= NULL
) {
5822 who_type
= ZFS_DELEG_GROUP
;
5825 (void) snprintf(errbuf
, 256, gettext(
5826 "invalid user/group %s\n"), curr
);
5827 allow_usage(un
, B_TRUE
, errbuf
);
5831 (void) sprintf(id
, "%u", rid
);
5834 store_allow_perm(who_type
, opts
->local
,
5835 opts
->descend
, who
, opts
->perms
, *nvlp
);
5844 print_set_creat_perms(uu_avl_t
*who_avl
)
5846 const char *sc_title
[] = {
5847 gettext("Permission sets:\n"),
5848 gettext("Create time permissions:\n"),
5851 who_perm_node_t
*who_node
= NULL
;
5852 int prev_weight
= -1;
5854 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
5855 who_node
= uu_avl_next(who_avl
, who_node
)) {
5856 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5857 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5858 const char *who_name
= who_node
->who_perm
.who_name
;
5859 int weight
= who_type2weight(who_type
);
5860 boolean_t first
= B_TRUE
;
5861 deleg_perm_node_t
*deleg_node
;
5863 if (prev_weight
!= weight
) {
5864 (void) printf("%s", sc_title
[weight
]);
5865 prev_weight
= weight
;
5868 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
5869 (void) printf("\t");
5871 (void) printf("\t%s ", who_name
);
5873 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
5874 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5877 deleg_node
->dpn_perm
.dp_name
);
5880 (void) printf(",%s",
5881 deleg_node
->dpn_perm
.dp_name
);
5884 (void) printf("\n");
5889 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
5892 who_perm_node_t
*who_node
= NULL
;
5893 boolean_t prt_title
= B_TRUE
;
5894 uu_avl_walk_t
*walk
;
5896 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
5899 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
5900 const char *who_name
= who_node
->who_perm
.who_name
;
5901 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
5902 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
5903 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
5905 deleg_perm_node_t
*deleg_node
;
5906 boolean_t prt_who
= B_TRUE
;
5908 for (deleg_node
= uu_avl_first(avl
);
5910 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
5911 if (local
!= deleg_node
->dpn_perm
.dp_local
||
5912 descend
!= deleg_node
->dpn_perm
.dp_descend
)
5916 const char *who
= NULL
;
5918 prt_title
= B_FALSE
;
5919 (void) printf("%s", title
);
5923 case ZFS_DELEG_USER_SETS
:
5924 case ZFS_DELEG_USER
:
5925 who
= gettext("user");
5927 who_name
= nice_who_name
;
5929 case ZFS_DELEG_GROUP_SETS
:
5930 case ZFS_DELEG_GROUP
:
5931 who
= gettext("group");
5933 who_name
= nice_who_name
;
5935 case ZFS_DELEG_EVERYONE_SETS
:
5936 case ZFS_DELEG_EVERYONE
:
5937 who
= gettext("everyone");
5942 assert(who
!= NULL
);
5946 if (who_name
== NULL
)
5947 (void) printf("\t%s", who
);
5949 (void) printf("\t%s %s", who
, who_name
);
5952 (void) printf("%c%s", delim
,
5953 deleg_node
->dpn_perm
.dp_name
);
5958 (void) printf("\n");
5961 uu_avl_walk_end(walk
);
5965 print_fs_perms(fs_perm_set_t
*fspset
)
5967 fs_perm_node_t
*node
= NULL
;
5968 char buf
[MAXNAMELEN
+ 32];
5969 const char *dsname
= buf
;
5971 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
5972 node
= uu_list_next(fspset
->fsps_list
, node
)) {
5973 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
5974 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
5977 (void) snprintf(buf
, sizeof (buf
),
5978 gettext("---- Permissions on %s "),
5979 node
->fspn_fsperm
.fsp_name
);
5980 (void) printf("%s", dsname
);
5981 left
= 70 - strlen(buf
);
5984 (void) printf("\n");
5986 print_set_creat_perms(sc_avl
);
5987 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
5988 gettext("Local permissions:\n"));
5989 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
5990 gettext("Descendent permissions:\n"));
5991 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
5992 gettext("Local+Descendent permissions:\n"));
5996 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
5998 struct deleg_perms
{
6004 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
6006 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
6007 zfs_type_t zfs_type
= zfs_get_type(zhp
);
6009 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
6012 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
6016 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
6019 nvlist_t
*perm_nvl
= NULL
;
6020 nvlist_t
*update_perm_nvl
= NULL
;
6023 struct allow_opts opts
= { 0 };
6025 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
6028 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
6031 opts
.local
= B_TRUE
;
6034 opts
.descend
= B_TRUE
;
6040 opts
.group
= B_TRUE
;
6043 opts
.everyone
= B_TRUE
;
6049 opts
.create
= B_TRUE
;
6052 opts
.recursive
= B_TRUE
;
6055 (void) fprintf(stderr
, gettext("missing argument for "
6056 "'%c' option\n"), optopt
);
6060 opts
.prt_usage
= B_TRUE
;
6063 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6072 /* check arguments */
6073 parse_allow_args(argc
, argv
, un
, &opts
);
6075 /* try to open the dataset */
6076 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
6077 ZFS_TYPE_VOLUME
)) == NULL
) {
6078 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
6083 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
6086 fs_perm_set_init(&fs_perm_set
);
6087 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
6088 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
6093 print_fs_perms(&fs_perm_set
);
6095 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
6096 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
6099 if (un
&& opts
.recursive
) {
6100 struct deleg_perms data
= { un
, update_perm_nvl
};
6101 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
6110 nvlist_free(perm_nvl
);
6111 nvlist_free(update_perm_nvl
);
6113 fs_perm_set_fini(&fs_perm_set
);
6121 zfs_do_allow(int argc
, char **argv
)
6123 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
6127 zfs_do_unallow(int argc
, char **argv
)
6129 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
6133 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
6138 boolean_t recursive
= B_FALSE
;
6139 const char *opts
= holding
? "rt" : "r";
6143 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
6149 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6158 /* check number of arguments */
6166 if (holding
&& tag
[0] == '.') {
6167 /* tags starting with '.' are reserved for libzfs */
6168 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
6172 for (i
= 0; i
< argc
; ++i
) {
6174 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
6176 char *path
= argv
[i
];
6178 delim
= strchr(path
, '@');
6179 if (delim
== NULL
) {
6180 (void) fprintf(stderr
,
6181 gettext("'%s' is not a snapshot\n"), path
);
6185 (void) strncpy(parent
, path
, delim
- path
);
6186 parent
[delim
- path
] = '\0';
6188 zhp
= zfs_open(g_zfs
, parent
,
6189 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
6195 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
6198 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
6204 return (errors
!= 0);
6208 * zfs hold [-r] [-t] <tag> <snap> ...
6210 * -r Recursively hold
6212 * Apply a user-hold with the given tag to the list of snapshots.
6215 zfs_do_hold(int argc
, char **argv
)
6217 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
6221 * zfs release [-r] <tag> <snap> ...
6223 * -r Recursively release
6225 * Release a user-hold with the given tag from the list of snapshots.
6228 zfs_do_release(int argc
, char **argv
)
6230 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
6233 typedef struct holds_cbdata
{
6234 boolean_t cb_recursive
;
6235 const char *cb_snapname
;
6237 size_t cb_max_namelen
;
6238 size_t cb_max_taglen
;
6241 #define STRFTIME_FMT_STR "%a %b %e %H:%M %Y"
6242 #define DATETIME_BUF_LEN (32)
6247 print_holds(boolean_t scripted
, int nwidth
, int tagwidth
, nvlist_t
*nvl
)
6250 nvpair_t
*nvp
= NULL
;
6251 char *hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
6255 for (i
= 0; i
< 3; i
++) {
6256 col
= gettext(hdr_cols
[i
]);
6258 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
6261 (void) printf("%s\n", col
);
6265 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
6266 char *zname
= nvpair_name(nvp
);
6268 nvpair_t
*nvp2
= NULL
;
6269 (void) nvpair_value_nvlist(nvp
, &nvl2
);
6270 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
6271 char tsbuf
[DATETIME_BUF_LEN
];
6272 char *tagname
= nvpair_name(nvp2
);
6277 (void) nvpair_value_uint64(nvp2
, &val
);
6279 (void) localtime_r(&time
, &t
);
6280 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
6281 gettext(STRFTIME_FMT_STR
), &t
);
6284 (void) printf("%s\t%s\t%s\n", zname
,
6287 (void) printf("%-*s %-*s %s\n", nwidth
,
6288 zname
, tagwidth
, tagname
, tsbuf
);
6295 * Generic callback function to list a dataset or snapshot.
6298 holds_callback(zfs_handle_t
*zhp
, void *data
)
6300 holds_cbdata_t
*cbp
= data
;
6301 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
6302 nvlist_t
*nvl
= NULL
;
6303 nvpair_t
*nvp
= NULL
;
6304 const char *zname
= zfs_get_name(zhp
);
6305 size_t znamelen
= strlen(zname
);
6307 if (cbp
->cb_recursive
) {
6308 const char *snapname
;
6309 char *delim
= strchr(zname
, '@');
6313 snapname
= delim
+ 1;
6314 if (strcmp(cbp
->cb_snapname
, snapname
))
6318 if (zfs_get_holds(zhp
, &nvl
) != 0)
6321 if (znamelen
> cbp
->cb_max_namelen
)
6322 cbp
->cb_max_namelen
= znamelen
;
6324 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
6325 const char *tag
= nvpair_name(nvp
);
6326 size_t taglen
= strlen(tag
);
6327 if (taglen
> cbp
->cb_max_taglen
)
6328 cbp
->cb_max_taglen
= taglen
;
6331 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
6335 * zfs holds [-rH] <snap> ...
6337 * -r Lists holds that are set on the named snapshots recursively.
6338 * -H Scripted mode; elide headers and separate columns by tabs.
6341 zfs_do_holds(int argc
, char **argv
)
6346 boolean_t scripted
= B_FALSE
;
6347 boolean_t recursive
= B_FALSE
;
6348 const char *opts
= "rH";
6351 int types
= ZFS_TYPE_SNAPSHOT
;
6352 holds_cbdata_t cb
= { 0 };
6359 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
6368 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6375 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
6376 flags
|= ZFS_ITER_RECURSE
;
6382 /* check number of arguments */
6386 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
6389 for (i
= 0; i
< argc
; ++i
) {
6390 char *snapshot
= argv
[i
];
6392 const char *snapname
;
6394 delim
= strchr(snapshot
, '@');
6395 if (delim
== NULL
) {
6396 (void) fprintf(stderr
,
6397 gettext("'%s' is not a snapshot\n"), snapshot
);
6401 snapname
= delim
+ 1;
6403 snapshot
[delim
- snapshot
] = '\0';
6405 cb
.cb_recursive
= recursive
;
6406 cb
.cb_snapname
= snapname
;
6410 * 1. collect holds data, set format options
6412 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
, NULL
, limit
,
6413 holds_callback
, &cb
);
6419 * 2. print holds data
6421 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
6423 if (nvlist_empty(nvl
))
6424 (void) fprintf(stderr
, gettext("no datasets available\n"));
6428 return (0 != errors
);
6431 #define CHECK_SPINNER 30
6432 #define SPINNER_TIME 3 /* seconds */
6433 #define MOUNT_TIME 1 /* seconds */
6435 typedef struct get_all_state
{
6436 boolean_t ga_verbose
;
6437 get_all_cb_t
*ga_cbp
;
6441 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
6443 static char *spin
[] = { "-", "\\", "|", "/" };
6444 static int spinval
= 0;
6445 static int spincheck
= 0;
6446 static time_t last_spin_time
= (time_t)0;
6447 get_all_state_t
*state
= data
;
6448 zfs_type_t type
= zfs_get_type(zhp
);
6450 if (state
->ga_verbose
) {
6451 if (--spincheck
< 0) {
6452 time_t now
= time(NULL
);
6453 if (last_spin_time
+ SPINNER_TIME
< now
) {
6454 update_progress(spin
[spinval
++ % 4]);
6455 last_spin_time
= now
;
6457 spincheck
= CHECK_SPINNER
;
6462 * Iterate over any nested datasets.
6464 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
6470 * Skip any datasets whose type does not match.
6472 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
6476 libzfs_add_handle(state
->ga_cbp
, zhp
);
6477 assert(state
->ga_cbp
->cb_used
<= state
->ga_cbp
->cb_alloc
);
6483 get_all_datasets(get_all_cb_t
*cbp
, boolean_t verbose
)
6485 get_all_state_t state
= {
6486 .ga_verbose
= verbose
,
6491 set_progress_header(gettext("Reading ZFS config"));
6492 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &state
);
6495 finish_progress(gettext("done."));
6499 * Generic callback for sharing or mounting filesystems. Because the code is so
6500 * similar, we have a common function with an extra parameter to determine which
6501 * mode we are using.
6503 typedef enum { OP_SHARE
, OP_MOUNT
} share_mount_op_t
;
6505 typedef struct share_mount_state
{
6506 share_mount_op_t sm_op
;
6507 boolean_t sm_verbose
;
6510 char *sm_proto
; /* only valid for OP_SHARE */
6511 pthread_mutex_t sm_lock
; /* protects the remaining fields */
6512 uint_t sm_total
; /* number of filesystems to process */
6513 uint_t sm_done
; /* number of filesystems processed */
6514 int sm_status
; /* -1 if any of the share/mount operations failed */
6515 } share_mount_state_t
;
6518 * Share or mount a dataset.
6521 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
6522 boolean_t
explicit, const char *options
)
6524 char mountpoint
[ZFS_MAXPROPLEN
];
6525 char shareopts
[ZFS_MAXPROPLEN
];
6526 char smbshareopts
[ZFS_MAXPROPLEN
];
6527 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
6529 uint64_t zoned
, canmount
;
6530 boolean_t shared_nfs
, shared_smb
;
6532 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
6535 * Check to make sure we can mount/share this dataset. If we
6536 * are in the global zone and the filesystem is exported to a
6537 * local zone, or if we are in a local zone and the
6538 * filesystem is not exported, then it is an error.
6540 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
6542 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
6546 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6547 "dataset is exported to a local zone\n"), cmdname
,
6551 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
6555 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6556 "permission denied\n"), cmdname
,
6562 * Ignore any filesystems which don't apply to us. This
6563 * includes those with a legacy mountpoint, or those with
6564 * legacy share options.
6566 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
6567 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
6568 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
6569 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
6570 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
6571 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
6573 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
6574 strcmp(smbshareopts
, "off") == 0) {
6578 (void) fprintf(stderr
, gettext("cannot share '%s': "
6579 "legacy share\n"), zfs_get_name(zhp
));
6580 (void) fprintf(stderr
, gettext("use share(1M) to "
6581 "share this filesystem, or set "
6582 "sharenfs property on\n"));
6587 * We cannot share or mount legacy filesystems. If the
6588 * shareopts is non-legacy but the mountpoint is legacy, we
6589 * treat it as a legacy share.
6591 if (strcmp(mountpoint
, "legacy") == 0) {
6595 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6596 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
6597 (void) fprintf(stderr
, gettext("use %s(1M) to "
6598 "%s this filesystem\n"), cmdname
, cmdname
);
6602 if (strcmp(mountpoint
, "none") == 0) {
6606 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
6607 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
6612 * canmount explicit outcome
6613 * on no pass through
6614 * on yes pass through
6616 * off yes display error, return 1
6617 * noauto no return 0
6618 * noauto yes pass through
6620 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
6621 if (canmount
== ZFS_CANMOUNT_OFF
) {
6625 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6626 "'canmount' property is set to 'off'\n"), cmdname
,
6629 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
6631 * When performing a 'zfs mount -a', we skip any mounts for
6632 * datasets that have 'noauto' set. Sharing a dataset with
6633 * 'noauto' set is only allowed if it's mounted.
6637 if (op
== OP_SHARE
&& !zfs_is_mounted(zhp
, NULL
)) {
6638 /* also purge it from existing exports */
6639 zfs_unshareall_bypath(zhp
, mountpoint
);
6645 * If this filesystem is encrypted and does not have
6646 * a loaded key, we can not mount it.
6648 if ((flags
& MS_CRYPT
) == 0 &&
6649 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) != ZIO_CRYPT_OFF
&&
6650 zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
) ==
6651 ZFS_KEYSTATUS_UNAVAILABLE
) {
6655 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6656 "encryption key not loaded\n"), cmdname
, zfs_get_name(zhp
));
6661 * If this filesystem is inconsistent and has a receive resume
6662 * token, we can not mount it.
6664 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
6665 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
6666 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
6670 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6671 "Contains partially-completed state from "
6672 "\"zfs receive -s\", which can be resumed with "
6673 "\"zfs send -t\"\n"),
6674 cmdname
, zfs_get_name(zhp
));
6678 if (zfs_prop_get_int(zhp
, ZFS_PROP_REDACTED
) && !(flags
& MS_FORCE
)) {
6682 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6683 "Dataset is not complete, was created by receiving "
6684 "a redacted zfs send stream.\n"), cmdname
,
6690 * At this point, we have verified that the mountpoint and/or
6691 * shareopts are appropriate for auto management. If the
6692 * filesystem is already mounted or shared, return (failing
6693 * for explicit requests); otherwise mount or share the
6699 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
6700 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
6702 if ((shared_nfs
&& shared_smb
) ||
6703 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
6704 strcmp(smbshareopts
, "off") == 0) ||
6705 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
6706 strcmp(shareopts
, "off") == 0)) {
6710 (void) fprintf(stderr
, gettext("cannot share "
6711 "'%s': filesystem already shared\n"),
6716 if (!zfs_is_mounted(zhp
, NULL
) &&
6717 zfs_mount(zhp
, NULL
, flags
) != 0)
6720 if (protocol
== NULL
) {
6721 if (zfs_shareall(zhp
) != 0)
6723 } else if (strcmp(protocol
, "nfs") == 0) {
6724 if (zfs_share_nfs(zhp
))
6726 } else if (strcmp(protocol
, "smb") == 0) {
6727 if (zfs_share_smb(zhp
))
6730 (void) fprintf(stderr
, gettext("cannot share "
6731 "'%s': invalid share type '%s' "
6733 zfs_get_name(zhp
), protocol
);
6740 if (options
== NULL
)
6741 mnt
.mnt_mntopts
= "";
6743 mnt
.mnt_mntopts
= (char *)options
;
6745 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
6746 zfs_is_mounted(zhp
, NULL
)) {
6750 (void) fprintf(stderr
, gettext("cannot mount "
6751 "'%s': filesystem already mounted\n"),
6756 if (zfs_mount(zhp
, options
, flags
) != 0)
6765 * Reports progress in the form "(current/total)". Not thread-safe.
6768 report_mount_progress(int current
, int total
)
6770 static time_t last_progress_time
= 0;
6771 time_t now
= time(NULL
);
6774 /* report 1..n instead of 0..n-1 */
6777 /* display header if we're here for the first time */
6779 set_progress_header(gettext("Mounting ZFS filesystems"));
6780 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
6781 /* too soon to report again */
6785 last_progress_time
= now
;
6787 (void) sprintf(info
, "(%d/%d)", current
, total
);
6789 if (current
== total
)
6790 finish_progress(info
);
6792 update_progress(info
);
6796 * zfs_foreach_mountpoint() callback that mounts or shares one filesystem and
6797 * updates the progress meter.
6800 share_mount_one_cb(zfs_handle_t
*zhp
, void *arg
)
6802 share_mount_state_t
*sms
= arg
;
6805 ret
= share_mount_one(zhp
, sms
->sm_op
, sms
->sm_flags
, sms
->sm_proto
,
6806 B_FALSE
, sms
->sm_options
);
6808 pthread_mutex_lock(&sms
->sm_lock
);
6810 sms
->sm_status
= ret
;
6812 if (sms
->sm_verbose
)
6813 report_mount_progress(sms
->sm_done
, sms
->sm_total
);
6814 pthread_mutex_unlock(&sms
->sm_lock
);
6819 append_options(char *mntopts
, char *newopts
)
6821 int len
= strlen(mntopts
);
6823 /* original length plus new string to append plus 1 for the comma */
6824 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
6825 (void) fprintf(stderr
, gettext("the opts argument for "
6826 "'%s' option is too long (more than %d chars)\n"),
6827 "-o", MNT_LINE_MAX
);
6832 mntopts
[len
++] = ',';
6834 (void) strcpy(&mntopts
[len
], newopts
);
6838 share_mount(int op
, int argc
, char **argv
)
6841 boolean_t verbose
= B_FALSE
;
6843 char *options
= NULL
;
6847 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":alvo:Of" : "al"))
6860 if (*optarg
== '\0') {
6861 (void) fprintf(stderr
, gettext("empty mount "
6862 "options (-o) specified\n"));
6866 if (options
== NULL
)
6867 options
= safe_malloc(MNT_LINE_MAX
+ 1);
6869 /* option validation is done later */
6870 append_options(options
, optarg
);
6873 flags
|= MS_OVERLAY
;
6879 (void) fprintf(stderr
, gettext("missing argument for "
6880 "'%c' option\n"), optopt
);
6884 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6893 /* check number of arguments */
6895 char *protocol
= NULL
;
6897 if (op
== OP_SHARE
&& argc
> 0) {
6898 if (strcmp(argv
[0], "nfs") != 0 &&
6899 strcmp(argv
[0], "smb") != 0) {
6900 (void) fprintf(stderr
, gettext("share type "
6901 "must be 'nfs' or 'smb'\n"));
6910 (void) fprintf(stderr
, gettext("too many arguments\n"));
6914 start_progress_timer();
6915 get_all_cb_t cb
= { 0 };
6916 get_all_datasets(&cb
, verbose
);
6918 if (cb
.cb_used
== 0) {
6919 if (options
!= NULL
)
6924 share_mount_state_t share_mount_state
= { 0 };
6925 share_mount_state
.sm_op
= op
;
6926 share_mount_state
.sm_verbose
= verbose
;
6927 share_mount_state
.sm_flags
= flags
;
6928 share_mount_state
.sm_options
= options
;
6929 share_mount_state
.sm_proto
= protocol
;
6930 share_mount_state
.sm_total
= cb
.cb_used
;
6931 pthread_mutex_init(&share_mount_state
.sm_lock
, NULL
);
6934 * libshare isn't mt-safe, so only do the operation in parallel
6935 * if we're mounting. Additionally, the key-loading option must
6936 * be serialized so that we can prompt the user for their keys
6937 * in a consistent manner.
6939 zfs_foreach_mountpoint(g_zfs
, cb
.cb_handles
, cb
.cb_used
,
6940 share_mount_one_cb
, &share_mount_state
,
6941 op
== OP_MOUNT
&& !(flags
& MS_CRYPT
));
6942 zfs_commit_all_shares();
6944 ret
= share_mount_state
.sm_status
;
6946 for (int i
= 0; i
< cb
.cb_used
; i
++)
6947 zfs_close(cb
.cb_handles
[i
]);
6948 free(cb
.cb_handles
);
6949 } else if (argc
== 0) {
6950 struct mnttab entry
;
6952 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
6953 (void) fprintf(stderr
, gettext("missing filesystem "
6954 "argument (specify -a for all)\n"));
6959 * When mount is given no arguments, go through
6960 * /proc/self/mounts and display any active ZFS mounts.
6961 * We hide any snapshots, since they are controlled
6965 /* Reopen MNTTAB to prevent reading stale data from open file */
6966 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
) {
6967 if (options
!= NULL
)
6972 while (getmntent(mnttab_file
, &entry
) == 0) {
6973 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
6974 strchr(entry
.mnt_special
, '@') != NULL
)
6977 (void) printf("%-30s %s\n", entry
.mnt_special
,
6985 (void) fprintf(stderr
,
6986 gettext("too many arguments\n"));
6990 if ((zhp
= zfs_open(g_zfs
, argv
[0],
6991 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
6994 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
6996 zfs_commit_all_shares();
7001 if (options
!= NULL
)
7008 * zfs mount -a [nfs]
7009 * zfs mount filesystem
7011 * Mount all filesystems, or mount the given filesystem.
7014 zfs_do_mount(int argc
, char **argv
)
7016 return (share_mount(OP_MOUNT
, argc
, argv
));
7020 * zfs share -a [nfs | smb]
7021 * zfs share filesystem
7023 * Share all filesystems, or share the given filesystem.
7026 zfs_do_share(int argc
, char **argv
)
7028 return (share_mount(OP_SHARE
, argc
, argv
));
7031 typedef struct unshare_unmount_node
{
7032 zfs_handle_t
*un_zhp
;
7034 uu_avl_node_t un_avlnode
;
7035 } unshare_unmount_node_t
;
7039 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
7041 const unshare_unmount_node_t
*l
= larg
;
7042 const unshare_unmount_node_t
*r
= rarg
;
7044 return (strcmp(l
->un_mountp
, r
->un_mountp
));
7048 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
7049 * absolute path, find the entry /proc/self/mounts, verify that it's a
7050 * ZFS filesystem, and unmount it appropriately.
7053 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
7057 struct stat64 statbuf
;
7058 struct extmnttab entry
;
7059 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
7063 * Search for the given (major,minor) pair in the mount table.
7066 /* Reopen MNTTAB to prevent reading stale data from open file */
7067 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
7070 if (getextmntent(path
, &entry
, &statbuf
) != 0) {
7071 if (op
== OP_SHARE
) {
7072 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
7073 "currently mounted\n"), cmdname
, path
);
7076 (void) fprintf(stderr
, gettext("warning: %s not in"
7077 "/proc/self/mounts\n"), path
);
7078 if ((ret
= umount2(path
, flags
)) != 0)
7079 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
7083 path_inode
= statbuf
.st_ino
;
7085 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
7086 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
7087 "filesystem\n"), cmdname
, path
);
7091 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
7092 ZFS_TYPE_FILESYSTEM
)) == NULL
)
7096 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
7097 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
7098 cmdname
, path
, strerror(errno
));
7100 } else if (statbuf
.st_ino
!= path_inode
) {
7101 (void) fprintf(stderr
, gettext("cannot "
7102 "%s '%s': not a mountpoint\n"), cmdname
, path
);
7106 if (op
== OP_SHARE
) {
7107 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
7108 char smbshare_prop
[ZFS_MAXPROPLEN
];
7110 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
7111 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
7112 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
7113 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
7115 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
7116 strcmp(smbshare_prop
, "off") == 0) {
7117 (void) fprintf(stderr
, gettext("cannot unshare "
7118 "'%s': legacy share\n"), path
);
7119 (void) fprintf(stderr
, gettext("use exportfs(8) "
7120 "or smbcontrol(1) to unshare this filesystem\n"));
7121 } else if (!zfs_is_shared(zhp
)) {
7122 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
7123 "not currently shared\n"), path
);
7125 ret
= zfs_unshareall_bypath(zhp
, path
);
7126 zfs_commit_all_shares();
7129 char mtpt_prop
[ZFS_MAXPROPLEN
];
7131 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
7132 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
7135 ret
= zfs_unmount(zhp
, NULL
, flags
);
7136 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
7137 (void) fprintf(stderr
, gettext("cannot unmount "
7138 "'%s': legacy mountpoint\n"),
7140 (void) fprintf(stderr
, gettext("use umount(8) "
7141 "to unmount this filesystem\n"));
7143 ret
= zfs_unmountall(zhp
, flags
);
7154 * Generic callback for unsharing or unmounting a filesystem.
7157 unshare_unmount(int op
, int argc
, char **argv
)
7164 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
7165 char sharesmb
[ZFS_MAXPROPLEN
];
7168 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? ":a" : "afu")) != -1) {
7180 (void) fprintf(stderr
, gettext("missing argument for "
7181 "'%c' option\n"), optopt
);
7185 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7196 * We could make use of zfs_for_each() to walk all datasets in
7197 * the system, but this would be very inefficient, especially
7198 * since we would have to linearly search /proc/self/mounts for
7199 * each one. Instead, do one pass through /proc/self/mounts
7200 * looking for zfs entries and call zfs_unmount() for each one.
7202 * Things get a little tricky if the administrator has created
7203 * mountpoints beneath other ZFS filesystems. In this case, we
7204 * have to unmount the deepest filesystems first. To accomplish
7205 * this, we place all the mountpoints in an AVL tree sorted by
7206 * the special type (dataset name), and walk the result in
7207 * reverse to make sure to get any snapshots first.
7209 struct mnttab entry
;
7210 uu_avl_pool_t
*pool
;
7211 uu_avl_t
*tree
= NULL
;
7212 unshare_unmount_node_t
*node
;
7214 uu_avl_walk_t
*walk
;
7215 char *protocol
= NULL
;
7217 if (op
== OP_SHARE
&& argc
> 0) {
7218 if (strcmp(argv
[0], "nfs") != 0 &&
7219 strcmp(argv
[0], "smb") != 0) {
7220 (void) fprintf(stderr
, gettext("share type "
7221 "must be 'nfs' or 'smb'\n"));
7230 (void) fprintf(stderr
, gettext("too many arguments\n"));
7234 if (((pool
= uu_avl_pool_create("unmount_pool",
7235 sizeof (unshare_unmount_node_t
),
7236 offsetof(unshare_unmount_node_t
, un_avlnode
),
7237 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
7238 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
7241 /* Reopen MNTTAB to prevent reading stale data from open file */
7242 if (freopen(MNTTAB
, "r", mnttab_file
) == NULL
)
7245 while (getmntent(mnttab_file
, &entry
) == 0) {
7247 /* ignore non-ZFS entries */
7248 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
7251 /* ignore snapshots */
7252 if (strchr(entry
.mnt_special
, '@') != NULL
)
7255 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
7256 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
7262 * Ignore datasets that are excluded/restricted by
7265 if (zpool_skip_pool(zfs_get_pool_name(zhp
))) {
7272 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
7274 sizeof (nfs_mnt_prop
),
7275 NULL
, NULL
, 0, B_FALSE
) == 0);
7276 if (strcmp(nfs_mnt_prop
, "off") != 0)
7278 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
7280 sizeof (nfs_mnt_prop
),
7281 NULL
, NULL
, 0, B_FALSE
) == 0);
7282 if (strcmp(nfs_mnt_prop
, "off") == 0)
7286 /* Ignore legacy mounts */
7287 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
7289 sizeof (nfs_mnt_prop
),
7290 NULL
, NULL
, 0, B_FALSE
) == 0);
7291 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
7293 /* Ignore canmount=noauto mounts */
7294 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
7295 ZFS_CANMOUNT_NOAUTO
)
7301 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
7303 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
7305 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
7307 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
7308 uu_avl_insert(tree
, node
, idx
);
7310 zfs_close(node
->un_zhp
);
7311 free(node
->un_mountp
);
7317 * Walk the AVL tree in reverse, unmounting each filesystem and
7318 * removing it from the AVL tree in the process.
7320 if ((walk
= uu_avl_walk_start(tree
,
7321 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
7324 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
7325 const char *mntarg
= NULL
;
7327 uu_avl_remove(tree
, node
);
7330 if (zfs_unshareall_bytype(node
->un_zhp
,
7331 node
->un_mountp
, protocol
) != 0)
7336 if (zfs_unmount(node
->un_zhp
,
7337 mntarg
, flags
) != 0)
7342 zfs_close(node
->un_zhp
);
7343 free(node
->un_mountp
);
7348 zfs_commit_shares(protocol
);
7350 uu_avl_walk_end(walk
);
7351 uu_avl_destroy(tree
);
7352 uu_avl_pool_destroy(pool
);
7357 (void) fprintf(stderr
,
7358 gettext("missing filesystem argument\n"));
7360 (void) fprintf(stderr
,
7361 gettext("too many arguments\n"));
7366 * We have an argument, but it may be a full path or a ZFS
7367 * filesystem. Pass full paths off to unmount_path() (shared by
7368 * manual_unmount), otherwise open the filesystem and pass to
7371 if (argv
[0][0] == '/')
7372 return (unshare_unmount_path(op
, argv
[0],
7375 if ((zhp
= zfs_open(g_zfs
, argv
[0],
7376 ZFS_TYPE_FILESYSTEM
)) == NULL
)
7379 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
7380 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
7381 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
7382 NULL
, 0, B_FALSE
) == 0);
7386 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
7388 sizeof (nfs_mnt_prop
),
7389 NULL
, NULL
, 0, B_FALSE
) == 0);
7390 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
7391 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
7394 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
7395 strcmp(sharesmb
, "off") == 0) {
7396 (void) fprintf(stderr
, gettext("cannot "
7397 "unshare '%s': legacy share\n"),
7399 (void) fprintf(stderr
, gettext("use "
7400 "unshare(1M) to unshare this "
7403 } else if (!zfs_is_shared(zhp
)) {
7404 (void) fprintf(stderr
, gettext("cannot "
7405 "unshare '%s': not currently "
7406 "shared\n"), zfs_get_name(zhp
));
7408 } else if (zfs_unshareall(zhp
) != 0) {
7414 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
7415 (void) fprintf(stderr
, gettext("cannot "
7416 "unmount '%s': legacy "
7417 "mountpoint\n"), zfs_get_name(zhp
));
7418 (void) fprintf(stderr
, gettext("use "
7419 "umount(1M) to unmount this "
7422 } else if (!zfs_is_mounted(zhp
, NULL
)) {
7423 (void) fprintf(stderr
, gettext("cannot "
7424 "unmount '%s': not currently "
7428 } else if (zfs_unmountall(zhp
, flags
) != 0) {
7441 * zfs unmount [-fu] -a
7442 * zfs unmount [-fu] filesystem
7444 * Unmount all filesystems, or a specific ZFS filesystem.
7447 zfs_do_unmount(int argc
, char **argv
)
7449 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
7454 * zfs unshare filesystem
7456 * Unshare all filesystems, or a specific ZFS filesystem.
7459 zfs_do_unshare(int argc
, char **argv
)
7461 return (unshare_unmount(OP_SHARE
, argc
, argv
));
7465 find_command_idx(char *command
, int *idx
)
7469 for (i
= 0; i
< NCOMMAND
; i
++) {
7470 if (command_table
[i
].name
== NULL
)
7473 if (strcmp(command
, command_table
[i
].name
) == 0) {
7482 zfs_do_diff(int argc
, char **argv
)
7486 char *tosnap
= NULL
;
7487 char *fromsnap
= NULL
;
7491 struct sigaction sa
;
7493 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
7496 flags
|= ZFS_DIFF_CLASSIFY
;
7499 flags
|= ZFS_DIFF_PARSEABLE
;
7502 flags
|= ZFS_DIFF_TIMESTAMP
;
7505 (void) fprintf(stderr
,
7506 gettext("invalid option '%c'\n"), optopt
);
7515 (void) fprintf(stderr
,
7516 gettext("must provide at least one snapshot name\n"));
7521 (void) fprintf(stderr
, gettext("too many arguments\n"));
7526 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
7529 if (*fromsnap
!= '@')
7530 copy
= strdup(fromsnap
);
7532 copy
= strdup(tosnap
);
7536 if ((atp
= strchr(copy
, '@')) != NULL
)
7539 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
) {
7546 * Ignore SIGPIPE so that the library can give us
7547 * information on any failure
7549 if (sigemptyset(&sa
.sa_mask
) == -1) {
7554 sa
.sa_handler
= SIG_IGN
;
7555 if (sigaction(SIGPIPE
, &sa
, NULL
) == -1) {
7560 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
7568 * zfs bookmark <fs@source>|<fs#source> <fs#bookmark>
7570 * Creates a bookmark with the given name from the source snapshot
7571 * or creates a copy of an existing source bookmark.
7574 zfs_do_bookmark(int argc
, char **argv
)
7576 char *source
, *bookname
;
7577 char expbuf
[ZFS_MAX_DATASET_NAME_LEN
];
7584 while ((c
= getopt(argc
, argv
, "")) != -1) {
7587 (void) fprintf(stderr
,
7588 gettext("invalid option '%c'\n"), optopt
);
7596 /* check number of arguments */
7598 (void) fprintf(stderr
, gettext("missing source argument\n"));
7602 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
7609 if (strchr(source
, '@') == NULL
&& strchr(source
, '#') == NULL
) {
7610 (void) fprintf(stderr
,
7611 gettext("invalid source name '%s': "
7612 "must contain a '@' or '#'\n"), source
);
7615 if (strchr(bookname
, '#') == NULL
) {
7616 (void) fprintf(stderr
,
7617 gettext("invalid bookmark name '%s': "
7618 "must contain a '#'\n"), bookname
);
7623 * expand source or bookname to full path:
7624 * one of them may be specified as short name
7628 char *source_short
, *bookname_short
;
7629 source_short
= strpbrk(source
, "@#");
7630 bookname_short
= strpbrk(bookname
, "#");
7631 if (source_short
== source
&&
7632 bookname_short
== bookname
) {
7633 (void) fprintf(stderr
, gettext(
7634 "either source or bookmark must be specified as "
7635 "full dataset paths"));
7637 } else if (source_short
!= source
&&
7638 bookname_short
!= bookname
) {
7640 } else if (source_short
!= source
) {
7641 strlcpy(expbuf
, source
, sizeof (expbuf
));
7643 } else if (bookname_short
!= bookname
) {
7644 strlcpy(expbuf
, bookname
, sizeof (expbuf
));
7649 if (expand
!= NULL
) {
7650 *strpbrk(expbuf
, "@#") = '\0'; /* dataset name in buf */
7651 (void) strlcat(expbuf
, *expand
, sizeof (expbuf
));
7656 /* determine source type */
7657 switch (*strpbrk(source
, "@#")) {
7658 case '@': source_type
= ZFS_TYPE_SNAPSHOT
; break;
7659 case '#': source_type
= ZFS_TYPE_BOOKMARK
; break;
7663 /* test the source exists */
7665 zhp
= zfs_open(g_zfs
, source
, source_type
);
7670 nvl
= fnvlist_alloc();
7671 fnvlist_add_string(nvl
, bookname
, source
);
7672 ret
= lzc_bookmark(nvl
, NULL
);
7676 const char *err_msg
= NULL
;
7679 (void) snprintf(errbuf
, sizeof (errbuf
),
7680 dgettext(TEXT_DOMAIN
,
7681 "cannot create bookmark '%s'"), bookname
);
7685 err_msg
= "bookmark is in a different pool";
7687 case ZFS_ERR_BOOKMARK_SOURCE_NOT_ANCESTOR
:
7688 err_msg
= "source is not an ancestor of the "
7689 "new bookmark's dataset";
7692 err_msg
= "bookmark exists";
7695 err_msg
= "invalid argument";
7698 err_msg
= "bookmark feature not enabled";
7701 err_msg
= "out of space";
7704 err_msg
= "dataset does not exist";
7707 (void) zfs_standard_error(g_zfs
, ret
, errbuf
);
7710 if (err_msg
!= NULL
) {
7711 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
7712 dgettext(TEXT_DOMAIN
, err_msg
));
7724 zfs_do_channel_program(int argc
, char **argv
)
7727 char *progbuf
, *filename
, *poolname
;
7728 size_t progsize
, progread
;
7729 nvlist_t
*outnvl
= NULL
;
7730 uint64_t instrlimit
= ZCP_DEFAULT_INSTRLIMIT
;
7731 uint64_t memlimit
= ZCP_DEFAULT_MEMLIMIT
;
7732 boolean_t sync_flag
= B_TRUE
, json_output
= B_FALSE
;
7733 zpool_handle_t
*zhp
;
7736 while ((c
= getopt(argc
, argv
, "nt:m:j")) != -1) {
7744 arg
= strtoull(optarg
, &endp
, 0);
7745 if (errno
!= 0 || *endp
!= '\0') {
7746 (void) fprintf(stderr
, gettext(
7748 "'%s': expected integer\n"), optarg
);
7755 ASSERT3U(c
, ==, 'm');
7761 sync_flag
= B_FALSE
;
7765 json_output
= B_TRUE
;
7769 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7779 (void) fprintf(stderr
,
7780 gettext("invalid number of arguments\n"));
7786 if (strcmp(filename
, "-") == 0) {
7788 filename
= "standard input";
7789 } else if ((fd
= open(filename
, O_RDONLY
)) < 0) {
7790 (void) fprintf(stderr
, gettext("cannot open '%s': %s\n"),
7791 filename
, strerror(errno
));
7795 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
7796 (void) fprintf(stderr
, gettext("cannot open pool '%s'\n"),
7805 * Read in the channel program, expanding the program buffer as
7810 progbuf
= safe_malloc(progsize
);
7812 ret
= read(fd
, progbuf
+ progread
, progsize
- progread
);
7814 if (progread
== progsize
&& ret
> 0) {
7816 progbuf
= safe_realloc(progbuf
, progsize
);
7824 (void) fprintf(stderr
,
7825 gettext("cannot read '%s': %s\n"),
7826 filename
, strerror(errno
));
7829 progbuf
[progread
] = '\0';
7832 * Any remaining arguments are passed as arguments to the lua script as
7835 * "argv" -> [ "arg 1", ... "arg n" ],
7838 nvlist_t
*argnvl
= fnvlist_alloc();
7839 fnvlist_add_string_array(argnvl
, ZCP_ARG_CLIARGV
, argv
+ 2, argc
- 2);
7842 ret
= lzc_channel_program(poolname
, progbuf
,
7843 instrlimit
, memlimit
, argnvl
, &outnvl
);
7845 ret
= lzc_channel_program_nosync(poolname
, progbuf
,
7846 instrlimit
, memlimit
, argnvl
, &outnvl
);
7851 * On error, report the error message handed back by lua if one
7852 * exists. Otherwise, generate an appropriate error message,
7853 * falling back on strerror() for an unexpected return code.
7855 char *errstring
= NULL
;
7856 const char *msg
= gettext("Channel program execution failed");
7857 uint64_t instructions
= 0;
7858 if (outnvl
!= NULL
&& nvlist_exists(outnvl
, ZCP_RET_ERROR
)) {
7859 (void) nvlist_lookup_string(outnvl
,
7860 ZCP_RET_ERROR
, &errstring
);
7861 if (errstring
== NULL
)
7862 errstring
= strerror(ret
);
7864 (void) nvlist_lookup_uint64(outnvl
,
7865 ZCP_ARG_INSTRLIMIT
, &instructions
);
7871 "Invalid instruction or memory limit.";
7874 errstring
= "Return value too large.";
7877 errstring
= "Memory limit exhausted.";
7880 errstring
= "Timed out.";
7883 errstring
= "Permission denied. Channel "
7884 "programs must be run as root.";
7887 (void) zfs_standard_error(g_zfs
, ret
, msg
);
7890 if (errstring
!= NULL
)
7891 (void) fprintf(stderr
, "%s:\n%s\n", msg
, errstring
);
7893 if (ret
== ETIME
&& instructions
!= 0)
7894 (void) fprintf(stderr
,
7895 gettext("%llu Lua instructions\n"),
7896 (u_longlong_t
)instructions
);
7899 (void) nvlist_print_json(stdout
, outnvl
);
7900 } else if (nvlist_empty(outnvl
)) {
7901 (void) fprintf(stdout
, gettext("Channel program fully "
7902 "executed and did not produce output.\n"));
7904 (void) fprintf(stdout
, gettext("Channel program fully "
7905 "executed and produced output:\n"));
7906 dump_nvlist(outnvl
, 4);
7911 fnvlist_free(outnvl
);
7912 fnvlist_free(argnvl
);
7921 typedef struct loadkey_cbdata
{
7922 boolean_t cb_loadkey
;
7923 boolean_t cb_recursive
;
7925 char *cb_keylocation
;
7926 uint64_t cb_numfailed
;
7927 uint64_t cb_numattempted
;
7931 load_key_callback(zfs_handle_t
*zhp
, void *data
)
7934 boolean_t is_encroot
;
7935 loadkey_cbdata_t
*cb
= data
;
7936 uint64_t keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
7939 * If we are working recursively, we want to skip loading / unloading
7940 * keys for non-encryption roots and datasets whose keys are already
7941 * in the desired end-state.
7943 if (cb
->cb_recursive
) {
7944 ret
= zfs_crypto_get_encryption_root(zhp
, &is_encroot
, NULL
);
7950 if ((cb
->cb_loadkey
&& keystatus
== ZFS_KEYSTATUS_AVAILABLE
) ||
7951 (!cb
->cb_loadkey
&& keystatus
== ZFS_KEYSTATUS_UNAVAILABLE
))
7955 cb
->cb_numattempted
++;
7958 ret
= zfs_crypto_load_key(zhp
, cb
->cb_noop
, cb
->cb_keylocation
);
7960 ret
= zfs_crypto_unload_key(zhp
);
7971 load_unload_keys(int argc
, char **argv
, boolean_t loadkey
)
7973 int c
, ret
= 0, flags
= 0;
7974 boolean_t do_all
= B_FALSE
;
7975 loadkey_cbdata_t cb
= { 0 };
7977 cb
.cb_loadkey
= loadkey
;
7979 while ((c
= getopt(argc
, argv
, "anrL:")) != -1) {
7980 /* noop and alternate keylocations only apply to zfs load-key */
7984 cb
.cb_noop
= B_TRUE
;
7987 cb
.cb_keylocation
= optarg
;
7997 cb
.cb_recursive
= B_TRUE
;
8000 flags
|= ZFS_ITER_RECURSE
;
8001 cb
.cb_recursive
= B_TRUE
;
8004 (void) fprintf(stderr
,
8005 gettext("invalid option '%c'\n"), optopt
);
8013 if (!do_all
&& argc
== 0) {
8014 (void) fprintf(stderr
,
8015 gettext("Missing dataset argument or -a option\n"));
8019 if (do_all
&& argc
!= 0) {
8020 (void) fprintf(stderr
,
8021 gettext("Cannot specify dataset with -a option\n"));
8025 if (cb
.cb_recursive
&& cb
.cb_keylocation
!= NULL
&&
8026 strcmp(cb
.cb_keylocation
, "prompt") != 0) {
8027 (void) fprintf(stderr
, gettext("alternate keylocation may only "
8028 "be 'prompt' with -r or -a\n"));
8032 ret
= zfs_for_each(argc
, argv
, flags
,
8033 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
, NULL
, NULL
, 0,
8034 load_key_callback
, &cb
);
8036 if (cb
.cb_noop
|| (cb
.cb_recursive
&& cb
.cb_numattempted
!= 0)) {
8037 (void) printf(gettext("%llu / %llu key(s) successfully %s\n"),
8038 (u_longlong_t
)(cb
.cb_numattempted
- cb
.cb_numfailed
),
8039 (u_longlong_t
)cb
.cb_numattempted
,
8040 loadkey
? (cb
.cb_noop
? "verified" : "loaded") :
8044 if (cb
.cb_numfailed
!= 0)
8051 zfs_do_load_key(int argc
, char **argv
)
8053 return (load_unload_keys(argc
, argv
, B_TRUE
));
8058 zfs_do_unload_key(int argc
, char **argv
)
8060 return (load_unload_keys(argc
, argv
, B_FALSE
));
8064 zfs_do_change_key(int argc
, char **argv
)
8068 boolean_t loadkey
= B_FALSE
, inheritkey
= B_FALSE
;
8069 zfs_handle_t
*zhp
= NULL
;
8070 nvlist_t
*props
= fnvlist_alloc();
8072 while ((c
= getopt(argc
, argv
, "lio:")) != -1) {
8078 inheritkey
= B_TRUE
;
8081 if (!parseprop(props
, optarg
)) {
8087 (void) fprintf(stderr
,
8088 gettext("invalid option '%c'\n"), optopt
);
8093 if (inheritkey
&& !nvlist_empty(props
)) {
8094 (void) fprintf(stderr
,
8095 gettext("Properties not allowed for inheriting\n"));
8103 (void) fprintf(stderr
, gettext("Missing dataset argument\n"));
8108 (void) fprintf(stderr
, gettext("Too many arguments\n"));
8112 zhp
= zfs_open(g_zfs
, argv
[argc
- 1],
8113 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
8118 keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
8119 if (keystatus
!= ZFS_KEYSTATUS_AVAILABLE
) {
8120 ret
= zfs_crypto_load_key(zhp
, B_FALSE
, NULL
);
8128 /* refresh the properties so the new keystatus is visible */
8129 zfs_refresh_properties(zhp
);
8132 ret
= zfs_crypto_rewrap(zhp
, props
, inheritkey
);
8145 * 1) zfs project [-d|-r] <file|directory ...>
8146 * List project ID and inherit flag of file(s) or directories.
8147 * -d: List the directory itself, not its children.
8148 * -r: List subdirectories recursively.
8150 * 2) zfs project -C [-k] [-r] <file|directory ...>
8151 * Clear project inherit flag and/or ID on the file(s) or directories.
8152 * -k: Keep the project ID unchanged. If not specified, the project ID
8153 * will be reset as zero.
8154 * -r: Clear on subdirectories recursively.
8156 * 3) zfs project -c [-0] [-d|-r] [-p id] <file|directory ...>
8157 * Check project ID and inherit flag on the file(s) or directories,
8158 * report the outliers.
8159 * -0: Print file name followed by a NUL instead of newline.
8160 * -d: Check the directory itself, not its children.
8161 * -p: Specify the referenced ID for comparing with the target file(s)
8162 * or directories' project IDs. If not specified, the target (top)
8163 * directory's project ID will be used as the referenced one.
8164 * -r: Check subdirectories recursively.
8166 * 4) zfs project [-p id] [-r] [-s] <file|directory ...>
8167 * Set project ID and/or inherit flag on the file(s) or directories.
8168 * -p: Set the project ID as the given id.
8169 * -r: Set on subdirectories recursively. If not specify "-p" option,
8170 * it will use top-level directory's project ID as the given id,
8171 * then set both project ID and inherit flag on all descendants
8172 * of the top-level directory.
8173 * -s: Set project inherit flag.
8176 zfs_do_project(int argc
, char **argv
)
8178 zfs_project_control_t zpc
= {
8179 .zpc_expected_projid
= ZFS_INVALID_PROJID
,
8180 .zpc_op
= ZFS_PROJECT_OP_DEFAULT
,
8181 .zpc_dironly
= B_FALSE
,
8182 .zpc_keep_projid
= B_FALSE
,
8183 .zpc_newline
= B_TRUE
,
8184 .zpc_recursive
= B_FALSE
,
8185 .zpc_set_flag
= B_FALSE
,
8192 while ((c
= getopt(argc
, argv
, "0Ccdkp:rs")) != -1) {
8195 zpc
.zpc_newline
= B_FALSE
;
8198 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
8199 (void) fprintf(stderr
, gettext("cannot "
8200 "specify '-C' '-c' '-s' together\n"));
8204 zpc
.zpc_op
= ZFS_PROJECT_OP_CLEAR
;
8207 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
8208 (void) fprintf(stderr
, gettext("cannot "
8209 "specify '-C' '-c' '-s' together\n"));
8213 zpc
.zpc_op
= ZFS_PROJECT_OP_CHECK
;
8216 zpc
.zpc_dironly
= B_TRUE
;
8217 /* overwrite "-r" option */
8218 zpc
.zpc_recursive
= B_FALSE
;
8221 zpc
.zpc_keep_projid
= B_TRUE
;
8227 zpc
.zpc_expected_projid
= strtoull(optarg
, &endptr
, 0);
8228 if (errno
!= 0 || *endptr
!= '\0') {
8229 (void) fprintf(stderr
,
8230 gettext("project ID must be less than "
8231 "%u\n"), UINT32_MAX
);
8234 if (zpc
.zpc_expected_projid
>= UINT32_MAX
) {
8235 (void) fprintf(stderr
,
8236 gettext("invalid project ID\n"));
8242 zpc
.zpc_recursive
= B_TRUE
;
8243 /* overwrite "-d" option */
8244 zpc
.zpc_dironly
= B_FALSE
;
8247 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
8248 (void) fprintf(stderr
, gettext("cannot "
8249 "specify '-C' '-c' '-s' together\n"));
8253 zpc
.zpc_set_flag
= B_TRUE
;
8254 zpc
.zpc_op
= ZFS_PROJECT_OP_SET
;
8257 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8263 if (zpc
.zpc_op
== ZFS_PROJECT_OP_DEFAULT
) {
8264 if (zpc
.zpc_expected_projid
!= ZFS_INVALID_PROJID
)
8265 zpc
.zpc_op
= ZFS_PROJECT_OP_SET
;
8267 zpc
.zpc_op
= ZFS_PROJECT_OP_LIST
;
8270 switch (zpc
.zpc_op
) {
8271 case ZFS_PROJECT_OP_LIST
:
8272 if (zpc
.zpc_keep_projid
) {
8273 (void) fprintf(stderr
,
8274 gettext("'-k' is only valid together with '-C'\n"));
8277 if (!zpc
.zpc_newline
) {
8278 (void) fprintf(stderr
,
8279 gettext("'-0' is only valid together with '-c'\n"));
8283 case ZFS_PROJECT_OP_CHECK
:
8284 if (zpc
.zpc_keep_projid
) {
8285 (void) fprintf(stderr
,
8286 gettext("'-k' is only valid together with '-C'\n"));
8290 case ZFS_PROJECT_OP_CLEAR
:
8291 if (zpc
.zpc_dironly
) {
8292 (void) fprintf(stderr
,
8293 gettext("'-d' is useless together with '-C'\n"));
8296 if (!zpc
.zpc_newline
) {
8297 (void) fprintf(stderr
,
8298 gettext("'-0' is only valid together with '-c'\n"));
8301 if (zpc
.zpc_expected_projid
!= ZFS_INVALID_PROJID
) {
8302 (void) fprintf(stderr
,
8303 gettext("'-p' is useless together with '-C'\n"));
8307 case ZFS_PROJECT_OP_SET
:
8308 if (zpc
.zpc_dironly
) {
8309 (void) fprintf(stderr
,
8310 gettext("'-d' is useless for set project ID and/or "
8314 if (zpc
.zpc_keep_projid
) {
8315 (void) fprintf(stderr
,
8316 gettext("'-k' is only valid together with '-C'\n"));
8319 if (!zpc
.zpc_newline
) {
8320 (void) fprintf(stderr
,
8321 gettext("'-0' is only valid together with '-c'\n"));
8333 (void) fprintf(stderr
,
8334 gettext("missing file or directory target(s)\n"));
8338 for (int i
= 0; i
< argc
; i
++) {
8341 err
= zfs_project_handle(argv
[i
], &zpc
);
8350 zfs_do_wait(int argc
, char **argv
)
8352 boolean_t enabled
[ZFS_WAIT_NUM_ACTIVITIES
];
8356 /* By default, wait for all types of activity. */
8357 for (i
= 0; i
< ZFS_WAIT_NUM_ACTIVITIES
; i
++)
8358 enabled
[i
] = B_TRUE
;
8360 while ((c
= getopt(argc
, argv
, "t:")) != -1) {
8364 static char *col_subopts
[] = { "deleteq", NULL
};
8367 /* Reset activities array */
8368 bzero(&enabled
, sizeof (enabled
));
8369 while (*optarg
!= '\0') {
8370 int activity
= getsubopt(&optarg
, col_subopts
,
8374 (void) fprintf(stderr
,
8375 gettext("invalid activity '%s'\n"),
8380 enabled
[activity
] = B_TRUE
;
8385 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8394 (void) fprintf(stderr
, gettext("missing 'filesystem' "
8399 (void) fprintf(stderr
, gettext("too many arguments\n"));
8403 zfs_handle_t
*zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
);
8408 boolean_t missing
= B_FALSE
;
8409 boolean_t any_waited
= B_FALSE
;
8411 for (int i
= 0; i
< ZFS_WAIT_NUM_ACTIVITIES
; i
++) {
8417 error
= zfs_wait_status(zhp
, i
, &missing
, &waited
);
8418 if (error
!= 0 || missing
)
8421 any_waited
= (any_waited
|| waited
);
8424 if (error
!= 0 || missing
|| !any_waited
)
8434 * Display version message
8437 zfs_do_version(int argc
, char **argv
)
8439 if (zfs_version_print() == -1)
8446 main(int argc
, char **argv
)
8453 (void) setlocale(LC_ALL
, "");
8454 (void) textdomain(TEXT_DOMAIN
);
8459 * Make sure the user has specified some command.
8462 (void) fprintf(stderr
, gettext("missing command\n"));
8469 * The 'umount' command is an alias for 'unmount'
8471 if (strcmp(cmdname
, "umount") == 0)
8472 cmdname
= "unmount";
8475 * The 'recv' command is an alias for 'receive'
8477 if (strcmp(cmdname
, "recv") == 0)
8478 cmdname
= "receive";
8481 * The 'snap' command is an alias for 'snapshot'
8483 if (strcmp(cmdname
, "snap") == 0)
8484 cmdname
= "snapshot";
8489 if ((strcmp(cmdname
, "-?") == 0) ||
8490 (strcmp(cmdname
, "--help") == 0))
8494 * Special case '-V|--version'
8496 if ((strcmp(cmdname
, "-V") == 0) || (strcmp(cmdname
, "--version") == 0))
8497 return (zfs_do_version(argc
, argv
));
8499 if ((g_zfs
= libzfs_init()) == NULL
) {
8500 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
8504 mnttab_file
= g_zfs
->libzfs_mnttab
;
8506 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
8508 libzfs_print_on_error(g_zfs
, B_TRUE
);
8511 * Many commands modify input strings for string parsing reasons.
8512 * We create a copy to protect the original argv.
8514 newargv
= malloc((argc
+ 1) * sizeof (newargv
[0]));
8515 for (i
= 0; i
< argc
; i
++)
8516 newargv
[i
] = strdup(argv
[i
]);
8517 newargv
[argc
] = NULL
;
8520 * Run the appropriate command.
8522 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
8523 if (find_command_idx(cmdname
, &i
) == 0) {
8524 current_command
= &command_table
[i
];
8525 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
8526 } else if (strchr(cmdname
, '=') != NULL
) {
8527 verify(find_command_idx("set", &i
) == 0);
8528 current_command
= &command_table
[i
];
8529 ret
= command_table
[i
].func(argc
, newargv
);
8531 (void) fprintf(stderr
, gettext("unrecognized "
8532 "command '%s'\n"), cmdname
);
8537 for (i
= 0; i
< argc
; i
++)
8541 if (ret
== 0 && log_history
)
8542 (void) zpool_log_history(g_zfs
, history_str
);
8547 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8548 * for the purposes of running ::findleaks.
8550 if (getenv("ZFS_ABORT") != NULL
) {
8551 (void) printf("dumping core by request\n");
8559 #include <sys/jail.h>
8562 * Attach/detach the given dataset to/from the given jail
8566 zfs_do_jail_impl(int argc
, char **argv
, boolean_t attach
)
8571 /* check number of arguments */
8573 (void) fprintf(stderr
, gettext("missing argument(s)\n"));
8577 (void) fprintf(stderr
, gettext("too many arguments\n"));
8581 jailid
= jail_getid(argv
[1]);
8583 (void) fprintf(stderr
, gettext("invalid jail id or name\n"));
8587 zhp
= zfs_open(g_zfs
, argv
[2], ZFS_TYPE_FILESYSTEM
);
8591 ret
= (zfs_jail(zhp
, jailid
, attach
) != 0);
8598 * zfs jail jailid filesystem
8600 * Attach the given dataset to the given jail
8604 zfs_do_jail(int argc
, char **argv
)
8606 return (zfs_do_jail_impl(argc
, argv
, B_TRUE
));
8610 * zfs unjail jailid filesystem
8612 * Detach the given dataset from the given jail
8616 zfs_do_unjail(int argc
, char **argv
)
8618 return (zfs_do_jail_impl(argc
, argv
, B_FALSE
));