1a9bcb68mmacy/*
2a9bcb68mmacy * CDDL HEADER START
3a9bcb68mmacy *
4a9bcb68mmacy * The contents of this file are subject to the terms of the
5a9bcb68mmacy * Common Development and Distribution License (the "License").
6a9bcb68mmacy * You may not use this file except in compliance with the License.
7a9bcb68mmacy *
8a9bcb68mmacy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9a9bcb68mmacy * or http://www.opensolaris.org/os/licensing.
10a9bcb68mmacy * See the License for the specific language governing permissions
11a9bcb68mmacy * and limitations under the License.
12a9bcb68mmacy *
13a9bcb68mmacy * When distributing Covered Code, include this CDDL HEADER in each
14a9bcb68mmacy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15a9bcb68mmacy * If applicable, add the following below this CDDL HEADER, with the
16a9bcb68mmacy * fields enclosed by brackets "[]" replaced with your own identifying
17a9bcb68mmacy * information: Portions Copyright [yyyy] [name of copyright owner]
18a9bcb68mmacy *
19a9bcb68mmacy * CDDL HEADER END
20a9bcb68mmacy */
21a9bcb68mmacy
22a9bcb68mmacy/*
23a9bcb68mmacy * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24a9bcb68mmacy * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25a9bcb68mmacy * Copyright 2012 Milan Jurik. All rights reserved.
26a9bcb68mmacy * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27a9bcb68mmacy * Copyright (c) 2013 Steven Hartland.  All rights reserved.
28a9bcb68mmacy * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
29a9bcb68mmacy * Copyright 2016 Nexenta Systems, Inc.
30a9bcb68mmacy * Copyright (c) 2019 Datto Inc.
31a9bcb68mmacy * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
32a9bcb68mmacy * Copyright 2019 Joyent, Inc.
33a9bcb68mmacy * Copyright (c) 2019, 2020 by Christian Schwarz. All rights reserved.
34a9bcb68mmacy */
35a9bcb68mmacy
36a9bcb68mmacy#include <assert.h>
37a9bcb68mmacy#include <ctype.h>
38a9bcb68mmacy#include <sys/debug.h>
39a9bcb68mmacy#include <errno.h>
40a9bcb68mmacy#include <getopt.h>
41a9bcb68mmacy#include <libgen.h>
42a9bcb68mmacy#include <libintl.h>
43a9bcb68mmacy#include <libuutil.h>
44a9bcb68mmacy#include <libnvpair.h>
45a9bcb68mmacy#include <locale.h>
46a9bcb68mmacy#include <stddef.h>
47a9bcb68mmacy#include <stdio.h>
48a9bcb68mmacy#include <stdlib.h>
49a9bcb68mmacy#include <strings.h>
50a9bcb68mmacy#include <unistd.h>
51a9bcb68mmacy#include <fcntl.h>
52a9bcb68mmacy#include <zone.h>
53a9bcb68mmacy#include <grp.h>
54a9bcb68mmacy#include <pwd.h>
55a9bcb68mmacy#include <signal.h>
56a9bcb68mmacy#include <sys/debug.h>
57a9bcb68mmacy#include <sys/list.h>
58a9bcb68mmacy#include <sys/mkdev.h>
59a9bcb68mmacy#include <sys/mntent.h>
60a9bcb68mmacy#include <sys/mnttab.h>
61a9bcb68mmacy#include <sys/mount.h>
62a9bcb68mmacy#include <sys/stat.h>
63a9bcb68mmacy#include <sys/fs/zfs.h>
64a9bcb68mmacy#include <sys/systeminfo.h>
65a9bcb68mmacy#include <sys/types.h>
66a9bcb68mmacy#include <time.h>
67a9bcb68mmacy#include <sys/zfs_project.h>
68a9bcb68mmacy
69a9bcb68mmacy#include <libzfs.h>
70a9bcb68mmacy#include <libzfs_core.h>
71a9bcb68mmacy#include <zfs_prop.h>
72a9bcb68mmacy#include <zfs_deleg.h>
73a9bcb68mmacy#include <libzutil.h>
74a9bcb68mmacy#include <libuutil.h>
75a9bcb68mmacy#ifdef HAVE_IDMAP
76a9bcb68mmacy#include <aclutils.h>
77a9bcb68mmacy#include <directory.h>
78a9bcb68mmacy#endif /* HAVE_IDMAP */
79a9bcb68mmacy
80a9bcb68mmacy#include "zfs_iter.h"
81a9bcb68mmacy#include "zfs_util.h"
82a9bcb68mmacy#include "zfs_comutil.h"
83a9bcb68mmacy#include "libzfs_impl.h"
84a9bcb68mmacy#include "zfs_projectutil.h"
85a9bcb68mmacy
86a9bcb68mmacylibzfs_handle_t *g_zfs;
87a9bcb68mmacy
88a9bcb68mmacystatic FILE *mnttab_file;
89a9bcb68mmacystatic char history_str[HIS_MAX_RECORD_LEN];
90a9bcb68mmacystatic boolean_t log_history = B_TRUE;
91a9bcb68mmacy
92a9bcb68mmacystatic int zfs_do_clone(int argc, char **argv);
93a9bcb68mmacystatic int zfs_do_create(int argc, char **argv);
94a9bcb68mmacystatic int zfs_do_destroy(int argc, char **argv);
95a9bcb68mmacystatic int zfs_do_get(int argc, char **argv);
96a9bcb68mmacystatic int zfs_do_inherit(int argc, char **argv);
97a9bcb68mmacystatic int zfs_do_list(int argc, char **argv);
98a9bcb68mmacystatic int zfs_do_mount(int argc, char **argv);
99a9bcb68mmacystatic int zfs_do_rename(int argc, char **argv);
100a9bcb68mmacystatic int zfs_do_rollback(int argc, char **argv);
101a9bcb68mmacystatic int zfs_do_set(int argc, char **argv);
102a9bcb68mmacystatic int zfs_do_upgrade(int argc, char **argv);
103a9bcb68mmacystatic int zfs_do_snapshot(int argc, char **argv);
104a9bcb68mmacystatic int zfs_do_unmount(int argc, char **argv);
105a9bcb68mmacystatic int zfs_do_share(int argc, char **argv);
106a9bcb68mmacystatic int zfs_do_unshare(int argc, char **argv);
107a9bcb68mmacystatic int zfs_do_send(int argc, char **argv);
108a9bcb68mmacystatic int zfs_do_receive(int argc, char **argv);
109a9bcb68mmacystatic int zfs_do_promote(int argc, char **argv);
110a9bcb68mmacystatic int zfs_do_userspace(int argc, char **argv);
111a9bcb68mmacystatic int zfs_do_allow(int argc, char **argv);
112a9bcb68mmacystatic int zfs_do_unallow(int argc, char **argv);
113a9bcb68mmacystatic int zfs_do_hold(int argc, char **argv);
114a9bcb68mmacystatic int zfs_do_holds(int argc, char **argv);
115a9bcb68mmacystatic int zfs_do_release(int argc, char **argv);
116a9bcb68mmacystatic int zfs_do_diff(int argc, char **argv);
117a9bcb68mmacystatic int zfs_do_bookmark(int argc, char **argv);
118a9bcb68mmacystatic int zfs_do_channel_program(int argc, char **argv);
119a9bcb68mmacystatic int zfs_do_load_key(int argc, char **argv);
120a9bcb68mmacystatic int zfs_do_unload_key(int argc, char **argv);
121a9bcb68mmacystatic int zfs_do_change_key(int argc, char **argv);
122a9bcb68mmacystatic int zfs_do_project(int argc, char **argv);
123a9bcb68mmacystatic int zfs_do_version(int argc, char **argv);
124a9bcb68mmacystatic int zfs_do_redact(int argc, char **argv);
125a9bcb68mmacystatic int zfs_do_wait(int argc, char **argv);
126a9bcb68mmacy
127a9bcb68mmacy#ifdef __FreeBSD__
128a9bcb68mmacystatic int zfs_do_jail(int argc, char **argv);
129a9bcb68mmacystatic int zfs_do_unjail(int argc, char **argv);
130a9bcb68mmacy#endif
131a9bcb68mmacy
132a9bcb68mmacy/*
133a9bcb68mmacy * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
134a9bcb68mmacy */
135a9bcb68mmacy
136a9bcb68mmacy#ifdef DEBUG
137a9bcb68mmacyconst char *
138a9bcb68mmacy_umem_debug_init(void)
139a9bcb68mmacy{
140a9bcb68mmacy	return ("default,verbose"); /* $UMEM_DEBUG setting */
141a9bcb68mmacy}
142a9bcb68mmacy
143a9bcb68mmacyconst char *
144a9bcb68mmacy_umem_logging_init(void)
145a9bcb68mmacy{
146a9bcb68mmacy	return ("fail,contents"); /* $UMEM_LOGGING setting */
147a9bcb68mmacy}
148a9bcb68mmacy#endif
149a9bcb68mmacy
150a9bcb68mmacytypedef enum {
151a9bcb68mmacy	HELP_CLONE,
152a9bcb68mmacy	HELP_CREATE,
153a9bcb68mmacy	HELP_DESTROY,
154a9bcb68mmacy	HELP_GET,
155a9bcb68mmacy	HELP_INHERIT,
156a9bcb68mmacy	HELP_UPGRADE,
157a9bcb68mmacy	HELP_LIST,
158a9bcb68mmacy	HELP_MOUNT,
159a9bcb68mmacy	HELP_PROMOTE,
160a9bcb68mmacy	HELP_RECEIVE,
161a9bcb68mmacy	HELP_RENAME,
162a9bcb68mmacy	HELP_ROLLBACK,
163a9bcb68mmacy	HELP_SEND,
164a9bcb68mmacy	HELP_SET,
165a9bcb68mmacy	HELP_SHARE,
166a9bcb68mmacy	HELP_SNAPSHOT,
167a9bcb68mmacy	HELP_UNMOUNT,
168a9bcb68mmacy	HELP_UNSHARE,
169a9bcb68mmacy	HELP_ALLOW,
170a9bcb68mmacy	HELP_UNALLOW,
171a9bcb68mmacy	HELP_USERSPACE,
172a9bcb68mmacy	HELP_GROUPSPACE,
173a9bcb68mmacy	HELP_PROJECTSPACE,
174a9bcb68mmacy	HELP_PROJECT,
175a9bcb68mmacy	HELP_HOLD,
176a9bcb68mmacy	HELP_HOLDS,
177a9bcb68mmacy	HELP_RELEASE,
178a9bcb68mmacy	HELP_DIFF,
179a9bcb68mmacy	HELP_BOOKMARK,
180a9bcb68mmacy	HELP_CHANNEL_PROGRAM,
181a9bcb68mmacy	HELP_LOAD_KEY,
182a9bcb68mmacy	HELP_UNLOAD_KEY,
183a9bcb68mmacy	HELP_CHANGE_KEY,
184a9bcb68mmacy	HELP_VERSION,
185a9bcb68mmacy	HELP_REDACT,
186a9bcb68mmacy	HELP_JAIL,
187a9bcb68mmacy	HELP_UNJAIL,
188a9bcb68mmacy	HELP_WAIT,
189a9bcb68mmacy} zfs_help_t;
190a9bcb68mmacy
191a9bcb68mmacytypedef struct zfs_command {
192a9bcb68mmacy	const char	*name;
193a9bcb68mmacy	int		(*func)(int argc, char **argv);
194a9bcb68mmacy	zfs_help_t	usage;
195a9bcb68mmacy} zfs_command_t;
196a9bcb68mmacy
197a9bcb68mmacy/*
198a9bcb68mmacy * Master command table.  Each ZFS command has a name, associated function, and
199a9bcb68mmacy * usage message.  The usage messages need to be internationalized, so we have
200a9bcb68mmacy * to have a function to return the usage message based on a command index.
201a9bcb68mmacy *
202a9bcb68mmacy * These commands are organized according to how they are displayed in the usage
203a9bcb68mmacy * message.  An empty command (one with a NULL name) indicates an empty line in
204a9bcb68mmacy * the generic usage message.
205a9bcb68mmacy */
206a9bcb68mmacystatic zfs_command_t command_table[] = {
207a9bcb68mmacy	{ "version",	zfs_do_version, 	HELP_VERSION		},
208a9bcb68mmacy	{ NULL },
209a9bcb68mmacy	{ "create",	zfs_do_create,		HELP_CREATE		},
210a9bcb68mmacy	{ "destroy",	zfs_do_destroy,		HELP_DESTROY		},
211a9bcb68mmacy	{ NULL },
212a9bcb68mmacy	{ "snapshot",	zfs_do_snapshot,	HELP_SNAPSHOT		},
213a9bcb68mmacy	{ "rollback",	zfs_do_rollback,	HELP_ROLLBACK		},
214a9bcb68mmacy	{ "clone",	zfs_do_clone,		HELP_CLONE		},
215a9bcb68mmacy	{ "promote",	zfs_do_promote,		HELP_PROMOTE		},
216a9bcb68mmacy	{ "rename",	zfs_do_rename,		HELP_RENAME		},
217a9bcb68mmacy	{ "bookmark",	zfs_do_bookmark,	HELP_BOOKMARK		},
218a9bcb68mmacy	{ "program",    zfs_do_channel_program, HELP_CHANNEL_PROGRAM    },
219a9bcb68mmacy	{ NULL },
220a9bcb68mmacy	{ "list",	zfs_do_list,		HELP_LIST		},
221a9bcb68mmacy	{ NULL },
222a9bcb68mmacy	{ "set",	zfs_do_set,		HELP_SET		},
223a9bcb68mmacy	{ "get",	zfs_do_get,		HELP_GET		},
224a9bcb68mmacy	{ "inherit",	zfs_do_inherit,		HELP_INHERIT		},
225a9bcb68mmacy	{ "upgrade",	zfs_do_upgrade,		HELP_UPGRADE		},
226a9bcb68mmacy	{ NULL },
227a9bcb68mmacy	{ "userspace",	zfs_do_userspace,	HELP_USERSPACE		},
228a9bcb68mmacy	{ "groupspace",	zfs_do_userspace,	HELP_GROUPSPACE		},
229a9bcb68mmacy	{ "projectspace", zfs_do_userspace,	HELP_PROJECTSPACE	},
230a9bcb68mmacy	{ NULL },
231a9bcb68mmacy	{ "project",	zfs_do_project,		HELP_PROJECT		},
232a9bcb68mmacy	{ NULL },
233a9bcb68mmacy	{ "mount",	zfs_do_mount,		HELP_MOUNT		},
234a9bcb68mmacy	{ "unmount",	zfs_do_unmount,		HELP_UNMOUNT		},
235a9bcb68mmacy	{ "share",	zfs_do_share,		HELP_SHARE		},
236a9bcb68mmacy	{ "unshare",	zfs_do_unshare,		HELP_UNSHARE		},
237a9bcb68mmacy	{ NULL },
238a9bcb68mmacy	{ "send",	zfs_do_send,		HELP_SEND		},
239a9bcb68mmacy	{ "receive",	zfs_do_receive,		HELP_RECEIVE		},
240a9bcb68mmacy	{ NULL },
241a9bcb68mmacy	{ "allow",	zfs_do_allow,		HELP_ALLOW		},
242a9bcb68mmacy	{ NULL },
243a9bcb68mmacy	{ "unallow",	zfs_do_unallow,		HELP_UNALLOW		},
244a9bcb68mmacy	{ NULL },
245a9bcb68mmacy	{ "hold",	zfs_do_hold,		HELP_HOLD		},
246a9bcb68mmacy	{ "holds",	zfs_do_holds,		HELP_HOLDS		},
247a9bcb68mmacy	{ "release",	zfs_do_release,		HELP_RELEASE		},
248a9bcb68mmacy	{ "diff",	zfs_do_diff,		HELP_DIFF		},
249a9bcb68mmacy	{ "load-key",	zfs_do_load_key,	HELP_LOAD_KEY		},
250a9bcb68mmacy	{ "unload-key",	zfs_do_unload_key,	HELP_UNLOAD_KEY		},
251a9bcb68mmacy	{ "change-key",	zfs_do_change_key,	HELP_CHANGE_KEY		},
252a9bcb68mmacy	{ "redact",	zfs_do_redact,		HELP_REDACT		},
253a9bcb68mmacy	{ "wait",	zfs_do_wait,		HELP_WAIT		},
254a9bcb68mmacy
255a9bcb68mmacy#ifdef __FreeBSD__
256a9bcb68mmacy	{ "jail",	zfs_do_jail,		HELP_JAIL		},
257a9bcb68mmacy	{ "unjail",	zfs_do_unjail,		HELP_UNJAIL		},
258a9bcb68mmacy#endif
259a9bcb68mmacy};
260a9bcb68mmacy
261a9bcb68mmacy#define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
262a9bcb68mmacy
263a9bcb68mmacyzfs_command_t *current_command;
264a9bcb68mmacy
265a9bcb68mmacystatic const char *
266a9bcb68mmacyget_usage(zfs_help_t idx)
267a9bcb68mmacy{
268a9bcb68mmacy	switch (idx) {
269a9bcb68mmacy	case HELP_CLONE:
270a9bcb68mmacy		return (gettext("\tclone [-p] [-o property=value] ... "
271a9bcb68mmacy		    "<snapshot> <filesystem|volume>\n"));
272a9bcb68mmacy	case HELP_CREATE:
273a9bcb68mmacy		return (gettext("\tcreate [-Pnpv] [-o property=value] ... "
274a9bcb68mmacy		    "<filesystem>\n"
275a9bcb68mmacy		    "\tcreate [-Pnpsv] [-b blocksize] [-o property=value] ... "
276a9bcb68mmacy		    "-V <size> <volume>\n"));
277a9bcb68mmacy	case HELP_DESTROY:
278a9bcb68mmacy		return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
279a9bcb68mmacy		    "\tdestroy [-dnpRrv] "
280a9bcb68mmacy		    "<filesystem|volume>@<snap>[%<snap>][,...]\n"
281a9bcb68mmacy		    "\tdestroy <filesystem|volume>#<bookmark>\n"));
282a9bcb68mmacy	case HELP_GET:
283a9bcb68mmacy		return (gettext("\tget [-rHp] [-d max] "
284a9bcb68mmacy		    "[-o \"all\" | field[,...]]\n"
285a9bcb68mmacy		    "\t    [-t type[,...]] [-s source[,...]]\n"
286a9bcb68mmacy		    "\t    <\"all\" | property[,...]> "
287a9bcb68mmacy		    "[filesystem|volume|snapshot|bookmark] ...\n"));
288a9bcb68mmacy	case HELP_INHERIT:
289a9bcb68mmacy		return (gettext("\tinherit [-rS] <property> "
290a9bcb68mmacy		    "<filesystem|volume|snapshot> ...\n"));
291a9bcb68mmacy	case HELP_UPGRADE:
292a9bcb68mmacy		return (gettext("\tupgrade [-v]\n"
293a9bcb68mmacy		    "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
294a9bcb68mmacy	case HELP_LIST:
295a9bcb68mmacy		return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
296a9bcb68mmacy		    "[-s property]...\n\t    [-S property]... [-t type[,...]] "
297a9bcb68mmacy		    "[filesystem|volume|snapshot] ...\n"));
298a9bcb68mmacy	case HELP_MOUNT:
299a9bcb68mmacy		return (gettext("\tmount\n"
300a9bcb68mmacy		    "\tmount [-flvO] [-o opts] <-a | filesystem>\n"));
301a9bcb68mmacy	case HELP_PROMOTE:
302a9bcb68mmacy		return (gettext("\tpromote <clone-filesystem>\n"));
303a9bcb68mmacy	case HELP_RECEIVE:
304a9bcb68mmacy		return (gettext("\treceive [-vMnsFhu] "
305a9bcb68mmacy		    "[-o <property>=<value>] ... [-x <property>] ...\n"
306a9bcb68mmacy		    "\t    <filesystem|volume|snapshot>\n"
307a9bcb68mmacy		    "\treceive [-vMnsFhu] [-o <property>=<value>] ... "
308a9bcb68mmacy		    "[-x <property>] ... \n"
309a9bcb68mmacy		    "\t    [-d | -e] <filesystem>\n"
310a9bcb68mmacy		    "\treceive -A <filesystem|volume>\n"));
311a9bcb68mmacy	case HELP_RENAME:
312a9bcb68mmacy		return (gettext("\trename [-f] <filesystem|volume|snapshot> "
313a9bcb68mmacy		    "<filesystem|volume|snapshot>\n"
314d7d48eemmacy		    "\trename -p [-f] <filesystem|volume> <filesystem|volume>\n"
315d7d48eemmacy		    "\trename -u [-f] <filesystem> <filesystem>\n"
316a9bcb68mmacy		    "\trename -r <snapshot> <snapshot>\n"));
317a9bcb68mmacy	case HELP_ROLLBACK:
318a9bcb68mmacy		return (gettext("\trollback [-rRf] <snapshot>\n"));
319a9bcb68mmacy	case HELP_SEND:
320a9bcb68mmacy		return (gettext("\tsend [-DnPpRvLecwhb] [-[i|I] snapshot] "
321a9bcb68mmacy		    "<snapshot>\n"
322a9bcb68mmacy		    "\tsend [-nvPLecw] [-i snapshot|bookmark] "
323a9bcb68mmacy		    "<filesystem|volume|snapshot>\n"
324a9bcb68mmacy		    "\tsend [-DnPpvLec] [-i bookmark|snapshot] "
325a9bcb68mmacy		    "--redact <bookmark> <snapshot>\n"
326a9bcb68mmacy		    "\tsend [-nvPe] -t <receive_resume_token>\n"
327a9bcb68mmacy		    "\tsend [-Pnv] --saved filesystem\n"));
328a9bcb68mmacy	case HELP_SET:
329a9bcb68mmacy		return (gettext("\tset <property=value> ... "
330a9bcb68mmacy		    "<filesystem|volume|snapshot> ...\n"));
331a9bcb68mmacy	case HELP_SHARE:
332a9bcb68mmacy		return (gettext("\tshare [-l] <-a [nfs|smb] | filesystem>\n"));
333a9bcb68mmacy	case HELP_SNAPSHOT:
334a9bcb68mmacy		return (gettext("\tsnapshot [-r] [-o property=value] ... "
335a9bcb68mmacy		    "<filesystem|volume>@<snap> ...\n"));
336a9bcb68mmacy	case HELP_UNMOUNT:
337a9bcb68mmacy		return (gettext("\tunmount [-fu] "
338a9bcb68mmacy		    "<-a | filesystem|mountpoint>\n"));
339a9bcb68mmacy	case HELP_UNSHARE:
340a9bcb68mmacy		return (gettext("\tunshare "
341a9bcb68mmacy		    "<-a [nfs|smb] | filesystem|mountpoint>\n"));
342a9bcb68mmacy	case HELP_ALLOW:
343a9bcb68mmacy		return (gettext("\tallow <filesystem|volume>\n"
344a9bcb68mmacy		    "\tallow [-ldug] "
345a9bcb68mmacy		    "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
346a9bcb68mmacy		    "\t    <filesystem|volume>\n"
347a9bcb68mmacy		    "\tallow [-ld] -e <perm|@setname>[,...] "
348a9bcb68mmacy		    "<filesystem|volume>\n"
349a9bcb68mmacy		    "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
350a9bcb68mmacy		    "\tallow -s @setname <perm|@setname>[,...] "
351a9bcb68mmacy		    "<filesystem|volume>\n"));
352a9bcb68mmacy	case HELP_UNALLOW:
353a9bcb68mmacy		return (gettext("\tunallow [-rldug] "
354a9bcb68mmacy		    "<\"everyone\"|user|group>[,...]\n"
355a9bcb68mmacy		    "\t    [<perm|@setname>[,...]] <filesystem|volume>\n"
356a9bcb68mmacy		    "\tunallow [-rld] -e [<perm|@setname>[,...]] "
357a9bcb68mmacy		    "<filesystem|volume>\n"
358a9bcb68mmacy		    "\tunallow [-r] -c [<perm|@setname>[,...]] "
359a9bcb68mmacy		    "<filesystem|volume>\n"
360a9bcb68mmacy		    "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
361a9bcb68mmacy		    "<filesystem|volume>\n"));
362a9bcb68mmacy	case HELP_USERSPACE:
363a9bcb68mmacy		return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
364a9bcb68mmacy		    "[-s field] ...\n"
365a9bcb68mmacy		    "\t    [-S field] ... [-t type[,...]] "
366f7a21c3mmacy		    "<filesystem|snapshot|path>\n"));
367a9bcb68mmacy	case HELP_GROUPSPACE:
368a9bcb68mmacy		return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
369a9bcb68mmacy		    "[-s field] ...\n"
370a9bcb68mmacy		    "\t    [-S field] ... [-t type[,...]] "
371f7a21c3mmacy		    "<filesystem|snapshot|path>\n"));
372a9bcb68mmacy	case HELP_PROJECTSPACE:
373a9bcb68mmacy		return (gettext("\tprojectspace [-Hp] [-o field[,...]] "
374a9bcb68mmacy		    "[-s field] ... \n"
375f7a21c3mmacy		    "\t    [-S field] ... <filesystem|snapshot|path>\n"));
376a9bcb68mmacy	case HELP_PROJECT:
377a9bcb68mmacy		return (gettext("\tproject [-d|-r] <directory|file ...>\n"
378a9bcb68mmacy		    "\tproject -c [-0] [-d|-r] [-p id] <directory|file ...>\n"
379a9bcb68mmacy		    "\tproject -C [-k] [-r] <directory ...>\n"
380a9bcb68mmacy		    "\tproject [-p id] [-r] [-s] <directory ...>\n"));
381a9bcb68mmacy	case HELP_HOLD:
382a9bcb68mmacy		return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
383a9bcb68mmacy	case HELP_HOLDS:
384a9bcb68mmacy		return (gettext("\tholds [-rH] <snapshot> ...\n"));
385a9bcb68mmacy	case HELP_RELEASE:
386a9bcb68mmacy		return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
387a9bcb68mmacy	case HELP_DIFF:
388a9bcb68mmacy		return (gettext("\tdiff [-FHt] <snapshot> "
389a9bcb68mmacy		    "[snapshot|filesystem]\n"));
390a9bcb68mmacy	case HELP_BOOKMARK:
391a9bcb68mmacy		return (gettext("\tbookmark <snapshot|bookmark> "
392a9bcb68mmacy		    "<newbookmark>\n"));
393a9bcb68mmacy	case HELP_CHANNEL_PROGRAM:
394a9bcb68mmacy		return (gettext("\tprogram [-jn] [-t <instruction limit>] "
395a9bcb68mmacy		    "[-m <memory limit (b)>]\n"
396a9bcb68mmacy		    "\t    <pool> <program file> [lua args...]\n"));
397a9bcb68mmacy	case HELP_LOAD_KEY:
398a9bcb68mmacy		return (gettext("\tload-key [-rn] [-L <keylocation>] "
399a9bcb68mmacy		    "<-a | filesystem|volume>\n"));
400a9bcb68mmacy	case HELP_UNLOAD_KEY:
401a9bcb68mmacy		return (gettext("\tunload-key [-r] "
402a9bcb68mmacy		    "<-a | filesystem|volume>\n"));
403a9bcb68mmacy	case HELP_CHANGE_KEY:
404a9bcb68mmacy		return (gettext("\tchange-key [-l] [-o keyformat=<value>]\n"
405d7d48eemmacy		    "\t    [-o keylocation=<value>] [-o pbkdf2iters=<value>]\n"
406a9bcb68mmacy		    "\t    <filesystem|volume>\n"
407a9bcb68mmacy		    "\tchange-key -i [-l] <filesystem|volume>\n"));
408a9bcb68mmacy	case HELP_VERSION:
409a9bcb68mmacy		return (gettext("\tversion\n"));
410a9bcb68mmacy	case HELP_REDACT:
411a9bcb68mmacy		return (gettext("\tredact <snapshot> <bookmark> "
412a9bcb68mmacy		    "<redaction_snapshot> ...\n"));
413a9bcb68mmacy	case HELP_JAIL:
414a9bcb68mmacy		return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
415a9bcb68mmacy	case HELP_UNJAIL:
416a9bcb68mmacy		return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
417a9bcb68mmacy	case HELP_WAIT:
418a9bcb68mmacy		return (gettext("\twait [-t <activity>] <filesystem>\n"));
419a9bcb68mmacy	}
420a9bcb68mmacy
421a9bcb68mmacy	abort();
422a9bcb68mmacy	/* NOTREACHED */
423a9bcb68mmacy}
424a9bcb68mmacy
425a9bcb68mmacyvoid
426a9bcb68mmacynomem(void)
427a9bcb68mmacy{
428a9bcb68mmacy	(void) fprintf(stderr, gettext("internal error: out of memory\n"));
429a9bcb68mmacy	exit(1);
430a9bcb68mmacy}
431a9bcb68mmacy
432a9bcb68mmacy/*
433a9bcb68mmacy * Utility function to guarantee malloc() success.
434a9bcb68mmacy */
435a9bcb68mmacy
436a9bcb68mmacyvoid *
437a9bcb68mmacysafe_malloc(size_t size)
438a9bcb68mmacy{
439a9bcb68mmacy	void *data;
440a9bcb68mmacy
441a9bcb68mmacy	if ((data = calloc(1, size)) == NULL)
442a9bcb68mmacy		nomem();
443a9bcb68mmacy
444a9bcb68mmacy	return (data);
445a9bcb68mmacy}
446a9bcb68mmacy
447a9bcb68mmacystatic void *
448a9bcb68mmacysafe_realloc(void *data, size_t size)
449a9bcb68mmacy{
450a9bcb68mmacy	void *newp;
451a9bcb68mmacy	if ((newp = realloc(data, size)) == NULL) {
452a9bcb68mmacy		free(data);
453a9bcb68mmacy		nomem();
454a9bcb68mmacy	}
455a9bcb68mmacy
456a9bcb68mmacy	return (newp);
457a9bcb68mmacy}
458a9bcb68mmacy
459a9bcb68mmacystatic char *
460a9bcb68mmacysafe_strdup(char *str)
461a9bcb68mmacy{
462a9bcb68mmacy	char *dupstr = strdup(str);
463a9bcb68mmacy
464a9bcb68mmacy	if (dupstr == NULL)
465a9bcb68mmacy		nomem();
466a9bcb68mmacy
467a9bcb68mmacy	return (dupstr);
468a9bcb68mmacy}
469a9bcb68mmacy
470a9bcb68mmacy/*
471a9bcb68mmacy * Callback routine that will print out information for each of
472a9bcb68mmacy * the properties.
473a9bcb68mmacy */
474a9bcb68mmacystatic int
475a9bcb68mmacyusage_prop_cb(int prop, void *cb)
476a9bcb68mmacy{
477a9bcb68mmacy	FILE *fp = cb;
478a9bcb68mmacy
479a9bcb68mmacy	(void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
480a9bcb68mmacy
481a9bcb68mmacy	if (zfs_prop_readonly(prop))
482a9bcb68mmacy		(void) fprintf(fp, " NO    ");
483a9bcb68mmacy	else
484a9bcb68mmacy		(void) fprintf(fp, "YES    ");
485a9bcb68mmacy
486a9bcb68mmacy	if (zfs_prop_inheritable(prop))
487a9bcb68mmacy		(void) fprintf(fp, "  YES   ");
488a9bcb68mmacy	else
489a9bcb68mmacy		(void) fprintf(fp, "   NO   ");
490a9bcb68mmacy
491a9bcb68mmacy	if (zfs_prop_values(prop) == NULL)
492a9bcb68mmacy		(void) fprintf(fp, "-\n");
493a9bcb68mmacy	else
494a9bcb68mmacy		(void) fprintf(fp, "%s\n", zfs_prop_values(prop));
495a9bcb68mmacy
496a9bcb68mmacy	return (ZPROP_CONT);
497a9bcb68mmacy}
498a9bcb68mmacy
499a9bcb68mmacy/*
500a9bcb68mmacy * Display usage message.  If we're inside a command, display only the usage for
501a9bcb68mmacy * that command.  Otherwise, iterate over the entire command table and display
502a9bcb68mmacy * a complete usage message.
503a9bcb68mmacy */
504a9bcb68mmacystatic void
505a9bcb68mmacyusage(boolean_t requested)
506a9bcb68mmacy{
507a9bcb68mmacy	int i;
508a9bcb68mmacy	boolean_t show_properties = B_FALSE;
509a9bcb68mmacy	FILE *fp = requested ? stdout : stderr;
510a9bcb68mmacy
511a9bcb68mmacy	if (current_command == NULL) {
512a9bcb68mmacy
513a9bcb68mmacy		(void) fprintf(fp, gettext("usage: zfs command args ...\n"));
514a9bcb68mmacy		(void) fprintf(fp,
515a9bcb68mmacy		    gettext("where 'command' is one of the following:\n\n"));
516a9bcb68mmacy
517a9bcb68mmacy		for (i = 0; i < NCOMMAND; i++) {
518a9bcb68mmacy			if (command_table[i].name == NULL)
519a9bcb68mmacy				(void) fprintf(fp, "\n");
520a9bcb68mmacy			else
521a9bcb68mmacy				(void) fprintf(fp, "%s",
522a9bcb68mmacy				    get_usage(command_table[i].usage));
523a9bcb68mmacy		}
524a9bcb68mmacy
525a9bcb68mmacy		(void) fprintf(fp, gettext("\nEach dataset is of the form: "
526a9bcb68mmacy		    "pool/[dataset/]*dataset[@name]\n"));
527a9bcb68mmacy	} else {
528a9bcb68mmacy		(void) fprintf(fp, gettext("usage:\n"));
529a9bcb68mmacy		(void) fprintf(fp, "%s", get_usage(current_command->usage));
530a9bcb68mmacy	}
531a9bcb68mmacy
532a9bcb68mmacy	if (current_command != NULL &&
533a9bcb68mmacy	    (strcmp(current_command->name, "set") == 0 ||
534a9bcb68mmacy	    strcmp(current_command->name, "get") == 0 ||
535a9bcb68mmacy	    strcmp(current_command->name, "inherit") == 0 ||
536a9bcb68mmacy	    strcmp(current_command->name, "list") == 0))
537a9bcb68mmacy		show_properties = B_TRUE;
538a9bcb68mmacy
539a9bcb68mmacy	if (show_properties) {
540a9bcb68mmacy		(void) fprintf(fp,
541a9bcb68mmacy		    gettext("\nThe following properties are supported:\n"));
542a9bcb68mmacy
543a9bcb68mmacy		(void) fprintf(fp, "\n\t%-14s %s  %s   %s\n\n",
544a9bcb68mmacy		    "PROPERTY", "EDIT", "INHERIT", "VALUES");
545a9bcb68mmacy
546a9bcb68mmacy		/* Iterate over all properties */
547a9bcb68mmacy		(void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
548a9bcb68mmacy		    ZFS_TYPE_DATASET);
549a9bcb68mmacy
550a9bcb68mmacy		(void) fprintf(fp, "\t%-15s ", "userused@...");
551a9bcb68mmacy		(void) fprintf(fp, " NO       NO   <size>\n");
552a9bcb68mmacy		(void) fprintf(fp, "\t%-15s ", "groupused@...");
553a9bcb68mmacy		(void) fprintf(fp, " NO       NO   <size>\n");
554a9bcb68mmacy		(void) fprintf(fp, "\t%-15s ", "projectused@...");
555a9bcb68mmacy		(void) fprintf(fp, " NO       NO   <size>\n");
556a9bcb68mmacy		(void) fprintf(fp, "\t%-15s ", "userobjused@...");
557a9bcb68mmacy		(void) fprintf(fp, " NO       NO   <size>\n");
558a9bcb68mmacy		(void) fprintf(fp, "\t%-15s ", "groupobjused@...");
559a9bcb68mmacy		(void) fprintf(fp, " NO       NO   <size>\n");
560a9bcb68mmacy		(void) fprintf(fp, "\t%-15s ", "projectobjused@...");
561a9bcb68mmacy		(void) fprintf(fp, " NO       NO   <size>\n");
562a9bcb68mmacy		(void) fprintf(fp, "\t%-15s ", "userquota@...");
563a9bcb68mmacy		(void) fprintf(fp, "YES       NO   <size> | none\n");
564a9bcb68mmacy		(void) fprintf(fp, "\t%-15s ", "groupquota@...");
565a9bcb68mmacy		(void) fprintf(fp, "YES       NO   <size> | none\n");
566a9bcb68mmacy		(void) fprintf(fp, "\t%-15s ", "projectquota@...");
567a9bcb68mmacy		(void) fprintf(fp, "YES       NO   <size> | none\n");
568a9bcb68mmacy		(void) fprintf(fp, "\t%-15s ", "userobjquota@...");
569a9bcb68mmacy		(void) fprintf(fp, "YES       NO   <size> | none\n");
570a9bcb68mmacy		(void) fprintf(fp, "\t%-15s ", "groupobjquota@...");
571a9bcb68mmacy		(void) fprintf(fp, "YES       NO   <size> | none\n");
572a9bcb68mmacy		(void) fprintf(fp, "\t%-15s ", "projectobjquota@...");
573a9bcb68mmacy		(void) fprintf(fp, "YES       NO   <size> | none\n");
574a9bcb68mmacy		(void) fprintf(fp, "\t%-15s ", "written@<snap>");
575a9bcb68mmacy		(void) fprintf(fp, " NO       NO   <size>\n");
576a9bcb68mmacy		(void) fprintf(fp, "\t%-15s ", "written#<bookmark>");
577a9bcb68mmacy		(void) fprintf(fp, " NO       NO   <size>\n");
578a9bcb68mmacy
579a9bcb68mmacy		(void) fprintf(fp, gettext("\nSizes are specified in bytes "
580a9bcb68mmacy		    "with standard units such as K, M, G, etc.\n"));
581a9bcb68mmacy		(void) fprintf(fp, gettext("\nUser-defined properties can "
582a9bcb68mmacy		    "be specified by using a name containing a colon (:).\n"));
583a9bcb68mmacy		(void) fprintf(fp, gettext("\nThe {user|group|project}"
584a9bcb68mmacy		    "[obj]{used|quota}@ properties must be appended with\n"
585a9bcb68mmacy		    "a user|group|project specifier of one of these forms:\n"
586a9bcb68mmacy		    "    POSIX name      (eg: \"matt\")\n"
587a9bcb68mmacy		    "    POSIX id        (eg: \"126829\")\n"
588a9bcb68mmacy		    "    SMB name@domain (eg: \"matt@sun\")\n"
589a9bcb68mmacy		    "    SMB SID         (eg: \"S-1-234-567-89\")\n"));
590a9bcb68mmacy	} else {
591a9bcb68mmacy		(void) fprintf(fp,
592a9bcb68mmacy		    gettext("\nFor the property list, run: %s\n"),
593a9bcb68mmacy		    "zfs set|get");
594a9bcb68mmacy		(void) fprintf(fp,
595a9bcb68mmacy		    gettext("\nFor the delegated permission list, run: %s\n"),
596a9bcb68mmacy		    "zfs allow|unallow");
597a9bcb68mmacy	}
598a9bcb68mmacy
599a9bcb68mmacy	/*
600a9bcb68mmacy	 * See comments at end of main().
601a9bcb68mmacy	 */
602a9bcb68mmacy	if (getenv("ZFS_ABORT") != NULL) {
603a9bcb68mmacy		(void) printf("dumping core by request\n");
604a9bcb68mmacy		abort();
605a9bcb68mmacy	}
606a9bcb68mmacy
607a9bcb68mmacy	exit(requested ? 0 : 2);
608a9bcb68mmacy}
609a9bcb68mmacy
610a9bcb68mmacy/*
611a9bcb68mmacy * Take a property=value argument string and add it to the given nvlist.
612a9bcb68mmacy * Modifies the argument inplace.
613a9bcb68mmacy */
614a9bcb68mmacystatic boolean_t
615a9bcb68mmacyparseprop(nvlist_t *props, char *propname)
616a9bcb68mmacy{
617a9bcb68mmacy	char *propval;
618a9bcb68mmacy
619a9bcb68mmacy	if ((propval = strchr(propname, '=')) == NULL) {
620a9bcb68mmacy		(void) fprintf(stderr, gettext("missing "
621a9bcb68mmacy		    "'=' for property=value argument\n"));
622a9bcb68mmacy		return (B_FALSE);
623a9bcb68mmacy	}
624a9bcb68mmacy	*propval = '\0';
625a9bcb68mmacy	propval++;
626a9bcb68mmacy	if (nvlist_exists(props, propname)) {
627a9bcb68mmacy		(void) fprintf(stderr, gettext("property '%s' "
628a9bcb68mmacy		    "specified multiple times\n"), propname);
629a9bcb68mmacy		return (B_FALSE);
630a9bcb68mmacy	}
631a9bcb68mmacy	if (nvlist_add_string(props, propname, propval) != 0)
632a9bcb68mmacy		nomem();
633a9bcb68mmacy	return (B_TRUE);
634a9bcb68mmacy}
635a9bcb68mmacy
636a9bcb68mmacy/*
637a9bcb68mmacy * Take a property name argument and add it to the given nvlist.
638a9bcb68mmacy * Modifies the argument inplace.
639a9bcb68mmacy */
640a9bcb68mmacystatic boolean_t
641a9bcb68mmacyparsepropname(nvlist_t *props, char *propname)
642a9bcb68mmacy{
643a9bcb68mmacy	if (strchr(propname, '=') != NULL) {
644a9bcb68mmacy		(void) fprintf(stderr, gettext("invalid character "
645a9bcb68mmacy		    "'=' in property argument\n"));
646a9bcb68mmacy		return (B_FALSE);
647a9bcb68mmacy	}
648a9bcb68mmacy	if (nvlist_exists(props, propname)) {
649a9bcb68mmacy		(void) fprintf(stderr, gettext("property '%s' "
650a9bcb68mmacy		    "specified multiple times\n"), propname);
651a9bcb68mmacy		return (B_FALSE);
652a9bcb68mmacy	}
653a9bcb68mmacy	if (nvlist_add_boolean(props, propname) != 0)
654a9bcb68mmacy		nomem();
655a9bcb68mmacy	return (B_TRUE);
656a9bcb68mmacy}
657a9bcb68mmacy
658a9bcb68mmacystatic int
659a9bcb68mmacyparse_depth(char *opt, int *flags)
660a9bcb68mmacy{
661a9bcb68mmacy	char *tmp;
662a9bcb68mmacy	int depth;
663a9bcb68mmacy
664a9bcb68mmacy	depth = (int)strtol(opt, &tmp, 0);
665a9bcb68mmacy	if (*tmp) {
666a9bcb68mmacy		(void) fprintf(stderr,
667a9bcb68mmacy		    gettext("%s is not an integer\n"), optarg);
668a9bcb68mmacy		usage(B_FALSE);
669a9bcb68mmacy	}
670a9bcb68mmacy	if (depth < 0) {
671a9bcb68mmacy		(void) fprintf(stderr,
672a9bcb68mmacy		    gettext("Depth can not be negative.\n"));
673a9bcb68mmacy		usage(B_FALSE);
674a9bcb68mmacy	}
675a9bcb68mmacy	*flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
676a9bcb68mmacy	return (depth);
677a9bcb68mmacy}
678a9bcb68mmacy
679a9bcb68mmacy#define	PROGRESS_DELAY 2		/* seconds */
680a9bcb68mmacy
681a9bcb68mmacystatic 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";
682a9bcb68mmacystatic time_t pt_begin;
683a9bcb68mmacystatic char *pt_header = NULL;
684a9bcb68mmacystatic boolean_t pt_shown;
685a9bcb68mmacy
686a9bcb68mmacystatic void
687a9bcb68mmacystart_progress_timer(void)
688a9bcb68mmacy{
689a9bcb68mmacy	pt_begin = time(NULL) + PROGRESS_DELAY;
690a9bcb68mmacy	pt_shown = B_FALSE;
691a9bcb68mmacy}
692a9bcb68mmacy
693a9bcb68mmacystatic void
694a9bcb68mmacyset_progress_header(char *header)
695a9bcb68mmacy{
696a9bcb68mmacy	assert(pt_header == NULL);
697a9bcb68mmacy	pt_header = safe_strdup(header);
698a9bcb68mmacy	if (pt_shown) {
699a9bcb68mmacy		(void) printf("%s: ", header);
700a9bcb68mmacy		(void) fflush(stdout);
701a9bcb68mmacy	}
702a9bcb68mmacy}
703a9bcb68mmacy
704a9bcb68mmacystatic void
705a9bcb68mmacyupdate_progress(char *update)
706a9bcb68mmacy{
707a9bcb68mmacy	if (!pt_shown && time(NULL) > pt_begin) {
708a9bcb68mmacy		int len = strlen(update);
709a9bcb68mmacy
710a9bcb68mmacy		(void) printf("%s: %s%*.*s", pt_header, update, len, len,
711a9bcb68mmacy		    pt_reverse);
712a9bcb68mmacy		(void) fflush(stdout);
713a9bcb68mmacy		pt_shown = B_TRUE;
714a9bcb68mmacy	} else if (pt_shown) {
715a9bcb68mmacy		int len = strlen(update);
716a9bcb68mmacy
717a9bcb68mmacy		(void) printf("%s%*.*s", update, len, len, pt_reverse);
718a9bcb68mmacy		(void) fflush(stdout);
719a9bcb68mmacy	}
720a9bcb68mmacy}
721a9bcb68mmacy
722a9bcb68mmacystatic void
723a9bcb68mmacyfinish_progress(char *done)
724a9bcb68mmacy{
725a9bcb68mmacy	if (pt_shown) {
726a9bcb68mmacy		(void) printf("%s\n", done);
727a9bcb68mmacy		(void) fflush(stdout);
728a9bcb68mmacy	}
729a9bcb68mmacy	free(pt_header);
730a9bcb68mmacy	pt_header = NULL;
731a9bcb68mmacy}
732a9bcb68mmacy
733a9bcb68mmacystatic int
734a9bcb68mmacyzfs_mount_and_share(libzfs_handle_t *hdl, const char *dataset, zfs_type_t type)
735a9bcb68mmacy{
736a9bcb68mmacy	zfs_handle_t *zhp = NULL;
737a9bcb68mmacy	int ret = 0;
738a9bcb68mmacy
739a9bcb68mmacy	zhp = zfs_open(hdl, dataset, type);
740a9bcb68mmacy	if (zhp == NULL)
741a9bcb68mmacy		return (1);
742a9bcb68mmacy
743a9bcb68mmacy	/*
744a9bcb68mmacy	 * Volumes may neither be mounted or shared.  Potentially in the
745a9bcb68mmacy	 * future filesystems detected on these volumes could be mounted.
746a9bcb68mmacy	 */
747a9bcb68mmacy	if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
748a9bcb68mmacy		zfs_close(zhp);
749a9bcb68mmacy		return (0);
750a9bcb68mmacy	}
751a9bcb68mmacy
752a9bcb68mmacy	/*
753a9bcb68mmacy	 * Mount and/or share the new filesystem as appropriate.  We provide a
754a9bcb68mmacy	 * verbose error message to let the user know that their filesystem was
755a9bcb68mmacy	 * in fact created, even if we failed to mount or share it.
756a9bcb68mmacy	 *
757a9bcb68mmacy	 * If the user doesn't want the dataset automatically mounted, then
758a9bcb68mmacy	 * skip the mount/share step
759a9bcb68mmacy	 */
760a9bcb68mmacy	if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type, B_FALSE) &&
761a9bcb68mmacy	    zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON) {
762a9bcb68mmacy		if (zfs_mount_delegation_check()) {
763a9bcb68mmacy			(void) fprintf(stderr, gettext("filesystem "
764a9bcb68mmacy			    "successfully created, but it may only be "
765a9bcb68mmacy			    "mounted by root\n"));
766a9bcb68mmacy			ret = 1;
767a9bcb68mmacy		} else if (zfs_mount(zhp, NULL, 0) != 0) {
768a9bcb68mmacy			(void) fprintf(stderr, gettext("filesystem "
769a9bcb68mmacy			    "successfully created, but not mounted\n"));
770a9bcb68mmacy			ret = 1;
771a9bcb68mmacy		} else if (zfs_share(zhp) != 0) {
772a9bcb68mmacy			(void) fprintf(stderr, gettext("filesystem "
773a9bcb68mmacy			    "successfully created, but not shared\n"));
774a9bcb68mmacy			ret = 1;
775a9bcb68mmacy		}
776a9bcb68mmacy		zfs_commit_all_shares();
777a9bcb68mmacy	}
778a9bcb68mmacy
779a9bcb68mmacy	zfs_close(zhp);
780a9bcb68mmacy
781a9bcb68mmacy	return (ret);
782a9bcb68mmacy}
783a9bcb68mmacy
784a9bcb68mmacy/*
785a9bcb68mmacy * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
786a9bcb68mmacy *
787a9bcb68mmacy * Given an existing dataset, create a writable copy whose initial contents
788a9bcb68mmacy * are the same as the source.  The newly created dataset maintains a
789a9bcb68mmacy * dependency on the original; the original cannot be destroyed so long as
790a9bcb68mmacy * the clone exists.
791a9bcb68mmacy *
792a9bcb68mmacy * The '-p' flag creates all the non-existing ancestors of the target first.
793a9bcb68mmacy */
794a9bcb68mmacystatic int
795a9bcb68mmacyzfs_do_clone(int argc, char **argv)
796a9bcb68mmacy{
797a9bcb68mmacy	zfs_handle_t *zhp = NULL;
798a9bcb68mmacy	boolean_t parents = B_FALSE;
799a9bcb68mmacy	nvlist_t *props;
800a9bcb68mmacy	int ret = 0;
801a9bcb68mmacy	int c;
802a9bcb68mmacy
803a9bcb68mmacy	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
804a9bcb68mmacy		nomem();
805a9bcb68mmacy
806a9bcb68mmacy	/* check options */
807a9bcb68mmacy	while ((c = getopt(argc, argv, "o:p")) != -1) {
808a9bcb68mmacy		switch (c) {
809a9bcb68mmacy		case 'o':
810a9bcb68mmacy			if (!parseprop(props, optarg)) {
811a9bcb68mmacy				nvlist_free(props);
812a9bcb68mmacy				return (1);
813a9bcb68mmacy			}
814a9bcb68mmacy			break;
815a9bcb68mmacy		case 'p':
816a9bcb68mmacy			parents = B_TRUE;
817a9bcb68mmacy			break;
818a9bcb68mmacy		case '?':
819a9bcb68mmacy			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
820a9bcb68mmacy			    optopt);
821a9bcb68mmacy			goto usage;
822a9bcb68mmacy		}
823a9bcb68mmacy	}
824a9bcb68mmacy
825a9bcb68mmacy	argc -= optind;
826a9bcb68mmacy	argv += optind;
827a9bcb68mmacy
828a9bcb68mmacy	/* check number of arguments */
829a9bcb68mmacy	if (argc < 1) {
830a9bcb68mmacy		(void) fprintf(stderr, gettext("missing source dataset "
831a9bcb68mmacy		    "argument\n"));
832a9bcb68mmacy		goto usage;
833a9bcb68mmacy	}
834a9bcb68mmacy	if (argc < 2) {
835a9bcb68mmacy		(void) fprintf(stderr, gettext("missing target dataset "
836a9bcb68mmacy		    "argument\n"));
837a9bcb68mmacy		goto usage;
838a9bcb68mmacy	}
839a9bcb68mmacy	if (argc > 2) {
840a9bcb68mmacy		(void) fprintf(stderr, gettext("too many arguments\n"));
841a9bcb68mmacy		goto usage;
842a9bcb68mmacy	}
843a9bcb68mmacy
844a9bcb68mmacy	/* open the source dataset */
845a9bcb68mmacy	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) {
846a9bcb68mmacy		nvlist_free(props);
847a9bcb68mmacy		return (1);
848a9bcb68mmacy	}
849a9bcb68mmacy
850a9bcb68mmacy	if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
851a9bcb68mmacy	    ZFS_TYPE_VOLUME)) {
852a9bcb68mmacy		/*
853a9bcb68mmacy		 * Now create the ancestors of the target dataset.  If the
854a9bcb68mmacy		 * target already exists and '-p' option was used we should not
855a9bcb68mmacy		 * complain.
856a9bcb68mmacy		 */
857a9bcb68mmacy		if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
858a9bcb68mmacy		    ZFS_TYPE_VOLUME)) {
859a9bcb68mmacy			zfs_close(zhp);
860a9bcb68mmacy			nvlist_free(props);
861a9bcb68mmacy			return (0);
862a9bcb68mmacy		}
863a9bcb68mmacy		if (zfs_create_ancestors(g_zfs, argv[1]) != 0) {
864a9bcb68mmacy			zfs_close(zhp);
865a9bcb68mmacy			nvlist_free(props);
866a9bcb68mmacy			return (1);
867a9bcb68mmacy		}
868a9bcb68mmacy	}
869a9bcb68mmacy
870a9bcb68mmacy	/* pass to libzfs */
871a9bcb68mmacy	ret = zfs_clone(zhp, argv[1], props);
872a9bcb68mmacy
873a9bcb68mmacy	/* create the mountpoint if necessary */
874a9bcb68mmacy	if (ret == 0) {
875a9bcb68mmacy		if (log_history) {
876a9bcb68mmacy			(void) zpool_log_history(g_zfs, history_str);
877a9bcb68mmacy			log_history = B_FALSE;
878a9bcb68mmacy		}
879a9bcb68mmacy
880a9bcb68mmacy		ret = zfs_mount_and_share(g_zfs, argv[1], ZFS_TYPE_DATASET);
881a9bcb68mmacy	}
882a9bcb68mmacy
883a9bcb68mmacy	zfs_close(zhp);
884a9bcb68mmacy	nvlist_free(props);
885a9bcb68mmacy
886a9bcb68mmacy	return (!!ret);
887a9bcb68mmacy
888a9bcb68mmacyusage:
889a9bcb68mmacy	ASSERT3P(zhp, ==, NULL);
890a9bcb68mmacy	nvlist_free(props);
891a9bcb68mmacy	usage(B_FALSE);
892a9bcb68mmacy	return (-1);
893a9bcb68mmacy}
894a9bcb68mmacy
895a9bcb68mmacy/*
896a9bcb68mmacy * zfs create [-Pnpv] [-o prop=value] ... fs
897a9bcb68mmacy * zfs create [-Pnpsv] [-b blocksize] [-o prop=value] ... -V vol size
898a9bcb68mmacy *
899a9bcb68mmacy * Create a new dataset.  This command can be used to create filesystems
900a9bcb68mmacy * and volumes.  Snapshot creation is handled by 'zfs snapshot'.
901a9bcb68mmacy * For volumes, the user must specify a size to be used.
902a9bcb68mmacy *
903a9bcb68mmacy * The '-s' flag applies only to volumes, and indicates that we should not try
904a9bcb68mmacy * to set the reservation for this volume.  By default we set a reservation
905a9bcb68mmacy * equal to the size for any volume.  For pools with SPA_VERSION >=
906a9bcb68mmacy * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
907a9bcb68mmacy *
908a9bcb68mmacy * The '-p' flag creates all the non-existing ancestors of the target first.
909a9bcb68mmacy *
910a9bcb68mmacy * The '-n' flag is no-op (dry run) mode.  This will perform a user-space sanity
911a9bcb68mmacy * check of arguments and properties, but does not check for permissions,
912a9bcb68mmacy * available space, etc.
913a9bcb68mmacy *
914a9bcb68mmacy * The '-v' flag is for verbose output.
915a9bcb68mmacy *
916a9bcb68mmacy * The '-P' flag is used for parseable output.  It implies '-v'.
917a9bcb68mmacy */
918a9bcb68mmacystatic int
919a9bcb68mmacyzfs_do_create(int argc, char **argv)
920a9bcb68mmacy{
921a9bcb68mmacy	zfs_type_t type = ZFS_TYPE_FILESYSTEM;
922a9bcb68mmacy	zpool_handle_t *zpool_handle = NULL;
923a9bcb68mmacy	nvlist_t *real_props = NULL;
924a9bcb68mmacy	uint64_t volsize = 0;
925a9bcb68mmacy	int c;
926a9bcb68mmacy	boolean_t noreserve = B_FALSE;
927a9bcb68mmacy	boolean_t bflag = B_FALSE;
928a9bcb68mmacy	boolean_t parents = B_FALSE;
929a9bcb68mmacy	boolean_t dryrun = B_FALSE;
930a9bcb68mmacy	boolean_t verbose = B_FALSE;
931a9bcb68mmacy	boolean_t parseable = B_FALSE;
932a9bcb68mmacy	int ret = 1;
933a9bcb68mmacy	nvlist_t *props;
934a9bcb68mmacy	uint64_t intval;
935a9bcb68mmacy
936a9bcb68mmacy	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
937a9bcb68mmacy		nomem();
938a9bcb68mmacy
939a9bcb68mmacy	/* check options */
940a9bcb68mmacy	while ((c = getopt(argc, argv, ":PV:b:nso:pv")) != -1) {
941a9bcb68mmacy		switch (c) {
942a9bcb68mmacy		case 'V':
943a9bcb68mmacy			type = ZFS_TYPE_VOLUME;
944a9bcb68mmacy			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
945a9bcb68mmacy				(void) fprintf(stderr, gettext("bad volume "
946a9bcb68mmacy				    "size '%s': %s\n"), optarg,
947a9bcb68mmacy				    libzfs_error_description(g_zfs));
948a9bcb68mmacy				goto error;
949a9bcb68mmacy			}
950a9bcb68mmacy
951a9bcb68mmacy			if (nvlist_add_uint64(props,
952a9bcb68mmacy			    zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
953a9bcb68mmacy				nomem();
954a9bcb68mmacy			volsize = intval;
955a9bcb68mmacy			break;
956a9bcb68mmacy		case 'P':
957a9bcb68mmacy			verbose = B_TRUE;
958a9bcb68mmacy			parseable = B_TRUE;
959a9bcb68mmacy			break;
960a9bcb68mmacy		case 'p':
961a9bcb68mmacy			parents = B_TRUE;
962a9bcb68mmacy			break;
963a9bcb68mmacy		case 'b':
964a9bcb68mmacy			bflag = B_TRUE;
965a9bcb68mmacy			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
966a9bcb68mmacy				(void) fprintf(stderr, gettext("bad volume "
967a9bcb68mmacy				    "block size '%s': %s\n"), optarg,
968a9bcb68mmacy				    libzfs_error_description(g_zfs));
969a9bcb68mmacy				goto error;
970a9bcb68mmacy			}
971a9bcb68mmacy
972a9bcb68mmacy			if (nvlist_add_uint64(props,
973a9bcb68mmacy			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
974a9bcb68mmacy			    intval) != 0)
975a9bcb68mmacy				nomem();
976a9bcb68mmacy			break;
977a9bcb68mmacy		case 'n':
978a9bcb68mmacy			dryrun = B_TRUE;
979a9bcb68mmacy			break;
980a9bcb68mmacy		case 'o':
981a9bcb68mmacy			if (!parseprop(props, optarg))
982a9bcb68mmacy				goto error;
983a9bcb68mmacy			break;
984a9bcb68mmacy		case 's':
985a9bcb68mmacy			noreserve = B_TRUE;
986a9bcb68mmacy			break;
987a9bcb68mmacy		case 'v':
988a9bcb68mmacy			verbose = B_TRUE;
989a9bcb68mmacy			break;
990a9bcb68mmacy		case ':':
991a9bcb68mmacy			(void) fprintf(stderr, gettext("missing size "
992a9bcb68mmacy			    "argument\n"));
993a9bcb68mmacy			goto badusage;
994a9bcb68mmacy		case '?':
995a9bcb68mmacy			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
996a9bcb68mmacy			    optopt);
997a9bcb68mmacy			goto badusage;
998a9bcb68mmacy		}
999a9bcb68mmacy	}
1000a9bcb68mmacy
1001a9bcb68mmacy	if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
1002a9bcb68mmacy		(void) fprintf(stderr, gettext("'-s' and '-b' can only be "
1003a9bcb68mmacy		    "used when creating a volume\n"));
1004a9bcb68mmacy		goto badusage;
1005a9bcb68mmacy	}
1006a9bcb68mmacy
1007a9bcb68mmacy	argc -= optind;
1008a9bcb68mmacy	argv += optind;
1009a9bcb68mmacy
1010a9bcb68mmacy	/* check number of arguments */
1011a9bcb68mmacy	if (argc == 0) {
1012a9bcb68mmacy		(void) fprintf(stderr, gettext("missing %s argument\n"),
1013a9bcb68mmacy		    zfs_type_to_name(type));
1014a9bcb68mmacy		goto badusage;
1015a9bcb68mmacy	}
1016a9bcb68mmacy	if (argc > 1) {
1017a9bcb68mmacy		(void) fprintf(stderr, gettext("too many arguments\n"));
1018a9bcb68mmacy		goto badusage;
1019a9bcb68mmacy	}
1020a9bcb68mmacy
1021a9bcb68mmacy	if (dryrun || (type == ZFS_TYPE_VOLUME && !noreserve)) {
1022a9bcb68mmacy		char msg[ZFS_MAX_DATASET_NAME_LEN * 2];
1023a9bcb68mmacy		char *p;
1024a9bcb68mmacy
1025a9bcb68mmacy		if ((p = strchr(argv[0], '/')) != NULL)
1026a9bcb68mmacy			*p = '\0';
1027a9bcb68mmacy		zpool_handle = zpool_open(g_zfs, argv[0]);
1028a9bcb68mmacy		if (p != NULL)
1029a9bcb68mmacy			*p = '/';
1030a9bcb68mmacy		if (zpool_handle == NULL)
1031a9bcb68mmacy			goto error;
1032a9bcb68mmacy
1033a9bcb68mmacy		(void) snprintf(msg, sizeof (msg),
1034a9bcb68mmacy		    dryrun ? gettext("cannot verify '%s'") :
1035a9bcb68mmacy		    gettext("cannot create '%s'"), argv[0]);
1036a9bcb68mmacy		if (props && (real_props = zfs_valid_proplist(g_zfs, type,
1037a9bcb68mmacy		    props, 0, NULL, zpool_handle, B_TRUE, msg)) == NULL) {
1038a9bcb68mmacy			zpool_close(zpool_handle);
1039a9bcb68mmacy			goto error;
1040a9bcb68mmacy		}
1041a9bcb68mmacy	}
1042a9bcb68mmacy
1043a9bcb68mmacy	/*
1044a9bcb68mmacy	 * if volsize is not a multiple of volblocksize, round it up to the
1045a9bcb68mmacy	 * nearest multiple of the volblocksize
1046a9bcb68mmacy	 */
1047a9bcb68mmacy	if (type == ZFS_TYPE_VOLUME) {
1048a9bcb68mmacy		uint64_t volblocksize;
1049a9bcb68mmacy
1050a9bcb68mmacy		if (nvlist_lookup_uint64(props,
1051a9bcb68mmacy		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1052a9bcb68mmacy		    &volblocksize) != 0)
1053a9bcb68mmacy			volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
1054a9bcb68mmacy
1055a9bcb68mmacy		if (volsize % volblocksize) {
1056a9bcb68mmacy			volsize = P2ROUNDUP_TYPED(volsize, volblocksize,
1057a9bcb68mmacy			    uint64_t);
1058a9bcb68mmacy
1059a9bcb68mmacy			if (nvlist_add_uint64(props,
1060a9bcb68mmacy			    zfs_prop_to_name(ZFS_PROP_VOLSIZE), volsize) != 0) {
1061a9bcb68mmacy				nvlist_free(props);
1062a9bcb68mmacy				nomem();
1063a9bcb68mmacy			}
1064a9bcb68mmacy		}
1065a9bcb68mmacy	}
1066a9bcb68mmacy
1067a9bcb68mmacy
1068a9bcb68mmacy	if (type == ZFS_TYPE_VOLUME && !noreserve) {
1069a9bcb68mmacy		uint64_t spa_version;
1070a9bcb68mmacy		zfs_prop_t resv_prop;
1071a9bcb68mmacy		char *strval;
1072a9bcb68mmacy
1073a9bcb68mmacy		spa_version = zpool_get_prop_int(zpool_handle,
1074a9bcb68mmacy		    ZPOOL_PROP_VERSION, NULL);
1075a9bcb68mmacy		if (spa_version >= SPA_VERSION_REFRESERVATION)
1076a9bcb68mmacy			resv_prop = ZFS_PROP_REFRESERVATION;
1077a9bcb68mmacy		else
1078a9bcb68mmacy			resv_prop = ZFS_PROP_RESERVATION;
1079a9bcb68mmacy
1080a9bcb68mmacy		volsize = zvol_volsize_to_reservation(zpool_handle, volsize,
1081a9bcb68mmacy		    real_props);
1082a9bcb68mmacy
1083a9bcb68mmacy		if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
1084a9bcb68mmacy		    &strval) != 0) {
1085a9bcb68mmacy			if (nvlist_add_uint64(props,
1086a9bcb68mmacy			    zfs_prop_to_name(resv_prop), volsize) != 0) {
1087a9bcb68mmacy				nvlist_free(props);
1088a9bcb68mmacy				nomem();
1089a9bcb68mmacy			}
1090a9bcb68mmacy		}
1091a9bcb68mmacy	}
1092a9bcb68mmacy	if (zpool_handle != NULL) {
1093a9bcb68mmacy		zpool_close(zpool_handle);
1094a9bcb68mmacy		nvlist_free(real_props);
1095a9bcb68mmacy	}
1096a9bcb68mmacy
1097a9bcb68mmacy	if (parents && zfs_name_valid(argv[0], type)) {
1098a9bcb68mmacy		/*
1099a9bcb68mmacy		 * Now create the ancestors of target dataset.  If the target
1100a9bcb68mmacy		 * already exists and '-p' option was used we should not
1101a9bcb68mmacy		 * complain.
1102a9bcb68mmacy		 */
1103a9bcb68mmacy		if (zfs_dataset_exists(g_zfs, argv[0], type)) {
1104a9bcb68mmacy			ret = 0;
1105a9bcb68mmacy			goto error;
1106a9bcb68mmacy		}
1107a9bcb68mmacy		if (verbose) {
1108a9bcb68mmacy			(void) printf(parseable ? "create_ancestors\t%s\n" :
1109a9bcb68mmacy			    dryrun ?  "would create ancestors of %s\n" :
1110a9bcb68mmacy			    "create ancestors of %s\n", argv[0]);
1111a9bcb68mmacy		}
1112a9bcb68mmacy		if (!dryrun) {
1113a9bcb68mmacy			if (zfs_create_ancestors(g_zfs, argv[0]) != 0) {
1114a9bcb68mmacy				goto error;
1115a9bcb68mmacy			}
1116a9bcb68mmacy		}
1117a9bcb68mmacy	}
1118a9bcb68mmacy
1119a9bcb68mmacy	if (verbose) {
1120a9bcb68mmacy		nvpair_t *nvp = NULL;
1121a9bcb68mmacy		(void) printf(parseable ? "create\t%s\n" :
1122a9bcb68mmacy		    dryrun ? "would create %s\n" : "create %s\n", argv[0]);
1123a9bcb68mmacy		while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) {
1124a9bcb68mmacy			uint64_t uval;
1125a9bcb68mmacy			char *sval;
1126a9bcb68mmacy
1127a9bcb68mmacy			switch (nvpair_type(nvp)) {
1128a9bcb68mmacy			case DATA_TYPE_UINT64:
1129a9bcb68mmacy				VERIFY0(nvpair_value_uint64(nvp, &uval));
1130a9bcb68mmacy				(void) printf(parseable ?
1131a9bcb68mmacy				    "property\t%s\t%llu\n" : "\t%s=%llu\n",
1132a9bcb68mmacy				    nvpair_name(nvp), (u_longlong_t)uval);
1133a9bcb68mmacy				break;
1134a9bcb68mmacy			case DATA_TYPE_STRING:
1135a9bcb68mmacy				VERIFY0(nvpair_value_string(nvp, &sval));
1136a9bcb68mmacy				(void) printf(parseable ?
1137a9bcb68mmacy				    "property\t%s\t%s\n" : "\t%s=%s\n",
1138a9bcb68mmacy				    nvpair_name(nvp), sval);
1139a9bcb68mmacy				break;
1140a9bcb68mmacy			default:
1141a9bcb68mmacy				(void) fprintf(stderr, "property '%s' "
1142a9bcb68mmacy				    "has illegal type %d\n",
1143a9bcb68mmacy				    nvpair_name(nvp), nvpair_type(nvp));
1144a9bcb68mmacy				abort();
1145a9bcb68mmacy			}
1146a9bcb68mmacy		}
1147a9bcb68mmacy	}
1148a9bcb68mmacy	if (dryrun) {
1149a9bcb68mmacy		ret = 0;
1150a9bcb68mmacy		goto error;
1151a9bcb68mmacy	}
1152a9bcb68mmacy
1153a9bcb68mmacy	/* pass to libzfs */
1154a9bcb68mmacy	if (zfs_create(g_zfs, argv[0], type, props) != 0)
1155a9bcb68mmacy		goto error;
1156a9bcb68mmacy
1157a9bcb68mmacy	if (log_history) {
1158a9bcb68mmacy		(void) zpool_log_history(g_zfs, history_str);
1159a9bcb68mmacy		log_history = B_FALSE;
1160a9bcb68mmacy	}
1161a9bcb68mmacy
1162a9bcb68mmacy	ret = zfs_mount_and_share(g_zfs, argv[0], ZFS_TYPE_DATASET);
1163a9bcb68mmacyerror:
1164a9bcb68mmacy	nvlist_free(props);
1165a9bcb68mmacy	return (ret);
1166a9bcb68mmacybadusage:
1167a9bcb68mmacy	nvlist_free(props);
1168a9bcb68mmacy	usage(B_FALSE);
1169a9bcb68mmacy	return (2);
1170a9bcb68mmacy}
1171a9bcb68mmacy
1172a9bcb68mmacy/*
1173a9bcb68mmacy * zfs destroy [-rRf] <fs, vol>
1174a9bcb68mmacy * zfs destroy [-rRd] <snap>
1175a9bcb68mmacy *
1176a9bcb68mmacy *	-r	Recursively destroy all children
1177a9bcb68mmacy *	-R	Recursively destroy all dependents, including clones
1178a9bcb68mmacy *	-f	Force unmounting of any dependents
1179a9bcb68mmacy *	-d	If we can't destroy now, mark for deferred destruction
1180a9bcb68mmacy *
1181a9bcb68mmacy * Destroys the given dataset.  By default, it will unmount any filesystems,
1182a9bcb68mmacy * and refuse to destroy a dataset that has any dependents.  A dependent can
1183a9bcb68mmacy * either be a child, or a clone of a child.
1184a9bcb68mmacy */
1185a9bcb68mmacytypedef struct destroy_cbdata {
1186a9bcb68mmacy	boolean_t	cb_first;
1187a9bcb68mmacy	boolean_t	cb_force;
1188a9bcb68mmacy	boolean_t	cb_recurse;
1189a9bcb68mmacy	boolean_t	cb_error;
1190a9bcb68mmacy	boolean_t	cb_doclones;
1191a9bcb68mmacy	zfs_handle_t	*cb_target;
1192a9bcb68mmacy	boolean_t	cb_defer_destroy;
1193a9bcb68mmacy	boolean_t	cb_verbose;
1194a9bcb68mmacy	boolean_t	cb_parsable;
1195a9bcb68mmacy	boolean_t	cb_dryrun;
1196a9bcb68mmacy	nvlist_t	*cb_nvl;
1197a9bcb68mmacy	nvlist_t	*cb_batchedsnaps;
1198a9bcb68mmacy
1199a9bcb68mmacy	/* first snap in contiguous run */
1200a9bcb68mmacy	char		*cb_firstsnap;
1201a9bcb68mmacy	/* previous snap in contiguous run */
1202a9bcb68mmacy	char		*cb_prevsnap;
1203a9bcb68mmacy	int64_t		cb_snapused;
1204a9bcb68mmacy	char		*cb_snapspec;
1205a9bcb68mmacy	char		*cb_bookmark;
1206a9bcb68mmacy	uint64_t	cb_snap_count;
1207a9bcb68mmacy} destroy_cbdata_t;
1208a9bcb68mmacy
1209a9bcb68mmacy/*
1210a9bcb68mmacy * Check for any dependents based on the '-r' or '-R' flags.
1211a9bcb68mmacy */
1212a9bcb68mmacystatic int
1213a9bcb68mmacydestroy_check_dependent(zfs_handle_t *zhp, void *data)
1214a9bcb68mmacy{
1215a9bcb68mmacy	destroy_cbdata_t *cbp = data;
1216a9bcb68mmacy	const char *tname = zfs_get_name(cbp->cb_target);
1217a9bcb68mmacy	const char *name = zfs_get_name(zhp);
1218a9bcb68mmacy
1219a9bcb68mmacy	if (strncmp(tname, name, strlen(tname)) == 0 &&
1220a9bcb68mmacy	    (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
1221a9bcb68mmacy		/*
1222a9bcb68mmacy		 * This is a direct descendant, not a clone somewhere else in
1223a9bcb68mmacy		 * the hierarchy.
1224a9bcb68mmacy		 */
1225a9bcb68mmacy		if (cbp->cb_recurse)
1226a9bcb68mmacy			goto out;
1227a9bcb68mmacy
1228a9bcb68mmacy		if (cbp->cb_first) {
1229a9bcb68mmacy			(void) fprintf(stderr, gettext("cannot destroy '%s': "
1230a9bcb68mmacy			    "%s has children\n"),
1231a9bcb68mmacy			    zfs_get_name(cbp->cb_target),
1232a9bcb68mmacy			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1233a9bcb68mmacy			(void) fprintf(stderr, gettext("use '-r' to destroy "
1234a9bcb68mmacy			    "the following datasets:\n"));
1235a9bcb68mmacy			cbp->cb_first = B_FALSE;
1236a9bcb68mmacy			cbp->cb_error = B_TRUE;
1237a9bcb68mmacy		}
1238a9bcb68mmacy
1239a9bcb68mmacy		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1240a9bcb68mmacy	} else {
1241a9bcb68mmacy		/*
1242a9bcb68mmacy		 * This is a clone.  We only want to report this if the '-r'
1243a9bcb68mmacy		 * wasn't specified, or the target is a snapshot.
1244a9bcb68mmacy		 */
1245a9bcb68mmacy		if (!cbp->cb_recurse &&
1246a9bcb68mmacy		    zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1247a9bcb68mmacy			goto out;
1248a9bcb68mmacy
1249a9bcb68mmacy		if (cbp->cb_first) {
1250a9bcb68mmacy			(void) fprintf(stderr, gettext("cannot destroy '%s': "
1251a9bcb68mmacy			    "%s has dependent clones\n"),
1252a9bcb68mmacy			    zfs_get_name(cbp->cb_target),
1253a9bcb68mmacy			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1254a9bcb68mmacy			(void) fprintf(stderr, gettext("use '-R' to destroy "
1255a9bcb68mmacy			    "the following datasets:\n"));
1256a9bcb68mmacy			cbp->cb_first = B_FALSE;
1257a9bcb68mmacy			cbp->cb_error = B_TRUE;
1258a9bcb68mmacy			cbp->cb_dryrun = B_TRUE;
1259a9bcb68mmacy		}
1260a9bcb68mmacy
1261a9bcb68mmacy		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1262a9bcb68mmacy	}
1263a9bcb68mmacy
1264a9bcb68mmacyout:
1265a9bcb68mmacy	zfs_close(zhp);
1266a9bcb68mmacy	return (0);
1267a9bcb68mmacy}
1268a9bcb68mmacy
1269a9bcb68mmacystatic int
1270a9bcb68mmacydestroy_batched(destroy_cbdata_t *cb)
1271a9bcb68mmacy{
1272a9bcb68mmacy	int error = zfs_destroy_snaps_nvl(g_zfs,
1273a9bcb68mmacy	    cb->cb_batchedsnaps, B_FALSE);
1274a9bcb68mmacy	fnvlist_free(cb->cb_batchedsnaps);
1275a9bcb68mmacy	cb->cb_batchedsnaps = fnvlist_alloc();
1276a9bcb68mmacy	return (error);
1277a9bcb68mmacy}
1278a9bcb68mmacy
1279a9bcb68mmacystatic int
1280a9bcb68mmacydestroy_callback(zfs_handle_t *zhp, void *data)
1281a9bcb68mmacy{
1282a9bcb68mmacy	destroy_cbdata_t *cb = data;
1283a9bcb68mmacy	const char *name = zfs_get_name(zhp);
1284a9bcb68mmacy	int error;
1285a9bcb68mmacy
1286a9bcb68mmacy	if (cb->cb_verbose) {
1287a9bcb68mmacy		if (cb->cb_parsable) {
1288a9bcb68mmacy			(void) printf("destroy\t%s\n", name);
1289a9bcb68mmacy		} else if (cb->cb_dryrun) {
1290a9bcb68mmacy			(void) printf(gettext("would destroy %s\n"),
1291a9bcb68mmacy			    name);
1292a9bcb68mmacy		} else {
1293a9bcb68mmacy			(void) printf(gettext("will destroy %s\n"),
1294a9bcb68mmacy			    name);
1295a9bcb68mmacy		}
1296a9bcb68mmacy	}
1297a9bcb68mmacy
1298a9bcb68mmacy	/*
1299a9bcb68mmacy	 * Ignore pools (which we've already flagged as an error before getting
1300a9bcb68mmacy	 * here).
1301a9bcb68mmacy	 */
1302a9bcb68mmacy	if (strchr(zfs_get_name(zhp), '/') == NULL &&
1303a9bcb68mmacy	    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1304a9bcb68mmacy		zfs_close(zhp);
1305a9bcb68mmacy		return (0);
1306a9bcb68mmacy	}
1307a9bcb68mmacy	if (cb->cb_dryrun) {
1308a9bcb68mmacy		zfs_close(zhp);
1309a9bcb68mmacy		return (0);
1310a9bcb68mmacy	}
1311a9bcb68mmacy
1312a9bcb68mmacy	/*
1313a9bcb68mmacy	 * We batch up all contiguous snapshots (even of different
1314a9bcb68mmacy	 * filesystems) and destroy them with one ioctl.  We can't
1315a9bcb68mmacy	 * simply do all snap deletions and then all fs deletions,
1316a9bcb68mmacy	 * because we must delete a clone before its origin.
1317a9bcb68mmacy	 */
1318a9bcb68mmacy	if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1319a9bcb68mmacy		cb->cb_snap_count++;
1320a9bcb68mmacy		fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1321a9bcb68mmacy		if (cb->cb_snap_count % 10 == 0 && cb->cb_defer_destroy)
1322a9bcb68mmacy			error = destroy_batched(cb);
1323a9bcb68mmacy	} else {
1324a9bcb68mmacy		error = destroy_batched(cb);
1325a9bcb68mmacy		if (error != 0 ||
1326a9bcb68mmacy		    zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1327a9bcb68mmacy		    zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1328a9bcb68mmacy			zfs_close(zhp);
1329a9bcb68mmacy			/*
1330a9bcb68mmacy			 * When performing a recursive destroy we ignore errors
1331a9bcb68mmacy			 * so that the recursive destroy could continue
1332a9bcb68mmacy			 * destroying past problem datasets
1333a9bcb68mmacy			 */
1334a9bcb68mmacy			if (cb->cb_recurse) {
1335a9bcb68mmacy				cb->cb_error = B_TRUE;
1336a9bcb68mmacy				return (0);
1337a9bcb68mmacy			}
1338a9bcb68mmacy			return (-1);
1339a9bcb68mmacy		}
1340a9bcb68mmacy	}
1341a9bcb68mmacy
1342a9bcb68mmacy	zfs_close(zhp);
1343a9bcb68mmacy	return (0);
1344a9bcb68mmacy}
1345a9bcb68mmacy
1346a9bcb68mmacystatic int
1347a9bcb68mmacydestroy_print_cb(zfs_handle_t *zhp, void *arg)
1348a9bcb68mmacy{
1349a9bcb68mmacy	destroy_cbdata_t *cb = arg;
1350a9bcb68mmacy	const char *name = zfs_get_name(zhp);
1351a9bcb68mmacy	int err = 0;
1352a9bcb68mmacy
1353a9bcb68mmacy	if (nvlist_exists(cb->cb_nvl, name)) {
1354a9bcb68mmacy		if (cb->cb_firstsnap == NULL)
1355a9bcb68mmacy			cb->cb_firstsnap = strdup(name);
1356a9bcb68mmacy		if (cb->cb_prevsnap != NULL)
1357a9bcb68mmacy			free(cb->cb_prevsnap);
1358a9bcb68mmacy		/* this snap continues the current range */
1359a9bcb68mmacy		cb->cb_prevsnap = strdup(name);
1360a9bcb68mmacy		if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1361a9bcb68mmacy			nomem();
1362a9bcb68mmacy		if (cb->cb_verbose) {
1363a9bcb68mmacy			if (cb->cb_parsable) {
1364a9bcb68mmacy				(void) printf("destroy\t%s\n", name);
1365a9bcb68mmacy			} else if (cb->cb_dryrun) {
1366a9bcb68mmacy				(void) printf(gettext("would destroy %s\n"),
1367a9bcb68mmacy				    name);
1368a9bcb68mmacy			} else {
1369a9bcb68mmacy				(void) printf(gettext("will destroy %s\n"),
1370a9bcb68mmacy				    name);
1371a9bcb68mmacy			}
1372a9bcb68mmacy		}
1373a9bcb68mmacy	} else if (cb->cb_firstsnap != NULL) {
1374a9bcb68mmacy		/* end of this range */
1375a9bcb68mmacy		uint64_t used = 0;
1376a9bcb68mmacy		err = lzc_snaprange_space(cb->cb_firstsnap,
1377a9bcb68mmacy		    cb->cb_prevsnap, &used);
1378a9bcb68mmacy		cb->cb_snapused += used;
1379a9bcb68mmacy		free(cb->cb_firstsnap);
1380a9bcb68mmacy		cb->cb_firstsnap = NULL;
1381a9bcb68mmacy		free(cb->cb_prevsnap);
1382a9bcb68mmacy		cb->cb_prevsnap = NULL;
1383a9bcb68mmacy	}
1384a9bcb68mmacy	zfs_close(zhp);
1385a9bcb68mmacy	return (err);
1386a9bcb68mmacy}
1387a9bcb68mmacy
1388a9bcb68mmacystatic int
1389a9bcb68mmacydestroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1390a9bcb68mmacy{
1391a9bcb68mmacy	int err;
1392a9bcb68mmacy	assert(cb->cb_firstsnap == NULL);
1393a9bcb68mmacy	assert(cb->cb_prevsnap == NULL);
1394a9bcb68mmacy	err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb, 0, 0);
1395a9bcb68mmacy	if (cb->cb_firstsnap != NULL) {
1396a9bcb68mmacy		uint64_t used = 0;
1397a9bcb68mmacy		if (err == 0) {
1398a9bcb68mmacy			err = lzc_snaprange_space(cb->cb_firstsnap,
1399a9bcb68mmacy			    cb->cb_prevsnap, &used);
1400a9bcb68mmacy		}
1401a9bcb68mmacy		cb->cb_snapused += used;
1402a9bcb68mmacy		free(cb->cb_firstsnap);
1403a9bcb68mmacy		cb->cb_firstsnap = NULL;
1404a9bcb68mmacy		free(cb->cb_prevsnap);
1405a9bcb68mmacy		cb->cb_prevsnap = NULL;
1406a9bcb68mmacy	}
1407a9bcb68mmacy	return (err);
1408a9bcb68mmacy}
1409a9bcb68mmacy
1410a9bcb68mmacystatic int
1411a9bcb68mmacysnapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1412a9bcb68mmacy{
1413a9bcb68mmacy	destroy_cbdata_t *cb = arg;
1414a9bcb68mmacy	int err = 0;
1415a9bcb68mmacy
1416a9bcb68mmacy	/* Check for clones. */
1417a9bcb68mmacy	if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1418a9bcb68mmacy		cb->cb_target = zhp;
1419a9bcb68mmacy		cb->cb_first = B_TRUE;
1420a9bcb68mmacy		err = zfs_iter_dependents(zhp, B_TRUE,
1421a9bcb68mmacy		    destroy_check_dependent, cb);
1422a9bcb68mmacy	}
1423a9bcb68mmacy
1424a9bcb68mmacy	if (err == 0) {
1425a9bcb68mmacy		if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1426a9bcb68mmacy			nomem();
1427a9bcb68mmacy	}
1428a9bcb68mmacy	zfs_close(zhp);
1429a9bcb68mmacy	return (err);
1430a9bcb68mmacy}
1431a9bcb68mmacy
1432a9bcb68mmacystatic int
1433a9bcb68mmacygather_snapshots(zfs_handle_t *zhp, void *arg)
1434a9bcb68mmacy{
1435a9bcb68mmacy	destroy_cbdata_t *cb = arg;
1436a9bcb68mmacy	int err = 0;
1437a9bcb68mmacy
1438a9bcb68mmacy	err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1439a9bcb68mmacy	if (err == ENOENT)
1440a9bcb68mmacy		err = 0;
1441a9bcb68mmacy	if (err != 0)
1442a9bcb68mmacy		goto out;
1443a9bcb68mmacy
1444a9bcb68mmacy	if (cb->cb_verbose) {
1445a9bcb68mmacy		err = destroy_print_snapshots(zhp, cb);
1446a9bcb68mmacy		if (err != 0)
1447a9bcb68mmacy			goto out;
1448a9bcb68mmacy	}
1449a9bcb68mmacy
1450a9bcb68mmacy	if (cb->cb_recurse)
1451a9bcb68mmacy		err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1452a9bcb68mmacy
1453a9bcb68mmacyout:
1454a9bcb68mmacy	zfs_close(zhp);
1455a9bcb68mmacy	return (err);
1456a9bcb68mmacy}
1457a9bcb68mmacy
1458a9bcb68mmacystatic int
1459a9bcb68mmacydestroy_clones(destroy_cbdata_t *cb)
1460a9bcb68mmacy{
1461a9bcb68mmacy	nvpair_t *pair;
1462a9bcb68mmacy	for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1463a9bcb68mmacy	    pair != NULL;
1464a9bcb68mmacy	    pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1465a9bcb68mmacy		zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1466a9bcb68mmacy		    ZFS_TYPE_SNAPSHOT);
1467a9bcb68mmacy		if (zhp != NULL) {
1468a9bcb68mmacy			boolean_t defer = cb->cb_defer_destroy;
1469a9bcb68mmacy			int err;
1470a9bcb68mmacy
1471a9bcb68mmacy			/*
1472a9bcb68mmacy			 * We can't defer destroy non-snapshots, so set it to
1473a9bcb68mmacy			 * false while destroying the clones.
1474a9bcb68mmacy			 */
1475a9bcb68mmacy			cb->cb_defer_destroy = B_FALSE;
1476a9bcb68mmacy			err = zfs_iter_dependents(zhp, B_FALSE,
1477a9bcb68mmacy			    destroy_callback, cb);
1478a9bcb68mmacy			cb->cb_defer_destroy = defer;
1479a9bcb68mmacy			zfs_close(zhp);
1480a9bcb68mmacy			if (err != 0)
1481a9bcb68mmacy				return (err);
1482a9bcb68mmacy		}
1483a9bcb68mmacy	}
1484a9bcb68mmacy	return (0);
1485a9bcb68mmacy}
1486a9bcb68mmacy
1487a9bcb68mmacystatic int
1488a9bcb68mmacyzfs_do_destroy(int argc, char **argv)
1489a9bcb68mmacy{
1490a9bcb68mmacy	destroy_cbdata_t cb = { 0 };
1491a9bcb68mmacy	int rv = 0;
1492a9bcb68mmacy	int err = 0;
1493a9bcb68mmacy	int c;
1494a9bcb68mmacy	zfs_handle_t *zhp = NULL;
1495a9bcb68mmacy	char *at, *pound;
1496a9bcb68mmacy	zfs_type_t type = ZFS_TYPE_DATASET;
1497a9bcb68mmacy
1498a9bcb68mmacy	/* check options */
1499a9bcb68mmacy	while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1500a9bcb68mmacy		switch (c) {
1501a9bcb68mmacy		case 'v':
1502a9bcb68mmacy			cb.cb_verbose = B_TRUE;
1503a9bcb68mmacy			break;
1504a9bcb68mmacy		case 'p':
1505a9bcb68mmacy			cb.cb_verbose = B_TRUE;
1506a9bcb68mmacy			cb.cb_parsable = B_TRUE;
1507a9bcb68mmacy			break;
1508a9bcb68mmacy		case 'n':
1509a9bcb68mmacy			cb.cb_dryrun = B_TRUE;
1510a9bcb68mmacy			break;
1511a9bcb68mmacy		case 'd':
1512a9bcb68mmacy			cb.cb_defer_destroy = B_TRUE;
1513a9bcb68mmacy			type = ZFS_TYPE_SNAPSHOT;
1514a9bcb68mmacy			break;
1515a9bcb68mmacy		case 'f':
1516a9bcb68mmacy			cb.cb_force = B_TRUE;
1517a9bcb68mmacy			break;
1518a9bcb68mmacy		case 'r':
1519a9bcb68mmacy			cb.cb_recurse = B_TRUE;
1520a9bcb68mmacy			break;
1521a9bcb68mmacy		case 'R':
1522a9bcb68mmacy			cb.cb_recurse = B_TRUE;
1523a9bcb68mmacy			cb.cb_doclones = B_TRUE;
1524a9bcb68mmacy			break;
1525a9bcb68mmacy		case '?':
1526a9bcb68mmacy		default:
1527a9bcb68mmacy			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
1528a9bcb68mmacy			    optopt);
1529a9bcb68mmacy			usage(B_FALSE);
1530a9bcb68mmacy		}
1531a9bcb68mmacy	}
1532a9bcb68mmacy
1533a9bcb68mmacy	argc -= optind;
1534a9bcb68mmacy	argv += optind;
1535a9bcb68mmacy
1536a9bcb68mmacy	/* check number of arguments */
1537a9bcb68mmacy	if (argc == 0) {
1538a9bcb68mmacy		(void) fprintf(stderr, gettext("missing dataset argument\n"));
1539a9bcb68mmacy		usage(B_FALSE);
1540a9bcb68mmacy	}
1541a9bcb68mmacy	if (argc > 1) {
1542a9bcb68mmacy		(void) fprintf(stderr, gettext("too many arguments\n"));
1543a9bcb68mmacy		usage(B_FALSE);
1544a9bcb68mmacy	}
1545a9bcb68mmacy
1546a9bcb68mmacy	at = strchr(argv[0], '@');
1547a9bcb68mmacy	pound = strchr(argv[0], '#');
1548a9bcb68mmacy	if (at != NULL) {
1549a9bcb68mmacy
1550a9bcb68mmacy		/* Build the list of snaps to destroy in cb_nvl. */
1551a9bcb68mmacy		cb.cb_nvl = fnvlist_alloc();
1552a9bcb68mmacy
1553a9bcb68mmacy		*at = '\0';
1554a9bcb68mmacy		zhp = zfs_open(g_zfs, argv[0],
1555a9bcb68mmacy		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1556a9bcb68mmacy		if (zhp == NULL) {
1557a9bcb68mmacy			nvlist_free(cb.cb_nvl);
1558a9bcb68mmacy			return (1);
1559a9bcb68mmacy		}
1560a9bcb68mmacy
1561a9bcb68mmacy		cb.cb_snapspec = at + 1;
1562a9bcb68mmacy		if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1563a9bcb68mmacy		    cb.cb_error) {
1564a9bcb68mmacy			rv = 1;
1565a9bcb68mmacy			goto out;
1566a9bcb68mmacy		}
1567a9bcb68mmacy
1568a9bcb68mmacy		if (nvlist_empty(cb.cb_nvl)) {
1569a9bcb68mmacy			(void) fprintf(stderr, gettext("could not find any "
1570a9bcb68mmacy			    "snapshots to destroy; check snapshot names.\n"));
1571a9bcb68mmacy			rv = 1;
1572a9bcb68mmacy			goto out;
1573a9bcb68mmacy		}
1574a9bcb68mmacy
1575a9bcb68mmacy		if (cb.cb_verbose) {
1576a9bcb68mmacy			char buf[16];
1577a9bcb68mmacy			zfs_nicebytes(cb.cb_snapused, buf, sizeof (buf));
1578a9bcb68mmacy			if (cb.cb_parsable) {
1579a9bcb68mmacy				(void) printf("reclaim\t%llu\n",
1580a9bcb68mmacy				    (u_longlong_t)cb.cb_snapused);
1581a9bcb68mmacy			} else if (cb.cb_dryrun) {
1582a9bcb68mmacy				(void) printf(gettext("would reclaim %s\n"),
1583a9bcb68mmacy				    buf);
1584a9bcb68mmacy			} else {
1585a9bcb68mmacy				(void) printf(gettext("will reclaim %s\n"),
1586a9bcb68mmacy				    buf);
1587a9bcb68mmacy			}
1588a9bcb68mmacy		}
1589a9bcb68mmacy
1590a9bcb68mmacy		if (!cb.cb_dryrun) {
1591a9bcb68mmacy			if (cb.cb_doclones) {
1592a9bcb68mmacy				cb.cb_batchedsnaps = fnvlist_alloc();
1593a9bcb68mmacy				err = destroy_clones(&cb);
1594a9bcb68mmacy				if (err == 0) {
1595a9bcb68mmacy					err = zfs_destroy_snaps_nvl(g_zfs,
1596a9bcb68mmacy					    cb.cb_batchedsnaps, B_FALSE);
1597a9bcb68mmacy				}
1598a9bcb68mmacy				if (err != 0) {
1599a9bcb68mmacy					rv = 1;
1600a9bcb68mmacy					goto out;
1601a9bcb68mmacy				}
1602a9bcb68mmacy			}
1603a9bcb68mmacy			if (err == 0) {
1604a9bcb68mmacy				err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1605a9bcb68mmacy				    cb.cb_defer_destroy);
1606a9bcb68mmacy			}
1607a9bcb68mmacy		}
1608a9bcb68mmacy
1609a9bcb68mmacy		if (err != 0)
1610a9bcb68mmacy			rv = 1;
1611a9bcb68mmacy	} else if (pound != NULL) {
1612a9bcb68mmacy		int err;
1613a9bcb68mmacy		nvlist_t *nvl;
1614a9bcb68mmacy
1615a9bcb68mmacy		if (cb.cb_dryrun) {
1616a9bcb68mmacy			(void) fprintf(stderr,
1617a9bcb68mmacy			    "dryrun is not supported with bookmark\n");
1618a9bcb68mmacy			return (-1);
1619a9bcb68mmacy		}
1620a9bcb68mmacy
1621a9bcb68mmacy		if (cb.cb_defer_destroy) {
1622a9bcb68mmacy			(void) fprintf(stderr,
1623a9bcb68mmacy			    "defer destroy is not supported with bookmark\n");
1624a9bcb68mmacy			return (-1);
1625a9bcb68mmacy		}
1626a9bcb68mmacy
1627a9bcb68mmacy		if (cb.cb_recurse) {
1628a9bcb68mmacy			(void) fprintf(stderr,
1629a9bcb68mmacy			    "recursive is not supported with bookmark\n");
1630a9bcb68mmacy			return (-1);
1631a9bcb68mmacy		}
1632a9bcb68mmacy
1633a9bcb68mmacy		/*
1634a9bcb68mmacy		 * Unfortunately, zfs_bookmark() doesn't honor the
1635a9bcb68mmacy		 * casesensitivity setting.  However, we can't simply
1636a9bcb68mmacy		 * remove this check, because lzc_destroy_bookmarks()
1637a9bcb68mmacy		 * ignores non-existent bookmarks, so this is necessary
1638a9bcb68mmacy		 * to get a proper error message.
1639a9bcb68mmacy		 */
1640a9bcb68mmacy		if (!zfs_bookmark_exists(argv[0])) {
1641a9bcb68mmacy			(void) fprintf(stderr, gettext("bookmark '%s' "
1642a9bcb68mmacy			    "does not exist.\n"), argv[0]);
1643a9bcb68mmacy			return (1);
1644a9bcb68mmacy		}
1645a9bcb68mmacy
1646a9bcb68mmacy		nvl = fnvlist_alloc();
1647a9bcb68mmacy		fnvlist_add_boolean(nvl, argv[0]);
1648a9bcb68mmacy
1649a9bcb68mmacy		err = lzc_destroy_bookmarks(nvl, NULL);
1650a9bcb68mmacy		if (err != 0) {
1651a9bcb68mmacy			(void) zfs_standard_error(g_zfs, err,
1652a9bcb68mmacy			    "cannot destroy bookmark");
1653a9bcb68mmacy		}
1654a9bcb68mmacy
1655a9bcb68mmacy		nvlist_free(nvl);
1656a9bcb68mmacy
1657a9bcb68mmacy		return (err);
1658a9bcb68mmacy	} else {
1659a9bcb68mmacy		/* Open the given dataset */
1660a9bcb68mmacy		if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1661a9bcb68mmacy			return (1);
1662a9bcb68mmacy
1663a9bcb68mmacy		cb.cb_target = zhp;
1664a9bcb68mmacy
1665a9bcb68mmacy		/*
1666a9bcb68mmacy		 * Perform an explicit check for pools before going any further.
1667a9bcb68mmacy		 */
1668a9bcb68mmacy		if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1669a9bcb68mmacy		    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1670a9bcb68mmacy			(void) fprintf(stderr, gettext("cannot destroy '%s': "
1671a9bcb68mmacy			    "operation does not apply to pools\n"),
1672a9bcb68mmacy			    zfs_get_name(zhp));
1673a9bcb68mmacy			(void) fprintf(stderr, gettext("use 'zfs destroy -r "
1674a9bcb68mmacy			    "%s' to destroy all datasets in the pool\n"),
1675a9bcb68mmacy			    zfs_get_name(zhp));
1676a9bcb68mmacy			(void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1677a9bcb68mmacy			    "to destroy the pool itself\n"), zfs_get_name(zhp));
1678a9bcb68mmacy			rv = 1;
1679a9bcb68mmacy			goto out;
1680a9bcb68mmacy		}
1681a9bcb68mmacy
1682a9bcb68mmacy		/*
1683a9bcb68mmacy		 * Check for any dependents and/or clones.
1684a9bcb68mmacy		 */
1685a9bcb68mmacy		cb.cb_first = B_TRUE;
1686a9bcb68mmacy		if (!cb.cb_doclones &&
1687a9bcb68mmacy		    zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1688a9bcb68mmacy		    &cb) != 0) {
1689a9bcb68mmacy			rv = 1;
1690a9bcb68mmacy			goto out;
1691a9bcb68mmacy		}
1692a9bcb68mmacy
1693a9bcb68mmacy		if (cb.cb_error) {
1694a9bcb68mmacy			rv = 1;
1695a9bcb68mmacy			goto out;
1696a9bcb68mmacy		}
1697a9bcb68mmacy		cb.cb_batchedsnaps = fnvlist_alloc();
1698a9bcb68mmacy		if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1699a9bcb68mmacy		    &cb) != 0) {
1700a9bcb68mmacy			rv = 1;
1701a9bcb68mmacy			goto out;
1702a9bcb68mmacy		}
1703a9bcb68mmacy
1704a9bcb68mmacy		/*
1705a9bcb68mmacy		 * Do the real thing.  The callback will close the
1706a9bcb68mmacy		 * handle regardless of whether it succeeds or not.
1707a9bcb68mmacy		 */
1708a9bcb68mmacy		err = destroy_callback(zhp, &cb);
1709a9bcb68mmacy		zhp = NULL;
1710a9bcb68mmacy		if (err == 0) {
1711a9bcb68mmacy			err = zfs_destroy_snaps_nvl(g_zfs,
1712a9bcb68mmacy			    cb.cb_batchedsnaps, cb.cb_defer_destroy);
1713a9bcb68mmacy		}
1714a9bcb68mmacy		if (err != 0 || cb.cb_error == B_TRUE)
1715a9bcb68mmacy			rv = 1;
1716a9bcb68mmacy	}
1717a9bcb68mmacy
1718a9bcb68mmacyout:
1719a9bcb68mmacy	fnvlist_free(cb.cb_batchedsnaps);
1720a9bcb68mmacy	fnvlist_free(cb.cb_nvl);
1721a9bcb68mmacy	if (zhp != NULL)
1722a9bcb68mmacy		zfs_close(zhp);
1723a9bcb68mmacy	return (rv);
1724a9bcb68mmacy}
1725a9bcb68mmacy
1726a9bcb68mmacystatic boolean_t
1727a9bcb68mmacyis_recvd_column(zprop_get_cbdata_t *cbp)
1728a9bcb68mmacy{
1729a9bcb68mmacy	int i;
1730a9bcb68mmacy	zfs_get_column_t col;
1731a9bcb68mmacy
1732a9bcb68mmacy	for (i = 0; i < ZFS_GET_NCOLS &&
1733a9bcb68mmacy	    (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1734a9bcb68mmacy		if (col == GET_COL_RECVD)
1735a9bcb68mmacy			return (B_TRUE);
1736a9bcb68mmacy	return (B_FALSE);
1737a9bcb68mmacy}
1738a9bcb68mmacy
1739a9bcb68mmacy/*
1740a9bcb68mmacy * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1741a9bcb68mmacy *	< all | property[,property]... > < fs | snap | vol > ...
1742a9bcb68mmacy *
1743a9bcb68mmacy *	-r	recurse over any child datasets
1744a9bcb68mmacy *	-H	scripted mode.  Headers are stripped, and fields are separated
1745a9bcb68mmacy *		by tabs instead of spaces.
1746a9bcb68mmacy *	-o	Set of fields to display.  One of "name,property,value,
1747a9bcb68mmacy *		received,source". Default is "name,property,value,source".
1748a9bcb68mmacy *		"all" is an alias for all five.
1749a9bcb68mmacy *	-s	Set of sources to allow.  One of
1750a9bcb68mmacy *		"local,default,inherited,received,temporary,none".  Default is
1751a9bcb68mmacy *		all six.
1752a9bcb68mmacy *	-p	Display values in parsable (literal) format.
1753a9bcb68mmacy *
1754a9bcb68mmacy *  Prints properties for the given datasets.  The user can control which
1755a9bcb68mmacy *  columns to display as well as which property types to allow.
1756a9bcb68mmacy */
1757a9bcb68mmacy
1758a9bcb68mmacy/*
1759a9bcb68mmacy * Invoked to display the properties for a single dataset.
1760a9bcb68mmacy */
1761a9bcb68mmacystatic int
1762a9bcb68mmacyget_callback(zfs_handle_t *zhp, void *data)
1763a9bcb68mmacy{
1764a9bcb68mmacy	char buf[ZFS_MAXPROPLEN];
1765a9bcb68mmacy	char rbuf[ZFS_MAXPROPLEN];
1766a9bcb68mmacy	zprop_source_t sourcetype;
1767a9bcb68mmacy	char source[ZFS_MAX_DATASET_NAME_LEN];
1768a9bcb68mmacy	zprop_get_cbdata_t *cbp = data;
1769a9bcb68mmacy	nvlist_t *user_props = zfs_get_user_props(zhp);
1770a9bcb68mmacy	zprop_list_t *pl = cbp->cb_proplist;
1771a9bcb68mmacy	nvlist_t *propval;
1772a9bcb68mmacy	char *strval;
1773a9bcb68mmacy	char *sourceval;
1774a9bcb68mmacy	boolean_t received = is_recvd_column(cbp);
1775a9bcb68mmacy
1776a9bcb68mmacy	for (; pl != NULL; pl = pl->pl_next) {
1777a9bcb68mmacy		char *recvdval = NULL;
1778a9bcb68mmacy		/*
1779a9bcb68mmacy		 * Skip the special fake placeholder.  This will also skip over
1780a9bcb68mmacy		 * the name property when 'all' is specified.
1781a9bcb68mmacy		 */
1782a9bcb68mmacy		if (pl->pl_prop == ZFS_PROP_NAME &&
1783a9bcb68mmacy		    pl == cbp->cb_proplist)
1784a9bcb68mmacy			continue;
1785a9bcb68mmacy
1786a9bcb68mmacy		if (pl->pl_prop != ZPROP_INVAL) {
1787a9bcb68mmacy			if (zfs_prop_get(zhp, pl->pl_prop, buf,
1788a9bcb68mmacy			    sizeof (buf), &sourcetype, source,
1789a9bcb68mmacy			    sizeof (source),
1790a9bcb68mmacy			    cbp->cb_literal) != 0) {
1791a9bcb68mmacy				if (pl->pl_all)
1792a9bcb68mmacy					continue;
1793a9bcb68mmacy				if (!zfs_prop_valid_for_type(pl->pl_prop,
1794a9bcb68mmacy				    ZFS_TYPE_DATASET, B_FALSE)) {
1795a9bcb68mmacy					(void) fprintf(stderr,
1796a9bcb68mmacy					    gettext("No such property '%s'\n"),
1797a9bcb68mmacy					    zfs_prop_to_name(pl->pl_prop));
1798a9bcb68mmacy					continue;
1799a9bcb68mmacy				}
1800a9bcb68mmacy				sourcetype = ZPROP_SRC_NONE;
1801a9bcb68mmacy				(void) strlcpy(buf, "-", sizeof (buf));
1802a9bcb68mmacy			}
1803a9bcb68mmacy
1804a9bcb68mmacy			if (received && (zfs_prop_get_recvd(zhp,
1805a9bcb68mmacy			    zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1806a9bcb68mmacy			    cbp->cb_literal) == 0))
1807a9bcb68mmacy				recvdval = rbuf;
1808a9bcb68mmacy
1809a9bcb68mmacy			zprop_print_one_property(zfs_get_name(zhp), cbp,
1810a9bcb68mmacy			    zfs_prop_to_name(pl->pl_prop),
1811a9bcb68mmacy			    buf, sourcetype, source, recvdval);
1812a9bcb68mmacy		} else if (zfs_prop_userquota(pl->pl_user_prop)) {
1813a9bcb68mmacy			sourcetype = ZPROP_SRC_LOCAL;
1814a9bcb68mmacy
1815a9bcb68mmacy			if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1816a9bcb68mmacy			    buf, sizeof (buf), cbp->cb_literal) != 0) {
1817a9bcb68mmacy				sourcetype = ZPROP_SRC_NONE;
1818a9bcb68mmacy				(void) strlcpy(buf, "-", sizeof (buf));
1819a9bcb68mmacy			}
1820a9bcb68mmacy
1821a9bcb68mmacy			zprop_print_one_property(zfs_get_name(zhp), cbp,
1822a9bcb68mmacy			    pl->pl_user_prop, buf, sourcetype, source, NULL);
1823a9bcb68mmacy		} else if (zfs_prop_written(pl->pl_user_prop)) {
1824a9bcb68mmacy			sourcetype = ZPROP_SRC_LOCAL;
1825a9bcb68mmacy
1826a9bcb68mmacy			if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1827a9bcb68mmacy			    buf, sizeof (buf), cbp->cb_literal) != 0) {
1828a9bcb68mmacy				sourcetype = ZPROP_SRC_NONE;
1829a9bcb68mmacy				(void) strlcpy(buf, "-", sizeof (buf));
1830a9bcb68mmacy			}
1831a9bcb68mmacy
1832a9bcb68mmacy			zprop_print_one_property(zfs_get_name(zhp), cbp,
1833a9bcb68mmacy			    pl->pl_user_prop, buf, sourcetype, source, NULL);
1834a9bcb68mmacy		} else {
1835a9bcb68mmacy			if (nvlist_lookup_nvlist(user_props,
1836a9bcb68mmacy			    pl->pl_user_prop, &propval) != 0) {
1837a9bcb68mmacy				if (pl->pl_all)
1838a9bcb68mmacy					continue;
1839a9bcb68mmacy				sourcetype = ZPROP_SRC_NONE;
1840a9bcb68mmacy				strval = "-";
1841a9bcb68mmacy			} else {
1842a9bcb68mmacy				verify(nvlist_lookup_string(propval,
1843a9bcb68mmacy				    ZPROP_VALUE, &strval) == 0);
1844a9bcb68mmacy				verify(nvlist_lookup_string(propval,
1845a9bcb68mmacy				    ZPROP_SOURCE, &sourceval) == 0);
1846a9bcb68mmacy
1847a9bcb68mmacy				if (strcmp(sourceval,
1848a9bcb68mmacy				    zfs_get_name(zhp)) == 0) {
1849a9bcb68mmacy					sourcetype = ZPROP_SRC_LOCAL;
1850a9bcb68mmacy				} else if (strcmp(sourceval,
1851a9bcb68mmacy				    ZPROP_SOURCE_VAL_RECVD) == 0) {
1852a9bcb68mmacy					sourcetype = ZPROP_SRC_RECEIVED;
1853a9bcb68mmacy				} else {
1854a9bcb68mmacy					sourcetype = ZPROP_SRC_INHERITED;
1855a9bcb68mmacy					(void) strlcpy(source,
1856a9bcb68mmacy					    sourceval, sizeof (source));
1857a9bcb68mmacy				}
1858a9bcb68mmacy			}
1859a9bcb68mmacy
1860a9bcb68mmacy			if (received && (zfs_prop_get_recvd(zhp,
1861a9bcb68mmacy			    pl->pl_user_prop, rbuf, sizeof (rbuf),
1862a9bcb68mmacy			    cbp->cb_literal) == 0))
1863a9bcb68mmacy				recvdval = rbuf;
1864a9bcb68mmacy
1865a9bcb68mmacy			zprop_print_one_property(zfs_get_name(zhp), cbp,
1866a9bcb68mmacy			    pl->pl_user_prop, strval, sourcetype,
1867a9bcb68mmacy			    source, recvdval);
1868a9bcb68mmacy		}
1869a9bcb68mmacy	}
1870a9bcb68mmacy
1871a9bcb68mmacy	return (0);
1872a9bcb68mmacy}
1873a9bcb68mmacy
1874a9bcb68mmacystatic int
1875a9bcb68mmacyzfs_do_get(int argc, char **argv)
1876a9bcb68mmacy{
1877a9bcb68mmacy	zprop_get_cbdata_t cb = { 0 };
1878a9bcb68mmacy	int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1879a9bcb68mmacy	int types = ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK;
1880a9bcb68mmacy	char *value, *fields;
1881a9bcb68mmacy	int ret = 0;
1882a9bcb68mmacy	int limit = 0;
1883a9bcb68mmacy	zprop_list_t fake_name = { 0 };
1884a9bcb68mmacy
1885a9bcb68mmacy	/*
1886a9bcb68mmacy	 * Set up default columns and sources.
1887a9bcb68mmacy	 */
1888a9bcb68mmacy	cb.cb_sources = ZPROP_SRC_ALL;
1889a9bcb68mmacy	cb.cb_columns[0] = GET_COL_NAME;
1890a9bcb68mmacy	cb.cb_columns[1] = GET_COL_PROPERTY;
1891a9bcb68mmacy	cb.cb_columns[2] = GET_COL_VALUE;
1892a9bcb68mmacy	cb.cb_columns[3] = GET_COL_SOURCE;
1893a9bcb68mmacy	cb.cb_type = ZFS_TYPE_DATASET;
1894a9bcb68mmacy
1895a9bcb68mmacy	/* check options */
1896a9bcb68mmacy	while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1897a9bcb68mmacy		switch (c) {
1898a9bcb68mmacy		case 'p':
1899a9bcb68mmacy			cb.cb_literal = B_TRUE;
1900a9bcb68mmacy			break;
1901a9bcb68mmacy		case 'd':
1902a9bcb68mmacy			limit = parse_depth(optarg, &flags);
1903a9bcb68mmacy			break;
1904a9bcb68mmacy		case 'r':
1905a9bcb68mmacy			flags |= ZFS_ITER_RECURSE;
1906a9bcb68mmacy			break;
1907a9bcb68mmacy		case 'H':
1908a9bcb68mmacy			cb.cb_scripted = B_TRUE;
1909a9bcb68mmacy			break;
1910a9bcb68mmacy		case ':':
1911a9bcb68mmacy			(void) fprintf(stderr, gettext("missing argument for "
1912a9bcb68mmacy			    "'%c' option\n"), optopt);
1913a9bcb68mmacy			usage(B_FALSE);
1914a9bcb68mmacy			break;
1915a9bcb68mmacy		case 'o':
1916a9bcb68mmacy			/*
1917a9bcb68mmacy			 * Process the set of columns to display.  We zero out
1918a9bcb68mmacy			 * the structure to give us a blank slate.
1919a9bcb68mmacy			 */
1920a9bcb68mmacy			bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1921a9bcb68mmacy			i = 0;
1922a9bcb68mmacy			while (*optarg != '\0') {
1923a9bcb68mmacy				static char *col_subopts[] =
1924a9bcb68mmacy				    { "name", "property", "value", "received",
1925a9bcb68mmacy				    "source", "all", NULL };
1926a9bcb68mmacy
1927a9bcb68mmacy				if (i == ZFS_GET_NCOLS) {
1928a9bcb68mmacy					(void) fprintf(stderr, gettext("too "
1929a9bcb68mmacy					    "many fields given to -o "
1930a9bcb68mmacy					    "option\n"));
1931a9bcb68mmacy					usage(B_FALSE);
1932a9bcb68mmacy				}
1933a9bcb68mmacy
1934a9bcb68mmacy				switch (getsubopt(&optarg, col_subopts,
1935a9bcb68mmacy				    &value)) {
1936a9bcb68mmacy				case 0:
1937a9bcb68mmacy					cb.cb_columns[i++] = GET_COL_NAME;
1938a9bcb68mmacy					break;
1939a9bcb68mmacy				case 1:
1940a9bcb68mmacy					cb.cb_columns[i++] = GET_COL_PROPERTY;
1941a9bcb68mmacy					break;
1942a9bcb68mmacy				case 2:
1943a9bcb68mmacy					cb.cb_columns[i++] = GET_COL_VALUE;
1944a9bcb68mmacy					break;
1945a9bcb68mmacy				case 3:
1946a9bcb68mmacy					cb.cb_columns[i++] = GET_COL_RECVD;
1947a9bcb68mmacy					flags |= ZFS_ITER_RECVD_PROPS;
1948a9bcb68mmacy					break;
1949a9bcb68mmacy				case 4:
1950a9bcb68mmacy					cb.cb_columns[i++] = GET_COL_SOURCE;
1951a9bcb68mmacy					break;
1952a9bcb68mmacy				case 5:
1953a9bcb68mmacy					if (i > 0) {
1954a9bcb68mmacy						(void) fprintf(stderr,
1955a9bcb68mmacy						    gettext("\"all\" conflicts "
1956a9bcb68mmacy						    "with specific fields "
1957a9bcb68mmacy						    "given to -o option\n"));
1958a9bcb68mmacy						usage(B_FALSE);
1959a9bcb68mmacy					}
1960a9bcb68mmacy					cb.cb_columns[0] = GET_COL_NAME;
1961a9bcb68mmacy					cb.cb_columns[1] = GET_COL_PROPERTY;
1962a9bcb68mmacy					cb.cb_columns[2] = GET_COL_VALUE;
1963a9bcb68mmacy					cb.cb_columns[3] = GET_COL_RECVD;
1964a9bcb68mmacy					cb.cb_columns[4] = GET_COL_SOURCE;
1965a9bcb68mmacy					flags |= ZFS_ITER_RECVD_PROPS;
1966a9bcb68mmacy					i = ZFS_GET_NCOLS;
1967a9bcb68mmacy					break;
1968a9bcb68mmacy				default:
1969a9bcb68mmacy					(void) fprintf(stderr,
1970a9bcb68mmacy					    gettext("invalid column name "
1971a9bcb68mmacy					    "'%s'\n"), value);
1972a9bcb68mmacy					usage(B_FALSE);
1973a9bcb68mmacy				}
1974a9bcb68mmacy			}
1975a9bcb68mmacy			break;
1976a9bcb68mmacy
1977a9bcb68mmacy		case 's':
1978a9bcb68mmacy			cb.cb_sources = 0;
1979a9bcb68mmacy			while (*optarg != '\0') {
1980a9bcb68mmacy				static char *source_subopts[] = {
1981a9bcb68mmacy					"local", "default", "inherited",
1982a9bcb68mmacy					"received", "temporary", "none",
1983a9bcb68mmacy					NULL };
1984a9bcb68mmacy
1985a9bcb68mmacy				switch (getsubopt(&optarg, source_subopts,
1986a9bcb68mmacy				    &value)) {
1987a9bcb68mmacy				case 0:
1988a9bcb68mmacy					cb.cb_sources |= ZPROP_SRC_LOCAL;
1989a9bcb68mmacy					break;
1990a9bcb68mmacy				case 1:
1991a9bcb68mmacy					cb.cb_sources |= ZPROP_SRC_DEFAULT;
1992a9bcb68mmacy					break;
1993a9bcb68mmacy				case 2:
1994a9bcb68mmacy					cb.cb_sources |= ZPROP_SRC_INHERITED;
1995a9bcb68mmacy					break;
1996a9bcb68mmacy				case 3:
1997a9bcb68mmacy					cb.cb_sources |= ZPROP_SRC_RECEIVED;
1998a9bcb68mmacy					break;
1999a9bcb68mmacy				case 4:
2000a9bcb68mmacy					cb.cb_sources |= ZPROP_SRC_TEMPORARY;
2001a9bcb68mmacy					break;
2002a9bcb68mmacy				case 5:
2003a9bcb68mmacy					cb.cb_sources |= ZPROP_SRC_NONE;
2004a9bcb68mmacy					break;
2005a9bcb68mmacy				default:
2006a9bcb68mmacy					(void) fprintf(stderr,
2007a9bcb68mmacy					    gettext("invalid source "
2008a9bcb68mmacy					    "'%s'\n"), value);
2009a9bcb68mmacy					usage(B_FALSE);
2010a9bcb68mmacy				}
2011a9bcb68mmacy			}
2012a9bcb68mmacy			break;
2013a9bcb68mmacy
2014a9bcb68mmacy		case 't':
2015a9bcb68mmacy			types = 0;
2016a9bcb68mmacy			flags &= ~ZFS_ITER_PROP_LISTSNAPS;
2017a9bcb68mmacy			while (*optarg != '\0') {
2018a9bcb68mmacy				static char *type_subopts[] = { "filesystem",
2019a9bcb68mmacy				    "volume", "snapshot", "snap", "bookmark",
2020a9bcb68mmacy				    "all", NULL };
2021a9bcb68mmacy
2022a9bcb68mmacy				switch (getsubopt(&optarg, type_subopts,
2023a9bcb68mmacy				    &value)) {
2024a9bcb68mmacy				case 0:
2025a9bcb68mmacy					types |= ZFS_TYPE_FILESYSTEM;
2026a9bcb68mmacy					break;
2027a9bcb68mmacy				case 1:
2028a9bcb68mmacy					types |= ZFS_TYPE_VOLUME;
2029a9bcb68mmacy					break;
2030a9bcb68mmacy				case 2:
2031a9bcb68mmacy				case 3:
2032a9bcb68mmacy					types |= ZFS_TYPE_SNAPSHOT;
2033a9bcb68mmacy					break;
2034a9bcb68mmacy				case 4:
2035a9bcb68mmacy					types |= ZFS_TYPE_BOOKMARK;
2036a9bcb68mmacy					break;
2037a9bcb68mmacy				case 5:
2038a9bcb68mmacy					types = ZFS_TYPE_DATASET |
2039a9bcb68mmacy					    ZFS_TYPE_BOOKMARK;
2040a9bcb68mmacy					break;
2041a9bcb68mmacy
2042a9bcb68mmacy				default:
2043a9bcb68mmacy					(void) fprintf(stderr,
2044a9bcb68mmacy					    gettext("invalid type '%s'\n"),
2045a9bcb68mmacy					    value);
2046a9bcb68mmacy					usage(B_FALSE);
2047a9bcb68mmacy				}
2048a9bcb68mmacy			}
2049a9bcb68mmacy			break;
2050a9bcb68mmacy
2051a9bcb68mmacy		case '?':
2052a9bcb68mmacy			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2053a9bcb68mmacy			    optopt);
2054a9bcb68mmacy			usage(B_FALSE);
2055a9bcb68mmacy		}
2056a9bcb68mmacy	}
2057a9bcb68mmacy
2058a9bcb68mmacy	argc -= optind;
2059a9bcb68mmacy	argv += optind;
2060a9bcb68mmacy
2061a9bcb68mmacy	if (argc < 1) {
2062a9bcb68mmacy		(void) fprintf(stderr, gettext("missing property "
2063a9bcb68mmacy		    "argument\n"));
2064a9bcb68mmacy		usage(B_FALSE);
2065a9bcb68mmacy	}
2066a9bcb68mmacy
2067a9bcb68mmacy	fields = argv[0];
2068a9bcb68mmacy
2069a9bcb68mmacy	/*
2070a9bcb68mmacy	 * Handle users who want to get all snapshots or bookmarks
2071a9bcb68mmacy	 * of a dataset (ex. 'zfs get -t snapshot refer <dataset>').
2072a9bcb68mmacy	 */
2073a9bcb68mmacy	if ((types == ZFS_TYPE_SNAPSHOT || types == ZFS_TYPE_BOOKMARK) &&
2074a9bcb68mmacy	    argc > 1 && (flags & ZFS_ITER_RECURSE) == 0 && limit == 0) {
2075a9bcb68mmacy		flags |= (ZFS_ITER_DEPTH_LIMIT | ZFS_ITER_RECURSE);
2076a9bcb68mmacy		limit = 1;
2077a9bcb68mmacy	}
2078a9bcb68mmacy
2079a9bcb68mmacy	if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
2080a9bcb68mmacy	    != 0)
2081a9bcb68mmacy		usage(B_FALSE);
2082a9bcb68mmacy
2083a9bcb68mmacy	argc--;
2084a9bcb68mmacy	argv++;
2085a9bcb68mmacy
2086a9bcb68mmacy	/*
2087a9bcb68mmacy	 * As part of zfs_expand_proplist(), we keep track of the maximum column
2088a9bcb68mmacy	 * width for each property.  For the 'NAME' (and 'SOURCE') columns, we
2089a9bcb68mmacy	 * need to know the maximum name length.  However, the user likely did
2090a9bcb68mmacy	 * not specify 'name' as one of the properties to fetch, so we need to
2091a9bcb68mmacy	 * make sure we always include at least this property for
2092a9bcb68mmacy	 * print_get_headers() to work properly.
2093a9bcb68mmacy	 */
2094a9bcb68mmacy	if (cb.cb_proplist != NULL) {
2095a9bcb68mmacy		fake_name.pl_prop = ZFS_PROP_NAME;
2096a9bcb68mmacy		fake_name.pl_width = strlen(gettext("NAME"));
2097a9bcb68mmacy		fake_name.pl_next = cb.cb_proplist;
2098a9bcb68mmacy		cb.cb_proplist = &fake_name;
2099a9bcb68mmacy	}
2100a9bcb68mmacy
2101a9bcb68mmacy	cb.cb_first = B_TRUE;
2102a9bcb68mmacy
2103a9bcb68mmacy	/* run for each object */
2104a9bcb68mmacy	ret = zfs_for_each(argc, argv, flags, types, NULL,
2105a9bcb68mmacy	    &cb.cb_proplist, limit, get_callback, &cb);
2106a9bcb68mmacy
2107a9bcb68mmacy	if (cb.cb_proplist == &fake_name)
2108a9bcb68mmacy		zprop_free_list(fake_name.pl_next);
2109a9bcb68mmacy	else
2110a9bcb68mmacy		zprop_free_list(cb.cb_proplist);
2111a9bcb68mmacy
2112a9bcb68mmacy	return (ret);
2113a9bcb68mmacy}
2114a9bcb68mmacy
2115a9bcb68mmacy/*
2116a9bcb68mmacy * inherit [-rS] <property> <fs|vol> ...
2117a9bcb68mmacy *
2118a9bcb68mmacy *	-r	Recurse over all children
2119a9bcb68mmacy *	-S	Revert to received value, if any
2120a9bcb68mmacy *
2121a9bcb68mmacy * For each dataset specified on the command line, inherit the given property
2122a9bcb68mmacy * from its parent.  Inheriting a property at the pool level will cause it to
2123a9bcb68mmacy * use the default value.  The '-r' flag will recurse over all children, and is
2124a9bcb68mmacy * useful for setting a property on a hierarchy-wide basis, regardless of any
2125a9bcb68mmacy * local modifications for each dataset.
2126a9bcb68mmacy */
2127a9bcb68mmacy
2128a9bcb68mmacytypedef struct inherit_cbdata {
2129a9bcb68mmacy	const char *cb_propname;
2130a9bcb68mmacy	boolean_t cb_received;
2131a9bcb68mmacy} inherit_cbdata_t;
2132a9bcb68mmacy
2133a9bcb68mmacystatic int
2134a9bcb68mmacyinherit_recurse_cb(zfs_handle_t *zhp, void *data)
2135a9bcb68mmacy{
2136a9bcb68mmacy	inherit_cbdata_t *cb = data;
2137a9bcb68mmacy	zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
2138a9bcb68mmacy
2139a9bcb68mmacy	/*
2140a9bcb68mmacy	 * If we're doing it recursively, then ignore properties that
2141a9bcb68mmacy	 * are not valid for this type of dataset.
2142a9bcb68mmacy	 */
2143a9bcb68mmacy	if (prop != ZPROP_INVAL &&
2144a9bcb68mmacy	    !zfs_prop_valid_for_type(prop, zfs_get_type(zhp), B_FALSE))
2145a9bcb68mmacy		return (0);
2146a9bcb68mmacy
2147a9bcb68mmacy	return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
2148a9bcb68mmacy}
2149a9bcb68mmacy
2150a9bcb68mmacystatic int
2151a9bcb68mmacyinherit_cb(zfs_handle_t *zhp, void *data)
2152a9bcb68mmacy{
2153a9bcb68mmacy	inherit_cbdata_t *cb = data;
2154a9bcb68mmacy
2155a9bcb68mmacy	return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
2156a9bcb68mmacy}
2157a9bcb68mmacy
2158a9bcb68mmacystatic int
2159a9bcb68mmacyzfs_do_inherit(int argc, char **argv)
2160a9bcb68mmacy{
2161a9bcb68mmacy	int c;
2162a9bcb68mmacy	zfs_prop_t prop;
2163a9bcb68mmacy	inherit_cbdata_t cb = { 0 };
2164a9bcb68mmacy	char *propname;
2165a9bcb68mmacy	int ret = 0;
2166a9bcb68mmacy	int flags = 0;
2167a9bcb68mmacy	boolean_t received = B_FALSE;
2168a9bcb68mmacy
2169a9bcb68mmacy	/* check options */
2170a9bcb68mmacy	while ((c = getopt(argc, argv, "rS")) != -1) {
2171a9bcb68mmacy		switch (c) {
2172a9bcb68mmacy		case 'r':
2173a9bcb68mmacy			flags |= ZFS_ITER_RECURSE;
2174a9bcb68mmacy			break;
2175a9bcb68mmacy		case 'S':
2176a9bcb68mmacy			received = B_TRUE;
2177a9bcb68mmacy			break;
2178a9bcb68mmacy		case '?':
2179a9bcb68mmacy		default:
2180a9bcb68mmacy			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2181a9bcb68mmacy			    optopt);
2182a9bcb68mmacy			usage(B_FALSE);
2183a9bcb68mmacy		}
2184a9bcb68mmacy	}
2185a9bcb68mmacy
2186a9bcb68mmacy	argc -= optind;
2187a9bcb68mmacy	argv += optind;
2188a9bcb68mmacy
2189a9bcb68mmacy	/* check number of arguments */
2190a9bcb68mmacy	if (argc < 1) {
2191a9bcb68mmacy		(void) fprintf(stderr, gettext("missing property argument\n"));
2192a9bcb68mmacy		usage(B_FALSE);
2193a9bcb68mmacy	}
2194a9bcb68mmacy	if (argc < 2) {
2195a9bcb68mmacy		(void) fprintf(stderr, gettext("missing dataset argument\n"));
2196a9bcb68mmacy		usage(B_FALSE);
2197a9bcb68mmacy	}
2198a9bcb68mmacy
2199a9bcb68mmacy	propname = argv[0];
2200a9bcb68mmacy	argc--;
2201a9bcb68mmacy	argv++;
2202a9bcb68mmacy
2203a9bcb68mmacy	if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
2204a9bcb68mmacy		if (zfs_prop_readonly(prop)) {
2205a9bcb68mmacy			(void) fprintf(stderr, gettext(
2206a9bcb68mmacy			    "%s property is read-only\n"),
2207a9bcb68mmacy			    propname);
2208a9bcb68mmacy			return (1);
2209a9bcb68mmacy		}
2210a9bcb68mmacy		if (!zfs_prop_inheritable(prop) && !received) {
2211a9bcb68mmacy			(void) fprintf(stderr, gettext("'%s' property cannot "
2212a9bcb68mmacy			    "be inherited\n"), propname);
2213a9bcb68mmacy			if (prop == ZFS_PROP_QUOTA ||
2214a9bcb68mmacy			    prop == ZFS_PROP_RESERVATION ||
2215a9bcb68mmacy			    prop == ZFS_PROP_REFQUOTA ||
2216a9bcb68mmacy			    prop == ZFS_PROP_REFRESERVATION) {
2217a9bcb68mmacy				(void) fprintf(stderr, gettext("use 'zfs set "
2218a9bcb68mmacy				    "%s=none' to clear\n"), propname);
2219a9bcb68mmacy				(void) fprintf(stderr, gettext("use 'zfs "
2220a9bcb68mmacy				    "inherit -S %s' to revert to received "
2221a9bcb68mmacy				    "value\n"), propname);
2222a9bcb68mmacy			}
2223a9bcb68mmacy			return (1);
2224a9bcb68mmacy		}
2225a9bcb68mmacy		if (received && (prop == ZFS_PROP_VOLSIZE ||
2226a9bcb68mmacy		    prop == ZFS_PROP_VERSION)) {
2227a9bcb68mmacy			(void) fprintf(stderr, gettext("'%s' property cannot "
2228a9bcb68mmacy			    "be reverted to a received value\n"), propname);
2229a9bcb68mmacy			return (1);
2230a9bcb68mmacy		}
2231a9bcb68mmacy	} else if (!zfs_prop_user(propname)) {
2232a9bcb68mmacy		(void) fprintf(stderr, gettext("invalid property '%s'\n"),
2233a9bcb68mmacy		    propname);
2234a9bcb68mmacy		usage(B_FALSE);
2235a9bcb68mmacy	}
2236a9bcb68mmacy
2237a9bcb68mmacy	cb.cb_propname = propname;
2238a9bcb68mmacy	cb.cb_received = received;
2239a9bcb68mmacy
2240a9bcb68mmacy	if (flags & ZFS_ITER_RECURSE) {
2241a9bcb68mmacy		ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2242a9bcb68mmacy		    NULL, NULL, 0, inherit_recurse_cb, &cb);
2243a9bcb68mmacy	} else {
2244a9bcb68mmacy		ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2245a9bcb68mmacy		    NULL, NULL, 0, inherit_cb, &cb);
2246a9bcb68mmacy	}
2247a9bcb68mmacy
2248a9bcb68mmacy	return (ret);
2249a9bcb68mmacy}
2250a9bcb68mmacy
2251a9bcb68mmacytypedef struct upgrade_cbdata {
2252a9bcb68mmacy	uint64_t cb_numupgraded;
2253a9bcb68mmacy	uint64_t cb_numsamegraded;
2254a9bcb68mmacy	uint64_t cb_numfailed;
2255a9bcb68mmacy	uint64_t cb_version;
2256a9bcb68mmacy	boolean_t cb_newer;
2257a9bcb68mmacy	boolean_t cb_foundone;
2258a9bcb68mmacy	char cb_lastfs[ZFS_MAX_DATASET_NAME_LEN];
2259a9bcb68mmacy} upgrade_cbdata_t;
2260a9bcb68mmacy
2261a9bcb68mmacystatic int
2262a9bcb68mmacysame_pool(zfs_handle_t *zhp, const char *name)
2263a9bcb68mmacy{
2264a9bcb68mmacy	int len1 = strcspn(name, "/@");
2265a9bcb68mmacy	const char *zhname = zfs_get_name(zhp);
2266a9bcb68mmacy	int len2 = strcspn(zhname, "/@");
2267a9bcb68mmacy
2268a9bcb68mmacy	if (len1 != len2)
2269a9bcb68mmacy		return (B_FALSE);
2270a9bcb68mmacy	return (strncmp(name, zhname, len1) == 0);
2271a9bcb68mmacy}
2272a9bcb68mmacy
2273a9bcb68mmacystatic int
2274a9bcb68mmacyupgrade_list_callback(zfs_handle_t *zhp, void *data)
2275a9bcb68mmacy{
2276a9bcb68mmacy	upgrade_cbdata_t *cb = data;
2277a9bcb68mmacy	int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2278a9bcb68mmacy
2279a9bcb68mmacy	/* list if it's old/new */
2280a9bcb68mmacy	if ((!cb->cb_newer && version < ZPL_VERSION) ||
2281a9bcb68mmacy	    (cb->cb_newer && version > ZPL_VERSION)) {
2282a9bcb68mmacy		char *str;
2283a9bcb68mmacy		if (cb->cb_newer) {
2284a9bcb68mmacy			str = gettext("The following filesystems are "
2285a9bcb68mmacy			    "formatted using a newer software version and\n"
2286a9bcb68mmacy			    "cannot be accessed on the current system.\n\n");
2287a9bcb68mmacy		} else {
2288a9bcb68mmacy			str = gettext("The following filesystems are "
2289a9bcb68mmacy			    "out of date, and can be upgraded.  After being\n"
2290a9bcb68mmacy			    "upgraded, these filesystems (and any 'zfs send' "
2291a9bcb68mmacy			    "streams generated from\n"
2292a9bcb68mmacy			    "subsequent snapshots) will no longer be "
2293a9bcb68mmacy			    "accessible by older software versions.\n\n");
2294a9bcb68mmacy		}
2295a9bcb68mmacy
2296a9bcb68mmacy		if (!cb->cb_foundone) {
2297a9bcb68mmacy			(void) puts(str);
2298a9bcb68mmacy			(void) printf(gettext("VER  FILESYSTEM\n"));
2299a9bcb68mmacy			(void) printf(gettext("---  ------------\n"));
2300a9bcb68mmacy			cb->cb_foundone = B_TRUE;
2301a9bcb68mmacy		}
2302a9bcb68mmacy
2303a9bcb68mmacy		(void) printf("%2u   %s\n", version, zfs_get_name(zhp));
2304a9bcb68mmacy	}
2305a9bcb68mmacy
2306a9bcb68mmacy	return (0);
2307a9bcb68mmacy}
2308a9bcb68mmacy
2309a9bcb68mmacystatic int
2310a9bcb68mmacyupgrade_set_callback(zfs_handle_t *zhp, void *data)
2311a9bcb68mmacy{
2312a9bcb68mmacy	upgrade_cbdata_t *cb = data;
2313a9bcb68mmacy	int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2314a9bcb68mmacy	int needed_spa_version;
2315a9bcb68mmacy	int spa_version;
2316a9bcb68mmacy
2317a9bcb68mmacy	if (zfs_spa_version(zhp, &spa_version) < 0)
2318a9bcb68mmacy		return (-1);
2319a9bcb68mmacy
2320a9bcb68mmacy	needed_spa_version = zfs_spa_version_map(cb->cb_version);
2321a9bcb68mmacy
2322a9bcb68mmacy	if (needed_spa_version < 0)
2323a9bcb68mmacy		return (-1);
2324a9bcb68mmacy
2325a9bcb68mmacy	if (spa_version < needed_spa_version) {
2326a9bcb68mmacy		/* can't upgrade */
2327a9bcb68mmacy		(void) printf(gettext("%s: can not be "
2328a9bcb68mmacy		    "upgraded; the pool version needs to first "
2329a9bcb68mmacy		    "be upgraded\nto version %d\n\n"),
2330a9bcb68mmacy		    zfs_get_name(zhp), needed_spa_version);
2331a9bcb68mmacy		cb->cb_numfailed++;
2332a9bcb68mmacy		return (0);
2333a9bcb68mmacy	}
2334a9bcb68mmacy
2335a9bcb68mmacy	/* upgrade */
2336a9bcb68mmacy	if (version < cb->cb_version) {
2337a9bcb68mmacy		char verstr[16];
2338a9bcb68mmacy		(void) snprintf(verstr, sizeof (verstr),
2339a9bcb68mmacy		    "%llu", (u_longlong_t)cb->cb_version);
2340a9bcb68mmacy		if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2341a9bcb68mmacy			/*
2342a9bcb68mmacy			 * If they did "zfs upgrade -a", then we could
2343a9bcb68mmacy			 * be doing ioctls to different pools.  We need
2344a9bcb68mmacy			 * to log this history once to each pool, and bypass
2345a9bcb68mmacy			 * the normal history logging that happens in main().
2346a9bcb68mmacy			 */
2347a9bcb68mmacy			(void) zpool_log_history(g_zfs, history_str);
2348a9bcb68mmacy			log_history = B_FALSE;
2349a9bcb68mmacy		}
2350a9bcb68mmacy		if (zfs_prop_set(zhp, "version", verstr) == 0)
2351a9bcb68mmacy			cb->cb_numupgraded++;
2352a9bcb68mmacy		else
2353a9bcb68mmacy			cb->cb_numfailed++;
2354a9bcb68mmacy		(void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2355a9bcb68mmacy	} else if (version > cb->cb_version) {
2356a9bcb68mmacy		/* can't downgrade */
2357a9bcb68mmacy		(void) printf(gettext("%s: can not be downgraded; "
2358a9bcb68mmacy		    "it is already at version %u\n"),
2359a9bcb68mmacy		    zfs_get_name(zhp), version);
2360a9bcb68mmacy		cb->cb_numfailed++;
2361a9bcb68mmacy	} else {
2362a9bcb68mmacy		cb->cb_numsamegraded++;
2363a9bcb68mmacy	}
2364a9bcb68mmacy	return (0);
2365a9bcb68mmacy}
2366a9bcb68mmacy
2367a9bcb68mmacy/*
2368a9bcb68mmacy * zfs upgrade
2369a9bcb68mmacy * zfs upgrade -v
2370a9bcb68mmacy * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2371a9bcb68mmacy */
2372a9bcb68mmacystatic int
2373a9bcb68mmacyzfs_do_upgrade(int argc, char **argv)
2374a9bcb68mmacy{
2375a9bcb68mmacy	boolean_t all = B_FALSE;
2376a9bcb68mmacy	boolean_t showversions = B_FALSE;
2377a9bcb68mmacy	int ret = 0;
2378a9bcb68mmacy	upgrade_cbdata_t cb = { 0 };
2379a9bcb68mmacy	int c;
2380a9bcb68mmacy	int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2381a9bcb68mmacy
2382a9bcb68mmacy	/* check options */
2383a9bcb68mmacy	while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2384a9bcb68mmacy		switch (c) {
2385a9bcb68mmacy		case 'r':
2386a9bcb68mmacy			flags |= ZFS_ITER_RECURSE;
2387a9bcb68mmacy			break;
2388a9bcb68mmacy		case 'v':
2389a9bcb68mmacy			showversions = B_TRUE;
2390a9bcb68mmacy			break;
2391a9bcb68mmacy		case 'V':
2392a9bcb68mmacy			if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2393a9bcb68mmacy			    optarg, &cb.cb_version) != 0) {
2394a9bcb68mmacy				(void) fprintf(stderr,
2395a9bcb68mmacy				    gettext("invalid version %s\n"), optarg);
2396a9bcb68mmacy				usage(B_FALSE);
2397a9bcb68mmacy			}
2398a9bcb68mmacy			break;
2399a9bcb68mmacy		case 'a':
2400a9bcb68mmacy			all = B_TRUE;
2401a9bcb68mmacy			break;
2402a9bcb68mmacy		case '?':
2403a9bcb68mmacy		default:
2404a9bcb68mmacy			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
2405a9bcb68mmacy			    optopt);
2406a9bcb68mmacy			usage(B_FALSE);
2407a9bcb68mmacy		}
2408a9bcb68mmacy	}
2409a9bcb68mmacy
2410a9bcb68mmacy	argc -= optind;
2411a9bcb68mmacy	argv += optind;
2412a9bcb68mmacy
2413a9bcb68mmacy	if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2414a9bcb68mmacy		usage(B_FALSE);
2415a9bcb68mmacy	if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2416a9bcb68mmacy	    cb.cb_version || argc))
2417a9bcb68mmacy		usage(B_FALSE);
2418a9bcb68mmacy	if ((all || argc) && (showversions))
2419a9bcb68mmacy		usage(B_FALSE);
2420a9bcb68mmacy	if (all && argc)
2421a9bcb68mmacy		usage(B_FALSE);
2422a9bcb68mmacy
2423a9bcb68mmacy	if (showversions) {
2424a9bcb68mmacy		/* Show info on available versions. */
2425a9bcb68mmacy		(void) printf(gettext("The following filesystem versions are "
2426a9bcb68mmacy		    "supported:\n\n"));
2427a9bcb68mmacy		(void) printf(gettext("VER  DESCRIPTION\n"));
2428a9bcb68mmacy		(void) printf("---  -----------------------------------------"
2429a9bcb68mmacy		    "---------------\n");
2430a9bcb68mmacy		(void) printf(gettext(" 1   Initial ZFS filesystem version\n"));
2431a9bcb68mmacy		(void) printf(gettext(" 2   Enhanced directory entries\n"));
2432a9bcb68mmacy		(void) printf(gettext(" 3   Case insensitive and filesystem "
2433a9bcb68mmacy		    "user identifier (FUID)\n"));
2434a9bcb68mmacy		(void) printf(gettext(" 4   userquota, groupquota "
2435a9bcb68mmacy		    "properties\n"));
2436a9bcb68mmacy		(void) printf(gettext(" 5   System attributes\n"));
2437a9bcb68mmacy		(void) printf(gettext("\nFor more information on a particular "
2438a9bcb68mmacy		    "version, including supported releases,\n"));
2439a9bcb68mmacy		(void) printf("see the ZFS Administration Guide.\n\n");
2440a9bcb68mmacy		ret = 0;
2441a9bcb68mmacy	} else if (argc || all) {
2442a9bcb68mmacy		/* Upgrade filesystems */
2443a9bcb68mmacy		if (cb.cb_version == 0)
2444a9bcb68mmacy			cb.cb_version = ZPL_VERSION;
2445a9bcb68mmacy		ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2446a9bcb68mmacy		    NULL, NULL, 0, upgrade_set_callback, &cb);
2447a9bcb68mmacy		(void) printf(gettext("%llu filesystems upgraded\n"),
2448a9bcb68mmacy		    (u_longlong_t)cb.cb_numupgraded);
2449a9bcb68mmacy		if (cb.cb_numsamegraded) {
2450a9bcb68mmacy			(void) printf(gettext("%llu filesystems already at "
2451a9bcb68mmacy			    "this version\n"),
2452a9bcb68mmacy			    (u_longlong_t)cb.cb_numsamegraded);
2453a9bcb68mmacy		}
2454a9bcb68mmacy		if (cb.cb_numfailed != 0)
2455a9bcb68mmacy			ret = 1;
2456a9bcb68mmacy	} else {
2457a9bcb68mmacy		/* List old-version filesystems */
2458a9bcb68mmacy		boolean_t found;
2459a9bcb68mmacy		(void) printf(gettext("This system is currently running "
2460a9bcb68mmacy		    "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2461a9bcb68mmacy
2462a9bcb68mmacy		flags |= ZFS_ITER_RECURSE;
2463a9bcb68mmacy		ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2464a9bcb68mmacy		    NULL, NULL, 0, upgrade_list_callback, &cb);
2465a9bcb68mmacy
2466a9bcb68mmacy		found = cb.cb_foundone;
2467a9bcb68mmacy		cb.cb_foundone = B_FALSE;
2468a9bcb68mmacy		cb.cb_newer = B_TRUE;
2469a9bcb68mmacy
2470a9bcb68mmacy		ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2471a9bcb68mmacy		    NULL, NULL, 0, upgrade_list_callback, &cb);
2472a9bcb68mmacy
2473a9bcb68mmacy		if (!cb.cb_foundone && !found) {
2474a9bcb68mmacy			(void) printf(gettext("All filesystems are "
2475a9bcb68mmacy			    "formatted with the current version.\n"));
2476a9bcb68mmacy		}
2477a9bcb68mmacy	}
2478a9bcb68mmacy
2479a9bcb68mmacy	return (ret);
2480a9bcb68mmacy}
2481a9bcb68mmacy
2482a9bcb68mmacy/*
2483a9bcb68mmacy * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2484f7a21c3mmacy *               [-S field [-S field]...] [-t type[,...]]
2485f7a21c3mmacy *               filesystem | snapshot | path
2486a9bcb68mmacy * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2487f7a21c3mmacy *                [-S field [-S field]...] [-t type[,...]]
2488f7a21c3mmacy *                filesystem | snapshot | path
2489a9bcb68mmacy * zfs projectspace [-Hp] [-o field[,...]] [-s field [-s field]...]
2490f7a21c3mmacy *                [-S field [-S field]...] filesystem | snapshot | path
2491a9bcb68mmacy *
2492a9bcb68mmacy *	-H      Scripted mode; elide headers and separate columns by tabs.
2493a9bcb68mmacy *	-i	Translate SID to POSIX ID.
2494a9bcb68mmacy *	-n	Print numeric ID instead of user/group name.
2495a9bcb68mmacy *	-o      Control which fields to display.
2496a9bcb68mmacy *	-p	Use exact (parsable) numeric output.
2497a9bcb68mmacy *	-s      Specify sort columns, descending order.
2498a9bcb68mmacy *	-S      Specify sort columns, ascending order.
2499a9bcb68mmacy *	-t      Control which object types to display.
2500a9bcb68mmacy *
2501a9bcb68mmacy *	Displays space consumed by, and quotas on, each user in the specified
2502a9bcb68mmacy *	filesystem or snapshot.
2503a9bcb68mmacy */
2504a9bcb68mmacy
2505a9bcb68mmacy/* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2506a9bcb68mmacyenum us_field_types {
2507a9bcb68mmacy	USFIELD_TYPE,
2508a9bcb68mmacy	USFIELD_NAME,
2509a9bcb68mmacy	USFIELD_USED,
2510a9bcb68mmacy	USFIELD_QUOTA,
2511a9bcb68mmacy	USFIELD_OBJUSED,
2512a9bcb68mmacy	USFIELD_OBJQUOTA
2513a9bcb68mmacy};
2514a9bcb68mmacystatic char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA",
2515a9bcb68mmacy				    "OBJUSED", "OBJQUOTA" };
2516a9bcb68mmacystatic char *us_field_names[] = { "type", "name", "used", "quota",
2517a9bcb68mmacy				    "objused", "objquota" };
2518a9bcb68mmacy#define	USFIELD_LAST	(sizeof (us_field_names) / sizeof (char *))
2519a9bcb68mmacy
2520a9bcb68mmacy#define	USTYPE_PSX_GRP	(1 << 0)
2521a9bcb68mmacy#define	USTYPE_PSX_USR	(1 << 1)
2522a9bcb68mmacy#define	USTYPE_SMB_GRP	(1 << 2)
2523a9bcb68mmacy#define	USTYPE_SMB_USR	(1 << 3)
2524a9bcb68mmacy#define	USTYPE_PROJ	(1 << 4)
2525a9bcb68mmacy#define	USTYPE_ALL	\
2526a9bcb68mmacy	(USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR | \
2527a9bcb68mmacy	    USTYPE_PROJ)
2528a9bcb68mmacy
2529a9bcb68mmacystatic int us_type_bits[] = {
2530a9bcb68mmacy	USTYPE_PSX_GRP,
2531a9bcb68mmacy	USTYPE_PSX_USR,
2532a9bcb68mmacy	USTYPE_SMB_GRP,
2533a9bcb68mmacy	USTYPE_SMB_USR,
2534a9bcb68mmacy	USTYPE_ALL
2535a9bcb68mmacy};
2536a9bcb68mmacystatic char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2537a9bcb68mmacy	"smbuser", "all" };
2538a9bcb68mmacy
2539a9bcb68mmacytypedef struct us_node {
2540a9bcb68mmacy	nvlist_t	*usn_nvl;
2541a9bcb68mmacy	uu_avl_node_t	usn_avlnode;
2542a9bcb68mmacy	uu_list_node_t	usn_listnode;
2543a9bcb68mmacy} us_node_t;
2544a9bcb68mmacy
2545a9bcb68mmacytypedef struct us_cbdata {
2546a9bcb68mmacy	nvlist_t	**cb_nvlp;
2547a9bcb68mmacy	uu_avl_pool_t	*cb_avl_pool;
2548a9bcb68mmacy	uu_avl_t	*cb_avl;
2549a9bcb68mmacy	boolean_t	cb_numname;
2550a9bcb68mmacy	boolean_t	cb_nicenum;
2551a9bcb68mmacy	boolean_t	cb_sid2posix;
2552a9bcb68mmacy	zfs_userquota_prop_t cb_prop;
2553a9bcb68mmacy	zfs_sort_column_t *cb_sortcol;
2554a9bcb68mmacy	size_t		cb_width[USFIELD_LAST];
2555a9bcb68mmacy} us_cbdata_t;
2556a9bcb68mmacy
2557a9bcb68mmacystatic boolean_t us_populated = B_FALSE;
2558a9bcb68mmacy
2559a9bcb68mmacytypedef struct {
2560a9bcb68mmacy	zfs_sort_column_t *si_sortcol;
2561a9bcb68mmacy	boolean_t	si_numname;
2562a9bcb68mmacy} us_sort_info_t;
2563a9bcb68mmacy
2564a9bcb68mmacystatic int
2565a9bcb68mmacyus_field_index(char *field)
2566a9bcb68mmacy{
2567a9bcb68mmacy	int i;
2568a9bcb68mmacy
2569a9bcb68mmacy	for (i = 0; i < USFIELD_LAST; i++) {
2570a9bcb68mmacy		if (strcmp(field, us_field_names[i]) == 0)
2571a9bcb68mmacy			return (i);
2572a9bcb68mmacy	}
2573a9bcb68mmacy
2574a9bcb68mmacy	return (-1);
2575a9bcb68mmacy}
2576a9bcb68mmacy
2577a9bcb68mmacystatic int
2578a9bcb68mmacyus_compare(const void *larg, const void *rarg, void *unused)
2579a9bcb68mmacy{
2580a9bcb68mmacy	const us_node_t *l = larg;
2581a9bcb68mmacy	const us_node_t *r = rarg;
2582a9bcb68mmacy	us_sort_info_t *si = (us_sort_info_t *)unused;
2583a9bcb68mmacy	zfs_sort_column_t *sortcol = si->si_sortcol;
2584a9bcb68mmacy	boolean_t numname = si->si_numname;
2585a9bcb68mmacy	nvlist_t *lnvl = l->usn_nvl;
2586a9bcb68mmacy	nvlist_t *rnvl = r->usn_nvl;
2587a9bcb68mmacy	int rc = 0;
2588a9bcb68mmacy	boolean_t lvb, rvb;
2589a9bcb68mmacy
2590a9bcb68mmacy	for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2591a9bcb68mmacy		char *lvstr = "";
2592a9bcb68mmacy		char *rvstr = "";
2593a9bcb68mmacy		uint32_t lv32 = 0;
2594a9bcb68mmacy		uint32_t rv32 = 0;
2595a9bcb68mmacy		uint64_t lv64 = 0;
2596a9bcb68mmacy		uint64_t rv64 = 0;
2597a9bcb68mmacy		zfs_prop_t prop = sortcol->sc_prop;
2598a9bcb68mmacy		const char *propname = NULL;
2599a9bcb68mmacy		boolean_t reverse = sortcol->sc_reverse;
2600a9bcb68mmacy
2601a9bcb68mmacy		switch (prop) {
2602a9bcb68mmacy		case ZFS_PROP_TYPE:
2603a9bcb68mmacy			propname = "type";
2604a9bcb68mmacy			(void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2605a9bcb68mmacy			(void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2606a9bcb68mmacy			if (rv32 != lv32)
2607a9bcb68mmacy				rc = (rv32 < lv32) ? 1 : -1;
2608a9bcb68mmacy			break;
2609a9bcb68mmacy		case ZFS_PROP_NAME:
2610a9bcb68mmacy			propname = "name";
2611a9bcb68mmacy			if (numname) {
2612a9bcb68mmacycompare_nums:
2613a9bcb68mmacy				(void) nvlist_lookup_uint64(lnvl, propname,
2614a9bcb68mmacy				    &lv64);
2615a9bcb68mmacy				(void) nvlist_lookup_uint64(rnvl, propname,
2616a9bcb68mmacy				    &rv64);
2617a9bcb68mmacy				if (rv64 != lv64)
2618a9bcb68mmacy					rc = (rv64 < lv64) ? 1 : -1;
2619a9bcb68mmacy			} else {
2620a9bcb68mmacy				if ((nvlist_lookup_string(lnvl, propname,
2621a9bcb68mmacy				    &lvstr) == ENOENT) ||
2622a9bcb68mmacy				    (nvlist_lookup_string(rnvl, propname,
2623a9bcb68mmacy				    &rvstr) == ENOENT)) {
2624a9bcb68mmacy					goto compare_nums;
2625a9bcb68mmacy				}
2626a9bcb68mmacy				rc = strcmp(lvstr, rvstr);
2627a9bcb68mmacy			}
2628a9bcb68mmacy			break;
2629a9bcb68mmacy		case ZFS_PROP_USED:
2630a9bcb68mmacy		case ZFS_PROP_QUOTA:
2631a9bcb68mmacy			if (!us_populated)
2632a9bcb68mmacy				break;
2633a9bcb68mmacy			if (prop == ZFS_PROP_USED)
2634a9bcb68mmacy				propname = "used";
2635a9bcb68mmacy			else
2636a9bcb68mmacy				propname = "quota";
2637a9bcb68mmacy			(void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2638a9bcb68mmacy			(void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2639a9bcb68mmacy			if (rv64 != lv64)
2640a9bcb68mmacy				rc = (rv64 < lv64) ? 1 : -1;
2641a9bcb68mmacy			break;
2642a9bcb68mmacy
2643a9bcb68mmacy		default:
2644a9bcb68mmacy			break;
2645a9bcb68mmacy		}
2646a9bcb68mmacy
2647a9bcb68mmacy		if (rc != 0) {
2648a9bcb68mmacy			if (rc < 0)
2649a9bcb68mmacy				return (reverse ? 1 : -1);
2650a9bcb68mmacy			else
2651a9bcb68mmacy				return (reverse ? -1 : 1);
2652a9bcb68mmacy		}
2653a9bcb68mmacy	}
2654a9bcb68mmacy
2655a9bcb68mmacy	/*
2656a9bcb68mmacy	 * If entries still seem to be the same, check if they are of the same
2657a9bcb68mmacy	 * type (smbentity is added only if we are doing SID to POSIX ID
2658a9bcb68mmacy	 * translation where we can have duplicate type/name combinations).
2659a9bcb68mmacy	 */
2660a9bcb68mmacy	if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2661a9bcb68mmacy	    nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2662a9bcb68mmacy	    lvb != rvb)
2663a9bcb68mmacy		return (lvb < rvb ? -1 : 1);
2664a9bcb68mmacy
2665a9bcb68mmacy	return (0);
2666a9bcb68mmacy}
2667a9bcb68mmacy
2668a9bcb68mmacystatic boolean_t
2669a9bcb68mmacyzfs_prop_is_user(unsigned p)
2670a9bcb68mmacy{
2671a9bcb68mmacy	return (p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA ||
2672a9bcb68mmacy	    p == ZFS_PROP_USEROBJUSED || p == ZFS_PROP_USEROBJQUOTA);
2673a9bcb68mmacy}
2674a9bcb68mmacy
2675a9bcb68mmacystatic boolean_t
2676a9bcb68mmacyzfs_prop_is_group(unsigned p)
2677a9bcb68mmacy{
2678a9bcb68mmacy	return (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA ||
2679a9bcb68mmacy	    p == ZFS_PROP_GROUPOBJUSED || p == ZFS_PROP_GROUPOBJQUOTA);
2680a9bcb68mmacy}
2681a9bcb68mmacy
2682a9bcb68mmacystatic boolean_t
2683a9bcb68mmacyzfs_prop_is_project(unsigned p)
2684a9bcb68mmacy{
2685a9bcb68mmacy	return (p == ZFS_PROP_PROJECTUSED || p == ZFS_PROP_PROJECTQUOTA ||
2686a9bcb68mmacy	    p == ZFS_PROP_PROJECTOBJUSED || p == ZFS_PROP_PROJECTOBJQUOTA);
2687a9bcb68mmacy}
2688a9bcb68mmacy
2689a9bcb68mmacystatic inline const char *
2690a9bcb68mmacyus_type2str(unsigned field_type)
2691a9bcb68mmacy{
2692a9bcb68mmacy	switch (field_type) {
2693a9bcb68mmacy	case USTYPE_PSX_USR:
2694a9bcb68mmacy		return ("POSIX User");
2695a9bcb68mmacy	case USTYPE_PSX_GRP:
2696a9bcb68mmacy		return ("POSIX Group");
2697a9bcb68mmacy	case USTYPE_SMB_USR:
2698a9bcb68mmacy		return ("SMB User");
2699a9bcb68mmacy	case USTYPE_SMB_GRP:
2700a9bcb68mmacy		return ("SMB Group");
2701a9bcb68mmacy	case USTYPE_PROJ:
2702a9bcb68mmacy		return ("Project");
2703a9bcb68mmacy	default:
2704a9bcb68mmacy		return ("Undefined");
2705a9bcb68mmacy	}
2706a9bcb68mmacy}
2707a9bcb68mmacy
2708a9bcb68mmacystatic int
2709a9bcb68mmacyuserspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2710a9bcb68mmacy{
2711a9bcb68mmacy	us_cbdata_t *cb = (us_cbdata_t *)arg;
2712a9bcb68mmacy	zfs_userquota_prop_t prop = cb->cb_prop;
2713a9bcb68mmacy	char *name = NULL;
2714a9bcb68mmacy	char *propname;
2715a9bcb68mmacy	char sizebuf[32];
2716a9bcb68mmacy	us_node_t *node;
2717a9bcb68mmacy	uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2718a9bcb68mmacy	uu_avl_t *avl = cb->cb_avl;
2719a9bcb68mmacy	uu_avl_index_t idx;
2720a9bcb68mmacy	nvlist_t *props;
2721a9bcb68mmacy	us_node_t *n;
2722a9bcb68mmacy	zfs_sort_column_t *sortcol = cb->cb_sortcol;
2723a9bcb68mmacy	unsigned type = 0;
2724a9bcb68mmacy	const char *typestr;
2725a9bcb68mmacy	size_t namelen;
2726a9bcb68mmacy	size_t typelen;
2727a9bcb68mmacy	size_t sizelen;
2728a9bcb68mmacy	int typeidx, nameidx, sizeidx;
2729a9bcb68mmacy	us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2730a9bcb68mmacy	boolean_t smbentity = B_FALSE;
2731a9bcb68mmacy
2732a9bcb68mmacy	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2733a9bcb68mmacy		nomem();
2734a9bcb68mmacy	node = safe_malloc(sizeof (us_node_t));
2735a9bcb68mmacy	uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2736a9bcb68mmacy	node->usn_nvl = props;
2737a9bcb68mmacy
2738a9bcb68mmacy	if (domain != NULL && domain[0] != '\0') {
2739a9bcb68mmacy#ifdef HAVE_IDMAP
2740a9bcb68mmacy		/* SMB */
2741a9bcb68mmacy		char sid[MAXNAMELEN + 32];
2742a9bcb68mmacy		uid_t id;
2743a9bcb68mmacy		uint64_t classes;
2744a9bcb68mmacy		int err;
2745a9bcb68mmacy		directory_error_t e;
2746a9bcb68mmacy
2747a9bcb68mmacy		smbentity = B_TRUE;
2748a9bcb68mmacy
2749a9bcb68mmacy		(void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2750a9bcb68mmacy
2751a9bcb68mmacy		if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2752a9bcb68mmacy			type = USTYPE_SMB_GRP;
2753a9bcb68mmacy			err = sid_to_id(sid, B_FALSE, &id);
2754a9bcb68mmacy		} else {
2755a9bcb68mmacy			type = USTYPE_SMB_USR;
2756a9bcb68mmacy			err = sid_to_id(sid, B_TRUE, &id);
2757a9bcb68mmacy		}
2758a9bcb68mmacy
2759a9bcb68mmacy		if (err == 0) {
2760a9bcb68mmacy			rid = id;
2761a9bcb68mmacy			if (!cb->cb_sid2posix) {
2762a9bcb68mmacy				e = directory_name_from_sid(NULL, sid, &name,
2763a9bcb68mmacy				    &classes);
2764a9bcb68mmacy				if (e != NULL)
2765a9bcb68mmacy					directory_error_free(e);
2766a9bcb68mmacy				if (name == NULL)
2767a9bcb68mmacy					name = sid;
2768a9bcb68mmacy			}
2769a9bcb68mmacy		}
2770a9bcb68mmacy#else
2771a9bcb68mmacy		nvlist_free(props);
2772a9bcb68mmacy		free(node);
2773a9bcb68mmacy
2774a9bcb68mmacy		return (-1);
2775a9bcb68mmacy#endif /* HAVE_IDMAP */
2776a9bcb68mmacy	}
2777a9bcb68mmacy
2778a9bcb68mmacy	if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2779a9bcb68mmacy		/* POSIX or -i */
2780a9bcb68mmacy		if (zfs_prop_is_group(prop)) {
2781a9bcb68mmacy			type = USTYPE_PSX_GRP;
2782a9bcb68mmacy			if (!cb->cb_numname) {
2783a9bcb68mmacy				struct group *g;
2784a9bcb68mmacy
2785a9bcb68mmacy				if ((g = getgrgid(rid)) != NULL)
2786a9bcb68mmacy					name = g->gr_name;
2787a9bcb68mmacy			}
2788a9bcb68mmacy		} else if (zfs_prop_is_user(prop)) {
2789a9bcb68mmacy			type = USTYPE_PSX_USR;
2790a9bcb68mmacy			if (!cb->cb_numname) {
2791a9bcb68mmacy				struct passwd *p;
2792a9bcb68mmacy
2793a9bcb68mmacy				if ((p = getpwuid(rid)) != NULL)
2794a9bcb68mmacy					name = p->pw_name;
2795a9bcb68mmacy			}
2796a9bcb68mmacy		} else {
2797a9bcb68mmacy			type = USTYPE_PROJ;
2798a9bcb68mmacy		}
2799a9bcb68mmacy	}
2800a9bcb68mmacy
2801a9bcb68mmacy	/*
2802a9bcb68mmacy	 * Make sure that the type/name combination is unique when doing
2803a9bcb68mmacy	 * SID to POSIX ID translation (hence changing the type from SMB to
2804a9bcb68mmacy	 * POSIX).
2805a9bcb68mmacy	 */
2806a9bcb68mmacy	if (cb->cb_sid2posix &&
2807a9bcb68mmacy	    nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2808a9bcb68mmacy		nomem();
2809a9bcb68mmacy
2810a9bcb68mmacy	/* Calculate/update width of TYPE field */
2811a9bcb68mmacy	typestr = us_type2str(type);
2812a9bcb68mmacy	typelen = strlen(gettext(typestr));
2813a9bcb68mmacy	typeidx = us_field_index("type");
2814a9bcb68mmacy	if (typelen > cb->cb_width[typeidx])
2815a9bcb68mmacy		cb->cb_width[typeidx] = typelen;
2816a9bcb68mmacy	if (nvlist_add_uint32(props, "type", type) != 0)
2817a9bcb68mmacy		nomem();
2818a9bcb68mmacy
2819a9bcb68mmacy	/* Calculate/update width of NAME field */
2820a9bcb68mmacy	if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2821a9bcb68mmacy		if (nvlist_add_uint64(props, "name", rid) != 0)
2822a9bcb68mmacy			nomem();
2823a9bcb68mmacy		namelen = snprintf(NULL, 0, "%u", rid);
2824a9bcb68mmacy	} else {
2825a9bcb68mmacy		if (nvlist_add_string(props, "name", name) != 0)
2826a9bcb68mmacy			nomem();
2827a9bcb68mmacy		namelen = strlen(name);
2828a9bcb68mmacy	}
2829a9bcb68mmacy	nameidx = us_field_index("name");
2830a9bcb68mmacy	if (nameidx >= 0 && namelen > cb->cb_width[nameidx])
2831