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