]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | ||
22 | /* | |
428870ff | 23 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
34dc7c2f BB |
24 | */ |
25 | ||
34dc7c2f BB |
26 | #include <assert.h> |
27 | #include <ctype.h> | |
28 | #include <dirent.h> | |
29 | #include <errno.h> | |
30 | #include <fcntl.h> | |
31 | #include <libgen.h> | |
32 | #include <libintl.h> | |
33 | #include <libuutil.h> | |
34 | #include <locale.h> | |
35 | #include <stdio.h> | |
36 | #include <stdlib.h> | |
37 | #include <string.h> | |
38 | #include <strings.h> | |
39 | #include <unistd.h> | |
40 | #include <priv.h> | |
41 | #include <pwd.h> | |
42 | #include <zone.h> | |
43 | #include <sys/fs/zfs.h> | |
34dc7c2f | 44 | #include <sys/stat.h> |
26685276 BB |
45 | #include <sys/fm/util.h> |
46 | #include <sys/fm/protocol.h> | |
34dc7c2f BB |
47 | |
48 | #include <libzfs.h> | |
49 | ||
50 | #include "zpool_util.h" | |
51 | #include "zfs_comutil.h" | |
52 | ||
428870ff BB |
53 | #include "statcommon.h" |
54 | ||
34dc7c2f BB |
55 | static int zpool_do_create(int, char **); |
56 | static int zpool_do_destroy(int, char **); | |
57 | ||
58 | static int zpool_do_add(int, char **); | |
59 | static int zpool_do_remove(int, char **); | |
60 | ||
61 | static int zpool_do_list(int, char **); | |
62 | static int zpool_do_iostat(int, char **); | |
63 | static int zpool_do_status(int, char **); | |
64 | ||
65 | static int zpool_do_online(int, char **); | |
66 | static int zpool_do_offline(int, char **); | |
67 | static int zpool_do_clear(int, char **); | |
68 | ||
69 | static int zpool_do_attach(int, char **); | |
70 | static int zpool_do_detach(int, char **); | |
71 | static int zpool_do_replace(int, char **); | |
428870ff | 72 | static int zpool_do_split(int, char **); |
34dc7c2f BB |
73 | |
74 | static int zpool_do_scrub(int, char **); | |
75 | ||
76 | static int zpool_do_import(int, char **); | |
77 | static int zpool_do_export(int, char **); | |
78 | ||
79 | static int zpool_do_upgrade(int, char **); | |
80 | ||
81 | static int zpool_do_history(int, char **); | |
26685276 | 82 | static int zpool_do_events(int, char **); |
34dc7c2f BB |
83 | |
84 | static int zpool_do_get(int, char **); | |
85 | static int zpool_do_set(int, char **); | |
86 | ||
87 | /* | |
88 | * These libumem hooks provide a reasonable set of defaults for the allocator's | |
89 | * debugging facilities. | |
90 | */ | |
b128c09f BB |
91 | |
92 | #ifdef DEBUG | |
34dc7c2f BB |
93 | const char * |
94 | _umem_debug_init(void) | |
95 | { | |
96 | return ("default,verbose"); /* $UMEM_DEBUG setting */ | |
97 | } | |
98 | ||
99 | const char * | |
100 | _umem_logging_init(void) | |
101 | { | |
102 | return ("fail,contents"); /* $UMEM_LOGGING setting */ | |
103 | } | |
b128c09f | 104 | #endif |
34dc7c2f BB |
105 | |
106 | typedef enum { | |
107 | HELP_ADD, | |
108 | HELP_ATTACH, | |
109 | HELP_CLEAR, | |
110 | HELP_CREATE, | |
111 | HELP_DESTROY, | |
112 | HELP_DETACH, | |
113 | HELP_EXPORT, | |
114 | HELP_HISTORY, | |
115 | HELP_IMPORT, | |
116 | HELP_IOSTAT, | |
117 | HELP_LIST, | |
118 | HELP_OFFLINE, | |
119 | HELP_ONLINE, | |
120 | HELP_REPLACE, | |
121 | HELP_REMOVE, | |
122 | HELP_SCRUB, | |
123 | HELP_STATUS, | |
124 | HELP_UPGRADE, | |
26685276 | 125 | HELP_EVENTS, |
34dc7c2f | 126 | HELP_GET, |
428870ff BB |
127 | HELP_SET, |
128 | HELP_SPLIT | |
34dc7c2f BB |
129 | } zpool_help_t; |
130 | ||
131 | ||
132 | typedef struct zpool_command { | |
133 | const char *name; | |
134 | int (*func)(int, char **); | |
135 | zpool_help_t usage; | |
136 | } zpool_command_t; | |
137 | ||
138 | /* | |
139 | * Master command table. Each ZFS command has a name, associated function, and | |
140 | * usage message. The usage messages need to be internationalized, so we have | |
141 | * to have a function to return the usage message based on a command index. | |
142 | * | |
143 | * These commands are organized according to how they are displayed in the usage | |
144 | * message. An empty command (one with a NULL name) indicates an empty line in | |
145 | * the generic usage message. | |
146 | */ | |
147 | static zpool_command_t command_table[] = { | |
148 | { "create", zpool_do_create, HELP_CREATE }, | |
149 | { "destroy", zpool_do_destroy, HELP_DESTROY }, | |
150 | { NULL }, | |
151 | { "add", zpool_do_add, HELP_ADD }, | |
152 | { "remove", zpool_do_remove, HELP_REMOVE }, | |
153 | { NULL }, | |
154 | { "list", zpool_do_list, HELP_LIST }, | |
155 | { "iostat", zpool_do_iostat, HELP_IOSTAT }, | |
156 | { "status", zpool_do_status, HELP_STATUS }, | |
157 | { NULL }, | |
158 | { "online", zpool_do_online, HELP_ONLINE }, | |
159 | { "offline", zpool_do_offline, HELP_OFFLINE }, | |
160 | { "clear", zpool_do_clear, HELP_CLEAR }, | |
161 | { NULL }, | |
162 | { "attach", zpool_do_attach, HELP_ATTACH }, | |
163 | { "detach", zpool_do_detach, HELP_DETACH }, | |
164 | { "replace", zpool_do_replace, HELP_REPLACE }, | |
428870ff | 165 | { "split", zpool_do_split, HELP_SPLIT }, |
34dc7c2f BB |
166 | { NULL }, |
167 | { "scrub", zpool_do_scrub, HELP_SCRUB }, | |
168 | { NULL }, | |
169 | { "import", zpool_do_import, HELP_IMPORT }, | |
170 | { "export", zpool_do_export, HELP_EXPORT }, | |
171 | { "upgrade", zpool_do_upgrade, HELP_UPGRADE }, | |
172 | { NULL }, | |
173 | { "history", zpool_do_history, HELP_HISTORY }, | |
26685276 BB |
174 | { "events", zpool_do_events, HELP_EVENTS }, |
175 | { NULL }, | |
34dc7c2f BB |
176 | { "get", zpool_do_get, HELP_GET }, |
177 | { "set", zpool_do_set, HELP_SET }, | |
178 | }; | |
179 | ||
180 | #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0])) | |
181 | ||
182 | zpool_command_t *current_command; | |
183 | static char history_str[HIS_MAX_RECORD_LEN]; | |
184 | ||
428870ff BB |
185 | static uint_t timestamp_fmt = NODATE; |
186 | ||
34dc7c2f BB |
187 | static const char * |
188 | get_usage(zpool_help_t idx) { | |
189 | switch (idx) { | |
190 | case HELP_ADD: | |
191 | return (gettext("\tadd [-fn] <pool> <vdev> ...\n")); | |
192 | case HELP_ATTACH: | |
193 | return (gettext("\tattach [-f] <pool> <device> " | |
194 | "<new-device>\n")); | |
195 | case HELP_CLEAR: | |
428870ff | 196 | return (gettext("\tclear [-nF] <pool> [device]\n")); |
34dc7c2f BB |
197 | case HELP_CREATE: |
198 | return (gettext("\tcreate [-fn] [-o property=value] ... \n" | |
b128c09f | 199 | "\t [-O file-system-property=value] ... \n" |
34dc7c2f BB |
200 | "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n")); |
201 | case HELP_DESTROY: | |
202 | return (gettext("\tdestroy [-f] <pool>\n")); | |
203 | case HELP_DETACH: | |
204 | return (gettext("\tdetach <pool> <device>\n")); | |
205 | case HELP_EXPORT: | |
206 | return (gettext("\texport [-f] <pool> ...\n")); | |
207 | case HELP_HISTORY: | |
208 | return (gettext("\thistory [-il] [<pool>] ...\n")); | |
209 | case HELP_IMPORT: | |
210 | return (gettext("\timport [-d dir] [-D]\n" | |
572e2857 | 211 | "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n" |
34dc7c2f | 212 | "\timport [-o mntopts] [-o property=value] ... \n" |
572e2857 BB |
213 | "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] " |
214 | "[-R root] [-F [-n]] -a\n" | |
34dc7c2f | 215 | "\timport [-o mntopts] [-o property=value] ... \n" |
572e2857 BB |
216 | "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] " |
217 | "[-R root] [-F [-n]]\n" | |
218 | "\t <pool | id> [newpool]\n")); | |
34dc7c2f | 219 | case HELP_IOSTAT: |
428870ff | 220 | return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval " |
34dc7c2f BB |
221 | "[count]]\n")); |
222 | case HELP_LIST: | |
223 | return (gettext("\tlist [-H] [-o property[,...]] " | |
428870ff | 224 | "[-T d|u] [pool] ... [interval [count]]\n")); |
34dc7c2f BB |
225 | case HELP_OFFLINE: |
226 | return (gettext("\toffline [-t] <pool> <device> ...\n")); | |
227 | case HELP_ONLINE: | |
228 | return (gettext("\tonline <pool> <device> ...\n")); | |
229 | case HELP_REPLACE: | |
230 | return (gettext("\treplace [-f] <pool> <device> " | |
231 | "[new-device]\n")); | |
232 | case HELP_REMOVE: | |
233 | return (gettext("\tremove <pool> <device> ...\n")); | |
234 | case HELP_SCRUB: | |
235 | return (gettext("\tscrub [-s] <pool> ...\n")); | |
236 | case HELP_STATUS: | |
428870ff BB |
237 | return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval " |
238 | "[count]]\n")); | |
34dc7c2f BB |
239 | case HELP_UPGRADE: |
240 | return (gettext("\tupgrade\n" | |
241 | "\tupgrade -v\n" | |
242 | "\tupgrade [-V version] <-a | pool ...>\n")); | |
26685276 | 243 | case HELP_EVENTS: |
c5343ba7 | 244 | return (gettext("\tevents [-vHfc]\n")); |
34dc7c2f BB |
245 | case HELP_GET: |
246 | return (gettext("\tget <\"all\" | property[,...]> " | |
247 | "<pool> ...\n")); | |
248 | case HELP_SET: | |
249 | return (gettext("\tset <property=value> <pool> \n")); | |
428870ff BB |
250 | case HELP_SPLIT: |
251 | return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n" | |
252 | "\t [-o property=value] <pool> <newpool> " | |
253 | "[<device> ...]\n")); | |
34dc7c2f BB |
254 | } |
255 | ||
256 | abort(); | |
257 | /* NOTREACHED */ | |
258 | } | |
259 | ||
260 | ||
261 | /* | |
262 | * Callback routine that will print out a pool property value. | |
263 | */ | |
264 | static int | |
265 | print_prop_cb(int prop, void *cb) | |
266 | { | |
267 | FILE *fp = cb; | |
268 | ||
428870ff | 269 | (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop)); |
34dc7c2f BB |
270 | |
271 | if (zpool_prop_readonly(prop)) | |
272 | (void) fprintf(fp, " NO "); | |
273 | else | |
428870ff | 274 | (void) fprintf(fp, " YES "); |
34dc7c2f BB |
275 | |
276 | if (zpool_prop_values(prop) == NULL) | |
277 | (void) fprintf(fp, "-\n"); | |
278 | else | |
279 | (void) fprintf(fp, "%s\n", zpool_prop_values(prop)); | |
280 | ||
281 | return (ZPROP_CONT); | |
282 | } | |
283 | ||
284 | /* | |
285 | * Display usage message. If we're inside a command, display only the usage for | |
286 | * that command. Otherwise, iterate over the entire command table and display | |
287 | * a complete usage message. | |
288 | */ | |
289 | void | |
290 | usage(boolean_t requested) | |
291 | { | |
292 | FILE *fp = requested ? stdout : stderr; | |
293 | ||
294 | if (current_command == NULL) { | |
295 | int i; | |
296 | ||
297 | (void) fprintf(fp, gettext("usage: zpool command args ...\n")); | |
298 | (void) fprintf(fp, | |
299 | gettext("where 'command' is one of the following:\n\n")); | |
300 | ||
301 | for (i = 0; i < NCOMMAND; i++) { | |
302 | if (command_table[i].name == NULL) | |
303 | (void) fprintf(fp, "\n"); | |
304 | else | |
305 | (void) fprintf(fp, "%s", | |
306 | get_usage(command_table[i].usage)); | |
307 | } | |
308 | } else { | |
309 | (void) fprintf(fp, gettext("usage:\n")); | |
310 | (void) fprintf(fp, "%s", get_usage(current_command->usage)); | |
311 | } | |
312 | ||
313 | if (current_command != NULL && | |
314 | ((strcmp(current_command->name, "set") == 0) || | |
315 | (strcmp(current_command->name, "get") == 0) || | |
316 | (strcmp(current_command->name, "list") == 0))) { | |
317 | ||
318 | (void) fprintf(fp, | |
319 | gettext("\nthe following properties are supported:\n")); | |
320 | ||
428870ff | 321 | (void) fprintf(fp, "\n\t%-15s %s %s\n\n", |
34dc7c2f BB |
322 | "PROPERTY", "EDIT", "VALUES"); |
323 | ||
324 | /* Iterate over all properties */ | |
325 | (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE, | |
326 | ZFS_TYPE_POOL); | |
327 | } | |
328 | ||
329 | /* | |
330 | * See comments at end of main(). | |
331 | */ | |
332 | if (getenv("ZFS_ABORT") != NULL) { | |
333 | (void) printf("dumping core by request\n"); | |
334 | abort(); | |
335 | } | |
336 | ||
337 | exit(requested ? 0 : 2); | |
338 | } | |
339 | ||
340 | void | |
341 | print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent, | |
342 | boolean_t print_logs) | |
343 | { | |
344 | nvlist_t **child; | |
345 | uint_t c, children; | |
346 | char *vname; | |
347 | ||
348 | if (name != NULL) | |
349 | (void) printf("\t%*s%s\n", indent, "", name); | |
350 | ||
351 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
352 | &child, &children) != 0) | |
353 | return; | |
354 | ||
355 | for (c = 0; c < children; c++) { | |
356 | uint64_t is_log = B_FALSE; | |
357 | ||
358 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, | |
359 | &is_log); | |
360 | if ((is_log && !print_logs) || (!is_log && print_logs)) | |
361 | continue; | |
362 | ||
428870ff | 363 | vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE); |
34dc7c2f BB |
364 | print_vdev_tree(zhp, vname, child[c], indent + 2, |
365 | B_FALSE); | |
366 | free(vname); | |
367 | } | |
368 | } | |
369 | ||
370 | /* | |
371 | * Add a property pair (name, string-value) into a property nvlist. | |
372 | */ | |
373 | static int | |
b128c09f BB |
374 | add_prop_list(const char *propname, char *propval, nvlist_t **props, |
375 | boolean_t poolprop) | |
34dc7c2f | 376 | { |
b128c09f BB |
377 | zpool_prop_t prop = ZPROP_INVAL; |
378 | zfs_prop_t fprop; | |
34dc7c2f | 379 | nvlist_t *proplist; |
b128c09f BB |
380 | const char *normnm; |
381 | char *strval; | |
34dc7c2f BB |
382 | |
383 | if (*props == NULL && | |
384 | nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) { | |
385 | (void) fprintf(stderr, | |
386 | gettext("internal error: out of memory\n")); | |
387 | return (1); | |
388 | } | |
389 | ||
390 | proplist = *props; | |
391 | ||
b128c09f BB |
392 | if (poolprop) { |
393 | if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) { | |
394 | (void) fprintf(stderr, gettext("property '%s' is " | |
395 | "not a valid pool property\n"), propname); | |
396 | return (2); | |
397 | } | |
398 | normnm = zpool_prop_to_name(prop); | |
399 | } else { | |
9babb374 BB |
400 | if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) { |
401 | normnm = zfs_prop_to_name(fprop); | |
402 | } else { | |
403 | normnm = propname; | |
b128c09f | 404 | } |
34dc7c2f BB |
405 | } |
406 | ||
b128c09f BB |
407 | if (nvlist_lookup_string(proplist, normnm, &strval) == 0 && |
408 | prop != ZPOOL_PROP_CACHEFILE) { | |
34dc7c2f BB |
409 | (void) fprintf(stderr, gettext("property '%s' " |
410 | "specified multiple times\n"), propname); | |
411 | return (2); | |
412 | } | |
413 | ||
b128c09f | 414 | if (nvlist_add_string(proplist, normnm, propval) != 0) { |
34dc7c2f BB |
415 | (void) fprintf(stderr, gettext("internal " |
416 | "error: out of memory\n")); | |
417 | return (1); | |
418 | } | |
419 | ||
420 | return (0); | |
421 | } | |
422 | ||
423 | /* | |
424 | * zpool add [-fn] <pool> <vdev> ... | |
425 | * | |
426 | * -f Force addition of devices, even if they appear in use | |
427 | * -n Do not add the devices, but display the resulting layout if | |
428 | * they were to be added. | |
429 | * | |
430 | * Adds the given vdevs to 'pool'. As with create, the bulk of this work is | |
431 | * handled by get_vdev_spec(), which constructs the nvlist needed to pass to | |
432 | * libzfs. | |
433 | */ | |
434 | int | |
435 | zpool_do_add(int argc, char **argv) | |
436 | { | |
437 | boolean_t force = B_FALSE; | |
438 | boolean_t dryrun = B_FALSE; | |
439 | int c; | |
440 | nvlist_t *nvroot; | |
441 | char *poolname; | |
442 | int ret; | |
443 | zpool_handle_t *zhp; | |
444 | nvlist_t *config; | |
445 | ||
446 | /* check options */ | |
447 | while ((c = getopt(argc, argv, "fn")) != -1) { | |
448 | switch (c) { | |
449 | case 'f': | |
450 | force = B_TRUE; | |
451 | break; | |
452 | case 'n': | |
453 | dryrun = B_TRUE; | |
454 | break; | |
455 | case '?': | |
456 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
457 | optopt); | |
458 | usage(B_FALSE); | |
459 | } | |
460 | } | |
461 | ||
462 | argc -= optind; | |
463 | argv += optind; | |
464 | ||
465 | /* get pool name and check number of arguments */ | |
466 | if (argc < 1) { | |
467 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
468 | usage(B_FALSE); | |
469 | } | |
470 | if (argc < 2) { | |
471 | (void) fprintf(stderr, gettext("missing vdev specification\n")); | |
472 | usage(B_FALSE); | |
473 | } | |
474 | ||
475 | poolname = argv[0]; | |
476 | ||
477 | argc--; | |
478 | argv++; | |
479 | ||
480 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
481 | return (1); | |
482 | ||
483 | if ((config = zpool_get_config(zhp, NULL)) == NULL) { | |
484 | (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), | |
485 | poolname); | |
486 | zpool_close(zhp); | |
487 | return (1); | |
488 | } | |
489 | ||
490 | /* pass off to get_vdev_spec for processing */ | |
df30f566 | 491 | nvroot = make_root_vdev(zhp, NULL, force, !force, B_FALSE, dryrun, |
b128c09f | 492 | argc, argv); |
34dc7c2f BB |
493 | if (nvroot == NULL) { |
494 | zpool_close(zhp); | |
495 | return (1); | |
496 | } | |
497 | ||
498 | if (dryrun) { | |
499 | nvlist_t *poolnvroot; | |
500 | ||
501 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
502 | &poolnvroot) == 0); | |
503 | ||
504 | (void) printf(gettext("would update '%s' to the following " | |
505 | "configuration:\n"), zpool_get_name(zhp)); | |
506 | ||
507 | /* print original main pool and new tree */ | |
508 | print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE); | |
509 | print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE); | |
510 | ||
511 | /* Do the same for the logs */ | |
512 | if (num_logs(poolnvroot) > 0) { | |
513 | print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE); | |
514 | print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE); | |
515 | } else if (num_logs(nvroot) > 0) { | |
516 | print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE); | |
517 | } | |
518 | ||
519 | ret = 0; | |
520 | } else { | |
521 | ret = (zpool_add(zhp, nvroot) != 0); | |
522 | } | |
523 | ||
524 | nvlist_free(nvroot); | |
525 | zpool_close(zhp); | |
526 | ||
527 | return (ret); | |
528 | } | |
529 | ||
530 | /* | |
428870ff | 531 | * zpool remove <pool> <vdev> ... |
34dc7c2f | 532 | * |
428870ff BB |
533 | * Removes the given vdev from the pool. Currently, this supports removing |
534 | * spares, cache, and log devices from the pool. | |
34dc7c2f BB |
535 | */ |
536 | int | |
537 | zpool_do_remove(int argc, char **argv) | |
538 | { | |
539 | char *poolname; | |
540 | int i, ret = 0; | |
541 | zpool_handle_t *zhp; | |
542 | ||
543 | argc--; | |
544 | argv++; | |
545 | ||
546 | /* get pool name and check number of arguments */ | |
547 | if (argc < 1) { | |
548 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
549 | usage(B_FALSE); | |
550 | } | |
551 | if (argc < 2) { | |
552 | (void) fprintf(stderr, gettext("missing device\n")); | |
553 | usage(B_FALSE); | |
554 | } | |
555 | ||
556 | poolname = argv[0]; | |
557 | ||
558 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
559 | return (1); | |
560 | ||
561 | for (i = 1; i < argc; i++) { | |
562 | if (zpool_vdev_remove(zhp, argv[i]) != 0) | |
563 | ret = 1; | |
564 | } | |
565 | ||
566 | return (ret); | |
567 | } | |
568 | ||
569 | /* | |
b128c09f BB |
570 | * zpool create [-fn] [-o property=value] ... |
571 | * [-O file-system-property=value] ... | |
572 | * [-R root] [-m mountpoint] <pool> <dev> ... | |
34dc7c2f BB |
573 | * |
574 | * -f Force creation, even if devices appear in use | |
575 | * -n Do not create the pool, but display the resulting layout if it | |
576 | * were to be created. | |
577 | * -R Create a pool under an alternate root | |
578 | * -m Set default mountpoint for the root dataset. By default it's | |
579 | * '/<pool>' | |
580 | * -o Set property=value. | |
b128c09f | 581 | * -O Set fsproperty=value in the pool's root file system |
34dc7c2f BB |
582 | * |
583 | * Creates the named pool according to the given vdev specification. The | |
584 | * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once | |
585 | * we get the nvlist back from get_vdev_spec(), we either print out the contents | |
586 | * (if '-n' was specified), or pass it to libzfs to do the creation. | |
587 | */ | |
588 | int | |
589 | zpool_do_create(int argc, char **argv) | |
590 | { | |
591 | boolean_t force = B_FALSE; | |
592 | boolean_t dryrun = B_FALSE; | |
593 | int c; | |
594 | nvlist_t *nvroot = NULL; | |
595 | char *poolname; | |
596 | int ret = 1; | |
597 | char *altroot = NULL; | |
598 | char *mountpoint = NULL; | |
b128c09f | 599 | nvlist_t *fsprops = NULL; |
34dc7c2f BB |
600 | nvlist_t *props = NULL; |
601 | char *propval; | |
602 | ||
603 | /* check options */ | |
b128c09f | 604 | while ((c = getopt(argc, argv, ":fnR:m:o:O:")) != -1) { |
34dc7c2f BB |
605 | switch (c) { |
606 | case 'f': | |
607 | force = B_TRUE; | |
608 | break; | |
609 | case 'n': | |
610 | dryrun = B_TRUE; | |
611 | break; | |
612 | case 'R': | |
613 | altroot = optarg; | |
614 | if (add_prop_list(zpool_prop_to_name( | |
b128c09f | 615 | ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) |
34dc7c2f BB |
616 | goto errout; |
617 | if (nvlist_lookup_string(props, | |
618 | zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), | |
619 | &propval) == 0) | |
620 | break; | |
621 | if (add_prop_list(zpool_prop_to_name( | |
b128c09f | 622 | ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) |
34dc7c2f BB |
623 | goto errout; |
624 | break; | |
625 | case 'm': | |
626 | mountpoint = optarg; | |
627 | break; | |
628 | case 'o': | |
629 | if ((propval = strchr(optarg, '=')) == NULL) { | |
630 | (void) fprintf(stderr, gettext("missing " | |
631 | "'=' for -o option\n")); | |
632 | goto errout; | |
633 | } | |
634 | *propval = '\0'; | |
635 | propval++; | |
636 | ||
b128c09f BB |
637 | if (add_prop_list(optarg, propval, &props, B_TRUE)) |
638 | goto errout; | |
639 | break; | |
640 | case 'O': | |
641 | if ((propval = strchr(optarg, '=')) == NULL) { | |
642 | (void) fprintf(stderr, gettext("missing " | |
643 | "'=' for -O option\n")); | |
644 | goto errout; | |
645 | } | |
646 | *propval = '\0'; | |
647 | propval++; | |
648 | ||
649 | if (add_prop_list(optarg, propval, &fsprops, B_FALSE)) | |
34dc7c2f BB |
650 | goto errout; |
651 | break; | |
652 | case ':': | |
653 | (void) fprintf(stderr, gettext("missing argument for " | |
654 | "'%c' option\n"), optopt); | |
655 | goto badusage; | |
656 | case '?': | |
657 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
658 | optopt); | |
659 | goto badusage; | |
660 | } | |
661 | } | |
662 | ||
663 | argc -= optind; | |
664 | argv += optind; | |
665 | ||
666 | /* get pool name and check number of arguments */ | |
667 | if (argc < 1) { | |
668 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
669 | goto badusage; | |
670 | } | |
671 | if (argc < 2) { | |
672 | (void) fprintf(stderr, gettext("missing vdev specification\n")); | |
673 | goto badusage; | |
674 | } | |
675 | ||
676 | poolname = argv[0]; | |
677 | ||
678 | /* | |
679 | * As a special case, check for use of '/' in the name, and direct the | |
680 | * user to use 'zfs create' instead. | |
681 | */ | |
682 | if (strchr(poolname, '/') != NULL) { | |
683 | (void) fprintf(stderr, gettext("cannot create '%s': invalid " | |
684 | "character '/' in pool name\n"), poolname); | |
685 | (void) fprintf(stderr, gettext("use 'zfs create' to " | |
686 | "create a dataset\n")); | |
687 | goto errout; | |
688 | } | |
689 | ||
690 | /* pass off to get_vdev_spec for bulk processing */ | |
df30f566 | 691 | nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun, |
b128c09f | 692 | argc - 1, argv + 1); |
34dc7c2f | 693 | if (nvroot == NULL) |
b128c09f | 694 | goto errout; |
34dc7c2f BB |
695 | |
696 | /* make_root_vdev() allows 0 toplevel children if there are spares */ | |
697 | if (!zfs_allocatable_devs(nvroot)) { | |
698 | (void) fprintf(stderr, gettext("invalid vdev " | |
699 | "specification: at least one toplevel vdev must be " | |
700 | "specified\n")); | |
701 | goto errout; | |
702 | } | |
703 | ||
704 | ||
705 | if (altroot != NULL && altroot[0] != '/') { | |
706 | (void) fprintf(stderr, gettext("invalid alternate root '%s': " | |
707 | "must be an absolute path\n"), altroot); | |
708 | goto errout; | |
709 | } | |
710 | ||
711 | /* | |
712 | * Check the validity of the mountpoint and direct the user to use the | |
713 | * '-m' mountpoint option if it looks like its in use. | |
714 | */ | |
715 | if (mountpoint == NULL || | |
716 | (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 && | |
717 | strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) { | |
718 | char buf[MAXPATHLEN]; | |
719 | DIR *dirp; | |
720 | ||
721 | if (mountpoint && mountpoint[0] != '/') { | |
722 | (void) fprintf(stderr, gettext("invalid mountpoint " | |
723 | "'%s': must be an absolute path, 'legacy', or " | |
724 | "'none'\n"), mountpoint); | |
725 | goto errout; | |
726 | } | |
727 | ||
728 | if (mountpoint == NULL) { | |
729 | if (altroot != NULL) | |
730 | (void) snprintf(buf, sizeof (buf), "%s/%s", | |
731 | altroot, poolname); | |
732 | else | |
733 | (void) snprintf(buf, sizeof (buf), "/%s", | |
734 | poolname); | |
735 | } else { | |
736 | if (altroot != NULL) | |
737 | (void) snprintf(buf, sizeof (buf), "%s%s", | |
738 | altroot, mountpoint); | |
739 | else | |
740 | (void) snprintf(buf, sizeof (buf), "%s", | |
741 | mountpoint); | |
742 | } | |
743 | ||
744 | if ((dirp = opendir(buf)) == NULL && errno != ENOENT) { | |
745 | (void) fprintf(stderr, gettext("mountpoint '%s' : " | |
746 | "%s\n"), buf, strerror(errno)); | |
747 | (void) fprintf(stderr, gettext("use '-m' " | |
748 | "option to provide a different default\n")); | |
749 | goto errout; | |
750 | } else if (dirp) { | |
751 | int count = 0; | |
752 | ||
753 | while (count < 3 && readdir(dirp) != NULL) | |
754 | count++; | |
755 | (void) closedir(dirp); | |
756 | ||
757 | if (count > 2) { | |
758 | (void) fprintf(stderr, gettext("mountpoint " | |
759 | "'%s' exists and is not empty\n"), buf); | |
760 | (void) fprintf(stderr, gettext("use '-m' " | |
761 | "option to provide a " | |
762 | "different default\n")); | |
763 | goto errout; | |
764 | } | |
765 | } | |
766 | } | |
767 | ||
768 | if (dryrun) { | |
769 | /* | |
770 | * For a dry run invocation, print out a basic message and run | |
771 | * through all the vdevs in the list and print out in an | |
772 | * appropriate hierarchy. | |
773 | */ | |
774 | (void) printf(gettext("would create '%s' with the " | |
775 | "following layout:\n\n"), poolname); | |
776 | ||
777 | print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE); | |
778 | if (num_logs(nvroot) > 0) | |
779 | print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE); | |
780 | ||
781 | ret = 0; | |
782 | } else { | |
783 | /* | |
784 | * Hand off to libzfs. | |
785 | */ | |
b128c09f BB |
786 | if (zpool_create(g_zfs, poolname, |
787 | nvroot, props, fsprops) == 0) { | |
34dc7c2f BB |
788 | zfs_handle_t *pool = zfs_open(g_zfs, poolname, |
789 | ZFS_TYPE_FILESYSTEM); | |
790 | if (pool != NULL) { | |
791 | if (mountpoint != NULL) | |
792 | verify(zfs_prop_set(pool, | |
793 | zfs_prop_to_name( | |
794 | ZFS_PROP_MOUNTPOINT), | |
795 | mountpoint) == 0); | |
796 | if (zfs_mount(pool, NULL, 0) == 0) | |
797 | ret = zfs_shareall(pool); | |
798 | zfs_close(pool); | |
799 | } | |
800 | } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) { | |
801 | (void) fprintf(stderr, gettext("pool name may have " | |
802 | "been omitted\n")); | |
803 | } | |
804 | } | |
805 | ||
806 | errout: | |
807 | nvlist_free(nvroot); | |
b128c09f | 808 | nvlist_free(fsprops); |
34dc7c2f BB |
809 | nvlist_free(props); |
810 | return (ret); | |
811 | badusage: | |
b128c09f | 812 | nvlist_free(fsprops); |
34dc7c2f BB |
813 | nvlist_free(props); |
814 | usage(B_FALSE); | |
815 | return (2); | |
816 | } | |
817 | ||
818 | /* | |
819 | * zpool destroy <pool> | |
820 | * | |
821 | * -f Forcefully unmount any datasets | |
822 | * | |
823 | * Destroy the given pool. Automatically unmounts any datasets in the pool. | |
824 | */ | |
825 | int | |
826 | zpool_do_destroy(int argc, char **argv) | |
827 | { | |
828 | boolean_t force = B_FALSE; | |
829 | int c; | |
830 | char *pool; | |
831 | zpool_handle_t *zhp; | |
832 | int ret; | |
833 | ||
834 | /* check options */ | |
835 | while ((c = getopt(argc, argv, "f")) != -1) { | |
836 | switch (c) { | |
837 | case 'f': | |
838 | force = B_TRUE; | |
839 | break; | |
840 | case '?': | |
841 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
842 | optopt); | |
843 | usage(B_FALSE); | |
844 | } | |
845 | } | |
846 | ||
847 | argc -= optind; | |
848 | argv += optind; | |
849 | ||
850 | /* check arguments */ | |
851 | if (argc < 1) { | |
852 | (void) fprintf(stderr, gettext("missing pool argument\n")); | |
853 | usage(B_FALSE); | |
854 | } | |
855 | if (argc > 1) { | |
856 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
857 | usage(B_FALSE); | |
858 | } | |
859 | ||
860 | pool = argv[0]; | |
861 | ||
862 | if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { | |
863 | /* | |
864 | * As a special case, check for use of '/' in the name, and | |
865 | * direct the user to use 'zfs destroy' instead. | |
866 | */ | |
867 | if (strchr(pool, '/') != NULL) | |
868 | (void) fprintf(stderr, gettext("use 'zfs destroy' to " | |
869 | "destroy a dataset\n")); | |
870 | return (1); | |
871 | } | |
872 | ||
873 | if (zpool_disable_datasets(zhp, force) != 0) { | |
874 | (void) fprintf(stderr, gettext("could not destroy '%s': " | |
875 | "could not unmount datasets\n"), zpool_get_name(zhp)); | |
876 | return (1); | |
877 | } | |
878 | ||
879 | ret = (zpool_destroy(zhp) != 0); | |
880 | ||
881 | zpool_close(zhp); | |
882 | ||
883 | return (ret); | |
884 | } | |
885 | ||
886 | /* | |
887 | * zpool export [-f] <pool> ... | |
888 | * | |
889 | * -f Forcefully unmount datasets | |
890 | * | |
891 | * Export the given pools. By default, the command will attempt to cleanly | |
892 | * unmount any active datasets within the pool. If the '-f' flag is specified, | |
893 | * then the datasets will be forcefully unmounted. | |
894 | */ | |
895 | int | |
896 | zpool_do_export(int argc, char **argv) | |
897 | { | |
898 | boolean_t force = B_FALSE; | |
fb5f0bc8 | 899 | boolean_t hardforce = B_FALSE; |
34dc7c2f BB |
900 | int c; |
901 | zpool_handle_t *zhp; | |
902 | int ret; | |
903 | int i; | |
904 | ||
905 | /* check options */ | |
fb5f0bc8 | 906 | while ((c = getopt(argc, argv, "fF")) != -1) { |
34dc7c2f BB |
907 | switch (c) { |
908 | case 'f': | |
909 | force = B_TRUE; | |
910 | break; | |
fb5f0bc8 BB |
911 | case 'F': |
912 | hardforce = B_TRUE; | |
913 | break; | |
34dc7c2f BB |
914 | case '?': |
915 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
916 | optopt); | |
917 | usage(B_FALSE); | |
918 | } | |
919 | } | |
920 | ||
921 | argc -= optind; | |
922 | argv += optind; | |
923 | ||
924 | /* check arguments */ | |
925 | if (argc < 1) { | |
926 | (void) fprintf(stderr, gettext("missing pool argument\n")); | |
927 | usage(B_FALSE); | |
928 | } | |
929 | ||
930 | ret = 0; | |
931 | for (i = 0; i < argc; i++) { | |
932 | if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) { | |
933 | ret = 1; | |
934 | continue; | |
935 | } | |
936 | ||
937 | if (zpool_disable_datasets(zhp, force) != 0) { | |
938 | ret = 1; | |
939 | zpool_close(zhp); | |
940 | continue; | |
941 | } | |
942 | ||
fb5f0bc8 BB |
943 | if (hardforce) { |
944 | if (zpool_export_force(zhp) != 0) | |
945 | ret = 1; | |
946 | } else if (zpool_export(zhp, force) != 0) { | |
34dc7c2f | 947 | ret = 1; |
fb5f0bc8 | 948 | } |
34dc7c2f BB |
949 | |
950 | zpool_close(zhp); | |
951 | } | |
952 | ||
953 | return (ret); | |
954 | } | |
955 | ||
956 | /* | |
957 | * Given a vdev configuration, determine the maximum width needed for the device | |
958 | * name column. | |
959 | */ | |
960 | static int | |
961 | max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max) | |
962 | { | |
428870ff | 963 | char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE); |
34dc7c2f BB |
964 | nvlist_t **child; |
965 | uint_t c, children; | |
966 | int ret; | |
967 | ||
968 | if (strlen(name) + depth > max) | |
969 | max = strlen(name) + depth; | |
970 | ||
971 | free(name); | |
972 | ||
973 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, | |
974 | &child, &children) == 0) { | |
975 | for (c = 0; c < children; c++) | |
976 | if ((ret = max_width(zhp, child[c], depth + 2, | |
977 | max)) > max) | |
978 | max = ret; | |
979 | } | |
980 | ||
981 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, | |
982 | &child, &children) == 0) { | |
983 | for (c = 0; c < children; c++) | |
984 | if ((ret = max_width(zhp, child[c], depth + 2, | |
985 | max)) > max) | |
986 | max = ret; | |
987 | } | |
988 | ||
989 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
990 | &child, &children) == 0) { | |
991 | for (c = 0; c < children; c++) | |
992 | if ((ret = max_width(zhp, child[c], depth + 2, | |
993 | max)) > max) | |
994 | max = ret; | |
995 | } | |
996 | ||
997 | ||
998 | return (max); | |
999 | } | |
1000 | ||
9babb374 BB |
1001 | typedef struct spare_cbdata { |
1002 | uint64_t cb_guid; | |
1003 | zpool_handle_t *cb_zhp; | |
1004 | } spare_cbdata_t; | |
1005 | ||
1006 | static boolean_t | |
1007 | find_vdev(nvlist_t *nv, uint64_t search) | |
1008 | { | |
1009 | uint64_t guid; | |
1010 | nvlist_t **child; | |
1011 | uint_t c, children; | |
1012 | ||
1013 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 && | |
1014 | search == guid) | |
1015 | return (B_TRUE); | |
1016 | ||
1017 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
1018 | &child, &children) == 0) { | |
1019 | for (c = 0; c < children; c++) | |
1020 | if (find_vdev(child[c], search)) | |
1021 | return (B_TRUE); | |
1022 | } | |
1023 | ||
1024 | return (B_FALSE); | |
1025 | } | |
1026 | ||
1027 | static int | |
1028 | find_spare(zpool_handle_t *zhp, void *data) | |
1029 | { | |
1030 | spare_cbdata_t *cbp = data; | |
1031 | nvlist_t *config, *nvroot; | |
1032 | ||
1033 | config = zpool_get_config(zhp, NULL); | |
1034 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
1035 | &nvroot) == 0); | |
1036 | ||
1037 | if (find_vdev(nvroot, cbp->cb_guid)) { | |
1038 | cbp->cb_zhp = zhp; | |
1039 | return (1); | |
1040 | } | |
1041 | ||
1042 | zpool_close(zhp); | |
1043 | return (0); | |
1044 | } | |
1045 | ||
1046 | /* | |
1047 | * Print out configuration state as requested by status_callback. | |
1048 | */ | |
1049 | void | |
1050 | print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv, | |
1051 | int namewidth, int depth, boolean_t isspare) | |
1052 | { | |
1053 | nvlist_t **child; | |
1054 | uint_t c, children; | |
428870ff | 1055 | pool_scan_stat_t *ps = NULL; |
9babb374 | 1056 | vdev_stat_t *vs; |
428870ff | 1057 | char rbuf[6], wbuf[6], cbuf[6]; |
9babb374 BB |
1058 | char *vname; |
1059 | uint64_t notpresent; | |
1060 | spare_cbdata_t cb; | |
1061 | char *state; | |
1062 | ||
9babb374 BB |
1063 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, |
1064 | &child, &children) != 0) | |
1065 | children = 0; | |
1066 | ||
428870ff BB |
1067 | verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, |
1068 | (uint64_t **)&vs, &c) == 0); | |
1069 | ||
9babb374 BB |
1070 | state = zpool_state_to_name(vs->vs_state, vs->vs_aux); |
1071 | if (isspare) { | |
1072 | /* | |
1073 | * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for | |
1074 | * online drives. | |
1075 | */ | |
1076 | if (vs->vs_aux == VDEV_AUX_SPARED) | |
1077 | state = "INUSE"; | |
1078 | else if (vs->vs_state == VDEV_STATE_HEALTHY) | |
1079 | state = "AVAIL"; | |
1080 | } | |
1081 | ||
1082 | (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth, | |
1083 | name, state); | |
1084 | ||
1085 | if (!isspare) { | |
1086 | zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf)); | |
1087 | zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf)); | |
1088 | zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf)); | |
1089 | (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf); | |
1090 | } | |
1091 | ||
1092 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, | |
1093 | ¬present) == 0) { | |
1094 | char *path; | |
1095 | verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); | |
1096 | (void) printf(" was %s", path); | |
1097 | } else if (vs->vs_aux != 0) { | |
1098 | (void) printf(" "); | |
1099 | ||
1100 | switch (vs->vs_aux) { | |
1101 | case VDEV_AUX_OPEN_FAILED: | |
1102 | (void) printf(gettext("cannot open")); | |
1103 | break; | |
1104 | ||
1105 | case VDEV_AUX_BAD_GUID_SUM: | |
1106 | (void) printf(gettext("missing device")); | |
1107 | break; | |
1108 | ||
1109 | case VDEV_AUX_NO_REPLICAS: | |
1110 | (void) printf(gettext("insufficient replicas")); | |
1111 | break; | |
1112 | ||
1113 | case VDEV_AUX_VERSION_NEWER: | |
1114 | (void) printf(gettext("newer version")); | |
1115 | break; | |
1116 | ||
1117 | case VDEV_AUX_SPARED: | |
1118 | verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, | |
1119 | &cb.cb_guid) == 0); | |
1120 | if (zpool_iter(g_zfs, find_spare, &cb) == 1) { | |
1121 | if (strcmp(zpool_get_name(cb.cb_zhp), | |
1122 | zpool_get_name(zhp)) == 0) | |
1123 | (void) printf(gettext("currently in " | |
1124 | "use")); | |
1125 | else | |
1126 | (void) printf(gettext("in use by " | |
1127 | "pool '%s'"), | |
1128 | zpool_get_name(cb.cb_zhp)); | |
1129 | zpool_close(cb.cb_zhp); | |
1130 | } else { | |
1131 | (void) printf(gettext("currently in use")); | |
1132 | } | |
1133 | break; | |
1134 | ||
1135 | case VDEV_AUX_ERR_EXCEEDED: | |
1136 | (void) printf(gettext("too many errors")); | |
1137 | break; | |
1138 | ||
1139 | case VDEV_AUX_IO_FAILURE: | |
1140 | (void) printf(gettext("experienced I/O failures")); | |
1141 | break; | |
1142 | ||
1143 | case VDEV_AUX_BAD_LOG: | |
1144 | (void) printf(gettext("bad intent log")); | |
1145 | break; | |
1146 | ||
428870ff BB |
1147 | case VDEV_AUX_EXTERNAL: |
1148 | (void) printf(gettext("external device fault")); | |
1149 | break; | |
1150 | ||
1151 | case VDEV_AUX_SPLIT_POOL: | |
1152 | (void) printf(gettext("split into new pool")); | |
1153 | break; | |
1154 | ||
9babb374 BB |
1155 | default: |
1156 | (void) printf(gettext("corrupted data")); | |
1157 | break; | |
1158 | } | |
428870ff BB |
1159 | } |
1160 | ||
1161 | (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS, | |
1162 | (uint64_t **)&ps, &c); | |
1163 | ||
1164 | if (ps && ps->pss_state == DSS_SCANNING && | |
1165 | vs->vs_scan_processed != 0 && children == 0) { | |
1166 | (void) printf(gettext(" (%s)"), | |
1167 | (ps->pss_func == POOL_SCAN_RESILVER) ? | |
1168 | "resilvering" : "repairing"); | |
9babb374 BB |
1169 | } |
1170 | ||
1171 | (void) printf("\n"); | |
1172 | ||
1173 | for (c = 0; c < children; c++) { | |
428870ff | 1174 | uint64_t islog = B_FALSE, ishole = B_FALSE; |
9babb374 | 1175 | |
428870ff | 1176 | /* Don't print logs or holes here */ |
9babb374 | 1177 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, |
428870ff BB |
1178 | &islog); |
1179 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, | |
1180 | &ishole); | |
1181 | if (islog || ishole) | |
9babb374 | 1182 | continue; |
428870ff | 1183 | vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE); |
9babb374 BB |
1184 | print_status_config(zhp, vname, child[c], |
1185 | namewidth, depth + 2, isspare); | |
1186 | free(vname); | |
1187 | } | |
1188 | } | |
1189 | ||
34dc7c2f BB |
1190 | |
1191 | /* | |
1192 | * Print the configuration of an exported pool. Iterate over all vdevs in the | |
1193 | * pool, printing out the name and status for each one. | |
1194 | */ | |
1195 | void | |
9babb374 | 1196 | print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth) |
34dc7c2f BB |
1197 | { |
1198 | nvlist_t **child; | |
1199 | uint_t c, children; | |
1200 | vdev_stat_t *vs; | |
1201 | char *type, *vname; | |
1202 | ||
1203 | verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); | |
428870ff BB |
1204 | if (strcmp(type, VDEV_TYPE_MISSING) == 0 || |
1205 | strcmp(type, VDEV_TYPE_HOLE) == 0) | |
34dc7c2f BB |
1206 | return; |
1207 | ||
428870ff | 1208 | verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
1209 | (uint64_t **)&vs, &c) == 0); |
1210 | ||
1211 | (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name); | |
1212 | (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux)); | |
1213 | ||
1214 | if (vs->vs_aux != 0) { | |
1215 | (void) printf(" "); | |
1216 | ||
1217 | switch (vs->vs_aux) { | |
1218 | case VDEV_AUX_OPEN_FAILED: | |
1219 | (void) printf(gettext("cannot open")); | |
1220 | break; | |
1221 | ||
1222 | case VDEV_AUX_BAD_GUID_SUM: | |
1223 | (void) printf(gettext("missing device")); | |
1224 | break; | |
1225 | ||
1226 | case VDEV_AUX_NO_REPLICAS: | |
1227 | (void) printf(gettext("insufficient replicas")); | |
1228 | break; | |
1229 | ||
1230 | case VDEV_AUX_VERSION_NEWER: | |
1231 | (void) printf(gettext("newer version")); | |
1232 | break; | |
1233 | ||
1234 | case VDEV_AUX_ERR_EXCEEDED: | |
1235 | (void) printf(gettext("too many errors")); | |
1236 | break; | |
1237 | ||
1238 | default: | |
1239 | (void) printf(gettext("corrupted data")); | |
1240 | break; | |
1241 | } | |
1242 | } | |
1243 | (void) printf("\n"); | |
1244 | ||
1245 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
1246 | &child, &children) != 0) | |
1247 | return; | |
1248 | ||
1249 | for (c = 0; c < children; c++) { | |
1250 | uint64_t is_log = B_FALSE; | |
1251 | ||
1252 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, | |
1253 | &is_log); | |
9babb374 | 1254 | if (is_log) |
34dc7c2f BB |
1255 | continue; |
1256 | ||
428870ff | 1257 | vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE); |
9babb374 | 1258 | print_import_config(vname, child[c], namewidth, depth + 2); |
34dc7c2f BB |
1259 | free(vname); |
1260 | } | |
1261 | ||
1262 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, | |
1263 | &child, &children) == 0) { | |
1264 | (void) printf(gettext("\tcache\n")); | |
1265 | for (c = 0; c < children; c++) { | |
428870ff | 1266 | vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE); |
34dc7c2f BB |
1267 | (void) printf("\t %s\n", vname); |
1268 | free(vname); | |
1269 | } | |
1270 | } | |
1271 | ||
1272 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, | |
1273 | &child, &children) == 0) { | |
1274 | (void) printf(gettext("\tspares\n")); | |
1275 | for (c = 0; c < children; c++) { | |
428870ff | 1276 | vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE); |
34dc7c2f BB |
1277 | (void) printf("\t %s\n", vname); |
1278 | free(vname); | |
1279 | } | |
1280 | } | |
1281 | } | |
1282 | ||
9babb374 BB |
1283 | /* |
1284 | * Print log vdevs. | |
1285 | * Logs are recorded as top level vdevs in the main pool child array | |
1286 | * but with "is_log" set to 1. We use either print_status_config() or | |
1287 | * print_import_config() to print the top level logs then any log | |
1288 | * children (eg mirrored slogs) are printed recursively - which | |
1289 | * works because only the top level vdev is marked "is_log" | |
1290 | */ | |
1291 | static void | |
1292 | print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose) | |
1293 | { | |
1294 | uint_t c, children; | |
1295 | nvlist_t **child; | |
1296 | ||
1297 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child, | |
1298 | &children) != 0) | |
1299 | return; | |
1300 | ||
1301 | (void) printf(gettext("\tlogs\n")); | |
1302 | ||
1303 | for (c = 0; c < children; c++) { | |
1304 | uint64_t is_log = B_FALSE; | |
1305 | char *name; | |
1306 | ||
1307 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, | |
1308 | &is_log); | |
1309 | if (!is_log) | |
1310 | continue; | |
428870ff | 1311 | name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE); |
9babb374 BB |
1312 | if (verbose) |
1313 | print_status_config(zhp, name, child[c], namewidth, | |
1314 | 2, B_FALSE); | |
1315 | else | |
1316 | print_import_config(name, child[c], namewidth, 2); | |
1317 | free(name); | |
1318 | } | |
1319 | } | |
428870ff | 1320 | |
34dc7c2f BB |
1321 | /* |
1322 | * Display the status for the given pool. | |
1323 | */ | |
1324 | static void | |
1325 | show_import(nvlist_t *config) | |
1326 | { | |
1327 | uint64_t pool_state; | |
1328 | vdev_stat_t *vs; | |
1329 | char *name; | |
1330 | uint64_t guid; | |
1331 | char *msgid; | |
1332 | nvlist_t *nvroot; | |
1333 | int reason; | |
1334 | const char *health; | |
1335 | uint_t vsc; | |
1336 | int namewidth; | |
1337 | ||
1338 | verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, | |
1339 | &name) == 0); | |
1340 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
1341 | &guid) == 0); | |
1342 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
1343 | &pool_state) == 0); | |
1344 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
1345 | &nvroot) == 0); | |
1346 | ||
428870ff | 1347 | verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
1348 | (uint64_t **)&vs, &vsc) == 0); |
1349 | health = zpool_state_to_name(vs->vs_state, vs->vs_aux); | |
1350 | ||
1351 | reason = zpool_import_status(config, &msgid); | |
1352 | ||
1353 | (void) printf(gettext(" pool: %s\n"), name); | |
1354 | (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid); | |
1355 | (void) printf(gettext(" state: %s"), health); | |
1356 | if (pool_state == POOL_STATE_DESTROYED) | |
1357 | (void) printf(gettext(" (DESTROYED)")); | |
1358 | (void) printf("\n"); | |
1359 | ||
1360 | switch (reason) { | |
1361 | case ZPOOL_STATUS_MISSING_DEV_R: | |
1362 | case ZPOOL_STATUS_MISSING_DEV_NR: | |
1363 | case ZPOOL_STATUS_BAD_GUID_SUM: | |
1364 | (void) printf(gettext("status: One or more devices are missing " | |
1365 | "from the system.\n")); | |
1366 | break; | |
1367 | ||
1368 | case ZPOOL_STATUS_CORRUPT_LABEL_R: | |
1369 | case ZPOOL_STATUS_CORRUPT_LABEL_NR: | |
1370 | (void) printf(gettext("status: One or more devices contains " | |
1371 | "corrupted data.\n")); | |
1372 | break; | |
1373 | ||
1374 | case ZPOOL_STATUS_CORRUPT_DATA: | |
1375 | (void) printf(gettext("status: The pool data is corrupted.\n")); | |
1376 | break; | |
1377 | ||
1378 | case ZPOOL_STATUS_OFFLINE_DEV: | |
1379 | (void) printf(gettext("status: One or more devices " | |
1380 | "are offlined.\n")); | |
1381 | break; | |
1382 | ||
1383 | case ZPOOL_STATUS_CORRUPT_POOL: | |
1384 | (void) printf(gettext("status: The pool metadata is " | |
1385 | "corrupted.\n")); | |
1386 | break; | |
1387 | ||
1388 | case ZPOOL_STATUS_VERSION_OLDER: | |
1389 | (void) printf(gettext("status: The pool is formatted using an " | |
1390 | "older on-disk version.\n")); | |
1391 | break; | |
1392 | ||
1393 | case ZPOOL_STATUS_VERSION_NEWER: | |
1394 | (void) printf(gettext("status: The pool is formatted using an " | |
1395 | "incompatible version.\n")); | |
1396 | break; | |
b128c09f | 1397 | |
34dc7c2f BB |
1398 | case ZPOOL_STATUS_HOSTID_MISMATCH: |
1399 | (void) printf(gettext("status: The pool was last accessed by " | |
1400 | "another system.\n")); | |
1401 | break; | |
b128c09f | 1402 | |
34dc7c2f BB |
1403 | case ZPOOL_STATUS_FAULTED_DEV_R: |
1404 | case ZPOOL_STATUS_FAULTED_DEV_NR: | |
1405 | (void) printf(gettext("status: One or more devices are " | |
1406 | "faulted.\n")); | |
1407 | break; | |
1408 | ||
b128c09f BB |
1409 | case ZPOOL_STATUS_BAD_LOG: |
1410 | (void) printf(gettext("status: An intent log record cannot be " | |
1411 | "read.\n")); | |
1412 | break; | |
1413 | ||
428870ff BB |
1414 | case ZPOOL_STATUS_RESILVERING: |
1415 | (void) printf(gettext("status: One or more devices were being " | |
1416 | "resilvered.\n")); | |
1417 | break; | |
1418 | ||
34dc7c2f BB |
1419 | default: |
1420 | /* | |
1421 | * No other status can be seen when importing pools. | |
1422 | */ | |
1423 | assert(reason == ZPOOL_STATUS_OK); | |
1424 | } | |
1425 | ||
1426 | /* | |
1427 | * Print out an action according to the overall state of the pool. | |
1428 | */ | |
1429 | if (vs->vs_state == VDEV_STATE_HEALTHY) { | |
1430 | if (reason == ZPOOL_STATUS_VERSION_OLDER) | |
1431 | (void) printf(gettext("action: The pool can be " | |
1432 | "imported using its name or numeric identifier, " | |
1433 | "though\n\tsome features will not be available " | |
1434 | "without an explicit 'zpool upgrade'.\n")); | |
1435 | else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) | |
1436 | (void) printf(gettext("action: The pool can be " | |
1437 | "imported using its name or numeric " | |
1438 | "identifier and\n\tthe '-f' flag.\n")); | |
1439 | else | |
1440 | (void) printf(gettext("action: The pool can be " | |
1441 | "imported using its name or numeric " | |
1442 | "identifier.\n")); | |
1443 | } else if (vs->vs_state == VDEV_STATE_DEGRADED) { | |
1444 | (void) printf(gettext("action: The pool can be imported " | |
1445 | "despite missing or damaged devices. The\n\tfault " | |
1446 | "tolerance of the pool may be compromised if imported.\n")); | |
1447 | } else { | |
1448 | switch (reason) { | |
1449 | case ZPOOL_STATUS_VERSION_NEWER: | |
1450 | (void) printf(gettext("action: The pool cannot be " | |
1451 | "imported. Access the pool on a system running " | |
1452 | "newer\n\tsoftware, or recreate the pool from " | |
1453 | "backup.\n")); | |
1454 | break; | |
1455 | case ZPOOL_STATUS_MISSING_DEV_R: | |
1456 | case ZPOOL_STATUS_MISSING_DEV_NR: | |
1457 | case ZPOOL_STATUS_BAD_GUID_SUM: | |
1458 | (void) printf(gettext("action: The pool cannot be " | |
1459 | "imported. Attach the missing\n\tdevices and try " | |
1460 | "again.\n")); | |
1461 | break; | |
1462 | default: | |
1463 | (void) printf(gettext("action: The pool cannot be " | |
1464 | "imported due to damaged devices or data.\n")); | |
1465 | } | |
1466 | } | |
1467 | ||
1468 | /* | |
1469 | * If the state is "closed" or "can't open", and the aux state | |
1470 | * is "corrupt data": | |
1471 | */ | |
1472 | if (((vs->vs_state == VDEV_STATE_CLOSED) || | |
1473 | (vs->vs_state == VDEV_STATE_CANT_OPEN)) && | |
1474 | (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) { | |
1475 | if (pool_state == POOL_STATE_DESTROYED) | |
1476 | (void) printf(gettext("\tThe pool was destroyed, " | |
1477 | "but can be imported using the '-Df' flags.\n")); | |
1478 | else if (pool_state != POOL_STATE_EXPORTED) | |
1479 | (void) printf(gettext("\tThe pool may be active on " | |
1480 | "another system, but can be imported using\n\t" | |
1481 | "the '-f' flag.\n")); | |
1482 | } | |
1483 | ||
1484 | if (msgid != NULL) | |
1485 | (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"), | |
1486 | msgid); | |
1487 | ||
1488 | (void) printf(gettext("config:\n\n")); | |
1489 | ||
1490 | namewidth = max_width(NULL, nvroot, 0, 0); | |
1491 | if (namewidth < 10) | |
1492 | namewidth = 10; | |
1493 | ||
9babb374 BB |
1494 | print_import_config(name, nvroot, namewidth, 0); |
1495 | if (num_logs(nvroot) > 0) | |
1496 | print_logs(NULL, nvroot, namewidth, B_FALSE); | |
34dc7c2f BB |
1497 | |
1498 | if (reason == ZPOOL_STATUS_BAD_GUID_SUM) { | |
1499 | (void) printf(gettext("\n\tAdditional devices are known to " | |
1500 | "be part of this pool, though their\n\texact " | |
1501 | "configuration cannot be determined.\n")); | |
1502 | } | |
1503 | } | |
1504 | ||
1505 | /* | |
1506 | * Perform the import for the given configuration. This passes the heavy | |
1507 | * lifting off to zpool_import_props(), and then mounts the datasets contained | |
1508 | * within the pool. | |
1509 | */ | |
1510 | static int | |
1511 | do_import(nvlist_t *config, const char *newname, const char *mntopts, | |
572e2857 | 1512 | nvlist_t *props, int flags) |
34dc7c2f BB |
1513 | { |
1514 | zpool_handle_t *zhp; | |
1515 | char *name; | |
1516 | uint64_t state; | |
1517 | uint64_t version; | |
34dc7c2f BB |
1518 | |
1519 | verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, | |
1520 | &name) == 0); | |
1521 | ||
1522 | verify(nvlist_lookup_uint64(config, | |
1523 | ZPOOL_CONFIG_POOL_STATE, &state) == 0); | |
1524 | verify(nvlist_lookup_uint64(config, | |
1525 | ZPOOL_CONFIG_VERSION, &version) == 0); | |
1526 | if (version > SPA_VERSION) { | |
1527 | (void) fprintf(stderr, gettext("cannot import '%s': pool " | |
1528 | "is formatted using a newer ZFS version\n"), name); | |
1529 | return (1); | |
572e2857 BB |
1530 | } else if (state != POOL_STATE_EXPORTED && |
1531 | !(flags & ZFS_IMPORT_ANY_HOST)) { | |
34dc7c2f BB |
1532 | uint64_t hostid; |
1533 | ||
1534 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, | |
1535 | &hostid) == 0) { | |
492b8e9e DH |
1536 | unsigned long system_hostid = gethostid() & 0xffffffff; |
1537 | ||
1538 | if ((unsigned long)hostid != system_hostid) { | |
34dc7c2f BB |
1539 | char *hostname; |
1540 | uint64_t timestamp; | |
1541 | time_t t; | |
1542 | ||
1543 | verify(nvlist_lookup_string(config, | |
1544 | ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); | |
1545 | verify(nvlist_lookup_uint64(config, | |
1546 | ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0); | |
1547 | t = timestamp; | |
1548 | (void) fprintf(stderr, gettext("cannot import " | |
1549 | "'%s': pool may be in use from other " | |
1550 | "system, it was last accessed by %s " | |
1551 | "(hostid: 0x%lx) on %s"), name, hostname, | |
1552 | (unsigned long)hostid, | |
1553 | asctime(localtime(&t))); | |
1554 | (void) fprintf(stderr, gettext("use '-f' to " | |
1555 | "import anyway\n")); | |
1556 | return (1); | |
1557 | } | |
1558 | } else { | |
1559 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
1560 | "pool may be in use from other system\n"), name); | |
1561 | (void) fprintf(stderr, gettext("use '-f' to import " | |
1562 | "anyway\n")); | |
1563 | return (1); | |
1564 | } | |
1565 | } | |
1566 | ||
572e2857 | 1567 | if (zpool_import_props(g_zfs, config, newname, props, flags) != 0) |
34dc7c2f BB |
1568 | return (1); |
1569 | ||
1570 | if (newname != NULL) | |
1571 | name = (char *)newname; | |
1572 | ||
45d1cae3 BB |
1573 | if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL) |
1574 | return (1); | |
34dc7c2f | 1575 | |
d164b209 | 1576 | if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && |
572e2857 | 1577 | !(flags & ZFS_IMPORT_ONLY) && |
d164b209 | 1578 | zpool_enable_datasets(zhp, mntopts, 0) != 0) { |
34dc7c2f BB |
1579 | zpool_close(zhp); |
1580 | return (1); | |
1581 | } | |
1582 | ||
1583 | zpool_close(zhp); | |
428870ff | 1584 | return (0); |
34dc7c2f BB |
1585 | } |
1586 | ||
1587 | /* | |
1588 | * zpool import [-d dir] [-D] | |
1589 | * import [-o mntopts] [-o prop=value] ... [-R root] [-D] | |
1590 | * [-d dir | -c cachefile] [-f] -a | |
1591 | * import [-o mntopts] [-o prop=value] ... [-R root] [-D] | |
428870ff | 1592 | * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool] |
34dc7c2f BB |
1593 | * |
1594 | * -c Read pool information from a cachefile instead of searching | |
1595 | * devices. | |
1596 | * | |
d603ed6c | 1597 | * -d Scan in a specific directory, other than /dev/. More than |
34dc7c2f BB |
1598 | * one directory can be specified using multiple '-d' options. |
1599 | * | |
1600 | * -D Scan for previously destroyed pools or import all or only | |
1601 | * specified destroyed pools. | |
1602 | * | |
1603 | * -R Temporarily import the pool, with all mountpoints relative to | |
1604 | * the given root. The pool will remain exported when the machine | |
1605 | * is rebooted. | |
1606 | * | |
428870ff | 1607 | * -V Import even in the presence of faulted vdevs. This is an |
b128c09f BB |
1608 | * intentionally undocumented option for testing purposes, and |
1609 | * treats the pool configuration as complete, leaving any bad | |
45d1cae3 BB |
1610 | * vdevs in the FAULTED state. In other words, it does verbatim |
1611 | * import. | |
b128c09f | 1612 | * |
428870ff BB |
1613 | * -f Force import, even if it appears that the pool is active. |
1614 | * | |
1615 | * -F Attempt rewind if necessary. | |
1616 | * | |
1617 | * -n See if rewind would work, but don't actually rewind. | |
1618 | * | |
572e2857 BB |
1619 | * -N Import the pool but don't mount datasets. |
1620 | * | |
1621 | * -T Specify a starting txg to use for import. This option is | |
1622 | * intentionally undocumented option for testing purposes. | |
1623 | * | |
34dc7c2f BB |
1624 | * -a Import all pools found. |
1625 | * | |
1626 | * -o Set property=value and/or temporary mount options (without '='). | |
1627 | * | |
1628 | * The import command scans for pools to import, and import pools based on pool | |
1629 | * name and GUID. The pool can also be renamed as part of the import process. | |
1630 | */ | |
1631 | int | |
1632 | zpool_do_import(int argc, char **argv) | |
1633 | { | |
1634 | char **searchdirs = NULL; | |
1635 | int nsearch = 0; | |
1636 | int c; | |
428870ff | 1637 | int err = 0; |
34dc7c2f BB |
1638 | nvlist_t *pools = NULL; |
1639 | boolean_t do_all = B_FALSE; | |
1640 | boolean_t do_destroyed = B_FALSE; | |
1641 | char *mntopts = NULL; | |
34dc7c2f BB |
1642 | nvpair_t *elem; |
1643 | nvlist_t *config; | |
b128c09f BB |
1644 | uint64_t searchguid = 0; |
1645 | char *searchname = NULL; | |
34dc7c2f BB |
1646 | char *propval; |
1647 | nvlist_t *found_config; | |
428870ff | 1648 | nvlist_t *policy = NULL; |
34dc7c2f BB |
1649 | nvlist_t *props = NULL; |
1650 | boolean_t first; | |
572e2857 | 1651 | int flags = ZFS_IMPORT_NORMAL; |
428870ff BB |
1652 | uint32_t rewind_policy = ZPOOL_NO_REWIND; |
1653 | boolean_t dryrun = B_FALSE; | |
1654 | boolean_t do_rewind = B_FALSE; | |
1655 | boolean_t xtreme_rewind = B_FALSE; | |
572e2857 | 1656 | uint64_t pool_state, txg = -1ULL; |
34dc7c2f | 1657 | char *cachefile = NULL; |
428870ff | 1658 | importargs_t idata = { 0 }; |
572e2857 | 1659 | char *endptr; |
34dc7c2f BB |
1660 | |
1661 | /* check options */ | |
572e2857 | 1662 | while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) { |
34dc7c2f BB |
1663 | switch (c) { |
1664 | case 'a': | |
1665 | do_all = B_TRUE; | |
1666 | break; | |
1667 | case 'c': | |
1668 | cachefile = optarg; | |
1669 | break; | |
1670 | case 'd': | |
1671 | if (searchdirs == NULL) { | |
1672 | searchdirs = safe_malloc(sizeof (char *)); | |
1673 | } else { | |
1674 | char **tmp = safe_malloc((nsearch + 1) * | |
1675 | sizeof (char *)); | |
1676 | bcopy(searchdirs, tmp, nsearch * | |
1677 | sizeof (char *)); | |
1678 | free(searchdirs); | |
1679 | searchdirs = tmp; | |
1680 | } | |
1681 | searchdirs[nsearch++] = optarg; | |
1682 | break; | |
1683 | case 'D': | |
1684 | do_destroyed = B_TRUE; | |
1685 | break; | |
1686 | case 'f': | |
572e2857 | 1687 | flags |= ZFS_IMPORT_ANY_HOST; |
34dc7c2f | 1688 | break; |
b128c09f | 1689 | case 'F': |
428870ff BB |
1690 | do_rewind = B_TRUE; |
1691 | break; | |
572e2857 BB |
1692 | case 'm': |
1693 | flags |= ZFS_IMPORT_MISSING_LOG; | |
1694 | break; | |
428870ff BB |
1695 | case 'n': |
1696 | dryrun = B_TRUE; | |
b128c09f | 1697 | break; |
572e2857 BB |
1698 | case 'N': |
1699 | flags |= ZFS_IMPORT_ONLY; | |
1700 | break; | |
34dc7c2f BB |
1701 | case 'o': |
1702 | if ((propval = strchr(optarg, '=')) != NULL) { | |
1703 | *propval = '\0'; | |
1704 | propval++; | |
b128c09f BB |
1705 | if (add_prop_list(optarg, propval, |
1706 | &props, B_TRUE)) | |
34dc7c2f BB |
1707 | goto error; |
1708 | } else { | |
1709 | mntopts = optarg; | |
1710 | } | |
1711 | break; | |
1712 | case 'R': | |
1713 | if (add_prop_list(zpool_prop_to_name( | |
b128c09f | 1714 | ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) |
34dc7c2f BB |
1715 | goto error; |
1716 | if (nvlist_lookup_string(props, | |
1717 | zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), | |
1718 | &propval) == 0) | |
1719 | break; | |
1720 | if (add_prop_list(zpool_prop_to_name( | |
b128c09f | 1721 | ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) |
34dc7c2f BB |
1722 | goto error; |
1723 | break; | |
572e2857 BB |
1724 | case 'T': |
1725 | errno = 0; | |
1726 | txg = strtoull(optarg, &endptr, 10); | |
1727 | if (errno != 0 || *endptr != '\0') { | |
1728 | (void) fprintf(stderr, | |
1729 | gettext("invalid txg value\n")); | |
1730 | usage(B_FALSE); | |
1731 | } | |
1732 | rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND; | |
1733 | break; | |
428870ff | 1734 | case 'V': |
572e2857 | 1735 | flags |= ZFS_IMPORT_VERBATIM; |
428870ff BB |
1736 | break; |
1737 | case 'X': | |
1738 | xtreme_rewind = B_TRUE; | |
1739 | break; | |
34dc7c2f BB |
1740 | case ':': |
1741 | (void) fprintf(stderr, gettext("missing argument for " | |
1742 | "'%c' option\n"), optopt); | |
1743 | usage(B_FALSE); | |
1744 | break; | |
1745 | case '?': | |
1746 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
1747 | optopt); | |
1748 | usage(B_FALSE); | |
1749 | } | |
1750 | } | |
1751 | ||
1752 | argc -= optind; | |
1753 | argv += optind; | |
1754 | ||
1755 | if (cachefile && nsearch != 0) { | |
1756 | (void) fprintf(stderr, gettext("-c is incompatible with -d\n")); | |
1757 | usage(B_FALSE); | |
1758 | } | |
1759 | ||
428870ff BB |
1760 | if ((dryrun || xtreme_rewind) && !do_rewind) { |
1761 | (void) fprintf(stderr, | |
1762 | gettext("-n or -X only meaningful with -F\n")); | |
1763 | usage(B_FALSE); | |
1764 | } | |
1765 | if (dryrun) | |
1766 | rewind_policy = ZPOOL_TRY_REWIND; | |
1767 | else if (do_rewind) | |
1768 | rewind_policy = ZPOOL_DO_REWIND; | |
1769 | if (xtreme_rewind) | |
1770 | rewind_policy |= ZPOOL_EXTREME_REWIND; | |
1771 | ||
1772 | /* In the future, we can capture further policy and include it here */ | |
1773 | if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || | |
572e2857 | 1774 | nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 || |
428870ff BB |
1775 | nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0) |
1776 | goto error; | |
1777 | ||
34dc7c2f BB |
1778 | /* check argument count */ |
1779 | if (do_all) { | |
1780 | if (argc != 0) { | |
1781 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
1782 | usage(B_FALSE); | |
1783 | } | |
1784 | } else { | |
1785 | if (argc > 2) { | |
1786 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
1787 | usage(B_FALSE); | |
1788 | } | |
1789 | ||
1790 | /* | |
1791 | * Check for the SYS_CONFIG privilege. We do this explicitly | |
1792 | * here because otherwise any attempt to discover pools will | |
1793 | * silently fail. | |
1794 | */ | |
1795 | if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) { | |
1796 | (void) fprintf(stderr, gettext("cannot " | |
1797 | "discover pools: permission denied\n")); | |
d603ed6c BB |
1798 | if (searchdirs != NULL) |
1799 | free(searchdirs); | |
1800 | ||
428870ff | 1801 | nvlist_free(policy); |
34dc7c2f BB |
1802 | return (1); |
1803 | } | |
1804 | } | |
1805 | ||
34dc7c2f | 1806 | /* |
34dc7c2f BB |
1807 | * Depending on the arguments given, we do one of the following: |
1808 | * | |
1809 | * <none> Iterate through all pools and display information about | |
1810 | * each one. | |
1811 | * | |
1812 | * -a Iterate through all pools and try to import each one. | |
1813 | * | |
1814 | * <id> Find the pool that corresponds to the given GUID/pool | |
1815 | * name and import that one. | |
1816 | * | |
1817 | * -D Above options applies only to destroyed pools. | |
1818 | */ | |
1819 | if (argc != 0) { | |
1820 | char *endptr; | |
1821 | ||
1822 | errno = 0; | |
1823 | searchguid = strtoull(argv[0], &endptr, 10); | |
1824 | if (errno != 0 || *endptr != '\0') | |
1825 | searchname = argv[0]; | |
34dc7c2f | 1826 | found_config = NULL; |
34dc7c2f | 1827 | |
b128c09f | 1828 | /* |
428870ff | 1829 | * User specified a name or guid. Ensure it's unique. |
b128c09f | 1830 | */ |
428870ff | 1831 | idata.unique = B_TRUE; |
b128c09f BB |
1832 | } |
1833 | ||
428870ff BB |
1834 | |
1835 | idata.path = searchdirs; | |
1836 | idata.paths = nsearch; | |
1837 | idata.poolname = searchname; | |
1838 | idata.guid = searchguid; | |
1839 | idata.cachefile = cachefile; | |
1840 | ||
1841 | pools = zpool_search_import(g_zfs, &idata); | |
1842 | ||
1843 | if (pools != NULL && idata.exists && | |
1844 | (argc == 1 || strcmp(argv[0], argv[1]) == 0)) { | |
1845 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
1846 | "a pool with that name already exists\n"), | |
1847 | argv[0]); | |
1848 | (void) fprintf(stderr, gettext("use the form '%s " | |
1849 | "<pool | id> <newpool>' to give it a new name\n"), | |
1850 | "zpool import"); | |
1851 | err = 1; | |
1852 | } else if (pools == NULL && idata.exists) { | |
1853 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
1854 | "a pool with that name is already created/imported,\n"), | |
1855 | argv[0]); | |
1856 | (void) fprintf(stderr, gettext("and no additional pools " | |
1857 | "with that name were found\n")); | |
1858 | err = 1; | |
1859 | } else if (pools == NULL) { | |
b128c09f BB |
1860 | if (argc != 0) { |
1861 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
1862 | "no such pool available\n"), argv[0]); | |
1863 | } | |
428870ff BB |
1864 | err = 1; |
1865 | } | |
1866 | ||
1867 | if (err == 1) { | |
d603ed6c BB |
1868 | if (searchdirs != NULL) |
1869 | free(searchdirs); | |
428870ff | 1870 | nvlist_free(policy); |
b128c09f BB |
1871 | return (1); |
1872 | } | |
1873 | ||
1874 | /* | |
1875 | * At this point we have a list of import candidate configs. Even if | |
1876 | * we were searching by pool name or guid, we still need to | |
1877 | * post-process the list to deal with pool state and possible | |
1878 | * duplicate names. | |
1879 | */ | |
34dc7c2f BB |
1880 | err = 0; |
1881 | elem = NULL; | |
1882 | first = B_TRUE; | |
1883 | while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { | |
1884 | ||
1885 | verify(nvpair_value_nvlist(elem, &config) == 0); | |
1886 | ||
1887 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
1888 | &pool_state) == 0); | |
1889 | if (!do_destroyed && pool_state == POOL_STATE_DESTROYED) | |
1890 | continue; | |
1891 | if (do_destroyed && pool_state != POOL_STATE_DESTROYED) | |
1892 | continue; | |
1893 | ||
428870ff BB |
1894 | verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY, |
1895 | policy) == 0); | |
1896 | ||
34dc7c2f BB |
1897 | if (argc == 0) { |
1898 | if (first) | |
1899 | first = B_FALSE; | |
1900 | else if (!do_all) | |
1901 | (void) printf("\n"); | |
1902 | ||
428870ff | 1903 | if (do_all) { |
34dc7c2f | 1904 | err |= do_import(config, NULL, mntopts, |
572e2857 | 1905 | props, flags); |
428870ff | 1906 | } else { |
34dc7c2f | 1907 | show_import(config); |
428870ff | 1908 | } |
34dc7c2f BB |
1909 | } else if (searchname != NULL) { |
1910 | char *name; | |
1911 | ||
1912 | /* | |
1913 | * We are searching for a pool based on name. | |
1914 | */ | |
1915 | verify(nvlist_lookup_string(config, | |
1916 | ZPOOL_CONFIG_POOL_NAME, &name) == 0); | |
1917 | ||
1918 | if (strcmp(name, searchname) == 0) { | |
1919 | if (found_config != NULL) { | |
1920 | (void) fprintf(stderr, gettext( | |
1921 | "cannot import '%s': more than " | |
1922 | "one matching pool\n"), searchname); | |
1923 | (void) fprintf(stderr, gettext( | |
1924 | "import by numeric ID instead\n")); | |
1925 | err = B_TRUE; | |
1926 | } | |
1927 | found_config = config; | |
1928 | } | |
1929 | } else { | |
1930 | uint64_t guid; | |
1931 | ||
1932 | /* | |
1933 | * Search for a pool by guid. | |
1934 | */ | |
1935 | verify(nvlist_lookup_uint64(config, | |
1936 | ZPOOL_CONFIG_POOL_GUID, &guid) == 0); | |
1937 | ||
1938 | if (guid == searchguid) | |
1939 | found_config = config; | |
1940 | } | |
1941 | } | |
1942 | ||
1943 | /* | |
1944 | * If we were searching for a specific pool, verify that we found a | |
1945 | * pool, and then do the import. | |
1946 | */ | |
1947 | if (argc != 0 && err == 0) { | |
1948 | if (found_config == NULL) { | |
1949 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
1950 | "no such pool available\n"), argv[0]); | |
1951 | err = B_TRUE; | |
1952 | } else { | |
1953 | err |= do_import(found_config, argc == 1 ? NULL : | |
572e2857 | 1954 | argv[1], mntopts, props, flags); |
34dc7c2f BB |
1955 | } |
1956 | } | |
1957 | ||
1958 | /* | |
1959 | * If we were just looking for pools, report an error if none were | |
1960 | * found. | |
1961 | */ | |
1962 | if (argc == 0 && first) | |
1963 | (void) fprintf(stderr, | |
1964 | gettext("no pools available to import\n")); | |
1965 | ||
1966 | error: | |
1967 | nvlist_free(props); | |
1968 | nvlist_free(pools); | |
428870ff | 1969 | nvlist_free(policy); |
d603ed6c BB |
1970 | if (searchdirs != NULL) |
1971 | free(searchdirs); | |
34dc7c2f BB |
1972 | |
1973 | return (err ? 1 : 0); | |
1974 | } | |
1975 | ||
1976 | typedef struct iostat_cbdata { | |
1977 | zpool_list_t *cb_list; | |
1978 | int cb_verbose; | |
1979 | int cb_iteration; | |
1980 | int cb_namewidth; | |
1981 | } iostat_cbdata_t; | |
1982 | ||
1983 | static void | |
1984 | print_iostat_separator(iostat_cbdata_t *cb) | |
1985 | { | |
1986 | int i = 0; | |
1987 | ||
1988 | for (i = 0; i < cb->cb_namewidth; i++) | |
1989 | (void) printf("-"); | |
1990 | (void) printf(" ----- ----- ----- ----- ----- -----\n"); | |
1991 | } | |
1992 | ||
1993 | static void | |
1994 | print_iostat_header(iostat_cbdata_t *cb) | |
1995 | { | |
1996 | (void) printf("%*s capacity operations bandwidth\n", | |
1997 | cb->cb_namewidth, ""); | |
428870ff | 1998 | (void) printf("%-*s alloc free read write read write\n", |
34dc7c2f BB |
1999 | cb->cb_namewidth, "pool"); |
2000 | print_iostat_separator(cb); | |
2001 | } | |
2002 | ||
2003 | /* | |
2004 | * Display a single statistic. | |
2005 | */ | |
2006 | static void | |
2007 | print_one_stat(uint64_t value) | |
2008 | { | |
2009 | char buf[64]; | |
2010 | ||
2011 | zfs_nicenum(value, buf, sizeof (buf)); | |
2012 | (void) printf(" %5s", buf); | |
2013 | } | |
2014 | ||
2015 | /* | |
2016 | * Print out all the statistics for the given vdev. This can either be the | |
2017 | * toplevel configuration, or called recursively. If 'name' is NULL, then this | |
2018 | * is a verbose output, and we don't want to display the toplevel pool stats. | |
2019 | */ | |
2020 | void | |
2021 | print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv, | |
2022 | nvlist_t *newnv, iostat_cbdata_t *cb, int depth) | |
2023 | { | |
2024 | nvlist_t **oldchild, **newchild; | |
2025 | uint_t c, children; | |
2026 | vdev_stat_t *oldvs, *newvs; | |
2027 | vdev_stat_t zerovs = { 0 }; | |
2028 | uint64_t tdelta; | |
2029 | double scale; | |
2030 | char *vname; | |
2031 | ||
2032 | if (oldnv != NULL) { | |
428870ff BB |
2033 | verify(nvlist_lookup_uint64_array(oldnv, |
2034 | ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0); | |
34dc7c2f BB |
2035 | } else { |
2036 | oldvs = &zerovs; | |
2037 | } | |
2038 | ||
428870ff | 2039 | verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
2040 | (uint64_t **)&newvs, &c) == 0); |
2041 | ||
2042 | if (strlen(name) + depth > cb->cb_namewidth) | |
2043 | (void) printf("%*s%s", depth, "", name); | |
2044 | else | |
2045 | (void) printf("%*s%s%*s", depth, "", name, | |
2046 | (int)(cb->cb_namewidth - strlen(name) - depth), ""); | |
2047 | ||
2048 | tdelta = newvs->vs_timestamp - oldvs->vs_timestamp; | |
2049 | ||
2050 | if (tdelta == 0) | |
2051 | scale = 1.0; | |
2052 | else | |
2053 | scale = (double)NANOSEC / tdelta; | |
2054 | ||
2055 | /* only toplevel vdevs have capacity stats */ | |
2056 | if (newvs->vs_space == 0) { | |
2057 | (void) printf(" - -"); | |
2058 | } else { | |
2059 | print_one_stat(newvs->vs_alloc); | |
2060 | print_one_stat(newvs->vs_space - newvs->vs_alloc); | |
2061 | } | |
2062 | ||
2063 | print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] - | |
2064 | oldvs->vs_ops[ZIO_TYPE_READ]))); | |
2065 | ||
2066 | print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] - | |
2067 | oldvs->vs_ops[ZIO_TYPE_WRITE]))); | |
2068 | ||
2069 | print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] - | |
2070 | oldvs->vs_bytes[ZIO_TYPE_READ]))); | |
2071 | ||
2072 | print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] - | |
2073 | oldvs->vs_bytes[ZIO_TYPE_WRITE]))); | |
2074 | ||
2075 | (void) printf("\n"); | |
2076 | ||
2077 | if (!cb->cb_verbose) | |
2078 | return; | |
2079 | ||
2080 | if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN, | |
2081 | &newchild, &children) != 0) | |
2082 | return; | |
2083 | ||
2084 | if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN, | |
2085 | &oldchild, &c) != 0) | |
2086 | return; | |
2087 | ||
2088 | for (c = 0; c < children; c++) { | |
428870ff BB |
2089 | uint64_t ishole = B_FALSE; |
2090 | ||
2091 | if (nvlist_lookup_uint64(newchild[c], | |
2092 | ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole) | |
2093 | continue; | |
2094 | ||
2095 | vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE); | |
34dc7c2f BB |
2096 | print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL, |
2097 | newchild[c], cb, depth + 2); | |
2098 | free(vname); | |
2099 | } | |
2100 | ||
2101 | /* | |
2102 | * Include level 2 ARC devices in iostat output | |
2103 | */ | |
2104 | if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE, | |
2105 | &newchild, &children) != 0) | |
2106 | return; | |
2107 | ||
2108 | if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE, | |
2109 | &oldchild, &c) != 0) | |
2110 | return; | |
2111 | ||
2112 | if (children > 0) { | |
2113 | (void) printf("%-*s - - - - - " | |
2114 | "-\n", cb->cb_namewidth, "cache"); | |
2115 | for (c = 0; c < children; c++) { | |
428870ff BB |
2116 | vname = zpool_vdev_name(g_zfs, zhp, newchild[c], |
2117 | B_FALSE); | |
34dc7c2f BB |
2118 | print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL, |
2119 | newchild[c], cb, depth + 2); | |
2120 | free(vname); | |
2121 | } | |
2122 | } | |
2123 | } | |
2124 | ||
2125 | static int | |
2126 | refresh_iostat(zpool_handle_t *zhp, void *data) | |
2127 | { | |
2128 | iostat_cbdata_t *cb = data; | |
2129 | boolean_t missing; | |
2130 | ||
2131 | /* | |
2132 | * If the pool has disappeared, remove it from the list and continue. | |
2133 | */ | |
2134 | if (zpool_refresh_stats(zhp, &missing) != 0) | |
2135 | return (-1); | |
2136 | ||
2137 | if (missing) | |
2138 | pool_list_remove(cb->cb_list, zhp); | |
2139 | ||
2140 | return (0); | |
2141 | } | |
2142 | ||
2143 | /* | |
2144 | * Callback to print out the iostats for the given pool. | |
2145 | */ | |
2146 | int | |
2147 | print_iostat(zpool_handle_t *zhp, void *data) | |
2148 | { | |
2149 | iostat_cbdata_t *cb = data; | |
2150 | nvlist_t *oldconfig, *newconfig; | |
2151 | nvlist_t *oldnvroot, *newnvroot; | |
2152 | ||
2153 | newconfig = zpool_get_config(zhp, &oldconfig); | |
2154 | ||
2155 | if (cb->cb_iteration == 1) | |
2156 | oldconfig = NULL; | |
2157 | ||
2158 | verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE, | |
2159 | &newnvroot) == 0); | |
2160 | ||
2161 | if (oldconfig == NULL) | |
2162 | oldnvroot = NULL; | |
2163 | else | |
2164 | verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE, | |
2165 | &oldnvroot) == 0); | |
2166 | ||
2167 | /* | |
2168 | * Print out the statistics for the pool. | |
2169 | */ | |
2170 | print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0); | |
2171 | ||
2172 | if (cb->cb_verbose) | |
2173 | print_iostat_separator(cb); | |
2174 | ||
2175 | return (0); | |
2176 | } | |
2177 | ||
2178 | int | |
2179 | get_namewidth(zpool_handle_t *zhp, void *data) | |
2180 | { | |
2181 | iostat_cbdata_t *cb = data; | |
2182 | nvlist_t *config, *nvroot; | |
2183 | ||
2184 | if ((config = zpool_get_config(zhp, NULL)) != NULL) { | |
2185 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
2186 | &nvroot) == 0); | |
2187 | if (!cb->cb_verbose) | |
2188 | cb->cb_namewidth = strlen(zpool_get_name(zhp)); | |
2189 | else | |
2190 | cb->cb_namewidth = max_width(zhp, nvroot, 0, 0); | |
2191 | } | |
2192 | ||
2193 | /* | |
2194 | * The width must fall into the range [10,38]. The upper limit is the | |
2195 | * maximum we can have and still fit in 80 columns. | |
2196 | */ | |
2197 | if (cb->cb_namewidth < 10) | |
2198 | cb->cb_namewidth = 10; | |
2199 | if (cb->cb_namewidth > 38) | |
2200 | cb->cb_namewidth = 38; | |
2201 | ||
2202 | return (0); | |
2203 | } | |
2204 | ||
2205 | /* | |
428870ff | 2206 | * Parse the input string, get the 'interval' and 'count' value if there is one. |
34dc7c2f | 2207 | */ |
428870ff BB |
2208 | static void |
2209 | get_interval_count(int *argcp, char **argv, unsigned long *iv, | |
2210 | unsigned long *cnt) | |
34dc7c2f | 2211 | { |
34dc7c2f | 2212 | unsigned long interval = 0, count = 0; |
1fde1e37 | 2213 | int argc = *argcp; |
34dc7c2f BB |
2214 | |
2215 | /* | |
2216 | * Determine if the last argument is an integer or a pool name | |
2217 | */ | |
2218 | if (argc > 0 && isdigit(argv[argc - 1][0])) { | |
2219 | char *end; | |
2220 | ||
2221 | errno = 0; | |
2222 | interval = strtoul(argv[argc - 1], &end, 10); | |
2223 | ||
2224 | if (*end == '\0' && errno == 0) { | |
2225 | if (interval == 0) { | |
2226 | (void) fprintf(stderr, gettext("interval " | |
2227 | "cannot be zero\n")); | |
2228 | usage(B_FALSE); | |
2229 | } | |
34dc7c2f BB |
2230 | /* |
2231 | * Ignore the last parameter | |
2232 | */ | |
2233 | argc--; | |
2234 | } else { | |
2235 | /* | |
2236 | * If this is not a valid number, just plow on. The | |
2237 | * user will get a more informative error message later | |
2238 | * on. | |
2239 | */ | |
2240 | interval = 0; | |
2241 | } | |
2242 | } | |
2243 | ||
2244 | /* | |
2245 | * If the last argument is also an integer, then we have both a count | |
428870ff | 2246 | * and an interval. |
34dc7c2f BB |
2247 | */ |
2248 | if (argc > 0 && isdigit(argv[argc - 1][0])) { | |
2249 | char *end; | |
2250 | ||
2251 | errno = 0; | |
2252 | count = interval; | |
2253 | interval = strtoul(argv[argc - 1], &end, 10); | |
2254 | ||
2255 | if (*end == '\0' && errno == 0) { | |
2256 | if (interval == 0) { | |
2257 | (void) fprintf(stderr, gettext("interval " | |
2258 | "cannot be zero\n")); | |
2259 | usage(B_FALSE); | |
2260 | } | |
2261 | ||
2262 | /* | |
2263 | * Ignore the last parameter | |
2264 | */ | |
2265 | argc--; | |
2266 | } else { | |
2267 | interval = 0; | |
2268 | } | |
2269 | } | |
2270 | ||
428870ff BB |
2271 | *iv = interval; |
2272 | *cnt = count; | |
2273 | *argcp = argc; | |
2274 | } | |
2275 | ||
2276 | static void | |
2277 | get_timestamp_arg(char c) | |
2278 | { | |
2279 | if (c == 'u') | |
2280 | timestamp_fmt = UDATE; | |
2281 | else if (c == 'd') | |
2282 | timestamp_fmt = DDATE; | |
2283 | else | |
2284 | usage(B_FALSE); | |
2285 | } | |
2286 | ||
2287 | /* | |
2288 | * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]] | |
2289 | * | |
2290 | * -v Display statistics for individual vdevs | |
2291 | * -T Display a timestamp in date(1) or Unix format | |
2292 | * | |
2293 | * This command can be tricky because we want to be able to deal with pool | |
2294 | * creation/destruction as well as vdev configuration changes. The bulk of this | |
2295 | * processing is handled by the pool_list_* routines in zpool_iter.c. We rely | |
2296 | * on pool_list_update() to detect the addition of new pools. Configuration | |
2297 | * changes are all handled within libzfs. | |
2298 | */ | |
2299 | int | |
2300 | zpool_do_iostat(int argc, char **argv) | |
2301 | { | |
2302 | int c; | |
2303 | int ret; | |
2304 | int npools; | |
2305 | unsigned long interval = 0, count = 0; | |
2306 | zpool_list_t *list; | |
2307 | boolean_t verbose = B_FALSE; | |
2308 | iostat_cbdata_t cb; | |
2309 | ||
2310 | /* check options */ | |
2311 | while ((c = getopt(argc, argv, "T:v")) != -1) { | |
2312 | switch (c) { | |
2313 | case 'T': | |
2314 | get_timestamp_arg(*optarg); | |
2315 | break; | |
2316 | case 'v': | |
2317 | verbose = B_TRUE; | |
2318 | break; | |
2319 | case '?': | |
2320 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
2321 | optopt); | |
2322 | usage(B_FALSE); | |
2323 | } | |
2324 | } | |
2325 | ||
2326 | argc -= optind; | |
2327 | argv += optind; | |
2328 | ||
2329 | get_interval_count(&argc, argv, &interval, &count); | |
2330 | ||
34dc7c2f BB |
2331 | /* |
2332 | * Construct the list of all interesting pools. | |
2333 | */ | |
2334 | ret = 0; | |
2335 | if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL) | |
2336 | return (1); | |
2337 | ||
2338 | if (pool_list_count(list) == 0 && argc != 0) { | |
2339 | pool_list_free(list); | |
2340 | return (1); | |
2341 | } | |
2342 | ||
2343 | if (pool_list_count(list) == 0 && interval == 0) { | |
2344 | pool_list_free(list); | |
2345 | (void) fprintf(stderr, gettext("no pools available\n")); | |
2346 | return (1); | |
2347 | } | |
2348 | ||
2349 | /* | |
2350 | * Enter the main iostat loop. | |
2351 | */ | |
2352 | cb.cb_list = list; | |
2353 | cb.cb_verbose = verbose; | |
2354 | cb.cb_iteration = 0; | |
2355 | cb.cb_namewidth = 0; | |
2356 | ||
2357 | for (;;) { | |
2358 | pool_list_update(list); | |
2359 | ||
2360 | if ((npools = pool_list_count(list)) == 0) | |
2361 | break; | |
2362 | ||
2363 | /* | |
2364 | * Refresh all statistics. This is done as an explicit step | |
2365 | * before calculating the maximum name width, so that any | |
2366 | * configuration changes are properly accounted for. | |
2367 | */ | |
2368 | (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb); | |
2369 | ||
2370 | /* | |
2371 | * Iterate over all pools to determine the maximum width | |
2372 | * for the pool / device name column across all pools. | |
2373 | */ | |
2374 | cb.cb_namewidth = 0; | |
2375 | (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb); | |
2376 | ||
428870ff BB |
2377 | if (timestamp_fmt != NODATE) |
2378 | print_timestamp(timestamp_fmt); | |
2379 | ||
34dc7c2f BB |
2380 | /* |
2381 | * If it's the first time, or verbose mode, print the header. | |
2382 | */ | |
2383 | if (++cb.cb_iteration == 1 || verbose) | |
2384 | print_iostat_header(&cb); | |
2385 | ||
2386 | (void) pool_list_iter(list, B_FALSE, print_iostat, &cb); | |
2387 | ||
2388 | /* | |
2389 | * If there's more than one pool, and we're not in verbose mode | |
2390 | * (which prints a separator for us), then print a separator. | |
2391 | */ | |
2392 | if (npools > 1 && !verbose) | |
2393 | print_iostat_separator(&cb); | |
2394 | ||
2395 | if (verbose) | |
2396 | (void) printf("\n"); | |
2397 | ||
2398 | /* | |
2399 | * Flush the output so that redirection to a file isn't buffered | |
2400 | * indefinitely. | |
2401 | */ | |
2402 | (void) fflush(stdout); | |
2403 | ||
2404 | if (interval == 0) | |
2405 | break; | |
2406 | ||
2407 | if (count != 0 && --count == 0) | |
2408 | break; | |
2409 | ||
2410 | (void) sleep(interval); | |
2411 | } | |
2412 | ||
2413 | pool_list_free(list); | |
2414 | ||
2415 | return (ret); | |
2416 | } | |
2417 | ||
2418 | typedef struct list_cbdata { | |
2419 | boolean_t cb_scripted; | |
2420 | boolean_t cb_first; | |
2421 | zprop_list_t *cb_proplist; | |
2422 | } list_cbdata_t; | |
2423 | ||
2424 | /* | |
2425 | * Given a list of columns to display, output appropriate headers for each one. | |
2426 | */ | |
2427 | static void | |
2428 | print_header(zprop_list_t *pl) | |
2429 | { | |
2430 | const char *header; | |
2431 | boolean_t first = B_TRUE; | |
2432 | boolean_t right_justify; | |
2433 | ||
2434 | for (; pl != NULL; pl = pl->pl_next) { | |
2435 | if (pl->pl_prop == ZPROP_INVAL) | |
2436 | continue; | |
2437 | ||
2438 | if (!first) | |
2439 | (void) printf(" "); | |
2440 | else | |
2441 | first = B_FALSE; | |
2442 | ||
2443 | header = zpool_prop_column_name(pl->pl_prop); | |
2444 | right_justify = zpool_prop_align_right(pl->pl_prop); | |
2445 | ||
2446 | if (pl->pl_next == NULL && !right_justify) | |
2447 | (void) printf("%s", header); | |
2448 | else if (right_justify) | |
b8864a23 | 2449 | (void) printf("%*s", (int)pl->pl_width, header); |
34dc7c2f | 2450 | else |
b8864a23 | 2451 | (void) printf("%-*s", (int)pl->pl_width, header); |
34dc7c2f BB |
2452 | } |
2453 | ||
2454 | (void) printf("\n"); | |
2455 | } | |
2456 | ||
2457 | /* | |
2458 | * Given a pool and a list of properties, print out all the properties according | |
2459 | * to the described layout. | |
2460 | */ | |
2461 | static void | |
2462 | print_pool(zpool_handle_t *zhp, zprop_list_t *pl, int scripted) | |
2463 | { | |
2464 | boolean_t first = B_TRUE; | |
2465 | char property[ZPOOL_MAXPROPLEN]; | |
2466 | char *propstr; | |
2467 | boolean_t right_justify; | |
2468 | int width; | |
2469 | ||
2470 | for (; pl != NULL; pl = pl->pl_next) { | |
2471 | if (!first) { | |
2472 | if (scripted) | |
2473 | (void) printf("\t"); | |
2474 | else | |
2475 | (void) printf(" "); | |
2476 | } else { | |
2477 | first = B_FALSE; | |
2478 | } | |
2479 | ||
2480 | right_justify = B_FALSE; | |
2481 | if (pl->pl_prop != ZPROP_INVAL) { | |
2482 | if (zpool_get_prop(zhp, pl->pl_prop, property, | |
2483 | sizeof (property), NULL) != 0) | |
2484 | propstr = "-"; | |
2485 | else | |
2486 | propstr = property; | |
2487 | ||
2488 | right_justify = zpool_prop_align_right(pl->pl_prop); | |
2489 | } else { | |
2490 | propstr = "-"; | |
2491 | } | |
2492 | ||
2493 | width = pl->pl_width; | |
2494 | ||
2495 | /* | |
2496 | * If this is being called in scripted mode, or if this is the | |
2497 | * last column and it is left-justified, don't include a width | |
2498 | * format specifier. | |
2499 | */ | |
2500 | if (scripted || (pl->pl_next == NULL && !right_justify)) | |
2501 | (void) printf("%s", propstr); | |
2502 | else if (right_justify) | |
2503 | (void) printf("%*s", width, propstr); | |
2504 | else | |
2505 | (void) printf("%-*s", width, propstr); | |
2506 | } | |
2507 | ||
2508 | (void) printf("\n"); | |
2509 | } | |
2510 | ||
2511 | /* | |
2512 | * Generic callback function to list a pool. | |
2513 | */ | |
2514 | int | |
2515 | list_callback(zpool_handle_t *zhp, void *data) | |
2516 | { | |
2517 | list_cbdata_t *cbp = data; | |
2518 | ||
2519 | if (cbp->cb_first) { | |
2520 | if (!cbp->cb_scripted) | |
2521 | print_header(cbp->cb_proplist); | |
2522 | cbp->cb_first = B_FALSE; | |
2523 | } | |
2524 | ||
2525 | print_pool(zhp, cbp->cb_proplist, cbp->cb_scripted); | |
2526 | ||
2527 | return (0); | |
2528 | } | |
2529 | ||
2530 | /* | |
428870ff | 2531 | * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]] |
34dc7c2f BB |
2532 | * |
2533 | * -H Scripted mode. Don't display headers, and separate properties | |
2534 | * by a single tab. | |
2535 | * -o List of properties to display. Defaults to | |
428870ff BB |
2536 | * "name,size,allocated,free,capacity,health,altroot" |
2537 | * -T Display a timestamp in date(1) or Unix format | |
34dc7c2f BB |
2538 | * |
2539 | * List all pools in the system, whether or not they're healthy. Output space | |
2540 | * statistics for each one, as well as health status summary. | |
2541 | */ | |
2542 | int | |
2543 | zpool_do_list(int argc, char **argv) | |
2544 | { | |
2545 | int c; | |
2546 | int ret; | |
2547 | list_cbdata_t cb = { 0 }; | |
2548 | static char default_props[] = | |
428870ff | 2549 | "name,size,allocated,free,capacity,dedupratio,health,altroot"; |
34dc7c2f | 2550 | char *props = default_props; |
428870ff | 2551 | unsigned long interval = 0, count = 0; |
34dc7c2f BB |
2552 | |
2553 | /* check options */ | |
428870ff | 2554 | while ((c = getopt(argc, argv, ":Ho:T:")) != -1) { |
34dc7c2f BB |
2555 | switch (c) { |
2556 | case 'H': | |
2557 | cb.cb_scripted = B_TRUE; | |
2558 | break; | |
2559 | case 'o': | |
2560 | props = optarg; | |
2561 | break; | |
428870ff BB |
2562 | case 'T': |
2563 | get_timestamp_arg(*optarg); | |
2564 | break; | |
34dc7c2f BB |
2565 | case ':': |
2566 | (void) fprintf(stderr, gettext("missing argument for " | |
2567 | "'%c' option\n"), optopt); | |
2568 | usage(B_FALSE); | |
2569 | break; | |
2570 | case '?': | |
2571 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
2572 | optopt); | |
2573 | usage(B_FALSE); | |
2574 | } | |
2575 | } | |
2576 | ||
2577 | argc -= optind; | |
2578 | argv += optind; | |
2579 | ||
428870ff BB |
2580 | get_interval_count(&argc, argv, &interval, &count); |
2581 | ||
34dc7c2f BB |
2582 | if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0) |
2583 | usage(B_FALSE); | |
2584 | ||
2585 | cb.cb_first = B_TRUE; | |
2586 | ||
428870ff | 2587 | for (;;) { |
34dc7c2f | 2588 | |
428870ff BB |
2589 | if (timestamp_fmt != NODATE) |
2590 | print_timestamp(timestamp_fmt); | |
34dc7c2f | 2591 | |
428870ff BB |
2592 | ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist, |
2593 | list_callback, &cb); | |
2594 | ||
2595 | if (argc == 0 && cb.cb_first && !cb.cb_scripted) { | |
2596 | (void) printf(gettext("no pools available\n")); | |
2597 | zprop_free_list(cb.cb_proplist); | |
2598 | return (0); | |
2599 | } | |
2600 | ||
2601 | if (interval == 0) | |
2602 | break; | |
2603 | ||
2604 | if (count != 0 && --count == 0) | |
2605 | break; | |
2606 | ||
2607 | (void) sleep(interval); | |
34dc7c2f BB |
2608 | } |
2609 | ||
428870ff | 2610 | zprop_free_list(cb.cb_proplist); |
34dc7c2f BB |
2611 | return (ret); |
2612 | } | |
2613 | ||
34dc7c2f BB |
2614 | static int |
2615 | zpool_do_attach_or_replace(int argc, char **argv, int replacing) | |
2616 | { | |
2617 | boolean_t force = B_FALSE; | |
2618 | int c; | |
2619 | nvlist_t *nvroot; | |
2620 | char *poolname, *old_disk, *new_disk; | |
2621 | zpool_handle_t *zhp; | |
2622 | int ret; | |
2623 | ||
2624 | /* check options */ | |
2625 | while ((c = getopt(argc, argv, "f")) != -1) { | |
2626 | switch (c) { | |
2627 | case 'f': | |
2628 | force = B_TRUE; | |
2629 | break; | |
2630 | case '?': | |
2631 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
2632 | optopt); | |
2633 | usage(B_FALSE); | |
2634 | } | |
2635 | } | |
2636 | ||
2637 | argc -= optind; | |
2638 | argv += optind; | |
2639 | ||
2640 | /* get pool name and check number of arguments */ | |
2641 | if (argc < 1) { | |
2642 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
2643 | usage(B_FALSE); | |
2644 | } | |
2645 | ||
2646 | poolname = argv[0]; | |
2647 | ||
2648 | if (argc < 2) { | |
2649 | (void) fprintf(stderr, | |
2650 | gettext("missing <device> specification\n")); | |
2651 | usage(B_FALSE); | |
2652 | } | |
2653 | ||
2654 | old_disk = argv[1]; | |
2655 | ||
2656 | if (argc < 3) { | |
2657 | if (!replacing) { | |
2658 | (void) fprintf(stderr, | |
2659 | gettext("missing <new_device> specification\n")); | |
2660 | usage(B_FALSE); | |
2661 | } | |
2662 | new_disk = old_disk; | |
2663 | argc -= 1; | |
2664 | argv += 1; | |
2665 | } else { | |
2666 | new_disk = argv[2]; | |
2667 | argc -= 2; | |
2668 | argv += 2; | |
2669 | } | |
2670 | ||
2671 | if (argc > 1) { | |
2672 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
2673 | usage(B_FALSE); | |
2674 | } | |
2675 | ||
2676 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
2677 | return (1); | |
2678 | ||
2679 | if (zpool_get_config(zhp, NULL) == NULL) { | |
2680 | (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), | |
2681 | poolname); | |
2682 | zpool_close(zhp); | |
2683 | return (1); | |
2684 | } | |
2685 | ||
df30f566 | 2686 | nvroot = make_root_vdev(zhp, NULL, force, B_FALSE, replacing, B_FALSE, |
b128c09f | 2687 | argc, argv); |
34dc7c2f BB |
2688 | if (nvroot == NULL) { |
2689 | zpool_close(zhp); | |
2690 | return (1); | |
2691 | } | |
2692 | ||
2693 | ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing); | |
2694 | ||
2695 | nvlist_free(nvroot); | |
2696 | zpool_close(zhp); | |
2697 | ||
2698 | return (ret); | |
2699 | } | |
2700 | ||
2701 | /* | |
2702 | * zpool replace [-f] <pool> <device> <new_device> | |
2703 | * | |
2704 | * -f Force attach, even if <new_device> appears to be in use. | |
2705 | * | |
2706 | * Replace <device> with <new_device>. | |
2707 | */ | |
2708 | /* ARGSUSED */ | |
2709 | int | |
2710 | zpool_do_replace(int argc, char **argv) | |
2711 | { | |
2712 | return (zpool_do_attach_or_replace(argc, argv, B_TRUE)); | |
2713 | } | |
2714 | ||
2715 | /* | |
2716 | * zpool attach [-f] <pool> <device> <new_device> | |
2717 | * | |
2718 | * -f Force attach, even if <new_device> appears to be in use. | |
2719 | * | |
2720 | * Attach <new_device> to the mirror containing <device>. If <device> is not | |
2721 | * part of a mirror, then <device> will be transformed into a mirror of | |
2722 | * <device> and <new_device>. In either case, <new_device> will begin life | |
2723 | * with a DTL of [0, now], and will immediately begin to resilver itself. | |
2724 | */ | |
2725 | int | |
2726 | zpool_do_attach(int argc, char **argv) | |
2727 | { | |
2728 | return (zpool_do_attach_or_replace(argc, argv, B_FALSE)); | |
2729 | } | |
2730 | ||
2731 | /* | |
2732 | * zpool detach [-f] <pool> <device> | |
2733 | * | |
2734 | * -f Force detach of <device>, even if DTLs argue against it | |
2735 | * (not supported yet) | |
2736 | * | |
2737 | * Detach a device from a mirror. The operation will be refused if <device> | |
2738 | * is the last device in the mirror, or if the DTLs indicate that this device | |
2739 | * has the only valid copy of some data. | |
2740 | */ | |
2741 | /* ARGSUSED */ | |
2742 | int | |
2743 | zpool_do_detach(int argc, char **argv) | |
2744 | { | |
2745 | int c; | |
2746 | char *poolname, *path; | |
2747 | zpool_handle_t *zhp; | |
2748 | int ret; | |
2749 | ||
2750 | /* check options */ | |
2751 | while ((c = getopt(argc, argv, "f")) != -1) { | |
2752 | switch (c) { | |
2753 | case 'f': | |
2754 | case '?': | |
2755 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
2756 | optopt); | |
2757 | usage(B_FALSE); | |
2758 | } | |
2759 | } | |
2760 | ||
2761 | argc -= optind; | |
2762 | argv += optind; | |
2763 | ||
2764 | /* get pool name and check number of arguments */ | |
2765 | if (argc < 1) { | |
2766 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
2767 | usage(B_FALSE); | |
2768 | } | |
2769 | ||
2770 | if (argc < 2) { | |
2771 | (void) fprintf(stderr, | |
2772 | gettext("missing <device> specification\n")); | |
2773 | usage(B_FALSE); | |
2774 | } | |
2775 | ||
2776 | poolname = argv[0]; | |
2777 | path = argv[1]; | |
2778 | ||
2779 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
2780 | return (1); | |
2781 | ||
2782 | ret = zpool_vdev_detach(zhp, path); | |
2783 | ||
2784 | zpool_close(zhp); | |
2785 | ||
2786 | return (ret); | |
2787 | } | |
2788 | ||
428870ff BB |
2789 | /* |
2790 | * zpool split [-n] [-o prop=val] ... | |
2791 | * [-o mntopt] ... | |
2792 | * [-R altroot] <pool> <newpool> [<device> ...] | |
2793 | * | |
2794 | * -n Do not split the pool, but display the resulting layout if | |
2795 | * it were to be split. | |
2796 | * -o Set property=value, or set mount options. | |
2797 | * -R Mount the split-off pool under an alternate root. | |
2798 | * | |
2799 | * Splits the named pool and gives it the new pool name. Devices to be split | |
2800 | * off may be listed, provided that no more than one device is specified | |
2801 | * per top-level vdev mirror. The newly split pool is left in an exported | |
2802 | * state unless -R is specified. | |
2803 | * | |
2804 | * Restrictions: the top-level of the pool pool must only be made up of | |
2805 | * mirrors; all devices in the pool must be healthy; no device may be | |
2806 | * undergoing a resilvering operation. | |
2807 | */ | |
2808 | int | |
2809 | zpool_do_split(int argc, char **argv) | |
2810 | { | |
2811 | char *srcpool, *newpool, *propval; | |
2812 | char *mntopts = NULL; | |
2813 | splitflags_t flags; | |
2814 | int c, ret = 0; | |
2815 | zpool_handle_t *zhp; | |
2816 | nvlist_t *config, *props = NULL; | |
2817 | ||
2818 | flags.dryrun = B_FALSE; | |
2819 | flags.import = B_FALSE; | |
2820 | ||
2821 | /* check options */ | |
2822 | while ((c = getopt(argc, argv, ":R:no:")) != -1) { | |
2823 | switch (c) { | |
2824 | case 'R': | |
2825 | flags.import = B_TRUE; | |
2826 | if (add_prop_list( | |
2827 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg, | |
2828 | &props, B_TRUE) != 0) { | |
2829 | if (props) | |
2830 | nvlist_free(props); | |
2831 | usage(B_FALSE); | |
2832 | } | |
2833 | break; | |
2834 | case 'n': | |
2835 | flags.dryrun = B_TRUE; | |
2836 | break; | |
2837 | case 'o': | |
2838 | if ((propval = strchr(optarg, '=')) != NULL) { | |
2839 | *propval = '\0'; | |
2840 | propval++; | |
2841 | if (add_prop_list(optarg, propval, | |
2842 | &props, B_TRUE) != 0) { | |
2843 | if (props) | |
2844 | nvlist_free(props); | |
2845 | usage(B_FALSE); | |
2846 | } | |
2847 | } else { | |
2848 | mntopts = optarg; | |
2849 | } | |
2850 | break; | |
2851 | case ':': | |
2852 | (void) fprintf(stderr, gettext("missing argument for " | |
2853 | "'%c' option\n"), optopt); | |
2854 | usage(B_FALSE); | |
2855 | break; | |
2856 | case '?': | |
2857 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
2858 | optopt); | |
2859 | usage(B_FALSE); | |
2860 | break; | |
2861 | } | |
2862 | } | |
2863 | ||
2864 | if (!flags.import && mntopts != NULL) { | |
2865 | (void) fprintf(stderr, gettext("setting mntopts is only " | |
2866 | "valid when importing the pool\n")); | |
2867 | usage(B_FALSE); | |
2868 | } | |
2869 | ||
2870 | argc -= optind; | |
2871 | argv += optind; | |
2872 | ||
2873 | if (argc < 1) { | |
2874 | (void) fprintf(stderr, gettext("Missing pool name\n")); | |
2875 | usage(B_FALSE); | |
2876 | } | |
2877 | if (argc < 2) { | |
2878 | (void) fprintf(stderr, gettext("Missing new pool name\n")); | |
2879 | usage(B_FALSE); | |
2880 | } | |
2881 | ||
2882 | srcpool = argv[0]; | |
2883 | newpool = argv[1]; | |
2884 | ||
2885 | argc -= 2; | |
2886 | argv += 2; | |
2887 | ||
2888 | if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) | |
2889 | return (1); | |
2890 | ||
2891 | config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv); | |
2892 | if (config == NULL) { | |
2893 | ret = 1; | |
2894 | } else { | |
2895 | if (flags.dryrun) { | |
2896 | (void) printf(gettext("would create '%s' with the " | |
2897 | "following layout:\n\n"), newpool); | |
2898 | print_vdev_tree(NULL, newpool, config, 0, B_FALSE); | |
2899 | } | |
2900 | nvlist_free(config); | |
2901 | } | |
2902 | ||
2903 | zpool_close(zhp); | |
2904 | ||
2905 | if (ret != 0 || flags.dryrun || !flags.import) | |
2906 | return (ret); | |
2907 | ||
2908 | /* | |
2909 | * The split was successful. Now we need to open the new | |
2910 | * pool and import it. | |
2911 | */ | |
2912 | if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) | |
2913 | return (1); | |
2914 | if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && | |
2915 | zpool_enable_datasets(zhp, mntopts, 0) != 0) { | |
2916 | ret = 1; | |
2917 | (void) fprintf(stderr, gettext("Split was succssful, but " | |
2918 | "the datasets could not all be mounted\n")); | |
2919 | (void) fprintf(stderr, gettext("Try doing '%s' with a " | |
2920 | "different altroot\n"), "zpool import"); | |
2921 | } | |
2922 | zpool_close(zhp); | |
2923 | ||
2924 | return (ret); | |
2925 | } | |
2926 | ||
2927 | ||
2928 | ||
34dc7c2f BB |
2929 | /* |
2930 | * zpool online <pool> <device> ... | |
2931 | */ | |
2932 | int | |
2933 | zpool_do_online(int argc, char **argv) | |
2934 | { | |
2935 | int c, i; | |
2936 | char *poolname; | |
2937 | zpool_handle_t *zhp; | |
2938 | int ret = 0; | |
2939 | vdev_state_t newstate; | |
9babb374 | 2940 | int flags = 0; |
34dc7c2f BB |
2941 | |
2942 | /* check options */ | |
9babb374 | 2943 | while ((c = getopt(argc, argv, "et")) != -1) { |
34dc7c2f | 2944 | switch (c) { |
9babb374 BB |
2945 | case 'e': |
2946 | flags |= ZFS_ONLINE_EXPAND; | |
2947 | break; | |
34dc7c2f BB |
2948 | case 't': |
2949 | case '?': | |
2950 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
2951 | optopt); | |
2952 | usage(B_FALSE); | |
2953 | } | |
2954 | } | |
2955 | ||
2956 | argc -= optind; | |
2957 | argv += optind; | |
2958 | ||
2959 | /* get pool name and check number of arguments */ | |
2960 | if (argc < 1) { | |
2961 | (void) fprintf(stderr, gettext("missing pool name\n")); | |
2962 | usage(B_FALSE); | |
2963 | } | |
2964 | if (argc < 2) { | |
2965 | (void) fprintf(stderr, gettext("missing device name\n")); | |
2966 | usage(B_FALSE); | |
2967 | } | |
2968 | ||
2969 | poolname = argv[0]; | |
2970 | ||
2971 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
2972 | return (1); | |
2973 | ||
2974 | for (i = 1; i < argc; i++) { | |
9babb374 | 2975 | if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) { |
34dc7c2f BB |
2976 | if (newstate != VDEV_STATE_HEALTHY) { |
2977 | (void) printf(gettext("warning: device '%s' " | |
2978 | "onlined, but remains in faulted state\n"), | |
2979 | argv[i]); | |
2980 | if (newstate == VDEV_STATE_FAULTED) | |
2981 | (void) printf(gettext("use 'zpool " | |
2982 | "clear' to restore a faulted " | |
2983 | "device\n")); | |
2984 | else | |
2985 | (void) printf(gettext("use 'zpool " | |
2986 | "replace' to replace devices " | |
2987 | "that are no longer present\n")); | |
2988 | } | |
2989 | } else { | |
2990 | ret = 1; | |
2991 | } | |
2992 | } | |
2993 | ||
2994 | zpool_close(zhp); | |
2995 | ||
2996 | return (ret); | |
2997 | } | |
2998 | ||
2999 | /* | |
3000 | * zpool offline [-ft] <pool> <device> ... | |
3001 | * | |
3002 | * -f Force the device into the offline state, even if doing | |
3003 | * so would appear to compromise pool availability. | |
3004 | * (not supported yet) | |
3005 | * | |
3006 | * -t Only take the device off-line temporarily. The offline | |
3007 | * state will not be persistent across reboots. | |
3008 | */ | |
3009 | /* ARGSUSED */ | |
3010 | int | |
3011 | zpool_do_offline(int argc, char **argv) | |
3012 | { | |
3013 | int c, i; | |
3014 | char *poolname; | |
3015 | zpool_handle_t *zhp; | |
3016 | int ret = 0; | |
3017 | boolean_t istmp = B_FALSE; | |
3018 | ||
3019 | /* check options */ | |
3020 | while ((c = getopt(argc, argv, "ft")) != -1) { | |
3021 | switch (c) { | |
3022 | case 't': | |
3023 | istmp = B_TRUE; | |
3024 | break; | |
3025 | case 'f': | |
3026 | case '?': | |
3027 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
3028 | optopt); | |
3029 | usage(B_FALSE); | |
3030 | } | |
3031 | } | |
3032 | ||
3033 | argc -= optind; | |
3034 | argv += optind; | |
3035 | ||
3036 | /* get pool name and check number of arguments */ | |
3037 | if (argc < 1) { | |
3038 | (void) fprintf(stderr, gettext("missing pool name\n")); | |
3039 | usage(B_FALSE); | |
3040 | } | |
3041 | if (argc < 2) { | |
3042 | (void) fprintf(stderr, gettext("missing device name\n")); | |
3043 | usage(B_FALSE); | |
3044 | } | |
3045 | ||
3046 | poolname = argv[0]; | |
3047 | ||
3048 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
3049 | return (1); | |
3050 | ||
3051 | for (i = 1; i < argc; i++) { | |
3052 | if (zpool_vdev_offline(zhp, argv[i], istmp) != 0) | |
3053 | ret = 1; | |
3054 | } | |
3055 | ||
3056 | zpool_close(zhp); | |
3057 | ||
3058 | return (ret); | |
3059 | } | |
3060 | ||
3061 | /* | |
3062 | * zpool clear <pool> [device] | |
3063 | * | |
3064 | * Clear all errors associated with a pool or a particular device. | |
3065 | */ | |
3066 | int | |
3067 | zpool_do_clear(int argc, char **argv) | |
3068 | { | |
428870ff | 3069 | int c; |
34dc7c2f | 3070 | int ret = 0; |
428870ff BB |
3071 | boolean_t dryrun = B_FALSE; |
3072 | boolean_t do_rewind = B_FALSE; | |
3073 | boolean_t xtreme_rewind = B_FALSE; | |
3074 | uint32_t rewind_policy = ZPOOL_NO_REWIND; | |
3075 | nvlist_t *policy = NULL; | |
34dc7c2f BB |
3076 | zpool_handle_t *zhp; |
3077 | char *pool, *device; | |
3078 | ||
428870ff BB |
3079 | /* check options */ |
3080 | while ((c = getopt(argc, argv, "FnX")) != -1) { | |
3081 | switch (c) { | |
3082 | case 'F': | |
3083 | do_rewind = B_TRUE; | |
3084 | break; | |
3085 | case 'n': | |
3086 | dryrun = B_TRUE; | |
3087 | break; | |
3088 | case 'X': | |
3089 | xtreme_rewind = B_TRUE; | |
3090 | break; | |
3091 | case '?': | |
3092 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
3093 | optopt); | |
3094 | usage(B_FALSE); | |
3095 | } | |
3096 | } | |
3097 | ||
3098 | argc -= optind; | |
3099 | argv += optind; | |
3100 | ||
3101 | if (argc < 1) { | |
34dc7c2f BB |
3102 | (void) fprintf(stderr, gettext("missing pool name\n")); |
3103 | usage(B_FALSE); | |
3104 | } | |
3105 | ||
428870ff | 3106 | if (argc > 2) { |
34dc7c2f BB |
3107 | (void) fprintf(stderr, gettext("too many arguments\n")); |
3108 | usage(B_FALSE); | |
3109 | } | |
3110 | ||
428870ff BB |
3111 | if ((dryrun || xtreme_rewind) && !do_rewind) { |
3112 | (void) fprintf(stderr, | |
3113 | gettext("-n or -X only meaningful with -F\n")); | |
3114 | usage(B_FALSE); | |
3115 | } | |
3116 | if (dryrun) | |
3117 | rewind_policy = ZPOOL_TRY_REWIND; | |
3118 | else if (do_rewind) | |
3119 | rewind_policy = ZPOOL_DO_REWIND; | |
3120 | if (xtreme_rewind) | |
3121 | rewind_policy |= ZPOOL_EXTREME_REWIND; | |
3122 | ||
3123 | /* In future, further rewind policy choices can be passed along here */ | |
3124 | if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || | |
3125 | nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0) | |
3126 | return (1); | |
3127 | ||
3128 | pool = argv[0]; | |
3129 | device = argc == 2 ? argv[1] : NULL; | |
34dc7c2f | 3130 | |
428870ff BB |
3131 | if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { |
3132 | nvlist_free(policy); | |
34dc7c2f | 3133 | return (1); |
428870ff | 3134 | } |
34dc7c2f | 3135 | |
428870ff | 3136 | if (zpool_clear(zhp, device, policy) != 0) |
34dc7c2f BB |
3137 | ret = 1; |
3138 | ||
3139 | zpool_close(zhp); | |
3140 | ||
428870ff BB |
3141 | nvlist_free(policy); |
3142 | ||
34dc7c2f BB |
3143 | return (ret); |
3144 | } | |
3145 | ||
3146 | typedef struct scrub_cbdata { | |
3147 | int cb_type; | |
3148 | int cb_argc; | |
3149 | char **cb_argv; | |
3150 | } scrub_cbdata_t; | |
3151 | ||
3152 | int | |
3153 | scrub_callback(zpool_handle_t *zhp, void *data) | |
3154 | { | |
3155 | scrub_cbdata_t *cb = data; | |
3156 | int err; | |
3157 | ||
3158 | /* | |
3159 | * Ignore faulted pools. | |
3160 | */ | |
3161 | if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { | |
3162 | (void) fprintf(stderr, gettext("cannot scrub '%s': pool is " | |
3163 | "currently unavailable\n"), zpool_get_name(zhp)); | |
3164 | return (1); | |
3165 | } | |
3166 | ||
428870ff | 3167 | err = zpool_scan(zhp, cb->cb_type); |
34dc7c2f BB |
3168 | |
3169 | return (err != 0); | |
3170 | } | |
3171 | ||
3172 | /* | |
3173 | * zpool scrub [-s] <pool> ... | |
3174 | * | |
3175 | * -s Stop. Stops any in-progress scrub. | |
3176 | */ | |
3177 | int | |
3178 | zpool_do_scrub(int argc, char **argv) | |
3179 | { | |
3180 | int c; | |
3181 | scrub_cbdata_t cb; | |
3182 | ||
428870ff | 3183 | cb.cb_type = POOL_SCAN_SCRUB; |
34dc7c2f BB |
3184 | |
3185 | /* check options */ | |
3186 | while ((c = getopt(argc, argv, "s")) != -1) { | |
3187 | switch (c) { | |
3188 | case 's': | |
428870ff | 3189 | cb.cb_type = POOL_SCAN_NONE; |
34dc7c2f BB |
3190 | break; |
3191 | case '?': | |
3192 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
3193 | optopt); | |
3194 | usage(B_FALSE); | |
3195 | } | |
3196 | } | |
3197 | ||
3198 | cb.cb_argc = argc; | |
3199 | cb.cb_argv = argv; | |
3200 | argc -= optind; | |
3201 | argv += optind; | |
3202 | ||
3203 | if (argc < 1) { | |
3204 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
3205 | usage(B_FALSE); | |
3206 | } | |
3207 | ||
3208 | return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb)); | |
3209 | } | |
3210 | ||
3211 | typedef struct status_cbdata { | |
3212 | int cb_count; | |
3213 | boolean_t cb_allpools; | |
3214 | boolean_t cb_verbose; | |
3215 | boolean_t cb_explain; | |
3216 | boolean_t cb_first; | |
428870ff | 3217 | boolean_t cb_dedup_stats; |
34dc7c2f BB |
3218 | } status_cbdata_t; |
3219 | ||
3220 | /* | |
3221 | * Print out detailed scrub status. | |
3222 | */ | |
3223 | void | |
428870ff | 3224 | print_scan_status(pool_scan_stat_t *ps) |
34dc7c2f | 3225 | { |
428870ff | 3226 | time_t start, end; |
572e2857 | 3227 | uint64_t elapsed, mins_left, hours_left; |
428870ff BB |
3228 | uint64_t pass_exam, examined, total; |
3229 | uint_t rate; | |
34dc7c2f | 3230 | double fraction_done; |
428870ff | 3231 | char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7]; |
34dc7c2f | 3232 | |
428870ff | 3233 | (void) printf(gettext(" scan: ")); |
34dc7c2f | 3234 | |
428870ff BB |
3235 | /* If there's never been a scan, there's not much to say. */ |
3236 | if (ps == NULL || ps->pss_func == POOL_SCAN_NONE || | |
3237 | ps->pss_func >= POOL_SCAN_FUNCS) { | |
34dc7c2f BB |
3238 | (void) printf(gettext("none requested\n")); |
3239 | return; | |
3240 | } | |
3241 | ||
428870ff BB |
3242 | start = ps->pss_start_time; |
3243 | end = ps->pss_end_time; | |
3244 | zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf)); | |
34dc7c2f | 3245 | |
428870ff BB |
3246 | assert(ps->pss_func == POOL_SCAN_SCRUB || |
3247 | ps->pss_func == POOL_SCAN_RESILVER); | |
3248 | /* | |
3249 | * Scan is finished or canceled. | |
3250 | */ | |
3251 | if (ps->pss_state == DSS_FINISHED) { | |
3252 | uint64_t minutes_taken = (end - start) / 60; | |
d4ed6673 | 3253 | char *fmt = NULL; |
428870ff BB |
3254 | |
3255 | if (ps->pss_func == POOL_SCAN_SCRUB) { | |
3256 | fmt = gettext("scrub repaired %s in %lluh%um with " | |
3257 | "%llu errors on %s"); | |
3258 | } else if (ps->pss_func == POOL_SCAN_RESILVER) { | |
3259 | fmt = gettext("resilvered %s in %lluh%um with " | |
3260 | "%llu errors on %s"); | |
3261 | } | |
3262 | /* LINTED */ | |
3263 | (void) printf(fmt, processed_buf, | |
34dc7c2f BB |
3264 | (u_longlong_t)(minutes_taken / 60), |
3265 | (uint_t)(minutes_taken % 60), | |
428870ff BB |
3266 | (u_longlong_t)ps->pss_errors, |
3267 | ctime((time_t *)&end)); | |
3268 | return; | |
3269 | } else if (ps->pss_state == DSS_CANCELED) { | |
3270 | if (ps->pss_func == POOL_SCAN_SCRUB) { | |
3271 | (void) printf(gettext("scrub canceled on %s"), | |
3272 | ctime(&end)); | |
3273 | } else if (ps->pss_func == POOL_SCAN_RESILVER) { | |
3274 | (void) printf(gettext("resilver canceled on %s"), | |
3275 | ctime(&end)); | |
3276 | } | |
34dc7c2f BB |
3277 | return; |
3278 | } | |
3279 | ||
428870ff BB |
3280 | assert(ps->pss_state == DSS_SCANNING); |
3281 | ||
3282 | /* | |
3283 | * Scan is in progress. | |
3284 | */ | |
3285 | if (ps->pss_func == POOL_SCAN_SCRUB) { | |
3286 | (void) printf(gettext("scrub in progress since %s"), | |
3287 | ctime(&start)); | |
3288 | } else if (ps->pss_func == POOL_SCAN_RESILVER) { | |
3289 | (void) printf(gettext("resilver in progress since %s"), | |
3290 | ctime(&start)); | |
3291 | } | |
34dc7c2f | 3292 | |
428870ff BB |
3293 | examined = ps->pss_examined ? ps->pss_examined : 1; |
3294 | total = ps->pss_to_examine; | |
34dc7c2f | 3295 | fraction_done = (double)examined / total; |
428870ff BB |
3296 | |
3297 | /* elapsed time for this pass */ | |
3298 | elapsed = time(NULL) - ps->pss_pass_start; | |
3299 | elapsed = elapsed ? elapsed : 1; | |
3300 | pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1; | |
3301 | rate = pass_exam / elapsed; | |
3302 | rate = rate ? rate : 1; | |
3303 | mins_left = ((total - examined) / rate) / 60; | |
572e2857 | 3304 | hours_left = mins_left / 60; |
428870ff BB |
3305 | |
3306 | zfs_nicenum(examined, examined_buf, sizeof (examined_buf)); | |
3307 | zfs_nicenum(total, total_buf, sizeof (total_buf)); | |
3308 | zfs_nicenum(rate, rate_buf, sizeof (rate_buf)); | |
3309 | ||
572e2857 BB |
3310 | /* |
3311 | * do not print estimated time if hours_left is more than 30 days | |
3312 | */ | |
3313 | (void) printf(gettext(" %s scanned out of %s at %s/s"), | |
3314 | examined_buf, total_buf, rate_buf); | |
3315 | if (hours_left < (30 * 24)) { | |
3316 | (void) printf(gettext(", %lluh%um to go\n"), | |
3317 | (u_longlong_t)hours_left, (uint_t)(mins_left % 60)); | |
3318 | } else { | |
3319 | (void) printf(gettext( | |
3320 | ", (scan is slow, no estimated time)\n")); | |
3321 | } | |
428870ff BB |
3322 | |
3323 | if (ps->pss_func == POOL_SCAN_RESILVER) { | |
3324 | (void) printf(gettext(" %s resilvered, %.2f%% done\n"), | |
3325 | processed_buf, 100 * fraction_done); | |
3326 | } else if (ps->pss_func == POOL_SCAN_SCRUB) { | |
3327 | (void) printf(gettext(" %s repaired, %.2f%% done\n"), | |
3328 | processed_buf, 100 * fraction_done); | |
3329 | } | |
34dc7c2f BB |
3330 | } |
3331 | ||
34dc7c2f BB |
3332 | static void |
3333 | print_error_log(zpool_handle_t *zhp) | |
3334 | { | |
3335 | nvlist_t *nverrlist = NULL; | |
3336 | nvpair_t *elem; | |
3337 | char *pathname; | |
3338 | size_t len = MAXPATHLEN * 2; | |
3339 | ||
3340 | if (zpool_get_errlog(zhp, &nverrlist) != 0) { | |
3341 | (void) printf("errors: List of errors unavailable " | |
3342 | "(insufficient privileges)\n"); | |
3343 | return; | |
3344 | } | |
3345 | ||
3346 | (void) printf("errors: Permanent errors have been " | |
3347 | "detected in the following files:\n\n"); | |
3348 | ||
3349 | pathname = safe_malloc(len); | |
3350 | elem = NULL; | |
3351 | while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) { | |
3352 | nvlist_t *nv; | |
3353 | uint64_t dsobj, obj; | |
3354 | ||
3355 | verify(nvpair_value_nvlist(elem, &nv) == 0); | |
3356 | verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET, | |
3357 | &dsobj) == 0); | |
3358 | verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT, | |
3359 | &obj) == 0); | |
3360 | zpool_obj_to_path(zhp, dsobj, obj, pathname, len); | |
3361 | (void) printf("%7s %s\n", "", pathname); | |
3362 | } | |
3363 | free(pathname); | |
3364 | nvlist_free(nverrlist); | |
3365 | } | |
3366 | ||
3367 | static void | |
3368 | print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares, | |
3369 | int namewidth) | |
3370 | { | |
3371 | uint_t i; | |
3372 | char *name; | |
3373 | ||
3374 | if (nspares == 0) | |
3375 | return; | |
3376 | ||
3377 | (void) printf(gettext("\tspares\n")); | |
3378 | ||
3379 | for (i = 0; i < nspares; i++) { | |
428870ff | 3380 | name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE); |
34dc7c2f | 3381 | print_status_config(zhp, name, spares[i], |
9babb374 | 3382 | namewidth, 2, B_TRUE); |
34dc7c2f BB |
3383 | free(name); |
3384 | } | |
3385 | } | |
3386 | ||
3387 | static void | |
3388 | print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache, | |
3389 | int namewidth) | |
3390 | { | |
3391 | uint_t i; | |
3392 | char *name; | |
3393 | ||
3394 | if (nl2cache == 0) | |
3395 | return; | |
3396 | ||
3397 | (void) printf(gettext("\tcache\n")); | |
3398 | ||
3399 | for (i = 0; i < nl2cache; i++) { | |
428870ff | 3400 | name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE); |
34dc7c2f | 3401 | print_status_config(zhp, name, l2cache[i], |
9babb374 | 3402 | namewidth, 2, B_FALSE); |
34dc7c2f BB |
3403 | free(name); |
3404 | } | |
3405 | } | |
3406 | ||
428870ff BB |
3407 | static void |
3408 | print_dedup_stats(nvlist_t *config) | |
3409 | { | |
3410 | ddt_histogram_t *ddh; | |
3411 | ddt_stat_t *dds; | |
3412 | ddt_object_t *ddo; | |
3413 | uint_t c; | |
3414 | ||
3415 | /* | |
3416 | * If the pool was faulted then we may not have been able to | |
3417 | * obtain the config. Otherwise, if have anything in the dedup | |
3418 | * table continue processing the stats. | |
3419 | */ | |
3420 | if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS, | |
3421 | (uint64_t **)&ddo, &c) != 0 || ddo->ddo_count == 0) | |
3422 | return; | |
3423 | ||
3424 | (void) printf("\n"); | |
3425 | (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n", | |
3426 | (u_longlong_t)ddo->ddo_count, | |
3427 | (u_longlong_t)ddo->ddo_dspace, | |
3428 | (u_longlong_t)ddo->ddo_mspace); | |
3429 | ||
3430 | verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS, | |
3431 | (uint64_t **)&dds, &c) == 0); | |
3432 | verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM, | |
3433 | (uint64_t **)&ddh, &c) == 0); | |
3434 | zpool_dump_ddt(dds, ddh); | |
3435 | } | |
3436 | ||
34dc7c2f BB |
3437 | /* |
3438 | * Display a summary of pool status. Displays a summary such as: | |
3439 | * | |
3440 | * pool: tank | |
3441 | * status: DEGRADED | |
3442 | * reason: One or more devices ... | |
3443 | * see: http://www.sun.com/msg/ZFS-xxxx-01 | |
3444 | * config: | |
3445 | * mirror DEGRADED | |
3446 | * c1t0d0 OK | |
3447 | * c2t0d0 UNAVAIL | |
3448 | * | |
3449 | * When given the '-v' option, we print out the complete config. If the '-e' | |
3450 | * option is specified, then we print out error rate information as well. | |
3451 | */ | |
3452 | int | |
3453 | status_callback(zpool_handle_t *zhp, void *data) | |
3454 | { | |
3455 | status_cbdata_t *cbp = data; | |
3456 | nvlist_t *config, *nvroot; | |
3457 | char *msgid; | |
3458 | int reason; | |
3459 | const char *health; | |
3460 | uint_t c; | |
3461 | vdev_stat_t *vs; | |
3462 | ||
3463 | config = zpool_get_config(zhp, NULL); | |
3464 | reason = zpool_get_status(zhp, &msgid); | |
3465 | ||
3466 | cbp->cb_count++; | |
3467 | ||
3468 | /* | |
3469 | * If we were given 'zpool status -x', only report those pools with | |
3470 | * problems. | |
3471 | */ | |
3472 | if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) { | |
3473 | if (!cbp->cb_allpools) { | |
3474 | (void) printf(gettext("pool '%s' is healthy\n"), | |
3475 | zpool_get_name(zhp)); | |
3476 | if (cbp->cb_first) | |
3477 | cbp->cb_first = B_FALSE; | |
3478 | } | |
3479 | return (0); | |
3480 | } | |
3481 | ||
3482 | if (cbp->cb_first) | |
3483 | cbp->cb_first = B_FALSE; | |
3484 | else | |
3485 | (void) printf("\n"); | |
3486 | ||
3487 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
3488 | &nvroot) == 0); | |
428870ff | 3489 | verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
3490 | (uint64_t **)&vs, &c) == 0); |
3491 | health = zpool_state_to_name(vs->vs_state, vs->vs_aux); | |
3492 | ||
3493 | (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp)); | |
3494 | (void) printf(gettext(" state: %s\n"), health); | |
3495 | ||
3496 | switch (reason) { | |
3497 | case ZPOOL_STATUS_MISSING_DEV_R: | |
3498 | (void) printf(gettext("status: One or more devices could not " | |
3499 | "be opened. Sufficient replicas exist for\n\tthe pool to " | |
3500 | "continue functioning in a degraded state.\n")); | |
3501 | (void) printf(gettext("action: Attach the missing device and " | |
3502 | "online it using 'zpool online'.\n")); | |
3503 | break; | |
3504 | ||
3505 | case ZPOOL_STATUS_MISSING_DEV_NR: | |
3506 | (void) printf(gettext("status: One or more devices could not " | |
3507 | "be opened. There are insufficient\n\treplicas for the " | |
3508 | "pool to continue functioning.\n")); | |
3509 | (void) printf(gettext("action: Attach the missing device and " | |
3510 | "online it using 'zpool online'.\n")); | |
3511 | break; | |
3512 | ||
3513 | case ZPOOL_STATUS_CORRUPT_LABEL_R: | |
3514 | (void) printf(gettext("status: One or more devices could not " | |
3515 | "be used because the label is missing or\n\tinvalid. " | |
3516 | "Sufficient replicas exist for the pool to continue\n\t" | |
3517 | "functioning in a degraded state.\n")); | |
3518 | (void) printf(gettext("action: Replace the device using " | |
3519 | "'zpool replace'.\n")); | |
3520 | break; | |
3521 | ||
3522 | case ZPOOL_STATUS_CORRUPT_LABEL_NR: | |
3523 | (void) printf(gettext("status: One or more devices could not " | |
3524 | "be used because the label is missing \n\tor invalid. " | |
3525 | "There are insufficient replicas for the pool to " | |
3526 | "continue\n\tfunctioning.\n")); | |
428870ff BB |
3527 | zpool_explain_recover(zpool_get_handle(zhp), |
3528 | zpool_get_name(zhp), reason, config); | |
34dc7c2f BB |
3529 | break; |
3530 | ||
3531 | case ZPOOL_STATUS_FAILING_DEV: | |
3532 | (void) printf(gettext("status: One or more devices has " | |
3533 | "experienced an unrecoverable error. An\n\tattempt was " | |
3534 | "made to correct the error. Applications are " | |
3535 | "unaffected.\n")); | |
3536 | (void) printf(gettext("action: Determine if the device needs " | |
3537 | "to be replaced, and clear the errors\n\tusing " | |
3538 | "'zpool clear' or replace the device with 'zpool " | |
3539 | "replace'.\n")); | |
3540 | break; | |
3541 | ||
3542 | case ZPOOL_STATUS_OFFLINE_DEV: | |
3543 | (void) printf(gettext("status: One or more devices has " | |
3544 | "been taken offline by the administrator.\n\tSufficient " | |
3545 | "replicas exist for the pool to continue functioning in " | |
3546 | "a\n\tdegraded state.\n")); | |
3547 | (void) printf(gettext("action: Online the device using " | |
3548 | "'zpool online' or replace the device with\n\t'zpool " | |
3549 | "replace'.\n")); | |
3550 | break; | |
3551 | ||
45d1cae3 BB |
3552 | case ZPOOL_STATUS_REMOVED_DEV: |
3553 | (void) printf(gettext("status: One or more devices has " | |
3554 | "been removed by the administrator.\n\tSufficient " | |
3555 | "replicas exist for the pool to continue functioning in " | |
3556 | "a\n\tdegraded state.\n")); | |
3557 | (void) printf(gettext("action: Online the device using " | |
3558 | "'zpool online' or replace the device with\n\t'zpool " | |
3559 | "replace'.\n")); | |
3560 | break; | |
3561 | ||
34dc7c2f BB |
3562 | case ZPOOL_STATUS_RESILVERING: |
3563 | (void) printf(gettext("status: One or more devices is " | |
3564 | "currently being resilvered. The pool will\n\tcontinue " | |
3565 | "to function, possibly in a degraded state.\n")); | |
3566 | (void) printf(gettext("action: Wait for the resilver to " | |
3567 | "complete.\n")); | |
3568 | break; | |
3569 | ||
3570 | case ZPOOL_STATUS_CORRUPT_DATA: | |
3571 | (void) printf(gettext("status: One or more devices has " | |
3572 | "experienced an error resulting in data\n\tcorruption. " | |
3573 | "Applications may be affected.\n")); | |
3574 | (void) printf(gettext("action: Restore the file in question " | |
3575 | "if possible. Otherwise restore the\n\tentire pool from " | |
3576 | "backup.\n")); | |
3577 | break; | |
3578 | ||
3579 | case ZPOOL_STATUS_CORRUPT_POOL: | |
3580 | (void) printf(gettext("status: The pool metadata is corrupted " | |
3581 | "and the pool cannot be opened.\n")); | |
428870ff BB |
3582 | zpool_explain_recover(zpool_get_handle(zhp), |
3583 | zpool_get_name(zhp), reason, config); | |
34dc7c2f BB |
3584 | break; |
3585 | ||
3586 | case ZPOOL_STATUS_VERSION_OLDER: | |
3587 | (void) printf(gettext("status: The pool is formatted using an " | |
3588 | "older on-disk format. The pool can\n\tstill be used, but " | |
3589 | "some features are unavailable.\n")); | |
3590 | (void) printf(gettext("action: Upgrade the pool using 'zpool " | |
3591 | "upgrade'. Once this is done, the\n\tpool will no longer " | |
3592 | "be accessible on older software versions.\n")); | |
3593 | break; | |
3594 | ||
3595 | case ZPOOL_STATUS_VERSION_NEWER: | |
3596 | (void) printf(gettext("status: The pool has been upgraded to a " | |
3597 | "newer, incompatible on-disk version.\n\tThe pool cannot " | |
3598 | "be accessed on this system.\n")); | |
3599 | (void) printf(gettext("action: Access the pool from a system " | |
3600 | "running more recent software, or\n\trestore the pool from " | |
3601 | "backup.\n")); | |
3602 | break; | |
3603 | ||
3604 | case ZPOOL_STATUS_FAULTED_DEV_R: | |
3605 | (void) printf(gettext("status: One or more devices are " | |
3606 | "faulted in response to persistent errors.\n\tSufficient " | |
3607 | "replicas exist for the pool to continue functioning " | |
3608 | "in a\n\tdegraded state.\n")); | |
3609 | (void) printf(gettext("action: Replace the faulted device, " | |
3610 | "or use 'zpool clear' to mark the device\n\trepaired.\n")); | |
3611 | break; | |
3612 | ||
3613 | case ZPOOL_STATUS_FAULTED_DEV_NR: | |
3614 | (void) printf(gettext("status: One or more devices are " | |
3615 | "faulted in response to persistent errors. There are " | |
3616 | "insufficient replicas for the pool to\n\tcontinue " | |
3617 | "functioning.\n")); | |
3618 | (void) printf(gettext("action: Destroy and re-create the pool " | |
3619 | "from a backup source. Manually marking the device\n" | |
3620 | "\trepaired using 'zpool clear' may allow some data " | |
3621 | "to be recovered.\n")); | |
3622 | break; | |
3623 | ||
b128c09f BB |
3624 | case ZPOOL_STATUS_IO_FAILURE_WAIT: |
3625 | case ZPOOL_STATUS_IO_FAILURE_CONTINUE: | |
3626 | (void) printf(gettext("status: One or more devices are " | |
3627 | "faulted in response to IO failures.\n")); | |
3628 | (void) printf(gettext("action: Make sure the affected devices " | |
3629 | "are connected, then run 'zpool clear'.\n")); | |
3630 | break; | |
3631 | ||
3632 | case ZPOOL_STATUS_BAD_LOG: | |
3633 | (void) printf(gettext("status: An intent log record " | |
3634 | "could not be read.\n" | |
3635 | "\tWaiting for adminstrator intervention to fix the " | |
3636 | "faulted pool.\n")); | |
3637 | (void) printf(gettext("action: Either restore the affected " | |
3638 | "device(s) and run 'zpool online',\n" | |
3639 | "\tor ignore the intent log records by running " | |
3640 | "'zpool clear'.\n")); | |
3641 | break; | |
3642 | ||
34dc7c2f BB |
3643 | default: |
3644 | /* | |
3645 | * The remaining errors can't actually be generated, yet. | |
3646 | */ | |
3647 | assert(reason == ZPOOL_STATUS_OK); | |
3648 | } | |
3649 | ||
3650 | if (msgid != NULL) | |
3651 | (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"), | |
3652 | msgid); | |
3653 | ||
3654 | if (config != NULL) { | |
3655 | int namewidth; | |
3656 | uint64_t nerr; | |
3657 | nvlist_t **spares, **l2cache; | |
3658 | uint_t nspares, nl2cache; | |
428870ff | 3659 | pool_scan_stat_t *ps = NULL; |
34dc7c2f | 3660 | |
428870ff BB |
3661 | (void) nvlist_lookup_uint64_array(nvroot, |
3662 | ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c); | |
3663 | print_scan_status(ps); | |
34dc7c2f BB |
3664 | |
3665 | namewidth = max_width(zhp, nvroot, 0, 0); | |
3666 | if (namewidth < 10) | |
3667 | namewidth = 10; | |
3668 | ||
3669 | (void) printf(gettext("config:\n\n")); | |
3670 | (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth, | |
3671 | "NAME", "STATE", "READ", "WRITE", "CKSUM"); | |
3672 | print_status_config(zhp, zpool_get_name(zhp), nvroot, | |
9babb374 | 3673 | namewidth, 0, B_FALSE); |
34dc7c2f | 3674 | |
9babb374 BB |
3675 | if (num_logs(nvroot) > 0) |
3676 | print_logs(zhp, nvroot, namewidth, B_TRUE); | |
34dc7c2f BB |
3677 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, |
3678 | &l2cache, &nl2cache) == 0) | |
3679 | print_l2cache(zhp, l2cache, nl2cache, namewidth); | |
3680 | ||
3681 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
3682 | &spares, &nspares) == 0) | |
3683 | print_spares(zhp, spares, nspares, namewidth); | |
3684 | ||
3685 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT, | |
3686 | &nerr) == 0) { | |
3687 | nvlist_t *nverrlist = NULL; | |
3688 | ||
3689 | /* | |
3690 | * If the approximate error count is small, get a | |
3691 | * precise count by fetching the entire log and | |
3692 | * uniquifying the results. | |
3693 | */ | |
3694 | if (nerr > 0 && nerr < 100 && !cbp->cb_verbose && | |
3695 | zpool_get_errlog(zhp, &nverrlist) == 0) { | |
3696 | nvpair_t *elem; | |
3697 | ||
3698 | elem = NULL; | |
3699 | nerr = 0; | |
3700 | while ((elem = nvlist_next_nvpair(nverrlist, | |
3701 | elem)) != NULL) { | |
3702 | nerr++; | |
3703 | } | |
3704 | } | |
3705 | nvlist_free(nverrlist); | |
3706 | ||
3707 | (void) printf("\n"); | |
3708 | ||
3709 | if (nerr == 0) | |
3710 | (void) printf(gettext("errors: No known data " | |
3711 | "errors\n")); | |
3712 | else if (!cbp->cb_verbose) | |
3713 | (void) printf(gettext("errors: %llu data " | |
3714 | "errors, use '-v' for a list\n"), | |
3715 | (u_longlong_t)nerr); | |
3716 | else | |
3717 | print_error_log(zhp); | |
3718 | } | |
428870ff BB |
3719 | |
3720 | if (cbp->cb_dedup_stats) | |
3721 | print_dedup_stats(config); | |
34dc7c2f BB |
3722 | } else { |
3723 | (void) printf(gettext("config: The configuration cannot be " | |
3724 | "determined.\n")); | |
3725 | } | |
3726 | ||
3727 | return (0); | |
3728 | } | |
3729 | ||
3730 | /* | |
428870ff | 3731 | * zpool status [-vx] [-T d|u] [pool] ... [interval [count]] |
34dc7c2f BB |
3732 | * |
3733 | * -v Display complete error logs | |
3734 | * -x Display only pools with potential problems | |
428870ff BB |
3735 | * -D Display dedup status (undocumented) |
3736 | * -T Display a timestamp in date(1) or Unix format | |
34dc7c2f BB |
3737 | * |
3738 | * Describes the health status of all pools or some subset. | |
3739 | */ | |
3740 | int | |
3741 | zpool_do_status(int argc, char **argv) | |
3742 | { | |
3743 | int c; | |
3744 | int ret; | |
428870ff | 3745 | unsigned long interval = 0, count = 0; |
34dc7c2f BB |
3746 | status_cbdata_t cb = { 0 }; |
3747 | ||
3748 | /* check options */ | |
428870ff | 3749 | while ((c = getopt(argc, argv, "vxDT:")) != -1) { |
34dc7c2f BB |
3750 | switch (c) { |
3751 | case 'v': | |
3752 | cb.cb_verbose = B_TRUE; | |
3753 | break; | |
3754 | case 'x': | |
3755 | cb.cb_explain = B_TRUE; | |
3756 | break; | |
428870ff BB |
3757 | case 'D': |
3758 | cb.cb_dedup_stats = B_TRUE; | |
3759 | break; | |
3760 | case 'T': | |
3761 | get_timestamp_arg(*optarg); | |
3762 | break; | |
34dc7c2f BB |
3763 | case '?': |
3764 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
3765 | optopt); | |
3766 | usage(B_FALSE); | |
3767 | } | |
3768 | } | |
3769 | ||
3770 | argc -= optind; | |
3771 | argv += optind; | |
3772 | ||
428870ff | 3773 | get_interval_count(&argc, argv, &interval, &count); |
34dc7c2f BB |
3774 | |
3775 | if (argc == 0) | |
3776 | cb.cb_allpools = B_TRUE; | |
3777 | ||
428870ff | 3778 | cb.cb_first = B_TRUE; |
34dc7c2f | 3779 | |
428870ff BB |
3780 | for (;;) { |
3781 | if (timestamp_fmt != NODATE) | |
3782 | print_timestamp(timestamp_fmt); | |
34dc7c2f | 3783 | |
428870ff BB |
3784 | ret = for_each_pool(argc, argv, B_TRUE, NULL, |
3785 | status_callback, &cb); | |
3786 | ||
3787 | if (argc == 0 && cb.cb_count == 0) | |
3788 | (void) printf(gettext("no pools available\n")); | |
3789 | else if (cb.cb_explain && cb.cb_first && cb.cb_allpools) | |
3790 | (void) printf(gettext("all pools are healthy\n")); | |
3791 | ||
3792 | if (ret != 0) | |
3793 | return (ret); | |
3794 | ||
3795 | if (interval == 0) | |
3796 | break; | |
3797 | ||
3798 | if (count != 0 && --count == 0) | |
3799 | break; | |
3800 | ||
3801 | (void) sleep(interval); | |
3802 | } | |
3803 | ||
3804 | return (0); | |
34dc7c2f BB |
3805 | } |
3806 | ||
3807 | typedef struct upgrade_cbdata { | |
3808 | int cb_all; | |
3809 | int cb_first; | |
3810 | int cb_newer; | |
3811 | int cb_argc; | |
3812 | uint64_t cb_version; | |
3813 | char **cb_argv; | |
3814 | } upgrade_cbdata_t; | |
3815 | ||
3816 | static int | |
3817 | upgrade_cb(zpool_handle_t *zhp, void *arg) | |
3818 | { | |
3819 | upgrade_cbdata_t *cbp = arg; | |
3820 | nvlist_t *config; | |
3821 | uint64_t version; | |
3822 | int ret = 0; | |
3823 | ||
3824 | config = zpool_get_config(zhp, NULL); | |
3825 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
3826 | &version) == 0); | |
3827 | ||
3828 | if (!cbp->cb_newer && version < SPA_VERSION) { | |
3829 | if (!cbp->cb_all) { | |
3830 | if (cbp->cb_first) { | |
3831 | (void) printf(gettext("The following pools are " | |
3832 | "out of date, and can be upgraded. After " | |
3833 | "being\nupgraded, these pools will no " | |
3834 | "longer be accessible by older software " | |
3835 | "versions.\n\n")); | |
3836 | (void) printf(gettext("VER POOL\n")); | |
3837 | (void) printf(gettext("--- ------------\n")); | |
3838 | cbp->cb_first = B_FALSE; | |
3839 | } | |
3840 | ||
3841 | (void) printf("%2llu %s\n", (u_longlong_t)version, | |
3842 | zpool_get_name(zhp)); | |
3843 | } else { | |
3844 | cbp->cb_first = B_FALSE; | |
3845 | ret = zpool_upgrade(zhp, cbp->cb_version); | |
3846 | if (!ret) { | |
3847 | (void) printf(gettext("Successfully upgraded " | |
3848 | "'%s'\n\n"), zpool_get_name(zhp)); | |
3849 | } | |
3850 | } | |
3851 | } else if (cbp->cb_newer && version > SPA_VERSION) { | |
3852 | assert(!cbp->cb_all); | |
3853 | ||
3854 | if (cbp->cb_first) { | |
3855 | (void) printf(gettext("The following pools are " | |
3856 | "formatted using a newer software version and\n" | |
3857 | "cannot be accessed on the current system.\n\n")); | |
3858 | (void) printf(gettext("VER POOL\n")); | |
3859 | (void) printf(gettext("--- ------------\n")); | |
3860 | cbp->cb_first = B_FALSE; | |
3861 | } | |
3862 | ||
3863 | (void) printf("%2llu %s\n", (u_longlong_t)version, | |
3864 | zpool_get_name(zhp)); | |
3865 | } | |
3866 | ||
3867 | zpool_close(zhp); | |
3868 | return (ret); | |
3869 | } | |
3870 | ||
3871 | /* ARGSUSED */ | |
3872 | static int | |
3873 | upgrade_one(zpool_handle_t *zhp, void *data) | |
3874 | { | |
3875 | upgrade_cbdata_t *cbp = data; | |
3876 | uint64_t cur_version; | |
3877 | int ret; | |
3878 | ||
3879 | if (strcmp("log", zpool_get_name(zhp)) == 0) { | |
3880 | (void) printf(gettext("'log' is now a reserved word\n" | |
3881 | "Pool 'log' must be renamed using export and import" | |
3882 | " to upgrade.\n")); | |
3883 | return (1); | |
3884 | } | |
3885 | ||
3886 | cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); | |
3887 | if (cur_version > cbp->cb_version) { | |
3888 | (void) printf(gettext("Pool '%s' is already formatted " | |
3889 | "using more current version '%llu'.\n"), | |
b8864a23 | 3890 | zpool_get_name(zhp), (u_longlong_t) cur_version); |
34dc7c2f BB |
3891 | return (0); |
3892 | } | |
3893 | if (cur_version == cbp->cb_version) { | |
3894 | (void) printf(gettext("Pool '%s' is already formatted " | |
3895 | "using the current version.\n"), zpool_get_name(zhp)); | |
3896 | return (0); | |
3897 | } | |
3898 | ||
3899 | ret = zpool_upgrade(zhp, cbp->cb_version); | |
3900 | ||
3901 | if (!ret) { | |
3902 | (void) printf(gettext("Successfully upgraded '%s' " | |
3903 | "from version %llu to version %llu\n\n"), | |
3904 | zpool_get_name(zhp), (u_longlong_t)cur_version, | |
3905 | (u_longlong_t)cbp->cb_version); | |
3906 | } | |
3907 | ||
3908 | return (ret != 0); | |
3909 | } | |
3910 | ||
3911 | /* | |
3912 | * zpool upgrade | |
3913 | * zpool upgrade -v | |
3914 | * zpool upgrade [-V version] <-a | pool ...> | |
3915 | * | |
3916 | * With no arguments, display downrev'd ZFS pool available for upgrade. | |
3917 | * Individual pools can be upgraded by specifying the pool, and '-a' will | |
3918 | * upgrade all pools. | |
3919 | */ | |
3920 | int | |
3921 | zpool_do_upgrade(int argc, char **argv) | |
3922 | { | |
3923 | int c; | |
3924 | upgrade_cbdata_t cb = { 0 }; | |
3925 | int ret = 0; | |
3926 | boolean_t showversions = B_FALSE; | |
3927 | char *end; | |
3928 | ||
3929 | ||
3930 | /* check options */ | |
9babb374 | 3931 | while ((c = getopt(argc, argv, ":avV:")) != -1) { |
34dc7c2f BB |
3932 | switch (c) { |
3933 | case 'a': | |
3934 | cb.cb_all = B_TRUE; | |
3935 | break; | |
3936 | case 'v': | |
3937 | showversions = B_TRUE; | |
3938 | break; | |
3939 | case 'V': | |
3940 | cb.cb_version = strtoll(optarg, &end, 10); | |
3941 | if (*end != '\0' || cb.cb_version > SPA_VERSION || | |
3942 | cb.cb_version < SPA_VERSION_1) { | |
3943 | (void) fprintf(stderr, | |
3944 | gettext("invalid version '%s'\n"), optarg); | |
3945 | usage(B_FALSE); | |
3946 | } | |
3947 | break; | |
9babb374 BB |
3948 | case ':': |
3949 | (void) fprintf(stderr, gettext("missing argument for " | |
3950 | "'%c' option\n"), optopt); | |
3951 | usage(B_FALSE); | |
3952 | break; | |
34dc7c2f BB |
3953 | case '?': |
3954 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
3955 | optopt); | |
3956 | usage(B_FALSE); | |
3957 | } | |
3958 | } | |
3959 | ||
3960 | cb.cb_argc = argc; | |
3961 | cb.cb_argv = argv; | |
3962 | argc -= optind; | |
3963 | argv += optind; | |
3964 | ||
3965 | if (cb.cb_version == 0) { | |
3966 | cb.cb_version = SPA_VERSION; | |
3967 | } else if (!cb.cb_all && argc == 0) { | |
3968 | (void) fprintf(stderr, gettext("-V option is " | |
3969 | "incompatible with other arguments\n")); | |
3970 | usage(B_FALSE); | |
3971 | } | |
3972 | ||
3973 | if (showversions) { | |
3974 | if (cb.cb_all || argc != 0) { | |
3975 | (void) fprintf(stderr, gettext("-v option is " | |
3976 | "incompatible with other arguments\n")); | |
3977 | usage(B_FALSE); | |
3978 | } | |
3979 | } else if (cb.cb_all) { | |
3980 | if (argc != 0) { | |
3981 | (void) fprintf(stderr, gettext("-a option should not " | |
3982 | "be used along with a pool name\n")); | |
3983 | usage(B_FALSE); | |
3984 | } | |
3985 | } | |
3986 | ||
3987 | (void) printf(gettext("This system is currently running " | |
3988 | "ZFS pool version %llu.\n\n"), SPA_VERSION); | |
3989 | cb.cb_first = B_TRUE; | |
3990 | if (showversions) { | |
3991 | (void) printf(gettext("The following versions are " | |
3992 | "supported:\n\n")); | |
3993 | (void) printf(gettext("VER DESCRIPTION\n")); | |
3994 | (void) printf("--- -----------------------------------------" | |
3995 | "---------------\n"); | |
3996 | (void) printf(gettext(" 1 Initial ZFS version\n")); | |
3997 | (void) printf(gettext(" 2 Ditto blocks " | |
3998 | "(replicated metadata)\n")); | |
3999 | (void) printf(gettext(" 3 Hot spares and double parity " | |
4000 | "RAID-Z\n")); | |
4001 | (void) printf(gettext(" 4 zpool history\n")); | |
4002 | (void) printf(gettext(" 5 Compression using the gzip " | |
4003 | "algorithm\n")); | |
4004 | (void) printf(gettext(" 6 bootfs pool property\n")); | |
4005 | (void) printf(gettext(" 7 Separate intent log devices\n")); | |
4006 | (void) printf(gettext(" 8 Delegated administration\n")); | |
4007 | (void) printf(gettext(" 9 refquota and refreservation " | |
4008 | "properties\n")); | |
4009 | (void) printf(gettext(" 10 Cache devices\n")); | |
b128c09f BB |
4010 | (void) printf(gettext(" 11 Improved scrub performance\n")); |
4011 | (void) printf(gettext(" 12 Snapshot properties\n")); | |
4012 | (void) printf(gettext(" 13 snapused property\n")); | |
9babb374 BB |
4013 | (void) printf(gettext(" 14 passthrough-x aclinherit\n")); |
4014 | (void) printf(gettext(" 15 user/group space accounting\n")); | |
4015 | (void) printf(gettext(" 16 stmf property support\n")); | |
45d1cae3 | 4016 | (void) printf(gettext(" 17 Triple-parity RAID-Z\n")); |
428870ff BB |
4017 | (void) printf(gettext(" 18 Snapshot user holds\n")); |
4018 | (void) printf(gettext(" 19 Log device removal\n")); | |
4019 | (void) printf(gettext(" 20 Compression using zle " | |
4020 | "(zero-length encoding)\n")); | |
4021 | (void) printf(gettext(" 21 Deduplication\n")); | |
4022 | (void) printf(gettext(" 22 Received properties\n")); | |
4023 | (void) printf(gettext(" 23 Slim ZIL\n")); | |
4024 | (void) printf(gettext(" 24 System attributes\n")); | |
4025 | (void) printf(gettext(" 25 Improved scrub stats\n")); | |
4026 | (void) printf(gettext(" 26 Improved snapshot deletion " | |
4027 | "performance\n")); | |
572e2857 BB |
4028 | (void) printf(gettext(" 27 Improved snapshot creation " |
4029 | "performance\n")); | |
4030 | (void) printf(gettext(" 28 Multiple vdev replacements\n")); | |
428870ff BB |
4031 | (void) printf(gettext("\nFor more information on a particular " |
4032 | "version, including supported releases,\n")); | |
4033 | (void) printf(gettext("see the ZFS Administration Guide.\n\n")); | |
34dc7c2f BB |
4034 | } else if (argc == 0) { |
4035 | int notfound; | |
4036 | ||
4037 | ret = zpool_iter(g_zfs, upgrade_cb, &cb); | |
4038 | notfound = cb.cb_first; | |
4039 | ||
4040 | if (!cb.cb_all && ret == 0) { | |
4041 | if (!cb.cb_first) | |
4042 | (void) printf("\n"); | |
4043 | cb.cb_first = B_TRUE; | |
4044 | cb.cb_newer = B_TRUE; | |
4045 | ret = zpool_iter(g_zfs, upgrade_cb, &cb); | |
4046 | if (!cb.cb_first) { | |
4047 | notfound = B_FALSE; | |
4048 | (void) printf("\n"); | |
4049 | } | |
4050 | } | |
4051 | ||
4052 | if (ret == 0) { | |
4053 | if (notfound) | |
4054 | (void) printf(gettext("All pools are formatted " | |
4055 | "using this version.\n")); | |
4056 | else if (!cb.cb_all) | |
4057 | (void) printf(gettext("Use 'zpool upgrade -v' " | |
4058 | "for a list of available versions and " | |
4059 | "their associated\nfeatures.\n")); | |
4060 | } | |
4061 | } else { | |
4062 | ret = for_each_pool(argc, argv, B_FALSE, NULL, | |
4063 | upgrade_one, &cb); | |
4064 | } | |
4065 | ||
4066 | return (ret); | |
4067 | } | |
4068 | ||
4069 | typedef struct hist_cbdata { | |
4070 | boolean_t first; | |
4071 | int longfmt; | |
4072 | int internal; | |
4073 | } hist_cbdata_t; | |
4074 | ||
34dc7c2f BB |
4075 | /* |
4076 | * Print out the command history for a specific pool. | |
4077 | */ | |
4078 | static int | |
4079 | get_history_one(zpool_handle_t *zhp, void *data) | |
4080 | { | |
4081 | nvlist_t *nvhis; | |
4082 | nvlist_t **records; | |
4083 | uint_t numrecords; | |
4084 | char *cmdstr; | |
4085 | char *pathstr; | |
4086 | uint64_t dst_time; | |
4087 | time_t tsec; | |
4088 | struct tm t; | |
4089 | char tbuf[30]; | |
4090 | int ret, i; | |
4091 | uint64_t who; | |
4092 | struct passwd *pwd; | |
4093 | char *hostname; | |
4094 | char *zonename; | |
4095 | char internalstr[MAXPATHLEN]; | |
4096 | hist_cbdata_t *cb = (hist_cbdata_t *)data; | |
4097 | uint64_t txg; | |
4098 | uint64_t ievent; | |
4099 | ||
4100 | cb->first = B_FALSE; | |
4101 | ||
4102 | (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp)); | |
4103 | ||
4104 | if ((ret = zpool_get_history(zhp, &nvhis)) != 0) | |
4105 | return (ret); | |
4106 | ||
4107 | verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD, | |
4108 | &records, &numrecords) == 0); | |
4109 | for (i = 0; i < numrecords; i++) { | |
4110 | if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME, | |
4111 | &dst_time) != 0) | |
4112 | continue; | |
4113 | ||
4114 | /* is it an internal event or a standard event? */ | |
4115 | if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD, | |
4116 | &cmdstr) != 0) { | |
4117 | if (cb->internal == 0) | |
4118 | continue; | |
4119 | ||
4120 | if (nvlist_lookup_uint64(records[i], | |
4121 | ZPOOL_HIST_INT_EVENT, &ievent) != 0) | |
4122 | continue; | |
4123 | verify(nvlist_lookup_uint64(records[i], | |
4124 | ZPOOL_HIST_TXG, &txg) == 0); | |
4125 | verify(nvlist_lookup_string(records[i], | |
4126 | ZPOOL_HIST_INT_STR, &pathstr) == 0); | |
b128c09f | 4127 | if (ievent >= LOG_END) |
34dc7c2f BB |
4128 | continue; |
4129 | (void) snprintf(internalstr, | |
4130 | sizeof (internalstr), | |
b8864a23 BB |
4131 | "[internal %s txg:%llu] %s", |
4132 | zfs_history_event_names[ievent], (u_longlong_t)txg, | |
34dc7c2f BB |
4133 | pathstr); |
4134 | cmdstr = internalstr; | |
4135 | } | |
4136 | tsec = dst_time; | |
4137 | (void) localtime_r(&tsec, &t); | |
4138 | (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); | |
4139 | (void) printf("%s %s", tbuf, cmdstr); | |
4140 | ||
4141 | if (!cb->longfmt) { | |
4142 | (void) printf("\n"); | |
4143 | continue; | |
4144 | } | |
4145 | (void) printf(" ["); | |
4146 | if (nvlist_lookup_uint64(records[i], | |
4147 | ZPOOL_HIST_WHO, &who) == 0) { | |
4148 | pwd = getpwuid((uid_t)who); | |
4149 | if (pwd) | |
4150 | (void) printf("user %s on", | |
4151 | pwd->pw_name); | |
4152 | else | |
4153 | (void) printf("user %d on", | |
4154 | (int)who); | |
4155 | } else { | |
4156 | (void) printf(gettext("no info]\n")); | |
4157 | continue; | |
4158 | } | |
4159 | if (nvlist_lookup_string(records[i], | |
4160 | ZPOOL_HIST_HOST, &hostname) == 0) { | |
4161 | (void) printf(" %s", hostname); | |
4162 | } | |
4163 | if (nvlist_lookup_string(records[i], | |
4164 | ZPOOL_HIST_ZONE, &zonename) == 0) { | |
4165 | (void) printf(":%s", zonename); | |
4166 | } | |
4167 | ||
4168 | (void) printf("]"); | |
4169 | (void) printf("\n"); | |
4170 | } | |
4171 | (void) printf("\n"); | |
4172 | nvlist_free(nvhis); | |
4173 | ||
4174 | return (ret); | |
4175 | } | |
4176 | ||
4177 | /* | |
4178 | * zpool history <pool> | |
4179 | * | |
4180 | * Displays the history of commands that modified pools. | |
4181 | */ | |
4182 | ||
4183 | ||
4184 | int | |
4185 | zpool_do_history(int argc, char **argv) | |
4186 | { | |
4187 | hist_cbdata_t cbdata = { 0 }; | |
4188 | int ret; | |
4189 | int c; | |
4190 | ||
4191 | cbdata.first = B_TRUE; | |
4192 | /* check options */ | |
4193 | while ((c = getopt(argc, argv, "li")) != -1) { | |
4194 | switch (c) { | |
4195 | case 'l': | |
4196 | cbdata.longfmt = 1; | |
4197 | break; | |
4198 | case 'i': | |
4199 | cbdata.internal = 1; | |
4200 | break; | |
4201 | case '?': | |
4202 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
4203 | optopt); | |
4204 | usage(B_FALSE); | |
4205 | } | |
4206 | } | |
4207 | argc -= optind; | |
4208 | argv += optind; | |
4209 | ||
4210 | ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one, | |
4211 | &cbdata); | |
4212 | ||
4213 | if (argc == 0 && cbdata.first == B_TRUE) { | |
4214 | (void) printf(gettext("no pools available\n")); | |
4215 | return (0); | |
4216 | } | |
4217 | ||
4218 | return (ret); | |
4219 | } | |
4220 | ||
26685276 BB |
4221 | typedef struct ev_opts { |
4222 | int verbose; | |
c5343ba7 | 4223 | int scripted; |
26685276 BB |
4224 | int follow; |
4225 | int clear; | |
4226 | } ev_opts_t; | |
4227 | ||
4228 | static void | |
4229 | zpool_do_events_short(nvlist_t *nvl) | |
4230 | { | |
4231 | char ctime_str[26], str[32], *ptr; | |
4232 | int64_t *tv; | |
4233 | uint_t n; | |
4234 | ||
4235 | verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0); | |
4236 | memset(str, ' ', 32); | |
4237 | (void) ctime_r((const time_t *)&tv[0], ctime_str); | |
4238 | (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */ | |
4239 | (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */ | |
4240 | (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */ | |
4241 | (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]);/* '.123456789' */ | |
4242 | (void) printf(gettext("%s "), str); | |
4243 | ||
4244 | verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0); | |
4245 | (void) printf(gettext("%s\n"), ptr); | |
4246 | } | |
4247 | ||
4248 | static void | |
4249 | zpool_do_events_nvprint(nvlist_t *nvl, int depth) | |
4250 | { | |
4251 | nvpair_t *nvp; | |
4252 | ||
4253 | for (nvp = nvlist_next_nvpair(nvl, NULL); | |
4254 | nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) { | |
4255 | ||
4256 | data_type_t type = nvpair_type(nvp); | |
4257 | const char *name = nvpair_name(nvp); | |
4258 | ||
4259 | boolean_t b; | |
4260 | uint8_t i8; | |
4261 | uint16_t i16; | |
4262 | uint32_t i32; | |
4263 | uint64_t i64; | |
4264 | char *str; | |
4265 | nvlist_t *cnv; | |
4266 | ||
4267 | printf(gettext("%*s%s = "), depth, "", name); | |
4268 | ||
4269 | switch (type) { | |
4270 | case DATA_TYPE_BOOLEAN: | |
4271 | printf(gettext("%s"), "1"); | |
4272 | break; | |
4273 | ||
4274 | case DATA_TYPE_BOOLEAN_VALUE: | |
4275 | (void) nvpair_value_boolean_value(nvp, &b); | |
4276 | printf(gettext("%s"), b ? "1" : "0"); | |
4277 | break; | |
4278 | ||
4279 | case DATA_TYPE_BYTE: | |
4280 | (void) nvpair_value_byte(nvp, &i8); | |
4281 | printf(gettext("0x%x"), i8); | |
4282 | break; | |
4283 | ||
4284 | case DATA_TYPE_INT8: | |
4285 | (void) nvpair_value_int8(nvp, (void *)&i8); | |
4286 | printf(gettext("0x%x"), i8); | |
4287 | break; | |
4288 | ||
4289 | case DATA_TYPE_UINT8: | |
4290 | (void) nvpair_value_uint8(nvp, &i8); | |
4291 | printf(gettext("0x%x"), i8); | |
4292 | break; | |
4293 | ||
4294 | case DATA_TYPE_INT16: | |
4295 | (void) nvpair_value_int16(nvp, (void *)&i16); | |
4296 | printf(gettext("0x%x"), i16); | |
4297 | break; | |
4298 | ||
4299 | case DATA_TYPE_UINT16: | |
4300 | (void) nvpair_value_uint16(nvp, &i16); | |
4301 | printf(gettext("0x%x"), i16); | |
4302 | break; | |
4303 | ||
4304 | case DATA_TYPE_INT32: | |
4305 | (void) nvpair_value_int32(nvp, (void *)&i32); | |
4306 | printf(gettext("0x%x"), i32); | |
4307 | break; | |
4308 | ||
4309 | case DATA_TYPE_UINT32: | |
4310 | (void) nvpair_value_uint32(nvp, &i32); | |
4311 | printf(gettext("0x%x"), i32); | |
4312 | break; | |
4313 | ||
4314 | case DATA_TYPE_INT64: | |
4315 | (void) nvpair_value_int64(nvp, (void *)&i64); | |
4316 | printf(gettext("0x%llx"), (u_longlong_t)i64); | |
4317 | break; | |
4318 | ||
4319 | case DATA_TYPE_UINT64: | |
4320 | (void) nvpair_value_uint64(nvp, &i64); | |
4321 | printf(gettext("0x%llx"), (u_longlong_t)i64); | |
4322 | break; | |
4323 | ||
4324 | case DATA_TYPE_HRTIME: | |
4325 | (void) nvpair_value_hrtime(nvp, (void *)&i64); | |
4326 | printf(gettext("0x%llx"), (u_longlong_t)i64); | |
4327 | break; | |
4328 | ||
4329 | case DATA_TYPE_STRING: | |
4330 | (void) nvpair_value_string(nvp, &str); | |
4331 | printf(gettext("\"%s\""), str ? str : "<NULL>"); | |
4332 | break; | |
4333 | ||
4334 | case DATA_TYPE_NVLIST: | |
4335 | printf(gettext("(embedded nvlist)\n")); | |
4336 | (void) nvpair_value_nvlist(nvp, &cnv); | |
4337 | zpool_do_events_nvprint(cnv, depth + 8); | |
c5343ba7 | 4338 | printf(gettext("%*s(end %s)"), depth, "", name); |
26685276 BB |
4339 | break; |
4340 | ||
4341 | case DATA_TYPE_NVLIST_ARRAY: { | |
4342 | nvlist_t **val; | |
4343 | uint_t i, nelem; | |
4344 | ||
4345 | (void) nvpair_value_nvlist_array(nvp, &val, &nelem); | |
4346 | printf(gettext("(%d embedded nvlists)\n"), nelem); | |
4347 | for (i = 0; i < nelem; i++) { | |
4348 | printf(gettext("%*s%s[%d] = %s\n"), | |
4349 | depth, "", name, i, "(embedded nvlist)"); | |
4350 | zpool_do_events_nvprint(val[i], depth + 8); | |
4351 | printf(gettext("%*s(end %s[%i])\n"), | |
4352 | depth, "", name, i); | |
4353 | } | |
4354 | printf(gettext("%*s(end %s)\n"), depth, "", name); | |
4355 | } | |
4356 | break; | |
4357 | ||
4358 | case DATA_TYPE_INT8_ARRAY: { | |
4359 | int8_t *val; | |
4360 | uint_t i, nelem; | |
4361 | ||
4362 | (void) nvpair_value_int8_array(nvp, &val, &nelem); | |
4363 | for (i = 0; i < nelem; i++) | |
4364 | printf(gettext("0x%x "), val[i]); | |
4365 | ||
4366 | break; | |
4367 | } | |
4368 | ||
4369 | case DATA_TYPE_UINT8_ARRAY: { | |
4370 | uint8_t *val; | |
4371 | uint_t i, nelem; | |
4372 | ||
4373 | (void) nvpair_value_uint8_array(nvp, &val, &nelem); | |
4374 | for (i = 0; i < nelem; i++) | |
4375 | printf(gettext("0x%x "), val[i]); | |
4376 | ||
4377 | break; | |
4378 | } | |
4379 | ||
4380 | case DATA_TYPE_INT16_ARRAY: { | |
4381 | int16_t *val; | |
4382 | uint_t i, nelem; | |
4383 | ||
4384 | (void) nvpair_value_int16_array(nvp, &val, &nelem); | |
4385 | for (i = 0; i < nelem; i++) | |
4386 | printf(gettext("0x%x "), val[i]); | |
4387 | ||
4388 | break; | |
4389 | } | |
4390 | ||
4391 | case DATA_TYPE_UINT16_ARRAY: { | |
4392 | uint16_t *val; | |
4393 | uint_t i, nelem; | |
4394 | ||
4395 | (void) nvpair_value_uint16_array(nvp, &val, &nelem); | |
4396 | for (i = 0; i < nelem; i++) | |
4397 | printf(gettext("0x%x "), val[i]); | |
4398 | ||
4399 | break; | |
4400 | } | |
4401 | ||
4402 | case DATA_TYPE_INT32_ARRAY: { | |
4403 | int32_t *val; | |
4404 | uint_t i, nelem; | |
4405 | ||
4406 | (void) nvpair_value_int32_array(nvp, &val, &nelem); | |
4407 | for (i = 0; i < nelem; i++) | |
4408 | printf(gettext("0x%x "), val[i]); | |
4409 | ||
4410 | break; | |
4411 | } | |
4412 | ||
4413 | case DATA_TYPE_UINT32_ARRAY: { | |
4414 | uint32_t *val; | |
4415 | uint_t i, nelem; | |
4416 | ||
4417 | (void) nvpair_value_uint32_array(nvp, &val, &nelem); | |
4418 | for (i = 0; i < nelem; i++) | |
4419 | printf(gettext("0x%x "), val[i]); | |
4420 | ||
4421 | break; | |
4422 | } | |
4423 | ||
4424 | case DATA_TYPE_INT64_ARRAY: { | |
4425 | int64_t *val; | |
4426 | uint_t i, nelem; | |
4427 | ||
4428 | (void) nvpair_value_int64_array(nvp, &val, &nelem); | |
4429 | for (i = 0; i < nelem; i++) | |
4430 | printf(gettext("0x%llx "), (u_longlong_t)val[i]); | |
4431 | ||
4432 | break; | |
4433 | } | |
4434 | ||
4435 | case DATA_TYPE_UINT64_ARRAY: { | |
4436 | uint64_t *val; | |
4437 | uint_t i, nelem; | |
4438 | ||
4439 | (void) nvpair_value_uint64_array(nvp, &val, &nelem); | |
4440 | for (i = 0; i < nelem; i++) | |
4441 | printf(gettext("0x%llx "), (u_longlong_t)val[i]); | |
4442 | ||
4443 | break; | |
4444 | } | |
4445 | ||
4446 | case DATA_TYPE_STRING_ARRAY: | |
4447 | case DATA_TYPE_BOOLEAN_ARRAY: | |
4448 | case DATA_TYPE_BYTE_ARRAY: | |
4449 | case DATA_TYPE_DOUBLE: | |
4450 | case DATA_TYPE_UNKNOWN: | |
4451 | printf(gettext("<unknown>")); | |
4452 | break; | |
4453 | } | |
4454 | ||
4455 | printf(gettext("\n")); | |
4456 | } | |
4457 | } | |
4458 | ||
4459 | static int | |
4460 | zpool_do_events_next(ev_opts_t *opts) | |
4461 | { | |
4462 | nvlist_t *nvl; | |
4463 | int cleanup_fd, ret, dropped; | |
4464 | ||
4465 | cleanup_fd = open(ZFS_DEV, O_RDWR); | |
4466 | VERIFY(cleanup_fd >= 0); | |
4467 | ||
c5343ba7 BB |
4468 | if (!opts->scripted) |
4469 | (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS"); | |
26685276 BB |
4470 | |
4471 | while (1) { | |
4472 | ret = zpool_events_next(g_zfs, &nvl, &dropped, | |
4473 | !!opts->follow, cleanup_fd); | |
4474 | if (ret || nvl == NULL) | |
4475 | break; | |
4476 | ||
4477 | if (dropped > 0) | |
4478 | (void) printf(gettext("dropped %d events\n"), dropped); | |
4479 | ||
4480 | zpool_do_events_short(nvl); | |
4481 | ||
4482 | if (opts->verbose) { | |
4483 | zpool_do_events_nvprint(nvl, 8); | |
4484 | printf(gettext("\n")); | |
4485 | } | |
4486 | ||
4487 | nvlist_free(nvl); | |
4488 | } | |
4489 | ||
4490 | VERIFY(0 == close(cleanup_fd)); | |
4491 | ||
4492 | return (ret); | |
4493 | } | |
4494 | ||
4495 | static int | |
4496 | zpool_do_events_clear(ev_opts_t *opts) | |
4497 | { | |
4498 | int count, ret; | |
4499 | ||
4500 | ret = zpool_events_clear(g_zfs, &count); | |
4501 | if (!ret) | |
4502 | (void) printf(gettext("cleared %d events\n"), count); | |
4503 | ||
4504 | return (ret); | |
4505 | } | |
4506 | ||
4507 | /* | |
4508 | * zpool events [-vfc] | |
4509 | * | |
4510 | * Displays events logs by ZFS. | |
4511 | */ | |
4512 | int | |
4513 | zpool_do_events(int argc, char **argv) | |
4514 | { | |
4515 | ev_opts_t opts = { 0 }; | |
4516 | int ret; | |
4517 | int c; | |
4518 | ||
4519 | /* check options */ | |
c5343ba7 | 4520 | while ((c = getopt(argc, argv, "vHfc")) != -1) { |
26685276 BB |
4521 | switch (c) { |
4522 | case 'v': | |
4523 | opts.verbose = 1; | |
c5343ba7 BB |
4524 | break; |
4525 | case 'H': | |
4526 | opts.scripted = 1; | |
26685276 BB |
4527 | break; |
4528 | case 'f': | |
4529 | opts.follow = 1; | |
4530 | break; | |
4531 | case 'c': | |
4532 | opts.clear = 1; | |
4533 | break; | |
4534 | case '?': | |
4535 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
4536 | optopt); | |
4537 | usage(B_FALSE); | |
4538 | } | |
4539 | } | |
4540 | argc -= optind; | |
4541 | argv += optind; | |
4542 | ||
4543 | if (opts.clear) | |
4544 | ret = zpool_do_events_clear(&opts); | |
4545 | else | |
4546 | ret = zpool_do_events_next(&opts); | |
4547 | ||
4548 | return ret; | |
4549 | } | |
4550 | ||
34dc7c2f BB |
4551 | static int |
4552 | get_callback(zpool_handle_t *zhp, void *data) | |
4553 | { | |
4554 | zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; | |
4555 | char value[MAXNAMELEN]; | |
4556 | zprop_source_t srctype; | |
4557 | zprop_list_t *pl; | |
4558 | ||
4559 | for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { | |
4560 | ||
4561 | /* | |
4562 | * Skip the special fake placeholder. This will also skip | |
4563 | * over the name property when 'all' is specified. | |
4564 | */ | |
4565 | if (pl->pl_prop == ZPOOL_PROP_NAME && | |
4566 | pl == cbp->cb_proplist) | |
4567 | continue; | |
4568 | ||
4569 | if (zpool_get_prop(zhp, pl->pl_prop, | |
4570 | value, sizeof (value), &srctype) != 0) | |
4571 | continue; | |
4572 | ||
4573 | zprop_print_one_property(zpool_get_name(zhp), cbp, | |
428870ff BB |
4574 | zpool_prop_to_name(pl->pl_prop), value, srctype, NULL, |
4575 | NULL); | |
34dc7c2f BB |
4576 | } |
4577 | return (0); | |
4578 | } | |
4579 | ||
4580 | int | |
4581 | zpool_do_get(int argc, char **argv) | |
4582 | { | |
4583 | zprop_get_cbdata_t cb = { 0 }; | |
4584 | zprop_list_t fake_name = { 0 }; | |
4585 | int ret; | |
4586 | ||
4587 | if (argc < 3) | |
4588 | usage(B_FALSE); | |
4589 | ||
4590 | cb.cb_first = B_TRUE; | |
4591 | cb.cb_sources = ZPROP_SRC_ALL; | |
4592 | cb.cb_columns[0] = GET_COL_NAME; | |
4593 | cb.cb_columns[1] = GET_COL_PROPERTY; | |
4594 | cb.cb_columns[2] = GET_COL_VALUE; | |
4595 | cb.cb_columns[3] = GET_COL_SOURCE; | |
4596 | cb.cb_type = ZFS_TYPE_POOL; | |
4597 | ||
4598 | if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist, | |
4599 | ZFS_TYPE_POOL) != 0) | |
4600 | usage(B_FALSE); | |
4601 | ||
4602 | if (cb.cb_proplist != NULL) { | |
4603 | fake_name.pl_prop = ZPOOL_PROP_NAME; | |
4604 | fake_name.pl_width = strlen(gettext("NAME")); | |
4605 | fake_name.pl_next = cb.cb_proplist; | |
4606 | cb.cb_proplist = &fake_name; | |
4607 | } | |
4608 | ||
4609 | ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist, | |
4610 | get_callback, &cb); | |
4611 | ||
4612 | if (cb.cb_proplist == &fake_name) | |
4613 | zprop_free_list(fake_name.pl_next); | |
4614 | else | |
4615 | zprop_free_list(cb.cb_proplist); | |
4616 | ||
4617 | return (ret); | |
4618 | } | |
4619 | ||
4620 | typedef struct set_cbdata { | |
4621 | char *cb_propname; | |
4622 | char *cb_value; | |
4623 | boolean_t cb_any_successful; | |
4624 | } set_cbdata_t; | |
4625 | ||
4626 | int | |
4627 | set_callback(zpool_handle_t *zhp, void *data) | |
4628 | { | |
4629 | int error; | |
4630 | set_cbdata_t *cb = (set_cbdata_t *)data; | |
4631 | ||
4632 | error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value); | |
4633 | ||
4634 | if (!error) | |
4635 | cb->cb_any_successful = B_TRUE; | |
4636 | ||
4637 | return (error); | |
4638 | } | |
4639 | ||
4640 | int | |
4641 | zpool_do_set(int argc, char **argv) | |
4642 | { | |
4643 | set_cbdata_t cb = { 0 }; | |
4644 | int error; | |
4645 | ||
4646 | if (argc > 1 && argv[1][0] == '-') { | |
4647 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
4648 | argv[1][1]); | |
4649 | usage(B_FALSE); | |
4650 | } | |
4651 | ||
4652 | if (argc < 2) { | |
4653 | (void) fprintf(stderr, gettext("missing property=value " | |
4654 | "argument\n")); | |
4655 | usage(B_FALSE); | |
4656 | } | |
4657 | ||
4658 | if (argc < 3) { | |
4659 | (void) fprintf(stderr, gettext("missing pool name\n")); | |
4660 | usage(B_FALSE); | |
4661 | } | |
4662 | ||
4663 | if (argc > 3) { | |
4664 | (void) fprintf(stderr, gettext("too many pool names\n")); | |
4665 | usage(B_FALSE); | |
4666 | } | |
4667 | ||
4668 | cb.cb_propname = argv[1]; | |
4669 | cb.cb_value = strchr(cb.cb_propname, '='); | |
4670 | if (cb.cb_value == NULL) { | |
4671 | (void) fprintf(stderr, gettext("missing value in " | |
4672 | "property=value argument\n")); | |
4673 | usage(B_FALSE); | |
4674 | } | |
4675 | ||
4676 | *(cb.cb_value) = '\0'; | |
4677 | cb.cb_value++; | |
4678 | ||
4679 | error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL, | |
4680 | set_callback, &cb); | |
4681 | ||
4682 | return (error); | |
4683 | } | |
4684 | ||
4685 | static int | |
4686 | find_command_idx(char *command, int *idx) | |
4687 | { | |
4688 | int i; | |
4689 | ||
4690 | for (i = 0; i < NCOMMAND; i++) { | |
4691 | if (command_table[i].name == NULL) | |
4692 | continue; | |
4693 | ||
4694 | if (strcmp(command, command_table[i].name) == 0) { | |
4695 | *idx = i; | |
4696 | return (0); | |
4697 | } | |
4698 | } | |
4699 | return (1); | |
4700 | } | |
4701 | ||
4702 | int | |
4703 | main(int argc, char **argv) | |
4704 | { | |
4705 | int ret; | |
d4ed6673 | 4706 | int i = 0; |
34dc7c2f BB |
4707 | char *cmdname; |
4708 | ||
4709 | (void) setlocale(LC_ALL, ""); | |
4710 | (void) textdomain(TEXT_DOMAIN); | |
4711 | ||
34dc7c2f BB |
4712 | opterr = 0; |
4713 | ||
4714 | /* | |
4715 | * Make sure the user has specified some command. | |
4716 | */ | |
4717 | if (argc < 2) { | |
4718 | (void) fprintf(stderr, gettext("missing command\n")); | |
4719 | usage(B_FALSE); | |
4720 | } | |
4721 | ||
4722 | cmdname = argv[1]; | |
4723 | ||
4724 | /* | |
4725 | * Special case '-?' | |
4726 | */ | |
9b020fd9 BB |
4727 | if ((strcmp(cmdname, "-?") == 0) || |
4728 | strcmp(cmdname, "--help") == 0) | |
34dc7c2f BB |
4729 | usage(B_TRUE); |
4730 | ||
9b020fd9 BB |
4731 | if ((g_zfs = libzfs_init()) == NULL) |
4732 | return (1); | |
4733 | ||
4734 | libzfs_print_on_error(g_zfs, B_TRUE); | |
4735 | ||
34dc7c2f BB |
4736 | zpool_set_history_str("zpool", argc, argv, history_str); |
4737 | verify(zpool_stage_history(g_zfs, history_str) == 0); | |
4738 | ||
4739 | /* | |
4740 | * Run the appropriate command. | |
4741 | */ | |
4742 | if (find_command_idx(cmdname, &i) == 0) { | |
4743 | current_command = &command_table[i]; | |
4744 | ret = command_table[i].func(argc - 1, argv + 1); | |
4745 | } else if (strchr(cmdname, '=')) { | |
4746 | verify(find_command_idx("set", &i) == 0); | |
4747 | current_command = &command_table[i]; | |
4748 | ret = command_table[i].func(argc, argv); | |
4749 | } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) { | |
4750 | /* | |
4751 | * 'freeze' is a vile debugging abomination, so we treat | |
4752 | * it as such. | |
4753 | */ | |
4754 | char buf[16384]; | |
4755 | int fd = open(ZFS_DEV, O_RDWR); | |
4756 | (void) strcpy((void *)buf, argv[2]); | |
4757 | return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf)); | |
4758 | } else { | |
4759 | (void) fprintf(stderr, gettext("unrecognized " | |
4760 | "command '%s'\n"), cmdname); | |
4761 | usage(B_FALSE); | |
d4ed6673 | 4762 | ret = 1; |
34dc7c2f BB |
4763 | } |
4764 | ||
4765 | libzfs_fini(g_zfs); | |
4766 | ||
4767 | /* | |
4768 | * The 'ZFS_ABORT' environment variable causes us to dump core on exit | |
4769 | * for the purposes of running ::findleaks. | |
4770 | */ | |
4771 | if (getenv("ZFS_ABORT") != NULL) { | |
4772 | (void) printf("dumping core by request\n"); | |
4773 | abort(); | |
4774 | } | |
4775 | ||
4776 | return (ret); | |
4777 | } |