]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/vdev.c
FreeBSD rS271776 - Persist vdev_resilver_txg changes
[mirror_zfs.git] / module / zfs / vdev.c
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
26 */
27
28 #include <sys/zfs_context.h>
29 #include <sys/fm/fs/zfs.h>
30 #include <sys/spa.h>
31 #include <sys/spa_impl.h>
32 #include <sys/dmu.h>
33 #include <sys/dmu_tx.h>
34 #include <sys/vdev_impl.h>
35 #include <sys/uberblock_impl.h>
36 #include <sys/metaslab.h>
37 #include <sys/metaslab_impl.h>
38 #include <sys/space_map.h>
39 #include <sys/space_reftree.h>
40 #include <sys/zio.h>
41 #include <sys/zap.h>
42 #include <sys/fs/zfs.h>
43 #include <sys/arc.h>
44 #include <sys/zil.h>
45 #include <sys/dsl_scan.h>
46 #include <sys/zvol.h>
47
48 /*
49 * When a vdev is added, it will be divided into approximately (but no
50 * more than) this number of metaslabs.
51 */
52 int metaslabs_per_vdev = 200;
53
54 /*
55 * Virtual device management.
56 */
57
58 static vdev_ops_t *vdev_ops_table[] = {
59 &vdev_root_ops,
60 &vdev_raidz_ops,
61 &vdev_mirror_ops,
62 &vdev_replacing_ops,
63 &vdev_spare_ops,
64 &vdev_disk_ops,
65 &vdev_file_ops,
66 &vdev_missing_ops,
67 &vdev_hole_ops,
68 NULL
69 };
70
71 /*
72 * Given a vdev type, return the appropriate ops vector.
73 */
74 static vdev_ops_t *
75 vdev_getops(const char *type)
76 {
77 vdev_ops_t *ops, **opspp;
78
79 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
80 if (strcmp(ops->vdev_op_type, type) == 0)
81 break;
82
83 return (ops);
84 }
85
86 /*
87 * Default asize function: return the MAX of psize with the asize of
88 * all children. This is what's used by anything other than RAID-Z.
89 */
90 uint64_t
91 vdev_default_asize(vdev_t *vd, uint64_t psize)
92 {
93 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
94 uint64_t csize;
95 int c;
96
97 for (c = 0; c < vd->vdev_children; c++) {
98 csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
99 asize = MAX(asize, csize);
100 }
101
102 return (asize);
103 }
104
105 /*
106 * Get the minimum allocatable size. We define the allocatable size as
107 * the vdev's asize rounded to the nearest metaslab. This allows us to
108 * replace or attach devices which don't have the same physical size but
109 * can still satisfy the same number of allocations.
110 */
111 uint64_t
112 vdev_get_min_asize(vdev_t *vd)
113 {
114 vdev_t *pvd = vd->vdev_parent;
115
116 /*
117 * If our parent is NULL (inactive spare or cache) or is the root,
118 * just return our own asize.
119 */
120 if (pvd == NULL)
121 return (vd->vdev_asize);
122
123 /*
124 * The top-level vdev just returns the allocatable size rounded
125 * to the nearest metaslab.
126 */
127 if (vd == vd->vdev_top)
128 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
129
130 /*
131 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
132 * so each child must provide at least 1/Nth of its asize.
133 */
134 if (pvd->vdev_ops == &vdev_raidz_ops)
135 return (pvd->vdev_min_asize / pvd->vdev_children);
136
137 return (pvd->vdev_min_asize);
138 }
139
140 void
141 vdev_set_min_asize(vdev_t *vd)
142 {
143 int c;
144 vd->vdev_min_asize = vdev_get_min_asize(vd);
145
146 for (c = 0; c < vd->vdev_children; c++)
147 vdev_set_min_asize(vd->vdev_child[c]);
148 }
149
150 vdev_t *
151 vdev_lookup_top(spa_t *spa, uint64_t vdev)
152 {
153 vdev_t *rvd = spa->spa_root_vdev;
154
155 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
156
157 if (vdev < rvd->vdev_children) {
158 ASSERT(rvd->vdev_child[vdev] != NULL);
159 return (rvd->vdev_child[vdev]);
160 }
161
162 return (NULL);
163 }
164
165 vdev_t *
166 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
167 {
168 vdev_t *mvd;
169 int c;
170
171 if (vd->vdev_guid == guid)
172 return (vd);
173
174 for (c = 0; c < vd->vdev_children; c++)
175 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
176 NULL)
177 return (mvd);
178
179 return (NULL);
180 }
181
182 static int
183 vdev_count_leaves_impl(vdev_t *vd)
184 {
185 int n = 0;
186 int c;
187
188 if (vd->vdev_ops->vdev_op_leaf)
189 return (1);
190
191 for (c = 0; c < vd->vdev_children; c++)
192 n += vdev_count_leaves_impl(vd->vdev_child[c]);
193
194 return (n);
195 }
196
197 int
198 vdev_count_leaves(spa_t *spa)
199 {
200 return (vdev_count_leaves_impl(spa->spa_root_vdev));
201 }
202
203 void
204 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
205 {
206 size_t oldsize, newsize;
207 uint64_t id = cvd->vdev_id;
208 vdev_t **newchild;
209
210 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
211 ASSERT(cvd->vdev_parent == NULL);
212
213 cvd->vdev_parent = pvd;
214
215 if (pvd == NULL)
216 return;
217
218 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
219
220 oldsize = pvd->vdev_children * sizeof (vdev_t *);
221 pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
222 newsize = pvd->vdev_children * sizeof (vdev_t *);
223
224 newchild = kmem_alloc(newsize, KM_SLEEP);
225 if (pvd->vdev_child != NULL) {
226 bcopy(pvd->vdev_child, newchild, oldsize);
227 kmem_free(pvd->vdev_child, oldsize);
228 }
229
230 pvd->vdev_child = newchild;
231 pvd->vdev_child[id] = cvd;
232
233 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
234 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
235
236 /*
237 * Walk up all ancestors to update guid sum.
238 */
239 for (; pvd != NULL; pvd = pvd->vdev_parent)
240 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
241 }
242
243 void
244 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
245 {
246 int c;
247 uint_t id = cvd->vdev_id;
248
249 ASSERT(cvd->vdev_parent == pvd);
250
251 if (pvd == NULL)
252 return;
253
254 ASSERT(id < pvd->vdev_children);
255 ASSERT(pvd->vdev_child[id] == cvd);
256
257 pvd->vdev_child[id] = NULL;
258 cvd->vdev_parent = NULL;
259
260 for (c = 0; c < pvd->vdev_children; c++)
261 if (pvd->vdev_child[c])
262 break;
263
264 if (c == pvd->vdev_children) {
265 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
266 pvd->vdev_child = NULL;
267 pvd->vdev_children = 0;
268 }
269
270 /*
271 * Walk up all ancestors to update guid sum.
272 */
273 for (; pvd != NULL; pvd = pvd->vdev_parent)
274 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
275 }
276
277 /*
278 * Remove any holes in the child array.
279 */
280 void
281 vdev_compact_children(vdev_t *pvd)
282 {
283 vdev_t **newchild, *cvd;
284 int oldc = pvd->vdev_children;
285 int newc;
286 int c;
287
288 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
289
290 for (c = newc = 0; c < oldc; c++)
291 if (pvd->vdev_child[c])
292 newc++;
293
294 newchild = kmem_zalloc(newc * sizeof (vdev_t *), KM_SLEEP);
295
296 for (c = newc = 0; c < oldc; c++) {
297 if ((cvd = pvd->vdev_child[c]) != NULL) {
298 newchild[newc] = cvd;
299 cvd->vdev_id = newc++;
300 }
301 }
302
303 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
304 pvd->vdev_child = newchild;
305 pvd->vdev_children = newc;
306 }
307
308 /*
309 * Allocate and minimally initialize a vdev_t.
310 */
311 vdev_t *
312 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
313 {
314 vdev_t *vd;
315 int t;
316
317 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
318
319 if (spa->spa_root_vdev == NULL) {
320 ASSERT(ops == &vdev_root_ops);
321 spa->spa_root_vdev = vd;
322 spa->spa_load_guid = spa_generate_guid(NULL);
323 }
324
325 if (guid == 0 && ops != &vdev_hole_ops) {
326 if (spa->spa_root_vdev == vd) {
327 /*
328 * The root vdev's guid will also be the pool guid,
329 * which must be unique among all pools.
330 */
331 guid = spa_generate_guid(NULL);
332 } else {
333 /*
334 * Any other vdev's guid must be unique within the pool.
335 */
336 guid = spa_generate_guid(spa);
337 }
338 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
339 }
340
341 vd->vdev_spa = spa;
342 vd->vdev_id = id;
343 vd->vdev_guid = guid;
344 vd->vdev_guid_sum = guid;
345 vd->vdev_ops = ops;
346 vd->vdev_state = VDEV_STATE_CLOSED;
347 vd->vdev_ishole = (ops == &vdev_hole_ops);
348
349 list_link_init(&vd->vdev_config_dirty_node);
350 list_link_init(&vd->vdev_state_dirty_node);
351 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_NOLOCKDEP, NULL);
352 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
353 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
354 for (t = 0; t < DTL_TYPES; t++) {
355 vd->vdev_dtl[t] = range_tree_create(NULL, NULL,
356 &vd->vdev_dtl_lock);
357 }
358 txg_list_create(&vd->vdev_ms_list,
359 offsetof(struct metaslab, ms_txg_node));
360 txg_list_create(&vd->vdev_dtl_list,
361 offsetof(struct vdev, vdev_dtl_node));
362 vd->vdev_stat.vs_timestamp = gethrtime();
363 vdev_queue_init(vd);
364 vdev_cache_init(vd);
365
366 return (vd);
367 }
368
369 /*
370 * Allocate a new vdev. The 'alloctype' is used to control whether we are
371 * creating a new vdev or loading an existing one - the behavior is slightly
372 * different for each case.
373 */
374 int
375 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
376 int alloctype)
377 {
378 vdev_ops_t *ops;
379 char *type;
380 uint64_t guid = 0, islog, nparity;
381 vdev_t *vd;
382
383 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
384
385 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
386 return (SET_ERROR(EINVAL));
387
388 if ((ops = vdev_getops(type)) == NULL)
389 return (SET_ERROR(EINVAL));
390
391 /*
392 * If this is a load, get the vdev guid from the nvlist.
393 * Otherwise, vdev_alloc_common() will generate one for us.
394 */
395 if (alloctype == VDEV_ALLOC_LOAD) {
396 uint64_t label_id;
397
398 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
399 label_id != id)
400 return (SET_ERROR(EINVAL));
401
402 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
403 return (SET_ERROR(EINVAL));
404 } else if (alloctype == VDEV_ALLOC_SPARE) {
405 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
406 return (SET_ERROR(EINVAL));
407 } else if (alloctype == VDEV_ALLOC_L2CACHE) {
408 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
409 return (SET_ERROR(EINVAL));
410 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
411 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
412 return (SET_ERROR(EINVAL));
413 }
414
415 /*
416 * The first allocated vdev must be of type 'root'.
417 */
418 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
419 return (SET_ERROR(EINVAL));
420
421 /*
422 * Determine whether we're a log vdev.
423 */
424 islog = 0;
425 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
426 if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
427 return (SET_ERROR(ENOTSUP));
428
429 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
430 return (SET_ERROR(ENOTSUP));
431
432 /*
433 * Set the nparity property for RAID-Z vdevs.
434 */
435 nparity = -1ULL;
436 if (ops == &vdev_raidz_ops) {
437 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
438 &nparity) == 0) {
439 if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
440 return (SET_ERROR(EINVAL));
441 /*
442 * Previous versions could only support 1 or 2 parity
443 * device.
444 */
445 if (nparity > 1 &&
446 spa_version(spa) < SPA_VERSION_RAIDZ2)
447 return (SET_ERROR(ENOTSUP));
448 if (nparity > 2 &&
449 spa_version(spa) < SPA_VERSION_RAIDZ3)
450 return (SET_ERROR(ENOTSUP));
451 } else {
452 /*
453 * We require the parity to be specified for SPAs that
454 * support multiple parity levels.
455 */
456 if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
457 return (SET_ERROR(EINVAL));
458 /*
459 * Otherwise, we default to 1 parity device for RAID-Z.
460 */
461 nparity = 1;
462 }
463 } else {
464 nparity = 0;
465 }
466 ASSERT(nparity != -1ULL);
467
468 vd = vdev_alloc_common(spa, id, guid, ops);
469
470 vd->vdev_islog = islog;
471 vd->vdev_nparity = nparity;
472
473 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
474 vd->vdev_path = spa_strdup(vd->vdev_path);
475 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
476 vd->vdev_devid = spa_strdup(vd->vdev_devid);
477 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
478 &vd->vdev_physpath) == 0)
479 vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
480 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
481 vd->vdev_fru = spa_strdup(vd->vdev_fru);
482
483 /*
484 * Set the whole_disk property. If it's not specified, leave the value
485 * as -1.
486 */
487 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
488 &vd->vdev_wholedisk) != 0)
489 vd->vdev_wholedisk = -1ULL;
490
491 /*
492 * Look for the 'not present' flag. This will only be set if the device
493 * was not present at the time of import.
494 */
495 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
496 &vd->vdev_not_present);
497
498 /*
499 * Get the alignment requirement.
500 */
501 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
502
503 /*
504 * Retrieve the vdev creation time.
505 */
506 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
507 &vd->vdev_crtxg);
508
509 /*
510 * If we're a top-level vdev, try to load the allocation parameters.
511 */
512 if (parent && !parent->vdev_parent &&
513 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
514 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
515 &vd->vdev_ms_array);
516 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
517 &vd->vdev_ms_shift);
518 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
519 &vd->vdev_asize);
520 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
521 &vd->vdev_removing);
522 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
523 &vd->vdev_top_zap);
524 } else {
525 ASSERT0(vd->vdev_top_zap);
526 }
527
528 if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) {
529 ASSERT(alloctype == VDEV_ALLOC_LOAD ||
530 alloctype == VDEV_ALLOC_ADD ||
531 alloctype == VDEV_ALLOC_SPLIT ||
532 alloctype == VDEV_ALLOC_ROOTPOOL);
533 vd->vdev_mg = metaslab_group_create(islog ?
534 spa_log_class(spa) : spa_normal_class(spa), vd);
535 }
536
537 if (vd->vdev_ops->vdev_op_leaf &&
538 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
539 (void) nvlist_lookup_uint64(nv,
540 ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
541 } else {
542 ASSERT0(vd->vdev_leaf_zap);
543 }
544
545 /*
546 * If we're a leaf vdev, try to load the DTL object and other state.
547 */
548
549 if (vd->vdev_ops->vdev_op_leaf &&
550 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
551 alloctype == VDEV_ALLOC_ROOTPOOL)) {
552 if (alloctype == VDEV_ALLOC_LOAD) {
553 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
554 &vd->vdev_dtl_object);
555 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
556 &vd->vdev_unspare);
557 }
558
559 if (alloctype == VDEV_ALLOC_ROOTPOOL) {
560 uint64_t spare = 0;
561
562 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
563 &spare) == 0 && spare)
564 spa_spare_add(vd);
565 }
566
567 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
568 &vd->vdev_offline);
569
570 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
571 &vd->vdev_resilver_txg);
572
573 /*
574 * When importing a pool, we want to ignore the persistent fault
575 * state, as the diagnosis made on another system may not be
576 * valid in the current context. Local vdevs will
577 * remain in the faulted state.
578 */
579 if (spa_load_state(spa) == SPA_LOAD_OPEN) {
580 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
581 &vd->vdev_faulted);
582 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
583 &vd->vdev_degraded);
584 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
585 &vd->vdev_removed);
586
587 if (vd->vdev_faulted || vd->vdev_degraded) {
588 char *aux;
589
590 vd->vdev_label_aux =
591 VDEV_AUX_ERR_EXCEEDED;
592 if (nvlist_lookup_string(nv,
593 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
594 strcmp(aux, "external") == 0)
595 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
596 }
597 }
598 }
599
600 /*
601 * Add ourselves to the parent's list of children.
602 */
603 vdev_add_child(parent, vd);
604
605 *vdp = vd;
606
607 return (0);
608 }
609
610 void
611 vdev_free(vdev_t *vd)
612 {
613 int c, t;
614 spa_t *spa = vd->vdev_spa;
615
616 /*
617 * vdev_free() implies closing the vdev first. This is simpler than
618 * trying to ensure complicated semantics for all callers.
619 */
620 vdev_close(vd);
621
622 ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
623 ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
624
625 /*
626 * Free all children.
627 */
628 for (c = 0; c < vd->vdev_children; c++)
629 vdev_free(vd->vdev_child[c]);
630
631 ASSERT(vd->vdev_child == NULL);
632 ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
633
634 /*
635 * Discard allocation state.
636 */
637 if (vd->vdev_mg != NULL) {
638 vdev_metaslab_fini(vd);
639 metaslab_group_destroy(vd->vdev_mg);
640 }
641
642 ASSERT0(vd->vdev_stat.vs_space);
643 ASSERT0(vd->vdev_stat.vs_dspace);
644 ASSERT0(vd->vdev_stat.vs_alloc);
645
646 /*
647 * Remove this vdev from its parent's child list.
648 */
649 vdev_remove_child(vd->vdev_parent, vd);
650
651 ASSERT(vd->vdev_parent == NULL);
652
653 /*
654 * Clean up vdev structure.
655 */
656 vdev_queue_fini(vd);
657 vdev_cache_fini(vd);
658
659 if (vd->vdev_path)
660 spa_strfree(vd->vdev_path);
661 if (vd->vdev_devid)
662 spa_strfree(vd->vdev_devid);
663 if (vd->vdev_physpath)
664 spa_strfree(vd->vdev_physpath);
665 if (vd->vdev_fru)
666 spa_strfree(vd->vdev_fru);
667
668 if (vd->vdev_isspare)
669 spa_spare_remove(vd);
670 if (vd->vdev_isl2cache)
671 spa_l2cache_remove(vd);
672
673 txg_list_destroy(&vd->vdev_ms_list);
674 txg_list_destroy(&vd->vdev_dtl_list);
675
676 mutex_enter(&vd->vdev_dtl_lock);
677 space_map_close(vd->vdev_dtl_sm);
678 for (t = 0; t < DTL_TYPES; t++) {
679 range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
680 range_tree_destroy(vd->vdev_dtl[t]);
681 }
682 mutex_exit(&vd->vdev_dtl_lock);
683
684 mutex_destroy(&vd->vdev_dtl_lock);
685 mutex_destroy(&vd->vdev_stat_lock);
686 mutex_destroy(&vd->vdev_probe_lock);
687
688 if (vd == spa->spa_root_vdev)
689 spa->spa_root_vdev = NULL;
690
691 kmem_free(vd, sizeof (vdev_t));
692 }
693
694 /*
695 * Transfer top-level vdev state from svd to tvd.
696 */
697 static void
698 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
699 {
700 spa_t *spa = svd->vdev_spa;
701 metaslab_t *msp;
702 vdev_t *vd;
703 int t;
704
705 ASSERT(tvd == tvd->vdev_top);
706
707 tvd->vdev_ms_array = svd->vdev_ms_array;
708 tvd->vdev_ms_shift = svd->vdev_ms_shift;
709 tvd->vdev_ms_count = svd->vdev_ms_count;
710 tvd->vdev_top_zap = svd->vdev_top_zap;
711
712 svd->vdev_ms_array = 0;
713 svd->vdev_ms_shift = 0;
714 svd->vdev_ms_count = 0;
715 svd->vdev_top_zap = 0;
716
717 if (tvd->vdev_mg)
718 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
719 tvd->vdev_mg = svd->vdev_mg;
720 tvd->vdev_ms = svd->vdev_ms;
721
722 svd->vdev_mg = NULL;
723 svd->vdev_ms = NULL;
724
725 if (tvd->vdev_mg != NULL)
726 tvd->vdev_mg->mg_vd = tvd;
727
728 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
729 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
730 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
731
732 svd->vdev_stat.vs_alloc = 0;
733 svd->vdev_stat.vs_space = 0;
734 svd->vdev_stat.vs_dspace = 0;
735
736 for (t = 0; t < TXG_SIZE; t++) {
737 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
738 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
739 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
740 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
741 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
742 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
743 }
744
745 if (list_link_active(&svd->vdev_config_dirty_node)) {
746 vdev_config_clean(svd);
747 vdev_config_dirty(tvd);
748 }
749
750 if (list_link_active(&svd->vdev_state_dirty_node)) {
751 vdev_state_clean(svd);
752 vdev_state_dirty(tvd);
753 }
754
755 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
756 svd->vdev_deflate_ratio = 0;
757
758 tvd->vdev_islog = svd->vdev_islog;
759 svd->vdev_islog = 0;
760 }
761
762 static void
763 vdev_top_update(vdev_t *tvd, vdev_t *vd)
764 {
765 int c;
766
767 if (vd == NULL)
768 return;
769
770 vd->vdev_top = tvd;
771
772 for (c = 0; c < vd->vdev_children; c++)
773 vdev_top_update(tvd, vd->vdev_child[c]);
774 }
775
776 /*
777 * Add a mirror/replacing vdev above an existing vdev.
778 */
779 vdev_t *
780 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
781 {
782 spa_t *spa = cvd->vdev_spa;
783 vdev_t *pvd = cvd->vdev_parent;
784 vdev_t *mvd;
785
786 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
787
788 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
789
790 mvd->vdev_asize = cvd->vdev_asize;
791 mvd->vdev_min_asize = cvd->vdev_min_asize;
792 mvd->vdev_max_asize = cvd->vdev_max_asize;
793 mvd->vdev_ashift = cvd->vdev_ashift;
794 mvd->vdev_state = cvd->vdev_state;
795 mvd->vdev_crtxg = cvd->vdev_crtxg;
796
797 vdev_remove_child(pvd, cvd);
798 vdev_add_child(pvd, mvd);
799 cvd->vdev_id = mvd->vdev_children;
800 vdev_add_child(mvd, cvd);
801 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
802
803 if (mvd == mvd->vdev_top)
804 vdev_top_transfer(cvd, mvd);
805
806 return (mvd);
807 }
808
809 /*
810 * Remove a 1-way mirror/replacing vdev from the tree.
811 */
812 void
813 vdev_remove_parent(vdev_t *cvd)
814 {
815 vdev_t *mvd = cvd->vdev_parent;
816 vdev_t *pvd = mvd->vdev_parent;
817
818 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
819
820 ASSERT(mvd->vdev_children == 1);
821 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
822 mvd->vdev_ops == &vdev_replacing_ops ||
823 mvd->vdev_ops == &vdev_spare_ops);
824 cvd->vdev_ashift = mvd->vdev_ashift;
825
826 vdev_remove_child(mvd, cvd);
827 vdev_remove_child(pvd, mvd);
828
829 /*
830 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
831 * Otherwise, we could have detached an offline device, and when we
832 * go to import the pool we'll think we have two top-level vdevs,
833 * instead of a different version of the same top-level vdev.
834 */
835 if (mvd->vdev_top == mvd) {
836 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
837 cvd->vdev_orig_guid = cvd->vdev_guid;
838 cvd->vdev_guid += guid_delta;
839 cvd->vdev_guid_sum += guid_delta;
840
841 /*
842 * If pool not set for autoexpand, we need to also preserve
843 * mvd's asize to prevent automatic expansion of cvd.
844 * Otherwise if we are adjusting the mirror by attaching and
845 * detaching children of non-uniform sizes, the mirror could
846 * autoexpand, unexpectedly requiring larger devices to
847 * re-establish the mirror.
848 */
849 if (!cvd->vdev_spa->spa_autoexpand)
850 cvd->vdev_asize = mvd->vdev_asize;
851 }
852 cvd->vdev_id = mvd->vdev_id;
853 vdev_add_child(pvd, cvd);
854 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
855
856 if (cvd == cvd->vdev_top)
857 vdev_top_transfer(mvd, cvd);
858
859 ASSERT(mvd->vdev_children == 0);
860 vdev_free(mvd);
861 }
862
863 int
864 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
865 {
866 spa_t *spa = vd->vdev_spa;
867 objset_t *mos = spa->spa_meta_objset;
868 uint64_t m;
869 uint64_t oldc = vd->vdev_ms_count;
870 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
871 metaslab_t **mspp;
872 int error;
873
874 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
875
876 /*
877 * This vdev is not being allocated from yet or is a hole.
878 */
879 if (vd->vdev_ms_shift == 0)
880 return (0);
881
882 ASSERT(!vd->vdev_ishole);
883
884 /*
885 * Compute the raidz-deflation ratio. Note, we hard-code
886 * in 128k (1 << 17) because it is the "typical" blocksize.
887 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
888 * otherwise it would inconsistently account for existing bp's.
889 */
890 vd->vdev_deflate_ratio = (1 << 17) /
891 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
892
893 ASSERT(oldc <= newc);
894
895 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
896
897 if (oldc != 0) {
898 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
899 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
900 }
901
902 vd->vdev_ms = mspp;
903 vd->vdev_ms_count = newc;
904
905 for (m = oldc; m < newc; m++) {
906 uint64_t object = 0;
907
908 if (txg == 0) {
909 error = dmu_read(mos, vd->vdev_ms_array,
910 m * sizeof (uint64_t), sizeof (uint64_t), &object,
911 DMU_READ_PREFETCH);
912 if (error)
913 return (error);
914 }
915
916 error = metaslab_init(vd->vdev_mg, m, object, txg,
917 &(vd->vdev_ms[m]));
918 if (error)
919 return (error);
920 }
921
922 if (txg == 0)
923 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
924
925 /*
926 * If the vdev is being removed we don't activate
927 * the metaslabs since we want to ensure that no new
928 * allocations are performed on this device.
929 */
930 if (oldc == 0 && !vd->vdev_removing)
931 metaslab_group_activate(vd->vdev_mg);
932
933 if (txg == 0)
934 spa_config_exit(spa, SCL_ALLOC, FTAG);
935
936 return (0);
937 }
938
939 void
940 vdev_metaslab_fini(vdev_t *vd)
941 {
942 uint64_t m;
943 uint64_t count = vd->vdev_ms_count;
944
945 if (vd->vdev_ms != NULL) {
946 metaslab_group_passivate(vd->vdev_mg);
947 for (m = 0; m < count; m++) {
948 metaslab_t *msp = vd->vdev_ms[m];
949
950 if (msp != NULL)
951 metaslab_fini(msp);
952 }
953 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
954 vd->vdev_ms = NULL;
955 }
956
957 ASSERT3U(vd->vdev_pending_fastwrite, ==, 0);
958 }
959
960 typedef struct vdev_probe_stats {
961 boolean_t vps_readable;
962 boolean_t vps_writeable;
963 int vps_flags;
964 } vdev_probe_stats_t;
965
966 static void
967 vdev_probe_done(zio_t *zio)
968 {
969 spa_t *spa = zio->io_spa;
970 vdev_t *vd = zio->io_vd;
971 vdev_probe_stats_t *vps = zio->io_private;
972
973 ASSERT(vd->vdev_probe_zio != NULL);
974
975 if (zio->io_type == ZIO_TYPE_READ) {
976 if (zio->io_error == 0)
977 vps->vps_readable = 1;
978 if (zio->io_error == 0 && spa_writeable(spa)) {
979 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
980 zio->io_offset, zio->io_size, zio->io_data,
981 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
982 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
983 } else {
984 zio_buf_free(zio->io_data, zio->io_size);
985 }
986 } else if (zio->io_type == ZIO_TYPE_WRITE) {
987 if (zio->io_error == 0)
988 vps->vps_writeable = 1;
989 zio_buf_free(zio->io_data, zio->io_size);
990 } else if (zio->io_type == ZIO_TYPE_NULL) {
991 zio_t *pio;
992
993 vd->vdev_cant_read |= !vps->vps_readable;
994 vd->vdev_cant_write |= !vps->vps_writeable;
995
996 if (vdev_readable(vd) &&
997 (vdev_writeable(vd) || !spa_writeable(spa))) {
998 zio->io_error = 0;
999 } else {
1000 ASSERT(zio->io_error != 0);
1001 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
1002 spa, vd, NULL, 0, 0);
1003 zio->io_error = SET_ERROR(ENXIO);
1004 }
1005
1006 mutex_enter(&vd->vdev_probe_lock);
1007 ASSERT(vd->vdev_probe_zio == zio);
1008 vd->vdev_probe_zio = NULL;
1009 mutex_exit(&vd->vdev_probe_lock);
1010
1011 while ((pio = zio_walk_parents(zio)) != NULL)
1012 if (!vdev_accessible(vd, pio))
1013 pio->io_error = SET_ERROR(ENXIO);
1014
1015 kmem_free(vps, sizeof (*vps));
1016 }
1017 }
1018
1019 /*
1020 * Determine whether this device is accessible.
1021 *
1022 * Read and write to several known locations: the pad regions of each
1023 * vdev label but the first, which we leave alone in case it contains
1024 * a VTOC.
1025 */
1026 zio_t *
1027 vdev_probe(vdev_t *vd, zio_t *zio)
1028 {
1029 spa_t *spa = vd->vdev_spa;
1030 vdev_probe_stats_t *vps = NULL;
1031 zio_t *pio;
1032 int l;
1033
1034 ASSERT(vd->vdev_ops->vdev_op_leaf);
1035
1036 /*
1037 * Don't probe the probe.
1038 */
1039 if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1040 return (NULL);
1041
1042 /*
1043 * To prevent 'probe storms' when a device fails, we create
1044 * just one probe i/o at a time. All zios that want to probe
1045 * this vdev will become parents of the probe io.
1046 */
1047 mutex_enter(&vd->vdev_probe_lock);
1048
1049 if ((pio = vd->vdev_probe_zio) == NULL) {
1050 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1051
1052 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1053 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
1054 ZIO_FLAG_TRYHARD;
1055
1056 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1057 /*
1058 * vdev_cant_read and vdev_cant_write can only
1059 * transition from TRUE to FALSE when we have the
1060 * SCL_ZIO lock as writer; otherwise they can only
1061 * transition from FALSE to TRUE. This ensures that
1062 * any zio looking at these values can assume that
1063 * failures persist for the life of the I/O. That's
1064 * important because when a device has intermittent
1065 * connectivity problems, we want to ensure that
1066 * they're ascribed to the device (ENXIO) and not
1067 * the zio (EIO).
1068 *
1069 * Since we hold SCL_ZIO as writer here, clear both
1070 * values so the probe can reevaluate from first
1071 * principles.
1072 */
1073 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1074 vd->vdev_cant_read = B_FALSE;
1075 vd->vdev_cant_write = B_FALSE;
1076 }
1077
1078 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1079 vdev_probe_done, vps,
1080 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1081
1082 /*
1083 * We can't change the vdev state in this context, so we
1084 * kick off an async task to do it on our behalf.
1085 */
1086 if (zio != NULL) {
1087 vd->vdev_probe_wanted = B_TRUE;
1088 spa_async_request(spa, SPA_ASYNC_PROBE);
1089 }
1090 }
1091
1092 if (zio != NULL)
1093 zio_add_child(zio, pio);
1094
1095 mutex_exit(&vd->vdev_probe_lock);
1096
1097 if (vps == NULL) {
1098 ASSERT(zio != NULL);
1099 return (NULL);
1100 }
1101
1102 for (l = 1; l < VDEV_LABELS; l++) {
1103 zio_nowait(zio_read_phys(pio, vd,
1104 vdev_label_offset(vd->vdev_psize, l,
1105 offsetof(vdev_label_t, vl_pad2)),
1106 VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE),
1107 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1108 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1109 }
1110
1111 if (zio == NULL)
1112 return (pio);
1113
1114 zio_nowait(pio);
1115 return (NULL);
1116 }
1117
1118 static void
1119 vdev_open_child(void *arg)
1120 {
1121 vdev_t *vd = arg;
1122
1123 vd->vdev_open_thread = curthread;
1124 vd->vdev_open_error = vdev_open(vd);
1125 vd->vdev_open_thread = NULL;
1126 vd->vdev_parent->vdev_nonrot &= vd->vdev_nonrot;
1127 }
1128
1129 static boolean_t
1130 vdev_uses_zvols(vdev_t *vd)
1131 {
1132 int c;
1133
1134 #ifdef _KERNEL
1135 if (zvol_is_zvol(vd->vdev_path))
1136 return (B_TRUE);
1137 #endif
1138
1139 for (c = 0; c < vd->vdev_children; c++)
1140 if (vdev_uses_zvols(vd->vdev_child[c]))
1141 return (B_TRUE);
1142
1143 return (B_FALSE);
1144 }
1145
1146 void
1147 vdev_open_children(vdev_t *vd)
1148 {
1149 taskq_t *tq;
1150 int children = vd->vdev_children;
1151 int c;
1152
1153 vd->vdev_nonrot = B_TRUE;
1154
1155 /*
1156 * in order to handle pools on top of zvols, do the opens
1157 * in a single thread so that the same thread holds the
1158 * spa_namespace_lock
1159 */
1160 if (vdev_uses_zvols(vd)) {
1161 for (c = 0; c < children; c++) {
1162 vd->vdev_child[c]->vdev_open_error =
1163 vdev_open(vd->vdev_child[c]);
1164 vd->vdev_nonrot &= vd->vdev_child[c]->vdev_nonrot;
1165 }
1166 return;
1167 }
1168 tq = taskq_create("vdev_open", children, minclsyspri,
1169 children, children, TASKQ_PREPOPULATE);
1170
1171 for (c = 0; c < children; c++)
1172 VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1173 TQ_SLEEP) != 0);
1174
1175 taskq_destroy(tq);
1176
1177 for (c = 0; c < children; c++)
1178 vd->vdev_nonrot &= vd->vdev_child[c]->vdev_nonrot;
1179 }
1180
1181 /*
1182 * Prepare a virtual device for access.
1183 */
1184 int
1185 vdev_open(vdev_t *vd)
1186 {
1187 spa_t *spa = vd->vdev_spa;
1188 int error;
1189 uint64_t osize = 0;
1190 uint64_t max_osize = 0;
1191 uint64_t asize, max_asize, psize;
1192 uint64_t ashift = 0;
1193 int c;
1194
1195 ASSERT(vd->vdev_open_thread == curthread ||
1196 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1197 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1198 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1199 vd->vdev_state == VDEV_STATE_OFFLINE);
1200
1201 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1202 vd->vdev_cant_read = B_FALSE;
1203 vd->vdev_cant_write = B_FALSE;
1204 vd->vdev_min_asize = vdev_get_min_asize(vd);
1205
1206 /*
1207 * If this vdev is not removed, check its fault status. If it's
1208 * faulted, bail out of the open.
1209 */
1210 if (!vd->vdev_removed && vd->vdev_faulted) {
1211 ASSERT(vd->vdev_children == 0);
1212 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1213 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1214 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1215 vd->vdev_label_aux);
1216 return (SET_ERROR(ENXIO));
1217 } else if (vd->vdev_offline) {
1218 ASSERT(vd->vdev_children == 0);
1219 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1220 return (SET_ERROR(ENXIO));
1221 }
1222
1223 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift);
1224
1225 /*
1226 * Reset the vdev_reopening flag so that we actually close
1227 * the vdev on error.
1228 */
1229 vd->vdev_reopening = B_FALSE;
1230 if (zio_injection_enabled && error == 0)
1231 error = zio_handle_device_injection(vd, NULL, ENXIO);
1232
1233 if (error) {
1234 if (vd->vdev_removed &&
1235 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
1236 vd->vdev_removed = B_FALSE;
1237
1238 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1239 vd->vdev_stat.vs_aux);
1240 return (error);
1241 }
1242
1243 vd->vdev_removed = B_FALSE;
1244
1245 /*
1246 * Recheck the faulted flag now that we have confirmed that
1247 * the vdev is accessible. If we're faulted, bail.
1248 */
1249 if (vd->vdev_faulted) {
1250 ASSERT(vd->vdev_children == 0);
1251 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1252 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1253 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1254 vd->vdev_label_aux);
1255 return (SET_ERROR(ENXIO));
1256 }
1257
1258 if (vd->vdev_degraded) {
1259 ASSERT(vd->vdev_children == 0);
1260 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1261 VDEV_AUX_ERR_EXCEEDED);
1262 } else {
1263 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
1264 }
1265
1266 /*
1267 * For hole or missing vdevs we just return success.
1268 */
1269 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
1270 return (0);
1271
1272 for (c = 0; c < vd->vdev_children; c++) {
1273 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1274 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1275 VDEV_AUX_NONE);
1276 break;
1277 }
1278 }
1279
1280 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1281 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1282
1283 if (vd->vdev_children == 0) {
1284 if (osize < SPA_MINDEVSIZE) {
1285 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1286 VDEV_AUX_TOO_SMALL);
1287 return (SET_ERROR(EOVERFLOW));
1288 }
1289 psize = osize;
1290 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1291 max_asize = max_osize - (VDEV_LABEL_START_SIZE +
1292 VDEV_LABEL_END_SIZE);
1293 } else {
1294 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1295 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1296 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1297 VDEV_AUX_TOO_SMALL);
1298 return (SET_ERROR(EOVERFLOW));
1299 }
1300 psize = 0;
1301 asize = osize;
1302 max_asize = max_osize;
1303 }
1304
1305 vd->vdev_psize = psize;
1306
1307 /*
1308 * Make sure the allocatable size hasn't shrunk.
1309 */
1310 if (asize < vd->vdev_min_asize) {
1311 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1312 VDEV_AUX_BAD_LABEL);
1313 return (SET_ERROR(EINVAL));
1314 }
1315
1316 if (vd->vdev_asize == 0) {
1317 /*
1318 * This is the first-ever open, so use the computed values.
1319 * For compatibility, a different ashift can be requested.
1320 */
1321 vd->vdev_asize = asize;
1322 vd->vdev_max_asize = max_asize;
1323 if (vd->vdev_ashift == 0)
1324 vd->vdev_ashift = ashift;
1325 } else {
1326 /*
1327 * Detect if the alignment requirement has increased.
1328 * We don't want to make the pool unavailable, just
1329 * post an event instead.
1330 */
1331 if (ashift > vd->vdev_top->vdev_ashift &&
1332 vd->vdev_ops->vdev_op_leaf) {
1333 zfs_ereport_post(FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT,
1334 spa, vd, NULL, 0, 0);
1335 }
1336
1337 vd->vdev_max_asize = max_asize;
1338 }
1339
1340 /*
1341 * If all children are healthy and the asize has increased,
1342 * then we've experienced dynamic LUN growth. If automatic
1343 * expansion is enabled then use the additional space.
1344 */
1345 if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize &&
1346 (vd->vdev_expanding || spa->spa_autoexpand))
1347 vd->vdev_asize = asize;
1348
1349 vdev_set_min_asize(vd);
1350
1351 /*
1352 * Ensure we can issue some IO before declaring the
1353 * vdev open for business.
1354 */
1355 if (vd->vdev_ops->vdev_op_leaf &&
1356 (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
1357 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1358 VDEV_AUX_ERR_EXCEEDED);
1359 return (error);
1360 }
1361
1362 /*
1363 * Track the min and max ashift values for normal data devices.
1364 */
1365 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1366 !vd->vdev_islog && vd->vdev_aux == NULL) {
1367 if (vd->vdev_ashift > spa->spa_max_ashift)
1368 spa->spa_max_ashift = vd->vdev_ashift;
1369 if (vd->vdev_ashift < spa->spa_min_ashift)
1370 spa->spa_min_ashift = vd->vdev_ashift;
1371 }
1372
1373 /*
1374 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1375 * resilver. But don't do this if we are doing a reopen for a scrub,
1376 * since this would just restart the scrub we are already doing.
1377 */
1378 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
1379 vdev_resilver_needed(vd, NULL, NULL))
1380 spa_async_request(spa, SPA_ASYNC_RESILVER);
1381
1382 return (0);
1383 }
1384
1385 /*
1386 * Called once the vdevs are all opened, this routine validates the label
1387 * contents. This needs to be done before vdev_load() so that we don't
1388 * inadvertently do repair I/Os to the wrong device.
1389 *
1390 * If 'strict' is false ignore the spa guid check. This is necessary because
1391 * if the machine crashed during a re-guid the new guid might have been written
1392 * to all of the vdev labels, but not the cached config. The strict check
1393 * will be performed when the pool is opened again using the mos config.
1394 *
1395 * This function will only return failure if one of the vdevs indicates that it
1396 * has since been destroyed or exported. This is only possible if
1397 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1398 * will be updated but the function will return 0.
1399 */
1400 int
1401 vdev_validate(vdev_t *vd, boolean_t strict)
1402 {
1403 spa_t *spa = vd->vdev_spa;
1404 nvlist_t *label;
1405 uint64_t guid = 0, top_guid;
1406 uint64_t state;
1407 int c;
1408
1409 for (c = 0; c < vd->vdev_children; c++)
1410 if (vdev_validate(vd->vdev_child[c], strict) != 0)
1411 return (SET_ERROR(EBADF));
1412
1413 /*
1414 * If the device has already failed, or was marked offline, don't do
1415 * any further validation. Otherwise, label I/O will fail and we will
1416 * overwrite the previous state.
1417 */
1418 if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
1419 uint64_t aux_guid = 0;
1420 nvlist_t *nvl;
1421 uint64_t txg = spa_last_synced_txg(spa) != 0 ?
1422 spa_last_synced_txg(spa) : -1ULL;
1423
1424 if ((label = vdev_label_read_config(vd, txg)) == NULL) {
1425 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1426 VDEV_AUX_BAD_LABEL);
1427 return (0);
1428 }
1429
1430 /*
1431 * Determine if this vdev has been split off into another
1432 * pool. If so, then refuse to open it.
1433 */
1434 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
1435 &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
1436 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1437 VDEV_AUX_SPLIT_POOL);
1438 nvlist_free(label);
1439 return (0);
1440 }
1441
1442 if (strict && (nvlist_lookup_uint64(label,
1443 ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1444 guid != spa_guid(spa))) {
1445 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1446 VDEV_AUX_CORRUPT_DATA);
1447 nvlist_free(label);
1448 return (0);
1449 }
1450
1451 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
1452 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
1453 &aux_guid) != 0)
1454 aux_guid = 0;
1455
1456 /*
1457 * If this vdev just became a top-level vdev because its
1458 * sibling was detached, it will have adopted the parent's
1459 * vdev guid -- but the label may or may not be on disk yet.
1460 * Fortunately, either version of the label will have the
1461 * same top guid, so if we're a top-level vdev, we can
1462 * safely compare to that instead.
1463 *
1464 * If we split this vdev off instead, then we also check the
1465 * original pool's guid. We don't want to consider the vdev
1466 * corrupt if it is partway through a split operation.
1467 */
1468 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
1469 &guid) != 0 ||
1470 nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
1471 &top_guid) != 0 ||
1472 ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) &&
1473 (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
1474 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1475 VDEV_AUX_CORRUPT_DATA);
1476 nvlist_free(label);
1477 return (0);
1478 }
1479
1480 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1481 &state) != 0) {
1482 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1483 VDEV_AUX_CORRUPT_DATA);
1484 nvlist_free(label);
1485 return (0);
1486 }
1487
1488 nvlist_free(label);
1489
1490 /*
1491 * If this is a verbatim import, no need to check the
1492 * state of the pool.
1493 */
1494 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
1495 spa_load_state(spa) == SPA_LOAD_OPEN &&
1496 state != POOL_STATE_ACTIVE)
1497 return (SET_ERROR(EBADF));
1498
1499 /*
1500 * If we were able to open and validate a vdev that was
1501 * previously marked permanently unavailable, clear that state
1502 * now.
1503 */
1504 if (vd->vdev_not_present)
1505 vd->vdev_not_present = 0;
1506 }
1507
1508 return (0);
1509 }
1510
1511 /*
1512 * Close a virtual device.
1513 */
1514 void
1515 vdev_close(vdev_t *vd)
1516 {
1517 vdev_t *pvd = vd->vdev_parent;
1518 ASSERTV(spa_t *spa = vd->vdev_spa);
1519
1520 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1521
1522 /*
1523 * If our parent is reopening, then we are as well, unless we are
1524 * going offline.
1525 */
1526 if (pvd != NULL && pvd->vdev_reopening)
1527 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
1528
1529 vd->vdev_ops->vdev_op_close(vd);
1530
1531 vdev_cache_purge(vd);
1532
1533 /*
1534 * We record the previous state before we close it, so that if we are
1535 * doing a reopen(), we don't generate FMA ereports if we notice that
1536 * it's still faulted.
1537 */
1538 vd->vdev_prevstate = vd->vdev_state;
1539
1540 if (vd->vdev_offline)
1541 vd->vdev_state = VDEV_STATE_OFFLINE;
1542 else
1543 vd->vdev_state = VDEV_STATE_CLOSED;
1544 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1545 }
1546
1547 void
1548 vdev_hold(vdev_t *vd)
1549 {
1550 spa_t *spa = vd->vdev_spa;
1551 int c;
1552
1553 ASSERT(spa_is_root(spa));
1554 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1555 return;
1556
1557 for (c = 0; c < vd->vdev_children; c++)
1558 vdev_hold(vd->vdev_child[c]);
1559
1560 if (vd->vdev_ops->vdev_op_leaf)
1561 vd->vdev_ops->vdev_op_hold(vd);
1562 }
1563
1564 void
1565 vdev_rele(vdev_t *vd)
1566 {
1567 int c;
1568
1569 ASSERT(spa_is_root(vd->vdev_spa));
1570 for (c = 0; c < vd->vdev_children; c++)
1571 vdev_rele(vd->vdev_child[c]);
1572
1573 if (vd->vdev_ops->vdev_op_leaf)
1574 vd->vdev_ops->vdev_op_rele(vd);
1575 }
1576
1577 /*
1578 * Reopen all interior vdevs and any unopened leaves. We don't actually
1579 * reopen leaf vdevs which had previously been opened as they might deadlock
1580 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
1581 * If the leaf has never been opened then open it, as usual.
1582 */
1583 void
1584 vdev_reopen(vdev_t *vd)
1585 {
1586 spa_t *spa = vd->vdev_spa;
1587
1588 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1589
1590 /* set the reopening flag unless we're taking the vdev offline */
1591 vd->vdev_reopening = !vd->vdev_offline;
1592 vdev_close(vd);
1593 (void) vdev_open(vd);
1594
1595 /*
1596 * Call vdev_validate() here to make sure we have the same device.
1597 * Otherwise, a device with an invalid label could be successfully
1598 * opened in response to vdev_reopen().
1599 */
1600 if (vd->vdev_aux) {
1601 (void) vdev_validate_aux(vd);
1602 if (vdev_readable(vd) && vdev_writeable(vd) &&
1603 vd->vdev_aux == &spa->spa_l2cache &&
1604 !l2arc_vdev_present(vd))
1605 l2arc_add_vdev(spa, vd);
1606 } else {
1607 (void) vdev_validate(vd, B_TRUE);
1608 }
1609
1610 /*
1611 * Reassess parent vdev's health.
1612 */
1613 vdev_propagate_state(vd);
1614 }
1615
1616 int
1617 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1618 {
1619 int error;
1620
1621 /*
1622 * Normally, partial opens (e.g. of a mirror) are allowed.
1623 * For a create, however, we want to fail the request if
1624 * there are any components we can't open.
1625 */
1626 error = vdev_open(vd);
1627
1628 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1629 vdev_close(vd);
1630 return (error ? error : ENXIO);
1631 }
1632
1633 /*
1634 * Recursively load DTLs and initialize all labels.
1635 */
1636 if ((error = vdev_dtl_load(vd)) != 0 ||
1637 (error = vdev_label_init(vd, txg, isreplacing ?
1638 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1639 vdev_close(vd);
1640 return (error);
1641 }
1642
1643 return (0);
1644 }
1645
1646 void
1647 vdev_metaslab_set_size(vdev_t *vd)
1648 {
1649 /*
1650 * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev.
1651 */
1652 vd->vdev_ms_shift = highbit64(vd->vdev_asize / metaslabs_per_vdev);
1653 vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1654 }
1655
1656 void
1657 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1658 {
1659 ASSERT(vd == vd->vdev_top);
1660 ASSERT(!vd->vdev_ishole);
1661 ASSERT(ISP2(flags));
1662 ASSERT(spa_writeable(vd->vdev_spa));
1663
1664 if (flags & VDD_METASLAB)
1665 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1666
1667 if (flags & VDD_DTL)
1668 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1669
1670 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1671 }
1672
1673 void
1674 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
1675 {
1676 int c;
1677
1678 for (c = 0; c < vd->vdev_children; c++)
1679 vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
1680
1681 if (vd->vdev_ops->vdev_op_leaf)
1682 vdev_dirty(vd->vdev_top, flags, vd, txg);
1683 }
1684
1685 /*
1686 * DTLs.
1687 *
1688 * A vdev's DTL (dirty time log) is the set of transaction groups for which
1689 * the vdev has less than perfect replication. There are four kinds of DTL:
1690 *
1691 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
1692 *
1693 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
1694 *
1695 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
1696 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
1697 * txgs that was scrubbed.
1698 *
1699 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
1700 * persistent errors or just some device being offline.
1701 * Unlike the other three, the DTL_OUTAGE map is not generally
1702 * maintained; it's only computed when needed, typically to
1703 * determine whether a device can be detached.
1704 *
1705 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
1706 * either has the data or it doesn't.
1707 *
1708 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
1709 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
1710 * if any child is less than fully replicated, then so is its parent.
1711 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
1712 * comprising only those txgs which appear in 'maxfaults' or more children;
1713 * those are the txgs we don't have enough replication to read. For example,
1714 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
1715 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
1716 * two child DTL_MISSING maps.
1717 *
1718 * It should be clear from the above that to compute the DTLs and outage maps
1719 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
1720 * Therefore, that is all we keep on disk. When loading the pool, or after
1721 * a configuration change, we generate all other DTLs from first principles.
1722 */
1723 void
1724 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1725 {
1726 range_tree_t *rt = vd->vdev_dtl[t];
1727
1728 ASSERT(t < DTL_TYPES);
1729 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1730 ASSERT(spa_writeable(vd->vdev_spa));
1731
1732 mutex_enter(rt->rt_lock);
1733 if (!range_tree_contains(rt, txg, size))
1734 range_tree_add(rt, txg, size);
1735 mutex_exit(rt->rt_lock);
1736 }
1737
1738 boolean_t
1739 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1740 {
1741 range_tree_t *rt = vd->vdev_dtl[t];
1742 boolean_t dirty = B_FALSE;
1743
1744 ASSERT(t < DTL_TYPES);
1745 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1746
1747 mutex_enter(rt->rt_lock);
1748 if (range_tree_space(rt) != 0)
1749 dirty = range_tree_contains(rt, txg, size);
1750 mutex_exit(rt->rt_lock);
1751
1752 return (dirty);
1753 }
1754
1755 boolean_t
1756 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
1757 {
1758 range_tree_t *rt = vd->vdev_dtl[t];
1759 boolean_t empty;
1760
1761 mutex_enter(rt->rt_lock);
1762 empty = (range_tree_space(rt) == 0);
1763 mutex_exit(rt->rt_lock);
1764
1765 return (empty);
1766 }
1767
1768 /*
1769 * Returns the lowest txg in the DTL range.
1770 */
1771 static uint64_t
1772 vdev_dtl_min(vdev_t *vd)
1773 {
1774 range_seg_t *rs;
1775
1776 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
1777 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1778 ASSERT0(vd->vdev_children);
1779
1780 rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
1781 return (rs->rs_start - 1);
1782 }
1783
1784 /*
1785 * Returns the highest txg in the DTL.
1786 */
1787 static uint64_t
1788 vdev_dtl_max(vdev_t *vd)
1789 {
1790 range_seg_t *rs;
1791
1792 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
1793 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1794 ASSERT0(vd->vdev_children);
1795
1796 rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
1797 return (rs->rs_end);
1798 }
1799
1800 /*
1801 * Determine if a resilvering vdev should remove any DTL entries from
1802 * its range. If the vdev was resilvering for the entire duration of the
1803 * scan then it should excise that range from its DTLs. Otherwise, this
1804 * vdev is considered partially resilvered and should leave its DTL
1805 * entries intact. The comment in vdev_dtl_reassess() describes how we
1806 * excise the DTLs.
1807 */
1808 static boolean_t
1809 vdev_dtl_should_excise(vdev_t *vd)
1810 {
1811 spa_t *spa = vd->vdev_spa;
1812 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1813
1814 ASSERT0(scn->scn_phys.scn_errors);
1815 ASSERT0(vd->vdev_children);
1816
1817 if (vd->vdev_resilver_txg == 0 ||
1818 range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0)
1819 return (B_TRUE);
1820
1821 /*
1822 * When a resilver is initiated the scan will assign the scn_max_txg
1823 * value to the highest txg value that exists in all DTLs. If this
1824 * device's max DTL is not part of this scan (i.e. it is not in
1825 * the range (scn_min_txg, scn_max_txg] then it is not eligible
1826 * for excision.
1827 */
1828 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
1829 ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
1830 ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
1831 ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
1832 return (B_TRUE);
1833 }
1834 return (B_FALSE);
1835 }
1836
1837 /*
1838 * Reassess DTLs after a config change or scrub completion.
1839 */
1840 void
1841 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1842 {
1843 spa_t *spa = vd->vdev_spa;
1844 avl_tree_t reftree;
1845 int c, t, minref;
1846
1847 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1848
1849 for (c = 0; c < vd->vdev_children; c++)
1850 vdev_dtl_reassess(vd->vdev_child[c], txg,
1851 scrub_txg, scrub_done);
1852
1853 if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux)
1854 return;
1855
1856 if (vd->vdev_ops->vdev_op_leaf) {
1857 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1858
1859 mutex_enter(&vd->vdev_dtl_lock);
1860
1861 /*
1862 * If we've completed a scan cleanly then determine
1863 * if this vdev should remove any DTLs. We only want to
1864 * excise regions on vdevs that were available during
1865 * the entire duration of this scan.
1866 */
1867 if (scrub_txg != 0 &&
1868 (spa->spa_scrub_started ||
1869 (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
1870 vdev_dtl_should_excise(vd)) {
1871 /*
1872 * We completed a scrub up to scrub_txg. If we
1873 * did it without rebooting, then the scrub dtl
1874 * will be valid, so excise the old region and
1875 * fold in the scrub dtl. Otherwise, leave the
1876 * dtl as-is if there was an error.
1877 *
1878 * There's little trick here: to excise the beginning
1879 * of the DTL_MISSING map, we put it into a reference
1880 * tree and then add a segment with refcnt -1 that
1881 * covers the range [0, scrub_txg). This means
1882 * that each txg in that range has refcnt -1 or 0.
1883 * We then add DTL_SCRUB with a refcnt of 2, so that
1884 * entries in the range [0, scrub_txg) will have a
1885 * positive refcnt -- either 1 or 2. We then convert
1886 * the reference tree into the new DTL_MISSING map.
1887 */
1888 space_reftree_create(&reftree);
1889 space_reftree_add_map(&reftree,
1890 vd->vdev_dtl[DTL_MISSING], 1);
1891 space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
1892 space_reftree_add_map(&reftree,
1893 vd->vdev_dtl[DTL_SCRUB], 2);
1894 space_reftree_generate_map(&reftree,
1895 vd->vdev_dtl[DTL_MISSING], 1);
1896 space_reftree_destroy(&reftree);
1897 }
1898 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
1899 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
1900 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
1901 if (scrub_done)
1902 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
1903 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
1904 if (!vdev_readable(vd))
1905 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
1906 else
1907 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
1908 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
1909
1910 /*
1911 * If the vdev was resilvering and no longer has any
1912 * DTLs then reset its resilvering flag and dirty
1913 * the top level so that we persist the change.
1914 */
1915 if (vd->vdev_resilver_txg != 0 &&
1916 range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 &&
1917 range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0) {
1918 vd->vdev_resilver_txg = 0;
1919 vdev_config_dirty(vd->vdev_top);
1920 }
1921
1922 mutex_exit(&vd->vdev_dtl_lock);
1923
1924 if (txg != 0)
1925 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1926 return;
1927 }
1928
1929 mutex_enter(&vd->vdev_dtl_lock);
1930 for (t = 0; t < DTL_TYPES; t++) {
1931 int c;
1932
1933 /* account for child's outage in parent's missing map */
1934 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
1935 if (t == DTL_SCRUB)
1936 continue; /* leaf vdevs only */
1937 if (t == DTL_PARTIAL)
1938 minref = 1; /* i.e. non-zero */
1939 else if (vd->vdev_nparity != 0)
1940 minref = vd->vdev_nparity + 1; /* RAID-Z */
1941 else
1942 minref = vd->vdev_children; /* any kind of mirror */
1943 space_reftree_create(&reftree);
1944 for (c = 0; c < vd->vdev_children; c++) {
1945 vdev_t *cvd = vd->vdev_child[c];
1946 mutex_enter(&cvd->vdev_dtl_lock);
1947 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
1948 mutex_exit(&cvd->vdev_dtl_lock);
1949 }
1950 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
1951 space_reftree_destroy(&reftree);
1952 }
1953 mutex_exit(&vd->vdev_dtl_lock);
1954 }
1955
1956 int
1957 vdev_dtl_load(vdev_t *vd)
1958 {
1959 spa_t *spa = vd->vdev_spa;
1960 objset_t *mos = spa->spa_meta_objset;
1961 int error = 0;
1962 int c;
1963
1964 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
1965 ASSERT(!vd->vdev_ishole);
1966
1967 error = space_map_open(&vd->vdev_dtl_sm, mos,
1968 vd->vdev_dtl_object, 0, -1ULL, 0, &vd->vdev_dtl_lock);
1969 if (error)
1970 return (error);
1971 ASSERT(vd->vdev_dtl_sm != NULL);
1972
1973 mutex_enter(&vd->vdev_dtl_lock);
1974
1975 /*
1976 * Now that we've opened the space_map we need to update
1977 * the in-core DTL.
1978 */
1979 space_map_update(vd->vdev_dtl_sm);
1980
1981 error = space_map_load(vd->vdev_dtl_sm,
1982 vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
1983 mutex_exit(&vd->vdev_dtl_lock);
1984
1985 return (error);
1986 }
1987
1988 for (c = 0; c < vd->vdev_children; c++) {
1989 error = vdev_dtl_load(vd->vdev_child[c]);
1990 if (error != 0)
1991 break;
1992 }
1993
1994 return (error);
1995 }
1996
1997 void
1998 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
1999 {
2000 spa_t *spa = vd->vdev_spa;
2001
2002 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
2003 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2004 zapobj, tx));
2005 }
2006
2007 uint64_t
2008 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
2009 {
2010 spa_t *spa = vd->vdev_spa;
2011 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
2012 DMU_OT_NONE, 0, tx);
2013
2014 ASSERT(zap != 0);
2015 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2016 zap, tx));
2017
2018 return (zap);
2019 }
2020
2021 void
2022 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
2023 {
2024 uint64_t i;
2025
2026 if (vd->vdev_ops != &vdev_hole_ops &&
2027 vd->vdev_ops != &vdev_missing_ops &&
2028 vd->vdev_ops != &vdev_root_ops &&
2029 !vd->vdev_top->vdev_removing) {
2030 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
2031 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
2032 }
2033 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
2034 vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
2035 }
2036 }
2037 for (i = 0; i < vd->vdev_children; i++) {
2038 vdev_construct_zaps(vd->vdev_child[i], tx);
2039 }
2040 }
2041
2042 void
2043 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
2044 {
2045 spa_t *spa = vd->vdev_spa;
2046 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
2047 objset_t *mos = spa->spa_meta_objset;
2048 range_tree_t *rtsync;
2049 kmutex_t rtlock;
2050 dmu_tx_t *tx;
2051 uint64_t object = space_map_object(vd->vdev_dtl_sm);
2052
2053 ASSERT(!vd->vdev_ishole);
2054 ASSERT(vd->vdev_ops->vdev_op_leaf);
2055
2056 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2057
2058 if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
2059 mutex_enter(&vd->vdev_dtl_lock);
2060 space_map_free(vd->vdev_dtl_sm, tx);
2061 space_map_close(vd->vdev_dtl_sm);
2062 vd->vdev_dtl_sm = NULL;
2063 mutex_exit(&vd->vdev_dtl_lock);
2064
2065 /*
2066 * We only destroy the leaf ZAP for detached leaves or for
2067 * removed log devices. Removed data devices handle leaf ZAP
2068 * cleanup later, once cancellation is no longer possible.
2069 */
2070 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
2071 vd->vdev_top->vdev_islog)) {
2072 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
2073 vd->vdev_leaf_zap = 0;
2074 }
2075
2076 dmu_tx_commit(tx);
2077 return;
2078 }
2079
2080 if (vd->vdev_dtl_sm == NULL) {
2081 uint64_t new_object;
2082
2083 new_object = space_map_alloc(mos, tx);
2084 VERIFY3U(new_object, !=, 0);
2085
2086 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
2087 0, -1ULL, 0, &vd->vdev_dtl_lock));
2088 ASSERT(vd->vdev_dtl_sm != NULL);
2089 }
2090
2091 mutex_init(&rtlock, NULL, MUTEX_DEFAULT, NULL);
2092
2093 rtsync = range_tree_create(NULL, NULL, &rtlock);
2094
2095 mutex_enter(&rtlock);
2096
2097 mutex_enter(&vd->vdev_dtl_lock);
2098 range_tree_walk(rt, range_tree_add, rtsync);
2099 mutex_exit(&vd->vdev_dtl_lock);
2100
2101 space_map_truncate(vd->vdev_dtl_sm, tx);
2102 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx);
2103 range_tree_vacate(rtsync, NULL, NULL);
2104
2105 range_tree_destroy(rtsync);
2106
2107 mutex_exit(&rtlock);
2108 mutex_destroy(&rtlock);
2109
2110 /*
2111 * If the object for the space map has changed then dirty
2112 * the top level so that we update the config.
2113 */
2114 if (object != space_map_object(vd->vdev_dtl_sm)) {
2115 zfs_dbgmsg("txg %llu, spa %s, DTL old object %llu, "
2116 "new object %llu", txg, spa_name(spa), object,
2117 space_map_object(vd->vdev_dtl_sm));
2118 vdev_config_dirty(vd->vdev_top);
2119 }
2120
2121 dmu_tx_commit(tx);
2122
2123 mutex_enter(&vd->vdev_dtl_lock);
2124 space_map_update(vd->vdev_dtl_sm);
2125 mutex_exit(&vd->vdev_dtl_lock);
2126 }
2127
2128 /*
2129 * Determine whether the specified vdev can be offlined/detached/removed
2130 * without losing data.
2131 */
2132 boolean_t
2133 vdev_dtl_required(vdev_t *vd)
2134 {
2135 spa_t *spa = vd->vdev_spa;
2136 vdev_t *tvd = vd->vdev_top;
2137 uint8_t cant_read = vd->vdev_cant_read;
2138 boolean_t required;
2139
2140 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2141
2142 if (vd == spa->spa_root_vdev || vd == tvd)
2143 return (B_TRUE);
2144
2145 /*
2146 * Temporarily mark the device as unreadable, and then determine
2147 * whether this results in any DTL outages in the top-level vdev.
2148 * If not, we can safely offline/detach/remove the device.
2149 */
2150 vd->vdev_cant_read = B_TRUE;
2151 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2152 required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
2153 vd->vdev_cant_read = cant_read;
2154 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2155
2156 if (!required && zio_injection_enabled)
2157 required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2158
2159 return (required);
2160 }
2161
2162 /*
2163 * Determine if resilver is needed, and if so the txg range.
2164 */
2165 boolean_t
2166 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2167 {
2168 boolean_t needed = B_FALSE;
2169 uint64_t thismin = UINT64_MAX;
2170 uint64_t thismax = 0;
2171 int c;
2172
2173 if (vd->vdev_children == 0) {
2174 mutex_enter(&vd->vdev_dtl_lock);
2175 if (range_tree_space(vd->vdev_dtl[DTL_MISSING]) != 0 &&
2176 vdev_writeable(vd)) {
2177
2178 thismin = vdev_dtl_min(vd);
2179 thismax = vdev_dtl_max(vd);
2180 needed = B_TRUE;
2181 }
2182 mutex_exit(&vd->vdev_dtl_lock);
2183 } else {
2184 for (c = 0; c < vd->vdev_children; c++) {
2185 vdev_t *cvd = vd->vdev_child[c];
2186 uint64_t cmin, cmax;
2187
2188 if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2189 thismin = MIN(thismin, cmin);
2190 thismax = MAX(thismax, cmax);
2191 needed = B_TRUE;
2192 }
2193 }
2194 }
2195
2196 if (needed && minp) {
2197 *minp = thismin;
2198 *maxp = thismax;
2199 }
2200 return (needed);
2201 }
2202
2203 void
2204 vdev_load(vdev_t *vd)
2205 {
2206 int c;
2207
2208 /*
2209 * Recursively load all children.
2210 */
2211 for (c = 0; c < vd->vdev_children; c++)
2212 vdev_load(vd->vdev_child[c]);
2213
2214 /*
2215 * If this is a top-level vdev, initialize its metaslabs.
2216 */
2217 if (vd == vd->vdev_top && !vd->vdev_ishole &&
2218 (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
2219 vdev_metaslab_init(vd, 0) != 0))
2220 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2221 VDEV_AUX_CORRUPT_DATA);
2222
2223 /*
2224 * If this is a leaf vdev, load its DTL.
2225 */
2226 if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
2227 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2228 VDEV_AUX_CORRUPT_DATA);
2229 }
2230
2231 /*
2232 * The special vdev case is used for hot spares and l2cache devices. Its
2233 * sole purpose it to set the vdev state for the associated vdev. To do this,
2234 * we make sure that we can open the underlying device, then try to read the
2235 * label, and make sure that the label is sane and that it hasn't been
2236 * repurposed to another pool.
2237 */
2238 int
2239 vdev_validate_aux(vdev_t *vd)
2240 {
2241 nvlist_t *label;
2242 uint64_t guid, version;
2243 uint64_t state;
2244
2245 if (!vdev_readable(vd))
2246 return (0);
2247
2248 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
2249 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2250 VDEV_AUX_CORRUPT_DATA);
2251 return (-1);
2252 }
2253
2254 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
2255 !SPA_VERSION_IS_SUPPORTED(version) ||
2256 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
2257 guid != vd->vdev_guid ||
2258 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
2259 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2260 VDEV_AUX_CORRUPT_DATA);
2261 nvlist_free(label);
2262 return (-1);
2263 }
2264
2265 /*
2266 * We don't actually check the pool state here. If it's in fact in
2267 * use by another pool, we update this fact on the fly when requested.
2268 */
2269 nvlist_free(label);
2270 return (0);
2271 }
2272
2273 void
2274 vdev_remove(vdev_t *vd, uint64_t txg)
2275 {
2276 spa_t *spa = vd->vdev_spa;
2277 objset_t *mos = spa->spa_meta_objset;
2278 dmu_tx_t *tx;
2279 int m, i;
2280
2281 tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
2282 ASSERT(vd == vd->vdev_top);
2283 ASSERT3U(txg, ==, spa_syncing_txg(spa));
2284
2285 if (vd->vdev_ms != NULL) {
2286 metaslab_group_t *mg = vd->vdev_mg;
2287
2288 metaslab_group_histogram_verify(mg);
2289 metaslab_class_histogram_verify(mg->mg_class);
2290
2291 for (m = 0; m < vd->vdev_ms_count; m++) {
2292 metaslab_t *msp = vd->vdev_ms[m];
2293
2294 if (msp == NULL || msp->ms_sm == NULL)
2295 continue;
2296
2297 mutex_enter(&msp->ms_lock);
2298 /*
2299 * If the metaslab was not loaded when the vdev
2300 * was removed then the histogram accounting may
2301 * not be accurate. Update the histogram information
2302 * here so that we ensure that the metaslab group
2303 * and metaslab class are up-to-date.
2304 */
2305 metaslab_group_histogram_remove(mg, msp);
2306
2307 VERIFY0(space_map_allocated(msp->ms_sm));
2308 space_map_free(msp->ms_sm, tx);
2309 space_map_close(msp->ms_sm);
2310 msp->ms_sm = NULL;
2311 mutex_exit(&msp->ms_lock);
2312 }
2313
2314 metaslab_group_histogram_verify(mg);
2315 metaslab_class_histogram_verify(mg->mg_class);
2316 for (i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
2317 ASSERT0(mg->mg_histogram[i]);
2318
2319 }
2320
2321 if (vd->vdev_ms_array) {
2322 (void) dmu_object_free(mos, vd->vdev_ms_array, tx);
2323 vd->vdev_ms_array = 0;
2324 }
2325
2326 if (vd->vdev_islog && vd->vdev_top_zap != 0) {
2327 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
2328 vd->vdev_top_zap = 0;
2329 }
2330 dmu_tx_commit(tx);
2331 }
2332
2333 void
2334 vdev_sync_done(vdev_t *vd, uint64_t txg)
2335 {
2336 metaslab_t *msp;
2337 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2338
2339 ASSERT(!vd->vdev_ishole);
2340
2341 while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))))
2342 metaslab_sync_done(msp, txg);
2343
2344 if (reassess)
2345 metaslab_sync_reassess(vd->vdev_mg);
2346 }
2347
2348 void
2349 vdev_sync(vdev_t *vd, uint64_t txg)
2350 {
2351 spa_t *spa = vd->vdev_spa;
2352 vdev_t *lvd;
2353 metaslab_t *msp;
2354 dmu_tx_t *tx;
2355
2356 ASSERT(!vd->vdev_ishole);
2357
2358 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
2359 ASSERT(vd == vd->vdev_top);
2360 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2361 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
2362 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
2363 ASSERT(vd->vdev_ms_array != 0);
2364 vdev_config_dirty(vd);
2365 dmu_tx_commit(tx);
2366 }
2367
2368 /*
2369 * Remove the metadata associated with this vdev once it's empty.
2370 */
2371 if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
2372 vdev_remove(vd, txg);
2373
2374 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2375 metaslab_sync(msp, txg);
2376 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
2377 }
2378
2379 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2380 vdev_dtl_sync(lvd, txg);
2381
2382 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2383 }
2384
2385 uint64_t
2386 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2387 {
2388 return (vd->vdev_ops->vdev_op_asize(vd, psize));
2389 }
2390
2391 /*
2392 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
2393 * not be opened, and no I/O is attempted.
2394 */
2395 int
2396 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2397 {
2398 vdev_t *vd, *tvd;
2399
2400 spa_vdev_state_enter(spa, SCL_NONE);
2401
2402 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2403 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2404
2405 if (!vd->vdev_ops->vdev_op_leaf)
2406 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2407
2408 tvd = vd->vdev_top;
2409
2410 /*
2411 * We don't directly use the aux state here, but if we do a
2412 * vdev_reopen(), we need this value to be present to remember why we
2413 * were faulted.
2414 */
2415 vd->vdev_label_aux = aux;
2416
2417 /*
2418 * Faulted state takes precedence over degraded.
2419 */
2420 vd->vdev_delayed_close = B_FALSE;
2421 vd->vdev_faulted = 1ULL;
2422 vd->vdev_degraded = 0ULL;
2423 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
2424
2425 /*
2426 * If this device has the only valid copy of the data, then
2427 * back off and simply mark the vdev as degraded instead.
2428 */
2429 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
2430 vd->vdev_degraded = 1ULL;
2431 vd->vdev_faulted = 0ULL;
2432
2433 /*
2434 * If we reopen the device and it's not dead, only then do we
2435 * mark it degraded.
2436 */
2437 vdev_reopen(tvd);
2438
2439 if (vdev_readable(vd))
2440 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
2441 }
2442
2443 return (spa_vdev_state_exit(spa, vd, 0));
2444 }
2445
2446 /*
2447 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
2448 * user that something is wrong. The vdev continues to operate as normal as far
2449 * as I/O is concerned.
2450 */
2451 int
2452 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2453 {
2454 vdev_t *vd;
2455
2456 spa_vdev_state_enter(spa, SCL_NONE);
2457
2458 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2459 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2460
2461 if (!vd->vdev_ops->vdev_op_leaf)
2462 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2463
2464 /*
2465 * If the vdev is already faulted, then don't do anything.
2466 */
2467 if (vd->vdev_faulted || vd->vdev_degraded)
2468 return (spa_vdev_state_exit(spa, NULL, 0));
2469
2470 vd->vdev_degraded = 1ULL;
2471 if (!vdev_is_dead(vd))
2472 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
2473 aux);
2474
2475 return (spa_vdev_state_exit(spa, vd, 0));
2476 }
2477
2478 /*
2479 * Online the given vdev.
2480 *
2481 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
2482 * spare device should be detached when the device finishes resilvering.
2483 * Second, the online should be treated like a 'test' online case, so no FMA
2484 * events are generated if the device fails to open.
2485 */
2486 int
2487 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
2488 {
2489 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
2490
2491 spa_vdev_state_enter(spa, SCL_NONE);
2492
2493 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2494 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2495
2496 if (!vd->vdev_ops->vdev_op_leaf)
2497 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2498
2499 tvd = vd->vdev_top;
2500 vd->vdev_offline = B_FALSE;
2501 vd->vdev_tmpoffline = B_FALSE;
2502 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
2503 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
2504
2505 /* XXX - L2ARC 1.0 does not support expansion */
2506 if (!vd->vdev_aux) {
2507 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2508 pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
2509 }
2510
2511 vdev_reopen(tvd);
2512 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
2513
2514 if (!vd->vdev_aux) {
2515 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2516 pvd->vdev_expanding = B_FALSE;
2517 }
2518
2519 if (newstate)
2520 *newstate = vd->vdev_state;
2521 if ((flags & ZFS_ONLINE_UNSPARE) &&
2522 !vdev_is_dead(vd) && vd->vdev_parent &&
2523 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2524 vd->vdev_parent->vdev_child[0] == vd)
2525 vd->vdev_unspare = B_TRUE;
2526
2527 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
2528
2529 /* XXX - L2ARC 1.0 does not support expansion */
2530 if (vd->vdev_aux)
2531 return (spa_vdev_state_exit(spa, vd, ENOTSUP));
2532 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2533 }
2534 return (spa_vdev_state_exit(spa, vd, 0));
2535 }
2536
2537 static int
2538 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
2539 {
2540 vdev_t *vd, *tvd;
2541 int error = 0;
2542 uint64_t generation;
2543 metaslab_group_t *mg;
2544
2545 top:
2546 spa_vdev_state_enter(spa, SCL_ALLOC);
2547
2548 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2549 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2550
2551 if (!vd->vdev_ops->vdev_op_leaf)
2552 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2553
2554 tvd = vd->vdev_top;
2555 mg = tvd->vdev_mg;
2556 generation = spa->spa_config_generation + 1;
2557
2558 /*
2559 * If the device isn't already offline, try to offline it.
2560 */
2561 if (!vd->vdev_offline) {
2562 /*
2563 * If this device has the only valid copy of some data,
2564 * don't allow it to be offlined. Log devices are always
2565 * expendable.
2566 */
2567 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2568 vdev_dtl_required(vd))
2569 return (spa_vdev_state_exit(spa, NULL, EBUSY));
2570
2571 /*
2572 * If the top-level is a slog and it has had allocations
2573 * then proceed. We check that the vdev's metaslab group
2574 * is not NULL since it's possible that we may have just
2575 * added this vdev but not yet initialized its metaslabs.
2576 */
2577 if (tvd->vdev_islog && mg != NULL) {
2578 /*
2579 * Prevent any future allocations.
2580 */
2581 metaslab_group_passivate(mg);
2582 (void) spa_vdev_state_exit(spa, vd, 0);
2583
2584 error = spa_offline_log(spa);
2585
2586 spa_vdev_state_enter(spa, SCL_ALLOC);
2587
2588 /*
2589 * Check to see if the config has changed.
2590 */
2591 if (error || generation != spa->spa_config_generation) {
2592 metaslab_group_activate(mg);
2593 if (error)
2594 return (spa_vdev_state_exit(spa,
2595 vd, error));
2596 (void) spa_vdev_state_exit(spa, vd, 0);
2597 goto top;
2598 }
2599 ASSERT0(tvd->vdev_stat.vs_alloc);
2600 }
2601
2602 /*
2603 * Offline this device and reopen its top-level vdev.
2604 * If the top-level vdev is a log device then just offline
2605 * it. Otherwise, if this action results in the top-level
2606 * vdev becoming unusable, undo it and fail the request.
2607 */
2608 vd->vdev_offline = B_TRUE;
2609 vdev_reopen(tvd);
2610
2611 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2612 vdev_is_dead(tvd)) {
2613 vd->vdev_offline = B_FALSE;
2614 vdev_reopen(tvd);
2615 return (spa_vdev_state_exit(spa, NULL, EBUSY));
2616 }
2617
2618 /*
2619 * Add the device back into the metaslab rotor so that
2620 * once we online the device it's open for business.
2621 */
2622 if (tvd->vdev_islog && mg != NULL)
2623 metaslab_group_activate(mg);
2624 }
2625
2626 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
2627
2628 return (spa_vdev_state_exit(spa, vd, 0));
2629 }
2630
2631 int
2632 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
2633 {
2634 int error;
2635
2636 mutex_enter(&spa->spa_vdev_top_lock);
2637 error = vdev_offline_locked(spa, guid, flags);
2638 mutex_exit(&spa->spa_vdev_top_lock);
2639
2640 return (error);
2641 }
2642
2643 /*
2644 * Clear the error counts associated with this vdev. Unlike vdev_online() and
2645 * vdev_offline(), we assume the spa config is locked. We also clear all
2646 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
2647 */
2648 void
2649 vdev_clear(spa_t *spa, vdev_t *vd)
2650 {
2651 vdev_t *rvd = spa->spa_root_vdev;
2652 int c;
2653
2654 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2655
2656 if (vd == NULL)
2657 vd = rvd;
2658
2659 vd->vdev_stat.vs_read_errors = 0;
2660 vd->vdev_stat.vs_write_errors = 0;
2661 vd->vdev_stat.vs_checksum_errors = 0;
2662
2663 for (c = 0; c < vd->vdev_children; c++)
2664 vdev_clear(spa, vd->vdev_child[c]);
2665
2666 /*
2667 * If we're in the FAULTED state or have experienced failed I/O, then
2668 * clear the persistent state and attempt to reopen the device. We
2669 * also mark the vdev config dirty, so that the new faulted state is
2670 * written out to disk.
2671 */
2672 if (vd->vdev_faulted || vd->vdev_degraded ||
2673 !vdev_readable(vd) || !vdev_writeable(vd)) {
2674
2675 /*
2676 * When reopening in reponse to a clear event, it may be due to
2677 * a fmadm repair request. In this case, if the device is
2678 * still broken, we want to still post the ereport again.
2679 */
2680 vd->vdev_forcefault = B_TRUE;
2681
2682 vd->vdev_faulted = vd->vdev_degraded = 0ULL;
2683 vd->vdev_cant_read = B_FALSE;
2684 vd->vdev_cant_write = B_FALSE;
2685
2686 vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
2687
2688 vd->vdev_forcefault = B_FALSE;
2689
2690 if (vd != rvd && vdev_writeable(vd->vdev_top))
2691 vdev_state_dirty(vd->vdev_top);
2692
2693 if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
2694 spa_async_request(spa, SPA_ASYNC_RESILVER);
2695
2696 spa_event_notify(spa, vd, FM_EREPORT_ZFS_DEVICE_CLEAR);
2697 }
2698
2699 /*
2700 * When clearing a FMA-diagnosed fault, we always want to
2701 * unspare the device, as we assume that the original spare was
2702 * done in response to the FMA fault.
2703 */
2704 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
2705 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2706 vd->vdev_parent->vdev_child[0] == vd)
2707 vd->vdev_unspare = B_TRUE;
2708 }
2709
2710 boolean_t
2711 vdev_is_dead(vdev_t *vd)
2712 {
2713 /*
2714 * Holes and missing devices are always considered "dead".
2715 * This simplifies the code since we don't have to check for
2716 * these types of devices in the various code paths.
2717 * Instead we rely on the fact that we skip over dead devices
2718 * before issuing I/O to them.
2719 */
2720 return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole ||
2721 vd->vdev_ops == &vdev_missing_ops);
2722 }
2723
2724 boolean_t
2725 vdev_readable(vdev_t *vd)
2726 {
2727 return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
2728 }
2729
2730 boolean_t
2731 vdev_writeable(vdev_t *vd)
2732 {
2733 return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
2734 }
2735
2736 boolean_t
2737 vdev_allocatable(vdev_t *vd)
2738 {
2739 uint64_t state = vd->vdev_state;
2740
2741 /*
2742 * We currently allow allocations from vdevs which may be in the
2743 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
2744 * fails to reopen then we'll catch it later when we're holding
2745 * the proper locks. Note that we have to get the vdev state
2746 * in a local variable because although it changes atomically,
2747 * we're asking two separate questions about it.
2748 */
2749 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
2750 !vd->vdev_cant_write && !vd->vdev_ishole);
2751 }
2752
2753 boolean_t
2754 vdev_accessible(vdev_t *vd, zio_t *zio)
2755 {
2756 ASSERT(zio->io_vd == vd);
2757
2758 if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
2759 return (B_FALSE);
2760
2761 if (zio->io_type == ZIO_TYPE_READ)
2762 return (!vd->vdev_cant_read);
2763
2764 if (zio->io_type == ZIO_TYPE_WRITE)
2765 return (!vd->vdev_cant_write);
2766
2767 return (B_TRUE);
2768 }
2769
2770 static void
2771 vdev_get_child_stat(vdev_t *cvd, vdev_stat_t *vs, vdev_stat_t *cvs)
2772 {
2773 int t;
2774 for (t = 0; t < ZIO_TYPES; t++) {
2775 vs->vs_ops[t] += cvs->vs_ops[t];
2776 vs->vs_bytes[t] += cvs->vs_bytes[t];
2777 }
2778
2779 cvs->vs_scan_removing = cvd->vdev_removing;
2780 }
2781
2782 /*
2783 * Get extended stats
2784 */
2785 static void
2786 vdev_get_child_stat_ex(vdev_t *cvd, vdev_stat_ex_t *vsx, vdev_stat_ex_t *cvsx)
2787 {
2788 int t, b;
2789 for (t = 0; t < ZIO_TYPES; t++) {
2790 for (b = 0; b < ARRAY_SIZE(vsx->vsx_disk_histo[0]); b++)
2791 vsx->vsx_disk_histo[t][b] += cvsx->vsx_disk_histo[t][b];
2792
2793 for (b = 0; b < ARRAY_SIZE(vsx->vsx_total_histo[0]); b++) {
2794 vsx->vsx_total_histo[t][b] +=
2795 cvsx->vsx_total_histo[t][b];
2796 }
2797 }
2798
2799 for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) {
2800 for (b = 0; b < ARRAY_SIZE(vsx->vsx_queue_histo[0]); b++) {
2801 vsx->vsx_queue_histo[t][b] +=
2802 cvsx->vsx_queue_histo[t][b];
2803 }
2804 vsx->vsx_active_queue[t] += cvsx->vsx_active_queue[t];
2805 vsx->vsx_pend_queue[t] += cvsx->vsx_pend_queue[t];
2806
2807 for (b = 0; b < ARRAY_SIZE(vsx->vsx_ind_histo[0]); b++)
2808 vsx->vsx_ind_histo[t][b] += cvsx->vsx_ind_histo[t][b];
2809
2810 for (b = 0; b < ARRAY_SIZE(vsx->vsx_agg_histo[0]); b++)
2811 vsx->vsx_agg_histo[t][b] += cvsx->vsx_agg_histo[t][b];
2812 }
2813
2814 }
2815
2816 /*
2817 * Get statistics for the given vdev.
2818 */
2819 static void
2820 vdev_get_stats_ex_impl(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
2821 {
2822 int c, t;
2823 /*
2824 * If we're getting stats on the root vdev, aggregate the I/O counts
2825 * over all top-level vdevs (i.e. the direct children of the root).
2826 */
2827 if (!vd->vdev_ops->vdev_op_leaf) {
2828 if (vs) {
2829 memset(vs->vs_ops, 0, sizeof (vs->vs_ops));
2830 memset(vs->vs_bytes, 0, sizeof (vs->vs_bytes));
2831 }
2832 if (vsx)
2833 memset(vsx, 0, sizeof (*vsx));
2834
2835 for (c = 0; c < vd->vdev_children; c++) {
2836 vdev_t *cvd = vd->vdev_child[c];
2837 vdev_stat_t *cvs = &cvd->vdev_stat;
2838 vdev_stat_ex_t *cvsx = &cvd->vdev_stat_ex;
2839
2840 vdev_get_stats_ex_impl(cvd, cvs, cvsx);
2841 if (vs)
2842 vdev_get_child_stat(cvd, vs, cvs);
2843 if (vsx)
2844 vdev_get_child_stat_ex(cvd, vsx, cvsx);
2845
2846 }
2847 } else {
2848 /*
2849 * We're a leaf. Just copy our ZIO active queue stats in. The
2850 * other leaf stats are updated in vdev_stat_update().
2851 */
2852 if (!vsx)
2853 return;
2854
2855 memcpy(vsx, &vd->vdev_stat_ex, sizeof (vd->vdev_stat_ex));
2856
2857 for (t = 0; t < ARRAY_SIZE(vd->vdev_queue.vq_class); t++) {
2858 vsx->vsx_active_queue[t] =
2859 vd->vdev_queue.vq_class[t].vqc_active;
2860 vsx->vsx_pend_queue[t] = avl_numnodes(
2861 &vd->vdev_queue.vq_class[t].vqc_queued_tree);
2862 }
2863 }
2864 }
2865
2866 void
2867 vdev_get_stats_ex(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
2868 {
2869 mutex_enter(&vd->vdev_stat_lock);
2870 if (vs) {
2871 bcopy(&vd->vdev_stat, vs, sizeof (*vs));
2872 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2873 vs->vs_state = vd->vdev_state;
2874 vs->vs_rsize = vdev_get_min_asize(vd);
2875 if (vd->vdev_ops->vdev_op_leaf)
2876 vs->vs_rsize += VDEV_LABEL_START_SIZE +
2877 VDEV_LABEL_END_SIZE;
2878 vs->vs_esize = vd->vdev_max_asize - vd->vdev_asize;
2879 if (vd->vdev_aux == NULL && vd == vd->vdev_top &&
2880 !vd->vdev_ishole) {
2881 vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation;
2882 }
2883 }
2884
2885 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_READER) != 0);
2886 vdev_get_stats_ex_impl(vd, vs, vsx);
2887 mutex_exit(&vd->vdev_stat_lock);
2888 }
2889
2890 void
2891 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
2892 {
2893 return (vdev_get_stats_ex(vd, vs, NULL));
2894 }
2895
2896 void
2897 vdev_clear_stats(vdev_t *vd)
2898 {
2899 mutex_enter(&vd->vdev_stat_lock);
2900 vd->vdev_stat.vs_space = 0;
2901 vd->vdev_stat.vs_dspace = 0;
2902 vd->vdev_stat.vs_alloc = 0;
2903 mutex_exit(&vd->vdev_stat_lock);
2904 }
2905
2906 void
2907 vdev_scan_stat_init(vdev_t *vd)
2908 {
2909 vdev_stat_t *vs = &vd->vdev_stat;
2910 int c;
2911
2912 for (c = 0; c < vd->vdev_children; c++)
2913 vdev_scan_stat_init(vd->vdev_child[c]);
2914
2915 mutex_enter(&vd->vdev_stat_lock);
2916 vs->vs_scan_processed = 0;
2917 mutex_exit(&vd->vdev_stat_lock);
2918 }
2919
2920 void
2921 vdev_stat_update(zio_t *zio, uint64_t psize)
2922 {
2923 spa_t *spa = zio->io_spa;
2924 vdev_t *rvd = spa->spa_root_vdev;
2925 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
2926 vdev_t *pvd;
2927 uint64_t txg = zio->io_txg;
2928 vdev_stat_t *vs = &vd->vdev_stat;
2929 vdev_stat_ex_t *vsx = &vd->vdev_stat_ex;
2930 zio_type_t type = zio->io_type;
2931 int flags = zio->io_flags;
2932
2933 /*
2934 * If this i/o is a gang leader, it didn't do any actual work.
2935 */
2936 if (zio->io_gang_tree)
2937 return;
2938
2939 if (zio->io_error == 0) {
2940 /*
2941 * If this is a root i/o, don't count it -- we've already
2942 * counted the top-level vdevs, and vdev_get_stats() will
2943 * aggregate them when asked. This reduces contention on
2944 * the root vdev_stat_lock and implicitly handles blocks
2945 * that compress away to holes, for which there is no i/o.
2946 * (Holes never create vdev children, so all the counters
2947 * remain zero, which is what we want.)
2948 *
2949 * Note: this only applies to successful i/o (io_error == 0)
2950 * because unlike i/o counts, errors are not additive.
2951 * When reading a ditto block, for example, failure of
2952 * one top-level vdev does not imply a root-level error.
2953 */
2954 if (vd == rvd)
2955 return;
2956
2957 ASSERT(vd == zio->io_vd);
2958
2959 if (flags & ZIO_FLAG_IO_BYPASS)
2960 return;
2961
2962 mutex_enter(&vd->vdev_stat_lock);
2963
2964 if (flags & ZIO_FLAG_IO_REPAIR) {
2965 if (flags & ZIO_FLAG_SCAN_THREAD) {
2966 dsl_scan_phys_t *scn_phys =
2967 &spa->spa_dsl_pool->dp_scan->scn_phys;
2968 uint64_t *processed = &scn_phys->scn_processed;
2969
2970 /* XXX cleanup? */
2971 if (vd->vdev_ops->vdev_op_leaf)
2972 atomic_add_64(processed, psize);
2973 vs->vs_scan_processed += psize;
2974 }
2975
2976 if (flags & ZIO_FLAG_SELF_HEAL)
2977 vs->vs_self_healed += psize;
2978 }
2979
2980 /*
2981 * The bytes/ops/histograms are recorded at the leaf level and
2982 * aggregated into the higher level vdevs in vdev_get_stats().
2983 */
2984 if (vd->vdev_ops->vdev_op_leaf) {
2985
2986 vs->vs_ops[type]++;
2987 vs->vs_bytes[type] += psize;
2988
2989 if (flags & ZIO_FLAG_DELEGATED) {
2990 vsx->vsx_agg_histo[zio->io_priority]
2991 [RQ_HISTO(zio->io_size)]++;
2992 } else {
2993 vsx->vsx_ind_histo[zio->io_priority]
2994 [RQ_HISTO(zio->io_size)]++;
2995 }
2996
2997 if (zio->io_delta && zio->io_delay) {
2998 vsx->vsx_queue_histo[zio->io_priority]
2999 [L_HISTO(zio->io_delta - zio->io_delay)]++;
3000 vsx->vsx_disk_histo[type]
3001 [L_HISTO(zio->io_delay)]++;
3002 vsx->vsx_total_histo[type]
3003 [L_HISTO(zio->io_delta)]++;
3004 }
3005 }
3006
3007 mutex_exit(&vd->vdev_stat_lock);
3008 return;
3009 }
3010
3011 if (flags & ZIO_FLAG_SPECULATIVE)
3012 return;
3013
3014 /*
3015 * If this is an I/O error that is going to be retried, then ignore the
3016 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
3017 * hard errors, when in reality they can happen for any number of
3018 * innocuous reasons (bus resets, MPxIO link failure, etc).
3019 */
3020 if (zio->io_error == EIO &&
3021 !(zio->io_flags & ZIO_FLAG_IO_RETRY))
3022 return;
3023
3024 /*
3025 * Intent logs writes won't propagate their error to the root
3026 * I/O so don't mark these types of failures as pool-level
3027 * errors.
3028 */
3029 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
3030 return;
3031
3032 mutex_enter(&vd->vdev_stat_lock);
3033 if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
3034 if (zio->io_error == ECKSUM)
3035 vs->vs_checksum_errors++;
3036 else
3037 vs->vs_read_errors++;
3038 }
3039 if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
3040 vs->vs_write_errors++;
3041 mutex_exit(&vd->vdev_stat_lock);
3042
3043 if (type == ZIO_TYPE_WRITE && txg != 0 &&
3044 (!(flags & ZIO_FLAG_IO_REPAIR) ||
3045 (flags & ZIO_FLAG_SCAN_THREAD) ||
3046 spa->spa_claiming)) {
3047 /*
3048 * This is either a normal write (not a repair), or it's
3049 * a repair induced by the scrub thread, or it's a repair
3050 * made by zil_claim() during spa_load() in the first txg.
3051 * In the normal case, we commit the DTL change in the same
3052 * txg as the block was born. In the scrub-induced repair
3053 * case, we know that scrubs run in first-pass syncing context,
3054 * so we commit the DTL change in spa_syncing_txg(spa).
3055 * In the zil_claim() case, we commit in spa_first_txg(spa).
3056 *
3057 * We currently do not make DTL entries for failed spontaneous
3058 * self-healing writes triggered by normal (non-scrubbing)
3059 * reads, because we have no transactional context in which to
3060 * do so -- and it's not clear that it'd be desirable anyway.
3061 */
3062 if (vd->vdev_ops->vdev_op_leaf) {
3063 uint64_t commit_txg = txg;
3064 if (flags & ZIO_FLAG_SCAN_THREAD) {
3065 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
3066 ASSERT(spa_sync_pass(spa) == 1);
3067 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
3068 commit_txg = spa_syncing_txg(spa);
3069 } else if (spa->spa_claiming) {
3070 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
3071 commit_txg = spa_first_txg(spa);
3072 }
3073 ASSERT(commit_txg >= spa_syncing_txg(spa));
3074 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
3075 return;
3076 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3077 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
3078 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
3079 }
3080 if (vd != rvd)
3081 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
3082 }
3083 }
3084
3085 /*
3086 * Update the in-core space usage stats for this vdev, its metaslab class,
3087 * and the root vdev.
3088 */
3089 void
3090 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
3091 int64_t space_delta)
3092 {
3093 int64_t dspace_delta = space_delta;
3094 spa_t *spa = vd->vdev_spa;
3095 vdev_t *rvd = spa->spa_root_vdev;
3096 metaslab_group_t *mg = vd->vdev_mg;
3097 metaslab_class_t *mc = mg ? mg->mg_class : NULL;
3098
3099 ASSERT(vd == vd->vdev_top);
3100
3101 /*
3102 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
3103 * factor. We must calculate this here and not at the root vdev
3104 * because the root vdev's psize-to-asize is simply the max of its
3105 * childrens', thus not accurate enough for us.
3106 */
3107 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
3108 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
3109 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
3110 vd->vdev_deflate_ratio;
3111
3112 mutex_enter(&vd->vdev_stat_lock);
3113 vd->vdev_stat.vs_alloc += alloc_delta;
3114 vd->vdev_stat.vs_space += space_delta;
3115 vd->vdev_stat.vs_dspace += dspace_delta;
3116 mutex_exit(&vd->vdev_stat_lock);
3117
3118 if (mc == spa_normal_class(spa)) {
3119 mutex_enter(&rvd->vdev_stat_lock);
3120 rvd->vdev_stat.vs_alloc += alloc_delta;
3121 rvd->vdev_stat.vs_space += space_delta;
3122 rvd->vdev_stat.vs_dspace += dspace_delta;
3123 mutex_exit(&rvd->vdev_stat_lock);
3124 }
3125
3126 if (mc != NULL) {
3127 ASSERT(rvd == vd->vdev_parent);
3128 ASSERT(vd->vdev_ms_count != 0);
3129
3130 metaslab_class_space_update(mc,
3131 alloc_delta, defer_delta, space_delta, dspace_delta);
3132 }
3133 }
3134
3135 /*
3136 * Mark a top-level vdev's config as dirty, placing it on the dirty list
3137 * so that it will be written out next time the vdev configuration is synced.
3138 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
3139 */
3140 void
3141 vdev_config_dirty(vdev_t *vd)
3142 {
3143 spa_t *spa = vd->vdev_spa;
3144 vdev_t *rvd = spa->spa_root_vdev;
3145 int c;
3146
3147 ASSERT(spa_writeable(spa));
3148
3149 /*
3150 * If this is an aux vdev (as with l2cache and spare devices), then we
3151 * update the vdev config manually and set the sync flag.
3152 */
3153 if (vd->vdev_aux != NULL) {
3154 spa_aux_vdev_t *sav = vd->vdev_aux;
3155 nvlist_t **aux;
3156 uint_t naux;
3157
3158 for (c = 0; c < sav->sav_count; c++) {
3159 if (sav->sav_vdevs[c] == vd)
3160 break;
3161 }
3162
3163 if (c == sav->sav_count) {
3164 /*
3165 * We're being removed. There's nothing more to do.
3166 */
3167 ASSERT(sav->sav_sync == B_TRUE);
3168 return;
3169 }
3170
3171 sav->sav_sync = B_TRUE;
3172
3173 if (nvlist_lookup_nvlist_array(sav->sav_config,
3174 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
3175 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
3176 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
3177 }
3178
3179 ASSERT(c < naux);
3180
3181 /*
3182 * Setting the nvlist in the middle if the array is a little
3183 * sketchy, but it will work.
3184 */
3185 nvlist_free(aux[c]);
3186 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
3187
3188 return;
3189 }
3190
3191 /*
3192 * The dirty list is protected by the SCL_CONFIG lock. The caller
3193 * must either hold SCL_CONFIG as writer, or must be the sync thread
3194 * (which holds SCL_CONFIG as reader). There's only one sync thread,
3195 * so this is sufficient to ensure mutual exclusion.
3196 */
3197 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3198 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3199 spa_config_held(spa, SCL_CONFIG, RW_READER)));
3200
3201 if (vd == rvd) {
3202 for (c = 0; c < rvd->vdev_children; c++)
3203 vdev_config_dirty(rvd->vdev_child[c]);
3204 } else {
3205 ASSERT(vd == vd->vdev_top);
3206
3207 if (!list_link_active(&vd->vdev_config_dirty_node) &&
3208 !vd->vdev_ishole)
3209 list_insert_head(&spa->spa_config_dirty_list, vd);
3210 }
3211 }
3212
3213 void
3214 vdev_config_clean(vdev_t *vd)
3215 {
3216 spa_t *spa = vd->vdev_spa;
3217
3218 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3219 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3220 spa_config_held(spa, SCL_CONFIG, RW_READER)));
3221
3222 ASSERT(list_link_active(&vd->vdev_config_dirty_node));
3223 list_remove(&spa->spa_config_dirty_list, vd);
3224 }
3225
3226 /*
3227 * Mark a top-level vdev's state as dirty, so that the next pass of
3228 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
3229 * the state changes from larger config changes because they require
3230 * much less locking, and are often needed for administrative actions.
3231 */
3232 void
3233 vdev_state_dirty(vdev_t *vd)
3234 {
3235 spa_t *spa = vd->vdev_spa;
3236
3237 ASSERT(spa_writeable(spa));
3238 ASSERT(vd == vd->vdev_top);
3239
3240 /*
3241 * The state list is protected by the SCL_STATE lock. The caller
3242 * must either hold SCL_STATE as writer, or must be the sync thread
3243 * (which holds SCL_STATE as reader). There's only one sync thread,
3244 * so this is sufficient to ensure mutual exclusion.
3245 */
3246 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3247 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3248 spa_config_held(spa, SCL_STATE, RW_READER)));
3249
3250 if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole)
3251 list_insert_head(&spa->spa_state_dirty_list, vd);
3252 }
3253
3254 void
3255 vdev_state_clean(vdev_t *vd)
3256 {
3257 spa_t *spa = vd->vdev_spa;
3258
3259 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3260 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3261 spa_config_held(spa, SCL_STATE, RW_READER)));
3262
3263 ASSERT(list_link_active(&vd->vdev_state_dirty_node));
3264 list_remove(&spa->spa_state_dirty_list, vd);
3265 }
3266
3267 /*
3268 * Propagate vdev state up from children to parent.
3269 */
3270 void
3271 vdev_propagate_state(vdev_t *vd)
3272 {
3273 spa_t *spa = vd->vdev_spa;
3274 vdev_t *rvd = spa->spa_root_vdev;
3275 int degraded = 0, faulted = 0;
3276 int corrupted = 0;
3277 vdev_t *child;
3278 int c;
3279
3280 if (vd->vdev_children > 0) {
3281 for (c = 0; c < vd->vdev_children; c++) {
3282 child = vd->vdev_child[c];
3283
3284 /*
3285 * Don't factor holes into the decision.
3286 */
3287 if (child->vdev_ishole)
3288 continue;
3289
3290 if (!vdev_readable(child) ||
3291 (!vdev_writeable(child) && spa_writeable(spa))) {
3292 /*
3293 * Root special: if there is a top-level log
3294 * device, treat the root vdev as if it were
3295 * degraded.
3296 */
3297 if (child->vdev_islog && vd == rvd)
3298 degraded++;
3299 else
3300 faulted++;
3301 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
3302 degraded++;
3303 }
3304
3305 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
3306 corrupted++;
3307 }
3308
3309 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
3310
3311 /*
3312 * Root special: if there is a top-level vdev that cannot be
3313 * opened due to corrupted metadata, then propagate the root
3314 * vdev's aux state as 'corrupt' rather than 'insufficient
3315 * replicas'.
3316 */
3317 if (corrupted && vd == rvd &&
3318 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
3319 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
3320 VDEV_AUX_CORRUPT_DATA);
3321 }
3322
3323 if (vd->vdev_parent)
3324 vdev_propagate_state(vd->vdev_parent);
3325 }
3326
3327 /*
3328 * Set a vdev's state. If this is during an open, we don't update the parent
3329 * state, because we're in the process of opening children depth-first.
3330 * Otherwise, we propagate the change to the parent.
3331 *
3332 * If this routine places a device in a faulted state, an appropriate ereport is
3333 * generated.
3334 */
3335 void
3336 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
3337 {
3338 uint64_t save_state;
3339 spa_t *spa = vd->vdev_spa;
3340
3341 if (state == vd->vdev_state) {
3342 vd->vdev_stat.vs_aux = aux;
3343 return;
3344 }
3345
3346 save_state = vd->vdev_state;
3347
3348 vd->vdev_state = state;
3349 vd->vdev_stat.vs_aux = aux;
3350
3351 /*
3352 * If we are setting the vdev state to anything but an open state, then
3353 * always close the underlying device unless the device has requested
3354 * a delayed close (i.e. we're about to remove or fault the device).
3355 * Otherwise, we keep accessible but invalid devices open forever.
3356 * We don't call vdev_close() itself, because that implies some extra
3357 * checks (offline, etc) that we don't want here. This is limited to
3358 * leaf devices, because otherwise closing the device will affect other
3359 * children.
3360 */
3361 if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
3362 vd->vdev_ops->vdev_op_leaf)
3363 vd->vdev_ops->vdev_op_close(vd);
3364
3365 /*
3366 * If we have brought this vdev back into service, we need
3367 * to notify fmd so that it can gracefully repair any outstanding
3368 * cases due to a missing device. We do this in all cases, even those
3369 * that probably don't correlate to a repaired fault. This is sure to
3370 * catch all cases, and we let the zfs-retire agent sort it out. If
3371 * this is a transient state it's OK, as the retire agent will
3372 * double-check the state of the vdev before repairing it.
3373 */
3374 if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
3375 vd->vdev_prevstate != state)
3376 zfs_post_state_change(spa, vd);
3377
3378 if (vd->vdev_removed &&
3379 state == VDEV_STATE_CANT_OPEN &&
3380 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
3381 /*
3382 * If the previous state is set to VDEV_STATE_REMOVED, then this
3383 * device was previously marked removed and someone attempted to
3384 * reopen it. If this failed due to a nonexistent device, then
3385 * keep the device in the REMOVED state. We also let this be if
3386 * it is one of our special test online cases, which is only
3387 * attempting to online the device and shouldn't generate an FMA
3388 * fault.
3389 */
3390 vd->vdev_state = VDEV_STATE_REMOVED;
3391 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
3392 } else if (state == VDEV_STATE_REMOVED) {
3393 vd->vdev_removed = B_TRUE;
3394 } else if (state == VDEV_STATE_CANT_OPEN) {
3395 /*
3396 * If we fail to open a vdev during an import or recovery, we
3397 * mark it as "not available", which signifies that it was
3398 * never there to begin with. Failure to open such a device
3399 * is not considered an error.
3400 */
3401 if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
3402 spa_load_state(spa) == SPA_LOAD_RECOVER) &&
3403 vd->vdev_ops->vdev_op_leaf)
3404 vd->vdev_not_present = 1;
3405
3406 /*
3407 * Post the appropriate ereport. If the 'prevstate' field is
3408 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3409 * that this is part of a vdev_reopen(). In this case, we don't
3410 * want to post the ereport if the device was already in the
3411 * CANT_OPEN state beforehand.
3412 *
3413 * If the 'checkremove' flag is set, then this is an attempt to
3414 * online the device in response to an insertion event. If we
3415 * hit this case, then we have detected an insertion event for a
3416 * faulted or offline device that wasn't in the removed state.
3417 * In this scenario, we don't post an ereport because we are
3418 * about to replace the device, or attempt an online with
3419 * vdev_forcefault, which will generate the fault for us.
3420 */
3421 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
3422 !vd->vdev_not_present && !vd->vdev_checkremove &&
3423 vd != spa->spa_root_vdev) {
3424 const char *class;
3425
3426 switch (aux) {
3427 case VDEV_AUX_OPEN_FAILED:
3428 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
3429 break;
3430 case VDEV_AUX_CORRUPT_DATA:
3431 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
3432 break;
3433 case VDEV_AUX_NO_REPLICAS:
3434 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
3435 break;
3436 case VDEV_AUX_BAD_GUID_SUM:
3437 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
3438 break;
3439 case VDEV_AUX_TOO_SMALL:
3440 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
3441 break;
3442 case VDEV_AUX_BAD_LABEL:
3443 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
3444 break;
3445 default:
3446 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
3447 }
3448
3449 zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
3450 }
3451
3452 /* Erase any notion of persistent removed state */
3453 vd->vdev_removed = B_FALSE;
3454 } else {
3455 vd->vdev_removed = B_FALSE;
3456 }
3457
3458 if (!isopen && vd->vdev_parent)
3459 vdev_propagate_state(vd->vdev_parent);
3460 }
3461
3462 /*
3463 * Check the vdev configuration to ensure that it's capable of supporting
3464 * a root pool.
3465 */
3466 boolean_t
3467 vdev_is_bootable(vdev_t *vd)
3468 {
3469 #if defined(__sun__) || defined(__sun)
3470 /*
3471 * Currently, we do not support RAID-Z or partial configuration.
3472 * In addition, only a single top-level vdev is allowed and none of the
3473 * leaves can be wholedisks.
3474 */
3475 int c;
3476
3477 if (!vd->vdev_ops->vdev_op_leaf) {
3478 char *vdev_type = vd->vdev_ops->vdev_op_type;
3479
3480 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
3481 vd->vdev_children > 1) {
3482 return (B_FALSE);
3483 } else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
3484 strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
3485 return (B_FALSE);
3486 }
3487 } else if (vd->vdev_wholedisk == 1) {
3488 return (B_FALSE);
3489 }
3490
3491 for (c = 0; c < vd->vdev_children; c++) {
3492 if (!vdev_is_bootable(vd->vdev_child[c]))
3493 return (B_FALSE);
3494 }
3495 #endif /* __sun__ || __sun */
3496 return (B_TRUE);
3497 }
3498
3499 /*
3500 * Load the state from the original vdev tree (ovd) which
3501 * we've retrieved from the MOS config object. If the original
3502 * vdev was offline or faulted then we transfer that state to the
3503 * device in the current vdev tree (nvd).
3504 */
3505 void
3506 vdev_load_log_state(vdev_t *nvd, vdev_t *ovd)
3507 {
3508 int c;
3509
3510 ASSERT(nvd->vdev_top->vdev_islog);
3511 ASSERT(spa_config_held(nvd->vdev_spa,
3512 SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3513 ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid);
3514
3515 for (c = 0; c < nvd->vdev_children; c++)
3516 vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]);
3517
3518 if (nvd->vdev_ops->vdev_op_leaf) {
3519 /*
3520 * Restore the persistent vdev state
3521 */
3522 nvd->vdev_offline = ovd->vdev_offline;
3523 nvd->vdev_faulted = ovd->vdev_faulted;
3524 nvd->vdev_degraded = ovd->vdev_degraded;
3525 nvd->vdev_removed = ovd->vdev_removed;
3526 }
3527 }
3528
3529 /*
3530 * Determine if a log device has valid content. If the vdev was
3531 * removed or faulted in the MOS config then we know that
3532 * the content on the log device has already been written to the pool.
3533 */
3534 boolean_t
3535 vdev_log_state_valid(vdev_t *vd)
3536 {
3537 int c;
3538
3539 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
3540 !vd->vdev_removed)
3541 return (B_TRUE);
3542
3543 for (c = 0; c < vd->vdev_children; c++)
3544 if (vdev_log_state_valid(vd->vdev_child[c]))
3545 return (B_TRUE);
3546
3547 return (B_FALSE);
3548 }
3549
3550 /*
3551 * Expand a vdev if possible.
3552 */
3553 void
3554 vdev_expand(vdev_t *vd, uint64_t txg)
3555 {
3556 ASSERT(vd->vdev_top == vd);
3557 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3558
3559 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
3560 VERIFY(vdev_metaslab_init(vd, txg) == 0);
3561 vdev_config_dirty(vd);
3562 }
3563 }
3564
3565 /*
3566 * Split a vdev.
3567 */
3568 void
3569 vdev_split(vdev_t *vd)
3570 {
3571 vdev_t *cvd, *pvd = vd->vdev_parent;
3572
3573 vdev_remove_child(pvd, vd);
3574 vdev_compact_children(pvd);
3575
3576 cvd = pvd->vdev_child[0];
3577 if (pvd->vdev_children == 1) {
3578 vdev_remove_parent(cvd);
3579 cvd->vdev_splitting = B_TRUE;
3580 }
3581 vdev_propagate_state(cvd);
3582 }
3583
3584 void
3585 vdev_deadman(vdev_t *vd)
3586 {
3587 int c;
3588
3589 for (c = 0; c < vd->vdev_children; c++) {
3590 vdev_t *cvd = vd->vdev_child[c];
3591
3592 vdev_deadman(cvd);
3593 }
3594
3595 if (vd->vdev_ops->vdev_op_leaf) {
3596 vdev_queue_t *vq = &vd->vdev_queue;
3597
3598 mutex_enter(&vq->vq_lock);
3599 if (avl_numnodes(&vq->vq_active_tree) > 0) {
3600 spa_t *spa = vd->vdev_spa;
3601 zio_t *fio;
3602 uint64_t delta;
3603
3604 /*
3605 * Look at the head of all the pending queues,
3606 * if any I/O has been outstanding for longer than
3607 * the spa_deadman_synctime we log a zevent.
3608 */
3609 fio = avl_first(&vq->vq_active_tree);
3610 delta = gethrtime() - fio->io_timestamp;
3611 if (delta > spa_deadman_synctime(spa)) {
3612 zfs_dbgmsg("SLOW IO: zio timestamp %lluns, "
3613 "delta %lluns, last io %lluns",
3614 fio->io_timestamp, delta,
3615 vq->vq_io_complete_ts);
3616 zfs_ereport_post(FM_EREPORT_ZFS_DELAY,
3617 spa, vd, fio, 0, 0);
3618 }
3619 }
3620 mutex_exit(&vq->vq_lock);
3621 }
3622 }
3623
3624 #if defined(_KERNEL) && defined(HAVE_SPL)
3625 EXPORT_SYMBOL(vdev_fault);
3626 EXPORT_SYMBOL(vdev_degrade);
3627 EXPORT_SYMBOL(vdev_online);
3628 EXPORT_SYMBOL(vdev_offline);
3629 EXPORT_SYMBOL(vdev_clear);
3630
3631 module_param(metaslabs_per_vdev, int, 0644);
3632 MODULE_PARM_DESC(metaslabs_per_vdev,
3633 "Divide added vdev into approximately (but no more than) this number "
3634 "of metaslabs");
3635 #endif