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