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
);
136 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
141 _umem_debug_init(void)
143 return ("default,verbose"); /* $UMEM_DEBUG setting */
147 _umem_logging_init(void)
149 return ("fail,contents"); /* $UMEM_LOGGING setting */
183 HELP_CHANNEL_PROGRAM
,
196 typedef struct zfs_command
{
198 int (*func
)(int argc
, char **argv
);
203 * Master command table. Each ZFS command has a name, associated function, and
204 * usage message. The usage messages need to be internationalized, so we have
205 * to have a function to return the usage message based on a command index.
207 * These commands are organized according to how they are displayed in the usage
208 * message. An empty command (one with a NULL name) indicates an empty line in
209 * the generic usage message.
211 static zfs_command_t command_table
[] = {
212 { "version", zfs_do_version
, HELP_VERSION
},
214 { "create", zfs_do_create
, HELP_CREATE
},
215 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
217 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
218 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
219 { "clone", zfs_do_clone
, HELP_CLONE
},
220 { "promote", zfs_do_promote
, HELP_PROMOTE
},
221 { "rename", zfs_do_rename
, HELP_RENAME
},
222 { "bookmark", zfs_do_bookmark
, HELP_BOOKMARK
},
223 { "program", zfs_do_channel_program
, HELP_CHANNEL_PROGRAM
},
225 { "list", zfs_do_list
, HELP_LIST
},
227 { "set", zfs_do_set
, HELP_SET
},
228 { "get", zfs_do_get
, HELP_GET
},
229 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
230 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
232 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
233 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
234 { "projectspace", zfs_do_userspace
, HELP_PROJECTSPACE
},
236 { "project", zfs_do_project
, HELP_PROJECT
},
238 { "mount", zfs_do_mount
, HELP_MOUNT
},
239 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
240 { "share", zfs_do_share
, HELP_SHARE
},
241 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
243 { "send", zfs_do_send
, HELP_SEND
},
244 { "receive", zfs_do_receive
, HELP_RECEIVE
},
246 { "allow", zfs_do_allow
, HELP_ALLOW
},
248 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
250 { "hold", zfs_do_hold
, HELP_HOLD
},
251 { "holds", zfs_do_holds
, HELP_HOLDS
},
252 { "release", zfs_do_release
, HELP_RELEASE
},
253 { "diff", zfs_do_diff
, HELP_DIFF
},
254 { "load-key", zfs_do_load_key
, HELP_LOAD_KEY
},
255 { "unload-key", zfs_do_unload_key
, HELP_UNLOAD_KEY
},
256 { "change-key", zfs_do_change_key
, HELP_CHANGE_KEY
},
257 { "redact", zfs_do_redact
, HELP_REDACT
},
258 { "wait", zfs_do_wait
, HELP_WAIT
},
261 { "jail", zfs_do_jail
, HELP_JAIL
},
262 { "unjail", zfs_do_unjail
, HELP_UNJAIL
},
266 { "zone", zfs_do_zone
, HELP_ZONE
},
267 { "unzone", zfs_do_unzone
, HELP_UNZONE
},
271 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
273 zfs_command_t
*current_command
;
276 get_usage(zfs_help_t idx
)
280 return (gettext("\tclone [-p] [-o property=value] ... "
281 "<snapshot> <filesystem|volume>\n"));
283 return (gettext("\tcreate [-Pnpuv] [-o property=value] ... "
285 "\tcreate [-Pnpsv] [-b blocksize] [-o property=value] ... "
286 "-V <size> <volume>\n"));
288 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
289 "\tdestroy [-dnpRrv] "
290 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
291 "\tdestroy <filesystem|volume>#<bookmark>\n"));
293 return (gettext("\tget [-rHp] [-d max] "
294 "[-o \"all\" | field[,...]]\n"
295 "\t [-t type[,...]] [-s source[,...]]\n"
296 "\t <\"all\" | property[,...]> "
297 "[filesystem|volume|snapshot|bookmark] ...\n"));
299 return (gettext("\tinherit [-rS] <property> "
300 "<filesystem|volume|snapshot> ...\n"));
302 return (gettext("\tupgrade [-v]\n"
303 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
305 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
306 "[-s property]...\n\t [-S property]... [-t type[,...]] "
307 "[filesystem|volume|snapshot] ...\n"));
309 return (gettext("\tmount\n"
310 "\tmount [-flvO] [-o opts] <-a | filesystem>\n"));
312 return (gettext("\tpromote <clone-filesystem>\n"));
314 return (gettext("\treceive [-vMnsFhu] "
315 "[-o <property>=<value>] ... [-x <property>] ...\n"
316 "\t <filesystem|volume|snapshot>\n"
317 "\treceive [-vMnsFhu] [-o <property>=<value>] ... "
318 "[-x <property>] ... \n"
319 "\t [-d | -e] <filesystem>\n"
320 "\treceive -A <filesystem|volume>\n"));
322 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
323 "<filesystem|volume|snapshot>\n"
324 "\trename -p [-f] <filesystem|volume> <filesystem|volume>\n"
325 "\trename -u [-f] <filesystem> <filesystem>\n"
326 "\trename -r <snapshot> <snapshot>\n"));
328 return (gettext("\trollback [-rRf] <snapshot>\n"));
330 return (gettext("\tsend [-DLPbcehnpsvw] "
332 "\t [-R [-X dataset[,dataset]...]] <snapshot>\n"
333 "\tsend [-DnvPLecw] [-i snapshot|bookmark] "
334 "<filesystem|volume|snapshot>\n"
335 "\tsend [-DnPpvLec] [-i bookmark|snapshot] "
336 "--redact <bookmark> <snapshot>\n"
337 "\tsend [-nvPe] -t <receive_resume_token>\n"
338 "\tsend [-Pnv] --saved filesystem\n"));
340 return (gettext("\tset <property=value> ... "
341 "<filesystem|volume|snapshot> ...\n"));
343 return (gettext("\tshare [-l] <-a [nfs|smb] | filesystem>\n"));
345 return (gettext("\tsnapshot [-r] [-o property=value] ... "
346 "<filesystem|volume>@<snap> ...\n"));
348 return (gettext("\tunmount [-fu] "
349 "<-a | filesystem|mountpoint>\n"));
351 return (gettext("\tunshare "
352 "<-a [nfs|smb] | filesystem|mountpoint>\n"));
354 return (gettext("\tallow <filesystem|volume>\n"
356 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
357 "\t <filesystem|volume>\n"
358 "\tallow [-ld] -e <perm|@setname>[,...] "
359 "<filesystem|volume>\n"
360 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
361 "\tallow -s @setname <perm|@setname>[,...] "
362 "<filesystem|volume>\n"));
364 return (gettext("\tunallow [-rldug] "
365 "<\"everyone\"|user|group>[,...]\n"
366 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
367 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
368 "<filesystem|volume>\n"
369 "\tunallow [-r] -c [<perm|@setname>[,...]] "
370 "<filesystem|volume>\n"
371 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
372 "<filesystem|volume>\n"));
374 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
376 "\t [-S field] ... [-t type[,...]] "
377 "<filesystem|snapshot|path>\n"));
378 case HELP_GROUPSPACE
:
379 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
381 "\t [-S field] ... [-t type[,...]] "
382 "<filesystem|snapshot|path>\n"));
383 case HELP_PROJECTSPACE
:
384 return (gettext("\tprojectspace [-Hp] [-o field[,...]] "
386 "\t [-S field] ... <filesystem|snapshot|path>\n"));
388 return (gettext("\tproject [-d|-r] <directory|file ...>\n"
389 "\tproject -c [-0] [-d|-r] [-p id] <directory|file ...>\n"
390 "\tproject -C [-k] [-r] <directory ...>\n"
391 "\tproject [-p id] [-r] [-s] <directory ...>\n"));
393 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
395 return (gettext("\tholds [-rH] <snapshot> ...\n"));
397 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
399 return (gettext("\tdiff [-FHt] <snapshot> "
400 "[snapshot|filesystem]\n"));
402 return (gettext("\tbookmark <snapshot|bookmark> "
404 case HELP_CHANNEL_PROGRAM
:
405 return (gettext("\tprogram [-jn] [-t <instruction limit>] "
406 "[-m <memory limit (b)>]\n"
407 "\t <pool> <program file> [lua args...]\n"));
409 return (gettext("\tload-key [-rn] [-L <keylocation>] "
410 "<-a | filesystem|volume>\n"));
411 case HELP_UNLOAD_KEY
:
412 return (gettext("\tunload-key [-r] "
413 "<-a | filesystem|volume>\n"));
414 case HELP_CHANGE_KEY
:
415 return (gettext("\tchange-key [-l] [-o keyformat=<value>]\n"
416 "\t [-o keylocation=<value>] [-o pbkdf2iters=<value>]\n"
417 "\t <filesystem|volume>\n"
418 "\tchange-key -i [-l] <filesystem|volume>\n"));
420 return (gettext("\tversion\n"));
422 return (gettext("\tredact <snapshot> <bookmark> "
423 "<redaction_snapshot> ...\n"));
425 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
427 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
429 return (gettext("\twait [-t <activity>] <filesystem>\n"));
431 return (gettext("\tzone <nsfile> <filesystem>\n"));
433 return (gettext("\tunzone <nsfile> <filesystem>\n"));
435 __builtin_unreachable();
442 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
447 * Utility function to guarantee malloc() success.
451 safe_malloc(size_t size
)
455 if ((data
= calloc(1, size
)) == NULL
)
462 safe_realloc(void *data
, size_t size
)
465 if ((newp
= realloc(data
, size
)) == NULL
) {
474 safe_strdup(const char *str
)
476 char *dupstr
= strdup(str
);
485 * Callback routine that will print out information for each of
489 usage_prop_cb(int prop
, void *cb
)
493 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
495 if (zfs_prop_readonly(prop
))
496 (void) fprintf(fp
, " NO ");
498 (void) fprintf(fp
, "YES ");
500 if (zfs_prop_inheritable(prop
))
501 (void) fprintf(fp
, " YES ");
503 (void) fprintf(fp
, " NO ");
505 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
) ?: "-");
511 * Display usage message. If we're inside a command, display only the usage for
512 * that command. Otherwise, iterate over the entire command table and display
513 * a complete usage message.
515 static __attribute__((noreturn
)) void
516 usage(boolean_t requested
)
519 boolean_t show_properties
= B_FALSE
;
520 FILE *fp
= requested
? stdout
: stderr
;
522 if (current_command
== NULL
) {
524 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
526 gettext("where 'command' is one of the following:\n\n"));
528 for (i
= 0; i
< NCOMMAND
; i
++) {
529 if (command_table
[i
].name
== NULL
)
530 (void) fprintf(fp
, "\n");
532 (void) fprintf(fp
, "%s",
533 get_usage(command_table
[i
].usage
));
536 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
537 "pool/[dataset/]*dataset[@name]\n"));
539 (void) fprintf(fp
, gettext("usage:\n"));
540 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
543 if (current_command
!= NULL
&&
544 (strcmp(current_command
->name
, "set") == 0 ||
545 strcmp(current_command
->name
, "get") == 0 ||
546 strcmp(current_command
->name
, "inherit") == 0 ||
547 strcmp(current_command
->name
, "list") == 0))
548 show_properties
= B_TRUE
;
550 if (show_properties
) {
552 gettext("\nThe following properties are supported:\n"));
554 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
555 "PROPERTY", "EDIT", "INHERIT", "VALUES");
557 /* Iterate over all properties */
558 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
561 (void) fprintf(fp
, "\t%-15s ", "userused@...");
562 (void) fprintf(fp
, " NO NO <size>\n");
563 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
564 (void) fprintf(fp
, " NO NO <size>\n");
565 (void) fprintf(fp
, "\t%-15s ", "projectused@...");
566 (void) fprintf(fp
, " NO NO <size>\n");
567 (void) fprintf(fp
, "\t%-15s ", "userobjused@...");
568 (void) fprintf(fp
, " NO NO <size>\n");
569 (void) fprintf(fp
, "\t%-15s ", "groupobjused@...");
570 (void) fprintf(fp
, " NO NO <size>\n");
571 (void) fprintf(fp
, "\t%-15s ", "projectobjused@...");
572 (void) fprintf(fp
, " NO NO <size>\n");
573 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
574 (void) fprintf(fp
, "YES NO <size> | none\n");
575 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
576 (void) fprintf(fp
, "YES NO <size> | none\n");
577 (void) fprintf(fp
, "\t%-15s ", "projectquota@...");
578 (void) fprintf(fp
, "YES NO <size> | none\n");
579 (void) fprintf(fp
, "\t%-15s ", "userobjquota@...");
580 (void) fprintf(fp
, "YES NO <size> | none\n");
581 (void) fprintf(fp
, "\t%-15s ", "groupobjquota@...");
582 (void) fprintf(fp
, "YES NO <size> | none\n");
583 (void) fprintf(fp
, "\t%-15s ", "projectobjquota@...");
584 (void) fprintf(fp
, "YES NO <size> | none\n");
585 (void) fprintf(fp
, "\t%-15s ", "written@<snap>");
586 (void) fprintf(fp
, " NO NO <size>\n");
587 (void) fprintf(fp
, "\t%-15s ", "written#<bookmark>");
588 (void) fprintf(fp
, " NO NO <size>\n");
590 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
591 "with standard units such as K, M, G, etc.\n"));
592 (void) fprintf(fp
, "%s", gettext("\nUser-defined properties "
593 "can be specified by using a name containing a colon "
595 (void) fprintf(fp
, gettext("\nThe {user|group|project}"
596 "[obj]{used|quota}@ properties must be appended with\n"
597 "a user|group|project specifier of one of these forms:\n"
598 " POSIX name (eg: \"matt\")\n"
599 " POSIX id (eg: \"126829\")\n"
600 " SMB name@domain (eg: \"matt@sun\")\n"
601 " SMB SID (eg: \"S-1-234-567-89\")\n"));
604 gettext("\nFor the property list, run: %s\n"),
607 gettext("\nFor the delegated permission list, run: %s\n"),
608 "zfs allow|unallow");
612 * See comments at end of main().
614 if (getenv("ZFS_ABORT") != NULL
) {
615 (void) printf("dumping core by request\n");
619 exit(requested
? 0 : 2);
623 * Take a property=value argument string and add it to the given nvlist.
624 * Modifies the argument inplace.
627 parseprop(nvlist_t
*props
, char *propname
)
631 if ((propval
= strchr(propname
, '=')) == NULL
) {
632 (void) fprintf(stderr
, gettext("missing "
633 "'=' for property=value argument\n"));
638 if (nvlist_exists(props
, propname
)) {
639 (void) fprintf(stderr
, gettext("property '%s' "
640 "specified multiple times\n"), propname
);
643 if (nvlist_add_string(props
, propname
, propval
) != 0)
649 * Take a property name argument and add it to the given nvlist.
650 * Modifies the argument inplace.
653 parsepropname(nvlist_t
*props
, char *propname
)
655 if (strchr(propname
, '=') != NULL
) {
656 (void) fprintf(stderr
, gettext("invalid character "
657 "'=' in property argument\n"));
660 if (nvlist_exists(props
, propname
)) {
661 (void) fprintf(stderr
, gettext("property '%s' "
662 "specified multiple times\n"), propname
);
665 if (nvlist_add_boolean(props
, propname
) != 0)
671 parse_depth(char *opt
, int *flags
)
676 depth
= (int)strtol(opt
, &tmp
, 0);
678 (void) fprintf(stderr
,
679 gettext("%s is not an integer\n"), optarg
);
683 (void) fprintf(stderr
,
684 gettext("Depth can not be negative.\n"));
687 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
691 #define PROGRESS_DELAY 2 /* seconds */
693 static const char *pt_reverse
=
694 "\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";
695 static time_t pt_begin
;
696 static char *pt_header
= NULL
;
697 static boolean_t pt_shown
;
700 start_progress_timer(void)
702 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
707 set_progress_header(const char *header
)
709 assert(pt_header
== NULL
);
710 pt_header
= safe_strdup(header
);
712 (void) printf("%s: ", header
);
713 (void) fflush(stdout
);
718 update_progress(const char *update
)
720 if (!pt_shown
&& time(NULL
) > pt_begin
) {
721 int len
= strlen(update
);
723 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
725 (void) fflush(stdout
);
727 } else if (pt_shown
) {
728 int len
= strlen(update
);
730 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
731 (void) fflush(stdout
);
736 finish_progress(const char *done
)
740 (void) fflush(stdout
);
747 zfs_mount_and_share(libzfs_handle_t
*hdl
, const char *dataset
, zfs_type_t type
)
749 zfs_handle_t
*zhp
= NULL
;
752 zhp
= zfs_open(hdl
, dataset
, type
);
757 * Volumes may neither be mounted or shared. Potentially in the
758 * future filesystems detected on these volumes could be mounted.
760 if (zfs_get_type(zhp
) == ZFS_TYPE_VOLUME
) {
766 * Mount and/or share the new filesystem as appropriate. We provide a
767 * verbose error message to let the user know that their filesystem was
768 * in fact created, even if we failed to mount or share it.
770 * If the user doesn't want the dataset automatically mounted, then
771 * skip the mount/share step
773 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
, B_FALSE
) &&
774 zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
) {
775 if (zfs_mount_delegation_check()) {
776 (void) fprintf(stderr
, gettext("filesystem "
777 "successfully created, but it may only be "
778 "mounted by root\n"));
780 } else if (zfs_mount(zhp
, NULL
, 0) != 0) {
781 (void) fprintf(stderr
, gettext("filesystem "
782 "successfully created, but not mounted\n"));
784 } else if (zfs_share(zhp
, NULL
) != 0) {
785 (void) fprintf(stderr
, gettext("filesystem "
786 "successfully created, but not shared\n"));
789 zfs_commit_shares(NULL
);
798 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
800 * Given an existing dataset, create a writable copy whose initial contents
801 * are the same as the source. The newly created dataset maintains a
802 * dependency on the original; the original cannot be destroyed so long as
805 * The '-p' flag creates all the non-existing ancestors of the target first.
808 zfs_do_clone(int argc
, char **argv
)
810 zfs_handle_t
*zhp
= NULL
;
811 boolean_t parents
= B_FALSE
;
816 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
820 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
823 if (!parseprop(props
, optarg
)) {
832 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
841 /* check number of arguments */
843 (void) fprintf(stderr
, gettext("missing source dataset "
848 (void) fprintf(stderr
, gettext("missing target dataset "
853 (void) fprintf(stderr
, gettext("too many arguments\n"));
857 /* open the source dataset */
858 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
) {
863 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
866 * Now create the ancestors of the target dataset. If the
867 * target already exists and '-p' option was used we should not
870 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
876 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
884 ret
= zfs_clone(zhp
, argv
[1], props
);
886 /* create the mountpoint if necessary */
889 (void) zpool_log_history(g_zfs
, history_str
);
890 log_history
= B_FALSE
;
893 ret
= zfs_mount_and_share(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
902 ASSERT3P(zhp
, ==, NULL
);
909 * Return a default volblocksize for the pool which always uses more than
910 * half of the data sectors. This primarily applies to dRAID which always
911 * writes full stripe widths.
914 default_volblocksize(zpool_handle_t
*zhp
, nvlist_t
*props
)
916 uint64_t volblocksize
, asize
= SPA_MINBLOCKSIZE
;
917 nvlist_t
*tree
, **vdevs
;
920 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
922 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
) != 0 ||
923 nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
,
924 &vdevs
, &nvdevs
) != 0) {
925 return (ZVOL_DEFAULT_BLOCKSIZE
);
928 for (int i
= 0; i
< nvdevs
; i
++) {
929 nvlist_t
*nv
= vdevs
[i
];
930 uint64_t ashift
, ndata
, nparity
;
932 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASHIFT
, &ashift
) != 0)
935 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DRAID_NDATA
,
937 /* dRAID minimum allocation width */
938 asize
= MAX(asize
, ndata
* (1ULL << ashift
));
939 } else if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
941 /* raidz minimum allocation width */
943 asize
= MAX(asize
, 2 * (1ULL << ashift
));
945 asize
= MAX(asize
, 4 * (1ULL << ashift
));
947 /* mirror or (non-redundant) leaf vdev */
948 asize
= MAX(asize
, 1ULL << ashift
);
953 * Calculate the target volblocksize such that more than half
954 * of the asize is used. The following table is for 4k sectors.
956 * n asize blksz used | n asize blksz used
957 * -------------------------+---------------------------------
958 * 1 4,096 8,192 100% | 9 36,864 32,768 88%
959 * 2 8,192 8,192 100% | 10 40,960 32,768 80%
960 * 3 12,288 8,192 66% | 11 45,056 32,768 72%
961 * 4 16,384 16,384 100% | 12 49,152 32,768 66%
962 * 5 20,480 16,384 80% | 13 53,248 32,768 61%
963 * 6 24,576 16,384 66% | 14 57,344 32,768 57%
964 * 7 28,672 16,384 57% | 15 61,440 32,768 53%
965 * 8 32,768 32,768 100% | 16 65,536 65,636 100%
967 * This is primarily a concern for dRAID which always allocates
968 * a full stripe width. For dRAID the default stripe width is
969 * n=8 in which case the volblocksize is set to 32k. Ignoring
970 * compression there are no unused sectors. This same reasoning
971 * applies to raidz[2,3] so target 4 sectors to minimize waste.
973 uint64_t tgt_volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
974 while (tgt_volblocksize
* 2 <= asize
)
975 tgt_volblocksize
*= 2;
977 const char *prop
= zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
);
978 if (nvlist_lookup_uint64(props
, prop
, &volblocksize
) == 0) {
980 /* Issue a warning when a non-optimal size is requested. */
981 if (volblocksize
< ZVOL_DEFAULT_BLOCKSIZE
) {
982 (void) fprintf(stderr
, gettext("Warning: "
983 "volblocksize (%llu) is less than the default "
984 "minimum block size (%llu).\nTo reduce wasted "
985 "space a volblocksize of %llu is recommended.\n"),
986 (u_longlong_t
)volblocksize
,
987 (u_longlong_t
)ZVOL_DEFAULT_BLOCKSIZE
,
988 (u_longlong_t
)tgt_volblocksize
);
989 } else if (volblocksize
< tgt_volblocksize
) {
990 (void) fprintf(stderr
, gettext("Warning: "
991 "volblocksize (%llu) is much less than the "
992 "minimum allocation\nunit (%llu), which wastes "
993 "at least %llu%% of space. To reduce wasted "
994 "space,\nuse a larger volblocksize (%llu is "
995 "recommended), fewer dRAID data disks\n"
996 "per group, or smaller sector size (ashift).\n"),
997 (u_longlong_t
)volblocksize
, (u_longlong_t
)asize
,
998 (u_longlong_t
)((100 * (asize
- volblocksize
)) /
999 asize
), (u_longlong_t
)tgt_volblocksize
);
1002 volblocksize
= tgt_volblocksize
;
1003 fnvlist_add_uint64(props
, prop
, volblocksize
);
1006 return (volblocksize
);
1010 * zfs create [-Pnpv] [-o prop=value] ... fs
1011 * zfs create [-Pnpsv] [-b blocksize] [-o prop=value] ... -V vol size
1013 * Create a new dataset. This command can be used to create filesystems
1014 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
1015 * For volumes, the user must specify a size to be used.
1017 * The '-s' flag applies only to volumes, and indicates that we should not try
1018 * to set the reservation for this volume. By default we set a reservation
1019 * equal to the size for any volume. For pools with SPA_VERSION >=
1020 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
1022 * The '-p' flag creates all the non-existing ancestors of the target first.
1024 * The '-n' flag is no-op (dry run) mode. This will perform a user-space sanity
1025 * check of arguments and properties, but does not check for permissions,
1026 * available space, etc.
1028 * The '-u' flag prevents the newly created file system from being mounted.
1030 * The '-v' flag is for verbose output.
1032 * The '-P' flag is used for parseable output. It implies '-v'.
1035 zfs_do_create(int argc
, char **argv
)
1037 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
1038 zpool_handle_t
*zpool_handle
= NULL
;
1039 nvlist_t
*real_props
= NULL
;
1040 uint64_t volsize
= 0;
1042 boolean_t noreserve
= B_FALSE
;
1043 boolean_t bflag
= B_FALSE
;
1044 boolean_t parents
= B_FALSE
;
1045 boolean_t dryrun
= B_FALSE
;
1046 boolean_t nomount
= B_FALSE
;
1047 boolean_t verbose
= B_FALSE
;
1048 boolean_t parseable
= B_FALSE
;
1054 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
1058 while ((c
= getopt(argc
, argv
, ":PV:b:nso:puv")) != -1) {
1061 type
= ZFS_TYPE_VOLUME
;
1062 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
1063 (void) fprintf(stderr
, gettext("bad volume "
1064 "size '%s': %s\n"), optarg
,
1065 libzfs_error_description(g_zfs
));
1069 if (nvlist_add_uint64(props
,
1070 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
1083 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
1084 (void) fprintf(stderr
, gettext("bad volume "
1085 "block size '%s': %s\n"), optarg
,
1086 libzfs_error_description(g_zfs
));
1090 if (nvlist_add_uint64(props
,
1091 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1099 if (!parseprop(props
, optarg
))
1112 (void) fprintf(stderr
, gettext("missing size "
1116 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1122 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
1123 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
1124 "used when creating a volume\n"));
1127 if (nomount
&& type
!= ZFS_TYPE_FILESYSTEM
) {
1128 (void) fprintf(stderr
, gettext("'-u' can only be "
1129 "used when creating a filesystem\n"));
1136 /* check number of arguments */
1138 (void) fprintf(stderr
, gettext("missing %s argument\n"),
1139 zfs_type_to_name(type
));
1143 (void) fprintf(stderr
, gettext("too many arguments\n"));
1147 if (dryrun
|| type
== ZFS_TYPE_VOLUME
) {
1148 char msg
[ZFS_MAX_DATASET_NAME_LEN
* 2];
1151 if ((p
= strchr(argv
[0], '/')) != NULL
)
1153 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
1156 if (zpool_handle
== NULL
)
1159 (void) snprintf(msg
, sizeof (msg
),
1160 dryrun
? gettext("cannot verify '%s'") :
1161 gettext("cannot create '%s'"), argv
[0]);
1162 if (props
&& (real_props
= zfs_valid_proplist(g_zfs
, type
,
1163 props
, 0, NULL
, zpool_handle
, B_TRUE
, msg
)) == NULL
) {
1164 zpool_close(zpool_handle
);
1169 if (type
== ZFS_TYPE_VOLUME
) {
1170 const char *prop
= zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
);
1171 uint64_t volblocksize
= default_volblocksize(zpool_handle
,
1174 if (volblocksize
!= ZVOL_DEFAULT_BLOCKSIZE
&&
1175 nvlist_lookup_string(props
, prop
, &strval
) != 0) {
1176 if (asprintf(&strval
, "%llu",
1177 (u_longlong_t
)volblocksize
) == -1)
1179 nvlist_add_string(props
, prop
, strval
);
1184 * If volsize is not a multiple of volblocksize, round it
1185 * up to the nearest multiple of the volblocksize.
1187 if (volsize
% volblocksize
) {
1188 volsize
= P2ROUNDUP_TYPED(volsize
, volblocksize
,
1191 if (nvlist_add_uint64(props
,
1192 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), volsize
) != 0) {
1199 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
1200 uint64_t spa_version
;
1201 zfs_prop_t resv_prop
;
1203 spa_version
= zpool_get_prop_int(zpool_handle
,
1204 ZPOOL_PROP_VERSION
, NULL
);
1205 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
1206 resv_prop
= ZFS_PROP_REFRESERVATION
;
1208 resv_prop
= ZFS_PROP_RESERVATION
;
1210 volsize
= zvol_volsize_to_reservation(zpool_handle
, volsize
,
1213 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
1215 if (nvlist_add_uint64(props
,
1216 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
1222 if (zpool_handle
!= NULL
) {
1223 zpool_close(zpool_handle
);
1224 nvlist_free(real_props
);
1227 if (parents
&& zfs_name_valid(argv
[0], type
)) {
1229 * Now create the ancestors of target dataset. If the target
1230 * already exists and '-p' option was used we should not
1233 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
1238 (void) printf(parseable
? "create_ancestors\t%s\n" :
1239 dryrun
? "would create ancestors of %s\n" :
1240 "create ancestors of %s\n", argv
[0]);
1243 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0) {
1250 nvpair_t
*nvp
= NULL
;
1251 (void) printf(parseable
? "create\t%s\n" :
1252 dryrun
? "would create %s\n" : "create %s\n", argv
[0]);
1253 while ((nvp
= nvlist_next_nvpair(props
, nvp
)) != NULL
) {
1257 switch (nvpair_type(nvp
)) {
1258 case DATA_TYPE_UINT64
:
1259 VERIFY0(nvpair_value_uint64(nvp
, &uval
));
1260 (void) printf(parseable
?
1261 "property\t%s\t%llu\n" : "\t%s=%llu\n",
1262 nvpair_name(nvp
), (u_longlong_t
)uval
);
1264 case DATA_TYPE_STRING
:
1265 VERIFY0(nvpair_value_string(nvp
, &sval
));
1266 (void) printf(parseable
?
1267 "property\t%s\t%s\n" : "\t%s=%s\n",
1268 nvpair_name(nvp
), sval
);
1271 (void) fprintf(stderr
, "property '%s' "
1272 "has illegal type %d\n",
1273 nvpair_name(nvp
), nvpair_type(nvp
));
1283 /* pass to libzfs */
1284 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
1288 (void) zpool_log_history(g_zfs
, history_str
);
1289 log_history
= B_FALSE
;
1297 ret
= zfs_mount_and_share(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
1308 * zfs destroy [-rRf] <fs, vol>
1309 * zfs destroy [-rRd] <snap>
1311 * -r Recursively destroy all children
1312 * -R Recursively destroy all dependents, including clones
1313 * -f Force unmounting of any dependents
1314 * -d If we can't destroy now, mark for deferred destruction
1316 * Destroys the given dataset. By default, it will unmount any filesystems,
1317 * and refuse to destroy a dataset that has any dependents. A dependent can
1318 * either be a child, or a clone of a child.
1320 typedef struct destroy_cbdata
{
1323 boolean_t cb_recurse
;
1325 boolean_t cb_doclones
;
1326 zfs_handle_t
*cb_target
;
1327 boolean_t cb_defer_destroy
;
1328 boolean_t cb_verbose
;
1329 boolean_t cb_parsable
;
1330 boolean_t cb_dryrun
;
1332 nvlist_t
*cb_batchedsnaps
;
1334 /* first snap in contiguous run */
1336 /* previous snap in contiguous run */
1338 int64_t cb_snapused
;
1341 uint64_t cb_snap_count
;
1345 * Check for any dependents based on the '-r' or '-R' flags.
1348 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
1350 destroy_cbdata_t
*cbp
= data
;
1351 const char *tname
= zfs_get_name(cbp
->cb_target
);
1352 const char *name
= zfs_get_name(zhp
);
1354 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
1355 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
1357 * This is a direct descendant, not a clone somewhere else in
1360 if (cbp
->cb_recurse
)
1363 if (cbp
->cb_first
) {
1364 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1365 "%s has children\n"),
1366 zfs_get_name(cbp
->cb_target
),
1367 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1368 (void) fprintf(stderr
, gettext("use '-r' to destroy "
1369 "the following datasets:\n"));
1370 cbp
->cb_first
= B_FALSE
;
1371 cbp
->cb_error
= B_TRUE
;
1374 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1377 * This is a clone. We only want to report this if the '-r'
1378 * wasn't specified, or the target is a snapshot.
1380 if (!cbp
->cb_recurse
&&
1381 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
1384 if (cbp
->cb_first
) {
1385 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1386 "%s has dependent clones\n"),
1387 zfs_get_name(cbp
->cb_target
),
1388 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
1389 (void) fprintf(stderr
, gettext("use '-R' to destroy "
1390 "the following datasets:\n"));
1391 cbp
->cb_first
= B_FALSE
;
1392 cbp
->cb_error
= B_TRUE
;
1393 cbp
->cb_dryrun
= B_TRUE
;
1396 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
1405 destroy_batched(destroy_cbdata_t
*cb
)
1407 int error
= zfs_destroy_snaps_nvl(g_zfs
,
1408 cb
->cb_batchedsnaps
, B_FALSE
);
1409 fnvlist_free(cb
->cb_batchedsnaps
);
1410 cb
->cb_batchedsnaps
= fnvlist_alloc();
1415 destroy_callback(zfs_handle_t
*zhp
, void *data
)
1417 destroy_cbdata_t
*cb
= data
;
1418 const char *name
= zfs_get_name(zhp
);
1421 if (cb
->cb_verbose
) {
1422 if (cb
->cb_parsable
) {
1423 (void) printf("destroy\t%s\n", name
);
1424 } else if (cb
->cb_dryrun
) {
1425 (void) printf(gettext("would destroy %s\n"),
1428 (void) printf(gettext("will destroy %s\n"),
1434 * Ignore pools (which we've already flagged as an error before getting
1437 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
1438 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1442 if (cb
->cb_dryrun
) {
1448 * We batch up all contiguous snapshots (even of different
1449 * filesystems) and destroy them with one ioctl. We can't
1450 * simply do all snap deletions and then all fs deletions,
1451 * because we must delete a clone before its origin.
1453 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) {
1454 cb
->cb_snap_count
++;
1455 fnvlist_add_boolean(cb
->cb_batchedsnaps
, name
);
1456 if (cb
->cb_snap_count
% 10 == 0 && cb
->cb_defer_destroy
)
1457 error
= destroy_batched(cb
);
1459 error
= destroy_batched(cb
);
1461 zfs_unmount(zhp
, NULL
, cb
->cb_force
? MS_FORCE
: 0) != 0 ||
1462 zfs_destroy(zhp
, cb
->cb_defer_destroy
) != 0) {
1465 * When performing a recursive destroy we ignore errors
1466 * so that the recursive destroy could continue
1467 * destroying past problem datasets
1469 if (cb
->cb_recurse
) {
1470 cb
->cb_error
= B_TRUE
;
1482 destroy_print_cb(zfs_handle_t
*zhp
, void *arg
)
1484 destroy_cbdata_t
*cb
= arg
;
1485 const char *name
= zfs_get_name(zhp
);
1488 if (nvlist_exists(cb
->cb_nvl
, name
)) {
1489 if (cb
->cb_firstsnap
== NULL
)
1490 cb
->cb_firstsnap
= strdup(name
);
1491 if (cb
->cb_prevsnap
!= NULL
)
1492 free(cb
->cb_prevsnap
);
1493 /* this snap continues the current range */
1494 cb
->cb_prevsnap
= strdup(name
);
1495 if (cb
->cb_firstsnap
== NULL
|| cb
->cb_prevsnap
== NULL
)
1497 if (cb
->cb_verbose
) {
1498 if (cb
->cb_parsable
) {
1499 (void) printf("destroy\t%s\n", name
);
1500 } else if (cb
->cb_dryrun
) {
1501 (void) printf(gettext("would destroy %s\n"),
1504 (void) printf(gettext("will destroy %s\n"),
1508 } else if (cb
->cb_firstsnap
!= NULL
) {
1509 /* end of this range */
1511 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1512 cb
->cb_prevsnap
, &used
);
1513 cb
->cb_snapused
+= used
;
1514 free(cb
->cb_firstsnap
);
1515 cb
->cb_firstsnap
= NULL
;
1516 free(cb
->cb_prevsnap
);
1517 cb
->cb_prevsnap
= NULL
;
1524 destroy_print_snapshots(zfs_handle_t
*fs_zhp
, destroy_cbdata_t
*cb
)
1527 assert(cb
->cb_firstsnap
== NULL
);
1528 assert(cb
->cb_prevsnap
== NULL
);
1529 err
= zfs_iter_snapshots_sorted(fs_zhp
, destroy_print_cb
, cb
, 0, 0);
1530 if (cb
->cb_firstsnap
!= NULL
) {
1533 err
= lzc_snaprange_space(cb
->cb_firstsnap
,
1534 cb
->cb_prevsnap
, &used
);
1536 cb
->cb_snapused
+= used
;
1537 free(cb
->cb_firstsnap
);
1538 cb
->cb_firstsnap
= NULL
;
1539 free(cb
->cb_prevsnap
);
1540 cb
->cb_prevsnap
= NULL
;
1546 snapshot_to_nvl_cb(zfs_handle_t
*zhp
, void *arg
)
1548 destroy_cbdata_t
*cb
= arg
;
1551 /* Check for clones. */
1552 if (!cb
->cb_doclones
&& !cb
->cb_defer_destroy
) {
1553 cb
->cb_target
= zhp
;
1554 cb
->cb_first
= B_TRUE
;
1555 err
= zfs_iter_dependents(zhp
, B_TRUE
,
1556 destroy_check_dependent
, cb
);
1560 if (nvlist_add_boolean(cb
->cb_nvl
, zfs_get_name(zhp
)))
1568 gather_snapshots(zfs_handle_t
*zhp
, void *arg
)
1570 destroy_cbdata_t
*cb
= arg
;
1573 err
= zfs_iter_snapspec(zhp
, cb
->cb_snapspec
, snapshot_to_nvl_cb
, cb
);
1579 if (cb
->cb_verbose
) {
1580 err
= destroy_print_snapshots(zhp
, cb
);
1586 err
= zfs_iter_filesystems(zhp
, gather_snapshots
, cb
);
1594 destroy_clones(destroy_cbdata_t
*cb
)
1597 for (pair
= nvlist_next_nvpair(cb
->cb_nvl
, NULL
);
1599 pair
= nvlist_next_nvpair(cb
->cb_nvl
, pair
)) {
1600 zfs_handle_t
*zhp
= zfs_open(g_zfs
, nvpair_name(pair
),
1603 boolean_t defer
= cb
->cb_defer_destroy
;
1607 * We can't defer destroy non-snapshots, so set it to
1608 * false while destroying the clones.
1610 cb
->cb_defer_destroy
= B_FALSE
;
1611 err
= zfs_iter_dependents(zhp
, B_FALSE
,
1612 destroy_callback
, cb
);
1613 cb
->cb_defer_destroy
= defer
;
1623 zfs_do_destroy(int argc
, char **argv
)
1625 destroy_cbdata_t cb
= { 0 };
1629 zfs_handle_t
*zhp
= NULL
;
1631 zfs_type_t type
= ZFS_TYPE_DATASET
;
1634 while ((c
= getopt(argc
, argv
, "vpndfrR")) != -1) {
1637 cb
.cb_verbose
= B_TRUE
;
1640 cb
.cb_verbose
= B_TRUE
;
1641 cb
.cb_parsable
= B_TRUE
;
1644 cb
.cb_dryrun
= B_TRUE
;
1647 cb
.cb_defer_destroy
= B_TRUE
;
1648 type
= ZFS_TYPE_SNAPSHOT
;
1651 cb
.cb_force
= B_TRUE
;
1654 cb
.cb_recurse
= B_TRUE
;
1657 cb
.cb_recurse
= B_TRUE
;
1658 cb
.cb_doclones
= B_TRUE
;
1662 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1671 /* check number of arguments */
1673 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1677 (void) fprintf(stderr
, gettext("too many arguments\n"));
1681 at
= strchr(argv
[0], '@');
1682 pound
= strchr(argv
[0], '#');
1685 /* Build the list of snaps to destroy in cb_nvl. */
1686 cb
.cb_nvl
= fnvlist_alloc();
1689 zhp
= zfs_open(g_zfs
, argv
[0],
1690 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1692 nvlist_free(cb
.cb_nvl
);
1696 cb
.cb_snapspec
= at
+ 1;
1697 if (gather_snapshots(zfs_handle_dup(zhp
), &cb
) != 0 ||
1703 if (nvlist_empty(cb
.cb_nvl
)) {
1704 (void) fprintf(stderr
, gettext("could not find any "
1705 "snapshots to destroy; check snapshot names.\n"));
1710 if (cb
.cb_verbose
) {
1712 zfs_nicebytes(cb
.cb_snapused
, buf
, sizeof (buf
));
1713 if (cb
.cb_parsable
) {
1714 (void) printf("reclaim\t%llu\n",
1715 (u_longlong_t
)cb
.cb_snapused
);
1716 } else if (cb
.cb_dryrun
) {
1717 (void) printf(gettext("would reclaim %s\n"),
1720 (void) printf(gettext("will reclaim %s\n"),
1725 if (!cb
.cb_dryrun
) {
1726 if (cb
.cb_doclones
) {
1727 cb
.cb_batchedsnaps
= fnvlist_alloc();
1728 err
= destroy_clones(&cb
);
1730 err
= zfs_destroy_snaps_nvl(g_zfs
,
1731 cb
.cb_batchedsnaps
, B_FALSE
);
1739 err
= zfs_destroy_snaps_nvl(g_zfs
, cb
.cb_nvl
,
1740 cb
.cb_defer_destroy
);
1746 } else if (pound
!= NULL
) {
1751 (void) fprintf(stderr
,
1752 "dryrun is not supported with bookmark\n");
1756 if (cb
.cb_defer_destroy
) {
1757 (void) fprintf(stderr
,
1758 "defer destroy is not supported with bookmark\n");
1762 if (cb
.cb_recurse
) {
1763 (void) fprintf(stderr
,
1764 "recursive is not supported with bookmark\n");
1769 * Unfortunately, zfs_bookmark() doesn't honor the
1770 * casesensitivity setting. However, we can't simply
1771 * remove this check, because lzc_destroy_bookmarks()
1772 * ignores non-existent bookmarks, so this is necessary
1773 * to get a proper error message.
1775 if (!zfs_bookmark_exists(argv
[0])) {
1776 (void) fprintf(stderr
, gettext("bookmark '%s' "
1777 "does not exist.\n"), argv
[0]);
1781 nvl
= fnvlist_alloc();
1782 fnvlist_add_boolean(nvl
, argv
[0]);
1784 err
= lzc_destroy_bookmarks(nvl
, NULL
);
1786 (void) zfs_standard_error(g_zfs
, err
,
1787 "cannot destroy bookmark");
1794 /* Open the given dataset */
1795 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1801 * Perform an explicit check for pools before going any further.
1803 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1804 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1805 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1806 "operation does not apply to pools\n"),
1808 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1809 "%s' to destroy all datasets in the pool\n"),
1811 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1812 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1818 * Check for any dependents and/or clones.
1820 cb
.cb_first
= B_TRUE
;
1821 if (!cb
.cb_doclones
&&
1822 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1832 cb
.cb_batchedsnaps
= fnvlist_alloc();
1833 if (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
,
1840 * Do the real thing. The callback will close the
1841 * handle regardless of whether it succeeds or not.
1843 err
= destroy_callback(zhp
, &cb
);
1846 err
= zfs_destroy_snaps_nvl(g_zfs
,
1847 cb
.cb_batchedsnaps
, cb
.cb_defer_destroy
);
1849 if (err
!= 0 || cb
.cb_error
== B_TRUE
)
1854 fnvlist_free(cb
.cb_batchedsnaps
);
1855 fnvlist_free(cb
.cb_nvl
);
1862 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1865 zfs_get_column_t col
;
1867 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1868 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1869 if (col
== GET_COL_RECVD
)
1875 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1876 * < all | property[,property]... > < fs | snap | vol > ...
1878 * -r recurse over any child datasets
1879 * -H scripted mode. Headers are stripped, and fields are separated
1880 * by tabs instead of spaces.
1881 * -o Set of fields to display. One of "name,property,value,
1882 * received,source". Default is "name,property,value,source".
1883 * "all" is an alias for all five.
1884 * -s Set of sources to allow. One of
1885 * "local,default,inherited,received,temporary,none". Default is
1887 * -p Display values in parsable (literal) format.
1889 * Prints properties for the given datasets. The user can control which
1890 * columns to display as well as which property types to allow.
1894 * Invoked to display the properties for a single dataset.
1897 get_callback(zfs_handle_t
*zhp
, void *data
)
1899 char buf
[ZFS_MAXPROPLEN
];
1900 char rbuf
[ZFS_MAXPROPLEN
];
1901 zprop_source_t sourcetype
;
1902 char source
[ZFS_MAX_DATASET_NAME_LEN
];
1903 zprop_get_cbdata_t
*cbp
= data
;
1904 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1905 zprop_list_t
*pl
= cbp
->cb_proplist
;
1908 const char *sourceval
;
1909 boolean_t received
= is_recvd_column(cbp
);
1911 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1912 char *recvdval
= NULL
;
1914 * Skip the special fake placeholder. This will also skip over
1915 * the name property when 'all' is specified.
1917 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1918 pl
== cbp
->cb_proplist
)
1921 if (pl
->pl_prop
!= ZPROP_USERPROP
) {
1922 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1923 sizeof (buf
), &sourcetype
, source
,
1925 cbp
->cb_literal
) != 0) {
1928 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1929 ZFS_TYPE_DATASET
, B_FALSE
)) {
1930 (void) fprintf(stderr
,
1931 gettext("No such property '%s'\n"),
1932 zfs_prop_to_name(pl
->pl_prop
));
1935 sourcetype
= ZPROP_SRC_NONE
;
1936 (void) strlcpy(buf
, "-", sizeof (buf
));
1939 if (received
&& (zfs_prop_get_recvd(zhp
,
1940 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1941 cbp
->cb_literal
) == 0))
1944 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1945 zfs_prop_to_name(pl
->pl_prop
),
1946 buf
, sourcetype
, source
, recvdval
);
1947 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1948 sourcetype
= ZPROP_SRC_LOCAL
;
1950 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1951 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1952 sourcetype
= ZPROP_SRC_NONE
;
1953 (void) strlcpy(buf
, "-", sizeof (buf
));
1956 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1957 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1958 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
1959 sourcetype
= ZPROP_SRC_LOCAL
;
1961 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
1962 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1963 sourcetype
= ZPROP_SRC_NONE
;
1964 (void) strlcpy(buf
, "-", sizeof (buf
));
1967 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1968 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1970 if (nvlist_lookup_nvlist(user_props
,
1971 pl
->pl_user_prop
, &propval
) != 0) {
1974 sourcetype
= ZPROP_SRC_NONE
;
1977 strval
= fnvlist_lookup_string(propval
,
1979 sourceval
= fnvlist_lookup_string(propval
,
1982 if (strcmp(sourceval
,
1983 zfs_get_name(zhp
)) == 0) {
1984 sourcetype
= ZPROP_SRC_LOCAL
;
1985 } else if (strcmp(sourceval
,
1986 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1987 sourcetype
= ZPROP_SRC_RECEIVED
;
1989 sourcetype
= ZPROP_SRC_INHERITED
;
1990 (void) strlcpy(source
,
1991 sourceval
, sizeof (source
));
1995 if (received
&& (zfs_prop_get_recvd(zhp
,
1996 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1997 cbp
->cb_literal
) == 0))
2000 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
2001 pl
->pl_user_prop
, strval
, sourcetype
,
2010 zfs_do_get(int argc
, char **argv
)
2012 zprop_get_cbdata_t cb
= { 0 };
2013 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2014 int types
= ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
;
2018 zprop_list_t fake_name
= { 0 };
2021 * Set up default columns and sources.
2023 cb
.cb_sources
= ZPROP_SRC_ALL
;
2024 cb
.cb_columns
[0] = GET_COL_NAME
;
2025 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
2026 cb
.cb_columns
[2] = GET_COL_VALUE
;
2027 cb
.cb_columns
[3] = GET_COL_SOURCE
;
2028 cb
.cb_type
= ZFS_TYPE_DATASET
;
2031 while ((c
= getopt(argc
, argv
, ":d:o:s:rt:Hp")) != -1) {
2034 cb
.cb_literal
= B_TRUE
;
2037 limit
= parse_depth(optarg
, &flags
);
2040 flags
|= ZFS_ITER_RECURSE
;
2043 cb
.cb_scripted
= B_TRUE
;
2046 (void) fprintf(stderr
, gettext("missing argument for "
2047 "'%c' option\n"), optopt
);
2052 * Process the set of columns to display. We zero out
2053 * the structure to give us a blank slate.
2055 memset(&cb
.cb_columns
, 0, sizeof (cb
.cb_columns
));
2058 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
2059 static const char *const col_subopts
[] =
2060 { "name", "property", "value",
2061 "received", "source", "all" };
2062 static const zfs_get_column_t col_subopt_col
[] =
2063 { GET_COL_NAME
, GET_COL_PROPERTY
, GET_COL_VALUE
,
2064 GET_COL_RECVD
, GET_COL_SOURCE
};
2065 static const int col_subopt_flags
[] =
2066 { 0, 0, 0, ZFS_ITER_RECVD_PROPS
, 0 };
2068 if (i
== ZFS_GET_NCOLS
) {
2069 (void) fprintf(stderr
, gettext("too "
2070 "many fields given to -o "
2075 for (c
= 0; c
< ARRAY_SIZE(col_subopts
); ++c
)
2076 if (strcmp(tok
, col_subopts
[c
]) == 0)
2079 (void) fprintf(stderr
,
2080 gettext("invalid column name '%s'\n"), tok
);
2086 (void) fprintf(stderr
,
2087 gettext("\"all\" conflicts "
2088 "with specific fields "
2089 "given to -o option\n"));
2093 memcpy(cb
.cb_columns
, col_subopt_col
,
2094 sizeof (col_subopt_col
));
2095 flags
|= ZFS_ITER_RECVD_PROPS
;
2098 cb
.cb_columns
[i
++] = col_subopt_col
[c
];
2099 flags
|= col_subopt_flags
[c
];
2107 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
2108 static const char *const source_opt
[] = {
2110 "inherited", "received",
2111 "temporary", "none" };
2112 static const int source_flg
[] = {
2113 ZPROP_SRC_LOCAL
, ZPROP_SRC_DEFAULT
,
2114 ZPROP_SRC_INHERITED
, ZPROP_SRC_RECEIVED
,
2115 ZPROP_SRC_TEMPORARY
, ZPROP_SRC_NONE
};
2117 for (i
= 0; i
< ARRAY_SIZE(source_opt
); ++i
)
2118 if (strcmp(tok
, source_opt
[i
]) == 0) {
2119 cb
.cb_sources
|= source_flg
[i
];
2123 (void) fprintf(stderr
,
2124 gettext("invalid source '%s'\n"), tok
);
2132 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
2134 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
2135 static const char *const type_opts
[] = {
2136 "filesystem", "volume",
2140 static const int type_types
[] = {
2141 ZFS_TYPE_FILESYSTEM
, ZFS_TYPE_VOLUME
,
2142 ZFS_TYPE_SNAPSHOT
, ZFS_TYPE_SNAPSHOT
,
2144 ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
};
2146 for (i
= 0; i
< ARRAY_SIZE(type_opts
); ++i
)
2147 if (strcmp(tok
, type_opts
[i
]) == 0) {
2148 types
|= type_types
[i
];
2152 (void) fprintf(stderr
,
2153 gettext("invalid type '%s'\n"), tok
);
2160 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2170 (void) fprintf(stderr
, gettext("missing property "
2178 * Handle users who want to get all snapshots or bookmarks
2179 * of a dataset (ex. 'zfs get -t snapshot refer <dataset>').
2181 if ((types
== ZFS_TYPE_SNAPSHOT
|| types
== ZFS_TYPE_BOOKMARK
) &&
2182 argc
> 1 && (flags
& ZFS_ITER_RECURSE
) == 0 && limit
== 0) {
2183 flags
|= (ZFS_ITER_DEPTH_LIMIT
| ZFS_ITER_RECURSE
);
2187 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
2195 * As part of zfs_expand_proplist(), we keep track of the maximum column
2196 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
2197 * need to know the maximum name length. However, the user likely did
2198 * not specify 'name' as one of the properties to fetch, so we need to
2199 * make sure we always include at least this property for
2200 * print_get_headers() to work properly.
2202 if (cb
.cb_proplist
!= NULL
) {
2203 fake_name
.pl_prop
= ZFS_PROP_NAME
;
2204 fake_name
.pl_width
= strlen(gettext("NAME"));
2205 fake_name
.pl_next
= cb
.cb_proplist
;
2206 cb
.cb_proplist
= &fake_name
;
2209 cb
.cb_first
= B_TRUE
;
2211 /* run for each object */
2212 ret
= zfs_for_each(argc
, argv
, flags
, types
, NULL
,
2213 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
2215 if (cb
.cb_proplist
== &fake_name
)
2216 zprop_free_list(fake_name
.pl_next
);
2218 zprop_free_list(cb
.cb_proplist
);
2224 * inherit [-rS] <property> <fs|vol> ...
2226 * -r Recurse over all children
2227 * -S Revert to received value, if any
2229 * For each dataset specified on the command line, inherit the given property
2230 * from its parent. Inheriting a property at the pool level will cause it to
2231 * use the default value. The '-r' flag will recurse over all children, and is
2232 * useful for setting a property on a hierarchy-wide basis, regardless of any
2233 * local modifications for each dataset.
2236 typedef struct inherit_cbdata
{
2237 const char *cb_propname
;
2238 boolean_t cb_received
;
2242 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
2244 inherit_cbdata_t
*cb
= data
;
2245 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
2248 * If we're doing it recursively, then ignore properties that
2249 * are not valid for this type of dataset.
2251 if (prop
!= ZPROP_INVAL
&&
2252 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
), B_FALSE
))
2255 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
2259 inherit_cb(zfs_handle_t
*zhp
, void *data
)
2261 inherit_cbdata_t
*cb
= data
;
2263 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
2267 zfs_do_inherit(int argc
, char **argv
)
2271 inherit_cbdata_t cb
= { 0 };
2275 boolean_t received
= B_FALSE
;
2278 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
2281 flags
|= ZFS_ITER_RECURSE
;
2288 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2297 /* check number of arguments */
2299 (void) fprintf(stderr
, gettext("missing property argument\n"));
2303 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
2311 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_USERPROP
) {
2312 if (zfs_prop_readonly(prop
)) {
2313 (void) fprintf(stderr
, gettext(
2314 "%s property is read-only\n"),
2318 if (!zfs_prop_inheritable(prop
) && !received
) {
2319 (void) fprintf(stderr
, gettext("'%s' property cannot "
2320 "be inherited\n"), propname
);
2321 if (prop
== ZFS_PROP_QUOTA
||
2322 prop
== ZFS_PROP_RESERVATION
||
2323 prop
== ZFS_PROP_REFQUOTA
||
2324 prop
== ZFS_PROP_REFRESERVATION
) {
2325 (void) fprintf(stderr
, gettext("use 'zfs set "
2326 "%s=none' to clear\n"), propname
);
2327 (void) fprintf(stderr
, gettext("use 'zfs "
2328 "inherit -S %s' to revert to received "
2329 "value\n"), propname
);
2333 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
2334 prop
== ZFS_PROP_VERSION
)) {
2335 (void) fprintf(stderr
, gettext("'%s' property cannot "
2336 "be reverted to a received value\n"), propname
);
2339 } else if (!zfs_prop_user(propname
)) {
2340 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
2345 cb
.cb_propname
= propname
;
2346 cb
.cb_received
= received
;
2348 if (flags
& ZFS_ITER_RECURSE
) {
2349 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
2350 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
2352 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
2353 NULL
, NULL
, 0, inherit_cb
, &cb
);
2359 typedef struct upgrade_cbdata
{
2360 uint64_t cb_numupgraded
;
2361 uint64_t cb_numsamegraded
;
2362 uint64_t cb_numfailed
;
2363 uint64_t cb_version
;
2365 boolean_t cb_foundone
;
2366 char cb_lastfs
[ZFS_MAX_DATASET_NAME_LEN
];
2370 same_pool(zfs_handle_t
*zhp
, const char *name
)
2372 int len1
= strcspn(name
, "/@");
2373 const char *zhname
= zfs_get_name(zhp
);
2374 int len2
= strcspn(zhname
, "/@");
2378 return (strncmp(name
, zhname
, len1
) == 0);
2382 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
2384 upgrade_cbdata_t
*cb
= data
;
2385 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2387 /* list if it's old/new */
2388 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
2389 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
2392 str
= gettext("The following filesystems are "
2393 "formatted using a newer software version and\n"
2394 "cannot be accessed on the current system.\n\n");
2396 str
= gettext("The following filesystems are "
2397 "out of date, and can be upgraded. After being\n"
2398 "upgraded, these filesystems (and any 'zfs send' "
2399 "streams generated from\n"
2400 "subsequent snapshots) will no longer be "
2401 "accessible by older software versions.\n\n");
2404 if (!cb
->cb_foundone
) {
2406 (void) printf(gettext("VER FILESYSTEM\n"));
2407 (void) printf(gettext("--- ------------\n"));
2408 cb
->cb_foundone
= B_TRUE
;
2411 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
2418 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
2420 upgrade_cbdata_t
*cb
= data
;
2421 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
2422 int needed_spa_version
;
2425 if (zfs_spa_version(zhp
, &spa_version
) < 0)
2428 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
2430 if (needed_spa_version
< 0)
2433 if (spa_version
< needed_spa_version
) {
2435 (void) printf(gettext("%s: can not be "
2436 "upgraded; the pool version needs to first "
2437 "be upgraded\nto version %d\n\n"),
2438 zfs_get_name(zhp
), needed_spa_version
);
2444 if (version
< cb
->cb_version
) {
2446 (void) snprintf(verstr
, sizeof (verstr
),
2447 "%llu", (u_longlong_t
)cb
->cb_version
);
2448 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
2450 * If they did "zfs upgrade -a", then we could
2451 * be doing ioctls to different pools. We need
2452 * to log this history once to each pool, and bypass
2453 * the normal history logging that happens in main().
2455 (void) zpool_log_history(g_zfs
, history_str
);
2456 log_history
= B_FALSE
;
2458 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
2459 cb
->cb_numupgraded
++;
2462 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
2463 } else if (version
> cb
->cb_version
) {
2464 /* can't downgrade */
2465 (void) printf(gettext("%s: can not be downgraded; "
2466 "it is already at version %u\n"),
2467 zfs_get_name(zhp
), version
);
2470 cb
->cb_numsamegraded
++;
2478 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2481 zfs_do_upgrade(int argc
, char **argv
)
2483 boolean_t all
= B_FALSE
;
2484 boolean_t showversions
= B_FALSE
;
2486 upgrade_cbdata_t cb
= { 0 };
2488 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
2491 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
2494 flags
|= ZFS_ITER_RECURSE
;
2497 showversions
= B_TRUE
;
2500 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
2501 optarg
, &cb
.cb_version
) != 0) {
2502 (void) fprintf(stderr
,
2503 gettext("invalid version %s\n"), optarg
);
2512 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2521 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
2523 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
2524 cb
.cb_version
|| argc
))
2526 if ((all
|| argc
) && (showversions
))
2532 /* Show info on available versions. */
2533 (void) printf(gettext("The following filesystem versions are "
2535 (void) printf(gettext("VER DESCRIPTION\n"));
2536 (void) printf("--- -----------------------------------------"
2537 "---------------\n");
2538 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2539 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2540 (void) printf(gettext(" 3 Case insensitive and filesystem "
2541 "user identifier (FUID)\n"));
2542 (void) printf(gettext(" 4 userquota, groupquota "
2544 (void) printf(gettext(" 5 System attributes\n"));
2545 (void) printf(gettext("\nFor more information on a particular "
2546 "version, including supported releases,\n"));
2547 (void) printf("see the ZFS Administration Guide.\n\n");
2549 } else if (argc
|| all
) {
2550 /* Upgrade filesystems */
2551 if (cb
.cb_version
== 0)
2552 cb
.cb_version
= ZPL_VERSION
;
2553 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
2554 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
2555 (void) printf(gettext("%llu filesystems upgraded\n"),
2556 (u_longlong_t
)cb
.cb_numupgraded
);
2557 if (cb
.cb_numsamegraded
) {
2558 (void) printf(gettext("%llu filesystems already at "
2560 (u_longlong_t
)cb
.cb_numsamegraded
);
2562 if (cb
.cb_numfailed
!= 0)
2565 /* List old-version filesystems */
2567 (void) printf(gettext("This system is currently running "
2568 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
2570 flags
|= ZFS_ITER_RECURSE
;
2571 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2572 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2574 found
= cb
.cb_foundone
;
2575 cb
.cb_foundone
= B_FALSE
;
2576 cb
.cb_newer
= B_TRUE
;
2578 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
2579 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
2581 if (!cb
.cb_foundone
&& !found
) {
2582 (void) printf(gettext("All filesystems are "
2583 "formatted with the current version.\n"));
2591 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2592 * [-S field [-S field]...] [-t type[,...]]
2593 * filesystem | snapshot | path
2594 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2595 * [-S field [-S field]...] [-t type[,...]]
2596 * filesystem | snapshot | path
2597 * zfs projectspace [-Hp] [-o field[,...]] [-s field [-s field]...]
2598 * [-S field [-S field]...] filesystem | snapshot | path
2600 * -H Scripted mode; elide headers and separate columns by tabs.
2601 * -i Translate SID to POSIX ID.
2602 * -n Print numeric ID instead of user/group name.
2603 * -o Control which fields to display.
2604 * -p Use exact (parsable) numeric output.
2605 * -s Specify sort columns, descending order.
2606 * -S Specify sort columns, ascending order.
2607 * -t Control which object types to display.
2609 * Displays space consumed by, and quotas on, each user in the specified
2610 * filesystem or snapshot.
2613 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2614 enum us_field_types
{
2622 static const char *const us_field_hdr
[] = { "TYPE", "NAME", "USED", "QUOTA",
2623 "OBJUSED", "OBJQUOTA" };
2624 static const char *const us_field_names
[] = { "type", "name", "used", "quota",
2625 "objused", "objquota" };
2626 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2628 #define USTYPE_PSX_GRP (1 << 0)
2629 #define USTYPE_PSX_USR (1 << 1)
2630 #define USTYPE_SMB_GRP (1 << 2)
2631 #define USTYPE_SMB_USR (1 << 3)
2632 #define USTYPE_PROJ (1 << 4)
2633 #define USTYPE_ALL \
2634 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR | \
2637 static int us_type_bits
[] = {
2644 static const char *const us_type_names
[] = { "posixgroup", "posixuser",
2645 "smbgroup", "smbuser", "all" };
2647 typedef struct us_node
{
2649 uu_avl_node_t usn_avlnode
;
2650 uu_list_node_t usn_listnode
;
2653 typedef struct us_cbdata
{
2655 uu_avl_pool_t
*cb_avl_pool
;
2657 boolean_t cb_numname
;
2658 boolean_t cb_nicenum
;
2659 boolean_t cb_sid2posix
;
2660 zfs_userquota_prop_t cb_prop
;
2661 zfs_sort_column_t
*cb_sortcol
;
2662 size_t cb_width
[USFIELD_LAST
];
2665 static boolean_t us_populated
= B_FALSE
;
2668 zfs_sort_column_t
*si_sortcol
;
2669 boolean_t si_numname
;
2673 us_field_index(const char *field
)
2675 for (int i
= 0; i
< USFIELD_LAST
; i
++) {
2676 if (strcmp(field
, us_field_names
[i
]) == 0)
2684 us_compare(const void *larg
, const void *rarg
, void *unused
)
2686 const us_node_t
*l
= larg
;
2687 const us_node_t
*r
= rarg
;
2688 us_sort_info_t
*si
= (us_sort_info_t
*)unused
;
2689 zfs_sort_column_t
*sortcol
= si
->si_sortcol
;
2690 boolean_t numname
= si
->si_numname
;
2691 nvlist_t
*lnvl
= l
->usn_nvl
;
2692 nvlist_t
*rnvl
= r
->usn_nvl
;
2696 for (; sortcol
!= NULL
; sortcol
= sortcol
->sc_next
) {
2697 char *lvstr
= (char *)"";
2698 char *rvstr
= (char *)"";
2703 zfs_prop_t prop
= sortcol
->sc_prop
;
2704 const char *propname
= NULL
;
2705 boolean_t reverse
= sortcol
->sc_reverse
;
2710 (void) nvlist_lookup_uint32(lnvl
, propname
, &lv32
);
2711 (void) nvlist_lookup_uint32(rnvl
, propname
, &rv32
);
2713 rc
= (rv32
< lv32
) ? 1 : -1;
2719 (void) nvlist_lookup_uint64(lnvl
, propname
,
2721 (void) nvlist_lookup_uint64(rnvl
, propname
,
2724 rc
= (rv64
< lv64
) ? 1 : -1;
2726 if ((nvlist_lookup_string(lnvl
, propname
,
2727 &lvstr
) == ENOENT
) ||
2728 (nvlist_lookup_string(rnvl
, propname
,
2729 &rvstr
) == ENOENT
)) {
2732 rc
= strcmp(lvstr
, rvstr
);
2736 case ZFS_PROP_QUOTA
:
2739 if (prop
== ZFS_PROP_USED
)
2743 (void) nvlist_lookup_uint64(lnvl
, propname
, &lv64
);
2744 (void) nvlist_lookup_uint64(rnvl
, propname
, &rv64
);
2746 rc
= (rv64
< lv64
) ? 1 : -1;
2755 return (reverse
? 1 : -1);
2757 return (reverse
? -1 : 1);
2762 * If entries still seem to be the same, check if they are of the same
2763 * type (smbentity is added only if we are doing SID to POSIX ID
2764 * translation where we can have duplicate type/name combinations).
2766 if (nvlist_lookup_boolean_value(lnvl
, "smbentity", &lvb
) == 0 &&
2767 nvlist_lookup_boolean_value(rnvl
, "smbentity", &rvb
) == 0 &&
2769 return (lvb
< rvb
? -1 : 1);
2775 zfs_prop_is_user(unsigned p
)
2777 return (p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_USERQUOTA
||
2778 p
== ZFS_PROP_USEROBJUSED
|| p
== ZFS_PROP_USEROBJQUOTA
);
2782 zfs_prop_is_group(unsigned p
)
2784 return (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
||
2785 p
== ZFS_PROP_GROUPOBJUSED
|| p
== ZFS_PROP_GROUPOBJQUOTA
);
2789 zfs_prop_is_project(unsigned p
)
2791 return (p
== ZFS_PROP_PROJECTUSED
|| p
== ZFS_PROP_PROJECTQUOTA
||
2792 p
== ZFS_PROP_PROJECTOBJUSED
|| p
== ZFS_PROP_PROJECTOBJQUOTA
);
2795 static inline const char *
2796 us_type2str(unsigned field_type
)
2798 switch (field_type
) {
2799 case USTYPE_PSX_USR
:
2800 return ("POSIX User");
2801 case USTYPE_PSX_GRP
:
2802 return ("POSIX Group");
2803 case USTYPE_SMB_USR
:
2804 return ("SMB User");
2805 case USTYPE_SMB_GRP
:
2806 return ("SMB Group");
2810 return ("Undefined");
2815 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
2817 us_cbdata_t
*cb
= (us_cbdata_t
*)arg
;
2818 zfs_userquota_prop_t prop
= cb
->cb_prop
;
2820 const char *propname
;
2823 uu_avl_pool_t
*avl_pool
= cb
->cb_avl_pool
;
2824 uu_avl_t
*avl
= cb
->cb_avl
;
2828 zfs_sort_column_t
*sortcol
= cb
->cb_sortcol
;
2830 const char *typestr
;
2834 int typeidx
, nameidx
, sizeidx
;
2835 us_sort_info_t sortinfo
= { sortcol
, cb
->cb_numname
};
2836 boolean_t smbentity
= B_FALSE
;
2838 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2840 node
= safe_malloc(sizeof (us_node_t
));
2841 uu_avl_node_init(node
, &node
->usn_avlnode
, avl_pool
);
2842 node
->usn_nvl
= props
;
2844 if (domain
!= NULL
&& domain
[0] != '\0') {
2847 char sid
[MAXNAMELEN
+ 32];
2851 directory_error_t e
;
2855 (void) snprintf(sid
, sizeof (sid
), "%s-%u", domain
, rid
);
2857 if (prop
== ZFS_PROP_GROUPUSED
|| prop
== ZFS_PROP_GROUPQUOTA
) {
2858 type
= USTYPE_SMB_GRP
;
2859 err
= sid_to_id(sid
, B_FALSE
, &id
);
2861 type
= USTYPE_SMB_USR
;
2862 err
= sid_to_id(sid
, B_TRUE
, &id
);
2867 if (!cb
->cb_sid2posix
) {
2868 e
= directory_name_from_sid(NULL
, sid
, &name
,
2871 directory_error_free(e
);
2881 #endif /* HAVE_IDMAP */
2884 if (cb
->cb_sid2posix
|| domain
== NULL
|| domain
[0] == '\0') {
2886 if (zfs_prop_is_group(prop
)) {
2887 type
= USTYPE_PSX_GRP
;
2888 if (!cb
->cb_numname
) {
2891 if ((g
= getgrgid(rid
)) != NULL
)
2894 } else if (zfs_prop_is_user(prop
)) {
2895 type
= USTYPE_PSX_USR
;
2896 if (!cb
->cb_numname
) {
2899 if ((p
= getpwuid(rid
)) != NULL
)
2908 * Make sure that the type/name combination is unique when doing
2909 * SID to POSIX ID translation (hence changing the type from SMB to
2912 if (cb
->cb_sid2posix
&&
2913 nvlist_add_boolean_value(props
, "smbentity", smbentity
) != 0)
2916 /* Calculate/update width of TYPE field */
2917 typestr
= us_type2str(type
);
2918 typelen
= strlen(gettext(typestr
));
2919 typeidx
= us_field_index("type");
2920 if (typelen
> cb
->cb_width
[typeidx
])
2921 cb
->cb_width
[typeidx
] = typelen
;
2922 if (nvlist_add_uint32(props
, "type", type
) != 0)
2925 /* Calculate/update width of NAME field */
2926 if ((cb
->cb_numname
&& cb
->cb_sid2posix
) || name
== NULL
) {
2927 if (nvlist_add_uint64(props
, "name", rid
) != 0)
2929 namelen
= snprintf(NULL
, 0, "%u", rid
);
2931 if (nvlist_add_string(props
, "name", name
) != 0)
2933 namelen
= strlen(name
);
2935 nameidx
= us_field_index("name");
2936 if (nameidx
>= 0 && namelen
> cb
->cb_width
[nameidx
])
2937 cb
->cb_width
[nameidx
] = namelen
;
2940 * Check if this type/name combination is in the list and update it;
2941 * otherwise add new node to the list.
2943 if ((n
= uu_avl_find(avl
, node
, &sortinfo
, &idx
)) == NULL
) {
2944 uu_avl_insert(avl
, node
, idx
);
2949 props
= node
->usn_nvl
;
2952 /* Calculate/update width of USED/QUOTA fields */
2953 if (cb
->cb_nicenum
) {
2954 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
||
2955 prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
||
2956 prop
== ZFS_PROP_PROJECTUSED
||
2957 prop
== ZFS_PROP_PROJECTQUOTA
) {
2958 zfs_nicebytes(space
, sizebuf
, sizeof (sizebuf
));
2960 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
2963 (void) snprintf(sizebuf
, sizeof (sizebuf
), "%llu",
2964 (u_longlong_t
)space
);
2966 sizelen
= strlen(sizebuf
);
2967 if (prop
== ZFS_PROP_USERUSED
|| prop
== ZFS_PROP_GROUPUSED
||
2968 prop
== ZFS_PROP_PROJECTUSED
) {
2970 if (!nvlist_exists(props
, "quota"))
2971 (void) nvlist_add_uint64(props
, "quota", 0);
2972 } else if (prop
== ZFS_PROP_USERQUOTA
|| prop
== ZFS_PROP_GROUPQUOTA
||
2973 prop
== ZFS_PROP_PROJECTQUOTA
) {
2975 if (!nvlist_exists(props
, "used"))
2976 (void) nvlist_add_uint64(props
, "used", 0);
2977 } else if (prop
== ZFS_PROP_USEROBJUSED
||
2978 prop
== ZFS_PROP_GROUPOBJUSED
|| prop
== ZFS_PROP_PROJECTOBJUSED
) {
2979 propname
= "objused";
2980 if (!nvlist_exists(props
, "objquota"))
2981 (void) nvlist_add_uint64(props
, "objquota", 0);
2982 } else if (prop
== ZFS_PROP_USEROBJQUOTA
||
2983 prop
== ZFS_PROP_GROUPOBJQUOTA
||
2984 prop
== ZFS_PROP_PROJECTOBJQUOTA
) {
2985 propname
= "objquota";
2986 if (!nvlist_exists(props
, "objused"))
2987 (void) nvlist_add_uint64(props
, "objused", 0);
2991 sizeidx
= us_field_index(propname
);
2992 if (sizeidx
>= 0 && sizelen
> cb
->cb_width
[sizeidx
])
2993 cb
->cb_width
[sizeidx
] = sizelen
;
2995 if (nvlist_add_uint64(props
, propname
, space
) != 0)
3002 print_us_node(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
3003 size_t *width
, us_node_t
*node
)
3005 nvlist_t
*nvl
= node
->usn_nvl
;
3006 char valstr
[MAXNAMELEN
];
3007 boolean_t first
= B_TRUE
;
3013 (void) nvlist_lookup_uint32(nvl
, "type", &ustype
);
3014 if (!(ustype
& types
))
3017 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
3018 nvpair_t
*nvp
= NULL
;
3020 uint32_t val32
= -1;
3021 uint64_t val64
= -1;
3022 const char *strval
= "-";
3024 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
)
3025 if (strcmp(nvpair_name(nvp
),
3026 us_field_names
[field
]) == 0)
3029 type
= nvp
== NULL
? DATA_TYPE_UNKNOWN
: nvpair_type(nvp
);
3031 case DATA_TYPE_UINT32
:
3032 val32
= fnvpair_value_uint32(nvp
);
3034 case DATA_TYPE_UINT64
:
3035 val64
= fnvpair_value_uint64(nvp
);
3037 case DATA_TYPE_STRING
:
3038 strval
= fnvpair_value_string(nvp
);
3040 case DATA_TYPE_UNKNOWN
:
3043 (void) fprintf(stderr
, "invalid data type\n");
3048 if (type
== DATA_TYPE_UINT32
)
3049 strval
= us_type2str(val32
);
3052 if (type
== DATA_TYPE_UINT64
) {
3053 (void) sprintf(valstr
, "%llu",
3054 (u_longlong_t
)val64
);
3060 if (type
== DATA_TYPE_UINT64
) {
3062 (void) sprintf(valstr
, "%llu",
3063 (u_longlong_t
)val64
);
3065 } else if (field
== USFIELD_QUOTA
&&
3069 zfs_nicebytes(val64
, valstr
,
3075 case USFIELD_OBJUSED
:
3076 case USFIELD_OBJQUOTA
:
3077 if (type
== DATA_TYPE_UINT64
) {
3079 (void) sprintf(valstr
, "%llu",
3080 (u_longlong_t
)val64
);
3082 } else if (field
== USFIELD_OBJQUOTA
&&
3086 zfs_nicenum(val64
, valstr
,
3096 (void) putchar('\t');
3098 (void) fputs(" ", stdout
);
3101 (void) fputs(strval
, stdout
);
3102 else if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
)
3103 (void) printf("%-*s", (int)width
[field
], strval
);
3105 (void) printf("%*s", (int)width
[field
], strval
);
3111 (void) putchar('\n');
3115 print_us(boolean_t scripted
, boolean_t parsable
, int *fields
, int types
,
3116 size_t *width
, boolean_t rmnode
, uu_avl_t
*avl
)
3124 boolean_t first
= B_TRUE
;
3126 while ((field
= fields
[cfield
]) != USFIELD_LAST
) {
3127 col
= gettext(us_field_hdr
[field
]);
3128 if (field
== USFIELD_TYPE
|| field
== USFIELD_NAME
) {
3129 (void) printf(first
? "%-*s" : " %-*s",
3130 (int)width
[field
], col
);
3132 (void) printf(first
? "%*s" : " %*s",
3133 (int)width
[field
], col
);
3138 (void) printf("\n");
3141 for (node
= uu_avl_first(avl
); node
; node
= uu_avl_next(avl
, node
)) {
3142 print_us_node(scripted
, parsable
, fields
, types
, width
, node
);
3144 nvlist_free(node
->usn_nvl
);
3149 zfs_do_userspace(int argc
, char **argv
)
3152 zfs_userquota_prop_t p
;
3153 uu_avl_pool_t
*avl_pool
;
3155 uu_avl_walk_t
*walk
;
3157 char deffields
[] = "type,name,used,quota,objused,objquota";
3158 char *ofield
= NULL
;
3159 char *tfield
= NULL
;
3163 boolean_t scripted
= B_FALSE
;
3164 boolean_t prtnum
= B_FALSE
;
3165 boolean_t parsable
= B_FALSE
;
3166 boolean_t sid2posix
= B_FALSE
;
3169 zfs_sort_column_t
*sortcol
= NULL
;
3170 int types
= USTYPE_PSX_USR
| USTYPE_SMB_USR
;
3174 uu_list_pool_t
*listpool
;
3176 uu_avl_index_t idx
= 0;
3177 uu_list_index_t idx2
= 0;
3182 if (strcmp(argv
[0], "groupspace") == 0) {
3183 /* Toggle default group types */
3184 types
= USTYPE_PSX_GRP
| USTYPE_SMB_GRP
;
3185 } else if (strcmp(argv
[0], "projectspace") == 0) {
3186 types
= USTYPE_PROJ
;
3190 while ((c
= getopt(argc
, argv
, "nHpo:s:S:t:i")) != -1) {
3193 if (types
== USTYPE_PROJ
) {
3194 (void) fprintf(stderr
,
3195 gettext("invalid option 'n'\n"));
3211 if (zfs_add_sort_column(&sortcol
, optarg
,
3212 c
== 's' ? B_FALSE
: B_TRUE
) != 0) {
3213 (void) fprintf(stderr
,
3214 gettext("invalid field '%s'\n"), optarg
);
3219 if (types
== USTYPE_PROJ
) {
3220 (void) fprintf(stderr
,
3221 gettext("invalid option 't'\n"));
3227 if (types
== USTYPE_PROJ
) {
3228 (void) fprintf(stderr
,
3229 gettext("invalid option 'i'\n"));
3235 (void) fprintf(stderr
, gettext("missing argument for "
3236 "'%c' option\n"), optopt
);
3240 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3250 (void) fprintf(stderr
, gettext("missing dataset name\n"));
3254 (void) fprintf(stderr
, gettext("too many arguments\n"));
3258 /* Use default output fields if not specified using -o */
3262 if ((delim
= strchr(ofield
, ',')) != NULL
)
3264 if ((fields
[cfield
++] = us_field_index(ofield
)) == -1) {
3265 (void) fprintf(stderr
, gettext("invalid type '%s' "
3266 "for -o option\n"), ofield
);
3271 } while (delim
!= NULL
);
3272 fields
[cfield
] = USFIELD_LAST
;
3274 /* Override output types (-t option) */
3275 if (tfield
!= NULL
) {
3279 boolean_t found
= B_FALSE
;
3281 if ((delim
= strchr(tfield
, ',')) != NULL
)
3283 for (i
= 0; i
< sizeof (us_type_bits
) / sizeof (int);
3285 if (strcmp(tfield
, us_type_names
[i
]) == 0) {
3287 types
|= us_type_bits
[i
];
3292 (void) fprintf(stderr
, gettext("invalid type "
3293 "'%s' for -t option\n"), tfield
);
3298 } while (delim
!= NULL
);
3301 if ((zhp
= zfs_path_to_zhandle(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
|
3302 ZFS_TYPE_SNAPSHOT
)) == NULL
)
3304 if (zfs_get_underlying_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3305 (void) fprintf(stderr
, gettext("operation is only applicable "
3306 "to filesystems and their snapshots\n"));
3311 if ((avl_pool
= uu_avl_pool_create("us_avl_pool", sizeof (us_node_t
),
3312 offsetof(us_node_t
, usn_avlnode
), us_compare
, UU_DEFAULT
)) == NULL
)
3314 if ((avl_tree
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
3317 /* Always add default sorting columns */
3318 (void) zfs_add_sort_column(&sortcol
, "type", B_FALSE
);
3319 (void) zfs_add_sort_column(&sortcol
, "name", B_FALSE
);
3321 cb
.cb_sortcol
= sortcol
;
3322 cb
.cb_numname
= prtnum
;
3323 cb
.cb_nicenum
= !parsable
;
3324 cb
.cb_avl_pool
= avl_pool
;
3325 cb
.cb_avl
= avl_tree
;
3326 cb
.cb_sid2posix
= sid2posix
;
3328 for (i
= 0; i
< USFIELD_LAST
; i
++)
3329 cb
.cb_width
[i
] = strlen(gettext(us_field_hdr
[i
]));
3331 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
3332 if ((zfs_prop_is_user(p
) &&
3333 !(types
& (USTYPE_PSX_USR
| USTYPE_SMB_USR
))) ||
3334 (zfs_prop_is_group(p
) &&
3335 !(types
& (USTYPE_PSX_GRP
| USTYPE_SMB_GRP
))) ||
3336 (zfs_prop_is_project(p
) && types
!= USTYPE_PROJ
))
3340 if ((ret
= zfs_userspace(zhp
, p
, userspace_cb
, &cb
)) != 0) {
3348 if ((node
= uu_avl_first(avl_tree
)) == NULL
)
3351 us_populated
= B_TRUE
;
3353 listpool
= uu_list_pool_create("tmplist", sizeof (us_node_t
),
3354 offsetof(us_node_t
, usn_listnode
), NULL
, UU_DEFAULT
);
3355 list
= uu_list_create(listpool
, NULL
, UU_DEFAULT
);
3356 uu_list_node_init(node
, &node
->usn_listnode
, listpool
);
3358 while (node
!= NULL
) {
3360 node
= uu_avl_next(avl_tree
, node
);
3361 uu_avl_remove(avl_tree
, rmnode
);
3362 if (uu_list_find(list
, rmnode
, NULL
, &idx2
) == NULL
)
3363 uu_list_insert(list
, rmnode
, idx2
);
3366 for (node
= uu_list_first(list
); node
!= NULL
;
3367 node
= uu_list_next(list
, node
)) {
3368 us_sort_info_t sortinfo
= { sortcol
, cb
.cb_numname
};
3370 if (uu_avl_find(avl_tree
, node
, &sortinfo
, &idx
) == NULL
)
3371 uu_avl_insert(avl_tree
, node
, idx
);
3374 uu_list_destroy(list
);
3375 uu_list_pool_destroy(listpool
);
3377 /* Print and free node nvlist memory */
3378 print_us(scripted
, parsable
, fields
, types
, cb
.cb_width
, B_TRUE
,
3381 zfs_free_sort_columns(sortcol
);
3383 /* Clean up the AVL tree */
3384 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
3387 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
3388 uu_avl_remove(cb
.cb_avl
, node
);
3392 uu_avl_walk_end(walk
);
3393 uu_avl_destroy(avl_tree
);
3394 uu_avl_pool_destroy(avl_pool
);
3400 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
3401 * [-t type[,...]] [filesystem|volume|snapshot] ...
3403 * -H Scripted mode; elide headers and separate columns by tabs
3404 * -p Display values in parsable (literal) format.
3405 * -r Recurse over all children
3406 * -d Limit recursion by depth.
3407 * -o Control which fields to display.
3408 * -s Specify sort columns, descending order.
3409 * -S Specify sort columns, ascending order.
3410 * -t Control which object types to display.
3412 * When given no arguments, list all filesystems in the system.
3413 * Otherwise, list the specified datasets, optionally recursing down them if
3414 * '-r' is specified.
3416 typedef struct list_cbdata
{
3418 boolean_t cb_literal
;
3419 boolean_t cb_scripted
;
3420 zprop_list_t
*cb_proplist
;
3424 * Given a list of columns to display, output appropriate headers for each one.
3427 print_header(list_cbdata_t
*cb
)
3429 zprop_list_t
*pl
= cb
->cb_proplist
;
3430 char headerbuf
[ZFS_MAXPROPLEN
];
3433 boolean_t first
= B_TRUE
;
3434 boolean_t right_justify
;
3436 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3443 right_justify
= B_FALSE
;
3444 if (pl
->pl_prop
!= ZPROP_USERPROP
) {
3445 header
= zfs_prop_column_name(pl
->pl_prop
);
3446 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3448 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
3449 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
3450 headerbuf
[i
] = '\0';
3454 if (pl
->pl_next
== NULL
&& !right_justify
)
3455 (void) printf("%s", header
);
3456 else if (right_justify
)
3457 (void) printf("%*s", (int)pl
->pl_width
, header
);
3459 (void) printf("%-*s", (int)pl
->pl_width
, header
);
3462 (void) printf("\n");
3466 * Given a dataset and a list of fields, print out all the properties according
3467 * to the described layout.
3470 print_dataset(zfs_handle_t
*zhp
, list_cbdata_t
*cb
)
3472 zprop_list_t
*pl
= cb
->cb_proplist
;
3473 boolean_t first
= B_TRUE
;
3474 char property
[ZFS_MAXPROPLEN
];
3475 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
3477 const char *propstr
;
3478 boolean_t right_justify
;
3480 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3482 if (cb
->cb_scripted
)
3483 (void) putchar('\t');
3485 (void) fputs(" ", stdout
);
3490 if (pl
->pl_prop
== ZFS_PROP_NAME
) {
3491 (void) strlcpy(property
, zfs_get_name(zhp
),
3494 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3495 } else if (pl
->pl_prop
!= ZPROP_USERPROP
) {
3496 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
3497 sizeof (property
), NULL
, NULL
, 0,
3498 cb
->cb_literal
) != 0)
3502 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
3503 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
3504 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
3505 property
, sizeof (property
), cb
->cb_literal
) != 0)
3509 right_justify
= B_TRUE
;
3510 } else if (zfs_prop_written(pl
->pl_user_prop
)) {
3511 if (zfs_prop_get_written(zhp
, pl
->pl_user_prop
,
3512 property
, sizeof (property
), cb
->cb_literal
) != 0)
3516 right_justify
= B_TRUE
;
3518 if (nvlist_lookup_nvlist(userprops
,
3519 pl
->pl_user_prop
, &propval
) != 0)
3522 propstr
= fnvlist_lookup_string(propval
,
3524 right_justify
= B_FALSE
;
3528 * If this is being called in scripted mode, or if this is the
3529 * last column and it is left-justified, don't include a width
3532 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3533 (void) fputs(propstr
, stdout
);
3534 else if (right_justify
)
3535 (void) printf("%*s", (int)pl
->pl_width
, propstr
);
3537 (void) printf("%-*s", (int)pl
->pl_width
, propstr
);
3540 (void) putchar('\n');
3544 * Generic callback function to list a dataset or snapshot.
3547 list_callback(zfs_handle_t
*zhp
, void *data
)
3549 list_cbdata_t
*cbp
= data
;
3551 if (cbp
->cb_first
) {
3552 if (!cbp
->cb_scripted
)
3554 cbp
->cb_first
= B_FALSE
;
3557 print_dataset(zhp
, cbp
);
3563 zfs_do_list(int argc
, char **argv
)
3566 char default_fields
[] =
3567 "name,used,available,referenced,mountpoint";
3568 int types
= ZFS_TYPE_DATASET
;
3569 boolean_t types_specified
= B_FALSE
;
3570 char *fields
= default_fields
;
3571 list_cbdata_t cb
= { 0 };
3574 zfs_sort_column_t
*sortcol
= NULL
;
3575 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
3578 while ((c
= getopt(argc
, argv
, "HS:d:o:prs:t:")) != -1) {
3584 cb
.cb_literal
= B_TRUE
;
3585 flags
|= ZFS_ITER_LITERAL_PROPS
;
3588 limit
= parse_depth(optarg
, &flags
);
3591 flags
|= ZFS_ITER_RECURSE
;
3594 cb
.cb_scripted
= B_TRUE
;
3597 if (zfs_add_sort_column(&sortcol
, optarg
,
3599 (void) fprintf(stderr
,
3600 gettext("invalid property '%s'\n"), optarg
);
3605 if (zfs_add_sort_column(&sortcol
, optarg
,
3607 (void) fprintf(stderr
,
3608 gettext("invalid property '%s'\n"), optarg
);
3614 types_specified
= B_TRUE
;
3615 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
3617 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
3618 static const char *const type_subopts
[] = {
3619 "filesystem", "volume",
3623 static const int type_types
[] = {
3624 ZFS_TYPE_FILESYSTEM
, ZFS_TYPE_VOLUME
,
3625 ZFS_TYPE_SNAPSHOT
, ZFS_TYPE_SNAPSHOT
,
3627 ZFS_TYPE_DATASET
| ZFS_TYPE_BOOKMARK
};
3629 for (c
= 0; c
< ARRAY_SIZE(type_subopts
); ++c
)
3630 if (strcmp(tok
, type_subopts
[c
]) == 0) {
3631 types
|= type_types
[c
];
3635 (void) fprintf(stderr
,
3636 gettext("invalid type '%s'\n"), tok
);
3642 (void) fprintf(stderr
, gettext("missing argument for "
3643 "'%c' option\n"), optopt
);
3647 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3657 * If we are only going to list snapshot names and sort by name or
3658 * by createtxg, then we can use faster version.
3660 if (strcmp(fields
, "name") == 0 &&
3661 (zfs_sort_only_by_name(sortcol
) ||
3662 zfs_sort_only_by_createtxg(sortcol
))) {
3663 flags
|= ZFS_ITER_SIMPLE
;
3667 * If "-o space" and no types were specified, don't display snapshots.
3669 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
3670 types
&= ~ZFS_TYPE_SNAPSHOT
;
3673 * Handle users who want to list all snapshots or bookmarks
3674 * of the current dataset (ex. 'zfs list -t snapshot <dataset>').
3676 if ((types
== ZFS_TYPE_SNAPSHOT
|| types
== ZFS_TYPE_BOOKMARK
) &&
3677 argc
> 0 && (flags
& ZFS_ITER_RECURSE
) == 0 && limit
== 0) {
3678 flags
|= (ZFS_ITER_DEPTH_LIMIT
| ZFS_ITER_RECURSE
);
3683 * If the user specifies '-o all', the zprop_get_list() doesn't
3684 * normally include the name of the dataset. For 'zfs list', we always
3685 * want this property to be first.
3687 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
3691 cb
.cb_first
= B_TRUE
;
3693 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
3694 limit
, list_callback
, &cb
);
3696 zprop_free_list(cb
.cb_proplist
);
3697 zfs_free_sort_columns(sortcol
);
3699 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
3700 (void) fprintf(stderr
, gettext("no datasets available\n"));
3706 * zfs rename [-fu] <fs | snap | vol> <fs | snap | vol>
3707 * zfs rename [-f] -p <fs | vol> <fs | vol>
3708 * zfs rename [-u] -r <snap> <snap>
3710 * Renames the given dataset to another of the same type.
3712 * The '-p' flag creates all the non-existing ancestors of the target first.
3713 * The '-u' flag prevents file systems from being remounted during rename.
3716 zfs_do_rename(int argc
, char **argv
)
3719 renameflags_t flags
= { 0 };
3723 boolean_t parents
= B_FALSE
;
3726 while ((c
= getopt(argc
, argv
, "pruf")) != -1) {
3732 flags
.recursive
= B_TRUE
;
3735 flags
.nounmount
= B_TRUE
;
3738 flags
.forceunmount
= B_TRUE
;
3742 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3751 /* check number of arguments */
3753 (void) fprintf(stderr
, gettext("missing source dataset "
3758 (void) fprintf(stderr
, gettext("missing target dataset "
3763 (void) fprintf(stderr
, gettext("too many arguments\n"));
3767 if (flags
.recursive
&& parents
) {
3768 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
3773 if (flags
.nounmount
&& parents
) {
3774 (void) fprintf(stderr
, gettext("-u and -p options are mutually "
3779 if (flags
.recursive
&& strchr(argv
[0], '@') == 0) {
3780 (void) fprintf(stderr
, gettext("source dataset for recursive "
3781 "rename must be a snapshot\n"));
3785 if (flags
.nounmount
)
3786 types
= ZFS_TYPE_FILESYSTEM
;
3788 types
= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
3790 types
= ZFS_TYPE_DATASET
;
3792 if ((zhp
= zfs_open(g_zfs
, argv
[0], types
)) == NULL
)
3795 /* If we were asked and the name looks good, try to create ancestors. */
3796 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
3797 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
3802 ret
= (zfs_rename(zhp
, argv
[1], flags
) != 0);
3811 * Promotes the given clone fs to be the parent
3814 zfs_do_promote(int argc
, char **argv
)
3820 if (argc
> 1 && argv
[1][0] == '-') {
3821 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3826 /* check number of arguments */
3828 (void) fprintf(stderr
, gettext("missing clone filesystem"
3833 (void) fprintf(stderr
, gettext("too many arguments\n"));
3837 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3841 ret
= (zfs_promote(zhp
) != 0);
3849 zfs_do_redact(int argc
, char **argv
)
3852 char *bookname
= NULL
;
3853 char **rsnaps
= NULL
;
3858 (void) fprintf(stderr
, gettext("too few arguments\n"));
3865 numrsnaps
= argc
- 2;
3867 nvlist_t
*rsnapnv
= fnvlist_alloc();
3869 for (int i
= 0; i
< numrsnaps
; i
++) {
3870 fnvlist_add_boolean(rsnapnv
, rsnaps
[i
]);
3873 int err
= lzc_redact(snap
, bookname
, rsnapnv
);
3874 fnvlist_free(rsnapnv
);
3880 (void) fprintf(stderr
,
3881 gettext("provided snapshot %s does not exist\n"), snap
);
3884 (void) fprintf(stderr
, gettext("specified redaction bookmark "
3885 "(%s) provided already exists\n"), bookname
);
3888 (void) fprintf(stderr
, gettext("provided bookmark name cannot "
3889 "be used, final name would be too long\n"));
3892 (void) fprintf(stderr
, gettext("too many redaction snapshots "
3896 if (strchr(bookname
, '#') != NULL
)
3897 (void) fprintf(stderr
, gettext(
3898 "redaction bookmark name must not contain '#'\n"));
3900 (void) fprintf(stderr
, gettext(
3901 "redaction snapshot must be descendent of "
3902 "snapshot being redacted\n"));
3905 (void) fprintf(stderr
, gettext("attempted to redact redacted "
3906 "dataset or with respect to redacted dataset\n"));
3909 (void) fprintf(stderr
, gettext("redaction bookmarks feature "
3913 (void) fprintf(stderr
, gettext("potentially invalid redaction "
3914 "snapshot; full dataset names required\n"));
3917 (void) fprintf(stderr
, gettext("internal error: %s\n"),
3925 * zfs rollback [-rRf] <snapshot>
3927 * -r Delete any intervening snapshots before doing rollback
3928 * -R Delete any snapshots and their clones
3929 * -f ignored for backwards compatibility
3931 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3932 * since then and making it the active dataset. If more recent snapshots exist,
3933 * the command will complain unless the '-r' flag is given.
3935 typedef struct rollback_cbdata
{
3937 uint8_t cb_younger_ds_printed
;
3942 boolean_t cb_recurse
;
3943 } rollback_cbdata_t
;
3946 rollback_check_dependent(zfs_handle_t
*zhp
, void *data
)
3948 rollback_cbdata_t
*cbp
= data
;
3950 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
3951 (void) fprintf(stderr
, gettext("cannot rollback to "
3952 "'%s': clones of previous snapshots exist\n"),
3954 (void) fprintf(stderr
, gettext("use '-R' to "
3955 "force deletion of the following clones and "
3961 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
3969 * Report some snapshots/bookmarks more recent than the one specified.
3970 * Used when '-r' is not specified. We reuse this same callback for the
3971 * snapshot dependents - if 'cb_dependent' is set, then this is a
3972 * dependent and we should report it without checking the transaction group.
3975 rollback_check(zfs_handle_t
*zhp
, void *data
)
3977 rollback_cbdata_t
*cbp
= data
;
3979 * Max number of younger snapshots and/or bookmarks to display before
3980 * we stop the iteration.
3982 const uint8_t max_younger
= 32;
3984 if (cbp
->cb_doclones
) {
3989 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3990 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
3991 (void) fprintf(stderr
, gettext("cannot "
3992 "rollback to '%s': more recent snapshots "
3993 "or bookmarks exist\n"),
3995 (void) fprintf(stderr
, gettext("use '-r' to "
3996 "force deletion of the following "
3997 "snapshots and bookmarks:\n"));
4002 if (cbp
->cb_recurse
) {
4003 if (zfs_iter_dependents(zhp
, B_TRUE
,
4004 rollback_check_dependent
, cbp
) != 0) {
4009 (void) fprintf(stderr
, "%s\n",
4011 cbp
->cb_younger_ds_printed
++;
4016 if (cbp
->cb_younger_ds_printed
== max_younger
) {
4018 * This non-recursive rollback is going to fail due to the
4019 * presence of snapshots and/or bookmarks that are younger than
4020 * the rollback target.
4021 * We printed some of the offending objects, now we stop
4022 * zfs_iter_snapshot/bookmark iteration so we can fail fast and
4023 * avoid iterating over the rest of the younger objects
4025 (void) fprintf(stderr
, gettext("Output limited to %d "
4026 "snapshots/bookmarks\n"), max_younger
);
4033 zfs_do_rollback(int argc
, char **argv
)
4037 boolean_t force
= B_FALSE
;
4038 rollback_cbdata_t cb
= { 0 };
4039 zfs_handle_t
*zhp
, *snap
;
4040 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
4042 uint64_t min_txg
= 0;
4045 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
4058 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4067 /* check number of arguments */
4069 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
4073 (void) fprintf(stderr
, gettext("too many arguments\n"));
4077 /* open the snapshot */
4078 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
4081 /* open the parent dataset */
4082 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
4083 verify((delim
= strrchr(parentname
, '@')) != NULL
);
4085 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
4091 * Check for more recent snapshots and/or clones based on the presence
4094 cb
.cb_target
= argv
[0];
4095 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
4096 cb
.cb_first
= B_TRUE
;
4099 if (cb
.cb_create
> 0)
4100 min_txg
= cb
.cb_create
;
4102 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, rollback_check
, &cb
,
4105 if ((ret
= zfs_iter_bookmarks(zhp
, rollback_check
, &cb
)) != 0)
4108 if ((ret
= cb
.cb_error
) != 0)
4112 * Rollback parent to the given snapshot.
4114 ret
= zfs_rollback(zhp
, snap
, force
);
4127 * zfs set property=value ... { fs | snap | vol } ...
4129 * Sets the given properties for all datasets specified on the command line.
4133 set_callback(zfs_handle_t
*zhp
, void *data
)
4135 nvlist_t
*props
= data
;
4137 if (zfs_prop_set_list(zhp
, props
) != 0) {
4138 switch (libzfs_errno(g_zfs
)) {
4139 case EZFS_MOUNTFAILED
:
4140 (void) fprintf(stderr
, gettext("property may be set "
4141 "but unable to remount filesystem\n"));
4143 case EZFS_SHARENFSFAILED
:
4144 (void) fprintf(stderr
, gettext("property may be set "
4145 "but unable to reshare filesystem\n"));
4154 zfs_do_set(int argc
, char **argv
)
4156 nvlist_t
*props
= NULL
;
4157 int ds_start
= -1; /* argv idx of first dataset arg */
4161 /* check for options */
4162 if (argc
> 1 && argv
[1][0] == '-') {
4163 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4168 /* check number of arguments */
4170 (void) fprintf(stderr
, gettext("missing arguments\n"));
4174 if (strchr(argv
[1], '=') == NULL
) {
4175 (void) fprintf(stderr
, gettext("missing property=value "
4178 (void) fprintf(stderr
, gettext("missing dataset "
4184 /* validate argument order: prop=val args followed by dataset args */
4185 for (i
= 1; i
< argc
; i
++) {
4186 if (strchr(argv
[i
], '=') != NULL
) {
4188 /* out-of-order prop=val argument */
4189 (void) fprintf(stderr
, gettext("invalid "
4190 "argument order\n"));
4193 } else if (ds_start
< 0) {
4198 (void) fprintf(stderr
, gettext("missing dataset name(s)\n"));
4202 /* Populate a list of property settings */
4203 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
4205 for (i
= 1; i
< ds_start
; i
++) {
4206 if (!parseprop(props
, argv
[i
])) {
4212 ret
= zfs_for_each(argc
- ds_start
, argv
+ ds_start
, 0,
4213 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, props
);
4220 typedef struct snap_cbdata
{
4222 boolean_t sd_recursive
;
4223 const char *sd_snapname
;
4227 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
4229 snap_cbdata_t
*sd
= arg
;
4234 if (sd
->sd_recursive
&&
4235 zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) != 0) {
4240 error
= asprintf(&name
, "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
4243 fnvlist_add_boolean(sd
->sd_nvl
, name
);
4246 if (sd
->sd_recursive
)
4247 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
4253 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
4255 * Creates a snapshot with the given name. While functionally equivalent to
4256 * 'zfs create', it is a separate command to differentiate intent.
4259 zfs_do_snapshot(int argc
, char **argv
)
4264 snap_cbdata_t sd
= { 0 };
4265 boolean_t multiple_snaps
= B_FALSE
;
4267 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
4269 if (nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) != 0)
4273 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
4276 if (!parseprop(props
, optarg
)) {
4277 nvlist_free(sd
.sd_nvl
);
4283 sd
.sd_recursive
= B_TRUE
;
4284 multiple_snaps
= B_TRUE
;
4287 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4296 /* check number of arguments */
4298 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4303 multiple_snaps
= B_TRUE
;
4304 for (; argc
> 0; argc
--, argv
++) {
4308 atp
= strchr(argv
[0], '@');
4312 sd
.sd_snapname
= atp
+ 1;
4313 zhp
= zfs_open(g_zfs
, argv
[0],
4314 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4317 if (zfs_snapshot_cb(zhp
, &sd
) != 0)
4321 ret
= zfs_snapshot_nvl(g_zfs
, sd
.sd_nvl
, props
);
4322 nvlist_free(sd
.sd_nvl
);
4324 if (ret
!= 0 && multiple_snaps
)
4325 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
4329 nvlist_free(sd
.sd_nvl
);
4336 * Array of prefixes to exclude –
4337 * a linear search, even if executed for each dataset,
4338 * is plenty good enough.
4340 typedef struct zfs_send_exclude_arg
{
4343 } zfs_send_exclude_arg_t
;
4346 zfs_do_send_exclude(zfs_handle_t
*zhp
, void *context
)
4348 zfs_send_exclude_arg_t
*excludes
= context
;
4349 const char *name
= zfs_get_name(zhp
);
4351 for (size_t i
= 0; i
< excludes
->count
; ++i
) {
4352 size_t len
= strlen(excludes
->list
[i
]);
4353 if (strncmp(name
, excludes
->list
[i
], len
) == 0 &&
4354 memchr("/@", name
[len
], sizeof ("/@")))
4362 * Send a backup stream to stdout.
4365 zfs_do_send(int argc
, char **argv
)
4367 char *fromname
= NULL
;
4368 char *toname
= NULL
;
4369 char *resume_token
= NULL
;
4372 sendflags_t flags
= { 0 };
4374 nvlist_t
*dbgnv
= NULL
;
4375 char *redactbook
= NULL
;
4376 zfs_send_exclude_arg_t excludes
= { 0 };
4378 struct option long_options
[] = {
4379 {"replicate", no_argument
, NULL
, 'R'},
4380 {"skip-missing", no_argument
, NULL
, 's'},
4381 {"redact", required_argument
, NULL
, 'd'},
4382 {"props", no_argument
, NULL
, 'p'},
4383 {"parsable", no_argument
, NULL
, 'P'},
4384 {"dedup", no_argument
, NULL
, 'D'},
4385 {"verbose", no_argument
, NULL
, 'v'},
4386 {"dryrun", no_argument
, NULL
, 'n'},
4387 {"large-block", no_argument
, NULL
, 'L'},
4388 {"embed", no_argument
, NULL
, 'e'},
4389 {"resume", required_argument
, NULL
, 't'},
4390 {"compressed", no_argument
, NULL
, 'c'},
4391 {"raw", no_argument
, NULL
, 'w'},
4392 {"backup", no_argument
, NULL
, 'b'},
4393 {"holds", no_argument
, NULL
, 'h'},
4394 {"saved", no_argument
, NULL
, 'S'},
4395 {"exclude", required_argument
, NULL
, 'X'},
4400 while ((c
= getopt_long(argc
, argv
, ":i:I:RsDpvnPLeht:cwbd:SX:",
4401 long_options
, NULL
)) != -1) {
4404 for (char *ds
; (ds
= strsep(&optarg
, ",")) != NULL
; ) {
4405 if (!zfs_name_valid(ds
, ZFS_TYPE_DATASET
) ||
4406 strchr(ds
, '/') == NULL
) {
4407 (void) fprintf(stderr
, gettext("-X %s: "
4408 "not a valid non-root dataset name"
4412 excludes
.list
= safe_realloc(excludes
.list
,
4413 sizeof (char *) * (excludes
.count
+ 1));
4414 excludes
.list
[excludes
.count
++] = ds
;
4426 flags
.doall
= B_TRUE
;
4429 flags
.replicate
= B_TRUE
;
4432 flags
.skipmissing
= B_TRUE
;
4435 redactbook
= optarg
;
4438 flags
.props
= B_TRUE
;
4441 flags
.backup
= B_TRUE
;
4444 flags
.holds
= B_TRUE
;
4447 flags
.parsable
= B_TRUE
;
4451 flags
.progress
= B_TRUE
;
4454 (void) fprintf(stderr
,
4455 gettext("WARNING: deduplicated send is no "
4456 "longer supported. A regular,\n"
4457 "non-deduplicated stream will be generated.\n\n"));
4460 flags
.dryrun
= B_TRUE
;
4463 flags
.largeblock
= B_TRUE
;
4466 flags
.embed_data
= B_TRUE
;
4469 resume_token
= optarg
;
4472 flags
.compress
= B_TRUE
;
4476 flags
.compress
= B_TRUE
;
4477 flags
.embed_data
= B_TRUE
;
4478 flags
.largeblock
= B_TRUE
;
4481 flags
.saved
= B_TRUE
;
4485 * If a parameter was not passed, optopt contains the
4486 * value that would normally lead us into the
4487 * appropriate case statement. If it's > 256, then this
4488 * must be a longopt and we should look at argv to get
4489 * the string. Otherwise it's just the character, so we
4490 * should use it directly.
4492 if (optopt
<= UINT8_MAX
) {
4493 (void) fprintf(stderr
,
4494 gettext("missing argument for '%c' "
4495 "option\n"), optopt
);
4497 (void) fprintf(stderr
,
4498 gettext("missing argument for '%s' "
4499 "option\n"), argv
[optind
- 1]);
4506 * If an invalid flag was passed, optopt contains the
4507 * character if it was a short flag, or 0 if it was a
4511 (void) fprintf(stderr
,
4512 gettext("invalid option '%c'\n"), optopt
);
4514 (void) fprintf(stderr
,
4515 gettext("invalid option '%s'\n"),
4523 if (flags
.parsable
&& flags
.verbosity
== 0)
4524 flags
.verbosity
= 1;
4526 if (excludes
.count
> 0 && !flags
.replicate
) {
4527 (void) fprintf(stderr
, gettext("Cannot specify "
4528 "dataset exclusion (-X) on a non-recursive "
4536 if (resume_token
!= NULL
) {
4537 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
4538 flags
.backup
|| flags
.holds
||
4539 flags
.saved
|| redactbook
!= NULL
) {
4540 (void) fprintf(stderr
,
4541 gettext("invalid flags combined with -t\n"));
4545 (void) fprintf(stderr
, gettext("too many arguments\n"));
4550 (void) fprintf(stderr
,
4551 gettext("missing snapshot argument\n"));
4555 (void) fprintf(stderr
, gettext("too many arguments\n"));
4561 if (fromname
!= NULL
|| flags
.replicate
|| flags
.props
||
4562 flags
.doall
|| flags
.backup
||
4563 flags
.holds
|| flags
.largeblock
|| flags
.embed_data
||
4564 flags
.compress
|| flags
.raw
|| redactbook
!= NULL
) {
4565 (void) fprintf(stderr
, gettext("incompatible flags "
4566 "combined with saved send flag\n"));
4569 if (strchr(argv
[0], '@') != NULL
) {
4570 (void) fprintf(stderr
, gettext("saved send must "
4571 "specify the dataset with partially-received "
4577 if (flags
.raw
&& redactbook
!= NULL
) {
4578 (void) fprintf(stderr
,
4579 gettext("Error: raw sends may not be redacted.\n"));
4583 if (!flags
.dryrun
&& isatty(STDOUT_FILENO
)) {
4584 (void) fprintf(stderr
,
4585 gettext("Error: Stream can not be written to a terminal.\n"
4586 "You must redirect standard output.\n"));
4591 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
4595 err
= zfs_send_saved(zhp
, &flags
, STDOUT_FILENO
,
4599 } else if (resume_token
!= NULL
) {
4600 return (zfs_send_resume(g_zfs
, &flags
, STDOUT_FILENO
,
4604 if (flags
.skipmissing
&& !flags
.replicate
) {
4605 (void) fprintf(stderr
,
4606 gettext("skip-missing flag can only be used in "
4607 "conjunction with replicate\n"));
4612 * For everything except -R and -I, use the new, cleaner code path.
4614 if (!(flags
.replicate
|| flags
.doall
)) {
4615 char frombuf
[ZFS_MAX_DATASET_NAME_LEN
];
4617 if (fromname
!= NULL
&& (strchr(fromname
, '#') == NULL
&&
4618 strchr(fromname
, '@') == NULL
)) {
4620 * Neither bookmark or snapshot was specified. Print a
4621 * warning, and assume snapshot.
4623 (void) fprintf(stderr
, "Warning: incremental source "
4624 "didn't specify type, assuming snapshot. Use '@' "
4625 "or '#' prefix to avoid ambiguity.\n");
4626 (void) snprintf(frombuf
, sizeof (frombuf
), "@%s",
4630 if (fromname
!= NULL
&&
4631 (fromname
[0] == '#' || fromname
[0] == '@')) {
4633 * Incremental source name begins with # or @.
4634 * Default to same fs as target.
4636 char tmpbuf
[ZFS_MAX_DATASET_NAME_LEN
];
4637 (void) strlcpy(tmpbuf
, fromname
, sizeof (tmpbuf
));
4638 (void) strlcpy(frombuf
, argv
[0], sizeof (frombuf
));
4639 cp
= strchr(frombuf
, '@');
4642 (void) strlcat(frombuf
, tmpbuf
, sizeof (frombuf
));
4646 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
);
4649 err
= zfs_send_one(zhp
, fromname
, STDOUT_FILENO
, &flags
,
4655 if (fromname
!= NULL
&& strchr(fromname
, '#')) {
4656 (void) fprintf(stderr
,
4657 gettext("Error: multiple snapshots cannot be "
4658 "sent from a bookmark.\n"));
4662 if (redactbook
!= NULL
) {
4663 (void) fprintf(stderr
, gettext("Error: multiple snapshots "
4664 "cannot be sent redacted.\n"));
4668 if ((cp
= strchr(argv
[0], '@')) == NULL
) {
4669 (void) fprintf(stderr
, gettext("Error: "
4670 "Unsupported flag with filesystem or bookmark.\n"));
4675 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4680 * If they specified the full path to the snapshot, chop off
4681 * everything except the short name of the snapshot, but special
4682 * case if they specify the origin.
4684 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
4685 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
4688 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
4689 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
4691 if (strcmp(origin
, fromname
) == 0) {
4693 flags
.fromorigin
= B_TRUE
;
4696 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
4697 (void) fprintf(stderr
,
4698 gettext("incremental source must be "
4699 "in same filesystem\n"));
4703 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
4704 (void) fprintf(stderr
,
4705 gettext("invalid incremental source\n"));
4711 if (flags
.replicate
&& fromname
== NULL
)
4712 flags
.doall
= B_TRUE
;
4714 err
= zfs_send(zhp
, fromname
, toname
, &flags
, STDOUT_FILENO
,
4715 excludes
.count
> 0 ? zfs_do_send_exclude
: NULL
,
4716 &excludes
, flags
.verbosity
>= 3 ? &dbgnv
: NULL
);
4718 if (flags
.verbosity
>= 3 && dbgnv
!= NULL
) {
4720 * dump_nvlist prints to stdout, but that's been
4721 * redirected to a file. Make it print to stderr
4724 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
4725 dump_nvlist(dbgnv
, 0);
4730 free(excludes
.list
);
4735 * Restore a backup stream from stdin.
4738 zfs_do_receive(int argc
, char **argv
)
4741 recvflags_t flags
= { 0 };
4742 boolean_t abort_resumable
= B_FALSE
;
4745 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
4749 while ((c
= getopt(argc
, argv
, ":o:x:dehMnuvFsAc")) != -1) {
4752 if (!parseprop(props
, optarg
)) {
4758 if (!parsepropname(props
, optarg
)) {
4765 (void) fprintf(stderr
, gettext("invalid option "
4766 "combination: -d and -e are mutually "
4770 flags
.isprefix
= B_TRUE
;
4773 if (flags
.isprefix
) {
4774 (void) fprintf(stderr
, gettext("invalid option "
4775 "combination: -d and -e are mutually "
4779 flags
.istail
= B_TRUE
;
4782 flags
.skipholds
= B_TRUE
;
4785 flags
.forceunmount
= B_TRUE
;
4788 flags
.dryrun
= B_TRUE
;
4791 flags
.nomount
= B_TRUE
;
4794 flags
.verbose
= B_TRUE
;
4797 flags
.resumable
= B_TRUE
;
4800 flags
.force
= B_TRUE
;
4803 abort_resumable
= B_TRUE
;
4806 flags
.heal
= B_TRUE
;
4809 (void) fprintf(stderr
, gettext("missing argument for "
4810 "'%c' option\n"), optopt
);
4814 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4823 /* zfs recv -e (use "tail" name) implies -d (remove dataset "head") */
4825 flags
.isprefix
= B_TRUE
;
4827 /* check number of arguments */
4829 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
4833 (void) fprintf(stderr
, gettext("too many arguments\n"));
4837 if (abort_resumable
) {
4838 if (flags
.isprefix
|| flags
.istail
|| flags
.dryrun
||
4839 flags
.resumable
|| flags
.nomount
) {
4840 (void) fprintf(stderr
, gettext("invalid option\n"));
4844 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
4845 (void) snprintf(namebuf
, sizeof (namebuf
),
4846 "%s/%%recv", argv
[0]);
4848 if (zfs_dataset_exists(g_zfs
, namebuf
,
4849 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) {
4850 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4851 namebuf
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4856 err
= zfs_destroy(zhp
, B_FALSE
);
4859 zfs_handle_t
*zhp
= zfs_open(g_zfs
,
4860 argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4863 if (!zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) ||
4864 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
4865 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == -1) {
4866 (void) fprintf(stderr
,
4867 gettext("'%s' does not have any "
4868 "resumable receive state to abort\n"),
4874 err
= zfs_destroy(zhp
, B_FALSE
);
4881 if (isatty(STDIN_FILENO
)) {
4882 (void) fprintf(stderr
,
4883 gettext("Error: Backup stream can not be read "
4884 "from a terminal.\n"
4885 "You must redirect standard input.\n"));
4889 err
= zfs_receive(g_zfs
, argv
[0], props
, &flags
, STDIN_FILENO
, NULL
);
4896 * allow/unallow stuff
4898 /* copied from zfs/sys/dsl_deleg.h */
4899 #define ZFS_DELEG_PERM_CREATE "create"
4900 #define ZFS_DELEG_PERM_DESTROY "destroy"
4901 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4902 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4903 #define ZFS_DELEG_PERM_CLONE "clone"
4904 #define ZFS_DELEG_PERM_PROMOTE "promote"
4905 #define ZFS_DELEG_PERM_RENAME "rename"
4906 #define ZFS_DELEG_PERM_MOUNT "mount"
4907 #define ZFS_DELEG_PERM_SHARE "share"
4908 #define ZFS_DELEG_PERM_SEND "send"
4909 #define ZFS_DELEG_PERM_RECEIVE "receive"
4910 #define ZFS_DELEG_PERM_ALLOW "allow"
4911 #define ZFS_DELEG_PERM_USERPROP "userprop"
4912 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4913 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4914 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4915 #define ZFS_DELEG_PERM_USERUSED "userused"
4916 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4917 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
4918 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
4919 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
4920 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
4922 #define ZFS_DELEG_PERM_HOLD "hold"
4923 #define ZFS_DELEG_PERM_RELEASE "release"
4924 #define ZFS_DELEG_PERM_DIFF "diff"
4925 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4926 #define ZFS_DELEG_PERM_LOAD_KEY "load-key"
4927 #define ZFS_DELEG_PERM_CHANGE_KEY "change-key"
4929 #define ZFS_DELEG_PERM_PROJECTUSED "projectused"
4930 #define ZFS_DELEG_PERM_PROJECTQUOTA "projectquota"
4931 #define ZFS_DELEG_PERM_PROJECTOBJUSED "projectobjused"
4932 #define ZFS_DELEG_PERM_PROJECTOBJQUOTA "projectobjquota"
4934 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4936 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl
[] = {
4937 { ZFS_DELEG_PERM_ALLOW
, ZFS_DELEG_NOTE_ALLOW
},
4938 { ZFS_DELEG_PERM_CLONE
, ZFS_DELEG_NOTE_CLONE
},
4939 { ZFS_DELEG_PERM_CREATE
, ZFS_DELEG_NOTE_CREATE
},
4940 { ZFS_DELEG_PERM_DESTROY
, ZFS_DELEG_NOTE_DESTROY
},
4941 { ZFS_DELEG_PERM_DIFF
, ZFS_DELEG_NOTE_DIFF
},
4942 { ZFS_DELEG_PERM_HOLD
, ZFS_DELEG_NOTE_HOLD
},
4943 { ZFS_DELEG_PERM_MOUNT
, ZFS_DELEG_NOTE_MOUNT
},
4944 { ZFS_DELEG_PERM_PROMOTE
, ZFS_DELEG_NOTE_PROMOTE
},
4945 { ZFS_DELEG_PERM_RECEIVE
, ZFS_DELEG_NOTE_RECEIVE
},
4946 { ZFS_DELEG_PERM_RELEASE
, ZFS_DELEG_NOTE_RELEASE
},
4947 { ZFS_DELEG_PERM_RENAME
, ZFS_DELEG_NOTE_RENAME
},
4948 { ZFS_DELEG_PERM_ROLLBACK
, ZFS_DELEG_NOTE_ROLLBACK
},
4949 { ZFS_DELEG_PERM_SEND
, ZFS_DELEG_NOTE_SEND
},
4950 { ZFS_DELEG_PERM_SHARE
, ZFS_DELEG_NOTE_SHARE
},
4951 { ZFS_DELEG_PERM_SNAPSHOT
, ZFS_DELEG_NOTE_SNAPSHOT
},
4952 { ZFS_DELEG_PERM_BOOKMARK
, ZFS_DELEG_NOTE_BOOKMARK
},
4953 { ZFS_DELEG_PERM_LOAD_KEY
, ZFS_DELEG_NOTE_LOAD_KEY
},
4954 { ZFS_DELEG_PERM_CHANGE_KEY
, ZFS_DELEG_NOTE_CHANGE_KEY
},
4956 { ZFS_DELEG_PERM_GROUPQUOTA
, ZFS_DELEG_NOTE_GROUPQUOTA
},
4957 { ZFS_DELEG_PERM_GROUPUSED
, ZFS_DELEG_NOTE_GROUPUSED
},
4958 { ZFS_DELEG_PERM_USERPROP
, ZFS_DELEG_NOTE_USERPROP
},
4959 { ZFS_DELEG_PERM_USERQUOTA
, ZFS_DELEG_NOTE_USERQUOTA
},
4960 { ZFS_DELEG_PERM_USERUSED
, ZFS_DELEG_NOTE_USERUSED
},
4961 { ZFS_DELEG_PERM_USEROBJQUOTA
, ZFS_DELEG_NOTE_USEROBJQUOTA
},
4962 { ZFS_DELEG_PERM_USEROBJUSED
, ZFS_DELEG_NOTE_USEROBJUSED
},
4963 { ZFS_DELEG_PERM_GROUPOBJQUOTA
, ZFS_DELEG_NOTE_GROUPOBJQUOTA
},
4964 { ZFS_DELEG_PERM_GROUPOBJUSED
, ZFS_DELEG_NOTE_GROUPOBJUSED
},
4965 { ZFS_DELEG_PERM_PROJECTUSED
, ZFS_DELEG_NOTE_PROJECTUSED
},
4966 { ZFS_DELEG_PERM_PROJECTQUOTA
, ZFS_DELEG_NOTE_PROJECTQUOTA
},
4967 { ZFS_DELEG_PERM_PROJECTOBJUSED
, ZFS_DELEG_NOTE_PROJECTOBJUSED
},
4968 { ZFS_DELEG_PERM_PROJECTOBJQUOTA
, ZFS_DELEG_NOTE_PROJECTOBJQUOTA
},
4969 { NULL
, ZFS_DELEG_NOTE_NONE
}
4972 /* permission structure */
4973 typedef struct deleg_perm
{
4974 zfs_deleg_who_type_t dp_who_type
;
4975 const char *dp_name
;
4977 boolean_t dp_descend
;
4981 typedef struct deleg_perm_node
{
4982 deleg_perm_t dpn_perm
;
4984 uu_avl_node_t dpn_avl_node
;
4985 } deleg_perm_node_t
;
4987 typedef struct fs_perm fs_perm_t
;
4989 /* permissions set */
4990 typedef struct who_perm
{
4991 zfs_deleg_who_type_t who_type
;
4992 const char *who_name
; /* id */
4993 char who_ug_name
[256]; /* user/group name */
4994 fs_perm_t
*who_fsperm
; /* uplink */
4996 uu_avl_t
*who_deleg_perm_avl
; /* permissions */
5000 typedef struct who_perm_node
{
5001 who_perm_t who_perm
;
5002 uu_avl_node_t who_avl_node
;
5005 typedef struct fs_perm_set fs_perm_set_t
;
5006 /* fs permissions */
5008 const char *fsp_name
;
5010 uu_avl_t
*fsp_sc_avl
; /* sets,create */
5011 uu_avl_t
*fsp_uge_avl
; /* user,group,everyone */
5013 fs_perm_set_t
*fsp_set
; /* uplink */
5017 typedef struct fs_perm_node
{
5018 fs_perm_t fspn_fsperm
;
5021 uu_list_node_t fspn_list_node
;
5024 /* top level structure */
5025 struct fs_perm_set
{
5026 uu_list_pool_t
*fsps_list_pool
;
5027 uu_list_t
*fsps_list
; /* list of fs_perms */
5029 uu_avl_pool_t
*fsps_named_set_avl_pool
;
5030 uu_avl_pool_t
*fsps_who_perm_avl_pool
;
5031 uu_avl_pool_t
*fsps_deleg_perm_avl_pool
;
5034 static inline const char *
5035 deleg_perm_type(zfs_deleg_note_t note
)
5041 case ZFS_DELEG_NOTE_GROUPQUOTA
:
5042 case ZFS_DELEG_NOTE_GROUPUSED
:
5043 case ZFS_DELEG_NOTE_USERPROP
:
5044 case ZFS_DELEG_NOTE_USERQUOTA
:
5045 case ZFS_DELEG_NOTE_USERUSED
:
5046 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
5047 case ZFS_DELEG_NOTE_USEROBJUSED
:
5048 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
5049 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
5050 case ZFS_DELEG_NOTE_PROJECTUSED
:
5051 case ZFS_DELEG_NOTE_PROJECTQUOTA
:
5052 case ZFS_DELEG_NOTE_PROJECTOBJUSED
:
5053 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA
:
5055 return (gettext("other"));
5057 return (gettext("subcommand"));
5062 who_type2weight(zfs_deleg_who_type_t who_type
)
5066 case ZFS_DELEG_NAMED_SET_SETS
:
5067 case ZFS_DELEG_NAMED_SET
:
5070 case ZFS_DELEG_CREATE_SETS
:
5071 case ZFS_DELEG_CREATE
:
5074 case ZFS_DELEG_USER_SETS
:
5075 case ZFS_DELEG_USER
:
5078 case ZFS_DELEG_GROUP_SETS
:
5079 case ZFS_DELEG_GROUP
:
5082 case ZFS_DELEG_EVERYONE_SETS
:
5083 case ZFS_DELEG_EVERYONE
:
5094 who_perm_compare(const void *larg
, const void *rarg
, void *unused
)
5097 const who_perm_node_t
*l
= larg
;
5098 const who_perm_node_t
*r
= rarg
;
5099 zfs_deleg_who_type_t ltype
= l
->who_perm
.who_type
;
5100 zfs_deleg_who_type_t rtype
= r
->who_perm
.who_type
;
5101 int lweight
= who_type2weight(ltype
);
5102 int rweight
= who_type2weight(rtype
);
5103 int res
= lweight
- rweight
;
5105 res
= strncmp(l
->who_perm
.who_name
, r
->who_perm
.who_name
,
5106 ZFS_MAX_DELEG_NAME
-1);
5117 deleg_perm_compare(const void *larg
, const void *rarg
, void *unused
)
5120 const deleg_perm_node_t
*l
= larg
;
5121 const deleg_perm_node_t
*r
= rarg
;
5122 int res
= strncmp(l
->dpn_perm
.dp_name
, r
->dpn_perm
.dp_name
,
5123 ZFS_MAX_DELEG_NAME
-1);
5135 fs_perm_set_init(fs_perm_set_t
*fspset
)
5137 memset(fspset
, 0, sizeof (fs_perm_set_t
));
5139 if ((fspset
->fsps_list_pool
= uu_list_pool_create("fsps_list_pool",
5140 sizeof (fs_perm_node_t
), offsetof(fs_perm_node_t
, fspn_list_node
),
5141 NULL
, UU_DEFAULT
)) == NULL
)
5143 if ((fspset
->fsps_list
= uu_list_create(fspset
->fsps_list_pool
, NULL
,
5144 UU_DEFAULT
)) == NULL
)
5147 if ((fspset
->fsps_named_set_avl_pool
= uu_avl_pool_create(
5148 "named_set_avl_pool", sizeof (who_perm_node_t
), offsetof(
5149 who_perm_node_t
, who_avl_node
), who_perm_compare
,
5150 UU_DEFAULT
)) == NULL
)
5153 if ((fspset
->fsps_who_perm_avl_pool
= uu_avl_pool_create(
5154 "who_perm_avl_pool", sizeof (who_perm_node_t
), offsetof(
5155 who_perm_node_t
, who_avl_node
), who_perm_compare
,
5156 UU_DEFAULT
)) == NULL
)
5159 if ((fspset
->fsps_deleg_perm_avl_pool
= uu_avl_pool_create(
5160 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t
), offsetof(
5161 deleg_perm_node_t
, dpn_avl_node
), deleg_perm_compare
, UU_DEFAULT
))
5166 static inline void fs_perm_fini(fs_perm_t
*);
5167 static inline void who_perm_fini(who_perm_t
*);
5170 fs_perm_set_fini(fs_perm_set_t
*fspset
)
5172 fs_perm_node_t
*node
= uu_list_first(fspset
->fsps_list
);
5174 while (node
!= NULL
) {
5175 fs_perm_node_t
*next_node
=
5176 uu_list_next(fspset
->fsps_list
, node
);
5177 fs_perm_t
*fsperm
= &node
->fspn_fsperm
;
5178 fs_perm_fini(fsperm
);
5179 uu_list_remove(fspset
->fsps_list
, node
);
5184 uu_avl_pool_destroy(fspset
->fsps_named_set_avl_pool
);
5185 uu_avl_pool_destroy(fspset
->fsps_who_perm_avl_pool
);
5186 uu_avl_pool_destroy(fspset
->fsps_deleg_perm_avl_pool
);
5190 deleg_perm_init(deleg_perm_t
*deleg_perm
, zfs_deleg_who_type_t type
,
5193 deleg_perm
->dp_who_type
= type
;
5194 deleg_perm
->dp_name
= name
;
5198 who_perm_init(who_perm_t
*who_perm
, fs_perm_t
*fsperm
,
5199 zfs_deleg_who_type_t type
, const char *name
)
5201 uu_avl_pool_t
*pool
;
5202 pool
= fsperm
->fsp_set
->fsps_deleg_perm_avl_pool
;
5204 memset(who_perm
, 0, sizeof (who_perm_t
));
5206 if ((who_perm
->who_deleg_perm_avl
= uu_avl_create(pool
, NULL
,
5207 UU_DEFAULT
)) == NULL
)
5210 who_perm
->who_type
= type
;
5211 who_perm
->who_name
= name
;
5212 who_perm
->who_fsperm
= fsperm
;
5216 who_perm_fini(who_perm_t
*who_perm
)
5218 deleg_perm_node_t
*node
= uu_avl_first(who_perm
->who_deleg_perm_avl
);
5220 while (node
!= NULL
) {
5221 deleg_perm_node_t
*next_node
=
5222 uu_avl_next(who_perm
->who_deleg_perm_avl
, node
);
5224 uu_avl_remove(who_perm
->who_deleg_perm_avl
, node
);
5229 uu_avl_destroy(who_perm
->who_deleg_perm_avl
);
5233 fs_perm_init(fs_perm_t
*fsperm
, fs_perm_set_t
*fspset
, const char *fsname
)
5235 uu_avl_pool_t
*nset_pool
= fspset
->fsps_named_set_avl_pool
;
5236 uu_avl_pool_t
*who_pool
= fspset
->fsps_who_perm_avl_pool
;
5238 memset(fsperm
, 0, sizeof (fs_perm_t
));
5240 if ((fsperm
->fsp_sc_avl
= uu_avl_create(nset_pool
, NULL
, UU_DEFAULT
))
5244 if ((fsperm
->fsp_uge_avl
= uu_avl_create(who_pool
, NULL
, UU_DEFAULT
))
5248 fsperm
->fsp_set
= fspset
;
5249 fsperm
->fsp_name
= fsname
;
5253 fs_perm_fini(fs_perm_t
*fsperm
)
5255 who_perm_node_t
*node
= uu_avl_first(fsperm
->fsp_sc_avl
);
5256 while (node
!= NULL
) {
5257 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_sc_avl
,
5259 who_perm_t
*who_perm
= &node
->who_perm
;
5260 who_perm_fini(who_perm
);
5261 uu_avl_remove(fsperm
->fsp_sc_avl
, node
);
5266 node
= uu_avl_first(fsperm
->fsp_uge_avl
);
5267 while (node
!= NULL
) {
5268 who_perm_node_t
*next_node
= uu_avl_next(fsperm
->fsp_uge_avl
,
5270 who_perm_t
*who_perm
= &node
->who_perm
;
5271 who_perm_fini(who_perm
);
5272 uu_avl_remove(fsperm
->fsp_uge_avl
, node
);
5277 uu_avl_destroy(fsperm
->fsp_sc_avl
);
5278 uu_avl_destroy(fsperm
->fsp_uge_avl
);
5282 set_deleg_perm_node(uu_avl_t
*avl
, deleg_perm_node_t
*node
,
5283 zfs_deleg_who_type_t who_type
, const char *name
, char locality
)
5285 uu_avl_index_t idx
= 0;
5287 deleg_perm_node_t
*found_node
= NULL
;
5288 deleg_perm_t
*deleg_perm
= &node
->dpn_perm
;
5290 deleg_perm_init(deleg_perm
, who_type
, name
);
5292 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
5294 uu_avl_insert(avl
, node
, idx
);
5297 deleg_perm
= &node
->dpn_perm
;
5302 case ZFS_DELEG_LOCAL
:
5303 deleg_perm
->dp_local
= B_TRUE
;
5305 case ZFS_DELEG_DESCENDENT
:
5306 deleg_perm
->dp_descend
= B_TRUE
;
5311 assert(B_FALSE
); /* invalid locality */
5316 parse_who_perm(who_perm_t
*who_perm
, nvlist_t
*nvl
, char locality
)
5318 nvpair_t
*nvp
= NULL
;
5319 fs_perm_set_t
*fspset
= who_perm
->who_fsperm
->fsp_set
;
5320 uu_avl_t
*avl
= who_perm
->who_deleg_perm_avl
;
5321 zfs_deleg_who_type_t who_type
= who_perm
->who_type
;
5323 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5324 const char *name
= nvpair_name(nvp
);
5325 data_type_t type
= nvpair_type(nvp
);
5326 uu_avl_pool_t
*avl_pool
= fspset
->fsps_deleg_perm_avl_pool
;
5327 deleg_perm_node_t
*node
=
5328 safe_malloc(sizeof (deleg_perm_node_t
));
5330 VERIFY(type
== DATA_TYPE_BOOLEAN
);
5332 uu_avl_node_init(node
, &node
->dpn_avl_node
, avl_pool
);
5333 set_deleg_perm_node(avl
, node
, who_type
, name
, locality
);
5340 parse_fs_perm(fs_perm_t
*fsperm
, nvlist_t
*nvl
)
5342 nvpair_t
*nvp
= NULL
;
5343 fs_perm_set_t
*fspset
= fsperm
->fsp_set
;
5345 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5346 nvlist_t
*nvl2
= NULL
;
5347 const char *name
= nvpair_name(nvp
);
5348 uu_avl_t
*avl
= NULL
;
5349 uu_avl_pool_t
*avl_pool
= NULL
;
5350 zfs_deleg_who_type_t perm_type
= name
[0];
5351 char perm_locality
= name
[1];
5352 const char *perm_name
= name
+ 3;
5353 who_perm_t
*who_perm
= NULL
;
5355 assert('$' == name
[2]);
5357 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
5360 switch (perm_type
) {
5361 case ZFS_DELEG_CREATE
:
5362 case ZFS_DELEG_CREATE_SETS
:
5363 case ZFS_DELEG_NAMED_SET
:
5364 case ZFS_DELEG_NAMED_SET_SETS
:
5365 avl_pool
= fspset
->fsps_named_set_avl_pool
;
5366 avl
= fsperm
->fsp_sc_avl
;
5368 case ZFS_DELEG_USER
:
5369 case ZFS_DELEG_USER_SETS
:
5370 case ZFS_DELEG_GROUP
:
5371 case ZFS_DELEG_GROUP_SETS
:
5372 case ZFS_DELEG_EVERYONE
:
5373 case ZFS_DELEG_EVERYONE_SETS
:
5374 avl_pool
= fspset
->fsps_who_perm_avl_pool
;
5375 avl
= fsperm
->fsp_uge_avl
;
5379 assert(!"unhandled zfs_deleg_who_type_t");
5382 who_perm_node_t
*found_node
= NULL
;
5383 who_perm_node_t
*node
= safe_malloc(
5384 sizeof (who_perm_node_t
));
5385 who_perm
= &node
->who_perm
;
5386 uu_avl_index_t idx
= 0;
5388 uu_avl_node_init(node
, &node
->who_avl_node
, avl_pool
);
5389 who_perm_init(who_perm
, fsperm
, perm_type
, perm_name
);
5391 if ((found_node
= uu_avl_find(avl
, node
, NULL
, &idx
))
5393 if (avl
== fsperm
->fsp_uge_avl
) {
5395 struct passwd
*p
= NULL
;
5396 struct group
*g
= NULL
;
5397 const char *nice_name
= NULL
;
5399 switch (perm_type
) {
5400 case ZFS_DELEG_USER_SETS
:
5401 case ZFS_DELEG_USER
:
5402 rid
= atoi(perm_name
);
5405 nice_name
= p
->pw_name
;
5407 case ZFS_DELEG_GROUP_SETS
:
5408 case ZFS_DELEG_GROUP
:
5409 rid
= atoi(perm_name
);
5412 nice_name
= g
->gr_name
;
5419 if (nice_name
!= NULL
) {
5421 node
->who_perm
.who_ug_name
,
5424 /* User or group unknown */
5426 node
->who_perm
.who_ug_name
,
5427 sizeof (node
->who_perm
.who_ug_name
),
5428 "(unknown: %d)", rid
);
5432 uu_avl_insert(avl
, node
, idx
);
5435 who_perm
= &node
->who_perm
;
5438 assert(who_perm
!= NULL
);
5439 (void) parse_who_perm(who_perm
, nvl2
, perm_locality
);
5446 parse_fs_perm_set(fs_perm_set_t
*fspset
, nvlist_t
*nvl
)
5448 nvpair_t
*nvp
= NULL
;
5449 uu_avl_index_t idx
= 0;
5451 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
5452 nvlist_t
*nvl2
= NULL
;
5453 const char *fsname
= nvpair_name(nvp
);
5454 data_type_t type
= nvpair_type(nvp
);
5455 fs_perm_t
*fsperm
= NULL
;
5456 fs_perm_node_t
*node
= safe_malloc(sizeof (fs_perm_node_t
));
5458 fsperm
= &node
->fspn_fsperm
;
5460 VERIFY(DATA_TYPE_NVLIST
== type
);
5462 uu_list_node_init(node
, &node
->fspn_list_node
,
5463 fspset
->fsps_list_pool
);
5465 idx
= uu_list_numnodes(fspset
->fsps_list
);
5466 fs_perm_init(fsperm
, fspset
, fsname
);
5468 if (nvpair_value_nvlist(nvp
, &nvl2
) != 0)
5471 (void) parse_fs_perm(fsperm
, nvl2
);
5473 uu_list_insert(fspset
->fsps_list
, node
, idx
);
5479 static inline const char *
5480 deleg_perm_comment(zfs_deleg_note_t note
)
5482 const char *str
= "";
5487 case ZFS_DELEG_NOTE_ALLOW
:
5488 str
= gettext("Must also have the permission that is being"
5489 "\n\t\t\t\tallowed");
5491 case ZFS_DELEG_NOTE_CLONE
:
5492 str
= gettext("Must also have the 'create' ability and 'mount'"
5493 "\n\t\t\t\tability in the origin file system");
5495 case ZFS_DELEG_NOTE_CREATE
:
5496 str
= gettext("Must also have the 'mount' ability");
5498 case ZFS_DELEG_NOTE_DESTROY
:
5499 str
= gettext("Must also have the 'mount' ability");
5501 case ZFS_DELEG_NOTE_DIFF
:
5502 str
= gettext("Allows lookup of paths within a dataset;"
5503 "\n\t\t\t\tgiven an object number. Ordinary users need this"
5504 "\n\t\t\t\tin order to use zfs diff");
5506 case ZFS_DELEG_NOTE_HOLD
:
5507 str
= gettext("Allows adding a user hold to a snapshot");
5509 case ZFS_DELEG_NOTE_MOUNT
:
5510 str
= gettext("Allows mount/umount of ZFS datasets");
5512 case ZFS_DELEG_NOTE_PROMOTE
:
5513 str
= gettext("Must also have the 'mount'\n\t\t\t\tand"
5514 " 'promote' ability in the origin file system");
5516 case ZFS_DELEG_NOTE_RECEIVE
:
5517 str
= gettext("Must also have the 'mount' and 'create'"
5520 case ZFS_DELEG_NOTE_RELEASE
:
5521 str
= gettext("Allows releasing a user hold which\n\t\t\t\t"
5522 "might destroy the snapshot");
5524 case ZFS_DELEG_NOTE_RENAME
:
5525 str
= gettext("Must also have the 'mount' and 'create'"
5526 "\n\t\t\t\tability in the new parent");
5528 case ZFS_DELEG_NOTE_ROLLBACK
:
5531 case ZFS_DELEG_NOTE_SEND
:
5534 case ZFS_DELEG_NOTE_SHARE
:
5535 str
= gettext("Allows sharing file systems over NFS or SMB"
5536 "\n\t\t\t\tprotocols");
5538 case ZFS_DELEG_NOTE_SNAPSHOT
:
5541 case ZFS_DELEG_NOTE_LOAD_KEY
:
5542 str
= gettext("Allows loading or unloading an encryption key");
5544 case ZFS_DELEG_NOTE_CHANGE_KEY
:
5545 str
= gettext("Allows changing or adding an encryption key");
5548 * case ZFS_DELEG_NOTE_VSCAN:
5549 * str = gettext("");
5553 case ZFS_DELEG_NOTE_GROUPQUOTA
:
5554 str
= gettext("Allows accessing any groupquota@... property");
5556 case ZFS_DELEG_NOTE_GROUPUSED
:
5557 str
= gettext("Allows reading any groupused@... property");
5559 case ZFS_DELEG_NOTE_USERPROP
:
5560 str
= gettext("Allows changing any user property");
5562 case ZFS_DELEG_NOTE_USERQUOTA
:
5563 str
= gettext("Allows accessing any userquota@... property");
5565 case ZFS_DELEG_NOTE_USERUSED
:
5566 str
= gettext("Allows reading any userused@... property");
5568 case ZFS_DELEG_NOTE_USEROBJQUOTA
:
5569 str
= gettext("Allows accessing any userobjquota@... property");
5571 case ZFS_DELEG_NOTE_GROUPOBJQUOTA
:
5572 str
= gettext("Allows accessing any \n\t\t\t\t"
5573 "groupobjquota@... property");
5575 case ZFS_DELEG_NOTE_GROUPOBJUSED
:
5576 str
= gettext("Allows reading any groupobjused@... property");
5578 case ZFS_DELEG_NOTE_USEROBJUSED
:
5579 str
= gettext("Allows reading any userobjused@... property");
5581 case ZFS_DELEG_NOTE_PROJECTQUOTA
:
5582 str
= gettext("Allows accessing any projectquota@... property");
5584 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA
:
5585 str
= gettext("Allows accessing any \n\t\t\t\t"
5586 "projectobjquota@... property");
5588 case ZFS_DELEG_NOTE_PROJECTUSED
:
5589 str
= gettext("Allows reading any projectused@... property");
5591 case ZFS_DELEG_NOTE_PROJECTOBJUSED
:
5592 str
= gettext("Allows accessing any \n\t\t\t\t"
5593 "projectobjused@... property");
5611 boolean_t recursive
; /* unallow only */
5612 boolean_t prt_usage
;
5614 boolean_t prt_perms
;
5617 const char *dataset
;
5621 prop_cmp(const void *a
, const void *b
)
5623 const char *str1
= *(const char **)a
;
5624 const char *str2
= *(const char **)b
;
5625 return (strcmp(str1
, str2
));
5629 allow_usage(boolean_t un
, boolean_t requested
, const char *msg
)
5631 const char *opt_desc
[] = {
5632 "-h", gettext("show this help message and exit"),
5633 "-l", gettext("set permission locally"),
5634 "-d", gettext("set permission for descents"),
5635 "-u", gettext("set permission for user"),
5636 "-g", gettext("set permission for group"),
5637 "-e", gettext("set permission for everyone"),
5638 "-c", gettext("set create time permission"),
5639 "-s", gettext("define permission set"),
5641 "-r", gettext("remove permissions recursively"),
5643 size_t unallow_size
= sizeof (opt_desc
) / sizeof (char *);
5644 size_t allow_size
= unallow_size
- 2;
5645 const char *props
[ZFS_NUM_PROPS
];
5648 FILE *fp
= requested
? stdout
: stderr
;
5649 zprop_desc_t
*pdtbl
= zfs_prop_get_table();
5650 const char *fmt
= gettext("%-16s %-14s\t%s\n");
5652 (void) fprintf(fp
, gettext("Usage: %s\n"), get_usage(un
? HELP_UNALLOW
:
5654 (void) fprintf(fp
, gettext("Options:\n"));
5655 for (i
= 0; i
< (un
? unallow_size
: allow_size
); i
+= 2) {
5656 const char *opt
= opt_desc
[i
];
5657 const char *optdsc
= opt_desc
[i
+ 1];
5658 (void) fprintf(fp
, gettext(" %-10s %s\n"), opt
, optdsc
);
5661 (void) fprintf(fp
, gettext("\nThe following permissions are "
5663 (void) fprintf(fp
, fmt
, gettext("NAME"), gettext("TYPE"),
5665 for (i
= 0; i
< ZFS_NUM_DELEG_NOTES
; i
++) {
5666 const char *perm_name
= zfs_deleg_perm_tbl
[i
].z_perm
;
5667 zfs_deleg_note_t perm_note
= zfs_deleg_perm_tbl
[i
].z_note
;
5668 const char *perm_type
= deleg_perm_type(perm_note
);
5669 const char *perm_comment
= deleg_perm_comment(perm_note
);
5670 (void) fprintf(fp
, fmt
, perm_name
, perm_type
, perm_comment
);
5673 for (i
= 0; i
< ZFS_NUM_PROPS
; i
++) {
5674 zprop_desc_t
*pd
= &pdtbl
[i
];
5675 if (pd
->pd_visible
!= B_TRUE
)
5678 if (pd
->pd_attr
== PROP_READONLY
)
5681 props
[count
++] = pd
->pd_name
;
5683 props
[count
] = NULL
;
5685 qsort(props
, count
, sizeof (char *), prop_cmp
);
5687 for (i
= 0; i
< count
; i
++)
5688 (void) fprintf(fp
, fmt
, props
[i
], gettext("property"), "");
5691 (void) fprintf(fp
, gettext("\nzfs: error: %s"), msg
);
5693 exit(requested
? 0 : 2);
5696 static inline const char *
5697 munge_args(int argc
, char **argv
, boolean_t un
, size_t expected_argc
,
5700 if (un
&& argc
== expected_argc
- 1)
5702 else if (argc
== expected_argc
)
5703 *permsp
= argv
[argc
- 2];
5705 allow_usage(un
, B_FALSE
,
5706 gettext("wrong number of parameters\n"));
5708 return (argv
[argc
- 1]);
5712 parse_allow_args(int argc
, char **argv
, boolean_t un
, struct allow_opts
*opts
)
5714 int uge_sum
= opts
->user
+ opts
->group
+ opts
->everyone
;
5715 int csuge_sum
= opts
->create
+ opts
->set
+ uge_sum
;
5716 int ldcsuge_sum
= csuge_sum
+ opts
->local
+ opts
->descend
;
5717 int all_sum
= un
? ldcsuge_sum
+ opts
->recursive
: ldcsuge_sum
;
5720 allow_usage(un
, B_FALSE
,
5721 gettext("-u, -g, and -e are mutually exclusive\n"));
5723 if (opts
->prt_usage
) {
5724 if (argc
== 0 && all_sum
== 0)
5725 allow_usage(un
, B_TRUE
, NULL
);
5732 allow_usage(un
, B_FALSE
,
5733 gettext("invalid options combined with -s\n"));
5735 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
5736 if (argv
[0][0] != '@')
5737 allow_usage(un
, B_FALSE
,
5738 gettext("invalid set name: missing '@' prefix\n"));
5739 opts
->who
= argv
[0];
5740 } else if (opts
->create
) {
5741 if (ldcsuge_sum
> 1)
5742 allow_usage(un
, B_FALSE
,
5743 gettext("invalid options combined with -c\n"));
5744 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5745 } else if (opts
->everyone
) {
5747 allow_usage(un
, B_FALSE
,
5748 gettext("invalid options combined with -e\n"));
5749 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5750 } else if (uge_sum
== 0 && argc
> 0 && strcmp(argv
[0], "everyone")
5752 opts
->everyone
= B_TRUE
;
5755 opts
->dataset
= munge_args(argc
, argv
, un
, 2, &opts
->perms
);
5756 } else if (argc
== 1 && !un
) {
5757 opts
->prt_perms
= B_TRUE
;
5758 opts
->dataset
= argv
[argc
-1];
5760 opts
->dataset
= munge_args(argc
, argv
, un
, 3, &opts
->perms
);
5761 opts
->who
= argv
[0];
5764 if (!opts
->local
&& !opts
->descend
) {
5765 opts
->local
= B_TRUE
;
5766 opts
->descend
= B_TRUE
;
5771 store_allow_perm(zfs_deleg_who_type_t type
, boolean_t local
, boolean_t descend
,
5772 const char *who
, char *perms
, nvlist_t
*top_nvl
)
5775 char ld
[2] = { '\0', '\0' };
5776 char who_buf
[MAXNAMELEN
+ 32];
5777 char base_type
= '\0';
5778 char set_type
= '\0';
5779 nvlist_t
*base_nvl
= NULL
;
5780 nvlist_t
*set_nvl
= NULL
;
5783 if (nvlist_alloc(&base_nvl
, NV_UNIQUE_NAME
, 0) != 0)
5785 if (nvlist_alloc(&set_nvl
, NV_UNIQUE_NAME
, 0) != 0)
5789 case ZFS_DELEG_NAMED_SET_SETS
:
5790 case ZFS_DELEG_NAMED_SET
:
5791 set_type
= ZFS_DELEG_NAMED_SET_SETS
;
5792 base_type
= ZFS_DELEG_NAMED_SET
;
5793 ld
[0] = ZFS_DELEG_NA
;
5795 case ZFS_DELEG_CREATE_SETS
:
5796 case ZFS_DELEG_CREATE
:
5797 set_type
= ZFS_DELEG_CREATE_SETS
;
5798 base_type
= ZFS_DELEG_CREATE
;
5799 ld
[0] = ZFS_DELEG_NA
;
5801 case ZFS_DELEG_USER_SETS
:
5802 case ZFS_DELEG_USER
:
5803 set_type
= ZFS_DELEG_USER_SETS
;
5804 base_type
= ZFS_DELEG_USER
;
5806 ld
[0] = ZFS_DELEG_LOCAL
;
5808 ld
[1] = ZFS_DELEG_DESCENDENT
;
5810 case ZFS_DELEG_GROUP_SETS
:
5811 case ZFS_DELEG_GROUP
:
5812 set_type
= ZFS_DELEG_GROUP_SETS
;
5813 base_type
= ZFS_DELEG_GROUP
;
5815 ld
[0] = ZFS_DELEG_LOCAL
;
5817 ld
[1] = ZFS_DELEG_DESCENDENT
;
5819 case ZFS_DELEG_EVERYONE_SETS
:
5820 case ZFS_DELEG_EVERYONE
:
5821 set_type
= ZFS_DELEG_EVERYONE_SETS
;
5822 base_type
= ZFS_DELEG_EVERYONE
;
5824 ld
[0] = ZFS_DELEG_LOCAL
;
5826 ld
[1] = ZFS_DELEG_DESCENDENT
;
5830 assert(set_type
!= '\0' && base_type
!= '\0');
5833 if (perms
!= NULL
) {
5835 char *end
= curr
+ strlen(perms
);
5837 while (curr
< end
) {
5838 char *delim
= strchr(curr
, ',');
5849 (void) nvlist_add_boolean(nvl
, curr
);
5855 for (i
= 0; i
< 2; i
++) {
5856 char locality
= ld
[i
];
5860 if (!nvlist_empty(base_nvl
)) {
5862 (void) snprintf(who_buf
,
5863 sizeof (who_buf
), "%c%c$%s",
5864 base_type
, locality
, who
);
5866 (void) snprintf(who_buf
,
5867 sizeof (who_buf
), "%c%c$",
5868 base_type
, locality
);
5870 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5875 if (!nvlist_empty(set_nvl
)) {
5877 (void) snprintf(who_buf
,
5878 sizeof (who_buf
), "%c%c$%s",
5879 set_type
, locality
, who
);
5881 (void) snprintf(who_buf
,
5882 sizeof (who_buf
), "%c%c$",
5883 set_type
, locality
);
5885 (void) nvlist_add_nvlist(top_nvl
, who_buf
,
5890 for (i
= 0; i
< 2; i
++) {
5891 char locality
= ld
[i
];
5896 (void) snprintf(who_buf
, sizeof (who_buf
),
5897 "%c%c$%s", base_type
, locality
, who
);
5899 (void) snprintf(who_buf
, sizeof (who_buf
),
5900 "%c%c$", base_type
, locality
);
5901 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5904 (void) snprintf(who_buf
, sizeof (who_buf
),
5905 "%c%c$%s", set_type
, locality
, who
);
5907 (void) snprintf(who_buf
, sizeof (who_buf
),
5908 "%c%c$", set_type
, locality
);
5909 (void) nvlist_add_boolean(top_nvl
, who_buf
);
5915 construct_fsacl_list(boolean_t un
, struct allow_opts
*opts
, nvlist_t
**nvlp
)
5917 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0)
5921 store_allow_perm(ZFS_DELEG_NAMED_SET
, opts
->local
,
5922 opts
->descend
, opts
->who
, opts
->perms
, *nvlp
);
5923 } else if (opts
->create
) {
5924 store_allow_perm(ZFS_DELEG_CREATE
, opts
->local
,
5925 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5926 } else if (opts
->everyone
) {
5927 store_allow_perm(ZFS_DELEG_EVERYONE
, opts
->local
,
5928 opts
->descend
, NULL
, opts
->perms
, *nvlp
);
5930 char *curr
= opts
->who
;
5931 char *end
= curr
+ strlen(curr
);
5933 while (curr
< end
) {
5935 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
5937 char *delim
= strchr(curr
, ',');
5940 struct passwd
*p
= NULL
;
5941 struct group
*g
= NULL
;
5949 rid
= (uid_t
)strtol(curr
, &endch
, 0);
5951 who_type
= ZFS_DELEG_USER
;
5959 else if (*endch
!= '\0') {
5960 (void) snprintf(errbuf
, 256, gettext(
5961 "invalid user %s\n"), curr
);
5962 allow_usage(un
, B_TRUE
, errbuf
);
5964 } else if (opts
->group
) {
5965 who_type
= ZFS_DELEG_GROUP
;
5973 else if (*endch
!= '\0') {
5974 (void) snprintf(errbuf
, 256, gettext(
5975 "invalid group %s\n"), curr
);
5976 allow_usage(un
, B_TRUE
, errbuf
);
5979 if (*endch
!= '\0') {
5986 if (*endch
!= '\0') {
5994 who_type
= ZFS_DELEG_USER
;
5996 } else if (g
!= NULL
) {
5997 who_type
= ZFS_DELEG_GROUP
;
6000 (void) snprintf(errbuf
, 256, gettext(
6001 "invalid user/group %s\n"), curr
);
6002 allow_usage(un
, B_TRUE
, errbuf
);
6006 (void) sprintf(id
, "%u", rid
);
6009 store_allow_perm(who_type
, opts
->local
,
6010 opts
->descend
, who
, opts
->perms
, *nvlp
);
6019 print_set_creat_perms(uu_avl_t
*who_avl
)
6021 const char *sc_title
[] = {
6022 gettext("Permission sets:\n"),
6023 gettext("Create time permissions:\n"),
6026 who_perm_node_t
*who_node
= NULL
;
6027 int prev_weight
= -1;
6029 for (who_node
= uu_avl_first(who_avl
); who_node
!= NULL
;
6030 who_node
= uu_avl_next(who_avl
, who_node
)) {
6031 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
6032 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
6033 const char *who_name
= who_node
->who_perm
.who_name
;
6034 int weight
= who_type2weight(who_type
);
6035 boolean_t first
= B_TRUE
;
6036 deleg_perm_node_t
*deleg_node
;
6038 if (prev_weight
!= weight
) {
6039 (void) printf("%s", sc_title
[weight
]);
6040 prev_weight
= weight
;
6043 if (who_name
== NULL
|| strnlen(who_name
, 1) == 0)
6044 (void) printf("\t");
6046 (void) printf("\t%s ", who_name
);
6048 for (deleg_node
= uu_avl_first(avl
); deleg_node
!= NULL
;
6049 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
6052 deleg_node
->dpn_perm
.dp_name
);
6055 (void) printf(",%s",
6056 deleg_node
->dpn_perm
.dp_name
);
6059 (void) printf("\n");
6064 print_uge_deleg_perms(uu_avl_t
*who_avl
, boolean_t local
, boolean_t descend
,
6067 who_perm_node_t
*who_node
= NULL
;
6068 boolean_t prt_title
= B_TRUE
;
6069 uu_avl_walk_t
*walk
;
6071 if ((walk
= uu_avl_walk_start(who_avl
, UU_WALK_ROBUST
)) == NULL
)
6074 while ((who_node
= uu_avl_walk_next(walk
)) != NULL
) {
6075 const char *who_name
= who_node
->who_perm
.who_name
;
6076 const char *nice_who_name
= who_node
->who_perm
.who_ug_name
;
6077 uu_avl_t
*avl
= who_node
->who_perm
.who_deleg_perm_avl
;
6078 zfs_deleg_who_type_t who_type
= who_node
->who_perm
.who_type
;
6080 deleg_perm_node_t
*deleg_node
;
6081 boolean_t prt_who
= B_TRUE
;
6083 for (deleg_node
= uu_avl_first(avl
);
6085 deleg_node
= uu_avl_next(avl
, deleg_node
)) {
6086 if (local
!= deleg_node
->dpn_perm
.dp_local
||
6087 descend
!= deleg_node
->dpn_perm
.dp_descend
)
6091 const char *who
= NULL
;
6093 prt_title
= B_FALSE
;
6094 (void) printf("%s", title
);
6098 case ZFS_DELEG_USER_SETS
:
6099 case ZFS_DELEG_USER
:
6100 who
= gettext("user");
6102 who_name
= nice_who_name
;
6104 case ZFS_DELEG_GROUP_SETS
:
6105 case ZFS_DELEG_GROUP
:
6106 who
= gettext("group");
6108 who_name
= nice_who_name
;
6110 case ZFS_DELEG_EVERYONE_SETS
:
6111 case ZFS_DELEG_EVERYONE
:
6112 who
= gettext("everyone");
6117 assert(who
!= NULL
);
6121 if (who_name
== NULL
)
6122 (void) printf("\t%s", who
);
6124 (void) printf("\t%s %s", who
, who_name
);
6127 (void) printf("%c%s", delim
,
6128 deleg_node
->dpn_perm
.dp_name
);
6133 (void) printf("\n");
6136 uu_avl_walk_end(walk
);
6140 print_fs_perms(fs_perm_set_t
*fspset
)
6142 fs_perm_node_t
*node
= NULL
;
6143 char buf
[MAXNAMELEN
+ 32];
6144 const char *dsname
= buf
;
6146 for (node
= uu_list_first(fspset
->fsps_list
); node
!= NULL
;
6147 node
= uu_list_next(fspset
->fsps_list
, node
)) {
6148 uu_avl_t
*sc_avl
= node
->fspn_fsperm
.fsp_sc_avl
;
6149 uu_avl_t
*uge_avl
= node
->fspn_fsperm
.fsp_uge_avl
;
6152 (void) snprintf(buf
, sizeof (buf
),
6153 gettext("---- Permissions on %s "),
6154 node
->fspn_fsperm
.fsp_name
);
6155 (void) printf("%s", dsname
);
6156 left
= 70 - strlen(buf
);
6159 (void) printf("\n");
6161 print_set_creat_perms(sc_avl
);
6162 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_FALSE
,
6163 gettext("Local permissions:\n"));
6164 print_uge_deleg_perms(uge_avl
, B_FALSE
, B_TRUE
,
6165 gettext("Descendent permissions:\n"));
6166 print_uge_deleg_perms(uge_avl
, B_TRUE
, B_TRUE
,
6167 gettext("Local+Descendent permissions:\n"));
6171 static fs_perm_set_t fs_perm_set
= { NULL
, NULL
, NULL
, NULL
};
6173 struct deleg_perms
{
6179 set_deleg_perms(zfs_handle_t
*zhp
, void *data
)
6181 struct deleg_perms
*perms
= (struct deleg_perms
*)data
;
6182 zfs_type_t zfs_type
= zfs_get_type(zhp
);
6184 if (zfs_type
!= ZFS_TYPE_FILESYSTEM
&& zfs_type
!= ZFS_TYPE_VOLUME
)
6187 return (zfs_set_fsacl(zhp
, perms
->un
, perms
->nvl
));
6191 zfs_do_allow_unallow_impl(int argc
, char **argv
, boolean_t un
)
6194 nvlist_t
*perm_nvl
= NULL
;
6195 nvlist_t
*update_perm_nvl
= NULL
;
6198 struct allow_opts opts
= { 0 };
6200 const char *optstr
= un
? "ldugecsrh" : "ldugecsh";
6203 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
6206 opts
.local
= B_TRUE
;
6209 opts
.descend
= B_TRUE
;
6215 opts
.group
= B_TRUE
;
6218 opts
.everyone
= B_TRUE
;
6224 opts
.create
= B_TRUE
;
6227 opts
.recursive
= B_TRUE
;
6230 (void) fprintf(stderr
, gettext("missing argument for "
6231 "'%c' option\n"), optopt
);
6235 opts
.prt_usage
= B_TRUE
;
6238 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6247 /* check arguments */
6248 parse_allow_args(argc
, argv
, un
, &opts
);
6250 /* try to open the dataset */
6251 if ((zhp
= zfs_open(g_zfs
, opts
.dataset
, ZFS_TYPE_FILESYSTEM
|
6252 ZFS_TYPE_VOLUME
)) == NULL
) {
6253 (void) fprintf(stderr
, "Failed to open dataset: %s\n",
6258 if (zfs_get_fsacl(zhp
, &perm_nvl
) != 0)
6261 fs_perm_set_init(&fs_perm_set
);
6262 if (parse_fs_perm_set(&fs_perm_set
, perm_nvl
) != 0) {
6263 (void) fprintf(stderr
, "Failed to parse fsacl permissions\n");
6268 print_fs_perms(&fs_perm_set
);
6270 (void) construct_fsacl_list(un
, &opts
, &update_perm_nvl
);
6271 if (zfs_set_fsacl(zhp
, un
, update_perm_nvl
) != 0)
6274 if (un
&& opts
.recursive
) {
6275 struct deleg_perms data
= { un
, update_perm_nvl
};
6276 if (zfs_iter_filesystems(zhp
, set_deleg_perms
,
6285 nvlist_free(perm_nvl
);
6286 nvlist_free(update_perm_nvl
);
6288 fs_perm_set_fini(&fs_perm_set
);
6296 zfs_do_allow(int argc
, char **argv
)
6298 return (zfs_do_allow_unallow_impl(argc
, argv
, B_FALSE
));
6302 zfs_do_unallow(int argc
, char **argv
)
6304 return (zfs_do_allow_unallow_impl(argc
, argv
, B_TRUE
));
6308 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
6313 boolean_t recursive
= B_FALSE
;
6314 const char *opts
= holding
? "rt" : "r";
6318 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
6324 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6333 /* check number of arguments */
6341 if (holding
&& tag
[0] == '.') {
6342 /* tags starting with '.' are reserved for libzfs */
6343 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
6347 for (i
= 0; i
< argc
; ++i
) {
6349 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
6351 char *path
= argv
[i
];
6353 delim
= strchr(path
, '@');
6354 if (delim
== NULL
) {
6355 (void) fprintf(stderr
,
6356 gettext("'%s' is not a snapshot\n"), path
);
6360 (void) strncpy(parent
, path
, delim
- path
);
6361 parent
[delim
- path
] = '\0';
6363 zhp
= zfs_open(g_zfs
, parent
,
6364 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
6370 if (zfs_hold(zhp
, delim
+1, tag
, recursive
, -1) != 0)
6373 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
6379 return (errors
!= 0);
6383 * zfs hold [-r] [-t] <tag> <snap> ...
6385 * -r Recursively hold
6387 * Apply a user-hold with the given tag to the list of snapshots.
6390 zfs_do_hold(int argc
, char **argv
)
6392 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
6396 * zfs release [-r] <tag> <snap> ...
6398 * -r Recursively release
6400 * Release a user-hold with the given tag from the list of snapshots.
6403 zfs_do_release(int argc
, char **argv
)
6405 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
6408 typedef struct holds_cbdata
{
6409 boolean_t cb_recursive
;
6410 const char *cb_snapname
;
6412 size_t cb_max_namelen
;
6413 size_t cb_max_taglen
;
6416 #define STRFTIME_FMT_STR "%a %b %e %H:%M %Y"
6417 #define DATETIME_BUF_LEN (32)
6422 print_holds(boolean_t scripted
, int nwidth
, int tagwidth
, nvlist_t
*nvl
)
6425 nvpair_t
*nvp
= NULL
;
6426 const char *const hdr_cols
[] = { "NAME", "TAG", "TIMESTAMP" };
6430 for (i
= 0; i
< 3; i
++) {
6431 col
= gettext(hdr_cols
[i
]);
6433 (void) printf("%-*s ", i
? tagwidth
: nwidth
,
6436 (void) printf("%s\n", col
);
6440 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
6441 char *zname
= nvpair_name(nvp
);
6443 nvpair_t
*nvp2
= NULL
;
6444 (void) nvpair_value_nvlist(nvp
, &nvl2
);
6445 while ((nvp2
= nvlist_next_nvpair(nvl2
, nvp2
)) != NULL
) {
6446 char tsbuf
[DATETIME_BUF_LEN
];
6447 const char *tagname
= nvpair_name(nvp2
);
6452 (void) nvpair_value_uint64(nvp2
, &val
);
6454 (void) localtime_r(&time
, &t
);
6455 (void) strftime(tsbuf
, DATETIME_BUF_LEN
,
6456 gettext(STRFTIME_FMT_STR
), &t
);
6459 (void) printf("%s\t%s\t%s\n", zname
,
6462 (void) printf("%-*s %-*s %s\n", nwidth
,
6463 zname
, tagwidth
, tagname
, tsbuf
);
6470 * Generic callback function to list a dataset or snapshot.
6473 holds_callback(zfs_handle_t
*zhp
, void *data
)
6475 holds_cbdata_t
*cbp
= data
;
6476 nvlist_t
*top_nvl
= *cbp
->cb_nvlp
;
6477 nvlist_t
*nvl
= NULL
;
6478 nvpair_t
*nvp
= NULL
;
6479 const char *zname
= zfs_get_name(zhp
);
6480 size_t znamelen
= strlen(zname
);
6482 if (cbp
->cb_recursive
) {
6483 const char *snapname
;
6484 char *delim
= strchr(zname
, '@');
6488 snapname
= delim
+ 1;
6489 if (strcmp(cbp
->cb_snapname
, snapname
))
6493 if (zfs_get_holds(zhp
, &nvl
) != 0)
6496 if (znamelen
> cbp
->cb_max_namelen
)
6497 cbp
->cb_max_namelen
= znamelen
;
6499 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
6500 const char *tag
= nvpair_name(nvp
);
6501 size_t taglen
= strlen(tag
);
6502 if (taglen
> cbp
->cb_max_taglen
)
6503 cbp
->cb_max_taglen
= taglen
;
6506 return (nvlist_add_nvlist(top_nvl
, zname
, nvl
));
6510 * zfs holds [-rH] <snap> ...
6512 * -r Lists holds that are set on the named snapshots recursively.
6513 * -H Scripted mode; elide headers and separate columns by tabs.
6516 zfs_do_holds(int argc
, char **argv
)
6519 boolean_t errors
= B_FALSE
;
6520 boolean_t scripted
= B_FALSE
;
6521 boolean_t recursive
= B_FALSE
;
6523 int types
= ZFS_TYPE_SNAPSHOT
;
6524 holds_cbdata_t cb
= { 0 };
6531 while ((c
= getopt(argc
, argv
, "rH")) != -1) {
6540 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6547 types
|= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
6548 flags
|= ZFS_ITER_RECURSE
;
6554 /* check number of arguments */
6558 nvlist_t
*nvl
= fnvlist_alloc();
6560 for (int i
= 0; i
< argc
; ++i
) {
6561 char *snapshot
= argv
[i
];
6563 const char *snapname
;
6565 delim
= strchr(snapshot
, '@');
6566 if (delim
== NULL
) {
6567 (void) fprintf(stderr
,
6568 gettext("'%s' is not a snapshot\n"), snapshot
);
6572 snapname
= delim
+ 1;
6574 snapshot
[delim
- snapshot
] = '\0';
6576 cb
.cb_recursive
= recursive
;
6577 cb
.cb_snapname
= snapname
;
6581 * 1. collect holds data, set format options
6583 ret
= zfs_for_each(1, argv
+ i
, flags
, types
, NULL
, NULL
, limit
,
6584 holds_callback
, &cb
);
6590 * 2. print holds data
6592 print_holds(scripted
, cb
.cb_max_namelen
, cb
.cb_max_taglen
, nvl
);
6594 if (nvlist_empty(nvl
))
6595 (void) fprintf(stderr
, gettext("no datasets available\n"));
6602 #define CHECK_SPINNER 30
6603 #define SPINNER_TIME 3 /* seconds */
6604 #define MOUNT_TIME 1 /* seconds */
6606 typedef struct get_all_state
{
6607 boolean_t ga_verbose
;
6608 get_all_cb_t
*ga_cbp
;
6612 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
6614 static const char *const spin
[] = { "-", "\\", "|", "/" };
6615 static int spinval
= 0;
6616 static int spincheck
= 0;
6617 static time_t last_spin_time
= (time_t)0;
6618 get_all_state_t
*state
= data
;
6619 zfs_type_t type
= zfs_get_type(zhp
);
6621 if (state
->ga_verbose
) {
6622 if (--spincheck
< 0) {
6623 time_t now
= time(NULL
);
6624 if (last_spin_time
+ SPINNER_TIME
< now
) {
6625 update_progress(spin
[spinval
++ % 4]);
6626 last_spin_time
= now
;
6628 spincheck
= CHECK_SPINNER
;
6633 * Iterate over any nested datasets.
6635 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
6641 * Skip any datasets whose type does not match.
6643 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
6647 libzfs_add_handle(state
->ga_cbp
, zhp
);
6648 assert(state
->ga_cbp
->cb_used
<= state
->ga_cbp
->cb_alloc
);
6654 get_all_datasets(get_all_cb_t
*cbp
, boolean_t verbose
)
6656 get_all_state_t state
= {
6657 .ga_verbose
= verbose
,
6662 set_progress_header(gettext("Reading ZFS config"));
6663 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &state
);
6666 finish_progress(gettext("done."));
6670 * Generic callback for sharing or mounting filesystems. Because the code is so
6671 * similar, we have a common function with an extra parameter to determine which
6672 * mode we are using.
6674 typedef enum { OP_SHARE
, OP_MOUNT
} share_mount_op_t
;
6676 typedef struct share_mount_state
{
6677 share_mount_op_t sm_op
;
6678 boolean_t sm_verbose
;
6681 enum sa_protocol sm_proto
; /* only valid for OP_SHARE */
6682 pthread_mutex_t sm_lock
; /* protects the remaining fields */
6683 uint_t sm_total
; /* number of filesystems to process */
6684 uint_t sm_done
; /* number of filesystems processed */
6685 int sm_status
; /* -1 if any of the share/mount operations failed */
6686 } share_mount_state_t
;
6689 * Share or mount a dataset.
6692 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, enum sa_protocol protocol
,
6693 boolean_t
explicit, const char *options
)
6695 char mountpoint
[ZFS_MAXPROPLEN
];
6696 char shareopts
[ZFS_MAXPROPLEN
];
6697 char smbshareopts
[ZFS_MAXPROPLEN
];
6698 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
6700 uint64_t zoned
, canmount
;
6701 boolean_t shared_nfs
, shared_smb
;
6703 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
6706 * Check to make sure we can mount/share this dataset. If we
6707 * are in the global zone and the filesystem is exported to a
6708 * local zone, or if we are in a local zone and the
6709 * filesystem is not exported, then it is an error.
6711 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
6713 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
6717 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6718 "dataset is exported to a local zone\n"), cmdname
,
6722 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
6726 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6727 "permission denied\n"), cmdname
,
6733 * Ignore any filesystems which don't apply to us. This
6734 * includes those with a legacy mountpoint, or those with
6735 * legacy share options.
6737 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
6738 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
6739 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
6740 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
6741 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
6742 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
6744 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
6745 strcmp(smbshareopts
, "off") == 0) {
6749 (void) fprintf(stderr
, gettext("cannot share '%s': "
6750 "legacy share\n"), zfs_get_name(zhp
));
6751 (void) fprintf(stderr
, gettext("use exports(5) or "
6752 "smb.conf(5) to share this filesystem, or set "
6753 "the sharenfs or sharesmb property\n"));
6758 * We cannot share or mount legacy filesystems. If the
6759 * shareopts is non-legacy but the mountpoint is legacy, we
6760 * treat it as a legacy share.
6762 if (strcmp(mountpoint
, "legacy") == 0) {
6766 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6767 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
6768 (void) fprintf(stderr
, gettext("use %s(8) to "
6769 "%s this filesystem\n"), cmdname
, cmdname
);
6773 if (strcmp(mountpoint
, "none") == 0) {
6777 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
6778 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
6783 * canmount explicit outcome
6784 * on no pass through
6785 * on yes pass through
6787 * off yes display error, return 1
6788 * noauto no return 0
6789 * noauto yes pass through
6791 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
6792 if (canmount
== ZFS_CANMOUNT_OFF
) {
6796 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6797 "'canmount' property is set to 'off'\n"), cmdname
,
6800 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
6802 * When performing a 'zfs mount -a', we skip any mounts for
6803 * datasets that have 'noauto' set. Sharing a dataset with
6804 * 'noauto' set is only allowed if it's mounted.
6808 if (op
== OP_SHARE
&& !zfs_is_mounted(zhp
, NULL
)) {
6809 /* also purge it from existing exports */
6810 zfs_unshare(zhp
, mountpoint
, NULL
);
6816 * If this filesystem is encrypted and does not have
6817 * a loaded key, we can not mount it.
6819 if ((flags
& MS_CRYPT
) == 0 &&
6820 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) != ZIO_CRYPT_OFF
&&
6821 zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
) ==
6822 ZFS_KEYSTATUS_UNAVAILABLE
) {
6826 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6827 "encryption key not loaded\n"), cmdname
, zfs_get_name(zhp
));
6832 * If this filesystem is inconsistent and has a receive resume
6833 * token, we can not mount it.
6835 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
6836 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
6837 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
6841 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6842 "Contains partially-completed state from "
6843 "\"zfs receive -s\", which can be resumed with "
6844 "\"zfs send -t\"\n"),
6845 cmdname
, zfs_get_name(zhp
));
6849 if (zfs_prop_get_int(zhp
, ZFS_PROP_REDACTED
) && !(flags
& MS_FORCE
)) {
6853 (void) fprintf(stderr
, gettext("cannot %s '%s': "
6854 "Dataset is not complete, was created by receiving "
6855 "a redacted zfs send stream.\n"), cmdname
,
6861 * At this point, we have verified that the mountpoint and/or
6862 * shareopts are appropriate for auto management. If the
6863 * filesystem is already mounted or shared, return (failing
6864 * for explicit requests); otherwise mount or share the
6869 enum sa_protocol prot
[] = {SA_PROTOCOL_NFS
, SA_NO_PROTOCOL
};
6870 shared_nfs
= zfs_is_shared(zhp
, NULL
, prot
);
6871 *prot
= SA_PROTOCOL_SMB
;
6872 shared_smb
= zfs_is_shared(zhp
, NULL
, prot
);
6874 if ((shared_nfs
&& shared_smb
) ||
6875 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
6876 strcmp(smbshareopts
, "off") == 0) ||
6877 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
6878 strcmp(shareopts
, "off") == 0)) {
6882 (void) fprintf(stderr
, gettext("cannot share "
6883 "'%s': filesystem already shared\n"),
6888 if (!zfs_is_mounted(zhp
, NULL
) &&
6889 zfs_mount(zhp
, NULL
, flags
) != 0)
6893 if (zfs_share(zhp
, protocol
== SA_NO_PROTOCOL
? NULL
: prot
))
6900 mnt
.mnt_mntopts
= (char *)(options
?: "");
6902 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
6903 zfs_is_mounted(zhp
, NULL
)) {
6907 (void) fprintf(stderr
, gettext("cannot mount "
6908 "'%s': filesystem already mounted\n"),
6913 if (zfs_mount(zhp
, options
, flags
) != 0)
6922 * Reports progress in the form "(current/total)". Not thread-safe.
6925 report_mount_progress(int current
, int total
)
6927 static time_t last_progress_time
= 0;
6928 time_t now
= time(NULL
);
6931 /* display header if we're here for the first time */
6933 set_progress_header(gettext("Mounting ZFS filesystems"));
6934 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
6935 /* too soon to report again */
6939 last_progress_time
= now
;
6941 (void) sprintf(info
, "(%d/%d)", current
, total
);
6943 if (current
== total
)
6944 finish_progress(info
);
6946 update_progress(info
);
6950 * zfs_foreach_mountpoint() callback that mounts or shares one filesystem and
6951 * updates the progress meter.
6954 share_mount_one_cb(zfs_handle_t
*zhp
, void *arg
)
6956 share_mount_state_t
*sms
= arg
;
6959 ret
= share_mount_one(zhp
, sms
->sm_op
, sms
->sm_flags
, sms
->sm_proto
,
6960 B_FALSE
, sms
->sm_options
);
6962 pthread_mutex_lock(&sms
->sm_lock
);
6964 sms
->sm_status
= ret
;
6966 if (sms
->sm_verbose
)
6967 report_mount_progress(sms
->sm_done
, sms
->sm_total
);
6968 pthread_mutex_unlock(&sms
->sm_lock
);
6973 append_options(char *mntopts
, char *newopts
)
6975 int len
= strlen(mntopts
);
6977 /* original length plus new string to append plus 1 for the comma */
6978 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
6979 (void) fprintf(stderr
, gettext("the opts argument for "
6980 "'%s' option is too long (more than %d chars)\n"),
6981 "-o", MNT_LINE_MAX
);
6986 mntopts
[len
++] = ',';
6988 (void) strcpy(&mntopts
[len
], newopts
);
6991 static enum sa_protocol
6992 sa_protocol_decode(const char *protocol
)
6994 for (enum sa_protocol i
= 0; i
< ARRAY_SIZE(sa_protocol_names
); ++i
)
6995 if (strcmp(protocol
, sa_protocol_names
[i
]) == 0)
6998 (void) fputs(gettext("share type must be one of: "), stderr
);
6999 for (enum sa_protocol i
= 0;
7000 i
< ARRAY_SIZE(sa_protocol_names
); ++i
)
7001 (void) fprintf(stderr
, "%s%s",
7002 i
!= 0 ? ", " : "", sa_protocol_names
[i
]);
7003 (void) fputc('\n', stderr
);
7008 share_mount(int op
, int argc
, char **argv
)
7011 boolean_t verbose
= B_FALSE
;
7013 char *options
= NULL
;
7017 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":alvo:Of" : "al"))
7030 if (*optarg
== '\0') {
7031 (void) fprintf(stderr
, gettext("empty mount "
7032 "options (-o) specified\n"));
7036 if (options
== NULL
)
7037 options
= safe_malloc(MNT_LINE_MAX
+ 1);
7039 /* option validation is done later */
7040 append_options(options
, optarg
);
7043 flags
|= MS_OVERLAY
;
7049 (void) fprintf(stderr
, gettext("missing argument for "
7050 "'%c' option\n"), optopt
);
7054 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7063 /* check number of arguments */
7065 enum sa_protocol protocol
= SA_NO_PROTOCOL
;
7067 if (op
== OP_SHARE
&& argc
> 0) {
7068 protocol
= sa_protocol_decode(argv
[0]);
7074 (void) fprintf(stderr
, gettext("too many arguments\n"));
7078 start_progress_timer();
7079 get_all_cb_t cb
= { 0 };
7080 get_all_datasets(&cb
, verbose
);
7082 if (cb
.cb_used
== 0) {
7087 share_mount_state_t share_mount_state
= { 0 };
7088 share_mount_state
.sm_op
= op
;
7089 share_mount_state
.sm_verbose
= verbose
;
7090 share_mount_state
.sm_flags
= flags
;
7091 share_mount_state
.sm_options
= options
;
7092 share_mount_state
.sm_proto
= protocol
;
7093 share_mount_state
.sm_total
= cb
.cb_used
;
7094 pthread_mutex_init(&share_mount_state
.sm_lock
, NULL
);
7096 /* For a 'zfs share -a' operation start with a clean slate. */
7097 zfs_truncate_shares(NULL
);
7100 * libshare isn't mt-safe, so only do the operation in parallel
7101 * if we're mounting. Additionally, the key-loading option must
7102 * be serialized so that we can prompt the user for their keys
7103 * in a consistent manner.
7105 zfs_foreach_mountpoint(g_zfs
, cb
.cb_handles
, cb
.cb_used
,
7106 share_mount_one_cb
, &share_mount_state
,
7107 op
== OP_MOUNT
&& !(flags
& MS_CRYPT
));
7108 zfs_commit_shares(NULL
);
7110 ret
= share_mount_state
.sm_status
;
7112 for (int i
= 0; i
< cb
.cb_used
; i
++)
7113 zfs_close(cb
.cb_handles
[i
]);
7114 free(cb
.cb_handles
);
7115 } else if (argc
== 0) {
7117 struct mnttab entry
;
7119 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
7120 (void) fprintf(stderr
, gettext("missing filesystem "
7121 "argument (specify -a for all)\n"));
7126 * When mount is given no arguments, go through
7127 * /proc/self/mounts and display any active ZFS mounts.
7128 * We hide any snapshots, since they are controlled
7132 if ((mnttab
= fopen(MNTTAB
, "re")) == NULL
) {
7137 while (getmntent(mnttab
, &entry
) == 0) {
7138 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
7139 strchr(entry
.mnt_special
, '@') != NULL
)
7142 (void) printf("%-30s %s\n", entry
.mnt_special
,
7146 (void) fclose(mnttab
);
7151 (void) fprintf(stderr
,
7152 gettext("too many arguments\n"));
7156 if ((zhp
= zfs_open(g_zfs
, argv
[0],
7157 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
7160 ret
= share_mount_one(zhp
, op
, flags
, SA_NO_PROTOCOL
,
7162 zfs_commit_shares(NULL
);
7173 * zfs mount filesystem
7175 * Mount all filesystems, or mount the given filesystem.
7178 zfs_do_mount(int argc
, char **argv
)
7180 return (share_mount(OP_MOUNT
, argc
, argv
));
7184 * zfs share -a [nfs | smb]
7185 * zfs share filesystem
7187 * Share all filesystems, or share the given filesystem.
7190 zfs_do_share(int argc
, char **argv
)
7192 return (share_mount(OP_SHARE
, argc
, argv
));
7195 typedef struct unshare_unmount_node
{
7196 zfs_handle_t
*un_zhp
;
7198 uu_avl_node_t un_avlnode
;
7199 } unshare_unmount_node_t
;
7202 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
7205 const unshare_unmount_node_t
*l
= larg
;
7206 const unshare_unmount_node_t
*r
= rarg
;
7208 return (strcmp(l
->un_mountp
, r
->un_mountp
));
7212 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
7213 * absolute path, find the entry /proc/self/mounts, verify that it's a
7214 * ZFS filesystem, and unmount it appropriately.
7217 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
7221 struct stat64 statbuf
;
7222 struct extmnttab entry
;
7223 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
7227 * Search for the given (major,minor) pair in the mount table.
7230 if (getextmntent(path
, &entry
, &statbuf
) != 0) {
7231 if (op
== OP_SHARE
) {
7232 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
7233 "currently mounted\n"), cmdname
, path
);
7236 (void) fprintf(stderr
, gettext("warning: %s not in"
7237 "/proc/self/mounts\n"), path
);
7238 if ((ret
= umount2(path
, flags
)) != 0)
7239 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
7243 path_inode
= statbuf
.st_ino
;
7245 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
7246 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
7247 "filesystem\n"), cmdname
, path
);
7251 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
7252 ZFS_TYPE_FILESYSTEM
)) == NULL
)
7256 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
7257 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
7258 cmdname
, path
, strerror(errno
));
7260 } else if (statbuf
.st_ino
!= path_inode
) {
7261 (void) fprintf(stderr
, gettext("cannot "
7262 "%s '%s': not a mountpoint\n"), cmdname
, path
);
7266 if (op
== OP_SHARE
) {
7267 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
7268 char smbshare_prop
[ZFS_MAXPROPLEN
];
7270 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
7271 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
7272 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
7273 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
7275 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
7276 strcmp(smbshare_prop
, "off") == 0) {
7277 (void) fprintf(stderr
, gettext("cannot unshare "
7278 "'%s': legacy share\n"), path
);
7279 (void) fprintf(stderr
, gettext("use exportfs(8) "
7280 "or smbcontrol(1) to unshare this filesystem\n"));
7281 } else if (!zfs_is_shared(zhp
, NULL
, NULL
)) {
7282 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
7283 "not currently shared\n"), path
);
7285 ret
= zfs_unshare(zhp
, path
, NULL
);
7286 zfs_commit_shares(NULL
);
7289 char mtpt_prop
[ZFS_MAXPROPLEN
];
7291 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
7292 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
7295 ret
= zfs_unmount(zhp
, NULL
, flags
);
7296 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
7297 (void) fprintf(stderr
, gettext("cannot unmount "
7298 "'%s': legacy mountpoint\n"),
7300 (void) fprintf(stderr
, gettext("use umount(8) "
7301 "to unmount this filesystem\n"));
7303 ret
= zfs_unmountall(zhp
, flags
);
7314 * Generic callback for unsharing or unmounting a filesystem.
7317 unshare_unmount(int op
, int argc
, char **argv
)
7324 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
7325 char sharesmb
[ZFS_MAXPROPLEN
];
7328 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? ":a" : "afu")) != -1) {
7340 (void) fprintf(stderr
, gettext("missing argument for "
7341 "'%c' option\n"), optopt
);
7345 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7356 * We could make use of zfs_for_each() to walk all datasets in
7357 * the system, but this would be very inefficient, especially
7358 * since we would have to linearly search /proc/self/mounts for
7359 * each one. Instead, do one pass through /proc/self/mounts
7360 * looking for zfs entries and call zfs_unmount() for each one.
7362 * Things get a little tricky if the administrator has created
7363 * mountpoints beneath other ZFS filesystems. In this case, we
7364 * have to unmount the deepest filesystems first. To accomplish
7365 * this, we place all the mountpoints in an AVL tree sorted by
7366 * the special type (dataset name), and walk the result in
7367 * reverse to make sure to get any snapshots first.
7370 struct mnttab entry
;
7371 uu_avl_pool_t
*pool
;
7372 uu_avl_t
*tree
= NULL
;
7373 unshare_unmount_node_t
*node
;
7375 uu_avl_walk_t
*walk
;
7376 enum sa_protocol
*protocol
= NULL
,
7377 single_protocol
[] = {SA_NO_PROTOCOL
, SA_NO_PROTOCOL
};
7379 if (op
== OP_SHARE
&& argc
> 0) {
7380 *single_protocol
= sa_protocol_decode(argv
[0]);
7381 protocol
= single_protocol
;
7387 (void) fprintf(stderr
, gettext("too many arguments\n"));
7391 if (((pool
= uu_avl_pool_create("unmount_pool",
7392 sizeof (unshare_unmount_node_t
),
7393 offsetof(unshare_unmount_node_t
, un_avlnode
),
7394 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
7395 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
7398 if ((mnttab
= fopen(MNTTAB
, "re")) == NULL
)
7401 while (getmntent(mnttab
, &entry
) == 0) {
7403 /* ignore non-ZFS entries */
7404 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
7407 /* ignore snapshots */
7408 if (strchr(entry
.mnt_special
, '@') != NULL
)
7411 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
7412 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
7418 * Ignore datasets that are excluded/restricted by
7421 if (zpool_skip_pool(zfs_get_pool_name(zhp
))) {
7428 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
7430 sizeof (nfs_mnt_prop
),
7431 NULL
, NULL
, 0, B_FALSE
) == 0);
7432 if (strcmp(nfs_mnt_prop
, "off") != 0)
7434 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
7436 sizeof (nfs_mnt_prop
),
7437 NULL
, NULL
, 0, B_FALSE
) == 0);
7438 if (strcmp(nfs_mnt_prop
, "off") == 0)
7442 /* Ignore legacy mounts */
7443 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
7445 sizeof (nfs_mnt_prop
),
7446 NULL
, NULL
, 0, B_FALSE
) == 0);
7447 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
7449 /* Ignore canmount=noauto mounts */
7450 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
7451 ZFS_CANMOUNT_NOAUTO
)
7458 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
7460 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
7462 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
7464 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
7465 uu_avl_insert(tree
, node
, idx
);
7467 zfs_close(node
->un_zhp
);
7468 free(node
->un_mountp
);
7472 (void) fclose(mnttab
);
7475 * Walk the AVL tree in reverse, unmounting each filesystem and
7476 * removing it from the AVL tree in the process.
7478 if ((walk
= uu_avl_walk_start(tree
,
7479 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
7482 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
7483 const char *mntarg
= NULL
;
7485 uu_avl_remove(tree
, node
);
7488 if (zfs_unshare(node
->un_zhp
,
7489 node
->un_mountp
, protocol
) != 0)
7494 if (zfs_unmount(node
->un_zhp
,
7495 mntarg
, flags
) != 0)
7500 zfs_close(node
->un_zhp
);
7501 free(node
->un_mountp
);
7506 zfs_commit_shares(protocol
);
7508 uu_avl_walk_end(walk
);
7509 uu_avl_destroy(tree
);
7510 uu_avl_pool_destroy(pool
);
7515 (void) fprintf(stderr
,
7516 gettext("missing filesystem argument\n"));
7518 (void) fprintf(stderr
,
7519 gettext("too many arguments\n"));
7524 * We have an argument, but it may be a full path or a ZFS
7525 * filesystem. Pass full paths off to unmount_path() (shared by
7526 * manual_unmount), otherwise open the filesystem and pass to
7529 if (argv
[0][0] == '/')
7530 return (unshare_unmount_path(op
, argv
[0],
7533 if ((zhp
= zfs_open(g_zfs
, argv
[0],
7534 ZFS_TYPE_FILESYSTEM
)) == NULL
)
7537 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
7538 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
7539 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
7540 NULL
, 0, B_FALSE
) == 0);
7544 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
7546 sizeof (nfs_mnt_prop
),
7547 NULL
, NULL
, 0, B_FALSE
) == 0);
7548 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
7549 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
7552 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
7553 strcmp(sharesmb
, "off") == 0) {
7554 (void) fprintf(stderr
, gettext("cannot "
7555 "unshare '%s': legacy share\n"),
7557 (void) fprintf(stderr
, gettext("use "
7558 "exports(5) or smb.conf(5) to unshare "
7559 "this filesystem\n"));
7561 } else if (!zfs_is_shared(zhp
, NULL
, NULL
)) {
7562 (void) fprintf(stderr
, gettext("cannot "
7563 "unshare '%s': not currently "
7564 "shared\n"), zfs_get_name(zhp
));
7566 } else if (zfs_unshareall(zhp
, NULL
) != 0) {
7572 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
7573 (void) fprintf(stderr
, gettext("cannot "
7574 "unmount '%s': legacy "
7575 "mountpoint\n"), zfs_get_name(zhp
));
7576 (void) fprintf(stderr
, gettext("use "
7577 "umount(8) to unmount this "
7580 } else if (!zfs_is_mounted(zhp
, NULL
)) {
7581 (void) fprintf(stderr
, gettext("cannot "
7582 "unmount '%s': not currently "
7586 } else if (zfs_unmountall(zhp
, flags
) != 0) {
7599 * zfs unmount [-fu] -a
7600 * zfs unmount [-fu] filesystem
7602 * Unmount all filesystems, or a specific ZFS filesystem.
7605 zfs_do_unmount(int argc
, char **argv
)
7607 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
7612 * zfs unshare filesystem
7614 * Unshare all filesystems, or a specific ZFS filesystem.
7617 zfs_do_unshare(int argc
, char **argv
)
7619 return (unshare_unmount(OP_SHARE
, argc
, argv
));
7623 find_command_idx(const char *command
, int *idx
)
7627 for (i
= 0; i
< NCOMMAND
; i
++) {
7628 if (command_table
[i
].name
== NULL
)
7631 if (strcmp(command
, command_table
[i
].name
) == 0) {
7640 zfs_do_diff(int argc
, char **argv
)
7644 char *tosnap
= NULL
;
7645 char *fromsnap
= NULL
;
7649 struct sigaction sa
;
7651 while ((c
= getopt(argc
, argv
, "FHth")) != -1) {
7654 flags
|= ZFS_DIFF_CLASSIFY
;
7657 flags
|= ZFS_DIFF_PARSEABLE
;
7660 flags
|= ZFS_DIFF_TIMESTAMP
;
7663 flags
|= ZFS_DIFF_NO_MANGLE
;
7666 (void) fprintf(stderr
,
7667 gettext("invalid option '%c'\n"), optopt
);
7676 (void) fprintf(stderr
,
7677 gettext("must provide at least one snapshot name\n"));
7682 (void) fprintf(stderr
, gettext("too many arguments\n"));
7687 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
7690 if (*fromsnap
!= '@')
7691 copy
= strdup(fromsnap
);
7693 copy
= strdup(tosnap
);
7697 if ((atp
= strchr(copy
, '@')) != NULL
)
7700 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
) {
7707 * Ignore SIGPIPE so that the library can give us
7708 * information on any failure
7710 if (sigemptyset(&sa
.sa_mask
) == -1) {
7715 sa
.sa_handler
= SIG_IGN
;
7716 if (sigaction(SIGPIPE
, &sa
, NULL
) == -1) {
7721 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
7729 * zfs bookmark <fs@source>|<fs#source> <fs#bookmark>
7731 * Creates a bookmark with the given name from the source snapshot
7732 * or creates a copy of an existing source bookmark.
7735 zfs_do_bookmark(int argc
, char **argv
)
7737 char *source
, *bookname
;
7738 char expbuf
[ZFS_MAX_DATASET_NAME_LEN
];
7745 while ((c
= getopt(argc
, argv
, "")) != -1) {
7748 (void) fprintf(stderr
,
7749 gettext("invalid option '%c'\n"), optopt
);
7757 /* check number of arguments */
7759 (void) fprintf(stderr
, gettext("missing source argument\n"));
7763 (void) fprintf(stderr
, gettext("missing bookmark argument\n"));
7770 if (strchr(source
, '@') == NULL
&& strchr(source
, '#') == NULL
) {
7771 (void) fprintf(stderr
,
7772 gettext("invalid source name '%s': "
7773 "must contain a '@' or '#'\n"), source
);
7776 if (strchr(bookname
, '#') == NULL
) {
7777 (void) fprintf(stderr
,
7778 gettext("invalid bookmark name '%s': "
7779 "must contain a '#'\n"), bookname
);
7784 * expand source or bookname to full path:
7785 * one of them may be specified as short name
7789 char *source_short
, *bookname_short
;
7790 source_short
= strpbrk(source
, "@#");
7791 bookname_short
= strpbrk(bookname
, "#");
7792 if (source_short
== source
&&
7793 bookname_short
== bookname
) {
7794 (void) fprintf(stderr
, gettext(
7795 "either source or bookmark must be specified as "
7796 "full dataset paths"));
7798 } else if (source_short
!= source
&&
7799 bookname_short
!= bookname
) {
7801 } else if (source_short
!= source
) {
7802 strlcpy(expbuf
, source
, sizeof (expbuf
));
7804 } else if (bookname_short
!= bookname
) {
7805 strlcpy(expbuf
, bookname
, sizeof (expbuf
));
7810 if (expand
!= NULL
) {
7811 *strpbrk(expbuf
, "@#") = '\0'; /* dataset name in buf */
7812 (void) strlcat(expbuf
, *expand
, sizeof (expbuf
));
7817 /* determine source type */
7818 switch (*strpbrk(source
, "@#")) {
7819 case '@': source_type
= ZFS_TYPE_SNAPSHOT
; break;
7820 case '#': source_type
= ZFS_TYPE_BOOKMARK
; break;
7824 /* test the source exists */
7826 zhp
= zfs_open(g_zfs
, source
, source_type
);
7831 nvl
= fnvlist_alloc();
7832 fnvlist_add_string(nvl
, bookname
, source
);
7833 ret
= lzc_bookmark(nvl
, NULL
);
7837 const char *err_msg
= NULL
;
7840 (void) snprintf(errbuf
, sizeof (errbuf
),
7841 dgettext(TEXT_DOMAIN
,
7842 "cannot create bookmark '%s'"), bookname
);
7846 err_msg
= "bookmark is in a different pool";
7848 case ZFS_ERR_BOOKMARK_SOURCE_NOT_ANCESTOR
:
7849 err_msg
= "source is not an ancestor of the "
7850 "new bookmark's dataset";
7853 err_msg
= "bookmark exists";
7856 err_msg
= "invalid argument";
7859 err_msg
= "bookmark feature not enabled";
7862 err_msg
= "out of space";
7865 err_msg
= "dataset does not exist";
7868 (void) zfs_standard_error(g_zfs
, ret
, errbuf
);
7871 if (err_msg
!= NULL
) {
7872 (void) fprintf(stderr
, "%s: %s\n", errbuf
,
7873 dgettext(TEXT_DOMAIN
, err_msg
));
7885 zfs_do_channel_program(int argc
, char **argv
)
7888 size_t progsize
, progread
;
7889 nvlist_t
*outnvl
= NULL
;
7890 uint64_t instrlimit
= ZCP_DEFAULT_INSTRLIMIT
;
7891 uint64_t memlimit
= ZCP_DEFAULT_MEMLIMIT
;
7892 boolean_t sync_flag
= B_TRUE
, json_output
= B_FALSE
;
7893 zpool_handle_t
*zhp
;
7896 while ((c
= getopt(argc
, argv
, "nt:m:j")) != -1) {
7904 arg
= strtoull(optarg
, &endp
, 0);
7905 if (errno
!= 0 || *endp
!= '\0') {
7906 (void) fprintf(stderr
, gettext(
7908 "'%s': expected integer\n"), optarg
);
7915 ASSERT3U(c
, ==, 'm');
7921 sync_flag
= B_FALSE
;
7925 json_output
= B_TRUE
;
7929 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7939 (void) fprintf(stderr
,
7940 gettext("invalid number of arguments\n"));
7944 const char *poolname
= argv
[0];
7945 const char *filename
= argv
[1];
7946 if (strcmp(filename
, "-") == 0) {
7948 filename
= "standard input";
7949 } else if ((fd
= open(filename
, O_RDONLY
)) < 0) {
7950 (void) fprintf(stderr
, gettext("cannot open '%s': %s\n"),
7951 filename
, strerror(errno
));
7955 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
7956 (void) fprintf(stderr
, gettext("cannot open pool '%s'\n"),
7965 * Read in the channel program, expanding the program buffer as
7970 char *progbuf
= safe_malloc(progsize
);
7972 ret
= read(fd
, progbuf
+ progread
, progsize
- progread
);
7974 if (progread
== progsize
&& ret
> 0) {
7976 progbuf
= safe_realloc(progbuf
, progsize
);
7984 (void) fprintf(stderr
,
7985 gettext("cannot read '%s': %s\n"),
7986 filename
, strerror(errno
));
7989 progbuf
[progread
] = '\0';
7992 * Any remaining arguments are passed as arguments to the lua script as
7995 * "argv" -> [ "arg 1", ... "arg n" ],
7998 nvlist_t
*argnvl
= fnvlist_alloc();
7999 fnvlist_add_string_array(argnvl
, ZCP_ARG_CLIARGV
,
8000 (const char **)argv
+ 2, argc
- 2);
8003 ret
= lzc_channel_program(poolname
, progbuf
,
8004 instrlimit
, memlimit
, argnvl
, &outnvl
);
8006 ret
= lzc_channel_program_nosync(poolname
, progbuf
,
8007 instrlimit
, memlimit
, argnvl
, &outnvl
);
8012 * On error, report the error message handed back by lua if one
8013 * exists. Otherwise, generate an appropriate error message,
8014 * falling back on strerror() for an unexpected return code.
8016 const char *errstring
= NULL
;
8017 const char *msg
= gettext("Channel program execution failed");
8018 uint64_t instructions
= 0;
8019 if (outnvl
!= NULL
&& nvlist_exists(outnvl
, ZCP_RET_ERROR
)) {
8021 (void) nvlist_lookup_string(outnvl
,
8022 ZCP_RET_ERROR
, &es
);
8024 errstring
= strerror(ret
);
8028 (void) nvlist_lookup_uint64(outnvl
,
8029 ZCP_ARG_INSTRLIMIT
, &instructions
);
8035 "Invalid instruction or memory limit.";
8038 errstring
= "Return value too large.";
8041 errstring
= "Memory limit exhausted.";
8044 errstring
= "Timed out.";
8047 errstring
= "Permission denied. Channel "
8048 "programs must be run as root.";
8051 (void) zfs_standard_error(g_zfs
, ret
, msg
);
8054 if (errstring
!= NULL
)
8055 (void) fprintf(stderr
, "%s:\n%s\n", msg
, errstring
);
8057 if (ret
== ETIME
&& instructions
!= 0)
8058 (void) fprintf(stderr
,
8059 gettext("%llu Lua instructions\n"),
8060 (u_longlong_t
)instructions
);
8063 (void) nvlist_print_json(stdout
, outnvl
);
8064 } else if (nvlist_empty(outnvl
)) {
8065 (void) fprintf(stdout
, gettext("Channel program fully "
8066 "executed and did not produce output.\n"));
8068 (void) fprintf(stdout
, gettext("Channel program fully "
8069 "executed and produced output:\n"));
8070 dump_nvlist(outnvl
, 4);
8075 fnvlist_free(outnvl
);
8076 fnvlist_free(argnvl
);
8085 typedef struct loadkey_cbdata
{
8086 boolean_t cb_loadkey
;
8087 boolean_t cb_recursive
;
8089 char *cb_keylocation
;
8090 uint64_t cb_numfailed
;
8091 uint64_t cb_numattempted
;
8095 load_key_callback(zfs_handle_t
*zhp
, void *data
)
8098 boolean_t is_encroot
;
8099 loadkey_cbdata_t
*cb
= data
;
8100 uint64_t keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
8103 * If we are working recursively, we want to skip loading / unloading
8104 * keys for non-encryption roots and datasets whose keys are already
8105 * in the desired end-state.
8107 if (cb
->cb_recursive
) {
8108 ret
= zfs_crypto_get_encryption_root(zhp
, &is_encroot
, NULL
);
8114 if ((cb
->cb_loadkey
&& keystatus
== ZFS_KEYSTATUS_AVAILABLE
) ||
8115 (!cb
->cb_loadkey
&& keystatus
== ZFS_KEYSTATUS_UNAVAILABLE
))
8119 cb
->cb_numattempted
++;
8122 ret
= zfs_crypto_load_key(zhp
, cb
->cb_noop
, cb
->cb_keylocation
);
8124 ret
= zfs_crypto_unload_key(zhp
);
8135 load_unload_keys(int argc
, char **argv
, boolean_t loadkey
)
8137 int c
, ret
= 0, flags
= 0;
8138 boolean_t do_all
= B_FALSE
;
8139 loadkey_cbdata_t cb
= { 0 };
8141 cb
.cb_loadkey
= loadkey
;
8143 while ((c
= getopt(argc
, argv
, "anrL:")) != -1) {
8144 /* noop and alternate keylocations only apply to zfs load-key */
8148 cb
.cb_noop
= B_TRUE
;
8151 cb
.cb_keylocation
= optarg
;
8161 cb
.cb_recursive
= B_TRUE
;
8164 flags
|= ZFS_ITER_RECURSE
;
8165 cb
.cb_recursive
= B_TRUE
;
8168 (void) fprintf(stderr
,
8169 gettext("invalid option '%c'\n"), optopt
);
8177 if (!do_all
&& argc
== 0) {
8178 (void) fprintf(stderr
,
8179 gettext("Missing dataset argument or -a option\n"));
8183 if (do_all
&& argc
!= 0) {
8184 (void) fprintf(stderr
,
8185 gettext("Cannot specify dataset with -a option\n"));
8189 if (cb
.cb_recursive
&& cb
.cb_keylocation
!= NULL
&&
8190 strcmp(cb
.cb_keylocation
, "prompt") != 0) {
8191 (void) fprintf(stderr
, gettext("alternate keylocation may only "
8192 "be 'prompt' with -r or -a\n"));
8196 ret
= zfs_for_each(argc
, argv
, flags
,
8197 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
, NULL
, NULL
, 0,
8198 load_key_callback
, &cb
);
8200 if (cb
.cb_noop
|| (cb
.cb_recursive
&& cb
.cb_numattempted
!= 0)) {
8201 (void) printf(gettext("%llu / %llu key(s) successfully %s\n"),
8202 (u_longlong_t
)(cb
.cb_numattempted
- cb
.cb_numfailed
),
8203 (u_longlong_t
)cb
.cb_numattempted
,
8204 loadkey
? (cb
.cb_noop
? "verified" : "loaded") :
8208 if (cb
.cb_numfailed
!= 0)
8215 zfs_do_load_key(int argc
, char **argv
)
8217 return (load_unload_keys(argc
, argv
, B_TRUE
));
8222 zfs_do_unload_key(int argc
, char **argv
)
8224 return (load_unload_keys(argc
, argv
, B_FALSE
));
8228 zfs_do_change_key(int argc
, char **argv
)
8232 boolean_t loadkey
= B_FALSE
, inheritkey
= B_FALSE
;
8233 zfs_handle_t
*zhp
= NULL
;
8234 nvlist_t
*props
= fnvlist_alloc();
8236 while ((c
= getopt(argc
, argv
, "lio:")) != -1) {
8242 inheritkey
= B_TRUE
;
8245 if (!parseprop(props
, optarg
)) {
8251 (void) fprintf(stderr
,
8252 gettext("invalid option '%c'\n"), optopt
);
8257 if (inheritkey
&& !nvlist_empty(props
)) {
8258 (void) fprintf(stderr
,
8259 gettext("Properties not allowed for inheriting\n"));
8267 (void) fprintf(stderr
, gettext("Missing dataset argument\n"));
8272 (void) fprintf(stderr
, gettext("Too many arguments\n"));
8276 zhp
= zfs_open(g_zfs
, argv
[argc
- 1],
8277 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
8282 keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
8283 if (keystatus
!= ZFS_KEYSTATUS_AVAILABLE
) {
8284 ret
= zfs_crypto_load_key(zhp
, B_FALSE
, NULL
);
8292 /* refresh the properties so the new keystatus is visible */
8293 zfs_refresh_properties(zhp
);
8296 ret
= zfs_crypto_rewrap(zhp
, props
, inheritkey
);
8309 * 1) zfs project [-d|-r] <file|directory ...>
8310 * List project ID and inherit flag of file(s) or directories.
8311 * -d: List the directory itself, not its children.
8312 * -r: List subdirectories recursively.
8314 * 2) zfs project -C [-k] [-r] <file|directory ...>
8315 * Clear project inherit flag and/or ID on the file(s) or directories.
8316 * -k: Keep the project ID unchanged. If not specified, the project ID
8317 * will be reset as zero.
8318 * -r: Clear on subdirectories recursively.
8320 * 3) zfs project -c [-0] [-d|-r] [-p id] <file|directory ...>
8321 * Check project ID and inherit flag on the file(s) or directories,
8322 * report the outliers.
8323 * -0: Print file name followed by a NUL instead of newline.
8324 * -d: Check the directory itself, not its children.
8325 * -p: Specify the referenced ID for comparing with the target file(s)
8326 * or directories' project IDs. If not specified, the target (top)
8327 * directory's project ID will be used as the referenced one.
8328 * -r: Check subdirectories recursively.
8330 * 4) zfs project [-p id] [-r] [-s] <file|directory ...>
8331 * Set project ID and/or inherit flag on the file(s) or directories.
8332 * -p: Set the project ID as the given id.
8333 * -r: Set on subdirectories recursively. If not specify "-p" option,
8334 * it will use top-level directory's project ID as the given id,
8335 * then set both project ID and inherit flag on all descendants
8336 * of the top-level directory.
8337 * -s: Set project inherit flag.
8340 zfs_do_project(int argc
, char **argv
)
8342 zfs_project_control_t zpc
= {
8343 .zpc_expected_projid
= ZFS_INVALID_PROJID
,
8344 .zpc_op
= ZFS_PROJECT_OP_DEFAULT
,
8345 .zpc_dironly
= B_FALSE
,
8346 .zpc_keep_projid
= B_FALSE
,
8347 .zpc_newline
= B_TRUE
,
8348 .zpc_recursive
= B_FALSE
,
8349 .zpc_set_flag
= B_FALSE
,
8356 while ((c
= getopt(argc
, argv
, "0Ccdkp:rs")) != -1) {
8359 zpc
.zpc_newline
= B_FALSE
;
8362 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
8363 (void) fprintf(stderr
, gettext("cannot "
8364 "specify '-C' '-c' '-s' together\n"));
8368 zpc
.zpc_op
= ZFS_PROJECT_OP_CLEAR
;
8371 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
8372 (void) fprintf(stderr
, gettext("cannot "
8373 "specify '-C' '-c' '-s' together\n"));
8377 zpc
.zpc_op
= ZFS_PROJECT_OP_CHECK
;
8380 zpc
.zpc_dironly
= B_TRUE
;
8381 /* overwrite "-r" option */
8382 zpc
.zpc_recursive
= B_FALSE
;
8385 zpc
.zpc_keep_projid
= B_TRUE
;
8391 zpc
.zpc_expected_projid
= strtoull(optarg
, &endptr
, 0);
8392 if (errno
!= 0 || *endptr
!= '\0') {
8393 (void) fprintf(stderr
,
8394 gettext("project ID must be less than "
8395 "%u\n"), UINT32_MAX
);
8398 if (zpc
.zpc_expected_projid
>= UINT32_MAX
) {
8399 (void) fprintf(stderr
,
8400 gettext("invalid project ID\n"));
8406 zpc
.zpc_recursive
= B_TRUE
;
8407 /* overwrite "-d" option */
8408 zpc
.zpc_dironly
= B_FALSE
;
8411 if (zpc
.zpc_op
!= ZFS_PROJECT_OP_DEFAULT
) {
8412 (void) fprintf(stderr
, gettext("cannot "
8413 "specify '-C' '-c' '-s' together\n"));
8417 zpc
.zpc_set_flag
= B_TRUE
;
8418 zpc
.zpc_op
= ZFS_PROJECT_OP_SET
;
8421 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8427 if (zpc
.zpc_op
== ZFS_PROJECT_OP_DEFAULT
) {
8428 if (zpc
.zpc_expected_projid
!= ZFS_INVALID_PROJID
)
8429 zpc
.zpc_op
= ZFS_PROJECT_OP_SET
;
8431 zpc
.zpc_op
= ZFS_PROJECT_OP_LIST
;
8434 switch (zpc
.zpc_op
) {
8435 case ZFS_PROJECT_OP_LIST
:
8436 if (zpc
.zpc_keep_projid
) {
8437 (void) fprintf(stderr
,
8438 gettext("'-k' is only valid together with '-C'\n"));
8441 if (!zpc
.zpc_newline
) {
8442 (void) fprintf(stderr
,
8443 gettext("'-0' is only valid together with '-c'\n"));
8447 case ZFS_PROJECT_OP_CHECK
:
8448 if (zpc
.zpc_keep_projid
) {
8449 (void) fprintf(stderr
,
8450 gettext("'-k' is only valid together with '-C'\n"));
8454 case ZFS_PROJECT_OP_CLEAR
:
8455 if (zpc
.zpc_dironly
) {
8456 (void) fprintf(stderr
,
8457 gettext("'-d' is useless together with '-C'\n"));
8460 if (!zpc
.zpc_newline
) {
8461 (void) fprintf(stderr
,
8462 gettext("'-0' is only valid together with '-c'\n"));
8465 if (zpc
.zpc_expected_projid
!= ZFS_INVALID_PROJID
) {
8466 (void) fprintf(stderr
,
8467 gettext("'-p' is useless together with '-C'\n"));
8471 case ZFS_PROJECT_OP_SET
:
8472 if (zpc
.zpc_dironly
) {
8473 (void) fprintf(stderr
,
8474 gettext("'-d' is useless for set project ID and/or "
8478 if (zpc
.zpc_keep_projid
) {
8479 (void) fprintf(stderr
,
8480 gettext("'-k' is only valid together with '-C'\n"));
8483 if (!zpc
.zpc_newline
) {
8484 (void) fprintf(stderr
,
8485 gettext("'-0' is only valid together with '-c'\n"));
8497 (void) fprintf(stderr
,
8498 gettext("missing file or directory target(s)\n"));
8502 for (int i
= 0; i
< argc
; i
++) {
8505 err
= zfs_project_handle(argv
[i
], &zpc
);
8514 zfs_do_wait(int argc
, char **argv
)
8516 boolean_t enabled
[ZFS_WAIT_NUM_ACTIVITIES
];
8520 /* By default, wait for all types of activity. */
8521 for (i
= 0; i
< ZFS_WAIT_NUM_ACTIVITIES
; i
++)
8522 enabled
[i
] = B_TRUE
;
8524 while ((c
= getopt(argc
, argv
, "t:")) != -1) {
8527 /* Reset activities array */
8528 memset(&enabled
, 0, sizeof (enabled
));
8530 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
8531 static const char *const col_subopts
[
8532 ZFS_WAIT_NUM_ACTIVITIES
] = { "deleteq" };
8534 for (i
= 0; i
< ARRAY_SIZE(col_subopts
); ++i
)
8535 if (strcmp(tok
, col_subopts
[i
]) == 0) {
8536 enabled
[i
] = B_TRUE
;
8540 (void) fprintf(stderr
,
8541 gettext("invalid activity '%s'\n"), tok
);
8547 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8556 (void) fprintf(stderr
, gettext("missing 'filesystem' "
8561 (void) fprintf(stderr
, gettext("too many arguments\n"));
8565 zfs_handle_t
*zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
);
8570 boolean_t missing
= B_FALSE
;
8571 boolean_t any_waited
= B_FALSE
;
8573 for (int i
= 0; i
< ZFS_WAIT_NUM_ACTIVITIES
; i
++) {
8579 error
= zfs_wait_status(zhp
, i
, &missing
, &waited
);
8580 if (error
!= 0 || missing
)
8583 any_waited
= (any_waited
|| waited
);
8586 if (error
!= 0 || missing
|| !any_waited
)
8596 * Display version message
8599 zfs_do_version(int argc
, char **argv
)
8601 (void) argc
, (void) argv
;
8602 return (zfs_version_print() != 0);
8606 main(int argc
, char **argv
)
8610 const char *cmdname
;
8613 (void) setlocale(LC_ALL
, "");
8614 (void) setlocale(LC_NUMERIC
, "C");
8615 (void) textdomain(TEXT_DOMAIN
);
8620 * Make sure the user has specified some command.
8623 (void) fprintf(stderr
, gettext("missing command\n"));
8630 * The 'umount' command is an alias for 'unmount'
8632 if (strcmp(cmdname
, "umount") == 0)
8633 cmdname
= "unmount";
8636 * The 'recv' command is an alias for 'receive'
8638 if (strcmp(cmdname
, "recv") == 0)
8639 cmdname
= "receive";
8642 * The 'snap' command is an alias for 'snapshot'
8644 if (strcmp(cmdname
, "snap") == 0)
8645 cmdname
= "snapshot";
8650 if ((strcmp(cmdname
, "-?") == 0) ||
8651 (strcmp(cmdname
, "--help") == 0))
8655 * Special case '-V|--version'
8657 if ((strcmp(cmdname
, "-V") == 0) || (strcmp(cmdname
, "--version") == 0))
8658 return (zfs_do_version(argc
, argv
));
8660 if ((g_zfs
= libzfs_init()) == NULL
) {
8661 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
8665 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
8667 libzfs_print_on_error(g_zfs
, B_TRUE
);
8670 * Many commands modify input strings for string parsing reasons.
8671 * We create a copy to protect the original argv.
8673 newargv
= safe_malloc((argc
+ 1) * sizeof (newargv
[0]));
8674 for (i
= 0; i
< argc
; i
++)
8675 newargv
[i
] = strdup(argv
[i
]);
8676 newargv
[argc
] = NULL
;
8679 * Run the appropriate command.
8681 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
8682 if (find_command_idx(cmdname
, &i
) == 0) {
8683 current_command
= &command_table
[i
];
8684 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
8685 } else if (strchr(cmdname
, '=') != NULL
) {
8686 verify(find_command_idx("set", &i
) == 0);
8687 current_command
= &command_table
[i
];
8688 ret
= command_table
[i
].func(argc
, newargv
);
8690 (void) fprintf(stderr
, gettext("unrecognized "
8691 "command '%s'\n"), cmdname
);
8696 for (i
= 0; i
< argc
; i
++)
8700 if (ret
== 0 && log_history
)
8701 (void) zpool_log_history(g_zfs
, history_str
);
8706 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8707 * for the purposes of running ::findleaks.
8709 if (getenv("ZFS_ABORT") != NULL
) {
8710 (void) printf("dumping core by request\n");
8718 * zfs zone nsfile filesystem
8720 * Add or delete the given dataset to/from the namespace.
8724 zfs_do_zone_impl(int argc
, char **argv
, boolean_t attach
)
8730 (void) fprintf(stderr
, gettext("missing argument(s)\n"));
8734 (void) fprintf(stderr
, gettext("too many arguments\n"));
8738 zhp
= zfs_open(g_zfs
, argv
[2], ZFS_TYPE_FILESYSTEM
);
8742 ret
= (zfs_userns(zhp
, argv
[1], attach
) != 0);
8749 zfs_do_zone(int argc
, char **argv
)
8751 return (zfs_do_zone_impl(argc
, argv
, B_TRUE
));
8755 zfs_do_unzone(int argc
, char **argv
)
8757 return (zfs_do_zone_impl(argc
, argv
, B_FALSE
));
8762 #include <sys/jail.h>
8765 * Attach/detach the given dataset to/from the given jail
8768 zfs_do_jail_impl(int argc
, char **argv
, boolean_t attach
)
8773 /* check number of arguments */
8775 (void) fprintf(stderr
, gettext("missing argument(s)\n"));
8779 (void) fprintf(stderr
, gettext("too many arguments\n"));
8783 jailid
= jail_getid(argv
[1]);
8785 (void) fprintf(stderr
, gettext("invalid jail id or name\n"));
8789 zhp
= zfs_open(g_zfs
, argv
[2], ZFS_TYPE_FILESYSTEM
);
8793 ret
= (zfs_jail(zhp
, jailid
, attach
) != 0);
8800 * zfs jail jailid filesystem
8802 * Attach the given dataset to the given jail
8805 zfs_do_jail(int argc
, char **argv
)
8807 return (zfs_do_jail_impl(argc
, argv
, B_TRUE
));
8811 * zfs unjail jailid filesystem
8813 * Detach the given dataset from the given jail
8816 zfs_do_unjail(int argc
, char **argv
)
8818 return (zfs_do_jail_impl(argc
, argv
, B_FALSE
));