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