xref: /freebsd/sys/contrib/openzfs/cmd/zfs/zfs_main.c (revision 7877fdebeeb35fad1cbbafce22598b1bdf97c786)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25eda14cbcSMatt Macy  * Copyright 2012 Milan Jurik. All rights reserved.
26eda14cbcSMatt Macy  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27eda14cbcSMatt Macy  * Copyright (c) 2013 Steven Hartland.  All rights reserved.
28eda14cbcSMatt Macy  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
29eda14cbcSMatt Macy  * Copyright 2016 Nexenta Systems, Inc.
30eda14cbcSMatt Macy  * Copyright (c) 2019 Datto Inc.
31eda14cbcSMatt Macy  * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
32eda14cbcSMatt Macy  * Copyright 2019 Joyent, Inc.
33eda14cbcSMatt Macy  * Copyright (c) 2019, 2020 by Christian Schwarz. All rights reserved.
34eda14cbcSMatt Macy  */
35eda14cbcSMatt Macy 
36eda14cbcSMatt Macy #include <assert.h>
37eda14cbcSMatt Macy #include <ctype.h>
38eda14cbcSMatt Macy #include <sys/debug.h>
39eda14cbcSMatt Macy #include <errno.h>
40eda14cbcSMatt Macy #include <getopt.h>
41eda14cbcSMatt Macy #include <libgen.h>
42eda14cbcSMatt Macy #include <libintl.h>
43eda14cbcSMatt Macy #include <libuutil.h>
44eda14cbcSMatt Macy #include <libnvpair.h>
45eda14cbcSMatt Macy #include <locale.h>
46eda14cbcSMatt Macy #include <stddef.h>
47eda14cbcSMatt Macy #include <stdio.h>
48eda14cbcSMatt Macy #include <stdlib.h>
49eda14cbcSMatt Macy #include <strings.h>
50eda14cbcSMatt Macy #include <unistd.h>
51eda14cbcSMatt Macy #include <fcntl.h>
52eda14cbcSMatt Macy #include <zone.h>
53eda14cbcSMatt Macy #include <grp.h>
54eda14cbcSMatt Macy #include <pwd.h>
55eda14cbcSMatt Macy #include <signal.h>
56eda14cbcSMatt Macy #include <sys/debug.h>
57eda14cbcSMatt Macy #include <sys/list.h>
58eda14cbcSMatt Macy #include <sys/mkdev.h>
59eda14cbcSMatt Macy #include <sys/mntent.h>
60eda14cbcSMatt Macy #include <sys/mnttab.h>
61eda14cbcSMatt Macy #include <sys/mount.h>
62eda14cbcSMatt Macy #include <sys/stat.h>
63eda14cbcSMatt Macy #include <sys/fs/zfs.h>
64eda14cbcSMatt Macy #include <sys/systeminfo.h>
65eda14cbcSMatt Macy #include <sys/types.h>
66eda14cbcSMatt Macy #include <time.h>
67eda14cbcSMatt Macy #include <sys/zfs_project.h>
68eda14cbcSMatt Macy 
69eda14cbcSMatt Macy #include <libzfs.h>
70eda14cbcSMatt Macy #include <libzfs_core.h>
71eda14cbcSMatt Macy #include <zfs_prop.h>
72eda14cbcSMatt Macy #include <zfs_deleg.h>
73eda14cbcSMatt Macy #include <libzutil.h>
74eda14cbcSMatt Macy #include <libuutil.h>
75eda14cbcSMatt Macy #ifdef HAVE_IDMAP
76eda14cbcSMatt Macy #include <aclutils.h>
77eda14cbcSMatt Macy #include <directory.h>
78eda14cbcSMatt Macy #endif /* HAVE_IDMAP */
79eda14cbcSMatt Macy 
80eda14cbcSMatt Macy #include "zfs_iter.h"
81eda14cbcSMatt Macy #include "zfs_util.h"
82eda14cbcSMatt Macy #include "zfs_comutil.h"
83eda14cbcSMatt Macy #include "libzfs_impl.h"
84eda14cbcSMatt Macy #include "zfs_projectutil.h"
85eda14cbcSMatt Macy 
86eda14cbcSMatt Macy libzfs_handle_t *g_zfs;
87eda14cbcSMatt Macy 
88eda14cbcSMatt Macy static FILE *mnttab_file;
89eda14cbcSMatt Macy static char history_str[HIS_MAX_RECORD_LEN];
90eda14cbcSMatt Macy static boolean_t log_history = B_TRUE;
91eda14cbcSMatt Macy 
92eda14cbcSMatt Macy static int zfs_do_clone(int argc, char **argv);
93eda14cbcSMatt Macy static int zfs_do_create(int argc, char **argv);
94eda14cbcSMatt Macy static int zfs_do_destroy(int argc, char **argv);
95eda14cbcSMatt Macy static int zfs_do_get(int argc, char **argv);
96eda14cbcSMatt Macy static int zfs_do_inherit(int argc, char **argv);
97eda14cbcSMatt Macy static int zfs_do_list(int argc, char **argv);
98eda14cbcSMatt Macy static int zfs_do_mount(int argc, char **argv);
99eda14cbcSMatt Macy static int zfs_do_rename(int argc, char **argv);
100eda14cbcSMatt Macy static int zfs_do_rollback(int argc, char **argv);
101eda14cbcSMatt Macy static int zfs_do_set(int argc, char **argv);
102eda14cbcSMatt Macy static int zfs_do_upgrade(int argc, char **argv);
103eda14cbcSMatt Macy static int zfs_do_snapshot(int argc, char **argv);
104eda14cbcSMatt Macy static int zfs_do_unmount(int argc, char **argv);
105eda14cbcSMatt Macy static int zfs_do_share(int argc, char **argv);
106eda14cbcSMatt Macy static int zfs_do_unshare(int argc, char **argv);
107eda14cbcSMatt Macy static int zfs_do_send(int argc, char **argv);
108eda14cbcSMatt Macy static int zfs_do_receive(int argc, char **argv);
109eda14cbcSMatt Macy static int zfs_do_promote(int argc, char **argv);
110eda14cbcSMatt Macy static int zfs_do_userspace(int argc, char **argv);
111eda14cbcSMatt Macy static int zfs_do_allow(int argc, char **argv);
112eda14cbcSMatt Macy static int zfs_do_unallow(int argc, char **argv);
113eda14cbcSMatt Macy static int zfs_do_hold(int argc, char **argv);
114eda14cbcSMatt Macy static int zfs_do_holds(int argc, char **argv);
115eda14cbcSMatt Macy static int zfs_do_release(int argc, char **argv);
116eda14cbcSMatt Macy static int zfs_do_diff(int argc, char **argv);
117eda14cbcSMatt Macy static int zfs_do_bookmark(int argc, char **argv);
118eda14cbcSMatt Macy static int zfs_do_channel_program(int argc, char **argv);
119eda14cbcSMatt Macy static int zfs_do_load_key(int argc, char **argv);
120eda14cbcSMatt Macy static int zfs_do_unload_key(int argc, char **argv);
121eda14cbcSMatt Macy static int zfs_do_change_key(int argc, char **argv);
122eda14cbcSMatt Macy static int zfs_do_project(int argc, char **argv);
123eda14cbcSMatt Macy static int zfs_do_version(int argc, char **argv);
124eda14cbcSMatt Macy static int zfs_do_redact(int argc, char **argv);
125eda14cbcSMatt Macy static int zfs_do_wait(int argc, char **argv);
126eda14cbcSMatt Macy 
127eda14cbcSMatt Macy #ifdef __FreeBSD__
128eda14cbcSMatt Macy static int zfs_do_jail(int argc, char **argv);
129eda14cbcSMatt Macy static int zfs_do_unjail(int argc, char **argv);
130eda14cbcSMatt Macy #endif
131eda14cbcSMatt Macy 
132eda14cbcSMatt Macy /*
133eda14cbcSMatt Macy  * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
134eda14cbcSMatt Macy  */
135eda14cbcSMatt Macy 
136eda14cbcSMatt Macy #ifdef DEBUG
137eda14cbcSMatt Macy const char *
138eda14cbcSMatt Macy _umem_debug_init(void)
139eda14cbcSMatt Macy {
140eda14cbcSMatt Macy 	return ("default,verbose"); /* $UMEM_DEBUG setting */
141eda14cbcSMatt Macy }
142eda14cbcSMatt Macy 
143eda14cbcSMatt Macy const char *
144eda14cbcSMatt Macy _umem_logging_init(void)
145eda14cbcSMatt Macy {
146eda14cbcSMatt Macy 	return ("fail,contents"); /* $UMEM_LOGGING setting */
147eda14cbcSMatt Macy }
148eda14cbcSMatt Macy #endif
149eda14cbcSMatt Macy 
150eda14cbcSMatt Macy typedef enum {
151eda14cbcSMatt Macy 	HELP_CLONE,
152eda14cbcSMatt Macy 	HELP_CREATE,
153eda14cbcSMatt Macy 	HELP_DESTROY,
154eda14cbcSMatt Macy 	HELP_GET,
155eda14cbcSMatt Macy 	HELP_INHERIT,
156eda14cbcSMatt Macy 	HELP_UPGRADE,
157eda14cbcSMatt Macy 	HELP_LIST,
158eda14cbcSMatt Macy 	HELP_MOUNT,
159eda14cbcSMatt Macy 	HELP_PROMOTE,
160eda14cbcSMatt Macy 	HELP_RECEIVE,
161eda14cbcSMatt Macy 	HELP_RENAME,
162eda14cbcSMatt Macy 	HELP_ROLLBACK,
163eda14cbcSMatt Macy 	HELP_SEND,
164eda14cbcSMatt Macy 	HELP_SET,
165eda14cbcSMatt Macy 	HELP_SHARE,
166eda14cbcSMatt Macy 	HELP_SNAPSHOT,
167eda14cbcSMatt Macy 	HELP_UNMOUNT,
168eda14cbcSMatt Macy 	HELP_UNSHARE,
169eda14cbcSMatt Macy 	HELP_ALLOW,
170eda14cbcSMatt Macy 	HELP_UNALLOW,
171eda14cbcSMatt Macy 	HELP_USERSPACE,
172eda14cbcSMatt Macy 	HELP_GROUPSPACE,
173eda14cbcSMatt Macy 	HELP_PROJECTSPACE,
174eda14cbcSMatt Macy 	HELP_PROJECT,
175eda14cbcSMatt Macy 	HELP_HOLD,
176eda14cbcSMatt Macy 	HELP_HOLDS,
177eda14cbcSMatt Macy 	HELP_RELEASE,
178eda14cbcSMatt Macy 	HELP_DIFF,
179eda14cbcSMatt Macy 	HELP_BOOKMARK,
180eda14cbcSMatt Macy 	HELP_CHANNEL_PROGRAM,
181eda14cbcSMatt Macy 	HELP_LOAD_KEY,
182eda14cbcSMatt Macy 	HELP_UNLOAD_KEY,
183eda14cbcSMatt Macy 	HELP_CHANGE_KEY,
184eda14cbcSMatt Macy 	HELP_VERSION,
185eda14cbcSMatt Macy 	HELP_REDACT,
186eda14cbcSMatt Macy 	HELP_JAIL,
187eda14cbcSMatt Macy 	HELP_UNJAIL,
188eda14cbcSMatt Macy 	HELP_WAIT,
189eda14cbcSMatt Macy } zfs_help_t;
190eda14cbcSMatt Macy 
191eda14cbcSMatt Macy typedef struct zfs_command {
192eda14cbcSMatt Macy 	const char	*name;
193eda14cbcSMatt Macy 	int		(*func)(int argc, char **argv);
194eda14cbcSMatt Macy 	zfs_help_t	usage;
195eda14cbcSMatt Macy } zfs_command_t;
196eda14cbcSMatt Macy 
197eda14cbcSMatt Macy /*
198eda14cbcSMatt Macy  * Master command table.  Each ZFS command has a name, associated function, and
199eda14cbcSMatt Macy  * usage message.  The usage messages need to be internationalized, so we have
200eda14cbcSMatt Macy  * to have a function to return the usage message based on a command index.
201eda14cbcSMatt Macy  *
202eda14cbcSMatt Macy  * These commands are organized according to how they are displayed in the usage
203eda14cbcSMatt Macy  * message.  An empty command (one with a NULL name) indicates an empty line in
204eda14cbcSMatt Macy  * the generic usage message.
205eda14cbcSMatt Macy  */
206eda14cbcSMatt Macy static zfs_command_t command_table[] = {
207eda14cbcSMatt Macy 	{ "version",	zfs_do_version, 	HELP_VERSION		},
208eda14cbcSMatt Macy 	{ NULL },
209eda14cbcSMatt Macy 	{ "create",	zfs_do_create,		HELP_CREATE		},
210eda14cbcSMatt Macy 	{ "destroy",	zfs_do_destroy,		HELP_DESTROY		},
211eda14cbcSMatt Macy 	{ NULL },
212eda14cbcSMatt Macy 	{ "snapshot",	zfs_do_snapshot,	HELP_SNAPSHOT		},
213eda14cbcSMatt Macy 	{ "rollback",	zfs_do_rollback,	HELP_ROLLBACK		},
214eda14cbcSMatt Macy 	{ "clone",	zfs_do_clone,		HELP_CLONE		},
215eda14cbcSMatt Macy 	{ "promote",	zfs_do_promote,		HELP_PROMOTE		},
216eda14cbcSMatt Macy 	{ "rename",	zfs_do_rename,		HELP_RENAME		},
217eda14cbcSMatt Macy 	{ "bookmark",	zfs_do_bookmark,	HELP_BOOKMARK		},
218eda14cbcSMatt Macy 	{ "program",    zfs_do_channel_program, HELP_CHANNEL_PROGRAM    },
219eda14cbcSMatt Macy 	{ NULL },
220eda14cbcSMatt Macy 	{ "list",	zfs_do_list,		HELP_LIST		},
221eda14cbcSMatt Macy 	{ NULL },
222eda14cbcSMatt Macy 	{ "set",	zfs_do_set,		HELP_SET		},
223eda14cbcSMatt Macy 	{ "get",	zfs_do_get,		HELP_GET		},
224eda14cbcSMatt Macy 	{ "inherit",	zfs_do_inherit,		HELP_INHERIT		},
225eda14cbcSMatt Macy 	{ "upgrade",	zfs_do_upgrade,		HELP_UPGRADE		},
226eda14cbcSMatt Macy 	{ NULL },
227eda14cbcSMatt Macy 	{ "userspace",	zfs_do_userspace,	HELP_USERSPACE		},
228eda14cbcSMatt Macy 	{ "groupspace",	zfs_do_userspace,	HELP_GROUPSPACE		},
229eda14cbcSMatt Macy 	{ "projectspace", zfs_do_userspace,	HELP_PROJECTSPACE	},
230eda14cbcSMatt Macy 	{ NULL },
231eda14cbcSMatt Macy 	{ "project",	zfs_do_project,		HELP_PROJECT		},
232eda14cbcSMatt Macy 	{ NULL },
233eda14cbcSMatt Macy 	{ "mount",	zfs_do_mount,		HELP_MOUNT		},
234eda14cbcSMatt Macy 	{ "unmount",	zfs_do_unmount,		HELP_UNMOUNT		},
235eda14cbcSMatt Macy 	{ "share",	zfs_do_share,		HELP_SHARE		},
236eda14cbcSMatt Macy 	{ "unshare",	zfs_do_unshare,		HELP_UNSHARE		},
237eda14cbcSMatt Macy 	{ NULL },
238eda14cbcSMatt Macy 	{ "send",	zfs_do_send,		HELP_SEND		},
239eda14cbcSMatt Macy 	{ "receive",	zfs_do_receive,		HELP_RECEIVE		},
240eda14cbcSMatt Macy 	{ NULL },
241eda14cbcSMatt Macy 	{ "allow",	zfs_do_allow,		HELP_ALLOW		},
242eda14cbcSMatt Macy 	{ NULL },
243eda14cbcSMatt Macy 	{ "unallow",	zfs_do_unallow,		HELP_UNALLOW		},
244eda14cbcSMatt Macy 	{ NULL },
245eda14cbcSMatt Macy 	{ "hold",	zfs_do_hold,		HELP_HOLD		},
246eda14cbcSMatt Macy 	{ "holds",	zfs_do_holds,		HELP_HOLDS		},
247eda14cbcSMatt Macy 	{ "release",	zfs_do_release,		HELP_RELEASE		},
248eda14cbcSMatt Macy 	{ "diff",	zfs_do_diff,		HELP_DIFF		},
249eda14cbcSMatt Macy 	{ "load-key",	zfs_do_load_key,	HELP_LOAD_KEY		},
250eda14cbcSMatt Macy 	{ "unload-key",	zfs_do_unload_key,	HELP_UNLOAD_KEY		},
251eda14cbcSMatt Macy 	{ "change-key",	zfs_do_change_key,	HELP_CHANGE_KEY		},
252eda14cbcSMatt Macy 	{ "redact",	zfs_do_redact,		HELP_REDACT		},
253eda14cbcSMatt Macy 	{ "wait",	zfs_do_wait,		HELP_WAIT		},
254eda14cbcSMatt Macy 
255eda14cbcSMatt Macy #ifdef __FreeBSD__
256eda14cbcSMatt Macy 	{ "jail",	zfs_do_jail,		HELP_JAIL		},
257eda14cbcSMatt Macy 	{ "unjail",	zfs_do_unjail,		HELP_UNJAIL		},
258eda14cbcSMatt Macy #endif
259eda14cbcSMatt Macy };
260eda14cbcSMatt Macy 
261eda14cbcSMatt Macy #define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
262eda14cbcSMatt Macy 
263eda14cbcSMatt Macy zfs_command_t *current_command;
264eda14cbcSMatt Macy 
265eda14cbcSMatt Macy static const char *
266eda14cbcSMatt Macy get_usage(zfs_help_t idx)
267eda14cbcSMatt Macy {
268eda14cbcSMatt Macy 	switch (idx) {
269eda14cbcSMatt Macy 	case HELP_CLONE:
270eda14cbcSMatt Macy 		return (gettext("\tclone [-p] [-o property=value] ... "
271eda14cbcSMatt Macy 		    "<snapshot> <filesystem|volume>\n"));
272eda14cbcSMatt Macy 	case HELP_CREATE:
273*7877fdebSMatt Macy 		return (gettext("\tcreate [-Pnpuv] [-o property=value] ... "
274eda14cbcSMatt Macy 		    "<filesystem>\n"
275eda14cbcSMatt Macy 		    "\tcreate [-Pnpsv] [-b blocksize] [-o property=value] ... "
276eda14cbcSMatt Macy 		    "-V <size> <volume>\n"));
277eda14cbcSMatt Macy 	case HELP_DESTROY:
278eda14cbcSMatt Macy 		return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
279eda14cbcSMatt Macy 		    "\tdestroy [-dnpRrv] "
280eda14cbcSMatt Macy 		    "<filesystem|volume>@<snap>[%<snap>][,...]\n"
281eda14cbcSMatt Macy 		    "\tdestroy <filesystem|volume>#<bookmark>\n"));
282eda14cbcSMatt Macy 	case HELP_GET:
283eda14cbcSMatt Macy 		return (gettext("\tget [-rHp] [-d max] "
284eda14cbcSMatt Macy 		    "[-o \"all\" | field[,...]]\n"
285eda14cbcSMatt Macy 		    "\t    [-t type[,...]] [-s source[,...]]\n"
286eda14cbcSMatt Macy 		    "\t    <\"all\" | property[,...]> "
287eda14cbcSMatt Macy 		    "[filesystem|volume|snapshot|bookmark] ...\n"));
288eda14cbcSMatt Macy 	case HELP_INHERIT:
289eda14cbcSMatt Macy 		return (gettext("\tinherit [-rS] <property> "
290eda14cbcSMatt Macy 		    "<filesystem|volume|snapshot> ...\n"));
291eda14cbcSMatt Macy 	case HELP_UPGRADE:
292eda14cbcSMatt Macy 		return (gettext("\tupgrade [-v]\n"
293eda14cbcSMatt Macy 		    "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
294eda14cbcSMatt Macy 	case HELP_LIST:
295eda14cbcSMatt Macy 		return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
296eda14cbcSMatt Macy 		    "[-s property]...\n\t    [-S property]... [-t type[,...]] "
297eda14cbcSMatt Macy 		    "[filesystem|volume|snapshot] ...\n"));
298eda14cbcSMatt Macy 	case HELP_MOUNT:
299eda14cbcSMatt Macy 		return (gettext("\tmount\n"
300eda14cbcSMatt Macy 		    "\tmount [-flvO] [-o opts] <-a | filesystem>\n"));
301eda14cbcSMatt Macy 	case HELP_PROMOTE:
302eda14cbcSMatt Macy 		return (gettext("\tpromote <clone-filesystem>\n"));
303eda14cbcSMatt Macy 	case HELP_RECEIVE:
304eda14cbcSMatt Macy 		return (gettext("\treceive [-vMnsFhu] "
305eda14cbcSMatt Macy 		    "[-o <property>=<value>] ... [-x <property>] ...\n"
306eda14cbcSMatt Macy 		    "\t    <filesystem|volume|snapshot>\n"
307eda14cbcSMatt Macy 		    "\treceive [-vMnsFhu] [-o <property>=<value>] ... "
308eda14cbcSMatt Macy 		    "[-x <property>] ... \n"
309eda14cbcSMatt Macy 		    "\t    [-d | -e] <filesystem>\n"
310eda14cbcSMatt Macy 		    "\treceive -A <filesystem|volume>\n"));
311eda14cbcSMatt Macy 	case HELP_RENAME:
312eda14cbcSMatt Macy 		return (gettext("\trename [-f] <filesystem|volume|snapshot> "
313eda14cbcSMatt Macy 		    "<filesystem|volume|snapshot>\n"
314eac7052fSMatt Macy 		    "\trename -p [-f] <filesystem|volume> <filesystem|volume>\n"
315eac7052fSMatt Macy 		    "\trename -u [-f] <filesystem> <filesystem>\n"
316eda14cbcSMatt Macy 		    "\trename -r <snapshot> <snapshot>\n"));
317eda14cbcSMatt Macy 	case HELP_ROLLBACK:
318eda14cbcSMatt Macy 		return (gettext("\trollback [-rRf] <snapshot>\n"));
319eda14cbcSMatt Macy 	case HELP_SEND:
320eda14cbcSMatt Macy 		return (gettext("\tsend [-DnPpRvLecwhb] [-[i|I] snapshot] "
321eda14cbcSMatt Macy 		    "<snapshot>\n"
322eda14cbcSMatt Macy 		    "\tsend [-nvPLecw] [-i snapshot|bookmark] "
323eda14cbcSMatt Macy 		    "<filesystem|volume|snapshot>\n"
324eda14cbcSMatt Macy 		    "\tsend [-DnPpvLec] [-i bookmark|snapshot] "
325eda14cbcSMatt Macy 		    "--redact <bookmark> <snapshot>\n"
326eda14cbcSMatt Macy 		    "\tsend [-nvPe] -t <receive_resume_token>\n"
327eda14cbcSMatt Macy 		    "\tsend [-Pnv] --saved filesystem\n"));
328eda14cbcSMatt Macy 	case HELP_SET:
329eda14cbcSMatt Macy 		return (gettext("\tset <property=value> ... "
330eda14cbcSMatt Macy 		    "<filesystem|volume|snapshot> ...\n"));
331eda14cbcSMatt Macy 	case HELP_SHARE:
332eda14cbcSMatt Macy 		return (gettext("\tshare [-l] <-a [nfs|smb] | filesystem>\n"));
333eda14cbcSMatt Macy 	case HELP_SNAPSHOT:
334eda14cbcSMatt Macy 		return (gettext("\tsnapshot [-r] [-o property=value] ... "
335eda14cbcSMatt Macy 		    "<filesystem|volume>@<snap> ...\n"));
336eda14cbcSMatt Macy 	case HELP_UNMOUNT:
337eda14cbcSMatt Macy 		return (gettext("\tunmount [-fu] "
338eda14cbcSMatt Macy 		    "<-a | filesystem|mountpoint>\n"));
339eda14cbcSMatt Macy 	case HELP_UNSHARE:
340eda14cbcSMatt Macy 		return (gettext("\tunshare "
341eda14cbcSMatt Macy 		    "<-a [nfs|smb] | filesystem|mountpoint>\n"));
342eda14cbcSMatt Macy 	case HELP_ALLOW:
343eda14cbcSMatt Macy 		return (gettext("\tallow <filesystem|volume>\n"
344eda14cbcSMatt Macy 		    "\tallow [-ldug] "
345eda14cbcSMatt Macy 		    "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
346eda14cbcSMatt Macy 		    "\t    <filesystem|volume>\n"
347eda14cbcSMatt Macy 		    "\tallow [-ld] -e <perm|@setname>[,...] "
348eda14cbcSMatt Macy 		    "<filesystem|volume>\n"
349eda14cbcSMatt Macy 		    "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
350eda14cbcSMatt Macy 		    "\tallow -s @setname <perm|@setname>[,...] "
351eda14cbcSMatt Macy 		    "<filesystem|volume>\n"));
352eda14cbcSMatt Macy 	case HELP_UNALLOW:
353eda14cbcSMatt Macy 		return (gettext("\tunallow [-rldug] "
354eda14cbcSMatt Macy 		    "<\"everyone\"|user|group>[,...]\n"
355eda14cbcSMatt Macy 		    "\t    [<perm|@setname>[,...]] <filesystem|volume>\n"
356eda14cbcSMatt Macy 		    "\tunallow [-rld] -e [<perm|@setname>[,...]] "
357eda14cbcSMatt Macy 		    "<filesystem|volume>\n"
358eda14cbcSMatt Macy 		    "\tunallow [-r] -c [<perm|@setname>[,...]] "
359eda14cbcSMatt Macy 		    "<filesystem|volume>\n"
360eda14cbcSMatt Macy 		    "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
361eda14cbcSMatt Macy 		    "<filesystem|volume>\n"));
362eda14cbcSMatt Macy 	case HELP_USERSPACE:
363eda14cbcSMatt Macy 		return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
364eda14cbcSMatt Macy 		    "[-s field] ...\n"
365eda14cbcSMatt Macy 		    "\t    [-S field] ... [-t type[,...]] "
366c40487d4SMatt Macy 		    "<filesystem|snapshot|path>\n"));
367eda14cbcSMatt Macy 	case HELP_GROUPSPACE:
368eda14cbcSMatt Macy 		return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
369eda14cbcSMatt Macy 		    "[-s field] ...\n"
370eda14cbcSMatt Macy 		    "\t    [-S field] ... [-t type[,...]] "
371c40487d4SMatt Macy 		    "<filesystem|snapshot|path>\n"));
372eda14cbcSMatt Macy 	case HELP_PROJECTSPACE:
373eda14cbcSMatt Macy 		return (gettext("\tprojectspace [-Hp] [-o field[,...]] "
374eda14cbcSMatt Macy 		    "[-s field] ... \n"
375c40487d4SMatt Macy 		    "\t    [-S field] ... <filesystem|snapshot|path>\n"));
376eda14cbcSMatt Macy 	case HELP_PROJECT:
377eda14cbcSMatt Macy 		return (gettext("\tproject [-d|-r] <directory|file ...>\n"
378eda14cbcSMatt Macy 		    "\tproject -c [-0] [-d|-r] [-p id] <directory|file ...>\n"
379eda14cbcSMatt Macy 		    "\tproject -C [-k] [-r] <directory ...>\n"
380eda14cbcSMatt Macy 		    "\tproject [-p id] [-r] [-s] <directory ...>\n"));
381eda14cbcSMatt Macy 	case HELP_HOLD:
382eda14cbcSMatt Macy 		return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
383eda14cbcSMatt Macy 	case HELP_HOLDS:
384eda14cbcSMatt Macy 		return (gettext("\tholds [-rH] <snapshot> ...\n"));
385eda14cbcSMatt Macy 	case HELP_RELEASE:
386eda14cbcSMatt Macy 		return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
387eda14cbcSMatt Macy 	case HELP_DIFF:
388eda14cbcSMatt Macy 		return (gettext("\tdiff [-FHt] <snapshot> "
389eda14cbcSMatt Macy 		    "[snapshot|filesystem]\n"));
390eda14cbcSMatt Macy 	case HELP_BOOKMARK:
391eda14cbcSMatt Macy 		return (gettext("\tbookmark <snapshot|bookmark> "
392eda14cbcSMatt Macy 		    "<newbookmark>\n"));
393eda14cbcSMatt Macy 	case HELP_CHANNEL_PROGRAM:
394eda14cbcSMatt Macy 		return (gettext("\tprogram [-jn] [-t <instruction limit>] "
395eda14cbcSMatt Macy 		    "[-m <memory limit (b)>]\n"
396eda14cbcSMatt Macy 		    "\t    <pool> <program file> [lua args...]\n"));
397eda14cbcSMatt Macy 	case HELP_LOAD_KEY:
398eda14cbcSMatt Macy 		return (gettext("\tload-key [-rn] [-L <keylocation>] "
399eda14cbcSMatt Macy 		    "<-a | filesystem|volume>\n"));
400eda14cbcSMatt Macy 	case HELP_UNLOAD_KEY:
401eda14cbcSMatt Macy 		return (gettext("\tunload-key [-r] "
402eda14cbcSMatt Macy 		    "<-a | filesystem|volume>\n"));
403eda14cbcSMatt Macy 	case HELP_CHANGE_KEY:
404eda14cbcSMatt Macy 		return (gettext("\tchange-key [-l] [-o keyformat=<value>]\n"
405eac7052fSMatt Macy 		    "\t    [-o keylocation=<value>] [-o pbkdf2iters=<value>]\n"
406eda14cbcSMatt Macy 		    "\t    <filesystem|volume>\n"
407eda14cbcSMatt Macy 		    "\tchange-key -i [-l] <filesystem|volume>\n"));
408eda14cbcSMatt Macy 	case HELP_VERSION:
409eda14cbcSMatt Macy 		return (gettext("\tversion\n"));
410eda14cbcSMatt Macy 	case HELP_REDACT:
411eda14cbcSMatt Macy 		return (gettext("\tredact <snapshot> <bookmark> "
412eda14cbcSMatt Macy 		    "<redaction_snapshot> ...\n"));
413eda14cbcSMatt Macy 	case HELP_JAIL:
414eda14cbcSMatt Macy 		return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
415eda14cbcSMatt Macy 	case HELP_UNJAIL:
416eda14cbcSMatt Macy 		return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
417eda14cbcSMatt Macy 	case HELP_WAIT:
418eda14cbcSMatt Macy 		return (gettext("\twait [-t <activity>] <filesystem>\n"));
419eda14cbcSMatt Macy 	}
420eda14cbcSMatt Macy 
421eda14cbcSMatt Macy 	abort();
422eda14cbcSMatt Macy 	/* NOTREACHED */
423eda14cbcSMatt Macy }
424eda14cbcSMatt Macy 
425eda14cbcSMatt Macy void
426eda14cbcSMatt Macy nomem(void)
427eda14cbcSMatt Macy {
428eda14cbcSMatt Macy 	(void) fprintf(stderr, gettext("internal error: out of memory\n"));
429eda14cbcSMatt Macy 	exit(1);
430eda14cbcSMatt Macy }
431eda14cbcSMatt Macy 
432eda14cbcSMatt Macy /*
433eda14cbcSMatt Macy  * Utility function to guarantee malloc() success.
434eda14cbcSMatt Macy  */
435eda14cbcSMatt Macy 
436eda14cbcSMatt Macy void *
437eda14cbcSMatt Macy safe_malloc(size_t size)
438eda14cbcSMatt Macy {
439eda14cbcSMatt Macy 	void *data;
440eda14cbcSMatt Macy 
441eda14cbcSMatt Macy 	if ((data = calloc(1, size)) == NULL)
442eda14cbcSMatt Macy 		nomem();
443eda14cbcSMatt Macy 
444eda14cbcSMatt Macy 	return (data);
445eda14cbcSMatt Macy }
446eda14cbcSMatt Macy 
447eda14cbcSMatt Macy static void *
448eda14cbcSMatt Macy safe_realloc(void *data, size_t size)
449eda14cbcSMatt Macy {
450eda14cbcSMatt Macy 	void *newp;
451eda14cbcSMatt Macy 	if ((newp = realloc(data, size)) == NULL) {
452eda14cbcSMatt Macy 		free(data);
453eda14cbcSMatt Macy 		nomem();
454eda14cbcSMatt Macy 	}
455eda14cbcSMatt Macy 
456eda14cbcSMatt Macy 	return (newp);
457eda14cbcSMatt Macy }
458eda14cbcSMatt Macy 
459eda14cbcSMatt Macy static char *
460eda14cbcSMatt Macy safe_strdup(char *str)
461eda14cbcSMatt Macy {
462eda14cbcSMatt Macy 	char *dupstr = strdup(str);
463eda14cbcSMatt Macy 
464eda14cbcSMatt Macy 	if (dupstr == NULL)
465eda14cbcSMatt Macy 		nomem();
466eda14cbcSMatt Macy 
467eda14cbcSMatt Macy 	return (dupstr);
468eda14cbcSMatt Macy }
469eda14cbcSMatt Macy 
470eda14cbcSMatt Macy /*
471eda14cbcSMatt Macy  * Callback routine that will print out information for each of
472eda14cbcSMatt Macy  * the properties.
473eda14cbcSMatt Macy  */
474eda14cbcSMatt Macy static int
475eda14cbcSMatt Macy usage_prop_cb(int prop, void *cb)
476eda14cbcSMatt Macy {
477eda14cbcSMatt Macy 	FILE *fp = cb;
478eda14cbcSMatt Macy 
479eda14cbcSMatt Macy 	(void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
480eda14cbcSMatt Macy 
481eda14cbcSMatt Macy 	if (zfs_prop_readonly(prop))
482eda14cbcSMatt Macy 		(void) fprintf(fp, " NO    ");
483eda14cbcSMatt Macy 	else
484eda14cbcSMatt Macy 		(void) fprintf(fp, "YES    ");
485eda14cbcSMatt Macy 
486eda14cbcSMatt Macy 	if (zfs_prop_inheritable(prop))
487eda14cbcSMatt Macy 		(void) fprintf(fp, "  YES   ");
488eda14cbcSMatt Macy 	else
489eda14cbcSMatt Macy 		(void) fprintf(fp, "   NO   ");
490eda14cbcSMatt Macy 
491eda14cbcSMatt Macy 	if (zfs_prop_values(prop) == NULL)
492eda14cbcSMatt Macy 		(void) fprintf(fp, "-\n");
493eda14cbcSMatt Macy 	else
494eda14cbcSMatt Macy 		(void) fprintf(fp, "%s\n", zfs_prop_values(prop));
495eda14cbcSMatt Macy 
496eda14cbcSMatt Macy 	return (ZPROP_CONT);
497eda14cbcSMatt Macy }
498eda14cbcSMatt Macy 
499eda14cbcSMatt Macy /*
500eda14cbcSMatt Macy  * Display usage message.  If we're inside a command, display only the usage for
501eda14cbcSMatt Macy  * that command.  Otherwise, iterate over the entire command table and display
502eda14cbcSMatt Macy  * a complete usage message.
503eda14cbcSMatt Macy  */
504eda14cbcSMatt Macy static void
505eda14cbcSMatt Macy usage(boolean_t requested)
506eda14cbcSMatt Macy {
507eda14cbcSMatt Macy 	int i;
508eda14cbcSMatt Macy 	boolean_t show_properties = B_FALSE;
509eda14cbcSMatt Macy 	FILE *fp = requested ? stdout : stderr;
510eda14cbcSMatt Macy 
511eda14cbcSMatt Macy 	if (current_command == NULL) {
512eda14cbcSMatt Macy 
513eda14cbcSMatt Macy 		(void) fprintf(fp, gettext("usage: zfs command args ...\n"));
514eda14cbcSMatt Macy 		(void) fprintf(fp,
515eda14cbcSMatt Macy 		    gettext("where 'command' is one of the following:\n\n"));
516eda14cbcSMatt Macy 
517eda14cbcSMatt Macy 		for (i = 0; i < NCOMMAND; i++) {
518eda14cbcSMatt Macy 			if (command_table[i].name == NULL)
519eda14cbcSMatt Macy 				(void) fprintf(fp, "\n");
520eda14cbcSMatt Macy 			else
521eda14cbcSMatt Macy 				(void) fprintf(fp, "%s",
522eda14cbcSMatt Macy 				    get_usage(command_table[i].usage));
523eda14cbcSMatt Macy 		}
524eda14cbcSMatt Macy 
525eda14cbcSMatt Macy 		(void) fprintf(fp, gettext("\nEach dataset is of the form: "
526eda14cbcSMatt Macy 		    "pool/[dataset/]*dataset[@name]\n"));
527eda14cbcSMatt Macy 	} else {
528eda14cbcSMatt Macy 		(void) fprintf(fp, gettext("usage:\n"));
529eda14cbcSMatt Macy 		(void) fprintf(fp, "%s", get_usage(current_command->usage));
530eda14cbcSMatt Macy 	}
531eda14cbcSMatt Macy 
532eda14cbcSMatt Macy 	if (current_command != NULL &&
533eda14cbcSMatt Macy 	    (strcmp(current_command->name, "set") == 0 ||
534eda14cbcSMatt Macy 	    strcmp(current_command->name, "get") == 0 ||
535eda14cbcSMatt Macy 	    strcmp(current_command->name, "inherit") == 0 ||
536eda14cbcSMatt Macy 	    strcmp(current_command->name, "list") == 0))
537eda14cbcSMatt Macy 		show_properties = B_TRUE;
538eda14cbcSMatt Macy 
539eda14cbcSMatt Macy 	if (show_properties) {
540eda14cbcSMatt Macy 		(void) fprintf(fp,
541eda14cbcSMatt Macy 		    gettext("\nThe following properties are supported:\n"));
542eda14cbcSMatt Macy 
543eda14cbcSMatt Macy 		(void) fprintf(fp, "\n\t%-14s %s  %s   %s\n\n",
544eda14cbcSMatt Macy 		    "PROPERTY", "EDIT", "INHERIT", "VALUES");
545eda14cbcSMatt Macy 
546eda14cbcSMatt Macy 		/* Iterate over all properties */
547eda14cbcSMatt Macy 		(void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
548eda14cbcSMatt Macy 		    ZFS_TYPE_DATASET);
549eda14cbcSMatt Macy 
550eda14cbcSMatt Macy 		(void) fprintf(fp, "\t%-15s ", "userused@...");
551eda14cbcSMatt Macy 		(void) fprintf(fp, " NO       NO   <size>\n");
552eda14cbcSMatt Macy 		(void) fprintf(fp, "\t%-15s ", "groupused@...");
553eda14cbcSMatt Macy 		(void) fprintf(fp, " NO       NO   <size>\n");
554eda14cbcSMatt Macy 		(void) fprintf(fp, "\t%-15s ", "projectused@...");
555eda14cbcSMatt Macy 		(void) fprintf(fp, " NO       NO   <size>\n");
556eda14cbcSMatt Macy 		(void) fprintf(fp, "\t%-15s ", "userobjused@...");
557eda14cbcSMatt Macy 		(void) fprintf(fp, " NO       NO   <size>\n");
558eda14cbcSMatt Macy 		(void) fprintf(fp, "\t%-15s ", "groupobjused@...");
559eda14cbcSMatt Macy 		(void) fprintf(fp, " NO       NO   <size>\n");
560eda14cbcSMatt Macy 		(void) fprintf(fp, "\t%-15s ", "projectobjused@...");
561eda14cbcSMatt Macy 		(void) fprintf(fp, " NO       NO   <size>\n");
562eda14cbcSMatt Macy 		(void) fprintf(fp, "\t%-15s ", "userquota@...");
563eda14cbcSMatt Macy 		(void) fprintf(fp, "YES       NO   <size> | none\n");
564eda14cbcSMatt Macy 		(void) fprintf(fp, "\t%-15s ", "groupquota@...");
565eda14cbcSMatt Macy 		(void) fprintf(fp, "YES       NO   <size> | none\n");
566eda14cbcSMatt Macy 		(void) fprintf(fp, "\t%-15s ", "projectquota@...");
567eda14cbcSMatt Macy 		(void) fprintf(fp, "YES       NO   <size> | none\n");
568eda14cbcSMatt Macy 		(void) fprintf(fp, "\t%-15s ", "userobjquota@...");
569eda14cbcSMatt Macy 		(void) fprintf(fp, "YES       NO   <size> | none\n");
570eda14cbcSMatt Macy 		(void) fprintf(fp, "\t%-15s ", "groupobjquota@...");
571eda14cbcSMatt Macy 		(void) fprintf(fp, "YES       NO   <size> | none\n");
572eda14cbcSMatt Macy 		(void) fprintf(fp, "\t%-15s ", "projectobjquota@...");
573eda14cbcSMatt Macy 		(void) fprintf(fp, "YES       NO   <size> | none\n");
574eda14cbcSMatt Macy 		(void) fprintf(fp, "\t%-15s ", "written@<snap>");
575eda14cbcSMatt Macy 		(void) fprintf(fp, " NO       NO   <size>\n");
576eda14cbcSMatt Macy 		(void) fprintf(fp, "\t%-15s ", "written#<bookmark>");
577eda14cbcSMatt Macy 		(void) fprintf(fp, " NO       NO   <size>\n");
578eda14cbcSMatt Macy 
579eda14cbcSMatt Macy 		(void) fprintf(fp, gettext("\nSizes are specified in bytes "
580eda14cbcSMatt Macy 		    "with standard units such as K, M, G, etc.\n"));
581eda14cbcSMatt Macy 		(void) fprintf(fp, gettext("\nUser-defined properties can "
582eda14cbcSMatt Macy 		    "be specified by using a name containing a colon (:).\n"));
583eda14cbcSMatt Macy 		(void) fprintf(fp, gettext("\nThe {user|group|project}"
584eda14cbcSMatt Macy 		    "[obj]{used|quota}@ properties must be appended with\n"
585eda14cbcSMatt Macy 		    "a user|group|project specifier of one of these forms:\n"
586eda14cbcSMatt Macy 		    "    POSIX name      (eg: \"matt\")\n"
587eda14cbcSMatt Macy 		    "    POSIX id        (eg: \"126829\")\n"
588eda14cbcSMatt Macy 		    "    SMB name@domain (eg: \"matt@sun\")\n"
589eda14cbcSMatt Macy 		    "    SMB SID         (eg: \"S-1-234-567-89\")\n"));
590eda14cbcSMatt Macy 	} else {
591eda14cbcSMatt Macy 		(void) fprintf(fp,
592eda14cbcSMatt Macy 		    gettext("\nFor the property list, run: %s\n"),
593eda14cbcSMatt Macy 		    "zfs set|get");
594eda14cbcSMatt Macy 		(void) fprintf(fp,
595eda14cbcSMatt Macy 		    gettext("\nFor the delegated permission list, run: %s\n"),
596eda14cbcSMatt Macy 		    "zfs allow|unallow");
597eda14cbcSMatt Macy 	}
598eda14cbcSMatt Macy 
599eda14cbcSMatt Macy 	/*
600eda14cbcSMatt Macy 	 * See comments at end of main().
601eda14cbcSMatt Macy 	 */
602eda14cbcSMatt Macy 	if (getenv("ZFS_ABORT") != NULL) {
603eda14cbcSMatt Macy 		(void) printf("dumping core by request\n");
604eda14cbcSMatt Macy 		abort();
605eda14cbcSMatt Macy 	}
606eda14cbcSMatt Macy 
607eda14cbcSMatt Macy 	exit(requested ? 0 : 2);
608eda14cbcSMatt Macy }
609eda14cbcSMatt Macy 
610eda14cbcSMatt Macy /*
611eda14cbcSMatt Macy  * Take a property=value argument string and add it to the given nvlist.
612eda14cbcSMatt Macy  * Modifies the argument inplace.
613eda14cbcSMatt Macy  */
614eda14cbcSMatt Macy static boolean_t
615eda14cbcSMatt Macy parseprop(nvlist_t *props, char *propname)
616eda14cbcSMatt Macy {
617eda14cbcSMatt Macy 	char *propval;
618eda14cbcSMatt Macy 
619eda14cbcSMatt Macy 	if ((propval = strchr(propname, '=')) == NULL) {
620eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing "
621eda14cbcSMatt Macy 		    "'=' for property=value argument\n"));
622eda14cbcSMatt Macy 		return (B_FALSE);
623eda14cbcSMatt Macy 	}
624eda14cbcSMatt Macy 	*propval = '\0';
625eda14cbcSMatt Macy 	propval++;
626eda14cbcSMatt Macy 	if (nvlist_exists(props, propname)) {
627eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("property '%s' "
628eda14cbcSMatt Macy 		    "specified multiple times\n"), propname);
629eda14cbcSMatt Macy 		return (B_FALSE);
630eda14cbcSMatt Macy 	}
631eda14cbcSMatt Macy 	if (nvlist_add_string(props, propname, propval) != 0)
632eda14cbcSMatt Macy 		nomem();
633eda14cbcSMatt Macy 	return (B_TRUE);
634eda14cbcSMatt Macy }
635eda14cbcSMatt Macy 
636eda14cbcSMatt Macy /*
637eda14cbcSMatt Macy  * Take a property name argument and add it to the given nvlist.
638eda14cbcSMatt Macy  * Modifies the argument inplace.
639eda14cbcSMatt Macy  */
640eda14cbcSMatt Macy static boolean_t
641eda14cbcSMatt Macy parsepropname(nvlist_t *props, char *propname)
642eda14cbcSMatt Macy {
643eda14cbcSMatt Macy 	if (strchr(propname, '=') != NULL) {
644eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("invalid character "
645eda14cbcSMatt Macy 		    "'=' in property argument\n"));
646eda14cbcSMatt Macy 		return (B_FALSE);
647eda14cbcSMatt Macy 	}
648eda14cbcSMatt Macy 	if (nvlist_exists(props, propname)) {
649eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("property '%s' "
650eda14cbcSMatt Macy 		    "specified multiple times\n"), propname);
651eda14cbcSMatt Macy 		return (B_FALSE);
652eda14cbcSMatt Macy 	}
653eda14cbcSMatt Macy 	if (nvlist_add_boolean(props, propname) != 0)
654eda14cbcSMatt Macy 		nomem();
655eda14cbcSMatt Macy 	return (B_TRUE);
656eda14cbcSMatt Macy }
657eda14cbcSMatt Macy 
658eda14cbcSMatt Macy static int
659eda14cbcSMatt Macy parse_depth(char *opt, int *flags)
660eda14cbcSMatt Macy {
661eda14cbcSMatt Macy 	char *tmp;
662eda14cbcSMatt Macy 	int depth;
663eda14cbcSMatt Macy 
664eda14cbcSMatt Macy 	depth = (int)strtol(opt, &tmp, 0);
665eda14cbcSMatt Macy 	if (*tmp) {
666eda14cbcSMatt Macy 		(void) fprintf(stderr,
667eda14cbcSMatt Macy 		    gettext("%s is not an integer\n"), optarg);
668eda14cbcSMatt Macy 		usage(B_FALSE);
669eda14cbcSMatt Macy 	}
670eda14cbcSMatt Macy 	if (depth < 0) {
671eda14cbcSMatt Macy 		(void) fprintf(stderr,
672eda14cbcSMatt Macy 		    gettext("Depth can not be negative.\n"));
673eda14cbcSMatt Macy 		usage(B_FALSE);
674eda14cbcSMatt Macy 	}
675eda14cbcSMatt Macy 	*flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
676eda14cbcSMatt Macy 	return (depth);
677eda14cbcSMatt Macy }
678eda14cbcSMatt Macy 
679eda14cbcSMatt Macy #define	PROGRESS_DELAY 2		/* seconds */
680eda14cbcSMatt Macy 
681eda14cbcSMatt Macy static char *pt_reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
682eda14cbcSMatt Macy static time_t pt_begin;
683eda14cbcSMatt Macy static char *pt_header = NULL;
684eda14cbcSMatt Macy static boolean_t pt_shown;
685eda14cbcSMatt Macy 
686eda14cbcSMatt Macy static void
687eda14cbcSMatt Macy start_progress_timer(void)
688eda14cbcSMatt Macy {
689eda14cbcSMatt Macy 	pt_begin = time(NULL) + PROGRESS_DELAY;
690eda14cbcSMatt Macy 	pt_shown = B_FALSE;
691eda14cbcSMatt Macy }
692eda14cbcSMatt Macy 
693eda14cbcSMatt Macy static void
694eda14cbcSMatt Macy set_progress_header(char *header)
695eda14cbcSMatt Macy {
696eda14cbcSMatt Macy 	assert(pt_header == NULL);
697eda14cbcSMatt Macy 	pt_header = safe_strdup(header);
698eda14cbcSMatt Macy 	if (pt_shown) {
699eda14cbcSMatt Macy 		(void) printf("%s: ", header);
700eda14cbcSMatt Macy 		(void) fflush(stdout);
701eda14cbcSMatt Macy 	}
702eda14cbcSMatt Macy }
703eda14cbcSMatt Macy 
704eda14cbcSMatt Macy static void
705eda14cbcSMatt Macy update_progress(char *update)
706eda14cbcSMatt Macy {
707eda14cbcSMatt Macy 	if (!pt_shown && time(NULL) > pt_begin) {
708eda14cbcSMatt Macy 		int len = strlen(update);
709eda14cbcSMatt Macy 
710eda14cbcSMatt Macy 		(void) printf("%s: %s%*.*s", pt_header, update, len, len,
711eda14cbcSMatt Macy 		    pt_reverse);
712eda14cbcSMatt Macy 		(void) fflush(stdout);
713eda14cbcSMatt Macy 		pt_shown = B_TRUE;
714eda14cbcSMatt Macy 	} else if (pt_shown) {
715eda14cbcSMatt Macy 		int len = strlen(update);
716eda14cbcSMatt Macy 
717eda14cbcSMatt Macy 		(void) printf("%s%*.*s", update, len, len, pt_reverse);
718eda14cbcSMatt Macy 		(void) fflush(stdout);
719eda14cbcSMatt Macy 	}
720eda14cbcSMatt Macy }
721eda14cbcSMatt Macy 
722eda14cbcSMatt Macy static void
723eda14cbcSMatt Macy finish_progress(char *done)
724eda14cbcSMatt Macy {
725eda14cbcSMatt Macy 	if (pt_shown) {
726eda14cbcSMatt Macy 		(void) printf("%s\n", done);
727eda14cbcSMatt Macy 		(void) fflush(stdout);
728eda14cbcSMatt Macy 	}
729eda14cbcSMatt Macy 	free(pt_header);
730eda14cbcSMatt Macy 	pt_header = NULL;
731eda14cbcSMatt Macy }
732eda14cbcSMatt Macy 
733eda14cbcSMatt Macy static int
734eda14cbcSMatt Macy zfs_mount_and_share(libzfs_handle_t *hdl, const char *dataset, zfs_type_t type)
735eda14cbcSMatt Macy {
736eda14cbcSMatt Macy 	zfs_handle_t *zhp = NULL;
737eda14cbcSMatt Macy 	int ret = 0;
738eda14cbcSMatt Macy 
739eda14cbcSMatt Macy 	zhp = zfs_open(hdl, dataset, type);
740eda14cbcSMatt Macy 	if (zhp == NULL)
741eda14cbcSMatt Macy 		return (1);
742eda14cbcSMatt Macy 
743eda14cbcSMatt Macy 	/*
744eda14cbcSMatt Macy 	 * Volumes may neither be mounted or shared.  Potentially in the
745eda14cbcSMatt Macy 	 * future filesystems detected on these volumes could be mounted.
746eda14cbcSMatt Macy 	 */
747eda14cbcSMatt Macy 	if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
748eda14cbcSMatt Macy 		zfs_close(zhp);
749eda14cbcSMatt Macy 		return (0);
750eda14cbcSMatt Macy 	}
751eda14cbcSMatt Macy 
752eda14cbcSMatt Macy 	/*
753eda14cbcSMatt Macy 	 * Mount and/or share the new filesystem as appropriate.  We provide a
754eda14cbcSMatt Macy 	 * verbose error message to let the user know that their filesystem was
755eda14cbcSMatt Macy 	 * in fact created, even if we failed to mount or share it.
756eda14cbcSMatt Macy 	 *
757eda14cbcSMatt Macy 	 * If the user doesn't want the dataset automatically mounted, then
758eda14cbcSMatt Macy 	 * skip the mount/share step
759eda14cbcSMatt Macy 	 */
760eda14cbcSMatt Macy 	if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type, B_FALSE) &&
761eda14cbcSMatt Macy 	    zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON) {
762eda14cbcSMatt Macy 		if (zfs_mount_delegation_check()) {
763eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("filesystem "
764eda14cbcSMatt Macy 			    "successfully created, but it may only be "
765eda14cbcSMatt Macy 			    "mounted by root\n"));
766eda14cbcSMatt Macy 			ret = 1;
767eda14cbcSMatt Macy 		} else if (zfs_mount(zhp, NULL, 0) != 0) {
768eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("filesystem "
769eda14cbcSMatt Macy 			    "successfully created, but not mounted\n"));
770eda14cbcSMatt Macy 			ret = 1;
771eda14cbcSMatt Macy 		} else if (zfs_share(zhp) != 0) {
772eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("filesystem "
773eda14cbcSMatt Macy 			    "successfully created, but not shared\n"));
774eda14cbcSMatt Macy 			ret = 1;
775eda14cbcSMatt Macy 		}
776eda14cbcSMatt Macy 		zfs_commit_all_shares();
777eda14cbcSMatt Macy 	}
778eda14cbcSMatt Macy 
779eda14cbcSMatt Macy 	zfs_close(zhp);
780eda14cbcSMatt Macy 
781eda14cbcSMatt Macy 	return (ret);
782eda14cbcSMatt Macy }
783eda14cbcSMatt Macy 
784eda14cbcSMatt Macy /*
785eda14cbcSMatt Macy  * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
786eda14cbcSMatt Macy  *
787eda14cbcSMatt Macy  * Given an existing dataset, create a writable copy whose initial contents
788eda14cbcSMatt Macy  * are the same as the source.  The newly created dataset maintains a
789eda14cbcSMatt Macy  * dependency on the original; the original cannot be destroyed so long as
790eda14cbcSMatt Macy  * the clone exists.
791eda14cbcSMatt Macy  *
792eda14cbcSMatt Macy  * The '-p' flag creates all the non-existing ancestors of the target first.
793eda14cbcSMatt Macy  */
794eda14cbcSMatt Macy static int
795eda14cbcSMatt Macy zfs_do_clone(int argc, char **argv)
796eda14cbcSMatt Macy {
797eda14cbcSMatt Macy 	zfs_handle_t *zhp = NULL;
798eda14cbcSMatt Macy 	boolean_t parents = B_FALSE;
799eda14cbcSMatt Macy 	nvlist_t *props;
800eda14cbcSMatt Macy 	int ret = 0;
801eda14cbcSMatt Macy 	int c;
802eda14cbcSMatt Macy 
803eda14cbcSMatt Macy 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
804eda14cbcSMatt Macy 		nomem();
805eda14cbcSMatt Macy 
806eda14cbcSMatt Macy 	/* check options */
807eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, "o:p")) != -1) {
808eda14cbcSMatt Macy 		switch (c) {
809eda14cbcSMatt Macy 		case 'o':
810eda14cbcSMatt Macy 			if (!parseprop(props, optarg)) {
811eda14cbcSMatt Macy 				nvlist_free(props);
812eda14cbcSMatt Macy 				return (1);
813eda14cbcSMatt Macy 			}
814eda14cbcSMatt Macy 			break;
815eda14cbcSMatt Macy 		case 'p':
816eda14cbcSMatt Macy 			parents = B_TRUE;
817eda14cbcSMatt Macy 			break;
818eda14cbcSMatt Macy 		case '?':
819eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
820eda14cbcSMatt Macy 			    optopt);
821eda14cbcSMatt Macy 			goto usage;
822eda14cbcSMatt Macy 		}
823eda14cbcSMatt Macy 	}
824eda14cbcSMatt Macy 
825eda14cbcSMatt Macy 	argc -= optind;
826eda14cbcSMatt Macy 	argv += optind;
827eda14cbcSMatt Macy 
828eda14cbcSMatt Macy 	/* check number of arguments */
829eda14cbcSMatt Macy 	if (argc < 1) {
830eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing source dataset "
831eda14cbcSMatt Macy 		    "argument\n"));
832eda14cbcSMatt Macy 		goto usage;
833eda14cbcSMatt Macy 	}
834eda14cbcSMatt Macy 	if (argc < 2) {
835eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing target dataset "
836eda14cbcSMatt Macy 		    "argument\n"));
837eda14cbcSMatt Macy 		goto usage;
838eda14cbcSMatt Macy 	}
839eda14cbcSMatt Macy 	if (argc > 2) {
840eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("too many arguments\n"));
841eda14cbcSMatt Macy 		goto usage;
842eda14cbcSMatt Macy 	}
843eda14cbcSMatt Macy 
844eda14cbcSMatt Macy 	/* open the source dataset */
845eda14cbcSMatt Macy 	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) {
846eda14cbcSMatt Macy 		nvlist_free(props);
847eda14cbcSMatt Macy 		return (1);
848eda14cbcSMatt Macy 	}
849eda14cbcSMatt Macy 
850eda14cbcSMatt Macy 	if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
851eda14cbcSMatt Macy 	    ZFS_TYPE_VOLUME)) {
852eda14cbcSMatt Macy 		/*
853eda14cbcSMatt Macy 		 * Now create the ancestors of the target dataset.  If the
854eda14cbcSMatt Macy 		 * target already exists and '-p' option was used we should not
855eda14cbcSMatt Macy 		 * complain.
856eda14cbcSMatt Macy 		 */
857eda14cbcSMatt Macy 		if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
858eda14cbcSMatt Macy 		    ZFS_TYPE_VOLUME)) {
859eda14cbcSMatt Macy 			zfs_close(zhp);
860eda14cbcSMatt Macy 			nvlist_free(props);
861eda14cbcSMatt Macy 			return (0);
862eda14cbcSMatt Macy 		}
863eda14cbcSMatt Macy 		if (zfs_create_ancestors(g_zfs, argv[1]) != 0) {
864eda14cbcSMatt Macy 			zfs_close(zhp);
865eda14cbcSMatt Macy 			nvlist_free(props);
866eda14cbcSMatt Macy 			return (1);
867eda14cbcSMatt Macy 		}
868eda14cbcSMatt Macy 	}
869eda14cbcSMatt Macy 
870eda14cbcSMatt Macy 	/* pass to libzfs */
871eda14cbcSMatt Macy 	ret = zfs_clone(zhp, argv[1], props);
872eda14cbcSMatt Macy 
873eda14cbcSMatt Macy 	/* create the mountpoint if necessary */
874eda14cbcSMatt Macy 	if (ret == 0) {
875eda14cbcSMatt Macy 		if (log_history) {
876eda14cbcSMatt Macy 			(void) zpool_log_history(g_zfs, history_str);
877eda14cbcSMatt Macy 			log_history = B_FALSE;
878eda14cbcSMatt Macy 		}
879eda14cbcSMatt Macy 
880eda14cbcSMatt Macy 		ret = zfs_mount_and_share(g_zfs, argv[1], ZFS_TYPE_DATASET);
881eda14cbcSMatt Macy 	}
882eda14cbcSMatt Macy 
883eda14cbcSMatt Macy 	zfs_close(zhp);
884eda14cbcSMatt Macy 	nvlist_free(props);
885eda14cbcSMatt Macy 
886eda14cbcSMatt Macy 	return (!!ret);
887eda14cbcSMatt Macy 
888eda14cbcSMatt Macy usage:
889eda14cbcSMatt Macy 	ASSERT3P(zhp, ==, NULL);
890eda14cbcSMatt Macy 	nvlist_free(props);
891eda14cbcSMatt Macy 	usage(B_FALSE);
892eda14cbcSMatt Macy 	return (-1);
893eda14cbcSMatt Macy }
894eda14cbcSMatt Macy 
895eda14cbcSMatt Macy /*
896*7877fdebSMatt Macy  * Return a default volblocksize for the pool which always uses more than
897*7877fdebSMatt Macy  * half of the data sectors.  This primarily applies to dRAID which always
898*7877fdebSMatt Macy  * writes full stripe widths.
899*7877fdebSMatt Macy  */
900*7877fdebSMatt Macy static uint64_t
901*7877fdebSMatt Macy default_volblocksize(zpool_handle_t *zhp, nvlist_t *props)
902*7877fdebSMatt Macy {
903*7877fdebSMatt Macy 	uint64_t volblocksize, asize = SPA_MINBLOCKSIZE;
904*7877fdebSMatt Macy 	nvlist_t *tree, **vdevs;
905*7877fdebSMatt Macy 	uint_t nvdevs;
906*7877fdebSMatt Macy 
907*7877fdebSMatt Macy 	nvlist_t *config = zpool_get_config(zhp, NULL);
908*7877fdebSMatt Macy 
909*7877fdebSMatt Macy 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) != 0 ||
910*7877fdebSMatt Macy 	    nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN,
911*7877fdebSMatt Macy 	    &vdevs, &nvdevs) != 0) {
912*7877fdebSMatt Macy 		return (ZVOL_DEFAULT_BLOCKSIZE);
913*7877fdebSMatt Macy 	}
914*7877fdebSMatt Macy 
915*7877fdebSMatt Macy 	for (int i = 0; i < nvdevs; i++) {
916*7877fdebSMatt Macy 		nvlist_t *nv = vdevs[i];
917*7877fdebSMatt Macy 		uint64_t ashift, ndata, nparity;
918*7877fdebSMatt Macy 
919*7877fdebSMatt Macy 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &ashift) != 0)
920*7877fdebSMatt Macy 			continue;
921*7877fdebSMatt Macy 
922*7877fdebSMatt Macy 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DRAID_NDATA,
923*7877fdebSMatt Macy 		    &ndata) == 0) {
924*7877fdebSMatt Macy 			/* dRAID minimum allocation width */
925*7877fdebSMatt Macy 			asize = MAX(asize, ndata * (1ULL << ashift));
926*7877fdebSMatt Macy 		} else if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
927*7877fdebSMatt Macy 		    &nparity) == 0) {
928*7877fdebSMatt Macy 			/* raidz minimum allocation width */
929*7877fdebSMatt Macy 			if (nparity == 1)
930*7877fdebSMatt Macy 				asize = MAX(asize, 2 * (1ULL << ashift));
931*7877fdebSMatt Macy 			else
932*7877fdebSMatt Macy 				asize = MAX(asize, 4 * (1ULL << ashift));
933*7877fdebSMatt Macy 		} else {
934*7877fdebSMatt Macy 			/* mirror or (non-redundant) leaf vdev */
935*7877fdebSMatt Macy 			asize = MAX(asize, 1ULL << ashift);
936*7877fdebSMatt Macy 		}
937*7877fdebSMatt Macy 	}
938*7877fdebSMatt Macy 
939*7877fdebSMatt Macy 	/*
940*7877fdebSMatt Macy 	 * Calculate the target volblocksize such that more than half
941*7877fdebSMatt Macy 	 * of the asize is used. The following table is for 4k sectors.
942*7877fdebSMatt Macy 	 *
943*7877fdebSMatt Macy 	 * n   asize   blksz  used  |   n   asize   blksz  used
944*7877fdebSMatt Macy 	 * -------------------------+---------------------------------
945*7877fdebSMatt Macy 	 * 1   4,096   8,192  100%  |   9  36,864  32,768   88%
946*7877fdebSMatt Macy 	 * 2   8,192   8,192  100%  |  10  40,960  32,768   80%
947*7877fdebSMatt Macy 	 * 3  12,288   8,192   66%  |  11  45,056  32,768   72%
948*7877fdebSMatt Macy 	 * 4  16,384  16,384  100%  |  12  49,152  32,768   66%
949*7877fdebSMatt Macy 	 * 5  20,480  16,384   80%  |  13  53,248  32,768   61%
950*7877fdebSMatt Macy 	 * 6  24,576  16,384   66%  |  14  57,344  32,768   57%
951*7877fdebSMatt Macy 	 * 7  28,672  16,384   57%  |  15  61,440  32,768   53%
952*7877fdebSMatt Macy 	 * 8  32,768  32,768  100%  |  16  65,536  65,636  100%
953*7877fdebSMatt Macy 	 *
954*7877fdebSMatt Macy 	 * This is primarily a concern for dRAID which always allocates
955*7877fdebSMatt Macy 	 * a full stripe width.  For dRAID the default stripe width is
956*7877fdebSMatt Macy 	 * n=8 in which case the volblocksize is set to 32k. Ignoring
957*7877fdebSMatt Macy 	 * compression there are no unused sectors.  This same reasoning
958*7877fdebSMatt Macy 	 * applies to raidz[2,3] so target 4 sectors to minimize waste.
959*7877fdebSMatt Macy 	 */
960*7877fdebSMatt Macy 	uint64_t tgt_volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
961*7877fdebSMatt Macy 	while (tgt_volblocksize * 2 <= asize)
962*7877fdebSMatt Macy 		tgt_volblocksize *= 2;
963*7877fdebSMatt Macy 
964*7877fdebSMatt Macy 	const char *prop = zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE);
965*7877fdebSMatt Macy 	if (nvlist_lookup_uint64(props, prop, &volblocksize) == 0) {
966*7877fdebSMatt Macy 
967*7877fdebSMatt Macy 		/* Issue a warning when a non-optimal size is requested. */
968*7877fdebSMatt Macy 		if (volblocksize < ZVOL_DEFAULT_BLOCKSIZE) {
969*7877fdebSMatt Macy 			(void) fprintf(stderr, gettext("Warning: "
970*7877fdebSMatt Macy 			    "volblocksize (%llu) is less than the default "
971*7877fdebSMatt Macy 			    "minimum block size (%llu).\nTo reduce wasted "
972*7877fdebSMatt Macy 			    "space a volblocksize of %llu is recommended.\n"),
973*7877fdebSMatt Macy 			    (u_longlong_t)volblocksize,
974*7877fdebSMatt Macy 			    (u_longlong_t)ZVOL_DEFAULT_BLOCKSIZE,
975*7877fdebSMatt Macy 			    (u_longlong_t)tgt_volblocksize);
976*7877fdebSMatt Macy 		} else if (volblocksize < tgt_volblocksize) {
977*7877fdebSMatt Macy 			(void) fprintf(stderr, gettext("Warning: "
978*7877fdebSMatt Macy 			    "volblocksize (%llu) is much less than the "
979*7877fdebSMatt Macy 			    "minimum allocation\nunit (%llu), which wastes "
980*7877fdebSMatt Macy 			    "at least %llu%% of space. To reduce wasted "
981*7877fdebSMatt Macy 			    "space,\nuse a larger volblocksize (%llu is "
982*7877fdebSMatt Macy 			    "recommended), fewer dRAID data disks\n"
983*7877fdebSMatt Macy 			    "per group, or smaller sector size (ashift).\n"),
984*7877fdebSMatt Macy 			    (u_longlong_t)volblocksize, (u_longlong_t)asize,
985*7877fdebSMatt Macy 			    (u_longlong_t)((100 * (asize - volblocksize)) /
986*7877fdebSMatt Macy 			    asize), (u_longlong_t)tgt_volblocksize);
987*7877fdebSMatt Macy 		}
988*7877fdebSMatt Macy 	} else {
989*7877fdebSMatt Macy 		volblocksize = tgt_volblocksize;
990*7877fdebSMatt Macy 		fnvlist_add_uint64(props, prop, volblocksize);
991*7877fdebSMatt Macy 	}
992*7877fdebSMatt Macy 
993*7877fdebSMatt Macy 	return (volblocksize);
994*7877fdebSMatt Macy }
995*7877fdebSMatt Macy 
996*7877fdebSMatt Macy /*
997eda14cbcSMatt Macy  * zfs create [-Pnpv] [-o prop=value] ... fs
998eda14cbcSMatt Macy  * zfs create [-Pnpsv] [-b blocksize] [-o prop=value] ... -V vol size
999eda14cbcSMatt Macy  *
1000eda14cbcSMatt Macy  * Create a new dataset.  This command can be used to create filesystems
1001eda14cbcSMatt Macy  * and volumes.  Snapshot creation is handled by 'zfs snapshot'.
1002eda14cbcSMatt Macy  * For volumes, the user must specify a size to be used.
1003eda14cbcSMatt Macy  *
1004eda14cbcSMatt Macy  * The '-s' flag applies only to volumes, and indicates that we should not try
1005eda14cbcSMatt Macy  * to set the reservation for this volume.  By default we set a reservation
1006eda14cbcSMatt Macy  * equal to the size for any volume.  For pools with SPA_VERSION >=
1007eda14cbcSMatt Macy  * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
1008eda14cbcSMatt Macy  *
1009eda14cbcSMatt Macy  * The '-p' flag creates all the non-existing ancestors of the target first.
1010eda14cbcSMatt Macy  *
1011eda14cbcSMatt Macy  * The '-n' flag is no-op (dry run) mode.  This will perform a user-space sanity
1012eda14cbcSMatt Macy  * check of arguments and properties, but does not check for permissions,
1013eda14cbcSMatt Macy  * available space, etc.
1014eda14cbcSMatt Macy  *
1015*7877fdebSMatt Macy  * The '-u' flag prevents the newly created file system from being mounted.
1016*7877fdebSMatt Macy  *
1017eda14cbcSMatt Macy  * The '-v' flag is for verbose output.
1018eda14cbcSMatt Macy  *
1019eda14cbcSMatt Macy  * The '-P' flag is used for parseable output.  It implies '-v'.
1020eda14cbcSMatt Macy  */
1021eda14cbcSMatt Macy static int
1022eda14cbcSMatt Macy zfs_do_create(int argc, char **argv)
1023eda14cbcSMatt Macy {
1024eda14cbcSMatt Macy 	zfs_type_t type = ZFS_TYPE_FILESYSTEM;
1025eda14cbcSMatt Macy 	zpool_handle_t *zpool_handle = NULL;
1026eda14cbcSMatt Macy 	nvlist_t *real_props = NULL;
1027eda14cbcSMatt Macy 	uint64_t volsize = 0;
1028eda14cbcSMatt Macy 	int c;
1029eda14cbcSMatt Macy 	boolean_t noreserve = B_FALSE;
1030eda14cbcSMatt Macy 	boolean_t bflag = B_FALSE;
1031eda14cbcSMatt Macy 	boolean_t parents = B_FALSE;
1032eda14cbcSMatt Macy 	boolean_t dryrun = B_FALSE;
1033*7877fdebSMatt Macy 	boolean_t nomount = B_FALSE;
1034eda14cbcSMatt Macy 	boolean_t verbose = B_FALSE;
1035eda14cbcSMatt Macy 	boolean_t parseable = B_FALSE;
1036eda14cbcSMatt Macy 	int ret = 1;
1037eda14cbcSMatt Macy 	nvlist_t *props;
1038eda14cbcSMatt Macy 	uint64_t intval;
1039*7877fdebSMatt Macy 	char *strval;
1040eda14cbcSMatt Macy 
1041eda14cbcSMatt Macy 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
1042eda14cbcSMatt Macy 		nomem();
1043eda14cbcSMatt Macy 
1044eda14cbcSMatt Macy 	/* check options */
1045*7877fdebSMatt Macy 	while ((c = getopt(argc, argv, ":PV:b:nso:puv")) != -1) {
1046eda14cbcSMatt Macy 		switch (c) {
1047eda14cbcSMatt Macy 		case 'V':
1048eda14cbcSMatt Macy 			type = ZFS_TYPE_VOLUME;
1049eda14cbcSMatt Macy 			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
1050eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("bad volume "
1051eda14cbcSMatt Macy 				    "size '%s': %s\n"), optarg,
1052eda14cbcSMatt Macy 				    libzfs_error_description(g_zfs));
1053eda14cbcSMatt Macy 				goto error;
1054eda14cbcSMatt Macy 			}
1055eda14cbcSMatt Macy 
1056eda14cbcSMatt Macy 			if (nvlist_add_uint64(props,
1057eda14cbcSMatt Macy 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
1058eda14cbcSMatt Macy 				nomem();
1059eda14cbcSMatt Macy 			volsize = intval;
1060eda14cbcSMatt Macy 			break;
1061eda14cbcSMatt Macy 		case 'P':
1062eda14cbcSMatt Macy 			verbose = B_TRUE;
1063eda14cbcSMatt Macy 			parseable = B_TRUE;
1064eda14cbcSMatt Macy 			break;
1065eda14cbcSMatt Macy 		case 'p':
1066eda14cbcSMatt Macy 			parents = B_TRUE;
1067eda14cbcSMatt Macy 			break;
1068eda14cbcSMatt Macy 		case 'b':
1069eda14cbcSMatt Macy 			bflag = B_TRUE;
1070eda14cbcSMatt Macy 			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
1071eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("bad volume "
1072eda14cbcSMatt Macy 				    "block size '%s': %s\n"), optarg,
1073eda14cbcSMatt Macy 				    libzfs_error_description(g_zfs));
1074eda14cbcSMatt Macy 				goto error;
1075eda14cbcSMatt Macy 			}
1076eda14cbcSMatt Macy 
1077eda14cbcSMatt Macy 			if (nvlist_add_uint64(props,
1078eda14cbcSMatt Macy 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1079eda14cbcSMatt Macy 			    intval) != 0)
1080eda14cbcSMatt Macy 				nomem();
1081eda14cbcSMatt Macy 			break;
1082eda14cbcSMatt Macy 		case 'n':
1083eda14cbcSMatt Macy 			dryrun = B_TRUE;
1084eda14cbcSMatt Macy 			break;
1085eda14cbcSMatt Macy 		case 'o':
1086eda14cbcSMatt Macy 			if (!parseprop(props, optarg))
1087eda14cbcSMatt Macy 				goto error;
1088eda14cbcSMatt Macy 			break;
1089eda14cbcSMatt Macy 		case 's':
1090eda14cbcSMatt Macy 			noreserve = B_TRUE;
1091eda14cbcSMatt Macy 			break;
1092*7877fdebSMatt Macy 		case 'u':
1093*7877fdebSMatt Macy 			nomount = B_TRUE;
1094*7877fdebSMatt Macy 			break;
1095eda14cbcSMatt Macy 		case 'v':
1096eda14cbcSMatt Macy 			verbose = B_TRUE;
1097eda14cbcSMatt Macy 			break;
1098eda14cbcSMatt Macy 		case ':':
1099eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("missing size "
1100eda14cbcSMatt Macy 			    "argument\n"));
1101eda14cbcSMatt Macy 			goto badusage;
1102eda14cbcSMatt Macy 		case '?':
1103eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1104eda14cbcSMatt Macy 			    optopt);
1105eda14cbcSMatt Macy 			goto badusage;
1106eda14cbcSMatt Macy 		}
1107eda14cbcSMatt Macy 	}
1108eda14cbcSMatt Macy 
1109eda14cbcSMatt Macy 	if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
1110eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("'-s' and '-b' can only be "
1111eda14cbcSMatt Macy 		    "used when creating a volume\n"));
1112eda14cbcSMatt Macy 		goto badusage;
1113eda14cbcSMatt Macy 	}
1114*7877fdebSMatt Macy 	if (nomount && type != ZFS_TYPE_FILESYSTEM) {
1115*7877fdebSMatt Macy 		(void) fprintf(stderr, gettext("'-u' can only be "
1116*7877fdebSMatt Macy 		    "used when creating a filesystem\n"));
1117*7877fdebSMatt Macy 		goto badusage;
1118*7877fdebSMatt Macy 	}
1119eda14cbcSMatt Macy 
1120eda14cbcSMatt Macy 	argc -= optind;
1121eda14cbcSMatt Macy 	argv += optind;
1122eda14cbcSMatt Macy 
1123eda14cbcSMatt Macy 	/* check number of arguments */
1124eda14cbcSMatt Macy 	if (argc == 0) {
1125eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing %s argument\n"),
1126eda14cbcSMatt Macy 		    zfs_type_to_name(type));
1127eda14cbcSMatt Macy 		goto badusage;
1128eda14cbcSMatt Macy 	}
1129eda14cbcSMatt Macy 	if (argc > 1) {
1130eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("too many arguments\n"));
1131eda14cbcSMatt Macy 		goto badusage;
1132eda14cbcSMatt Macy 	}
1133eda14cbcSMatt Macy 
1134*7877fdebSMatt Macy 	if (dryrun || type == ZFS_TYPE_VOLUME) {
1135eda14cbcSMatt Macy 		char msg[ZFS_MAX_DATASET_NAME_LEN * 2];
1136eda14cbcSMatt Macy 		char *p;
1137eda14cbcSMatt Macy 
1138eda14cbcSMatt Macy 		if ((p = strchr(argv[0], '/')) != NULL)
1139eda14cbcSMatt Macy 			*p = '\0';
1140eda14cbcSMatt Macy 		zpool_handle = zpool_open(g_zfs, argv[0]);
1141eda14cbcSMatt Macy 		if (p != NULL)
1142eda14cbcSMatt Macy 			*p = '/';
1143eda14cbcSMatt Macy 		if (zpool_handle == NULL)
1144eda14cbcSMatt Macy 			goto error;
1145eda14cbcSMatt Macy 
1146eda14cbcSMatt Macy 		(void) snprintf(msg, sizeof (msg),
1147eda14cbcSMatt Macy 		    dryrun ? gettext("cannot verify '%s'") :
1148eda14cbcSMatt Macy 		    gettext("cannot create '%s'"), argv[0]);
1149eda14cbcSMatt Macy 		if (props && (real_props = zfs_valid_proplist(g_zfs, type,
1150eda14cbcSMatt Macy 		    props, 0, NULL, zpool_handle, B_TRUE, msg)) == NULL) {
1151eda14cbcSMatt Macy 			zpool_close(zpool_handle);
1152eda14cbcSMatt Macy 			goto error;
1153eda14cbcSMatt Macy 		}
1154eda14cbcSMatt Macy 	}
1155eda14cbcSMatt Macy 
1156eda14cbcSMatt Macy 	if (type == ZFS_TYPE_VOLUME) {
1157*7877fdebSMatt Macy 		const char *prop = zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE);
1158*7877fdebSMatt Macy 		uint64_t volblocksize = default_volblocksize(zpool_handle,
1159*7877fdebSMatt Macy 		    real_props);
1160eda14cbcSMatt Macy 
1161*7877fdebSMatt Macy 		if (volblocksize != ZVOL_DEFAULT_BLOCKSIZE &&
1162*7877fdebSMatt Macy 		    nvlist_lookup_string(props, prop, &strval) != 0) {
1163*7877fdebSMatt Macy 			if (asprintf(&strval, "%llu",
1164*7877fdebSMatt Macy 			    (u_longlong_t)volblocksize) == -1)
1165*7877fdebSMatt Macy 				nomem();
1166*7877fdebSMatt Macy 			nvlist_add_string(props, prop, strval);
1167*7877fdebSMatt Macy 			free(strval);
1168*7877fdebSMatt Macy 		}
1169eda14cbcSMatt Macy 
1170*7877fdebSMatt Macy 		/*
1171*7877fdebSMatt Macy 		 * If volsize is not a multiple of volblocksize, round it
1172*7877fdebSMatt Macy 		 * up to the nearest multiple of the volblocksize.
1173*7877fdebSMatt Macy 		 */
1174eda14cbcSMatt Macy 		if (volsize % volblocksize) {
1175eda14cbcSMatt Macy 			volsize = P2ROUNDUP_TYPED(volsize, volblocksize,
1176eda14cbcSMatt Macy 			    uint64_t);
1177eda14cbcSMatt Macy 
1178eda14cbcSMatt Macy 			if (nvlist_add_uint64(props,
1179eda14cbcSMatt Macy 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE), volsize) != 0) {
1180eda14cbcSMatt Macy 				nvlist_free(props);
1181eda14cbcSMatt Macy 				nomem();
1182eda14cbcSMatt Macy 			}
1183eda14cbcSMatt Macy 		}
1184eda14cbcSMatt Macy 	}
1185eda14cbcSMatt Macy 
1186eda14cbcSMatt Macy 	if (type == ZFS_TYPE_VOLUME && !noreserve) {
1187eda14cbcSMatt Macy 		uint64_t spa_version;
1188eda14cbcSMatt Macy 		zfs_prop_t resv_prop;
1189eda14cbcSMatt Macy 
1190eda14cbcSMatt Macy 		spa_version = zpool_get_prop_int(zpool_handle,
1191eda14cbcSMatt Macy 		    ZPOOL_PROP_VERSION, NULL);
1192eda14cbcSMatt Macy 		if (spa_version >= SPA_VERSION_REFRESERVATION)
1193eda14cbcSMatt Macy 			resv_prop = ZFS_PROP_REFRESERVATION;
1194eda14cbcSMatt Macy 		else
1195eda14cbcSMatt Macy 			resv_prop = ZFS_PROP_RESERVATION;
1196eda14cbcSMatt Macy 
1197eda14cbcSMatt Macy 		volsize = zvol_volsize_to_reservation(zpool_handle, volsize,
1198eda14cbcSMatt Macy 		    real_props);
1199eda14cbcSMatt Macy 
1200eda14cbcSMatt Macy 		if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
1201eda14cbcSMatt Macy 		    &strval) != 0) {
1202eda14cbcSMatt Macy 			if (nvlist_add_uint64(props,
1203eda14cbcSMatt Macy 			    zfs_prop_to_name(resv_prop), volsize) != 0) {
1204eda14cbcSMatt Macy 				nvlist_free(props);
1205eda14cbcSMatt Macy 				nomem();
1206eda14cbcSMatt Macy 			}
1207eda14cbcSMatt Macy 		}
1208eda14cbcSMatt Macy 	}
1209eda14cbcSMatt Macy 	if (zpool_handle != NULL) {
1210eda14cbcSMatt Macy 		zpool_close(zpool_handle);
1211eda14cbcSMatt Macy 		nvlist_free(real_props);
1212eda14cbcSMatt Macy 	}
1213eda14cbcSMatt Macy 
1214eda14cbcSMatt Macy 	if (parents && zfs_name_valid(argv[0], type)) {
1215eda14cbcSMatt Macy 		/*
1216eda14cbcSMatt Macy 		 * Now create the ancestors of target dataset.  If the target
1217eda14cbcSMatt Macy 		 * already exists and '-p' option was used we should not
1218eda14cbcSMatt Macy 		 * complain.
1219eda14cbcSMatt Macy 		 */
1220eda14cbcSMatt Macy 		if (zfs_dataset_exists(g_zfs, argv[0], type)) {
1221eda14cbcSMatt Macy 			ret = 0;
1222eda14cbcSMatt Macy 			goto error;
1223eda14cbcSMatt Macy 		}
1224eda14cbcSMatt Macy 		if (verbose) {
1225eda14cbcSMatt Macy 			(void) printf(parseable ? "create_ancestors\t%s\n" :
1226eda14cbcSMatt Macy 			    dryrun ?  "would create ancestors of %s\n" :
1227eda14cbcSMatt Macy 			    "create ancestors of %s\n", argv[0]);
1228eda14cbcSMatt Macy 		}
1229eda14cbcSMatt Macy 		if (!dryrun) {
1230eda14cbcSMatt Macy 			if (zfs_create_ancestors(g_zfs, argv[0]) != 0) {
1231eda14cbcSMatt Macy 				goto error;
1232eda14cbcSMatt Macy 			}
1233eda14cbcSMatt Macy 		}
1234eda14cbcSMatt Macy 	}
1235eda14cbcSMatt Macy 
1236eda14cbcSMatt Macy 	if (verbose) {
1237eda14cbcSMatt Macy 		nvpair_t *nvp = NULL;
1238eda14cbcSMatt Macy 		(void) printf(parseable ? "create\t%s\n" :
1239eda14cbcSMatt Macy 		    dryrun ? "would create %s\n" : "create %s\n", argv[0]);
1240eda14cbcSMatt Macy 		while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) {
1241eda14cbcSMatt Macy 			uint64_t uval;
1242eda14cbcSMatt Macy 			char *sval;
1243eda14cbcSMatt Macy 
1244eda14cbcSMatt Macy 			switch (nvpair_type(nvp)) {
1245eda14cbcSMatt Macy 			case DATA_TYPE_UINT64:
1246eda14cbcSMatt Macy 				VERIFY0(nvpair_value_uint64(nvp, &uval));
1247eda14cbcSMatt Macy 				(void) printf(parseable ?
1248eda14cbcSMatt Macy 				    "property\t%s\t%llu\n" : "\t%s=%llu\n",
1249eda14cbcSMatt Macy 				    nvpair_name(nvp), (u_longlong_t)uval);
1250eda14cbcSMatt Macy 				break;
1251eda14cbcSMatt Macy 			case DATA_TYPE_STRING:
1252eda14cbcSMatt Macy 				VERIFY0(nvpair_value_string(nvp, &sval));
1253eda14cbcSMatt Macy 				(void) printf(parseable ?
1254eda14cbcSMatt Macy 				    "property\t%s\t%s\n" : "\t%s=%s\n",
1255eda14cbcSMatt Macy 				    nvpair_name(nvp), sval);
1256eda14cbcSMatt Macy 				break;
1257eda14cbcSMatt Macy 			default:
1258eda14cbcSMatt Macy 				(void) fprintf(stderr, "property '%s' "
1259eda14cbcSMatt Macy 				    "has illegal type %d\n",
1260eda14cbcSMatt Macy 				    nvpair_name(nvp), nvpair_type(nvp));
1261eda14cbcSMatt Macy 				abort();
1262eda14cbcSMatt Macy 			}
1263eda14cbcSMatt Macy 		}
1264eda14cbcSMatt Macy 	}
1265eda14cbcSMatt Macy 	if (dryrun) {
1266eda14cbcSMatt Macy 		ret = 0;
1267eda14cbcSMatt Macy 		goto error;
1268eda14cbcSMatt Macy 	}
1269eda14cbcSMatt Macy 
1270eda14cbcSMatt Macy 	/* pass to libzfs */
1271eda14cbcSMatt Macy 	if (zfs_create(g_zfs, argv[0], type, props) != 0)
1272eda14cbcSMatt Macy 		goto error;
1273eda14cbcSMatt Macy 
1274eda14cbcSMatt Macy 	if (log_history) {
1275eda14cbcSMatt Macy 		(void) zpool_log_history(g_zfs, history_str);
1276eda14cbcSMatt Macy 		log_history = B_FALSE;
1277eda14cbcSMatt Macy 	}
1278eda14cbcSMatt Macy 
1279*7877fdebSMatt Macy 	if (nomount) {
1280*7877fdebSMatt Macy 		ret = 0;
1281*7877fdebSMatt Macy 		goto error;
1282*7877fdebSMatt Macy 	}
1283*7877fdebSMatt Macy 
1284eda14cbcSMatt Macy 	ret = zfs_mount_and_share(g_zfs, argv[0], ZFS_TYPE_DATASET);
1285eda14cbcSMatt Macy error:
1286eda14cbcSMatt Macy 	nvlist_free(props);
1287eda14cbcSMatt Macy 	return (ret);
1288eda14cbcSMatt Macy badusage:
1289eda14cbcSMatt Macy 	nvlist_free(props);
1290eda14cbcSMatt Macy 	usage(B_FALSE);
1291eda14cbcSMatt Macy 	return (2);
1292eda14cbcSMatt Macy }
1293eda14cbcSMatt Macy 
1294eda14cbcSMatt Macy /*
1295eda14cbcSMatt Macy  * zfs destroy [-rRf] <fs, vol>
1296eda14cbcSMatt Macy  * zfs destroy [-rRd] <snap>
1297eda14cbcSMatt Macy  *
1298eda14cbcSMatt Macy  *	-r	Recursively destroy all children
1299eda14cbcSMatt Macy  *	-R	Recursively destroy all dependents, including clones
1300eda14cbcSMatt Macy  *	-f	Force unmounting of any dependents
1301eda14cbcSMatt Macy  *	-d	If we can't destroy now, mark for deferred destruction
1302eda14cbcSMatt Macy  *
1303eda14cbcSMatt Macy  * Destroys the given dataset.  By default, it will unmount any filesystems,
1304eda14cbcSMatt Macy  * and refuse to destroy a dataset that has any dependents.  A dependent can
1305eda14cbcSMatt Macy  * either be a child, or a clone of a child.
1306eda14cbcSMatt Macy  */
1307eda14cbcSMatt Macy typedef struct destroy_cbdata {
1308eda14cbcSMatt Macy 	boolean_t	cb_first;
1309eda14cbcSMatt Macy 	boolean_t	cb_force;
1310eda14cbcSMatt Macy 	boolean_t	cb_recurse;
1311eda14cbcSMatt Macy 	boolean_t	cb_error;
1312eda14cbcSMatt Macy 	boolean_t	cb_doclones;
1313eda14cbcSMatt Macy 	zfs_handle_t	*cb_target;
1314eda14cbcSMatt Macy 	boolean_t	cb_defer_destroy;
1315eda14cbcSMatt Macy 	boolean_t	cb_verbose;
1316eda14cbcSMatt Macy 	boolean_t	cb_parsable;
1317eda14cbcSMatt Macy 	boolean_t	cb_dryrun;
1318eda14cbcSMatt Macy 	nvlist_t	*cb_nvl;
1319eda14cbcSMatt Macy 	nvlist_t	*cb_batchedsnaps;
1320eda14cbcSMatt Macy 
1321eda14cbcSMatt Macy 	/* first snap in contiguous run */
1322eda14cbcSMatt Macy 	char		*cb_firstsnap;
1323eda14cbcSMatt Macy 	/* previous snap in contiguous run */
1324eda14cbcSMatt Macy 	char		*cb_prevsnap;
1325eda14cbcSMatt Macy 	int64_t		cb_snapused;
1326eda14cbcSMatt Macy 	char		*cb_snapspec;
1327eda14cbcSMatt Macy 	char		*cb_bookmark;
1328eda14cbcSMatt Macy 	uint64_t	cb_snap_count;
1329eda14cbcSMatt Macy } destroy_cbdata_t;
1330eda14cbcSMatt Macy 
1331eda14cbcSMatt Macy /*
1332eda14cbcSMatt Macy  * Check for any dependents based on the '-r' or '-R' flags.
1333eda14cbcSMatt Macy  */
1334eda14cbcSMatt Macy static int
1335eda14cbcSMatt Macy destroy_check_dependent(zfs_handle_t *zhp, void *data)
1336eda14cbcSMatt Macy {
1337eda14cbcSMatt Macy 	destroy_cbdata_t *cbp = data;
1338eda14cbcSMatt Macy 	const char *tname = zfs_get_name(cbp->cb_target);
1339eda14cbcSMatt Macy 	const char *name = zfs_get_name(zhp);
1340eda14cbcSMatt Macy 
1341eda14cbcSMatt Macy 	if (strncmp(tname, name, strlen(tname)) == 0 &&
1342eda14cbcSMatt Macy 	    (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
1343eda14cbcSMatt Macy 		/*
1344eda14cbcSMatt Macy 		 * This is a direct descendant, not a clone somewhere else in
1345eda14cbcSMatt Macy 		 * the hierarchy.
1346eda14cbcSMatt Macy 		 */
1347eda14cbcSMatt Macy 		if (cbp->cb_recurse)
1348eda14cbcSMatt Macy 			goto out;
1349eda14cbcSMatt Macy 
1350eda14cbcSMatt Macy 		if (cbp->cb_first) {
1351eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("cannot destroy '%s': "
1352eda14cbcSMatt Macy 			    "%s has children\n"),
1353eda14cbcSMatt Macy 			    zfs_get_name(cbp->cb_target),
1354eda14cbcSMatt Macy 			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1355eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("use '-r' to destroy "
1356eda14cbcSMatt Macy 			    "the following datasets:\n"));
1357eda14cbcSMatt Macy 			cbp->cb_first = B_FALSE;
1358eda14cbcSMatt Macy 			cbp->cb_error = B_TRUE;
1359eda14cbcSMatt Macy 		}
1360eda14cbcSMatt Macy 
1361eda14cbcSMatt Macy 		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1362eda14cbcSMatt Macy 	} else {
1363eda14cbcSMatt Macy 		/*
1364eda14cbcSMatt Macy 		 * This is a clone.  We only want to report this if the '-r'
1365eda14cbcSMatt Macy 		 * wasn't specified, or the target is a snapshot.
1366eda14cbcSMatt Macy 		 */
1367eda14cbcSMatt Macy 		if (!cbp->cb_recurse &&
1368eda14cbcSMatt Macy 		    zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1369eda14cbcSMatt Macy 			goto out;
1370eda14cbcSMatt Macy 
1371eda14cbcSMatt Macy 		if (cbp->cb_first) {
1372eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("cannot destroy '%s': "
1373eda14cbcSMatt Macy 			    "%s has dependent clones\n"),
1374eda14cbcSMatt Macy 			    zfs_get_name(cbp->cb_target),
1375eda14cbcSMatt Macy 			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1376eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("use '-R' to destroy "
1377eda14cbcSMatt Macy 			    "the following datasets:\n"));
1378eda14cbcSMatt Macy 			cbp->cb_first = B_FALSE;
1379eda14cbcSMatt Macy 			cbp->cb_error = B_TRUE;
1380eda14cbcSMatt Macy 			cbp->cb_dryrun = B_TRUE;
1381eda14cbcSMatt Macy 		}
1382eda14cbcSMatt Macy 
1383eda14cbcSMatt Macy 		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1384eda14cbcSMatt Macy 	}
1385eda14cbcSMatt Macy 
1386eda14cbcSMatt Macy out:
1387eda14cbcSMatt Macy 	zfs_close(zhp);
1388eda14cbcSMatt Macy 	return (0);
1389eda14cbcSMatt Macy }
1390eda14cbcSMatt Macy 
1391eda14cbcSMatt Macy static int
1392eda14cbcSMatt Macy destroy_batched(destroy_cbdata_t *cb)
1393eda14cbcSMatt Macy {
1394eda14cbcSMatt Macy 	int error = zfs_destroy_snaps_nvl(g_zfs,
1395eda14cbcSMatt Macy 	    cb->cb_batchedsnaps, B_FALSE);
1396eda14cbcSMatt Macy 	fnvlist_free(cb->cb_batchedsnaps);
1397eda14cbcSMatt Macy 	cb->cb_batchedsnaps = fnvlist_alloc();
1398eda14cbcSMatt Macy 	return (error);
1399eda14cbcSMatt Macy }
1400eda14cbcSMatt Macy 
1401eda14cbcSMatt Macy static int
1402eda14cbcSMatt Macy destroy_callback(zfs_handle_t *zhp, void *data)
1403eda14cbcSMatt Macy {
1404eda14cbcSMatt Macy 	destroy_cbdata_t *cb = data;
1405eda14cbcSMatt Macy 	const char *name = zfs_get_name(zhp);
1406eda14cbcSMatt Macy 	int error;
1407eda14cbcSMatt Macy 
1408eda14cbcSMatt Macy 	if (cb->cb_verbose) {
1409eda14cbcSMatt Macy 		if (cb->cb_parsable) {
1410eda14cbcSMatt Macy 			(void) printf("destroy\t%s\n", name);
1411eda14cbcSMatt Macy 		} else if (cb->cb_dryrun) {
1412eda14cbcSMatt Macy 			(void) printf(gettext("would destroy %s\n"),
1413eda14cbcSMatt Macy 			    name);
1414eda14cbcSMatt Macy 		} else {
1415eda14cbcSMatt Macy 			(void) printf(gettext("will destroy %s\n"),
1416eda14cbcSMatt Macy 			    name);
1417eda14cbcSMatt Macy 		}
1418eda14cbcSMatt Macy 	}
1419eda14cbcSMatt Macy 
1420eda14cbcSMatt Macy 	/*
1421eda14cbcSMatt Macy 	 * Ignore pools (which we've already flagged as an error before getting
1422eda14cbcSMatt Macy 	 * here).
1423eda14cbcSMatt Macy 	 */
1424eda14cbcSMatt Macy 	if (strchr(zfs_get_name(zhp), '/') == NULL &&
1425eda14cbcSMatt Macy 	    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1426eda14cbcSMatt Macy 		zfs_close(zhp);
1427eda14cbcSMatt Macy 		return (0);
1428eda14cbcSMatt Macy 	}
1429eda14cbcSMatt Macy 	if (cb->cb_dryrun) {
1430eda14cbcSMatt Macy 		zfs_close(zhp);
1431eda14cbcSMatt Macy 		return (0);
1432eda14cbcSMatt Macy 	}
1433eda14cbcSMatt Macy 
1434eda14cbcSMatt Macy 	/*
1435eda14cbcSMatt Macy 	 * We batch up all contiguous snapshots (even of different
1436eda14cbcSMatt Macy 	 * filesystems) and destroy them with one ioctl.  We can't
1437eda14cbcSMatt Macy 	 * simply do all snap deletions and then all fs deletions,
1438eda14cbcSMatt Macy 	 * because we must delete a clone before its origin.
1439eda14cbcSMatt Macy 	 */
1440eda14cbcSMatt Macy 	if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1441eda14cbcSMatt Macy 		cb->cb_snap_count++;
1442eda14cbcSMatt Macy 		fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1443eda14cbcSMatt Macy 		if (cb->cb_snap_count % 10 == 0 && cb->cb_defer_destroy)
1444eda14cbcSMatt Macy 			error = destroy_batched(cb);
1445eda14cbcSMatt Macy 	} else {
1446eda14cbcSMatt Macy 		error = destroy_batched(cb);
1447eda14cbcSMatt Macy 		if (error != 0 ||
1448eda14cbcSMatt Macy 		    zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1449eda14cbcSMatt Macy 		    zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1450eda14cbcSMatt Macy 			zfs_close(zhp);
1451eda14cbcSMatt Macy 			/*
1452eda14cbcSMatt Macy 			 * When performing a recursive destroy we ignore errors
1453eda14cbcSMatt Macy 			 * so that the recursive destroy could continue
1454eda14cbcSMatt Macy 			 * destroying past problem datasets
1455eda14cbcSMatt Macy 			 */
1456eda14cbcSMatt Macy 			if (cb->cb_recurse) {
1457eda14cbcSMatt Macy 				cb->cb_error = B_TRUE;
1458eda14cbcSMatt Macy 				return (0);
1459eda14cbcSMatt Macy 			}
1460eda14cbcSMatt Macy 			return (-1);
1461eda14cbcSMatt Macy 		}
1462eda14cbcSMatt Macy 	}
1463eda14cbcSMatt Macy 
1464eda14cbcSMatt Macy 	zfs_close(zhp);
1465eda14cbcSMatt Macy 	return (0);
1466eda14cbcSMatt Macy }
1467eda14cbcSMatt Macy 
1468eda14cbcSMatt Macy static int
1469eda14cbcSMatt Macy destroy_print_cb(zfs_handle_t *zhp, void *arg)
1470eda14cbcSMatt Macy {
1471eda14cbcSMatt Macy 	destroy_cbdata_t *cb = arg;
1472eda14cbcSMatt Macy 	const char *name = zfs_get_name(zhp);
1473eda14cbcSMatt Macy 	int err = 0;
1474eda14cbcSMatt Macy 
1475eda14cbcSMatt Macy 	if (nvlist_exists(cb->cb_nvl, name)) {
1476eda14cbcSMatt Macy 		if (cb->cb_firstsnap == NULL)
1477eda14cbcSMatt Macy 			cb->cb_firstsnap = strdup(name);
1478eda14cbcSMatt Macy 		if (cb->cb_prevsnap != NULL)
1479eda14cbcSMatt Macy 			free(cb->cb_prevsnap);
1480eda14cbcSMatt Macy 		/* this snap continues the current range */
1481eda14cbcSMatt Macy 		cb->cb_prevsnap = strdup(name);
1482eda14cbcSMatt Macy 		if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1483eda14cbcSMatt Macy 			nomem();
1484eda14cbcSMatt Macy 		if (cb->cb_verbose) {
1485eda14cbcSMatt Macy 			if (cb->cb_parsable) {
1486eda14cbcSMatt Macy 				(void) printf("destroy\t%s\n", name);
1487eda14cbcSMatt Macy 			} else if (cb->cb_dryrun) {
1488eda14cbcSMatt Macy 				(void) printf(gettext("would destroy %s\n"),
1489eda14cbcSMatt Macy 				    name);
1490eda14cbcSMatt Macy 			} else {
1491eda14cbcSMatt Macy 				(void) printf(gettext("will destroy %s\n"),
1492eda14cbcSMatt Macy 				    name);
1493eda14cbcSMatt Macy 			}
1494eda14cbcSMatt Macy 		}
1495eda14cbcSMatt Macy 	} else if (cb->cb_firstsnap != NULL) {
1496eda14cbcSMatt Macy 		/* end of this range */
1497eda14cbcSMatt Macy 		uint64_t used = 0;
1498eda14cbcSMatt Macy 		err = lzc_snaprange_space(cb->cb_firstsnap,
1499eda14cbcSMatt Macy 		    cb->cb_prevsnap, &used);
1500eda14cbcSMatt Macy 		cb->cb_snapused += used;
1501eda14cbcSMatt Macy 		free(cb->cb_firstsnap);
1502eda14cbcSMatt Macy 		cb->cb_firstsnap = NULL;
1503eda14cbcSMatt Macy 		free(cb->cb_prevsnap);
1504eda14cbcSMatt Macy 		cb->cb_prevsnap = NULL;
1505eda14cbcSMatt Macy 	}
1506eda14cbcSMatt Macy 	zfs_close(zhp);
1507eda14cbcSMatt Macy 	return (err);
1508eda14cbcSMatt Macy }
1509eda14cbcSMatt Macy 
1510eda14cbcSMatt Macy static int
1511eda14cbcSMatt Macy destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1512eda14cbcSMatt Macy {
1513eda14cbcSMatt Macy 	int err;
1514eda14cbcSMatt Macy 	assert(cb->cb_firstsnap == NULL);
1515eda14cbcSMatt Macy 	assert(cb->cb_prevsnap == NULL);
1516eda14cbcSMatt Macy 	err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb, 0, 0);
1517eda14cbcSMatt Macy 	if (cb->cb_firstsnap != NULL) {
1518eda14cbcSMatt Macy 		uint64_t used = 0;
1519eda14cbcSMatt Macy 		if (err == 0) {
1520eda14cbcSMatt Macy 			err = lzc_snaprange_space(cb->cb_firstsnap,
1521eda14cbcSMatt Macy 			    cb->cb_prevsnap, &used);
1522eda14cbcSMatt Macy 		}
1523eda14cbcSMatt Macy 		cb->cb_snapused += used;
1524eda14cbcSMatt Macy 		free(cb->cb_firstsnap);
1525eda14cbcSMatt Macy 		cb->cb_firstsnap = NULL;
1526eda14cbcSMatt Macy 		free(cb->cb_prevsnap);
1527eda14cbcSMatt Macy 		cb->cb_prevsnap = NULL;
1528eda14cbcSMatt Macy 	}
1529eda14cbcSMatt Macy 	return (err);
1530eda14cbcSMatt Macy }
1531eda14cbcSMatt Macy 
1532eda14cbcSMatt Macy static int
1533eda14cbcSMatt Macy snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1534eda14cbcSMatt Macy {
1535eda14cbcSMatt Macy 	destroy_cbdata_t *cb = arg;
1536eda14cbcSMatt Macy 	int err = 0;
1537eda14cbcSMatt Macy 
1538eda14cbcSMatt Macy 	/* Check for clones. */
1539eda14cbcSMatt Macy 	if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1540eda14cbcSMatt Macy 		cb->cb_target = zhp;
1541eda14cbcSMatt Macy 		cb->cb_first = B_TRUE;
1542eda14cbcSMatt Macy 		err = zfs_iter_dependents(zhp, B_TRUE,
1543eda14cbcSMatt Macy 		    destroy_check_dependent, cb);
1544eda14cbcSMatt Macy 	}
1545eda14cbcSMatt Macy 
1546eda14cbcSMatt Macy 	if (err == 0) {
1547eda14cbcSMatt Macy 		if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1548eda14cbcSMatt Macy 			nomem();
1549eda14cbcSMatt Macy 	}
1550eda14cbcSMatt Macy 	zfs_close(zhp);
1551eda14cbcSMatt Macy 	return (err);
1552eda14cbcSMatt Macy }
1553eda14cbcSMatt Macy 
1554eda14cbcSMatt Macy static int
1555eda14cbcSMatt Macy gather_snapshots(zfs_handle_t *zhp, void *arg)
1556eda14cbcSMatt Macy {
1557eda14cbcSMatt Macy 	destroy_cbdata_t *cb = arg;
1558eda14cbcSMatt Macy 	int err = 0;
1559eda14cbcSMatt Macy 
1560eda14cbcSMatt Macy 	err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1561eda14cbcSMatt Macy 	if (err == ENOENT)
1562eda14cbcSMatt Macy 		err = 0;
1563eda14cbcSMatt Macy 	if (err != 0)
1564eda14cbcSMatt Macy 		goto out;
1565eda14cbcSMatt Macy 
1566eda14cbcSMatt Macy 	if (cb->cb_verbose) {
1567eda14cbcSMatt Macy 		err = destroy_print_snapshots(zhp, cb);
1568eda14cbcSMatt Macy 		if (err != 0)
1569eda14cbcSMatt Macy 			goto out;
1570eda14cbcSMatt Macy 	}
1571eda14cbcSMatt Macy 
1572eda14cbcSMatt Macy 	if (cb->cb_recurse)
1573eda14cbcSMatt Macy 		err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1574eda14cbcSMatt Macy 
1575eda14cbcSMatt Macy out:
1576eda14cbcSMatt Macy 	zfs_close(zhp);
1577eda14cbcSMatt Macy 	return (err);
1578eda14cbcSMatt Macy }
1579eda14cbcSMatt Macy 
1580eda14cbcSMatt Macy static int
1581eda14cbcSMatt Macy destroy_clones(destroy_cbdata_t *cb)
1582eda14cbcSMatt Macy {
1583eda14cbcSMatt Macy 	nvpair_t *pair;
1584eda14cbcSMatt Macy 	for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1585eda14cbcSMatt Macy 	    pair != NULL;
1586eda14cbcSMatt Macy 	    pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1587eda14cbcSMatt Macy 		zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1588eda14cbcSMatt Macy 		    ZFS_TYPE_SNAPSHOT);
1589eda14cbcSMatt Macy 		if (zhp != NULL) {
1590eda14cbcSMatt Macy 			boolean_t defer = cb->cb_defer_destroy;
1591eda14cbcSMatt Macy 			int err;
1592eda14cbcSMatt Macy 
1593eda14cbcSMatt Macy 			/*
1594eda14cbcSMatt Macy 			 * We can't defer destroy non-snapshots, so set it to
1595eda14cbcSMatt Macy 			 * false while destroying the clones.
1596eda14cbcSMatt Macy 			 */
1597eda14cbcSMatt Macy 			cb->cb_defer_destroy = B_FALSE;
1598eda14cbcSMatt Macy 			err = zfs_iter_dependents(zhp, B_FALSE,
1599eda14cbcSMatt Macy 			    destroy_callback, cb);
1600eda14cbcSMatt Macy 			cb->cb_defer_destroy = defer;
1601eda14cbcSMatt Macy 			zfs_close(zhp);
1602eda14cbcSMatt Macy 			if (err != 0)
1603eda14cbcSMatt Macy 				return (err);
1604eda14cbcSMatt Macy 		}
1605eda14cbcSMatt Macy 	}
1606eda14cbcSMatt Macy 	return (0);
1607eda14cbcSMatt Macy }
1608eda14cbcSMatt Macy 
1609eda14cbcSMatt Macy static int
1610eda14cbcSMatt Macy zfs_do_destroy(int argc, char **argv)
1611eda14cbcSMatt Macy {
1612eda14cbcSMatt Macy 	destroy_cbdata_t cb = { 0 };
1613eda14cbcSMatt Macy 	int rv = 0;
1614eda14cbcSMatt Macy 	int err = 0;
1615eda14cbcSMatt Macy 	int c;
1616eda14cbcSMatt Macy 	zfs_handle_t *zhp = NULL;
1617eda14cbcSMatt Macy 	char *at, *pound;
1618eda14cbcSMatt Macy 	zfs_type_t type = ZFS_TYPE_DATASET;
1619eda14cbcSMatt Macy 
1620eda14cbcSMatt Macy 	/* check options */
1621eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1622eda14cbcSMatt Macy 		switch (c) {
1623eda14cbcSMatt Macy 		case 'v':
1624eda14cbcSMatt Macy 			cb.cb_verbose = B_TRUE;
1625eda14cbcSMatt Macy 			break;
1626eda14cbcSMatt Macy 		case 'p':
1627eda14cbcSMatt Macy 			cb.cb_verbose = B_TRUE;
1628eda14cbcSMatt Macy 			cb.cb_parsable = B_TRUE;
1629eda14cbcSMatt Macy 			break;
1630eda14cbcSMatt Macy 		case 'n':
1631eda14cbcSMatt Macy 			cb.cb_dryrun = B_TRUE;
1632eda14cbcSMatt Macy 			break;
1633eda14cbcSMatt Macy 		case 'd':
1634eda14cbcSMatt Macy 			cb.cb_defer_destroy = B_TRUE;
1635eda14cbcSMatt Macy 			type = ZFS_TYPE_SNAPSHOT;
1636eda14cbcSMatt Macy 			break;
1637eda14cbcSMatt Macy 		case 'f':
1638eda14cbcSMatt Macy 			cb.cb_force = B_TRUE;
1639eda14cbcSMatt Macy 			break;
1640eda14cbcSMatt Macy 		case 'r':
1641eda14cbcSMatt Macy 			cb.cb_recurse = B_TRUE;
1642eda14cbcSMatt Macy 			break;
1643eda14cbcSMatt Macy 		case 'R':
1644eda14cbcSMatt Macy 			cb.cb_recurse = B_TRUE;
1645eda14cbcSMatt Macy 			cb.cb_doclones = B_TRUE;
1646eda14cbcSMatt Macy 			break;
1647eda14cbcSMatt Macy 		case '?':
1648eda14cbcSMatt Macy 		default:
1649eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1650eda14cbcSMatt Macy 			    optopt);
1651eda14cbcSMatt Macy 			usage(B_FALSE);
1652eda14cbcSMatt Macy 		}
1653eda14cbcSMatt Macy 	}
1654eda14cbcSMatt Macy 
1655eda14cbcSMatt Macy 	argc -= optind;
1656eda14cbcSMatt Macy 	argv += optind;
1657eda14cbcSMatt Macy 
1658eda14cbcSMatt Macy 	/* check number of arguments */
1659eda14cbcSMatt Macy 	if (argc == 0) {
1660eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing dataset argument\n"));
1661eda14cbcSMatt Macy 		usage(B_FALSE);
1662eda14cbcSMatt Macy 	}
1663eda14cbcSMatt Macy 	if (argc > 1) {
1664eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("too many arguments\n"));
1665eda14cbcSMatt Macy 		usage(B_FALSE);
1666eda14cbcSMatt Macy 	}
1667eda14cbcSMatt Macy 
1668eda14cbcSMatt Macy 	at = strchr(argv[0], '@');
1669eda14cbcSMatt Macy 	pound = strchr(argv[0], '#');
1670eda14cbcSMatt Macy 	if (at != NULL) {
1671eda14cbcSMatt Macy 
1672eda14cbcSMatt Macy 		/* Build the list of snaps to destroy in cb_nvl. */
1673eda14cbcSMatt Macy 		cb.cb_nvl = fnvlist_alloc();
1674eda14cbcSMatt Macy 
1675eda14cbcSMatt Macy 		*at = '\0';
1676eda14cbcSMatt Macy 		zhp = zfs_open(g_zfs, argv[0],
1677eda14cbcSMatt Macy 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1678eda14cbcSMatt Macy 		if (zhp == NULL) {
1679eda14cbcSMatt Macy 			nvlist_free(cb.cb_nvl);
1680eda14cbcSMatt Macy 			return (1);
1681eda14cbcSMatt Macy 		}
1682eda14cbcSMatt Macy 
1683eda14cbcSMatt Macy 		cb.cb_snapspec = at + 1;
1684eda14cbcSMatt Macy 		if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1685eda14cbcSMatt Macy 		    cb.cb_error) {
1686eda14cbcSMatt Macy 			rv = 1;
1687eda14cbcSMatt Macy 			goto out;
1688eda14cbcSMatt Macy 		}
1689eda14cbcSMatt Macy 
1690eda14cbcSMatt Macy 		if (nvlist_empty(cb.cb_nvl)) {
1691eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("could not find any "
1692eda14cbcSMatt Macy 			    "snapshots to destroy; check snapshot names.\n"));
1693eda14cbcSMatt Macy 			rv = 1;
1694eda14cbcSMatt Macy 			goto out;
1695eda14cbcSMatt Macy 		}
1696eda14cbcSMatt Macy 
1697eda14cbcSMatt Macy 		if (cb.cb_verbose) {
1698eda14cbcSMatt Macy 			char buf[16];
1699eda14cbcSMatt Macy 			zfs_nicebytes(cb.cb_snapused, buf, sizeof (buf));
1700eda14cbcSMatt Macy 			if (cb.cb_parsable) {
1701eda14cbcSMatt Macy 				(void) printf("reclaim\t%llu\n",
1702eda14cbcSMatt Macy 				    (u_longlong_t)cb.cb_snapused);
1703eda14cbcSMatt Macy 			} else if (cb.cb_dryrun) {
1704eda14cbcSMatt Macy 				(void) printf(gettext("would reclaim %s\n"),
1705eda14cbcSMatt Macy 				    buf);
1706eda14cbcSMatt Macy 			} else {
1707eda14cbcSMatt Macy 				(void) printf(gettext("will reclaim %s\n"),
1708eda14cbcSMatt Macy 				    buf);
1709eda14cbcSMatt Macy 			}
1710eda14cbcSMatt Macy 		}
1711eda14cbcSMatt Macy 
1712eda14cbcSMatt Macy 		if (!cb.cb_dryrun) {
1713eda14cbcSMatt Macy 			if (cb.cb_doclones) {
1714eda14cbcSMatt Macy 				cb.cb_batchedsnaps = fnvlist_alloc();
1715eda14cbcSMatt Macy 				err = destroy_clones(&cb);
1716eda14cbcSMatt Macy 				if (err == 0) {
1717eda14cbcSMatt Macy 					err = zfs_destroy_snaps_nvl(g_zfs,
1718eda14cbcSMatt Macy 					    cb.cb_batchedsnaps, B_FALSE);
1719eda14cbcSMatt Macy 				}
1720eda14cbcSMatt Macy 				if (err != 0) {
1721eda14cbcSMatt Macy 					rv = 1;
1722eda14cbcSMatt Macy 					goto out;
1723eda14cbcSMatt Macy 				}
1724eda14cbcSMatt Macy 			}
1725eda14cbcSMatt Macy 			if (err == 0) {
1726eda14cbcSMatt Macy 				err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1727eda14cbcSMatt Macy 				    cb.cb_defer_destroy);
1728eda14cbcSMatt Macy 			}
1729eda14cbcSMatt Macy 		}
1730eda14cbcSMatt Macy 
1731eda14cbcSMatt Macy 		if (err != 0)
1732eda14cbcSMatt Macy 			rv = 1;
1733eda14cbcSMatt Macy 	} else if (pound != NULL) {
1734eda14cbcSMatt Macy 		int err;
1735eda14cbcSMatt Macy 		nvlist_t *nvl;
1736eda14cbcSMatt Macy 
1737eda14cbcSMatt Macy 		if (cb.cb_dryrun) {
1738eda14cbcSMatt Macy 			(void) fprintf(stderr,
1739eda14cbcSMatt Macy 			    "dryrun is not supported with bookmark\n");
1740eda14cbcSMatt Macy 			return (-1);
1741eda14cbcSMatt Macy 		}
1742eda14cbcSMatt Macy 
1743eda14cbcSMatt Macy 		if (cb.cb_defer_destroy) {
1744eda14cbcSMatt Macy 			(void) fprintf(stderr,
1745eda14cbcSMatt Macy 			    "defer destroy is not supported with bookmark\n");
1746eda14cbcSMatt Macy 			return (-1);
1747eda14cbcSMatt Macy 		}
1748eda14cbcSMatt Macy 
1749eda14cbcSMatt Macy 		if (cb.cb_recurse) {
1750eda14cbcSMatt Macy 			(void) fprintf(stderr,
1751eda14cbcSMatt Macy 			    "recursive is not supported with bookmark\n");
1752eda14cbcSMatt Macy 			return (-1);
1753eda14cbcSMatt Macy 		}
1754eda14cbcSMatt Macy 
1755eda14cbcSMatt Macy 		/*
1756eda14cbcSMatt Macy 		 * Unfortunately, zfs_bookmark() doesn't honor the
1757eda14cbcSMatt Macy 		 * casesensitivity setting.  However, we can't simply
1758eda14cbcSMatt Macy 		 * remove this check, because lzc_destroy_bookmarks()
1759eda14cbcSMatt Macy 		 * ignores non-existent bookmarks, so this is necessary
1760eda14cbcSMatt Macy 		 * to get a proper error message.
1761eda14cbcSMatt Macy 		 */
1762eda14cbcSMatt Macy 		if (!zfs_bookmark_exists(argv[0])) {
1763eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("bookmark '%s' "
1764eda14cbcSMatt Macy 			    "does not exist.\n"), argv[0]);
1765eda14cbcSMatt Macy 			return (1);
1766eda14cbcSMatt Macy 		}
1767eda14cbcSMatt Macy 
1768eda14cbcSMatt Macy 		nvl = fnvlist_alloc();
1769eda14cbcSMatt Macy 		fnvlist_add_boolean(nvl, argv[0]);
1770eda14cbcSMatt Macy 
1771eda14cbcSMatt Macy 		err = lzc_destroy_bookmarks(nvl, NULL);
1772eda14cbcSMatt Macy 		if (err != 0) {
1773eda14cbcSMatt Macy 			(void) zfs_standard_error(g_zfs, err,
1774eda14cbcSMatt Macy 			    "cannot destroy bookmark");
1775eda14cbcSMatt Macy 		}
1776eda14cbcSMatt Macy 
1777eda14cbcSMatt Macy 		nvlist_free(nvl);
1778eda14cbcSMatt Macy 
1779eda14cbcSMatt Macy 		return (err);
1780eda14cbcSMatt Macy 	} else {
1781eda14cbcSMatt Macy 		/* Open the given dataset */
1782eda14cbcSMatt Macy 		if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1783eda14cbcSMatt Macy 			return (1);
1784eda14cbcSMatt Macy 
1785eda14cbcSMatt Macy 		cb.cb_target = zhp;
1786eda14cbcSMatt Macy 
1787eda14cbcSMatt Macy 		/*
1788eda14cbcSMatt Macy 		 * Perform an explicit check for pools before going any further.
1789eda14cbcSMatt Macy 		 */
1790eda14cbcSMatt Macy 		if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1791eda14cbcSMatt Macy 		    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1792eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("cannot destroy '%s': "
1793eda14cbcSMatt Macy 			    "operation does not apply to pools\n"),
1794eda14cbcSMatt Macy 			    zfs_get_name(zhp));
1795eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("use 'zfs destroy -r "
1796eda14cbcSMatt Macy 			    "%s' to destroy all datasets in the pool\n"),
1797eda14cbcSMatt Macy 			    zfs_get_name(zhp));
1798eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1799eda14cbcSMatt Macy 			    "to destroy the pool itself\n"), zfs_get_name(zhp));
1800eda14cbcSMatt Macy 			rv = 1;
1801eda14cbcSMatt Macy 			goto out;
1802eda14cbcSMatt Macy 		}
1803eda14cbcSMatt Macy 
1804eda14cbcSMatt Macy 		/*
1805eda14cbcSMatt Macy 		 * Check for any dependents and/or clones.
1806eda14cbcSMatt Macy 		 */
1807eda14cbcSMatt Macy 		cb.cb_first = B_TRUE;
1808eda14cbcSMatt Macy 		if (!cb.cb_doclones &&
1809eda14cbcSMatt Macy 		    zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1810eda14cbcSMatt Macy 		    &cb) != 0) {
1811eda14cbcSMatt Macy 			rv = 1;
1812eda14cbcSMatt Macy 			goto out;
1813eda14cbcSMatt Macy 		}
1814eda14cbcSMatt Macy 
1815eda14cbcSMatt Macy 		if (cb.cb_error) {
1816eda14cbcSMatt Macy 			rv = 1;
1817eda14cbcSMatt Macy 			goto out;
1818eda14cbcSMatt Macy 		}
1819eda14cbcSMatt Macy 		cb.cb_batchedsnaps = fnvlist_alloc();
1820eda14cbcSMatt Macy 		if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1821eda14cbcSMatt Macy 		    &cb) != 0) {
1822eda14cbcSMatt Macy 			rv = 1;
1823eda14cbcSMatt Macy 			goto out;
1824eda14cbcSMatt Macy 		}
1825eda14cbcSMatt Macy 
1826eda14cbcSMatt Macy 		/*
1827eda14cbcSMatt Macy 		 * Do the real thing.  The callback will close the
1828eda14cbcSMatt Macy 		 * handle regardless of whether it succeeds or not.
1829eda14cbcSMatt Macy 		 */
1830eda14cbcSMatt Macy 		err = destroy_callback(zhp, &cb);
1831eda14cbcSMatt Macy 		zhp = NULL;
1832eda14cbcSMatt Macy 		if (err == 0) {
1833eda14cbcSMatt Macy 			err = zfs_destroy_snaps_nvl(g_zfs,
1834eda14cbcSMatt Macy 			    cb.cb_batchedsnaps, cb.cb_defer_destroy);
1835eda14cbcSMatt Macy 		}
1836eda14cbcSMatt Macy 		if (err != 0 || cb.cb_error == B_TRUE)
1837eda14cbcSMatt Macy 			rv = 1;
1838eda14cbcSMatt Macy 	}
1839eda14cbcSMatt Macy 
1840eda14cbcSMatt Macy out:
1841eda14cbcSMatt Macy 	fnvlist_free(cb.cb_batchedsnaps);
1842eda14cbcSMatt Macy 	fnvlist_free(cb.cb_nvl);
1843eda14cbcSMatt Macy 	if (zhp != NULL)
1844eda14cbcSMatt Macy 		zfs_close(zhp);
1845eda14cbcSMatt Macy 	return (rv);
1846eda14cbcSMatt Macy }
1847eda14cbcSMatt Macy 
1848eda14cbcSMatt Macy static boolean_t
1849eda14cbcSMatt Macy is_recvd_column(zprop_get_cbdata_t *cbp)
1850eda14cbcSMatt Macy {
1851eda14cbcSMatt Macy 	int i;
1852eda14cbcSMatt Macy 	zfs_get_column_t col;
1853eda14cbcSMatt Macy 
1854eda14cbcSMatt Macy 	for (i = 0; i < ZFS_GET_NCOLS &&
1855eda14cbcSMatt Macy 	    (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1856eda14cbcSMatt Macy 		if (col == GET_COL_RECVD)
1857eda14cbcSMatt Macy 			return (B_TRUE);
1858eda14cbcSMatt Macy 	return (B_FALSE);
1859eda14cbcSMatt Macy }
1860eda14cbcSMatt Macy 
1861eda14cbcSMatt Macy /*
1862eda14cbcSMatt Macy  * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1863eda14cbcSMatt Macy  *	< all | property[,property]... > < fs | snap | vol > ...
1864eda14cbcSMatt Macy  *
1865eda14cbcSMatt Macy  *	-r	recurse over any child datasets
1866eda14cbcSMatt Macy  *	-H	scripted mode.  Headers are stripped, and fields are separated
1867eda14cbcSMatt Macy  *		by tabs instead of spaces.
1868eda14cbcSMatt Macy  *	-o	Set of fields to display.  One of "name,property,value,
1869eda14cbcSMatt Macy  *		received,source". Default is "name,property,value,source".
1870eda14cbcSMatt Macy  *		"all" is an alias for all five.
1871eda14cbcSMatt Macy  *	-s	Set of sources to allow.  One of
1872eda14cbcSMatt Macy  *		"local,default,inherited,received,temporary,none".  Default is
1873eda14cbcSMatt Macy  *		all six.
1874eda14cbcSMatt Macy  *	-p	Display values in parsable (literal) format.
1875eda14cbcSMatt Macy  *
1876eda14cbcSMatt Macy  *  Prints properties for the given datasets.  The user can control which
1877eda14cbcSMatt Macy  *  columns to display as well as which property types to allow.
1878eda14cbcSMatt Macy  */
1879eda14cbcSMatt Macy 
1880eda14cbcSMatt Macy /*
1881eda14cbcSMatt Macy  * Invoked to display the properties for a single dataset.
1882eda14cbcSMatt Macy  */
1883eda14cbcSMatt Macy static int
1884eda14cbcSMatt Macy get_callback(zfs_handle_t *zhp, void *data)
1885eda14cbcSMatt Macy {
1886eda14cbcSMatt Macy 	char buf[ZFS_MAXPROPLEN];
1887eda14cbcSMatt Macy 	char rbuf[ZFS_MAXPROPLEN];
1888eda14cbcSMatt Macy 	zprop_source_t sourcetype;
1889eda14cbcSMatt Macy 	char source[ZFS_MAX_DATASET_NAME_LEN];
1890eda14cbcSMatt Macy 	zprop_get_cbdata_t *cbp = data;
1891eda14cbcSMatt Macy 	nvlist_t *user_props = zfs_get_user_props(zhp);
1892eda14cbcSMatt Macy 	zprop_list_t *pl = cbp->cb_proplist;
1893eda14cbcSMatt Macy 	nvlist_t *propval;
1894eda14cbcSMatt Macy 	char *strval;
1895eda14cbcSMatt Macy 	char *sourceval;
1896eda14cbcSMatt Macy 	boolean_t received = is_recvd_column(cbp);
1897eda14cbcSMatt Macy 
1898eda14cbcSMatt Macy 	for (; pl != NULL; pl = pl->pl_next) {
1899eda14cbcSMatt Macy 		char *recvdval = NULL;
1900eda14cbcSMatt Macy 		/*
1901eda14cbcSMatt Macy 		 * Skip the special fake placeholder.  This will also skip over
1902eda14cbcSMatt Macy 		 * the name property when 'all' is specified.
1903eda14cbcSMatt Macy 		 */
1904eda14cbcSMatt Macy 		if (pl->pl_prop == ZFS_PROP_NAME &&
1905eda14cbcSMatt Macy 		    pl == cbp->cb_proplist)
1906eda14cbcSMatt Macy 			continue;
1907eda14cbcSMatt Macy 
1908eda14cbcSMatt Macy 		if (pl->pl_prop != ZPROP_INVAL) {
1909eda14cbcSMatt Macy 			if (zfs_prop_get(zhp, pl->pl_prop, buf,
1910eda14cbcSMatt Macy 			    sizeof (buf), &sourcetype, source,
1911eda14cbcSMatt Macy 			    sizeof (source),
1912eda14cbcSMatt Macy 			    cbp->cb_literal) != 0) {
1913eda14cbcSMatt Macy 				if (pl->pl_all)
1914eda14cbcSMatt Macy 					continue;
1915eda14cbcSMatt Macy 				if (!zfs_prop_valid_for_type(pl->pl_prop,
1916eda14cbcSMatt Macy 				    ZFS_TYPE_DATASET, B_FALSE)) {
1917eda14cbcSMatt Macy 					(void) fprintf(stderr,
1918eda14cbcSMatt Macy 					    gettext("No such property '%s'\n"),
1919eda14cbcSMatt Macy 					    zfs_prop_to_name(pl->pl_prop));
1920eda14cbcSMatt Macy 					continue;
1921eda14cbcSMatt Macy 				}
1922eda14cbcSMatt Macy 				sourcetype = ZPROP_SRC_NONE;
1923eda14cbcSMatt Macy 				(void) strlcpy(buf, "-", sizeof (buf));
1924eda14cbcSMatt Macy 			}
1925eda14cbcSMatt Macy 
1926eda14cbcSMatt Macy 			if (received && (zfs_prop_get_recvd(zhp,
1927eda14cbcSMatt Macy 			    zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1928eda14cbcSMatt Macy 			    cbp->cb_literal) == 0))
1929eda14cbcSMatt Macy 				recvdval = rbuf;
1930eda14cbcSMatt Macy 
1931eda14cbcSMatt Macy 			zprop_print_one_property(zfs_get_name(zhp), cbp,
1932eda14cbcSMatt Macy 			    zfs_prop_to_name(pl->pl_prop),
1933eda14cbcSMatt Macy 			    buf, sourcetype, source, recvdval);
1934eda14cbcSMatt Macy 		} else if (zfs_prop_userquota(pl->pl_user_prop)) {
1935eda14cbcSMatt Macy 			sourcetype = ZPROP_SRC_LOCAL;
1936eda14cbcSMatt Macy 
1937eda14cbcSMatt Macy 			if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1938eda14cbcSMatt Macy 			    buf, sizeof (buf), cbp->cb_literal) != 0) {
1939eda14cbcSMatt Macy 				sourcetype = ZPROP_SRC_NONE;
1940eda14cbcSMatt Macy 				(void) strlcpy(buf, "-", sizeof (buf));
1941eda14cbcSMatt Macy 			}
1942eda14cbcSMatt Macy 
1943eda14cbcSMatt Macy 			zprop_print_one_property(zfs_get_name(zhp), cbp,
1944eda14cbcSMatt Macy 			    pl->pl_user_prop, buf, sourcetype, source, NULL);
1945eda14cbcSMatt Macy 		} else if (zfs_prop_written(pl->pl_user_prop)) {
1946eda14cbcSMatt Macy 			sourcetype = ZPROP_SRC_LOCAL;
1947eda14cbcSMatt Macy 
1948eda14cbcSMatt Macy 			if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1949eda14cbcSMatt Macy 			    buf, sizeof (buf), cbp->cb_literal) != 0) {
1950eda14cbcSMatt Macy 				sourcetype = ZPROP_SRC_NONE;
1951eda14cbcSMatt Macy 				(void) strlcpy(buf, "-", sizeof (buf));
1952eda14cbcSMatt Macy 			}
1953eda14cbcSMatt Macy 
1954eda14cbcSMatt Macy 			zprop_print_one_property(zfs_get_name(zhp), cbp,
1955eda14cbcSMatt Macy 			    pl->pl_user_prop, buf, sourcetype, source, NULL);
1956eda14cbcSMatt Macy 		} else {
1957eda14cbcSMatt Macy 			if (nvlist_lookup_nvlist(user_props,
1958eda14cbcSMatt Macy 			    pl->pl_user_prop, &propval) != 0) {
1959eda14cbcSMatt Macy 				if (pl->pl_all)
1960eda14cbcSMatt Macy 					continue;
1961eda14cbcSMatt Macy 				sourcetype = ZPROP_SRC_NONE;
1962eda14cbcSMatt Macy 				strval = "-";
1963eda14cbcSMatt Macy 			} else {
1964eda14cbcSMatt Macy 				verify(nvlist_lookup_string(propval,
1965eda14cbcSMatt Macy 				    ZPROP_VALUE, &strval) == 0);
1966eda14cbcSMatt Macy 				verify(nvlist_lookup_string(propval,
1967eda14cbcSMatt Macy 				    ZPROP_SOURCE, &sourceval) == 0);
1968eda14cbcSMatt Macy 
1969eda14cbcSMatt Macy 				if (strcmp(sourceval,
1970eda14cbcSMatt Macy 				    zfs_get_name(zhp)) == 0) {
1971eda14cbcSMatt Macy 					sourcetype = ZPROP_SRC_LOCAL;
1972eda14cbcSMatt Macy 				} else if (strcmp(sourceval,
1973eda14cbcSMatt Macy 				    ZPROP_SOURCE_VAL_RECVD) == 0) {
1974eda14cbcSMatt Macy 					sourcetype = ZPROP_SRC_RECEIVED;
1975eda14cbcSMatt Macy 				} else {
1976eda14cbcSMatt Macy 					sourcetype = ZPROP_SRC_INHERITED;
1977eda14cbcSMatt Macy 					(void) strlcpy(source,
1978eda14cbcSMatt Macy 					    sourceval, sizeof (source));
1979eda14cbcSMatt Macy 				}
1980eda14cbcSMatt Macy 			}
1981eda14cbcSMatt Macy 
1982eda14cbcSMatt Macy 			if (received && (zfs_prop_get_recvd(zhp,
1983eda14cbcSMatt Macy 			    pl->pl_user_prop, rbuf, sizeof (rbuf),
1984eda14cbcSMatt Macy 			    cbp->cb_literal) == 0))
1985eda14cbcSMatt Macy 				recvdval = rbuf;
1986eda14cbcSMatt Macy 
1987eda14cbcSMatt Macy 			zprop_print_one_property(zfs_get_name(zhp), cbp,
1988eda14cbcSMatt Macy 			    pl->pl_user_prop, strval, sourcetype,
1989eda14cbcSMatt Macy 			    source, recvdval);
1990eda14cbcSMatt Macy 		}
1991eda14cbcSMatt Macy 	}
1992eda14cbcSMatt Macy 
1993eda14cbcSMatt Macy 	return (0);
1994eda14cbcSMatt Macy }
1995eda14cbcSMatt Macy 
1996eda14cbcSMatt Macy static int
1997eda14cbcSMatt Macy zfs_do_get(int argc, char **argv)
1998eda14cbcSMatt Macy {
1999eda14cbcSMatt Macy 	zprop_get_cbdata_t cb = { 0 };
2000eda14cbcSMatt Macy 	int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2001eda14cbcSMatt Macy 	int types = ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK;
2002eda14cbcSMatt Macy 	char *value, *fields;
2003eda14cbcSMatt Macy 	int ret = 0;
2004eda14cbcSMatt Macy 	int limit = 0;
2005eda14cbcSMatt Macy 	zprop_list_t fake_name = { 0 };
2006eda14cbcSMatt Macy 
2007eda14cbcSMatt Macy 	/*
2008eda14cbcSMatt Macy 	 * Set up default columns and sources.
2009eda14cbcSMatt Macy 	 */
2010eda14cbcSMatt Macy 	cb.cb_sources = ZPROP_SRC_ALL;
2011eda14cbcSMatt Macy 	cb.cb_columns[0] = GET_COL_NAME;
2012eda14cbcSMatt Macy 	cb.cb_columns[1] = GET_COL_PROPERTY;
2013eda14cbcSMatt Macy 	cb.cb_columns[2] = GET_COL_VALUE;
2014eda14cbcSMatt Macy 	cb.cb_columns[3] = GET_COL_SOURCE;
2015eda14cbcSMatt Macy 	cb.cb_type = ZFS_TYPE_DATASET;
2016eda14cbcSMatt Macy 
2017eda14cbcSMatt Macy 	/* check options */
2018eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
2019eda14cbcSMatt Macy 		switch (c) {
2020eda14cbcSMatt Macy 		case 'p':
2021eda14cbcSMatt Macy 			cb.cb_literal = B_TRUE;
2022eda14cbcSMatt Macy 			break;
2023eda14cbcSMatt Macy 		case 'd':
2024eda14cbcSMatt Macy 			limit = parse_depth(optarg, &flags);
2025eda14cbcSMatt Macy 			break;
2026eda14cbcSMatt Macy 		case 'r':
2027eda14cbcSMatt Macy 			flags |= ZFS_ITER_RECURSE;
2028eda14cbcSMatt Macy 			break;
2029eda14cbcSMatt Macy 		case 'H':
2030eda14cbcSMatt Macy 			cb.cb_scripted = B_TRUE;
2031eda14cbcSMatt Macy 			break;
2032eda14cbcSMatt Macy 		case ':':
2033eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("missing argument for "
2034eda14cbcSMatt Macy 			    "'%c' option\n"), optopt);
2035eda14cbcSMatt Macy 			usage(B_FALSE);
2036eda14cbcSMatt Macy 			break;
2037eda14cbcSMatt Macy 		case 'o':
2038eda14cbcSMatt Macy 			/*
2039eda14cbcSMatt Macy 			 * Process the set of columns to display.  We zero out
2040eda14cbcSMatt Macy 			 * the structure to give us a blank slate.
2041eda14cbcSMatt Macy 			 */
2042eda14cbcSMatt Macy 			bzero(&cb.cb_columns, sizeof (cb.cb_columns));
2043eda14cbcSMatt Macy 			i = 0;
2044eda14cbcSMatt Macy 			while (*optarg != '\0') {
2045eda14cbcSMatt Macy 				static char *col_subopts[] =
2046eda14cbcSMatt Macy 				    { "name", "property", "value", "received",
2047eda14cbcSMatt Macy 				    "source", "all", NULL };
2048eda14cbcSMatt Macy 
2049eda14cbcSMatt Macy 				if (i == ZFS_GET_NCOLS) {
2050eda14cbcSMatt Macy 					(void) fprintf(stderr, gettext("too "
2051eda14cbcSMatt Macy 					    "many fields given to -o "
2052eda14cbcSMatt Macy 					    "option\n"));
2053eda14cbcSMatt Macy 					usage(B_FALSE);
2054eda14cbcSMatt Macy 				}
2055eda14cbcSMatt Macy 
2056eda14cbcSMatt Macy 				switch (getsubopt(&optarg, col_subopts,
2057eda14cbcSMatt Macy 				    &value)) {
2058eda14cbcSMatt Macy 				case 0:
2059eda14cbcSMatt Macy 					cb.cb_columns[i++] = GET_COL_NAME;
2060eda14cbcSMatt Macy 					break;
2061eda14cbcSMatt Macy 				case 1:
2062eda14cbcSMatt Macy 					cb.cb_columns[i++] = GET_COL_PROPERTY;
2063eda14cbcSMatt Macy 					break;
2064eda14cbcSMatt Macy 				case 2:
2065eda14cbcSMatt Macy 					cb.cb_columns[i++] = GET_COL_VALUE;
2066eda14cbcSMatt Macy 					break;
2067eda14cbcSMatt Macy 				case 3:
2068eda14cbcSMatt Macy 					cb.cb_columns[i++] = GET_COL_RECVD;
2069eda14cbcSMatt Macy 					flags |= ZFS_ITER_RECVD_PROPS;
2070eda14cbcSMatt Macy 					break;
2071eda14cbcSMatt Macy 				case 4:
2072eda14cbcSMatt Macy 					cb.cb_columns[i++] = GET_COL_SOURCE;
2073eda14cbcSMatt Macy 					break;
2074eda14cbcSMatt Macy 				case 5:
2075eda14cbcSMatt Macy 					if (i > 0) {
2076eda14cbcSMatt Macy 						(void) fprintf(stderr,
2077eda14cbcSMatt Macy 						    gettext("\"all\" conflicts "
2078eda14cbcSMatt Macy 						    "with specific fields "
2079eda14cbcSMatt Macy 						    "given to -o option\n"));
2080eda14cbcSMatt Macy 						usage(B_FALSE);
2081eda14cbcSMatt Macy 					}
2082eda14cbcSMatt Macy 					cb.cb_columns[0] = GET_COL_NAME;
2083eda14cbcSMatt Macy 					cb.cb_columns[1] = GET_COL_PROPERTY;
2084eda14cbcSMatt Macy 					cb.cb_columns[2] = GET_COL_VALUE;
2085eda14cbcSMatt Macy 					cb.cb_columns[3] = GET_COL_RECVD;
2086eda14cbcSMatt Macy 					cb.cb_columns[4] = GET_COL_SOURCE;
2087eda14cbcSMatt Macy 					flags |= ZFS_ITER_RECVD_PROPS;
2088eda14cbcSMatt Macy 					i = ZFS_GET_NCOLS;
2089eda14cbcSMatt Macy 					break;
2090eda14cbcSMatt Macy 				default:
2091eda14cbcSMatt Macy 					(void) fprintf(stderr,
2092eda14cbcSMatt Macy 					    gettext("invalid column name "
2093eda14cbcSMatt Macy 					    "'%s'\n"), value);
2094eda14cbcSMatt Macy 					usage(B_FALSE);
2095eda14cbcSMatt Macy 				}
2096eda14cbcSMatt Macy 			}
2097eda14cbcSMatt Macy 			break;
2098eda14cbcSMatt Macy 
2099eda14cbcSMatt Macy 		case 's':
2100eda14cbcSMatt Macy 			cb.cb_sources = 0;
2101eda14cbcSMatt Macy 			while (*optarg != '\0') {
2102eda14cbcSMatt Macy 				static char *source_subopts[] = {
2103eda14cbcSMatt Macy 					"local", "default", "inherited",
2104eda14cbcSMatt Macy 					"received", "temporary", "none",
2105eda14cbcSMatt Macy 					NULL };
2106eda14cbcSMatt Macy 
2107eda14cbcSMatt Macy 				switch (getsubopt(&optarg, source_subopts,
2108eda14cbcSMatt Macy 				    &value)) {
2109eda14cbcSMatt Macy 				case 0:
2110eda14cbcSMatt Macy 					cb.cb_sources |= ZPROP_SRC_LOCAL;
2111eda14cbcSMatt Macy 					break;
2112eda14cbcSMatt Macy 				case 1:
2113eda14cbcSMatt Macy 					cb.cb_sources |= ZPROP_SRC_DEFAULT;
2114eda14cbcSMatt Macy 					break;
2115eda14cbcSMatt Macy 				case 2:
2116eda14cbcSMatt Macy 					cb.cb_sources |= ZPROP_SRC_INHERITED;
2117eda14cbcSMatt Macy 					break;
2118eda14cbcSMatt Macy 				case 3:
2119eda14cbcSMatt Macy 					cb.cb_sources |= ZPROP_SRC_RECEIVED;
2120eda14cbcSMatt Macy 					break;
2121eda14cbcSMatt Macy 				case 4:
2122eda14cbcSMatt Macy 					cb.cb_sources |= ZPROP_SRC_TEMPORARY;
2123eda14cbcSMatt Macy 					break;
2124eda14cbcSMatt Macy 				case 5:
2125eda14cbcSMatt Macy 					cb.cb_sources |= ZPROP_SRC_NONE;
2126eda14cbcSMatt Macy 					break;
2127eda14cbcSMatt Macy 				default:
2128eda14cbcSMatt Macy 					(void) fprintf(stderr,
2129eda14cbcSMatt Macy 					    gettext("invalid source "
2130eda14cbcSMatt Macy 					    "'%s'\n"), value);
2131eda14cbcSMatt Macy 					usage(B_FALSE);
2132eda14cbcSMatt Macy 				}
2133eda14cbcSMatt Macy 			}
2134eda14cbcSMatt Macy 			break;
2135eda14cbcSMatt Macy 
2136eda14cbcSMatt Macy 		case 't':
2137eda14cbcSMatt Macy 			types = 0;
2138eda14cbcSMatt Macy 			flags &= ~ZFS_ITER_PROP_LISTSNAPS;
2139eda14cbcSMatt Macy 			while (*optarg != '\0') {
2140eda14cbcSMatt Macy 				static char *type_subopts[] = { "filesystem",
2141eda14cbcSMatt Macy 				    "volume", "snapshot", "snap", "bookmark",
2142eda14cbcSMatt Macy 				    "all", NULL };
2143eda14cbcSMatt Macy 
2144eda14cbcSMatt Macy 				switch (getsubopt(&optarg, type_subopts,
2145eda14cbcSMatt Macy 				    &value)) {
2146eda14cbcSMatt Macy 				case 0:
2147eda14cbcSMatt Macy 					types |= ZFS_TYPE_FILESYSTEM;
2148eda14cbcSMatt Macy 					break;
2149eda14cbcSMatt Macy 				case 1:
2150eda14cbcSMatt Macy 					types |= ZFS_TYPE_VOLUME;
2151eda14cbcSMatt Macy 					break;
2152eda14cbcSMatt Macy 				case 2:
2153eda14cbcSMatt Macy 				case 3:
2154eda14cbcSMatt Macy 					types |= ZFS_TYPE_SNAPSHOT;
2155eda14cbcSMatt Macy 					break;
2156eda14cbcSMatt Macy 				case 4:
2157eda14cbcSMatt Macy 					types |= ZFS_TYPE_BOOKMARK;
2158eda14cbcSMatt Macy 					break;
2159eda14cbcSMatt Macy 				case 5:
2160eda14cbcSMatt Macy 					types = ZFS_TYPE_DATASET |
2161eda14cbcSMatt Macy 					    ZFS_TYPE_BOOKMARK;
2162eda14cbcSMatt Macy 					break;
2163eda14cbcSMatt Macy 
2164eda14cbcSMatt Macy 				default:
2165eda14cbcSMatt Macy 					(void) fprintf(stderr,
2166eda14cbcSMatt Macy 					    gettext("invalid type '%s'\n"),
2167eda14cbcSMatt Macy 					    value);
2168eda14cbcSMatt Macy 					usage(B_FALSE);
2169eda14cbcSMatt Macy 				}
2170eda14cbcSMatt Macy 			}
2171eda14cbcSMatt Macy 			break;
2172eda14cbcSMatt Macy 
2173eda14cbcSMatt Macy 		case '?':
2174eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2175eda14cbcSMatt Macy 			    optopt);
2176eda14cbcSMatt Macy 			usage(B_FALSE);
2177eda14cbcSMatt Macy 		}
2178eda14cbcSMatt Macy 	}
2179eda14cbcSMatt Macy 
2180eda14cbcSMatt Macy 	argc -= optind;
2181eda14cbcSMatt Macy 	argv += optind;
2182eda14cbcSMatt Macy 
2183eda14cbcSMatt Macy 	if (argc < 1) {
2184eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing property "
2185eda14cbcSMatt Macy 		    "argument\n"));
2186eda14cbcSMatt Macy 		usage(B_FALSE);
2187eda14cbcSMatt Macy 	}
2188eda14cbcSMatt Macy 
2189eda14cbcSMatt Macy 	fields = argv[0];
2190eda14cbcSMatt Macy 
2191eda14cbcSMatt Macy 	/*
2192eda14cbcSMatt Macy 	 * Handle users who want to get all snapshots or bookmarks
2193eda14cbcSMatt Macy 	 * of a dataset (ex. 'zfs get -t snapshot refer <dataset>').
2194eda14cbcSMatt Macy 	 */
2195eda14cbcSMatt Macy 	if ((types == ZFS_TYPE_SNAPSHOT || types == ZFS_TYPE_BOOKMARK) &&
2196eda14cbcSMatt Macy 	    argc > 1 && (flags & ZFS_ITER_RECURSE) == 0 && limit == 0) {
2197eda14cbcSMatt Macy 		flags |= (ZFS_ITER_DEPTH_LIMIT | ZFS_ITER_RECURSE);
2198eda14cbcSMatt Macy 		limit = 1;
2199eda14cbcSMatt Macy 	}
2200eda14cbcSMatt Macy 
2201eda14cbcSMatt Macy 	if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
2202eda14cbcSMatt Macy 	    != 0)
2203eda14cbcSMatt Macy 		usage(B_FALSE);
2204eda14cbcSMatt Macy 
2205eda14cbcSMatt Macy 	argc--;
2206eda14cbcSMatt Macy 	argv++;
2207eda14cbcSMatt Macy 
2208eda14cbcSMatt Macy 	/*
2209eda14cbcSMatt Macy 	 * As part of zfs_expand_proplist(), we keep track of the maximum column
2210eda14cbcSMatt Macy 	 * width for each property.  For the 'NAME' (and 'SOURCE') columns, we
2211eda14cbcSMatt Macy 	 * need to know the maximum name length.  However, the user likely did
2212eda14cbcSMatt Macy 	 * not specify 'name' as one of the properties to fetch, so we need to
2213eda14cbcSMatt Macy 	 * make sure we always include at least this property for
2214eda14cbcSMatt Macy 	 * print_get_headers() to work properly.
2215eda14cbcSMatt Macy 	 */
2216eda14cbcSMatt Macy 	if (cb.cb_proplist != NULL) {
2217eda14cbcSMatt Macy 		fake_name.pl_prop = ZFS_PROP_NAME;
2218eda14cbcSMatt Macy 		fake_name.pl_width = strlen(gettext("NAME"));
2219eda14cbcSMatt Macy 		fake_name.pl_next = cb.cb_proplist;
2220eda14cbcSMatt Macy 		cb.cb_proplist = &fake_name;
2221eda14cbcSMatt Macy 	}
2222eda14cbcSMatt Macy 
2223eda14cbcSMatt Macy 	cb.cb_first = B_TRUE;
2224eda14cbcSMatt Macy 
2225eda14cbcSMatt Macy 	/* run for each object */
2226eda14cbcSMatt Macy 	ret = zfs_for_each(argc, argv, flags, types, NULL,
2227eda14cbcSMatt Macy 	    &cb.cb_proplist, limit, get_callback, &cb);
2228eda14cbcSMatt Macy 
2229eda14cbcSMatt Macy 	if (cb.cb_proplist == &fake_name)
2230eda14cbcSMatt Macy 		zprop_free_list(fake_name.pl_next);
2231eda14cbcSMatt Macy 	else
2232eda14cbcSMatt Macy 		zprop_free_list(cb.cb_proplist);
2233eda14cbcSMatt Macy 
2234eda14cbcSMatt Macy 	return (ret);
2235eda14cbcSMatt Macy }
2236eda14cbcSMatt Macy 
2237eda14cbcSMatt Macy /*
2238eda14cbcSMatt Macy  * inherit [-rS] <property> <fs|vol> ...
2239eda14cbcSMatt Macy  *
2240eda14cbcSMatt Macy  *	-r	Recurse over all children
2241eda14cbcSMatt Macy  *	-S	Revert to received value, if any
2242eda14cbcSMatt Macy  *
2243eda14cbcSMatt Macy  * For each dataset specified on the command line, inherit the given property
2244eda14cbcSMatt Macy  * from its parent.  Inheriting a property at the pool level will cause it to
2245eda14cbcSMatt Macy  * use the default value.  The '-r' flag will recurse over all children, and is
2246eda14cbcSMatt Macy  * useful for setting a property on a hierarchy-wide basis, regardless of any
2247eda14cbcSMatt Macy  * local modifications for each dataset.
2248eda14cbcSMatt Macy  */
2249eda14cbcSMatt Macy 
2250eda14cbcSMatt Macy typedef struct inherit_cbdata {
2251eda14cbcSMatt Macy 	const char *cb_propname;
2252eda14cbcSMatt Macy 	boolean_t cb_received;
2253eda14cbcSMatt Macy } inherit_cbdata_t;
2254eda14cbcSMatt Macy 
2255eda14cbcSMatt Macy static int
2256eda14cbcSMatt Macy inherit_recurse_cb(zfs_handle_t *zhp, void *data)
2257eda14cbcSMatt Macy {
2258eda14cbcSMatt Macy 	inherit_cbdata_t *cb = data;
2259eda14cbcSMatt Macy 	zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
2260eda14cbcSMatt Macy 
2261eda14cbcSMatt Macy 	/*
2262eda14cbcSMatt Macy 	 * If we're doing it recursively, then ignore properties that
2263eda14cbcSMatt Macy 	 * are not valid for this type of dataset.
2264eda14cbcSMatt Macy 	 */
2265eda14cbcSMatt Macy 	if (prop != ZPROP_INVAL &&
2266eda14cbcSMatt Macy 	    !zfs_prop_valid_for_type(prop, zfs_get_type(zhp), B_FALSE))
2267eda14cbcSMatt Macy 		return (0);
2268eda14cbcSMatt Macy 
2269eda14cbcSMatt Macy 	return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
2270eda14cbcSMatt Macy }
2271eda14cbcSMatt Macy 
2272eda14cbcSMatt Macy static int
2273eda14cbcSMatt Macy inherit_cb(zfs_handle_t *zhp, void *data)
2274eda14cbcSMatt Macy {
2275eda14cbcSMatt Macy 	inherit_cbdata_t *cb = data;
2276eda14cbcSMatt Macy 
2277eda14cbcSMatt Macy 	return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
2278eda14cbcSMatt Macy }
2279eda14cbcSMatt Macy 
2280eda14cbcSMatt Macy static int
2281eda14cbcSMatt Macy zfs_do_inherit(int argc, char **argv)
2282eda14cbcSMatt Macy {
2283eda14cbcSMatt Macy 	int c;
2284eda14cbcSMatt Macy 	zfs_prop_t prop;
2285eda14cbcSMatt Macy 	inherit_cbdata_t cb = { 0 };
2286eda14cbcSMatt Macy 	char *propname;
2287eda14cbcSMatt Macy 	int ret = 0;
2288eda14cbcSMatt Macy 	int flags = 0;
2289eda14cbcSMatt Macy 	boolean_t received = B_FALSE;
2290eda14cbcSMatt Macy 
2291eda14cbcSMatt Macy 	/* check options */
2292eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, "rS")) != -1) {
2293eda14cbcSMatt Macy 		switch (c) {
2294eda14cbcSMatt Macy 		case 'r':
2295eda14cbcSMatt Macy 			flags |= ZFS_ITER_RECURSE;
2296eda14cbcSMatt Macy 			break;
2297eda14cbcSMatt Macy 		case 'S':
2298eda14cbcSMatt Macy 			received = B_TRUE;
2299eda14cbcSMatt Macy 			break;
2300eda14cbcSMatt Macy 		case '?':
2301eda14cbcSMatt Macy 		default:
2302eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2303eda14cbcSMatt Macy 			    optopt);
2304eda14cbcSMatt Macy 			usage(B_FALSE);
2305eda14cbcSMatt Macy 		}
2306eda14cbcSMatt Macy 	}
2307eda14cbcSMatt Macy 
2308eda14cbcSMatt Macy 	argc -= optind;
2309eda14cbcSMatt Macy 	argv += optind;
2310eda14cbcSMatt Macy 
2311eda14cbcSMatt Macy 	/* check number of arguments */
2312eda14cbcSMatt Macy 	if (argc < 1) {
2313eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing property argument\n"));
2314eda14cbcSMatt Macy 		usage(B_FALSE);
2315eda14cbcSMatt Macy 	}
2316eda14cbcSMatt Macy 	if (argc < 2) {
2317eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing dataset argument\n"));
2318eda14cbcSMatt Macy 		usage(B_FALSE);
2319eda14cbcSMatt Macy 	}
2320eda14cbcSMatt Macy 
2321eda14cbcSMatt Macy 	propname = argv[0];
2322eda14cbcSMatt Macy 	argc--;
2323eda14cbcSMatt Macy 	argv++;
2324eda14cbcSMatt Macy 
2325eda14cbcSMatt Macy 	if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
2326eda14cbcSMatt Macy 		if (zfs_prop_readonly(prop)) {
2327eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext(
2328eda14cbcSMatt Macy 			    "%s property is read-only\n"),
2329eda14cbcSMatt Macy 			    propname);
2330eda14cbcSMatt Macy 			return (1);
2331eda14cbcSMatt Macy 		}
2332eda14cbcSMatt Macy 		if (!zfs_prop_inheritable(prop) && !received) {
2333eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("'%s' property cannot "
2334eda14cbcSMatt Macy 			    "be inherited\n"), propname);
2335eda14cbcSMatt Macy 			if (prop == ZFS_PROP_QUOTA ||
2336eda14cbcSMatt Macy 			    prop == ZFS_PROP_RESERVATION ||
2337eda14cbcSMatt Macy 			    prop == ZFS_PROP_REFQUOTA ||
2338eda14cbcSMatt Macy 			    prop == ZFS_PROP_REFRESERVATION) {
2339eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("use 'zfs set "
2340eda14cbcSMatt Macy 				    "%s=none' to clear\n"), propname);
2341eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("use 'zfs "
2342eda14cbcSMatt Macy 				    "inherit -S %s' to revert to received "
2343eda14cbcSMatt Macy 				    "value\n"), propname);
2344eda14cbcSMatt Macy 			}
2345eda14cbcSMatt Macy 			return (1);
2346eda14cbcSMatt Macy 		}
2347eda14cbcSMatt Macy 		if (received && (prop == ZFS_PROP_VOLSIZE ||
2348eda14cbcSMatt Macy 		    prop == ZFS_PROP_VERSION)) {
2349eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("'%s' property cannot "
2350eda14cbcSMatt Macy 			    "be reverted to a received value\n"), propname);
2351eda14cbcSMatt Macy 			return (1);
2352eda14cbcSMatt Macy 		}
2353eda14cbcSMatt Macy 	} else if (!zfs_prop_user(propname)) {
2354eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("invalid property '%s'\n"),
2355eda14cbcSMatt Macy 		    propname);
2356eda14cbcSMatt Macy 		usage(B_FALSE);
2357eda14cbcSMatt Macy 	}
2358eda14cbcSMatt Macy 
2359eda14cbcSMatt Macy 	cb.cb_propname = propname;
2360eda14cbcSMatt Macy 	cb.cb_received = received;
2361eda14cbcSMatt Macy 
2362eda14cbcSMatt Macy 	if (flags & ZFS_ITER_RECURSE) {
2363eda14cbcSMatt Macy 		ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2364eda14cbcSMatt Macy 		    NULL, NULL, 0, inherit_recurse_cb, &cb);
2365eda14cbcSMatt Macy 	} else {
2366eda14cbcSMatt Macy 		ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2367eda14cbcSMatt Macy 		    NULL, NULL, 0, inherit_cb, &cb);
2368eda14cbcSMatt Macy 	}
2369eda14cbcSMatt Macy 
2370eda14cbcSMatt Macy 	return (ret);
2371eda14cbcSMatt Macy }
2372eda14cbcSMatt Macy 
2373eda14cbcSMatt Macy typedef struct upgrade_cbdata {
2374eda14cbcSMatt Macy 	uint64_t cb_numupgraded;
2375eda14cbcSMatt Macy 	uint64_t cb_numsamegraded;
2376eda14cbcSMatt Macy 	uint64_t cb_numfailed;
2377eda14cbcSMatt Macy 	uint64_t cb_version;
2378eda14cbcSMatt Macy 	boolean_t cb_newer;
2379eda14cbcSMatt Macy 	boolean_t cb_foundone;
2380eda14cbcSMatt Macy 	char cb_lastfs[ZFS_MAX_DATASET_NAME_LEN];
2381eda14cbcSMatt Macy } upgrade_cbdata_t;
2382eda14cbcSMatt Macy 
2383eda14cbcSMatt Macy static int
2384eda14cbcSMatt Macy same_pool(zfs_handle_t *zhp, const char *name)
2385eda14cbcSMatt Macy {
2386eda14cbcSMatt Macy 	int len1 = strcspn(name, "/@");
2387eda14cbcSMatt Macy 	const char *zhname = zfs_get_name(zhp);
2388eda14cbcSMatt Macy 	int len2 = strcspn(zhname, "/@");
2389eda14cbcSMatt Macy 
2390eda14cbcSMatt Macy 	if (len1 != len2)
2391eda14cbcSMatt Macy 		return (B_FALSE);
2392eda14cbcSMatt Macy 	return (strncmp(name, zhname, len1) == 0);
2393eda14cbcSMatt Macy }
2394eda14cbcSMatt Macy 
2395eda14cbcSMatt Macy static int
2396eda14cbcSMatt Macy upgrade_list_callback(zfs_handle_t *zhp, void *data)
2397eda14cbcSMatt Macy {
2398eda14cbcSMatt Macy 	upgrade_cbdata_t *cb = data;
2399eda14cbcSMatt Macy 	int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2400eda14cbcSMatt Macy 
2401eda14cbcSMatt Macy 	/* list if it's old/new */
2402eda14cbcSMatt Macy 	if ((!cb->cb_newer && version < ZPL_VERSION) ||
2403eda14cbcSMatt Macy 	    (cb->cb_newer && version > ZPL_VERSION)) {
2404eda14cbcSMatt Macy 		char *str;
2405eda14cbcSMatt Macy 		if (cb->cb_newer) {
2406eda14cbcSMatt Macy 			str = gettext("The following filesystems are "
2407eda14cbcSMatt Macy 			    "formatted using a newer software version and\n"
2408eda14cbcSMatt Macy 			    "cannot be accessed on the current system.\n\n");
2409eda14cbcSMatt Macy 		} else {
2410eda14cbcSMatt Macy 			str = gettext("The following filesystems are "
2411eda14cbcSMatt Macy 			    "out of date, and can be upgraded.  After being\n"
2412eda14cbcSMatt Macy 			    "upgraded, these filesystems (and any 'zfs send' "
2413eda14cbcSMatt Macy 			    "streams generated from\n"
2414eda14cbcSMatt Macy 			    "subsequent snapshots) will no longer be "
2415eda14cbcSMatt Macy 			    "accessible by older software versions.\n\n");
2416eda14cbcSMatt Macy 		}
2417eda14cbcSMatt Macy 
2418eda14cbcSMatt Macy 		if (!cb->cb_foundone) {
2419eda14cbcSMatt Macy 			(void) puts(str);
2420eda14cbcSMatt Macy 			(void) printf(gettext("VER  FILESYSTEM\n"));
2421eda14cbcSMatt Macy 			(void) printf(gettext("---  ------------\n"));
2422eda14cbcSMatt Macy 			cb->cb_foundone = B_TRUE;
2423eda14cbcSMatt Macy 		}
2424eda14cbcSMatt Macy 
2425eda14cbcSMatt Macy 		(void) printf("%2u   %s\n", version, zfs_get_name(zhp));
2426eda14cbcSMatt Macy 	}
2427eda14cbcSMatt Macy 
2428eda14cbcSMatt Macy 	return (0);
2429eda14cbcSMatt Macy }
2430eda14cbcSMatt Macy 
2431eda14cbcSMatt Macy static int
2432eda14cbcSMatt Macy upgrade_set_callback(zfs_handle_t *zhp, void *data)
2433eda14cbcSMatt Macy {
2434eda14cbcSMatt Macy 	upgrade_cbdata_t *cb = data;
2435eda14cbcSMatt Macy 	int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2436eda14cbcSMatt Macy 	int needed_spa_version;
2437eda14cbcSMatt Macy 	int spa_version;
2438eda14cbcSMatt Macy 
2439eda14cbcSMatt Macy 	if (zfs_spa_version(zhp, &spa_version) < 0)
2440eda14cbcSMatt Macy 		return (-1);
2441eda14cbcSMatt Macy 
2442eda14cbcSMatt Macy 	needed_spa_version = zfs_spa_version_map(cb->cb_version);
2443eda14cbcSMatt Macy 
2444eda14cbcSMatt Macy 	if (needed_spa_version < 0)
2445eda14cbcSMatt Macy 		return (-1);
2446eda14cbcSMatt Macy 
2447eda14cbcSMatt Macy 	if (spa_version < needed_spa_version) {
2448eda14cbcSMatt Macy 		/* can't upgrade */
2449eda14cbcSMatt Macy 		(void) printf(gettext("%s: can not be "
2450eda14cbcSMatt Macy 		    "upgraded; the pool version needs to first "
2451eda14cbcSMatt Macy 		    "be upgraded\nto version %d\n\n"),
2452eda14cbcSMatt Macy 		    zfs_get_name(zhp), needed_spa_version);
2453eda14cbcSMatt Macy 		cb->cb_numfailed++;
2454eda14cbcSMatt Macy 		return (0);
2455eda14cbcSMatt Macy 	}
2456eda14cbcSMatt Macy 
2457eda14cbcSMatt Macy 	/* upgrade */
2458eda14cbcSMatt Macy 	if (version < cb->cb_version) {
2459eda14cbcSMatt Macy 		char verstr[16];
2460eda14cbcSMatt Macy 		(void) snprintf(verstr, sizeof (verstr),
2461eda14cbcSMatt Macy 		    "%llu", (u_longlong_t)cb->cb_version);
2462eda14cbcSMatt Macy 		if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2463eda14cbcSMatt Macy 			/*
2464eda14cbcSMatt Macy 			 * If they did "zfs upgrade -a", then we could
2465eda14cbcSMatt Macy 			 * be doing ioctls to different pools.  We need
2466eda14cbcSMatt Macy 			 * to log this history once to each pool, and bypass
2467eda14cbcSMatt Macy 			 * the normal history logging that happens in main().
2468eda14cbcSMatt Macy 			 */
2469eda14cbcSMatt Macy 			(void) zpool_log_history(g_zfs, history_str);
2470eda14cbcSMatt Macy 			log_history = B_FALSE;
2471eda14cbcSMatt Macy 		}
2472eda14cbcSMatt Macy 		if (zfs_prop_set(zhp, "version", verstr) == 0)
2473eda14cbcSMatt Macy 			cb->cb_numupgraded++;
2474eda14cbcSMatt Macy 		else
2475eda14cbcSMatt Macy 			cb->cb_numfailed++;
2476eda14cbcSMatt Macy 		(void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2477eda14cbcSMatt Macy 	} else if (version > cb->cb_version) {
2478eda14cbcSMatt Macy 		/* can't downgrade */
2479eda14cbcSMatt Macy 		(void) printf(gettext("%s: can not be downgraded; "
2480eda14cbcSMatt Macy 		    "it is already at version %u\n"),
2481eda14cbcSMatt Macy 		    zfs_get_name(zhp), version);
2482eda14cbcSMatt Macy 		cb->cb_numfailed++;
2483eda14cbcSMatt Macy 	} else {
2484eda14cbcSMatt Macy 		cb->cb_numsamegraded++;
2485eda14cbcSMatt Macy 	}
2486eda14cbcSMatt Macy 	return (0);
2487eda14cbcSMatt Macy }
2488eda14cbcSMatt Macy 
2489eda14cbcSMatt Macy /*
2490eda14cbcSMatt Macy  * zfs upgrade
2491eda14cbcSMatt Macy  * zfs upgrade -v
2492eda14cbcSMatt Macy  * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2493eda14cbcSMatt Macy  */
2494eda14cbcSMatt Macy static int
2495eda14cbcSMatt Macy zfs_do_upgrade(int argc, char **argv)
2496eda14cbcSMatt Macy {
2497eda14cbcSMatt Macy 	boolean_t all = B_FALSE;
2498eda14cbcSMatt Macy 	boolean_t showversions = B_FALSE;
2499eda14cbcSMatt Macy 	int ret = 0;
2500eda14cbcSMatt Macy 	upgrade_cbdata_t cb = { 0 };
2501eda14cbcSMatt Macy 	int c;
2502eda14cbcSMatt Macy 	int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2503eda14cbcSMatt Macy 
2504eda14cbcSMatt Macy 	/* check options */
2505eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2506eda14cbcSMatt Macy 		switch (c) {
2507eda14cbcSMatt Macy 		case 'r':
2508eda14cbcSMatt Macy 			flags |= ZFS_ITER_RECURSE;
2509eda14cbcSMatt Macy 			break;
2510eda14cbcSMatt Macy 		case 'v':
2511eda14cbcSMatt Macy 			showversions = B_TRUE;
2512eda14cbcSMatt Macy 			break;
2513eda14cbcSMatt Macy 		case 'V':
2514eda14cbcSMatt Macy 			if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2515eda14cbcSMatt Macy 			    optarg, &cb.cb_version) != 0) {
2516eda14cbcSMatt Macy 				(void) fprintf(stderr,
2517eda14cbcSMatt Macy 				    gettext("invalid version %s\n"), optarg);
2518eda14cbcSMatt Macy 				usage(B_FALSE);
2519eda14cbcSMatt Macy 			}
2520eda14cbcSMatt Macy 			break;
2521eda14cbcSMatt Macy 		case 'a':
2522eda14cbcSMatt Macy 			all = B_TRUE;
2523eda14cbcSMatt Macy 			break;
2524eda14cbcSMatt Macy 		case '?':
2525eda14cbcSMatt Macy 		default:
2526eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2527eda14cbcSMatt Macy 			    optopt);
2528eda14cbcSMatt Macy 			usage(B_FALSE);
2529eda14cbcSMatt Macy 		}
2530eda14cbcSMatt Macy 	}
2531eda14cbcSMatt Macy 
2532eda14cbcSMatt Macy 	argc -= optind;
2533eda14cbcSMatt Macy 	argv += optind;
2534eda14cbcSMatt Macy 
2535eda14cbcSMatt Macy 	if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2536eda14cbcSMatt Macy 		usage(B_FALSE);
2537eda14cbcSMatt Macy 	if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2538eda14cbcSMatt Macy 	    cb.cb_version || argc))
2539eda14cbcSMatt Macy 		usage(B_FALSE);
2540eda14cbcSMatt Macy 	if ((all || argc) && (showversions))
2541eda14cbcSMatt Macy 		usage(B_FALSE);
2542eda14cbcSMatt Macy 	if (all && argc)
2543eda14cbcSMatt Macy 		usage(B_FALSE);
2544eda14cbcSMatt Macy 
2545eda14cbcSMatt Macy 	if (showversions) {
2546eda14cbcSMatt Macy 		/* Show info on available versions. */
2547eda14cbcSMatt Macy 		(void) printf(gettext("The following filesystem versions are "
2548eda14cbcSMatt Macy 		    "supported:\n\n"));
2549eda14cbcSMatt Macy 		(void) printf(gettext("VER  DESCRIPTION\n"));
2550eda14cbcSMatt Macy 		(void) printf("---  -----------------------------------------"
2551eda14cbcSMatt Macy 		    "---------------\n");
2552eda14cbcSMatt Macy 		(void) printf(gettext(" 1   Initial ZFS filesystem version\n"));
2553eda14cbcSMatt Macy 		(void) printf(gettext(" 2   Enhanced directory entries\n"));
2554eda14cbcSMatt Macy 		(void) printf(gettext(" 3   Case insensitive and filesystem "
2555eda14cbcSMatt Macy 		    "user identifier (FUID)\n"));
2556eda14cbcSMatt Macy 		(void) printf(gettext(" 4   userquota, groupquota "
2557eda14cbcSMatt Macy 		    "properties\n"));
2558eda14cbcSMatt Macy 		(void) printf(gettext(" 5   System attributes\n"));
2559eda14cbcSMatt Macy 		(void) printf(gettext("\nFor more information on a particular "
2560eda14cbcSMatt Macy 		    "version, including supported releases,\n"));
2561eda14cbcSMatt Macy 		(void) printf("see the ZFS Administration Guide.\n\n");
2562eda14cbcSMatt Macy 		ret = 0;
2563eda14cbcSMatt Macy 	} else if (argc || all) {
2564eda14cbcSMatt Macy 		/* Upgrade filesystems */
2565eda14cbcSMatt Macy 		if (cb.cb_version == 0)
2566eda14cbcSMatt Macy 			cb.cb_version = ZPL_VERSION;
2567eda14cbcSMatt Macy 		ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2568eda14cbcSMatt Macy 		    NULL, NULL, 0, upgrade_set_callback, &cb);
2569eda14cbcSMatt Macy 		(void) printf(gettext("%llu filesystems upgraded\n"),
2570eda14cbcSMatt Macy 		    (u_longlong_t)cb.cb_numupgraded);
2571eda14cbcSMatt Macy 		if (cb.cb_numsamegraded) {
2572eda14cbcSMatt Macy 			(void) printf(gettext("%llu filesystems already at "
2573eda14cbcSMatt Macy 			    "this version\n"),
2574eda14cbcSMatt Macy 			    (u_longlong_t)cb.cb_numsamegraded);
2575eda14cbcSMatt Macy 		}
2576eda14cbcSMatt Macy 		if (cb.cb_numfailed != 0)
2577eda14cbcSMatt Macy 			ret = 1;
2578eda14cbcSMatt Macy 	} else {
2579eda14cbcSMatt Macy 		/* List old-version filesystems */
2580eda14cbcSMatt Macy 		boolean_t found;
2581eda14cbcSMatt Macy 		(void) printf(gettext("This system is currently running "
2582eda14cbcSMatt Macy 		    "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2583eda14cbcSMatt Macy 
2584eda14cbcSMatt Macy 		flags |= ZFS_ITER_RECURSE;
2585eda14cbcSMatt Macy 		ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2586eda14cbcSMatt Macy 		    NULL, NULL, 0, upgrade_list_callback, &cb);
2587eda14cbcSMatt Macy 
2588eda14cbcSMatt Macy 		found = cb.cb_foundone;
2589eda14cbcSMatt Macy 		cb.cb_foundone = B_FALSE;
2590eda14cbcSMatt Macy 		cb.cb_newer = B_TRUE;
2591eda14cbcSMatt Macy 
2592eda14cbcSMatt Macy 		ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2593eda14cbcSMatt Macy 		    NULL, NULL, 0, upgrade_list_callback, &cb);
2594eda14cbcSMatt Macy 
2595eda14cbcSMatt Macy 		if (!cb.cb_foundone && !found) {
2596eda14cbcSMatt Macy 			(void) printf(gettext("All filesystems are "
2597eda14cbcSMatt Macy 			    "formatted with the current version.\n"));
2598eda14cbcSMatt Macy 		}
2599eda14cbcSMatt Macy 	}
2600eda14cbcSMatt Macy 
2601eda14cbcSMatt Macy 	return (ret);
2602eda14cbcSMatt Macy }
2603eda14cbcSMatt Macy 
2604eda14cbcSMatt Macy /*
2605eda14cbcSMatt Macy  * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2606c40487d4SMatt Macy  *               [-S field [-S field]...] [-t type[,...]]
2607c40487d4SMatt Macy  *               filesystem | snapshot | path
2608eda14cbcSMatt Macy  * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2609c40487d4SMatt Macy  *                [-S field [-S field]...] [-t type[,...]]
2610c40487d4SMatt Macy  *                filesystem | snapshot | path
2611eda14cbcSMatt Macy  * zfs projectspace [-Hp] [-o field[,...]] [-s field [-s field]...]
2612c40487d4SMatt Macy  *                [-S field [-S field]...] filesystem | snapshot | path
2613eda14cbcSMatt Macy  *
2614eda14cbcSMatt Macy  *	-H      Scripted mode; elide headers and separate columns by tabs.
2615eda14cbcSMatt Macy  *	-i	Translate SID to POSIX ID.
2616eda14cbcSMatt Macy  *	-n	Print numeric ID instead of user/group name.
2617eda14cbcSMatt Macy  *	-o      Control which fields to display.
2618eda14cbcSMatt Macy  *	-p	Use exact (parsable) numeric output.
2619eda14cbcSMatt Macy  *	-s      Specify sort columns, descending order.
2620eda14cbcSMatt Macy  *	-S      Specify sort columns, ascending order.
2621eda14cbcSMatt Macy  *	-t      Control which object types to display.
2622eda14cbcSMatt Macy  *
2623eda14cbcSMatt Macy  *	Displays space consumed by, and quotas on, each user in the specified
2624eda14cbcSMatt Macy  *	filesystem or snapshot.
2625eda14cbcSMatt Macy  */
2626eda14cbcSMatt Macy 
2627eda14cbcSMatt Macy /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2628eda14cbcSMatt Macy enum us_field_types {
2629eda14cbcSMatt Macy 	USFIELD_TYPE,
2630eda14cbcSMatt Macy 	USFIELD_NAME,
2631eda14cbcSMatt Macy 	USFIELD_USED,
2632eda14cbcSMatt Macy 	USFIELD_QUOTA,
2633eda14cbcSMatt Macy 	USFIELD_OBJUSED,
2634eda14cbcSMatt Macy 	USFIELD_OBJQUOTA
2635eda14cbcSMatt Macy };
2636eda14cbcSMatt Macy static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA",
2637eda14cbcSMatt Macy 				    "OBJUSED", "OBJQUOTA" };
2638eda14cbcSMatt Macy static char *us_field_names[] = { "type", "name", "used", "quota",
2639eda14cbcSMatt Macy 				    "objused", "objquota" };
2640eda14cbcSMatt Macy #define	USFIELD_LAST	(sizeof (us_field_names) / sizeof (char *))
2641eda14cbcSMatt Macy 
2642eda14cbcSMatt Macy #define	USTYPE_PSX_GRP	(1 << 0)
2643eda14cbcSMatt Macy #define	USTYPE_PSX_USR	(1 << 1)
2644eda14cbcSMatt Macy #define	USTYPE_SMB_GRP	(1 << 2)
2645eda14cbcSMatt Macy #define	USTYPE_SMB_USR	(1 << 3)
2646eda14cbcSMatt Macy #define	USTYPE_PROJ	(1 << 4)
2647eda14cbcSMatt Macy #define	USTYPE_ALL	\
2648eda14cbcSMatt Macy 	(USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR | \
2649eda14cbcSMatt Macy 	    USTYPE_PROJ)
2650eda14cbcSMatt Macy 
2651eda14cbcSMatt Macy static int us_type_bits[] = {
2652eda14cbcSMatt Macy 	USTYPE_PSX_GRP,
2653eda14cbcSMatt Macy 	USTYPE_PSX_USR,
2654eda14cbcSMatt Macy 	USTYPE_SMB_GRP,
2655eda14cbcSMatt Macy 	USTYPE_SMB_USR,
2656eda14cbcSMatt Macy 	USTYPE_ALL
2657eda14cbcSMatt Macy };
2658eda14cbcSMatt Macy static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2659eda14cbcSMatt Macy 	"smbuser", "all" };
2660eda14cbcSMatt Macy 
2661eda14cbcSMatt Macy typedef struct us_node {
2662eda14cbcSMatt Macy 	nvlist_t	*usn_nvl;
2663eda14cbcSMatt Macy 	uu_avl_node_t	usn_avlnode;
2664eda14cbcSMatt Macy 	uu_list_node_t	usn_listnode;
2665eda14cbcSMatt Macy } us_node_t;
2666eda14cbcSMatt Macy 
2667eda14cbcSMatt Macy typedef struct us_cbdata {
2668eda14cbcSMatt Macy 	nvlist_t	**cb_nvlp;
2669eda14cbcSMatt Macy 	uu_avl_pool_t	*cb_avl_pool;
2670eda14cbcSMatt Macy 	uu_avl_t	*cb_avl;
2671eda14cbcSMatt Macy 	boolean_t	cb_numname;
2672eda14cbcSMatt Macy 	boolean_t	cb_nicenum;
2673eda14cbcSMatt Macy 	boolean_t	cb_sid2posix;
2674eda14cbcSMatt Macy 	zfs_userquota_prop_t cb_prop;
2675eda14cbcSMatt Macy 	zfs_sort_column_t *cb_sortcol;
2676eda14cbcSMatt Macy 	size_t		cb_width[USFIELD_LAST];
2677eda14cbcSMatt Macy } us_cbdata_t;
2678eda14cbcSMatt Macy 
2679eda14cbcSMatt Macy static boolean_t us_populated = B_FALSE;
2680eda14cbcSMatt Macy 
2681eda14cbcSMatt Macy typedef struct {
2682eda14cbcSMatt Macy 	zfs_sort_column_t *si_sortcol;
2683eda14cbcSMatt Macy 	boolean_t	si_numname;
2684eda14cbcSMatt Macy } us_sort_info_t;
2685eda14cbcSMatt Macy 
2686eda14cbcSMatt Macy static int
2687eda14cbcSMatt Macy us_field_index(char *field)
2688eda14cbcSMatt Macy {
2689eda14cbcSMatt Macy 	int i;
2690eda14cbcSMatt Macy 
2691eda14cbcSMatt Macy 	for (i = 0; i < USFIELD_LAST; i++) {
2692eda14cbcSMatt Macy 		if (strcmp(field, us_field_names[i]) == 0)
2693eda14cbcSMatt Macy 			return (i);
2694eda14cbcSMatt Macy 	}
2695eda14cbcSMatt Macy 
2696eda14cbcSMatt Macy 	return (-1);
2697eda14cbcSMatt Macy }
2698eda14cbcSMatt Macy 
2699eda14cbcSMatt Macy static int
2700eda14cbcSMatt Macy us_compare(const void *larg, const void *rarg, void *unused)
2701eda14cbcSMatt Macy {
2702eda14cbcSMatt Macy 	const us_node_t *l = larg;
2703eda14cbcSMatt Macy 	const us_node_t *r = rarg;
2704eda14cbcSMatt Macy 	us_sort_info_t *si = (us_sort_info_t *)unused;
2705eda14cbcSMatt Macy 	zfs_sort_column_t *sortcol = si->si_sortcol;
2706eda14cbcSMatt Macy 	boolean_t numname = si->si_numname;
2707eda14cbcSMatt Macy 	nvlist_t *lnvl = l->usn_nvl;
2708eda14cbcSMatt Macy 	nvlist_t *rnvl = r->usn_nvl;
2709eda14cbcSMatt Macy 	int rc = 0;
2710eda14cbcSMatt Macy 	boolean_t lvb, rvb;
2711eda14cbcSMatt Macy 
2712eda14cbcSMatt Macy 	for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2713eda14cbcSMatt Macy 		char *lvstr = "";
2714eda14cbcSMatt Macy 		char *rvstr = "";
2715eda14cbcSMatt Macy 		uint32_t lv32 = 0;
2716eda14cbcSMatt Macy 		uint32_t rv32 = 0;
2717eda14cbcSMatt Macy 		uint64_t lv64 = 0;
2718eda14cbcSMatt Macy 		uint64_t rv64 = 0;
2719eda14cbcSMatt Macy 		zfs_prop_t prop = sortcol->sc_prop;
2720eda14cbcSMatt Macy 		const char *propname = NULL;
2721eda14cbcSMatt Macy 		boolean_t reverse = sortcol->sc_reverse;
2722eda14cbcSMatt Macy 
2723eda14cbcSMatt Macy 		switch (prop) {
2724eda14cbcSMatt Macy 		case ZFS_PROP_TYPE:
2725eda14cbcSMatt Macy 			propname = "type";
2726eda14cbcSMatt Macy 			(void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2727eda14cbcSMatt Macy 			(void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2728eda14cbcSMatt Macy 			if (rv32 != lv32)
2729eda14cbcSMatt Macy 				rc = (rv32 < lv32) ? 1 : -1;
2730eda14cbcSMatt Macy 			break;
2731eda14cbcSMatt Macy 		case ZFS_PROP_NAME:
2732eda14cbcSMatt Macy 			propname = "name";
2733eda14cbcSMatt Macy 			if (numname) {
2734eda14cbcSMatt Macy compare_nums:
2735eda14cbcSMatt Macy 				(void) nvlist_lookup_uint64(lnvl, propname,
2736eda14cbcSMatt Macy 				    &lv64);
2737eda14cbcSMatt Macy 				(void) nvlist_lookup_uint64(rnvl, propname,
2738eda14cbcSMatt Macy 				    &rv64);
2739eda14cbcSMatt Macy 				if (rv64 != lv64)
2740eda14cbcSMatt Macy 					rc = (rv64 < lv64) ? 1 : -1;
2741eda14cbcSMatt Macy 			} else {
2742eda14cbcSMatt Macy 				if ((nvlist_lookup_string(lnvl, propname,
2743eda14cbcSMatt Macy 				    &lvstr) == ENOENT) ||
2744eda14cbcSMatt Macy 				    (nvlist_lookup_string(rnvl, propname,
2745eda14cbcSMatt Macy 				    &rvstr) == ENOENT)) {
2746eda14cbcSMatt Macy 					goto compare_nums;
2747eda14cbcSMatt Macy 				}
2748eda14cbcSMatt Macy 				rc = strcmp(lvstr, rvstr);
2749eda14cbcSMatt Macy 			}
2750eda14cbcSMatt Macy 			break;
2751eda14cbcSMatt Macy 		case ZFS_PROP_USED:
2752eda14cbcSMatt Macy 		case ZFS_PROP_QUOTA:
2753eda14cbcSMatt Macy 			if (!us_populated)
2754eda14cbcSMatt Macy 				break;
2755eda14cbcSMatt Macy 			if (prop == ZFS_PROP_USED)
2756eda14cbcSMatt Macy 				propname = "used";
2757eda14cbcSMatt Macy 			else
2758eda14cbcSMatt Macy 				propname = "quota";
2759eda14cbcSMatt Macy 			(void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2760eda14cbcSMatt Macy 			(void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2761eda14cbcSMatt Macy 			if (rv64 != lv64)
2762eda14cbcSMatt Macy 				rc = (rv64 < lv64) ? 1 : -1;
2763eda14cbcSMatt Macy 			break;
2764eda14cbcSMatt Macy 
2765eda14cbcSMatt Macy 		default:
2766eda14cbcSMatt Macy 			break;
2767eda14cbcSMatt Macy 		}
2768eda14cbcSMatt Macy 
2769eda14cbcSMatt Macy 		if (rc != 0) {
2770eda14cbcSMatt Macy 			if (rc < 0)
2771eda14cbcSMatt Macy 				return (reverse ? 1 : -1);
2772eda14cbcSMatt Macy 			else
2773eda14cbcSMatt Macy 				return (reverse ? -1 : 1);
2774eda14cbcSMatt Macy 		}
2775eda14cbcSMatt Macy 	}
2776eda14cbcSMatt Macy 
2777eda14cbcSMatt Macy 	/*
2778eda14cbcSMatt Macy 	 * If entries still seem to be the same, check if they are of the same
2779eda14cbcSMatt Macy 	 * type (smbentity is added only if we are doing SID to POSIX ID
2780eda14cbcSMatt Macy 	 * translation where we can have duplicate type/name combinations).
2781eda14cbcSMatt Macy 	 */
2782eda14cbcSMatt Macy 	if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2783eda14cbcSMatt Macy 	    nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2784eda14cbcSMatt Macy 	    lvb != rvb)
2785eda14cbcSMatt Macy 		return (lvb < rvb ? -1 : 1);
2786eda14cbcSMatt Macy 
2787eda14cbcSMatt Macy 	return (0);
2788eda14cbcSMatt Macy }
2789eda14cbcSMatt Macy 
2790eda14cbcSMatt Macy static boolean_t
2791eda14cbcSMatt Macy zfs_prop_is_user(unsigned p)
2792eda14cbcSMatt Macy {
2793eda14cbcSMatt Macy 	return (p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA ||
2794eda14cbcSMatt Macy 	    p == ZFS_PROP_USEROBJUSED || p == ZFS_PROP_USEROBJQUOTA);
2795eda14cbcSMatt Macy }
2796eda14cbcSMatt Macy 
2797eda14cbcSMatt Macy static boolean_t
2798eda14cbcSMatt Macy zfs_prop_is_group(unsigned p)
2799eda14cbcSMatt Macy {
2800eda14cbcSMatt Macy 	return (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA ||
2801eda14cbcSMatt Macy 	    p == ZFS_PROP_GROUPOBJUSED || p == ZFS_PROP_GROUPOBJQUOTA);
2802eda14cbcSMatt Macy }
2803eda14cbcSMatt Macy 
2804eda14cbcSMatt Macy static boolean_t
2805eda14cbcSMatt Macy zfs_prop_is_project(unsigned p)
2806eda14cbcSMatt Macy {
2807eda14cbcSMatt Macy 	return (p == ZFS_PROP_PROJECTUSED || p == ZFS_PROP_PROJECTQUOTA ||
2808eda14cbcSMatt Macy 	    p == ZFS_PROP_PROJECTOBJUSED || p == ZFS_PROP_PROJECTOBJQUOTA);
2809eda14cbcSMatt Macy }
2810eda14cbcSMatt Macy 
2811eda14cbcSMatt Macy static inline const char *
2812eda14cbcSMatt Macy us_type2str(unsigned field_type)
2813eda14cbcSMatt Macy {
2814eda14cbcSMatt Macy 	switch (field_type) {
2815eda14cbcSMatt Macy 	case USTYPE_PSX_USR:
2816eda14cbcSMatt Macy 		return ("POSIX User");
2817eda14cbcSMatt Macy 	case USTYPE_PSX_GRP:
2818eda14cbcSMatt Macy 		return ("POSIX Group");
2819eda14cbcSMatt Macy 	case USTYPE_SMB_USR:
2820eda14cbcSMatt Macy 		return ("SMB User");
2821eda14cbcSMatt Macy 	case USTYPE_SMB_GRP:
2822eda14cbcSMatt Macy 		return ("SMB Group");
2823eda14cbcSMatt Macy 	case USTYPE_PROJ:
2824eda14cbcSMatt Macy 		return ("Project");
2825eda14cbcSMatt Macy 	default:
2826eda14cbcSMatt Macy 		return ("Undefined");
2827eda14cbcSMatt Macy 	}
2828eda14cbcSMatt Macy }
2829eda14cbcSMatt Macy 
2830eda14cbcSMatt Macy static int
2831eda14cbcSMatt Macy userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2832eda14cbcSMatt Macy {
2833eda14cbcSMatt Macy 	us_cbdata_t *cb = (us_cbdata_t *)arg;
2834eda14cbcSMatt Macy 	zfs_userquota_prop_t prop = cb->cb_prop;
2835eda14cbcSMatt Macy 	char *name = NULL;
2836eda14cbcSMatt Macy 	char *propname;
2837eda14cbcSMatt Macy 	char sizebuf[32];
2838eda14cbcSMatt Macy 	us_node_t *node;
2839eda14cbcSMatt Macy 	uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2840eda14cbcSMatt Macy 	uu_avl_t *avl = cb->cb_avl;
2841eda14cbcSMatt Macy 	uu_avl_index_t idx;
2842eda14cbcSMatt Macy 	nvlist_t *props;
2843eda14cbcSMatt Macy 	us_node_t *n;
2844eda14cbcSMatt Macy 	zfs_sort_column_t *sortcol = cb->cb_sortcol;
2845eda14cbcSMatt Macy 	unsigned type = 0;
2846eda14cbcSMatt Macy 	const char *typestr;
2847eda14cbcSMatt Macy 	size_t namelen;
2848eda14cbcSMatt Macy 	size_t typelen;
2849eda14cbcSMatt Macy 	size_t sizelen;
2850eda14cbcSMatt Macy 	int typeidx, nameidx, sizeidx;
2851eda14cbcSMatt Macy 	us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2852eda14cbcSMatt Macy 	boolean_t smbentity = B_FALSE;
2853eda14cbcSMatt Macy 
2854eda14cbcSMatt Macy 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2855eda14cbcSMatt Macy 		nomem();
2856eda14cbcSMatt Macy 	node = safe_malloc(sizeof (us_node_t));
2857eda14cbcSMatt Macy 	uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2858eda14cbcSMatt Macy 	node->usn_nvl = props;
2859eda14cbcSMatt Macy 
2860eda14cbcSMatt Macy 	if (domain != NULL && domain[0] != '\0') {
2861eda14cbcSMatt Macy #ifdef HAVE_IDMAP
2862eda14cbcSMatt Macy 		/* SMB */
2863eda14cbcSMatt Macy 		char sid[MAXNAMELEN + 32];
2864eda14cbcSMatt Macy 		uid_t id;
2865eda14cbcSMatt Macy 		uint64_t classes;
2866eda14cbcSMatt Macy 		int err;
2867eda14cbcSMatt Macy 		directory_error_t e;
2868eda14cbcSMatt Macy 
2869eda14cbcSMatt Macy 		smbentity = B_TRUE;
2870eda14cbcSMatt Macy 
2871eda14cbcSMatt Macy 		(void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2872eda14cbcSMatt Macy 
2873eda14cbcSMatt Macy 		if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2874eda14cbcSMatt Macy 			type = USTYPE_SMB_GRP;
2875eda14cbcSMatt Macy 			err = sid_to_id(sid, B_FALSE, &id);
2876eda14cbcSMatt Macy 		} else {
2877eda14cbcSMatt Macy 			type = USTYPE_SMB_USR;
2878eda14cbcSMatt Macy 			err = sid_to_id(sid, B_TRUE, &id);
2879eda14cbcSMatt Macy 		}
2880eda14cbcSMatt Macy 
2881eda14cbcSMatt Macy 		if (err == 0) {
2882eda14cbcSMatt Macy 			rid = id;
2883eda14cbcSMatt Macy 			if (!cb->cb_sid2posix) {
2884eda14cbcSMatt Macy 				e = directory_name_from_sid(NULL, sid, &name,
2885eda14cbcSMatt Macy 				    &classes);
2886eda14cbcSMatt Macy 				if (e != NULL)
2887eda14cbcSMatt Macy 					directory_error_free(e);
2888eda14cbcSMatt Macy 				if (name == NULL)
2889eda14cbcSMatt Macy 					name = sid;
2890eda14cbcSMatt Macy 			}
2891eda14cbcSMatt Macy 		}
2892eda14cbcSMatt Macy #else
2893eda14cbcSMatt Macy 		nvlist_free(props);
2894eda14cbcSMatt Macy 		free(node);
2895eda14cbcSMatt Macy 
2896eda14cbcSMatt Macy 		return (-1);
2897eda14cbcSMatt Macy #endif /* HAVE_IDMAP */
2898eda14cbcSMatt Macy 	}
2899eda14cbcSMatt Macy 
2900eda14cbcSMatt Macy 	if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2901eda14cbcSMatt Macy 		/* POSIX or -i */
2902eda14cbcSMatt Macy 		if (zfs_prop_is_group(prop)) {
2903eda14cbcSMatt Macy 			type = USTYPE_PSX_GRP;
2904eda14cbcSMatt Macy 			if (!cb->cb_numname) {
2905eda14cbcSMatt Macy 				struct group *g;
2906eda14cbcSMatt Macy 
2907eda14cbcSMatt Macy 				if ((g = getgrgid(rid)) != NULL)
2908eda14cbcSMatt Macy 					name = g->gr_name;
2909eda14cbcSMatt Macy 			}
2910eda14cbcSMatt Macy 		} else if (zfs_prop_is_user(prop)) {
2911eda14cbcSMatt Macy 			type = USTYPE_PSX_USR;
2912eda14cbcSMatt Macy 			if (!cb->cb_numname) {
2913eda14cbcSMatt Macy 				struct passwd *p;
2914eda14cbcSMatt Macy 
2915eda14cbcSMatt Macy 				if ((p = getpwuid(rid)) != NULL)
2916eda14cbcSMatt Macy 					name = p->pw_name;
2917eda14cbcSMatt Macy 			}
2918eda14cbcSMatt Macy 		} else {
2919eda14cbcSMatt Macy 			type = USTYPE_PROJ;
2920eda14cbcSMatt Macy 		}
2921eda14cbcSMatt Macy 	}
2922eda14cbcSMatt Macy 
2923eda14cbcSMatt Macy 	/*
2924eda14cbcSMatt Macy 	 * Make sure that the type/name combination is unique when doing
2925eda14cbcSMatt Macy 	 * SID to POSIX ID translation (hence changing the type from SMB to
2926eda14cbcSMatt Macy 	 * POSIX).
2927eda14cbcSMatt Macy 	 */
2928eda14cbcSMatt Macy 	if (cb->cb_sid2posix &&
2929eda14cbcSMatt Macy 	    nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2930eda14cbcSMatt Macy 		nomem();
2931eda14cbcSMatt Macy 
2932eda14cbcSMatt Macy 	/* Calculate/update width of TYPE field */
2933eda14cbcSMatt Macy 	typestr = us_type2str(type);
2934eda14cbcSMatt Macy 	typelen = strlen(gettext(typestr));
2935eda14cbcSMatt Macy 	typeidx = us_field_index("type");
2936eda14cbcSMatt Macy 	if (typelen > cb->cb_width[typeidx])
2937eda14cbcSMatt Macy 		cb->cb_width[typeidx] = typelen;
2938eda14cbcSMatt Macy 	if (nvlist_add_uint32(props, "type", type) != 0)
2939eda14cbcSMatt Macy 		nomem();
2940eda14cbcSMatt Macy 
2941eda14cbcSMatt Macy 	/* Calculate/update width of NAME field */
2942eda14cbcSMatt Macy 	if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2943eda14cbcSMatt Macy 		if (nvlist_add_uint64(props, "name", rid) != 0)
2944eda14cbcSMatt Macy 			nomem();
2945eda14cbcSMatt Macy 		namelen = snprintf(NULL, 0, "%u", rid);
2946eda14cbcSMatt Macy 	} else {
2947eda14cbcSMatt Macy 		if (nvlist_add_string(props, "name", name) != 0)
2948eda14cbcSMatt Macy 			nomem();
2949eda14cbcSMatt Macy 		namelen = strlen(name);
2950eda14cbcSMatt Macy 	}
2951eda14cbcSMatt Macy 	nameidx = us_field_index("name");
2952eda14cbcSMatt Macy 	if (nameidx >= 0 && namelen > cb->cb_width[nameidx])
2953eda14cbcSMatt Macy 		cb->cb_width[nameidx] = namelen;
2954eda14cbcSMatt Macy 
2955eda14cbcSMatt Macy 	/*
2956eda14cbcSMatt Macy 	 * Check if this type/name combination is in the list and update it;
2957eda14cbcSMatt Macy 	 * otherwise add new node to the list.
2958eda14cbcSMatt Macy 	 */
2959eda14cbcSMatt Macy 	if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2960eda14cbcSMatt Macy 		uu_avl_insert(avl, node, idx);
2961eda14cbcSMatt Macy 	} else {
2962eda14cbcSMatt Macy 		nvlist_free(props);
2963eda14cbcSMatt Macy 		free(node);
2964eda14cbcSMatt Macy 		node = n;
2965eda14cbcSMatt Macy 		props = node->usn_nvl;
2966eda14cbcSMatt Macy 	}
2967eda14cbcSMatt Macy 
2968eda14cbcSMatt Macy 	/* Calculate/update width of USED/QUOTA fields */
2969eda14cbcSMatt Macy 	if (cb->cb_nicenum) {
2970eda14cbcSMatt Macy 		if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED ||
2971eda14cbcSMatt Macy 		    prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA ||
2972eda14cbcSMatt Macy 		    prop == ZFS_PROP_PROJECTUSED ||
2973eda14cbcSMatt Macy 		    prop == ZFS_PROP_PROJECTQUOTA) {
2974eda14cbcSMatt Macy 			zfs_nicebytes(space, sizebuf, sizeof (sizebuf));
2975eda14cbcSMatt Macy 		} else {
2976eda14cbcSMatt Macy 			zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2977eda14cbcSMatt Macy 		}
2978eda14cbcSMatt Macy 	} else {
2979eda14cbcSMatt Macy 		(void) snprintf(sizebuf, sizeof (sizebuf), "%llu",
2980eda14cbcSMatt Macy 		    (u_longlong_t)space);
2981eda14cbcSMatt Macy 	}
2982eda14cbcSMatt Macy 	sizelen = strlen(sizebuf);
2983eda14cbcSMatt Macy 	if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED ||
2984eda14cbcSMatt Macy 	    prop == ZFS_PROP_PROJECTUSED) {
2985eda14cbcSMatt Macy 		propname = "used";
2986eda14cbcSMatt Macy 		if (!nvlist_exists(props, "quota"))
2987eda14cbcSMatt Macy 			(void) nvlist_add_uint64(props, "quota", 0);
2988eda14cbcSMatt Macy 	} else if (prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA ||
2989eda14cbcSMatt Macy 	    prop == ZFS_PROP_PROJECTQUOTA) {
2990eda14cbcSMatt Macy 		propname = "quota";
2991eda14cbcSMatt Macy 		if (!nvlist_exists(props, "used"))
2992eda14cbcSMatt Macy 			(void) nvlist_add_uint64(props, "used", 0);
2993eda14cbcSMatt Macy 	} else if (prop == ZFS_PROP_USEROBJUSED ||
2994eda14cbcSMatt Macy 	    prop == ZFS_PROP_GROUPOBJUSED || prop == ZFS_PROP_PROJECTOBJUSED) {
2995eda14cbcSMatt Macy 		propname = "objused";
2996eda14cbcSMatt Macy 		if (!nvlist_exists(props, "objquota"))
2997eda14cbcSMatt Macy 			(void) nvlist_add_uint64(props, "objquota", 0);
2998eda14cbcSMatt Macy 	} else if (prop == ZFS_PROP_USEROBJQUOTA ||
2999eda14cbcSMatt Macy 	    prop == ZFS_PROP_GROUPOBJQUOTA ||
3000eda14cbcSMatt Macy 	    prop == ZFS_PROP_PROJECTOBJQUOTA) {
3001eda14cbcSMatt Macy 		propname = "objquota";
3002eda14cbcSMatt Macy 		if (!nvlist_exists(props, "objused"))
3003eda14cbcSMatt Macy 			(void) nvlist_add_uint64(props, "objused", 0);
3004eda14cbcSMatt Macy 	} else {
3005eda14cbcSMatt Macy 		return (-1);
3006eda14cbcSMatt Macy 	}
3007eda14cbcSMatt Macy 	sizeidx = us_field_index(propname);
3008eda14cbcSMatt Macy 	if (sizeidx >= 0 && sizelen > cb->cb_width[sizeidx])
3009eda14cbcSMatt Macy 		cb->cb_width[sizeidx] = sizelen;
3010eda14cbcSMatt Macy 
3011eda14cbcSMatt Macy 	if (nvlist_add_uint64(props, propname, space) != 0)
3012eda14cbcSMatt Macy 		nomem();
3013eda14cbcSMatt Macy 
3014eda14cbcSMatt Macy 	return (0);
3015eda14cbcSMatt Macy }
3016eda14cbcSMatt Macy 
3017eda14cbcSMatt Macy static void
3018eda14cbcSMatt Macy print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
3019eda14cbcSMatt Macy     size_t *width, us_node_t *node)
3020eda14cbcSMatt Macy {
3021eda14cbcSMatt Macy 	nvlist_t *nvl = node->usn_nvl;
3022eda14cbcSMatt Macy 	char valstr[MAXNAMELEN];
3023eda14cbcSMatt Macy 	boolean_t first = B_TRUE;
3024eda14cbcSMatt Macy 	int cfield = 0;
3025eda14cbcSMatt Macy 	int field;
3026eda14cbcSMatt Macy 	uint32_t ustype;
3027eda14cbcSMatt Macy 
3028eda14cbcSMatt Macy 	/* Check type */
3029eda14cbcSMatt Macy 	(void) nvlist_lookup_uint32(nvl, "type", &ustype);
3030eda14cbcSMatt Macy 	if (!(ustype & types))
3031eda14cbcSMatt Macy 		return;
3032eda14cbcSMatt Macy 
3033eda14cbcSMatt Macy 	while ((field = fields[cfield]) != USFIELD_LAST) {
3034eda14cbcSMatt Macy 		nvpair_t *nvp = NULL;
3035eda14cbcSMatt Macy 		data_type_t type;
3036eda14cbcSMatt Macy 		uint32_t val32;
3037eda14cbcSMatt Macy 		uint64_t val64;
3038eda14cbcSMatt Macy 		char *strval = "-";
3039eda14cbcSMatt Macy 
3040eda14cbcSMatt Macy 		while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
3041eda14cbcSMatt Macy 			if (strcmp(nvpair_name(nvp),
3042eda14cbcSMatt Macy 			    us_field_names[field]) == 0)
3043eda14cbcSMatt Macy 				break;
3044eda14cbcSMatt Macy 		}
3045eda14cbcSMatt Macy 
3046eda14cbcSMatt Macy 		type = nvp == NULL ? DATA_TYPE_UNKNOWN : nvpair_type(nvp);
3047eda14cbcSMatt Macy 		switch (type) {
3048eda14cbcSMatt Macy 		case DATA_TYPE_UINT32:
3049eda14cbcSMatt Macy 			(void) nvpair_value_uint32(nvp, &val32);
3050eda14cbcSMatt Macy 			break;
3051eda14cbcSMatt Macy 		case DATA_TYPE_UINT64:
3052eda14cbcSMatt Macy 			(void) nvpair_value_uint64(nvp, &val64);
3053eda14cbcSMatt Macy 			break;
3054eda14cbcSMatt Macy 		case DATA_TYPE_STRING:
3055eda14cbcSMatt Macy 			(void) nvpair_value_string(nvp, &strval);
3056eda14cbcSMatt Macy 			break;
3057eda14cbcSMatt Macy 		case DATA_TYPE_UNKNOWN:
3058eda14cbcSMatt Macy 			break;
3059eda14cbcSMatt Macy 		default:
3060eda14cbcSMatt Macy 			(void) fprintf(stderr, "invalid data type\n");
3061eda14cbcSMatt Macy 		}
3062eda14cbcSMatt Macy 
3063eda14cbcSMatt Macy 		switch (field) {
3064eda14cbcSMatt Macy 		case USFIELD_TYPE:
3065eda14cbcSMatt Macy 			if (type == DATA_TYPE_UINT32)
3066eda14cbcSMatt Macy 				strval = (char *)us_type2str(val32);
3067eda14cbcSMatt Macy 			break;
3068eda14cbcSMatt Macy 		case USFIELD_NAME:
3069eda14cbcSMatt Macy 			if (type == DATA_TYPE_UINT64) {
3070eda14cbcSMatt Macy 				(void) sprintf(valstr, "%llu",
3071eda14cbcSMatt Macy 				    (u_longlong_t)val64);
3072eda14cbcSMatt Macy 				strval = valstr;
3073eda14cbcSMatt Macy 			}
3074eda14cbcSMatt Macy 			break;
3075eda14cbcSMatt Macy 		case USFIELD_USED:
3076eda14cbcSMatt Macy 		case USFIELD_QUOTA:
3077eda14cbcSMatt Macy 			if (type == DATA_TYPE_UINT64) {
3078eda14cbcSMatt Macy 				if (parsable) {
3079eda14cbcSMatt Macy 					(void) sprintf(valstr, "%llu",
3080eda14cbcSMatt Macy 					    (u_longlong_t)val64);
3081eda14cbcSMatt Macy 					strval = valstr;
3082eda14cbcSMatt Macy 				} else if (field == USFIELD_QUOTA &&
3083eda14cbcSMatt Macy 				    val64 == 0) {
3084eda14cbcSMatt Macy 					strval = "none";
3085eda14cbcSMatt Macy 				} else {
3086eda14cbcSMatt Macy 					zfs_nicebytes(val64, valstr,
3087eda14cbcSMatt Macy 					    sizeof (valstr));
3088eda14cbcSMatt Macy 					strval = valstr;
3089eda14cbcSMatt Macy 				}
3090eda14cbcSMatt Macy 			}
3091eda14cbcSMatt Macy 			break;
3092eda14cbcSMatt Macy 		case USFIELD_OBJUSED:
3093eda14cbcSMatt Macy 		case USFIELD_OBJQUOTA:
3094eda14cbcSMatt Macy 			if (type == DATA_TYPE_UINT64) {
3095eda14cbcSMatt Macy 				if (parsable) {
3096eda14cbcSMatt Macy 					(void) sprintf(valstr, "%llu",
3097eda14cbcSMatt Macy 					    (u_longlong_t)val64);
3098eda14cbcSMatt Macy 					strval = valstr;
3099eda14cbcSMatt Macy 				} else if (field == USFIELD_OBJQUOTA &&
3100eda14cbcSMatt Macy 				    val64 == 0) {
3101eda14cbcSMatt Macy 					strval = "none";
3102eda14cbcSMatt Macy 				} else {
3103eda14cbcSMatt Macy 					zfs_nicenum(val64, valstr,
3104eda14cbcSMatt Macy 					    sizeof (valstr));
3105eda14cbcSMatt Macy 					strval = valstr;
3106eda14cbcSMatt Macy 				}
3107eda14cbcSMatt Macy 			}
3108eda14cbcSMatt Macy 			break;
3109eda14cbcSMatt Macy 		}
3110eda14cbcSMatt Macy 
3111eda14cbcSMatt Macy 		if (!first) {
3112eda14cbcSMatt Macy 			if (scripted)
3113eda14cbcSMatt Macy 				(void) printf("\t");
3114eda14cbcSMatt Macy 			else
3115eda14cbcSMatt Macy 				(void) printf("  ");
3116eda14cbcSMatt Macy 		}
3117eda14cbcSMatt Macy 		if (scripted)
3118eda14cbcSMatt Macy 			(void) printf("%s", strval);
3119eda14cbcSMatt Macy 		else if (field == USFIELD_TYPE || field == USFIELD_NAME)
3120eda14cbcSMatt Macy 			(void) printf("%-*s", (int)width[field], strval);
3121eda14cbcSMatt Macy 		else
3122eda14cbcSMatt Macy 			(void) printf("%*s", (int)width[field], strval);
3123eda14cbcSMatt Macy 
3124eda14cbcSMatt Macy 		first = B_FALSE;
3125eda14cbcSMatt Macy 		cfield++;
3126eda14cbcSMatt Macy 	}
3127eda14cbcSMatt Macy 
3128eda14cbcSMatt Macy 	(void) printf("\n");
3129eda14cbcSMatt Macy }
3130eda14cbcSMatt Macy 
3131eda14cbcSMatt Macy static void
3132eda14cbcSMatt Macy print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
3133eda14cbcSMatt Macy     size_t *width, boolean_t rmnode, uu_avl_t *avl)
3134eda14cbcSMatt Macy {
3135eda14cbcSMatt Macy 	us_node_t *node;
3136eda14cbcSMatt Macy 	const char *col;
3137eda14cbcSMatt Macy 	int cfield = 0;
3138eda14cbcSMatt Macy 	int field;
3139eda14cbcSMatt Macy 
3140eda14cbcSMatt Macy 	if (!scripted) {
3141eda14cbcSMatt Macy 		boolean_t first = B_TRUE;
3142eda14cbcSMatt Macy 
3143eda14cbcSMatt Macy 		while ((field = fields[cfield]) != USFIELD_LAST) {
3144eda14cbcSMatt Macy 			col = gettext(us_field_hdr[field]);
3145eda14cbcSMatt Macy 			if (field == USFIELD_TYPE || field == USFIELD_NAME) {
3146eda14cbcSMatt Macy 				(void) printf(first ? "%-*s" : "  %-*s",
3147eda14cbcSMatt Macy 				    (int)width[field], col);
3148eda14cbcSMatt Macy 			} else {
3149eda14cbcSMatt Macy 				(void) printf(first ? "%*s" : "  %*s",
3150eda14cbcSMatt Macy 				    (int)width[field], col);
3151eda14cbcSMatt Macy 			}
3152eda14cbcSMatt Macy 			first = B_FALSE;
3153eda14cbcSMatt Macy 			cfield++;
3154eda14cbcSMatt Macy 		}
3155eda14cbcSMatt Macy 		(void) printf("\n");
3156eda14cbcSMatt Macy 	}
3157eda14cbcSMatt Macy 
3158eda14cbcSMatt Macy 	for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
3159eda14cbcSMatt Macy 		print_us_node(scripted, parsable, fields, types, width, node);
3160eda14cbcSMatt Macy 		if (rmnode)
3161eda14cbcSMatt Macy 			nvlist_free(node->usn_nvl);
3162eda14cbcSMatt Macy 	}
3163eda14cbcSMatt Macy }
3164eda14cbcSMatt Macy 
3165eda14cbcSMatt Macy static int
3166eda14cbcSMatt Macy zfs_do_userspace(int argc, char **argv)
3167eda14cbcSMatt Macy {
3168eda14cbcSMatt Macy 	zfs_handle_t *zhp;
3169eda14cbcSMatt Macy 	zfs_userquota_prop_t p;
3170eda14cbcSMatt Macy 	uu_avl_pool_t *avl_pool;
3171eda14cbcSMatt Macy 	uu_avl_t *avl_tree;
3172eda14cbcSMatt Macy 	uu_avl_walk_t *walk;
3173eda14cbcSMatt Macy 	char *delim;
3174eda14cbcSMatt Macy 	char deffields[] = "type,name,used,quota,objused,objquota";
3175eda14cbcSMatt Macy 	char *ofield = NULL;
3176eda14cbcSMatt Macy 	char *tfield = NULL;
3177eda14cbcSMatt Macy 	int cfield = 0;
3178eda14cbcSMatt Macy 	int fields[256];
3179eda14cbcSMatt Macy 	int i;
3180eda14cbcSMatt Macy 	boolean_t scripted = B_FALSE;
3181eda14cbcSMatt Macy 	boolean_t prtnum = B_FALSE;
3182eda14cbcSMatt Macy 	boolean_t parsable = B_FALSE;
3183eda14cbcSMatt Macy 	boolean_t sid2posix = B_FALSE;
3184eda14cbcSMatt Macy 	int ret = 0;
3185eda14cbcSMatt Macy 	int c;
3186eda14cbcSMatt Macy 	zfs_sort_column_t *sortcol = NULL;
3187eda14cbcSMatt Macy 	int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
3188eda14cbcSMatt Macy 	us_cbdata_t cb;
3189eda14cbcSMatt Macy 	us_node_t *node;
3190eda14cbcSMatt Macy 	us_node_t *rmnode;
3191eda14cbcSMatt Macy 	uu_list_pool_t *listpool;
3192eda14cbcSMatt Macy 	uu_list_t *list;
3193eda14cbcSMatt Macy 	uu_avl_index_t idx = 0;
3194eda14cbcSMatt Macy 	uu_list_index_t idx2 = 0;
3195eda14cbcSMatt Macy 
3196eda14cbcSMatt Macy 	if (argc < 2)
3197eda14cbcSMatt Macy 		usage(B_FALSE);
3198eda14cbcSMatt Macy 
3199eda14cbcSMatt Macy 	if (strcmp(argv[0], "groupspace") == 0) {
3200eda14cbcSMatt Macy 		/* Toggle default group types */
3201eda14cbcSMatt Macy 		types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
3202eda14cbcSMatt Macy 	} else if (strcmp(argv[0], "projectspace") == 0) {
3203eda14cbcSMatt Macy 		types = USTYPE_PROJ;
3204eda14cbcSMatt Macy 		prtnum = B_TRUE;
3205eda14cbcSMatt Macy 	}
3206eda14cbcSMatt Macy 
3207eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
3208eda14cbcSMatt Macy 		switch (c) {
3209eda14cbcSMatt Macy 		case 'n':
3210eda14cbcSMatt Macy 			if (types == USTYPE_PROJ) {
3211eda14cbcSMatt Macy 				(void) fprintf(stderr,
3212eda14cbcSMatt Macy 				    gettext("invalid option 'n'\n"));
3213eda14cbcSMatt Macy 				usage(B_FALSE);
3214eda14cbcSMatt Macy 			}
3215eda14cbcSMatt Macy 			prtnum = B_TRUE;
3216eda14cbcSMatt Macy 			break;
3217eda14cbcSMatt Macy 		case 'H':
3218eda14cbcSMatt Macy 			scripted = B_TRUE;
3219eda14cbcSMatt Macy 			break;
3220eda14cbcSMatt Macy 		case 'p':
3221eda14cbcSMatt Macy 			parsable = B_TRUE;
3222eda14cbcSMatt Macy 			break;
3223eda14cbcSMatt Macy 		case 'o':
3224eda14cbcSMatt Macy 			ofield = optarg;
3225eda14cbcSMatt Macy 			break;
3226eda14cbcSMatt Macy 		case 's':
3227eda14cbcSMatt Macy 		case 'S':
3228eda14cbcSMatt Macy 			if (zfs_add_sort_column(&sortcol, optarg,
3229eda14cbcSMatt Macy 			    c == 's' ? B_FALSE : B_TRUE) != 0) {
3230eda14cbcSMatt Macy 				(void) fprintf(stderr,
3231eda14cbcSMatt Macy 				    gettext("invalid field '%s'\n"), optarg);
3232eda14cbcSMatt Macy 				usage(B_FALSE);
3233eda14cbcSMatt Macy 			}
3234eda14cbcSMatt Macy 			break;
3235eda14cbcSMatt Macy 		case 't':
3236eda14cbcSMatt Macy 			if (types == USTYPE_PROJ) {
3237eda14cbcSMatt Macy 				(void) fprintf(stderr,
3238eda14cbcSMatt Macy 				    gettext("invalid option 't'\n"));
3239eda14cbcSMatt Macy 				usage(B_FALSE);
3240eda14cbcSMatt Macy 			}
3241eda14cbcSMatt Macy 			tfield = optarg;
3242eda14cbcSMatt Macy 			break;
3243eda14cbcSMatt Macy 		case 'i':
3244eda14cbcSMatt Macy 			if (types == USTYPE_PROJ) {
3245eda14cbcSMatt Macy 				(void) fprintf(stderr,
3246eda14cbcSMatt Macy 				    gettext("invalid option 'i'\n"));
3247eda14cbcSMatt Macy 				usage(B_FALSE);
3248eda14cbcSMatt Macy 			}
3249eda14cbcSMatt Macy 			sid2posix = B_TRUE;
3250eda14cbcSMatt Macy 			break;
3251eda14cbcSMatt Macy 		case ':':
3252eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("missing argument for "
3253eda14cbcSMatt Macy 			    "'%c' option\n"), optopt);
3254eda14cbcSMatt Macy 			usage(B_FALSE);
3255eda14cbcSMatt Macy 			break;
3256eda14cbcSMatt Macy 		case '?':
3257eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3258eda14cbcSMatt Macy 			    optopt);
3259eda14cbcSMatt Macy 			usage(B_FALSE);
3260eda14cbcSMatt Macy 		}
3261eda14cbcSMatt Macy 	}
3262eda14cbcSMatt Macy 
3263eda14cbcSMatt Macy 	argc -= optind;
3264eda14cbcSMatt Macy 	argv += optind;
3265eda14cbcSMatt Macy 
3266eda14cbcSMatt Macy 	if (argc < 1) {
3267eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing dataset name\n"));
3268eda14cbcSMatt Macy 		usage(B_FALSE);
3269eda14cbcSMatt Macy 	}
3270eda14cbcSMatt Macy 	if (argc > 1) {
3271eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("too many arguments\n"));
3272eda14cbcSMatt Macy 		usage(B_FALSE);
3273eda14cbcSMatt Macy 	}
3274eda14cbcSMatt Macy 
3275eda14cbcSMatt Macy 	/* Use default output fields if not specified using -o */
3276eda14cbcSMatt Macy 	if (ofield == NULL)
3277eda14cbcSMatt Macy 		ofield = deffields;
3278eda14cbcSMatt Macy 	do {
3279eda14cbcSMatt Macy 		if ((delim = strchr(ofield, ',')) != NULL)
3280eda14cbcSMatt Macy 			*delim = '\0';
3281eda14cbcSMatt Macy 		if ((fields[cfield++] = us_field_index(ofield)) == -1) {
3282eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid type '%s' "
3283eda14cbcSMatt Macy 			    "for -o option\n"), ofield);
3284eda14cbcSMatt Macy 			return (-1);
3285eda14cbcSMatt Macy 		}
3286eda14cbcSMatt Macy 		if (delim != NULL)
3287eda14cbcSMatt Macy 			ofield = delim + 1;
3288eda14cbcSMatt Macy 	} while (delim != NULL);
3289eda14cbcSMatt Macy 	fields[cfield] = USFIELD_LAST;
3290eda14cbcSMatt Macy 
3291eda14cbcSMatt Macy 	/* Override output types (-t option) */
3292eda14cbcSMatt Macy 	if (tfield != NULL) {
3293eda14cbcSMatt Macy 		types = 0;
3294eda14cbcSMatt Macy 
3295eda14cbcSMatt Macy 		do {
3296eda14cbcSMatt Macy 			boolean_t found = B_FALSE;
3297eda14cbcSMatt Macy 
3298eda14cbcSMatt Macy 			if ((delim = strchr(tfield, ',')) != NULL)
3299eda14cbcSMatt Macy 				*delim = '\0';
3300eda14cbcSMatt Macy 			for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
3301eda14cbcSMatt Macy 			    i++) {
3302eda14cbcSMatt Macy 				if (strcmp(tfield, us_type_names[i]) == 0) {
3303eda14cbcSMatt Macy 					found = B_TRUE;
3304eda14cbcSMatt Macy 					types |= us_type_bits[i];
3305eda14cbcSMatt Macy 					break;
3306eda14cbcSMatt Macy 				}
3307eda14cbcSMatt Macy 			}
3308eda14cbcSMatt Macy 			if (!found) {
3309eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("invalid type "
3310eda14cbcSMatt Macy 				    "'%s' for -t option\n"), tfield);
3311eda14cbcSMatt Macy 				return (-1);
3312eda14cbcSMatt Macy 			}
3313eda14cbcSMatt Macy 			if (delim != NULL)
3314eda14cbcSMatt Macy 				tfield = delim + 1;
3315eda14cbcSMatt Macy 		} while (delim != NULL);
3316eda14cbcSMatt Macy 	}
3317eda14cbcSMatt Macy 
3318c40487d4SMatt Macy 	if ((zhp = zfs_path_to_zhandle(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM |
3319eda14cbcSMatt Macy 	    ZFS_TYPE_SNAPSHOT)) == NULL)
3320eda14cbcSMatt Macy 		return (1);
3321eda14cbcSMatt Macy 	if (zhp->zfs_head_type != ZFS_TYPE_FILESYSTEM) {
3322eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("operation is only applicable "
3323eda14cbcSMatt Macy 		    "to filesystems and their snapshots\n"));
3324eda14cbcSMatt Macy 		zfs_close(zhp);
3325eda14cbcSMatt Macy 		return (1);
3326eda14cbcSMatt Macy 	}
3327eda14cbcSMatt Macy 
3328eda14cbcSMatt Macy 	if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
3329eda14cbcSMatt Macy 	    offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
3330eda14cbcSMatt Macy 		nomem();
3331eda14cbcSMatt Macy 	if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
3332eda14cbcSMatt Macy 		nomem();
3333eda14cbcSMatt Macy 
3334eda14cbcSMatt Macy 	/* Always add default sorting columns */
3335eda14cbcSMatt Macy 	(void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
3336eda14cbcSMatt Macy 	(void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
3337eda14cbcSMatt Macy 
3338eda14cbcSMatt Macy 	cb.cb_sortcol = sortcol;
3339eda14cbcSMatt Macy 	cb.cb_numname = prtnum;
3340eda14cbcSMatt Macy 	cb.cb_nicenum = !parsable;
3341eda14cbcSMatt Macy 	cb.cb_avl_pool = avl_pool;
3342eda14cbcSMatt Macy 	cb.cb_avl = avl_tree;
3343eda14cbcSMatt Macy 	cb.cb_sid2posix = sid2posix;
3344eda14cbcSMatt Macy 
3345eda14cbcSMatt Macy 	for (i = 0; i < USFIELD_LAST; i++)
3346eda14cbcSMatt Macy 		cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
3347eda14cbcSMatt Macy 
3348eda14cbcSMatt Macy 	for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
3349eda14cbcSMatt Macy 		if ((zfs_prop_is_user(p) &&
3350eda14cbcSMatt Macy 		    !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
3351eda14cbcSMatt Macy 		    (zfs_prop_is_group(p) &&
3352eda14cbcSMatt Macy 		    !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))) ||
3353eda14cbcSMatt Macy 		    (zfs_prop_is_project(p) && types != USTYPE_PROJ))
3354eda14cbcSMatt Macy 			continue;
3355eda14cbcSMatt Macy 
3356eda14cbcSMatt Macy 		cb.cb_prop = p;
3357eda14cbcSMatt Macy 		if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0) {
3358eda14cbcSMatt Macy 			zfs_close(zhp);
3359eda14cbcSMatt Macy 			return (ret);
3360eda14cbcSMatt Macy 		}
3361eda14cbcSMatt Macy 	}
3362eda14cbcSMatt Macy 	zfs_close(zhp);
3363eda14cbcSMatt Macy 
3364eda14cbcSMatt Macy 	/* Sort the list */
3365eda14cbcSMatt Macy 	if ((node = uu_avl_first(avl_tree)) == NULL)
3366eda14cbcSMatt Macy 		return (0);
3367eda14cbcSMatt Macy 
3368eda14cbcSMatt Macy 	us_populated = B_TRUE;
3369eda14cbcSMatt Macy 
3370eda14cbcSMatt Macy 	listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
3371eda14cbcSMatt Macy 	    offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
3372eda14cbcSMatt Macy 	list = uu_list_create(listpool, NULL, UU_DEFAULT);
3373eda14cbcSMatt Macy 	uu_list_node_init(node, &node->usn_listnode, listpool);
3374eda14cbcSMatt Macy 
3375eda14cbcSMatt Macy 	while (node != NULL) {
3376eda14cbcSMatt Macy 		rmnode = node;
3377eda14cbcSMatt Macy 		node = uu_avl_next(avl_tree, node);
3378eda14cbcSMatt Macy 		uu_avl_remove(avl_tree, rmnode);
3379eda14cbcSMatt Macy 		if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
3380eda14cbcSMatt Macy 			uu_list_insert(list, rmnode, idx2);
3381eda14cbcSMatt Macy 	}
3382eda14cbcSMatt Macy 
3383eda14cbcSMatt Macy 	for (node = uu_list_first(list); node != NULL;
3384eda14cbcSMatt Macy 	    node = uu_list_next(list, node)) {
3385eda14cbcSMatt Macy 		us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
3386eda14cbcSMatt Macy 
3387eda14cbcSMatt Macy 		if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
3388eda14cbcSMatt Macy 			uu_avl_insert(avl_tree, node, idx);
3389eda14cbcSMatt Macy 	}
3390eda14cbcSMatt Macy 
3391eda14cbcSMatt Macy 	uu_list_destroy(list);
3392eda14cbcSMatt Macy 	uu_list_pool_destroy(listpool);
3393eda14cbcSMatt Macy 
3394eda14cbcSMatt Macy 	/* Print and free node nvlist memory */
3395eda14cbcSMatt Macy 	print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
3396eda14cbcSMatt Macy 	    cb.cb_avl);
3397eda14cbcSMatt Macy 
3398eda14cbcSMatt Macy 	zfs_free_sort_columns(sortcol);
3399eda14cbcSMatt Macy 
3400eda14cbcSMatt Macy 	/* Clean up the AVL tree */
3401eda14cbcSMatt Macy 	if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
3402eda14cbcSMatt Macy 		nomem();
3403eda14cbcSMatt Macy 
3404eda14cbcSMatt Macy 	while ((node = uu_avl_walk_next(walk)) != NULL) {
3405eda14cbcSMatt Macy 		uu_avl_remove(cb.cb_avl, node);
3406eda14cbcSMatt Macy 		free(node);
3407eda14cbcSMatt Macy 	}
3408eda14cbcSMatt Macy 
3409eda14cbcSMatt Macy 	uu_avl_walk_end(walk);
3410eda14cbcSMatt Macy 	uu_avl_destroy(avl_tree);
3411eda14cbcSMatt Macy 	uu_avl_pool_destroy(avl_pool);
3412eda14cbcSMatt Macy 
3413eda14cbcSMatt Macy 	return (ret);
3414eda14cbcSMatt Macy }
3415eda14cbcSMatt Macy 
3416eda14cbcSMatt Macy /*
3417eda14cbcSMatt Macy  * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
3418eda14cbcSMatt Macy  *      [-t type[,...]] [filesystem|volume|snapshot] ...
3419eda14cbcSMatt Macy  *
3420eda14cbcSMatt Macy  *	-H	Scripted mode; elide headers and separate columns by tabs
3421eda14cbcSMatt Macy  *	-p	Display values in parsable (literal) format.
3422eda14cbcSMatt Macy  *	-r	Recurse over all children
3423eda14cbcSMatt Macy  *	-d	Limit recursion by depth.
3424eda14cbcSMatt Macy  *	-o	Control which fields to display.
3425eda14cbcSMatt Macy  *	-s	Specify sort columns, descending order.
3426eda14cbcSMatt Macy  *	-S	Specify sort columns, ascending order.
3427eda14cbcSMatt Macy  *	-t	Control which object types to display.
3428eda14cbcSMatt Macy  *
3429eda14cbcSMatt Macy  * When given no arguments, list all filesystems in the system.
3430eda14cbcSMatt Macy  * Otherwise, list the specified datasets, optionally recursing down them if
3431eda14cbcSMatt Macy  * '-r' is specified.
3432eda14cbcSMatt Macy  */
3433eda14cbcSMatt Macy typedef struct list_cbdata {
3434eda14cbcSMatt Macy 	boolean_t	cb_first;
3435eda14cbcSMatt Macy 	boolean_t	cb_literal;
3436eda14cbcSMatt Macy 	boolean_t	cb_scripted;
3437eda14cbcSMatt Macy 	zprop_list_t	*cb_proplist;
3438eda14cbcSMatt Macy } list_cbdata_t;
3439eda14cbcSMatt Macy 
3440eda14cbcSMatt Macy /*
3441eda14cbcSMatt Macy  * Given a list of columns to display, output appropriate headers for each one.
3442eda14cbcSMatt Macy  */
3443eda14cbcSMatt Macy static void
3444eda14cbcSMatt Macy print_header(list_cbdata_t *cb)
3445eda14cbcSMatt Macy {
3446eda14cbcSMatt Macy 	zprop_list_t *pl = cb->cb_proplist;
3447eda14cbcSMatt Macy 	char headerbuf[ZFS_MAXPROPLEN];
3448eda14cbcSMatt Macy 	const char *header;
3449eda14cbcSMatt Macy 	int i;
3450eda14cbcSMatt Macy 	boolean_t first = B_TRUE;
3451eda14cbcSMatt Macy 	boolean_t right_justify;
3452eda14cbcSMatt Macy 
3453eda14cbcSMatt Macy 	for (; pl != NULL; pl = pl->pl_next) {
3454eda14cbcSMatt Macy 		if (!first) {
3455eda14cbcSMatt Macy 			(void) printf("  ");
3456eda14cbcSMatt Macy 		} else {
3457eda14cbcSMatt Macy 			first = B_FALSE;
3458eda14cbcSMatt Macy 		}
3459eda14cbcSMatt Macy 
3460eda14cbcSMatt Macy 		right_justify = B_FALSE;
3461eda14cbcSMatt Macy 		if (pl->pl_prop != ZPROP_INVAL) {
3462eda14cbcSMatt Macy 			header = zfs_prop_column_name(pl->pl_prop);
3463eda14cbcSMatt Macy 			right_justify = zfs_prop_align_right(pl->pl_prop);
3464eda14cbcSMatt Macy 		} else {
3465eda14cbcSMatt Macy 			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
3466eda14cbcSMatt Macy 				headerbuf[i] = toupper(pl->pl_user_prop[i]);
3467eda14cbcSMatt Macy 			headerbuf[i] = '\0';
3468eda14cbcSMatt Macy 			header = headerbuf;
3469eda14cbcSMatt Macy 		}
3470eda14cbcSMatt Macy 
3471eda14cbcSMatt Macy 		if (pl->pl_next == NULL && !right_justify)
3472eda14cbcSMatt Macy 			(void) printf("%s", header);
3473eda14cbcSMatt Macy 		else if (right_justify)
3474eda14cbcSMatt Macy 			(void) printf("%*s", (int)pl->pl_width, header);
3475eda14cbcSMatt Macy 		else
3476eda14cbcSMatt Macy 			(void) printf("%-*s", (int)pl->pl_width, header);
3477eda14cbcSMatt Macy 	}
3478eda14cbcSMatt Macy 
3479eda14cbcSMatt Macy 	(void) printf("\n");
3480eda14cbcSMatt Macy }
3481eda14cbcSMatt Macy 
3482eda14cbcSMatt Macy /*
3483eda14cbcSMatt Macy  * Given a dataset and a list of fields, print out all the properties according
3484eda14cbcSMatt Macy  * to the described layout.
3485eda14cbcSMatt Macy  */
3486eda14cbcSMatt Macy static void
3487eda14cbcSMatt Macy print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
3488eda14cbcSMatt Macy {
3489eda14cbcSMatt Macy 	zprop_list_t *pl = cb->cb_proplist;
3490eda14cbcSMatt Macy 	boolean_t first = B_TRUE;
3491eda14cbcSMatt Macy 	char property[ZFS_MAXPROPLEN];
3492eda14cbcSMatt Macy 	nvlist_t *userprops = zfs_get_user_props(zhp);
3493eda14cbcSMatt Macy 	nvlist_t *propval;
3494eda14cbcSMatt Macy 	char *propstr;
3495eda14cbcSMatt Macy 	boolean_t right_justify;
3496eda14cbcSMatt Macy 
3497eda14cbcSMatt Macy 	for (; pl != NULL; pl = pl->pl_next) {
3498eda14cbcSMatt Macy 		if (!first) {
3499eda14cbcSMatt Macy 			if (cb->cb_scripted)
3500eda14cbcSMatt Macy 				(void) printf("\t");
3501eda14cbcSMatt Macy 			else
3502eda14cbcSMatt Macy 				(void) printf("  ");
3503eda14cbcSMatt Macy 		} else {
3504eda14cbcSMatt Macy 			first = B_FALSE;
3505eda14cbcSMatt Macy 		}
3506eda14cbcSMatt Macy 
3507eda14cbcSMatt Macy 		if (pl->pl_prop == ZFS_PROP_NAME) {
3508eda14cbcSMatt Macy 			(void) strlcpy(property, zfs_get_name(zhp),
3509eda14cbcSMatt Macy 			    sizeof (property));
3510eda14cbcSMatt Macy 			propstr = property;
3511eda14cbcSMatt Macy 			right_justify = zfs_prop_align_right(pl->pl_prop);
3512eda14cbcSMatt Macy 		} else if (pl->pl_prop != ZPROP_INVAL) {
3513eda14cbcSMatt Macy 			if (zfs_prop_get(zhp, pl->pl_prop, property,
3514eda14cbcSMatt Macy 			    sizeof (property), NULL, NULL, 0,
3515eda14cbcSMatt Macy 			    cb->cb_literal) != 0)
3516eda14cbcSMatt Macy 				propstr = "-";
3517eda14cbcSMatt Macy 			else
3518eda14cbcSMatt Macy 				propstr = property;
3519eda14cbcSMatt Macy 			right_justify = zfs_prop_align_right(pl->pl_prop);
3520eda14cbcSMatt Macy 		} else if (zfs_prop_userquota(pl->pl_user_prop)) {
3521eda14cbcSMatt Macy 			if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3522eda14cbcSMatt Macy 			    property, sizeof (property), cb->cb_literal) != 0)
3523eda14cbcSMatt Macy 				propstr = "-";
3524eda14cbcSMatt Macy 			else
3525eda14cbcSMatt Macy 				propstr = property;
3526eda14cbcSMatt Macy 			right_justify = B_TRUE;
3527eda14cbcSMatt Macy 		} else if (zfs_prop_written(pl->pl_user_prop)) {
3528eda14cbcSMatt Macy 			if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3529eda14cbcSMatt Macy 			    property, sizeof (property), cb->cb_literal) != 0)
3530eda14cbcSMatt Macy 				propstr = "-";
3531eda14cbcSMatt Macy 			else
3532eda14cbcSMatt Macy 				propstr = property;
3533eda14cbcSMatt Macy 			right_justify = B_TRUE;
3534eda14cbcSMatt Macy 		} else {
3535eda14cbcSMatt Macy 			if (nvlist_lookup_nvlist(userprops,
3536eda14cbcSMatt Macy 			    pl->pl_user_prop, &propval) != 0)
3537eda14cbcSMatt Macy 				propstr = "-";
3538eda14cbcSMatt Macy 			else
3539eda14cbcSMatt Macy 				verify(nvlist_lookup_string(propval,
3540eda14cbcSMatt Macy 				    ZPROP_VALUE, &propstr) == 0);
3541eda14cbcSMatt Macy 			right_justify = B_FALSE;
3542eda14cbcSMatt Macy 		}
3543eda14cbcSMatt Macy 
3544eda14cbcSMatt Macy 		/*
3545eda14cbcSMatt Macy 		 * If this is being called in scripted mode, or if this is the
3546eda14cbcSMatt Macy 		 * last column and it is left-justified, don't include a width
3547eda14cbcSMatt Macy 		 * format specifier.
3548eda14cbcSMatt Macy 		 */
3549eda14cbcSMatt Macy 		if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3550eda14cbcSMatt Macy 			(void) printf("%s", propstr);
3551eda14cbcSMatt Macy 		else if (right_justify)
3552eda14cbcSMatt Macy 			(void) printf("%*s", (int)pl->pl_width, propstr);
3553eda14cbcSMatt Macy 		else
3554eda14cbcSMatt Macy 			(void) printf("%-*s", (int)pl->pl_width, propstr);
3555eda14cbcSMatt Macy 	}
3556eda14cbcSMatt Macy 
3557eda14cbcSMatt Macy 	(void) printf("\n");
3558eda14cbcSMatt Macy }
3559eda14cbcSMatt Macy 
3560eda14cbcSMatt Macy /*
3561eda14cbcSMatt Macy  * Generic callback function to list a dataset or snapshot.
3562eda14cbcSMatt Macy  */
3563eda14cbcSMatt Macy static int
3564eda14cbcSMatt Macy list_callback(zfs_handle_t *zhp, void *data)
3565eda14cbcSMatt Macy {
3566eda14cbcSMatt Macy 	list_cbdata_t *cbp = data;
3567eda14cbcSMatt Macy 
3568eda14cbcSMatt Macy 	if (cbp->cb_first) {
3569eda14cbcSMatt Macy 		if (!cbp->cb_scripted)
3570eda14cbcSMatt Macy 			print_header(cbp);
3571eda14cbcSMatt Macy 		cbp->cb_first = B_FALSE;
3572eda14cbcSMatt Macy 	}
3573eda14cbcSMatt Macy 
3574eda14cbcSMatt Macy 	print_dataset(zhp, cbp);
3575eda14cbcSMatt Macy 
3576eda14cbcSMatt Macy 	return (0);
3577eda14cbcSMatt Macy }
3578eda14cbcSMatt Macy 
3579eda14cbcSMatt Macy static int
3580eda14cbcSMatt Macy zfs_do_list(int argc, char **argv)
3581eda14cbcSMatt Macy {
3582eda14cbcSMatt Macy 	int c;
3583eda14cbcSMatt Macy 	static char default_fields[] =
3584eda14cbcSMatt Macy 	    "name,used,available,referenced,mountpoint";
3585eda14cbcSMatt Macy 	int types = ZFS_TYPE_DATASET;
3586eda14cbcSMatt Macy 	boolean_t types_specified = B_FALSE;
3587eda14cbcSMatt Macy 	char *fields = NULL;
3588eda14cbcSMatt Macy 	list_cbdata_t cb = { 0 };
3589eda14cbcSMatt Macy 	char *value;
3590eda14cbcSMatt Macy 	int limit = 0;
3591eda14cbcSMatt Macy 	int ret = 0;
3592eda14cbcSMatt Macy 	zfs_sort_column_t *sortcol = NULL;
3593eda14cbcSMatt Macy 	int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3594eda14cbcSMatt Macy 
3595eda14cbcSMatt Macy 	/* check options */
3596eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3597eda14cbcSMatt Macy 		switch (c) {
3598eda14cbcSMatt Macy 		case 'o':
3599eda14cbcSMatt Macy 			fields = optarg;
3600eda14cbcSMatt Macy 			break;
3601eda14cbcSMatt Macy 		case 'p':
3602eda14cbcSMatt Macy 			cb.cb_literal = B_TRUE;
3603eda14cbcSMatt Macy 			flags |= ZFS_ITER_LITERAL_PROPS;
3604eda14cbcSMatt Macy 			break;
3605eda14cbcSMatt Macy 		case 'd':
3606eda14cbcSMatt Macy 			limit = parse_depth(optarg, &flags);
3607eda14cbcSMatt Macy 			break;
3608eda14cbcSMatt Macy 		case 'r':
3609eda14cbcSMatt Macy 			flags |= ZFS_ITER_RECURSE;
3610eda14cbcSMatt Macy 			break;
3611eda14cbcSMatt Macy 		case 'H':
3612eda14cbcSMatt Macy 			cb.cb_scripted = B_TRUE;
3613eda14cbcSMatt Macy 			break;
3614eda14cbcSMatt Macy 		case 's':
3615eda14cbcSMatt Macy 			if (zfs_add_sort_column(&sortcol, optarg,
3616eda14cbcSMatt Macy 			    B_FALSE) != 0) {
3617eda14cbcSMatt Macy 				(void) fprintf(stderr,
3618eda14cbcSMatt Macy 				    gettext("invalid property '%s'\n"), optarg);
3619eda14cbcSMatt Macy 				usage(B_FALSE);
3620eda14cbcSMatt Macy 			}
3621eda14cbcSMatt Macy 			break;
3622eda14cbcSMatt Macy 		case 'S':
3623eda14cbcSMatt Macy 			if (zfs_add_sort_column(&sortcol, optarg,
3624eda14cbcSMatt Macy 			    B_TRUE) != 0) {
3625eda14cbcSMatt Macy 				(void) fprintf(stderr,
3626eda14cbcSMatt Macy 				    gettext("invalid property '%s'\n"), optarg);
3627eda14cbcSMatt Macy 				usage(B_FALSE);
3628eda14cbcSMatt Macy 			}
3629eda14cbcSMatt Macy 			break;
3630eda14cbcSMatt Macy 		case 't':
3631eda14cbcSMatt Macy 			types = 0;
3632eda14cbcSMatt Macy 			types_specified = B_TRUE;
3633eda14cbcSMatt Macy 			flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3634eda14cbcSMatt Macy 			while (*optarg != '\0') {
3635eda14cbcSMatt Macy 				static char *type_subopts[] = { "filesystem",
3636eda14cbcSMatt Macy 				    "volume", "snapshot", "snap", "bookmark",
3637eda14cbcSMatt Macy 				    "all", NULL };
3638eda14cbcSMatt Macy 
3639eda14cbcSMatt Macy 				switch (getsubopt(&optarg, type_subopts,
3640eda14cbcSMatt Macy 				    &value)) {
3641eda14cbcSMatt Macy 				case 0:
3642eda14cbcSMatt Macy 					types |= ZFS_TYPE_FILESYSTEM;
3643eda14cbcSMatt Macy 					break;
3644eda14cbcSMatt Macy 				case 1:
3645eda14cbcSMatt Macy 					types |= ZFS_TYPE_VOLUME;
3646eda14cbcSMatt Macy 					break;
3647eda14cbcSMatt Macy 				case 2:
3648eda14cbcSMatt Macy 				case 3:
3649eda14cbcSMatt Macy 					types |= ZFS_TYPE_SNAPSHOT;
3650eda14cbcSMatt Macy 					break;
3651eda14cbcSMatt Macy 				case 4:
3652eda14cbcSMatt Macy 					types |= ZFS_TYPE_BOOKMARK;
3653eda14cbcSMatt Macy 					break;
3654eda14cbcSMatt Macy 				case 5:
3655eda14cbcSMatt Macy 					types = ZFS_TYPE_DATASET |
3656eda14cbcSMatt Macy 					    ZFS_TYPE_BOOKMARK;
3657eda14cbcSMatt Macy 					break;
3658eda14cbcSMatt Macy 				default:
3659eda14cbcSMatt Macy 					(void) fprintf(stderr,
3660eda14cbcSMatt Macy 					    gettext("invalid type '%s'\n"),
3661eda14cbcSMatt Macy 					    value);
3662eda14cbcSMatt Macy 					usage(B_FALSE);
3663eda14cbcSMatt Macy 				}
3664eda14cbcSMatt Macy 			}
3665eda14cbcSMatt Macy 			break;
3666eda14cbcSMatt Macy 		case ':':
3667eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("missing argument for "
3668eda14cbcSMatt Macy 			    "'%c' option\n"), optopt);
3669eda14cbcSMatt Macy 			usage(B_FALSE);
3670eda14cbcSMatt Macy 			break;
3671eda14cbcSMatt Macy 		case '?':
3672eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3673eda14cbcSMatt Macy 			    optopt);
3674eda14cbcSMatt Macy 			usage(B_FALSE);
3675eda14cbcSMatt Macy 		}
3676eda14cbcSMatt Macy 	}
3677eda14cbcSMatt Macy 
3678eda14cbcSMatt Macy 	argc -= optind;
3679eda14cbcSMatt Macy 	argv += optind;
3680eda14cbcSMatt Macy 
3681eda14cbcSMatt Macy 	if (fields == NULL)
3682eda14cbcSMatt Macy 		fields = default_fields;
3683eda14cbcSMatt Macy 
3684eda14cbcSMatt Macy 	/*
3685eda14cbcSMatt Macy 	 * If we are only going to list snapshot names and sort by name,
3686eda14cbcSMatt Macy 	 * then we can use faster version.
3687eda14cbcSMatt Macy 	 */
3688eda14cbcSMatt Macy 	if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3689eda14cbcSMatt Macy 		flags |= ZFS_ITER_SIMPLE;
3690eda14cbcSMatt Macy 
3691eda14cbcSMatt Macy 	/*
3692eda14cbcSMatt Macy 	 * If "-o space" and no types were specified, don't display snapshots.
3693eda14cbcSMatt Macy 	 */
3694eda14cbcSMatt Macy 	if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3695eda14cbcSMatt Macy 		types &= ~ZFS_TYPE_SNAPSHOT;
3696eda14cbcSMatt Macy 
3697eda14cbcSMatt Macy 	/*
3698eda14cbcSMatt Macy 	 * Handle users who want to list all snapshots or bookmarks
3699eda14cbcSMatt Macy 	 * of the current dataset (ex. 'zfs list -t snapshot <dataset>').
3700eda14cbcSMatt Macy 	 */
3701eda14cbcSMatt Macy 	if ((types == ZFS_TYPE_SNAPSHOT || types == ZFS_TYPE_BOOKMARK) &&
3702eda14cbcSMatt Macy 	    argc > 0 && (flags & ZFS_ITER_RECURSE) == 0 && limit == 0) {
3703eda14cbcSMatt Macy 		flags |= (ZFS_ITER_DEPTH_LIMIT | ZFS_ITER_RECURSE);
3704eda14cbcSMatt Macy 		limit = 1;
3705eda14cbcSMatt Macy 	}
3706eda14cbcSMatt Macy 
3707eda14cbcSMatt Macy 	/*
3708eda14cbcSMatt Macy 	 * If the user specifies '-o all', the zprop_get_list() doesn't
3709eda14cbcSMatt Macy 	 * normally include the name of the dataset.  For 'zfs list', we always
3710eda14cbcSMatt Macy 	 * want this property to be first.
3711eda14cbcSMatt Macy 	 */
3712eda14cbcSMatt Macy 	if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3713eda14cbcSMatt Macy 	    != 0)
3714eda14cbcSMatt Macy 		usage(B_FALSE);
3715eda14cbcSMatt Macy 
3716eda14cbcSMatt Macy 	cb.cb_first = B_TRUE;
3717eda14cbcSMatt Macy 
3718eda14cbcSMatt Macy 	ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3719eda14cbcSMatt Macy 	    limit, list_callback, &cb);
3720eda14cbcSMatt Macy 
3721eda14cbcSMatt Macy 	zprop_free_list(cb.cb_proplist);
3722eda14cbcSMatt Macy 	zfs_free_sort_columns(sortcol);
3723eda14cbcSMatt Macy 
3724eda14cbcSMatt Macy 	if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3725eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("no datasets available\n"));
3726eda14cbcSMatt Macy 
3727eda14cbcSMatt Macy 	return (ret);
3728eda14cbcSMatt Macy }
3729eda14cbcSMatt Macy 
3730eda14cbcSMatt Macy /*
3731eac7052fSMatt Macy  * zfs rename [-fu] <fs | snap | vol> <fs | snap | vol>
3732eda14cbcSMatt Macy  * zfs rename [-f] -p <fs | vol> <fs | vol>
3733eac7052fSMatt Macy  * zfs rename [-u] -r <snap> <snap>
3734eda14cbcSMatt Macy  *
3735eda14cbcSMatt Macy  * Renames the given dataset to another of the same type.
3736eda14cbcSMatt Macy  *
3737eda14cbcSMatt Macy  * The '-p' flag creates all the non-existing ancestors of the target first.
3738eac7052fSMatt Macy  * The '-u' flag prevents file systems from being remounted during rename.
3739eda14cbcSMatt Macy  */
3740eda14cbcSMatt Macy /* ARGSUSED */
3741eda14cbcSMatt Macy static int
3742eda14cbcSMatt Macy zfs_do_rename(int argc, char **argv)
3743eda14cbcSMatt Macy {
3744eda14cbcSMatt Macy 	zfs_handle_t *zhp;
3745eac7052fSMatt Macy 	renameflags_t flags = { 0 };
3746eda14cbcSMatt Macy 	int c;
3747eda14cbcSMatt Macy 	int ret = 0;
3748eac7052fSMatt Macy 	int types;
3749eda14cbcSMatt Macy 	boolean_t parents = B_FALSE;
3750eda14cbcSMatt Macy 
3751eda14cbcSMatt Macy 	/* check options */
3752eac7052fSMatt Macy 	while ((c = getopt(argc, argv, "pruf")) != -1) {
3753eda14cbcSMatt Macy 		switch (c) {
3754eda14cbcSMatt Macy 		case 'p':
3755eda14cbcSMatt Macy 			parents = B_TRUE;
3756eda14cbcSMatt Macy 			break;
3757eda14cbcSMatt Macy 		case 'r':
3758eac7052fSMatt Macy 			flags.recursive = B_TRUE;
3759eac7052fSMatt Macy 			break;
3760eac7052fSMatt Macy 		case 'u':
3761eac7052fSMatt Macy 			flags.nounmount = B_TRUE;
3762eda14cbcSMatt Macy 			break;
3763eda14cbcSMatt Macy 		case 'f':
3764eac7052fSMatt Macy 			flags.forceunmount = B_TRUE;
3765eda14cbcSMatt Macy 			break;
3766eda14cbcSMatt Macy 		case '?':
3767eda14cbcSMatt Macy 		default:
3768eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3769eda14cbcSMatt Macy 			    optopt);
3770eda14cbcSMatt Macy 			usage(B_FALSE);
3771eda14cbcSMatt Macy 		}
3772eda14cbcSMatt Macy 	}
3773eda14cbcSMatt Macy 
3774eda14cbcSMatt Macy 	argc -= optind;
3775eda14cbcSMatt Macy 	argv += optind;
3776eda14cbcSMatt Macy 
3777eda14cbcSMatt Macy 	/* check number of arguments */
3778eda14cbcSMatt Macy 	if (argc < 1) {
3779eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing source dataset "
3780eda14cbcSMatt Macy 		    "argument\n"));
3781eda14cbcSMatt Macy 		usage(B_FALSE);
3782eda14cbcSMatt Macy 	}
3783eda14cbcSMatt Macy 	if (argc < 2) {
3784eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing target dataset "
3785eda14cbcSMatt Macy 		    "argument\n"));
3786eda14cbcSMatt Macy 		usage(B_FALSE);
3787eda14cbcSMatt Macy 	}
3788eda14cbcSMatt Macy 	if (argc > 2) {
3789eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("too many arguments\n"));
3790eda14cbcSMatt Macy 		usage(B_FALSE);
3791eda14cbcSMatt Macy 	}
3792eda14cbcSMatt Macy 
3793eac7052fSMatt Macy 	if (flags.recursive && parents) {
3794eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("-p and -r options are mutually "
3795eda14cbcSMatt Macy 		    "exclusive\n"));
3796eda14cbcSMatt Macy 		usage(B_FALSE);
3797eda14cbcSMatt Macy 	}
3798eda14cbcSMatt Macy 
3799eac7052fSMatt Macy 	if (flags.nounmount && parents) {
3800eac7052fSMatt Macy 		(void) fprintf(stderr, gettext("-u and -p options are mutually "
3801eac7052fSMatt Macy 		    "exclusive\n"));
3802eac7052fSMatt Macy 		usage(B_FALSE);
3803eac7052fSMatt Macy 	}
3804eac7052fSMatt Macy 
3805eac7052fSMatt Macy 	if (flags.recursive && strchr(argv[0], '@') == 0) {
3806eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("source dataset for recursive "
3807eda14cbcSMatt Macy 		    "rename must be a snapshot\n"));
3808eda14cbcSMatt Macy 		usage(B_FALSE);
3809eda14cbcSMatt Macy 	}
3810eda14cbcSMatt Macy 
3811eac7052fSMatt Macy 	if (flags.nounmount)
3812eac7052fSMatt Macy 		types = ZFS_TYPE_FILESYSTEM;
3813eac7052fSMatt Macy 	else if (parents)
3814eac7052fSMatt Macy 		types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3815eac7052fSMatt Macy 	else
3816eac7052fSMatt Macy 		types = ZFS_TYPE_DATASET;
3817eac7052fSMatt Macy 
3818eac7052fSMatt Macy 	if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3819eda14cbcSMatt Macy 		return (1);
3820eda14cbcSMatt Macy 
3821eda14cbcSMatt Macy 	/* If we were asked and the name looks good, try to create ancestors. */
3822eda14cbcSMatt Macy 	if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3823eda14cbcSMatt Macy 	    zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3824eda14cbcSMatt Macy 		zfs_close(zhp);
3825eda14cbcSMatt Macy 		return (1);
3826eda14cbcSMatt Macy 	}
3827eda14cbcSMatt Macy 
3828eac7052fSMatt Macy 	ret = (zfs_rename(zhp, argv[1], flags) != 0);
3829eda14cbcSMatt Macy 
3830eda14cbcSMatt Macy 	zfs_close(zhp);
3831eda14cbcSMatt Macy 	return (ret);
3832eda14cbcSMatt Macy }
3833eda14cbcSMatt Macy 
3834eda14cbcSMatt Macy /*
3835eda14cbcSMatt Macy  * zfs promote <fs>
3836eda14cbcSMatt Macy  *
3837eda14cbcSMatt Macy  * Promotes the given clone fs to be the parent
3838eda14cbcSMatt Macy  */
3839eda14cbcSMatt Macy /* ARGSUSED */
3840eda14cbcSMatt Macy static int
3841eda14cbcSMatt Macy zfs_do_promote(int argc, char **argv)
3842eda14cbcSMatt Macy {
3843eda14cbcSMatt Macy 	zfs_handle_t *zhp;
3844eda14cbcSMatt Macy 	int ret = 0;
3845eda14cbcSMatt Macy 
3846eda14cbcSMatt Macy 	/* check options */
3847eda14cbcSMatt Macy 	if (argc > 1 && argv[1][0] == '-') {
3848eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
3849eda14cbcSMatt Macy 		    argv[1][1]);
3850eda14cbcSMatt Macy 		usage(B_FALSE);
3851eda14cbcSMatt Macy 	}
3852eda14cbcSMatt Macy 
3853eda14cbcSMatt Macy 	/* check number of arguments */
3854eda14cbcSMatt Macy 	if (argc < 2) {
3855eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing clone filesystem"
3856eda14cbcSMatt Macy 		    " argument\n"));
3857eda14cbcSMatt Macy 		usage(B_FALSE);
3858eda14cbcSMatt Macy 	}
3859eda14cbcSMatt Macy 	if (argc > 2) {
3860eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("too many arguments\n"));
3861eda14cbcSMatt Macy 		usage(B_FALSE);
3862eda14cbcSMatt Macy 	}
3863eda14cbcSMatt Macy 
3864eda14cbcSMatt Macy 	zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3865eda14cbcSMatt Macy 	if (zhp == NULL)
3866eda14cbcSMatt Macy 		return (1);
3867eda14cbcSMatt Macy 
3868eda14cbcSMatt Macy 	ret = (zfs_promote(zhp) != 0);
3869eda14cbcSMatt Macy 
3870eda14cbcSMatt Macy 
3871eda14cbcSMatt Macy 	zfs_close(zhp);
3872eda14cbcSMatt Macy 	return (ret);
3873eda14cbcSMatt Macy }
3874eda14cbcSMatt Macy 
3875eda14cbcSMatt Macy static int
3876eda14cbcSMatt Macy zfs_do_redact(int argc, char **argv)
3877eda14cbcSMatt Macy {
3878eda14cbcSMatt Macy 	char *snap = NULL;
3879eda14cbcSMatt Macy 	char *bookname = NULL;
3880eda14cbcSMatt Macy 	char **rsnaps = NULL;
3881eda14cbcSMatt Macy 	int numrsnaps = 0;
3882eda14cbcSMatt Macy 	argv++;
3883eda14cbcSMatt Macy 	argc--;
3884eda14cbcSMatt Macy 	if (argc < 3) {
3885eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("too few arguments\n"));
3886eda14cbcSMatt Macy 		usage(B_FALSE);
3887eda14cbcSMatt Macy 	}
3888eda14cbcSMatt Macy 
3889eda14cbcSMatt Macy 	snap = argv[0];
3890eda14cbcSMatt Macy 	bookname = argv[1];
3891eda14cbcSMatt Macy 	rsnaps = argv + 2;
3892eda14cbcSMatt Macy 	numrsnaps = argc - 2;
3893eda14cbcSMatt Macy 
3894eda14cbcSMatt Macy 	nvlist_t *rsnapnv = fnvlist_alloc();
3895eda14cbcSMatt Macy 
3896eda14cbcSMatt Macy 	for (int i = 0; i < numrsnaps; i++) {
3897eda14cbcSMatt Macy 		fnvlist_add_boolean(rsnapnv, rsnaps[i]);
3898eda14cbcSMatt Macy 	}
3899eda14cbcSMatt Macy 
3900eda14cbcSMatt Macy 	int err = lzc_redact(snap, bookname, rsnapnv);
3901eda14cbcSMatt Macy 	fnvlist_free(rsnapnv);
3902eda14cbcSMatt Macy 
3903eda14cbcSMatt Macy 	switch (err) {
3904eda14cbcSMatt Macy 	case 0:
3905eda14cbcSMatt Macy 		break;
3906eda14cbcSMatt Macy 	case ENOENT:
3907eda14cbcSMatt Macy 		(void) fprintf(stderr,
3908eda14cbcSMatt Macy 		    gettext("provided snapshot %s does not exist\n"), snap);
3909eda14cbcSMatt Macy 		break;
3910eda14cbcSMatt Macy 	case EEXIST:
3911eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("specified redaction bookmark "
3912eda14cbcSMatt Macy 		    "(%s) provided already exists\n"), bookname);
3913eda14cbcSMatt Macy 		break;
3914eda14cbcSMatt Macy 	case ENAMETOOLONG:
3915eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("provided bookmark name cannot "
3916eda14cbcSMatt Macy 		    "be used, final name would be too long\n"));
3917eda14cbcSMatt Macy 		break;
3918eda14cbcSMatt Macy 	case E2BIG:
3919eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("too many redaction snapshots "
3920eda14cbcSMatt Macy 		    "specified\n"));
3921eda14cbcSMatt Macy 		break;
3922eda14cbcSMatt Macy 	case EINVAL:
3923eda14cbcSMatt Macy 		if (strchr(bookname, '#') != NULL)
3924eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext(
3925eda14cbcSMatt Macy 			    "redaction bookmark name must not contain '#'\n"));
3926eda14cbcSMatt Macy 		else
3927eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext(
3928eda14cbcSMatt Macy 			    "redaction snapshot must be descendent of "
3929eda14cbcSMatt Macy 			    "snapshot being redacted\n"));
3930eda14cbcSMatt Macy 		break;
3931eda14cbcSMatt Macy 	case EALREADY:
3932eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("attempted to redact redacted "
3933eda14cbcSMatt Macy 		    "dataset or with respect to redacted dataset\n"));
3934eda14cbcSMatt Macy 		break;
3935eda14cbcSMatt Macy 	case ENOTSUP:
3936eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("redaction bookmarks feature "
3937eda14cbcSMatt Macy 		    "not enabled\n"));
3938eda14cbcSMatt Macy 		break;
3939eda14cbcSMatt Macy 	case EXDEV:
3940eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("potentially invalid redaction "
3941eda14cbcSMatt Macy 		    "snapshot; full dataset names required\n"));
3942eda14cbcSMatt Macy 		break;
3943eda14cbcSMatt Macy 	default:
3944eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("internal error: %s\n"),
3945eda14cbcSMatt Macy 		    strerror(errno));
3946eda14cbcSMatt Macy 	}
3947eda14cbcSMatt Macy 
3948eda14cbcSMatt Macy 	return (err);
3949eda14cbcSMatt Macy }
3950eda14cbcSMatt Macy 
3951eda14cbcSMatt Macy /*
3952eda14cbcSMatt Macy  * zfs rollback [-rRf] <snapshot>
3953eda14cbcSMatt Macy  *
3954eda14cbcSMatt Macy  *	-r	Delete any intervening snapshots before doing rollback
3955eda14cbcSMatt Macy  *	-R	Delete any snapshots and their clones
3956eda14cbcSMatt Macy  *	-f	ignored for backwards compatibility
3957eda14cbcSMatt Macy  *
3958eda14cbcSMatt Macy  * Given a filesystem, rollback to a specific snapshot, discarding any changes
3959eda14cbcSMatt Macy  * since then and making it the active dataset.  If more recent snapshots exist,
3960eda14cbcSMatt Macy  * the command will complain unless the '-r' flag is given.
3961eda14cbcSMatt Macy  */
3962eda14cbcSMatt Macy typedef struct rollback_cbdata {
3963eda14cbcSMatt Macy 	uint64_t	cb_create;
3964eda14cbcSMatt Macy 	uint8_t		cb_younger_ds_printed;
3965eda14cbcSMatt Macy 	boolean_t	cb_first;
3966eda14cbcSMatt Macy 	int		cb_doclones;
3967eda14cbcSMatt Macy 	char		*cb_target;
3968eda14cbcSMatt Macy 	int		cb_error;
3969eda14cbcSMatt Macy 	boolean_t	cb_recurse;
3970eda14cbcSMatt Macy } rollback_cbdata_t;
3971eda14cbcSMatt Macy 
3972eda14cbcSMatt Macy static int
3973eda14cbcSMatt Macy rollback_check_dependent(zfs_handle_t *zhp, void *data)
3974eda14cbcSMatt Macy {
3975eda14cbcSMatt Macy 	rollback_cbdata_t *cbp = data;
3976eda14cbcSMatt Macy 
3977eda14cbcSMatt Macy 	if (cbp->cb_first && cbp->cb_recurse) {
3978eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("cannot rollback to "
3979eda14cbcSMatt Macy 		    "'%s': clones of previous snapshots exist\n"),
3980eda14cbcSMatt Macy 		    cbp->cb_target);
3981eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("use '-R' to "
3982eda14cbcSMatt Macy 		    "force deletion of the following clones and "
3983eda14cbcSMatt Macy 		    "dependents:\n"));
3984eda14cbcSMatt Macy 		cbp->cb_first = 0;
3985eda14cbcSMatt Macy 		cbp->cb_error = 1;
3986eda14cbcSMatt Macy 	}
3987eda14cbcSMatt Macy 
3988eda14cbcSMatt Macy 	(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3989eda14cbcSMatt Macy 
3990eda14cbcSMatt Macy 	zfs_close(zhp);
3991eda14cbcSMatt Macy 	return (0);
3992eda14cbcSMatt Macy }
3993eda14cbcSMatt Macy 
3994eda14cbcSMatt Macy 
3995eda14cbcSMatt Macy /*
3996eda14cbcSMatt Macy  * Report some snapshots/bookmarks more recent than the one specified.
3997eda14cbcSMatt Macy  * Used when '-r' is not specified. We reuse this same callback for the
3998eda14cbcSMatt Macy  * snapshot dependents - if 'cb_dependent' is set, then this is a
3999eda14cbcSMatt Macy  * dependent and we should report it without checking the transaction group.
4000eda14cbcSMatt Macy  */
4001eda14cbcSMatt Macy static int
4002eda14cbcSMatt Macy rollback_check(zfs_handle_t *zhp, void *data)
4003eda14cbcSMatt Macy {
4004eda14cbcSMatt Macy 	rollback_cbdata_t *cbp = data;
4005eda14cbcSMatt Macy 	/*
4006eda14cbcSMatt Macy 	 * Max number of younger snapshots and/or bookmarks to display before
4007eda14cbcSMatt Macy 	 * we stop the iteration.
4008eda14cbcSMatt Macy 	 */
4009eda14cbcSMatt Macy 	const uint8_t max_younger = 32;
4010eda14cbcSMatt Macy 
4011eda14cbcSMatt Macy 	if (cbp->cb_doclones) {
4012eda14cbcSMatt Macy 		zfs_close(zhp);
4013eda14cbcSMatt Macy 		return (0);
4014eda14cbcSMatt Macy 	}
4015eda14cbcSMatt Macy 
4016eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
4017eda14cbcSMatt Macy 		if (cbp->cb_first && !cbp->cb_recurse) {
4018eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("cannot "
4019eda14cbcSMatt Macy 			    "rollback to '%s': more recent snapshots "
4020eda14cbcSMatt Macy 			    "or bookmarks exist\n"),
4021eda14cbcSMatt Macy 			    cbp->cb_target);
4022eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("use '-r' to "
4023eda14cbcSMatt Macy 			    "force deletion of the following "
4024eda14cbcSMatt Macy 			    "snapshots and bookmarks:\n"));
4025eda14cbcSMatt Macy 			cbp->cb_first = 0;
4026eda14cbcSMatt Macy 			cbp->cb_error = 1;
4027eda14cbcSMatt Macy 		}
4028eda14cbcSMatt Macy 
4029eda14cbcSMatt Macy 		if (cbp->cb_recurse) {
4030eda14cbcSMatt Macy 			if (zfs_iter_dependents(zhp, B_TRUE,
4031eda14cbcSMatt Macy 			    rollback_check_dependent, cbp) != 0) {
4032eda14cbcSMatt Macy 				zfs_close(zhp);
4033eda14cbcSMatt Macy 				return (-1);
4034eda14cbcSMatt Macy 			}
4035eda14cbcSMatt Macy 		} else {
4036eda14cbcSMatt Macy 			(void) fprintf(stderr, "%s\n",
4037eda14cbcSMatt Macy 			    zfs_get_name(zhp));
4038eda14cbcSMatt Macy 			cbp->cb_younger_ds_printed++;
4039eda14cbcSMatt Macy 		}
4040eda14cbcSMatt Macy 	}
4041eda14cbcSMatt Macy 	zfs_close(zhp);
4042eda14cbcSMatt Macy 
4043eda14cbcSMatt Macy 	if (cbp->cb_younger_ds_printed == max_younger) {
4044eda14cbcSMatt Macy 		/*
4045eda14cbcSMatt Macy 		 * This non-recursive rollback is going to fail due to the
4046eda14cbcSMatt Macy 		 * presence of snapshots and/or bookmarks that are younger than
4047eda14cbcSMatt Macy 		 * the rollback target.
4048eda14cbcSMatt Macy 		 * We printed some of the offending objects, now we stop
4049eda14cbcSMatt Macy 		 * zfs_iter_snapshot/bookmark iteration so we can fail fast and
4050eda14cbcSMatt Macy 		 * avoid iterating over the rest of the younger objects
4051eda14cbcSMatt Macy 		 */
4052eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("Output limited to %d "
4053eda14cbcSMatt Macy 		    "snapshots/bookmarks\n"), max_younger);
4054eda14cbcSMatt Macy 		return (-1);
4055eda14cbcSMatt Macy 	}
4056eda14cbcSMatt Macy 	return (0);
4057eda14cbcSMatt Macy }
4058eda14cbcSMatt Macy 
4059eda14cbcSMatt Macy static int
4060eda14cbcSMatt Macy zfs_do_rollback(int argc, char **argv)
4061eda14cbcSMatt Macy {
4062eda14cbcSMatt Macy 	int ret = 0;
4063eda14cbcSMatt Macy 	int c;
4064eda14cbcSMatt Macy 	boolean_t force = B_FALSE;
4065eda14cbcSMatt Macy 	rollback_cbdata_t cb = { 0 };
4066eda14cbcSMatt Macy 	zfs_handle_t *zhp, *snap;
4067eda14cbcSMatt Macy 	char parentname[ZFS_MAX_DATASET_NAME_LEN];
4068eda14cbcSMatt Macy 	char *delim;
4069eda14cbcSMatt Macy 	uint64_t min_txg = 0;
4070eda14cbcSMatt Macy 
4071eda14cbcSMatt Macy 	/* check options */
4072eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, "rRf")) != -1) {
4073eda14cbcSMatt Macy 		switch (c) {
4074eda14cbcSMatt Macy 		case 'r':
4075eda14cbcSMatt Macy 			cb.cb_recurse = 1;
4076eda14cbcSMatt Macy 			break;
4077eda14cbcSMatt Macy 		case 'R':
4078eda14cbcSMatt Macy 			cb.cb_recurse = 1;
4079eda14cbcSMatt Macy 			cb.cb_doclones = 1;
4080eda14cbcSMatt Macy 			break;
4081eda14cbcSMatt Macy 		case 'f':
4082eda14cbcSMatt Macy 			force = B_TRUE;
4083eda14cbcSMatt Macy 			break;
4084eda14cbcSMatt Macy 		case '?':
4085eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4086eda14cbcSMatt Macy 			    optopt);
4087eda14cbcSMatt Macy 			usage(B_FALSE);
4088eda14cbcSMatt Macy 		}
4089eda14cbcSMatt Macy 	}
4090eda14cbcSMatt Macy 
4091eda14cbcSMatt Macy 	argc -= optind;
4092eda14cbcSMatt Macy 	argv += optind;
4093eda14cbcSMatt Macy 
4094eda14cbcSMatt Macy 	/* check number of arguments */
4095eda14cbcSMatt Macy 	if (argc < 1) {
4096eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing dataset argument\n"));
4097eda14cbcSMatt Macy 		usage(B_FALSE);
4098eda14cbcSMatt Macy 	}
4099eda14cbcSMatt Macy 	if (argc > 1) {
4100eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("too many arguments\n"));
4101eda14cbcSMatt Macy 		usage(B_FALSE);
4102eda14cbcSMatt Macy 	}
4103eda14cbcSMatt Macy 
4104eda14cbcSMatt Macy 	/* open the snapshot */
4105eda14cbcSMatt Macy 	if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
4106eda14cbcSMatt Macy 		return (1);
4107eda14cbcSMatt Macy 
4108eda14cbcSMatt Macy 	/* open the parent dataset */
4109eda14cbcSMatt Macy 	(void) strlcpy(parentname, argv[0], sizeof (parentname));
4110eda14cbcSMatt Macy 	verify((delim = strrchr(parentname, '@')) != NULL);
4111eda14cbcSMatt Macy 	*delim = '\0';
4112eda14cbcSMatt Macy 	if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
4113eda14cbcSMatt Macy 		zfs_close(snap);
4114eda14cbcSMatt Macy 		return (1);
4115eda14cbcSMatt Macy 	}
4116eda14cbcSMatt Macy 
4117eda14cbcSMatt Macy 	/*
4118eda14cbcSMatt Macy 	 * Check for more recent snapshots and/or clones based on the presence
4119eda14cbcSMatt Macy 	 * of '-r' and '-R'.
4120eda14cbcSMatt Macy 	 */
4121eda14cbcSMatt Macy 	cb.cb_target = argv[0];
4122eda14cbcSMatt Macy 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
4123eda14cbcSMatt Macy 	cb.cb_first = B_TRUE;
4124eda14cbcSMatt Macy 	cb.cb_error = 0;
4125eda14cbcSMatt Macy 
4126eda14cbcSMatt Macy 	if (cb.cb_create > 0)
4127eda14cbcSMatt Macy 		min_txg = cb.cb_create;
4128eda14cbcSMatt Macy 
4129eda14cbcSMatt Macy 	if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb,
4130eda14cbcSMatt Macy 	    min_txg, 0)) != 0)
4131eda14cbcSMatt Macy 		goto out;
4132eda14cbcSMatt Macy 	if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
4133eda14cbcSMatt Macy 		goto out;
4134eda14cbcSMatt Macy 
4135eda14cbcSMatt Macy 	if ((ret = cb.cb_error) != 0)
4136eda14cbcSMatt Macy 		goto out;
4137eda14cbcSMatt Macy 
4138eda14cbcSMatt Macy 	/*
4139eda14cbcSMatt Macy 	 * Rollback parent to the given snapshot.
4140eda14cbcSMatt Macy 	 */
4141eda14cbcSMatt Macy 	ret = zfs_rollback(zhp, snap, force);
4142eda14cbcSMatt Macy 
4143eda14cbcSMatt Macy out:
4144eda14cbcSMatt Macy 	zfs_close(snap);
4145eda14cbcSMatt Macy 	zfs_close(zhp);
4146eda14cbcSMatt Macy 
4147eda14cbcSMatt Macy 	if (ret == 0)
4148eda14cbcSMatt Macy 		return (0);
4149eda14cbcSMatt Macy 	else
4150eda14cbcSMatt Macy 		return (1);
4151eda14cbcSMatt Macy }
4152eda14cbcSMatt Macy 
4153eda14cbcSMatt Macy /*
4154eda14cbcSMatt Macy  * zfs set property=value ... { fs | snap | vol } ...
4155eda14cbcSMatt Macy  *
4156eda14cbcSMatt Macy  * Sets the given properties for all datasets specified on the command line.
4157eda14cbcSMatt Macy  */
4158eda14cbcSMatt Macy 
4159eda14cbcSMatt Macy static int
4160eda14cbcSMatt Macy set_callback(zfs_handle_t *zhp, void *data)
4161eda14cbcSMatt Macy {
4162eda14cbcSMatt Macy 	nvlist_t *props = data;
4163eda14cbcSMatt Macy 
4164eda14cbcSMatt Macy 	if (zfs_prop_set_list(zhp, props) != 0) {
4165eda14cbcSMatt Macy 		switch (libzfs_errno(g_zfs)) {
4166eda14cbcSMatt Macy 		case EZFS_MOUNTFAILED:
4167eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("property may be set "
4168eda14cbcSMatt Macy 			    "but unable to remount filesystem\n"));
4169eda14cbcSMatt Macy 			break;
4170eda14cbcSMatt Macy 		case EZFS_SHARENFSFAILED:
4171eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("property may be set "
4172eda14cbcSMatt Macy 			    "but unable to reshare filesystem\n"));
4173eda14cbcSMatt Macy 			break;
4174eda14cbcSMatt Macy 		}
4175eda14cbcSMatt Macy 		return (1);
4176eda14cbcSMatt Macy 	}
4177eda14cbcSMatt Macy 	return (0);
4178eda14cbcSMatt Macy }
4179eda14cbcSMatt Macy 
4180eda14cbcSMatt Macy static int
4181eda14cbcSMatt Macy zfs_do_set(int argc, char **argv)
4182eda14cbcSMatt Macy {
4183eda14cbcSMatt Macy 	nvlist_t *props = NULL;
4184eda14cbcSMatt Macy 	int ds_start = -1; /* argv idx of first dataset arg */
4185eda14cbcSMatt Macy 	int ret = 0;
4186eda14cbcSMatt Macy 	int i;
4187eda14cbcSMatt Macy 
4188eda14cbcSMatt Macy 	/* check for options */
4189eda14cbcSMatt Macy 	if (argc > 1 && argv[1][0] == '-') {
4190eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4191eda14cbcSMatt Macy 		    argv[1][1]);
4192eda14cbcSMatt Macy 		usage(B_FALSE);
4193eda14cbcSMatt Macy 	}
4194eda14cbcSMatt Macy 
4195eda14cbcSMatt Macy 	/* check number of arguments */
4196eda14cbcSMatt Macy 	if (argc < 2) {
4197eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing arguments\n"));
4198eda14cbcSMatt Macy 		usage(B_FALSE);
4199eda14cbcSMatt Macy 	}
4200eda14cbcSMatt Macy 	if (argc < 3) {
4201eda14cbcSMatt Macy 		if (strchr(argv[1], '=') == NULL) {
4202eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("missing property=value "
4203eda14cbcSMatt Macy 			    "argument(s)\n"));
4204eda14cbcSMatt Macy 		} else {
4205eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("missing dataset "
4206eda14cbcSMatt Macy 			    "name(s)\n"));
4207eda14cbcSMatt Macy 		}
4208eda14cbcSMatt Macy 		usage(B_FALSE);
4209eda14cbcSMatt Macy 	}
4210eda14cbcSMatt Macy 
4211eda14cbcSMatt Macy 	/* validate argument order:  prop=val args followed by dataset args */
4212eda14cbcSMatt Macy 	for (i = 1; i < argc; i++) {
4213eda14cbcSMatt Macy 		if (strchr(argv[i], '=') != NULL) {
4214eda14cbcSMatt Macy 			if (ds_start > 0) {
4215eda14cbcSMatt Macy 				/* out-of-order prop=val argument */
4216eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("invalid "
4217eda14cbcSMatt Macy 				    "argument order\n"));
4218eda14cbcSMatt Macy 				usage(B_FALSE);
4219eda14cbcSMatt Macy 			}
4220eda14cbcSMatt Macy 		} else if (ds_start < 0) {
4221eda14cbcSMatt Macy 			ds_start = i;
4222eda14cbcSMatt Macy 		}
4223eda14cbcSMatt Macy 	}
4224eda14cbcSMatt Macy 	if (ds_start < 0) {
4225eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing dataset name(s)\n"));
4226eda14cbcSMatt Macy 		usage(B_FALSE);
4227eda14cbcSMatt Macy 	}
4228eda14cbcSMatt Macy 
4229eda14cbcSMatt Macy 	/* Populate a list of property settings */
4230eda14cbcSMatt Macy 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4231eda14cbcSMatt Macy 		nomem();
4232eda14cbcSMatt Macy 	for (i = 1; i < ds_start; i++) {
4233eda14cbcSMatt Macy 		if (!parseprop(props, argv[i])) {
4234eda14cbcSMatt Macy 			ret = -1;
4235eda14cbcSMatt Macy 			goto error;
4236eda14cbcSMatt Macy 		}
4237eda14cbcSMatt Macy 	}
4238eda14cbcSMatt Macy 
4239eda14cbcSMatt Macy 	ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
4240eda14cbcSMatt Macy 	    ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
4241eda14cbcSMatt Macy 
4242eda14cbcSMatt Macy error:
4243eda14cbcSMatt Macy 	nvlist_free(props);
4244eda14cbcSMatt Macy 	return (ret);
4245eda14cbcSMatt Macy }
4246eda14cbcSMatt Macy 
4247eda14cbcSMatt Macy typedef struct snap_cbdata {
4248eda14cbcSMatt Macy 	nvlist_t *sd_nvl;
4249eda14cbcSMatt Macy 	boolean_t sd_recursive;
4250eda14cbcSMatt Macy 	const char *sd_snapname;
4251eda14cbcSMatt Macy } snap_cbdata_t;
4252eda14cbcSMatt Macy 
4253eda14cbcSMatt Macy static int
4254eda14cbcSMatt Macy zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
4255eda14cbcSMatt Macy {
4256eda14cbcSMatt Macy 	snap_cbdata_t *sd = arg;
4257eda14cbcSMatt Macy 	char *name;
4258eda14cbcSMatt Macy 	int rv = 0;
4259eda14cbcSMatt Macy 	int error;
4260eda14cbcSMatt Macy 
4261eda14cbcSMatt Macy 	if (sd->sd_recursive &&
4262eda14cbcSMatt Macy 	    zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
4263eda14cbcSMatt Macy 		zfs_close(zhp);
4264eda14cbcSMatt Macy 		return (0);
4265eda14cbcSMatt Macy 	}
4266eda14cbcSMatt Macy 
4267eda14cbcSMatt Macy 	error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
4268eda14cbcSMatt Macy 	if (error == -1)
4269eda14cbcSMatt Macy 		nomem();
4270eda14cbcSMatt Macy 	fnvlist_add_boolean(sd->sd_nvl, name);
4271eda14cbcSMatt Macy 	free(name);
4272eda14cbcSMatt Macy 
4273eda14cbcSMatt Macy 	if (sd->sd_recursive)
4274eda14cbcSMatt Macy 		rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
4275eda14cbcSMatt Macy 	zfs_close(zhp);
4276eda14cbcSMatt Macy 	return (rv);
4277eda14cbcSMatt Macy }
4278eda14cbcSMatt Macy 
4279eda14cbcSMatt Macy /*
4280eda14cbcSMatt Macy  * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
4281eda14cbcSMatt Macy  *
4282eda14cbcSMatt Macy  * Creates a snapshot with the given name.  While functionally equivalent to
4283eda14cbcSMatt Macy  * 'zfs create', it is a separate command to differentiate intent.
4284eda14cbcSMatt Macy  */
4285eda14cbcSMatt Macy static int
4286eda14cbcSMatt Macy zfs_do_snapshot(int argc, char **argv)
4287eda14cbcSMatt Macy {
4288eda14cbcSMatt Macy 	int ret = 0;
4289eda14cbcSMatt Macy 	int c;
4290eda14cbcSMatt Macy 	nvlist_t *props;
4291eda14cbcSMatt Macy 	snap_cbdata_t sd = { 0 };
4292eda14cbcSMatt Macy 	boolean_t multiple_snaps = B_FALSE;
4293eda14cbcSMatt Macy 
4294eda14cbcSMatt Macy 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4295eda14cbcSMatt Macy 		nomem();
4296eda14cbcSMatt Macy 	if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
4297eda14cbcSMatt Macy 		nomem();
4298eda14cbcSMatt Macy 
4299eda14cbcSMatt Macy 	/* check options */
4300eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, "ro:")) != -1) {
4301eda14cbcSMatt Macy 		switch (c) {
4302eda14cbcSMatt Macy 		case 'o':
4303eda14cbcSMatt Macy 			if (!parseprop(props, optarg)) {
4304eda14cbcSMatt Macy 				nvlist_free(sd.sd_nvl);
4305eda14cbcSMatt Macy 				nvlist_free(props);
4306eda14cbcSMatt Macy 				return (1);
4307eda14cbcSMatt Macy 			}
4308eda14cbcSMatt Macy 			break;
4309eda14cbcSMatt Macy 		case 'r':
4310eda14cbcSMatt Macy 			sd.sd_recursive = B_TRUE;
4311eda14cbcSMatt Macy 			multiple_snaps = B_TRUE;
4312eda14cbcSMatt Macy 			break;
4313eda14cbcSMatt Macy 		case '?':
4314eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4315eda14cbcSMatt Macy 			    optopt);
4316eda14cbcSMatt Macy 			goto usage;
4317eda14cbcSMatt Macy 		}
4318eda14cbcSMatt Macy 	}
4319eda14cbcSMatt Macy 
4320eda14cbcSMatt Macy 	argc -= optind;
4321eda14cbcSMatt Macy 	argv += optind;
4322eda14cbcSMatt Macy 
4323eda14cbcSMatt Macy 	/* check number of arguments */
4324eda14cbcSMatt Macy 	if (argc < 1) {
4325eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
4326eda14cbcSMatt Macy 		goto usage;
4327eda14cbcSMatt Macy 	}
4328eda14cbcSMatt Macy 
4329eda14cbcSMatt Macy 	if (argc > 1)
4330eda14cbcSMatt Macy 		multiple_snaps = B_TRUE;
4331eda14cbcSMatt Macy 	for (; argc > 0; argc--, argv++) {
4332eda14cbcSMatt Macy 		char *atp;
4333eda14cbcSMatt Macy 		zfs_handle_t *zhp;
4334eda14cbcSMatt Macy 
4335eda14cbcSMatt Macy 		atp = strchr(argv[0], '@');
4336eda14cbcSMatt Macy 		if (atp == NULL)
4337eda14cbcSMatt Macy 			goto usage;
4338eda14cbcSMatt Macy 		*atp = '\0';
4339eda14cbcSMatt Macy 		sd.sd_snapname = atp + 1;
4340eda14cbcSMatt Macy 		zhp = zfs_open(g_zfs, argv[0],
4341eda14cbcSMatt Macy 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4342eda14cbcSMatt Macy 		if (zhp == NULL)
4343eda14cbcSMatt Macy 			goto usage;
4344eda14cbcSMatt Macy 		if (zfs_snapshot_cb(zhp, &sd) != 0)
4345eda14cbcSMatt Macy 			goto usage;
4346eda14cbcSMatt Macy 	}
4347eda14cbcSMatt Macy 
4348eda14cbcSMatt Macy 	ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
4349eda14cbcSMatt Macy 	nvlist_free(sd.sd_nvl);
4350eda14cbcSMatt Macy 	nvlist_free(props);
4351eda14cbcSMatt Macy 	if (ret != 0 && multiple_snaps)
4352eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("no snapshots were created\n"));
4353eda14cbcSMatt Macy 	return (ret != 0);
4354eda14cbcSMatt Macy 
4355eda14cbcSMatt Macy usage:
4356eda14cbcSMatt Macy 	nvlist_free(sd.sd_nvl);
4357eda14cbcSMatt Macy 	nvlist_free(props);
4358eda14cbcSMatt Macy 	usage(B_FALSE);
4359eda14cbcSMatt Macy 	return (-1);
4360eda14cbcSMatt Macy }
4361eda14cbcSMatt Macy 
4362eda14cbcSMatt Macy 
4363eda14cbcSMatt Macy /*
4364eda14cbcSMatt Macy  * Send a backup stream to stdout.
4365eda14cbcSMatt Macy  */
4366eda14cbcSMatt Macy static int
4367eda14cbcSMatt Macy zfs_do_send(int argc, char **argv)
4368eda14cbcSMatt Macy {
4369eda14cbcSMatt Macy 	char *fromname = NULL;
4370eda14cbcSMatt Macy 	char *toname = NULL;
4371eda14cbcSMatt Macy 	char *resume_token = NULL;
4372eda14cbcSMatt Macy 	char *cp;
4373eda14cbcSMatt Macy 	zfs_handle_t *zhp;
4374eda14cbcSMatt Macy 	sendflags_t flags = { 0 };
4375eda14cbcSMatt Macy 	int c, err;
4376eda14cbcSMatt Macy 	nvlist_t *dbgnv = NULL;
4377eda14cbcSMatt Macy 	char *redactbook = NULL;
4378eda14cbcSMatt Macy 
4379eda14cbcSMatt Macy 	struct option long_options[] = {
4380eda14cbcSMatt Macy 		{"replicate",	no_argument,		NULL, 'R'},
4381eda14cbcSMatt Macy 		{"redact",	required_argument,	NULL, 'd'},
4382eda14cbcSMatt Macy 		{"props",	no_argument,		NULL, 'p'},
4383eda14cbcSMatt Macy 		{"parsable",	no_argument,		NULL, 'P'},
4384eda14cbcSMatt Macy 		{"dedup",	no_argument,		NULL, 'D'},
4385eda14cbcSMatt Macy 		{"verbose",	no_argument,		NULL, 'v'},
4386eda14cbcSMatt Macy 		{"dryrun",	no_argument,		NULL, 'n'},
4387eda14cbcSMatt Macy 		{"large-block",	no_argument,		NULL, 'L'},
4388eda14cbcSMatt Macy 		{"embed",	no_argument,		NULL, 'e'},
4389eda14cbcSMatt Macy 		{"resume",	required_argument,	NULL, 't'},
4390eda14cbcSMatt Macy 		{"compressed",	no_argument,		NULL, 'c'},
4391eda14cbcSMatt Macy 		{"raw",		no_argument,		NULL, 'w'},
4392eda14cbcSMatt Macy 		{"backup",	no_argument,		NULL, 'b'},
4393eda14cbcSMatt Macy 		{"holds",	no_argument,		NULL, 'h'},
4394eda14cbcSMatt Macy 		{"saved",	no_argument,		NULL, 'S'},
4395eda14cbcSMatt Macy 		{0, 0, 0, 0}
4396eda14cbcSMatt Macy 	};
4397eda14cbcSMatt Macy 
4398eda14cbcSMatt Macy 	/* check options */
4399eda14cbcSMatt Macy 	while ((c = getopt_long(argc, argv, ":i:I:RDpvnPLeht:cwbd:S",
4400eda14cbcSMatt Macy 	    long_options, NULL)) != -1) {
4401eda14cbcSMatt Macy 		switch (c) {
4402eda14cbcSMatt Macy 		case 'i':
4403eda14cbcSMatt Macy 			if (fromname)
4404eda14cbcSMatt Macy 				usage(B_FALSE);
4405eda14cbcSMatt Macy 			fromname = optarg;
4406eda14cbcSMatt Macy 			break;
4407eda14cbcSMatt Macy 		case 'I':
4408eda14cbcSMatt Macy 			if (fromname)
4409eda14cbcSMatt Macy 				usage(B_FALSE);
4410eda14cbcSMatt Macy 			fromname = optarg;
4411eda14cbcSMatt Macy 			flags.doall = B_TRUE;
4412eda14cbcSMatt Macy 			break;
4413eda14cbcSMatt Macy 		case 'R':
4414eda14cbcSMatt Macy 			flags.replicate = B_TRUE;
4415eda14cbcSMatt Macy 			break;
4416eda14cbcSMatt Macy 		case 'd':
4417eda14cbcSMatt Macy 			redactbook = optarg;
4418eda14cbcSMatt Macy 			break;
4419eda14cbcSMatt Macy 		case 'p':
4420eda14cbcSMatt Macy 			flags.props = B_TRUE;
4421eda14cbcSMatt Macy 			break;
4422eda14cbcSMatt Macy 		case 'b':
4423eda14cbcSMatt Macy 			flags.backup = B_TRUE;
4424eda14cbcSMatt Macy 			break;
4425eda14cbcSMatt Macy 		case 'h':
4426eda14cbcSMatt Macy 			flags.holds = B_TRUE;
4427eda14cbcSMatt Macy 			break;
4428eda14cbcSMatt Macy 		case 'P':
4429eda14cbcSMatt Macy 			flags.parsable = B_TRUE;
4430eda14cbcSMatt Macy 			break;
4431eda14cbcSMatt Macy 		case 'v':
4432eda14cbcSMatt Macy 			flags.verbosity++;
4433eda14cbcSMatt Macy 			flags.progress = B_TRUE;
4434eda14cbcSMatt Macy 			break;
4435eda14cbcSMatt Macy 		case 'D':
4436eda14cbcSMatt Macy 			(void) fprintf(stderr,
4437eda14cbcSMatt Macy 			    gettext("WARNING: deduplicated send is no "
4438eda14cbcSMatt Macy 			    "longer supported.  A regular,\n"
4439eda14cbcSMatt Macy 			    "non-deduplicated stream will be generated.\n\n"));
4440eda14cbcSMatt Macy 			break;
4441eda14cbcSMatt Macy 		case 'n':
4442eda14cbcSMatt Macy 			flags.dryrun = B_TRUE;
4443eda14cbcSMatt Macy 			break;
4444eda14cbcSMatt Macy 		case 'L':
4445eda14cbcSMatt Macy 			flags.largeblock = B_TRUE;
4446eda14cbcSMatt Macy 			break;
4447eda14cbcSMatt Macy 		case 'e':
4448eda14cbcSMatt Macy 			flags.embed_data = B_TRUE;
4449eda14cbcSMatt Macy 			break;
4450eda14cbcSMatt Macy 		case 't':
4451eda14cbcSMatt Macy 			resume_token = optarg;
4452eda14cbcSMatt Macy 			break;
4453eda14cbcSMatt Macy 		case 'c':
4454eda14cbcSMatt Macy 			flags.compress = B_TRUE;
4455eda14cbcSMatt Macy 			break;
4456eda14cbcSMatt Macy 		case 'w':
4457eda14cbcSMatt Macy 			flags.raw = B_TRUE;
4458eda14cbcSMatt Macy 			flags.compress = B_TRUE;
4459eda14cbcSMatt Macy 			flags.embed_data = B_TRUE;
4460eda14cbcSMatt Macy 			flags.largeblock = B_TRUE;
4461eda14cbcSMatt Macy 			break;
4462eda14cbcSMatt Macy 		case 'S':
4463eda14cbcSMatt Macy 			flags.saved = B_TRUE;
4464eda14cbcSMatt Macy 			break;
4465eda14cbcSMatt Macy 		case ':':
4466eda14cbcSMatt Macy 			/*
4467eda14cbcSMatt Macy 			 * If a parameter was not passed, optopt contains the
4468eda14cbcSMatt Macy 			 * value that would normally lead us into the
4469eda14cbcSMatt Macy 			 * appropriate case statement.  If it's > 256, then this
4470eda14cbcSMatt Macy 			 * must be a longopt and we should look at argv to get
4471eda14cbcSMatt Macy 			 * the string.  Otherwise it's just the character, so we
4472eda14cbcSMatt Macy 			 * should use it directly.
4473eda14cbcSMatt Macy 			 */
4474eda14cbcSMatt Macy 			if (optopt <= UINT8_MAX) {
4475eda14cbcSMatt Macy 				(void) fprintf(stderr,
4476eda14cbcSMatt Macy 				    gettext("missing argument for '%c' "
4477eda14cbcSMatt Macy 				    "option\n"), optopt);
4478eda14cbcSMatt Macy 			} else {
4479eda14cbcSMatt Macy 				(void) fprintf(stderr,
4480eda14cbcSMatt Macy 				    gettext("missing argument for '%s' "
4481eda14cbcSMatt Macy 				    "option\n"), argv[optind - 1]);
4482eda14cbcSMatt Macy 			}
4483eda14cbcSMatt Macy 			usage(B_FALSE);
4484eda14cbcSMatt Macy 			break;
4485eda14cbcSMatt Macy 		case '?':
4486eda14cbcSMatt Macy 			/*FALLTHROUGH*/
4487eda14cbcSMatt Macy 		default:
4488eda14cbcSMatt Macy 			/*
4489eda14cbcSMatt Macy 			 * If an invalid flag was passed, optopt contains the
4490eda14cbcSMatt Macy 			 * character if it was a short flag, or 0 if it was a
4491eda14cbcSMatt Macy 			 * longopt.
4492eda14cbcSMatt Macy 			 */
4493eda14cbcSMatt Macy 			if (optopt != 0) {
4494eda14cbcSMatt Macy 				(void) fprintf(stderr,
4495eda14cbcSMatt Macy 				    gettext("invalid option '%c'\n"), optopt);
4496eda14cbcSMatt Macy 			} else {
4497eda14cbcSMatt Macy 				(void) fprintf(stderr,
4498eda14cbcSMatt Macy 				    gettext("invalid option '%s'\n"),
4499eda14cbcSMatt Macy 				    argv[optind - 1]);
4500eda14cbcSMatt Macy 
4501eda14cbcSMatt Macy 			}
4502eda14cbcSMatt Macy 			usage(B_FALSE);
4503eda14cbcSMatt Macy 		}
4504eda14cbcSMatt Macy 	}
4505eda14cbcSMatt Macy 
4506eda14cbcSMatt Macy 	if (flags.parsable && flags.verbosity == 0)
4507eda14cbcSMatt Macy 		flags.verbosity = 1;
4508eda14cbcSMatt Macy 
4509eda14cbcSMatt Macy 	argc -= optind;
4510eda14cbcSMatt Macy 	argv += optind;
4511eda14cbcSMatt Macy 
4512eda14cbcSMatt Macy 	if (resume_token != NULL) {
4513eda14cbcSMatt Macy 		if (fromname != NULL || flags.replicate || flags.props ||
4514eda14cbcSMatt Macy 		    flags.backup || flags.holds ||
4515eda14cbcSMatt Macy 		    flags.saved || redactbook != NULL) {
4516eda14cbcSMatt Macy 			(void) fprintf(stderr,
4517eda14cbcSMatt Macy 			    gettext("invalid flags combined with -t\n"));
4518eda14cbcSMatt Macy 			usage(B_FALSE);
4519eda14cbcSMatt Macy 		}
4520eda14cbcSMatt Macy 		if (argc > 0) {
4521eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("too many arguments\n"));
4522eda14cbcSMatt Macy 			usage(B_FALSE);
4523eda14cbcSMatt Macy 		}
4524eda14cbcSMatt Macy 	} else {
4525eda14cbcSMatt Macy 		if (argc < 1) {
4526eda14cbcSMatt Macy 			(void) fprintf(stderr,
4527eda14cbcSMatt Macy 			    gettext("missing snapshot argument\n"));
4528eda14cbcSMatt Macy 			usage(B_FALSE);
4529eda14cbcSMatt Macy 		}
4530eda14cbcSMatt Macy 		if (argc > 1) {
4531eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("too many arguments\n"));
4532eda14cbcSMatt Macy 			usage(B_FALSE);
4533eda14cbcSMatt Macy 		}
4534eda14cbcSMatt Macy 	}
4535eda14cbcSMatt Macy 
4536eda14cbcSMatt Macy 	if (flags.saved) {
4537eda14cbcSMatt Macy 		if (fromname != NULL || flags.replicate || flags.props ||
4538eda14cbcSMatt Macy 		    flags.doall || flags.backup ||
4539eda14cbcSMatt Macy 		    flags.holds || flags.largeblock || flags.embed_data ||
4540eda14cbcSMatt Macy 		    flags.compress || flags.raw || redactbook != NULL) {
4541eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("incompatible flags "
4542eda14cbcSMatt Macy 			    "combined with saved send flag\n"));
4543eda14cbcSMatt Macy 			usage(B_FALSE);
4544eda14cbcSMatt Macy 		}
4545eda14cbcSMatt Macy 		if (strchr(argv[0], '@') != NULL) {
4546eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("saved send must "
4547eda14cbcSMatt Macy 			    "specify the dataset with partially-received "
4548eda14cbcSMatt Macy 			    "state\n"));
4549eda14cbcSMatt Macy 			usage(B_FALSE);
4550eda14cbcSMatt Macy 		}
4551eda14cbcSMatt Macy 	}
4552eda14cbcSMatt Macy 
4553eda14cbcSMatt Macy 	if (flags.raw && redactbook != NULL) {
4554eda14cbcSMatt Macy 		(void) fprintf(stderr,
4555eda14cbcSMatt Macy 		    gettext("Error: raw sends may not be redacted.\n"));
4556eda14cbcSMatt Macy 		return (1);
4557eda14cbcSMatt Macy 	}
4558eda14cbcSMatt Macy 
4559eda14cbcSMatt Macy 	if (!flags.dryrun && isatty(STDOUT_FILENO)) {
4560eda14cbcSMatt Macy 		(void) fprintf(stderr,
4561eda14cbcSMatt Macy 		    gettext("Error: Stream can not be written to a terminal.\n"
4562eda14cbcSMatt Macy 		    "You must redirect standard output.\n"));
4563eda14cbcSMatt Macy 		return (1);
4564eda14cbcSMatt Macy 	}
4565eda14cbcSMatt Macy 
4566eda14cbcSMatt Macy 	if (flags.saved) {
4567eda14cbcSMatt Macy 		zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
4568eda14cbcSMatt Macy 		if (zhp == NULL)
4569eda14cbcSMatt Macy 			return (1);
4570eda14cbcSMatt Macy 
4571eda14cbcSMatt Macy 		err = zfs_send_saved(zhp, &flags, STDOUT_FILENO,
4572eda14cbcSMatt Macy 		    resume_token);
4573eda14cbcSMatt Macy 		zfs_close(zhp);
4574eda14cbcSMatt Macy 		return (err != 0);
4575eda14cbcSMatt Macy 	} else if (resume_token != NULL) {
4576eda14cbcSMatt Macy 		return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
4577eda14cbcSMatt Macy 		    resume_token));
4578eda14cbcSMatt Macy 	}
4579eda14cbcSMatt Macy 
4580eda14cbcSMatt Macy 	/*
4581eda14cbcSMatt Macy 	 * For everything except -R and -I, use the new, cleaner code path.
4582eda14cbcSMatt Macy 	 */
4583eda14cbcSMatt Macy 	if (!(flags.replicate || flags.doall)) {
4584eda14cbcSMatt Macy 		char frombuf[ZFS_MAX_DATASET_NAME_LEN];
4585eda14cbcSMatt Macy 
4586eda14cbcSMatt Macy 		if (fromname != NULL && (strchr(fromname, '#') == NULL &&
4587eda14cbcSMatt Macy 		    strchr(fromname, '@') == NULL)) {
4588eda14cbcSMatt Macy 			/*
4589eda14cbcSMatt Macy 			 * Neither bookmark or snapshot was specified.  Print a
4590eda14cbcSMatt Macy 			 * warning, and assume snapshot.
4591eda14cbcSMatt Macy 			 */
4592eda14cbcSMatt Macy 			(void) fprintf(stderr, "Warning: incremental source "
4593eda14cbcSMatt Macy 			    "didn't specify type, assuming snapshot. Use '@' "
4594eda14cbcSMatt Macy 			    "or '#' prefix to avoid ambiguity.\n");
4595eda14cbcSMatt Macy 			(void) snprintf(frombuf, sizeof (frombuf), "@%s",
4596eda14cbcSMatt Macy 			    fromname);
4597eda14cbcSMatt Macy 			fromname = frombuf;
4598eda14cbcSMatt Macy 		}
4599eda14cbcSMatt Macy 		if (fromname != NULL &&
4600eda14cbcSMatt Macy 		    (fromname[0] == '#' || fromname[0] == '@')) {
4601eda14cbcSMatt Macy 			/*
4602eda14cbcSMatt Macy 			 * Incremental source name begins with # or @.
4603eda14cbcSMatt Macy 			 * Default to same fs as target.
4604eda14cbcSMatt Macy 			 */
4605eda14cbcSMatt Macy 			char tmpbuf[ZFS_MAX_DATASET_NAME_LEN];
4606eda14cbcSMatt Macy 			(void) strlcpy(tmpbuf, fromname, sizeof (tmpbuf));
4607eda14cbcSMatt Macy 			(void) strlcpy(frombuf, argv[0], sizeof (frombuf));
4608eda14cbcSMatt Macy 			cp = strchr(frombuf, '@');
4609eda14cbcSMatt Macy 			if (cp != NULL)
4610eda14cbcSMatt Macy 				*cp = '\0';
4611eda14cbcSMatt Macy 			(void) strlcat(frombuf, tmpbuf, sizeof (frombuf));
4612eda14cbcSMatt Macy 			fromname = frombuf;
4613eda14cbcSMatt Macy 		}
4614eda14cbcSMatt Macy 
4615eda14cbcSMatt Macy 		zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
4616eda14cbcSMatt Macy 		if (zhp == NULL)
4617eda14cbcSMatt Macy 			return (1);
4618eda14cbcSMatt Macy 		err = zfs_send_one(zhp, fromname, STDOUT_FILENO, &flags,
4619eda14cbcSMatt Macy 		    redactbook);
4620eda14cbcSMatt Macy 		zfs_close(zhp);
4621eda14cbcSMatt Macy 		return (err != 0);
4622eda14cbcSMatt Macy 	}
4623eda14cbcSMatt Macy 
4624eda14cbcSMatt Macy 	if (fromname != NULL && strchr(fromname, '#')) {
4625eda14cbcSMatt Macy 		(void) fprintf(stderr,
4626eda14cbcSMatt Macy 		    gettext("Error: multiple snapshots cannot be "
4627eda14cbcSMatt Macy 		    "sent from a bookmark.\n"));
4628eda14cbcSMatt Macy 		return (1);
4629eda14cbcSMatt Macy 	}
4630eda14cbcSMatt Macy 
4631eda14cbcSMatt Macy 	if (redactbook != NULL) {
4632eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("Error: multiple snapshots "
4633eda14cbcSMatt Macy 		    "cannot be sent redacted.\n"));
4634eda14cbcSMatt Macy 		return (1);
4635eda14cbcSMatt Macy 	}
4636eda14cbcSMatt Macy 
4637eda14cbcSMatt Macy 	if ((cp = strchr(argv[0], '@')) == NULL) {
4638eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("Error: "
4639eda14cbcSMatt Macy 		    "Unsupported flag with filesystem or bookmark.\n"));
4640eda14cbcSMatt Macy 		return (1);
4641eda14cbcSMatt Macy 	}
4642eda14cbcSMatt Macy 	*cp = '\0';
4643eda14cbcSMatt Macy 	toname = cp + 1;
4644eda14cbcSMatt Macy 	zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4645eda14cbcSMatt Macy 	if (zhp == NULL)
4646eda14cbcSMatt Macy 		return (1);
4647eda14cbcSMatt Macy 
4648eda14cbcSMatt Macy 	/*
4649eda14cbcSMatt Macy 	 * If they specified the full path to the snapshot, chop off
4650eda14cbcSMatt Macy 	 * everything except the short name of the snapshot, but special
4651eda14cbcSMatt Macy 	 * case if they specify the origin.
4652eda14cbcSMatt Macy 	 */
4653eda14cbcSMatt Macy 	if (fromname && (cp = strchr(fromname, '@')) != NULL) {
4654eda14cbcSMatt Macy 		char origin[ZFS_MAX_DATASET_NAME_LEN];
4655eda14cbcSMatt Macy 		zprop_source_t src;
4656eda14cbcSMatt Macy 
4657eda14cbcSMatt Macy 		(void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
4658eda14cbcSMatt Macy 		    origin, sizeof (origin), &src, NULL, 0, B_FALSE);
4659eda14cbcSMatt Macy 
4660eda14cbcSMatt Macy 		if (strcmp(origin, fromname) == 0) {
4661eda14cbcSMatt Macy 			fromname = NULL;
4662eda14cbcSMatt Macy 			flags.fromorigin = B_TRUE;
4663eda14cbcSMatt Macy 		} else {
4664eda14cbcSMatt Macy 			*cp = '\0';
4665eda14cbcSMatt Macy 			if (cp != fromname && strcmp(argv[0], fromname)) {
4666eda14cbcSMatt Macy 				(void) fprintf(stderr,
4667eda14cbcSMatt Macy 				    gettext("incremental source must be "
4668eda14cbcSMatt Macy 				    "in same filesystem\n"));
4669eda14cbcSMatt Macy 				usage(B_FALSE);
4670eda14cbcSMatt Macy 			}
4671eda14cbcSMatt Macy 			fromname = cp + 1;
4672eda14cbcSMatt Macy 			if (strchr(fromname, '@') || strchr(fromname, '/')) {
4673eda14cbcSMatt Macy 				(void) fprintf(stderr,
4674eda14cbcSMatt Macy 				    gettext("invalid incremental source\n"));
4675eda14cbcSMatt Macy 				usage(B_FALSE);
4676eda14cbcSMatt Macy 			}
4677eda14cbcSMatt Macy 		}
4678eda14cbcSMatt Macy 	}
4679eda14cbcSMatt Macy 
4680eda14cbcSMatt Macy 	if (flags.replicate && fromname == NULL)
4681eda14cbcSMatt Macy 		flags.doall = B_TRUE;
4682eda14cbcSMatt Macy 
4683eda14cbcSMatt Macy 	err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
4684eda14cbcSMatt Macy 	    flags.verbosity >= 3 ? &dbgnv : NULL);
4685eda14cbcSMatt Macy 
4686eda14cbcSMatt Macy 	if (flags.verbosity >= 3 && dbgnv != NULL) {
4687eda14cbcSMatt Macy 		/*
4688eda14cbcSMatt Macy 		 * dump_nvlist prints to stdout, but that's been
4689eda14cbcSMatt Macy 		 * redirected to a file.  Make it print to stderr
4690eda14cbcSMatt Macy 		 * instead.
4691eda14cbcSMatt Macy 		 */
4692eda14cbcSMatt Macy 		(void) dup2(STDERR_FILENO, STDOUT_FILENO);
4693eda14cbcSMatt Macy 		dump_nvlist(dbgnv, 0);
4694eda14cbcSMatt Macy 		nvlist_free(dbgnv);
4695eda14cbcSMatt Macy 	}
4696eda14cbcSMatt Macy 	zfs_close(zhp);
4697eda14cbcSMatt Macy 
4698eda14cbcSMatt Macy 	return (err != 0);
4699eda14cbcSMatt Macy }
4700eda14cbcSMatt Macy 
4701eda14cbcSMatt Macy /*
4702eda14cbcSMatt Macy  * Restore a backup stream from stdin.
4703eda14cbcSMatt Macy  */
4704eda14cbcSMatt Macy static int
4705eda14cbcSMatt Macy zfs_do_receive(int argc, char **argv)
4706eda14cbcSMatt Macy {
4707eda14cbcSMatt Macy 	int c, err = 0;
4708eda14cbcSMatt Macy 	recvflags_t flags = { 0 };
4709eda14cbcSMatt Macy 	boolean_t abort_resumable = B_FALSE;
4710eda14cbcSMatt Macy 	nvlist_t *props;
4711eda14cbcSMatt Macy 
4712eda14cbcSMatt Macy 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4713eda14cbcSMatt Macy 		nomem();
4714eda14cbcSMatt Macy 
4715eda14cbcSMatt Macy 	/* check options */
4716eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, ":o:x:dehMnuvFsA")) != -1) {
4717eda14cbcSMatt Macy 		switch (c) {
4718eda14cbcSMatt Macy 		case 'o':
4719eda14cbcSMatt Macy 			if (!parseprop(props, optarg)) {
4720eda14cbcSMatt Macy 				nvlist_free(props);
4721eda14cbcSMatt Macy 				usage(B_FALSE);
4722eda14cbcSMatt Macy 			}
4723eda14cbcSMatt Macy 			break;
4724eda14cbcSMatt Macy 		case 'x':
4725eda14cbcSMatt Macy 			if (!parsepropname(props, optarg)) {
4726eda14cbcSMatt Macy 				nvlist_free(props);
4727eda14cbcSMatt Macy 				usage(B_FALSE);
4728eda14cbcSMatt Macy 			}
4729eda14cbcSMatt Macy 			break;
4730eda14cbcSMatt Macy 		case 'd':
4731eda14cbcSMatt Macy 			if (flags.istail) {
4732eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("invalid option "
4733eda14cbcSMatt Macy 				    "combination: -d and -e are mutually "
4734eda14cbcSMatt Macy 				    "exclusive\n"));
4735eda14cbcSMatt Macy 				usage(B_FALSE);
4736eda14cbcSMatt Macy 			}
4737eda14cbcSMatt Macy 			flags.isprefix = B_TRUE;
4738eda14cbcSMatt Macy 			break;
4739eda14cbcSMatt Macy 		case 'e':
4740eda14cbcSMatt Macy 			if (flags.isprefix) {
4741eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("invalid option "
4742eda14cbcSMatt Macy 				    "combination: -d and -e are mutually "
4743eda14cbcSMatt Macy 				    "exclusive\n"));
4744eda14cbcSMatt Macy 				usage(B_FALSE);
4745eda14cbcSMatt Macy 			}
4746eda14cbcSMatt Macy 			flags.istail = B_TRUE;
4747eda14cbcSMatt Macy 			break;
4748eda14cbcSMatt Macy 		case 'h':
4749eda14cbcSMatt Macy 			flags.skipholds = B_TRUE;
4750eda14cbcSMatt Macy 			break;
4751eda14cbcSMatt Macy 		case 'M':
4752eda14cbcSMatt Macy 			flags.forceunmount = B_TRUE;
4753eda14cbcSMatt Macy 			break;
4754eda14cbcSMatt Macy 		case 'n':
4755eda14cbcSMatt Macy 			flags.dryrun = B_TRUE;
4756eda14cbcSMatt Macy 			break;
4757eda14cbcSMatt Macy 		case 'u':
4758eda14cbcSMatt Macy 			flags.nomount = B_TRUE;
4759eda14cbcSMatt Macy 			break;
4760eda14cbcSMatt Macy 		case 'v':
4761eda14cbcSMatt Macy 			flags.verbose = B_TRUE;
4762eda14cbcSMatt Macy 			break;
4763eda14cbcSMatt Macy 		case 's':
4764eda14cbcSMatt Macy 			flags.resumable = B_TRUE;
4765eda14cbcSMatt Macy 			break;
4766eda14cbcSMatt Macy 		case 'F':
4767eda14cbcSMatt Macy 			flags.force = B_TRUE;
4768eda14cbcSMatt Macy 			break;
4769eda14cbcSMatt Macy 		case 'A':
4770eda14cbcSMatt Macy 			abort_resumable = B_TRUE;
4771eda14cbcSMatt Macy 			break;
4772eda14cbcSMatt Macy 		case ':':
4773eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("missing argument for "
4774eda14cbcSMatt Macy 			    "'%c' option\n"), optopt);
4775eda14cbcSMatt Macy 			usage(B_FALSE);
4776eda14cbcSMatt Macy 			break;
4777eda14cbcSMatt Macy 		case '?':
4778eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
4779eda14cbcSMatt Macy 			    optopt);
4780eda14cbcSMatt Macy 			usage(B_FALSE);
4781eda14cbcSMatt Macy 		}
4782eda14cbcSMatt Macy 	}
4783eda14cbcSMatt Macy 
4784eda14cbcSMatt Macy 	argc -= optind;
4785eda14cbcSMatt Macy 	argv += optind;
4786eda14cbcSMatt Macy 
4787eda14cbcSMatt Macy 	/* zfs recv -e (use "tail" name) implies -d (remove dataset "head") */
4788eda14cbcSMatt Macy 	if (flags.istail)
4789eda14cbcSMatt Macy 		flags.isprefix = B_TRUE;
4790eda14cbcSMatt Macy 
4791eda14cbcSMatt Macy 	/* check number of arguments */
4792eda14cbcSMatt Macy 	if (argc < 1) {
4793eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
4794eda14cbcSMatt Macy 		usage(B_FALSE);
4795eda14cbcSMatt Macy 	}
4796eda14cbcSMatt Macy 	if (argc > 1) {
4797eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("too many arguments\n"));
4798eda14cbcSMatt Macy 		usage(B_FALSE);
4799eda14cbcSMatt Macy 	}
4800eda14cbcSMatt Macy 
4801eda14cbcSMatt Macy 	if (abort_resumable) {
4802eda14cbcSMatt Macy 		if (flags.isprefix || flags.istail || flags.dryrun ||
4803eda14cbcSMatt Macy 		    flags.resumable || flags.nomount) {
4804eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option\n"));
4805eda14cbcSMatt Macy 			usage(B_FALSE);
4806eda14cbcSMatt Macy 		}
4807eda14cbcSMatt Macy 
4808eda14cbcSMatt Macy 		char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4809eda14cbcSMatt Macy 		(void) snprintf(namebuf, sizeof (namebuf),
4810eda14cbcSMatt Macy 		    "%s/%%recv", argv[0]);
4811eda14cbcSMatt Macy 
4812eda14cbcSMatt Macy 		if (zfs_dataset_exists(g_zfs, namebuf,
4813eda14cbcSMatt Macy 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4814eda14cbcSMatt Macy 			zfs_handle_t *zhp = zfs_open(g_zfs,
4815eda14cbcSMatt Macy 			    namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4816eda14cbcSMatt Macy 			if (zhp == NULL) {
4817eda14cbcSMatt Macy 				nvlist_free(props);
4818eda14cbcSMatt Macy 				return (1);
4819eda14cbcSMatt Macy 			}
4820eda14cbcSMatt Macy 			err = zfs_destroy(zhp, B_FALSE);
4821eda14cbcSMatt Macy 			zfs_close(zhp);
4822eda14cbcSMatt Macy 		} else {
4823eda14cbcSMatt Macy 			zfs_handle_t *zhp = zfs_open(g_zfs,
4824eda14cbcSMatt Macy 			    argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4825eda14cbcSMatt Macy 			if (zhp == NULL)
4826eda14cbcSMatt Macy 				usage(B_FALSE);
4827eda14cbcSMatt Macy 			if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4828eda14cbcSMatt Macy 			    zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4829eda14cbcSMatt Macy 			    NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4830eda14cbcSMatt Macy 				(void) fprintf(stderr,
4831eda14cbcSMatt Macy 				    gettext("'%s' does not have any "
4832eda14cbcSMatt Macy 				    "resumable receive state to abort\n"),
4833eda14cbcSMatt Macy 				    argv[0]);
4834eda14cbcSMatt Macy 				nvlist_free(props);
4835eda14cbcSMatt Macy 				zfs_close(zhp);
4836eda14cbcSMatt Macy 				return (1);
4837eda14cbcSMatt Macy 			}
4838eda14cbcSMatt Macy 			err = zfs_destroy(zhp, B_FALSE);
4839eda14cbcSMatt Macy 			zfs_close(zhp);
4840eda14cbcSMatt Macy 		}
4841eda14cbcSMatt Macy 		nvlist_free(props);
4842eda14cbcSMatt Macy 		return (err != 0);
4843eda14cbcSMatt Macy 	}
4844eda14cbcSMatt Macy 
4845eda14cbcSMatt Macy 	if (isatty(STDIN_FILENO)) {
4846eda14cbcSMatt Macy 		(void) fprintf(stderr,
4847eda14cbcSMatt Macy 		    gettext("Error: Backup stream can not be read "
4848eda14cbcSMatt Macy 		    "from a terminal.\n"
4849eda14cbcSMatt Macy 		    "You must redirect standard input.\n"));
4850eda14cbcSMatt Macy 		nvlist_free(props);
4851eda14cbcSMatt Macy 		return (1);
4852eda14cbcSMatt Macy 	}
4853eda14cbcSMatt Macy 	err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4854eda14cbcSMatt Macy 	nvlist_free(props);
4855eda14cbcSMatt Macy 
4856eda14cbcSMatt Macy 	return (err != 0);
4857eda14cbcSMatt Macy }
4858eda14cbcSMatt Macy 
4859eda14cbcSMatt Macy /*
4860eda14cbcSMatt Macy  * allow/unallow stuff
4861eda14cbcSMatt Macy  */
4862eda14cbcSMatt Macy /* copied from zfs/sys/dsl_deleg.h */
4863eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_CREATE		"create"
4864eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_DESTROY		"destroy"
4865eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_SNAPSHOT		"snapshot"
4866eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_ROLLBACK		"rollback"
4867eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_CLONE		"clone"
4868eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_PROMOTE		"promote"
4869eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_RENAME		"rename"
4870eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_MOUNT		"mount"
4871eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_SHARE		"share"
4872eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_SEND		"send"
4873eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_RECEIVE		"receive"
4874eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_ALLOW		"allow"
4875eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_USERPROP		"userprop"
4876eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_VSCAN		"vscan" /* ??? */
4877eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_USERQUOTA	"userquota"
4878eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_GROUPQUOTA	"groupquota"
4879eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_USERUSED		"userused"
4880eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_GROUPUSED	"groupused"
4881eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_USEROBJQUOTA	"userobjquota"
4882eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_GROUPOBJQUOTA	"groupobjquota"
4883eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_USEROBJUSED	"userobjused"
4884eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_GROUPOBJUSED	"groupobjused"
4885eda14cbcSMatt Macy 
4886eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_HOLD		"hold"
4887eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_RELEASE		"release"
4888eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_DIFF		"diff"
4889eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_BOOKMARK		"bookmark"
4890eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_LOAD_KEY		"load-key"
4891eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_CHANGE_KEY	"change-key"
4892eda14cbcSMatt Macy 
4893eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_PROJECTUSED	"projectused"
4894eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_PROJECTQUOTA	"projectquota"
4895eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_PROJECTOBJUSED	"projectobjused"
4896eda14cbcSMatt Macy #define	ZFS_DELEG_PERM_PROJECTOBJQUOTA	"projectobjquota"
4897eda14cbcSMatt Macy 
4898eda14cbcSMatt Macy #define	ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4899eda14cbcSMatt Macy 
4900eda14cbcSMatt Macy static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4901eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4902eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4903eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4904eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4905eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4906eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4907eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4908eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4909eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4910eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4911eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4912eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4913eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4914eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4915eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4916eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4917eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_LOAD_KEY, ZFS_DELEG_NOTE_LOAD_KEY },
4918eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_CHANGE_KEY, ZFS_DELEG_NOTE_CHANGE_KEY },
4919eda14cbcSMatt Macy 
4920eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4921eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4922eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4923eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4924eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4925eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_USEROBJQUOTA, ZFS_DELEG_NOTE_USEROBJQUOTA },
4926eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_USEROBJUSED, ZFS_DELEG_NOTE_USEROBJUSED },
4927eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_GROUPOBJQUOTA, ZFS_DELEG_NOTE_GROUPOBJQUOTA },
4928eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_GROUPOBJUSED, ZFS_DELEG_NOTE_GROUPOBJUSED },
4929eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_PROJECTUSED, ZFS_DELEG_NOTE_PROJECTUSED },
4930eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_PROJECTQUOTA, ZFS_DELEG_NOTE_PROJECTQUOTA },
4931eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_PROJECTOBJUSED, ZFS_DELEG_NOTE_PROJECTOBJUSED },
4932eda14cbcSMatt Macy 	{ ZFS_DELEG_PERM_PROJECTOBJQUOTA, ZFS_DELEG_NOTE_PROJECTOBJQUOTA },
4933eda14cbcSMatt Macy 	{ NULL, ZFS_DELEG_NOTE_NONE }
4934eda14cbcSMatt Macy };
4935eda14cbcSMatt Macy 
4936eda14cbcSMatt Macy /* permission structure */
4937eda14cbcSMatt Macy typedef struct deleg_perm {
4938eda14cbcSMatt Macy 	zfs_deleg_who_type_t	dp_who_type;
4939eda14cbcSMatt Macy 	const char		*dp_name;
4940eda14cbcSMatt Macy 	boolean_t		dp_local;
4941eda14cbcSMatt Macy 	boolean_t		dp_descend;
4942eda14cbcSMatt Macy } deleg_perm_t;
4943eda14cbcSMatt Macy 
4944eda14cbcSMatt Macy /* */
4945eda14cbcSMatt Macy typedef struct deleg_perm_node {
4946eda14cbcSMatt Macy 	deleg_perm_t		dpn_perm;
4947eda14cbcSMatt Macy 
4948eda14cbcSMatt Macy 	uu_avl_node_t		dpn_avl_node;
4949eda14cbcSMatt Macy } deleg_perm_node_t;
4950eda14cbcSMatt Macy 
4951eda14cbcSMatt Macy typedef struct fs_perm fs_perm_t;
4952eda14cbcSMatt Macy 
4953eda14cbcSMatt Macy /* permissions set */
4954eda14cbcSMatt Macy typedef struct who_perm {
4955eda14cbcSMatt Macy 	zfs_deleg_who_type_t	who_type;
4956eda14cbcSMatt Macy 	const char		*who_name;		/* id */
4957eda14cbcSMatt Macy 	char			who_ug_name[256];	/* user/group name */
4958eda14cbcSMatt Macy 	fs_perm_t		*who_fsperm;		/* uplink */
4959eda14cbcSMatt Macy 
4960eda14cbcSMatt Macy 	uu_avl_t		*who_deleg_perm_avl;	/* permissions */
4961eda14cbcSMatt Macy } who_perm_t;
4962eda14cbcSMatt Macy 
4963eda14cbcSMatt Macy /* */
4964eda14cbcSMatt Macy typedef struct who_perm_node {
4965eda14cbcSMatt Macy 	who_perm_t	who_perm;
4966eda14cbcSMatt Macy 	uu_avl_node_t	who_avl_node;
4967eda14cbcSMatt Macy } who_perm_node_t;
4968eda14cbcSMatt Macy 
4969eda14cbcSMatt Macy typedef struct fs_perm_set fs_perm_set_t;
4970eda14cbcSMatt Macy /* fs permissions */
4971eda14cbcSMatt Macy struct fs_perm {
4972eda14cbcSMatt Macy 	const char		*fsp_name;
4973eda14cbcSMatt Macy 
4974eda14cbcSMatt Macy 	uu_avl_t		*fsp_sc_avl;	/* sets,create */
4975eda14cbcSMatt Macy 	uu_avl_t		*fsp_uge_avl;	/* user,group,everyone */
4976eda14cbcSMatt Macy 
4977eda14cbcSMatt Macy 	fs_perm_set_t		*fsp_set;	/* uplink */
4978eda14cbcSMatt Macy };
4979eda14cbcSMatt Macy 
4980eda14cbcSMatt Macy /* */
4981eda14cbcSMatt Macy typedef struct fs_perm_node {
4982eda14cbcSMatt Macy 	fs_perm_t	fspn_fsperm;
4983eda14cbcSMatt Macy 	uu_avl_t	*fspn_avl;
4984eda14cbcSMatt Macy 
4985eda14cbcSMatt Macy 	uu_list_node_t	fspn_list_node;
4986eda14cbcSMatt Macy } fs_perm_node_t;
4987eda14cbcSMatt Macy 
4988eda14cbcSMatt Macy /* top level structure */
4989eda14cbcSMatt Macy struct fs_perm_set {
4990eda14cbcSMatt Macy 	uu_list_pool_t	*fsps_list_pool;
4991eda14cbcSMatt Macy 	uu_list_t	*fsps_list; /* list of fs_perms */
4992eda14cbcSMatt Macy 
4993eda14cbcSMatt Macy 	uu_avl_pool_t	*fsps_named_set_avl_pool;
4994eda14cbcSMatt Macy 	uu_avl_pool_t	*fsps_who_perm_avl_pool;
4995eda14cbcSMatt Macy 	uu_avl_pool_t	*fsps_deleg_perm_avl_pool;
4996eda14cbcSMatt Macy };
4997eda14cbcSMatt Macy 
4998eda14cbcSMatt Macy static inline const char *
4999eda14cbcSMatt Macy deleg_perm_type(zfs_deleg_note_t note)
5000eda14cbcSMatt Macy {
5001eda14cbcSMatt Macy 	/* subcommands */
5002eda14cbcSMatt Macy 	switch (note) {
5003eda14cbcSMatt Macy 		/* SUBCOMMANDS */
5004eda14cbcSMatt Macy 		/* OTHER */
5005eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_GROUPQUOTA:
5006eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_GROUPUSED:
5007eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_USERPROP:
5008eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_USERQUOTA:
5009eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_USERUSED:
5010eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_USEROBJQUOTA:
5011eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_USEROBJUSED:
5012eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
5013eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_GROUPOBJUSED:
5014eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_PROJECTUSED:
5015eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_PROJECTQUOTA:
5016eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_PROJECTOBJUSED:
5017eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_PROJECTOBJQUOTA:
5018eda14cbcSMatt Macy 		/* other */
5019eda14cbcSMatt Macy 		return (gettext("other"));
5020eda14cbcSMatt Macy 	default:
5021eda14cbcSMatt Macy 		return (gettext("subcommand"));
5022eda14cbcSMatt Macy 	}
5023eda14cbcSMatt Macy }
5024eda14cbcSMatt Macy 
5025eda14cbcSMatt Macy static int
5026eda14cbcSMatt Macy who_type2weight(zfs_deleg_who_type_t who_type)
5027eda14cbcSMatt Macy {
5028eda14cbcSMatt Macy 	int res;
5029eda14cbcSMatt Macy 	switch (who_type) {
5030eda14cbcSMatt Macy 		case ZFS_DELEG_NAMED_SET_SETS:
5031eda14cbcSMatt Macy 		case ZFS_DELEG_NAMED_SET:
5032eda14cbcSMatt Macy 			res = 0;
5033eda14cbcSMatt Macy 			break;
5034eda14cbcSMatt Macy 		case ZFS_DELEG_CREATE_SETS:
5035eda14cbcSMatt Macy 		case ZFS_DELEG_CREATE:
5036eda14cbcSMatt Macy 			res = 1;
5037eda14cbcSMatt Macy 			break;
5038eda14cbcSMatt Macy 		case ZFS_DELEG_USER_SETS:
5039eda14cbcSMatt Macy 		case ZFS_DELEG_USER:
5040eda14cbcSMatt Macy 			res = 2;
5041eda14cbcSMatt Macy 			break;
5042eda14cbcSMatt Macy 		case ZFS_DELEG_GROUP_SETS:
5043eda14cbcSMatt Macy 		case ZFS_DELEG_GROUP:
5044eda14cbcSMatt Macy 			res = 3;
5045eda14cbcSMatt Macy 			break;
5046eda14cbcSMatt Macy 		case ZFS_DELEG_EVERYONE_SETS:
5047eda14cbcSMatt Macy 		case ZFS_DELEG_EVERYONE:
5048eda14cbcSMatt Macy 			res = 4;
5049eda14cbcSMatt Macy 			break;
5050eda14cbcSMatt Macy 		default:
5051eda14cbcSMatt Macy 			res = -1;
5052eda14cbcSMatt Macy 	}
5053eda14cbcSMatt Macy 
5054eda14cbcSMatt Macy 	return (res);
5055eda14cbcSMatt Macy }
5056eda14cbcSMatt Macy 
5057eda14cbcSMatt Macy /* ARGSUSED */
5058eda14cbcSMatt Macy static int
5059eda14cbcSMatt Macy who_perm_compare(const void *larg, const void *rarg, void *unused)
5060eda14cbcSMatt Macy {
5061eda14cbcSMatt Macy 	const who_perm_node_t *l = larg;
5062eda14cbcSMatt Macy 	const who_perm_node_t *r = rarg;
5063eda14cbcSMatt Macy 	zfs_deleg_who_type_t ltype = l->who_perm.who_type;
5064eda14cbcSMatt Macy 	zfs_deleg_who_type_t rtype = r->who_perm.who_type;
5065eda14cbcSMatt Macy 	int lweight = who_type2weight(ltype);
5066eda14cbcSMatt Macy 	int rweight = who_type2weight(rtype);
5067eda14cbcSMatt Macy 	int res = lweight - rweight;
5068eda14cbcSMatt Macy 	if (res == 0)
5069eda14cbcSMatt Macy 		res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
5070eda14cbcSMatt Macy 		    ZFS_MAX_DELEG_NAME-1);
5071eda14cbcSMatt Macy 
5072eda14cbcSMatt Macy 	if (res == 0)
5073eda14cbcSMatt Macy 		return (0);
5074eda14cbcSMatt Macy 	if (res > 0)
5075eda14cbcSMatt Macy 		return (1);
5076eda14cbcSMatt Macy 	else
5077eda14cbcSMatt Macy 		return (-1);
5078eda14cbcSMatt Macy }
5079eda14cbcSMatt Macy 
5080eda14cbcSMatt Macy /* ARGSUSED */
5081eda14cbcSMatt Macy static int
5082eda14cbcSMatt Macy deleg_perm_compare(const void *larg, const void *rarg, void *unused)
5083eda14cbcSMatt Macy {
5084eda14cbcSMatt Macy 	const deleg_perm_node_t *l = larg;
5085eda14cbcSMatt Macy 	const deleg_perm_node_t *r = rarg;
5086eda14cbcSMatt Macy 	int res =  strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
5087eda14cbcSMatt Macy 	    ZFS_MAX_DELEG_NAME-1);
5088eda14cbcSMatt Macy 
5089eda14cbcSMatt Macy 	if (res == 0)
5090eda14cbcSMatt Macy 		return (0);
5091eda14cbcSMatt Macy 
5092eda14cbcSMatt Macy 	if (res > 0)
5093eda14cbcSMatt Macy 		return (1);
5094eda14cbcSMatt Macy 	else
5095eda14cbcSMatt Macy 		return (-1);
5096eda14cbcSMatt Macy }
5097eda14cbcSMatt Macy 
5098eda14cbcSMatt Macy static inline void
5099eda14cbcSMatt Macy fs_perm_set_init(fs_perm_set_t *fspset)
5100eda14cbcSMatt Macy {
5101eda14cbcSMatt Macy 	bzero(fspset, sizeof (fs_perm_set_t));
5102eda14cbcSMatt Macy 
5103eda14cbcSMatt Macy 	if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
5104eda14cbcSMatt Macy 	    sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
5105eda14cbcSMatt Macy 	    NULL, UU_DEFAULT)) == NULL)
5106eda14cbcSMatt Macy 		nomem();
5107eda14cbcSMatt Macy 	if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
5108eda14cbcSMatt Macy 	    UU_DEFAULT)) == NULL)
5109eda14cbcSMatt Macy 		nomem();
5110eda14cbcSMatt Macy 
5111eda14cbcSMatt Macy 	if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
5112eda14cbcSMatt Macy 	    "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
5113eda14cbcSMatt Macy 	    who_perm_node_t, who_avl_node), who_perm_compare,
5114eda14cbcSMatt Macy 	    UU_DEFAULT)) == NULL)
5115eda14cbcSMatt Macy 		nomem();
5116eda14cbcSMatt Macy 
5117eda14cbcSMatt Macy 	if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
5118eda14cbcSMatt Macy 	    "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
5119eda14cbcSMatt Macy 	    who_perm_node_t, who_avl_node), who_perm_compare,
5120eda14cbcSMatt Macy 	    UU_DEFAULT)) == NULL)
5121eda14cbcSMatt Macy 		nomem();
5122eda14cbcSMatt Macy 
5123eda14cbcSMatt Macy 	if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
5124eda14cbcSMatt Macy 	    "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
5125eda14cbcSMatt Macy 	    deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
5126eda14cbcSMatt Macy 	    == NULL)
5127eda14cbcSMatt Macy 		nomem();
5128eda14cbcSMatt Macy }
5129eda14cbcSMatt Macy 
5130eda14cbcSMatt Macy static inline void fs_perm_fini(fs_perm_t *);
5131eda14cbcSMatt Macy static inline void who_perm_fini(who_perm_t *);
5132eda14cbcSMatt Macy 
5133eda14cbcSMatt Macy static inline void
5134eda14cbcSMatt Macy fs_perm_set_fini(fs_perm_set_t *fspset)
5135eda14cbcSMatt Macy {
5136eda14cbcSMatt Macy 	fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
5137eda14cbcSMatt Macy 
5138eda14cbcSMatt Macy 	while (node != NULL) {
5139eda14cbcSMatt Macy 		fs_perm_node_t *next_node =
5140eda14cbcSMatt Macy 		    uu_list_next(fspset->fsps_list, node);
5141eda14cbcSMatt Macy 		fs_perm_t *fsperm = &node->fspn_fsperm;
5142eda14cbcSMatt Macy 		fs_perm_fini(fsperm);
5143eda14cbcSMatt Macy 		uu_list_remove(fspset->fsps_list, node);
5144eda14cbcSMatt Macy 		free(node);
5145eda14cbcSMatt Macy 		node = next_node;
5146eda14cbcSMatt Macy 	}
5147eda14cbcSMatt Macy 
5148eda14cbcSMatt Macy 	uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
5149eda14cbcSMatt Macy 	uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
5150eda14cbcSMatt Macy 	uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
5151eda14cbcSMatt Macy }
5152eda14cbcSMatt Macy 
5153eda14cbcSMatt Macy static inline void
5154eda14cbcSMatt Macy deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
5155eda14cbcSMatt Macy     const char *name)
5156eda14cbcSMatt Macy {
5157eda14cbcSMatt Macy 	deleg_perm->dp_who_type = type;
5158eda14cbcSMatt Macy 	deleg_perm->dp_name = name;
5159eda14cbcSMatt Macy }
5160eda14cbcSMatt Macy 
5161eda14cbcSMatt Macy static inline void
5162eda14cbcSMatt Macy who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
5163eda14cbcSMatt Macy     zfs_deleg_who_type_t type, const char *name)
5164eda14cbcSMatt Macy {
5165eda14cbcSMatt Macy 	uu_avl_pool_t	*pool;
5166eda14cbcSMatt Macy 	pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
5167eda14cbcSMatt Macy 
5168eda14cbcSMatt Macy 	bzero(who_perm, sizeof (who_perm_t));
5169eda14cbcSMatt Macy 
5170eda14cbcSMatt Macy 	if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
5171eda14cbcSMatt Macy 	    UU_DEFAULT)) == NULL)
5172eda14cbcSMatt Macy 		nomem();
5173eda14cbcSMatt Macy 
5174eda14cbcSMatt Macy 	who_perm->who_type = type;
5175eda14cbcSMatt Macy 	who_perm->who_name = name;
5176eda14cbcSMatt Macy 	who_perm->who_fsperm = fsperm;
5177eda14cbcSMatt Macy }
5178eda14cbcSMatt Macy 
5179eda14cbcSMatt Macy static inline void
5180eda14cbcSMatt Macy who_perm_fini(who_perm_t *who_perm)
5181eda14cbcSMatt Macy {
5182eda14cbcSMatt Macy 	deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
5183eda14cbcSMatt Macy 
5184eda14cbcSMatt Macy 	while (node != NULL) {
5185eda14cbcSMatt Macy 		deleg_perm_node_t *next_node =
5186eda14cbcSMatt Macy 		    uu_avl_next(who_perm->who_deleg_perm_avl, node);
5187eda14cbcSMatt Macy 
5188eda14cbcSMatt Macy 		uu_avl_remove(who_perm->who_deleg_perm_avl, node);
5189eda14cbcSMatt Macy 		free(node);
5190eda14cbcSMatt Macy 		node = next_node;
5191eda14cbcSMatt Macy 	}
5192eda14cbcSMatt Macy 
5193eda14cbcSMatt Macy 	uu_avl_destroy(who_perm->who_deleg_perm_avl);
5194eda14cbcSMatt Macy }
5195eda14cbcSMatt Macy 
5196eda14cbcSMatt Macy static inline void
5197eda14cbcSMatt Macy fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
5198eda14cbcSMatt Macy {
5199eda14cbcSMatt Macy 	uu_avl_pool_t	*nset_pool = fspset->fsps_named_set_avl_pool;
5200eda14cbcSMatt Macy 	uu_avl_pool_t	*who_pool = fspset->fsps_who_perm_avl_pool;
5201eda14cbcSMatt Macy 
5202eda14cbcSMatt Macy 	bzero(fsperm, sizeof (fs_perm_t));
5203eda14cbcSMatt Macy 
5204eda14cbcSMatt Macy 	if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
5205eda14cbcSMatt Macy 	    == NULL)
5206eda14cbcSMatt Macy 		nomem();
5207eda14cbcSMatt Macy 
5208eda14cbcSMatt Macy 	if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
5209eda14cbcSMatt Macy 	    == NULL)
5210eda14cbcSMatt Macy 		nomem();
5211eda14cbcSMatt Macy 
5212eda14cbcSMatt Macy 	fsperm->fsp_set = fspset;
5213eda14cbcSMatt Macy 	fsperm->fsp_name = fsname;
5214eda14cbcSMatt Macy }
5215eda14cbcSMatt Macy 
5216eda14cbcSMatt Macy static inline void
5217eda14cbcSMatt Macy fs_perm_fini(fs_perm_t *fsperm)
5218eda14cbcSMatt Macy {
5219eda14cbcSMatt Macy 	who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
5220eda14cbcSMatt Macy 	while (node != NULL) {
5221eda14cbcSMatt Macy 		who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
5222eda14cbcSMatt Macy 		    node);
5223eda14cbcSMatt Macy 		who_perm_t *who_perm = &node->who_perm;
5224eda14cbcSMatt Macy 		who_perm_fini(who_perm);
5225eda14cbcSMatt Macy 		uu_avl_remove(fsperm->fsp_sc_avl, node);
5226eda14cbcSMatt Macy 		free(node);
5227eda14cbcSMatt Macy 		node = next_node;
5228eda14cbcSMatt Macy 	}
5229eda14cbcSMatt Macy 
5230eda14cbcSMatt Macy 	node = uu_avl_first(fsperm->fsp_uge_avl);
5231eda14cbcSMatt Macy 	while (node != NULL) {
5232eda14cbcSMatt Macy 		who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
5233eda14cbcSMatt Macy 		    node);
5234eda14cbcSMatt Macy 		who_perm_t *who_perm = &node->who_perm;
5235eda14cbcSMatt Macy 		who_perm_fini(who_perm);
5236eda14cbcSMatt Macy 		uu_avl_remove(fsperm->fsp_uge_avl, node);
5237eda14cbcSMatt Macy 		free(node);
5238eda14cbcSMatt Macy 		node = next_node;
5239eda14cbcSMatt Macy 	}
5240eda14cbcSMatt Macy 
5241eda14cbcSMatt Macy 	uu_avl_destroy(fsperm->fsp_sc_avl);
5242eda14cbcSMatt Macy 	uu_avl_destroy(fsperm->fsp_uge_avl);
5243eda14cbcSMatt Macy }
5244eda14cbcSMatt Macy 
5245eda14cbcSMatt Macy static void
5246eda14cbcSMatt Macy set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
5247eda14cbcSMatt Macy     zfs_deleg_who_type_t who_type, const char *name, char locality)
5248eda14cbcSMatt Macy {
5249eda14cbcSMatt Macy 	uu_avl_index_t idx = 0;
5250eda14cbcSMatt Macy 
5251eda14cbcSMatt Macy 	deleg_perm_node_t *found_node = NULL;
5252eda14cbcSMatt Macy 	deleg_perm_t	*deleg_perm = &node->dpn_perm;
5253eda14cbcSMatt Macy 
5254eda14cbcSMatt Macy 	deleg_perm_init(deleg_perm, who_type, name);
5255eda14cbcSMatt Macy 
5256eda14cbcSMatt Macy 	if ((found_node = uu_avl_find(avl, node, NULL, &idx))
5257eda14cbcSMatt Macy 	    == NULL)
5258eda14cbcSMatt Macy 		uu_avl_insert(avl, node, idx);
5259eda14cbcSMatt Macy 	else {
5260eda14cbcSMatt Macy 		node = found_node;
5261eda14cbcSMatt Macy 		deleg_perm = &node->dpn_perm;
5262eda14cbcSMatt Macy 	}
5263eda14cbcSMatt Macy 
5264eda14cbcSMatt Macy 
5265eda14cbcSMatt Macy 	switch (locality) {
5266eda14cbcSMatt Macy 	case ZFS_DELEG_LOCAL:
5267eda14cbcSMatt Macy 		deleg_perm->dp_local = B_TRUE;
5268eda14cbcSMatt Macy 		break;
5269eda14cbcSMatt Macy 	case ZFS_DELEG_DESCENDENT:
5270eda14cbcSMatt Macy 		deleg_perm->dp_descend = B_TRUE;
5271eda14cbcSMatt Macy 		break;
5272eda14cbcSMatt Macy 	case ZFS_DELEG_NA:
5273eda14cbcSMatt Macy 		break;
5274eda14cbcSMatt Macy 	default:
5275eda14cbcSMatt Macy 		assert(B_FALSE); /* invalid locality */
5276eda14cbcSMatt Macy 	}
5277eda14cbcSMatt Macy }
5278eda14cbcSMatt Macy 
5279eda14cbcSMatt Macy static inline int
5280eda14cbcSMatt Macy parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
5281eda14cbcSMatt Macy {
5282eda14cbcSMatt Macy 	nvpair_t *nvp = NULL;
5283eda14cbcSMatt Macy 	fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
5284eda14cbcSMatt Macy 	uu_avl_t *avl = who_perm->who_deleg_perm_avl;
5285eda14cbcSMatt Macy 	zfs_deleg_who_type_t who_type = who_perm->who_type;
5286eda14cbcSMatt Macy 
5287eda14cbcSMatt Macy 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5288eda14cbcSMatt Macy 		const char *name = nvpair_name(nvp);
5289eda14cbcSMatt Macy 		data_type_t type = nvpair_type(nvp);
5290eda14cbcSMatt Macy 		uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
5291eda14cbcSMatt Macy 		deleg_perm_node_t *node =
5292eda14cbcSMatt Macy 		    safe_malloc(sizeof (deleg_perm_node_t));
5293eda14cbcSMatt Macy 
5294eda14cbcSMatt Macy 		VERIFY(type == DATA_TYPE_BOOLEAN);
5295eda14cbcSMatt Macy 
5296eda14cbcSMatt Macy 		uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
5297eda14cbcSMatt Macy 		set_deleg_perm_node(avl, node, who_type, name, locality);
5298eda14cbcSMatt Macy 	}
5299eda14cbcSMatt Macy 
5300eda14cbcSMatt Macy 	return (0);
5301eda14cbcSMatt Macy }
5302eda14cbcSMatt Macy 
5303eda14cbcSMatt Macy static inline int
5304eda14cbcSMatt Macy parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
5305eda14cbcSMatt Macy {
5306eda14cbcSMatt Macy 	nvpair_t *nvp = NULL;
5307eda14cbcSMatt Macy 	fs_perm_set_t *fspset = fsperm->fsp_set;
5308eda14cbcSMatt Macy 
5309eda14cbcSMatt Macy 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5310eda14cbcSMatt Macy 		nvlist_t *nvl2 = NULL;
5311eda14cbcSMatt Macy 		const char *name = nvpair_name(nvp);
5312eda14cbcSMatt Macy 		uu_avl_t *avl = NULL;
5313eda14cbcSMatt Macy 		uu_avl_pool_t *avl_pool = NULL;
5314eda14cbcSMatt Macy 		zfs_deleg_who_type_t perm_type = name[0];
5315eda14cbcSMatt Macy 		char perm_locality = name[1];
5316eda14cbcSMatt Macy 		const char *perm_name = name + 3;
5317eda14cbcSMatt Macy 		who_perm_t *who_perm = NULL;
5318eda14cbcSMatt Macy 
5319eda14cbcSMatt Macy 		assert('$' == name[2]);
5320eda14cbcSMatt Macy 
5321eda14cbcSMatt Macy 		if (nvpair_value_nvlist(nvp, &nvl2) != 0)
5322eda14cbcSMatt Macy 			return (-1);
5323eda14cbcSMatt Macy 
5324eda14cbcSMatt Macy 		switch (perm_type) {
5325eda14cbcSMatt Macy 		case ZFS_DELEG_CREATE:
5326eda14cbcSMatt Macy 		case ZFS_DELEG_CREATE_SETS:
5327eda14cbcSMatt Macy 		case ZFS_DELEG_NAMED_SET:
5328eda14cbcSMatt Macy 		case ZFS_DELEG_NAMED_SET_SETS:
5329eda14cbcSMatt Macy 			avl_pool = fspset->fsps_named_set_avl_pool;
5330eda14cbcSMatt Macy 			avl = fsperm->fsp_sc_avl;
5331eda14cbcSMatt Macy 			break;
5332eda14cbcSMatt Macy 		case ZFS_DELEG_USER:
5333eda14cbcSMatt Macy 		case ZFS_DELEG_USER_SETS:
5334eda14cbcSMatt Macy 		case ZFS_DELEG_GROUP:
5335eda14cbcSMatt Macy 		case ZFS_DELEG_GROUP_SETS:
5336eda14cbcSMatt Macy 		case ZFS_DELEG_EVERYONE:
5337eda14cbcSMatt Macy 		case ZFS_DELEG_EVERYONE_SETS:
5338eda14cbcSMatt Macy 			avl_pool = fspset->fsps_who_perm_avl_pool;
5339eda14cbcSMatt Macy 			avl = fsperm->fsp_uge_avl;
5340eda14cbcSMatt Macy 			break;
5341eda14cbcSMatt Macy 
5342eda14cbcSMatt Macy 		default:
5343eda14cbcSMatt Macy 			assert(!"unhandled zfs_deleg_who_type_t");
5344eda14cbcSMatt Macy 		}
5345eda14cbcSMatt Macy 
5346eda14cbcSMatt Macy 		who_perm_node_t *found_node = NULL;
5347eda14cbcSMatt Macy 		who_perm_node_t *node = safe_malloc(
5348eda14cbcSMatt Macy 		    sizeof (who_perm_node_t));
5349eda14cbcSMatt Macy 		who_perm = &node->who_perm;
5350eda14cbcSMatt Macy 		uu_avl_index_t idx = 0;
5351eda14cbcSMatt Macy 
5352eda14cbcSMatt Macy 		uu_avl_node_init(node, &node->who_avl_node, avl_pool);
5353eda14cbcSMatt Macy 		who_perm_init(who_perm, fsperm, perm_type, perm_name);
5354eda14cbcSMatt Macy 
5355eda14cbcSMatt Macy 		if ((found_node = uu_avl_find(avl, node, NULL, &idx))
5356eda14cbcSMatt Macy 		    == NULL) {
5357eda14cbcSMatt Macy 			if (avl == fsperm->fsp_uge_avl) {
5358eda14cbcSMatt Macy 				uid_t rid = 0;
5359eda14cbcSMatt Macy 				struct passwd *p = NULL;
5360eda14cbcSMatt Macy 				struct group *g = NULL;
5361eda14cbcSMatt Macy 				const char *nice_name = NULL;
5362eda14cbcSMatt Macy 
5363eda14cbcSMatt Macy 				switch (perm_type) {
5364eda14cbcSMatt Macy 				case ZFS_DELEG_USER_SETS:
5365eda14cbcSMatt Macy 				case ZFS_DELEG_USER:
5366eda14cbcSMatt Macy 					rid = atoi(perm_name);
5367eda14cbcSMatt Macy 					p = getpwuid(rid);
5368eda14cbcSMatt Macy 					if (p)
5369eda14cbcSMatt Macy 						nice_name = p->pw_name;
5370eda14cbcSMatt Macy 					break;
5371eda14cbcSMatt Macy 				case ZFS_DELEG_GROUP_SETS:
5372eda14cbcSMatt Macy 				case ZFS_DELEG_GROUP:
5373eda14cbcSMatt Macy 					rid = atoi(perm_name);
5374eda14cbcSMatt Macy 					g = getgrgid(rid);
5375eda14cbcSMatt Macy 					if (g)
5376eda14cbcSMatt Macy 						nice_name = g->gr_name;
5377eda14cbcSMatt Macy 					break;
5378eda14cbcSMatt Macy 
5379eda14cbcSMatt Macy 				default:
5380eda14cbcSMatt Macy 					break;
5381eda14cbcSMatt Macy 				}
5382eda14cbcSMatt Macy 
5383eda14cbcSMatt Macy 				if (nice_name != NULL) {
5384eda14cbcSMatt Macy 					(void) strlcpy(
5385eda14cbcSMatt Macy 					    node->who_perm.who_ug_name,
5386eda14cbcSMatt Macy 					    nice_name, 256);
5387eda14cbcSMatt Macy 				} else {
5388eda14cbcSMatt Macy 					/* User or group unknown */
5389eda14cbcSMatt Macy 					(void) snprintf(
5390eda14cbcSMatt Macy 					    node->who_perm.who_ug_name,
5391eda14cbcSMatt Macy 					    sizeof (node->who_perm.who_ug_name),
5392eda14cbcSMatt Macy 					    "(unknown: %d)", rid);
5393eda14cbcSMatt Macy 				}
5394eda14cbcSMatt Macy 			}
5395eda14cbcSMatt Macy 
5396eda14cbcSMatt Macy 			uu_avl_insert(avl, node, idx);
5397eda14cbcSMatt Macy 		} else {
5398eda14cbcSMatt Macy 			node = found_node;
5399eda14cbcSMatt Macy 			who_perm = &node->who_perm;
5400eda14cbcSMatt Macy 		}
5401eda14cbcSMatt Macy 
5402eda14cbcSMatt Macy 		assert(who_perm != NULL);
5403eda14cbcSMatt Macy 		(void) parse_who_perm(who_perm, nvl2, perm_locality);
5404eda14cbcSMatt Macy 	}
5405eda14cbcSMatt Macy 
5406eda14cbcSMatt Macy 	return (0);
5407eda14cbcSMatt Macy }
5408eda14cbcSMatt Macy 
5409eda14cbcSMatt Macy static inline int
5410eda14cbcSMatt Macy parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
5411eda14cbcSMatt Macy {
5412eda14cbcSMatt Macy 	nvpair_t *nvp = NULL;
5413eda14cbcSMatt Macy 	uu_avl_index_t idx = 0;
5414eda14cbcSMatt Macy 
5415eda14cbcSMatt Macy 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5416eda14cbcSMatt Macy 		nvlist_t *nvl2 = NULL;
5417eda14cbcSMatt Macy 		const char *fsname = nvpair_name(nvp);
5418eda14cbcSMatt Macy 		data_type_t type = nvpair_type(nvp);
5419eda14cbcSMatt Macy 		fs_perm_t *fsperm = NULL;
5420eda14cbcSMatt Macy 		fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
5421eda14cbcSMatt Macy 		if (node == NULL)
5422eda14cbcSMatt Macy 			nomem();
5423eda14cbcSMatt Macy 
5424eda14cbcSMatt Macy 		fsperm = &node->fspn_fsperm;
5425eda14cbcSMatt Macy 
5426eda14cbcSMatt Macy 		VERIFY(DATA_TYPE_NVLIST == type);
5427eda14cbcSMatt Macy 
5428eda14cbcSMatt Macy 		uu_list_node_init(node, &node->fspn_list_node,
5429eda14cbcSMatt Macy 		    fspset->fsps_list_pool);
5430eda14cbcSMatt Macy 
5431eda14cbcSMatt Macy 		idx = uu_list_numnodes(fspset->fsps_list);
5432eda14cbcSMatt Macy 		fs_perm_init(fsperm, fspset, fsname);
5433eda14cbcSMatt Macy 
5434eda14cbcSMatt Macy 		if (nvpair_value_nvlist(nvp, &nvl2) != 0)
5435eda14cbcSMatt Macy 			return (-1);
5436eda14cbcSMatt Macy 
5437eda14cbcSMatt Macy 		(void) parse_fs_perm(fsperm, nvl2);
5438eda14cbcSMatt Macy 
5439eda14cbcSMatt Macy 		uu_list_insert(fspset->fsps_list, node, idx);
5440eda14cbcSMatt Macy 	}
5441eda14cbcSMatt Macy 
5442eda14cbcSMatt Macy 	return (0);
5443eda14cbcSMatt Macy }
5444eda14cbcSMatt Macy 
5445eda14cbcSMatt Macy static inline const char *
5446eda14cbcSMatt Macy deleg_perm_comment(zfs_deleg_note_t note)
5447eda14cbcSMatt Macy {
5448eda14cbcSMatt Macy 	const char *str = "";
5449eda14cbcSMatt Macy 
5450eda14cbcSMatt Macy 	/* subcommands */
5451eda14cbcSMatt Macy 	switch (note) {
5452eda14cbcSMatt Macy 		/* SUBCOMMANDS */
5453eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_ALLOW:
5454eda14cbcSMatt Macy 		str = gettext("Must also have the permission that is being"
5455eda14cbcSMatt Macy 		    "\n\t\t\t\tallowed");
5456eda14cbcSMatt Macy 		break;
5457eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_CLONE:
5458eda14cbcSMatt Macy 		str = gettext("Must also have the 'create' ability and 'mount'"
5459eda14cbcSMatt Macy 		    "\n\t\t\t\tability in the origin file system");
5460eda14cbcSMatt Macy 		break;
5461eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_CREATE:
5462eda14cbcSMatt Macy 		str = gettext("Must also have the 'mount' ability");
5463eda14cbcSMatt Macy 		break;
5464eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_DESTROY:
5465eda14cbcSMatt Macy 		str = gettext("Must also have the 'mount' ability");
5466eda14cbcSMatt Macy 		break;
5467eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_DIFF:
5468eda14cbcSMatt Macy 		str = gettext("Allows lookup of paths within a dataset;"
5469eda14cbcSMatt Macy 		    "\n\t\t\t\tgiven an object number. Ordinary users need this"
5470eda14cbcSMatt Macy 		    "\n\t\t\t\tin order to use zfs diff");
5471eda14cbcSMatt Macy 		break;
5472eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_HOLD:
5473eda14cbcSMatt Macy 		str = gettext("Allows adding a user hold to a snapshot");
5474eda14cbcSMatt Macy 		break;
5475eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_MOUNT:
5476eda14cbcSMatt Macy 		str = gettext("Allows mount/umount of ZFS datasets");
5477eda14cbcSMatt Macy 		break;
5478eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_PROMOTE:
5479eda14cbcSMatt Macy 		str = gettext("Must also have the 'mount'\n\t\t\t\tand"
5480eda14cbcSMatt Macy 		    " 'promote' ability in the origin file system");
5481eda14cbcSMatt Macy 		break;
5482eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_RECEIVE:
5483eda14cbcSMatt Macy 		str = gettext("Must also have the 'mount' and 'create'"
5484eda14cbcSMatt Macy 		    " ability");
5485eda14cbcSMatt Macy 		break;
5486eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_RELEASE:
5487eda14cbcSMatt Macy 		str = gettext("Allows releasing a user hold which\n\t\t\t\t"
5488eda14cbcSMatt Macy 		    "might destroy the snapshot");
5489eda14cbcSMatt Macy 		break;
5490eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_RENAME:
5491eda14cbcSMatt Macy 		str = gettext("Must also have the 'mount' and 'create'"
5492eda14cbcSMatt Macy 		    "\n\t\t\t\tability in the new parent");
5493eda14cbcSMatt Macy 		break;
5494eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_ROLLBACK:
5495eda14cbcSMatt Macy 		str = gettext("");
5496eda14cbcSMatt Macy 		break;
5497eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_SEND:
5498eda14cbcSMatt Macy 		str = gettext("");
5499eda14cbcSMatt Macy 		break;
5500eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_SHARE:
5501eda14cbcSMatt Macy 		str = gettext("Allows sharing file systems over NFS or SMB"
5502eda14cbcSMatt Macy 		    "\n\t\t\t\tprotocols");
5503eda14cbcSMatt Macy 		break;
5504eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_SNAPSHOT:
5505eda14cbcSMatt Macy 		str = gettext("");
5506eda14cbcSMatt Macy 		break;
5507eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_LOAD_KEY:
5508eda14cbcSMatt Macy 		str = gettext("Allows loading or unloading an encryption key");
5509eda14cbcSMatt Macy 		break;
5510eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_CHANGE_KEY:
5511eda14cbcSMatt Macy 		str = gettext("Allows changing or adding an encryption key");
5512eda14cbcSMatt Macy 		break;
5513eda14cbcSMatt Macy /*
5514eda14cbcSMatt Macy  *	case ZFS_DELEG_NOTE_VSCAN:
5515eda14cbcSMatt Macy  *		str = gettext("");
5516eda14cbcSMatt Macy  *		break;
5517eda14cbcSMatt Macy  */
5518eda14cbcSMatt Macy 		/* OTHER */
5519eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_GROUPQUOTA:
5520eda14cbcSMatt Macy 		str = gettext("Allows accessing any groupquota@... property");
5521eda14cbcSMatt Macy 		break;
5522eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_GROUPUSED:
5523eda14cbcSMatt Macy 		str = gettext("Allows reading any groupused@... property");
5524eda14cbcSMatt Macy 		break;
5525eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_USERPROP:
5526eda14cbcSMatt Macy 		str = gettext("Allows changing any user property");
5527eda14cbcSMatt Macy 		break;
5528eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_USERQUOTA:
5529eda14cbcSMatt Macy 		str = gettext("Allows accessing any userquota@... property");
5530eda14cbcSMatt Macy 		break;
5531eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_USERUSED:
5532eda14cbcSMatt Macy 		str = gettext("Allows reading any userused@... property");
5533eda14cbcSMatt Macy 		break;
5534eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_USEROBJQUOTA:
5535eda14cbcSMatt Macy 		str = gettext("Allows accessing any userobjquota@... property");
5536eda14cbcSMatt Macy 		break;
5537eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
5538eda14cbcSMatt Macy 		str = gettext("Allows accessing any \n\t\t\t\t"
5539eda14cbcSMatt Macy 		    "groupobjquota@... property");
5540eda14cbcSMatt Macy 		break;
5541eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_GROUPOBJUSED:
5542eda14cbcSMatt Macy 		str = gettext("Allows reading any groupobjused@... property");
5543eda14cbcSMatt Macy 		break;
5544eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_USEROBJUSED:
5545eda14cbcSMatt Macy 		str = gettext("Allows reading any userobjused@... property");
5546eda14cbcSMatt Macy 		break;
5547eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_PROJECTQUOTA:
5548eda14cbcSMatt Macy 		str = gettext("Allows accessing any projectquota@... property");
5549eda14cbcSMatt Macy 		break;
5550eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_PROJECTOBJQUOTA:
5551eda14cbcSMatt Macy 		str = gettext("Allows accessing any \n\t\t\t\t"
5552eda14cbcSMatt Macy 		    "projectobjquota@... property");
5553eda14cbcSMatt Macy 		break;
5554eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_PROJECTUSED:
5555eda14cbcSMatt Macy 		str = gettext("Allows reading any projectused@... property");
5556eda14cbcSMatt Macy 		break;
5557eda14cbcSMatt Macy 	case ZFS_DELEG_NOTE_PROJECTOBJUSED:
5558eda14cbcSMatt Macy 		str = gettext("Allows accessing any \n\t\t\t\t"
5559eda14cbcSMatt Macy 		    "projectobjused@... property");
5560eda14cbcSMatt Macy 		break;
5561eda14cbcSMatt Macy 		/* other */
5562eda14cbcSMatt Macy 	default:
5563eda14cbcSMatt Macy 		str = "";
5564eda14cbcSMatt Macy 	}
5565eda14cbcSMatt Macy 
5566eda14cbcSMatt Macy 	return (str);
5567eda14cbcSMatt Macy }
5568eda14cbcSMatt Macy 
5569eda14cbcSMatt Macy struct allow_opts {
5570eda14cbcSMatt Macy 	boolean_t local;
5571eda14cbcSMatt Macy 	boolean_t descend;
5572eda14cbcSMatt Macy 	boolean_t user;
5573eda14cbcSMatt Macy 	boolean_t group;
5574eda14cbcSMatt Macy 	boolean_t everyone;
5575eda14cbcSMatt Macy 	boolean_t create;
5576eda14cbcSMatt Macy 	boolean_t set;
5577eda14cbcSMatt Macy 	boolean_t recursive; /* unallow only */
5578eda14cbcSMatt Macy 	boolean_t prt_usage;
5579eda14cbcSMatt Macy 
5580eda14cbcSMatt Macy 	boolean_t prt_perms;
5581eda14cbcSMatt Macy 	char *who;
5582eda14cbcSMatt Macy 	char *perms;
5583eda14cbcSMatt Macy 	const char *dataset;
5584eda14cbcSMatt Macy };
5585eda14cbcSMatt Macy 
5586eda14cbcSMatt Macy static inline int
5587eda14cbcSMatt Macy prop_cmp(const void *a, const void *b)
5588eda14cbcSMatt Macy {
5589eda14cbcSMatt Macy 	const char *str1 = *(const char **)a;
5590eda14cbcSMatt Macy 	const char *str2 = *(const char **)b;
5591eda14cbcSMatt Macy 	return (strcmp(str1, str2));
5592eda14cbcSMatt Macy }
5593eda14cbcSMatt Macy 
5594eda14cbcSMatt Macy static void
5595eda14cbcSMatt Macy allow_usage(boolean_t un, boolean_t requested, const char *msg)
5596eda14cbcSMatt Macy {
5597eda14cbcSMatt Macy 	const char *opt_desc[] = {
5598eda14cbcSMatt Macy 		"-h", gettext("show this help message and exit"),
5599eda14cbcSMatt Macy 		"-l", gettext("set permission locally"),
5600eda14cbcSMatt Macy 		"-d", gettext("set permission for descents"),
5601eda14cbcSMatt Macy 		"-u", gettext("set permission for user"),
5602eda14cbcSMatt Macy 		"-g", gettext("set permission for group"),
5603eda14cbcSMatt Macy 		"-e", gettext("set permission for everyone"),
5604eda14cbcSMatt Macy 		"-c", gettext("set create time permission"),
5605eda14cbcSMatt Macy 		"-s", gettext("define permission set"),
5606eda14cbcSMatt Macy 		/* unallow only */
5607eda14cbcSMatt Macy 		"-r", gettext("remove permissions recursively"),
5608eda14cbcSMatt Macy 	};
5609eda14cbcSMatt Macy 	size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
5610eda14cbcSMatt Macy 	size_t allow_size = unallow_size - 2;
5611eda14cbcSMatt Macy 	const char *props[ZFS_NUM_PROPS];
5612eda14cbcSMatt Macy 	int i;
5613eda14cbcSMatt Macy 	size_t count = 0;
5614eda14cbcSMatt Macy 	FILE *fp = requested ? stdout : stderr;
5615eda14cbcSMatt Macy 	zprop_desc_t *pdtbl = zfs_prop_get_table();
5616eda14cbcSMatt Macy 	const char *fmt = gettext("%-16s %-14s\t%s\n");
5617eda14cbcSMatt Macy 
5618eda14cbcSMatt Macy 	(void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
5619eda14cbcSMatt Macy 	    HELP_ALLOW));
5620eda14cbcSMatt Macy 	(void) fprintf(fp, gettext("Options:\n"));
5621eda14cbcSMatt Macy 	for (i = 0; i < (un ? unallow_size : allow_size); i += 2) {
5622eda14cbcSMatt Macy 		const char *opt = opt_desc[i];
5623eda14cbcSMatt Macy 		const char *optdsc = opt_desc[i + 1];
5624eda14cbcSMatt Macy 		(void) fprintf(fp, gettext("  %-10s  %s\n"), opt, optdsc);
5625eda14cbcSMatt Macy 	}
5626eda14cbcSMatt Macy 
5627eda14cbcSMatt Macy 	(void) fprintf(fp, gettext("\nThe following permissions are "
5628eda14cbcSMatt Macy 	    "supported:\n\n"));
5629eda14cbcSMatt Macy 	(void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
5630eda14cbcSMatt Macy 	    gettext("NOTES"));
5631eda14cbcSMatt Macy 	for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
5632eda14cbcSMatt Macy 		const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
5633eda14cbcSMatt Macy 		zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
5634eda14cbcSMatt Macy 		const char *perm_type = deleg_perm_type(perm_note);
5635eda14cbcSMatt Macy 		const char *perm_comment = deleg_perm_comment(perm_note);
5636eda14cbcSMatt Macy 		(void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
5637eda14cbcSMatt Macy 	}
5638eda14cbcSMatt Macy 
5639eda14cbcSMatt Macy 	for (i = 0; i < ZFS_NUM_PROPS; i++) {
5640eda14cbcSMatt Macy 		zprop_desc_t *pd = &pdtbl[i];
5641eda14cbcSMatt Macy 		if (pd->pd_visible != B_TRUE)
5642eda14cbcSMatt Macy 			continue;
5643eda14cbcSMatt Macy 
5644eda14cbcSMatt Macy 		if (pd->pd_attr == PROP_READONLY)
5645eda14cbcSMatt Macy 			continue;
5646eda14cbcSMatt Macy 
5647eda14cbcSMatt Macy 		props[count++] = pd->pd_name;
5648eda14cbcSMatt Macy 	}
5649eda14cbcSMatt Macy 	props[count] = NULL;
5650eda14cbcSMatt Macy 
5651eda14cbcSMatt Macy 	qsort(props, count, sizeof (char *), prop_cmp);
5652eda14cbcSMatt Macy 
5653eda14cbcSMatt Macy 	for (i = 0; i < count; i++)
5654eda14cbcSMatt Macy 		(void) fprintf(fp, fmt, props[i], gettext("property"), "");
5655eda14cbcSMatt Macy 
5656eda14cbcSMatt Macy 	if (msg != NULL)
5657eda14cbcSMatt Macy 		(void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
5658eda14cbcSMatt Macy 
5659eda14cbcSMatt Macy 	exit(requested ? 0 : 2);
5660eda14cbcSMatt Macy }
5661eda14cbcSMatt Macy 
5662eda14cbcSMatt Macy static inline const char *
5663eda14cbcSMatt Macy munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
5664eda14cbcSMatt Macy     char **permsp)
5665eda14cbcSMatt Macy {
5666eda14cbcSMatt Macy 	if (un && argc == expected_argc - 1)
5667eda14cbcSMatt Macy 		*permsp = NULL;
5668eda14cbcSMatt Macy 	else if (argc == expected_argc)
5669eda14cbcSMatt Macy 		*permsp = argv[argc - 2];
5670eda14cbcSMatt Macy 	else
5671eda14cbcSMatt Macy 		allow_usage(un, B_FALSE,
5672eda14cbcSMatt Macy 		    gettext("wrong number of parameters\n"));
5673eda14cbcSMatt Macy 
5674eda14cbcSMatt Macy 	return (argv[argc - 1]);
5675eda14cbcSMatt Macy }
5676eda14cbcSMatt Macy 
5677eda14cbcSMatt Macy static void
5678eda14cbcSMatt Macy parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
5679eda14cbcSMatt Macy {
5680eda14cbcSMatt Macy 	int uge_sum = opts->user + opts->group + opts->everyone;
5681eda14cbcSMatt Macy 	int csuge_sum = opts->create + opts->set + uge_sum;
5682eda14cbcSMatt Macy 	int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
5683eda14cbcSMatt Macy 	int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
5684eda14cbcSMatt Macy 
5685eda14cbcSMatt Macy 	if (uge_sum > 1)
5686eda14cbcSMatt Macy 		allow_usage(un, B_FALSE,
5687eda14cbcSMatt Macy 		    gettext("-u, -g, and -e are mutually exclusive\n"));
5688eda14cbcSMatt Macy 
5689eda14cbcSMatt Macy 	if (opts->prt_usage) {
5690eda14cbcSMatt Macy 		if (argc == 0 && all_sum == 0)
5691eda14cbcSMatt Macy 			allow_usage(un, B_TRUE, NULL);
5692eda14cbcSMatt Macy 		else
5693eda14cbcSMatt Macy 			usage(B_FALSE);
5694eda14cbcSMatt Macy 	}
5695eda14cbcSMatt Macy 
5696eda14cbcSMatt Macy 	if (opts->set) {
5697eda14cbcSMatt Macy 		if (csuge_sum > 1)
5698eda14cbcSMatt Macy 			allow_usage(un, B_FALSE,
5699eda14cbcSMatt Macy 			    gettext("invalid options combined with -s\n"));
5700eda14cbcSMatt Macy 
5701eda14cbcSMatt Macy 		opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
5702eda14cbcSMatt Macy 		if (argv[0][0] != '@')
5703eda14cbcSMatt Macy 			allow_usage(un, B_FALSE,
5704eda14cbcSMatt Macy 			    gettext("invalid set name: missing '@' prefix\n"));
5705eda14cbcSMatt Macy 		opts->who = argv[0];
5706eda14cbcSMatt Macy 	} else if (opts->create) {
5707eda14cbcSMatt Macy 		if (ldcsuge_sum > 1)
5708eda14cbcSMatt Macy 			allow_usage(un, B_FALSE,
5709eda14cbcSMatt Macy 			    gettext("invalid options combined with -c\n"));
5710eda14cbcSMatt Macy 		opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5711eda14cbcSMatt Macy 	} else if (opts->everyone) {
5712eda14cbcSMatt Macy 		if (csuge_sum > 1)
5713eda14cbcSMatt Macy 			allow_usage(un, B_FALSE,
5714eda14cbcSMatt Macy 			    gettext("invalid options combined with -e\n"));
5715eda14cbcSMatt Macy 		opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5716eda14cbcSMatt Macy 	} else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
5717eda14cbcSMatt Macy 	    == 0) {
5718eda14cbcSMatt Macy 		opts->everyone = B_TRUE;
5719eda14cbcSMatt Macy 		argc--;
5720eda14cbcSMatt Macy 		argv++;
5721eda14cbcSMatt Macy 		opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5722eda14cbcSMatt Macy 	} else if (argc == 1 && !un) {
5723eda14cbcSMatt Macy 		opts->prt_perms = B_TRUE;
5724eda14cbcSMatt Macy 		opts->dataset = argv[argc-1];
5725eda14cbcSMatt Macy 	} else {
5726eda14cbcSMatt Macy 		opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
5727eda14cbcSMatt Macy 		opts->who = argv[0];
5728eda14cbcSMatt Macy 	}
5729eda14cbcSMatt Macy 
5730eda14cbcSMatt Macy 	if (!opts->local && !opts->descend) {
5731eda14cbcSMatt Macy 		opts->local = B_TRUE;
5732eda14cbcSMatt Macy 		opts->descend = B_TRUE;
5733eda14cbcSMatt Macy 	}
5734eda14cbcSMatt Macy }
5735eda14cbcSMatt Macy 
5736eda14cbcSMatt Macy static void
5737eda14cbcSMatt Macy store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
5738eda14cbcSMatt Macy     const char *who, char *perms, nvlist_t *top_nvl)
5739eda14cbcSMatt Macy {
5740eda14cbcSMatt Macy 	int i;
5741eda14cbcSMatt Macy 	char ld[2] = { '\0', '\0' };
5742eda14cbcSMatt Macy 	char who_buf[MAXNAMELEN + 32];
5743eda14cbcSMatt Macy 	char base_type = '\0';
5744eda14cbcSMatt Macy 	char set_type = '\0';
5745eda14cbcSMatt Macy 	nvlist_t *base_nvl = NULL;
5746eda14cbcSMatt Macy 	nvlist_t *set_nvl = NULL;
5747eda14cbcSMatt Macy 	nvlist_t *nvl;
5748eda14cbcSMatt Macy 
5749eda14cbcSMatt Macy 	if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
5750eda14cbcSMatt Macy 		nomem();
5751eda14cbcSMatt Macy 	if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) !=  0)
5752eda14cbcSMatt Macy 		nomem();
5753eda14cbcSMatt Macy 
5754eda14cbcSMatt Macy 	switch (type) {
5755eda14cbcSMatt Macy 	case ZFS_DELEG_NAMED_SET_SETS:
5756eda14cbcSMatt Macy 	case ZFS_DELEG_NAMED_SET:
5757eda14cbcSMatt Macy 		set_type = ZFS_DELEG_NAMED_SET_SETS;
5758eda14cbcSMatt Macy 		base_type = ZFS_DELEG_NAMED_SET;
5759eda14cbcSMatt Macy 		ld[0] = ZFS_DELEG_NA;
5760eda14cbcSMatt Macy 		break;
5761eda14cbcSMatt Macy 	case ZFS_DELEG_CREATE_SETS:
5762eda14cbcSMatt Macy 	case ZFS_DELEG_CREATE:
5763eda14cbcSMatt Macy 		set_type = ZFS_DELEG_CREATE_SETS;
5764eda14cbcSMatt Macy 		base_type = ZFS_DELEG_CREATE;
5765eda14cbcSMatt Macy 		ld[0] = ZFS_DELEG_NA;
5766eda14cbcSMatt Macy 		break;
5767eda14cbcSMatt Macy 	case ZFS_DELEG_USER_SETS:
5768eda14cbcSMatt Macy 	case ZFS_DELEG_USER:
5769eda14cbcSMatt Macy 		set_type = ZFS_DELEG_USER_SETS;
5770eda14cbcSMatt Macy 		base_type = ZFS_DELEG_USER;
5771eda14cbcSMatt Macy 		if (local)
5772eda14cbcSMatt Macy 			ld[0] = ZFS_DELEG_LOCAL;
5773eda14cbcSMatt Macy 		if (descend)
5774eda14cbcSMatt Macy 			ld[1] = ZFS_DELEG_DESCENDENT;
5775eda14cbcSMatt Macy 		break;
5776eda14cbcSMatt Macy 	case ZFS_DELEG_GROUP_SETS:
5777eda14cbcSMatt Macy 	case ZFS_DELEG_GROUP:
5778eda14cbcSMatt Macy 		set_type = ZFS_DELEG_GROUP_SETS;
5779eda14cbcSMatt Macy 		base_type = ZFS_DELEG_GROUP;
5780eda14cbcSMatt Macy 		if (local)
5781eda14cbcSMatt Macy 			ld[0] = ZFS_DELEG_LOCAL;
5782eda14cbcSMatt Macy 		if (descend)
5783eda14cbcSMatt Macy 			ld[1] = ZFS_DELEG_DESCENDENT;
5784eda14cbcSMatt Macy 		break;
5785eda14cbcSMatt Macy 	case ZFS_DELEG_EVERYONE_SETS:
5786eda14cbcSMatt Macy 	case ZFS_DELEG_EVERYONE:
5787eda14cbcSMatt Macy 		set_type = ZFS_DELEG_EVERYONE_SETS;
5788eda14cbcSMatt Macy 		base_type = ZFS_DELEG_EVERYONE;
5789eda14cbcSMatt Macy 		if (local)
5790eda14cbcSMatt Macy 			ld[0] = ZFS_DELEG_LOCAL;
5791eda14cbcSMatt Macy 		if (descend)
5792eda14cbcSMatt Macy 			ld[1] = ZFS_DELEG_DESCENDENT;
5793eda14cbcSMatt Macy 		break;
5794eda14cbcSMatt Macy 
5795eda14cbcSMatt Macy 	default:
5796eda14cbcSMatt Macy 		assert(set_type != '\0' && base_type != '\0');
5797eda14cbcSMatt Macy 	}
5798eda14cbcSMatt Macy 
5799eda14cbcSMatt Macy 	if (perms != NULL) {
5800eda14cbcSMatt Macy 		char *curr = perms;
5801eda14cbcSMatt Macy 		char *end = curr + strlen(perms);
5802eda14cbcSMatt Macy 
5803eda14cbcSMatt Macy 		while (curr < end) {
5804eda14cbcSMatt Macy 			char *delim = strchr(curr, ',');
5805eda14cbcSMatt Macy 			if (delim == NULL)
5806eda14cbcSMatt Macy 				delim = end;
5807eda14cbcSMatt Macy 			else
5808eda14cbcSMatt Macy 				*delim = '\0';
5809eda14cbcSMatt Macy 
5810eda14cbcSMatt Macy 			if (curr[0] == '@')
5811eda14cbcSMatt Macy 				nvl = set_nvl;
5812eda14cbcSMatt Macy 			else
5813eda14cbcSMatt Macy 				nvl = base_nvl;
5814eda14cbcSMatt Macy 
5815eda14cbcSMatt Macy 			(void) nvlist_add_boolean(nvl, curr);
5816eda14cbcSMatt Macy 			if (delim != end)
5817eda14cbcSMatt Macy 				*delim = ',';
5818eda14cbcSMatt Macy 			curr = delim + 1;
5819eda14cbcSMatt Macy 		}
5820eda14cbcSMatt Macy 
5821eda14cbcSMatt Macy 		for (i = 0; i < 2; i++) {
5822eda14cbcSMatt Macy 			char locality = ld[i];
5823eda14cbcSMatt Macy 			if (locality == 0)
5824eda14cbcSMatt Macy 				continue;
5825eda14cbcSMatt Macy 
5826eda14cbcSMatt Macy 			if (!nvlist_empty(base_nvl)) {
5827eda14cbcSMatt Macy 				if (who != NULL)
5828eda14cbcSMatt Macy 					(void) snprintf(who_buf,
5829eda14cbcSMatt Macy 					    sizeof (who_buf), "%c%c$%s",
5830eda14cbcSMatt Macy 					    base_type, locality, who);
5831eda14cbcSMatt Macy 				else
5832eda14cbcSMatt Macy 					(void) snprintf(who_buf,
5833eda14cbcSMatt Macy 					    sizeof (who_buf), "%c%c$",
5834eda14cbcSMatt Macy 					    base_type, locality);
5835eda14cbcSMatt Macy 
5836eda14cbcSMatt Macy 				(void) nvlist_add_nvlist(top_nvl, who_buf,
5837eda14cbcSMatt Macy 				    base_nvl);
5838eda14cbcSMatt Macy 			}
5839eda14cbcSMatt Macy 
5840eda14cbcSMatt Macy 
5841eda14cbcSMatt Macy 			if (!nvlist_empty(set_nvl)) {
5842eda14cbcSMatt Macy 				if (who != NULL)
5843eda14cbcSMatt Macy 					(void) snprintf(who_buf,
5844eda14cbcSMatt Macy 					    sizeof (who_buf), "%c%c$%s",
5845eda14cbcSMatt Macy 					    set_type, locality, who);
5846eda14cbcSMatt Macy 				else
5847eda14cbcSMatt Macy 					(void) snprintf(who_buf,
5848eda14cbcSMatt Macy 					    sizeof (who_buf), "%c%c$",
5849eda14cbcSMatt Macy 					    set_type, locality);
5850eda14cbcSMatt Macy 
5851eda14cbcSMatt Macy 				(void) nvlist_add_nvlist(top_nvl, who_buf,
5852eda14cbcSMatt Macy 				    set_nvl);
5853eda14cbcSMatt Macy 			}
5854eda14cbcSMatt Macy 		}
5855eda14cbcSMatt Macy 	} else {
5856eda14cbcSMatt Macy 		for (i = 0; i < 2; i++) {
5857eda14cbcSMatt Macy 			char locality = ld[i];
5858eda14cbcSMatt Macy 			if (locality == 0)
5859eda14cbcSMatt Macy 				continue;
5860eda14cbcSMatt Macy 
5861eda14cbcSMatt Macy 			if (who != NULL)
5862eda14cbcSMatt Macy 				(void) snprintf(who_buf, sizeof (who_buf),
5863eda14cbcSMatt Macy 				    "%c%c$%s", base_type, locality, who);
5864eda14cbcSMatt Macy 			else
5865eda14cbcSMatt Macy 				(void) snprintf(who_buf, sizeof (who_buf),
5866eda14cbcSMatt Macy 				    "%c%c$", base_type, locality);
5867eda14cbcSMatt Macy 			(void) nvlist_add_boolean(top_nvl, who_buf);
5868eda14cbcSMatt Macy 
5869eda14cbcSMatt Macy 			if (who != NULL)
5870eda14cbcSMatt Macy 				(void) snprintf(who_buf, sizeof (who_buf),
5871eda14cbcSMatt Macy 				    "%c%c$%s", set_type, locality, who);
5872eda14cbcSMatt Macy 			else
5873eda14cbcSMatt Macy 				(void) snprintf(who_buf, sizeof (who_buf),
5874eda14cbcSMatt Macy 				    "%c%c$", set_type, locality);
5875eda14cbcSMatt Macy 			(void) nvlist_add_boolean(top_nvl, who_buf);
5876eda14cbcSMatt Macy 		}
5877eda14cbcSMatt Macy 	}
5878eda14cbcSMatt Macy }
5879eda14cbcSMatt Macy 
5880eda14cbcSMatt Macy static int
5881eda14cbcSMatt Macy construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5882eda14cbcSMatt Macy {
5883eda14cbcSMatt Macy 	if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5884eda14cbcSMatt Macy 		nomem();
5885eda14cbcSMatt Macy 
5886eda14cbcSMatt Macy 	if (opts->set) {
5887eda14cbcSMatt Macy 		store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5888eda14cbcSMatt Macy 		    opts->descend, opts->who, opts->perms, *nvlp);
5889eda14cbcSMatt Macy 	} else if (opts->create) {
5890eda14cbcSMatt Macy 		store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5891eda14cbcSMatt Macy 		    opts->descend, NULL, opts->perms, *nvlp);
5892eda14cbcSMatt Macy 	} else if (opts->everyone) {
5893eda14cbcSMatt Macy 		store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5894eda14cbcSMatt Macy 		    opts->descend, NULL, opts->perms, *nvlp);
5895eda14cbcSMatt Macy 	} else {
5896eda14cbcSMatt Macy 		char *curr = opts->who;
5897eda14cbcSMatt Macy 		char *end = curr + strlen(curr);
5898eda14cbcSMatt Macy 
5899eda14cbcSMatt Macy 		while (curr < end) {
5900eda14cbcSMatt Macy 			const char *who;
5901eda14cbcSMatt Macy 			zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
5902eda14cbcSMatt Macy 			char *endch;
5903eda14cbcSMatt Macy 			char *delim = strchr(curr, ',');
5904eda14cbcSMatt Macy 			char errbuf[256];
5905eda14cbcSMatt Macy 			char id[64];
5906eda14cbcSMatt Macy 			struct passwd *p = NULL;
5907eda14cbcSMatt Macy 			struct group *g = NULL;
5908eda14cbcSMatt Macy 
5909eda14cbcSMatt Macy 			uid_t rid;
5910eda14cbcSMatt Macy 			if (delim == NULL)
5911eda14cbcSMatt Macy 				delim = end;
5912eda14cbcSMatt Macy 			else
5913eda14cbcSMatt Macy 				*delim = '\0';
5914eda14cbcSMatt Macy 
5915eda14cbcSMatt Macy 			rid = (uid_t)strtol(curr, &endch, 0);
5916eda14cbcSMatt Macy 			if (opts->user) {
5917eda14cbcSMatt Macy 				who_type = ZFS_DELEG_USER;
5918eda14cbcSMatt Macy 				if (*endch != '\0')
5919eda14cbcSMatt Macy 					p = getpwnam(curr);
5920eda14cbcSMatt Macy 				else
5921eda14cbcSMatt Macy 					p = getpwuid(rid);
5922eda14cbcSMatt Macy 
5923eda14cbcSMatt Macy 				if (p != NULL)
5924eda14cbcSMatt Macy 					rid = p->pw_uid;
5925eda14cbcSMatt Macy 				else if (*endch != '\0') {
5926eda14cbcSMatt Macy 					(void) snprintf(errbuf, 256, gettext(
5927eda14cbcSMatt Macy 					    "invalid user %s\n"), curr);
5928eda14cbcSMatt Macy 					allow_usage(un, B_TRUE, errbuf);
5929eda14cbcSMatt Macy 				}
5930eda14cbcSMatt Macy 			} else if (opts->group) {
5931eda14cbcSMatt Macy 				who_type = ZFS_DELEG_GROUP;
5932eda14cbcSMatt Macy 				if (*endch != '\0')
5933eda14cbcSMatt Macy 					g = getgrnam(curr);
5934eda14cbcSMatt Macy 				else
5935eda14cbcSMatt Macy 					g = getgrgid(rid);
5936eda14cbcSMatt Macy 
5937eda14cbcSMatt Macy 				if (g != NULL)
5938eda14cbcSMatt Macy 					rid = g->gr_gid;
5939eda14cbcSMatt Macy 				else if (*endch != '\0') {
5940eda14cbcSMatt Macy 					(void) snprintf(errbuf, 256, gettext(
5941eda14cbcSMatt Macy 					    "invalid group %s\n"),  curr);
5942eda14cbcSMatt Macy 					allow_usage(un, B_TRUE, errbuf);
5943eda14cbcSMatt Macy 				}
5944eda14cbcSMatt Macy 			} else {
5945eda14cbcSMatt Macy 				if (*endch != '\0') {
5946eda14cbcSMatt Macy 					p = getpwnam(curr);
5947eda14cbcSMatt Macy 				} else {
5948eda14cbcSMatt Macy 					p = getpwuid(rid);
5949eda14cbcSMatt Macy 				}
5950eda14cbcSMatt Macy 
5951eda14cbcSMatt Macy 				if (p == NULL) {
5952eda14cbcSMatt Macy 					if (*endch != '\0') {
5953eda14cbcSMatt Macy 						g = getgrnam(curr);
5954eda14cbcSMatt Macy 					} else {
5955eda14cbcSMatt Macy 						g = getgrgid(rid);
5956eda14cbcSMatt Macy 					}
5957eda14cbcSMatt Macy 				}
5958eda14cbcSMatt Macy 
5959eda14cbcSMatt Macy 				if (p != NULL) {
5960eda14cbcSMatt Macy 					who_type = ZFS_DELEG_USER;
5961eda14cbcSMatt Macy 					rid = p->pw_uid;
5962eda14cbcSMatt Macy 				} else if (g != NULL) {
5963eda14cbcSMatt Macy 					who_type = ZFS_DELEG_GROUP;
5964eda14cbcSMatt Macy 					rid = g->gr_gid;
5965eda14cbcSMatt Macy 				} else {
5966eda14cbcSMatt Macy 					(void) snprintf(errbuf, 256, gettext(
5967eda14cbcSMatt Macy 					    "invalid user/group %s\n"), curr);
5968eda14cbcSMatt Macy 					allow_usage(un, B_TRUE, errbuf);
5969eda14cbcSMatt Macy 				}
5970eda14cbcSMatt Macy 			}
5971eda14cbcSMatt Macy 
5972eda14cbcSMatt Macy 			(void) sprintf(id, "%u", rid);
5973eda14cbcSMatt Macy 			who = id;
5974eda14cbcSMatt Macy 
5975eda14cbcSMatt Macy 			store_allow_perm(who_type, opts->local,
5976eda14cbcSMatt Macy 			    opts->descend, who, opts->perms, *nvlp);
5977eda14cbcSMatt Macy 			curr = delim + 1;
5978eda14cbcSMatt Macy 		}
5979eda14cbcSMatt Macy 	}
5980eda14cbcSMatt Macy 
5981eda14cbcSMatt Macy 	return (0);
5982eda14cbcSMatt Macy }
5983eda14cbcSMatt Macy 
5984eda14cbcSMatt Macy static void
5985eda14cbcSMatt Macy print_set_creat_perms(uu_avl_t *who_avl)
5986eda14cbcSMatt Macy {
5987eda14cbcSMatt Macy 	const char *sc_title[] = {
5988eda14cbcSMatt Macy 		gettext("Permission sets:\n"),
5989eda14cbcSMatt Macy 		gettext("Create time permissions:\n"),
5990eda14cbcSMatt Macy 		NULL
5991eda14cbcSMatt Macy 	};
5992eda14cbcSMatt Macy 	who_perm_node_t *who_node = NULL;
5993eda14cbcSMatt Macy 	int prev_weight = -1;
5994eda14cbcSMatt Macy 
5995eda14cbcSMatt Macy 	for (who_node = uu_avl_first(who_avl); who_node != NULL;
5996eda14cbcSMatt Macy 	    who_node = uu_avl_next(who_avl, who_node)) {
5997eda14cbcSMatt Macy 		uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5998eda14cbcSMatt Macy 		zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5999eda14cbcSMatt Macy 		const char *who_name = who_node->who_perm.who_name;
6000eda14cbcSMatt Macy 		int weight = who_type2weight(who_type);
6001eda14cbcSMatt Macy 		boolean_t first = B_TRUE;
6002eda14cbcSMatt Macy 		deleg_perm_node_t *deleg_node;
6003eda14cbcSMatt Macy 
6004eda14cbcSMatt Macy 		if (prev_weight != weight) {
6005eda14cbcSMatt Macy 			(void) printf("%s", sc_title[weight]);
6006eda14cbcSMatt Macy 			prev_weight = weight;
6007eda14cbcSMatt Macy 		}
6008eda14cbcSMatt Macy 
6009eda14cbcSMatt Macy 		if (who_name == NULL || strnlen(who_name, 1) == 0)
6010eda14cbcSMatt Macy 			(void) printf("\t");
6011eda14cbcSMatt Macy 		else
6012eda14cbcSMatt Macy 			(void) printf("\t%s ", who_name);
6013eda14cbcSMatt Macy 
6014eda14cbcSMatt Macy 		for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
6015eda14cbcSMatt Macy 		    deleg_node = uu_avl_next(avl, deleg_node)) {
6016eda14cbcSMatt Macy 			if (first) {
6017eda14cbcSMatt Macy 				(void) printf("%s",
6018eda14cbcSMatt Macy 				    deleg_node->dpn_perm.dp_name);
6019eda14cbcSMatt Macy 				first = B_FALSE;
6020eda14cbcSMatt Macy 			} else
6021eda14cbcSMatt Macy 				(void) printf(",%s",
6022eda14cbcSMatt Macy 				    deleg_node->dpn_perm.dp_name);
6023eda14cbcSMatt Macy 		}
6024eda14cbcSMatt Macy 
6025eda14cbcSMatt Macy 		(void) printf("\n");
6026eda14cbcSMatt Macy 	}
6027eda14cbcSMatt Macy }
6028eda14cbcSMatt Macy 
6029eda14cbcSMatt Macy static void
6030eda14cbcSMatt Macy print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
6031eda14cbcSMatt Macy     const char *title)
6032eda14cbcSMatt Macy {
6033eda14cbcSMatt Macy 	who_perm_node_t *who_node = NULL;
6034eda14cbcSMatt Macy 	boolean_t prt_title = B_TRUE;
6035eda14cbcSMatt Macy 	uu_avl_walk_t *walk;
6036eda14cbcSMatt Macy 
6037eda14cbcSMatt Macy 	if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
6038eda14cbcSMatt Macy 		nomem();
6039eda14cbcSMatt Macy 
6040eda14cbcSMatt Macy 	while ((who_node = uu_avl_walk_next(walk)) != NULL) {
6041eda14cbcSMatt Macy 		const char *who_name = who_node->who_perm.who_name;
6042eda14cbcSMatt Macy 		const char *nice_who_name = who_node->who_perm.who_ug_name;
6043eda14cbcSMatt Macy 		uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
6044eda14cbcSMatt Macy 		zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
6045eda14cbcSMatt Macy 		char delim = ' ';
6046eda14cbcSMatt Macy 		deleg_perm_node_t *deleg_node;
6047eda14cbcSMatt Macy 		boolean_t prt_who = B_TRUE;
6048eda14cbcSMatt Macy 
6049eda14cbcSMatt Macy 		for (deleg_node = uu_avl_first(avl);
6050eda14cbcSMatt Macy 		    deleg_node != NULL;
6051eda14cbcSMatt Macy 		    deleg_node = uu_avl_next(avl, deleg_node)) {
6052eda14cbcSMatt Macy 			if (local != deleg_node->dpn_perm.dp_local ||
6053eda14cbcSMatt Macy 			    descend != deleg_node->dpn_perm.dp_descend)
6054eda14cbcSMatt Macy 				continue;
6055eda14cbcSMatt Macy 
6056eda14cbcSMatt Macy 			if (prt_who) {
6057eda14cbcSMatt Macy 				const char *who = NULL;
6058eda14cbcSMatt Macy 				if (prt_title) {
6059eda14cbcSMatt Macy 					prt_title = B_FALSE;
6060eda14cbcSMatt Macy 					(void) printf("%s", title);
6061eda14cbcSMatt Macy 				}
6062eda14cbcSMatt Macy 
6063eda14cbcSMatt Macy 				switch (who_type) {
6064eda14cbcSMatt Macy 				case ZFS_DELEG_USER_SETS:
6065eda14cbcSMatt Macy 				case ZFS_DELEG_USER:
6066eda14cbcSMatt Macy 					who = gettext("user");
6067eda14cbcSMatt Macy 					if (nice_who_name)
6068eda14cbcSMatt Macy 						who_name  = nice_who_name;
6069eda14cbcSMatt Macy 					break;
6070eda14cbcSMatt Macy 				case ZFS_DELEG_GROUP_SETS:
6071eda14cbcSMatt Macy 				case ZFS_DELEG_GROUP:
6072eda14cbcSMatt Macy 					who = gettext("group");
6073eda14cbcSMatt Macy 					if (nice_who_name)
6074eda14cbcSMatt Macy 						who_name  = nice_who_name;
6075eda14cbcSMatt Macy 					break;
6076eda14cbcSMatt Macy 				case ZFS_DELEG_EVERYONE_SETS:
6077eda14cbcSMatt Macy 				case ZFS_DELEG_EVERYONE:
6078eda14cbcSMatt Macy 					who = gettext("everyone");
6079eda14cbcSMatt Macy 					who_name = NULL;
6080eda14cbcSMatt Macy 					break;
6081eda14cbcSMatt Macy 
6082eda14cbcSMatt Macy 				default:
6083eda14cbcSMatt Macy 					assert(who != NULL);
6084eda14cbcSMatt Macy 				}
6085eda14cbcSMatt Macy 
6086eda14cbcSMatt Macy 				prt_who = B_FALSE;
6087eda14cbcSMatt Macy 				if (who_name == NULL)
6088eda14cbcSMatt Macy 					(void) printf("\t%s", who);
6089eda14cbcSMatt Macy 				else
6090eda14cbcSMatt Macy 					(void) printf("\t%s %s", who, who_name);
6091eda14cbcSMatt Macy 			}
6092eda14cbcSMatt Macy 
6093eda14cbcSMatt Macy 			(void) printf("%c%s", delim,
6094eda14cbcSMatt Macy 			    deleg_node->dpn_perm.dp_name);
6095eda14cbcSMatt Macy 			delim = ',';
6096eda14cbcSMatt Macy 		}
6097eda14cbcSMatt Macy 
6098eda14cbcSMatt Macy 		if (!prt_who)
6099eda14cbcSMatt Macy 			(void) printf("\n");
6100eda14cbcSMatt Macy 	}
6101eda14cbcSMatt Macy 
6102eda14cbcSMatt Macy 	uu_avl_walk_end(walk);
6103eda14cbcSMatt Macy }
6104eda14cbcSMatt Macy 
6105eda14cbcSMatt Macy static void
6106eda14cbcSMatt Macy print_fs_perms(fs_perm_set_t *fspset)
6107eda14cbcSMatt Macy {
6108eda14cbcSMatt Macy 	fs_perm_node_t *node = NULL;
6109eda14cbcSMatt Macy 	char buf[MAXNAMELEN + 32];
6110eda14cbcSMatt Macy 	const char *dsname = buf;
6111eda14cbcSMatt Macy 
6112eda14cbcSMatt Macy 	for (node = uu_list_first(fspset->fsps_list); node != NULL;
6113eda14cbcSMatt Macy 	    node = uu_list_next(fspset->fsps_list, node)) {
6114eda14cbcSMatt Macy 		uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
6115eda14cbcSMatt Macy 		uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
6116eda14cbcSMatt Macy 		int left = 0;
6117eda14cbcSMatt Macy 
6118eda14cbcSMatt Macy 		(void) snprintf(buf, sizeof (buf),
6119eda14cbcSMatt Macy 		    gettext("---- Permissions on %s "),
6120eda14cbcSMatt Macy 		    node->fspn_fsperm.fsp_name);
6121eda14cbcSMatt Macy 		(void) printf("%s", dsname);
6122eda14cbcSMatt Macy 		left = 70 - strlen(buf);
6123eda14cbcSMatt Macy 		while (left-- > 0)
6124eda14cbcSMatt Macy 			(void) printf("-");
6125eda14cbcSMatt Macy 		(void) printf("\n");
6126eda14cbcSMatt Macy 
6127eda14cbcSMatt Macy 		print_set_creat_perms(sc_avl);
6128eda14cbcSMatt Macy 		print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
6129eda14cbcSMatt Macy 		    gettext("Local permissions:\n"));
6130eda14cbcSMatt Macy 		print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
6131eda14cbcSMatt Macy 		    gettext("Descendent permissions:\n"));
6132eda14cbcSMatt Macy 		print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
6133eda14cbcSMatt Macy 		    gettext("Local+Descendent permissions:\n"));
6134eda14cbcSMatt Macy 	}
6135eda14cbcSMatt Macy }
6136eda14cbcSMatt Macy 
6137eda14cbcSMatt Macy static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
6138eda14cbcSMatt Macy 
6139eda14cbcSMatt Macy struct deleg_perms {
6140eda14cbcSMatt Macy 	boolean_t un;
6141eda14cbcSMatt Macy 	nvlist_t *nvl;
6142eda14cbcSMatt Macy };
6143eda14cbcSMatt Macy 
6144eda14cbcSMatt Macy static int
6145eda14cbcSMatt Macy set_deleg_perms(zfs_handle_t *zhp, void *data)
6146eda14cbcSMatt Macy {
6147eda14cbcSMatt Macy 	struct deleg_perms *perms = (struct deleg_perms *)data;
6148eda14cbcSMatt Macy 	zfs_type_t zfs_type = zfs_get_type(zhp);
6149eda14cbcSMatt Macy 
6150eda14cbcSMatt Macy 	if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
6151eda14cbcSMatt Macy 		return (0);
6152eda14cbcSMatt Macy 
6153eda14cbcSMatt Macy 	return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
6154eda14cbcSMatt Macy }
6155eda14cbcSMatt Macy 
6156eda14cbcSMatt Macy static int
6157eda14cbcSMatt Macy zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
6158eda14cbcSMatt Macy {
6159eda14cbcSMatt Macy 	zfs_handle_t *zhp;
6160eda14cbcSMatt Macy 	nvlist_t *perm_nvl = NULL;
6161eda14cbcSMatt Macy 	nvlist_t *update_perm_nvl = NULL;
6162eda14cbcSMatt Macy 	int error = 1;
6163eda14cbcSMatt Macy 	int c;
6164eda14cbcSMatt Macy 	struct allow_opts opts = { 0 };
6165eda14cbcSMatt Macy 
6166eda14cbcSMatt Macy 	const char *optstr = un ? "ldugecsrh" : "ldugecsh";
6167eda14cbcSMatt Macy 
6168eda14cbcSMatt Macy 	/* check opts */
6169eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, optstr)) != -1) {
6170eda14cbcSMatt Macy 		switch (c) {
6171eda14cbcSMatt Macy 		case 'l':
6172eda14cbcSMatt Macy 			opts.local = B_TRUE;
6173eda14cbcSMatt Macy 			break;
6174eda14cbcSMatt Macy 		case 'd':
6175eda14cbcSMatt Macy 			opts.descend = B_TRUE;
6176eda14cbcSMatt Macy 			break;
6177eda14cbcSMatt Macy 		case 'u':
6178eda14cbcSMatt Macy 			opts.user = B_TRUE;
6179eda14cbcSMatt Macy 			break;
6180eda14cbcSMatt Macy 		case 'g':
6181eda14cbcSMatt Macy 			opts.group = B_TRUE;
6182eda14cbcSMatt Macy 			break;
6183eda14cbcSMatt Macy 		case 'e':
6184eda14cbcSMatt Macy 			opts.everyone = B_TRUE;
6185eda14cbcSMatt Macy 			break;
6186eda14cbcSMatt Macy 		case 's':
6187eda14cbcSMatt Macy 			opts.set = B_TRUE;
6188eda14cbcSMatt Macy 			break;
6189eda14cbcSMatt Macy 		case 'c':
6190eda14cbcSMatt Macy 			opts.create = B_TRUE;
6191eda14cbcSMatt Macy 			break;
6192eda14cbcSMatt Macy 		case 'r':
6193eda14cbcSMatt Macy 			opts.recursive = B_TRUE;
6194eda14cbcSMatt Macy 			break;
6195eda14cbcSMatt Macy 		case ':':
6196eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("missing argument for "
6197eda14cbcSMatt Macy 			    "'%c' option\n"), optopt);
6198eda14cbcSMatt Macy 			usage(B_FALSE);
6199eda14cbcSMatt Macy 			break;
6200eda14cbcSMatt Macy 		case 'h':
6201eda14cbcSMatt Macy 			opts.prt_usage = B_TRUE;
6202eda14cbcSMatt Macy 			break;
6203eda14cbcSMatt Macy 		case '?':
6204eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6205eda14cbcSMatt Macy 			    optopt);
6206eda14cbcSMatt Macy 			usage(B_FALSE);
6207eda14cbcSMatt Macy 		}
6208eda14cbcSMatt Macy 	}
6209eda14cbcSMatt Macy 
6210eda14cbcSMatt Macy 	argc -= optind;
6211eda14cbcSMatt Macy 	argv += optind;
6212eda14cbcSMatt Macy 
6213eda14cbcSMatt Macy 	/* check arguments */
6214eda14cbcSMatt Macy 	parse_allow_args(argc, argv, un, &opts);
6215eda14cbcSMatt Macy 
6216eda14cbcSMatt Macy 	/* try to open the dataset */
6217eda14cbcSMatt Macy 	if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
6218eda14cbcSMatt Macy 	    ZFS_TYPE_VOLUME)) == NULL) {
6219eda14cbcSMatt Macy 		(void) fprintf(stderr, "Failed to open dataset: %s\n",
6220eda14cbcSMatt Macy 		    opts.dataset);
6221eda14cbcSMatt Macy 		return (-1);
6222eda14cbcSMatt Macy 	}
6223eda14cbcSMatt Macy 
6224eda14cbcSMatt Macy 	if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
6225eda14cbcSMatt Macy 		goto cleanup2;
6226eda14cbcSMatt Macy 
6227eda14cbcSMatt Macy 	fs_perm_set_init(&fs_perm_set);
6228eda14cbcSMatt Macy 	if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
6229eda14cbcSMatt Macy 		(void) fprintf(stderr, "Failed to parse fsacl permissions\n");
6230eda14cbcSMatt Macy 		goto cleanup1;
6231eda14cbcSMatt Macy 	}
6232eda14cbcSMatt Macy 
6233eda14cbcSMatt Macy 	if (opts.prt_perms)
6234eda14cbcSMatt Macy 		print_fs_perms(&fs_perm_set);
6235eda14cbcSMatt Macy 	else {
6236eda14cbcSMatt Macy 		(void) construct_fsacl_list(un, &opts, &update_perm_nvl);
6237eda14cbcSMatt Macy 		if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
6238eda14cbcSMatt Macy 			goto cleanup0;
6239eda14cbcSMatt Macy 
6240eda14cbcSMatt Macy 		if (un && opts.recursive) {
6241eda14cbcSMatt Macy 			struct deleg_perms data = { un, update_perm_nvl };
6242eda14cbcSMatt Macy 			if (zfs_iter_filesystems(zhp, set_deleg_perms,
6243eda14cbcSMatt Macy 			    &data) != 0)
6244eda14cbcSMatt Macy 				goto cleanup0;
6245eda14cbcSMatt Macy 		}
6246eda14cbcSMatt Macy 	}
6247eda14cbcSMatt Macy 
6248eda14cbcSMatt Macy 	error = 0;
6249eda14cbcSMatt Macy 
6250eda14cbcSMatt Macy cleanup0:
6251eda14cbcSMatt Macy 	nvlist_free(perm_nvl);
6252eda14cbcSMatt Macy 	nvlist_free(update_perm_nvl);
6253eda14cbcSMatt Macy cleanup1:
6254eda14cbcSMatt Macy 	fs_perm_set_fini(&fs_perm_set);
6255eda14cbcSMatt Macy cleanup2:
6256eda14cbcSMatt Macy 	zfs_close(zhp);
6257eda14cbcSMatt Macy 
6258eda14cbcSMatt Macy 	return (error);
6259eda14cbcSMatt Macy }
6260eda14cbcSMatt Macy 
6261eda14cbcSMatt Macy static int
6262eda14cbcSMatt Macy zfs_do_allow(int argc, char **argv)
6263eda14cbcSMatt Macy {
6264eda14cbcSMatt Macy 	return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
6265eda14cbcSMatt Macy }
6266eda14cbcSMatt Macy 
6267eda14cbcSMatt Macy static int
6268eda14cbcSMatt Macy zfs_do_unallow(int argc, char **argv)
6269eda14cbcSMatt Macy {
6270eda14cbcSMatt Macy 	return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
6271eda14cbcSMatt Macy }
6272eda14cbcSMatt Macy 
6273eda14cbcSMatt Macy static int
6274eda14cbcSMatt Macy zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
6275eda14cbcSMatt Macy {
6276eda14cbcSMatt Macy 	int errors = 0;
6277eda14cbcSMatt Macy 	int i;
6278eda14cbcSMatt Macy 	const char *tag;
6279eda14cbcSMatt Macy 	boolean_t recursive = B_FALSE;
6280eda14cbcSMatt Macy 	const char *opts = holding ? "rt" : "r";
6281eda14cbcSMatt Macy 	int c;
6282eda14cbcSMatt Macy 
6283eda14cbcSMatt Macy 	/* check options */
6284eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, opts)) != -1) {
6285eda14cbcSMatt Macy 		switch (c) {
6286eda14cbcSMatt Macy 		case 'r':
6287eda14cbcSMatt Macy 			recursive = B_TRUE;
6288eda14cbcSMatt Macy 			break;
6289eda14cbcSMatt Macy 		case '?':
6290eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6291eda14cbcSMatt Macy 			    optopt);
6292eda14cbcSMatt Macy 			usage(B_FALSE);
6293eda14cbcSMatt Macy 		}
6294eda14cbcSMatt Macy 	}
6295eda14cbcSMatt Macy 
6296eda14cbcSMatt Macy 	argc -= optind;
6297eda14cbcSMatt Macy 	argv += optind;
6298eda14cbcSMatt Macy 
6299eda14cbcSMatt Macy 	/* check number of arguments */
6300eda14cbcSMatt Macy 	if (argc < 2)
6301eda14cbcSMatt Macy 		usage(B_FALSE);
6302eda14cbcSMatt Macy 
6303eda14cbcSMatt Macy 	tag = argv[0];
6304eda14cbcSMatt Macy 	--argc;
6305eda14cbcSMatt Macy 	++argv;
6306eda14cbcSMatt Macy 
6307eda14cbcSMatt Macy 	if (holding && tag[0] == '.') {
6308eda14cbcSMatt Macy 		/* tags starting with '.' are reserved for libzfs */
6309eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("tag may not start with '.'\n"));
6310eda14cbcSMatt Macy 		usage(B_FALSE);
6311eda14cbcSMatt Macy 	}
6312eda14cbcSMatt Macy 
6313eda14cbcSMatt Macy 	for (i = 0; i < argc; ++i) {
6314eda14cbcSMatt Macy 		zfs_handle_t *zhp;
6315eda14cbcSMatt Macy 		char parent[ZFS_MAX_DATASET_NAME_LEN];
6316eda14cbcSMatt Macy 		const char *delim;
6317eda14cbcSMatt Macy 		char *path = argv[i];
6318eda14cbcSMatt Macy 
6319eda14cbcSMatt Macy 		delim = strchr(path, '@');
6320eda14cbcSMatt Macy 		if (delim == NULL) {
6321eda14cbcSMatt Macy 			(void) fprintf(stderr,
6322eda14cbcSMatt Macy 			    gettext("'%s' is not a snapshot\n"), path);
6323eda14cbcSMatt Macy 			++errors;
6324eda14cbcSMatt Macy 			continue;
6325eda14cbcSMatt Macy 		}
6326eda14cbcSMatt Macy 		(void) strncpy(parent, path, delim - path);
6327eda14cbcSMatt Macy 		parent[delim - path] = '\0';
6328eda14cbcSMatt Macy 
6329eda14cbcSMatt Macy 		zhp = zfs_open(g_zfs, parent,
6330eda14cbcSMatt Macy 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
6331eda14cbcSMatt Macy 		if (zhp == NULL) {
6332eda14cbcSMatt Macy 			++errors;
6333eda14cbcSMatt Macy 			continue;
6334eda14cbcSMatt Macy 		}
6335eda14cbcSMatt Macy 		if (holding) {
6336eda14cbcSMatt Macy 			if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
6337eda14cbcSMatt Macy 				++errors;
6338eda14cbcSMatt Macy 		} else {
6339eda14cbcSMatt Macy 			if (zfs_release(zhp, delim+1, tag, recursive) != 0)
6340eda14cbcSMatt Macy 				++errors;
6341eda14cbcSMatt Macy 		}
6342eda14cbcSMatt Macy 		zfs_close(zhp);
6343eda14cbcSMatt Macy 	}
6344eda14cbcSMatt Macy 
6345eda14cbcSMatt Macy 	return (errors != 0);
6346eda14cbcSMatt Macy }
6347eda14cbcSMatt Macy 
6348eda14cbcSMatt Macy /*
6349eda14cbcSMatt Macy  * zfs hold [-r] [-t] <tag> <snap> ...
6350eda14cbcSMatt Macy  *
6351eda14cbcSMatt Macy  *	-r	Recursively hold
6352eda14cbcSMatt Macy  *
6353eda14cbcSMatt Macy  * Apply a user-hold with the given tag to the list of snapshots.
6354eda14cbcSMatt Macy  */
6355eda14cbcSMatt Macy static int
6356eda14cbcSMatt Macy zfs_do_hold(int argc, char **argv)
6357eda14cbcSMatt Macy {
6358eda14cbcSMatt Macy 	return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
6359eda14cbcSMatt Macy }
6360eda14cbcSMatt Macy 
6361eda14cbcSMatt Macy /*
6362eda14cbcSMatt Macy  * zfs release [-r] <tag> <snap> ...
6363eda14cbcSMatt Macy  *
6364eda14cbcSMatt Macy  *	-r	Recursively release
6365eda14cbcSMatt Macy  *
6366eda14cbcSMatt Macy  * Release a user-hold with the given tag from the list of snapshots.
6367eda14cbcSMatt Macy  */
6368eda14cbcSMatt Macy static int
6369eda14cbcSMatt Macy zfs_do_release(int argc, char **argv)
6370eda14cbcSMatt Macy {
6371eda14cbcSMatt Macy 	return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
6372eda14cbcSMatt Macy }
6373eda14cbcSMatt Macy 
6374eda14cbcSMatt Macy typedef struct holds_cbdata {
6375eda14cbcSMatt Macy 	boolean_t	cb_recursive;
6376eda14cbcSMatt Macy 	const char	*cb_snapname;
6377eda14cbcSMatt Macy 	nvlist_t	**cb_nvlp;
6378eda14cbcSMatt Macy 	size_t		cb_max_namelen;
6379eda14cbcSMatt Macy 	size_t		cb_max_taglen;
6380eda14cbcSMatt Macy } holds_cbdata_t;
6381eda14cbcSMatt Macy 
6382eda14cbcSMatt Macy #define	STRFTIME_FMT_STR "%a %b %e %H:%M %Y"
6383eda14cbcSMatt Macy #define	DATETIME_BUF_LEN (32)
6384eda14cbcSMatt Macy /*
6385eda14cbcSMatt Macy  *
6386eda14cbcSMatt Macy  */
6387eda14cbcSMatt Macy static void
6388eda14cbcSMatt Macy print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl)
6389eda14cbcSMatt Macy {
6390eda14cbcSMatt Macy 	int i;
6391eda14cbcSMatt Macy 	nvpair_t *nvp = NULL;
6392eda14cbcSMatt Macy 	char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
6393eda14cbcSMatt Macy 	const char *col;
6394eda14cbcSMatt Macy 
6395eda14cbcSMatt Macy 	if (!scripted) {
6396eda14cbcSMatt Macy 		for (i = 0; i < 3; i++) {
6397eda14cbcSMatt Macy 			col = gettext(hdr_cols[i]);
6398eda14cbcSMatt Macy 			if (i < 2)
6399eda14cbcSMatt Macy 				(void) printf("%-*s  ", i ? tagwidth : nwidth,
6400eda14cbcSMatt Macy 				    col);
6401eda14cbcSMatt Macy 			else
6402eda14cbcSMatt Macy 				(void) printf("%s\n", col);
6403eda14cbcSMatt Macy 		}
6404eda14cbcSMatt Macy 	}
6405eda14cbcSMatt Macy 
6406eda14cbcSMatt Macy 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
6407eda14cbcSMatt Macy 		char *zname = nvpair_name(nvp);
6408eda14cbcSMatt Macy 		nvlist_t *nvl2;
6409eda14cbcSMatt Macy 		nvpair_t *nvp2 = NULL;
6410eda14cbcSMatt Macy 		(void) nvpair_value_nvlist(nvp, &nvl2);
6411eda14cbcSMatt Macy 		while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
6412eda14cbcSMatt Macy 			char tsbuf[DATETIME_BUF_LEN];
6413eda14cbcSMatt Macy 			char *tagname = nvpair_name(nvp2);
6414eda14cbcSMatt Macy 			uint64_t val = 0;
6415eda14cbcSMatt Macy 			time_t time;
6416eda14cbcSMatt Macy 			struct tm t;
6417eda14cbcSMatt Macy 
6418eda14cbcSMatt Macy 			(void) nvpair_value_uint64(nvp2, &val);
6419eda14cbcSMatt Macy 			time = (time_t)val;
6420eda14cbcSMatt Macy 			(void) localtime_r(&time, &t);
6421eda14cbcSMatt Macy 			(void) strftime(tsbuf, DATETIME_BUF_LEN,
6422eda14cbcSMatt Macy 			    gettext(STRFTIME_FMT_STR), &t);
6423eda14cbcSMatt Macy 
6424eda14cbcSMatt Macy 			if (scripted) {
6425eda14cbcSMatt Macy 				(void) printf("%s\t%s\t%s\n", zname,
6426eda14cbcSMatt Macy 				    tagname, tsbuf);
6427eda14cbcSMatt Macy 			} else {
6428eda14cbcSMatt Macy 				(void) printf("%-*s  %-*s  %s\n", nwidth,
6429eda14cbcSMatt Macy 				    zname, tagwidth, tagname, tsbuf);
6430eda14cbcSMatt Macy 			}
6431eda14cbcSMatt Macy 		}
6432eda14cbcSMatt Macy 	}
6433eda14cbcSMatt Macy }
6434eda14cbcSMatt Macy 
6435eda14cbcSMatt Macy /*
6436eda14cbcSMatt Macy  * Generic callback function to list a dataset or snapshot.
6437eda14cbcSMatt Macy  */
6438eda14cbcSMatt Macy static int
6439eda14cbcSMatt Macy holds_callback(zfs_handle_t *zhp, void *data)
6440eda14cbcSMatt Macy {
6441eda14cbcSMatt Macy 	holds_cbdata_t *cbp = data;
6442eda14cbcSMatt Macy 	nvlist_t *top_nvl = *cbp->cb_nvlp;
6443eda14cbcSMatt Macy 	nvlist_t *nvl = NULL;
6444eda14cbcSMatt Macy 	nvpair_t *nvp = NULL;
6445eda14cbcSMatt Macy 	const char *zname = zfs_get_name(zhp);
6446eda14cbcSMatt Macy 	size_t znamelen = strlen(zname);
6447eda14cbcSMatt Macy 
6448eda14cbcSMatt Macy 	if (cbp->cb_recursive) {
6449eda14cbcSMatt Macy 		const char *snapname;
6450eda14cbcSMatt Macy 		char *delim  = strchr(zname, '@');
6451eda14cbcSMatt Macy 		if (delim == NULL)
6452eda14cbcSMatt Macy 			return (0);
6453eda14cbcSMatt Macy 
6454eda14cbcSMatt Macy 		snapname = delim + 1;
6455eda14cbcSMatt Macy 		if (strcmp(cbp->cb_snapname, snapname))
6456eda14cbcSMatt Macy 			return (0);
6457eda14cbcSMatt Macy 	}
6458eda14cbcSMatt Macy 
6459eda14cbcSMatt Macy 	if (zfs_get_holds(zhp, &nvl) != 0)
6460eda14cbcSMatt Macy 		return (-1);
6461eda14cbcSMatt Macy 
6462eda14cbcSMatt Macy 	if (znamelen > cbp->cb_max_namelen)
6463eda14cbcSMatt Macy 		cbp->cb_max_namelen  = znamelen;
6464eda14cbcSMatt Macy 
6465eda14cbcSMatt Macy 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
6466eda14cbcSMatt Macy 		const char *tag = nvpair_name(nvp);
6467eda14cbcSMatt Macy 		size_t taglen = strlen(tag);
6468eda14cbcSMatt Macy 		if (taglen > cbp->cb_max_taglen)
6469eda14cbcSMatt Macy 			cbp->cb_max_taglen  = taglen;
6470eda14cbcSMatt Macy 	}
6471eda14cbcSMatt Macy 
6472eda14cbcSMatt Macy 	return (nvlist_add_nvlist(top_nvl, zname, nvl));
6473eda14cbcSMatt Macy }
6474eda14cbcSMatt Macy 
6475eda14cbcSMatt Macy /*
6476eda14cbcSMatt Macy  * zfs holds [-rH] <snap> ...
6477eda14cbcSMatt Macy  *
6478eda14cbcSMatt Macy  *	-r	Lists holds that are set on the named snapshots recursively.
6479eda14cbcSMatt Macy  *	-H	Scripted mode; elide headers and separate columns by tabs.
6480eda14cbcSMatt Macy  */
6481eda14cbcSMatt Macy static int
6482eda14cbcSMatt Macy zfs_do_holds(int argc, char **argv)
6483eda14cbcSMatt Macy {
6484eda14cbcSMatt Macy 	int errors = 0;
6485eda14cbcSMatt Macy 	int c;
6486eda14cbcSMatt Macy 	int i;
6487eda14cbcSMatt Macy 	boolean_t scripted = B_FALSE;
6488eda14cbcSMatt Macy 	boolean_t recursive = B_FALSE;
6489eda14cbcSMatt Macy 	const char *opts = "rH";
6490eda14cbcSMatt Macy 	nvlist_t *nvl;
6491eda14cbcSMatt Macy 
6492eda14cbcSMatt Macy 	int types = ZFS_TYPE_SNAPSHOT;
6493eda14cbcSMatt Macy 	holds_cbdata_t cb = { 0 };
6494eda14cbcSMatt Macy 
6495eda14cbcSMatt Macy 	int limit = 0;
6496eda14cbcSMatt Macy 	int ret = 0;
6497eda14cbcSMatt Macy 	int flags = 0;
6498eda14cbcSMatt Macy 
6499eda14cbcSMatt Macy 	/* check options */
6500eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, opts)) != -1) {
6501eda14cbcSMatt Macy 		switch (c) {
6502eda14cbcSMatt Macy 		case 'r':
6503eda14cbcSMatt Macy 			recursive = B_TRUE;
6504eda14cbcSMatt Macy 			break;
6505eda14cbcSMatt Macy 		case 'H':
6506eda14cbcSMatt Macy 			scripted = B_TRUE;
6507eda14cbcSMatt Macy 			break;
6508eda14cbcSMatt Macy 		case '?':
6509eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
6510eda14cbcSMatt Macy 			    optopt);
6511eda14cbcSMatt Macy 			usage(B_FALSE);
6512eda14cbcSMatt Macy 		}
6513eda14cbcSMatt Macy 	}
6514eda14cbcSMatt Macy 
6515eda14cbcSMatt Macy 	if (recursive) {
6516eda14cbcSMatt Macy 		types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
6517eda14cbcSMatt Macy 		flags |= ZFS_ITER_RECURSE;
6518eda14cbcSMatt Macy 	}
6519eda14cbcSMatt Macy 
6520eda14cbcSMatt Macy 	argc -= optind;
6521eda14cbcSMatt Macy 	argv += optind;
6522eda14cbcSMatt Macy 
6523eda14cbcSMatt Macy 	/* check number of arguments */
6524eda14cbcSMatt Macy 	if (argc < 1)
6525eda14cbcSMatt Macy 		usage(B_FALSE);
6526eda14cbcSMatt Macy 
6527eda14cbcSMatt Macy 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
6528eda14cbcSMatt Macy 		nomem();
6529eda14cbcSMatt Macy 
6530eda14cbcSMatt Macy 	for (i = 0; i < argc; ++i) {
6531eda14cbcSMatt Macy 		char *snapshot = argv[i];
6532eda14cbcSMatt Macy 		const char *delim;
6533eda14cbcSMatt Macy 		const char *snapname;
6534eda14cbcSMatt Macy 
6535eda14cbcSMatt Macy 		delim = strchr(snapshot, '@');
6536eda14cbcSMatt Macy 		if (delim == NULL) {
6537eda14cbcSMatt Macy 			(void) fprintf(stderr,
6538eda14cbcSMatt Macy 			    gettext("'%s' is not a snapshot\n"), snapshot);
6539eda14cbcSMatt Macy 			++errors;
6540eda14cbcSMatt Macy 			continue;
6541eda14cbcSMatt Macy 		}
6542eda14cbcSMatt Macy 		snapname = delim + 1;
6543eda14cbcSMatt Macy 		if (recursive)
6544eda14cbcSMatt Macy 			snapshot[delim - snapshot] = '\0';
6545eda14cbcSMatt Macy 
6546eda14cbcSMatt Macy 		cb.cb_recursive = recursive;
6547eda14cbcSMatt Macy 		cb.cb_snapname = snapname;
6548eda14cbcSMatt Macy 		cb.cb_nvlp = &nvl;
6549eda14cbcSMatt Macy 
6550eda14cbcSMatt Macy 		/*
6551eda14cbcSMatt Macy 		 *  1. collect holds data, set format options
6552eda14cbcSMatt Macy 		 */
6553eda14cbcSMatt Macy 		ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
6554eda14cbcSMatt Macy 		    holds_callback, &cb);
6555eda14cbcSMatt Macy 		if (ret != 0)
6556eda14cbcSMatt Macy 			++errors;
6557eda14cbcSMatt Macy 	}
6558eda14cbcSMatt Macy 
6559eda14cbcSMatt Macy 	/*
6560eda14cbcSMatt Macy 	 *  2. print holds data
6561eda14cbcSMatt Macy 	 */
6562eda14cbcSMatt Macy 	print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
6563eda14cbcSMatt Macy 
6564eda14cbcSMatt Macy 	if (nvlist_empty(nvl))
6565eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("no datasets available\n"));
6566eda14cbcSMatt Macy 
6567eda14cbcSMatt Macy 	nvlist_free(nvl);
6568eda14cbcSMatt Macy 
6569eda14cbcSMatt Macy 	return (0 != errors);
6570eda14cbcSMatt Macy }
6571eda14cbcSMatt Macy 
6572eda14cbcSMatt Macy #define	CHECK_SPINNER 30
6573eda14cbcSMatt Macy #define	SPINNER_TIME 3		/* seconds */
6574eda14cbcSMatt Macy #define	MOUNT_TIME 1		/* seconds */
6575eda14cbcSMatt Macy 
6576eda14cbcSMatt Macy typedef struct get_all_state {
6577eda14cbcSMatt Macy 	boolean_t	ga_verbose;
6578eda14cbcSMatt Macy 	get_all_cb_t	*ga_cbp;
6579eda14cbcSMatt Macy } get_all_state_t;
6580eda14cbcSMatt Macy 
6581eda14cbcSMatt Macy static int
6582eda14cbcSMatt Macy get_one_dataset(zfs_handle_t *zhp, void *data)
6583eda14cbcSMatt Macy {
6584eda14cbcSMatt Macy 	static char *spin[] = { "-", "\\", "|", "/" };
6585eda14cbcSMatt Macy 	static int spinval = 0;
6586eda14cbcSMatt Macy 	static int spincheck = 0;
6587eda14cbcSMatt Macy 	static time_t last_spin_time = (time_t)0;
6588eda14cbcSMatt Macy 	get_all_state_t *state = data;
6589eda14cbcSMatt Macy 	zfs_type_t type = zfs_get_type(zhp);
6590eda14cbcSMatt Macy 
6591eda14cbcSMatt Macy 	if (state->ga_verbose) {
6592eda14cbcSMatt Macy 		if (--spincheck < 0) {
6593eda14cbcSMatt Macy 			time_t now = time(NULL);
6594eda14cbcSMatt Macy 			if (last_spin_time + SPINNER_TIME < now) {
6595eda14cbcSMatt Macy 				update_progress(spin[spinval++ % 4]);
6596eda14cbcSMatt Macy 				last_spin_time = now;
6597eda14cbcSMatt Macy 			}
6598eda14cbcSMatt Macy 			spincheck = CHECK_SPINNER;
6599eda14cbcSMatt Macy 		}
6600eda14cbcSMatt Macy 	}
6601eda14cbcSMatt Macy 
6602eda14cbcSMatt Macy 	/*
6603eda14cbcSMatt Macy 	 * Iterate over any nested datasets.
6604eda14cbcSMatt Macy 	 */
6605eda14cbcSMatt Macy 	if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
6606eda14cbcSMatt Macy 		zfs_close(zhp);
6607eda14cbcSMatt Macy 		return (1);
6608eda14cbcSMatt Macy 	}
6609eda14cbcSMatt Macy 
6610eda14cbcSMatt Macy 	/*
6611eda14cbcSMatt Macy 	 * Skip any datasets whose type does not match.
6612eda14cbcSMatt Macy 	 */
6613eda14cbcSMatt Macy 	if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
6614eda14cbcSMatt Macy 		zfs_close(zhp);
6615eda14cbcSMatt Macy 		return (0);
6616eda14cbcSMatt Macy 	}
6617eda14cbcSMatt Macy 	libzfs_add_handle(state->ga_cbp, zhp);
6618eda14cbcSMatt Macy 	assert(state->ga_cbp->cb_used <= state->ga_cbp->cb_alloc);
6619eda14cbcSMatt Macy 
6620eda14cbcSMatt Macy 	return (0);
6621eda14cbcSMatt Macy }
6622eda14cbcSMatt Macy 
6623eda14cbcSMatt Macy static void
6624eda14cbcSMatt Macy get_all_datasets(get_all_cb_t *cbp, boolean_t verbose)
6625eda14cbcSMatt Macy {
6626eda14cbcSMatt Macy 	get_all_state_t state = {
6627eda14cbcSMatt Macy 	    .ga_verbose = verbose,
6628eda14cbcSMatt Macy 	    .ga_cbp = cbp
6629eda14cbcSMatt Macy 	};
6630eda14cbcSMatt Macy 
6631eda14cbcSMatt Macy 	if (verbose)
6632eda14cbcSMatt Macy 		set_progress_header(gettext("Reading ZFS config"));
6633eda14cbcSMatt Macy 	(void) zfs_iter_root(g_zfs, get_one_dataset, &state);
6634eda14cbcSMatt Macy 
6635eda14cbcSMatt Macy 	if (verbose)
6636eda14cbcSMatt Macy 		finish_progress(gettext("done."));
6637eda14cbcSMatt Macy }
6638eda14cbcSMatt Macy 
6639eda14cbcSMatt Macy /*
6640eda14cbcSMatt Macy  * Generic callback for sharing or mounting filesystems.  Because the code is so
6641eda14cbcSMatt Macy  * similar, we have a common function with an extra parameter to determine which
6642eda14cbcSMatt Macy  * mode we are using.
6643eda14cbcSMatt Macy  */
6644eda14cbcSMatt Macy typedef enum { OP_SHARE, OP_MOUNT } share_mount_op_t;
6645eda14cbcSMatt Macy 
6646eda14cbcSMatt Macy typedef struct share_mount_state {
6647eda14cbcSMatt Macy 	share_mount_op_t	sm_op;
6648eda14cbcSMatt Macy 	boolean_t	sm_verbose;
6649eda14cbcSMatt Macy 	int	sm_flags;
6650eda14cbcSMatt Macy 	char	*sm_options;
6651eda14cbcSMatt Macy 	char	*sm_proto; /* only valid for OP_SHARE */
6652eda14cbcSMatt Macy 	pthread_mutex_t	sm_lock; /* protects the remaining fields */
6653eda14cbcSMatt Macy 	uint_t	sm_total; /* number of filesystems to process */
6654eda14cbcSMatt Macy 	uint_t	sm_done; /* number of filesystems processed */
6655eda14cbcSMatt Macy 	int	sm_status; /* -1 if any of the share/mount operations failed */
6656eda14cbcSMatt Macy } share_mount_state_t;
6657eda14cbcSMatt Macy 
6658eda14cbcSMatt Macy /*
6659eda14cbcSMatt Macy  * Share or mount a dataset.
6660eda14cbcSMatt Macy  */
6661eda14cbcSMatt Macy static int
6662eda14cbcSMatt Macy share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
6663eda14cbcSMatt Macy     boolean_t explicit, const char *options)
6664eda14cbcSMatt Macy {
6665eda14cbcSMatt Macy 	char mountpoint[ZFS_MAXPROPLEN];
6666eda14cbcSMatt Macy 	char shareopts[ZFS_MAXPROPLEN];
6667eda14cbcSMatt Macy 	char smbshareopts[ZFS_MAXPROPLEN];
6668eda14cbcSMatt Macy 	const char *cmdname = op == OP_SHARE ? "share" : "mount";
6669eda14cbcSMatt Macy 	struct mnttab mnt;
6670eda14cbcSMatt Macy 	uint64_t zoned, canmount;
6671eda14cbcSMatt Macy 	boolean_t shared_nfs, shared_smb;
6672eda14cbcSMatt Macy 
6673eda14cbcSMatt Macy 	assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
6674eda14cbcSMatt Macy 
6675eda14cbcSMatt Macy 	/*
6676eda14cbcSMatt Macy 	 * Check to make sure we can mount/share this dataset.  If we
6677eda14cbcSMatt Macy 	 * are in the global zone and the filesystem is exported to a
6678eda14cbcSMatt Macy 	 * local zone, or if we are in a local zone and the
6679eda14cbcSMatt Macy 	 * filesystem is not exported, then it is an error.
6680eda14cbcSMatt Macy 	 */
6681eda14cbcSMatt Macy 	zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
6682eda14cbcSMatt Macy 
6683eda14cbcSMatt Macy 	if (zoned && getzoneid() == GLOBAL_ZONEID) {
6684eda14cbcSMatt Macy 		if (!explicit)
6685eda14cbcSMatt Macy 			return (0);
6686eda14cbcSMatt Macy 
6687eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("cannot %s '%s': "
6688eda14cbcSMatt Macy 		    "dataset is exported to a local zone\n"), cmdname,
6689eda14cbcSMatt Macy 		    zfs_get_name(zhp));
6690eda14cbcSMatt Macy 		return (1);
6691eda14cbcSMatt Macy 
6692eda14cbcSMatt Macy 	} else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
6693eda14cbcSMatt Macy 		if (!explicit)
6694eda14cbcSMatt Macy 			return (0);
6695eda14cbcSMatt Macy 
6696eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("cannot %s '%s': "
6697eda14cbcSMatt Macy 		    "permission denied\n"), cmdname,
6698eda14cbcSMatt Macy 		    zfs_get_name(zhp));
6699eda14cbcSMatt Macy 		return (1);
6700eda14cbcSMatt Macy 	}
6701eda14cbcSMatt Macy 
6702eda14cbcSMatt Macy 	/*
6703eda14cbcSMatt Macy 	 * Ignore any filesystems which don't apply to us. This
6704eda14cbcSMatt Macy 	 * includes those with a legacy mountpoint, or those with
6705eda14cbcSMatt Macy 	 * legacy share options.
6706eda14cbcSMatt Macy 	 */
6707eda14cbcSMatt Macy 	verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6708eda14cbcSMatt Macy 	    sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
6709eda14cbcSMatt Macy 	verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
6710eda14cbcSMatt Macy 	    sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
6711eda14cbcSMatt Macy 	verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
6712eda14cbcSMatt Macy 	    sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
6713eda14cbcSMatt Macy 
6714eda14cbcSMatt Macy 	if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
6715eda14cbcSMatt Macy 	    strcmp(smbshareopts, "off") == 0) {
6716eda14cbcSMatt Macy 		if (!explicit)
6717eda14cbcSMatt Macy 			return (0);
6718eda14cbcSMatt Macy 
6719eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("cannot share '%s': "
6720eda14cbcSMatt Macy 		    "legacy share\n"), zfs_get_name(zhp));
6721*7877fdebSMatt Macy 		(void) fprintf(stderr, gettext("use exports(5) or "
6722*7877fdebSMatt Macy 		    "smb.conf(5) to share this filesystem, or set "
6723*7877fdebSMatt Macy 		    "the sharenfs or sharesmb property\n"));
6724eda14cbcSMatt Macy 		return (1);
6725eda14cbcSMatt Macy 	}
6726eda14cbcSMatt Macy 
6727eda14cbcSMatt Macy 	/*
6728eda14cbcSMatt Macy 	 * We cannot share or mount legacy filesystems. If the
6729eda14cbcSMatt Macy 	 * shareopts is non-legacy but the mountpoint is legacy, we
6730eda14cbcSMatt Macy 	 * treat it as a legacy share.
6731eda14cbcSMatt Macy 	 */
6732eda14cbcSMatt Macy 	if (strcmp(mountpoint, "legacy") == 0) {
6733eda14cbcSMatt Macy 		if (!explicit)
6734eda14cbcSMatt Macy 			return (0);
6735eda14cbcSMatt Macy 
6736eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("cannot %s '%s': "
6737eda14cbcSMatt Macy 		    "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
6738*7877fdebSMatt Macy 		(void) fprintf(stderr, gettext("use %s(8) to "
6739eda14cbcSMatt Macy 		    "%s this filesystem\n"), cmdname, cmdname);
6740eda14cbcSMatt Macy 		return (1);
6741eda14cbcSMatt Macy 	}
6742eda14cbcSMatt Macy 
6743eda14cbcSMatt Macy 	if (strcmp(mountpoint, "none") == 0) {
6744eda14cbcSMatt Macy 		if (!explicit)
6745eda14cbcSMatt Macy 			return (0);
6746eda14cbcSMatt Macy 
6747eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("cannot %s '%s': no "
6748eda14cbcSMatt Macy 		    "mountpoint set\n"), cmdname, zfs_get_name(zhp));
6749eda14cbcSMatt Macy 		return (1);
6750eda14cbcSMatt Macy 	}
6751eda14cbcSMatt Macy 
6752eda14cbcSMatt Macy 	/*
6753eda14cbcSMatt Macy 	 * canmount	explicit	outcome
6754eda14cbcSMatt Macy 	 * on		no		pass through
6755eda14cbcSMatt Macy 	 * on		yes		pass through
6756eda14cbcSMatt Macy 	 * off		no		return 0
6757eda14cbcSMatt Macy 	 * off		yes		display error, return 1
6758eda14cbcSMatt Macy 	 * noauto	no		return 0
6759eda14cbcSMatt Macy 	 * noauto	yes		pass through
6760eda14cbcSMatt Macy 	 */
6761eda14cbcSMatt Macy 	canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
6762eda14cbcSMatt Macy 	if (canmount == ZFS_CANMOUNT_OFF) {
6763eda14cbcSMatt Macy 		if (!explicit)
6764eda14cbcSMatt Macy 			return (0);
6765eda14cbcSMatt Macy 
6766eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("cannot %s '%s': "
6767eda14cbcSMatt Macy 		    "'canmount' property is set to 'off'\n"), cmdname,
6768eda14cbcSMatt Macy 		    zfs_get_name(zhp));
6769eda14cbcSMatt Macy 		return (1);
6770eda14cbcSMatt Macy 	} else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
6771eda14cbcSMatt Macy 		/*
6772eda14cbcSMatt Macy 		 * When performing a 'zfs mount -a', we skip any mounts for
6773eda14cbcSMatt Macy 		 * datasets that have 'noauto' set. Sharing a dataset with
6774eda14cbcSMatt Macy 		 * 'noauto' set is only allowed if it's mounted.
6775eda14cbcSMatt Macy 		 */
6776eda14cbcSMatt Macy 		if (op == OP_MOUNT)
6777eda14cbcSMatt Macy 			return (0);
6778eda14cbcSMatt Macy 		if (op == OP_SHARE && !zfs_is_mounted(zhp, NULL)) {
6779eda14cbcSMatt Macy 			/* also purge it from existing exports */
6780eda14cbcSMatt Macy 			zfs_unshareall_bypath(zhp, mountpoint);
6781eda14cbcSMatt Macy 			return (0);
6782eda14cbcSMatt Macy 		}
6783eda14cbcSMatt Macy 	}
6784eda14cbcSMatt Macy 
6785eda14cbcSMatt Macy 	/*
6786eda14cbcSMatt Macy 	 * If this filesystem is encrypted and does not have
6787eda14cbcSMatt Macy 	 * a loaded key, we can not mount it.
6788eda14cbcSMatt Macy 	 */
6789eda14cbcSMatt Macy 	if ((flags & MS_CRYPT) == 0 &&
6790eda14cbcSMatt Macy 	    zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF &&
6791eda14cbcSMatt Macy 	    zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
6792eda14cbcSMatt Macy 	    ZFS_KEYSTATUS_UNAVAILABLE) {
6793eda14cbcSMatt Macy 		if (!explicit)
6794eda14cbcSMatt Macy 			return (0);
6795eda14cbcSMatt Macy 
6796eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("cannot %s '%s': "
6797eda14cbcSMatt Macy 		    "encryption key not loaded\n"), cmdname, zfs_get_name(zhp));
6798eda14cbcSMatt Macy 		return (1);
6799eda14cbcSMatt Macy 	}
6800eda14cbcSMatt Macy 
6801eda14cbcSMatt Macy 	/*
6802eda14cbcSMatt Macy 	 * If this filesystem is inconsistent and has a receive resume
6803eda14cbcSMatt Macy 	 * token, we can not mount it.
6804eda14cbcSMatt Macy 	 */
6805eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
6806eda14cbcSMatt Macy 	    zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
6807eda14cbcSMatt Macy 	    NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
6808eda14cbcSMatt Macy 		if (!explicit)
6809eda14cbcSMatt Macy 			return (0);
6810eda14cbcSMatt Macy 
6811eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("cannot %s '%s': "
6812eda14cbcSMatt Macy 		    "Contains partially-completed state from "
6813eda14cbcSMatt Macy 		    "\"zfs receive -s\", which can be resumed with "
6814eda14cbcSMatt Macy 		    "\"zfs send -t\"\n"),
6815eda14cbcSMatt Macy 		    cmdname, zfs_get_name(zhp));
6816eda14cbcSMatt Macy 		return (1);
6817eda14cbcSMatt Macy 	}
6818eda14cbcSMatt Macy 
6819eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_REDACTED) && !(flags & MS_FORCE)) {
6820eda14cbcSMatt Macy 		if (!explicit)
6821eda14cbcSMatt Macy 			return (0);
6822eda14cbcSMatt Macy 
6823eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("cannot %s '%s': "
6824eda14cbcSMatt Macy 		    "Dataset is not complete, was created by receiving "
6825eda14cbcSMatt Macy 		    "a redacted zfs send stream.\n"), cmdname,
6826eda14cbcSMatt Macy 		    zfs_get_name(zhp));
6827eda14cbcSMatt Macy 		return (1);
6828eda14cbcSMatt Macy 	}
6829eda14cbcSMatt Macy 
6830eda14cbcSMatt Macy 	/*
6831eda14cbcSMatt Macy 	 * At this point, we have verified that the mountpoint and/or
6832eda14cbcSMatt Macy 	 * shareopts are appropriate for auto management. If the
6833eda14cbcSMatt Macy 	 * filesystem is already mounted or shared, return (failing
6834eda14cbcSMatt Macy 	 * for explicit requests); otherwise mount or share the
6835eda14cbcSMatt Macy 	 * filesystem.
6836eda14cbcSMatt Macy 	 */
6837eda14cbcSMatt Macy 	switch (op) {
6838eda14cbcSMatt Macy 	case OP_SHARE:
6839eda14cbcSMatt Macy 
6840eda14cbcSMatt Macy 		shared_nfs = zfs_is_shared_nfs(zhp, NULL);
6841eda14cbcSMatt Macy 		shared_smb = zfs_is_shared_smb(zhp, NULL);
6842eda14cbcSMatt Macy 
6843eda14cbcSMatt Macy 		if ((shared_nfs && shared_smb) ||
6844eda14cbcSMatt Macy 		    (shared_nfs && strcmp(shareopts, "on") == 0 &&
6845eda14cbcSMatt Macy 		    strcmp(smbshareopts, "off") == 0) ||
6846eda14cbcSMatt Macy 		    (shared_smb && strcmp(smbshareopts, "on") == 0 &&
6847eda14cbcSMatt Macy 		    strcmp(shareopts, "off") == 0)) {
6848eda14cbcSMatt Macy 			if (!explicit)
6849eda14cbcSMatt Macy 				return (0);
6850eda14cbcSMatt Macy 
6851eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("cannot share "
6852eda14cbcSMatt Macy 			    "'%s': filesystem already shared\n"),
6853eda14cbcSMatt Macy 			    zfs_get_name(zhp));
6854eda14cbcSMatt Macy 			return (1);
6855eda14cbcSMatt Macy 		}
6856eda14cbcSMatt Macy 
6857eda14cbcSMatt Macy 		if (!zfs_is_mounted(zhp, NULL) &&
6858eda14cbcSMatt Macy 		    zfs_mount(zhp, NULL, flags) != 0)
6859eda14cbcSMatt Macy 			return (1);
6860eda14cbcSMatt Macy 
6861eda14cbcSMatt Macy 		if (protocol == NULL) {
6862eda14cbcSMatt Macy 			if (zfs_shareall(zhp) != 0)
6863eda14cbcSMatt Macy 				return (1);
6864eda14cbcSMatt Macy 		} else if (strcmp(protocol, "nfs") == 0) {
6865eda14cbcSMatt Macy 			if (zfs_share_nfs(zhp))
6866eda14cbcSMatt Macy 				return (1);
6867eda14cbcSMatt Macy 		} else if (strcmp(protocol, "smb") == 0) {
6868eda14cbcSMatt Macy 			if (zfs_share_smb(zhp))
6869eda14cbcSMatt Macy 				return (1);
6870eda14cbcSMatt Macy 		} else {
6871eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("cannot share "
6872eda14cbcSMatt Macy 			    "'%s': invalid share type '%s' "
6873eda14cbcSMatt Macy 			    "specified\n"),
6874eda14cbcSMatt Macy 			    zfs_get_name(zhp), protocol);
6875eda14cbcSMatt Macy 			return (1);
6876eda14cbcSMatt Macy 		}
6877eda14cbcSMatt Macy 
6878eda14cbcSMatt Macy 		break;
6879eda14cbcSMatt Macy 
6880eda14cbcSMatt Macy 	case OP_MOUNT:
6881eda14cbcSMatt Macy 		if (options == NULL)
6882eda14cbcSMatt Macy 			mnt.mnt_mntopts = "";
6883eda14cbcSMatt Macy 		else
6884eda14cbcSMatt Macy 			mnt.mnt_mntopts = (char *)options;
6885eda14cbcSMatt Macy 
6886eda14cbcSMatt Macy 		if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
6887eda14cbcSMatt Macy 		    zfs_is_mounted(zhp, NULL)) {
6888eda14cbcSMatt Macy 			if (!explicit)
6889eda14cbcSMatt Macy 				return (0);
6890eda14cbcSMatt Macy 
6891eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("cannot mount "
6892eda14cbcSMatt Macy 			    "'%s': filesystem already mounted\n"),
6893eda14cbcSMatt Macy 			    zfs_get_name(zhp));
6894eda14cbcSMatt Macy 			return (1);
6895eda14cbcSMatt Macy 		}
6896eda14cbcSMatt Macy 
6897eda14cbcSMatt Macy 		if (zfs_mount(zhp, options, flags) != 0)
6898eda14cbcSMatt Macy 			return (1);
6899eda14cbcSMatt Macy 		break;
6900eda14cbcSMatt Macy 	}
6901eda14cbcSMatt Macy 
6902eda14cbcSMatt Macy 	return (0);
6903eda14cbcSMatt Macy }
6904eda14cbcSMatt Macy 
6905eda14cbcSMatt Macy /*
6906eda14cbcSMatt Macy  * Reports progress in the form "(current/total)".  Not thread-safe.
6907eda14cbcSMatt Macy  */
6908eda14cbcSMatt Macy static void
6909eda14cbcSMatt Macy report_mount_progress(int current, int total)
6910eda14cbcSMatt Macy {
6911eda14cbcSMatt Macy 	static time_t last_progress_time = 0;
6912eda14cbcSMatt Macy 	time_t now = time(NULL);
6913eda14cbcSMatt Macy 	char info[32];
6914eda14cbcSMatt Macy 
6915eda14cbcSMatt Macy 	/* report 1..n instead of 0..n-1 */
6916eda14cbcSMatt Macy 	++current;
6917eda14cbcSMatt Macy 
6918eda14cbcSMatt Macy 	/* display header if we're here for the first time */
6919eda14cbcSMatt Macy 	if (current == 1) {
6920eda14cbcSMatt Macy 		set_progress_header(gettext("Mounting ZFS filesystems"));
6921eda14cbcSMatt Macy 	} else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6922eda14cbcSMatt Macy 		/* too soon to report again */
6923eda14cbcSMatt Macy 		return;
6924eda14cbcSMatt Macy 	}
6925eda14cbcSMatt Macy 
6926eda14cbcSMatt Macy 	last_progress_time = now;
6927eda14cbcSMatt Macy 
6928eda14cbcSMatt Macy 	(void) sprintf(info, "(%d/%d)", current, total);
6929eda14cbcSMatt Macy 
6930eda14cbcSMatt Macy 	if (current == total)
6931eda14cbcSMatt Macy 		finish_progress(info);
6932eda14cbcSMatt Macy 	else
6933eda14cbcSMatt Macy 		update_progress(info);
6934eda14cbcSMatt Macy }
6935eda14cbcSMatt Macy 
6936eda14cbcSMatt Macy /*
6937eda14cbcSMatt Macy  * zfs_foreach_mountpoint() callback that mounts or shares one filesystem and
6938eda14cbcSMatt Macy  * updates the progress meter.
6939eda14cbcSMatt Macy  */
6940eda14cbcSMatt Macy static int
6941eda14cbcSMatt Macy share_mount_one_cb(zfs_handle_t *zhp, void *arg)
6942eda14cbcSMatt Macy {
6943eda14cbcSMatt Macy 	share_mount_state_t *sms = arg;
6944eda14cbcSMatt Macy 	int ret;
6945eda14cbcSMatt Macy 
6946eda14cbcSMatt Macy 	ret = share_mount_one(zhp, sms->sm_op, sms->sm_flags, sms->sm_proto,
6947eda14cbcSMatt Macy 	    B_FALSE, sms->sm_options);
6948eda14cbcSMatt Macy 
6949eda14cbcSMatt Macy 	pthread_mutex_lock(&sms->sm_lock);
6950eda14cbcSMatt Macy 	if (ret != 0)
6951eda14cbcSMatt Macy 		sms->sm_status = ret;
6952eda14cbcSMatt Macy 	sms->sm_done++;
6953eda14cbcSMatt Macy 	if (sms->sm_verbose)
6954eda14cbcSMatt Macy 		report_mount_progress(sms->sm_done, sms->sm_total);
6955eda14cbcSMatt Macy 	pthread_mutex_unlock(&sms->sm_lock);
6956eda14cbcSMatt Macy 	return (ret);
6957eda14cbcSMatt Macy }
6958eda14cbcSMatt Macy 
6959eda14cbcSMatt Macy static void
6960eda14cbcSMatt Macy append_options(char *mntopts, char *newopts)
6961eda14cbcSMatt Macy {
6962eda14cbcSMatt Macy 	int len = strlen(mntopts);
6963eda14cbcSMatt Macy 
6964eda14cbcSMatt Macy 	/* original length plus new string to append plus 1 for the comma */
6965eda14cbcSMatt Macy 	if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6966eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("the opts argument for "
6967eda14cbcSMatt Macy 		    "'%s' option is too long (more than %d chars)\n"),
6968eda14cbcSMatt Macy 		    "-o", MNT_LINE_MAX);
6969eda14cbcSMatt Macy 		usage(B_FALSE);
6970eda14cbcSMatt Macy 	}
6971eda14cbcSMatt Macy 
6972eda14cbcSMatt Macy 	if (*mntopts)
6973eda14cbcSMatt Macy 		mntopts[len++] = ',';
6974eda14cbcSMatt Macy 
6975eda14cbcSMatt Macy 	(void) strcpy(&mntopts[len], newopts);
6976eda14cbcSMatt Macy }
6977eda14cbcSMatt Macy 
6978eda14cbcSMatt Macy static int
6979eda14cbcSMatt Macy share_mount(int op, int argc, char **argv)
6980eda14cbcSMatt Macy {
6981eda14cbcSMatt Macy 	int do_all = 0;
6982eda14cbcSMatt Macy 	boolean_t verbose = B_FALSE;
6983eda14cbcSMatt Macy 	int c, ret = 0;
6984eda14cbcSMatt Macy 	char *options = NULL;
6985eda14cbcSMatt Macy 	int flags = 0;
6986eda14cbcSMatt Macy 
6987eda14cbcSMatt Macy 	/* check options */
6988eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, op == OP_MOUNT ? ":alvo:Of" : "al"))
6989eda14cbcSMatt Macy 	    != -1) {
6990eda14cbcSMatt Macy 		switch (c) {
6991eda14cbcSMatt Macy 		case 'a':
6992eda14cbcSMatt Macy 			do_all = 1;
6993eda14cbcSMatt Macy 			break;
6994eda14cbcSMatt Macy 		case 'v':
6995eda14cbcSMatt Macy 			verbose = B_TRUE;
6996eda14cbcSMatt Macy 			break;
6997eda14cbcSMatt Macy 		case 'l':
6998eda14cbcSMatt Macy 			flags |= MS_CRYPT;
6999eda14cbcSMatt Macy 			break;
7000eda14cbcSMatt Macy 		case 'o':
7001eda14cbcSMatt Macy 			if (*optarg == '\0') {
7002eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("empty mount "
7003eda14cbcSMatt Macy 				    "options (-o) specified\n"));
7004eda14cbcSMatt Macy 				usage(B_FALSE);
7005eda14cbcSMatt Macy 			}
7006eda14cbcSMatt Macy 
7007eda14cbcSMatt Macy 			if (options == NULL)
7008eda14cbcSMatt Macy 				options = safe_malloc(MNT_LINE_MAX + 1);
7009eda14cbcSMatt Macy 
7010eda14cbcSMatt Macy 			/* option validation is done later */
7011eda14cbcSMatt Macy 			append_options(options, optarg);
7012eda14cbcSMatt Macy 			break;
7013eda14cbcSMatt Macy 		case 'O':
7014eda14cbcSMatt Macy 			flags |= MS_OVERLAY;
7015eda14cbcSMatt Macy 			break;
7016eda14cbcSMatt Macy 		case 'f':
7017eda14cbcSMatt Macy 			flags |= MS_FORCE;
7018eda14cbcSMatt Macy 			break;
7019eda14cbcSMatt Macy 		case ':':
7020eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("missing argument for "
7021eda14cbcSMatt Macy 			    "'%c' option\n"), optopt);
7022eda14cbcSMatt Macy 			usage(B_FALSE);
7023eda14cbcSMatt Macy 			break;
7024eda14cbcSMatt Macy 		case '?':
7025eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
7026eda14cbcSMatt Macy 			    optopt);
7027eda14cbcSMatt Macy 			usage(B_FALSE);
7028eda14cbcSMatt Macy 		}
7029eda14cbcSMatt Macy 	}
7030eda14cbcSMatt Macy 
7031eda14cbcSMatt Macy 	argc -= optind;
7032eda14cbcSMatt Macy 	argv += optind;
7033eda14cbcSMatt Macy 
7034eda14cbcSMatt Macy 	/* check number of arguments */
7035eda14cbcSMatt Macy 	if (do_all) {
7036eda14cbcSMatt Macy 		char *protocol = NULL;
7037eda14cbcSMatt Macy 
7038eda14cbcSMatt Macy 		if (op == OP_SHARE && argc > 0) {
7039eda14cbcSMatt Macy 			if (strcmp(argv[0], "nfs") != 0 &&
7040eda14cbcSMatt Macy 			    strcmp(argv[0], "smb") != 0) {
7041eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("share type "
7042eda14cbcSMatt Macy 				    "must be 'nfs' or 'smb'\n"));
7043eda14cbcSMatt Macy 				usage(B_FALSE);
7044eda14cbcSMatt Macy 			}
7045eda14cbcSMatt Macy 			protocol = argv[0];
7046eda14cbcSMatt Macy 			argc--;
7047eda14cbcSMatt Macy 			argv++;
7048eda14cbcSMatt Macy 		}
7049eda14cbcSMatt Macy 
7050eda14cbcSMatt Macy 		if (argc != 0) {
7051eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("too many arguments\n"));
7052eda14cbcSMatt Macy 			usage(B_FALSE);
7053eda14cbcSMatt Macy 		}
7054eda14cbcSMatt Macy 
7055eda14cbcSMatt Macy 		start_progress_timer();
7056eda14cbcSMatt Macy 		get_all_cb_t cb = { 0 };
7057eda14cbcSMatt Macy 		get_all_datasets(&cb, verbose);
7058eda14cbcSMatt Macy 
7059eda14cbcSMatt Macy 		if (cb.cb_used == 0) {
7060eda14cbcSMatt Macy 			if (options != NULL)
7061eda14cbcSMatt Macy 				free(options);
7062eda14cbcSMatt Macy 			return (0);
7063eda14cbcSMatt Macy 		}
7064eda14cbcSMatt Macy 
7065eda14cbcSMatt Macy 		share_mount_state_t share_mount_state = { 0 };
7066eda14cbcSMatt Macy 		share_mount_state.sm_op = op;
7067eda14cbcSMatt Macy 		share_mount_state.sm_verbose = verbose;
7068eda14cbcSMatt Macy 		share_mount_state.sm_flags = flags;
7069eda14cbcSMatt Macy 		share_mount_state.sm_options = options;
7070eda14cbcSMatt Macy 		share_mount_state.sm_proto = protocol;
7071eda14cbcSMatt Macy 		share_mount_state.sm_total = cb.cb_used;
7072eda14cbcSMatt Macy 		pthread_mutex_init(&share_mount_state.sm_lock, NULL);
7073eda14cbcSMatt Macy 
7074eda14cbcSMatt Macy 		/*
7075eda14cbcSMatt Macy 		 * libshare isn't mt-safe, so only do the operation in parallel
7076eda14cbcSMatt Macy 		 * if we're mounting. Additionally, the key-loading option must
7077eda14cbcSMatt Macy 		 * be serialized so that we can prompt the user for their keys
7078eda14cbcSMatt Macy 		 * in a consistent manner.
7079eda14cbcSMatt Macy 		 */
7080eda14cbcSMatt Macy 		zfs_foreach_mountpoint(g_zfs, cb.cb_handles, cb.cb_used,
7081eda14cbcSMatt Macy 		    share_mount_one_cb, &share_mount_state,
7082eda14cbcSMatt Macy 		    op == OP_MOUNT && !(flags & MS_CRYPT));
7083eda14cbcSMatt Macy 		zfs_commit_all_shares();
7084eda14cbcSMatt Macy 
7085eda14cbcSMatt Macy 		ret = share_mount_state.sm_status;
7086eda14cbcSMatt Macy 
7087eda14cbcSMatt Macy 		for (int i = 0; i < cb.cb_used; i++)
7088eda14cbcSMatt Macy 			zfs_close(cb.cb_handles[i]);
7089eda14cbcSMatt Macy 		free(cb.cb_handles);
7090eda14cbcSMatt Macy 	} else if (argc == 0) {
7091eda14cbcSMatt Macy 		struct mnttab entry;
7092eda14cbcSMatt Macy 
7093eda14cbcSMatt Macy 		if ((op == OP_SHARE) || (options != NULL)) {
7094eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("missing filesystem "
7095eda14cbcSMatt Macy 			    "argument (specify -a for all)\n"));
7096eda14cbcSMatt Macy 			usage(B_FALSE);
7097eda14cbcSMatt Macy 		}
7098eda14cbcSMatt Macy 
7099eda14cbcSMatt Macy 		/*
7100eda14cbcSMatt Macy 		 * When mount is given no arguments, go through
7101eda14cbcSMatt Macy 		 * /proc/self/mounts and display any active ZFS mounts.
7102eda14cbcSMatt Macy 		 * We hide any snapshots, since they are controlled
7103eda14cbcSMatt Macy 		 * automatically.
7104eda14cbcSMatt Macy 		 */
7105eda14cbcSMatt Macy 
7106eda14cbcSMatt Macy 		/* Reopen MNTTAB to prevent reading stale data from open file */
7107eda14cbcSMatt Macy 		if (freopen(MNTTAB, "r", mnttab_file) == NULL) {
7108eda14cbcSMatt Macy 			if (options != NULL)
7109eda14cbcSMatt Macy 				free(options);
7110eda14cbcSMatt Macy 			return (ENOENT);
7111eda14cbcSMatt Macy 		}
7112eda14cbcSMatt Macy 
7113eda14cbcSMatt Macy 		while (getmntent(mnttab_file, &entry) == 0) {
7114eda14cbcSMatt Macy 			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
7115eda14cbcSMatt Macy 			    strchr(entry.mnt_special, '@') != NULL)
7116eda14cbcSMatt Macy 				continue;
7117eda14cbcSMatt Macy 
7118eda14cbcSMatt Macy 			(void) printf("%-30s  %s\n", entry.mnt_special,
7119eda14cbcSMatt Macy 			    entry.mnt_mountp);
7120eda14cbcSMatt Macy 		}
7121eda14cbcSMatt Macy 
7122eda14cbcSMatt Macy 	} else {
7123eda14cbcSMatt Macy 		zfs_handle_t *zhp;
7124eda14cbcSMatt Macy 
7125eda14cbcSMatt Macy 		if (argc > 1) {
7126eda14cbcSMatt Macy 			(void) fprintf(stderr,
7127eda14cbcSMatt Macy 			    gettext("too many arguments\n"));
7128eda14cbcSMatt Macy 			usage(B_FALSE);
7129eda14cbcSMatt Macy 		}
7130eda14cbcSMatt Macy 
7131eda14cbcSMatt Macy 		if ((zhp = zfs_open(g_zfs, argv[0],
7132eda14cbcSMatt Macy 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
7133eda14cbcSMatt Macy 			ret = 1;
7134eda14cbcSMatt Macy 		} else {
7135eda14cbcSMatt Macy 			ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
7136eda14cbcSMatt Macy 			    options);
7137eda14cbcSMatt Macy 			zfs_commit_all_shares();
7138eda14cbcSMatt Macy 			zfs_close(zhp);
7139eda14cbcSMatt Macy 		}
7140eda14cbcSMatt Macy 	}
7141eda14cbcSMatt Macy 
7142eda14cbcSMatt Macy 	if (options != NULL)
7143eda14cbcSMatt Macy 		free(options);
7144eda14cbcSMatt Macy 
7145eda14cbcSMatt Macy 	return (ret);
7146eda14cbcSMatt Macy }
7147eda14cbcSMatt Macy 
7148eda14cbcSMatt Macy /*
7149eda14cbcSMatt Macy  * zfs mount -a [nfs]
7150eda14cbcSMatt Macy  * zfs mount filesystem
7151eda14cbcSMatt Macy  *
7152eda14cbcSMatt Macy  * Mount all filesystems, or mount the given filesystem.
7153eda14cbcSMatt Macy  */
7154eda14cbcSMatt Macy static int
7155eda14cbcSMatt Macy zfs_do_mount(int argc, char **argv)
7156eda14cbcSMatt Macy {
7157eda14cbcSMatt Macy 	return (share_mount(OP_MOUNT, argc, argv));
7158eda14cbcSMatt Macy }
7159eda14cbcSMatt Macy 
7160eda14cbcSMatt Macy /*
7161eda14cbcSMatt Macy  * zfs share -a [nfs | smb]
7162eda14cbcSMatt Macy  * zfs share filesystem
7163eda14cbcSMatt Macy  *
7164eda14cbcSMatt Macy  * Share all filesystems, or share the given filesystem.
7165eda14cbcSMatt Macy  */
7166eda14cbcSMatt Macy static int
7167eda14cbcSMatt Macy zfs_do_share(int argc, char **argv)
7168eda14cbcSMatt Macy {
7169eda14cbcSMatt Macy 	return (share_mount(OP_SHARE, argc, argv));
7170eda14cbcSMatt Macy }
7171eda14cbcSMatt Macy 
7172eda14cbcSMatt Macy typedef struct unshare_unmount_node {
7173eda14cbcSMatt Macy 	zfs_handle_t	*un_zhp;
7174eda14cbcSMatt Macy 	char		*un_mountp;
7175eda14cbcSMatt Macy 	uu_avl_node_t	un_avlnode;
7176eda14cbcSMatt Macy } unshare_unmount_node_t;
7177eda14cbcSMatt Macy 
7178eda14cbcSMatt Macy /* ARGSUSED */
7179eda14cbcSMatt Macy static int
7180eda14cbcSMatt Macy unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
7181eda14cbcSMatt Macy {
7182eda14cbcSMatt Macy 	const unshare_unmount_node_t *l = larg;
7183eda14cbcSMatt Macy 	const unshare_unmount_node_t *r = rarg;
7184eda14cbcSMatt Macy 
7185eda14cbcSMatt Macy 	return (strcmp(l->un_mountp, r->un_mountp));
7186eda14cbcSMatt Macy }
7187eda14cbcSMatt Macy 
7188eda14cbcSMatt Macy /*
7189eda14cbcSMatt Macy  * Convenience routine used by zfs_do_umount() and manual_unmount().  Given an
7190eda14cbcSMatt Macy  * absolute path, find the entry /proc/self/mounts, verify that it's a
7191eda14cbcSMatt Macy  * ZFS filesystem, and unmount it appropriately.
7192eda14cbcSMatt Macy  */
7193eda14cbcSMatt Macy static int
7194eda14cbcSMatt Macy unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
7195eda14cbcSMatt Macy {
7196eda14cbcSMatt Macy 	zfs_handle_t *zhp;
7197eda14cbcSMatt Macy 	int ret = 0;
7198eda14cbcSMatt Macy 	struct stat64 statbuf;
7199eda14cbcSMatt Macy 	struct extmnttab entry;
7200eda14cbcSMatt Macy 	const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
7201eda14cbcSMatt Macy 	ino_t path_inode;
7202eda14cbcSMatt Macy 
7203eda14cbcSMatt Macy 	/*
7204eda14cbcSMatt Macy 	 * Search for the given (major,minor) pair in the mount table.
7205eda14cbcSMatt Macy 	 */
7206eda14cbcSMatt Macy 
7207eda14cbcSMatt Macy 	/* Reopen MNTTAB to prevent reading stale data from open file */
7208eda14cbcSMatt Macy 	if (freopen(MNTTAB, "r", mnttab_file) == NULL)
7209eda14cbcSMatt Macy 		return (ENOENT);
7210eda14cbcSMatt Macy 
7211eda14cbcSMatt Macy 	if (getextmntent(path, &entry, &statbuf) != 0) {
7212eda14cbcSMatt Macy 		if (op == OP_SHARE) {
7213eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("cannot %s '%s': not "
7214eda14cbcSMatt Macy 			    "currently mounted\n"), cmdname, path);
7215eda14cbcSMatt Macy 			return (1);
7216eda14cbcSMatt Macy 		}
7217eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("warning: %s not in"
7218eda14cbcSMatt Macy 		    "/proc/self/mounts\n"), path);
7219eda14cbcSMatt Macy 		if ((ret = umount2(path, flags)) != 0)
7220eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("%s: %s\n"), path,
7221eda14cbcSMatt Macy 			    strerror(errno));
7222eda14cbcSMatt Macy 		return (ret != 0);
7223eda14cbcSMatt Macy 	}
7224eda14cbcSMatt Macy 	path_inode = statbuf.st_ino;
7225eda14cbcSMatt Macy 
7226eda14cbcSMatt Macy 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
7227eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
7228eda14cbcSMatt Macy 		    "filesystem\n"), cmdname, path);
7229eda14cbcSMatt Macy 		return (1);
7230eda14cbcSMatt Macy 	}
7231eda14cbcSMatt Macy 
7232eda14cbcSMatt Macy 	if ((zhp = zfs_open(g_zfs, entry.mnt_special,
7233eda14cbcSMatt Macy 	    ZFS_TYPE_FILESYSTEM)) == NULL)
7234eda14cbcSMatt Macy 		return (1);
7235eda14cbcSMatt Macy 
7236eda14cbcSMatt Macy 	ret = 1;
7237eda14cbcSMatt Macy 	if (stat64(entry.mnt_mountp, &statbuf) != 0) {
7238eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
7239eda14cbcSMatt Macy 		    cmdname, path, strerror(errno));
7240eda14cbcSMatt Macy 		goto out;
7241eda14cbcSMatt Macy 	} else if (statbuf.st_ino != path_inode) {
7242eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("cannot "
7243eda14cbcSMatt Macy 		    "%s '%s': not a mountpoint\n"), cmdname, path);
7244eda14cbcSMatt Macy 		goto out;
7245eda14cbcSMatt Macy 	}
7246eda14cbcSMatt Macy 
7247eda14cbcSMatt Macy 	if (op == OP_SHARE) {
7248eda14cbcSMatt Macy 		char nfs_mnt_prop[ZFS_MAXPROPLEN];
7249eda14cbcSMatt Macy 		char smbshare_prop[ZFS_MAXPROPLEN];
7250eda14cbcSMatt Macy 
7251eda14cbcSMatt Macy 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
7252eda14cbcSMatt Macy 		    sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
7253eda14cbcSMatt Macy 		verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
7254eda14cbcSMatt Macy 		    sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
7255eda14cbcSMatt Macy 
7256eda14cbcSMatt Macy 		if (strcmp(nfs_mnt_prop, "off") == 0 &&
7257eda14cbcSMatt Macy 		    strcmp(smbshare_prop, "off") == 0) {
7258eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("cannot unshare "
7259eda14cbcSMatt Macy 			    "'%s': legacy share\n"), path);
7260eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("use exportfs(8) "
7261eda14cbcSMatt Macy 			    "or smbcontrol(1) to unshare this filesystem\n"));
7262eda14cbcSMatt Macy 		} else if (!zfs_is_shared(zhp)) {
7263eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("cannot unshare '%s': "
7264eda14cbcSMatt Macy 			    "not currently shared\n"), path);
7265eda14cbcSMatt Macy 		} else {
7266eda14cbcSMatt Macy 			ret = zfs_unshareall_bypath(zhp, path);
7267eda14cbcSMatt Macy 			zfs_commit_all_shares();
7268eda14cbcSMatt Macy 		}
7269eda14cbcSMatt Macy 	} else {
7270eda14cbcSMatt Macy 		char mtpt_prop[ZFS_MAXPROPLEN];
7271eda14cbcSMatt Macy 
7272eda14cbcSMatt Macy 		verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
7273eda14cbcSMatt Macy 		    sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
7274eda14cbcSMatt Macy 
7275eda14cbcSMatt Macy 		if (is_manual) {
7276eda14cbcSMatt Macy 			ret = zfs_unmount(zhp, NULL, flags);
7277eda14cbcSMatt Macy 		} else if (strcmp(mtpt_prop, "legacy") == 0) {
7278eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("cannot unmount "
7279eda14cbcSMatt Macy 			    "'%s': legacy mountpoint\n"),
7280eda14cbcSMatt Macy 			    zfs_get_name(zhp));
7281eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("use umount(8) "
7282eda14cbcSMatt Macy 			    "to unmount this filesystem\n"));
7283eda14cbcSMatt Macy 		} else {
7284eda14cbcSMatt Macy 			ret = zfs_unmountall(zhp, flags);
7285eda14cbcSMatt Macy 		}
7286eda14cbcSMatt Macy 	}
7287eda14cbcSMatt Macy 
7288eda14cbcSMatt Macy out:
7289eda14cbcSMatt Macy 	zfs_close(zhp);
7290eda14cbcSMatt Macy 
7291eda14cbcSMatt Macy 	return (ret != 0);
7292eda14cbcSMatt Macy }
7293eda14cbcSMatt Macy 
7294eda14cbcSMatt Macy /*
7295eda14cbcSMatt Macy  * Generic callback for unsharing or unmounting a filesystem.
7296eda14cbcSMatt Macy  */
7297eda14cbcSMatt Macy static int
7298eda14cbcSMatt Macy unshare_unmount(int op, int argc, char **argv)
7299eda14cbcSMatt Macy {
7300eda14cbcSMatt Macy 	int do_all = 0;
7301eda14cbcSMatt Macy 	int flags = 0;
7302eda14cbcSMatt Macy 	int ret = 0;
7303eda14cbcSMatt Macy 	int c;
7304eda14cbcSMatt Macy 	zfs_handle_t *zhp;
7305eda14cbcSMatt Macy 	char nfs_mnt_prop[ZFS_MAXPROPLEN];
7306eda14cbcSMatt Macy 	char sharesmb[ZFS_MAXPROPLEN];
7307eda14cbcSMatt Macy 
7308eda14cbcSMatt Macy 	/* check options */
7309eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, op == OP_SHARE ? ":a" : "afu")) != -1) {
7310eda14cbcSMatt Macy 		switch (c) {
7311eda14cbcSMatt Macy 		case 'a':
7312eda14cbcSMatt Macy 			do_all = 1;
7313eda14cbcSMatt Macy 			break;
7314eda14cbcSMatt Macy 		case 'f':
7315eda14cbcSMatt Macy 			flags |= MS_FORCE;
7316eda14cbcSMatt Macy 			break;
7317eda14cbcSMatt Macy 		case 'u':
7318eda14cbcSMatt Macy 			flags |= MS_CRYPT;
7319eda14cbcSMatt Macy 			break;
7320eda14cbcSMatt Macy 		case ':':
7321eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("missing argument for "
7322eda14cbcSMatt Macy 			    "'%c' option\n"), optopt);
7323eda14cbcSMatt Macy 			usage(B_FALSE);
7324eda14cbcSMatt Macy 			break;
7325eda14cbcSMatt Macy 		case '?':
7326eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
7327eda14cbcSMatt Macy 			    optopt);
7328eda14cbcSMatt Macy 			usage(B_FALSE);
7329eda14cbcSMatt Macy 		}
7330eda14cbcSMatt Macy 	}
7331eda14cbcSMatt Macy 
7332eda14cbcSMatt Macy 	argc -= optind;
7333eda14cbcSMatt Macy 	argv += optind;
7334eda14cbcSMatt Macy 
7335eda14cbcSMatt Macy 	if (do_all) {
7336eda14cbcSMatt Macy 		/*
7337eda14cbcSMatt Macy 		 * We could make use of zfs_for_each() to walk all datasets in
7338eda14cbcSMatt Macy 		 * the system, but this would be very inefficient, especially
7339eda14cbcSMatt Macy 		 * since we would have to linearly search /proc/self/mounts for
7340eda14cbcSMatt Macy 		 * each one. Instead, do one pass through /proc/self/mounts
7341eda14cbcSMatt Macy 		 * looking for zfs entries and call zfs_unmount() for each one.
7342eda14cbcSMatt Macy 		 *
7343eda14cbcSMatt Macy 		 * Things get a little tricky if the administrator has created
7344eda14cbcSMatt Macy 		 * mountpoints beneath other ZFS filesystems.  In this case, we
7345eda14cbcSMatt Macy 		 * have to unmount the deepest filesystems first.  To accomplish
7346eda14cbcSMatt Macy 		 * this, we place all the mountpoints in an AVL tree sorted by
7347eda14cbcSMatt Macy 		 * the special type (dataset name), and walk the result in
7348eda14cbcSMatt Macy 		 * reverse to make sure to get any snapshots first.
7349eda14cbcSMatt Macy 		 */
7350eda14cbcSMatt Macy 		struct mnttab entry;
7351eda14cbcSMatt Macy 		uu_avl_pool_t *pool;
7352eda14cbcSMatt Macy 		uu_avl_t *tree = NULL;
7353eda14cbcSMatt Macy 		unshare_unmount_node_t *node;
7354eda14cbcSMatt Macy 		uu_avl_index_t idx;
7355eda14cbcSMatt Macy 		uu_avl_walk_t *walk;
7356eda14cbcSMatt Macy 		char *protocol = NULL;
7357eda14cbcSMatt Macy 
7358eda14cbcSMatt Macy 		if (op == OP_SHARE && argc > 0) {
7359eda14cbcSMatt Macy 			if (strcmp(argv[0], "nfs") != 0 &&
7360eda14cbcSMatt Macy 			    strcmp(argv[0], "smb") != 0) {
7361eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("share type "
7362eda14cbcSMatt Macy 				    "must be 'nfs' or 'smb'\n"));
7363eda14cbcSMatt Macy 				usage(B_FALSE);
7364eda14cbcSMatt Macy 			}
7365eda14cbcSMatt Macy 			protocol = argv[0];
7366eda14cbcSMatt Macy 			argc--;
7367eda14cbcSMatt Macy 			argv++;
7368eda14cbcSMatt Macy 		}
7369eda14cbcSMatt Macy 
7370eda14cbcSMatt Macy 		if (argc != 0) {
7371eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("too many arguments\n"));
7372eda14cbcSMatt Macy 			usage(B_FALSE);
7373eda14cbcSMatt Macy 		}
7374eda14cbcSMatt Macy 
7375eda14cbcSMatt Macy 		if (((pool = uu_avl_pool_create("unmount_pool",
7376eda14cbcSMatt Macy 		    sizeof (unshare_unmount_node_t),
7377eda14cbcSMatt Macy 		    offsetof(unshare_unmount_node_t, un_avlnode),
7378eda14cbcSMatt Macy 		    unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
7379eda14cbcSMatt Macy 		    ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
7380eda14cbcSMatt Macy 			nomem();
7381eda14cbcSMatt Macy 
7382eda14cbcSMatt Macy 		/* Reopen MNTTAB to prevent reading stale data from open file */
7383eda14cbcSMatt Macy 		if (freopen(MNTTAB, "r", mnttab_file) == NULL)
7384eda14cbcSMatt Macy 			return (ENOENT);
7385eda14cbcSMatt Macy 
7386eda14cbcSMatt Macy 		while (getmntent(mnttab_file, &entry) == 0) {
7387eda14cbcSMatt Macy 
7388eda14cbcSMatt Macy 			/* ignore non-ZFS entries */
7389eda14cbcSMatt Macy 			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
7390eda14cbcSMatt Macy 				continue;
7391eda14cbcSMatt Macy 
7392eda14cbcSMatt Macy 			/* ignore snapshots */
7393eda14cbcSMatt Macy 			if (strchr(entry.mnt_special, '@') != NULL)
7394eda14cbcSMatt Macy 				continue;
7395eda14cbcSMatt Macy 
7396eda14cbcSMatt Macy 			if ((zhp = zfs_open(g_zfs, entry.mnt_special,
7397eda14cbcSMatt Macy 			    ZFS_TYPE_FILESYSTEM)) == NULL) {
7398eda14cbcSMatt Macy 				ret = 1;
7399eda14cbcSMatt Macy 				continue;
7400eda14cbcSMatt Macy 			}
7401eda14cbcSMatt Macy 
7402eda14cbcSMatt Macy 			/*
7403eda14cbcSMatt Macy 			 * Ignore datasets that are excluded/restricted by
7404eda14cbcSMatt Macy 			 * parent pool name.
7405eda14cbcSMatt Macy 			 */
7406eda14cbcSMatt Macy 			if (zpool_skip_pool(zfs_get_pool_name(zhp))) {
7407eda14cbcSMatt Macy 				zfs_close(zhp);
7408eda14cbcSMatt Macy 				continue;
7409eda14cbcSMatt Macy 			}
7410eda14cbcSMatt Macy 
7411eda14cbcSMatt Macy 			switch (op) {
7412eda14cbcSMatt Macy 			case OP_SHARE:
7413eda14cbcSMatt Macy 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
7414eda14cbcSMatt Macy 				    nfs_mnt_prop,
7415eda14cbcSMatt Macy 				    sizeof (nfs_mnt_prop),
7416eda14cbcSMatt Macy 				    NULL, NULL, 0, B_FALSE) == 0);
7417eda14cbcSMatt Macy 				if (strcmp(nfs_mnt_prop, "off") != 0)
7418eda14cbcSMatt Macy 					break;
7419eda14cbcSMatt Macy 				verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
7420eda14cbcSMatt Macy 				    nfs_mnt_prop,
7421eda14cbcSMatt Macy 				    sizeof (nfs_mnt_prop),
7422eda14cbcSMatt Macy 				    NULL, NULL, 0, B_FALSE) == 0);
7423eda14cbcSMatt Macy 				if (strcmp(nfs_mnt_prop, "off") == 0)
7424eda14cbcSMatt Macy 					continue;
7425eda14cbcSMatt Macy 				break;
7426eda14cbcSMatt Macy 			case OP_MOUNT:
7427eda14cbcSMatt Macy 				/* Ignore legacy mounts */
7428eda14cbcSMatt Macy 				verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
7429eda14cbcSMatt Macy 				    nfs_mnt_prop,
7430eda14cbcSMatt Macy 				    sizeof (nfs_mnt_prop),
7431eda14cbcSMatt Macy 				    NULL, NULL, 0, B_FALSE) == 0);
7432eda14cbcSMatt Macy 				if (strcmp(nfs_mnt_prop, "legacy") == 0)
7433eda14cbcSMatt Macy 					continue;
7434eda14cbcSMatt Macy 				/* Ignore canmount=noauto mounts */
7435eda14cbcSMatt Macy 				if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
7436eda14cbcSMatt Macy 				    ZFS_CANMOUNT_NOAUTO)
7437eda14cbcSMatt Macy 					continue;
7438eda14cbcSMatt Macy 			default:
7439eda14cbcSMatt Macy 				break;
7440eda14cbcSMatt Macy 			}
7441eda14cbcSMatt Macy 
7442eda14cbcSMatt Macy 			node = safe_malloc(sizeof (unshare_unmount_node_t));
7443eda14cbcSMatt Macy 			node->un_zhp = zhp;
7444eda14cbcSMatt Macy 			node->un_mountp = safe_strdup(entry.mnt_mountp);
7445eda14cbcSMatt Macy 
7446eda14cbcSMatt Macy 			uu_avl_node_init(node, &node->un_avlnode, pool);
7447eda14cbcSMatt Macy 
7448eda14cbcSMatt Macy 			if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
7449eda14cbcSMatt Macy 				uu_avl_insert(tree, node, idx);
7450eda14cbcSMatt Macy 			} else {
7451eda14cbcSMatt Macy 				zfs_close(node->un_zhp);
7452eda14cbcSMatt Macy 				free(node->un_mountp);
7453eda14cbcSMatt Macy 				free(node);
7454eda14cbcSMatt Macy 			}
7455eda14cbcSMatt Macy 		}
7456eda14cbcSMatt Macy 
7457eda14cbcSMatt Macy 		/*
7458eda14cbcSMatt Macy 		 * Walk the AVL tree in reverse, unmounting each filesystem and
7459eda14cbcSMatt Macy 		 * removing it from the AVL tree in the process.
7460eda14cbcSMatt Macy 		 */
7461eda14cbcSMatt Macy 		if ((walk = uu_avl_walk_start(tree,
7462eda14cbcSMatt Macy 		    UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
7463eda14cbcSMatt Macy 			nomem();
7464eda14cbcSMatt Macy 
7465eda14cbcSMatt Macy 		while ((node = uu_avl_walk_next(walk)) != NULL) {
7466eda14cbcSMatt Macy 			const char *mntarg = NULL;
7467eda14cbcSMatt Macy 
7468eda14cbcSMatt Macy 			uu_avl_remove(tree, node);
7469eda14cbcSMatt Macy 			switch (op) {
7470eda14cbcSMatt Macy 			case OP_SHARE:
7471eda14cbcSMatt Macy 				if (zfs_unshareall_bytype(node->un_zhp,
7472eda14cbcSMatt Macy 				    node->un_mountp, protocol) != 0)
7473eda14cbcSMatt Macy 					ret = 1;
7474eda14cbcSMatt Macy 				break;
7475eda14cbcSMatt Macy 
7476eda14cbcSMatt Macy 			case OP_MOUNT:
7477eda14cbcSMatt Macy 				if (zfs_unmount(node->un_zhp,
7478eda14cbcSMatt Macy 				    mntarg, flags) != 0)
7479eda14cbcSMatt Macy 					ret = 1;
7480eda14cbcSMatt Macy 				break;
7481eda14cbcSMatt Macy 			}
7482eda14cbcSMatt Macy 
7483eda14cbcSMatt Macy 			zfs_close(node->un_zhp);
7484eda14cbcSMatt Macy 			free(node->un_mountp);
7485eda14cbcSMatt Macy 			free(node);
7486eda14cbcSMatt Macy 		}
7487eda14cbcSMatt Macy 
7488eda14cbcSMatt Macy 		if (op == OP_SHARE)
7489eda14cbcSMatt Macy 			zfs_commit_shares(protocol);
7490eda14cbcSMatt Macy 
7491eda14cbcSMatt Macy 		uu_avl_walk_end(walk);
7492eda14cbcSMatt Macy 		uu_avl_destroy(tree);
7493eda14cbcSMatt Macy 		uu_avl_pool_destroy(pool);
7494eda14cbcSMatt Macy 
7495eda14cbcSMatt Macy 	} else {
7496eda14cbcSMatt Macy 		if (argc != 1) {
7497eda14cbcSMatt Macy 			if (argc == 0)
7498eda14cbcSMatt Macy 				(void) fprintf(stderr,
7499eda14cbcSMatt Macy 				    gettext("missing filesystem argument\n"));
7500eda14cbcSMatt Macy 			else
7501eda14cbcSMatt Macy 				(void) fprintf(stderr,
7502eda14cbcSMatt Macy 				    gettext("too many arguments\n"));
7503eda14cbcSMatt Macy 			usage(B_FALSE);
7504eda14cbcSMatt Macy 		}
7505eda14cbcSMatt Macy 
7506eda14cbcSMatt Macy 		/*
7507eda14cbcSMatt Macy 		 * We have an argument, but it may be a full path or a ZFS
7508eda14cbcSMatt Macy 		 * filesystem.  Pass full paths off to unmount_path() (shared by
7509eda14cbcSMatt Macy 		 * manual_unmount), otherwise open the filesystem and pass to
7510eda14cbcSMatt Macy 		 * zfs_unmount().
7511eda14cbcSMatt Macy 		 */
7512eda14cbcSMatt Macy 		if (argv[0][0] == '/')
7513eda14cbcSMatt Macy 			return (unshare_unmount_path(op, argv[0],
7514eda14cbcSMatt Macy 			    flags, B_FALSE));
7515eda14cbcSMatt Macy 
7516eda14cbcSMatt Macy 		if ((zhp = zfs_open(g_zfs, argv[0],
7517eda14cbcSMatt Macy 		    ZFS_TYPE_FILESYSTEM)) == NULL)
7518eda14cbcSMatt Macy 			return (1);
7519eda14cbcSMatt Macy 
7520eda14cbcSMatt Macy 		verify(zfs_prop_get(zhp, op == OP_SHARE ?
7521eda14cbcSMatt Macy 		    ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
7522eda14cbcSMatt Macy 		    nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
7523eda14cbcSMatt Macy 		    NULL, 0, B_FALSE) == 0);
7524eda14cbcSMatt Macy 
7525eda14cbcSMatt Macy 		switch (op) {
7526eda14cbcSMatt Macy 		case OP_SHARE:
7527eda14cbcSMatt Macy 			verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
7528eda14cbcSMatt Macy 			    nfs_mnt_prop,
7529eda14cbcSMatt Macy 			    sizeof (nfs_mnt_prop),
7530eda14cbcSMatt Macy 			    NULL, NULL, 0, B_FALSE) == 0);
7531eda14cbcSMatt Macy 			verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
7532eda14cbcSMatt Macy 			    sharesmb, sizeof (sharesmb), NULL, NULL,
7533eda14cbcSMatt Macy 			    0, B_FALSE) == 0);
7534eda14cbcSMatt Macy 
7535eda14cbcSMatt Macy 			if (strcmp(nfs_mnt_prop, "off") == 0 &&
7536eda14cbcSMatt Macy 			    strcmp(sharesmb, "off") == 0) {
7537eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("cannot "
7538eda14cbcSMatt Macy 				    "unshare '%s': legacy share\n"),
7539eda14cbcSMatt Macy 				    zfs_get_name(zhp));
7540eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("use "
7541*7877fdebSMatt Macy 				    "exports(5) or smb.conf(5) to unshare "
7542*7877fdebSMatt Macy 				    "this filesystem\n"));
7543eda14cbcSMatt Macy 				ret = 1;
7544eda14cbcSMatt Macy 			} else if (!zfs_is_shared(zhp)) {
7545eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("cannot "
7546eda14cbcSMatt Macy 				    "unshare '%s': not currently "
7547eda14cbcSMatt Macy 				    "shared\n"), zfs_get_name(zhp));
7548eda14cbcSMatt Macy 				ret = 1;
7549eda14cbcSMatt Macy 			} else if (zfs_unshareall(zhp) != 0) {
7550eda14cbcSMatt Macy 				ret = 1;
7551eda14cbcSMatt Macy 			}
7552eda14cbcSMatt Macy 			break;
7553eda14cbcSMatt Macy 
7554eda14cbcSMatt Macy 		case OP_MOUNT:
7555eda14cbcSMatt Macy 			if (strcmp(nfs_mnt_prop, "legacy") == 0) {
7556eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("cannot "
7557eda14cbcSMatt Macy 				    "unmount '%s': legacy "
7558eda14cbcSMatt Macy 				    "mountpoint\n"), zfs_get_name(zhp));
7559eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("use "
7560*7877fdebSMatt Macy 				    "umount(8) to unmount this "
7561eda14cbcSMatt Macy 				    "filesystem\n"));
7562eda14cbcSMatt Macy 				ret = 1;
7563eda14cbcSMatt Macy 			} else if (!zfs_is_mounted(zhp, NULL)) {
7564eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("cannot "
7565eda14cbcSMatt Macy 				    "unmount '%s': not currently "
7566eda14cbcSMatt Macy 				    "mounted\n"),
7567eda14cbcSMatt Macy 				    zfs_get_name(zhp));
7568eda14cbcSMatt Macy 				ret = 1;
7569eda14cbcSMatt Macy 			} else if (zfs_unmountall(zhp, flags) != 0) {
7570eda14cbcSMatt Macy 				ret = 1;
7571eda14cbcSMatt Macy 			}
7572eda14cbcSMatt Macy 			break;
7573eda14cbcSMatt Macy 		}
7574eda14cbcSMatt Macy 
7575eda14cbcSMatt Macy 		zfs_close(zhp);
7576eda14cbcSMatt Macy 	}
7577eda14cbcSMatt Macy 
7578eda14cbcSMatt Macy 	return (ret);
7579eda14cbcSMatt Macy }
7580eda14cbcSMatt Macy 
7581eda14cbcSMatt Macy /*
7582eda14cbcSMatt Macy  * zfs unmount [-fu] -a
7583eda14cbcSMatt Macy  * zfs unmount [-fu] filesystem
7584eda14cbcSMatt Macy  *
7585eda14cbcSMatt Macy  * Unmount all filesystems, or a specific ZFS filesystem.
7586eda14cbcSMatt Macy  */
7587eda14cbcSMatt Macy static int
7588eda14cbcSMatt Macy zfs_do_unmount(int argc, char **argv)
7589eda14cbcSMatt Macy {
7590eda14cbcSMatt Macy 	return (unshare_unmount(OP_MOUNT, argc, argv));
7591eda14cbcSMatt Macy }
7592eda14cbcSMatt Macy 
7593eda14cbcSMatt Macy /*
7594eda14cbcSMatt Macy  * zfs unshare -a
7595eda14cbcSMatt Macy  * zfs unshare filesystem
7596eda14cbcSMatt Macy  *
7597eda14cbcSMatt Macy  * Unshare all filesystems, or a specific ZFS filesystem.
7598eda14cbcSMatt Macy  */
7599eda14cbcSMatt Macy static int
7600eda14cbcSMatt Macy zfs_do_unshare(int argc, char **argv)
7601eda14cbcSMatt Macy {
7602eda14cbcSMatt Macy 	return (unshare_unmount(OP_SHARE, argc, argv));
7603eda14cbcSMatt Macy }
7604eda14cbcSMatt Macy 
7605eda14cbcSMatt Macy static int
7606eda14cbcSMatt Macy find_command_idx(char *command, int *idx)
7607eda14cbcSMatt Macy {
7608eda14cbcSMatt Macy 	int i;
7609eda14cbcSMatt Macy 
7610eda14cbcSMatt Macy 	for (i = 0; i < NCOMMAND; i++) {
7611eda14cbcSMatt Macy 		if (command_table[i].name == NULL)
7612eda14cbcSMatt Macy 			continue;
7613eda14cbcSMatt Macy 
7614eda14cbcSMatt Macy 		if (strcmp(command, command_table[i].name) == 0) {
7615eda14cbcSMatt Macy 			*idx = i;
7616eda14cbcSMatt Macy 			return (0);
7617eda14cbcSMatt Macy 		}
7618eda14cbcSMatt Macy 	}
7619eda14cbcSMatt Macy 	return (1);
7620eda14cbcSMatt Macy }
7621eda14cbcSMatt Macy 
7622eda14cbcSMatt Macy static int
7623eda14cbcSMatt Macy zfs_do_diff(int argc, char **argv)
7624eda14cbcSMatt Macy {
7625eda14cbcSMatt Macy 	zfs_handle_t *zhp;
7626eda14cbcSMatt Macy 	int flags = 0;
7627eda14cbcSMatt Macy 	char *tosnap = NULL;
7628eda14cbcSMatt Macy 	char *fromsnap = NULL;
7629eda14cbcSMatt Macy 	char *atp, *copy;
7630eda14cbcSMatt Macy 	int err = 0;
7631eda14cbcSMatt Macy 	int c;
7632eda14cbcSMatt Macy 	struct sigaction sa;
7633eda14cbcSMatt Macy 
7634eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, "FHt")) != -1) {
7635eda14cbcSMatt Macy 		switch (c) {
7636eda14cbcSMatt Macy 		case 'F':
7637eda14cbcSMatt Macy 			flags |= ZFS_DIFF_CLASSIFY;
7638eda14cbcSMatt Macy 			break;
7639eda14cbcSMatt Macy 		case 'H':
7640eda14cbcSMatt Macy 			flags |= ZFS_DIFF_PARSEABLE;
7641eda14cbcSMatt Macy 			break;
7642eda14cbcSMatt Macy 		case 't':
7643eda14cbcSMatt Macy 			flags |= ZFS_DIFF_TIMESTAMP;
7644eda14cbcSMatt Macy 			break;
7645eda14cbcSMatt Macy 		default:
7646eda14cbcSMatt Macy 			(void) fprintf(stderr,
7647eda14cbcSMatt Macy 			    gettext("invalid option '%c'\n"), optopt);
7648eda14cbcSMatt Macy 			usage(B_FALSE);
7649eda14cbcSMatt Macy 		}
7650eda14cbcSMatt Macy 	}
7651eda14cbcSMatt Macy 
7652eda14cbcSMatt Macy 	argc -= optind;
7653eda14cbcSMatt Macy 	argv += optind;
7654eda14cbcSMatt Macy 
7655eda14cbcSMatt Macy 	if (argc < 1) {
7656eda14cbcSMatt Macy 		(void) fprintf(stderr,
7657eda14cbcSMatt Macy 		    gettext("must provide at least one snapshot name\n"));
7658eda14cbcSMatt Macy 		usage(B_FALSE);
7659eda14cbcSMatt Macy 	}
7660eda14cbcSMatt Macy 
7661eda14cbcSMatt Macy 	if (argc > 2) {
7662eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("too many arguments\n"));
7663eda14cbcSMatt Macy 		usage(B_FALSE);
7664eda14cbcSMatt Macy 	}
7665eda14cbcSMatt Macy 
7666eda14cbcSMatt Macy 	fromsnap = argv[0];
7667eda14cbcSMatt Macy 	tosnap = (argc == 2) ? argv[1] : NULL;
7668eda14cbcSMatt Macy 
7669eda14cbcSMatt Macy 	copy = NULL;
7670eda14cbcSMatt Macy 	if (*fromsnap != '@')
7671eda14cbcSMatt Macy 		copy = strdup(fromsnap);
7672eda14cbcSMatt Macy 	else if (tosnap)
7673eda14cbcSMatt Macy 		copy = strdup(tosnap);
7674eda14cbcSMatt Macy 	if (copy == NULL)
7675eda14cbcSMatt Macy 		usage(B_FALSE);
7676eda14cbcSMatt Macy 
7677eda14cbcSMatt Macy 	if ((atp = strchr(copy, '@')) != NULL)
7678eda14cbcSMatt Macy 		*atp = '\0';
7679eda14cbcSMatt Macy 
7680eda14cbcSMatt Macy 	if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL) {
7681eda14cbcSMatt Macy 		free(copy);
7682eda14cbcSMatt Macy 		return (1);
7683eda14cbcSMatt Macy 	}
7684eda14cbcSMatt Macy 	free(copy);
7685eda14cbcSMatt Macy 
7686eda14cbcSMatt Macy 	/*
7687eda14cbcSMatt Macy 	 * Ignore SIGPIPE so that the library can give us
7688eda14cbcSMatt Macy 	 * information on any failure
7689eda14cbcSMatt Macy 	 */
7690eda14cbcSMatt Macy 	if (sigemptyset(&sa.sa_mask) == -1) {
7691eda14cbcSMatt Macy 		err = errno;
7692eda14cbcSMatt Macy 		goto out;
7693eda14cbcSMatt Macy 	}
7694eda14cbcSMatt Macy 	sa.sa_flags = 0;
7695eda14cbcSMatt Macy 	sa.sa_handler = SIG_IGN;
7696eda14cbcSMatt Macy 	if (sigaction(SIGPIPE, &sa, NULL) == -1) {
7697eda14cbcSMatt Macy 		err = errno;
7698eda14cbcSMatt Macy 		goto out;
7699eda14cbcSMatt Macy 	}
7700eda14cbcSMatt Macy 
7701eda14cbcSMatt Macy 	err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
7702eda14cbcSMatt Macy out:
7703eda14cbcSMatt Macy 	zfs_close(zhp);
7704eda14cbcSMatt Macy 
7705eda14cbcSMatt Macy 	return (err != 0);
7706eda14cbcSMatt Macy }
7707eda14cbcSMatt Macy 
7708eda14cbcSMatt Macy /*
7709eda14cbcSMatt Macy  * zfs bookmark <fs@source>|<fs#source> <fs#bookmark>
7710eda14cbcSMatt Macy  *
7711eda14cbcSMatt Macy  * Creates a bookmark with the given name from the source snapshot
7712eda14cbcSMatt Macy  * or creates a copy of an existing source bookmark.
7713eda14cbcSMatt Macy  */
7714eda14cbcSMatt Macy static int
7715eda14cbcSMatt Macy zfs_do_bookmark(int argc, char **argv)
7716eda14cbcSMatt Macy {
7717eda14cbcSMatt Macy 	char *source, *bookname;
7718eda14cbcSMatt Macy 	char expbuf[ZFS_MAX_DATASET_NAME_LEN];
7719eda14cbcSMatt Macy 	int source_type;
7720eda14cbcSMatt Macy 	nvlist_t *nvl;
7721eda14cbcSMatt Macy 	int ret = 0;
7722eda14cbcSMatt Macy 	int c;
7723eda14cbcSMatt Macy 
7724eda14cbcSMatt Macy 	/* check options */
7725eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, "")) != -1) {
7726eda14cbcSMatt Macy 		switch (c) {
7727eda14cbcSMatt Macy 		case '?':
7728eda14cbcSMatt Macy 			(void) fprintf(stderr,
7729eda14cbcSMatt Macy 			    gettext("invalid option '%c'\n"), optopt);
7730eda14cbcSMatt Macy 			goto usage;
7731eda14cbcSMatt Macy 		}
7732eda14cbcSMatt Macy 	}
7733eda14cbcSMatt Macy 
7734eda14cbcSMatt Macy 	argc -= optind;
7735eda14cbcSMatt Macy 	argv += optind;
7736eda14cbcSMatt Macy 
7737eda14cbcSMatt Macy 	/* check number of arguments */
7738eda14cbcSMatt Macy 	if (argc < 1) {
7739eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing source argument\n"));
7740eda14cbcSMatt Macy 		goto usage;
7741eda14cbcSMatt Macy 	}
7742eda14cbcSMatt Macy 	if (argc < 2) {
7743eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing bookmark argument\n"));
7744eda14cbcSMatt Macy 		goto usage;
7745eda14cbcSMatt Macy 	}
7746eda14cbcSMatt Macy 
7747eda14cbcSMatt Macy 	source = argv[0];
7748eda14cbcSMatt Macy 	bookname = argv[1];
7749eda14cbcSMatt Macy 
7750eda14cbcSMatt Macy 	if (strchr(source, '@') == NULL && strchr(source, '#') == NULL) {
7751eda14cbcSMatt Macy 		(void) fprintf(stderr,
7752eda14cbcSMatt Macy 		    gettext("invalid source name '%s': "
7753eda14cbcSMatt Macy 		    "must contain a '@' or '#'\n"), source);
7754eda14cbcSMatt Macy 		goto usage;
7755eda14cbcSMatt Macy 	}
7756eda14cbcSMatt Macy 	if (strchr(bookname, '#') == NULL) {
7757eda14cbcSMatt Macy 		(void) fprintf(stderr,
7758eda14cbcSMatt Macy 		    gettext("invalid bookmark name '%s': "
7759eda14cbcSMatt Macy 		    "must contain a '#'\n"), bookname);
7760eda14cbcSMatt Macy 		goto usage;
7761eda14cbcSMatt Macy 	}
7762eda14cbcSMatt Macy 
7763eda14cbcSMatt Macy 	/*
7764eda14cbcSMatt Macy 	 * expand source or bookname to full path:
7765eda14cbcSMatt Macy 	 * one of them may be specified as short name
7766eda14cbcSMatt Macy 	 */
7767eda14cbcSMatt Macy 	{
7768eda14cbcSMatt Macy 		char **expand;
7769eda14cbcSMatt Macy 		char *source_short, *bookname_short;
7770eda14cbcSMatt Macy 		source_short = strpbrk(source, "@#");
7771eda14cbcSMatt Macy 		bookname_short = strpbrk(bookname, "#");
7772eda14cbcSMatt Macy 		if (source_short == source &&
7773eda14cbcSMatt Macy 		    bookname_short == bookname) {
7774eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext(
7775eda14cbcSMatt Macy 			    "either source or bookmark must be specified as "
7776eda14cbcSMatt Macy 			    "full dataset paths"));
7777eda14cbcSMatt Macy 			goto usage;
7778eda14cbcSMatt Macy 		} else if (source_short != source &&
7779eda14cbcSMatt Macy 		    bookname_short != bookname) {
7780eda14cbcSMatt Macy 			expand = NULL;
7781eda14cbcSMatt Macy 		} else if (source_short != source) {
7782eda14cbcSMatt Macy 			strlcpy(expbuf, source, sizeof (expbuf));
7783eda14cbcSMatt Macy 			expand = &bookname;
7784eda14cbcSMatt Macy 		} else if (bookname_short != bookname) {
7785eda14cbcSMatt Macy 			strlcpy(expbuf, bookname, sizeof (expbuf));
7786eda14cbcSMatt Macy 			expand = &source;
7787eda14cbcSMatt Macy 		} else {
7788eda14cbcSMatt Macy 			abort();
7789eda14cbcSMatt Macy 		}
7790eda14cbcSMatt Macy 		if (expand != NULL) {
7791eda14cbcSMatt Macy 			*strpbrk(expbuf, "@#") = '\0'; /* dataset name in buf */
7792eda14cbcSMatt Macy 			(void) strlcat(expbuf, *expand, sizeof (expbuf));
7793eda14cbcSMatt Macy 			*expand = expbuf;
7794eda14cbcSMatt Macy 		}
7795eda14cbcSMatt Macy 	}
7796eda14cbcSMatt Macy 
7797eda14cbcSMatt Macy 	/* determine source type */
7798eda14cbcSMatt Macy 	switch (*strpbrk(source, "@#")) {
7799eda14cbcSMatt Macy 		case '@': source_type = ZFS_TYPE_SNAPSHOT; break;
7800eda14cbcSMatt Macy 		case '#': source_type = ZFS_TYPE_BOOKMARK; break;
7801eda14cbcSMatt Macy 		default: abort();
7802eda14cbcSMatt Macy 	}
7803eda14cbcSMatt Macy 
7804eda14cbcSMatt Macy 	/* test the source exists */
7805eda14cbcSMatt Macy 	zfs_handle_t *zhp;
7806eda14cbcSMatt Macy 	zhp = zfs_open(g_zfs, source, source_type);
7807eda14cbcSMatt Macy 	if (zhp == NULL)
7808eda14cbcSMatt Macy 		goto usage;
7809eda14cbcSMatt Macy 	zfs_close(zhp);
7810eda14cbcSMatt Macy 
7811eda14cbcSMatt Macy 	nvl = fnvlist_alloc();
7812eda14cbcSMatt Macy 	fnvlist_add_string(nvl, bookname, source);
7813eda14cbcSMatt Macy 	ret = lzc_bookmark(nvl, NULL);
7814eda14cbcSMatt Macy 	fnvlist_free(nvl);
7815eda14cbcSMatt Macy 
7816eda14cbcSMatt Macy 	if (ret != 0) {
7817eda14cbcSMatt Macy 		const char *err_msg = NULL;
7818eda14cbcSMatt Macy 		char errbuf[1024];
7819eda14cbcSMatt Macy 
7820eda14cbcSMatt Macy 		(void) snprintf(errbuf, sizeof (errbuf),
7821eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN,
7822eda14cbcSMatt Macy 		    "cannot create bookmark '%s'"), bookname);
7823eda14cbcSMatt Macy 
7824eda14cbcSMatt Macy 		switch (ret) {
7825eda14cbcSMatt Macy 		case EXDEV:
7826eda14cbcSMatt Macy 			err_msg = "bookmark is in a different pool";
7827eda14cbcSMatt Macy 			break;
7828eda14cbcSMatt Macy 		case ZFS_ERR_BOOKMARK_SOURCE_NOT_ANCESTOR:
7829eda14cbcSMatt Macy 			err_msg = "source is not an ancestor of the "
7830eda14cbcSMatt Macy 			    "new bookmark's dataset";
7831eda14cbcSMatt Macy 			break;
7832eda14cbcSMatt Macy 		case EEXIST:
7833eda14cbcSMatt Macy 			err_msg = "bookmark exists";
7834eda14cbcSMatt Macy 			break;
7835eda14cbcSMatt Macy 		case EINVAL:
7836eda14cbcSMatt Macy 			err_msg = "invalid argument";
7837eda14cbcSMatt Macy 			break;
7838eda14cbcSMatt Macy 		case ENOTSUP:
7839eda14cbcSMatt Macy 			err_msg = "bookmark feature not enabled";
7840eda14cbcSMatt Macy 			break;
7841eda14cbcSMatt Macy 		case ENOSPC:
7842eda14cbcSMatt Macy 			err_msg = "out of space";
7843eda14cbcSMatt Macy 			break;
7844eda14cbcSMatt Macy 		case ENOENT:
7845eda14cbcSMatt Macy 			err_msg = "dataset does not exist";
7846eda14cbcSMatt Macy 			break;
7847eda14cbcSMatt Macy 		default:
7848eda14cbcSMatt Macy 			(void) zfs_standard_error(g_zfs, ret, errbuf);
7849eda14cbcSMatt Macy 			break;
7850eda14cbcSMatt Macy 		}
7851eda14cbcSMatt Macy 		if (err_msg != NULL) {
7852eda14cbcSMatt Macy 			(void) fprintf(stderr, "%s: %s\n", errbuf,
7853eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, err_msg));
7854eda14cbcSMatt Macy 		}
7855eda14cbcSMatt Macy 	}
7856eda14cbcSMatt Macy 
7857eda14cbcSMatt Macy 	return (ret != 0);
7858eda14cbcSMatt Macy 
7859eda14cbcSMatt Macy usage:
7860eda14cbcSMatt Macy 	usage(B_FALSE);
7861eda14cbcSMatt Macy 	return (-1);
7862eda14cbcSMatt Macy }
7863eda14cbcSMatt Macy 
7864eda14cbcSMatt Macy static int
7865eda14cbcSMatt Macy zfs_do_channel_program(int argc, char **argv)
7866eda14cbcSMatt Macy {
7867eda14cbcSMatt Macy 	int ret, fd, c;
7868eda14cbcSMatt Macy 	char *progbuf, *filename, *poolname;
7869eda14cbcSMatt Macy 	size_t progsize, progread;
7870eda14cbcSMatt Macy 	nvlist_t *outnvl = NULL;
7871eda14cbcSMatt Macy 	uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT;
7872eda14cbcSMatt Macy 	uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT;
7873eda14cbcSMatt Macy 	boolean_t sync_flag = B_TRUE, json_output = B_FALSE;
7874eda14cbcSMatt Macy 	zpool_handle_t *zhp;
7875eda14cbcSMatt Macy 
7876eda14cbcSMatt Macy 	/* check options */
7877eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, "nt:m:j")) != -1) {
7878eda14cbcSMatt Macy 		switch (c) {
7879eda14cbcSMatt Macy 		case 't':
7880eda14cbcSMatt Macy 		case 'm': {
7881eda14cbcSMatt Macy 			uint64_t arg;
7882eda14cbcSMatt Macy 			char *endp;
7883eda14cbcSMatt Macy 
7884eda14cbcSMatt Macy 			errno = 0;
7885eda14cbcSMatt Macy 			arg = strtoull(optarg, &endp, 0);
7886eda14cbcSMatt Macy 			if (errno != 0 || *endp != '\0') {
7887eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext(
7888eda14cbcSMatt Macy 				    "invalid argument "
7889eda14cbcSMatt Macy 				    "'%s': expected integer\n"), optarg);
7890eda14cbcSMatt Macy 				goto usage;
7891eda14cbcSMatt Macy 			}
7892eda14cbcSMatt Macy 
7893eda14cbcSMatt Macy 			if (c == 't') {
7894eda14cbcSMatt Macy 				instrlimit = arg;
7895eda14cbcSMatt Macy 			} else {
7896eda14cbcSMatt Macy 				ASSERT3U(c, ==, 'm');
7897eda14cbcSMatt Macy 				memlimit = arg;
7898eda14cbcSMatt Macy 			}
7899eda14cbcSMatt Macy 			break;
7900eda14cbcSMatt Macy 		}
7901eda14cbcSMatt Macy 		case 'n': {
7902eda14cbcSMatt Macy 			sync_flag = B_FALSE;
7903eda14cbcSMatt Macy 			break;
7904eda14cbcSMatt Macy 		}
7905eda14cbcSMatt Macy 		case 'j': {
7906eda14cbcSMatt Macy 			json_output = B_TRUE;
7907eda14cbcSMatt Macy 			break;
7908eda14cbcSMatt Macy 		}
7909eda14cbcSMatt Macy 		case '?':
7910eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
7911eda14cbcSMatt Macy 			    optopt);
7912eda14cbcSMatt Macy 			goto usage;
7913eda14cbcSMatt Macy 		}
7914eda14cbcSMatt Macy 	}
7915eda14cbcSMatt Macy 
7916eda14cbcSMatt Macy 	argc -= optind;
7917eda14cbcSMatt Macy 	argv += optind;
7918eda14cbcSMatt Macy 
7919eda14cbcSMatt Macy 	if (argc < 2) {
7920eda14cbcSMatt Macy 		(void) fprintf(stderr,
7921eda14cbcSMatt Macy 		    gettext("invalid number of arguments\n"));
7922eda14cbcSMatt Macy 		goto usage;
7923eda14cbcSMatt Macy 	}
7924eda14cbcSMatt Macy 
7925eda14cbcSMatt Macy 	poolname = argv[0];
7926eda14cbcSMatt Macy 	filename = argv[1];
7927eda14cbcSMatt Macy 	if (strcmp(filename, "-") == 0) {
7928eda14cbcSMatt Macy 		fd = 0;
7929eda14cbcSMatt Macy 		filename = "standard input";
7930eda14cbcSMatt Macy 	} else if ((fd = open(filename, O_RDONLY)) < 0) {
7931eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
7932eda14cbcSMatt Macy 		    filename, strerror(errno));
7933eda14cbcSMatt Macy 		return (1);
7934eda14cbcSMatt Macy 	}
7935eda14cbcSMatt Macy 
7936eda14cbcSMatt Macy 	if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
7937eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("cannot open pool '%s'\n"),
7938eda14cbcSMatt Macy 		    poolname);
7939eda14cbcSMatt Macy 		if (fd != 0)
7940eda14cbcSMatt Macy 			(void) close(fd);
7941eda14cbcSMatt Macy 		return (1);
7942eda14cbcSMatt Macy 	}
7943eda14cbcSMatt Macy 	zpool_close(zhp);
7944eda14cbcSMatt Macy 
7945eda14cbcSMatt Macy 	/*
7946eda14cbcSMatt Macy 	 * Read in the channel program, expanding the program buffer as
7947eda14cbcSMatt Macy 	 * necessary.
7948eda14cbcSMatt Macy 	 */
7949eda14cbcSMatt Macy 	progread = 0;
7950eda14cbcSMatt Macy 	progsize = 1024;
7951eda14cbcSMatt Macy 	progbuf = safe_malloc(progsize);
7952eda14cbcSMatt Macy 	do {
7953eda14cbcSMatt Macy 		ret = read(fd, progbuf + progread, progsize - progread);
7954eda14cbcSMatt Macy 		progread += ret;
7955eda14cbcSMatt Macy 		if (progread == progsize && ret > 0) {
7956eda14cbcSMatt Macy 			progsize *= 2;
7957eda14cbcSMatt Macy 			progbuf = safe_realloc(progbuf, progsize);
7958eda14cbcSMatt Macy 		}
7959eda14cbcSMatt Macy 	} while (ret > 0);
7960eda14cbcSMatt Macy 
7961eda14cbcSMatt Macy 	if (fd != 0)
7962eda14cbcSMatt Macy 		(void) close(fd);
7963eda14cbcSMatt Macy 	if (ret < 0) {
7964eda14cbcSMatt Macy 		free(progbuf);
7965eda14cbcSMatt Macy 		(void) fprintf(stderr,
7966eda14cbcSMatt Macy 		    gettext("cannot read '%s': %s\n"),
7967eda14cbcSMatt Macy 		    filename, strerror(errno));
7968eda14cbcSMatt Macy 		return (1);
7969eda14cbcSMatt Macy 	}
7970eda14cbcSMatt Macy 	progbuf[progread] = '\0';
7971eda14cbcSMatt Macy 
7972eda14cbcSMatt Macy 	/*
7973eda14cbcSMatt Macy 	 * Any remaining arguments are passed as arguments to the lua script as
7974eda14cbcSMatt Macy 	 * a string array:
7975eda14cbcSMatt Macy 	 * {
7976eda14cbcSMatt Macy 	 *	"argv" -> [ "arg 1", ... "arg n" ],
7977eda14cbcSMatt Macy 	 * }
7978eda14cbcSMatt Macy 	 */
7979eda14cbcSMatt Macy 	nvlist_t *argnvl = fnvlist_alloc();
7980eda14cbcSMatt Macy 	fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV, argv + 2, argc - 2);
7981eda14cbcSMatt Macy 
7982eda14cbcSMatt Macy 	if (sync_flag) {
7983eda14cbcSMatt Macy 		ret = lzc_channel_program(poolname, progbuf,
7984eda14cbcSMatt Macy 		    instrlimit, memlimit, argnvl, &outnvl);
7985eda14cbcSMatt Macy 	} else {
7986eda14cbcSMatt Macy 		ret = lzc_channel_program_nosync(poolname, progbuf,
7987eda14cbcSMatt Macy 		    instrlimit, memlimit, argnvl, &outnvl);
7988eda14cbcSMatt Macy 	}
7989eda14cbcSMatt Macy 
7990eda14cbcSMatt Macy 	if (ret != 0) {
7991eda14cbcSMatt Macy 		/*
7992eda14cbcSMatt Macy 		 * On error, report the error message handed back by lua if one
7993eda14cbcSMatt Macy 		 * exists.  Otherwise, generate an appropriate error message,
7994eda14cbcSMatt Macy 		 * falling back on strerror() for an unexpected return code.
7995eda14cbcSMatt Macy 		 */
7996eda14cbcSMatt Macy 		char *errstring = NULL;
7997eda14cbcSMatt Macy 		const char *msg = gettext("Channel program execution failed");
7998eda14cbcSMatt Macy 		uint64_t instructions = 0;
7999eda14cbcSMatt Macy 		if (outnvl != NULL && nvlist_exists(outnvl, ZCP_RET_ERROR)) {
8000eda14cbcSMatt Macy 			(void) nvlist_lookup_string(outnvl,
8001eda14cbcSMatt Macy 			    ZCP_RET_ERROR, &errstring);
8002eda14cbcSMatt Macy 			if (errstring == NULL)
8003eda14cbcSMatt Macy 				errstring = strerror(ret);
8004eda14cbcSMatt Macy 			if (ret == ETIME) {
8005eda14cbcSMatt Macy 				(void) nvlist_lookup_uint64(outnvl,
8006eda14cbcSMatt Macy 				    ZCP_ARG_INSTRLIMIT, &instructions);
8007eda14cbcSMatt Macy 			}
8008eda14cbcSMatt Macy 		} else {
8009eda14cbcSMatt Macy 			switch (ret) {
8010eda14cbcSMatt Macy 			case EINVAL:
8011eda14cbcSMatt Macy 				errstring =
8012eda14cbcSMatt Macy 				    "Invalid instruction or memory limit.";
8013eda14cbcSMatt Macy 				break;
8014eda14cbcSMatt Macy 			case ENOMEM:
8015eda14cbcSMatt Macy 				errstring = "Return value too large.";
8016eda14cbcSMatt Macy 				break;
8017eda14cbcSMatt Macy 			case ENOSPC:
8018eda14cbcSMatt Macy 				errstring = "Memory limit exhausted.";
8019eda14cbcSMatt Macy 				break;
8020eda14cbcSMatt Macy 			case ETIME:
8021eda14cbcSMatt Macy 				errstring = "Timed out.";
8022eda14cbcSMatt Macy 				break;
8023eda14cbcSMatt Macy 			case EPERM:
8024eda14cbcSMatt Macy 				errstring = "Permission denied. Channel "
8025eda14cbcSMatt Macy 				    "programs must be run as root.";
8026eda14cbcSMatt Macy 				break;
8027eda14cbcSMatt Macy 			default:
8028eda14cbcSMatt Macy 				(void) zfs_standard_error(g_zfs, ret, msg);
8029eda14cbcSMatt Macy 			}
8030eda14cbcSMatt Macy 		}
8031eda14cbcSMatt Macy 		if (errstring != NULL)
8032eda14cbcSMatt Macy 			(void) fprintf(stderr, "%s:\n%s\n", msg, errstring);
8033eda14cbcSMatt Macy 
8034eda14cbcSMatt Macy 		if (ret == ETIME && instructions != 0)
8035eda14cbcSMatt Macy 			(void) fprintf(stderr,
8036eda14cbcSMatt Macy 			    gettext("%llu Lua instructions\n"),
8037eda14cbcSMatt Macy 			    (u_longlong_t)instructions);
8038eda14cbcSMatt Macy 	} else {
8039eda14cbcSMatt Macy 		if (json_output) {
8040eda14cbcSMatt Macy 			(void) nvlist_print_json(stdout, outnvl);
8041eda14cbcSMatt Macy 		} else if (nvlist_empty(outnvl)) {
8042eda14cbcSMatt Macy 			(void) fprintf(stdout, gettext("Channel program fully "
8043eda14cbcSMatt Macy 			    "executed and did not produce output.\n"));
8044eda14cbcSMatt Macy 		} else {
8045eda14cbcSMatt Macy 			(void) fprintf(stdout, gettext("Channel program fully "
8046eda14cbcSMatt Macy 			    "executed and produced output:\n"));
8047eda14cbcSMatt Macy 			dump_nvlist(outnvl, 4);
8048eda14cbcSMatt Macy 		}
8049eda14cbcSMatt Macy 	}
8050eda14cbcSMatt Macy 
8051eda14cbcSMatt Macy 	free(progbuf);
8052eda14cbcSMatt Macy 	fnvlist_free(outnvl);
8053eda14cbcSMatt Macy 	fnvlist_free(argnvl);
8054eda14cbcSMatt Macy 	return (ret != 0);
8055eda14cbcSMatt Macy 
8056eda14cbcSMatt Macy usage:
8057eda14cbcSMatt Macy 	usage(B_FALSE);
8058eda14cbcSMatt Macy 	return (-1);
8059eda14cbcSMatt Macy }
8060eda14cbcSMatt Macy 
8061eda14cbcSMatt Macy 
8062eda14cbcSMatt Macy typedef struct loadkey_cbdata {
8063eda14cbcSMatt Macy 	boolean_t cb_loadkey;
8064eda14cbcSMatt Macy 	boolean_t cb_recursive;
8065eda14cbcSMatt Macy 	boolean_t cb_noop;
8066eda14cbcSMatt Macy 	char *cb_keylocation;
8067eda14cbcSMatt Macy 	uint64_t cb_numfailed;
8068eda14cbcSMatt Macy 	uint64_t cb_numattempted;
8069eda14cbcSMatt Macy } loadkey_cbdata_t;
8070eda14cbcSMatt Macy 
8071eda14cbcSMatt Macy static int
8072eda14cbcSMatt Macy load_key_callback(zfs_handle_t *zhp, void *data)
8073eda14cbcSMatt Macy {
8074eda14cbcSMatt Macy 	int ret;
8075eda14cbcSMatt Macy 	boolean_t is_encroot;
8076eda14cbcSMatt Macy 	loadkey_cbdata_t *cb = data;
8077eda14cbcSMatt Macy 	uint64_t keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
8078eda14cbcSMatt Macy 
8079eda14cbcSMatt Macy 	/*
8080eda14cbcSMatt Macy 	 * If we are working recursively, we want to skip loading / unloading
8081eda14cbcSMatt Macy 	 * keys for non-encryption roots and datasets whose keys are already
8082eda14cbcSMatt Macy 	 * in the desired end-state.
8083eda14cbcSMatt Macy 	 */
8084eda14cbcSMatt Macy 	if (cb->cb_recursive) {
8085eda14cbcSMatt Macy 		ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
8086eda14cbcSMatt Macy 		if (ret != 0)
8087eda14cbcSMatt Macy 			return (ret);
8088eda14cbcSMatt Macy 		if (!is_encroot)
8089eda14cbcSMatt Macy 			return (0);
8090eda14cbcSMatt Macy 
8091eda14cbcSMatt Macy 		if ((cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_AVAILABLE) ||
8092eda14cbcSMatt Macy 		    (!cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_UNAVAILABLE))
8093eda14cbcSMatt Macy 			return (0);
8094eda14cbcSMatt Macy 	}
8095eda14cbcSMatt Macy 
8096eda14cbcSMatt Macy 	cb->cb_numattempted++;
8097eda14cbcSMatt Macy 
8098eda14cbcSMatt Macy 	if (cb->cb_loadkey)
8099eda14cbcSMatt Macy 		ret = zfs_crypto_load_key(zhp, cb->cb_noop, cb->cb_keylocation);
8100eda14cbcSMatt Macy 	else
8101eda14cbcSMatt Macy 		ret = zfs_crypto_unload_key(zhp);
8102eda14cbcSMatt Macy 
8103eda14cbcSMatt Macy 	if (ret != 0) {
8104eda14cbcSMatt Macy 		cb->cb_numfailed++;
8105eda14cbcSMatt Macy 		return (ret);
8106eda14cbcSMatt Macy 	}
8107eda14cbcSMatt Macy 
8108eda14cbcSMatt Macy 	return (0);
8109eda14cbcSMatt Macy }
8110eda14cbcSMatt Macy 
8111eda14cbcSMatt Macy static int
8112eda14cbcSMatt Macy load_unload_keys(int argc, char **argv, boolean_t loadkey)
8113eda14cbcSMatt Macy {
8114eda14cbcSMatt Macy 	int c, ret = 0, flags = 0;
8115eda14cbcSMatt Macy 	boolean_t do_all = B_FALSE;
8116eda14cbcSMatt Macy 	loadkey_cbdata_t cb = { 0 };
8117eda14cbcSMatt Macy 
8118eda14cbcSMatt Macy 	cb.cb_loadkey = loadkey;
8119eda14cbcSMatt Macy 
8120eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, "anrL:")) != -1) {
8121eda14cbcSMatt Macy 		/* noop and alternate keylocations only apply to zfs load-key */
8122eda14cbcSMatt Macy 		if (loadkey) {
8123eda14cbcSMatt Macy 			switch (c) {
8124eda14cbcSMatt Macy 			case 'n':
8125eda14cbcSMatt Macy 				cb.cb_noop = B_TRUE;
8126eda14cbcSMatt Macy 				continue;
8127eda14cbcSMatt Macy 			case 'L':
8128eda14cbcSMatt Macy 				cb.cb_keylocation = optarg;
8129eda14cbcSMatt Macy 				continue;
8130eda14cbcSMatt Macy 			default:
8131eda14cbcSMatt Macy 				break;
8132eda14cbcSMatt Macy 			}
8133eda14cbcSMatt Macy 		}
8134eda14cbcSMatt Macy 
8135eda14cbcSMatt Macy 		switch (c) {
8136eda14cbcSMatt Macy 		case 'a':
8137eda14cbcSMatt Macy 			do_all = B_TRUE;
8138eda14cbcSMatt Macy 			cb.cb_recursive = B_TRUE;
8139eda14cbcSMatt Macy 			break;
8140eda14cbcSMatt Macy 		case 'r':
8141eda14cbcSMatt Macy 			flags |= ZFS_ITER_RECURSE;
8142eda14cbcSMatt Macy 			cb.cb_recursive = B_TRUE;
8143eda14cbcSMatt Macy 			break;
8144eda14cbcSMatt Macy 		default:
8145eda14cbcSMatt Macy 			(void) fprintf(stderr,
8146eda14cbcSMatt Macy 			    gettext("invalid option '%c'\n"), optopt);
8147eda14cbcSMatt Macy 			usage(B_FALSE);
8148eda14cbcSMatt Macy 		}
8149eda14cbcSMatt Macy 	}
8150eda14cbcSMatt Macy 
8151eda14cbcSMatt Macy 	argc -= optind;
8152eda14cbcSMatt Macy 	argv += optind;
8153eda14cbcSMatt Macy 
8154eda14cbcSMatt Macy 	if (!do_all && argc == 0) {
8155eda14cbcSMatt Macy 		(void) fprintf(stderr,
8156eda14cbcSMatt Macy 		    gettext("Missing dataset argument or -a option\n"));
8157eda14cbcSMatt Macy 		usage(B_FALSE);
8158eda14cbcSMatt Macy 	}
8159eda14cbcSMatt Macy 
8160eda14cbcSMatt Macy 	if (do_all && argc != 0) {
8161eda14cbcSMatt Macy 		(void) fprintf(stderr,
8162eda14cbcSMatt Macy 		    gettext("Cannot specify dataset with -a option\n"));
8163eda14cbcSMatt Macy 		usage(B_FALSE);
8164eda14cbcSMatt Macy 	}
8165eda14cbcSMatt Macy 
8166eda14cbcSMatt Macy 	if (cb.cb_recursive && cb.cb_keylocation != NULL &&
8167eda14cbcSMatt Macy 	    strcmp(cb.cb_keylocation, "prompt") != 0) {
8168eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("alternate keylocation may only "
8169eda14cbcSMatt Macy 		    "be 'prompt' with -r or -a\n"));
8170eda14cbcSMatt Macy 		usage(B_FALSE);
8171eda14cbcSMatt Macy 	}
8172eda14cbcSMatt Macy 
8173eda14cbcSMatt Macy 	ret = zfs_for_each(argc, argv, flags,
8174eda14cbcSMatt Macy 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, NULL, NULL, 0,
8175eda14cbcSMatt Macy 	    load_key_callback, &cb);
8176eda14cbcSMatt Macy 
8177eda14cbcSMatt Macy 	if (cb.cb_noop || (cb.cb_recursive && cb.cb_numattempted != 0)) {
8178eda14cbcSMatt Macy 		(void) printf(gettext("%llu / %llu key(s) successfully %s\n"),
8179eda14cbcSMatt Macy 		    (u_longlong_t)(cb.cb_numattempted - cb.cb_numfailed),
8180eda14cbcSMatt Macy 		    (u_longlong_t)cb.cb_numattempted,
8181eda14cbcSMatt Macy 		    loadkey ? (cb.cb_noop ? "verified" : "loaded") :
8182eda14cbcSMatt Macy 		    "unloaded");
8183eda14cbcSMatt Macy 	}
8184eda14cbcSMatt Macy 
8185eda14cbcSMatt Macy 	if (cb.cb_numfailed != 0)
8186eda14cbcSMatt Macy 		ret = -1;
8187eda14cbcSMatt Macy 
8188eda14cbcSMatt Macy 	return (ret);
8189eda14cbcSMatt Macy }
8190eda14cbcSMatt Macy 
8191eda14cbcSMatt Macy static int
8192eda14cbcSMatt Macy zfs_do_load_key(int argc, char **argv)
8193eda14cbcSMatt Macy {
8194eda14cbcSMatt Macy 	return (load_unload_keys(argc, argv, B_TRUE));
8195eda14cbcSMatt Macy }
8196eda14cbcSMatt Macy 
8197eda14cbcSMatt Macy 
8198eda14cbcSMatt Macy static int
8199eda14cbcSMatt Macy zfs_do_unload_key(int argc, char **argv)
8200eda14cbcSMatt Macy {
8201eda14cbcSMatt Macy 	return (load_unload_keys(argc, argv, B_FALSE));
8202eda14cbcSMatt Macy }
8203eda14cbcSMatt Macy 
8204eda14cbcSMatt Macy static int
8205eda14cbcSMatt Macy zfs_do_change_key(int argc, char **argv)
8206eda14cbcSMatt Macy {
8207eda14cbcSMatt Macy 	int c, ret;
8208eda14cbcSMatt Macy 	uint64_t keystatus;
8209eda14cbcSMatt Macy 	boolean_t loadkey = B_FALSE, inheritkey = B_FALSE;
8210eda14cbcSMatt Macy 	zfs_handle_t *zhp = NULL;
8211eda14cbcSMatt Macy 	nvlist_t *props = fnvlist_alloc();
8212eda14cbcSMatt Macy 
8213eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, "lio:")) != -1) {
8214eda14cbcSMatt Macy 		switch (c) {
8215eda14cbcSMatt Macy 		case 'l':
8216eda14cbcSMatt Macy 			loadkey = B_TRUE;
8217eda14cbcSMatt Macy 			break;
8218eda14cbcSMatt Macy 		case 'i':
8219eda14cbcSMatt Macy 			inheritkey = B_TRUE;
8220eda14cbcSMatt Macy 			break;
8221eda14cbcSMatt Macy 		case 'o':
8222eda14cbcSMatt Macy 			if (!parseprop(props, optarg)) {
8223eda14cbcSMatt Macy 				nvlist_free(props);
8224eda14cbcSMatt Macy 				return (1);
8225eda14cbcSMatt Macy 			}
8226eda14cbcSMatt Macy 			break;
8227eda14cbcSMatt Macy 		default:
8228eda14cbcSMatt Macy 			(void) fprintf(stderr,
8229eda14cbcSMatt Macy 			    gettext("invalid option '%c'\n"), optopt);
8230eda14cbcSMatt Macy 			usage(B_FALSE);
8231eda14cbcSMatt Macy 		}
8232eda14cbcSMatt Macy 	}
8233eda14cbcSMatt Macy 
8234eda14cbcSMatt Macy 	if (inheritkey && !nvlist_empty(props)) {
8235eda14cbcSMatt Macy 		(void) fprintf(stderr,
8236eda14cbcSMatt Macy 		    gettext("Properties not allowed for inheriting\n"));
8237eda14cbcSMatt Macy 		usage(B_FALSE);
8238eda14cbcSMatt Macy 	}
8239eda14cbcSMatt Macy 
8240eda14cbcSMatt Macy 	argc -= optind;
8241eda14cbcSMatt Macy 	argv += optind;
8242eda14cbcSMatt Macy 
8243eda14cbcSMatt Macy 	if (argc < 1) {
8244eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("Missing dataset argument\n"));
8245eda14cbcSMatt Macy 		usage(B_FALSE);
8246eda14cbcSMatt Macy 	}
8247eda14cbcSMatt Macy 
8248eda14cbcSMatt Macy 	if (argc > 1) {
8249eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("Too many arguments\n"));
8250eda14cbcSMatt Macy 		usage(B_FALSE);
8251eda14cbcSMatt Macy 	}
8252eda14cbcSMatt Macy 
8253eda14cbcSMatt Macy 	zhp = zfs_open(g_zfs, argv[argc - 1],
8254eda14cbcSMatt Macy 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
8255eda14cbcSMatt Macy 	if (zhp == NULL)
8256eda14cbcSMatt Macy 		usage(B_FALSE);
8257eda14cbcSMatt Macy 
8258eda14cbcSMatt Macy 	if (loadkey) {
8259eda14cbcSMatt Macy 		keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
8260eda14cbcSMatt Macy 		if (keystatus != ZFS_KEYSTATUS_AVAILABLE) {
8261eda14cbcSMatt Macy 			ret = zfs_crypto_load_key(zhp, B_FALSE, NULL);
8262eda14cbcSMatt Macy 			if (ret != 0) {
8263eda14cbcSMatt Macy 				nvlist_free(props);
8264eda14cbcSMatt Macy 				zfs_close(zhp);
8265eda14cbcSMatt Macy 				return (-1);
8266eda14cbcSMatt Macy 			}
8267eda14cbcSMatt Macy 		}
8268eda14cbcSMatt Macy 
8269eda14cbcSMatt Macy 		/* refresh the properties so the new keystatus is visible */
8270eda14cbcSMatt Macy 		zfs_refresh_properties(zhp);
8271eda14cbcSMatt Macy 	}
8272eda14cbcSMatt Macy 
8273eda14cbcSMatt Macy 	ret = zfs_crypto_rewrap(zhp, props, inheritkey);
8274eda14cbcSMatt Macy 	if (ret != 0) {
8275eda14cbcSMatt Macy 		nvlist_free(props);
8276eda14cbcSMatt Macy 		zfs_close(zhp);
8277eda14cbcSMatt Macy 		return (-1);
8278eda14cbcSMatt Macy 	}
8279eda14cbcSMatt Macy 
8280eda14cbcSMatt Macy 	nvlist_free(props);
8281eda14cbcSMatt Macy 	zfs_close(zhp);
8282eda14cbcSMatt Macy 	return (0);
8283eda14cbcSMatt Macy }
8284eda14cbcSMatt Macy 
8285eda14cbcSMatt Macy /*
8286eda14cbcSMatt Macy  * 1) zfs project [-d|-r] <file|directory ...>
8287eda14cbcSMatt Macy  *    List project ID and inherit flag of file(s) or directories.
8288eda14cbcSMatt Macy  *    -d: List the directory itself, not its children.
8289eda14cbcSMatt Macy  *    -r: List subdirectories recursively.
8290eda14cbcSMatt Macy  *
8291eda14cbcSMatt Macy  * 2) zfs project -C [-k] [-r] <file|directory ...>
8292eda14cbcSMatt Macy  *    Clear project inherit flag and/or ID on the file(s) or directories.
8293eda14cbcSMatt Macy  *    -k: Keep the project ID unchanged. If not specified, the project ID
8294eda14cbcSMatt Macy  *	  will be reset as zero.
8295eda14cbcSMatt Macy  *    -r: Clear on subdirectories recursively.
8296eda14cbcSMatt Macy  *
8297eda14cbcSMatt Macy  * 3) zfs project -c [-0] [-d|-r] [-p id] <file|directory ...>
8298eda14cbcSMatt Macy  *    Check project ID and inherit flag on the file(s) or directories,
8299eda14cbcSMatt Macy  *    report the outliers.
8300eda14cbcSMatt Macy  *    -0: Print file name followed by a NUL instead of newline.
8301eda14cbcSMatt Macy  *    -d: Check the directory itself, not its children.
8302eda14cbcSMatt Macy  *    -p: Specify the referenced ID for comparing with the target file(s)
8303eda14cbcSMatt Macy  *	  or directories' project IDs. If not specified, the target (top)
8304eda14cbcSMatt Macy  *	  directory's project ID will be used as the referenced one.
8305eda14cbcSMatt Macy  *    -r: Check subdirectories recursively.
8306eda14cbcSMatt Macy  *
8307eda14cbcSMatt Macy  * 4) zfs project [-p id] [-r] [-s] <file|directory ...>
8308eda14cbcSMatt Macy  *    Set project ID and/or inherit flag on the file(s) or directories.
8309eda14cbcSMatt Macy  *    -p: Set the project ID as the given id.
8310eda14cbcSMatt Macy  *    -r: Set on subdirectories recursively. If not specify "-p" option,
8311eda14cbcSMatt Macy  *	  it will use top-level directory's project ID as the given id,
8312eda14cbcSMatt Macy  *	  then set both project ID and inherit flag on all descendants
8313eda14cbcSMatt Macy  *	  of the top-level directory.
8314eda14cbcSMatt Macy  *    -s: Set project inherit flag.
8315eda14cbcSMatt Macy  */
8316eda14cbcSMatt Macy static int
8317eda14cbcSMatt Macy zfs_do_project(int argc, char **argv)
8318eda14cbcSMatt Macy {
8319eda14cbcSMatt Macy 	zfs_project_control_t zpc = {
8320eda14cbcSMatt Macy 		.zpc_expected_projid = ZFS_INVALID_PROJID,
8321eda14cbcSMatt Macy 		.zpc_op = ZFS_PROJECT_OP_DEFAULT,
8322eda14cbcSMatt Macy 		.zpc_dironly = B_FALSE,
8323eda14cbcSMatt Macy 		.zpc_keep_projid = B_FALSE,
8324eda14cbcSMatt Macy 		.zpc_newline = B_TRUE,
8325eda14cbcSMatt Macy 		.zpc_recursive = B_FALSE,
8326eda14cbcSMatt Macy 		.zpc_set_flag = B_FALSE,
8327eda14cbcSMatt Macy 	};
8328eda14cbcSMatt Macy 	int ret = 0, c;
8329eda14cbcSMatt Macy 
8330eda14cbcSMatt Macy 	if (argc < 2)
8331eda14cbcSMatt Macy 		usage(B_FALSE);
8332eda14cbcSMatt Macy 
8333eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, "0Ccdkp:rs")) != -1) {
8334eda14cbcSMatt Macy 		switch (c) {
8335eda14cbcSMatt Macy 		case '0':
8336eda14cbcSMatt Macy 			zpc.zpc_newline = B_FALSE;
8337eda14cbcSMatt Macy 			break;
8338eda14cbcSMatt Macy 		case 'C':
8339eda14cbcSMatt Macy 			if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
8340eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("cannot "
8341eda14cbcSMatt Macy 				    "specify '-C' '-c' '-s' together\n"));
8342eda14cbcSMatt Macy 				usage(B_FALSE);
8343eda14cbcSMatt Macy 			}
8344eda14cbcSMatt Macy 
8345eda14cbcSMatt Macy 			zpc.zpc_op = ZFS_PROJECT_OP_CLEAR;
8346eda14cbcSMatt Macy 			break;
8347eda14cbcSMatt Macy 		case 'c':
8348eda14cbcSMatt Macy 			if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
8349eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("cannot "
8350eda14cbcSMatt Macy 				    "specify '-C' '-c' '-s' together\n"));
8351eda14cbcSMatt Macy 				usage(B_FALSE);
8352eda14cbcSMatt Macy 			}
8353eda14cbcSMatt Macy 
8354eda14cbcSMatt Macy 			zpc.zpc_op = ZFS_PROJECT_OP_CHECK;
8355eda14cbcSMatt Macy 			break;
8356eda14cbcSMatt Macy 		case 'd':
8357eda14cbcSMatt Macy 			zpc.zpc_dironly = B_TRUE;
8358eda14cbcSMatt Macy 			/* overwrite "-r" option */
8359eda14cbcSMatt Macy 			zpc.zpc_recursive = B_FALSE;
8360eda14cbcSMatt Macy 			break;
8361eda14cbcSMatt Macy 		case 'k':
8362eda14cbcSMatt Macy 			zpc.zpc_keep_projid = B_TRUE;
8363eda14cbcSMatt Macy 			break;
8364eda14cbcSMatt Macy 		case 'p': {
8365eda14cbcSMatt Macy 			char *endptr;
8366eda14cbcSMatt Macy 
8367eda14cbcSMatt Macy 			errno = 0;
8368eda14cbcSMatt Macy 			zpc.zpc_expected_projid = strtoull(optarg, &endptr, 0);
8369eda14cbcSMatt Macy 			if (errno != 0 || *endptr != '\0') {
8370eda14cbcSMatt Macy 				(void) fprintf(stderr,
8371eda14cbcSMatt Macy 				    gettext("project ID must be less than "
8372eda14cbcSMatt Macy 				    "%u\n"), UINT32_MAX);
8373eda14cbcSMatt Macy 				usage(B_FALSE);
8374eda14cbcSMatt Macy 			}
8375eda14cbcSMatt Macy 			if (zpc.zpc_expected_projid >= UINT32_MAX) {
8376eda14cbcSMatt Macy 				(void) fprintf(stderr,
8377eda14cbcSMatt Macy 				    gettext("invalid project ID\n"));
8378eda14cbcSMatt Macy 				usage(B_FALSE);
8379eda14cbcSMatt Macy 			}
8380eda14cbcSMatt Macy 			break;
8381eda14cbcSMatt Macy 		}
8382eda14cbcSMatt Macy 		case 'r':
8383eda14cbcSMatt Macy 			zpc.zpc_recursive = B_TRUE;
8384eda14cbcSMatt Macy 			/* overwrite "-d" option */
8385eda14cbcSMatt Macy 			zpc.zpc_dironly = B_FALSE;
8386eda14cbcSMatt Macy 			break;
8387eda14cbcSMatt Macy 		case 's':
8388eda14cbcSMatt Macy 			if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
8389eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("cannot "
8390eda14cbcSMatt Macy 				    "specify '-C' '-c' '-s' together\n"));
8391eda14cbcSMatt Macy 				usage(B_FALSE);
8392eda14cbcSMatt Macy 			}
8393eda14cbcSMatt Macy 
8394eda14cbcSMatt Macy 			zpc.zpc_set_flag = B_TRUE;
8395eda14cbcSMatt Macy 			zpc.zpc_op = ZFS_PROJECT_OP_SET;
8396eda14cbcSMatt Macy 			break;
8397eda14cbcSMatt Macy 		default:
8398eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
8399eda14cbcSMatt Macy 			    optopt);
8400eda14cbcSMatt Macy 			usage(B_FALSE);
8401eda14cbcSMatt Macy 		}
8402eda14cbcSMatt Macy 	}
8403eda14cbcSMatt Macy 
8404eda14cbcSMatt Macy 	if (zpc.zpc_op == ZFS_PROJECT_OP_DEFAULT) {
8405eda14cbcSMatt Macy 		if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID)
8406eda14cbcSMatt Macy 			zpc.zpc_op = ZFS_PROJECT_OP_SET;
8407eda14cbcSMatt Macy 		else
8408eda14cbcSMatt Macy 			zpc.zpc_op = ZFS_PROJECT_OP_LIST;
8409eda14cbcSMatt Macy 	}
8410eda14cbcSMatt Macy 
8411eda14cbcSMatt Macy 	switch (zpc.zpc_op) {
8412eda14cbcSMatt Macy 	case ZFS_PROJECT_OP_LIST:
8413eda14cbcSMatt Macy 		if (zpc.zpc_keep_projid) {
8414eda14cbcSMatt Macy 			(void) fprintf(stderr,
8415eda14cbcSMatt Macy 			    gettext("'-k' is only valid together with '-C'\n"));
8416eda14cbcSMatt Macy 			usage(B_FALSE);
8417eda14cbcSMatt Macy 		}
8418eda14cbcSMatt Macy 		if (!zpc.zpc_newline) {
8419eda14cbcSMatt Macy 			(void) fprintf(stderr,
8420eda14cbcSMatt Macy 			    gettext("'-0' is only valid together with '-c'\n"));
8421eda14cbcSMatt Macy 			usage(B_FALSE);
8422eda14cbcSMatt Macy 		}
8423eda14cbcSMatt Macy 		break;
8424eda14cbcSMatt Macy 	case ZFS_PROJECT_OP_CHECK:
8425eda14cbcSMatt Macy 		if (zpc.zpc_keep_projid) {
8426eda14cbcSMatt Macy 			(void) fprintf(stderr,
8427eda14cbcSMatt Macy 			    gettext("'-k' is only valid together with '-C'\n"));
8428eda14cbcSMatt Macy 			usage(B_FALSE);
8429eda14cbcSMatt Macy 		}
8430eda14cbcSMatt Macy 		break;
8431eda14cbcSMatt Macy 	case ZFS_PROJECT_OP_CLEAR:
8432eda14cbcSMatt Macy 		if (zpc.zpc_dironly) {
8433eda14cbcSMatt Macy 			(void) fprintf(stderr,
8434eda14cbcSMatt Macy 			    gettext("'-d' is useless together with '-C'\n"));
8435eda14cbcSMatt Macy 			usage(B_FALSE);
8436eda14cbcSMatt Macy 		}
8437eda14cbcSMatt Macy 		if (!zpc.zpc_newline) {
8438eda14cbcSMatt Macy 			(void) fprintf(stderr,
8439eda14cbcSMatt Macy 			    gettext("'-0' is only valid together with '-c'\n"));
8440eda14cbcSMatt Macy 			usage(B_FALSE);
8441eda14cbcSMatt Macy 		}
8442eda14cbcSMatt Macy 		if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID) {
8443eda14cbcSMatt Macy 			(void) fprintf(stderr,
8444eda14cbcSMatt Macy 			    gettext("'-p' is useless together with '-C'\n"));
8445eda14cbcSMatt Macy 			usage(B_FALSE);
8446eda14cbcSMatt Macy 		}
8447eda14cbcSMatt Macy 		break;
8448eda14cbcSMatt Macy 	case ZFS_PROJECT_OP_SET:
8449eda14cbcSMatt Macy 		if (zpc.zpc_dironly) {
8450eda14cbcSMatt Macy 			(void) fprintf(stderr,
8451eda14cbcSMatt Macy 			    gettext("'-d' is useless for set project ID and/or "
8452eda14cbcSMatt Macy 			    "inherit flag\n"));
8453eda14cbcSMatt Macy 			usage(B_FALSE);
8454eda14cbcSMatt Macy 		}
8455eda14cbcSMatt Macy 		if (zpc.zpc_keep_projid) {
8456eda14cbcSMatt Macy 			(void) fprintf(stderr,
8457eda14cbcSMatt Macy 			    gettext("'-k' is only valid together with '-C'\n"));
8458eda14cbcSMatt Macy 			usage(B_FALSE);
8459eda14cbcSMatt Macy 		}
8460eda14cbcSMatt Macy 		if (!zpc.zpc_newline) {
8461eda14cbcSMatt Macy 			(void) fprintf(stderr,
8462eda14cbcSMatt Macy 			    gettext("'-0' is only valid together with '-c'\n"));
8463eda14cbcSMatt Macy 			usage(B_FALSE);
8464eda14cbcSMatt Macy 		}
8465eda14cbcSMatt Macy 		break;
8466eda14cbcSMatt Macy 	default:
8467eda14cbcSMatt Macy 		ASSERT(0);
8468eda14cbcSMatt Macy 		break;
8469eda14cbcSMatt Macy 	}
8470eda14cbcSMatt Macy 
8471eda14cbcSMatt Macy 	argv += optind;
8472eda14cbcSMatt Macy 	argc -= optind;
8473eda14cbcSMatt Macy 	if (argc == 0) {
8474eda14cbcSMatt Macy 		(void) fprintf(stderr,
8475eda14cbcSMatt Macy 		    gettext("missing file or directory target(s)\n"));
8476eda14cbcSMatt Macy 		usage(B_FALSE);
8477eda14cbcSMatt Macy 	}
8478eda14cbcSMatt Macy 
8479eda14cbcSMatt Macy 	for (int i = 0; i < argc; i++) {
8480eda14cbcSMatt Macy 		int err;
8481eda14cbcSMatt Macy 
8482eda14cbcSMatt Macy 		err = zfs_project_handle(argv[i], &zpc);
8483eda14cbcSMatt Macy 		if (err && !ret)
8484eda14cbcSMatt Macy 			ret = err;
8485eda14cbcSMatt Macy 	}
8486eda14cbcSMatt Macy 
8487eda14cbcSMatt Macy 	return (ret);
8488eda14cbcSMatt Macy }
8489eda14cbcSMatt Macy 
8490eda14cbcSMatt Macy static int
8491eda14cbcSMatt Macy zfs_do_wait(int argc, char **argv)
8492eda14cbcSMatt Macy {
8493eda14cbcSMatt Macy 	boolean_t enabled[ZFS_WAIT_NUM_ACTIVITIES];
8494eda14cbcSMatt Macy 	int error, i;
8495*7877fdebSMatt Macy 	int c;
8496eda14cbcSMatt Macy 
8497eda14cbcSMatt Macy 	/* By default, wait for all types of activity. */
8498eda14cbcSMatt Macy 	for (i = 0; i < ZFS_WAIT_NUM_ACTIVITIES; i++)
8499eda14cbcSMatt Macy 		enabled[i] = B_TRUE;
8500eda14cbcSMatt Macy 
8501eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, "t:")) != -1) {
8502eda14cbcSMatt Macy 		switch (c) {
8503eda14cbcSMatt Macy 		case 't':
8504eda14cbcSMatt Macy 		{
8505eda14cbcSMatt Macy 			static char *col_subopts[] = { "deleteq", NULL };
8506eda14cbcSMatt Macy 			char *value;
8507eda14cbcSMatt Macy 
8508eda14cbcSMatt Macy 			/* Reset activities array */
8509eda14cbcSMatt Macy 			bzero(&enabled, sizeof (enabled));
8510eda14cbcSMatt Macy 			while (*optarg != '\0') {
8511eda14cbcSMatt Macy 				int activity = getsubopt(&optarg, col_subopts,
8512eda14cbcSMatt Macy 				    &value);
8513eda14cbcSMatt Macy 
8514eda14cbcSMatt Macy 				if (activity < 0) {
8515eda14cbcSMatt Macy 					(void) fprintf(stderr,
8516eda14cbcSMatt Macy 					    gettext("invalid activity '%s'\n"),
8517eda14cbcSMatt Macy 					    value);
8518eda14cbcSMatt Macy 					usage(B_FALSE);
8519eda14cbcSMatt Macy 				}
8520eda14cbcSMatt Macy 
8521eda14cbcSMatt Macy 				enabled[activity] = B_TRUE;
8522eda14cbcSMatt Macy 			}
8523eda14cbcSMatt Macy 			break;
8524eda14cbcSMatt Macy 		}
8525eda14cbcSMatt Macy 		case '?':
8526eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
8527eda14cbcSMatt Macy 			    optopt);
8528eda14cbcSMatt Macy 			usage(B_FALSE);
8529eda14cbcSMatt Macy 		}
8530eda14cbcSMatt Macy 	}
8531eda14cbcSMatt Macy 
8532eda14cbcSMatt Macy 	argv += optind;
8533eda14cbcSMatt Macy 	argc -= optind;
8534eda14cbcSMatt Macy 	if (argc < 1) {
8535eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing 'filesystem' "
8536eda14cbcSMatt Macy 		    "argument\n"));
8537eda14cbcSMatt Macy 		usage(B_FALSE);
8538eda14cbcSMatt Macy 	}
8539eda14cbcSMatt Macy 	if (argc > 1) {
8540eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("too many arguments\n"));
8541eda14cbcSMatt Macy 		usage(B_FALSE);
8542eda14cbcSMatt Macy 	}
8543eda14cbcSMatt Macy 
8544eda14cbcSMatt Macy 	zfs_handle_t *zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM);
8545eda14cbcSMatt Macy 	if (zhp == NULL)
8546eda14cbcSMatt Macy 		return (1);
8547eda14cbcSMatt Macy 
8548eda14cbcSMatt Macy 	for (;;) {
8549eda14cbcSMatt Macy 		boolean_t missing = B_FALSE;
8550eda14cbcSMatt Macy 		boolean_t any_waited = B_FALSE;
8551eda14cbcSMatt Macy 
8552eda14cbcSMatt Macy 		for (int i = 0; i < ZFS_WAIT_NUM_ACTIVITIES; i++) {
8553eda14cbcSMatt Macy 			boolean_t waited;
8554eda14cbcSMatt Macy 
8555eda14cbcSMatt Macy 			if (!enabled[i])
8556eda14cbcSMatt Macy 				continue;
8557eda14cbcSMatt Macy 
8558eda14cbcSMatt Macy 			error = zfs_wait_status(zhp, i, &missing, &waited);
8559eda14cbcSMatt Macy 			if (error != 0 || missing)
8560eda14cbcSMatt Macy 				break;
8561eda14cbcSMatt Macy 
8562eda14cbcSMatt Macy 			any_waited = (any_waited || waited);
8563eda14cbcSMatt Macy 		}
8564eda14cbcSMatt Macy 
8565eda14cbcSMatt Macy 		if (error != 0 || missing || !any_waited)
8566eda14cbcSMatt Macy 			break;
8567eda14cbcSMatt Macy 	}
8568eda14cbcSMatt Macy 
8569eda14cbcSMatt Macy 	zfs_close(zhp);
8570eda14cbcSMatt Macy 
8571eda14cbcSMatt Macy 	return (error);
8572eda14cbcSMatt Macy }
8573eda14cbcSMatt Macy 
8574eda14cbcSMatt Macy /*
8575eda14cbcSMatt Macy  * Display version message
8576eda14cbcSMatt Macy  */
8577eda14cbcSMatt Macy static int
8578eda14cbcSMatt Macy zfs_do_version(int argc, char **argv)
8579eda14cbcSMatt Macy {
8580eda14cbcSMatt Macy 	if (zfs_version_print() == -1)
8581eda14cbcSMatt Macy 		return (1);
8582eda14cbcSMatt Macy 
8583eda14cbcSMatt Macy 	return (0);
8584eda14cbcSMatt Macy }
8585eda14cbcSMatt Macy 
8586eda14cbcSMatt Macy int
8587eda14cbcSMatt Macy main(int argc, char **argv)
8588eda14cbcSMatt Macy {
8589eda14cbcSMatt Macy 	int ret = 0;
8590eda14cbcSMatt Macy 	int i = 0;
8591eda14cbcSMatt Macy 	char *cmdname;
8592eda14cbcSMatt Macy 	char **newargv;
8593eda14cbcSMatt Macy 
8594eda14cbcSMatt Macy 	(void) setlocale(LC_ALL, "");
85952c48331dSMatt Macy 	(void) setlocale(LC_NUMERIC, "C");
8596eda14cbcSMatt Macy 	(void) textdomain(TEXT_DOMAIN);
8597eda14cbcSMatt Macy 
8598eda14cbcSMatt Macy 	opterr = 0;
8599eda14cbcSMatt Macy 
8600eda14cbcSMatt Macy 	/*
8601eda14cbcSMatt Macy 	 * Make sure the user has specified some command.
8602eda14cbcSMatt Macy 	 */
8603eda14cbcSMatt Macy 	if (argc < 2) {
8604eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing command\n"));
8605eda14cbcSMatt Macy 		usage(B_FALSE);
8606eda14cbcSMatt Macy 	}
8607eda14cbcSMatt Macy 
8608eda14cbcSMatt Macy 	cmdname = argv[1];
8609eda14cbcSMatt Macy 
8610eda14cbcSMatt Macy 	/*
8611eda14cbcSMatt Macy 	 * The 'umount' command is an alias for 'unmount'
8612eda14cbcSMatt Macy 	 */
8613eda14cbcSMatt Macy 	if (strcmp(cmdname, "umount") == 0)
8614eda14cbcSMatt Macy 		cmdname = "unmount";
8615eda14cbcSMatt Macy 
8616eda14cbcSMatt Macy 	/*
8617eda14cbcSMatt Macy 	 * The 'recv' command is an alias for 'receive'
8618eda14cbcSMatt Macy 	 */
8619eda14cbcSMatt Macy 	if (strcmp(cmdname, "recv") == 0)
8620eda14cbcSMatt Macy 		cmdname = "receive";
8621eda14cbcSMatt Macy 
8622eda14cbcSMatt Macy 	/*
8623eda14cbcSMatt Macy 	 * The 'snap' command is an alias for 'snapshot'
8624eda14cbcSMatt Macy 	 */
8625eda14cbcSMatt Macy 	if (strcmp(cmdname, "snap") == 0)
8626eda14cbcSMatt Macy 		cmdname = "snapshot";
8627eda14cbcSMatt Macy 
8628eda14cbcSMatt Macy 	/*
8629eda14cbcSMatt Macy 	 * Special case '-?'
8630eda14cbcSMatt Macy 	 */
8631eda14cbcSMatt Macy 	if ((strcmp(cmdname, "-?") == 0) ||
8632eda14cbcSMatt Macy 	    (strcmp(cmdname, "--help") == 0))
8633eda14cbcSMatt Macy 		usage(B_TRUE);
8634eda14cbcSMatt Macy 
8635eda14cbcSMatt Macy 	/*
8636eda14cbcSMatt Macy 	 * Special case '-V|--version'
8637eda14cbcSMatt Macy 	 */
8638eda14cbcSMatt Macy 	if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0))
8639eda14cbcSMatt Macy 		return (zfs_do_version(argc, argv));
8640eda14cbcSMatt Macy 
8641eda14cbcSMatt Macy 	if ((g_zfs = libzfs_init()) == NULL) {
8642eda14cbcSMatt Macy 		(void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
8643eda14cbcSMatt Macy 		return (1);
8644eda14cbcSMatt Macy 	}
8645eda14cbcSMatt Macy 
8646eda14cbcSMatt Macy 	mnttab_file = g_zfs->libzfs_mnttab;
8647eda14cbcSMatt Macy 
8648eda14cbcSMatt Macy 	zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
8649eda14cbcSMatt Macy 
8650eda14cbcSMatt Macy 	libzfs_print_on_error(g_zfs, B_TRUE);
8651eda14cbcSMatt Macy 
8652eda14cbcSMatt Macy 	/*
8653eda14cbcSMatt Macy 	 * Many commands modify input strings for string parsing reasons.
8654eda14cbcSMatt Macy 	 * We create a copy to protect the original argv.
8655eda14cbcSMatt Macy 	 */
8656eda14cbcSMatt Macy 	newargv = malloc((argc + 1) * sizeof (newargv[0]));
8657eda14cbcSMatt Macy 	for (i = 0; i < argc; i++)
8658eda14cbcSMatt Macy 		newargv[i] = strdup(argv[i]);
8659eda14cbcSMatt Macy 	newargv[argc] = NULL;
8660eda14cbcSMatt Macy 
8661eda14cbcSMatt Macy 	/*
8662eda14cbcSMatt Macy 	 * Run the appropriate command.
8663eda14cbcSMatt Macy 	 */
8664eda14cbcSMatt Macy 	libzfs_mnttab_cache(g_zfs, B_TRUE);
8665eda14cbcSMatt Macy 	if (find_command_idx(cmdname, &i) == 0) {
8666eda14cbcSMatt Macy 		current_command = &command_table[i];
8667eda14cbcSMatt Macy 		ret = command_table[i].func(argc - 1, newargv + 1);
8668eda14cbcSMatt Macy 	} else if (strchr(cmdname, '=') != NULL) {
8669eda14cbcSMatt Macy 		verify(find_command_idx("set", &i) == 0);
8670eda14cbcSMatt Macy 		current_command = &command_table[i];
8671eda14cbcSMatt Macy 		ret = command_table[i].func(argc, newargv);
8672eda14cbcSMatt Macy 	} else {
8673eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("unrecognized "
8674eda14cbcSMatt Macy 		    "command '%s'\n"), cmdname);
8675eda14cbcSMatt Macy 		usage(B_FALSE);
8676eda14cbcSMatt Macy 		ret = 1;
8677eda14cbcSMatt Macy 	}
8678eda14cbcSMatt Macy 
8679eda14cbcSMatt Macy 	for (i = 0; i < argc; i++)
8680eda14cbcSMatt Macy 		free(newargv[i]);
8681eda14cbcSMatt Macy 	free(newargv);
8682eda14cbcSMatt Macy 
8683eda14cbcSMatt Macy 	if (ret == 0 && log_history)
8684eda14cbcSMatt Macy 		(void) zpool_log_history(g_zfs, history_str);
8685eda14cbcSMatt Macy 
8686eda14cbcSMatt Macy 	libzfs_fini(g_zfs);
8687eda14cbcSMatt Macy 
8688eda14cbcSMatt Macy 	/*
8689eda14cbcSMatt Macy 	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8690eda14cbcSMatt Macy 	 * for the purposes of running ::findleaks.
8691eda14cbcSMatt Macy 	 */
8692eda14cbcSMatt Macy 	if (getenv("ZFS_ABORT") != NULL) {
8693eda14cbcSMatt Macy 		(void) printf("dumping core by request\n");
8694eda14cbcSMatt Macy 		abort();
8695eda14cbcSMatt Macy 	}
8696eda14cbcSMatt Macy 
8697eda14cbcSMatt Macy 	return (ret);
8698eda14cbcSMatt Macy }
8699eda14cbcSMatt Macy 
8700eda14cbcSMatt Macy #ifdef __FreeBSD__
8701eda14cbcSMatt Macy #include <sys/jail.h>
8702eda14cbcSMatt Macy #include <jail.h>
8703eda14cbcSMatt Macy /*
8704eda14cbcSMatt Macy  * Attach/detach the given dataset to/from the given jail
8705eda14cbcSMatt Macy  */
8706eda14cbcSMatt Macy /* ARGSUSED */
8707eda14cbcSMatt Macy static int
8708eda14cbcSMatt Macy zfs_do_jail_impl(int argc, char **argv, boolean_t attach)
8709eda14cbcSMatt Macy {
8710eda14cbcSMatt Macy 	zfs_handle_t *zhp;
8711eda14cbcSMatt Macy 	int jailid, ret;
8712eda14cbcSMatt Macy 
8713eda14cbcSMatt Macy 	/* check number of arguments */
8714eda14cbcSMatt Macy 	if (argc < 3) {
8715eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("missing argument(s)\n"));
8716eda14cbcSMatt Macy 		usage(B_FALSE);
8717eda14cbcSMatt Macy 	}
8718eda14cbcSMatt Macy 	if (argc > 3) {
8719eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("too many arguments\n"));
8720eda14cbcSMatt Macy 		usage(B_FALSE);
8721eda14cbcSMatt Macy 	}
8722eda14cbcSMatt Macy 
8723eda14cbcSMatt Macy 	jailid = jail_getid(argv[1]);
8724eda14cbcSMatt Macy 	if (jailid < 0) {
8725eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("invalid jail id or name\n"));
8726eda14cbcSMatt Macy 		usage(B_FALSE);
8727eda14cbcSMatt Macy 	}
8728eda14cbcSMatt Macy 
8729eda14cbcSMatt Macy 	zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
8730eda14cbcSMatt Macy 	if (zhp == NULL)
8731eda14cbcSMatt Macy 		return (1);
8732eda14cbcSMatt Macy 
8733eda14cbcSMatt Macy 	ret = (zfs_jail(zhp, jailid, attach) != 0);
8734eda14cbcSMatt Macy 
8735eda14cbcSMatt Macy 	zfs_close(zhp);
8736eda14cbcSMatt Macy 	return (ret);
8737eda14cbcSMatt Macy }
8738eda14cbcSMatt Macy 
8739eda14cbcSMatt Macy /*
8740eda14cbcSMatt Macy  * zfs jail jailid filesystem
8741eda14cbcSMatt Macy  *
8742eda14cbcSMatt Macy  * Attach the given dataset to the given jail
8743eda14cbcSMatt Macy  */
8744eda14cbcSMatt Macy /* ARGSUSED */
8745eda14cbcSMatt Macy static int
8746eda14cbcSMatt Macy zfs_do_jail(int argc, char **argv)
8747eda14cbcSMatt Macy {
8748eda14cbcSMatt Macy 	return (zfs_do_jail_impl(argc, argv, B_TRUE));
8749eda14cbcSMatt Macy }
8750eda14cbcSMatt Macy 
8751eda14cbcSMatt Macy /*
8752eda14cbcSMatt Macy  * zfs unjail jailid filesystem
8753eda14cbcSMatt Macy  *
8754eda14cbcSMatt Macy  * Detach the given dataset from the given jail
8755eda14cbcSMatt Macy  */
8756eda14cbcSMatt Macy /* ARGSUSED */
8757eda14cbcSMatt Macy static int
8758eda14cbcSMatt Macy zfs_do_unjail(int argc, char **argv)
8759eda14cbcSMatt Macy {
8760eda14cbcSMatt Macy 	return (zfs_do_jail_impl(argc, argv, B_FALSE));
8761eda14cbcSMatt Macy }
8762eda14cbcSMatt Macy #endif
8763