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