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