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