xref: /onnv-gate/usr/src/cmd/zfs/zfs_main.c (revision 11935:538c866aaac6)
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 /*
2311417SChris.Kirby@sun.com  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #include <assert.h>
282676Seschrock #include <ctype.h>
29789Sahrens #include <errno.h>
30789Sahrens #include <libgen.h>
31789Sahrens #include <libintl.h>
32789Sahrens #include <libuutil.h>
334543Smarks #include <libnvpair.h>
34789Sahrens #include <locale.h>
35789Sahrens #include <stddef.h>
36789Sahrens #include <stdio.h>
37789Sahrens #include <stdlib.h>
38789Sahrens #include <strings.h>
39789Sahrens #include <unistd.h>
40789Sahrens #include <fcntl.h>
41789Sahrens #include <zone.h>
429396SMatthew.Ahrens@Sun.COM #include <grp.h>
439396SMatthew.Ahrens@Sun.COM #include <pwd.h>
44789Sahrens #include <sys/mkdev.h>
45789Sahrens #include <sys/mntent.h>
46789Sahrens #include <sys/mnttab.h>
47789Sahrens #include <sys/mount.h>
48789Sahrens #include <sys/stat.h>
499396SMatthew.Ahrens@Sun.COM #include <sys/fs/zfs.h>
50789Sahrens 
51789Sahrens #include <libzfs.h>
524543Smarks #include <libuutil.h>
53789Sahrens 
54789Sahrens #include "zfs_iter.h"
552082Seschrock #include "zfs_util.h"
56*11935SMark.Shellenbaum@Sun.COM #include "zfs_comutil.h"
572082Seschrock 
582082Seschrock libzfs_handle_t *g_zfs;
59789Sahrens 
60789Sahrens static FILE *mnttab_file;
614988Sek110237 static char history_str[HIS_MAX_RECORD_LEN];
629396SMatthew.Ahrens@Sun.COM const char *pypath = "/usr/lib/zfs/pyzfs.py";
63789Sahrens 
64789Sahrens static int zfs_do_clone(int argc, char **argv);
65789Sahrens static int zfs_do_create(int argc, char **argv);
66789Sahrens static int zfs_do_destroy(int argc, char **argv);
67789Sahrens static int zfs_do_get(int argc, char **argv);
68789Sahrens static int zfs_do_inherit(int argc, char **argv);
69789Sahrens static int zfs_do_list(int argc, char **argv);
70789Sahrens static int zfs_do_mount(int argc, char **argv);
71789Sahrens static int zfs_do_rename(int argc, char **argv);
72789Sahrens static int zfs_do_rollback(int argc, char **argv);
73789Sahrens static int zfs_do_set(int argc, char **argv);
744577Sahrens static int zfs_do_upgrade(int argc, char **argv);
75789Sahrens static int zfs_do_snapshot(int argc, char **argv);
76789Sahrens static int zfs_do_unmount(int argc, char **argv);
77789Sahrens static int zfs_do_share(int argc, char **argv);
78789Sahrens static int zfs_do_unshare(int argc, char **argv);
791749Sahrens static int zfs_do_send(int argc, char **argv);
801749Sahrens static int zfs_do_receive(int argc, char **argv);
812082Seschrock static int zfs_do_promote(int argc, char **argv);
829396SMatthew.Ahrens@Sun.COM static int zfs_do_userspace(int argc, char **argv);
839396SMatthew.Ahrens@Sun.COM static int zfs_do_python(int argc, char **argv);
8410242Schris.kirby@sun.com static int zfs_do_hold(int argc, char **argv);
8510242Schris.kirby@sun.com static int zfs_do_release(int argc, char **argv);
86789Sahrens 
87789Sahrens /*
886865Srm160521  * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
89789Sahrens  */
906865Srm160521 
916865Srm160521 #ifdef DEBUG
92789Sahrens const char *
933126Sahl _umem_debug_init(void)
94789Sahrens {
95789Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
96789Sahrens }
97789Sahrens 
98789Sahrens const char *
99789Sahrens _umem_logging_init(void)
100789Sahrens {
101789Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
102789Sahrens }
1036865Srm160521 #endif
104789Sahrens 
1051387Seschrock typedef enum {
1061387Seschrock 	HELP_CLONE,
1071387Seschrock 	HELP_CREATE,
1081387Seschrock 	HELP_DESTROY,
1091387Seschrock 	HELP_GET,
1101387Seschrock 	HELP_INHERIT,
1114577Sahrens 	HELP_UPGRADE,
1121387Seschrock 	HELP_LIST,
1131387Seschrock 	HELP_MOUNT,
1142082Seschrock 	HELP_PROMOTE,
1151749Sahrens 	HELP_RECEIVE,
1161387Seschrock 	HELP_RENAME,
1171387Seschrock 	HELP_ROLLBACK,
1181749Sahrens 	HELP_SEND,
1191387Seschrock 	HELP_SET,
1201387Seschrock 	HELP_SHARE,
1211387Seschrock 	HELP_SNAPSHOT,
1221387Seschrock 	HELP_UNMOUNT,
1234543Smarks 	HELP_UNSHARE,
1244543Smarks 	HELP_ALLOW,
1259396SMatthew.Ahrens@Sun.COM 	HELP_UNALLOW,
1269396SMatthew.Ahrens@Sun.COM 	HELP_USERSPACE,
12710242Schris.kirby@sun.com 	HELP_GROUPSPACE,
12810242Schris.kirby@sun.com 	HELP_HOLD,
12910242Schris.kirby@sun.com 	HELP_HOLDS,
13010242Schris.kirby@sun.com 	HELP_RELEASE
1311387Seschrock } zfs_help_t;
1321387Seschrock 
133789Sahrens typedef struct zfs_command {
134789Sahrens 	const char	*name;
135789Sahrens 	int		(*func)(int argc, char **argv);
1361387Seschrock 	zfs_help_t	usage;
137789Sahrens } zfs_command_t;
138789Sahrens 
139789Sahrens /*
140789Sahrens  * Master command table.  Each ZFS command has a name, associated function, and
1411544Seschrock  * usage message.  The usage messages need to be internationalized, so we have
1421544Seschrock  * to have a function to return the usage message based on a command index.
1431387Seschrock  *
1441387Seschrock  * These commands are organized according to how they are displayed in the usage
1451387Seschrock  * message.  An empty command (one with a NULL name) indicates an empty line in
1461387Seschrock  * the generic usage message.
147789Sahrens  */
148789Sahrens static zfs_command_t command_table[] = {
1491387Seschrock 	{ "create",	zfs_do_create,		HELP_CREATE		},
1501387Seschrock 	{ "destroy",	zfs_do_destroy,		HELP_DESTROY		},
151789Sahrens 	{ NULL },
1521387Seschrock 	{ "snapshot",	zfs_do_snapshot,	HELP_SNAPSHOT		},
1531387Seschrock 	{ "rollback",	zfs_do_rollback,	HELP_ROLLBACK		},
1541387Seschrock 	{ "clone",	zfs_do_clone,		HELP_CLONE		},
1552082Seschrock 	{ "promote",	zfs_do_promote,		HELP_PROMOTE		},
1561387Seschrock 	{ "rename",	zfs_do_rename,		HELP_RENAME		},
157789Sahrens 	{ NULL },
1581387Seschrock 	{ "list",	zfs_do_list,		HELP_LIST		},
159789Sahrens 	{ NULL },
1601387Seschrock 	{ "set",	zfs_do_set,		HELP_SET		},
16111022STom.Erickson@Sun.COM 	{ "get",	zfs_do_get,		HELP_GET		},
1621387Seschrock 	{ "inherit",	zfs_do_inherit,		HELP_INHERIT		},
1634577Sahrens 	{ "upgrade",	zfs_do_upgrade,		HELP_UPGRADE		},
1649396SMatthew.Ahrens@Sun.COM 	{ "userspace",	zfs_do_userspace,	HELP_USERSPACE		},
1659396SMatthew.Ahrens@Sun.COM 	{ "groupspace",	zfs_do_userspace,	HELP_GROUPSPACE		},
166789Sahrens 	{ NULL },
1671387Seschrock 	{ "mount",	zfs_do_mount,		HELP_MOUNT		},
1681387Seschrock 	{ "unmount",	zfs_do_unmount,		HELP_UNMOUNT		},
1691387Seschrock 	{ "share",	zfs_do_share,		HELP_SHARE		},
1701387Seschrock 	{ "unshare",	zfs_do_unshare,		HELP_UNSHARE		},
171789Sahrens 	{ NULL },
1721749Sahrens 	{ "send",	zfs_do_send,		HELP_SEND		},
1731749Sahrens 	{ "receive",	zfs_do_receive,		HELP_RECEIVE		},
1744543Smarks 	{ NULL },
1759396SMatthew.Ahrens@Sun.COM 	{ "allow",	zfs_do_python,		HELP_ALLOW		},
1764543Smarks 	{ NULL },
1779396SMatthew.Ahrens@Sun.COM 	{ "unallow",	zfs_do_python,		HELP_UNALLOW		},
17810242Schris.kirby@sun.com 	{ NULL },
17910242Schris.kirby@sun.com 	{ "hold",	zfs_do_hold,		HELP_HOLD		},
18010242Schris.kirby@sun.com 	{ "holds",	zfs_do_python,		HELP_HOLDS		},
18110242Schris.kirby@sun.com 	{ "release",	zfs_do_release,		HELP_RELEASE		},
182789Sahrens };
183789Sahrens 
184789Sahrens #define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
185789Sahrens 
186789Sahrens zfs_command_t *current_command;
187789Sahrens 
1881387Seschrock static const char *
1891387Seschrock get_usage(zfs_help_t idx)
1901387Seschrock {
1911387Seschrock 	switch (idx) {
1921387Seschrock 	case HELP_CLONE:
1937265Sahrens 		return (gettext("\tclone [-p] [-o property=value] ... "
1947265Sahrens 		    "<snapshot> <filesystem|volume>\n"));
1951387Seschrock 	case HELP_CREATE:
1964849Sahrens 		return (gettext("\tcreate [-p] [-o property=value] ... "
1972676Seschrock 		    "<filesystem>\n"
1984849Sahrens 		    "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
1994849Sahrens 		    "-V <size> <volume>\n"));
2001387Seschrock 	case HELP_DESTROY:
20110385Schris.kirby@sun.com 		return (gettext("\tdestroy [-rRf] <filesystem|volume>\n"
20210385Schris.kirby@sun.com 		    "\tdestroy [-rRd] <snapshot>\n"));
2031387Seschrock 	case HELP_GET:
2049365SChris.Gerhard@sun.com 		return (gettext("\tget [-rHp] [-d max] "
20511022STom.Erickson@Sun.COM 		    "[-o \"all\" | field[,...]] [-s source[,...]]\n"
2064849Sahrens 		    "\t    <\"all\" | property[,...]> "
2072676Seschrock 		    "[filesystem|volume|snapshot] ...\n"));
2081387Seschrock 	case HELP_INHERIT:
20911022STom.Erickson@Sun.COM 		return (gettext("\tinherit [-rS] <property> "
2107265Sahrens 		    "<filesystem|volume|snapshot> ...\n"));
2114577Sahrens 	case HELP_UPGRADE:
2124577Sahrens 		return (gettext("\tupgrade [-v]\n"
2134577Sahrens 		    "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
2141387Seschrock 	case HELP_LIST:
2159365SChris.Gerhard@sun.com 		return (gettext("\tlist [-rH][-d max] "
2169365SChris.Gerhard@sun.com 		    "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
2174849Sahrens 		    "\t    [-S property] ... "
2184849Sahrens 		    "[filesystem|volume|snapshot] ...\n"));
2191387Seschrock 	case HELP_MOUNT:
2201387Seschrock 		return (gettext("\tmount\n"
2214849Sahrens 		    "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
2222082Seschrock 	case HELP_PROMOTE:
2234849Sahrens 		return (gettext("\tpromote <clone-filesystem>\n"));
2241749Sahrens 	case HELP_RECEIVE:
2252665Snd150628 		return (gettext("\treceive [-vnF] <filesystem|volume|"
2262665Snd150628 		"snapshot>\n"
2272665Snd150628 		"\treceive [-vnF] -d <filesystem>\n"));
2281387Seschrock 	case HELP_RENAME:
2291387Seschrock 		return (gettext("\trename <filesystem|volume|snapshot> "
2304007Smmusante 		    "<filesystem|volume|snapshot>\n"
2314490Svb160487 		    "\trename -p <filesystem|volume> <filesystem|volume>\n"
2324007Smmusante 		    "\trename -r <snapshot> <snapshot>"));
2331387Seschrock 	case HELP_ROLLBACK:
2345568Sahrens 		return (gettext("\trollback [-rRf] <snapshot>\n"));
2351749Sahrens 	case HELP_SEND:
23611022STom.Erickson@Sun.COM 		return (gettext("\tsend [-RDp] [-[iI] snapshot] <snapshot>\n"));
2371387Seschrock 	case HELP_SET:
2381387Seschrock 		return (gettext("\tset <property=value> "
2397265Sahrens 		    "<filesystem|volume|snapshot> ...\n"));
2401387Seschrock 	case HELP_SHARE:
2414849Sahrens 		return (gettext("\tshare <-a | filesystem>\n"));
2421387Seschrock 	case HELP_SNAPSHOT:
2437265Sahrens 		return (gettext("\tsnapshot [-r] [-o property=value] ... "
2444849Sahrens 		    "<filesystem@snapname|volume@snapname>\n"));
2451387Seschrock 	case HELP_UNMOUNT:
2464849Sahrens 		return (gettext("\tunmount [-f] "
2474849Sahrens 		    "<-a | filesystem|mountpoint>\n"));
2481387Seschrock 	case HELP_UNSHARE:
24910228SStephanie.Scheffler@Sun.COM 		return (gettext("\tunshare "
2504849Sahrens 		    "<-a | filesystem|mountpoint>\n"));
2514543Smarks 	case HELP_ALLOW:
2529564SStephanie.Scheffler@Sun.COM 		return (gettext("\tallow <filesystem|volume>\n"
2539564SStephanie.Scheffler@Sun.COM 		    "\tallow [-ldug] "
2544849Sahrens 		    "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
2554849Sahrens 		    "\t    <filesystem|volume>\n"
2564849Sahrens 		    "\tallow [-ld] -e <perm|@setname>[,...] "
2574849Sahrens 		    "<filesystem|volume>\n"
2584849Sahrens 		    "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
2594849Sahrens 		    "\tallow -s @setname <perm|@setname>[,...] "
2604849Sahrens 		    "<filesystem|volume>\n"));
2614543Smarks 	case HELP_UNALLOW:
2624849Sahrens 		return (gettext("\tunallow [-rldug] "
2634849Sahrens 		    "<\"everyone\"|user|group>[,...]\n"
2644849Sahrens 		    "\t    [<perm|@setname>[,...]] <filesystem|volume>\n"
2654849Sahrens 		    "\tunallow [-rld] -e [<perm|@setname>[,...]] "
2664849Sahrens 		    "<filesystem|volume>\n"
2674849Sahrens 		    "\tunallow [-r] -c [<perm|@setname>[,...]] "
2684849Sahrens 		    "<filesystem|volume>\n"
2694849Sahrens 		    "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
2704849Sahrens 		    "<filesystem|volume>\n"));
2719396SMatthew.Ahrens@Sun.COM 	case HELP_USERSPACE:
2729396SMatthew.Ahrens@Sun.COM 		return (gettext("\tuserspace [-hniHp] [-o field[,...]] "
2739396SMatthew.Ahrens@Sun.COM 		    "[-sS field] ... [-t type[,...]]\n"
2749396SMatthew.Ahrens@Sun.COM 		    "\t    <filesystem|snapshot>\n"));
2759396SMatthew.Ahrens@Sun.COM 	case HELP_GROUPSPACE:
2769396SMatthew.Ahrens@Sun.COM 		return (gettext("\tgroupspace [-hniHpU] [-o field[,...]] "
2779396SMatthew.Ahrens@Sun.COM 		    "[-sS field] ... [-t type[,...]]\n"
2789396SMatthew.Ahrens@Sun.COM 		    "\t    <filesystem|snapshot>\n"));
27910242Schris.kirby@sun.com 	case HELP_HOLD:
28010242Schris.kirby@sun.com 		return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
28110242Schris.kirby@sun.com 	case HELP_HOLDS:
28210242Schris.kirby@sun.com 		return (gettext("\tholds [-r] <snapshot> ...\n"));
28310242Schris.kirby@sun.com 	case HELP_RELEASE:
28410242Schris.kirby@sun.com 		return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
2851387Seschrock 	}
2861387Seschrock 
2871387Seschrock 	abort();
2881387Seschrock 	/* NOTREACHED */
2891387Seschrock }
2901387Seschrock 
291789Sahrens /*
292789Sahrens  * Utility function to guarantee malloc() success.
293789Sahrens  */
294789Sahrens void *
295789Sahrens safe_malloc(size_t size)
296789Sahrens {
297789Sahrens 	void *data;
298789Sahrens 
299789Sahrens 	if ((data = calloc(1, size)) == NULL) {
300789Sahrens 		(void) fprintf(stderr, "internal error: out of memory\n");
301789Sahrens 		exit(1);
302789Sahrens 	}
303789Sahrens 
304789Sahrens 	return (data);
305789Sahrens }
306789Sahrens 
307789Sahrens /*
3085094Slling  * Callback routine that will print out information for each of
3093654Sgw25295  * the properties.
3103654Sgw25295  */
3115094Slling static int
3125094Slling usage_prop_cb(int prop, void *cb)
3133654Sgw25295 {
3143654Sgw25295 	FILE *fp = cb;
3153654Sgw25295 
3166643Seschrock 	(void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
3176643Seschrock 
3186643Seschrock 	if (zfs_prop_readonly(prop))
3196643Seschrock 		(void) fprintf(fp, " NO    ");
3203654Sgw25295 	else
3216643Seschrock 		(void) fprintf(fp, "YES    ");
3223654Sgw25295 
3233654Sgw25295 	if (zfs_prop_inheritable(prop))
3243654Sgw25295 		(void) fprintf(fp, "  YES   ");
3253654Sgw25295 	else
3263654Sgw25295 		(void) fprintf(fp, "   NO   ");
3273654Sgw25295 
3283654Sgw25295 	if (zfs_prop_values(prop) == NULL)
3293654Sgw25295 		(void) fprintf(fp, "-\n");
3303654Sgw25295 	else
3313654Sgw25295 		(void) fprintf(fp, "%s\n", zfs_prop_values(prop));
3323654Sgw25295 
3335094Slling 	return (ZPROP_CONT);
3343654Sgw25295 }
3353654Sgw25295 
3363654Sgw25295 /*
337789Sahrens  * Display usage message.  If we're inside a command, display only the usage for
338789Sahrens  * that command.  Otherwise, iterate over the entire command table and display
339789Sahrens  * a complete usage message.
340789Sahrens  */
341789Sahrens static void
3422082Seschrock usage(boolean_t requested)
343789Sahrens {
344789Sahrens 	int i;
3452082Seschrock 	boolean_t show_properties = B_FALSE;
346789Sahrens 	FILE *fp = requested ? stdout : stderr;
347789Sahrens 
348789Sahrens 	if (current_command == NULL) {
349789Sahrens 
350789Sahrens 		(void) fprintf(fp, gettext("usage: zfs command args ...\n"));
351789Sahrens 		(void) fprintf(fp,
352789Sahrens 		    gettext("where 'command' is one of the following:\n\n"));
353789Sahrens 
354789Sahrens 		for (i = 0; i < NCOMMAND; i++) {
355789Sahrens 			if (command_table[i].name == NULL)
356789Sahrens 				(void) fprintf(fp, "\n");
357789Sahrens 			else
358789Sahrens 				(void) fprintf(fp, "%s",
3591387Seschrock 				    get_usage(command_table[i].usage));
360789Sahrens 		}
361789Sahrens 
362789Sahrens 		(void) fprintf(fp, gettext("\nEach dataset is of the form: "
363789Sahrens 		    "pool/[dataset/]*dataset[@name]\n"));
364789Sahrens 	} else {
365789Sahrens 		(void) fprintf(fp, gettext("usage:\n"));
3661387Seschrock 		(void) fprintf(fp, "%s", get_usage(current_command->usage));
367789Sahrens 	}
368789Sahrens 
3692190Sdarrenm 	if (current_command != NULL &&
3702190Sdarrenm 	    (strcmp(current_command->name, "set") == 0 ||
371789Sahrens 	    strcmp(current_command->name, "get") == 0 ||
372789Sahrens 	    strcmp(current_command->name, "inherit") == 0 ||
3732190Sdarrenm 	    strcmp(current_command->name, "list") == 0))
3742082Seschrock 		show_properties = B_TRUE;
375789Sahrens 
376789Sahrens 	if (show_properties) {
377789Sahrens 		(void) fprintf(fp,
378789Sahrens 		    gettext("\nThe following properties are supported:\n"));
379789Sahrens 
3805378Sck153898 		(void) fprintf(fp, "\n\t%-14s %s  %s   %s\n\n",
381789Sahrens 		    "PROPERTY", "EDIT", "INHERIT", "VALUES");
382789Sahrens 
3833654Sgw25295 		/* Iterate over all properties */
3845094Slling 		(void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
3855094Slling 		    ZFS_TYPE_DATASET);
3863654Sgw25295 
3879396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, "\t%-15s ", "userused@...");
3889396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, " NO       NO   <size>\n");
3899396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, "\t%-15s ", "groupused@...");
3909396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, " NO       NO   <size>\n");
3919396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, "\t%-15s ", "userquota@...");
3929396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, "YES       NO   <size> | none\n");
3939396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, "\t%-15s ", "groupquota@...");
3949396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, "YES       NO   <size> | none\n");
3959396SMatthew.Ahrens@Sun.COM 
396789Sahrens 		(void) fprintf(fp, gettext("\nSizes are specified in bytes "
397789Sahrens 		    "with standard units such as K, M, G, etc.\n"));
3987390SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, gettext("\nUser-defined properties can "
3992676Seschrock 		    "be specified by using a name containing a colon (:).\n"));
4009396SMatthew.Ahrens@Sun.COM 		(void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
4019396SMatthew.Ahrens@Sun.COM 		    "properties must be appended with\n"
4029396SMatthew.Ahrens@Sun.COM 		    "a user or group specifier of one of these forms:\n"
4039396SMatthew.Ahrens@Sun.COM 		    "    POSIX name      (eg: \"matt\")\n"
4049396SMatthew.Ahrens@Sun.COM 		    "    POSIX id        (eg: \"126829\")\n"
4059396SMatthew.Ahrens@Sun.COM 		    "    SMB name@domain (eg: \"matt@sun\")\n"
4069396SMatthew.Ahrens@Sun.COM 		    "    SMB SID         (eg: \"S-1-234-567-89\")\n"));
4072190Sdarrenm 	} else {
4082190Sdarrenm 		(void) fprintf(fp,
4098269SMark.Musante@Sun.COM 		    gettext("\nFor the property list, run: %s\n"),
4108269SMark.Musante@Sun.COM 		    "zfs set|get");
4115993Smarks 		(void) fprintf(fp,
4128269SMark.Musante@Sun.COM 		    gettext("\nFor the delegated permission list, run: %s\n"),
4138269SMark.Musante@Sun.COM 		    "zfs allow|unallow");
414789Sahrens 	}
415789Sahrens 
4162676Seschrock 	/*
4172676Seschrock 	 * See comments at end of main().
4182676Seschrock 	 */
4192676Seschrock 	if (getenv("ZFS_ABORT") != NULL) {
4202676Seschrock 		(void) printf("dumping core by request\n");
4212676Seschrock 		abort();
4222676Seschrock 	}
4232676Seschrock 
424789Sahrens 	exit(requested ? 0 : 2);
425789Sahrens }
426789Sahrens 
4277265Sahrens static int
4287265Sahrens parseprop(nvlist_t *props)
4297265Sahrens {
4307265Sahrens 	char *propname = optarg;
4317265Sahrens 	char *propval, *strval;
4327265Sahrens 
4337265Sahrens 	if ((propval = strchr(propname, '=')) == NULL) {
4347265Sahrens 		(void) fprintf(stderr, gettext("missing "
4357265Sahrens 		    "'=' for -o option\n"));
4367265Sahrens 		return (-1);
4377265Sahrens 	}
4387265Sahrens 	*propval = '\0';
4397265Sahrens 	propval++;
4407265Sahrens 	if (nvlist_lookup_string(props, propname, &strval) == 0) {
4417265Sahrens 		(void) fprintf(stderr, gettext("property '%s' "
4427265Sahrens 		    "specified multiple times\n"), propname);
4437265Sahrens 		return (-1);
4447265Sahrens 	}
4457265Sahrens 	if (nvlist_add_string(props, propname, propval) != 0) {
4467265Sahrens 		(void) fprintf(stderr, gettext("internal "
4477265Sahrens 		    "error: out of memory\n"));
4487265Sahrens 		return (-1);
4497265Sahrens 	}
4507265Sahrens 	return (0);
4517265Sahrens }
4527265Sahrens 
4539365SChris.Gerhard@sun.com static int
4549365SChris.Gerhard@sun.com parse_depth(char *opt, int *flags)
4559365SChris.Gerhard@sun.com {
4569365SChris.Gerhard@sun.com 	char *tmp;
4579365SChris.Gerhard@sun.com 	int depth;
4589365SChris.Gerhard@sun.com 
4599365SChris.Gerhard@sun.com 	depth = (int)strtol(opt, &tmp, 0);
4609365SChris.Gerhard@sun.com 	if (*tmp) {
4619365SChris.Gerhard@sun.com 		(void) fprintf(stderr,
4629365SChris.Gerhard@sun.com 		    gettext("%s is not an integer\n"), optarg);
4639365SChris.Gerhard@sun.com 		usage(B_FALSE);
4649365SChris.Gerhard@sun.com 	}
4659365SChris.Gerhard@sun.com 	if (depth < 0) {
4669365SChris.Gerhard@sun.com 		(void) fprintf(stderr,
4679365SChris.Gerhard@sun.com 		    gettext("Depth can not be negative.\n"));
4689365SChris.Gerhard@sun.com 		usage(B_FALSE);
4699365SChris.Gerhard@sun.com 	}
4709365SChris.Gerhard@sun.com 	*flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
4719365SChris.Gerhard@sun.com 	return (depth);
4729365SChris.Gerhard@sun.com }
4739365SChris.Gerhard@sun.com 
474789Sahrens /*
4757265Sahrens  * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
476789Sahrens  *
477789Sahrens  * Given an existing dataset, create a writable copy whose initial contents
478789Sahrens  * are the same as the source.  The newly created dataset maintains a
479789Sahrens  * dependency on the original; the original cannot be destroyed so long as
480789Sahrens  * the clone exists.
4814490Svb160487  *
4824490Svb160487  * The '-p' flag creates all the non-existing ancestors of the target first.
483789Sahrens  */
484789Sahrens static int
485789Sahrens zfs_do_clone(int argc, char **argv)
486789Sahrens {
4877265Sahrens 	zfs_handle_t *zhp = NULL;
4884490Svb160487 	boolean_t parents = B_FALSE;
4897265Sahrens 	nvlist_t *props;
490789Sahrens 	int ret;
4914490Svb160487 	int c;
492789Sahrens 
4937265Sahrens 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
4947265Sahrens 		(void) fprintf(stderr, gettext("internal error: "
4957265Sahrens 		    "out of memory\n"));
4967265Sahrens 		return (1);
4977265Sahrens 	}
4987265Sahrens 
499789Sahrens 	/* check options */
5007265Sahrens 	while ((c = getopt(argc, argv, "o:p")) != -1) {
5014490Svb160487 		switch (c) {
5027265Sahrens 		case 'o':
5037265Sahrens 			if (parseprop(props))
5047265Sahrens 				return (1);
5057265Sahrens 			break;
5064490Svb160487 		case 'p':
5074490Svb160487 			parents = B_TRUE;
5084490Svb160487 			break;
5094490Svb160487 		case '?':
5104490Svb160487 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
5114490Svb160487 			    optopt);
5127265Sahrens 			goto usage;
5134490Svb160487 		}
514789Sahrens 	}
515789Sahrens 
5164490Svb160487 	argc -= optind;
5174490Svb160487 	argv += optind;
5184490Svb160487 
519789Sahrens 	/* check number of arguments */
5204490Svb160487 	if (argc < 1) {
521789Sahrens 		(void) fprintf(stderr, gettext("missing source dataset "
522789Sahrens 		    "argument\n"));
5237265Sahrens 		goto usage;
524789Sahrens 	}
5254490Svb160487 	if (argc < 2) {
526789Sahrens 		(void) fprintf(stderr, gettext("missing target dataset "
527789Sahrens 		    "argument\n"));
5287265Sahrens 		goto usage;
529789Sahrens 	}
5304490Svb160487 	if (argc > 2) {
531789Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
5327265Sahrens 		goto usage;
533789Sahrens 	}
534789Sahrens 
535789Sahrens 	/* open the source dataset */
5364490Svb160487 	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
537789Sahrens 		return (1);
538789Sahrens 
5394490Svb160487 	if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
5404490Svb160487 	    ZFS_TYPE_VOLUME)) {
5414490Svb160487 		/*
5424490Svb160487 		 * Now create the ancestors of the target dataset.  If the
5434490Svb160487 		 * target already exists and '-p' option was used we should not
5444490Svb160487 		 * complain.
5454490Svb160487 		 */
5464490Svb160487 		if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
5474490Svb160487 		    ZFS_TYPE_VOLUME))
5484490Svb160487 			return (0);
5494490Svb160487 		if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
5504490Svb160487 			return (1);
5514490Svb160487 	}
5524490Svb160487 
553789Sahrens 	/* pass to libzfs */
5547265Sahrens 	ret = zfs_clone(zhp, argv[1], props);
555789Sahrens 
556789Sahrens 	/* create the mountpoint if necessary */
557789Sahrens 	if (ret == 0) {
5585094Slling 		zfs_handle_t *clone;
5595094Slling 
5605094Slling 		clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
561789Sahrens 		if (clone != NULL) {
562789Sahrens 			if ((ret = zfs_mount(clone, NULL, 0)) == 0)
563789Sahrens 				ret = zfs_share(clone);
564789Sahrens 			zfs_close(clone);
565789Sahrens 		}
566789Sahrens 	}
567789Sahrens 
568789Sahrens 	zfs_close(zhp);
5697265Sahrens 	nvlist_free(props);
5707265Sahrens 
5717265Sahrens 	return (!!ret);
5727265Sahrens 
5737265Sahrens usage:
5747265Sahrens 	if (zhp)
5757265Sahrens 		zfs_close(zhp);
5767265Sahrens 	nvlist_free(props);
5777265Sahrens 	usage(B_FALSE);
5787265Sahrens 	return (-1);
579789Sahrens }
580789Sahrens 
581789Sahrens /*
5824490Svb160487  * zfs create [-p] [-o prop=value] ... fs
5834490Svb160487  * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
584789Sahrens  *
585789Sahrens  * Create a new dataset.  This command can be used to create filesystems
586789Sahrens  * and volumes.  Snapshot creation is handled by 'zfs snapshot'.
587789Sahrens  * For volumes, the user must specify a size to be used.
588789Sahrens  *
589789Sahrens  * The '-s' flag applies only to volumes, and indicates that we should not try
590789Sahrens  * to set the reservation for this volume.  By default we set a reservation
5915481Sck153898  * equal to the size for any volume.  For pools with SPA_VERSION >=
5925481Sck153898  * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
5934490Svb160487  *
5944490Svb160487  * The '-p' flag creates all the non-existing ancestors of the target first.
595789Sahrens  */
596789Sahrens static int
597789Sahrens zfs_do_create(int argc, char **argv)
598789Sahrens {
599789Sahrens 	zfs_type_t type = ZFS_TYPE_FILESYSTEM;
6002676Seschrock 	zfs_handle_t *zhp = NULL;
6012676Seschrock 	uint64_t volsize;
602789Sahrens 	int c;
6032082Seschrock 	boolean_t noreserve = B_FALSE;
6045367Sahrens 	boolean_t bflag = B_FALSE;
6054490Svb160487 	boolean_t parents = B_FALSE;
6062676Seschrock 	int ret = 1;
6077265Sahrens 	nvlist_t *props;
6082676Seschrock 	uint64_t intval;
6096168Shs24103 	int canmount;
6102676Seschrock 
6112676Seschrock 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
6122676Seschrock 		(void) fprintf(stderr, gettext("internal error: "
6132676Seschrock 		    "out of memory\n"));
6142676Seschrock 		return (1);
6152676Seschrock 	}
616789Sahrens 
617789Sahrens 	/* check options */
6184490Svb160487 	while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
619789Sahrens 		switch (c) {
620789Sahrens 		case 'V':
621789Sahrens 			type = ZFS_TYPE_VOLUME;
6222676Seschrock 			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
6232676Seschrock 				(void) fprintf(stderr, gettext("bad volume "
6242676Seschrock 				    "size '%s': %s\n"), optarg,
6252676Seschrock 				    libzfs_error_description(g_zfs));
6262676Seschrock 				goto error;
6272676Seschrock 			}
6282676Seschrock 
6292676Seschrock 			if (nvlist_add_uint64(props,
6302676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
6312676Seschrock 			    intval) != 0) {
6322676Seschrock 				(void) fprintf(stderr, gettext("internal "
6332676Seschrock 				    "error: out of memory\n"));
6342676Seschrock 				goto error;
6352676Seschrock 			}
6362676Seschrock 			volsize = intval;
637789Sahrens 			break;
6384490Svb160487 		case 'p':
6394490Svb160487 			parents = B_TRUE;
6404490Svb160487 			break;
641789Sahrens 		case 'b':
6425367Sahrens 			bflag = B_TRUE;
6432676Seschrock 			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
6442676Seschrock 				(void) fprintf(stderr, gettext("bad volume "
6452676Seschrock 				    "block size '%s': %s\n"), optarg,
6462676Seschrock 				    libzfs_error_description(g_zfs));
6472676Seschrock 				goto error;
6482676Seschrock 			}
6492676Seschrock 
6502676Seschrock 			if (nvlist_add_uint64(props,
6512676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
6522676Seschrock 			    intval) != 0) {
6532676Seschrock 				(void) fprintf(stderr, gettext("internal "
6542676Seschrock 				    "error: out of memory\n"));
6552676Seschrock 				goto error;
6562676Seschrock 			}
6572676Seschrock 			break;
6582676Seschrock 		case 'o':
6597265Sahrens 			if (parseprop(props))
6602676Seschrock 				goto error;
661789Sahrens 			break;
662789Sahrens 		case 's':
6632082Seschrock 			noreserve = B_TRUE;
664789Sahrens 			break;
665789Sahrens 		case ':':
666789Sahrens 			(void) fprintf(stderr, gettext("missing size "
667789Sahrens 			    "argument\n"));
6682676Seschrock 			goto badusage;
669789Sahrens 			break;
670789Sahrens 		case '?':
671789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
672789Sahrens 			    optopt);
6732676Seschrock 			goto badusage;
674789Sahrens 		}
675789Sahrens 	}
676789Sahrens 
6775367Sahrens 	if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
6785367Sahrens 		(void) fprintf(stderr, gettext("'-s' and '-b' can only be "
6795367Sahrens 		    "used when creating a volume\n"));
6802676Seschrock 		goto badusage;
681789Sahrens 	}
682789Sahrens 
683789Sahrens 	argc -= optind;
684789Sahrens 	argv += optind;
685789Sahrens 
686789Sahrens 	/* check number of arguments */
687789Sahrens 	if (argc == 0) {
688789Sahrens 		(void) fprintf(stderr, gettext("missing %s argument\n"),
689789Sahrens 		    zfs_type_to_name(type));
6902676Seschrock 		goto badusage;
691789Sahrens 	}
692789Sahrens 	if (argc > 1) {
693789Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
6942676Seschrock 		goto badusage;
6952676Seschrock 	}
6962676Seschrock 
6975481Sck153898 	if (type == ZFS_TYPE_VOLUME && !noreserve) {
6985481Sck153898 		zpool_handle_t *zpool_handle;
6995481Sck153898 		uint64_t spa_version;
7005481Sck153898 		char *p;
7015481Sck153898 		zfs_prop_t resv_prop;
7027265Sahrens 		char *strval;
7035481Sck153898 
7045481Sck153898 		if (p = strchr(argv[0], '/'))
7055481Sck153898 			*p = '\0';
7065481Sck153898 		zpool_handle = zpool_open(g_zfs, argv[0]);
7075481Sck153898 		if (p != NULL)
7085481Sck153898 			*p = '/';
7095481Sck153898 		if (zpool_handle == NULL)
7105481Sck153898 			goto error;
7115481Sck153898 		spa_version = zpool_get_prop_int(zpool_handle,
7125481Sck153898 		    ZPOOL_PROP_VERSION, NULL);
7135481Sck153898 		zpool_close(zpool_handle);
7145481Sck153898 		if (spa_version >= SPA_VERSION_REFRESERVATION)
7155481Sck153898 			resv_prop = ZFS_PROP_REFRESERVATION;
7165481Sck153898 		else
7175481Sck153898 			resv_prop = ZFS_PROP_RESERVATION;
71811449SEric.Taylor@Sun.COM 		volsize = zvol_volsize_to_reservation(volsize, props);
7195481Sck153898 
7205481Sck153898 		if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
7215481Sck153898 		    &strval) != 0) {
7225481Sck153898 			if (nvlist_add_uint64(props,
7235481Sck153898 			    zfs_prop_to_name(resv_prop), volsize) != 0) {
7245481Sck153898 				(void) fprintf(stderr, gettext("internal "
7255481Sck153898 				    "error: out of memory\n"));
7265481Sck153898 				nvlist_free(props);
7275481Sck153898 				return (1);
7285481Sck153898 			}
7292676Seschrock 		}
730789Sahrens 	}
731789Sahrens 
7324490Svb160487 	if (parents && zfs_name_valid(argv[0], type)) {
7334490Svb160487 		/*
7344490Svb160487 		 * Now create the ancestors of target dataset.  If the target
7354490Svb160487 		 * already exists and '-p' option was used we should not
7364490Svb160487 		 * complain.
7374490Svb160487 		 */
7384490Svb160487 		if (zfs_dataset_exists(g_zfs, argv[0], type)) {
7394490Svb160487 			ret = 0;
7404490Svb160487 			goto error;
7414490Svb160487 		}
7424490Svb160487 		if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
7434490Svb160487 			goto error;
7444490Svb160487 	}
7454490Svb160487 
746789Sahrens 	/* pass to libzfs */
7472676Seschrock 	if (zfs_create(g_zfs, argv[0], type, props) != 0)
7482676Seschrock 		goto error;
749789Sahrens 
7505094Slling 	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
7512676Seschrock 		goto error;
7526168Shs24103 	/*
7536168Shs24103 	 * if the user doesn't want the dataset automatically mounted,
7546168Shs24103 	 * then skip the mount/share step
7556168Shs24103 	 */
7566168Shs24103 
7576168Shs24103 	canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
758789Sahrens 
759789Sahrens 	/*
760789Sahrens 	 * Mount and/or share the new filesystem as appropriate.  We provide a
761789Sahrens 	 * verbose error message to let the user know that their filesystem was
762789Sahrens 	 * in fact created, even if we failed to mount or share it.
763789Sahrens 	 */
7646168Shs24103 	ret = 0;
7656168Shs24103 	if (canmount == ZFS_CANMOUNT_ON) {
7666168Shs24103 		if (zfs_mount(zhp, NULL, 0) != 0) {
7676168Shs24103 			(void) fprintf(stderr, gettext("filesystem "
7686168Shs24103 			    "successfully created, but not mounted\n"));
7696168Shs24103 			ret = 1;
7706168Shs24103 		} else if (zfs_share(zhp) != 0) {
7716168Shs24103 			(void) fprintf(stderr, gettext("filesystem "
7726168Shs24103 			    "successfully created, but not shared\n"));
7736168Shs24103 			ret = 1;
7746168Shs24103 		}
775789Sahrens 	}
776789Sahrens 
7772676Seschrock error:
7782676Seschrock 	if (zhp)
7792676Seschrock 		zfs_close(zhp);
7802676Seschrock 	nvlist_free(props);
781789Sahrens 	return (ret);
7822676Seschrock badusage:
7832676Seschrock 	nvlist_free(props);
7842676Seschrock 	usage(B_FALSE);
7852676Seschrock 	return (2);
786789Sahrens }
787789Sahrens 
788789Sahrens /*
78910385Schris.kirby@sun.com  * zfs destroy [-rRf] <fs, vol>
79010385Schris.kirby@sun.com  * zfs destroy [-rRd] <snap>
791789Sahrens  *
79211022STom.Erickson@Sun.COM  *	-r	Recursively destroy all children
79311022STom.Erickson@Sun.COM  *	-R	Recursively destroy all dependents, including clones
79411022STom.Erickson@Sun.COM  *	-f	Force unmounting of any dependents
79510242Schris.kirby@sun.com  *	-d	If we can't destroy now, mark for deferred destruction
796789Sahrens  *
797789Sahrens  * Destroys the given dataset.  By default, it will unmount any filesystems,
798789Sahrens  * and refuse to destroy a dataset that has any dependents.  A dependent can
799789Sahrens  * either be a child, or a clone of a child.
800789Sahrens  */
801789Sahrens typedef struct destroy_cbdata {
8022082Seschrock 	boolean_t	cb_first;
803789Sahrens 	int		cb_force;
804789Sahrens 	int		cb_recurse;
805789Sahrens 	int		cb_error;
806789Sahrens 	int		cb_needforce;
807789Sahrens 	int		cb_doclones;
8083265Sahrens 	boolean_t	cb_closezhp;
809789Sahrens 	zfs_handle_t	*cb_target;
8102199Sahrens 	char		*cb_snapname;
81110242Schris.kirby@sun.com 	boolean_t	cb_defer_destroy;
812789Sahrens } destroy_cbdata_t;
813789Sahrens 
814789Sahrens /*
815789Sahrens  * Check for any dependents based on the '-r' or '-R' flags.
816789Sahrens  */
817789Sahrens static int
818789Sahrens destroy_check_dependent(zfs_handle_t *zhp, void *data)
819789Sahrens {
820789Sahrens 	destroy_cbdata_t *cbp = data;
821789Sahrens 	const char *tname = zfs_get_name(cbp->cb_target);
822789Sahrens 	const char *name = zfs_get_name(zhp);
823789Sahrens 
824789Sahrens 	if (strncmp(tname, name, strlen(tname)) == 0 &&
825789Sahrens 	    (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
826789Sahrens 		/*
827789Sahrens 		 * This is a direct descendant, not a clone somewhere else in
828789Sahrens 		 * the hierarchy.
829789Sahrens 		 */
830789Sahrens 		if (cbp->cb_recurse)
831789Sahrens 			goto out;
832789Sahrens 
833789Sahrens 		if (cbp->cb_first) {
834789Sahrens 			(void) fprintf(stderr, gettext("cannot destroy '%s': "
835789Sahrens 			    "%s has children\n"),
836789Sahrens 			    zfs_get_name(cbp->cb_target),
837789Sahrens 			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
838789Sahrens 			(void) fprintf(stderr, gettext("use '-r' to destroy "
839789Sahrens 			    "the following datasets:\n"));
8402082Seschrock 			cbp->cb_first = B_FALSE;
841789Sahrens 			cbp->cb_error = 1;
842789Sahrens 		}
843789Sahrens 
844789Sahrens 		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
845789Sahrens 	} else {
846789Sahrens 		/*
847789Sahrens 		 * This is a clone.  We only want to report this if the '-r'
848789Sahrens 		 * wasn't specified, or the target is a snapshot.
849789Sahrens 		 */
850789Sahrens 		if (!cbp->cb_recurse &&
851789Sahrens 		    zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
852789Sahrens 			goto out;
853789Sahrens 
854789Sahrens 		if (cbp->cb_first) {
855789Sahrens 			(void) fprintf(stderr, gettext("cannot destroy '%s': "
856789Sahrens 			    "%s has dependent clones\n"),
857789Sahrens 			    zfs_get_name(cbp->cb_target),
858789Sahrens 			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
859789Sahrens 			(void) fprintf(stderr, gettext("use '-R' to destroy "
860789Sahrens 			    "the following datasets:\n"));
8612082Seschrock 			cbp->cb_first = B_FALSE;
862789Sahrens 			cbp->cb_error = 1;
863789Sahrens 		}
864789Sahrens 
865789Sahrens 		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
866789Sahrens 	}
867789Sahrens 
868789Sahrens out:
869789Sahrens 	zfs_close(zhp);
870789Sahrens 	return (0);
871789Sahrens }
872789Sahrens 
873789Sahrens static int
874789Sahrens destroy_callback(zfs_handle_t *zhp, void *data)
875789Sahrens {
876789Sahrens 	destroy_cbdata_t *cbp = data;
877789Sahrens 
878789Sahrens 	/*
879789Sahrens 	 * Ignore pools (which we've already flagged as an error before getting
88010588SEric.Taylor@Sun.COM 	 * here).
881789Sahrens 	 */
882789Sahrens 	if (strchr(zfs_get_name(zhp), '/') == NULL &&
883789Sahrens 	    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
884789Sahrens 		zfs_close(zhp);
885789Sahrens 		return (0);
886789Sahrens 	}
887789Sahrens 
888789Sahrens 	/*
889789Sahrens 	 * Bail out on the first error.
890789Sahrens 	 */
891789Sahrens 	if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 ||
89210242Schris.kirby@sun.com 	    zfs_destroy(zhp, cbp->cb_defer_destroy) != 0) {
893789Sahrens 		zfs_close(zhp);
894789Sahrens 		return (-1);
895789Sahrens 	}
896789Sahrens 
897789Sahrens 	zfs_close(zhp);
898789Sahrens 	return (0);
899789Sahrens }
900789Sahrens 
9012199Sahrens static int
9022199Sahrens destroy_snap_clones(zfs_handle_t *zhp, void *arg)
9032199Sahrens {
9042199Sahrens 	destroy_cbdata_t *cbp = arg;
9052199Sahrens 	char thissnap[MAXPATHLEN];
9062199Sahrens 	zfs_handle_t *szhp;
9073265Sahrens 	boolean_t closezhp = cbp->cb_closezhp;
9083265Sahrens 	int rv;
9092199Sahrens 
9102199Sahrens 	(void) snprintf(thissnap, sizeof (thissnap),
9112199Sahrens 	    "%s@%s", zfs_get_name(zhp), cbp->cb_snapname);
9122199Sahrens 
9132199Sahrens 	libzfs_print_on_error(g_zfs, B_FALSE);
9142199Sahrens 	szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT);
9152199Sahrens 	libzfs_print_on_error(g_zfs, B_TRUE);
9162199Sahrens 	if (szhp) {
9172199Sahrens 		/*
9182199Sahrens 		 * Destroy any clones of this snapshot
9192199Sahrens 		 */
9202474Seschrock 		if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback,
9212474Seschrock 		    cbp) != 0) {
9222474Seschrock 			zfs_close(szhp);
9233265Sahrens 			if (closezhp)
9243265Sahrens 				zfs_close(zhp);
9252474Seschrock 			return (-1);
9262474Seschrock 		}
9272199Sahrens 		zfs_close(szhp);
9282199Sahrens 	}
9292199Sahrens 
9303265Sahrens 	cbp->cb_closezhp = B_TRUE;
9313265Sahrens 	rv = zfs_iter_filesystems(zhp, destroy_snap_clones, arg);
9323265Sahrens 	if (closezhp)
9333265Sahrens 		zfs_close(zhp);
9343265Sahrens 	return (rv);
9352199Sahrens }
936789Sahrens 
937789Sahrens static int
938789Sahrens zfs_do_destroy(int argc, char **argv)
939789Sahrens {
940789Sahrens 	destroy_cbdata_t cb = { 0 };
941789Sahrens 	int c;
942789Sahrens 	zfs_handle_t *zhp;
9432199Sahrens 	char *cp;
94410385Schris.kirby@sun.com 	zfs_type_t type = ZFS_TYPE_DATASET;
945789Sahrens 
946789Sahrens 	/* check options */
94710242Schris.kirby@sun.com 	while ((c = getopt(argc, argv, "dfrR")) != -1) {
948789Sahrens 		switch (c) {
94910242Schris.kirby@sun.com 		case 'd':
95010242Schris.kirby@sun.com 			cb.cb_defer_destroy = B_TRUE;
95110385Schris.kirby@sun.com 			type = ZFS_TYPE_SNAPSHOT;
95210242Schris.kirby@sun.com 			break;
953789Sahrens 		case 'f':
954789Sahrens 			cb.cb_force = 1;
955789Sahrens 			break;
956789Sahrens 		case 'r':
957789Sahrens 			cb.cb_recurse = 1;
958789Sahrens 			break;
959789Sahrens 		case 'R':
960789Sahrens 			cb.cb_recurse = 1;
961789Sahrens 			cb.cb_doclones = 1;
962789Sahrens 			break;
963789Sahrens 		case '?':
964789Sahrens 		default:
965789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
966789Sahrens 			    optopt);
9672082Seschrock 			usage(B_FALSE);
968789Sahrens 		}
969789Sahrens 	}
970789Sahrens 
971789Sahrens 	argc -= optind;
972789Sahrens 	argv += optind;
973789Sahrens 
974789Sahrens 	/* check number of arguments */
975789Sahrens 	if (argc == 0) {
976789Sahrens 		(void) fprintf(stderr, gettext("missing path argument\n"));
9772082Seschrock 		usage(B_FALSE);
978789Sahrens 	}
979789Sahrens 	if (argc > 1) {
980789Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
9812082Seschrock 		usage(B_FALSE);
982789Sahrens 	}
983789Sahrens 
9842199Sahrens 	/*
9852199Sahrens 	 * If we are doing recursive destroy of a snapshot, then the
9862199Sahrens 	 * named snapshot may not exist.  Go straight to libzfs.
9872199Sahrens 	 */
9882199Sahrens 	if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) {
9892199Sahrens 		int ret;
9902199Sahrens 
9912199Sahrens 		*cp = '\0';
9925094Slling 		if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
9932199Sahrens 			return (1);
9942199Sahrens 		*cp = '@';
9952199Sahrens 		cp++;
9962199Sahrens 
9972199Sahrens 		if (cb.cb_doclones) {
99810385Schris.kirby@sun.com 			boolean_t defer = cb.cb_defer_destroy;
99910385Schris.kirby@sun.com 
100010385Schris.kirby@sun.com 			/*
100110385Schris.kirby@sun.com 			 * Temporarily ignore the defer_destroy setting since
100210385Schris.kirby@sun.com 			 * it's not supported for clones.
100310385Schris.kirby@sun.com 			 */
100410385Schris.kirby@sun.com 			cb.cb_defer_destroy = B_FALSE;
10052199Sahrens 			cb.cb_snapname = cp;
10062474Seschrock 			if (destroy_snap_clones(zhp, &cb) != 0) {
10072474Seschrock 				zfs_close(zhp);
10082474Seschrock 				return (1);
10092474Seschrock 			}
101010385Schris.kirby@sun.com 			cb.cb_defer_destroy = defer;
10112199Sahrens 		}
10122199Sahrens 
101310242Schris.kirby@sun.com 		ret = zfs_destroy_snaps(zhp, cp, cb.cb_defer_destroy);
10142199Sahrens 		zfs_close(zhp);
10152199Sahrens 		if (ret) {
10162199Sahrens 			(void) fprintf(stderr,
10172199Sahrens 			    gettext("no snapshots destroyed\n"));
10182199Sahrens 		}
10192199Sahrens 		return (ret != 0);
10202199Sahrens 	}
10212199Sahrens 
1022789Sahrens 	/* Open the given dataset */
102310385Schris.kirby@sun.com 	if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1024789Sahrens 		return (1);
1025789Sahrens 
1026789Sahrens 	cb.cb_target = zhp;
1027789Sahrens 
1028789Sahrens 	/*
1029789Sahrens 	 * Perform an explicit check for pools before going any further.
1030789Sahrens 	 */
1031789Sahrens 	if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1032789Sahrens 	    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1033789Sahrens 		(void) fprintf(stderr, gettext("cannot destroy '%s': "
1034789Sahrens 		    "operation does not apply to pools\n"),
1035789Sahrens 		    zfs_get_name(zhp));
1036789Sahrens 		(void) fprintf(stderr, gettext("use 'zfs destroy -r "
1037789Sahrens 		    "%s' to destroy all datasets in the pool\n"),
1038789Sahrens 		    zfs_get_name(zhp));
1039789Sahrens 		(void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1040789Sahrens 		    "to destroy the pool itself\n"), zfs_get_name(zhp));
1041789Sahrens 		zfs_close(zhp);
1042789Sahrens 		return (1);
1043789Sahrens 	}
1044789Sahrens 
1045789Sahrens 	/*
1046789Sahrens 	 * Check for any dependents and/or clones.
1047789Sahrens 	 */
10482082Seschrock 	cb.cb_first = B_TRUE;
104910242Schris.kirby@sun.com 	if (!cb.cb_doclones && !cb.cb_defer_destroy &&
10502474Seschrock 	    zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
10512474Seschrock 	    &cb) != 0) {
10522474Seschrock 		zfs_close(zhp);
10532474Seschrock 		return (1);
10542474Seschrock 	}
10552474Seschrock 
105610242Schris.kirby@sun.com 	if (cb.cb_error || (!cb.cb_defer_destroy &&
105710242Schris.kirby@sun.com 	    (zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0))) {
1058789Sahrens 		zfs_close(zhp);
1059789Sahrens 		return (1);
1060789Sahrens 	}
1061789Sahrens 
1062789Sahrens 	/*
10632474Seschrock 	 * Do the real thing.  The callback will close the handle regardless of
10642474Seschrock 	 * whether it succeeds or not.
1065789Sahrens 	 */
10664543Smarks 
10672474Seschrock 	if (destroy_callback(zhp, &cb) != 0)
10682474Seschrock 		return (1);
10692474Seschrock 
10702474Seschrock 	return (0);
1071789Sahrens }
1072789Sahrens 
107311022STom.Erickson@Sun.COM static boolean_t
107411022STom.Erickson@Sun.COM is_recvd_column(zprop_get_cbdata_t *cbp)
107511022STom.Erickson@Sun.COM {
107611022STom.Erickson@Sun.COM 	int i;
107711022STom.Erickson@Sun.COM 	zfs_get_column_t col;
107811022STom.Erickson@Sun.COM 
107911022STom.Erickson@Sun.COM 	for (i = 0; i < ZFS_GET_NCOLS &&
108011022STom.Erickson@Sun.COM 	    (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
108111022STom.Erickson@Sun.COM 		if (col == GET_COL_RECVD)
108211022STom.Erickson@Sun.COM 			return (B_TRUE);
108311022STom.Erickson@Sun.COM 	return (B_FALSE);
108411022STom.Erickson@Sun.COM }
108511022STom.Erickson@Sun.COM 
1086789Sahrens /*
108711022STom.Erickson@Sun.COM  * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
108811022STom.Erickson@Sun.COM  *	< all | property[,property]... > < fs | snap | vol > ...
1089789Sahrens  *
1090789Sahrens  *	-r	recurse over any child datasets
1091789Sahrens  *	-H	scripted mode.  Headers are stripped, and fields are separated
1092789Sahrens  *		by tabs instead of spaces.
109311022STom.Erickson@Sun.COM  *	-o	Set of fields to display.  One of "name,property,value,
109411022STom.Erickson@Sun.COM  *		received,source". Default is "name,property,value,source".
109511022STom.Erickson@Sun.COM  *		"all" is an alias for all five.
1096789Sahrens  *	-s	Set of sources to allow.  One of
109711022STom.Erickson@Sun.COM  *		"local,default,inherited,received,temporary,none".  Default is
109811022STom.Erickson@Sun.COM  *		all six.
1099789Sahrens  *	-p	Display values in parsable (literal) format.
1100789Sahrens  *
1101789Sahrens  *  Prints properties for the given datasets.  The user can control which
1102789Sahrens  *  columns to display as well as which property types to allow.
1103789Sahrens  */
1104789Sahrens 
1105789Sahrens /*
1106789Sahrens  * Invoked to display the properties for a single dataset.
1107789Sahrens  */
1108789Sahrens static int
1109789Sahrens get_callback(zfs_handle_t *zhp, void *data)
1110789Sahrens {
1111789Sahrens 	char buf[ZFS_MAXPROPLEN];
111211022STom.Erickson@Sun.COM 	char rbuf[ZFS_MAXPROPLEN];
11135094Slling 	zprop_source_t sourcetype;
1114789Sahrens 	char source[ZFS_MAXNAMELEN];
11155094Slling 	zprop_get_cbdata_t *cbp = data;
111611022STom.Erickson@Sun.COM 	nvlist_t *user_props = zfs_get_user_props(zhp);
11175094Slling 	zprop_list_t *pl = cbp->cb_proplist;
11182676Seschrock 	nvlist_t *propval;
11192676Seschrock 	char *strval;
11202676Seschrock 	char *sourceval;
112111022STom.Erickson@Sun.COM 	boolean_t received = is_recvd_column(cbp);
11222676Seschrock 
11232676Seschrock 	for (; pl != NULL; pl = pl->pl_next) {
112411022STom.Erickson@Sun.COM 		char *recvdval = NULL;
11252676Seschrock 		/*
11262676Seschrock 		 * Skip the special fake placeholder.  This will also skip over
11272676Seschrock 		 * the name property when 'all' is specified.
11282676Seschrock 		 */
11292676Seschrock 		if (pl->pl_prop == ZFS_PROP_NAME &&
11302676Seschrock 		    pl == cbp->cb_proplist)
11312676Seschrock 			continue;
11322676Seschrock 
11335094Slling 		if (pl->pl_prop != ZPROP_INVAL) {
11342676Seschrock 			if (zfs_prop_get(zhp, pl->pl_prop, buf,
11352676Seschrock 			    sizeof (buf), &sourcetype, source,
11362676Seschrock 			    sizeof (source),
11372676Seschrock 			    cbp->cb_literal) != 0) {
11382676Seschrock 				if (pl->pl_all)
11392676Seschrock 					continue;
11403912Slling 				if (!zfs_prop_valid_for_type(pl->pl_prop,
11415094Slling 				    ZFS_TYPE_DATASET)) {
11423912Slling 					(void) fprintf(stderr,
11433912Slling 					    gettext("No such property '%s'\n"),
11443912Slling 					    zfs_prop_to_name(pl->pl_prop));
11453912Slling 					continue;
11463912Slling 				}
11475094Slling 				sourcetype = ZPROP_SRC_NONE;
11482676Seschrock 				(void) strlcpy(buf, "-", sizeof (buf));
11492676Seschrock 			}
11502676Seschrock 
115111022STom.Erickson@Sun.COM 			if (received && (zfs_prop_get_recvd(zhp,
115211022STom.Erickson@Sun.COM 			    zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
115311022STom.Erickson@Sun.COM 			    cbp->cb_literal) == 0))
115411022STom.Erickson@Sun.COM 				recvdval = rbuf;
115511022STom.Erickson@Sun.COM 
11565094Slling 			zprop_print_one_property(zfs_get_name(zhp), cbp,
11572676Seschrock 			    zfs_prop_to_name(pl->pl_prop),
115811022STom.Erickson@Sun.COM 			    buf, sourcetype, source, recvdval);
11599396SMatthew.Ahrens@Sun.COM 		} else if (zfs_prop_userquota(pl->pl_user_prop)) {
11609396SMatthew.Ahrens@Sun.COM 			sourcetype = ZPROP_SRC_LOCAL;
11619396SMatthew.Ahrens@Sun.COM 
11629396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
11639396SMatthew.Ahrens@Sun.COM 			    buf, sizeof (buf), cbp->cb_literal) != 0) {
11649396SMatthew.Ahrens@Sun.COM 				sourcetype = ZPROP_SRC_NONE;
11659396SMatthew.Ahrens@Sun.COM 				(void) strlcpy(buf, "-", sizeof (buf));
11669396SMatthew.Ahrens@Sun.COM 			}
11679396SMatthew.Ahrens@Sun.COM 
11689396SMatthew.Ahrens@Sun.COM 			zprop_print_one_property(zfs_get_name(zhp), cbp,
116911022STom.Erickson@Sun.COM 			    pl->pl_user_prop, buf, sourcetype, source, NULL);
11702676Seschrock 		} else {
117111022STom.Erickson@Sun.COM 			if (nvlist_lookup_nvlist(user_props,
11722676Seschrock 			    pl->pl_user_prop, &propval) != 0) {
11732676Seschrock 				if (pl->pl_all)
11742676Seschrock 					continue;
11755094Slling 				sourcetype = ZPROP_SRC_NONE;
11762676Seschrock 				strval = "-";
11772676Seschrock 			} else {
11782676Seschrock 				verify(nvlist_lookup_string(propval,
11795094Slling 				    ZPROP_VALUE, &strval) == 0);
11802676Seschrock 				verify(nvlist_lookup_string(propval,
11815094Slling 				    ZPROP_SOURCE, &sourceval) == 0);
11822676Seschrock 
11832676Seschrock 				if (strcmp(sourceval,
11842676Seschrock 				    zfs_get_name(zhp)) == 0) {
11855094Slling 					sourcetype = ZPROP_SRC_LOCAL;
118611022STom.Erickson@Sun.COM 				} else if (strcmp(sourceval,
118711022STom.Erickson@Sun.COM 				    ZPROP_SOURCE_VAL_RECVD) == 0) {
118811022STom.Erickson@Sun.COM 					sourcetype = ZPROP_SRC_RECEIVED;
11892676Seschrock 				} else {
11905094Slling 					sourcetype = ZPROP_SRC_INHERITED;
11912676Seschrock 					(void) strlcpy(source,
11922676Seschrock 					    sourceval, sizeof (source));
11932676Seschrock 				}
11942676Seschrock 			}
11952676Seschrock 
119611022STom.Erickson@Sun.COM 			if (received && (zfs_prop_get_recvd(zhp,
119711022STom.Erickson@Sun.COM 			    pl->pl_user_prop, rbuf, sizeof (rbuf),
119811022STom.Erickson@Sun.COM 			    cbp->cb_literal) == 0))
119911022STom.Erickson@Sun.COM 				recvdval = rbuf;
120011022STom.Erickson@Sun.COM 
12015094Slling 			zprop_print_one_property(zfs_get_name(zhp), cbp,
12022676Seschrock 			    pl->pl_user_prop, strval, sourcetype,
120311022STom.Erickson@Sun.COM 			    source, recvdval);
1204866Seschrock 		}
1205789Sahrens 	}
1206789Sahrens 
1207789Sahrens 	return (0);
1208789Sahrens }
1209789Sahrens 
1210789Sahrens static int
1211789Sahrens zfs_do_get(int argc, char **argv)
1212789Sahrens {
12135094Slling 	zprop_get_cbdata_t cb = { 0 };
12147538SRichard.Morris@Sun.COM 	int i, c, flags = 0;
12152676Seschrock 	char *value, *fields;
1216866Seschrock 	int ret;
12179365SChris.Gerhard@sun.com 	int limit = 0;
12185094Slling 	zprop_list_t fake_name = { 0 };
1219789Sahrens 
1220789Sahrens 	/*
1221789Sahrens 	 * Set up default columns and sources.
1222789Sahrens 	 */
12235094Slling 	cb.cb_sources = ZPROP_SRC_ALL;
1224789Sahrens 	cb.cb_columns[0] = GET_COL_NAME;
1225789Sahrens 	cb.cb_columns[1] = GET_COL_PROPERTY;
1226789Sahrens 	cb.cb_columns[2] = GET_COL_VALUE;
1227789Sahrens 	cb.cb_columns[3] = GET_COL_SOURCE;
12285094Slling 	cb.cb_type = ZFS_TYPE_DATASET;
1229789Sahrens 
1230789Sahrens 	/* check options */
12319365SChris.Gerhard@sun.com 	while ((c = getopt(argc, argv, ":d:o:s:rHp")) != -1) {
1232789Sahrens 		switch (c) {
1233789Sahrens 		case 'p':
12342082Seschrock 			cb.cb_literal = B_TRUE;
1235789Sahrens 			break;
12369365SChris.Gerhard@sun.com 		case 'd':
12379365SChris.Gerhard@sun.com 			limit = parse_depth(optarg, &flags);
12389365SChris.Gerhard@sun.com 			break;
1239789Sahrens 		case 'r':
12407538SRichard.Morris@Sun.COM 			flags |= ZFS_ITER_RECURSE;
1241789Sahrens 			break;
1242789Sahrens 		case 'H':
12432082Seschrock 			cb.cb_scripted = B_TRUE;
1244789Sahrens 			break;
1245789Sahrens 		case ':':
1246789Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
1247789Sahrens 			    "'%c' option\n"), optopt);
12482082Seschrock 			usage(B_FALSE);
1249789Sahrens 			break;
1250789Sahrens 		case 'o':
1251789Sahrens 			/*
1252789Sahrens 			 * Process the set of columns to display.  We zero out
1253789Sahrens 			 * the structure to give us a blank slate.
1254789Sahrens 			 */
1255789Sahrens 			bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1256789Sahrens 			i = 0;
1257789Sahrens 			while (*optarg != '\0') {
1258789Sahrens 				static char *col_subopts[] =
125911022STom.Erickson@Sun.COM 				    { "name", "property", "value", "received",
126011022STom.Erickson@Sun.COM 				    "source", "all", NULL };
126111022STom.Erickson@Sun.COM 
126211022STom.Erickson@Sun.COM 				if (i == ZFS_GET_NCOLS) {
1263789Sahrens 					(void) fprintf(stderr, gettext("too "
1264789Sahrens 					    "many fields given to -o "
1265789Sahrens 					    "option\n"));
12662082Seschrock 					usage(B_FALSE);
1267789Sahrens 				}
1268789Sahrens 
1269789Sahrens 				switch (getsubopt(&optarg, col_subopts,
1270789Sahrens 				    &value)) {
1271789Sahrens 				case 0:
1272789Sahrens 					cb.cb_columns[i++] = GET_COL_NAME;
1273789Sahrens 					break;
1274789Sahrens 				case 1:
1275789Sahrens 					cb.cb_columns[i++] = GET_COL_PROPERTY;
1276789Sahrens 					break;
1277789Sahrens 				case 2:
1278789Sahrens 					cb.cb_columns[i++] = GET_COL_VALUE;
1279789Sahrens 					break;
1280789Sahrens 				case 3:
128111022STom.Erickson@Sun.COM 					cb.cb_columns[i++] = GET_COL_RECVD;
128211022STom.Erickson@Sun.COM 					flags |= ZFS_ITER_RECVD_PROPS;
128311022STom.Erickson@Sun.COM 					break;
128411022STom.Erickson@Sun.COM 				case 4:
1285789Sahrens 					cb.cb_columns[i++] = GET_COL_SOURCE;
1286789Sahrens 					break;
128711022STom.Erickson@Sun.COM 				case 5:
128811022STom.Erickson@Sun.COM 					if (i > 0) {
128911022STom.Erickson@Sun.COM 						(void) fprintf(stderr,
129011022STom.Erickson@Sun.COM 						    gettext("\"all\" conflicts "
129111022STom.Erickson@Sun.COM 						    "with specific fields "
129211022STom.Erickson@Sun.COM 						    "given to -o option\n"));
129311022STom.Erickson@Sun.COM 						usage(B_FALSE);
129411022STom.Erickson@Sun.COM 					}
129511022STom.Erickson@Sun.COM 					cb.cb_columns[0] = GET_COL_NAME;
129611022STom.Erickson@Sun.COM 					cb.cb_columns[1] = GET_COL_PROPERTY;
129711022STom.Erickson@Sun.COM 					cb.cb_columns[2] = GET_COL_VALUE;
129811022STom.Erickson@Sun.COM 					cb.cb_columns[3] = GET_COL_RECVD;
129911022STom.Erickson@Sun.COM 					cb.cb_columns[4] = GET_COL_SOURCE;
130011022STom.Erickson@Sun.COM 					flags |= ZFS_ITER_RECVD_PROPS;
130111022STom.Erickson@Sun.COM 					i = ZFS_GET_NCOLS;
130211022STom.Erickson@Sun.COM 					break;
1303789Sahrens 				default:
1304789Sahrens 					(void) fprintf(stderr,
1305789Sahrens 					    gettext("invalid column name "
1306789Sahrens 					    "'%s'\n"), value);
13073912Slling 					usage(B_FALSE);
1308789Sahrens 				}
1309789Sahrens 			}
1310789Sahrens 			break;
1311789Sahrens 
1312789Sahrens 		case 's':
1313789Sahrens 			cb.cb_sources = 0;
1314789Sahrens 			while (*optarg != '\0') {
1315789Sahrens 				static char *source_subopts[] = {
1316789Sahrens 					"local", "default", "inherited",
131711022STom.Erickson@Sun.COM 					"received", "temporary", "none",
131811022STom.Erickson@Sun.COM 					NULL };
1319789Sahrens 
1320789Sahrens 				switch (getsubopt(&optarg, source_subopts,
1321789Sahrens 				    &value)) {
1322789Sahrens 				case 0:
13235094Slling 					cb.cb_sources |= ZPROP_SRC_LOCAL;
1324789Sahrens 					break;
1325789Sahrens 				case 1:
13265094Slling 					cb.cb_sources |= ZPROP_SRC_DEFAULT;
1327789Sahrens 					break;
1328789Sahrens 				case 2:
13295094Slling 					cb.cb_sources |= ZPROP_SRC_INHERITED;
1330789Sahrens 					break;
1331789Sahrens 				case 3:
133211022STom.Erickson@Sun.COM 					cb.cb_sources |= ZPROP_SRC_RECEIVED;
133311022STom.Erickson@Sun.COM 					break;
133411022STom.Erickson@Sun.COM 				case 4:
13355094Slling 					cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1336789Sahrens 					break;
133711022STom.Erickson@Sun.COM 				case 5:
13385094Slling 					cb.cb_sources |= ZPROP_SRC_NONE;
1339789Sahrens 					break;
1340789Sahrens 				default:
1341789Sahrens 					(void) fprintf(stderr,
1342789Sahrens 					    gettext("invalid source "
1343789Sahrens 					    "'%s'\n"), value);
13443912Slling 					usage(B_FALSE);
1345789Sahrens 				}
1346789Sahrens 			}
1347789Sahrens 			break;
1348789Sahrens 
1349789Sahrens 		case '?':
1350789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1351789Sahrens 			    optopt);
13522082Seschrock 			usage(B_FALSE);
1353789Sahrens 		}
1354789Sahrens 	}
1355789Sahrens 
1356789Sahrens 	argc -= optind;
1357789Sahrens 	argv += optind;
1358789Sahrens 
1359789Sahrens 	if (argc < 1) {
1360789Sahrens 		(void) fprintf(stderr, gettext("missing property "
1361789Sahrens 		    "argument\n"));
13622082Seschrock 		usage(B_FALSE);
1363789Sahrens 	}
1364789Sahrens 
1365789Sahrens 	fields = argv[0];
1366789Sahrens 
13675094Slling 	if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
13685094Slling 	    != 0)
13692082Seschrock 		usage(B_FALSE);
1370789Sahrens 
1371789Sahrens 	argc--;
1372789Sahrens 	argv++;
1373789Sahrens 
1374789Sahrens 	/*
13752676Seschrock 	 * As part of zfs_expand_proplist(), we keep track of the maximum column
13762676Seschrock 	 * width for each property.  For the 'NAME' (and 'SOURCE') columns, we
13772676Seschrock 	 * need to know the maximum name length.  However, the user likely did
13782676Seschrock 	 * not specify 'name' as one of the properties to fetch, so we need to
13792676Seschrock 	 * make sure we always include at least this property for
13802676Seschrock 	 * print_get_headers() to work properly.
1381789Sahrens 	 */
13822676Seschrock 	if (cb.cb_proplist != NULL) {
13832676Seschrock 		fake_name.pl_prop = ZFS_PROP_NAME;
13842676Seschrock 		fake_name.pl_width = strlen(gettext("NAME"));
13852676Seschrock 		fake_name.pl_next = cb.cb_proplist;
13862676Seschrock 		cb.cb_proplist = &fake_name;
1387789Sahrens 	}
1388789Sahrens 
13892676Seschrock 	cb.cb_first = B_TRUE;
13902676Seschrock 
1391789Sahrens 	/* run for each object */
13927538SRichard.Morris@Sun.COM 	ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, NULL,
13939365SChris.Gerhard@sun.com 	    &cb.cb_proplist, limit, get_callback, &cb);
13942676Seschrock 
13952676Seschrock 	if (cb.cb_proplist == &fake_name)
13965094Slling 		zprop_free_list(fake_name.pl_next);
13972676Seschrock 	else
13985094Slling 		zprop_free_list(cb.cb_proplist);
13992676Seschrock 
14002676Seschrock 	return (ret);
1401789Sahrens }
1402789Sahrens 
1403789Sahrens /*
140411022STom.Erickson@Sun.COM  * inherit [-rS] <property> <fs|vol> ...
1405789Sahrens  *
140611022STom.Erickson@Sun.COM  *	-r	Recurse over all children
140711022STom.Erickson@Sun.COM  *	-S	Revert to received value, if any
1408789Sahrens  *
1409789Sahrens  * For each dataset specified on the command line, inherit the given property
1410789Sahrens  * from its parent.  Inheriting a property at the pool level will cause it to
1411789Sahrens  * use the default value.  The '-r' flag will recurse over all children, and is
1412789Sahrens  * useful for setting a property on a hierarchy-wide basis, regardless of any
1413789Sahrens  * local modifications for each dataset.
1414789Sahrens  */
14152926Sek110237 
141611022STom.Erickson@Sun.COM typedef struct inherit_cbdata {
141711022STom.Erickson@Sun.COM 	const char *cb_propname;
141811022STom.Erickson@Sun.COM 	boolean_t cb_received;
141911022STom.Erickson@Sun.COM } inherit_cbdata_t;
142011022STom.Erickson@Sun.COM 
1421789Sahrens static int
14227265Sahrens inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1423789Sahrens {
142411022STom.Erickson@Sun.COM 	inherit_cbdata_t *cb = data;
142511022STom.Erickson@Sun.COM 	zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
14267265Sahrens 
14277265Sahrens 	/*
14287265Sahrens 	 * If we're doing it recursively, then ignore properties that
14297265Sahrens 	 * are not valid for this type of dataset.
14307265Sahrens 	 */
14317265Sahrens 	if (prop != ZPROP_INVAL &&
14327265Sahrens 	    !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
14337265Sahrens 		return (0);
14347265Sahrens 
143511022STom.Erickson@Sun.COM 	return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
14367265Sahrens }
14377265Sahrens 
14387265Sahrens static int
14397265Sahrens inherit_cb(zfs_handle_t *zhp, void *data)
14407265Sahrens {
144111022STom.Erickson@Sun.COM 	inherit_cbdata_t *cb = data;
144211022STom.Erickson@Sun.COM 
144311022STom.Erickson@Sun.COM 	return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1444789Sahrens }
1445789Sahrens 
1446789Sahrens static int
1447789Sahrens zfs_do_inherit(int argc, char **argv)
1448789Sahrens {
1449789Sahrens 	int c;
1450789Sahrens 	zfs_prop_t prop;
145111022STom.Erickson@Sun.COM 	inherit_cbdata_t cb = { 0 };
14524543Smarks 	char *propname;
14532926Sek110237 	int ret;
14547538SRichard.Morris@Sun.COM 	int flags = 0;
145511022STom.Erickson@Sun.COM 	boolean_t received = B_FALSE;
1456789Sahrens 
1457789Sahrens 	/* check options */
145811022STom.Erickson@Sun.COM 	while ((c = getopt(argc, argv, "rS")) != -1) {
1459789Sahrens 		switch (c) {
1460789Sahrens 		case 'r':
14617538SRichard.Morris@Sun.COM 			flags |= ZFS_ITER_RECURSE;
1462789Sahrens 			break;
146311022STom.Erickson@Sun.COM 		case 'S':
146411022STom.Erickson@Sun.COM 			received = B_TRUE;
146511022STom.Erickson@Sun.COM 			break;
1466789Sahrens 		case '?':
1467789Sahrens 		default:
1468789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1469789Sahrens 			    optopt);
14702082Seschrock 			usage(B_FALSE);
1471789Sahrens 		}
1472789Sahrens 	}
1473789Sahrens 
1474789Sahrens 	argc -= optind;
1475789Sahrens 	argv += optind;
1476789Sahrens 
1477789Sahrens 	/* check number of arguments */
1478789Sahrens 	if (argc < 1) {
1479789Sahrens 		(void) fprintf(stderr, gettext("missing property argument\n"));
14802082Seschrock 		usage(B_FALSE);
1481789Sahrens 	}
1482789Sahrens 	if (argc < 2) {
1483789Sahrens 		(void) fprintf(stderr, gettext("missing dataset argument\n"));
14842082Seschrock 		usage(B_FALSE);
1485789Sahrens 	}
1486789Sahrens 
14874543Smarks 	propname = argv[0];
14882676Seschrock 	argc--;
14892676Seschrock 	argv++;
14902676Seschrock 
14915094Slling 	if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
14922676Seschrock 		if (zfs_prop_readonly(prop)) {
14932676Seschrock 			(void) fprintf(stderr, gettext(
14942676Seschrock 			    "%s property is read-only\n"),
14954543Smarks 			    propname);
14962676Seschrock 			return (1);
14972676Seschrock 		}
149811022STom.Erickson@Sun.COM 		if (!zfs_prop_inheritable(prop) && !received) {
14992676Seschrock 			(void) fprintf(stderr, gettext("'%s' property cannot "
15004543Smarks 			    "be inherited\n"), propname);
15012676Seschrock 			if (prop == ZFS_PROP_QUOTA ||
15025378Sck153898 			    prop == ZFS_PROP_RESERVATION ||
15035378Sck153898 			    prop == ZFS_PROP_REFQUOTA ||
15045378Sck153898 			    prop == ZFS_PROP_REFRESERVATION)
15052676Seschrock 				(void) fprintf(stderr, gettext("use 'zfs set "
15064543Smarks 				    "%s=none' to clear\n"), propname);
15072676Seschrock 			return (1);
15082676Seschrock 		}
150911515STom.Erickson@Sun.COM 		if (received && (prop == ZFS_PROP_VOLSIZE ||
151011515STom.Erickson@Sun.COM 		    prop == ZFS_PROP_VERSION)) {
151111515STom.Erickson@Sun.COM 			(void) fprintf(stderr, gettext("'%s' property cannot "
151211515STom.Erickson@Sun.COM 			    "be reverted to a received value\n"), propname);
151311515STom.Erickson@Sun.COM 			return (1);
151411515STom.Erickson@Sun.COM 		}
15154543Smarks 	} else if (!zfs_prop_user(propname)) {
15164543Smarks 		(void) fprintf(stderr, gettext("invalid property '%s'\n"),
15174543Smarks 		    propname);
15182082Seschrock 		usage(B_FALSE);
1519789Sahrens 	}
15202676Seschrock 
152111022STom.Erickson@Sun.COM 	cb.cb_propname = propname;
152211022STom.Erickson@Sun.COM 	cb.cb_received = received;
152311022STom.Erickson@Sun.COM 
15247538SRichard.Morris@Sun.COM 	if (flags & ZFS_ITER_RECURSE) {
15257538SRichard.Morris@Sun.COM 		ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
152611022STom.Erickson@Sun.COM 		    NULL, NULL, 0, inherit_recurse_cb, &cb);
15277265Sahrens 	} else {
15287538SRichard.Morris@Sun.COM 		ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
152911022STom.Erickson@Sun.COM 		    NULL, NULL, 0, inherit_cb, &cb);
15307265Sahrens 	}
15312926Sek110237 
15322926Sek110237 	return (ret);
1533789Sahrens }
1534789Sahrens 
15354577Sahrens typedef struct upgrade_cbdata {
15364577Sahrens 	uint64_t cb_numupgraded;
15374577Sahrens 	uint64_t cb_numsamegraded;
15384849Sahrens 	uint64_t cb_numfailed;
15394577Sahrens 	uint64_t cb_version;
15404577Sahrens 	boolean_t cb_newer;
15414577Sahrens 	boolean_t cb_foundone;
15424577Sahrens 	char cb_lastfs[ZFS_MAXNAMELEN];
15434577Sahrens } upgrade_cbdata_t;
15444577Sahrens 
15454577Sahrens static int
15464577Sahrens same_pool(zfs_handle_t *zhp, const char *name)
15474577Sahrens {
15484577Sahrens 	int len1 = strcspn(name, "/@");
15494577Sahrens 	const char *zhname = zfs_get_name(zhp);
15504577Sahrens 	int len2 = strcspn(zhname, "/@");
15514577Sahrens 
15524577Sahrens 	if (len1 != len2)
15534577Sahrens 		return (B_FALSE);
15544988Sek110237 	return (strncmp(name, zhname, len1) == 0);
15554577Sahrens }
15564577Sahrens 
15574577Sahrens static int
15584577Sahrens upgrade_list_callback(zfs_handle_t *zhp, void *data)
15594577Sahrens {
15604577Sahrens 	upgrade_cbdata_t *cb = data;
15614577Sahrens 	int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
15624577Sahrens 
15634577Sahrens 	/* list if it's old/new */
15644577Sahrens 	if ((!cb->cb_newer && version < ZPL_VERSION) ||
15655367Sahrens 	    (cb->cb_newer && version > ZPL_VERSION)) {
15664577Sahrens 		char *str;
15674577Sahrens 		if (cb->cb_newer) {
15684577Sahrens 			str = gettext("The following filesystems are "
15694577Sahrens 			    "formatted using a newer software version and\n"
15704577Sahrens 			    "cannot be accessed on the current system.\n\n");
15714577Sahrens 		} else {
15724577Sahrens 			str = gettext("The following filesystems are "
15734577Sahrens 			    "out of date, and can be upgraded.  After being\n"
15744577Sahrens 			    "upgraded, these filesystems (and any 'zfs send' "
15754577Sahrens 			    "streams generated from\n"
15764577Sahrens 			    "subsequent snapshots) will no longer be "
15774577Sahrens 			    "accessible by older software versions.\n\n");
15784577Sahrens 		}
15794577Sahrens 
15804577Sahrens 		if (!cb->cb_foundone) {
15814577Sahrens 			(void) puts(str);
15824577Sahrens 			(void) printf(gettext("VER  FILESYSTEM\n"));
15834577Sahrens 			(void) printf(gettext("---  ------------\n"));
15844577Sahrens 			cb->cb_foundone = B_TRUE;
15854577Sahrens 		}
15864577Sahrens 
15874577Sahrens 		(void) printf("%2u   %s\n", version, zfs_get_name(zhp));
15884577Sahrens 	}
15894577Sahrens 
15904577Sahrens 	return (0);
15914577Sahrens }
15924577Sahrens 
15934577Sahrens static int
15944577Sahrens upgrade_set_callback(zfs_handle_t *zhp, void *data)
15954577Sahrens {
15964577Sahrens 	upgrade_cbdata_t *cb = data;
15974577Sahrens 	int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1598*11935SMark.Shellenbaum@Sun.COM 	int needed_spa_version;
1599*11935SMark.Shellenbaum@Sun.COM 	int spa_version;
1600*11935SMark.Shellenbaum@Sun.COM 
1601*11935SMark.Shellenbaum@Sun.COM 	if (zfs_spa_version(zhp, &spa_version) < 0)
1602*11935SMark.Shellenbaum@Sun.COM 		return (-1);
1603*11935SMark.Shellenbaum@Sun.COM 
1604*11935SMark.Shellenbaum@Sun.COM 	needed_spa_version = zfs_spa_version_map(cb->cb_version);
1605*11935SMark.Shellenbaum@Sun.COM 
1606*11935SMark.Shellenbaum@Sun.COM 	if (needed_spa_version < 0)
1607*11935SMark.Shellenbaum@Sun.COM 		return (-1);
1608*11935SMark.Shellenbaum@Sun.COM 
1609*11935SMark.Shellenbaum@Sun.COM 	if (spa_version < needed_spa_version) {
1610*11935SMark.Shellenbaum@Sun.COM 		/* can't upgrade */
1611*11935SMark.Shellenbaum@Sun.COM 		(void) printf(gettext("%s: can not be "
1612*11935SMark.Shellenbaum@Sun.COM 		    "upgraded; the pool version needs to first "
1613*11935SMark.Shellenbaum@Sun.COM 		    "be upgraded\nto version %d\n\n"),
1614*11935SMark.Shellenbaum@Sun.COM 		    zfs_get_name(zhp), needed_spa_version);
1615*11935SMark.Shellenbaum@Sun.COM 		cb->cb_numfailed++;
1616*11935SMark.Shellenbaum@Sun.COM 		return (0);
16175331Samw 	}
16185331Samw 
16194577Sahrens 	/* upgrade */
16204577Sahrens 	if (version < cb->cb_version) {
16214577Sahrens 		char verstr[16];
16224849Sahrens 		(void) snprintf(verstr, sizeof (verstr),
16234849Sahrens 		    "%llu", cb->cb_version);
16244577Sahrens 		if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
16254577Sahrens 			/*
16264577Sahrens 			 * If they did "zfs upgrade -a", then we could
16274577Sahrens 			 * be doing ioctls to different pools.  We need
16284577Sahrens 			 * to log this history once to each pool.
16294577Sahrens 			 */
16304988Sek110237 			verify(zpool_stage_history(g_zfs, history_str) == 0);
16314577Sahrens 		}
16324577Sahrens 		if (zfs_prop_set(zhp, "version", verstr) == 0)
16334577Sahrens 			cb->cb_numupgraded++;
16344849Sahrens 		else
16354849Sahrens 			cb->cb_numfailed++;
16364577Sahrens 		(void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
16374577Sahrens 	} else if (version > cb->cb_version) {
16384577Sahrens 		/* can't downgrade */
16394577Sahrens 		(void) printf(gettext("%s: can not be downgraded; "
16404577Sahrens 		    "it is already at version %u\n"),
16414577Sahrens 		    zfs_get_name(zhp), version);
16424849Sahrens 		cb->cb_numfailed++;
16434577Sahrens 	} else {
16444577Sahrens 		cb->cb_numsamegraded++;
16454577Sahrens 	}
16464577Sahrens 	return (0);
16474577Sahrens }
16484577Sahrens 
16494577Sahrens /*
16504577Sahrens  * zfs upgrade
16514577Sahrens  * zfs upgrade -v
16524577Sahrens  * zfs upgrade [-r] [-V <version>] <-a | filesystem>
16534577Sahrens  */
16544577Sahrens static int
16554577Sahrens zfs_do_upgrade(int argc, char **argv)
16564577Sahrens {
16574577Sahrens 	boolean_t all = B_FALSE;
16584577Sahrens 	boolean_t showversions = B_FALSE;
16594577Sahrens 	int ret;
16604577Sahrens 	upgrade_cbdata_t cb = { 0 };
16614577Sahrens 	char c;
16627538SRichard.Morris@Sun.COM 	int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
16634577Sahrens 
16644577Sahrens 	/* check options */
16654577Sahrens 	while ((c = getopt(argc, argv, "rvV:a")) != -1) {
16664577Sahrens 		switch (c) {
16674577Sahrens 		case 'r':
16687538SRichard.Morris@Sun.COM 			flags |= ZFS_ITER_RECURSE;
16694577Sahrens 			break;
16704577Sahrens 		case 'v':
16714577Sahrens 			showversions = B_TRUE;
16724577Sahrens 			break;
16734577Sahrens 		case 'V':
16744577Sahrens 			if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
16754577Sahrens 			    optarg, &cb.cb_version) != 0) {
16764577Sahrens 				(void) fprintf(stderr,
16774577Sahrens 				    gettext("invalid version %s\n"), optarg);
16784577Sahrens 				usage(B_FALSE);
16794577Sahrens 			}
16804577Sahrens 			break;
16814577Sahrens 		case 'a':
16824577Sahrens 			all = B_TRUE;
16834577Sahrens 			break;
16844577Sahrens 		case '?':
16854577Sahrens 		default:
16864577Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
16874577Sahrens 			    optopt);
16884577Sahrens 			usage(B_FALSE);
16894577Sahrens 		}
16904577Sahrens 	}
16914577Sahrens 
16924577Sahrens 	argc -= optind;
16934577Sahrens 	argv += optind;
16944577Sahrens 
16957538SRichard.Morris@Sun.COM 	if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
16964577Sahrens 		usage(B_FALSE);
16977538SRichard.Morris@Sun.COM 	if (showversions && (flags & ZFS_ITER_RECURSE || all ||
16987538SRichard.Morris@Sun.COM 	    cb.cb_version || argc))
16994577Sahrens 		usage(B_FALSE);
17004577Sahrens 	if ((all || argc) && (showversions))
17014577Sahrens 		usage(B_FALSE);
17024577Sahrens 	if (all && argc)
17034577Sahrens 		usage(B_FALSE);
17044577Sahrens 
17054577Sahrens 	if (showversions) {
17064577Sahrens 		/* Show info on available versions. */
17074577Sahrens 		(void) printf(gettext("The following filesystem versions are "
17084577Sahrens 		    "supported:\n\n"));
17094577Sahrens 		(void) printf(gettext("VER  DESCRIPTION\n"));
17104577Sahrens 		(void) printf("---  -----------------------------------------"
17114577Sahrens 		    "---------------\n");
17124577Sahrens 		(void) printf(gettext(" 1   Initial ZFS filesystem version\n"));
17134577Sahrens 		(void) printf(gettext(" 2   Enhanced directory entries\n"));
17145331Samw 		(void) printf(gettext(" 3   Case insensitive and File system "
171510228SStephanie.Scheffler@Sun.COM 		    "unique identifier (FUID)\n"));
17169396SMatthew.Ahrens@Sun.COM 		(void) printf(gettext(" 4   userquota, groupquota "
17179396SMatthew.Ahrens@Sun.COM 		    "properties\n"));
1718*11935SMark.Shellenbaum@Sun.COM 		(void) printf(gettext(" 5   System attributes\n"));
17194577Sahrens 		(void) printf(gettext("\nFor more information on a particular "
17204577Sahrens 		    "version, including supported releases, see:\n\n"));
17214577Sahrens 		(void) printf("http://www.opensolaris.org/os/community/zfs/"
17224577Sahrens 		    "version/zpl/N\n\n");
17234577Sahrens 		(void) printf(gettext("Where 'N' is the version number.\n"));
17244577Sahrens 		ret = 0;
17254577Sahrens 	} else if (argc || all) {
17264577Sahrens 		/* Upgrade filesystems */
17274577Sahrens 		if (cb.cb_version == 0)
17284577Sahrens 			cb.cb_version = ZPL_VERSION;
17297538SRichard.Morris@Sun.COM 		ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
17309365SChris.Gerhard@sun.com 		    NULL, NULL, 0, upgrade_set_callback, &cb);
17314577Sahrens 		(void) printf(gettext("%llu filesystems upgraded\n"),
17324577Sahrens 		    cb.cb_numupgraded);
17334577Sahrens 		if (cb.cb_numsamegraded) {
17344577Sahrens 			(void) printf(gettext("%llu filesystems already at "
17354577Sahrens 			    "this version\n"),
17364577Sahrens 			    cb.cb_numsamegraded);
17374577Sahrens 		}
17384849Sahrens 		if (cb.cb_numfailed != 0)
17394577Sahrens 			ret = 1;
17404577Sahrens 	} else {
17414577Sahrens 		/* List old-version filesytems */
17424577Sahrens 		boolean_t found;
17434577Sahrens 		(void) printf(gettext("This system is currently running "
17444577Sahrens 		    "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
17454577Sahrens 
17467538SRichard.Morris@Sun.COM 		flags |= ZFS_ITER_RECURSE;
17477538SRichard.Morris@Sun.COM 		ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
17489365SChris.Gerhard@sun.com 		    NULL, NULL, 0, upgrade_list_callback, &cb);
17494577Sahrens 
17504577Sahrens 		found = cb.cb_foundone;
17514577Sahrens 		cb.cb_foundone = B_FALSE;
17524577Sahrens 		cb.cb_newer = B_TRUE;
17534577Sahrens 
17547538SRichard.Morris@Sun.COM 		ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
17559365SChris.Gerhard@sun.com 		    NULL, NULL, 0, upgrade_list_callback, &cb);
17564577Sahrens 
17574577Sahrens 		if (!cb.cb_foundone && !found) {
17584577Sahrens 			(void) printf(gettext("All filesystems are "
17594577Sahrens 			    "formatted with the current version.\n"));
17604577Sahrens 		}
17614577Sahrens 	}
17624577Sahrens 
17634577Sahrens 	return (ret);
17644577Sahrens }
17654577Sahrens 
1766789Sahrens /*
17679396SMatthew.Ahrens@Sun.COM  * zfs userspace
17689396SMatthew.Ahrens@Sun.COM  */
17699554SMatthew.Ahrens@Sun.COM static int
17709396SMatthew.Ahrens@Sun.COM userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
17719396SMatthew.Ahrens@Sun.COM {
17729396SMatthew.Ahrens@Sun.COM 	zfs_userquota_prop_t *typep = arg;
17739396SMatthew.Ahrens@Sun.COM 	zfs_userquota_prop_t p = *typep;
17749554SMatthew.Ahrens@Sun.COM 	char *name = NULL;
17759554SMatthew.Ahrens@Sun.COM 	char *ug, *propname;
17769396SMatthew.Ahrens@Sun.COM 	char namebuf[32];
17779396SMatthew.Ahrens@Sun.COM 	char sizebuf[32];
17789396SMatthew.Ahrens@Sun.COM 
17799396SMatthew.Ahrens@Sun.COM 	if (domain == NULL || domain[0] == '\0') {
17809396SMatthew.Ahrens@Sun.COM 		if (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) {
17819396SMatthew.Ahrens@Sun.COM 			struct group *g = getgrgid(rid);
17829396SMatthew.Ahrens@Sun.COM 			if (g)
17839396SMatthew.Ahrens@Sun.COM 				name = g->gr_name;
17849396SMatthew.Ahrens@Sun.COM 		} else {
17859396SMatthew.Ahrens@Sun.COM 			struct passwd *p = getpwuid(rid);
17869396SMatthew.Ahrens@Sun.COM 			if (p)
17879396SMatthew.Ahrens@Sun.COM 				name = p->pw_name;
17889396SMatthew.Ahrens@Sun.COM 		}
17899396SMatthew.Ahrens@Sun.COM 	}
17909396SMatthew.Ahrens@Sun.COM 
17919396SMatthew.Ahrens@Sun.COM 	if (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA)
17929396SMatthew.Ahrens@Sun.COM 		ug = "group";
17939396SMatthew.Ahrens@Sun.COM 	else
17949396SMatthew.Ahrens@Sun.COM 		ug = "user";
17959396SMatthew.Ahrens@Sun.COM 
17969396SMatthew.Ahrens@Sun.COM 	if (p == ZFS_PROP_USERUSED || p == ZFS_PROP_GROUPUSED)
17979396SMatthew.Ahrens@Sun.COM 		propname = "used";
17989396SMatthew.Ahrens@Sun.COM 	else
17999396SMatthew.Ahrens@Sun.COM 		propname = "quota";
18009396SMatthew.Ahrens@Sun.COM 
18019554SMatthew.Ahrens@Sun.COM 	if (name == NULL) {
18029396SMatthew.Ahrens@Sun.COM 		(void) snprintf(namebuf, sizeof (namebuf),
18039396SMatthew.Ahrens@Sun.COM 		    "%llu", (longlong_t)rid);
18049396SMatthew.Ahrens@Sun.COM 		name = namebuf;
18059396SMatthew.Ahrens@Sun.COM 	}
18069396SMatthew.Ahrens@Sun.COM 	zfs_nicenum(space, sizebuf, sizeof (sizebuf));
18079396SMatthew.Ahrens@Sun.COM 
18089396SMatthew.Ahrens@Sun.COM 	(void) printf("%s %s %s%c%s %s\n", propname, ug, domain,
18099396SMatthew.Ahrens@Sun.COM 	    domain[0] ? '-' : ' ', name, sizebuf);
18109554SMatthew.Ahrens@Sun.COM 
18119554SMatthew.Ahrens@Sun.COM 	return (0);
18129396SMatthew.Ahrens@Sun.COM }
18139396SMatthew.Ahrens@Sun.COM 
18149396SMatthew.Ahrens@Sun.COM static int
18159396SMatthew.Ahrens@Sun.COM zfs_do_userspace(int argc, char **argv)
18169396SMatthew.Ahrens@Sun.COM {
18179396SMatthew.Ahrens@Sun.COM 	zfs_handle_t *zhp;
18189396SMatthew.Ahrens@Sun.COM 	zfs_userquota_prop_t p;
18199396SMatthew.Ahrens@Sun.COM 	int error;
18209396SMatthew.Ahrens@Sun.COM 
18219396SMatthew.Ahrens@Sun.COM 	/*
18229396SMatthew.Ahrens@Sun.COM 	 * Try the python version.  If the execv fails, we'll continue
18239396SMatthew.Ahrens@Sun.COM 	 * and do a simplistic implementation.
18249396SMatthew.Ahrens@Sun.COM 	 */
18259396SMatthew.Ahrens@Sun.COM 	(void) execv(pypath, argv-1);
18269396SMatthew.Ahrens@Sun.COM 
18279396SMatthew.Ahrens@Sun.COM 	(void) printf("internal error: %s not found\n"
18289396SMatthew.Ahrens@Sun.COM 	    "falling back on built-in implementation, "
18299396SMatthew.Ahrens@Sun.COM 	    "some features will not work\n", pypath);
18309396SMatthew.Ahrens@Sun.COM 
18319396SMatthew.Ahrens@Sun.COM 	if ((zhp = zfs_open(g_zfs, argv[argc-1], ZFS_TYPE_DATASET)) == NULL)
18329396SMatthew.Ahrens@Sun.COM 		return (1);
18339396SMatthew.Ahrens@Sun.COM 
18349396SMatthew.Ahrens@Sun.COM 	(void) printf("PROP TYPE NAME VALUE\n");
18359396SMatthew.Ahrens@Sun.COM 
18369396SMatthew.Ahrens@Sun.COM 	for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
18379396SMatthew.Ahrens@Sun.COM 		error = zfs_userspace(zhp, p, userspace_cb, &p);
18389396SMatthew.Ahrens@Sun.COM 		if (error)
18399396SMatthew.Ahrens@Sun.COM 			break;
18409396SMatthew.Ahrens@Sun.COM 	}
18419396SMatthew.Ahrens@Sun.COM 	return (error);
18429396SMatthew.Ahrens@Sun.COM }
18439396SMatthew.Ahrens@Sun.COM 
18449396SMatthew.Ahrens@Sun.COM /*
18459365SChris.Gerhard@sun.com  * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
18462379Ssjelinek  *      [-s property [-s property]...] [-S property [-S property]...]
18472379Ssjelinek  *      <dataset> ...
1848789Sahrens  *
184911022STom.Erickson@Sun.COM  *	-r	Recurse over all children
185011022STom.Erickson@Sun.COM  *	-d	Limit recursion by depth.
185111022STom.Erickson@Sun.COM  *	-H	Scripted mode; elide headers and separate columns by tabs
185211022STom.Erickson@Sun.COM  *	-o	Control which fields to display.
185311022STom.Erickson@Sun.COM  *	-t	Control which object types to display.
18542379Ssjelinek  *	-s	Specify sort columns, descending order.
18552379Ssjelinek  *	-S	Specify sort columns, ascending order.
1856789Sahrens  *
1857789Sahrens  * When given no arguments, lists all filesystems in the system.
1858789Sahrens  * Otherwise, list the specified datasets, optionally recursing down them if
1859789Sahrens  * '-r' is specified.
1860789Sahrens  */
1861789Sahrens typedef struct list_cbdata {
18622082Seschrock 	boolean_t	cb_first;
18632082Seschrock 	boolean_t	cb_scripted;
18645094Slling 	zprop_list_t	*cb_proplist;
1865789Sahrens } list_cbdata_t;
1866789Sahrens 
1867789Sahrens /*
1868789Sahrens  * Given a list of columns to display, output appropriate headers for each one.
1869789Sahrens  */
1870789Sahrens static void
18715094Slling print_header(zprop_list_t *pl)
1872789Sahrens {
18732676Seschrock 	char headerbuf[ZFS_MAXPROPLEN];
18742676Seschrock 	const char *header;
1875789Sahrens 	int i;
18762676Seschrock 	boolean_t first = B_TRUE;
18772676Seschrock 	boolean_t right_justify;
18782676Seschrock 
18792676Seschrock 	for (; pl != NULL; pl = pl->pl_next) {
18802676Seschrock 		if (!first) {
1881789Sahrens 			(void) printf("  ");
18822676Seschrock 		} else {
18832676Seschrock 			first = B_FALSE;
18842676Seschrock 		}
18852676Seschrock 
18862676Seschrock 		right_justify = B_FALSE;
18875094Slling 		if (pl->pl_prop != ZPROP_INVAL) {
18882676Seschrock 			header = zfs_prop_column_name(pl->pl_prop);
18892676Seschrock 			right_justify = zfs_prop_align_right(pl->pl_prop);
18902676Seschrock 		} else {
18912676Seschrock 			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
18922676Seschrock 				headerbuf[i] = toupper(pl->pl_user_prop[i]);
18932676Seschrock 			headerbuf[i] = '\0';
18942676Seschrock 			header = headerbuf;
18952676Seschrock 		}
18962676Seschrock 
18972676Seschrock 		if (pl->pl_next == NULL && !right_justify)
18982676Seschrock 			(void) printf("%s", header);
18992676Seschrock 		else if (right_justify)
19002676Seschrock 			(void) printf("%*s", pl->pl_width, header);
19012676Seschrock 		else
19022676Seschrock 			(void) printf("%-*s", pl->pl_width, header);
1903789Sahrens 	}
1904789Sahrens 
1905789Sahrens 	(void) printf("\n");
1906789Sahrens }
1907789Sahrens 
1908789Sahrens /*
1909789Sahrens  * Given a dataset and a list of fields, print out all the properties according
1910789Sahrens  * to the described layout.
1911789Sahrens  */
1912789Sahrens static void
19135094Slling print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
1914789Sahrens {
19152676Seschrock 	boolean_t first = B_TRUE;
1916789Sahrens 	char property[ZFS_MAXPROPLEN];
19172676Seschrock 	nvlist_t *userprops = zfs_get_user_props(zhp);
19182676Seschrock 	nvlist_t *propval;
19192676Seschrock 	char *propstr;
19202676Seschrock 	boolean_t right_justify;
19212676Seschrock 	int width;
19222676Seschrock 
19232676Seschrock 	for (; pl != NULL; pl = pl->pl_next) {
19242676Seschrock 		if (!first) {
1925789Sahrens 			if (scripted)
1926789Sahrens 				(void) printf("\t");
1927789Sahrens 			else
1928789Sahrens 				(void) printf("  ");
19292676Seschrock 		} else {
19302676Seschrock 			first = B_FALSE;
1931789Sahrens 		}
1932789Sahrens 
19335094Slling 		if (pl->pl_prop != ZPROP_INVAL) {
19342676Seschrock 			if (zfs_prop_get(zhp, pl->pl_prop, property,
19352676Seschrock 			    sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
19362676Seschrock 				propstr = "-";
19372676Seschrock 			else
19382676Seschrock 				propstr = property;
19392676Seschrock 
19402676Seschrock 			right_justify = zfs_prop_align_right(pl->pl_prop);
19419396SMatthew.Ahrens@Sun.COM 		} else if (zfs_prop_userquota(pl->pl_user_prop)) {
19429396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
19439396SMatthew.Ahrens@Sun.COM 			    property, sizeof (property), B_FALSE) != 0)
19449396SMatthew.Ahrens@Sun.COM 				propstr = "-";
19459396SMatthew.Ahrens@Sun.COM 			else
19469396SMatthew.Ahrens@Sun.COM 				propstr = property;
19479396SMatthew.Ahrens@Sun.COM 			right_justify = B_TRUE;
19482676Seschrock 		} else {
19492676Seschrock 			if (nvlist_lookup_nvlist(userprops,
19502676Seschrock 			    pl->pl_user_prop, &propval) != 0)
19512676Seschrock 				propstr = "-";
19522676Seschrock 			else
19532676Seschrock 				verify(nvlist_lookup_string(propval,
19545094Slling 				    ZPROP_VALUE, &propstr) == 0);
19559396SMatthew.Ahrens@Sun.COM 			right_justify = B_FALSE;
19562676Seschrock 		}
19572676Seschrock 
19582676Seschrock 		width = pl->pl_width;
1959789Sahrens 
1960866Seschrock 		/*
1961866Seschrock 		 * If this is being called in scripted mode, or if this is the
1962866Seschrock 		 * last column and it is left-justified, don't include a width
1963866Seschrock 		 * format specifier.
1964866Seschrock 		 */
19652676Seschrock 		if (scripted || (pl->pl_next == NULL && !right_justify))
19662676Seschrock 			(void) printf("%s", propstr);
19672676Seschrock 		else if (right_justify)
19682676Seschrock 			(void) printf("%*s", width, propstr);
19692676Seschrock 		else
19702676Seschrock 			(void) printf("%-*s", width, propstr);
1971789Sahrens 	}
1972789Sahrens 
1973789Sahrens 	(void) printf("\n");
1974789Sahrens }
1975789Sahrens 
1976789Sahrens /*
1977789Sahrens  * Generic callback function to list a dataset or snapshot.
1978789Sahrens  */
1979789Sahrens static int
1980789Sahrens list_callback(zfs_handle_t *zhp, void *data)
1981789Sahrens {
1982789Sahrens 	list_cbdata_t *cbp = data;
1983789Sahrens 
1984789Sahrens 	if (cbp->cb_first) {
1985789Sahrens 		if (!cbp->cb_scripted)
19862676Seschrock 			print_header(cbp->cb_proplist);
19872082Seschrock 		cbp->cb_first = B_FALSE;
1988789Sahrens 	}
1989789Sahrens 
19902676Seschrock 	print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
1991789Sahrens 
1992789Sahrens 	return (0);
1993789Sahrens }
1994789Sahrens 
1995789Sahrens static int
1996789Sahrens zfs_do_list(int argc, char **argv)
1997789Sahrens {
1998789Sahrens 	int c;
19992082Seschrock 	boolean_t scripted = B_FALSE;
2000789Sahrens 	static char default_fields[] =
2001789Sahrens 	    "name,used,available,referenced,mountpoint";
20028415SRichard.Morris@Sun.COM 	int types = ZFS_TYPE_DATASET;
20037538SRichard.Morris@Sun.COM 	boolean_t types_specified = B_FALSE;
2004789Sahrens 	char *fields = NULL;
2005789Sahrens 	list_cbdata_t cb = { 0 };
2006789Sahrens 	char *value;
20079365SChris.Gerhard@sun.com 	int limit = 0;
2008789Sahrens 	int ret;
20092379Ssjelinek 	zfs_sort_column_t *sortcol = NULL;
20107538SRichard.Morris@Sun.COM 	int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
2011789Sahrens 
2012789Sahrens 	/* check options */
20139365SChris.Gerhard@sun.com 	while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
2014789Sahrens 		switch (c) {
2015789Sahrens 		case 'o':
2016789Sahrens 			fields = optarg;
2017789Sahrens 			break;
20189365SChris.Gerhard@sun.com 		case 'd':
20199365SChris.Gerhard@sun.com 			limit = parse_depth(optarg, &flags);
20209365SChris.Gerhard@sun.com 			break;
2021789Sahrens 		case 'r':
20227538SRichard.Morris@Sun.COM 			flags |= ZFS_ITER_RECURSE;
2023789Sahrens 			break;
2024789Sahrens 		case 'H':
20252082Seschrock 			scripted = B_TRUE;
2026789Sahrens 			break;
20272379Ssjelinek 		case 's':
20282676Seschrock 			if (zfs_add_sort_column(&sortcol, optarg,
20292676Seschrock 			    B_FALSE) != 0) {
20302379Ssjelinek 				(void) fprintf(stderr,
20312379Ssjelinek 				    gettext("invalid property '%s'\n"), optarg);
20322379Ssjelinek 				usage(B_FALSE);
20332379Ssjelinek 			}
20342379Ssjelinek 			break;
20352379Ssjelinek 		case 'S':
20362676Seschrock 			if (zfs_add_sort_column(&sortcol, optarg,
20372676Seschrock 			    B_TRUE) != 0) {
20382379Ssjelinek 				(void) fprintf(stderr,
20392379Ssjelinek 				    gettext("invalid property '%s'\n"), optarg);
20402379Ssjelinek 				usage(B_FALSE);
20412379Ssjelinek 			}
20422379Ssjelinek 			break;
2043789Sahrens 		case 't':
2044789Sahrens 			types = 0;
20457538SRichard.Morris@Sun.COM 			types_specified = B_TRUE;
20467538SRichard.Morris@Sun.COM 			flags &= ~ZFS_ITER_PROP_LISTSNAPS;
2047789Sahrens 			while (*optarg != '\0') {
20487538SRichard.Morris@Sun.COM 				static char *type_subopts[] = { "filesystem",
20497538SRichard.Morris@Sun.COM 				    "volume", "snapshot", "all", NULL };
20507538SRichard.Morris@Sun.COM 
2051789Sahrens 				switch (getsubopt(&optarg, type_subopts,
2052789Sahrens 				    &value)) {
2053789Sahrens 				case 0:
2054789Sahrens 					types |= ZFS_TYPE_FILESYSTEM;
2055789Sahrens 					break;
2056789Sahrens 				case 1:
2057789Sahrens 					types |= ZFS_TYPE_VOLUME;
2058789Sahrens 					break;
2059789Sahrens 				case 2:
2060789Sahrens 					types |= ZFS_TYPE_SNAPSHOT;
2061789Sahrens 					break;
20627538SRichard.Morris@Sun.COM 				case 3:
20637538SRichard.Morris@Sun.COM 					types = ZFS_TYPE_DATASET;
20647538SRichard.Morris@Sun.COM 					break;
20657538SRichard.Morris@Sun.COM 
2066789Sahrens 				default:
2067789Sahrens 					(void) fprintf(stderr,
2068789Sahrens 					    gettext("invalid type '%s'\n"),
2069789Sahrens 					    value);
20702082Seschrock 					usage(B_FALSE);
2071789Sahrens 				}
2072789Sahrens 			}
2073789Sahrens 			break;
2074789Sahrens 		case ':':
2075789Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
2076789Sahrens 			    "'%c' option\n"), optopt);
20772082Seschrock 			usage(B_FALSE);
2078789Sahrens 			break;
2079789Sahrens 		case '?':
2080789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2081789Sahrens 			    optopt);
20822082Seschrock 			usage(B_FALSE);
2083789Sahrens 		}
2084789Sahrens 	}
2085789Sahrens 
2086789Sahrens 	argc -= optind;
2087789Sahrens 	argv += optind;
2088789Sahrens 
2089789Sahrens 	if (fields == NULL)
20907390SMatthew.Ahrens@Sun.COM 		fields = default_fields;
20917390SMatthew.Ahrens@Sun.COM 
20927390SMatthew.Ahrens@Sun.COM 	/*
20937538SRichard.Morris@Sun.COM 	 * If "-o space" and no types were specified, don't display snapshots.
20947390SMatthew.Ahrens@Sun.COM 	 */
20957538SRichard.Morris@Sun.COM 	if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
20967390SMatthew.Ahrens@Sun.COM 		types &= ~ZFS_TYPE_SNAPSHOT;
2097789Sahrens 
2098866Seschrock 	/*
20995094Slling 	 * If the user specifies '-o all', the zprop_get_list() doesn't
2100866Seschrock 	 * normally include the name of the dataset.  For 'zfs list', we always
2101866Seschrock 	 * want this property to be first.
2102866Seschrock 	 */
21035094Slling 	if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
21045094Slling 	    != 0)
21052082Seschrock 		usage(B_FALSE);
21062676Seschrock 
2107789Sahrens 	cb.cb_scripted = scripted;
21082082Seschrock 	cb.cb_first = B_TRUE;
2109789Sahrens 
21107538SRichard.Morris@Sun.COM 	ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
21119365SChris.Gerhard@sun.com 	    limit, list_callback, &cb);
21122379Ssjelinek 
21135094Slling 	zprop_free_list(cb.cb_proplist);
21142379Ssjelinek 	zfs_free_sort_columns(sortcol);
2115789Sahrens 
21164221Smmusante 	if (ret == 0 && cb.cb_first && !cb.cb_scripted)
2117789Sahrens 		(void) printf(gettext("no datasets available\n"));
2118789Sahrens 
2119789Sahrens 	return (ret);
2120789Sahrens }
2121789Sahrens 
2122789Sahrens /*
21234490Svb160487  * zfs rename <fs | snap | vol> <fs | snap | vol>
21244490Svb160487  * zfs rename -p <fs | vol> <fs | vol>
21254490Svb160487  * zfs rename -r <snap> <snap>
2126789Sahrens  *
2127789Sahrens  * Renames the given dataset to another of the same type.
21284490Svb160487  *
21294490Svb160487  * The '-p' flag creates all the non-existing ancestors of the target first.
2130789Sahrens  */
2131789Sahrens /* ARGSUSED */
2132789Sahrens static int
2133789Sahrens zfs_do_rename(int argc, char **argv)
2134789Sahrens {
2135789Sahrens 	zfs_handle_t *zhp;
21364007Smmusante 	int c;
21372082Seschrock 	int ret;
21384490Svb160487 	boolean_t recurse = B_FALSE;
21394490Svb160487 	boolean_t parents = B_FALSE;
2140789Sahrens 
2141789Sahrens 	/* check options */
21424490Svb160487 	while ((c = getopt(argc, argv, "pr")) != -1) {
21434007Smmusante 		switch (c) {
21444490Svb160487 		case 'p':
21454490Svb160487 			parents = B_TRUE;
21464490Svb160487 			break;
21474007Smmusante 		case 'r':
21484490Svb160487 			recurse = B_TRUE;
21494007Smmusante 			break;
21504007Smmusante 		case '?':
21514007Smmusante 		default:
21524007Smmusante 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
21534007Smmusante 			    optopt);
21544007Smmusante 			usage(B_FALSE);
21554007Smmusante 		}
2156789Sahrens 	}
2157789Sahrens 
21584007Smmusante 	argc -= optind;
21594007Smmusante 	argv += optind;
21604007Smmusante 
2161789Sahrens 	/* check number of arguments */
21624007Smmusante 	if (argc < 1) {
2163789Sahrens 		(void) fprintf(stderr, gettext("missing source dataset "
2164789Sahrens 		    "argument\n"));
21652082Seschrock 		usage(B_FALSE);
2166789Sahrens 	}
21674007Smmusante 	if (argc < 2) {
2168789Sahrens 		(void) fprintf(stderr, gettext("missing target dataset "
2169789Sahrens 		    "argument\n"));
21702082Seschrock 		usage(B_FALSE);
2171789Sahrens 	}
21724007Smmusante 	if (argc > 2) {
2173789Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
21742082Seschrock 		usage(B_FALSE);
2175789Sahrens 	}
2176789Sahrens 
21774490Svb160487 	if (recurse && parents) {
21784490Svb160487 		(void) fprintf(stderr, gettext("-p and -r options are mutually "
21794490Svb160487 		    "exclusive\n"));
21804490Svb160487 		usage(B_FALSE);
21814490Svb160487 	}
21824490Svb160487 
21834007Smmusante 	if (recurse && strchr(argv[0], '@') == 0) {
21844007Smmusante 		(void) fprintf(stderr, gettext("source dataset for recursive "
21854007Smmusante 		    "rename must be a snapshot\n"));
21864007Smmusante 		usage(B_FALSE);
21874007Smmusante 	}
21884007Smmusante 
21894490Svb160487 	if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
21905094Slling 	    ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
2191789Sahrens 		return (1);
2192789Sahrens 
21934490Svb160487 	/* If we were asked and the name looks good, try to create ancestors. */
21944490Svb160487 	if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
21954490Svb160487 	    zfs_create_ancestors(g_zfs, argv[1]) != 0) {
21964490Svb160487 		zfs_close(zhp);
21974490Svb160487 		return (1);
21984490Svb160487 	}
21994490Svb160487 
22004007Smmusante 	ret = (zfs_rename(zhp, argv[1], recurse) != 0);
22012082Seschrock 
22022082Seschrock 	zfs_close(zhp);
22032082Seschrock 	return (ret);
22042082Seschrock }
22052082Seschrock 
22062082Seschrock /*
22072082Seschrock  * zfs promote <fs>
22082082Seschrock  *
22092082Seschrock  * Promotes the given clone fs to be the parent
22102082Seschrock  */
22112082Seschrock /* ARGSUSED */
22122082Seschrock static int
22132082Seschrock zfs_do_promote(int argc, char **argv)
22142082Seschrock {
22152082Seschrock 	zfs_handle_t *zhp;
22162082Seschrock 	int ret;
22172082Seschrock 
22182082Seschrock 	/* check options */
22192082Seschrock 	if (argc > 1 && argv[1][0] == '-') {
22202082Seschrock 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
22212082Seschrock 		    argv[1][1]);
22222082Seschrock 		usage(B_FALSE);
22232082Seschrock 	}
22242082Seschrock 
22252082Seschrock 	/* check number of arguments */
22262082Seschrock 	if (argc < 2) {
22272082Seschrock 		(void) fprintf(stderr, gettext("missing clone filesystem"
22282597Snd150628 		    " argument\n"));
22292082Seschrock 		usage(B_FALSE);
22302082Seschrock 	}
22312082Seschrock 	if (argc > 2) {
22322082Seschrock 		(void) fprintf(stderr, gettext("too many arguments\n"));
22332082Seschrock 		usage(B_FALSE);
22342082Seschrock 	}
22352082Seschrock 
22362082Seschrock 	zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
22372082Seschrock 	if (zhp == NULL)
22382082Seschrock 		return (1);
22392082Seschrock 
22402082Seschrock 	ret = (zfs_promote(zhp) != 0);
22412082Seschrock 
22422926Sek110237 
2243789Sahrens 	zfs_close(zhp);
2244789Sahrens 	return (ret);
2245789Sahrens }
2246789Sahrens 
2247789Sahrens /*
22485568Sahrens  * zfs rollback [-rRf] <snapshot>
2249789Sahrens  *
225011022STom.Erickson@Sun.COM  *	-r	Delete any intervening snapshots before doing rollback
225111022STom.Erickson@Sun.COM  *	-R	Delete any snapshots and their clones
225211022STom.Erickson@Sun.COM  *	-f	ignored for backwards compatability
2253789Sahrens  *
2254789Sahrens  * Given a filesystem, rollback to a specific snapshot, discarding any changes
2255789Sahrens  * since then and making it the active dataset.  If more recent snapshots exist,
2256789Sahrens  * the command will complain unless the '-r' flag is given.
2257789Sahrens  */
2258789Sahrens typedef struct rollback_cbdata {
2259789Sahrens 	uint64_t	cb_create;
22602082Seschrock 	boolean_t	cb_first;
2261789Sahrens 	int		cb_doclones;
2262789Sahrens 	char		*cb_target;
2263789Sahrens 	int		cb_error;
22642082Seschrock 	boolean_t	cb_recurse;
22652082Seschrock 	boolean_t	cb_dependent;
2266789Sahrens } rollback_cbdata_t;
2267789Sahrens 
2268789Sahrens /*
2269789Sahrens  * Report any snapshots more recent than the one specified.  Used when '-r' is
2270789Sahrens  * not specified.  We reuse this same callback for the snapshot dependents - if
2271789Sahrens  * 'cb_dependent' is set, then this is a dependent and we should report it
2272789Sahrens  * without checking the transaction group.
2273789Sahrens  */
2274789Sahrens static int
2275789Sahrens rollback_check(zfs_handle_t *zhp, void *data)
2276789Sahrens {
2277789Sahrens 	rollback_cbdata_t *cbp = data;
2278789Sahrens 
22792082Seschrock 	if (cbp->cb_doclones) {
22802082Seschrock 		zfs_close(zhp);
2281789Sahrens 		return (0);
22822082Seschrock 	}
2283789Sahrens 
2284789Sahrens 	if (!cbp->cb_dependent) {
2285789Sahrens 		if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
22861294Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2287789Sahrens 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
2288789Sahrens 		    cbp->cb_create) {
2289789Sahrens 
2290789Sahrens 			if (cbp->cb_first && !cbp->cb_recurse) {
2291789Sahrens 				(void) fprintf(stderr, gettext("cannot "
2292789Sahrens 				    "rollback to '%s': more recent snapshots "
2293789Sahrens 				    "exist\n"),
2294789Sahrens 				    cbp->cb_target);
2295789Sahrens 				(void) fprintf(stderr, gettext("use '-r' to "
2296789Sahrens 				    "force deletion of the following "
2297789Sahrens 				    "snapshots:\n"));
2298789Sahrens 				cbp->cb_first = 0;
2299789Sahrens 				cbp->cb_error = 1;
2300789Sahrens 			}
2301789Sahrens 
2302789Sahrens 			if (cbp->cb_recurse) {
23032082Seschrock 				cbp->cb_dependent = B_TRUE;
23042474Seschrock 				if (zfs_iter_dependents(zhp, B_TRUE,
23052474Seschrock 				    rollback_check, cbp) != 0) {
23062474Seschrock 					zfs_close(zhp);
23072474Seschrock 					return (-1);
23082474Seschrock 				}
23092082Seschrock 				cbp->cb_dependent = B_FALSE;
2310789Sahrens 			} else {
2311789Sahrens 				(void) fprintf(stderr, "%s\n",
2312789Sahrens 				    zfs_get_name(zhp));
2313789Sahrens 			}
2314789Sahrens 		}
2315789Sahrens 	} else {
2316789Sahrens 		if (cbp->cb_first && cbp->cb_recurse) {
2317789Sahrens 			(void) fprintf(stderr, gettext("cannot rollback to "
2318789Sahrens 			    "'%s': clones of previous snapshots exist\n"),
2319789Sahrens 			    cbp->cb_target);
2320789Sahrens 			(void) fprintf(stderr, gettext("use '-R' to "
2321789Sahrens 			    "force deletion of the following clones and "
2322789Sahrens 			    "dependents:\n"));
2323789Sahrens 			cbp->cb_first = 0;
2324789Sahrens 			cbp->cb_error = 1;
2325789Sahrens 		}
2326789Sahrens 
2327789Sahrens 		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
2328789Sahrens 	}
2329789Sahrens 
2330789Sahrens 	zfs_close(zhp);
2331789Sahrens 	return (0);
2332789Sahrens }
2333789Sahrens 
2334789Sahrens static int
2335789Sahrens zfs_do_rollback(int argc, char **argv)
2336789Sahrens {
2337789Sahrens 	int ret;
2338789Sahrens 	int c;
23395749Sahrens 	boolean_t force = B_FALSE;
2340789Sahrens 	rollback_cbdata_t cb = { 0 };
2341789Sahrens 	zfs_handle_t *zhp, *snap;
2342789Sahrens 	char parentname[ZFS_MAXNAMELEN];
2343789Sahrens 	char *delim;
2344789Sahrens 
2345789Sahrens 	/* check options */
23465568Sahrens 	while ((c = getopt(argc, argv, "rRf")) != -1) {
2347789Sahrens 		switch (c) {
2348789Sahrens 		case 'r':
2349789Sahrens 			cb.cb_recurse = 1;
2350789Sahrens 			break;
2351789Sahrens 		case 'R':
2352789Sahrens 			cb.cb_recurse = 1;
2353789Sahrens 			cb.cb_doclones = 1;
2354789Sahrens 			break;
23555568Sahrens 		case 'f':
23565749Sahrens 			force = B_TRUE;
23575568Sahrens 			break;
2358789Sahrens 		case '?':
2359789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2360789Sahrens 			    optopt);
23612082Seschrock 			usage(B_FALSE);
2362789Sahrens 		}
2363789Sahrens 	}
2364789Sahrens 
2365789Sahrens 	argc -= optind;
2366789Sahrens 	argv += optind;
2367789Sahrens 
2368789Sahrens 	/* check number of arguments */
2369789Sahrens 	if (argc < 1) {
2370789Sahrens 		(void) fprintf(stderr, gettext("missing dataset argument\n"));
23712082Seschrock 		usage(B_FALSE);
2372789Sahrens 	}
2373789Sahrens 	if (argc > 1) {
2374789Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
23752082Seschrock 		usage(B_FALSE);
2376789Sahrens 	}
2377789Sahrens 
2378789Sahrens 	/* open the snapshot */
23792082Seschrock 	if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
2380789Sahrens 		return (1);
2381789Sahrens 
23821294Slling 	/* open the parent dataset */
23831294Slling 	(void) strlcpy(parentname, argv[0], sizeof (parentname));
2384789Sahrens 	verify((delim = strrchr(parentname, '@')) != NULL);
2385789Sahrens 	*delim = '\0';
23865094Slling 	if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
2387789Sahrens 		zfs_close(snap);
2388789Sahrens 		return (1);
2389789Sahrens 	}
2390789Sahrens 
2391789Sahrens 	/*
2392789Sahrens 	 * Check for more recent snapshots and/or clones based on the presence
2393789Sahrens 	 * of '-r' and '-R'.
2394789Sahrens 	 */
23951294Slling 	cb.cb_target = argv[0];
23961294Slling 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
23972082Seschrock 	cb.cb_first = B_TRUE;
2398789Sahrens 	cb.cb_error = 0;
23992474Seschrock 	if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
24002474Seschrock 		goto out;
2401789Sahrens 
2402789Sahrens 	if ((ret = cb.cb_error) != 0)
2403789Sahrens 		goto out;
2404789Sahrens 
2405789Sahrens 	/*
24061294Slling 	 * Rollback parent to the given snapshot.
2407789Sahrens 	 */
24085749Sahrens 	ret = zfs_rollback(zhp, snap, force);
2409789Sahrens 
2410789Sahrens out:
2411789Sahrens 	zfs_close(snap);
2412789Sahrens 	zfs_close(zhp);
2413789Sahrens 
2414789Sahrens 	if (ret == 0)
2415789Sahrens 		return (0);
2416789Sahrens 	else
2417789Sahrens 		return (1);
2418789Sahrens }
2419789Sahrens 
2420789Sahrens /*
2421789Sahrens  * zfs set property=value { fs | snap | vol } ...
2422789Sahrens  *
2423789Sahrens  * Sets the given property for all datasets specified on the command line.
2424789Sahrens  */
2425789Sahrens typedef struct set_cbdata {
2426789Sahrens 	char		*cb_propname;
2427789Sahrens 	char		*cb_value;
2428789Sahrens } set_cbdata_t;
2429789Sahrens 
2430789Sahrens static int
2431789Sahrens set_callback(zfs_handle_t *zhp, void *data)
2432789Sahrens {
2433789Sahrens 	set_cbdata_t *cbp = data;
2434789Sahrens 
24352676Seschrock 	if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
24362169Snd150628 		switch (libzfs_errno(g_zfs)) {
24372169Snd150628 		case EZFS_MOUNTFAILED:
24382169Snd150628 			(void) fprintf(stderr, gettext("property may be set "
24392169Snd150628 			    "but unable to remount filesystem\n"));
24402169Snd150628 			break;
24413126Sahl 		case EZFS_SHARENFSFAILED:
24422169Snd150628 			(void) fprintf(stderr, gettext("property may be set "
24432169Snd150628 			    "but unable to reshare filesystem\n"));
24442169Snd150628 			break;
24452169Snd150628 		}
2446789Sahrens 		return (1);
24472169Snd150628 	}
24482856Snd150628 	return (0);
2449789Sahrens }
2450789Sahrens 
2451789Sahrens static int
2452789Sahrens zfs_do_set(int argc, char **argv)
2453789Sahrens {
2454789Sahrens 	set_cbdata_t cb;
24552926Sek110237 	int ret;
2456789Sahrens 
2457789Sahrens 	/* check for options */
2458789Sahrens 	if (argc > 1 && argv[1][0] == '-') {
2459789Sahrens 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2460789Sahrens 		    argv[1][1]);
24612082Seschrock 		usage(B_FALSE);
2462789Sahrens 	}
2463789Sahrens 
2464789Sahrens 	/* check number of arguments */
2465789Sahrens 	if (argc < 2) {
2466789Sahrens 		(void) fprintf(stderr, gettext("missing property=value "
2467789Sahrens 		    "argument\n"));
24682082Seschrock 		usage(B_FALSE);
2469789Sahrens 	}
2470789Sahrens 	if (argc < 3) {
2471789Sahrens 		(void) fprintf(stderr, gettext("missing dataset name\n"));
24722082Seschrock 		usage(B_FALSE);
2473789Sahrens 	}
2474789Sahrens 
2475789Sahrens 	/* validate property=value argument */
2476789Sahrens 	cb.cb_propname = argv[1];
24776993Ssg201626 	if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
24786993Ssg201626 	    (cb.cb_value[1] == '\0')) {
2479789Sahrens 		(void) fprintf(stderr, gettext("missing value in "
2480789Sahrens 		    "property=value argument\n"));
24812082Seschrock 		usage(B_FALSE);
2482789Sahrens 	}
2483789Sahrens 
2484789Sahrens 	*cb.cb_value = '\0';
2485789Sahrens 	cb.cb_value++;
2486789Sahrens 
2487789Sahrens 	if (*cb.cb_propname == '\0') {
2488789Sahrens 		(void) fprintf(stderr,
2489789Sahrens 		    gettext("missing property in property=value argument\n"));
24902082Seschrock 		usage(B_FALSE);
2491789Sahrens 	}
2492789Sahrens 
24937538SRichard.Morris@Sun.COM 	ret = zfs_for_each(argc - 2, argv + 2, NULL,
24949365SChris.Gerhard@sun.com 	    ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
24952926Sek110237 
24962926Sek110237 	return (ret);
2497789Sahrens }
2498789Sahrens 
2499789Sahrens /*
25007265Sahrens  * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
2501789Sahrens  *
2502789Sahrens  * Creates a snapshot with the given name.  While functionally equivalent to
25035331Samw  * 'zfs create', it is a separate command to differentiate intent.
2504789Sahrens  */
2505789Sahrens static int
2506789Sahrens zfs_do_snapshot(int argc, char **argv)
2507789Sahrens {
25084490Svb160487 	boolean_t recursive = B_FALSE;
25092199Sahrens 	int ret;
25102199Sahrens 	char c;
25117265Sahrens 	nvlist_t *props;
25127265Sahrens 
25137265Sahrens 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
25147265Sahrens 		(void) fprintf(stderr, gettext("internal error: "
25157265Sahrens 		    "out of memory\n"));
25167265Sahrens 		return (1);
25177265Sahrens 	}
25182199Sahrens 
2519789Sahrens 	/* check options */
25207265Sahrens 	while ((c = getopt(argc, argv, "ro:")) != -1) {
25212199Sahrens 		switch (c) {
25227265Sahrens 		case 'o':
25237265Sahrens 			if (parseprop(props))
25247265Sahrens 				return (1);
25257265Sahrens 			break;
25262199Sahrens 		case 'r':
25272199Sahrens 			recursive = B_TRUE;
25282199Sahrens 			break;
25292199Sahrens 		case '?':
25302199Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
25312199Sahrens 			    optopt);
25327265Sahrens 			goto usage;
25332199Sahrens 		}
2534789Sahrens 	}
2535789Sahrens 
25362199Sahrens 	argc -= optind;
25372199Sahrens 	argv += optind;
25382199Sahrens 
2539789Sahrens 	/* check number of arguments */
25402199Sahrens 	if (argc < 1) {
2541789Sahrens 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
25427265Sahrens 		goto usage;
2543789Sahrens 	}
25442199Sahrens 	if (argc > 1) {
2545789Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
25467265Sahrens 		goto usage;
2547789Sahrens 	}
2548789Sahrens 
25497265Sahrens 	ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
25507265Sahrens 	nvlist_free(props);
25512199Sahrens 	if (ret && recursive)
25522199Sahrens 		(void) fprintf(stderr, gettext("no snapshots were created\n"));
25532199Sahrens 	return (ret != 0);
25547265Sahrens 
25557265Sahrens usage:
25567265Sahrens 	nvlist_free(props);
25577265Sahrens 	usage(B_FALSE);
25587265Sahrens 	return (-1);
2559789Sahrens }
2560789Sahrens 
2561789Sahrens /*
256211022STom.Erickson@Sun.COM  * zfs send [-vDp] -R [-i|-I <@snap>] <fs@snap>
256311022STom.Erickson@Sun.COM  * zfs send [-vDp] [-i|-I <@snap>] <fs@snap>
2564789Sahrens  *
2565789Sahrens  * Send a backup stream to stdout.
2566789Sahrens  */
2567789Sahrens static int
25681749Sahrens zfs_do_send(int argc, char **argv)
2569789Sahrens {
2570789Sahrens 	char *fromname = NULL;
25715367Sahrens 	char *toname = NULL;
25722885Sahrens 	char *cp;
25732885Sahrens 	zfs_handle_t *zhp;
257411007SLori.Alt@Sun.COM 	sendflags_t flags = { 0 };
2575789Sahrens 	int c, err;
2576789Sahrens 
2577789Sahrens 	/* check options */
257811022STom.Erickson@Sun.COM 	while ((c = getopt(argc, argv, ":i:I:RDpv")) != -1) {
2579789Sahrens 		switch (c) {
2580789Sahrens 		case 'i':
25812885Sahrens 			if (fromname)
25822885Sahrens 				usage(B_FALSE);
2583789Sahrens 			fromname = optarg;
2584789Sahrens 			break;
25855367Sahrens 		case 'I':
25865367Sahrens 			if (fromname)
25875367Sahrens 				usage(B_FALSE);
25885367Sahrens 			fromname = optarg;
258911007SLori.Alt@Sun.COM 			flags.doall = B_TRUE;
25905367Sahrens 			break;
25915367Sahrens 		case 'R':
259211007SLori.Alt@Sun.COM 			flags.replicate = B_TRUE;
25935367Sahrens 			break;
259411022STom.Erickson@Sun.COM 		case 'p':
259511022STom.Erickson@Sun.COM 			flags.props = B_TRUE;
259611022STom.Erickson@Sun.COM 			break;
25975367Sahrens 		case 'v':
259811007SLori.Alt@Sun.COM 			flags.verbose = B_TRUE;
259911007SLori.Alt@Sun.COM 			break;
260011007SLori.Alt@Sun.COM 		case 'D':
260111007SLori.Alt@Sun.COM 			flags.dedup = B_TRUE;
26025367Sahrens 			break;
2603789Sahrens 		case ':':
2604789Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
2605789Sahrens 			    "'%c' option\n"), optopt);
26062082Seschrock 			usage(B_FALSE);
2607789Sahrens 			break;
2608789Sahrens 		case '?':
2609789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2610789Sahrens 			    optopt);
26112082Seschrock 			usage(B_FALSE);
2612789Sahrens 		}
2613789Sahrens 	}
2614789Sahrens 
2615789Sahrens 	argc -= optind;
2616789Sahrens 	argv += optind;
2617789Sahrens 
2618789Sahrens 	/* check number of arguments */
2619789Sahrens 	if (argc < 1) {
2620789Sahrens 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
26212082Seschrock 		usage(B_FALSE);
2622789Sahrens 	}
2623789Sahrens 	if (argc > 1) {
2624789Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
26252082Seschrock 		usage(B_FALSE);
2626789Sahrens 	}
2627789Sahrens 
2628789Sahrens 	if (isatty(STDOUT_FILENO)) {
2629789Sahrens 		(void) fprintf(stderr,
26302885Sahrens 		    gettext("Error: Stream can not be written to a terminal.\n"
26313912Slling 		    "You must redirect standard output.\n"));
2632789Sahrens 		return (1);
2633789Sahrens 	}
2634789Sahrens 
26355367Sahrens 	cp = strchr(argv[0], '@');
26365367Sahrens 	if (cp == NULL) {
26375367Sahrens 		(void) fprintf(stderr,
26385367Sahrens 		    gettext("argument must be a snapshot\n"));
26395367Sahrens 		usage(B_FALSE);
26405367Sahrens 	}
26415367Sahrens 	*cp = '\0';
26425367Sahrens 	toname = cp + 1;
26435367Sahrens 	zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
26445367Sahrens 	if (zhp == NULL)
26452665Snd150628 		return (1);
26462665Snd150628 
26472885Sahrens 	/*
26482885Sahrens 	 * If they specified the full path to the snapshot, chop off
26495367Sahrens 	 * everything except the short name of the snapshot, but special
26505367Sahrens 	 * case if they specify the origin.
26512885Sahrens 	 */
26522885Sahrens 	if (fromname && (cp = strchr(fromname, '@')) != NULL) {
26535367Sahrens 		char origin[ZFS_MAXNAMELEN];
26545367Sahrens 		zprop_source_t src;
26555367Sahrens 
26565367Sahrens 		(void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
26575367Sahrens 		    origin, sizeof (origin), &src, NULL, 0, B_FALSE);
26585367Sahrens 
26595367Sahrens 		if (strcmp(origin, fromname) == 0) {
26605367Sahrens 			fromname = NULL;
266111007SLori.Alt@Sun.COM 			flags.fromorigin = B_TRUE;
26625367Sahrens 		} else {
26635367Sahrens 			*cp = '\0';
26645367Sahrens 			if (cp != fromname && strcmp(argv[0], fromname)) {
26655367Sahrens 				(void) fprintf(stderr,
26665367Sahrens 				    gettext("incremental source must be "
26675367Sahrens 				    "in same filesystem\n"));
26685367Sahrens 				usage(B_FALSE);
26695367Sahrens 			}
26705367Sahrens 			fromname = cp + 1;
26715367Sahrens 			if (strchr(fromname, '@') || strchr(fromname, '/')) {
26725367Sahrens 				(void) fprintf(stderr,
26735367Sahrens 				    gettext("invalid incremental source\n"));
26745367Sahrens 				usage(B_FALSE);
26755367Sahrens 			}
26762885Sahrens 		}
2677789Sahrens 	}
2678789Sahrens 
267911007SLori.Alt@Sun.COM 	if (flags.replicate && fromname == NULL)
268011007SLori.Alt@Sun.COM 		flags.doall = B_TRUE;
268111007SLori.Alt@Sun.COM 
268211007SLori.Alt@Sun.COM 	err = zfs_send(zhp, fromname, toname, flags, STDOUT_FILENO, NULL, 0);
26832885Sahrens 	zfs_close(zhp);
2684789Sahrens 
2685789Sahrens 	return (err != 0);
2686789Sahrens }
2687789Sahrens 
2688789Sahrens /*
268911461STom.Erickson@Sun.COM  * zfs receive [-denvF] <fs@snap>
2690789Sahrens  *
2691789Sahrens  * Restore a backup stream from stdin.
2692789Sahrens  */
2693789Sahrens static int
26941749Sahrens zfs_do_receive(int argc, char **argv)
2695789Sahrens {
2696789Sahrens 	int c, err;
269711022STom.Erickson@Sun.COM 	recvflags_t flags = { 0 };
269811022STom.Erickson@Sun.COM 
2699789Sahrens 	/* check options */
270011461STom.Erickson@Sun.COM 	while ((c = getopt(argc, argv, ":denuvF")) != -1) {
2701789Sahrens 		switch (c) {
2702789Sahrens 		case 'd':
27035367Sahrens 			flags.isprefix = B_TRUE;
2704789Sahrens 			break;
270511461STom.Erickson@Sun.COM 		case 'e':
270611461STom.Erickson@Sun.COM 			flags.isprefix = B_TRUE;
270711461STom.Erickson@Sun.COM 			flags.istail = B_TRUE;
270811461STom.Erickson@Sun.COM 			break;
2709789Sahrens 		case 'n':
27105367Sahrens 			flags.dryrun = B_TRUE;
2711789Sahrens 			break;
27128584SLori.Alt@Sun.COM 		case 'u':
27138584SLori.Alt@Sun.COM 			flags.nomount = B_TRUE;
27148584SLori.Alt@Sun.COM 			break;
2715789Sahrens 		case 'v':
27165367Sahrens 			flags.verbose = B_TRUE;
2717789Sahrens 			break;
27182665Snd150628 		case 'F':
27195367Sahrens 			flags.force = B_TRUE;
27202665Snd150628 			break;
2721789Sahrens 		case ':':
2722789Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
2723789Sahrens 			    "'%c' option\n"), optopt);
27242082Seschrock 			usage(B_FALSE);
2725789Sahrens 			break;
2726789Sahrens 		case '?':
2727789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2728789Sahrens 			    optopt);
27292082Seschrock 			usage(B_FALSE);
2730789Sahrens 		}
2731789Sahrens 	}
2732789Sahrens 
2733789Sahrens 	argc -= optind;
2734789Sahrens 	argv += optind;
2735789Sahrens 
2736789Sahrens 	/* check number of arguments */
2737789Sahrens 	if (argc < 1) {
2738789Sahrens 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
27392082Seschrock 		usage(B_FALSE);
2740789Sahrens 	}
2741789Sahrens 	if (argc > 1) {
2742789Sahrens 		(void) fprintf(stderr, gettext("too many arguments\n"));
27432082Seschrock 		usage(B_FALSE);
2744789Sahrens 	}
2745789Sahrens 
2746789Sahrens 	if (isatty(STDIN_FILENO)) {
2747789Sahrens 		(void) fprintf(stderr,
2748789Sahrens 		    gettext("Error: Backup stream can not be read "
27493912Slling 		    "from a terminal.\n"
27503912Slling 		    "You must redirect standard input.\n"));
2751789Sahrens 		return (1);
2752789Sahrens 	}
2753789Sahrens 
27545367Sahrens 	err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL);
27552926Sek110237 
27564543Smarks 	return (err != 0);
27574543Smarks }
27584543Smarks 
275910242Schris.kirby@sun.com static int
276010242Schris.kirby@sun.com zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
276110242Schris.kirby@sun.com {
276210242Schris.kirby@sun.com 	int errors = 0;
276310242Schris.kirby@sun.com 	int i;
276410242Schris.kirby@sun.com 	const char *tag;
276510242Schris.kirby@sun.com 	boolean_t recursive = B_FALSE;
276610342Schris.kirby@sun.com 	boolean_t temphold = B_FALSE;
276710342Schris.kirby@sun.com 	const char *opts = holding ? "rt" : "r";
276810242Schris.kirby@sun.com 	int c;
276910242Schris.kirby@sun.com 
277010242Schris.kirby@sun.com 	/* check options */
277110342Schris.kirby@sun.com 	while ((c = getopt(argc, argv, opts)) != -1) {
277210242Schris.kirby@sun.com 		switch (c) {
277310242Schris.kirby@sun.com 		case 'r':
277410242Schris.kirby@sun.com 			recursive = B_TRUE;
277510242Schris.kirby@sun.com 			break;
277610342Schris.kirby@sun.com 		case 't':
277710342Schris.kirby@sun.com 			temphold = B_TRUE;
277810342Schris.kirby@sun.com 			break;
277910242Schris.kirby@sun.com 		case '?':
278010242Schris.kirby@sun.com 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
278110242Schris.kirby@sun.com 			    optopt);
278210242Schris.kirby@sun.com 			usage(B_FALSE);
278310242Schris.kirby@sun.com 		}
278410242Schris.kirby@sun.com 	}
278510242Schris.kirby@sun.com 
278610242Schris.kirby@sun.com 	argc -= optind;
278710242Schris.kirby@sun.com 	argv += optind;
278810242Schris.kirby@sun.com 
278910242Schris.kirby@sun.com 	/* check number of arguments */
279010242Schris.kirby@sun.com 	if (argc < 2)
279110242Schris.kirby@sun.com 		usage(B_FALSE);
279210242Schris.kirby@sun.com 
279310242Schris.kirby@sun.com 	tag = argv[0];
279410242Schris.kirby@sun.com 	--argc;
279510242Schris.kirby@sun.com 	++argv;
279610242Schris.kirby@sun.com 
279710342Schris.kirby@sun.com 	if (holding && tag[0] == '.') {
279810342Schris.kirby@sun.com 		/* tags starting with '.' are reserved for libzfs */
279910342Schris.kirby@sun.com 		(void) fprintf(stderr, gettext("tag may not start with '.'\n"));
280010342Schris.kirby@sun.com 		usage(B_FALSE);
280110242Schris.kirby@sun.com 	}
280210242Schris.kirby@sun.com 
280310242Schris.kirby@sun.com 	for (i = 0; i < argc; ++i) {
280410242Schris.kirby@sun.com 		zfs_handle_t *zhp;
280510242Schris.kirby@sun.com 		char parent[ZFS_MAXNAMELEN];
280610242Schris.kirby@sun.com 		const char *delim;
280710242Schris.kirby@sun.com 		char *path = argv[i];
280810242Schris.kirby@sun.com 
280910242Schris.kirby@sun.com 		delim = strchr(path, '@');
281010242Schris.kirby@sun.com 		if (delim == NULL) {
281110242Schris.kirby@sun.com 			(void) fprintf(stderr,
281210242Schris.kirby@sun.com 			    gettext("'%s' is not a snapshot\n"), path);
281310242Schris.kirby@sun.com 			++errors;
281410242Schris.kirby@sun.com 			continue;
281510242Schris.kirby@sun.com 		}
281610242Schris.kirby@sun.com 		(void) strncpy(parent, path, delim - path);
281710242Schris.kirby@sun.com 		parent[delim - path] = '\0';
281810242Schris.kirby@sun.com 
281910242Schris.kirby@sun.com 		zhp = zfs_open(g_zfs, parent,
282010242Schris.kirby@sun.com 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
282110242Schris.kirby@sun.com 		if (zhp == NULL) {
282210242Schris.kirby@sun.com 			++errors;
282310242Schris.kirby@sun.com 			continue;
282410242Schris.kirby@sun.com 		}
282510342Schris.kirby@sun.com 		if (holding) {
282610342Schris.kirby@sun.com 			if (zfs_hold(zhp, delim+1, tag, recursive,
282711417SChris.Kirby@sun.com 			    temphold, B_FALSE) != 0)
282810342Schris.kirby@sun.com 				++errors;
282910342Schris.kirby@sun.com 		} else {
283010342Schris.kirby@sun.com 			if (zfs_release(zhp, delim+1, tag, recursive) != 0)
283110342Schris.kirby@sun.com 				++errors;
283210342Schris.kirby@sun.com 		}
283310242Schris.kirby@sun.com 		zfs_close(zhp);
283410242Schris.kirby@sun.com 	}
283510242Schris.kirby@sun.com 
283610242Schris.kirby@sun.com 	return (errors != 0);
283710242Schris.kirby@sun.com }
283810242Schris.kirby@sun.com 
283910242Schris.kirby@sun.com /*
284010342Schris.kirby@sun.com  * zfs hold [-r] [-t] <tag> <snap> ...
284110242Schris.kirby@sun.com  *
284211022STom.Erickson@Sun.COM  *	-r	Recursively hold
284310342Schris.kirby@sun.com  *	-t	Temporary hold (hidden option)
284410242Schris.kirby@sun.com  *
284510242Schris.kirby@sun.com  * Apply a user-hold with the given tag to the list of snapshots.
284610242Schris.kirby@sun.com  */
284710242Schris.kirby@sun.com static int
284810242Schris.kirby@sun.com zfs_do_hold(int argc, char **argv)
284910242Schris.kirby@sun.com {
285010242Schris.kirby@sun.com 	return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
285110242Schris.kirby@sun.com }
285210242Schris.kirby@sun.com 
285310242Schris.kirby@sun.com /*
285410242Schris.kirby@sun.com  * zfs release [-r] <tag> <snap> ...
285510242Schris.kirby@sun.com  *
285611022STom.Erickson@Sun.COM  *	-r	Recursively release
285710242Schris.kirby@sun.com  *
285810242Schris.kirby@sun.com  * Release a user-hold with the given tag from the list of snapshots.
285910242Schris.kirby@sun.com  */
286010242Schris.kirby@sun.com static int
286110242Schris.kirby@sun.com zfs_do_release(int argc, char **argv)
286210242Schris.kirby@sun.com {
286310242Schris.kirby@sun.com 	return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
286410242Schris.kirby@sun.com }
286510242Schris.kirby@sun.com 
28661356Seschrock typedef struct get_all_cbdata {
28671356Seschrock 	zfs_handle_t	**cb_handles;
28681356Seschrock 	size_t		cb_alloc;
28691356Seschrock 	size_t		cb_used;
28703126Sahl 	uint_t		cb_types;
28714737Smmusante 	boolean_t	cb_verbose;
28721356Seschrock } get_all_cbdata_t;
28731356Seschrock 
28744737Smmusante #define	CHECK_SPINNER 30
28754737Smmusante #define	SPINNER_TIME 3		/* seconds */
28764737Smmusante #define	MOUNT_TIME 5		/* seconds */
28774737Smmusante 
28781356Seschrock static int
28793126Sahl get_one_dataset(zfs_handle_t *zhp, void *data)
28801356Seschrock {
28814737Smmusante 	static char spin[] = { '-', '\\', '|', '/' };
28824737Smmusante 	static int spinval = 0;
28834737Smmusante 	static int spincheck = 0;
28844737Smmusante 	static time_t last_spin_time = (time_t)0;
28851356Seschrock 	get_all_cbdata_t *cbp = data;
28863126Sahl 	zfs_type_t type = zfs_get_type(zhp);
28871356Seschrock 
28884737Smmusante 	if (cbp->cb_verbose) {
28894737Smmusante 		if (--spincheck < 0) {
28904737Smmusante 			time_t now = time(NULL);
28914737Smmusante 			if (last_spin_time + SPINNER_TIME < now) {
28924737Smmusante 				(void) printf("\b%c", spin[spinval++ % 4]);
28934737Smmusante 				(void) fflush(stdout);
28944737Smmusante 				last_spin_time = now;
28954737Smmusante 			}
28964737Smmusante 			spincheck = CHECK_SPINNER;
28974737Smmusante 		}
28984737Smmusante 	}
28994737Smmusante 
29001356Seschrock 	/*
29013126Sahl 	 * Interate over any nested datasets.
29021356Seschrock 	 */
29033126Sahl 	if (type == ZFS_TYPE_FILESYSTEM &&
29043126Sahl 	    zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
29053265Sahrens 		zfs_close(zhp);
29063126Sahl 		return (1);
29073126Sahl 	}
29083126Sahl 
29093126Sahl 	/*
29103126Sahl 	 * Skip any datasets whose type does not match.
29113126Sahl 	 */
29123126Sahl 	if ((type & cbp->cb_types) == 0) {
29131356Seschrock 		zfs_close(zhp);
29141356Seschrock 		return (0);
29151356Seschrock 	}
29161356Seschrock 
29171356Seschrock 	if (cbp->cb_alloc == cbp->cb_used) {
29181356Seschrock 		zfs_handle_t **handles;
29191356Seschrock 
29201356Seschrock 		if (cbp->cb_alloc == 0)
29211356Seschrock 			cbp->cb_alloc = 64;
29221356Seschrock 		else
29231356Seschrock 			cbp->cb_alloc *= 2;
29241356Seschrock 
29251356Seschrock 		handles = safe_malloc(cbp->cb_alloc * sizeof (void *));
29261356Seschrock 
29271356Seschrock 		if (cbp->cb_handles) {
29281356Seschrock 			bcopy(cbp->cb_handles, handles,
29291356Seschrock 			    cbp->cb_used * sizeof (void *));
29301356Seschrock 			free(cbp->cb_handles);
29311356Seschrock 		}
29321356Seschrock 
29331356Seschrock 		cbp->cb_handles = handles;
29341356Seschrock 	}
29351356Seschrock 
29361356Seschrock 	cbp->cb_handles[cbp->cb_used++] = zhp;
29371356Seschrock 
29383126Sahl 	return (0);
29391356Seschrock }
29401356Seschrock 
29411356Seschrock static void
29424737Smmusante get_all_datasets(uint_t types, zfs_handle_t ***dslist, size_t *count,
29434737Smmusante     boolean_t verbose)
29441356Seschrock {
29451356Seschrock 	get_all_cbdata_t cb = { 0 };
29463126Sahl 	cb.cb_types = types;
29474737Smmusante 	cb.cb_verbose = verbose;
29484737Smmusante 
29494737Smmusante 	if (verbose) {
29504737Smmusante 		(void) printf("%s: *", gettext("Reading ZFS config"));
29514737Smmusante 		(void) fflush(stdout);
29524737Smmusante 	}
29533126Sahl 
29543126Sahl 	(void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
29553126Sahl 
29563126Sahl 	*dslist = cb.cb_handles;
29571356Seschrock 	*count = cb.cb_used;
29584737Smmusante 
29594737Smmusante 	if (verbose) {
29604737Smmusante 		(void) printf("\b%s\n", gettext("done."));
29614737Smmusante 	}
29621356Seschrock }
29631356Seschrock 
29641356Seschrock static int
29653126Sahl dataset_cmp(const void *a, const void *b)
29661356Seschrock {
29671356Seschrock 	zfs_handle_t **za = (zfs_handle_t **)a;
29681356Seschrock 	zfs_handle_t **zb = (zfs_handle_t **)b;
29691356Seschrock 	char mounta[MAXPATHLEN];
29701356Seschrock 	char mountb[MAXPATHLEN];
29713126Sahl 	boolean_t gota, gotb;
29723126Sahl 
29733126Sahl 	if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
29743126Sahl 		verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
29753126Sahl 		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
29763126Sahl 	if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
29773126Sahl 		verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
29783126Sahl 		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
29793126Sahl 
29803126Sahl 	if (gota && gotb)
29813126Sahl 		return (strcmp(mounta, mountb));
29823126Sahl 
29833126Sahl 	if (gota)
29843126Sahl 		return (-1);
29853126Sahl 	if (gotb)
29863126Sahl 		return (1);
29873126Sahl 
29883126Sahl 	return (strcmp(zfs_get_name(a), zfs_get_name(b)));
29891356Seschrock }
2990789Sahrens 
2991789Sahrens /*
2992789Sahrens  * Generic callback for sharing or mounting filesystems.  Because the code is so
2993789Sahrens  * similar, we have a common function with an extra parameter to determine which
2994789Sahrens  * mode we are using.
2995789Sahrens  */
2996789Sahrens #define	OP_SHARE	0x1
2997789Sahrens #define	OP_MOUNT	0x2
2998789Sahrens 
2999789Sahrens /*
30003126Sahl  * Share or mount a dataset.
3001789Sahrens  */
3002789Sahrens static int
30035331Samw share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
30045331Samw     boolean_t explicit, const char *options)
3005789Sahrens {
3006789Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
3007789Sahrens 	char shareopts[ZFS_MAXPROPLEN];
30085331Samw 	char smbshareopts[ZFS_MAXPROPLEN];
30093126Sahl 	const char *cmdname = op == OP_SHARE ? "share" : "mount";
3010789Sahrens 	struct mnttab mnt;
30112676Seschrock 	uint64_t zoned, canmount;
30123126Sahl 	zfs_type_t type = zfs_get_type(zhp);
30135331Samw 	boolean_t shared_nfs, shared_smb;
30143126Sahl 
30153126Sahl 	assert(type & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME));
30163126Sahl 
30173126Sahl 	if (type == ZFS_TYPE_FILESYSTEM) {
30183126Sahl 		/*
30193126Sahl 		 * Check to make sure we can mount/share this dataset.  If we
30203126Sahl 		 * are in the global zone and the filesystem is exported to a
30213126Sahl 		 * local zone, or if we are in a local zone and the
30223126Sahl 		 * filesystem is not exported, then it is an error.
30233126Sahl 		 */
30243126Sahl 		zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
30253126Sahl 
30263126Sahl 		if (zoned && getzoneid() == GLOBAL_ZONEID) {
30273126Sahl 			if (!explicit)
30283126Sahl 				return (0);
30293126Sahl 
30303126Sahl 			(void) fprintf(stderr, gettext("cannot %s '%s': "
30313126Sahl 			    "dataset is exported to a local zone\n"), cmdname,
30323126Sahl 			    zfs_get_name(zhp));
30333126Sahl 			return (1);
30343126Sahl 
30353126Sahl 		} else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
30363126Sahl 			if (!explicit)
30373126Sahl 				return (0);
30383126Sahl 
30393126Sahl 			(void) fprintf(stderr, gettext("cannot %s '%s': "
30403126Sahl 			    "permission denied\n"), cmdname,
30413126Sahl 			    zfs_get_name(zhp));
30423126Sahl 			return (1);
30433126Sahl 		}
30443126Sahl 
30453126Sahl 		/*
30463126Sahl 		 * Ignore any filesystems which don't apply to us. This
30473126Sahl 		 * includes those with a legacy mountpoint, or those with
30483126Sahl 		 * legacy share options.
30493126Sahl 		 */
30503126Sahl 		verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
30513126Sahl 		    sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
30523126Sahl 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
30533126Sahl 		    sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
30545331Samw 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
30555331Samw 		    sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
30563126Sahl 
30575331Samw 		if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
30585331Samw 		    strcmp(smbshareopts, "off") == 0) {
30593126Sahl 			if (!explicit)
3060789Sahrens 				return (0);
3061789Sahrens 
3062789Sahrens 			(void) fprintf(stderr, gettext("cannot share '%s': "
3063789Sahrens 			    "legacy share\n"), zfs_get_name(zhp));
3064789Sahrens 			(void) fprintf(stderr, gettext("use share(1M) to "
30658269SMark.Musante@Sun.COM 			    "share this filesystem, or set "
30668269SMark.Musante@Sun.COM 			    "sharenfs property on\n"));
3067789Sahrens 			return (1);
3068789Sahrens 		}
30693126Sahl 
30703126Sahl 		/*
30713126Sahl 		 * We cannot share or mount legacy filesystems. If the
30723126Sahl 		 * shareopts is non-legacy but the mountpoint is legacy, we
30733126Sahl 		 * treat it as a legacy share.
30743126Sahl 		 */
30753126Sahl 		if (strcmp(mountpoint, "legacy") == 0) {
30763126Sahl 			if (!explicit)
30773126Sahl 				return (0);
30783126Sahl 
30793126Sahl 			(void) fprintf(stderr, gettext("cannot %s '%s': "
30803126Sahl 			    "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
30815367Sahrens 			(void) fprintf(stderr, gettext("use %s(1M) to "
30825367Sahrens 			    "%s this filesystem\n"), cmdname, cmdname);
30833126Sahl 			return (1);
30843126Sahl 		}
30853126Sahl 
30863126Sahl 		if (strcmp(mountpoint, "none") == 0) {
30873126Sahl 			if (!explicit)
30883126Sahl 				return (0);
30893126Sahl 
30903126Sahl 			(void) fprintf(stderr, gettext("cannot %s '%s': no "
30913126Sahl 			    "mountpoint set\n"), cmdname, zfs_get_name(zhp));
30923126Sahl 			return (1);
30933126Sahl 		}
30943126Sahl 
30956168Shs24103 		/*
30966168Shs24103 		 * canmount	explicit	outcome
30976168Shs24103 		 * on		no		pass through
30986168Shs24103 		 * on		yes		pass through
30996168Shs24103 		 * off		no		return 0
31006168Shs24103 		 * off		yes		display error, return 1
31016168Shs24103 		 * noauto	no		return 0
31026168Shs24103 		 * noauto	yes		pass through
31036168Shs24103 		 */
31048269SMark.Musante@Sun.COM 		canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
31056168Shs24103 		if (canmount == ZFS_CANMOUNT_OFF) {
31063126Sahl 			if (!explicit)
31073126Sahl 				return (0);
31083126Sahl 
31093126Sahl 			(void) fprintf(stderr, gettext("cannot %s '%s': "
31103126Sahl 			    "'canmount' property is set to 'off'\n"), cmdname,
31113126Sahl 			    zfs_get_name(zhp));
31123126Sahl 			return (1);
31136168Shs24103 		} else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
31146168Shs24103 			return (0);
31153126Sahl 		}
31163126Sahl 
31173126Sahl 		/*
31183126Sahl 		 * At this point, we have verified that the mountpoint and/or
31193126Sahl 		 * shareopts are appropriate for auto management. If the
31203126Sahl 		 * filesystem is already mounted or shared, return (failing
31213126Sahl 		 * for explicit requests); otherwise mount or share the
31223126Sahl 		 * filesystem.
31233126Sahl 		 */
31243126Sahl 		switch (op) {
31253126Sahl 		case OP_SHARE:
31265331Samw 
31275331Samw 			shared_nfs = zfs_is_shared_nfs(zhp, NULL);
31285331Samw 			shared_smb = zfs_is_shared_smb(zhp, NULL);
31295331Samw 
31305331Samw 			if (shared_nfs && shared_smb ||
31315331Samw 			    (shared_nfs && strcmp(shareopts, "on") == 0 &&
31325331Samw 			    strcmp(smbshareopts, "off") == 0) ||
31335331Samw 			    (shared_smb && strcmp(smbshareopts, "on") == 0 &&
31345331Samw 			    strcmp(shareopts, "off") == 0)) {
31353126Sahl 				if (!explicit)
31363126Sahl 					return (0);
31373126Sahl 
3138789Sahrens 				(void) fprintf(stderr, gettext("cannot share "
3139789Sahrens 				    "'%s': filesystem already shared\n"),
3140789Sahrens 				    zfs_get_name(zhp));
3141789Sahrens 				return (1);
3142789Sahrens 			}
31433126Sahl 
31443126Sahl 			if (!zfs_is_mounted(zhp, NULL) &&
31453126Sahl 			    zfs_mount(zhp, NULL, 0) != 0)
31463126Sahl 				return (1);
31473126Sahl 
31485331Samw 			if (protocol == NULL) {
31495331Samw 				if (zfs_shareall(zhp) != 0)
31505331Samw 					return (1);
31515331Samw 			} else if (strcmp(protocol, "nfs") == 0) {
31525331Samw 				if (zfs_share_nfs(zhp))
31535331Samw 					return (1);
31545331Samw 			} else if (strcmp(protocol, "smb") == 0) {
31555331Samw 				if (zfs_share_smb(zhp))
31565331Samw 					return (1);
31575331Samw 			} else {
31585331Samw 				(void) fprintf(stderr, gettext("cannot share "
31595331Samw 				    "'%s': invalid share type '%s' "
31605331Samw 				    "specified\n"),
31615331Samw 				    zfs_get_name(zhp), protocol);
31623126Sahl 				return (1);
31635331Samw 			}
31645331Samw 
31653126Sahl 			break;
31663126Sahl 
31673126Sahl 		case OP_MOUNT:
31683126Sahl 			if (options == NULL)
31693126Sahl 				mnt.mnt_mntopts = "";
31703126Sahl 			else
31713126Sahl 				mnt.mnt_mntopts = (char *)options;
31723126Sahl 
31733126Sahl 			if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
31743126Sahl 			    zfs_is_mounted(zhp, NULL)) {
31753126Sahl 				if (!explicit)
31763126Sahl 					return (0);
31773126Sahl 
3178789Sahrens 				(void) fprintf(stderr, gettext("cannot mount "
3179789Sahrens 				    "'%s': filesystem already mounted\n"),
3180789Sahrens 				    zfs_get_name(zhp));
3181789Sahrens 				return (1);
3182789Sahrens 			}
31833126Sahl 
31843126Sahl 			if (zfs_mount(zhp, options, flags) != 0)
31853126Sahl 				return (1);
31863126Sahl 			break;
3187789Sahrens 		}
318811876SJames.Dunham@Sun.COM 	} else
31893126Sahl 		assert(op == OP_SHARE);
31903126Sahl 
3191789Sahrens 	return (0);
3192789Sahrens }
3193789Sahrens 
31944737Smmusante /*
31954737Smmusante  * Reports progress in the form "(current/total)".  Not thread-safe.
31964737Smmusante  */
31974737Smmusante static void
31984737Smmusante report_mount_progress(int current, int total)
31994737Smmusante {
32004737Smmusante 	static int len;
32014737Smmusante 	static char *reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
32024737Smmusante 	    "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
32034737Smmusante 	static time_t last_progress_time;
32044737Smmusante 	time_t now = time(NULL);
32054737Smmusante 
32064737Smmusante 	/* report 1..n instead of 0..n-1 */
32074737Smmusante 	++current;
32084737Smmusante 
32094737Smmusante 	/* display header if we're here for the first time */
32104737Smmusante 	if (current == 1) {
32114737Smmusante 		(void) printf(gettext("Mounting ZFS filesystems: "));
32124737Smmusante 		len = 0;
32135367Sahrens 	} else if (current != total && last_progress_time + MOUNT_TIME >= now) {
32145367Sahrens 		/* too soon to report again */
32155367Sahrens 		return;
32165367Sahrens 	}
32174737Smmusante 
32184737Smmusante 	last_progress_time = now;
32194737Smmusante 
32204737Smmusante 	/* back up to prepare for overwriting */
32214737Smmusante 	if (len)
32224737Smmusante 		(void) printf("%*.*s", len, len, reverse);
32234737Smmusante 
32244737Smmusante 	/* We put a newline at the end if this is the last one.  */
32254737Smmusante 	len = printf("(%d/%d)%s", current, total, current == total ? "\n" : "");
32264737Smmusante 	(void) fflush(stdout);
32274737Smmusante }
32284737Smmusante 
32296289Smmusante static void
32306289Smmusante append_options(char *mntopts, char *newopts)
32316289Smmusante {
32326289Smmusante 	int len = strlen(mntopts);
32336289Smmusante 
32346289Smmusante 	/* original length plus new string to append plus 1 for the comma */
32356289Smmusante 	if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
32366289Smmusante 		(void) fprintf(stderr, gettext("the opts argument for "
32376289Smmusante 		    "'%c' option is too long (more than %d chars)\n"),
32386289Smmusante 		    "-o", MNT_LINE_MAX);
32396289Smmusante 		usage(B_FALSE);
32406289Smmusante 	}
32416289Smmusante 
32426289Smmusante 	if (*mntopts)
32436289Smmusante 		mntopts[len++] = ',';
32446289Smmusante 
32456289Smmusante 	(void) strcpy(&mntopts[len], newopts);
32466289Smmusante }
32476289Smmusante 
3248789Sahrens static int
32493126Sahl share_mount(int op, int argc, char **argv)
3250789Sahrens {
3251789Sahrens 	int do_all = 0;
32524737Smmusante 	boolean_t verbose = B_FALSE;
32532372Slling 	int c, ret = 0;
32546289Smmusante 	char *options = NULL;
32553126Sahl 	int types, flags = 0;
3256789Sahrens 
3257789Sahrens 	/* check options */
32584737Smmusante 	while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
3259789Sahrens 	    != -1) {
3260789Sahrens 		switch (c) {
3261789Sahrens 		case 'a':
3262789Sahrens 			do_all = 1;
3263789Sahrens 			break;
32644737Smmusante 		case 'v':
32654737Smmusante 			verbose = B_TRUE;
32664737Smmusante 			break;
3267789Sahrens 		case 'o':
32686289Smmusante 			if (*optarg == '\0') {
32696289Smmusante 				(void) fprintf(stderr, gettext("empty mount "
32706289Smmusante 				    "options (-o) specified\n"));
32716289Smmusante 				usage(B_FALSE);
32724717Srm160521 			}
32736289Smmusante 
32746289Smmusante 			if (options == NULL)
32756289Smmusante 				options = safe_malloc(MNT_LINE_MAX + 1);
32766289Smmusante 
32776289Smmusante 			/* option validation is done later */
32786289Smmusante 			append_options(options, optarg);
3279789Sahrens 			break;
32804717Srm160521 
3281789Sahrens 		case 'O':
32823126Sahl 			flags |= MS_OVERLAY;
3283789Sahrens 			break;
3284789Sahrens 		case ':':
3285789Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
3286789Sahrens 			    "'%c' option\n"), optopt);
32872082Seschrock 			usage(B_FALSE);
3288789Sahrens 			break;
3289789Sahrens 		case '?':
3290789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3291789Sahrens 			    optopt);
32922082Seschrock 			usage(B_FALSE);
3293789Sahrens 		}
3294789Sahrens 	}
3295789Sahrens 
3296789Sahrens 	argc -= optind;
3297789Sahrens 	argv += optind;
3298789Sahrens 
3299789Sahrens 	/* check number of arguments */
3300789Sahrens 	if (do_all) {
33013126Sahl 		zfs_handle_t **dslist = NULL;
33021356Seschrock 		size_t i, count = 0;
33035331Samw 		char *protocol = NULL;
33041356Seschrock 
33053126Sahl 		if (op == OP_MOUNT) {
33063126Sahl 			types = ZFS_TYPE_FILESYSTEM;
33073126Sahl 		} else if (argc > 0) {
33085331Samw 			if (strcmp(argv[0], "nfs") == 0 ||
33095331Samw 			    strcmp(argv[0], "smb") == 0) {
33103126Sahl 				types = ZFS_TYPE_FILESYSTEM;
33113126Sahl 			} else {
33123126Sahl 				(void) fprintf(stderr, gettext("share type "
331311876SJames.Dunham@Sun.COM 				    "must be 'nfs' or 'smb'\n"));
33143126Sahl 				usage(B_FALSE);
33153126Sahl 			}
33165331Samw 			protocol = argv[0];
33173126Sahl 			argc--;
33183126Sahl 			argv++;
33193126Sahl 		} else {
33203126Sahl 			types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
33213126Sahl 		}
33223126Sahl 
3323789Sahrens 		if (argc != 0) {
3324789Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
33252082Seschrock 			usage(B_FALSE);
3326789Sahrens 		}
3327789Sahrens 
33284737Smmusante 		get_all_datasets(types, &dslist, &count, verbose);
33291356Seschrock 
33301356Seschrock 		if (count == 0)
33311356Seschrock 			return (0);
33321356Seschrock 
33333126Sahl 		qsort(dslist, count, sizeof (void *), dataset_cmp);
33341356Seschrock 
33351356Seschrock 		for (i = 0; i < count; i++) {
33364737Smmusante 			if (verbose)
33374737Smmusante 				report_mount_progress(i, count);
33384737Smmusante 
33395331Samw 			if (share_mount_one(dslist[i], op, flags, protocol,
33405331Samw 			    B_FALSE, options) != 0)
33412369Slling 				ret = 1;
33423126Sahl 			zfs_close(dslist[i]);
33431356Seschrock 		}
33441356Seschrock 
33453126Sahl 		free(dslist);
3346789Sahrens 	} else if (argc == 0) {
3347789Sahrens 		struct mnttab entry;
3348789Sahrens 
33494717Srm160521 		if ((op == OP_SHARE) || (options != NULL)) {
3350789Sahrens 			(void) fprintf(stderr, gettext("missing filesystem "
33514717Srm160521 			    "argument (specify -a for all)\n"));
33522082Seschrock 			usage(B_FALSE);
3353789Sahrens 		}
3354789Sahrens 
3355789Sahrens 		/*
3356789Sahrens 		 * When mount is given no arguments, go through /etc/mnttab and
3357789Sahrens 		 * display any active ZFS mounts.  We hide any snapshots, since
3358789Sahrens 		 * they are controlled automatically.
3359789Sahrens 		 */
3360789Sahrens 		rewind(mnttab_file);
3361789Sahrens 		while (getmntent(mnttab_file, &entry) == 0) {
3362789Sahrens 			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
3363789Sahrens 			    strchr(entry.mnt_special, '@') != NULL)
3364789Sahrens 				continue;
3365789Sahrens 
3366789Sahrens 			(void) printf("%-30s  %s\n", entry.mnt_special,
3367789Sahrens 			    entry.mnt_mountp);
3368789Sahrens 		}
3369789Sahrens 
3370789Sahrens 	} else {
3371789Sahrens 		zfs_handle_t *zhp;
3372789Sahrens 
33733126Sahl 		types = ZFS_TYPE_FILESYSTEM;
33743126Sahl 		if (op == OP_SHARE)
33753126Sahl 			types |= ZFS_TYPE_VOLUME;
33763126Sahl 
3377789Sahrens 		if (argc > 1) {
3378789Sahrens 			(void) fprintf(stderr,
3379789Sahrens 			    gettext("too many arguments\n"));
33802082Seschrock 			usage(B_FALSE);
3381789Sahrens 		}
3382789Sahrens 
33833126Sahl 		if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) {
3384789Sahrens 			ret = 1;
33853126Sahl 		} else {
33865331Samw 			ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
33873126Sahl 			    options);
3388789Sahrens 			zfs_close(zhp);
3389789Sahrens 		}
3390789Sahrens 	}
3391789Sahrens 
3392789Sahrens 	return (ret);
3393789Sahrens }
3394789Sahrens 
3395789Sahrens /*
339611876SJames.Dunham@Sun.COM  * zfs mount -a [nfs]
3397789Sahrens  * zfs mount filesystem
3398789Sahrens  *
3399789Sahrens  * Mount all filesystems, or mount the given filesystem.
3400789Sahrens  */
3401789Sahrens static int
3402789Sahrens zfs_do_mount(int argc, char **argv)
3403789Sahrens {
34043126Sahl 	return (share_mount(OP_MOUNT, argc, argv));
3405789Sahrens }
3406789Sahrens 
3407789Sahrens /*
340811876SJames.Dunham@Sun.COM  * zfs share -a [nfs | smb]
3409789Sahrens  * zfs share filesystem
3410789Sahrens  *
3411789Sahrens  * Share all filesystems, or share the given filesystem.
3412789Sahrens  */
3413789Sahrens static int
3414789Sahrens zfs_do_share(int argc, char **argv)
3415789Sahrens {
34163126Sahl 	return (share_mount(OP_SHARE, argc, argv));
3417789Sahrens }
3418789Sahrens 
3419789Sahrens typedef struct unshare_unmount_node {
3420789Sahrens 	zfs_handle_t	*un_zhp;
3421789Sahrens 	char		*un_mountp;
3422789Sahrens 	uu_avl_node_t	un_avlnode;
3423789Sahrens } unshare_unmount_node_t;
3424789Sahrens 
3425789Sahrens /* ARGSUSED */
3426789Sahrens static int
3427789Sahrens unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
3428789Sahrens {
3429789Sahrens 	const unshare_unmount_node_t *l = larg;
3430789Sahrens 	const unshare_unmount_node_t *r = rarg;
3431789Sahrens 
3432789Sahrens 	return (strcmp(l->un_mountp, r->un_mountp));
3433789Sahrens }
3434789Sahrens 
3435789Sahrens /*
3436789Sahrens  * Convenience routine used by zfs_do_umount() and manual_unmount().  Given an
3437789Sahrens  * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
3438789Sahrens  * and unmount it appropriately.
3439789Sahrens  */
3440789Sahrens static int
34413126Sahl unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
3442789Sahrens {
3443789Sahrens 	zfs_handle_t *zhp;
3444789Sahrens 	int ret;
3445789Sahrens 	struct stat64 statbuf;
3446789Sahrens 	struct extmnttab entry;
34473126Sahl 	const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
34486289Smmusante 	ino_t path_inode;
3449789Sahrens 
3450789Sahrens 	/*
3451789Sahrens 	 * Search for the path in /etc/mnttab.  Rather than looking for the
3452789Sahrens 	 * specific path, which can be fooled by non-standard paths (i.e. ".."
3453789Sahrens 	 * or "//"), we stat() the path and search for the corresponding
3454789Sahrens 	 * (major,minor) device pair.
3455789Sahrens 	 */
3456789Sahrens 	if (stat64(path, &statbuf) != 0) {
3457789Sahrens 		(void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3458789Sahrens 		    cmdname, path, strerror(errno));
3459789Sahrens 		return (1);
3460789Sahrens 	}
34616289Smmusante 	path_inode = statbuf.st_ino;
3462789Sahrens 
3463789Sahrens 	/*
3464789Sahrens 	 * Search for the given (major,minor) pair in the mount table.
3465789Sahrens 	 */
3466789Sahrens 	rewind(mnttab_file);
3467789Sahrens 	while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
3468789Sahrens 		if (entry.mnt_major == major(statbuf.st_dev) &&
3469789Sahrens 		    entry.mnt_minor == minor(statbuf.st_dev))
3470789Sahrens 			break;
3471789Sahrens 	}
3472789Sahrens 	if (ret != 0) {
34735904Stimh 		if (op == OP_SHARE) {
34745904Stimh 			(void) fprintf(stderr, gettext("cannot %s '%s': not "
34755904Stimh 			    "currently mounted\n"), cmdname, path);
34765904Stimh 			return (1);
34775904Stimh 		}
34785904Stimh 		(void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
34795904Stimh 		    path);
34805904Stimh 		if ((ret = umount2(path, flags)) != 0)
34815904Stimh 			(void) fprintf(stderr, gettext("%s: %s\n"), path,
34825904Stimh 			    strerror(errno));
34835904Stimh 		return (ret != 0);
3484789Sahrens 	}
3485789Sahrens 
3486789Sahrens 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
3487789Sahrens 		(void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
3488789Sahrens 		    "filesystem\n"), cmdname, path);
3489789Sahrens 		return (1);
3490789Sahrens 	}
3491789Sahrens 
34922082Seschrock 	if ((zhp = zfs_open(g_zfs, entry.mnt_special,
34932082Seschrock 	    ZFS_TYPE_FILESYSTEM)) == NULL)
3494789Sahrens 		return (1);
3495789Sahrens 
34966289Smmusante 	ret = 1;
34977386SSachin.Gaikwad@Sun.COM 	if (stat64(entry.mnt_mountp, &statbuf) != 0) {
34987386SSachin.Gaikwad@Sun.COM 		(void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
34997386SSachin.Gaikwad@Sun.COM 		    cmdname, path, strerror(errno));
35007386SSachin.Gaikwad@Sun.COM 		goto out;
35017386SSachin.Gaikwad@Sun.COM 	} else if (statbuf.st_ino != path_inode) {
35027386SSachin.Gaikwad@Sun.COM 		(void) fprintf(stderr, gettext("cannot "
35037386SSachin.Gaikwad@Sun.COM 		    "%s '%s': not a mountpoint\n"), cmdname, path);
35047386SSachin.Gaikwad@Sun.COM 		goto out;
35057386SSachin.Gaikwad@Sun.COM 	}
35067386SSachin.Gaikwad@Sun.COM 
35073126Sahl 	if (op == OP_SHARE) {
35086289Smmusante 		char nfs_mnt_prop[ZFS_MAXPROPLEN];
35096289Smmusante 		char smbshare_prop[ZFS_MAXPROPLEN];
35106289Smmusante 
35116289Smmusante 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
35126289Smmusante 		    sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
35136289Smmusante 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
35146289Smmusante 		    sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
35156289Smmusante 
35165331Samw 		if (strcmp(nfs_mnt_prop, "off") == 0 &&
35175331Samw 		    strcmp(smbshare_prop, "off") == 0) {
3518789Sahrens 			(void) fprintf(stderr, gettext("cannot unshare "
3519789Sahrens 			    "'%s': legacy share\n"), path);
3520789Sahrens 			(void) fprintf(stderr, gettext("use "
3521789Sahrens 			    "unshare(1M) to unshare this filesystem\n"));
35225331Samw 		} else if (!zfs_is_shared(zhp)) {
3523789Sahrens 			(void) fprintf(stderr, gettext("cannot unshare '%s': "
3524789Sahrens 			    "not currently shared\n"), path);
3525789Sahrens 		} else {
35265331Samw 			ret = zfs_unshareall_bypath(zhp, path);
3527789Sahrens 		}
3528789Sahrens 	} else {
35296289Smmusante 		char mtpt_prop[ZFS_MAXPROPLEN];
35306289Smmusante 
35316289Smmusante 		verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
35326289Smmusante 		    sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
35336289Smmusante 
35347386SSachin.Gaikwad@Sun.COM 		if (is_manual) {
35351264Slling 			ret = zfs_unmount(zhp, NULL, flags);
35366289Smmusante 		} else if (strcmp(mtpt_prop, "legacy") == 0) {
35371264Slling 			(void) fprintf(stderr, gettext("cannot unmount "
35381264Slling 			    "'%s': legacy mountpoint\n"),
35391264Slling 			    zfs_get_name(zhp));
35401264Slling 			(void) fprintf(stderr, gettext("use umount(1M) "
35411264Slling 			    "to unmount this filesystem\n"));
3542789Sahrens 		} else {
3543789Sahrens 			ret = zfs_unmountall(zhp, flags);
3544789Sahrens 		}
3545789Sahrens 	}
3546789Sahrens 
35477386SSachin.Gaikwad@Sun.COM out:
3548789Sahrens 	zfs_close(zhp);
3549789Sahrens 
3550789Sahrens 	return (ret != 0);
3551789Sahrens }
3552789Sahrens 
3553789Sahrens /*
3554789Sahrens  * Generic callback for unsharing or unmounting a filesystem.
3555789Sahrens  */
3556789Sahrens static int
35573126Sahl unshare_unmount(int op, int argc, char **argv)
3558789Sahrens {
3559789Sahrens 	int do_all = 0;
3560789Sahrens 	int flags = 0;
3561789Sahrens 	int ret = 0;
35623126Sahl 	int types, c;
3563789Sahrens 	zfs_handle_t *zhp;
356411876SJames.Dunham@Sun.COM 	char nfs_mnt_prop[ZFS_MAXPROPLEN];
35655331Samw 	char sharesmb[ZFS_MAXPROPLEN];
3566789Sahrens 
3567789Sahrens 	/* check options */
35683126Sahl 	while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
3569789Sahrens 		switch (c) {
3570789Sahrens 		case 'a':
3571789Sahrens 			do_all = 1;
3572789Sahrens 			break;
3573789Sahrens 		case 'f':
3574789Sahrens 			flags = MS_FORCE;
3575789Sahrens 			break;
3576789Sahrens 		case '?':
3577789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3578789Sahrens 			    optopt);
35792082Seschrock 			usage(B_FALSE);
3580789Sahrens 		}
3581789Sahrens 	}
3582789Sahrens 
3583789Sahrens 	argc -= optind;
3584789Sahrens 	argv += optind;
3585789Sahrens 
3586789Sahrens 	if (do_all) {
3587789Sahrens 		/*
3588789Sahrens 		 * We could make use of zfs_for_each() to walk all datasets in
3589789Sahrens 		 * the system, but this would be very inefficient, especially
3590789Sahrens 		 * since we would have to linearly search /etc/mnttab for each
3591789Sahrens 		 * one.  Instead, do one pass through /etc/mnttab looking for
3592789Sahrens 		 * zfs entries and call zfs_unmount() for each one.
3593789Sahrens 		 *
3594789Sahrens 		 * Things get a little tricky if the administrator has created
3595789Sahrens 		 * mountpoints beneath other ZFS filesystems.  In this case, we
3596789Sahrens 		 * have to unmount the deepest filesystems first.  To accomplish
3597789Sahrens 		 * this, we place all the mountpoints in an AVL tree sorted by
3598789Sahrens 		 * the special type (dataset name), and walk the result in
3599789Sahrens 		 * reverse to make sure to get any snapshots first.
3600789Sahrens 		 */
3601789Sahrens 		struct mnttab entry;
3602789Sahrens 		uu_avl_pool_t *pool;
3603789Sahrens 		uu_avl_t *tree;
3604789Sahrens 		unshare_unmount_node_t *node;
3605789Sahrens 		uu_avl_index_t idx;
3606789Sahrens 		uu_avl_walk_t *walk;
3607789Sahrens 
36083126Sahl 		if (argc != 0) {
36093126Sahl 			(void) fprintf(stderr, gettext("too many arguments\n"));
36103126Sahl 			usage(B_FALSE);
36113126Sahl 		}
36123126Sahl 
3613789Sahrens 		if ((pool = uu_avl_pool_create("unmount_pool",
3614789Sahrens 		    sizeof (unshare_unmount_node_t),
3615789Sahrens 		    offsetof(unshare_unmount_node_t, un_avlnode),
3616789Sahrens 		    unshare_unmount_compare,
3617789Sahrens 		    UU_DEFAULT)) == NULL) {
3618789Sahrens 			(void) fprintf(stderr, gettext("internal error: "
3619789Sahrens 			    "out of memory\n"));
3620789Sahrens 			exit(1);
3621789Sahrens 		}
3622789Sahrens 
3623789Sahrens 		if ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL) {
3624789Sahrens 			(void) fprintf(stderr, gettext("internal error: "
3625789Sahrens 			    "out of memory\n"));
3626789Sahrens 			exit(1);
3627789Sahrens 		}
3628789Sahrens 
3629789Sahrens 		rewind(mnttab_file);
3630789Sahrens 		while (getmntent(mnttab_file, &entry) == 0) {
3631789Sahrens 
3632789Sahrens 			/* ignore non-ZFS entries */
3633789Sahrens 			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
3634789Sahrens 				continue;
3635789Sahrens 
3636789Sahrens 			/* ignore snapshots */
3637789Sahrens 			if (strchr(entry.mnt_special, '@') != NULL)
3638789Sahrens 				continue;
3639789Sahrens 
36402082Seschrock 			if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3641789Sahrens 			    ZFS_TYPE_FILESYSTEM)) == NULL) {
3642789Sahrens 				ret = 1;
3643789Sahrens 				continue;
3644789Sahrens 			}
3645789Sahrens 
36465331Samw 			switch (op) {
36475331Samw 			case OP_SHARE:
36485331Samw 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
364911876SJames.Dunham@Sun.COM 				    nfs_mnt_prop,
365011876SJames.Dunham@Sun.COM 				    sizeof (nfs_mnt_prop),
36515331Samw 				    NULL, NULL, 0, B_FALSE) == 0);
365211876SJames.Dunham@Sun.COM 				if (strcmp(nfs_mnt_prop, "off") != 0)
36535331Samw 					break;
36545331Samw 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
365511876SJames.Dunham@Sun.COM 				    nfs_mnt_prop,
365611876SJames.Dunham@Sun.COM 				    sizeof (nfs_mnt_prop),
36575331Samw 				    NULL, NULL, 0, B_FALSE) == 0);
365811876SJames.Dunham@Sun.COM 				if (strcmp(nfs_mnt_prop, "off") == 0)
36595331Samw 					continue;
36605331Samw 				break;
36615331Samw 			case OP_MOUNT:
36625331Samw 				/* Ignore legacy mounts */
36635331Samw 				verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
366411876SJames.Dunham@Sun.COM 				    nfs_mnt_prop,
366511876SJames.Dunham@Sun.COM 				    sizeof (nfs_mnt_prop),
36665331Samw 				    NULL, NULL, 0, B_FALSE) == 0);
366711876SJames.Dunham@Sun.COM 				if (strcmp(nfs_mnt_prop, "legacy") == 0)
36685331Samw 					continue;
36696168Shs24103 				/* Ignore canmount=noauto mounts */
36706168Shs24103 				if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
36716168Shs24103 				    ZFS_CANMOUNT_NOAUTO)
36726168Shs24103 					continue;
36735331Samw 			default:
36745331Samw 				break;
3675789Sahrens 			}
3676789Sahrens 
3677789Sahrens 			node = safe_malloc(sizeof (unshare_unmount_node_t));
3678789Sahrens 			node->un_zhp = zhp;
3679789Sahrens 
3680789Sahrens 			if ((node->un_mountp = strdup(entry.mnt_mountp)) ==
3681789Sahrens 			    NULL) {
3682789Sahrens 				(void) fprintf(stderr, gettext("internal error:"
3683789Sahrens 				    " out of memory\n"));
3684789Sahrens 				exit(1);
3685789Sahrens 			}
3686789Sahrens 
3687789Sahrens 			uu_avl_node_init(node, &node->un_avlnode, pool);
3688789Sahrens 
3689789Sahrens 			if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
3690789Sahrens 				uu_avl_insert(tree, node, idx);
3691789Sahrens 			} else {
3692789Sahrens 				zfs_close(node->un_zhp);
3693789Sahrens 				free(node->un_mountp);
3694789Sahrens 				free(node);
3695789Sahrens 			}
3696789Sahrens 		}
3697789Sahrens 
3698789Sahrens 		/*
3699789Sahrens 		 * Walk the AVL tree in reverse, unmounting each filesystem and
3700789Sahrens 		 * removing it from the AVL tree in the process.
3701789Sahrens 		 */
3702789Sahrens 		if ((walk = uu_avl_walk_start(tree,
3703789Sahrens 		    UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) {
3704789Sahrens 			(void) fprintf(stderr,
3705789Sahrens 			    gettext("internal error: out of memory"));
3706789Sahrens 			exit(1);
3707789Sahrens 		}
3708789Sahrens 
3709789Sahrens 		while ((node = uu_avl_walk_next(walk)) != NULL) {
3710789Sahrens 			uu_avl_remove(tree, node);
3711789Sahrens 
37123126Sahl 			switch (op) {
3713789Sahrens 			case OP_SHARE:
37145331Samw 				if (zfs_unshareall_bypath(node->un_zhp,
3715789Sahrens 				    node->un_mountp) != 0)
3716789Sahrens 					ret = 1;
3717789Sahrens 				break;
3718789Sahrens 
3719789Sahrens 			case OP_MOUNT:
3720789Sahrens 				if (zfs_unmount(node->un_zhp,
3721789Sahrens 				    node->un_mountp, flags) != 0)
3722789Sahrens 					ret = 1;
3723789Sahrens 				break;
3724789Sahrens 			}
3725789Sahrens 
3726789Sahrens 			zfs_close(node->un_zhp);
3727789Sahrens 			free(node->un_mountp);
3728789Sahrens 			free(node);
3729789Sahrens 		}
3730789Sahrens 
3731789Sahrens 		uu_avl_walk_end(walk);
3732789Sahrens 		uu_avl_destroy(tree);
3733789Sahrens 		uu_avl_pool_destroy(pool);
37343126Sahl 
3735789Sahrens 	} else {
37363126Sahl 		if (argc != 1) {
37373126Sahl 			if (argc == 0)
37383126Sahl 				(void) fprintf(stderr,
37393126Sahl 				    gettext("missing filesystem argument\n"));
37403126Sahl 			else
37413126Sahl 				(void) fprintf(stderr,
37423126Sahl 				    gettext("too many arguments\n"));
37433126Sahl 			usage(B_FALSE);
37443126Sahl 		}
37453126Sahl 
3746789Sahrens 		/*
3747789Sahrens 		 * We have an argument, but it may be a full path or a ZFS
3748789Sahrens 		 * filesystem.  Pass full paths off to unmount_path() (shared by
3749789Sahrens 		 * manual_unmount), otherwise open the filesystem and pass to
3750789Sahrens 		 * zfs_unmount().
3751789Sahrens 		 */
3752789Sahrens 		if (argv[0][0] == '/')
37533126Sahl 			return (unshare_unmount_path(op, argv[0],
37543912Slling 			    flags, B_FALSE));
37552082Seschrock 
37563126Sahl 		types = ZFS_TYPE_FILESYSTEM;
37573126Sahl 		if (op == OP_SHARE)
37583126Sahl 			types |= ZFS_TYPE_VOLUME;
37593126Sahl 
37603126Sahl 		if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3761789Sahrens 			return (1);
3762789Sahrens 
37633126Sahl 		if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
37643126Sahl 			verify(zfs_prop_get(zhp, op == OP_SHARE ?
37655331Samw 			    ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
376611876SJames.Dunham@Sun.COM 			    nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
37675331Samw 			    NULL, 0, B_FALSE) == 0);
37683126Sahl 
37693126Sahl 			switch (op) {
37703126Sahl 			case OP_SHARE:
37715331Samw 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
377211876SJames.Dunham@Sun.COM 				    nfs_mnt_prop,
377311876SJames.Dunham@Sun.COM 				    sizeof (nfs_mnt_prop),
37745331Samw 				    NULL, NULL, 0, B_FALSE) == 0);
37755331Samw 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
37765331Samw 				    sharesmb, sizeof (sharesmb), NULL, NULL,
37775331Samw 				    0, B_FALSE) == 0);
37785331Samw 
377911876SJames.Dunham@Sun.COM 				if (strcmp(nfs_mnt_prop, "off") == 0 &&
37805331Samw 				    strcmp(sharesmb, "off") == 0) {
37813126Sahl 					(void) fprintf(stderr, gettext("cannot "
37823126Sahl 					    "unshare '%s': legacy share\n"),
37833126Sahl 					    zfs_get_name(zhp));
37843126Sahl 					(void) fprintf(stderr, gettext("use "
37853126Sahl 					    "unshare(1M) to unshare this "
37863126Sahl 					    "filesystem\n"));
37873126Sahl 					ret = 1;
37885331Samw 				} else if (!zfs_is_shared(zhp)) {
37893126Sahl 					(void) fprintf(stderr, gettext("cannot "
37903126Sahl 					    "unshare '%s': not currently "
37913126Sahl 					    "shared\n"), zfs_get_name(zhp));
37923126Sahl 					ret = 1;
37935331Samw 				} else if (zfs_unshareall(zhp) != 0) {
37943126Sahl 					ret = 1;
37953126Sahl 				}
37963126Sahl 				break;
37973126Sahl 
37983126Sahl 			case OP_MOUNT:
379911876SJames.Dunham@Sun.COM 				if (strcmp(nfs_mnt_prop, "legacy") == 0) {
38003126Sahl 					(void) fprintf(stderr, gettext("cannot "
38013126Sahl 					    "unmount '%s': legacy "
38023126Sahl 					    "mountpoint\n"), zfs_get_name(zhp));
38033126Sahl 					(void) fprintf(stderr, gettext("use "
38043126Sahl 					    "umount(1M) to unmount this "
38053126Sahl 					    "filesystem\n"));
38063126Sahl 					ret = 1;
38073126Sahl 				} else if (!zfs_is_mounted(zhp, NULL)) {
38083126Sahl 					(void) fprintf(stderr, gettext("cannot "
38093126Sahl 					    "unmount '%s': not currently "
38103126Sahl 					    "mounted\n"),
38113126Sahl 					    zfs_get_name(zhp));
38123126Sahl 					ret = 1;
38133126Sahl 				} else if (zfs_unmountall(zhp, flags) != 0) {
38143126Sahl 					ret = 1;
38153126Sahl 				}
38163126Sahl 				break;
38173126Sahl 			}
3818789Sahrens 		}
3819789Sahrens 
3820789Sahrens 		zfs_close(zhp);
3821789Sahrens 	}
3822789Sahrens 
3823789Sahrens 	return (ret);
3824789Sahrens }
3825789Sahrens 
3826789Sahrens /*
3827789Sahrens  * zfs unmount -a
3828789Sahrens  * zfs unmount filesystem
3829789Sahrens  *
3830789Sahrens  * Unmount all filesystems, or a specific ZFS filesystem.
3831789Sahrens  */
3832789Sahrens static int
3833789Sahrens zfs_do_unmount(int argc, char **argv)
3834789Sahrens {
3835789Sahrens 	return (unshare_unmount(OP_MOUNT, argc, argv));
3836789Sahrens }
3837789Sahrens 
3838789Sahrens /*
3839789Sahrens  * zfs unshare -a
3840789Sahrens  * zfs unshare filesystem
3841789Sahrens  *
3842789Sahrens  * Unshare all filesystems, or a specific ZFS filesystem.
3843789Sahrens  */
3844789Sahrens static int
3845789Sahrens zfs_do_unshare(int argc, char **argv)
3846789Sahrens {
3847789Sahrens 	return (unshare_unmount(OP_SHARE, argc, argv));
3848789Sahrens }
3849789Sahrens 
38509396SMatthew.Ahrens@Sun.COM /* ARGSUSED */
38519396SMatthew.Ahrens@Sun.COM static int
38529396SMatthew.Ahrens@Sun.COM zfs_do_python(int argc, char **argv)
38539396SMatthew.Ahrens@Sun.COM {
38549396SMatthew.Ahrens@Sun.COM 	(void) execv(pypath, argv-1);
38559396SMatthew.Ahrens@Sun.COM 	(void) printf("internal error: %s not found\n", pypath);
38569396SMatthew.Ahrens@Sun.COM 	return (-1);
38579396SMatthew.Ahrens@Sun.COM }
38589396SMatthew.Ahrens@Sun.COM 
3859789Sahrens /*
3860789Sahrens  * Called when invoked as /etc/fs/zfs/mount.  Do the mount if the mountpoint is
3861789Sahrens  * 'legacy'.  Otherwise, complain that use should be using 'zfs mount'.
3862789Sahrens  */
3863789Sahrens static int
3864789Sahrens manual_mount(int argc, char **argv)
3865789Sahrens {
3866789Sahrens 	zfs_handle_t *zhp;
3867789Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
3868789Sahrens 	char mntopts[MNT_LINE_MAX] = { '\0' };
3869789Sahrens 	int ret;
3870789Sahrens 	int c;
3871789Sahrens 	int flags = 0;
3872789Sahrens 	char *dataset, *path;
3873789Sahrens 
3874789Sahrens 	/* check options */
38751544Seschrock 	while ((c = getopt(argc, argv, ":mo:O")) != -1) {
3876789Sahrens 		switch (c) {
3877789Sahrens 		case 'o':
3878789Sahrens 			(void) strlcpy(mntopts, optarg, sizeof (mntopts));
3879789Sahrens 			break;
3880789Sahrens 		case 'O':
3881789Sahrens 			flags |= MS_OVERLAY;
3882789Sahrens 			break;
38831544Seschrock 		case 'm':
38841544Seschrock 			flags |= MS_NOMNTTAB;
38851544Seschrock 			break;
3886789Sahrens 		case ':':
3887789Sahrens 			(void) fprintf(stderr, gettext("missing argument for "
3888789Sahrens 			    "'%c' option\n"), optopt);
38892082Seschrock 			usage(B_FALSE);
3890789Sahrens 			break;
3891789Sahrens 		case '?':
3892789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3893789Sahrens 			    optopt);
3894789Sahrens 			(void) fprintf(stderr, gettext("usage: mount [-o opts] "
3895789Sahrens 			    "<path>\n"));
3896789Sahrens 			return (2);
3897789Sahrens 		}
3898789Sahrens 	}
3899789Sahrens 
3900789Sahrens 	argc -= optind;
3901789Sahrens 	argv += optind;
3902789Sahrens 
3903789Sahrens 	/* check that we only have two arguments */
3904789Sahrens 	if (argc != 2) {
3905789Sahrens 		if (argc == 0)
3906789Sahrens 			(void) fprintf(stderr, gettext("missing dataset "
3907789Sahrens 			    "argument\n"));
3908789Sahrens 		else if (argc == 1)
3909789Sahrens 			(void) fprintf(stderr,
3910789Sahrens 			    gettext("missing mountpoint argument\n"));
3911789Sahrens 		else
3912789Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
3913789Sahrens 		(void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
3914789Sahrens 		return (2);
3915789Sahrens 	}
3916789Sahrens 
3917789Sahrens 	dataset = argv[0];
3918789Sahrens 	path = argv[1];
3919789Sahrens 
3920789Sahrens 	/* try to open the dataset */
39212082Seschrock 	if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
3922789Sahrens 		return (1);
3923789Sahrens 
3924789Sahrens 	(void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
39252082Seschrock 	    sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
3926789Sahrens 
3927789Sahrens 	/* check for legacy mountpoint and complain appropriately */
3928789Sahrens 	ret = 0;
3929789Sahrens 	if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
3930789Sahrens 		if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS,
3931789Sahrens 		    NULL, 0, mntopts, sizeof (mntopts)) != 0) {
3932789Sahrens 			(void) fprintf(stderr, gettext("mount failed: %s\n"),
3933789Sahrens 			    strerror(errno));
3934789Sahrens 			ret = 1;
3935789Sahrens 		}
3936789Sahrens 	} else {
3937789Sahrens 		(void) fprintf(stderr, gettext("filesystem '%s' cannot be "
3938789Sahrens 		    "mounted using 'mount -F zfs'\n"), dataset);
3939789Sahrens 		(void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
3940789Sahrens 		    "instead.\n"), path);
3941789Sahrens 		(void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
3942789Sahrens 		    "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
3943789Sahrens 		(void) fprintf(stderr, gettext("See zfs(1M) for more "
3944789Sahrens 		    "information.\n"));
3945789Sahrens 		ret = 1;
3946789Sahrens 	}
3947789Sahrens 
3948789Sahrens 	return (ret);
3949789Sahrens }
3950789Sahrens 
3951789Sahrens /*
3952789Sahrens  * Called when invoked as /etc/fs/zfs/umount.  Unlike a manual mount, we allow
3953789Sahrens  * unmounts of non-legacy filesystems, as this is the dominant administrative
3954789Sahrens  * interface.
3955789Sahrens  */
3956789Sahrens static int
3957789Sahrens manual_unmount(int argc, char **argv)
3958789Sahrens {
3959789Sahrens 	int flags = 0;
3960789Sahrens 	int c;
3961789Sahrens 
3962789Sahrens 	/* check options */
3963789Sahrens 	while ((c = getopt(argc, argv, "f")) != -1) {
3964789Sahrens 		switch (c) {
3965789Sahrens 		case 'f':
3966789Sahrens 			flags = MS_FORCE;
3967789Sahrens 			break;
3968789Sahrens 		case '?':
3969789Sahrens 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3970789Sahrens 			    optopt);
3971789Sahrens 			(void) fprintf(stderr, gettext("usage: unmount [-f] "
3972789Sahrens 			    "<path>\n"));
3973789Sahrens 			return (2);
3974789Sahrens 		}
3975789Sahrens 	}
3976789Sahrens 
3977789Sahrens 	argc -= optind;
3978789Sahrens 	argv += optind;
3979789Sahrens 
3980789Sahrens 	/* check arguments */
3981789Sahrens 	if (argc != 1) {
3982789Sahrens 		if (argc == 0)
3983789Sahrens 			(void) fprintf(stderr, gettext("missing path "
3984789Sahrens 			    "argument\n"));
3985789Sahrens 		else
3986789Sahrens 			(void) fprintf(stderr, gettext("too many arguments\n"));
3987789Sahrens 		(void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
3988789Sahrens 		return (2);
3989789Sahrens 	}
3990789Sahrens 
39912082Seschrock 	return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
3992789Sahrens }
3993789Sahrens 
3994789Sahrens static int
39954543Smarks find_command_idx(char *command, int *idx)
39964543Smarks {
39974543Smarks 	int i;
39984543Smarks 
39994543Smarks 	for (i = 0; i < NCOMMAND; i++) {
40004543Smarks 		if (command_table[i].name == NULL)
40014543Smarks 			continue;
40024543Smarks 
40034543Smarks 		if (strcmp(command, command_table[i].name) == 0) {
40044543Smarks 			*idx = i;
40054543Smarks 			return (0);
40064543Smarks 		}
40074543Smarks 	}
40084543Smarks 	return (1);
40094543Smarks }
40104543Smarks 
4011789Sahrens int
4012789Sahrens main(int argc, char **argv)
4013789Sahrens {
4014789Sahrens 	int ret;
4015789Sahrens 	int i;
4016789Sahrens 	char *progname;
4017789Sahrens 	char *cmdname;
4018789Sahrens 
4019789Sahrens 	(void) setlocale(LC_ALL, "");
4020789Sahrens 	(void) textdomain(TEXT_DOMAIN);
4021789Sahrens 
4022789Sahrens 	opterr = 0;
4023789Sahrens 
40242082Seschrock 	if ((g_zfs = libzfs_init()) == NULL) {
40252082Seschrock 		(void) fprintf(stderr, gettext("internal error: failed to "
40262082Seschrock 		    "initialize ZFS library\n"));
40272082Seschrock 		return (1);
40282082Seschrock 	}
40292082Seschrock 
40304988Sek110237 	zpool_set_history_str("zfs", argc, argv, history_str);
40314988Sek110237 	verify(zpool_stage_history(g_zfs, history_str) == 0);
40324543Smarks 
40332082Seschrock 	libzfs_print_on_error(g_zfs, B_TRUE);
40342082Seschrock 
4035789Sahrens 	if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
4036789Sahrens 		(void) fprintf(stderr, gettext("internal error: unable to "
4037789Sahrens 		    "open %s\n"), MNTTAB);
4038789Sahrens 		return (1);
4039789Sahrens 	}
4040789Sahrens 
4041789Sahrens 	/*
4042789Sahrens 	 * This command also doubles as the /etc/fs mount and unmount program.
4043789Sahrens 	 * Determine if we should take this behavior based on argv[0].
4044789Sahrens 	 */
4045789Sahrens 	progname = basename(argv[0]);
4046789Sahrens 	if (strcmp(progname, "mount") == 0) {
4047789Sahrens 		ret = manual_mount(argc, argv);
4048789Sahrens 	} else if (strcmp(progname, "umount") == 0) {
4049789Sahrens 		ret = manual_unmount(argc, argv);
4050789Sahrens 	} else {
4051789Sahrens 		/*
4052789Sahrens 		 * Make sure the user has specified some command.
4053789Sahrens 		 */
4054789Sahrens 		if (argc < 2) {
4055789Sahrens 			(void) fprintf(stderr, gettext("missing command\n"));
40562082Seschrock 			usage(B_FALSE);
4057789Sahrens 		}
4058789Sahrens 
4059789Sahrens 		cmdname = argv[1];
4060789Sahrens 
4061789Sahrens 		/*
4062789Sahrens 		 * The 'umount' command is an alias for 'unmount'
4063789Sahrens 		 */
4064789Sahrens 		if (strcmp(cmdname, "umount") == 0)
4065789Sahrens 			cmdname = "unmount";
4066789Sahrens 
4067789Sahrens 		/*
40681749Sahrens 		 * The 'recv' command is an alias for 'receive'
40691749Sahrens 		 */
40701749Sahrens 		if (strcmp(cmdname, "recv") == 0)
40711749Sahrens 			cmdname = "receive";
40721749Sahrens 
40731749Sahrens 		/*
4074789Sahrens 		 * Special case '-?'
4075789Sahrens 		 */
4076789Sahrens 		if (strcmp(cmdname, "-?") == 0)
40772082Seschrock 			usage(B_TRUE);
4078789Sahrens 
4079789Sahrens 		/*
4080789Sahrens 		 * Run the appropriate command.
4081789Sahrens 		 */
40828811SEric.Taylor@Sun.COM 		libzfs_mnttab_cache(g_zfs, B_TRUE);
40834543Smarks 		if (find_command_idx(cmdname, &i) == 0) {
40844543Smarks 			current_command = &command_table[i];
40854543Smarks 			ret = command_table[i].func(argc - 1, argv + 1);
40864787Sahrens 		} else if (strchr(cmdname, '=') != NULL) {
40874787Sahrens 			verify(find_command_idx("set", &i) == 0);
40884787Sahrens 			current_command = &command_table[i];
40894787Sahrens 			ret = command_table[i].func(argc, argv);
40904787Sahrens 		} else {
4091789Sahrens 			(void) fprintf(stderr, gettext("unrecognized "
4092789Sahrens 			    "command '%s'\n"), cmdname);
40932082Seschrock 			usage(B_FALSE);
4094789Sahrens 		}
40958811SEric.Taylor@Sun.COM 		libzfs_mnttab_cache(g_zfs, B_FALSE);
4096789Sahrens 	}
4097789Sahrens 
4098789Sahrens 	(void) fclose(mnttab_file);
4099789Sahrens 
41002082Seschrock 	libzfs_fini(g_zfs);
41012082Seschrock 
4102789Sahrens 	/*
4103789Sahrens 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4104789Sahrens 	 * for the purposes of running ::findleaks.
4105789Sahrens 	 */
4106789Sahrens 	if (getenv("ZFS_ABORT") != NULL) {
4107789Sahrens 		(void) printf("dumping core by request\n");
4108789Sahrens 		abort();
4109789Sahrens 	}
4110789Sahrens 
4111789Sahrens 	return (ret);
4112789Sahrens }
4113