Deleted Added
full compact
vdev.c (177698) vdev.c (185029)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE

--- 6 unchanged lines hidden (view full) ---

15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE

--- 6 unchanged lines hidden (view full) ---

15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
24 * Use is subject to license terms.
25 */
26
27#pragma ident "%Z%%M% %I% %E% SMI"
28
29#include <sys/zfs_context.h>
30#include <sys/fm/fs/zfs.h>
31#include <sys/spa.h>
32#include <sys/spa_impl.h>
33#include <sys/dmu.h>
34#include <sys/dmu_tx.h>
35#include <sys/vdev_impl.h>
36#include <sys/uberblock_impl.h>
37#include <sys/metaslab.h>
38#include <sys/metaslab_impl.h>
39#include <sys/space_map.h>
40#include <sys/zio.h>
41#include <sys/zap.h>
42#include <sys/fs/zfs.h>
27#include <sys/zfs_context.h>
28#include <sys/fm/fs/zfs.h>
29#include <sys/spa.h>
30#include <sys/spa_impl.h>
31#include <sys/dmu.h>
32#include <sys/dmu_tx.h>
33#include <sys/vdev_impl.h>
34#include <sys/uberblock_impl.h>
35#include <sys/metaslab.h>
36#include <sys/metaslab_impl.h>
37#include <sys/space_map.h>
38#include <sys/zio.h>
39#include <sys/zap.h>
40#include <sys/fs/zfs.h>
41#include <sys/arc.h>
43
44SYSCTL_DECL(_vfs_zfs);
45SYSCTL_NODE(_vfs_zfs, OID_AUTO, vdev, CTLFLAG_RW, 0, "ZFS VDEV");
46
47/*
48 * Virtual device management.
49 */
50
51static vdev_ops_t *vdev_ops_table[] = {
52 &vdev_root_ops,
53 &vdev_raidz_ops,
54 &vdev_mirror_ops,
55 &vdev_replacing_ops,
56 &vdev_spare_ops,
57#ifdef _KERNEL
58 &vdev_geom_ops,
59#else
60 &vdev_disk_ops,
42
43SYSCTL_DECL(_vfs_zfs);
44SYSCTL_NODE(_vfs_zfs, OID_AUTO, vdev, CTLFLAG_RW, 0, "ZFS VDEV");
45
46/*
47 * Virtual device management.
48 */
49
50static vdev_ops_t *vdev_ops_table[] = {
51 &vdev_root_ops,
52 &vdev_raidz_ops,
53 &vdev_mirror_ops,
54 &vdev_replacing_ops,
55 &vdev_spare_ops,
56#ifdef _KERNEL
57 &vdev_geom_ops,
58#else
59 &vdev_disk_ops,
61 &vdev_file_ops,
62#endif
60#endif
61 &vdev_file_ops,
63 &vdev_missing_ops,
64 NULL
65};
66
62 &vdev_missing_ops,
63 NULL
64};
65
67/* maximum scrub/resilver I/O queue */
68int zfs_scrub_limit = 70;
66/* maximum scrub/resilver I/O queue per leaf vdev */
67int zfs_scrub_limit = 10;
69
68
69TUNABLE_INT("vfs.zfs.scrub_limit", &zfs_scrub_limit);
70SYSCTL_INT(_vfs_zfs, OID_AUTO, scrub_limit, CTLFLAG_RDTUN, &zfs_scrub_limit, 0,
71 "Maximum scrub/resilver I/O queue");
72
70/*
71 * Given a vdev type, return the appropriate ops vector.
72 */
73static vdev_ops_t *
74vdev_getops(const char *type)
75{
76 vdev_ops_t *ops, **opspp;
77

--- 60 unchanged lines hidden (view full) ---

138 return (rsize);
139}
140
141vdev_t *
142vdev_lookup_top(spa_t *spa, uint64_t vdev)
143{
144 vdev_t *rvd = spa->spa_root_vdev;
145
73/*
74 * Given a vdev type, return the appropriate ops vector.
75 */
76static vdev_ops_t *
77vdev_getops(const char *type)
78{
79 vdev_ops_t *ops, **opspp;
80

--- 60 unchanged lines hidden (view full) ---

141 return (rsize);
142}
143
144vdev_t *
145vdev_lookup_top(spa_t *spa, uint64_t vdev)
146{
147 vdev_t *rvd = spa->spa_root_vdev;
148
146 if (vdev < rvd->vdev_children)
149 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
150
151 if (vdev < rvd->vdev_children) {
152 ASSERT(rvd->vdev_child[vdev] != NULL);
147 return (rvd->vdev_child[vdev]);
153 return (rvd->vdev_child[vdev]);
154 }
148
149 return (NULL);
150}
151
152vdev_t *
153vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
154{
155 int c;

--- 12 unchanged lines hidden (view full) ---

168
169void
170vdev_add_child(vdev_t *pvd, vdev_t *cvd)
171{
172 size_t oldsize, newsize;
173 uint64_t id = cvd->vdev_id;
174 vdev_t **newchild;
175
155
156 return (NULL);
157}
158
159vdev_t *
160vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
161{
162 int c;

--- 12 unchanged lines hidden (view full) ---

175
176void
177vdev_add_child(vdev_t *pvd, vdev_t *cvd)
178{
179 size_t oldsize, newsize;
180 uint64_t id = cvd->vdev_id;
181 vdev_t **newchild;
182
176 ASSERT(spa_config_held(cvd->vdev_spa, RW_WRITER));
183 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
177 ASSERT(cvd->vdev_parent == NULL);
178
179 cvd->vdev_parent = pvd;
180
181 if (pvd == NULL)
182 return;
183
184 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);

--- 66 unchanged lines hidden (view full) ---

251 */
252void
253vdev_compact_children(vdev_t *pvd)
254{
255 vdev_t **newchild, *cvd;
256 int oldc = pvd->vdev_children;
257 int newc, c;
258
184 ASSERT(cvd->vdev_parent == NULL);
185
186 cvd->vdev_parent = pvd;
187
188 if (pvd == NULL)
189 return;
190
191 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);

--- 66 unchanged lines hidden (view full) ---

258 */
259void
260vdev_compact_children(vdev_t *pvd)
261{
262 vdev_t **newchild, *cvd;
263 int oldc = pvd->vdev_children;
264 int newc, c;
265
259 ASSERT(spa_config_held(pvd->vdev_spa, RW_WRITER));
266 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
260
261 for (c = newc = 0; c < oldc; c++)
262 if (pvd->vdev_child[c])
263 newc++;
264
265 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
266
267 for (c = newc = 0; c < oldc; c++) {

--- 46 unchanged lines hidden (view full) ---

314 vd->vdev_id = id;
315 vd->vdev_guid = guid;
316 vd->vdev_guid_sum = guid;
317 vd->vdev_ops = ops;
318 vd->vdev_state = VDEV_STATE_CLOSED;
319
320 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
321 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
267
268 for (c = newc = 0; c < oldc; c++)
269 if (pvd->vdev_child[c])
270 newc++;
271
272 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
273
274 for (c = newc = 0; c < oldc; c++) {

--- 46 unchanged lines hidden (view full) ---

321 vd->vdev_id = id;
322 vd->vdev_guid = guid;
323 vd->vdev_guid_sum = guid;
324 vd->vdev_ops = ops;
325 vd->vdev_state = VDEV_STATE_CLOSED;
326
327 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
328 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
329 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
322 space_map_create(&vd->vdev_dtl_map, 0, -1ULL, 0, &vd->vdev_dtl_lock);
323 space_map_create(&vd->vdev_dtl_scrub, 0, -1ULL, 0, &vd->vdev_dtl_lock);
324 txg_list_create(&vd->vdev_ms_list,
325 offsetof(struct metaslab, ms_txg_node));
326 txg_list_create(&vd->vdev_dtl_list,
327 offsetof(struct vdev, vdev_dtl_node));
328 vd->vdev_stat.vs_timestamp = gethrtime();
330 space_map_create(&vd->vdev_dtl_map, 0, -1ULL, 0, &vd->vdev_dtl_lock);
331 space_map_create(&vd->vdev_dtl_scrub, 0, -1ULL, 0, &vd->vdev_dtl_lock);
332 txg_list_create(&vd->vdev_ms_list,
333 offsetof(struct metaslab, ms_txg_node));
334 txg_list_create(&vd->vdev_dtl_list,
335 offsetof(struct vdev, vdev_dtl_node));
336 vd->vdev_stat.vs_timestamp = gethrtime();
337 vdev_queue_init(vd);
338 vdev_cache_init(vd);
329
330 return (vd);
331}
332
333/*
339
340 return (vd);
341}
342
343/*
334 * Free a vdev_t that has been removed from service.
335 */
336static void
337vdev_free_common(vdev_t *vd)
338{
339 spa_t *spa = vd->vdev_spa;
340
341 if (vd->vdev_path)
342 spa_strfree(vd->vdev_path);
343 if (vd->vdev_devid)
344 spa_strfree(vd->vdev_devid);
345
346 if (vd->vdev_isspare)
347 spa_spare_remove(vd);
348
349 txg_list_destroy(&vd->vdev_ms_list);
350 txg_list_destroy(&vd->vdev_dtl_list);
351 mutex_enter(&vd->vdev_dtl_lock);
352 space_map_unload(&vd->vdev_dtl_map);
353 space_map_destroy(&vd->vdev_dtl_map);
354 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
355 space_map_destroy(&vd->vdev_dtl_scrub);
356 mutex_exit(&vd->vdev_dtl_lock);
357 mutex_destroy(&vd->vdev_dtl_lock);
358 mutex_destroy(&vd->vdev_stat_lock);
359
360 if (vd == spa->spa_root_vdev)
361 spa->spa_root_vdev = NULL;
362
363 kmem_free(vd, sizeof (vdev_t));
364}
365
366/*
367 * Allocate a new vdev. The 'alloctype' is used to control whether we are
368 * creating a new vdev or loading an existing one - the behavior is slightly
369 * different for each case.
370 */
371int
372vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
373 int alloctype)
374{
375 vdev_ops_t *ops;
376 char *type;
344 * Allocate a new vdev. The 'alloctype' is used to control whether we are
345 * creating a new vdev or loading an existing one - the behavior is slightly
346 * different for each case.
347 */
348int
349vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
350 int alloctype)
351{
352 vdev_ops_t *ops;
353 char *type;
377 uint64_t guid = 0;
354 uint64_t guid = 0, islog, nparity;
378 vdev_t *vd;
379
355 vdev_t *vd;
356
380 ASSERT(spa_config_held(spa, RW_WRITER));
357 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
381
382 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
383 return (EINVAL);
384
385 if ((ops = vdev_getops(type)) == NULL)
386 return (EINVAL);
387
388 /*

--- 7 unchanged lines hidden (view full) ---

396 label_id != id)
397 return (EINVAL);
398
399 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
400 return (EINVAL);
401 } else if (alloctype == VDEV_ALLOC_SPARE) {
402 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
403 return (EINVAL);
358
359 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
360 return (EINVAL);
361
362 if ((ops = vdev_getops(type)) == NULL)
363 return (EINVAL);
364
365 /*

--- 7 unchanged lines hidden (view full) ---

373 label_id != id)
374 return (EINVAL);
375
376 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
377 return (EINVAL);
378 } else if (alloctype == VDEV_ALLOC_SPARE) {
379 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
380 return (EINVAL);
381 } else if (alloctype == VDEV_ALLOC_L2CACHE) {
382 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
383 return (EINVAL);
404 }
405
406 /*
407 * The first allocated vdev must be of type 'root'.
408 */
409 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
410 return (EINVAL);
411
384 }
385
386 /*
387 * The first allocated vdev must be of type 'root'.
388 */
389 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
390 return (EINVAL);
391
412 vd = vdev_alloc_common(spa, id, guid, ops);
392 /*
393 * Determine whether we're a log vdev.
394 */
395 islog = 0;
396 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
397 if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
398 return (ENOTSUP);
413
399
414 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
415 vd->vdev_path = spa_strdup(vd->vdev_path);
416 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
417 vd->vdev_devid = spa_strdup(vd->vdev_devid);
418
419 /*
400 /*
420 * Set the nparity propery for RAID-Z vdevs.
401 * Set the nparity property for RAID-Z vdevs.
421 */
402 */
403 nparity = -1ULL;
422 if (ops == &vdev_raidz_ops) {
423 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
404 if (ops == &vdev_raidz_ops) {
405 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
424 &vd->vdev_nparity) == 0) {
406 &nparity) == 0) {
425 /*
426 * Currently, we can only support 2 parity devices.
427 */
407 /*
408 * Currently, we can only support 2 parity devices.
409 */
428 if (vd->vdev_nparity > 2)
410 if (nparity == 0 || nparity > 2)
429 return (EINVAL);
430 /*
431 * Older versions can only support 1 parity device.
432 */
411 return (EINVAL);
412 /*
413 * Older versions can only support 1 parity device.
414 */
433 if (vd->vdev_nparity == 2 &&
434 spa_version(spa) < ZFS_VERSION_RAID6)
415 if (nparity == 2 &&
416 spa_version(spa) < SPA_VERSION_RAID6)
435 return (ENOTSUP);
417 return (ENOTSUP);
436
437 } else {
438 /*
439 * We require the parity to be specified for SPAs that
440 * support multiple parity levels.
441 */
418 } else {
419 /*
420 * We require the parity to be specified for SPAs that
421 * support multiple parity levels.
422 */
442 if (spa_version(spa) >= ZFS_VERSION_RAID6)
423 if (spa_version(spa) >= SPA_VERSION_RAID6)
443 return (EINVAL);
424 return (EINVAL);
444
445 /*
446 * Otherwise, we default to 1 parity device for RAID-Z.
447 */
425 /*
426 * Otherwise, we default to 1 parity device for RAID-Z.
427 */
448 vd->vdev_nparity = 1;
428 nparity = 1;
449 }
450 } else {
429 }
430 } else {
451 vd->vdev_nparity = 0;
431 nparity = 0;
452 }
432 }
433 ASSERT(nparity != -1ULL);
453
434
435 vd = vdev_alloc_common(spa, id, guid, ops);
436
437 vd->vdev_islog = islog;
438 vd->vdev_nparity = nparity;
439
440 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
441 vd->vdev_path = spa_strdup(vd->vdev_path);
442 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
443 vd->vdev_devid = spa_strdup(vd->vdev_devid);
444 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
445 &vd->vdev_physpath) == 0)
446 vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
447
454 /*
455 * Set the whole_disk property. If it's not specified, leave the value
456 * as -1.
457 */
458 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
459 &vd->vdev_wholedisk) != 0)
460 vd->vdev_wholedisk = -1ULL;
461
462 /*
463 * Look for the 'not present' flag. This will only be set if the device
464 * was not present at the time of import.
465 */
448 /*
449 * Set the whole_disk property. If it's not specified, leave the value
450 * as -1.
451 */
452 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
453 &vd->vdev_wholedisk) != 0)
454 vd->vdev_wholedisk = -1ULL;
455
456 /*
457 * Look for the 'not present' flag. This will only be set if the device
458 * was not present at the time of import.
459 */
466 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
467 &vd->vdev_not_present);
460 if (!spa->spa_import_faulted)
461 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
462 &vd->vdev_not_present);
468
469 /*
470 * Get the alignment requirement.
471 */
472 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
473
474 /*
475 * If we're a top-level vdev, try to load the allocation parameters.
476 */
477 if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) {
478 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
479 &vd->vdev_ms_array);
480 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
481 &vd->vdev_ms_shift);
482 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
483 &vd->vdev_asize);
484 }
485
486 /*
463
464 /*
465 * Get the alignment requirement.
466 */
467 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
468
469 /*
470 * If we're a top-level vdev, try to load the allocation parameters.
471 */
472 if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) {
473 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
474 &vd->vdev_ms_array);
475 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
476 &vd->vdev_ms_shift);
477 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
478 &vd->vdev_asize);
479 }
480
481 /*
487 * If we're a leaf vdev, try to load the DTL object and offline state.
482 * If we're a leaf vdev, try to load the DTL object and other state.
488 */
483 */
489 if (vd->vdev_ops->vdev_op_leaf && alloctype == VDEV_ALLOC_LOAD) {
490 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
491 &vd->vdev_dtl.smo_object);
484 if (vd->vdev_ops->vdev_op_leaf &&
485 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE)) {
486 if (alloctype == VDEV_ALLOC_LOAD) {
487 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
488 &vd->vdev_dtl.smo_object);
489 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
490 &vd->vdev_unspare);
491 }
492 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
493 &vd->vdev_offline);
492 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
493 &vd->vdev_offline);
494
495 /*
496 * When importing a pool, we want to ignore the persistent fault
497 * state, as the diagnosis made on another system may not be
498 * valid in the current context.
499 */
500 if (spa->spa_load_state == SPA_LOAD_OPEN) {
501 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
502 &vd->vdev_faulted);
503 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
504 &vd->vdev_degraded);
505 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
506 &vd->vdev_removed);
507 }
494 }
495
496 /*
497 * Add ourselves to the parent's list of children.
498 */
499 vdev_add_child(parent, vd);
500
501 *vdp = vd;
502
503 return (0);
504}
505
506void
507vdev_free(vdev_t *vd)
508{
509 int c;
508 }
509
510 /*
511 * Add ourselves to the parent's list of children.
512 */
513 vdev_add_child(parent, vd);
514
515 *vdp = vd;
516
517 return (0);
518}
519
520void
521vdev_free(vdev_t *vd)
522{
523 int c;
524 spa_t *spa = vd->vdev_spa;
510
511 /*
512 * vdev_free() implies closing the vdev first. This is simpler than
513 * trying to ensure complicated semantics for all callers.
514 */
515 vdev_close(vd);
516
525
526 /*
527 * vdev_free() implies closing the vdev first. This is simpler than
528 * trying to ensure complicated semantics for all callers.
529 */
530 vdev_close(vd);
531
517 ASSERT(!list_link_active(&vd->vdev_dirty_node));
532 ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
518
519 /*
520 * Free all children.
521 */
522 for (c = 0; c < vd->vdev_children; c++)
523 vdev_free(vd->vdev_child[c]);
524
525 ASSERT(vd->vdev_child == NULL);

--- 11 unchanged lines hidden (view full) ---

537
538 /*
539 * Remove this vdev from its parent's child list.
540 */
541 vdev_remove_child(vd->vdev_parent, vd);
542
543 ASSERT(vd->vdev_parent == NULL);
544
533
534 /*
535 * Free all children.
536 */
537 for (c = 0; c < vd->vdev_children; c++)
538 vdev_free(vd->vdev_child[c]);
539
540 ASSERT(vd->vdev_child == NULL);

--- 11 unchanged lines hidden (view full) ---

552
553 /*
554 * Remove this vdev from its parent's child list.
555 */
556 vdev_remove_child(vd->vdev_parent, vd);
557
558 ASSERT(vd->vdev_parent == NULL);
559
545 vdev_free_common(vd);
560 /*
561 * Clean up vdev structure.
562 */
563 vdev_queue_fini(vd);
564 vdev_cache_fini(vd);
565
566 if (vd->vdev_path)
567 spa_strfree(vd->vdev_path);
568 if (vd->vdev_devid)
569 spa_strfree(vd->vdev_devid);
570 if (vd->vdev_physpath)
571 spa_strfree(vd->vdev_physpath);
572
573 if (vd->vdev_isspare)
574 spa_spare_remove(vd);
575 if (vd->vdev_isl2cache)
576 spa_l2cache_remove(vd);
577
578 txg_list_destroy(&vd->vdev_ms_list);
579 txg_list_destroy(&vd->vdev_dtl_list);
580 mutex_enter(&vd->vdev_dtl_lock);
581 space_map_unload(&vd->vdev_dtl_map);
582 space_map_destroy(&vd->vdev_dtl_map);
583 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
584 space_map_destroy(&vd->vdev_dtl_scrub);
585 mutex_exit(&vd->vdev_dtl_lock);
586 mutex_destroy(&vd->vdev_dtl_lock);
587 mutex_destroy(&vd->vdev_stat_lock);
588 mutex_destroy(&vd->vdev_probe_lock);
589
590 if (vd == spa->spa_root_vdev)
591 spa->spa_root_vdev = NULL;
592
593 kmem_free(vd, sizeof (vdev_t));
546}
547
548/*
549 * Transfer top-level vdev state from svd to tvd.
550 */
551static void
552vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
553{

--- 33 unchanged lines hidden (view full) ---

587 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
588 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
589 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
590 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
591 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
592 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
593 }
594
594}
595
596/*
597 * Transfer top-level vdev state from svd to tvd.
598 */
599static void
600vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
601{

--- 33 unchanged lines hidden (view full) ---

635 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
636 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
637 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
638 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
639 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
640 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
641 }
642
595 if (list_link_active(&svd->vdev_dirty_node)) {
643 if (list_link_active(&svd->vdev_config_dirty_node)) {
596 vdev_config_clean(svd);
597 vdev_config_dirty(tvd);
598 }
599
644 vdev_config_clean(svd);
645 vdev_config_dirty(tvd);
646 }
647
600 tvd->vdev_reopen_wanted = svd->vdev_reopen_wanted;
601 svd->vdev_reopen_wanted = 0;
648 if (list_link_active(&svd->vdev_state_dirty_node)) {
649 vdev_state_clean(svd);
650 vdev_state_dirty(tvd);
651 }
602
603 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
604 svd->vdev_deflate_ratio = 0;
652
653 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
654 svd->vdev_deflate_ratio = 0;
655
656 tvd->vdev_islog = svd->vdev_islog;
657 svd->vdev_islog = 0;
605}
606
607static void
608vdev_top_update(vdev_t *tvd, vdev_t *vd)
609{
610 int c;
611
612 if (vd == NULL)

--- 10 unchanged lines hidden (view full) ---

623 */
624vdev_t *
625vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
626{
627 spa_t *spa = cvd->vdev_spa;
628 vdev_t *pvd = cvd->vdev_parent;
629 vdev_t *mvd;
630
658}
659
660static void
661vdev_top_update(vdev_t *tvd, vdev_t *vd)
662{
663 int c;
664
665 if (vd == NULL)

--- 10 unchanged lines hidden (view full) ---

676 */
677vdev_t *
678vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
679{
680 spa_t *spa = cvd->vdev_spa;
681 vdev_t *pvd = cvd->vdev_parent;
682 vdev_t *mvd;
683
631 ASSERT(spa_config_held(spa, RW_WRITER));
684 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
632
633 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
634
635 mvd->vdev_asize = cvd->vdev_asize;
636 mvd->vdev_ashift = cvd->vdev_ashift;
637 mvd->vdev_state = cvd->vdev_state;
638
639 vdev_remove_child(pvd, cvd);

--- 12 unchanged lines hidden (view full) ---

652 * Remove a 1-way mirror/replacing vdev from the tree.
653 */
654void
655vdev_remove_parent(vdev_t *cvd)
656{
657 vdev_t *mvd = cvd->vdev_parent;
658 vdev_t *pvd = mvd->vdev_parent;
659
685
686 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
687
688 mvd->vdev_asize = cvd->vdev_asize;
689 mvd->vdev_ashift = cvd->vdev_ashift;
690 mvd->vdev_state = cvd->vdev_state;
691
692 vdev_remove_child(pvd, cvd);

--- 12 unchanged lines hidden (view full) ---

705 * Remove a 1-way mirror/replacing vdev from the tree.
706 */
707void
708vdev_remove_parent(vdev_t *cvd)
709{
710 vdev_t *mvd = cvd->vdev_parent;
711 vdev_t *pvd = mvd->vdev_parent;
712
660 ASSERT(spa_config_held(cvd->vdev_spa, RW_WRITER));
713 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
661
662 ASSERT(mvd->vdev_children == 1);
663 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
664 mvd->vdev_ops == &vdev_replacing_ops ||
665 mvd->vdev_ops == &vdev_spare_ops);
666 cvd->vdev_ashift = mvd->vdev_ashift;
667
668 vdev_remove_child(mvd, cvd);
669 vdev_remove_child(pvd, mvd);
714
715 ASSERT(mvd->vdev_children == 1);
716 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
717 mvd->vdev_ops == &vdev_replacing_ops ||
718 mvd->vdev_ops == &vdev_spare_ops);
719 cvd->vdev_ashift = mvd->vdev_ashift;
720
721 vdev_remove_child(mvd, cvd);
722 vdev_remove_child(pvd, mvd);
670 cvd->vdev_id = mvd->vdev_id;
671 vdev_add_child(pvd, cvd);
672 /*
723 /*
673 * If we created a new toplevel vdev, then we need to change the child's
674 * vdev GUID to match the old toplevel vdev. Otherwise, we could have
675 * detached an offline device, and when we go to import the pool we'll
676 * think we have two toplevel vdevs, instead of a different version of
677 * the same toplevel vdev.
724 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
725 * Otherwise, we could have detached an offline device, and when we
726 * go to import the pool we'll think we have two top-level vdevs,
727 * instead of a different version of the same top-level vdev.
678 */
728 */
679 if (cvd->vdev_top == cvd) {
680 pvd->vdev_guid_sum -= cvd->vdev_guid;
681 cvd->vdev_guid_sum -= cvd->vdev_guid;
682 cvd->vdev_guid = mvd->vdev_guid;
683 cvd->vdev_guid_sum += mvd->vdev_guid;
684 pvd->vdev_guid_sum += cvd->vdev_guid;
685 }
729 if (mvd->vdev_top == mvd)
730 cvd->vdev_guid = cvd->vdev_guid_sum = mvd->vdev_guid;
731 cvd->vdev_id = mvd->vdev_id;
732 vdev_add_child(pvd, cvd);
686 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
687
688 if (cvd == cvd->vdev_top)
689 vdev_top_transfer(mvd, cvd);
690
691 ASSERT(mvd->vdev_children == 0);
692 vdev_free(mvd);
693}
694
695int
696vdev_metaslab_init(vdev_t *vd, uint64_t txg)
697{
698 spa_t *spa = vd->vdev_spa;
699 objset_t *mos = spa->spa_meta_objset;
733 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
734
735 if (cvd == cvd->vdev_top)
736 vdev_top_transfer(mvd, cvd);
737
738 ASSERT(mvd->vdev_children == 0);
739 vdev_free(mvd);
740}
741
742int
743vdev_metaslab_init(vdev_t *vd, uint64_t txg)
744{
745 spa_t *spa = vd->vdev_spa;
746 objset_t *mos = spa->spa_meta_objset;
700 metaslab_class_t *mc = spa_metaslab_class_select(spa);
747 metaslab_class_t *mc;
701 uint64_t m;
702 uint64_t oldc = vd->vdev_ms_count;
703 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
704 metaslab_t **mspp;
705 int error;
706
707 if (vd->vdev_ms_shift == 0) /* not being allocated from yet */
708 return (0);
709
748 uint64_t m;
749 uint64_t oldc = vd->vdev_ms_count;
750 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
751 metaslab_t **mspp;
752 int error;
753
754 if (vd->vdev_ms_shift == 0) /* not being allocated from yet */
755 return (0);
756
710 dprintf("%s oldc %llu newc %llu\n", vdev_description(vd), oldc, newc);
711
712 ASSERT(oldc <= newc);
713
757 ASSERT(oldc <= newc);
758
759 if (vd->vdev_islog)
760 mc = spa->spa_log_class;
761 else
762 mc = spa->spa_normal_class;
763
714 if (vd->vdev_mg == NULL)
715 vd->vdev_mg = metaslab_group_create(mc, vd);
716
717 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
718
719 if (oldc != 0) {
720 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
721 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));

--- 10 unchanged lines hidden (view full) ---

732 m * sizeof (uint64_t), sizeof (uint64_t), &object);
733 if (error)
734 return (error);
735 if (object != 0) {
736 dmu_buf_t *db;
737 error = dmu_bonus_hold(mos, object, FTAG, &db);
738 if (error)
739 return (error);
764 if (vd->vdev_mg == NULL)
765 vd->vdev_mg = metaslab_group_create(mc, vd);
766
767 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
768
769 if (oldc != 0) {
770 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
771 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));

--- 10 unchanged lines hidden (view full) ---

782 m * sizeof (uint64_t), sizeof (uint64_t), &object);
783 if (error)
784 return (error);
785 if (object != 0) {
786 dmu_buf_t *db;
787 error = dmu_bonus_hold(mos, object, FTAG, &db);
788 if (error)
789 return (error);
740 ASSERT3U(db->db_size, ==, sizeof (smo));
741 bcopy(db->db_data, &smo, db->db_size);
790 ASSERT3U(db->db_size, >=, sizeof (smo));
791 bcopy(db->db_data, &smo, sizeof (smo));
742 ASSERT3U(smo.smo_object, ==, object);
743 dmu_buf_rele(db, FTAG);
744 }
745 }
746 vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
747 m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
748 }
749

--- 10 unchanged lines hidden (view full) ---

760 for (m = 0; m < count; m++)
761 if (vd->vdev_ms[m] != NULL)
762 metaslab_fini(vd->vdev_ms[m]);
763 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
764 vd->vdev_ms = NULL;
765 }
766}
767
792 ASSERT3U(smo.smo_object, ==, object);
793 dmu_buf_rele(db, FTAG);
794 }
795 }
796 vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
797 m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
798 }
799

--- 10 unchanged lines hidden (view full) ---

810 for (m = 0; m < count; m++)
811 if (vd->vdev_ms[m] != NULL)
812 metaslab_fini(vd->vdev_ms[m]);
813 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
814 vd->vdev_ms = NULL;
815 }
816}
817
818typedef struct vdev_probe_stats {
819 boolean_t vps_readable;
820 boolean_t vps_writeable;
821 int vps_flags;
822 zio_t *vps_root;
823 vdev_t *vps_vd;
824} vdev_probe_stats_t;
825
826static void
827vdev_probe_done(zio_t *zio)
828{
829 vdev_probe_stats_t *vps = zio->io_private;
830 vdev_t *vd = vps->vps_vd;
831
832 if (zio->io_type == ZIO_TYPE_READ) {
833 ASSERT(zio->io_vd == vd);
834 if (zio->io_error == 0)
835 vps->vps_readable = 1;
836 if (zio->io_error == 0 && (spa_mode & FWRITE)) {
837 zio_nowait(zio_write_phys(vps->vps_root, vd,
838 zio->io_offset, zio->io_size, zio->io_data,
839 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
840 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
841 } else {
842 zio_buf_free(zio->io_data, zio->io_size);
843 }
844 } else if (zio->io_type == ZIO_TYPE_WRITE) {
845 ASSERT(zio->io_vd == vd);
846 if (zio->io_error == 0)
847 vps->vps_writeable = 1;
848 zio_buf_free(zio->io_data, zio->io_size);
849 } else if (zio->io_type == ZIO_TYPE_NULL) {
850 ASSERT(zio->io_vd == NULL);
851 ASSERT(zio == vps->vps_root);
852
853 vd->vdev_cant_read |= !vps->vps_readable;
854 vd->vdev_cant_write |= !vps->vps_writeable;
855
856 if (vdev_readable(vd) &&
857 (vdev_writeable(vd) || !(spa_mode & FWRITE))) {
858 zio->io_error = 0;
859 } else {
860 ASSERT(zio->io_error != 0);
861 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
862 zio->io_spa, vd, NULL, 0, 0);
863 zio->io_error = ENXIO;
864 }
865 kmem_free(vps, sizeof (*vps));
866 }
867}
868
768/*
869/*
870 * Determine whether this device is accessible by reading and writing
871 * to several known locations: the pad regions of each vdev label
872 * but the first (which we leave alone in case it contains a VTOC).
873 */
874zio_t *
875vdev_probe(vdev_t *vd, zio_t *pio)
876{
877 spa_t *spa = vd->vdev_spa;
878 vdev_probe_stats_t *vps;
879 zio_t *zio;
880
881 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
882
883 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
884 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | ZIO_FLAG_DONT_RETRY;
885
886 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
887 /*
888 * vdev_cant_read and vdev_cant_write can only transition
889 * from TRUE to FALSE when we have the SCL_ZIO lock as writer;
890 * otherwise they can only transition from FALSE to TRUE.
891 * This ensures that any zio looking at these values can
892 * assume that failures persist for the life of the I/O.
893 * That's important because when a device has intermittent
894 * connectivity problems, we want to ensure that they're
895 * ascribed to the device (ENXIO) and not the zio (EIO).
896 *
897 * Since we hold SCL_ZIO as writer here, clear both values
898 * so the probe can reevaluate from first principles.
899 */
900 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
901 vd->vdev_cant_read = B_FALSE;
902 vd->vdev_cant_write = B_FALSE;
903 }
904
905 ASSERT(vd->vdev_ops->vdev_op_leaf);
906
907 zio = zio_null(pio, spa, vdev_probe_done, vps, vps->vps_flags);
908
909 vps->vps_root = zio;
910 vps->vps_vd = vd;
911
912 for (int l = 1; l < VDEV_LABELS; l++) {
913 zio_nowait(zio_read_phys(zio, vd,
914 vdev_label_offset(vd->vdev_psize, l,
915 offsetof(vdev_label_t, vl_pad)),
916 VDEV_SKIP_SIZE, zio_buf_alloc(VDEV_SKIP_SIZE),
917 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
918 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
919 }
920
921 return (zio);
922}
923
924/*
769 * Prepare a virtual device for access.
770 */
771int
772vdev_open(vdev_t *vd)
773{
774 int error;
775 int c;
776 uint64_t osize = 0;
777 uint64_t asize, psize;
778 uint64_t ashift = 0;
779
780 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
781 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
782 vd->vdev_state == VDEV_STATE_OFFLINE);
783
925 * Prepare a virtual device for access.
926 */
927int
928vdev_open(vdev_t *vd)
929{
930 int error;
931 int c;
932 uint64_t osize = 0;
933 uint64_t asize, psize;
934 uint64_t ashift = 0;
935
936 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
937 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
938 vd->vdev_state == VDEV_STATE_OFFLINE);
939
784 if (vd->vdev_fault_mode == VDEV_FAULT_COUNT)
785 vd->vdev_fault_arg >>= 1;
786 else
787 vd->vdev_fault_mode = VDEV_FAULT_NONE;
788
789 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
790
940 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
941
791 if (vd->vdev_ops->vdev_op_leaf) {
792 vdev_cache_init(vd);
793 vdev_queue_init(vd);
794 vd->vdev_cache_active = B_TRUE;
795 }
796
797 if (vd->vdev_offline) {
942 if (!vd->vdev_removed && vd->vdev_faulted) {
798 ASSERT(vd->vdev_children == 0);
943 ASSERT(vd->vdev_children == 0);
944 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
945 VDEV_AUX_ERR_EXCEEDED);
946 return (ENXIO);
947 } else if (vd->vdev_offline) {
948 ASSERT(vd->vdev_children == 0);
799 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
800 return (ENXIO);
801 }
802
803 error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
804
805 if (zio_injection_enabled && error == 0)
806 error = zio_handle_device_injection(vd, ENXIO);
807
949 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
950 return (ENXIO);
951 }
952
953 error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
954
955 if (zio_injection_enabled && error == 0)
956 error = zio_handle_device_injection(vd, ENXIO);
957
808 dprintf("%s = %d, osize %llu, state = %d\n",
809 vdev_description(vd), error, osize, vd->vdev_state);
810
811 if (error) {
958 if (error) {
959 if (vd->vdev_removed &&
960 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
961 vd->vdev_removed = B_FALSE;
962
812 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
813 vd->vdev_stat.vs_aux);
814 return (error);
815 }
816
963 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
964 vd->vdev_stat.vs_aux);
965 return (error);
966 }
967
817 vd->vdev_state = VDEV_STATE_HEALTHY;
968 vd->vdev_removed = B_FALSE;
818
969
970 if (vd->vdev_degraded) {
971 ASSERT(vd->vdev_children == 0);
972 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
973 VDEV_AUX_ERR_EXCEEDED);
974 } else {
975 vd->vdev_state = VDEV_STATE_HEALTHY;
976 }
977
819 for (c = 0; c < vd->vdev_children; c++)
820 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
821 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
822 VDEV_AUX_NONE);
823 break;
824 }
825
826 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));

--- 51 unchanged lines hidden (view full) ---

878 */
879 if (vd->vdev_state == VDEV_STATE_HEALTHY &&
880 asize > vd->vdev_asize) {
881 vd->vdev_asize = asize;
882 }
883 }
884
885 /*
978 for (c = 0; c < vd->vdev_children; c++)
979 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
980 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
981 VDEV_AUX_NONE);
982 break;
983 }
984
985 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));

--- 51 unchanged lines hidden (view full) ---

1037 */
1038 if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1039 asize > vd->vdev_asize) {
1040 vd->vdev_asize = asize;
1041 }
1042 }
1043
1044 /*
1045 * Ensure we can issue some IO before declaring the
1046 * vdev open for business.
1047 */
1048 if (vd->vdev_ops->vdev_op_leaf &&
1049 (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
1050 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1051 VDEV_AUX_IO_FAILURE);
1052 return (error);
1053 }
1054
1055 /*
886 * If this is a top-level vdev, compute the raidz-deflation
887 * ratio. Note, we hard-code in 128k (1<<17) because it is the
888 * current "typical" blocksize. Even if SPA_MAXBLOCKSIZE
889 * changes, this algorithm must never change, or we will
890 * inconsistently account for existing bp's.
891 */
892 if (vd->vdev_top == vd) {
893 vd->vdev_deflate_ratio = (1<<17) /
894 (vdev_psize_to_asize(vd, 1<<17) >> SPA_MINBLOCKSHIFT);
895 }
896
897 /*
1056 * If this is a top-level vdev, compute the raidz-deflation
1057 * ratio. Note, we hard-code in 128k (1<<17) because it is the
1058 * current "typical" blocksize. Even if SPA_MAXBLOCKSIZE
1059 * changes, this algorithm must never change, or we will
1060 * inconsistently account for existing bp's.
1061 */
1062 if (vd->vdev_top == vd) {
1063 vd->vdev_deflate_ratio = (1<<17) /
1064 (vdev_psize_to_asize(vd, 1<<17) >> SPA_MINBLOCKSHIFT);
1065 }
1066
1067 /*
898 * This allows the ZFS DE to close cases appropriately. If a device
899 * goes away and later returns, we want to close the associated case.
900 * But it's not enough to simply post this only when a device goes from
901 * CANT_OPEN -> HEALTHY. If we reboot the system and the device is
902 * back, we also need to close the case (otherwise we will try to replay
903 * it). So we have to post this notifier every time. Since this only
904 * occurs during pool open or error recovery, this should not be an
905 * issue.
1068 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1069 * resilver. But don't do this if we are doing a reopen for a
1070 * scrub, since this would just restart the scrub we are already
1071 * doing.
906 */
1072 */
907 zfs_post_ok(vd->vdev_spa, vd);
1073 if (vd->vdev_children == 0 && !vd->vdev_spa->spa_scrub_reopen) {
1074 mutex_enter(&vd->vdev_dtl_lock);
1075 if (vd->vdev_dtl_map.sm_space != 0 && vdev_writeable(vd))
1076 spa_async_request(vd->vdev_spa, SPA_ASYNC_RESILVER);
1077 mutex_exit(&vd->vdev_dtl_lock);
1078 }
908
909 return (0);
910}
911
912/*
913 * Called once the vdevs are all opened, this routine validates the label
914 * contents. This needs to be done before vdev_load() so that we don't
1079
1080 return (0);
1081}
1082
1083/*
1084 * Called once the vdevs are all opened, this routine validates the label
1085 * contents. This needs to be done before vdev_load() so that we don't
915 * inadvertently do repair I/Os to the wrong device, and so that vdev_reopen()
916 * won't succeed if the device has been changed underneath.
1086 * inadvertently do repair I/Os to the wrong device.
917 *
918 * This function will only return failure if one of the vdevs indicates that it
919 * has since been destroyed or exported. This is only possible if
920 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
921 * will be updated but the function will return 0.
922 */
923int
924vdev_validate(vdev_t *vd)
925{
926 spa_t *spa = vd->vdev_spa;
927 int c;
928 nvlist_t *label;
1087 *
1088 * This function will only return failure if one of the vdevs indicates that it
1089 * has since been destroyed or exported. This is only possible if
1090 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1091 * will be updated but the function will return 0.
1092 */
1093int
1094vdev_validate(vdev_t *vd)
1095{
1096 spa_t *spa = vd->vdev_spa;
1097 int c;
1098 nvlist_t *label;
929 uint64_t guid;
1099 uint64_t guid, top_guid;
930 uint64_t state;
931
932 for (c = 0; c < vd->vdev_children; c++)
933 if (vdev_validate(vd->vdev_child[c]) != 0)
934 return (EBADF);
935
936 /*
937 * If the device has already failed, or was marked offline, don't do
938 * any further validation. Otherwise, label I/O will fail and we will
939 * overwrite the previous state.
940 */
1100 uint64_t state;
1101
1102 for (c = 0; c < vd->vdev_children; c++)
1103 if (vdev_validate(vd->vdev_child[c]) != 0)
1104 return (EBADF);
1105
1106 /*
1107 * If the device has already failed, or was marked offline, don't do
1108 * any further validation. Otherwise, label I/O will fail and we will
1109 * overwrite the previous state.
1110 */
941 if (vd->vdev_ops->vdev_op_leaf && !vdev_is_dead(vd)) {
1111 if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
942
943 if ((label = vdev_label_read_config(vd)) == NULL) {
944 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
945 VDEV_AUX_BAD_LABEL);
946 return (0);
947 }
948
949 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
950 &guid) != 0 || guid != spa_guid(spa)) {
951 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
952 VDEV_AUX_CORRUPT_DATA);
953 nvlist_free(label);
954 return (0);
955 }
956
1112
1113 if ((label = vdev_label_read_config(vd)) == NULL) {
1114 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1115 VDEV_AUX_BAD_LABEL);
1116 return (0);
1117 }
1118
1119 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
1120 &guid) != 0 || guid != spa_guid(spa)) {
1121 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1122 VDEV_AUX_CORRUPT_DATA);
1123 nvlist_free(label);
1124 return (0);
1125 }
1126
1127 /*
1128 * If this vdev just became a top-level vdev because its
1129 * sibling was detached, it will have adopted the parent's
1130 * vdev guid -- but the label may or may not be on disk yet.
1131 * Fortunately, either version of the label will have the
1132 * same top guid, so if we're a top-level vdev, we can
1133 * safely compare to that instead.
1134 */
957 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
1135 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
958 &guid) != 0 || guid != vd->vdev_guid) {
1136 &guid) != 0 ||
1137 nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
1138 &top_guid) != 0 ||
1139 (vd->vdev_guid != guid &&
1140 (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
959 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
960 VDEV_AUX_CORRUPT_DATA);
961 nvlist_free(label);
962 return (0);
963 }
964
965 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
966 &state) != 0) {
967 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
968 VDEV_AUX_CORRUPT_DATA);
969 nvlist_free(label);
970 return (0);
971 }
972
973 nvlist_free(label);
974
975 if (spa->spa_load_state == SPA_LOAD_OPEN &&
976 state != POOL_STATE_ACTIVE)
977 return (EBADF);
1141 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1142 VDEV_AUX_CORRUPT_DATA);
1143 nvlist_free(label);
1144 return (0);
1145 }
1146
1147 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1148 &state) != 0) {
1149 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1150 VDEV_AUX_CORRUPT_DATA);
1151 nvlist_free(label);
1152 return (0);
1153 }
1154
1155 nvlist_free(label);
1156
1157 if (spa->spa_load_state == SPA_LOAD_OPEN &&
1158 state != POOL_STATE_ACTIVE)
1159 return (EBADF);
1160
1161 /*
1162 * If we were able to open and validate a vdev that was
1163 * previously marked permanently unavailable, clear that state
1164 * now.
1165 */
1166 if (vd->vdev_not_present)
1167 vd->vdev_not_present = 0;
978 }
979
1168 }
1169
980 /*
981 * If we were able to open and validate a vdev that was previously
982 * marked permanently unavailable, clear that state now.
983 */
984 if (vd->vdev_not_present)
985 vd->vdev_not_present = 0;
986
987 return (0);
988}
989
990/*
991 * Close a virtual device.
992 */
993void
994vdev_close(vdev_t *vd)
995{
996 vd->vdev_ops->vdev_op_close(vd);
997
1170 return (0);
1171}
1172
1173/*
1174 * Close a virtual device.
1175 */
1176void
1177vdev_close(vdev_t *vd)
1178{
1179 vd->vdev_ops->vdev_op_close(vd);
1180
998 if (vd->vdev_cache_active) {
999 vdev_cache_fini(vd);
1000 vdev_queue_fini(vd);
1001 vd->vdev_cache_active = B_FALSE;
1002 }
1181 vdev_cache_purge(vd);
1003
1004 /*
1005 * We record the previous state before we close it, so that if we are
1006 * doing a reopen(), we don't generate FMA ereports if we notice that
1007 * it's still faulted.
1008 */
1009 vd->vdev_prevstate = vd->vdev_state;
1010

--- 4 unchanged lines hidden (view full) ---

1015 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1016}
1017
1018void
1019vdev_reopen(vdev_t *vd)
1020{
1021 spa_t *spa = vd->vdev_spa;
1022
1182
1183 /*
1184 * We record the previous state before we close it, so that if we are
1185 * doing a reopen(), we don't generate FMA ereports if we notice that
1186 * it's still faulted.
1187 */
1188 vd->vdev_prevstate = vd->vdev_state;
1189

--- 4 unchanged lines hidden (view full) ---

1194 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1195}
1196
1197void
1198vdev_reopen(vdev_t *vd)
1199{
1200 spa_t *spa = vd->vdev_spa;
1201
1023 ASSERT(spa_config_held(spa, RW_WRITER));
1202 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1024
1025 vdev_close(vd);
1026 (void) vdev_open(vd);
1027
1028 /*
1029 * Call vdev_validate() here to make sure we have the same device.
1030 * Otherwise, a device with an invalid label could be successfully
1031 * opened in response to vdev_reopen().
1203
1204 vdev_close(vd);
1205 (void) vdev_open(vd);
1206
1207 /*
1208 * Call vdev_validate() here to make sure we have the same device.
1209 * Otherwise, a device with an invalid label could be successfully
1210 * opened in response to vdev_reopen().
1032 *
1033 * The downside to this is that if the user is simply experimenting by
1034 * overwriting an entire disk, we'll fault the device rather than
1035 * demonstrate self-healing capabilities. On the other hand, with
1036 * proper FMA integration, the series of errors we'd see from the device
1037 * would result in a faulted device anyway. Given that this doesn't
1038 * model any real-world corruption, it's better to catch this here and
1039 * correctly identify that the device has either changed beneath us, or
1040 * is corrupted beyond recognition.
1041 */
1211 */
1042 (void) vdev_validate(vd);
1212 if (vd->vdev_aux) {
1213 (void) vdev_validate_aux(vd);
1214 if (vdev_readable(vd) && vdev_writeable(vd) &&
1215 !l2arc_vdev_present(vd)) {
1216 uint64_t size = vdev_get_rsize(vd);
1217 l2arc_add_vdev(spa, vd,
1218 VDEV_LABEL_START_SIZE,
1219 size - VDEV_LABEL_START_SIZE);
1220 }
1221 } else {
1222 (void) vdev_validate(vd);
1223 }
1043
1044 /*
1224
1225 /*
1045 * Reassess root vdev's health.
1226 * Reassess parent vdev's health.
1046 */
1227 */
1047 vdev_propagate_state(spa->spa_root_vdev);
1228 vdev_propagate_state(vd);
1048}
1049
1050int
1051vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1052{
1053 int error;
1054
1055 /*

--- 89 unchanged lines hidden (view full) ---

1145 * Reassess DTLs after a config change or scrub completion.
1146 */
1147void
1148vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1149{
1150 spa_t *spa = vd->vdev_spa;
1151 int c;
1152
1229}
1230
1231int
1232vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1233{
1234 int error;
1235
1236 /*

--- 89 unchanged lines hidden (view full) ---

1326 * Reassess DTLs after a config change or scrub completion.
1327 */
1328void
1329vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1330{
1331 spa_t *spa = vd->vdev_spa;
1332 int c;
1333
1153 ASSERT(spa_config_held(spa, RW_WRITER));
1334 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
1154
1155 if (vd->vdev_children == 0) {
1156 mutex_enter(&vd->vdev_dtl_lock);
1335
1336 if (vd->vdev_children == 0) {
1337 mutex_enter(&vd->vdev_dtl_lock);
1157 /*
1158 * We're successfully scrubbed everything up to scrub_txg.
1159 * Therefore, excise all old DTLs up to that point, then
1160 * fold in the DTLs for everything we couldn't scrub.
1161 */
1162 if (scrub_txg != 0) {
1338 if (scrub_txg != 0 &&
1339 (spa->spa_scrub_started || spa->spa_scrub_errors == 0)) {
1340 /* XXX should check scrub_done? */
1341 /*
1342 * We completed a scrub up to scrub_txg. If we
1343 * did it without rebooting, then the scrub dtl
1344 * will be valid, so excise the old region and
1345 * fold in the scrub dtl. Otherwise, leave the
1346 * dtl as-is if there was an error.
1347 */
1163 space_map_excise(&vd->vdev_dtl_map, 0, scrub_txg);
1164 space_map_union(&vd->vdev_dtl_map, &vd->vdev_dtl_scrub);
1165 }
1166 if (scrub_done)
1167 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1168 mutex_exit(&vd->vdev_dtl_lock);
1348 space_map_excise(&vd->vdev_dtl_map, 0, scrub_txg);
1349 space_map_union(&vd->vdev_dtl_map, &vd->vdev_dtl_scrub);
1350 }
1351 if (scrub_done)
1352 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1353 mutex_exit(&vd->vdev_dtl_lock);
1354
1169 if (txg != 0)
1170 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1171 return;
1172 }
1173
1174 /*
1175 * Make sure the DTLs are always correct under the scrub lock.
1176 */

--- 30 unchanged lines hidden (view full) ---

1207 ASSERT(vd->vdev_children == 0);
1208
1209 if (smo->smo_object == 0)
1210 return (0);
1211
1212 if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
1213 return (error);
1214
1355 if (txg != 0)
1356 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1357 return;
1358 }
1359
1360 /*
1361 * Make sure the DTLs are always correct under the scrub lock.
1362 */

--- 30 unchanged lines hidden (view full) ---

1393 ASSERT(vd->vdev_children == 0);
1394
1395 if (smo->smo_object == 0)
1396 return (0);
1397
1398 if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
1399 return (error);
1400
1215 ASSERT3U(db->db_size, ==, sizeof (*smo));
1216 bcopy(db->db_data, smo, db->db_size);
1401 ASSERT3U(db->db_size, >=, sizeof (*smo));
1402 bcopy(db->db_data, smo, sizeof (*smo));
1217 dmu_buf_rele(db, FTAG);
1218
1219 mutex_enter(&vd->vdev_dtl_lock);
1220 error = space_map_load(&vd->vdev_dtl_map, NULL, SM_ALLOC, smo, mos);
1221 mutex_exit(&vd->vdev_dtl_lock);
1222
1223 return (error);
1224}

--- 5 unchanged lines hidden (view full) ---

1230 space_map_obj_t *smo = &vd->vdev_dtl;
1231 space_map_t *sm = &vd->vdev_dtl_map;
1232 objset_t *mos = spa->spa_meta_objset;
1233 space_map_t smsync;
1234 kmutex_t smlock;
1235 dmu_buf_t *db;
1236 dmu_tx_t *tx;
1237
1403 dmu_buf_rele(db, FTAG);
1404
1405 mutex_enter(&vd->vdev_dtl_lock);
1406 error = space_map_load(&vd->vdev_dtl_map, NULL, SM_ALLOC, smo, mos);
1407 mutex_exit(&vd->vdev_dtl_lock);
1408
1409 return (error);
1410}

--- 5 unchanged lines hidden (view full) ---

1416 space_map_obj_t *smo = &vd->vdev_dtl;
1417 space_map_t *sm = &vd->vdev_dtl_map;
1418 objset_t *mos = spa->spa_meta_objset;
1419 space_map_t smsync;
1420 kmutex_t smlock;
1421 dmu_buf_t *db;
1422 dmu_tx_t *tx;
1423
1238 dprintf("%s in txg %llu pass %d\n",
1239 vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa));
1240
1241 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1242
1243 if (vd->vdev_detached) {
1244 if (smo->smo_object != 0) {
1245 int err = dmu_object_free(mos, smo->smo_object, tx);
1246 ASSERT3U(err, ==, 0);
1247 smo->smo_object = 0;
1248 }
1249 dmu_tx_commit(tx);
1424 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1425
1426 if (vd->vdev_detached) {
1427 if (smo->smo_object != 0) {
1428 int err = dmu_object_free(mos, smo->smo_object, tx);
1429 ASSERT3U(err, ==, 0);
1430 smo->smo_object = 0;
1431 }
1432 dmu_tx_commit(tx);
1250 dprintf("detach %s committed in txg %llu\n",
1251 vdev_description(vd), txg);
1252 return;
1253 }
1254
1255 if (smo->smo_object == 0) {
1256 ASSERT(smo->smo_objsize == 0);
1257 ASSERT(smo->smo_alloc == 0);
1258 smo->smo_object = dmu_object_alloc(mos,
1259 DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,

--- 18 unchanged lines hidden (view full) ---

1278
1279 space_map_destroy(&smsync);
1280
1281 mutex_exit(&smlock);
1282 mutex_destroy(&smlock);
1283
1284 VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1285 dmu_buf_will_dirty(db, tx);
1433 return;
1434 }
1435
1436 if (smo->smo_object == 0) {
1437 ASSERT(smo->smo_objsize == 0);
1438 ASSERT(smo->smo_alloc == 0);
1439 smo->smo_object = dmu_object_alloc(mos,
1440 DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,

--- 18 unchanged lines hidden (view full) ---

1459
1460 space_map_destroy(&smsync);
1461
1462 mutex_exit(&smlock);
1463 mutex_destroy(&smlock);
1464
1465 VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1466 dmu_buf_will_dirty(db, tx);
1286 ASSERT3U(db->db_size, ==, sizeof (*smo));
1287 bcopy(smo, db->db_data, db->db_size);
1467 ASSERT3U(db->db_size, >=, sizeof (*smo));
1468 bcopy(smo, db->db_data, sizeof (*smo));
1288 dmu_buf_rele(db, FTAG);
1289
1290 dmu_tx_commit(tx);
1291}
1292
1469 dmu_buf_rele(db, FTAG);
1470
1471 dmu_tx_commit(tx);
1472}
1473
1474/*
1475 * Determine if resilver is needed, and if so the txg range.
1476 */
1477boolean_t
1478vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
1479{
1480 boolean_t needed = B_FALSE;
1481 uint64_t thismin = UINT64_MAX;
1482 uint64_t thismax = 0;
1483
1484 if (vd->vdev_children == 0) {
1485 mutex_enter(&vd->vdev_dtl_lock);
1486 if (vd->vdev_dtl_map.sm_space != 0 && vdev_writeable(vd)) {
1487 space_seg_t *ss;
1488
1489 ss = avl_first(&vd->vdev_dtl_map.sm_root);
1490 thismin = ss->ss_start - 1;
1491 ss = avl_last(&vd->vdev_dtl_map.sm_root);
1492 thismax = ss->ss_end;
1493 needed = B_TRUE;
1494 }
1495 mutex_exit(&vd->vdev_dtl_lock);
1496 } else {
1497 int c;
1498 for (c = 0; c < vd->vdev_children; c++) {
1499 vdev_t *cvd = vd->vdev_child[c];
1500 uint64_t cmin, cmax;
1501
1502 if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
1503 thismin = MIN(thismin, cmin);
1504 thismax = MAX(thismax, cmax);
1505 needed = B_TRUE;
1506 }
1507 }
1508 }
1509
1510 if (needed && minp) {
1511 *minp = thismin;
1512 *maxp = thismax;
1513 }
1514 return (needed);
1515}
1516
1293void
1294vdev_load(vdev_t *vd)
1295{
1296 int c;
1297
1298 /*
1299 * Recursively load all children.
1300 */

--- 13 unchanged lines hidden (view full) ---

1314 * If this is a leaf vdev, load its DTL.
1315 */
1316 if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
1317 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1318 VDEV_AUX_CORRUPT_DATA);
1319}
1320
1321/*
1517void
1518vdev_load(vdev_t *vd)
1519{
1520 int c;
1521
1522 /*
1523 * Recursively load all children.
1524 */

--- 13 unchanged lines hidden (view full) ---

1538 * If this is a leaf vdev, load its DTL.
1539 */
1540 if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
1541 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1542 VDEV_AUX_CORRUPT_DATA);
1543}
1544
1545/*
1322 * This special case of vdev_spare() is used for hot spares. It's sole purpose
1323 * it to set the vdev state for the associated vdev. To do this, we make sure
1324 * that we can open the underlying device, then try to read the label, and make
1325 * sure that the label is sane and that it hasn't been repurposed to another
1326 * pool.
1546 * The special vdev case is used for hot spares and l2cache devices. Its
1547 * sole purpose it to set the vdev state for the associated vdev. To do this,
1548 * we make sure that we can open the underlying device, then try to read the
1549 * label, and make sure that the label is sane and that it hasn't been
1550 * repurposed to another pool.
1327 */
1328int
1551 */
1552int
1329vdev_validate_spare(vdev_t *vd)
1553vdev_validate_aux(vdev_t *vd)
1330{
1331 nvlist_t *label;
1332 uint64_t guid, version;
1333 uint64_t state;
1334
1554{
1555 nvlist_t *label;
1556 uint64_t guid, version;
1557 uint64_t state;
1558
1559 if (!vdev_readable(vd))
1560 return (0);
1561
1335 if ((label = vdev_label_read_config(vd)) == NULL) {
1336 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1337 VDEV_AUX_CORRUPT_DATA);
1338 return (-1);
1339 }
1340
1341 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
1562 if ((label = vdev_label_read_config(vd)) == NULL) {
1563 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1564 VDEV_AUX_CORRUPT_DATA);
1565 return (-1);
1566 }
1567
1568 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
1342 version > ZFS_VERSION ||
1569 version > SPA_VERSION ||
1343 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
1344 guid != vd->vdev_guid ||
1345 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
1346 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1347 VDEV_AUX_CORRUPT_DATA);
1348 nvlist_free(label);
1349 return (-1);
1350 }
1351
1570 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
1571 guid != vd->vdev_guid ||
1572 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
1573 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1574 VDEV_AUX_CORRUPT_DATA);
1575 nvlist_free(label);
1576 return (-1);
1577 }
1578
1352 spa_spare_add(vd);
1353
1354 /*
1355 * We don't actually check the pool state here. If it's in fact in
1356 * use by another pool, we update this fact on the fly when requested.
1357 */
1358 nvlist_free(label);
1359 return (0);
1360}
1361
1362void
1363vdev_sync_done(vdev_t *vd, uint64_t txg)
1364{
1365 metaslab_t *msp;
1366
1579 /*
1580 * We don't actually check the pool state here. If it's in fact in
1581 * use by another pool, we update this fact on the fly when requested.
1582 */
1583 nvlist_free(label);
1584 return (0);
1585}
1586
1587void
1588vdev_sync_done(vdev_t *vd, uint64_t txg)
1589{
1590 metaslab_t *msp;
1591
1367 dprintf("%s txg %llu\n", vdev_description(vd), txg);
1368
1369 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1370 metaslab_sync_done(msp, txg);
1371}
1372
1373void
1374vdev_sync(vdev_t *vd, uint64_t txg)
1375{
1376 spa_t *spa = vd->vdev_spa;
1377 vdev_t *lvd;
1378 metaslab_t *msp;
1379 dmu_tx_t *tx;
1380
1592 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1593 metaslab_sync_done(msp, txg);
1594}
1595
1596void
1597vdev_sync(vdev_t *vd, uint64_t txg)
1598{
1599 spa_t *spa = vd->vdev_spa;
1600 vdev_t *lvd;
1601 metaslab_t *msp;
1602 dmu_tx_t *tx;
1603
1381 dprintf("%s txg %llu pass %d\n",
1382 vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa));
1383
1384 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
1385 ASSERT(vd == vd->vdev_top);
1386 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1387 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
1388 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
1389 ASSERT(vd->vdev_ms_array != 0);
1390 vdev_config_dirty(vd);
1391 dmu_tx_commit(tx);

--- 11 unchanged lines hidden (view full) ---

1403}
1404
1405uint64_t
1406vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
1407{
1408 return (vd->vdev_ops->vdev_op_asize(vd, psize));
1409}
1410
1604 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
1605 ASSERT(vd == vd->vdev_top);
1606 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1607 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
1608 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
1609 ASSERT(vd->vdev_ms_array != 0);
1610 vdev_config_dirty(vd);
1611 dmu_tx_commit(tx);

--- 11 unchanged lines hidden (view full) ---

1623}
1624
1625uint64_t
1626vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
1627{
1628 return (vd->vdev_ops->vdev_op_asize(vd, psize));
1629}
1630
1411void
1412vdev_io_start(zio_t *zio)
1631/*
1632 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
1633 * not be opened, and no I/O is attempted.
1634 */
1635int
1636vdev_fault(spa_t *spa, uint64_t guid)
1413{
1637{
1414 zio->io_vd->vdev_ops->vdev_op_io_start(zio);
1415}
1638 vdev_t *vd;
1416
1639
1417void
1418vdev_io_done(zio_t *zio)
1419{
1420 zio->io_vd->vdev_ops->vdev_op_io_done(zio);
1640 spa_vdev_state_enter(spa);
1641
1642 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1643 return (spa_vdev_state_exit(spa, NULL, ENODEV));
1644
1645 if (!vd->vdev_ops->vdev_op_leaf)
1646 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1647
1648 /*
1649 * Faulted state takes precedence over degraded.
1650 */
1651 vd->vdev_faulted = 1ULL;
1652 vd->vdev_degraded = 0ULL;
1653 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, VDEV_AUX_ERR_EXCEEDED);
1654
1655 /*
1656 * If marking the vdev as faulted cause the top-level vdev to become
1657 * unavailable, then back off and simply mark the vdev as degraded
1658 * instead.
1659 */
1660 if (vdev_is_dead(vd->vdev_top) && vd->vdev_aux == NULL) {
1661 vd->vdev_degraded = 1ULL;
1662 vd->vdev_faulted = 0ULL;
1663
1664 /*
1665 * If we reopen the device and it's not dead, only then do we
1666 * mark it degraded.
1667 */
1668 vdev_reopen(vd);
1669
1670 if (vdev_readable(vd)) {
1671 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
1672 VDEV_AUX_ERR_EXCEEDED);
1673 }
1674 }
1675
1676 return (spa_vdev_state_exit(spa, vd, 0));
1421}
1422
1677}
1678
1423const char *
1424vdev_description(vdev_t *vd)
1679/*
1680 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
1681 * user that something is wrong. The vdev continues to operate as normal as far
1682 * as I/O is concerned.
1683 */
1684int
1685vdev_degrade(spa_t *spa, uint64_t guid)
1425{
1686{
1426 if (vd == NULL || vd->vdev_ops == NULL)
1427 return ("<unknown>");
1687 vdev_t *vd;
1428
1688
1429 if (vd->vdev_path != NULL)
1430 return (vd->vdev_path);
1689 spa_vdev_state_enter(spa);
1431
1690
1432 if (vd->vdev_parent == NULL)
1433 return (spa_name(vd->vdev_spa));
1691 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1692 return (spa_vdev_state_exit(spa, NULL, ENODEV));
1434
1693
1435 return (vd->vdev_ops->vdev_op_type);
1694 if (!vd->vdev_ops->vdev_op_leaf)
1695 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1696
1697 /*
1698 * If the vdev is already faulted, then don't do anything.
1699 */
1700 if (vd->vdev_faulted || vd->vdev_degraded)
1701 return (spa_vdev_state_exit(spa, NULL, 0));
1702
1703 vd->vdev_degraded = 1ULL;
1704 if (!vdev_is_dead(vd))
1705 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
1706 VDEV_AUX_ERR_EXCEEDED);
1707
1708 return (spa_vdev_state_exit(spa, vd, 0));
1436}
1437
1709}
1710
1711/*
1712 * Online the given vdev. If 'unspare' is set, it implies two things. First,
1713 * any attached spare device should be detached when the device finishes
1714 * resilvering. Second, the online should be treated like a 'test' online case,
1715 * so no FMA events are generated if the device fails to open.
1716 */
1438int
1717int
1439vdev_online(spa_t *spa, uint64_t guid)
1718vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
1440{
1719{
1441 vdev_t *rvd, *vd;
1442 uint64_t txg;
1720 vdev_t *vd;
1443
1721
1444 txg = spa_vdev_enter(spa);
1722 spa_vdev_state_enter(spa);
1445
1723
1446 rvd = spa->spa_root_vdev;
1724 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1725 return (spa_vdev_state_exit(spa, NULL, ENODEV));
1447
1726
1448 if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1449 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1450
1451 if (!vd->vdev_ops->vdev_op_leaf)
1727 if (!vd->vdev_ops->vdev_op_leaf)
1452 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1728 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1453
1729
1454 dprintf("ONLINE: %s\n", vdev_description(vd));
1455
1456 vd->vdev_offline = B_FALSE;
1457 vd->vdev_tmpoffline = B_FALSE;
1730 vd->vdev_offline = B_FALSE;
1731 vd->vdev_tmpoffline = B_FALSE;
1732 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
1733 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
1458 vdev_reopen(vd->vdev_top);
1734 vdev_reopen(vd->vdev_top);
1735 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
1459
1736
1460 vdev_config_dirty(vd->vdev_top);
1737 if (newstate)
1738 *newstate = vd->vdev_state;
1739 if ((flags & ZFS_ONLINE_UNSPARE) &&
1740 !vdev_is_dead(vd) && vd->vdev_parent &&
1741 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
1742 vd->vdev_parent->vdev_child[0] == vd)
1743 vd->vdev_unspare = B_TRUE;
1461
1744
1462 (void) spa_vdev_exit(spa, NULL, txg, 0);
1745 (void) spa_vdev_state_exit(spa, vd, 0);
1463
1746
1464 VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
1747 VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
1465
1466 return (0);
1467}
1468
1469int
1748
1749 return (0);
1750}
1751
1752int
1470vdev_offline(spa_t *spa, uint64_t guid, int istmp)
1753vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
1471{
1754{
1472 vdev_t *rvd, *vd;
1473 uint64_t txg;
1755 vdev_t *vd;
1474
1756
1475 txg = spa_vdev_enter(spa);
1757 spa_vdev_state_enter(spa);
1476
1758
1477 rvd = spa->spa_root_vdev;
1759 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1760 return (spa_vdev_state_exit(spa, NULL, ENODEV));
1478
1761
1479 if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1480 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1481
1482 if (!vd->vdev_ops->vdev_op_leaf)
1762 if (!vd->vdev_ops->vdev_op_leaf)
1483 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1763 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1484
1764
1485 dprintf("OFFLINE: %s\n", vdev_description(vd));
1486
1487 /*
1488 * If the device isn't already offline, try to offline it.
1489 */
1490 if (!vd->vdev_offline) {
1491 /*
1492 * If this device's top-level vdev has a non-empty DTL,
1493 * don't allow the device to be offlined.
1494 *
1495 * XXX -- make this more precise by allowing the offline
1496 * as long as the remaining devices don't have any DTL holes.
1497 */
1498 if (vd->vdev_top->vdev_dtl_map.sm_space != 0)
1765 /*
1766 * If the device isn't already offline, try to offline it.
1767 */
1768 if (!vd->vdev_offline) {
1769 /*
1770 * If this device's top-level vdev has a non-empty DTL,
1771 * don't allow the device to be offlined.
1772 *
1773 * XXX -- make this more precise by allowing the offline
1774 * as long as the remaining devices don't have any DTL holes.
1775 */
1776 if (vd->vdev_top->vdev_dtl_map.sm_space != 0)
1499 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1777 return (spa_vdev_state_exit(spa, NULL, EBUSY));
1500
1501 /*
1502 * Offline this device and reopen its top-level vdev.
1503 * If this action results in the top-level vdev becoming
1504 * unusable, undo it and fail the request.
1505 */
1506 vd->vdev_offline = B_TRUE;
1507 vdev_reopen(vd->vdev_top);
1778
1779 /*
1780 * Offline this device and reopen its top-level vdev.
1781 * If this action results in the top-level vdev becoming
1782 * unusable, undo it and fail the request.
1783 */
1784 vd->vdev_offline = B_TRUE;
1785 vdev_reopen(vd->vdev_top);
1508 if (vdev_is_dead(vd->vdev_top)) {
1786 if (vdev_is_dead(vd->vdev_top) && vd->vdev_aux == NULL) {
1509 vd->vdev_offline = B_FALSE;
1510 vdev_reopen(vd->vdev_top);
1787 vd->vdev_offline = B_FALSE;
1788 vdev_reopen(vd->vdev_top);
1511 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1789 return (spa_vdev_state_exit(spa, NULL, EBUSY));
1512 }
1513 }
1514
1790 }
1791 }
1792
1515 vd->vdev_tmpoffline = istmp;
1793 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
1516
1794
1517 vdev_config_dirty(vd->vdev_top);
1518
1519 return (spa_vdev_exit(spa, NULL, txg, 0));
1795 return (spa_vdev_state_exit(spa, vd, 0));
1520}
1521
1522/*
1523 * Clear the error counts associated with this vdev. Unlike vdev_online() and
1524 * vdev_offline(), we assume the spa config is locked. We also clear all
1525 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
1526 */
1527void
1528vdev_clear(spa_t *spa, vdev_t *vd)
1529{
1796}
1797
1798/*
1799 * Clear the error counts associated with this vdev. Unlike vdev_online() and
1800 * vdev_offline(), we assume the spa config is locked. We also clear all
1801 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
1802 */
1803void
1804vdev_clear(spa_t *spa, vdev_t *vd)
1805{
1530 int c;
1806 vdev_t *rvd = spa->spa_root_vdev;
1531
1807
1808 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1809
1532 if (vd == NULL)
1810 if (vd == NULL)
1533 vd = spa->spa_root_vdev;
1811 vd = rvd;
1534
1535 vd->vdev_stat.vs_read_errors = 0;
1536 vd->vdev_stat.vs_write_errors = 0;
1537 vd->vdev_stat.vs_checksum_errors = 0;
1538
1812
1813 vd->vdev_stat.vs_read_errors = 0;
1814 vd->vdev_stat.vs_write_errors = 0;
1815 vd->vdev_stat.vs_checksum_errors = 0;
1816
1539 for (c = 0; c < vd->vdev_children; c++)
1817 for (int c = 0; c < vd->vdev_children; c++)
1540 vdev_clear(spa, vd->vdev_child[c]);
1818 vdev_clear(spa, vd->vdev_child[c]);
1819
1820 /*
1821 * If we're in the FAULTED state or have experienced failed I/O, then
1822 * clear the persistent state and attempt to reopen the device. We
1823 * also mark the vdev config dirty, so that the new faulted state is
1824 * written out to disk.
1825 */
1826 if (vd->vdev_faulted || vd->vdev_degraded ||
1827 !vdev_readable(vd) || !vdev_writeable(vd)) {
1828
1829 vd->vdev_faulted = vd->vdev_degraded = 0;
1830 vd->vdev_cant_read = B_FALSE;
1831 vd->vdev_cant_write = B_FALSE;
1832
1833 vdev_reopen(vd);
1834
1835 if (vd != rvd)
1836 vdev_state_dirty(vd->vdev_top);
1837
1838 if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
1839 spa_async_request(spa, SPA_ASYNC_RESILVER);
1840
1841 spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
1842 }
1541}
1542
1843}
1844
1543int
1845boolean_t
1544vdev_is_dead(vdev_t *vd)
1545{
1846vdev_is_dead(vdev_t *vd)
1847{
1546 return (vd->vdev_state <= VDEV_STATE_CANT_OPEN);
1848 return (vd->vdev_state < VDEV_STATE_DEGRADED);
1547}
1548
1849}
1850
1549int
1550vdev_error_inject(vdev_t *vd, zio_t *zio)
1851boolean_t
1852vdev_readable(vdev_t *vd)
1551{
1853{
1552 int error = 0;
1854 return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
1855}
1553
1856
1554 if (vd->vdev_fault_mode == VDEV_FAULT_NONE)
1555 return (0);
1857boolean_t
1858vdev_writeable(vdev_t *vd)
1859{
1860 return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
1861}
1556
1862
1557 if (((1ULL << zio->io_type) & vd->vdev_fault_mask) == 0)
1558 return (0);
1863boolean_t
1864vdev_accessible(vdev_t *vd, zio_t *zio)
1865{
1866 ASSERT(zio->io_vd == vd);
1559
1867
1560 switch (vd->vdev_fault_mode) {
1561 case VDEV_FAULT_RANDOM:
1562 if (spa_get_random(vd->vdev_fault_arg) == 0)
1563 error = EIO;
1564 break;
1868 if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
1869 return (B_FALSE);
1565
1870
1566 case VDEV_FAULT_COUNT:
1567 if ((int64_t)--vd->vdev_fault_arg <= 0)
1568 vd->vdev_fault_mode = VDEV_FAULT_NONE;
1569 error = EIO;
1570 break;
1571 }
1871 if (zio->io_type == ZIO_TYPE_READ)
1872 return (!vd->vdev_cant_read);
1572
1873
1573 if (error != 0) {
1574 dprintf("returning %d for type %d on %s state %d offset %llx\n",
1575 error, zio->io_type, vdev_description(vd),
1576 vd->vdev_state, zio->io_offset);
1577 }
1874 if (zio->io_type == ZIO_TYPE_WRITE)
1875 return (!vd->vdev_cant_write);
1578
1876
1579 return (error);
1877 return (B_TRUE);
1580}
1581
1582/*
1583 * Get statistics for the given vdev.
1584 */
1585void
1586vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
1587{
1588 vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1878}
1879
1880/*
1881 * Get statistics for the given vdev.
1882 */
1883void
1884vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
1885{
1886 vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1589 int c, t;
1590
1591 mutex_enter(&vd->vdev_stat_lock);
1592 bcopy(&vd->vdev_stat, vs, sizeof (*vs));
1887
1888 mutex_enter(&vd->vdev_stat_lock);
1889 bcopy(&vd->vdev_stat, vs, sizeof (*vs));
1890 vs->vs_scrub_errors = vd->vdev_spa->spa_scrub_errors;
1593 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
1594 vs->vs_state = vd->vdev_state;
1595 vs->vs_rsize = vdev_get_rsize(vd);
1596 mutex_exit(&vd->vdev_stat_lock);
1597
1598 /*
1599 * If we're getting stats on the root vdev, aggregate the I/O counts
1600 * over all top-level vdevs (i.e. the direct children of the root).
1601 */
1602 if (vd == rvd) {
1891 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
1892 vs->vs_state = vd->vdev_state;
1893 vs->vs_rsize = vdev_get_rsize(vd);
1894 mutex_exit(&vd->vdev_stat_lock);
1895
1896 /*
1897 * If we're getting stats on the root vdev, aggregate the I/O counts
1898 * over all top-level vdevs (i.e. the direct children of the root).
1899 */
1900 if (vd == rvd) {
1603 for (c = 0; c < rvd->vdev_children; c++) {
1901 for (int c = 0; c < rvd->vdev_children; c++) {
1604 vdev_t *cvd = rvd->vdev_child[c];
1605 vdev_stat_t *cvs = &cvd->vdev_stat;
1606
1607 mutex_enter(&vd->vdev_stat_lock);
1902 vdev_t *cvd = rvd->vdev_child[c];
1903 vdev_stat_t *cvs = &cvd->vdev_stat;
1904
1905 mutex_enter(&vd->vdev_stat_lock);
1608 for (t = 0; t < ZIO_TYPES; t++) {
1906 for (int t = 0; t < ZIO_TYPES; t++) {
1609 vs->vs_ops[t] += cvs->vs_ops[t];
1610 vs->vs_bytes[t] += cvs->vs_bytes[t];
1611 }
1907 vs->vs_ops[t] += cvs->vs_ops[t];
1908 vs->vs_bytes[t] += cvs->vs_bytes[t];
1909 }
1612 vs->vs_read_errors += cvs->vs_read_errors;
1613 vs->vs_write_errors += cvs->vs_write_errors;
1614 vs->vs_checksum_errors += cvs->vs_checksum_errors;
1615 vs->vs_scrub_examined += cvs->vs_scrub_examined;
1910 vs->vs_scrub_examined += cvs->vs_scrub_examined;
1616 vs->vs_scrub_errors += cvs->vs_scrub_errors;
1617 mutex_exit(&vd->vdev_stat_lock);
1618 }
1619 }
1620}
1621
1622void
1911 mutex_exit(&vd->vdev_stat_lock);
1912 }
1913 }
1914}
1915
1916void
1623vdev_stat_update(zio_t *zio)
1917vdev_clear_stats(vdev_t *vd)
1624{
1918{
1625 vdev_t *vd = zio->io_vd;
1919 mutex_enter(&vd->vdev_stat_lock);
1920 vd->vdev_stat.vs_space = 0;
1921 vd->vdev_stat.vs_dspace = 0;
1922 vd->vdev_stat.vs_alloc = 0;
1923 mutex_exit(&vd->vdev_stat_lock);
1924}
1925
1926void
1927vdev_stat_update(zio_t *zio, uint64_t psize)
1928{
1929 vdev_t *rvd = zio->io_spa->spa_root_vdev;
1930 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
1626 vdev_t *pvd;
1627 uint64_t txg = zio->io_txg;
1628 vdev_stat_t *vs = &vd->vdev_stat;
1629 zio_type_t type = zio->io_type;
1630 int flags = zio->io_flags;
1631
1931 vdev_t *pvd;
1932 uint64_t txg = zio->io_txg;
1933 vdev_stat_t *vs = &vd->vdev_stat;
1934 zio_type_t type = zio->io_type;
1935 int flags = zio->io_flags;
1936
1937 /*
1938 * If this i/o is a gang leader, it didn't do any actual work.
1939 */
1940 if (zio->io_gang_tree)
1941 return;
1942
1632 if (zio->io_error == 0) {
1943 if (zio->io_error == 0) {
1944 /*
1945 * If this is a root i/o, don't count it -- we've already
1946 * counted the top-level vdevs, and vdev_get_stats() will
1947 * aggregate them when asked. This reduces contention on
1948 * the root vdev_stat_lock and implicitly handles blocks
1949 * that compress away to holes, for which there is no i/o.
1950 * (Holes never create vdev children, so all the counters
1951 * remain zero, which is what we want.)
1952 *
1953 * Note: this only applies to successful i/o (io_error == 0)
1954 * because unlike i/o counts, errors are not additive.
1955 * When reading a ditto block, for example, failure of
1956 * one top-level vdev does not imply a root-level error.
1957 */
1958 if (vd == rvd)
1959 return;
1960
1961 ASSERT(vd == zio->io_vd);
1633 if (!(flags & ZIO_FLAG_IO_BYPASS)) {
1634 mutex_enter(&vd->vdev_stat_lock);
1635 vs->vs_ops[type]++;
1962 if (!(flags & ZIO_FLAG_IO_BYPASS)) {
1963 mutex_enter(&vd->vdev_stat_lock);
1964 vs->vs_ops[type]++;
1636 vs->vs_bytes[type] += zio->io_size;
1965 vs->vs_bytes[type] += psize;
1637 mutex_exit(&vd->vdev_stat_lock);
1638 }
1966 mutex_exit(&vd->vdev_stat_lock);
1967 }
1639 if ((flags & ZIO_FLAG_IO_REPAIR) &&
1640 zio->io_delegate_list == NULL) {
1968 if (flags & ZIO_FLAG_IO_REPAIR) {
1969 ASSERT(zio->io_delegate_list == NULL);
1641 mutex_enter(&vd->vdev_stat_lock);
1642 if (flags & ZIO_FLAG_SCRUB_THREAD)
1970 mutex_enter(&vd->vdev_stat_lock);
1971 if (flags & ZIO_FLAG_SCRUB_THREAD)
1643 vs->vs_scrub_repaired += zio->io_size;
1972 vs->vs_scrub_repaired += psize;
1644 else
1973 else
1645 vs->vs_self_healed += zio->io_size;
1974 vs->vs_self_healed += psize;
1646 mutex_exit(&vd->vdev_stat_lock);
1647 }
1648 return;
1649 }
1650
1651 if (flags & ZIO_FLAG_SPECULATIVE)
1652 return;
1653
1975 mutex_exit(&vd->vdev_stat_lock);
1976 }
1977 return;
1978 }
1979
1980 if (flags & ZIO_FLAG_SPECULATIVE)
1981 return;
1982
1654 if (!vdev_is_dead(vd)) {
1655 mutex_enter(&vd->vdev_stat_lock);
1656 if (type == ZIO_TYPE_READ) {
1657 if (zio->io_error == ECKSUM)
1658 vs->vs_checksum_errors++;
1659 else
1660 vs->vs_read_errors++;
1661 }
1662 if (type == ZIO_TYPE_WRITE)
1663 vs->vs_write_errors++;
1664 mutex_exit(&vd->vdev_stat_lock);
1983 mutex_enter(&vd->vdev_stat_lock);
1984 if (type == ZIO_TYPE_READ) {
1985 if (zio->io_error == ECKSUM)
1986 vs->vs_checksum_errors++;
1987 else
1988 vs->vs_read_errors++;
1665 }
1989 }
1990 if (type == ZIO_TYPE_WRITE)
1991 vs->vs_write_errors++;
1992 mutex_exit(&vd->vdev_stat_lock);
1666
1993
1667 if (type == ZIO_TYPE_WRITE) {
1668 if (txg == 0 || vd->vdev_children != 0)
1669 return;
1994 if (type == ZIO_TYPE_WRITE && txg != 0 && vd->vdev_children == 0) {
1670 if (flags & ZIO_FLAG_SCRUB_THREAD) {
1671 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
1672 for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1673 vdev_dtl_dirty(&pvd->vdev_dtl_scrub, txg, 1);
1674 }
1675 if (!(flags & ZIO_FLAG_IO_REPAIR)) {
1676 if (vdev_dtl_contains(&vd->vdev_dtl_map, txg, 1))
1677 return;

--- 22 unchanged lines hidden (view full) ---

1700 */
1701 vs->vs_scrub_complete = complete;
1702 vs->vs_scrub_end = gethrestime_sec();
1703 } else {
1704 vs->vs_scrub_type = type;
1705 vs->vs_scrub_complete = 0;
1706 vs->vs_scrub_examined = 0;
1707 vs->vs_scrub_repaired = 0;
1995 if (flags & ZIO_FLAG_SCRUB_THREAD) {
1996 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
1997 for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1998 vdev_dtl_dirty(&pvd->vdev_dtl_scrub, txg, 1);
1999 }
2000 if (!(flags & ZIO_FLAG_IO_REPAIR)) {
2001 if (vdev_dtl_contains(&vd->vdev_dtl_map, txg, 1))
2002 return;

--- 22 unchanged lines hidden (view full) ---

2025 */
2026 vs->vs_scrub_complete = complete;
2027 vs->vs_scrub_end = gethrestime_sec();
2028 } else {
2029 vs->vs_scrub_type = type;
2030 vs->vs_scrub_complete = 0;
2031 vs->vs_scrub_examined = 0;
2032 vs->vs_scrub_repaired = 0;
1708 vs->vs_scrub_errors = 0;
1709 vs->vs_scrub_start = gethrestime_sec();
1710 vs->vs_scrub_end = 0;
1711 }
1712
1713 mutex_exit(&vd->vdev_stat_lock);
1714}
1715
1716/*
1717 * Update the in-core space usage stats for this vdev and the root vdev.
1718 */
1719void
2033 vs->vs_scrub_start = gethrestime_sec();
2034 vs->vs_scrub_end = 0;
2035 }
2036
2037 mutex_exit(&vd->vdev_stat_lock);
2038}
2039
2040/*
2041 * Update the in-core space usage stats for this vdev and the root vdev.
2042 */
2043void
1720vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta)
2044vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta,
2045 boolean_t update_root)
1721{
2046{
1722 ASSERT(vd == vd->vdev_top);
1723 int64_t dspace_delta = space_delta;
2047 int64_t dspace_delta = space_delta;
2048 spa_t *spa = vd->vdev_spa;
2049 vdev_t *rvd = spa->spa_root_vdev;
1724
2050
1725 do {
1726 if (vd->vdev_ms_count) {
1727 /*
1728 * If this is a top-level vdev, apply the
1729 * inverse of its psize-to-asize (ie. RAID-Z)
1730 * space-expansion factor. We must calculate
1731 * this here and not at the root vdev because
1732 * the root vdev's psize-to-asize is simply the
1733 * max of its childrens', thus not accurate
1734 * enough for us.
1735 */
1736 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
1737 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
1738 vd->vdev_deflate_ratio;
1739 }
2051 ASSERT(vd == vd->vdev_top);
1740
2052
1741 mutex_enter(&vd->vdev_stat_lock);
1742 vd->vdev_stat.vs_space += space_delta;
1743 vd->vdev_stat.vs_alloc += alloc_delta;
1744 vd->vdev_stat.vs_dspace += dspace_delta;
1745 mutex_exit(&vd->vdev_stat_lock);
1746 } while ((vd = vd->vdev_parent) != NULL);
2053 /*
2054 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
2055 * factor. We must calculate this here and not at the root vdev
2056 * because the root vdev's psize-to-asize is simply the max of its
2057 * childrens', thus not accurate enough for us.
2058 */
2059 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
2060 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
2061 vd->vdev_deflate_ratio;
2062
2063 mutex_enter(&vd->vdev_stat_lock);
2064 vd->vdev_stat.vs_space += space_delta;
2065 vd->vdev_stat.vs_alloc += alloc_delta;
2066 vd->vdev_stat.vs_dspace += dspace_delta;
2067 mutex_exit(&vd->vdev_stat_lock);
2068
2069 if (update_root) {
2070 ASSERT(rvd == vd->vdev_parent);
2071 ASSERT(vd->vdev_ms_count != 0);
2072
2073 /*
2074 * Don't count non-normal (e.g. intent log) space as part of
2075 * the pool's capacity.
2076 */
2077 if (vd->vdev_mg->mg_class != spa->spa_normal_class)
2078 return;
2079
2080 mutex_enter(&rvd->vdev_stat_lock);
2081 rvd->vdev_stat.vs_space += space_delta;
2082 rvd->vdev_stat.vs_alloc += alloc_delta;
2083 rvd->vdev_stat.vs_dspace += dspace_delta;
2084 mutex_exit(&rvd->vdev_stat_lock);
2085 }
1747}
1748
1749/*
1750 * Mark a top-level vdev's config as dirty, placing it on the dirty list
1751 * so that it will be written out next time the vdev configuration is synced.
1752 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
1753 */
1754void
1755vdev_config_dirty(vdev_t *vd)
1756{
1757 spa_t *spa = vd->vdev_spa;
1758 vdev_t *rvd = spa->spa_root_vdev;
1759 int c;
1760
1761 /*
2086}
2087
2088/*
2089 * Mark a top-level vdev's config as dirty, placing it on the dirty list
2090 * so that it will be written out next time the vdev configuration is synced.
2091 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2092 */
2093void
2094vdev_config_dirty(vdev_t *vd)
2095{
2096 spa_t *spa = vd->vdev_spa;
2097 vdev_t *rvd = spa->spa_root_vdev;
2098 int c;
2099
2100 /*
1762 * The dirty list is protected by the config lock. The caller must
1763 * either hold the config lock as writer, or must be the sync thread
1764 * (which holds the lock as reader). There's only one sync thread,
2101 * If this is an aux vdev (as with l2cache devices), then we update the
2102 * vdev config manually and set the sync flag.
2103 */
2104 if (vd->vdev_aux != NULL) {
2105 spa_aux_vdev_t *sav = vd->vdev_aux;
2106 nvlist_t **aux;
2107 uint_t naux;
2108
2109 for (c = 0; c < sav->sav_count; c++) {
2110 if (sav->sav_vdevs[c] == vd)
2111 break;
2112 }
2113
2114 if (c == sav->sav_count) {
2115 /*
2116 * We're being removed. There's nothing more to do.
2117 */
2118 ASSERT(sav->sav_sync == B_TRUE);
2119 return;
2120 }
2121
2122 sav->sav_sync = B_TRUE;
2123
2124 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
2125 ZPOOL_CONFIG_L2CACHE, &aux, &naux) == 0);
2126
2127 ASSERT(c < naux);
2128
2129 /*
2130 * Setting the nvlist in the middle if the array is a little
2131 * sketchy, but it will work.
2132 */
2133 nvlist_free(aux[c]);
2134 aux[c] = vdev_config_generate(spa, vd, B_TRUE, B_FALSE, B_TRUE);
2135
2136 return;
2137 }
2138
2139 /*
2140 * The dirty list is protected by the SCL_CONFIG lock. The caller
2141 * must either hold SCL_CONFIG as writer, or must be the sync thread
2142 * (which holds SCL_CONFIG as reader). There's only one sync thread,
1765 * so this is sufficient to ensure mutual exclusion.
1766 */
2143 * so this is sufficient to ensure mutual exclusion.
2144 */
1767 ASSERT(spa_config_held(spa, RW_WRITER) ||
1768 dsl_pool_sync_context(spa_get_dsl(spa)));
2145 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2146 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2147 spa_config_held(spa, SCL_CONFIG, RW_READER)));
1769
1770 if (vd == rvd) {
1771 for (c = 0; c < rvd->vdev_children; c++)
1772 vdev_config_dirty(rvd->vdev_child[c]);
1773 } else {
1774 ASSERT(vd == vd->vdev_top);
1775
2148
2149 if (vd == rvd) {
2150 for (c = 0; c < rvd->vdev_children; c++)
2151 vdev_config_dirty(rvd->vdev_child[c]);
2152 } else {
2153 ASSERT(vd == vd->vdev_top);
2154
1776 if (!list_link_active(&vd->vdev_dirty_node))
1777 list_insert_head(&spa->spa_dirty_list, vd);
2155 if (!list_link_active(&vd->vdev_config_dirty_node))
2156 list_insert_head(&spa->spa_config_dirty_list, vd);
1778 }
1779}
1780
1781void
1782vdev_config_clean(vdev_t *vd)
1783{
1784 spa_t *spa = vd->vdev_spa;
1785
2157 }
2158}
2159
2160void
2161vdev_config_clean(vdev_t *vd)
2162{
2163 spa_t *spa = vd->vdev_spa;
2164
1786 ASSERT(spa_config_held(spa, RW_WRITER) ||
1787 dsl_pool_sync_context(spa_get_dsl(spa)));
2165 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2166 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2167 spa_config_held(spa, SCL_CONFIG, RW_READER)));
1788
2168
1789 ASSERT(list_link_active(&vd->vdev_dirty_node));
1790 list_remove(&spa->spa_dirty_list, vd);
2169 ASSERT(list_link_active(&vd->vdev_config_dirty_node));
2170 list_remove(&spa->spa_config_dirty_list, vd);
1791}
1792
2171}
2172
2173/*
2174 * Mark a top-level vdev's state as dirty, so that the next pass of
2175 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
2176 * the state changes from larger config changes because they require
2177 * much less locking, and are often needed for administrative actions.
2178 */
1793void
2179void
2180vdev_state_dirty(vdev_t *vd)
2181{
2182 spa_t *spa = vd->vdev_spa;
2183
2184 ASSERT(vd == vd->vdev_top);
2185
2186 /*
2187 * The state list is protected by the SCL_STATE lock. The caller
2188 * must either hold SCL_STATE as writer, or must be the sync thread
2189 * (which holds SCL_STATE as reader). There's only one sync thread,
2190 * so this is sufficient to ensure mutual exclusion.
2191 */
2192 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
2193 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2194 spa_config_held(spa, SCL_STATE, RW_READER)));
2195
2196 if (!list_link_active(&vd->vdev_state_dirty_node))
2197 list_insert_head(&spa->spa_state_dirty_list, vd);
2198}
2199
2200void
2201vdev_state_clean(vdev_t *vd)
2202{
2203 spa_t *spa = vd->vdev_spa;
2204
2205 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
2206 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2207 spa_config_held(spa, SCL_STATE, RW_READER)));
2208
2209 ASSERT(list_link_active(&vd->vdev_state_dirty_node));
2210 list_remove(&spa->spa_state_dirty_list, vd);
2211}
2212
2213/*
2214 * Propagate vdev state up from children to parent.
2215 */
2216void
1794vdev_propagate_state(vdev_t *vd)
1795{
1796 vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1797 int degraded = 0, faulted = 0;
1798 int corrupted = 0;
1799 int c;
1800 vdev_t *child;
1801
2217vdev_propagate_state(vdev_t *vd)
2218{
2219 vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
2220 int degraded = 0, faulted = 0;
2221 int corrupted = 0;
2222 int c;
2223 vdev_t *child;
2224
1802 for (c = 0; c < vd->vdev_children; c++) {
1803 child = vd->vdev_child[c];
1804 if (child->vdev_state <= VDEV_STATE_CANT_OPEN)
1805 faulted++;
1806 else if (child->vdev_state == VDEV_STATE_DEGRADED)
1807 degraded++;
2225 if (vd->vdev_children > 0) {
2226 for (c = 0; c < vd->vdev_children; c++) {
2227 child = vd->vdev_child[c];
1808
2228
1809 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
1810 corrupted++;
1811 }
2229 if (!vdev_readable(child) ||
2230 (!vdev_writeable(child) && (spa_mode & FWRITE))) {
2231 /*
2232 * Root special: if there is a top-level log
2233 * device, treat the root vdev as if it were
2234 * degraded.
2235 */
2236 if (child->vdev_islog && vd == rvd)
2237 degraded++;
2238 else
2239 faulted++;
2240 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
2241 degraded++;
2242 }
1812
2243
1813 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
2244 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
2245 corrupted++;
2246 }
1814
2247
1815 /*
1816 * Root special: if there is a toplevel vdev that cannot be
1817 * opened due to corrupted metadata, then propagate the root
1818 * vdev's aux state as 'corrupt' rather than 'insufficient
1819 * replicas'.
1820 */
1821 if (corrupted && vd == rvd && rvd->vdev_state == VDEV_STATE_CANT_OPEN)
1822 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
1823 VDEV_AUX_CORRUPT_DATA);
2248 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
2249
2250 /*
2251 * Root special: if there is a top-level vdev that cannot be
2252 * opened due to corrupted metadata, then propagate the root
2253 * vdev's aux state as 'corrupt' rather than 'insufficient
2254 * replicas'.
2255 */
2256 if (corrupted && vd == rvd &&
2257 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
2258 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
2259 VDEV_AUX_CORRUPT_DATA);
2260 }
2261
2262 if (vd->vdev_parent)
2263 vdev_propagate_state(vd->vdev_parent);
1824}
1825
1826/*
1827 * Set a vdev's state. If this is during an open, we don't update the parent
1828 * state, because we're in the process of opening children depth-first.
1829 * Otherwise, we propagate the change to the parent.
1830 *
1831 * If this routine places a device in a faulted state, an appropriate ereport is
1832 * generated.
1833 */
1834void
1835vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
1836{
1837 uint64_t save_state;
2264}
2265
2266/*
2267 * Set a vdev's state. If this is during an open, we don't update the parent
2268 * state, because we're in the process of opening children depth-first.
2269 * Otherwise, we propagate the change to the parent.
2270 *
2271 * If this routine places a device in a faulted state, an appropriate ereport is
2272 * generated.
2273 */
2274void
2275vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
2276{
2277 uint64_t save_state;
2278 spa_t *spa = vd->vdev_spa;
1838
1839 if (state == vd->vdev_state) {
1840 vd->vdev_stat.vs_aux = aux;
1841 return;
1842 }
1843
1844 save_state = vd->vdev_state;
1845

--- 6 unchanged lines hidden (view full) ---

1852 * but invalid devices open forever. We don't call vdev_close() itself,
1853 * because that implies some extra checks (offline, etc) that we don't
1854 * want here. This is limited to leaf devices, because otherwise
1855 * closing the device will affect other children.
1856 */
1857 if (vdev_is_dead(vd) && vd->vdev_ops->vdev_op_leaf)
1858 vd->vdev_ops->vdev_op_close(vd);
1859
2279
2280 if (state == vd->vdev_state) {
2281 vd->vdev_stat.vs_aux = aux;
2282 return;
2283 }
2284
2285 save_state = vd->vdev_state;
2286

--- 6 unchanged lines hidden (view full) ---

2293 * but invalid devices open forever. We don't call vdev_close() itself,
2294 * because that implies some extra checks (offline, etc) that we don't
2295 * want here. This is limited to leaf devices, because otherwise
2296 * closing the device will affect other children.
2297 */
2298 if (vdev_is_dead(vd) && vd->vdev_ops->vdev_op_leaf)
2299 vd->vdev_ops->vdev_op_close(vd);
2300
1860 if (state == VDEV_STATE_CANT_OPEN) {
2301 if (vd->vdev_removed &&
2302 state == VDEV_STATE_CANT_OPEN &&
2303 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
1861 /*
2304 /*
2305 * If the previous state is set to VDEV_STATE_REMOVED, then this
2306 * device was previously marked removed and someone attempted to
2307 * reopen it. If this failed due to a nonexistent device, then
2308 * keep the device in the REMOVED state. We also let this be if
2309 * it is one of our special test online cases, which is only
2310 * attempting to online the device and shouldn't generate an FMA
2311 * fault.
2312 */
2313 vd->vdev_state = VDEV_STATE_REMOVED;
2314 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2315 } else if (state == VDEV_STATE_REMOVED) {
2316 /*
2317 * Indicate to the ZFS DE that this device has been removed, and
2318 * any recent errors should be ignored.
2319 */
2320 zfs_post_remove(spa, vd);
2321 vd->vdev_removed = B_TRUE;
2322 } else if (state == VDEV_STATE_CANT_OPEN) {
2323 /*
1862 * If we fail to open a vdev during an import, we mark it as
1863 * "not available", which signifies that it was never there to
1864 * begin with. Failure to open such a device is not considered
1865 * an error.
1866 */
2324 * If we fail to open a vdev during an import, we mark it as
2325 * "not available", which signifies that it was never there to
2326 * begin with. Failure to open such a device is not considered
2327 * an error.
2328 */
1867 if (vd->vdev_spa->spa_load_state == SPA_LOAD_IMPORT &&
2329 if (spa->spa_load_state == SPA_LOAD_IMPORT &&
2330 !spa->spa_import_faulted &&
1868 vd->vdev_ops->vdev_op_leaf)
1869 vd->vdev_not_present = 1;
1870
1871 /*
1872 * Post the appropriate ereport. If the 'prevstate' field is
1873 * set to something other than VDEV_STATE_UNKNOWN, it indicates
1874 * that this is part of a vdev_reopen(). In this case, we don't
1875 * want to post the ereport if the device was already in the
1876 * CANT_OPEN state beforehand.
2331 vd->vdev_ops->vdev_op_leaf)
2332 vd->vdev_not_present = 1;
2333
2334 /*
2335 * Post the appropriate ereport. If the 'prevstate' field is
2336 * set to something other than VDEV_STATE_UNKNOWN, it indicates
2337 * that this is part of a vdev_reopen(). In this case, we don't
2338 * want to post the ereport if the device was already in the
2339 * CANT_OPEN state beforehand.
2340 *
2341 * If the 'checkremove' flag is set, then this is an attempt to
2342 * online the device in response to an insertion event. If we
2343 * hit this case, then we have detected an insertion event for a
2344 * faulted or offline device that wasn't in the removed state.
2345 * In this scenario, we don't post an ereport because we are
2346 * about to replace the device, or attempt an online with
2347 * vdev_forcefault, which will generate the fault for us.
1877 */
2348 */
1878 if (vd->vdev_prevstate != state && !vd->vdev_not_present &&
1879 vd != vd->vdev_spa->spa_root_vdev) {
2349 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
2350 !vd->vdev_not_present && !vd->vdev_checkremove &&
2351 vd != spa->spa_root_vdev) {
1880 const char *class;
1881
1882 switch (aux) {
1883 case VDEV_AUX_OPEN_FAILED:
1884 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
1885 break;
1886 case VDEV_AUX_CORRUPT_DATA:
1887 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;

--- 5 unchanged lines hidden (view full) ---

1893 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
1894 break;
1895 case VDEV_AUX_TOO_SMALL:
1896 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
1897 break;
1898 case VDEV_AUX_BAD_LABEL:
1899 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
1900 break;
2352 const char *class;
2353
2354 switch (aux) {
2355 case VDEV_AUX_OPEN_FAILED:
2356 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
2357 break;
2358 case VDEV_AUX_CORRUPT_DATA:
2359 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;

--- 5 unchanged lines hidden (view full) ---

2365 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
2366 break;
2367 case VDEV_AUX_TOO_SMALL:
2368 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
2369 break;
2370 case VDEV_AUX_BAD_LABEL:
2371 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
2372 break;
2373 case VDEV_AUX_IO_FAILURE:
2374 class = FM_EREPORT_ZFS_IO_FAILURE;
2375 break;
1901 default:
1902 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
1903 }
1904
2376 default:
2377 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
2378 }
2379
1905 zfs_ereport_post(class, vd->vdev_spa,
1906 vd, NULL, save_state, 0);
2380 zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
1907 }
2381 }
2382
2383 /* Erase any notion of persistent removed state */
2384 vd->vdev_removed = B_FALSE;
2385 } else {
2386 vd->vdev_removed = B_FALSE;
1908 }
1909
2387 }
2388
1910 if (isopen)
1911 return;
2389 if (!isopen)
2390 vdev_propagate_state(vd);
2391}
1912
2392
1913 if (vd->vdev_parent != NULL)
1914 vdev_propagate_state(vd->vdev_parent);
2393/*
2394 * Check the vdev configuration to ensure that it's capable of supporting
2395 * a root pool. Currently, we do not support RAID-Z or partial configuration.
2396 * In addition, only a single top-level vdev is allowed and none of the leaves
2397 * can be wholedisks.
2398 */
2399boolean_t
2400vdev_is_bootable(vdev_t *vd)
2401{
2402 int c;
2403
2404 if (!vd->vdev_ops->vdev_op_leaf) {
2405 char *vdev_type = vd->vdev_ops->vdev_op_type;
2406
2407 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
2408 vd->vdev_children > 1) {
2409 return (B_FALSE);
2410 } else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
2411 strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
2412 return (B_FALSE);
2413 }
2414 } else if (vd->vdev_wholedisk == 1) {
2415 return (B_FALSE);
2416 }
2417
2418 for (c = 0; c < vd->vdev_children; c++) {
2419 if (!vdev_is_bootable(vd->vdev_child[c]))
2420 return (B_FALSE);
2421 }
2422 return (B_TRUE);
1915}
2423}