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