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