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