xref: /freebsd/sys/contrib/openzfs/cmd/zfs/zfs_main.c (revision 069ac184)
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", "volume",
3676 					"snapshot", "snap",
3677 					"bookmark",
3678 					"all" };
3679 				static const int type_types[] = {
3680 					ZFS_TYPE_FILESYSTEM, ZFS_TYPE_VOLUME,
3681 					ZFS_TYPE_SNAPSHOT, ZFS_TYPE_SNAPSHOT,
3682 					ZFS_TYPE_BOOKMARK,
3683 					ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK };
3684 
3685 				for (c = 0; c < ARRAY_SIZE(type_subopts); ++c)
3686 					if (strcmp(tok, type_subopts[c]) == 0) {
3687 						types |= type_types[c];
3688 						goto found3;
3689 					}
3690 
3691 				(void) fprintf(stderr,
3692 				    gettext("invalid type '%s'\n"), tok);
3693 				usage(B_FALSE);
3694 found3:;
3695 			}
3696 			break;
3697 		case ':':
3698 			(void) fprintf(stderr, gettext("missing argument for "
3699 			    "'%c' option\n"), optopt);
3700 			usage(B_FALSE);
3701 			break;
3702 		case '?':
3703 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3704 			    optopt);
3705 			usage(B_FALSE);
3706 		}
3707 	}
3708 
3709 	argc -= optind;
3710 	argv += optind;
3711 
3712 	/*
3713 	 * If "-o space" and no types were specified, don't display snapshots.
3714 	 */
3715 	if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3716 		types &= ~ZFS_TYPE_SNAPSHOT;
3717 
3718 	/*
3719 	 * Handle users who want to list all snapshots or bookmarks
3720 	 * of the current dataset (ex. 'zfs list -t snapshot <dataset>').
3721 	 */
3722 	if ((types == ZFS_TYPE_SNAPSHOT || types == ZFS_TYPE_BOOKMARK) &&
3723 	    argc > 0 && (flags & ZFS_ITER_RECURSE) == 0 && limit == 0) {
3724 		flags |= (ZFS_ITER_DEPTH_LIMIT | ZFS_ITER_RECURSE);
3725 		limit = 1;
3726 	}
3727 
3728 	/*
3729 	 * If the user specifies '-o all', the zprop_get_list() doesn't
3730 	 * normally include the name of the dataset.  For 'zfs list', we always
3731 	 * want this property to be first.
3732 	 */
3733 	if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3734 	    != 0)
3735 		usage(B_FALSE);
3736 
3737 	cb.cb_first = B_TRUE;
3738 
3739 	/*
3740 	 * If we are only going to list and sort by properties that are "fast"
3741 	 * then we can use "simple" mode and avoid populating the properties
3742 	 * nvlist.
3743 	 */
3744 	if (zfs_list_only_by_fast(cb.cb_proplist) &&
3745 	    zfs_sort_only_by_fast(sortcol))
3746 		flags |= ZFS_ITER_SIMPLE;
3747 
3748 	ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3749 	    limit, list_callback, &cb);
3750 
3751 	zprop_free_list(cb.cb_proplist);
3752 	zfs_free_sort_columns(sortcol);
3753 
3754 	if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3755 		(void) fprintf(stderr, gettext("no datasets available\n"));
3756 
3757 	return (ret);
3758 }
3759 
3760 /*
3761  * zfs rename [-fu] <fs | snap | vol> <fs | snap | vol>
3762  * zfs rename [-f] -p <fs | vol> <fs | vol>
3763  * zfs rename [-u] -r <snap> <snap>
3764  *
3765  * Renames the given dataset to another of the same type.
3766  *
3767  * The '-p' flag creates all the non-existing ancestors of the target first.
3768  * The '-u' flag prevents file systems from being remounted during rename.
3769  */
3770 static int
3771 zfs_do_rename(int argc, char **argv)
3772 {
3773 	zfs_handle_t *zhp;
3774 	renameflags_t flags = { 0 };
3775 	int c;
3776 	int ret = 0;
3777 	int types;
3778 	boolean_t parents = B_FALSE;
3779 
3780 	/* check options */
3781 	while ((c = getopt(argc, argv, "pruf")) != -1) {
3782 		switch (c) {
3783 		case 'p':
3784 			parents = B_TRUE;
3785 			break;
3786 		case 'r':
3787 			flags.recursive = B_TRUE;
3788 			break;
3789 		case 'u':
3790 			flags.nounmount = B_TRUE;
3791 			break;
3792 		case 'f':
3793 			flags.forceunmount = B_TRUE;
3794 			break;
3795 		case '?':
3796 		default:
3797 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3798 			    optopt);
3799 			usage(B_FALSE);
3800 		}
3801 	}
3802 
3803 	argc -= optind;
3804 	argv += optind;
3805 
3806 	/* check number of arguments */
3807 	if (argc < 1) {
3808 		(void) fprintf(stderr, gettext("missing source dataset "
3809 		    "argument\n"));
3810 		usage(B_FALSE);
3811 	}
3812 	if (argc < 2) {
3813 		(void) fprintf(stderr, gettext("missing target dataset "
3814 		    "argument\n"));
3815 		usage(B_FALSE);
3816 	}
3817 	if (argc > 2) {
3818 		(void) fprintf(stderr, gettext("too many arguments\n"));
3819 		usage(B_FALSE);
3820 	}
3821 
3822 	if (flags.recursive && parents) {
3823 		(void) fprintf(stderr, gettext("-p and -r options are mutually "
3824 		    "exclusive\n"));
3825 		usage(B_FALSE);
3826 	}
3827 
3828 	if (flags.nounmount && parents) {
3829 		(void) fprintf(stderr, gettext("-u and -p options are mutually "
3830 		    "exclusive\n"));
3831 		usage(B_FALSE);
3832 	}
3833 
3834 	if (flags.recursive && strchr(argv[0], '@') == 0) {
3835 		(void) fprintf(stderr, gettext("source dataset for recursive "
3836 		    "rename must be a snapshot\n"));
3837 		usage(B_FALSE);
3838 	}
3839 
3840 	if (flags.nounmount)
3841 		types = ZFS_TYPE_FILESYSTEM;
3842 	else if (parents)
3843 		types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3844 	else
3845 		types = ZFS_TYPE_DATASET;
3846 
3847 	if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3848 		return (1);
3849 
3850 	/* If we were asked and the name looks good, try to create ancestors. */
3851 	if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3852 	    zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3853 		zfs_close(zhp);
3854 		return (1);
3855 	}
3856 
3857 	ret = (zfs_rename(zhp, argv[1], flags) != 0);
3858 
3859 	zfs_close(zhp);
3860 	return (ret);
3861 }
3862 
3863 /*
3864  * zfs promote <fs>
3865  *
3866  * Promotes the given clone fs to be the parent
3867  */
3868 static int
3869 zfs_do_promote(int argc, char **argv)
3870 {
3871 	zfs_handle_t *zhp;
3872 	int ret = 0;
3873 
3874 	/* check options */
3875 	if (argc > 1 && argv[1][0] == '-') {
3876 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3877 		    argv[1][1]);
3878 		usage(B_FALSE);
3879 	}
3880 
3881 	/* check number of arguments */
3882 	if (argc < 2) {
3883 		(void) fprintf(stderr, gettext("missing clone filesystem"
3884 		    " argument\n"));
3885 		usage(B_FALSE);
3886 	}
3887 	if (argc > 2) {
3888 		(void) fprintf(stderr, gettext("too many arguments\n"));
3889 		usage(B_FALSE);
3890 	}
3891 
3892 	zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3893 	if (zhp == NULL)
3894 		return (1);
3895 
3896 	ret = (zfs_promote(zhp) != 0);
3897 
3898 
3899 	zfs_close(zhp);
3900 	return (ret);
3901 }
3902 
3903 static int
3904 zfs_do_redact(int argc, char **argv)
3905 {
3906 	char *snap = NULL;
3907 	char *bookname = NULL;
3908 	char **rsnaps = NULL;
3909 	int numrsnaps = 0;
3910 	argv++;
3911 	argc--;
3912 	if (argc < 3) {
3913 		(void) fprintf(stderr, gettext("too few arguments\n"));
3914 		usage(B_FALSE);
3915 	}
3916 
3917 	snap = argv[0];
3918 	bookname = argv[1];
3919 	rsnaps = argv + 2;
3920 	numrsnaps = argc - 2;
3921 
3922 	nvlist_t *rsnapnv = fnvlist_alloc();
3923 
3924 	for (int i = 0; i < numrsnaps; i++) {
3925 		fnvlist_add_boolean(rsnapnv, rsnaps[i]);
3926 	}
3927 
3928 	int err = lzc_redact(snap, bookname, rsnapnv);
3929 	fnvlist_free(rsnapnv);
3930 
3931 	switch (err) {
3932 	case 0:
3933 		break;
3934 	case ENOENT: {
3935 		zfs_handle_t *zhp = zfs_open(g_zfs, snap, ZFS_TYPE_SNAPSHOT);
3936 		if (zhp == NULL) {
3937 			(void) fprintf(stderr, gettext("provided snapshot %s "
3938 			    "does not exist\n"), snap);
3939 		} else {
3940 			zfs_close(zhp);
3941 		}
3942 		for (int i = 0; i < numrsnaps; i++) {
3943 			zhp = zfs_open(g_zfs, rsnaps[i], ZFS_TYPE_SNAPSHOT);
3944 			if (zhp == NULL) {
3945 				(void) fprintf(stderr, gettext("provided "
3946 				    "snapshot %s does not exist\n"), rsnaps[i]);
3947 			} else {
3948 				zfs_close(zhp);
3949 			}
3950 		}
3951 		break;
3952 	}
3953 	case EEXIST:
3954 		(void) fprintf(stderr, gettext("specified redaction bookmark "
3955 		    "(%s) provided already exists\n"), bookname);
3956 		break;
3957 	case ENAMETOOLONG:
3958 		(void) fprintf(stderr, gettext("provided bookmark name cannot "
3959 		    "be used, final name would be too long\n"));
3960 		break;
3961 	case E2BIG:
3962 		(void) fprintf(stderr, gettext("too many redaction snapshots "
3963 		    "specified\n"));
3964 		break;
3965 	case EINVAL:
3966 		if (strchr(bookname, '#') != NULL)
3967 			(void) fprintf(stderr, gettext(
3968 			    "redaction bookmark name must not contain '#'\n"));
3969 		else
3970 			(void) fprintf(stderr, gettext(
3971 			    "redaction snapshot must be descendent of "
3972 			    "snapshot being redacted\n"));
3973 		break;
3974 	case EALREADY:
3975 		(void) fprintf(stderr, gettext("attempted to redact redacted "
3976 		    "dataset or with respect to redacted dataset\n"));
3977 		break;
3978 	case ENOTSUP:
3979 		(void) fprintf(stderr, gettext("redaction bookmarks feature "
3980 		    "not enabled\n"));
3981 		break;
3982 	case EXDEV:
3983 		(void) fprintf(stderr, gettext("potentially invalid redaction "
3984 		    "snapshot; full dataset names required\n"));
3985 		break;
3986 	case ESRCH:
3987 		(void) fprintf(stderr, gettext("attempted to resume redaction "
3988 		    " with a mismatched redaction list\n"));
3989 		break;
3990 	default:
3991 		(void) fprintf(stderr, gettext("internal error: %s\n"),
3992 		    strerror(errno));
3993 	}
3994 
3995 	return (err);
3996 }
3997 
3998 /*
3999  * zfs rollback [-rRf] <snapshot>
4000  *
4001  *	-r	Delete any intervening snapshots before doing rollback
4002  *	-R	Delete any snapshots and their clones
4003  *	-f	ignored for backwards compatibility
4004  *
4005  * Given a filesystem, rollback to a specific snapshot, discarding any changes
4006  * since then and making it the active dataset.  If more recent snapshots exist,
4007  * the command will complain unless the '-r' flag is given.
4008  */
4009 typedef struct rollback_cbdata {
4010 	uint64_t	cb_create;
4011 	uint8_t		cb_younger_ds_printed;
4012 	boolean_t	cb_first;
4013 	int		cb_doclones;
4014 	char		*cb_target;
4015 	int		cb_error;
4016 	boolean_t	cb_recurse;
4017 } rollback_cbdata_t;
4018 
4019 static int
4020 rollback_check_dependent(zfs_handle_t *zhp, void *data)
4021 {
4022 	rollback_cbdata_t *cbp = data;
4023 
4024 	if (cbp->cb_first && cbp->cb_recurse) {
4025 		(void) fprintf(stderr, gettext("cannot rollback to "
4026 		    "'%s': clones of previous snapshots exist\n"),
4027 		    cbp->cb_target);
4028 		(void) fprintf(stderr, gettext("use '-R' to "
4029 		    "force deletion of the following clones and "
4030 		    "dependents:\n"));
4031 		cbp->cb_first = 0;
4032 		cbp->cb_error = 1;
4033 	}
4034 
4035 	(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
4036 
4037 	zfs_close(zhp);
4038 	return (0);
4039 }
4040 
4041 
4042 /*
4043  * Report some snapshots/bookmarks more recent than the one specified.
4044  * Used when '-r' is not specified. We reuse this same callback for the
4045  * snapshot dependents - if 'cb_dependent' is set, then this is a
4046  * dependent and we should report it without checking the transaction group.
4047  */
4048 static int
4049 rollback_check(zfs_handle_t *zhp, void *data)
4050 {
4051 	rollback_cbdata_t *cbp = data;
4052 	/*
4053 	 * Max number of younger snapshots and/or bookmarks to display before
4054 	 * we stop the iteration.
4055 	 */
4056 	const uint8_t max_younger = 32;
4057 
4058 	if (cbp->cb_doclones) {
4059 		zfs_close(zhp);
4060 		return (0);
4061 	}
4062 
4063 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
4064 		if (cbp->cb_first && !cbp->cb_recurse) {
4065 			(void) fprintf(stderr, gettext("cannot "
4066 			    "rollback to '%s': more recent snapshots "
4067 			    "or bookmarks exist\n"),
4068 			    cbp->cb_target);
4069 			(void) fprintf(stderr, gettext("use '-r' to "
4070 			    "force deletion of the following "
4071 			    "snapshots and bookmarks:\n"));
4072 			cbp->cb_first = 0;
4073 			cbp->cb_error = 1;
4074 		}
4075 
4076 		if (cbp->cb_recurse) {
4077 			if (zfs_iter_dependents_v2(zhp, 0, B_TRUE,
4078 			    rollback_check_dependent, cbp) != 0) {
4079 				zfs_close(zhp);
4080 				return (-1);
4081 			}
4082 		} else {
4083 			(void) fprintf(stderr, "%s\n",
4084 			    zfs_get_name(zhp));
4085 			cbp->cb_younger_ds_printed++;
4086 		}
4087 	}
4088 	zfs_close(zhp);
4089 
4090 	if (cbp->cb_younger_ds_printed == max_younger) {
4091 		/*
4092 		 * This non-recursive rollback is going to fail due to the
4093 		 * presence of snapshots and/or bookmarks that are younger than
4094 		 * the rollback target.
4095 		 * We printed some of the offending objects, now we stop
4096 		 * zfs_iter_snapshot/bookmark iteration so we can fail fast and
4097 		 * avoid iterating over the rest of the younger objects
4098 		 */
4099 		(void) fprintf(stderr, gettext("Output limited to %d "
4100 		    "snapshots/bookmarks\n"), max_younger);
4101 		return (-1);
4102 	}
4103 	return (0);
4104 }
4105 
4106 static int
4107 zfs_do_rollback(int argc, char **argv)
4108 {
4109 	int ret = 0;
4110 	int c;
4111 	boolean_t force = B_FALSE;
4112 	rollback_cbdata_t cb = { 0 };
4113 	zfs_handle_t *zhp, *snap;
4114 	char parentname[ZFS_MAX_DATASET_NAME_LEN];
4115 	char *delim;
4116 	uint64_t min_txg = 0;
4117 
4118 	/* check options */
4119 	while ((c = getopt(argc, argv, "rRf")) != -1) {
4120 		switch (c) {
4121 		case 'r':
4122 			cb.cb_recurse = 1;
4123 			break;
4124 		case 'R':
4125 			cb.cb_recurse = 1;
4126 			cb.cb_doclones = 1;
4127 			break;
4128 		case 'f':
4129 			force = B_TRUE;
4130 			break;
4131 		case '?':
4132 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4133 			    optopt);
4134 			usage(B_FALSE);
4135 		}
4136 	}
4137 
4138 	argc -= optind;
4139 	argv += optind;
4140 
4141 	/* check number of arguments */
4142 	if (argc < 1) {
4143 		(void) fprintf(stderr, gettext("missing dataset argument\n"));
4144 		usage(B_FALSE);
4145 	}
4146 	if (argc > 1) {
4147 		(void) fprintf(stderr, gettext("too many arguments\n"));
4148 		usage(B_FALSE);
4149 	}
4150 
4151 	/* open the snapshot */
4152 	if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
4153 		return (1);
4154 
4155 	/* open the parent dataset */
4156 	(void) strlcpy(parentname, argv[0], sizeof (parentname));
4157 	verify((delim = strrchr(parentname, '@')) != NULL);
4158 	*delim = '\0';
4159 	if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
4160 		zfs_close(snap);
4161 		return (1);
4162 	}
4163 
4164 	/*
4165 	 * Check for more recent snapshots and/or clones based on the presence
4166 	 * of '-r' and '-R'.
4167 	 */
4168 	cb.cb_target = argv[0];
4169 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
4170 	cb.cb_first = B_TRUE;
4171 	cb.cb_error = 0;
4172 
4173 	if (cb.cb_create > 0)
4174 		min_txg = cb.cb_create;
4175 
4176 	if ((ret = zfs_iter_snapshots_v2(zhp, 0, rollback_check, &cb,
4177 	    min_txg, 0)) != 0)
4178 		goto out;
4179 	if ((ret = zfs_iter_bookmarks_v2(zhp, 0, rollback_check, &cb)) != 0)
4180 		goto out;
4181 
4182 	if ((ret = cb.cb_error) != 0)
4183 		goto out;
4184 
4185 	/*
4186 	 * Rollback parent to the given snapshot.
4187 	 */
4188 	ret = zfs_rollback(zhp, snap, force);
4189 
4190 out:
4191 	zfs_close(snap);
4192 	zfs_close(zhp);
4193 
4194 	if (ret == 0)
4195 		return (0);
4196 	else
4197 		return (1);
4198 }
4199 
4200 /*
4201  * zfs set property=value ... { fs | snap | vol } ...
4202  *
4203  * Sets the given properties for all datasets specified on the command line.
4204  */
4205 
4206 static int
4207 set_callback(zfs_handle_t *zhp, void *data)
4208 {
4209 	zprop_set_cbdata_t *cb = data;
4210 	int ret = zfs_prop_set_list_flags(zhp, cb->cb_proplist, cb->cb_flags);
4211 
4212 	if (ret != 0 || libzfs_errno(g_zfs) != EZFS_SUCCESS) {
4213 		switch (libzfs_errno(g_zfs)) {
4214 		case EZFS_MOUNTFAILED:
4215 			(void) fprintf(stderr, gettext("property may be set "
4216 			    "but unable to remount filesystem\n"));
4217 			break;
4218 		case EZFS_SHARENFSFAILED:
4219 			(void) fprintf(stderr, gettext("property may be set "
4220 			    "but unable to reshare filesystem\n"));
4221 			break;
4222 		}
4223 	}
4224 	return (ret);
4225 }
4226 
4227 static int
4228 zfs_do_set(int argc, char **argv)
4229 {
4230 	zprop_set_cbdata_t cb = { 0 };
4231 	int ds_start = -1; /* argv idx of first dataset arg */
4232 	int ret = 0;
4233 	int i, c;
4234 
4235 	/* check options */
4236 	while ((c = getopt(argc, argv, "u")) != -1) {
4237 		switch (c) {
4238 		case 'u':
4239 			cb.cb_flags |= ZFS_SET_NOMOUNT;
4240 			break;
4241 		case '?':
4242 		default:
4243 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4244 			    optopt);
4245 			usage(B_FALSE);
4246 		}
4247 	}
4248 
4249 	argc -= optind;
4250 	argv += optind;
4251 
4252 	/* check number of arguments */
4253 	if (argc < 1) {
4254 		(void) fprintf(stderr, gettext("missing arguments\n"));
4255 		usage(B_FALSE);
4256 	}
4257 	if (argc < 2) {
4258 		if (strchr(argv[0], '=') == NULL) {
4259 			(void) fprintf(stderr, gettext("missing property=value "
4260 			    "argument(s)\n"));
4261 		} else {
4262 			(void) fprintf(stderr, gettext("missing dataset "
4263 			    "name(s)\n"));
4264 		}
4265 		usage(B_FALSE);
4266 	}
4267 
4268 	/* validate argument order:  prop=val args followed by dataset args */
4269 	for (i = 0; i < argc; i++) {
4270 		if (strchr(argv[i], '=') != NULL) {
4271 			if (ds_start > 0) {
4272 				/* out-of-order prop=val argument */
4273 				(void) fprintf(stderr, gettext("invalid "
4274 				    "argument order\n"));
4275 				usage(B_FALSE);
4276 			}
4277 		} else if (ds_start < 0) {
4278 			ds_start = i;
4279 		}
4280 	}
4281 	if (ds_start < 0) {
4282 		(void) fprintf(stderr, gettext("missing dataset name(s)\n"));
4283 		usage(B_FALSE);
4284 	}
4285 
4286 	/* Populate a list of property settings */
4287 	if (nvlist_alloc(&cb.cb_proplist, NV_UNIQUE_NAME, 0) != 0)
4288 		nomem();
4289 	for (i = 0; i < ds_start; i++) {
4290 		if (!parseprop(cb.cb_proplist, argv[i])) {
4291 			ret = -1;
4292 			goto error;
4293 		}
4294 	}
4295 
4296 	ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
4297 	    ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
4298 
4299 error:
4300 	nvlist_free(cb.cb_proplist);
4301 	return (ret);
4302 }
4303 
4304 typedef struct snap_cbdata {
4305 	nvlist_t *sd_nvl;
4306 	boolean_t sd_recursive;
4307 	const char *sd_snapname;
4308 } snap_cbdata_t;
4309 
4310 static int
4311 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
4312 {
4313 	snap_cbdata_t *sd = arg;
4314 	char *name;
4315 	int rv = 0;
4316 	int error;
4317 
4318 	if (sd->sd_recursive &&
4319 	    zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
4320 		zfs_close(zhp);
4321 		return (0);
4322 	}
4323 
4324 	error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
4325 	if (error == -1)
4326 		nomem();
4327 	fnvlist_add_boolean(sd->sd_nvl, name);
4328 	free(name);
4329 
4330 	if (sd->sd_recursive)
4331 		rv = zfs_iter_filesystems_v2(zhp, 0, zfs_snapshot_cb, sd);
4332 	zfs_close(zhp);
4333 	return (rv);
4334 }
4335 
4336 /*
4337  * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
4338  *
4339  * Creates a snapshot with the given name.  While functionally equivalent to
4340  * 'zfs create', it is a separate command to differentiate intent.
4341  */
4342 static int
4343 zfs_do_snapshot(int argc, char **argv)
4344 {
4345 	int ret = 0;
4346 	int c;
4347 	nvlist_t *props;
4348 	snap_cbdata_t sd = { 0 };
4349 	boolean_t multiple_snaps = B_FALSE;
4350 
4351 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4352 		nomem();
4353 	if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
4354 		nomem();
4355 
4356 	/* check options */
4357 	while ((c = getopt(argc, argv, "ro:")) != -1) {
4358 		switch (c) {
4359 		case 'o':
4360 			if (!parseprop(props, optarg)) {
4361 				nvlist_free(sd.sd_nvl);
4362 				nvlist_free(props);
4363 				return (1);
4364 			}
4365 			break;
4366 		case 'r':
4367 			sd.sd_recursive = B_TRUE;
4368 			multiple_snaps = B_TRUE;
4369 			break;
4370 		case '?':
4371 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4372 			    optopt);
4373 			goto usage;
4374 		}
4375 	}
4376 
4377 	argc -= optind;
4378 	argv += optind;
4379 
4380 	/* check number of arguments */
4381 	if (argc < 1) {
4382 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
4383 		goto usage;
4384 	}
4385 
4386 	if (argc > 1)
4387 		multiple_snaps = B_TRUE;
4388 	for (; argc > 0; argc--, argv++) {
4389 		char *atp;
4390 		zfs_handle_t *zhp;
4391 
4392 		atp = strchr(argv[0], '@');
4393 		if (atp == NULL)
4394 			goto usage;
4395 		*atp = '\0';
4396 		sd.sd_snapname = atp + 1;
4397 		zhp = zfs_open(g_zfs, argv[0],
4398 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4399 		if (zhp == NULL)
4400 			goto usage;
4401 		if (zfs_snapshot_cb(zhp, &sd) != 0)
4402 			goto usage;
4403 	}
4404 
4405 	ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
4406 	nvlist_free(sd.sd_nvl);
4407 	nvlist_free(props);
4408 	if (ret != 0 && multiple_snaps)
4409 		(void) fprintf(stderr, gettext("no snapshots were created\n"));
4410 	return (ret != 0);
4411 
4412 usage:
4413 	nvlist_free(sd.sd_nvl);
4414 	nvlist_free(props);
4415 	usage(B_FALSE);
4416 	return (-1);
4417 }
4418 
4419 /*
4420  * Array of prefixes to exclude –
4421  * a linear search, even if executed for each dataset,
4422  * is plenty good enough.
4423  */
4424 typedef struct zfs_send_exclude_arg {
4425 	size_t count;
4426 	const char **list;
4427 } zfs_send_exclude_arg_t;
4428 
4429 static boolean_t
4430 zfs_do_send_exclude(zfs_handle_t *zhp, void *context)
4431 {
4432 	zfs_send_exclude_arg_t *excludes = context;
4433 	const char *name = zfs_get_name(zhp);
4434 
4435 	for (size_t i = 0; i < excludes->count; ++i) {
4436 		size_t len = strlen(excludes->list[i]);
4437 		if (strncmp(name, excludes->list[i], len) == 0 &&
4438 		    memchr("/@", name[len], sizeof ("/@")))
4439 			return (B_FALSE);
4440 	}
4441 
4442 	return (B_TRUE);
4443 }
4444 
4445 /*
4446  * Send a backup stream to stdout.
4447  */
4448 static int
4449 zfs_do_send(int argc, char **argv)
4450 {
4451 	char *fromname = NULL;
4452 	char *toname = NULL;
4453 	char *resume_token = NULL;
4454 	char *cp;
4455 	zfs_handle_t *zhp;
4456 	sendflags_t flags = { 0 };
4457 	int c, err;
4458 	nvlist_t *dbgnv = NULL;
4459 	char *redactbook = NULL;
4460 	zfs_send_exclude_arg_t excludes = { 0 };
4461 
4462 	struct option long_options[] = {
4463 		{"replicate",	no_argument,		NULL, 'R'},
4464 		{"skip-missing",	no_argument,	NULL, 's'},
4465 		{"redact",	required_argument,	NULL, 'd'},
4466 		{"props",	no_argument,		NULL, 'p'},
4467 		{"parsable",	no_argument,		NULL, 'P'},
4468 		{"dedup",	no_argument,		NULL, 'D'},
4469 		{"proctitle",	no_argument,		NULL, 'V'},
4470 		{"verbose",	no_argument,		NULL, 'v'},
4471 		{"dryrun",	no_argument,		NULL, 'n'},
4472 		{"large-block",	no_argument,		NULL, 'L'},
4473 		{"embed",	no_argument,		NULL, 'e'},
4474 		{"resume",	required_argument,	NULL, 't'},
4475 		{"compressed",	no_argument,		NULL, 'c'},
4476 		{"raw",		no_argument,		NULL, 'w'},
4477 		{"backup",	no_argument,		NULL, 'b'},
4478 		{"holds",	no_argument,		NULL, 'h'},
4479 		{"saved",	no_argument,		NULL, 'S'},
4480 		{"exclude",	required_argument,	NULL, 'X'},
4481 		{0, 0, 0, 0}
4482 	};
4483 
4484 	/* check options */
4485 	while ((c = getopt_long(argc, argv, ":i:I:RsDpVvnPLeht:cwbd:SX:",
4486 	    long_options, NULL)) != -1) {
4487 		switch (c) {
4488 		case 'X':
4489 			for (char *ds; (ds = strsep(&optarg, ",")) != NULL; ) {
4490 				if (!zfs_name_valid(ds, ZFS_TYPE_DATASET) ||
4491 				    strchr(ds, '/') == NULL) {
4492 					(void) fprintf(stderr, gettext("-X %s: "
4493 					    "not a valid non-root dataset name"
4494 					    ".\n"), ds);
4495 					usage(B_FALSE);
4496 				}
4497 				excludes.list = safe_realloc(excludes.list,
4498 				    sizeof (char *) * (excludes.count + 1));
4499 				excludes.list[excludes.count++] = ds;
4500 			}
4501 			break;
4502 		case 'i':
4503 			if (fromname)
4504 				usage(B_FALSE);
4505 			fromname = optarg;
4506 			break;
4507 		case 'I':
4508 			if (fromname)
4509 				usage(B_FALSE);
4510 			fromname = optarg;
4511 			flags.doall = B_TRUE;
4512 			break;
4513 		case 'R':
4514 			flags.replicate = B_TRUE;
4515 			break;
4516 		case 's':
4517 			flags.skipmissing = B_TRUE;
4518 			break;
4519 		case 'd':
4520 			redactbook = optarg;
4521 			break;
4522 		case 'p':
4523 			flags.props = B_TRUE;
4524 			break;
4525 		case 'b':
4526 			flags.backup = B_TRUE;
4527 			break;
4528 		case 'h':
4529 			flags.holds = B_TRUE;
4530 			break;
4531 		case 'P':
4532 			flags.parsable = B_TRUE;
4533 			break;
4534 		case 'V':
4535 			flags.progressastitle = B_TRUE;
4536 			break;
4537 		case 'v':
4538 			flags.verbosity++;
4539 			flags.progress = B_TRUE;
4540 			break;
4541 		case 'D':
4542 			(void) fprintf(stderr,
4543 			    gettext("WARNING: deduplicated send is no "
4544 			    "longer supported.  A regular,\n"
4545 			    "non-deduplicated stream will be generated.\n\n"));
4546 			break;
4547 		case 'n':
4548 			flags.dryrun = B_TRUE;
4549 			break;
4550 		case 'L':
4551 			flags.largeblock = B_TRUE;
4552 			break;
4553 		case 'e':
4554 			flags.embed_data = B_TRUE;
4555 			break;
4556 		case 't':
4557 			resume_token = optarg;
4558 			break;
4559 		case 'c':
4560 			flags.compress = B_TRUE;
4561 			break;
4562 		case 'w':
4563 			flags.raw = B_TRUE;
4564 			flags.compress = B_TRUE;
4565 			flags.embed_data = B_TRUE;
4566 			flags.largeblock = B_TRUE;
4567 			break;
4568 		case 'S':
4569 			flags.saved = B_TRUE;
4570 			break;
4571 		case ':':
4572 			/*
4573 			 * If a parameter was not passed, optopt contains the
4574 			 * value that would normally lead us into the
4575 			 * appropriate case statement.  If it's > 256, then this
4576 			 * must be a longopt and we should look at argv to get
4577 			 * the string.  Otherwise it's just the character, so we
4578 			 * should use it directly.
4579 			 */
4580 			if (optopt <= UINT8_MAX) {
4581 				(void) fprintf(stderr,
4582 				    gettext("missing argument for '%c' "
4583 				    "option\n"), optopt);
4584 			} else {
4585 				(void) fprintf(stderr,
4586 				    gettext("missing argument for '%s' "
4587 				    "option\n"), argv[optind - 1]);
4588 			}
4589 			free(excludes.list);
4590 			usage(B_FALSE);
4591 			break;
4592 		case '?':
4593 		default:
4594 			/*
4595 			 * If an invalid flag was passed, optopt contains the
4596 			 * character if it was a short flag, or 0 if it was a
4597 			 * longopt.
4598 			 */
4599 			if (optopt != 0) {
4600 				(void) fprintf(stderr,
4601 				    gettext("invalid option '%c'\n"), optopt);
4602 			} else {
4603 				(void) fprintf(stderr,
4604 				    gettext("invalid option '%s'\n"),
4605 				    argv[optind - 1]);
4606 
4607 			}
4608 			free(excludes.list);
4609 			usage(B_FALSE);
4610 		}
4611 	}
4612 
4613 	if ((flags.parsable || flags.progressastitle) && flags.verbosity == 0)
4614 		flags.verbosity = 1;
4615 
4616 	if (excludes.count > 0 && !flags.replicate) {
4617 		free(excludes.list);
4618 		(void) fprintf(stderr, gettext("Cannot specify "
4619 		    "dataset exclusion (-X) on a non-recursive "
4620 		    "send.\n"));
4621 		return (1);
4622 	}
4623 
4624 	argc -= optind;
4625 	argv += optind;
4626 
4627 	if (resume_token != NULL) {
4628 		if (fromname != NULL || flags.replicate || flags.props ||
4629 		    flags.backup || flags.holds ||
4630 		    flags.saved || redactbook != NULL) {
4631 			free(excludes.list);
4632 			(void) fprintf(stderr,
4633 			    gettext("invalid flags combined with -t\n"));
4634 			usage(B_FALSE);
4635 		}
4636 		if (argc > 0) {
4637 			free(excludes.list);
4638 			(void) fprintf(stderr, gettext("too many arguments\n"));
4639 			usage(B_FALSE);
4640 		}
4641 	} else {
4642 		if (argc < 1) {
4643 			free(excludes.list);
4644 			(void) fprintf(stderr,
4645 			    gettext("missing snapshot argument\n"));
4646 			usage(B_FALSE);
4647 		}
4648 		if (argc > 1) {
4649 			free(excludes.list);
4650 			(void) fprintf(stderr, gettext("too many arguments\n"));
4651 			usage(B_FALSE);
4652 		}
4653 	}
4654 
4655 	if (flags.saved) {
4656 		if (fromname != NULL || flags.replicate || flags.props ||
4657 		    flags.doall || flags.backup ||
4658 		    flags.holds || flags.largeblock || flags.embed_data ||
4659 		    flags.compress || flags.raw || redactbook != NULL) {
4660 			free(excludes.list);
4661 
4662 			(void) fprintf(stderr, gettext("incompatible flags "
4663 			    "combined with saved send flag\n"));
4664 			usage(B_FALSE);
4665 		}
4666 		if (strchr(argv[0], '@') != NULL) {
4667 			free(excludes.list);
4668 
4669 			(void) fprintf(stderr, gettext("saved send must "
4670 			    "specify the dataset with partially-received "
4671 			    "state\n"));
4672 			usage(B_FALSE);
4673 		}
4674 	}
4675 
4676 	if (flags.raw && redactbook != NULL) {
4677 		free(excludes.list);
4678 		(void) fprintf(stderr,
4679 		    gettext("Error: raw sends may not be redacted.\n"));
4680 		return (1);
4681 	}
4682 
4683 	if (!flags.dryrun && isatty(STDOUT_FILENO)) {
4684 		free(excludes.list);
4685 		(void) fprintf(stderr,
4686 		    gettext("Error: Stream can not be written to a terminal.\n"
4687 		    "You must redirect standard output.\n"));
4688 		return (1);
4689 	}
4690 
4691 	if (flags.saved) {
4692 		zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
4693 		if (zhp == NULL) {
4694 			free(excludes.list);
4695 			return (1);
4696 		}
4697 
4698 		err = zfs_send_saved(zhp, &flags, STDOUT_FILENO,
4699 		    resume_token);
4700 		free(excludes.list);
4701 		zfs_close(zhp);
4702 		return (err != 0);
4703 	} else if (resume_token != NULL) {
4704 		free(excludes.list);
4705 		return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
4706 		    resume_token));
4707 	}
4708 
4709 	if (flags.skipmissing && !flags.replicate) {
4710 		free(excludes.list);
4711 		(void) fprintf(stderr,
4712 		    gettext("skip-missing flag can only be used in "
4713 		    "conjunction with replicate\n"));
4714 		usage(B_FALSE);
4715 	}
4716 
4717 	/*
4718 	 * For everything except -R and -I, use the new, cleaner code path.
4719 	 */
4720 	if (!(flags.replicate || flags.doall)) {
4721 		char frombuf[ZFS_MAX_DATASET_NAME_LEN];
4722 
4723 		if (fromname != NULL && (strchr(fromname, '#') == NULL &&
4724 		    strchr(fromname, '@') == NULL)) {
4725 			/*
4726 			 * Neither bookmark or snapshot was specified.  Print a
4727 			 * warning, and assume snapshot.
4728 			 */
4729 			(void) fprintf(stderr, "Warning: incremental source "
4730 			    "didn't specify type, assuming snapshot. Use '@' "
4731 			    "or '#' prefix to avoid ambiguity.\n");
4732 			(void) snprintf(frombuf, sizeof (frombuf), "@%s",
4733 			    fromname);
4734 			fromname = frombuf;
4735 		}
4736 		if (fromname != NULL &&
4737 		    (fromname[0] == '#' || fromname[0] == '@')) {
4738 			/*
4739 			 * Incremental source name begins with # or @.
4740 			 * Default to same fs as target.
4741 			 */
4742 			char tmpbuf[ZFS_MAX_DATASET_NAME_LEN];
4743 			(void) strlcpy(tmpbuf, fromname, sizeof (tmpbuf));
4744 			(void) strlcpy(frombuf, argv[0], sizeof (frombuf));
4745 			cp = strchr(frombuf, '@');
4746 			if (cp != NULL)
4747 				*cp = '\0';
4748 			(void) strlcat(frombuf, tmpbuf, sizeof (frombuf));
4749 			fromname = frombuf;
4750 		}
4751 
4752 		zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
4753 		if (zhp == NULL) {
4754 			free(excludes.list);
4755 			return (1);
4756 		}
4757 		err = zfs_send_one(zhp, fromname, STDOUT_FILENO, &flags,
4758 		    redactbook);
4759 
4760 		free(excludes.list);
4761 		zfs_close(zhp);
4762 		return (err != 0);
4763 	}
4764 
4765 	if (fromname != NULL && strchr(fromname, '#')) {
4766 		(void) fprintf(stderr,
4767 		    gettext("Error: multiple snapshots cannot be "
4768 		    "sent from a bookmark.\n"));
4769 		free(excludes.list);
4770 		return (1);
4771 	}
4772 
4773 	if (redactbook != NULL) {
4774 		(void) fprintf(stderr, gettext("Error: multiple snapshots "
4775 		    "cannot be sent redacted.\n"));
4776 		free(excludes.list);
4777 		return (1);
4778 	}
4779 
4780 	if ((cp = strchr(argv[0], '@')) == NULL) {
4781 		(void) fprintf(stderr, gettext("Error: "
4782 		    "Unsupported flag with filesystem or bookmark.\n"));
4783 		free(excludes.list);
4784 		return (1);
4785 	}
4786 	*cp = '\0';
4787 	toname = cp + 1;
4788 	zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4789 	if (zhp == NULL) {
4790 		free(excludes.list);
4791 		return (1);
4792 	}
4793 
4794 	/*
4795 	 * If they specified the full path to the snapshot, chop off
4796 	 * everything except the short name of the snapshot, but special
4797 	 * case if they specify the origin.
4798 	 */
4799 	if (fromname && (cp = strchr(fromname, '@')) != NULL) {
4800 		char origin[ZFS_MAX_DATASET_NAME_LEN];
4801 		zprop_source_t src;
4802 
4803 		(void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
4804 		    origin, sizeof (origin), &src, NULL, 0, B_FALSE);
4805 
4806 		if (strcmp(origin, fromname) == 0) {
4807 			fromname = NULL;
4808 			flags.fromorigin = B_TRUE;
4809 		} else {
4810 			*cp = '\0';
4811 			if (cp != fromname && strcmp(argv[0], fromname)) {
4812 				zfs_close(zhp);
4813 				free(excludes.list);
4814 				(void) fprintf(stderr,
4815 				    gettext("incremental source must be "
4816 				    "in same filesystem\n"));
4817 				usage(B_FALSE);
4818 			}
4819 			fromname = cp + 1;
4820 			if (strchr(fromname, '@') || strchr(fromname, '/')) {
4821 				zfs_close(zhp);
4822 				free(excludes.list);
4823 				(void) fprintf(stderr,
4824 				    gettext("invalid incremental source\n"));
4825 				usage(B_FALSE);
4826 			}
4827 		}
4828 	}
4829 
4830 	if (flags.replicate && fromname == NULL)
4831 		flags.doall = B_TRUE;
4832 
4833 	err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO,
4834 	    excludes.count > 0 ? zfs_do_send_exclude : NULL,
4835 	    &excludes, flags.verbosity >= 3 ? &dbgnv : NULL);
4836 
4837 	if (flags.verbosity >= 3 && dbgnv != NULL) {
4838 		/*
4839 		 * dump_nvlist prints to stdout, but that's been
4840 		 * redirected to a file.  Make it print to stderr
4841 		 * instead.
4842 		 */
4843 		(void) dup2(STDERR_FILENO, STDOUT_FILENO);
4844 		dump_nvlist(dbgnv, 0);
4845 		nvlist_free(dbgnv);
4846 	}
4847 
4848 	zfs_close(zhp);
4849 	free(excludes.list);
4850 	return (err != 0);
4851 }
4852 
4853 /*
4854  * Restore a backup stream from stdin.
4855  */
4856 static int
4857 zfs_do_receive(int argc, char **argv)
4858 {
4859 	int c, err = 0;
4860 	recvflags_t flags = { 0 };
4861 	boolean_t abort_resumable = B_FALSE;
4862 	nvlist_t *props;
4863 
4864 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4865 		nomem();
4866 
4867 	/* check options */
4868 	while ((c = getopt(argc, argv, ":o:x:dehMnuvFsAc")) != -1) {
4869 		switch (c) {
4870 		case 'o':
4871 			if (!parseprop(props, optarg)) {
4872 				nvlist_free(props);
4873 				usage(B_FALSE);
4874 			}
4875 			break;
4876 		case 'x':
4877 			if (!parsepropname(props, optarg)) {
4878 				nvlist_free(props);
4879 				usage(B_FALSE);
4880 			}
4881 			break;
4882 		case 'd':
4883 			if (flags.istail) {
4884 				(void) fprintf(stderr, gettext("invalid option "
4885 				    "combination: -d and -e are mutually "
4886 				    "exclusive\n"));
4887 				usage(B_FALSE);
4888 			}
4889 			flags.isprefix = B_TRUE;
4890 			break;
4891 		case 'e':
4892 			if (flags.isprefix) {
4893 				(void) fprintf(stderr, gettext("invalid option "
4894 				    "combination: -d and -e are mutually "
4895 				    "exclusive\n"));
4896 				usage(B_FALSE);
4897 			}
4898 			flags.istail = B_TRUE;
4899 			break;
4900 		case 'h':
4901 			flags.skipholds = B_TRUE;
4902 			break;
4903 		case 'M':
4904 			flags.forceunmount = B_TRUE;
4905 			break;
4906 		case 'n':
4907 			flags.dryrun = B_TRUE;
4908 			break;
4909 		case 'u':
4910 			flags.nomount = B_TRUE;
4911 			break;
4912 		case 'v':
4913 			flags.verbose = B_TRUE;
4914 			break;
4915 		case 's':
4916 			flags.resumable = B_TRUE;
4917 			break;
4918 		case 'F':
4919 			flags.force = B_TRUE;
4920 			break;
4921 		case 'A':
4922 			abort_resumable = B_TRUE;
4923 			break;
4924 		case 'c':
4925 			flags.heal = B_TRUE;
4926 			break;
4927 		case ':':
4928 			(void) fprintf(stderr, gettext("missing argument for "
4929 			    "'%c' option\n"), optopt);
4930 			usage(B_FALSE);
4931 			break;
4932 		case '?':
4933 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4934 			    optopt);
4935 			usage(B_FALSE);
4936 		}
4937 	}
4938 
4939 	argc -= optind;
4940 	argv += optind;
4941 
4942 	/* zfs recv -e (use "tail" name) implies -d (remove dataset "head") */
4943 	if (flags.istail)
4944 		flags.isprefix = B_TRUE;
4945 
4946 	/* check number of arguments */
4947 	if (argc < 1) {
4948 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
4949 		usage(B_FALSE);
4950 	}
4951 	if (argc > 1) {
4952 		(void) fprintf(stderr, gettext("too many arguments\n"));
4953 		usage(B_FALSE);
4954 	}
4955 
4956 	if (abort_resumable) {
4957 		if (flags.isprefix || flags.istail || flags.dryrun ||
4958 		    flags.resumable || flags.nomount) {
4959 			(void) fprintf(stderr, gettext("invalid option\n"));
4960 			usage(B_FALSE);
4961 		}
4962 
4963 		char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4964 		(void) snprintf(namebuf, sizeof (namebuf),
4965 		    "%s/%%recv", argv[0]);
4966 
4967 		if (zfs_dataset_exists(g_zfs, namebuf,
4968 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4969 			zfs_handle_t *zhp = zfs_open(g_zfs,
4970 			    namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4971 			if (zhp == NULL) {
4972 				nvlist_free(props);
4973 				return (1);
4974 			}
4975 			err = zfs_destroy(zhp, B_FALSE);
4976 			zfs_close(zhp);
4977 		} else {
4978 			zfs_handle_t *zhp = zfs_open(g_zfs,
4979 			    argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4980 			if (zhp == NULL)
4981 				usage(B_FALSE);
4982 			if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4983 			    zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4984 			    NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4985 				(void) fprintf(stderr,
4986 				    gettext("'%s' does not have any "
4987 				    "resumable receive state to abort\n"),
4988 				    argv[0]);
4989 				nvlist_free(props);
4990 				zfs_close(zhp);
4991 				return (1);
4992 			}
4993 			err = zfs_destroy(zhp, B_FALSE);
4994 			zfs_close(zhp);
4995 		}
4996 		nvlist_free(props);
4997 		return (err != 0);
4998 	}
4999 
5000 	if (isatty(STDIN_FILENO)) {
5001 		(void) fprintf(stderr,
5002 		    gettext("Error: Backup stream can not be read "
5003 		    "from a terminal.\n"
5004 		    "You must redirect standard input.\n"));
5005 		nvlist_free(props);
5006 		return (1);
5007 	}
5008 	err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
5009 	nvlist_free(props);
5010 
5011 	return (err != 0);
5012 }
5013 
5014 /*
5015  * allow/unallow stuff
5016  */
5017 /* copied from zfs/sys/dsl_deleg.h */
5018 #define	ZFS_DELEG_PERM_CREATE		"create"
5019 #define	ZFS_DELEG_PERM_DESTROY		"destroy"
5020 #define	ZFS_DELEG_PERM_SNAPSHOT		"snapshot"
5021 #define	ZFS_DELEG_PERM_ROLLBACK		"rollback"
5022 #define	ZFS_DELEG_PERM_CLONE		"clone"
5023 #define	ZFS_DELEG_PERM_PROMOTE		"promote"
5024 #define	ZFS_DELEG_PERM_RENAME		"rename"
5025 #define	ZFS_DELEG_PERM_MOUNT		"mount"
5026 #define	ZFS_DELEG_PERM_SHARE		"share"
5027 #define	ZFS_DELEG_PERM_SEND		"send"
5028 #define	ZFS_DELEG_PERM_RECEIVE		"receive"
5029 #define	ZFS_DELEG_PERM_ALLOW		"allow"
5030 #define	ZFS_DELEG_PERM_USERPROP		"userprop"
5031 #define	ZFS_DELEG_PERM_VSCAN		"vscan" /* ??? */
5032 #define	ZFS_DELEG_PERM_USERQUOTA	"userquota"
5033 #define	ZFS_DELEG_PERM_GROUPQUOTA	"groupquota"
5034 #define	ZFS_DELEG_PERM_USERUSED		"userused"
5035 #define	ZFS_DELEG_PERM_GROUPUSED	"groupused"
5036 #define	ZFS_DELEG_PERM_USEROBJQUOTA	"userobjquota"
5037 #define	ZFS_DELEG_PERM_GROUPOBJQUOTA	"groupobjquota"
5038 #define	ZFS_DELEG_PERM_USEROBJUSED	"userobjused"
5039 #define	ZFS_DELEG_PERM_GROUPOBJUSED	"groupobjused"
5040 
5041 #define	ZFS_DELEG_PERM_HOLD		"hold"
5042 #define	ZFS_DELEG_PERM_RELEASE		"release"
5043 #define	ZFS_DELEG_PERM_DIFF		"diff"
5044 #define	ZFS_DELEG_PERM_BOOKMARK		"bookmark"
5045 #define	ZFS_DELEG_PERM_LOAD_KEY		"load-key"
5046 #define	ZFS_DELEG_PERM_CHANGE_KEY	"change-key"
5047 
5048 #define	ZFS_DELEG_PERM_PROJECTUSED	"projectused"
5049 #define	ZFS_DELEG_PERM_PROJECTQUOTA	"projectquota"
5050 #define	ZFS_DELEG_PERM_PROJECTOBJUSED	"projectobjused"
5051 #define	ZFS_DELEG_PERM_PROJECTOBJQUOTA	"projectobjquota"
5052 
5053 #define	ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
5054 
5055 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
5056 	{ ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
5057 	{ ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
5058 	{ ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
5059 	{ ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
5060 	{ ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
5061 	{ ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
5062 	{ ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
5063 	{ ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
5064 	{ ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
5065 	{ ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
5066 	{ ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
5067 	{ ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
5068 	{ ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
5069 	{ ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
5070 	{ ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
5071 	{ ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
5072 	{ ZFS_DELEG_PERM_LOAD_KEY, ZFS_DELEG_NOTE_LOAD_KEY },
5073 	{ ZFS_DELEG_PERM_CHANGE_KEY, ZFS_DELEG_NOTE_CHANGE_KEY },
5074 
5075 	{ ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
5076 	{ ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
5077 	{ ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
5078 	{ ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
5079 	{ ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
5080 	{ ZFS_DELEG_PERM_USEROBJQUOTA, ZFS_DELEG_NOTE_USEROBJQUOTA },
5081 	{ ZFS_DELEG_PERM_USEROBJUSED, ZFS_DELEG_NOTE_USEROBJUSED },
5082 	{ ZFS_DELEG_PERM_GROUPOBJQUOTA, ZFS_DELEG_NOTE_GROUPOBJQUOTA },
5083 	{ ZFS_DELEG_PERM_GROUPOBJUSED, ZFS_DELEG_NOTE_GROUPOBJUSED },
5084 	{ ZFS_DELEG_PERM_PROJECTUSED, ZFS_DELEG_NOTE_PROJECTUSED },
5085 	{ ZFS_DELEG_PERM_PROJECTQUOTA, ZFS_DELEG_NOTE_PROJECTQUOTA },
5086 	{ ZFS_DELEG_PERM_PROJECTOBJUSED, ZFS_DELEG_NOTE_PROJECTOBJUSED },
5087 	{ ZFS_DELEG_PERM_PROJECTOBJQUOTA, ZFS_DELEG_NOTE_PROJECTOBJQUOTA },
5088 	{ NULL, ZFS_DELEG_NOTE_NONE }
5089 };
5090 
5091 /* permission structure */
5092 typedef struct deleg_perm {
5093 	zfs_deleg_who_type_t	dp_who_type;
5094 	const char		*dp_name;
5095 	boolean_t		dp_local;
5096 	boolean_t		dp_descend;
5097 } deleg_perm_t;
5098 
5099 /* */
5100 typedef struct deleg_perm_node {
5101 	deleg_perm_t		dpn_perm;
5102 
5103 	uu_avl_node_t		dpn_avl_node;
5104 } deleg_perm_node_t;
5105 
5106 typedef struct fs_perm fs_perm_t;
5107 
5108 /* permissions set */
5109 typedef struct who_perm {
5110 	zfs_deleg_who_type_t	who_type;
5111 	const char		*who_name;		/* id */
5112 	char			who_ug_name[256];	/* user/group name */
5113 	fs_perm_t		*who_fsperm;		/* uplink */
5114 
5115 	uu_avl_t		*who_deleg_perm_avl;	/* permissions */
5116 } who_perm_t;
5117 
5118 /* */
5119 typedef struct who_perm_node {
5120 	who_perm_t	who_perm;
5121 	uu_avl_node_t	who_avl_node;
5122 } who_perm_node_t;
5123 
5124 typedef struct fs_perm_set fs_perm_set_t;
5125 /* fs permissions */
5126 struct fs_perm {
5127 	const char		*fsp_name;
5128 
5129 	uu_avl_t		*fsp_sc_avl;	/* sets,create */
5130 	uu_avl_t		*fsp_uge_avl;	/* user,group,everyone */
5131 
5132 	fs_perm_set_t		*fsp_set;	/* uplink */
5133 };
5134 
5135 /* */
5136 typedef struct fs_perm_node {
5137 	fs_perm_t	fspn_fsperm;
5138 	uu_avl_t	*fspn_avl;
5139 
5140 	uu_list_node_t	fspn_list_node;
5141 } fs_perm_node_t;
5142 
5143 /* top level structure */
5144 struct fs_perm_set {
5145 	uu_list_pool_t	*fsps_list_pool;
5146 	uu_list_t	*fsps_list; /* list of fs_perms */
5147 
5148 	uu_avl_pool_t	*fsps_named_set_avl_pool;
5149 	uu_avl_pool_t	*fsps_who_perm_avl_pool;
5150 	uu_avl_pool_t	*fsps_deleg_perm_avl_pool;
5151 };
5152 
5153 static inline const char *
5154 deleg_perm_type(zfs_deleg_note_t note)
5155 {
5156 	/* subcommands */
5157 	switch (note) {
5158 		/* SUBCOMMANDS */
5159 		/* OTHER */
5160 	case ZFS_DELEG_NOTE_GROUPQUOTA:
5161 	case ZFS_DELEG_NOTE_GROUPUSED:
5162 	case ZFS_DELEG_NOTE_USERPROP:
5163 	case ZFS_DELEG_NOTE_USERQUOTA:
5164 	case ZFS_DELEG_NOTE_USERUSED:
5165 	case ZFS_DELEG_NOTE_USEROBJQUOTA:
5166 	case ZFS_DELEG_NOTE_USEROBJUSED:
5167 	case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
5168 	case ZFS_DELEG_NOTE_GROUPOBJUSED:
5169 	case ZFS_DELEG_NOTE_PROJECTUSED:
5170 	case ZFS_DELEG_NOTE_PROJECTQUOTA:
5171 	case ZFS_DELEG_NOTE_PROJECTOBJUSED:
5172 	case ZFS_DELEG_NOTE_PROJECTOBJQUOTA:
5173 		/* other */
5174 		return (gettext("other"));
5175 	default:
5176 		return (gettext("subcommand"));
5177 	}
5178 }
5179 
5180 static int
5181 who_type2weight(zfs_deleg_who_type_t who_type)
5182 {
5183 	int res;
5184 	switch (who_type) {
5185 		case ZFS_DELEG_NAMED_SET_SETS:
5186 		case ZFS_DELEG_NAMED_SET:
5187 			res = 0;
5188 			break;
5189 		case ZFS_DELEG_CREATE_SETS:
5190 		case ZFS_DELEG_CREATE:
5191 			res = 1;
5192 			break;
5193 		case ZFS_DELEG_USER_SETS:
5194 		case ZFS_DELEG_USER:
5195 			res = 2;
5196 			break;
5197 		case ZFS_DELEG_GROUP_SETS:
5198 		case ZFS_DELEG_GROUP:
5199 			res = 3;
5200 			break;
5201 		case ZFS_DELEG_EVERYONE_SETS:
5202 		case ZFS_DELEG_EVERYONE:
5203 			res = 4;
5204 			break;
5205 		default:
5206 			res = -1;
5207 	}
5208 
5209 	return (res);
5210 }
5211 
5212 static int
5213 who_perm_compare(const void *larg, const void *rarg, void *unused)
5214 {
5215 	(void) unused;
5216 	const who_perm_node_t *l = larg;
5217 	const who_perm_node_t *r = rarg;
5218 	zfs_deleg_who_type_t ltype = l->who_perm.who_type;
5219 	zfs_deleg_who_type_t rtype = r->who_perm.who_type;
5220 	int lweight = who_type2weight(ltype);
5221 	int rweight = who_type2weight(rtype);
5222 	int res = lweight - rweight;
5223 	if (res == 0)
5224 		res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
5225 		    ZFS_MAX_DELEG_NAME-1);
5226 
5227 	if (res == 0)
5228 		return (0);
5229 	if (res > 0)
5230 		return (1);
5231 	else
5232 		return (-1);
5233 }
5234 
5235 static int
5236 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
5237 {
5238 	(void) unused;
5239 	const deleg_perm_node_t *l = larg;
5240 	const deleg_perm_node_t *r = rarg;
5241 	int res =  strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
5242 	    ZFS_MAX_DELEG_NAME-1);
5243 
5244 	if (res == 0)
5245 		return (0);
5246 
5247 	if (res > 0)
5248 		return (1);
5249 	else
5250 		return (-1);
5251 }
5252 
5253 static inline void
5254 fs_perm_set_init(fs_perm_set_t *fspset)
5255 {
5256 	memset(fspset, 0, sizeof (fs_perm_set_t));
5257 
5258 	if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
5259 	    sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
5260 	    NULL, UU_DEFAULT)) == NULL)
5261 		nomem();
5262 	if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
5263 	    UU_DEFAULT)) == NULL)
5264 		nomem();
5265 
5266 	if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
5267 	    "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
5268 	    who_perm_node_t, who_avl_node), who_perm_compare,
5269 	    UU_DEFAULT)) == NULL)
5270 		nomem();
5271 
5272 	if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
5273 	    "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
5274 	    who_perm_node_t, who_avl_node), who_perm_compare,
5275 	    UU_DEFAULT)) == NULL)
5276 		nomem();
5277 
5278 	if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
5279 	    "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
5280 	    deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
5281 	    == NULL)
5282 		nomem();
5283 }
5284 
5285 static inline void fs_perm_fini(fs_perm_t *);
5286 static inline void who_perm_fini(who_perm_t *);
5287 
5288 static inline void
5289 fs_perm_set_fini(fs_perm_set_t *fspset)
5290 {
5291 	fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
5292 
5293 	while (node != NULL) {
5294 		fs_perm_node_t *next_node =
5295 		    uu_list_next(fspset->fsps_list, node);
5296 		fs_perm_t *fsperm = &node->fspn_fsperm;
5297 		fs_perm_fini(fsperm);
5298 		uu_list_remove(fspset->fsps_list, node);
5299 		free(node);
5300 		node = next_node;
5301 	}
5302 
5303 	uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
5304 	uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
5305 	uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
5306 }
5307 
5308 static inline void
5309 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
5310     const char *name)
5311 {
5312 	deleg_perm->dp_who_type = type;
5313 	deleg_perm->dp_name = name;
5314 }
5315 
5316 static inline void
5317 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
5318     zfs_deleg_who_type_t type, const char *name)
5319 {
5320 	uu_avl_pool_t	*pool;
5321 	pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
5322 
5323 	memset(who_perm, 0, sizeof (who_perm_t));
5324 
5325 	if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
5326 	    UU_DEFAULT)) == NULL)
5327 		nomem();
5328 
5329 	who_perm->who_type = type;
5330 	who_perm->who_name = name;
5331 	who_perm->who_fsperm = fsperm;
5332 }
5333 
5334 static inline void
5335 who_perm_fini(who_perm_t *who_perm)
5336 {
5337 	deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
5338 
5339 	while (node != NULL) {
5340 		deleg_perm_node_t *next_node =
5341 		    uu_avl_next(who_perm->who_deleg_perm_avl, node);
5342 
5343 		uu_avl_remove(who_perm->who_deleg_perm_avl, node);
5344 		free(node);
5345 		node = next_node;
5346 	}
5347 
5348 	uu_avl_destroy(who_perm->who_deleg_perm_avl);
5349 }
5350 
5351 static inline void
5352 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
5353 {
5354 	uu_avl_pool_t	*nset_pool = fspset->fsps_named_set_avl_pool;
5355 	uu_avl_pool_t	*who_pool = fspset->fsps_who_perm_avl_pool;
5356 
5357 	memset(fsperm, 0, sizeof (fs_perm_t));
5358 
5359 	if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
5360 	    == NULL)
5361 		nomem();
5362 
5363 	if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
5364 	    == NULL)
5365 		nomem();
5366 
5367 	fsperm->fsp_set = fspset;
5368 	fsperm->fsp_name = fsname;
5369 }
5370 
5371 static inline void
5372 fs_perm_fini(fs_perm_t *fsperm)
5373 {
5374 	who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
5375 	while (node != NULL) {
5376 		who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
5377 		    node);
5378 		who_perm_t *who_perm = &node->who_perm;
5379 		who_perm_fini(who_perm);
5380 		uu_avl_remove(fsperm->fsp_sc_avl, node);
5381 		free(node);
5382 		node = next_node;
5383 	}
5384 
5385 	node = uu_avl_first(fsperm->fsp_uge_avl);
5386 	while (node != NULL) {
5387 		who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
5388 		    node);
5389 		who_perm_t *who_perm = &node->who_perm;
5390 		who_perm_fini(who_perm);
5391 		uu_avl_remove(fsperm->fsp_uge_avl, node);
5392 		free(node);
5393 		node = next_node;
5394 	}
5395 
5396 	uu_avl_destroy(fsperm->fsp_sc_avl);
5397 	uu_avl_destroy(fsperm->fsp_uge_avl);
5398 }
5399 
5400 static void
5401 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
5402     zfs_deleg_who_type_t who_type, const char *name, char locality)
5403 {
5404 	uu_avl_index_t idx = 0;
5405 
5406 	deleg_perm_node_t *found_node = NULL;
5407 	deleg_perm_t	*deleg_perm = &node->dpn_perm;
5408 
5409 	deleg_perm_init(deleg_perm, who_type, name);
5410 
5411 	if ((found_node = uu_avl_find(avl, node, NULL, &idx))
5412 	    == NULL)
5413 		uu_avl_insert(avl, node, idx);
5414 	else {
5415 		node = found_node;
5416 		deleg_perm = &node->dpn_perm;
5417 	}
5418 
5419 
5420 	switch (locality) {
5421 	case ZFS_DELEG_LOCAL:
5422 		deleg_perm->dp_local = B_TRUE;
5423 		break;
5424 	case ZFS_DELEG_DESCENDENT:
5425 		deleg_perm->dp_descend = B_TRUE;
5426 		break;
5427 	case ZFS_DELEG_NA:
5428 		break;
5429 	default:
5430 		assert(B_FALSE); /* invalid locality */
5431 	}
5432 }
5433 
5434 static inline int
5435 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
5436 {
5437 	nvpair_t *nvp = NULL;
5438 	fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
5439 	uu_avl_t *avl = who_perm->who_deleg_perm_avl;
5440 	zfs_deleg_who_type_t who_type = who_perm->who_type;
5441 
5442 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5443 		const char *name = nvpair_name(nvp);
5444 		data_type_t type = nvpair_type(nvp);
5445 		uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
5446 		deleg_perm_node_t *node =
5447 		    safe_malloc(sizeof (deleg_perm_node_t));
5448 
5449 		VERIFY(type == DATA_TYPE_BOOLEAN);
5450 
5451 		uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
5452 		set_deleg_perm_node(avl, node, who_type, name, locality);
5453 	}
5454 
5455 	return (0);
5456 }
5457 
5458 static inline int
5459 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
5460 {
5461 	nvpair_t *nvp = NULL;
5462 	fs_perm_set_t *fspset = fsperm->fsp_set;
5463 
5464 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5465 		nvlist_t *nvl2 = NULL;
5466 		const char *name = nvpair_name(nvp);
5467 		uu_avl_t *avl = NULL;
5468 		uu_avl_pool_t *avl_pool = NULL;
5469 		zfs_deleg_who_type_t perm_type = name[0];
5470 		char perm_locality = name[1];
5471 		const char *perm_name = name + 3;
5472 		who_perm_t *who_perm = NULL;
5473 
5474 		assert('$' == name[2]);
5475 
5476 		if (nvpair_value_nvlist(nvp, &nvl2) != 0)
5477 			return (-1);
5478 
5479 		switch (perm_type) {
5480 		case ZFS_DELEG_CREATE:
5481 		case ZFS_DELEG_CREATE_SETS:
5482 		case ZFS_DELEG_NAMED_SET:
5483 		case ZFS_DELEG_NAMED_SET_SETS:
5484 			avl_pool = fspset->fsps_named_set_avl_pool;
5485 			avl = fsperm->fsp_sc_avl;
5486 			break;
5487 		case ZFS_DELEG_USER:
5488 		case ZFS_DELEG_USER_SETS:
5489 		case ZFS_DELEG_GROUP:
5490 		case ZFS_DELEG_GROUP_SETS:
5491 		case ZFS_DELEG_EVERYONE:
5492 		case ZFS_DELEG_EVERYONE_SETS:
5493 			avl_pool = fspset->fsps_who_perm_avl_pool;
5494 			avl = fsperm->fsp_uge_avl;
5495 			break;
5496 
5497 		default:
5498 			assert(!"unhandled zfs_deleg_who_type_t");
5499 		}
5500 
5501 		who_perm_node_t *found_node = NULL;
5502 		who_perm_node_t *node = safe_malloc(
5503 		    sizeof (who_perm_node_t));
5504 		who_perm = &node->who_perm;
5505 		uu_avl_index_t idx = 0;
5506 
5507 		uu_avl_node_init(node, &node->who_avl_node, avl_pool);
5508 		who_perm_init(who_perm, fsperm, perm_type, perm_name);
5509 
5510 		if ((found_node = uu_avl_find(avl, node, NULL, &idx))
5511 		    == NULL) {
5512 			if (avl == fsperm->fsp_uge_avl) {
5513 				uid_t rid = 0;
5514 				struct passwd *p = NULL;
5515 				struct group *g = NULL;
5516 				const char *nice_name = NULL;
5517 
5518 				switch (perm_type) {
5519 				case ZFS_DELEG_USER_SETS:
5520 				case ZFS_DELEG_USER:
5521 					rid = atoi(perm_name);
5522 					p = getpwuid(rid);
5523 					if (p)
5524 						nice_name = p->pw_name;
5525 					break;
5526 				case ZFS_DELEG_GROUP_SETS:
5527 				case ZFS_DELEG_GROUP:
5528 					rid = atoi(perm_name);
5529 					g = getgrgid(rid);
5530 					if (g)
5531 						nice_name = g->gr_name;
5532 					break;
5533 
5534 				default:
5535 					break;
5536 				}
5537 
5538 				if (nice_name != NULL) {
5539 					(void) strlcpy(
5540 					    node->who_perm.who_ug_name,
5541 					    nice_name, 256);
5542 				} else {
5543 					/* User or group unknown */
5544 					(void) snprintf(
5545 					    node->who_perm.who_ug_name,
5546 					    sizeof (node->who_perm.who_ug_name),
5547 					    "(unknown: %d)", rid);
5548 				}
5549 			}
5550 
5551 			uu_avl_insert(avl, node, idx);
5552 		} else {
5553 			node = found_node;
5554 			who_perm = &node->who_perm;
5555 		}
5556 
5557 		assert(who_perm != NULL);
5558 		(void) parse_who_perm(who_perm, nvl2, perm_locality);
5559 	}
5560 
5561 	return (0);
5562 }
5563 
5564 static inline int
5565 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
5566 {
5567 	nvpair_t *nvp = NULL;
5568 	uu_avl_index_t idx = 0;
5569 
5570 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5571 		nvlist_t *nvl2 = NULL;
5572 		const char *fsname = nvpair_name(nvp);
5573 		data_type_t type = nvpair_type(nvp);
5574 		fs_perm_t *fsperm = NULL;
5575 		fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
5576 
5577 		fsperm = &node->fspn_fsperm;
5578 
5579 		VERIFY(DATA_TYPE_NVLIST == type);
5580 
5581 		uu_list_node_init(node, &node->fspn_list_node,
5582 		    fspset->fsps_list_pool);
5583 
5584 		idx = uu_list_numnodes(fspset->fsps_list);
5585 		fs_perm_init(fsperm, fspset, fsname);
5586 
5587 		if (nvpair_value_nvlist(nvp, &nvl2) != 0)
5588 			return (-1);
5589 
5590 		(void) parse_fs_perm(fsperm, nvl2);
5591 
5592 		uu_list_insert(fspset->fsps_list, node, idx);
5593 	}
5594 
5595 	return (0);
5596 }
5597 
5598 static inline const char *
5599 deleg_perm_comment(zfs_deleg_note_t note)
5600 {
5601 	const char *str = "";
5602 
5603 	/* subcommands */
5604 	switch (note) {
5605 		/* SUBCOMMANDS */
5606 	case ZFS_DELEG_NOTE_ALLOW:
5607 		str = gettext("Must also have the permission that is being"
5608 		    "\n\t\t\t\tallowed");
5609 		break;
5610 	case ZFS_DELEG_NOTE_CLONE:
5611 		str = gettext("Must also have the 'create' ability and 'mount'"
5612 		    "\n\t\t\t\tability in the origin file system");
5613 		break;
5614 	case ZFS_DELEG_NOTE_CREATE:
5615 		str = gettext("Must also have the 'mount' ability");
5616 		break;
5617 	case ZFS_DELEG_NOTE_DESTROY:
5618 		str = gettext("Must also have the 'mount' ability");
5619 		break;
5620 	case ZFS_DELEG_NOTE_DIFF:
5621 		str = gettext("Allows lookup of paths within a dataset;"
5622 		    "\n\t\t\t\tgiven an object number. Ordinary users need this"
5623 		    "\n\t\t\t\tin order to use zfs diff");
5624 		break;
5625 	case ZFS_DELEG_NOTE_HOLD:
5626 		str = gettext("Allows adding a user hold to a snapshot");
5627 		break;
5628 	case ZFS_DELEG_NOTE_MOUNT:
5629 		str = gettext("Allows mount/umount of ZFS datasets");
5630 		break;
5631 	case ZFS_DELEG_NOTE_PROMOTE:
5632 		str = gettext("Must also have the 'mount'\n\t\t\t\tand"
5633 		    " 'promote' ability in the origin file system");
5634 		break;
5635 	case ZFS_DELEG_NOTE_RECEIVE:
5636 		str = gettext("Must also have the 'mount' and 'create'"
5637 		    " ability");
5638 		break;
5639 	case ZFS_DELEG_NOTE_RELEASE:
5640 		str = gettext("Allows releasing a user hold which\n\t\t\t\t"
5641 		    "might destroy the snapshot");
5642 		break;
5643 	case ZFS_DELEG_NOTE_RENAME:
5644 		str = gettext("Must also have the 'mount' and 'create'"
5645 		    "\n\t\t\t\tability in the new parent");
5646 		break;
5647 	case ZFS_DELEG_NOTE_ROLLBACK:
5648 		str = gettext("");
5649 		break;
5650 	case ZFS_DELEG_NOTE_SEND:
5651 		str = gettext("");
5652 		break;
5653 	case ZFS_DELEG_NOTE_SHARE:
5654 		str = gettext("Allows sharing file systems over NFS or SMB"
5655 		    "\n\t\t\t\tprotocols");
5656 		break;
5657 	case ZFS_DELEG_NOTE_SNAPSHOT:
5658 		str = gettext("");
5659 		break;
5660 	case ZFS_DELEG_NOTE_LOAD_KEY:
5661 		str = gettext("Allows loading or unloading an encryption key");
5662 		break;
5663 	case ZFS_DELEG_NOTE_CHANGE_KEY:
5664 		str = gettext("Allows changing or adding an encryption key");
5665 		break;
5666 /*
5667  *	case ZFS_DELEG_NOTE_VSCAN:
5668  *		str = gettext("");
5669  *		break;
5670  */
5671 		/* OTHER */
5672 	case ZFS_DELEG_NOTE_GROUPQUOTA:
5673 		str = gettext("Allows accessing any groupquota@... property");
5674 		break;
5675 	case ZFS_DELEG_NOTE_GROUPUSED:
5676 		str = gettext("Allows reading any groupused@... property");
5677 		break;
5678 	case ZFS_DELEG_NOTE_USERPROP:
5679 		str = gettext("Allows changing any user property");
5680 		break;
5681 	case ZFS_DELEG_NOTE_USERQUOTA:
5682 		str = gettext("Allows accessing any userquota@... property");
5683 		break;
5684 	case ZFS_DELEG_NOTE_USERUSED:
5685 		str = gettext("Allows reading any userused@... property");
5686 		break;
5687 	case ZFS_DELEG_NOTE_USEROBJQUOTA:
5688 		str = gettext("Allows accessing any userobjquota@... property");
5689 		break;
5690 	case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
5691 		str = gettext("Allows accessing any \n\t\t\t\t"
5692 		    "groupobjquota@... property");
5693 		break;
5694 	case ZFS_DELEG_NOTE_GROUPOBJUSED:
5695 		str = gettext("Allows reading any groupobjused@... property");
5696 		break;
5697 	case ZFS_DELEG_NOTE_USEROBJUSED:
5698 		str = gettext("Allows reading any userobjused@... property");
5699 		break;
5700 	case ZFS_DELEG_NOTE_PROJECTQUOTA:
5701 		str = gettext("Allows accessing any projectquota@... property");
5702 		break;
5703 	case ZFS_DELEG_NOTE_PROJECTOBJQUOTA:
5704 		str = gettext("Allows accessing any \n\t\t\t\t"
5705 		    "projectobjquota@... property");
5706 		break;
5707 	case ZFS_DELEG_NOTE_PROJECTUSED:
5708 		str = gettext("Allows reading any projectused@... property");
5709 		break;
5710 	case ZFS_DELEG_NOTE_PROJECTOBJUSED:
5711 		str = gettext("Allows accessing any \n\t\t\t\t"
5712 		    "projectobjused@... property");
5713 		break;
5714 		/* other */
5715 	default:
5716 		str = "";
5717 	}
5718 
5719 	return (str);
5720 }
5721 
5722 struct allow_opts {
5723 	boolean_t local;
5724 	boolean_t descend;
5725 	boolean_t user;
5726 	boolean_t group;
5727 	boolean_t everyone;
5728 	boolean_t create;
5729 	boolean_t set;
5730 	boolean_t recursive; /* unallow only */
5731 	boolean_t prt_usage;
5732 
5733 	boolean_t prt_perms;
5734 	char *who;
5735 	char *perms;
5736 	const char *dataset;
5737 };
5738 
5739 static inline int
5740 prop_cmp(const void *a, const void *b)
5741 {
5742 	const char *str1 = *(const char **)a;
5743 	const char *str2 = *(const char **)b;
5744 	return (strcmp(str1, str2));
5745 }
5746 
5747 static void
5748 allow_usage(boolean_t un, boolean_t requested, const char *msg)
5749 {
5750 	const char *opt_desc[] = {
5751 		"-h", gettext("show this help message and exit"),
5752 		"-l", gettext("set permission locally"),
5753 		"-d", gettext("set permission for descents"),
5754 		"-u", gettext("set permission for user"),
5755 		"-g", gettext("set permission for group"),
5756 		"-e", gettext("set permission for everyone"),
5757 		"-c", gettext("set create time permission"),
5758 		"-s", gettext("define permission set"),
5759 		/* unallow only */
5760 		"-r", gettext("remove permissions recursively"),
5761 	};
5762 	size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
5763 	size_t allow_size = unallow_size - 2;
5764 	const char *props[ZFS_NUM_PROPS];
5765 	int i;
5766 	size_t count = 0;
5767 	FILE *fp = requested ? stdout : stderr;
5768 	zprop_desc_t *pdtbl = zfs_prop_get_table();
5769 	const char *fmt = gettext("%-16s %-14s\t%s\n");
5770 
5771 	(void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
5772 	    HELP_ALLOW));
5773 	(void) fprintf(fp, gettext("Options:\n"));
5774 	for (i = 0; i < (un ? unallow_size : allow_size); i += 2) {
5775 		const char *opt = opt_desc[i];
5776 		const char *optdsc = opt_desc[i + 1];
5777 		(void) fprintf(fp, gettext("  %-10s  %s\n"), opt, optdsc);
5778 	}
5779 
5780 	(void) fprintf(fp, gettext("\nThe following permissions are "
5781 	    "supported:\n\n"));
5782 	(void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
5783 	    gettext("NOTES"));
5784 	for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
5785 		const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
5786 		zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
5787 		const char *perm_type = deleg_perm_type(perm_note);
5788 		const char *perm_comment = deleg_perm_comment(perm_note);
5789 		(void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
5790 	}
5791 
5792 	for (i = 0; i < ZFS_NUM_PROPS; i++) {
5793 		zprop_desc_t *pd = &pdtbl[i];
5794 		if (pd->pd_visible != B_TRUE)
5795 			continue;
5796 
5797 		if (pd->pd_attr == PROP_READONLY)
5798 			continue;
5799 
5800 		props[count++] = pd->pd_name;
5801 	}
5802 	props[count] = NULL;
5803 
5804 	qsort(props, count, sizeof (char *), prop_cmp);
5805 
5806 	for (i = 0; i < count; i++)
5807 		(void) fprintf(fp, fmt, props[i], gettext("property"), "");
5808 
5809 	if (msg != NULL)
5810 		(void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
5811 
5812 	exit(requested ? 0 : 2);
5813 }
5814 
5815 static inline const char *
5816 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
5817     char **permsp)
5818 {
5819 	if (un && argc == expected_argc - 1)
5820 		*permsp = NULL;
5821 	else if (argc == expected_argc)
5822 		*permsp = argv[argc - 2];
5823 	else
5824 		allow_usage(un, B_FALSE,
5825 		    gettext("wrong number of parameters\n"));
5826 
5827 	return (argv[argc - 1]);
5828 }
5829 
5830 static void
5831 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
5832 {
5833 	int uge_sum = opts->user + opts->group + opts->everyone;
5834 	int csuge_sum = opts->create + opts->set + uge_sum;
5835 	int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
5836 	int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
5837 
5838 	if (uge_sum > 1)
5839 		allow_usage(un, B_FALSE,
5840 		    gettext("-u, -g, and -e are mutually exclusive\n"));
5841 
5842 	if (opts->prt_usage) {
5843 		if (argc == 0 && all_sum == 0)
5844 			allow_usage(un, B_TRUE, NULL);
5845 		else
5846 			usage(B_FALSE);
5847 	}
5848 
5849 	if (opts->set) {
5850 		if (csuge_sum > 1)
5851 			allow_usage(un, B_FALSE,
5852 			    gettext("invalid options combined with -s\n"));
5853 
5854 		opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
5855 		if (argv[0][0] != '@')
5856 			allow_usage(un, B_FALSE,
5857 			    gettext("invalid set name: missing '@' prefix\n"));
5858 		opts->who = argv[0];
5859 	} else if (opts->create) {
5860 		if (ldcsuge_sum > 1)
5861 			allow_usage(un, B_FALSE,
5862 			    gettext("invalid options combined with -c\n"));
5863 		opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5864 	} else if (opts->everyone) {
5865 		if (csuge_sum > 1)
5866 			allow_usage(un, B_FALSE,
5867 			    gettext("invalid options combined with -e\n"));
5868 		opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5869 	} else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
5870 	    == 0) {
5871 		opts->everyone = B_TRUE;
5872 		argc--;
5873 		argv++;
5874 		opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5875 	} else if (argc == 1 && !un) {
5876 		opts->prt_perms = B_TRUE;
5877 		opts->dataset = argv[argc-1];
5878 	} else {
5879 		opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
5880 		opts->who = argv[0];
5881 	}
5882 
5883 	if (!opts->local && !opts->descend) {
5884 		opts->local = B_TRUE;
5885 		opts->descend = B_TRUE;
5886 	}
5887 }
5888 
5889 static void
5890 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
5891     const char *who, char *perms, nvlist_t *top_nvl)
5892 {
5893 	int i;
5894 	char ld[2] = { '\0', '\0' };
5895 	char who_buf[MAXNAMELEN + 32];
5896 	char base_type = '\0';
5897 	char set_type = '\0';
5898 	nvlist_t *base_nvl = NULL;
5899 	nvlist_t *set_nvl = NULL;
5900 	nvlist_t *nvl;
5901 
5902 	if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
5903 		nomem();
5904 	if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) !=  0)
5905 		nomem();
5906 
5907 	switch (type) {
5908 	case ZFS_DELEG_NAMED_SET_SETS:
5909 	case ZFS_DELEG_NAMED_SET:
5910 		set_type = ZFS_DELEG_NAMED_SET_SETS;
5911 		base_type = ZFS_DELEG_NAMED_SET;
5912 		ld[0] = ZFS_DELEG_NA;
5913 		break;
5914 	case ZFS_DELEG_CREATE_SETS:
5915 	case ZFS_DELEG_CREATE:
5916 		set_type = ZFS_DELEG_CREATE_SETS;
5917 		base_type = ZFS_DELEG_CREATE;
5918 		ld[0] = ZFS_DELEG_NA;
5919 		break;
5920 	case ZFS_DELEG_USER_SETS:
5921 	case ZFS_DELEG_USER:
5922 		set_type = ZFS_DELEG_USER_SETS;
5923 		base_type = ZFS_DELEG_USER;
5924 		if (local)
5925 			ld[0] = ZFS_DELEG_LOCAL;
5926 		if (descend)
5927 			ld[1] = ZFS_DELEG_DESCENDENT;
5928 		break;
5929 	case ZFS_DELEG_GROUP_SETS:
5930 	case ZFS_DELEG_GROUP:
5931 		set_type = ZFS_DELEG_GROUP_SETS;
5932 		base_type = ZFS_DELEG_GROUP;
5933 		if (local)
5934 			ld[0] = ZFS_DELEG_LOCAL;
5935 		if (descend)
5936 			ld[1] = ZFS_DELEG_DESCENDENT;
5937 		break;
5938 	case ZFS_DELEG_EVERYONE_SETS:
5939 	case ZFS_DELEG_EVERYONE:
5940 		set_type = ZFS_DELEG_EVERYONE_SETS;
5941 		base_type = ZFS_DELEG_EVERYONE;
5942 		if (local)
5943 			ld[0] = ZFS_DELEG_LOCAL;
5944 		if (descend)
5945 			ld[1] = ZFS_DELEG_DESCENDENT;
5946 		break;
5947 
5948 	default:
5949 		assert(set_type != '\0' && base_type != '\0');
5950 	}
5951 
5952 	if (perms != NULL) {
5953 		char *curr = perms;
5954 		char *end = curr + strlen(perms);
5955 
5956 		while (curr < end) {
5957 			char *delim = strchr(curr, ',');
5958 			if (delim == NULL)
5959 				delim = end;
5960 			else
5961 				*delim = '\0';
5962 
5963 			if (curr[0] == '@')
5964 				nvl = set_nvl;
5965 			else
5966 				nvl = base_nvl;
5967 
5968 			(void) nvlist_add_boolean(nvl, curr);
5969 			if (delim != end)
5970 				*delim = ',';
5971 			curr = delim + 1;
5972 		}
5973 
5974 		for (i = 0; i < 2; i++) {
5975 			char locality = ld[i];
5976 			if (locality == 0)
5977 				continue;
5978 
5979 			if (!nvlist_empty(base_nvl)) {
5980 				if (who != NULL)
5981 					(void) snprintf(who_buf,
5982 					    sizeof (who_buf), "%c%c$%s",
5983 					    base_type, locality, who);
5984 				else
5985 					(void) snprintf(who_buf,
5986 					    sizeof (who_buf), "%c%c$",
5987 					    base_type, locality);
5988 
5989 				(void) nvlist_add_nvlist(top_nvl, who_buf,
5990 				    base_nvl);
5991 			}
5992 
5993 
5994 			if (!nvlist_empty(set_nvl)) {
5995 				if (who != NULL)
5996 					(void) snprintf(who_buf,
5997 					    sizeof (who_buf), "%c%c$%s",
5998 					    set_type, locality, who);
5999 				else
6000 					(void) snprintf(who_buf,
6001 					    sizeof (who_buf), "%c%c$",
6002 					    set_type, locality);
6003 
6004 				(void) nvlist_add_nvlist(top_nvl, who_buf,
6005 				    set_nvl);
6006 			}
6007 		}
6008 	} else {
6009 		for (i = 0; i < 2; i++) {
6010 			char locality = ld[i];
6011 			if (locality == 0)
6012 				continue;
6013 
6014 			if (who != NULL)
6015 				(void) snprintf(who_buf, sizeof (who_buf),
6016 				    "%c%c$%s", base_type, locality, who);
6017 			else
6018 				(void) snprintf(who_buf, sizeof (who_buf),
6019 				    "%c%c$", base_type, locality);
6020 			(void) nvlist_add_boolean(top_nvl, who_buf);
6021 
6022 			if (who != NULL)
6023 				(void) snprintf(who_buf, sizeof (who_buf),
6024 				    "%c%c$%s", set_type, locality, who);
6025 			else
6026 				(void) snprintf(who_buf, sizeof (who_buf),
6027 				    "%c%c$", set_type, locality);
6028 			(void) nvlist_add_boolean(top_nvl, who_buf);
6029 		}
6030 	}
6031 }
6032 
6033 static int
6034 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
6035 {
6036 	if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
6037 		nomem();
6038 
6039 	if (opts->set) {
6040 		store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
6041 		    opts->descend, opts->who, opts->perms, *nvlp);
6042 	} else if (opts->create) {
6043 		store_allow_perm(ZFS_DELEG_CREATE, opts->local,
6044 		    opts->descend, NULL, opts->perms, *nvlp);
6045 	} else if (opts->everyone) {
6046 		store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
6047 		    opts->descend, NULL, opts->perms, *nvlp);
6048 	} else {
6049 		char *curr = opts->who;
6050 		char *end = curr + strlen(curr);
6051 
6052 		while (curr < end) {
6053 			const char *who;
6054 			zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
6055 			char *endch;
6056 			char *delim = strchr(curr, ',');
6057 			char errbuf[256];
6058 			char id[64];
6059 			struct passwd *p = NULL;
6060 			struct group *g = NULL;
6061 
6062 			uid_t rid;
6063 			if (delim == NULL)
6064 				delim = end;
6065 			else
6066 				*delim = '\0';
6067 
6068 			rid = (uid_t)strtol(curr, &endch, 0);
6069 			if (opts->user) {
6070 				who_type = ZFS_DELEG_USER;
6071 				if (*endch != '\0')
6072 					p = getpwnam(curr);
6073 				else
6074 					p = getpwuid(rid);
6075 
6076 				if (p != NULL)
6077 					rid = p->pw_uid;
6078 				else if (*endch != '\0') {
6079 					(void) snprintf(errbuf, sizeof (errbuf),
6080 					    gettext("invalid user %s\n"), curr);
6081 					allow_usage(un, B_TRUE, errbuf);
6082 				}
6083 			} else if (opts->group) {
6084 				who_type = ZFS_DELEG_GROUP;
6085 				if (*endch != '\0')
6086 					g = getgrnam(curr);
6087 				else
6088 					g = getgrgid(rid);
6089 
6090 				if (g != NULL)
6091 					rid = g->gr_gid;
6092 				else if (*endch != '\0') {
6093 					(void) snprintf(errbuf, sizeof (errbuf),
6094 					    gettext("invalid group %s\n"),
6095 					    curr);
6096 					allow_usage(un, B_TRUE, errbuf);
6097 				}
6098 			} else {
6099 				if (*endch != '\0') {
6100 					p = getpwnam(curr);
6101 				} else {
6102 					p = getpwuid(rid);
6103 				}
6104 
6105 				if (p == NULL) {
6106 					if (*endch != '\0') {
6107 						g = getgrnam(curr);
6108 					} else {
6109 						g = getgrgid(rid);
6110 					}
6111 				}
6112 
6113 				if (p != NULL) {
6114 					who_type = ZFS_DELEG_USER;
6115 					rid = p->pw_uid;
6116 				} else if (g != NULL) {
6117 					who_type = ZFS_DELEG_GROUP;
6118 					rid = g->gr_gid;
6119 				} else {
6120 					(void) snprintf(errbuf, sizeof (errbuf),
6121 					    gettext("invalid user/group %s\n"),
6122 					    curr);
6123 					allow_usage(un, B_TRUE, errbuf);
6124 				}
6125 			}
6126 
6127 			(void) sprintf(id, "%u", rid);
6128 			who = id;
6129 
6130 			store_allow_perm(who_type, opts->local,
6131 			    opts->descend, who, opts->perms, *nvlp);
6132 			curr = delim + 1;
6133 		}
6134 	}
6135 
6136 	return (0);
6137 }
6138 
6139 static void
6140 print_set_creat_perms(uu_avl_t *who_avl)
6141 {
6142 	const char *sc_title[] = {
6143 		gettext("Permission sets:\n"),
6144 		gettext("Create time permissions:\n"),
6145 		NULL
6146 	};
6147 	who_perm_node_t *who_node = NULL;
6148 	int prev_weight = -1;
6149 
6150 	for (who_node = uu_avl_first(who_avl); who_node != NULL;
6151 	    who_node = uu_avl_next(who_avl, who_node)) {
6152 		uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
6153 		zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
6154 		const char *who_name = who_node->who_perm.who_name;
6155 		int weight = who_type2weight(who_type);
6156 		boolean_t first = B_TRUE;
6157 		deleg_perm_node_t *deleg_node;
6158 
6159 		if (prev_weight != weight) {
6160 			(void) printf("%s", sc_title[weight]);
6161 			prev_weight = weight;
6162 		}
6163 
6164 		if (who_name == NULL || strnlen(who_name, 1) == 0)
6165 			(void) printf("\t");
6166 		else
6167 			(void) printf("\t%s ", who_name);
6168 
6169 		for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
6170 		    deleg_node = uu_avl_next(avl, deleg_node)) {
6171 			if (first) {
6172 				(void) printf("%s",
6173 				    deleg_node->dpn_perm.dp_name);
6174 				first = B_FALSE;
6175 			} else
6176 				(void) printf(",%s",
6177 				    deleg_node->dpn_perm.dp_name);
6178 		}
6179 
6180 		(void) printf("\n");
6181 	}
6182 }
6183 
6184 static void
6185 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
6186     const char *title)
6187 {
6188 	who_perm_node_t *who_node = NULL;
6189 	boolean_t prt_title = B_TRUE;
6190 	uu_avl_walk_t *walk;
6191 
6192 	if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
6193 		nomem();
6194 
6195 	while ((who_node = uu_avl_walk_next(walk)) != NULL) {
6196 		const char *who_name = who_node->who_perm.who_name;
6197 		const char *nice_who_name = who_node->who_perm.who_ug_name;
6198 		uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
6199 		zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
6200 		char delim = ' ';
6201 		deleg_perm_node_t *deleg_node;
6202 		boolean_t prt_who = B_TRUE;
6203 
6204 		for (deleg_node = uu_avl_first(avl);
6205 		    deleg_node != NULL;
6206 		    deleg_node = uu_avl_next(avl, deleg_node)) {
6207 			if (local != deleg_node->dpn_perm.dp_local ||
6208 			    descend != deleg_node->dpn_perm.dp_descend)
6209 				continue;
6210 
6211 			if (prt_who) {
6212 				const char *who = NULL;
6213 				if (prt_title) {
6214 					prt_title = B_FALSE;
6215 					(void) printf("%s", title);
6216 				}
6217 
6218 				switch (who_type) {
6219 				case ZFS_DELEG_USER_SETS:
6220 				case ZFS_DELEG_USER:
6221 					who = gettext("user");
6222 					if (nice_who_name)
6223 						who_name  = nice_who_name;
6224 					break;
6225 				case ZFS_DELEG_GROUP_SETS:
6226 				case ZFS_DELEG_GROUP:
6227 					who = gettext("group");
6228 					if (nice_who_name)
6229 						who_name  = nice_who_name;
6230 					break;
6231 				case ZFS_DELEG_EVERYONE_SETS:
6232 				case ZFS_DELEG_EVERYONE:
6233 					who = gettext("everyone");
6234 					who_name = NULL;
6235 					break;
6236 
6237 				default:
6238 					assert(who != NULL);
6239 				}
6240 
6241 				prt_who = B_FALSE;
6242 				if (who_name == NULL)
6243 					(void) printf("\t%s", who);
6244 				else
6245 					(void) printf("\t%s %s", who, who_name);
6246 			}
6247 
6248 			(void) printf("%c%s", delim,
6249 			    deleg_node->dpn_perm.dp_name);
6250 			delim = ',';
6251 		}
6252 
6253 		if (!prt_who)
6254 			(void) printf("\n");
6255 	}
6256 
6257 	uu_avl_walk_end(walk);
6258 }
6259 
6260 static void
6261 print_fs_perms(fs_perm_set_t *fspset)
6262 {
6263 	fs_perm_node_t *node = NULL;
6264 	char buf[MAXNAMELEN + 32];
6265 	const char *dsname = buf;
6266 
6267 	for (node = uu_list_first(fspset->fsps_list); node != NULL;
6268 	    node = uu_list_next(fspset->fsps_list, node)) {
6269 		uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
6270 		uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
6271 		int left = 0;
6272 
6273 		(void) snprintf(buf, sizeof (buf),
6274 		    gettext("---- Permissions on %s "),
6275 		    node->fspn_fsperm.fsp_name);
6276 		(void) printf("%s", dsname);
6277 		left = 70 - strlen(buf);
6278 		while (left-- > 0)
6279 			(void) printf("-");
6280 		(void) printf("\n");
6281 
6282 		print_set_creat_perms(sc_avl);
6283 		print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
6284 		    gettext("Local permissions:\n"));
6285 		print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
6286 		    gettext("Descendent permissions:\n"));
6287 		print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
6288 		    gettext("Local+Descendent permissions:\n"));
6289 	}
6290 }
6291 
6292 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
6293 
6294 struct deleg_perms {
6295 	boolean_t un;
6296 	nvlist_t *nvl;
6297 };
6298 
6299 static int
6300 set_deleg_perms(zfs_handle_t *zhp, void *data)
6301 {
6302 	struct deleg_perms *perms = (struct deleg_perms *)data;
6303 	zfs_type_t zfs_type = zfs_get_type(zhp);
6304 
6305 	if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
6306 		return (0);
6307 
6308 	return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
6309 }
6310 
6311 static int
6312 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
6313 {
6314 	zfs_handle_t *zhp;
6315 	nvlist_t *perm_nvl = NULL;
6316 	nvlist_t *update_perm_nvl = NULL;
6317 	int error = 1;
6318 	int c;
6319 	struct allow_opts opts = { 0 };
6320 
6321 	const char *optstr = un ? "ldugecsrh" : "ldugecsh";
6322 
6323 	/* check opts */
6324 	while ((c = getopt(argc, argv, optstr)) != -1) {
6325 		switch (c) {
6326 		case 'l':
6327 			opts.local = B_TRUE;
6328 			break;
6329 		case 'd':
6330 			opts.descend = B_TRUE;
6331 			break;
6332 		case 'u':
6333 			opts.user = B_TRUE;
6334 			break;
6335 		case 'g':
6336 			opts.group = B_TRUE;
6337 			break;
6338 		case 'e':
6339 			opts.everyone = B_TRUE;
6340 			break;
6341 		case 's':
6342 			opts.set = B_TRUE;
6343 			break;
6344 		case 'c':
6345 			opts.create = B_TRUE;
6346 			break;
6347 		case 'r':
6348 			opts.recursive = B_TRUE;
6349 			break;
6350 		case ':':
6351 			(void) fprintf(stderr, gettext("missing argument for "
6352 			    "'%c' option\n"), optopt);
6353 			usage(B_FALSE);
6354 			break;
6355 		case 'h':
6356 			opts.prt_usage = B_TRUE;
6357 			break;
6358 		case '?':
6359 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6360 			    optopt);
6361 			usage(B_FALSE);
6362 		}
6363 	}
6364 
6365 	argc -= optind;
6366 	argv += optind;
6367 
6368 	/* check arguments */
6369 	parse_allow_args(argc, argv, un, &opts);
6370 
6371 	/* try to open the dataset */
6372 	if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
6373 	    ZFS_TYPE_VOLUME)) == NULL) {
6374 		(void) fprintf(stderr, "Failed to open dataset: %s\n",
6375 		    opts.dataset);
6376 		return (-1);
6377 	}
6378 
6379 	if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
6380 		goto cleanup2;
6381 
6382 	fs_perm_set_init(&fs_perm_set);
6383 	if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
6384 		(void) fprintf(stderr, "Failed to parse fsacl permissions\n");
6385 		goto cleanup1;
6386 	}
6387 
6388 	if (opts.prt_perms)
6389 		print_fs_perms(&fs_perm_set);
6390 	else {
6391 		(void) construct_fsacl_list(un, &opts, &update_perm_nvl);
6392 		if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
6393 			goto cleanup0;
6394 
6395 		if (un && opts.recursive) {
6396 			struct deleg_perms data = { un, update_perm_nvl };
6397 			if (zfs_iter_filesystems_v2(zhp, 0, set_deleg_perms,
6398 			    &data) != 0)
6399 				goto cleanup0;
6400 		}
6401 	}
6402 
6403 	error = 0;
6404 
6405 cleanup0:
6406 	nvlist_free(perm_nvl);
6407 	nvlist_free(update_perm_nvl);
6408 cleanup1:
6409 	fs_perm_set_fini(&fs_perm_set);
6410 cleanup2:
6411 	zfs_close(zhp);
6412 
6413 	return (error);
6414 }
6415 
6416 static int
6417 zfs_do_allow(int argc, char **argv)
6418 {
6419 	return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
6420 }
6421 
6422 static int
6423 zfs_do_unallow(int argc, char **argv)
6424 {
6425 	return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
6426 }
6427 
6428 static int
6429 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
6430 {
6431 	int errors = 0;
6432 	int i;
6433 	const char *tag;
6434 	boolean_t recursive = B_FALSE;
6435 	const char *opts = holding ? "rt" : "r";
6436 	int c;
6437 
6438 	/* check options */
6439 	while ((c = getopt(argc, argv, opts)) != -1) {
6440 		switch (c) {
6441 		case 'r':
6442 			recursive = B_TRUE;
6443 			break;
6444 		case '?':
6445 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6446 			    optopt);
6447 			usage(B_FALSE);
6448 		}
6449 	}
6450 
6451 	argc -= optind;
6452 	argv += optind;
6453 
6454 	/* check number of arguments */
6455 	if (argc < 2)
6456 		usage(B_FALSE);
6457 
6458 	tag = argv[0];
6459 	--argc;
6460 	++argv;
6461 
6462 	if (holding && tag[0] == '.') {
6463 		/* tags starting with '.' are reserved for libzfs */
6464 		(void) fprintf(stderr, gettext("tag may not start with '.'\n"));
6465 		usage(B_FALSE);
6466 	}
6467 
6468 	for (i = 0; i < argc; ++i) {
6469 		zfs_handle_t *zhp;
6470 		char parent[ZFS_MAX_DATASET_NAME_LEN];
6471 		const char *delim;
6472 		char *path = argv[i];
6473 
6474 		delim = strchr(path, '@');
6475 		if (delim == NULL) {
6476 			(void) fprintf(stderr,
6477 			    gettext("'%s' is not a snapshot\n"), path);
6478 			++errors;
6479 			continue;
6480 		}
6481 		(void) strlcpy(parent, path, MIN(sizeof (parent),
6482 		    delim - path + 1));
6483 
6484 		zhp = zfs_open(g_zfs, parent,
6485 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
6486 		if (zhp == NULL) {
6487 			++errors;
6488 			continue;
6489 		}
6490 		if (holding) {
6491 			if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
6492 				++errors;
6493 		} else {
6494 			if (zfs_release(zhp, delim+1, tag, recursive) != 0)
6495 				++errors;
6496 		}
6497 		zfs_close(zhp);
6498 	}
6499 
6500 	return (errors != 0);
6501 }
6502 
6503 /*
6504  * zfs hold [-r] [-t] <tag> <snap> ...
6505  *
6506  *	-r	Recursively hold
6507  *
6508  * Apply a user-hold with the given tag to the list of snapshots.
6509  */
6510 static int
6511 zfs_do_hold(int argc, char **argv)
6512 {
6513 	return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
6514 }
6515 
6516 /*
6517  * zfs release [-r] <tag> <snap> ...
6518  *
6519  *	-r	Recursively release
6520  *
6521  * Release a user-hold with the given tag from the list of snapshots.
6522  */
6523 static int
6524 zfs_do_release(int argc, char **argv)
6525 {
6526 	return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
6527 }
6528 
6529 typedef struct holds_cbdata {
6530 	boolean_t	cb_recursive;
6531 	const char	*cb_snapname;
6532 	nvlist_t	**cb_nvlp;
6533 	size_t		cb_max_namelen;
6534 	size_t		cb_max_taglen;
6535 } holds_cbdata_t;
6536 
6537 #define	STRFTIME_FMT_STR "%a %b %e %H:%M %Y"
6538 #define	DATETIME_BUF_LEN (32)
6539 /*
6540  *
6541  */
6542 static void
6543 print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl,
6544     boolean_t parsable)
6545 {
6546 	int i;
6547 	nvpair_t *nvp = NULL;
6548 	const char *const hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
6549 	const char *col;
6550 
6551 	if (!scripted) {
6552 		for (i = 0; i < 3; i++) {
6553 			col = gettext(hdr_cols[i]);
6554 			if (i < 2)
6555 				(void) printf("%-*s  ", i ? tagwidth : nwidth,
6556 				    col);
6557 			else
6558 				(void) printf("%s\n", col);
6559 		}
6560 	}
6561 
6562 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
6563 		const char *zname = nvpair_name(nvp);
6564 		nvlist_t *nvl2;
6565 		nvpair_t *nvp2 = NULL;
6566 		(void) nvpair_value_nvlist(nvp, &nvl2);
6567 		while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
6568 			char tsbuf[DATETIME_BUF_LEN];
6569 			const char *tagname = nvpair_name(nvp2);
6570 			uint64_t val = 0;
6571 			time_t time;
6572 			struct tm t;
6573 
6574 			(void) nvpair_value_uint64(nvp2, &val);
6575 			time = (time_t)val;
6576 			(void) localtime_r(&time, &t);
6577 			(void) strftime(tsbuf, DATETIME_BUF_LEN,
6578 			    gettext(STRFTIME_FMT_STR), &t);
6579 
6580 			if (scripted) {
6581 				if (parsable) {
6582 					(void) printf("%s\t%s\t%ld\n", zname,
6583 					    tagname, (unsigned long)time);
6584 				} else {
6585 					(void) printf("%s\t%s\t%s\n", zname,
6586 					    tagname, tsbuf);
6587 				}
6588 			} else {
6589 				if (parsable) {
6590 					(void) printf("%-*s  %-*s  %ld\n",
6591 					    nwidth, zname, tagwidth,
6592 					    tagname, (unsigned long)time);
6593 				} else {
6594 					(void) printf("%-*s  %-*s  %s\n",
6595 					    nwidth, zname, tagwidth,
6596 					    tagname, tsbuf);
6597 				}
6598 			}
6599 		}
6600 	}
6601 }
6602 
6603 /*
6604  * Generic callback function to list a dataset or snapshot.
6605  */
6606 static int
6607 holds_callback(zfs_handle_t *zhp, void *data)
6608 {
6609 	holds_cbdata_t *cbp = data;
6610 	nvlist_t *top_nvl = *cbp->cb_nvlp;
6611 	nvlist_t *nvl = NULL;
6612 	nvpair_t *nvp = NULL;
6613 	const char *zname = zfs_get_name(zhp);
6614 	size_t znamelen = strlen(zname);
6615 
6616 	if (cbp->cb_recursive) {
6617 		const char *snapname;
6618 		char *delim  = strchr(zname, '@');
6619 		if (delim == NULL)
6620 			return (0);
6621 
6622 		snapname = delim + 1;
6623 		if (strcmp(cbp->cb_snapname, snapname))
6624 			return (0);
6625 	}
6626 
6627 	if (zfs_get_holds(zhp, &nvl) != 0)
6628 		return (-1);
6629 
6630 	if (znamelen > cbp->cb_max_namelen)
6631 		cbp->cb_max_namelen  = znamelen;
6632 
6633 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
6634 		const char *tag = nvpair_name(nvp);
6635 		size_t taglen = strlen(tag);
6636 		if (taglen > cbp->cb_max_taglen)
6637 			cbp->cb_max_taglen  = taglen;
6638 	}
6639 
6640 	return (nvlist_add_nvlist(top_nvl, zname, nvl));
6641 }
6642 
6643 /*
6644  * zfs holds [-rHp] <snap> ...
6645  *
6646  *	-r	Lists holds that are set on the named snapshots recursively.
6647  *	-H	Scripted mode; elide headers and separate columns by tabs.
6648  *	-p	Display values in parsable (literal) format.
6649  */
6650 static int
6651 zfs_do_holds(int argc, char **argv)
6652 {
6653 	int c;
6654 	boolean_t errors = B_FALSE;
6655 	boolean_t scripted = B_FALSE;
6656 	boolean_t recursive = B_FALSE;
6657 	boolean_t parsable = B_FALSE;
6658 
6659 	int types = ZFS_TYPE_SNAPSHOT;
6660 	holds_cbdata_t cb = { 0 };
6661 
6662 	int limit = 0;
6663 	int ret = 0;
6664 	int flags = 0;
6665 
6666 	/* check options */
6667 	while ((c = getopt(argc, argv, "rHp")) != -1) {
6668 		switch (c) {
6669 		case 'r':
6670 			recursive = B_TRUE;
6671 			break;
6672 		case 'H':
6673 			scripted = B_TRUE;
6674 			break;
6675 		case 'p':
6676 			parsable = B_TRUE;
6677 			break;
6678 		case '?':
6679 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6680 			    optopt);
6681 			usage(B_FALSE);
6682 		}
6683 	}
6684 
6685 	if (recursive) {
6686 		types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
6687 		flags |= ZFS_ITER_RECURSE;
6688 	}
6689 
6690 	argc -= optind;
6691 	argv += optind;
6692 
6693 	/* check number of arguments */
6694 	if (argc < 1)
6695 		usage(B_FALSE);
6696 
6697 	nvlist_t *nvl = fnvlist_alloc();
6698 
6699 	for (int i = 0; i < argc; ++i) {
6700 		char *snapshot = argv[i];
6701 		const char *delim;
6702 		const char *snapname;
6703 
6704 		delim = strchr(snapshot, '@');
6705 		if (delim == NULL) {
6706 			(void) fprintf(stderr,
6707 			    gettext("'%s' is not a snapshot\n"), snapshot);
6708 			errors = B_TRUE;
6709 			continue;
6710 		}
6711 		snapname = delim + 1;
6712 		if (recursive)
6713 			snapshot[delim - snapshot] = '\0';
6714 
6715 		cb.cb_recursive = recursive;
6716 		cb.cb_snapname = snapname;
6717 		cb.cb_nvlp = &nvl;
6718 
6719 		/*
6720 		 *  1. collect holds data, set format options
6721 		 */
6722 		ret = zfs_for_each(1, argv + i, flags, types, NULL, NULL, limit,
6723 		    holds_callback, &cb);
6724 		if (ret != 0)
6725 			errors = B_TRUE;
6726 	}
6727 
6728 	/*
6729 	 *  2. print holds data
6730 	 */
6731 	print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl,
6732 	    parsable);
6733 
6734 	if (nvlist_empty(nvl))
6735 		(void) fprintf(stderr, gettext("no datasets available\n"));
6736 
6737 	nvlist_free(nvl);
6738 
6739 	return (errors);
6740 }
6741 
6742 #define	CHECK_SPINNER 30
6743 #define	SPINNER_TIME 3		/* seconds */
6744 #define	MOUNT_TIME 1		/* seconds */
6745 
6746 typedef struct get_all_state {
6747 	boolean_t	ga_verbose;
6748 	get_all_cb_t	*ga_cbp;
6749 } get_all_state_t;
6750 
6751 static int
6752 get_one_dataset(zfs_handle_t *zhp, void *data)
6753 {
6754 	static const char *const spin[] = { "-", "\\", "|", "/" };
6755 	static int spinval = 0;
6756 	static int spincheck = 0;
6757 	static time_t last_spin_time = (time_t)0;
6758 	get_all_state_t *state = data;
6759 	zfs_type_t type = zfs_get_type(zhp);
6760 
6761 	if (state->ga_verbose) {
6762 		if (--spincheck < 0) {
6763 			time_t now = time(NULL);
6764 			if (last_spin_time + SPINNER_TIME < now) {
6765 				update_progress(spin[spinval++ % 4]);
6766 				last_spin_time = now;
6767 			}
6768 			spincheck = CHECK_SPINNER;
6769 		}
6770 	}
6771 
6772 	/*
6773 	 * Iterate over any nested datasets.
6774 	 */
6775 	if (zfs_iter_filesystems_v2(zhp, 0, get_one_dataset, data) != 0) {
6776 		zfs_close(zhp);
6777 		return (1);
6778 	}
6779 
6780 	/*
6781 	 * Skip any datasets whose type does not match.
6782 	 */
6783 	if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
6784 		zfs_close(zhp);
6785 		return (0);
6786 	}
6787 	libzfs_add_handle(state->ga_cbp, zhp);
6788 	assert(state->ga_cbp->cb_used <= state->ga_cbp->cb_alloc);
6789 
6790 	return (0);
6791 }
6792 
6793 static void
6794 get_all_datasets(get_all_cb_t *cbp, boolean_t verbose)
6795 {
6796 	get_all_state_t state = {
6797 	    .ga_verbose = verbose,
6798 	    .ga_cbp = cbp
6799 	};
6800 
6801 	if (verbose)
6802 		set_progress_header(gettext("Reading ZFS config"));
6803 	(void) zfs_iter_root(g_zfs, get_one_dataset, &state);
6804 
6805 	if (verbose)
6806 		finish_progress(gettext("done."));
6807 }
6808 
6809 /*
6810  * Generic callback for sharing or mounting filesystems.  Because the code is so
6811  * similar, we have a common function with an extra parameter to determine which
6812  * mode we are using.
6813  */
6814 typedef enum { OP_SHARE, OP_MOUNT } share_mount_op_t;
6815 
6816 typedef struct share_mount_state {
6817 	share_mount_op_t	sm_op;
6818 	boolean_t	sm_verbose;
6819 	int	sm_flags;
6820 	char	*sm_options;
6821 	enum sa_protocol	sm_proto; /* only valid for OP_SHARE */
6822 	pthread_mutex_t	sm_lock; /* protects the remaining fields */
6823 	uint_t	sm_total; /* number of filesystems to process */
6824 	uint_t	sm_done; /* number of filesystems processed */
6825 	int	sm_status; /* -1 if any of the share/mount operations failed */
6826 } share_mount_state_t;
6827 
6828 /*
6829  * Share or mount a dataset.
6830  */
6831 static int
6832 share_mount_one(zfs_handle_t *zhp, int op, int flags, enum sa_protocol protocol,
6833     boolean_t explicit, const char *options)
6834 {
6835 	char mountpoint[ZFS_MAXPROPLEN];
6836 	char shareopts[ZFS_MAXPROPLEN];
6837 	char smbshareopts[ZFS_MAXPROPLEN];
6838 	const char *cmdname = op == OP_SHARE ? "share" : "mount";
6839 	struct mnttab mnt;
6840 	uint64_t zoned, canmount;
6841 	boolean_t shared_nfs, shared_smb;
6842 
6843 	assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
6844 
6845 	/*
6846 	 * Check to make sure we can mount/share this dataset.  If we
6847 	 * are in the global zone and the filesystem is exported to a
6848 	 * local zone, or if we are in a local zone and the
6849 	 * filesystem is not exported, then it is an error.
6850 	 */
6851 	zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
6852 
6853 	if (zoned && getzoneid() == GLOBAL_ZONEID) {
6854 		if (!explicit)
6855 			return (0);
6856 
6857 		(void) fprintf(stderr, gettext("cannot %s '%s': "
6858 		    "dataset is exported to a local zone\n"), cmdname,
6859 		    zfs_get_name(zhp));
6860 		return (1);
6861 
6862 	} else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
6863 		if (!explicit)
6864 			return (0);
6865 
6866 		(void) fprintf(stderr, gettext("cannot %s '%s': "
6867 		    "permission denied\n"), cmdname,
6868 		    zfs_get_name(zhp));
6869 		return (1);
6870 	}
6871 
6872 	/*
6873 	 * Ignore any filesystems which don't apply to us. This
6874 	 * includes those with a legacy mountpoint, or those with
6875 	 * legacy share options.
6876 	 */
6877 	verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6878 	    sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
6879 	verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
6880 	    sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
6881 	verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
6882 	    sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
6883 
6884 	if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
6885 	    strcmp(smbshareopts, "off") == 0) {
6886 		if (!explicit)
6887 			return (0);
6888 
6889 		(void) fprintf(stderr, gettext("cannot share '%s': "
6890 		    "legacy share\n"), zfs_get_name(zhp));
6891 		(void) fprintf(stderr, gettext("use exports(5) or "
6892 		    "smb.conf(5) to share this filesystem, or set "
6893 		    "the sharenfs or sharesmb property\n"));
6894 		return (1);
6895 	}
6896 
6897 	/*
6898 	 * We cannot share or mount legacy filesystems. If the
6899 	 * shareopts is non-legacy but the mountpoint is legacy, we
6900 	 * treat it as a legacy share.
6901 	 */
6902 	if (strcmp(mountpoint, "legacy") == 0) {
6903 		if (!explicit)
6904 			return (0);
6905 
6906 		(void) fprintf(stderr, gettext("cannot %s '%s': "
6907 		    "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
6908 		(void) fprintf(stderr, gettext("use %s(8) to "
6909 		    "%s this filesystem\n"), cmdname, cmdname);
6910 		return (1);
6911 	}
6912 
6913 	if (strcmp(mountpoint, "none") == 0) {
6914 		if (!explicit)
6915 			return (0);
6916 
6917 		(void) fprintf(stderr, gettext("cannot %s '%s': no "
6918 		    "mountpoint set\n"), cmdname, zfs_get_name(zhp));
6919 		return (1);
6920 	}
6921 
6922 	/*
6923 	 * canmount	explicit	outcome
6924 	 * on		no		pass through
6925 	 * on		yes		pass through
6926 	 * off		no		return 0
6927 	 * off		yes		display error, return 1
6928 	 * noauto	no		return 0
6929 	 * noauto	yes		pass through
6930 	 */
6931 	canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
6932 	if (canmount == ZFS_CANMOUNT_OFF) {
6933 		if (!explicit)
6934 			return (0);
6935 
6936 		(void) fprintf(stderr, gettext("cannot %s '%s': "
6937 		    "'canmount' property is set to 'off'\n"), cmdname,
6938 		    zfs_get_name(zhp));
6939 		return (1);
6940 	} else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
6941 		/*
6942 		 * When performing a 'zfs mount -a', we skip any mounts for
6943 		 * datasets that have 'noauto' set. Sharing a dataset with
6944 		 * 'noauto' set is only allowed if it's mounted.
6945 		 */
6946 		if (op == OP_MOUNT)
6947 			return (0);
6948 		if (op == OP_SHARE && !zfs_is_mounted(zhp, NULL)) {
6949 			/* also purge it from existing exports */
6950 			zfs_unshare(zhp, mountpoint, NULL);
6951 			return (0);
6952 		}
6953 	}
6954 
6955 	/*
6956 	 * If this filesystem is encrypted and does not have
6957 	 * a loaded key, we can not mount it.
6958 	 */
6959 	if ((flags & MS_CRYPT) == 0 &&
6960 	    zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF &&
6961 	    zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
6962 	    ZFS_KEYSTATUS_UNAVAILABLE) {
6963 		if (!explicit)
6964 			return (0);
6965 
6966 		(void) fprintf(stderr, gettext("cannot %s '%s': "
6967 		    "encryption key not loaded\n"), cmdname, zfs_get_name(zhp));
6968 		return (1);
6969 	}
6970 
6971 	/*
6972 	 * If this filesystem is inconsistent and has a receive resume
6973 	 * token, we can not mount it.
6974 	 */
6975 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
6976 	    zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
6977 	    NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
6978 		if (!explicit)
6979 			return (0);
6980 
6981 		(void) fprintf(stderr, gettext("cannot %s '%s': "
6982 		    "Contains partially-completed state from "
6983 		    "\"zfs receive -s\", which can be resumed with "
6984 		    "\"zfs send -t\"\n"),
6985 		    cmdname, zfs_get_name(zhp));
6986 		return (1);
6987 	}
6988 
6989 	if (zfs_prop_get_int(zhp, ZFS_PROP_REDACTED) && !(flags & MS_FORCE)) {
6990 		if (!explicit)
6991 			return (0);
6992 
6993 		(void) fprintf(stderr, gettext("cannot %s '%s': "
6994 		    "Dataset is not complete, was created by receiving "
6995 		    "a redacted zfs send stream.\n"), cmdname,
6996 		    zfs_get_name(zhp));
6997 		return (1);
6998 	}
6999 
7000 	/*
7001 	 * At this point, we have verified that the mountpoint and/or
7002 	 * shareopts are appropriate for auto management. If the
7003 	 * filesystem is already mounted or shared, return (failing
7004 	 * for explicit requests); otherwise mount or share the
7005 	 * filesystem.
7006 	 */
7007 	switch (op) {
7008 	case OP_SHARE: {
7009 		enum sa_protocol prot[] = {SA_PROTOCOL_NFS, SA_NO_PROTOCOL};
7010 		shared_nfs = zfs_is_shared(zhp, NULL, prot);
7011 		*prot = SA_PROTOCOL_SMB;
7012 		shared_smb = zfs_is_shared(zhp, NULL, prot);
7013 
7014 		if ((shared_nfs && shared_smb) ||
7015 		    (shared_nfs && strcmp(shareopts, "on") == 0 &&
7016 		    strcmp(smbshareopts, "off") == 0) ||
7017 		    (shared_smb && strcmp(smbshareopts, "on") == 0 &&
7018 		    strcmp(shareopts, "off") == 0)) {
7019 			if (!explicit)
7020 				return (0);
7021 
7022 			(void) fprintf(stderr, gettext("cannot share "
7023 			    "'%s': filesystem already shared\n"),
7024 			    zfs_get_name(zhp));
7025 			return (1);
7026 		}
7027 
7028 		if (!zfs_is_mounted(zhp, NULL) &&
7029 		    zfs_mount(zhp, NULL, flags) != 0)
7030 			return (1);
7031 
7032 		*prot = protocol;
7033 		if (zfs_share(zhp, protocol == SA_NO_PROTOCOL ? NULL : prot))
7034 			return (1);
7035 
7036 	}
7037 		break;
7038 
7039 	case OP_MOUNT:
7040 		mnt.mnt_mntopts = (char *)(options ?: "");
7041 
7042 		if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
7043 		    zfs_is_mounted(zhp, NULL)) {
7044 			if (!explicit)
7045 				return (0);
7046 
7047 			(void) fprintf(stderr, gettext("cannot mount "
7048 			    "'%s': filesystem already mounted\n"),
7049 			    zfs_get_name(zhp));
7050 			return (1);
7051 		}
7052 
7053 		if (zfs_mount(zhp, options, flags) != 0)
7054 			return (1);
7055 		break;
7056 	}
7057 
7058 	return (0);
7059 }
7060 
7061 /*
7062  * Reports progress in the form "(current/total)".  Not thread-safe.
7063  */
7064 static void
7065 report_mount_progress(int current, int total)
7066 {
7067 	static time_t last_progress_time = 0;
7068 	time_t now = time(NULL);
7069 	char info[32];
7070 
7071 	/* display header if we're here for the first time */
7072 	if (current == 1) {
7073 		set_progress_header(gettext("Mounting ZFS filesystems"));
7074 	} else if (current != total && last_progress_time + MOUNT_TIME >= now) {
7075 		/* too soon to report again */
7076 		return;
7077 	}
7078 
7079 	last_progress_time = now;
7080 
7081 	(void) sprintf(info, "(%d/%d)", current, total);
7082 
7083 	if (current == total)
7084 		finish_progress(info);
7085 	else
7086 		update_progress(info);
7087 }
7088 
7089 /*
7090  * zfs_foreach_mountpoint() callback that mounts or shares one filesystem and
7091  * updates the progress meter.
7092  */
7093 static int
7094 share_mount_one_cb(zfs_handle_t *zhp, void *arg)
7095 {
7096 	share_mount_state_t *sms = arg;
7097 	int ret;
7098 
7099 	ret = share_mount_one(zhp, sms->sm_op, sms->sm_flags, sms->sm_proto,
7100 	    B_FALSE, sms->sm_options);
7101 
7102 	pthread_mutex_lock(&sms->sm_lock);
7103 	if (ret != 0)
7104 		sms->sm_status = ret;
7105 	sms->sm_done++;
7106 	if (sms->sm_verbose)
7107 		report_mount_progress(sms->sm_done, sms->sm_total);
7108 	pthread_mutex_unlock(&sms->sm_lock);
7109 	return (ret);
7110 }
7111 
7112 static void
7113 append_options(char *mntopts, char *newopts)
7114 {
7115 	int len = strlen(mntopts);
7116 
7117 	/* original length plus new string to append plus 1 for the comma */
7118 	if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
7119 		(void) fprintf(stderr, gettext("the opts argument for "
7120 		    "'%s' option is too long (more than %d chars)\n"),
7121 		    "-o", MNT_LINE_MAX);
7122 		usage(B_FALSE);
7123 	}
7124 
7125 	if (*mntopts)
7126 		mntopts[len++] = ',';
7127 
7128 	(void) strcpy(&mntopts[len], newopts);
7129 }
7130 
7131 static enum sa_protocol
7132 sa_protocol_decode(const char *protocol)
7133 {
7134 	for (enum sa_protocol i = 0; i < ARRAY_SIZE(sa_protocol_names); ++i)
7135 		if (strcmp(protocol, sa_protocol_names[i]) == 0)
7136 			return (i);
7137 
7138 	(void) fputs(gettext("share type must be one of: "), stderr);
7139 	for (enum sa_protocol i = 0;
7140 	    i < ARRAY_SIZE(sa_protocol_names); ++i)
7141 		(void) fprintf(stderr, "%s%s",
7142 		    i != 0 ? ", " : "", sa_protocol_names[i]);
7143 	(void) fputc('\n', stderr);
7144 	usage(B_FALSE);
7145 }
7146 
7147 static int
7148 share_mount(int op, int argc, char **argv)
7149 {
7150 	int do_all = 0;
7151 	boolean_t verbose = B_FALSE;
7152 	int c, ret = 0;
7153 	char *options = NULL;
7154 	int flags = 0;
7155 
7156 	/* check options */
7157 	while ((c = getopt(argc, argv, op == OP_MOUNT ? ":alvo:Of" : "al"))
7158 	    != -1) {
7159 		switch (c) {
7160 		case 'a':
7161 			do_all = 1;
7162 			break;
7163 		case 'v':
7164 			verbose = B_TRUE;
7165 			break;
7166 		case 'l':
7167 			flags |= MS_CRYPT;
7168 			break;
7169 		case 'o':
7170 			if (*optarg == '\0') {
7171 				(void) fprintf(stderr, gettext("empty mount "
7172 				    "options (-o) specified\n"));
7173 				usage(B_FALSE);
7174 			}
7175 
7176 			if (options == NULL)
7177 				options = safe_malloc(MNT_LINE_MAX + 1);
7178 
7179 			/* option validation is done later */
7180 			append_options(options, optarg);
7181 			break;
7182 		case 'O':
7183 			flags |= MS_OVERLAY;
7184 			break;
7185 		case 'f':
7186 			flags |= MS_FORCE;
7187 			break;
7188 		case ':':
7189 			(void) fprintf(stderr, gettext("missing argument for "
7190 			    "'%c' option\n"), optopt);
7191 			usage(B_FALSE);
7192 			break;
7193 		case '?':
7194 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
7195 			    optopt);
7196 			usage(B_FALSE);
7197 		}
7198 	}
7199 
7200 	argc -= optind;
7201 	argv += optind;
7202 
7203 	/* check number of arguments */
7204 	if (do_all) {
7205 		enum sa_protocol protocol = SA_NO_PROTOCOL;
7206 
7207 		if (op == OP_SHARE && argc > 0) {
7208 			protocol = sa_protocol_decode(argv[0]);
7209 			argc--;
7210 			argv++;
7211 		}
7212 
7213 		if (argc != 0) {
7214 			(void) fprintf(stderr, gettext("too many arguments\n"));
7215 			usage(B_FALSE);
7216 		}
7217 
7218 		start_progress_timer();
7219 		get_all_cb_t cb = { 0 };
7220 		get_all_datasets(&cb, verbose);
7221 
7222 		if (cb.cb_used == 0) {
7223 			free(options);
7224 			return (0);
7225 		}
7226 
7227 		share_mount_state_t share_mount_state = { 0 };
7228 		share_mount_state.sm_op = op;
7229 		share_mount_state.sm_verbose = verbose;
7230 		share_mount_state.sm_flags = flags;
7231 		share_mount_state.sm_options = options;
7232 		share_mount_state.sm_proto = protocol;
7233 		share_mount_state.sm_total = cb.cb_used;
7234 		pthread_mutex_init(&share_mount_state.sm_lock, NULL);
7235 
7236 		/* For a 'zfs share -a' operation start with a clean slate. */
7237 		zfs_truncate_shares(NULL);
7238 
7239 		/*
7240 		 * libshare isn't mt-safe, so only do the operation in parallel
7241 		 * if we're mounting. Additionally, the key-loading option must
7242 		 * be serialized so that we can prompt the user for their keys
7243 		 * in a consistent manner.
7244 		 */
7245 		zfs_foreach_mountpoint(g_zfs, cb.cb_handles, cb.cb_used,
7246 		    share_mount_one_cb, &share_mount_state,
7247 		    op == OP_MOUNT && !(flags & MS_CRYPT));
7248 		zfs_commit_shares(NULL);
7249 
7250 		ret = share_mount_state.sm_status;
7251 
7252 		for (int i = 0; i < cb.cb_used; i++)
7253 			zfs_close(cb.cb_handles[i]);
7254 		free(cb.cb_handles);
7255 	} else if (argc == 0) {
7256 		FILE *mnttab;
7257 		struct mnttab entry;
7258 
7259 		if ((op == OP_SHARE) || (options != NULL)) {
7260 			(void) fprintf(stderr, gettext("missing filesystem "
7261 			    "argument (specify -a for all)\n"));
7262 			usage(B_FALSE);
7263 		}
7264 
7265 		/*
7266 		 * When mount is given no arguments, go through
7267 		 * /proc/self/mounts and display any active ZFS mounts.
7268 		 * We hide any snapshots, since they are controlled
7269 		 * automatically.
7270 		 */
7271 
7272 		if ((mnttab = fopen(MNTTAB, "re")) == NULL) {
7273 			free(options);
7274 			return (ENOENT);
7275 		}
7276 
7277 		while (getmntent(mnttab, &entry) == 0) {
7278 			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
7279 			    strchr(entry.mnt_special, '@') != NULL)
7280 				continue;
7281 
7282 			(void) printf("%-30s  %s\n", entry.mnt_special,
7283 			    entry.mnt_mountp);
7284 		}
7285 
7286 		(void) fclose(mnttab);
7287 	} else {
7288 		zfs_handle_t *zhp;
7289 
7290 		if (argc > 1) {
7291 			(void) fprintf(stderr,
7292 			    gettext("too many arguments\n"));
7293 			usage(B_FALSE);
7294 		}
7295 
7296 		if ((zhp = zfs_open(g_zfs, argv[0],
7297 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
7298 			ret = 1;
7299 		} else {
7300 			ret = share_mount_one(zhp, op, flags, SA_NO_PROTOCOL,
7301 			    B_TRUE, options);
7302 			zfs_commit_shares(NULL);
7303 			zfs_close(zhp);
7304 		}
7305 	}
7306 
7307 	free(options);
7308 	return (ret);
7309 }
7310 
7311 /*
7312  * zfs mount -a
7313  * zfs mount filesystem
7314  *
7315  * Mount all filesystems, or mount the given filesystem.
7316  */
7317 static int
7318 zfs_do_mount(int argc, char **argv)
7319 {
7320 	return (share_mount(OP_MOUNT, argc, argv));
7321 }
7322 
7323 /*
7324  * zfs share -a [nfs | smb]
7325  * zfs share filesystem
7326  *
7327  * Share all filesystems, or share the given filesystem.
7328  */
7329 static int
7330 zfs_do_share(int argc, char **argv)
7331 {
7332 	return (share_mount(OP_SHARE, argc, argv));
7333 }
7334 
7335 typedef struct unshare_unmount_node {
7336 	zfs_handle_t	*un_zhp;
7337 	char		*un_mountp;
7338 	uu_avl_node_t	un_avlnode;
7339 } unshare_unmount_node_t;
7340 
7341 static int
7342 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
7343 {
7344 	(void) unused;
7345 	const unshare_unmount_node_t *l = larg;
7346 	const unshare_unmount_node_t *r = rarg;
7347 
7348 	return (strcmp(l->un_mountp, r->un_mountp));
7349 }
7350 
7351 /*
7352  * Convenience routine used by zfs_do_umount() and manual_unmount().  Given an
7353  * absolute path, find the entry /proc/self/mounts, verify that it's a
7354  * ZFS filesystem, and unmount it appropriately.
7355  */
7356 static int
7357 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
7358 {
7359 	zfs_handle_t *zhp;
7360 	int ret = 0;
7361 	struct stat64 statbuf;
7362 	struct extmnttab entry;
7363 	const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
7364 	ino_t path_inode;
7365 
7366 	/*
7367 	 * Search for the given (major,minor) pair in the mount table.
7368 	 */
7369 
7370 	if (getextmntent(path, &entry, &statbuf) != 0) {
7371 		if (op == OP_SHARE) {
7372 			(void) fprintf(stderr, gettext("cannot %s '%s': not "
7373 			    "currently mounted\n"), cmdname, path);
7374 			return (1);
7375 		}
7376 		(void) fprintf(stderr, gettext("warning: %s not in"
7377 		    "/proc/self/mounts\n"), path);
7378 		if ((ret = umount2(path, flags)) != 0)
7379 			(void) fprintf(stderr, gettext("%s: %s\n"), path,
7380 			    strerror(errno));
7381 		return (ret != 0);
7382 	}
7383 	path_inode = statbuf.st_ino;
7384 
7385 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
7386 		(void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
7387 		    "filesystem\n"), cmdname, path);
7388 		return (1);
7389 	}
7390 
7391 	if ((zhp = zfs_open(g_zfs, entry.mnt_special,
7392 	    ZFS_TYPE_FILESYSTEM)) == NULL)
7393 		return (1);
7394 
7395 	ret = 1;
7396 	if (stat64(entry.mnt_mountp, &statbuf) != 0) {
7397 		(void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
7398 		    cmdname, path, strerror(errno));
7399 		goto out;
7400 	} else if (statbuf.st_ino != path_inode) {
7401 		(void) fprintf(stderr, gettext("cannot "
7402 		    "%s '%s': not a mountpoint\n"), cmdname, path);
7403 		goto out;
7404 	}
7405 
7406 	if (op == OP_SHARE) {
7407 		char nfs_mnt_prop[ZFS_MAXPROPLEN];
7408 		char smbshare_prop[ZFS_MAXPROPLEN];
7409 
7410 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
7411 		    sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
7412 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
7413 		    sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
7414 
7415 		if (strcmp(nfs_mnt_prop, "off") == 0 &&
7416 		    strcmp(smbshare_prop, "off") == 0) {
7417 			(void) fprintf(stderr, gettext("cannot unshare "
7418 			    "'%s': legacy share\n"), path);
7419 			(void) fprintf(stderr, gettext("use exportfs(8) "
7420 			    "or smbcontrol(1) to unshare this filesystem\n"));
7421 		} else if (!zfs_is_shared(zhp, NULL, NULL)) {
7422 			(void) fprintf(stderr, gettext("cannot unshare '%s': "
7423 			    "not currently shared\n"), path);
7424 		} else {
7425 			ret = zfs_unshare(zhp, path, NULL);
7426 			zfs_commit_shares(NULL);
7427 		}
7428 	} else {
7429 		char mtpt_prop[ZFS_MAXPROPLEN];
7430 
7431 		verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
7432 		    sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
7433 
7434 		if (is_manual) {
7435 			ret = zfs_unmount(zhp, NULL, flags);
7436 		} else if (strcmp(mtpt_prop, "legacy") == 0) {
7437 			(void) fprintf(stderr, gettext("cannot unmount "
7438 			    "'%s': legacy mountpoint\n"),
7439 			    zfs_get_name(zhp));
7440 			(void) fprintf(stderr, gettext("use umount(8) "
7441 			    "to unmount this filesystem\n"));
7442 		} else {
7443 			ret = zfs_unmountall(zhp, flags);
7444 		}
7445 	}
7446 
7447 out:
7448 	zfs_close(zhp);
7449 
7450 	return (ret != 0);
7451 }
7452 
7453 /*
7454  * Generic callback for unsharing or unmounting a filesystem.
7455  */
7456 static int
7457 unshare_unmount(int op, int argc, char **argv)
7458 {
7459 	int do_all = 0;
7460 	int flags = 0;
7461 	int ret = 0;
7462 	int c;
7463 	zfs_handle_t *zhp;
7464 	char nfs_mnt_prop[ZFS_MAXPROPLEN];
7465 	char sharesmb[ZFS_MAXPROPLEN];
7466 
7467 	/* check options */
7468 	while ((c = getopt(argc, argv, op == OP_SHARE ? ":a" : "afu")) != -1) {
7469 		switch (c) {
7470 		case 'a':
7471 			do_all = 1;
7472 			break;
7473 		case 'f':
7474 			flags |= MS_FORCE;
7475 			break;
7476 		case 'u':
7477 			flags |= MS_CRYPT;
7478 			break;
7479 		case ':':
7480 			(void) fprintf(stderr, gettext("missing argument for "
7481 			    "'%c' option\n"), optopt);
7482 			usage(B_FALSE);
7483 			break;
7484 		case '?':
7485 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
7486 			    optopt);
7487 			usage(B_FALSE);
7488 		}
7489 	}
7490 
7491 	argc -= optind;
7492 	argv += optind;
7493 
7494 	if (do_all) {
7495 		/*
7496 		 * We could make use of zfs_for_each() to walk all datasets in
7497 		 * the system, but this would be very inefficient, especially
7498 		 * since we would have to linearly search /proc/self/mounts for
7499 		 * each one. Instead, do one pass through /proc/self/mounts
7500 		 * looking for zfs entries and call zfs_unmount() for each one.
7501 		 *
7502 		 * Things get a little tricky if the administrator has created
7503 		 * mountpoints beneath other ZFS filesystems.  In this case, we
7504 		 * have to unmount the deepest filesystems first.  To accomplish
7505 		 * this, we place all the mountpoints in an AVL tree sorted by
7506 		 * the special type (dataset name), and walk the result in
7507 		 * reverse to make sure to get any snapshots first.
7508 		 */
7509 		FILE *mnttab;
7510 		struct mnttab entry;
7511 		uu_avl_pool_t *pool;
7512 		uu_avl_t *tree = NULL;
7513 		unshare_unmount_node_t *node;
7514 		uu_avl_index_t idx;
7515 		uu_avl_walk_t *walk;
7516 		enum sa_protocol *protocol = NULL,
7517 		    single_protocol[] = {SA_NO_PROTOCOL, SA_NO_PROTOCOL};
7518 
7519 		if (op == OP_SHARE && argc > 0) {
7520 			*single_protocol = sa_protocol_decode(argv[0]);
7521 			protocol = single_protocol;
7522 			argc--;
7523 			argv++;
7524 		}
7525 
7526 		if (argc != 0) {
7527 			(void) fprintf(stderr, gettext("too many arguments\n"));
7528 			usage(B_FALSE);
7529 		}
7530 
7531 		if (((pool = uu_avl_pool_create("unmount_pool",
7532 		    sizeof (unshare_unmount_node_t),
7533 		    offsetof(unshare_unmount_node_t, un_avlnode),
7534 		    unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
7535 		    ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
7536 			nomem();
7537 
7538 		if ((mnttab = fopen(MNTTAB, "re")) == NULL) {
7539 			uu_avl_destroy(tree);
7540 			uu_avl_pool_destroy(pool);
7541 			return (ENOENT);
7542 		}
7543 
7544 		while (getmntent(mnttab, &entry) == 0) {
7545 
7546 			/* ignore non-ZFS entries */
7547 			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
7548 				continue;
7549 
7550 			/* ignore snapshots */
7551 			if (strchr(entry.mnt_special, '@') != NULL)
7552 				continue;
7553 
7554 			if ((zhp = zfs_open(g_zfs, entry.mnt_special,
7555 			    ZFS_TYPE_FILESYSTEM)) == NULL) {
7556 				ret = 1;
7557 				continue;
7558 			}
7559 
7560 			/*
7561 			 * Ignore datasets that are excluded/restricted by
7562 			 * parent pool name.
7563 			 */
7564 			if (zpool_skip_pool(zfs_get_pool_name(zhp))) {
7565 				zfs_close(zhp);
7566 				continue;
7567 			}
7568 
7569 			switch (op) {
7570 			case OP_SHARE:
7571 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
7572 				    nfs_mnt_prop,
7573 				    sizeof (nfs_mnt_prop),
7574 				    NULL, NULL, 0, B_FALSE) == 0);
7575 				if (strcmp(nfs_mnt_prop, "off") != 0)
7576 					break;
7577 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
7578 				    nfs_mnt_prop,
7579 				    sizeof (nfs_mnt_prop),
7580 				    NULL, NULL, 0, B_FALSE) == 0);
7581 				if (strcmp(nfs_mnt_prop, "off") == 0)
7582 					continue;
7583 				break;
7584 			case OP_MOUNT:
7585 				/* Ignore legacy mounts */
7586 				verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
7587 				    nfs_mnt_prop,
7588 				    sizeof (nfs_mnt_prop),
7589 				    NULL, NULL, 0, B_FALSE) == 0);
7590 				if (strcmp(nfs_mnt_prop, "legacy") == 0)
7591 					continue;
7592 				/* Ignore canmount=noauto mounts */
7593 				if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
7594 				    ZFS_CANMOUNT_NOAUTO)
7595 					continue;
7596 				break;
7597 			default:
7598 				break;
7599 			}
7600 
7601 			node = safe_malloc(sizeof (unshare_unmount_node_t));
7602 			node->un_zhp = zhp;
7603 			node->un_mountp = safe_strdup(entry.mnt_mountp);
7604 
7605 			uu_avl_node_init(node, &node->un_avlnode, pool);
7606 
7607 			if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
7608 				uu_avl_insert(tree, node, idx);
7609 			} else {
7610 				zfs_close(node->un_zhp);
7611 				free(node->un_mountp);
7612 				free(node);
7613 			}
7614 		}
7615 		(void) fclose(mnttab);
7616 
7617 		/*
7618 		 * Walk the AVL tree in reverse, unmounting each filesystem and
7619 		 * removing it from the AVL tree in the process.
7620 		 */
7621 		if ((walk = uu_avl_walk_start(tree,
7622 		    UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
7623 			nomem();
7624 
7625 		while ((node = uu_avl_walk_next(walk)) != NULL) {
7626 			const char *mntarg = NULL;
7627 
7628 			uu_avl_remove(tree, node);
7629 			switch (op) {
7630 			case OP_SHARE:
7631 				if (zfs_unshare(node->un_zhp,
7632 				    node->un_mountp, protocol) != 0)
7633 					ret = 1;
7634 				break;
7635 
7636 			case OP_MOUNT:
7637 				if (zfs_unmount(node->un_zhp,
7638 				    mntarg, flags) != 0)
7639 					ret = 1;
7640 				break;
7641 			}
7642 
7643 			zfs_close(node->un_zhp);
7644 			free(node->un_mountp);
7645 			free(node);
7646 		}
7647 
7648 		if (op == OP_SHARE)
7649 			zfs_commit_shares(protocol);
7650 
7651 		uu_avl_walk_end(walk);
7652 		uu_avl_destroy(tree);
7653 		uu_avl_pool_destroy(pool);
7654 
7655 	} else {
7656 		if (argc != 1) {
7657 			if (argc == 0)
7658 				(void) fprintf(stderr,
7659 				    gettext("missing filesystem argument\n"));
7660 			else
7661 				(void) fprintf(stderr,
7662 				    gettext("too many arguments\n"));
7663 			usage(B_FALSE);
7664 		}
7665 
7666 		/*
7667 		 * We have an argument, but it may be a full path or a ZFS
7668 		 * filesystem.  Pass full paths off to unmount_path() (shared by
7669 		 * manual_unmount), otherwise open the filesystem and pass to
7670 		 * zfs_unmount().
7671 		 */
7672 		if (argv[0][0] == '/')
7673 			return (unshare_unmount_path(op, argv[0],
7674 			    flags, B_FALSE));
7675 
7676 		if ((zhp = zfs_open(g_zfs, argv[0],
7677 		    ZFS_TYPE_FILESYSTEM)) == NULL)
7678 			return (1);
7679 
7680 		verify(zfs_prop_get(zhp, op == OP_SHARE ?
7681 		    ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
7682 		    nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
7683 		    NULL, 0, B_FALSE) == 0);
7684 
7685 		switch (op) {
7686 		case OP_SHARE:
7687 			verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
7688 			    nfs_mnt_prop,
7689 			    sizeof (nfs_mnt_prop),
7690 			    NULL, NULL, 0, B_FALSE) == 0);
7691 			verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
7692 			    sharesmb, sizeof (sharesmb), NULL, NULL,
7693 			    0, B_FALSE) == 0);
7694 
7695 			if (strcmp(nfs_mnt_prop, "off") == 0 &&
7696 			    strcmp(sharesmb, "off") == 0) {
7697 				(void) fprintf(stderr, gettext("cannot "
7698 				    "unshare '%s': legacy share\n"),
7699 				    zfs_get_name(zhp));
7700 				(void) fprintf(stderr, gettext("use "
7701 				    "exports(5) or smb.conf(5) to unshare "
7702 				    "this filesystem\n"));
7703 				ret = 1;
7704 			} else if (!zfs_is_shared(zhp, NULL, NULL)) {
7705 				(void) fprintf(stderr, gettext("cannot "
7706 				    "unshare '%s': not currently "
7707 				    "shared\n"), zfs_get_name(zhp));
7708 				ret = 1;
7709 			} else if (zfs_unshareall(zhp, NULL) != 0) {
7710 				ret = 1;
7711 			}
7712 			break;
7713 
7714 		case OP_MOUNT:
7715 			if (strcmp(nfs_mnt_prop, "legacy") == 0) {
7716 				(void) fprintf(stderr, gettext("cannot "
7717 				    "unmount '%s': legacy "
7718 				    "mountpoint\n"), zfs_get_name(zhp));
7719 				(void) fprintf(stderr, gettext("use "
7720 				    "umount(8) to unmount this "
7721 				    "filesystem\n"));
7722 				ret = 1;
7723 			} else if (!zfs_is_mounted(zhp, NULL)) {
7724 				(void) fprintf(stderr, gettext("cannot "
7725 				    "unmount '%s': not currently "
7726 				    "mounted\n"),
7727 				    zfs_get_name(zhp));
7728 				ret = 1;
7729 			} else if (zfs_unmountall(zhp, flags) != 0) {
7730 				ret = 1;
7731 			}
7732 			break;
7733 		}
7734 
7735 		zfs_close(zhp);
7736 	}
7737 
7738 	return (ret);
7739 }
7740 
7741 /*
7742  * zfs unmount [-fu] -a
7743  * zfs unmount [-fu] filesystem
7744  *
7745  * Unmount all filesystems, or a specific ZFS filesystem.
7746  */
7747 static int
7748 zfs_do_unmount(int argc, char **argv)
7749 {
7750 	return (unshare_unmount(OP_MOUNT, argc, argv));
7751 }
7752 
7753 /*
7754  * zfs unshare -a
7755  * zfs unshare filesystem
7756  *
7757  * Unshare all filesystems, or a specific ZFS filesystem.
7758  */
7759 static int
7760 zfs_do_unshare(int argc, char **argv)
7761 {
7762 	return (unshare_unmount(OP_SHARE, argc, argv));
7763 }
7764 
7765 static int
7766 find_command_idx(const char *command, int *idx)
7767 {
7768 	int i;
7769 
7770 	for (i = 0; i < NCOMMAND; i++) {
7771 		if (command_table[i].name == NULL)
7772 			continue;
7773 
7774 		if (strcmp(command, command_table[i].name) == 0) {
7775 			*idx = i;
7776 			return (0);
7777 		}
7778 	}
7779 	return (1);
7780 }
7781 
7782 static int
7783 zfs_do_diff(int argc, char **argv)
7784 {
7785 	zfs_handle_t *zhp;
7786 	int flags = 0;
7787 	char *tosnap = NULL;
7788 	char *fromsnap = NULL;
7789 	char *atp, *copy;
7790 	int err = 0;
7791 	int c;
7792 	struct sigaction sa;
7793 
7794 	while ((c = getopt(argc, argv, "FHth")) != -1) {
7795 		switch (c) {
7796 		case 'F':
7797 			flags |= ZFS_DIFF_CLASSIFY;
7798 			break;
7799 		case 'H':
7800 			flags |= ZFS_DIFF_PARSEABLE;
7801 			break;
7802 		case 't':
7803 			flags |= ZFS_DIFF_TIMESTAMP;
7804 			break;
7805 		case 'h':
7806 			flags |= ZFS_DIFF_NO_MANGLE;
7807 			break;
7808 		default:
7809 			(void) fprintf(stderr,
7810 			    gettext("invalid option '%c'\n"), optopt);
7811 			usage(B_FALSE);
7812 		}
7813 	}
7814 
7815 	argc -= optind;
7816 	argv += optind;
7817 
7818 	if (argc < 1) {
7819 		(void) fprintf(stderr,
7820 		    gettext("must provide at least one snapshot name\n"));
7821 		usage(B_FALSE);
7822 	}
7823 
7824 	if (argc > 2) {
7825 		(void) fprintf(stderr, gettext("too many arguments\n"));
7826 		usage(B_FALSE);
7827 	}
7828 
7829 	fromsnap = argv[0];
7830 	tosnap = (argc == 2) ? argv[1] : NULL;
7831 
7832 	copy = NULL;
7833 	if (*fromsnap != '@')
7834 		copy = strdup(fromsnap);
7835 	else if (tosnap)
7836 		copy = strdup(tosnap);
7837 	if (copy == NULL)
7838 		usage(B_FALSE);
7839 
7840 	if ((atp = strchr(copy, '@')) != NULL)
7841 		*atp = '\0';
7842 
7843 	if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL) {
7844 		free(copy);
7845 		return (1);
7846 	}
7847 	free(copy);
7848 
7849 	/*
7850 	 * Ignore SIGPIPE so that the library can give us
7851 	 * information on any failure
7852 	 */
7853 	if (sigemptyset(&sa.sa_mask) == -1) {
7854 		err = errno;
7855 		goto out;
7856 	}
7857 	sa.sa_flags = 0;
7858 	sa.sa_handler = SIG_IGN;
7859 	if (sigaction(SIGPIPE, &sa, NULL) == -1) {
7860 		err = errno;
7861 		goto out;
7862 	}
7863 
7864 	err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
7865 out:
7866 	zfs_close(zhp);
7867 
7868 	return (err != 0);
7869 }
7870 
7871 /*
7872  * zfs bookmark <fs@source>|<fs#source> <fs#bookmark>
7873  *
7874  * Creates a bookmark with the given name from the source snapshot
7875  * or creates a copy of an existing source bookmark.
7876  */
7877 static int
7878 zfs_do_bookmark(int argc, char **argv)
7879 {
7880 	char *source, *bookname;
7881 	char expbuf[ZFS_MAX_DATASET_NAME_LEN];
7882 	int source_type;
7883 	nvlist_t *nvl;
7884 	int ret = 0;
7885 	int c;
7886 
7887 	/* check options */
7888 	while ((c = getopt(argc, argv, "")) != -1) {
7889 		switch (c) {
7890 		case '?':
7891 			(void) fprintf(stderr,
7892 			    gettext("invalid option '%c'\n"), optopt);
7893 			goto usage;
7894 		}
7895 	}
7896 
7897 	argc -= optind;
7898 	argv += optind;
7899 
7900 	/* check number of arguments */
7901 	if (argc < 1) {
7902 		(void) fprintf(stderr, gettext("missing source argument\n"));
7903 		goto usage;
7904 	}
7905 	if (argc < 2) {
7906 		(void) fprintf(stderr, gettext("missing bookmark argument\n"));
7907 		goto usage;
7908 	}
7909 
7910 	source = argv[0];
7911 	bookname = argv[1];
7912 
7913 	if (strchr(source, '@') == NULL && strchr(source, '#') == NULL) {
7914 		(void) fprintf(stderr,
7915 		    gettext("invalid source name '%s': "
7916 		    "must contain a '@' or '#'\n"), source);
7917 		goto usage;
7918 	}
7919 	if (strchr(bookname, '#') == NULL) {
7920 		(void) fprintf(stderr,
7921 		    gettext("invalid bookmark name '%s': "
7922 		    "must contain a '#'\n"), bookname);
7923 		goto usage;
7924 	}
7925 
7926 	/*
7927 	 * expand source or bookname to full path:
7928 	 * one of them may be specified as short name
7929 	 */
7930 	{
7931 		char **expand;
7932 		char *source_short, *bookname_short;
7933 		source_short = strpbrk(source, "@#");
7934 		bookname_short = strpbrk(bookname, "#");
7935 		if (source_short == source &&
7936 		    bookname_short == bookname) {
7937 			(void) fprintf(stderr, gettext(
7938 			    "either source or bookmark must be specified as "
7939 			    "full dataset paths"));
7940 			goto usage;
7941 		} else if (source_short != source &&
7942 		    bookname_short != bookname) {
7943 			expand = NULL;
7944 		} else if (source_short != source) {
7945 			strlcpy(expbuf, source, sizeof (expbuf));
7946 			expand = &bookname;
7947 		} else if (bookname_short != bookname) {
7948 			strlcpy(expbuf, bookname, sizeof (expbuf));
7949 			expand = &source;
7950 		} else {
7951 			abort();
7952 		}
7953 		if (expand != NULL) {
7954 			*strpbrk(expbuf, "@#") = '\0'; /* dataset name in buf */
7955 			(void) strlcat(expbuf, *expand, sizeof (expbuf));
7956 			*expand = expbuf;
7957 		}
7958 	}
7959 
7960 	/* determine source type */
7961 	switch (*strpbrk(source, "@#")) {
7962 		case '@': source_type = ZFS_TYPE_SNAPSHOT; break;
7963 		case '#': source_type = ZFS_TYPE_BOOKMARK; break;
7964 		default: abort();
7965 	}
7966 
7967 	/* test the source exists */
7968 	zfs_handle_t *zhp;
7969 	zhp = zfs_open(g_zfs, source, source_type);
7970 	if (zhp == NULL)
7971 		goto usage;
7972 	zfs_close(zhp);
7973 
7974 	nvl = fnvlist_alloc();
7975 	fnvlist_add_string(nvl, bookname, source);
7976 	ret = lzc_bookmark(nvl, NULL);
7977 	fnvlist_free(nvl);
7978 
7979 	if (ret != 0) {
7980 		const char *err_msg = NULL;
7981 		char errbuf[1024];
7982 
7983 		(void) snprintf(errbuf, sizeof (errbuf),
7984 		    dgettext(TEXT_DOMAIN,
7985 		    "cannot create bookmark '%s'"), bookname);
7986 
7987 		switch (ret) {
7988 		case EXDEV:
7989 			err_msg = "bookmark is in a different pool";
7990 			break;
7991 		case ZFS_ERR_BOOKMARK_SOURCE_NOT_ANCESTOR:
7992 			err_msg = "source is not an ancestor of the "
7993 			    "new bookmark's dataset";
7994 			break;
7995 		case EEXIST:
7996 			err_msg = "bookmark exists";
7997 			break;
7998 		case EINVAL:
7999 			err_msg = "invalid argument";
8000 			break;
8001 		case ENOTSUP:
8002 			err_msg = "bookmark feature not enabled";
8003 			break;
8004 		case ENOSPC:
8005 			err_msg = "out of space";
8006 			break;
8007 		case ENOENT:
8008 			err_msg = "dataset does not exist";
8009 			break;
8010 		default:
8011 			(void) zfs_standard_error(g_zfs, ret, errbuf);
8012 			break;
8013 		}
8014 		if (err_msg != NULL) {
8015 			(void) fprintf(stderr, "%s: %s\n", errbuf,
8016 			    dgettext(TEXT_DOMAIN, err_msg));
8017 		}
8018 	}
8019 
8020 	return (ret != 0);
8021 
8022 usage:
8023 	usage(B_FALSE);
8024 	return (-1);
8025 }
8026 
8027 static int
8028 zfs_do_channel_program(int argc, char **argv)
8029 {
8030 	int ret, fd, c;
8031 	size_t progsize, progread;
8032 	nvlist_t *outnvl = NULL;
8033 	uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT;
8034 	uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT;
8035 	boolean_t sync_flag = B_TRUE, json_output = B_FALSE;
8036 	zpool_handle_t *zhp;
8037 
8038 	/* check options */
8039 	while ((c = getopt(argc, argv, "nt:m:j")) != -1) {
8040 		switch (c) {
8041 		case 't':
8042 		case 'm': {
8043 			uint64_t arg;
8044 			char *endp;
8045 
8046 			errno = 0;
8047 			arg = strtoull(optarg, &endp, 0);
8048 			if (errno != 0 || *endp != '\0') {
8049 				(void) fprintf(stderr, gettext(
8050 				    "invalid argument "
8051 				    "'%s': expected integer\n"), optarg);
8052 				goto usage;
8053 			}
8054 
8055 			if (c == 't') {
8056 				instrlimit = arg;
8057 			} else {
8058 				ASSERT3U(c, ==, 'm');
8059 				memlimit = arg;
8060 			}
8061 			break;
8062 		}
8063 		case 'n': {
8064 			sync_flag = B_FALSE;
8065 			break;
8066 		}
8067 		case 'j': {
8068 			json_output = B_TRUE;
8069 			break;
8070 		}
8071 		case '?':
8072 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
8073 			    optopt);
8074 			goto usage;
8075 		}
8076 	}
8077 
8078 	argc -= optind;
8079 	argv += optind;
8080 
8081 	if (argc < 2) {
8082 		(void) fprintf(stderr,
8083 		    gettext("invalid number of arguments\n"));
8084 		goto usage;
8085 	}
8086 
8087 	const char *poolname = argv[0];
8088 	const char *filename = argv[1];
8089 	if (strcmp(filename, "-") == 0) {
8090 		fd = 0;
8091 		filename = "standard input";
8092 	} else if ((fd = open(filename, O_RDONLY)) < 0) {
8093 		(void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
8094 		    filename, strerror(errno));
8095 		return (1);
8096 	}
8097 
8098 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
8099 		(void) fprintf(stderr, gettext("cannot open pool '%s'\n"),
8100 		    poolname);
8101 		if (fd != 0)
8102 			(void) close(fd);
8103 		return (1);
8104 	}
8105 	zpool_close(zhp);
8106 
8107 	/*
8108 	 * Read in the channel program, expanding the program buffer as
8109 	 * necessary.
8110 	 */
8111 	progread = 0;
8112 	progsize = 1024;
8113 	char *progbuf = safe_malloc(progsize);
8114 	do {
8115 		ret = read(fd, progbuf + progread, progsize - progread);
8116 		progread += ret;
8117 		if (progread == progsize && ret > 0) {
8118 			progsize *= 2;
8119 			progbuf = safe_realloc(progbuf, progsize);
8120 		}
8121 	} while (ret > 0);
8122 
8123 	if (fd != 0)
8124 		(void) close(fd);
8125 	if (ret < 0) {
8126 		free(progbuf);
8127 		(void) fprintf(stderr,
8128 		    gettext("cannot read '%s': %s\n"),
8129 		    filename, strerror(errno));
8130 		return (1);
8131 	}
8132 	progbuf[progread] = '\0';
8133 
8134 	/*
8135 	 * Any remaining arguments are passed as arguments to the lua script as
8136 	 * a string array:
8137 	 * {
8138 	 *	"argv" -> [ "arg 1", ... "arg n" ],
8139 	 * }
8140 	 */
8141 	nvlist_t *argnvl = fnvlist_alloc();
8142 	fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV,
8143 	    (const char **)argv + 2, argc - 2);
8144 
8145 	if (sync_flag) {
8146 		ret = lzc_channel_program(poolname, progbuf,
8147 		    instrlimit, memlimit, argnvl, &outnvl);
8148 	} else {
8149 		ret = lzc_channel_program_nosync(poolname, progbuf,
8150 		    instrlimit, memlimit, argnvl, &outnvl);
8151 	}
8152 
8153 	if (ret != 0) {
8154 		/*
8155 		 * On error, report the error message handed back by lua if one
8156 		 * exists.  Otherwise, generate an appropriate error message,
8157 		 * falling back on strerror() for an unexpected return code.
8158 		 */
8159 		const char *errstring = NULL;
8160 		const char *msg = gettext("Channel program execution failed");
8161 		uint64_t instructions = 0;
8162 		if (outnvl != NULL && nvlist_exists(outnvl, ZCP_RET_ERROR)) {
8163 			const char *es = NULL;
8164 			(void) nvlist_lookup_string(outnvl,
8165 			    ZCP_RET_ERROR, &es);
8166 			if (es == NULL)
8167 				errstring = strerror(ret);
8168 			else
8169 				errstring = es;
8170 			if (ret == ETIME) {
8171 				(void) nvlist_lookup_uint64(outnvl,
8172 				    ZCP_ARG_INSTRLIMIT, &instructions);
8173 			}
8174 		} else {
8175 			switch (ret) {
8176 			case EINVAL:
8177 				errstring =
8178 				    "Invalid instruction or memory limit.";
8179 				break;
8180 			case ENOMEM:
8181 				errstring = "Return value too large.";
8182 				break;
8183 			case ENOSPC:
8184 				errstring = "Memory limit exhausted.";
8185 				break;
8186 			case ETIME:
8187 				errstring = "Timed out.";
8188 				break;
8189 			case EPERM:
8190 				errstring = "Permission denied. Channel "
8191 				    "programs must be run as root.";
8192 				break;
8193 			default:
8194 				(void) zfs_standard_error(g_zfs, ret, msg);
8195 			}
8196 		}
8197 		if (errstring != NULL)
8198 			(void) fprintf(stderr, "%s:\n%s\n", msg, errstring);
8199 
8200 		if (ret == ETIME && instructions != 0)
8201 			(void) fprintf(stderr,
8202 			    gettext("%llu Lua instructions\n"),
8203 			    (u_longlong_t)instructions);
8204 	} else {
8205 		if (json_output) {
8206 			(void) nvlist_print_json(stdout, outnvl);
8207 		} else if (nvlist_empty(outnvl)) {
8208 			(void) fprintf(stdout, gettext("Channel program fully "
8209 			    "executed and did not produce output.\n"));
8210 		} else {
8211 			(void) fprintf(stdout, gettext("Channel program fully "
8212 			    "executed and produced output:\n"));
8213 			dump_nvlist(outnvl, 4);
8214 		}
8215 	}
8216 
8217 	free(progbuf);
8218 	fnvlist_free(outnvl);
8219 	fnvlist_free(argnvl);
8220 	return (ret != 0);
8221 
8222 usage:
8223 	usage(B_FALSE);
8224 	return (-1);
8225 }
8226 
8227 
8228 typedef struct loadkey_cbdata {
8229 	boolean_t cb_loadkey;
8230 	boolean_t cb_recursive;
8231 	boolean_t cb_noop;
8232 	char *cb_keylocation;
8233 	uint64_t cb_numfailed;
8234 	uint64_t cb_numattempted;
8235 } loadkey_cbdata_t;
8236 
8237 static int
8238 load_key_callback(zfs_handle_t *zhp, void *data)
8239 {
8240 	int ret;
8241 	boolean_t is_encroot;
8242 	loadkey_cbdata_t *cb = data;
8243 	uint64_t keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
8244 
8245 	/*
8246 	 * If we are working recursively, we want to skip loading / unloading
8247 	 * keys for non-encryption roots and datasets whose keys are already
8248 	 * in the desired end-state.
8249 	 */
8250 	if (cb->cb_recursive) {
8251 		ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
8252 		if (ret != 0)
8253 			return (ret);
8254 		if (!is_encroot)
8255 			return (0);
8256 
8257 		if ((cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_AVAILABLE) ||
8258 		    (!cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_UNAVAILABLE))
8259 			return (0);
8260 	}
8261 
8262 	cb->cb_numattempted++;
8263 
8264 	if (cb->cb_loadkey)
8265 		ret = zfs_crypto_load_key(zhp, cb->cb_noop, cb->cb_keylocation);
8266 	else
8267 		ret = zfs_crypto_unload_key(zhp);
8268 
8269 	if (ret != 0) {
8270 		cb->cb_numfailed++;
8271 		return (ret);
8272 	}
8273 
8274 	return (0);
8275 }
8276 
8277 static int
8278 load_unload_keys(int argc, char **argv, boolean_t loadkey)
8279 {
8280 	int c, ret = 0, flags = 0;
8281 	boolean_t do_all = B_FALSE;
8282 	loadkey_cbdata_t cb = { 0 };
8283 
8284 	cb.cb_loadkey = loadkey;
8285 
8286 	while ((c = getopt(argc, argv, "anrL:")) != -1) {
8287 		/* noop and alternate keylocations only apply to zfs load-key */
8288 		if (loadkey) {
8289 			switch (c) {
8290 			case 'n':
8291 				cb.cb_noop = B_TRUE;
8292 				continue;
8293 			case 'L':
8294 				cb.cb_keylocation = optarg;
8295 				continue;
8296 			default:
8297 				break;
8298 			}
8299 		}
8300 
8301 		switch (c) {
8302 		case 'a':
8303 			do_all = B_TRUE;
8304 			cb.cb_recursive = B_TRUE;
8305 			break;
8306 		case 'r':
8307 			flags |= ZFS_ITER_RECURSE;
8308 			cb.cb_recursive = B_TRUE;
8309 			break;
8310 		default:
8311 			(void) fprintf(stderr,
8312 			    gettext("invalid option '%c'\n"), optopt);
8313 			usage(B_FALSE);
8314 		}
8315 	}
8316 
8317 	argc -= optind;
8318 	argv += optind;
8319 
8320 	if (!do_all && argc == 0) {
8321 		(void) fprintf(stderr,
8322 		    gettext("Missing dataset argument or -a option\n"));
8323 		usage(B_FALSE);
8324 	}
8325 
8326 	if (do_all && argc != 0) {
8327 		(void) fprintf(stderr,
8328 		    gettext("Cannot specify dataset with -a option\n"));
8329 		usage(B_FALSE);
8330 	}
8331 
8332 	if (cb.cb_recursive && cb.cb_keylocation != NULL &&
8333 	    strcmp(cb.cb_keylocation, "prompt") != 0) {
8334 		(void) fprintf(stderr, gettext("alternate keylocation may only "
8335 		    "be 'prompt' with -r or -a\n"));
8336 		usage(B_FALSE);
8337 	}
8338 
8339 	ret = zfs_for_each(argc, argv, flags,
8340 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, NULL, NULL, 0,
8341 	    load_key_callback, &cb);
8342 
8343 	if (cb.cb_noop || (cb.cb_recursive && cb.cb_numattempted != 0)) {
8344 		(void) printf(gettext("%llu / %llu key(s) successfully %s\n"),
8345 		    (u_longlong_t)(cb.cb_numattempted - cb.cb_numfailed),
8346 		    (u_longlong_t)cb.cb_numattempted,
8347 		    loadkey ? (cb.cb_noop ? "verified" : "loaded") :
8348 		    "unloaded");
8349 	}
8350 
8351 	if (cb.cb_numfailed != 0)
8352 		ret = -1;
8353 
8354 	return (ret);
8355 }
8356 
8357 static int
8358 zfs_do_load_key(int argc, char **argv)
8359 {
8360 	return (load_unload_keys(argc, argv, B_TRUE));
8361 }
8362 
8363 
8364 static int
8365 zfs_do_unload_key(int argc, char **argv)
8366 {
8367 	return (load_unload_keys(argc, argv, B_FALSE));
8368 }
8369 
8370 static int
8371 zfs_do_change_key(int argc, char **argv)
8372 {
8373 	int c, ret;
8374 	uint64_t keystatus;
8375 	boolean_t loadkey = B_FALSE, inheritkey = B_FALSE;
8376 	zfs_handle_t *zhp = NULL;
8377 	nvlist_t *props = fnvlist_alloc();
8378 
8379 	while ((c = getopt(argc, argv, "lio:")) != -1) {
8380 		switch (c) {
8381 		case 'l':
8382 			loadkey = B_TRUE;
8383 			break;
8384 		case 'i':
8385 			inheritkey = B_TRUE;
8386 			break;
8387 		case 'o':
8388 			if (!parseprop(props, optarg)) {
8389 				nvlist_free(props);
8390 				return (1);
8391 			}
8392 			break;
8393 		default:
8394 			(void) fprintf(stderr,
8395 			    gettext("invalid option '%c'\n"), optopt);
8396 			usage(B_FALSE);
8397 		}
8398 	}
8399 
8400 	if (inheritkey && !nvlist_empty(props)) {
8401 		(void) fprintf(stderr,
8402 		    gettext("Properties not allowed for inheriting\n"));
8403 		usage(B_FALSE);
8404 	}
8405 
8406 	argc -= optind;
8407 	argv += optind;
8408 
8409 	if (argc < 1) {
8410 		(void) fprintf(stderr, gettext("Missing dataset argument\n"));
8411 		usage(B_FALSE);
8412 	}
8413 
8414 	if (argc > 1) {
8415 		(void) fprintf(stderr, gettext("Too many arguments\n"));
8416 		usage(B_FALSE);
8417 	}
8418 
8419 	zhp = zfs_open(g_zfs, argv[argc - 1],
8420 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
8421 	if (zhp == NULL)
8422 		usage(B_FALSE);
8423 
8424 	if (loadkey) {
8425 		keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
8426 		if (keystatus != ZFS_KEYSTATUS_AVAILABLE) {
8427 			ret = zfs_crypto_load_key(zhp, B_FALSE, NULL);
8428 			if (ret != 0) {
8429 				nvlist_free(props);
8430 				zfs_close(zhp);
8431 				return (-1);
8432 			}
8433 		}
8434 
8435 		/* refresh the properties so the new keystatus is visible */
8436 		zfs_refresh_properties(zhp);
8437 	}
8438 
8439 	ret = zfs_crypto_rewrap(zhp, props, inheritkey);
8440 	if (ret != 0) {
8441 		nvlist_free(props);
8442 		zfs_close(zhp);
8443 		return (-1);
8444 	}
8445 
8446 	nvlist_free(props);
8447 	zfs_close(zhp);
8448 	return (0);
8449 }
8450 
8451 /*
8452  * 1) zfs project [-d|-r] <file|directory ...>
8453  *    List project ID and inherit flag of file(s) or directories.
8454  *    -d: List the directory itself, not its children.
8455  *    -r: List subdirectories recursively.
8456  *
8457  * 2) zfs project -C [-k] [-r] <file|directory ...>
8458  *    Clear project inherit flag and/or ID on the file(s) or directories.
8459  *    -k: Keep the project ID unchanged. If not specified, the project ID
8460  *	  will be reset as zero.
8461  *    -r: Clear on subdirectories recursively.
8462  *
8463  * 3) zfs project -c [-0] [-d|-r] [-p id] <file|directory ...>
8464  *    Check project ID and inherit flag on the file(s) or directories,
8465  *    report the outliers.
8466  *    -0: Print file name followed by a NUL instead of newline.
8467  *    -d: Check the directory itself, not its children.
8468  *    -p: Specify the referenced ID for comparing with the target file(s)
8469  *	  or directories' project IDs. If not specified, the target (top)
8470  *	  directory's project ID will be used as the referenced one.
8471  *    -r: Check subdirectories recursively.
8472  *
8473  * 4) zfs project [-p id] [-r] [-s] <file|directory ...>
8474  *    Set project ID and/or inherit flag on the file(s) or directories.
8475  *    -p: Set the project ID as the given id.
8476  *    -r: Set on subdirectories recursively. If not specify "-p" option,
8477  *	  it will use top-level directory's project ID as the given id,
8478  *	  then set both project ID and inherit flag on all descendants
8479  *	  of the top-level directory.
8480  *    -s: Set project inherit flag.
8481  */
8482 static int
8483 zfs_do_project(int argc, char **argv)
8484 {
8485 	zfs_project_control_t zpc = {
8486 		.zpc_expected_projid = ZFS_INVALID_PROJID,
8487 		.zpc_op = ZFS_PROJECT_OP_DEFAULT,
8488 		.zpc_dironly = B_FALSE,
8489 		.zpc_keep_projid = B_FALSE,
8490 		.zpc_newline = B_TRUE,
8491 		.zpc_recursive = B_FALSE,
8492 		.zpc_set_flag = B_FALSE,
8493 	};
8494 	int ret = 0, c;
8495 
8496 	if (argc < 2)
8497 		usage(B_FALSE);
8498 
8499 	while ((c = getopt(argc, argv, "0Ccdkp:rs")) != -1) {
8500 		switch (c) {
8501 		case '0':
8502 			zpc.zpc_newline = B_FALSE;
8503 			break;
8504 		case 'C':
8505 			if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
8506 				(void) fprintf(stderr, gettext("cannot "
8507 				    "specify '-C' '-c' '-s' together\n"));
8508 				usage(B_FALSE);
8509 			}
8510 
8511 			zpc.zpc_op = ZFS_PROJECT_OP_CLEAR;
8512 			break;
8513 		case 'c':
8514 			if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
8515 				(void) fprintf(stderr, gettext("cannot "
8516 				    "specify '-C' '-c' '-s' together\n"));
8517 				usage(B_FALSE);
8518 			}
8519 
8520 			zpc.zpc_op = ZFS_PROJECT_OP_CHECK;
8521 			break;
8522 		case 'd':
8523 			zpc.zpc_dironly = B_TRUE;
8524 			/* overwrite "-r" option */
8525 			zpc.zpc_recursive = B_FALSE;
8526 			break;
8527 		case 'k':
8528 			zpc.zpc_keep_projid = B_TRUE;
8529 			break;
8530 		case 'p': {
8531 			char *endptr;
8532 
8533 			errno = 0;
8534 			zpc.zpc_expected_projid = strtoull(optarg, &endptr, 0);
8535 			if (errno != 0 || *endptr != '\0') {
8536 				(void) fprintf(stderr,
8537 				    gettext("project ID must be less than "
8538 				    "%u\n"), UINT32_MAX);
8539 				usage(B_FALSE);
8540 			}
8541 			if (zpc.zpc_expected_projid >= UINT32_MAX) {
8542 				(void) fprintf(stderr,
8543 				    gettext("invalid project ID\n"));
8544 				usage(B_FALSE);
8545 			}
8546 			break;
8547 		}
8548 		case 'r':
8549 			zpc.zpc_recursive = B_TRUE;
8550 			/* overwrite "-d" option */
8551 			zpc.zpc_dironly = B_FALSE;
8552 			break;
8553 		case 's':
8554 			if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
8555 				(void) fprintf(stderr, gettext("cannot "
8556 				    "specify '-C' '-c' '-s' together\n"));
8557 				usage(B_FALSE);
8558 			}
8559 
8560 			zpc.zpc_set_flag = B_TRUE;
8561 			zpc.zpc_op = ZFS_PROJECT_OP_SET;
8562 			break;
8563 		default:
8564 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
8565 			    optopt);
8566 			usage(B_FALSE);
8567 		}
8568 	}
8569 
8570 	if (zpc.zpc_op == ZFS_PROJECT_OP_DEFAULT) {
8571 		if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID)
8572 			zpc.zpc_op = ZFS_PROJECT_OP_SET;
8573 		else
8574 			zpc.zpc_op = ZFS_PROJECT_OP_LIST;
8575 	}
8576 
8577 	switch (zpc.zpc_op) {
8578 	case ZFS_PROJECT_OP_LIST:
8579 		if (zpc.zpc_keep_projid) {
8580 			(void) fprintf(stderr,
8581 			    gettext("'-k' is only valid together with '-C'\n"));
8582 			usage(B_FALSE);
8583 		}
8584 		if (!zpc.zpc_newline) {
8585 			(void) fprintf(stderr,
8586 			    gettext("'-0' is only valid together with '-c'\n"));
8587 			usage(B_FALSE);
8588 		}
8589 		break;
8590 	case ZFS_PROJECT_OP_CHECK:
8591 		if (zpc.zpc_keep_projid) {
8592 			(void) fprintf(stderr,
8593 			    gettext("'-k' is only valid together with '-C'\n"));
8594 			usage(B_FALSE);
8595 		}
8596 		break;
8597 	case ZFS_PROJECT_OP_CLEAR:
8598 		if (zpc.zpc_dironly) {
8599 			(void) fprintf(stderr,
8600 			    gettext("'-d' is useless together with '-C'\n"));
8601 			usage(B_FALSE);
8602 		}
8603 		if (!zpc.zpc_newline) {
8604 			(void) fprintf(stderr,
8605 			    gettext("'-0' is only valid together with '-c'\n"));
8606 			usage(B_FALSE);
8607 		}
8608 		if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID) {
8609 			(void) fprintf(stderr,
8610 			    gettext("'-p' is useless together with '-C'\n"));
8611 			usage(B_FALSE);
8612 		}
8613 		break;
8614 	case ZFS_PROJECT_OP_SET:
8615 		if (zpc.zpc_dironly) {
8616 			(void) fprintf(stderr,
8617 			    gettext("'-d' is useless for set project ID and/or "
8618 			    "inherit flag\n"));
8619 			usage(B_FALSE);
8620 		}
8621 		if (zpc.zpc_keep_projid) {
8622 			(void) fprintf(stderr,
8623 			    gettext("'-k' is only valid together with '-C'\n"));
8624 			usage(B_FALSE);
8625 		}
8626 		if (!zpc.zpc_newline) {
8627 			(void) fprintf(stderr,
8628 			    gettext("'-0' is only valid together with '-c'\n"));
8629 			usage(B_FALSE);
8630 		}
8631 		break;
8632 	default:
8633 		ASSERT(0);
8634 		break;
8635 	}
8636 
8637 	argv += optind;
8638 	argc -= optind;
8639 	if (argc == 0) {
8640 		(void) fprintf(stderr,
8641 		    gettext("missing file or directory target(s)\n"));
8642 		usage(B_FALSE);
8643 	}
8644 
8645 	for (int i = 0; i < argc; i++) {
8646 		int err;
8647 
8648 		err = zfs_project_handle(argv[i], &zpc);
8649 		if (err && !ret)
8650 			ret = err;
8651 	}
8652 
8653 	return (ret);
8654 }
8655 
8656 static int
8657 zfs_do_wait(int argc, char **argv)
8658 {
8659 	boolean_t enabled[ZFS_WAIT_NUM_ACTIVITIES];
8660 	int error = 0, i;
8661 	int c;
8662 
8663 	/* By default, wait for all types of activity. */
8664 	for (i = 0; i < ZFS_WAIT_NUM_ACTIVITIES; i++)
8665 		enabled[i] = B_TRUE;
8666 
8667 	while ((c = getopt(argc, argv, "t:")) != -1) {
8668 		switch (c) {
8669 		case 't':
8670 			/* Reset activities array */
8671 			memset(&enabled, 0, sizeof (enabled));
8672 
8673 			for (char *tok; (tok = strsep(&optarg, ",")); ) {
8674 				static const char *const col_subopts[
8675 				    ZFS_WAIT_NUM_ACTIVITIES] = { "deleteq" };
8676 
8677 				for (i = 0; i < ARRAY_SIZE(col_subopts); ++i)
8678 					if (strcmp(tok, col_subopts[i]) == 0) {
8679 						enabled[i] = B_TRUE;
8680 						goto found;
8681 					}
8682 
8683 				(void) fprintf(stderr,
8684 				    gettext("invalid activity '%s'\n"), tok);
8685 				usage(B_FALSE);
8686 found:;
8687 			}
8688 			break;
8689 		case '?':
8690 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
8691 			    optopt);
8692 			usage(B_FALSE);
8693 		}
8694 	}
8695 
8696 	argv += optind;
8697 	argc -= optind;
8698 	if (argc < 1) {
8699 		(void) fprintf(stderr, gettext("missing 'filesystem' "
8700 		    "argument\n"));
8701 		usage(B_FALSE);
8702 	}
8703 	if (argc > 1) {
8704 		(void) fprintf(stderr, gettext("too many arguments\n"));
8705 		usage(B_FALSE);
8706 	}
8707 
8708 	zfs_handle_t *zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM);
8709 	if (zhp == NULL)
8710 		return (1);
8711 
8712 	for (;;) {
8713 		boolean_t missing = B_FALSE;
8714 		boolean_t any_waited = B_FALSE;
8715 
8716 		for (int i = 0; i < ZFS_WAIT_NUM_ACTIVITIES; i++) {
8717 			boolean_t waited;
8718 
8719 			if (!enabled[i])
8720 				continue;
8721 
8722 			error = zfs_wait_status(zhp, i, &missing, &waited);
8723 			if (error != 0 || missing)
8724 				break;
8725 
8726 			any_waited = (any_waited || waited);
8727 		}
8728 
8729 		if (error != 0 || missing || !any_waited)
8730 			break;
8731 	}
8732 
8733 	zfs_close(zhp);
8734 
8735 	return (error);
8736 }
8737 
8738 /*
8739  * Display version message
8740  */
8741 static int
8742 zfs_do_version(int argc, char **argv)
8743 {
8744 	(void) argc, (void) argv;
8745 	return (zfs_version_print() != 0);
8746 }
8747 
8748 /* Display documentation */
8749 static int
8750 zfs_do_help(int argc, char **argv)
8751 {
8752 	char page[MAXNAMELEN];
8753 	if (argc < 3 || strcmp(argv[2], "zfs") == 0)
8754 		strcpy(page, "zfs");
8755 	else if (strcmp(argv[2], "concepts") == 0 ||
8756 	    strcmp(argv[2], "props") == 0)
8757 		snprintf(page, sizeof (page), "zfs%s", argv[2]);
8758 	else
8759 		snprintf(page, sizeof (page), "zfs-%s", argv[2]);
8760 
8761 	execlp("man", "man", page, NULL);
8762 
8763 	fprintf(stderr, "couldn't run man program: %s", strerror(errno));
8764 	return (-1);
8765 }
8766 
8767 int
8768 main(int argc, char **argv)
8769 {
8770 	int ret = 0;
8771 	int i = 0;
8772 	const char *cmdname;
8773 	char **newargv;
8774 
8775 	(void) setlocale(LC_ALL, "");
8776 	(void) setlocale(LC_NUMERIC, "C");
8777 	(void) textdomain(TEXT_DOMAIN);
8778 
8779 	opterr = 0;
8780 
8781 	/*
8782 	 * Make sure the user has specified some command.
8783 	 */
8784 	if (argc < 2) {
8785 		(void) fprintf(stderr, gettext("missing command\n"));
8786 		usage(B_FALSE);
8787 	}
8788 
8789 	cmdname = argv[1];
8790 
8791 	/*
8792 	 * The 'umount' command is an alias for 'unmount'
8793 	 */
8794 	if (strcmp(cmdname, "umount") == 0)
8795 		cmdname = "unmount";
8796 
8797 	/*
8798 	 * The 'recv' command is an alias for 'receive'
8799 	 */
8800 	if (strcmp(cmdname, "recv") == 0)
8801 		cmdname = "receive";
8802 
8803 	/*
8804 	 * The 'snap' command is an alias for 'snapshot'
8805 	 */
8806 	if (strcmp(cmdname, "snap") == 0)
8807 		cmdname = "snapshot";
8808 
8809 	/*
8810 	 * Special case '-?'
8811 	 */
8812 	if ((strcmp(cmdname, "-?") == 0) ||
8813 	    (strcmp(cmdname, "--help") == 0))
8814 		usage(B_TRUE);
8815 
8816 	/*
8817 	 * Special case '-V|--version'
8818 	 */
8819 	if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0))
8820 		return (zfs_do_version(argc, argv));
8821 
8822 	/*
8823 	 * Special case 'help'
8824 	 */
8825 	if (strcmp(cmdname, "help") == 0)
8826 		return (zfs_do_help(argc, argv));
8827 
8828 	if ((g_zfs = libzfs_init()) == NULL) {
8829 		(void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
8830 		return (1);
8831 	}
8832 
8833 	zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
8834 
8835 	libzfs_print_on_error(g_zfs, B_TRUE);
8836 
8837 	zfs_setproctitle_init(argc, argv, environ);
8838 
8839 	/*
8840 	 * Many commands modify input strings for string parsing reasons.
8841 	 * We create a copy to protect the original argv.
8842 	 */
8843 	newargv = safe_malloc((argc + 1) * sizeof (newargv[0]));
8844 	for (i = 0; i < argc; i++)
8845 		newargv[i] = strdup(argv[i]);
8846 	newargv[argc] = NULL;
8847 
8848 	/*
8849 	 * Run the appropriate command.
8850 	 */
8851 	libzfs_mnttab_cache(g_zfs, B_TRUE);
8852 	if (find_command_idx(cmdname, &i) == 0) {
8853 		current_command = &command_table[i];
8854 		ret = command_table[i].func(argc - 1, newargv + 1);
8855 	} else if (strchr(cmdname, '=') != NULL) {
8856 		verify(find_command_idx("set", &i) == 0);
8857 		current_command = &command_table[i];
8858 		ret = command_table[i].func(argc, newargv);
8859 	} else {
8860 		(void) fprintf(stderr, gettext("unrecognized "
8861 		    "command '%s'\n"), cmdname);
8862 		usage(B_FALSE);
8863 		ret = 1;
8864 	}
8865 
8866 	for (i = 0; i < argc; i++)
8867 		free(newargv[i]);
8868 	free(newargv);
8869 
8870 	if (ret == 0 && log_history)
8871 		(void) zpool_log_history(g_zfs, history_str);
8872 
8873 	libzfs_fini(g_zfs);
8874 
8875 	/*
8876 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8877 	 * for the purposes of running ::findleaks.
8878 	 */
8879 	if (getenv("ZFS_ABORT") != NULL) {
8880 		(void) printf("dumping core by request\n");
8881 		abort();
8882 	}
8883 
8884 	return (ret);
8885 }
8886 
8887 /*
8888  * zfs zone nsfile filesystem
8889  *
8890  * Add or delete the given dataset to/from the namespace.
8891  */
8892 #ifdef __linux__
8893 static int
8894 zfs_do_zone_impl(int argc, char **argv, boolean_t attach)
8895 {
8896 	zfs_handle_t *zhp;
8897 	int ret;
8898 
8899 	if (argc < 3) {
8900 		(void) fprintf(stderr, gettext("missing argument(s)\n"));
8901 		usage(B_FALSE);
8902 	}
8903 	if (argc > 3) {
8904 		(void) fprintf(stderr, gettext("too many arguments\n"));
8905 		usage(B_FALSE);
8906 	}
8907 
8908 	zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
8909 	if (zhp == NULL)
8910 		return (1);
8911 
8912 	ret = (zfs_userns(zhp, argv[1], attach) != 0);
8913 
8914 	zfs_close(zhp);
8915 	return (ret);
8916 }
8917 
8918 static int
8919 zfs_do_zone(int argc, char **argv)
8920 {
8921 	return (zfs_do_zone_impl(argc, argv, B_TRUE));
8922 }
8923 
8924 static int
8925 zfs_do_unzone(int argc, char **argv)
8926 {
8927 	return (zfs_do_zone_impl(argc, argv, B_FALSE));
8928 }
8929 #endif
8930 
8931 #ifdef __FreeBSD__
8932 #include <sys/jail.h>
8933 #include <jail.h>
8934 /*
8935  * Attach/detach the given dataset to/from the given jail
8936  */
8937 static int
8938 zfs_do_jail_impl(int argc, char **argv, boolean_t attach)
8939 {
8940 	zfs_handle_t *zhp;
8941 	int jailid, ret;
8942 
8943 	/* check number of arguments */
8944 	if (argc < 3) {
8945 		(void) fprintf(stderr, gettext("missing argument(s)\n"));
8946 		usage(B_FALSE);
8947 	}
8948 	if (argc > 3) {
8949 		(void) fprintf(stderr, gettext("too many arguments\n"));
8950 		usage(B_FALSE);
8951 	}
8952 
8953 	jailid = jail_getid(argv[1]);
8954 	if (jailid < 0) {
8955 		(void) fprintf(stderr, gettext("invalid jail id or name\n"));
8956 		usage(B_FALSE);
8957 	}
8958 
8959 	zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
8960 	if (zhp == NULL)
8961 		return (1);
8962 
8963 	ret = (zfs_jail(zhp, jailid, attach) != 0);
8964 
8965 	zfs_close(zhp);
8966 	return (ret);
8967 }
8968 
8969 /*
8970  * zfs jail jailid filesystem
8971  *
8972  * Attach the given dataset to the given jail
8973  */
8974 static int
8975 zfs_do_jail(int argc, char **argv)
8976 {
8977 	return (zfs_do_jail_impl(argc, argv, B_TRUE));
8978 }
8979 
8980 /*
8981  * zfs unjail jailid filesystem
8982  *
8983  * Detach the given dataset from the given jail
8984  */
8985 static int
8986 zfs_do_unjail(int argc, char **argv)
8987 {
8988 	return (zfs_do_jail_impl(argc, argv, B_FALSE));
8989 }
8990 #endif
8991