xref: /titanic_41/usr/src/cmd/zfs/zfs_main.c (revision 4ebb14b236958cfe1ef4ff3b7a50216d9e51f997)
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 2006 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 <locale.h>
36 #include <stddef.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <strings.h>
40 #include <unistd.h>
41 #include <fcntl.h>
42 #include <zone.h>
43 #include <sys/mkdev.h>
44 #include <sys/mntent.h>
45 #include <sys/mnttab.h>
46 #include <sys/mount.h>
47 #include <sys/stat.h>
48 
49 #include <libzfs.h>
50 
51 #include "zfs_iter.h"
52 #include "zfs_util.h"
53 
54 libzfs_handle_t *g_zfs;
55 
56 static FILE *mnttab_file;
57 
58 static int zfs_do_clone(int argc, char **argv);
59 static int zfs_do_create(int argc, char **argv);
60 static int zfs_do_destroy(int argc, char **argv);
61 static int zfs_do_get(int argc, char **argv);
62 static int zfs_do_inherit(int argc, char **argv);
63 static int zfs_do_list(int argc, char **argv);
64 static int zfs_do_mount(int argc, char **argv);
65 static int zfs_do_rename(int argc, char **argv);
66 static int zfs_do_rollback(int argc, char **argv);
67 static int zfs_do_set(int argc, char **argv);
68 static int zfs_do_snapshot(int argc, char **argv);
69 static int zfs_do_unmount(int argc, char **argv);
70 static int zfs_do_share(int argc, char **argv);
71 static int zfs_do_unshare(int argc, char **argv);
72 static int zfs_do_send(int argc, char **argv);
73 static int zfs_do_receive(int argc, char **argv);
74 static int zfs_do_promote(int argc, char **argv);
75 
76 /*
77  * These libumem hooks provide a reasonable set of defaults for the allocator's
78  * debugging facilities.
79  */
80 const char *
81 _umem_debug_init(void)
82 {
83 	return ("default,verbose"); /* $UMEM_DEBUG setting */
84 }
85 
86 const char *
87 _umem_logging_init(void)
88 {
89 	return ("fail,contents"); /* $UMEM_LOGGING setting */
90 }
91 
92 typedef enum {
93 	HELP_CLONE,
94 	HELP_CREATE,
95 	HELP_DESTROY,
96 	HELP_GET,
97 	HELP_INHERIT,
98 	HELP_LIST,
99 	HELP_MOUNT,
100 	HELP_PROMOTE,
101 	HELP_RECEIVE,
102 	HELP_RENAME,
103 	HELP_ROLLBACK,
104 	HELP_SEND,
105 	HELP_SET,
106 	HELP_SHARE,
107 	HELP_SNAPSHOT,
108 	HELP_UNMOUNT,
109 	HELP_UNSHARE
110 } zfs_help_t;
111 
112 typedef struct zfs_command {
113 	const char	*name;
114 	int		(*func)(int argc, char **argv);
115 	zfs_help_t	usage;
116 } zfs_command_t;
117 
118 /*
119  * Master command table.  Each ZFS command has a name, associated function, and
120  * usage message.  The usage messages need to be internationalized, so we have
121  * to have a function to return the usage message based on a command index.
122  *
123  * These commands are organized according to how they are displayed in the usage
124  * message.  An empty command (one with a NULL name) indicates an empty line in
125  * the generic usage message.
126  */
127 static zfs_command_t command_table[] = {
128 	{ "create",	zfs_do_create,		HELP_CREATE		},
129 	{ "destroy",	zfs_do_destroy,		HELP_DESTROY		},
130 	{ NULL },
131 	{ "snapshot",	zfs_do_snapshot,	HELP_SNAPSHOT		},
132 	{ "rollback",	zfs_do_rollback,	HELP_ROLLBACK		},
133 	{ "clone",	zfs_do_clone,		HELP_CLONE		},
134 	{ "promote",	zfs_do_promote,		HELP_PROMOTE		},
135 	{ "rename",	zfs_do_rename,		HELP_RENAME		},
136 	{ NULL },
137 	{ "list",	zfs_do_list,		HELP_LIST		},
138 	{ NULL },
139 	{ "set",	zfs_do_set,		HELP_SET		},
140 	{ "get", 	zfs_do_get,		HELP_GET		},
141 	{ "inherit",	zfs_do_inherit,		HELP_INHERIT		},
142 	{ NULL },
143 	{ "mount",	zfs_do_mount,		HELP_MOUNT		},
144 	{ NULL },
145 	{ "unmount",	zfs_do_unmount,		HELP_UNMOUNT		},
146 	{ NULL },
147 	{ "share",	zfs_do_share,		HELP_SHARE		},
148 	{ NULL },
149 	{ "unshare",	zfs_do_unshare,		HELP_UNSHARE		},
150 	{ NULL },
151 	{ "send",	zfs_do_send,		HELP_SEND		},
152 	{ "receive",	zfs_do_receive,		HELP_RECEIVE		},
153 };
154 
155 #define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
156 
157 zfs_command_t *current_command;
158 
159 static const char *
160 get_usage(zfs_help_t idx)
161 {
162 	switch (idx) {
163 	case HELP_CLONE:
164 		return (gettext("\tclone <snapshot> <filesystem|volume>\n"));
165 	case HELP_CREATE:
166 		return (gettext("\tcreate [[-o property=value] ... ] "
167 		    "<filesystem>\n"
168 		    "\tcreate [-s] [-b blocksize] [[-o property=value] ...]\n"
169 		    "\t    -V <size> <volume>\n"));
170 	case HELP_DESTROY:
171 		return (gettext("\tdestroy [-rRf] "
172 		    "<filesystem|volume|snapshot>\n"));
173 	case HELP_GET:
174 		return (gettext("\tget [-rHp] [-o field[,field]...] "
175 		    "[-s source[,source]...]\n"
176 		    "\t    <all | property[,property]...> "
177 		    "[filesystem|volume|snapshot] ...\n"));
178 	case HELP_INHERIT:
179 		return (gettext("\tinherit [-r] <property> "
180 		    "<filesystem|volume> ...\n"));
181 	case HELP_LIST:
182 		return (gettext("\tlist [-rH] [-o property[,property]...] "
183 		    "[-t type[,type]...]\n"
184 		    "\t    [-s property [-s property]...]"
185 		    " [-S property [-S property]...]\n"
186 		    "\t    [filesystem|volume|snapshot] ...\n"));
187 	case HELP_MOUNT:
188 		return (gettext("\tmount\n"
189 		    "\tmount [-o opts] [-O] -a\n"
190 		    "\tmount [-o opts] [-O] <filesystem>\n"));
191 	case HELP_PROMOTE:
192 		return (gettext("\tpromote <clone filesystem>\n"));
193 	case HELP_RECEIVE:
194 		return (gettext("\treceive [-vnF] <filesystem|volume|"
195 		"snapshot>\n"
196 		"\treceive [-vnF] -d <filesystem>\n"));
197 	case HELP_RENAME:
198 		return (gettext("\trename <filesystem|volume|snapshot> "
199 		    "<filesystem|volume|snapshot>\n"));
200 	case HELP_ROLLBACK:
201 		return (gettext("\trollback [-rRf] <snapshot>\n"));
202 	case HELP_SEND:
203 		return (gettext("\tsend [-i <snapshot>] <snapshot>\n"));
204 	case HELP_SET:
205 		return (gettext("\tset <property=value> "
206 		    "<filesystem|volume> ...\n"));
207 	case HELP_SHARE:
208 		return (gettext("\tshare -a\n"
209 		    "\tshare <filesystem>\n"));
210 	case HELP_SNAPSHOT:
211 		return (gettext("\tsnapshot [-r] "
212 		    "<filesystem@name|volume@name>\n"));
213 	case HELP_UNMOUNT:
214 		return (gettext("\tunmount [-f] -a\n"
215 		    "\tunmount [-f] <filesystem|mountpoint>\n"));
216 	case HELP_UNSHARE:
217 		return (gettext("\tunshare [-f] -a\n"
218 		    "\tunshare [-f] <filesystem|mountpoint>\n"));
219 	}
220 
221 	abort();
222 	/* NOTREACHED */
223 }
224 
225 /*
226  * Utility function to guarantee malloc() success.
227  */
228 void *
229 safe_malloc(size_t size)
230 {
231 	void *data;
232 
233 	if ((data = calloc(1, size)) == NULL) {
234 		(void) fprintf(stderr, "internal error: out of memory\n");
235 		exit(1);
236 	}
237 
238 	return (data);
239 }
240 
241 /*
242  * Display usage message.  If we're inside a command, display only the usage for
243  * that command.  Otherwise, iterate over the entire command table and display
244  * a complete usage message.
245  */
246 static void
247 usage(boolean_t requested)
248 {
249 	int i;
250 	boolean_t show_properties = B_FALSE;
251 	FILE *fp = requested ? stdout : stderr;
252 
253 	if (current_command == NULL) {
254 
255 		(void) fprintf(fp, gettext("usage: zfs command args ...\n"));
256 		(void) fprintf(fp,
257 		    gettext("where 'command' is one of the following:\n\n"));
258 
259 		for (i = 0; i < NCOMMAND; i++) {
260 			if (command_table[i].name == NULL)
261 				(void) fprintf(fp, "\n");
262 			else
263 				(void) fprintf(fp, "%s",
264 				    get_usage(command_table[i].usage));
265 		}
266 
267 		(void) fprintf(fp, gettext("\nEach dataset is of the form: "
268 		    "pool/[dataset/]*dataset[@name]\n"));
269 	} else {
270 		(void) fprintf(fp, gettext("usage:\n"));
271 		(void) fprintf(fp, "%s", get_usage(current_command->usage));
272 	}
273 
274 	if (current_command != NULL &&
275 	    (strcmp(current_command->name, "set") == 0 ||
276 	    strcmp(current_command->name, "get") == 0 ||
277 	    strcmp(current_command->name, "inherit") == 0 ||
278 	    strcmp(current_command->name, "list") == 0))
279 		show_properties = B_TRUE;
280 
281 	if (show_properties) {
282 
283 		(void) fprintf(fp,
284 		    gettext("\nThe following properties are supported:\n"));
285 
286 		(void) fprintf(fp, "\n\t%-13s  %s  %s   %s\n\n",
287 		    "PROPERTY", "EDIT", "INHERIT", "VALUES");
288 
289 		for (i = 0; i < ZFS_NPROP_VISIBLE; i++) {
290 			(void) fprintf(fp, "\t%-13s  ", zfs_prop_to_name(i));
291 
292 			if (zfs_prop_readonly(i))
293 				(void) fprintf(fp, "  NO    ");
294 			else
295 				(void) fprintf(fp, " YES    ");
296 
297 			if (zfs_prop_inheritable(i))
298 				(void) fprintf(fp, "  YES   ");
299 			else
300 				(void) fprintf(fp, "   NO   ");
301 
302 			if (zfs_prop_values(i) == NULL)
303 				(void) fprintf(fp, "-\n");
304 			else
305 				(void) fprintf(fp, "%s\n", zfs_prop_values(i));
306 		}
307 		(void) fprintf(fp, gettext("\nSizes are specified in bytes "
308 		    "with standard units such as K, M, G, etc.\n"));
309 		(void) fprintf(fp, gettext("\n\nUser-defined properties can "
310 		    "be specified by using a name containing a colon (:).\n"));
311 	} else {
312 		/*
313 		 * TRANSLATION NOTE:
314 		 * "zfs set|get" must not be localised this is the
315 		 * command name and arguments.
316 		 */
317 		(void) fprintf(fp,
318 		    gettext("\nFor the property list, run: zfs set|get\n"));
319 	}
320 
321 	/*
322 	 * See comments at end of main().
323 	 */
324 	if (getenv("ZFS_ABORT") != NULL) {
325 		(void) printf("dumping core by request\n");
326 		abort();
327 	}
328 
329 	exit(requested ? 0 : 2);
330 }
331 
332 /*
333  * zfs clone <fs, snap, vol> fs
334  *
335  * Given an existing dataset, create a writable copy whose initial contents
336  * are the same as the source.  The newly created dataset maintains a
337  * dependency on the original; the original cannot be destroyed so long as
338  * the clone exists.
339  */
340 static int
341 zfs_do_clone(int argc, char **argv)
342 {
343 	zfs_handle_t *zhp;
344 	int ret;
345 
346 	/* check options */
347 	if (argc > 1 && argv[1][0] == '-') {
348 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
349 		    argv[1][1]);
350 		usage(B_FALSE);
351 	}
352 
353 	/* check number of arguments */
354 	if (argc < 2) {
355 		(void) fprintf(stderr, gettext("missing source dataset "
356 		    "argument\n"));
357 		usage(B_FALSE);
358 	}
359 	if (argc < 3) {
360 		(void) fprintf(stderr, gettext("missing target dataset "
361 		    "argument\n"));
362 		usage(B_FALSE);
363 	}
364 	if (argc > 3) {
365 		(void) fprintf(stderr, gettext("too many arguments\n"));
366 		usage(B_FALSE);
367 	}
368 
369 	/* open the source dataset */
370 	if ((zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_SNAPSHOT)) == NULL)
371 		return (1);
372 
373 	/* pass to libzfs */
374 	ret = zfs_clone(zhp, argv[2], NULL);
375 
376 	/* create the mountpoint if necessary */
377 	if (ret == 0) {
378 		zfs_handle_t *clone = zfs_open(g_zfs, argv[2], ZFS_TYPE_ANY);
379 		if (clone != NULL) {
380 			if ((ret = zfs_mount(clone, NULL, 0)) == 0)
381 				ret = zfs_share(clone);
382 			zfs_close(clone);
383 		}
384 		zpool_log_history(g_zfs, argc, argv, argv[2], B_FALSE, B_FALSE);
385 	}
386 
387 	zfs_close(zhp);
388 
389 	return (ret == 0 ? 0 : 1);
390 }
391 
392 /*
393  * zfs create [-o prop=value] ... fs
394  * zfs create [-s] [-b blocksize] [-o prop=value] ... -V vol size
395  *
396  * Create a new dataset.  This command can be used to create filesystems
397  * and volumes.  Snapshot creation is handled by 'zfs snapshot'.
398  * For volumes, the user must specify a size to be used.
399  *
400  * The '-s' flag applies only to volumes, and indicates that we should not try
401  * to set the reservation for this volume.  By default we set a reservation
402  * equal to the size for any volume.
403  */
404 static int
405 zfs_do_create(int argc, char **argv)
406 {
407 	zfs_type_t type = ZFS_TYPE_FILESYSTEM;
408 	zfs_handle_t *zhp = NULL;
409 	uint64_t volsize;
410 	int c;
411 	boolean_t noreserve = B_FALSE;
412 	int ret = 1;
413 	nvlist_t *props = NULL;
414 	uint64_t intval;
415 	char *propname;
416 	char *propval = NULL;
417 	char *strval;
418 
419 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
420 		(void) fprintf(stderr, gettext("internal error: "
421 		    "out of memory\n"));
422 		return (1);
423 	}
424 
425 	/* check options */
426 	while ((c = getopt(argc, argv, ":V:b:so:")) != -1) {
427 		switch (c) {
428 		case 'V':
429 			type = ZFS_TYPE_VOLUME;
430 			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
431 				(void) fprintf(stderr, gettext("bad volume "
432 				    "size '%s': %s\n"), optarg,
433 				    libzfs_error_description(g_zfs));
434 				goto error;
435 			}
436 
437 			if (nvlist_add_uint64(props,
438 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
439 			    intval) != 0) {
440 				(void) fprintf(stderr, gettext("internal "
441 				    "error: out of memory\n"));
442 				goto error;
443 			}
444 			volsize = intval;
445 			break;
446 		case 'b':
447 			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
448 				(void) fprintf(stderr, gettext("bad volume "
449 				    "block size '%s': %s\n"), optarg,
450 				    libzfs_error_description(g_zfs));
451 				goto error;
452 			}
453 
454 			if (nvlist_add_uint64(props,
455 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
456 			    intval) != 0) {
457 				(void) fprintf(stderr, gettext("internal "
458 				    "error: out of memory\n"));
459 				goto error;
460 			}
461 			break;
462 		case 'o':
463 			propname = optarg;
464 			if ((propval = strchr(propname, '=')) == NULL) {
465 				(void) fprintf(stderr, gettext("missing "
466 				    "'=' for -o option\n"));
467 				goto error;
468 			}
469 			*propval = '\0';
470 			propval++;
471 			if (nvlist_lookup_string(props, propname,
472 			    &strval) == 0) {
473 				(void) fprintf(stderr, gettext("property '%s' "
474 				    "specified multiple times\n"), propname);
475 				goto error;
476 			}
477 			if (nvlist_add_string(props, propname, propval) != 0) {
478 				(void) fprintf(stderr, gettext("internal "
479 				    "error: out of memory\n"));
480 				goto error;
481 			}
482 			break;
483 		case 's':
484 			noreserve = B_TRUE;
485 			break;
486 		case ':':
487 			(void) fprintf(stderr, gettext("missing size "
488 			    "argument\n"));
489 			goto badusage;
490 			break;
491 		case '?':
492 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
493 			    optopt);
494 			goto badusage;
495 		}
496 	}
497 
498 	if (noreserve && type != ZFS_TYPE_VOLUME) {
499 		(void) fprintf(stderr, gettext("'-s' can only be used when "
500 		    "creating a volume\n"));
501 		goto badusage;
502 	}
503 
504 	argc -= optind;
505 	argv += optind;
506 
507 	/* check number of arguments */
508 	if (argc == 0) {
509 		(void) fprintf(stderr, gettext("missing %s argument\n"),
510 		    zfs_type_to_name(type));
511 		goto badusage;
512 	}
513 	if (argc > 1) {
514 		(void) fprintf(stderr, gettext("too many arguments\n"));
515 		goto badusage;
516 	}
517 
518 	if (type == ZFS_TYPE_VOLUME && !noreserve &&
519 	    nvlist_lookup_string(props, zfs_prop_to_name(ZFS_PROP_RESERVATION),
520 	    &strval) != 0) {
521 		if (nvlist_add_uint64(props,
522 		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
523 		    volsize) != 0) {
524 			(void) fprintf(stderr, gettext("internal "
525 			    "error: out of memory\n"));
526 			nvlist_free(props);
527 			return (1);
528 		}
529 	}
530 
531 	/* pass to libzfs */
532 	if (zfs_create(g_zfs, argv[0], type, props) != 0)
533 		goto error;
534 
535 	if (propval != NULL)
536 		*(propval - 1) = '=';
537 	zpool_log_history(g_zfs, argc + optind, argv - optind, argv[0],
538 	    B_FALSE, B_FALSE);
539 
540 	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_ANY)) == NULL)
541 		goto error;
542 
543 	/*
544 	 * Mount and/or share the new filesystem as appropriate.  We provide a
545 	 * verbose error message to let the user know that their filesystem was
546 	 * in fact created, even if we failed to mount or share it.
547 	 */
548 	if (zfs_mount(zhp, NULL, 0) != 0) {
549 		(void) fprintf(stderr, gettext("filesystem successfully "
550 		    "created, but not mounted\n"));
551 		ret = 1;
552 	} else if (zfs_share(zhp) != 0) {
553 		(void) fprintf(stderr, gettext("filesystem successfully "
554 		    "created, but not shared\n"));
555 		ret = 1;
556 	} else {
557 		ret = 0;
558 	}
559 
560 error:
561 	if (zhp)
562 		zfs_close(zhp);
563 	nvlist_free(props);
564 	return (ret);
565 badusage:
566 	nvlist_free(props);
567 	usage(B_FALSE);
568 	return (2);
569 }
570 
571 /*
572  * zfs destroy [-rf] <fs, snap, vol>
573  *
574  * 	-r	Recursively destroy all children
575  * 	-R	Recursively destroy all dependents, including clones
576  * 	-f	Force unmounting of any dependents
577  *
578  * Destroys the given dataset.  By default, it will unmount any filesystems,
579  * and refuse to destroy a dataset that has any dependents.  A dependent can
580  * either be a child, or a clone of a child.
581  */
582 typedef struct destroy_cbdata {
583 	boolean_t	cb_first;
584 	int		cb_force;
585 	int		cb_recurse;
586 	int		cb_error;
587 	int		cb_needforce;
588 	int		cb_doclones;
589 	zfs_handle_t	*cb_target;
590 	char		*cb_snapname;
591 } destroy_cbdata_t;
592 
593 /*
594  * Check for any dependents based on the '-r' or '-R' flags.
595  */
596 static int
597 destroy_check_dependent(zfs_handle_t *zhp, void *data)
598 {
599 	destroy_cbdata_t *cbp = data;
600 	const char *tname = zfs_get_name(cbp->cb_target);
601 	const char *name = zfs_get_name(zhp);
602 
603 	if (strncmp(tname, name, strlen(tname)) == 0 &&
604 	    (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
605 		/*
606 		 * This is a direct descendant, not a clone somewhere else in
607 		 * the hierarchy.
608 		 */
609 		if (cbp->cb_recurse)
610 			goto out;
611 
612 		if (cbp->cb_first) {
613 			(void) fprintf(stderr, gettext("cannot destroy '%s': "
614 			    "%s has children\n"),
615 			    zfs_get_name(cbp->cb_target),
616 			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
617 			(void) fprintf(stderr, gettext("use '-r' to destroy "
618 			    "the following datasets:\n"));
619 			cbp->cb_first = B_FALSE;
620 			cbp->cb_error = 1;
621 		}
622 
623 		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
624 	} else {
625 		/*
626 		 * This is a clone.  We only want to report this if the '-r'
627 		 * wasn't specified, or the target is a snapshot.
628 		 */
629 		if (!cbp->cb_recurse &&
630 		    zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
631 			goto out;
632 
633 		if (cbp->cb_first) {
634 			(void) fprintf(stderr, gettext("cannot destroy '%s': "
635 			    "%s has dependent clones\n"),
636 			    zfs_get_name(cbp->cb_target),
637 			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
638 			(void) fprintf(stderr, gettext("use '-R' to destroy "
639 			    "the following datasets:\n"));
640 			cbp->cb_first = B_FALSE;
641 			cbp->cb_error = 1;
642 		}
643 
644 		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
645 	}
646 
647 out:
648 	zfs_close(zhp);
649 	return (0);
650 }
651 
652 static int
653 destroy_callback(zfs_handle_t *zhp, void *data)
654 {
655 	destroy_cbdata_t *cbp = data;
656 
657 	/*
658 	 * Ignore pools (which we've already flagged as an error before getting
659 	 * here.
660 	 */
661 	if (strchr(zfs_get_name(zhp), '/') == NULL &&
662 	    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
663 		zfs_close(zhp);
664 		return (0);
665 	}
666 
667 	/*
668 	 * Bail out on the first error.
669 	 */
670 	if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 ||
671 	    zfs_destroy(zhp) != 0) {
672 		zfs_close(zhp);
673 		return (-1);
674 	}
675 
676 	zfs_close(zhp);
677 	return (0);
678 }
679 
680 static int
681 destroy_snap_clones(zfs_handle_t *zhp, void *arg)
682 {
683 	destroy_cbdata_t *cbp = arg;
684 	char thissnap[MAXPATHLEN];
685 	zfs_handle_t *szhp;
686 
687 	(void) snprintf(thissnap, sizeof (thissnap),
688 	    "%s@%s", zfs_get_name(zhp), cbp->cb_snapname);
689 
690 	libzfs_print_on_error(g_zfs, B_FALSE);
691 	szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT);
692 	libzfs_print_on_error(g_zfs, B_TRUE);
693 	if (szhp) {
694 		/*
695 		 * Destroy any clones of this snapshot
696 		 */
697 		if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback,
698 		    cbp) != 0) {
699 			zfs_close(szhp);
700 			return (-1);
701 		}
702 		zfs_close(szhp);
703 	}
704 
705 	return (zfs_iter_filesystems(zhp, destroy_snap_clones, arg));
706 }
707 
708 static int
709 zfs_do_destroy(int argc, char **argv)
710 {
711 	destroy_cbdata_t cb = { 0 };
712 	int c;
713 	zfs_handle_t *zhp;
714 	char *cp;
715 
716 	/* check options */
717 	while ((c = getopt(argc, argv, "frR")) != -1) {
718 		switch (c) {
719 		case 'f':
720 			cb.cb_force = 1;
721 			break;
722 		case 'r':
723 			cb.cb_recurse = 1;
724 			break;
725 		case 'R':
726 			cb.cb_recurse = 1;
727 			cb.cb_doclones = 1;
728 			break;
729 		case '?':
730 		default:
731 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
732 			    optopt);
733 			usage(B_FALSE);
734 		}
735 	}
736 
737 	argc -= optind;
738 	argv += optind;
739 
740 	/* check number of arguments */
741 	if (argc == 0) {
742 		(void) fprintf(stderr, gettext("missing path argument\n"));
743 		usage(B_FALSE);
744 	}
745 	if (argc > 1) {
746 		(void) fprintf(stderr, gettext("too many arguments\n"));
747 		usage(B_FALSE);
748 	}
749 
750 	/*
751 	 * If we are doing recursive destroy of a snapshot, then the
752 	 * named snapshot may not exist.  Go straight to libzfs.
753 	 */
754 	if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) {
755 		int ret;
756 
757 		*cp = '\0';
758 		if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_ANY)) == NULL)
759 			return (1);
760 		*cp = '@';
761 		cp++;
762 
763 		if (cb.cb_doclones) {
764 			cb.cb_snapname = cp;
765 			if (destroy_snap_clones(zhp, &cb) != 0) {
766 				zfs_close(zhp);
767 				return (1);
768 			}
769 		}
770 
771 		ret = zfs_destroy_snaps(zhp, cp);
772 		zfs_close(zhp);
773 		if (ret) {
774 			(void) fprintf(stderr,
775 			    gettext("no snapshots destroyed\n"));
776 		}
777 		return (ret != 0);
778 	}
779 
780 
781 	/* Open the given dataset */
782 	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_ANY)) == NULL)
783 		return (1);
784 
785 	cb.cb_target = zhp;
786 
787 	/*
788 	 * Perform an explicit check for pools before going any further.
789 	 */
790 	if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
791 	    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
792 		(void) fprintf(stderr, gettext("cannot destroy '%s': "
793 		    "operation does not apply to pools\n"),
794 		    zfs_get_name(zhp));
795 		(void) fprintf(stderr, gettext("use 'zfs destroy -r "
796 		    "%s' to destroy all datasets in the pool\n"),
797 		    zfs_get_name(zhp));
798 		(void) fprintf(stderr, gettext("use 'zpool destroy %s' "
799 		    "to destroy the pool itself\n"), zfs_get_name(zhp));
800 		zfs_close(zhp);
801 		return (1);
802 	}
803 
804 	/*
805 	 * Check for any dependents and/or clones.
806 	 */
807 	cb.cb_first = B_TRUE;
808 	if (!cb.cb_doclones &&
809 	    zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
810 	    &cb) != 0) {
811 		zfs_close(zhp);
812 		return (1);
813 	}
814 
815 
816 	if (cb.cb_error ||
817 	    zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0) {
818 		zfs_close(zhp);
819 		return (1);
820 	}
821 
822 	/*
823 	 * Do the real thing.  The callback will close the handle regardless of
824 	 * whether it succeeds or not.
825 	 */
826 	if (destroy_callback(zhp, &cb) != 0)
827 		return (1);
828 
829 	zpool_log_history(g_zfs, argc + optind, argv - optind, argv[0],
830 	    B_FALSE, B_FALSE);
831 
832 	return (0);
833 }
834 
835 /*
836  * zfs get [-rHp] [-o field[,field]...] [-s source[,source]...]
837  * 	< all | property[,property]... > < fs | snap | vol > ...
838  *
839  *	-r	recurse over any child datasets
840  *	-H	scripted mode.  Headers are stripped, and fields are separated
841  *		by tabs instead of spaces.
842  *	-o	Set of fields to display.  One of "name,property,value,source".
843  *		Default is all four.
844  *	-s	Set of sources to allow.  One of
845  *		"local,default,inherited,temporary,none".  Default is all
846  *		five.
847  *	-p	Display values in parsable (literal) format.
848  *
849  *  Prints properties for the given datasets.  The user can control which
850  *  columns to display as well as which property types to allow.
851  */
852 typedef struct get_cbdata {
853 	int cb_sources;
854 	int cb_columns[4];
855 	int cb_colwidths[5];
856 	boolean_t cb_scripted;
857 	boolean_t cb_literal;
858 	boolean_t cb_first;
859 	zfs_proplist_t *cb_proplist;
860 } get_cbdata_t;
861 
862 #define	GET_COL_NAME		1
863 #define	GET_COL_PROPERTY	2
864 #define	GET_COL_VALUE		3
865 #define	GET_COL_SOURCE		4
866 
867 /*
868  * Print the column headers for 'zfs get'.
869  */
870 static void
871 print_get_headers(get_cbdata_t *cbp)
872 {
873 	zfs_proplist_t *pl = cbp->cb_proplist;
874 	int i;
875 	char *title;
876 	size_t len;
877 
878 	cbp->cb_first = B_FALSE;
879 	if (cbp->cb_scripted)
880 		return;
881 
882 	/*
883 	 * Start with the length of the column headers.
884 	 */
885 	cbp->cb_colwidths[GET_COL_NAME] = strlen(gettext("NAME"));
886 	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(gettext("PROPERTY"));
887 	cbp->cb_colwidths[GET_COL_VALUE] = strlen(gettext("VALUE"));
888 	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(gettext("SOURCE"));
889 
890 	/*
891 	 * Go through and calculate the widths for each column.  For the
892 	 * 'source' column, we kludge it up by taking the worst-case scenario of
893 	 * inheriting from the longest name.  This is acceptable because in the
894 	 * majority of cases 'SOURCE' is the last column displayed, and we don't
895 	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
896 	 * if the name of the property is much longer the any values we find.
897 	 */
898 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
899 		/*
900 		 * 'PROPERTY' column
901 		 */
902 		if (pl->pl_prop != ZFS_PROP_INVAL) {
903 			len = strlen(zfs_prop_to_name(pl->pl_prop));
904 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
905 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
906 		} else {
907 			len = strlen(pl->pl_user_prop);
908 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
909 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
910 		}
911 
912 		/*
913 		 * 'VALUE' column
914 		 */
915 		if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) &&
916 		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
917 			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
918 
919 		/*
920 		 * 'NAME' and 'SOURCE' columns
921 		 */
922 		if (pl->pl_prop == ZFS_PROP_NAME &&
923 		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
924 			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
925 			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
926 			    strlen(gettext("inherited from"));
927 		}
928 	}
929 
930 	/*
931 	 * Now go through and print the headers.
932 	 */
933 	for (i = 0; i < 4; i++) {
934 		switch (cbp->cb_columns[i]) {
935 		case GET_COL_NAME:
936 			title = gettext("NAME");
937 			break;
938 		case GET_COL_PROPERTY:
939 			title = gettext("PROPERTY");
940 			break;
941 		case GET_COL_VALUE:
942 			title = gettext("VALUE");
943 			break;
944 		case GET_COL_SOURCE:
945 			title = gettext("SOURCE");
946 			break;
947 		default:
948 			title = NULL;
949 		}
950 
951 		if (title != NULL) {
952 			if (i == 3 || cbp->cb_columns[i + 1] == 0)
953 				(void) printf("%s", title);
954 			else
955 				(void) printf("%-*s  ",
956 				    cbp->cb_colwidths[cbp->cb_columns[i]],
957 				    title);
958 		}
959 	}
960 	(void) printf("\n");
961 }
962 
963 /*
964  * Display a single line of output, according to the settings in the callback
965  * structure.
966  */
967 static void
968 print_one_property(zfs_handle_t *zhp, get_cbdata_t *cbp, const char *propname,
969     const char *value, zfs_source_t sourcetype, const char *source)
970 {
971 	int i;
972 	const char *str;
973 	char buf[128];
974 
975 	/*
976 	 * Ignore those source types that the user has chosen to ignore.
977 	 */
978 	if ((sourcetype & cbp->cb_sources) == 0)
979 		return;
980 
981 	if (cbp->cb_first)
982 		print_get_headers(cbp);
983 
984 	for (i = 0; i < 4; i++) {
985 		switch (cbp->cb_columns[i]) {
986 		case GET_COL_NAME:
987 			str = zfs_get_name(zhp);
988 			break;
989 
990 		case GET_COL_PROPERTY:
991 			str = propname;
992 			break;
993 
994 		case GET_COL_VALUE:
995 			str = value;
996 			break;
997 
998 		case GET_COL_SOURCE:
999 			switch (sourcetype) {
1000 			case ZFS_SRC_NONE:
1001 				str = "-";
1002 				break;
1003 
1004 			case ZFS_SRC_DEFAULT:
1005 				str = "default";
1006 				break;
1007 
1008 			case ZFS_SRC_LOCAL:
1009 				str = "local";
1010 				break;
1011 
1012 			case ZFS_SRC_TEMPORARY:
1013 				str = "temporary";
1014 				break;
1015 
1016 			case ZFS_SRC_INHERITED:
1017 				(void) snprintf(buf, sizeof (buf),
1018 				    "inherited from %s", source);
1019 				str = buf;
1020 				break;
1021 			}
1022 			break;
1023 
1024 		default:
1025 			continue;
1026 		}
1027 
1028 		if (cbp->cb_columns[i + 1] == 0)
1029 			(void) printf("%s", str);
1030 		else if (cbp->cb_scripted)
1031 			(void) printf("%s\t", str);
1032 		else
1033 			(void) printf("%-*s  ",
1034 			    cbp->cb_colwidths[cbp->cb_columns[i]],
1035 			    str);
1036 
1037 	}
1038 
1039 	(void) printf("\n");
1040 }
1041 
1042 /*
1043  * Invoked to display the properties for a single dataset.
1044  */
1045 static int
1046 get_callback(zfs_handle_t *zhp, void *data)
1047 {
1048 	char buf[ZFS_MAXPROPLEN];
1049 	zfs_source_t sourcetype;
1050 	char source[ZFS_MAXNAMELEN];
1051 	get_cbdata_t *cbp = data;
1052 	nvlist_t *userprop = zfs_get_user_props(zhp);
1053 	zfs_proplist_t *pl = cbp->cb_proplist;
1054 	nvlist_t *propval;
1055 	char *strval;
1056 	char *sourceval;
1057 
1058 	for (; pl != NULL; pl = pl->pl_next) {
1059 		/*
1060 		 * Skip the special fake placeholder.  This will also skip over
1061 		 * the name property when 'all' is specified.
1062 		 */
1063 		if (pl->pl_prop == ZFS_PROP_NAME &&
1064 		    pl == cbp->cb_proplist)
1065 			continue;
1066 
1067 		if (pl->pl_prop != ZFS_PROP_INVAL) {
1068 			if (zfs_prop_get(zhp, pl->pl_prop, buf,
1069 			    sizeof (buf), &sourcetype, source,
1070 			    sizeof (source),
1071 			    cbp->cb_literal) != 0) {
1072 				if (pl->pl_all)
1073 					continue;
1074 				sourcetype = ZFS_SRC_NONE;
1075 				(void) strlcpy(buf, "-", sizeof (buf));
1076 			}
1077 
1078 			print_one_property(zhp, cbp,
1079 			    zfs_prop_to_name(pl->pl_prop),
1080 			    buf, sourcetype, source);
1081 		} else {
1082 			if (nvlist_lookup_nvlist(userprop,
1083 			    pl->pl_user_prop, &propval) != 0) {
1084 				if (pl->pl_all)
1085 					continue;
1086 				sourcetype = ZFS_SRC_NONE;
1087 				strval = "-";
1088 			} else {
1089 				verify(nvlist_lookup_string(propval,
1090 				    ZFS_PROP_VALUE, &strval) == 0);
1091 				verify(nvlist_lookup_string(propval,
1092 				    ZFS_PROP_SOURCE, &sourceval) == 0);
1093 
1094 				if (strcmp(sourceval,
1095 				    zfs_get_name(zhp)) == 0) {
1096 					sourcetype = ZFS_SRC_LOCAL;
1097 				} else {
1098 					sourcetype = ZFS_SRC_INHERITED;
1099 					(void) strlcpy(source,
1100 					    sourceval, sizeof (source));
1101 				}
1102 			}
1103 
1104 			print_one_property(zhp, cbp,
1105 			    pl->pl_user_prop, strval, sourcetype,
1106 			    source);
1107 		}
1108 	}
1109 
1110 	return (0);
1111 }
1112 
1113 static int
1114 zfs_do_get(int argc, char **argv)
1115 {
1116 	get_cbdata_t cb = { 0 };
1117 	boolean_t recurse = B_FALSE;
1118 	int i, c;
1119 	char *value, *fields;
1120 	int ret;
1121 	zfs_proplist_t fake_name = { 0 };
1122 
1123 	/*
1124 	 * Set up default columns and sources.
1125 	 */
1126 	cb.cb_sources = ZFS_SRC_ALL;
1127 	cb.cb_columns[0] = GET_COL_NAME;
1128 	cb.cb_columns[1] = GET_COL_PROPERTY;
1129 	cb.cb_columns[2] = GET_COL_VALUE;
1130 	cb.cb_columns[3] = GET_COL_SOURCE;
1131 
1132 	/* check options */
1133 	while ((c = getopt(argc, argv, ":o:s:rHp")) != -1) {
1134 		switch (c) {
1135 		case 'p':
1136 			cb.cb_literal = B_TRUE;
1137 			break;
1138 		case 'r':
1139 			recurse = B_TRUE;
1140 			break;
1141 		case 'H':
1142 			cb.cb_scripted = B_TRUE;
1143 			break;
1144 		case ':':
1145 			(void) fprintf(stderr, gettext("missing argument for "
1146 			    "'%c' option\n"), optopt);
1147 			usage(B_FALSE);
1148 			break;
1149 		case 'o':
1150 			/*
1151 			 * Process the set of columns to display.  We zero out
1152 			 * the structure to give us a blank slate.
1153 			 */
1154 			bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1155 			i = 0;
1156 			while (*optarg != '\0') {
1157 				static char *col_subopts[] =
1158 				    { "name", "property", "value", "source",
1159 				    NULL };
1160 
1161 				if (i == 4) {
1162 					(void) fprintf(stderr, gettext("too "
1163 					    "many fields given to -o "
1164 					    "option\n"));
1165 					usage(B_FALSE);
1166 				}
1167 
1168 				switch (getsubopt(&optarg, col_subopts,
1169 				    &value)) {
1170 				case 0:
1171 					cb.cb_columns[i++] = GET_COL_NAME;
1172 					break;
1173 				case 1:
1174 					cb.cb_columns[i++] = GET_COL_PROPERTY;
1175 					break;
1176 				case 2:
1177 					cb.cb_columns[i++] = GET_COL_VALUE;
1178 					break;
1179 				case 3:
1180 					cb.cb_columns[i++] = GET_COL_SOURCE;
1181 					break;
1182 				default:
1183 					(void) fprintf(stderr,
1184 					    gettext("invalid column name "
1185 					    "'%s'\n"), value);
1186 					    usage(B_FALSE);
1187 				}
1188 			}
1189 			break;
1190 
1191 		case 's':
1192 			cb.cb_sources = 0;
1193 			while (*optarg != '\0') {
1194 				static char *source_subopts[] = {
1195 					"local", "default", "inherited",
1196 					"temporary", "none", NULL };
1197 
1198 				switch (getsubopt(&optarg, source_subopts,
1199 				    &value)) {
1200 				case 0:
1201 					cb.cb_sources |= ZFS_SRC_LOCAL;
1202 					break;
1203 				case 1:
1204 					cb.cb_sources |= ZFS_SRC_DEFAULT;
1205 					break;
1206 				case 2:
1207 					cb.cb_sources |= ZFS_SRC_INHERITED;
1208 					break;
1209 				case 3:
1210 					cb.cb_sources |= ZFS_SRC_TEMPORARY;
1211 					break;
1212 				case 4:
1213 					cb.cb_sources |= ZFS_SRC_NONE;
1214 					break;
1215 				default:
1216 					(void) fprintf(stderr,
1217 					    gettext("invalid source "
1218 					    "'%s'\n"), value);
1219 					    usage(B_FALSE);
1220 				}
1221 			}
1222 			break;
1223 
1224 		case '?':
1225 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1226 			    optopt);
1227 			usage(B_FALSE);
1228 		}
1229 	}
1230 
1231 	argc -= optind;
1232 	argv += optind;
1233 
1234 	if (argc < 1) {
1235 		(void) fprintf(stderr, gettext("missing property "
1236 		    "argument\n"));
1237 		usage(B_FALSE);
1238 	}
1239 
1240 	fields = argv[0];
1241 
1242 	if (zfs_get_proplist(g_zfs, fields, &cb.cb_proplist) != 0)
1243 		usage(B_FALSE);
1244 
1245 	argc--;
1246 	argv++;
1247 
1248 	/*
1249 	 * As part of zfs_expand_proplist(), we keep track of the maximum column
1250 	 * width for each property.  For the 'NAME' (and 'SOURCE') columns, we
1251 	 * need to know the maximum name length.  However, the user likely did
1252 	 * not specify 'name' as one of the properties to fetch, so we need to
1253 	 * make sure we always include at least this property for
1254 	 * print_get_headers() to work properly.
1255 	 */
1256 	if (cb.cb_proplist != NULL) {
1257 		fake_name.pl_prop = ZFS_PROP_NAME;
1258 		fake_name.pl_width = strlen(gettext("NAME"));
1259 		fake_name.pl_next = cb.cb_proplist;
1260 		cb.cb_proplist = &fake_name;
1261 	}
1262 
1263 	cb.cb_first = B_TRUE;
1264 
1265 	/* run for each object */
1266 	ret = zfs_for_each(argc, argv, recurse, ZFS_TYPE_ANY, NULL,
1267 	    &cb.cb_proplist, get_callback, &cb);
1268 
1269 	if (cb.cb_proplist == &fake_name)
1270 		zfs_free_proplist(fake_name.pl_next);
1271 	else
1272 		zfs_free_proplist(cb.cb_proplist);
1273 
1274 	return (ret);
1275 }
1276 
1277 /*
1278  * inherit [-r] <property> <fs|vol> ...
1279  *
1280  * 	-r	Recurse over all children
1281  *
1282  * For each dataset specified on the command line, inherit the given property
1283  * from its parent.  Inheriting a property at the pool level will cause it to
1284  * use the default value.  The '-r' flag will recurse over all children, and is
1285  * useful for setting a property on a hierarchy-wide basis, regardless of any
1286  * local modifications for each dataset.
1287  */
1288 typedef struct inherit_cbdata {
1289 	char		*cb_propname;
1290 	boolean_t	cb_any_successful;
1291 } inherit_cbdata_t;
1292 
1293 static int
1294 inherit_callback(zfs_handle_t *zhp, void *data)
1295 {
1296 	inherit_cbdata_t *cbp = data;
1297 	int ret;
1298 
1299 	ret = zfs_prop_inherit(zhp, cbp->cb_propname);
1300 	if (ret == 0)
1301 		cbp->cb_any_successful = B_TRUE;
1302 	return (ret != 0);
1303 }
1304 
1305 static int
1306 zfs_do_inherit(int argc, char **argv)
1307 {
1308 	boolean_t recurse = B_FALSE;
1309 	int c;
1310 	zfs_prop_t prop;
1311 	inherit_cbdata_t cb;
1312 	int ret;
1313 
1314 	/* check options */
1315 	while ((c = getopt(argc, argv, "r")) != -1) {
1316 		switch (c) {
1317 		case 'r':
1318 			recurse = B_TRUE;
1319 			break;
1320 		case '?':
1321 		default:
1322 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1323 			    optopt);
1324 			usage(B_FALSE);
1325 		}
1326 	}
1327 
1328 	argc -= optind;
1329 	argv += optind;
1330 
1331 	/* check number of arguments */
1332 	if (argc < 1) {
1333 		(void) fprintf(stderr, gettext("missing property argument\n"));
1334 		usage(B_FALSE);
1335 	}
1336 	if (argc < 2) {
1337 		(void) fprintf(stderr, gettext("missing dataset argument\n"));
1338 		usage(B_FALSE);
1339 	}
1340 
1341 	cb.cb_propname = argv[0];
1342 	argc--;
1343 	argv++;
1344 
1345 	if ((prop = zfs_name_to_prop(cb.cb_propname)) != ZFS_PROP_INVAL) {
1346 		if (zfs_prop_readonly(prop)) {
1347 			(void) fprintf(stderr, gettext(
1348 			    "%s property is read-only\n"),
1349 			    cb.cb_propname);
1350 			return (1);
1351 		}
1352 		if (!zfs_prop_inheritable(prop)) {
1353 			(void) fprintf(stderr, gettext("'%s' property cannot "
1354 			    "be inherited\n"), cb.cb_propname);
1355 			if (prop == ZFS_PROP_QUOTA ||
1356 			    prop == ZFS_PROP_RESERVATION)
1357 				(void) fprintf(stderr, gettext("use 'zfs set "
1358 				    "%s=none' to clear\n"), cb.cb_propname);
1359 			return (1);
1360 		}
1361 	} else if (!zfs_prop_user(cb.cb_propname)) {
1362 		(void) fprintf(stderr, gettext(
1363 		    "invalid property '%s'\n"),
1364 		    cb.cb_propname);
1365 		usage(B_FALSE);
1366 	}
1367 
1368 	cb.cb_any_successful = B_FALSE;
1369 
1370 	ret = zfs_for_each(argc, argv, recurse,
1371 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, NULL, NULL,
1372 	    inherit_callback, &cb);
1373 
1374 	if (cb.cb_any_successful) {
1375 		zpool_log_history(g_zfs, argc + optind + 1, argv - optind - 1,
1376 		    argv[0], B_FALSE, B_FALSE);
1377 	}
1378 
1379 	return (ret);
1380 }
1381 
1382 /*
1383  * list [-rH] [-o property[,property]...] [-t type[,type]...]
1384  *      [-s property [-s property]...] [-S property [-S property]...]
1385  *      <dataset> ...
1386  *
1387  * 	-r	Recurse over all children
1388  * 	-H	Scripted mode; elide headers and separate colums by tabs
1389  * 	-o	Control which fields to display.
1390  * 	-t	Control which object types to display.
1391  *	-s	Specify sort columns, descending order.
1392  *	-S	Specify sort columns, ascending order.
1393  *
1394  * When given no arguments, lists all filesystems in the system.
1395  * Otherwise, list the specified datasets, optionally recursing down them if
1396  * '-r' is specified.
1397  */
1398 typedef struct list_cbdata {
1399 	boolean_t	cb_first;
1400 	boolean_t	cb_scripted;
1401 	zfs_proplist_t	*cb_proplist;
1402 } list_cbdata_t;
1403 
1404 /*
1405  * Given a list of columns to display, output appropriate headers for each one.
1406  */
1407 static void
1408 print_header(zfs_proplist_t *pl)
1409 {
1410 	char headerbuf[ZFS_MAXPROPLEN];
1411 	const char *header;
1412 	int i;
1413 	boolean_t first = B_TRUE;
1414 	boolean_t right_justify;
1415 
1416 	for (; pl != NULL; pl = pl->pl_next) {
1417 		if (!first) {
1418 			(void) printf("  ");
1419 		} else {
1420 			first = B_FALSE;
1421 		}
1422 
1423 		right_justify = B_FALSE;
1424 		if (pl->pl_prop != ZFS_PROP_INVAL) {
1425 			header = zfs_prop_column_name(pl->pl_prop);
1426 			right_justify = zfs_prop_align_right(pl->pl_prop);
1427 		} else {
1428 			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
1429 				headerbuf[i] = toupper(pl->pl_user_prop[i]);
1430 			headerbuf[i] = '\0';
1431 			header = headerbuf;
1432 		}
1433 
1434 		if (pl->pl_next == NULL && !right_justify)
1435 			(void) printf("%s", header);
1436 		else if (right_justify)
1437 			(void) printf("%*s", pl->pl_width, header);
1438 		else
1439 			(void) printf("%-*s", pl->pl_width, header);
1440 	}
1441 
1442 	(void) printf("\n");
1443 }
1444 
1445 /*
1446  * Given a dataset and a list of fields, print out all the properties according
1447  * to the described layout.
1448  */
1449 static void
1450 print_dataset(zfs_handle_t *zhp, zfs_proplist_t *pl, int scripted)
1451 {
1452 	boolean_t first = B_TRUE;
1453 	char property[ZFS_MAXPROPLEN];
1454 	nvlist_t *userprops = zfs_get_user_props(zhp);
1455 	nvlist_t *propval;
1456 	char *propstr;
1457 	boolean_t right_justify;
1458 	int width;
1459 
1460 	for (; pl != NULL; pl = pl->pl_next) {
1461 		if (!first) {
1462 			if (scripted)
1463 				(void) printf("\t");
1464 			else
1465 				(void) printf("  ");
1466 		} else {
1467 			first = B_FALSE;
1468 		}
1469 
1470 		right_justify = B_FALSE;
1471 		if (pl->pl_prop != ZFS_PROP_INVAL) {
1472 			if (zfs_prop_get(zhp, pl->pl_prop, property,
1473 			    sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
1474 				propstr = "-";
1475 			else
1476 				propstr = property;
1477 
1478 			right_justify = zfs_prop_align_right(pl->pl_prop);
1479 		} else {
1480 			if (nvlist_lookup_nvlist(userprops,
1481 			    pl->pl_user_prop, &propval) != 0)
1482 				propstr = "-";
1483 			else
1484 				verify(nvlist_lookup_string(propval,
1485 				    ZFS_PROP_VALUE, &propstr) == 0);
1486 		}
1487 
1488 		width = pl->pl_width;
1489 
1490 		/*
1491 		 * If this is being called in scripted mode, or if this is the
1492 		 * last column and it is left-justified, don't include a width
1493 		 * format specifier.
1494 		 */
1495 		if (scripted || (pl->pl_next == NULL && !right_justify))
1496 			(void) printf("%s", propstr);
1497 		else if (right_justify)
1498 			(void) printf("%*s", width, propstr);
1499 		else
1500 			(void) printf("%-*s", width, propstr);
1501 	}
1502 
1503 	(void) printf("\n");
1504 }
1505 
1506 /*
1507  * Generic callback function to list a dataset or snapshot.
1508  */
1509 static int
1510 list_callback(zfs_handle_t *zhp, void *data)
1511 {
1512 	list_cbdata_t *cbp = data;
1513 
1514 	if (cbp->cb_first) {
1515 		if (!cbp->cb_scripted)
1516 			print_header(cbp->cb_proplist);
1517 		cbp->cb_first = B_FALSE;
1518 	}
1519 
1520 	print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
1521 
1522 	return (0);
1523 }
1524 
1525 static int
1526 zfs_do_list(int argc, char **argv)
1527 {
1528 	int c;
1529 	boolean_t recurse = B_FALSE;
1530 	boolean_t scripted = B_FALSE;
1531 	static char default_fields[] =
1532 	    "name,used,available,referenced,mountpoint";
1533 	int types = ZFS_TYPE_ANY;
1534 	char *fields = NULL;
1535 	char *basic_fields = default_fields;
1536 	list_cbdata_t cb = { 0 };
1537 	char *value;
1538 	int ret;
1539 	char *type_subopts[] = { "filesystem", "volume", "snapshot", NULL };
1540 	zfs_sort_column_t *sortcol = NULL;
1541 
1542 	/* check options */
1543 	while ((c = getopt(argc, argv, ":o:rt:Hs:S:")) != -1) {
1544 		switch (c) {
1545 		case 'o':
1546 			fields = optarg;
1547 			break;
1548 		case 'r':
1549 			recurse = B_TRUE;
1550 			break;
1551 		case 'H':
1552 			scripted = B_TRUE;
1553 			break;
1554 		case 's':
1555 			if (zfs_add_sort_column(&sortcol, optarg,
1556 			    B_FALSE) != 0) {
1557 				(void) fprintf(stderr,
1558 				    gettext("invalid property '%s'\n"), optarg);
1559 				usage(B_FALSE);
1560 			}
1561 			break;
1562 		case 'S':
1563 			if (zfs_add_sort_column(&sortcol, optarg,
1564 			    B_TRUE) != 0) {
1565 				(void) fprintf(stderr,
1566 				    gettext("invalid property '%s'\n"), optarg);
1567 				usage(B_FALSE);
1568 			}
1569 			break;
1570 		case 't':
1571 			types = 0;
1572 			while (*optarg != '\0') {
1573 				switch (getsubopt(&optarg, type_subopts,
1574 				    &value)) {
1575 				case 0:
1576 					types |= ZFS_TYPE_FILESYSTEM;
1577 					break;
1578 				case 1:
1579 					types |= ZFS_TYPE_VOLUME;
1580 					break;
1581 				case 2:
1582 					types |= ZFS_TYPE_SNAPSHOT;
1583 					break;
1584 				default:
1585 					(void) fprintf(stderr,
1586 					    gettext("invalid type '%s'\n"),
1587 					    value);
1588 					usage(B_FALSE);
1589 				}
1590 			}
1591 			break;
1592 		case ':':
1593 			(void) fprintf(stderr, gettext("missing argument for "
1594 			    "'%c' option\n"), optopt);
1595 			usage(B_FALSE);
1596 			break;
1597 		case '?':
1598 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1599 			    optopt);
1600 			usage(B_FALSE);
1601 		}
1602 	}
1603 
1604 	argc -= optind;
1605 	argv += optind;
1606 
1607 	if (fields == NULL)
1608 		fields = basic_fields;
1609 
1610 	/*
1611 	 * If the user specifies '-o all', the zfs_get_proplist() doesn't
1612 	 * normally include the name of the dataset.  For 'zfs list', we always
1613 	 * want this property to be first.
1614 	 */
1615 	if (zfs_get_proplist(g_zfs, fields, &cb.cb_proplist) != 0)
1616 		usage(B_FALSE);
1617 
1618 	cb.cb_scripted = scripted;
1619 	cb.cb_first = B_TRUE;
1620 
1621 	ret = zfs_for_each(argc, argv, recurse, types, sortcol, &cb.cb_proplist,
1622 	    list_callback, &cb);
1623 
1624 	zfs_free_proplist(cb.cb_proplist);
1625 	zfs_free_sort_columns(sortcol);
1626 
1627 	if (ret == 0 && cb.cb_first)
1628 		(void) printf(gettext("no datasets available\n"));
1629 
1630 	return (ret);
1631 }
1632 
1633 /*
1634  * zfs rename <fs | snap | vol> <fs | snap | vol>
1635  *
1636  * Renames the given dataset to another of the same type.
1637  */
1638 /* ARGSUSED */
1639 static int
1640 zfs_do_rename(int argc, char **argv)
1641 {
1642 	zfs_handle_t *zhp;
1643 	int ret;
1644 
1645 	/* check options */
1646 	if (argc > 1 && argv[1][0] == '-') {
1647 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1648 		    argv[1][1]);
1649 		usage(B_FALSE);
1650 	}
1651 
1652 	/* check number of arguments */
1653 	if (argc < 2) {
1654 		(void) fprintf(stderr, gettext("missing source dataset "
1655 		    "argument\n"));
1656 		usage(B_FALSE);
1657 	}
1658 	if (argc < 3) {
1659 		(void) fprintf(stderr, gettext("missing target dataset "
1660 		    "argument\n"));
1661 		usage(B_FALSE);
1662 	}
1663 	if (argc > 3) {
1664 		(void) fprintf(stderr, gettext("too many arguments\n"));
1665 		usage(B_FALSE);
1666 	}
1667 
1668 	if ((zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_ANY)) == NULL)
1669 		return (1);
1670 
1671 	ret = (zfs_rename(zhp, argv[2]) != 0);
1672 
1673 	if (!ret)
1674 		zpool_log_history(g_zfs, argc, argv, argv[2], B_FALSE, B_FALSE);
1675 
1676 	zfs_close(zhp);
1677 	return (ret);
1678 }
1679 
1680 /*
1681  * zfs promote <fs>
1682  *
1683  * Promotes the given clone fs to be the parent
1684  */
1685 /* ARGSUSED */
1686 static int
1687 zfs_do_promote(int argc, char **argv)
1688 {
1689 	zfs_handle_t *zhp;
1690 	int ret;
1691 
1692 	/* check options */
1693 	if (argc > 1 && argv[1][0] == '-') {
1694 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1695 		    argv[1][1]);
1696 		usage(B_FALSE);
1697 	}
1698 
1699 	/* check number of arguments */
1700 	if (argc < 2) {
1701 		(void) fprintf(stderr, gettext("missing clone filesystem"
1702 		    " argument\n"));
1703 		usage(B_FALSE);
1704 	}
1705 	if (argc > 2) {
1706 		(void) fprintf(stderr, gettext("too many arguments\n"));
1707 		usage(B_FALSE);
1708 	}
1709 
1710 	zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1711 	if (zhp == NULL)
1712 		return (1);
1713 
1714 	ret = (zfs_promote(zhp) != 0);
1715 
1716 	if (!ret)
1717 		zpool_log_history(g_zfs, argc, argv, argv[1], B_FALSE, B_FALSE);
1718 
1719 	zfs_close(zhp);
1720 	return (ret);
1721 }
1722 
1723 /*
1724  * zfs rollback [-rfR] <snapshot>
1725  *
1726  * 	-r	Delete any intervening snapshots before doing rollback
1727  * 	-R	Delete any snapshots and their clones
1728  * 	-f	Force unmount filesystems, even if they are in use.
1729  *
1730  * Given a filesystem, rollback to a specific snapshot, discarding any changes
1731  * since then and making it the active dataset.  If more recent snapshots exist,
1732  * the command will complain unless the '-r' flag is given.
1733  */
1734 typedef struct rollback_cbdata {
1735 	uint64_t	cb_create;
1736 	boolean_t	cb_first;
1737 	int		cb_doclones;
1738 	char		*cb_target;
1739 	int		cb_error;
1740 	boolean_t	cb_recurse;
1741 	boolean_t	cb_dependent;
1742 } rollback_cbdata_t;
1743 
1744 /*
1745  * Report any snapshots more recent than the one specified.  Used when '-r' is
1746  * not specified.  We reuse this same callback for the snapshot dependents - if
1747  * 'cb_dependent' is set, then this is a dependent and we should report it
1748  * without checking the transaction group.
1749  */
1750 static int
1751 rollback_check(zfs_handle_t *zhp, void *data)
1752 {
1753 	rollback_cbdata_t *cbp = data;
1754 
1755 	if (cbp->cb_doclones) {
1756 		zfs_close(zhp);
1757 		return (0);
1758 	}
1759 
1760 	if (!cbp->cb_dependent) {
1761 		if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
1762 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
1763 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
1764 		    cbp->cb_create) {
1765 
1766 			if (cbp->cb_first && !cbp->cb_recurse) {
1767 				(void) fprintf(stderr, gettext("cannot "
1768 				    "rollback to '%s': more recent snapshots "
1769 				    "exist\n"),
1770 				    cbp->cb_target);
1771 				(void) fprintf(stderr, gettext("use '-r' to "
1772 				    "force deletion of the following "
1773 				    "snapshots:\n"));
1774 				cbp->cb_first = 0;
1775 				cbp->cb_error = 1;
1776 			}
1777 
1778 			if (cbp->cb_recurse) {
1779 				cbp->cb_dependent = B_TRUE;
1780 				if (zfs_iter_dependents(zhp, B_TRUE,
1781 				    rollback_check, cbp) != 0) {
1782 					zfs_close(zhp);
1783 					return (-1);
1784 				}
1785 				cbp->cb_dependent = B_FALSE;
1786 			} else {
1787 				(void) fprintf(stderr, "%s\n",
1788 				    zfs_get_name(zhp));
1789 			}
1790 		}
1791 	} else {
1792 		if (cbp->cb_first && cbp->cb_recurse) {
1793 			(void) fprintf(stderr, gettext("cannot rollback to "
1794 			    "'%s': clones of previous snapshots exist\n"),
1795 			    cbp->cb_target);
1796 			(void) fprintf(stderr, gettext("use '-R' to "
1797 			    "force deletion of the following clones and "
1798 			    "dependents:\n"));
1799 			cbp->cb_first = 0;
1800 			cbp->cb_error = 1;
1801 		}
1802 
1803 		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1804 	}
1805 
1806 	zfs_close(zhp);
1807 	return (0);
1808 }
1809 
1810 static int
1811 zfs_do_rollback(int argc, char **argv)
1812 {
1813 	int ret;
1814 	int c;
1815 	rollback_cbdata_t cb = { 0 };
1816 	zfs_handle_t *zhp, *snap;
1817 	char parentname[ZFS_MAXNAMELEN];
1818 	char *delim;
1819 	int force = 0;
1820 
1821 	/* check options */
1822 	while ((c = getopt(argc, argv, "rfR")) != -1) {
1823 		switch (c) {
1824 		case 'f':
1825 			force = 1;
1826 			break;
1827 		case 'r':
1828 			cb.cb_recurse = 1;
1829 			break;
1830 		case 'R':
1831 			cb.cb_recurse = 1;
1832 			cb.cb_doclones = 1;
1833 			break;
1834 		case '?':
1835 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1836 			    optopt);
1837 			usage(B_FALSE);
1838 		}
1839 	}
1840 
1841 	argc -= optind;
1842 	argv += optind;
1843 
1844 	/* check number of arguments */
1845 	if (argc < 1) {
1846 		(void) fprintf(stderr, gettext("missing dataset argument\n"));
1847 		usage(B_FALSE);
1848 	}
1849 	if (argc > 1) {
1850 		(void) fprintf(stderr, gettext("too many arguments\n"));
1851 		usage(B_FALSE);
1852 	}
1853 
1854 	/* open the snapshot */
1855 	if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
1856 		return (1);
1857 
1858 	/* open the parent dataset */
1859 	(void) strlcpy(parentname, argv[0], sizeof (parentname));
1860 	verify((delim = strrchr(parentname, '@')) != NULL);
1861 	*delim = '\0';
1862 	if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_ANY)) == NULL) {
1863 		zfs_close(snap);
1864 		return (1);
1865 	}
1866 
1867 	/*
1868 	 * Check for more recent snapshots and/or clones based on the presence
1869 	 * of '-r' and '-R'.
1870 	 */
1871 	cb.cb_target = argv[0];
1872 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
1873 	cb.cb_first = B_TRUE;
1874 	cb.cb_error = 0;
1875 	if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
1876 		goto out;
1877 
1878 	if ((ret = cb.cb_error) != 0)
1879 		goto out;
1880 
1881 	/*
1882 	 * Rollback parent to the given snapshot.
1883 	 */
1884 	ret = zfs_rollback(zhp, snap, force);
1885 
1886 	if (!ret) {
1887 		zpool_log_history(g_zfs, argc + optind, argv - optind, argv[0],
1888 		    B_FALSE, B_FALSE);
1889 	}
1890 
1891 out:
1892 	zfs_close(snap);
1893 	zfs_close(zhp);
1894 
1895 	if (ret == 0)
1896 		return (0);
1897 	else
1898 		return (1);
1899 }
1900 
1901 /*
1902  * zfs set property=value { fs | snap | vol } ...
1903  *
1904  * Sets the given property for all datasets specified on the command line.
1905  */
1906 typedef struct set_cbdata {
1907 	char		*cb_propname;
1908 	char		*cb_value;
1909 	boolean_t	cb_any_successful;
1910 } set_cbdata_t;
1911 
1912 static int
1913 set_callback(zfs_handle_t *zhp, void *data)
1914 {
1915 	set_cbdata_t *cbp = data;
1916 
1917 	if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
1918 		switch (libzfs_errno(g_zfs)) {
1919 		case EZFS_MOUNTFAILED:
1920 			(void) fprintf(stderr, gettext("property may be set "
1921 			    "but unable to remount filesystem\n"));
1922 			break;
1923 		case EZFS_SHARENFSFAILED:
1924 			(void) fprintf(stderr, gettext("property may be set "
1925 			    "but unable to reshare filesystem\n"));
1926 			break;
1927 		}
1928 		return (1);
1929 	}
1930 	cbp->cb_any_successful = B_TRUE;
1931 	return (0);
1932 }
1933 
1934 static int
1935 zfs_do_set(int argc, char **argv)
1936 {
1937 	set_cbdata_t cb;
1938 	int ret;
1939 
1940 	/* check for options */
1941 	if (argc > 1 && argv[1][0] == '-') {
1942 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1943 		    argv[1][1]);
1944 		usage(B_FALSE);
1945 	}
1946 
1947 	/* check number of arguments */
1948 	if (argc < 2) {
1949 		(void) fprintf(stderr, gettext("missing property=value "
1950 		    "argument\n"));
1951 		usage(B_FALSE);
1952 	}
1953 	if (argc < 3) {
1954 		(void) fprintf(stderr, gettext("missing dataset name\n"));
1955 		usage(B_FALSE);
1956 	}
1957 
1958 	/* validate property=value argument */
1959 	cb.cb_propname = argv[1];
1960 	if ((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) {
1961 		(void) fprintf(stderr, gettext("missing value in "
1962 		    "property=value argument\n"));
1963 		usage(B_FALSE);
1964 	}
1965 
1966 	*cb.cb_value = '\0';
1967 	cb.cb_value++;
1968 	cb.cb_any_successful = B_FALSE;
1969 
1970 	if (*cb.cb_propname == '\0') {
1971 		(void) fprintf(stderr,
1972 		    gettext("missing property in property=value argument\n"));
1973 		usage(B_FALSE);
1974 	}
1975 
1976 	ret = zfs_for_each(argc - 2, argv + 2, B_FALSE,
1977 	    ZFS_TYPE_ANY, NULL, NULL, set_callback, &cb);
1978 
1979 	if (cb.cb_any_successful) {
1980 		*(cb.cb_value - 1) = '=';
1981 		zpool_log_history(g_zfs, argc, argv, argv[2], B_FALSE, B_FALSE);
1982 	}
1983 
1984 	return (ret);
1985 }
1986 
1987 /*
1988  * zfs snapshot [-r] <fs@snap>
1989  *
1990  * Creates a snapshot with the given name.  While functionally equivalent to
1991  * 'zfs create', it is a separate command to diffferentiate intent.
1992  */
1993 static int
1994 zfs_do_snapshot(int argc, char **argv)
1995 {
1996 	int recursive = B_FALSE;
1997 	int ret;
1998 	char c;
1999 
2000 	/* check options */
2001 	while ((c = getopt(argc, argv, ":r")) != -1) {
2002 		switch (c) {
2003 		case 'r':
2004 			recursive = B_TRUE;
2005 			break;
2006 		case '?':
2007 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2008 			    optopt);
2009 			usage(B_FALSE);
2010 		}
2011 	}
2012 
2013 	argc -= optind;
2014 	argv += optind;
2015 
2016 	/* check number of arguments */
2017 	if (argc < 1) {
2018 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
2019 		usage(B_FALSE);
2020 	}
2021 	if (argc > 1) {
2022 		(void) fprintf(stderr, gettext("too many arguments\n"));
2023 		usage(B_FALSE);
2024 	}
2025 
2026 	ret = zfs_snapshot(g_zfs, argv[0], recursive);
2027 	if (ret && recursive)
2028 		(void) fprintf(stderr, gettext("no snapshots were created\n"));
2029 	if (!ret) {
2030 		zpool_log_history(g_zfs, argc + optind, argv - optind, argv[0],
2031 		    B_FALSE, B_FALSE);
2032 	}
2033 	return (ret != 0);
2034 }
2035 
2036 /*
2037  * zfs send [-i <@snap>] <fs@snap>
2038  *
2039  * Send a backup stream to stdout.
2040  */
2041 static int
2042 zfs_do_send(int argc, char **argv)
2043 {
2044 	char *fromname = NULL;
2045 	char *cp;
2046 	zfs_handle_t *zhp;
2047 	int c, err;
2048 
2049 	/* check options */
2050 	while ((c = getopt(argc, argv, ":i:")) != -1) {
2051 		switch (c) {
2052 		case 'i':
2053 			if (fromname)
2054 				usage(B_FALSE);
2055 			fromname = optarg;
2056 			break;
2057 		case ':':
2058 			(void) fprintf(stderr, gettext("missing argument for "
2059 			    "'%c' option\n"), optopt);
2060 			usage(B_FALSE);
2061 			break;
2062 		case '?':
2063 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2064 			    optopt);
2065 			usage(B_FALSE);
2066 		}
2067 	}
2068 
2069 	argc -= optind;
2070 	argv += optind;
2071 
2072 	/* check number of arguments */
2073 	if (argc < 1) {
2074 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
2075 		usage(B_FALSE);
2076 	}
2077 	if (argc > 1) {
2078 		(void) fprintf(stderr, gettext("too many arguments\n"));
2079 		usage(B_FALSE);
2080 	}
2081 
2082 	if (isatty(STDOUT_FILENO)) {
2083 		(void) fprintf(stderr,
2084 		    gettext("Error: Stream can not be written to a terminal.\n"
2085 			    "You must redirect standard output.\n"));
2086 		return (1);
2087 	}
2088 
2089 	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
2090 		return (1);
2091 
2092 	/*
2093 	 * If they specified the full path to the snapshot, chop off
2094 	 * everything except the short name of the snapshot.
2095 	 */
2096 	if (fromname && (cp = strchr(fromname, '@')) != NULL) {
2097 		if (cp != fromname &&
2098 		    strncmp(argv[0], fromname, cp - fromname + 1)) {
2099 			(void) fprintf(stderr,
2100 			    gettext("incremental source must be "
2101 			    "in same filesystem\n"));
2102 			usage(B_FALSE);
2103 		}
2104 		fromname = cp + 1;
2105 		if (strchr(fromname, '@') || strchr(fromname, '/')) {
2106 			(void) fprintf(stderr,
2107 			    gettext("invalid incremental source\n"));
2108 			usage(B_FALSE);
2109 		}
2110 	}
2111 
2112 	err = zfs_send(zhp, fromname);
2113 	zfs_close(zhp);
2114 
2115 	return (err != 0);
2116 }
2117 
2118 /*
2119  * zfs receive <fs@snap>
2120  *
2121  * Restore a backup stream from stdin.
2122  */
2123 static int
2124 zfs_do_receive(int argc, char **argv)
2125 {
2126 	int c, err;
2127 	boolean_t isprefix = B_FALSE;
2128 	boolean_t dryrun = B_FALSE;
2129 	boolean_t verbose = B_FALSE;
2130 	boolean_t force = B_FALSE;
2131 
2132 	/* check options */
2133 	while ((c = getopt(argc, argv, ":dnvF")) != -1) {
2134 		switch (c) {
2135 		case 'd':
2136 			isprefix = B_TRUE;
2137 			break;
2138 		case 'n':
2139 			dryrun = B_TRUE;
2140 			break;
2141 		case 'v':
2142 			verbose = B_TRUE;
2143 			break;
2144 		case 'F':
2145 			force = B_TRUE;
2146 			break;
2147 		case ':':
2148 			(void) fprintf(stderr, gettext("missing argument for "
2149 			    "'%c' option\n"), optopt);
2150 			usage(B_FALSE);
2151 			break;
2152 		case '?':
2153 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2154 			    optopt);
2155 			usage(B_FALSE);
2156 		}
2157 	}
2158 
2159 	argc -= optind;
2160 	argv += optind;
2161 
2162 	/* check number of arguments */
2163 	if (argc < 1) {
2164 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
2165 		usage(B_FALSE);
2166 	}
2167 	if (argc > 1) {
2168 		(void) fprintf(stderr, gettext("too many arguments\n"));
2169 		usage(B_FALSE);
2170 	}
2171 
2172 	if (isatty(STDIN_FILENO)) {
2173 		(void) fprintf(stderr,
2174 		    gettext("Error: Backup stream can not be read "
2175 			    "from a terminal.\n"
2176 			    "You must redirect standard input.\n"));
2177 		return (1);
2178 	}
2179 
2180 	err = zfs_receive(g_zfs, argv[0], isprefix, verbose, dryrun, force);
2181 
2182 	if (!err) {
2183 		zpool_log_history(g_zfs, argc + optind, argv - optind, argv[0],
2184 		    B_FALSE, B_FALSE);
2185 	}
2186 
2187 	return (err != 0);
2188 }
2189 
2190 typedef struct get_all_cbdata {
2191 	zfs_handle_t	**cb_handles;
2192 	size_t		cb_alloc;
2193 	size_t		cb_used;
2194 	uint_t		cb_types;
2195 } get_all_cbdata_t;
2196 
2197 static int
2198 get_one_dataset(zfs_handle_t *zhp, void *data)
2199 {
2200 	get_all_cbdata_t *cbp = data;
2201 	zfs_type_t type = zfs_get_type(zhp);
2202 
2203 	/*
2204 	 * Interate over any nested datasets.
2205 	 */
2206 	if (type == ZFS_TYPE_FILESYSTEM &&
2207 	    zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
2208 		return (1);
2209 	}
2210 
2211 	/*
2212 	 * Skip any datasets whose type does not match.
2213 	 */
2214 	if ((type & cbp->cb_types) == 0) {
2215 		zfs_close(zhp);
2216 		return (0);
2217 	}
2218 
2219 	if (cbp->cb_alloc == cbp->cb_used) {
2220 		zfs_handle_t **handles;
2221 
2222 		if (cbp->cb_alloc == 0)
2223 			cbp->cb_alloc = 64;
2224 		else
2225 			cbp->cb_alloc *= 2;
2226 
2227 		handles = safe_malloc(cbp->cb_alloc * sizeof (void *));
2228 
2229 		if (cbp->cb_handles) {
2230 			bcopy(cbp->cb_handles, handles,
2231 			    cbp->cb_used * sizeof (void *));
2232 			free(cbp->cb_handles);
2233 		}
2234 
2235 		cbp->cb_handles = handles;
2236 	}
2237 
2238 	cbp->cb_handles[cbp->cb_used++] = zhp;
2239 
2240 	return (0);
2241 }
2242 
2243 static void
2244 get_all_datasets(uint_t types, zfs_handle_t ***dslist, size_t *count)
2245 {
2246 	get_all_cbdata_t cb = { 0 };
2247 	cb.cb_types = types;
2248 
2249 	(void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
2250 
2251 	*dslist = cb.cb_handles;
2252 	*count = cb.cb_used;
2253 }
2254 
2255 static int
2256 dataset_cmp(const void *a, const void *b)
2257 {
2258 	zfs_handle_t **za = (zfs_handle_t **)a;
2259 	zfs_handle_t **zb = (zfs_handle_t **)b;
2260 	char mounta[MAXPATHLEN];
2261 	char mountb[MAXPATHLEN];
2262 	boolean_t gota, gotb;
2263 
2264 	if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
2265 		verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
2266 		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
2267 	if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
2268 		verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
2269 		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
2270 
2271 	if (gota && gotb)
2272 		return (strcmp(mounta, mountb));
2273 
2274 	if (gota)
2275 		return (-1);
2276 	if (gotb)
2277 		return (1);
2278 
2279 	return (strcmp(zfs_get_name(a), zfs_get_name(b)));
2280 }
2281 
2282 /*
2283  * Generic callback for sharing or mounting filesystems.  Because the code is so
2284  * similar, we have a common function with an extra parameter to determine which
2285  * mode we are using.
2286  */
2287 #define	OP_SHARE	0x1
2288 #define	OP_MOUNT	0x2
2289 
2290 /*
2291  * Share or mount a dataset.
2292  */
2293 static int
2294 share_mount_one(zfs_handle_t *zhp, int op, int flags, boolean_t explicit,
2295     const char *options)
2296 {
2297 	char mountpoint[ZFS_MAXPROPLEN];
2298 	char shareopts[ZFS_MAXPROPLEN];
2299 	const char *cmdname = op == OP_SHARE ? "share" : "mount";
2300 	struct mnttab mnt;
2301 	uint64_t zoned, canmount;
2302 	zfs_type_t type = zfs_get_type(zhp);
2303 
2304 	assert(type & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME));
2305 
2306 	if (type == ZFS_TYPE_FILESYSTEM) {
2307 		/*
2308 		 * Check to make sure we can mount/share this dataset.  If we
2309 		 * are in the global zone and the filesystem is exported to a
2310 		 * local zone, or if we are in a local zone and the
2311 		 * filesystem is not exported, then it is an error.
2312 		 */
2313 		zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2314 
2315 		if (zoned && getzoneid() == GLOBAL_ZONEID) {
2316 			if (!explicit)
2317 				return (0);
2318 
2319 			(void) fprintf(stderr, gettext("cannot %s '%s': "
2320 			    "dataset is exported to a local zone\n"), cmdname,
2321 			    zfs_get_name(zhp));
2322 			return (1);
2323 
2324 		} else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
2325 			if (!explicit)
2326 				return (0);
2327 
2328 			(void) fprintf(stderr, gettext("cannot %s '%s': "
2329 			    "permission denied\n"), cmdname,
2330 			    zfs_get_name(zhp));
2331 			return (1);
2332 		}
2333 
2334 		/*
2335 		 * Ignore any filesystems which don't apply to us. This
2336 		 * includes those with a legacy mountpoint, or those with
2337 		 * legacy share options.
2338 		 */
2339 		verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
2340 		    sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
2341 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
2342 		    sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
2343 		canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
2344 
2345 		if (op == OP_SHARE && strcmp(shareopts, "off") == 0) {
2346 			if (!explicit)
2347 				return (0);
2348 
2349 			(void) fprintf(stderr, gettext("cannot share '%s': "
2350 			    "legacy share\n"), zfs_get_name(zhp));
2351 			(void) fprintf(stderr, gettext("use share(1M) to "
2352 			    "share this filesystem\n"));
2353 			return (1);
2354 		}
2355 
2356 		/*
2357 		 * We cannot share or mount legacy filesystems. If the
2358 		 * shareopts is non-legacy but the mountpoint is legacy, we
2359 		 * treat it as a legacy share.
2360 		 */
2361 		if (strcmp(mountpoint, "legacy") == 0) {
2362 			if (!explicit)
2363 				return (0);
2364 
2365 			(void) fprintf(stderr, gettext("cannot %s '%s': "
2366 			    "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
2367 			(void) fprintf(stderr, gettext("use %s to "
2368 			    "%s this filesystem\n"), op == OP_SHARE ?
2369 			    "share(1M)" : "mount(1M)", cmdname);
2370 			return (1);
2371 		}
2372 
2373 		if (strcmp(mountpoint, "none") == 0) {
2374 			if (!explicit)
2375 				return (0);
2376 
2377 			(void) fprintf(stderr, gettext("cannot %s '%s': no "
2378 			    "mountpoint set\n"), cmdname, zfs_get_name(zhp));
2379 			return (1);
2380 		}
2381 
2382 		if (!canmount) {
2383 			if (!explicit)
2384 				return (0);
2385 
2386 			(void) fprintf(stderr, gettext("cannot %s '%s': "
2387 			    "'canmount' property is set to 'off'\n"), cmdname,
2388 			    zfs_get_name(zhp));
2389 			return (1);
2390 		}
2391 
2392 		/*
2393 		 * At this point, we have verified that the mountpoint and/or
2394 		 * shareopts are appropriate for auto management. If the
2395 		 * filesystem is already mounted or shared, return (failing
2396 		 * for explicit requests); otherwise mount or share the
2397 		 * filesystem.
2398 		 */
2399 		switch (op) {
2400 		case OP_SHARE:
2401 			if (zfs_is_shared_nfs(zhp, NULL)) {
2402 				if (!explicit)
2403 					return (0);
2404 
2405 				(void) fprintf(stderr, gettext("cannot share "
2406 				    "'%s': filesystem already shared\n"),
2407 				    zfs_get_name(zhp));
2408 				return (1);
2409 			}
2410 
2411 			if (!zfs_is_mounted(zhp, NULL) &&
2412 			    zfs_mount(zhp, NULL, 0) != 0)
2413 				return (1);
2414 
2415 			if (zfs_share_nfs(zhp) != 0)
2416 				return (1);
2417 			break;
2418 
2419 		case OP_MOUNT:
2420 			if (options == NULL)
2421 				mnt.mnt_mntopts = "";
2422 			else
2423 				mnt.mnt_mntopts = (char *)options;
2424 
2425 			if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
2426 			    zfs_is_mounted(zhp, NULL)) {
2427 				if (!explicit)
2428 					return (0);
2429 
2430 				(void) fprintf(stderr, gettext("cannot mount "
2431 				    "'%s': filesystem already mounted\n"),
2432 				    zfs_get_name(zhp));
2433 				return (1);
2434 			}
2435 
2436 			if (zfs_mount(zhp, options, flags) != 0)
2437 				return (1);
2438 			break;
2439 		}
2440 	} else {
2441 		assert(op == OP_SHARE);
2442 
2443 		/*
2444 		 * Ignore any volumes that aren't shared.
2445 		 */
2446 		verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
2447 		    sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
2448 
2449 		if (strcmp(shareopts, "off") == 0) {
2450 			if (!explicit)
2451 				return (0);
2452 
2453 			(void) fprintf(stderr, gettext("cannot share '%s': "
2454 			    "'shareiscsi' property not set\n"),
2455 			    zfs_get_name(zhp));
2456 			(void) fprintf(stderr, gettext("set 'shareiscsi' "
2457 			    "property or use iscsitadm(1M) to share this "
2458 			    "volume\n"));
2459 			return (1);
2460 		}
2461 
2462 		if (zfs_is_shared_iscsi(zhp)) {
2463 			if (!explicit)
2464 				return (0);
2465 
2466 			(void) fprintf(stderr, gettext("cannot share "
2467 			    "'%s': volume already shared\n"),
2468 			    zfs_get_name(zhp));
2469 			return (1);
2470 		}
2471 
2472 		if (zfs_share_iscsi(zhp) != 0)
2473 			return (1);
2474 	}
2475 
2476 	return (0);
2477 }
2478 
2479 static int
2480 share_mount(int op, int argc, char **argv)
2481 {
2482 	int do_all = 0;
2483 	int c, ret = 0;
2484 	const char *options = NULL;
2485 	int types, flags = 0;
2486 
2487 	/* check options */
2488 	while ((c = getopt(argc, argv, op == OP_MOUNT ? ":ao:O" : "a"))
2489 	    != -1) {
2490 		switch (c) {
2491 		case 'a':
2492 			do_all = 1;
2493 			break;
2494 		case 'o':
2495 			options = optarg;
2496 			break;
2497 		case 'O':
2498 			flags |= MS_OVERLAY;
2499 			break;
2500 		case ':':
2501 			(void) fprintf(stderr, gettext("missing argument for "
2502 			    "'%c' option\n"), optopt);
2503 			usage(B_FALSE);
2504 			break;
2505 		case '?':
2506 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2507 			    optopt);
2508 			usage(B_FALSE);
2509 		}
2510 	}
2511 
2512 	argc -= optind;
2513 	argv += optind;
2514 
2515 	/* check number of arguments */
2516 	if (do_all) {
2517 		zfs_handle_t **dslist = NULL;
2518 		size_t i, count = 0;
2519 
2520 		if (op == OP_MOUNT) {
2521 			types = ZFS_TYPE_FILESYSTEM;
2522 		} else if (argc > 0) {
2523 			if (strcmp(argv[0], "nfs") == 0) {
2524 				types = ZFS_TYPE_FILESYSTEM;
2525 			} else if (strcmp(argv[0], "iscsi") == 0) {
2526 				types = ZFS_TYPE_VOLUME;
2527 			} else {
2528 				(void) fprintf(stderr, gettext("share type "
2529 				    "must be 'nfs' or 'iscsi'\n"));
2530 				usage(B_FALSE);
2531 			}
2532 
2533 			argc--;
2534 			argv++;
2535 		} else {
2536 			types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
2537 		}
2538 
2539 		if (argc != 0) {
2540 			(void) fprintf(stderr, gettext("too many arguments\n"));
2541 			usage(B_FALSE);
2542 		}
2543 
2544 		get_all_datasets(types, &dslist, &count);
2545 
2546 		if (count == 0)
2547 			return (0);
2548 
2549 		qsort(dslist, count, sizeof (void *), dataset_cmp);
2550 
2551 		for (i = 0; i < count; i++) {
2552 			if (share_mount_one(dslist[i], op, flags, B_FALSE,
2553 			    options) != 0)
2554 				ret = 1;
2555 			zfs_close(dslist[i]);
2556 		}
2557 
2558 		free(dslist);
2559 	} else if (argc == 0) {
2560 		struct mnttab entry;
2561 
2562 		if (op == OP_SHARE) {
2563 			(void) fprintf(stderr, gettext("missing filesystem "
2564 			    "argument\n"));
2565 			usage(B_FALSE);
2566 		}
2567 
2568 		/*
2569 		 * When mount is given no arguments, go through /etc/mnttab and
2570 		 * display any active ZFS mounts.  We hide any snapshots, since
2571 		 * they are controlled automatically.
2572 		 */
2573 		rewind(mnttab_file);
2574 		while (getmntent(mnttab_file, &entry) == 0) {
2575 			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
2576 			    strchr(entry.mnt_special, '@') != NULL)
2577 				continue;
2578 
2579 			(void) printf("%-30s  %s\n", entry.mnt_special,
2580 			    entry.mnt_mountp);
2581 		}
2582 
2583 	} else {
2584 		zfs_handle_t *zhp;
2585 
2586 		types = ZFS_TYPE_FILESYSTEM;
2587 		if (op == OP_SHARE)
2588 			types |= ZFS_TYPE_VOLUME;
2589 
2590 		if (argc > 1) {
2591 			(void) fprintf(stderr,
2592 			    gettext("too many arguments\n"));
2593 			usage(B_FALSE);
2594 		}
2595 
2596 		if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) {
2597 			ret = 1;
2598 		} else {
2599 			ret = share_mount_one(zhp, op, flags, B_TRUE,
2600 			    options);
2601 			zfs_close(zhp);
2602 		}
2603 	}
2604 
2605 	return (ret);
2606 }
2607 
2608 /*
2609  * zfs mount -a [nfs | iscsi]
2610  * zfs mount filesystem
2611  *
2612  * Mount all filesystems, or mount the given filesystem.
2613  */
2614 static int
2615 zfs_do_mount(int argc, char **argv)
2616 {
2617 	return (share_mount(OP_MOUNT, argc, argv));
2618 }
2619 
2620 /*
2621  * zfs share -a [nfs | iscsi]
2622  * zfs share filesystem
2623  *
2624  * Share all filesystems, or share the given filesystem.
2625  */
2626 static int
2627 zfs_do_share(int argc, char **argv)
2628 {
2629 	return (share_mount(OP_SHARE, argc, argv));
2630 }
2631 
2632 typedef struct unshare_unmount_node {
2633 	zfs_handle_t	*un_zhp;
2634 	char		*un_mountp;
2635 	uu_avl_node_t	un_avlnode;
2636 } unshare_unmount_node_t;
2637 
2638 /* ARGSUSED */
2639 static int
2640 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
2641 {
2642 	const unshare_unmount_node_t *l = larg;
2643 	const unshare_unmount_node_t *r = rarg;
2644 
2645 	return (strcmp(l->un_mountp, r->un_mountp));
2646 }
2647 
2648 /*
2649  * Convenience routine used by zfs_do_umount() and manual_unmount().  Given an
2650  * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
2651  * and unmount it appropriately.
2652  */
2653 static int
2654 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
2655 {
2656 	zfs_handle_t *zhp;
2657 	int ret;
2658 	struct stat64 statbuf;
2659 	struct extmnttab entry;
2660 	const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
2661 	char property[ZFS_MAXPROPLEN];
2662 
2663 	/*
2664 	 * Search for the path in /etc/mnttab.  Rather than looking for the
2665 	 * specific path, which can be fooled by non-standard paths (i.e. ".."
2666 	 * or "//"), we stat() the path and search for the corresponding
2667 	 * (major,minor) device pair.
2668 	 */
2669 	if (stat64(path, &statbuf) != 0) {
2670 		(void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
2671 		    cmdname, path, strerror(errno));
2672 		return (1);
2673 	}
2674 
2675 	/*
2676 	 * Search for the given (major,minor) pair in the mount table.
2677 	 */
2678 	rewind(mnttab_file);
2679 	while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
2680 		if (entry.mnt_major == major(statbuf.st_dev) &&
2681 		    entry.mnt_minor == minor(statbuf.st_dev))
2682 			break;
2683 	}
2684 	if (ret != 0) {
2685 		(void) fprintf(stderr, gettext("cannot %s '%s': not "
2686 		    "currently mounted\n"), cmdname, path);
2687 		return (1);
2688 	}
2689 
2690 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
2691 		(void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
2692 		    "filesystem\n"), cmdname, path);
2693 		return (1);
2694 	}
2695 
2696 	if ((zhp = zfs_open(g_zfs, entry.mnt_special,
2697 	    ZFS_TYPE_FILESYSTEM)) == NULL)
2698 		return (1);
2699 
2700 	verify(zfs_prop_get(zhp, op == OP_SHARE ?
2701 		ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, property,
2702 		sizeof (property), NULL, NULL, 0, B_FALSE) == 0);
2703 
2704 	if (op == OP_SHARE) {
2705 		if (strcmp(property, "off") == 0) {
2706 			(void) fprintf(stderr, gettext("cannot unshare "
2707 			    "'%s': legacy share\n"), path);
2708 			(void) fprintf(stderr, gettext("use "
2709 			    "unshare(1M) to unshare this filesystem\n"));
2710 			ret = 1;
2711 		} else if (!zfs_is_shared_nfs(zhp, NULL)) {
2712 			(void) fprintf(stderr, gettext("cannot unshare '%s': "
2713 			    "not currently shared\n"), path);
2714 			ret = 1;
2715 		} else {
2716 			ret = zfs_unshareall_nfs(zhp);
2717 		}
2718 	} else {
2719 		if (is_manual) {
2720 			ret = zfs_unmount(zhp, NULL, flags);
2721 		} else if (strcmp(property, "legacy") == 0) {
2722 			(void) fprintf(stderr, gettext("cannot unmount "
2723 			    "'%s': legacy mountpoint\n"),
2724 			    zfs_get_name(zhp));
2725 			(void) fprintf(stderr, gettext("use umount(1M) "
2726 			    "to unmount this filesystem\n"));
2727 			ret = 1;
2728 		} else {
2729 			ret = zfs_unmountall(zhp, flags);
2730 		}
2731 	}
2732 
2733 	zfs_close(zhp);
2734 
2735 	return (ret != 0);
2736 }
2737 
2738 /*
2739  * Generic callback for unsharing or unmounting a filesystem.
2740  */
2741 static int
2742 unshare_unmount(int op, int argc, char **argv)
2743 {
2744 	int do_all = 0;
2745 	int flags = 0;
2746 	int ret = 0;
2747 	int types, c;
2748 	zfs_handle_t *zhp;
2749 	char property[ZFS_MAXPROPLEN];
2750 
2751 	/* check options */
2752 	while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
2753 		switch (c) {
2754 		case 'a':
2755 			do_all = 1;
2756 			break;
2757 		case 'f':
2758 			flags = MS_FORCE;
2759 			break;
2760 		case '?':
2761 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2762 			    optopt);
2763 			usage(B_FALSE);
2764 		}
2765 	}
2766 
2767 	argc -= optind;
2768 	argv += optind;
2769 
2770 	if (do_all) {
2771 		/*
2772 		 * We could make use of zfs_for_each() to walk all datasets in
2773 		 * the system, but this would be very inefficient, especially
2774 		 * since we would have to linearly search /etc/mnttab for each
2775 		 * one.  Instead, do one pass through /etc/mnttab looking for
2776 		 * zfs entries and call zfs_unmount() for each one.
2777 		 *
2778 		 * Things get a little tricky if the administrator has created
2779 		 * mountpoints beneath other ZFS filesystems.  In this case, we
2780 		 * have to unmount the deepest filesystems first.  To accomplish
2781 		 * this, we place all the mountpoints in an AVL tree sorted by
2782 		 * the special type (dataset name), and walk the result in
2783 		 * reverse to make sure to get any snapshots first.
2784 		 */
2785 		struct mnttab entry;
2786 		uu_avl_pool_t *pool;
2787 		uu_avl_t *tree;
2788 		unshare_unmount_node_t *node;
2789 		uu_avl_index_t idx;
2790 		uu_avl_walk_t *walk;
2791 
2792 		if (argc != 0) {
2793 			(void) fprintf(stderr, gettext("too many arguments\n"));
2794 			usage(B_FALSE);
2795 		}
2796 
2797 		if ((pool = uu_avl_pool_create("unmount_pool",
2798 		    sizeof (unshare_unmount_node_t),
2799 		    offsetof(unshare_unmount_node_t, un_avlnode),
2800 		    unshare_unmount_compare,
2801 		    UU_DEFAULT)) == NULL) {
2802 			(void) fprintf(stderr, gettext("internal error: "
2803 			    "out of memory\n"));
2804 			exit(1);
2805 		}
2806 
2807 		if ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL) {
2808 			(void) fprintf(stderr, gettext("internal error: "
2809 			    "out of memory\n"));
2810 			exit(1);
2811 		}
2812 
2813 		rewind(mnttab_file);
2814 		while (getmntent(mnttab_file, &entry) == 0) {
2815 
2816 			/* ignore non-ZFS entries */
2817 			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
2818 				continue;
2819 
2820 			/* ignore snapshots */
2821 			if (strchr(entry.mnt_special, '@') != NULL)
2822 				continue;
2823 
2824 			if ((zhp = zfs_open(g_zfs, entry.mnt_special,
2825 			    ZFS_TYPE_FILESYSTEM)) == NULL) {
2826 				ret = 1;
2827 				continue;
2828 			}
2829 
2830 			verify(zfs_prop_get(zhp, op == OP_SHARE ?
2831 			    ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
2832 			    property, sizeof (property), NULL, NULL,
2833 			    0, B_FALSE) == 0);
2834 
2835 			/* Ignore legacy mounts and shares */
2836 			if ((op == OP_SHARE &&
2837 			    strcmp(property, "off") == 0) ||
2838 			    (op == OP_MOUNT &&
2839 			    strcmp(property, "legacy") == 0)) {
2840 				zfs_close(zhp);
2841 				continue;
2842 			}
2843 
2844 			node = safe_malloc(sizeof (unshare_unmount_node_t));
2845 			node->un_zhp = zhp;
2846 
2847 			if ((node->un_mountp = strdup(entry.mnt_mountp)) ==
2848 			    NULL) {
2849 				(void) fprintf(stderr, gettext("internal error:"
2850 				    " out of memory\n"));
2851 				exit(1);
2852 			}
2853 
2854 			uu_avl_node_init(node, &node->un_avlnode, pool);
2855 
2856 			if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
2857 				uu_avl_insert(tree, node, idx);
2858 			} else {
2859 				zfs_close(node->un_zhp);
2860 				free(node->un_mountp);
2861 				free(node);
2862 			}
2863 		}
2864 
2865 		/*
2866 		 * Walk the AVL tree in reverse, unmounting each filesystem and
2867 		 * removing it from the AVL tree in the process.
2868 		 */
2869 		if ((walk = uu_avl_walk_start(tree,
2870 		    UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) {
2871 			(void) fprintf(stderr,
2872 			    gettext("internal error: out of memory"));
2873 			exit(1);
2874 		}
2875 
2876 		while ((node = uu_avl_walk_next(walk)) != NULL) {
2877 			uu_avl_remove(tree, node);
2878 
2879 			switch (op) {
2880 			case OP_SHARE:
2881 				if (zfs_unshare_nfs(node->un_zhp,
2882 				    node->un_mountp) != 0)
2883 					ret = 1;
2884 				break;
2885 
2886 			case OP_MOUNT:
2887 				if (zfs_unmount(node->un_zhp,
2888 				    node->un_mountp, flags) != 0)
2889 					ret = 1;
2890 				break;
2891 			}
2892 
2893 			zfs_close(node->un_zhp);
2894 			free(node->un_mountp);
2895 			free(node);
2896 		}
2897 
2898 		uu_avl_walk_end(walk);
2899 		uu_avl_destroy(tree);
2900 		uu_avl_pool_destroy(pool);
2901 
2902 		if (op == OP_SHARE) {
2903 			/*
2904 			 * Finally, unshare any volumes shared via iSCSI.
2905 			 */
2906 			zfs_handle_t **dslist = NULL;
2907 			size_t i, count = 0;
2908 
2909 			get_all_datasets(ZFS_TYPE_VOLUME, &dslist, &count);
2910 
2911 			if (count != 0) {
2912 				qsort(dslist, count, sizeof (void *),
2913 				    dataset_cmp);
2914 
2915 				for (i = 0; i < count; i++) {
2916 					if (zfs_unshare_iscsi(dslist[i]) != 0)
2917 						ret = 1;
2918 					zfs_close(dslist[i]);
2919 				}
2920 
2921 				free(dslist);
2922 			}
2923 		}
2924 	} else {
2925 		if (argc != 1) {
2926 			if (argc == 0)
2927 				(void) fprintf(stderr,
2928 				    gettext("missing filesystem argument\n"));
2929 			else
2930 				(void) fprintf(stderr,
2931 				    gettext("too many arguments\n"));
2932 			usage(B_FALSE);
2933 		}
2934 
2935 		/*
2936 		 * We have an argument, but it may be a full path or a ZFS
2937 		 * filesystem.  Pass full paths off to unmount_path() (shared by
2938 		 * manual_unmount), otherwise open the filesystem and pass to
2939 		 * zfs_unmount().
2940 		 */
2941 		if (argv[0][0] == '/')
2942 			return (unshare_unmount_path(op, argv[0],
2943 				flags, B_FALSE));
2944 
2945 		types = ZFS_TYPE_FILESYSTEM;
2946 		if (op == OP_SHARE)
2947 			types |= ZFS_TYPE_VOLUME;
2948 
2949 		if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
2950 			return (1);
2951 
2952 		if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
2953 			verify(zfs_prop_get(zhp, op == OP_SHARE ?
2954 			    ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, property,
2955 			    sizeof (property), NULL, NULL, 0, B_FALSE) == 0);
2956 
2957 			switch (op) {
2958 			case OP_SHARE:
2959 				if (strcmp(property, "off") == 0) {
2960 					(void) fprintf(stderr, gettext("cannot "
2961 					    "unshare '%s': legacy share\n"),
2962 					    zfs_get_name(zhp));
2963 					(void) fprintf(stderr, gettext("use "
2964 					    "unshare(1M) to unshare this "
2965 					    "filesystem\n"));
2966 					ret = 1;
2967 				} else if (!zfs_is_shared_nfs(zhp, NULL)) {
2968 					(void) fprintf(stderr, gettext("cannot "
2969 					    "unshare '%s': not currently "
2970 					    "shared\n"), zfs_get_name(zhp));
2971 					ret = 1;
2972 				} else if (zfs_unshareall_nfs(zhp) != 0) {
2973 					ret = 1;
2974 				}
2975 				break;
2976 
2977 			case OP_MOUNT:
2978 				if (strcmp(property, "legacy") == 0) {
2979 					(void) fprintf(stderr, gettext("cannot "
2980 					    "unmount '%s': legacy "
2981 					    "mountpoint\n"), zfs_get_name(zhp));
2982 					(void) fprintf(stderr, gettext("use "
2983 					    "umount(1M) to unmount this "
2984 					    "filesystem\n"));
2985 					ret = 1;
2986 				} else if (!zfs_is_mounted(zhp, NULL)) {
2987 					(void) fprintf(stderr, gettext("cannot "
2988 					    "unmount '%s': not currently "
2989 					    "mounted\n"),
2990 					    zfs_get_name(zhp));
2991 					ret = 1;
2992 				} else if (zfs_unmountall(zhp, flags) != 0) {
2993 					ret = 1;
2994 				}
2995 				break;
2996 			}
2997 		} else {
2998 			assert(op == OP_SHARE);
2999 
3000 			verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, property,
3001 			    sizeof (property), NULL, NULL, 0, B_FALSE) == 0);
3002 
3003 			if (strcmp(property, "off") == 0) {
3004 				(void) fprintf(stderr, gettext("cannot unshare "
3005 				    "'%s': 'shareiscsi' property not set\n"),
3006 				    zfs_get_name(zhp));
3007 				(void) fprintf(stderr, gettext("set "
3008 				    "'shareiscsi' property or use "
3009 				    "iscsitadm(1M) to share this volume\n"));
3010 				ret = 1;
3011 			} else if (!zfs_is_shared_iscsi(zhp)) {
3012 				(void) fprintf(stderr, gettext("cannot "
3013 				    "unshare '%s': not currently shared\n"),
3014 				    zfs_get_name(zhp));
3015 				ret = 1;
3016 			} else if (zfs_unshare_iscsi(zhp) != 0) {
3017 				ret = 1;
3018 			}
3019 		}
3020 
3021 		zfs_close(zhp);
3022 	}
3023 
3024 	return (ret);
3025 }
3026 
3027 /*
3028  * zfs unmount -a
3029  * zfs unmount filesystem
3030  *
3031  * Unmount all filesystems, or a specific ZFS filesystem.
3032  */
3033 static int
3034 zfs_do_unmount(int argc, char **argv)
3035 {
3036 	return (unshare_unmount(OP_MOUNT, argc, argv));
3037 }
3038 
3039 /*
3040  * zfs unshare -a
3041  * zfs unshare filesystem
3042  *
3043  * Unshare all filesystems, or a specific ZFS filesystem.
3044  */
3045 static int
3046 zfs_do_unshare(int argc, char **argv)
3047 {
3048 	return (unshare_unmount(OP_SHARE, argc, argv));
3049 }
3050 
3051 /*
3052  * Called when invoked as /etc/fs/zfs/mount.  Do the mount if the mountpoint is
3053  * 'legacy'.  Otherwise, complain that use should be using 'zfs mount'.
3054  */
3055 static int
3056 manual_mount(int argc, char **argv)
3057 {
3058 	zfs_handle_t *zhp;
3059 	char mountpoint[ZFS_MAXPROPLEN];
3060 	char mntopts[MNT_LINE_MAX] = { '\0' };
3061 	int ret;
3062 	int c;
3063 	int flags = 0;
3064 	char *dataset, *path;
3065 
3066 	/* check options */
3067 	while ((c = getopt(argc, argv, ":mo:O")) != -1) {
3068 		switch (c) {
3069 		case 'o':
3070 			(void) strlcpy(mntopts, optarg, sizeof (mntopts));
3071 			break;
3072 		case 'O':
3073 			flags |= MS_OVERLAY;
3074 			break;
3075 		case 'm':
3076 			flags |= MS_NOMNTTAB;
3077 			break;
3078 		case ':':
3079 			(void) fprintf(stderr, gettext("missing argument for "
3080 			    "'%c' option\n"), optopt);
3081 			usage(B_FALSE);
3082 			break;
3083 		case '?':
3084 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3085 			    optopt);
3086 			(void) fprintf(stderr, gettext("usage: mount [-o opts] "
3087 			    "<path>\n"));
3088 			return (2);
3089 		}
3090 	}
3091 
3092 	argc -= optind;
3093 	argv += optind;
3094 
3095 	/* check that we only have two arguments */
3096 	if (argc != 2) {
3097 		if (argc == 0)
3098 			(void) fprintf(stderr, gettext("missing dataset "
3099 			    "argument\n"));
3100 		else if (argc == 1)
3101 			(void) fprintf(stderr,
3102 			    gettext("missing mountpoint argument\n"));
3103 		else
3104 			(void) fprintf(stderr, gettext("too many arguments\n"));
3105 		(void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
3106 		return (2);
3107 	}
3108 
3109 	dataset = argv[0];
3110 	path = argv[1];
3111 
3112 	/* try to open the dataset */
3113 	if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
3114 		return (1);
3115 
3116 	(void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
3117 	    sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
3118 
3119 	/* check for legacy mountpoint and complain appropriately */
3120 	ret = 0;
3121 	if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
3122 		if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS,
3123 		    NULL, 0, mntopts, sizeof (mntopts)) != 0) {
3124 			(void) fprintf(stderr, gettext("mount failed: %s\n"),
3125 			    strerror(errno));
3126 			ret = 1;
3127 		}
3128 	} else {
3129 		(void) fprintf(stderr, gettext("filesystem '%s' cannot be "
3130 		    "mounted using 'mount -F zfs'\n"), dataset);
3131 		(void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
3132 		    "instead.\n"), path);
3133 		(void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
3134 		    "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
3135 		(void) fprintf(stderr, gettext("See zfs(1M) for more "
3136 		    "information.\n"));
3137 		ret = 1;
3138 	}
3139 
3140 	return (ret);
3141 }
3142 
3143 /*
3144  * Called when invoked as /etc/fs/zfs/umount.  Unlike a manual mount, we allow
3145  * unmounts of non-legacy filesystems, as this is the dominant administrative
3146  * interface.
3147  */
3148 static int
3149 manual_unmount(int argc, char **argv)
3150 {
3151 	int flags = 0;
3152 	int c;
3153 
3154 	/* check options */
3155 	while ((c = getopt(argc, argv, "f")) != -1) {
3156 		switch (c) {
3157 		case 'f':
3158 			flags = MS_FORCE;
3159 			break;
3160 		case '?':
3161 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3162 			    optopt);
3163 			(void) fprintf(stderr, gettext("usage: unmount [-f] "
3164 			    "<path>\n"));
3165 			return (2);
3166 		}
3167 	}
3168 
3169 	argc -= optind;
3170 	argv += optind;
3171 
3172 	/* check arguments */
3173 	if (argc != 1) {
3174 		if (argc == 0)
3175 			(void) fprintf(stderr, gettext("missing path "
3176 			    "argument\n"));
3177 		else
3178 			(void) fprintf(stderr, gettext("too many arguments\n"));
3179 		(void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
3180 		return (2);
3181 	}
3182 
3183 	return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
3184 }
3185 
3186 static int
3187 volcheck(zpool_handle_t *zhp, void *data)
3188 {
3189 	boolean_t isinit = *((boolean_t *)data);
3190 
3191 	if (isinit)
3192 		return (zpool_create_zvol_links(zhp));
3193 	else
3194 		return (zpool_remove_zvol_links(zhp));
3195 }
3196 
3197 /*
3198  * Iterate over all pools in the system and either create or destroy /dev/zvol
3199  * links, depending on the value of 'isinit'.
3200  */
3201 static int
3202 do_volcheck(boolean_t isinit)
3203 {
3204 	return (zpool_iter(g_zfs, volcheck, &isinit) ? 1 : 0);
3205 }
3206 
3207 int
3208 main(int argc, char **argv)
3209 {
3210 	int ret;
3211 	int i;
3212 	char *progname;
3213 	char *cmdname;
3214 
3215 	(void) setlocale(LC_ALL, "");
3216 	(void) textdomain(TEXT_DOMAIN);
3217 
3218 	opterr = 0;
3219 
3220 	if ((g_zfs = libzfs_init()) == NULL) {
3221 		(void) fprintf(stderr, gettext("internal error: failed to "
3222 		    "initialize ZFS library\n"));
3223 		return (1);
3224 	}
3225 
3226 	libzfs_print_on_error(g_zfs, B_TRUE);
3227 
3228 	if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
3229 		(void) fprintf(stderr, gettext("internal error: unable to "
3230 		    "open %s\n"), MNTTAB);
3231 		return (1);
3232 	}
3233 
3234 	/*
3235 	 * This command also doubles as the /etc/fs mount and unmount program.
3236 	 * Determine if we should take this behavior based on argv[0].
3237 	 */
3238 	progname = basename(argv[0]);
3239 	if (strcmp(progname, "mount") == 0) {
3240 		ret = manual_mount(argc, argv);
3241 	} else if (strcmp(progname, "umount") == 0) {
3242 		ret = manual_unmount(argc, argv);
3243 	} else {
3244 		/*
3245 		 * Make sure the user has specified some command.
3246 		 */
3247 		if (argc < 2) {
3248 			(void) fprintf(stderr, gettext("missing command\n"));
3249 			usage(B_FALSE);
3250 		}
3251 
3252 		cmdname = argv[1];
3253 
3254 		/*
3255 		 * The 'umount' command is an alias for 'unmount'
3256 		 */
3257 		if (strcmp(cmdname, "umount") == 0)
3258 			cmdname = "unmount";
3259 
3260 		/*
3261 		 * The 'recv' command is an alias for 'receive'
3262 		 */
3263 		if (strcmp(cmdname, "recv") == 0)
3264 			cmdname = "receive";
3265 
3266 		/*
3267 		 * Special case '-?'
3268 		 */
3269 		if (strcmp(cmdname, "-?") == 0)
3270 			usage(B_TRUE);
3271 
3272 		/*
3273 		 * 'volinit' and 'volfini' do not appear in the usage message,
3274 		 * so we have to special case them here.
3275 		 */
3276 		if (strcmp(cmdname, "volinit") == 0)
3277 			return (do_volcheck(B_TRUE));
3278 		else if (strcmp(cmdname, "volfini") == 0)
3279 			return (do_volcheck(B_FALSE));
3280 
3281 		/*
3282 		 * Run the appropriate command.
3283 		 */
3284 		for (i = 0; i < NCOMMAND; i++) {
3285 			if (command_table[i].name == NULL)
3286 				continue;
3287 
3288 			if (strcmp(cmdname, command_table[i].name) == 0) {
3289 				current_command = &command_table[i];
3290 				ret = command_table[i].func(argc - 1, argv + 1);
3291 				break;
3292 			}
3293 		}
3294 
3295 		if (i == NCOMMAND) {
3296 			(void) fprintf(stderr, gettext("unrecognized "
3297 			    "command '%s'\n"), cmdname);
3298 			usage(B_FALSE);
3299 		}
3300 	}
3301 
3302 	(void) fclose(mnttab_file);
3303 
3304 	libzfs_fini(g_zfs);
3305 
3306 	/*
3307 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
3308 	 * for the purposes of running ::findleaks.
3309 	 */
3310 	if (getenv("ZFS_ABORT") != NULL) {
3311 		(void) printf("dumping core by request\n");
3312 		abort();
3313 	}
3314 
3315 	return (ret);
3316 }
3317