4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
29 * Copyright 2016 Nexenta Systems, Inc.
30 * Copyright (c) 2019 Datto Inc.
31 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
32 * Copyright 2019 Joyent, Inc.
33 * Copyright (c) 2019, 2020 by Christian Schwarz. All rights reserved.
38 #include <sys/debug.h>
44 #include <libnvpair.h>
59 #include <sys/mkdev.h>
60 #include <sys/mntent.h>
61 #include <sys/mnttab.h>
62 #include <sys/mount.h>
64 #include <sys/fs/zfs.h>
65 #include <sys/systeminfo.h>
66 #include <sys/types.h>
68 #include <sys/zfs_project.h>
71 #include <libzfs_core.h>
73 #include <zfs_deleg.h>
77 #include <directory.h>
78 #endif /* HAVE_IDMAP */
82 #include "zfs_comutil.h"
83 #include "zfs_projectutil.h"
85 libzfs_handle_t
*g_zfs
;
87 static char history_str
[HIS_MAX_RECORD_LEN
];
88 static boolean_t log_history
= B_TRUE
;
90 static int zfs_do_clone(int argc
, char **argv
);
91 static int zfs_do_create(int argc
, char **argv
);
92 static int zfs_do_destroy(int argc
, char **argv
);
93 static int zfs_do_get(int argc
, char **argv
);
94 static int zfs_do_inherit(int argc
, char **argv
);
95 static int zfs_do_list(int argc
, char **argv
);
96 static int zfs_do_mount(int argc
, char **argv
);
97 static int zfs_do_rename(int argc
, char **argv
);
98 static int zfs_do_rollback(int argc
, char **argv
);
99 static int zfs_do_set(int argc
, char **argv
);
100 static int zfs_do_upgrade(int argc
, char **argv
);
101 static int zfs_do_snapshot(int argc
, char **argv
);
102 static int zfs_do_unmount(int argc
, char **argv
);
103 static int zfs_do_share(int argc
, char **argv
);
104 static int zfs_do_unshare(int argc
, char **argv
);
105 static int zfs_do_send(int argc
, char **argv
);
106 static int zfs_do_receive(int argc
, char **argv
);
107 static int zfs_do_promote(int argc
, char **argv
);
108 static int zfs_do_userspace(int argc
, char **argv
);
109 static int zfs_do_allow(int argc
, char **argv
);
110 static int zfs_do_unallow(int argc
, char **argv
);
111 static int zfs_do_hold(int argc
, char **argv
);
112 static int zfs_do_holds(int argc
, char **argv
);
113 static int zfs_do_release(int argc
, char **argv
);
114 static int zfs_do_diff(int argc
, char **argv
);
115 static int zfs_do_bookmark(int argc
, char **argv
);
116 static int zfs_do_channel_program(int argc
, char **argv
);
117 static int zfs_do_load_key(int argc
, char **argv
);
118 static int zfs_do_unload_key(int argc
, char **argv
);
119 static int zfs_do_change_key(int argc
, char **argv
);
120 static int zfs_do_project(int argc
, char **argv
);
121 static int zfs_do_version(int argc
, char **argv
);
122 static int zfs_do_redact(int argc
, char **argv
);
123 static int zfs_do_wait(int argc
, char **argv
);
126 static int zfs_do_jail(int argc
, char **argv
);
127 static int zfs_do_unjail(int argc
, char **argv
);
131 static int zfs_do_zone(int argc
, char **argv
);
132 static int zfs_do_unzone(int argc
, char **argv
);
135 static int zfs_do_help(int argc
, char **argv
);
138 ZFS_OPTION_JSON_NUMS_AS_INT
= 1024
142 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
147 _umem_debug_init(void)
149 return ("default,verbose"); /* $UMEM_DEBUG setting */
153 _umem_logging_init(void)
155 return ("fail,contents"); /* $UMEM_LOGGING setting */
189 HELP_CHANNEL_PROGRAM
,
202 typedef struct zfs_command
{
204 int (*func
)(int argc
, char **argv
);
209 * Master command table. Each ZFS command has a name, associated function, and
210 * usage message. The usage messages need to be internationalized, so we have
211 * to have a function to return the usage message based on a command index.
213 * These commands are organized according to how they are displayed in the usage
214 * message. An empty command (one with a NULL name) indicates an empty line in
215 * the generic usage message.
217 static zfs_command_t command_table
[] = {
218 { "version", zfs_do_version
, HELP_VERSION
},
220 { "create", zfs_do_create
, HELP_CREATE
},
221 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
223 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
224 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
225 { "clone", zfs_do_clone
, HELP_CLONE
},
226 { "promote", zfs_do_promote
, HELP_PROMOTE
},
227 { "rename", zfs_do_rename
, HELP_RENAME
},
228 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
229 { "program", zfs_do_channel_program
, HELP_CHANNEL_PROGRAM
},
231 { "list", zfs_do_list
, HELP_LIST
},
233 { "set", zfs_do_set
, HELP_SET
},
234 { "get", zfs_do_get
, HELP_GET
},
235 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
236 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
238 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
239 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
240 { "projectspace", zfs_do_userspace
, HELP_PROJECTSPACE
},
242 { "project", zfs_do_project
, HELP_PROJECT
},
244 { "mount", zfs_do_mount
, HELP_MOUNT
},
245 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
246 { "share", zfs_do_share
, HELP_SHARE
},
247 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
249 { "send", zfs_do_send
, HELP_SEND
},
250 { "receive", zfs_do_receive
, HELP_RECEIVE
},
252 { "allow", zfs_do_allow
, HELP_ALLOW
},
254 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
256 { "hold", zfs_do_hold
, HELP_HOLD
},
257 { "holds", zfs_do_holds
, HELP_HOLDS
},
258 { "release", zfs_do_release
, HELP_RELEASE
},
259 { "diff", zfs_do_diff
, HELP_DIFF
},
260 { "load-key", zfs_do_load_key
, HELP_LOAD_KEY
},
261 { "unload-key", zfs_do_unload_key
, HELP_UNLOAD_KEY
},
262 { "change-key", zfs_do_change_key
, HELP_CHANGE_KEY
},
263 { "redact", zfs_do_redact
, HELP_REDACT
},
264 { "wait", zfs_do_wait
, HELP_WAIT
},
267 { "jail", zfs_do_jail
, HELP_JAIL
},
268 { "unjail", zfs_do_unjail
, HELP_UNJAIL
},
272 { "zone", zfs_do_zone
, HELP_ZONE
},
273 { "unzone", zfs_do_unzone
, HELP_UNZONE
},
277 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
279 #define MAX_CMD_LEN 256
281 zfs_command_t
*current_command
;
284 get_usage(zfs_help_t idx
)
288 return (gettext("\tclone [-p] [-o property=value] ... "
289 "<snapshot> <filesystem|volume>\n"));
291 return (gettext("\tcreate [-Pnpuv] [-o property=value] ... "
293 "\tcreate [-Pnpsv] [-b blocksize] [-o property=value] ... "
294 "-V <size> <volume>\n"));
296 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
297 "\tdestroy [-dnpRrv] "
298 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
299 "\tdestroy <filesystem|volume>#<bookmark>\n"));
301 return (gettext("\tget [-rHp] [-j [--json-int]] [-d max] "
302 "[-o \"all\" | field[,...]]\n"
303 "\t [-t type[,...]] [-s source[,...]]\n"
304 "\t <\"all\" | property[,...]> "
305 "[filesystem|volume|snapshot|bookmark] ...\n"));
307 return (gettext("\tinherit [-rS] <property> "
308 "<filesystem|volume|snapshot> ...\n"));
310 return (gettext("\tupgrade [-v]\n"
311 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
313 return (gettext("\tlist [-Hp] [-j [--json-int]] [-r|-d max] "
314 "[-o property[,...]] [-s property]...\n\t "
315 "[-S property]... [-t type[,...]] "
316 "[filesystem|volume|snapshot] ...\n"));
318 return (gettext("\tmount [-j]\n"
319 "\tmount [-flvO] [-o opts] <-a|-R filesystem|"
322 return (gettext("\tpromote <clone-filesystem>\n"));
324 return (gettext("\treceive [-vMnsFhu] "
325 "[-o <property>=<value>] ... [-x <property>] ...\n"
326 "\t <filesystem|volume|snapshot>\n"
327 "\treceive [-vMnsFhu] [-o <property>=<value>] ... "
328 "[-x <property>] ... \n"
329 "\t [-d | -e] <filesystem>\n"
330 "\treceive -A <filesystem|volume>\n"));
332 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
333 "<filesystem|volume|snapshot>\n"
334 "\trename -p [-f] <filesystem|volume> <filesystem|volume>\n"
335 "\trename -u [-f] <filesystem> <filesystem>\n"
336 "\trename -r <snapshot> <snapshot>\n"));
338 return (gettext("\trollback [-rRf] <snapshot>\n"));
340 return (gettext("\tsend [-DLPbcehnpsVvw] "
342 "\t [-R [-X dataset[,dataset]...]] <snapshot>\n"
343 "\tsend [-DnVvPLecw] [-i snapshot|bookmark] "
344 "<filesystem|volume|snapshot>\n"
345 "\tsend [-DnPpVvLec] [-i bookmark|snapshot] "
346 "--redact <bookmark> <snapshot>\n"
347 "\tsend [-nVvPe] -t <receive_resume_token>\n"
348 "\tsend [-PnVv] --saved filesystem\n"));
350 return (gettext("\tset [-u] <property=value> ... "
351 "<filesystem|volume|snapshot> ...\n"));
353 return (gettext("\tshare [-l] <-a [nfs|smb] | filesystem>\n"));
355 return (gettext("\tsnapshot [-r] [-o property=value] ... "
356 "<filesystem|volume>@<snap> ...\n"));
358 return (gettext("\tunmount [-fu] "
359 "<-a | filesystem|mountpoint>\n"));
361 return (gettext("\tunshare "
362 "<-a [nfs|smb] | filesystem|mountpoint>\n"));
364 return (gettext("\tallow <filesystem|volume>\n"
366 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
367 "\t <filesystem|volume>\n"
368 "\tallow [-ld] -e <perm|@setname>[,...] "
369 "<filesystem|volume>\n"
370 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
371 "\tallow -s @setname <perm|@setname>[,...] "
372 "<filesystem|volume>\n"));
374 return (gettext("\tunallow [-rldug] "
375 "<\"everyone\"|user|group>[,...]\n"
376 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
377 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
378 "<filesystem|volume>\n"
379 "\tunallow [-r] -c [<perm|@setname>[,...]] "
380 "<filesystem|volume>\n"
381 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
382 "<filesystem|volume>\n"));
384 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
386 "\t [-S field] ... [-t type[,...]] "
387 "<filesystem|snapshot|path>\n"));
388 case HELP_GROUPSPACE
:
389 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
391 "\t [-S field] ... [-t type[,...]] "
392 "<filesystem|snapshot|path>\n"));
393 case HELP_PROJECTSPACE
:
394 return (gettext("\tprojectspace [-Hp] [-o field[,...]] "
396 "\t [-S field] ... <filesystem|snapshot|path>\n"));
398 return (gettext("\tproject [-d|-r] <directory|file ...>\n"
399 "\tproject -c [-0] [-d|-r] [-p id] <directory|file ...>\n"
400 "\tproject -C [-k] [-r] <directory ...>\n"
401 "\tproject [-p id] [-r] [-s] <directory ...>\n"));
403 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
405 return (gettext("\tholds [-rHp] <snapshot> ...\n"));
407 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
409 return (gettext("\tdiff [-FHth] <snapshot> "
410 "[snapshot|filesystem]\n"));
412 return (gettext("\tbookmark <snapshot|bookmark> "
414 case HELP_CHANNEL_PROGRAM
:
415 return (gettext("\tprogram [-jn] [-t <instruction limit>] "
416 "[-m <memory limit (b)>]\n"
417 "\t <pool> <program file> [lua args...]\n"));
419 return (gettext("\tload-key [-rn] [-L <keylocation>] "
420 "<-a | filesystem|volume>\n"));
421 case HELP_UNLOAD_KEY
:
422 return (gettext("\tunload-key [-r] "
423 "<-a | filesystem|volume>\n"));
424 case HELP_CHANGE_KEY
:
425 return (gettext("\tchange-key [-l] [-o keyformat=<value>]\n"
426 "\t [-o keylocation=<value>] [-o pbkdf2iters=<value>]\n"
427 "\t <filesystem|volume>\n"
428 "\tchange-key -i [-l] <filesystem|volume>\n"));
430 return (gettext("\tversion [-j]\n"));
432 return (gettext("\tredact <snapshot> <bookmark> "
433 "<redaction_snapshot> ...\n"));
435 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
437 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
439 return (gettext("\twait [-t <activity>] <filesystem>\n"));
441 return (gettext("\tzone <nsfile> <filesystem>\n"));
443 return (gettext("\tunzone <nsfile> <filesystem>\n"));
445 __builtin_unreachable();
452 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
457 * Utility function to guarantee malloc() success.
461 safe_malloc(size_t size
)
465 if ((data
= calloc(1, size
)) == NULL
)
472 safe_realloc(void *data
, size_t size
)
475 if ((newp
= realloc(data
, size
)) == NULL
) {
484 safe_strdup(const char *str
)
486 char *dupstr
= strdup(str
);
495 * Callback routine that will print out information for each of
499 usage_prop_cb(int prop
, void *cb
)
503 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
505 if (zfs_prop_readonly(prop
))
506 (void) fprintf(fp
, " NO ");
508 (void) fprintf(fp
, "YES ");
510 if (zfs_prop_inheritable(prop
))
511 (void) fprintf(fp
, " YES ");
513 (void) fprintf(fp
, " NO ");
515 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
) ?: "-");
521 * Display usage message. If we're inside a command, display only the usage for
522 * that command. Otherwise, iterate over the entire command table and display
523 * a complete usage message.
525 static __attribute__((noreturn
)) void
526 usage(boolean_t requested
)
529 boolean_t show_properties
= B_FALSE
;
530 FILE *fp
= requested
? stdout
: stderr
;
532 if (current_command
== NULL
) {
534 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
536 gettext("where 'command' is one of the following:\n\n"));
538 for (i
= 0; i
< NCOMMAND
; i
++) {
539 if (command_table
[i
].name
== NULL
)
540 (void) fprintf(fp
, "\n");
542 (void) fprintf(fp
, "%s",
543 get_usage(command_table
[i
].usage
));
546 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
547 "pool/[dataset/]*dataset[@name]\n"));
549 (void) fprintf(fp
, gettext("usage:\n"));
550 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
553 if (current_command
!= NULL
&&
554 (strcmp(current_command
->name
, "set") == 0 ||
555 strcmp(current_command
->name
, "get") == 0 ||
556 strcmp(current_command
->name
, "inherit") == 0 ||
557 strcmp(current_command
->name
, "list") == 0))
558 show_properties
= B_TRUE
;
560 if (show_properties
) {
561 (void) fprintf(fp
, "%s",
562 gettext("\nThe following properties are supported:\n"));
564 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
565 "PROPERTY", "EDIT", "INHERIT", "VALUES");
567 /* Iterate over all properties */
568 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
571 (void) fprintf(fp
, "\t%-15s ", "userused@...");
572 (void) fprintf(fp
, " NO NO <size>\n");
573 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
574 (void) fprintf(fp
, " NO NO <size>\n");
575 (void) fprintf(fp
, "\t%-15s ", "projectused@...");
576 (void) fprintf(fp
, " NO NO <size>\n");
577 (void) fprintf(fp
, "\t%-15s ", "userobjused@...");
578 (void) fprintf(fp
, " NO NO <size>\n");
579 (void) fprintf(fp
, "\t%-15s ", "groupobjused@...");
580 (void) fprintf(fp
, " NO NO <size>\n");
581 (void) fprintf(fp
, "\t%-15s ", "projectobjused@...");
582 (void) fprintf(fp
, " NO NO <size>\n");
583 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
584 (void) fprintf(fp
, "YES NO <size> | none\n");
585 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
586 (void) fprintf(fp
, "YES NO <size> | none\n");
587 (void) fprintf(fp
, "\t%-15s ", "projectquota@...");
588 (void) fprintf(fp
, "YES NO <size> | none\n");
589 (void) fprintf(fp
, "\t%-15s ", "userobjquota@...");
590 (void) fprintf(fp
, "YES NO <size> | none\n");
591 (void) fprintf(fp
, "\t%-15s ", "groupobjquota@...");
592 (void) fprintf(fp
, "YES NO <size> | none\n");
593 (void) fprintf(fp
, "\t%-15s ", "projectobjquota@...");
594 (void) fprintf(fp
, "YES NO <size> | none\n");
595 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
596 (void) fprintf(fp
, " NO NO <size>\n");
597 (void) fprintf(fp
, "\t%-15s ", "written#<bookmark>");
598 (void) fprintf(fp
, " NO NO <size>\n");
600 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
601 "with standard units such as K, M, G, etc.\n"));
602 (void) fprintf(fp
, "%s", gettext("\nUser-defined properties "
603 "can be specified by using a name containing a colon "
605 (void) fprintf(fp
, gettext("\nThe {user|group|project}"
606 "[obj]{used|quota}@ properties must be appended with\n"
607 "a user|group|project specifier of one of these forms:\n"
608 " POSIX name (eg: \"matt\")\n"
609 " POSIX id (eg: \"126829\")\n"
610 " SMB name@domain (eg: \"matt@sun\")\n"
611 " SMB SID (eg: \"S-1-234-567-89\")\n"));
614 gettext("\nFor the property list, run: %s\n"),
617 gettext("\nFor the delegated permission list, run: %s\n"),
618 "zfs allow|unallow");
620 gettext("\nFor further help on a command or topic, "
621 "run: %s\n"), "zfs help [<topic>]");
625 * See comments at end of main().
627 if (getenv("ZFS_ABORT") != NULL
) {
628 (void) printf("dumping core by request\n");
632 exit(requested
? 0 : 2);
636 * Take a property=value argument string and add it to the given nvlist.
637 * Modifies the argument inplace.
640 parseprop(nvlist_t
*props
, char *propname
)
644 if ((propval
= strchr(propname
, '=')) == NULL
) {
645 (void) fprintf(stderr
, gettext("missing "
646 "'=' for property=value argument\n"));
651 if (nvlist_exists(props
, propname
)) {
652 (void) fprintf(stderr
, gettext("property '%s' "
653 "specified multiple times\n"), propname
);
656 if (nvlist_add_string(props
, propname
, propval
) != 0)
662 * Take a property name argument and add it to the given nvlist.
663 * Modifies the argument inplace.
666 parsepropname(nvlist_t
*props
, char *propname
)
668 if (strchr(propname
, '=') != NULL
) {
669 (void) fprintf(stderr
, gettext("invalid character "
670 "'=' in property argument\n"));
673 if (nvlist_exists(props
, propname
)) {
674 (void) fprintf(stderr
, gettext("property '%s' "
675 "specified multiple times\n"), propname
);
678 if (nvlist_add_boolean(props
, propname
) != 0)
684 parse_depth(char *opt
, int *flags
)
689 depth
= (int)strtol(opt
, &tmp
, 0);
691 (void) fprintf(stderr
,
692 gettext("%s is not an integer\n"), optarg
);
696 (void) fprintf(stderr
,
697 gettext("Depth can not be negative.\n"));
700 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
704 #define PROGRESS_DELAY 2 /* seconds */
706 static const char *pt_reverse
=
707 "\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";
708 static time_t pt_begin
;
709 static char *pt_header
= NULL
;
710 static boolean_t pt_shown
;
713 start_progress_timer(void)
715 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
720 set_progress_header(const char *header
)
722 assert(pt_header
== NULL
);
723 pt_header
= safe_strdup(header
);
725 (void) printf("%s: ", header
);
726 (void) fflush(stdout
);
731 update_progress(const char *update
)
733 if (!pt_shown
&& time(NULL
) > pt_begin
) {
734 int len
= strlen(update
);
736 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
738 (void) fflush(stdout
);
740 } else if (pt_shown
) {
741 int len
= strlen(update
);
743 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
744 (void) fflush(stdout
);
749 finish_progress(const char *done
)
753 (void) fflush(stdout
);
760 zfs_mount_and_share(libzfs_handle_t
*hdl
, const char *dataset
, zfs_type_t type
)
762 zfs_handle_t
*zhp
= NULL
;
765 zhp
= zfs_open(hdl
, dataset
, type
);
770 * Volumes may neither be mounted or shared. Potentially in the
771 * future filesystems detected on these volumes could be mounted.
773 if (zfs_get_type(zhp
) == ZFS_TYPE_VOLUME
) {
779 * Mount and/or share the new filesystem as appropriate. We provide a
780 * verbose error message to let the user know that their filesystem was
781 * in fact created, even if we failed to mount or share it.
783 * If the user doesn't want the dataset automatically mounted, then
784 * skip the mount/share step
786 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
, B_FALSE
) &&
787 zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
) {
788 if (zfs_mount_delegation_check()) {
789 (void) fprintf(stderr
, gettext("filesystem "
790 "successfully created, but it may only be "
791 "mounted by root\n"));
793 } else if (zfs_mount(zhp
, NULL
, 0) != 0) {
794 (void) fprintf(stderr
, gettext("filesystem "
795 "successfully created, but not mounted\n"));
797 } else if (zfs_share(zhp
, NULL
) != 0) {
798 (void) fprintf(stderr
, gettext("filesystem "
799 "successfully created, but not shared\n"));
802 zfs_commit_shares(NULL
);
811 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
813 * Given an existing dataset, create a writable copy whose initial contents
814 * are the same as the source. The newly created dataset maintains a
815 * dependency on the original; the original cannot be destroyed so long as
818 * The '-p' flag creates all the non-existing ancestors of the target first.
821 zfs_do_clone(int argc
, char **argv
)
823 zfs_handle_t
*zhp
= NULL
;
824 boolean_t parents
= B_FALSE
;
829 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
833 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
836 if (!parseprop(props
, optarg
)) {
845 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
854 /* check number of arguments */
856 (void) fprintf(stderr
, gettext("missing source dataset "
861 (void) fprintf(stderr
, gettext("missing target dataset "
866 (void) fprintf(stderr
, gettext("too many arguments\n"));
870 /* open the source dataset */
871 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
) {
876 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
879 * Now create the ancestors of the target dataset. If the
880 * target already exists and '-p' option was used we should not
883 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
889 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
897 ret
= zfs_clone(zhp
, argv
[1], props
);
899 /* create the mountpoint if necessary */
902 (void) zpool_log_history(g_zfs
, history_str
);
903 log_history
= B_FALSE
;
906 ret
= zfs_mount_and_share(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
915 ASSERT3P(zhp
, ==, NULL
);
922 * Return a default volblocksize for the pool which always uses more than
923 * half of the data sectors. This primarily applies to dRAID which always
924 * writes full stripe widths.
927 default_volblocksize(zpool_handle_t
*zhp
, nvlist_t
*props
)
929 uint64_t volblocksize
, asize
= SPA_MINBLOCKSIZE
;
930 nvlist_t
*tree
, **vdevs
;
933 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
935 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
) != 0 ||
936 nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
,
937 &vdevs
, &nvdevs
) != 0) {
938 return (ZVOL_DEFAULT_BLOCKSIZE
);
941 for (int i
= 0; i
< nvdevs
; i
++) {
942 nvlist_t
*nv
= vdevs
[i
];
943 uint64_t ashift
, ndata
, nparity
;
945 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASHIFT
, &ashift
) != 0)
948 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DRAID_NDATA
,
950 /* dRAID minimum allocation width */
951 asize
= MAX(asize
, ndata
* (1ULL << ashift
));
952 } else if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
954 /* raidz minimum allocation width */
956 asize
= MAX(asize
, 2 * (1ULL << ashift
));
958 asize
= MAX(asize
, 4 * (1ULL << ashift
));
960 /* mirror or (non-redundant) leaf vdev */
961 asize
= MAX(asize
, 1ULL << ashift
);
966 * Calculate the target volblocksize such that more than half
967 * of the asize is used. The following table is for 4k sectors.
969 * n asize blksz used | n asize blksz used
970 * -------------------------+---------------------------------
971 * 1 4,096 8,192 100% | 9 36,864 32,768 88%
972 * 2 8,192 8,192 100% | 10 40,960 32,768 80%
973 * 3 12,288 8,192 66% | 11 45,056 32,768 72%
974 * 4 16,384 16,384 100% | 12 49,152 32,768 66%
975 * 5 20,480 16,384 80% | 13 53,248 32,768 61%
976 * 6 24,576 16,384 66% | 14 57,344 32,768 57%
977 * 7 28,672 16,384 57% | 15 61,440 32,768 53%
978 * 8 32,768 32,768 100% | 16 65,536 65,636 100%
980 * This is primarily a concern for dRAID which always allocates
981 * a full stripe width. For dRAID the default stripe width is
982 * n=8 in which case the volblocksize is set to 32k. Ignoring
983 * compression there are no unused sectors. This same reasoning
984 * applies to raidz[2,3] so target 4 sectors to minimize waste.
986 uint64_t tgt_volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
987 while (tgt_volblocksize
* 2 <= asize
)
988 tgt_volblocksize
*= 2;
990 const char *prop
= zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
);
991 if (nvlist_lookup_uint64(props
, prop
, &volblocksize
) == 0) {
993 /* Issue a warning when a non-optimal size is requested. */
994 if (volblocksize
< ZVOL_DEFAULT_BLOCKSIZE
) {
995 (void) fprintf(stderr
, gettext("Warning: "
996 "volblocksize (%llu) is less than the default "
997 "minimum block size (%llu).\nTo reduce wasted "
998 "space a volblocksize of %llu is recommended.\n"),
999 (u_longlong_t
)volblocksize
,
1000 (u_longlong_t
)ZVOL_DEFAULT_BLOCKSIZE
,
1001 (u_longlong_t
)tgt_volblocksize
);
1002 } else if (volblocksize
< tgt_volblocksize
) {
1003 (void) fprintf(stderr
, gettext("Warning: "
1004 "volblocksize (%llu) is much less than the "
1005 "minimum allocation\nunit (%llu), which wastes "
1006 "at least %llu%% of space. To reduce wasted "
1007 "space,\nuse a larger volblocksize (%llu is "
1008 "recommended), fewer dRAID data disks\n"
1009 "per group, or smaller sector size (ashift).\n"),
1010 (u_longlong_t
)volblocksize
, (u_longlong_t
)asize
,
1011 (u_longlong_t
)((100 * (asize
- volblocksize
)) /
1012 asize
), (u_longlong_t
)tgt_volblocksize
);
1015 volblocksize
= tgt_volblocksize
;
1016 fnvlist_add_uint64(props
, prop
, volblocksize
);
1019 return (volblocksize
);
1023 * zfs create [-Pnpv] [-o prop=value] ... fs
1024 * zfs create [-Pnpsv] [-b blocksize] [-o prop=value] ... -V vol size
1026 * Create a new dataset. This command can be used to create filesystems
1027 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
1028 * For volumes, the user must specify a size to be used.
1030 * The '-s' flag applies only to volumes, and indicates that we should not try
1031 * to set the reservation for this volume. By default we set a reservation
1032 * equal to the size for any volume. For pools with SPA_VERSION >=
1033 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
1035 * The '-p' flag creates all the non-existing ancestors of the target first.
1037 * The '-n' flag is no-op (dry run) mode. This will perform a user-space sanity
1038 * check of arguments and properties, but does not check for permissions,
1039 * available space, etc.
1041 * The '-u' flag prevents the newly created file system from being mounted.
1043 * The '-v' flag is for verbose output.
1045 * The '-P' flag is used for parseable output. It implies '-v'.
1048 zfs_do_create(int argc
, char **argv
)
1050 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
1051 zpool_handle_t
*zpool_handle
= NULL
;
1052 nvlist_t
*real_props
= NULL
;
1053 uint64_t volsize
= 0;
1055 boolean_t noreserve
= B_FALSE
;
1056 boolean_t bflag
= B_FALSE
;
1057 boolean_t parents
= B_FALSE
;
1058 boolean_t dryrun
= B_FALSE
;
1059 boolean_t nomount
= B_FALSE
;
1060 boolean_t verbose
= B_FALSE
;
1061 boolean_t parseable
= B_FALSE
;
1067 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
1071 while ((c
= getopt(argc
, argv
, ":PV:b:nso:puv")) != -1) {
1074 type
= ZFS_TYPE_VOLUME
;
1075 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
1076 (void) fprintf(stderr
, gettext("bad volume "
1077 "size '%s': %s\n"), optarg
,
1078 libzfs_error_description(g_zfs
));
1082 if (nvlist_add_uint64(props
,
1083 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
1096 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
1097 (void) fprintf(stderr
, gettext("bad volume "
1098 "block size '%s': %s\n"), optarg
,
1099 libzfs_error_description(g_zfs
));
1103 if (nvlist_add_uint64(props
,
1104 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1112 if (!parseprop(props
, optarg
))
1125 (void) fprintf(stderr
, gettext("missing size "
1129 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1135 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
1136 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
1137 "used when creating a volume\n"));
1140 if (nomount
&& type
!= ZFS_TYPE_FILESYSTEM
) {
1141 (void) fprintf(stderr
, gettext("'-u' can only be "
1142 "used when creating a filesystem\n"));
1149 /* check number of arguments */
1151 (void) fprintf(stderr
, gettext("missing %s argument\n"),
1152 zfs_type_to_name(type
));
1156 (void) fprintf(stderr
, gettext("too many arguments\n"));
1160 if (dryrun
|| type
== ZFS_TYPE_VOLUME
) {
1161 char msg
[ZFS_MAX_DATASET_NAME_LEN
* 2];
1164 if ((p
= strchr(argv
[0], '/')) != NULL
)
1166 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
1169 if (zpool_handle
== NULL
)
1172 (void) snprintf(msg
, sizeof (msg
),
1173 dryrun
? gettext("cannot verify '%s'") :
1174 gettext("cannot create '%s'"), argv
[0]);
1175 if (props
&& (real_props
= zfs_valid_proplist(g_zfs
, type
,
1176 props
, 0, NULL
, zpool_handle
, B_TRUE
, msg
)) == NULL
) {
1177 zpool_close(zpool_handle
);
1182 if (type
== ZFS_TYPE_VOLUME
) {
1183 const char *prop
= zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
);
1184 uint64_t volblocksize
= default_volblocksize(zpool_handle
,
1187 if (volblocksize
!= ZVOL_DEFAULT_BLOCKSIZE
&&
1188 nvlist_lookup_string(props
, prop
, &strval
) != 0) {
1190 if (asprintf(&tmp
, "%llu",
1191 (u_longlong_t
)volblocksize
) == -1)
1193 nvlist_add_string(props
, prop
, tmp
);
1198 * If volsize is not a multiple of volblocksize, round it
1199 * up to the nearest multiple of the volblocksize.
1201 if (volsize
% volblocksize
) {
1202 volsize
= P2ROUNDUP_TYPED(volsize
, volblocksize
,
1205 if (nvlist_add_uint64(props
,
1206 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), volsize
) != 0) {
1213 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
1214 uint64_t spa_version
;
1215 zfs_prop_t resv_prop
;
1217 spa_version
= zpool_get_prop_int(zpool_handle
,
1218 ZPOOL_PROP_VERSION
, NULL
);
1219 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
1220 resv_prop
= ZFS_PROP_REFRESERVATION
;
1222 resv_prop
= ZFS_PROP_RESERVATION
;
1224 volsize
= zvol_volsize_to_reservation(zpool_handle
, volsize
,
1227 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
1229 if (nvlist_add_uint64(props
,
1230 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
1236 if (zpool_handle
!= NULL
) {
1237 zpool_close(zpool_handle
);
1238 nvlist_free(real_props
);
1241 if (parents
&& zfs_name_valid(argv
[0], type
)) {
1243 * Now create the ancestors of target dataset. If the target
1244 * already exists and '-p' option was used we should not
1247 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
1252 (void) printf(parseable
? "create_ancestors\t%s\n" :
1253 dryrun
? "would create ancestors of %s\n" :
1254 "create ancestors of %s\n", argv
[0]);
1257 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0) {
1264 nvpair_t
*nvp
= NULL
;
1265 (void) printf(parseable
? "create\t%s\n" :
1266 dryrun
? "would create %s\n" : "create %s\n", argv
[0]);
1267 while ((nvp
= nvlist_next_nvpair(props
, nvp
)) != NULL
) {
1271 switch (nvpair_type(nvp
)) {
1272 case DATA_TYPE_UINT64
:
1273 VERIFY0(nvpair_value_uint64(nvp
, &uval
));
1274 (void) printf(parseable
?
1275 "property\t%s\t%llu\n" : "\t%s=%llu\n",
1276 nvpair_name(nvp
), (u_longlong_t
)uval
);
1278 case DATA_TYPE_STRING
:
1279 VERIFY0(nvpair_value_string(nvp
, &sval
));
1280 (void) printf(parseable
?
1281 "property\t%s\t%s\n" : "\t%s=%s\n",
1282 nvpair_name(nvp
), sval
);
1285 (void) fprintf(stderr
, "property '%s' "
1286 "has illegal type %d\n",
1287 nvpair_name(nvp
), nvpair_type(nvp
));
1297 /* pass to libzfs */
1298 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
1302 (void) zpool_log_history(g_zfs
, history_str
);
1303 log_history
= B_FALSE
;
1311 ret
= zfs_mount_and_share(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
1322 * zfs destroy [-rRf] <fs, vol>
1323 * zfs destroy [-rRd] <snap>
1325 * -r Recursively destroy all children
1326 * -R Recursively destroy all dependents, including clones
1327 * -f Force unmounting of any dependents
1328 * -d If we can't destroy now, mark for deferred destruction
1330 * Destroys the given dataset. By default, it will unmount any filesystems,
1331 * and refuse to destroy a dataset that has any dependents. A dependent can
1332 * either be a child, or a clone of a child.
1334 typedef struct destroy_cbdata
{
1337 boolean_t cb_recurse
;
1339 boolean_t cb_doclones
;
1340 zfs_handle_t
*cb_target
;
1341 boolean_t cb_defer_destroy
;
1342 boolean_t cb_verbose
;
1343 boolean_t cb_parsable
;
1344 boolean_t cb_dryrun
;
1346 nvlist_t
*cb_batchedsnaps
;
1348 /* first snap in contiguous run */
1350 /* previous snap in contiguous run */
1352 int64_t cb_snapused
;
1355 uint64_t cb_snap_count
;
1359 * Check for any dependents based on the '-r' or '-R' flags.
1362 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
1364 destroy_cbdata_t
*cbp
= data
;
1365 const char *tname
= zfs_get_name(cbp
->cb_target
);
1366 const char *name
= zfs_get_name(zhp
);
1368 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
1369 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
1371 * This is a direct descendant, not a clone somewhere else in
1374 if (cbp
->cb_recurse
)
1377 if (cbp
->cb_first
) {
1378 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1379 "%s has children\n"),
1380 zfs_get_name(cbp
->cb_target
),
1381 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1382 (void) fprintf(stderr
, gettext("use '-r' to destroy "
1383 "the following datasets:\n"));
1384 cbp
->cb_first
= B_FALSE
;
1385 cbp
->cb_error
= B_TRUE
;
1388 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1391 * This is a clone. We only want to report this if the '-r'
1392 * wasn't specified, or the target is a snapshot.
1394 if (!cbp
->cb_recurse
&&
1395 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
1398 if (cbp
->cb_first
) {
1399 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1400 "%s has dependent clones\n"),
1401 zfs_get_name(cbp
->cb_target
),
1402 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1403 (void) fprintf(stderr
, gettext("use '-R' to destroy "
1404 "the following datasets:\n"));
1405 cbp
->cb_first
= B_FALSE
;
1406 cbp
->cb_error
= B_TRUE
;
1407 cbp
->cb_dryrun
= B_TRUE
;
1410 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1419 destroy_batched(destroy_cbdata_t
*cb
)
1421 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1422 cb
->cb_batchedsnaps
, B_FALSE
);
1423 fnvlist_free(cb
->cb_batchedsnaps
);
1424 cb
->cb_batchedsnaps
= fnvlist_alloc();
1429 destroy_callback(zfs_handle_t
*zhp
, void *data
)
1431 destroy_cbdata_t
*cb
= data
;
1432 const char *name
= zfs_get_name(zhp
);
1435 if (cb
->cb_verbose
) {
1436 if (cb
->cb_parsable
) {
1437 (void) printf("destroy\t%s\n", name
);
1438 } else if (cb
->cb_dryrun
) {
1439 (void) printf(gettext("would destroy %s\n"),
1442 (void) printf(gettext("will destroy %s\n"),
1448 * Ignore pools (which we've already flagged as an error before getting
1451 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1452 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1456 if (cb
->cb_dryrun
) {
1462 * We batch up all contiguous snapshots (even of different
1463 * filesystems) and destroy them with one ioctl. We can't
1464 * simply do all snap deletions and then all fs deletions,
1465 * because we must delete a clone before its origin.
1467 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1468 cb
->cb_snap_count
++;
1469 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1470 if (cb
->cb_snap_count
% 10 == 0 && cb
->cb_defer_destroy
) {
1471 error
= destroy_batched(cb
);
1478 error
= destroy_batched(cb
);
1480 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1481 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1484 * When performing a recursive destroy we ignore errors
1485 * so that the recursive destroy could continue
1486 * destroying past problem datasets
1488 if (cb
->cb_recurse
) {
1489 cb
->cb_error
= B_TRUE
;
1501 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1503 destroy_cbdata_t
*cb
= arg
;
1504 const char *name
= zfs_get_name(zhp
);
1507 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1508 if (cb
->cb_firstsnap
== NULL
)
1509 cb
->cb_firstsnap
= strdup(name
);
1510 if (cb
->cb_prevsnap
!= NULL
)
1511 free(cb
->cb_prevsnap
);
1512 /* this snap continues the current range */
1513 cb
->cb_prevsnap
= strdup(name
);
1514 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1516 if (cb
->cb_verbose
) {
1517 if (cb
->cb_parsable
) {
1518 (void) printf("destroy\t%s\n", name
);
1519 } else if (cb
->cb_dryrun
) {
1520 (void) printf(gettext("would destroy %s\n"),
1523 (void) printf(gettext("will destroy %s\n"),
1527 } else if (cb
->cb_firstsnap
!= NULL
) {
1528 /* end of this range */
1530 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1531 cb
->cb_prevsnap
, &used
);
1532 cb
->cb_snapused
+= used
;
1533 free(cb
->cb_firstsnap
);
1534 cb
->cb_firstsnap
= NULL
;
1535 free(cb
->cb_prevsnap
);
1536 cb
->cb_prevsnap
= NULL
;
1543 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1546 assert(cb
->cb_firstsnap
== NULL
);
1547 assert(cb
->cb_prevsnap
== NULL
);
1548 err
= zfs_iter_snapshots_sorted_v2(fs_zhp
, 0, destroy_print_cb
, cb
, 0,
1550 if (cb
->cb_firstsnap
!= NULL
) {
1553 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1554 cb
->cb_prevsnap
, &used
);
1556 cb
->cb_snapused
+= used
;
1557 free(cb
->cb_firstsnap
);
1558 cb
->cb_firstsnap
= NULL
;
1559 free(cb
->cb_prevsnap
);
1560 cb
->cb_prevsnap
= NULL
;
1566 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1568 destroy_cbdata_t
*cb
= arg
;
1571 /* Check for clones. */
1572 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1573 cb
->cb_target
= zhp
;
1574 cb
->cb_first
= B_TRUE
;
1575 err
= zfs_iter_dependents_v2(zhp
, 0, B_TRUE
,
1576 destroy_check_dependent
, cb
);
1580 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1588 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1590 destroy_cbdata_t
*cb
= arg
;
1593 err
= zfs_iter_snapspec_v2(zhp
, 0, cb
->cb_snapspec
,
1594 snapshot_to_nvl_cb
, cb
);
1600 if (cb
->cb_verbose
) {
1601 err
= destroy_print_snapshots(zhp
, cb
);
1607 err
= zfs_iter_filesystems_v2(zhp
, 0, gather_snapshots
, cb
);
1615 destroy_clones(destroy_cbdata_t
*cb
)
1618 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1620 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1621 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1624 boolean_t defer
= cb
->cb_defer_destroy
;
1628 * We can't defer destroy non-snapshots, so set it to
1629 * false while destroying the clones.
1631 cb
->cb_defer_destroy
= B_FALSE
;
1632 err
= zfs_iter_dependents_v2(zhp
, 0, B_FALSE
,
1633 destroy_callback
, cb
);
1634 cb
->cb_defer_destroy
= defer
;
1644 zfs_do_destroy(int argc
, char **argv
)
1646 destroy_cbdata_t cb
= { 0 };
1650 zfs_handle_t
*zhp
= NULL
;
1652 zfs_type_t type
= ZFS_TYPE_DATASET
;
1655 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1658 cb
.cb_verbose
= B_TRUE
;
1661 cb
.cb_verbose
= B_TRUE
;
1662 cb
.cb_parsable
= B_TRUE
;
1665 cb
.cb_dryrun
= B_TRUE
;
1668 cb
.cb_defer_destroy
= B_TRUE
;
1669 type
= ZFS_TYPE_SNAPSHOT
;
1672 cb
.cb_force
= B_TRUE
;
1675 cb
.cb_recurse
= B_TRUE
;
1678 cb
.cb_recurse
= B_TRUE
;
1679 cb
.cb_doclones
= B_TRUE
;
1683 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1692 /* check number of arguments */
1694 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1698 (void) fprintf(stderr
, gettext("too many arguments\n"));
1702 at
= strchr(argv
[0], '@');
1703 pound
= strchr(argv
[0], '#');
1706 /* Build the list of snaps to destroy in cb_nvl. */
1707 cb
.cb_nvl
= fnvlist_alloc();
1710 zhp
= zfs_open(g_zfs
, argv
[0],
1711 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1713 nvlist_free(cb
.cb_nvl
);
1717 cb
.cb_snapspec
= at
+ 1;
1718 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1724 if (nvlist_empty(cb
.cb_nvl
)) {
1725 (void) fprintf(stderr
, gettext("could not find any "
1726 "snapshots to destroy; check snapshot names.\n"));
1731 if (cb
.cb_verbose
) {
1733 zfs_nicebytes(cb
.cb_snapused
, buf
, sizeof (buf
));
1734 if (cb
.cb_parsable
) {
1735 (void) printf("reclaim\t%llu\n",
1736 (u_longlong_t
)cb
.cb_snapused
);
1737 } else if (cb
.cb_dryrun
) {
1738 (void) printf(gettext("would reclaim %s\n"),
1741 (void) printf(gettext("will reclaim %s\n"),
1746 if (!cb
.cb_dryrun
) {
1747 if (cb
.cb_doclones
) {
1748 cb
.cb_batchedsnaps
= fnvlist_alloc();
1749 err
= destroy_clones(&cb
);
1751 err
= zfs_destroy_snaps_nvl(g_zfs
,
1752 cb
.cb_batchedsnaps
, B_FALSE
);
1760 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1761 cb
.cb_defer_destroy
);
1767 } else if (pound
!= NULL
) {
1772 (void) fprintf(stderr
,
1773 "dryrun is not supported with bookmark\n");
1777 if (cb
.cb_defer_destroy
) {
1778 (void) fprintf(stderr
,
1779 "defer destroy is not supported with bookmark\n");
1783 if (cb
.cb_recurse
) {
1784 (void) fprintf(stderr
,
1785 "recursive is not supported with bookmark\n");
1790 * Unfortunately, zfs_bookmark() doesn't honor the
1791 * casesensitivity setting. However, we can't simply
1792 * remove this check, because lzc_destroy_bookmarks()
1793 * ignores non-existent bookmarks, so this is necessary
1794 * to get a proper error message.
1796 if (!zfs_bookmark_exists(argv
[0])) {
1797 (void) fprintf(stderr
, gettext("bookmark '%s' "
1798 "does not exist.\n"), argv
[0]);
1802 nvl
= fnvlist_alloc();
1803 fnvlist_add_boolean(nvl
, argv
[0]);
1805 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1807 (void) zfs_standard_error(g_zfs
, err
,
1808 "cannot destroy bookmark");
1815 /* Open the given dataset */
1816 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1822 * Perform an explicit check for pools before going any further.
1824 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1825 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1826 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1827 "operation does not apply to pools\n"),
1829 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1830 "%s' to destroy all datasets in the pool\n"),
1832 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1833 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1839 * Check for any dependents and/or clones.
1841 cb
.cb_first
= B_TRUE
;
1842 if (!cb
.cb_doclones
&& zfs_iter_dependents_v2(zhp
, 0, B_TRUE
,
1843 destroy_check_dependent
, &cb
) != 0) {
1852 cb
.cb_batchedsnaps
= fnvlist_alloc();
1853 if (zfs_iter_dependents_v2(zhp
, 0, B_FALSE
, destroy_callback
,
1860 * Do the real thing. The callback will close the
1861 * handle regardless of whether it succeeds or not.
1863 err
= destroy_callback(zhp
, &cb
);
1866 err
= zfs_destroy_snaps_nvl(g_zfs
,
1867 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1869 if (err
!= 0 || cb
.cb_error
== B_TRUE
)
1874 fnvlist_free(cb
.cb_batchedsnaps
);
1875 fnvlist_free(cb
.cb_nvl
);
1882 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1885 zfs_get_column_t col
;
1887 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1888 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1889 if (col
== GET_COL_RECVD
)
1895 * Generates an nvlist with output version for every command based on params.
1896 * Purpose of this is to add a version of JSON output, considering the schema
1897 * format might be updated for each command in future.
1901 * "output_version": {
1902 * "command": string,
1903 * "vers_major": integer,
1904 * "vers_minor": integer,
1908 zfs_json_schema(int maj_v
, int min_v
)
1910 nvlist_t
*sch
= NULL
;
1911 nvlist_t
*ov
= NULL
;
1912 char cmd
[MAX_CMD_LEN
];
1913 snprintf(cmd
, MAX_CMD_LEN
, "zfs %s", current_command
->name
);
1915 sch
= fnvlist_alloc();
1916 ov
= fnvlist_alloc();
1917 fnvlist_add_string(ov
, "command", cmd
);
1918 fnvlist_add_uint32(ov
, "vers_major", maj_v
);
1919 fnvlist_add_uint32(ov
, "vers_minor", min_v
);
1920 fnvlist_add_nvlist(sch
, "output_version", ov
);
1926 fill_dataset_info(nvlist_t
*list
, zfs_handle_t
*zhp
, boolean_t as_int
)
1928 char createtxg
[ZFS_MAXPROPLEN
];
1929 zfs_type_t type
= zfs_get_type(zhp
);
1930 nvlist_add_string(list
, "name", zfs_get_name(zhp
));
1933 case ZFS_TYPE_FILESYSTEM
:
1934 fnvlist_add_string(list
, "type", "FILESYSTEM");
1936 case ZFS_TYPE_VOLUME
:
1937 fnvlist_add_string(list
, "type", "VOLUME");
1939 case ZFS_TYPE_SNAPSHOT
:
1940 fnvlist_add_string(list
, "type", "SNAPSHOT");
1943 fnvlist_add_string(list
, "type", "POOL");
1945 case ZFS_TYPE_BOOKMARK
:
1946 fnvlist_add_string(list
, "type", "BOOKMARK");
1949 fnvlist_add_string(list
, "type", "UNKNOWN");
1953 if (type
!= ZFS_TYPE_POOL
)
1954 fnvlist_add_string(list
, "pool", zfs_get_pool_name(zhp
));
1957 fnvlist_add_uint64(list
, "createtxg", zfs_prop_get_int(zhp
,
1958 ZFS_PROP_CREATETXG
));
1960 if (zfs_prop_get(zhp
, ZFS_PROP_CREATETXG
, createtxg
,
1961 sizeof (createtxg
), NULL
, NULL
, 0, B_TRUE
) == 0)
1962 fnvlist_add_string(list
, "createtxg", createtxg
);
1965 if (type
== ZFS_TYPE_SNAPSHOT
) {
1967 ds
= snap
= strdup(zfs_get_name(zhp
));
1968 ds
= strsep(&snap
, "@");
1969 fnvlist_add_string(list
, "dataset", ds
);
1970 fnvlist_add_string(list
, "snapshot_name", snap
);
1976 * zfs get [-rHp] [-j [--json-int]] [-o all | field[,field]...]
1977 * [-s source[,source]...]
1978 * < all | property[,property]... > < fs | snap | vol > ...
1980 * -r recurse over any child datasets
1981 * -H scripted mode. Headers are stripped, and fields are separated
1982 * by tabs instead of spaces.
1983 * -o Set of fields to display. One of "name,property,value,
1984 * received,source". Default is "name,property,value,source".
1985 * "all" is an alias for all five.
1986 * -s Set of sources to allow. One of
1987 * "local,default,inherited,received,temporary,none". Default is
1989 * -p Display values in parsable (literal) format.
1990 * -j Display output in JSON format.
1991 * --json-int Display numbers as integers instead of strings.
1993 * Prints properties for the given datasets. The user can control which
1994 * columns to display as well as which property types to allow.
1998 * Invoked to display the properties for a single dataset.
2001 get_callback(zfs_handle_t
*zhp
, void *data
)
2003 char buf
[ZFS_MAXPROPLEN
];
2004 char rbuf
[ZFS_MAXPROPLEN
];
2005 zprop_source_t sourcetype
;
2006 char source
[ZFS_MAX_DATASET_NAME_LEN
];
2007 zprop_get_cbdata_t
*cbp
= data
;
2008 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
2009 zprop_list_t
*pl
= cbp
->cb_proplist
;
2011 nvlist_t
*item
, *d
, *props
;
2012 item
= d
= props
= NULL
;
2014 const char *sourceval
;
2015 boolean_t received
= is_recvd_column(cbp
);
2019 d
= fnvlist_lookup_nvlist(cbp
->cb_jsobj
, "datasets");
2021 fprintf(stderr
, "datasets obj not found.\n");
2024 props
= fnvlist_alloc();
2027 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2028 char *recvdval
= NULL
;
2030 * Skip the special fake placeholder. This will also skip over
2031 * the name property when 'all' is specified.
2033 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
2034 pl
== cbp
->cb_proplist
)
2037 if (pl
->pl_prop
!= ZPROP_USERPROP
) {
2038 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
2039 sizeof (buf
), &sourcetype
, source
,
2041 cbp
->cb_literal
) != 0) {
2044 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
2045 ZFS_TYPE_DATASET
, B_FALSE
)) {
2046 (void) fprintf(stderr
,
2047 gettext("No such property '%s'\n"),
2048 zfs_prop_to_name(pl
->pl_prop
));
2051 sourcetype
= ZPROP_SRC_NONE
;
2052 (void) strlcpy(buf
, "-", sizeof (buf
));
2055 if (received
&& (zfs_prop_get_recvd(zhp
,
2056 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
2057 cbp
->cb_literal
) == 0))
2060 err
= zprop_collect_property(zfs_get_name(zhp
), cbp
,
2061 zfs_prop_to_name(pl
->pl_prop
),
2062 buf
, sourcetype
, source
, recvdval
, props
);
2063 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
2064 sourcetype
= ZPROP_SRC_LOCAL
;
2066 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
2067 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
2068 sourcetype
= ZPROP_SRC_NONE
;
2069 (void) strlcpy(buf
, "-", sizeof (buf
));
2072 err
= zprop_collect_property(zfs_get_name(zhp
), cbp
,
2073 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
,
2075 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
2076 sourcetype
= ZPROP_SRC_LOCAL
;
2078 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
2079 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
2080 sourcetype
= ZPROP_SRC_NONE
;
2081 (void) strlcpy(buf
, "-", sizeof (buf
));
2084 err
= zprop_collect_property(zfs_get_name(zhp
), cbp
,
2085 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
,
2088 if (nvlist_lookup_nvlist(user_props
,
2089 pl
->pl_user_prop
, &propval
) != 0) {
2092 sourcetype
= ZPROP_SRC_NONE
;
2095 strval
= fnvlist_lookup_string(propval
,
2097 sourceval
= fnvlist_lookup_string(propval
,
2100 if (strcmp(sourceval
,
2101 zfs_get_name(zhp
)) == 0) {
2102 sourcetype
= ZPROP_SRC_LOCAL
;
2103 } else if (strcmp(sourceval
,
2104 ZPROP_SOURCE_VAL_RECVD
) == 0) {
2105 sourcetype
= ZPROP_SRC_RECEIVED
;
2107 sourcetype
= ZPROP_SRC_INHERITED
;
2108 (void) strlcpy(source
,
2109 sourceval
, sizeof (source
));
2113 if (received
&& (zfs_prop_get_recvd(zhp
,
2114 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
2115 cbp
->cb_literal
) == 0))
2118 err
= zprop_collect_property(zfs_get_name(zhp
), cbp
,
2119 pl
->pl_user_prop
, strval
, sourcetype
,
2120 source
, recvdval
, props
);
2127 if (!nvlist_empty(props
)) {
2128 item
= fnvlist_alloc();
2129 fill_dataset_info(item
, zhp
, cbp
->cb_json_as_int
);
2130 fnvlist_add_nvlist(item
, "properties", props
);
2131 fnvlist_add_nvlist(d
, zfs_get_name(zhp
), item
);
2132 fnvlist_free(props
);
2135 fnvlist_free(props
);
2143 zfs_do_get(int argc
, char **argv
)
2145 zprop_get_cbdata_t cb
= { 0 };
2146 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2147 int types
= ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
;
2151 zprop_list_t fake_name
= { 0 };
2155 * Set up default columns and sources.
2157 cb
.cb_sources
= ZPROP_SRC_ALL
;
2158 cb
.cb_columns
[0] = GET_COL_NAME
;
2159 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
2160 cb
.cb_columns
[2] = GET_COL_VALUE
;
2161 cb
.cb_columns
[3] = GET_COL_SOURCE
;
2162 cb
.cb_type
= ZFS_TYPE_DATASET
;
2164 struct option long_options
[] = {
2165 {"json", no_argument
, NULL
, 'j'},
2166 {"json-int", no_argument
, NULL
, ZFS_OPTION_JSON_NUMS_AS_INT
},
2171 while ((c
= getopt_long(argc
, argv
, ":d:o:s:jrt:Hp", long_options
,
2175 cb
.cb_literal
= B_TRUE
;
2178 limit
= parse_depth(optarg
, &flags
);
2181 flags
|= ZFS_ITER_RECURSE
;
2184 cb
.cb_scripted
= B_TRUE
;
2187 cb
.cb_json
= B_TRUE
;
2188 cb
.cb_jsobj
= zfs_json_schema(0, 1);
2189 data
= fnvlist_alloc();
2190 fnvlist_add_nvlist(cb
.cb_jsobj
, "datasets", data
);
2193 case ZFS_OPTION_JSON_NUMS_AS_INT
:
2194 cb
.cb_json_as_int
= B_TRUE
;
2195 cb
.cb_literal
= B_TRUE
;
2198 (void) fprintf(stderr
, gettext("missing argument for "
2199 "'%c' option\n"), optopt
);
2204 * Process the set of columns to display. We zero out
2205 * the structure to give us a blank slate.
2207 memset(&cb
.cb_columns
, 0, sizeof (cb
.cb_columns
));
2210 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
2211 static const char *const col_subopts
[] =
2212 { "name", "property", "value",
2213 "received", "source", "all" };
2214 static const zfs_get_column_t col_subopt_col
[] =
2215 { GET_COL_NAME
, GET_COL_PROPERTY
, GET_COL_VALUE
,
2216 GET_COL_RECVD
, GET_COL_SOURCE
};
2217 static const int col_subopt_flags
[] =
2218 { 0, 0, 0, ZFS_ITER_RECVD_PROPS
, 0 };
2220 if (i
== ZFS_GET_NCOLS
) {
2221 (void) fprintf(stderr
, gettext("too "
2222 "many fields given to -o "
2227 for (c
= 0; c
< ARRAY_SIZE(col_subopts
); ++c
)
2228 if (strcmp(tok
, col_subopts
[c
]) == 0)
2231 (void) fprintf(stderr
,
2232 gettext("invalid column name '%s'\n"), tok
);
2238 (void) fprintf(stderr
,
2239 gettext("\"all\" conflicts "
2240 "with specific fields "
2241 "given to -o option\n"));
2245 memcpy(cb
.cb_columns
, col_subopt_col
,
2246 sizeof (col_subopt_col
));
2247 flags
|= ZFS_ITER_RECVD_PROPS
;
2250 cb
.cb_columns
[i
++] = col_subopt_col
[c
];
2251 flags
|= col_subopt_flags
[c
];
2259 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
2260 static const char *const source_opt
[] = {
2262 "inherited", "received",
2263 "temporary", "none" };
2264 static const int source_flg
[] = {
2265 ZPROP_SRC_LOCAL
, ZPROP_SRC_DEFAULT
,
2266 ZPROP_SRC_INHERITED
, ZPROP_SRC_RECEIVED
,
2267 ZPROP_SRC_TEMPORARY
, ZPROP_SRC_NONE
};
2269 for (i
= 0; i
< ARRAY_SIZE(source_opt
); ++i
)
2270 if (strcmp(tok
, source_opt
[i
]) == 0) {
2271 cb
.cb_sources
|= source_flg
[i
];
2275 (void) fprintf(stderr
,
2276 gettext("invalid source '%s'\n"), tok
);
2284 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
2286 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
2287 static const char *const type_opts
[] = {
2297 static const int type_types
[] = {
2298 ZFS_TYPE_FILESYSTEM
,
2299 ZFS_TYPE_FILESYSTEM
,
2305 ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
2308 for (i
= 0; i
< ARRAY_SIZE(type_opts
); ++i
)
2309 if (strcmp(tok
, type_opts
[i
]) == 0) {
2310 types
|= type_types
[i
];
2314 (void) fprintf(stderr
,
2315 gettext("invalid type '%s'\n"), tok
);
2321 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2331 (void) fprintf(stderr
, gettext("missing property "
2336 if (!cb
.cb_json
&& cb
.cb_json_as_int
) {
2337 (void) fprintf(stderr
, gettext("'--json-int' only works with"
2345 * Handle users who want to get all snapshots or bookmarks
2346 * of a dataset (ex. 'zfs get -t snapshot refer <dataset>').
2348 if ((types
== ZFS_TYPE_SNAPSHOT
|| types
== ZFS_TYPE_BOOKMARK
) &&
2349 argc
> 1 && (flags
& ZFS_ITER_RECURSE
) == 0 && limit
== 0) {
2350 flags
|= (ZFS_ITER_DEPTH_LIMIT
| ZFS_ITER_RECURSE
);
2354 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
2362 * As part of zfs_expand_proplist(), we keep track of the maximum column
2363 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
2364 * need to know the maximum name length. However, the user likely did
2365 * not specify 'name' as one of the properties to fetch, so we need to
2366 * make sure we always include at least this property for
2367 * print_get_headers() to work properly.
2369 if (cb
.cb_proplist
!= NULL
) {
2370 fake_name
.pl_prop
= ZFS_PROP_NAME
;
2371 fake_name
.pl_width
= strlen(gettext("NAME"));
2372 fake_name
.pl_next
= cb
.cb_proplist
;
2373 cb
.cb_proplist
= &fake_name
;
2376 cb
.cb_first
= B_TRUE
;
2378 /* run for each object */
2379 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
2380 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
2382 if (ret
== 0 && cb
.cb_json
)
2383 zcmd_print_json(cb
.cb_jsobj
);
2384 else if (ret
!= 0 && cb
.cb_json
)
2385 nvlist_free(cb
.cb_jsobj
);
2387 if (cb
.cb_proplist
== &fake_name
)
2388 zprop_free_list(fake_name
.pl_next
);
2390 zprop_free_list(cb
.cb_proplist
);
2396 * inherit [-rS] <property> <fs|vol> ...
2398 * -r Recurse over all children
2399 * -S Revert to received value, if any
2401 * For each dataset specified on the command line, inherit the given property
2402 * from its parent. Inheriting a property at the pool level will cause it to
2403 * use the default value. The '-r' flag will recurse over all children, and is
2404 * useful for setting a property on a hierarchy-wide basis, regardless of any
2405 * local modifications for each dataset.
2408 typedef struct inherit_cbdata
{
2409 const char *cb_propname
;
2410 boolean_t cb_received
;
2414 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
2416 inherit_cbdata_t
*cb
= data
;
2417 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
2420 * If we're doing it recursively, then ignore properties that
2421 * are not valid for this type of dataset.
2423 if (prop
!= ZPROP_INVAL
&&
2424 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
), B_FALSE
))
2427 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
2431 inherit_cb(zfs_handle_t
*zhp
, void *data
)
2433 inherit_cbdata_t
*cb
= data
;
2435 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
2439 zfs_do_inherit(int argc
, char **argv
)
2443 inherit_cbdata_t cb
= { 0 };
2447 boolean_t received
= B_FALSE
;
2450 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
2453 flags
|= ZFS_ITER_RECURSE
;
2460 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2469 /* check number of arguments */
2471 (void) fprintf(stderr
, gettext("missing property argument\n"));
2475 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
2483 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_USERPROP
) {
2484 if (zfs_prop_readonly(prop
)) {
2485 (void) fprintf(stderr
, gettext(
2486 "%s property is read-only\n"),
2490 if (!zfs_prop_inheritable(prop
) && !received
) {
2491 (void) fprintf(stderr
, gettext("'%s' property cannot "
2492 "be inherited\n"), propname
);
2493 if (prop
== ZFS_PROP_QUOTA
||
2494 prop
== ZFS_PROP_RESERVATION
||
2495 prop
== ZFS_PROP_REFQUOTA
||
2496 prop
== ZFS_PROP_REFRESERVATION
) {
2497 (void) fprintf(stderr
, gettext("use 'zfs set "
2498 "%s=none' to clear\n"), propname
);
2499 (void) fprintf(stderr
, gettext("use 'zfs "
2500 "inherit -S %s' to revert to received "
2501 "value\n"), propname
);
2505 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
2506 prop
== ZFS_PROP_VERSION
)) {
2507 (void) fprintf(stderr
, gettext("'%s' property cannot "
2508 "be reverted to a received value\n"), propname
);
2511 } else if (!zfs_prop_user(propname
)) {
2512 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
2517 cb
.cb_propname
= propname
;
2518 cb
.cb_received
= received
;
2520 if (flags
& ZFS_ITER_RECURSE
) {
2521 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
2522 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
2524 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
2525 NULL
, NULL
, 0, inherit_cb
, &cb
);
2531 typedef struct upgrade_cbdata
{
2532 uint64_t cb_numupgraded
;
2533 uint64_t cb_numsamegraded
;
2534 uint64_t cb_numfailed
;
2535 uint64_t cb_version
;
2537 boolean_t cb_foundone
;
2538 char cb_lastfs
[ZFS_MAX_DATASET_NAME_LEN
];
2542 same_pool(zfs_handle_t
*zhp
, const char *name
)
2544 int len1
= strcspn(name
, "/@");
2545 const char *zhname
= zfs_get_name(zhp
);
2546 int len2
= strcspn(zhname
, "/@");
2550 return (strncmp(name
, zhname
, len1
) == 0);
2554 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
2556 upgrade_cbdata_t
*cb
= data
;
2557 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2559 /* list if it's old/new */
2560 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
2561 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
2564 str
= gettext("The following filesystems are "
2565 "formatted using a newer software version and\n"
2566 "cannot be accessed on the current system.\n\n");
2568 str
= gettext("The following filesystems are "
2569 "out of date, and can be upgraded. After being\n"
2570 "upgraded, these filesystems (and any 'zfs send' "
2571 "streams generated from\n"
2572 "subsequent snapshots) will no longer be "
2573 "accessible by older software versions.\n\n");
2576 if (!cb
->cb_foundone
) {
2578 (void) printf(gettext("VER FILESYSTEM\n"));
2579 (void) printf(gettext("--- ------------\n"));
2580 cb
->cb_foundone
= B_TRUE
;
2583 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
2590 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2592 upgrade_cbdata_t
*cb
= data
;
2593 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2594 int needed_spa_version
;
2597 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2600 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2602 if (needed_spa_version
< 0)
2605 if (spa_version
< needed_spa_version
) {
2607 (void) printf(gettext("%s: can not be "
2608 "upgraded; the pool version needs to first "
2609 "be upgraded\nto version %d\n\n"),
2610 zfs_get_name(zhp
), needed_spa_version
);
2616 if (version
< cb
->cb_version
) {
2618 (void) snprintf(verstr
, sizeof (verstr
),
2619 "%llu", (u_longlong_t
)cb
->cb_version
);
2620 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2622 * If they did "zfs upgrade -a", then we could
2623 * be doing ioctls to different pools. We need
2624 * to log this history once to each pool, and bypass
2625 * the normal history logging that happens in main().
2627 (void) zpool_log_history(g_zfs
, history_str
);
2628 log_history
= B_FALSE
;
2630 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2631 cb
->cb_numupgraded
++;
2634 (void) strlcpy(cb
->cb_lastfs
, zfs_get_name(zhp
),
2635 sizeof (cb
->cb_lastfs
));
2636 } else if (version
> cb
->cb_version
) {
2637 /* can't downgrade */
2638 (void) printf(gettext("%s: can not be downgraded; "
2639 "it is already at version %u\n"),
2640 zfs_get_name(zhp
), version
);
2643 cb
->cb_numsamegraded
++;
2651 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2654 zfs_do_upgrade(int argc
, char **argv
)
2656 boolean_t all
= B_FALSE
;
2657 boolean_t showversions
= B_FALSE
;
2659 upgrade_cbdata_t cb
= { 0 };
2661 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2664 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2667 flags
|= ZFS_ITER_RECURSE
;
2670 showversions
= B_TRUE
;
2673 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2674 optarg
, &cb
.cb_version
) != 0) {
2675 (void) fprintf(stderr
,
2676 gettext("invalid version %s\n"), optarg
);
2685 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2694 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2696 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2697 cb
.cb_version
|| argc
))
2699 if ((all
|| argc
) && (showversions
))
2705 /* Show info on available versions. */
2706 (void) printf(gettext("The following filesystem versions are "
2708 (void) printf(gettext("VER DESCRIPTION\n"));
2709 (void) printf("--- -----------------------------------------"
2710 "---------------\n");
2711 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2712 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2713 (void) printf(gettext(" 3 Case insensitive and filesystem "
2714 "user identifier (FUID)\n"));
2715 (void) printf(gettext(" 4 userquota, groupquota "
2717 (void) printf(gettext(" 5 System attributes\n"));
2718 (void) printf(gettext("\nFor more information on a particular "
2719 "version, including supported releases,\n"));
2720 (void) printf("see the ZFS Administration Guide.\n\n");
2722 } else if (argc
|| all
) {
2723 /* Upgrade filesystems */
2724 if (cb
.cb_version
== 0)
2725 cb
.cb_version
= ZPL_VERSION
;
2726 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2727 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2728 (void) printf(gettext("%llu filesystems upgraded\n"),
2729 (u_longlong_t
)cb
.cb_numupgraded
);
2730 if (cb
.cb_numsamegraded
) {
2731 (void) printf(gettext("%llu filesystems already at "
2733 (u_longlong_t
)cb
.cb_numsamegraded
);
2735 if (cb
.cb_numfailed
!= 0)
2738 /* List old-version filesystems */
2740 (void) printf(gettext("This system is currently running "
2741 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2743 flags
|= ZFS_ITER_RECURSE
;
2744 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2745 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2747 found
= cb
.cb_foundone
;
2748 cb
.cb_foundone
= B_FALSE
;
2749 cb
.cb_newer
= B_TRUE
;
2751 ret
|= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2752 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2754 if (!cb
.cb_foundone
&& !found
) {
2755 (void) printf(gettext("All filesystems are "
2756 "formatted with the current version.\n"));
2764 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2765 * [-S field [-S field]...] [-t type[,...]]
2766 * filesystem | snapshot | path
2767 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2768 * [-S field [-S field]...] [-t type[,...]]
2769 * filesystem | snapshot | path
2770 * zfs projectspace [-Hp] [-o field[,...]] [-s field [-s field]...]
2771 * [-S field [-S field]...] filesystem | snapshot | path
2773 * -H Scripted mode; elide headers and separate columns by tabs.
2774 * -i Translate SID to POSIX ID.
2775 * -n Print numeric ID instead of user/group name.
2776 * -o Control which fields to display.
2777 * -p Use exact (parsable) numeric output.
2778 * -s Specify sort columns, descending order.
2779 * -S Specify sort columns, ascending order.
2780 * -t Control which object types to display.
2782 * Displays space consumed by, and quotas on, each user in the specified
2783 * filesystem or snapshot.
2786 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2787 enum us_field_types
{
2795 static const char *const us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA",
2796 "OBJUSED", "OBJQUOTA" };
2797 static const char *const us_field_names
[] = { "type", "name", "used", "quota",
2798 "objused", "objquota" };
2799 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2801 #define USTYPE_PSX_GRP (1 << 0)
2802 #define USTYPE_PSX_USR (1 << 1)
2803 #define USTYPE_SMB_GRP (1 << 2)
2804 #define USTYPE_SMB_USR (1 << 3)
2805 #define USTYPE_PROJ (1 << 4)
2806 #define USTYPE_ALL \
2807 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR | \
2810 static int us_type_bits
[] = {
2817 static const char *const us_type_names
[] = { "posixgroup", "posixuser",
2818 "smbgroup", "smbuser", "all" };
2820 typedef struct us_node
{
2822 uu_avl_node_t usn_avlnode
;
2823 uu_list_node_t usn_listnode
;
2826 typedef struct us_cbdata
{
2828 uu_avl_pool_t
*cb_avl_pool
;
2830 boolean_t cb_numname
;
2831 boolean_t cb_nicenum
;
2832 boolean_t cb_sid2posix
;
2833 zfs_userquota_prop_t cb_prop
;
2834 zfs_sort_column_t
*cb_sortcol
;
2835 size_t cb_width
[USFIELD_LAST
];
2838 static boolean_t us_populated
= B_FALSE
;
2841 zfs_sort_column_t
*si_sortcol
;
2842 boolean_t si_numname
;
2846 us_field_index(const char *field
)
2848 for (int i
= 0; i
< USFIELD_LAST
; i
++) {
2849 if (strcmp(field
, us_field_names
[i
]) == 0)
2857 us_compare(const void *larg
, const void *rarg
, void *unused
)
2859 const us_node_t
*l
= larg
;
2860 const us_node_t
*r
= rarg
;
2861 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2862 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2863 boolean_t numname
= si
->si_numname
;
2864 nvlist_t
*lnvl
= l
->usn_nvl
;
2865 nvlist_t
*rnvl
= r
->usn_nvl
;
2869 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2870 const char *lvstr
= "";
2871 const char *rvstr
= "";
2876 zfs_prop_t prop
= sortcol
->sc_prop
;
2877 const char *propname
= NULL
;
2878 boolean_t reverse
= sortcol
->sc_reverse
;
2883 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2884 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2886 rc
= (rv32
< lv32
) ? 1 : -1;
2892 (void) nvlist_lookup_uint64(lnvl
, propname
,
2894 (void) nvlist_lookup_uint64(rnvl
, propname
,
2897 rc
= (rv64
< lv64
) ? 1 : -1;
2899 if ((nvlist_lookup_string(lnvl
, propname
,
2900 &lvstr
) == ENOENT
) ||
2901 (nvlist_lookup_string(rnvl
, propname
,
2902 &rvstr
) == ENOENT
)) {
2905 rc
= strcmp(lvstr
, rvstr
);
2909 case ZFS_PROP_QUOTA
:
2912 if (prop
== ZFS_PROP_USED
)
2916 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2917 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2919 rc
= (rv64
< lv64
) ? 1 : -1;
2928 return (reverse
? 1 : -1);
2930 return (reverse
? -1 : 1);
2935 * If entries still seem to be the same, check if they are of the same
2936 * type (smbentity is added only if we are doing SID to POSIX ID
2937 * translation where we can have duplicate type/name combinations).
2939 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2940 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2942 return (lvb
< rvb
? -1 : 1);
2948 zfs_prop_is_user(unsigned p
)
2950 return (p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
||
2951 p
== ZFS_PROP_USEROBJUSED
|| p
== ZFS_PROP_USEROBJQUOTA
);
2955 zfs_prop_is_group(unsigned p
)
2957 return (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
||
2958 p
== ZFS_PROP_GROUPOBJUSED
|| p
== ZFS_PROP_GROUPOBJQUOTA
);
2962 zfs_prop_is_project(unsigned p
)
2964 return (p
== ZFS_PROP_PROJECTUSED
|| p
== ZFS_PROP_PROJECTQUOTA
||
2965 p
== ZFS_PROP_PROJECTOBJUSED
|| p
== ZFS_PROP_PROJECTOBJQUOTA
);
2968 static inline const char *
2969 us_type2str(unsigned field_type
)
2971 switch (field_type
) {
2972 case USTYPE_PSX_USR
:
2973 return ("POSIX User");
2974 case USTYPE_PSX_GRP
:
2975 return ("POSIX Group");
2976 case USTYPE_SMB_USR
:
2977 return ("SMB User");
2978 case USTYPE_SMB_GRP
:
2979 return ("SMB Group");
2983 return ("Undefined");
2988 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2990 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2991 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2993 const char *propname
;
2996 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2997 uu_avl_t
*avl
= cb
->cb_avl
;
3001 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
3003 const char *typestr
;
3007 int typeidx
, nameidx
, sizeidx
;
3008 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
3009 boolean_t smbentity
= B_FALSE
;
3011 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
3013 node
= safe_malloc(sizeof (us_node_t
));
3014 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
3015 node
->usn_nvl
= props
;
3017 if (domain
!= NULL
&& domain
[0] != '\0') {
3020 char sid
[MAXNAMELEN
+ 32];
3024 directory_error_t e
;
3028 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
3030 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
3031 type
= USTYPE_SMB_GRP
;
3032 err
= sid_to_id(sid
, B_FALSE
, &id
);
3034 type
= USTYPE_SMB_USR
;
3035 err
= sid_to_id(sid
, B_TRUE
, &id
);
3040 if (!cb
->cb_sid2posix
) {
3041 e
= directory_name_from_sid(NULL
, sid
, &name
,
3044 directory_error_free(e
);
3054 #endif /* HAVE_IDMAP */
3057 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
3059 if (zfs_prop_is_group(prop
)) {
3060 type
= USTYPE_PSX_GRP
;
3061 if (!cb
->cb_numname
) {
3064 if ((g
= getgrgid(rid
)) != NULL
)
3067 } else if (zfs_prop_is_user(prop
)) {
3068 type
= USTYPE_PSX_USR
;
3069 if (!cb
->cb_numname
) {
3072 if ((p
= getpwuid(rid
)) != NULL
)
3081 * Make sure that the type/name combination is unique when doing
3082 * SID to POSIX ID translation (hence changing the type from SMB to
3085 if (cb
->cb_sid2posix
&&
3086 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
3089 /* Calculate/update width of TYPE field */
3090 typestr
= us_type2str(type
);
3091 typelen
= strlen(gettext(typestr
));
3092 typeidx
= us_field_index("type");
3093 if (typelen
> cb
->cb_width
[typeidx
])
3094 cb
->cb_width
[typeidx
] = typelen
;
3095 if (nvlist_add_uint32(props
, "type", type
) != 0)
3098 /* Calculate/update width of NAME field */
3099 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
3100 if (nvlist_add_uint64(props
, "name", rid
) != 0)
3102 namelen
= snprintf(NULL
, 0, "%u", rid
);
3104 if (nvlist_add_string(props
, "name", name
) != 0)
3106 namelen
= strlen(name
);
3108 nameidx
= us_field_index("name");
3109 if (nameidx
>= 0 && namelen
> cb
->cb_width
[nameidx
])
3110 cb
->cb_width
[nameidx
] = namelen
;
3113 * Check if this type/name combination is in the list and update it;
3114 * otherwise add new node to the list.
3116 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
3117 uu_avl_insert(avl
, node
, idx
);
3122 props
= node
->usn_nvl
;
3125 /* Calculate/update width of USED/QUOTA fields */
3126 if (cb
->cb_nicenum
) {
3127 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
||
3128 prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
||
3129 prop
== ZFS_PROP_PROJECTUSED
||
3130 prop
== ZFS_PROP_PROJECTQUOTA
) {
3131 zfs_nicebytes(space
, sizebuf
, sizeof (sizebuf
));
3133 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
3136 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu",
3137 (u_longlong_t
)space
);
3139 sizelen
= strlen(sizebuf
);
3140 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
||
3141 prop
== ZFS_PROP_PROJECTUSED
) {
3143 if (!nvlist_exists(props
, "quota"))
3144 (void) nvlist_add_uint64(props
, "quota", 0);
3145 } else if (prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
||
3146 prop
== ZFS_PROP_PROJECTQUOTA
) {
3148 if (!nvlist_exists(props
, "used"))
3149 (void) nvlist_add_uint64(props
, "used", 0);
3150 } else if (prop
== ZFS_PROP_USEROBJUSED
||
3151 prop
== ZFS_PROP_GROUPOBJUSED
|| prop
== ZFS_PROP_PROJECTOBJUSED
) {
3152 propname
= "objused";
3153 if (!nvlist_exists(props
, "objquota"))
3154 (void) nvlist_add_uint64(props
, "objquota", 0);
3155 } else if (prop
== ZFS_PROP_USEROBJQUOTA
||
3156 prop
== ZFS_PROP_GROUPOBJQUOTA
||
3157 prop
== ZFS_PROP_PROJECTOBJQUOTA
) {
3158 propname
= "objquota";
3159 if (!nvlist_exists(props
, "objused"))
3160 (void) nvlist_add_uint64(props
, "objused", 0);
3164 sizeidx
= us_field_index(propname
);
3165 if (sizeidx
>= 0 && sizelen
> cb
->cb_width
[sizeidx
])
3166 cb
->cb_width
[sizeidx
] = sizelen
;
3168 if (nvlist_add_uint64(props
, propname
, space
) != 0)
3175 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
3176 size_t *width
, us_node_t
*node
)
3178 nvlist_t
*nvl
= node
->usn_nvl
;
3179 char valstr
[MAXNAMELEN
];
3180 boolean_t first
= B_TRUE
;
3186 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
3187 if (!(ustype
& types
))
3190 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
3191 nvpair_t
*nvp
= NULL
;
3193 uint32_t val32
= -1;
3194 uint64_t val64
= -1;
3195 const char *strval
= "-";
3197 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
)
3198 if (strcmp(nvpair_name(nvp
),
3199 us_field_names
[field
]) == 0)
3202 type
= nvp
== NULL
? DATA_TYPE_UNKNOWN
: nvpair_type(nvp
);
3204 case DATA_TYPE_UINT32
:
3205 val32
= fnvpair_value_uint32(nvp
);
3207 case DATA_TYPE_UINT64
:
3208 val64
= fnvpair_value_uint64(nvp
);
3210 case DATA_TYPE_STRING
:
3211 strval
= fnvpair_value_string(nvp
);
3213 case DATA_TYPE_UNKNOWN
:
3216 (void) fprintf(stderr
, "invalid data type\n");
3221 if (type
== DATA_TYPE_UINT32
)
3222 strval
= us_type2str(val32
);
3225 if (type
== DATA_TYPE_UINT64
) {
3226 (void) sprintf(valstr
, "%llu",
3227 (u_longlong_t
)val64
);
3233 if (type
== DATA_TYPE_UINT64
) {
3235 (void) sprintf(valstr
, "%llu",
3236 (u_longlong_t
)val64
);
3238 } else if (field
== USFIELD_QUOTA
&&
3242 zfs_nicebytes(val64
, valstr
,
3248 case USFIELD_OBJUSED
:
3249 case USFIELD_OBJQUOTA
:
3250 if (type
== DATA_TYPE_UINT64
) {
3252 (void) sprintf(valstr
, "%llu",
3253 (u_longlong_t
)val64
);
3255 } else if (field
== USFIELD_OBJQUOTA
&&
3259 zfs_nicenum(val64
, valstr
,
3269 (void) putchar('\t');
3271 (void) fputs(" ", stdout
);
3274 (void) fputs(strval
, stdout
);
3275 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
3276 (void) printf("%-*s", (int)width
[field
], strval
);
3278 (void) printf("%*s", (int)width
[field
], strval
);
3284 (void) putchar('\n');
3288 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
3289 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
3297 boolean_t first
= B_TRUE
;
3299 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
3300 col
= gettext(us_field_hdr
[field
]);
3301 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
3302 (void) printf(first
? "%-*s" : " %-*s",
3303 (int)width
[field
], col
);
3305 (void) printf(first
? "%*s" : " %*s",
3306 (int)width
[field
], col
);
3311 (void) printf("\n");
3314 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
3315 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
3317 nvlist_free(node
->usn_nvl
);
3322 zfs_do_userspace(int argc
, char **argv
)
3325 zfs_userquota_prop_t p
;
3326 uu_avl_pool_t
*avl_pool
;
3328 uu_avl_walk_t
*walk
;
3330 char deffields
[] = "type,name,used,quota,objused,objquota";
3331 char *ofield
= NULL
;
3332 char *tfield
= NULL
;
3336 boolean_t scripted
= B_FALSE
;
3337 boolean_t prtnum
= B_FALSE
;
3338 boolean_t parsable
= B_FALSE
;
3339 boolean_t sid2posix
= B_FALSE
;
3342 zfs_sort_column_t
*sortcol
= NULL
;
3343 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
3347 uu_list_pool_t
*listpool
;
3349 uu_avl_index_t idx
= 0;
3350 uu_list_index_t idx2
= 0;
3355 if (strcmp(argv
[0], "groupspace") == 0) {
3356 /* Toggle default group types */
3357 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
3358 } else if (strcmp(argv
[0], "projectspace") == 0) {
3359 types
= USTYPE_PROJ
;
3363 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
3366 if (types
== USTYPE_PROJ
) {
3367 (void) fprintf(stderr
,
3368 gettext("invalid option 'n'\n"));
3384 if (zfs_add_sort_column(&sortcol
, optarg
,
3385 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
3386 (void) fprintf(stderr
,
3387 gettext("invalid field '%s'\n"), optarg
);
3392 if (types
== USTYPE_PROJ
) {
3393 (void) fprintf(stderr
,
3394 gettext("invalid option 't'\n"));
3400 if (types
== USTYPE_PROJ
) {
3401 (void) fprintf(stderr
,
3402 gettext("invalid option 'i'\n"));
3408 (void) fprintf(stderr
, gettext("missing argument for "
3409 "'%c' option\n"), optopt
);
3413 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3423 (void) fprintf(stderr
, gettext("missing dataset name\n"));
3427 (void) fprintf(stderr
, gettext("too many arguments\n"));
3431 /* Use default output fields if not specified using -o */
3435 if ((delim
= strchr(ofield
, ',')) != NULL
)
3437 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
3438 (void) fprintf(stderr
, gettext("invalid type '%s' "
3439 "for -o option\n"), ofield
);
3444 } while (delim
!= NULL
);
3445 fields
[cfield
] = USFIELD_LAST
;
3447 /* Override output types (-t option) */
3448 if (tfield
!= NULL
) {
3452 boolean_t found
= B_FALSE
;
3454 if ((delim
= strchr(tfield
, ',')) != NULL
)
3456 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
3458 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
3460 types
|= us_type_bits
[i
];
3465 (void) fprintf(stderr
, gettext("invalid type "
3466 "'%s' for -t option\n"), tfield
);
3471 } while (delim
!= NULL
);
3474 if ((zhp
= zfs_path_to_zhandle(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
|
3475 ZFS_TYPE_SNAPSHOT
)) == NULL
)
3477 if (zfs_get_underlying_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3478 (void) fprintf(stderr
, gettext("operation is only applicable "
3479 "to filesystems and their snapshots\n"));
3484 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
3485 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
3487 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
3490 /* Always add default sorting columns */
3491 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
3492 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
3494 cb
.cb_sortcol
= sortcol
;
3495 cb
.cb_numname
= prtnum
;
3496 cb
.cb_nicenum
= !parsable
;
3497 cb
.cb_avl_pool
= avl_pool
;
3498 cb
.cb_avl
= avl_tree
;
3499 cb
.cb_sid2posix
= sid2posix
;
3501 for (i
= 0; i
< USFIELD_LAST
; i
++)
3502 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
3504 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
3505 if ((zfs_prop_is_user(p
) &&
3506 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
3507 (zfs_prop_is_group(p
) &&
3508 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))) ||
3509 (zfs_prop_is_project(p
) && types
!= USTYPE_PROJ
))
3513 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0) {
3521 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
3524 us_populated
= B_TRUE
;
3526 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
3527 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
3528 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
3529 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
3531 while (node
!= NULL
) {
3533 node
= uu_avl_next(avl_tree
, node
);
3534 uu_avl_remove(avl_tree
, rmnode
);
3535 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
3536 uu_list_insert(list
, rmnode
, idx2
);
3539 for (node
= uu_list_first(list
); node
!= NULL
;
3540 node
= uu_list_next(list
, node
)) {
3541 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
3543 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
3544 uu_avl_insert(avl_tree
, node
, idx
);
3547 uu_list_destroy(list
);
3548 uu_list_pool_destroy(listpool
);
3550 /* Print and free node nvlist memory */
3551 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
3554 zfs_free_sort_columns(sortcol
);
3556 /* Clean up the AVL tree */
3557 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
3560 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
3561 uu_avl_remove(cb
.cb_avl
, node
);
3565 uu_avl_walk_end(walk
);
3566 uu_avl_destroy(avl_tree
);
3567 uu_avl_pool_destroy(avl_pool
);
3573 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
3574 * [-t type[,...]] [filesystem|volume|snapshot] ...
3576 * -H Scripted mode; elide headers and separate columns by tabs
3577 * -p Display values in parsable (literal) format.
3578 * -r Recurse over all children
3579 * -d Limit recursion by depth.
3580 * -o Control which fields to display.
3581 * -s Specify sort columns, descending order.
3582 * -S Specify sort columns, ascending order.
3583 * -t Control which object types to display.
3585 * When given no arguments, list all filesystems in the system.
3586 * Otherwise, list the specified datasets, optionally recursing down them if
3587 * '-r' is specified.
3589 typedef struct list_cbdata
{
3591 boolean_t cb_literal
;
3592 boolean_t cb_scripted
;
3593 zprop_list_t
*cb_proplist
;
3596 boolean_t cb_json_as_int
;
3600 * Given a list of columns to display, output appropriate headers for each one.
3603 print_header(list_cbdata_t
*cb
)
3605 zprop_list_t
*pl
= cb
->cb_proplist
;
3606 char headerbuf
[ZFS_MAXPROPLEN
];
3609 boolean_t first
= B_TRUE
;
3610 boolean_t right_justify
;
3612 color_start(ANSI_BOLD
);
3614 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3621 right_justify
= B_FALSE
;
3622 if (pl
->pl_prop
!= ZPROP_USERPROP
) {
3623 header
= zfs_prop_column_name(pl
->pl_prop
);
3624 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3626 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
3627 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
3628 headerbuf
[i
] = '\0';
3632 if (pl
->pl_next
== NULL
&& !right_justify
)
3633 (void) printf("%s", header
);
3634 else if (right_justify
)
3635 (void) printf("%*s", (int)pl
->pl_width
, header
);
3637 (void) printf("%-*s", (int)pl
->pl_width
, header
);
3642 (void) printf("\n");
3646 * Decides on the color that the avail value should be printed in.
3647 * > 80% used = yellow
3651 zfs_list_avail_color(zfs_handle_t
*zhp
)
3653 uint64_t used
= zfs_prop_get_int(zhp
, ZFS_PROP_USED
);
3654 uint64_t avail
= zfs_prop_get_int(zhp
, ZFS_PROP_AVAILABLE
);
3655 int percentage
= (int)((double)avail
/ MAX(avail
+ used
, 1) * 100);
3657 if (percentage
> 20)
3659 else if (percentage
> 10)
3660 return (ANSI_YELLOW
);
3666 * Given a dataset and a list of fields, print out all the properties according
3667 * to the described layout, or return an nvlist containing all the fields, later
3668 * to be printed out as JSON object.
3671 collect_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
3673 zprop_list_t
*pl
= cb
->cb_proplist
;
3674 boolean_t first
= B_TRUE
;
3675 char property
[ZFS_MAXPROPLEN
];
3676 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
3678 const char *propstr
;
3679 boolean_t right_justify
;
3680 nvlist_t
*item
, *d
, *props
;
3681 item
= d
= props
= NULL
;
3682 zprop_source_t sourcetype
= ZPROP_SRC_NONE
;
3683 char source
[ZFS_MAX_DATASET_NAME_LEN
];
3685 d
= fnvlist_lookup_nvlist(cb
->cb_jsobj
, "datasets");
3687 fprintf(stderr
, "datasets obj not found.\n");
3690 item
= fnvlist_alloc();
3691 props
= fnvlist_alloc();
3692 fill_dataset_info(item
, zhp
, cb
->cb_json_as_int
);
3695 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3696 if (!cb
->cb_json
&& !first
) {
3697 if (cb
->cb_scripted
)
3698 (void) putchar('\t');
3700 (void) fputs(" ", stdout
);
3705 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
3706 (void) strlcpy(property
, zfs_get_name(zhp
),
3709 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3710 } else if (pl
->pl_prop
!= ZPROP_USERPROP
) {
3711 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
3712 sizeof (property
), &sourcetype
, source
,
3713 sizeof (source
), cb
->cb_literal
) != 0)
3717 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3718 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
3719 sourcetype
= ZPROP_SRC_LOCAL
;
3720 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
3721 property
, sizeof (property
), cb
->cb_literal
) != 0) {
3722 sourcetype
= ZPROP_SRC_NONE
;
3727 right_justify
= B_TRUE
;
3728 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
3729 sourcetype
= ZPROP_SRC_LOCAL
;
3730 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
3731 property
, sizeof (property
), cb
->cb_literal
) != 0) {
3732 sourcetype
= ZPROP_SRC_NONE
;
3737 right_justify
= B_TRUE
;
3739 if (nvlist_lookup_nvlist(userprops
,
3740 pl
->pl_user_prop
, &propval
) != 0) {
3743 propstr
= fnvlist_lookup_string(propval
,
3746 fnvlist_lookup_string(propval
,
3747 ZPROP_SOURCE
), ZFS_MAX_DATASET_NAME_LEN
);
3749 zfs_get_name(zhp
)) == 0) {
3750 sourcetype
= ZPROP_SRC_LOCAL
;
3751 } else if (strcmp(source
,
3752 ZPROP_SOURCE_VAL_RECVD
) == 0) {
3753 sourcetype
= ZPROP_SRC_RECEIVED
;
3755 sourcetype
= ZPROP_SRC_INHERITED
;
3758 right_justify
= B_FALSE
;
3762 if (pl
->pl_prop
== ZFS_PROP_NAME
)
3764 const char *prop_name
;
3765 if (pl
->pl_prop
!= ZPROP_USERPROP
)
3766 prop_name
= zfs_prop_to_name(pl
->pl_prop
);
3768 prop_name
= pl
->pl_user_prop
;
3769 if (zprop_nvlist_one_property(
3771 sourcetype
, source
, NULL
, props
,
3772 cb
->cb_json_as_int
) != 0)
3776 * zfs_list_avail_color() needs
3777 * ZFS_PROP_AVAILABLE + USED, so we need another
3778 * for() search for the USED part when no colors
3779 * wanted, we can skip the whole thing
3781 if (use_color() && pl
->pl_prop
== ZFS_PROP_AVAILABLE
) {
3782 zprop_list_t
*pl2
= cb
->cb_proplist
;
3783 for (; pl2
!= NULL
; pl2
= pl2
->pl_next
) {
3784 if (pl2
->pl_prop
== ZFS_PROP_USED
) {
3786 zfs_list_avail_color(zhp
));
3788 * found it, no need for more
3797 * If this is being called in scripted mode, or if
3798 * this is the last column and it is left-justified,
3799 * don't include a width format specifier.
3801 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&&
3803 (void) fputs(propstr
, stdout
);
3804 else if (right_justify
) {
3805 (void) printf("%*s", (int)pl
->pl_width
,
3808 (void) printf("%-*s", (int)pl
->pl_width
,
3812 if (pl
->pl_prop
== ZFS_PROP_AVAILABLE
)
3817 fnvlist_add_nvlist(item
, "properties", props
);
3818 fnvlist_add_nvlist(d
, zfs_get_name(zhp
), item
);
3819 fnvlist_free(props
);
3822 (void) putchar('\n');
3826 * Generic callback function to list a dataset or snapshot.
3829 list_callback(zfs_handle_t
*zhp
, void *data
)
3831 list_cbdata_t
*cbp
= data
;
3833 if (cbp
->cb_first
) {
3834 if (!cbp
->cb_scripted
&& !cbp
->cb_json
)
3836 cbp
->cb_first
= B_FALSE
;
3839 collect_dataset(zhp
, cbp
);
3845 zfs_do_list(int argc
, char **argv
)
3848 char default_fields
[] =
3849 "name,used,available,referenced,mountpoint";
3850 int types
= ZFS_TYPE_DATASET
;
3851 boolean_t types_specified
= B_FALSE
;
3852 char *fields
= default_fields
;
3853 list_cbdata_t cb
= { 0 };
3856 zfs_sort_column_t
*sortcol
= NULL
;
3857 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3858 nvlist_t
*data
= NULL
;
3860 struct option long_options
[] = {
3861 {"json", no_argument
, NULL
, 'j'},
3862 {"json-int", no_argument
, NULL
, ZFS_OPTION_JSON_NUMS_AS_INT
},
3867 while ((c
= getopt_long(argc
, argv
, "jHS:d:o:prs:t:", long_options
,
3874 cb
.cb_literal
= B_TRUE
;
3875 flags
|= ZFS_ITER_LITERAL_PROPS
;
3878 limit
= parse_depth(optarg
, &flags
);
3881 flags
|= ZFS_ITER_RECURSE
;
3884 cb
.cb_json
= B_TRUE
;
3885 cb
.cb_jsobj
= zfs_json_schema(0, 1);
3886 data
= fnvlist_alloc();
3887 fnvlist_add_nvlist(cb
.cb_jsobj
, "datasets", data
);
3890 case ZFS_OPTION_JSON_NUMS_AS_INT
:
3891 cb
.cb_json_as_int
= B_TRUE
;
3892 cb
.cb_literal
= B_TRUE
;
3895 cb
.cb_scripted
= B_TRUE
;
3898 if (zfs_add_sort_column(&sortcol
, optarg
,
3900 (void) fprintf(stderr
,
3901 gettext("invalid property '%s'\n"), optarg
);
3906 if (zfs_add_sort_column(&sortcol
, optarg
,
3908 (void) fprintf(stderr
,
3909 gettext("invalid property '%s'\n"), optarg
);
3915 types_specified
= B_TRUE
;
3916 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3918 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
3919 static const char *const type_subopts
[] = {
3929 static const int type_types
[] = {
3930 ZFS_TYPE_FILESYSTEM
,
3931 ZFS_TYPE_FILESYSTEM
,
3937 ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
3940 for (c
= 0; c
< ARRAY_SIZE(type_subopts
); ++c
)
3941 if (strcmp(tok
, type_subopts
[c
]) == 0) {
3942 types
|= type_types
[c
];
3946 (void) fprintf(stderr
,
3947 gettext("invalid type '%s'\n"), tok
);
3953 (void) fprintf(stderr
, gettext("missing argument for "
3954 "'%c' option\n"), optopt
);
3958 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3967 if (!cb
.cb_json
&& cb
.cb_json_as_int
) {
3968 (void) fprintf(stderr
, gettext("'--json-int' only works with"
3974 * If "-o space" and no types were specified, don't display snapshots.
3976 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3977 types
&= ~ZFS_TYPE_SNAPSHOT
;
3980 * Handle users who want to list all snapshots or bookmarks
3981 * of the current dataset (ex. 'zfs list -t snapshot <dataset>').
3983 if ((types
== ZFS_TYPE_SNAPSHOT
|| types
== ZFS_TYPE_BOOKMARK
) &&
3984 argc
> 0 && (flags
& ZFS_ITER_RECURSE
) == 0 && limit
== 0) {
3985 flags
|= (ZFS_ITER_DEPTH_LIMIT
| ZFS_ITER_RECURSE
);
3990 * If the user specifies '-o all', the zprop_get_list() doesn't
3991 * normally include the name of the dataset. For 'zfs list', we always
3992 * want this property to be first.
3994 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3998 cb
.cb_first
= B_TRUE
;
4001 * If we are only going to list and sort by properties that are "fast"
4002 * then we can use "simple" mode and avoid populating the properties
4005 if (zfs_list_only_by_fast(cb
.cb_proplist
) &&
4006 zfs_sort_only_by_fast(sortcol
))
4007 flags
|= ZFS_ITER_SIMPLE
;
4009 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
4010 limit
, list_callback
, &cb
);
4012 if (ret
== 0 && cb
.cb_json
)
4013 zcmd_print_json(cb
.cb_jsobj
);
4014 else if (ret
!= 0 && cb
.cb_json
)
4015 nvlist_free(cb
.cb_jsobj
);
4017 zprop_free_list(cb
.cb_proplist
);
4018 zfs_free_sort_columns(sortcol
);
4020 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
4021 (void) fprintf(stderr
, gettext("no datasets available\n"));
4027 * zfs rename [-fu] <fs | snap | vol> <fs | snap | vol>
4028 * zfs rename [-f] -p <fs | vol> <fs | vol>
4029 * zfs rename [-u] -r <snap> <snap>
4031 * Renames the given dataset to another of the same type.
4033 * The '-p' flag creates all the non-existing ancestors of the target first.
4034 * The '-u' flag prevents file systems from being remounted during rename.
4037 zfs_do_rename(int argc
, char **argv
)
4040 renameflags_t flags
= { 0 };
4044 boolean_t parents
= B_FALSE
;
4047 while ((c
= getopt(argc
, argv
, "pruf")) != -1) {
4053 flags
.recursive
= B_TRUE
;
4056 flags
.nounmount
= B_TRUE
;
4059 flags
.forceunmount
= B_TRUE
;
4063 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4072 /* check number of arguments */
4074 (void) fprintf(stderr
, gettext("missing source dataset "
4079 (void) fprintf(stderr
, gettext("missing target dataset "
4084 (void) fprintf(stderr
, gettext("too many arguments\n"));
4088 if (flags
.recursive
&& parents
) {
4089 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
4094 if (flags
.nounmount
&& parents
) {
4095 (void) fprintf(stderr
, gettext("-u and -p options are mutually "
4100 if (flags
.recursive
&& strchr(argv
[0], '@') == 0) {
4101 (void) fprintf(stderr
, gettext("source dataset for recursive "
4102 "rename must be a snapshot\n"));
4106 if (flags
.nounmount
)
4107 types
= ZFS_TYPE_FILESYSTEM
;
4109 types
= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
4111 types
= ZFS_TYPE_DATASET
;
4113 if ((zhp
= zfs_open(g_zfs
, argv
[0], types
)) == NULL
)
4116 /* If we were asked and the name looks good, try to create ancestors. */
4117 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
4118 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
4123 ret
= (zfs_rename(zhp
, argv
[1], flags
) != 0);
4132 * Promotes the given clone fs to be the parent
4135 zfs_do_promote(int argc
, char **argv
)
4141 if (argc
> 1 && argv
[1][0] == '-') {
4142 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4147 /* check number of arguments */
4149 (void) fprintf(stderr
, gettext("missing clone filesystem"
4154 (void) fprintf(stderr
, gettext("too many arguments\n"));
4158 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4162 ret
= (zfs_promote(zhp
) != 0);
4170 zfs_do_redact(int argc
, char **argv
)
4173 char *bookname
= NULL
;
4174 char **rsnaps
= NULL
;
4179 (void) fprintf(stderr
, gettext("too few arguments\n"));
4186 numrsnaps
= argc
- 2;
4188 nvlist_t
*rsnapnv
= fnvlist_alloc();
4190 for (int i
= 0; i
< numrsnaps
; i
++) {
4191 fnvlist_add_boolean(rsnapnv
, rsnaps
[i
]);
4194 int err
= lzc_redact(snap
, bookname
, rsnapnv
);
4195 fnvlist_free(rsnapnv
);
4201 zfs_handle_t
*zhp
= zfs_open(g_zfs
, snap
, ZFS_TYPE_SNAPSHOT
);
4203 (void) fprintf(stderr
, gettext("provided snapshot %s "
4204 "does not exist\n"), snap
);
4208 for (int i
= 0; i
< numrsnaps
; i
++) {
4209 zhp
= zfs_open(g_zfs
, rsnaps
[i
], ZFS_TYPE_SNAPSHOT
);
4211 (void) fprintf(stderr
, gettext("provided "
4212 "snapshot %s does not exist\n"), rsnaps
[i
]);
4220 (void) fprintf(stderr
, gettext("specified redaction bookmark "
4221 "(%s) provided already exists\n"), bookname
);
4224 (void) fprintf(stderr
, gettext("provided bookmark name cannot "
4225 "be used, final name would be too long\n"));
4228 (void) fprintf(stderr
, gettext("too many redaction snapshots "
4232 if (strchr(bookname
, '#') != NULL
)
4233 (void) fprintf(stderr
, gettext(
4234 "redaction bookmark name must not contain '#'\n"));
4236 (void) fprintf(stderr
, gettext(
4237 "redaction snapshot must be descendent of "
4238 "snapshot being redacted\n"));
4241 (void) fprintf(stderr
, gettext("attempted to redact redacted "
4242 "dataset or with respect to redacted dataset\n"));
4245 (void) fprintf(stderr
, gettext("redaction bookmarks feature "
4249 (void) fprintf(stderr
, gettext("potentially invalid redaction "
4250 "snapshot; full dataset names required\n"));
4253 (void) fprintf(stderr
, gettext("attempted to resume redaction "
4254 " with a mismatched redaction list\n"));
4257 (void) fprintf(stderr
, gettext("internal error: %s\n"),
4265 * zfs rollback [-rRf] <snapshot>
4267 * -r Delete any intervening snapshots before doing rollback
4268 * -R Delete any snapshots and their clones
4269 * -f ignored for backwards compatibility
4271 * Given a filesystem, rollback to a specific snapshot, discarding any changes
4272 * since then and making it the active dataset. If more recent snapshots exist,
4273 * the command will complain unless the '-r' flag is given.
4275 typedef struct rollback_cbdata
{
4277 uint8_t cb_younger_ds_printed
;
4282 boolean_t cb_recurse
;
4283 } rollback_cbdata_t
;
4286 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
4288 rollback_cbdata_t
*cbp
= data
;
4290 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
4291 (void) fprintf(stderr
, gettext("cannot rollback to "
4292 "'%s': clones of previous snapshots exist\n"),
4294 (void) fprintf(stderr
, gettext("use '-R' to "
4295 "force deletion of the following clones and "
4301 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
4309 * Report some snapshots/bookmarks more recent than the one specified.
4310 * Used when '-r' is not specified. We reuse this same callback for the
4311 * snapshot dependents - if 'cb_dependent' is set, then this is a
4312 * dependent and we should report it without checking the transaction group.
4315 rollback_check(zfs_handle_t
*zhp
, void *data
)
4317 rollback_cbdata_t
*cbp
= data
;
4319 * Max number of younger snapshots and/or bookmarks to display before
4320 * we stop the iteration.
4322 const uint8_t max_younger
= 32;
4324 if (cbp
->cb_doclones
) {
4329 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
4330 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
4331 (void) fprintf(stderr
, gettext("cannot "
4332 "rollback to '%s': more recent snapshots "
4333 "or bookmarks exist\n"),
4335 (void) fprintf(stderr
, gettext("use '-r' to "
4336 "force deletion of the following "
4337 "snapshots and bookmarks:\n"));
4342 if (cbp
->cb_recurse
) {
4343 if (zfs_iter_dependents_v2(zhp
, 0, B_TRUE
,
4344 rollback_check_dependent
, cbp
) != 0) {
4349 (void) fprintf(stderr
, "%s\n",
4351 cbp
->cb_younger_ds_printed
++;
4356 if (cbp
->cb_younger_ds_printed
== max_younger
) {
4358 * This non-recursive rollback is going to fail due to the
4359 * presence of snapshots and/or bookmarks that are younger than
4360 * the rollback target.
4361 * We printed some of the offending objects, now we stop
4362 * zfs_iter_snapshot/bookmark iteration so we can fail fast and
4363 * avoid iterating over the rest of the younger objects
4365 (void) fprintf(stderr
, gettext("Output limited to %d "
4366 "snapshots/bookmarks\n"), max_younger
);
4373 zfs_do_rollback(int argc
, char **argv
)
4377 boolean_t force
= B_FALSE
;
4378 rollback_cbdata_t cb
= { 0 };
4379 zfs_handle_t
*zhp
, *snap
;
4380 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
4382 uint64_t min_txg
= 0;
4385 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
4398 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4407 /* check number of arguments */
4409 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
4413 (void) fprintf(stderr
, gettext("too many arguments\n"));
4417 /* open the snapshot */
4418 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
4421 /* open the parent dataset */
4422 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
4423 verify((delim
= strrchr(parentname
, '@')) != NULL
);
4425 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
4431 * Check for more recent snapshots and/or clones based on the presence
4434 cb
.cb_target
= argv
[0];
4435 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
4436 cb
.cb_first
= B_TRUE
;
4439 if (cb
.cb_create
> 0)
4440 min_txg
= cb
.cb_create
;
4442 if ((ret
= zfs_iter_snapshots_v2(zhp
, 0, rollback_check
, &cb
,
4445 if ((ret
= zfs_iter_bookmarks_v2(zhp
, 0, rollback_check
, &cb
)) != 0)
4448 if ((ret
= cb
.cb_error
) != 0)
4452 * Rollback parent to the given snapshot.
4454 ret
= zfs_rollback(zhp
, snap
, force
);
4467 * zfs set property=value ... { fs | snap | vol } ...
4469 * Sets the given properties for all datasets specified on the command line.
4473 set_callback(zfs_handle_t
*zhp
, void *data
)
4475 zprop_set_cbdata_t
*cb
= data
;
4476 int ret
= zfs_prop_set_list_flags(zhp
, cb
->cb_proplist
, cb
->cb_flags
);
4478 if (ret
!= 0 || libzfs_errno(g_zfs
) != EZFS_SUCCESS
) {
4479 switch (libzfs_errno(g_zfs
)) {
4480 case EZFS_MOUNTFAILED
:
4481 (void) fprintf(stderr
, gettext("property may be set "
4482 "but unable to remount filesystem\n"));
4484 case EZFS_SHARENFSFAILED
:
4485 (void) fprintf(stderr
, gettext("property may be set "
4486 "but unable to reshare filesystem\n"));
4494 zfs_do_set(int argc
, char **argv
)
4496 zprop_set_cbdata_t cb
= { 0 };
4497 int ds_start
= -1; /* argv idx of first dataset arg */
4502 while ((c
= getopt(argc
, argv
, "u")) != -1) {
4505 cb
.cb_flags
|= ZFS_SET_NOMOUNT
;
4509 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4518 /* check number of arguments */
4520 (void) fprintf(stderr
, gettext("missing arguments\n"));
4524 if (strchr(argv
[0], '=') == NULL
) {
4525 (void) fprintf(stderr
, gettext("missing property=value "
4528 (void) fprintf(stderr
, gettext("missing dataset "
4534 /* validate argument order: prop=val args followed by dataset args */
4535 for (i
= 0; i
< argc
; i
++) {
4536 if (strchr(argv
[i
], '=') != NULL
) {
4538 /* out-of-order prop=val argument */
4539 (void) fprintf(stderr
, gettext("invalid "
4540 "argument order\n"));
4543 } else if (ds_start
< 0) {
4548 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
4552 /* Populate a list of property settings */
4553 if (nvlist_alloc(&cb
.cb_proplist
, NV_UNIQUE_NAME
, 0) != 0)
4555 for (i
= 0; i
< ds_start
; i
++) {
4556 if (!parseprop(cb
.cb_proplist
, argv
[i
])) {
4562 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
4563 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, &cb
);
4566 nvlist_free(cb
.cb_proplist
);
4570 typedef struct snap_cbdata
{
4572 boolean_t sd_recursive
;
4573 const char *sd_snapname
;
4577 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
4579 snap_cbdata_t
*sd
= arg
;
4584 if (sd
->sd_recursive
&&
4585 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
4590 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
4593 fnvlist_add_boolean(sd
->sd_nvl
, name
);
4596 if (sd
->sd_recursive
)
4597 rv
= zfs_iter_filesystems_v2(zhp
, 0, zfs_snapshot_cb
, sd
);
4603 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
4605 * Creates a snapshot with the given name. While functionally equivalent to
4606 * 'zfs create', it is a separate command to differentiate intent.
4609 zfs_do_snapshot(int argc
, char **argv
)
4614 snap_cbdata_t sd
= { 0 };
4615 boolean_t multiple_snaps
= B_FALSE
;
4617 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
4619 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4623 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
4626 if (!parseprop(props
, optarg
)) {
4627 nvlist_free(sd
.sd_nvl
);
4633 sd
.sd_recursive
= B_TRUE
;
4634 multiple_snaps
= B_TRUE
;
4637 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4646 /* check number of arguments */
4648 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4653 multiple_snaps
= B_TRUE
;
4654 for (; argc
> 0; argc
--, argv
++) {
4658 atp
= strchr(argv
[0], '@');
4662 sd
.sd_snapname
= atp
+ 1;
4663 zhp
= zfs_open(g_zfs
, argv
[0],
4664 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4667 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
4671 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
4672 nvlist_free(sd
.sd_nvl
);
4674 if (ret
!= 0 && multiple_snaps
)
4675 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
4679 nvlist_free(sd
.sd_nvl
);
4686 * Array of prefixes to exclude –
4687 * a linear search, even if executed for each dataset,
4688 * is plenty good enough.
4690 typedef struct zfs_send_exclude_arg
{
4693 } zfs_send_exclude_arg_t
;
4696 zfs_do_send_exclude(zfs_handle_t
*zhp
, void *context
)
4698 zfs_send_exclude_arg_t
*excludes
= context
;
4699 const char *name
= zfs_get_name(zhp
);
4701 for (size_t i
= 0; i
< excludes
->count
; ++i
) {
4702 size_t len
= strlen(excludes
->list
[i
]);
4703 if (strncmp(name
, excludes
->list
[i
], len
) == 0 &&
4704 memchr("/@", name
[len
], sizeof ("/@")))
4712 * Send a backup stream to stdout.
4715 zfs_do_send(int argc
, char **argv
)
4717 char *fromname
= NULL
;
4718 char *toname
= NULL
;
4719 char *resume_token
= NULL
;
4722 sendflags_t flags
= { 0 };
4724 nvlist_t
*dbgnv
= NULL
;
4725 char *redactbook
= NULL
;
4726 zfs_send_exclude_arg_t excludes
= { 0 };
4728 struct option long_options
[] = {
4729 {"replicate", no_argument
, NULL
, 'R'},
4730 {"skip-missing", no_argument
, NULL
, 's'},
4731 {"redact", required_argument
, NULL
, 'd'},
4732 {"props", no_argument
, NULL
, 'p'},
4733 {"parsable", no_argument
, NULL
, 'P'},
4734 {"dedup", no_argument
, NULL
, 'D'},
4735 {"proctitle", no_argument
, NULL
, 'V'},
4736 {"verbose", no_argument
, NULL
, 'v'},
4737 {"dryrun", no_argument
, NULL
, 'n'},
4738 {"large-block", no_argument
, NULL
, 'L'},
4739 {"embed", no_argument
, NULL
, 'e'},
4740 {"resume", required_argument
, NULL
, 't'},
4741 {"compressed", no_argument
, NULL
, 'c'},
4742 {"raw", no_argument
, NULL
, 'w'},
4743 {"backup", no_argument
, NULL
, 'b'},
4744 {"holds", no_argument
, NULL
, 'h'},
4745 {"saved", no_argument
, NULL
, 'S'},
4746 {"exclude", required_argument
, NULL
, 'X'},
4751 while ((c
= getopt_long(argc
, argv
, ":i:I:RsDpVvnPLeht:cwbd:SX:",
4752 long_options
, NULL
)) != -1) {
4755 for (char *ds
; (ds
= strsep(&optarg
, ",")) != NULL
; ) {
4756 if (!zfs_name_valid(ds
, ZFS_TYPE_DATASET
) ||
4757 strchr(ds
, '/') == NULL
) {
4758 (void) fprintf(stderr
, gettext("-X %s: "
4759 "not a valid non-root dataset name"
4763 excludes
.list
= safe_realloc(excludes
.list
,
4764 sizeof (char *) * (excludes
.count
+ 1));
4765 excludes
.list
[excludes
.count
++] = ds
;
4777 flags
.doall
= B_TRUE
;
4780 flags
.replicate
= B_TRUE
;
4783 flags
.skipmissing
= B_TRUE
;
4786 redactbook
= optarg
;
4789 flags
.props
= B_TRUE
;
4792 flags
.backup
= B_TRUE
;
4795 flags
.holds
= B_TRUE
;
4798 flags
.parsable
= B_TRUE
;
4801 flags
.progressastitle
= B_TRUE
;
4805 flags
.progress
= B_TRUE
;
4808 (void) fprintf(stderr
,
4809 gettext("WARNING: deduplicated send is no "
4810 "longer supported. A regular,\n"
4811 "non-deduplicated stream will be generated.\n\n"));
4814 flags
.dryrun
= B_TRUE
;
4817 flags
.largeblock
= B_TRUE
;
4820 flags
.embed_data
= B_TRUE
;
4823 resume_token
= optarg
;
4826 flags
.compress
= B_TRUE
;
4830 flags
.compress
= B_TRUE
;
4831 flags
.embed_data
= B_TRUE
;
4832 flags
.largeblock
= B_TRUE
;
4835 flags
.saved
= B_TRUE
;
4839 * If a parameter was not passed, optopt contains the
4840 * value that would normally lead us into the
4841 * appropriate case statement. If it's > 256, then this
4842 * must be a longopt and we should look at argv to get
4843 * the string. Otherwise it's just the character, so we
4844 * should use it directly.
4846 if (optopt
<= UINT8_MAX
) {
4847 (void) fprintf(stderr
,
4848 gettext("missing argument for '%c' "
4849 "option\n"), optopt
);
4851 (void) fprintf(stderr
,
4852 gettext("missing argument for '%s' "
4853 "option\n"), argv
[optind
- 1]);
4855 free(excludes
.list
);
4861 * If an invalid flag was passed, optopt contains the
4862 * character if it was a short flag, or 0 if it was a
4866 (void) fprintf(stderr
,
4867 gettext("invalid option '%c'\n"), optopt
);
4869 (void) fprintf(stderr
,
4870 gettext("invalid option '%s'\n"),
4874 free(excludes
.list
);
4879 if ((flags
.parsable
|| flags
.progressastitle
) && flags
.verbosity
== 0)
4880 flags
.verbosity
= 1;
4882 if (excludes
.count
> 0 && !flags
.replicate
) {
4883 free(excludes
.list
);
4884 (void) fprintf(stderr
, gettext("Cannot specify "
4885 "dataset exclusion (-X) on a non-recursive "
4893 if (resume_token
!= NULL
) {
4894 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
4895 flags
.backup
|| flags
.holds
||
4896 flags
.saved
|| redactbook
!= NULL
) {
4897 free(excludes
.list
);
4898 (void) fprintf(stderr
,
4899 gettext("invalid flags combined with -t\n"));
4903 free(excludes
.list
);
4904 (void) fprintf(stderr
, gettext("too many arguments\n"));
4909 free(excludes
.list
);
4910 (void) fprintf(stderr
,
4911 gettext("missing snapshot argument\n"));
4915 free(excludes
.list
);
4916 (void) fprintf(stderr
, gettext("too many arguments\n"));
4922 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
4923 flags
.doall
|| flags
.backup
||
4924 flags
.holds
|| flags
.largeblock
|| flags
.embed_data
||
4925 flags
.compress
|| flags
.raw
|| redactbook
!= NULL
) {
4926 free(excludes
.list
);
4928 (void) fprintf(stderr
, gettext("incompatible flags "
4929 "combined with saved send flag\n"));
4932 if (strchr(argv
[0], '@') != NULL
) {
4933 free(excludes
.list
);
4935 (void) fprintf(stderr
, gettext("saved send must "
4936 "specify the dataset with partially-received "
4942 if (flags
.raw
&& redactbook
!= NULL
) {
4943 free(excludes
.list
);
4944 (void) fprintf(stderr
,
4945 gettext("Error: raw sends may not be redacted.\n"));
4949 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
4950 free(excludes
.list
);
4951 (void) fprintf(stderr
,
4952 gettext("Error: Stream can not be written to a terminal.\n"
4953 "You must redirect standard output.\n"));
4958 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
4960 free(excludes
.list
);
4964 err
= zfs_send_saved(zhp
, &flags
, STDOUT_FILENO
,
4966 free(excludes
.list
);
4969 } else if (resume_token
!= NULL
) {
4970 free(excludes
.list
);
4971 return (zfs_send_resume(g_zfs
, &flags
, STDOUT_FILENO
,
4975 if (flags
.skipmissing
&& !flags
.replicate
) {
4976 free(excludes
.list
);
4977 (void) fprintf(stderr
,
4978 gettext("skip-missing flag can only be used in "
4979 "conjunction with replicate\n"));
4984 * For everything except -R and -I, use the new, cleaner code path.
4986 if (!(flags
.replicate
|| flags
.doall
)) {
4987 char frombuf
[ZFS_MAX_DATASET_NAME_LEN
];
4989 if (fromname
!= NULL
&& (strchr(fromname
, '#') == NULL
&&
4990 strchr(fromname
, '@') == NULL
)) {
4992 * Neither bookmark or snapshot was specified. Print a
4993 * warning, and assume snapshot.
4995 (void) fprintf(stderr
, "Warning: incremental source "
4996 "didn't specify type, assuming snapshot. Use '@' "
4997 "or '#' prefix to avoid ambiguity.\n");
4998 (void) snprintf(frombuf
, sizeof (frombuf
), "@%s",
5002 if (fromname
!= NULL
&&
5003 (fromname
[0] == '#' || fromname
[0] == '@')) {
5005 * Incremental source name begins with # or @.
5006 * Default to same fs as target.
5008 char tmpbuf
[ZFS_MAX_DATASET_NAME_LEN
];
5009 (void) strlcpy(tmpbuf
, fromname
, sizeof (tmpbuf
));
5010 (void) strlcpy(frombuf
, argv
[0], sizeof (frombuf
));
5011 cp
= strchr(frombuf
, '@');
5014 (void) strlcat(frombuf
, tmpbuf
, sizeof (frombuf
));
5018 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
5020 free(excludes
.list
);
5023 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, &flags
,
5026 free(excludes
.list
);
5031 if (fromname
!= NULL
&& strchr(fromname
, '#')) {
5032 (void) fprintf(stderr
,
5033 gettext("Error: multiple snapshots cannot be "
5034 "sent from a bookmark.\n"));
5035 free(excludes
.list
);
5039 if (redactbook
!= NULL
) {
5040 (void) fprintf(stderr
, gettext("Error: multiple snapshots "
5041 "cannot be sent redacted.\n"));
5042 free(excludes
.list
);
5046 if ((cp
= strchr(argv
[0], '@')) == NULL
) {
5047 (void) fprintf(stderr
, gettext("Error: "
5048 "Unsupported flag with filesystem or bookmark.\n"));
5049 free(excludes
.list
);
5054 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5056 free(excludes
.list
);
5061 * If they specified the full path to the snapshot, chop off
5062 * everything except the short name of the snapshot, but special
5063 * case if they specify the origin.
5065 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
5066 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
5069 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
5070 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
5072 if (strcmp(origin
, fromname
) == 0) {
5074 flags
.fromorigin
= B_TRUE
;
5077 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
5079 free(excludes
.list
);
5080 (void) fprintf(stderr
,
5081 gettext("incremental source must be "
5082 "in same filesystem\n"));
5086 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
5088 free(excludes
.list
);
5089 (void) fprintf(stderr
,
5090 gettext("invalid incremental source\n"));
5096 if (flags
.replicate
&& fromname
== NULL
)
5097 flags
.doall
= B_TRUE
;
5099 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
,
5100 excludes
.count
> 0 ? zfs_do_send_exclude
: NULL
,
5101 &excludes
, flags
.verbosity
>= 3 ? &dbgnv
: NULL
);
5103 if (flags
.verbosity
>= 3 && dbgnv
!= NULL
) {
5105 * dump_nvlist prints to stdout, but that's been
5106 * redirected to a file. Make it print to stderr
5109 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
5110 dump_nvlist(dbgnv
, 0);
5115 free(excludes
.list
);
5120 * Restore a backup stream from stdin.
5123 zfs_do_receive(int argc
, char **argv
)
5126 recvflags_t flags
= { 0 };
5127 boolean_t abort_resumable
= B_FALSE
;
5130 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
5134 while ((c
= getopt(argc
, argv
, ":o:x:dehMnuvFsAc")) != -1) {
5137 if (!parseprop(props
, optarg
)) {
5143 if (!parsepropname(props
, optarg
)) {
5150 (void) fprintf(stderr
, gettext("invalid option "
5151 "combination: -d and -e are mutually "
5155 flags
.isprefix
= B_TRUE
;
5158 if (flags
.isprefix
) {
5159 (void) fprintf(stderr
, gettext("invalid option "
5160 "combination: -d and -e are mutually "
5164 flags
.istail
= B_TRUE
;
5167 flags
.skipholds
= B_TRUE
;
5170 flags
.forceunmount
= B_TRUE
;
5173 flags
.dryrun
= B_TRUE
;
5176 flags
.nomount
= B_TRUE
;
5179 flags
.verbose
= B_TRUE
;
5182 flags
.resumable
= B_TRUE
;
5185 flags
.force
= B_TRUE
;
5188 abort_resumable
= B_TRUE
;
5191 flags
.heal
= B_TRUE
;
5194 (void) fprintf(stderr
, gettext("missing argument for "
5195 "'%c' option\n"), optopt
);
5199 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5208 /* zfs recv -e (use "tail" name) implies -d (remove dataset "head") */
5210 flags
.isprefix
= B_TRUE
;
5212 /* check number of arguments */
5214 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
5218 (void) fprintf(stderr
, gettext("too many arguments\n"));
5222 if (abort_resumable
) {
5223 if (flags
.isprefix
|| flags
.istail
|| flags
.dryrun
||
5224 flags
.resumable
|| flags
.nomount
) {
5225 (void) fprintf(stderr
, gettext("invalid option\n"));
5229 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
5230 (void) snprintf(namebuf
, sizeof (namebuf
),
5231 "%s/%%recv", argv
[0]);
5233 if (zfs_dataset_exists(g_zfs
, namebuf
,
5234 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) {
5235 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
5236 namebuf
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5241 err
= zfs_destroy(zhp
, B_FALSE
);
5244 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
5245 argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5248 if (!zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) ||
5249 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
5250 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == -1) {
5251 (void) fprintf(stderr
,
5252 gettext("'%s' does not have any "
5253 "resumable receive state to abort\n"),
5259 err
= zfs_destroy(zhp
, B_FALSE
);
5266 if (isatty(STDIN_FILENO
)) {
5267 (void) fprintf(stderr
,
5268 gettext("Error: Backup stream can not be read "
5269 "from a terminal.\n"
5270 "You must redirect standard input.\n"));
5274 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
5281 * allow/unallow stuff
5283 /* copied from zfs/sys/dsl_deleg.h */
5284 #define ZFS_DELEG_PERM_CREATE "create"
5285 #define ZFS_DELEG_PERM_DESTROY "destroy"
5286 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
5287 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
5288 #define ZFS_DELEG_PERM_CLONE "clone"
5289 #define ZFS_DELEG_PERM_PROMOTE "promote"
5290 #define ZFS_DELEG_PERM_RENAME "rename"
5291 #define ZFS_DELEG_PERM_MOUNT "mount"
5292 #define ZFS_DELEG_PERM_SHARE "share"
5293 #define ZFS_DELEG_PERM_SEND "send"
5294 #define ZFS_DELEG_PERM_RECEIVE "receive"
5295 #define ZFS_DELEG_PERM_ALLOW "allow"
5296 #define ZFS_DELEG_PERM_USERPROP "userprop"
5297 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
5298 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
5299 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
5300 #define ZFS_DELEG_PERM_USERUSED "userused"
5301 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
5302 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
5303 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
5304 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
5305 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
5307 #define ZFS_DELEG_PERM_HOLD "hold"
5308 #define ZFS_DELEG_PERM_RELEASE "release"
5309 #define ZFS_DELEG_PERM_DIFF "diff"
5310 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
5311 #define ZFS_DELEG_PERM_LOAD_KEY "load-key"
5312 #define ZFS_DELEG_PERM_CHANGE_KEY "change-key"
5314 #define ZFS_DELEG_PERM_PROJECTUSED "projectused"
5315 #define ZFS_DELEG_PERM_PROJECTQUOTA "projectquota"
5316 #define ZFS_DELEG_PERM_PROJECTOBJUSED "projectobjused"
5317 #define ZFS_DELEG_PERM_PROJECTOBJQUOTA "projectobjquota"
5319 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
5321 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
5322 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
5323 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
5324 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
5325 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
5326 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
5327 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
5328 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
5329 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
5330 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
5331 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
5332 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
5333 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
5334 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
5335 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
5336 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
5337 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
5338 { ZFS_DELEG_PERM_LOAD_KEY
, ZFS_DELEG_NOTE_LOAD_KEY
},
5339 { ZFS_DELEG_PERM_CHANGE_KEY
, ZFS_DELEG_NOTE_CHANGE_KEY
},
5341 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
5342 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
5343 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
5344 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
5345 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
5346 { ZFS_DELEG_PERM_USEROBJQUOTA
, ZFS_DELEG_NOTE_USEROBJQUOTA
},
5347 { ZFS_DELEG_PERM_USEROBJUSED
, ZFS_DELEG_NOTE_USEROBJUSED
},
5348 { ZFS_DELEG_PERM_GROUPOBJQUOTA
, ZFS_DELEG_NOTE_GROUPOBJQUOTA
},
5349 { ZFS_DELEG_PERM_GROUPOBJUSED
, ZFS_DELEG_NOTE_GROUPOBJUSED
},
5350 { ZFS_DELEG_PERM_PROJECTUSED
, ZFS_DELEG_NOTE_PROJECTUSED
},
5351 { ZFS_DELEG_PERM_PROJECTQUOTA
, ZFS_DELEG_NOTE_PROJECTQUOTA
},
5352 { ZFS_DELEG_PERM_PROJECTOBJUSED
, ZFS_DELEG_NOTE_PROJECTOBJUSED
},
5353 { ZFS_DELEG_PERM_PROJECTOBJQUOTA
, ZFS_DELEG_NOTE_PROJECTOBJQUOTA
},
5354 { NULL
, ZFS_DELEG_NOTE_NONE
}
5357 /* permission structure */
5358 typedef struct deleg_perm
{
5359 zfs_deleg_who_type_t dp_who_type
;
5360 const char *dp_name
;
5362 boolean_t dp_descend
;
5366 typedef struct deleg_perm_node
{
5367 deleg_perm_t dpn_perm
;
5369 uu_avl_node_t dpn_avl_node
;
5370 } deleg_perm_node_t
;
5372 typedef struct fs_perm fs_perm_t
;
5374 /* permissions set */
5375 typedef struct who_perm
{
5376 zfs_deleg_who_type_t who_type
;
5377 const char *who_name
; /* id */
5378 char who_ug_name
[256]; /* user/group name */
5379 fs_perm_t
*who_fsperm
; /* uplink */
5381 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
5385 typedef struct who_perm_node
{
5386 who_perm_t who_perm
;
5387 uu_avl_node_t who_avl_node
;
5390 typedef struct fs_perm_set fs_perm_set_t
;
5391 /* fs permissions */
5393 const char *fsp_name
;
5395 uu_avl_t
*fsp_sc_avl
; /* sets,create */
5396 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
5398 fs_perm_set_t
*fsp_set
; /* uplink */
5402 typedef struct fs_perm_node
{
5403 fs_perm_t fspn_fsperm
;
5406 uu_list_node_t fspn_list_node
;
5409 /* top level structure */
5410 struct fs_perm_set
{
5411 uu_list_pool_t
*fsps_list_pool
;
5412 uu_list_t
*fsps_list
; /* list of fs_perms */
5414 uu_avl_pool_t
*fsps_named_set_avl_pool
;
5415 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
5416 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
5419 static inline const char *
5420 deleg_perm_type(zfs_deleg_note_t note
)
5426 case ZFS_DELEG_NOTE_GROUPQUOTA
:
5427 case ZFS_DELEG_NOTE_GROUPUSED
:
5428 case ZFS_DELEG_NOTE_USERPROP
:
5429 case ZFS_DELEG_NOTE_USERQUOTA
:
5430 case ZFS_DELEG_NOTE_USERUSED
:
5431 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
5432 case ZFS_DELEG_NOTE_USEROBJUSED
:
5433 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
5434 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
5435 case ZFS_DELEG_NOTE_PROJECTUSED
:
5436 case ZFS_DELEG_NOTE_PROJECTQUOTA
:
5437 case ZFS_DELEG_NOTE_PROJECTOBJUSED
:
5438 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA
:
5440 return (gettext("other"));
5442 return (gettext("subcommand"));
5447 who_type2weight(zfs_deleg_who_type_t who_type
)
5451 case ZFS_DELEG_NAMED_SET_SETS
:
5452 case ZFS_DELEG_NAMED_SET
:
5455 case ZFS_DELEG_CREATE_SETS
:
5456 case ZFS_DELEG_CREATE
:
5459 case ZFS_DELEG_USER_SETS
:
5460 case ZFS_DELEG_USER
:
5463 case ZFS_DELEG_GROUP_SETS
:
5464 case ZFS_DELEG_GROUP
:
5467 case ZFS_DELEG_EVERYONE_SETS
:
5468 case ZFS_DELEG_EVERYONE
:
5479 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
5482 const who_perm_node_t
*l
= larg
;
5483 const who_perm_node_t
*r
= rarg
;
5484 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
5485 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
5486 int lweight
= who_type2weight(ltype
);
5487 int rweight
= who_type2weight(rtype
);
5488 int res
= lweight
- rweight
;
5490 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
5491 ZFS_MAX_DELEG_NAME
-1);
5502 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
5505 const deleg_perm_node_t
*l
= larg
;
5506 const deleg_perm_node_t
*r
= rarg
;
5507 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
5508 ZFS_MAX_DELEG_NAME
-1);
5520 fs_perm_set_init(fs_perm_set_t
*fspset
)
5522 memset(fspset
, 0, sizeof (fs_perm_set_t
));
5524 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
5525 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
5526 NULL
, UU_DEFAULT
)) == NULL
)
5528 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
5529 UU_DEFAULT
)) == NULL
)
5532 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
5533 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
5534 who_perm_node_t
, who_avl_node
), who_perm_compare
,
5535 UU_DEFAULT
)) == NULL
)
5538 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
5539 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
5540 who_perm_node_t
, who_avl_node
), who_perm_compare
,
5541 UU_DEFAULT
)) == NULL
)
5544 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
5545 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
5546 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
5551 static inline void fs_perm_fini(fs_perm_t
*);
5552 static inline void who_perm_fini(who_perm_t
*);
5555 fs_perm_set_fini(fs_perm_set_t
*fspset
)
5557 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
5559 while (node
!= NULL
) {
5560 fs_perm_node_t
*next_node
=
5561 uu_list_next(fspset
->fsps_list
, node
);
5562 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
5563 fs_perm_fini(fsperm
);
5564 uu_list_remove(fspset
->fsps_list
, node
);
5569 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
5570 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
5571 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
5575 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
5578 deleg_perm
->dp_who_type
= type
;
5579 deleg_perm
->dp_name
= name
;
5583 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
5584 zfs_deleg_who_type_t type
, const char *name
)
5586 uu_avl_pool_t
*pool
;
5587 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
5589 memset(who_perm
, 0, sizeof (who_perm_t
));
5591 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
5592 UU_DEFAULT
)) == NULL
)
5595 who_perm
->who_type
= type
;
5596 who_perm
->who_name
= name
;
5597 who_perm
->who_fsperm
= fsperm
;
5601 who_perm_fini(who_perm_t
*who_perm
)
5603 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
5605 while (node
!= NULL
) {
5606 deleg_perm_node_t
*next_node
=
5607 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
5609 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
5614 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
5618 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
5620 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
5621 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
5623 memset(fsperm
, 0, sizeof (fs_perm_t
));
5625 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
5629 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
5633 fsperm
->fsp_set
= fspset
;
5634 fsperm
->fsp_name
= fsname
;
5638 fs_perm_fini(fs_perm_t
*fsperm
)
5640 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
5641 while (node
!= NULL
) {
5642 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
5644 who_perm_t
*who_perm
= &node
->who_perm
;
5645 who_perm_fini(who_perm
);
5646 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
5651 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
5652 while (node
!= NULL
) {
5653 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
5655 who_perm_t
*who_perm
= &node
->who_perm
;
5656 who_perm_fini(who_perm
);
5657 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
5662 uu_avl_destroy(fsperm
->fsp_sc_avl
);
5663 uu_avl_destroy(fsperm
->fsp_uge_avl
);
5667 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
5668 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
5670 uu_avl_index_t idx
= 0;
5672 deleg_perm_node_t
*found_node
= NULL
;
5673 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
5675 deleg_perm_init(deleg_perm
, who_type
, name
);
5677 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
5679 uu_avl_insert(avl
, node
, idx
);
5682 deleg_perm
= &node
->dpn_perm
;
5687 case ZFS_DELEG_LOCAL
:
5688 deleg_perm
->dp_local
= B_TRUE
;
5690 case ZFS_DELEG_DESCENDENT
:
5691 deleg_perm
->dp_descend
= B_TRUE
;
5696 assert(B_FALSE
); /* invalid locality */
5701 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
5703 nvpair_t
*nvp
= NULL
;
5704 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
5705 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
5706 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
5708 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5709 const char *name
= nvpair_name(nvp
);
5710 data_type_t type
= nvpair_type(nvp
);
5711 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
5712 deleg_perm_node_t
*node
=
5713 safe_malloc(sizeof (deleg_perm_node_t
));
5715 VERIFY(type
== DATA_TYPE_BOOLEAN
);
5717 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
5718 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
5725 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
5727 nvpair_t
*nvp
= NULL
;
5728 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
5730 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5731 nvlist_t
*nvl2
= NULL
;
5732 const char *name
= nvpair_name(nvp
);
5733 uu_avl_t
*avl
= NULL
;
5734 uu_avl_pool_t
*avl_pool
= NULL
;
5735 zfs_deleg_who_type_t perm_type
= name
[0];
5736 char perm_locality
= name
[1];
5737 const char *perm_name
= name
+ 3;
5738 who_perm_t
*who_perm
= NULL
;
5740 assert('$' == name
[2]);
5742 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
5745 switch (perm_type
) {
5746 case ZFS_DELEG_CREATE
:
5747 case ZFS_DELEG_CREATE_SETS
:
5748 case ZFS_DELEG_NAMED_SET
:
5749 case ZFS_DELEG_NAMED_SET_SETS
:
5750 avl_pool
= fspset
->fsps_named_set_avl_pool
;
5751 avl
= fsperm
->fsp_sc_avl
;
5753 case ZFS_DELEG_USER
:
5754 case ZFS_DELEG_USER_SETS
:
5755 case ZFS_DELEG_GROUP
:
5756 case ZFS_DELEG_GROUP_SETS
:
5757 case ZFS_DELEG_EVERYONE
:
5758 case ZFS_DELEG_EVERYONE_SETS
:
5759 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
5760 avl
= fsperm
->fsp_uge_avl
;
5764 assert(!"unhandled zfs_deleg_who_type_t");
5767 who_perm_node_t
*found_node
= NULL
;
5768 who_perm_node_t
*node
= safe_malloc(
5769 sizeof (who_perm_node_t
));
5770 who_perm
= &node
->who_perm
;
5771 uu_avl_index_t idx
= 0;
5773 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
5774 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
5776 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
5778 if (avl
== fsperm
->fsp_uge_avl
) {
5780 struct passwd
*p
= NULL
;
5781 struct group
*g
= NULL
;
5782 const char *nice_name
= NULL
;
5784 switch (perm_type
) {
5785 case ZFS_DELEG_USER_SETS
:
5786 case ZFS_DELEG_USER
:
5787 rid
= atoi(perm_name
);
5790 nice_name
= p
->pw_name
;
5792 case ZFS_DELEG_GROUP_SETS
:
5793 case ZFS_DELEG_GROUP
:
5794 rid
= atoi(perm_name
);
5797 nice_name
= g
->gr_name
;
5804 if (nice_name
!= NULL
) {
5806 node
->who_perm
.who_ug_name
,
5809 /* User or group unknown */
5811 node
->who_perm
.who_ug_name
,
5812 sizeof (node
->who_perm
.who_ug_name
),
5813 "(unknown: %d)", rid
);
5817 uu_avl_insert(avl
, node
, idx
);
5820 who_perm
= &node
->who_perm
;
5823 assert(who_perm
!= NULL
);
5824 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
5831 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
5833 nvpair_t
*nvp
= NULL
;
5834 uu_avl_index_t idx
= 0;
5836 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5837 nvlist_t
*nvl2
= NULL
;
5838 const char *fsname
= nvpair_name(nvp
);
5839 data_type_t type
= nvpair_type(nvp
);
5840 fs_perm_t
*fsperm
= NULL
;
5841 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
5843 fsperm
= &node
->fspn_fsperm
;
5845 VERIFY(DATA_TYPE_NVLIST
== type
);
5847 uu_list_node_init(node
, &node
->fspn_list_node
,
5848 fspset
->fsps_list_pool
);
5850 idx
= uu_list_numnodes(fspset
->fsps_list
);
5851 fs_perm_init(fsperm
, fspset
, fsname
);
5853 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
5856 (void) parse_fs_perm(fsperm
, nvl2
);
5858 uu_list_insert(fspset
->fsps_list
, node
, idx
);
5864 static inline const char *
5865 deleg_perm_comment(zfs_deleg_note_t note
)
5867 const char *str
= "";
5872 case ZFS_DELEG_NOTE_ALLOW
:
5873 str
= gettext("Must also have the permission that is being"
5874 "\n\t\t\t\tallowed");
5876 case ZFS_DELEG_NOTE_CLONE
:
5877 str
= gettext("Must also have the 'create' ability and 'mount'"
5878 "\n\t\t\t\tability in the origin file system");
5880 case ZFS_DELEG_NOTE_CREATE
:
5881 str
= gettext("Must also have the 'mount' ability");
5883 case ZFS_DELEG_NOTE_DESTROY
:
5884 str
= gettext("Must also have the 'mount' ability");
5886 case ZFS_DELEG_NOTE_DIFF
:
5887 str
= gettext("Allows lookup of paths within a dataset;"
5888 "\n\t\t\t\tgiven an object number. Ordinary users need this"
5889 "\n\t\t\t\tin order to use zfs diff");
5891 case ZFS_DELEG_NOTE_HOLD
:
5892 str
= gettext("Allows adding a user hold to a snapshot");
5894 case ZFS_DELEG_NOTE_MOUNT
:
5895 str
= gettext("Allows mount/umount of ZFS datasets");
5897 case ZFS_DELEG_NOTE_PROMOTE
:
5898 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
5899 " 'promote' ability in the origin file system");
5901 case ZFS_DELEG_NOTE_RECEIVE
:
5902 str
= gettext("Must also have the 'mount' and 'create'"
5905 case ZFS_DELEG_NOTE_RELEASE
:
5906 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
5907 "might destroy the snapshot");
5909 case ZFS_DELEG_NOTE_RENAME
:
5910 str
= gettext("Must also have the 'mount' and 'create'"
5911 "\n\t\t\t\tability in the new parent");
5913 case ZFS_DELEG_NOTE_ROLLBACK
:
5916 case ZFS_DELEG_NOTE_SEND
:
5919 case ZFS_DELEG_NOTE_SHARE
:
5920 str
= gettext("Allows sharing file systems over NFS or SMB"
5921 "\n\t\t\t\tprotocols");
5923 case ZFS_DELEG_NOTE_SNAPSHOT
:
5926 case ZFS_DELEG_NOTE_LOAD_KEY
:
5927 str
= gettext("Allows loading or unloading an encryption key");
5929 case ZFS_DELEG_NOTE_CHANGE_KEY
:
5930 str
= gettext("Allows changing or adding an encryption key");
5933 * case ZFS_DELEG_NOTE_VSCAN:
5934 * str = gettext("");
5938 case ZFS_DELEG_NOTE_GROUPQUOTA
:
5939 str
= gettext("Allows accessing any groupquota@... property");
5941 case ZFS_DELEG_NOTE_GROUPUSED
:
5942 str
= gettext("Allows reading any groupused@... property");
5944 case ZFS_DELEG_NOTE_USERPROP
:
5945 str
= gettext("Allows changing any user property");
5947 case ZFS_DELEG_NOTE_USERQUOTA
:
5948 str
= gettext("Allows accessing any userquota@... property");
5950 case ZFS_DELEG_NOTE_USERUSED
:
5951 str
= gettext("Allows reading any userused@... property");
5953 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
5954 str
= gettext("Allows accessing any userobjquota@... property");
5956 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
5957 str
= gettext("Allows accessing any \n\t\t\t\t"
5958 "groupobjquota@... property");
5960 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
5961 str
= gettext("Allows reading any groupobjused@... property");
5963 case ZFS_DELEG_NOTE_USEROBJUSED
:
5964 str
= gettext("Allows reading any userobjused@... property");
5966 case ZFS_DELEG_NOTE_PROJECTQUOTA
:
5967 str
= gettext("Allows accessing any projectquota@... property");
5969 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA
:
5970 str
= gettext("Allows accessing any \n\t\t\t\t"
5971 "projectobjquota@... property");
5973 case ZFS_DELEG_NOTE_PROJECTUSED
:
5974 str
= gettext("Allows reading any projectused@... property");
5976 case ZFS_DELEG_NOTE_PROJECTOBJUSED
:
5977 str
= gettext("Allows accessing any \n\t\t\t\t"
5978 "projectobjused@... property");
5996 boolean_t recursive
; /* unallow only */
5997 boolean_t prt_usage
;
5999 boolean_t prt_perms
;
6002 const char *dataset
;
6006 prop_cmp(const void *a
, const void *b
)
6008 const char *str1
= *(const char **)a
;
6009 const char *str2
= *(const char **)b
;
6010 return (strcmp(str1
, str2
));
6014 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
6016 const char *opt_desc
[] = {
6017 "-h", gettext("show this help message and exit"),
6018 "-l", gettext("set permission locally"),
6019 "-d", gettext("set permission for descents"),
6020 "-u", gettext("set permission for user"),
6021 "-g", gettext("set permission for group"),
6022 "-e", gettext("set permission for everyone"),
6023 "-c", gettext("set create time permission"),
6024 "-s", gettext("define permission set"),
6026 "-r", gettext("remove permissions recursively"),
6028 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
6029 size_t allow_size
= unallow_size
- 2;
6030 const char *props
[ZFS_NUM_PROPS
];
6033 FILE *fp
= requested
? stdout
: stderr
;
6034 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
6035 const char *fmt
= gettext("%-16s %-14s\t%s\n");
6037 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
6039 (void) fprintf(fp
, gettext("Options:\n"));
6040 for (i
= 0; i
< (un
? unallow_size
: allow_size
); i
+= 2) {
6041 const char *opt
= opt_desc
[i
];
6042 const char *optdsc
= opt_desc
[i
+ 1];
6043 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
6046 (void) fprintf(fp
, gettext("\nThe following permissions are "
6048 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
6050 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
6051 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
6052 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
6053 const char *perm_type
= deleg_perm_type(perm_note
);
6054 const char *perm_comment
= deleg_perm_comment(perm_note
);
6055 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
6058 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
6059 zprop_desc_t
*pd
= &pdtbl
[i
];
6060 if (pd
->pd_visible
!= B_TRUE
)
6063 if (pd
->pd_attr
== PROP_READONLY
)
6066 props
[count
++] = pd
->pd_name
;
6068 props
[count
] = NULL
;
6070 qsort(props
, count
, sizeof (char *), prop_cmp
);
6072 for (i
= 0; i
< count
; i
++)
6073 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
6076 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
6078 exit(requested
? 0 : 2);
6081 static inline const char *
6082 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
6085 if (un
&& argc
== expected_argc
- 1)
6087 else if (argc
== expected_argc
)
6088 *permsp
= argv
[argc
- 2];
6090 allow_usage(un
, B_FALSE
,
6091 gettext("wrong number of parameters\n"));
6093 return (argv
[argc
- 1]);
6097 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
6099 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
6100 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
6101 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
6102 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
6105 allow_usage(un
, B_FALSE
,
6106 gettext("-u, -g, and -e are mutually exclusive\n"));
6108 if (opts
->prt_usage
) {
6109 if (argc
== 0 && all_sum
== 0)
6110 allow_usage(un
, B_TRUE
, NULL
);
6117 allow_usage(un
, B_FALSE
,
6118 gettext("invalid options combined with -s\n"));
6120 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
6121 if (argv
[0][0] != '@')
6122 allow_usage(un
, B_FALSE
,
6123 gettext("invalid set name: missing '@' prefix\n"));
6124 opts
->who
= argv
[0];
6125 } else if (opts
->create
) {
6126 if (ldcsuge_sum
> 1)
6127 allow_usage(un
, B_FALSE
,
6128 gettext("invalid options combined with -c\n"));
6129 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
6130 } else if (opts
->everyone
) {
6132 allow_usage(un
, B_FALSE
,
6133 gettext("invalid options combined with -e\n"));
6134 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
6135 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
6137 opts
->everyone
= B_TRUE
;
6140 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
6141 } else if (argc
== 1 && !un
) {
6142 opts
->prt_perms
= B_TRUE
;
6143 opts
->dataset
= argv
[argc
-1];
6145 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
6146 opts
->who
= argv
[0];
6149 if (!opts
->local
&& !opts
->descend
) {
6150 opts
->local
= B_TRUE
;
6151 opts
->descend
= B_TRUE
;
6156 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
6157 const char *who
, char *perms
, nvlist_t
*top_nvl
)
6160 char ld
[2] = { '\0', '\0' };
6161 char who_buf
[MAXNAMELEN
+ 32];
6162 char base_type
= '\0';
6163 char set_type
= '\0';
6164 nvlist_t
*base_nvl
= NULL
;
6165 nvlist_t
*set_nvl
= NULL
;
6168 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
6170 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
6174 case ZFS_DELEG_NAMED_SET_SETS
:
6175 case ZFS_DELEG_NAMED_SET
:
6176 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
6177 base_type
= ZFS_DELEG_NAMED_SET
;
6178 ld
[0] = ZFS_DELEG_NA
;
6180 case ZFS_DELEG_CREATE_SETS
:
6181 case ZFS_DELEG_CREATE
:
6182 set_type
= ZFS_DELEG_CREATE_SETS
;
6183 base_type
= ZFS_DELEG_CREATE
;
6184 ld
[0] = ZFS_DELEG_NA
;
6186 case ZFS_DELEG_USER_SETS
:
6187 case ZFS_DELEG_USER
:
6188 set_type
= ZFS_DELEG_USER_SETS
;
6189 base_type
= ZFS_DELEG_USER
;
6191 ld
[0] = ZFS_DELEG_LOCAL
;
6193 ld
[1] = ZFS_DELEG_DESCENDENT
;
6195 case ZFS_DELEG_GROUP_SETS
:
6196 case ZFS_DELEG_GROUP
:
6197 set_type
= ZFS_DELEG_GROUP_SETS
;
6198 base_type
= ZFS_DELEG_GROUP
;
6200 ld
[0] = ZFS_DELEG_LOCAL
;
6202 ld
[1] = ZFS_DELEG_DESCENDENT
;
6204 case ZFS_DELEG_EVERYONE_SETS
:
6205 case ZFS_DELEG_EVERYONE
:
6206 set_type
= ZFS_DELEG_EVERYONE_SETS
;
6207 base_type
= ZFS_DELEG_EVERYONE
;
6209 ld
[0] = ZFS_DELEG_LOCAL
;
6211 ld
[1] = ZFS_DELEG_DESCENDENT
;
6215 assert(set_type
!= '\0' && base_type
!= '\0');
6218 if (perms
!= NULL
) {
6220 char *end
= curr
+ strlen(perms
);
6222 while (curr
< end
) {
6223 char *delim
= strchr(curr
, ',');
6234 (void) nvlist_add_boolean(nvl
, curr
);
6240 for (i
= 0; i
< 2; i
++) {
6241 char locality
= ld
[i
];
6245 if (!nvlist_empty(base_nvl
)) {
6247 (void) snprintf(who_buf
,
6248 sizeof (who_buf
), "%c%c$%s",
6249 base_type
, locality
, who
);
6251 (void) snprintf(who_buf
,
6252 sizeof (who_buf
), "%c%c$",
6253 base_type
, locality
);
6255 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
6260 if (!nvlist_empty(set_nvl
)) {
6262 (void) snprintf(who_buf
,
6263 sizeof (who_buf
), "%c%c$%s",
6264 set_type
, locality
, who
);
6266 (void) snprintf(who_buf
,
6267 sizeof (who_buf
), "%c%c$",
6268 set_type
, locality
);
6270 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
6275 for (i
= 0; i
< 2; i
++) {
6276 char locality
= ld
[i
];
6281 (void) snprintf(who_buf
, sizeof (who_buf
),
6282 "%c%c$%s", base_type
, locality
, who
);
6284 (void) snprintf(who_buf
, sizeof (who_buf
),
6285 "%c%c$", base_type
, locality
);
6286 (void) nvlist_add_boolean(top_nvl
, who_buf
);
6289 (void) snprintf(who_buf
, sizeof (who_buf
),
6290 "%c%c$%s", set_type
, locality
, who
);
6292 (void) snprintf(who_buf
, sizeof (who_buf
),
6293 "%c%c$", set_type
, locality
);
6294 (void) nvlist_add_boolean(top_nvl
, who_buf
);
6300 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
6302 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
6306 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
6307 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
6308 } else if (opts
->create
) {
6309 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
6310 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
6311 } else if (opts
->everyone
) {
6312 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
6313 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
6315 char *curr
= opts
->who
;
6316 char *end
= curr
+ strlen(curr
);
6318 while (curr
< end
) {
6320 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
6322 char *delim
= strchr(curr
, ',');
6325 struct passwd
*p
= NULL
;
6326 struct group
*g
= NULL
;
6334 rid
= (uid_t
)strtol(curr
, &endch
, 0);
6336 who_type
= ZFS_DELEG_USER
;
6344 else if (*endch
!= '\0') {
6345 (void) snprintf(errbuf
, sizeof (errbuf
),
6346 gettext("invalid user %s\n"), curr
);
6347 allow_usage(un
, B_TRUE
, errbuf
);
6349 } else if (opts
->group
) {
6350 who_type
= ZFS_DELEG_GROUP
;
6358 else if (*endch
!= '\0') {
6359 (void) snprintf(errbuf
, sizeof (errbuf
),
6360 gettext("invalid group %s\n"),
6362 allow_usage(un
, B_TRUE
, errbuf
);
6365 if (*endch
!= '\0') {
6372 if (*endch
!= '\0') {
6380 who_type
= ZFS_DELEG_USER
;
6382 } else if (g
!= NULL
) {
6383 who_type
= ZFS_DELEG_GROUP
;
6386 (void) snprintf(errbuf
, sizeof (errbuf
),
6387 gettext("invalid user/group %s\n"),
6389 allow_usage(un
, B_TRUE
, errbuf
);
6393 (void) sprintf(id
, "%u", rid
);
6396 store_allow_perm(who_type
, opts
->local
,
6397 opts
->descend
, who
, opts
->perms
, *nvlp
);
6406 print_set_creat_perms(uu_avl_t
*who_avl
)
6408 const char *sc_title
[] = {
6409 gettext("Permission sets:\n"),
6410 gettext("Create time permissions:\n"),
6413 who_perm_node_t
*who_node
= NULL
;
6414 int prev_weight
= -1;
6416 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
6417 who_node
= uu_avl_next(who_avl
, who_node
)) {
6418 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
6419 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
6420 const char *who_name
= who_node
->who_perm
.who_name
;
6421 int weight
= who_type2weight(who_type
);
6422 boolean_t first
= B_TRUE
;
6423 deleg_perm_node_t
*deleg_node
;
6425 if (prev_weight
!= weight
) {
6426 (void) printf("%s", sc_title
[weight
]);
6427 prev_weight
= weight
;
6430 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
6431 (void) printf("\t");
6433 (void) printf("\t%s ", who_name
);
6435 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
6436 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
6439 deleg_node
->dpn_perm
.dp_name
);
6442 (void) printf(",%s",
6443 deleg_node
->dpn_perm
.dp_name
);
6446 (void) printf("\n");
6451 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
6454 who_perm_node_t
*who_node
= NULL
;
6455 boolean_t prt_title
= B_TRUE
;
6456 uu_avl_walk_t
*walk
;
6458 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
6461 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
6462 const char *who_name
= who_node
->who_perm
.who_name
;
6463 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
6464 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
6465 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
6467 deleg_perm_node_t
*deleg_node
;
6468 boolean_t prt_who
= B_TRUE
;
6470 for (deleg_node
= uu_avl_first(avl
);
6472 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
6473 if (local
!= deleg_node
->dpn_perm
.dp_local
||
6474 descend
!= deleg_node
->dpn_perm
.dp_descend
)
6478 const char *who
= NULL
;
6480 prt_title
= B_FALSE
;
6481 (void) printf("%s", title
);
6485 case ZFS_DELEG_USER_SETS
:
6486 case ZFS_DELEG_USER
:
6487 who
= gettext("user");
6489 who_name
= nice_who_name
;
6491 case ZFS_DELEG_GROUP_SETS
:
6492 case ZFS_DELEG_GROUP
:
6493 who
= gettext("group");
6495 who_name
= nice_who_name
;
6497 case ZFS_DELEG_EVERYONE_SETS
:
6498 case ZFS_DELEG_EVERYONE
:
6499 who
= gettext("everyone");
6504 assert(who
!= NULL
);
6508 if (who_name
== NULL
)
6509 (void) printf("\t%s", who
);
6511 (void) printf("\t%s %s", who
, who_name
);
6514 (void) printf("%c%s", delim
,
6515 deleg_node
->dpn_perm
.dp_name
);
6520 (void) printf("\n");
6523 uu_avl_walk_end(walk
);
6527 print_fs_perms(fs_perm_set_t
*fspset
)
6529 fs_perm_node_t
*node
= NULL
;
6530 char buf
[MAXNAMELEN
+ 32];
6531 const char *dsname
= buf
;
6533 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
6534 node
= uu_list_next(fspset
->fsps_list
, node
)) {
6535 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
6536 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
6539 (void) snprintf(buf
, sizeof (buf
),
6540 gettext("---- Permissions on %s "),
6541 node
->fspn_fsperm
.fsp_name
);
6542 (void) printf("%s", dsname
);
6543 left
= 70 - strlen(buf
);
6546 (void) printf("\n");
6548 print_set_creat_perms(sc_avl
);
6549 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
6550 gettext("Local permissions:\n"));
6551 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
6552 gettext("Descendent permissions:\n"));
6553 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
6554 gettext("Local+Descendent permissions:\n"));
6558 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
6560 struct deleg_perms
{
6566 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
6568 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
6569 zfs_type_t zfs_type
= zfs_get_type(zhp
);
6571 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
6574 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
6578 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
6581 nvlist_t
*perm_nvl
= NULL
;
6582 nvlist_t
*update_perm_nvl
= NULL
;
6585 struct allow_opts opts
= { 0 };
6587 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
6590 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
6593 opts
.local
= B_TRUE
;
6596 opts
.descend
= B_TRUE
;
6602 opts
.group
= B_TRUE
;
6605 opts
.everyone
= B_TRUE
;
6611 opts
.create
= B_TRUE
;
6614 opts
.recursive
= B_TRUE
;
6617 (void) fprintf(stderr
, gettext("missing argument for "
6618 "'%c' option\n"), optopt
);
6622 opts
.prt_usage
= B_TRUE
;
6625 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6634 /* check arguments */
6635 parse_allow_args(argc
, argv
, un
, &opts
);
6637 /* try to open the dataset */
6638 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
6639 ZFS_TYPE_VOLUME
)) == NULL
) {
6640 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
6645 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
6648 fs_perm_set_init(&fs_perm_set
);
6649 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
6650 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
6655 print_fs_perms(&fs_perm_set
);
6657 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
6658 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
6661 if (un
&& opts
.recursive
) {
6662 struct deleg_perms data
= { un
, update_perm_nvl
};
6663 if (zfs_iter_filesystems_v2(zhp
, 0, set_deleg_perms
,
6672 nvlist_free(perm_nvl
);
6673 nvlist_free(update_perm_nvl
);
6675 fs_perm_set_fini(&fs_perm_set
);
6683 zfs_do_allow(int argc
, char **argv
)
6685 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
6689 zfs_do_unallow(int argc
, char **argv
)
6691 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
6695 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
6700 boolean_t recursive
= B_FALSE
;
6701 const char *opts
= holding
? "rt" : "r";
6705 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
6711 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6720 /* check number of arguments */
6728 if (holding
&& tag
[0] == '.') {
6729 /* tags starting with '.' are reserved for libzfs */
6730 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
6734 for (i
= 0; i
< argc
; ++i
) {
6736 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
6738 char *path
= argv
[i
];
6740 delim
= strchr(path
, '@');
6741 if (delim
== NULL
) {
6742 (void) fprintf(stderr
,
6743 gettext("'%s' is not a snapshot\n"), path
);
6747 (void) strlcpy(parent
, path
, MIN(sizeof (parent
),
6750 zhp
= zfs_open(g_zfs
, parent
,
6751 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
6757 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
6760 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
6766 return (errors
!= 0);
6770 * zfs hold [-r] [-t] <tag> <snap> ...
6772 * -r Recursively hold
6774 * Apply a user-hold with the given tag to the list of snapshots.
6777 zfs_do_hold(int argc
, char **argv
)
6779 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
6783 * zfs release [-r] <tag> <snap> ...
6785 * -r Recursively release
6787 * Release a user-hold with the given tag from the list of snapshots.
6790 zfs_do_release(int argc
, char **argv
)
6792 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
6795 typedef struct holds_cbdata
{
6796 boolean_t cb_recursive
;
6797 const char *cb_snapname
;
6799 size_t cb_max_namelen
;
6800 size_t cb_max_taglen
;
6803 #define STRFTIME_FMT_STR "%a %b %e %H:%M %Y"
6804 #define DATETIME_BUF_LEN (32)
6809 print_holds(boolean_t scripted
, int nwidth
, int tagwidth
, nvlist_t
*nvl
,
6813 nvpair_t
*nvp
= NULL
;
6814 const char *const hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
6818 for (i
= 0; i
< 3; i
++) {
6819 col
= gettext(hdr_cols
[i
]);
6821 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
6824 (void) printf("%s\n", col
);
6828 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
6829 const char *zname
= nvpair_name(nvp
);
6831 nvpair_t
*nvp2
= NULL
;
6832 (void) nvpair_value_nvlist(nvp
, &nvl2
);
6833 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
6834 char tsbuf
[DATETIME_BUF_LEN
];
6835 const char *tagname
= nvpair_name(nvp2
);
6840 (void) nvpair_value_uint64(nvp2
, &val
);
6842 (void) localtime_r(&time
, &t
);
6843 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
6844 gettext(STRFTIME_FMT_STR
), &t
);
6848 (void) printf("%s\t%s\t%ld\n", zname
,
6851 (void) printf("%s\t%s\t%s\n", zname
,
6856 (void) printf("%-*s %-*s %ld\n",
6857 nwidth
, zname
, tagwidth
,
6860 (void) printf("%-*s %-*s %s\n",
6861 nwidth
, zname
, tagwidth
,
6870 * Generic callback function to list a dataset or snapshot.
6873 holds_callback(zfs_handle_t
*zhp
, void *data
)
6875 holds_cbdata_t
*cbp
= data
;
6876 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
6877 nvlist_t
*nvl
= NULL
;
6878 nvpair_t
*nvp
= NULL
;
6879 const char *zname
= zfs_get_name(zhp
);
6880 size_t znamelen
= strlen(zname
);
6882 if (cbp
->cb_recursive
) {
6883 const char *snapname
;
6884 char *delim
= strchr(zname
, '@');
6888 snapname
= delim
+ 1;
6889 if (strcmp(cbp
->cb_snapname
, snapname
))
6893 if (zfs_get_holds(zhp
, &nvl
) != 0)
6896 if (znamelen
> cbp
->cb_max_namelen
)
6897 cbp
->cb_max_namelen
= znamelen
;
6899 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
6900 const char *tag
= nvpair_name(nvp
);
6901 size_t taglen
= strlen(tag
);
6902 if (taglen
> cbp
->cb_max_taglen
)
6903 cbp
->cb_max_taglen
= taglen
;
6906 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
6910 * zfs holds [-rHp] <snap> ...
6912 * -r Lists holds that are set on the named snapshots recursively.
6913 * -H Scripted mode; elide headers and separate columns by tabs.
6914 * -p Display values in parsable (literal) format.
6917 zfs_do_holds(int argc
, char **argv
)
6920 boolean_t errors
= B_FALSE
;
6921 boolean_t scripted
= B_FALSE
;
6922 boolean_t recursive
= B_FALSE
;
6923 boolean_t parsable
= B_FALSE
;
6925 int types
= ZFS_TYPE_SNAPSHOT
;
6926 holds_cbdata_t cb
= { 0 };
6933 while ((c
= getopt(argc
, argv
, "rHp")) != -1) {
6945 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6952 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
6953 flags
|= ZFS_ITER_RECURSE
;
6959 /* check number of arguments */
6963 nvlist_t
*nvl
= fnvlist_alloc();
6965 for (int i
= 0; i
< argc
; ++i
) {
6966 char *snapshot
= argv
[i
];
6968 const char *snapname
;
6970 delim
= strchr(snapshot
, '@');
6971 if (delim
== NULL
) {
6972 (void) fprintf(stderr
,
6973 gettext("'%s' is not a snapshot\n"), snapshot
);
6977 snapname
= delim
+ 1;
6979 snapshot
[delim
- snapshot
] = '\0';
6981 cb
.cb_recursive
= recursive
;
6982 cb
.cb_snapname
= snapname
;
6986 * 1. collect holds data, set format options
6988 ret
= zfs_for_each(1, argv
+ i
, flags
, types
, NULL
, NULL
, limit
,
6989 holds_callback
, &cb
);
6995 * 2. print holds data
6997 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
,
7000 if (nvlist_empty(nvl
))
7001 (void) fprintf(stderr
, gettext("no datasets available\n"));
7008 #define CHECK_SPINNER 30
7009 #define SPINNER_TIME 3 /* seconds */
7010 #define MOUNT_TIME 1 /* seconds */
7012 typedef struct get_all_state
{
7015 boolean_t ga_verbose
;
7016 get_all_cb_t
*ga_cbp
;
7020 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
7022 static const char *const spin
[] = { "-", "\\", "|", "/" };
7023 static int spinval
= 0;
7024 static int spincheck
= 0;
7025 static time_t last_spin_time
= (time_t)0;
7026 get_all_state_t
*state
= data
;
7027 zfs_type_t type
= zfs_get_type(zhp
);
7029 if (state
->ga_verbose
) {
7030 if (--spincheck
< 0) {
7031 time_t now
= time(NULL
);
7032 if (last_spin_time
+ SPINNER_TIME
< now
) {
7033 update_progress(spin
[spinval
++ % 4]);
7034 last_spin_time
= now
;
7036 spincheck
= CHECK_SPINNER
;
7041 * Iterate over any nested datasets.
7043 if (zfs_iter_filesystems_v2(zhp
, 0, get_one_dataset
, data
) != 0) {
7049 * Skip any datasets whose type does not match.
7051 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
7055 libzfs_add_handle(state
->ga_cbp
, zhp
);
7056 assert(state
->ga_cbp
->cb_used
<= state
->ga_cbp
->cb_alloc
);
7062 get_recursive_datasets(zfs_handle_t
*zhp
, void *data
)
7064 get_all_state_t
*state
= data
;
7065 int len
= strlen(zfs_get_name(zhp
));
7066 for (int i
= 0; i
< state
->ga_count
; ++i
) {
7067 if (strcmp(state
->ga_datasets
[i
], zfs_get_name(zhp
)) == 0)
7068 return (get_one_dataset(zhp
, data
));
7069 else if ((strncmp(state
->ga_datasets
[i
], zfs_get_name(zhp
),
7070 len
) == 0) && state
->ga_datasets
[i
][len
] == '/') {
7071 (void) zfs_iter_filesystems_v2(zhp
, 0,
7072 get_recursive_datasets
, data
);
7080 get_all_datasets(get_all_state_t
*state
)
7082 if (state
->ga_verbose
)
7083 set_progress_header(gettext("Reading ZFS config"));
7084 if (state
->ga_datasets
== NULL
)
7085 (void) zfs_iter_root(g_zfs
, get_one_dataset
, state
);
7087 (void) zfs_iter_root(g_zfs
, get_recursive_datasets
, state
);
7089 if (state
->ga_verbose
)
7090 finish_progress(gettext("done."));
7094 * Generic callback for sharing or mounting filesystems. Because the code is so
7095 * similar, we have a common function with an extra parameter to determine which
7096 * mode we are using.
7098 typedef enum { OP_SHARE
, OP_MOUNT
} share_mount_op_t
;
7100 typedef struct share_mount_state
{
7101 share_mount_op_t sm_op
;
7102 boolean_t sm_verbose
;
7105 enum sa_protocol sm_proto
; /* only valid for OP_SHARE */
7106 pthread_mutex_t sm_lock
; /* protects the remaining fields */
7107 uint_t sm_total
; /* number of filesystems to process */
7108 uint_t sm_done
; /* number of filesystems processed */
7109 int sm_status
; /* -1 if any of the share/mount operations failed */
7110 } share_mount_state_t
;
7113 * Share or mount a dataset.
7116 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, enum sa_protocol protocol
,
7117 boolean_t
explicit, const char *options
)
7119 char mountpoint
[ZFS_MAXPROPLEN
];
7120 char shareopts
[ZFS_MAXPROPLEN
];
7121 char smbshareopts
[ZFS_MAXPROPLEN
];
7122 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
7124 uint64_t zoned
, canmount
;
7125 boolean_t shared_nfs
, shared_smb
;
7127 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
7130 * Check to make sure we can mount/share this dataset. If we
7131 * are in the global zone and the filesystem is exported to a
7132 * local zone, or if we are in a local zone and the
7133 * filesystem is not exported, then it is an error.
7135 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
7137 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
7141 (void) fprintf(stderr
, gettext("cannot %s '%s': "
7142 "dataset is exported to a local zone\n"), cmdname
,
7146 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
7150 (void) fprintf(stderr
, gettext("cannot %s '%s': "
7151 "permission denied\n"), cmdname
,
7157 * Ignore any filesystems which don't apply to us. This
7158 * includes those with a legacy mountpoint, or those with
7159 * legacy share options.
7161 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
7162 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
7163 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
7164 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
7165 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
7166 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
7168 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
7169 strcmp(smbshareopts
, "off") == 0) {
7173 (void) fprintf(stderr
, gettext("cannot share '%s': "
7174 "legacy share\n"), zfs_get_name(zhp
));
7175 (void) fprintf(stderr
, gettext("use exports(5) or "
7176 "smb.conf(5) to share this filesystem, or set "
7177 "the sharenfs or sharesmb property\n"));
7182 * We cannot share or mount legacy filesystems. If the
7183 * shareopts is non-legacy but the mountpoint is legacy, we
7184 * treat it as a legacy share.
7186 if (strcmp(mountpoint
, "legacy") == 0) {
7190 (void) fprintf(stderr
, gettext("cannot %s '%s': "
7191 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
7192 (void) fprintf(stderr
, gettext("use %s(8) to "
7193 "%s this filesystem\n"), cmdname
, cmdname
);
7197 if (strcmp(mountpoint
, "none") == 0) {
7201 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
7202 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
7207 * canmount explicit outcome
7208 * on no pass through
7209 * on yes pass through
7211 * off yes display error, return 1
7212 * noauto no return 0
7213 * noauto yes pass through
7215 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
7216 if (canmount
== ZFS_CANMOUNT_OFF
) {
7220 (void) fprintf(stderr
, gettext("cannot %s '%s': "
7221 "'canmount' property is set to 'off'\n"), cmdname
,
7224 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
7226 * When performing a 'zfs mount -a', we skip any mounts for
7227 * datasets that have 'noauto' set. Sharing a dataset with
7228 * 'noauto' set is only allowed if it's mounted.
7232 if (op
== OP_SHARE
&& !zfs_is_mounted(zhp
, NULL
)) {
7233 /* also purge it from existing exports */
7234 zfs_unshare(zhp
, mountpoint
, NULL
);
7240 * If this filesystem is encrypted and does not have
7241 * a loaded key, we can not mount it.
7243 if ((flags
& MS_CRYPT
) == 0 &&
7244 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) != ZIO_CRYPT_OFF
&&
7245 zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
) ==
7246 ZFS_KEYSTATUS_UNAVAILABLE
) {
7250 (void) fprintf(stderr
, gettext("cannot %s '%s': "
7251 "encryption key not loaded\n"), cmdname
, zfs_get_name(zhp
));
7256 * If this filesystem is inconsistent and has a receive resume
7257 * token, we can not mount it.
7259 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
7260 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
7261 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
7265 (void) fprintf(stderr
, gettext("cannot %s '%s': "
7266 "Contains partially-completed state from "
7267 "\"zfs receive -s\", which can be resumed with "
7268 "\"zfs send -t\"\n"),
7269 cmdname
, zfs_get_name(zhp
));
7273 if (zfs_prop_get_int(zhp
, ZFS_PROP_REDACTED
) && !(flags
& MS_FORCE
)) {
7277 (void) fprintf(stderr
, gettext("cannot %s '%s': "
7278 "Dataset is not complete, was created by receiving "
7279 "a redacted zfs send stream.\n"), cmdname
,
7285 * At this point, we have verified that the mountpoint and/or
7286 * shareopts are appropriate for auto management. If the
7287 * filesystem is already mounted or shared, return (failing
7288 * for explicit requests); otherwise mount or share the
7293 enum sa_protocol prot
[] = {SA_PROTOCOL_NFS
, SA_NO_PROTOCOL
};
7294 shared_nfs
= zfs_is_shared(zhp
, NULL
, prot
);
7295 *prot
= SA_PROTOCOL_SMB
;
7296 shared_smb
= zfs_is_shared(zhp
, NULL
, prot
);
7298 if ((shared_nfs
&& shared_smb
) ||
7299 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
7300 strcmp(smbshareopts
, "off") == 0) ||
7301 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
7302 strcmp(shareopts
, "off") == 0)) {
7306 (void) fprintf(stderr
, gettext("cannot share "
7307 "'%s': filesystem already shared\n"),
7312 if (!zfs_is_mounted(zhp
, NULL
) &&
7313 zfs_mount(zhp
, NULL
, flags
) != 0)
7317 if (zfs_share(zhp
, protocol
== SA_NO_PROTOCOL
? NULL
: prot
))
7324 mnt
.mnt_mntopts
= (char *)(options
?: "");
7326 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
7327 zfs_is_mounted(zhp
, NULL
)) {
7331 (void) fprintf(stderr
, gettext("cannot mount "
7332 "'%s': filesystem already mounted\n"),
7337 if (zfs_mount(zhp
, options
, flags
) != 0)
7346 * Reports progress in the form "(current/total)". Not thread-safe.
7349 report_mount_progress(int current
, int total
)
7351 static time_t last_progress_time
= 0;
7352 time_t now
= time(NULL
);
7355 /* display header if we're here for the first time */
7357 set_progress_header(gettext("Mounting ZFS filesystems"));
7358 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
7359 /* too soon to report again */
7363 last_progress_time
= now
;
7365 (void) sprintf(info
, "(%d/%d)", current
, total
);
7367 if (current
== total
)
7368 finish_progress(info
);
7370 update_progress(info
);
7374 * zfs_foreach_mountpoint() callback that mounts or shares one filesystem and
7375 * updates the progress meter.
7378 share_mount_one_cb(zfs_handle_t
*zhp
, void *arg
)
7380 share_mount_state_t
*sms
= arg
;
7383 ret
= share_mount_one(zhp
, sms
->sm_op
, sms
->sm_flags
, sms
->sm_proto
,
7384 B_FALSE
, sms
->sm_options
);
7386 pthread_mutex_lock(&sms
->sm_lock
);
7388 sms
->sm_status
= ret
;
7390 if (sms
->sm_verbose
)
7391 report_mount_progress(sms
->sm_done
, sms
->sm_total
);
7392 pthread_mutex_unlock(&sms
->sm_lock
);
7397 append_options(char *mntopts
, char *newopts
)
7399 int len
= strlen(mntopts
);
7401 /* original length plus new string to append plus 1 for the comma */
7402 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
7403 (void) fprintf(stderr
, gettext("the opts argument for "
7404 "'%s' option is too long (more than %d chars)\n"),
7405 "-o", MNT_LINE_MAX
);
7410 mntopts
[len
++] = ',';
7412 (void) strcpy(&mntopts
[len
], newopts
);
7415 static enum sa_protocol
7416 sa_protocol_decode(const char *protocol
)
7418 for (enum sa_protocol i
= 0; i
< ARRAY_SIZE(sa_protocol_names
); ++i
)
7419 if (strcmp(protocol
, sa_protocol_names
[i
]) == 0)
7422 (void) fputs(gettext("share type must be one of: "), stderr
);
7423 for (enum sa_protocol i
= 0;
7424 i
< ARRAY_SIZE(sa_protocol_names
); ++i
)
7425 (void) fprintf(stderr
, "%s%s",
7426 i
!= 0 ? ", " : "", sa_protocol_names
[i
]);
7427 (void) fputc('\n', stderr
);
7432 share_mount(int op
, int argc
, char **argv
)
7436 boolean_t verbose
= B_FALSE
;
7437 boolean_t json
= B_FALSE
;
7439 char *options
= NULL
;
7441 nvlist_t
*jsobj
, *data
, *item
;
7442 const uint_t mount_nthr
= 512;
7444 jsobj
= data
= item
= NULL
;
7446 struct option long_options
[] = {
7447 {"json", no_argument
, NULL
, 'j'},
7452 while ((c
= getopt_long(argc
, argv
,
7453 op
== OP_MOUNT
? ":ajRlvo:Of" : "al",
7454 op
== OP_MOUNT
? long_options
: NULL
, NULL
)) != -1) {
7470 jsobj
= zfs_json_schema(0, 1);
7471 data
= fnvlist_alloc();
7474 if (*optarg
== '\0') {
7475 (void) fprintf(stderr
, gettext("empty mount "
7476 "options (-o) specified\n"));
7480 if (options
== NULL
)
7481 options
= safe_malloc(MNT_LINE_MAX
+ 1);
7483 /* option validation is done later */
7484 append_options(options
, optarg
);
7487 flags
|= MS_OVERLAY
;
7493 (void) fprintf(stderr
, gettext("missing argument for "
7494 "'%c' option\n"), optopt
);
7498 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7507 if (json
&& argc
!= 0) {
7508 (void) fprintf(stderr
, gettext("too many arguments\n"));
7512 /* check number of arguments */
7513 if (do_all
|| recursive
) {
7514 enum sa_protocol protocol
= SA_NO_PROTOCOL
;
7516 if (op
== OP_SHARE
&& argc
> 0) {
7517 protocol
= sa_protocol_decode(argv
[0]);
7522 if (argc
!= 0 && do_all
) {
7523 (void) fprintf(stderr
, gettext("too many arguments\n"));
7527 if (argc
== 0 && recursive
) {
7528 (void) fprintf(stderr
,
7529 gettext("no dataset provided\n"));
7533 start_progress_timer();
7534 get_all_cb_t cb
= { 0 };
7535 get_all_state_t state
= { 0 };
7537 state
.ga_datasets
= NULL
;
7538 state
.ga_count
= -1;
7541 for (int i
= 0; i
< argc
; i
++) {
7542 zhp
= zfs_open(g_zfs
, argv
[i
],
7543 ZFS_TYPE_FILESYSTEM
);
7548 state
.ga_datasets
= argv
;
7549 state
.ga_count
= argc
;
7551 state
.ga_verbose
= verbose
;
7553 get_all_datasets(&state
);
7555 if (cb
.cb_used
== 0) {
7560 share_mount_state_t share_mount_state
= { 0 };
7561 share_mount_state
.sm_op
= op
;
7562 share_mount_state
.sm_verbose
= verbose
;
7563 share_mount_state
.sm_flags
= flags
;
7564 share_mount_state
.sm_options
= options
;
7565 share_mount_state
.sm_proto
= protocol
;
7566 share_mount_state
.sm_total
= cb
.cb_used
;
7567 pthread_mutex_init(&share_mount_state
.sm_lock
, NULL
);
7569 /* For a 'zfs share -a' operation start with a clean slate. */
7571 zfs_truncate_shares(NULL
);
7574 * libshare isn't mt-safe, so only do the operation in parallel
7575 * if we're mounting. Additionally, the key-loading option must
7576 * be serialized so that we can prompt the user for their keys
7577 * in a consistent manner.
7579 nthr
= op
== OP_MOUNT
&& !(flags
& MS_CRYPT
) ? mount_nthr
: 1;
7580 zfs_foreach_mountpoint(g_zfs
, cb
.cb_handles
, cb
.cb_used
,
7581 share_mount_one_cb
, &share_mount_state
, nthr
);
7582 zfs_commit_shares(NULL
);
7584 ret
= share_mount_state
.sm_status
;
7586 for (int i
= 0; i
< cb
.cb_used
; i
++)
7587 zfs_close(cb
.cb_handles
[i
]);
7588 free(cb
.cb_handles
);
7589 } else if (argc
== 0) {
7591 struct mnttab entry
;
7593 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
7594 (void) fprintf(stderr
, gettext("missing filesystem "
7595 "argument (specify -a for all)\n"));
7600 * When mount is given no arguments, go through
7601 * /proc/self/mounts and display any active ZFS mounts.
7602 * We hide any snapshots, since they are controlled
7606 if ((mnttab
= fopen(MNTTAB
, "re")) == NULL
) {
7611 while (getmntent(mnttab
, &entry
) == 0) {
7612 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
7613 strchr(entry
.mnt_special
, '@') != NULL
)
7616 item
= fnvlist_alloc();
7617 fnvlist_add_string(item
, "filesystem",
7619 fnvlist_add_string(item
, "mountpoint",
7621 fnvlist_add_nvlist(data
, entry
.mnt_special
,
7625 (void) printf("%-30s %s\n", entry
.mnt_special
,
7630 (void) fclose(mnttab
);
7632 fnvlist_add_nvlist(jsobj
, "datasets", data
);
7633 if (nvlist_empty(data
))
7634 fnvlist_free(jsobj
);
7636 zcmd_print_json(jsobj
);
7643 (void) fprintf(stderr
,
7644 gettext("too many arguments\n"));
7648 if ((zhp
= zfs_open(g_zfs
, argv
[0],
7649 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
7652 ret
= share_mount_one(zhp
, op
, flags
, SA_NO_PROTOCOL
,
7654 zfs_commit_shares(NULL
);
7665 * zfs mount filesystem
7667 * Mount all filesystems, or mount the given filesystem.
7670 zfs_do_mount(int argc
, char **argv
)
7672 return (share_mount(OP_MOUNT
, argc
, argv
));
7676 * zfs share -a [nfs | smb]
7677 * zfs share filesystem
7679 * Share all filesystems, or share the given filesystem.
7682 zfs_do_share(int argc
, char **argv
)
7684 return (share_mount(OP_SHARE
, argc
, argv
));
7687 typedef struct unshare_unmount_node
{
7688 zfs_handle_t
*un_zhp
;
7690 uu_avl_node_t un_avlnode
;
7691 } unshare_unmount_node_t
;
7694 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
7697 const unshare_unmount_node_t
*l
= larg
;
7698 const unshare_unmount_node_t
*r
= rarg
;
7700 return (strcmp(l
->un_mountp
, r
->un_mountp
));
7704 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
7705 * absolute path, find the entry /proc/self/mounts, verify that it's a
7706 * ZFS filesystem, and unmount it appropriately.
7709 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
7713 struct stat64 statbuf
;
7714 struct extmnttab entry
;
7715 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
7719 * Search for the given (major,minor) pair in the mount table.
7722 if (getextmntent(path
, &entry
, &statbuf
) != 0) {
7723 if (op
== OP_SHARE
) {
7724 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
7725 "currently mounted\n"), cmdname
, path
);
7728 (void) fprintf(stderr
, gettext("warning: %s not in"
7729 "/proc/self/mounts\n"), path
);
7730 if ((ret
= umount2(path
, flags
)) != 0)
7731 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
7735 path_inode
= statbuf
.st_ino
;
7737 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
7738 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
7739 "filesystem\n"), cmdname
, path
);
7743 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
7744 ZFS_TYPE_FILESYSTEM
)) == NULL
)
7748 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
7749 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
7750 cmdname
, path
, strerror(errno
));
7752 } else if (statbuf
.st_ino
!= path_inode
) {
7753 (void) fprintf(stderr
, gettext("cannot "
7754 "%s '%s': not a mountpoint\n"), cmdname
, path
);
7758 if (op
== OP_SHARE
) {
7759 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
7760 char smbshare_prop
[ZFS_MAXPROPLEN
];
7762 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
7763 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
7764 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
7765 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
7767 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
7768 strcmp(smbshare_prop
, "off") == 0) {
7769 (void) fprintf(stderr
, gettext("cannot unshare "
7770 "'%s': legacy share\n"), path
);
7771 (void) fprintf(stderr
, gettext("use exportfs(8) "
7772 "or smbcontrol(1) to unshare this filesystem\n"));
7773 } else if (!zfs_is_shared(zhp
, NULL
, NULL
)) {
7774 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
7775 "not currently shared\n"), path
);
7777 ret
= zfs_unshare(zhp
, path
, NULL
);
7778 zfs_commit_shares(NULL
);
7781 char mtpt_prop
[ZFS_MAXPROPLEN
];
7783 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
7784 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
7787 ret
= zfs_unmount(zhp
, NULL
, flags
);
7788 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
7789 (void) fprintf(stderr
, gettext("cannot unmount "
7790 "'%s': legacy mountpoint\n"),
7792 (void) fprintf(stderr
, gettext("use umount(8) "
7793 "to unmount this filesystem\n"));
7795 ret
= zfs_unmountall(zhp
, flags
);
7806 * Generic callback for unsharing or unmounting a filesystem.
7809 unshare_unmount(int op
, int argc
, char **argv
)
7816 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
7817 char sharesmb
[ZFS_MAXPROPLEN
];
7820 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? ":a" : "afu")) != -1) {
7832 (void) fprintf(stderr
, gettext("missing argument for "
7833 "'%c' option\n"), optopt
);
7837 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7848 * We could make use of zfs_for_each() to walk all datasets in
7849 * the system, but this would be very inefficient, especially
7850 * since we would have to linearly search /proc/self/mounts for
7851 * each one. Instead, do one pass through /proc/self/mounts
7852 * looking for zfs entries and call zfs_unmount() for each one.
7854 * Things get a little tricky if the administrator has created
7855 * mountpoints beneath other ZFS filesystems. In this case, we
7856 * have to unmount the deepest filesystems first. To accomplish
7857 * this, we place all the mountpoints in an AVL tree sorted by
7858 * the special type (dataset name), and walk the result in
7859 * reverse to make sure to get any snapshots first.
7862 struct mnttab entry
;
7863 uu_avl_pool_t
*pool
;
7864 uu_avl_t
*tree
= NULL
;
7865 unshare_unmount_node_t
*node
;
7867 uu_avl_walk_t
*walk
;
7868 enum sa_protocol
*protocol
= NULL
,
7869 single_protocol
[] = {SA_NO_PROTOCOL
, SA_NO_PROTOCOL
};
7871 if (op
== OP_SHARE
&& argc
> 0) {
7872 *single_protocol
= sa_protocol_decode(argv
[0]);
7873 protocol
= single_protocol
;
7879 (void) fprintf(stderr
, gettext("too many arguments\n"));
7883 if (((pool
= uu_avl_pool_create("unmount_pool",
7884 sizeof (unshare_unmount_node_t
),
7885 offsetof(unshare_unmount_node_t
, un_avlnode
),
7886 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
7887 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
7890 if ((mnttab
= fopen(MNTTAB
, "re")) == NULL
) {
7891 uu_avl_destroy(tree
);
7892 uu_avl_pool_destroy(pool
);
7896 while (getmntent(mnttab
, &entry
) == 0) {
7898 /* ignore non-ZFS entries */
7899 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
7902 /* ignore snapshots */
7903 if (strchr(entry
.mnt_special
, '@') != NULL
)
7906 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
7907 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
7913 * Ignore datasets that are excluded/restricted by
7916 if (zpool_skip_pool(zfs_get_pool_name(zhp
))) {
7923 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
7925 sizeof (nfs_mnt_prop
),
7926 NULL
, NULL
, 0, B_FALSE
) == 0);
7927 if (strcmp(nfs_mnt_prop
, "off") != 0)
7929 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
7931 sizeof (nfs_mnt_prop
),
7932 NULL
, NULL
, 0, B_FALSE
) == 0);
7933 if (strcmp(nfs_mnt_prop
, "off") == 0)
7937 /* Ignore legacy mounts */
7938 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
7940 sizeof (nfs_mnt_prop
),
7941 NULL
, NULL
, 0, B_FALSE
) == 0);
7942 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
7944 /* Ignore canmount=noauto mounts */
7945 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
7946 ZFS_CANMOUNT_NOAUTO
)
7953 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
7955 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
7957 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
7959 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
7960 uu_avl_insert(tree
, node
, idx
);
7962 zfs_close(node
->un_zhp
);
7963 free(node
->un_mountp
);
7967 (void) fclose(mnttab
);
7970 * Walk the AVL tree in reverse, unmounting each filesystem and
7971 * removing it from the AVL tree in the process.
7973 if ((walk
= uu_avl_walk_start(tree
,
7974 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
7977 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
7978 const char *mntarg
= NULL
;
7980 uu_avl_remove(tree
, node
);
7983 if (zfs_unshare(node
->un_zhp
,
7984 node
->un_mountp
, protocol
) != 0)
7989 if (zfs_unmount(node
->un_zhp
,
7990 mntarg
, flags
) != 0)
7995 zfs_close(node
->un_zhp
);
7996 free(node
->un_mountp
);
8001 zfs_commit_shares(protocol
);
8003 uu_avl_walk_end(walk
);
8004 uu_avl_destroy(tree
);
8005 uu_avl_pool_destroy(pool
);
8010 (void) fprintf(stderr
,
8011 gettext("missing filesystem argument\n"));
8013 (void) fprintf(stderr
,
8014 gettext("too many arguments\n"));
8019 * We have an argument, but it may be a full path or a ZFS
8020 * filesystem. Pass full paths off to unmount_path() (shared by
8021 * manual_unmount), otherwise open the filesystem and pass to
8024 if (argv
[0][0] == '/')
8025 return (unshare_unmount_path(op
, argv
[0],
8028 if ((zhp
= zfs_open(g_zfs
, argv
[0],
8029 ZFS_TYPE_FILESYSTEM
)) == NULL
)
8032 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
8033 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
8034 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
8035 NULL
, 0, B_FALSE
) == 0);
8039 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
8041 sizeof (nfs_mnt_prop
),
8042 NULL
, NULL
, 0, B_FALSE
) == 0);
8043 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
8044 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
8047 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
8048 strcmp(sharesmb
, "off") == 0) {
8049 (void) fprintf(stderr
, gettext("cannot "
8050 "unshare '%s': legacy share\n"),
8052 (void) fprintf(stderr
, gettext("use "
8053 "exports(5) or smb.conf(5) to unshare "
8054 "this filesystem\n"));
8056 } else if (!zfs_is_shared(zhp
, NULL
, NULL
)) {
8057 (void) fprintf(stderr
, gettext("cannot "
8058 "unshare '%s': not currently "
8059 "shared\n"), zfs_get_name(zhp
));
8061 } else if (zfs_unshareall(zhp
, NULL
) != 0) {
8067 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
8068 (void) fprintf(stderr
, gettext("cannot "
8069 "unmount '%s': legacy "
8070 "mountpoint\n"), zfs_get_name(zhp
));
8071 (void) fprintf(stderr
, gettext("use "
8072 "umount(8) to unmount this "
8075 } else if (!zfs_is_mounted(zhp
, NULL
)) {
8076 (void) fprintf(stderr
, gettext("cannot "
8077 "unmount '%s': not currently "
8081 } else if (zfs_unmountall(zhp
, flags
) != 0) {
8094 * zfs unmount [-fu] -a
8095 * zfs unmount [-fu] filesystem
8097 * Unmount all filesystems, or a specific ZFS filesystem.
8100 zfs_do_unmount(int argc
, char **argv
)
8102 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
8107 * zfs unshare filesystem
8109 * Unshare all filesystems, or a specific ZFS filesystem.
8112 zfs_do_unshare(int argc
, char **argv
)
8114 return (unshare_unmount(OP_SHARE
, argc
, argv
));
8118 find_command_idx(const char *command
, int *idx
)
8122 for (i
= 0; i
< NCOMMAND
; i
++) {
8123 if (command_table
[i
].name
== NULL
)
8126 if (strcmp(command
, command_table
[i
].name
) == 0) {
8135 zfs_do_diff(int argc
, char **argv
)
8139 char *tosnap
= NULL
;
8140 char *fromsnap
= NULL
;
8144 struct sigaction sa
;
8146 while ((c
= getopt(argc
, argv
, "FHth")) != -1) {
8149 flags
|= ZFS_DIFF_CLASSIFY
;
8152 flags
|= ZFS_DIFF_PARSEABLE
;
8155 flags
|= ZFS_DIFF_TIMESTAMP
;
8158 flags
|= ZFS_DIFF_NO_MANGLE
;
8161 (void) fprintf(stderr
,
8162 gettext("invalid option '%c'\n"), optopt
);
8171 (void) fprintf(stderr
,
8172 gettext("must provide at least one snapshot name\n"));
8177 (void) fprintf(stderr
, gettext("too many arguments\n"));
8182 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
8185 if (*fromsnap
!= '@')
8186 copy
= strdup(fromsnap
);
8188 copy
= strdup(tosnap
);
8192 if ((atp
= strchr(copy
, '@')) != NULL
)
8195 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
) {
8202 * Ignore SIGPIPE so that the library can give us
8203 * information on any failure
8205 if (sigemptyset(&sa
.sa_mask
) == -1) {
8210 sa
.sa_handler
= SIG_IGN
;
8211 if (sigaction(SIGPIPE
, &sa
, NULL
) == -1) {
8216 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
8224 * zfs bookmark <fs@source>|<fs#source> <fs#bookmark>
8226 * Creates a bookmark with the given name from the source snapshot
8227 * or creates a copy of an existing source bookmark.
8230 zfs_do_bookmark(int argc
, char **argv
)
8232 char *source
, *bookname
;
8233 char expbuf
[ZFS_MAX_DATASET_NAME_LEN
];
8240 while ((c
= getopt(argc
, argv
, "")) != -1) {
8243 (void) fprintf(stderr
,
8244 gettext("invalid option '%c'\n"), optopt
);
8252 /* check number of arguments */
8254 (void) fprintf(stderr
, gettext("missing source argument\n"));
8258 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
8265 if (strchr(source
, '@') == NULL
&& strchr(source
, '#') == NULL
) {
8266 (void) fprintf(stderr
,
8267 gettext("invalid source name '%s': "
8268 "must contain a '@' or '#'\n"), source
);
8271 if (strchr(bookname
, '#') == NULL
) {
8272 (void) fprintf(stderr
,
8273 gettext("invalid bookmark name '%s': "
8274 "must contain a '#'\n"), bookname
);
8279 * expand source or bookname to full path:
8280 * one of them may be specified as short name
8284 char *source_short
, *bookname_short
;
8285 source_short
= strpbrk(source
, "@#");
8286 bookname_short
= strpbrk(bookname
, "#");
8287 if (source_short
== source
&&
8288 bookname_short
== bookname
) {
8289 (void) fprintf(stderr
, gettext(
8290 "either source or bookmark must be specified as "
8291 "full dataset paths"));
8293 } else if (source_short
!= source
&&
8294 bookname_short
!= bookname
) {
8296 } else if (source_short
!= source
) {
8297 strlcpy(expbuf
, source
, sizeof (expbuf
));
8299 } else if (bookname_short
!= bookname
) {
8300 strlcpy(expbuf
, bookname
, sizeof (expbuf
));
8305 if (expand
!= NULL
) {
8306 *strpbrk(expbuf
, "@#") = '\0'; /* dataset name in buf */
8307 (void) strlcat(expbuf
, *expand
, sizeof (expbuf
));
8312 /* determine source type */
8313 switch (*strpbrk(source
, "@#")) {
8314 case '@': source_type
= ZFS_TYPE_SNAPSHOT
; break;
8315 case '#': source_type
= ZFS_TYPE_BOOKMARK
; break;
8319 /* test the source exists */
8321 zhp
= zfs_open(g_zfs
, source
, source_type
);
8326 nvl
= fnvlist_alloc();
8327 fnvlist_add_string(nvl
, bookname
, source
);
8328 ret
= lzc_bookmark(nvl
, NULL
);
8332 const char *err_msg
= NULL
;
8335 (void) snprintf(errbuf
, sizeof (errbuf
),
8336 dgettext(TEXT_DOMAIN
,
8337 "cannot create bookmark '%s'"), bookname
);
8341 err_msg
= "bookmark is in a different pool";
8343 case ZFS_ERR_BOOKMARK_SOURCE_NOT_ANCESTOR
:
8344 err_msg
= "source is not an ancestor of the "
8345 "new bookmark's dataset";
8348 err_msg
= "bookmark exists";
8351 err_msg
= "invalid argument";
8354 err_msg
= "bookmark feature not enabled";
8357 err_msg
= "out of space";
8360 err_msg
= "dataset does not exist";
8363 (void) zfs_standard_error(g_zfs
, ret
, errbuf
);
8366 if (err_msg
!= NULL
) {
8367 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
8368 dgettext(TEXT_DOMAIN
, err_msg
));
8380 zfs_do_channel_program(int argc
, char **argv
)
8383 size_t progsize
, progread
;
8384 nvlist_t
*outnvl
= NULL
;
8385 uint64_t instrlimit
= ZCP_DEFAULT_INSTRLIMIT
;
8386 uint64_t memlimit
= ZCP_DEFAULT_MEMLIMIT
;
8387 boolean_t sync_flag
= B_TRUE
, json_output
= B_FALSE
;
8388 zpool_handle_t
*zhp
;
8390 struct option long_options
[] = {
8391 {"json", no_argument
, NULL
, 'j'},
8396 while ((c
= getopt_long(argc
, argv
, "nt:m:j", long_options
,
8405 arg
= strtoull(optarg
, &endp
, 0);
8406 if (errno
!= 0 || *endp
!= '\0') {
8407 (void) fprintf(stderr
, gettext(
8409 "'%s': expected integer\n"), optarg
);
8416 ASSERT3U(c
, ==, 'm');
8422 sync_flag
= B_FALSE
;
8426 json_output
= B_TRUE
;
8430 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8440 (void) fprintf(stderr
,
8441 gettext("invalid number of arguments\n"));
8445 const char *poolname
= argv
[0];
8446 const char *filename
= argv
[1];
8447 if (strcmp(filename
, "-") == 0) {
8449 filename
= "standard input";
8450 } else if ((fd
= open(filename
, O_RDONLY
)) < 0) {
8451 (void) fprintf(stderr
, gettext("cannot open '%s': %s\n"),
8452 filename
, strerror(errno
));
8456 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
8457 (void) fprintf(stderr
, gettext("cannot open pool '%s'\n"),
8466 * Read in the channel program, expanding the program buffer as
8471 char *progbuf
= safe_malloc(progsize
);
8473 ret
= read(fd
, progbuf
+ progread
, progsize
- progread
);
8475 if (progread
== progsize
&& ret
> 0) {
8477 progbuf
= safe_realloc(progbuf
, progsize
);
8485 (void) fprintf(stderr
,
8486 gettext("cannot read '%s': %s\n"),
8487 filename
, strerror(errno
));
8490 progbuf
[progread
] = '\0';
8493 * Any remaining arguments are passed as arguments to the lua script as
8496 * "argv" -> [ "arg 1", ... "arg n" ],
8499 nvlist_t
*argnvl
= fnvlist_alloc();
8500 fnvlist_add_string_array(argnvl
, ZCP_ARG_CLIARGV
,
8501 (const char **)argv
+ 2, argc
- 2);
8504 ret
= lzc_channel_program(poolname
, progbuf
,
8505 instrlimit
, memlimit
, argnvl
, &outnvl
);
8507 ret
= lzc_channel_program_nosync(poolname
, progbuf
,
8508 instrlimit
, memlimit
, argnvl
, &outnvl
);
8513 * On error, report the error message handed back by lua if one
8514 * exists. Otherwise, generate an appropriate error message,
8515 * falling back on strerror() for an unexpected return code.
8517 const char *errstring
= NULL
;
8518 const char *msg
= gettext("Channel program execution failed");
8519 uint64_t instructions
= 0;
8520 if (outnvl
!= NULL
&& nvlist_exists(outnvl
, ZCP_RET_ERROR
)) {
8521 const char *es
= NULL
;
8522 (void) nvlist_lookup_string(outnvl
,
8523 ZCP_RET_ERROR
, &es
);
8525 errstring
= strerror(ret
);
8529 (void) nvlist_lookup_uint64(outnvl
,
8530 ZCP_ARG_INSTRLIMIT
, &instructions
);
8536 "Invalid instruction or memory limit.";
8539 errstring
= "Return value too large.";
8542 errstring
= "Memory limit exhausted.";
8545 errstring
= "Timed out.";
8548 errstring
= "Permission denied. Channel "
8549 "programs must be run as root.";
8552 (void) zfs_standard_error(g_zfs
, ret
, msg
);
8555 if (errstring
!= NULL
)
8556 (void) fprintf(stderr
, "%s:\n%s\n", msg
, errstring
);
8558 if (ret
== ETIME
&& instructions
!= 0)
8559 (void) fprintf(stderr
,
8560 gettext("%llu Lua instructions\n"),
8561 (u_longlong_t
)instructions
);
8564 (void) nvlist_print_json(stdout
, outnvl
);
8565 } else if (nvlist_empty(outnvl
)) {
8566 (void) fprintf(stdout
, gettext("Channel program fully "
8567 "executed and did not produce output.\n"));
8569 (void) fprintf(stdout
, gettext("Channel program fully "
8570 "executed and produced output:\n"));
8571 dump_nvlist(outnvl
, 4);
8576 fnvlist_free(outnvl
);
8577 fnvlist_free(argnvl
);
8586 typedef struct loadkey_cbdata
{
8587 boolean_t cb_loadkey
;
8588 boolean_t cb_recursive
;
8590 char *cb_keylocation
;
8591 uint64_t cb_numfailed
;
8592 uint64_t cb_numattempted
;
8596 load_key_callback(zfs_handle_t
*zhp
, void *data
)
8599 boolean_t is_encroot
;
8600 loadkey_cbdata_t
*cb
= data
;
8601 uint64_t keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
8604 * If we are working recursively, we want to skip loading / unloading
8605 * keys for non-encryption roots and datasets whose keys are already
8606 * in the desired end-state.
8608 if (cb
->cb_recursive
) {
8609 ret
= zfs_crypto_get_encryption_root(zhp
, &is_encroot
, NULL
);
8615 if ((cb
->cb_loadkey
&& keystatus
== ZFS_KEYSTATUS_AVAILABLE
) ||
8616 (!cb
->cb_loadkey
&& keystatus
== ZFS_KEYSTATUS_UNAVAILABLE
))
8620 cb
->cb_numattempted
++;
8623 ret
= zfs_crypto_load_key(zhp
, cb
->cb_noop
, cb
->cb_keylocation
);
8625 ret
= zfs_crypto_unload_key(zhp
);
8636 load_unload_keys(int argc
, char **argv
, boolean_t loadkey
)
8638 int c
, ret
= 0, flags
= 0;
8639 boolean_t do_all
= B_FALSE
;
8640 loadkey_cbdata_t cb
= { 0 };
8642 cb
.cb_loadkey
= loadkey
;
8644 while ((c
= getopt(argc
, argv
, "anrL:")) != -1) {
8645 /* noop and alternate keylocations only apply to zfs load-key */
8649 cb
.cb_noop
= B_TRUE
;
8652 cb
.cb_keylocation
= optarg
;
8662 cb
.cb_recursive
= B_TRUE
;
8665 flags
|= ZFS_ITER_RECURSE
;
8666 cb
.cb_recursive
= B_TRUE
;
8669 (void) fprintf(stderr
,
8670 gettext("invalid option '%c'\n"), optopt
);
8678 if (!do_all
&& argc
== 0) {
8679 (void) fprintf(stderr
,
8680 gettext("Missing dataset argument or -a option\n"));
8684 if (do_all
&& argc
!= 0) {
8685 (void) fprintf(stderr
,
8686 gettext("Cannot specify dataset with -a option\n"));
8690 if (cb
.cb_recursive
&& cb
.cb_keylocation
!= NULL
&&
8691 strcmp(cb
.cb_keylocation
, "prompt") != 0) {
8692 (void) fprintf(stderr
, gettext("alternate keylocation may only "
8693 "be 'prompt' with -r or -a\n"));
8697 ret
= zfs_for_each(argc
, argv
, flags
,
8698 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
, NULL
, NULL
, 0,
8699 load_key_callback
, &cb
);
8701 if (cb
.cb_noop
|| (cb
.cb_recursive
&& cb
.cb_numattempted
!= 0)) {
8702 (void) printf(gettext("%llu / %llu key(s) successfully %s\n"),
8703 (u_longlong_t
)(cb
.cb_numattempted
- cb
.cb_numfailed
),
8704 (u_longlong_t
)cb
.cb_numattempted
,
8705 loadkey
? (cb
.cb_noop
? "verified" : "loaded") :
8709 if (cb
.cb_numfailed
!= 0)
8716 zfs_do_load_key(int argc
, char **argv
)
8718 return (load_unload_keys(argc
, argv
, B_TRUE
));
8723 zfs_do_unload_key(int argc
, char **argv
)
8725 return (load_unload_keys(argc
, argv
, B_FALSE
));
8729 zfs_do_change_key(int argc
, char **argv
)
8733 boolean_t loadkey
= B_FALSE
, inheritkey
= B_FALSE
;
8734 zfs_handle_t
*zhp
= NULL
;
8735 nvlist_t
*props
= fnvlist_alloc();
8737 while ((c
= getopt(argc
, argv
, "lio:")) != -1) {
8743 inheritkey
= B_TRUE
;
8746 if (!parseprop(props
, optarg
)) {
8752 (void) fprintf(stderr
,
8753 gettext("invalid option '%c'\n"), optopt
);
8758 if (inheritkey
&& !nvlist_empty(props
)) {
8759 (void) fprintf(stderr
,
8760 gettext("Properties not allowed for inheriting\n"));
8768 (void) fprintf(stderr
, gettext("Missing dataset argument\n"));
8773 (void) fprintf(stderr
, gettext("Too many arguments\n"));
8777 zhp
= zfs_open(g_zfs
, argv
[argc
- 1],
8778 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
8783 keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
8784 if (keystatus
!= ZFS_KEYSTATUS_AVAILABLE
) {
8785 ret
= zfs_crypto_load_key(zhp
, B_FALSE
, NULL
);
8793 /* refresh the properties so the new keystatus is visible */
8794 zfs_refresh_properties(zhp
);
8797 ret
= zfs_crypto_rewrap(zhp
, props
, inheritkey
);
8810 * 1) zfs project [-d|-r] <file|directory ...>
8811 * List project ID and inherit flag of file(s) or directories.
8812 * -d: List the directory itself, not its children.
8813 * -r: List subdirectories recursively.
8815 * 2) zfs project -C [-k] [-r] <file|directory ...>
8816 * Clear project inherit flag and/or ID on the file(s) or directories.
8817 * -k: Keep the project ID unchanged. If not specified, the project ID
8818 * will be reset as zero.
8819 * -r: Clear on subdirectories recursively.
8821 * 3) zfs project -c [-0] [-d|-r] [-p id] <file|directory ...>
8822 * Check project ID and inherit flag on the file(s) or directories,
8823 * report the outliers.
8824 * -0: Print file name followed by a NUL instead of newline.
8825 * -d: Check the directory itself, not its children.
8826 * -p: Specify the referenced ID for comparing with the target file(s)
8827 * or directories' project IDs. If not specified, the target (top)
8828 * directory's project ID will be used as the referenced one.
8829 * -r: Check subdirectories recursively.
8831 * 4) zfs project [-p id] [-r] [-s] <file|directory ...>
8832 * Set project ID and/or inherit flag on the file(s) or directories.
8833 * -p: Set the project ID as the given id.
8834 * -r: Set on subdirectories recursively. If not specify "-p" option,
8835 * it will use top-level directory's project ID as the given id,
8836 * then set both project ID and inherit flag on all descendants
8837 * of the top-level directory.
8838 * -s: Set project inherit flag.
8841 zfs_do_project(int argc
, char **argv
)
8843 zfs_project_control_t zpc
= {
8844 .zpc_expected_projid
= ZFS_INVALID_PROJID
,
8845 .zpc_op
= ZFS_PROJECT_OP_DEFAULT
,
8846 .zpc_dironly
= B_FALSE
,
8847 .zpc_keep_projid
= B_FALSE
,
8848 .zpc_newline
= B_TRUE
,
8849 .zpc_recursive
= B_FALSE
,
8850 .zpc_set_flag
= B_FALSE
,
8857 while ((c
= getopt(argc
, argv
, "0Ccdkp:rs")) != -1) {
8860 zpc
.zpc_newline
= B_FALSE
;
8863 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
8864 (void) fprintf(stderr
, gettext("cannot "
8865 "specify '-C' '-c' '-s' together\n"));
8869 zpc
.zpc_op
= ZFS_PROJECT_OP_CLEAR
;
8872 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
8873 (void) fprintf(stderr
, gettext("cannot "
8874 "specify '-C' '-c' '-s' together\n"));
8878 zpc
.zpc_op
= ZFS_PROJECT_OP_CHECK
;
8881 zpc
.zpc_dironly
= B_TRUE
;
8882 /* overwrite "-r" option */
8883 zpc
.zpc_recursive
= B_FALSE
;
8886 zpc
.zpc_keep_projid
= B_TRUE
;
8892 zpc
.zpc_expected_projid
= strtoull(optarg
, &endptr
, 0);
8893 if (errno
!= 0 || *endptr
!= '\0') {
8894 (void) fprintf(stderr
,
8895 gettext("project ID must be less than "
8896 "%u\n"), UINT32_MAX
);
8899 if (zpc
.zpc_expected_projid
>= UINT32_MAX
) {
8900 (void) fprintf(stderr
,
8901 gettext("invalid project ID\n"));
8907 zpc
.zpc_recursive
= B_TRUE
;
8908 /* overwrite "-d" option */
8909 zpc
.zpc_dironly
= B_FALSE
;
8912 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
8913 (void) fprintf(stderr
, gettext("cannot "
8914 "specify '-C' '-c' '-s' together\n"));
8918 zpc
.zpc_set_flag
= B_TRUE
;
8919 zpc
.zpc_op
= ZFS_PROJECT_OP_SET
;
8922 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8928 if (zpc
.zpc_op
== ZFS_PROJECT_OP_DEFAULT
) {
8929 if (zpc
.zpc_expected_projid
!= ZFS_INVALID_PROJID
)
8930 zpc
.zpc_op
= ZFS_PROJECT_OP_SET
;
8932 zpc
.zpc_op
= ZFS_PROJECT_OP_LIST
;
8935 switch (zpc
.zpc_op
) {
8936 case ZFS_PROJECT_OP_LIST
:
8937 if (zpc
.zpc_keep_projid
) {
8938 (void) fprintf(stderr
,
8939 gettext("'-k' is only valid together with '-C'\n"));
8942 if (!zpc
.zpc_newline
) {
8943 (void) fprintf(stderr
,
8944 gettext("'-0' is only valid together with '-c'\n"));
8948 case ZFS_PROJECT_OP_CHECK
:
8949 if (zpc
.zpc_keep_projid
) {
8950 (void) fprintf(stderr
,
8951 gettext("'-k' is only valid together with '-C'\n"));
8955 case ZFS_PROJECT_OP_CLEAR
:
8956 if (zpc
.zpc_dironly
) {
8957 (void) fprintf(stderr
,
8958 gettext("'-d' is useless together with '-C'\n"));
8961 if (!zpc
.zpc_newline
) {
8962 (void) fprintf(stderr
,
8963 gettext("'-0' is only valid together with '-c'\n"));
8966 if (zpc
.zpc_expected_projid
!= ZFS_INVALID_PROJID
) {
8967 (void) fprintf(stderr
,
8968 gettext("'-p' is useless together with '-C'\n"));
8972 case ZFS_PROJECT_OP_SET
:
8973 if (zpc
.zpc_dironly
) {
8974 (void) fprintf(stderr
,
8975 gettext("'-d' is useless for set project ID and/or "
8979 if (zpc
.zpc_keep_projid
) {
8980 (void) fprintf(stderr
,
8981 gettext("'-k' is only valid together with '-C'\n"));
8984 if (!zpc
.zpc_newline
) {
8985 (void) fprintf(stderr
,
8986 gettext("'-0' is only valid together with '-c'\n"));
8998 (void) fprintf(stderr
,
8999 gettext("missing file or directory target(s)\n"));
9003 for (int i
= 0; i
< argc
; i
++) {
9006 err
= zfs_project_handle(argv
[i
], &zpc
);
9015 zfs_do_wait(int argc
, char **argv
)
9017 boolean_t enabled
[ZFS_WAIT_NUM_ACTIVITIES
];
9021 /* By default, wait for all types of activity. */
9022 for (i
= 0; i
< ZFS_WAIT_NUM_ACTIVITIES
; i
++)
9023 enabled
[i
] = B_TRUE
;
9025 while ((c
= getopt(argc
, argv
, "t:")) != -1) {
9028 /* Reset activities array */
9029 memset(&enabled
, 0, sizeof (enabled
));
9031 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
9032 static const char *const col_subopts
[
9033 ZFS_WAIT_NUM_ACTIVITIES
] = { "deleteq" };
9035 for (i
= 0; i
< ARRAY_SIZE(col_subopts
); ++i
)
9036 if (strcmp(tok
, col_subopts
[i
]) == 0) {
9037 enabled
[i
] = B_TRUE
;
9041 (void) fprintf(stderr
,
9042 gettext("invalid activity '%s'\n"), tok
);
9048 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9057 (void) fprintf(stderr
, gettext("missing 'filesystem' "
9062 (void) fprintf(stderr
, gettext("too many arguments\n"));
9066 zfs_handle_t
*zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
);
9071 boolean_t missing
= B_FALSE
;
9072 boolean_t any_waited
= B_FALSE
;
9074 for (int i
= 0; i
< ZFS_WAIT_NUM_ACTIVITIES
; i
++) {
9080 error
= zfs_wait_status(zhp
, i
, &missing
, &waited
);
9081 if (error
!= 0 || missing
)
9084 any_waited
= (any_waited
|| waited
);
9087 if (error
!= 0 || missing
|| !any_waited
)
9097 * Display version message
9100 zfs_do_version(int argc
, char **argv
)
9103 nvlist_t
*jsobj
= NULL
, *zfs_ver
= NULL
;
9104 boolean_t json
= B_FALSE
;
9106 struct option long_options
[] = {
9107 {"json", no_argument
, NULL
, 'j'},
9111 while ((c
= getopt_long(argc
, argv
, "j", long_options
, NULL
)) != -1) {
9115 jsobj
= zfs_json_schema(0, 1);
9118 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9126 (void) fprintf(stderr
, "too many arguments\n");
9131 zfs_ver
= zfs_version_nvlist();
9133 fnvlist_add_nvlist(jsobj
, "zfs_version", zfs_ver
);
9134 zcmd_print_json(jsobj
);
9135 fnvlist_free(zfs_ver
);
9140 return (zfs_version_print() != 0);
9143 /* Display documentation */
9145 zfs_do_help(int argc
, char **argv
)
9147 char page
[MAXNAMELEN
];
9148 if (argc
< 3 || strcmp(argv
[2], "zfs") == 0)
9149 strcpy(page
, "zfs");
9150 else if (strcmp(argv
[2], "concepts") == 0 ||
9151 strcmp(argv
[2], "props") == 0)
9152 snprintf(page
, sizeof (page
), "zfs%s", argv
[2]);
9154 snprintf(page
, sizeof (page
), "zfs-%s", argv
[2]);
9156 execlp("man", "man", page
, NULL
);
9158 fprintf(stderr
, "couldn't run man program: %s", strerror(errno
));
9163 main(int argc
, char **argv
)
9167 const char *cmdname
;
9170 (void) setlocale(LC_ALL
, "");
9171 (void) setlocale(LC_NUMERIC
, "C");
9172 (void) textdomain(TEXT_DOMAIN
);
9177 * Make sure the user has specified some command.
9180 (void) fprintf(stderr
, gettext("missing command\n"));
9187 * The 'umount' command is an alias for 'unmount'
9189 if (strcmp(cmdname
, "umount") == 0)
9190 cmdname
= "unmount";
9193 * The 'recv' command is an alias for 'receive'
9195 if (strcmp(cmdname
, "recv") == 0)
9196 cmdname
= "receive";
9199 * The 'snap' command is an alias for 'snapshot'
9201 if (strcmp(cmdname
, "snap") == 0)
9202 cmdname
= "snapshot";
9207 if ((strcmp(cmdname
, "-?") == 0) ||
9208 (strcmp(cmdname
, "--help") == 0))
9212 * Special case '-V|--version'
9214 if ((strcmp(cmdname
, "-V") == 0) || (strcmp(cmdname
, "--version") == 0))
9215 return (zfs_version_print() != 0);
9218 * Special case 'help'
9220 if (strcmp(cmdname
, "help") == 0)
9221 return (zfs_do_help(argc
, argv
));
9223 if ((g_zfs
= libzfs_init()) == NULL
) {
9224 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
9228 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
9230 libzfs_print_on_error(g_zfs
, B_TRUE
);
9232 zfs_setproctitle_init(argc
, argv
, environ
);
9235 * Many commands modify input strings for string parsing reasons.
9236 * We create a copy to protect the original argv.
9238 newargv
= safe_malloc((argc
+ 1) * sizeof (newargv
[0]));
9239 for (i
= 0; i
< argc
; i
++)
9240 newargv
[i
] = strdup(argv
[i
]);
9241 newargv
[argc
] = NULL
;
9244 * Run the appropriate command.
9246 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
9247 if (find_command_idx(cmdname
, &i
) == 0) {
9248 current_command
= &command_table
[i
];
9249 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
9250 } else if (strchr(cmdname
, '=') != NULL
) {
9251 verify(find_command_idx("set", &i
) == 0);
9252 current_command
= &command_table
[i
];
9253 ret
= command_table
[i
].func(argc
, newargv
);
9255 (void) fprintf(stderr
, gettext("unrecognized "
9256 "command '%s'\n"), cmdname
);
9261 for (i
= 0; i
< argc
; i
++)
9265 if (ret
== 0 && log_history
)
9266 (void) zpool_log_history(g_zfs
, history_str
);
9271 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
9272 * for the purposes of running ::findleaks.
9274 if (getenv("ZFS_ABORT") != NULL
) {
9275 (void) printf("dumping core by request\n");
9283 * zfs zone nsfile filesystem
9285 * Add or delete the given dataset to/from the namespace.
9289 zfs_do_zone_impl(int argc
, char **argv
, boolean_t attach
)
9295 (void) fprintf(stderr
, gettext("missing argument(s)\n"));
9299 (void) fprintf(stderr
, gettext("too many arguments\n"));
9303 zhp
= zfs_open(g_zfs
, argv
[2], ZFS_TYPE_FILESYSTEM
);
9307 ret
= (zfs_userns(zhp
, argv
[1], attach
) != 0);
9314 zfs_do_zone(int argc
, char **argv
)
9316 return (zfs_do_zone_impl(argc
, argv
, B_TRUE
));
9320 zfs_do_unzone(int argc
, char **argv
)
9322 return (zfs_do_zone_impl(argc
, argv
, B_FALSE
));
9327 #include <sys/jail.h>
9330 * Attach/detach the given dataset to/from the given jail
9333 zfs_do_jail_impl(int argc
, char **argv
, boolean_t attach
)
9338 /* check number of arguments */
9340 (void) fprintf(stderr
, gettext("missing argument(s)\n"));
9344 (void) fprintf(stderr
, gettext("too many arguments\n"));
9348 jailid
= jail_getid(argv
[1]);
9350 (void) fprintf(stderr
, gettext("invalid jail id or name\n"));
9354 zhp
= zfs_open(g_zfs
, argv
[2], ZFS_TYPE_FILESYSTEM
);
9358 ret
= (zfs_jail(zhp
, jailid
, attach
) != 0);
9365 * zfs jail jailid filesystem
9367 * Attach the given dataset to the given jail
9370 zfs_do_jail(int argc
, char **argv
)
9372 return (zfs_do_jail_impl(argc
, argv
, B_TRUE
));
9376 * zfs unjail jailid filesystem
9378 * Detach the given dataset from the given jail
9381 zfs_do_unjail(int argc
, char **argv
)
9383 return (zfs_do_jail_impl(argc
, argv
, B_FALSE
));