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