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