xref: /illumos-gate/usr/src/cmd/zfs/zfs_main.c (revision b6c10d8075f5dee6b99f706ed08f3b012a8dcbdd)
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 	zfs_prop_t prop = zfs_name_to_prop(propname);
1290 
1291 	/*
1292 	 * If we're doing it recursively, then ignore properties that
1293 	 * are not valid for this type of dataset.
1294 	 */
1295 	if (prop != ZPROP_INVAL &&
1296 	    !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1297 		return (0);
1298 
1299 	return (zfs_prop_inherit(zhp, propname) != 0);
1300 }
1301 
1302 static int
1303 inherit_cb(zfs_handle_t *zhp, void *data)
1304 {
1305 	char *propname = data;
1306 
1307 	return (zfs_prop_inherit(zhp, propname) != 0);
1308 }
1309 
1310 static int
1311 zfs_do_inherit(int argc, char **argv)
1312 {
1313 	boolean_t recurse = B_FALSE;
1314 	int c;
1315 	zfs_prop_t prop;
1316 	char *propname;
1317 	int ret;
1318 
1319 	/* check options */
1320 	while ((c = getopt(argc, argv, "r")) != -1) {
1321 		switch (c) {
1322 		case 'r':
1323 			recurse = B_TRUE;
1324 			break;
1325 		case '?':
1326 		default:
1327 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1328 			    optopt);
1329 			usage(B_FALSE);
1330 		}
1331 	}
1332 
1333 	argc -= optind;
1334 	argv += optind;
1335 
1336 	/* check number of arguments */
1337 	if (argc < 1) {
1338 		(void) fprintf(stderr, gettext("missing property argument\n"));
1339 		usage(B_FALSE);
1340 	}
1341 	if (argc < 2) {
1342 		(void) fprintf(stderr, gettext("missing dataset argument\n"));
1343 		usage(B_FALSE);
1344 	}
1345 
1346 	propname = argv[0];
1347 	argc--;
1348 	argv++;
1349 
1350 	if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1351 		if (zfs_prop_readonly(prop)) {
1352 			(void) fprintf(stderr, gettext(
1353 			    "%s property is read-only\n"),
1354 			    propname);
1355 			return (1);
1356 		}
1357 		if (!zfs_prop_inheritable(prop)) {
1358 			(void) fprintf(stderr, gettext("'%s' property cannot "
1359 			    "be inherited\n"), propname);
1360 			if (prop == ZFS_PROP_QUOTA ||
1361 			    prop == ZFS_PROP_RESERVATION ||
1362 			    prop == ZFS_PROP_REFQUOTA ||
1363 			    prop == ZFS_PROP_REFRESERVATION)
1364 				(void) fprintf(stderr, gettext("use 'zfs set "
1365 				    "%s=none' to clear\n"), propname);
1366 			return (1);
1367 		}
1368 	} else if (!zfs_prop_user(propname)) {
1369 		(void) fprintf(stderr, gettext("invalid property '%s'\n"),
1370 		    propname);
1371 		usage(B_FALSE);
1372 	}
1373 
1374 	if (recurse) {
1375 		ret = zfs_for_each(argc, argv, recurse, ZFS_TYPE_DATASET,
1376 		    NULL, NULL, inherit_recurse_cb, propname, B_FALSE);
1377 	} else {
1378 		ret = zfs_for_each(argc, argv, recurse, ZFS_TYPE_DATASET,
1379 		    NULL, NULL, inherit_cb, propname, B_FALSE);
1380 	}
1381 
1382 	return (ret);
1383 }
1384 
1385 typedef struct upgrade_cbdata {
1386 	uint64_t cb_numupgraded;
1387 	uint64_t cb_numsamegraded;
1388 	uint64_t cb_numfailed;
1389 	uint64_t cb_version;
1390 	boolean_t cb_newer;
1391 	boolean_t cb_foundone;
1392 	char cb_lastfs[ZFS_MAXNAMELEN];
1393 } upgrade_cbdata_t;
1394 
1395 static int
1396 same_pool(zfs_handle_t *zhp, const char *name)
1397 {
1398 	int len1 = strcspn(name, "/@");
1399 	const char *zhname = zfs_get_name(zhp);
1400 	int len2 = strcspn(zhname, "/@");
1401 
1402 	if (len1 != len2)
1403 		return (B_FALSE);
1404 	return (strncmp(name, zhname, len1) == 0);
1405 }
1406 
1407 static int
1408 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1409 {
1410 	upgrade_cbdata_t *cb = data;
1411 	int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1412 
1413 	/* list if it's old/new */
1414 	if ((!cb->cb_newer && version < ZPL_VERSION) ||
1415 	    (cb->cb_newer && version > ZPL_VERSION)) {
1416 		char *str;
1417 		if (cb->cb_newer) {
1418 			str = gettext("The following filesystems are "
1419 			    "formatted using a newer software version and\n"
1420 			    "cannot be accessed on the current system.\n\n");
1421 		} else {
1422 			str = gettext("The following filesystems are "
1423 			    "out of date, and can be upgraded.  After being\n"
1424 			    "upgraded, these filesystems (and any 'zfs send' "
1425 			    "streams generated from\n"
1426 			    "subsequent snapshots) will no longer be "
1427 			    "accessible by older software versions.\n\n");
1428 		}
1429 
1430 		if (!cb->cb_foundone) {
1431 			(void) puts(str);
1432 			(void) printf(gettext("VER  FILESYSTEM\n"));
1433 			(void) printf(gettext("---  ------------\n"));
1434 			cb->cb_foundone = B_TRUE;
1435 		}
1436 
1437 		(void) printf("%2u   %s\n", version, zfs_get_name(zhp));
1438 	}
1439 
1440 	return (0);
1441 }
1442 
1443 static int
1444 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1445 {
1446 	upgrade_cbdata_t *cb = data;
1447 	int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1448 
1449 	if (cb->cb_version >= ZPL_VERSION_FUID) {
1450 		int spa_version;
1451 
1452 		if (zfs_spa_version(zhp, &spa_version) < 0)
1453 			return (-1);
1454 
1455 		if (spa_version < SPA_VERSION_FUID) {
1456 			/* can't upgrade */
1457 			(void) printf(gettext("%s: can not be upgraded; "
1458 			    "the pool version needs to first be upgraded\nto "
1459 			    "version %d\n\n"),
1460 			    zfs_get_name(zhp), SPA_VERSION_FUID);
1461 			cb->cb_numfailed++;
1462 			return (0);
1463 		}
1464 	}
1465 
1466 	/* upgrade */
1467 	if (version < cb->cb_version) {
1468 		char verstr[16];
1469 		(void) snprintf(verstr, sizeof (verstr),
1470 		    "%llu", cb->cb_version);
1471 		if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1472 			/*
1473 			 * If they did "zfs upgrade -a", then we could
1474 			 * be doing ioctls to different pools.  We need
1475 			 * to log this history once to each pool.
1476 			 */
1477 			verify(zpool_stage_history(g_zfs, history_str) == 0);
1478 		}
1479 		if (zfs_prop_set(zhp, "version", verstr) == 0)
1480 			cb->cb_numupgraded++;
1481 		else
1482 			cb->cb_numfailed++;
1483 		(void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1484 	} else if (version > cb->cb_version) {
1485 		/* can't downgrade */
1486 		(void) printf(gettext("%s: can not be downgraded; "
1487 		    "it is already at version %u\n"),
1488 		    zfs_get_name(zhp), version);
1489 		cb->cb_numfailed++;
1490 	} else {
1491 		cb->cb_numsamegraded++;
1492 	}
1493 	return (0);
1494 }
1495 
1496 /*
1497  * zfs upgrade
1498  * zfs upgrade -v
1499  * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1500  */
1501 static int
1502 zfs_do_upgrade(int argc, char **argv)
1503 {
1504 	boolean_t recurse = B_FALSE;
1505 	boolean_t all = B_FALSE;
1506 	boolean_t showversions = B_FALSE;
1507 	int ret;
1508 	upgrade_cbdata_t cb = { 0 };
1509 	char c;
1510 
1511 	/* check options */
1512 	while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1513 		switch (c) {
1514 		case 'r':
1515 			recurse = B_TRUE;
1516 			break;
1517 		case 'v':
1518 			showversions = B_TRUE;
1519 			break;
1520 		case 'V':
1521 			if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1522 			    optarg, &cb.cb_version) != 0) {
1523 				(void) fprintf(stderr,
1524 				    gettext("invalid version %s\n"), optarg);
1525 				usage(B_FALSE);
1526 			}
1527 			break;
1528 		case 'a':
1529 			all = B_TRUE;
1530 			break;
1531 		case '?':
1532 		default:
1533 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1534 			    optopt);
1535 			usage(B_FALSE);
1536 		}
1537 	}
1538 
1539 	argc -= optind;
1540 	argv += optind;
1541 
1542 	if ((!all && !argc) && (recurse | cb.cb_version))
1543 		usage(B_FALSE);
1544 	if (showversions && (recurse || all || cb.cb_version || argc))
1545 		usage(B_FALSE);
1546 	if ((all || argc) && (showversions))
1547 		usage(B_FALSE);
1548 	if (all && argc)
1549 		usage(B_FALSE);
1550 
1551 	if (showversions) {
1552 		/* Show info on available versions. */
1553 		(void) printf(gettext("The following filesystem versions are "
1554 		    "supported:\n\n"));
1555 		(void) printf(gettext("VER  DESCRIPTION\n"));
1556 		(void) printf("---  -----------------------------------------"
1557 		    "---------------\n");
1558 		(void) printf(gettext(" 1   Initial ZFS filesystem version\n"));
1559 		(void) printf(gettext(" 2   Enhanced directory entries\n"));
1560 		(void) printf(gettext(" 3   Case insensitive and File system "
1561 		    "unique identifer (FUID)\n"));
1562 		(void) printf(gettext("\nFor more information on a particular "
1563 		    "version, including supported releases, see:\n\n"));
1564 		(void) printf("http://www.opensolaris.org/os/community/zfs/"
1565 		    "version/zpl/N\n\n");
1566 		(void) printf(gettext("Where 'N' is the version number.\n"));
1567 		ret = 0;
1568 	} else if (argc || all) {
1569 		/* Upgrade filesystems */
1570 		if (cb.cb_version == 0)
1571 			cb.cb_version = ZPL_VERSION;
1572 		ret = zfs_for_each(argc, argv, recurse, ZFS_TYPE_FILESYSTEM,
1573 		    NULL, NULL, upgrade_set_callback, &cb, B_TRUE);
1574 		(void) printf(gettext("%llu filesystems upgraded\n"),
1575 		    cb.cb_numupgraded);
1576 		if (cb.cb_numsamegraded) {
1577 			(void) printf(gettext("%llu filesystems already at "
1578 			    "this version\n"),
1579 			    cb.cb_numsamegraded);
1580 		}
1581 		if (cb.cb_numfailed != 0)
1582 			ret = 1;
1583 	} else {
1584 		/* List old-version filesytems */
1585 		boolean_t found;
1586 		(void) printf(gettext("This system is currently running "
1587 		    "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
1588 
1589 		ret = zfs_for_each(0, NULL, B_TRUE, ZFS_TYPE_FILESYSTEM,
1590 		    NULL, NULL, upgrade_list_callback, &cb, B_TRUE);
1591 
1592 		found = cb.cb_foundone;
1593 		cb.cb_foundone = B_FALSE;
1594 		cb.cb_newer = B_TRUE;
1595 
1596 		ret = zfs_for_each(0, NULL, B_TRUE, ZFS_TYPE_FILESYSTEM,
1597 		    NULL, NULL, upgrade_list_callback, &cb, B_TRUE);
1598 
1599 		if (!cb.cb_foundone && !found) {
1600 			(void) printf(gettext("All filesystems are "
1601 			    "formatted with the current version.\n"));
1602 		}
1603 	}
1604 
1605 	return (ret);
1606 }
1607 
1608 /*
1609  * list [-rH] [-o property[,property]...] [-t type[,type]...]
1610  *      [-s property [-s property]...] [-S property [-S property]...]
1611  *      <dataset> ...
1612  *
1613  * 	-r	Recurse over all children
1614  * 	-H	Scripted mode; elide headers and separate columns by tabs
1615  * 	-o	Control which fields to display.
1616  * 	-t	Control which object types to display.
1617  *	-s	Specify sort columns, descending order.
1618  *	-S	Specify sort columns, ascending order.
1619  *
1620  * When given no arguments, lists all filesystems in the system.
1621  * Otherwise, list the specified datasets, optionally recursing down them if
1622  * '-r' is specified.
1623  */
1624 typedef struct list_cbdata {
1625 	boolean_t	cb_first;
1626 	boolean_t	cb_scripted;
1627 	zprop_list_t	*cb_proplist;
1628 } list_cbdata_t;
1629 
1630 /*
1631  * Given a list of columns to display, output appropriate headers for each one.
1632  */
1633 static void
1634 print_header(zprop_list_t *pl)
1635 {
1636 	char headerbuf[ZFS_MAXPROPLEN];
1637 	const char *header;
1638 	int i;
1639 	boolean_t first = B_TRUE;
1640 	boolean_t right_justify;
1641 
1642 	for (; pl != NULL; pl = pl->pl_next) {
1643 		if (!first) {
1644 			(void) printf("  ");
1645 		} else {
1646 			first = B_FALSE;
1647 		}
1648 
1649 		right_justify = B_FALSE;
1650 		if (pl->pl_prop != ZPROP_INVAL) {
1651 			header = zfs_prop_column_name(pl->pl_prop);
1652 			right_justify = zfs_prop_align_right(pl->pl_prop);
1653 		} else {
1654 			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
1655 				headerbuf[i] = toupper(pl->pl_user_prop[i]);
1656 			headerbuf[i] = '\0';
1657 			header = headerbuf;
1658 		}
1659 
1660 		if (pl->pl_next == NULL && !right_justify)
1661 			(void) printf("%s", header);
1662 		else if (right_justify)
1663 			(void) printf("%*s", pl->pl_width, header);
1664 		else
1665 			(void) printf("%-*s", pl->pl_width, header);
1666 	}
1667 
1668 	(void) printf("\n");
1669 }
1670 
1671 /*
1672  * Given a dataset and a list of fields, print out all the properties according
1673  * to the described layout.
1674  */
1675 static void
1676 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
1677 {
1678 	boolean_t first = B_TRUE;
1679 	char property[ZFS_MAXPROPLEN];
1680 	nvlist_t *userprops = zfs_get_user_props(zhp);
1681 	nvlist_t *propval;
1682 	char *propstr;
1683 	boolean_t right_justify;
1684 	int width;
1685 
1686 	for (; pl != NULL; pl = pl->pl_next) {
1687 		if (!first) {
1688 			if (scripted)
1689 				(void) printf("\t");
1690 			else
1691 				(void) printf("  ");
1692 		} else {
1693 			first = B_FALSE;
1694 		}
1695 
1696 		right_justify = B_FALSE;
1697 		if (pl->pl_prop != ZPROP_INVAL) {
1698 			if (zfs_prop_get(zhp, pl->pl_prop, property,
1699 			    sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
1700 				propstr = "-";
1701 			else
1702 				propstr = property;
1703 
1704 			right_justify = zfs_prop_align_right(pl->pl_prop);
1705 		} else {
1706 			if (nvlist_lookup_nvlist(userprops,
1707 			    pl->pl_user_prop, &propval) != 0)
1708 				propstr = "-";
1709 			else
1710 				verify(nvlist_lookup_string(propval,
1711 				    ZPROP_VALUE, &propstr) == 0);
1712 		}
1713 
1714 		width = pl->pl_width;
1715 
1716 		/*
1717 		 * If this is being called in scripted mode, or if this is the
1718 		 * last column and it is left-justified, don't include a width
1719 		 * format specifier.
1720 		 */
1721 		if (scripted || (pl->pl_next == NULL && !right_justify))
1722 			(void) printf("%s", propstr);
1723 		else if (right_justify)
1724 			(void) printf("%*s", width, propstr);
1725 		else
1726 			(void) printf("%-*s", width, propstr);
1727 	}
1728 
1729 	(void) printf("\n");
1730 }
1731 
1732 /*
1733  * Generic callback function to list a dataset or snapshot.
1734  */
1735 static int
1736 list_callback(zfs_handle_t *zhp, void *data)
1737 {
1738 	list_cbdata_t *cbp = data;
1739 
1740 	if (cbp->cb_first) {
1741 		if (!cbp->cb_scripted)
1742 			print_header(cbp->cb_proplist);
1743 		cbp->cb_first = B_FALSE;
1744 	}
1745 
1746 	print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
1747 
1748 	return (0);
1749 }
1750 
1751 static int
1752 zfs_do_list(int argc, char **argv)
1753 {
1754 	int c;
1755 	boolean_t recurse = B_FALSE;
1756 	boolean_t scripted = B_FALSE;
1757 	static char default_fields[] =
1758 	    "name,used,available,referenced,mountpoint";
1759 	int types = ZFS_TYPE_DATASET;
1760 	char *fields = NULL;
1761 	char *basic_fields = default_fields;
1762 	list_cbdata_t cb = { 0 };
1763 	char *value;
1764 	int ret;
1765 	char *type_subopts[] = { "filesystem", "volume", "snapshot", NULL };
1766 	zfs_sort_column_t *sortcol = NULL;
1767 
1768 	/* check options */
1769 	while ((c = getopt(argc, argv, ":o:rt:Hs:S:")) != -1) {
1770 		switch (c) {
1771 		case 'o':
1772 			fields = optarg;
1773 			break;
1774 		case 'r':
1775 			recurse = B_TRUE;
1776 			break;
1777 		case 'H':
1778 			scripted = B_TRUE;
1779 			break;
1780 		case 's':
1781 			if (zfs_add_sort_column(&sortcol, optarg,
1782 			    B_FALSE) != 0) {
1783 				(void) fprintf(stderr,
1784 				    gettext("invalid property '%s'\n"), optarg);
1785 				usage(B_FALSE);
1786 			}
1787 			break;
1788 		case 'S':
1789 			if (zfs_add_sort_column(&sortcol, optarg,
1790 			    B_TRUE) != 0) {
1791 				(void) fprintf(stderr,
1792 				    gettext("invalid property '%s'\n"), optarg);
1793 				usage(B_FALSE);
1794 			}
1795 			break;
1796 		case 't':
1797 			types = 0;
1798 			while (*optarg != '\0') {
1799 				switch (getsubopt(&optarg, type_subopts,
1800 				    &value)) {
1801 				case 0:
1802 					types |= ZFS_TYPE_FILESYSTEM;
1803 					break;
1804 				case 1:
1805 					types |= ZFS_TYPE_VOLUME;
1806 					break;
1807 				case 2:
1808 					types |= ZFS_TYPE_SNAPSHOT;
1809 					break;
1810 				default:
1811 					(void) fprintf(stderr,
1812 					    gettext("invalid type '%s'\n"),
1813 					    value);
1814 					usage(B_FALSE);
1815 				}
1816 			}
1817 			break;
1818 		case ':':
1819 			(void) fprintf(stderr, gettext("missing argument for "
1820 			    "'%c' option\n"), optopt);
1821 			usage(B_FALSE);
1822 			break;
1823 		case '?':
1824 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1825 			    optopt);
1826 			usage(B_FALSE);
1827 		}
1828 	}
1829 
1830 	argc -= optind;
1831 	argv += optind;
1832 
1833 	if (fields == NULL)
1834 		fields = basic_fields;
1835 
1836 	/*
1837 	 * If the user specifies '-o all', the zprop_get_list() doesn't
1838 	 * normally include the name of the dataset.  For 'zfs list', we always
1839 	 * want this property to be first.
1840 	 */
1841 	if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1842 	    != 0)
1843 		usage(B_FALSE);
1844 
1845 	cb.cb_scripted = scripted;
1846 	cb.cb_first = B_TRUE;
1847 
1848 	ret = zfs_for_each(argc, argv, recurse, types, sortcol, &cb.cb_proplist,
1849 	    list_callback, &cb, B_TRUE);
1850 
1851 	zprop_free_list(cb.cb_proplist);
1852 	zfs_free_sort_columns(sortcol);
1853 
1854 	if (ret == 0 && cb.cb_first && !cb.cb_scripted)
1855 		(void) printf(gettext("no datasets available\n"));
1856 
1857 	return (ret);
1858 }
1859 
1860 /*
1861  * zfs rename <fs | snap | vol> <fs | snap | vol>
1862  * zfs rename -p <fs | vol> <fs | vol>
1863  * zfs rename -r <snap> <snap>
1864  *
1865  * Renames the given dataset to another of the same type.
1866  *
1867  * The '-p' flag creates all the non-existing ancestors of the target first.
1868  */
1869 /* ARGSUSED */
1870 static int
1871 zfs_do_rename(int argc, char **argv)
1872 {
1873 	zfs_handle_t *zhp;
1874 	int c;
1875 	int ret;
1876 	boolean_t recurse = B_FALSE;
1877 	boolean_t parents = B_FALSE;
1878 
1879 	/* check options */
1880 	while ((c = getopt(argc, argv, "pr")) != -1) {
1881 		switch (c) {
1882 		case 'p':
1883 			parents = B_TRUE;
1884 			break;
1885 		case 'r':
1886 			recurse = B_TRUE;
1887 			break;
1888 		case '?':
1889 		default:
1890 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1891 			    optopt);
1892 			usage(B_FALSE);
1893 		}
1894 	}
1895 
1896 	argc -= optind;
1897 	argv += optind;
1898 
1899 	/* check number of arguments */
1900 	if (argc < 1) {
1901 		(void) fprintf(stderr, gettext("missing source dataset "
1902 		    "argument\n"));
1903 		usage(B_FALSE);
1904 	}
1905 	if (argc < 2) {
1906 		(void) fprintf(stderr, gettext("missing target dataset "
1907 		    "argument\n"));
1908 		usage(B_FALSE);
1909 	}
1910 	if (argc > 2) {
1911 		(void) fprintf(stderr, gettext("too many arguments\n"));
1912 		usage(B_FALSE);
1913 	}
1914 
1915 	if (recurse && parents) {
1916 		(void) fprintf(stderr, gettext("-p and -r options are mutually "
1917 		    "exclusive\n"));
1918 		usage(B_FALSE);
1919 	}
1920 
1921 	if (recurse && strchr(argv[0], '@') == 0) {
1922 		(void) fprintf(stderr, gettext("source dataset for recursive "
1923 		    "rename must be a snapshot\n"));
1924 		usage(B_FALSE);
1925 	}
1926 
1927 	if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
1928 	    ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
1929 		return (1);
1930 
1931 	/* If we were asked and the name looks good, try to create ancestors. */
1932 	if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
1933 	    zfs_create_ancestors(g_zfs, argv[1]) != 0) {
1934 		zfs_close(zhp);
1935 		return (1);
1936 	}
1937 
1938 	ret = (zfs_rename(zhp, argv[1], recurse) != 0);
1939 
1940 	zfs_close(zhp);
1941 	return (ret);
1942 }
1943 
1944 /*
1945  * zfs promote <fs>
1946  *
1947  * Promotes the given clone fs to be the parent
1948  */
1949 /* ARGSUSED */
1950 static int
1951 zfs_do_promote(int argc, char **argv)
1952 {
1953 	zfs_handle_t *zhp;
1954 	int ret;
1955 
1956 	/* check options */
1957 	if (argc > 1 && argv[1][0] == '-') {
1958 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1959 		    argv[1][1]);
1960 		usage(B_FALSE);
1961 	}
1962 
1963 	/* check number of arguments */
1964 	if (argc < 2) {
1965 		(void) fprintf(stderr, gettext("missing clone filesystem"
1966 		    " argument\n"));
1967 		usage(B_FALSE);
1968 	}
1969 	if (argc > 2) {
1970 		(void) fprintf(stderr, gettext("too many arguments\n"));
1971 		usage(B_FALSE);
1972 	}
1973 
1974 	zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1975 	if (zhp == NULL)
1976 		return (1);
1977 
1978 	ret = (zfs_promote(zhp) != 0);
1979 
1980 
1981 	zfs_close(zhp);
1982 	return (ret);
1983 }
1984 
1985 /*
1986  * zfs rollback [-rRf] <snapshot>
1987  *
1988  * 	-r	Delete any intervening snapshots before doing rollback
1989  * 	-R	Delete any snapshots and their clones
1990  * 	-f	ignored for backwards compatability
1991  *
1992  * Given a filesystem, rollback to a specific snapshot, discarding any changes
1993  * since then and making it the active dataset.  If more recent snapshots exist,
1994  * the command will complain unless the '-r' flag is given.
1995  */
1996 typedef struct rollback_cbdata {
1997 	uint64_t	cb_create;
1998 	boolean_t	cb_first;
1999 	int		cb_doclones;
2000 	char		*cb_target;
2001 	int		cb_error;
2002 	boolean_t	cb_recurse;
2003 	boolean_t	cb_dependent;
2004 } rollback_cbdata_t;
2005 
2006 /*
2007  * Report any snapshots more recent than the one specified.  Used when '-r' is
2008  * not specified.  We reuse this same callback for the snapshot dependents - if
2009  * 'cb_dependent' is set, then this is a dependent and we should report it
2010  * without checking the transaction group.
2011  */
2012 static int
2013 rollback_check(zfs_handle_t *zhp, void *data)
2014 {
2015 	rollback_cbdata_t *cbp = data;
2016 
2017 	if (cbp->cb_doclones) {
2018 		zfs_close(zhp);
2019 		return (0);
2020 	}
2021 
2022 	if (!cbp->cb_dependent) {
2023 		if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
2024 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2025 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
2026 		    cbp->cb_create) {
2027 
2028 			if (cbp->cb_first && !cbp->cb_recurse) {
2029 				(void) fprintf(stderr, gettext("cannot "
2030 				    "rollback to '%s': more recent snapshots "
2031 				    "exist\n"),
2032 				    cbp->cb_target);
2033 				(void) fprintf(stderr, gettext("use '-r' to "
2034 				    "force deletion of the following "
2035 				    "snapshots:\n"));
2036 				cbp->cb_first = 0;
2037 				cbp->cb_error = 1;
2038 			}
2039 
2040 			if (cbp->cb_recurse) {
2041 				cbp->cb_dependent = B_TRUE;
2042 				if (zfs_iter_dependents(zhp, B_TRUE,
2043 				    rollback_check, cbp) != 0) {
2044 					zfs_close(zhp);
2045 					return (-1);
2046 				}
2047 				cbp->cb_dependent = B_FALSE;
2048 			} else {
2049 				(void) fprintf(stderr, "%s\n",
2050 				    zfs_get_name(zhp));
2051 			}
2052 		}
2053 	} else {
2054 		if (cbp->cb_first && cbp->cb_recurse) {
2055 			(void) fprintf(stderr, gettext("cannot rollback to "
2056 			    "'%s': clones of previous snapshots exist\n"),
2057 			    cbp->cb_target);
2058 			(void) fprintf(stderr, gettext("use '-R' to "
2059 			    "force deletion of the following clones and "
2060 			    "dependents:\n"));
2061 			cbp->cb_first = 0;
2062 			cbp->cb_error = 1;
2063 		}
2064 
2065 		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
2066 	}
2067 
2068 	zfs_close(zhp);
2069 	return (0);
2070 }
2071 
2072 static int
2073 zfs_do_rollback(int argc, char **argv)
2074 {
2075 	int ret;
2076 	int c;
2077 	boolean_t force = B_FALSE;
2078 	rollback_cbdata_t cb = { 0 };
2079 	zfs_handle_t *zhp, *snap;
2080 	char parentname[ZFS_MAXNAMELEN];
2081 	char *delim;
2082 
2083 	/* check options */
2084 	while ((c = getopt(argc, argv, "rRf")) != -1) {
2085 		switch (c) {
2086 		case 'r':
2087 			cb.cb_recurse = 1;
2088 			break;
2089 		case 'R':
2090 			cb.cb_recurse = 1;
2091 			cb.cb_doclones = 1;
2092 			break;
2093 		case 'f':
2094 			force = B_TRUE;
2095 			break;
2096 		case '?':
2097 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2098 			    optopt);
2099 			usage(B_FALSE);
2100 		}
2101 	}
2102 
2103 	argc -= optind;
2104 	argv += optind;
2105 
2106 	/* check number of arguments */
2107 	if (argc < 1) {
2108 		(void) fprintf(stderr, gettext("missing dataset argument\n"));
2109 		usage(B_FALSE);
2110 	}
2111 	if (argc > 1) {
2112 		(void) fprintf(stderr, gettext("too many arguments\n"));
2113 		usage(B_FALSE);
2114 	}
2115 
2116 	/* open the snapshot */
2117 	if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
2118 		return (1);
2119 
2120 	/* open the parent dataset */
2121 	(void) strlcpy(parentname, argv[0], sizeof (parentname));
2122 	verify((delim = strrchr(parentname, '@')) != NULL);
2123 	*delim = '\0';
2124 	if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
2125 		zfs_close(snap);
2126 		return (1);
2127 	}
2128 
2129 	/*
2130 	 * Check for more recent snapshots and/or clones based on the presence
2131 	 * of '-r' and '-R'.
2132 	 */
2133 	cb.cb_target = argv[0];
2134 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
2135 	cb.cb_first = B_TRUE;
2136 	cb.cb_error = 0;
2137 	if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
2138 		goto out;
2139 
2140 	if ((ret = cb.cb_error) != 0)
2141 		goto out;
2142 
2143 	/*
2144 	 * Rollback parent to the given snapshot.
2145 	 */
2146 	ret = zfs_rollback(zhp, snap, force);
2147 
2148 out:
2149 	zfs_close(snap);
2150 	zfs_close(zhp);
2151 
2152 	if (ret == 0)
2153 		return (0);
2154 	else
2155 		return (1);
2156 }
2157 
2158 /*
2159  * zfs set property=value { fs | snap | vol } ...
2160  *
2161  * Sets the given property for all datasets specified on the command line.
2162  */
2163 typedef struct set_cbdata {
2164 	char		*cb_propname;
2165 	char		*cb_value;
2166 } set_cbdata_t;
2167 
2168 static int
2169 set_callback(zfs_handle_t *zhp, void *data)
2170 {
2171 	set_cbdata_t *cbp = data;
2172 
2173 	if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
2174 		switch (libzfs_errno(g_zfs)) {
2175 		case EZFS_MOUNTFAILED:
2176 			(void) fprintf(stderr, gettext("property may be set "
2177 			    "but unable to remount filesystem\n"));
2178 			break;
2179 		case EZFS_SHARENFSFAILED:
2180 			(void) fprintf(stderr, gettext("property may be set "
2181 			    "but unable to reshare filesystem\n"));
2182 			break;
2183 		}
2184 		return (1);
2185 	}
2186 	return (0);
2187 }
2188 
2189 static int
2190 zfs_do_set(int argc, char **argv)
2191 {
2192 	set_cbdata_t cb;
2193 	int ret;
2194 
2195 	/* check for options */
2196 	if (argc > 1 && argv[1][0] == '-') {
2197 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2198 		    argv[1][1]);
2199 		usage(B_FALSE);
2200 	}
2201 
2202 	/* check number of arguments */
2203 	if (argc < 2) {
2204 		(void) fprintf(stderr, gettext("missing property=value "
2205 		    "argument\n"));
2206 		usage(B_FALSE);
2207 	}
2208 	if (argc < 3) {
2209 		(void) fprintf(stderr, gettext("missing dataset name\n"));
2210 		usage(B_FALSE);
2211 	}
2212 
2213 	/* validate property=value argument */
2214 	cb.cb_propname = argv[1];
2215 	if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
2216 	    (cb.cb_value[1] == '\0')) {
2217 		(void) fprintf(stderr, gettext("missing value in "
2218 		    "property=value argument\n"));
2219 		usage(B_FALSE);
2220 	}
2221 
2222 	*cb.cb_value = '\0';
2223 	cb.cb_value++;
2224 
2225 	if (*cb.cb_propname == '\0') {
2226 		(void) fprintf(stderr,
2227 		    gettext("missing property in property=value argument\n"));
2228 		usage(B_FALSE);
2229 	}
2230 
2231 	ret = zfs_for_each(argc - 2, argv + 2, B_FALSE,
2232 	    ZFS_TYPE_DATASET, NULL, NULL, set_callback, &cb, B_FALSE);
2233 
2234 	return (ret);
2235 }
2236 
2237 /*
2238  * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
2239  *
2240  * Creates a snapshot with the given name.  While functionally equivalent to
2241  * 'zfs create', it is a separate command to differentiate intent.
2242  */
2243 static int
2244 zfs_do_snapshot(int argc, char **argv)
2245 {
2246 	boolean_t recursive = B_FALSE;
2247 	int ret;
2248 	char c;
2249 	nvlist_t *props;
2250 
2251 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
2252 		(void) fprintf(stderr, gettext("internal error: "
2253 		    "out of memory\n"));
2254 		return (1);
2255 	}
2256 
2257 	/* check options */
2258 	while ((c = getopt(argc, argv, "ro:")) != -1) {
2259 		switch (c) {
2260 		case 'o':
2261 			if (parseprop(props))
2262 				return (1);
2263 			break;
2264 		case 'r':
2265 			recursive = B_TRUE;
2266 			break;
2267 		case '?':
2268 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2269 			    optopt);
2270 			goto usage;
2271 		}
2272 	}
2273 
2274 	argc -= optind;
2275 	argv += optind;
2276 
2277 	/* check number of arguments */
2278 	if (argc < 1) {
2279 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
2280 		goto usage;
2281 	}
2282 	if (argc > 1) {
2283 		(void) fprintf(stderr, gettext("too many arguments\n"));
2284 		goto usage;
2285 	}
2286 
2287 	ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
2288 	nvlist_free(props);
2289 	if (ret && recursive)
2290 		(void) fprintf(stderr, gettext("no snapshots were created\n"));
2291 	return (ret != 0);
2292 
2293 usage:
2294 	nvlist_free(props);
2295 	usage(B_FALSE);
2296 	return (-1);
2297 }
2298 
2299 /*
2300  * zfs send [-v] -R [-i|-I <@snap>] <fs@snap>
2301  * zfs send [-v] [-i|-I <@snap>] <fs@snap>
2302  *
2303  * Send a backup stream to stdout.
2304  */
2305 static int
2306 zfs_do_send(int argc, char **argv)
2307 {
2308 	char *fromname = NULL;
2309 	char *toname = NULL;
2310 	char *cp;
2311 	zfs_handle_t *zhp;
2312 	boolean_t doall = B_FALSE;
2313 	boolean_t replicate = B_FALSE;
2314 	boolean_t fromorigin = B_FALSE;
2315 	boolean_t verbose = B_FALSE;
2316 	int c, err;
2317 
2318 	/* check options */
2319 	while ((c = getopt(argc, argv, ":i:I:Rv")) != -1) {
2320 		switch (c) {
2321 		case 'i':
2322 			if (fromname)
2323 				usage(B_FALSE);
2324 			fromname = optarg;
2325 			break;
2326 		case 'I':
2327 			if (fromname)
2328 				usage(B_FALSE);
2329 			fromname = optarg;
2330 			doall = B_TRUE;
2331 			break;
2332 		case 'R':
2333 			replicate = B_TRUE;
2334 			break;
2335 		case 'v':
2336 			verbose = B_TRUE;
2337 			break;
2338 		case ':':
2339 			(void) fprintf(stderr, gettext("missing argument for "
2340 			    "'%c' option\n"), optopt);
2341 			usage(B_FALSE);
2342 			break;
2343 		case '?':
2344 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2345 			    optopt);
2346 			usage(B_FALSE);
2347 		}
2348 	}
2349 
2350 	argc -= optind;
2351 	argv += optind;
2352 
2353 	/* check number of arguments */
2354 	if (argc < 1) {
2355 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
2356 		usage(B_FALSE);
2357 	}
2358 	if (argc > 1) {
2359 		(void) fprintf(stderr, gettext("too many arguments\n"));
2360 		usage(B_FALSE);
2361 	}
2362 
2363 	if (isatty(STDOUT_FILENO)) {
2364 		(void) fprintf(stderr,
2365 		    gettext("Error: Stream can not be written to a terminal.\n"
2366 		    "You must redirect standard output.\n"));
2367 		return (1);
2368 	}
2369 
2370 	cp = strchr(argv[0], '@');
2371 	if (cp == NULL) {
2372 		(void) fprintf(stderr,
2373 		    gettext("argument must be a snapshot\n"));
2374 		usage(B_FALSE);
2375 	}
2376 	*cp = '\0';
2377 	toname = cp + 1;
2378 	zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2379 	if (zhp == NULL)
2380 		return (1);
2381 
2382 	/*
2383 	 * If they specified the full path to the snapshot, chop off
2384 	 * everything except the short name of the snapshot, but special
2385 	 * case if they specify the origin.
2386 	 */
2387 	if (fromname && (cp = strchr(fromname, '@')) != NULL) {
2388 		char origin[ZFS_MAXNAMELEN];
2389 		zprop_source_t src;
2390 
2391 		(void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
2392 		    origin, sizeof (origin), &src, NULL, 0, B_FALSE);
2393 
2394 		if (strcmp(origin, fromname) == 0) {
2395 			fromname = NULL;
2396 			fromorigin = B_TRUE;
2397 		} else {
2398 			*cp = '\0';
2399 			if (cp != fromname && strcmp(argv[0], fromname)) {
2400 				(void) fprintf(stderr,
2401 				    gettext("incremental source must be "
2402 				    "in same filesystem\n"));
2403 				usage(B_FALSE);
2404 			}
2405 			fromname = cp + 1;
2406 			if (strchr(fromname, '@') || strchr(fromname, '/')) {
2407 				(void) fprintf(stderr,
2408 				    gettext("invalid incremental source\n"));
2409 				usage(B_FALSE);
2410 			}
2411 		}
2412 	}
2413 
2414 	if (replicate && fromname == NULL)
2415 		doall = B_TRUE;
2416 
2417 	err = zfs_send(zhp, fromname, toname, replicate, doall, fromorigin,
2418 	    verbose, STDOUT_FILENO);
2419 	zfs_close(zhp);
2420 
2421 	return (err != 0);
2422 }
2423 
2424 /*
2425  * zfs receive [-dnvF] <fs@snap>
2426  *
2427  * Restore a backup stream from stdin.
2428  */
2429 static int
2430 zfs_do_receive(int argc, char **argv)
2431 {
2432 	int c, err;
2433 	recvflags_t flags;
2434 
2435 	bzero(&flags, sizeof (recvflags_t));
2436 	/* check options */
2437 	while ((c = getopt(argc, argv, ":dnvF")) != -1) {
2438 		switch (c) {
2439 		case 'd':
2440 			flags.isprefix = B_TRUE;
2441 			break;
2442 		case 'n':
2443 			flags.dryrun = B_TRUE;
2444 			break;
2445 		case 'v':
2446 			flags.verbose = B_TRUE;
2447 			break;
2448 		case 'F':
2449 			flags.force = B_TRUE;
2450 			break;
2451 		case ':':
2452 			(void) fprintf(stderr, gettext("missing argument for "
2453 			    "'%c' option\n"), optopt);
2454 			usage(B_FALSE);
2455 			break;
2456 		case '?':
2457 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2458 			    optopt);
2459 			usage(B_FALSE);
2460 		}
2461 	}
2462 
2463 	argc -= optind;
2464 	argv += optind;
2465 
2466 	/* check number of arguments */
2467 	if (argc < 1) {
2468 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
2469 		usage(B_FALSE);
2470 	}
2471 	if (argc > 1) {
2472 		(void) fprintf(stderr, gettext("too many arguments\n"));
2473 		usage(B_FALSE);
2474 	}
2475 
2476 	if (isatty(STDIN_FILENO)) {
2477 		(void) fprintf(stderr,
2478 		    gettext("Error: Backup stream can not be read "
2479 		    "from a terminal.\n"
2480 		    "You must redirect standard input.\n"));
2481 		return (1);
2482 	}
2483 
2484 	err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL);
2485 
2486 	return (err != 0);
2487 }
2488 
2489 typedef struct allow_cb {
2490 	int  a_permcnt;
2491 	size_t a_treeoffset;
2492 } allow_cb_t;
2493 
2494 static void
2495 zfs_print_perms(avl_tree_t *tree)
2496 {
2497 	zfs_perm_node_t *permnode;
2498 
2499 	permnode = avl_first(tree);
2500 	while (permnode != NULL) {
2501 		(void) printf("%s", permnode->z_pname);
2502 		permnode = AVL_NEXT(tree, permnode);
2503 		if (permnode)
2504 			(void) printf(",");
2505 		else
2506 			(void) printf("\n");
2507 	}
2508 }
2509 
2510 /*
2511  * Iterate over user/groups/everyone/... and the call perm_iter
2512  * function to print actual permission when tree has >0 nodes.
2513  */
2514 static void
2515 zfs_iter_perms(avl_tree_t *tree, const char *banner, allow_cb_t *cb)
2516 {
2517 	zfs_allow_node_t *item;
2518 	avl_tree_t *ptree;
2519 
2520 	item = avl_first(tree);
2521 	while (item) {
2522 		ptree = (void *)((char *)item + cb->a_treeoffset);
2523 		if (avl_numnodes(ptree)) {
2524 			if (cb->a_permcnt++ == 0)
2525 				(void) printf("%s\n", banner);
2526 			(void) printf("\t%s", item->z_key);
2527 			/*
2528 			 * Avoid an extra space being printed
2529 			 * for "everyone" which is keyed with a null
2530 			 * string
2531 			 */
2532 			if (item->z_key[0] != '\0')
2533 				(void) printf(" ");
2534 			zfs_print_perms(ptree);
2535 		}
2536 		item = AVL_NEXT(tree, item);
2537 	}
2538 }
2539 
2540 #define	LINES "-------------------------------------------------------------\n"
2541 static int
2542 zfs_print_allows(char *ds)
2543 {
2544 	zfs_allow_t *curperms, *perms;
2545 	zfs_handle_t *zhp;
2546 	allow_cb_t allowcb = { 0 };
2547 	char banner[MAXPATHLEN];
2548 
2549 	if (ds[0] == '-')
2550 		usage(B_FALSE);
2551 
2552 	if (strrchr(ds, '@')) {
2553 		(void) fprintf(stderr, gettext("Snapshots don't have 'allow'"
2554 		    " permissions\n"));
2555 		return (1);
2556 	}
2557 	if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_DATASET)) == NULL)
2558 		return (1);
2559 
2560 	if (zfs_perm_get(zhp, &perms)) {
2561 		(void) fprintf(stderr,
2562 		    gettext("Failed to retrieve 'allows' on %s\n"), ds);
2563 		zfs_close(zhp);
2564 		return (1);
2565 	}
2566 
2567 	zfs_close(zhp);
2568 
2569 	if (perms != NULL)
2570 		(void) printf("%s", LINES);
2571 	for (curperms = perms; curperms; curperms = curperms->z_next) {
2572 
2573 		(void) snprintf(banner, sizeof (banner),
2574 		    "Permission sets on (%s)", curperms->z_setpoint);
2575 		allowcb.a_treeoffset =
2576 		    offsetof(zfs_allow_node_t, z_localdescend);
2577 		allowcb.a_permcnt = 0;
2578 		zfs_iter_perms(&curperms->z_sets, banner, &allowcb);
2579 
2580 		(void) snprintf(banner, sizeof (banner),
2581 		    "Create time permissions on (%s)", curperms->z_setpoint);
2582 		allowcb.a_treeoffset =
2583 		    offsetof(zfs_allow_node_t, z_localdescend);
2584 		allowcb.a_permcnt = 0;
2585 		zfs_iter_perms(&curperms->z_crperms, banner, &allowcb);
2586 
2587 
2588 		(void) snprintf(banner, sizeof (banner),
2589 		    "Local permissions on (%s)", curperms->z_setpoint);
2590 		allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_local);
2591 		allowcb.a_permcnt = 0;
2592 		zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2593 		zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2594 		zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2595 
2596 		(void) snprintf(banner, sizeof (banner),
2597 		    "Descendent permissions on (%s)", curperms->z_setpoint);
2598 		allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_descend);
2599 		allowcb.a_permcnt = 0;
2600 		zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2601 		zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2602 		zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2603 
2604 		(void) snprintf(banner, sizeof (banner),
2605 		    "Local+Descendent permissions on (%s)",
2606 		    curperms->z_setpoint);
2607 		allowcb.a_treeoffset =
2608 		    offsetof(zfs_allow_node_t, z_localdescend);
2609 		allowcb.a_permcnt = 0;
2610 		zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2611 		zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2612 		zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2613 
2614 		(void) printf("%s", LINES);
2615 	}
2616 	zfs_free_allows(perms);
2617 	return (0);
2618 }
2619 
2620 #define	ALLOWOPTIONS "ldcsu:g:e"
2621 #define	UNALLOWOPTIONS "ldcsu:g:er"
2622 
2623 /*
2624  * Validate options, and build necessary datastructure to display/remove/add
2625  * permissions.
2626  * Returns 0 - If permissions should be added/removed
2627  * Returns 1 - If permissions should be displayed.
2628  * Returns -1 - on failure
2629  */
2630 int
2631 parse_allow_args(int *argc, char **argv[], boolean_t unallow,
2632     char **ds, int *recurse, nvlist_t **zperms)
2633 {
2634 	int c;
2635 	char *options = unallow ? UNALLOWOPTIONS : ALLOWOPTIONS;
2636 	zfs_deleg_inherit_t deleg_type = ZFS_DELEG_NONE;
2637 	zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
2638 	char *who = NULL;
2639 	char *perms = NULL;
2640 	zfs_handle_t *zhp;
2641 
2642 	while ((c = getopt(*argc, *argv, options)) != -1) {
2643 		switch (c) {
2644 		case 'l':
2645 			if (who_type == ZFS_DELEG_CREATE ||
2646 			    who_type == ZFS_DELEG_NAMED_SET)
2647 				usage(B_FALSE);
2648 
2649 			deleg_type |= ZFS_DELEG_PERM_LOCAL;
2650 			break;
2651 		case 'd':
2652 			if (who_type == ZFS_DELEG_CREATE ||
2653 			    who_type == ZFS_DELEG_NAMED_SET)
2654 				usage(B_FALSE);
2655 
2656 			deleg_type |= ZFS_DELEG_PERM_DESCENDENT;
2657 			break;
2658 		case 'r':
2659 			*recurse = B_TRUE;
2660 			break;
2661 		case 'c':
2662 			if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2663 				usage(B_FALSE);
2664 			if (deleg_type)
2665 				usage(B_FALSE);
2666 			who_type = ZFS_DELEG_CREATE;
2667 			break;
2668 		case 's':
2669 			if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2670 				usage(B_FALSE);
2671 			if (deleg_type)
2672 				usage(B_FALSE);
2673 			who_type = ZFS_DELEG_NAMED_SET;
2674 			break;
2675 		case 'u':
2676 			if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2677 				usage(B_FALSE);
2678 			who_type = ZFS_DELEG_USER;
2679 			who = optarg;
2680 			break;
2681 		case 'g':
2682 			if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2683 				usage(B_FALSE);
2684 			who_type = ZFS_DELEG_GROUP;
2685 			who = optarg;
2686 			break;
2687 		case 'e':
2688 			if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2689 				usage(B_FALSE);
2690 			who_type = ZFS_DELEG_EVERYONE;
2691 			break;
2692 		default:
2693 			usage(B_FALSE);
2694 			break;
2695 		}
2696 	}
2697 
2698 	if (deleg_type == 0)
2699 		deleg_type = ZFS_DELEG_PERM_LOCALDESCENDENT;
2700 
2701 	*argc -= optind;
2702 	*argv += optind;
2703 
2704 	if (unallow == B_FALSE && *argc == 1) {
2705 		/*
2706 		 * Only print permissions if no options were processed
2707 		 */
2708 		if (optind == 1)
2709 			return (1);
2710 		else
2711 			usage(B_FALSE);
2712 	}
2713 
2714 	/*
2715 	 * initialize variables for zfs_build_perms based on number
2716 	 * of arguments.
2717 	 * 3 arguments ==>	zfs [un]allow joe perm,perm,perm <dataset> or
2718 	 *			zfs [un]allow -s @set1 perm,perm <dataset>
2719 	 * 2 arguments ==>	zfs [un]allow -c perm,perm <dataset> or
2720 	 *			zfs [un]allow -u|-g <name> perm <dataset> or
2721 	 *			zfs [un]allow -e perm,perm <dataset>
2722 	 *			zfs unallow joe <dataset>
2723 	 *			zfs unallow -s @set1 <dataset>
2724 	 * 1 argument  ==>	zfs [un]allow -e <dataset> or
2725 	 *			zfs [un]allow -c <dataset>
2726 	 */
2727 
2728 	switch (*argc) {
2729 	case 3:
2730 		perms = (*argv)[1];
2731 		who = (*argv)[0];
2732 		*ds = (*argv)[2];
2733 
2734 		/*
2735 		 * advance argc/argv for do_allow cases.
2736 		 * for do_allow case make sure who have a know who type
2737 		 * and its not a permission set.
2738 		 */
2739 		if (unallow == B_TRUE) {
2740 			*argc -= 2;
2741 			*argv += 2;
2742 		} else if (who_type != ZFS_DELEG_WHO_UNKNOWN &&
2743 		    who_type != ZFS_DELEG_NAMED_SET)
2744 			usage(B_FALSE);
2745 		break;
2746 
2747 	case 2:
2748 		if (unallow == B_TRUE && (who_type == ZFS_DELEG_EVERYONE ||
2749 		    who_type == ZFS_DELEG_CREATE || who != NULL)) {
2750 			perms = (*argv)[0];
2751 			*ds = (*argv)[1];
2752 		} else {
2753 			if (unallow == B_FALSE &&
2754 			    (who_type == ZFS_DELEG_WHO_UNKNOWN ||
2755 			    who_type == ZFS_DELEG_NAMED_SET))
2756 				usage(B_FALSE);
2757 			else if (who_type == ZFS_DELEG_WHO_UNKNOWN ||
2758 			    who_type == ZFS_DELEG_NAMED_SET)
2759 				who = (*argv)[0];
2760 			else if (who_type != ZFS_DELEG_NAMED_SET)
2761 				perms = (*argv)[0];
2762 			*ds = (*argv)[1];
2763 		}
2764 		if (unallow == B_TRUE) {
2765 			(*argc)--;
2766 			(*argv)++;
2767 		}
2768 		break;
2769 
2770 	case 1:
2771 		if (unallow == B_FALSE)
2772 			usage(B_FALSE);
2773 		if (who == NULL && who_type != ZFS_DELEG_CREATE &&
2774 		    who_type != ZFS_DELEG_EVERYONE)
2775 			usage(B_FALSE);
2776 		*ds = (*argv)[0];
2777 		break;
2778 
2779 	default:
2780 		usage(B_FALSE);
2781 	}
2782 
2783 	if (strrchr(*ds, '@')) {
2784 		(void) fprintf(stderr,
2785 		    gettext("Can't set or remove 'allow' permissions "
2786 		    "on snapshots.\n"));
2787 			return (-1);
2788 	}
2789 
2790 	if ((zhp = zfs_open(g_zfs, *ds, ZFS_TYPE_DATASET)) == NULL)
2791 		return (-1);
2792 
2793 	if ((zfs_build_perms(zhp, who, perms,
2794 	    who_type, deleg_type, zperms)) != 0) {
2795 		zfs_close(zhp);
2796 		return (-1);
2797 	}
2798 	zfs_close(zhp);
2799 	return (0);
2800 }
2801 
2802 static int
2803 zfs_do_allow(int argc, char **argv)
2804 {
2805 	char *ds;
2806 	nvlist_t *zperms = NULL;
2807 	zfs_handle_t *zhp;
2808 	int unused;
2809 	int ret;
2810 
2811 	if ((ret = parse_allow_args(&argc, &argv, B_FALSE, &ds,
2812 	    &unused, &zperms)) == -1)
2813 		return (1);
2814 
2815 	if (ret == 1)
2816 		return (zfs_print_allows(argv[0]));
2817 
2818 	if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_DATASET)) == NULL)
2819 		return (1);
2820 
2821 	if (zfs_perm_set(zhp, zperms)) {
2822 		zfs_close(zhp);
2823 		nvlist_free(zperms);
2824 		return (1);
2825 	}
2826 	nvlist_free(zperms);
2827 	zfs_close(zhp);
2828 
2829 	return (0);
2830 }
2831 
2832 static int
2833 unallow_callback(zfs_handle_t *zhp, void *data)
2834 {
2835 	nvlist_t *nvp = (nvlist_t *)data;
2836 	int error;
2837 
2838 	error = zfs_perm_remove(zhp, nvp);
2839 	if (error) {
2840 		(void) fprintf(stderr, gettext("Failed to remove permissions "
2841 		    "on %s\n"), zfs_get_name(zhp));
2842 	}
2843 	return (error);
2844 }
2845 
2846 static int
2847 zfs_do_unallow(int argc, char **argv)
2848 {
2849 	int recurse = B_FALSE;
2850 	char *ds;
2851 	int error;
2852 	nvlist_t *zperms = NULL;
2853 
2854 	if (parse_allow_args(&argc, &argv, B_TRUE,
2855 	    &ds, &recurse, &zperms) == -1)
2856 		return (1);
2857 
2858 	error = zfs_for_each(argc, argv, recurse,
2859 	    ZFS_TYPE_FILESYSTEM|ZFS_TYPE_VOLUME, NULL,
2860 	    NULL, unallow_callback, (void *)zperms, B_FALSE);
2861 
2862 	if (zperms)
2863 		nvlist_free(zperms);
2864 
2865 	return (error);
2866 }
2867 
2868 typedef struct get_all_cbdata {
2869 	zfs_handle_t	**cb_handles;
2870 	size_t		cb_alloc;
2871 	size_t		cb_used;
2872 	uint_t		cb_types;
2873 	boolean_t	cb_verbose;
2874 } get_all_cbdata_t;
2875 
2876 #define	CHECK_SPINNER 30
2877 #define	SPINNER_TIME 3		/* seconds */
2878 #define	MOUNT_TIME 5		/* seconds */
2879 
2880 static int
2881 get_one_dataset(zfs_handle_t *zhp, void *data)
2882 {
2883 	static char spin[] = { '-', '\\', '|', '/' };
2884 	static int spinval = 0;
2885 	static int spincheck = 0;
2886 	static time_t last_spin_time = (time_t)0;
2887 	get_all_cbdata_t *cbp = data;
2888 	zfs_type_t type = zfs_get_type(zhp);
2889 
2890 	if (cbp->cb_verbose) {
2891 		if (--spincheck < 0) {
2892 			time_t now = time(NULL);
2893 			if (last_spin_time + SPINNER_TIME < now) {
2894 				(void) printf("\b%c", spin[spinval++ % 4]);
2895 				(void) fflush(stdout);
2896 				last_spin_time = now;
2897 			}
2898 			spincheck = CHECK_SPINNER;
2899 		}
2900 	}
2901 
2902 	/*
2903 	 * Interate over any nested datasets.
2904 	 */
2905 	if (type == ZFS_TYPE_FILESYSTEM &&
2906 	    zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
2907 		zfs_close(zhp);
2908 		return (1);
2909 	}
2910 
2911 	/*
2912 	 * Skip any datasets whose type does not match.
2913 	 */
2914 	if ((type & cbp->cb_types) == 0) {
2915 		zfs_close(zhp);
2916 		return (0);
2917 	}
2918 
2919 	if (cbp->cb_alloc == cbp->cb_used) {
2920 		zfs_handle_t **handles;
2921 
2922 		if (cbp->cb_alloc == 0)
2923 			cbp->cb_alloc = 64;
2924 		else
2925 			cbp->cb_alloc *= 2;
2926 
2927 		handles = safe_malloc(cbp->cb_alloc * sizeof (void *));
2928 
2929 		if (cbp->cb_handles) {
2930 			bcopy(cbp->cb_handles, handles,
2931 			    cbp->cb_used * sizeof (void *));
2932 			free(cbp->cb_handles);
2933 		}
2934 
2935 		cbp->cb_handles = handles;
2936 	}
2937 
2938 	cbp->cb_handles[cbp->cb_used++] = zhp;
2939 
2940 	return (0);
2941 }
2942 
2943 static void
2944 get_all_datasets(uint_t types, zfs_handle_t ***dslist, size_t *count,
2945     boolean_t verbose)
2946 {
2947 	get_all_cbdata_t cb = { 0 };
2948 	cb.cb_types = types;
2949 	cb.cb_verbose = verbose;
2950 
2951 	if (verbose) {
2952 		(void) printf("%s: *", gettext("Reading ZFS config"));
2953 		(void) fflush(stdout);
2954 	}
2955 
2956 	(void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
2957 
2958 	*dslist = cb.cb_handles;
2959 	*count = cb.cb_used;
2960 
2961 	if (verbose) {
2962 		(void) printf("\b%s\n", gettext("done."));
2963 	}
2964 }
2965 
2966 static int
2967 dataset_cmp(const void *a, const void *b)
2968 {
2969 	zfs_handle_t **za = (zfs_handle_t **)a;
2970 	zfs_handle_t **zb = (zfs_handle_t **)b;
2971 	char mounta[MAXPATHLEN];
2972 	char mountb[MAXPATHLEN];
2973 	boolean_t gota, gotb;
2974 
2975 	if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
2976 		verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
2977 		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
2978 	if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
2979 		verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
2980 		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
2981 
2982 	if (gota && gotb)
2983 		return (strcmp(mounta, mountb));
2984 
2985 	if (gota)
2986 		return (-1);
2987 	if (gotb)
2988 		return (1);
2989 
2990 	return (strcmp(zfs_get_name(a), zfs_get_name(b)));
2991 }
2992 
2993 /*
2994  * Generic callback for sharing or mounting filesystems.  Because the code is so
2995  * similar, we have a common function with an extra parameter to determine which
2996  * mode we are using.
2997  */
2998 #define	OP_SHARE	0x1
2999 #define	OP_MOUNT	0x2
3000 
3001 /*
3002  * Share or mount a dataset.
3003  */
3004 static int
3005 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
3006     boolean_t explicit, const char *options)
3007 {
3008 	char mountpoint[ZFS_MAXPROPLEN];
3009 	char shareopts[ZFS_MAXPROPLEN];
3010 	char smbshareopts[ZFS_MAXPROPLEN];
3011 	const char *cmdname = op == OP_SHARE ? "share" : "mount";
3012 	struct mnttab mnt;
3013 	uint64_t zoned, canmount;
3014 	zfs_type_t type = zfs_get_type(zhp);
3015 	boolean_t shared_nfs, shared_smb;
3016 
3017 	assert(type & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME));
3018 
3019 	if (type == ZFS_TYPE_FILESYSTEM) {
3020 		/*
3021 		 * Check to make sure we can mount/share this dataset.  If we
3022 		 * are in the global zone and the filesystem is exported to a
3023 		 * local zone, or if we are in a local zone and the
3024 		 * filesystem is not exported, then it is an error.
3025 		 */
3026 		zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3027 
3028 		if (zoned && getzoneid() == GLOBAL_ZONEID) {
3029 			if (!explicit)
3030 				return (0);
3031 
3032 			(void) fprintf(stderr, gettext("cannot %s '%s': "
3033 			    "dataset is exported to a local zone\n"), cmdname,
3034 			    zfs_get_name(zhp));
3035 			return (1);
3036 
3037 		} else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
3038 			if (!explicit)
3039 				return (0);
3040 
3041 			(void) fprintf(stderr, gettext("cannot %s '%s': "
3042 			    "permission denied\n"), cmdname,
3043 			    zfs_get_name(zhp));
3044 			return (1);
3045 		}
3046 
3047 		/*
3048 		 * Ignore any filesystems which don't apply to us. This
3049 		 * includes those with a legacy mountpoint, or those with
3050 		 * legacy share options.
3051 		 */
3052 		verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
3053 		    sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
3054 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
3055 		    sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
3056 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
3057 		    sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
3058 		canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
3059 
3060 		if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
3061 		    strcmp(smbshareopts, "off") == 0) {
3062 			if (!explicit)
3063 				return (0);
3064 
3065 			(void) fprintf(stderr, gettext("cannot share '%s': "
3066 			    "legacy share\n"), zfs_get_name(zhp));
3067 			(void) fprintf(stderr, gettext("use share(1M) to "
3068 			    "share this filesystem\n"));
3069 			return (1);
3070 		}
3071 
3072 		/*
3073 		 * We cannot share or mount legacy filesystems. If the
3074 		 * shareopts is non-legacy but the mountpoint is legacy, we
3075 		 * treat it as a legacy share.
3076 		 */
3077 		if (strcmp(mountpoint, "legacy") == 0) {
3078 			if (!explicit)
3079 				return (0);
3080 
3081 			(void) fprintf(stderr, gettext("cannot %s '%s': "
3082 			    "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
3083 			(void) fprintf(stderr, gettext("use %s(1M) to "
3084 			    "%s this filesystem\n"), cmdname, cmdname);
3085 			return (1);
3086 		}
3087 
3088 		if (strcmp(mountpoint, "none") == 0) {
3089 			if (!explicit)
3090 				return (0);
3091 
3092 			(void) fprintf(stderr, gettext("cannot %s '%s': no "
3093 			    "mountpoint set\n"), cmdname, zfs_get_name(zhp));
3094 			return (1);
3095 		}
3096 
3097 		/*
3098 		 * canmount	explicit	outcome
3099 		 * on		no		pass through
3100 		 * on		yes		pass through
3101 		 * off		no		return 0
3102 		 * off		yes		display error, return 1
3103 		 * noauto	no		return 0
3104 		 * noauto	yes		pass through
3105 		 */
3106 		if (canmount == ZFS_CANMOUNT_OFF) {
3107 			if (!explicit)
3108 				return (0);
3109 
3110 			(void) fprintf(stderr, gettext("cannot %s '%s': "
3111 			    "'canmount' property is set to 'off'\n"), cmdname,
3112 			    zfs_get_name(zhp));
3113 			return (1);
3114 		} else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
3115 			return (0);
3116 		}
3117 
3118 		/*
3119 		 * At this point, we have verified that the mountpoint and/or
3120 		 * shareopts are appropriate for auto management. If the
3121 		 * filesystem is already mounted or shared, return (failing
3122 		 * for explicit requests); otherwise mount or share the
3123 		 * filesystem.
3124 		 */
3125 		switch (op) {
3126 		case OP_SHARE:
3127 
3128 			shared_nfs = zfs_is_shared_nfs(zhp, NULL);
3129 			shared_smb = zfs_is_shared_smb(zhp, NULL);
3130 
3131 			if (shared_nfs && shared_smb ||
3132 			    (shared_nfs && strcmp(shareopts, "on") == 0 &&
3133 			    strcmp(smbshareopts, "off") == 0) ||
3134 			    (shared_smb && strcmp(smbshareopts, "on") == 0 &&
3135 			    strcmp(shareopts, "off") == 0)) {
3136 				if (!explicit)
3137 					return (0);
3138 
3139 				(void) fprintf(stderr, gettext("cannot share "
3140 				    "'%s': filesystem already shared\n"),
3141 				    zfs_get_name(zhp));
3142 				return (1);
3143 			}
3144 
3145 			if (!zfs_is_mounted(zhp, NULL) &&
3146 			    zfs_mount(zhp, NULL, 0) != 0)
3147 				return (1);
3148 
3149 			if (protocol == NULL) {
3150 				if (zfs_shareall(zhp) != 0)
3151 					return (1);
3152 			} else if (strcmp(protocol, "nfs") == 0) {
3153 				if (zfs_share_nfs(zhp))
3154 					return (1);
3155 			} else if (strcmp(protocol, "smb") == 0) {
3156 				if (zfs_share_smb(zhp))
3157 					return (1);
3158 			} else {
3159 				(void) fprintf(stderr, gettext("cannot share "
3160 				    "'%s': invalid share type '%s' "
3161 				    "specified\n"),
3162 				    zfs_get_name(zhp), protocol);
3163 				return (1);
3164 			}
3165 
3166 			break;
3167 
3168 		case OP_MOUNT:
3169 			if (options == NULL)
3170 				mnt.mnt_mntopts = "";
3171 			else
3172 				mnt.mnt_mntopts = (char *)options;
3173 
3174 			if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
3175 			    zfs_is_mounted(zhp, NULL)) {
3176 				if (!explicit)
3177 					return (0);
3178 
3179 				(void) fprintf(stderr, gettext("cannot mount "
3180 				    "'%s': filesystem already mounted\n"),
3181 				    zfs_get_name(zhp));
3182 				return (1);
3183 			}
3184 
3185 			if (zfs_mount(zhp, options, flags) != 0)
3186 				return (1);
3187 			break;
3188 		}
3189 	} else {
3190 		assert(op == OP_SHARE);
3191 
3192 		/*
3193 		 * Ignore any volumes that aren't shared.
3194 		 */
3195 		verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
3196 		    sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
3197 
3198 		if (strcmp(shareopts, "off") == 0) {
3199 			if (!explicit)
3200 				return (0);
3201 
3202 			(void) fprintf(stderr, gettext("cannot share '%s': "
3203 			    "'shareiscsi' property not set\n"),
3204 			    zfs_get_name(zhp));
3205 			(void) fprintf(stderr, gettext("set 'shareiscsi' "
3206 			    "property or use iscsitadm(1M) to share this "
3207 			    "volume\n"));
3208 			return (1);
3209 		}
3210 
3211 		if (zfs_is_shared_iscsi(zhp)) {
3212 			if (!explicit)
3213 				return (0);
3214 
3215 			(void) fprintf(stderr, gettext("cannot share "
3216 			    "'%s': volume already shared\n"),
3217 			    zfs_get_name(zhp));
3218 			return (1);
3219 		}
3220 
3221 		if (zfs_share_iscsi(zhp) != 0)
3222 			return (1);
3223 	}
3224 
3225 	return (0);
3226 }
3227 
3228 /*
3229  * Reports progress in the form "(current/total)".  Not thread-safe.
3230  */
3231 static void
3232 report_mount_progress(int current, int total)
3233 {
3234 	static int len;
3235 	static char *reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
3236 	    "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
3237 	static time_t last_progress_time;
3238 	time_t now = time(NULL);
3239 
3240 	/* report 1..n instead of 0..n-1 */
3241 	++current;
3242 
3243 	/* display header if we're here for the first time */
3244 	if (current == 1) {
3245 		(void) printf(gettext("Mounting ZFS filesystems: "));
3246 		len = 0;
3247 	} else if (current != total && last_progress_time + MOUNT_TIME >= now) {
3248 		/* too soon to report again */
3249 		return;
3250 	}
3251 
3252 	last_progress_time = now;
3253 
3254 	/* back up to prepare for overwriting */
3255 	if (len)
3256 		(void) printf("%*.*s", len, len, reverse);
3257 
3258 	/* We put a newline at the end if this is the last one.  */
3259 	len = printf("(%d/%d)%s", current, total, current == total ? "\n" : "");
3260 	(void) fflush(stdout);
3261 }
3262 
3263 static void
3264 append_options(char *mntopts, char *newopts)
3265 {
3266 	int len = strlen(mntopts);
3267 
3268 	/* original length plus new string to append plus 1 for the comma */
3269 	if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
3270 		(void) fprintf(stderr, gettext("the opts argument for "
3271 		    "'%c' option is too long (more than %d chars)\n"),
3272 		    "-o", MNT_LINE_MAX);
3273 		usage(B_FALSE);
3274 	}
3275 
3276 	if (*mntopts)
3277 		mntopts[len++] = ',';
3278 
3279 	(void) strcpy(&mntopts[len], newopts);
3280 }
3281 
3282 static int
3283 share_mount(int op, int argc, char **argv)
3284 {
3285 	int do_all = 0;
3286 	boolean_t verbose = B_FALSE;
3287 	int c, ret = 0;
3288 	char *options = NULL;
3289 	int types, flags = 0;
3290 
3291 	/* check options */
3292 	while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
3293 	    != -1) {
3294 		switch (c) {
3295 		case 'a':
3296 			do_all = 1;
3297 			break;
3298 		case 'v':
3299 			verbose = B_TRUE;
3300 			break;
3301 		case 'o':
3302 			if (*optarg == '\0') {
3303 				(void) fprintf(stderr, gettext("empty mount "
3304 				    "options (-o) specified\n"));
3305 				usage(B_FALSE);
3306 			}
3307 
3308 			if (options == NULL)
3309 				options = safe_malloc(MNT_LINE_MAX + 1);
3310 
3311 			/* option validation is done later */
3312 			append_options(options, optarg);
3313 			break;
3314 
3315 		case 'O':
3316 			flags |= MS_OVERLAY;
3317 			break;
3318 		case ':':
3319 			(void) fprintf(stderr, gettext("missing argument for "
3320 			    "'%c' option\n"), optopt);
3321 			usage(B_FALSE);
3322 			break;
3323 		case '?':
3324 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3325 			    optopt);
3326 			usage(B_FALSE);
3327 		}
3328 	}
3329 
3330 	argc -= optind;
3331 	argv += optind;
3332 
3333 	/* check number of arguments */
3334 	if (do_all) {
3335 		zfs_handle_t **dslist = NULL;
3336 		size_t i, count = 0;
3337 		char *protocol = NULL;
3338 
3339 		if (op == OP_MOUNT) {
3340 			types = ZFS_TYPE_FILESYSTEM;
3341 		} else if (argc > 0) {
3342 			if (strcmp(argv[0], "nfs") == 0 ||
3343 			    strcmp(argv[0], "smb") == 0) {
3344 				types = ZFS_TYPE_FILESYSTEM;
3345 			} else if (strcmp(argv[0], "iscsi") == 0) {
3346 				types = ZFS_TYPE_VOLUME;
3347 			} else {
3348 				(void) fprintf(stderr, gettext("share type "
3349 				    "must be 'nfs', 'smb' or 'iscsi'\n"));
3350 				usage(B_FALSE);
3351 			}
3352 			protocol = argv[0];
3353 			argc--;
3354 			argv++;
3355 		} else {
3356 			types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3357 		}
3358 
3359 		if (argc != 0) {
3360 			(void) fprintf(stderr, gettext("too many arguments\n"));
3361 			usage(B_FALSE);
3362 		}
3363 
3364 		get_all_datasets(types, &dslist, &count, verbose);
3365 
3366 		if (count == 0)
3367 			return (0);
3368 
3369 		qsort(dslist, count, sizeof (void *), dataset_cmp);
3370 
3371 		for (i = 0; i < count; i++) {
3372 			if (verbose)
3373 				report_mount_progress(i, count);
3374 
3375 			if (share_mount_one(dslist[i], op, flags, protocol,
3376 			    B_FALSE, options) != 0)
3377 				ret = 1;
3378 			zfs_close(dslist[i]);
3379 		}
3380 
3381 		free(dslist);
3382 	} else if (argc == 0) {
3383 		struct mnttab entry;
3384 
3385 		if ((op == OP_SHARE) || (options != NULL)) {
3386 			(void) fprintf(stderr, gettext("missing filesystem "
3387 			    "argument (specify -a for all)\n"));
3388 			usage(B_FALSE);
3389 		}
3390 
3391 		/*
3392 		 * When mount is given no arguments, go through /etc/mnttab and
3393 		 * display any active ZFS mounts.  We hide any snapshots, since
3394 		 * they are controlled automatically.
3395 		 */
3396 		rewind(mnttab_file);
3397 		while (getmntent(mnttab_file, &entry) == 0) {
3398 			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
3399 			    strchr(entry.mnt_special, '@') != NULL)
3400 				continue;
3401 
3402 			(void) printf("%-30s  %s\n", entry.mnt_special,
3403 			    entry.mnt_mountp);
3404 		}
3405 
3406 	} else {
3407 		zfs_handle_t *zhp;
3408 
3409 		types = ZFS_TYPE_FILESYSTEM;
3410 		if (op == OP_SHARE)
3411 			types |= ZFS_TYPE_VOLUME;
3412 
3413 		if (argc > 1) {
3414 			(void) fprintf(stderr,
3415 			    gettext("too many arguments\n"));
3416 			usage(B_FALSE);
3417 		}
3418 
3419 		if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) {
3420 			ret = 1;
3421 		} else {
3422 			ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
3423 			    options);
3424 			zfs_close(zhp);
3425 		}
3426 	}
3427 
3428 	return (ret);
3429 }
3430 
3431 /*
3432  * zfs mount -a [nfs | iscsi]
3433  * zfs mount filesystem
3434  *
3435  * Mount all filesystems, or mount the given filesystem.
3436  */
3437 static int
3438 zfs_do_mount(int argc, char **argv)
3439 {
3440 	return (share_mount(OP_MOUNT, argc, argv));
3441 }
3442 
3443 /*
3444  * zfs share -a [nfs | iscsi | smb]
3445  * zfs share filesystem
3446  *
3447  * Share all filesystems, or share the given filesystem.
3448  */
3449 static int
3450 zfs_do_share(int argc, char **argv)
3451 {
3452 	return (share_mount(OP_SHARE, argc, argv));
3453 }
3454 
3455 typedef struct unshare_unmount_node {
3456 	zfs_handle_t	*un_zhp;
3457 	char		*un_mountp;
3458 	uu_avl_node_t	un_avlnode;
3459 } unshare_unmount_node_t;
3460 
3461 /* ARGSUSED */
3462 static int
3463 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
3464 {
3465 	const unshare_unmount_node_t *l = larg;
3466 	const unshare_unmount_node_t *r = rarg;
3467 
3468 	return (strcmp(l->un_mountp, r->un_mountp));
3469 }
3470 
3471 /*
3472  * Convenience routine used by zfs_do_umount() and manual_unmount().  Given an
3473  * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
3474  * and unmount it appropriately.
3475  */
3476 static int
3477 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
3478 {
3479 	zfs_handle_t *zhp;
3480 	int ret;
3481 	struct stat64 statbuf;
3482 	struct extmnttab entry;
3483 	const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
3484 	ino_t path_inode;
3485 
3486 	/*
3487 	 * Search for the path in /etc/mnttab.  Rather than looking for the
3488 	 * specific path, which can be fooled by non-standard paths (i.e. ".."
3489 	 * or "//"), we stat() the path and search for the corresponding
3490 	 * (major,minor) device pair.
3491 	 */
3492 	if (stat64(path, &statbuf) != 0) {
3493 		(void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3494 		    cmdname, path, strerror(errno));
3495 		return (1);
3496 	}
3497 	path_inode = statbuf.st_ino;
3498 
3499 	/*
3500 	 * Search for the given (major,minor) pair in the mount table.
3501 	 */
3502 	rewind(mnttab_file);
3503 	while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
3504 		if (entry.mnt_major == major(statbuf.st_dev) &&
3505 		    entry.mnt_minor == minor(statbuf.st_dev))
3506 			break;
3507 	}
3508 	if (ret != 0) {
3509 		if (op == OP_SHARE) {
3510 			(void) fprintf(stderr, gettext("cannot %s '%s': not "
3511 			    "currently mounted\n"), cmdname, path);
3512 			return (1);
3513 		}
3514 		(void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
3515 		    path);
3516 		if ((ret = umount2(path, flags)) != 0)
3517 			(void) fprintf(stderr, gettext("%s: %s\n"), path,
3518 			    strerror(errno));
3519 		return (ret != 0);
3520 	}
3521 
3522 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
3523 		(void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
3524 		    "filesystem\n"), cmdname, path);
3525 		return (1);
3526 	}
3527 
3528 	if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3529 	    ZFS_TYPE_FILESYSTEM)) == NULL)
3530 		return (1);
3531 
3532 
3533 	ret = 1;
3534 	if (op == OP_SHARE) {
3535 		char nfs_mnt_prop[ZFS_MAXPROPLEN];
3536 		char smbshare_prop[ZFS_MAXPROPLEN];
3537 
3538 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
3539 		    sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
3540 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
3541 		    sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
3542 
3543 		if (strcmp(nfs_mnt_prop, "off") == 0 &&
3544 		    strcmp(smbshare_prop, "off") == 0) {
3545 			(void) fprintf(stderr, gettext("cannot unshare "
3546 			    "'%s': legacy share\n"), path);
3547 			(void) fprintf(stderr, gettext("use "
3548 			    "unshare(1M) to unshare this filesystem\n"));
3549 		} else if (!zfs_is_shared(zhp)) {
3550 			(void) fprintf(stderr, gettext("cannot unshare '%s': "
3551 			    "not currently shared\n"), path);
3552 		} else {
3553 			ret = zfs_unshareall_bypath(zhp, path);
3554 		}
3555 	} else {
3556 		char mtpt_prop[ZFS_MAXPROPLEN];
3557 
3558 		verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
3559 		    sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
3560 
3561 		if (stat64(entry.mnt_mountp, &statbuf) != 0) {
3562 			(void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3563 			    cmdname, path, strerror(errno));
3564 		} else if (statbuf.st_ino != path_inode) {
3565 			(void) fprintf(stderr, gettext("cannot "
3566 			    "unmount '%s': not a mountpoint\n"), path);
3567 		} else if (is_manual) {
3568 			ret = zfs_unmount(zhp, NULL, flags);
3569 		} else if (strcmp(mtpt_prop, "legacy") == 0) {
3570 			(void) fprintf(stderr, gettext("cannot unmount "
3571 			    "'%s': legacy mountpoint\n"),
3572 			    zfs_get_name(zhp));
3573 			(void) fprintf(stderr, gettext("use umount(1M) "
3574 			    "to unmount this filesystem\n"));
3575 		} else {
3576 			ret = zfs_unmountall(zhp, flags);
3577 		}
3578 	}
3579 
3580 	zfs_close(zhp);
3581 
3582 	return (ret != 0);
3583 }
3584 
3585 /*
3586  * Generic callback for unsharing or unmounting a filesystem.
3587  */
3588 static int
3589 unshare_unmount(int op, int argc, char **argv)
3590 {
3591 	int do_all = 0;
3592 	int flags = 0;
3593 	int ret = 0;
3594 	int types, c;
3595 	zfs_handle_t *zhp;
3596 	char nfsiscsi_mnt_prop[ZFS_MAXPROPLEN];
3597 	char sharesmb[ZFS_MAXPROPLEN];
3598 
3599 	/* check options */
3600 	while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
3601 		switch (c) {
3602 		case 'a':
3603 			do_all = 1;
3604 			break;
3605 		case 'f':
3606 			flags = MS_FORCE;
3607 			break;
3608 		case '?':
3609 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3610 			    optopt);
3611 			usage(B_FALSE);
3612 		}
3613 	}
3614 
3615 	argc -= optind;
3616 	argv += optind;
3617 
3618 	if (do_all) {
3619 		/*
3620 		 * We could make use of zfs_for_each() to walk all datasets in
3621 		 * the system, but this would be very inefficient, especially
3622 		 * since we would have to linearly search /etc/mnttab for each
3623 		 * one.  Instead, do one pass through /etc/mnttab looking for
3624 		 * zfs entries and call zfs_unmount() for each one.
3625 		 *
3626 		 * Things get a little tricky if the administrator has created
3627 		 * mountpoints beneath other ZFS filesystems.  In this case, we
3628 		 * have to unmount the deepest filesystems first.  To accomplish
3629 		 * this, we place all the mountpoints in an AVL tree sorted by
3630 		 * the special type (dataset name), and walk the result in
3631 		 * reverse to make sure to get any snapshots first.
3632 		 */
3633 		struct mnttab entry;
3634 		uu_avl_pool_t *pool;
3635 		uu_avl_t *tree;
3636 		unshare_unmount_node_t *node;
3637 		uu_avl_index_t idx;
3638 		uu_avl_walk_t *walk;
3639 
3640 		if (argc != 0) {
3641 			(void) fprintf(stderr, gettext("too many arguments\n"));
3642 			usage(B_FALSE);
3643 		}
3644 
3645 		if ((pool = uu_avl_pool_create("unmount_pool",
3646 		    sizeof (unshare_unmount_node_t),
3647 		    offsetof(unshare_unmount_node_t, un_avlnode),
3648 		    unshare_unmount_compare,
3649 		    UU_DEFAULT)) == NULL) {
3650 			(void) fprintf(stderr, gettext("internal error: "
3651 			    "out of memory\n"));
3652 			exit(1);
3653 		}
3654 
3655 		if ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL) {
3656 			(void) fprintf(stderr, gettext("internal error: "
3657 			    "out of memory\n"));
3658 			exit(1);
3659 		}
3660 
3661 		rewind(mnttab_file);
3662 		while (getmntent(mnttab_file, &entry) == 0) {
3663 
3664 			/* ignore non-ZFS entries */
3665 			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
3666 				continue;
3667 
3668 			/* ignore snapshots */
3669 			if (strchr(entry.mnt_special, '@') != NULL)
3670 				continue;
3671 
3672 			if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3673 			    ZFS_TYPE_FILESYSTEM)) == NULL) {
3674 				ret = 1;
3675 				continue;
3676 			}
3677 
3678 			switch (op) {
3679 			case OP_SHARE:
3680 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3681 				    nfsiscsi_mnt_prop,
3682 				    sizeof (nfsiscsi_mnt_prop),
3683 				    NULL, NULL, 0, B_FALSE) == 0);
3684 				if (strcmp(nfsiscsi_mnt_prop, "off") != 0)
3685 					break;
3686 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3687 				    nfsiscsi_mnt_prop,
3688 				    sizeof (nfsiscsi_mnt_prop),
3689 				    NULL, NULL, 0, B_FALSE) == 0);
3690 				if (strcmp(nfsiscsi_mnt_prop, "off") == 0)
3691 					continue;
3692 				break;
3693 			case OP_MOUNT:
3694 				/* Ignore legacy mounts */
3695 				verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
3696 				    nfsiscsi_mnt_prop,
3697 				    sizeof (nfsiscsi_mnt_prop),
3698 				    NULL, NULL, 0, B_FALSE) == 0);
3699 				if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0)
3700 					continue;
3701 				/* Ignore canmount=noauto mounts */
3702 				if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
3703 				    ZFS_CANMOUNT_NOAUTO)
3704 					continue;
3705 			default:
3706 				break;
3707 			}
3708 
3709 			node = safe_malloc(sizeof (unshare_unmount_node_t));
3710 			node->un_zhp = zhp;
3711 
3712 			if ((node->un_mountp = strdup(entry.mnt_mountp)) ==
3713 			    NULL) {
3714 				(void) fprintf(stderr, gettext("internal error:"
3715 				    " out of memory\n"));
3716 				exit(1);
3717 			}
3718 
3719 			uu_avl_node_init(node, &node->un_avlnode, pool);
3720 
3721 			if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
3722 				uu_avl_insert(tree, node, idx);
3723 			} else {
3724 				zfs_close(node->un_zhp);
3725 				free(node->un_mountp);
3726 				free(node);
3727 			}
3728 		}
3729 
3730 		/*
3731 		 * Walk the AVL tree in reverse, unmounting each filesystem and
3732 		 * removing it from the AVL tree in the process.
3733 		 */
3734 		if ((walk = uu_avl_walk_start(tree,
3735 		    UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) {
3736 			(void) fprintf(stderr,
3737 			    gettext("internal error: out of memory"));
3738 			exit(1);
3739 		}
3740 
3741 		while ((node = uu_avl_walk_next(walk)) != NULL) {
3742 			uu_avl_remove(tree, node);
3743 
3744 			switch (op) {
3745 			case OP_SHARE:
3746 				if (zfs_unshareall_bypath(node->un_zhp,
3747 				    node->un_mountp) != 0)
3748 					ret = 1;
3749 				break;
3750 
3751 			case OP_MOUNT:
3752 				if (zfs_unmount(node->un_zhp,
3753 				    node->un_mountp, flags) != 0)
3754 					ret = 1;
3755 				break;
3756 			}
3757 
3758 			zfs_close(node->un_zhp);
3759 			free(node->un_mountp);
3760 			free(node);
3761 		}
3762 
3763 		uu_avl_walk_end(walk);
3764 		uu_avl_destroy(tree);
3765 		uu_avl_pool_destroy(pool);
3766 
3767 		if (op == OP_SHARE) {
3768 			/*
3769 			 * Finally, unshare any volumes shared via iSCSI.
3770 			 */
3771 			zfs_handle_t **dslist = NULL;
3772 			size_t i, count = 0;
3773 
3774 			get_all_datasets(ZFS_TYPE_VOLUME, &dslist, &count,
3775 			    B_FALSE);
3776 
3777 			if (count != 0) {
3778 				qsort(dslist, count, sizeof (void *),
3779 				    dataset_cmp);
3780 
3781 				for (i = 0; i < count; i++) {
3782 					if (zfs_unshare_iscsi(dslist[i]) != 0)
3783 						ret = 1;
3784 					zfs_close(dslist[i]);
3785 				}
3786 
3787 				free(dslist);
3788 			}
3789 		}
3790 	} else {
3791 		if (argc != 1) {
3792 			if (argc == 0)
3793 				(void) fprintf(stderr,
3794 				    gettext("missing filesystem argument\n"));
3795 			else
3796 				(void) fprintf(stderr,
3797 				    gettext("too many arguments\n"));
3798 			usage(B_FALSE);
3799 		}
3800 
3801 		/*
3802 		 * We have an argument, but it may be a full path or a ZFS
3803 		 * filesystem.  Pass full paths off to unmount_path() (shared by
3804 		 * manual_unmount), otherwise open the filesystem and pass to
3805 		 * zfs_unmount().
3806 		 */
3807 		if (argv[0][0] == '/')
3808 			return (unshare_unmount_path(op, argv[0],
3809 			    flags, B_FALSE));
3810 
3811 		types = ZFS_TYPE_FILESYSTEM;
3812 		if (op == OP_SHARE)
3813 			types |= ZFS_TYPE_VOLUME;
3814 
3815 		if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3816 			return (1);
3817 
3818 		if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
3819 			verify(zfs_prop_get(zhp, op == OP_SHARE ?
3820 			    ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
3821 			    nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop), NULL,
3822 			    NULL, 0, B_FALSE) == 0);
3823 
3824 			switch (op) {
3825 			case OP_SHARE:
3826 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3827 				    nfsiscsi_mnt_prop,
3828 				    sizeof (nfsiscsi_mnt_prop),
3829 				    NULL, NULL, 0, B_FALSE) == 0);
3830 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3831 				    sharesmb, sizeof (sharesmb), NULL, NULL,
3832 				    0, B_FALSE) == 0);
3833 
3834 				if (strcmp(nfsiscsi_mnt_prop, "off") == 0 &&
3835 				    strcmp(sharesmb, "off") == 0) {
3836 					(void) fprintf(stderr, gettext("cannot "
3837 					    "unshare '%s': legacy share\n"),
3838 					    zfs_get_name(zhp));
3839 					(void) fprintf(stderr, gettext("use "
3840 					    "unshare(1M) to unshare this "
3841 					    "filesystem\n"));
3842 					ret = 1;
3843 				} else if (!zfs_is_shared(zhp)) {
3844 					(void) fprintf(stderr, gettext("cannot "
3845 					    "unshare '%s': not currently "
3846 					    "shared\n"), zfs_get_name(zhp));
3847 					ret = 1;
3848 				} else if (zfs_unshareall(zhp) != 0) {
3849 					ret = 1;
3850 				}
3851 				break;
3852 
3853 			case OP_MOUNT:
3854 				if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0) {
3855 					(void) fprintf(stderr, gettext("cannot "
3856 					    "unmount '%s': legacy "
3857 					    "mountpoint\n"), zfs_get_name(zhp));
3858 					(void) fprintf(stderr, gettext("use "
3859 					    "umount(1M) to unmount this "
3860 					    "filesystem\n"));
3861 					ret = 1;
3862 				} else if (!zfs_is_mounted(zhp, NULL)) {
3863 					(void) fprintf(stderr, gettext("cannot "
3864 					    "unmount '%s': not currently "
3865 					    "mounted\n"),
3866 					    zfs_get_name(zhp));
3867 					ret = 1;
3868 				} else if (zfs_unmountall(zhp, flags) != 0) {
3869 					ret = 1;
3870 				}
3871 				break;
3872 			}
3873 		} else {
3874 			assert(op == OP_SHARE);
3875 
3876 			verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI,
3877 			    nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop),
3878 			    NULL, NULL, 0, B_FALSE) == 0);
3879 
3880 			if (strcmp(nfsiscsi_mnt_prop, "off") == 0) {
3881 				(void) fprintf(stderr, gettext("cannot unshare "
3882 				    "'%s': 'shareiscsi' property not set\n"),
3883 				    zfs_get_name(zhp));
3884 				(void) fprintf(stderr, gettext("set "
3885 				    "'shareiscsi' property or use "
3886 				    "iscsitadm(1M) to share this volume\n"));
3887 				ret = 1;
3888 			} else if (!zfs_is_shared_iscsi(zhp)) {
3889 				(void) fprintf(stderr, gettext("cannot "
3890 				    "unshare '%s': not currently shared\n"),
3891 				    zfs_get_name(zhp));
3892 				ret = 1;
3893 			} else if (zfs_unshare_iscsi(zhp) != 0) {
3894 				ret = 1;
3895 			}
3896 		}
3897 
3898 		zfs_close(zhp);
3899 	}
3900 
3901 	return (ret);
3902 }
3903 
3904 /*
3905  * zfs unmount -a
3906  * zfs unmount filesystem
3907  *
3908  * Unmount all filesystems, or a specific ZFS filesystem.
3909  */
3910 static int
3911 zfs_do_unmount(int argc, char **argv)
3912 {
3913 	return (unshare_unmount(OP_MOUNT, argc, argv));
3914 }
3915 
3916 /*
3917  * zfs unshare -a
3918  * zfs unshare filesystem
3919  *
3920  * Unshare all filesystems, or a specific ZFS filesystem.
3921  */
3922 static int
3923 zfs_do_unshare(int argc, char **argv)
3924 {
3925 	return (unshare_unmount(OP_SHARE, argc, argv));
3926 }
3927 
3928 /*
3929  * Called when invoked as /etc/fs/zfs/mount.  Do the mount if the mountpoint is
3930  * 'legacy'.  Otherwise, complain that use should be using 'zfs mount'.
3931  */
3932 static int
3933 manual_mount(int argc, char **argv)
3934 {
3935 	zfs_handle_t *zhp;
3936 	char mountpoint[ZFS_MAXPROPLEN];
3937 	char mntopts[MNT_LINE_MAX] = { '\0' };
3938 	int ret;
3939 	int c;
3940 	int flags = 0;
3941 	char *dataset, *path;
3942 
3943 	/* check options */
3944 	while ((c = getopt(argc, argv, ":mo:O")) != -1) {
3945 		switch (c) {
3946 		case 'o':
3947 			(void) strlcpy(mntopts, optarg, sizeof (mntopts));
3948 			break;
3949 		case 'O':
3950 			flags |= MS_OVERLAY;
3951 			break;
3952 		case 'm':
3953 			flags |= MS_NOMNTTAB;
3954 			break;
3955 		case ':':
3956 			(void) fprintf(stderr, gettext("missing argument for "
3957 			    "'%c' option\n"), optopt);
3958 			usage(B_FALSE);
3959 			break;
3960 		case '?':
3961 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3962 			    optopt);
3963 			(void) fprintf(stderr, gettext("usage: mount [-o opts] "
3964 			    "<path>\n"));
3965 			return (2);
3966 		}
3967 	}
3968 
3969 	argc -= optind;
3970 	argv += optind;
3971 
3972 	/* check that we only have two arguments */
3973 	if (argc != 2) {
3974 		if (argc == 0)
3975 			(void) fprintf(stderr, gettext("missing dataset "
3976 			    "argument\n"));
3977 		else if (argc == 1)
3978 			(void) fprintf(stderr,
3979 			    gettext("missing mountpoint argument\n"));
3980 		else
3981 			(void) fprintf(stderr, gettext("too many arguments\n"));
3982 		(void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
3983 		return (2);
3984 	}
3985 
3986 	dataset = argv[0];
3987 	path = argv[1];
3988 
3989 	/* try to open the dataset */
3990 	if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
3991 		return (1);
3992 
3993 	(void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
3994 	    sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
3995 
3996 	/* check for legacy mountpoint and complain appropriately */
3997 	ret = 0;
3998 	if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
3999 		if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS,
4000 		    NULL, 0, mntopts, sizeof (mntopts)) != 0) {
4001 			(void) fprintf(stderr, gettext("mount failed: %s\n"),
4002 			    strerror(errno));
4003 			ret = 1;
4004 		}
4005 	} else {
4006 		(void) fprintf(stderr, gettext("filesystem '%s' cannot be "
4007 		    "mounted using 'mount -F zfs'\n"), dataset);
4008 		(void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
4009 		    "instead.\n"), path);
4010 		(void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
4011 		    "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
4012 		(void) fprintf(stderr, gettext("See zfs(1M) for more "
4013 		    "information.\n"));
4014 		ret = 1;
4015 	}
4016 
4017 	return (ret);
4018 }
4019 
4020 /*
4021  * Called when invoked as /etc/fs/zfs/umount.  Unlike a manual mount, we allow
4022  * unmounts of non-legacy filesystems, as this is the dominant administrative
4023  * interface.
4024  */
4025 static int
4026 manual_unmount(int argc, char **argv)
4027 {
4028 	int flags = 0;
4029 	int c;
4030 
4031 	/* check options */
4032 	while ((c = getopt(argc, argv, "f")) != -1) {
4033 		switch (c) {
4034 		case 'f':
4035 			flags = MS_FORCE;
4036 			break;
4037 		case '?':
4038 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4039 			    optopt);
4040 			(void) fprintf(stderr, gettext("usage: unmount [-f] "
4041 			    "<path>\n"));
4042 			return (2);
4043 		}
4044 	}
4045 
4046 	argc -= optind;
4047 	argv += optind;
4048 
4049 	/* check arguments */
4050 	if (argc != 1) {
4051 		if (argc == 0)
4052 			(void) fprintf(stderr, gettext("missing path "
4053 			    "argument\n"));
4054 		else
4055 			(void) fprintf(stderr, gettext("too many arguments\n"));
4056 		(void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
4057 		return (2);
4058 	}
4059 
4060 	return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
4061 }
4062 
4063 static int
4064 volcheck(zpool_handle_t *zhp, void *data)
4065 {
4066 	boolean_t isinit = *((boolean_t *)data);
4067 
4068 	if (isinit)
4069 		return (zpool_create_zvol_links(zhp));
4070 	else
4071 		return (zpool_remove_zvol_links(zhp));
4072 }
4073 
4074 /*
4075  * Iterate over all pools in the system and either create or destroy /dev/zvol
4076  * links, depending on the value of 'isinit'.
4077  */
4078 static int
4079 do_volcheck(boolean_t isinit)
4080 {
4081 	return (zpool_iter(g_zfs, volcheck, &isinit) ? 1 : 0);
4082 }
4083 
4084 static int
4085 find_command_idx(char *command, int *idx)
4086 {
4087 	int i;
4088 
4089 	for (i = 0; i < NCOMMAND; i++) {
4090 		if (command_table[i].name == NULL)
4091 			continue;
4092 
4093 		if (strcmp(command, command_table[i].name) == 0) {
4094 			*idx = i;
4095 			return (0);
4096 		}
4097 	}
4098 	return (1);
4099 }
4100 
4101 int
4102 main(int argc, char **argv)
4103 {
4104 	int ret;
4105 	int i;
4106 	char *progname;
4107 	char *cmdname;
4108 
4109 	(void) setlocale(LC_ALL, "");
4110 	(void) textdomain(TEXT_DOMAIN);
4111 
4112 	opterr = 0;
4113 
4114 	if ((g_zfs = libzfs_init()) == NULL) {
4115 		(void) fprintf(stderr, gettext("internal error: failed to "
4116 		    "initialize ZFS library\n"));
4117 		return (1);
4118 	}
4119 
4120 	zpool_set_history_str("zfs", argc, argv, history_str);
4121 	verify(zpool_stage_history(g_zfs, history_str) == 0);
4122 
4123 	libzfs_print_on_error(g_zfs, B_TRUE);
4124 
4125 	if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
4126 		(void) fprintf(stderr, gettext("internal error: unable to "
4127 		    "open %s\n"), MNTTAB);
4128 		return (1);
4129 	}
4130 
4131 	/*
4132 	 * This command also doubles as the /etc/fs mount and unmount program.
4133 	 * Determine if we should take this behavior based on argv[0].
4134 	 */
4135 	progname = basename(argv[0]);
4136 	if (strcmp(progname, "mount") == 0) {
4137 		ret = manual_mount(argc, argv);
4138 	} else if (strcmp(progname, "umount") == 0) {
4139 		ret = manual_unmount(argc, argv);
4140 	} else {
4141 		/*
4142 		 * Make sure the user has specified some command.
4143 		 */
4144 		if (argc < 2) {
4145 			(void) fprintf(stderr, gettext("missing command\n"));
4146 			usage(B_FALSE);
4147 		}
4148 
4149 		cmdname = argv[1];
4150 
4151 		/*
4152 		 * The 'umount' command is an alias for 'unmount'
4153 		 */
4154 		if (strcmp(cmdname, "umount") == 0)
4155 			cmdname = "unmount";
4156 
4157 		/*
4158 		 * The 'recv' command is an alias for 'receive'
4159 		 */
4160 		if (strcmp(cmdname, "recv") == 0)
4161 			cmdname = "receive";
4162 
4163 		/*
4164 		 * Special case '-?'
4165 		 */
4166 		if (strcmp(cmdname, "-?") == 0)
4167 			usage(B_TRUE);
4168 
4169 		/*
4170 		 * 'volinit' and 'volfini' do not appear in the usage message,
4171 		 * so we have to special case them here.
4172 		 */
4173 		if (strcmp(cmdname, "volinit") == 0)
4174 			return (do_volcheck(B_TRUE));
4175 		else if (strcmp(cmdname, "volfini") == 0)
4176 			return (do_volcheck(B_FALSE));
4177 
4178 		/*
4179 		 * Run the appropriate command.
4180 		 */
4181 		if (find_command_idx(cmdname, &i) == 0) {
4182 			current_command = &command_table[i];
4183 			ret = command_table[i].func(argc - 1, argv + 1);
4184 		} else if (strchr(cmdname, '=') != NULL) {
4185 			verify(find_command_idx("set", &i) == 0);
4186 			current_command = &command_table[i];
4187 			ret = command_table[i].func(argc, argv);
4188 		} else {
4189 			(void) fprintf(stderr, gettext("unrecognized "
4190 			    "command '%s'\n"), cmdname);
4191 			usage(B_FALSE);
4192 		}
4193 	}
4194 
4195 	(void) fclose(mnttab_file);
4196 
4197 	libzfs_fini(g_zfs);
4198 
4199 	/*
4200 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4201 	 * for the purposes of running ::findleaks.
4202 	 */
4203 	if (getenv("ZFS_ABORT") != NULL) {
4204 		(void) printf("dumping core by request\n");
4205 		abort();
4206 	}
4207 
4208 	return (ret);
4209 }
4210