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