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