xref: /onnv-gate/usr/src/cmd/zfs/zfs_main.c (revision 13025:e0bc0b6c7c2f)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
213126Sahl 
22789Sahrens /*
2312296SLin.Ling@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <assert.h>
272676Seschrock #include <ctype.h>
28789Sahrens #include <errno.h>
29789Sahrens #include <libgen.h>
30789Sahrens #include <libintl.h>
31789Sahrens #include <libuutil.h>
324543Smarks #include <libnvpair.h>
33789Sahrens #include <locale.h>
34789Sahrens #include <stddef.h>
35789Sahrens #include <stdio.h>
36789Sahrens #include <stdlib.h>
37789Sahrens #include <strings.h>
38789Sahrens #include <unistd.h>
39789Sahrens #include <fcntl.h>
40789Sahrens #include <zone.h>
419396SMatthew.Ahrens@Sun.COM #include <grp.h>
429396SMatthew.Ahrens@Sun.COM #include <pwd.h>
43789Sahrens #include <sys/mkdev.h>
44789Sahrens #include <sys/mntent.h>
45789Sahrens #include <sys/mnttab.h>
46789Sahrens #include <sys/mount.h>
47789Sahrens #include <sys/stat.h>
489396SMatthew.Ahrens@Sun.COM #include <sys/fs/zfs.h>
4912446SMark.Musante@Sun.COM #include <sys/types.h>
5012446SMark.Musante@Sun.COM #include <time.h>
51789Sahrens 
52789Sahrens #include <libzfs.h>
534543Smarks #include <libuutil.h>
54789Sahrens 
55789Sahrens #include "zfs_iter.h"
562082Seschrock #include "zfs_util.h"
5711935SMark.Shellenbaum@Sun.COM #include "zfs_comutil.h"
582082Seschrock 
592082Seschrock libzfs_handle_t *g_zfs;
60789Sahrens 
61789Sahrens static FILE *mnttab_file;
624988Sek110237 static char history_str[HIS_MAX_RECORD_LEN];
639396SMatthew.Ahrens@Sun.COM const char *pypath = "/usr/lib/zfs/pyzfs.py";
64789Sahrens 
65789Sahrens static int zfs_do_clone(int argc, char **argv);
66789Sahrens static int zfs_do_create(int argc, char **argv);
67789Sahrens static int zfs_do_destroy(int argc, char **argv);
68789Sahrens static int zfs_do_get(int argc, char **argv);
69789Sahrens static int zfs_do_inherit(int argc, char **argv);
70789Sahrens static int zfs_do_list(int argc, char **argv);
71789Sahrens static int zfs_do_mount(int argc, char **argv);
72789Sahrens static int zfs_do_rename(int argc, char **argv);
73789Sahrens static int zfs_do_rollback(int argc, char **argv);
74789Sahrens static int zfs_do_set(int argc, char **argv);
754577Sahrens static int zfs_do_upgrade(int argc, char **argv);
76789Sahrens static int zfs_do_snapshot(int argc, char **argv);
77789Sahrens static int zfs_do_unmount(int argc, char **argv);
78789Sahrens static int zfs_do_share(int argc, char **argv);
79789Sahrens static int zfs_do_unshare(int argc, char **argv);
801749Sahrens static int zfs_do_send(int argc, char **argv);
811749Sahrens static int zfs_do_receive(int argc, char **argv);
822082Seschrock static int zfs_do_promote(int argc, char **argv);
839396SMatthew.Ahrens@Sun.COM static int zfs_do_userspace(int argc, char **argv);
849396SMatthew.Ahrens@Sun.COM static int zfs_do_python(int argc, char **argv);
8510242Schris.kirby@sun.com static int zfs_do_hold(int argc, char **argv);
8610242Schris.kirby@sun.com static int zfs_do_release(int argc, char **argv);
87789Sahrens 
88789Sahrens /*
896865Srm160521  * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
90789Sahrens  */
916865Srm160521 
926865Srm160521 #ifdef DEBUG
93789Sahrens const char *
943126Sahl _umem_debug_init(void)
95789Sahrens {
96789Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
97789Sahrens }
98789Sahrens 
99789Sahrens const char *
100789Sahrens _umem_logging_init(void)
101789Sahrens {
102789Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
103789Sahrens }
1046865Srm160521 #endif
105789Sahrens 
1061387Seschrock typedef enum {
1071387Seschrock 	HELP_CLONE,
1081387Seschrock 	HELP_CREATE,
1091387Seschrock 	HELP_DESTROY,
1101387Seschrock 	HELP_GET,
1111387Seschrock 	HELP_INHERIT,
1124577Sahrens 	HELP_UPGRADE,
1131387Seschrock 	HELP_LIST,
1141387Seschrock 	HELP_MOUNT,
1152082Seschrock 	HELP_PROMOTE,
1161749Sahrens 	HELP_RECEIVE,
1171387Seschrock 	HELP_RENAME,
1181387Seschrock 	HELP_ROLLBACK,
1191749Sahrens 	HELP_SEND,
1201387Seschrock 	HELP_SET,
1211387Seschrock 	HELP_SHARE,
1221387Seschrock 	HELP_SNAPSHOT,
1231387Seschrock 	HELP_UNMOUNT,
1244543Smarks 	HELP_UNSHARE,
1254543Smarks 	HELP_ALLOW,
1269396SMatthew.Ahrens@Sun.COM 	HELP_UNALLOW,
1279396SMatthew.Ahrens@Sun.COM 	HELP_USERSPACE,
12810242Schris.kirby@sun.com 	HELP_GROUPSPACE,
12910242Schris.kirby@sun.com 	HELP_HOLD,
13010242Schris.kirby@sun.com 	HELP_HOLDS,
13110242Schris.kirby@sun.com 	HELP_RELEASE
1321387Seschrock } zfs_help_t;
1331387Seschrock 
134789Sahrens typedef struct zfs_command {
135789Sahrens 	const char	*name;
136789Sahrens 	int		(*func)(int argc, char **argv);
1371387Seschrock 	zfs_help_t	usage;
138789Sahrens } zfs_command_t;
139789Sahrens 
140789Sahrens /*
141789Sahrens  * Master command table.  Each ZFS command has a name, associated function, and
1421544Seschrock  * usage message.  The usage messages need to be internationalized, so we have
1431544Seschrock  * to have a function to return the usage message based on a command index.
1441387Seschrock  *
1451387Seschrock  * These commands are organized according to how they are displayed in the usage
1461387Seschrock  * message.  An empty command (one with a NULL name) indicates an empty line in
1471387Seschrock  * the generic usage message.
148789Sahrens  */
149789Sahrens static zfs_command_t command_table[] = {
1501387Seschrock 	{ "create",	zfs_do_create,		HELP_CREATE		},
1511387Seschrock 	{ "destroy",	zfs_do_destroy,		HELP_DESTROY		},
152789Sahrens 	{ NULL },
1531387Seschrock 	{ "snapshot",	zfs_do_snapshot,	HELP_SNAPSHOT		},
1541387Seschrock 	{ "rollback",	zfs_do_rollback,	HELP_ROLLBACK		},
1551387Seschrock 	{ "clone",	zfs_do_clone,		HELP_CLONE		},
1562082Seschrock 	{ "promote",	zfs_do_promote,		HELP_PROMOTE		},
1571387Seschrock 	{ "rename",	zfs_do_rename,		HELP_RENAME		},
158789Sahrens 	{ NULL },
1591387Seschrock 	{ "list",	zfs_do_list,		HELP_LIST		},
160789Sahrens 	{ NULL },
1611387Seschrock 	{ "set",	zfs_do_set,		HELP_SET		},
16211022STom.Erickson@Sun.COM 	{ "get",	zfs_do_get,		HELP_GET		},
1631387Seschrock 	{ "inherit",	zfs_do_inherit,		HELP_INHERIT		},
1644577Sahrens 	{ "upgrade",	zfs_do_upgrade,		HELP_UPGRADE		},
1659396SMatthew.Ahrens@Sun.COM 	{ "userspace",	zfs_do_userspace,	HELP_USERSPACE		},
1669396SMatthew.Ahrens@Sun.COM 	{ "groupspace",	zfs_do_userspace,	HELP_GROUPSPACE		},
167789Sahrens 	{ NULL },
1681387Seschrock 	{ "mount",	zfs_do_mount,		HELP_MOUNT		},
1691387Seschrock 	{ "unmount",	zfs_do_unmount,		HELP_UNMOUNT		},
1701387Seschrock 	{ "share",	zfs_do_share,		HELP_SHARE		},
1711387Seschrock 	{ "unshare",	zfs_do_unshare,		HELP_UNSHARE		},
172789Sahrens 	{ NULL },
1731749Sahrens 	{ "send",	zfs_do_send,		HELP_SEND		},
1741749Sahrens 	{ "receive",	zfs_do_receive,		HELP_RECEIVE		},
1754543Smarks 	{ NULL },
1769396SMatthew.Ahrens@Sun.COM 	{ "allow",	zfs_do_python,		HELP_ALLOW		},
1774543Smarks 	{ NULL },
1789396SMatthew.Ahrens@Sun.COM 	{ "unallow",	zfs_do_python,		HELP_UNALLOW		},
17910242Schris.kirby@sun.com 	{ NULL },
18010242Schris.kirby@sun.com 	{ "hold",	zfs_do_hold,		HELP_HOLD		},
18110242Schris.kirby@sun.com 	{ "holds",	zfs_do_python,		HELP_HOLDS		},
18210242Schris.kirby@sun.com 	{ "release",	zfs_do_release,		HELP_RELEASE		},
183789Sahrens };
184789Sahrens 
185789Sahrens #define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
186789Sahrens 
187789Sahrens zfs_command_t *current_command;
188789Sahrens 
1891387Seschrock static const char *
1901387Seschrock get_usage(zfs_help_t idx)
1911387Seschrock {
1921387Seschrock 	switch (idx) {
1931387Seschrock 	case HELP_CLONE:
1947265Sahrens 		return (gettext("\tclone [-p] [-o property=value] ... "
1957265Sahrens 		    "<snapshot> <filesystem|volume>\n"));
1961387Seschrock 	case HELP_CREATE:
1974849Sahrens 		return (gettext("\tcreate [-p] [-o property=value] ... "
1982676Seschrock 		    "<filesystem>\n"
1994849Sahrens 		    "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
2004849Sahrens 		    "-V <size> <volume>\n"));
2011387Seschrock 	case HELP_DESTROY:
20210385Schris.kirby@sun.com 		return (gettext("\tdestroy [-rRf] <filesystem|volume>\n"
20310385Schris.kirby@sun.com 		    "\tdestroy [-rRd] <snapshot>\n"));
2041387Seschrock 	case HELP_GET:
2059365SChris.Gerhard@sun.com 		return (gettext("\tget [-rHp] [-d max] "
20611022STom.Erickson@Sun.COM 		    "[-o \"all\" | field[,...]] [-s source[,...]]\n"
2074849Sahrens 		    "\t    <\"all\" | property[,...]> "
2082676Seschrock 		    "[filesystem|volume|snapshot] ...\n"));
2091387Seschrock 	case HELP_INHERIT:
21011022STom.Erickson@Sun.COM 		return (gettext("\tinherit [-rS] <property> "
2117265Sahrens 		    "<filesystem|volume|snapshot> ...\n"));
2124577Sahrens 	case HELP_UPGRADE:
2134577Sahrens 		return (gettext("\tupgrade [-v]\n"
2144577Sahrens 		    "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
2151387Seschrock 	case HELP_LIST:
2169365SChris.Gerhard@sun.com 		return (gettext("\tlist [-rH][-d max] "
2179365SChris.Gerhard@sun.com 		    "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
2184849Sahrens 		    "\t    [-S property] ... "
2194849Sahrens 		    "[filesystem|volume|snapshot] ...\n"));
2201387Seschrock 	case HELP_MOUNT:
2211387Seschrock 		return (gettext("\tmount\n"
2224849Sahrens 		    "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
2232082Seschrock 	case HELP_PROMOTE:
2244849Sahrens 		return (gettext("\tpromote <clone-filesystem>\n"));
2251749Sahrens 	case HELP_RECEIVE:
22612409STom.Erickson@Sun.COM 		return (gettext("\treceive [-vnFu] <filesystem|volume|"
2272665Snd150628 		"snapshot>\n"
22812409STom.Erickson@Sun.COM 		"\treceive [-vnFu] [-d | -e] <filesystem>\n"));
2291387Seschrock 	case HELP_RENAME:
2301387Seschrock 		return (gettext("\trename <filesystem|volume|snapshot> "
2314007Smmusante 		    "<filesystem|volume|snapshot>\n"
2324490Svb160487 		    "\trename -p <filesystem|volume> <filesystem|volume>\n"
2334007Smmusante 		    "\trename -r <snapshot> <snapshot>"));
2341387Seschrock 	case HELP_ROLLBACK:
2355568Sahrens 		return (gettext("\trollback [-rRf] <snapshot>\n"));
2361749Sahrens 	case HELP_SEND:
23711022STom.Erickson@Sun.COM 		return (gettext("\tsend [-RDp] [-[iI] snapshot] <snapshot>\n"));
2381387Seschrock 	case HELP_SET:
2391387Seschrock 		return (gettext("\tset <property=value> "
2407265Sahrens 		    "<filesystem|volume|snapshot> ...\n"));
2411387Seschrock 	case HELP_SHARE:
2424849Sahrens 		return (gettext("\tshare <-a | filesystem>\n"));
2431387Seschrock 	case HELP_SNAPSHOT:
2447265Sahrens 		return (gettext("\tsnapshot [-r] [-o property=value] ... "
2454849Sahrens 		    "<filesystem@snapname|volume@snapname>\n"));
2461387Seschrock 	case HELP_UNMOUNT:
2474849Sahrens 		return (gettext("\tunmount [-f] "
2484849Sahrens 		    "<-a | filesystem|mountpoint>\n"));
2491387Seschrock 	case HELP_UNSHARE:
25010228SStephanie.Scheffler@Sun.COM 		return (gettext("\tunshare "
2514849Sahrens 		    "<-a | filesystem|mountpoint>\n"));
2524543Smarks 	case HELP_ALLOW:
2539564SStephanie.Scheffler@Sun.COM 		return (gettext("\tallow <filesystem|volume>\n"
2549564SStephanie.Scheffler@Sun.COM 		    "\tallow [-ldug] "
2554849Sahrens 		    "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
2564849Sahrens 		    "\t    <filesystem|volume>\n"
2574849Sahrens 		    "\tallow [-ld] -e <perm|@setname>[,...] "
2584849Sahrens 		    "<filesystem|volume>\n"
2594849Sahrens 		    "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
2604849Sahrens 		    "\tallow -s @setname <perm|@setname>[,...] "
2614849Sahrens 		    "<filesystem|volume>\n"));
2624543Smarks 	case HELP_UNALLOW:
2634849Sahrens 		return (gettext("\tunallow [-rldug] "
2644849Sahrens 		    "<\"everyone\"|user|group>[,...]\n"
2654849Sahrens 		    "\t    [<perm|@setname>[,...]] <filesystem|volume>\n"
2664849Sahrens 		    "\tunallow [-rld] -e [<perm|@setname>[,...]] "
2674849Sahrens 		    "<filesystem|volume>\n"
2684849Sahrens 		    "\tunallow [-r] -c [<perm|@setname>[,...]] "
2694849Sahrens 		    "<filesystem|volume>\n"
2704849Sahrens 		    "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
2714849Sahrens 		    "<filesystem|volume>\n"));
2729396SMatthew.Ahrens@Sun.COM 	case HELP_USERSPACE:
2739396SMatthew.Ahrens@Sun.COM 		return (gettext("\tuserspace [-hniHp] [-o field[,...]] "
2749396SMatthew.Ahrens@Sun.COM 		    "[-sS field] ... [-t type[,...]]\n"
2759396SMatthew.Ahrens@Sun.COM 		    "\t    <filesystem|snapshot>\n"));
2769396SMatthew.Ahrens@Sun.COM 	case HELP_GROUPSPACE:
2779396SMatthew.Ahrens@Sun.COM 		return (gettext("\tgroupspace [-hniHpU] [-o field[,...]] "
2789396SMatthew.Ahrens@Sun.COM 		    "[-sS field] ... [-t type[,...]]\n"
2799396SMatthew.Ahrens@Sun.COM 		    "\t    <filesystem|snapshot>\n"));
28010242Schris.kirby@sun.com 	case HELP_HOLD:
28110242Schris.kirby@sun.com 		return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
28210242Schris.kirby@sun.com 	case HELP_HOLDS:
28310242Schris.kirby@sun.com 		return (gettext("\tholds [-r] <snapshot> ...\n"));
28410242Schris.kirby@sun.com 	case HELP_RELEASE:
28510242Schris.kirby@sun.com 		return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
2861387Seschrock 	}
2871387Seschrock 
2881387Seschrock 	abort();
2891387Seschrock 	/* NOTREACHED */
2901387Seschrock }
2911387Seschrock 
29212446SMark.Musante@Sun.COM void
29312446SMark.Musante@Sun.COM nomem(void)
29412446SMark.Musante@Sun.COM {
29512446SMark.Musante@Sun.COM 	(void) fprintf(stderr, gettext("internal error: out of memory\n"));
29612446SMark.Musante@Sun.COM 	exit(1);
29712446SMark.Musante@Sun.COM }
29812446SMark.Musante@Sun.COM 
299789Sahrens /*
300789Sahrens  * Utility function to guarantee malloc() success.
301789Sahrens  */
30212446SMark.Musante@Sun.COM 
303789Sahrens void *
304789Sahrens safe_malloc(size_t size)
305789Sahrens {
306789Sahrens 	void *data;
307789Sahrens 
30812446SMark.Musante@Sun.COM 	if ((data = calloc(1, size)) == NULL)
30912446SMark.Musante@Sun.COM 		nomem();
310789Sahrens 
311789Sahrens 	return (data);
312789Sahrens }
313789Sahrens 
31412446SMark.Musante@Sun.COM static char *
31512446SMark.Musante@Sun.COM safe_strdup(char *str)
31612446SMark.Musante@Sun.COM {
31712446SMark.Musante@Sun.COM 	char *dupstr = strdup(str);
31812446SMark.Musante@Sun.COM 
31912446SMark.Musante@Sun.COM 	if (dupstr == NULL)
32012446SMark.Musante@Sun.COM 		nomem();
32112446SMark.Musante@Sun.COM 
32212446SMark.Musante@Sun.COM 	return (dupstr);
32312446SMark.Musante@Sun.COM }
32412446SMark.Musante@Sun.COM 
325789Sahrens /*
3265094Slling  * Callback routine that will print out information for each of
3273654Sgw25295  * the properties.
3283654Sgw25295  */
3295094Slling static int
3305094Slling usage_prop_cb(int prop, void *cb)
3313654Sgw25295 {
3323654Sgw25295 	FILE *fp = cb;
3333654Sgw25295 
3346643Seschrock 	(void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
3356643Seschrock 
3366643Seschrock 	if (zfs_prop_readonly(prop))
3376643Seschrock 		(void) fprintf(fp, " NO    ");
3383654Sgw25295 	else
3396643Seschrock 		(void) fprintf(fp, "YES    ");
3403654Sgw25295 
3413654Sgw25295 	if (zfs_prop_inheritable(prop))
3423654Sgw25295 		(void) fprintf(fp, "  YES   ");
3433654Sgw25295 	else
3443654Sgw25295 		(void) fprintf(fp, "   NO   ");
3453654Sgw25295 
3463654Sgw25295 	if (zfs_prop_values(prop) == NULL)
3473654Sgw25295 		(void) fprintf(fp, "-\n");
3483654Sgw25295 	else
3493654Sgw25295 		(void) fprintf(fp, "%s\n", zfs_prop_values(prop));
3503654Sgw25295 
3515094Slling 	return (ZPROP_CONT);
3523654Sgw25295 }
3533654Sgw25295 
3543654Sgw25295 /*
355789Sahrens  * Display usage message.  If we're inside a command, display only the usage for
356789Sahrens  * that command.  Otherwise, iterate over the entire command table and display
357789Sahrens  * a complete usage message.
358789Sahrens  */
359789Sahrens static void
3602082Seschrock usage(boolean_t requested)
361789Sahrens {
362789Sahrens 	int i;
3632082Seschrock 	boolean_t show_properties = B_FALSE;
364789Sahrens 	FILE *fp = requested ? stdout : stderr;
365789Sahrens 
366789Sahrens 	if (current_command == NULL) {
367789Sahrens 
368789Sahrens 		(void) fprintf(fp, gettext("usage: zfs command args ...\n"));
369789Sahrens 		(void) fprintf(fp,
370789Sahrens 		    gettext("where 'command' is one of the following:\n\n"));
371789Sahrens 
372789Sahrens 		for (i = 0; i < NCOMMAND; i++) {
373789Sahrens 			if (command_table[i].name == NULL)
374789Sahrens 				(void) fprintf(fp, "\n");
375789Sahrens 			else
376789Sahrens 				(void) fprintf(fp, "%s",
3771387Seschrock 				    get_usage(command_table[i].usage));
378789Sahrens 		}
379789Sahrens 
380789Sahrens 		(void) fprintf(fp, gettext("\nEach dataset is of the form: "
381789Sahrens 		    "pool/[dataset/]*dataset[@name]\n"));
382789Sahrens 	} else {
383789Sahrens 		(void) fprintf(fp, gettext("usage:\n"));
3841387Seschrock 		(void) fprintf(fp, "%s", get_usage(current_command->usage));
385789Sahrens 	}
386789Sahrens 
3872190Sdarrenm 	if (current_command != NULL &&
3882190Sdarrenm 	    (strcmp(current_command->name, "set") == 0 ||
389789Sahrens 	    strcmp(current_command->name, "get") == 0 ||
390789Sahrens 	    strcmp(current_command->name, "inherit") == 0 ||
3912190Sdarrenm 	    strcmp(current_command->name, "list") == 0))
3922082Seschrock 		show_properties = B_TRUE;
393789Sahrens 
394789Sahrens 	if (show_properties) {
395789Sahrens 		(void) fprintf(fp,
396789Sahrens 		    gettext("\nThe following properties are supported:\n"));
397789Sahrens 
3985378Sck153898 		(void) fprintf(fp, "\n\t%-14s %s  %s   %s\n\n",
399789Sahrens 		    "PROPERTY", "EDIT", "INHERIT", "VALUES");
400789Sahrens 
4013654Sgw25295 		/* Iterate over all properties */
4025094Slling 		(void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
4035094Slling 		    ZFS_TYPE_DATASET);
4043654Sgw25295 
4059396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, "\t%-15s ", "userused@...");
4069396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, " NO       NO   <size>\n");
4079396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, "\t%-15s ", "groupused@...");
4089396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, " NO       NO   <size>\n");
4099396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, "\t%-15s ", "userquota@...");
4109396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, "YES       NO   <size> | none\n");
4119396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, "\t%-15s ", "groupquota@...");
4129396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, "YES       NO   <size> | none\n");
4139396SMatthew.Ahrens@Sun.COM 
414789Sahrens 		(void) fprintf(fp, gettext("\nSizes are specified in bytes "
415789Sahrens 		    "with standard units such as K, M, G, etc.\n"));
4167390SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, gettext("\nUser-defined properties can "
4172676Seschrock 		    "be specified by using a name containing a colon (:).\n"));
4189396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
4199396SMatthew.Ahrens@Sun.COM 		    "properties must be appended with\n"
4209396SMatthew.Ahrens@Sun.COM 		    "a user or group specifier of one of these forms:\n"
4219396SMatthew.Ahrens@Sun.COM 		    "    POSIX name      (eg: \"matt\")\n"
4229396SMatthew.Ahrens@Sun.COM 		    "    POSIX id        (eg: \"126829\")\n"
4239396SMatthew.Ahrens@Sun.COM 		    "    SMB name@domain (eg: \"matt@sun\")\n"
4249396SMatthew.Ahrens@Sun.COM 		    "    SMB SID         (eg: \"S-1-234-567-89\")\n"));
4252190Sdarrenm 	} else {
4262190Sdarrenm 		(void) fprintf(fp,
4278269SMark.Musante@Sun.COM 		    gettext("\nFor the property list, run: %s\n"),
4288269SMark.Musante@Sun.COM 		    "zfs set|get");
4295993Smarks 		(void) fprintf(fp,
4308269SMark.Musante@Sun.COM 		    gettext("\nFor the delegated permission list, run: %s\n"),
4318269SMark.Musante@Sun.COM 		    "zfs allow|unallow");
432789Sahrens 	}
433789Sahrens 
4342676Seschrock 	/*
4352676Seschrock 	 * See comments at end of main().
4362676Seschrock 	 */
4372676Seschrock 	if (getenv("ZFS_ABORT") != NULL) {
4382676Seschrock 		(void) printf("dumping core by request\n");
4392676Seschrock 		abort();
4402676Seschrock 	}
4412676Seschrock 
442789Sahrens 	exit(requested ? 0 : 2);
443789Sahrens }
444789Sahrens 
4457265Sahrens static int
4467265Sahrens parseprop(nvlist_t *props)
4477265Sahrens {
4487265Sahrens 	char *propname = optarg;
4497265Sahrens 	char *propval, *strval;
4507265Sahrens 
4517265Sahrens 	if ((propval = strchr(propname, '=')) == NULL) {
4527265Sahrens 		(void) fprintf(stderr, gettext("missing "
4537265Sahrens 		    "'=' for -o option\n"));
4547265Sahrens 		return (-1);
4557265Sahrens 	}
4567265Sahrens 	*propval = '\0';
4577265Sahrens 	propval++;
4587265Sahrens 	if (nvlist_lookup_string(props, propname, &strval) == 0) {
4597265Sahrens 		(void) fprintf(stderr, gettext("property '%s' "
4607265Sahrens 		    "specified multiple times\n"), propname);
4617265Sahrens 		return (-1);
4627265Sahrens 	}
46312446SMark.Musante@Sun.COM 	if (nvlist_add_string(props, propname, propval) != 0)
46412446SMark.Musante@Sun.COM 		nomem();
4657265Sahrens 	return (0);
4667265Sahrens }
4677265Sahrens 
4689365SChris.Gerhard@sun.com static int
4699365SChris.Gerhard@sun.com parse_depth(char *opt, int *flags)
4709365SChris.Gerhard@sun.com {
4719365SChris.Gerhard@sun.com 	char *tmp;
4729365SChris.Gerhard@sun.com 	int depth;
4739365SChris.Gerhard@sun.com 
4749365SChris.Gerhard@sun.com 	depth = (int)strtol(opt, &tmp, 0);
4759365SChris.Gerhard@sun.com 	if (*tmp) {
4769365SChris.Gerhard@sun.com 		(void) fprintf(stderr,
4779365SChris.Gerhard@sun.com 		    gettext("%s is not an integer\n"), optarg);
4789365SChris.Gerhard@sun.com 		usage(B_FALSE);
4799365SChris.Gerhard@sun.com 	}
4809365SChris.Gerhard@sun.com 	if (depth < 0) {
4819365SChris.Gerhard@sun.com 		(void) fprintf(stderr,
4829365SChris.Gerhard@sun.com 		    gettext("Depth can not be negative.\n"));
4839365SChris.Gerhard@sun.com 		usage(B_FALSE);
4849365SChris.Gerhard@sun.com 	}
4859365SChris.Gerhard@sun.com 	*flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
4869365SChris.Gerhard@sun.com 	return (depth);
4879365SChris.Gerhard@sun.com }
4889365SChris.Gerhard@sun.com 
48912446SMark.Musante@Sun.COM #define	PROGRESS_DELAY 2		/* seconds */
49012446SMark.Musante@Sun.COM 
49112446SMark.Musante@Sun.COM static char *pt_reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
49212446SMark.Musante@Sun.COM static time_t pt_begin;
49312446SMark.Musante@Sun.COM static char *pt_header = NULL;
49412446SMark.Musante@Sun.COM static boolean_t pt_shown;
49512446SMark.Musante@Sun.COM 
49612446SMark.Musante@Sun.COM static void
49712446SMark.Musante@Sun.COM start_progress_timer(void)
49812446SMark.Musante@Sun.COM {
49912446SMark.Musante@Sun.COM 	pt_begin = time(NULL) + PROGRESS_DELAY;
50012446SMark.Musante@Sun.COM 	pt_shown = B_FALSE;
50112446SMark.Musante@Sun.COM }
50212446SMark.Musante@Sun.COM 
50312446SMark.Musante@Sun.COM static void
50412446SMark.Musante@Sun.COM set_progress_header(char *header)
50512446SMark.Musante@Sun.COM {
50612446SMark.Musante@Sun.COM 	assert(pt_header == NULL);
50712446SMark.Musante@Sun.COM 	pt_header = safe_strdup(header);
50812446SMark.Musante@Sun.COM 	if (pt_shown) {
50912446SMark.Musante@Sun.COM 		(void) printf("%s: ", header);
51012446SMark.Musante@Sun.COM 		(void) fflush(stdout);
51112446SMark.Musante@Sun.COM 	}
51212446SMark.Musante@Sun.COM }
51312446SMark.Musante@Sun.COM 
51412446SMark.Musante@Sun.COM static void
51512446SMark.Musante@Sun.COM update_progress(char *update)
51612446SMark.Musante@Sun.COM {
51712446SMark.Musante@Sun.COM 	if (!pt_shown && time(NULL) > pt_begin) {
51812446SMark.Musante@Sun.COM 		int len = strlen(update);
51912446SMark.Musante@Sun.COM 
52012446SMark.Musante@Sun.COM 		(void) printf("%s: %s%*.*s", pt_header, update, len, len,
52112446SMark.Musante@Sun.COM 		    pt_reverse);
52212446SMark.Musante@Sun.COM 		(void) fflush(stdout);
52312446SMark.Musante@Sun.COM 		pt_shown = B_TRUE;
52412446SMark.Musante@Sun.COM 	} else if (pt_shown) {
52512446SMark.Musante@Sun.COM 		int len = strlen(update);
52612446SMark.Musante@Sun.COM 
52712446SMark.Musante@Sun.COM 		(void) printf("%s%*.*s", update, len, len, pt_reverse);
52812446SMark.Musante@Sun.COM 		(void) fflush(stdout);
52912446SMark.Musante@Sun.COM 	}
53012446SMark.Musante@Sun.COM }
53112446SMark.Musante@Sun.COM 
53212446SMark.Musante@Sun.COM static void
53312446SMark.Musante@Sun.COM finish_progress(char *done)
53412446SMark.Musante@Sun.COM {
53512446SMark.Musante@Sun.COM 	if (pt_shown) {
53612446SMark.Musante@Sun.COM 		(void) printf("%s\n", done);
53712446SMark.Musante@Sun.COM 		(void) fflush(stdout);
53812446SMark.Musante@Sun.COM 	}
53912446SMark.Musante@Sun.COM 	free(pt_header);
54012446SMark.Musante@Sun.COM 	pt_header = NULL;
54112446SMark.Musante@Sun.COM }
542789Sahrens /*
5437265Sahrens  * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
544789Sahrens  *
545789Sahrens  * Given an existing dataset, create a writable copy whose initial contents
546789Sahrens  * are the same as the source.  The newly created dataset maintains a
547789Sahrens  * dependency on the original; the original cannot be destroyed so long as
548789Sahrens  * the clone exists.
5494490Svb160487  *
5504490Svb160487  * The '-p' flag creates all the non-existing ancestors of the target first.
551789Sahrens  */
552789Sahrens static int
553789Sahrens zfs_do_clone(int argc, char **argv)
554789Sahrens {
5557265Sahrens 	zfs_handle_t *zhp = NULL;
5564490Svb160487 	boolean_t parents = B_FALSE;
5577265Sahrens 	nvlist_t *props;
558789Sahrens 	int ret;
5594490Svb160487 	int c;
560789Sahrens 
56112446SMark.Musante@Sun.COM 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
56212446SMark.Musante@Sun.COM 		nomem();
5637265Sahrens 
564789Sahrens 	/* check options */
5657265Sahrens 	while ((c = getopt(argc, argv, "o:p")) != -1) {
5664490Svb160487 		switch (c) {
5677265Sahrens 		case 'o':
5687265Sahrens 			if (parseprop(props))
5697265Sahrens 				return (1);
5707265Sahrens 			break;
5714490Svb160487 		case 'p':
5724490Svb160487 			parents = B_TRUE;
5734490Svb160487 			break;
5744490Svb160487 		case '?':
5754490Svb160487 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5764490Svb160487 			    optopt);
5777265Sahrens 			goto usage;
5784490Svb160487 		}
579789Sahrens 	}
580789Sahrens 
5814490Svb160487 	argc -= optind;
5824490Svb160487 	argv += optind;
5834490Svb160487 
584789Sahrens 	/* check number of arguments */
5854490Svb160487 	if (argc < 1) {
586789Sahrens 		(void) fprintf(stderr, gettext("missing source dataset "
587789Sahrens 		    "argument\n"));
5887265Sahrens 		goto usage;
589789Sahrens 	}
5904490Svb160487 	if (argc < 2) {
591789Sahrens 		(void) fprintf(stderr, gettext("missing target dataset "
592789Sahrens 		    "argument\n"));
5937265Sahrens 		goto usage;
594789Sahrens 	}
5954490Svb160487 	if (argc > 2) {
596789Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
5977265Sahrens 		goto usage;
598789Sahrens 	}
599789Sahrens 
600789Sahrens 	/* open the source dataset */
6014490Svb160487 	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
602789Sahrens 		return (1);
603789Sahrens 
6044490Svb160487 	if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
6054490Svb160487 	    ZFS_TYPE_VOLUME)) {
6064490Svb160487 		/*
6074490Svb160487 		 * Now create the ancestors of the target dataset.  If the
6084490Svb160487 		 * target already exists and '-p' option was used we should not
6094490Svb160487 		 * complain.
6104490Svb160487 		 */
6114490Svb160487 		if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
6124490Svb160487 		    ZFS_TYPE_VOLUME))
6134490Svb160487 			return (0);
6144490Svb160487 		if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
6154490Svb160487 			return (1);
6164490Svb160487 	}
6174490Svb160487 
618789Sahrens 	/* pass to libzfs */
6197265Sahrens 	ret = zfs_clone(zhp, argv[1], props);
620789Sahrens 
621789Sahrens 	/* create the mountpoint if necessary */
622789Sahrens 	if (ret == 0) {
6235094Slling 		zfs_handle_t *clone;
6245094Slling 
6255094Slling 		clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
626789Sahrens 		if (clone != NULL) {
627*13025SEric.Taylor@oracle.com 			if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
628*13025SEric.Taylor@oracle.com 				if ((ret = zfs_mount(clone, NULL, 0)) == 0)
629*13025SEric.Taylor@oracle.com 					ret = zfs_share(clone);
630789Sahrens 			zfs_close(clone);
631789Sahrens 		}
632789Sahrens 	}
633789Sahrens 
634789Sahrens 	zfs_close(zhp);
6357265Sahrens 	nvlist_free(props);
6367265Sahrens 
6377265Sahrens 	return (!!ret);
6387265Sahrens 
6397265Sahrens usage:
6407265Sahrens 	if (zhp)
6417265Sahrens 		zfs_close(zhp);
6427265Sahrens 	nvlist_free(props);
6437265Sahrens 	usage(B_FALSE);
6447265Sahrens 	return (-1);
645789Sahrens }
646789Sahrens 
647789Sahrens /*
6484490Svb160487  * zfs create [-p] [-o prop=value] ... fs
6494490Svb160487  * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
650789Sahrens  *
651789Sahrens  * Create a new dataset.  This command can be used to create filesystems
652789Sahrens  * and volumes.  Snapshot creation is handled by 'zfs snapshot'.
653789Sahrens  * For volumes, the user must specify a size to be used.
654789Sahrens  *
655789Sahrens  * The '-s' flag applies only to volumes, and indicates that we should not try
656789Sahrens  * to set the reservation for this volume.  By default we set a reservation
6575481Sck153898  * equal to the size for any volume.  For pools with SPA_VERSION >=
6585481Sck153898  * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
6594490Svb160487  *
6604490Svb160487  * The '-p' flag creates all the non-existing ancestors of the target first.
661789Sahrens  */
662789Sahrens static int
663789Sahrens zfs_do_create(int argc, char **argv)
664789Sahrens {
665789Sahrens 	zfs_type_t type = ZFS_TYPE_FILESYSTEM;
6662676Seschrock 	zfs_handle_t *zhp = NULL;
6672676Seschrock 	uint64_t volsize;
668789Sahrens 	int c;
6692082Seschrock 	boolean_t noreserve = B_FALSE;
6705367Sahrens 	boolean_t bflag = B_FALSE;
6714490Svb160487 	boolean_t parents = B_FALSE;
6722676Seschrock 	int ret = 1;
6737265Sahrens 	nvlist_t *props;
6742676Seschrock 	uint64_t intval;
675*13025SEric.Taylor@oracle.com 	int canmount = ZFS_CANMOUNT_OFF;
6762676Seschrock 
67712446SMark.Musante@Sun.COM 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
67812446SMark.Musante@Sun.COM 		nomem();
679789Sahrens 
680789Sahrens 	/* check options */
6814490Svb160487 	while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
682789Sahrens 		switch (c) {
683789Sahrens 		case 'V':
684789Sahrens 			type = ZFS_TYPE_VOLUME;
6852676Seschrock 			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
6862676Seschrock 				(void) fprintf(stderr, gettext("bad volume "
6872676Seschrock 				    "size '%s': %s\n"), optarg,
6882676Seschrock 				    libzfs_error_description(g_zfs));
6892676Seschrock 				goto error;
6902676Seschrock 			}
6912676Seschrock 
6922676Seschrock 			if (nvlist_add_uint64(props,
69312446SMark.Musante@Sun.COM 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
69412446SMark.Musante@Sun.COM 				nomem();
6952676Seschrock 			volsize = intval;
696789Sahrens 			break;
6974490Svb160487 		case 'p':
6984490Svb160487 			parents = B_TRUE;
6994490Svb160487 			break;
700789Sahrens 		case 'b':
7015367Sahrens 			bflag = B_TRUE;
7022676Seschrock 			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
7032676Seschrock 				(void) fprintf(stderr, gettext("bad volume "
7042676Seschrock 				    "block size '%s': %s\n"), optarg,
7052676Seschrock 				    libzfs_error_description(g_zfs));
7062676Seschrock 				goto error;
7072676Seschrock 			}
7082676Seschrock 
7092676Seschrock 			if (nvlist_add_uint64(props,
7102676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
71112446SMark.Musante@Sun.COM 			    intval) != 0)
71212446SMark.Musante@Sun.COM 				nomem();
7132676Seschrock 			break;
7142676Seschrock 		case 'o':
7157265Sahrens 			if (parseprop(props))
7162676Seschrock 				goto error;
717789Sahrens 			break;
718789Sahrens 		case 's':
7192082Seschrock 			noreserve = B_TRUE;
720789Sahrens 			break;
721789Sahrens 		case ':':
722789Sahrens 			(void) fprintf(stderr, gettext("missing size "
723789Sahrens 			    "argument\n"));
7242676Seschrock 			goto badusage;
725789Sahrens 			break;
726789Sahrens 		case '?':
727789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
728789Sahrens 			    optopt);
7292676Seschrock 			goto badusage;
730789Sahrens 		}
731789Sahrens 	}
732789Sahrens 
7335367Sahrens 	if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
7345367Sahrens 		(void) fprintf(stderr, gettext("'-s' and '-b' can only be "
7355367Sahrens 		    "used when creating a volume\n"));
7362676Seschrock 		goto badusage;
737789Sahrens 	}
738789Sahrens 
739789Sahrens 	argc -= optind;
740789Sahrens 	argv += optind;
741789Sahrens 
742789Sahrens 	/* check number of arguments */
743789Sahrens 	if (argc == 0) {
744789Sahrens 		(void) fprintf(stderr, gettext("missing %s argument\n"),
745789Sahrens 		    zfs_type_to_name(type));
7462676Seschrock 		goto badusage;
747789Sahrens 	}
748789Sahrens 	if (argc > 1) {
749789Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
7502676Seschrock 		goto badusage;
7512676Seschrock 	}
7522676Seschrock 
7535481Sck153898 	if (type == ZFS_TYPE_VOLUME && !noreserve) {
7545481Sck153898 		zpool_handle_t *zpool_handle;
7555481Sck153898 		uint64_t spa_version;
7565481Sck153898 		char *p;
7575481Sck153898 		zfs_prop_t resv_prop;
7587265Sahrens 		char *strval;
7595481Sck153898 
7605481Sck153898 		if (p = strchr(argv[0], '/'))
7615481Sck153898 			*p = '\0';
7625481Sck153898 		zpool_handle = zpool_open(g_zfs, argv[0]);
7635481Sck153898 		if (p != NULL)
7645481Sck153898 			*p = '/';
7655481Sck153898 		if (zpool_handle == NULL)
7665481Sck153898 			goto error;
7675481Sck153898 		spa_version = zpool_get_prop_int(zpool_handle,
7685481Sck153898 		    ZPOOL_PROP_VERSION, NULL);
7695481Sck153898 		zpool_close(zpool_handle);
7705481Sck153898 		if (spa_version >= SPA_VERSION_REFRESERVATION)
7715481Sck153898 			resv_prop = ZFS_PROP_REFRESERVATION;
7725481Sck153898 		else
7735481Sck153898 			resv_prop = ZFS_PROP_RESERVATION;
77411449SEric.Taylor@Sun.COM 		volsize = zvol_volsize_to_reservation(volsize, props);
7755481Sck153898 
7765481Sck153898 		if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
7775481Sck153898 		    &strval) != 0) {
7785481Sck153898 			if (nvlist_add_uint64(props,
7795481Sck153898 			    zfs_prop_to_name(resv_prop), volsize) != 0) {
7805481Sck153898 				nvlist_free(props);
78112446SMark.Musante@Sun.COM 				nomem();
7825481Sck153898 			}
7832676Seschrock 		}
784789Sahrens 	}
785789Sahrens 
7864490Svb160487 	if (parents && zfs_name_valid(argv[0], type)) {
7874490Svb160487 		/*
7884490Svb160487 		 * Now create the ancestors of target dataset.  If the target
7894490Svb160487 		 * already exists and '-p' option was used we should not
7904490Svb160487 		 * complain.
7914490Svb160487 		 */
7924490Svb160487 		if (zfs_dataset_exists(g_zfs, argv[0], type)) {
7934490Svb160487 			ret = 0;
7944490Svb160487 			goto error;
7954490Svb160487 		}
7964490Svb160487 		if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
7974490Svb160487 			goto error;
7984490Svb160487 	}
7994490Svb160487 
800789Sahrens 	/* pass to libzfs */
8012676Seschrock 	if (zfs_create(g_zfs, argv[0], type, props) != 0)
8022676Seschrock 		goto error;
803789Sahrens 
8045094Slling 	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
8052676Seschrock 		goto error;
806*13025SEric.Taylor@oracle.com 
807*13025SEric.Taylor@oracle.com 	ret = 0;
8086168Shs24103 	/*
8096168Shs24103 	 * if the user doesn't want the dataset automatically mounted,
8106168Shs24103 	 * then skip the mount/share step
8116168Shs24103 	 */
812*13025SEric.Taylor@oracle.com 	if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
813*13025SEric.Taylor@oracle.com 		canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
814789Sahrens 
815789Sahrens 	/*
816789Sahrens 	 * Mount and/or share the new filesystem as appropriate.  We provide a
817789Sahrens 	 * verbose error message to let the user know that their filesystem was
818789Sahrens 	 * in fact created, even if we failed to mount or share it.
819789Sahrens 	 */
8206168Shs24103 	if (canmount == ZFS_CANMOUNT_ON) {
8216168Shs24103 		if (zfs_mount(zhp, NULL, 0) != 0) {
8226168Shs24103 			(void) fprintf(stderr, gettext("filesystem "
8236168Shs24103 			    "successfully created, but not mounted\n"));
8246168Shs24103 			ret = 1;
8256168Shs24103 		} else if (zfs_share(zhp) != 0) {
8266168Shs24103 			(void) fprintf(stderr, gettext("filesystem "
8276168Shs24103 			    "successfully created, but not shared\n"));
8286168Shs24103 			ret = 1;
8296168Shs24103 		}
830789Sahrens 	}
831789Sahrens 
8322676Seschrock error:
8332676Seschrock 	if (zhp)
8342676Seschrock 		zfs_close(zhp);
8352676Seschrock 	nvlist_free(props);
836789Sahrens 	return (ret);
8372676Seschrock badusage:
8382676Seschrock 	nvlist_free(props);
8392676Seschrock 	usage(B_FALSE);
8402676Seschrock 	return (2);
841789Sahrens }
842789Sahrens 
843789Sahrens /*
84410385Schris.kirby@sun.com  * zfs destroy [-rRf] <fs, vol>
84510385Schris.kirby@sun.com  * zfs destroy [-rRd] <snap>
846789Sahrens  *
84711022STom.Erickson@Sun.COM  *	-r	Recursively destroy all children
84811022STom.Erickson@Sun.COM  *	-R	Recursively destroy all dependents, including clones
84911022STom.Erickson@Sun.COM  *	-f	Force unmounting of any dependents
85010242Schris.kirby@sun.com  *	-d	If we can't destroy now, mark for deferred destruction
851789Sahrens  *
852789Sahrens  * Destroys the given dataset.  By default, it will unmount any filesystems,
853789Sahrens  * and refuse to destroy a dataset that has any dependents.  A dependent can
854789Sahrens  * either be a child, or a clone of a child.
855789Sahrens  */
856789Sahrens typedef struct destroy_cbdata {
8572082Seschrock 	boolean_t	cb_first;
858789Sahrens 	int		cb_force;
859789Sahrens 	int		cb_recurse;
860789Sahrens 	int		cb_error;
861789Sahrens 	int		cb_needforce;
862789Sahrens 	int		cb_doclones;
8633265Sahrens 	boolean_t	cb_closezhp;
864789Sahrens 	zfs_handle_t	*cb_target;
8652199Sahrens 	char		*cb_snapname;
86610242Schris.kirby@sun.com 	boolean_t	cb_defer_destroy;
867789Sahrens } destroy_cbdata_t;
868789Sahrens 
869789Sahrens /*
870789Sahrens  * Check for any dependents based on the '-r' or '-R' flags.
871789Sahrens  */
872789Sahrens static int
873789Sahrens destroy_check_dependent(zfs_handle_t *zhp, void *data)
874789Sahrens {
875789Sahrens 	destroy_cbdata_t *cbp = data;
876789Sahrens 	const char *tname = zfs_get_name(cbp->cb_target);
877789Sahrens 	const char *name = zfs_get_name(zhp);
878789Sahrens 
879789Sahrens 	if (strncmp(tname, name, strlen(tname)) == 0 &&
880789Sahrens 	    (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
881789Sahrens 		/*
882789Sahrens 		 * This is a direct descendant, not a clone somewhere else in
883789Sahrens 		 * the hierarchy.
884789Sahrens 		 */
885789Sahrens 		if (cbp->cb_recurse)
886789Sahrens 			goto out;
887789Sahrens 
888789Sahrens 		if (cbp->cb_first) {
889789Sahrens 			(void) fprintf(stderr, gettext("cannot destroy '%s': "
890789Sahrens 			    "%s has children\n"),
891789Sahrens 			    zfs_get_name(cbp->cb_target),
892789Sahrens 			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
893789Sahrens 			(void) fprintf(stderr, gettext("use '-r' to destroy "
894789Sahrens 			    "the following datasets:\n"));
8952082Seschrock 			cbp->cb_first = B_FALSE;
896789Sahrens 			cbp->cb_error = 1;
897789Sahrens 		}
898789Sahrens 
899789Sahrens 		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
900789Sahrens 	} else {
901789Sahrens 		/*
902789Sahrens 		 * This is a clone.  We only want to report this if the '-r'
903789Sahrens 		 * wasn't specified, or the target is a snapshot.
904789Sahrens 		 */
905789Sahrens 		if (!cbp->cb_recurse &&
906789Sahrens 		    zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
907789Sahrens 			goto out;
908789Sahrens 
909789Sahrens 		if (cbp->cb_first) {
910789Sahrens 			(void) fprintf(stderr, gettext("cannot destroy '%s': "
911789Sahrens 			    "%s has dependent clones\n"),
912789Sahrens 			    zfs_get_name(cbp->cb_target),
913789Sahrens 			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
914789Sahrens 			(void) fprintf(stderr, gettext("use '-R' to destroy "
915789Sahrens 			    "the following datasets:\n"));
9162082Seschrock 			cbp->cb_first = B_FALSE;
917789Sahrens 			cbp->cb_error = 1;
918789Sahrens 		}
919789Sahrens 
920789Sahrens 		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
921789Sahrens 	}
922789Sahrens 
923789Sahrens out:
924789Sahrens 	zfs_close(zhp);
925789Sahrens 	return (0);
926789Sahrens }
927789Sahrens 
928789Sahrens static int
929789Sahrens destroy_callback(zfs_handle_t *zhp, void *data)
930789Sahrens {
931789Sahrens 	destroy_cbdata_t *cbp = data;
932789Sahrens 
933789Sahrens 	/*
934789Sahrens 	 * Ignore pools (which we've already flagged as an error before getting
93510588SEric.Taylor@Sun.COM 	 * here).
936789Sahrens 	 */
937789Sahrens 	if (strchr(zfs_get_name(zhp), '/') == NULL &&
938789Sahrens 	    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
939789Sahrens 		zfs_close(zhp);
940789Sahrens 		return (0);
941789Sahrens 	}
942789Sahrens 
943789Sahrens 	/*
944789Sahrens 	 * Bail out on the first error.
945789Sahrens 	 */
946789Sahrens 	if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 ||
94710242Schris.kirby@sun.com 	    zfs_destroy(zhp, cbp->cb_defer_destroy) != 0) {
948789Sahrens 		zfs_close(zhp);
949789Sahrens 		return (-1);
950789Sahrens 	}
951789Sahrens 
952789Sahrens 	zfs_close(zhp);
953789Sahrens 	return (0);
954789Sahrens }
955789Sahrens 
9562199Sahrens static int
9572199Sahrens destroy_snap_clones(zfs_handle_t *zhp, void *arg)
9582199Sahrens {
9592199Sahrens 	destroy_cbdata_t *cbp = arg;
9602199Sahrens 	char thissnap[MAXPATHLEN];
9612199Sahrens 	zfs_handle_t *szhp;
9623265Sahrens 	boolean_t closezhp = cbp->cb_closezhp;
9633265Sahrens 	int rv;
9642199Sahrens 
9652199Sahrens 	(void) snprintf(thissnap, sizeof (thissnap),
9662199Sahrens 	    "%s@%s", zfs_get_name(zhp), cbp->cb_snapname);
9672199Sahrens 
9682199Sahrens 	libzfs_print_on_error(g_zfs, B_FALSE);
9692199Sahrens 	szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT);
9702199Sahrens 	libzfs_print_on_error(g_zfs, B_TRUE);
9712199Sahrens 	if (szhp) {
9722199Sahrens 		/*
9732199Sahrens 		 * Destroy any clones of this snapshot
9742199Sahrens 		 */
9752474Seschrock 		if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback,
9762474Seschrock 		    cbp) != 0) {
9772474Seschrock 			zfs_close(szhp);
9783265Sahrens 			if (closezhp)
9793265Sahrens 				zfs_close(zhp);
9802474Seschrock 			return (-1);
9812474Seschrock 		}
9822199Sahrens 		zfs_close(szhp);
9832199Sahrens 	}
9842199Sahrens 
9853265Sahrens 	cbp->cb_closezhp = B_TRUE;
9863265Sahrens 	rv = zfs_iter_filesystems(zhp, destroy_snap_clones, arg);
9873265Sahrens 	if (closezhp)
9883265Sahrens 		zfs_close(zhp);
9893265Sahrens 	return (rv);
9902199Sahrens }
991789Sahrens 
992789Sahrens static int
993789Sahrens zfs_do_destroy(int argc, char **argv)
994789Sahrens {
995789Sahrens 	destroy_cbdata_t cb = { 0 };
996789Sahrens 	int c;
997789Sahrens 	zfs_handle_t *zhp;
9982199Sahrens 	char *cp;
99910385Schris.kirby@sun.com 	zfs_type_t type = ZFS_TYPE_DATASET;
1000789Sahrens 
1001789Sahrens 	/* check options */
100210242Schris.kirby@sun.com 	while ((c = getopt(argc, argv, "dfrR")) != -1) {
1003789Sahrens 		switch (c) {
100410242Schris.kirby@sun.com 		case 'd':
100510242Schris.kirby@sun.com 			cb.cb_defer_destroy = B_TRUE;
100610385Schris.kirby@sun.com 			type = ZFS_TYPE_SNAPSHOT;
100710242Schris.kirby@sun.com 			break;
1008789Sahrens 		case 'f':
1009789Sahrens 			cb.cb_force = 1;
1010789Sahrens 			break;
1011789Sahrens 		case 'r':
1012789Sahrens 			cb.cb_recurse = 1;
1013789Sahrens 			break;
1014789Sahrens 		case 'R':
1015789Sahrens 			cb.cb_recurse = 1;
1016789Sahrens 			cb.cb_doclones = 1;
1017789Sahrens 			break;
1018789Sahrens 		case '?':
1019789Sahrens 		default:
1020789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1021789Sahrens 			    optopt);
10222082Seschrock 			usage(B_FALSE);
1023789Sahrens 		}
1024789Sahrens 	}
1025789Sahrens 
1026789Sahrens 	argc -= optind;
1027789Sahrens 	argv += optind;
1028789Sahrens 
1029789Sahrens 	/* check number of arguments */
1030789Sahrens 	if (argc == 0) {
1031789Sahrens 		(void) fprintf(stderr, gettext("missing path argument\n"));
10322082Seschrock 		usage(B_FALSE);
1033789Sahrens 	}
1034789Sahrens 	if (argc > 1) {
1035789Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
10362082Seschrock 		usage(B_FALSE);
1037789Sahrens 	}
1038789Sahrens 
10392199Sahrens 	/*
10402199Sahrens 	 * If we are doing recursive destroy of a snapshot, then the
10412199Sahrens 	 * named snapshot may not exist.  Go straight to libzfs.
10422199Sahrens 	 */
10432199Sahrens 	if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) {
10442199Sahrens 		int ret;
10452199Sahrens 
10462199Sahrens 		*cp = '\0';
10475094Slling 		if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
10482199Sahrens 			return (1);
10492199Sahrens 		*cp = '@';
10502199Sahrens 		cp++;
10512199Sahrens 
10522199Sahrens 		if (cb.cb_doclones) {
105310385Schris.kirby@sun.com 			boolean_t defer = cb.cb_defer_destroy;
105410385Schris.kirby@sun.com 
105510385Schris.kirby@sun.com 			/*
105610385Schris.kirby@sun.com 			 * Temporarily ignore the defer_destroy setting since
105710385Schris.kirby@sun.com 			 * it's not supported for clones.
105810385Schris.kirby@sun.com 			 */
105910385Schris.kirby@sun.com 			cb.cb_defer_destroy = B_FALSE;
10602199Sahrens 			cb.cb_snapname = cp;
10612474Seschrock 			if (destroy_snap_clones(zhp, &cb) != 0) {
10622474Seschrock 				zfs_close(zhp);
10632474Seschrock 				return (1);
10642474Seschrock 			}
106510385Schris.kirby@sun.com 			cb.cb_defer_destroy = defer;
10662199Sahrens 		}
10672199Sahrens 
106810242Schris.kirby@sun.com 		ret = zfs_destroy_snaps(zhp, cp, cb.cb_defer_destroy);
10692199Sahrens 		zfs_close(zhp);
10702199Sahrens 		if (ret) {
10712199Sahrens 			(void) fprintf(stderr,
10722199Sahrens 			    gettext("no snapshots destroyed\n"));
10732199Sahrens 		}
10742199Sahrens 		return (ret != 0);
10752199Sahrens 	}
10762199Sahrens 
1077789Sahrens 	/* Open the given dataset */
107810385Schris.kirby@sun.com 	if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1079789Sahrens 		return (1);
1080789Sahrens 
1081789Sahrens 	cb.cb_target = zhp;
1082789Sahrens 
1083789Sahrens 	/*
1084789Sahrens 	 * Perform an explicit check for pools before going any further.
1085789Sahrens 	 */
1086789Sahrens 	if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1087789Sahrens 	    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1088789Sahrens 		(void) fprintf(stderr, gettext("cannot destroy '%s': "
1089789Sahrens 		    "operation does not apply to pools\n"),
1090789Sahrens 		    zfs_get_name(zhp));
1091789Sahrens 		(void) fprintf(stderr, gettext("use 'zfs destroy -r "
1092789Sahrens 		    "%s' to destroy all datasets in the pool\n"),
1093789Sahrens 		    zfs_get_name(zhp));
1094789Sahrens 		(void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1095789Sahrens 		    "to destroy the pool itself\n"), zfs_get_name(zhp));
1096789Sahrens 		zfs_close(zhp);
1097789Sahrens 		return (1);
1098789Sahrens 	}
1099789Sahrens 
1100789Sahrens 	/*
1101789Sahrens 	 * Check for any dependents and/or clones.
1102789Sahrens 	 */
11032082Seschrock 	cb.cb_first = B_TRUE;
110410242Schris.kirby@sun.com 	if (!cb.cb_doclones && !cb.cb_defer_destroy &&
11052474Seschrock 	    zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
11062474Seschrock 	    &cb) != 0) {
11072474Seschrock 		zfs_close(zhp);
11082474Seschrock 		return (1);
11092474Seschrock 	}
11102474Seschrock 
111110242Schris.kirby@sun.com 	if (cb.cb_error || (!cb.cb_defer_destroy &&
111210242Schris.kirby@sun.com 	    (zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0))) {
1113789Sahrens 		zfs_close(zhp);
1114789Sahrens 		return (1);
1115789Sahrens 	}
1116789Sahrens 
1117789Sahrens 	/*
11182474Seschrock 	 * Do the real thing.  The callback will close the handle regardless of
11192474Seschrock 	 * whether it succeeds or not.
1120789Sahrens 	 */
11214543Smarks 
11222474Seschrock 	if (destroy_callback(zhp, &cb) != 0)
11232474Seschrock 		return (1);
11242474Seschrock 
11252474Seschrock 	return (0);
1126789Sahrens }
1127789Sahrens 
112811022STom.Erickson@Sun.COM static boolean_t
112911022STom.Erickson@Sun.COM is_recvd_column(zprop_get_cbdata_t *cbp)
113011022STom.Erickson@Sun.COM {
113111022STom.Erickson@Sun.COM 	int i;
113211022STom.Erickson@Sun.COM 	zfs_get_column_t col;
113311022STom.Erickson@Sun.COM 
113411022STom.Erickson@Sun.COM 	for (i = 0; i < ZFS_GET_NCOLS &&
113511022STom.Erickson@Sun.COM 	    (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
113611022STom.Erickson@Sun.COM 		if (col == GET_COL_RECVD)
113711022STom.Erickson@Sun.COM 			return (B_TRUE);
113811022STom.Erickson@Sun.COM 	return (B_FALSE);
113911022STom.Erickson@Sun.COM }
114011022STom.Erickson@Sun.COM 
1141789Sahrens /*
114211022STom.Erickson@Sun.COM  * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
114311022STom.Erickson@Sun.COM  *	< all | property[,property]... > < fs | snap | vol > ...
1144789Sahrens  *
1145789Sahrens  *	-r	recurse over any child datasets
1146789Sahrens  *	-H	scripted mode.  Headers are stripped, and fields are separated
1147789Sahrens  *		by tabs instead of spaces.
114811022STom.Erickson@Sun.COM  *	-o	Set of fields to display.  One of "name,property,value,
114911022STom.Erickson@Sun.COM  *		received,source". Default is "name,property,value,source".
115011022STom.Erickson@Sun.COM  *		"all" is an alias for all five.
1151789Sahrens  *	-s	Set of sources to allow.  One of
115211022STom.Erickson@Sun.COM  *		"local,default,inherited,received,temporary,none".  Default is
115311022STom.Erickson@Sun.COM  *		all six.
1154789Sahrens  *	-p	Display values in parsable (literal) format.
1155789Sahrens  *
1156789Sahrens  *  Prints properties for the given datasets.  The user can control which
1157789Sahrens  *  columns to display as well as which property types to allow.
1158789Sahrens  */
1159789Sahrens 
1160789Sahrens /*
1161789Sahrens  * Invoked to display the properties for a single dataset.
1162789Sahrens  */
1163789Sahrens static int
1164789Sahrens get_callback(zfs_handle_t *zhp, void *data)
1165789Sahrens {
1166789Sahrens 	char buf[ZFS_MAXPROPLEN];
116711022STom.Erickson@Sun.COM 	char rbuf[ZFS_MAXPROPLEN];
11685094Slling 	zprop_source_t sourcetype;
1169789Sahrens 	char source[ZFS_MAXNAMELEN];
11705094Slling 	zprop_get_cbdata_t *cbp = data;
117111022STom.Erickson@Sun.COM 	nvlist_t *user_props = zfs_get_user_props(zhp);
11725094Slling 	zprop_list_t *pl = cbp->cb_proplist;
11732676Seschrock 	nvlist_t *propval;
11742676Seschrock 	char *strval;
11752676Seschrock 	char *sourceval;
117611022STom.Erickson@Sun.COM 	boolean_t received = is_recvd_column(cbp);
11772676Seschrock 
11782676Seschrock 	for (; pl != NULL; pl = pl->pl_next) {
117911022STom.Erickson@Sun.COM 		char *recvdval = NULL;
11802676Seschrock 		/*
11812676Seschrock 		 * Skip the special fake placeholder.  This will also skip over
11822676Seschrock 		 * the name property when 'all' is specified.
11832676Seschrock 		 */
11842676Seschrock 		if (pl->pl_prop == ZFS_PROP_NAME &&
11852676Seschrock 		    pl == cbp->cb_proplist)
11862676Seschrock 			continue;
11872676Seschrock 
11885094Slling 		if (pl->pl_prop != ZPROP_INVAL) {
11892676Seschrock 			if (zfs_prop_get(zhp, pl->pl_prop, buf,
11902676Seschrock 			    sizeof (buf), &sourcetype, source,
11912676Seschrock 			    sizeof (source),
11922676Seschrock 			    cbp->cb_literal) != 0) {
11932676Seschrock 				if (pl->pl_all)
11942676Seschrock 					continue;
11953912Slling 				if (!zfs_prop_valid_for_type(pl->pl_prop,
11965094Slling 				    ZFS_TYPE_DATASET)) {
11973912Slling 					(void) fprintf(stderr,
11983912Slling 					    gettext("No such property '%s'\n"),
11993912Slling 					    zfs_prop_to_name(pl->pl_prop));
12003912Slling 					continue;
12013912Slling 				}
12025094Slling 				sourcetype = ZPROP_SRC_NONE;
12032676Seschrock 				(void) strlcpy(buf, "-", sizeof (buf));
12042676Seschrock 			}
12052676Seschrock 
120611022STom.Erickson@Sun.COM 			if (received && (zfs_prop_get_recvd(zhp,
120711022STom.Erickson@Sun.COM 			    zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
120811022STom.Erickson@Sun.COM 			    cbp->cb_literal) == 0))
120911022STom.Erickson@Sun.COM 				recvdval = rbuf;
121011022STom.Erickson@Sun.COM 
12115094Slling 			zprop_print_one_property(zfs_get_name(zhp), cbp,
12122676Seschrock 			    zfs_prop_to_name(pl->pl_prop),
121311022STom.Erickson@Sun.COM 			    buf, sourcetype, source, recvdval);
12149396SMatthew.Ahrens@Sun.COM 		} else if (zfs_prop_userquota(pl->pl_user_prop)) {
12159396SMatthew.Ahrens@Sun.COM 			sourcetype = ZPROP_SRC_LOCAL;
12169396SMatthew.Ahrens@Sun.COM 
12179396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
12189396SMatthew.Ahrens@Sun.COM 			    buf, sizeof (buf), cbp->cb_literal) != 0) {
12199396SMatthew.Ahrens@Sun.COM 				sourcetype = ZPROP_SRC_NONE;
12209396SMatthew.Ahrens@Sun.COM 				(void) strlcpy(buf, "-", sizeof (buf));
12219396SMatthew.Ahrens@Sun.COM 			}
12229396SMatthew.Ahrens@Sun.COM 
12239396SMatthew.Ahrens@Sun.COM 			zprop_print_one_property(zfs_get_name(zhp), cbp,
122411022STom.Erickson@Sun.COM 			    pl->pl_user_prop, buf, sourcetype, source, NULL);
12252676Seschrock 		} else {
122611022STom.Erickson@Sun.COM 			if (nvlist_lookup_nvlist(user_props,
12272676Seschrock 			    pl->pl_user_prop, &propval) != 0) {
12282676Seschrock 				if (pl->pl_all)
12292676Seschrock 					continue;
12305094Slling 				sourcetype = ZPROP_SRC_NONE;
12312676Seschrock 				strval = "-";
12322676Seschrock 			} else {
12332676Seschrock 				verify(nvlist_lookup_string(propval,
12345094Slling 				    ZPROP_VALUE, &strval) == 0);
12352676Seschrock 				verify(nvlist_lookup_string(propval,
12365094Slling 				    ZPROP_SOURCE, &sourceval) == 0);
12372676Seschrock 
12382676Seschrock 				if (strcmp(sourceval,
12392676Seschrock 				    zfs_get_name(zhp)) == 0) {
12405094Slling 					sourcetype = ZPROP_SRC_LOCAL;
124111022STom.Erickson@Sun.COM 				} else if (strcmp(sourceval,
124211022STom.Erickson@Sun.COM 				    ZPROP_SOURCE_VAL_RECVD) == 0) {
124311022STom.Erickson@Sun.COM 					sourcetype = ZPROP_SRC_RECEIVED;
12442676Seschrock 				} else {
12455094Slling 					sourcetype = ZPROP_SRC_INHERITED;
12462676Seschrock 					(void) strlcpy(source,
12472676Seschrock 					    sourceval, sizeof (source));
12482676Seschrock 				}
12492676Seschrock 			}
12502676Seschrock 
125111022STom.Erickson@Sun.COM 			if (received && (zfs_prop_get_recvd(zhp,
125211022STom.Erickson@Sun.COM 			    pl->pl_user_prop, rbuf, sizeof (rbuf),
125311022STom.Erickson@Sun.COM 			    cbp->cb_literal) == 0))
125411022STom.Erickson@Sun.COM 				recvdval = rbuf;
125511022STom.Erickson@Sun.COM 
12565094Slling 			zprop_print_one_property(zfs_get_name(zhp), cbp,
12572676Seschrock 			    pl->pl_user_prop, strval, sourcetype,
125811022STom.Erickson@Sun.COM 			    source, recvdval);
1259866Seschrock 		}
1260789Sahrens 	}
1261789Sahrens 
1262789Sahrens 	return (0);
1263789Sahrens }
1264789Sahrens 
1265789Sahrens static int
1266789Sahrens zfs_do_get(int argc, char **argv)
1267789Sahrens {
12685094Slling 	zprop_get_cbdata_t cb = { 0 };
12697538SRichard.Morris@Sun.COM 	int i, c, flags = 0;
12702676Seschrock 	char *value, *fields;
1271866Seschrock 	int ret;
12729365SChris.Gerhard@sun.com 	int limit = 0;
12735094Slling 	zprop_list_t fake_name = { 0 };
1274789Sahrens 
1275789Sahrens 	/*
1276789Sahrens 	 * Set up default columns and sources.
1277789Sahrens 	 */
12785094Slling 	cb.cb_sources = ZPROP_SRC_ALL;
1279789Sahrens 	cb.cb_columns[0] = GET_COL_NAME;
1280789Sahrens 	cb.cb_columns[1] = GET_COL_PROPERTY;
1281789Sahrens 	cb.cb_columns[2] = GET_COL_VALUE;
1282789Sahrens 	cb.cb_columns[3] = GET_COL_SOURCE;
12835094Slling 	cb.cb_type = ZFS_TYPE_DATASET;
1284789Sahrens 
1285789Sahrens 	/* check options */
12869365SChris.Gerhard@sun.com 	while ((c = getopt(argc, argv, ":d:o:s:rHp")) != -1) {
1287789Sahrens 		switch (c) {
1288789Sahrens 		case 'p':
12892082Seschrock 			cb.cb_literal = B_TRUE;
1290789Sahrens 			break;
12919365SChris.Gerhard@sun.com 		case 'd':
12929365SChris.Gerhard@sun.com 			limit = parse_depth(optarg, &flags);
12939365SChris.Gerhard@sun.com 			break;
1294789Sahrens 		case 'r':
12957538SRichard.Morris@Sun.COM 			flags |= ZFS_ITER_RECURSE;
1296789Sahrens 			break;
1297789Sahrens 		case 'H':
12982082Seschrock 			cb.cb_scripted = B_TRUE;
1299789Sahrens 			break;
1300789Sahrens 		case ':':
1301789Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
1302789Sahrens 			    "'%c' option\n"), optopt);
13032082Seschrock 			usage(B_FALSE);
1304789Sahrens 			break;
1305789Sahrens 		case 'o':
1306789Sahrens 			/*
1307789Sahrens 			 * Process the set of columns to display.  We zero out
1308789Sahrens 			 * the structure to give us a blank slate.
1309789Sahrens 			 */
1310789Sahrens 			bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1311789Sahrens 			i = 0;
1312789Sahrens 			while (*optarg != '\0') {
1313789Sahrens 				static char *col_subopts[] =
131411022STom.Erickson@Sun.COM 				    { "name", "property", "value", "received",
131511022STom.Erickson@Sun.COM 				    "source", "all", NULL };
131611022STom.Erickson@Sun.COM 
131711022STom.Erickson@Sun.COM 				if (i == ZFS_GET_NCOLS) {
1318789Sahrens 					(void) fprintf(stderr, gettext("too "
1319789Sahrens 					    "many fields given to -o "
1320789Sahrens 					    "option\n"));
13212082Seschrock 					usage(B_FALSE);
1322789Sahrens 				}
1323789Sahrens 
1324789Sahrens 				switch (getsubopt(&optarg, col_subopts,
1325789Sahrens 				    &value)) {
1326789Sahrens 				case 0:
1327789Sahrens 					cb.cb_columns[i++] = GET_COL_NAME;
1328789Sahrens 					break;
1329789Sahrens 				case 1:
1330789Sahrens 					cb.cb_columns[i++] = GET_COL_PROPERTY;
1331789Sahrens 					break;
1332789Sahrens 				case 2:
1333789Sahrens 					cb.cb_columns[i++] = GET_COL_VALUE;
1334789Sahrens 					break;
1335789Sahrens 				case 3:
133611022STom.Erickson@Sun.COM 					cb.cb_columns[i++] = GET_COL_RECVD;
133711022STom.Erickson@Sun.COM 					flags |= ZFS_ITER_RECVD_PROPS;
133811022STom.Erickson@Sun.COM 					break;
133911022STom.Erickson@Sun.COM 				case 4:
1340789Sahrens 					cb.cb_columns[i++] = GET_COL_SOURCE;
1341789Sahrens 					break;
134211022STom.Erickson@Sun.COM 				case 5:
134311022STom.Erickson@Sun.COM 					if (i > 0) {
134411022STom.Erickson@Sun.COM 						(void) fprintf(stderr,
134511022STom.Erickson@Sun.COM 						    gettext("\"all\" conflicts "
134611022STom.Erickson@Sun.COM 						    "with specific fields "
134711022STom.Erickson@Sun.COM 						    "given to -o option\n"));
134811022STom.Erickson@Sun.COM 						usage(B_FALSE);
134911022STom.Erickson@Sun.COM 					}
135011022STom.Erickson@Sun.COM 					cb.cb_columns[0] = GET_COL_NAME;
135111022STom.Erickson@Sun.COM 					cb.cb_columns[1] = GET_COL_PROPERTY;
135211022STom.Erickson@Sun.COM 					cb.cb_columns[2] = GET_COL_VALUE;
135311022STom.Erickson@Sun.COM 					cb.cb_columns[3] = GET_COL_RECVD;
135411022STom.Erickson@Sun.COM 					cb.cb_columns[4] = GET_COL_SOURCE;
135511022STom.Erickson@Sun.COM 					flags |= ZFS_ITER_RECVD_PROPS;
135611022STom.Erickson@Sun.COM 					i = ZFS_GET_NCOLS;
135711022STom.Erickson@Sun.COM 					break;
1358789Sahrens 				default:
1359789Sahrens 					(void) fprintf(stderr,
1360789Sahrens 					    gettext("invalid column name "
1361789Sahrens 					    "'%s'\n"), value);
13623912Slling 					usage(B_FALSE);
1363789Sahrens 				}
1364789Sahrens 			}
1365789Sahrens 			break;
1366789Sahrens 
1367789Sahrens 		case 's':
1368789Sahrens 			cb.cb_sources = 0;
1369789Sahrens 			while (*optarg != '\0') {
1370789Sahrens 				static char *source_subopts[] = {
1371789Sahrens 					"local", "default", "inherited",
137211022STom.Erickson@Sun.COM 					"received", "temporary", "none",
137311022STom.Erickson@Sun.COM 					NULL };
1374789Sahrens 
1375789Sahrens 				switch (getsubopt(&optarg, source_subopts,
1376789Sahrens 				    &value)) {
1377789Sahrens 				case 0:
13785094Slling 					cb.cb_sources |= ZPROP_SRC_LOCAL;
1379789Sahrens 					break;
1380789Sahrens 				case 1:
13815094Slling 					cb.cb_sources |= ZPROP_SRC_DEFAULT;
1382789Sahrens 					break;
1383789Sahrens 				case 2:
13845094Slling 					cb.cb_sources |= ZPROP_SRC_INHERITED;
1385789Sahrens 					break;
1386789Sahrens 				case 3:
138711022STom.Erickson@Sun.COM 					cb.cb_sources |= ZPROP_SRC_RECEIVED;
138811022STom.Erickson@Sun.COM 					break;
138911022STom.Erickson@Sun.COM 				case 4:
13905094Slling 					cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1391789Sahrens 					break;
139211022STom.Erickson@Sun.COM 				case 5:
13935094Slling 					cb.cb_sources |= ZPROP_SRC_NONE;
1394789Sahrens 					break;
1395789Sahrens 				default:
1396789Sahrens 					(void) fprintf(stderr,
1397789Sahrens 					    gettext("invalid source "
1398789Sahrens 					    "'%s'\n"), value);
13993912Slling 					usage(B_FALSE);
1400789Sahrens 				}
1401789Sahrens 			}
1402789Sahrens 			break;
1403789Sahrens 
1404789Sahrens 		case '?':
1405789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1406789Sahrens 			    optopt);
14072082Seschrock 			usage(B_FALSE);
1408789Sahrens 		}
1409789Sahrens 	}
1410789Sahrens 
1411789Sahrens 	argc -= optind;
1412789Sahrens 	argv += optind;
1413789Sahrens 
1414789Sahrens 	if (argc < 1) {
1415789Sahrens 		(void) fprintf(stderr, gettext("missing property "
1416789Sahrens 		    "argument\n"));
14172082Seschrock 		usage(B_FALSE);
1418789Sahrens 	}
1419789Sahrens 
1420789Sahrens 	fields = argv[0];
1421789Sahrens 
14225094Slling 	if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
14235094Slling 	    != 0)
14242082Seschrock 		usage(B_FALSE);
1425789Sahrens 
1426789Sahrens 	argc--;
1427789Sahrens 	argv++;
1428789Sahrens 
1429789Sahrens 	/*
14302676Seschrock 	 * As part of zfs_expand_proplist(), we keep track of the maximum column
14312676Seschrock 	 * width for each property.  For the 'NAME' (and 'SOURCE') columns, we
14322676Seschrock 	 * need to know the maximum name length.  However, the user likely did
14332676Seschrock 	 * not specify 'name' as one of the properties to fetch, so we need to
14342676Seschrock 	 * make sure we always include at least this property for
14352676Seschrock 	 * print_get_headers() to work properly.
1436789Sahrens 	 */
14372676Seschrock 	if (cb.cb_proplist != NULL) {
14382676Seschrock 		fake_name.pl_prop = ZFS_PROP_NAME;
14392676Seschrock 		fake_name.pl_width = strlen(gettext("NAME"));
14402676Seschrock 		fake_name.pl_next = cb.cb_proplist;
14412676Seschrock 		cb.cb_proplist = &fake_name;
1442789Sahrens 	}
1443789Sahrens 
14442676Seschrock 	cb.cb_first = B_TRUE;
14452676Seschrock 
1446789Sahrens 	/* run for each object */
14477538SRichard.Morris@Sun.COM 	ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, NULL,
14489365SChris.Gerhard@sun.com 	    &cb.cb_proplist, limit, get_callback, &cb);
14492676Seschrock 
14502676Seschrock 	if (cb.cb_proplist == &fake_name)
14515094Slling 		zprop_free_list(fake_name.pl_next);
14522676Seschrock 	else
14535094Slling 		zprop_free_list(cb.cb_proplist);
14542676Seschrock 
14552676Seschrock 	return (ret);
1456789Sahrens }
1457789Sahrens 
1458789Sahrens /*
145911022STom.Erickson@Sun.COM  * inherit [-rS] <property> <fs|vol> ...
1460789Sahrens  *
146111022STom.Erickson@Sun.COM  *	-r	Recurse over all children
146211022STom.Erickson@Sun.COM  *	-S	Revert to received value, if any
1463789Sahrens  *
1464789Sahrens  * For each dataset specified on the command line, inherit the given property
1465789Sahrens  * from its parent.  Inheriting a property at the pool level will cause it to
1466789Sahrens  * use the default value.  The '-r' flag will recurse over all children, and is
1467789Sahrens  * useful for setting a property on a hierarchy-wide basis, regardless of any
1468789Sahrens  * local modifications for each dataset.
1469789Sahrens  */
14702926Sek110237 
147111022STom.Erickson@Sun.COM typedef struct inherit_cbdata {
147211022STom.Erickson@Sun.COM 	const char *cb_propname;
147311022STom.Erickson@Sun.COM 	boolean_t cb_received;
147411022STom.Erickson@Sun.COM } inherit_cbdata_t;
147511022STom.Erickson@Sun.COM 
1476789Sahrens static int
14777265Sahrens inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1478789Sahrens {
147911022STom.Erickson@Sun.COM 	inherit_cbdata_t *cb = data;
148011022STom.Erickson@Sun.COM 	zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
14817265Sahrens 
14827265Sahrens 	/*
14837265Sahrens 	 * If we're doing it recursively, then ignore properties that
14847265Sahrens 	 * are not valid for this type of dataset.
14857265Sahrens 	 */
14867265Sahrens 	if (prop != ZPROP_INVAL &&
14877265Sahrens 	    !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
14887265Sahrens 		return (0);
14897265Sahrens 
149011022STom.Erickson@Sun.COM 	return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
14917265Sahrens }
14927265Sahrens 
14937265Sahrens static int
14947265Sahrens inherit_cb(zfs_handle_t *zhp, void *data)
14957265Sahrens {
149611022STom.Erickson@Sun.COM 	inherit_cbdata_t *cb = data;
149711022STom.Erickson@Sun.COM 
149811022STom.Erickson@Sun.COM 	return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1499789Sahrens }
1500789Sahrens 
1501789Sahrens static int
1502789Sahrens zfs_do_inherit(int argc, char **argv)
1503789Sahrens {
1504789Sahrens 	int c;
1505789Sahrens 	zfs_prop_t prop;
150611022STom.Erickson@Sun.COM 	inherit_cbdata_t cb = { 0 };
15074543Smarks 	char *propname;
15082926Sek110237 	int ret;
15097538SRichard.Morris@Sun.COM 	int flags = 0;
151011022STom.Erickson@Sun.COM 	boolean_t received = B_FALSE;
1511789Sahrens 
1512789Sahrens 	/* check options */
151311022STom.Erickson@Sun.COM 	while ((c = getopt(argc, argv, "rS")) != -1) {
1514789Sahrens 		switch (c) {
1515789Sahrens 		case 'r':
15167538SRichard.Morris@Sun.COM 			flags |= ZFS_ITER_RECURSE;
1517789Sahrens 			break;
151811022STom.Erickson@Sun.COM 		case 'S':
151911022STom.Erickson@Sun.COM 			received = B_TRUE;
152011022STom.Erickson@Sun.COM 			break;
1521789Sahrens 		case '?':
1522789Sahrens 		default:
1523789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1524789Sahrens 			    optopt);
15252082Seschrock 			usage(B_FALSE);
1526789Sahrens 		}
1527789Sahrens 	}
1528789Sahrens 
1529789Sahrens 	argc -= optind;
1530789Sahrens 	argv += optind;
1531789Sahrens 
1532789Sahrens 	/* check number of arguments */
1533789Sahrens 	if (argc < 1) {
1534789Sahrens 		(void) fprintf(stderr, gettext("missing property argument\n"));
15352082Seschrock 		usage(B_FALSE);
1536789Sahrens 	}
1537789Sahrens 	if (argc < 2) {
1538789Sahrens 		(void) fprintf(stderr, gettext("missing dataset argument\n"));
15392082Seschrock 		usage(B_FALSE);
1540789Sahrens 	}
1541789Sahrens 
15424543Smarks 	propname = argv[0];
15432676Seschrock 	argc--;
15442676Seschrock 	argv++;
15452676Seschrock 
15465094Slling 	if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
15472676Seschrock 		if (zfs_prop_readonly(prop)) {
15482676Seschrock 			(void) fprintf(stderr, gettext(
15492676Seschrock 			    "%s property is read-only\n"),
15504543Smarks 			    propname);
15512676Seschrock 			return (1);
15522676Seschrock 		}
155311022STom.Erickson@Sun.COM 		if (!zfs_prop_inheritable(prop) && !received) {
15542676Seschrock 			(void) fprintf(stderr, gettext("'%s' property cannot "
15554543Smarks 			    "be inherited\n"), propname);
15562676Seschrock 			if (prop == ZFS_PROP_QUOTA ||
15575378Sck153898 			    prop == ZFS_PROP_RESERVATION ||
15585378Sck153898 			    prop == ZFS_PROP_REFQUOTA ||
15595378Sck153898 			    prop == ZFS_PROP_REFRESERVATION)
15602676Seschrock 				(void) fprintf(stderr, gettext("use 'zfs set "
15614543Smarks 				    "%s=none' to clear\n"), propname);
15622676Seschrock 			return (1);
15632676Seschrock 		}
156411515STom.Erickson@Sun.COM 		if (received && (prop == ZFS_PROP_VOLSIZE ||
156511515STom.Erickson@Sun.COM 		    prop == ZFS_PROP_VERSION)) {
156611515STom.Erickson@Sun.COM 			(void) fprintf(stderr, gettext("'%s' property cannot "
156711515STom.Erickson@Sun.COM 			    "be reverted to a received value\n"), propname);
156811515STom.Erickson@Sun.COM 			return (1);
156911515STom.Erickson@Sun.COM 		}
15704543Smarks 	} else if (!zfs_prop_user(propname)) {
15714543Smarks 		(void) fprintf(stderr, gettext("invalid property '%s'\n"),
15724543Smarks 		    propname);
15732082Seschrock 		usage(B_FALSE);
1574789Sahrens 	}
15752676Seschrock 
157611022STom.Erickson@Sun.COM 	cb.cb_propname = propname;
157711022STom.Erickson@Sun.COM 	cb.cb_received = received;
157811022STom.Erickson@Sun.COM 
15797538SRichard.Morris@Sun.COM 	if (flags & ZFS_ITER_RECURSE) {
15807538SRichard.Morris@Sun.COM 		ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
158111022STom.Erickson@Sun.COM 		    NULL, NULL, 0, inherit_recurse_cb, &cb);
15827265Sahrens 	} else {
15837538SRichard.Morris@Sun.COM 		ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
158411022STom.Erickson@Sun.COM 		    NULL, NULL, 0, inherit_cb, &cb);
15857265Sahrens 	}
15862926Sek110237 
15872926Sek110237 	return (ret);
1588789Sahrens }
1589789Sahrens 
15904577Sahrens typedef struct upgrade_cbdata {
15914577Sahrens 	uint64_t cb_numupgraded;
15924577Sahrens 	uint64_t cb_numsamegraded;
15934849Sahrens 	uint64_t cb_numfailed;
15944577Sahrens 	uint64_t cb_version;
15954577Sahrens 	boolean_t cb_newer;
15964577Sahrens 	boolean_t cb_foundone;
15974577Sahrens 	char cb_lastfs[ZFS_MAXNAMELEN];
15984577Sahrens } upgrade_cbdata_t;
15994577Sahrens 
16004577Sahrens static int
16014577Sahrens same_pool(zfs_handle_t *zhp, const char *name)
16024577Sahrens {
16034577Sahrens 	int len1 = strcspn(name, "/@");
16044577Sahrens 	const char *zhname = zfs_get_name(zhp);
16054577Sahrens 	int len2 = strcspn(zhname, "/@");
16064577Sahrens 
16074577Sahrens 	if (len1 != len2)
16084577Sahrens 		return (B_FALSE);
16094988Sek110237 	return (strncmp(name, zhname, len1) == 0);
16104577Sahrens }
16114577Sahrens 
16124577Sahrens static int
16134577Sahrens upgrade_list_callback(zfs_handle_t *zhp, void *data)
16144577Sahrens {
16154577Sahrens 	upgrade_cbdata_t *cb = data;
16164577Sahrens 	int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
16174577Sahrens 
16184577Sahrens 	/* list if it's old/new */
16194577Sahrens 	if ((!cb->cb_newer && version < ZPL_VERSION) ||
16205367Sahrens 	    (cb->cb_newer && version > ZPL_VERSION)) {
16214577Sahrens 		char *str;
16224577Sahrens 		if (cb->cb_newer) {
16234577Sahrens 			str = gettext("The following filesystems are "
16244577Sahrens 			    "formatted using a newer software version and\n"
16254577Sahrens 			    "cannot be accessed on the current system.\n\n");
16264577Sahrens 		} else {
16274577Sahrens 			str = gettext("The following filesystems are "
16284577Sahrens 			    "out of date, and can be upgraded.  After being\n"
16294577Sahrens 			    "upgraded, these filesystems (and any 'zfs send' "
16304577Sahrens 			    "streams generated from\n"
16314577Sahrens 			    "subsequent snapshots) will no longer be "
16324577Sahrens 			    "accessible by older software versions.\n\n");
16334577Sahrens 		}
16344577Sahrens 
16354577Sahrens 		if (!cb->cb_foundone) {
16364577Sahrens 			(void) puts(str);
16374577Sahrens 			(void) printf(gettext("VER  FILESYSTEM\n"));
16384577Sahrens 			(void) printf(gettext("---  ------------\n"));
16394577Sahrens 			cb->cb_foundone = B_TRUE;
16404577Sahrens 		}
16414577Sahrens 
16424577Sahrens 		(void) printf("%2u   %s\n", version, zfs_get_name(zhp));
16434577Sahrens 	}
16444577Sahrens 
16454577Sahrens 	return (0);
16464577Sahrens }
16474577Sahrens 
16484577Sahrens static int
16494577Sahrens upgrade_set_callback(zfs_handle_t *zhp, void *data)
16504577Sahrens {
16514577Sahrens 	upgrade_cbdata_t *cb = data;
16524577Sahrens 	int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
165311935SMark.Shellenbaum@Sun.COM 	int needed_spa_version;
165411935SMark.Shellenbaum@Sun.COM 	int spa_version;
165511935SMark.Shellenbaum@Sun.COM 
165611935SMark.Shellenbaum@Sun.COM 	if (zfs_spa_version(zhp, &spa_version) < 0)
165711935SMark.Shellenbaum@Sun.COM 		return (-1);
165811935SMark.Shellenbaum@Sun.COM 
165911935SMark.Shellenbaum@Sun.COM 	needed_spa_version = zfs_spa_version_map(cb->cb_version);
166011935SMark.Shellenbaum@Sun.COM 
166111935SMark.Shellenbaum@Sun.COM 	if (needed_spa_version < 0)
166211935SMark.Shellenbaum@Sun.COM 		return (-1);
166311935SMark.Shellenbaum@Sun.COM 
166411935SMark.Shellenbaum@Sun.COM 	if (spa_version < needed_spa_version) {
166511935SMark.Shellenbaum@Sun.COM 		/* can't upgrade */
166611935SMark.Shellenbaum@Sun.COM 		(void) printf(gettext("%s: can not be "
166711935SMark.Shellenbaum@Sun.COM 		    "upgraded; the pool version needs to first "
166811935SMark.Shellenbaum@Sun.COM 		    "be upgraded\nto version %d\n\n"),
166911935SMark.Shellenbaum@Sun.COM 		    zfs_get_name(zhp), needed_spa_version);
167011935SMark.Shellenbaum@Sun.COM 		cb->cb_numfailed++;
167111935SMark.Shellenbaum@Sun.COM 		return (0);
16725331Samw 	}
16735331Samw 
16744577Sahrens 	/* upgrade */
16754577Sahrens 	if (version < cb->cb_version) {
16764577Sahrens 		char verstr[16];
16774849Sahrens 		(void) snprintf(verstr, sizeof (verstr),
16784849Sahrens 		    "%llu", cb->cb_version);
16794577Sahrens 		if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
16804577Sahrens 			/*
16814577Sahrens 			 * If they did "zfs upgrade -a", then we could
16824577Sahrens 			 * be doing ioctls to different pools.  We need
16834577Sahrens 			 * to log this history once to each pool.
16844577Sahrens 			 */
16854988Sek110237 			verify(zpool_stage_history(g_zfs, history_str) == 0);
16864577Sahrens 		}
16874577Sahrens 		if (zfs_prop_set(zhp, "version", verstr) == 0)
16884577Sahrens 			cb->cb_numupgraded++;
16894849Sahrens 		else
16904849Sahrens 			cb->cb_numfailed++;
16914577Sahrens 		(void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
16924577Sahrens 	} else if (version > cb->cb_version) {
16934577Sahrens 		/* can't downgrade */
16944577Sahrens 		(void) printf(gettext("%s: can not be downgraded; "
16954577Sahrens 		    "it is already at version %u\n"),
16964577Sahrens 		    zfs_get_name(zhp), version);
16974849Sahrens 		cb->cb_numfailed++;
16984577Sahrens 	} else {
16994577Sahrens 		cb->cb_numsamegraded++;
17004577Sahrens 	}
17014577Sahrens 	return (0);
17024577Sahrens }
17034577Sahrens 
17044577Sahrens /*
17054577Sahrens  * zfs upgrade
17064577Sahrens  * zfs upgrade -v
17074577Sahrens  * zfs upgrade [-r] [-V <version>] <-a | filesystem>
17084577Sahrens  */
17094577Sahrens static int
17104577Sahrens zfs_do_upgrade(int argc, char **argv)
17114577Sahrens {
17124577Sahrens 	boolean_t all = B_FALSE;
17134577Sahrens 	boolean_t showversions = B_FALSE;
17144577Sahrens 	int ret;
17154577Sahrens 	upgrade_cbdata_t cb = { 0 };
17164577Sahrens 	char c;
17177538SRichard.Morris@Sun.COM 	int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
17184577Sahrens 
17194577Sahrens 	/* check options */
17204577Sahrens 	while ((c = getopt(argc, argv, "rvV:a")) != -1) {
17214577Sahrens 		switch (c) {
17224577Sahrens 		case 'r':
17237538SRichard.Morris@Sun.COM 			flags |= ZFS_ITER_RECURSE;
17244577Sahrens 			break;
17254577Sahrens 		case 'v':
17264577Sahrens 			showversions = B_TRUE;
17274577Sahrens 			break;
17284577Sahrens 		case 'V':
17294577Sahrens 			if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
17304577Sahrens 			    optarg, &cb.cb_version) != 0) {
17314577Sahrens 				(void) fprintf(stderr,
17324577Sahrens 				    gettext("invalid version %s\n"), optarg);
17334577Sahrens 				usage(B_FALSE);
17344577Sahrens 			}
17354577Sahrens 			break;
17364577Sahrens 		case 'a':
17374577Sahrens 			all = B_TRUE;
17384577Sahrens 			break;
17394577Sahrens 		case '?':
17404577Sahrens 		default:
17414577Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
17424577Sahrens 			    optopt);
17434577Sahrens 			usage(B_FALSE);
17444577Sahrens 		}
17454577Sahrens 	}
17464577Sahrens 
17474577Sahrens 	argc -= optind;
17484577Sahrens 	argv += optind;
17494577Sahrens 
17507538SRichard.Morris@Sun.COM 	if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
17514577Sahrens 		usage(B_FALSE);
17527538SRichard.Morris@Sun.COM 	if (showversions && (flags & ZFS_ITER_RECURSE || all ||
17537538SRichard.Morris@Sun.COM 	    cb.cb_version || argc))
17544577Sahrens 		usage(B_FALSE);
17554577Sahrens 	if ((all || argc) && (showversions))
17564577Sahrens 		usage(B_FALSE);
17574577Sahrens 	if (all && argc)
17584577Sahrens 		usage(B_FALSE);
17594577Sahrens 
17604577Sahrens 	if (showversions) {
17614577Sahrens 		/* Show info on available versions. */
17624577Sahrens 		(void) printf(gettext("The following filesystem versions are "
17634577Sahrens 		    "supported:\n\n"));
17644577Sahrens 		(void) printf(gettext("VER  DESCRIPTION\n"));
17654577Sahrens 		(void) printf("---  -----------------------------------------"
17664577Sahrens 		    "---------------\n");
17674577Sahrens 		(void) printf(gettext(" 1   Initial ZFS filesystem version\n"));
17684577Sahrens 		(void) printf(gettext(" 2   Enhanced directory entries\n"));
17695331Samw 		(void) printf(gettext(" 3   Case insensitive and File system "
177010228SStephanie.Scheffler@Sun.COM 		    "unique identifier (FUID)\n"));
17719396SMatthew.Ahrens@Sun.COM 		(void) printf(gettext(" 4   userquota, groupquota "
17729396SMatthew.Ahrens@Sun.COM 		    "properties\n"));
177311935SMark.Shellenbaum@Sun.COM 		(void) printf(gettext(" 5   System attributes\n"));
17744577Sahrens 		(void) printf(gettext("\nFor more information on a particular "
177512321SStephanie.Scheffler@Sun.COM 		    "version, including supported releases,\n"));
177612321SStephanie.Scheffler@Sun.COM 		(void) printf("see the ZFS Administration Guide.\n\n");
17774577Sahrens 		ret = 0;
17784577Sahrens 	} else if (argc || all) {
17794577Sahrens 		/* Upgrade filesystems */
17804577Sahrens 		if (cb.cb_version == 0)
17814577Sahrens 			cb.cb_version = ZPL_VERSION;
17827538SRichard.Morris@Sun.COM 		ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
17839365SChris.Gerhard@sun.com 		    NULL, NULL, 0, upgrade_set_callback, &cb);
17844577Sahrens 		(void) printf(gettext("%llu filesystems upgraded\n"),
17854577Sahrens 		    cb.cb_numupgraded);
17864577Sahrens 		if (cb.cb_numsamegraded) {
17874577Sahrens 			(void) printf(gettext("%llu filesystems already at "
17884577Sahrens 			    "this version\n"),
17894577Sahrens 			    cb.cb_numsamegraded);
17904577Sahrens 		}
17914849Sahrens 		if (cb.cb_numfailed != 0)
17924577Sahrens 			ret = 1;
17934577Sahrens 	} else {
17944577Sahrens 		/* List old-version filesytems */
17954577Sahrens 		boolean_t found;
17964577Sahrens 		(void) printf(gettext("This system is currently running "
17974577Sahrens 		    "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
17984577Sahrens 
17997538SRichard.Morris@Sun.COM 		flags |= ZFS_ITER_RECURSE;
18007538SRichard.Morris@Sun.COM 		ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
18019365SChris.Gerhard@sun.com 		    NULL, NULL, 0, upgrade_list_callback, &cb);
18024577Sahrens 
18034577Sahrens 		found = cb.cb_foundone;
18044577Sahrens 		cb.cb_foundone = B_FALSE;
18054577Sahrens 		cb.cb_newer = B_TRUE;
18064577Sahrens 
18077538SRichard.Morris@Sun.COM 		ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
18089365SChris.Gerhard@sun.com 		    NULL, NULL, 0, upgrade_list_callback, &cb);
18094577Sahrens 
18104577Sahrens 		if (!cb.cb_foundone && !found) {
18114577Sahrens 			(void) printf(gettext("All filesystems are "
18124577Sahrens 			    "formatted with the current version.\n"));
18134577Sahrens 		}
18144577Sahrens 	}
18154577Sahrens 
18164577Sahrens 	return (ret);
18174577Sahrens }
18184577Sahrens 
1819789Sahrens /*
18209396SMatthew.Ahrens@Sun.COM  * zfs userspace
18219396SMatthew.Ahrens@Sun.COM  */
18229554SMatthew.Ahrens@Sun.COM static int
18239396SMatthew.Ahrens@Sun.COM userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
18249396SMatthew.Ahrens@Sun.COM {
18259396SMatthew.Ahrens@Sun.COM 	zfs_userquota_prop_t *typep = arg;
18269396SMatthew.Ahrens@Sun.COM 	zfs_userquota_prop_t p = *typep;
18279554SMatthew.Ahrens@Sun.COM 	char *name = NULL;
18289554SMatthew.Ahrens@Sun.COM 	char *ug, *propname;
18299396SMatthew.Ahrens@Sun.COM 	char namebuf[32];
18309396SMatthew.Ahrens@Sun.COM 	char sizebuf[32];
18319396SMatthew.Ahrens@Sun.COM 
18329396SMatthew.Ahrens@Sun.COM 	if (domain == NULL || domain[0] == '\0') {
18339396SMatthew.Ahrens@Sun.COM 		if (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) {
18349396SMatthew.Ahrens@Sun.COM 			struct group *g = getgrgid(rid);
18359396SMatthew.Ahrens@Sun.COM 			if (g)
18369396SMatthew.Ahrens@Sun.COM 				name = g->gr_name;
18379396SMatthew.Ahrens@Sun.COM 		} else {
18389396SMatthew.Ahrens@Sun.COM 			struct passwd *p = getpwuid(rid);
18399396SMatthew.Ahrens@Sun.COM 			if (p)
18409396SMatthew.Ahrens@Sun.COM 				name = p->pw_name;
18419396SMatthew.Ahrens@Sun.COM 		}
18429396SMatthew.Ahrens@Sun.COM 	}
18439396SMatthew.Ahrens@Sun.COM 
18449396SMatthew.Ahrens@Sun.COM 	if (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA)
18459396SMatthew.Ahrens@Sun.COM 		ug = "group";
18469396SMatthew.Ahrens@Sun.COM 	else
18479396SMatthew.Ahrens@Sun.COM 		ug = "user";
18489396SMatthew.Ahrens@Sun.COM 
18499396SMatthew.Ahrens@Sun.COM 	if (p == ZFS_PROP_USERUSED || p == ZFS_PROP_GROUPUSED)
18509396SMatthew.Ahrens@Sun.COM 		propname = "used";
18519396SMatthew.Ahrens@Sun.COM 	else
18529396SMatthew.Ahrens@Sun.COM 		propname = "quota";
18539396SMatthew.Ahrens@Sun.COM 
18549554SMatthew.Ahrens@Sun.COM 	if (name == NULL) {
18559396SMatthew.Ahrens@Sun.COM 		(void) snprintf(namebuf, sizeof (namebuf),
18569396SMatthew.Ahrens@Sun.COM 		    "%llu", (longlong_t)rid);
18579396SMatthew.Ahrens@Sun.COM 		name = namebuf;
18589396SMatthew.Ahrens@Sun.COM 	}
18599396SMatthew.Ahrens@Sun.COM 	zfs_nicenum(space, sizebuf, sizeof (sizebuf));
18609396SMatthew.Ahrens@Sun.COM 
18619396SMatthew.Ahrens@Sun.COM 	(void) printf("%s %s %s%c%s %s\n", propname, ug, domain,
18629396SMatthew.Ahrens@Sun.COM 	    domain[0] ? '-' : ' ', name, sizebuf);
18639554SMatthew.Ahrens@Sun.COM 
18649554SMatthew.Ahrens@Sun.COM 	return (0);
18659396SMatthew.Ahrens@Sun.COM }
18669396SMatthew.Ahrens@Sun.COM 
18679396SMatthew.Ahrens@Sun.COM static int
18689396SMatthew.Ahrens@Sun.COM zfs_do_userspace(int argc, char **argv)
18699396SMatthew.Ahrens@Sun.COM {
18709396SMatthew.Ahrens@Sun.COM 	zfs_handle_t *zhp;
18719396SMatthew.Ahrens@Sun.COM 	zfs_userquota_prop_t p;
18729396SMatthew.Ahrens@Sun.COM 	int error;
18739396SMatthew.Ahrens@Sun.COM 
18749396SMatthew.Ahrens@Sun.COM 	/*
18759396SMatthew.Ahrens@Sun.COM 	 * Try the python version.  If the execv fails, we'll continue
18769396SMatthew.Ahrens@Sun.COM 	 * and do a simplistic implementation.
18779396SMatthew.Ahrens@Sun.COM 	 */
18789396SMatthew.Ahrens@Sun.COM 	(void) execv(pypath, argv-1);
18799396SMatthew.Ahrens@Sun.COM 
18809396SMatthew.Ahrens@Sun.COM 	(void) printf("internal error: %s not found\n"
18819396SMatthew.Ahrens@Sun.COM 	    "falling back on built-in implementation, "
18829396SMatthew.Ahrens@Sun.COM 	    "some features will not work\n", pypath);
18839396SMatthew.Ahrens@Sun.COM 
18849396SMatthew.Ahrens@Sun.COM 	if ((zhp = zfs_open(g_zfs, argv[argc-1], ZFS_TYPE_DATASET)) == NULL)
18859396SMatthew.Ahrens@Sun.COM 		return (1);
18869396SMatthew.Ahrens@Sun.COM 
18879396SMatthew.Ahrens@Sun.COM 	(void) printf("PROP TYPE NAME VALUE\n");
18889396SMatthew.Ahrens@Sun.COM 
18899396SMatthew.Ahrens@Sun.COM 	for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
18909396SMatthew.Ahrens@Sun.COM 		error = zfs_userspace(zhp, p, userspace_cb, &p);
18919396SMatthew.Ahrens@Sun.COM 		if (error)
18929396SMatthew.Ahrens@Sun.COM 			break;
18939396SMatthew.Ahrens@Sun.COM 	}
18949396SMatthew.Ahrens@Sun.COM 	return (error);
18959396SMatthew.Ahrens@Sun.COM }
18969396SMatthew.Ahrens@Sun.COM 
18979396SMatthew.Ahrens@Sun.COM /*
18989365SChris.Gerhard@sun.com  * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
18992379Ssjelinek  *      [-s property [-s property]...] [-S property [-S property]...]
19002379Ssjelinek  *      <dataset> ...
1901789Sahrens  *
190211022STom.Erickson@Sun.COM  *	-r	Recurse over all children
190311022STom.Erickson@Sun.COM  *	-d	Limit recursion by depth.
190411022STom.Erickson@Sun.COM  *	-H	Scripted mode; elide headers and separate columns by tabs
190511022STom.Erickson@Sun.COM  *	-o	Control which fields to display.
190611022STom.Erickson@Sun.COM  *	-t	Control which object types to display.
19072379Ssjelinek  *	-s	Specify sort columns, descending order.
19082379Ssjelinek  *	-S	Specify sort columns, ascending order.
1909789Sahrens  *
1910789Sahrens  * When given no arguments, lists all filesystems in the system.
1911789Sahrens  * Otherwise, list the specified datasets, optionally recursing down them if
1912789Sahrens  * '-r' is specified.
1913789Sahrens  */
1914789Sahrens typedef struct list_cbdata {
19152082Seschrock 	boolean_t	cb_first;
19162082Seschrock 	boolean_t	cb_scripted;
19175094Slling 	zprop_list_t	*cb_proplist;
1918789Sahrens } list_cbdata_t;
1919789Sahrens 
1920789Sahrens /*
1921789Sahrens  * Given a list of columns to display, output appropriate headers for each one.
1922789Sahrens  */
1923789Sahrens static void
19245094Slling print_header(zprop_list_t *pl)
1925789Sahrens {
19262676Seschrock 	char headerbuf[ZFS_MAXPROPLEN];
19272676Seschrock 	const char *header;
1928789Sahrens 	int i;
19292676Seschrock 	boolean_t first = B_TRUE;
19302676Seschrock 	boolean_t right_justify;
19312676Seschrock 
19322676Seschrock 	for (; pl != NULL; pl = pl->pl_next) {
19332676Seschrock 		if (!first) {
1934789Sahrens 			(void) printf("  ");
19352676Seschrock 		} else {
19362676Seschrock 			first = B_FALSE;
19372676Seschrock 		}
19382676Seschrock 
19392676Seschrock 		right_justify = B_FALSE;
19405094Slling 		if (pl->pl_prop != ZPROP_INVAL) {
19412676Seschrock 			header = zfs_prop_column_name(pl->pl_prop);
19422676Seschrock 			right_justify = zfs_prop_align_right(pl->pl_prop);
19432676Seschrock 		} else {
19442676Seschrock 			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
19452676Seschrock 				headerbuf[i] = toupper(pl->pl_user_prop[i]);
19462676Seschrock 			headerbuf[i] = '\0';
19472676Seschrock 			header = headerbuf;
19482676Seschrock 		}
19492676Seschrock 
19502676Seschrock 		if (pl->pl_next == NULL && !right_justify)
19512676Seschrock 			(void) printf("%s", header);
19522676Seschrock 		else if (right_justify)
19532676Seschrock 			(void) printf("%*s", pl->pl_width, header);
19542676Seschrock 		else
19552676Seschrock 			(void) printf("%-*s", pl->pl_width, header);
1956789Sahrens 	}
1957789Sahrens 
1958789Sahrens 	(void) printf("\n");
1959789Sahrens }
1960789Sahrens 
1961789Sahrens /*
1962789Sahrens  * Given a dataset and a list of fields, print out all the properties according
1963789Sahrens  * to the described layout.
1964789Sahrens  */
1965789Sahrens static void
19665094Slling print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
1967789Sahrens {
19682676Seschrock 	boolean_t first = B_TRUE;
1969789Sahrens 	char property[ZFS_MAXPROPLEN];
19702676Seschrock 	nvlist_t *userprops = zfs_get_user_props(zhp);
19712676Seschrock 	nvlist_t *propval;
19722676Seschrock 	char *propstr;
19732676Seschrock 	boolean_t right_justify;
19742676Seschrock 	int width;
19752676Seschrock 
19762676Seschrock 	for (; pl != NULL; pl = pl->pl_next) {
19772676Seschrock 		if (!first) {
1978789Sahrens 			if (scripted)
1979789Sahrens 				(void) printf("\t");
1980789Sahrens 			else
1981789Sahrens 				(void) printf("  ");
19822676Seschrock 		} else {
19832676Seschrock 			first = B_FALSE;
1984789Sahrens 		}
1985789Sahrens 
19865094Slling 		if (pl->pl_prop != ZPROP_INVAL) {
19872676Seschrock 			if (zfs_prop_get(zhp, pl->pl_prop, property,
19882676Seschrock 			    sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
19892676Seschrock 				propstr = "-";
19902676Seschrock 			else
19912676Seschrock 				propstr = property;
19922676Seschrock 
19932676Seschrock 			right_justify = zfs_prop_align_right(pl->pl_prop);
19949396SMatthew.Ahrens@Sun.COM 		} else if (zfs_prop_userquota(pl->pl_user_prop)) {
19959396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
19969396SMatthew.Ahrens@Sun.COM 			    property, sizeof (property), B_FALSE) != 0)
19979396SMatthew.Ahrens@Sun.COM 				propstr = "-";
19989396SMatthew.Ahrens@Sun.COM 			else
19999396SMatthew.Ahrens@Sun.COM 				propstr = property;
20009396SMatthew.Ahrens@Sun.COM 			right_justify = B_TRUE;
20012676Seschrock 		} else {
20022676Seschrock 			if (nvlist_lookup_nvlist(userprops,
20032676Seschrock 			    pl->pl_user_prop, &propval) != 0)
20042676Seschrock 				propstr = "-";
20052676Seschrock 			else
20062676Seschrock 				verify(nvlist_lookup_string(propval,
20075094Slling 				    ZPROP_VALUE, &propstr) == 0);
20089396SMatthew.Ahrens@Sun.COM 			right_justify = B_FALSE;
20092676Seschrock 		}
20102676Seschrock 
20112676Seschrock 		width = pl->pl_width;
2012789Sahrens 
2013866Seschrock 		/*
2014866Seschrock 		 * If this is being called in scripted mode, or if this is the
2015866Seschrock 		 * last column and it is left-justified, don't include a width
2016866Seschrock 		 * format specifier.
2017866Seschrock 		 */
20182676Seschrock 		if (scripted || (pl->pl_next == NULL && !right_justify))
20192676Seschrock 			(void) printf("%s", propstr);
20202676Seschrock 		else if (right_justify)
20212676Seschrock 			(void) printf("%*s", width, propstr);
20222676Seschrock 		else
20232676Seschrock 			(void) printf("%-*s", width, propstr);
2024789Sahrens 	}
2025789Sahrens 
2026789Sahrens 	(void) printf("\n");
2027789Sahrens }
2028789Sahrens 
2029789Sahrens /*
2030789Sahrens  * Generic callback function to list a dataset or snapshot.
2031789Sahrens  */
2032789Sahrens static int
2033789Sahrens list_callback(zfs_handle_t *zhp, void *data)
2034789Sahrens {
2035789Sahrens 	list_cbdata_t *cbp = data;
2036789Sahrens 
2037789Sahrens 	if (cbp->cb_first) {
2038789Sahrens 		if (!cbp->cb_scripted)
20392676Seschrock 			print_header(cbp->cb_proplist);
20402082Seschrock 		cbp->cb_first = B_FALSE;
2041789Sahrens 	}
2042789Sahrens 
20432676Seschrock 	print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
2044789Sahrens 
2045789Sahrens 	return (0);
2046789Sahrens }
2047789Sahrens 
2048789Sahrens static int
2049789Sahrens zfs_do_list(int argc, char **argv)
2050789Sahrens {
2051789Sahrens 	int c;
20522082Seschrock 	boolean_t scripted = B_FALSE;
2053789Sahrens 	static char default_fields[] =
2054789Sahrens 	    "name,used,available,referenced,mountpoint";
20558415SRichard.Morris@Sun.COM 	int types = ZFS_TYPE_DATASET;
20567538SRichard.Morris@Sun.COM 	boolean_t types_specified = B_FALSE;
2057789Sahrens 	char *fields = NULL;
2058789Sahrens 	list_cbdata_t cb = { 0 };
2059789Sahrens 	char *value;
20609365SChris.Gerhard@sun.com 	int limit = 0;
2061789Sahrens 	int ret;
20622379Ssjelinek 	zfs_sort_column_t *sortcol = NULL;
20637538SRichard.Morris@Sun.COM 	int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
2064789Sahrens 
2065789Sahrens 	/* check options */
20669365SChris.Gerhard@sun.com 	while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
2067789Sahrens 		switch (c) {
2068789Sahrens 		case 'o':
2069789Sahrens 			fields = optarg;
2070789Sahrens 			break;
20719365SChris.Gerhard@sun.com 		case 'd':
20729365SChris.Gerhard@sun.com 			limit = parse_depth(optarg, &flags);
20739365SChris.Gerhard@sun.com 			break;
2074789Sahrens 		case 'r':
20757538SRichard.Morris@Sun.COM 			flags |= ZFS_ITER_RECURSE;
2076789Sahrens 			break;
2077789Sahrens 		case 'H':
20782082Seschrock 			scripted = B_TRUE;
2079789Sahrens 			break;
20802379Ssjelinek 		case 's':
20812676Seschrock 			if (zfs_add_sort_column(&sortcol, optarg,
20822676Seschrock 			    B_FALSE) != 0) {
20832379Ssjelinek 				(void) fprintf(stderr,
20842379Ssjelinek 				    gettext("invalid property '%s'\n"), optarg);
20852379Ssjelinek 				usage(B_FALSE);
20862379Ssjelinek 			}
20872379Ssjelinek 			break;
20882379Ssjelinek 		case 'S':
20892676Seschrock 			if (zfs_add_sort_column(&sortcol, optarg,
20902676Seschrock 			    B_TRUE) != 0) {
20912379Ssjelinek 				(void) fprintf(stderr,
20922379Ssjelinek 				    gettext("invalid property '%s'\n"), optarg);
20932379Ssjelinek 				usage(B_FALSE);
20942379Ssjelinek 			}
20952379Ssjelinek 			break;
2096789Sahrens 		case 't':
2097789Sahrens 			types = 0;
20987538SRichard.Morris@Sun.COM 			types_specified = B_TRUE;
20997538SRichard.Morris@Sun.COM 			flags &= ~ZFS_ITER_PROP_LISTSNAPS;
2100789Sahrens 			while (*optarg != '\0') {
21017538SRichard.Morris@Sun.COM 				static char *type_subopts[] = { "filesystem",
21027538SRichard.Morris@Sun.COM 				    "volume", "snapshot", "all", NULL };
21037538SRichard.Morris@Sun.COM 
2104789Sahrens 				switch (getsubopt(&optarg, type_subopts,
2105789Sahrens 				    &value)) {
2106789Sahrens 				case 0:
2107789Sahrens 					types |= ZFS_TYPE_FILESYSTEM;
2108789Sahrens 					break;
2109789Sahrens 				case 1:
2110789Sahrens 					types |= ZFS_TYPE_VOLUME;
2111789Sahrens 					break;
2112789Sahrens 				case 2:
2113789Sahrens 					types |= ZFS_TYPE_SNAPSHOT;
2114789Sahrens 					break;
21157538SRichard.Morris@Sun.COM 				case 3:
21167538SRichard.Morris@Sun.COM 					types = ZFS_TYPE_DATASET;
21177538SRichard.Morris@Sun.COM 					break;
21187538SRichard.Morris@Sun.COM 
2119789Sahrens 				default:
2120789Sahrens 					(void) fprintf(stderr,
2121789Sahrens 					    gettext("invalid type '%s'\n"),
2122789Sahrens 					    value);
21232082Seschrock 					usage(B_FALSE);
2124789Sahrens 				}
2125789Sahrens 			}
2126789Sahrens 			break;
2127789Sahrens 		case ':':
2128789Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
2129789Sahrens 			    "'%c' option\n"), optopt);
21302082Seschrock 			usage(B_FALSE);
2131789Sahrens 			break;
2132789Sahrens 		case '?':
2133789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2134789Sahrens 			    optopt);
21352082Seschrock 			usage(B_FALSE);
2136789Sahrens 		}
2137789Sahrens 	}
2138789Sahrens 
2139789Sahrens 	argc -= optind;
2140789Sahrens 	argv += optind;
2141789Sahrens 
2142789Sahrens 	if (fields == NULL)
21437390SMatthew.Ahrens@Sun.COM 		fields = default_fields;
21447390SMatthew.Ahrens@Sun.COM 
21457390SMatthew.Ahrens@Sun.COM 	/*
21467538SRichard.Morris@Sun.COM 	 * If "-o space" and no types were specified, don't display snapshots.
21477390SMatthew.Ahrens@Sun.COM 	 */
21487538SRichard.Morris@Sun.COM 	if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
21497390SMatthew.Ahrens@Sun.COM 		types &= ~ZFS_TYPE_SNAPSHOT;
2150789Sahrens 
2151866Seschrock 	/*
21525094Slling 	 * If the user specifies '-o all', the zprop_get_list() doesn't
2153866Seschrock 	 * normally include the name of the dataset.  For 'zfs list', we always
2154866Seschrock 	 * want this property to be first.
2155866Seschrock 	 */
21565094Slling 	if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
21575094Slling 	    != 0)
21582082Seschrock 		usage(B_FALSE);
21592676Seschrock 
2160789Sahrens 	cb.cb_scripted = scripted;
21612082Seschrock 	cb.cb_first = B_TRUE;
2162789Sahrens 
21637538SRichard.Morris@Sun.COM 	ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
21649365SChris.Gerhard@sun.com 	    limit, list_callback, &cb);
21652379Ssjelinek 
21665094Slling 	zprop_free_list(cb.cb_proplist);
21672379Ssjelinek 	zfs_free_sort_columns(sortcol);
2168789Sahrens 
21694221Smmusante 	if (ret == 0 && cb.cb_first && !cb.cb_scripted)
2170789Sahrens 		(void) printf(gettext("no datasets available\n"));
2171789Sahrens 
2172789Sahrens 	return (ret);
2173789Sahrens }
2174789Sahrens 
2175789Sahrens /*
21764490Svb160487  * zfs rename <fs | snap | vol> <fs | snap | vol>
21774490Svb160487  * zfs rename -p <fs | vol> <fs | vol>
21784490Svb160487  * zfs rename -r <snap> <snap>
2179789Sahrens  *
2180789Sahrens  * Renames the given dataset to another of the same type.
21814490Svb160487  *
21824490Svb160487  * The '-p' flag creates all the non-existing ancestors of the target first.
2183789Sahrens  */
2184789Sahrens /* ARGSUSED */
2185789Sahrens static int
2186789Sahrens zfs_do_rename(int argc, char **argv)
2187789Sahrens {
2188789Sahrens 	zfs_handle_t *zhp;
21894007Smmusante 	int c;
21902082Seschrock 	int ret;
21914490Svb160487 	boolean_t recurse = B_FALSE;
21924490Svb160487 	boolean_t parents = B_FALSE;
2193789Sahrens 
2194789Sahrens 	/* check options */
21954490Svb160487 	while ((c = getopt(argc, argv, "pr")) != -1) {
21964007Smmusante 		switch (c) {
21974490Svb160487 		case 'p':
21984490Svb160487 			parents = B_TRUE;
21994490Svb160487 			break;
22004007Smmusante 		case 'r':
22014490Svb160487 			recurse = B_TRUE;
22024007Smmusante 			break;
22034007Smmusante 		case '?':
22044007Smmusante 		default:
22054007Smmusante 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
22064007Smmusante 			    optopt);
22074007Smmusante 			usage(B_FALSE);
22084007Smmusante 		}
2209789Sahrens 	}
2210789Sahrens 
22114007Smmusante 	argc -= optind;
22124007Smmusante 	argv += optind;
22134007Smmusante 
2214789Sahrens 	/* check number of arguments */
22154007Smmusante 	if (argc < 1) {
2216789Sahrens 		(void) fprintf(stderr, gettext("missing source dataset "
2217789Sahrens 		    "argument\n"));
22182082Seschrock 		usage(B_FALSE);
2219789Sahrens 	}
22204007Smmusante 	if (argc < 2) {
2221789Sahrens 		(void) fprintf(stderr, gettext("missing target dataset "
2222789Sahrens 		    "argument\n"));
22232082Seschrock 		usage(B_FALSE);
2224789Sahrens 	}
22254007Smmusante 	if (argc > 2) {
2226789Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
22272082Seschrock 		usage(B_FALSE);
2228789Sahrens 	}
2229789Sahrens 
22304490Svb160487 	if (recurse && parents) {
22314490Svb160487 		(void) fprintf(stderr, gettext("-p and -r options are mutually "
22324490Svb160487 		    "exclusive\n"));
22334490Svb160487 		usage(B_FALSE);
22344490Svb160487 	}
22354490Svb160487 
22364007Smmusante 	if (recurse && strchr(argv[0], '@') == 0) {
22374007Smmusante 		(void) fprintf(stderr, gettext("source dataset for recursive "
22384007Smmusante 		    "rename must be a snapshot\n"));
22394007Smmusante 		usage(B_FALSE);
22404007Smmusante 	}
22414007Smmusante 
22424490Svb160487 	if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
22435094Slling 	    ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
2244789Sahrens 		return (1);
2245789Sahrens 
22464490Svb160487 	/* If we were asked and the name looks good, try to create ancestors. */
22474490Svb160487 	if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
22484490Svb160487 	    zfs_create_ancestors(g_zfs, argv[1]) != 0) {
22494490Svb160487 		zfs_close(zhp);
22504490Svb160487 		return (1);
22514490Svb160487 	}
22524490Svb160487 
22534007Smmusante 	ret = (zfs_rename(zhp, argv[1], recurse) != 0);
22542082Seschrock 
22552082Seschrock 	zfs_close(zhp);
22562082Seschrock 	return (ret);
22572082Seschrock }
22582082Seschrock 
22592082Seschrock /*
22602082Seschrock  * zfs promote <fs>
22612082Seschrock  *
22622082Seschrock  * Promotes the given clone fs to be the parent
22632082Seschrock  */
22642082Seschrock /* ARGSUSED */
22652082Seschrock static int
22662082Seschrock zfs_do_promote(int argc, char **argv)
22672082Seschrock {
22682082Seschrock 	zfs_handle_t *zhp;
22692082Seschrock 	int ret;
22702082Seschrock 
22712082Seschrock 	/* check options */
22722082Seschrock 	if (argc > 1 && argv[1][0] == '-') {
22732082Seschrock 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
22742082Seschrock 		    argv[1][1]);
22752082Seschrock 		usage(B_FALSE);
22762082Seschrock 	}
22772082Seschrock 
22782082Seschrock 	/* check number of arguments */
22792082Seschrock 	if (argc < 2) {
22802082Seschrock 		(void) fprintf(stderr, gettext("missing clone filesystem"
22812597Snd150628 		    " argument\n"));
22822082Seschrock 		usage(B_FALSE);
22832082Seschrock 	}
22842082Seschrock 	if (argc > 2) {
22852082Seschrock 		(void) fprintf(stderr, gettext("too many arguments\n"));
22862082Seschrock 		usage(B_FALSE);
22872082Seschrock 	}
22882082Seschrock 
22892082Seschrock 	zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
22902082Seschrock 	if (zhp == NULL)
22912082Seschrock 		return (1);
22922082Seschrock 
22932082Seschrock 	ret = (zfs_promote(zhp) != 0);
22942082Seschrock 
22952926Sek110237 
2296789Sahrens 	zfs_close(zhp);
2297789Sahrens 	return (ret);
2298789Sahrens }
2299789Sahrens 
2300789Sahrens /*
23015568Sahrens  * zfs rollback [-rRf] <snapshot>
2302789Sahrens  *
230311022STom.Erickson@Sun.COM  *	-r	Delete any intervening snapshots before doing rollback
230411022STom.Erickson@Sun.COM  *	-R	Delete any snapshots and their clones
230511022STom.Erickson@Sun.COM  *	-f	ignored for backwards compatability
2306789Sahrens  *
2307789Sahrens  * Given a filesystem, rollback to a specific snapshot, discarding any changes
2308789Sahrens  * since then and making it the active dataset.  If more recent snapshots exist,
2309789Sahrens  * the command will complain unless the '-r' flag is given.
2310789Sahrens  */
2311789Sahrens typedef struct rollback_cbdata {
2312789Sahrens 	uint64_t	cb_create;
23132082Seschrock 	boolean_t	cb_first;
2314789Sahrens 	int		cb_doclones;
2315789Sahrens 	char		*cb_target;
2316789Sahrens 	int		cb_error;
23172082Seschrock 	boolean_t	cb_recurse;
23182082Seschrock 	boolean_t	cb_dependent;
2319789Sahrens } rollback_cbdata_t;
2320789Sahrens 
2321789Sahrens /*
2322789Sahrens  * Report any snapshots more recent than the one specified.  Used when '-r' is
2323789Sahrens  * not specified.  We reuse this same callback for the snapshot dependents - if
2324789Sahrens  * 'cb_dependent' is set, then this is a dependent and we should report it
2325789Sahrens  * without checking the transaction group.
2326789Sahrens  */
2327789Sahrens static int
2328789Sahrens rollback_check(zfs_handle_t *zhp, void *data)
2329789Sahrens {
2330789Sahrens 	rollback_cbdata_t *cbp = data;
2331789Sahrens 
23322082Seschrock 	if (cbp->cb_doclones) {
23332082Seschrock 		zfs_close(zhp);
2334789Sahrens 		return (0);
23352082Seschrock 	}
2336789Sahrens 
2337789Sahrens 	if (!cbp->cb_dependent) {
2338789Sahrens 		if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
23391294Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2340789Sahrens 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
2341789Sahrens 		    cbp->cb_create) {
2342789Sahrens 
2343789Sahrens 			if (cbp->cb_first && !cbp->cb_recurse) {
2344789Sahrens 				(void) fprintf(stderr, gettext("cannot "
2345789Sahrens 				    "rollback to '%s': more recent snapshots "
2346789Sahrens 				    "exist\n"),
2347789Sahrens 				    cbp->cb_target);
2348789Sahrens 				(void) fprintf(stderr, gettext("use '-r' to "
2349789Sahrens 				    "force deletion of the following "
2350789Sahrens 				    "snapshots:\n"));
2351789Sahrens 				cbp->cb_first = 0;
2352789Sahrens 				cbp->cb_error = 1;
2353789Sahrens 			}
2354789Sahrens 
2355789Sahrens 			if (cbp->cb_recurse) {
23562082Seschrock 				cbp->cb_dependent = B_TRUE;
23572474Seschrock 				if (zfs_iter_dependents(zhp, B_TRUE,
23582474Seschrock 				    rollback_check, cbp) != 0) {
23592474Seschrock 					zfs_close(zhp);
23602474Seschrock 					return (-1);
23612474Seschrock 				}
23622082Seschrock 				cbp->cb_dependent = B_FALSE;
2363789Sahrens 			} else {
2364789Sahrens 				(void) fprintf(stderr, "%s\n",
2365789Sahrens 				    zfs_get_name(zhp));
2366789Sahrens 			}
2367789Sahrens 		}
2368789Sahrens 	} else {
2369789Sahrens 		if (cbp->cb_first && cbp->cb_recurse) {
2370789Sahrens 			(void) fprintf(stderr, gettext("cannot rollback to "
2371789Sahrens 			    "'%s': clones of previous snapshots exist\n"),
2372789Sahrens 			    cbp->cb_target);
2373789Sahrens 			(void) fprintf(stderr, gettext("use '-R' to "
2374789Sahrens 			    "force deletion of the following clones and "
2375789Sahrens 			    "dependents:\n"));
2376789Sahrens 			cbp->cb_first = 0;
2377789Sahrens 			cbp->cb_error = 1;
2378789Sahrens 		}
2379789Sahrens 
2380789Sahrens 		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
2381789Sahrens 	}
2382789Sahrens 
2383789Sahrens 	zfs_close(zhp);
2384789Sahrens 	return (0);
2385789Sahrens }
2386789Sahrens 
2387789Sahrens static int
2388789Sahrens zfs_do_rollback(int argc, char **argv)
2389789Sahrens {
2390789Sahrens 	int ret;
2391789Sahrens 	int c;
23925749Sahrens 	boolean_t force = B_FALSE;
2393789Sahrens 	rollback_cbdata_t cb = { 0 };
2394789Sahrens 	zfs_handle_t *zhp, *snap;
2395789Sahrens 	char parentname[ZFS_MAXNAMELEN];
2396789Sahrens 	char *delim;
2397789Sahrens 
2398789Sahrens 	/* check options */
23995568Sahrens 	while ((c = getopt(argc, argv, "rRf")) != -1) {
2400789Sahrens 		switch (c) {
2401789Sahrens 		case 'r':
2402789Sahrens 			cb.cb_recurse = 1;
2403789Sahrens 			break;
2404789Sahrens 		case 'R':
2405789Sahrens 			cb.cb_recurse = 1;
2406789Sahrens 			cb.cb_doclones = 1;
2407789Sahrens 			break;
24085568Sahrens 		case 'f':
24095749Sahrens 			force = B_TRUE;
24105568Sahrens 			break;
2411789Sahrens 		case '?':
2412789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2413789Sahrens 			    optopt);
24142082Seschrock 			usage(B_FALSE);
2415789Sahrens 		}
2416789Sahrens 	}
2417789Sahrens 
2418789Sahrens 	argc -= optind;
2419789Sahrens 	argv += optind;
2420789Sahrens 
2421789Sahrens 	/* check number of arguments */
2422789Sahrens 	if (argc < 1) {
2423789Sahrens 		(void) fprintf(stderr, gettext("missing dataset argument\n"));
24242082Seschrock 		usage(B_FALSE);
2425789Sahrens 	}
2426789Sahrens 	if (argc > 1) {
2427789Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
24282082Seschrock 		usage(B_FALSE);
2429789Sahrens 	}
2430789Sahrens 
2431789Sahrens 	/* open the snapshot */
24322082Seschrock 	if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
2433789Sahrens 		return (1);
2434789Sahrens 
24351294Slling 	/* open the parent dataset */
24361294Slling 	(void) strlcpy(parentname, argv[0], sizeof (parentname));
2437789Sahrens 	verify((delim = strrchr(parentname, '@')) != NULL);
2438789Sahrens 	*delim = '\0';
24395094Slling 	if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
2440789Sahrens 		zfs_close(snap);
2441789Sahrens 		return (1);
2442789Sahrens 	}
2443789Sahrens 
2444789Sahrens 	/*
2445789Sahrens 	 * Check for more recent snapshots and/or clones based on the presence
2446789Sahrens 	 * of '-r' and '-R'.
2447789Sahrens 	 */
24481294Slling 	cb.cb_target = argv[0];
24491294Slling 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
24502082Seschrock 	cb.cb_first = B_TRUE;
2451789Sahrens 	cb.cb_error = 0;
24522474Seschrock 	if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
24532474Seschrock 		goto out;
2454789Sahrens 
2455789Sahrens 	if ((ret = cb.cb_error) != 0)
2456789Sahrens 		goto out;
2457789Sahrens 
2458789Sahrens 	/*
24591294Slling 	 * Rollback parent to the given snapshot.
2460789Sahrens 	 */
24615749Sahrens 	ret = zfs_rollback(zhp, snap, force);
2462789Sahrens 
2463789Sahrens out:
2464789Sahrens 	zfs_close(snap);
2465789Sahrens 	zfs_close(zhp);
2466789Sahrens 
2467789Sahrens 	if (ret == 0)
2468789Sahrens 		return (0);
2469789Sahrens 	else
2470789Sahrens 		return (1);
2471789Sahrens }
2472789Sahrens 
2473789Sahrens /*
2474789Sahrens  * zfs set property=value { fs | snap | vol } ...
2475789Sahrens  *
2476789Sahrens  * Sets the given property for all datasets specified on the command line.
2477789Sahrens  */
2478789Sahrens typedef struct set_cbdata {
2479789Sahrens 	char		*cb_propname;
2480789Sahrens 	char		*cb_value;
2481789Sahrens } set_cbdata_t;
2482789Sahrens 
2483789Sahrens static int
2484789Sahrens set_callback(zfs_handle_t *zhp, void *data)
2485789Sahrens {
2486789Sahrens 	set_cbdata_t *cbp = data;
2487789Sahrens 
24882676Seschrock 	if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
24892169Snd150628 		switch (libzfs_errno(g_zfs)) {
24902169Snd150628 		case EZFS_MOUNTFAILED:
24912169Snd150628 			(void) fprintf(stderr, gettext("property may be set "
24922169Snd150628 			    "but unable to remount filesystem\n"));
24932169Snd150628 			break;
24943126Sahl 		case EZFS_SHARENFSFAILED:
24952169Snd150628 			(void) fprintf(stderr, gettext("property may be set "
24962169Snd150628 			    "but unable to reshare filesystem\n"));
24972169Snd150628 			break;
24982169Snd150628 		}
2499789Sahrens 		return (1);
25002169Snd150628 	}
25012856Snd150628 	return (0);
2502789Sahrens }
2503789Sahrens 
2504789Sahrens static int
2505789Sahrens zfs_do_set(int argc, char **argv)
2506789Sahrens {
2507789Sahrens 	set_cbdata_t cb;
25082926Sek110237 	int ret;
2509789Sahrens 
2510789Sahrens 	/* check for options */
2511789Sahrens 	if (argc > 1 && argv[1][0] == '-') {
2512789Sahrens 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2513789Sahrens 		    argv[1][1]);
25142082Seschrock 		usage(B_FALSE);
2515789Sahrens 	}
2516789Sahrens 
2517789Sahrens 	/* check number of arguments */
2518789Sahrens 	if (argc < 2) {
2519789Sahrens 		(void) fprintf(stderr, gettext("missing property=value "
2520789Sahrens 		    "argument\n"));
25212082Seschrock 		usage(B_FALSE);
2522789Sahrens 	}
2523789Sahrens 	if (argc < 3) {
2524789Sahrens 		(void) fprintf(stderr, gettext("missing dataset name\n"));
25252082Seschrock 		usage(B_FALSE);
2526789Sahrens 	}
2527789Sahrens 
2528789Sahrens 	/* validate property=value argument */
2529789Sahrens 	cb.cb_propname = argv[1];
25306993Ssg201626 	if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
25316993Ssg201626 	    (cb.cb_value[1] == '\0')) {
2532789Sahrens 		(void) fprintf(stderr, gettext("missing value in "
2533789Sahrens 		    "property=value argument\n"));
25342082Seschrock 		usage(B_FALSE);
2535789Sahrens 	}
2536789Sahrens 
2537789Sahrens 	*cb.cb_value = '\0';
2538789Sahrens 	cb.cb_value++;
2539789Sahrens 
2540789Sahrens 	if (*cb.cb_propname == '\0') {
2541789Sahrens 		(void) fprintf(stderr,
2542789Sahrens 		    gettext("missing property in property=value argument\n"));
25432082Seschrock 		usage(B_FALSE);
2544789Sahrens 	}
2545789Sahrens 
25467538SRichard.Morris@Sun.COM 	ret = zfs_for_each(argc - 2, argv + 2, NULL,
25479365SChris.Gerhard@sun.com 	    ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
25482926Sek110237 
25492926Sek110237 	return (ret);
2550789Sahrens }
2551789Sahrens 
2552789Sahrens /*
25537265Sahrens  * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
2554789Sahrens  *
2555789Sahrens  * Creates a snapshot with the given name.  While functionally equivalent to
25565331Samw  * 'zfs create', it is a separate command to differentiate intent.
2557789Sahrens  */
2558789Sahrens static int
2559789Sahrens zfs_do_snapshot(int argc, char **argv)
2560789Sahrens {
25614490Svb160487 	boolean_t recursive = B_FALSE;
25622199Sahrens 	int ret;
25632199Sahrens 	char c;
25647265Sahrens 	nvlist_t *props;
25657265Sahrens 
256612446SMark.Musante@Sun.COM 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
256712446SMark.Musante@Sun.COM 		nomem();
25682199Sahrens 
2569789Sahrens 	/* check options */
25707265Sahrens 	while ((c = getopt(argc, argv, "ro:")) != -1) {
25712199Sahrens 		switch (c) {
25727265Sahrens 		case 'o':
25737265Sahrens 			if (parseprop(props))
25747265Sahrens 				return (1);
25757265Sahrens 			break;
25762199Sahrens 		case 'r':
25772199Sahrens 			recursive = B_TRUE;
25782199Sahrens 			break;
25792199Sahrens 		case '?':
25802199Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
25812199Sahrens 			    optopt);
25827265Sahrens 			goto usage;
25832199Sahrens 		}
2584789Sahrens 	}
2585789Sahrens 
25862199Sahrens 	argc -= optind;
25872199Sahrens 	argv += optind;
25882199Sahrens 
2589789Sahrens 	/* check number of arguments */
25902199Sahrens 	if (argc < 1) {
2591789Sahrens 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
25927265Sahrens 		goto usage;
2593789Sahrens 	}
25942199Sahrens 	if (argc > 1) {
2595789Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
25967265Sahrens 		goto usage;
2597789Sahrens 	}
2598789Sahrens 
25997265Sahrens 	ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
26007265Sahrens 	nvlist_free(props);
26012199Sahrens 	if (ret && recursive)
26022199Sahrens 		(void) fprintf(stderr, gettext("no snapshots were created\n"));
26032199Sahrens 	return (ret != 0);
26047265Sahrens 
26057265Sahrens usage:
26067265Sahrens 	nvlist_free(props);
26077265Sahrens 	usage(B_FALSE);
26087265Sahrens 	return (-1);
2609789Sahrens }
2610789Sahrens 
2611789Sahrens /*
261211022STom.Erickson@Sun.COM  * zfs send [-vDp] -R [-i|-I <@snap>] <fs@snap>
261311022STom.Erickson@Sun.COM  * zfs send [-vDp] [-i|-I <@snap>] <fs@snap>
2614789Sahrens  *
2615789Sahrens  * Send a backup stream to stdout.
2616789Sahrens  */
2617789Sahrens static int
26181749Sahrens zfs_do_send(int argc, char **argv)
2619789Sahrens {
2620789Sahrens 	char *fromname = NULL;
26215367Sahrens 	char *toname = NULL;
26222885Sahrens 	char *cp;
26232885Sahrens 	zfs_handle_t *zhp;
262411007SLori.Alt@Sun.COM 	sendflags_t flags = { 0 };
2625789Sahrens 	int c, err;
262612296SLin.Ling@Sun.COM 	nvlist_t *dbgnv;
262712296SLin.Ling@Sun.COM 	boolean_t extraverbose = B_FALSE;
2628789Sahrens 
2629789Sahrens 	/* check options */
263011022STom.Erickson@Sun.COM 	while ((c = getopt(argc, argv, ":i:I:RDpv")) != -1) {
2631789Sahrens 		switch (c) {
2632789Sahrens 		case 'i':
26332885Sahrens 			if (fromname)
26342885Sahrens 				usage(B_FALSE);
2635789Sahrens 			fromname = optarg;
2636789Sahrens 			break;
26375367Sahrens 		case 'I':
26385367Sahrens 			if (fromname)
26395367Sahrens 				usage(B_FALSE);
26405367Sahrens 			fromname = optarg;
264111007SLori.Alt@Sun.COM 			flags.doall = B_TRUE;
26425367Sahrens 			break;
26435367Sahrens 		case 'R':
264411007SLori.Alt@Sun.COM 			flags.replicate = B_TRUE;
26455367Sahrens 			break;
264611022STom.Erickson@Sun.COM 		case 'p':
264711022STom.Erickson@Sun.COM 			flags.props = B_TRUE;
264811022STom.Erickson@Sun.COM 			break;
26495367Sahrens 		case 'v':
265012296SLin.Ling@Sun.COM 			if (flags.verbose)
265112296SLin.Ling@Sun.COM 				extraverbose = B_TRUE;
265211007SLori.Alt@Sun.COM 			flags.verbose = B_TRUE;
265311007SLori.Alt@Sun.COM 			break;
265411007SLori.Alt@Sun.COM 		case 'D':
265511007SLori.Alt@Sun.COM 			flags.dedup = B_TRUE;
26565367Sahrens 			break;
2657789Sahrens 		case ':':
2658789Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
2659789Sahrens 			    "'%c' option\n"), optopt);
26602082Seschrock 			usage(B_FALSE);
2661789Sahrens 			break;
2662789Sahrens 		case '?':
2663789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2664789Sahrens 			    optopt);
26652082Seschrock 			usage(B_FALSE);
2666789Sahrens 		}
2667789Sahrens 	}
2668789Sahrens 
2669789Sahrens 	argc -= optind;
2670789Sahrens 	argv += optind;
2671789Sahrens 
2672789Sahrens 	/* check number of arguments */
2673789Sahrens 	if (argc < 1) {
2674789Sahrens 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
26752082Seschrock 		usage(B_FALSE);
2676789Sahrens 	}
2677789Sahrens 	if (argc > 1) {
2678789Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
26792082Seschrock 		usage(B_FALSE);
2680789Sahrens 	}
2681789Sahrens 
2682789Sahrens 	if (isatty(STDOUT_FILENO)) {
2683789Sahrens 		(void) fprintf(stderr,
26842885Sahrens 		    gettext("Error: Stream can not be written to a terminal.\n"
26853912Slling 		    "You must redirect standard output.\n"));
2686789Sahrens 		return (1);
2687789Sahrens 	}
2688789Sahrens 
26895367Sahrens 	cp = strchr(argv[0], '@');
26905367Sahrens 	if (cp == NULL) {
26915367Sahrens 		(void) fprintf(stderr,
26925367Sahrens 		    gettext("argument must be a snapshot\n"));
26935367Sahrens 		usage(B_FALSE);
26945367Sahrens 	}
26955367Sahrens 	*cp = '\0';
26965367Sahrens 	toname = cp + 1;
26975367Sahrens 	zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
26985367Sahrens 	if (zhp == NULL)
26992665Snd150628 		return (1);
27002665Snd150628 
27012885Sahrens 	/*
27022885Sahrens 	 * If they specified the full path to the snapshot, chop off
27035367Sahrens 	 * everything except the short name of the snapshot, but special
27045367Sahrens 	 * case if they specify the origin.
27052885Sahrens 	 */
27062885Sahrens 	if (fromname && (cp = strchr(fromname, '@')) != NULL) {
27075367Sahrens 		char origin[ZFS_MAXNAMELEN];
27085367Sahrens 		zprop_source_t src;
27095367Sahrens 
27105367Sahrens 		(void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
27115367Sahrens 		    origin, sizeof (origin), &src, NULL, 0, B_FALSE);
27125367Sahrens 
27135367Sahrens 		if (strcmp(origin, fromname) == 0) {
27145367Sahrens 			fromname = NULL;
271511007SLori.Alt@Sun.COM 			flags.fromorigin = B_TRUE;
27165367Sahrens 		} else {
27175367Sahrens 			*cp = '\0';
27185367Sahrens 			if (cp != fromname && strcmp(argv[0], fromname)) {
27195367Sahrens 				(void) fprintf(stderr,
27205367Sahrens 				    gettext("incremental source must be "
27215367Sahrens 				    "in same filesystem\n"));
27225367Sahrens 				usage(B_FALSE);
27235367Sahrens 			}
27245367Sahrens 			fromname = cp + 1;
27255367Sahrens 			if (strchr(fromname, '@') || strchr(fromname, '/')) {
27265367Sahrens 				(void) fprintf(stderr,
27275367Sahrens 				    gettext("invalid incremental source\n"));
27285367Sahrens 				usage(B_FALSE);
27295367Sahrens 			}
27302885Sahrens 		}
2731789Sahrens 	}
2732789Sahrens 
273311007SLori.Alt@Sun.COM 	if (flags.replicate && fromname == NULL)
273411007SLori.Alt@Sun.COM 		flags.doall = B_TRUE;
273511007SLori.Alt@Sun.COM 
273612296SLin.Ling@Sun.COM 	err = zfs_send(zhp, fromname, toname, flags, STDOUT_FILENO, NULL, 0,
273712296SLin.Ling@Sun.COM 	    extraverbose ? &dbgnv : NULL);
273812296SLin.Ling@Sun.COM 
273912296SLin.Ling@Sun.COM 	if (extraverbose) {
274012296SLin.Ling@Sun.COM 		/*
274112296SLin.Ling@Sun.COM 		 * dump_nvlist prints to stdout, but that's been
274212296SLin.Ling@Sun.COM 		 * redirected to a file.  Make it print to stderr
274312296SLin.Ling@Sun.COM 		 * instead.
274412296SLin.Ling@Sun.COM 		 */
274512296SLin.Ling@Sun.COM 		(void) dup2(STDERR_FILENO, STDOUT_FILENO);
274612296SLin.Ling@Sun.COM 		dump_nvlist(dbgnv, 0);
274712296SLin.Ling@Sun.COM 		nvlist_free(dbgnv);
274812296SLin.Ling@Sun.COM 	}
27492885Sahrens 	zfs_close(zhp);
2750789Sahrens 
2751789Sahrens 	return (err != 0);
2752789Sahrens }
2753789Sahrens 
2754789Sahrens /*
275512409STom.Erickson@Sun.COM  * zfs receive [-vnFu] [-d | -e] <fs@snap>
2756789Sahrens  *
2757789Sahrens  * Restore a backup stream from stdin.
2758789Sahrens  */
2759789Sahrens static int
27601749Sahrens zfs_do_receive(int argc, char **argv)
2761789Sahrens {
2762789Sahrens 	int c, err;
276311022STom.Erickson@Sun.COM 	recvflags_t flags = { 0 };
276411022STom.Erickson@Sun.COM 
2765789Sahrens 	/* check options */
276611461STom.Erickson@Sun.COM 	while ((c = getopt(argc, argv, ":denuvF")) != -1) {
2767789Sahrens 		switch (c) {
2768789Sahrens 		case 'd':
27695367Sahrens 			flags.isprefix = B_TRUE;
2770789Sahrens 			break;
277111461STom.Erickson@Sun.COM 		case 'e':
277211461STom.Erickson@Sun.COM 			flags.isprefix = B_TRUE;
277311461STom.Erickson@Sun.COM 			flags.istail = B_TRUE;
277411461STom.Erickson@Sun.COM 			break;
2775789Sahrens 		case 'n':
27765367Sahrens 			flags.dryrun = B_TRUE;
2777789Sahrens 			break;
27788584SLori.Alt@Sun.COM 		case 'u':
27798584SLori.Alt@Sun.COM 			flags.nomount = B_TRUE;
27808584SLori.Alt@Sun.COM 			break;
2781789Sahrens 		case 'v':
27825367Sahrens 			flags.verbose = B_TRUE;
2783789Sahrens 			break;
27842665Snd150628 		case 'F':
27855367Sahrens 			flags.force = B_TRUE;
27862665Snd150628 			break;
2787789Sahrens 		case ':':
2788789Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
2789789Sahrens 			    "'%c' option\n"), optopt);
27902082Seschrock 			usage(B_FALSE);
2791789Sahrens 			break;
2792789Sahrens 		case '?':
2793789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2794789Sahrens 			    optopt);
27952082Seschrock 			usage(B_FALSE);
2796789Sahrens 		}
2797789Sahrens 	}
2798789Sahrens 
2799789Sahrens 	argc -= optind;
2800789Sahrens 	argv += optind;
2801789Sahrens 
2802789Sahrens 	/* check number of arguments */
2803789Sahrens 	if (argc < 1) {
2804789Sahrens 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
28052082Seschrock 		usage(B_FALSE);
2806789Sahrens 	}
2807789Sahrens 	if (argc > 1) {
2808789Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
28092082Seschrock 		usage(B_FALSE);
2810789Sahrens 	}
2811789Sahrens 
2812789Sahrens 	if (isatty(STDIN_FILENO)) {
2813789Sahrens 		(void) fprintf(stderr,
2814789Sahrens 		    gettext("Error: Backup stream can not be read "
28153912Slling 		    "from a terminal.\n"
28163912Slling 		    "You must redirect standard input.\n"));
2817789Sahrens 		return (1);
2818789Sahrens 	}
2819789Sahrens 
28205367Sahrens 	err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL);
28212926Sek110237 
28224543Smarks 	return (err != 0);
28234543Smarks }
28244543Smarks 
282510242Schris.kirby@sun.com static int
282610242Schris.kirby@sun.com zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
282710242Schris.kirby@sun.com {
282810242Schris.kirby@sun.com 	int errors = 0;
282910242Schris.kirby@sun.com 	int i;
283010242Schris.kirby@sun.com 	const char *tag;
283110242Schris.kirby@sun.com 	boolean_t recursive = B_FALSE;
283210342Schris.kirby@sun.com 	boolean_t temphold = B_FALSE;
283310342Schris.kirby@sun.com 	const char *opts = holding ? "rt" : "r";
283410242Schris.kirby@sun.com 	int c;
283510242Schris.kirby@sun.com 
283610242Schris.kirby@sun.com 	/* check options */
283710342Schris.kirby@sun.com 	while ((c = getopt(argc, argv, opts)) != -1) {
283810242Schris.kirby@sun.com 		switch (c) {
283910242Schris.kirby@sun.com 		case 'r':
284010242Schris.kirby@sun.com 			recursive = B_TRUE;
284110242Schris.kirby@sun.com 			break;
284210342Schris.kirby@sun.com 		case 't':
284310342Schris.kirby@sun.com 			temphold = B_TRUE;
284410342Schris.kirby@sun.com 			break;
284510242Schris.kirby@sun.com 		case '?':
284610242Schris.kirby@sun.com 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
284710242Schris.kirby@sun.com 			    optopt);
284810242Schris.kirby@sun.com 			usage(B_FALSE);
284910242Schris.kirby@sun.com 		}
285010242Schris.kirby@sun.com 	}
285110242Schris.kirby@sun.com 
285210242Schris.kirby@sun.com 	argc -= optind;
285310242Schris.kirby@sun.com 	argv += optind;
285410242Schris.kirby@sun.com 
285510242Schris.kirby@sun.com 	/* check number of arguments */
285610242Schris.kirby@sun.com 	if (argc < 2)
285710242Schris.kirby@sun.com 		usage(B_FALSE);
285810242Schris.kirby@sun.com 
285910242Schris.kirby@sun.com 	tag = argv[0];
286010242Schris.kirby@sun.com 	--argc;
286110242Schris.kirby@sun.com 	++argv;
286210242Schris.kirby@sun.com 
286310342Schris.kirby@sun.com 	if (holding && tag[0] == '.') {
286410342Schris.kirby@sun.com 		/* tags starting with '.' are reserved for libzfs */
286510342Schris.kirby@sun.com 		(void) fprintf(stderr, gettext("tag may not start with '.'\n"));
286610342Schris.kirby@sun.com 		usage(B_FALSE);
286710242Schris.kirby@sun.com 	}
286810242Schris.kirby@sun.com 
286910242Schris.kirby@sun.com 	for (i = 0; i < argc; ++i) {
287010242Schris.kirby@sun.com 		zfs_handle_t *zhp;
287110242Schris.kirby@sun.com 		char parent[ZFS_MAXNAMELEN];
287210242Schris.kirby@sun.com 		const char *delim;
287310242Schris.kirby@sun.com 		char *path = argv[i];
287410242Schris.kirby@sun.com 
287510242Schris.kirby@sun.com 		delim = strchr(path, '@');
287610242Schris.kirby@sun.com 		if (delim == NULL) {
287710242Schris.kirby@sun.com 			(void) fprintf(stderr,
287810242Schris.kirby@sun.com 			    gettext("'%s' is not a snapshot\n"), path);
287910242Schris.kirby@sun.com 			++errors;
288010242Schris.kirby@sun.com 			continue;
288110242Schris.kirby@sun.com 		}
288210242Schris.kirby@sun.com 		(void) strncpy(parent, path, delim - path);
288310242Schris.kirby@sun.com 		parent[delim - path] = '\0';
288410242Schris.kirby@sun.com 
288510242Schris.kirby@sun.com 		zhp = zfs_open(g_zfs, parent,
288610242Schris.kirby@sun.com 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
288710242Schris.kirby@sun.com 		if (zhp == NULL) {
288810242Schris.kirby@sun.com 			++errors;
288910242Schris.kirby@sun.com 			continue;
289010242Schris.kirby@sun.com 		}
289110342Schris.kirby@sun.com 		if (holding) {
289210342Schris.kirby@sun.com 			if (zfs_hold(zhp, delim+1, tag, recursive,
289312786SChris.Kirby@oracle.com 			    temphold, B_FALSE, -1, 0, 0) != 0)
289410342Schris.kirby@sun.com 				++errors;
289510342Schris.kirby@sun.com 		} else {
289610342Schris.kirby@sun.com 			if (zfs_release(zhp, delim+1, tag, recursive) != 0)
289710342Schris.kirby@sun.com 				++errors;
289810342Schris.kirby@sun.com 		}
289910242Schris.kirby@sun.com 		zfs_close(zhp);
290010242Schris.kirby@sun.com 	}
290110242Schris.kirby@sun.com 
290210242Schris.kirby@sun.com 	return (errors != 0);
290310242Schris.kirby@sun.com }
290410242Schris.kirby@sun.com 
290510242Schris.kirby@sun.com /*
290610342Schris.kirby@sun.com  * zfs hold [-r] [-t] <tag> <snap> ...
290710242Schris.kirby@sun.com  *
290811022STom.Erickson@Sun.COM  *	-r	Recursively hold
290910342Schris.kirby@sun.com  *	-t	Temporary hold (hidden option)
291010242Schris.kirby@sun.com  *
291110242Schris.kirby@sun.com  * Apply a user-hold with the given tag to the list of snapshots.
291210242Schris.kirby@sun.com  */
291310242Schris.kirby@sun.com static int
291410242Schris.kirby@sun.com zfs_do_hold(int argc, char **argv)
291510242Schris.kirby@sun.com {
291610242Schris.kirby@sun.com 	return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
291710242Schris.kirby@sun.com }
291810242Schris.kirby@sun.com 
291910242Schris.kirby@sun.com /*
292010242Schris.kirby@sun.com  * zfs release [-r] <tag> <snap> ...
292110242Schris.kirby@sun.com  *
292211022STom.Erickson@Sun.COM  *	-r	Recursively release
292310242Schris.kirby@sun.com  *
292410242Schris.kirby@sun.com  * Release a user-hold with the given tag from the list of snapshots.
292510242Schris.kirby@sun.com  */
292610242Schris.kirby@sun.com static int
292710242Schris.kirby@sun.com zfs_do_release(int argc, char **argv)
292810242Schris.kirby@sun.com {
292910242Schris.kirby@sun.com 	return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
293010242Schris.kirby@sun.com }
293110242Schris.kirby@sun.com 
29324737Smmusante #define	CHECK_SPINNER 30
29334737Smmusante #define	SPINNER_TIME 3		/* seconds */
29344737Smmusante #define	MOUNT_TIME 5		/* seconds */
29354737Smmusante 
29361356Seschrock static int
29373126Sahl get_one_dataset(zfs_handle_t *zhp, void *data)
29381356Seschrock {
293912446SMark.Musante@Sun.COM 	static char *spin[] = { "-", "\\", "|", "/" };
29404737Smmusante 	static int spinval = 0;
29414737Smmusante 	static int spincheck = 0;
29424737Smmusante 	static time_t last_spin_time = (time_t)0;
2943*13025SEric.Taylor@oracle.com 	get_all_cb_t *cbp = data;
29443126Sahl 	zfs_type_t type = zfs_get_type(zhp);
29451356Seschrock 
29464737Smmusante 	if (cbp->cb_verbose) {
29474737Smmusante 		if (--spincheck < 0) {
29484737Smmusante 			time_t now = time(NULL);
29494737Smmusante 			if (last_spin_time + SPINNER_TIME < now) {
295012446SMark.Musante@Sun.COM 				update_progress(spin[spinval++ % 4]);
29514737Smmusante 				last_spin_time = now;
29524737Smmusante 			}
29534737Smmusante 			spincheck = CHECK_SPINNER;
29544737Smmusante 		}
29554737Smmusante 	}
29564737Smmusante 
29571356Seschrock 	/*
29583126Sahl 	 * Interate over any nested datasets.
29591356Seschrock 	 */
2960*13025SEric.Taylor@oracle.com 	if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
29613265Sahrens 		zfs_close(zhp);
29623126Sahl 		return (1);
29633126Sahl 	}
29643126Sahl 
29653126Sahl 	/*
29663126Sahl 	 * Skip any datasets whose type does not match.
29673126Sahl 	 */
2968*13025SEric.Taylor@oracle.com 	if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
29691356Seschrock 		zfs_close(zhp);
29701356Seschrock 		return (0);
29711356Seschrock 	}
2972*13025SEric.Taylor@oracle.com 	libzfs_add_handle(cbp, zhp);
2973*13025SEric.Taylor@oracle.com 	assert(cbp->cb_used <= cbp->cb_alloc);
29741356Seschrock 
29753126Sahl 	return (0);
29761356Seschrock }
29771356Seschrock 
29781356Seschrock static void
2979*13025SEric.Taylor@oracle.com get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
29801356Seschrock {
2981*13025SEric.Taylor@oracle.com 	get_all_cb_t cb = { 0 };
29824737Smmusante 	cb.cb_verbose = verbose;
2983*13025SEric.Taylor@oracle.com 	cb.cb_getone = get_one_dataset;
29844737Smmusante 
298512446SMark.Musante@Sun.COM 	if (verbose)
298612446SMark.Musante@Sun.COM 		set_progress_header(gettext("Reading ZFS config"));
29873126Sahl 	(void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
29883126Sahl 
29893126Sahl 	*dslist = cb.cb_handles;
29901356Seschrock 	*count = cb.cb_used;
29914737Smmusante 
299212446SMark.Musante@Sun.COM 	if (verbose)
299312446SMark.Musante@Sun.COM 		finish_progress(gettext("done."));
29941356Seschrock }
29951356Seschrock 
2996789Sahrens /*
2997789Sahrens  * Generic callback for sharing or mounting filesystems.  Because the code is so
2998789Sahrens  * similar, we have a common function with an extra parameter to determine which
2999789Sahrens  * mode we are using.
3000789Sahrens  */
3001789Sahrens #define	OP_SHARE	0x1
3002789Sahrens #define	OP_MOUNT	0x2
3003789Sahrens 
3004789Sahrens /*
30053126Sahl  * Share or mount a dataset.
3006789Sahrens  */
3007789Sahrens static int
30085331Samw share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
30095331Samw     boolean_t explicit, const char *options)
3010789Sahrens {
3011789Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
3012789Sahrens 	char shareopts[ZFS_MAXPROPLEN];
30135331Samw 	char smbshareopts[ZFS_MAXPROPLEN];
30143126Sahl 	const char *cmdname = op == OP_SHARE ? "share" : "mount";
3015789Sahrens 	struct mnttab mnt;
30162676Seschrock 	uint64_t zoned, canmount;
30175331Samw 	boolean_t shared_nfs, shared_smb;
30183126Sahl 
3019*13025SEric.Taylor@oracle.com 	assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
3020*13025SEric.Taylor@oracle.com 
3021*13025SEric.Taylor@oracle.com 	/*
3022*13025SEric.Taylor@oracle.com 	 * Check to make sure we can mount/share this dataset.  If we
3023*13025SEric.Taylor@oracle.com 	 * are in the global zone and the filesystem is exported to a
3024*13025SEric.Taylor@oracle.com 	 * local zone, or if we are in a local zone and the
3025*13025SEric.Taylor@oracle.com 	 * filesystem is not exported, then it is an error.
3026*13025SEric.Taylor@oracle.com 	 */
3027*13025SEric.Taylor@oracle.com 	zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3028*13025SEric.Taylor@oracle.com 
3029*13025SEric.Taylor@oracle.com 	if (zoned && getzoneid() == GLOBAL_ZONEID) {
3030*13025SEric.Taylor@oracle.com 		if (!explicit)
3031*13025SEric.Taylor@oracle.com 			return (0);
3032*13025SEric.Taylor@oracle.com 
3033*13025SEric.Taylor@oracle.com 		(void) fprintf(stderr, gettext("cannot %s '%s': "
3034*13025SEric.Taylor@oracle.com 		    "dataset is exported to a local zone\n"), cmdname,
3035*13025SEric.Taylor@oracle.com 		    zfs_get_name(zhp));
3036*13025SEric.Taylor@oracle.com 		return (1);
3037*13025SEric.Taylor@oracle.com 
3038*13025SEric.Taylor@oracle.com 	} else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
3039*13025SEric.Taylor@oracle.com 		if (!explicit)
3040*13025SEric.Taylor@oracle.com 			return (0);
3041*13025SEric.Taylor@oracle.com 
3042*13025SEric.Taylor@oracle.com 		(void) fprintf(stderr, gettext("cannot %s '%s': "
3043*13025SEric.Taylor@oracle.com 		    "permission denied\n"), cmdname,
3044*13025SEric.Taylor@oracle.com 		    zfs_get_name(zhp));
3045*13025SEric.Taylor@oracle.com 		return (1);
3046*13025SEric.Taylor@oracle.com 	}
3047*13025SEric.Taylor@oracle.com 
3048*13025SEric.Taylor@oracle.com 	/*
3049*13025SEric.Taylor@oracle.com 	 * Ignore any filesystems which don't apply to us. This
3050*13025SEric.Taylor@oracle.com 	 * includes those with a legacy mountpoint, or those with
3051*13025SEric.Taylor@oracle.com 	 * legacy share options.
3052*13025SEric.Taylor@oracle.com 	 */
3053*13025SEric.Taylor@oracle.com 	verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
3054*13025SEric.Taylor@oracle.com 	    sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
3055*13025SEric.Taylor@oracle.com 	verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
3056*13025SEric.Taylor@oracle.com 	    sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
3057*13025SEric.Taylor@oracle.com 	verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
3058*13025SEric.Taylor@oracle.com 	    sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
3059*13025SEric.Taylor@oracle.com 
3060*13025SEric.Taylor@oracle.com 	if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
3061*13025SEric.Taylor@oracle.com 	    strcmp(smbshareopts, "off") == 0) {
3062*13025SEric.Taylor@oracle.com 		if (!explicit)
3063*13025SEric.Taylor@oracle.com 			return (0);
3064*13025SEric.Taylor@oracle.com 
3065*13025SEric.Taylor@oracle.com 		(void) fprintf(stderr, gettext("cannot share '%s': "
3066*13025SEric.Taylor@oracle.com 		    "legacy share\n"), zfs_get_name(zhp));
3067*13025SEric.Taylor@oracle.com 		(void) fprintf(stderr, gettext("use share(1M) to "
3068*13025SEric.Taylor@oracle.com 		    "share this filesystem, or set "
3069*13025SEric.Taylor@oracle.com 		    "sharenfs property on\n"));
3070*13025SEric.Taylor@oracle.com 		return (1);
3071*13025SEric.Taylor@oracle.com 	}
3072*13025SEric.Taylor@oracle.com 
3073*13025SEric.Taylor@oracle.com 	/*
3074*13025SEric.Taylor@oracle.com 	 * We cannot share or mount legacy filesystems. If the
3075*13025SEric.Taylor@oracle.com 	 * shareopts is non-legacy but the mountpoint is legacy, we
3076*13025SEric.Taylor@oracle.com 	 * treat it as a legacy share.
3077*13025SEric.Taylor@oracle.com 	 */
3078*13025SEric.Taylor@oracle.com 	if (strcmp(mountpoint, "legacy") == 0) {
3079*13025SEric.Taylor@oracle.com 		if (!explicit)
3080*13025SEric.Taylor@oracle.com 			return (0);
3081*13025SEric.Taylor@oracle.com 
3082*13025SEric.Taylor@oracle.com 		(void) fprintf(stderr, gettext("cannot %s '%s': "
3083*13025SEric.Taylor@oracle.com 		    "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
3084*13025SEric.Taylor@oracle.com 		(void) fprintf(stderr, gettext("use %s(1M) to "
3085*13025SEric.Taylor@oracle.com 		    "%s this filesystem\n"), cmdname, cmdname);
3086*13025SEric.Taylor@oracle.com 		return (1);
3087*13025SEric.Taylor@oracle.com 	}
3088*13025SEric.Taylor@oracle.com 
3089*13025SEric.Taylor@oracle.com 	if (strcmp(mountpoint, "none") == 0) {
3090*13025SEric.Taylor@oracle.com 		if (!explicit)
3091*13025SEric.Taylor@oracle.com 			return (0);
3092*13025SEric.Taylor@oracle.com 
3093*13025SEric.Taylor@oracle.com 		(void) fprintf(stderr, gettext("cannot %s '%s': no "
3094*13025SEric.Taylor@oracle.com 		    "mountpoint set\n"), cmdname, zfs_get_name(zhp));
3095*13025SEric.Taylor@oracle.com 		return (1);
3096*13025SEric.Taylor@oracle.com 	}
3097*13025SEric.Taylor@oracle.com 
3098*13025SEric.Taylor@oracle.com 	/*
3099*13025SEric.Taylor@oracle.com 	 * canmount	explicit	outcome
3100*13025SEric.Taylor@oracle.com 	 * on		no		pass through
3101*13025SEric.Taylor@oracle.com 	 * on		yes		pass through
3102*13025SEric.Taylor@oracle.com 	 * off		no		return 0
3103*13025SEric.Taylor@oracle.com 	 * off		yes		display error, return 1
3104*13025SEric.Taylor@oracle.com 	 * noauto	no		return 0
3105*13025SEric.Taylor@oracle.com 	 * noauto	yes		pass through
3106*13025SEric.Taylor@oracle.com 	 */
3107*13025SEric.Taylor@oracle.com 	canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
3108*13025SEric.Taylor@oracle.com 	if (canmount == ZFS_CANMOUNT_OFF) {
3109*13025SEric.Taylor@oracle.com 		if (!explicit)
3110*13025SEric.Taylor@oracle.com 			return (0);
3111*13025SEric.Taylor@oracle.com 
3112*13025SEric.Taylor@oracle.com 		(void) fprintf(stderr, gettext("cannot %s '%s': "
3113*13025SEric.Taylor@oracle.com 		    "'canmount' property is set to 'off'\n"), cmdname,
3114*13025SEric.Taylor@oracle.com 		    zfs_get_name(zhp));
3115*13025SEric.Taylor@oracle.com 		return (1);
3116*13025SEric.Taylor@oracle.com 	} else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
3117*13025SEric.Taylor@oracle.com 		return (0);
3118*13025SEric.Taylor@oracle.com 	}
3119*13025SEric.Taylor@oracle.com 
3120*13025SEric.Taylor@oracle.com 	/*
3121*13025SEric.Taylor@oracle.com 	 * At this point, we have verified that the mountpoint and/or
3122*13025SEric.Taylor@oracle.com 	 * shareopts are appropriate for auto management. If the
3123*13025SEric.Taylor@oracle.com 	 * filesystem is already mounted or shared, return (failing
3124*13025SEric.Taylor@oracle.com 	 * for explicit requests); otherwise mount or share the
3125*13025SEric.Taylor@oracle.com 	 * filesystem.
3126*13025SEric.Taylor@oracle.com 	 */
3127*13025SEric.Taylor@oracle.com 	switch (op) {
3128*13025SEric.Taylor@oracle.com 	case OP_SHARE:
3129*13025SEric.Taylor@oracle.com 
3130*13025SEric.Taylor@oracle.com 		shared_nfs = zfs_is_shared_nfs(zhp, NULL);
3131*13025SEric.Taylor@oracle.com 		shared_smb = zfs_is_shared_smb(zhp, NULL);
3132*13025SEric.Taylor@oracle.com 
3133*13025SEric.Taylor@oracle.com 		if (shared_nfs && shared_smb ||
3134*13025SEric.Taylor@oracle.com 		    (shared_nfs && strcmp(shareopts, "on") == 0 &&
3135*13025SEric.Taylor@oracle.com 		    strcmp(smbshareopts, "off") == 0) ||
3136*13025SEric.Taylor@oracle.com 		    (shared_smb && strcmp(smbshareopts, "on") == 0 &&
3137*13025SEric.Taylor@oracle.com 		    strcmp(shareopts, "off") == 0)) {
31383126Sahl 			if (!explicit)
31393126Sahl 				return (0);
31403126Sahl 
3141*13025SEric.Taylor@oracle.com 			(void) fprintf(stderr, gettext("cannot share "
3142*13025SEric.Taylor@oracle.com 			    "'%s': filesystem already shared\n"),
31433126Sahl 			    zfs_get_name(zhp));
31443126Sahl 			return (1);
31453126Sahl 		}
31463126Sahl 
3147*13025SEric.Taylor@oracle.com 		if (!zfs_is_mounted(zhp, NULL) &&
3148*13025SEric.Taylor@oracle.com 		    zfs_mount(zhp, NULL, 0) != 0)
3149*13025SEric.Taylor@oracle.com 			return (1);
3150*13025SEric.Taylor@oracle.com 
3151*13025SEric.Taylor@oracle.com 		if (protocol == NULL) {
3152*13025SEric.Taylor@oracle.com 			if (zfs_shareall(zhp) != 0)
3153*13025SEric.Taylor@oracle.com 				return (1);
3154*13025SEric.Taylor@oracle.com 		} else if (strcmp(protocol, "nfs") == 0) {
3155*13025SEric.Taylor@oracle.com 			if (zfs_share_nfs(zhp))
3156*13025SEric.Taylor@oracle.com 				return (1);
3157*13025SEric.Taylor@oracle.com 		} else if (strcmp(protocol, "smb") == 0) {
3158*13025SEric.Taylor@oracle.com 			if (zfs_share_smb(zhp))
3159*13025SEric.Taylor@oracle.com 				return (1);
3160*13025SEric.Taylor@oracle.com 		} else {
3161*13025SEric.Taylor@oracle.com 			(void) fprintf(stderr, gettext("cannot share "
3162*13025SEric.Taylor@oracle.com 			    "'%s': invalid share type '%s' "
3163*13025SEric.Taylor@oracle.com 			    "specified\n"),
3164*13025SEric.Taylor@oracle.com 			    zfs_get_name(zhp), protocol);
3165789Sahrens 			return (1);
3166789Sahrens 		}
31673126Sahl 
3168*13025SEric.Taylor@oracle.com 		break;
3169*13025SEric.Taylor@oracle.com 
3170*13025SEric.Taylor@oracle.com 	case OP_MOUNT:
3171*13025SEric.Taylor@oracle.com 		if (options == NULL)
3172*13025SEric.Taylor@oracle.com 			mnt.mnt_mntopts = "";
3173*13025SEric.Taylor@oracle.com 		else
3174*13025SEric.Taylor@oracle.com 			mnt.mnt_mntopts = (char *)options;
3175*13025SEric.Taylor@oracle.com 
3176*13025SEric.Taylor@oracle.com 		if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
3177*13025SEric.Taylor@oracle.com 		    zfs_is_mounted(zhp, NULL)) {
31783126Sahl 			if (!explicit)
31793126Sahl 				return (0);
31803126Sahl 
3181*13025SEric.Taylor@oracle.com 			(void) fprintf(stderr, gettext("cannot mount "
3182*13025SEric.Taylor@oracle.com 			    "'%s': filesystem already mounted\n"),
31833126Sahl 			    zfs_get_name(zhp));
31843126Sahl 			return (1);
31853126Sahl 		}
31863126Sahl 
3187*13025SEric.Taylor@oracle.com 		if (zfs_mount(zhp, options, flags) != 0)
3188*13025SEric.Taylor@oracle.com 			return (1);
3189*13025SEric.Taylor@oracle.com 		break;
3190*13025SEric.Taylor@oracle.com 	}
31913126Sahl 
3192789Sahrens 	return (0);
3193789Sahrens }
3194789Sahrens 
31954737Smmusante /*
31964737Smmusante  * Reports progress in the form "(current/total)".  Not thread-safe.
31974737Smmusante  */
31984737Smmusante static void
31994737Smmusante report_mount_progress(int current, int total)
32004737Smmusante {
320112446SMark.Musante@Sun.COM 	static time_t last_progress_time = 0;
32024737Smmusante 	time_t now = time(NULL);
320312446SMark.Musante@Sun.COM 	char info[32];
32044737Smmusante 
32054737Smmusante 	/* report 1..n instead of 0..n-1 */
32064737Smmusante 	++current;
32074737Smmusante 
32084737Smmusante 	/* display header if we're here for the first time */
32094737Smmusante 	if (current == 1) {
321012446SMark.Musante@Sun.COM 		set_progress_header(gettext("Mounting ZFS filesystems"));
32115367Sahrens 	} else if (current != total && last_progress_time + MOUNT_TIME >= now) {
32125367Sahrens 		/* too soon to report again */
32135367Sahrens 		return;
32145367Sahrens 	}
32154737Smmusante 
32164737Smmusante 	last_progress_time = now;
32174737Smmusante 
321812446SMark.Musante@Sun.COM 	(void) sprintf(info, "(%d/%d)", current, total);
321912446SMark.Musante@Sun.COM 
322012446SMark.Musante@Sun.COM 	if (current == total)
322112446SMark.Musante@Sun.COM 		finish_progress(info);
322212446SMark.Musante@Sun.COM 	else
322312446SMark.Musante@Sun.COM 		update_progress(info);
32244737Smmusante }
32254737Smmusante 
32266289Smmusante static void
32276289Smmusante append_options(char *mntopts, char *newopts)
32286289Smmusante {
32296289Smmusante 	int len = strlen(mntopts);
32306289Smmusante 
32316289Smmusante 	/* original length plus new string to append plus 1 for the comma */
32326289Smmusante 	if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
32336289Smmusante 		(void) fprintf(stderr, gettext("the opts argument for "
32346289Smmusante 		    "'%c' option is too long (more than %d chars)\n"),
32356289Smmusante 		    "-o", MNT_LINE_MAX);
32366289Smmusante 		usage(B_FALSE);
32376289Smmusante 	}
32386289Smmusante 
32396289Smmusante 	if (*mntopts)
32406289Smmusante 		mntopts[len++] = ',';
32416289Smmusante 
32426289Smmusante 	(void) strcpy(&mntopts[len], newopts);
32436289Smmusante }
32446289Smmusante 
3245789Sahrens static int
32463126Sahl share_mount(int op, int argc, char **argv)
3247789Sahrens {
3248789Sahrens 	int do_all = 0;
32494737Smmusante 	boolean_t verbose = B_FALSE;
32502372Slling 	int c, ret = 0;
32516289Smmusante 	char *options = NULL;
3252*13025SEric.Taylor@oracle.com 	int flags = 0;
3253789Sahrens 
3254789Sahrens 	/* check options */
32554737Smmusante 	while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
3256789Sahrens 	    != -1) {
3257789Sahrens 		switch (c) {
3258789Sahrens 		case 'a':
3259789Sahrens 			do_all = 1;
3260789Sahrens 			break;
32614737Smmusante 		case 'v':
32624737Smmusante 			verbose = B_TRUE;
32634737Smmusante 			break;
3264789Sahrens 		case 'o':
32656289Smmusante 			if (*optarg == '\0') {
32666289Smmusante 				(void) fprintf(stderr, gettext("empty mount "
32676289Smmusante 				    "options (-o) specified\n"));
32686289Smmusante 				usage(B_FALSE);
32694717Srm160521 			}
32706289Smmusante 
32716289Smmusante 			if (options == NULL)
32726289Smmusante 				options = safe_malloc(MNT_LINE_MAX + 1);
32736289Smmusante 
32746289Smmusante 			/* option validation is done later */
32756289Smmusante 			append_options(options, optarg);
3276789Sahrens 			break;
32774717Srm160521 
3278789Sahrens 		case 'O':
32793126Sahl 			flags |= MS_OVERLAY;
3280789Sahrens 			break;
3281789Sahrens 		case ':':
3282789Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
3283789Sahrens 			    "'%c' option\n"), optopt);
32842082Seschrock 			usage(B_FALSE);
3285789Sahrens 			break;
3286789Sahrens 		case '?':
3287789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3288789Sahrens 			    optopt);
32892082Seschrock 			usage(B_FALSE);
3290789Sahrens 		}
3291789Sahrens 	}
3292789Sahrens 
3293789Sahrens 	argc -= optind;
3294789Sahrens 	argv += optind;
3295789Sahrens 
3296789Sahrens 	/* check number of arguments */
3297789Sahrens 	if (do_all) {
32983126Sahl 		zfs_handle_t **dslist = NULL;
32991356Seschrock 		size_t i, count = 0;
33005331Samw 		char *protocol = NULL;
33011356Seschrock 
3302*13025SEric.Taylor@oracle.com 		if (op == OP_SHARE && argc > 0) {
3303*13025SEric.Taylor@oracle.com 			if (strcmp(argv[0], "nfs") != 0 &&
3304*13025SEric.Taylor@oracle.com 			    strcmp(argv[0], "smb") != 0) {
33053126Sahl 				(void) fprintf(stderr, gettext("share type "
330611876SJames.Dunham@Sun.COM 				    "must be 'nfs' or 'smb'\n"));
33073126Sahl 				usage(B_FALSE);
33083126Sahl 			}
33095331Samw 			protocol = argv[0];
33103126Sahl 			argc--;
33113126Sahl 			argv++;
33123126Sahl 		}
33133126Sahl 
3314789Sahrens 		if (argc != 0) {
3315789Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
33162082Seschrock 			usage(B_FALSE);
3317789Sahrens 		}
3318789Sahrens 
331912446SMark.Musante@Sun.COM 		start_progress_timer();
3320*13025SEric.Taylor@oracle.com 		get_all_datasets(&dslist, &count, verbose);
33211356Seschrock 
33221356Seschrock 		if (count == 0)
33231356Seschrock 			return (0);
33241356Seschrock 
3325*13025SEric.Taylor@oracle.com 		qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
33261356Seschrock 
33271356Seschrock 		for (i = 0; i < count; i++) {
33284737Smmusante 			if (verbose)
33294737Smmusante 				report_mount_progress(i, count);
33304737Smmusante 
33315331Samw 			if (share_mount_one(dslist[i], op, flags, protocol,
33325331Samw 			    B_FALSE, options) != 0)
33332369Slling 				ret = 1;
33343126Sahl 			zfs_close(dslist[i]);
33351356Seschrock 		}
33361356Seschrock 
33373126Sahl 		free(dslist);
3338789Sahrens 	} else if (argc == 0) {
3339789Sahrens 		struct mnttab entry;
3340789Sahrens 
33414717Srm160521 		if ((op == OP_SHARE) || (options != NULL)) {
3342789Sahrens 			(void) fprintf(stderr, gettext("missing filesystem "
33434717Srm160521 			    "argument (specify -a for all)\n"));
33442082Seschrock 			usage(B_FALSE);
3345789Sahrens 		}
3346789Sahrens 
3347789Sahrens 		/*
3348789Sahrens 		 * When mount is given no arguments, go through /etc/mnttab and
3349789Sahrens 		 * display any active ZFS mounts.  We hide any snapshots, since
3350789Sahrens 		 * they are controlled automatically.
3351789Sahrens 		 */
3352789Sahrens 		rewind(mnttab_file);
3353789Sahrens 		while (getmntent(mnttab_file, &entry) == 0) {
3354789Sahrens 			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
3355789Sahrens 			    strchr(entry.mnt_special, '@') != NULL)
3356789Sahrens 				continue;
3357789Sahrens 
3358789Sahrens 			(void) printf("%-30s  %s\n", entry.mnt_special,
3359789Sahrens 			    entry.mnt_mountp);
3360789Sahrens 		}
3361789Sahrens 
3362789Sahrens 	} else {
3363789Sahrens 		zfs_handle_t *zhp;
3364789Sahrens 
3365789Sahrens 		if (argc > 1) {
3366789Sahrens 			(void) fprintf(stderr,
3367789Sahrens 			    gettext("too many arguments\n"));
33682082Seschrock 			usage(B_FALSE);
3369789Sahrens 		}
3370789Sahrens 
3371*13025SEric.Taylor@oracle.com 		if ((zhp = zfs_open(g_zfs, argv[0],
3372*13025SEric.Taylor@oracle.com 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
3373789Sahrens 			ret = 1;
33743126Sahl 		} else {
33755331Samw 			ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
33763126Sahl 			    options);
3377789Sahrens 			zfs_close(zhp);
3378789Sahrens 		}
3379789Sahrens 	}
3380789Sahrens 
3381789Sahrens 	return (ret);
3382789Sahrens }
3383789Sahrens 
3384789Sahrens /*
338511876SJames.Dunham@Sun.COM  * zfs mount -a [nfs]
3386789Sahrens  * zfs mount filesystem
3387789Sahrens  *
3388789Sahrens  * Mount all filesystems, or mount the given filesystem.
3389789Sahrens  */
3390789Sahrens static int
3391789Sahrens zfs_do_mount(int argc, char **argv)
3392789Sahrens {
33933126Sahl 	return (share_mount(OP_MOUNT, argc, argv));
3394789Sahrens }
3395789Sahrens 
3396789Sahrens /*
339711876SJames.Dunham@Sun.COM  * zfs share -a [nfs | smb]
3398789Sahrens  * zfs share filesystem
3399789Sahrens  *
3400789Sahrens  * Share all filesystems, or share the given filesystem.
3401789Sahrens  */
3402789Sahrens static int
3403789Sahrens zfs_do_share(int argc, char **argv)
3404789Sahrens {
34053126Sahl 	return (share_mount(OP_SHARE, argc, argv));
3406789Sahrens }
3407789Sahrens 
3408789Sahrens typedef struct unshare_unmount_node {
3409789Sahrens 	zfs_handle_t	*un_zhp;
3410789Sahrens 	char		*un_mountp;
3411789Sahrens 	uu_avl_node_t	un_avlnode;
3412789Sahrens } unshare_unmount_node_t;
3413789Sahrens 
3414789Sahrens /* ARGSUSED */
3415789Sahrens static int
3416789Sahrens unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
3417789Sahrens {
3418789Sahrens 	const unshare_unmount_node_t *l = larg;
3419789Sahrens 	const unshare_unmount_node_t *r = rarg;
3420789Sahrens 
3421789Sahrens 	return (strcmp(l->un_mountp, r->un_mountp));
3422789Sahrens }
3423789Sahrens 
3424789Sahrens /*
3425789Sahrens  * Convenience routine used by zfs_do_umount() and manual_unmount().  Given an
3426789Sahrens  * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
3427789Sahrens  * and unmount it appropriately.
3428789Sahrens  */
3429789Sahrens static int
34303126Sahl unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
3431789Sahrens {
3432789Sahrens 	zfs_handle_t *zhp;
3433789Sahrens 	int ret;
3434789Sahrens 	struct stat64 statbuf;
3435789Sahrens 	struct extmnttab entry;
34363126Sahl 	const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
34376289Smmusante 	ino_t path_inode;
3438789Sahrens 
3439789Sahrens 	/*
3440789Sahrens 	 * Search for the path in /etc/mnttab.  Rather than looking for the
3441789Sahrens 	 * specific path, which can be fooled by non-standard paths (i.e. ".."
3442789Sahrens 	 * or "//"), we stat() the path and search for the corresponding
3443789Sahrens 	 * (major,minor) device pair.
3444789Sahrens 	 */
3445789Sahrens 	if (stat64(path, &statbuf) != 0) {
3446789Sahrens 		(void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3447789Sahrens 		    cmdname, path, strerror(errno));
3448789Sahrens 		return (1);
3449789Sahrens 	}
34506289Smmusante 	path_inode = statbuf.st_ino;
3451789Sahrens 
3452789Sahrens 	/*
3453789Sahrens 	 * Search for the given (major,minor) pair in the mount table.
3454789Sahrens 	 */
3455789Sahrens 	rewind(mnttab_file);
3456789Sahrens 	while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
3457789Sahrens 		if (entry.mnt_major == major(statbuf.st_dev) &&
3458789Sahrens 		    entry.mnt_minor == minor(statbuf.st_dev))
3459789Sahrens 			break;
3460789Sahrens 	}
3461789Sahrens 	if (ret != 0) {
34625904Stimh 		if (op == OP_SHARE) {
34635904Stimh 			(void) fprintf(stderr, gettext("cannot %s '%s': not "
34645904Stimh 			    "currently mounted\n"), cmdname, path);
34655904Stimh 			return (1);
34665904Stimh 		}
34675904Stimh 		(void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
34685904Stimh 		    path);
34695904Stimh 		if ((ret = umount2(path, flags)) != 0)
34705904Stimh 			(void) fprintf(stderr, gettext("%s: %s\n"), path,
34715904Stimh 			    strerror(errno));
34725904Stimh 		return (ret != 0);
3473789Sahrens 	}
3474789Sahrens 
3475789Sahrens 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
3476789Sahrens 		(void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
3477789Sahrens 		    "filesystem\n"), cmdname, path);
3478789Sahrens 		return (1);
3479789Sahrens 	}
3480789Sahrens 
34812082Seschrock 	if ((zhp = zfs_open(g_zfs, entry.mnt_special,
34822082Seschrock 	    ZFS_TYPE_FILESYSTEM)) == NULL)
3483789Sahrens 		return (1);
3484789Sahrens 
34856289Smmusante 	ret = 1;
34867386SSachin.Gaikwad@Sun.COM 	if (stat64(entry.mnt_mountp, &statbuf) != 0) {
34877386SSachin.Gaikwad@Sun.COM 		(void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
34887386SSachin.Gaikwad@Sun.COM 		    cmdname, path, strerror(errno));
34897386SSachin.Gaikwad@Sun.COM 		goto out;
34907386SSachin.Gaikwad@Sun.COM 	} else if (statbuf.st_ino != path_inode) {
34917386SSachin.Gaikwad@Sun.COM 		(void) fprintf(stderr, gettext("cannot "
34927386SSachin.Gaikwad@Sun.COM 		    "%s '%s': not a mountpoint\n"), cmdname, path);
34937386SSachin.Gaikwad@Sun.COM 		goto out;
34947386SSachin.Gaikwad@Sun.COM 	}
34957386SSachin.Gaikwad@Sun.COM 
34963126Sahl 	if (op == OP_SHARE) {
34976289Smmusante 		char nfs_mnt_prop[ZFS_MAXPROPLEN];
34986289Smmusante 		char smbshare_prop[ZFS_MAXPROPLEN];
34996289Smmusante 
35006289Smmusante 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
35016289Smmusante 		    sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
35026289Smmusante 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
35036289Smmusante 		    sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
35046289Smmusante 
35055331Samw 		if (strcmp(nfs_mnt_prop, "off") == 0 &&
35065331Samw 		    strcmp(smbshare_prop, "off") == 0) {
3507789Sahrens 			(void) fprintf(stderr, gettext("cannot unshare "
3508789Sahrens 			    "'%s': legacy share\n"), path);
3509789Sahrens 			(void) fprintf(stderr, gettext("use "
3510789Sahrens 			    "unshare(1M) to unshare this filesystem\n"));
35115331Samw 		} else if (!zfs_is_shared(zhp)) {
3512789Sahrens 			(void) fprintf(stderr, gettext("cannot unshare '%s': "
3513789Sahrens 			    "not currently shared\n"), path);
3514789Sahrens 		} else {
35155331Samw 			ret = zfs_unshareall_bypath(zhp, path);
3516789Sahrens 		}
3517789Sahrens 	} else {
35186289Smmusante 		char mtpt_prop[ZFS_MAXPROPLEN];
35196289Smmusante 
35206289Smmusante 		verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
35216289Smmusante 		    sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
35226289Smmusante 
35237386SSachin.Gaikwad@Sun.COM 		if (is_manual) {
35241264Slling 			ret = zfs_unmount(zhp, NULL, flags);
35256289Smmusante 		} else if (strcmp(mtpt_prop, "legacy") == 0) {
35261264Slling 			(void) fprintf(stderr, gettext("cannot unmount "
35271264Slling 			    "'%s': legacy mountpoint\n"),
35281264Slling 			    zfs_get_name(zhp));
35291264Slling 			(void) fprintf(stderr, gettext("use umount(1M) "
35301264Slling 			    "to unmount this filesystem\n"));
3531789Sahrens 		} else {
3532789Sahrens 			ret = zfs_unmountall(zhp, flags);
3533789Sahrens 		}
3534789Sahrens 	}
3535789Sahrens 
35367386SSachin.Gaikwad@Sun.COM out:
3537789Sahrens 	zfs_close(zhp);
3538789Sahrens 
3539789Sahrens 	return (ret != 0);
3540789Sahrens }
3541789Sahrens 
3542789Sahrens /*
3543789Sahrens  * Generic callback for unsharing or unmounting a filesystem.
3544789Sahrens  */
3545789Sahrens static int
35463126Sahl unshare_unmount(int op, int argc, char **argv)
3547789Sahrens {
3548789Sahrens 	int do_all = 0;
3549789Sahrens 	int flags = 0;
3550789Sahrens 	int ret = 0;
3551*13025SEric.Taylor@oracle.com 	int c;
3552789Sahrens 	zfs_handle_t *zhp;
355311876SJames.Dunham@Sun.COM 	char nfs_mnt_prop[ZFS_MAXPROPLEN];
35545331Samw 	char sharesmb[ZFS_MAXPROPLEN];
3555789Sahrens 
3556789Sahrens 	/* check options */
35573126Sahl 	while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
3558789Sahrens 		switch (c) {
3559789Sahrens 		case 'a':
3560789Sahrens 			do_all = 1;
3561789Sahrens 			break;
3562789Sahrens 		case 'f':
3563789Sahrens 			flags = MS_FORCE;
3564789Sahrens 			break;
3565789Sahrens 		case '?':
3566789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3567789Sahrens 			    optopt);
35682082Seschrock 			usage(B_FALSE);
3569789Sahrens 		}
3570789Sahrens 	}
3571789Sahrens 
3572789Sahrens 	argc -= optind;
3573789Sahrens 	argv += optind;
3574789Sahrens 
3575789Sahrens 	if (do_all) {
3576789Sahrens 		/*
3577789Sahrens 		 * We could make use of zfs_for_each() to walk all datasets in
3578789Sahrens 		 * the system, but this would be very inefficient, especially
3579789Sahrens 		 * since we would have to linearly search /etc/mnttab for each
3580789Sahrens 		 * one.  Instead, do one pass through /etc/mnttab looking for
3581789Sahrens 		 * zfs entries and call zfs_unmount() for each one.
3582789Sahrens 		 *
3583789Sahrens 		 * Things get a little tricky if the administrator has created
3584789Sahrens 		 * mountpoints beneath other ZFS filesystems.  In this case, we
3585789Sahrens 		 * have to unmount the deepest filesystems first.  To accomplish
3586789Sahrens 		 * this, we place all the mountpoints in an AVL tree sorted by
3587789Sahrens 		 * the special type (dataset name), and walk the result in
3588789Sahrens 		 * reverse to make sure to get any snapshots first.
3589789Sahrens 		 */
3590789Sahrens 		struct mnttab entry;
3591789Sahrens 		uu_avl_pool_t *pool;
3592789Sahrens 		uu_avl_t *tree;
3593789Sahrens 		unshare_unmount_node_t *node;
3594789Sahrens 		uu_avl_index_t idx;
3595789Sahrens 		uu_avl_walk_t *walk;
3596789Sahrens 
35973126Sahl 		if (argc != 0) {
35983126Sahl 			(void) fprintf(stderr, gettext("too many arguments\n"));
35993126Sahl 			usage(B_FALSE);
36003126Sahl 		}
36013126Sahl 
360212446SMark.Musante@Sun.COM 		if (((pool = uu_avl_pool_create("unmount_pool",
3603789Sahrens 		    sizeof (unshare_unmount_node_t),
3604789Sahrens 		    offsetof(unshare_unmount_node_t, un_avlnode),
360512446SMark.Musante@Sun.COM 		    unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
360612446SMark.Musante@Sun.COM 		    ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
360712446SMark.Musante@Sun.COM 			nomem();
3608789Sahrens 
3609789Sahrens 		rewind(mnttab_file);
3610789Sahrens 		while (getmntent(mnttab_file, &entry) == 0) {
3611789Sahrens 
3612789Sahrens 			/* ignore non-ZFS entries */
3613789Sahrens 			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
3614789Sahrens 				continue;
3615789Sahrens 
3616789Sahrens 			/* ignore snapshots */
3617789Sahrens 			if (strchr(entry.mnt_special, '@') != NULL)
3618789Sahrens 				continue;
3619789Sahrens 
36202082Seschrock 			if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3621789Sahrens 			    ZFS_TYPE_FILESYSTEM)) == NULL) {
3622789Sahrens 				ret = 1;
3623789Sahrens 				continue;
3624789Sahrens 			}
3625789Sahrens 
36265331Samw 			switch (op) {
36275331Samw 			case OP_SHARE:
36285331Samw 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
362911876SJames.Dunham@Sun.COM 				    nfs_mnt_prop,
363011876SJames.Dunham@Sun.COM 				    sizeof (nfs_mnt_prop),
36315331Samw 				    NULL, NULL, 0, B_FALSE) == 0);
363211876SJames.Dunham@Sun.COM 				if (strcmp(nfs_mnt_prop, "off") != 0)
36335331Samw 					break;
36345331Samw 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
363511876SJames.Dunham@Sun.COM 				    nfs_mnt_prop,
363611876SJames.Dunham@Sun.COM 				    sizeof (nfs_mnt_prop),
36375331Samw 				    NULL, NULL, 0, B_FALSE) == 0);
363811876SJames.Dunham@Sun.COM 				if (strcmp(nfs_mnt_prop, "off") == 0)
36395331Samw 					continue;
36405331Samw 				break;
36415331Samw 			case OP_MOUNT:
36425331Samw 				/* Ignore legacy mounts */
36435331Samw 				verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
364411876SJames.Dunham@Sun.COM 				    nfs_mnt_prop,
364511876SJames.Dunham@Sun.COM 				    sizeof (nfs_mnt_prop),
36465331Samw 				    NULL, NULL, 0, B_FALSE) == 0);
364711876SJames.Dunham@Sun.COM 				if (strcmp(nfs_mnt_prop, "legacy") == 0)
36485331Samw 					continue;
36496168Shs24103 				/* Ignore canmount=noauto mounts */
36506168Shs24103 				if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
36516168Shs24103 				    ZFS_CANMOUNT_NOAUTO)
36526168Shs24103 					continue;
36535331Samw 			default:
36545331Samw 				break;
3655789Sahrens 			}
3656789Sahrens 
3657789Sahrens 			node = safe_malloc(sizeof (unshare_unmount_node_t));
3658789Sahrens 			node->un_zhp = zhp;
365912446SMark.Musante@Sun.COM 			node->un_mountp = safe_strdup(entry.mnt_mountp);
3660789Sahrens 
3661789Sahrens 			uu_avl_node_init(node, &node->un_avlnode, pool);
3662789Sahrens 
3663789Sahrens 			if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
3664789Sahrens 				uu_avl_insert(tree, node, idx);
3665789Sahrens 			} else {
3666789Sahrens 				zfs_close(node->un_zhp);
3667789Sahrens 				free(node->un_mountp);
3668789Sahrens 				free(node);
3669789Sahrens 			}
3670789Sahrens 		}
3671789Sahrens 
3672789Sahrens 		/*
3673789Sahrens 		 * Walk the AVL tree in reverse, unmounting each filesystem and
3674789Sahrens 		 * removing it from the AVL tree in the process.
3675789Sahrens 		 */
3676789Sahrens 		if ((walk = uu_avl_walk_start(tree,
367712446SMark.Musante@Sun.COM 		    UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
367812446SMark.Musante@Sun.COM 			nomem();
3679789Sahrens 
3680789Sahrens 		while ((node = uu_avl_walk_next(walk)) != NULL) {
3681789Sahrens 			uu_avl_remove(tree, node);
3682789Sahrens 
36833126Sahl 			switch (op) {
3684789Sahrens 			case OP_SHARE:
36855331Samw 				if (zfs_unshareall_bypath(node->un_zhp,
3686789Sahrens 				    node->un_mountp) != 0)
3687789Sahrens 					ret = 1;
3688789Sahrens 				break;
3689789Sahrens 
3690789Sahrens 			case OP_MOUNT:
3691789Sahrens 				if (zfs_unmount(node->un_zhp,
3692789Sahrens 				    node->un_mountp, flags) != 0)
3693789Sahrens 					ret = 1;
3694789Sahrens 				break;
3695789Sahrens 			}
3696789Sahrens 
3697789Sahrens 			zfs_close(node->un_zhp);
3698789Sahrens 			free(node->un_mountp);
3699789Sahrens 			free(node);
3700789Sahrens 		}
3701789Sahrens 
3702789Sahrens 		uu_avl_walk_end(walk);
3703789Sahrens 		uu_avl_destroy(tree);
3704789Sahrens 		uu_avl_pool_destroy(pool);
37053126Sahl 
3706789Sahrens 	} else {
37073126Sahl 		if (argc != 1) {
37083126Sahl 			if (argc == 0)
37093126Sahl 				(void) fprintf(stderr,
37103126Sahl 				    gettext("missing filesystem argument\n"));
37113126Sahl 			else
37123126Sahl 				(void) fprintf(stderr,
37133126Sahl 				    gettext("too many arguments\n"));
37143126Sahl 			usage(B_FALSE);
37153126Sahl 		}
37163126Sahl 
3717789Sahrens 		/*
3718789Sahrens 		 * We have an argument, but it may be a full path or a ZFS
3719789Sahrens 		 * filesystem.  Pass full paths off to unmount_path() (shared by
3720789Sahrens 		 * manual_unmount), otherwise open the filesystem and pass to
3721789Sahrens 		 * zfs_unmount().
3722789Sahrens 		 */
3723789Sahrens 		if (argv[0][0] == '/')
37243126Sahl 			return (unshare_unmount_path(op, argv[0],
37253912Slling 			    flags, B_FALSE));
37262082Seschrock 
3727*13025SEric.Taylor@oracle.com 		if ((zhp = zfs_open(g_zfs, argv[0],
3728*13025SEric.Taylor@oracle.com 		    ZFS_TYPE_FILESYSTEM)) == NULL)
3729789Sahrens 			return (1);
3730789Sahrens 
3731*13025SEric.Taylor@oracle.com 		verify(zfs_prop_get(zhp, op == OP_SHARE ?
3732*13025SEric.Taylor@oracle.com 		    ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
3733*13025SEric.Taylor@oracle.com 		    nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
3734*13025SEric.Taylor@oracle.com 		    NULL, 0, B_FALSE) == 0);
3735*13025SEric.Taylor@oracle.com 
3736*13025SEric.Taylor@oracle.com 		switch (op) {
3737*13025SEric.Taylor@oracle.com 		case OP_SHARE:
3738*13025SEric.Taylor@oracle.com 			verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3739*13025SEric.Taylor@oracle.com 			    nfs_mnt_prop,
3740*13025SEric.Taylor@oracle.com 			    sizeof (nfs_mnt_prop),
3741*13025SEric.Taylor@oracle.com 			    NULL, NULL, 0, B_FALSE) == 0);
3742*13025SEric.Taylor@oracle.com 			verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3743*13025SEric.Taylor@oracle.com 			    sharesmb, sizeof (sharesmb), NULL, NULL,
3744*13025SEric.Taylor@oracle.com 			    0, B_FALSE) == 0);
3745*13025SEric.Taylor@oracle.com 
3746*13025SEric.Taylor@oracle.com 			if (strcmp(nfs_mnt_prop, "off") == 0 &&
3747*13025SEric.Taylor@oracle.com 			    strcmp(sharesmb, "off") == 0) {
3748*13025SEric.Taylor@oracle.com 				(void) fprintf(stderr, gettext("cannot "
3749*13025SEric.Taylor@oracle.com 				    "unshare '%s': legacy share\n"),
3750*13025SEric.Taylor@oracle.com 				    zfs_get_name(zhp));
3751*13025SEric.Taylor@oracle.com 				(void) fprintf(stderr, gettext("use "
3752*13025SEric.Taylor@oracle.com 				    "unshare(1M) to unshare this "
3753*13025SEric.Taylor@oracle.com 				    "filesystem\n"));
3754*13025SEric.Taylor@oracle.com 				ret = 1;
3755*13025SEric.Taylor@oracle.com 			} else if (!zfs_is_shared(zhp)) {
3756*13025SEric.Taylor@oracle.com 				(void) fprintf(stderr, gettext("cannot "
3757*13025SEric.Taylor@oracle.com 				    "unshare '%s': not currently "
3758*13025SEric.Taylor@oracle.com 				    "shared\n"), zfs_get_name(zhp));
3759*13025SEric.Taylor@oracle.com 				ret = 1;
3760*13025SEric.Taylor@oracle.com 			} else if (zfs_unshareall(zhp) != 0) {
3761*13025SEric.Taylor@oracle.com 				ret = 1;
37623126Sahl 			}
3763*13025SEric.Taylor@oracle.com 			break;
3764*13025SEric.Taylor@oracle.com 
3765*13025SEric.Taylor@oracle.com 		case OP_MOUNT:
3766*13025SEric.Taylor@oracle.com 			if (strcmp(nfs_mnt_prop, "legacy") == 0) {
3767*13025SEric.Taylor@oracle.com 				(void) fprintf(stderr, gettext("cannot "
3768*13025SEric.Taylor@oracle.com 				    "unmount '%s': legacy "
3769*13025SEric.Taylor@oracle.com 				    "mountpoint\n"), zfs_get_name(zhp));
3770*13025SEric.Taylor@oracle.com 				(void) fprintf(stderr, gettext("use "
3771*13025SEric.Taylor@oracle.com 				    "umount(1M) to unmount this "
3772*13025SEric.Taylor@oracle.com 				    "filesystem\n"));
3773*13025SEric.Taylor@oracle.com 				ret = 1;
3774*13025SEric.Taylor@oracle.com 			} else if (!zfs_is_mounted(zhp, NULL)) {
3775*13025SEric.Taylor@oracle.com 				(void) fprintf(stderr, gettext("cannot "
3776*13025SEric.Taylor@oracle.com 				    "unmount '%s': not currently "
3777*13025SEric.Taylor@oracle.com 				    "mounted\n"),
3778*13025SEric.Taylor@oracle.com 				    zfs_get_name(zhp));
3779*13025SEric.Taylor@oracle.com 				ret = 1;
3780*13025SEric.Taylor@oracle.com 			} else if (zfs_unmountall(zhp, flags) != 0) {
3781*13025SEric.Taylor@oracle.com 				ret = 1;
3782*13025SEric.Taylor@oracle.com 			}
3783*13025SEric.Taylor@oracle.com 			break;
3784789Sahrens 		}
3785789Sahrens 
3786789Sahrens 		zfs_close(zhp);
3787789Sahrens 	}
3788789Sahrens 
3789789Sahrens 	return (ret);
3790789Sahrens }
3791789Sahrens 
3792789Sahrens /*
3793789Sahrens  * zfs unmount -a
3794789Sahrens  * zfs unmount filesystem
3795789Sahrens  *
3796789Sahrens  * Unmount all filesystems, or a specific ZFS filesystem.
3797789Sahrens  */
3798789Sahrens static int
3799789Sahrens zfs_do_unmount(int argc, char **argv)
3800789Sahrens {
3801789Sahrens 	return (unshare_unmount(OP_MOUNT, argc, argv));
3802789Sahrens }
3803789Sahrens 
3804789Sahrens /*
3805789Sahrens  * zfs unshare -a
3806789Sahrens  * zfs unshare filesystem
3807789Sahrens  *
3808789Sahrens  * Unshare all filesystems, or a specific ZFS filesystem.
3809789Sahrens  */
3810789Sahrens static int
3811789Sahrens zfs_do_unshare(int argc, char **argv)
3812789Sahrens {
3813789Sahrens 	return (unshare_unmount(OP_SHARE, argc, argv));
3814789Sahrens }
3815789Sahrens 
38169396SMatthew.Ahrens@Sun.COM /* ARGSUSED */
38179396SMatthew.Ahrens@Sun.COM static int
38189396SMatthew.Ahrens@Sun.COM zfs_do_python(int argc, char **argv)
38199396SMatthew.Ahrens@Sun.COM {
38209396SMatthew.Ahrens@Sun.COM 	(void) execv(pypath, argv-1);
38219396SMatthew.Ahrens@Sun.COM 	(void) printf("internal error: %s not found\n", pypath);
38229396SMatthew.Ahrens@Sun.COM 	return (-1);
38239396SMatthew.Ahrens@Sun.COM }
38249396SMatthew.Ahrens@Sun.COM 
3825789Sahrens /*
3826789Sahrens  * Called when invoked as /etc/fs/zfs/mount.  Do the mount if the mountpoint is
3827789Sahrens  * 'legacy'.  Otherwise, complain that use should be using 'zfs mount'.
3828789Sahrens  */
3829789Sahrens static int
3830789Sahrens manual_mount(int argc, char **argv)
3831789Sahrens {
3832789Sahrens 	zfs_handle_t *zhp;
3833789Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
3834789Sahrens 	char mntopts[MNT_LINE_MAX] = { '\0' };
3835789Sahrens 	int ret;
3836789Sahrens 	int c;
3837789Sahrens 	int flags = 0;
3838789Sahrens 	char *dataset, *path;
3839789Sahrens 
3840789Sahrens 	/* check options */
38411544Seschrock 	while ((c = getopt(argc, argv, ":mo:O")) != -1) {
3842789Sahrens 		switch (c) {
3843789Sahrens 		case 'o':
3844789Sahrens 			(void) strlcpy(mntopts, optarg, sizeof (mntopts));
3845789Sahrens 			break;
3846789Sahrens 		case 'O':
3847789Sahrens 			flags |= MS_OVERLAY;
3848789Sahrens 			break;
38491544Seschrock 		case 'm':
38501544Seschrock 			flags |= MS_NOMNTTAB;
38511544Seschrock 			break;
3852789Sahrens 		case ':':
3853789Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
3854789Sahrens 			    "'%c' option\n"), optopt);
38552082Seschrock 			usage(B_FALSE);
3856789Sahrens 			break;
3857789Sahrens 		case '?':
3858789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3859789Sahrens 			    optopt);
3860789Sahrens 			(void) fprintf(stderr, gettext("usage: mount [-o opts] "
3861789Sahrens 			    "<path>\n"));
3862789Sahrens 			return (2);
3863789Sahrens 		}
3864789Sahrens 	}
3865789Sahrens 
3866789Sahrens 	argc -= optind;
3867789Sahrens 	argv += optind;
3868789Sahrens 
3869789Sahrens 	/* check that we only have two arguments */
3870789Sahrens 	if (argc != 2) {
3871789Sahrens 		if (argc == 0)
3872789Sahrens 			(void) fprintf(stderr, gettext("missing dataset "
3873789Sahrens 			    "argument\n"));
3874789Sahrens 		else if (argc == 1)
3875789Sahrens 			(void) fprintf(stderr,
3876789Sahrens 			    gettext("missing mountpoint argument\n"));
3877789Sahrens 		else
3878789Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
3879789Sahrens 		(void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
3880789Sahrens 		return (2);
3881789Sahrens 	}
3882789Sahrens 
3883789Sahrens 	dataset = argv[0];
3884789Sahrens 	path = argv[1];
3885789Sahrens 
3886789Sahrens 	/* try to open the dataset */
38872082Seschrock 	if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
3888789Sahrens 		return (1);
3889789Sahrens 
3890789Sahrens 	(void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
38912082Seschrock 	    sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
3892789Sahrens 
3893789Sahrens 	/* check for legacy mountpoint and complain appropriately */
3894789Sahrens 	ret = 0;
3895789Sahrens 	if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
3896789Sahrens 		if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS,
3897789Sahrens 		    NULL, 0, mntopts, sizeof (mntopts)) != 0) {
3898789Sahrens 			(void) fprintf(stderr, gettext("mount failed: %s\n"),
3899789Sahrens 			    strerror(errno));
3900789Sahrens 			ret = 1;
3901789Sahrens 		}
3902789Sahrens 	} else {
3903789Sahrens 		(void) fprintf(stderr, gettext("filesystem '%s' cannot be "
3904789Sahrens 		    "mounted using 'mount -F zfs'\n"), dataset);
3905789Sahrens 		(void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
3906789Sahrens 		    "instead.\n"), path);
3907789Sahrens 		(void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
3908789Sahrens 		    "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
3909789Sahrens 		(void) fprintf(stderr, gettext("See zfs(1M) for more "
3910789Sahrens 		    "information.\n"));
3911789Sahrens 		ret = 1;
3912789Sahrens 	}
3913789Sahrens 
3914789Sahrens 	return (ret);
3915789Sahrens }
3916789Sahrens 
3917789Sahrens /*
3918789Sahrens  * Called when invoked as /etc/fs/zfs/umount.  Unlike a manual mount, we allow
3919789Sahrens  * unmounts of non-legacy filesystems, as this is the dominant administrative
3920789Sahrens  * interface.
3921789Sahrens  */
3922789Sahrens static int
3923789Sahrens manual_unmount(int argc, char **argv)
3924789Sahrens {
3925789Sahrens 	int flags = 0;
3926789Sahrens 	int c;
3927789Sahrens 
3928789Sahrens 	/* check options */
3929789Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
3930789Sahrens 		switch (c) {
3931789Sahrens 		case 'f':
3932789Sahrens 			flags = MS_FORCE;
3933789Sahrens 			break;
3934789Sahrens 		case '?':
3935789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3936789Sahrens 			    optopt);
3937789Sahrens 			(void) fprintf(stderr, gettext("usage: unmount [-f] "
3938789Sahrens 			    "<path>\n"));
3939789Sahrens 			return (2);
3940789Sahrens 		}
3941789Sahrens 	}
3942789Sahrens 
3943789Sahrens 	argc -= optind;
3944789Sahrens 	argv += optind;
3945789Sahrens 
3946789Sahrens 	/* check arguments */
3947789Sahrens 	if (argc != 1) {
3948789Sahrens 		if (argc == 0)
3949789Sahrens 			(void) fprintf(stderr, gettext("missing path "
3950789Sahrens 			    "argument\n"));
3951789Sahrens 		else
3952789Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
3953789Sahrens 		(void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
3954789Sahrens 		return (2);
3955789Sahrens 	}
3956789Sahrens 
39572082Seschrock 	return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
3958789Sahrens }
3959789Sahrens 
3960789Sahrens static int
39614543Smarks find_command_idx(char *command, int *idx)
39624543Smarks {
39634543Smarks 	int i;
39644543Smarks 
39654543Smarks 	for (i = 0; i < NCOMMAND; i++) {
39664543Smarks 		if (command_table[i].name == NULL)
39674543Smarks 			continue;
39684543Smarks 
39694543Smarks 		if (strcmp(command, command_table[i].name) == 0) {
39704543Smarks 			*idx = i;
39714543Smarks 			return (0);
39724543Smarks 		}
39734543Smarks 	}
39744543Smarks 	return (1);
39754543Smarks }
39764543Smarks 
3977789Sahrens int
3978789Sahrens main(int argc, char **argv)
3979789Sahrens {
3980789Sahrens 	int ret;
3981789Sahrens 	int i;
3982789Sahrens 	char *progname;
3983789Sahrens 	char *cmdname;
3984789Sahrens 
3985789Sahrens 	(void) setlocale(LC_ALL, "");
3986789Sahrens 	(void) textdomain(TEXT_DOMAIN);
3987789Sahrens 
3988789Sahrens 	opterr = 0;
3989789Sahrens 
39902082Seschrock 	if ((g_zfs = libzfs_init()) == NULL) {
39912082Seschrock 		(void) fprintf(stderr, gettext("internal error: failed to "
39922082Seschrock 		    "initialize ZFS library\n"));
39932082Seschrock 		return (1);
39942082Seschrock 	}
39952082Seschrock 
39964988Sek110237 	zpool_set_history_str("zfs", argc, argv, history_str);
39974988Sek110237 	verify(zpool_stage_history(g_zfs, history_str) == 0);
39984543Smarks 
39992082Seschrock 	libzfs_print_on_error(g_zfs, B_TRUE);
40002082Seschrock 
4001789Sahrens 	if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
4002789Sahrens 		(void) fprintf(stderr, gettext("internal error: unable to "
4003789Sahrens 		    "open %s\n"), MNTTAB);
4004789Sahrens 		return (1);
4005789Sahrens 	}
4006789Sahrens 
4007789Sahrens 	/*
4008789Sahrens 	 * This command also doubles as the /etc/fs mount and unmount program.
4009789Sahrens 	 * Determine if we should take this behavior based on argv[0].
4010789Sahrens 	 */
4011789Sahrens 	progname = basename(argv[0]);
4012789Sahrens 	if (strcmp(progname, "mount") == 0) {
4013789Sahrens 		ret = manual_mount(argc, argv);
4014789Sahrens 	} else if (strcmp(progname, "umount") == 0) {
4015789Sahrens 		ret = manual_unmount(argc, argv);
4016789Sahrens 	} else {
4017789Sahrens 		/*
4018789Sahrens 		 * Make sure the user has specified some command.
4019789Sahrens 		 */
4020789Sahrens 		if (argc < 2) {
4021789Sahrens 			(void) fprintf(stderr, gettext("missing command\n"));
40222082Seschrock 			usage(B_FALSE);
4023789Sahrens 		}
4024789Sahrens 
4025789Sahrens 		cmdname = argv[1];
4026789Sahrens 
4027789Sahrens 		/*
4028789Sahrens 		 * The 'umount' command is an alias for 'unmount'
4029789Sahrens 		 */
4030789Sahrens 		if (strcmp(cmdname, "umount") == 0)
4031789Sahrens 			cmdname = "unmount";
4032789Sahrens 
4033789Sahrens 		/*
40341749Sahrens 		 * The 'recv' command is an alias for 'receive'
40351749Sahrens 		 */
40361749Sahrens 		if (strcmp(cmdname, "recv") == 0)
40371749Sahrens 			cmdname = "receive";
40381749Sahrens 
40391749Sahrens 		/*
4040789Sahrens 		 * Special case '-?'
4041789Sahrens 		 */
4042789Sahrens 		if (strcmp(cmdname, "-?") == 0)
40432082Seschrock 			usage(B_TRUE);
4044789Sahrens 
4045789Sahrens 		/*
4046789Sahrens 		 * Run the appropriate command.
4047789Sahrens 		 */
40488811SEric.Taylor@Sun.COM 		libzfs_mnttab_cache(g_zfs, B_TRUE);
40494543Smarks 		if (find_command_idx(cmdname, &i) == 0) {
40504543Smarks 			current_command = &command_table[i];
40514543Smarks 			ret = command_table[i].func(argc - 1, argv + 1);
40524787Sahrens 		} else if (strchr(cmdname, '=') != NULL) {
40534787Sahrens 			verify(find_command_idx("set", &i) == 0);
40544787Sahrens 			current_command = &command_table[i];
40554787Sahrens 			ret = command_table[i].func(argc, argv);
40564787Sahrens 		} else {
4057789Sahrens 			(void) fprintf(stderr, gettext("unrecognized "
4058789Sahrens 			    "command '%s'\n"), cmdname);
40592082Seschrock 			usage(B_FALSE);
4060789Sahrens 		}
40618811SEric.Taylor@Sun.COM 		libzfs_mnttab_cache(g_zfs, B_FALSE);
4062789Sahrens 	}
4063789Sahrens 
4064789Sahrens 	(void) fclose(mnttab_file);
4065789Sahrens 
40662082Seschrock 	libzfs_fini(g_zfs);
40672082Seschrock 
4068789Sahrens 	/*
4069789Sahrens 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4070789Sahrens 	 * for the purposes of running ::findleaks.
4071789Sahrens 	 */
4072789Sahrens 	if (getenv("ZFS_ABORT") != NULL) {
4073789Sahrens 		(void) printf("dumping core by request\n");
4074789Sahrens 		abort();
4075789Sahrens 	}
4076789Sahrens 
4077789Sahrens 	return (ret);
4078789Sahrens }
4079