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