xref: /illumos-gate/usr/src/cmd/zfs/zfs_main.c (revision bb0ade0978a02d3fe0b0165cd4725fdcb593fbfb)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <assert.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <libgen.h>
33 #include <libintl.h>
34 #include <libuutil.h>
35 #include <libnvpair.h>
36 #include <locale.h>
37 #include <stddef.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <strings.h>
41 #include <unistd.h>
42 #include <fcntl.h>
43 #include <zone.h>
44 #include <sys/mkdev.h>
45 #include <sys/mntent.h>
46 #include <sys/mnttab.h>
47 #include <sys/mount.h>
48 #include <sys/stat.h>
49 #include <sys/avl.h>
50 
51 #include <libzfs.h>
52 #include <libuutil.h>
53 
54 #include "zfs_iter.h"
55 #include "zfs_util.h"
56 
57 libzfs_handle_t *g_zfs;
58 
59 static FILE *mnttab_file;
60 static char history_str[HIS_MAX_RECORD_LEN];
61 
62 static int zfs_do_clone(int argc, char **argv);
63 static int zfs_do_create(int argc, char **argv);
64 static int zfs_do_destroy(int argc, char **argv);
65 static int zfs_do_get(int argc, char **argv);
66 static int zfs_do_inherit(int argc, char **argv);
67 static int zfs_do_list(int argc, char **argv);
68 static int zfs_do_mount(int argc, char **argv);
69 static int zfs_do_rename(int argc, char **argv);
70 static int zfs_do_rollback(int argc, char **argv);
71 static int zfs_do_set(int argc, char **argv);
72 static int zfs_do_upgrade(int argc, char **argv);
73 static int zfs_do_snapshot(int argc, char **argv);
74 static int zfs_do_unmount(int argc, char **argv);
75 static int zfs_do_share(int argc, char **argv);
76 static int zfs_do_unshare(int argc, char **argv);
77 static int zfs_do_send(int argc, char **argv);
78 static int zfs_do_receive(int argc, char **argv);
79 static int zfs_do_promote(int argc, char **argv);
80 static int zfs_do_allow(int argc, char **argv);
81 static int zfs_do_unallow(int argc, char **argv);
82 
83 /*
84  * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
85  */
86 
87 #ifdef DEBUG
88 const char *
89 _umem_debug_init(void)
90 {
91 	return ("default,verbose"); /* $UMEM_DEBUG setting */
92 }
93 
94 const char *
95 _umem_logging_init(void)
96 {
97 	return ("fail,contents"); /* $UMEM_LOGGING setting */
98 }
99 #endif
100 
101 typedef enum {
102 	HELP_CLONE,
103 	HELP_CREATE,
104 	HELP_DESTROY,
105 	HELP_GET,
106 	HELP_INHERIT,
107 	HELP_UPGRADE,
108 	HELP_LIST,
109 	HELP_MOUNT,
110 	HELP_PROMOTE,
111 	HELP_RECEIVE,
112 	HELP_RENAME,
113 	HELP_ROLLBACK,
114 	HELP_SEND,
115 	HELP_SET,
116 	HELP_SHARE,
117 	HELP_SNAPSHOT,
118 	HELP_UNMOUNT,
119 	HELP_UNSHARE,
120 	HELP_ALLOW,
121 	HELP_UNALLOW
122 } zfs_help_t;
123 
124 typedef struct zfs_command {
125 	const char	*name;
126 	int		(*func)(int argc, char **argv);
127 	zfs_help_t	usage;
128 } zfs_command_t;
129 
130 /*
131  * Master command table.  Each ZFS command has a name, associated function, and
132  * usage message.  The usage messages need to be internationalized, so we have
133  * to have a function to return the usage message based on a command index.
134  *
135  * These commands are organized according to how they are displayed in the usage
136  * message.  An empty command (one with a NULL name) indicates an empty line in
137  * the generic usage message.
138  */
139 static zfs_command_t command_table[] = {
140 	{ "create",	zfs_do_create,		HELP_CREATE		},
141 	{ "destroy",	zfs_do_destroy,		HELP_DESTROY		},
142 	{ NULL },
143 	{ "snapshot",	zfs_do_snapshot,	HELP_SNAPSHOT		},
144 	{ "rollback",	zfs_do_rollback,	HELP_ROLLBACK		},
145 	{ "clone",	zfs_do_clone,		HELP_CLONE		},
146 	{ "promote",	zfs_do_promote,		HELP_PROMOTE		},
147 	{ "rename",	zfs_do_rename,		HELP_RENAME		},
148 	{ NULL },
149 	{ "list",	zfs_do_list,		HELP_LIST		},
150 	{ NULL },
151 	{ "set",	zfs_do_set,		HELP_SET		},
152 	{ "get", 	zfs_do_get,		HELP_GET		},
153 	{ "inherit",	zfs_do_inherit,		HELP_INHERIT		},
154 	{ "upgrade",	zfs_do_upgrade,		HELP_UPGRADE		},
155 	{ NULL },
156 	{ "mount",	zfs_do_mount,		HELP_MOUNT		},
157 	{ "unmount",	zfs_do_unmount,		HELP_UNMOUNT		},
158 	{ "share",	zfs_do_share,		HELP_SHARE		},
159 	{ "unshare",	zfs_do_unshare,		HELP_UNSHARE		},
160 	{ NULL },
161 	{ "send",	zfs_do_send,		HELP_SEND		},
162 	{ "receive",	zfs_do_receive,		HELP_RECEIVE		},
163 	{ NULL },
164 	{ "allow",	zfs_do_allow,		HELP_ALLOW		},
165 	{ NULL },
166 	{ "unallow",	zfs_do_unallow,		HELP_UNALLOW		},
167 };
168 
169 #define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
170 
171 zfs_command_t *current_command;
172 
173 static const char *
174 get_usage(zfs_help_t idx)
175 {
176 	switch (idx) {
177 	case HELP_CLONE:
178 		return (gettext("\tclone [-p] [-o property=value] ... "
179 		    "<snapshot> <filesystem|volume>\n"));
180 	case HELP_CREATE:
181 		return (gettext("\tcreate [-p] [-o property=value] ... "
182 		    "<filesystem>\n"
183 		    "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
184 		    "-V <size> <volume>\n"));
185 	case HELP_DESTROY:
186 		return (gettext("\tdestroy [-rRf] "
187 		    "<filesystem|volume|snapshot>\n"));
188 	case HELP_GET:
189 		return (gettext("\tget [-rHp] [-o field[,...]] "
190 		    "[-s source[,...]]\n"
191 		    "\t    <\"all\" | property[,...]> "
192 		    "[filesystem|volume|snapshot] ...\n"));
193 	case HELP_INHERIT:
194 		return (gettext("\tinherit [-r] <property> "
195 		    "<filesystem|volume|snapshot> ...\n"));
196 	case HELP_UPGRADE:
197 		return (gettext("\tupgrade [-v]\n"
198 		    "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
199 	case HELP_LIST:
200 		return (gettext("\tlist [-rH] [-o property[,...]] "
201 		    "[-t type[,...]] [-s property] ...\n"
202 		    "\t    [-S property] ... "
203 		    "[filesystem|volume|snapshot] ...\n"));
204 	case HELP_MOUNT:
205 		return (gettext("\tmount\n"
206 		    "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
207 	case HELP_PROMOTE:
208 		return (gettext("\tpromote <clone-filesystem>\n"));
209 	case HELP_RECEIVE:
210 		return (gettext("\treceive [-vnF] <filesystem|volume|"
211 		"snapshot>\n"
212 		"\treceive [-vnF] -d <filesystem>\n"));
213 	case HELP_RENAME:
214 		return (gettext("\trename <filesystem|volume|snapshot> "
215 		    "<filesystem|volume|snapshot>\n"
216 		    "\trename -p <filesystem|volume> <filesystem|volume>\n"
217 		    "\trename -r <snapshot> <snapshot>"));
218 	case HELP_ROLLBACK:
219 		return (gettext("\trollback [-rRf] <snapshot>\n"));
220 	case HELP_SEND:
221 		return (gettext("\tsend [-R] [-[iI] snapshot] <snapshot>\n"));
222 	case HELP_SET:
223 		return (gettext("\tset <property=value> "
224 		    "<filesystem|volume|snapshot> ...\n"));
225 	case HELP_SHARE:
226 		return (gettext("\tshare <-a | filesystem>\n"));
227 	case HELP_SNAPSHOT:
228 		return (gettext("\tsnapshot [-r] [-o property=value] ... "
229 		    "<filesystem@snapname|volume@snapname>\n"));
230 	case HELP_UNMOUNT:
231 		return (gettext("\tunmount [-f] "
232 		    "<-a | filesystem|mountpoint>\n"));
233 	case HELP_UNSHARE:
234 		return (gettext("\tunshare [-f] "
235 		    "<-a | filesystem|mountpoint>\n"));
236 	case HELP_ALLOW:
237 		return (gettext("\tallow [-ldug] "
238 		    "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
239 		    "\t    <filesystem|volume>\n"
240 		    "\tallow [-ld] -e <perm|@setname>[,...] "
241 		    "<filesystem|volume>\n"
242 		    "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
243 		    "\tallow -s @setname <perm|@setname>[,...] "
244 		    "<filesystem|volume>\n"));
245 	case HELP_UNALLOW:
246 		return (gettext("\tunallow [-rldug] "
247 		    "<\"everyone\"|user|group>[,...]\n"
248 		    "\t    [<perm|@setname>[,...]] <filesystem|volume>\n"
249 		    "\tunallow [-rld] -e [<perm|@setname>[,...]] "
250 		    "<filesystem|volume>\n"
251 		    "\tunallow [-r] -c [<perm|@setname>[,...]] "
252 		    "<filesystem|volume>\n"
253 		    "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
254 		    "<filesystem|volume>\n"));
255 	}
256 
257 	abort();
258 	/* NOTREACHED */
259 }
260 
261 /*
262  * Utility function to guarantee malloc() success.
263  */
264 void *
265 safe_malloc(size_t size)
266 {
267 	void *data;
268 
269 	if ((data = calloc(1, size)) == NULL) {
270 		(void) fprintf(stderr, "internal error: out of memory\n");
271 		exit(1);
272 	}
273 
274 	return (data);
275 }
276 
277 /*
278  * Callback routine that will print out information for each of
279  * the properties.
280  */
281 static int
282 usage_prop_cb(int prop, void *cb)
283 {
284 	FILE *fp = cb;
285 
286 	(void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
287 
288 	if (zfs_prop_readonly(prop))
289 		(void) fprintf(fp, " NO    ");
290 	else
291 		(void) fprintf(fp, "YES    ");
292 
293 	if (zfs_prop_inheritable(prop))
294 		(void) fprintf(fp, "  YES   ");
295 	else
296 		(void) fprintf(fp, "   NO   ");
297 
298 	if (zfs_prop_values(prop) == NULL)
299 		(void) fprintf(fp, "-\n");
300 	else
301 		(void) fprintf(fp, "%s\n", zfs_prop_values(prop));
302 
303 	return (ZPROP_CONT);
304 }
305 
306 /*
307  * Display usage message.  If we're inside a command, display only the usage for
308  * that command.  Otherwise, iterate over the entire command table and display
309  * a complete usage message.
310  */
311 static void
312 usage(boolean_t requested)
313 {
314 	int i;
315 	boolean_t show_properties = B_FALSE;
316 	boolean_t show_permissions = B_FALSE;
317 	FILE *fp = requested ? stdout : stderr;
318 
319 	if (current_command == NULL) {
320 
321 		(void) fprintf(fp, gettext("usage: zfs command args ...\n"));
322 		(void) fprintf(fp,
323 		    gettext("where 'command' is one of the following:\n\n"));
324 
325 		for (i = 0; i < NCOMMAND; i++) {
326 			if (command_table[i].name == NULL)
327 				(void) fprintf(fp, "\n");
328 			else
329 				(void) fprintf(fp, "%s",
330 				    get_usage(command_table[i].usage));
331 		}
332 
333 		(void) fprintf(fp, gettext("\nEach dataset is of the form: "
334 		    "pool/[dataset/]*dataset[@name]\n"));
335 	} else {
336 		(void) fprintf(fp, gettext("usage:\n"));
337 		(void) fprintf(fp, "%s", get_usage(current_command->usage));
338 	}
339 
340 	if (current_command != NULL &&
341 	    (strcmp(current_command->name, "set") == 0 ||
342 	    strcmp(current_command->name, "get") == 0 ||
343 	    strcmp(current_command->name, "inherit") == 0 ||
344 	    strcmp(current_command->name, "list") == 0))
345 		show_properties = B_TRUE;
346 
347 	if (current_command != NULL &&
348 	    (strcmp(current_command->name, "allow") == 0 ||
349 	    strcmp(current_command->name, "unallow") == 0))
350 		show_permissions = B_TRUE;
351 
352 	if (show_properties) {
353 
354 		(void) fprintf(fp,
355 		    gettext("\nThe following properties are supported:\n"));
356 
357 		(void) fprintf(fp, "\n\t%-14s %s  %s   %s\n\n",
358 		    "PROPERTY", "EDIT", "INHERIT", "VALUES");
359 
360 		/* Iterate over all properties */
361 		(void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
362 		    ZFS_TYPE_DATASET);
363 
364 		(void) fprintf(fp, gettext("\nSizes are specified in bytes "
365 		    "with standard units such as K, M, G, etc.\n"));
366 		(void) fprintf(fp, gettext("\n\nUser-defined properties can "
367 		    "be specified by using a name containing a colon (:).\n"));
368 
369 	} else if (show_permissions) {
370 		(void) fprintf(fp,
371 		    gettext("\nThe following permissions are supported:\n"));
372 
373 		zfs_deleg_permissions();
374 	} else {
375 		/*
376 		 * TRANSLATION NOTE:
377 		 * "zfs set|get" must not be localised this is the
378 		 * command name and arguments.
379 		 */
380 
381 		(void) fprintf(fp,
382 		    gettext("\nFor the property list, run: zfs set|get\n"));
383 
384 		(void) fprintf(fp,
385 		    gettext("\nFor the delegated permission list, run:"
386 		    " zfs allow|unallow\n"));
387 	}
388 
389 	/*
390 	 * See comments at end of main().
391 	 */
392 	if (getenv("ZFS_ABORT") != NULL) {
393 		(void) printf("dumping core by request\n");
394 		abort();
395 	}
396 
397 	exit(requested ? 0 : 2);
398 }
399 
400 static int
401 parseprop(nvlist_t *props)
402 {
403 	char *propname = optarg;
404 	char *propval, *strval;
405 
406 	if ((propval = strchr(propname, '=')) == NULL) {
407 		(void) fprintf(stderr, gettext("missing "
408 		    "'=' for -o option\n"));
409 		return (-1);
410 	}
411 	*propval = '\0';
412 	propval++;
413 	if (nvlist_lookup_string(props, propname, &strval) == 0) {
414 		(void) fprintf(stderr, gettext("property '%s' "
415 		    "specified multiple times\n"), propname);
416 		return (-1);
417 	}
418 	if (nvlist_add_string(props, propname, propval) != 0) {
419 		(void) fprintf(stderr, gettext("internal "
420 		    "error: out of memory\n"));
421 		return (-1);
422 	}
423 	return (0);
424 
425 }
426 
427 /*
428  * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
429  *
430  * Given an existing dataset, create a writable copy whose initial contents
431  * are the same as the source.  The newly created dataset maintains a
432  * dependency on the original; the original cannot be destroyed so long as
433  * the clone exists.
434  *
435  * The '-p' flag creates all the non-existing ancestors of the target first.
436  */
437 static int
438 zfs_do_clone(int argc, char **argv)
439 {
440 	zfs_handle_t *zhp = NULL;
441 	boolean_t parents = B_FALSE;
442 	nvlist_t *props;
443 	int ret;
444 	int c;
445 
446 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
447 		(void) fprintf(stderr, gettext("internal error: "
448 		    "out of memory\n"));
449 		return (1);
450 	}
451 
452 	/* check options */
453 	while ((c = getopt(argc, argv, "o:p")) != -1) {
454 		switch (c) {
455 		case 'o':
456 			if (parseprop(props))
457 				return (1);
458 			break;
459 		case 'p':
460 			parents = B_TRUE;
461 			break;
462 		case '?':
463 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
464 			    optopt);
465 			goto usage;
466 		}
467 	}
468 
469 	argc -= optind;
470 	argv += optind;
471 
472 	/* check number of arguments */
473 	if (argc < 1) {
474 		(void) fprintf(stderr, gettext("missing source dataset "
475 		    "argument\n"));
476 		goto usage;
477 	}
478 	if (argc < 2) {
479 		(void) fprintf(stderr, gettext("missing target dataset "
480 		    "argument\n"));
481 		goto usage;
482 	}
483 	if (argc > 2) {
484 		(void) fprintf(stderr, gettext("too many arguments\n"));
485 		goto usage;
486 	}
487 
488 	/* open the source dataset */
489 	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
490 		return (1);
491 
492 	if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
493 	    ZFS_TYPE_VOLUME)) {
494 		/*
495 		 * Now create the ancestors of the target dataset.  If the
496 		 * target already exists and '-p' option was used we should not
497 		 * complain.
498 		 */
499 		if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
500 		    ZFS_TYPE_VOLUME))
501 			return (0);
502 		if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
503 			return (1);
504 	}
505 
506 	/* pass to libzfs */
507 	ret = zfs_clone(zhp, argv[1], props);
508 
509 	/* create the mountpoint if necessary */
510 	if (ret == 0) {
511 		zfs_handle_t *clone;
512 
513 		clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
514 		if (clone != NULL) {
515 			if ((ret = zfs_mount(clone, NULL, 0)) == 0)
516 				ret = zfs_share(clone);
517 			zfs_close(clone);
518 		}
519 	}
520 
521 	zfs_close(zhp);
522 	nvlist_free(props);
523 
524 	return (!!ret);
525 
526 usage:
527 	if (zhp)
528 		zfs_close(zhp);
529 	nvlist_free(props);
530 	usage(B_FALSE);
531 	return (-1);
532 }
533 
534 /*
535  * zfs create [-p] [-o prop=value] ... fs
536  * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
537  *
538  * Create a new dataset.  This command can be used to create filesystems
539  * and volumes.  Snapshot creation is handled by 'zfs snapshot'.
540  * For volumes, the user must specify a size to be used.
541  *
542  * The '-s' flag applies only to volumes, and indicates that we should not try
543  * to set the reservation for this volume.  By default we set a reservation
544  * equal to the size for any volume.  For pools with SPA_VERSION >=
545  * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
546  *
547  * The '-p' flag creates all the non-existing ancestors of the target first.
548  */
549 static int
550 zfs_do_create(int argc, char **argv)
551 {
552 	zfs_type_t type = ZFS_TYPE_FILESYSTEM;
553 	zfs_handle_t *zhp = NULL;
554 	uint64_t volsize;
555 	int c;
556 	boolean_t noreserve = B_FALSE;
557 	boolean_t bflag = B_FALSE;
558 	boolean_t parents = B_FALSE;
559 	int ret = 1;
560 	nvlist_t *props;
561 	uint64_t intval;
562 	int canmount;
563 
564 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
565 		(void) fprintf(stderr, gettext("internal error: "
566 		    "out of memory\n"));
567 		return (1);
568 	}
569 
570 	/* check options */
571 	while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
572 		switch (c) {
573 		case 'V':
574 			type = ZFS_TYPE_VOLUME;
575 			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
576 				(void) fprintf(stderr, gettext("bad volume "
577 				    "size '%s': %s\n"), optarg,
578 				    libzfs_error_description(g_zfs));
579 				goto error;
580 			}
581 
582 			if (nvlist_add_uint64(props,
583 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
584 			    intval) != 0) {
585 				(void) fprintf(stderr, gettext("internal "
586 				    "error: out of memory\n"));
587 				goto error;
588 			}
589 			volsize = intval;
590 			break;
591 		case 'p':
592 			parents = B_TRUE;
593 			break;
594 		case 'b':
595 			bflag = B_TRUE;
596 			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
597 				(void) fprintf(stderr, gettext("bad volume "
598 				    "block size '%s': %s\n"), optarg,
599 				    libzfs_error_description(g_zfs));
600 				goto error;
601 			}
602 
603 			if (nvlist_add_uint64(props,
604 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
605 			    intval) != 0) {
606 				(void) fprintf(stderr, gettext("internal "
607 				    "error: out of memory\n"));
608 				goto error;
609 			}
610 			break;
611 		case 'o':
612 			if (parseprop(props))
613 				goto error;
614 			break;
615 		case 's':
616 			noreserve = B_TRUE;
617 			break;
618 		case ':':
619 			(void) fprintf(stderr, gettext("missing size "
620 			    "argument\n"));
621 			goto badusage;
622 			break;
623 		case '?':
624 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
625 			    optopt);
626 			goto badusage;
627 		}
628 	}
629 
630 	if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
631 		(void) fprintf(stderr, gettext("'-s' and '-b' can only be "
632 		    "used when creating a volume\n"));
633 		goto badusage;
634 	}
635 
636 	argc -= optind;
637 	argv += optind;
638 
639 	/* check number of arguments */
640 	if (argc == 0) {
641 		(void) fprintf(stderr, gettext("missing %s argument\n"),
642 		    zfs_type_to_name(type));
643 		goto badusage;
644 	}
645 	if (argc > 1) {
646 		(void) fprintf(stderr, gettext("too many arguments\n"));
647 		goto badusage;
648 	}
649 
650 	if (type == ZFS_TYPE_VOLUME && !noreserve) {
651 		zpool_handle_t *zpool_handle;
652 		uint64_t spa_version;
653 		char *p;
654 		zfs_prop_t resv_prop;
655 		char *strval;
656 
657 		if (p = strchr(argv[0], '/'))
658 			*p = '\0';
659 		zpool_handle = zpool_open(g_zfs, argv[0]);
660 		if (p != NULL)
661 			*p = '/';
662 		if (zpool_handle == NULL)
663 			goto error;
664 		spa_version = zpool_get_prop_int(zpool_handle,
665 		    ZPOOL_PROP_VERSION, NULL);
666 		zpool_close(zpool_handle);
667 		if (spa_version >= SPA_VERSION_REFRESERVATION)
668 			resv_prop = ZFS_PROP_REFRESERVATION;
669 		else
670 			resv_prop = ZFS_PROP_RESERVATION;
671 
672 		if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
673 		    &strval) != 0) {
674 			if (nvlist_add_uint64(props,
675 			    zfs_prop_to_name(resv_prop), volsize) != 0) {
676 				(void) fprintf(stderr, gettext("internal "
677 				    "error: out of memory\n"));
678 				nvlist_free(props);
679 				return (1);
680 			}
681 		}
682 	}
683 
684 	if (parents && zfs_name_valid(argv[0], type)) {
685 		/*
686 		 * Now create the ancestors of target dataset.  If the target
687 		 * already exists and '-p' option was used we should not
688 		 * complain.
689 		 */
690 		if (zfs_dataset_exists(g_zfs, argv[0], type)) {
691 			ret = 0;
692 			goto error;
693 		}
694 		if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
695 			goto error;
696 	}
697 
698 	/* pass to libzfs */
699 	if (zfs_create(g_zfs, argv[0], type, props) != 0)
700 		goto error;
701 
702 	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
703 		goto error;
704 	/*
705 	 * if the user doesn't want the dataset automatically mounted,
706 	 * then skip the mount/share step
707 	 */
708 
709 	canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
710 
711 	/*
712 	 * Mount and/or share the new filesystem as appropriate.  We provide a
713 	 * verbose error message to let the user know that their filesystem was
714 	 * in fact created, even if we failed to mount or share it.
715 	 */
716 	ret = 0;
717 	if (canmount == ZFS_CANMOUNT_ON) {
718 		if (zfs_mount(zhp, NULL, 0) != 0) {
719 			(void) fprintf(stderr, gettext("filesystem "
720 			    "successfully created, but not mounted\n"));
721 			ret = 1;
722 		} else if (zfs_share(zhp) != 0) {
723 			(void) fprintf(stderr, gettext("filesystem "
724 			    "successfully created, but not shared\n"));
725 			ret = 1;
726 		}
727 	}
728 
729 error:
730 	if (zhp)
731 		zfs_close(zhp);
732 	nvlist_free(props);
733 	return (ret);
734 badusage:
735 	nvlist_free(props);
736 	usage(B_FALSE);
737 	return (2);
738 }
739 
740 /*
741  * zfs destroy [-rf] <fs, snap, vol>
742  *
743  * 	-r	Recursively destroy all children
744  * 	-R	Recursively destroy all dependents, including clones
745  * 	-f	Force unmounting of any dependents
746  *
747  * Destroys the given dataset.  By default, it will unmount any filesystems,
748  * and refuse to destroy a dataset that has any dependents.  A dependent can
749  * either be a child, or a clone of a child.
750  */
751 typedef struct destroy_cbdata {
752 	boolean_t	cb_first;
753 	int		cb_force;
754 	int		cb_recurse;
755 	int		cb_error;
756 	int		cb_needforce;
757 	int		cb_doclones;
758 	boolean_t	cb_closezhp;
759 	zfs_handle_t	*cb_target;
760 	char		*cb_snapname;
761 } destroy_cbdata_t;
762 
763 /*
764  * Check for any dependents based on the '-r' or '-R' flags.
765  */
766 static int
767 destroy_check_dependent(zfs_handle_t *zhp, void *data)
768 {
769 	destroy_cbdata_t *cbp = data;
770 	const char *tname = zfs_get_name(cbp->cb_target);
771 	const char *name = zfs_get_name(zhp);
772 
773 	if (strncmp(tname, name, strlen(tname)) == 0 &&
774 	    (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
775 		/*
776 		 * This is a direct descendant, not a clone somewhere else in
777 		 * the hierarchy.
778 		 */
779 		if (cbp->cb_recurse)
780 			goto out;
781 
782 		if (cbp->cb_first) {
783 			(void) fprintf(stderr, gettext("cannot destroy '%s': "
784 			    "%s has children\n"),
785 			    zfs_get_name(cbp->cb_target),
786 			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
787 			(void) fprintf(stderr, gettext("use '-r' to destroy "
788 			    "the following datasets:\n"));
789 			cbp->cb_first = B_FALSE;
790 			cbp->cb_error = 1;
791 		}
792 
793 		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
794 	} else {
795 		/*
796 		 * This is a clone.  We only want to report this if the '-r'
797 		 * wasn't specified, or the target is a snapshot.
798 		 */
799 		if (!cbp->cb_recurse &&
800 		    zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
801 			goto out;
802 
803 		if (cbp->cb_first) {
804 			(void) fprintf(stderr, gettext("cannot destroy '%s': "
805 			    "%s has dependent clones\n"),
806 			    zfs_get_name(cbp->cb_target),
807 			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
808 			(void) fprintf(stderr, gettext("use '-R' to destroy "
809 			    "the following datasets:\n"));
810 			cbp->cb_first = B_FALSE;
811 			cbp->cb_error = 1;
812 		}
813 
814 		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
815 	}
816 
817 out:
818 	zfs_close(zhp);
819 	return (0);
820 }
821 
822 static int
823 destroy_callback(zfs_handle_t *zhp, void *data)
824 {
825 	destroy_cbdata_t *cbp = data;
826 
827 	/*
828 	 * Ignore pools (which we've already flagged as an error before getting
829 	 * here.
830 	 */
831 	if (strchr(zfs_get_name(zhp), '/') == NULL &&
832 	    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
833 		zfs_close(zhp);
834 		return (0);
835 	}
836 
837 	/*
838 	 * Bail out on the first error.
839 	 */
840 	if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 ||
841 	    zfs_destroy(zhp) != 0) {
842 		zfs_close(zhp);
843 		return (-1);
844 	}
845 
846 	zfs_close(zhp);
847 	return (0);
848 }
849 
850 static int
851 destroy_snap_clones(zfs_handle_t *zhp, void *arg)
852 {
853 	destroy_cbdata_t *cbp = arg;
854 	char thissnap[MAXPATHLEN];
855 	zfs_handle_t *szhp;
856 	boolean_t closezhp = cbp->cb_closezhp;
857 	int rv;
858 
859 	(void) snprintf(thissnap, sizeof (thissnap),
860 	    "%s@%s", zfs_get_name(zhp), cbp->cb_snapname);
861 
862 	libzfs_print_on_error(g_zfs, B_FALSE);
863 	szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT);
864 	libzfs_print_on_error(g_zfs, B_TRUE);
865 	if (szhp) {
866 		/*
867 		 * Destroy any clones of this snapshot
868 		 */
869 		if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback,
870 		    cbp) != 0) {
871 			zfs_close(szhp);
872 			if (closezhp)
873 				zfs_close(zhp);
874 			return (-1);
875 		}
876 		zfs_close(szhp);
877 	}
878 
879 	cbp->cb_closezhp = B_TRUE;
880 	rv = zfs_iter_filesystems(zhp, destroy_snap_clones, arg);
881 	if (closezhp)
882 		zfs_close(zhp);
883 	return (rv);
884 }
885 
886 static int
887 zfs_do_destroy(int argc, char **argv)
888 {
889 	destroy_cbdata_t cb = { 0 };
890 	int c;
891 	zfs_handle_t *zhp;
892 	char *cp;
893 
894 	/* check options */
895 	while ((c = getopt(argc, argv, "frR")) != -1) {
896 		switch (c) {
897 		case 'f':
898 			cb.cb_force = 1;
899 			break;
900 		case 'r':
901 			cb.cb_recurse = 1;
902 			break;
903 		case 'R':
904 			cb.cb_recurse = 1;
905 			cb.cb_doclones = 1;
906 			break;
907 		case '?':
908 		default:
909 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
910 			    optopt);
911 			usage(B_FALSE);
912 		}
913 	}
914 
915 	argc -= optind;
916 	argv += optind;
917 
918 	/* check number of arguments */
919 	if (argc == 0) {
920 		(void) fprintf(stderr, gettext("missing path argument\n"));
921 		usage(B_FALSE);
922 	}
923 	if (argc > 1) {
924 		(void) fprintf(stderr, gettext("too many arguments\n"));
925 		usage(B_FALSE);
926 	}
927 
928 	/*
929 	 * If we are doing recursive destroy of a snapshot, then the
930 	 * named snapshot may not exist.  Go straight to libzfs.
931 	 */
932 	if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) {
933 		int ret;
934 
935 		*cp = '\0';
936 		if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
937 			return (1);
938 		*cp = '@';
939 		cp++;
940 
941 		if (cb.cb_doclones) {
942 			cb.cb_snapname = cp;
943 			if (destroy_snap_clones(zhp, &cb) != 0) {
944 				zfs_close(zhp);
945 				return (1);
946 			}
947 		}
948 
949 		ret = zfs_destroy_snaps(zhp, cp);
950 		zfs_close(zhp);
951 		if (ret) {
952 			(void) fprintf(stderr,
953 			    gettext("no snapshots destroyed\n"));
954 		}
955 		return (ret != 0);
956 	}
957 
958 
959 	/* Open the given dataset */
960 	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
961 		return (1);
962 
963 	cb.cb_target = zhp;
964 
965 	/*
966 	 * Perform an explicit check for pools before going any further.
967 	 */
968 	if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
969 	    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
970 		(void) fprintf(stderr, gettext("cannot destroy '%s': "
971 		    "operation does not apply to pools\n"),
972 		    zfs_get_name(zhp));
973 		(void) fprintf(stderr, gettext("use 'zfs destroy -r "
974 		    "%s' to destroy all datasets in the pool\n"),
975 		    zfs_get_name(zhp));
976 		(void) fprintf(stderr, gettext("use 'zpool destroy %s' "
977 		    "to destroy the pool itself\n"), zfs_get_name(zhp));
978 		zfs_close(zhp);
979 		return (1);
980 	}
981 
982 	/*
983 	 * Check for any dependents and/or clones.
984 	 */
985 	cb.cb_first = B_TRUE;
986 	if (!cb.cb_doclones &&
987 	    zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
988 	    &cb) != 0) {
989 		zfs_close(zhp);
990 		return (1);
991 	}
992 
993 	if (cb.cb_error ||
994 	    zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0) {
995 		zfs_close(zhp);
996 		return (1);
997 	}
998 
999 	/*
1000 	 * Do the real thing.  The callback will close the handle regardless of
1001 	 * whether it succeeds or not.
1002 	 */
1003 
1004 	if (destroy_callback(zhp, &cb) != 0)
1005 		return (1);
1006 
1007 
1008 	return (0);
1009 }
1010 
1011 /*
1012  * zfs get [-rHp] [-o field[,field]...] [-s source[,source]...]
1013  * 	< all | property[,property]... > < fs | snap | vol > ...
1014  *
1015  *	-r	recurse over any child datasets
1016  *	-H	scripted mode.  Headers are stripped, and fields are separated
1017  *		by tabs instead of spaces.
1018  *	-o	Set of fields to display.  One of "name,property,value,source".
1019  *		Default is all four.
1020  *	-s	Set of sources to allow.  One of
1021  *		"local,default,inherited,temporary,none".  Default is all
1022  *		five.
1023  *	-p	Display values in parsable (literal) format.
1024  *
1025  *  Prints properties for the given datasets.  The user can control which
1026  *  columns to display as well as which property types to allow.
1027  */
1028 
1029 /*
1030  * Invoked to display the properties for a single dataset.
1031  */
1032 static int
1033 get_callback(zfs_handle_t *zhp, void *data)
1034 {
1035 	char buf[ZFS_MAXPROPLEN];
1036 	zprop_source_t sourcetype;
1037 	char source[ZFS_MAXNAMELEN];
1038 	zprop_get_cbdata_t *cbp = data;
1039 	nvlist_t *userprop = zfs_get_user_props(zhp);
1040 	zprop_list_t *pl = cbp->cb_proplist;
1041 	nvlist_t *propval;
1042 	char *strval;
1043 	char *sourceval;
1044 
1045 	for (; pl != NULL; pl = pl->pl_next) {
1046 		/*
1047 		 * Skip the special fake placeholder.  This will also skip over
1048 		 * the name property when 'all' is specified.
1049 		 */
1050 		if (pl->pl_prop == ZFS_PROP_NAME &&
1051 		    pl == cbp->cb_proplist)
1052 			continue;
1053 
1054 		if (pl->pl_prop != ZPROP_INVAL) {
1055 			if (zfs_prop_get(zhp, pl->pl_prop, buf,
1056 			    sizeof (buf), &sourcetype, source,
1057 			    sizeof (source),
1058 			    cbp->cb_literal) != 0) {
1059 				if (pl->pl_all)
1060 					continue;
1061 				if (!zfs_prop_valid_for_type(pl->pl_prop,
1062 				    ZFS_TYPE_DATASET)) {
1063 					(void) fprintf(stderr,
1064 					    gettext("No such property '%s'\n"),
1065 					    zfs_prop_to_name(pl->pl_prop));
1066 					continue;
1067 				}
1068 				sourcetype = ZPROP_SRC_NONE;
1069 				(void) strlcpy(buf, "-", sizeof (buf));
1070 			}
1071 
1072 			zprop_print_one_property(zfs_get_name(zhp), cbp,
1073 			    zfs_prop_to_name(pl->pl_prop),
1074 			    buf, sourcetype, source);
1075 		} else {
1076 			if (nvlist_lookup_nvlist(userprop,
1077 			    pl->pl_user_prop, &propval) != 0) {
1078 				if (pl->pl_all)
1079 					continue;
1080 				sourcetype = ZPROP_SRC_NONE;
1081 				strval = "-";
1082 			} else {
1083 				verify(nvlist_lookup_string(propval,
1084 				    ZPROP_VALUE, &strval) == 0);
1085 				verify(nvlist_lookup_string(propval,
1086 				    ZPROP_SOURCE, &sourceval) == 0);
1087 
1088 				if (strcmp(sourceval,
1089 				    zfs_get_name(zhp)) == 0) {
1090 					sourcetype = ZPROP_SRC_LOCAL;
1091 				} else {
1092 					sourcetype = ZPROP_SRC_INHERITED;
1093 					(void) strlcpy(source,
1094 					    sourceval, sizeof (source));
1095 				}
1096 			}
1097 
1098 			zprop_print_one_property(zfs_get_name(zhp), cbp,
1099 			    pl->pl_user_prop, strval, sourcetype,
1100 			    source);
1101 		}
1102 	}
1103 
1104 	return (0);
1105 }
1106 
1107 static int
1108 zfs_do_get(int argc, char **argv)
1109 {
1110 	zprop_get_cbdata_t cb = { 0 };
1111 	boolean_t recurse = B_FALSE;
1112 	int i, c;
1113 	char *value, *fields;
1114 	int ret;
1115 	zprop_list_t fake_name = { 0 };
1116 
1117 	/*
1118 	 * Set up default columns and sources.
1119 	 */
1120 	cb.cb_sources = ZPROP_SRC_ALL;
1121 	cb.cb_columns[0] = GET_COL_NAME;
1122 	cb.cb_columns[1] = GET_COL_PROPERTY;
1123 	cb.cb_columns[2] = GET_COL_VALUE;
1124 	cb.cb_columns[3] = GET_COL_SOURCE;
1125 	cb.cb_type = ZFS_TYPE_DATASET;
1126 
1127 	/* check options */
1128 	while ((c = getopt(argc, argv, ":o:s:rHp")) != -1) {
1129 		switch (c) {
1130 		case 'p':
1131 			cb.cb_literal = B_TRUE;
1132 			break;
1133 		case 'r':
1134 			recurse = B_TRUE;
1135 			break;
1136 		case 'H':
1137 			cb.cb_scripted = B_TRUE;
1138 			break;
1139 		case ':':
1140 			(void) fprintf(stderr, gettext("missing argument for "
1141 			    "'%c' option\n"), optopt);
1142 			usage(B_FALSE);
1143 			break;
1144 		case 'o':
1145 			/*
1146 			 * Process the set of columns to display.  We zero out
1147 			 * the structure to give us a blank slate.
1148 			 */
1149 			bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1150 			i = 0;
1151 			while (*optarg != '\0') {
1152 				static char *col_subopts[] =
1153 				    { "name", "property", "value", "source",
1154 				    NULL };
1155 
1156 				if (i == 4) {
1157 					(void) fprintf(stderr, gettext("too "
1158 					    "many fields given to -o "
1159 					    "option\n"));
1160 					usage(B_FALSE);
1161 				}
1162 
1163 				switch (getsubopt(&optarg, col_subopts,
1164 				    &value)) {
1165 				case 0:
1166 					cb.cb_columns[i++] = GET_COL_NAME;
1167 					break;
1168 				case 1:
1169 					cb.cb_columns[i++] = GET_COL_PROPERTY;
1170 					break;
1171 				case 2:
1172 					cb.cb_columns[i++] = GET_COL_VALUE;
1173 					break;
1174 				case 3:
1175 					cb.cb_columns[i++] = GET_COL_SOURCE;
1176 					break;
1177 				default:
1178 					(void) fprintf(stderr,
1179 					    gettext("invalid column name "
1180 					    "'%s'\n"), value);
1181 					usage(B_FALSE);
1182 				}
1183 			}
1184 			break;
1185 
1186 		case 's':
1187 			cb.cb_sources = 0;
1188 			while (*optarg != '\0') {
1189 				static char *source_subopts[] = {
1190 					"local", "default", "inherited",
1191 					"temporary", "none", NULL };
1192 
1193 				switch (getsubopt(&optarg, source_subopts,
1194 				    &value)) {
1195 				case 0:
1196 					cb.cb_sources |= ZPROP_SRC_LOCAL;
1197 					break;
1198 				case 1:
1199 					cb.cb_sources |= ZPROP_SRC_DEFAULT;
1200 					break;
1201 				case 2:
1202 					cb.cb_sources |= ZPROP_SRC_INHERITED;
1203 					break;
1204 				case 3:
1205 					cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1206 					break;
1207 				case 4:
1208 					cb.cb_sources |= ZPROP_SRC_NONE;
1209 					break;
1210 				default:
1211 					(void) fprintf(stderr,
1212 					    gettext("invalid source "
1213 					    "'%s'\n"), value);
1214 					usage(B_FALSE);
1215 				}
1216 			}
1217 			break;
1218 
1219 		case '?':
1220 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1221 			    optopt);
1222 			usage(B_FALSE);
1223 		}
1224 	}
1225 
1226 	argc -= optind;
1227 	argv += optind;
1228 
1229 	if (argc < 1) {
1230 		(void) fprintf(stderr, gettext("missing property "
1231 		    "argument\n"));
1232 		usage(B_FALSE);
1233 	}
1234 
1235 	fields = argv[0];
1236 
1237 	if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1238 	    != 0)
1239 		usage(B_FALSE);
1240 
1241 	argc--;
1242 	argv++;
1243 
1244 	/*
1245 	 * As part of zfs_expand_proplist(), we keep track of the maximum column
1246 	 * width for each property.  For the 'NAME' (and 'SOURCE') columns, we
1247 	 * need to know the maximum name length.  However, the user likely did
1248 	 * not specify 'name' as one of the properties to fetch, so we need to
1249 	 * make sure we always include at least this property for
1250 	 * print_get_headers() to work properly.
1251 	 */
1252 	if (cb.cb_proplist != NULL) {
1253 		fake_name.pl_prop = ZFS_PROP_NAME;
1254 		fake_name.pl_width = strlen(gettext("NAME"));
1255 		fake_name.pl_next = cb.cb_proplist;
1256 		cb.cb_proplist = &fake_name;
1257 	}
1258 
1259 	cb.cb_first = B_TRUE;
1260 
1261 	/* run for each object */
1262 	ret = zfs_for_each(argc, argv, recurse, ZFS_TYPE_DATASET, NULL,
1263 	    &cb.cb_proplist, get_callback, &cb, B_FALSE);
1264 
1265 	if (cb.cb_proplist == &fake_name)
1266 		zprop_free_list(fake_name.pl_next);
1267 	else
1268 		zprop_free_list(cb.cb_proplist);
1269 
1270 	return (ret);
1271 }
1272 
1273 /*
1274  * inherit [-r] <property> <fs|vol> ...
1275  *
1276  * 	-r	Recurse over all children
1277  *
1278  * For each dataset specified on the command line, inherit the given property
1279  * from its parent.  Inheriting a property at the pool level will cause it to
1280  * use the default value.  The '-r' flag will recurse over all children, and is
1281  * useful for setting a property on a hierarchy-wide basis, regardless of any
1282  * local modifications for each dataset.
1283  */
1284 
1285 static int
1286 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1287 {
1288 	char *propname = data;
1289 	int ret;
1290 	zfs_prop_t prop = zfs_name_to_prop(propname);
1291 
1292 	/*
1293 	 * If we're doing it recursively, then ignore properties that
1294 	 * are not valid for this type of dataset.
1295 	 */
1296 	if (prop != ZPROP_INVAL &&
1297 	    !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1298 		return (0);
1299 
1300 	return (zfs_prop_inherit(zhp, propname) != 0);
1301 }
1302 
1303 static int
1304 inherit_cb(zfs_handle_t *zhp, void *data)
1305 {
1306 	char *propname = data;
1307 
1308 	return (zfs_prop_inherit(zhp, propname) != 0);
1309 }
1310 
1311 static int
1312 zfs_do_inherit(int argc, char **argv)
1313 {
1314 	boolean_t recurse = B_FALSE;
1315 	int c;
1316 	zfs_prop_t prop;
1317 	char *propname;
1318 	int ret;
1319 
1320 	/* check options */
1321 	while ((c = getopt(argc, argv, "r")) != -1) {
1322 		switch (c) {
1323 		case 'r':
1324 			recurse = B_TRUE;
1325 			break;
1326 		case '?':
1327 		default:
1328 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1329 			    optopt);
1330 			usage(B_FALSE);
1331 		}
1332 	}
1333 
1334 	argc -= optind;
1335 	argv += optind;
1336 
1337 	/* check number of arguments */
1338 	if (argc < 1) {
1339 		(void) fprintf(stderr, gettext("missing property argument\n"));
1340 		usage(B_FALSE);
1341 	}
1342 	if (argc < 2) {
1343 		(void) fprintf(stderr, gettext("missing dataset argument\n"));
1344 		usage(B_FALSE);
1345 	}
1346 
1347 	propname = argv[0];
1348 	argc--;
1349 	argv++;
1350 
1351 	if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1352 		if (zfs_prop_readonly(prop)) {
1353 			(void) fprintf(stderr, gettext(
1354 			    "%s property is read-only\n"),
1355 			    propname);
1356 			return (1);
1357 		}
1358 		if (!zfs_prop_inheritable(prop)) {
1359 			(void) fprintf(stderr, gettext("'%s' property cannot "
1360 			    "be inherited\n"), propname);
1361 			if (prop == ZFS_PROP_QUOTA ||
1362 			    prop == ZFS_PROP_RESERVATION ||
1363 			    prop == ZFS_PROP_REFQUOTA ||
1364 			    prop == ZFS_PROP_REFRESERVATION)
1365 				(void) fprintf(stderr, gettext("use 'zfs set "
1366 				    "%s=none' to clear\n"), propname);
1367 			return (1);
1368 		}
1369 	} else if (!zfs_prop_user(propname)) {
1370 		(void) fprintf(stderr, gettext("invalid property '%s'\n"),
1371 		    propname);
1372 		usage(B_FALSE);
1373 	}
1374 
1375 	if (recurse) {
1376 		ret = zfs_for_each(argc, argv, recurse, ZFS_TYPE_DATASET,
1377 		    NULL, NULL, inherit_recurse_cb, propname, B_FALSE);
1378 	} else {
1379 		ret = zfs_for_each(argc, argv, recurse, ZFS_TYPE_DATASET,
1380 		    NULL, NULL, inherit_cb, propname, B_FALSE);
1381 	}
1382 
1383 	return (ret);
1384 }
1385 
1386 typedef struct upgrade_cbdata {
1387 	uint64_t cb_numupgraded;
1388 	uint64_t cb_numsamegraded;
1389 	uint64_t cb_numfailed;
1390 	uint64_t cb_version;
1391 	boolean_t cb_newer;
1392 	boolean_t cb_foundone;
1393 	char cb_lastfs[ZFS_MAXNAMELEN];
1394 } upgrade_cbdata_t;
1395 
1396 static int
1397 same_pool(zfs_handle_t *zhp, const char *name)
1398 {
1399 	int len1 = strcspn(name, "/@");
1400 	const char *zhname = zfs_get_name(zhp);
1401 	int len2 = strcspn(zhname, "/@");
1402 
1403 	if (len1 != len2)
1404 		return (B_FALSE);
1405 	return (strncmp(name, zhname, len1) == 0);
1406 }
1407 
1408 static int
1409 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1410 {
1411 	upgrade_cbdata_t *cb = data;
1412 	int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1413 
1414 	/* list if it's old/new */
1415 	if ((!cb->cb_newer && version < ZPL_VERSION) ||
1416 	    (cb->cb_newer && version > ZPL_VERSION)) {
1417 		char *str;
1418 		if (cb->cb_newer) {
1419 			str = gettext("The following filesystems are "
1420 			    "formatted using a newer software version and\n"
1421 			    "cannot be accessed on the current system.\n\n");
1422 		} else {
1423 			str = gettext("The following filesystems are "
1424 			    "out of date, and can be upgraded.  After being\n"
1425 			    "upgraded, these filesystems (and any 'zfs send' "
1426 			    "streams generated from\n"
1427 			    "subsequent snapshots) will no longer be "
1428 			    "accessible by older software versions.\n\n");
1429 		}
1430 
1431 		if (!cb->cb_foundone) {
1432 			(void) puts(str);
1433 			(void) printf(gettext("VER  FILESYSTEM\n"));
1434 			(void) printf(gettext("---  ------------\n"));
1435 			cb->cb_foundone = B_TRUE;
1436 		}
1437 
1438 		(void) printf("%2u   %s\n", version, zfs_get_name(zhp));
1439 	}
1440 
1441 	return (0);
1442 }
1443 
1444 static int
1445 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1446 {
1447 	upgrade_cbdata_t *cb = data;
1448 	int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1449 
1450 	if (cb->cb_version >= ZPL_VERSION_FUID) {
1451 		int spa_version;
1452 
1453 		if (zfs_spa_version(zhp, &spa_version) < 0)
1454 			return (-1);
1455 
1456 		if (spa_version < SPA_VERSION_FUID) {
1457 			/* can't upgrade */
1458 			(void) printf(gettext("%s: can not be upgraded; "
1459 			    "the pool version needs to first be upgraded\nto "
1460 			    "version %d\n\n"),
1461 			    zfs_get_name(zhp), SPA_VERSION_FUID);
1462 			cb->cb_numfailed++;
1463 			return (0);
1464 		}
1465 	}
1466 
1467 	/* upgrade */
1468 	if (version < cb->cb_version) {
1469 		char verstr[16];
1470 		(void) snprintf(verstr, sizeof (verstr),
1471 		    "%llu", cb->cb_version);
1472 		if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1473 			/*
1474 			 * If they did "zfs upgrade -a", then we could
1475 			 * be doing ioctls to different pools.  We need
1476 			 * to log this history once to each pool.
1477 			 */
1478 			verify(zpool_stage_history(g_zfs, history_str) == 0);
1479 		}
1480 		if (zfs_prop_set(zhp, "version", verstr) == 0)
1481 			cb->cb_numupgraded++;
1482 		else
1483 			cb->cb_numfailed++;
1484 		(void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1485 	} else if (version > cb->cb_version) {
1486 		/* can't downgrade */
1487 		(void) printf(gettext("%s: can not be downgraded; "
1488 		    "it is already at version %u\n"),
1489 		    zfs_get_name(zhp), version);
1490 		cb->cb_numfailed++;
1491 	} else {
1492 		cb->cb_numsamegraded++;
1493 	}
1494 	return (0);
1495 }
1496 
1497 /*
1498  * zfs upgrade
1499  * zfs upgrade -v
1500  * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1501  */
1502 static int
1503 zfs_do_upgrade(int argc, char **argv)
1504 {
1505 	boolean_t recurse = B_FALSE;
1506 	boolean_t all = B_FALSE;
1507 	boolean_t showversions = B_FALSE;
1508 	int ret;
1509 	upgrade_cbdata_t cb = { 0 };
1510 	char c;
1511 
1512 	/* check options */
1513 	while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1514 		switch (c) {
1515 		case 'r':
1516 			recurse = B_TRUE;
1517 			break;
1518 		case 'v':
1519 			showversions = B_TRUE;
1520 			break;
1521 		case 'V':
1522 			if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1523 			    optarg, &cb.cb_version) != 0) {
1524 				(void) fprintf(stderr,
1525 				    gettext("invalid version %s\n"), optarg);
1526 				usage(B_FALSE);
1527 			}
1528 			break;
1529 		case 'a':
1530 			all = B_TRUE;
1531 			break;
1532 		case '?':
1533 		default:
1534 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1535 			    optopt);
1536 			usage(B_FALSE);
1537 		}
1538 	}
1539 
1540 	argc -= optind;
1541 	argv += optind;
1542 
1543 	if ((!all && !argc) && (recurse | cb.cb_version))
1544 		usage(B_FALSE);
1545 	if (showversions && (recurse || all || cb.cb_version || argc))
1546 		usage(B_FALSE);
1547 	if ((all || argc) && (showversions))
1548 		usage(B_FALSE);
1549 	if (all && argc)
1550 		usage(B_FALSE);
1551 
1552 	if (showversions) {
1553 		/* Show info on available versions. */
1554 		(void) printf(gettext("The following filesystem versions are "
1555 		    "supported:\n\n"));
1556 		(void) printf(gettext("VER  DESCRIPTION\n"));
1557 		(void) printf("---  -----------------------------------------"
1558 		    "---------------\n");
1559 		(void) printf(gettext(" 1   Initial ZFS filesystem version\n"));
1560 		(void) printf(gettext(" 2   Enhanced directory entries\n"));
1561 		(void) printf(gettext(" 3   Case insensitive and File system "
1562 		    "unique identifer (FUID)\n"));
1563 		(void) printf(gettext("\nFor more information on a particular "
1564 		    "version, including supported releases, see:\n\n"));
1565 		(void) printf("http://www.opensolaris.org/os/community/zfs/"
1566 		    "version/zpl/N\n\n");
1567 		(void) printf(gettext("Where 'N' is the version number.\n"));
1568 		ret = 0;
1569 	} else if (argc || all) {
1570 		/* Upgrade filesystems */
1571 		if (cb.cb_version == 0)
1572 			cb.cb_version = ZPL_VERSION;
1573 		ret = zfs_for_each(argc, argv, recurse, ZFS_TYPE_FILESYSTEM,
1574 		    NULL, NULL, upgrade_set_callback, &cb, B_TRUE);
1575 		(void) printf(gettext("%llu filesystems upgraded\n"),
1576 		    cb.cb_numupgraded);
1577 		if (cb.cb_numsamegraded) {
1578 			(void) printf(gettext("%llu filesystems already at "
1579 			    "this version\n"),
1580 			    cb.cb_numsamegraded);
1581 		}
1582 		if (cb.cb_numfailed != 0)
1583 			ret = 1;
1584 	} else {
1585 		/* List old-version filesytems */
1586 		boolean_t found;
1587 		(void) printf(gettext("This system is currently running "
1588 		    "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
1589 
1590 		ret = zfs_for_each(0, NULL, B_TRUE, ZFS_TYPE_FILESYSTEM,
1591 		    NULL, NULL, upgrade_list_callback, &cb, B_TRUE);
1592 
1593 		found = cb.cb_foundone;
1594 		cb.cb_foundone = B_FALSE;
1595 		cb.cb_newer = B_TRUE;
1596 
1597 		ret = zfs_for_each(0, NULL, B_TRUE, ZFS_TYPE_FILESYSTEM,
1598 		    NULL, NULL, upgrade_list_callback, &cb, B_TRUE);
1599 
1600 		if (!cb.cb_foundone && !found) {
1601 			(void) printf(gettext("All filesystems are "
1602 			    "formatted with the current version.\n"));
1603 		}
1604 	}
1605 
1606 	return (ret);
1607 }
1608 
1609 /*
1610  * list [-rH] [-o property[,property]...] [-t type[,type]...]
1611  *      [-s property [-s property]...] [-S property [-S property]...]
1612  *      <dataset> ...
1613  *
1614  * 	-r	Recurse over all children
1615  * 	-H	Scripted mode; elide headers and separate columns by tabs
1616  * 	-o	Control which fields to display.
1617  * 	-t	Control which object types to display.
1618  *	-s	Specify sort columns, descending order.
1619  *	-S	Specify sort columns, ascending order.
1620  *
1621  * When given no arguments, lists all filesystems in the system.
1622  * Otherwise, list the specified datasets, optionally recursing down them if
1623  * '-r' is specified.
1624  */
1625 typedef struct list_cbdata {
1626 	boolean_t	cb_first;
1627 	boolean_t	cb_scripted;
1628 	zprop_list_t	*cb_proplist;
1629 } list_cbdata_t;
1630 
1631 /*
1632  * Given a list of columns to display, output appropriate headers for each one.
1633  */
1634 static void
1635 print_header(zprop_list_t *pl)
1636 {
1637 	char headerbuf[ZFS_MAXPROPLEN];
1638 	const char *header;
1639 	int i;
1640 	boolean_t first = B_TRUE;
1641 	boolean_t right_justify;
1642 
1643 	for (; pl != NULL; pl = pl->pl_next) {
1644 		if (!first) {
1645 			(void) printf("  ");
1646 		} else {
1647 			first = B_FALSE;
1648 		}
1649 
1650 		right_justify = B_FALSE;
1651 		if (pl->pl_prop != ZPROP_INVAL) {
1652 			header = zfs_prop_column_name(pl->pl_prop);
1653 			right_justify = zfs_prop_align_right(pl->pl_prop);
1654 		} else {
1655 			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
1656 				headerbuf[i] = toupper(pl->pl_user_prop[i]);
1657 			headerbuf[i] = '\0';
1658 			header = headerbuf;
1659 		}
1660 
1661 		if (pl->pl_next == NULL && !right_justify)
1662 			(void) printf("%s", header);
1663 		else if (right_justify)
1664 			(void) printf("%*s", pl->pl_width, header);
1665 		else
1666 			(void) printf("%-*s", pl->pl_width, header);
1667 	}
1668 
1669 	(void) printf("\n");
1670 }
1671 
1672 /*
1673  * Given a dataset and a list of fields, print out all the properties according
1674  * to the described layout.
1675  */
1676 static void
1677 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
1678 {
1679 	boolean_t first = B_TRUE;
1680 	char property[ZFS_MAXPROPLEN];
1681 	nvlist_t *userprops = zfs_get_user_props(zhp);
1682 	nvlist_t *propval;
1683 	char *propstr;
1684 	boolean_t right_justify;
1685 	int width;
1686 
1687 	for (; pl != NULL; pl = pl->pl_next) {
1688 		if (!first) {
1689 			if (scripted)
1690 				(void) printf("\t");
1691 			else
1692 				(void) printf("  ");
1693 		} else {
1694 			first = B_FALSE;
1695 		}
1696 
1697 		right_justify = B_FALSE;
1698 		if (pl->pl_prop != ZPROP_INVAL) {
1699 			if (zfs_prop_get(zhp, pl->pl_prop, property,
1700 			    sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
1701 				propstr = "-";
1702 			else
1703 				propstr = property;
1704 
1705 			right_justify = zfs_prop_align_right(pl->pl_prop);
1706 		} else {
1707 			if (nvlist_lookup_nvlist(userprops,
1708 			    pl->pl_user_prop, &propval) != 0)
1709 				propstr = "-";
1710 			else
1711 				verify(nvlist_lookup_string(propval,
1712 				    ZPROP_VALUE, &propstr) == 0);
1713 		}
1714 
1715 		width = pl->pl_width;
1716 
1717 		/*
1718 		 * If this is being called in scripted mode, or if this is the
1719 		 * last column and it is left-justified, don't include a width
1720 		 * format specifier.
1721 		 */
1722 		if (scripted || (pl->pl_next == NULL && !right_justify))
1723 			(void) printf("%s", propstr);
1724 		else if (right_justify)
1725 			(void) printf("%*s", width, propstr);
1726 		else
1727 			(void) printf("%-*s", width, propstr);
1728 	}
1729 
1730 	(void) printf("\n");
1731 }
1732 
1733 /*
1734  * Generic callback function to list a dataset or snapshot.
1735  */
1736 static int
1737 list_callback(zfs_handle_t *zhp, void *data)
1738 {
1739 	list_cbdata_t *cbp = data;
1740 
1741 	if (cbp->cb_first) {
1742 		if (!cbp->cb_scripted)
1743 			print_header(cbp->cb_proplist);
1744 		cbp->cb_first = B_FALSE;
1745 	}
1746 
1747 	print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
1748 
1749 	return (0);
1750 }
1751 
1752 static int
1753 zfs_do_list(int argc, char **argv)
1754 {
1755 	int c;
1756 	boolean_t recurse = B_FALSE;
1757 	boolean_t scripted = B_FALSE;
1758 	static char default_fields[] =
1759 	    "name,used,available,referenced,mountpoint";
1760 	int types = ZFS_TYPE_DATASET;
1761 	char *fields = NULL;
1762 	char *basic_fields = default_fields;
1763 	list_cbdata_t cb = { 0 };
1764 	char *value;
1765 	int ret;
1766 	char *type_subopts[] = { "filesystem", "volume", "snapshot", NULL };
1767 	zfs_sort_column_t *sortcol = NULL;
1768 
1769 	/* check options */
1770 	while ((c = getopt(argc, argv, ":o:rt:Hs:S:")) != -1) {
1771 		switch (c) {
1772 		case 'o':
1773 			fields = optarg;
1774 			break;
1775 		case 'r':
1776 			recurse = B_TRUE;
1777 			break;
1778 		case 'H':
1779 			scripted = B_TRUE;
1780 			break;
1781 		case 's':
1782 			if (zfs_add_sort_column(&sortcol, optarg,
1783 			    B_FALSE) != 0) {
1784 				(void) fprintf(stderr,
1785 				    gettext("invalid property '%s'\n"), optarg);
1786 				usage(B_FALSE);
1787 			}
1788 			break;
1789 		case 'S':
1790 			if (zfs_add_sort_column(&sortcol, optarg,
1791 			    B_TRUE) != 0) {
1792 				(void) fprintf(stderr,
1793 				    gettext("invalid property '%s'\n"), optarg);
1794 				usage(B_FALSE);
1795 			}
1796 			break;
1797 		case 't':
1798 			types = 0;
1799 			while (*optarg != '\0') {
1800 				switch (getsubopt(&optarg, type_subopts,
1801 				    &value)) {
1802 				case 0:
1803 					types |= ZFS_TYPE_FILESYSTEM;
1804 					break;
1805 				case 1:
1806 					types |= ZFS_TYPE_VOLUME;
1807 					break;
1808 				case 2:
1809 					types |= ZFS_TYPE_SNAPSHOT;
1810 					break;
1811 				default:
1812 					(void) fprintf(stderr,
1813 					    gettext("invalid type '%s'\n"),
1814 					    value);
1815 					usage(B_FALSE);
1816 				}
1817 			}
1818 			break;
1819 		case ':':
1820 			(void) fprintf(stderr, gettext("missing argument for "
1821 			    "'%c' option\n"), optopt);
1822 			usage(B_FALSE);
1823 			break;
1824 		case '?':
1825 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1826 			    optopt);
1827 			usage(B_FALSE);
1828 		}
1829 	}
1830 
1831 	argc -= optind;
1832 	argv += optind;
1833 
1834 	if (fields == NULL)
1835 		fields = basic_fields;
1836 
1837 	/*
1838 	 * If the user specifies '-o all', the zprop_get_list() doesn't
1839 	 * normally include the name of the dataset.  For 'zfs list', we always
1840 	 * want this property to be first.
1841 	 */
1842 	if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1843 	    != 0)
1844 		usage(B_FALSE);
1845 
1846 	cb.cb_scripted = scripted;
1847 	cb.cb_first = B_TRUE;
1848 
1849 	ret = zfs_for_each(argc, argv, recurse, types, sortcol, &cb.cb_proplist,
1850 	    list_callback, &cb, B_TRUE);
1851 
1852 	zprop_free_list(cb.cb_proplist);
1853 	zfs_free_sort_columns(sortcol);
1854 
1855 	if (ret == 0 && cb.cb_first && !cb.cb_scripted)
1856 		(void) printf(gettext("no datasets available\n"));
1857 
1858 	return (ret);
1859 }
1860 
1861 /*
1862  * zfs rename <fs | snap | vol> <fs | snap | vol>
1863  * zfs rename -p <fs | vol> <fs | vol>
1864  * zfs rename -r <snap> <snap>
1865  *
1866  * Renames the given dataset to another of the same type.
1867  *
1868  * The '-p' flag creates all the non-existing ancestors of the target first.
1869  */
1870 /* ARGSUSED */
1871 static int
1872 zfs_do_rename(int argc, char **argv)
1873 {
1874 	zfs_handle_t *zhp;
1875 	int c;
1876 	int ret;
1877 	boolean_t recurse = B_FALSE;
1878 	boolean_t parents = B_FALSE;
1879 
1880 	/* check options */
1881 	while ((c = getopt(argc, argv, "pr")) != -1) {
1882 		switch (c) {
1883 		case 'p':
1884 			parents = B_TRUE;
1885 			break;
1886 		case 'r':
1887 			recurse = B_TRUE;
1888 			break;
1889 		case '?':
1890 		default:
1891 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1892 			    optopt);
1893 			usage(B_FALSE);
1894 		}
1895 	}
1896 
1897 	argc -= optind;
1898 	argv += optind;
1899 
1900 	/* check number of arguments */
1901 	if (argc < 1) {
1902 		(void) fprintf(stderr, gettext("missing source dataset "
1903 		    "argument\n"));
1904 		usage(B_FALSE);
1905 	}
1906 	if (argc < 2) {
1907 		(void) fprintf(stderr, gettext("missing target dataset "
1908 		    "argument\n"));
1909 		usage(B_FALSE);
1910 	}
1911 	if (argc > 2) {
1912 		(void) fprintf(stderr, gettext("too many arguments\n"));
1913 		usage(B_FALSE);
1914 	}
1915 
1916 	if (recurse && parents) {
1917 		(void) fprintf(stderr, gettext("-p and -r options are mutually "
1918 		    "exclusive\n"));
1919 		usage(B_FALSE);
1920 	}
1921 
1922 	if (recurse && strchr(argv[0], '@') == 0) {
1923 		(void) fprintf(stderr, gettext("source dataset for recursive "
1924 		    "rename must be a snapshot\n"));
1925 		usage(B_FALSE);
1926 	}
1927 
1928 	if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
1929 	    ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
1930 		return (1);
1931 
1932 	/* If we were asked and the name looks good, try to create ancestors. */
1933 	if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
1934 	    zfs_create_ancestors(g_zfs, argv[1]) != 0) {
1935 		zfs_close(zhp);
1936 		return (1);
1937 	}
1938 
1939 	ret = (zfs_rename(zhp, argv[1], recurse) != 0);
1940 
1941 	zfs_close(zhp);
1942 	return (ret);
1943 }
1944 
1945 /*
1946  * zfs promote <fs>
1947  *
1948  * Promotes the given clone fs to be the parent
1949  */
1950 /* ARGSUSED */
1951 static int
1952 zfs_do_promote(int argc, char **argv)
1953 {
1954 	zfs_handle_t *zhp;
1955 	int ret;
1956 
1957 	/* check options */
1958 	if (argc > 1 && argv[1][0] == '-') {
1959 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1960 		    argv[1][1]);
1961 		usage(B_FALSE);
1962 	}
1963 
1964 	/* check number of arguments */
1965 	if (argc < 2) {
1966 		(void) fprintf(stderr, gettext("missing clone filesystem"
1967 		    " argument\n"));
1968 		usage(B_FALSE);
1969 	}
1970 	if (argc > 2) {
1971 		(void) fprintf(stderr, gettext("too many arguments\n"));
1972 		usage(B_FALSE);
1973 	}
1974 
1975 	zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1976 	if (zhp == NULL)
1977 		return (1);
1978 
1979 	ret = (zfs_promote(zhp) != 0);
1980 
1981 
1982 	zfs_close(zhp);
1983 	return (ret);
1984 }
1985 
1986 /*
1987  * zfs rollback [-rRf] <snapshot>
1988  *
1989  * 	-r	Delete any intervening snapshots before doing rollback
1990  * 	-R	Delete any snapshots and their clones
1991  * 	-f	ignored for backwards compatability
1992  *
1993  * Given a filesystem, rollback to a specific snapshot, discarding any changes
1994  * since then and making it the active dataset.  If more recent snapshots exist,
1995  * the command will complain unless the '-r' flag is given.
1996  */
1997 typedef struct rollback_cbdata {
1998 	uint64_t	cb_create;
1999 	boolean_t	cb_first;
2000 	int		cb_doclones;
2001 	char		*cb_target;
2002 	int		cb_error;
2003 	boolean_t	cb_recurse;
2004 	boolean_t	cb_dependent;
2005 } rollback_cbdata_t;
2006 
2007 /*
2008  * Report any snapshots more recent than the one specified.  Used when '-r' is
2009  * not specified.  We reuse this same callback for the snapshot dependents - if
2010  * 'cb_dependent' is set, then this is a dependent and we should report it
2011  * without checking the transaction group.
2012  */
2013 static int
2014 rollback_check(zfs_handle_t *zhp, void *data)
2015 {
2016 	rollback_cbdata_t *cbp = data;
2017 
2018 	if (cbp->cb_doclones) {
2019 		zfs_close(zhp);
2020 		return (0);
2021 	}
2022 
2023 	if (!cbp->cb_dependent) {
2024 		if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
2025 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2026 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
2027 		    cbp->cb_create) {
2028 
2029 			if (cbp->cb_first && !cbp->cb_recurse) {
2030 				(void) fprintf(stderr, gettext("cannot "
2031 				    "rollback to '%s': more recent snapshots "
2032 				    "exist\n"),
2033 				    cbp->cb_target);
2034 				(void) fprintf(stderr, gettext("use '-r' to "
2035 				    "force deletion of the following "
2036 				    "snapshots:\n"));
2037 				cbp->cb_first = 0;
2038 				cbp->cb_error = 1;
2039 			}
2040 
2041 			if (cbp->cb_recurse) {
2042 				cbp->cb_dependent = B_TRUE;
2043 				if (zfs_iter_dependents(zhp, B_TRUE,
2044 				    rollback_check, cbp) != 0) {
2045 					zfs_close(zhp);
2046 					return (-1);
2047 				}
2048 				cbp->cb_dependent = B_FALSE;
2049 			} else {
2050 				(void) fprintf(stderr, "%s\n",
2051 				    zfs_get_name(zhp));
2052 			}
2053 		}
2054 	} else {
2055 		if (cbp->cb_first && cbp->cb_recurse) {
2056 			(void) fprintf(stderr, gettext("cannot rollback to "
2057 			    "'%s': clones of previous snapshots exist\n"),
2058 			    cbp->cb_target);
2059 			(void) fprintf(stderr, gettext("use '-R' to "
2060 			    "force deletion of the following clones and "
2061 			    "dependents:\n"));
2062 			cbp->cb_first = 0;
2063 			cbp->cb_error = 1;
2064 		}
2065 
2066 		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
2067 	}
2068 
2069 	zfs_close(zhp);
2070 	return (0);
2071 }
2072 
2073 static int
2074 zfs_do_rollback(int argc, char **argv)
2075 {
2076 	int ret;
2077 	int c;
2078 	boolean_t force = B_FALSE;
2079 	rollback_cbdata_t cb = { 0 };
2080 	zfs_handle_t *zhp, *snap;
2081 	char parentname[ZFS_MAXNAMELEN];
2082 	char *delim;
2083 
2084 	/* check options */
2085 	while ((c = getopt(argc, argv, "rRf")) != -1) {
2086 		switch (c) {
2087 		case 'r':
2088 			cb.cb_recurse = 1;
2089 			break;
2090 		case 'R':
2091 			cb.cb_recurse = 1;
2092 			cb.cb_doclones = 1;
2093 			break;
2094 		case 'f':
2095 			force = B_TRUE;
2096 			break;
2097 		case '?':
2098 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2099 			    optopt);
2100 			usage(B_FALSE);
2101 		}
2102 	}
2103 
2104 	argc -= optind;
2105 	argv += optind;
2106 
2107 	/* check number of arguments */
2108 	if (argc < 1) {
2109 		(void) fprintf(stderr, gettext("missing dataset argument\n"));
2110 		usage(B_FALSE);
2111 	}
2112 	if (argc > 1) {
2113 		(void) fprintf(stderr, gettext("too many arguments\n"));
2114 		usage(B_FALSE);
2115 	}
2116 
2117 	/* open the snapshot */
2118 	if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
2119 		return (1);
2120 
2121 	/* open the parent dataset */
2122 	(void) strlcpy(parentname, argv[0], sizeof (parentname));
2123 	verify((delim = strrchr(parentname, '@')) != NULL);
2124 	*delim = '\0';
2125 	if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
2126 		zfs_close(snap);
2127 		return (1);
2128 	}
2129 
2130 	/*
2131 	 * Check for more recent snapshots and/or clones based on the presence
2132 	 * of '-r' and '-R'.
2133 	 */
2134 	cb.cb_target = argv[0];
2135 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
2136 	cb.cb_first = B_TRUE;
2137 	cb.cb_error = 0;
2138 	if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
2139 		goto out;
2140 
2141 	if ((ret = cb.cb_error) != 0)
2142 		goto out;
2143 
2144 	/*
2145 	 * Rollback parent to the given snapshot.
2146 	 */
2147 	ret = zfs_rollback(zhp, snap, force);
2148 
2149 out:
2150 	zfs_close(snap);
2151 	zfs_close(zhp);
2152 
2153 	if (ret == 0)
2154 		return (0);
2155 	else
2156 		return (1);
2157 }
2158 
2159 /*
2160  * zfs set property=value { fs | snap | vol } ...
2161  *
2162  * Sets the given property for all datasets specified on the command line.
2163  */
2164 typedef struct set_cbdata {
2165 	char		*cb_propname;
2166 	char		*cb_value;
2167 } set_cbdata_t;
2168 
2169 static int
2170 set_callback(zfs_handle_t *zhp, void *data)
2171 {
2172 	set_cbdata_t *cbp = data;
2173 
2174 	if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
2175 		switch (libzfs_errno(g_zfs)) {
2176 		case EZFS_MOUNTFAILED:
2177 			(void) fprintf(stderr, gettext("property may be set "
2178 			    "but unable to remount filesystem\n"));
2179 			break;
2180 		case EZFS_SHARENFSFAILED:
2181 			(void) fprintf(stderr, gettext("property may be set "
2182 			    "but unable to reshare filesystem\n"));
2183 			break;
2184 		}
2185 		return (1);
2186 	}
2187 	return (0);
2188 }
2189 
2190 static int
2191 zfs_do_set(int argc, char **argv)
2192 {
2193 	set_cbdata_t cb;
2194 	int ret;
2195 
2196 	/* check for options */
2197 	if (argc > 1 && argv[1][0] == '-') {
2198 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2199 		    argv[1][1]);
2200 		usage(B_FALSE);
2201 	}
2202 
2203 	/* check number of arguments */
2204 	if (argc < 2) {
2205 		(void) fprintf(stderr, gettext("missing property=value "
2206 		    "argument\n"));
2207 		usage(B_FALSE);
2208 	}
2209 	if (argc < 3) {
2210 		(void) fprintf(stderr, gettext("missing dataset name\n"));
2211 		usage(B_FALSE);
2212 	}
2213 
2214 	/* validate property=value argument */
2215 	cb.cb_propname = argv[1];
2216 	if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
2217 	    (cb.cb_value[1] == '\0')) {
2218 		(void) fprintf(stderr, gettext("missing value in "
2219 		    "property=value argument\n"));
2220 		usage(B_FALSE);
2221 	}
2222 
2223 	*cb.cb_value = '\0';
2224 	cb.cb_value++;
2225 
2226 	if (*cb.cb_propname == '\0') {
2227 		(void) fprintf(stderr,
2228 		    gettext("missing property in property=value argument\n"));
2229 		usage(B_FALSE);
2230 	}
2231 
2232 	ret = zfs_for_each(argc - 2, argv + 2, B_FALSE,
2233 	    ZFS_TYPE_DATASET, NULL, NULL, set_callback, &cb, B_FALSE);
2234 
2235 	return (ret);
2236 }
2237 
2238 /*
2239  * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
2240  *
2241  * Creates a snapshot with the given name.  While functionally equivalent to
2242  * 'zfs create', it is a separate command to differentiate intent.
2243  */
2244 static int
2245 zfs_do_snapshot(int argc, char **argv)
2246 {
2247 	boolean_t recursive = B_FALSE;
2248 	int ret;
2249 	char c;
2250 	nvlist_t *props;
2251 
2252 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
2253 		(void) fprintf(stderr, gettext("internal error: "
2254 		    "out of memory\n"));
2255 		return (1);
2256 	}
2257 
2258 	/* check options */
2259 	while ((c = getopt(argc, argv, "ro:")) != -1) {
2260 		switch (c) {
2261 		case 'o':
2262 			if (parseprop(props))
2263 				return (1);
2264 			break;
2265 		case 'r':
2266 			recursive = B_TRUE;
2267 			break;
2268 		case '?':
2269 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2270 			    optopt);
2271 			goto usage;
2272 		}
2273 	}
2274 
2275 	argc -= optind;
2276 	argv += optind;
2277 
2278 	/* check number of arguments */
2279 	if (argc < 1) {
2280 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
2281 		goto usage;
2282 	}
2283 	if (argc > 1) {
2284 		(void) fprintf(stderr, gettext("too many arguments\n"));
2285 		goto usage;
2286 	}
2287 
2288 	ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
2289 	nvlist_free(props);
2290 	if (ret && recursive)
2291 		(void) fprintf(stderr, gettext("no snapshots were created\n"));
2292 	return (ret != 0);
2293 
2294 usage:
2295 	nvlist_free(props);
2296 	usage(B_FALSE);
2297 	return (-1);
2298 }
2299 
2300 /*
2301  * zfs send [-v] -R [-i|-I <@snap>] <fs@snap>
2302  * zfs send [-v] [-i|-I <@snap>] <fs@snap>
2303  *
2304  * Send a backup stream to stdout.
2305  */
2306 static int
2307 zfs_do_send(int argc, char **argv)
2308 {
2309 	char *fromname = NULL;
2310 	char *toname = NULL;
2311 	char *cp;
2312 	zfs_handle_t *zhp;
2313 	boolean_t doall = B_FALSE;
2314 	boolean_t replicate = B_FALSE;
2315 	boolean_t fromorigin = B_FALSE;
2316 	boolean_t verbose = B_FALSE;
2317 	int c, err;
2318 
2319 	/* check options */
2320 	while ((c = getopt(argc, argv, ":i:I:Rv")) != -1) {
2321 		switch (c) {
2322 		case 'i':
2323 			if (fromname)
2324 				usage(B_FALSE);
2325 			fromname = optarg;
2326 			break;
2327 		case 'I':
2328 			if (fromname)
2329 				usage(B_FALSE);
2330 			fromname = optarg;
2331 			doall = B_TRUE;
2332 			break;
2333 		case 'R':
2334 			replicate = B_TRUE;
2335 			break;
2336 		case 'v':
2337 			verbose = B_TRUE;
2338 			break;
2339 		case ':':
2340 			(void) fprintf(stderr, gettext("missing argument for "
2341 			    "'%c' option\n"), optopt);
2342 			usage(B_FALSE);
2343 			break;
2344 		case '?':
2345 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2346 			    optopt);
2347 			usage(B_FALSE);
2348 		}
2349 	}
2350 
2351 	argc -= optind;
2352 	argv += optind;
2353 
2354 	/* check number of arguments */
2355 	if (argc < 1) {
2356 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
2357 		usage(B_FALSE);
2358 	}
2359 	if (argc > 1) {
2360 		(void) fprintf(stderr, gettext("too many arguments\n"));
2361 		usage(B_FALSE);
2362 	}
2363 
2364 	if (isatty(STDOUT_FILENO)) {
2365 		(void) fprintf(stderr,
2366 		    gettext("Error: Stream can not be written to a terminal.\n"
2367 		    "You must redirect standard output.\n"));
2368 		return (1);
2369 	}
2370 
2371 	cp = strchr(argv[0], '@');
2372 	if (cp == NULL) {
2373 		(void) fprintf(stderr,
2374 		    gettext("argument must be a snapshot\n"));
2375 		usage(B_FALSE);
2376 	}
2377 	*cp = '\0';
2378 	toname = cp + 1;
2379 	zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2380 	if (zhp == NULL)
2381 		return (1);
2382 
2383 	/*
2384 	 * If they specified the full path to the snapshot, chop off
2385 	 * everything except the short name of the snapshot, but special
2386 	 * case if they specify the origin.
2387 	 */
2388 	if (fromname && (cp = strchr(fromname, '@')) != NULL) {
2389 		char origin[ZFS_MAXNAMELEN];
2390 		zprop_source_t src;
2391 
2392 		(void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
2393 		    origin, sizeof (origin), &src, NULL, 0, B_FALSE);
2394 
2395 		if (strcmp(origin, fromname) == 0) {
2396 			fromname = NULL;
2397 			fromorigin = B_TRUE;
2398 		} else {
2399 			*cp = '\0';
2400 			if (cp != fromname && strcmp(argv[0], fromname)) {
2401 				(void) fprintf(stderr,
2402 				    gettext("incremental source must be "
2403 				    "in same filesystem\n"));
2404 				usage(B_FALSE);
2405 			}
2406 			fromname = cp + 1;
2407 			if (strchr(fromname, '@') || strchr(fromname, '/')) {
2408 				(void) fprintf(stderr,
2409 				    gettext("invalid incremental source\n"));
2410 				usage(B_FALSE);
2411 			}
2412 		}
2413 	}
2414 
2415 	if (replicate && fromname == NULL)
2416 		doall = B_TRUE;
2417 
2418 	err = zfs_send(zhp, fromname, toname, replicate, doall, fromorigin,
2419 	    verbose, STDOUT_FILENO);
2420 	zfs_close(zhp);
2421 
2422 	return (err != 0);
2423 }
2424 
2425 /*
2426  * zfs receive [-dnvF] <fs@snap>
2427  *
2428  * Restore a backup stream from stdin.
2429  */
2430 static int
2431 zfs_do_receive(int argc, char **argv)
2432 {
2433 	int c, err;
2434 	recvflags_t flags;
2435 
2436 	bzero(&flags, sizeof (recvflags_t));
2437 	/* check options */
2438 	while ((c = getopt(argc, argv, ":dnvF")) != -1) {
2439 		switch (c) {
2440 		case 'd':
2441 			flags.isprefix = B_TRUE;
2442 			break;
2443 		case 'n':
2444 			flags.dryrun = B_TRUE;
2445 			break;
2446 		case 'v':
2447 			flags.verbose = B_TRUE;
2448 			break;
2449 		case 'F':
2450 			flags.force = B_TRUE;
2451 			break;
2452 		case ':':
2453 			(void) fprintf(stderr, gettext("missing argument for "
2454 			    "'%c' option\n"), optopt);
2455 			usage(B_FALSE);
2456 			break;
2457 		case '?':
2458 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2459 			    optopt);
2460 			usage(B_FALSE);
2461 		}
2462 	}
2463 
2464 	argc -= optind;
2465 	argv += optind;
2466 
2467 	/* check number of arguments */
2468 	if (argc < 1) {
2469 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
2470 		usage(B_FALSE);
2471 	}
2472 	if (argc > 1) {
2473 		(void) fprintf(stderr, gettext("too many arguments\n"));
2474 		usage(B_FALSE);
2475 	}
2476 
2477 	if (isatty(STDIN_FILENO)) {
2478 		(void) fprintf(stderr,
2479 		    gettext("Error: Backup stream can not be read "
2480 		    "from a terminal.\n"
2481 		    "You must redirect standard input.\n"));
2482 		return (1);
2483 	}
2484 
2485 	err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL);
2486 
2487 	return (err != 0);
2488 }
2489 
2490 typedef struct allow_cb {
2491 	int  a_permcnt;
2492 	size_t a_treeoffset;
2493 } allow_cb_t;
2494 
2495 static void
2496 zfs_print_perms(avl_tree_t *tree)
2497 {
2498 	zfs_perm_node_t *permnode;
2499 
2500 	permnode = avl_first(tree);
2501 	while (permnode != NULL) {
2502 		(void) printf("%s", permnode->z_pname);
2503 		permnode = AVL_NEXT(tree, permnode);
2504 		if (permnode)
2505 			(void) printf(",");
2506 		else
2507 			(void) printf("\n");
2508 	}
2509 }
2510 
2511 /*
2512  * Iterate over user/groups/everyone/... and the call perm_iter
2513  * function to print actual permission when tree has >0 nodes.
2514  */
2515 static void
2516 zfs_iter_perms(avl_tree_t *tree, const char *banner, allow_cb_t *cb)
2517 {
2518 	zfs_allow_node_t *item;
2519 	avl_tree_t *ptree;
2520 
2521 	item = avl_first(tree);
2522 	while (item) {
2523 		ptree = (void *)((char *)item + cb->a_treeoffset);
2524 		if (avl_numnodes(ptree)) {
2525 			if (cb->a_permcnt++ == 0)
2526 				(void) printf("%s\n", banner);
2527 			(void) printf("\t%s", item->z_key);
2528 			/*
2529 			 * Avoid an extra space being printed
2530 			 * for "everyone" which is keyed with a null
2531 			 * string
2532 			 */
2533 			if (item->z_key[0] != '\0')
2534 				(void) printf(" ");
2535 			zfs_print_perms(ptree);
2536 		}
2537 		item = AVL_NEXT(tree, item);
2538 	}
2539 }
2540 
2541 #define	LINES "-------------------------------------------------------------\n"
2542 static int
2543 zfs_print_allows(char *ds)
2544 {
2545 	zfs_allow_t *curperms, *perms;
2546 	zfs_handle_t *zhp;
2547 	allow_cb_t allowcb = { 0 };
2548 	char banner[MAXPATHLEN];
2549 
2550 	if (ds[0] == '-')
2551 		usage(B_FALSE);
2552 
2553 	if (strrchr(ds, '@')) {
2554 		(void) fprintf(stderr, gettext("Snapshots don't have 'allow'"
2555 		    " permissions\n"));
2556 		return (1);
2557 	}
2558 	if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_DATASET)) == NULL)
2559 		return (1);
2560 
2561 	if (zfs_perm_get(zhp, &perms)) {
2562 		(void) fprintf(stderr,
2563 		    gettext("Failed to retrieve 'allows' on %s\n"), ds);
2564 		zfs_close(zhp);
2565 		return (1);
2566 	}
2567 
2568 	zfs_close(zhp);
2569 
2570 	if (perms != NULL)
2571 		(void) printf("%s", LINES);
2572 	for (curperms = perms; curperms; curperms = curperms->z_next) {
2573 
2574 		(void) snprintf(banner, sizeof (banner),
2575 		    "Permission sets on (%s)", curperms->z_setpoint);
2576 		allowcb.a_treeoffset =
2577 		    offsetof(zfs_allow_node_t, z_localdescend);
2578 		allowcb.a_permcnt = 0;
2579 		zfs_iter_perms(&curperms->z_sets, banner, &allowcb);
2580 
2581 		(void) snprintf(banner, sizeof (banner),
2582 		    "Create time permissions on (%s)", curperms->z_setpoint);
2583 		allowcb.a_treeoffset =
2584 		    offsetof(zfs_allow_node_t, z_localdescend);
2585 		allowcb.a_permcnt = 0;
2586 		zfs_iter_perms(&curperms->z_crperms, banner, &allowcb);
2587 
2588 
2589 		(void) snprintf(banner, sizeof (banner),
2590 		    "Local permissions on (%s)", curperms->z_setpoint);
2591 		allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_local);
2592 		allowcb.a_permcnt = 0;
2593 		zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2594 		zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2595 		zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2596 
2597 		(void) snprintf(banner, sizeof (banner),
2598 		    "Descendent permissions on (%s)", curperms->z_setpoint);
2599 		allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_descend);
2600 		allowcb.a_permcnt = 0;
2601 		zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2602 		zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2603 		zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2604 
2605 		(void) snprintf(banner, sizeof (banner),
2606 		    "Local+Descendent permissions on (%s)",
2607 		    curperms->z_setpoint);
2608 		allowcb.a_treeoffset =
2609 		    offsetof(zfs_allow_node_t, z_localdescend);
2610 		allowcb.a_permcnt = 0;
2611 		zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2612 		zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2613 		zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2614 
2615 		(void) printf("%s", LINES);
2616 	}
2617 	zfs_free_allows(perms);
2618 	return (0);
2619 }
2620 
2621 #define	ALLOWOPTIONS "ldcsu:g:e"
2622 #define	UNALLOWOPTIONS "ldcsu:g:er"
2623 
2624 /*
2625  * Validate options, and build necessary datastructure to display/remove/add
2626  * permissions.
2627  * Returns 0 - If permissions should be added/removed
2628  * Returns 1 - If permissions should be displayed.
2629  * Returns -1 - on failure
2630  */
2631 int
2632 parse_allow_args(int *argc, char **argv[], boolean_t unallow,
2633     char **ds, int *recurse, nvlist_t **zperms)
2634 {
2635 	int c;
2636 	char *options = unallow ? UNALLOWOPTIONS : ALLOWOPTIONS;
2637 	zfs_deleg_inherit_t deleg_type = ZFS_DELEG_NONE;
2638 	zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
2639 	char *who = NULL;
2640 	char *perms = NULL;
2641 	zfs_handle_t *zhp;
2642 
2643 	while ((c = getopt(*argc, *argv, options)) != -1) {
2644 		switch (c) {
2645 		case 'l':
2646 			if (who_type == ZFS_DELEG_CREATE ||
2647 			    who_type == ZFS_DELEG_NAMED_SET)
2648 				usage(B_FALSE);
2649 
2650 			deleg_type |= ZFS_DELEG_PERM_LOCAL;
2651 			break;
2652 		case 'd':
2653 			if (who_type == ZFS_DELEG_CREATE ||
2654 			    who_type == ZFS_DELEG_NAMED_SET)
2655 				usage(B_FALSE);
2656 
2657 			deleg_type |= ZFS_DELEG_PERM_DESCENDENT;
2658 			break;
2659 		case 'r':
2660 			*recurse = B_TRUE;
2661 			break;
2662 		case 'c':
2663 			if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2664 				usage(B_FALSE);
2665 			if (deleg_type)
2666 				usage(B_FALSE);
2667 			who_type = ZFS_DELEG_CREATE;
2668 			break;
2669 		case 's':
2670 			if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2671 				usage(B_FALSE);
2672 			if (deleg_type)
2673 				usage(B_FALSE);
2674 			who_type = ZFS_DELEG_NAMED_SET;
2675 			break;
2676 		case 'u':
2677 			if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2678 				usage(B_FALSE);
2679 			who_type = ZFS_DELEG_USER;
2680 			who = optarg;
2681 			break;
2682 		case 'g':
2683 			if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2684 				usage(B_FALSE);
2685 			who_type = ZFS_DELEG_GROUP;
2686 			who = optarg;
2687 			break;
2688 		case 'e':
2689 			if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2690 				usage(B_FALSE);
2691 			who_type = ZFS_DELEG_EVERYONE;
2692 			break;
2693 		default:
2694 			usage(B_FALSE);
2695 			break;
2696 		}
2697 	}
2698 
2699 	if (deleg_type == 0)
2700 		deleg_type = ZFS_DELEG_PERM_LOCALDESCENDENT;
2701 
2702 	*argc -= optind;
2703 	*argv += optind;
2704 
2705 	if (unallow == B_FALSE && *argc == 1) {
2706 		/*
2707 		 * Only print permissions if no options were processed
2708 		 */
2709 		if (optind == 1)
2710 			return (1);
2711 		else
2712 			usage(B_FALSE);
2713 	}
2714 
2715 	/*
2716 	 * initialize variables for zfs_build_perms based on number
2717 	 * of arguments.
2718 	 * 3 arguments ==>	zfs [un]allow joe perm,perm,perm <dataset> or
2719 	 *			zfs [un]allow -s @set1 perm,perm <dataset>
2720 	 * 2 arguments ==>	zfs [un]allow -c perm,perm <dataset> or
2721 	 *			zfs [un]allow -u|-g <name> perm <dataset> or
2722 	 *			zfs [un]allow -e perm,perm <dataset>
2723 	 *			zfs unallow joe <dataset>
2724 	 *			zfs unallow -s @set1 <dataset>
2725 	 * 1 argument  ==>	zfs [un]allow -e <dataset> or
2726 	 *			zfs [un]allow -c <dataset>
2727 	 */
2728 
2729 	switch (*argc) {
2730 	case 3:
2731 		perms = (*argv)[1];
2732 		who = (*argv)[0];
2733 		*ds = (*argv)[2];
2734 
2735 		/*
2736 		 * advance argc/argv for do_allow cases.
2737 		 * for do_allow case make sure who have a know who type
2738 		 * and its not a permission set.
2739 		 */
2740 		if (unallow == B_TRUE) {
2741 			*argc -= 2;
2742 			*argv += 2;
2743 		} else if (who_type != ZFS_DELEG_WHO_UNKNOWN &&
2744 		    who_type != ZFS_DELEG_NAMED_SET)
2745 			usage(B_FALSE);
2746 		break;
2747 
2748 	case 2:
2749 		if (unallow == B_TRUE && (who_type == ZFS_DELEG_EVERYONE ||
2750 		    who_type == ZFS_DELEG_CREATE || who != NULL)) {
2751 			perms = (*argv)[0];
2752 			*ds = (*argv)[1];
2753 		} else {
2754 			if (unallow == B_FALSE &&
2755 			    (who_type == ZFS_DELEG_WHO_UNKNOWN ||
2756 			    who_type == ZFS_DELEG_NAMED_SET))
2757 				usage(B_FALSE);
2758 			else if (who_type == ZFS_DELEG_WHO_UNKNOWN ||
2759 			    who_type == ZFS_DELEG_NAMED_SET)
2760 				who = (*argv)[0];
2761 			else if (who_type != ZFS_DELEG_NAMED_SET)
2762 				perms = (*argv)[0];
2763 			*ds = (*argv)[1];
2764 		}
2765 		if (unallow == B_TRUE) {
2766 			(*argc)--;
2767 			(*argv)++;
2768 		}
2769 		break;
2770 
2771 	case 1:
2772 		if (unallow == B_FALSE)
2773 			usage(B_FALSE);
2774 		if (who == NULL && who_type != ZFS_DELEG_CREATE &&
2775 		    who_type != ZFS_DELEG_EVERYONE)
2776 			usage(B_FALSE);
2777 		*ds = (*argv)[0];
2778 		break;
2779 
2780 	default:
2781 		usage(B_FALSE);
2782 	}
2783 
2784 	if (strrchr(*ds, '@')) {
2785 		(void) fprintf(stderr,
2786 		    gettext("Can't set or remove 'allow' permissions "
2787 		    "on snapshots.\n"));
2788 			return (-1);
2789 	}
2790 
2791 	if ((zhp = zfs_open(g_zfs, *ds, ZFS_TYPE_DATASET)) == NULL)
2792 		return (-1);
2793 
2794 	if ((zfs_build_perms(zhp, who, perms,
2795 	    who_type, deleg_type, zperms)) != 0) {
2796 		zfs_close(zhp);
2797 		return (-1);
2798 	}
2799 	zfs_close(zhp);
2800 	return (0);
2801 }
2802 
2803 static int
2804 zfs_do_allow(int argc, char **argv)
2805 {
2806 	char *ds;
2807 	nvlist_t *zperms = NULL;
2808 	zfs_handle_t *zhp;
2809 	int unused;
2810 	int ret;
2811 
2812 	if ((ret = parse_allow_args(&argc, &argv, B_FALSE, &ds,
2813 	    &unused, &zperms)) == -1)
2814 		return (1);
2815 
2816 	if (ret == 1)
2817 		return (zfs_print_allows(argv[0]));
2818 
2819 	if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_DATASET)) == NULL)
2820 		return (1);
2821 
2822 	if (zfs_perm_set(zhp, zperms)) {
2823 		zfs_close(zhp);
2824 		nvlist_free(zperms);
2825 		return (1);
2826 	}
2827 	nvlist_free(zperms);
2828 	zfs_close(zhp);
2829 
2830 	return (0);
2831 }
2832 
2833 static int
2834 unallow_callback(zfs_handle_t *zhp, void *data)
2835 {
2836 	nvlist_t *nvp = (nvlist_t *)data;
2837 	int error;
2838 
2839 	error = zfs_perm_remove(zhp, nvp);
2840 	if (error) {
2841 		(void) fprintf(stderr, gettext("Failed to remove permissions "
2842 		    "on %s\n"), zfs_get_name(zhp));
2843 	}
2844 	return (error);
2845 }
2846 
2847 static int
2848 zfs_do_unallow(int argc, char **argv)
2849 {
2850 	int recurse = B_FALSE;
2851 	char *ds;
2852 	int error;
2853 	nvlist_t *zperms = NULL;
2854 
2855 	if (parse_allow_args(&argc, &argv, B_TRUE,
2856 	    &ds, &recurse, &zperms) == -1)
2857 		return (1);
2858 
2859 	error = zfs_for_each(argc, argv, recurse,
2860 	    ZFS_TYPE_FILESYSTEM|ZFS_TYPE_VOLUME, NULL,
2861 	    NULL, unallow_callback, (void *)zperms, B_FALSE);
2862 
2863 	if (zperms)
2864 		nvlist_free(zperms);
2865 
2866 	return (error);
2867 }
2868 
2869 typedef struct get_all_cbdata {
2870 	zfs_handle_t	**cb_handles;
2871 	size_t		cb_alloc;
2872 	size_t		cb_used;
2873 	uint_t		cb_types;
2874 	boolean_t	cb_verbose;
2875 } get_all_cbdata_t;
2876 
2877 #define	CHECK_SPINNER 30
2878 #define	SPINNER_TIME 3		/* seconds */
2879 #define	MOUNT_TIME 5		/* seconds */
2880 
2881 static int
2882 get_one_dataset(zfs_handle_t *zhp, void *data)
2883 {
2884 	static char spin[] = { '-', '\\', '|', '/' };
2885 	static int spinval = 0;
2886 	static int spincheck = 0;
2887 	static time_t last_spin_time = (time_t)0;
2888 	get_all_cbdata_t *cbp = data;
2889 	zfs_type_t type = zfs_get_type(zhp);
2890 
2891 	if (cbp->cb_verbose) {
2892 		if (--spincheck < 0) {
2893 			time_t now = time(NULL);
2894 			if (last_spin_time + SPINNER_TIME < now) {
2895 				(void) printf("\b%c", spin[spinval++ % 4]);
2896 				(void) fflush(stdout);
2897 				last_spin_time = now;
2898 			}
2899 			spincheck = CHECK_SPINNER;
2900 		}
2901 	}
2902 
2903 	/*
2904 	 * Interate over any nested datasets.
2905 	 */
2906 	if (type == ZFS_TYPE_FILESYSTEM &&
2907 	    zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
2908 		zfs_close(zhp);
2909 		return (1);
2910 	}
2911 
2912 	/*
2913 	 * Skip any datasets whose type does not match.
2914 	 */
2915 	if ((type & cbp->cb_types) == 0) {
2916 		zfs_close(zhp);
2917 		return (0);
2918 	}
2919 
2920 	if (cbp->cb_alloc == cbp->cb_used) {
2921 		zfs_handle_t **handles;
2922 
2923 		if (cbp->cb_alloc == 0)
2924 			cbp->cb_alloc = 64;
2925 		else
2926 			cbp->cb_alloc *= 2;
2927 
2928 		handles = safe_malloc(cbp->cb_alloc * sizeof (void *));
2929 
2930 		if (cbp->cb_handles) {
2931 			bcopy(cbp->cb_handles, handles,
2932 			    cbp->cb_used * sizeof (void *));
2933 			free(cbp->cb_handles);
2934 		}
2935 
2936 		cbp->cb_handles = handles;
2937 	}
2938 
2939 	cbp->cb_handles[cbp->cb_used++] = zhp;
2940 
2941 	return (0);
2942 }
2943 
2944 static void
2945 get_all_datasets(uint_t types, zfs_handle_t ***dslist, size_t *count,
2946     boolean_t verbose)
2947 {
2948 	get_all_cbdata_t cb = { 0 };
2949 	cb.cb_types = types;
2950 	cb.cb_verbose = verbose;
2951 
2952 	if (verbose) {
2953 		(void) printf("%s: *", gettext("Reading ZFS config"));
2954 		(void) fflush(stdout);
2955 	}
2956 
2957 	(void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
2958 
2959 	*dslist = cb.cb_handles;
2960 	*count = cb.cb_used;
2961 
2962 	if (verbose) {
2963 		(void) printf("\b%s\n", gettext("done."));
2964 	}
2965 }
2966 
2967 static int
2968 dataset_cmp(const void *a, const void *b)
2969 {
2970 	zfs_handle_t **za = (zfs_handle_t **)a;
2971 	zfs_handle_t **zb = (zfs_handle_t **)b;
2972 	char mounta[MAXPATHLEN];
2973 	char mountb[MAXPATHLEN];
2974 	boolean_t gota, gotb;
2975 
2976 	if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
2977 		verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
2978 		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
2979 	if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
2980 		verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
2981 		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
2982 
2983 	if (gota && gotb)
2984 		return (strcmp(mounta, mountb));
2985 
2986 	if (gota)
2987 		return (-1);
2988 	if (gotb)
2989 		return (1);
2990 
2991 	return (strcmp(zfs_get_name(a), zfs_get_name(b)));
2992 }
2993 
2994 /*
2995  * Generic callback for sharing or mounting filesystems.  Because the code is so
2996  * similar, we have a common function with an extra parameter to determine which
2997  * mode we are using.
2998  */
2999 #define	OP_SHARE	0x1
3000 #define	OP_MOUNT	0x2
3001 
3002 /*
3003  * Share or mount a dataset.
3004  */
3005 static int
3006 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
3007     boolean_t explicit, const char *options)
3008 {
3009 	char mountpoint[ZFS_MAXPROPLEN];
3010 	char shareopts[ZFS_MAXPROPLEN];
3011 	char smbshareopts[ZFS_MAXPROPLEN];
3012 	const char *cmdname = op == OP_SHARE ? "share" : "mount";
3013 	struct mnttab mnt;
3014 	uint64_t zoned, canmount;
3015 	zfs_type_t type = zfs_get_type(zhp);
3016 	boolean_t shared_nfs, shared_smb;
3017 
3018 	assert(type & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME));
3019 
3020 	if (type == ZFS_TYPE_FILESYSTEM) {
3021 		/*
3022 		 * Check to make sure we can mount/share this dataset.  If we
3023 		 * are in the global zone and the filesystem is exported to a
3024 		 * local zone, or if we are in a local zone and the
3025 		 * filesystem is not exported, then it is an error.
3026 		 */
3027 		zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3028 
3029 		if (zoned && getzoneid() == GLOBAL_ZONEID) {
3030 			if (!explicit)
3031 				return (0);
3032 
3033 			(void) fprintf(stderr, gettext("cannot %s '%s': "
3034 			    "dataset is exported to a local zone\n"), cmdname,
3035 			    zfs_get_name(zhp));
3036 			return (1);
3037 
3038 		} else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
3039 			if (!explicit)
3040 				return (0);
3041 
3042 			(void) fprintf(stderr, gettext("cannot %s '%s': "
3043 			    "permission denied\n"), cmdname,
3044 			    zfs_get_name(zhp));
3045 			return (1);
3046 		}
3047 
3048 		/*
3049 		 * Ignore any filesystems which don't apply to us. This
3050 		 * includes those with a legacy mountpoint, or those with
3051 		 * legacy share options.
3052 		 */
3053 		verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
3054 		    sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
3055 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
3056 		    sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
3057 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
3058 		    sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
3059 		canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
3060 
3061 		if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
3062 		    strcmp(smbshareopts, "off") == 0) {
3063 			if (!explicit)
3064 				return (0);
3065 
3066 			(void) fprintf(stderr, gettext("cannot share '%s': "
3067 			    "legacy share\n"), zfs_get_name(zhp));
3068 			(void) fprintf(stderr, gettext("use share(1M) to "
3069 			    "share this filesystem\n"));
3070 			return (1);
3071 		}
3072 
3073 		/*
3074 		 * We cannot share or mount legacy filesystems. If the
3075 		 * shareopts is non-legacy but the mountpoint is legacy, we
3076 		 * treat it as a legacy share.
3077 		 */
3078 		if (strcmp(mountpoint, "legacy") == 0) {
3079 			if (!explicit)
3080 				return (0);
3081 
3082 			(void) fprintf(stderr, gettext("cannot %s '%s': "
3083 			    "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
3084 			(void) fprintf(stderr, gettext("use %s(1M) to "
3085 			    "%s this filesystem\n"), cmdname, cmdname);
3086 			return (1);
3087 		}
3088 
3089 		if (strcmp(mountpoint, "none") == 0) {
3090 			if (!explicit)
3091 				return (0);
3092 
3093 			(void) fprintf(stderr, gettext("cannot %s '%s': no "
3094 			    "mountpoint set\n"), cmdname, zfs_get_name(zhp));
3095 			return (1);
3096 		}
3097 
3098 		/*
3099 		 * canmount	explicit	outcome
3100 		 * on		no		pass through
3101 		 * on		yes		pass through
3102 		 * off		no		return 0
3103 		 * off		yes		display error, return 1
3104 		 * noauto	no		return 0
3105 		 * noauto	yes		pass through
3106 		 */
3107 		if (canmount == ZFS_CANMOUNT_OFF) {
3108 			if (!explicit)
3109 				return (0);
3110 
3111 			(void) fprintf(stderr, gettext("cannot %s '%s': "
3112 			    "'canmount' property is set to 'off'\n"), cmdname,
3113 			    zfs_get_name(zhp));
3114 			return (1);
3115 		} else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
3116 			return (0);
3117 		}
3118 
3119 		/*
3120 		 * At this point, we have verified that the mountpoint and/or
3121 		 * shareopts are appropriate for auto management. If the
3122 		 * filesystem is already mounted or shared, return (failing
3123 		 * for explicit requests); otherwise mount or share the
3124 		 * filesystem.
3125 		 */
3126 		switch (op) {
3127 		case OP_SHARE:
3128 
3129 			shared_nfs = zfs_is_shared_nfs(zhp, NULL);
3130 			shared_smb = zfs_is_shared_smb(zhp, NULL);
3131 
3132 			if (shared_nfs && shared_smb ||
3133 			    (shared_nfs && strcmp(shareopts, "on") == 0 &&
3134 			    strcmp(smbshareopts, "off") == 0) ||
3135 			    (shared_smb && strcmp(smbshareopts, "on") == 0 &&
3136 			    strcmp(shareopts, "off") == 0)) {
3137 				if (!explicit)
3138 					return (0);
3139 
3140 				(void) fprintf(stderr, gettext("cannot share "
3141 				    "'%s': filesystem already shared\n"),
3142 				    zfs_get_name(zhp));
3143 				return (1);
3144 			}
3145 
3146 			if (!zfs_is_mounted(zhp, NULL) &&
3147 			    zfs_mount(zhp, NULL, 0) != 0)
3148 				return (1);
3149 
3150 			if (protocol == NULL) {
3151 				if (zfs_shareall(zhp) != 0)
3152 					return (1);
3153 			} else if (strcmp(protocol, "nfs") == 0) {
3154 				if (zfs_share_nfs(zhp))
3155 					return (1);
3156 			} else if (strcmp(protocol, "smb") == 0) {
3157 				if (zfs_share_smb(zhp))
3158 					return (1);
3159 			} else {
3160 				(void) fprintf(stderr, gettext("cannot share "
3161 				    "'%s': invalid share type '%s' "
3162 				    "specified\n"),
3163 				    zfs_get_name(zhp), protocol);
3164 				return (1);
3165 			}
3166 
3167 			break;
3168 
3169 		case OP_MOUNT:
3170 			if (options == NULL)
3171 				mnt.mnt_mntopts = "";
3172 			else
3173 				mnt.mnt_mntopts = (char *)options;
3174 
3175 			if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
3176 			    zfs_is_mounted(zhp, NULL)) {
3177 				if (!explicit)
3178 					return (0);
3179 
3180 				(void) fprintf(stderr, gettext("cannot mount "
3181 				    "'%s': filesystem already mounted\n"),
3182 				    zfs_get_name(zhp));
3183 				return (1);
3184 			}
3185 
3186 			if (zfs_mount(zhp, options, flags) != 0)
3187 				return (1);
3188 			break;
3189 		}
3190 	} else {
3191 		assert(op == OP_SHARE);
3192 
3193 		/*
3194 		 * Ignore any volumes that aren't shared.
3195 		 */
3196 		verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
3197 		    sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
3198 
3199 		if (strcmp(shareopts, "off") == 0) {
3200 			if (!explicit)
3201 				return (0);
3202 
3203 			(void) fprintf(stderr, gettext("cannot share '%s': "
3204 			    "'shareiscsi' property not set\n"),
3205 			    zfs_get_name(zhp));
3206 			(void) fprintf(stderr, gettext("set 'shareiscsi' "
3207 			    "property or use iscsitadm(1M) to share this "
3208 			    "volume\n"));
3209 			return (1);
3210 		}
3211 
3212 		if (zfs_is_shared_iscsi(zhp)) {
3213 			if (!explicit)
3214 				return (0);
3215 
3216 			(void) fprintf(stderr, gettext("cannot share "
3217 			    "'%s': volume already shared\n"),
3218 			    zfs_get_name(zhp));
3219 			return (1);
3220 		}
3221 
3222 		if (zfs_share_iscsi(zhp) != 0)
3223 			return (1);
3224 	}
3225 
3226 	return (0);
3227 }
3228 
3229 /*
3230  * Reports progress in the form "(current/total)".  Not thread-safe.
3231  */
3232 static void
3233 report_mount_progress(int current, int total)
3234 {
3235 	static int len;
3236 	static char *reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
3237 	    "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
3238 	static time_t last_progress_time;
3239 	time_t now = time(NULL);
3240 
3241 	/* report 1..n instead of 0..n-1 */
3242 	++current;
3243 
3244 	/* display header if we're here for the first time */
3245 	if (current == 1) {
3246 		(void) printf(gettext("Mounting ZFS filesystems: "));
3247 		len = 0;
3248 	} else if (current != total && last_progress_time + MOUNT_TIME >= now) {
3249 		/* too soon to report again */
3250 		return;
3251 	}
3252 
3253 	last_progress_time = now;
3254 
3255 	/* back up to prepare for overwriting */
3256 	if (len)
3257 		(void) printf("%*.*s", len, len, reverse);
3258 
3259 	/* We put a newline at the end if this is the last one.  */
3260 	len = printf("(%d/%d)%s", current, total, current == total ? "\n" : "");
3261 	(void) fflush(stdout);
3262 }
3263 
3264 static void
3265 append_options(char *mntopts, char *newopts)
3266 {
3267 	int len = strlen(mntopts);
3268 
3269 	/* original length plus new string to append plus 1 for the comma */
3270 	if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
3271 		(void) fprintf(stderr, gettext("the opts argument for "
3272 		    "'%c' option is too long (more than %d chars)\n"),
3273 		    "-o", MNT_LINE_MAX);
3274 		usage(B_FALSE);
3275 	}
3276 
3277 	if (*mntopts)
3278 		mntopts[len++] = ',';
3279 
3280 	(void) strcpy(&mntopts[len], newopts);
3281 }
3282 
3283 static int
3284 share_mount(int op, int argc, char **argv)
3285 {
3286 	int do_all = 0;
3287 	boolean_t verbose = B_FALSE;
3288 	int c, ret = 0;
3289 	char *options = NULL;
3290 	int types, flags = 0;
3291 
3292 	/* check options */
3293 	while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
3294 	    != -1) {
3295 		switch (c) {
3296 		case 'a':
3297 			do_all = 1;
3298 			break;
3299 		case 'v':
3300 			verbose = B_TRUE;
3301 			break;
3302 		case 'o':
3303 			if (*optarg == '\0') {
3304 				(void) fprintf(stderr, gettext("empty mount "
3305 				    "options (-o) specified\n"));
3306 				usage(B_FALSE);
3307 			}
3308 
3309 			if (options == NULL)
3310 				options = safe_malloc(MNT_LINE_MAX + 1);
3311 
3312 			/* option validation is done later */
3313 			append_options(options, optarg);
3314 			break;
3315 
3316 		case 'O':
3317 			flags |= MS_OVERLAY;
3318 			break;
3319 		case ':':
3320 			(void) fprintf(stderr, gettext("missing argument for "
3321 			    "'%c' option\n"), optopt);
3322 			usage(B_FALSE);
3323 			break;
3324 		case '?':
3325 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3326 			    optopt);
3327 			usage(B_FALSE);
3328 		}
3329 	}
3330 
3331 	argc -= optind;
3332 	argv += optind;
3333 
3334 	/* check number of arguments */
3335 	if (do_all) {
3336 		zfs_handle_t **dslist = NULL;
3337 		size_t i, count = 0;
3338 		char *protocol = NULL;
3339 
3340 		if (op == OP_MOUNT) {
3341 			types = ZFS_TYPE_FILESYSTEM;
3342 		} else if (argc > 0) {
3343 			if (strcmp(argv[0], "nfs") == 0 ||
3344 			    strcmp(argv[0], "smb") == 0) {
3345 				types = ZFS_TYPE_FILESYSTEM;
3346 			} else if (strcmp(argv[0], "iscsi") == 0) {
3347 				types = ZFS_TYPE_VOLUME;
3348 			} else {
3349 				(void) fprintf(stderr, gettext("share type "
3350 				    "must be 'nfs', 'smb' or 'iscsi'\n"));
3351 				usage(B_FALSE);
3352 			}
3353 			protocol = argv[0];
3354 			argc--;
3355 			argv++;
3356 		} else {
3357 			types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3358 		}
3359 
3360 		if (argc != 0) {
3361 			(void) fprintf(stderr, gettext("too many arguments\n"));
3362 			usage(B_FALSE);
3363 		}
3364 
3365 		get_all_datasets(types, &dslist, &count, verbose);
3366 
3367 		if (count == 0)
3368 			return (0);
3369 
3370 		qsort(dslist, count, sizeof (void *), dataset_cmp);
3371 
3372 		for (i = 0; i < count; i++) {
3373 			if (verbose)
3374 				report_mount_progress(i, count);
3375 
3376 			if (share_mount_one(dslist[i], op, flags, protocol,
3377 			    B_FALSE, options) != 0)
3378 				ret = 1;
3379 			zfs_close(dslist[i]);
3380 		}
3381 
3382 		free(dslist);
3383 	} else if (argc == 0) {
3384 		struct mnttab entry;
3385 
3386 		if ((op == OP_SHARE) || (options != NULL)) {
3387 			(void) fprintf(stderr, gettext("missing filesystem "
3388 			    "argument (specify -a for all)\n"));
3389 			usage(B_FALSE);
3390 		}
3391 
3392 		/*
3393 		 * When mount is given no arguments, go through /etc/mnttab and
3394 		 * display any active ZFS mounts.  We hide any snapshots, since
3395 		 * they are controlled automatically.
3396 		 */
3397 		rewind(mnttab_file);
3398 		while (getmntent(mnttab_file, &entry) == 0) {
3399 			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
3400 			    strchr(entry.mnt_special, '@') != NULL)
3401 				continue;
3402 
3403 			(void) printf("%-30s  %s\n", entry.mnt_special,
3404 			    entry.mnt_mountp);
3405 		}
3406 
3407 	} else {
3408 		zfs_handle_t *zhp;
3409 
3410 		types = ZFS_TYPE_FILESYSTEM;
3411 		if (op == OP_SHARE)
3412 			types |= ZFS_TYPE_VOLUME;
3413 
3414 		if (argc > 1) {
3415 			(void) fprintf(stderr,
3416 			    gettext("too many arguments\n"));
3417 			usage(B_FALSE);
3418 		}
3419 
3420 		if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) {
3421 			ret = 1;
3422 		} else {
3423 			ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
3424 			    options);
3425 			zfs_close(zhp);
3426 		}
3427 	}
3428 
3429 	return (ret);
3430 }
3431 
3432 /*
3433  * zfs mount -a [nfs | iscsi]
3434  * zfs mount filesystem
3435  *
3436  * Mount all filesystems, or mount the given filesystem.
3437  */
3438 static int
3439 zfs_do_mount(int argc, char **argv)
3440 {
3441 	return (share_mount(OP_MOUNT, argc, argv));
3442 }
3443 
3444 /*
3445  * zfs share -a [nfs | iscsi | smb]
3446  * zfs share filesystem
3447  *
3448  * Share all filesystems, or share the given filesystem.
3449  */
3450 static int
3451 zfs_do_share(int argc, char **argv)
3452 {
3453 	return (share_mount(OP_SHARE, argc, argv));
3454 }
3455 
3456 typedef struct unshare_unmount_node {
3457 	zfs_handle_t	*un_zhp;
3458 	char		*un_mountp;
3459 	uu_avl_node_t	un_avlnode;
3460 } unshare_unmount_node_t;
3461 
3462 /* ARGSUSED */
3463 static int
3464 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
3465 {
3466 	const unshare_unmount_node_t *l = larg;
3467 	const unshare_unmount_node_t *r = rarg;
3468 
3469 	return (strcmp(l->un_mountp, r->un_mountp));
3470 }
3471 
3472 /*
3473  * Convenience routine used by zfs_do_umount() and manual_unmount().  Given an
3474  * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
3475  * and unmount it appropriately.
3476  */
3477 static int
3478 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
3479 {
3480 	zfs_handle_t *zhp;
3481 	int ret;
3482 	struct stat64 statbuf;
3483 	struct extmnttab entry;
3484 	const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
3485 	ino_t path_inode;
3486 
3487 	/*
3488 	 * Search for the path in /etc/mnttab.  Rather than looking for the
3489 	 * specific path, which can be fooled by non-standard paths (i.e. ".."
3490 	 * or "//"), we stat() the path and search for the corresponding
3491 	 * (major,minor) device pair.
3492 	 */
3493 	if (stat64(path, &statbuf) != 0) {
3494 		(void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3495 		    cmdname, path, strerror(errno));
3496 		return (1);
3497 	}
3498 	path_inode = statbuf.st_ino;
3499 
3500 	/*
3501 	 * Search for the given (major,minor) pair in the mount table.
3502 	 */
3503 	rewind(mnttab_file);
3504 	while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
3505 		if (entry.mnt_major == major(statbuf.st_dev) &&
3506 		    entry.mnt_minor == minor(statbuf.st_dev))
3507 			break;
3508 	}
3509 	if (ret != 0) {
3510 		if (op == OP_SHARE) {
3511 			(void) fprintf(stderr, gettext("cannot %s '%s': not "
3512 			    "currently mounted\n"), cmdname, path);
3513 			return (1);
3514 		}
3515 		(void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
3516 		    path);
3517 		if ((ret = umount2(path, flags)) != 0)
3518 			(void) fprintf(stderr, gettext("%s: %s\n"), path,
3519 			    strerror(errno));
3520 		return (ret != 0);
3521 	}
3522 
3523 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
3524 		(void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
3525 		    "filesystem\n"), cmdname, path);
3526 		return (1);
3527 	}
3528 
3529 	if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3530 	    ZFS_TYPE_FILESYSTEM)) == NULL)
3531 		return (1);
3532 
3533 
3534 	ret = 1;
3535 	if (op == OP_SHARE) {
3536 		char nfs_mnt_prop[ZFS_MAXPROPLEN];
3537 		char smbshare_prop[ZFS_MAXPROPLEN];
3538 
3539 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
3540 		    sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
3541 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
3542 		    sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
3543 
3544 		if (strcmp(nfs_mnt_prop, "off") == 0 &&
3545 		    strcmp(smbshare_prop, "off") == 0) {
3546 			(void) fprintf(stderr, gettext("cannot unshare "
3547 			    "'%s': legacy share\n"), path);
3548 			(void) fprintf(stderr, gettext("use "
3549 			    "unshare(1M) to unshare this filesystem\n"));
3550 		} else if (!zfs_is_shared(zhp)) {
3551 			(void) fprintf(stderr, gettext("cannot unshare '%s': "
3552 			    "not currently shared\n"), path);
3553 		} else {
3554 			ret = zfs_unshareall_bypath(zhp, path);
3555 		}
3556 	} else {
3557 		char mtpt_prop[ZFS_MAXPROPLEN];
3558 
3559 		verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
3560 		    sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
3561 
3562 		if (stat64(entry.mnt_mountp, &statbuf) != 0) {
3563 			(void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3564 			    cmdname, path, strerror(errno));
3565 		} else if (statbuf.st_ino != path_inode) {
3566 			(void) fprintf(stderr, gettext("cannot "
3567 			    "unmount '%s': not a mountpoint\n"), path);
3568 		} else if (is_manual) {
3569 			ret = zfs_unmount(zhp, NULL, flags);
3570 		} else if (strcmp(mtpt_prop, "legacy") == 0) {
3571 			(void) fprintf(stderr, gettext("cannot unmount "
3572 			    "'%s': legacy mountpoint\n"),
3573 			    zfs_get_name(zhp));
3574 			(void) fprintf(stderr, gettext("use umount(1M) "
3575 			    "to unmount this filesystem\n"));
3576 		} else {
3577 			ret = zfs_unmountall(zhp, flags);
3578 		}
3579 	}
3580 
3581 	zfs_close(zhp);
3582 
3583 	return (ret != 0);
3584 }
3585 
3586 /*
3587  * Generic callback for unsharing or unmounting a filesystem.
3588  */
3589 static int
3590 unshare_unmount(int op, int argc, char **argv)
3591 {
3592 	int do_all = 0;
3593 	int flags = 0;
3594 	int ret = 0;
3595 	int types, c;
3596 	zfs_handle_t *zhp;
3597 	char nfsiscsi_mnt_prop[ZFS_MAXPROPLEN];
3598 	char sharesmb[ZFS_MAXPROPLEN];
3599 
3600 	/* check options */
3601 	while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
3602 		switch (c) {
3603 		case 'a':
3604 			do_all = 1;
3605 			break;
3606 		case 'f':
3607 			flags = MS_FORCE;
3608 			break;
3609 		case '?':
3610 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3611 			    optopt);
3612 			usage(B_FALSE);
3613 		}
3614 	}
3615 
3616 	argc -= optind;
3617 	argv += optind;
3618 
3619 	if (do_all) {
3620 		/*
3621 		 * We could make use of zfs_for_each() to walk all datasets in
3622 		 * the system, but this would be very inefficient, especially
3623 		 * since we would have to linearly search /etc/mnttab for each
3624 		 * one.  Instead, do one pass through /etc/mnttab looking for
3625 		 * zfs entries and call zfs_unmount() for each one.
3626 		 *
3627 		 * Things get a little tricky if the administrator has created
3628 		 * mountpoints beneath other ZFS filesystems.  In this case, we
3629 		 * have to unmount the deepest filesystems first.  To accomplish
3630 		 * this, we place all the mountpoints in an AVL tree sorted by
3631 		 * the special type (dataset name), and walk the result in
3632 		 * reverse to make sure to get any snapshots first.
3633 		 */
3634 		struct mnttab entry;
3635 		uu_avl_pool_t *pool;
3636 		uu_avl_t *tree;
3637 		unshare_unmount_node_t *node;
3638 		uu_avl_index_t idx;
3639 		uu_avl_walk_t *walk;
3640 
3641 		if (argc != 0) {
3642 			(void) fprintf(stderr, gettext("too many arguments\n"));
3643 			usage(B_FALSE);
3644 		}
3645 
3646 		if ((pool = uu_avl_pool_create("unmount_pool",
3647 		    sizeof (unshare_unmount_node_t),
3648 		    offsetof(unshare_unmount_node_t, un_avlnode),
3649 		    unshare_unmount_compare,
3650 		    UU_DEFAULT)) == NULL) {
3651 			(void) fprintf(stderr, gettext("internal error: "
3652 			    "out of memory\n"));
3653 			exit(1);
3654 		}
3655 
3656 		if ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL) {
3657 			(void) fprintf(stderr, gettext("internal error: "
3658 			    "out of memory\n"));
3659 			exit(1);
3660 		}
3661 
3662 		rewind(mnttab_file);
3663 		while (getmntent(mnttab_file, &entry) == 0) {
3664 
3665 			/* ignore non-ZFS entries */
3666 			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
3667 				continue;
3668 
3669 			/* ignore snapshots */
3670 			if (strchr(entry.mnt_special, '@') != NULL)
3671 				continue;
3672 
3673 			if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3674 			    ZFS_TYPE_FILESYSTEM)) == NULL) {
3675 				ret = 1;
3676 				continue;
3677 			}
3678 
3679 			switch (op) {
3680 			case OP_SHARE:
3681 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3682 				    nfsiscsi_mnt_prop,
3683 				    sizeof (nfsiscsi_mnt_prop),
3684 				    NULL, NULL, 0, B_FALSE) == 0);
3685 				if (strcmp(nfsiscsi_mnt_prop, "off") != 0)
3686 					break;
3687 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3688 				    nfsiscsi_mnt_prop,
3689 				    sizeof (nfsiscsi_mnt_prop),
3690 				    NULL, NULL, 0, B_FALSE) == 0);
3691 				if (strcmp(nfsiscsi_mnt_prop, "off") == 0)
3692 					continue;
3693 				break;
3694 			case OP_MOUNT:
3695 				/* Ignore legacy mounts */
3696 				verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
3697 				    nfsiscsi_mnt_prop,
3698 				    sizeof (nfsiscsi_mnt_prop),
3699 				    NULL, NULL, 0, B_FALSE) == 0);
3700 				if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0)
3701 					continue;
3702 				/* Ignore canmount=noauto mounts */
3703 				if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
3704 				    ZFS_CANMOUNT_NOAUTO)
3705 					continue;
3706 			default:
3707 				break;
3708 			}
3709 
3710 			node = safe_malloc(sizeof (unshare_unmount_node_t));
3711 			node->un_zhp = zhp;
3712 
3713 			if ((node->un_mountp = strdup(entry.mnt_mountp)) ==
3714 			    NULL) {
3715 				(void) fprintf(stderr, gettext("internal error:"
3716 				    " out of memory\n"));
3717 				exit(1);
3718 			}
3719 
3720 			uu_avl_node_init(node, &node->un_avlnode, pool);
3721 
3722 			if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
3723 				uu_avl_insert(tree, node, idx);
3724 			} else {
3725 				zfs_close(node->un_zhp);
3726 				free(node->un_mountp);
3727 				free(node);
3728 			}
3729 		}
3730 
3731 		/*
3732 		 * Walk the AVL tree in reverse, unmounting each filesystem and
3733 		 * removing it from the AVL tree in the process.
3734 		 */
3735 		if ((walk = uu_avl_walk_start(tree,
3736 		    UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) {
3737 			(void) fprintf(stderr,
3738 			    gettext("internal error: out of memory"));
3739 			exit(1);
3740 		}
3741 
3742 		while ((node = uu_avl_walk_next(walk)) != NULL) {
3743 			uu_avl_remove(tree, node);
3744 
3745 			switch (op) {
3746 			case OP_SHARE:
3747 				if (zfs_unshareall_bypath(node->un_zhp,
3748 				    node->un_mountp) != 0)
3749 					ret = 1;
3750 				break;
3751 
3752 			case OP_MOUNT:
3753 				if (zfs_unmount(node->un_zhp,
3754 				    node->un_mountp, flags) != 0)
3755 					ret = 1;
3756 				break;
3757 			}
3758 
3759 			zfs_close(node->un_zhp);
3760 			free(node->un_mountp);
3761 			free(node);
3762 		}
3763 
3764 		uu_avl_walk_end(walk);
3765 		uu_avl_destroy(tree);
3766 		uu_avl_pool_destroy(pool);
3767 
3768 		if (op == OP_SHARE) {
3769 			/*
3770 			 * Finally, unshare any volumes shared via iSCSI.
3771 			 */
3772 			zfs_handle_t **dslist = NULL;
3773 			size_t i, count = 0;
3774 
3775 			get_all_datasets(ZFS_TYPE_VOLUME, &dslist, &count,
3776 			    B_FALSE);
3777 
3778 			if (count != 0) {
3779 				qsort(dslist, count, sizeof (void *),
3780 				    dataset_cmp);
3781 
3782 				for (i = 0; i < count; i++) {
3783 					if (zfs_unshare_iscsi(dslist[i]) != 0)
3784 						ret = 1;
3785 					zfs_close(dslist[i]);
3786 				}
3787 
3788 				free(dslist);
3789 			}
3790 		}
3791 	} else {
3792 		if (argc != 1) {
3793 			if (argc == 0)
3794 				(void) fprintf(stderr,
3795 				    gettext("missing filesystem argument\n"));
3796 			else
3797 				(void) fprintf(stderr,
3798 				    gettext("too many arguments\n"));
3799 			usage(B_FALSE);
3800 		}
3801 
3802 		/*
3803 		 * We have an argument, but it may be a full path or a ZFS
3804 		 * filesystem.  Pass full paths off to unmount_path() (shared by
3805 		 * manual_unmount), otherwise open the filesystem and pass to
3806 		 * zfs_unmount().
3807 		 */
3808 		if (argv[0][0] == '/')
3809 			return (unshare_unmount_path(op, argv[0],
3810 			    flags, B_FALSE));
3811 
3812 		types = ZFS_TYPE_FILESYSTEM;
3813 		if (op == OP_SHARE)
3814 			types |= ZFS_TYPE_VOLUME;
3815 
3816 		if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3817 			return (1);
3818 
3819 		if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
3820 			verify(zfs_prop_get(zhp, op == OP_SHARE ?
3821 			    ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
3822 			    nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop), NULL,
3823 			    NULL, 0, B_FALSE) == 0);
3824 
3825 			switch (op) {
3826 			case OP_SHARE:
3827 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3828 				    nfsiscsi_mnt_prop,
3829 				    sizeof (nfsiscsi_mnt_prop),
3830 				    NULL, NULL, 0, B_FALSE) == 0);
3831 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3832 				    sharesmb, sizeof (sharesmb), NULL, NULL,
3833 				    0, B_FALSE) == 0);
3834 
3835 				if (strcmp(nfsiscsi_mnt_prop, "off") == 0 &&
3836 				    strcmp(sharesmb, "off") == 0) {
3837 					(void) fprintf(stderr, gettext("cannot "
3838 					    "unshare '%s': legacy share\n"),
3839 					    zfs_get_name(zhp));
3840 					(void) fprintf(stderr, gettext("use "
3841 					    "unshare(1M) to unshare this "
3842 					    "filesystem\n"));
3843 					ret = 1;
3844 				} else if (!zfs_is_shared(zhp)) {
3845 					(void) fprintf(stderr, gettext("cannot "
3846 					    "unshare '%s': not currently "
3847 					    "shared\n"), zfs_get_name(zhp));
3848 					ret = 1;
3849 				} else if (zfs_unshareall(zhp) != 0) {
3850 					ret = 1;
3851 				}
3852 				break;
3853 
3854 			case OP_MOUNT:
3855 				if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0) {
3856 					(void) fprintf(stderr, gettext("cannot "
3857 					    "unmount '%s': legacy "
3858 					    "mountpoint\n"), zfs_get_name(zhp));
3859 					(void) fprintf(stderr, gettext("use "
3860 					    "umount(1M) to unmount this "
3861 					    "filesystem\n"));
3862 					ret = 1;
3863 				} else if (!zfs_is_mounted(zhp, NULL)) {
3864 					(void) fprintf(stderr, gettext("cannot "
3865 					    "unmount '%s': not currently "
3866 					    "mounted\n"),
3867 					    zfs_get_name(zhp));
3868 					ret = 1;
3869 				} else if (zfs_unmountall(zhp, flags) != 0) {
3870 					ret = 1;
3871 				}
3872 				break;
3873 			}
3874 		} else {
3875 			assert(op == OP_SHARE);
3876 
3877 			verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI,
3878 			    nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop),
3879 			    NULL, NULL, 0, B_FALSE) == 0);
3880 
3881 			if (strcmp(nfsiscsi_mnt_prop, "off") == 0) {
3882 				(void) fprintf(stderr, gettext("cannot unshare "
3883 				    "'%s': 'shareiscsi' property not set\n"),
3884 				    zfs_get_name(zhp));
3885 				(void) fprintf(stderr, gettext("set "
3886 				    "'shareiscsi' property or use "
3887 				    "iscsitadm(1M) to share this volume\n"));
3888 				ret = 1;
3889 			} else if (!zfs_is_shared_iscsi(zhp)) {
3890 				(void) fprintf(stderr, gettext("cannot "
3891 				    "unshare '%s': not currently shared\n"),
3892 				    zfs_get_name(zhp));
3893 				ret = 1;
3894 			} else if (zfs_unshare_iscsi(zhp) != 0) {
3895 				ret = 1;
3896 			}
3897 		}
3898 
3899 		zfs_close(zhp);
3900 	}
3901 
3902 	return (ret);
3903 }
3904 
3905 /*
3906  * zfs unmount -a
3907  * zfs unmount filesystem
3908  *
3909  * Unmount all filesystems, or a specific ZFS filesystem.
3910  */
3911 static int
3912 zfs_do_unmount(int argc, char **argv)
3913 {
3914 	return (unshare_unmount(OP_MOUNT, argc, argv));
3915 }
3916 
3917 /*
3918  * zfs unshare -a
3919  * zfs unshare filesystem
3920  *
3921  * Unshare all filesystems, or a specific ZFS filesystem.
3922  */
3923 static int
3924 zfs_do_unshare(int argc, char **argv)
3925 {
3926 	return (unshare_unmount(OP_SHARE, argc, argv));
3927 }
3928 
3929 /*
3930  * Called when invoked as /etc/fs/zfs/mount.  Do the mount if the mountpoint is
3931  * 'legacy'.  Otherwise, complain that use should be using 'zfs mount'.
3932  */
3933 static int
3934 manual_mount(int argc, char **argv)
3935 {
3936 	zfs_handle_t *zhp;
3937 	char mountpoint[ZFS_MAXPROPLEN];
3938 	char mntopts[MNT_LINE_MAX] = { '\0' };
3939 	int ret;
3940 	int c;
3941 	int flags = 0;
3942 	char *dataset, *path;
3943 
3944 	/* check options */
3945 	while ((c = getopt(argc, argv, ":mo:O")) != -1) {
3946 		switch (c) {
3947 		case 'o':
3948 			(void) strlcpy(mntopts, optarg, sizeof (mntopts));
3949 			break;
3950 		case 'O':
3951 			flags |= MS_OVERLAY;
3952 			break;
3953 		case 'm':
3954 			flags |= MS_NOMNTTAB;
3955 			break;
3956 		case ':':
3957 			(void) fprintf(stderr, gettext("missing argument for "
3958 			    "'%c' option\n"), optopt);
3959 			usage(B_FALSE);
3960 			break;
3961 		case '?':
3962 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3963 			    optopt);
3964 			(void) fprintf(stderr, gettext("usage: mount [-o opts] "
3965 			    "<path>\n"));
3966 			return (2);
3967 		}
3968 	}
3969 
3970 	argc -= optind;
3971 	argv += optind;
3972 
3973 	/* check that we only have two arguments */
3974 	if (argc != 2) {
3975 		if (argc == 0)
3976 			(void) fprintf(stderr, gettext("missing dataset "
3977 			    "argument\n"));
3978 		else if (argc == 1)
3979 			(void) fprintf(stderr,
3980 			    gettext("missing mountpoint argument\n"));
3981 		else
3982 			(void) fprintf(stderr, gettext("too many arguments\n"));
3983 		(void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
3984 		return (2);
3985 	}
3986 
3987 	dataset = argv[0];
3988 	path = argv[1];
3989 
3990 	/* try to open the dataset */
3991 	if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
3992 		return (1);
3993 
3994 	(void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
3995 	    sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
3996 
3997 	/* check for legacy mountpoint and complain appropriately */
3998 	ret = 0;
3999 	if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
4000 		if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS,
4001 		    NULL, 0, mntopts, sizeof (mntopts)) != 0) {
4002 			(void) fprintf(stderr, gettext("mount failed: %s\n"),
4003 			    strerror(errno));
4004 			ret = 1;
4005 		}
4006 	} else {
4007 		(void) fprintf(stderr, gettext("filesystem '%s' cannot be "
4008 		    "mounted using 'mount -F zfs'\n"), dataset);
4009 		(void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
4010 		    "instead.\n"), path);
4011 		(void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
4012 		    "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
4013 		(void) fprintf(stderr, gettext("See zfs(1M) for more "
4014 		    "information.\n"));
4015 		ret = 1;
4016 	}
4017 
4018 	return (ret);
4019 }
4020 
4021 /*
4022  * Called when invoked as /etc/fs/zfs/umount.  Unlike a manual mount, we allow
4023  * unmounts of non-legacy filesystems, as this is the dominant administrative
4024  * interface.
4025  */
4026 static int
4027 manual_unmount(int argc, char **argv)
4028 {
4029 	int flags = 0;
4030 	int c;
4031 
4032 	/* check options */
4033 	while ((c = getopt(argc, argv, "f")) != -1) {
4034 		switch (c) {
4035 		case 'f':
4036 			flags = MS_FORCE;
4037 			break;
4038 		case '?':
4039 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4040 			    optopt);
4041 			(void) fprintf(stderr, gettext("usage: unmount [-f] "
4042 			    "<path>\n"));
4043 			return (2);
4044 		}
4045 	}
4046 
4047 	argc -= optind;
4048 	argv += optind;
4049 
4050 	/* check arguments */
4051 	if (argc != 1) {
4052 		if (argc == 0)
4053 			(void) fprintf(stderr, gettext("missing path "
4054 			    "argument\n"));
4055 		else
4056 			(void) fprintf(stderr, gettext("too many arguments\n"));
4057 		(void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
4058 		return (2);
4059 	}
4060 
4061 	return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
4062 }
4063 
4064 static int
4065 volcheck(zpool_handle_t *zhp, void *data)
4066 {
4067 	boolean_t isinit = *((boolean_t *)data);
4068 
4069 	if (isinit)
4070 		return (zpool_create_zvol_links(zhp));
4071 	else
4072 		return (zpool_remove_zvol_links(zhp));
4073 }
4074 
4075 /*
4076  * Iterate over all pools in the system and either create or destroy /dev/zvol
4077  * links, depending on the value of 'isinit'.
4078  */
4079 static int
4080 do_volcheck(boolean_t isinit)
4081 {
4082 	return (zpool_iter(g_zfs, volcheck, &isinit) ? 1 : 0);
4083 }
4084 
4085 static int
4086 find_command_idx(char *command, int *idx)
4087 {
4088 	int i;
4089 
4090 	for (i = 0; i < NCOMMAND; i++) {
4091 		if (command_table[i].name == NULL)
4092 			continue;
4093 
4094 		if (strcmp(command, command_table[i].name) == 0) {
4095 			*idx = i;
4096 			return (0);
4097 		}
4098 	}
4099 	return (1);
4100 }
4101 
4102 int
4103 main(int argc, char **argv)
4104 {
4105 	int ret;
4106 	int i;
4107 	char *progname;
4108 	char *cmdname;
4109 
4110 	(void) setlocale(LC_ALL, "");
4111 	(void) textdomain(TEXT_DOMAIN);
4112 
4113 	opterr = 0;
4114 
4115 	if ((g_zfs = libzfs_init()) == NULL) {
4116 		(void) fprintf(stderr, gettext("internal error: failed to "
4117 		    "initialize ZFS library\n"));
4118 		return (1);
4119 	}
4120 
4121 	zpool_set_history_str("zfs", argc, argv, history_str);
4122 	verify(zpool_stage_history(g_zfs, history_str) == 0);
4123 
4124 	libzfs_print_on_error(g_zfs, B_TRUE);
4125 
4126 	if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
4127 		(void) fprintf(stderr, gettext("internal error: unable to "
4128 		    "open %s\n"), MNTTAB);
4129 		return (1);
4130 	}
4131 
4132 	/*
4133 	 * This command also doubles as the /etc/fs mount and unmount program.
4134 	 * Determine if we should take this behavior based on argv[0].
4135 	 */
4136 	progname = basename(argv[0]);
4137 	if (strcmp(progname, "mount") == 0) {
4138 		ret = manual_mount(argc, argv);
4139 	} else if (strcmp(progname, "umount") == 0) {
4140 		ret = manual_unmount(argc, argv);
4141 	} else {
4142 		/*
4143 		 * Make sure the user has specified some command.
4144 		 */
4145 		if (argc < 2) {
4146 			(void) fprintf(stderr, gettext("missing command\n"));
4147 			usage(B_FALSE);
4148 		}
4149 
4150 		cmdname = argv[1];
4151 
4152 		/*
4153 		 * The 'umount' command is an alias for 'unmount'
4154 		 */
4155 		if (strcmp(cmdname, "umount") == 0)
4156 			cmdname = "unmount";
4157 
4158 		/*
4159 		 * The 'recv' command is an alias for 'receive'
4160 		 */
4161 		if (strcmp(cmdname, "recv") == 0)
4162 			cmdname = "receive";
4163 
4164 		/*
4165 		 * Special case '-?'
4166 		 */
4167 		if (strcmp(cmdname, "-?") == 0)
4168 			usage(B_TRUE);
4169 
4170 		/*
4171 		 * 'volinit' and 'volfini' do not appear in the usage message,
4172 		 * so we have to special case them here.
4173 		 */
4174 		if (strcmp(cmdname, "volinit") == 0)
4175 			return (do_volcheck(B_TRUE));
4176 		else if (strcmp(cmdname, "volfini") == 0)
4177 			return (do_volcheck(B_FALSE));
4178 
4179 		/*
4180 		 * Run the appropriate command.
4181 		 */
4182 		if (find_command_idx(cmdname, &i) == 0) {
4183 			current_command = &command_table[i];
4184 			ret = command_table[i].func(argc - 1, argv + 1);
4185 		} else if (strchr(cmdname, '=') != NULL) {
4186 			verify(find_command_idx("set", &i) == 0);
4187 			current_command = &command_table[i];
4188 			ret = command_table[i].func(argc, argv);
4189 		} else {
4190 			(void) fprintf(stderr, gettext("unrecognized "
4191 			    "command '%s'\n"), cmdname);
4192 			usage(B_FALSE);
4193 		}
4194 	}
4195 
4196 	(void) fclose(mnttab_file);
4197 
4198 	libzfs_fini(g_zfs);
4199 
4200 	/*
4201 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4202 	 * for the purposes of running ::findleaks.
4203 	 */
4204 	if (getenv("ZFS_ABORT") != NULL) {
4205 		(void) printf("dumping core by request\n");
4206 		abort();
4207 	}
4208 
4209 	return (ret);
4210 }
4211