xref: /freebsd/sys/contrib/openzfs/cmd/zfs/zfs_main.c (revision d0b3ecdc274930e190ea233b6b69ff03782eaf8d)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25  * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
26  * Copyright 2012 Milan Jurik. All rights reserved.
27  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
28  * Copyright (c) 2013 Steven Hartland.  All rights reserved.
29  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
30  * Copyright 2016 Nexenta Systems, Inc.
31  * Copyright (c) 2019 Datto Inc.
32  * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
33  * Copyright 2019 Joyent, Inc.
34  * Copyright (c) 2019, 2020 by Christian Schwarz. All rights reserved.
35  * Copyright 2026 Oxide Computer Company
36  * Copyright (c) 2026, TrueNAS.
37  */
38 
39 #include <assert.h>
40 #include <ctype.h>
41 #include <sys/debug.h>
42 #include <dirent.h>
43 #include <errno.h>
44 #include <getopt.h>
45 #include <libgen.h>
46 #include <libintl.h>
47 #include <libnvpair.h>
48 #include <locale.h>
49 #include <stddef.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <fcntl.h>
55 #include <zone.h>
56 #include <grp.h>
57 #include <pwd.h>
58 #include <umem.h>
59 #include <pthread.h>
60 #include <signal.h>
61 #include <sys/list.h>
62 #include <sys/mkdev.h>
63 #include <sys/mntent.h>
64 #include <sys/mnttab.h>
65 #include <sys/mount.h>
66 #include <sys/stat.h>
67 #include <sys/fs/zfs.h>
68 #include <sys/systeminfo.h>
69 #include <sys/types.h>
70 #include <time.h>
71 #include <sys/zfs_project.h>
72 
73 #include <libzfs.h>
74 #include <libzfs_core.h>
75 #include <zfs_prop.h>
76 #include <zfs_deleg.h>
77 #include <libzutil.h>
78 #ifdef HAVE_IDMAP
79 #include <aclutils.h>
80 #include <directory.h>
81 #endif /* HAVE_IDMAP */
82 
83 #include "zfs_iter.h"
84 #include "zfs_util.h"
85 #include "zfs_comutil.h"
86 #include "zfs_projectutil.h"
87 
88 libzfs_handle_t *g_zfs;
89 
90 static char history_str[HIS_MAX_RECORD_LEN];
91 static boolean_t log_history = B_TRUE;
92 
93 static int zfs_do_clone(int argc, char **argv);
94 static int zfs_do_create(int argc, char **argv);
95 static int zfs_do_destroy(int argc, char **argv);
96 static int zfs_do_get(int argc, char **argv);
97 static int zfs_do_inherit(int argc, char **argv);
98 static int zfs_do_list(int argc, char **argv);
99 static int zfs_do_mount(int argc, char **argv);
100 static int zfs_do_rename(int argc, char **argv);
101 static int zfs_do_rollback(int argc, char **argv);
102 static int zfs_do_set(int argc, char **argv);
103 static int zfs_do_upgrade(int argc, char **argv);
104 static int zfs_do_snapshot(int argc, char **argv);
105 static int zfs_do_unmount(int argc, char **argv);
106 static int zfs_do_share(int argc, char **argv);
107 static int zfs_do_unshare(int argc, char **argv);
108 static int zfs_do_send(int argc, char **argv);
109 static int zfs_do_receive(int argc, char **argv);
110 static int zfs_do_promote(int argc, char **argv);
111 static int zfs_do_userspace(int argc, char **argv);
112 static int zfs_do_allow(int argc, char **argv);
113 static int zfs_do_unallow(int argc, char **argv);
114 static int zfs_do_hold(int argc, char **argv);
115 static int zfs_do_holds(int argc, char **argv);
116 static int zfs_do_release(int argc, char **argv);
117 static int zfs_do_diff(int argc, char **argv);
118 static int zfs_do_bookmark(int argc, char **argv);
119 static int zfs_do_channel_program(int argc, char **argv);
120 static int zfs_do_load_key(int argc, char **argv);
121 static int zfs_do_unload_key(int argc, char **argv);
122 static int zfs_do_change_key(int argc, char **argv);
123 static int zfs_do_project(int argc, char **argv);
124 static int zfs_do_version(int argc, char **argv);
125 static int zfs_do_redact(int argc, char **argv);
126 static int zfs_do_rewrite(int argc, char **argv);
127 static int zfs_do_wait(int argc, char **argv);
128 
129 #ifdef __FreeBSD__
130 static int zfs_do_jail(int argc, char **argv);
131 static int zfs_do_unjail(int argc, char **argv);
132 #endif
133 
134 #ifdef __linux__
135 static int zfs_do_zone(int argc, char **argv);
136 static int zfs_do_unzone(int argc, char **argv);
137 #endif
138 
139 static int zfs_do_help(int argc, char **argv);
140 
141 enum zfs_options {
142 	ZFS_OPTION_JSON_NUMS_AS_INT = 1024
143 };
144 
145 /*
146  * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
147  */
148 
149 #ifdef DEBUG
150 const char *
151 _umem_debug_init(void)
152 {
153 	return ("default,verbose"); /* $UMEM_DEBUG setting */
154 }
155 
156 const char *
157 _umem_logging_init(void)
158 {
159 	return ("fail,contents"); /* $UMEM_LOGGING setting */
160 }
161 #endif
162 
163 typedef enum {
164 	HELP_CLONE,
165 	HELP_CREATE,
166 	HELP_DESTROY,
167 	HELP_GET,
168 	HELP_INHERIT,
169 	HELP_UPGRADE,
170 	HELP_LIST,
171 	HELP_MOUNT,
172 	HELP_PROMOTE,
173 	HELP_RECEIVE,
174 	HELP_RENAME,
175 	HELP_ROLLBACK,
176 	HELP_SEND,
177 	HELP_SET,
178 	HELP_SHARE,
179 	HELP_SNAPSHOT,
180 	HELP_UNMOUNT,
181 	HELP_UNSHARE,
182 	HELP_ALLOW,
183 	HELP_UNALLOW,
184 	HELP_USERSPACE,
185 	HELP_GROUPSPACE,
186 	HELP_PROJECTSPACE,
187 	HELP_PROJECT,
188 	HELP_HOLD,
189 	HELP_HOLDS,
190 	HELP_RELEASE,
191 	HELP_DIFF,
192 	HELP_BOOKMARK,
193 	HELP_CHANNEL_PROGRAM,
194 	HELP_LOAD_KEY,
195 	HELP_UNLOAD_KEY,
196 	HELP_CHANGE_KEY,
197 	HELP_VERSION,
198 	HELP_REDACT,
199 	HELP_REWRITE,
200 	HELP_JAIL,
201 	HELP_UNJAIL,
202 	HELP_WAIT,
203 	HELP_ZONE,
204 	HELP_UNZONE,
205 } zfs_help_t;
206 
207 typedef struct zfs_command {
208 	const char	*name;
209 	int		(*func)(int argc, char **argv);
210 	zfs_help_t	usage;
211 } zfs_command_t;
212 
213 /*
214  * Master command table.  Each ZFS command has a name, associated function, and
215  * usage message.  The usage messages need to be internationalized, so we have
216  * to have a function to return the usage message based on a command index.
217  *
218  * These commands are organized according to how they are displayed in the usage
219  * message.  An empty command (one with a NULL name) indicates an empty line in
220  * the generic usage message.
221  */
222 static zfs_command_t command_table[] = {
223 	{ "version",	zfs_do_version, 	HELP_VERSION		},
224 	{ NULL },
225 	{ "create",	zfs_do_create,		HELP_CREATE		},
226 	{ "destroy",	zfs_do_destroy,		HELP_DESTROY		},
227 	{ NULL },
228 	{ "snapshot",	zfs_do_snapshot,	HELP_SNAPSHOT		},
229 	{ "rollback",	zfs_do_rollback,	HELP_ROLLBACK		},
230 	{ "clone",	zfs_do_clone,		HELP_CLONE		},
231 	{ "promote",	zfs_do_promote,		HELP_PROMOTE		},
232 	{ "rename",	zfs_do_rename,		HELP_RENAME		},
233 	{ "bookmark",	zfs_do_bookmark,	HELP_BOOKMARK		},
234 	{ "diff",	zfs_do_diff,		HELP_DIFF		},
235 	{ NULL },
236 	{ "list",	zfs_do_list,		HELP_LIST		},
237 	{ NULL },
238 	{ "set",	zfs_do_set,		HELP_SET		},
239 	{ "get",	zfs_do_get,		HELP_GET		},
240 	{ "inherit",	zfs_do_inherit,		HELP_INHERIT		},
241 	{ "upgrade",	zfs_do_upgrade,		HELP_UPGRADE		},
242 	{ NULL },
243 	{ "userspace",	zfs_do_userspace,	HELP_USERSPACE		},
244 	{ "groupspace",	zfs_do_userspace,	HELP_GROUPSPACE		},
245 	{ "projectspace", zfs_do_userspace,	HELP_PROJECTSPACE	},
246 	{ NULL },
247 	{ "project",	zfs_do_project,		HELP_PROJECT		},
248 	{ NULL },
249 	{ "mount",	zfs_do_mount,		HELP_MOUNT		},
250 	{ "unmount",	zfs_do_unmount,		HELP_UNMOUNT		},
251 	{ "share",	zfs_do_share,		HELP_SHARE		},
252 	{ "unshare",	zfs_do_unshare,		HELP_UNSHARE		},
253 	{ NULL },
254 	{ "send",	zfs_do_send,		HELP_SEND		},
255 	{ "receive",	zfs_do_receive,		HELP_RECEIVE		},
256 	{ "redact",	zfs_do_redact,		HELP_REDACT		},
257 	{ NULL },
258 	{ "allow",	zfs_do_allow,		HELP_ALLOW		},
259 	{ "unallow",	zfs_do_unallow,		HELP_UNALLOW		},
260 	{ NULL },
261 	{ "hold",	zfs_do_hold,		HELP_HOLD		},
262 	{ "holds",	zfs_do_holds,		HELP_HOLDS		},
263 	{ "release",	zfs_do_release,		HELP_RELEASE		},
264 	{ NULL },
265 	{ "load-key",	zfs_do_load_key,	HELP_LOAD_KEY		},
266 	{ "unload-key",	zfs_do_unload_key,	HELP_UNLOAD_KEY		},
267 	{ "change-key",	zfs_do_change_key,	HELP_CHANGE_KEY		},
268 	{ NULL },
269 	{ "program",	zfs_do_channel_program,	HELP_CHANNEL_PROGRAM	},
270 	{ "rewrite",	zfs_do_rewrite,		HELP_REWRITE		},
271 	{ "wait",	zfs_do_wait,		HELP_WAIT		},
272 
273 #ifdef __FreeBSD__
274 	{ NULL },
275 	{ "jail",	zfs_do_jail,		HELP_JAIL		},
276 	{ "unjail",	zfs_do_unjail,		HELP_UNJAIL		},
277 #endif
278 
279 #ifdef __linux__
280 	{ NULL },
281 	{ "zone",	zfs_do_zone,		HELP_ZONE		},
282 	{ "unzone",	zfs_do_unzone,		HELP_UNZONE		},
283 #endif
284 };
285 
286 #define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
287 
288 #define	MAX_CMD_LEN	256
289 
290 zfs_command_t *current_command;
291 
292 static const char *
293 get_usage(zfs_help_t idx)
294 {
295 	switch (idx) {
296 	case HELP_CLONE:
297 		return (gettext("\tclone [-p[p]u] [-o property=value] ... "
298 		    "<snapshot> <filesystem|volume>\n"));
299 	case HELP_CREATE:
300 		return (gettext("\tcreate [-Pnp[p]uv] [-o property=value] ... "
301 		    "<filesystem>\n"
302 		    "\tcreate [-Pnp[p]sv] [-b blocksize] "
303 		    "[-o property=value] ... "
304 		    "-V <size> <volume>\n"));
305 	case HELP_DESTROY:
306 		return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
307 		    "\tdestroy [-dnpRrv] "
308 		    "<filesystem|volume>@<snap>[%<snap>][,...]\n"
309 		    "\tdestroy <filesystem|volume>#<bookmark>\n"));
310 	case HELP_GET:
311 		return (gettext("\tget [-rHp] [-j [--json-int]] [-d max] "
312 		    "[-o \"all\" | field[,...]]\n"
313 		    "\t    [-t type[,...]] [-s source[,...]]\n"
314 		    "\t    <\"all\" | property[,...]> "
315 		    "[filesystem|volume|snapshot|bookmark] ...\n"));
316 	case HELP_INHERIT:
317 		return (gettext("\tinherit [-rS] <property> "
318 		    "<filesystem|volume|snapshot> ...\n"));
319 	case HELP_UPGRADE:
320 		return (gettext("\tupgrade [-v]\n"
321 		    "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
322 	case HELP_LIST:
323 		return (gettext("\tlist [-Hp] [-j [--json-int]] [-r|-d max] "
324 		    "[-o property[,...]] [-s property]...\n\t    "
325 		    "[-S property]... [-t type[,...]] "
326 		    "[filesystem|volume|snapshot] ...\n"));
327 	case HELP_MOUNT:
328 		return (gettext("\tmount [-j]\n"
329 		    "\tmount [-flvO] [-o opts] <-a|-R filesystem|"
330 		    "filesystem>\n"));
331 	case HELP_PROMOTE:
332 		return (gettext("\tpromote <clone-filesystem>\n"));
333 	case HELP_RECEIVE:
334 		return (gettext("\treceive [-vMnsFhu] "
335 		    "[-o <property>=<value>] ... [-x <property>] ...\n"
336 		    "\t    <filesystem|volume|snapshot>\n"
337 		    "\treceive [-vMnsFhu] [-o <property>=<value>] ... "
338 		    "[-x <property>] ... \n"
339 		    "\t    [-d | -e] <filesystem>\n"
340 		    "\treceive -A <filesystem|volume>\n"));
341 	case HELP_RENAME:
342 		return (gettext("\trename [-f] <filesystem|volume|snapshot> "
343 		    "<filesystem|volume|snapshot>\n"
344 		    "\trename -p[p] [-f] <filesystem|volume> "
345 		    "<filesystem|volume>\n"
346 		    "\trename -u [-f] <filesystem> <filesystem>\n"
347 		    "\trename -r <snapshot> <snapshot>\n"));
348 	case HELP_ROLLBACK:
349 		return (gettext("\trollback [-rRf] <snapshot>\n"));
350 	case HELP_SEND:
351 		return (gettext("\tsend [-DLPbcehnpsUVvw] "
352 		    "[-i|-I snapshot]\n"
353 		    "\t     [-R [-X dataset[,dataset]...]]     <snapshot>\n"
354 		    "\tsend [-DnVvPLecwU] [-i snapshot|bookmark] "
355 		    "<filesystem|volume|snapshot>\n"
356 		    "\tsend [-DnPpVvLec] [-i bookmark|snapshot] "
357 		    "--redact <bookmark> <snapshot>\n"
358 		    "\tsend [-nVvPe] -t <receive_resume_token>\n"
359 		    "\tsend [-PnVv] --saved filesystem\n"));
360 	case HELP_SET:
361 		return (gettext("\tset [-u] <property=value> ... "
362 		    "<filesystem|volume|snapshot> ...\n"));
363 	case HELP_SHARE:
364 		return (gettext("\tshare [-l] <-a [nfs|smb] | filesystem>\n"));
365 	case HELP_SNAPSHOT:
366 		return (gettext("\tsnapshot [-r] [-o property=value] ... "
367 		    "<filesystem|volume>@<snap> ...\n"));
368 	case HELP_UNMOUNT:
369 		return (gettext("\tunmount [-fu] "
370 		    "<-a | filesystem|mountpoint>\n"));
371 	case HELP_UNSHARE:
372 		return (gettext("\tunshare "
373 		    "<-a [nfs|smb] | filesystem|mountpoint>\n"));
374 	case HELP_ALLOW:
375 		return (gettext("\tallow <filesystem|volume>\n"
376 		    "\tallow [-ldug] "
377 		    "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
378 		    "\t    <filesystem|volume>\n"
379 		    "\tallow [-ld] -e <perm|@setname>[,...] "
380 		    "<filesystem|volume>\n"
381 		    "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
382 		    "\tallow -s @setname <perm|@setname>[,...] "
383 		    "<filesystem|volume>\n"));
384 	case HELP_UNALLOW:
385 		return (gettext("\tunallow [-rldug] "
386 		    "<\"everyone\"|user|group>[,...]\n"
387 		    "\t    [<perm|@setname>[,...]] <filesystem|volume>\n"
388 		    "\tunallow [-rld] -e [<perm|@setname>[,...]] "
389 		    "<filesystem|volume>\n"
390 		    "\tunallow [-r] -c [<perm|@setname>[,...]] "
391 		    "<filesystem|volume>\n"
392 		    "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
393 		    "<filesystem|volume>\n"));
394 	case HELP_USERSPACE:
395 		return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
396 		    "[-s field] ...\n"
397 		    "\t    [-S field] ... [-t type[,...]] "
398 		    "<filesystem|snapshot|path>\n"));
399 	case HELP_GROUPSPACE:
400 		return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
401 		    "[-s field] ...\n"
402 		    "\t    [-S field] ... [-t type[,...]] "
403 		    "<filesystem|snapshot|path>\n"));
404 	case HELP_PROJECTSPACE:
405 		return (gettext("\tprojectspace [-Hp] [-o field[,...]] "
406 		    "[-s field] ... \n"
407 		    "\t    [-S field] ... <filesystem|snapshot|path>\n"));
408 	case HELP_PROJECT:
409 		return (gettext("\tproject [-d|-r] <directory|file ...>\n"
410 		    "\tproject -c [-0] [-d|-r] [-p id] <directory|file ...>\n"
411 		    "\tproject -C [-k] [-r] <directory ...>\n"
412 		    "\tproject [-p id] [-r] [-s] <directory ...>\n"));
413 	case HELP_HOLD:
414 		return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
415 	case HELP_HOLDS:
416 		return (gettext("\tholds [-rHp] <snapshot> ...\n"));
417 	case HELP_RELEASE:
418 		return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
419 	case HELP_DIFF:
420 		return (gettext("\tdiff [-FHth] <snapshot> "
421 		    "[snapshot|filesystem]\n"));
422 	case HELP_BOOKMARK:
423 		return (gettext("\tbookmark <snapshot|bookmark> "
424 		    "<newbookmark>\n"));
425 	case HELP_CHANNEL_PROGRAM:
426 		return (gettext("\tprogram [-jn] [-t <instruction limit>] "
427 		    "[-m <memory limit (b)>]\n"
428 		    "\t    <pool> <program file> [lua args...]\n"));
429 	case HELP_LOAD_KEY:
430 		return (gettext("\tload-key [-rn] [-L <keylocation>] "
431 		    "<-a | filesystem|volume>\n"));
432 	case HELP_UNLOAD_KEY:
433 		return (gettext("\tunload-key [-r] "
434 		    "<-a | filesystem|volume>\n"));
435 	case HELP_CHANGE_KEY:
436 		return (gettext("\tchange-key [-l] [-o keyformat=<value>]\n"
437 		    "\t    [-o keylocation=<value>] [-o pbkdf2iters=<value>]\n"
438 		    "\t    <filesystem|volume>\n"
439 		    "\tchange-key -i [-l] <filesystem|volume>\n"));
440 	case HELP_VERSION:
441 		return (gettext("\tversion [-j]\n"));
442 	case HELP_REDACT:
443 		return (gettext("\tredact <snapshot> <bookmark> "
444 		    "<redaction_snapshot> ...\n"));
445 	case HELP_REWRITE:
446 		return (gettext("\trewrite [-CPSrvx] [-o <offset>] "
447 		    "[-l <length>] <directory|file ...>\n"));
448 	case HELP_JAIL:
449 		return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
450 	case HELP_UNJAIL:
451 		return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
452 	case HELP_WAIT:
453 		return (gettext("\twait [-t <activity>] <filesystem>\n"));
454 	case HELP_ZONE:
455 		return (gettext("\tzone <nsfile> <filesystem>\n"));
456 	case HELP_UNZONE:
457 		return (gettext("\tunzone <nsfile> <filesystem>\n"));
458 	default:
459 		__builtin_unreachable();
460 	}
461 }
462 
463 void
464 nomem(void)
465 {
466 	(void) fprintf(stderr, gettext("internal error: out of memory\n"));
467 	exit(1);
468 }
469 
470 /*
471  * Utility function to guarantee malloc() success.
472  */
473 
474 void *
475 safe_malloc(size_t size)
476 {
477 	void *data;
478 
479 	if ((data = calloc(1, size)) == NULL)
480 		nomem();
481 
482 	return (data);
483 }
484 
485 static void *
486 safe_realloc(void *data, size_t size)
487 {
488 	void *newp;
489 	if ((newp = realloc(data, size)) == NULL) {
490 		free(data);
491 		nomem();
492 	}
493 
494 	return (newp);
495 }
496 
497 static char *
498 safe_strdup(const char *str)
499 {
500 	char *dupstr = strdup(str);
501 
502 	if (dupstr == NULL)
503 		nomem();
504 
505 	return (dupstr);
506 }
507 
508 /*
509  * Callback routine that will print out information for each of
510  * the properties.
511  */
512 static int
513 usage_prop_cb(int prop, void *cb)
514 {
515 	FILE *fp = cb;
516 
517 	(void) fprintf(fp, "\t%-22s ", zfs_prop_to_name(prop));
518 
519 	if (zfs_prop_readonly(prop))
520 		(void) fprintf(fp, " NO    ");
521 	else
522 		(void) fprintf(fp, "YES    ");
523 
524 	if (zfs_prop_inheritable(prop))
525 		(void) fprintf(fp, "  YES   ");
526 	else
527 		(void) fprintf(fp, "   NO   ");
528 
529 	(void) fprintf(fp, "%s\n", zfs_prop_values(prop) ?: "-");
530 
531 	return (ZPROP_CONT);
532 }
533 
534 /*
535  * Display usage message.  If we're inside a command, display only the usage for
536  * that command.  Otherwise, iterate over the entire command table and display
537  * a complete usage message.
538  */
539 static __attribute__((noreturn)) void
540 usage(boolean_t requested)
541 {
542 	int i;
543 	boolean_t show_properties = B_FALSE;
544 	FILE *fp = requested ? stdout : stderr;
545 
546 	if (current_command == NULL) {
547 
548 		(void) fprintf(fp, gettext("usage: zfs command args ...\n"));
549 		(void) fprintf(fp,
550 		    gettext("where 'command' is one of the following:\n\n"));
551 
552 		for (i = 0; i < NCOMMAND; i++) {
553 			if (command_table[i].name == NULL)
554 				(void) fprintf(fp, "\n");
555 			else
556 				(void) fprintf(fp, "%s",
557 				    get_usage(command_table[i].usage));
558 		}
559 
560 		(void) fprintf(fp, gettext("\nEach dataset is of the form: "
561 		    "pool/[dataset/]*dataset[@name]\n"));
562 	} else {
563 		(void) fprintf(fp, gettext("usage:\n"));
564 		(void) fprintf(fp, "%s", get_usage(current_command->usage));
565 	}
566 
567 	if (current_command != NULL &&
568 	    (strcmp(current_command->name, "set") == 0 ||
569 	    strcmp(current_command->name, "get") == 0 ||
570 	    strcmp(current_command->name, "inherit") == 0 ||
571 	    strcmp(current_command->name, "list") == 0))
572 		show_properties = B_TRUE;
573 
574 	if (show_properties) {
575 		(void) fprintf(fp, "%s",
576 		    gettext("\nThe following properties are supported:\n"));
577 
578 		(void) fprintf(fp, "\n\t%-21s %s  %s   %s\n\n",
579 		    "PROPERTY", "EDIT", "INHERIT", "VALUES");
580 
581 		/* Iterate over all properties */
582 		(void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
583 		    ZFS_TYPE_DATASET);
584 
585 		(void) fprintf(fp, "\t%-22s ", "userused@...");
586 		(void) fprintf(fp, " NO       NO   <size>\n");
587 		(void) fprintf(fp, "\t%-22s ", "groupused@...");
588 		(void) fprintf(fp, " NO       NO   <size>\n");
589 		(void) fprintf(fp, "\t%-22s ", "projectused@...");
590 		(void) fprintf(fp, " NO       NO   <size>\n");
591 		(void) fprintf(fp, "\t%-22s ", "userobjused@...");
592 		(void) fprintf(fp, " NO       NO   <size>\n");
593 		(void) fprintf(fp, "\t%-22s ", "groupobjused@...");
594 		(void) fprintf(fp, " NO       NO   <size>\n");
595 		(void) fprintf(fp, "\t%-22s ", "projectobjused@...");
596 		(void) fprintf(fp, " NO       NO   <size>\n");
597 		(void) fprintf(fp, "\t%-22s ", "userquota@...");
598 		(void) fprintf(fp, "YES       NO   <size> | none\n");
599 		(void) fprintf(fp, "\t%-22s ", "groupquota@...");
600 		(void) fprintf(fp, "YES       NO   <size> | none\n");
601 		(void) fprintf(fp, "\t%-22s ", "projectquota@...");
602 		(void) fprintf(fp, "YES       NO   <size> | none\n");
603 		(void) fprintf(fp, "\t%-22s ", "userobjquota@...");
604 		(void) fprintf(fp, "YES       NO   <size> | none\n");
605 		(void) fprintf(fp, "\t%-22s ", "groupobjquota@...");
606 		(void) fprintf(fp, "YES       NO   <size> | none\n");
607 		(void) fprintf(fp, "\t%-22s ", "projectobjquota@...");
608 		(void) fprintf(fp, "YES       NO   <size> | none\n");
609 		(void) fprintf(fp, "\t%-22s ", "written@<snap>");
610 		(void) fprintf(fp, " NO       NO   <size>\n");
611 		(void) fprintf(fp, "\t%-22s ", "written#<bookmark>");
612 		(void) fprintf(fp, " NO       NO   <size>\n");
613 
614 		(void) fprintf(fp, gettext("\nSizes are specified in bytes "
615 		    "with standard units such as K, M, G, etc.\n"));
616 		(void) fprintf(fp, "%s", gettext("\nUser-defined properties "
617 		    "can be specified by using a name containing a colon "
618 		    "(:).\n"));
619 		(void) fprintf(fp, gettext("\nThe {user|group|project}"
620 		    "[obj]{used|quota}@ properties must be appended with\n"
621 		    "a user|group|project specifier of one of these forms:\n"
622 		    "    POSIX name      (eg: \"matt\")\n"
623 		    "    POSIX id        (eg: \"126829\")\n"
624 		    "    SMB name@domain (eg: \"matt@sun\")\n"
625 		    "    SMB SID         (eg: \"S-1-234-567-89\")\n"));
626 	} else {
627 		(void) fprintf(fp,
628 		    gettext("\nFor the property list, run: %s\n"),
629 		    "zfs set|get");
630 		(void) fprintf(fp,
631 		    gettext("\nFor the delegated permission list, run: %s\n"),
632 		    "zfs allow|unallow");
633 		(void) fprintf(fp,
634 		    gettext("\nFor further help on a command or topic, "
635 		    "run: %s\n"), "zfs help [<topic>]");
636 	}
637 
638 	/*
639 	 * See comments at end of main().
640 	 */
641 	if (getenv("ZFS_ABORT") != NULL) {
642 		(void) printf("dumping core by request\n");
643 		abort();
644 	}
645 
646 	exit(requested ? 0 : 2);
647 }
648 
649 /*
650  * Take a property=value argument string and add it to the given nvlist.
651  * Modifies the argument inplace.
652  */
653 static boolean_t
654 parseprop(nvlist_t *props, char *propname)
655 {
656 	char *propval;
657 
658 	if ((propval = strchr(propname, '=')) == NULL) {
659 		(void) fprintf(stderr, gettext("missing "
660 		    "'=' for property=value argument\n"));
661 		return (B_FALSE);
662 	}
663 	*propval = '\0';
664 	propval++;
665 	if (nvlist_exists(props, propname)) {
666 		(void) fprintf(stderr, gettext("property '%s' "
667 		    "specified multiple times\n"), propname);
668 		return (B_FALSE);
669 	}
670 	if (nvlist_add_string(props, propname, propval) != 0)
671 		nomem();
672 	return (B_TRUE);
673 }
674 
675 /*
676  * Take a property name argument and add it to the given nvlist.
677  * Modifies the argument inplace.
678  */
679 static boolean_t
680 parsepropname(nvlist_t *props, char *propname)
681 {
682 	if (strchr(propname, '=') != NULL) {
683 		(void) fprintf(stderr, gettext("invalid character "
684 		    "'=' in property argument\n"));
685 		return (B_FALSE);
686 	}
687 	if (nvlist_exists(props, propname)) {
688 		(void) fprintf(stderr, gettext("property '%s' "
689 		    "specified multiple times\n"), propname);
690 		return (B_FALSE);
691 	}
692 	if (nvlist_add_boolean(props, propname) != 0)
693 		nomem();
694 	return (B_TRUE);
695 }
696 
697 static int
698 parse_depth(char *opt, int *flags)
699 {
700 	char *tmp;
701 	int depth;
702 
703 	depth = (int)strtol(opt, &tmp, 0);
704 	if (*tmp) {
705 		(void) fprintf(stderr,
706 		    gettext("%s is not an integer\n"), optarg);
707 		usage(B_FALSE);
708 	}
709 	if (depth < 0) {
710 		(void) fprintf(stderr,
711 		    gettext("Depth can not be negative.\n"));
712 		usage(B_FALSE);
713 	}
714 	*flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
715 	return (depth);
716 }
717 
718 #define	PROGRESS_DELAY 2		/* seconds */
719 
720 static const char *pt_reverse =
721 	"\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";
722 static time_t pt_begin;
723 static char *pt_header = NULL;
724 static boolean_t pt_shown;
725 
726 static void
727 start_progress_timer(void)
728 {
729 	pt_begin = time(NULL) + PROGRESS_DELAY;
730 	pt_shown = B_FALSE;
731 }
732 
733 static void
734 set_progress_header(const char *header)
735 {
736 	assert(pt_header == NULL);
737 	pt_header = safe_strdup(header);
738 	if (pt_shown) {
739 		(void) printf("%s: ", header);
740 		(void) fflush(stdout);
741 	}
742 }
743 
744 static void
745 update_progress(const char *update)
746 {
747 	if (!pt_shown && time(NULL) > pt_begin) {
748 		int len = strlen(update);
749 
750 		(void) printf("%s: %s%*.*s", pt_header, update, len, len,
751 		    pt_reverse);
752 		(void) fflush(stdout);
753 		pt_shown = B_TRUE;
754 	} else if (pt_shown) {
755 		int len = strlen(update);
756 
757 		(void) printf("%s%*.*s", update, len, len, pt_reverse);
758 		(void) fflush(stdout);
759 	}
760 }
761 
762 static void
763 finish_progress(const char *done)
764 {
765 	if (pt_shown) {
766 		(void) puts(done);
767 		(void) fflush(stdout);
768 	}
769 	free(pt_header);
770 	pt_header = NULL;
771 }
772 
773 static void
774 makeprops_parents(nvlist_t **ptr, boolean_t parents_nomount)
775 {
776 	nvlist_t *props = NULL;
777 
778 	if (parents_nomount) {
779 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
780 			nomem();
781 
782 		if (nvlist_add_string(props,
783 		    zfs_prop_to_name(ZFS_PROP_CANMOUNT),
784 		    "off") != 0) {
785 			nvlist_free(props);
786 			nomem();
787 		}
788 	}
789 
790 	*ptr = props;
791 }
792 
793 static int
794 zfs_mount_and_share(libzfs_handle_t *hdl, const char *dataset, zfs_type_t type)
795 {
796 	zfs_handle_t *zhp = NULL;
797 	int ret = 0;
798 
799 	zhp = zfs_open(hdl, dataset, type);
800 	if (zhp == NULL)
801 		return (1);
802 
803 	/*
804 	 * Volumes may neither be mounted or shared.  Potentially in the
805 	 * future filesystems detected on these volumes could be mounted.
806 	 */
807 	if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
808 		zfs_close(zhp);
809 		return (0);
810 	}
811 
812 	/*
813 	 * Mount and/or share the new filesystem as appropriate.  We provide a
814 	 * verbose error message to let the user know that their filesystem was
815 	 * in fact created, even if we failed to mount or share it.
816 	 *
817 	 * If the user doesn't want the dataset automatically mounted, then
818 	 * skip the mount/share step
819 	 */
820 	if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type, B_FALSE) &&
821 	    zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON) {
822 		if (zfs_mount_delegation_check()) {
823 			(void) fprintf(stderr, gettext("filesystem "
824 			    "successfully created, but it may only be "
825 			    "mounted by root\n"));
826 			ret = 1;
827 		} else if (zfs_mount(zhp, NULL, 0) != 0) {
828 			(void) fprintf(stderr, gettext("filesystem "
829 			    "successfully created, but not mounted\n"));
830 			ret = 1;
831 		} else if (zfs_share(zhp, NULL) != 0) {
832 			(void) fprintf(stderr, gettext("filesystem "
833 			    "successfully created, but not shared\n"));
834 			ret = 1;
835 		}
836 		zfs_commit_shares(NULL);
837 	}
838 
839 	zfs_close(zhp);
840 
841 	return (ret);
842 }
843 
844 /*
845  * zfs clone [-pu] [-o prop=value] ... <snap> <fs | vol>
846  *
847  * Given an existing dataset, create a writable copy whose initial contents
848  * are the same as the source.  The newly created dataset maintains a
849  * dependency on the original; the original cannot be destroyed so long as
850  * the clone exists.
851  *
852  * The '-p' flag creates all the non-existing ancestors of the target first.
853  * If repeated twice, the ancestors are created with `canmount=off`.
854  *
855  * The '-u' flag prevents the newly created file system from being mounted.
856  */
857 static int
858 zfs_do_clone(int argc, char **argv)
859 {
860 	zfs_handle_t *zhp = NULL;
861 	boolean_t parents = B_FALSE;
862 	boolean_t parents_nomount = B_FALSE;
863 	boolean_t nomount = B_FALSE;
864 	nvlist_t *props;
865 	nvlist_t *props_parents = NULL;
866 	int ret = 1;
867 	int c;
868 
869 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
870 		nomem();
871 
872 	/* check options */
873 	while ((c = getopt(argc, argv, "o:pu")) != -1) {
874 		switch (c) {
875 		case 'o':
876 			if (!parseprop(props, optarg)) {
877 				nvlist_free(props);
878 				return (1);
879 			}
880 			break;
881 		case 'p':
882 			if (!parents)
883 				parents = B_TRUE;
884 			else
885 				parents_nomount = B_TRUE;
886 			break;
887 		case 'u':
888 			nomount = B_TRUE;
889 			break;
890 		case '?':
891 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
892 			    optopt);
893 			goto usage;
894 		}
895 	}
896 
897 	argc -= optind;
898 	argv += optind;
899 
900 	/* check number of arguments */
901 	if (argc < 1) {
902 		(void) fprintf(stderr, gettext("missing source dataset "
903 		    "argument\n"));
904 		goto usage;
905 	}
906 	if (argc < 2) {
907 		(void) fprintf(stderr, gettext("missing target dataset "
908 		    "argument\n"));
909 		goto usage;
910 	}
911 	if (argc > 2) {
912 		(void) fprintf(stderr, gettext("too many arguments\n"));
913 		goto usage;
914 	}
915 
916 	/* open the source dataset */
917 	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) {
918 		goto error_open;
919 	}
920 
921 	if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
922 	    ZFS_TYPE_VOLUME)) {
923 		/*
924 		 * Now create the ancestors of the target dataset.  If the
925 		 * target already exists and '-p' option was used we should not
926 		 * complain.
927 		 */
928 		if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
929 		    ZFS_TYPE_VOLUME)) {
930 			ret = 0;
931 			goto error;
932 		}
933 
934 		makeprops_parents(&props_parents, parents_nomount);
935 		if (zfs_create_ancestors_props(g_zfs, argv[1],
936 		    props_parents) != 0)
937 			goto error;
938 	}
939 
940 	/* pass to libzfs */
941 	ret = zfs_clone(zhp, argv[1], props);
942 
943 	if (ret != 0)
944 		goto error;
945 
946 	/* create the mountpoint if necessary */
947 	if (log_history) {
948 		(void) zpool_log_history(g_zfs, history_str);
949 		log_history = B_FALSE;
950 	}
951 
952 	if (nomount)
953 		goto error;
954 
955 	/*
956 	 * Dataset cloned successfully, mount/share failures are
957 	 * non-fatal.
958 	 */
959 	(void) zfs_mount_and_share(g_zfs, argv[1], ZFS_TYPE_DATASET);
960 
961 error:
962 	zfs_close(zhp);
963 error_open:
964 	nvlist_free(props);
965 	nvlist_free(props_parents);
966 	return (!!ret);
967 
968 usage:
969 	ASSERT0P(zhp);
970 	nvlist_free(props);
971 	nvlist_free(props_parents);
972 	usage(B_FALSE);
973 	return (-1);
974 }
975 
976 /*
977  * Calculate the minimum allocation size based on the top-level vdevs.
978  */
979 static uint64_t
980 calculate_volblocksize(nvlist_t *config)
981 {
982 	uint64_t asize = SPA_MINBLOCKSIZE;
983 	nvlist_t *tree, **vdevs;
984 	uint_t nvdevs;
985 
986 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) != 0 ||
987 	    nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN,
988 	    &vdevs, &nvdevs) != 0) {
989 		return (ZVOL_DEFAULT_BLOCKSIZE);
990 	}
991 
992 	for (int i = 0; i < nvdevs; i++) {
993 		nvlist_t *nv = vdevs[i];
994 		uint64_t ashift, ndata, nparity;
995 
996 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &ashift) != 0)
997 			continue;
998 
999 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DRAID_NDATA,
1000 		    &ndata) == 0) {
1001 			/* dRAID minimum allocation width */
1002 			asize = MAX(asize, ndata * (1ULL << ashift));
1003 		} else if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
1004 		    &nparity) == 0) {
1005 			/* raidz minimum allocation width */
1006 			if (nparity == 1)
1007 				asize = MAX(asize, 2 * (1ULL << ashift));
1008 			else
1009 				asize = MAX(asize, 4 * (1ULL << ashift));
1010 		} else {
1011 			/* mirror or (non-redundant) leaf vdev */
1012 			asize = MAX(asize, 1ULL << ashift);
1013 		}
1014 	}
1015 
1016 	return (asize);
1017 }
1018 
1019 /*
1020  * Return a default volblocksize for the pool which always uses more than
1021  * half of the data sectors.  This primarily applies to dRAID which always
1022  * writes full stripe widths.
1023  */
1024 static uint64_t
1025 default_volblocksize(zpool_handle_t *zhp, nvlist_t *props)
1026 {
1027 	uint64_t volblocksize, asize = SPA_MINBLOCKSIZE;
1028 
1029 	nvlist_t *config = zpool_get_config(zhp, NULL);
1030 
1031 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_MAX_ALLOC, &asize) != 0)
1032 		asize = calculate_volblocksize(config);
1033 
1034 	/*
1035 	 * Calculate the target volblocksize such that more than half
1036 	 * of the asize is used. The following table is for 4k sectors.
1037 	 *
1038 	 * n   asize   blksz  used  |   n   asize   blksz  used
1039 	 * -------------------------+---------------------------------
1040 	 * 1   4,096   8,192  100%  |   9  36,864  32,768   88%
1041 	 * 2   8,192   8,192  100%  |  10  40,960  32,768   80%
1042 	 * 3  12,288   8,192   66%  |  11  45,056  32,768   72%
1043 	 * 4  16,384  16,384  100%  |  12  49,152  32,768   66%
1044 	 * 5  20,480  16,384   80%  |  13  53,248  32,768   61%
1045 	 * 6  24,576  16,384   66%  |  14  57,344  32,768   57%
1046 	 * 7  28,672  16,384   57%  |  15  61,440  32,768   53%
1047 	 * 8  32,768  32,768  100%  |  16  65,536  65,636  100%
1048 	 *
1049 	 * This is primarily a concern for dRAID which always allocates
1050 	 * a full stripe width.  For dRAID the default stripe width is
1051 	 * n=8 in which case the volblocksize is set to 32k. Ignoring
1052 	 * compression there are no unused sectors.  This same reasoning
1053 	 * applies to raidz[2,3] so target 4 sectors to minimize waste.
1054 	 */
1055 	uint64_t tgt_volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
1056 	while (tgt_volblocksize * 2 <= asize)
1057 		tgt_volblocksize *= 2;
1058 
1059 	const char *prop = zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE);
1060 	if (nvlist_lookup_uint64(props, prop, &volblocksize) == 0) {
1061 
1062 		/* Issue a warning when a non-optimal size is requested. */
1063 		if (volblocksize < ZVOL_DEFAULT_BLOCKSIZE) {
1064 			(void) fprintf(stderr, gettext("Warning: "
1065 			    "volblocksize (%llu) is less than the default "
1066 			    "minimum block size (%llu).\nTo reduce wasted "
1067 			    "space a volblocksize of %llu is recommended.\n"),
1068 			    (u_longlong_t)volblocksize,
1069 			    (u_longlong_t)ZVOL_DEFAULT_BLOCKSIZE,
1070 			    (u_longlong_t)tgt_volblocksize);
1071 		} else if (volblocksize < tgt_volblocksize) {
1072 			(void) fprintf(stderr, gettext("Warning: "
1073 			    "volblocksize (%llu) is much less than the "
1074 			    "minimum allocation\nunit (%llu), which wastes "
1075 			    "at least %llu%% of space. To reduce wasted "
1076 			    "space,\nuse a larger volblocksize (%llu is "
1077 			    "recommended), fewer dRAID data disks\n"
1078 			    "per group, or smaller sector size (ashift).\n"),
1079 			    (u_longlong_t)volblocksize, (u_longlong_t)asize,
1080 			    (u_longlong_t)((100 * (asize - volblocksize)) /
1081 			    asize), (u_longlong_t)tgt_volblocksize);
1082 		}
1083 	} else {
1084 		volblocksize = tgt_volblocksize;
1085 		fnvlist_add_uint64(props, prop, volblocksize);
1086 	}
1087 
1088 	return (volblocksize);
1089 }
1090 
1091 /*
1092  * zfs create [-Pnpuv] [-o prop=value] ... fs
1093  * zfs create [-Pnpsv] [-b blocksize] [-o prop=value] ... -V vol size
1094  *
1095  * Create a new dataset.  This command can be used to create filesystems
1096  * and volumes.  Snapshot creation is handled by 'zfs snapshot'.
1097  * For volumes, the user must specify a size to be used.
1098  *
1099  * The '-s' flag applies only to volumes, and indicates that we should not try
1100  * to set the reservation for this volume.  By default we set a reservation
1101  * equal to the size for any volume.  For pools with SPA_VERSION >=
1102  * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
1103  *
1104  * The '-p' flag creates all the non-existing ancestors of the target first.
1105  * If repeated twice, the ancestors are created with `canmount=off`.
1106  *
1107  * The '-n' flag is no-op (dry run) mode.  This will perform a user-space sanity
1108  * check of arguments and properties, but does not check for permissions,
1109  * available space, etc.
1110  *
1111  * The '-u' flag prevents the newly created file system from being mounted.
1112  *
1113  * The '-v' flag is for verbose output.
1114  *
1115  * The '-P' flag is used for parseable output.  It implies '-v'.
1116  */
1117 static int
1118 zfs_do_create(int argc, char **argv)
1119 {
1120 	zfs_type_t type = ZFS_TYPE_FILESYSTEM;
1121 	zpool_handle_t *zpool_handle = NULL;
1122 	nvlist_t *real_props = NULL;
1123 	uint64_t volsize = 0;
1124 	int c;
1125 	boolean_t noreserve = B_FALSE;
1126 	boolean_t bflag = B_FALSE;
1127 	boolean_t parents = B_FALSE;
1128 	boolean_t parents_nomount = B_FALSE;
1129 	boolean_t dryrun = B_FALSE;
1130 	boolean_t nomount = B_FALSE;
1131 	boolean_t verbose = B_FALSE;
1132 	boolean_t parseable = B_FALSE;
1133 	int ret = 1;
1134 	nvlist_t *props;
1135 	nvlist_t *props_parents = NULL;
1136 	uint64_t intval;
1137 	const char *strval;
1138 
1139 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
1140 		nomem();
1141 
1142 	/* check options */
1143 	while ((c = getopt(argc, argv, ":PV:b:nso:puv")) != -1) {
1144 		switch (c) {
1145 		case 'V':
1146 			type = ZFS_TYPE_VOLUME;
1147 			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
1148 				(void) fprintf(stderr, gettext("bad volume "
1149 				    "size '%s': %s\n"), optarg,
1150 				    libzfs_error_description(g_zfs));
1151 				goto error;
1152 			}
1153 
1154 			if (nvlist_add_uint64(props,
1155 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
1156 				nomem();
1157 			volsize = intval;
1158 			break;
1159 		case 'P':
1160 			verbose = B_TRUE;
1161 			parseable = B_TRUE;
1162 			break;
1163 		case 'p':
1164 			if (!parents)
1165 				parents = B_TRUE;
1166 			else
1167 				parents_nomount = B_TRUE;
1168 			break;
1169 		case 'b':
1170 			bflag = B_TRUE;
1171 			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
1172 				(void) fprintf(stderr, gettext("bad volume "
1173 				    "block size '%s': %s\n"), optarg,
1174 				    libzfs_error_description(g_zfs));
1175 				goto error;
1176 			}
1177 
1178 			if (nvlist_add_uint64(props,
1179 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1180 			    intval) != 0)
1181 				nomem();
1182 			break;
1183 		case 'n':
1184 			dryrun = B_TRUE;
1185 			break;
1186 		case 'o':
1187 			if (!parseprop(props, optarg))
1188 				goto error;
1189 			break;
1190 		case 's':
1191 			noreserve = B_TRUE;
1192 			break;
1193 		case 'u':
1194 			nomount = B_TRUE;
1195 			break;
1196 		case 'v':
1197 			verbose = B_TRUE;
1198 			break;
1199 		case ':':
1200 			(void) fprintf(stderr, gettext("missing size "
1201 			    "argument\n"));
1202 			goto badusage;
1203 		case '?':
1204 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1205 			    optopt);
1206 			goto badusage;
1207 		}
1208 	}
1209 
1210 	if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
1211 		(void) fprintf(stderr, gettext("'-s' and '-b' can only be "
1212 		    "used when creating a volume\n"));
1213 		goto badusage;
1214 	}
1215 	if (nomount && type != ZFS_TYPE_FILESYSTEM) {
1216 		(void) fprintf(stderr, gettext("'-u' can only be "
1217 		    "used when creating a filesystem\n"));
1218 		goto badusage;
1219 	}
1220 
1221 	argc -= optind;
1222 	argv += optind;
1223 
1224 	/* check number of arguments */
1225 	if (argc == 0) {
1226 		(void) fprintf(stderr, gettext("missing %s argument\n"),
1227 		    zfs_type_to_name(type));
1228 		goto badusage;
1229 	}
1230 	if (argc > 1) {
1231 		(void) fprintf(stderr, gettext("too many arguments\n"));
1232 		goto badusage;
1233 	}
1234 
1235 	if (dryrun || type == ZFS_TYPE_VOLUME) {
1236 		char msg[ZFS_MAX_DATASET_NAME_LEN * 2];
1237 		char *p;
1238 
1239 		if ((p = strchr(argv[0], '/')) != NULL)
1240 			*p = '\0';
1241 		zpool_handle = zpool_open(g_zfs, argv[0]);
1242 		if (p != NULL)
1243 			*p = '/';
1244 		if (zpool_handle == NULL)
1245 			goto error;
1246 
1247 		(void) snprintf(msg, sizeof (msg),
1248 		    dryrun ? gettext("cannot verify '%s'") :
1249 		    gettext("cannot create '%s'"), argv[0]);
1250 		if (props && (real_props = zfs_valid_proplist(g_zfs, type,
1251 		    props, 0, NULL, zpool_handle, B_TRUE, msg)) == NULL) {
1252 			zpool_close(zpool_handle);
1253 			goto error;
1254 		}
1255 	}
1256 
1257 	if (type == ZFS_TYPE_VOLUME) {
1258 		const char *prop = zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE);
1259 		uint64_t volblocksize = default_volblocksize(zpool_handle,
1260 		    real_props);
1261 
1262 		if (volblocksize != ZVOL_DEFAULT_BLOCKSIZE &&
1263 		    nvlist_lookup_string(props, prop, &strval) != 0) {
1264 			char *tmp;
1265 			if (asprintf(&tmp, "%llu",
1266 			    (u_longlong_t)volblocksize) == -1)
1267 				nomem();
1268 			nvlist_add_string(props, prop, tmp);
1269 			free(tmp);
1270 		}
1271 
1272 		/*
1273 		 * If volsize is not a multiple of volblocksize, round it
1274 		 * up to the nearest multiple of the volblocksize.
1275 		 */
1276 		if (volsize % volblocksize) {
1277 			volsize = P2ROUNDUP_TYPED(volsize, volblocksize,
1278 			    uint64_t);
1279 
1280 			if (nvlist_add_uint64(props,
1281 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE), volsize) != 0) {
1282 				nvlist_free(props);
1283 				nomem();
1284 			}
1285 		}
1286 	}
1287 
1288 	if (type == ZFS_TYPE_VOLUME && !noreserve) {
1289 		uint64_t spa_version;
1290 		zfs_prop_t resv_prop;
1291 
1292 		spa_version = zpool_get_prop_int(zpool_handle,
1293 		    ZPOOL_PROP_VERSION, NULL);
1294 		if (spa_version >= SPA_VERSION_REFRESERVATION)
1295 			resv_prop = ZFS_PROP_REFRESERVATION;
1296 		else
1297 			resv_prop = ZFS_PROP_RESERVATION;
1298 
1299 		volsize = zvol_volsize_to_reservation(zpool_handle, volsize,
1300 		    real_props);
1301 
1302 		if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
1303 		    &strval) != 0) {
1304 			if (nvlist_add_uint64(props,
1305 			    zfs_prop_to_name(resv_prop), volsize) != 0) {
1306 				nvlist_free(props);
1307 				nomem();
1308 			}
1309 		}
1310 	}
1311 	if (zpool_handle != NULL) {
1312 		zpool_close(zpool_handle);
1313 		nvlist_free(real_props);
1314 	}
1315 
1316 	if (parents && zfs_name_valid(argv[0], type)) {
1317 		makeprops_parents(&props_parents, parents_nomount);
1318 
1319 		/*
1320 		 * Now create the ancestors of target dataset.  If the target
1321 		 * already exists and '-p' option was used we should not
1322 		 * complain.
1323 		 */
1324 		if (zfs_dataset_exists(g_zfs, argv[0], type)) {
1325 			ret = 0;
1326 			goto error;
1327 		}
1328 		if (verbose) {
1329 			(void) printf(parseable ? "create_ancestors\t%s\n" :
1330 			    dryrun ?  "would create ancestors of %s\n" :
1331 			    "create ancestors of %s\n", argv[0]);
1332 		}
1333 		if (!dryrun) {
1334 			if (zfs_create_ancestors_props(g_zfs, argv[0],
1335 			    props_parents) != 0) {
1336 				goto error;
1337 			}
1338 		}
1339 	}
1340 
1341 	if (verbose) {
1342 		nvpair_t *nvp = NULL;
1343 		(void) printf(parseable ? "create\t%s\n" :
1344 		    dryrun ? "would create %s\n" : "create %s\n", argv[0]);
1345 		while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) {
1346 			uint64_t uval;
1347 			const char *sval;
1348 
1349 			switch (nvpair_type(nvp)) {
1350 			case DATA_TYPE_UINT64:
1351 				VERIFY0(nvpair_value_uint64(nvp, &uval));
1352 				(void) printf(parseable ?
1353 				    "property\t%s\t%llu\n" : "\t%s=%llu\n",
1354 				    nvpair_name(nvp), (u_longlong_t)uval);
1355 				break;
1356 			case DATA_TYPE_STRING:
1357 				VERIFY0(nvpair_value_string(nvp, &sval));
1358 				(void) printf(parseable ?
1359 				    "property\t%s\t%s\n" : "\t%s=%s\n",
1360 				    nvpair_name(nvp), sval);
1361 				break;
1362 			default:
1363 				(void) fprintf(stderr, "property '%s' "
1364 				    "has illegal type %d\n",
1365 				    nvpair_name(nvp), nvpair_type(nvp));
1366 				abort();
1367 			}
1368 		}
1369 	}
1370 	if (dryrun) {
1371 		ret = 0;
1372 		goto error;
1373 	}
1374 
1375 	/* pass to libzfs */
1376 	if (zfs_create(g_zfs, argv[0], type, props) != 0)
1377 		goto error;
1378 
1379 	if (log_history) {
1380 		(void) zpool_log_history(g_zfs, history_str);
1381 		log_history = B_FALSE;
1382 	}
1383 
1384 	if (nomount) {
1385 		ret = 0;
1386 		goto error;
1387 	}
1388 
1389 	/* Dataset created successfully, mount/share failures are non-fatal */
1390 	ret = 0;
1391 	(void) zfs_mount_and_share(g_zfs, argv[0], ZFS_TYPE_DATASET);
1392 error:
1393 	nvlist_free(props);
1394 	nvlist_free(props_parents);
1395 	return (ret);
1396 badusage:
1397 	nvlist_free(props);
1398 	nvlist_free(props_parents);
1399 	usage(B_FALSE);
1400 	return (2);
1401 }
1402 
1403 /*
1404  * zfs destroy [-rRf] <fs, vol>
1405  * zfs destroy [-rRd] <snap>
1406  *
1407  *	-r	Recursively destroy all children
1408  *	-R	Recursively destroy all dependents, including clones
1409  *	-f	Force unmounting of any dependents
1410  *	-d	If we can't destroy now, mark for deferred destruction
1411  *
1412  * Destroys the given dataset.  By default, it will unmount any filesystems,
1413  * and refuse to destroy a dataset that has any dependents.  A dependent can
1414  * either be a child, or a clone of a child.
1415  */
1416 typedef struct destroy_cbdata {
1417 	boolean_t	cb_first;
1418 	boolean_t	cb_force;
1419 	boolean_t	cb_recurse;
1420 	boolean_t	cb_error;
1421 	boolean_t	cb_doclones;
1422 	zfs_handle_t	*cb_target;
1423 	boolean_t	cb_defer_destroy;
1424 	boolean_t	cb_verbose;
1425 	boolean_t	cb_parsable;
1426 	boolean_t	cb_dryrun;
1427 	nvlist_t	*cb_nvl;
1428 	nvlist_t	*cb_batchedsnaps;
1429 
1430 	/* first snap in contiguous run */
1431 	char		*cb_firstsnap;
1432 	/* previous snap in contiguous run */
1433 	char		*cb_prevsnap;
1434 	int64_t		cb_snapused;
1435 	char		*cb_snapspec;
1436 	char		*cb_bookmark;
1437 	uint64_t	cb_snap_count;
1438 } destroy_cbdata_t;
1439 
1440 /*
1441  * Check for any dependents based on the '-r' or '-R' flags.
1442  */
1443 static int
1444 destroy_check_dependent(zfs_handle_t *zhp, void *data)
1445 {
1446 	destroy_cbdata_t *cbp = data;
1447 	const char *tname = zfs_get_name(cbp->cb_target);
1448 	const char *name = zfs_get_name(zhp);
1449 
1450 	if (strncmp(tname, name, strlen(tname)) == 0 &&
1451 	    (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
1452 		/*
1453 		 * This is a direct descendant, not a clone somewhere else in
1454 		 * the hierarchy.
1455 		 */
1456 		if (cbp->cb_recurse)
1457 			goto out;
1458 
1459 		if (cbp->cb_first) {
1460 			(void) fprintf(stderr, gettext("cannot destroy '%s': "
1461 			    "%s has children\n"),
1462 			    zfs_get_name(cbp->cb_target),
1463 			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1464 			(void) fprintf(stderr, gettext("use '-r' to destroy "
1465 			    "the following datasets:\n"));
1466 			cbp->cb_first = B_FALSE;
1467 			cbp->cb_error = B_TRUE;
1468 		}
1469 
1470 		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1471 	} else {
1472 		/*
1473 		 * This is a clone.  We only want to report this if the '-r'
1474 		 * wasn't specified, or the target is a snapshot.
1475 		 */
1476 		if (!cbp->cb_recurse &&
1477 		    zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1478 			goto out;
1479 
1480 		if (cbp->cb_first) {
1481 			(void) fprintf(stderr, gettext("cannot destroy '%s': "
1482 			    "%s has dependent clones\n"),
1483 			    zfs_get_name(cbp->cb_target),
1484 			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1485 			(void) fprintf(stderr, gettext("use '-R' to destroy "
1486 			    "the following datasets:\n"));
1487 			cbp->cb_first = B_FALSE;
1488 			cbp->cb_error = B_TRUE;
1489 			cbp->cb_dryrun = B_TRUE;
1490 		}
1491 
1492 		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1493 	}
1494 
1495 out:
1496 	zfs_close(zhp);
1497 	return (0);
1498 }
1499 
1500 static int
1501 destroy_batched(destroy_cbdata_t *cb)
1502 {
1503 	int error = zfs_destroy_snaps_nvl(g_zfs,
1504 	    cb->cb_batchedsnaps, B_FALSE);
1505 	fnvlist_free(cb->cb_batchedsnaps);
1506 	cb->cb_batchedsnaps = fnvlist_alloc();
1507 	return (error);
1508 }
1509 
1510 static int
1511 destroy_callback(zfs_handle_t *zhp, void *data)
1512 {
1513 	destroy_cbdata_t *cb = data;
1514 	const char *name = zfs_get_name(zhp);
1515 	int error;
1516 
1517 	if (cb->cb_verbose) {
1518 		if (cb->cb_parsable) {
1519 			(void) printf("destroy\t%s\n", name);
1520 		} else if (cb->cb_dryrun) {
1521 			(void) printf(gettext("would destroy %s\n"),
1522 			    name);
1523 		} else {
1524 			(void) printf(gettext("will destroy %s\n"),
1525 			    name);
1526 		}
1527 	}
1528 
1529 	/*
1530 	 * Ignore pools (which we've already flagged as an error before getting
1531 	 * here).
1532 	 */
1533 	if (strchr(zfs_get_name(zhp), '/') == NULL &&
1534 	    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1535 		zfs_close(zhp);
1536 		return (0);
1537 	}
1538 	if (cb->cb_dryrun) {
1539 		zfs_close(zhp);
1540 		return (0);
1541 	}
1542 
1543 	/*
1544 	 * We batch up all contiguous snapshots (even of different
1545 	 * filesystems) and destroy them with one ioctl.  We can't
1546 	 * simply do all snap deletions and then all fs deletions,
1547 	 * because we must delete a clone before its origin.
1548 	 */
1549 	if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1550 		cb->cb_snap_count++;
1551 		fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1552 		if (cb->cb_snap_count % 10 == 0 && cb->cb_defer_destroy) {
1553 			error = destroy_batched(cb);
1554 			if (error != 0) {
1555 				zfs_close(zhp);
1556 				return (-1);
1557 			}
1558 		}
1559 	} else {
1560 		error = destroy_batched(cb);
1561 		if (error != 0 ||
1562 		    zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1563 		    zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1564 			zfs_close(zhp);
1565 			/*
1566 			 * When performing a recursive destroy we ignore errors
1567 			 * so that the recursive destroy could continue
1568 			 * destroying past problem datasets
1569 			 */
1570 			if (cb->cb_recurse) {
1571 				cb->cb_error = B_TRUE;
1572 				return (0);
1573 			}
1574 			return (-1);
1575 		}
1576 	}
1577 
1578 	zfs_close(zhp);
1579 	return (0);
1580 }
1581 
1582 static int
1583 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1584 {
1585 	destroy_cbdata_t *cb = arg;
1586 	const char *name = zfs_get_name(zhp);
1587 	int err = 0;
1588 
1589 	if (nvlist_exists(cb->cb_nvl, name)) {
1590 		if (cb->cb_firstsnap == NULL)
1591 			cb->cb_firstsnap = strdup(name);
1592 		if (cb->cb_prevsnap != NULL)
1593 			free(cb->cb_prevsnap);
1594 		/* this snap continues the current range */
1595 		cb->cb_prevsnap = strdup(name);
1596 		if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1597 			nomem();
1598 		if (cb->cb_verbose) {
1599 			if (cb->cb_parsable) {
1600 				(void) printf("destroy\t%s\n", name);
1601 			} else if (cb->cb_dryrun) {
1602 				(void) printf(gettext("would destroy %s\n"),
1603 				    name);
1604 			} else {
1605 				(void) printf(gettext("will destroy %s\n"),
1606 				    name);
1607 			}
1608 		}
1609 	} else if (cb->cb_firstsnap != NULL) {
1610 		/* end of this range */
1611 		uint64_t used = 0;
1612 		err = lzc_snaprange_space(cb->cb_firstsnap,
1613 		    cb->cb_prevsnap, &used);
1614 		cb->cb_snapused += used;
1615 		free(cb->cb_firstsnap);
1616 		cb->cb_firstsnap = NULL;
1617 		free(cb->cb_prevsnap);
1618 		cb->cb_prevsnap = NULL;
1619 	}
1620 	zfs_close(zhp);
1621 	return (err);
1622 }
1623 
1624 static int
1625 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1626 {
1627 	int err;
1628 	assert(cb->cb_firstsnap == NULL);
1629 	assert(cb->cb_prevsnap == NULL);
1630 	err = zfs_iter_snapshots_sorted_v2(fs_zhp, 0, destroy_print_cb, cb, 0,
1631 	    0);
1632 	if (cb->cb_firstsnap != NULL) {
1633 		uint64_t used = 0;
1634 		if (err == 0) {
1635 			err = lzc_snaprange_space(cb->cb_firstsnap,
1636 			    cb->cb_prevsnap, &used);
1637 		}
1638 		cb->cb_snapused += used;
1639 		free(cb->cb_firstsnap);
1640 		cb->cb_firstsnap = NULL;
1641 		free(cb->cb_prevsnap);
1642 		cb->cb_prevsnap = NULL;
1643 	}
1644 	return (err);
1645 }
1646 
1647 static int
1648 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1649 {
1650 	destroy_cbdata_t *cb = arg;
1651 	int err = 0;
1652 
1653 	/* Check for clones. */
1654 	if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1655 		cb->cb_target = zhp;
1656 		cb->cb_first = B_TRUE;
1657 		err = zfs_iter_dependents_v2(zhp, 0, B_TRUE,
1658 		    destroy_check_dependent, cb);
1659 	}
1660 
1661 	if (err == 0) {
1662 		if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1663 			nomem();
1664 	}
1665 	zfs_close(zhp);
1666 	return (err);
1667 }
1668 
1669 static int
1670 gather_snapshots(zfs_handle_t *zhp, void *arg)
1671 {
1672 	destroy_cbdata_t *cb = arg;
1673 	int err = 0;
1674 
1675 	err = zfs_iter_snapspec_v2(zhp, 0, cb->cb_snapspec,
1676 	    snapshot_to_nvl_cb, cb);
1677 	if (err == ENOENT)
1678 		err = 0;
1679 	if (err != 0)
1680 		goto out;
1681 
1682 	if (cb->cb_verbose) {
1683 		err = destroy_print_snapshots(zhp, cb);
1684 		if (err != 0)
1685 			goto out;
1686 	}
1687 
1688 	if (cb->cb_recurse)
1689 		err = zfs_iter_filesystems_v2(zhp, 0, gather_snapshots, cb);
1690 
1691 out:
1692 	zfs_close(zhp);
1693 	return (err);
1694 }
1695 
1696 static int
1697 destroy_clones(destroy_cbdata_t *cb)
1698 {
1699 	nvpair_t *pair;
1700 	for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1701 	    pair != NULL;
1702 	    pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1703 		zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1704 		    ZFS_TYPE_SNAPSHOT);
1705 		if (zhp != NULL) {
1706 			boolean_t defer = cb->cb_defer_destroy;
1707 			int err;
1708 
1709 			/*
1710 			 * We can't defer destroy non-snapshots, so set it to
1711 			 * false while destroying the clones.
1712 			 */
1713 			cb->cb_defer_destroy = B_FALSE;
1714 			err = zfs_iter_dependents_v2(zhp, 0, B_FALSE,
1715 			    destroy_callback, cb);
1716 			cb->cb_defer_destroy = defer;
1717 			zfs_close(zhp);
1718 			if (err != 0)
1719 				return (err);
1720 		}
1721 	}
1722 	return (0);
1723 }
1724 
1725 static int
1726 zfs_do_destroy(int argc, char **argv)
1727 {
1728 	destroy_cbdata_t cb = { 0 };
1729 	int rv = 0;
1730 	int err = 0;
1731 	int c;
1732 	zfs_handle_t *zhp = NULL;
1733 	char *at, *pound;
1734 	zfs_type_t type = ZFS_TYPE_DATASET;
1735 
1736 	/* check options */
1737 	while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1738 		switch (c) {
1739 		case 'v':
1740 			cb.cb_verbose = B_TRUE;
1741 			break;
1742 		case 'p':
1743 			cb.cb_verbose = B_TRUE;
1744 			cb.cb_parsable = B_TRUE;
1745 			break;
1746 		case 'n':
1747 			cb.cb_dryrun = B_TRUE;
1748 			break;
1749 		case 'd':
1750 			cb.cb_defer_destroy = B_TRUE;
1751 			type = ZFS_TYPE_SNAPSHOT;
1752 			break;
1753 		case 'f':
1754 			cb.cb_force = B_TRUE;
1755 			break;
1756 		case 'r':
1757 			cb.cb_recurse = B_TRUE;
1758 			break;
1759 		case 'R':
1760 			cb.cb_recurse = B_TRUE;
1761 			cb.cb_doclones = B_TRUE;
1762 			break;
1763 		case '?':
1764 		default:
1765 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1766 			    optopt);
1767 			usage(B_FALSE);
1768 		}
1769 	}
1770 
1771 	argc -= optind;
1772 	argv += optind;
1773 
1774 	/* check number of arguments */
1775 	if (argc == 0) {
1776 		(void) fprintf(stderr, gettext("missing dataset argument\n"));
1777 		usage(B_FALSE);
1778 	}
1779 	if (argc > 1) {
1780 		(void) fprintf(stderr, gettext("too many arguments\n"));
1781 		usage(B_FALSE);
1782 	}
1783 
1784 	at = strchr(argv[0], '@');
1785 	pound = strchr(argv[0], '#');
1786 	if (at != NULL) {
1787 
1788 		/* Build the list of snaps to destroy in cb_nvl. */
1789 		cb.cb_nvl = fnvlist_alloc();
1790 
1791 		*at = '\0';
1792 		zhp = zfs_open(g_zfs, argv[0],
1793 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1794 		if (zhp == NULL) {
1795 			nvlist_free(cb.cb_nvl);
1796 			return (1);
1797 		}
1798 
1799 		cb.cb_snapspec = at + 1;
1800 		if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1801 		    cb.cb_error) {
1802 			rv = 1;
1803 			goto out;
1804 		}
1805 
1806 		if (nvlist_empty(cb.cb_nvl)) {
1807 			(void) fprintf(stderr, gettext("could not find any "
1808 			    "snapshots to destroy; check snapshot names.\n"));
1809 			rv = 1;
1810 			goto out;
1811 		}
1812 
1813 		if (cb.cb_verbose) {
1814 			char buf[16];
1815 			zfs_nicebytes(cb.cb_snapused, buf, sizeof (buf));
1816 			if (cb.cb_parsable) {
1817 				(void) printf("reclaim\t%llu\n",
1818 				    (u_longlong_t)cb.cb_snapused);
1819 			} else if (cb.cb_dryrun) {
1820 				(void) printf(gettext("would reclaim %s\n"),
1821 				    buf);
1822 			} else {
1823 				(void) printf(gettext("will reclaim %s\n"),
1824 				    buf);
1825 			}
1826 		}
1827 
1828 		if (!cb.cb_dryrun) {
1829 			if (cb.cb_doclones) {
1830 				cb.cb_batchedsnaps = fnvlist_alloc();
1831 				err = destroy_clones(&cb);
1832 				if (err == 0) {
1833 					err = zfs_destroy_snaps_nvl(g_zfs,
1834 					    cb.cb_batchedsnaps, B_FALSE);
1835 				}
1836 				if (err != 0) {
1837 					rv = 1;
1838 					goto out;
1839 				}
1840 			}
1841 			if (err == 0) {
1842 				err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1843 				    cb.cb_defer_destroy);
1844 			}
1845 		}
1846 
1847 		if (err != 0)
1848 			rv = 1;
1849 	} else if (pound != NULL) {
1850 		int err;
1851 		nvlist_t *nvl;
1852 
1853 		if (cb.cb_dryrun) {
1854 			(void) fprintf(stderr,
1855 			    "dryrun is not supported with bookmark\n");
1856 			return (-1);
1857 		}
1858 
1859 		if (cb.cb_defer_destroy) {
1860 			(void) fprintf(stderr,
1861 			    "defer destroy is not supported with bookmark\n");
1862 			return (-1);
1863 		}
1864 
1865 		if (cb.cb_recurse) {
1866 			(void) fprintf(stderr,
1867 			    "recursive is not supported with bookmark\n");
1868 			return (-1);
1869 		}
1870 
1871 		/*
1872 		 * Unfortunately, zfs_bookmark() doesn't honor the
1873 		 * casesensitivity setting.  However, we can't simply
1874 		 * remove this check, because lzc_destroy_bookmarks()
1875 		 * ignores non-existent bookmarks, so this is necessary
1876 		 * to get a proper error message.
1877 		 */
1878 		if (!zfs_bookmark_exists(argv[0])) {
1879 			(void) fprintf(stderr, gettext("bookmark '%s' "
1880 			    "does not exist.\n"), argv[0]);
1881 			return (1);
1882 		}
1883 
1884 		nvl = fnvlist_alloc();
1885 		fnvlist_add_boolean(nvl, argv[0]);
1886 
1887 		err = lzc_destroy_bookmarks(nvl, NULL);
1888 		if (err != 0) {
1889 			(void) zfs_standard_error(g_zfs, err,
1890 			    "cannot destroy bookmark");
1891 		}
1892 
1893 		nvlist_free(nvl);
1894 
1895 		return (err);
1896 	} else {
1897 		/* Open the given dataset */
1898 		if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1899 			return (1);
1900 
1901 		cb.cb_target = zhp;
1902 
1903 		/*
1904 		 * Perform an explicit check for pools before going any further.
1905 		 */
1906 		if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1907 		    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1908 			(void) fprintf(stderr, gettext("cannot destroy '%s': "
1909 			    "operation does not apply to pools\n"),
1910 			    zfs_get_name(zhp));
1911 			(void) fprintf(stderr, gettext("use 'zfs destroy -r "
1912 			    "%s' to destroy all datasets in the pool\n"),
1913 			    zfs_get_name(zhp));
1914 			(void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1915 			    "to destroy the pool itself\n"), zfs_get_name(zhp));
1916 			rv = 1;
1917 			goto out;
1918 		}
1919 
1920 		/*
1921 		 * Check for any dependents and/or clones.
1922 		 */
1923 		cb.cb_first = B_TRUE;
1924 		if (!cb.cb_doclones && zfs_iter_dependents_v2(zhp, 0, B_TRUE,
1925 		    destroy_check_dependent, &cb) != 0) {
1926 			rv = 1;
1927 			goto out;
1928 		}
1929 
1930 		if (cb.cb_error) {
1931 			rv = 1;
1932 			goto out;
1933 		}
1934 		cb.cb_batchedsnaps = fnvlist_alloc();
1935 		if (zfs_iter_dependents_v2(zhp, 0, B_FALSE, destroy_callback,
1936 		    &cb) != 0) {
1937 			rv = 1;
1938 			goto out;
1939 		}
1940 
1941 		/*
1942 		 * Do the real thing.  The callback will close the
1943 		 * handle regardless of whether it succeeds or not.
1944 		 */
1945 		err = destroy_callback(zhp, &cb);
1946 		zhp = NULL;
1947 		if (err == 0) {
1948 			err = zfs_destroy_snaps_nvl(g_zfs,
1949 			    cb.cb_batchedsnaps, cb.cb_defer_destroy);
1950 		}
1951 		if (err != 0 || cb.cb_error == B_TRUE)
1952 			rv = 1;
1953 	}
1954 
1955 out:
1956 	fnvlist_free(cb.cb_batchedsnaps);
1957 	fnvlist_free(cb.cb_nvl);
1958 	if (zhp != NULL)
1959 		zfs_close(zhp);
1960 	return (rv);
1961 }
1962 
1963 static boolean_t
1964 is_recvd_column(zprop_get_cbdata_t *cbp)
1965 {
1966 	int i;
1967 	zfs_get_column_t col;
1968 
1969 	for (i = 0; i < ZFS_GET_NCOLS &&
1970 	    (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1971 		if (col == GET_COL_RECVD)
1972 			return (B_TRUE);
1973 	return (B_FALSE);
1974 }
1975 
1976 /*
1977  * Generates an nvlist with output version for every command based on params.
1978  * Purpose of this is to add a version of JSON output, considering the schema
1979  * format might be updated for each command in future.
1980  *
1981  * Schema:
1982  *
1983  * "output_version": {
1984  *    "command": string,
1985  *    "vers_major": integer,
1986  *    "vers_minor": integer,
1987  *  }
1988  */
1989 static nvlist_t *
1990 zfs_json_schema(int maj_v, int min_v)
1991 {
1992 	nvlist_t *sch = NULL;
1993 	nvlist_t *ov = NULL;
1994 	char cmd[MAX_CMD_LEN];
1995 	snprintf(cmd, MAX_CMD_LEN, "zfs %s", current_command->name);
1996 
1997 	sch = fnvlist_alloc();
1998 	ov = fnvlist_alloc();
1999 	fnvlist_add_string(ov, "command", cmd);
2000 	fnvlist_add_uint32(ov, "vers_major", maj_v);
2001 	fnvlist_add_uint32(ov, "vers_minor", min_v);
2002 	fnvlist_add_nvlist(sch, "output_version", ov);
2003 	fnvlist_free(ov);
2004 	return (sch);
2005 }
2006 
2007 static void
2008 fill_dataset_info(nvlist_t *list, zfs_handle_t *zhp, boolean_t as_int)
2009 {
2010 	char createtxg[ZFS_MAXPROPLEN];
2011 	zfs_type_t type = zfs_get_type(zhp);
2012 	nvlist_add_string(list, "name", zfs_get_name(zhp));
2013 
2014 	switch (type) {
2015 	case ZFS_TYPE_FILESYSTEM:
2016 		fnvlist_add_string(list, "type", "FILESYSTEM");
2017 		break;
2018 	case ZFS_TYPE_VOLUME:
2019 		fnvlist_add_string(list, "type", "VOLUME");
2020 		break;
2021 	case ZFS_TYPE_SNAPSHOT:
2022 		fnvlist_add_string(list, "type", "SNAPSHOT");
2023 		break;
2024 	case ZFS_TYPE_POOL:
2025 		fnvlist_add_string(list, "type", "POOL");
2026 		break;
2027 	case ZFS_TYPE_BOOKMARK:
2028 		fnvlist_add_string(list, "type", "BOOKMARK");
2029 		break;
2030 	default:
2031 		fnvlist_add_string(list, "type", "UNKNOWN");
2032 		break;
2033 	}
2034 
2035 	if (type != ZFS_TYPE_POOL)
2036 		fnvlist_add_string(list, "pool", zfs_get_pool_name(zhp));
2037 
2038 	if (as_int) {
2039 		fnvlist_add_uint64(list, "createtxg", zfs_prop_get_int(zhp,
2040 		    ZFS_PROP_CREATETXG));
2041 	} else {
2042 		if (zfs_prop_get(zhp, ZFS_PROP_CREATETXG, createtxg,
2043 		    sizeof (createtxg), NULL, NULL, 0, B_TRUE) == 0)
2044 			fnvlist_add_string(list, "createtxg", createtxg);
2045 	}
2046 
2047 	if (type == ZFS_TYPE_SNAPSHOT) {
2048 		char *snap = strdup(zfs_get_name(zhp));
2049 		char *ds = strsep(&snap, "@");
2050 		fnvlist_add_string(list, "dataset", ds);
2051 		fnvlist_add_string(list, "snapshot_name", snap);
2052 		free(ds);
2053 	}
2054 }
2055 
2056 /*
2057  * zfs get [-rHp] [-j [--json-int]] [-o all | field[,field]...]
2058  *		[-s source[,source]...]
2059  *	< all | property[,property]... > < fs | snap | vol > ...
2060  *
2061  *	-r	recurse over any child datasets
2062  *	-H	scripted mode.  Headers are stripped, and fields are separated
2063  *		by tabs instead of spaces.
2064  *	-o	Set of fields to display.  One of "name,property,value,
2065  *		received,source". Default is "name,property,value,source".
2066  *		"all" is an alias for all five.
2067  *	-s	Set of sources to allow.  One of
2068  *		"local,default,inherited,received,temporary,none".  Default is
2069  *		all six.
2070  *	-p	Display values in parsable (literal) format.
2071  *	-j	Display output in JSON format.
2072  *	--json-int	Display numbers as integers instead of strings.
2073  *
2074  *  Prints properties for the given datasets.  The user can control which
2075  *  columns to display as well as which property types to allow.
2076  */
2077 
2078 /*
2079  * Invoked to display the properties for a single dataset.
2080  */
2081 static int
2082 get_callback(zfs_handle_t *zhp, void *data)
2083 {
2084 	char buf[ZFS_MAXPROPLEN];
2085 	char rbuf[ZFS_MAXPROPLEN];
2086 	zprop_source_t sourcetype;
2087 	char source[ZFS_MAX_DATASET_NAME_LEN];
2088 	zprop_get_cbdata_t *cbp = data;
2089 	nvlist_t *user_props = zfs_get_user_props(zhp);
2090 	zprop_list_t *pl = cbp->cb_proplist;
2091 	nvlist_t *propval;
2092 	nvlist_t *item, *d = NULL, *props = NULL;
2093 	const char *strval;
2094 	const char *sourceval;
2095 	boolean_t received = is_recvd_column(cbp);
2096 	int err = 0;
2097 
2098 	if (cbp->cb_json) {
2099 		d = fnvlist_lookup_nvlist(cbp->cb_jsobj, "datasets");
2100 		if (d == NULL) {
2101 			fprintf(stderr, "datasets obj not found.\n");
2102 			exit(1);
2103 		}
2104 		props = fnvlist_alloc();
2105 	}
2106 
2107 	for (; pl != NULL; pl = pl->pl_next) {
2108 		char *recvdval = NULL;
2109 		/*
2110 		 * Skip the special fake placeholder.  This will also skip over
2111 		 * the name property when 'all' is specified.
2112 		 */
2113 		if (pl->pl_prop == ZFS_PROP_NAME &&
2114 		    pl == cbp->cb_proplist)
2115 			continue;
2116 
2117 		if (pl->pl_prop != ZPROP_USERPROP) {
2118 			if (zfs_prop_get(zhp, pl->pl_prop, buf,
2119 			    sizeof (buf), &sourcetype, source,
2120 			    sizeof (source),
2121 			    cbp->cb_literal) != 0) {
2122 				if (pl->pl_all)
2123 					continue;
2124 				if (!zfs_prop_valid_for_type(pl->pl_prop,
2125 				    ZFS_TYPE_DATASET, B_FALSE)) {
2126 					(void) fprintf(stderr,
2127 					    gettext("No such property '%s'\n"),
2128 					    zfs_prop_to_name(pl->pl_prop));
2129 					continue;
2130 				}
2131 				sourcetype = ZPROP_SRC_NONE;
2132 				(void) strlcpy(buf, "-", sizeof (buf));
2133 			}
2134 
2135 			if (received && (zfs_prop_get_recvd(zhp,
2136 			    zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
2137 			    cbp->cb_literal) == 0))
2138 				recvdval = rbuf;
2139 
2140 			err = zprop_collect_property(zfs_get_name(zhp), cbp,
2141 			    zfs_prop_to_name(pl->pl_prop),
2142 			    buf, sourcetype, source, recvdval, props);
2143 		} else if (zfs_prop_userquota(pl->pl_user_prop)) {
2144 			sourcetype = ZPROP_SRC_LOCAL;
2145 
2146 			if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2147 			    buf, sizeof (buf), cbp->cb_literal) != 0) {
2148 				sourcetype = ZPROP_SRC_NONE;
2149 				(void) strlcpy(buf, "-", sizeof (buf));
2150 			}
2151 
2152 			err = zprop_collect_property(zfs_get_name(zhp), cbp,
2153 			    pl->pl_user_prop, buf, sourcetype, source, NULL,
2154 			    props);
2155 		} else if (zfs_prop_written(pl->pl_user_prop)) {
2156 			sourcetype = ZPROP_SRC_LOCAL;
2157 
2158 			if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2159 			    buf, sizeof (buf), cbp->cb_literal) != 0) {
2160 				sourcetype = ZPROP_SRC_NONE;
2161 				(void) strlcpy(buf, "-", sizeof (buf));
2162 			}
2163 
2164 			err = zprop_collect_property(zfs_get_name(zhp), cbp,
2165 			    pl->pl_user_prop, buf, sourcetype, source, NULL,
2166 			    props);
2167 		} else {
2168 			if (nvlist_lookup_nvlist(user_props,
2169 			    pl->pl_user_prop, &propval) != 0) {
2170 				if (pl->pl_all)
2171 					continue;
2172 				sourcetype = ZPROP_SRC_NONE;
2173 				strval = "-";
2174 			} else {
2175 				strval = fnvlist_lookup_string(propval,
2176 				    ZPROP_VALUE);
2177 				sourceval = fnvlist_lookup_string(propval,
2178 				    ZPROP_SOURCE);
2179 
2180 				if (strcmp(sourceval,
2181 				    zfs_get_name(zhp)) == 0) {
2182 					sourcetype = ZPROP_SRC_LOCAL;
2183 				} else if (strcmp(sourceval,
2184 				    ZPROP_SOURCE_VAL_RECVD) == 0) {
2185 					sourcetype = ZPROP_SRC_RECEIVED;
2186 				} else {
2187 					sourcetype = ZPROP_SRC_INHERITED;
2188 					(void) strlcpy(source,
2189 					    sourceval, sizeof (source));
2190 				}
2191 			}
2192 
2193 			if (received && (zfs_prop_get_recvd(zhp,
2194 			    pl->pl_user_prop, rbuf, sizeof (rbuf),
2195 			    cbp->cb_literal) == 0))
2196 				recvdval = rbuf;
2197 
2198 			err = zprop_collect_property(zfs_get_name(zhp), cbp,
2199 			    pl->pl_user_prop, strval, sourcetype,
2200 			    source, recvdval, props);
2201 		}
2202 		if (err != 0)
2203 			return (err);
2204 	}
2205 
2206 	if (cbp->cb_json) {
2207 		if (!nvlist_empty(props)) {
2208 			item = fnvlist_alloc();
2209 			fill_dataset_info(item, zhp, cbp->cb_json_as_int);
2210 			fnvlist_add_nvlist(item, "properties", props);
2211 			fnvlist_add_nvlist(d, zfs_get_name(zhp), item);
2212 			fnvlist_free(props);
2213 			fnvlist_free(item);
2214 		} else {
2215 			fnvlist_free(props);
2216 		}
2217 	}
2218 
2219 	return (0);
2220 }
2221 
2222 static int
2223 zfs_do_get(int argc, char **argv)
2224 {
2225 	zprop_get_cbdata_t cb = { 0 };
2226 	int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2227 	int types = ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK;
2228 	char *fields;
2229 	int ret = 0;
2230 	int limit = 0;
2231 	zprop_list_t fake_name = { 0 };
2232 	nvlist_t *data;
2233 
2234 	/*
2235 	 * Set up default columns and sources.
2236 	 */
2237 	cb.cb_sources = ZPROP_SRC_ALL;
2238 	cb.cb_columns[0] = GET_COL_NAME;
2239 	cb.cb_columns[1] = GET_COL_PROPERTY;
2240 	cb.cb_columns[2] = GET_COL_VALUE;
2241 	cb.cb_columns[3] = GET_COL_SOURCE;
2242 	cb.cb_type = ZFS_TYPE_DATASET;
2243 
2244 	struct option long_options[] = {
2245 		{"json", no_argument, NULL, 'j'},
2246 		{"json-int", no_argument, NULL, ZFS_OPTION_JSON_NUMS_AS_INT},
2247 		{0, 0, 0, 0}
2248 	};
2249 
2250 	/* check options */
2251 	while ((c = getopt_long(argc, argv, ":d:o:s:jrt:Hp", long_options,
2252 	    NULL)) != -1) {
2253 		switch (c) {
2254 		case 'p':
2255 			cb.cb_literal = B_TRUE;
2256 			break;
2257 		case 'd':
2258 			limit = parse_depth(optarg, &flags);
2259 			break;
2260 		case 'r':
2261 			flags |= ZFS_ITER_RECURSE;
2262 			break;
2263 		case 'H':
2264 			cb.cb_scripted = B_TRUE;
2265 			break;
2266 		case 'j':
2267 			cb.cb_json = B_TRUE;
2268 			cb.cb_jsobj = zfs_json_schema(0, 1);
2269 			data = fnvlist_alloc();
2270 			fnvlist_add_nvlist(cb.cb_jsobj, "datasets", data);
2271 			fnvlist_free(data);
2272 			break;
2273 		case ZFS_OPTION_JSON_NUMS_AS_INT:
2274 			cb.cb_json_as_int = B_TRUE;
2275 			cb.cb_literal = B_TRUE;
2276 			break;
2277 		case ':':
2278 			(void) fprintf(stderr, gettext("missing argument for "
2279 			    "'%c' option\n"), optopt);
2280 			usage(B_FALSE);
2281 			break;
2282 		case 'o':
2283 			/*
2284 			 * Process the set of columns to display.  We zero out
2285 			 * the structure to give us a blank slate.
2286 			 */
2287 			memset(&cb.cb_columns, 0, sizeof (cb.cb_columns));
2288 
2289 			i = 0;
2290 			for (char *tok; (tok = strsep(&optarg, ",")); ) {
2291 				static const char *const col_subopts[] =
2292 				{ "name", "property", "value",
2293 				    "received", "source", "all" };
2294 				static const zfs_get_column_t col_subopt_col[] =
2295 				{ GET_COL_NAME, GET_COL_PROPERTY, GET_COL_VALUE,
2296 				    GET_COL_RECVD, GET_COL_SOURCE };
2297 				static const int col_subopt_flags[] =
2298 				{ 0, 0, 0, ZFS_ITER_RECVD_PROPS, 0 };
2299 
2300 				if (i == ZFS_GET_NCOLS) {
2301 					(void) fprintf(stderr, gettext("too "
2302 					    "many fields given to -o "
2303 					    "option\n"));
2304 					usage(B_FALSE);
2305 				}
2306 
2307 				for (c = 0; c < ARRAY_SIZE(col_subopts); ++c)
2308 					if (strcmp(tok, col_subopts[c]) == 0)
2309 						goto found;
2310 
2311 				(void) fprintf(stderr,
2312 				    gettext("invalid column name '%s'\n"), tok);
2313 				usage(B_FALSE);
2314 
2315 found:
2316 				if (c >= 5) {
2317 					if (i > 0) {
2318 						(void) fprintf(stderr,
2319 						    gettext("\"all\" conflicts "
2320 						    "with specific fields "
2321 						    "given to -o option\n"));
2322 						usage(B_FALSE);
2323 					}
2324 
2325 					memcpy(cb.cb_columns, col_subopt_col,
2326 					    sizeof (col_subopt_col));
2327 					flags |= ZFS_ITER_RECVD_PROPS;
2328 					i = ZFS_GET_NCOLS;
2329 				} else {
2330 					cb.cb_columns[i++] = col_subopt_col[c];
2331 					flags |= col_subopt_flags[c];
2332 				}
2333 			}
2334 			break;
2335 
2336 		case 's':
2337 			cb.cb_sources = 0;
2338 
2339 			for (char *tok; (tok = strsep(&optarg, ",")); ) {
2340 				static const char *const source_opt[] = {
2341 					"local", "default",
2342 					"inherited", "received",
2343 					"temporary", "none" };
2344 				static const int source_flg[] = {
2345 					ZPROP_SRC_LOCAL, ZPROP_SRC_DEFAULT,
2346 					ZPROP_SRC_INHERITED, ZPROP_SRC_RECEIVED,
2347 					ZPROP_SRC_TEMPORARY, ZPROP_SRC_NONE };
2348 
2349 				for (i = 0; i < ARRAY_SIZE(source_opt); ++i)
2350 					if (strcmp(tok, source_opt[i]) == 0) {
2351 						cb.cb_sources |= source_flg[i];
2352 						goto found2;
2353 					}
2354 
2355 				(void) fprintf(stderr,
2356 				    gettext("invalid source '%s'\n"), tok);
2357 				usage(B_FALSE);
2358 found2:;
2359 			}
2360 			break;
2361 
2362 		case 't':
2363 			types = 0;
2364 			flags &= ~ZFS_ITER_PROP_LISTSNAPS;
2365 
2366 			for (char *tok; (tok = strsep(&optarg, ",")); ) {
2367 				static const char *const type_opts[] = {
2368 					"filesystem",
2369 					"fs",
2370 					"volume",
2371 					"vol",
2372 					"snapshot",
2373 					"snap",
2374 					"bookmark",
2375 					"all"
2376 				};
2377 				static const int type_types[] = {
2378 					ZFS_TYPE_FILESYSTEM,
2379 					ZFS_TYPE_FILESYSTEM,
2380 					ZFS_TYPE_VOLUME,
2381 					ZFS_TYPE_VOLUME,
2382 					ZFS_TYPE_SNAPSHOT,
2383 					ZFS_TYPE_SNAPSHOT,
2384 					ZFS_TYPE_BOOKMARK,
2385 					ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK
2386 				};
2387 
2388 				for (i = 0; i < ARRAY_SIZE(type_opts); ++i)
2389 					if (strcmp(tok, type_opts[i]) == 0) {
2390 						types |= type_types[i];
2391 						goto found3;
2392 					}
2393 
2394 				(void) fprintf(stderr,
2395 				    gettext("invalid type '%s'\n"), tok);
2396 				usage(B_FALSE);
2397 found3:;
2398 			}
2399 			break;
2400 		case '?':
2401 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2402 			    optopt);
2403 			usage(B_FALSE);
2404 		}
2405 	}
2406 
2407 	argc -= optind;
2408 	argv += optind;
2409 
2410 	if (argc < 1) {
2411 		(void) fprintf(stderr, gettext("missing property "
2412 		    "argument\n"));
2413 		usage(B_FALSE);
2414 	}
2415 
2416 	if (!cb.cb_json && cb.cb_json_as_int) {
2417 		(void) fprintf(stderr, gettext("'--json-int' only works with"
2418 		    " '-j' option\n"));
2419 		usage(B_FALSE);
2420 	}
2421 
2422 	fields = argv[0];
2423 
2424 	/*
2425 	 * Handle users who want to get all snapshots or bookmarks
2426 	 * of a dataset (ex. 'zfs get -t snapshot refer <dataset>').
2427 	 */
2428 	if ((types == ZFS_TYPE_SNAPSHOT || types == ZFS_TYPE_BOOKMARK) &&
2429 	    argc > 1 && (flags & ZFS_ITER_RECURSE) == 0 && limit == 0) {
2430 		flags |= (ZFS_ITER_DEPTH_LIMIT | ZFS_ITER_RECURSE);
2431 		limit = 1;
2432 	}
2433 
2434 	if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
2435 	    != 0)
2436 		usage(B_FALSE);
2437 
2438 	argc--;
2439 	argv++;
2440 
2441 	/*
2442 	 * As part of zfs_expand_proplist(), we keep track of the maximum column
2443 	 * width for each property.  For the 'NAME' (and 'SOURCE') columns, we
2444 	 * need to know the maximum name length.  However, the user likely did
2445 	 * not specify 'name' as one of the properties to fetch, so we need to
2446 	 * make sure we always include at least this property for
2447 	 * print_get_headers() to work properly.
2448 	 */
2449 	if (cb.cb_proplist != NULL) {
2450 		fake_name.pl_prop = ZFS_PROP_NAME;
2451 		fake_name.pl_width = strlen(gettext("NAME"));
2452 		fake_name.pl_next = cb.cb_proplist;
2453 		cb.cb_proplist = &fake_name;
2454 	}
2455 
2456 	cb.cb_first = B_TRUE;
2457 
2458 	/* run for each object */
2459 	ret = zfs_for_each(argc, argv, flags, types, NULL,
2460 	    &cb.cb_proplist, limit, get_callback, &cb);
2461 
2462 	if (ret == 0 && cb.cb_json)
2463 		zcmd_print_json(cb.cb_jsobj);
2464 	else if (ret != 0 && cb.cb_json)
2465 		nvlist_free(cb.cb_jsobj);
2466 
2467 	if (cb.cb_proplist == &fake_name)
2468 		zprop_free_list(fake_name.pl_next);
2469 	else
2470 		zprop_free_list(cb.cb_proplist);
2471 
2472 	return (ret);
2473 }
2474 
2475 /*
2476  * inherit [-rS] <property> <fs|vol> ...
2477  *
2478  *	-r	Recurse over all children
2479  *	-S	Revert to received value, if any
2480  *
2481  * For each dataset specified on the command line, inherit the given property
2482  * from its parent.  Inheriting a property at the pool level will cause it to
2483  * use the default value.  The '-r' flag will recurse over all children, and is
2484  * useful for setting a property on a hierarchy-wide basis, regardless of any
2485  * local modifications for each dataset.
2486  */
2487 
2488 typedef struct inherit_cbdata {
2489 	const char *cb_propname;
2490 	boolean_t cb_received;
2491 } inherit_cbdata_t;
2492 
2493 static int
2494 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
2495 {
2496 	inherit_cbdata_t *cb = data;
2497 	zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
2498 
2499 	/*
2500 	 * If we're doing it recursively, then ignore properties that
2501 	 * are not valid for this type of dataset.
2502 	 */
2503 	if (prop != ZPROP_INVAL &&
2504 	    !zfs_prop_valid_for_type(prop, zfs_get_type(zhp), B_FALSE))
2505 		return (0);
2506 
2507 	return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
2508 }
2509 
2510 static int
2511 inherit_cb(zfs_handle_t *zhp, void *data)
2512 {
2513 	inherit_cbdata_t *cb = data;
2514 
2515 	return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
2516 }
2517 
2518 static int
2519 zfs_do_inherit(int argc, char **argv)
2520 {
2521 	int c;
2522 	zfs_prop_t prop;
2523 	inherit_cbdata_t cb = { 0 };
2524 	char *propname;
2525 	int ret = 0;
2526 	int flags = 0;
2527 	boolean_t received = B_FALSE;
2528 
2529 	/* check options */
2530 	while ((c = getopt(argc, argv, "rS")) != -1) {
2531 		switch (c) {
2532 		case 'r':
2533 			flags |= ZFS_ITER_RECURSE;
2534 			break;
2535 		case 'S':
2536 			received = B_TRUE;
2537 			break;
2538 		case '?':
2539 		default:
2540 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2541 			    optopt);
2542 			usage(B_FALSE);
2543 		}
2544 	}
2545 
2546 	argc -= optind;
2547 	argv += optind;
2548 
2549 	/* check number of arguments */
2550 	if (argc < 1) {
2551 		(void) fprintf(stderr, gettext("missing property argument\n"));
2552 		usage(B_FALSE);
2553 	}
2554 	if (argc < 2) {
2555 		(void) fprintf(stderr, gettext("missing dataset argument\n"));
2556 		usage(B_FALSE);
2557 	}
2558 
2559 	propname = argv[0];
2560 	argc--;
2561 	argv++;
2562 
2563 	if ((prop = zfs_name_to_prop(propname)) != ZPROP_USERPROP) {
2564 		if (zfs_prop_readonly(prop)) {
2565 			(void) fprintf(stderr, gettext(
2566 			    "%s property is read-only\n"),
2567 			    propname);
2568 			return (1);
2569 		}
2570 		if (!zfs_prop_inheritable(prop) && !received) {
2571 			(void) fprintf(stderr, gettext("'%s' property cannot "
2572 			    "be inherited\n"), propname);
2573 			if (prop == ZFS_PROP_QUOTA ||
2574 			    prop == ZFS_PROP_RESERVATION ||
2575 			    prop == ZFS_PROP_REFQUOTA ||
2576 			    prop == ZFS_PROP_REFRESERVATION) {
2577 				(void) fprintf(stderr, gettext("use 'zfs set "
2578 				    "%s=none' to clear\n"), propname);
2579 				(void) fprintf(stderr, gettext("use 'zfs "
2580 				    "inherit -S %s' to revert to received "
2581 				    "value\n"), propname);
2582 			}
2583 			return (1);
2584 		}
2585 		if (received && (prop == ZFS_PROP_VOLSIZE ||
2586 		    prop == ZFS_PROP_VERSION)) {
2587 			(void) fprintf(stderr, gettext("'%s' property cannot "
2588 			    "be reverted to a received value\n"), propname);
2589 			return (1);
2590 		}
2591 	} else if (!zfs_prop_user(propname)) {
2592 		(void) fprintf(stderr, gettext("invalid property '%s'\n"),
2593 		    propname);
2594 		usage(B_FALSE);
2595 	}
2596 
2597 	cb.cb_propname = propname;
2598 	cb.cb_received = received;
2599 
2600 	if (flags & ZFS_ITER_RECURSE) {
2601 		ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2602 		    NULL, NULL, 0, inherit_recurse_cb, &cb);
2603 	} else {
2604 		ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2605 		    NULL, NULL, 0, inherit_cb, &cb);
2606 	}
2607 
2608 	return (ret);
2609 }
2610 
2611 typedef struct upgrade_cbdata {
2612 	uint64_t cb_numupgraded;
2613 	uint64_t cb_numsamegraded;
2614 	uint64_t cb_numfailed;
2615 	uint64_t cb_version;
2616 	boolean_t cb_newer;
2617 	boolean_t cb_foundone;
2618 	char cb_lastfs[ZFS_MAX_DATASET_NAME_LEN];
2619 } upgrade_cbdata_t;
2620 
2621 static int
2622 same_pool(zfs_handle_t *zhp, const char *name)
2623 {
2624 	int len1 = strcspn(name, "/@");
2625 	const char *zhname = zfs_get_name(zhp);
2626 	int len2 = strcspn(zhname, "/@");
2627 
2628 	if (len1 != len2)
2629 		return (B_FALSE);
2630 	return (strncmp(name, zhname, len1) == 0);
2631 }
2632 
2633 static int
2634 upgrade_list_callback(zfs_handle_t *zhp, void *data)
2635 {
2636 	upgrade_cbdata_t *cb = data;
2637 	int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2638 
2639 	/* list if it's old/new */
2640 	if ((!cb->cb_newer && version < ZPL_VERSION) ||
2641 	    (cb->cb_newer && version > ZPL_VERSION)) {
2642 		char *str;
2643 		if (cb->cb_newer) {
2644 			str = gettext("The following filesystems are "
2645 			    "formatted using a newer software version and\n"
2646 			    "cannot be accessed on the current system.\n\n");
2647 		} else {
2648 			str = gettext("The following filesystems are "
2649 			    "out of date, and can be upgraded.  After being\n"
2650 			    "upgraded, these filesystems (and any 'zfs send' "
2651 			    "streams generated from\n"
2652 			    "subsequent snapshots) will no longer be "
2653 			    "accessible by older software versions.\n\n");
2654 		}
2655 
2656 		if (!cb->cb_foundone) {
2657 			(void) puts(str);
2658 			(void) printf(gettext("VER  FILESYSTEM\n"));
2659 			(void) printf(gettext("---  ------------\n"));
2660 			cb->cb_foundone = B_TRUE;
2661 		}
2662 
2663 		(void) printf("%2u   %s\n", version, zfs_get_name(zhp));
2664 	}
2665 
2666 	return (0);
2667 }
2668 
2669 static int
2670 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2671 {
2672 	upgrade_cbdata_t *cb = data;
2673 	int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2674 	int needed_spa_version;
2675 	int spa_version;
2676 
2677 	if (zfs_spa_version(zhp, &spa_version) < 0)
2678 		return (-1);
2679 
2680 	needed_spa_version = zfs_spa_version_map(cb->cb_version);
2681 
2682 	if (needed_spa_version < 0)
2683 		return (-1);
2684 
2685 	if (spa_version < needed_spa_version) {
2686 		/* can't upgrade */
2687 		(void) printf(gettext("%s: can not be "
2688 		    "upgraded; the pool version needs to first "
2689 		    "be upgraded\nto version %d\n\n"),
2690 		    zfs_get_name(zhp), needed_spa_version);
2691 		cb->cb_numfailed++;
2692 		return (0);
2693 	}
2694 
2695 	/* upgrade */
2696 	if (version < cb->cb_version) {
2697 		char verstr[24];
2698 		(void) snprintf(verstr, sizeof (verstr),
2699 		    "%llu", (u_longlong_t)cb->cb_version);
2700 		if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2701 			/*
2702 			 * If they did "zfs upgrade -a", then we could
2703 			 * be doing ioctls to different pools.  We need
2704 			 * to log this history once to each pool, and bypass
2705 			 * the normal history logging that happens in main().
2706 			 */
2707 			(void) zpool_log_history(g_zfs, history_str);
2708 			log_history = B_FALSE;
2709 		}
2710 		if (zfs_prop_set(zhp, "version", verstr) == 0)
2711 			cb->cb_numupgraded++;
2712 		else
2713 			cb->cb_numfailed++;
2714 		(void) strlcpy(cb->cb_lastfs, zfs_get_name(zhp),
2715 		    sizeof (cb->cb_lastfs));
2716 	} else if (version > cb->cb_version) {
2717 		/* can't downgrade */
2718 		(void) printf(gettext("%s: can not be downgraded; "
2719 		    "it is already at version %u\n"),
2720 		    zfs_get_name(zhp), version);
2721 		cb->cb_numfailed++;
2722 	} else {
2723 		cb->cb_numsamegraded++;
2724 	}
2725 	return (0);
2726 }
2727 
2728 /*
2729  * zfs upgrade
2730  * zfs upgrade -v
2731  * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2732  */
2733 static int
2734 zfs_do_upgrade(int argc, char **argv)
2735 {
2736 	boolean_t all = B_FALSE;
2737 	boolean_t showversions = B_FALSE;
2738 	int ret = 0;
2739 	upgrade_cbdata_t cb = { 0 };
2740 	int c;
2741 	int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2742 
2743 	/* check options */
2744 	while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2745 		switch (c) {
2746 		case 'r':
2747 			flags |= ZFS_ITER_RECURSE;
2748 			break;
2749 		case 'v':
2750 			showversions = B_TRUE;
2751 			break;
2752 		case 'V':
2753 			if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2754 			    optarg, &cb.cb_version) != 0) {
2755 				(void) fprintf(stderr,
2756 				    gettext("invalid version %s\n"), optarg);
2757 				usage(B_FALSE);
2758 			}
2759 			break;
2760 		case 'a':
2761 			all = B_TRUE;
2762 			break;
2763 		case '?':
2764 		default:
2765 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2766 			    optopt);
2767 			usage(B_FALSE);
2768 		}
2769 	}
2770 
2771 	argc -= optind;
2772 	argv += optind;
2773 
2774 	if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2775 		usage(B_FALSE);
2776 	if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2777 	    cb.cb_version || argc))
2778 		usage(B_FALSE);
2779 	if ((all || argc) && (showversions))
2780 		usage(B_FALSE);
2781 	if (all && argc)
2782 		usage(B_FALSE);
2783 
2784 	if (showversions) {
2785 		/* Show info on available versions. */
2786 		(void) printf(gettext("The following filesystem versions are "
2787 		    "supported:\n\n"));
2788 		(void) printf(gettext("VER  DESCRIPTION\n"));
2789 		(void) printf("---  -----------------------------------------"
2790 		    "---------------\n");
2791 		(void) printf(gettext(" 1   Initial ZFS filesystem version\n"));
2792 		(void) printf(gettext(" 2   Enhanced directory entries\n"));
2793 		(void) printf(gettext(" 3   Case insensitive and filesystem "
2794 		    "user identifier (FUID)\n"));
2795 		(void) printf(gettext(" 4   userquota, groupquota "
2796 		    "properties\n"));
2797 		(void) printf(gettext(" 5   System attributes\n"));
2798 		(void) printf(gettext("\nFor more information on a particular "
2799 		    "version, including supported releases,\n"));
2800 		(void) printf("see the ZFS Administration Guide.\n\n");
2801 		ret = 0;
2802 	} else if (argc || all) {
2803 		/* Upgrade filesystems */
2804 		if (cb.cb_version == 0)
2805 			cb.cb_version = ZPL_VERSION;
2806 		ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2807 		    NULL, NULL, 0, upgrade_set_callback, &cb);
2808 		(void) printf(gettext("%llu filesystems upgraded\n"),
2809 		    (u_longlong_t)cb.cb_numupgraded);
2810 		if (cb.cb_numsamegraded) {
2811 			(void) printf(gettext("%llu filesystems already at "
2812 			    "this version\n"),
2813 			    (u_longlong_t)cb.cb_numsamegraded);
2814 		}
2815 		if (cb.cb_numfailed != 0)
2816 			ret = 1;
2817 	} else {
2818 		/* List old-version filesystems */
2819 		boolean_t found;
2820 		(void) printf(gettext("This system is currently running "
2821 		    "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2822 
2823 		flags |= ZFS_ITER_RECURSE;
2824 		ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2825 		    NULL, NULL, 0, upgrade_list_callback, &cb);
2826 
2827 		found = cb.cb_foundone;
2828 		cb.cb_foundone = B_FALSE;
2829 		cb.cb_newer = B_TRUE;
2830 
2831 		ret |= zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2832 		    NULL, NULL, 0, upgrade_list_callback, &cb);
2833 
2834 		if (!cb.cb_foundone && !found) {
2835 			(void) printf(gettext("All filesystems are "
2836 			    "formatted with the current version.\n"));
2837 		}
2838 	}
2839 
2840 	return (ret);
2841 }
2842 
2843 /*
2844  * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2845  *               [-S field [-S field]...] [-t type[,...]]
2846  *               filesystem | snapshot | path
2847  * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2848  *                [-S field [-S field]...] [-t type[,...]]
2849  *                filesystem | snapshot | path
2850  * zfs projectspace [-Hp] [-o field[,...]] [-s field [-s field]...]
2851  *                [-S field [-S field]...] filesystem | snapshot | path
2852  *
2853  *	-H      Scripted mode; elide headers and separate columns by tabs.
2854  *	-i	Translate SID to POSIX ID.
2855  *	-n	Print numeric ID instead of user/group name.
2856  *	-o      Control which fields to display.
2857  *	-p	Use exact (parsable) numeric output.
2858  *	-s      Specify sort columns, descending order.
2859  *	-S      Specify sort columns, ascending order.
2860  *	-t      Control which object types to display.
2861  *
2862  *	Displays space consumed by, and quotas on, each user in the specified
2863  *	filesystem or snapshot.
2864  */
2865 
2866 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2867 enum us_field_types {
2868 	USFIELD_TYPE,
2869 	USFIELD_NAME,
2870 	USFIELD_USED,
2871 	USFIELD_QUOTA,
2872 	USFIELD_OBJUSED,
2873 	USFIELD_OBJQUOTA
2874 };
2875 static const char *const us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA",
2876 				    "OBJUSED", "OBJQUOTA" };
2877 static const char *const us_field_names[] = { "type", "name", "used", "quota",
2878 				    "objused", "objquota" };
2879 #define	USFIELD_LAST	(sizeof (us_field_names) / sizeof (char *))
2880 
2881 #define	USTYPE_PSX_GRP	(1 << 0)
2882 #define	USTYPE_PSX_USR	(1 << 1)
2883 #define	USTYPE_SMB_GRP	(1 << 2)
2884 #define	USTYPE_SMB_USR	(1 << 3)
2885 #define	USTYPE_PROJ	(1 << 4)
2886 #define	USTYPE_ALL	\
2887 	(USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR | \
2888 	    USTYPE_PROJ)
2889 
2890 static int us_type_bits[] = {
2891 	USTYPE_PSX_GRP,
2892 	USTYPE_PSX_USR,
2893 	USTYPE_SMB_GRP,
2894 	USTYPE_SMB_USR,
2895 	USTYPE_ALL
2896 };
2897 static const char *const us_type_names[] = { "posixgroup", "posixuser",
2898 	"smbgroup", "smbuser", "all" };
2899 
2900 typedef struct us_cbdata us_cbdata_t;
2901 typedef struct us_node {
2902 	nvlist_t	*usn_nvl;
2903 	us_cbdata_t	*usn_cbdata;
2904 	avl_node_t	usn_avlnode;
2905 	list_node_t	usn_listnode;
2906 } us_node_t;
2907 
2908 struct us_cbdata {
2909 	nvlist_t	**cb_nvlp;
2910 	avl_tree_t	cb_avl;
2911 	boolean_t	cb_numname;
2912 	boolean_t	cb_nicenum;
2913 	boolean_t	cb_sid2posix;
2914 	zfs_userquota_prop_t cb_prop;
2915 	zfs_sort_column_t *cb_sortcol;
2916 	size_t		cb_width[USFIELD_LAST];
2917 };
2918 
2919 static boolean_t us_populated = B_FALSE;
2920 
2921 static int
2922 us_field_index(const char *field)
2923 {
2924 	for (int i = 0; i < USFIELD_LAST; i++) {
2925 		if (strcmp(field, us_field_names[i]) == 0)
2926 			return (i);
2927 	}
2928 
2929 	return (-1);
2930 }
2931 
2932 static int
2933 us_compare(const void *larg, const void *rarg)
2934 {
2935 	const us_node_t *l = larg;
2936 	const us_node_t *r = rarg;
2937 	zfs_sort_column_t *sortcol = l->usn_cbdata->cb_sortcol;
2938 	boolean_t numname = l->usn_cbdata->cb_numname;
2939 	nvlist_t *lnvl = l->usn_nvl;
2940 	nvlist_t *rnvl = r->usn_nvl;
2941 	int rc = 0;
2942 	boolean_t lvb, rvb;
2943 
2944 	for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2945 		const char *lvstr = "";
2946 		const char *rvstr = "";
2947 		uint32_t lv32 = 0;
2948 		uint32_t rv32 = 0;
2949 		uint64_t lv64 = 0;
2950 		uint64_t rv64 = 0;
2951 		zfs_prop_t prop = sortcol->sc_prop;
2952 		const char *propname = NULL;
2953 
2954 		switch (prop) {
2955 		case ZFS_PROP_TYPE:
2956 			propname = "type";
2957 			(void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2958 			(void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2959 			rc = TREE_CMP(lv32, rv32);
2960 			break;
2961 		case ZFS_PROP_NAME:
2962 			propname = "name";
2963 			if (numname) {
2964 compare_nums:
2965 				(void) nvlist_lookup_uint64(lnvl, propname,
2966 				    &lv64);
2967 				(void) nvlist_lookup_uint64(rnvl, propname,
2968 				    &rv64);
2969 				rc = TREE_CMP(lv64, rv64);
2970 			} else {
2971 				if ((nvlist_lookup_string(lnvl, propname,
2972 				    &lvstr) == ENOENT) ||
2973 				    (nvlist_lookup_string(rnvl, propname,
2974 				    &rvstr) == ENOENT)) {
2975 					goto compare_nums;
2976 				}
2977 				rc = TREE_ISIGN(strcmp(lvstr, rvstr));
2978 			}
2979 			break;
2980 		case ZFS_PROP_USED:
2981 		case ZFS_PROP_QUOTA:
2982 			if (!us_populated)
2983 				break;
2984 			if (prop == ZFS_PROP_USED)
2985 				propname = "used";
2986 			else
2987 				propname = "quota";
2988 			(void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2989 			(void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2990 			rc = TREE_CMP(lv64, rv64);
2991 			break;
2992 
2993 		default:
2994 			break;
2995 		}
2996 
2997 		if (rc != 0) {
2998 			if (sortcol->sc_reverse)
2999 				return (-rc);
3000 			return (rc);
3001 		}
3002 	}
3003 
3004 	/*
3005 	 * If entries still seem to be the same, check if they are of the same
3006 	 * type (smbentity is added only if we are doing SID to POSIX ID
3007 	 * translation where we can have duplicate type/name combinations).
3008 	 */
3009 	if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
3010 	    nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0)
3011 		return (TREE_CMP(lvb, rvb));
3012 
3013 	return (0);
3014 }
3015 
3016 static boolean_t
3017 zfs_prop_is_user(unsigned p)
3018 {
3019 	return (p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA ||
3020 	    p == ZFS_PROP_USEROBJUSED || p == ZFS_PROP_USEROBJQUOTA);
3021 }
3022 
3023 static boolean_t
3024 zfs_prop_is_group(unsigned p)
3025 {
3026 	return (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA ||
3027 	    p == ZFS_PROP_GROUPOBJUSED || p == ZFS_PROP_GROUPOBJQUOTA);
3028 }
3029 
3030 static boolean_t
3031 zfs_prop_is_project(unsigned p)
3032 {
3033 	return (p == ZFS_PROP_PROJECTUSED || p == ZFS_PROP_PROJECTQUOTA ||
3034 	    p == ZFS_PROP_PROJECTOBJUSED || p == ZFS_PROP_PROJECTOBJQUOTA);
3035 }
3036 
3037 static inline const char *
3038 us_type2str(unsigned field_type)
3039 {
3040 	switch (field_type) {
3041 	case USTYPE_PSX_USR:
3042 		return ("POSIX User");
3043 	case USTYPE_PSX_GRP:
3044 		return ("POSIX Group");
3045 	case USTYPE_SMB_USR:
3046 		return ("SMB User");
3047 	case USTYPE_SMB_GRP:
3048 		return ("SMB Group");
3049 	case USTYPE_PROJ:
3050 		return ("Project");
3051 	default:
3052 		return ("Undefined");
3053 	}
3054 }
3055 
3056 static int
3057 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space,
3058     uint64_t default_quota)
3059 {
3060 	us_cbdata_t *cb = (us_cbdata_t *)arg;
3061 	zfs_userquota_prop_t prop = cb->cb_prop;
3062 	char *name = NULL;
3063 	const char *propname;
3064 	char sizebuf[32];
3065 	us_node_t *node;
3066 	avl_tree_t *avl = &cb->cb_avl;
3067 	avl_index_t idx;
3068 	nvlist_t *props;
3069 	us_node_t *n;
3070 	unsigned type = 0;
3071 	const char *typestr;
3072 	size_t namelen;
3073 	size_t typelen;
3074 	size_t sizelen;
3075 	int typeidx, nameidx, sizeidx;
3076 	boolean_t smbentity = B_FALSE;
3077 
3078 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3079 		nomem();
3080 	node = safe_malloc(sizeof (us_node_t));
3081 	node->usn_cbdata = cb;
3082 	node->usn_nvl = props;
3083 
3084 	if (domain != NULL && domain[0] != '\0') {
3085 #ifdef HAVE_IDMAP
3086 		/* SMB */
3087 		char sid[MAXNAMELEN + 32];
3088 		uid_t id;
3089 		uint64_t classes;
3090 		int err;
3091 		directory_error_t e;
3092 
3093 		smbentity = B_TRUE;
3094 
3095 		(void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
3096 
3097 		if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
3098 			type = USTYPE_SMB_GRP;
3099 			err = sid_to_id(sid, B_FALSE, &id);
3100 		} else {
3101 			type = USTYPE_SMB_USR;
3102 			err = sid_to_id(sid, B_TRUE, &id);
3103 		}
3104 
3105 		if (err == 0) {
3106 			rid = id;
3107 			if (!cb->cb_sid2posix) {
3108 				e = directory_name_from_sid(NULL, sid, &name,
3109 				    &classes);
3110 				if (e != NULL)
3111 					directory_error_free(e);
3112 				if (name == NULL)
3113 					name = sid;
3114 			}
3115 		}
3116 #else
3117 		nvlist_free(props);
3118 		free(node);
3119 
3120 		return (-1);
3121 #endif /* HAVE_IDMAP */
3122 	}
3123 
3124 	if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
3125 		/* POSIX or -i */
3126 		if (zfs_prop_is_group(prop)) {
3127 			type = USTYPE_PSX_GRP;
3128 			if (!cb->cb_numname) {
3129 				struct group *g;
3130 
3131 				if ((g = getgrgid(rid)) != NULL)
3132 					name = g->gr_name;
3133 			}
3134 		} else if (zfs_prop_is_user(prop)) {
3135 			type = USTYPE_PSX_USR;
3136 			if (!cb->cb_numname) {
3137 				struct passwd *p;
3138 
3139 				if ((p = getpwuid(rid)) != NULL)
3140 					name = p->pw_name;
3141 			}
3142 		} else {
3143 			type = USTYPE_PROJ;
3144 		}
3145 	}
3146 
3147 	/*
3148 	 * Make sure that the type/name combination is unique when doing
3149 	 * SID to POSIX ID translation (hence changing the type from SMB to
3150 	 * POSIX).
3151 	 */
3152 	if (cb->cb_sid2posix &&
3153 	    nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
3154 		nomem();
3155 
3156 	/* Calculate/update width of TYPE field */
3157 	typestr = us_type2str(type);
3158 	typelen = strlen(gettext(typestr));
3159 	typeidx = us_field_index("type");
3160 	if (typelen > cb->cb_width[typeidx])
3161 		cb->cb_width[typeidx] = typelen;
3162 	if (nvlist_add_uint32(props, "type", type) != 0)
3163 		nomem();
3164 
3165 	/* Calculate/update width of NAME field */
3166 	if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
3167 		if (nvlist_add_uint64(props, "name", rid) != 0)
3168 			nomem();
3169 		namelen = snprintf(NULL, 0, "%u", rid);
3170 	} else {
3171 		if (nvlist_add_string(props, "name", name) != 0)
3172 			nomem();
3173 		namelen = strlen(name);
3174 	}
3175 	nameidx = us_field_index("name");
3176 	if (nameidx >= 0 && namelen > cb->cb_width[nameidx])
3177 		cb->cb_width[nameidx] = namelen;
3178 
3179 	/*
3180 	 * Check if this type/name combination is in the list and update it;
3181 	 * otherwise add new node to the list.
3182 	 */
3183 	if ((n = avl_find(avl, node, &idx)) == NULL) {
3184 		avl_insert(avl, node, idx);
3185 	} else {
3186 		nvlist_free(props);
3187 		free(node);
3188 		node = n;
3189 		props = node->usn_nvl;
3190 	}
3191 
3192 	/* Calculate/update width of USED/QUOTA fields */
3193 	if (cb->cb_nicenum) {
3194 		if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED ||
3195 		    prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA ||
3196 		    prop == ZFS_PROP_PROJECTUSED ||
3197 		    prop == ZFS_PROP_PROJECTQUOTA) {
3198 			zfs_nicebytes(space, sizebuf, sizeof (sizebuf));
3199 		} else {
3200 			zfs_nicenum(space, sizebuf, sizeof (sizebuf));
3201 		}
3202 	} else {
3203 		(void) snprintf(sizebuf, sizeof (sizebuf), "%llu",
3204 		    (u_longlong_t)space);
3205 	}
3206 	sizelen = strlen(sizebuf);
3207 	if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED ||
3208 	    prop == ZFS_PROP_PROJECTUSED) {
3209 		propname = "used";
3210 		if (!nvlist_exists(props, "quota"))
3211 			(void) nvlist_add_uint64(props, "quota", default_quota);
3212 	} else if (prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA ||
3213 	    prop == ZFS_PROP_PROJECTQUOTA) {
3214 		propname = "quota";
3215 		if (!nvlist_exists(props, "used"))
3216 			(void) nvlist_add_uint64(props, "used", 0);
3217 	} else if (prop == ZFS_PROP_USEROBJUSED ||
3218 	    prop == ZFS_PROP_GROUPOBJUSED || prop == ZFS_PROP_PROJECTOBJUSED) {
3219 		propname = "objused";
3220 		if (!nvlist_exists(props, "objquota")) {
3221 			(void) nvlist_add_uint64(props, "objquota",
3222 			    default_quota);
3223 		}
3224 	} else if (prop == ZFS_PROP_USEROBJQUOTA ||
3225 	    prop == ZFS_PROP_GROUPOBJQUOTA ||
3226 	    prop == ZFS_PROP_PROJECTOBJQUOTA) {
3227 		propname = "objquota";
3228 		if (!nvlist_exists(props, "objused"))
3229 			(void) nvlist_add_uint64(props, "objused", 0);
3230 	} else {
3231 		return (-1);
3232 	}
3233 	sizeidx = us_field_index(propname);
3234 	if (sizeidx >= 0 && sizelen > cb->cb_width[sizeidx])
3235 		cb->cb_width[sizeidx] = sizelen;
3236 
3237 	if (nvlist_add_uint64(props, propname, space) != 0)
3238 		nomem();
3239 
3240 	return (0);
3241 }
3242 
3243 static void
3244 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
3245     size_t *width, us_node_t *node)
3246 {
3247 	nvlist_t *nvl = node->usn_nvl;
3248 	char valstr[MAXNAMELEN];
3249 	boolean_t first = B_TRUE;
3250 	int cfield = 0;
3251 	int field;
3252 	uint32_t ustype;
3253 
3254 	/* Check type */
3255 	(void) nvlist_lookup_uint32(nvl, "type", &ustype);
3256 	if (!(ustype & types))
3257 		return;
3258 
3259 	while ((field = fields[cfield]) != USFIELD_LAST) {
3260 		nvpair_t *nvp = NULL;
3261 		data_type_t type;
3262 		uint32_t val32 = -1;
3263 		uint64_t val64 = -1;
3264 		const char *strval = "-";
3265 
3266 		while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL)
3267 			if (strcmp(nvpair_name(nvp),
3268 			    us_field_names[field]) == 0)
3269 				break;
3270 
3271 		type = nvp == NULL ? DATA_TYPE_UNKNOWN : nvpair_type(nvp);
3272 		switch (type) {
3273 		case DATA_TYPE_UINT32:
3274 			val32 = fnvpair_value_uint32(nvp);
3275 			break;
3276 		case DATA_TYPE_UINT64:
3277 			val64 = fnvpair_value_uint64(nvp);
3278 			break;
3279 		case DATA_TYPE_STRING:
3280 			strval = fnvpair_value_string(nvp);
3281 			break;
3282 		case DATA_TYPE_UNKNOWN:
3283 			break;
3284 		default:
3285 			(void) fprintf(stderr, "invalid data type\n");
3286 		}
3287 
3288 		switch (field) {
3289 		case USFIELD_TYPE:
3290 			if (type == DATA_TYPE_UINT32)
3291 				strval = us_type2str(val32);
3292 			break;
3293 		case USFIELD_NAME:
3294 			if (type == DATA_TYPE_UINT64) {
3295 				(void) sprintf(valstr, "%llu",
3296 				    (u_longlong_t)val64);
3297 				strval = valstr;
3298 			}
3299 			break;
3300 		case USFIELD_USED:
3301 		case USFIELD_QUOTA:
3302 			if (type == DATA_TYPE_UINT64) {
3303 				if (parsable) {
3304 					(void) sprintf(valstr, "%llu",
3305 					    (u_longlong_t)val64);
3306 					strval = valstr;
3307 				} else if (field == USFIELD_QUOTA &&
3308 				    val64 == 0) {
3309 					strval = "none";
3310 				} else {
3311 					zfs_nicebytes(val64, valstr,
3312 					    sizeof (valstr));
3313 					strval = valstr;
3314 				}
3315 			}
3316 			break;
3317 		case USFIELD_OBJUSED:
3318 		case USFIELD_OBJQUOTA:
3319 			if (type == DATA_TYPE_UINT64) {
3320 				if (parsable) {
3321 					(void) sprintf(valstr, "%llu",
3322 					    (u_longlong_t)val64);
3323 					strval = valstr;
3324 				} else if (field == USFIELD_OBJQUOTA &&
3325 				    val64 == 0) {
3326 					strval = "none";
3327 				} else {
3328 					zfs_nicenum(val64, valstr,
3329 					    sizeof (valstr));
3330 					strval = valstr;
3331 				}
3332 			}
3333 			break;
3334 		}
3335 
3336 		if (!first) {
3337 			if (scripted)
3338 				(void) putchar('\t');
3339 			else
3340 				(void) fputs("  ", stdout);
3341 		}
3342 		if (scripted)
3343 			(void) fputs(strval, stdout);
3344 		else if (field == USFIELD_TYPE || field == USFIELD_NAME)
3345 			(void) printf("%-*s", (int)width[field], strval);
3346 		else
3347 			(void) printf("%*s", (int)width[field], strval);
3348 
3349 		first = B_FALSE;
3350 		cfield++;
3351 	}
3352 
3353 	(void) putchar('\n');
3354 }
3355 
3356 static void
3357 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
3358     size_t *width, boolean_t rmnode, avl_tree_t *avl)
3359 {
3360 	us_node_t *node;
3361 	const char *col;
3362 	int cfield = 0;
3363 	int field;
3364 
3365 	if (!scripted) {
3366 		boolean_t first = B_TRUE;
3367 
3368 		while ((field = fields[cfield]) != USFIELD_LAST) {
3369 			col = gettext(us_field_hdr[field]);
3370 			if (field == USFIELD_TYPE || field == USFIELD_NAME) {
3371 				(void) printf(first ? "%-*s" : "  %-*s",
3372 				    (int)width[field], col);
3373 			} else {
3374 				(void) printf(first ? "%*s" : "  %*s",
3375 				    (int)width[field], col);
3376 			}
3377 			first = B_FALSE;
3378 			cfield++;
3379 		}
3380 		(void) printf("\n");
3381 	}
3382 
3383 	for (node = avl_first(avl); node; node = AVL_NEXT(avl, node)) {
3384 		print_us_node(scripted, parsable, fields, types, width, node);
3385 		if (rmnode)
3386 			nvlist_free(node->usn_nvl);
3387 	}
3388 }
3389 
3390 static int
3391 zfs_do_userspace(int argc, char **argv)
3392 {
3393 	zfs_handle_t *zhp;
3394 	zfs_userquota_prop_t p;
3395 	char *delim;
3396 	char deffields[] = "type,name,used,quota,objused,objquota";
3397 	char *ofield = NULL;
3398 	char *tfield = NULL;
3399 	int cfield = 0;
3400 	int fields[256];
3401 	int i;
3402 	boolean_t scripted = B_FALSE;
3403 	boolean_t prtnum = B_FALSE;
3404 	boolean_t parsable = B_FALSE;
3405 	boolean_t sid2posix = B_FALSE;
3406 	int ret = 0;
3407 	int c;
3408 	zfs_sort_column_t *sortcol = NULL;
3409 	int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
3410 	us_cbdata_t cb;
3411 	us_node_t *node;
3412 	us_node_t *rmnode;
3413 	list_t list;
3414 	avl_index_t idx = 0;
3415 
3416 	if (argc < 2)
3417 		usage(B_FALSE);
3418 
3419 	if (strcmp(argv[0], "groupspace") == 0) {
3420 		/* Toggle default group types */
3421 		types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
3422 	} else if (strcmp(argv[0], "projectspace") == 0) {
3423 		types = USTYPE_PROJ;
3424 		prtnum = B_TRUE;
3425 	}
3426 
3427 	while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
3428 		switch (c) {
3429 		case 'n':
3430 			if (types == USTYPE_PROJ) {
3431 				(void) fprintf(stderr,
3432 				    gettext("invalid option 'n'\n"));
3433 				usage(B_FALSE);
3434 			}
3435 			prtnum = B_TRUE;
3436 			break;
3437 		case 'H':
3438 			scripted = B_TRUE;
3439 			break;
3440 		case 'p':
3441 			parsable = B_TRUE;
3442 			break;
3443 		case 'o':
3444 			ofield = optarg;
3445 			break;
3446 		case 's':
3447 		case 'S':
3448 			if (zfs_add_sort_column(&sortcol, optarg,
3449 			    c == 's' ? B_FALSE : B_TRUE) != 0) {
3450 				(void) fprintf(stderr,
3451 				    gettext("invalid field '%s'\n"), optarg);
3452 				usage(B_FALSE);
3453 			}
3454 			break;
3455 		case 't':
3456 			if (types == USTYPE_PROJ) {
3457 				(void) fprintf(stderr,
3458 				    gettext("invalid option 't'\n"));
3459 				usage(B_FALSE);
3460 			}
3461 			tfield = optarg;
3462 			break;
3463 		case 'i':
3464 			if (types == USTYPE_PROJ) {
3465 				(void) fprintf(stderr,
3466 				    gettext("invalid option 'i'\n"));
3467 				usage(B_FALSE);
3468 			}
3469 			sid2posix = B_TRUE;
3470 			break;
3471 		case ':':
3472 			(void) fprintf(stderr, gettext("missing argument for "
3473 			    "'%c' option\n"), optopt);
3474 			usage(B_FALSE);
3475 			break;
3476 		case '?':
3477 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3478 			    optopt);
3479 			usage(B_FALSE);
3480 		}
3481 	}
3482 
3483 	argc -= optind;
3484 	argv += optind;
3485 
3486 	if (argc < 1) {
3487 		(void) fprintf(stderr, gettext("missing dataset name\n"));
3488 		usage(B_FALSE);
3489 	}
3490 	if (argc > 1) {
3491 		(void) fprintf(stderr, gettext("too many arguments\n"));
3492 		usage(B_FALSE);
3493 	}
3494 
3495 	/* Use default output fields if not specified using -o */
3496 	if (ofield == NULL)
3497 		ofield = deffields;
3498 	do {
3499 		if ((delim = strchr(ofield, ',')) != NULL)
3500 			*delim = '\0';
3501 		if ((fields[cfield++] = us_field_index(ofield)) == -1) {
3502 			(void) fprintf(stderr, gettext("invalid type '%s' "
3503 			    "for -o option\n"), ofield);
3504 			return (-1);
3505 		}
3506 		if (delim != NULL)
3507 			ofield = delim + 1;
3508 	} while (delim != NULL);
3509 	fields[cfield] = USFIELD_LAST;
3510 
3511 	/* Override output types (-t option) */
3512 	if (tfield != NULL) {
3513 		types = 0;
3514 
3515 		do {
3516 			boolean_t found = B_FALSE;
3517 
3518 			if ((delim = strchr(tfield, ',')) != NULL)
3519 				*delim = '\0';
3520 			for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
3521 			    i++) {
3522 				if (strcmp(tfield, us_type_names[i]) == 0) {
3523 					found = B_TRUE;
3524 					types |= us_type_bits[i];
3525 					break;
3526 				}
3527 			}
3528 			if (!found) {
3529 				(void) fprintf(stderr, gettext("invalid type "
3530 				    "'%s' for -t option\n"), tfield);
3531 				return (-1);
3532 			}
3533 			if (delim != NULL)
3534 				tfield = delim + 1;
3535 		} while (delim != NULL);
3536 	}
3537 
3538 	if ((zhp = zfs_path_to_zhandle(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM |
3539 	    ZFS_TYPE_SNAPSHOT)) == NULL)
3540 		return (1);
3541 	if (zfs_get_underlying_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3542 		(void) fprintf(stderr, gettext("operation is only applicable "
3543 		    "to filesystems and their snapshots\n"));
3544 		zfs_close(zhp);
3545 		return (1);
3546 	}
3547 
3548 	/* Always add default sorting columns */
3549 	(void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
3550 	(void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
3551 
3552 	cb.cb_sortcol = sortcol;
3553 	cb.cb_numname = prtnum;
3554 	cb.cb_nicenum = !parsable;
3555 	cb.cb_sid2posix = sid2posix;
3556 
3557 	avl_create(&cb.cb_avl, us_compare,
3558 	    sizeof (us_node_t), offsetof(us_node_t, usn_avlnode));
3559 
3560 
3561 	for (i = 0; i < USFIELD_LAST; i++)
3562 		cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
3563 
3564 	for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
3565 		if ((zfs_prop_is_user(p) &&
3566 		    !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
3567 		    (zfs_prop_is_group(p) &&
3568 		    !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))) ||
3569 		    (zfs_prop_is_project(p) && types != USTYPE_PROJ))
3570 			continue;
3571 
3572 		cb.cb_prop = p;
3573 		if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0) {
3574 			zfs_close(zhp);
3575 			avl_destroy(&cb.cb_avl);
3576 			return (ret);
3577 		}
3578 	}
3579 	zfs_close(zhp);
3580 
3581 	/* Sort the list */
3582 	if ((node = avl_first(&cb.cb_avl)) == NULL) {
3583 		avl_destroy(&cb.cb_avl);
3584 		return (0);
3585 	}
3586 
3587 	us_populated = B_TRUE;
3588 
3589 	list_create(&list, sizeof (us_node_t),
3590 	    offsetof(us_node_t, usn_listnode));
3591 	list_link_init(&node->usn_listnode);
3592 
3593 	while (node != NULL) {
3594 		rmnode = node;
3595 		node = AVL_NEXT(&cb.cb_avl, node);
3596 		avl_remove(&cb.cb_avl, rmnode);
3597 		list_insert_head(&list, rmnode);
3598 	}
3599 
3600 	for (node = list_head(&list); node != NULL;
3601 	    node = list_next(&list, node)) {
3602 		if (avl_find(&cb.cb_avl, node, &idx) == NULL)
3603 			avl_insert(&cb.cb_avl, node, idx);
3604 	}
3605 
3606 	while ((node = list_remove_head(&list)) != NULL) { }
3607 	list_destroy(&list);
3608 
3609 	/* Print and free node nvlist memory */
3610 	print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
3611 	    &cb.cb_avl);
3612 
3613 	zfs_free_sort_columns(sortcol);
3614 
3615 	/* Clean up the AVL tree */
3616 	void *cookie = NULL;
3617 	while ((node = avl_destroy_nodes(&cb.cb_avl, &cookie)) != NULL) {
3618 		free(node);
3619 	}
3620 	avl_destroy(&cb.cb_avl);
3621 
3622 	return (ret);
3623 }
3624 
3625 /*
3626  * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
3627  *      [-t type[,...]] [filesystem|volume|snapshot] ...
3628  *
3629  *	-H	Scripted mode; elide headers and separate columns by tabs
3630  *	-p	Display values in parsable (literal) format.
3631  *	-r	Recurse over all children
3632  *	-d	Limit recursion by depth.
3633  *	-o	Control which fields to display.
3634  *	-s	Specify sort columns, descending order.
3635  *	-S	Specify sort columns, ascending order.
3636  *	-t	Control which object types to display.
3637  *
3638  * When given no arguments, list all filesystems in the system.
3639  * Otherwise, list the specified datasets, optionally recursing down them if
3640  * '-r' is specified.
3641  */
3642 typedef struct list_cbdata {
3643 	boolean_t	cb_first;
3644 	boolean_t	cb_literal;
3645 	boolean_t	cb_scripted;
3646 	zprop_list_t	*cb_proplist;
3647 	boolean_t	cb_json;
3648 	nvlist_t	*cb_jsobj;
3649 	boolean_t	cb_json_as_int;
3650 } list_cbdata_t;
3651 
3652 /*
3653  * Given a list of columns to display, output appropriate headers for each one.
3654  */
3655 static void
3656 print_header(list_cbdata_t *cb)
3657 {
3658 	zprop_list_t *pl = cb->cb_proplist;
3659 	char headerbuf[ZFS_MAXPROPLEN];
3660 	const char *header;
3661 	int i;
3662 	boolean_t first = B_TRUE;
3663 	boolean_t right_justify;
3664 
3665 	color_start(ANSI_BOLD);
3666 
3667 	for (; pl != NULL; pl = pl->pl_next) {
3668 		if (!first) {
3669 			(void) printf("  ");
3670 		} else {
3671 			first = B_FALSE;
3672 		}
3673 
3674 		right_justify = B_FALSE;
3675 		if (pl->pl_prop != ZPROP_USERPROP) {
3676 			header = zfs_prop_column_name(pl->pl_prop);
3677 			right_justify = zfs_prop_align_right(pl->pl_prop);
3678 		} else {
3679 			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
3680 				headerbuf[i] = toupper(pl->pl_user_prop[i]);
3681 			headerbuf[i] = '\0';
3682 			header = headerbuf;
3683 		}
3684 
3685 		if (pl->pl_next == NULL && !right_justify)
3686 			(void) printf("%s", header);
3687 		else if (right_justify)
3688 			(void) printf("%*s", (int)pl->pl_width, header);
3689 		else
3690 			(void) printf("%-*s", (int)pl->pl_width, header);
3691 	}
3692 
3693 	color_end();
3694 
3695 	(void) printf("\n");
3696 }
3697 
3698 /*
3699  * Decides on the color that the avail value should be printed in.
3700  * > 80% used = yellow
3701  * > 90% used = red
3702  */
3703 static const char *
3704 zfs_list_avail_color(zfs_handle_t *zhp)
3705 {
3706 	uint64_t used = zfs_prop_get_int(zhp, ZFS_PROP_USED);
3707 	uint64_t avail = zfs_prop_get_int(zhp, ZFS_PROP_AVAILABLE);
3708 	int percentage = (int)((double)avail / MAX(avail + used, 1) * 100);
3709 
3710 	if (percentage > 20)
3711 		return (NULL);
3712 	else if (percentage > 10)
3713 		return (ANSI_YELLOW);
3714 	else
3715 		return (ANSI_RED);
3716 }
3717 
3718 /*
3719  * Given a dataset and a list of fields, print out all the properties according
3720  * to the described layout, or return an nvlist containing all the fields, later
3721  * to be printed out as JSON object.
3722  */
3723 static void
3724 collect_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
3725 {
3726 	zprop_list_t *pl = cb->cb_proplist;
3727 	boolean_t first = B_TRUE;
3728 	char property[ZFS_MAXPROPLEN];
3729 	nvlist_t *userprops = zfs_get_user_props(zhp);
3730 	nvlist_t *propval;
3731 	const char *propstr;
3732 	boolean_t right_justify;
3733 	nvlist_t *item, *d, *props;
3734 	item = d = props = NULL;
3735 	zprop_source_t sourcetype = ZPROP_SRC_NONE;
3736 	char source[ZFS_MAX_DATASET_NAME_LEN];
3737 	if (cb->cb_json) {
3738 		d = fnvlist_lookup_nvlist(cb->cb_jsobj, "datasets");
3739 		if (d == NULL) {
3740 			fprintf(stderr, "datasets obj not found.\n");
3741 			exit(1);
3742 		}
3743 		item = fnvlist_alloc();
3744 		props = fnvlist_alloc();
3745 		fill_dataset_info(item, zhp, cb->cb_json_as_int);
3746 	}
3747 
3748 	for (; pl != NULL; pl = pl->pl_next) {
3749 		if (!cb->cb_json && !first) {
3750 			if (cb->cb_scripted)
3751 				(void) putchar('\t');
3752 			else
3753 				(void) fputs("  ", stdout);
3754 		} else {
3755 			first = B_FALSE;
3756 		}
3757 
3758 		if (pl->pl_prop == ZFS_PROP_NAME) {
3759 			(void) strlcpy(property, zfs_get_name(zhp),
3760 			    sizeof (property));
3761 			propstr = property;
3762 			right_justify = zfs_prop_align_right(pl->pl_prop);
3763 		} else if (pl->pl_prop != ZPROP_USERPROP) {
3764 			if (zfs_prop_get(zhp, pl->pl_prop, property,
3765 			    sizeof (property), &sourcetype, source,
3766 			    sizeof (source), cb->cb_literal) != 0)
3767 				propstr = "-";
3768 			else
3769 				propstr = property;
3770 			right_justify = zfs_prop_align_right(pl->pl_prop);
3771 		} else if (zfs_prop_userquota(pl->pl_user_prop)) {
3772 			sourcetype = ZPROP_SRC_LOCAL;
3773 			if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3774 			    property, sizeof (property), cb->cb_literal) != 0) {
3775 				sourcetype = ZPROP_SRC_NONE;
3776 				propstr = "-";
3777 			} else {
3778 				propstr = property;
3779 			}
3780 			right_justify = B_TRUE;
3781 		} else if (zfs_prop_written(pl->pl_user_prop)) {
3782 			sourcetype = ZPROP_SRC_LOCAL;
3783 			if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3784 			    property, sizeof (property), cb->cb_literal) != 0) {
3785 				sourcetype = ZPROP_SRC_NONE;
3786 				propstr = "-";
3787 			} else {
3788 				propstr = property;
3789 			}
3790 			right_justify = B_TRUE;
3791 		} else {
3792 			if (nvlist_lookup_nvlist(userprops,
3793 			    pl->pl_user_prop, &propval) != 0) {
3794 				propstr = "-";
3795 			} else {
3796 				propstr = fnvlist_lookup_string(propval,
3797 				    ZPROP_VALUE);
3798 				strlcpy(source,
3799 				    fnvlist_lookup_string(propval,
3800 				    ZPROP_SOURCE), ZFS_MAX_DATASET_NAME_LEN);
3801 				if (strcmp(source,
3802 				    zfs_get_name(zhp)) == 0) {
3803 					sourcetype = ZPROP_SRC_LOCAL;
3804 				} else if (strcmp(source,
3805 				    ZPROP_SOURCE_VAL_RECVD) == 0) {
3806 					sourcetype = ZPROP_SRC_RECEIVED;
3807 				} else {
3808 					sourcetype = ZPROP_SRC_INHERITED;
3809 				}
3810 			}
3811 			right_justify = B_FALSE;
3812 		}
3813 
3814 		if (cb->cb_json) {
3815 			if (pl->pl_prop == ZFS_PROP_NAME)
3816 				continue;
3817 			const char *prop_name;
3818 			if (pl->pl_prop != ZPROP_USERPROP)
3819 				prop_name = zfs_prop_to_name(pl->pl_prop);
3820 			else
3821 				prop_name = pl->pl_user_prop;
3822 			if (zprop_nvlist_one_property(
3823 			    prop_name, propstr,
3824 			    sourcetype, source, NULL, props,
3825 			    cb->cb_json_as_int) != 0)
3826 				nomem();
3827 		} else {
3828 			/*
3829 			 * zfs_list_avail_color() needs
3830 			 * ZFS_PROP_AVAILABLE + USED, so we need another
3831 			 * for() search for the USED part when no colors
3832 			 * wanted, we can skip the whole thing
3833 			 */
3834 			if (use_color() && pl->pl_prop == ZFS_PROP_AVAILABLE) {
3835 				zprop_list_t *pl2 = cb->cb_proplist;
3836 				for (; pl2 != NULL; pl2 = pl2->pl_next) {
3837 					if (pl2->pl_prop == ZFS_PROP_USED) {
3838 						color_start(
3839 						    zfs_list_avail_color(zhp));
3840 						/*
3841 						 * found it, no need for more
3842 						 * loops
3843 						 */
3844 						break;
3845 					}
3846 				}
3847 			}
3848 
3849 			/*
3850 			 * If this is being called in scripted mode, or if
3851 			 * this is the last column and it is left-justified,
3852 			 * don't include a width format specifier.
3853 			 */
3854 			if (cb->cb_scripted || (pl->pl_next == NULL &&
3855 			    !right_justify))
3856 				(void) fputs(propstr, stdout);
3857 			else if (right_justify) {
3858 				(void) printf("%*s", (int)pl->pl_width,
3859 				    propstr);
3860 			} else {
3861 				(void) printf("%-*s", (int)pl->pl_width,
3862 				    propstr);
3863 			}
3864 
3865 			if (pl->pl_prop == ZFS_PROP_AVAILABLE)
3866 				color_end();
3867 		}
3868 	}
3869 	if (cb->cb_json) {
3870 		fnvlist_add_nvlist(item, "properties", props);
3871 		fnvlist_add_nvlist(d, zfs_get_name(zhp), item);
3872 		fnvlist_free(props);
3873 		fnvlist_free(item);
3874 	} else
3875 		(void) putchar('\n');
3876 }
3877 
3878 /*
3879  * Generic callback function to list a dataset or snapshot.
3880  */
3881 static int
3882 list_callback(zfs_handle_t *zhp, void *data)
3883 {
3884 	list_cbdata_t *cbp = data;
3885 
3886 	if (cbp->cb_first) {
3887 		if (!cbp->cb_scripted && !cbp->cb_json)
3888 			print_header(cbp);
3889 		cbp->cb_first = B_FALSE;
3890 	}
3891 
3892 	collect_dataset(zhp, cbp);
3893 
3894 	return (0);
3895 }
3896 
3897 static int
3898 zfs_do_list(int argc, char **argv)
3899 {
3900 	int c;
3901 	char default_fields[] =
3902 	    "name,used,available,referenced,mountpoint";
3903 	int types = ZFS_TYPE_DATASET;
3904 	boolean_t types_specified = B_FALSE;
3905 	char *fields = default_fields;
3906 	list_cbdata_t cb = { 0 };
3907 	int limit = 0;
3908 	int ret = 0;
3909 	zfs_sort_column_t *sortcol = NULL;
3910 	int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3911 	nvlist_t *data = NULL;
3912 
3913 	struct option long_options[] = {
3914 		{"json", no_argument, NULL, 'j'},
3915 		{"json-int", no_argument, NULL, ZFS_OPTION_JSON_NUMS_AS_INT},
3916 		{0, 0, 0, 0}
3917 	};
3918 
3919 	/* check options */
3920 	while ((c = getopt_long(argc, argv, "jHS:d:o:prs:t:", long_options,
3921 	    NULL)) != -1) {
3922 		switch (c) {
3923 		case 'o':
3924 			fields = optarg;
3925 			break;
3926 		case 'p':
3927 			cb.cb_literal = B_TRUE;
3928 			flags |= ZFS_ITER_LITERAL_PROPS;
3929 			break;
3930 		case 'd':
3931 			limit = parse_depth(optarg, &flags);
3932 			break;
3933 		case 'r':
3934 			flags |= ZFS_ITER_RECURSE;
3935 			break;
3936 		case 'j':
3937 			cb.cb_json = B_TRUE;
3938 			cb.cb_jsobj = zfs_json_schema(0, 1);
3939 			data = fnvlist_alloc();
3940 			fnvlist_add_nvlist(cb.cb_jsobj, "datasets", data);
3941 			fnvlist_free(data);
3942 			break;
3943 		case ZFS_OPTION_JSON_NUMS_AS_INT:
3944 			cb.cb_json_as_int = B_TRUE;
3945 			cb.cb_literal = B_TRUE;
3946 			break;
3947 		case 'H':
3948 			cb.cb_scripted = B_TRUE;
3949 			break;
3950 		case 's':
3951 			if (zfs_add_sort_column(&sortcol, optarg,
3952 			    B_FALSE) != 0) {
3953 				(void) fprintf(stderr,
3954 				    gettext("invalid property '%s'\n"), optarg);
3955 				usage(B_FALSE);
3956 			}
3957 			break;
3958 		case 'S':
3959 			if (zfs_add_sort_column(&sortcol, optarg,
3960 			    B_TRUE) != 0) {
3961 				(void) fprintf(stderr,
3962 				    gettext("invalid property '%s'\n"), optarg);
3963 				usage(B_FALSE);
3964 			}
3965 			break;
3966 		case 't':
3967 			types = 0;
3968 			types_specified = B_TRUE;
3969 			flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3970 
3971 			for (char *tok; (tok = strsep(&optarg, ",")); ) {
3972 				static const char *const type_subopts[] = {
3973 					"filesystem",
3974 					"fs",
3975 					"volume",
3976 					"vol",
3977 					"snapshot",
3978 					"snap",
3979 					"bookmark",
3980 					"all"
3981 				};
3982 				static const int type_types[] = {
3983 					ZFS_TYPE_FILESYSTEM,
3984 					ZFS_TYPE_FILESYSTEM,
3985 					ZFS_TYPE_VOLUME,
3986 					ZFS_TYPE_VOLUME,
3987 					ZFS_TYPE_SNAPSHOT,
3988 					ZFS_TYPE_SNAPSHOT,
3989 					ZFS_TYPE_BOOKMARK,
3990 					ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK
3991 				};
3992 
3993 				for (c = 0; c < ARRAY_SIZE(type_subopts); ++c)
3994 					if (strcmp(tok, type_subopts[c]) == 0) {
3995 						types |= type_types[c];
3996 						goto found3;
3997 					}
3998 
3999 				(void) fprintf(stderr,
4000 				    gettext("invalid type '%s'\n"), tok);
4001 				usage(B_FALSE);
4002 found3:;
4003 			}
4004 			break;
4005 		case ':':
4006 			(void) fprintf(stderr, gettext("missing argument for "
4007 			    "'%c' option\n"), optopt);
4008 			usage(B_FALSE);
4009 			break;
4010 		case '?':
4011 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4012 			    optopt);
4013 			usage(B_FALSE);
4014 		}
4015 	}
4016 
4017 	argc -= optind;
4018 	argv += optind;
4019 
4020 	if (!cb.cb_json && cb.cb_json_as_int) {
4021 		(void) fprintf(stderr, gettext("'--json-int' only works with"
4022 		    " '-j' option\n"));
4023 		usage(B_FALSE);
4024 	}
4025 
4026 	/*
4027 	 * If "-o space" and no types were specified, don't display snapshots.
4028 	 */
4029 	if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
4030 		types &= ~ZFS_TYPE_SNAPSHOT;
4031 
4032 	/*
4033 	 * Handle users who want to list all snapshots or bookmarks
4034 	 * of the current dataset (ex. 'zfs list -t snapshot <dataset>').
4035 	 */
4036 	if ((types == ZFS_TYPE_SNAPSHOT || types == ZFS_TYPE_BOOKMARK) &&
4037 	    argc > 0 && (flags & ZFS_ITER_RECURSE) == 0 && limit == 0) {
4038 		flags |= (ZFS_ITER_DEPTH_LIMIT | ZFS_ITER_RECURSE);
4039 		limit = 1;
4040 	}
4041 
4042 	/*
4043 	 * If the user specifies '-o all', the zprop_get_list() doesn't
4044 	 * normally include the name of the dataset.  For 'zfs list', we always
4045 	 * want this property to be first.
4046 	 */
4047 	if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
4048 	    != 0)
4049 		usage(B_FALSE);
4050 
4051 	cb.cb_first = B_TRUE;
4052 
4053 	/*
4054 	 * If we are only going to list and sort by properties that are "fast"
4055 	 * then we can use "simple" mode and avoid populating the properties
4056 	 * nvlist.
4057 	 */
4058 	if (zfs_list_only_by_fast(cb.cb_proplist) &&
4059 	    zfs_sort_only_by_fast(sortcol))
4060 		flags |= ZFS_ITER_SIMPLE;
4061 
4062 	ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
4063 	    limit, list_callback, &cb);
4064 
4065 	if (ret == 0 && cb.cb_json)
4066 		zcmd_print_json(cb.cb_jsobj);
4067 	else if (ret != 0 && cb.cb_json)
4068 		nvlist_free(cb.cb_jsobj);
4069 
4070 	zprop_free_list(cb.cb_proplist);
4071 	zfs_free_sort_columns(sortcol);
4072 
4073 	if (ret == 0 && cb.cb_first && !cb.cb_scripted)
4074 		(void) fprintf(stderr, gettext("no datasets available\n"));
4075 
4076 	return (ret);
4077 }
4078 
4079 /*
4080  * zfs rename [-fu] <fs | snap | vol> <fs | snap | vol>
4081  * zfs rename [-f] -p <fs | vol> <fs | vol>
4082  * zfs rename [-u] -r <snap> <snap>
4083  *
4084  * Renames the given dataset to another of the same type.
4085  *
4086  * The '-p' flag creates all the non-existing ancestors of the target first.
4087  * If repeated twice, the ancestors are created with `canmount=off`.
4088  *
4089  * The '-u' flag prevents file systems from being remounted during rename.
4090  */
4091 static int
4092 zfs_do_rename(int argc, char **argv)
4093 {
4094 	zfs_handle_t *zhp;
4095 	renameflags_t flags = { 0 };
4096 	int c;
4097 	int ret = 1;
4098 	int types;
4099 	boolean_t parents = B_FALSE;
4100 	boolean_t parents_nomount = B_FALSE;
4101 	nvlist_t *props_parents = NULL;
4102 
4103 	/* check options */
4104 	while ((c = getopt(argc, argv, "pruf")) != -1) {
4105 		switch (c) {
4106 		case 'p':
4107 			if (parents)
4108 				parents_nomount = B_TRUE;
4109 			else
4110 				parents = B_TRUE;
4111 			break;
4112 		case 'r':
4113 			flags.recursive = B_TRUE;
4114 			break;
4115 		case 'u':
4116 			flags.nounmount = B_TRUE;
4117 			break;
4118 		case 'f':
4119 			flags.forceunmount = B_TRUE;
4120 			break;
4121 		case '?':
4122 		default:
4123 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4124 			    optopt);
4125 			usage(B_FALSE);
4126 		}
4127 	}
4128 
4129 	argc -= optind;
4130 	argv += optind;
4131 
4132 	/* check number of arguments */
4133 	if (argc < 1) {
4134 		(void) fprintf(stderr, gettext("missing source dataset "
4135 		    "argument\n"));
4136 		usage(B_FALSE);
4137 	}
4138 	if (argc < 2) {
4139 		(void) fprintf(stderr, gettext("missing target dataset "
4140 		    "argument\n"));
4141 		usage(B_FALSE);
4142 	}
4143 	if (argc > 2) {
4144 		(void) fprintf(stderr, gettext("too many arguments\n"));
4145 		usage(B_FALSE);
4146 	}
4147 
4148 	if (flags.recursive && parents) {
4149 		(void) fprintf(stderr, gettext("-p and -r options are mutually "
4150 		    "exclusive\n"));
4151 		usage(B_FALSE);
4152 	}
4153 
4154 	if (flags.nounmount && parents) {
4155 		(void) fprintf(stderr, gettext("-u and -p options are mutually "
4156 		    "exclusive\n"));
4157 		usage(B_FALSE);
4158 	}
4159 
4160 	if (flags.recursive && strchr(argv[0], '@') == 0) {
4161 		(void) fprintf(stderr, gettext("source dataset for recursive "
4162 		    "rename must be a snapshot\n"));
4163 		usage(B_FALSE);
4164 	}
4165 
4166 	if (flags.nounmount)
4167 		types = ZFS_TYPE_FILESYSTEM;
4168 	else if (parents)
4169 		types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
4170 	else
4171 		types = ZFS_TYPE_DATASET;
4172 
4173 	if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
4174 		goto error_open;
4175 
4176 	/* If we were asked and the name looks good, try to create ancestors. */
4177 	if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp))) {
4178 
4179 		makeprops_parents(&props_parents, parents_nomount);
4180 		if (zfs_create_ancestors_props(g_zfs, argv[1],
4181 		    props_parents) != 0) {
4182 			goto error;
4183 		}
4184 	}
4185 
4186 	ret = (zfs_rename(zhp, argv[1], flags) != 0);
4187 
4188 error:
4189 	zfs_close(zhp);
4190 error_open:
4191 	nvlist_free(props_parents);
4192 	return (ret);
4193 }
4194 
4195 /*
4196  * zfs promote <fs>
4197  *
4198  * Promotes the given clone fs to be the parent
4199  */
4200 static int
4201 zfs_do_promote(int argc, char **argv)
4202 {
4203 	zfs_handle_t *zhp;
4204 	int ret = 0;
4205 
4206 	/* check options */
4207 	if (argc > 1 && argv[1][0] == '-') {
4208 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4209 		    argv[1][1]);
4210 		usage(B_FALSE);
4211 	}
4212 
4213 	/* check number of arguments */
4214 	if (argc < 2) {
4215 		(void) fprintf(stderr, gettext("missing clone filesystem"
4216 		    " argument\n"));
4217 		usage(B_FALSE);
4218 	}
4219 	if (argc > 2) {
4220 		(void) fprintf(stderr, gettext("too many arguments\n"));
4221 		usage(B_FALSE);
4222 	}
4223 
4224 	zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4225 	if (zhp == NULL)
4226 		return (1);
4227 
4228 	ret = (zfs_promote(zhp) != 0);
4229 
4230 
4231 	zfs_close(zhp);
4232 	return (ret);
4233 }
4234 
4235 static int
4236 zfs_do_redact(int argc, char **argv)
4237 {
4238 	char *snap = NULL;
4239 	char *bookname = NULL;
4240 	char **rsnaps = NULL;
4241 	int numrsnaps = 0;
4242 	argv++;
4243 	argc--;
4244 	if (argc < 3) {
4245 		(void) fprintf(stderr, gettext("too few arguments\n"));
4246 		usage(B_FALSE);
4247 	}
4248 
4249 	snap = argv[0];
4250 	bookname = argv[1];
4251 	rsnaps = argv + 2;
4252 	numrsnaps = argc - 2;
4253 
4254 	nvlist_t *rsnapnv = fnvlist_alloc();
4255 
4256 	for (int i = 0; i < numrsnaps; i++) {
4257 		fnvlist_add_boolean(rsnapnv, rsnaps[i]);
4258 	}
4259 
4260 	int err = lzc_redact(snap, bookname, rsnapnv);
4261 	fnvlist_free(rsnapnv);
4262 
4263 	switch (err) {
4264 	case 0:
4265 		break;
4266 	case ENOENT: {
4267 		zfs_handle_t *zhp = zfs_open(g_zfs, snap, ZFS_TYPE_SNAPSHOT);
4268 		if (zhp == NULL) {
4269 			(void) fprintf(stderr, gettext("provided snapshot %s "
4270 			    "does not exist\n"), snap);
4271 		} else {
4272 			zfs_close(zhp);
4273 		}
4274 		for (int i = 0; i < numrsnaps; i++) {
4275 			zhp = zfs_open(g_zfs, rsnaps[i], ZFS_TYPE_SNAPSHOT);
4276 			if (zhp == NULL) {
4277 				(void) fprintf(stderr, gettext("provided "
4278 				    "snapshot %s does not exist\n"), rsnaps[i]);
4279 			} else {
4280 				zfs_close(zhp);
4281 			}
4282 		}
4283 		break;
4284 	}
4285 	case EEXIST:
4286 		(void) fprintf(stderr, gettext("specified redaction bookmark "
4287 		    "(%s) provided already exists\n"), bookname);
4288 		break;
4289 	case ENAMETOOLONG:
4290 		(void) fprintf(stderr, gettext("provided bookmark name cannot "
4291 		    "be used, final name would be too long\n"));
4292 		break;
4293 	case E2BIG:
4294 		(void) fprintf(stderr, gettext("too many redaction snapshots "
4295 		    "specified\n"));
4296 		break;
4297 	case EINVAL:
4298 		if (strchr(bookname, '#') != NULL)
4299 			(void) fprintf(stderr, gettext(
4300 			    "redaction bookmark name must not contain '#'\n"));
4301 		else
4302 			(void) fprintf(stderr, gettext(
4303 			    "redaction snapshot must be descendent of "
4304 			    "snapshot being redacted\n"));
4305 		break;
4306 	case EALREADY:
4307 		(void) fprintf(stderr, gettext("attempted to redact redacted "
4308 		    "dataset or with respect to redacted dataset\n"));
4309 		break;
4310 	case ENOTSUP:
4311 		(void) fprintf(stderr, gettext("redaction bookmarks feature "
4312 		    "not enabled\n"));
4313 		break;
4314 	case EXDEV:
4315 		(void) fprintf(stderr, gettext("potentially invalid redaction "
4316 		    "snapshot; full dataset names required\n"));
4317 		break;
4318 	case ESRCH:
4319 		(void) fprintf(stderr, gettext("attempted to resume redaction "
4320 		    " with a mismatched redaction list\n"));
4321 		break;
4322 	default:
4323 		(void) fprintf(stderr, gettext("internal error: %s\n"),
4324 		    strerror(errno));
4325 	}
4326 
4327 	return (err);
4328 }
4329 
4330 /*
4331  * zfs rollback [-rRf] <snapshot>
4332  *
4333  *	-r	Delete any intervening snapshots before doing rollback
4334  *	-R	Delete any snapshots and their clones
4335  *	-f	ignored for backwards compatibility
4336  *
4337  * Given a filesystem, rollback to a specific snapshot, discarding any changes
4338  * since then and making it the active dataset.  If more recent snapshots exist,
4339  * the command will complain unless the '-r' flag is given.
4340  */
4341 typedef struct rollback_cbdata {
4342 	uint64_t	cb_create;
4343 	uint8_t		cb_younger_ds_printed;
4344 	boolean_t	cb_first;
4345 	int		cb_doclones;
4346 	char		*cb_target;
4347 	int		cb_error;
4348 	boolean_t	cb_recurse;
4349 } rollback_cbdata_t;
4350 
4351 static int
4352 rollback_check_dependent(zfs_handle_t *zhp, void *data)
4353 {
4354 	rollback_cbdata_t *cbp = data;
4355 
4356 	if (cbp->cb_first && cbp->cb_recurse) {
4357 		(void) fprintf(stderr, gettext("cannot rollback to "
4358 		    "'%s': clones of previous snapshots exist\n"),
4359 		    cbp->cb_target);
4360 		(void) fprintf(stderr, gettext("use '-R' to "
4361 		    "force deletion of the following clones and "
4362 		    "dependents:\n"));
4363 		cbp->cb_first = 0;
4364 		cbp->cb_error = 1;
4365 	}
4366 
4367 	(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
4368 
4369 	zfs_close(zhp);
4370 	return (0);
4371 }
4372 
4373 
4374 /*
4375  * Report some snapshots/bookmarks more recent than the one specified.
4376  * Used when '-r' is not specified. We reuse this same callback for the
4377  * snapshot dependents - if 'cb_dependent' is set, then this is a
4378  * dependent and we should report it without checking the transaction group.
4379  */
4380 static int
4381 rollback_check(zfs_handle_t *zhp, void *data)
4382 {
4383 	rollback_cbdata_t *cbp = data;
4384 	/*
4385 	 * Max number of younger snapshots and/or bookmarks to display before
4386 	 * we stop the iteration.
4387 	 */
4388 	const uint8_t max_younger = 32;
4389 
4390 	if (cbp->cb_doclones) {
4391 		zfs_close(zhp);
4392 		return (0);
4393 	}
4394 
4395 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
4396 		if (cbp->cb_first && !cbp->cb_recurse) {
4397 			(void) fprintf(stderr, gettext("cannot "
4398 			    "rollback to '%s': more recent snapshots "
4399 			    "or bookmarks exist\n"),
4400 			    cbp->cb_target);
4401 			(void) fprintf(stderr, gettext("use '-r' to "
4402 			    "force deletion of the following "
4403 			    "snapshots and bookmarks:\n"));
4404 			cbp->cb_first = 0;
4405 			cbp->cb_error = 1;
4406 		}
4407 
4408 		if (cbp->cb_recurse) {
4409 			if (zfs_iter_dependents_v2(zhp, 0, B_TRUE,
4410 			    rollback_check_dependent, cbp) != 0) {
4411 				zfs_close(zhp);
4412 				return (-1);
4413 			}
4414 		} else {
4415 			(void) fprintf(stderr, "%s\n",
4416 			    zfs_get_name(zhp));
4417 			cbp->cb_younger_ds_printed++;
4418 		}
4419 	}
4420 	zfs_close(zhp);
4421 
4422 	if (cbp->cb_younger_ds_printed == max_younger) {
4423 		/*
4424 		 * This non-recursive rollback is going to fail due to the
4425 		 * presence of snapshots and/or bookmarks that are younger than
4426 		 * the rollback target.
4427 		 * We printed some of the offending objects, now we stop
4428 		 * zfs_iter_snapshot/bookmark iteration so we can fail fast and
4429 		 * avoid iterating over the rest of the younger objects
4430 		 */
4431 		(void) fprintf(stderr, gettext("Output limited to %d "
4432 		    "snapshots/bookmarks\n"), max_younger);
4433 		return (-1);
4434 	}
4435 	return (0);
4436 }
4437 
4438 static int
4439 zfs_do_rollback(int argc, char **argv)
4440 {
4441 	int ret = 0;
4442 	int c;
4443 	boolean_t force = B_FALSE;
4444 	rollback_cbdata_t cb = { 0 };
4445 	zfs_handle_t *zhp, *snap;
4446 	char parentname[ZFS_MAX_DATASET_NAME_LEN];
4447 	char *delim;
4448 	uint64_t min_txg = 0;
4449 
4450 	/* check options */
4451 	while ((c = getopt(argc, argv, "rRf")) != -1) {
4452 		switch (c) {
4453 		case 'r':
4454 			cb.cb_recurse = 1;
4455 			break;
4456 		case 'R':
4457 			cb.cb_recurse = 1;
4458 			cb.cb_doclones = 1;
4459 			break;
4460 		case 'f':
4461 			force = B_TRUE;
4462 			break;
4463 		case '?':
4464 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4465 			    optopt);
4466 			usage(B_FALSE);
4467 		}
4468 	}
4469 
4470 	argc -= optind;
4471 	argv += optind;
4472 
4473 	/* check number of arguments */
4474 	if (argc < 1) {
4475 		(void) fprintf(stderr, gettext("missing dataset argument\n"));
4476 		usage(B_FALSE);
4477 	}
4478 	if (argc > 1) {
4479 		(void) fprintf(stderr, gettext("too many arguments\n"));
4480 		usage(B_FALSE);
4481 	}
4482 
4483 	/* open the snapshot */
4484 	if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
4485 		return (1);
4486 
4487 	/* open the parent dataset */
4488 	(void) strlcpy(parentname, argv[0], sizeof (parentname));
4489 	verify((delim = strrchr(parentname, '@')) != NULL);
4490 	*delim = '\0';
4491 	if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
4492 		zfs_close(snap);
4493 		return (1);
4494 	}
4495 
4496 	/*
4497 	 * Check for more recent snapshots and/or clones based on the presence
4498 	 * of '-r' and '-R'.
4499 	 */
4500 	cb.cb_target = argv[0];
4501 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
4502 	cb.cb_first = B_TRUE;
4503 	cb.cb_error = 0;
4504 
4505 	if (cb.cb_create > 0)
4506 		min_txg = cb.cb_create;
4507 
4508 	if ((ret = zfs_iter_snapshots_sorted_v2(zhp, 0, rollback_check, &cb,
4509 	    min_txg, 0)) != 0)
4510 		goto out;
4511 	if ((ret = zfs_iter_bookmarks_v2(zhp, 0, rollback_check, &cb)) != 0)
4512 		goto out;
4513 
4514 	if ((ret = cb.cb_error) != 0)
4515 		goto out;
4516 
4517 	/*
4518 	 * Rollback parent to the given snapshot.
4519 	 */
4520 	ret = zfs_rollback(zhp, snap, force);
4521 
4522 out:
4523 	zfs_close(snap);
4524 	zfs_close(zhp);
4525 
4526 	if (ret == 0)
4527 		return (0);
4528 	else
4529 		return (1);
4530 }
4531 
4532 /*
4533  * zfs set property=value ... { fs | snap | vol } ...
4534  *
4535  * Sets the given properties for all datasets specified on the command line.
4536  */
4537 
4538 static int
4539 set_callback(zfs_handle_t *zhp, void *data)
4540 {
4541 	zprop_set_cbdata_t *cb = data;
4542 	int ret = zfs_prop_set_list_flags(zhp, cb->cb_proplist, cb->cb_flags);
4543 
4544 	if (ret != 0 || libzfs_errno(g_zfs) != EZFS_SUCCESS) {
4545 		switch (libzfs_errno(g_zfs)) {
4546 		case EZFS_MOUNTFAILED:
4547 			(void) fprintf(stderr, gettext("property may be set "
4548 			    "but unable to remount filesystem\n"));
4549 			break;
4550 		case EZFS_SHARENFSFAILED:
4551 			(void) fprintf(stderr, gettext("property may be set "
4552 			    "but unable to reshare filesystem\n"));
4553 			break;
4554 		}
4555 	}
4556 	return (ret);
4557 }
4558 
4559 static int
4560 zfs_do_set(int argc, char **argv)
4561 {
4562 	zprop_set_cbdata_t cb = { 0 };
4563 	int ds_start = -1; /* argv idx of first dataset arg */
4564 	int ret = 0;
4565 	int i, c;
4566 
4567 	/* check options */
4568 	while ((c = getopt(argc, argv, "u")) != -1) {
4569 		switch (c) {
4570 		case 'u':
4571 			cb.cb_flags |= ZFS_SET_NOMOUNT;
4572 			break;
4573 		case '?':
4574 		default:
4575 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4576 			    optopt);
4577 			usage(B_FALSE);
4578 		}
4579 	}
4580 
4581 	argc -= optind;
4582 	argv += optind;
4583 
4584 	/* check number of arguments */
4585 	if (argc < 1) {
4586 		(void) fprintf(stderr, gettext("missing arguments\n"));
4587 		usage(B_FALSE);
4588 	}
4589 	if (argc < 2) {
4590 		if (strchr(argv[0], '=') == NULL) {
4591 			(void) fprintf(stderr, gettext("missing property=value "
4592 			    "argument(s)\n"));
4593 		} else {
4594 			(void) fprintf(stderr, gettext("missing dataset "
4595 			    "name(s)\n"));
4596 		}
4597 		usage(B_FALSE);
4598 	}
4599 
4600 	/* validate argument order:  prop=val args followed by dataset args */
4601 	for (i = 0; i < argc; i++) {
4602 		if (strchr(argv[i], '=') != NULL) {
4603 			if (ds_start > 0) {
4604 				/* out-of-order prop=val argument */
4605 				(void) fprintf(stderr, gettext("invalid "
4606 				    "argument order\n"));
4607 				usage(B_FALSE);
4608 			}
4609 		} else if (ds_start < 0) {
4610 			ds_start = i;
4611 		}
4612 	}
4613 	if (ds_start < 0) {
4614 		(void) fprintf(stderr, gettext("missing dataset name(s)\n"));
4615 		usage(B_FALSE);
4616 	}
4617 
4618 	/* Populate a list of property settings */
4619 	if (nvlist_alloc(&cb.cb_proplist, NV_UNIQUE_NAME, 0) != 0)
4620 		nomem();
4621 	for (i = 0; i < ds_start; i++) {
4622 		if (!parseprop(cb.cb_proplist, argv[i])) {
4623 			ret = -1;
4624 			goto error;
4625 		}
4626 	}
4627 
4628 	ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
4629 	    ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
4630 
4631 error:
4632 	nvlist_free(cb.cb_proplist);
4633 	return (ret);
4634 }
4635 
4636 typedef struct snap_cbdata {
4637 	nvlist_t *sd_nvl;
4638 	boolean_t sd_recursive;
4639 	const char *sd_snapname;
4640 } snap_cbdata_t;
4641 
4642 static int
4643 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
4644 {
4645 	snap_cbdata_t *sd = arg;
4646 	char *name;
4647 	int rv = 0;
4648 	int error;
4649 
4650 	if (sd->sd_recursive &&
4651 	    zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
4652 		zfs_close(zhp);
4653 		return (0);
4654 	}
4655 
4656 	error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
4657 	if (error == -1)
4658 		nomem();
4659 	fnvlist_add_boolean(sd->sd_nvl, name);
4660 	free(name);
4661 
4662 	if (sd->sd_recursive)
4663 		rv = zfs_iter_filesystems_v2(zhp, 0, zfs_snapshot_cb, sd);
4664 	zfs_close(zhp);
4665 	return (rv);
4666 }
4667 
4668 /*
4669  * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
4670  *
4671  * Creates a snapshot with the given name.  While functionally equivalent to
4672  * 'zfs create', it is a separate command to differentiate intent.
4673  */
4674 static int
4675 zfs_do_snapshot(int argc, char **argv)
4676 {
4677 	int ret = 0;
4678 	int c;
4679 	nvlist_t *props;
4680 	snap_cbdata_t sd = { 0 };
4681 	boolean_t multiple_snaps = B_FALSE;
4682 
4683 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4684 		nomem();
4685 	if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
4686 		nomem();
4687 
4688 	/* check options */
4689 	while ((c = getopt(argc, argv, "ro:")) != -1) {
4690 		switch (c) {
4691 		case 'o':
4692 			if (!parseprop(props, optarg)) {
4693 				nvlist_free(sd.sd_nvl);
4694 				nvlist_free(props);
4695 				return (1);
4696 			}
4697 			break;
4698 		case 'r':
4699 			sd.sd_recursive = B_TRUE;
4700 			multiple_snaps = B_TRUE;
4701 			break;
4702 		case '?':
4703 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4704 			    optopt);
4705 			goto usage;
4706 		}
4707 	}
4708 
4709 	argc -= optind;
4710 	argv += optind;
4711 
4712 	/* check number of arguments */
4713 	if (argc < 1) {
4714 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
4715 		goto usage;
4716 	}
4717 
4718 	if (argc > 1)
4719 		multiple_snaps = B_TRUE;
4720 	for (; argc > 0; argc--, argv++) {
4721 		char *atp;
4722 		zfs_handle_t *zhp;
4723 
4724 		atp = strchr(argv[0], '@');
4725 		if (atp == NULL)
4726 			goto usage;
4727 		*atp = '\0';
4728 		sd.sd_snapname = atp + 1;
4729 		zhp = zfs_open(g_zfs, argv[0],
4730 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4731 		if (zhp == NULL)
4732 			goto usage;
4733 		if (zfs_snapshot_cb(zhp, &sd) != 0)
4734 			goto usage;
4735 	}
4736 
4737 	ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
4738 	nvlist_free(sd.sd_nvl);
4739 	nvlist_free(props);
4740 	if (ret != 0 && multiple_snaps)
4741 		(void) fprintf(stderr, gettext("no snapshots were created\n"));
4742 	return (ret != 0);
4743 
4744 usage:
4745 	nvlist_free(sd.sd_nvl);
4746 	nvlist_free(props);
4747 	usage(B_FALSE);
4748 	return (-1);
4749 }
4750 
4751 /*
4752  * Array of prefixes to exclude –
4753  * a linear search, even if executed for each dataset,
4754  * is plenty good enough.
4755  */
4756 typedef struct zfs_send_exclude_arg {
4757 	size_t count;
4758 	const char **list;
4759 } zfs_send_exclude_arg_t;
4760 
4761 static boolean_t
4762 zfs_do_send_exclude(zfs_handle_t *zhp, void *context)
4763 {
4764 	zfs_send_exclude_arg_t *excludes = context;
4765 	const char *name = zfs_get_name(zhp);
4766 
4767 	for (size_t i = 0; i < excludes->count; ++i) {
4768 		size_t len = strlen(excludes->list[i]);
4769 		if (strncmp(name, excludes->list[i], len) == 0 &&
4770 		    memchr("/@", name[len], sizeof ("/@")))
4771 			return (B_FALSE);
4772 	}
4773 
4774 	return (B_TRUE);
4775 }
4776 
4777 /*
4778  * Send a backup stream to stdout.
4779  */
4780 static int
4781 zfs_do_send(int argc, char **argv)
4782 {
4783 	char *fromname = NULL;
4784 	char *toname = NULL;
4785 	char *resume_token = NULL;
4786 	char *cp;
4787 	zfs_handle_t *zhp;
4788 	sendflags_t flags = { 0 };
4789 	int c, err;
4790 	nvlist_t *dbgnv = NULL;
4791 	char *redactbook = NULL;
4792 	zfs_send_exclude_arg_t excludes = { 0 };
4793 
4794 	struct option long_options[] = {
4795 		{"replicate",	no_argument,		NULL, 'R'},
4796 		{"skip-missing",	no_argument,	NULL, 's'},
4797 		{"redact",	required_argument,	NULL, 'd'},
4798 		{"props",	no_argument,		NULL, 'p'},
4799 		{"parsable",	no_argument,		NULL, 'P'},
4800 		{"dedup",	no_argument,		NULL, 'D'},
4801 		{"proctitle",	no_argument,		NULL, 'V'},
4802 		{"verbose",	no_argument,		NULL, 'v'},
4803 		{"dryrun",	no_argument,		NULL, 'n'},
4804 		{"large-block",	no_argument,		NULL, 'L'},
4805 		{"embed",	no_argument,		NULL, 'e'},
4806 		{"resume",	required_argument,	NULL, 't'},
4807 		{"compressed",	no_argument,		NULL, 'c'},
4808 		{"raw",		no_argument,		NULL, 'w'},
4809 		{"backup",	no_argument,		NULL, 'b'},
4810 		{"holds",	no_argument,		NULL, 'h'},
4811 		{"saved",	no_argument,		NULL, 'S'},
4812 		{"exclude",	required_argument,	NULL, 'X'},
4813 		{"no-preserve-encryption",	no_argument,	NULL, 'U'},
4814 		{0, 0, 0, 0}
4815 	};
4816 
4817 	/* check options */
4818 	while ((c = getopt_long(argc, argv, ":i:I:RsDpVvnPLeht:cwbd:SX:U",
4819 	    long_options, NULL)) != -1) {
4820 		switch (c) {
4821 		case 'X':
4822 			for (char *ds; (ds = strsep(&optarg, ",")) != NULL; ) {
4823 				if (!zfs_name_valid(ds, ZFS_TYPE_DATASET) ||
4824 				    strchr(ds, '/') == NULL) {
4825 					(void) fprintf(stderr, gettext("-X %s: "
4826 					    "not a valid non-root dataset name"
4827 					    ".\n"), ds);
4828 					usage(B_FALSE);
4829 				}
4830 				excludes.list = safe_realloc(excludes.list,
4831 				    sizeof (char *) * (excludes.count + 1));
4832 				excludes.list[excludes.count++] = ds;
4833 			}
4834 			break;
4835 		case 'i':
4836 			if (fromname)
4837 				usage(B_FALSE);
4838 			fromname = optarg;
4839 			break;
4840 		case 'I':
4841 			if (fromname)
4842 				usage(B_FALSE);
4843 			fromname = optarg;
4844 			flags.doall = B_TRUE;
4845 			break;
4846 		case 'R':
4847 			flags.replicate = B_TRUE;
4848 			break;
4849 		case 's':
4850 			flags.skipmissing = B_TRUE;
4851 			break;
4852 		case 'd':
4853 			redactbook = optarg;
4854 			break;
4855 		case 'p':
4856 			flags.props = B_TRUE;
4857 			break;
4858 		case 'b':
4859 			flags.backup = B_TRUE;
4860 			break;
4861 		case 'h':
4862 			flags.holds = B_TRUE;
4863 			break;
4864 		case 'P':
4865 			flags.parsable = B_TRUE;
4866 			break;
4867 		case 'V':
4868 			flags.progressastitle = B_TRUE;
4869 			break;
4870 		case 'v':
4871 			flags.verbosity++;
4872 			flags.progress = B_TRUE;
4873 			break;
4874 		case 'D':
4875 			(void) fprintf(stderr,
4876 			    gettext("WARNING: deduplicated send is no "
4877 			    "longer supported.  A regular,\n"
4878 			    "non-deduplicated stream will be generated.\n\n"));
4879 			break;
4880 		case 'n':
4881 			flags.dryrun = B_TRUE;
4882 			break;
4883 		case 'L':
4884 			flags.largeblock = B_TRUE;
4885 			break;
4886 		case 'e':
4887 			flags.embed_data = B_TRUE;
4888 			break;
4889 		case 't':
4890 			resume_token = optarg;
4891 			break;
4892 		case 'c':
4893 			flags.compress = B_TRUE;
4894 			break;
4895 		case 'w':
4896 			flags.raw = B_TRUE;
4897 			flags.compress = B_TRUE;
4898 			flags.embed_data = B_TRUE;
4899 			flags.largeblock = B_TRUE;
4900 			break;
4901 		case 'S':
4902 			flags.saved = B_TRUE;
4903 			break;
4904 		case 'U':
4905 			flags.no_preserve_encryption = B_TRUE;
4906 			break;
4907 		case ':':
4908 			/*
4909 			 * If a parameter was not passed, optopt contains the
4910 			 * value that would normally lead us into the
4911 			 * appropriate case statement.  If it's > 256, then this
4912 			 * must be a longopt and we should look at argv to get
4913 			 * the string.  Otherwise it's just the character, so we
4914 			 * should use it directly.
4915 			 */
4916 			if (optopt <= UINT8_MAX) {
4917 				(void) fprintf(stderr,
4918 				    gettext("missing argument for '%c' "
4919 				    "option\n"), optopt);
4920 			} else {
4921 				(void) fprintf(stderr,
4922 				    gettext("missing argument for '%s' "
4923 				    "option\n"), argv[optind - 1]);
4924 			}
4925 			free(excludes.list);
4926 			usage(B_FALSE);
4927 			break;
4928 		case '?':
4929 		default:
4930 			/*
4931 			 * If an invalid flag was passed, optopt contains the
4932 			 * character if it was a short flag, or 0 if it was a
4933 			 * longopt.
4934 			 */
4935 			if (optopt != 0) {
4936 				(void) fprintf(stderr,
4937 				    gettext("invalid option '%c'\n"), optopt);
4938 			} else {
4939 				(void) fprintf(stderr,
4940 				    gettext("invalid option '%s'\n"),
4941 				    argv[optind - 1]);
4942 
4943 			}
4944 			free(excludes.list);
4945 			usage(B_FALSE);
4946 		}
4947 	}
4948 
4949 	if ((flags.parsable || flags.progressastitle) && flags.verbosity == 0)
4950 		flags.verbosity = 1;
4951 
4952 	if (excludes.count > 0 && !flags.replicate) {
4953 		free(excludes.list);
4954 		(void) fprintf(stderr, gettext("Cannot specify "
4955 		    "dataset exclusion (-X) on a non-recursive "
4956 		    "send.\n"));
4957 		return (1);
4958 	}
4959 
4960 	argc -= optind;
4961 	argv += optind;
4962 
4963 	if (resume_token != NULL) {
4964 		if (fromname != NULL || flags.replicate || flags.props ||
4965 		    flags.backup || flags.holds ||
4966 		    flags.saved || redactbook != NULL) {
4967 			free(excludes.list);
4968 			(void) fprintf(stderr,
4969 			    gettext("invalid flags combined with -t\n"));
4970 			usage(B_FALSE);
4971 		}
4972 		if (argc > 0) {
4973 			free(excludes.list);
4974 			(void) fprintf(stderr, gettext("too many arguments\n"));
4975 			usage(B_FALSE);
4976 		}
4977 	} else {
4978 		if (argc < 1) {
4979 			free(excludes.list);
4980 			(void) fprintf(stderr,
4981 			    gettext("missing snapshot argument\n"));
4982 			usage(B_FALSE);
4983 		}
4984 		if (argc > 1) {
4985 			free(excludes.list);
4986 			(void) fprintf(stderr, gettext("too many arguments\n"));
4987 			usage(B_FALSE);
4988 		}
4989 	}
4990 
4991 	if (flags.saved) {
4992 		if (fromname != NULL || flags.replicate || flags.props ||
4993 		    flags.doall || flags.backup ||
4994 		    flags.holds || flags.largeblock || flags.embed_data ||
4995 		    flags.compress || flags.raw || redactbook != NULL) {
4996 			free(excludes.list);
4997 
4998 			(void) fprintf(stderr, gettext("incompatible flags "
4999 			    "combined with saved send flag\n"));
5000 			usage(B_FALSE);
5001 		}
5002 		if (strchr(argv[0], '@') != NULL) {
5003 			free(excludes.list);
5004 
5005 			(void) fprintf(stderr, gettext("saved send must "
5006 			    "specify the dataset with partially-received "
5007 			    "state\n"));
5008 			usage(B_FALSE);
5009 		}
5010 	}
5011 
5012 	if (flags.raw && redactbook != NULL) {
5013 		free(excludes.list);
5014 		(void) fprintf(stderr,
5015 		    gettext("Error: raw sends may not be redacted.\n"));
5016 		return (1);
5017 	}
5018 
5019 	if (!flags.dryrun && isatty(STDOUT_FILENO)) {
5020 		free(excludes.list);
5021 		(void) fprintf(stderr,
5022 		    gettext("Error: Stream can not be written to a terminal.\n"
5023 		    "You must redirect standard output.\n"));
5024 		return (1);
5025 	}
5026 
5027 	if (flags.saved) {
5028 		zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
5029 		if (zhp == NULL) {
5030 			free(excludes.list);
5031 			return (1);
5032 		}
5033 
5034 		err = zfs_send_saved(zhp, &flags, STDOUT_FILENO,
5035 		    resume_token);
5036 		free(excludes.list);
5037 		zfs_close(zhp);
5038 		return (err != 0);
5039 	} else if (resume_token != NULL) {
5040 		free(excludes.list);
5041 		return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
5042 		    resume_token));
5043 	}
5044 
5045 	if (flags.skipmissing && !flags.replicate) {
5046 		free(excludes.list);
5047 		(void) fprintf(stderr,
5048 		    gettext("skip-missing flag can only be used in "
5049 		    "conjunction with replicate\n"));
5050 		usage(B_FALSE);
5051 	}
5052 
5053 	/*
5054 	 * For everything except -R and -I, use the new, cleaner code path.
5055 	 */
5056 	if (!(flags.replicate || flags.doall)) {
5057 		char frombuf[ZFS_MAX_DATASET_NAME_LEN];
5058 
5059 		if (fromname != NULL && (strchr(fromname, '#') == NULL &&
5060 		    strchr(fromname, '@') == NULL)) {
5061 			/*
5062 			 * Neither bookmark or snapshot was specified.  Print a
5063 			 * warning, and assume snapshot.
5064 			 */
5065 			(void) fprintf(stderr, "Warning: incremental source "
5066 			    "didn't specify type, assuming snapshot. Use '@' "
5067 			    "or '#' prefix to avoid ambiguity.\n");
5068 			(void) snprintf(frombuf, sizeof (frombuf), "@%s",
5069 			    fromname);
5070 			fromname = frombuf;
5071 		}
5072 		if (fromname != NULL &&
5073 		    (fromname[0] == '#' || fromname[0] == '@')) {
5074 			/*
5075 			 * Incremental source name begins with # or @.
5076 			 * Default to same fs as target.
5077 			 */
5078 			char tmpbuf[ZFS_MAX_DATASET_NAME_LEN];
5079 			(void) strlcpy(tmpbuf, fromname, sizeof (tmpbuf));
5080 			(void) strlcpy(frombuf, argv[0], sizeof (frombuf));
5081 			cp = strchr(frombuf, '@');
5082 			if (cp != NULL)
5083 				*cp = '\0';
5084 			(void) strlcat(frombuf, tmpbuf, sizeof (frombuf));
5085 			fromname = frombuf;
5086 		}
5087 
5088 		zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
5089 		if (zhp == NULL) {
5090 			free(excludes.list);
5091 			return (1);
5092 		}
5093 		err = zfs_send_one(zhp, fromname, STDOUT_FILENO, &flags,
5094 		    redactbook);
5095 
5096 		free(excludes.list);
5097 		zfs_close(zhp);
5098 		return (err != 0);
5099 	}
5100 
5101 	if (fromname != NULL && strchr(fromname, '#')) {
5102 		(void) fprintf(stderr,
5103 		    gettext("Error: multiple snapshots cannot be "
5104 		    "sent from a bookmark.\n"));
5105 		free(excludes.list);
5106 		return (1);
5107 	}
5108 
5109 	if (redactbook != NULL) {
5110 		(void) fprintf(stderr, gettext("Error: multiple snapshots "
5111 		    "cannot be sent redacted.\n"));
5112 		free(excludes.list);
5113 		return (1);
5114 	}
5115 
5116 	if ((cp = strchr(argv[0], '@')) == NULL) {
5117 		(void) fprintf(stderr, gettext("Error: "
5118 		    "Unsupported flag with filesystem or bookmark.\n"));
5119 		free(excludes.list);
5120 		return (1);
5121 	}
5122 	*cp = '\0';
5123 	toname = cp + 1;
5124 	zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5125 	if (zhp == NULL) {
5126 		free(excludes.list);
5127 		return (1);
5128 	}
5129 
5130 	/*
5131 	 * If they specified the full path to the snapshot, chop off
5132 	 * everything except the short name of the snapshot, but special
5133 	 * case if they specify the origin.
5134 	 */
5135 	if (fromname && (cp = strchr(fromname, '@')) != NULL) {
5136 		char origin[ZFS_MAX_DATASET_NAME_LEN];
5137 		zprop_source_t src;
5138 
5139 		(void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
5140 		    origin, sizeof (origin), &src, NULL, 0, B_FALSE);
5141 
5142 		if (strcmp(origin, fromname) == 0) {
5143 			fromname = NULL;
5144 			flags.fromorigin = B_TRUE;
5145 		} else {
5146 			*cp = '\0';
5147 			if (cp != fromname && strcmp(argv[0], fromname)) {
5148 				zfs_close(zhp);
5149 				free(excludes.list);
5150 				(void) fprintf(stderr,
5151 				    gettext("incremental source must be "
5152 				    "in same filesystem\n"));
5153 				usage(B_FALSE);
5154 			}
5155 			fromname = cp + 1;
5156 			if (strchr(fromname, '@') || strchr(fromname, '/')) {
5157 				zfs_close(zhp);
5158 				free(excludes.list);
5159 				(void) fprintf(stderr,
5160 				    gettext("invalid incremental source\n"));
5161 				usage(B_FALSE);
5162 			}
5163 		}
5164 	}
5165 
5166 	if (flags.replicate && fromname == NULL)
5167 		flags.doall = B_TRUE;
5168 
5169 	err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO,
5170 	    excludes.count > 0 ? zfs_do_send_exclude : NULL,
5171 	    &excludes, flags.verbosity >= 3 ? &dbgnv : NULL);
5172 
5173 	if (flags.verbosity >= 3 && dbgnv != NULL) {
5174 		/*
5175 		 * dump_nvlist prints to stdout, but that's been
5176 		 * redirected to a file.  Make it print to stderr
5177 		 * instead.
5178 		 */
5179 		(void) dup2(STDERR_FILENO, STDOUT_FILENO);
5180 		dump_nvlist(dbgnv, 0);
5181 		nvlist_free(dbgnv);
5182 	}
5183 
5184 	zfs_close(zhp);
5185 	free(excludes.list);
5186 	return (err != 0);
5187 }
5188 
5189 /*
5190  * Restore a backup stream from stdin.
5191  */
5192 static int
5193 zfs_do_receive(int argc, char **argv)
5194 {
5195 	int c, err = 0;
5196 	recvflags_t flags = { 0 };
5197 	boolean_t abort_resumable = B_FALSE;
5198 	nvlist_t *props;
5199 
5200 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
5201 		nomem();
5202 
5203 	/* check options */
5204 	while ((c = getopt(argc, argv, ":o:x:dehMnuvFsAc")) != -1) {
5205 		switch (c) {
5206 		case 'o':
5207 			if (!parseprop(props, optarg)) {
5208 				nvlist_free(props);
5209 				usage(B_FALSE);
5210 			}
5211 			break;
5212 		case 'x':
5213 			if (!parsepropname(props, optarg)) {
5214 				nvlist_free(props);
5215 				usage(B_FALSE);
5216 			}
5217 			break;
5218 		case 'd':
5219 			if (flags.istail) {
5220 				(void) fprintf(stderr, gettext("invalid option "
5221 				    "combination: -d and -e are mutually "
5222 				    "exclusive\n"));
5223 				usage(B_FALSE);
5224 			}
5225 			flags.isprefix = B_TRUE;
5226 			break;
5227 		case 'e':
5228 			if (flags.isprefix) {
5229 				(void) fprintf(stderr, gettext("invalid option "
5230 				    "combination: -d and -e are mutually "
5231 				    "exclusive\n"));
5232 				usage(B_FALSE);
5233 			}
5234 			flags.istail = B_TRUE;
5235 			break;
5236 		case 'h':
5237 			flags.skipholds = B_TRUE;
5238 			break;
5239 		case 'M':
5240 			flags.forceunmount = B_TRUE;
5241 			break;
5242 		case 'n':
5243 			flags.dryrun = B_TRUE;
5244 			break;
5245 		case 'u':
5246 			flags.nomount = B_TRUE;
5247 			break;
5248 		case 'v':
5249 			flags.verbose = B_TRUE;
5250 			break;
5251 		case 's':
5252 			flags.resumable = B_TRUE;
5253 			break;
5254 		case 'F':
5255 			flags.force = B_TRUE;
5256 			break;
5257 		case 'A':
5258 			abort_resumable = B_TRUE;
5259 			break;
5260 		case 'c':
5261 			flags.heal = B_TRUE;
5262 			break;
5263 		case ':':
5264 			(void) fprintf(stderr, gettext("missing argument for "
5265 			    "'%c' option\n"), optopt);
5266 			usage(B_FALSE);
5267 			break;
5268 		case '?':
5269 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5270 			    optopt);
5271 			usage(B_FALSE);
5272 		}
5273 	}
5274 
5275 	argc -= optind;
5276 	argv += optind;
5277 
5278 	/* zfs recv -e (use "tail" name) implies -d (remove dataset "head") */
5279 	if (flags.istail)
5280 		flags.isprefix = B_TRUE;
5281 
5282 	/* check number of arguments */
5283 	if (argc < 1) {
5284 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
5285 		usage(B_FALSE);
5286 	}
5287 	if (argc > 1) {
5288 		(void) fprintf(stderr, gettext("too many arguments\n"));
5289 		usage(B_FALSE);
5290 	}
5291 
5292 	if (abort_resumable) {
5293 		if (flags.isprefix || flags.istail || flags.dryrun ||
5294 		    flags.resumable || flags.nomount) {
5295 			(void) fprintf(stderr, gettext("invalid option\n"));
5296 			usage(B_FALSE);
5297 		}
5298 
5299 		char namebuf[ZFS_MAX_DATASET_NAME_LEN];
5300 		(void) snprintf(namebuf, sizeof (namebuf),
5301 		    "%s/%%recv", argv[0]);
5302 
5303 		if (zfs_dataset_exists(g_zfs, namebuf,
5304 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
5305 			zfs_handle_t *zhp = zfs_open(g_zfs,
5306 			    namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5307 			if (zhp == NULL) {
5308 				nvlist_free(props);
5309 				return (1);
5310 			}
5311 			err = zfs_destroy(zhp, B_FALSE);
5312 			zfs_close(zhp);
5313 		} else {
5314 			zfs_handle_t *zhp = zfs_open(g_zfs,
5315 			    argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5316 			if (zhp == NULL)
5317 				usage(B_FALSE);
5318 			if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
5319 			    zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
5320 			    NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
5321 				(void) fprintf(stderr,
5322 				    gettext("'%s' does not have any "
5323 				    "resumable receive state to abort\n"),
5324 				    argv[0]);
5325 				nvlist_free(props);
5326 				zfs_close(zhp);
5327 				return (1);
5328 			}
5329 			err = zfs_destroy(zhp, B_FALSE);
5330 			zfs_close(zhp);
5331 		}
5332 		nvlist_free(props);
5333 		return (err != 0);
5334 	}
5335 
5336 	if (isatty(STDIN_FILENO)) {
5337 		(void) fprintf(stderr,
5338 		    gettext("Error: Backup stream can not be read "
5339 		    "from a terminal.\n"
5340 		    "You must redirect standard input.\n"));
5341 		nvlist_free(props);
5342 		return (1);
5343 	}
5344 	err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
5345 	nvlist_free(props);
5346 
5347 	return (err != 0);
5348 }
5349 
5350 /*
5351  * allow/unallow stuff
5352  */
5353 /* copied from zfs/sys/dsl_deleg.h */
5354 #define	ZFS_DELEG_PERM_CREATE		"create"
5355 #define	ZFS_DELEG_PERM_DESTROY		"destroy"
5356 #define	ZFS_DELEG_PERM_SNAPSHOT		"snapshot"
5357 #define	ZFS_DELEG_PERM_ROLLBACK		"rollback"
5358 #define	ZFS_DELEG_PERM_CLONE		"clone"
5359 #define	ZFS_DELEG_PERM_PROMOTE		"promote"
5360 #define	ZFS_DELEG_PERM_RENAME		"rename"
5361 #define	ZFS_DELEG_PERM_MOUNT		"mount"
5362 #define	ZFS_DELEG_PERM_SHARE		"share"
5363 #define	ZFS_DELEG_PERM_SEND		"send"
5364 #define	ZFS_DELEG_PERM_SEND_RAW		"send:raw"
5365 #define	ZFS_DELEG_PERM_SEND_ENCRYPTED	"send:encrypted"
5366 #define	ZFS_DELEG_PERM_RECEIVE		"receive"
5367 #define	ZFS_DELEG_PERM_RECEIVE_APPEND	"receive:append"
5368 #define	ZFS_DELEG_PERM_ALLOW		"allow"
5369 #define	ZFS_DELEG_PERM_USERPROP		"userprop"
5370 #define	ZFS_DELEG_PERM_VSCAN		"vscan" /* ??? */
5371 #define	ZFS_DELEG_PERM_USERQUOTA	"userquota"
5372 #define	ZFS_DELEG_PERM_GROUPQUOTA	"groupquota"
5373 #define	ZFS_DELEG_PERM_USERUSED		"userused"
5374 #define	ZFS_DELEG_PERM_GROUPUSED	"groupused"
5375 #define	ZFS_DELEG_PERM_USEROBJQUOTA	"userobjquota"
5376 #define	ZFS_DELEG_PERM_GROUPOBJQUOTA	"groupobjquota"
5377 #define	ZFS_DELEG_PERM_USEROBJUSED	"userobjused"
5378 #define	ZFS_DELEG_PERM_GROUPOBJUSED	"groupobjused"
5379 
5380 #define	ZFS_DELEG_PERM_HOLD		"hold"
5381 #define	ZFS_DELEG_PERM_RELEASE		"release"
5382 #define	ZFS_DELEG_PERM_DIFF		"diff"
5383 #define	ZFS_DELEG_PERM_BOOKMARK		"bookmark"
5384 #define	ZFS_DELEG_PERM_LOAD_KEY		"load-key"
5385 #define	ZFS_DELEG_PERM_CHANGE_KEY	"change-key"
5386 
5387 #define	ZFS_DELEG_PERM_PROJECTUSED	"projectused"
5388 #define	ZFS_DELEG_PERM_PROJECTQUOTA	"projectquota"
5389 #define	ZFS_DELEG_PERM_PROJECTOBJUSED	"projectobjused"
5390 #define	ZFS_DELEG_PERM_PROJECTOBJQUOTA	"projectobjquota"
5391 
5392 #define	ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
5393 
5394 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
5395 	{ ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
5396 	{ ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
5397 	{ ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
5398 	{ ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
5399 	{ ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
5400 	{ ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
5401 	{ ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
5402 	{ ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
5403 	{ ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
5404 	{ ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
5405 	{ ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
5406 	{ ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
5407 	{ ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
5408 	{ ZFS_DELEG_PERM_SEND_RAW, ZFS_DELEG_NOTE_SEND_RAW },
5409 	{ ZFS_DELEG_PERM_SEND_ENCRYPTED, ZFS_DELEG_NOTE_SEND_ENCRYPTED },
5410 	{ ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
5411 	{ ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
5412 	{ ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
5413 	{ ZFS_DELEG_PERM_LOAD_KEY, ZFS_DELEG_NOTE_LOAD_KEY },
5414 	{ ZFS_DELEG_PERM_CHANGE_KEY, ZFS_DELEG_NOTE_CHANGE_KEY },
5415 
5416 	{ ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
5417 	{ ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
5418 	{ ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
5419 	{ ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
5420 	{ ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
5421 	{ ZFS_DELEG_PERM_USEROBJQUOTA, ZFS_DELEG_NOTE_USEROBJQUOTA },
5422 	{ ZFS_DELEG_PERM_USEROBJUSED, ZFS_DELEG_NOTE_USEROBJUSED },
5423 	{ ZFS_DELEG_PERM_GROUPOBJQUOTA, ZFS_DELEG_NOTE_GROUPOBJQUOTA },
5424 	{ ZFS_DELEG_PERM_GROUPOBJUSED, ZFS_DELEG_NOTE_GROUPOBJUSED },
5425 	{ ZFS_DELEG_PERM_PROJECTUSED, ZFS_DELEG_NOTE_PROJECTUSED },
5426 	{ ZFS_DELEG_PERM_PROJECTQUOTA, ZFS_DELEG_NOTE_PROJECTQUOTA },
5427 	{ ZFS_DELEG_PERM_PROJECTOBJUSED, ZFS_DELEG_NOTE_PROJECTOBJUSED },
5428 	{ ZFS_DELEG_PERM_PROJECTOBJQUOTA, ZFS_DELEG_NOTE_PROJECTOBJQUOTA },
5429 	{ NULL, ZFS_DELEG_NOTE_NONE }
5430 };
5431 
5432 /* permission structure */
5433 typedef struct deleg_perm {
5434 	zfs_deleg_who_type_t	dp_who_type;
5435 	const char		*dp_name;
5436 	boolean_t		dp_local;
5437 	boolean_t		dp_descend;
5438 } deleg_perm_t;
5439 
5440 /* */
5441 typedef struct deleg_perm_node {
5442 	deleg_perm_t		dpn_perm;
5443 
5444 	avl_node_t		dpn_avl_node;
5445 } deleg_perm_node_t;
5446 
5447 typedef struct fs_perm fs_perm_t;
5448 
5449 /* permissions set */
5450 typedef struct who_perm {
5451 	zfs_deleg_who_type_t	who_type;
5452 	const char		*who_name;		/* id */
5453 	char			who_ug_name[256];	/* user/group name */
5454 	fs_perm_t		*who_fsperm;		/* uplink */
5455 
5456 	avl_tree_t		who_deleg_perm_avl;	/* permissions */
5457 } who_perm_t;
5458 
5459 /* */
5460 typedef struct who_perm_node {
5461 	who_perm_t	who_perm;
5462 	avl_node_t	who_avl_node;
5463 } who_perm_node_t;
5464 
5465 typedef struct fs_perm_set fs_perm_set_t;
5466 /* fs permissions */
5467 struct fs_perm {
5468 	const char		*fsp_name;
5469 
5470 	avl_tree_t		fsp_sc_avl;	/* sets,create */
5471 	avl_tree_t		fsp_uge_avl;	/* user,group,everyone */
5472 
5473 	fs_perm_set_t		*fsp_set;	/* uplink */
5474 };
5475 
5476 /* */
5477 typedef struct fs_perm_node {
5478 	fs_perm_t	fspn_fsperm;
5479 	avl_tree_t	fspn_avl;
5480 
5481 	list_node_t	fspn_list_node;
5482 } fs_perm_node_t;
5483 
5484 /* top level structure */
5485 struct fs_perm_set {
5486 	list_t		fsps_list; /* list of fs_perms */
5487 };
5488 
5489 static inline const char *
5490 deleg_perm_type(zfs_deleg_note_t note)
5491 {
5492 	/* subcommands */
5493 	switch (note) {
5494 		/* SUBCOMMANDS */
5495 		/* OTHER */
5496 	case ZFS_DELEG_NOTE_GROUPQUOTA:
5497 	case ZFS_DELEG_NOTE_GROUPUSED:
5498 	case ZFS_DELEG_NOTE_USERPROP:
5499 	case ZFS_DELEG_NOTE_USERQUOTA:
5500 	case ZFS_DELEG_NOTE_USERUSED:
5501 	case ZFS_DELEG_NOTE_USEROBJQUOTA:
5502 	case ZFS_DELEG_NOTE_USEROBJUSED:
5503 	case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
5504 	case ZFS_DELEG_NOTE_GROUPOBJUSED:
5505 	case ZFS_DELEG_NOTE_PROJECTUSED:
5506 	case ZFS_DELEG_NOTE_PROJECTQUOTA:
5507 	case ZFS_DELEG_NOTE_PROJECTOBJUSED:
5508 	case ZFS_DELEG_NOTE_PROJECTOBJQUOTA:
5509 		/* other */
5510 		return (gettext("other"));
5511 	default:
5512 		return (gettext("subcommand"));
5513 	}
5514 }
5515 
5516 static int
5517 who_type2weight(zfs_deleg_who_type_t who_type)
5518 {
5519 	int res;
5520 	switch (who_type) {
5521 		case ZFS_DELEG_NAMED_SET_SETS:
5522 		case ZFS_DELEG_NAMED_SET:
5523 			res = 0;
5524 			break;
5525 		case ZFS_DELEG_CREATE_SETS:
5526 		case ZFS_DELEG_CREATE:
5527 			res = 1;
5528 			break;
5529 		case ZFS_DELEG_USER_SETS:
5530 		case ZFS_DELEG_USER:
5531 			res = 2;
5532 			break;
5533 		case ZFS_DELEG_GROUP_SETS:
5534 		case ZFS_DELEG_GROUP:
5535 			res = 3;
5536 			break;
5537 		case ZFS_DELEG_EVERYONE_SETS:
5538 		case ZFS_DELEG_EVERYONE:
5539 			res = 4;
5540 			break;
5541 		default:
5542 			res = -1;
5543 	}
5544 
5545 	return (res);
5546 }
5547 
5548 static int
5549 who_perm_compare(const void *larg, const void *rarg)
5550 {
5551 	const who_perm_node_t *l = larg;
5552 	const who_perm_node_t *r = rarg;
5553 	zfs_deleg_who_type_t ltype = l->who_perm.who_type;
5554 	zfs_deleg_who_type_t rtype = r->who_perm.who_type;
5555 	int lweight = who_type2weight(ltype);
5556 	int rweight = who_type2weight(rtype);
5557 	int res = TREE_CMP(lweight, rweight);
5558 	if (res == 0)
5559 		res = TREE_ISIGN(strncmp(l->who_perm.who_name,
5560 		    r->who_perm.who_name, ZFS_MAX_DELEG_NAME-1));
5561 	return (res);
5562 }
5563 
5564 static int
5565 deleg_perm_compare(const void *larg, const void *rarg)
5566 {
5567 	const deleg_perm_node_t *l = larg;
5568 	const deleg_perm_node_t *r = rarg;
5569 	return (TREE_ISIGN(strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
5570 	    ZFS_MAX_DELEG_NAME-1)));
5571 }
5572 
5573 static inline void
5574 fs_perm_set_init(fs_perm_set_t *fspset)
5575 {
5576 	memset(fspset, 0, sizeof (fs_perm_set_t));
5577 	list_create(&fspset->fsps_list, sizeof (fs_perm_node_t),
5578 	    offsetof(fs_perm_node_t, fspn_list_node));
5579 }
5580 
5581 static inline void fs_perm_fini(fs_perm_t *);
5582 static inline void who_perm_fini(who_perm_t *);
5583 
5584 static inline void
5585 fs_perm_set_fini(fs_perm_set_t *fspset)
5586 {
5587 	fs_perm_node_t *node;
5588 	while ((node = list_remove_head(&fspset->fsps_list)) != NULL) {
5589 		fs_perm_t *fsperm = &node->fspn_fsperm;
5590 		fs_perm_fini(fsperm);
5591 		free(node);
5592 	}
5593 	list_destroy(&fspset->fsps_list);
5594 }
5595 
5596 static inline void
5597 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
5598     const char *name)
5599 {
5600 	deleg_perm->dp_who_type = type;
5601 	deleg_perm->dp_name = name;
5602 }
5603 
5604 static inline void
5605 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
5606     zfs_deleg_who_type_t type, const char *name)
5607 {
5608 	memset(who_perm, 0, sizeof (who_perm_t));
5609 
5610 	avl_create(&who_perm->who_deleg_perm_avl, deleg_perm_compare,
5611 	    sizeof (deleg_perm_node_t),
5612 	    offsetof(deleg_perm_node_t, dpn_avl_node));
5613 
5614 	who_perm->who_type = type;
5615 	who_perm->who_name = name;
5616 	who_perm->who_fsperm = fsperm;
5617 }
5618 
5619 static inline void
5620 who_perm_fini(who_perm_t *who_perm)
5621 {
5622 	deleg_perm_node_t *node;
5623 	void *cookie = NULL;
5624 
5625 	while ((node = avl_destroy_nodes(&who_perm->who_deleg_perm_avl,
5626 	    &cookie)) != NULL) {
5627 		free(node);
5628 	}
5629 
5630 	avl_destroy(&who_perm->who_deleg_perm_avl);
5631 }
5632 
5633 static inline void
5634 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
5635 {
5636 	memset(fsperm, 0, sizeof (fs_perm_t));
5637 
5638 	avl_create(&fsperm->fsp_sc_avl, who_perm_compare,
5639 	    sizeof (who_perm_node_t), offsetof(who_perm_node_t, who_avl_node));
5640 	avl_create(&fsperm->fsp_uge_avl, who_perm_compare,
5641 	    sizeof (who_perm_node_t), offsetof(who_perm_node_t, who_avl_node));
5642 
5643 	fsperm->fsp_set = fspset;
5644 	fsperm->fsp_name = fsname;
5645 }
5646 
5647 static inline void
5648 fs_perm_fini(fs_perm_t *fsperm)
5649 {
5650 	who_perm_node_t *node;
5651 	void *cookie = NULL;
5652 
5653 	while ((node = avl_destroy_nodes(&fsperm->fsp_sc_avl,
5654 	    &cookie)) != NULL) {
5655 		who_perm_t *who_perm = &node->who_perm;
5656 		who_perm_fini(who_perm);
5657 		free(node);
5658 	}
5659 
5660 	cookie = NULL;
5661 	while ((node = avl_destroy_nodes(&fsperm->fsp_uge_avl,
5662 	    &cookie)) != NULL) {
5663 		who_perm_t *who_perm = &node->who_perm;
5664 		who_perm_fini(who_perm);
5665 		free(node);
5666 	}
5667 
5668 	avl_destroy(&fsperm->fsp_sc_avl);
5669 	avl_destroy(&fsperm->fsp_uge_avl);
5670 }
5671 
5672 static void
5673 set_deleg_perm_node(avl_tree_t *avl, deleg_perm_node_t *node,
5674     zfs_deleg_who_type_t who_type, const char *name, char locality)
5675 {
5676 	avl_index_t idx = 0;
5677 
5678 	deleg_perm_node_t *found_node = NULL;
5679 	deleg_perm_t	*deleg_perm = &node->dpn_perm;
5680 
5681 	deleg_perm_init(deleg_perm, who_type, name);
5682 
5683 	if ((found_node = avl_find(avl, node, &idx)) == NULL)
5684 		avl_insert(avl, node, idx);
5685 	else {
5686 		node = found_node;
5687 		deleg_perm = &node->dpn_perm;
5688 	}
5689 
5690 
5691 	switch (locality) {
5692 	case ZFS_DELEG_LOCAL:
5693 		deleg_perm->dp_local = B_TRUE;
5694 		break;
5695 	case ZFS_DELEG_DESCENDENT:
5696 		deleg_perm->dp_descend = B_TRUE;
5697 		break;
5698 	case ZFS_DELEG_NA:
5699 		break;
5700 	default:
5701 		assert(B_FALSE); /* invalid locality */
5702 	}
5703 }
5704 
5705 static inline int
5706 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
5707 {
5708 	nvpair_t *nvp = NULL;
5709 	avl_tree_t *avl = &who_perm->who_deleg_perm_avl;
5710 	zfs_deleg_who_type_t who_type = who_perm->who_type;
5711 
5712 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5713 		const char *name = nvpair_name(nvp);
5714 		data_type_t type = nvpair_type(nvp);
5715 		deleg_perm_node_t *node =
5716 		    safe_malloc(sizeof (deleg_perm_node_t));
5717 
5718 		VERIFY(type == DATA_TYPE_BOOLEAN);
5719 
5720 		set_deleg_perm_node(avl, node, who_type, name, locality);
5721 	}
5722 
5723 	return (0);
5724 }
5725 
5726 static inline int
5727 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
5728 {
5729 	nvpair_t *nvp = NULL;
5730 
5731 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5732 		nvlist_t *nvl2 = NULL;
5733 		const char *name = nvpair_name(nvp);
5734 		avl_tree_t *avl = NULL;
5735 		zfs_deleg_who_type_t perm_type = name[0];
5736 		char perm_locality = name[1];
5737 		const char *perm_name = name + 3;
5738 		who_perm_t *who_perm = NULL;
5739 
5740 		assert('$' == name[2]);
5741 
5742 		if (nvpair_value_nvlist(nvp, &nvl2) != 0)
5743 			return (-1);
5744 
5745 		switch (perm_type) {
5746 		case ZFS_DELEG_CREATE:
5747 		case ZFS_DELEG_CREATE_SETS:
5748 		case ZFS_DELEG_NAMED_SET:
5749 		case ZFS_DELEG_NAMED_SET_SETS:
5750 			avl = &fsperm->fsp_sc_avl;
5751 			break;
5752 		case ZFS_DELEG_USER:
5753 		case ZFS_DELEG_USER_SETS:
5754 		case ZFS_DELEG_GROUP:
5755 		case ZFS_DELEG_GROUP_SETS:
5756 		case ZFS_DELEG_EVERYONE:
5757 		case ZFS_DELEG_EVERYONE_SETS:
5758 			avl = &fsperm->fsp_uge_avl;
5759 			break;
5760 
5761 		default:
5762 			assert(!"unhandled zfs_deleg_who_type_t");
5763 		}
5764 
5765 		who_perm_node_t *found_node = NULL;
5766 		who_perm_node_t *node = safe_malloc(
5767 		    sizeof (who_perm_node_t));
5768 		who_perm = &node->who_perm;
5769 		avl_index_t idx = 0;
5770 
5771 		who_perm_init(who_perm, fsperm, perm_type, perm_name);
5772 
5773 		if ((found_node = avl_find(avl, node, &idx)) == NULL) {
5774 			if (avl == &fsperm->fsp_uge_avl) {
5775 				uid_t rid = 0;
5776 				struct passwd *p = NULL;
5777 				struct group *g = NULL;
5778 				const char *nice_name = NULL;
5779 
5780 				switch (perm_type) {
5781 				case ZFS_DELEG_USER_SETS:
5782 				case ZFS_DELEG_USER:
5783 					rid = atoi(perm_name);
5784 					p = getpwuid(rid);
5785 					if (p)
5786 						nice_name = p->pw_name;
5787 					break;
5788 				case ZFS_DELEG_GROUP_SETS:
5789 				case ZFS_DELEG_GROUP:
5790 					rid = atoi(perm_name);
5791 					g = getgrgid(rid);
5792 					if (g)
5793 						nice_name = g->gr_name;
5794 					break;
5795 
5796 				default:
5797 					break;
5798 				}
5799 
5800 				if (nice_name != NULL) {
5801 					(void) strlcpy(
5802 					    node->who_perm.who_ug_name,
5803 					    nice_name, 256);
5804 				} else {
5805 					/* User or group unknown */
5806 					(void) snprintf(
5807 					    node->who_perm.who_ug_name,
5808 					    sizeof (node->who_perm.who_ug_name),
5809 					    "(unknown: %d)", rid);
5810 				}
5811 			}
5812 
5813 			avl_insert(avl, node, idx);
5814 		} else {
5815 			node = found_node;
5816 			who_perm = &node->who_perm;
5817 		}
5818 
5819 		assert(who_perm != NULL);
5820 		(void) parse_who_perm(who_perm, nvl2, perm_locality);
5821 	}
5822 
5823 	return (0);
5824 }
5825 
5826 static inline int
5827 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
5828 {
5829 	nvpair_t *nvp = NULL;
5830 
5831 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5832 		nvlist_t *nvl2 = NULL;
5833 		const char *fsname = nvpair_name(nvp);
5834 		data_type_t type = nvpair_type(nvp);
5835 		fs_perm_t *fsperm = NULL;
5836 		fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
5837 
5838 		fsperm = &node->fspn_fsperm;
5839 
5840 		VERIFY(DATA_TYPE_NVLIST == type);
5841 
5842 		fs_perm_init(fsperm, fspset, fsname);
5843 
5844 		if (nvpair_value_nvlist(nvp, &nvl2) != 0)
5845 			return (-1);
5846 
5847 		(void) parse_fs_perm(fsperm, nvl2);
5848 
5849 		list_insert_tail(&fspset->fsps_list, node);
5850 	}
5851 
5852 	return (0);
5853 }
5854 
5855 static inline const char *
5856 deleg_perm_comment(zfs_deleg_note_t note)
5857 {
5858 	const char *str;
5859 
5860 	/* subcommands */
5861 	switch (note) {
5862 		/* SUBCOMMANDS */
5863 	case ZFS_DELEG_NOTE_ALLOW:
5864 		str = gettext("Must also have the permission that is being"
5865 		    "\n\t\t\t\tallowed");
5866 		break;
5867 	case ZFS_DELEG_NOTE_CLONE:
5868 		str = gettext("Must also have the 'create' ability and 'mount'"
5869 		    "\n\t\t\t\tability in the origin file system");
5870 		break;
5871 	case ZFS_DELEG_NOTE_CREATE:
5872 		str = gettext("Must also have the 'mount' ability");
5873 		break;
5874 	case ZFS_DELEG_NOTE_DESTROY:
5875 		str = gettext("Must also have the 'mount' ability");
5876 		break;
5877 	case ZFS_DELEG_NOTE_DIFF:
5878 		str = gettext("Allows lookup of paths within a dataset;"
5879 		    "\n\t\t\t\tgiven an object number. Ordinary users need this"
5880 		    "\n\t\t\t\tin order to use zfs diff");
5881 		break;
5882 	case ZFS_DELEG_NOTE_HOLD:
5883 		str = gettext("Allows adding a user hold to a snapshot");
5884 		break;
5885 	case ZFS_DELEG_NOTE_MOUNT:
5886 		str = gettext("Allows mount/umount of ZFS datasets");
5887 		break;
5888 	case ZFS_DELEG_NOTE_PROMOTE:
5889 		str = gettext("Must also have the 'mount'\n\t\t\t\tand"
5890 		    " 'promote' ability in the origin file system");
5891 		break;
5892 	case ZFS_DELEG_NOTE_RECEIVE:
5893 		str = gettext("Must also have the 'mount' and 'create'"
5894 		    " ability");
5895 		break;
5896 	case ZFS_DELEG_NOTE_RELEASE:
5897 		str = gettext("Allows releasing a user hold which\n\t\t\t\t"
5898 		    "might destroy the snapshot");
5899 		break;
5900 	case ZFS_DELEG_NOTE_RENAME:
5901 		str = gettext("Must also have the 'mount' and 'create'"
5902 		    "\n\t\t\t\tability in the new parent");
5903 		break;
5904 	case ZFS_DELEG_NOTE_ROLLBACK:
5905 		str = gettext("");
5906 		break;
5907 	case ZFS_DELEG_NOTE_SEND:
5908 		str = gettext("Allow sending datasets");
5909 		break;
5910 	case ZFS_DELEG_NOTE_SEND_RAW:
5911 		str = gettext("Allow sending datasets, but only in 'raw'"
5912 		    "\n\t\t\t\treplication mode");
5913 		break;
5914 	case ZFS_DELEG_NOTE_SEND_ENCRYPTED:
5915 		str = gettext("Allow sending only encrypted datasets, and"
5916 		    "\n\t\t\t\tonly in 'raw' replication mode");
5917 		break;
5918 	case ZFS_DELEG_NOTE_SHARE:
5919 		str = gettext("Allows sharing file systems over NFS or SMB"
5920 		    "\n\t\t\t\tprotocols");
5921 		break;
5922 	case ZFS_DELEG_NOTE_SNAPSHOT:
5923 		str = gettext("");
5924 		break;
5925 	case ZFS_DELEG_NOTE_LOAD_KEY:
5926 		str = gettext("Allows loading or unloading an encryption key");
5927 		break;
5928 	case ZFS_DELEG_NOTE_CHANGE_KEY:
5929 		str = gettext("Allows changing or adding an encryption key");
5930 		break;
5931 /*
5932  *	case ZFS_DELEG_NOTE_VSCAN:
5933  *		str = gettext("");
5934  *		break;
5935  */
5936 		/* OTHER */
5937 	case ZFS_DELEG_NOTE_GROUPQUOTA:
5938 		str = gettext("Allows accessing any groupquota@... property");
5939 		break;
5940 	case ZFS_DELEG_NOTE_GROUPUSED:
5941 		str = gettext("Allows reading any groupused@... property");
5942 		break;
5943 	case ZFS_DELEG_NOTE_USERPROP:
5944 		str = gettext("Allows changing any user property");
5945 		break;
5946 	case ZFS_DELEG_NOTE_USERQUOTA:
5947 		str = gettext("Allows accessing any userquota@... property");
5948 		break;
5949 	case ZFS_DELEG_NOTE_USERUSED:
5950 		str = gettext("Allows reading any userused@... property");
5951 		break;
5952 	case ZFS_DELEG_NOTE_USEROBJQUOTA:
5953 		str = gettext("Allows accessing any userobjquota@... property");
5954 		break;
5955 	case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
5956 		str = gettext("Allows accessing any \n\t\t\t\t"
5957 		    "groupobjquota@... property");
5958 		break;
5959 	case ZFS_DELEG_NOTE_GROUPOBJUSED:
5960 		str = gettext("Allows reading any groupobjused@... property");
5961 		break;
5962 	case ZFS_DELEG_NOTE_USEROBJUSED:
5963 		str = gettext("Allows reading any userobjused@... property");
5964 		break;
5965 	case ZFS_DELEG_NOTE_PROJECTQUOTA:
5966 		str = gettext("Allows accessing any projectquota@... property");
5967 		break;
5968 	case ZFS_DELEG_NOTE_PROJECTOBJQUOTA:
5969 		str = gettext("Allows accessing any \n\t\t\t\t"
5970 		    "projectobjquota@... property");
5971 		break;
5972 	case ZFS_DELEG_NOTE_PROJECTUSED:
5973 		str = gettext("Allows reading any projectused@... property");
5974 		break;
5975 	case ZFS_DELEG_NOTE_PROJECTOBJUSED:
5976 		str = gettext("Allows accessing any \n\t\t\t\t"
5977 		    "projectobjused@... property");
5978 		break;
5979 		/* other */
5980 	default:
5981 		str = "";
5982 	}
5983 
5984 	return (str);
5985 }
5986 
5987 struct allow_opts {
5988 	boolean_t local;
5989 	boolean_t descend;
5990 	boolean_t user;
5991 	boolean_t group;
5992 	boolean_t everyone;
5993 	boolean_t create;
5994 	boolean_t set;
5995 	boolean_t recursive; /* unallow only */
5996 	boolean_t prt_usage;
5997 
5998 	boolean_t prt_perms;
5999 	char *who;
6000 	char *perms;
6001 	const char *dataset;
6002 };
6003 
6004 static inline int
6005 prop_cmp(const void *a, const void *b)
6006 {
6007 	const char *str1 = *(const char **)a;
6008 	const char *str2 = *(const char **)b;
6009 	return (strcmp(str1, str2));
6010 }
6011 
6012 static void
6013 allow_usage(boolean_t un, boolean_t requested, const char *msg)
6014 {
6015 	const char *opt_desc[] = {
6016 		"-h", gettext("show this help message and exit"),
6017 		"-l", gettext("set permission locally"),
6018 		"-d", gettext("set permission for descents"),
6019 		"-u", gettext("set permission for user"),
6020 		"-g", gettext("set permission for group"),
6021 		"-e", gettext("set permission for everyone"),
6022 		"-c", gettext("set create time permission"),
6023 		"-s", gettext("define permission set"),
6024 		/* unallow only */
6025 		"-r", gettext("remove permissions recursively"),
6026 	};
6027 	size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
6028 	size_t allow_size = unallow_size - 2;
6029 	const char *props[ZFS_NUM_PROPS];
6030 	int i;
6031 	size_t count = 0;
6032 	FILE *fp = requested ? stdout : stderr;
6033 	zprop_desc_t *pdtbl = zfs_prop_get_table();
6034 	const char *fmt = gettext("%-16s %-14s\t%s\n");
6035 
6036 	(void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
6037 	    HELP_ALLOW));
6038 	(void) fprintf(fp, gettext("Options:\n"));
6039 	for (i = 0; i < (un ? unallow_size : allow_size); i += 2) {
6040 		const char *opt = opt_desc[i];
6041 		const char *optdsc = opt_desc[i + 1];
6042 		(void) fprintf(fp, gettext("  %-10s  %s\n"), opt, optdsc);
6043 	}
6044 
6045 	(void) fprintf(fp, gettext("\nThe following permissions are "
6046 	    "supported:\n\n"));
6047 	(void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
6048 	    gettext("NOTES"));
6049 	for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
6050 		const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
6051 		zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
6052 		const char *perm_type = deleg_perm_type(perm_note);
6053 		const char *perm_comment = deleg_perm_comment(perm_note);
6054 		(void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
6055 	}
6056 
6057 	for (i = 0; i < ZFS_NUM_PROPS; i++) {
6058 		zprop_desc_t *pd = &pdtbl[i];
6059 		if (pd->pd_visible != B_TRUE)
6060 			continue;
6061 
6062 		if (pd->pd_attr == PROP_READONLY)
6063 			continue;
6064 
6065 		props[count++] = pd->pd_name;
6066 	}
6067 	props[count] = NULL;
6068 
6069 	qsort(props, count, sizeof (char *), prop_cmp);
6070 
6071 	for (i = 0; i < count; i++)
6072 		(void) fprintf(fp, fmt, props[i], gettext("property"), "");
6073 
6074 	if (msg != NULL)
6075 		(void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
6076 
6077 	exit(requested ? 0 : 2);
6078 }
6079 
6080 static inline const char *
6081 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
6082     char **permsp)
6083 {
6084 	if (un && argc == expected_argc - 1)
6085 		*permsp = NULL;
6086 	else if (argc == expected_argc)
6087 		*permsp = argv[argc - 2];
6088 	else
6089 		allow_usage(un, B_FALSE,
6090 		    gettext("wrong number of parameters\n"));
6091 
6092 	return (argv[argc - 1]);
6093 }
6094 
6095 static void
6096 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
6097 {
6098 	int uge_sum = opts->user + opts->group + opts->everyone;
6099 	int csuge_sum = opts->create + opts->set + uge_sum;
6100 	int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
6101 	int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
6102 
6103 	if (uge_sum > 1)
6104 		allow_usage(un, B_FALSE,
6105 		    gettext("-u, -g, and -e are mutually exclusive\n"));
6106 
6107 	if (opts->prt_usage) {
6108 		if (argc == 0 && all_sum == 0)
6109 			allow_usage(un, B_TRUE, NULL);
6110 		else
6111 			usage(B_FALSE);
6112 	}
6113 
6114 	if (opts->set) {
6115 		if (csuge_sum > 1)
6116 			allow_usage(un, B_FALSE,
6117 			    gettext("invalid options combined with -s\n"));
6118 
6119 		opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
6120 		if (argv[0][0] != '@')
6121 			allow_usage(un, B_FALSE,
6122 			    gettext("invalid set name: missing '@' prefix\n"));
6123 		opts->who = argv[0];
6124 	} else if (opts->create) {
6125 		if (ldcsuge_sum > 1)
6126 			allow_usage(un, B_FALSE,
6127 			    gettext("invalid options combined with -c\n"));
6128 		opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
6129 	} else if (opts->everyone) {
6130 		if (csuge_sum > 1)
6131 			allow_usage(un, B_FALSE,
6132 			    gettext("invalid options combined with -e\n"));
6133 		opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
6134 	} else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
6135 	    == 0) {
6136 		opts->everyone = B_TRUE;
6137 		argc--;
6138 		argv++;
6139 		opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
6140 	} else if (argc == 1 && !un) {
6141 		opts->prt_perms = B_TRUE;
6142 		opts->dataset = argv[argc-1];
6143 	} else {
6144 		opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
6145 		opts->who = argv[0];
6146 	}
6147 
6148 	if (!opts->local && !opts->descend) {
6149 		opts->local = B_TRUE;
6150 		opts->descend = B_TRUE;
6151 	}
6152 }
6153 
6154 static void
6155 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
6156     const char *who, char *perms, nvlist_t *top_nvl)
6157 {
6158 	int i;
6159 	char ld[2] = { '\0', '\0' };
6160 	char who_buf[MAXNAMELEN + 32];
6161 	char base_type = '\0';
6162 	char set_type = '\0';
6163 	nvlist_t *base_nvl = NULL;
6164 	nvlist_t *set_nvl = NULL;
6165 	nvlist_t *nvl;
6166 
6167 	if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
6168 		nomem();
6169 	if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) !=  0)
6170 		nomem();
6171 
6172 	switch (type) {
6173 	case ZFS_DELEG_NAMED_SET_SETS:
6174 	case ZFS_DELEG_NAMED_SET:
6175 		set_type = ZFS_DELEG_NAMED_SET_SETS;
6176 		base_type = ZFS_DELEG_NAMED_SET;
6177 		ld[0] = ZFS_DELEG_NA;
6178 		break;
6179 	case ZFS_DELEG_CREATE_SETS:
6180 	case ZFS_DELEG_CREATE:
6181 		set_type = ZFS_DELEG_CREATE_SETS;
6182 		base_type = ZFS_DELEG_CREATE;
6183 		ld[0] = ZFS_DELEG_NA;
6184 		break;
6185 	case ZFS_DELEG_USER_SETS:
6186 	case ZFS_DELEG_USER:
6187 		set_type = ZFS_DELEG_USER_SETS;
6188 		base_type = ZFS_DELEG_USER;
6189 		if (local)
6190 			ld[0] = ZFS_DELEG_LOCAL;
6191 		if (descend)
6192 			ld[1] = ZFS_DELEG_DESCENDENT;
6193 		break;
6194 	case ZFS_DELEG_GROUP_SETS:
6195 	case ZFS_DELEG_GROUP:
6196 		set_type = ZFS_DELEG_GROUP_SETS;
6197 		base_type = ZFS_DELEG_GROUP;
6198 		if (local)
6199 			ld[0] = ZFS_DELEG_LOCAL;
6200 		if (descend)
6201 			ld[1] = ZFS_DELEG_DESCENDENT;
6202 		break;
6203 	case ZFS_DELEG_EVERYONE_SETS:
6204 	case ZFS_DELEG_EVERYONE:
6205 		set_type = ZFS_DELEG_EVERYONE_SETS;
6206 		base_type = ZFS_DELEG_EVERYONE;
6207 		if (local)
6208 			ld[0] = ZFS_DELEG_LOCAL;
6209 		if (descend)
6210 			ld[1] = ZFS_DELEG_DESCENDENT;
6211 		break;
6212 
6213 	default:
6214 		assert(set_type != '\0' && base_type != '\0');
6215 	}
6216 
6217 	if (perms != NULL) {
6218 		char *curr = perms;
6219 		char *end = curr + strlen(perms);
6220 
6221 		while (curr < end) {
6222 			char *delim = strchr(curr, ',');
6223 			if (delim == NULL)
6224 				delim = end;
6225 			else
6226 				*delim = '\0';
6227 
6228 			if (curr[0] == '@')
6229 				nvl = set_nvl;
6230 			else
6231 				nvl = base_nvl;
6232 
6233 			(void) nvlist_add_boolean(nvl, curr);
6234 			if (delim != end)
6235 				*delim = ',';
6236 			curr = delim + 1;
6237 		}
6238 
6239 		for (i = 0; i < 2; i++) {
6240 			char locality = ld[i];
6241 			if (locality == 0)
6242 				continue;
6243 
6244 			if (!nvlist_empty(base_nvl)) {
6245 				if (who != NULL)
6246 					(void) snprintf(who_buf,
6247 					    sizeof (who_buf), "%c%c$%s",
6248 					    base_type, locality, who);
6249 				else
6250 					(void) snprintf(who_buf,
6251 					    sizeof (who_buf), "%c%c$",
6252 					    base_type, locality);
6253 
6254 				(void) nvlist_add_nvlist(top_nvl, who_buf,
6255 				    base_nvl);
6256 			}
6257 
6258 
6259 			if (!nvlist_empty(set_nvl)) {
6260 				if (who != NULL)
6261 					(void) snprintf(who_buf,
6262 					    sizeof (who_buf), "%c%c$%s",
6263 					    set_type, locality, who);
6264 				else
6265 					(void) snprintf(who_buf,
6266 					    sizeof (who_buf), "%c%c$",
6267 					    set_type, locality);
6268 
6269 				(void) nvlist_add_nvlist(top_nvl, who_buf,
6270 				    set_nvl);
6271 			}
6272 		}
6273 	} else {
6274 		for (i = 0; i < 2; i++) {
6275 			char locality = ld[i];
6276 			if (locality == 0)
6277 				continue;
6278 
6279 			if (who != NULL)
6280 				(void) snprintf(who_buf, sizeof (who_buf),
6281 				    "%c%c$%s", base_type, locality, who);
6282 			else
6283 				(void) snprintf(who_buf, sizeof (who_buf),
6284 				    "%c%c$", base_type, locality);
6285 			(void) nvlist_add_boolean(top_nvl, who_buf);
6286 
6287 			if (who != NULL)
6288 				(void) snprintf(who_buf, sizeof (who_buf),
6289 				    "%c%c$%s", set_type, locality, who);
6290 			else
6291 				(void) snprintf(who_buf, sizeof (who_buf),
6292 				    "%c%c$", set_type, locality);
6293 			(void) nvlist_add_boolean(top_nvl, who_buf);
6294 		}
6295 	}
6296 }
6297 
6298 static int
6299 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
6300 {
6301 	if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
6302 		nomem();
6303 
6304 	if (opts->set) {
6305 		store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
6306 		    opts->descend, opts->who, opts->perms, *nvlp);
6307 	} else if (opts->create) {
6308 		store_allow_perm(ZFS_DELEG_CREATE, opts->local,
6309 		    opts->descend, NULL, opts->perms, *nvlp);
6310 	} else if (opts->everyone) {
6311 		store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
6312 		    opts->descend, NULL, opts->perms, *nvlp);
6313 	} else {
6314 		char *curr = opts->who;
6315 		char *end = curr + strlen(curr);
6316 
6317 		while (curr < end) {
6318 			const char *who;
6319 			zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
6320 			char *endch;
6321 			char *delim = strchr(curr, ',');
6322 			char errbuf[256];
6323 			char id[64];
6324 			struct passwd *p = NULL;
6325 			struct group *g = NULL;
6326 
6327 			uid_t rid;
6328 			if (delim == NULL)
6329 				delim = end;
6330 			else
6331 				*delim = '\0';
6332 
6333 			rid = (uid_t)strtol(curr, &endch, 0);
6334 			if (opts->user) {
6335 				who_type = ZFS_DELEG_USER;
6336 				if (*endch != '\0')
6337 					p = getpwnam(curr);
6338 				else
6339 					p = getpwuid(rid);
6340 
6341 				if (p != NULL)
6342 					rid = p->pw_uid;
6343 				else if (*endch != '\0') {
6344 					(void) snprintf(errbuf, sizeof (errbuf),
6345 					    gettext("invalid user %s\n"), curr);
6346 					allow_usage(un, B_TRUE, errbuf);
6347 				}
6348 			} else if (opts->group) {
6349 				who_type = ZFS_DELEG_GROUP;
6350 				if (*endch != '\0')
6351 					g = getgrnam(curr);
6352 				else
6353 					g = getgrgid(rid);
6354 
6355 				if (g != NULL)
6356 					rid = g->gr_gid;
6357 				else if (*endch != '\0') {
6358 					(void) snprintf(errbuf, sizeof (errbuf),
6359 					    gettext("invalid group %s\n"),
6360 					    curr);
6361 					allow_usage(un, B_TRUE, errbuf);
6362 				}
6363 			} else {
6364 				if (*endch != '\0') {
6365 					p = getpwnam(curr);
6366 				} else {
6367 					p = getpwuid(rid);
6368 				}
6369 
6370 				if (p == NULL) {
6371 					if (*endch != '\0') {
6372 						g = getgrnam(curr);
6373 					} else {
6374 						g = getgrgid(rid);
6375 					}
6376 				}
6377 
6378 				if (p != NULL) {
6379 					who_type = ZFS_DELEG_USER;
6380 					rid = p->pw_uid;
6381 				} else if (g != NULL) {
6382 					who_type = ZFS_DELEG_GROUP;
6383 					rid = g->gr_gid;
6384 				} else {
6385 					(void) snprintf(errbuf, sizeof (errbuf),
6386 					    gettext("invalid user/group %s\n"),
6387 					    curr);
6388 					allow_usage(un, B_TRUE, errbuf);
6389 				}
6390 			}
6391 
6392 			(void) sprintf(id, "%u", rid);
6393 			who = id;
6394 
6395 			store_allow_perm(who_type, opts->local,
6396 			    opts->descend, who, opts->perms, *nvlp);
6397 			curr = delim + 1;
6398 		}
6399 	}
6400 
6401 	return (0);
6402 }
6403 
6404 static void
6405 print_set_creat_perms(avl_tree_t *who_avl)
6406 {
6407 	const char *sc_title[] = {
6408 		gettext("Permission sets:\n"),
6409 		gettext("Create time permissions:\n"),
6410 		NULL
6411 	};
6412 	who_perm_node_t *who_node = NULL;
6413 	int prev_weight = -1;
6414 
6415 	for (who_node = avl_first(who_avl); who_node != NULL;
6416 	    who_node = AVL_NEXT(who_avl, who_node)) {
6417 		avl_tree_t *avl = &who_node->who_perm.who_deleg_perm_avl;
6418 		zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
6419 		const char *who_name = who_node->who_perm.who_name;
6420 		int weight = who_type2weight(who_type);
6421 		boolean_t first = B_TRUE;
6422 		deleg_perm_node_t *deleg_node;
6423 
6424 		if (prev_weight != weight) {
6425 			(void) printf("%s", sc_title[weight]);
6426 			prev_weight = weight;
6427 		}
6428 
6429 		if (who_name == NULL || strnlen(who_name, 1) == 0)
6430 			(void) printf("\t");
6431 		else
6432 			(void) printf("\t%s ", who_name);
6433 
6434 		for (deleg_node = avl_first(avl); deleg_node != NULL;
6435 		    deleg_node = AVL_NEXT(avl, deleg_node)) {
6436 			if (first) {
6437 				(void) printf("%s",
6438 				    deleg_node->dpn_perm.dp_name);
6439 				first = B_FALSE;
6440 			} else
6441 				(void) printf(",%s",
6442 				    deleg_node->dpn_perm.dp_name);
6443 		}
6444 
6445 		(void) printf("\n");
6446 	}
6447 }
6448 
6449 static void
6450 print_uge_deleg_perms(avl_tree_t *who_avl, boolean_t local, boolean_t descend,
6451     const char *title)
6452 {
6453 	who_perm_node_t *who_node = NULL;
6454 	boolean_t prt_title = B_TRUE;
6455 
6456 	for (who_node = avl_first(who_avl); who_node != NULL;
6457 	    who_node = AVL_NEXT(who_avl, who_node)) {
6458 		const char *who_name = who_node->who_perm.who_name;
6459 		const char *nice_who_name = who_node->who_perm.who_ug_name;
6460 		avl_tree_t *avl = &who_node->who_perm.who_deleg_perm_avl;
6461 		zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
6462 		char delim = ' ';
6463 		deleg_perm_node_t *deleg_node;
6464 		boolean_t prt_who = B_TRUE;
6465 
6466 		for (deleg_node = avl_first(avl); deleg_node != NULL;
6467 		    deleg_node = AVL_NEXT(avl, deleg_node)) {
6468 			if (local != deleg_node->dpn_perm.dp_local ||
6469 			    descend != deleg_node->dpn_perm.dp_descend)
6470 				continue;
6471 
6472 			if (prt_who) {
6473 				const char *who = NULL;
6474 				if (prt_title) {
6475 					prt_title = B_FALSE;
6476 					(void) printf("%s", title);
6477 				}
6478 
6479 				switch (who_type) {
6480 				case ZFS_DELEG_USER_SETS:
6481 				case ZFS_DELEG_USER:
6482 					who = gettext("user");
6483 					if (nice_who_name)
6484 						who_name  = nice_who_name;
6485 					break;
6486 				case ZFS_DELEG_GROUP_SETS:
6487 				case ZFS_DELEG_GROUP:
6488 					who = gettext("group");
6489 					if (nice_who_name)
6490 						who_name  = nice_who_name;
6491 					break;
6492 				case ZFS_DELEG_EVERYONE_SETS:
6493 				case ZFS_DELEG_EVERYONE:
6494 					who = gettext("everyone");
6495 					who_name = NULL;
6496 					break;
6497 
6498 				default:
6499 					assert(who != NULL);
6500 				}
6501 
6502 				prt_who = B_FALSE;
6503 				if (who_name == NULL)
6504 					(void) printf("\t%s", who);
6505 				else
6506 					(void) printf("\t%s %s", who, who_name);
6507 			}
6508 
6509 			(void) printf("%c%s", delim,
6510 			    deleg_node->dpn_perm.dp_name);
6511 			delim = ',';
6512 		}
6513 
6514 		if (!prt_who)
6515 			(void) printf("\n");
6516 	}
6517 }
6518 
6519 static void
6520 print_fs_perms(fs_perm_set_t *fspset)
6521 {
6522 	fs_perm_node_t *node = NULL;
6523 	char buf[MAXNAMELEN + 32];
6524 	const char *dsname = buf;
6525 
6526 	for (node = list_head(&fspset->fsps_list); node != NULL;
6527 	    node = list_next(&fspset->fsps_list, node)) {
6528 		avl_tree_t *sc_avl = &node->fspn_fsperm.fsp_sc_avl;
6529 		avl_tree_t *uge_avl = &node->fspn_fsperm.fsp_uge_avl;
6530 		int left = 0;
6531 
6532 		(void) snprintf(buf, sizeof (buf),
6533 		    gettext("---- Permissions on %s "),
6534 		    node->fspn_fsperm.fsp_name);
6535 		(void) printf("%s", dsname);
6536 		left = 70 - strlen(buf);
6537 		while (left-- > 0)
6538 			(void) printf("-");
6539 		(void) printf("\n");
6540 
6541 		print_set_creat_perms(sc_avl);
6542 		print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
6543 		    gettext("Local permissions:\n"));
6544 		print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
6545 		    gettext("Descendent permissions:\n"));
6546 		print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
6547 		    gettext("Local+Descendent permissions:\n"));
6548 	}
6549 }
6550 
6551 static fs_perm_set_t fs_perm_set = {};
6552 
6553 struct deleg_perms {
6554 	boolean_t un;
6555 	nvlist_t *nvl;
6556 };
6557 
6558 static int
6559 set_deleg_perms(zfs_handle_t *zhp, void *data)
6560 {
6561 	struct deleg_perms *perms = (struct deleg_perms *)data;
6562 	zfs_type_t zfs_type = zfs_get_type(zhp);
6563 
6564 	if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
6565 		return (0);
6566 
6567 	return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
6568 }
6569 
6570 static int
6571 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
6572 {
6573 	zfs_handle_t *zhp;
6574 	nvlist_t *perm_nvl = NULL;
6575 	nvlist_t *update_perm_nvl = NULL;
6576 	int error = 1;
6577 	int c;
6578 	struct allow_opts opts = { 0 };
6579 
6580 	const char *optstr = un ? "ldugecsrh" : "ldugecsh";
6581 
6582 	/* check opts */
6583 	while ((c = getopt(argc, argv, optstr)) != -1) {
6584 		switch (c) {
6585 		case 'l':
6586 			opts.local = B_TRUE;
6587 			break;
6588 		case 'd':
6589 			opts.descend = B_TRUE;
6590 			break;
6591 		case 'u':
6592 			opts.user = B_TRUE;
6593 			break;
6594 		case 'g':
6595 			opts.group = B_TRUE;
6596 			break;
6597 		case 'e':
6598 			opts.everyone = B_TRUE;
6599 			break;
6600 		case 's':
6601 			opts.set = B_TRUE;
6602 			break;
6603 		case 'c':
6604 			opts.create = B_TRUE;
6605 			break;
6606 		case 'r':
6607 			opts.recursive = B_TRUE;
6608 			break;
6609 		case ':':
6610 			(void) fprintf(stderr, gettext("missing argument for "
6611 			    "'%c' option\n"), optopt);
6612 			usage(B_FALSE);
6613 			break;
6614 		case 'h':
6615 			opts.prt_usage = B_TRUE;
6616 			break;
6617 		case '?':
6618 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6619 			    optopt);
6620 			usage(B_FALSE);
6621 		}
6622 	}
6623 
6624 	argc -= optind;
6625 	argv += optind;
6626 
6627 	/* check arguments */
6628 	parse_allow_args(argc, argv, un, &opts);
6629 
6630 	/* try to open the dataset */
6631 	if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
6632 	    ZFS_TYPE_VOLUME)) == NULL) {
6633 		(void) fprintf(stderr, "Failed to open dataset: %s\n",
6634 		    opts.dataset);
6635 		return (-1);
6636 	}
6637 
6638 	if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
6639 		goto cleanup2;
6640 
6641 	fs_perm_set_init(&fs_perm_set);
6642 	if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
6643 		(void) fprintf(stderr, "Failed to parse fsacl permissions\n");
6644 		goto cleanup1;
6645 	}
6646 
6647 	if (opts.prt_perms)
6648 		print_fs_perms(&fs_perm_set);
6649 	else {
6650 		(void) construct_fsacl_list(un, &opts, &update_perm_nvl);
6651 		if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
6652 			goto cleanup0;
6653 
6654 		if (un && opts.recursive) {
6655 			struct deleg_perms data = { un, update_perm_nvl };
6656 			if (zfs_iter_filesystems_v2(zhp, 0, set_deleg_perms,
6657 			    &data) != 0)
6658 				goto cleanup0;
6659 		}
6660 	}
6661 
6662 	error = 0;
6663 
6664 cleanup0:
6665 	nvlist_free(perm_nvl);
6666 	nvlist_free(update_perm_nvl);
6667 cleanup1:
6668 	fs_perm_set_fini(&fs_perm_set);
6669 cleanup2:
6670 	zfs_close(zhp);
6671 
6672 	return (error);
6673 }
6674 
6675 static int
6676 zfs_do_allow(int argc, char **argv)
6677 {
6678 	return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
6679 }
6680 
6681 static int
6682 zfs_do_unallow(int argc, char **argv)
6683 {
6684 	return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
6685 }
6686 
6687 static int
6688 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
6689 {
6690 	int errors = 0;
6691 	int i;
6692 	const char *tag;
6693 	boolean_t recursive = B_FALSE;
6694 	const char *opts = holding ? "rt" : "r";
6695 	int c;
6696 
6697 	/* check options */
6698 	while ((c = getopt(argc, argv, opts)) != -1) {
6699 		switch (c) {
6700 		case 'r':
6701 			recursive = B_TRUE;
6702 			break;
6703 		case '?':
6704 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6705 			    optopt);
6706 			usage(B_FALSE);
6707 		}
6708 	}
6709 
6710 	argc -= optind;
6711 	argv += optind;
6712 
6713 	/* check number of arguments */
6714 	if (argc < 2)
6715 		usage(B_FALSE);
6716 
6717 	tag = argv[0];
6718 	--argc;
6719 	++argv;
6720 
6721 	if (holding && tag[0] == '.') {
6722 		/* tags starting with '.' are reserved for libzfs */
6723 		(void) fprintf(stderr, gettext("tag may not start with '.'\n"));
6724 		usage(B_FALSE);
6725 	}
6726 
6727 	for (i = 0; i < argc; ++i) {
6728 		zfs_handle_t *zhp;
6729 		char parent[ZFS_MAX_DATASET_NAME_LEN];
6730 		const char *delim;
6731 		char *path = argv[i];
6732 
6733 		delim = strchr(path, '@');
6734 		if (delim == NULL) {
6735 			(void) fprintf(stderr,
6736 			    gettext("'%s' is not a snapshot\n"), path);
6737 			++errors;
6738 			continue;
6739 		}
6740 		(void) strlcpy(parent, path, MIN(sizeof (parent),
6741 		    delim - path + 1));
6742 
6743 		zhp = zfs_open(g_zfs, parent,
6744 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
6745 		if (zhp == NULL) {
6746 			++errors;
6747 			continue;
6748 		}
6749 		if (holding) {
6750 			if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
6751 				++errors;
6752 		} else {
6753 			if (zfs_release(zhp, delim+1, tag, recursive) != 0)
6754 				++errors;
6755 		}
6756 		zfs_close(zhp);
6757 	}
6758 
6759 	return (errors != 0);
6760 }
6761 
6762 /*
6763  * zfs hold [-r] [-t] <tag> <snap> ...
6764  *
6765  *	-r	Recursively hold
6766  *
6767  * Apply a user-hold with the given tag to the list of snapshots.
6768  */
6769 static int
6770 zfs_do_hold(int argc, char **argv)
6771 {
6772 	return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
6773 }
6774 
6775 /*
6776  * zfs release [-r] <tag> <snap> ...
6777  *
6778  *	-r	Recursively release
6779  *
6780  * Release a user-hold with the given tag from the list of snapshots.
6781  */
6782 static int
6783 zfs_do_release(int argc, char **argv)
6784 {
6785 	return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
6786 }
6787 
6788 typedef struct holds_cbdata {
6789 	boolean_t	cb_recursive;
6790 	const char	*cb_snapname;
6791 	nvlist_t	**cb_nvlp;
6792 	size_t		cb_max_namelen;
6793 	size_t		cb_max_taglen;
6794 } holds_cbdata_t;
6795 
6796 #define	STRFTIME_FMT_STR "%a %b %e %H:%M %Y"
6797 #define	DATETIME_BUF_LEN (32)
6798 /*
6799  *
6800  */
6801 static void
6802 print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl,
6803     boolean_t parsable)
6804 {
6805 	int i;
6806 	nvpair_t *nvp = NULL;
6807 	const char *const hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
6808 	const char *col;
6809 
6810 	if (!scripted) {
6811 		for (i = 0; i < 3; i++) {
6812 			col = gettext(hdr_cols[i]);
6813 			if (i < 2)
6814 				(void) printf("%-*s  ", i ? tagwidth : nwidth,
6815 				    col);
6816 			else
6817 				(void) printf("%s\n", col);
6818 		}
6819 	}
6820 
6821 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
6822 		const char *zname = nvpair_name(nvp);
6823 		nvlist_t *nvl2;
6824 		nvpair_t *nvp2 = NULL;
6825 		(void) nvpair_value_nvlist(nvp, &nvl2);
6826 		while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
6827 			char tsbuf[DATETIME_BUF_LEN];
6828 			const char *tagname = nvpair_name(nvp2);
6829 			uint64_t val = 0;
6830 			time_t time;
6831 			struct tm t;
6832 
6833 			(void) nvpair_value_uint64(nvp2, &val);
6834 			time = (time_t)val;
6835 			(void) localtime_r(&time, &t);
6836 			(void) strftime(tsbuf, DATETIME_BUF_LEN,
6837 			    gettext(STRFTIME_FMT_STR), &t);
6838 
6839 			if (scripted) {
6840 				if (parsable) {
6841 					(void) printf("%s\t%s\t%lld\n", zname,
6842 					    tagname, (long long)time);
6843 				} else {
6844 					(void) printf("%s\t%s\t%s\n", zname,
6845 					    tagname, tsbuf);
6846 				}
6847 			} else {
6848 				if (parsable) {
6849 					(void) printf("%-*s  %-*s  %lld\n",
6850 					    nwidth, zname, tagwidth,
6851 					    tagname, (long long)time);
6852 				} else {
6853 					(void) printf("%-*s  %-*s  %s\n",
6854 					    nwidth, zname, tagwidth,
6855 					    tagname, tsbuf);
6856 				}
6857 			}
6858 		}
6859 	}
6860 }
6861 
6862 /*
6863  * Generic callback function to list a dataset or snapshot.
6864  */
6865 static int
6866 holds_callback(zfs_handle_t *zhp, void *data)
6867 {
6868 	holds_cbdata_t *cbp = data;
6869 	nvlist_t *top_nvl = *cbp->cb_nvlp;
6870 	nvlist_t *nvl = NULL;
6871 	nvpair_t *nvp = NULL;
6872 	const char *zname = zfs_get_name(zhp);
6873 	size_t znamelen = strlen(zname);
6874 
6875 	if (cbp->cb_recursive) {
6876 		const char *snapname;
6877 		const char *delim  = strchr(zname, '@');
6878 		if (delim == NULL)
6879 			return (0);
6880 
6881 		snapname = delim + 1;
6882 		if (strcmp(cbp->cb_snapname, snapname))
6883 			return (0);
6884 	}
6885 
6886 	if (zfs_get_holds(zhp, &nvl) != 0)
6887 		return (-1);
6888 
6889 	if (znamelen > cbp->cb_max_namelen)
6890 		cbp->cb_max_namelen  = znamelen;
6891 
6892 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
6893 		const char *tag = nvpair_name(nvp);
6894 		size_t taglen = strlen(tag);
6895 		if (taglen > cbp->cb_max_taglen)
6896 			cbp->cb_max_taglen  = taglen;
6897 	}
6898 
6899 	return (nvlist_add_nvlist(top_nvl, zname, nvl));
6900 }
6901 
6902 /*
6903  * zfs holds [-rHp] <snap> ...
6904  *
6905  *	-r	Lists holds that are set on the named snapshots recursively.
6906  *	-H	Scripted mode; elide headers and separate columns by tabs.
6907  *	-p	Display values in parsable (literal) format.
6908  */
6909 static int
6910 zfs_do_holds(int argc, char **argv)
6911 {
6912 	int c;
6913 	boolean_t errors = B_FALSE;
6914 	boolean_t scripted = B_FALSE;
6915 	boolean_t recursive = B_FALSE;
6916 	boolean_t parsable = B_FALSE;
6917 
6918 	int types = ZFS_TYPE_SNAPSHOT;
6919 	holds_cbdata_t cb = { 0 };
6920 
6921 	int limit = 0;
6922 	int ret = 0;
6923 	int flags = 0;
6924 
6925 	/* check options */
6926 	while ((c = getopt(argc, argv, "rHp")) != -1) {
6927 		switch (c) {
6928 		case 'r':
6929 			recursive = B_TRUE;
6930 			break;
6931 		case 'H':
6932 			scripted = B_TRUE;
6933 			break;
6934 		case 'p':
6935 			parsable = B_TRUE;
6936 			break;
6937 		case '?':
6938 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6939 			    optopt);
6940 			usage(B_FALSE);
6941 		}
6942 	}
6943 
6944 	if (recursive) {
6945 		types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
6946 		flags |= ZFS_ITER_RECURSE;
6947 	}
6948 
6949 	argc -= optind;
6950 	argv += optind;
6951 
6952 	/* check number of arguments */
6953 	if (argc < 1)
6954 		usage(B_FALSE);
6955 
6956 	nvlist_t *nvl = fnvlist_alloc();
6957 
6958 	for (int i = 0; i < argc; ++i) {
6959 		char *snapshot = argv[i];
6960 		const char *delim;
6961 		const char *snapname;
6962 
6963 		delim = strchr(snapshot, '@');
6964 		if (delim == NULL) {
6965 			(void) fprintf(stderr,
6966 			    gettext("'%s' is not a snapshot\n"), snapshot);
6967 			errors = B_TRUE;
6968 			continue;
6969 		}
6970 		snapname = delim + 1;
6971 		if (recursive)
6972 			snapshot[delim - snapshot] = '\0';
6973 
6974 		cb.cb_recursive = recursive;
6975 		cb.cb_snapname = snapname;
6976 		cb.cb_nvlp = &nvl;
6977 
6978 		/*
6979 		 *  1. collect holds data, set format options
6980 		 */
6981 		ret = zfs_for_each(1, argv + i, flags, types, NULL, NULL, limit,
6982 		    holds_callback, &cb);
6983 		if (ret != 0)
6984 			errors = B_TRUE;
6985 	}
6986 
6987 	/*
6988 	 *  2. print holds data
6989 	 */
6990 	print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl,
6991 	    parsable);
6992 
6993 	if (nvlist_empty(nvl))
6994 		(void) fprintf(stderr, gettext("no datasets available\n"));
6995 
6996 	nvlist_free(nvl);
6997 
6998 	return (errors);
6999 }
7000 
7001 #define	CHECK_SPINNER 30
7002 #define	SPINNER_TIME 3		/* seconds */
7003 #define	MOUNT_TIME 1		/* seconds */
7004 
7005 typedef struct get_all_state {
7006 	char		**ga_datasets;
7007 	int		ga_count;
7008 	boolean_t	ga_verbose;
7009 	get_all_cb_t	*ga_cbp;
7010 } get_all_state_t;
7011 
7012 static int
7013 get_one_dataset(zfs_handle_t *zhp, void *data)
7014 {
7015 	static const char *const spin[] = { "-", "\\", "|", "/" };
7016 	static int spinval = 0;
7017 	static int spincheck = 0;
7018 	static time_t last_spin_time = (time_t)0;
7019 	get_all_state_t *state = data;
7020 	zfs_type_t type = zfs_get_type(zhp);
7021 
7022 	if (state->ga_verbose) {
7023 		if (--spincheck < 0) {
7024 			time_t now = time(NULL);
7025 			if (last_spin_time + SPINNER_TIME < now) {
7026 				update_progress(spin[spinval++ % 4]);
7027 				last_spin_time = now;
7028 			}
7029 			spincheck = CHECK_SPINNER;
7030 		}
7031 	}
7032 
7033 	/*
7034 	 * Iterate over any nested datasets.
7035 	 */
7036 	if (zfs_iter_filesystems_v2(zhp, 0, get_one_dataset, data) != 0) {
7037 		zfs_close(zhp);
7038 		return (1);
7039 	}
7040 
7041 	/*
7042 	 * Skip any datasets whose type does not match.
7043 	 */
7044 	if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
7045 		zfs_close(zhp);
7046 		return (0);
7047 	}
7048 	libzfs_add_handle(state->ga_cbp, zhp);
7049 	assert(state->ga_cbp->cb_used <= state->ga_cbp->cb_alloc);
7050 
7051 	return (0);
7052 }
7053 
7054 static int
7055 get_recursive_datasets(zfs_handle_t *zhp, void *data)
7056 {
7057 	get_all_state_t *state = data;
7058 	int len = strlen(zfs_get_name(zhp));
7059 	for (int i = 0; i < state->ga_count; ++i) {
7060 		if (strcmp(state->ga_datasets[i], zfs_get_name(zhp)) == 0)
7061 			return (get_one_dataset(zhp, data));
7062 		else if ((strncmp(state->ga_datasets[i], zfs_get_name(zhp),
7063 		    len) == 0) && state->ga_datasets[i][len] == '/') {
7064 			(void) zfs_iter_filesystems_v2(zhp, 0,
7065 			    get_recursive_datasets, data);
7066 		}
7067 	}
7068 	zfs_close(zhp);
7069 	return (0);
7070 }
7071 
7072 static void
7073 get_all_datasets(get_all_state_t *state)
7074 {
7075 	if (state->ga_verbose)
7076 		set_progress_header(gettext("Reading ZFS config"));
7077 	if (state->ga_datasets == NULL)
7078 		(void) zfs_iter_root(g_zfs, get_one_dataset, state);
7079 	else
7080 		(void) zfs_iter_root(g_zfs, get_recursive_datasets, state);
7081 
7082 	if (state->ga_verbose)
7083 		finish_progress(gettext("done."));
7084 }
7085 
7086 /*
7087  * Generic callback for sharing or mounting filesystems.  Because the code is so
7088  * similar, we have a common function with an extra parameter to determine which
7089  * mode we are using.
7090  */
7091 typedef enum { OP_SHARE, OP_MOUNT } share_mount_op_t;
7092 
7093 typedef struct share_mount_state {
7094 	share_mount_op_t	sm_op;
7095 	boolean_t	sm_verbose;
7096 	int	sm_flags;
7097 	char	*sm_options;
7098 	enum sa_protocol	sm_proto; /* only valid for OP_SHARE */
7099 	pthread_mutex_t	sm_lock; /* protects the remaining fields */
7100 	uint_t	sm_total; /* number of filesystems to process */
7101 	uint_t	sm_done; /* number of filesystems processed */
7102 	int	sm_status; /* -1 if any of the share/mount operations failed */
7103 } share_mount_state_t;
7104 
7105 /*
7106  * Share or mount a dataset.
7107  */
7108 static int
7109 share_mount_one(zfs_handle_t *zhp, int op, int flags, enum sa_protocol protocol,
7110     boolean_t explicit, const char *options)
7111 {
7112 	char mountpoint[ZFS_MAXPROPLEN];
7113 	char shareopts[ZFS_MAXPROPLEN];
7114 	char smbshareopts[ZFS_MAXPROPLEN];
7115 	const char *cmdname = op == OP_SHARE ? "share" : "mount";
7116 	struct mnttab mnt;
7117 	uint64_t zoned, canmount;
7118 	boolean_t shared_nfs, shared_smb;
7119 
7120 	assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
7121 
7122 	/*
7123 	 * Check to make sure we can mount/share this dataset.  If we
7124 	 * are in the global zone and the filesystem is exported to a
7125 	 * local zone, or if we are in a local zone and the
7126 	 * filesystem is not exported, then it is an error.
7127 	 */
7128 	zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
7129 
7130 	if (zoned && getzoneid() == GLOBAL_ZONEID) {
7131 		if (!explicit)
7132 			return (0);
7133 
7134 		(void) fprintf(stderr, gettext("cannot %s '%s': "
7135 		    "dataset is exported to a local zone\n"), cmdname,
7136 		    zfs_get_name(zhp));
7137 		return (1);
7138 
7139 	} else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
7140 		if (!explicit)
7141 			return (0);
7142 
7143 		(void) fprintf(stderr, gettext("cannot %s '%s': "
7144 		    "permission denied\n"), cmdname,
7145 		    zfs_get_name(zhp));
7146 		return (1);
7147 	}
7148 
7149 	/*
7150 	 * Ignore any filesystems which don't apply to us. This
7151 	 * includes those with a legacy mountpoint, or those with
7152 	 * legacy share options.
7153 	 */
7154 	verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
7155 	    sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
7156 	verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
7157 	    sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
7158 	verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
7159 	    sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
7160 
7161 	if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
7162 	    strcmp(smbshareopts, "off") == 0) {
7163 		if (!explicit)
7164 			return (0);
7165 
7166 		(void) fprintf(stderr, gettext("cannot share '%s': "
7167 		    "legacy share\n"), zfs_get_name(zhp));
7168 		(void) fprintf(stderr, gettext("use exports(5) or "
7169 		    "smb.conf(5) to share this filesystem, or set "
7170 		    "the sharenfs or sharesmb property\n"));
7171 		return (1);
7172 	}
7173 
7174 	/*
7175 	 * We cannot share or mount legacy filesystems. If the
7176 	 * shareopts is non-legacy but the mountpoint is legacy, we
7177 	 * treat it as a legacy share.
7178 	 */
7179 	if (strcmp(mountpoint, "legacy") == 0) {
7180 		if (!explicit)
7181 			return (0);
7182 
7183 		(void) fprintf(stderr, gettext("cannot %s '%s': "
7184 		    "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
7185 		(void) fprintf(stderr, gettext("use %s(8) to "
7186 		    "%s this filesystem\n"), cmdname, cmdname);
7187 		return (1);
7188 	}
7189 
7190 	if (strcmp(mountpoint, "none") == 0) {
7191 		if (!explicit)
7192 			return (0);
7193 
7194 		(void) fprintf(stderr, gettext("cannot %s '%s': no "
7195 		    "mountpoint set\n"), cmdname, zfs_get_name(zhp));
7196 		return (1);
7197 	}
7198 
7199 	/*
7200 	 * canmount	explicit	outcome
7201 	 * on		no		pass through
7202 	 * on		yes		pass through
7203 	 * off		no		return 0
7204 	 * off		yes		display error, return 1
7205 	 * noauto	no		return 0
7206 	 * noauto	yes		pass through
7207 	 */
7208 	canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
7209 	if (canmount == ZFS_CANMOUNT_OFF) {
7210 		if (!explicit)
7211 			return (0);
7212 
7213 		(void) fprintf(stderr, gettext("cannot %s '%s': "
7214 		    "'canmount' property is set to 'off'\n"), cmdname,
7215 		    zfs_get_name(zhp));
7216 		return (1);
7217 	} else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
7218 		/*
7219 		 * When performing a 'zfs mount -a', we skip any mounts for
7220 		 * datasets that have 'noauto' set. Sharing a dataset with
7221 		 * 'noauto' set is only allowed if it's mounted.
7222 		 */
7223 		if (op == OP_MOUNT)
7224 			return (0);
7225 		if (op == OP_SHARE && !zfs_is_mounted(zhp, NULL)) {
7226 			/* also purge it from existing exports */
7227 			zfs_unshare(zhp, mountpoint, NULL);
7228 			return (0);
7229 		}
7230 	}
7231 
7232 	/*
7233 	 * If this filesystem is encrypted and does not have
7234 	 * a loaded key, we can not mount it.
7235 	 */
7236 	if ((flags & MS_CRYPT) == 0 &&
7237 	    zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF &&
7238 	    zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
7239 	    ZFS_KEYSTATUS_UNAVAILABLE) {
7240 		if (!explicit)
7241 			return (0);
7242 
7243 		(void) fprintf(stderr, gettext("cannot %s '%s': "
7244 		    "encryption key not loaded\n"), cmdname, zfs_get_name(zhp));
7245 		return (1);
7246 	}
7247 
7248 	/*
7249 	 * If this filesystem is inconsistent and has a receive resume
7250 	 * token, we can not mount it.
7251 	 */
7252 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
7253 	    zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
7254 	    NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
7255 		if (!explicit)
7256 			return (0);
7257 
7258 		(void) fprintf(stderr, gettext("cannot %s '%s': "
7259 		    "Contains partially-completed state from "
7260 		    "\"zfs receive -s\", which can be resumed with "
7261 		    "\"zfs send -t\"\n"),
7262 		    cmdname, zfs_get_name(zhp));
7263 		return (1);
7264 	}
7265 
7266 	if (zfs_prop_get_int(zhp, ZFS_PROP_REDACTED) && !(flags & MS_FORCE)) {
7267 		if (!explicit)
7268 			return (0);
7269 
7270 		(void) fprintf(stderr, gettext("cannot %s '%s': "
7271 		    "Dataset is not complete, was created by receiving "
7272 		    "a redacted zfs send stream.\n"), cmdname,
7273 		    zfs_get_name(zhp));
7274 		return (1);
7275 	}
7276 
7277 	/*
7278 	 * At this point, we have verified that the mountpoint and/or
7279 	 * shareopts are appropriate for auto management. If the
7280 	 * filesystem is already mounted or shared, return (failing
7281 	 * for explicit requests); otherwise mount or share the
7282 	 * filesystem.
7283 	 */
7284 	switch (op) {
7285 	case OP_SHARE: {
7286 		enum sa_protocol prot[] = {SA_PROTOCOL_NFS, SA_NO_PROTOCOL};
7287 		shared_nfs = zfs_is_shared(zhp, NULL, prot);
7288 		*prot = SA_PROTOCOL_SMB;
7289 		shared_smb = zfs_is_shared(zhp, NULL, prot);
7290 
7291 		if ((shared_nfs && shared_smb) ||
7292 		    (shared_nfs && strcmp(shareopts, "on") == 0 &&
7293 		    strcmp(smbshareopts, "off") == 0) ||
7294 		    (shared_smb && strcmp(smbshareopts, "on") == 0 &&
7295 		    strcmp(shareopts, "off") == 0)) {
7296 			if (!explicit)
7297 				return (0);
7298 
7299 			(void) fprintf(stderr, gettext("cannot share "
7300 			    "'%s': filesystem already shared\n"),
7301 			    zfs_get_name(zhp));
7302 			return (1);
7303 		}
7304 
7305 		if (!zfs_is_mounted(zhp, NULL) &&
7306 		    zfs_mount(zhp, NULL, flags) != 0)
7307 			return (1);
7308 
7309 		*prot = protocol;
7310 		if (zfs_share(zhp, protocol == SA_NO_PROTOCOL ? NULL : prot))
7311 			return (1);
7312 
7313 	}
7314 		break;
7315 
7316 	case OP_MOUNT:
7317 		mnt.mnt_mntopts = (char *)(options ?: "");
7318 
7319 		if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
7320 		    zfs_is_mounted(zhp, NULL)) {
7321 			if (!explicit)
7322 				return (0);
7323 
7324 			(void) fprintf(stderr, gettext("cannot mount "
7325 			    "'%s': filesystem already mounted\n"),
7326 			    zfs_get_name(zhp));
7327 			return (1);
7328 		}
7329 
7330 		if (zfs_mount(zhp, options, flags) != 0)
7331 			return (1);
7332 		break;
7333 	}
7334 
7335 	return (0);
7336 }
7337 
7338 /*
7339  * Reports progress in the form "(current/total)".  Not thread-safe.
7340  */
7341 static void
7342 report_mount_progress(int current, int total)
7343 {
7344 	static time_t last_progress_time = 0;
7345 	time_t now = time(NULL);
7346 	char info[32];
7347 
7348 	/* display header if we're here for the first time */
7349 	if (current == 1) {
7350 		set_progress_header(gettext("Mounting ZFS filesystems"));
7351 	} else if (current != total && last_progress_time + MOUNT_TIME >= now) {
7352 		/* too soon to report again */
7353 		return;
7354 	}
7355 
7356 	last_progress_time = now;
7357 
7358 	(void) sprintf(info, "(%d/%d)", current, total);
7359 
7360 	if (current == total)
7361 		finish_progress(info);
7362 	else
7363 		update_progress(info);
7364 }
7365 
7366 /*
7367  * zfs_foreach_mountpoint() callback that mounts or shares one filesystem and
7368  * updates the progress meter.
7369  */
7370 static int
7371 share_mount_one_cb(zfs_handle_t *zhp, void *arg)
7372 {
7373 	share_mount_state_t *sms = arg;
7374 	int ret;
7375 
7376 	ret = share_mount_one(zhp, sms->sm_op, sms->sm_flags, sms->sm_proto,
7377 	    B_FALSE, sms->sm_options);
7378 
7379 	pthread_mutex_lock(&sms->sm_lock);
7380 	if (ret != 0)
7381 		sms->sm_status = ret;
7382 	sms->sm_done++;
7383 	if (sms->sm_verbose)
7384 		report_mount_progress(sms->sm_done, sms->sm_total);
7385 	pthread_mutex_unlock(&sms->sm_lock);
7386 	return (ret);
7387 }
7388 
7389 static void
7390 append_options(char *mntopts, char *newopts)
7391 {
7392 	int len = strlen(mntopts);
7393 
7394 	/* original length plus new string to append plus 1 for the comma */
7395 	if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
7396 		(void) fprintf(stderr, gettext("the opts argument for "
7397 		    "'%s' option is too long (more than %d chars)\n"),
7398 		    "-o", MNT_LINE_MAX);
7399 		usage(B_FALSE);
7400 	}
7401 
7402 	if (*mntopts)
7403 		mntopts[len++] = ',';
7404 
7405 	(void) strcpy(&mntopts[len], newopts);
7406 }
7407 
7408 static enum sa_protocol
7409 sa_protocol_decode(const char *protocol)
7410 {
7411 	for (enum sa_protocol i = 0; i < SA_PROTOCOL_COUNT; ++i)
7412 		if (strcmp(protocol, zfs_share_protocol_name(i)) == 0)
7413 			return (i);
7414 
7415 	(void) fputs(gettext("share type must be one of: "), stderr);
7416 	for (enum sa_protocol i = 0; i < SA_PROTOCOL_COUNT; ++i)
7417 		(void) fprintf(stderr, "%s%s",
7418 		    i != 0 ? ", " : "", zfs_share_protocol_name(i));
7419 	(void) fputc('\n', stderr);
7420 	usage(B_FALSE);
7421 }
7422 
7423 static int
7424 share_mount(int op, int argc, char **argv)
7425 {
7426 	int do_all = 0;
7427 	int recursive = 0;
7428 	boolean_t verbose = B_FALSE;
7429 	boolean_t json = B_FALSE;
7430 	int c, ret = 0;
7431 	char *options = NULL;
7432 	int flags = 0;
7433 	nvlist_t *jsobj, *data, *item;
7434 	const uint_t mount_nthr = 512;
7435 	uint_t nthr;
7436 	jsobj = data = item = NULL;
7437 
7438 	struct option long_options[] = {
7439 		{"json", no_argument, NULL, 'j'},
7440 		{0, 0, 0, 0}
7441 	};
7442 
7443 	/* check options */
7444 	while ((c = getopt_long(argc, argv,
7445 	    op == OP_MOUNT ? ":ajRlvo:Of" : "al",
7446 	    op == OP_MOUNT ? long_options : NULL, NULL)) != -1) {
7447 		switch (c) {
7448 		case 'a':
7449 			do_all = 1;
7450 			break;
7451 		case 'R':
7452 			recursive = 1;
7453 			break;
7454 		case 'v':
7455 			verbose = B_TRUE;
7456 			break;
7457 		case 'l':
7458 			flags |= MS_CRYPT;
7459 			break;
7460 		case 'j':
7461 			json = B_TRUE;
7462 			jsobj = zfs_json_schema(0, 1);
7463 			data = fnvlist_alloc();
7464 			break;
7465 		case 'o':
7466 			if (*optarg == '\0') {
7467 				(void) fprintf(stderr, gettext("empty mount "
7468 				    "options (-o) specified\n"));
7469 				usage(B_FALSE);
7470 			}
7471 
7472 			if (options == NULL)
7473 				options = safe_malloc(MNT_LINE_MAX + 1);
7474 
7475 			/* option validation is done later */
7476 			append_options(options, optarg);
7477 			break;
7478 		case 'O':
7479 			flags |= MS_OVERLAY;
7480 			break;
7481 		case 'f':
7482 			flags |= MS_FORCE;
7483 			break;
7484 		case ':':
7485 			(void) fprintf(stderr, gettext("missing argument for "
7486 			    "'%c' option\n"), optopt);
7487 			usage(B_FALSE);
7488 			break;
7489 		case '?':
7490 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
7491 			    optopt);
7492 			usage(B_FALSE);
7493 		}
7494 	}
7495 
7496 	argc -= optind;
7497 	argv += optind;
7498 
7499 	if (json && argc != 0) {
7500 		(void) fprintf(stderr, gettext("too many arguments\n"));
7501 		usage(B_FALSE);
7502 	}
7503 
7504 	/* check number of arguments */
7505 	if (do_all || recursive) {
7506 		enum sa_protocol protocol = SA_NO_PROTOCOL;
7507 
7508 		if (op == OP_SHARE && argc > 0) {
7509 			protocol = sa_protocol_decode(argv[0]);
7510 			argc--;
7511 			argv++;
7512 		}
7513 
7514 		if (argc != 0 && do_all) {
7515 			(void) fprintf(stderr, gettext("too many arguments\n"));
7516 			usage(B_FALSE);
7517 		}
7518 
7519 		if (argc == 0 && recursive) {
7520 			(void) fprintf(stderr,
7521 			    gettext("no dataset provided\n"));
7522 			usage(B_FALSE);
7523 		}
7524 
7525 		start_progress_timer();
7526 		get_all_cb_t cb = { 0 };
7527 		get_all_state_t state = { 0 };
7528 		if (argc == 0) {
7529 			state.ga_datasets = NULL;
7530 			state.ga_count = -1;
7531 		} else {
7532 			zfs_handle_t *zhp;
7533 			for (int i = 0; i < argc; i++) {
7534 				zhp = zfs_open(g_zfs, argv[i],
7535 				    ZFS_TYPE_FILESYSTEM);
7536 				if (zhp == NULL)
7537 					usage(B_FALSE);
7538 				zfs_close(zhp);
7539 			}
7540 			state.ga_datasets = argv;
7541 			state.ga_count = argc;
7542 		}
7543 		state.ga_verbose = verbose;
7544 		state.ga_cbp = &cb;
7545 		get_all_datasets(&state);
7546 
7547 		if (cb.cb_used == 0) {
7548 			free(options);
7549 			return (0);
7550 		}
7551 
7552 		share_mount_state_t share_mount_state = { 0 };
7553 		share_mount_state.sm_op = op;
7554 		share_mount_state.sm_verbose = verbose;
7555 		share_mount_state.sm_flags = flags;
7556 		share_mount_state.sm_options = options;
7557 		share_mount_state.sm_proto = protocol;
7558 		share_mount_state.sm_total = cb.cb_used;
7559 		pthread_mutex_init(&share_mount_state.sm_lock, NULL);
7560 
7561 		/* For a 'zfs share -a' operation start with a clean slate. */
7562 		if (op == OP_SHARE)
7563 			zfs_truncate_shares(NULL);
7564 
7565 		/*
7566 		 * libshare isn't mt-safe, so only do the operation in parallel
7567 		 * if we're mounting. Additionally, the key-loading option must
7568 		 * be serialized so that we can prompt the user for their keys
7569 		 * in a consistent manner.
7570 		 */
7571 		nthr = op == OP_MOUNT && !(flags & MS_CRYPT) ? mount_nthr : 1;
7572 		zfs_foreach_mountpoint(g_zfs, cb.cb_handles, cb.cb_used,
7573 		    share_mount_one_cb, &share_mount_state, nthr);
7574 		zfs_commit_shares(NULL);
7575 
7576 		ret = share_mount_state.sm_status;
7577 
7578 		for (int i = 0; i < cb.cb_used; i++)
7579 			zfs_close(cb.cb_handles[i]);
7580 		free(cb.cb_handles);
7581 	} else if (argc == 0) {
7582 		FILE *mnttab;
7583 		struct mnttab entry;
7584 
7585 		if ((op == OP_SHARE) || (options != NULL)) {
7586 			(void) fprintf(stderr, gettext("missing filesystem "
7587 			    "argument (specify -a for all)\n"));
7588 			usage(B_FALSE);
7589 		}
7590 
7591 		/*
7592 		 * When mount is given no arguments, go through
7593 		 * /proc/self/mounts and display any active ZFS mounts.
7594 		 * We hide any snapshots, since they are controlled
7595 		 * automatically.
7596 		 */
7597 
7598 		if ((mnttab = fopen(MNTTAB, "re")) == NULL) {
7599 			free(options);
7600 			return (ENOENT);
7601 		}
7602 
7603 		while (getmntent(mnttab, &entry) == 0) {
7604 			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
7605 			    strchr(entry.mnt_special, '@') != NULL)
7606 				continue;
7607 			if (json) {
7608 				item = fnvlist_alloc();
7609 				fnvlist_add_string(item, "filesystem",
7610 				    entry.mnt_special);
7611 				fnvlist_add_string(item, "mountpoint",
7612 				    entry.mnt_mountp);
7613 				fnvlist_add_nvlist(data, entry.mnt_special,
7614 				    item);
7615 				fnvlist_free(item);
7616 			} else {
7617 				(void) printf("%-30s  %s\n", entry.mnt_special,
7618 				    entry.mnt_mountp);
7619 			}
7620 		}
7621 
7622 		(void) fclose(mnttab);
7623 		if (json) {
7624 			fnvlist_add_nvlist(jsobj, "datasets", data);
7625 			if (nvlist_empty(data))
7626 				fnvlist_free(jsobj);
7627 			else
7628 				zcmd_print_json(jsobj);
7629 			fnvlist_free(data);
7630 		}
7631 	} else {
7632 		zfs_handle_t *zhp;
7633 
7634 		if (argc > 1) {
7635 			(void) fprintf(stderr,
7636 			    gettext("too many arguments\n"));
7637 			usage(B_FALSE);
7638 		}
7639 
7640 		if ((zhp = zfs_open(g_zfs, argv[0],
7641 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
7642 			ret = 1;
7643 		} else {
7644 			ret = share_mount_one(zhp, op, flags, SA_NO_PROTOCOL,
7645 			    B_TRUE, options);
7646 			zfs_commit_shares(NULL);
7647 			zfs_close(zhp);
7648 		}
7649 	}
7650 
7651 	free(options);
7652 	return (ret);
7653 }
7654 
7655 /*
7656  * zfs mount -a
7657  * zfs mount filesystem
7658  *
7659  * Mount all filesystems, or mount the given filesystem.
7660  */
7661 static int
7662 zfs_do_mount(int argc, char **argv)
7663 {
7664 	return (share_mount(OP_MOUNT, argc, argv));
7665 }
7666 
7667 /*
7668  * zfs share -a [nfs | smb]
7669  * zfs share filesystem
7670  *
7671  * Share all filesystems, or share the given filesystem.
7672  */
7673 static int
7674 zfs_do_share(int argc, char **argv)
7675 {
7676 	return (share_mount(OP_SHARE, argc, argv));
7677 }
7678 
7679 typedef struct unshare_unmount_node {
7680 	zfs_handle_t	*un_zhp;
7681 	char		*un_mountp;
7682 	avl_node_t	un_avlnode;
7683 } unshare_unmount_node_t;
7684 
7685 static int
7686 unshare_unmount_compare(const void *larg, const void *rarg)
7687 {
7688 	const unshare_unmount_node_t *l = larg;
7689 	const unshare_unmount_node_t *r = rarg;
7690 
7691 	return (TREE_ISIGN(strcmp(l->un_mountp, r->un_mountp)));
7692 }
7693 
7694 /*
7695  * Convenience routine used by zfs_do_umount() and manual_unmount().  Given an
7696  * absolute path, find the entry /proc/self/mounts, verify that it's a
7697  * ZFS filesystem, and unmount it appropriately.
7698  */
7699 static int
7700 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
7701 {
7702 	zfs_handle_t *zhp;
7703 	int ret = 0;
7704 	struct stat64 statbuf;
7705 	struct mnttab entry;
7706 	const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
7707 	ino_t path_inode;
7708 	char *zfs_mntpnt, *entry_mntpnt;
7709 
7710 	if (getextmntent(path, &entry, &statbuf) != 0) {
7711 		if (op == OP_SHARE) {
7712 			(void) fprintf(stderr, gettext("cannot %s '%s': not "
7713 			    "currently mounted\n"), cmdname, path);
7714 			return (1);
7715 		}
7716 		(void) fprintf(stderr, gettext("warning: %s not in"
7717 		    "/proc/self/mounts\n"), path);
7718 		if ((ret = umount2(path, flags)) != 0)
7719 			(void) fprintf(stderr, gettext("%s: %s\n"), path,
7720 			    strerror(errno));
7721 		return (ret != 0);
7722 	}
7723 	path_inode = statbuf.st_ino;
7724 
7725 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
7726 		(void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
7727 		    "filesystem\n"), cmdname, path);
7728 		return (1);
7729 	}
7730 
7731 	if ((zhp = zfs_open(g_zfs, entry.mnt_special,
7732 	    ZFS_TYPE_FILESYSTEM)) == NULL)
7733 		return (1);
7734 
7735 	ret = 1;
7736 	if (stat64(entry.mnt_mountp, &statbuf) != 0) {
7737 		(void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
7738 		    cmdname, path, strerror(errno));
7739 		goto out;
7740 	} else if (statbuf.st_ino != path_inode) {
7741 		(void) fprintf(stderr, gettext("cannot "
7742 		    "%s '%s': not a mountpoint\n"), cmdname, path);
7743 		goto out;
7744 	}
7745 
7746 	/*
7747 	 * If the filesystem is mounted, check that the mountpoint matches
7748 	 * the one in the mnttab entry w.r.t. provided path. If it doesn't,
7749 	 * then we should not proceed further.
7750 	 */
7751 	entry_mntpnt = strdup(entry.mnt_mountp);
7752 	if (zfs_is_mounted(zhp, &zfs_mntpnt)) {
7753 		if (strcmp(zfs_mntpnt, entry_mntpnt) != 0) {
7754 			(void) fprintf(stderr, gettext("cannot %s '%s': "
7755 			    "not an original mountpoint\n"), cmdname, path);
7756 			free(zfs_mntpnt);
7757 			free(entry_mntpnt);
7758 			goto out;
7759 		}
7760 		free(zfs_mntpnt);
7761 	}
7762 	free(entry_mntpnt);
7763 
7764 	if (op == OP_SHARE) {
7765 		char nfs_mnt_prop[ZFS_MAXPROPLEN];
7766 		char smbshare_prop[ZFS_MAXPROPLEN];
7767 
7768 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
7769 		    sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
7770 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
7771 		    sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
7772 
7773 		if (strcmp(nfs_mnt_prop, "off") == 0 &&
7774 		    strcmp(smbshare_prop, "off") == 0) {
7775 			(void) fprintf(stderr, gettext("cannot unshare "
7776 			    "'%s': legacy share\n"), path);
7777 			(void) fprintf(stderr, gettext("use exportfs(8) "
7778 			    "or smbcontrol(1) to unshare this filesystem\n"));
7779 		} else if (!zfs_is_shared(zhp, NULL, NULL)) {
7780 			(void) fprintf(stderr, gettext("cannot unshare '%s': "
7781 			    "not currently shared\n"), path);
7782 		} else {
7783 			ret = zfs_unshare(zhp, path, NULL);
7784 			zfs_commit_shares(NULL);
7785 		}
7786 	} else {
7787 		char mtpt_prop[ZFS_MAXPROPLEN];
7788 
7789 		verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
7790 		    sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
7791 
7792 		if (is_manual) {
7793 			ret = zfs_unmount(zhp, NULL, flags);
7794 		} else if (strcmp(mtpt_prop, "legacy") == 0) {
7795 			(void) fprintf(stderr, gettext("cannot unmount "
7796 			    "'%s': legacy mountpoint\n"),
7797 			    zfs_get_name(zhp));
7798 			(void) fprintf(stderr, gettext("use umount(8) "
7799 			    "to unmount this filesystem\n"));
7800 		} else {
7801 			ret = zfs_unmountall(zhp, flags);
7802 		}
7803 	}
7804 
7805 out:
7806 	zfs_close(zhp);
7807 
7808 	return (ret != 0);
7809 }
7810 
7811 /*
7812  * Generic callback for unsharing or unmounting a filesystem.
7813  */
7814 static int
7815 unshare_unmount(int op, int argc, char **argv)
7816 {
7817 	int do_all = 0;
7818 	int flags = 0;
7819 	int ret = 0;
7820 	int c;
7821 	zfs_handle_t *zhp;
7822 	char nfs_mnt_prop[ZFS_MAXPROPLEN];
7823 	char sharesmb[ZFS_MAXPROPLEN];
7824 
7825 	/* check options */
7826 	while ((c = getopt(argc, argv, op == OP_SHARE ? ":a" : "afu")) != -1) {
7827 		switch (c) {
7828 		case 'a':
7829 			do_all = 1;
7830 			break;
7831 		case 'f':
7832 			flags |= MS_FORCE;
7833 			break;
7834 		case 'u':
7835 			flags |= MS_CRYPT;
7836 			break;
7837 		case ':':
7838 			(void) fprintf(stderr, gettext("missing argument for "
7839 			    "'%c' option\n"), optopt);
7840 			usage(B_FALSE);
7841 			break;
7842 		case '?':
7843 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
7844 			    optopt);
7845 			usage(B_FALSE);
7846 		}
7847 	}
7848 
7849 	argc -= optind;
7850 	argv += optind;
7851 
7852 	if (do_all) {
7853 		/*
7854 		 * We could make use of zfs_for_each() to walk all datasets in
7855 		 * the system, but this would be very inefficient, especially
7856 		 * since we would have to linearly search /proc/self/mounts for
7857 		 * each one. Instead, do one pass through /proc/self/mounts
7858 		 * looking for zfs entries and call zfs_unmount() for each one.
7859 		 *
7860 		 * Things get a little tricky if the administrator has created
7861 		 * mountpoints beneath other ZFS filesystems.  In this case, we
7862 		 * have to unmount the deepest filesystems first.  To accomplish
7863 		 * this, we place all the mountpoints in an AVL tree sorted by
7864 		 * the special type (dataset name), and walk the result in
7865 		 * reverse to make sure to get any snapshots first.
7866 		 */
7867 		FILE *mnttab;
7868 		struct mnttab entry;
7869 		avl_tree_t tree;
7870 		unshare_unmount_node_t *node;
7871 		avl_index_t idx;
7872 		enum sa_protocol *protocol = NULL,
7873 		    single_protocol[] = {SA_NO_PROTOCOL, SA_NO_PROTOCOL};
7874 
7875 		if (op == OP_SHARE && argc > 0) {
7876 			*single_protocol = sa_protocol_decode(argv[0]);
7877 			protocol = single_protocol;
7878 			argc--;
7879 			argv++;
7880 		}
7881 
7882 		if (argc != 0) {
7883 			(void) fprintf(stderr, gettext("too many arguments\n"));
7884 			usage(B_FALSE);
7885 		}
7886 
7887 		avl_create(&tree, unshare_unmount_compare,
7888 		    sizeof (unshare_unmount_node_t),
7889 		    offsetof(unshare_unmount_node_t, un_avlnode));
7890 
7891 		if ((mnttab = fopen(MNTTAB, "re")) == NULL) {
7892 			avl_destroy(&tree);
7893 			return (ENOENT);
7894 		}
7895 
7896 		while (getmntent(mnttab, &entry) == 0) {
7897 
7898 			/* ignore non-ZFS entries */
7899 			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
7900 				continue;
7901 
7902 			/* ignore snapshots */
7903 			if (strchr(entry.mnt_special, '@') != NULL)
7904 				continue;
7905 
7906 			if ((zhp = zfs_open(g_zfs, entry.mnt_special,
7907 			    ZFS_TYPE_FILESYSTEM)) == NULL) {
7908 				ret = 1;
7909 				continue;
7910 			}
7911 
7912 			/*
7913 			 * Ignore datasets that are excluded/restricted by
7914 			 * parent pool name.
7915 			 */
7916 			if (zpool_skip_pool(zfs_get_pool_name(zhp))) {
7917 				zfs_close(zhp);
7918 				continue;
7919 			}
7920 
7921 			switch (op) {
7922 			case OP_SHARE:
7923 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
7924 				    nfs_mnt_prop,
7925 				    sizeof (nfs_mnt_prop),
7926 				    NULL, NULL, 0, B_FALSE) == 0);
7927 				if (strcmp(nfs_mnt_prop, "off") != 0)
7928 					break;
7929 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
7930 				    nfs_mnt_prop,
7931 				    sizeof (nfs_mnt_prop),
7932 				    NULL, NULL, 0, B_FALSE) == 0);
7933 				if (strcmp(nfs_mnt_prop, "off") == 0)
7934 					continue;
7935 				break;
7936 			case OP_MOUNT:
7937 				/* Ignore legacy mounts */
7938 				verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
7939 				    nfs_mnt_prop,
7940 				    sizeof (nfs_mnt_prop),
7941 				    NULL, NULL, 0, B_FALSE) == 0);
7942 				if (strcmp(nfs_mnt_prop, "legacy") == 0)
7943 					continue;
7944 				/* Ignore canmount=noauto mounts */
7945 				if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
7946 				    ZFS_CANMOUNT_NOAUTO)
7947 					continue;
7948 				break;
7949 			default:
7950 				break;
7951 			}
7952 
7953 			node = safe_malloc(sizeof (unshare_unmount_node_t));
7954 			node->un_zhp = zhp;
7955 			node->un_mountp = safe_strdup(entry.mnt_mountp);
7956 
7957 			if (avl_find(&tree, node, &idx) == NULL) {
7958 				avl_insert(&tree, node, idx);
7959 			} else {
7960 				zfs_close(node->un_zhp);
7961 				free(node->un_mountp);
7962 				free(node);
7963 			}
7964 		}
7965 		(void) fclose(mnttab);
7966 
7967 		/*
7968 		 * Walk the AVL tree in reverse, unmounting each filesystem and
7969 		 * removing it from the AVL tree in the process.
7970 		 */
7971 		while ((node = avl_last(&tree)) != NULL) {
7972 			const char *mntarg = NULL;
7973 
7974 			avl_remove(&tree, node);
7975 			switch (op) {
7976 			case OP_SHARE:
7977 				if (zfs_unshare(node->un_zhp,
7978 				    node->un_mountp, protocol) != 0)
7979 					ret = 1;
7980 				break;
7981 
7982 			case OP_MOUNT:
7983 				if (zfs_unmount(node->un_zhp,
7984 				    mntarg, flags) != 0)
7985 					ret = 1;
7986 				break;
7987 			}
7988 
7989 			zfs_close(node->un_zhp);
7990 			free(node->un_mountp);
7991 			free(node);
7992 		}
7993 
7994 		if (op == OP_SHARE)
7995 			zfs_commit_shares(protocol);
7996 
7997 		avl_destroy(&tree);
7998 
7999 	} else {
8000 		if (argc != 1) {
8001 			if (argc == 0)
8002 				(void) fprintf(stderr,
8003 				    gettext("missing filesystem argument\n"));
8004 			else
8005 				(void) fprintf(stderr,
8006 				    gettext("too many arguments\n"));
8007 			usage(B_FALSE);
8008 		}
8009 
8010 		/*
8011 		 * We have an argument, but it may be a full path or a ZFS
8012 		 * filesystem.  Pass full paths off to unmount_path() (shared by
8013 		 * manual_unmount), otherwise open the filesystem and pass to
8014 		 * zfs_unmount().
8015 		 */
8016 		if (argv[0][0] == '/')
8017 			return (unshare_unmount_path(op, argv[0],
8018 			    flags, B_FALSE));
8019 
8020 		if ((zhp = zfs_open(g_zfs, argv[0],
8021 		    ZFS_TYPE_FILESYSTEM)) == NULL)
8022 			return (1);
8023 
8024 		verify(zfs_prop_get(zhp, op == OP_SHARE ?
8025 		    ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
8026 		    nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
8027 		    NULL, 0, B_FALSE) == 0);
8028 
8029 		switch (op) {
8030 		case OP_SHARE:
8031 			verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
8032 			    nfs_mnt_prop,
8033 			    sizeof (nfs_mnt_prop),
8034 			    NULL, NULL, 0, B_FALSE) == 0);
8035 			verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
8036 			    sharesmb, sizeof (sharesmb), NULL, NULL,
8037 			    0, B_FALSE) == 0);
8038 
8039 			if (strcmp(nfs_mnt_prop, "off") == 0 &&
8040 			    strcmp(sharesmb, "off") == 0) {
8041 				(void) fprintf(stderr, gettext("cannot "
8042 				    "unshare '%s': legacy share\n"),
8043 				    zfs_get_name(zhp));
8044 				(void) fprintf(stderr, gettext("use "
8045 				    "exports(5) or smb.conf(5) to unshare "
8046 				    "this filesystem\n"));
8047 				ret = 1;
8048 			} else if (!zfs_is_shared(zhp, NULL, NULL)) {
8049 				(void) fprintf(stderr, gettext("cannot "
8050 				    "unshare '%s': not currently "
8051 				    "shared\n"), zfs_get_name(zhp));
8052 				ret = 1;
8053 			} else if (zfs_unshareall(zhp, NULL) != 0) {
8054 				ret = 1;
8055 			}
8056 			break;
8057 
8058 		case OP_MOUNT:
8059 			if (strcmp(nfs_mnt_prop, "legacy") == 0) {
8060 				(void) fprintf(stderr, gettext("cannot "
8061 				    "unmount '%s': legacy "
8062 				    "mountpoint\n"), zfs_get_name(zhp));
8063 				(void) fprintf(stderr, gettext("use "
8064 				    "umount(8) to unmount this "
8065 				    "filesystem\n"));
8066 				ret = 1;
8067 			} else if (!zfs_is_mounted(zhp, NULL)) {
8068 				(void) fprintf(stderr, gettext("cannot "
8069 				    "unmount '%s': not currently "
8070 				    "mounted\n"),
8071 				    zfs_get_name(zhp));
8072 				ret = 1;
8073 			} else if (zfs_unmountall(zhp, flags) != 0) {
8074 				ret = 1;
8075 			}
8076 			break;
8077 		}
8078 
8079 		zfs_close(zhp);
8080 	}
8081 
8082 	return (ret);
8083 }
8084 
8085 /*
8086  * zfs unmount [-fu] -a
8087  * zfs unmount [-fu] filesystem
8088  *
8089  * Unmount all filesystems, or a specific ZFS filesystem.
8090  */
8091 static int
8092 zfs_do_unmount(int argc, char **argv)
8093 {
8094 	return (unshare_unmount(OP_MOUNT, argc, argv));
8095 }
8096 
8097 /*
8098  * zfs unshare -a
8099  * zfs unshare filesystem
8100  *
8101  * Unshare all filesystems, or a specific ZFS filesystem.
8102  */
8103 static int
8104 zfs_do_unshare(int argc, char **argv)
8105 {
8106 	return (unshare_unmount(OP_SHARE, argc, argv));
8107 }
8108 
8109 static int
8110 find_command_idx(const char *command, int *idx)
8111 {
8112 	int i;
8113 
8114 	for (i = 0; i < NCOMMAND; i++) {
8115 		if (command_table[i].name == NULL)
8116 			continue;
8117 
8118 		if (strcmp(command, command_table[i].name) == 0) {
8119 			*idx = i;
8120 			return (0);
8121 		}
8122 	}
8123 	return (1);
8124 }
8125 
8126 static int
8127 zfs_do_diff(int argc, char **argv)
8128 {
8129 	zfs_handle_t *zhp;
8130 	int flags = 0;
8131 	char *tosnap = NULL;
8132 	char *fromsnap = NULL;
8133 	char *atp, *copy;
8134 	int err = 0;
8135 	int c;
8136 	struct sigaction sa;
8137 
8138 	while ((c = getopt(argc, argv, "FHth")) != -1) {
8139 		switch (c) {
8140 		case 'F':
8141 			flags |= ZFS_DIFF_CLASSIFY;
8142 			break;
8143 		case 'H':
8144 			flags |= ZFS_DIFF_PARSEABLE;
8145 			break;
8146 		case 't':
8147 			flags |= ZFS_DIFF_TIMESTAMP;
8148 			break;
8149 		case 'h':
8150 			flags |= ZFS_DIFF_NO_MANGLE;
8151 			break;
8152 		default:
8153 			(void) fprintf(stderr,
8154 			    gettext("invalid option '%c'\n"), optopt);
8155 			usage(B_FALSE);
8156 		}
8157 	}
8158 
8159 	argc -= optind;
8160 	argv += optind;
8161 
8162 	if (argc < 1) {
8163 		(void) fprintf(stderr,
8164 		    gettext("must provide at least one snapshot name\n"));
8165 		usage(B_FALSE);
8166 	}
8167 
8168 	if (argc > 2) {
8169 		(void) fprintf(stderr, gettext("too many arguments\n"));
8170 		usage(B_FALSE);
8171 	}
8172 
8173 	fromsnap = argv[0];
8174 	tosnap = (argc == 2) ? argv[1] : NULL;
8175 
8176 	copy = NULL;
8177 	if (*fromsnap != '@')
8178 		copy = strdup(fromsnap);
8179 	else if (tosnap)
8180 		copy = strdup(tosnap);
8181 	if (copy == NULL)
8182 		usage(B_FALSE);
8183 
8184 	if ((atp = strchr(copy, '@')) != NULL)
8185 		*atp = '\0';
8186 
8187 	if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL) {
8188 		free(copy);
8189 		return (1);
8190 	}
8191 	free(copy);
8192 
8193 	/*
8194 	 * Ignore SIGPIPE so that the library can give us
8195 	 * information on any failure
8196 	 */
8197 	if (sigemptyset(&sa.sa_mask) == -1) {
8198 		err = errno;
8199 		goto out;
8200 	}
8201 	sa.sa_flags = 0;
8202 	sa.sa_handler = SIG_IGN;
8203 	if (sigaction(SIGPIPE, &sa, NULL) == -1) {
8204 		err = errno;
8205 		goto out;
8206 	}
8207 
8208 	err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
8209 out:
8210 	zfs_close(zhp);
8211 
8212 	return (err != 0);
8213 }
8214 
8215 /*
8216  * zfs bookmark <fs@source>|<fs#source> <fs#bookmark>
8217  *
8218  * Creates a bookmark with the given name from the source snapshot
8219  * or creates a copy of an existing source bookmark.
8220  */
8221 static int
8222 zfs_do_bookmark(int argc, char **argv)
8223 {
8224 	char *source, *bookname;
8225 	char expbuf[ZFS_MAX_DATASET_NAME_LEN];
8226 	int source_type;
8227 	nvlist_t *nvl;
8228 	int ret = 0;
8229 	int c;
8230 
8231 	/* check options */
8232 	while ((c = getopt(argc, argv, "")) != -1) {
8233 		switch (c) {
8234 		case '?':
8235 			(void) fprintf(stderr,
8236 			    gettext("invalid option '%c'\n"), optopt);
8237 			goto usage;
8238 		}
8239 	}
8240 
8241 	argc -= optind;
8242 	argv += optind;
8243 
8244 	/* check number of arguments */
8245 	if (argc < 1) {
8246 		(void) fprintf(stderr, gettext("missing source argument\n"));
8247 		goto usage;
8248 	}
8249 	if (argc < 2) {
8250 		(void) fprintf(stderr, gettext("missing bookmark argument\n"));
8251 		goto usage;
8252 	}
8253 
8254 	source = argv[0];
8255 	bookname = argv[1];
8256 
8257 	if (strchr(source, '@') == NULL && strchr(source, '#') == NULL) {
8258 		(void) fprintf(stderr,
8259 		    gettext("invalid source name '%s': "
8260 		    "must contain a '@' or '#'\n"), source);
8261 		goto usage;
8262 	}
8263 	if (strchr(bookname, '#') == NULL) {
8264 		(void) fprintf(stderr,
8265 		    gettext("invalid bookmark name '%s': "
8266 		    "must contain a '#'\n"), bookname);
8267 		goto usage;
8268 	}
8269 
8270 	/*
8271 	 * expand source or bookname to full path:
8272 	 * one of them may be specified as short name
8273 	 */
8274 	{
8275 		char **expand;
8276 		char *source_short, *bookname_short;
8277 		source_short = strpbrk(source, "@#");
8278 		bookname_short = strpbrk(bookname, "#");
8279 		if (source_short == source &&
8280 		    bookname_short == bookname) {
8281 			(void) fprintf(stderr, gettext(
8282 			    "either source or bookmark must be specified as "
8283 			    "full dataset paths"));
8284 			goto usage;
8285 		} else if (source_short != source &&
8286 		    bookname_short != bookname) {
8287 			expand = NULL;
8288 		} else if (source_short != source) {
8289 			strlcpy(expbuf, source, sizeof (expbuf));
8290 			expand = &bookname;
8291 		} else if (bookname_short != bookname) {
8292 			strlcpy(expbuf, bookname, sizeof (expbuf));
8293 			expand = &source;
8294 		} else {
8295 			abort();
8296 		}
8297 		if (expand != NULL) {
8298 			*strpbrk(expbuf, "@#") = '\0'; /* dataset name in buf */
8299 			(void) strlcat(expbuf, *expand, sizeof (expbuf));
8300 			*expand = expbuf;
8301 		}
8302 	}
8303 
8304 	/* determine source type */
8305 	switch (*strpbrk(source, "@#")) {
8306 		case '@': source_type = ZFS_TYPE_SNAPSHOT; break;
8307 		case '#': source_type = ZFS_TYPE_BOOKMARK; break;
8308 		default: abort();
8309 	}
8310 
8311 	/* test the source exists */
8312 	zfs_handle_t *zhp;
8313 	zhp = zfs_open(g_zfs, source, source_type);
8314 	if (zhp == NULL)
8315 		goto usage;
8316 	zfs_close(zhp);
8317 
8318 	nvl = fnvlist_alloc();
8319 	fnvlist_add_string(nvl, bookname, source);
8320 	ret = lzc_bookmark(nvl, NULL);
8321 	fnvlist_free(nvl);
8322 
8323 	if (ret != 0) {
8324 		const char *err_msg = NULL;
8325 		char errbuf[1024];
8326 
8327 		(void) snprintf(errbuf, sizeof (errbuf),
8328 		    dgettext(TEXT_DOMAIN,
8329 		    "cannot create bookmark '%s'"), bookname);
8330 
8331 		switch (ret) {
8332 		case EXDEV:
8333 			err_msg = "bookmark is in a different pool";
8334 			break;
8335 		case ZFS_ERR_BOOKMARK_SOURCE_NOT_ANCESTOR:
8336 			err_msg = "source is not an ancestor of the "
8337 			    "new bookmark's dataset";
8338 			break;
8339 		case EEXIST:
8340 			err_msg = "bookmark exists";
8341 			break;
8342 		case EINVAL:
8343 			err_msg = "invalid argument";
8344 			break;
8345 		case ENOTSUP:
8346 			err_msg = "bookmark feature not enabled";
8347 			break;
8348 		case ENOSPC:
8349 			err_msg = "out of space";
8350 			break;
8351 		case ENOENT:
8352 			err_msg = "dataset does not exist";
8353 			break;
8354 		default:
8355 			(void) zfs_standard_error(g_zfs, ret, errbuf);
8356 			break;
8357 		}
8358 		if (err_msg != NULL) {
8359 			(void) fprintf(stderr, "%s: %s\n", errbuf,
8360 			    dgettext(TEXT_DOMAIN, err_msg));
8361 		}
8362 	}
8363 
8364 	return (ret != 0);
8365 
8366 usage:
8367 	usage(B_FALSE);
8368 	return (-1);
8369 }
8370 
8371 static int
8372 zfs_do_channel_program(int argc, char **argv)
8373 {
8374 	int ret, fd, c;
8375 	size_t progsize, progread;
8376 	nvlist_t *outnvl = NULL;
8377 	uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT;
8378 	uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT;
8379 	boolean_t sync_flag = B_TRUE, json_output = B_FALSE;
8380 	zpool_handle_t *zhp;
8381 
8382 	struct option long_options[] = {
8383 		{"json", no_argument, NULL, 'j'},
8384 		{0, 0, 0, 0}
8385 	};
8386 
8387 	/* check options */
8388 	while ((c = getopt_long(argc, argv, "nt:m:j", long_options,
8389 	    NULL)) != -1) {
8390 		switch (c) {
8391 		case 't':
8392 		case 'm': {
8393 			uint64_t arg;
8394 			char *endp;
8395 
8396 			errno = 0;
8397 			arg = strtoull(optarg, &endp, 0);
8398 			if (errno != 0 || *endp != '\0') {
8399 				(void) fprintf(stderr, gettext(
8400 				    "invalid argument "
8401 				    "'%s': expected integer\n"), optarg);
8402 				goto usage;
8403 			}
8404 
8405 			if (c == 't') {
8406 				instrlimit = arg;
8407 			} else {
8408 				ASSERT3U(c, ==, 'm');
8409 				memlimit = arg;
8410 			}
8411 			break;
8412 		}
8413 		case 'n': {
8414 			sync_flag = B_FALSE;
8415 			break;
8416 		}
8417 		case 'j': {
8418 			json_output = B_TRUE;
8419 			break;
8420 		}
8421 		case '?':
8422 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
8423 			    optopt);
8424 			goto usage;
8425 		}
8426 	}
8427 
8428 	argc -= optind;
8429 	argv += optind;
8430 
8431 	if (argc < 2) {
8432 		(void) fprintf(stderr,
8433 		    gettext("invalid number of arguments\n"));
8434 		goto usage;
8435 	}
8436 
8437 	const char *poolname = argv[0];
8438 	const char *filename = argv[1];
8439 	if (strcmp(filename, "-") == 0) {
8440 		fd = 0;
8441 		filename = "standard input";
8442 	} else if ((fd = open(filename, O_RDONLY)) < 0) {
8443 		(void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
8444 		    filename, strerror(errno));
8445 		return (1);
8446 	}
8447 
8448 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
8449 		(void) fprintf(stderr, gettext("cannot open pool '%s'\n"),
8450 		    poolname);
8451 		if (fd != 0)
8452 			(void) close(fd);
8453 		return (1);
8454 	}
8455 	zpool_close(zhp);
8456 
8457 	/*
8458 	 * Read in the channel program, expanding the program buffer as
8459 	 * necessary.
8460 	 */
8461 	progread = 0;
8462 	progsize = 1024;
8463 	char *progbuf = safe_malloc(progsize);
8464 	do {
8465 		ret = read(fd, progbuf + progread, progsize - progread);
8466 		progread += ret;
8467 		if (progread == progsize && ret > 0) {
8468 			progsize *= 2;
8469 			progbuf = safe_realloc(progbuf, progsize);
8470 		}
8471 	} while (ret > 0);
8472 
8473 	if (fd != 0)
8474 		(void) close(fd);
8475 	if (ret < 0) {
8476 		free(progbuf);
8477 		(void) fprintf(stderr,
8478 		    gettext("cannot read '%s': %s\n"),
8479 		    filename, strerror(errno));
8480 		return (1);
8481 	}
8482 	progbuf[progread] = '\0';
8483 
8484 	/*
8485 	 * Any remaining arguments are passed as arguments to the lua script as
8486 	 * a string array:
8487 	 * {
8488 	 *	"argv" -> [ "arg 1", ... "arg n" ],
8489 	 * }
8490 	 */
8491 	nvlist_t *argnvl = fnvlist_alloc();
8492 	fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV,
8493 	    (const char **)argv + 2, argc - 2);
8494 
8495 	if (sync_flag) {
8496 		ret = lzc_channel_program(poolname, progbuf,
8497 		    instrlimit, memlimit, argnvl, &outnvl);
8498 	} else {
8499 		ret = lzc_channel_program_nosync(poolname, progbuf,
8500 		    instrlimit, memlimit, argnvl, &outnvl);
8501 	}
8502 
8503 	if (ret != 0) {
8504 		/*
8505 		 * On error, report the error message handed back by lua if one
8506 		 * exists.  Otherwise, generate an appropriate error message,
8507 		 * falling back on strerror() for an unexpected return code.
8508 		 */
8509 		const char *errstring = NULL;
8510 		const char *msg = gettext("Channel program execution failed");
8511 		uint64_t instructions = 0;
8512 		if (outnvl != NULL && nvlist_exists(outnvl, ZCP_RET_ERROR)) {
8513 			const char *es = NULL;
8514 			(void) nvlist_lookup_string(outnvl,
8515 			    ZCP_RET_ERROR, &es);
8516 			if (es == NULL)
8517 				errstring = strerror(ret);
8518 			else
8519 				errstring = es;
8520 			if (ret == ETIME) {
8521 				(void) nvlist_lookup_uint64(outnvl,
8522 				    ZCP_ARG_INSTRLIMIT, &instructions);
8523 			}
8524 		} else {
8525 			switch (ret) {
8526 			case EINVAL:
8527 				errstring =
8528 				    "Invalid instruction or memory limit.";
8529 				break;
8530 			case ENOMEM:
8531 				errstring = "Return value too large.";
8532 				break;
8533 			case ENOSPC:
8534 				errstring = "Memory limit exhausted.";
8535 				break;
8536 			case ETIME:
8537 				errstring = "Timed out.";
8538 				break;
8539 			case EPERM:
8540 				errstring = "Permission denied. Channel "
8541 				    "programs must be run as root.";
8542 				break;
8543 			default:
8544 				(void) zfs_standard_error(g_zfs, ret, msg);
8545 			}
8546 		}
8547 		if (errstring != NULL)
8548 			(void) fprintf(stderr, "%s:\n%s\n", msg, errstring);
8549 
8550 		if (ret == ETIME && instructions != 0)
8551 			(void) fprintf(stderr,
8552 			    gettext("%llu Lua instructions\n"),
8553 			    (u_longlong_t)instructions);
8554 	} else {
8555 		if (json_output) {
8556 			(void) nvlist_print_json(stdout, outnvl);
8557 		} else if (nvlist_empty(outnvl)) {
8558 			(void) fprintf(stdout, gettext("Channel program fully "
8559 			    "executed and did not produce output.\n"));
8560 		} else {
8561 			(void) fprintf(stdout, gettext("Channel program fully "
8562 			    "executed and produced output:\n"));
8563 			dump_nvlist(outnvl, 4);
8564 		}
8565 	}
8566 
8567 	free(progbuf);
8568 	fnvlist_free(outnvl);
8569 	fnvlist_free(argnvl);
8570 	return (ret != 0);
8571 
8572 usage:
8573 	usage(B_FALSE);
8574 	return (-1);
8575 }
8576 
8577 
8578 typedef struct loadkey_cbdata {
8579 	boolean_t cb_loadkey;
8580 	boolean_t cb_recursive;
8581 	boolean_t cb_noop;
8582 	char *cb_keylocation;
8583 	uint64_t cb_numfailed;
8584 	uint64_t cb_numattempted;
8585 } loadkey_cbdata_t;
8586 
8587 static int
8588 load_key_callback(zfs_handle_t *zhp, void *data)
8589 {
8590 	int ret;
8591 	boolean_t is_encroot;
8592 	loadkey_cbdata_t *cb = data;
8593 	uint64_t keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
8594 
8595 	/*
8596 	 * If we are working recursively, we want to skip loading / unloading
8597 	 * keys for non-encryption roots and datasets whose keys are already
8598 	 * in the desired end-state.
8599 	 */
8600 	if (cb->cb_recursive) {
8601 		ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
8602 		if (ret != 0)
8603 			return (ret);
8604 		if (!is_encroot)
8605 			return (0);
8606 
8607 		if ((cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_AVAILABLE) ||
8608 		    (!cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_UNAVAILABLE))
8609 			return (0);
8610 	}
8611 
8612 	cb->cb_numattempted++;
8613 
8614 	if (cb->cb_loadkey)
8615 		ret = zfs_crypto_load_key(zhp, cb->cb_noop, cb->cb_keylocation);
8616 	else
8617 		ret = zfs_crypto_unload_key(zhp);
8618 
8619 	if (ret != 0) {
8620 		cb->cb_numfailed++;
8621 		return (ret);
8622 	}
8623 
8624 	return (0);
8625 }
8626 
8627 static int
8628 load_unload_keys(int argc, char **argv, boolean_t loadkey)
8629 {
8630 	int c, ret = 0, flags = 0;
8631 	boolean_t do_all = B_FALSE;
8632 	loadkey_cbdata_t cb = { 0 };
8633 
8634 	cb.cb_loadkey = loadkey;
8635 
8636 	while ((c = getopt(argc, argv, "anrL:")) != -1) {
8637 		/* noop and alternate keylocations only apply to zfs load-key */
8638 		if (loadkey) {
8639 			switch (c) {
8640 			case 'n':
8641 				cb.cb_noop = B_TRUE;
8642 				continue;
8643 			case 'L':
8644 				cb.cb_keylocation = optarg;
8645 				continue;
8646 			default:
8647 				break;
8648 			}
8649 		}
8650 
8651 		switch (c) {
8652 		case 'a':
8653 			do_all = B_TRUE;
8654 			cb.cb_recursive = B_TRUE;
8655 			break;
8656 		case 'r':
8657 			flags |= ZFS_ITER_RECURSE;
8658 			cb.cb_recursive = B_TRUE;
8659 			break;
8660 		default:
8661 			(void) fprintf(stderr,
8662 			    gettext("invalid option '%c'\n"), optopt);
8663 			usage(B_FALSE);
8664 		}
8665 	}
8666 
8667 	argc -= optind;
8668 	argv += optind;
8669 
8670 	if (!do_all && argc == 0) {
8671 		(void) fprintf(stderr,
8672 		    gettext("Missing dataset argument or -a option\n"));
8673 		usage(B_FALSE);
8674 	}
8675 
8676 	if (do_all && argc != 0) {
8677 		(void) fprintf(stderr,
8678 		    gettext("Cannot specify dataset with -a option\n"));
8679 		usage(B_FALSE);
8680 	}
8681 
8682 	if (cb.cb_recursive && cb.cb_keylocation != NULL &&
8683 	    strcmp(cb.cb_keylocation, "prompt") != 0) {
8684 		(void) fprintf(stderr, gettext("alternate keylocation may only "
8685 		    "be 'prompt' with -r or -a\n"));
8686 		usage(B_FALSE);
8687 	}
8688 
8689 	ret = zfs_for_each(argc, argv, flags,
8690 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, NULL, NULL, 0,
8691 	    load_key_callback, &cb);
8692 
8693 	if (cb.cb_noop || (cb.cb_recursive && cb.cb_numattempted != 0)) {
8694 		(void) printf(gettext("%llu / %llu key(s) successfully %s\n"),
8695 		    (u_longlong_t)(cb.cb_numattempted - cb.cb_numfailed),
8696 		    (u_longlong_t)cb.cb_numattempted,
8697 		    loadkey ? (cb.cb_noop ? "verified" : "loaded") :
8698 		    "unloaded");
8699 	}
8700 
8701 	if (cb.cb_numfailed != 0)
8702 		ret = -1;
8703 
8704 	return (ret);
8705 }
8706 
8707 static int
8708 zfs_do_load_key(int argc, char **argv)
8709 {
8710 	return (load_unload_keys(argc, argv, B_TRUE));
8711 }
8712 
8713 
8714 static int
8715 zfs_do_unload_key(int argc, char **argv)
8716 {
8717 	return (load_unload_keys(argc, argv, B_FALSE));
8718 }
8719 
8720 static int
8721 zfs_do_change_key(int argc, char **argv)
8722 {
8723 	int c, ret;
8724 	uint64_t keystatus;
8725 	boolean_t loadkey = B_FALSE, inheritkey = B_FALSE;
8726 	zfs_handle_t *zhp = NULL;
8727 	nvlist_t *props = fnvlist_alloc();
8728 
8729 	while ((c = getopt(argc, argv, "lio:")) != -1) {
8730 		switch (c) {
8731 		case 'l':
8732 			loadkey = B_TRUE;
8733 			break;
8734 		case 'i':
8735 			inheritkey = B_TRUE;
8736 			break;
8737 		case 'o':
8738 			if (!parseprop(props, optarg)) {
8739 				nvlist_free(props);
8740 				return (1);
8741 			}
8742 			break;
8743 		default:
8744 			(void) fprintf(stderr,
8745 			    gettext("invalid option '%c'\n"), optopt);
8746 			usage(B_FALSE);
8747 		}
8748 	}
8749 
8750 	argc -= optind;
8751 	argv += optind;
8752 
8753 	if (argc < 1) {
8754 		(void) fprintf(stderr, gettext("Missing dataset argument\n"));
8755 		usage(B_FALSE);
8756 	}
8757 
8758 	if (argc > 1) {
8759 		(void) fprintf(stderr, gettext("Too many arguments\n"));
8760 		usage(B_FALSE);
8761 	}
8762 
8763 	zhp = zfs_open(g_zfs, argv[argc - 1],
8764 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
8765 	if (zhp == NULL)
8766 		usage(B_FALSE);
8767 
8768 	if (loadkey) {
8769 		keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
8770 		if (keystatus != ZFS_KEYSTATUS_AVAILABLE) {
8771 			ret = zfs_crypto_load_key(zhp, B_FALSE, NULL);
8772 			if (ret != 0) {
8773 				nvlist_free(props);
8774 				zfs_close(zhp);
8775 				return (-1);
8776 			}
8777 		}
8778 
8779 		/* refresh the properties so the new keystatus is visible */
8780 		zfs_refresh_properties(zhp);
8781 	}
8782 
8783 	ret = zfs_crypto_rewrap(zhp, props, inheritkey);
8784 	if (ret != 0) {
8785 		nvlist_free(props);
8786 		zfs_close(zhp);
8787 		return (-1);
8788 	}
8789 
8790 	nvlist_free(props);
8791 	zfs_close(zhp);
8792 	return (0);
8793 }
8794 
8795 /*
8796  * 1) zfs project [-d|-r] <file|directory ...>
8797  *    List project ID and inherit flag of file(s) or directories.
8798  *    -d: List the directory itself, not its children.
8799  *    -r: List subdirectories recursively.
8800  *
8801  * 2) zfs project -C [-k] [-r] <file|directory ...>
8802  *    Clear project inherit flag and/or ID on the file(s) or directories.
8803  *    -k: Keep the project ID unchanged. If not specified, the project ID
8804  *	  will be reset as zero.
8805  *    -r: Clear on subdirectories recursively.
8806  *
8807  * 3) zfs project -c [-0] [-d|-r] [-p id] <file|directory ...>
8808  *    Check project ID and inherit flag on the file(s) or directories,
8809  *    report the outliers.
8810  *    -0: Print file name followed by a NUL instead of newline.
8811  *    -d: Check the directory itself, not its children.
8812  *    -p: Specify the referenced ID for comparing with the target file(s)
8813  *	  or directories' project IDs. If not specified, the target (top)
8814  *	  directory's project ID will be used as the referenced one.
8815  *    -r: Check subdirectories recursively.
8816  *
8817  * 4) zfs project [-p id] [-r] [-s] <file|directory ...>
8818  *    Set project ID and/or inherit flag on the file(s) or directories.
8819  *    -p: Set the project ID as the given id.
8820  *    -r: Set on subdirectories recursively. If not specify "-p" option,
8821  *	  it will use top-level directory's project ID as the given id,
8822  *	  then set both project ID and inherit flag on all descendants
8823  *	  of the top-level directory.
8824  *    -s: Set project inherit flag.
8825  */
8826 static int
8827 zfs_do_project(int argc, char **argv)
8828 {
8829 	zfs_project_control_t zpc = {
8830 		.zpc_expected_projid = ZFS_INVALID_PROJID,
8831 		.zpc_op = ZFS_PROJECT_OP_DEFAULT,
8832 		.zpc_dironly = B_FALSE,
8833 		.zpc_keep_projid = B_FALSE,
8834 		.zpc_newline = B_TRUE,
8835 		.zpc_recursive = B_FALSE,
8836 		.zpc_set_flag = B_FALSE,
8837 	};
8838 	int ret = 0, c;
8839 
8840 	if (argc < 2)
8841 		usage(B_FALSE);
8842 
8843 	while ((c = getopt(argc, argv, "0Ccdkp:rs")) != -1) {
8844 		switch (c) {
8845 		case '0':
8846 			zpc.zpc_newline = B_FALSE;
8847 			break;
8848 		case 'C':
8849 			if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
8850 				(void) fprintf(stderr, gettext("cannot "
8851 				    "specify '-C' '-c' '-s' together\n"));
8852 				usage(B_FALSE);
8853 			}
8854 
8855 			zpc.zpc_op = ZFS_PROJECT_OP_CLEAR;
8856 			break;
8857 		case 'c':
8858 			if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
8859 				(void) fprintf(stderr, gettext("cannot "
8860 				    "specify '-C' '-c' '-s' together\n"));
8861 				usage(B_FALSE);
8862 			}
8863 
8864 			zpc.zpc_op = ZFS_PROJECT_OP_CHECK;
8865 			break;
8866 		case 'd':
8867 			zpc.zpc_dironly = B_TRUE;
8868 			/* overwrite "-r" option */
8869 			zpc.zpc_recursive = B_FALSE;
8870 			break;
8871 		case 'k':
8872 			zpc.zpc_keep_projid = B_TRUE;
8873 			break;
8874 		case 'p': {
8875 			char *endptr;
8876 
8877 			errno = 0;
8878 			zpc.zpc_expected_projid = strtoull(optarg, &endptr, 0);
8879 			if (errno != 0 || *endptr != '\0') {
8880 				(void) fprintf(stderr,
8881 				    gettext("project ID must be less than "
8882 				    "%u\n"), UINT32_MAX);
8883 				usage(B_FALSE);
8884 			}
8885 			if (zpc.zpc_expected_projid >= UINT32_MAX) {
8886 				(void) fprintf(stderr,
8887 				    gettext("invalid project ID\n"));
8888 				usage(B_FALSE);
8889 			}
8890 			break;
8891 		}
8892 		case 'r':
8893 			zpc.zpc_recursive = B_TRUE;
8894 			/* overwrite "-d" option */
8895 			zpc.zpc_dironly = B_FALSE;
8896 			break;
8897 		case 's':
8898 			if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
8899 				(void) fprintf(stderr, gettext("cannot "
8900 				    "specify '-C' '-c' '-s' together\n"));
8901 				usage(B_FALSE);
8902 			}
8903 
8904 			zpc.zpc_set_flag = B_TRUE;
8905 			zpc.zpc_op = ZFS_PROJECT_OP_SET;
8906 			break;
8907 		default:
8908 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
8909 			    optopt);
8910 			usage(B_FALSE);
8911 		}
8912 	}
8913 
8914 	if (zpc.zpc_op == ZFS_PROJECT_OP_DEFAULT) {
8915 		if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID)
8916 			zpc.zpc_op = ZFS_PROJECT_OP_SET;
8917 		else
8918 			zpc.zpc_op = ZFS_PROJECT_OP_LIST;
8919 	}
8920 
8921 	switch (zpc.zpc_op) {
8922 	case ZFS_PROJECT_OP_LIST:
8923 		if (zpc.zpc_keep_projid) {
8924 			(void) fprintf(stderr,
8925 			    gettext("'-k' is only valid together with '-C'\n"));
8926 			usage(B_FALSE);
8927 		}
8928 		if (!zpc.zpc_newline) {
8929 			(void) fprintf(stderr,
8930 			    gettext("'-0' is only valid together with '-c'\n"));
8931 			usage(B_FALSE);
8932 		}
8933 		break;
8934 	case ZFS_PROJECT_OP_CHECK:
8935 		if (zpc.zpc_keep_projid) {
8936 			(void) fprintf(stderr,
8937 			    gettext("'-k' is only valid together with '-C'\n"));
8938 			usage(B_FALSE);
8939 		}
8940 		break;
8941 	case ZFS_PROJECT_OP_CLEAR:
8942 		if (zpc.zpc_dironly) {
8943 			(void) fprintf(stderr,
8944 			    gettext("'-d' is useless together with '-C'\n"));
8945 			usage(B_FALSE);
8946 		}
8947 		if (!zpc.zpc_newline) {
8948 			(void) fprintf(stderr,
8949 			    gettext("'-0' is only valid together with '-c'\n"));
8950 			usage(B_FALSE);
8951 		}
8952 		if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID) {
8953 			(void) fprintf(stderr,
8954 			    gettext("'-p' is useless together with '-C'\n"));
8955 			usage(B_FALSE);
8956 		}
8957 		break;
8958 	case ZFS_PROJECT_OP_SET:
8959 		if (zpc.zpc_dironly) {
8960 			(void) fprintf(stderr,
8961 			    gettext("'-d' is useless for set project ID and/or "
8962 			    "inherit flag\n"));
8963 			usage(B_FALSE);
8964 		}
8965 		if (zpc.zpc_keep_projid) {
8966 			(void) fprintf(stderr,
8967 			    gettext("'-k' is only valid together with '-C'\n"));
8968 			usage(B_FALSE);
8969 		}
8970 		if (!zpc.zpc_newline) {
8971 			(void) fprintf(stderr,
8972 			    gettext("'-0' is only valid together with '-c'\n"));
8973 			usage(B_FALSE);
8974 		}
8975 		break;
8976 	default:
8977 		ASSERT(0);
8978 		break;
8979 	}
8980 
8981 	argv += optind;
8982 	argc -= optind;
8983 	if (argc == 0) {
8984 		(void) fprintf(stderr,
8985 		    gettext("missing file or directory target(s)\n"));
8986 		usage(B_FALSE);
8987 	}
8988 
8989 	for (int i = 0; i < argc; i++) {
8990 		int err;
8991 
8992 		err = zfs_project_handle(argv[i], &zpc);
8993 		if (err && !ret)
8994 			ret = err;
8995 	}
8996 
8997 	return (ret);
8998 }
8999 
9000 static int
9001 zfs_rewrite_file(const char *path, boolean_t verbose, zfs_rewrite_args_t *args)
9002 {
9003 	int fd, ret = 0;
9004 
9005 	fd = open(path, O_WRONLY);
9006 	if (fd < 0) {
9007 		ret = errno;
9008 		(void) fprintf(stderr, gettext("failed to open %s: %s\n"),
9009 		    path, strerror(errno));
9010 		return (ret);
9011 	}
9012 
9013 	if (ioctl(fd, ZFS_IOC_REWRITE, args) < 0) {
9014 		ret = errno;
9015 		(void) fprintf(stderr, gettext("failed to rewrite %s: %s\n"),
9016 		    path, strerror(errno));
9017 	} else if (verbose) {
9018 		printf("%s\n", path);
9019 	}
9020 
9021 	close(fd);
9022 	return (ret);
9023 }
9024 
9025 static int
9026 zfs_rewrite_dir(const char *path, boolean_t verbose, boolean_t xdev, dev_t dev,
9027     zfs_rewrite_args_t *args, nvlist_t *dirs)
9028 {
9029 	struct dirent *ent;
9030 	DIR *dir;
9031 	int ret = 0, err;
9032 
9033 	dir = opendir(path);
9034 	if (dir == NULL) {
9035 		if (errno == ENOENT)
9036 			return (0);
9037 		ret = errno;
9038 		(void) fprintf(stderr, gettext("failed to opendir %s: %s\n"),
9039 		    path, strerror(errno));
9040 		return (ret);
9041 	}
9042 
9043 	size_t plen = strlen(path) + 1;
9044 	while ((ent = readdir(dir)) != NULL) {
9045 		char *fullname;
9046 		struct stat st;
9047 
9048 		if (ent->d_type != DT_REG && ent->d_type != DT_DIR)
9049 			continue;
9050 
9051 		if (strcmp(ent->d_name, ".") == 0 ||
9052 		    strcmp(ent->d_name, "..") == 0)
9053 			continue;
9054 
9055 		if (plen + strlen(ent->d_name) >= PATH_MAX) {
9056 			(void) fprintf(stderr, gettext("path too long %s/%s\n"),
9057 			    path, ent->d_name);
9058 			ret = ENAMETOOLONG;
9059 			continue;
9060 		}
9061 
9062 		if (asprintf(&fullname, "%s/%s", path, ent->d_name) == -1) {
9063 			(void) fprintf(stderr,
9064 			    gettext("failed to allocate memory\n"));
9065 			ret = ENOMEM;
9066 			continue;
9067 		}
9068 
9069 		if (xdev) {
9070 			if (lstat(fullname, &st) < 0) {
9071 				ret = errno;
9072 				(void) fprintf(stderr,
9073 				    gettext("failed to stat %s: %s\n"),
9074 				    fullname, strerror(errno));
9075 				free(fullname);
9076 				continue;
9077 			}
9078 			if (st.st_dev != dev) {
9079 				free(fullname);
9080 				continue;
9081 			}
9082 		}
9083 
9084 		if (ent->d_type == DT_REG) {
9085 			err = zfs_rewrite_file(fullname, verbose, args);
9086 			if (err)
9087 				ret = err;
9088 		} else { /* DT_DIR */
9089 			fnvlist_add_uint64(dirs, fullname, dev);
9090 		}
9091 
9092 		free(fullname);
9093 	}
9094 
9095 	closedir(dir);
9096 	return (ret);
9097 }
9098 
9099 static int
9100 zfs_rewrite_path(const char *path, boolean_t verbose, boolean_t recurse,
9101     boolean_t xdev, zfs_rewrite_args_t *args, nvlist_t *dirs)
9102 {
9103 	struct stat st;
9104 	int ret = 0;
9105 
9106 	if (lstat(path, &st) < 0) {
9107 		ret = errno;
9108 		(void) fprintf(stderr, gettext("failed to stat %s: %s\n"),
9109 		    path, strerror(errno));
9110 		return (ret);
9111 	}
9112 
9113 	if (S_ISREG(st.st_mode)) {
9114 		ret = zfs_rewrite_file(path, verbose, args);
9115 	} else if (S_ISDIR(st.st_mode) && recurse) {
9116 		ret = zfs_rewrite_dir(path, verbose, xdev, st.st_dev, args,
9117 		    dirs);
9118 	}
9119 	return (ret);
9120 }
9121 
9122 static int
9123 zfs_do_rewrite(int argc, char **argv)
9124 {
9125 	int ret = 0, err, c;
9126 	boolean_t recurse = B_FALSE, verbose = B_FALSE, xdev = B_FALSE;
9127 
9128 	if (argc < 2)
9129 		usage(B_FALSE);
9130 
9131 	zfs_rewrite_args_t args;
9132 	memset(&args, 0, sizeof (args));
9133 
9134 	while ((c = getopt(argc, argv, "CPSl:o:rvx")) != -1) {
9135 		switch (c) {
9136 		case 'C':
9137 			args.flags |= ZFS_REWRITE_SKIP_BRT;
9138 			break;
9139 		case 'P':
9140 			args.flags |= ZFS_REWRITE_PHYSICAL;
9141 			break;
9142 		case 'S':
9143 			args.flags |= ZFS_REWRITE_SKIP_SNAPSHOT;
9144 			break;
9145 		case 'l':
9146 			args.len = strtoll(optarg, NULL, 0);
9147 			break;
9148 		case 'o':
9149 			args.off = strtoll(optarg, NULL, 0);
9150 			break;
9151 		case 'r':
9152 			recurse = B_TRUE;
9153 			break;
9154 		case 'v':
9155 			verbose = B_TRUE;
9156 			break;
9157 		case 'x':
9158 			xdev = B_TRUE;
9159 			break;
9160 		default:
9161 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
9162 			    optopt);
9163 			usage(B_FALSE);
9164 		}
9165 	}
9166 
9167 	argv += optind;
9168 	argc -= optind;
9169 	if (argc == 0) {
9170 		(void) fprintf(stderr,
9171 		    gettext("missing file or directory target(s)\n"));
9172 		usage(B_FALSE);
9173 	}
9174 
9175 	nvlist_t *dirs = fnvlist_alloc();
9176 	for (int i = 0; i < argc; i++) {
9177 		err = zfs_rewrite_path(argv[i], verbose, recurse, xdev, &args,
9178 		    dirs);
9179 		if (err)
9180 			ret = err;
9181 	}
9182 	nvpair_t *dir;
9183 	while ((dir = nvlist_next_nvpair(dirs, NULL)) != NULL) {
9184 		err = zfs_rewrite_dir(nvpair_name(dir), verbose, xdev,
9185 		    fnvpair_value_uint64(dir), &args, dirs);
9186 		if (err)
9187 			ret = err;
9188 		fnvlist_remove_nvpair(dirs, dir);
9189 	}
9190 	fnvlist_free(dirs);
9191 
9192 	return (ret);
9193 }
9194 
9195 static int
9196 zfs_do_wait(int argc, char **argv)
9197 {
9198 	boolean_t enabled[ZFS_WAIT_NUM_ACTIVITIES];
9199 	int error = 0, i;
9200 	int c;
9201 
9202 	/* By default, wait for all types of activity. */
9203 	for (i = 0; i < ZFS_WAIT_NUM_ACTIVITIES; i++)
9204 		enabled[i] = B_TRUE;
9205 
9206 	while ((c = getopt(argc, argv, "t:")) != -1) {
9207 		switch (c) {
9208 		case 't':
9209 			/* Reset activities array */
9210 			memset(&enabled, 0, sizeof (enabled));
9211 
9212 			for (char *tok; (tok = strsep(&optarg, ",")); ) {
9213 				static const char *const col_subopts[
9214 				    ZFS_WAIT_NUM_ACTIVITIES] = { "deleteq" };
9215 
9216 				for (i = 0; i < ARRAY_SIZE(col_subopts); ++i)
9217 					if (strcmp(tok, col_subopts[i]) == 0) {
9218 						enabled[i] = B_TRUE;
9219 						goto found;
9220 					}
9221 
9222 				(void) fprintf(stderr,
9223 				    gettext("invalid activity '%s'\n"), tok);
9224 				usage(B_FALSE);
9225 found:;
9226 			}
9227 			break;
9228 		case '?':
9229 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
9230 			    optopt);
9231 			usage(B_FALSE);
9232 		}
9233 	}
9234 
9235 	argv += optind;
9236 	argc -= optind;
9237 	if (argc < 1) {
9238 		(void) fprintf(stderr, gettext("missing 'filesystem' "
9239 		    "argument\n"));
9240 		usage(B_FALSE);
9241 	}
9242 	if (argc > 1) {
9243 		(void) fprintf(stderr, gettext("too many arguments\n"));
9244 		usage(B_FALSE);
9245 	}
9246 
9247 	zfs_handle_t *zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM);
9248 	if (zhp == NULL)
9249 		return (1);
9250 
9251 	for (;;) {
9252 		boolean_t missing = B_FALSE;
9253 		boolean_t any_waited = B_FALSE;
9254 
9255 		for (int i = 0; i < ZFS_WAIT_NUM_ACTIVITIES; i++) {
9256 			boolean_t waited;
9257 
9258 			if (!enabled[i])
9259 				continue;
9260 
9261 			error = zfs_wait_status(zhp, i, &missing, &waited);
9262 			if (error != 0 || missing)
9263 				break;
9264 
9265 			any_waited = (any_waited || waited);
9266 		}
9267 
9268 		if (error != 0 || missing || !any_waited)
9269 			break;
9270 	}
9271 
9272 	zfs_close(zhp);
9273 
9274 	return (error);
9275 }
9276 
9277 /*
9278  * Display version message
9279  */
9280 static int
9281 zfs_do_version(int argc, char **argv)
9282 {
9283 	int c;
9284 	nvlist_t *jsobj = NULL, *zfs_ver = NULL;
9285 	boolean_t json = B_FALSE;
9286 
9287 	struct option long_options[] = {
9288 		{"json", no_argument, NULL, 'j'},
9289 		{0, 0, 0, 0}
9290 	};
9291 
9292 	while ((c = getopt_long(argc, argv, "j", long_options, NULL)) != -1) {
9293 		switch (c) {
9294 		case 'j':
9295 			json = B_TRUE;
9296 			jsobj = zfs_json_schema(0, 1);
9297 			break;
9298 		case '?':
9299 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
9300 			    optopt);
9301 			usage(B_FALSE);
9302 		}
9303 	}
9304 
9305 	argc -= optind;
9306 	if (argc != 0) {
9307 		(void) fprintf(stderr, "too many arguments\n");
9308 		usage(B_FALSE);
9309 	}
9310 
9311 	if (json) {
9312 		zfs_ver = zfs_version_nvlist();
9313 		if (zfs_ver) {
9314 			fnvlist_add_nvlist(jsobj, "zfs_version", zfs_ver);
9315 			zcmd_print_json(jsobj);
9316 			fnvlist_free(zfs_ver);
9317 			return (0);
9318 		} else
9319 			return (-1);
9320 	} else
9321 		return (zfs_version_print() != 0);
9322 }
9323 
9324 /* Display documentation */
9325 static int
9326 zfs_do_help(int argc, char **argv)
9327 {
9328 	char page[MAXNAMELEN];
9329 	if (argc < 3 || strcmp(argv[2], "zfs") == 0)
9330 		strcpy(page, "zfs");
9331 	else if (strcmp(argv[2], "concepts") == 0 ||
9332 	    strcmp(argv[2], "props") == 0)
9333 		snprintf(page, sizeof (page), "zfs%s", argv[2]);
9334 	else
9335 		snprintf(page, sizeof (page), "zfs-%s", argv[2]);
9336 
9337 	execlp("man", "man", page, NULL);
9338 
9339 	fprintf(stderr, "couldn't run man program: %s\n", strerror(errno));
9340 	return (-1);
9341 }
9342 
9343 int
9344 main(int argc, char **argv)
9345 {
9346 	int ret = 0;
9347 	int i = 0;
9348 	const char *cmdname;
9349 	char **newargv;
9350 
9351 	(void) setlocale(LC_ALL, "");
9352 	(void) setlocale(LC_NUMERIC, "C");
9353 	(void) textdomain(TEXT_DOMAIN);
9354 
9355 	opterr = 0;
9356 
9357 	/*
9358 	 * Make sure the user has specified some command.
9359 	 */
9360 	if (argc < 2) {
9361 		(void) fprintf(stderr, gettext("missing command\n"));
9362 		usage(B_FALSE);
9363 	}
9364 
9365 	cmdname = argv[1];
9366 
9367 	/*
9368 	 * The 'umount' command is an alias for 'unmount'
9369 	 */
9370 	if (strcmp(cmdname, "umount") == 0)
9371 		cmdname = "unmount";
9372 
9373 	/*
9374 	 * The 'recv' command is an alias for 'receive'
9375 	 */
9376 	if (strcmp(cmdname, "recv") == 0)
9377 		cmdname = "receive";
9378 
9379 	/*
9380 	 * The 'snap' command is an alias for 'snapshot'
9381 	 */
9382 	if (strcmp(cmdname, "snap") == 0)
9383 		cmdname = "snapshot";
9384 
9385 	/*
9386 	 * Special case '-?'
9387 	 */
9388 	if ((strcmp(cmdname, "-?") == 0) ||
9389 	    (strcmp(cmdname, "--help") == 0))
9390 		usage(B_TRUE);
9391 
9392 	/*
9393 	 * Special case '-V|--version'
9394 	 */
9395 	if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0))
9396 		return (zfs_version_print() != 0);
9397 
9398 	/*
9399 	 * Special case 'help'
9400 	 */
9401 	if (strcmp(cmdname, "help") == 0)
9402 		return (zfs_do_help(argc, argv));
9403 
9404 	if ((g_zfs = libzfs_init()) == NULL) {
9405 		(void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
9406 		return (1);
9407 	}
9408 
9409 	/*
9410 	 * Special case '<subcommand> --help|-?'
9411 	 */
9412 	if (argc >= 3 && (strcmp(argv[2], "--help") == 0 ||
9413 	    strcmp(argv[2], "-?") == 0)) {
9414 		int idx;
9415 		if (find_command_idx(cmdname, &idx) == 0) {
9416 			current_command = &command_table[idx];
9417 			usage(B_FALSE);
9418 		}
9419 	}
9420 
9421 	zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
9422 
9423 	libzfs_print_on_error(g_zfs, B_TRUE);
9424 
9425 	zfs_setproctitle_init(argc, argv, environ);
9426 
9427 	/*
9428 	 * Many commands modify input strings for string parsing reasons.
9429 	 * We create a copy to protect the original argv.
9430 	 */
9431 	newargv = safe_malloc((argc + 1) * sizeof (newargv[0]));
9432 	for (i = 0; i < argc; i++)
9433 		newargv[i] = strdup(argv[i]);
9434 	newargv[argc] = NULL;
9435 
9436 	/*
9437 	 * Run the appropriate command.
9438 	 */
9439 	libzfs_mnttab_cache(g_zfs, B_TRUE);
9440 	if (find_command_idx(cmdname, &i) == 0) {
9441 		current_command = &command_table[i];
9442 		ret = command_table[i].func(argc - 1, newargv + 1);
9443 	} else if (strchr(cmdname, '=') != NULL) {
9444 		verify(find_command_idx("set", &i) == 0);
9445 		current_command = &command_table[i];
9446 		ret = command_table[i].func(argc, newargv);
9447 	} else {
9448 		(void) fprintf(stderr, gettext("unrecognized "
9449 		    "command '%s'\n"), cmdname);
9450 		usage(B_FALSE);
9451 		ret = 1;
9452 	}
9453 
9454 	for (i = 0; i < argc; i++)
9455 		free(newargv[i]);
9456 	free(newargv);
9457 
9458 	if (ret == 0 && log_history)
9459 		(void) zpool_log_history(g_zfs, history_str);
9460 
9461 	libzfs_fini(g_zfs);
9462 
9463 	/*
9464 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
9465 	 * for the purposes of running ::findleaks.
9466 	 */
9467 	if (getenv("ZFS_ABORT") != NULL) {
9468 		(void) printf("dumping core by request\n");
9469 		abort();
9470 	}
9471 
9472 	return (ret);
9473 }
9474 
9475 /*
9476  * zfs zone nsfile filesystem
9477  *
9478  * Add or delete the given dataset to/from the namespace.
9479  */
9480 #ifdef __linux__
9481 static int
9482 zfs_do_zone_impl(int argc, char **argv, boolean_t attach)
9483 {
9484 	zfs_handle_t *zhp;
9485 	int ret;
9486 
9487 	if (argc < 3) {
9488 		(void) fprintf(stderr, gettext("missing argument(s)\n"));
9489 		usage(B_FALSE);
9490 	}
9491 	if (argc > 3) {
9492 		(void) fprintf(stderr, gettext("too many arguments\n"));
9493 		usage(B_FALSE);
9494 	}
9495 
9496 	zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
9497 	if (zhp == NULL)
9498 		return (1);
9499 
9500 	ret = (zfs_userns(zhp, argv[1], attach) != 0);
9501 
9502 	zfs_close(zhp);
9503 	return (ret);
9504 }
9505 
9506 static int
9507 zfs_do_zone(int argc, char **argv)
9508 {
9509 	return (zfs_do_zone_impl(argc, argv, B_TRUE));
9510 }
9511 
9512 static int
9513 zfs_do_unzone(int argc, char **argv)
9514 {
9515 	return (zfs_do_zone_impl(argc, argv, B_FALSE));
9516 }
9517 #endif
9518 
9519 #ifdef __FreeBSD__
9520 #include <sys/jail.h>
9521 #include <jail.h>
9522 /*
9523  * Attach/detach the given dataset to/from the given jail
9524  */
9525 static int
9526 zfs_do_jail_impl(int argc, char **argv, boolean_t attach)
9527 {
9528 	zfs_handle_t *zhp;
9529 	int jailid, ret;
9530 
9531 	/* check number of arguments */
9532 	if (argc < 3) {
9533 		(void) fprintf(stderr, gettext("missing argument(s)\n"));
9534 		usage(B_FALSE);
9535 	}
9536 	if (argc > 3) {
9537 		(void) fprintf(stderr, gettext("too many arguments\n"));
9538 		usage(B_FALSE);
9539 	}
9540 
9541 	jailid = jail_getid(argv[1]);
9542 	if (jailid < 0) {
9543 		(void) fprintf(stderr, gettext("invalid jail id or name\n"));
9544 		usage(B_FALSE);
9545 	}
9546 
9547 	zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
9548 	if (zhp == NULL)
9549 		return (1);
9550 
9551 	ret = (zfs_jail(zhp, jailid, attach) != 0);
9552 
9553 	zfs_close(zhp);
9554 	return (ret);
9555 }
9556 
9557 /*
9558  * zfs jail jailid filesystem
9559  *
9560  * Attach the given dataset to the given jail
9561  */
9562 static int
9563 zfs_do_jail(int argc, char **argv)
9564 {
9565 	return (zfs_do_jail_impl(argc, argv, B_TRUE));
9566 }
9567 
9568 /*
9569  * zfs unjail jailid filesystem
9570  *
9571  * Detach the given dataset from the given jail
9572  */
9573 static int
9574 zfs_do_unjail(int argc, char **argv)
9575 {
9576 	return (zfs_do_jail_impl(argc, argv, B_FALSE));
9577 }
9578 #endif
9579