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