]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | ||
22 | /* | |
428870ff | 23 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
74d42600 | 24 | * Copyright (c) 2011, 2018 by Delphix. All rights reserved. |
153b2285 | 25 | * Copyright 2017 Nexenta Systems, Inc. |
e550644f BB |
26 | * Copyright (c) 2014 Integros [integros.com] |
27 | * Copyright 2016 Toomas Soome <tsoome@me.com> | |
12fa0466 | 28 | * Copyright 2017 Joyent, Inc. |
cc99f275 | 29 | * Copyright (c) 2017, Intel Corporation. |
34dc7c2f BB |
30 | */ |
31 | ||
34dc7c2f BB |
32 | #include <sys/zfs_context.h> |
33 | #include <sys/fm/fs/zfs.h> | |
34 | #include <sys/spa.h> | |
35 | #include <sys/spa_impl.h> | |
a1d477c2 | 36 | #include <sys/bpobj.h> |
34dc7c2f BB |
37 | #include <sys/dmu.h> |
38 | #include <sys/dmu_tx.h> | |
a1d477c2 | 39 | #include <sys/dsl_dir.h> |
34dc7c2f BB |
40 | #include <sys/vdev_impl.h> |
41 | #include <sys/uberblock_impl.h> | |
42 | #include <sys/metaslab.h> | |
43 | #include <sys/metaslab_impl.h> | |
44 | #include <sys/space_map.h> | |
93cf2076 | 45 | #include <sys/space_reftree.h> |
34dc7c2f BB |
46 | #include <sys/zio.h> |
47 | #include <sys/zap.h> | |
48 | #include <sys/fs/zfs.h> | |
b128c09f | 49 | #include <sys/arc.h> |
9babb374 | 50 | #include <sys/zil.h> |
428870ff | 51 | #include <sys/dsl_scan.h> |
a6255b7f | 52 | #include <sys/abd.h> |
619f0976 | 53 | #include <sys/vdev_initialize.h> |
6c285672 | 54 | #include <sys/zvol.h> |
6078881a | 55 | #include <sys/zfs_ratelimit.h> |
34dc7c2f | 56 | |
c853f382 SD |
57 | /* default target for number of metaslabs per top-level vdev */ |
58 | int zfs_vdev_default_ms_count = 200; | |
d2734cce | 59 | |
e4e94ca3 | 60 | /* minimum number of metaslabs per top-level vdev */ |
c853f382 | 61 | int zfs_vdev_min_ms_count = 16; |
d2734cce | 62 | |
e4e94ca3 | 63 | /* practical upper limit of total metaslabs per top-level vdev */ |
c853f382 | 64 | int zfs_vdev_ms_count_limit = 1ULL << 17; |
e4e94ca3 DB |
65 | |
66 | /* lower limit for metaslab size (512M) */ | |
c853f382 | 67 | int zfs_vdev_default_ms_shift = 29; |
d2734cce | 68 | |
c853f382 SD |
69 | /* upper limit for metaslab size (16G) */ |
70 | int zfs_vdev_max_ms_shift = 34; | |
e4e94ca3 | 71 | |
d2734cce SD |
72 | int vdev_validate_skip = B_FALSE; |
73 | ||
b8bcca18 | 74 | /* |
d2734cce SD |
75 | * Since the DTL space map of a vdev is not expected to have a lot of |
76 | * entries, we default its block size to 4K. | |
b8bcca18 | 77 | */ |
d2734cce | 78 | int vdev_dtl_sm_blksz = (1 << 12); |
b8bcca18 | 79 | |
80d52c39 | 80 | /* |
ad796b8a | 81 | * Rate limit slow IO (delay) events to this many per second. |
80d52c39 | 82 | */ |
ad796b8a | 83 | unsigned int zfs_slow_io_events_per_second = 20; |
80d52c39 TH |
84 | |
85 | /* | |
86 | * Rate limit checksum events after this many checksum errors per second. | |
87 | */ | |
ad796b8a | 88 | unsigned int zfs_checksum_events_per_second = 20; |
80d52c39 | 89 | |
02638a30 TC |
90 | /* |
91 | * Ignore errors during scrub/resilver. Allows to work around resilver | |
92 | * upon import when there are pool errors. | |
93 | */ | |
94 | int zfs_scan_ignore_errors = 0; | |
95 | ||
d2734cce SD |
96 | /* |
97 | * vdev-wide space maps that have lots of entries written to them at | |
98 | * the end of each transaction can benefit from a higher I/O bandwidth | |
99 | * (e.g. vdev_obsolete_sm), thus we default their block size to 128K. | |
100 | */ | |
101 | int vdev_standard_sm_blksz = (1 << 17); | |
6cb8e530 | 102 | |
53b1f5ea PS |
103 | /* |
104 | * Tunable parameter for debugging or performance analysis. Setting this | |
105 | * will cause pool corruption on power loss if a volatile out-of-order | |
106 | * write cache is enabled. | |
107 | */ | |
108 | int zfs_nocacheflush = 0; | |
109 | ||
4a0ee12a PZ |
110 | /*PRINTFLIKE2*/ |
111 | void | |
112 | vdev_dbgmsg(vdev_t *vd, const char *fmt, ...) | |
113 | { | |
114 | va_list adx; | |
115 | char buf[256]; | |
116 | ||
117 | va_start(adx, fmt); | |
118 | (void) vsnprintf(buf, sizeof (buf), fmt, adx); | |
119 | va_end(adx); | |
120 | ||
121 | if (vd->vdev_path != NULL) { | |
122 | zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type, | |
123 | vd->vdev_path, buf); | |
124 | } else { | |
125 | zfs_dbgmsg("%s-%llu vdev (guid %llu): %s", | |
126 | vd->vdev_ops->vdev_op_type, | |
127 | (u_longlong_t)vd->vdev_id, | |
128 | (u_longlong_t)vd->vdev_guid, buf); | |
129 | } | |
130 | } | |
131 | ||
6cb8e530 PZ |
132 | void |
133 | vdev_dbgmsg_print_tree(vdev_t *vd, int indent) | |
134 | { | |
135 | char state[20]; | |
136 | ||
137 | if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) { | |
138 | zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id, | |
139 | vd->vdev_ops->vdev_op_type); | |
140 | return; | |
141 | } | |
142 | ||
143 | switch (vd->vdev_state) { | |
144 | case VDEV_STATE_UNKNOWN: | |
145 | (void) snprintf(state, sizeof (state), "unknown"); | |
146 | break; | |
147 | case VDEV_STATE_CLOSED: | |
148 | (void) snprintf(state, sizeof (state), "closed"); | |
149 | break; | |
150 | case VDEV_STATE_OFFLINE: | |
151 | (void) snprintf(state, sizeof (state), "offline"); | |
152 | break; | |
153 | case VDEV_STATE_REMOVED: | |
154 | (void) snprintf(state, sizeof (state), "removed"); | |
155 | break; | |
156 | case VDEV_STATE_CANT_OPEN: | |
157 | (void) snprintf(state, sizeof (state), "can't open"); | |
158 | break; | |
159 | case VDEV_STATE_FAULTED: | |
160 | (void) snprintf(state, sizeof (state), "faulted"); | |
161 | break; | |
162 | case VDEV_STATE_DEGRADED: | |
163 | (void) snprintf(state, sizeof (state), "degraded"); | |
164 | break; | |
165 | case VDEV_STATE_HEALTHY: | |
166 | (void) snprintf(state, sizeof (state), "healthy"); | |
167 | break; | |
168 | default: | |
169 | (void) snprintf(state, sizeof (state), "<state %u>", | |
170 | (uint_t)vd->vdev_state); | |
171 | } | |
172 | ||
173 | zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent, | |
e902ddb0 | 174 | "", (int)vd->vdev_id, vd->vdev_ops->vdev_op_type, |
6cb8e530 PZ |
175 | vd->vdev_islog ? " (log)" : "", |
176 | (u_longlong_t)vd->vdev_guid, | |
177 | vd->vdev_path ? vd->vdev_path : "N/A", state); | |
178 | ||
179 | for (uint64_t i = 0; i < vd->vdev_children; i++) | |
180 | vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2); | |
181 | } | |
182 | ||
34dc7c2f BB |
183 | /* |
184 | * Virtual device management. | |
185 | */ | |
186 | ||
187 | static vdev_ops_t *vdev_ops_table[] = { | |
188 | &vdev_root_ops, | |
189 | &vdev_raidz_ops, | |
190 | &vdev_mirror_ops, | |
191 | &vdev_replacing_ops, | |
192 | &vdev_spare_ops, | |
193 | &vdev_disk_ops, | |
194 | &vdev_file_ops, | |
195 | &vdev_missing_ops, | |
428870ff | 196 | &vdev_hole_ops, |
a1d477c2 | 197 | &vdev_indirect_ops, |
34dc7c2f BB |
198 | NULL |
199 | }; | |
200 | ||
34dc7c2f BB |
201 | /* |
202 | * Given a vdev type, return the appropriate ops vector. | |
203 | */ | |
204 | static vdev_ops_t * | |
205 | vdev_getops(const char *type) | |
206 | { | |
207 | vdev_ops_t *ops, **opspp; | |
208 | ||
209 | for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) | |
210 | if (strcmp(ops->vdev_op_type, type) == 0) | |
211 | break; | |
212 | ||
213 | return (ops); | |
214 | } | |
215 | ||
619f0976 GW |
216 | /* ARGSUSED */ |
217 | void | |
218 | vdev_default_xlate(vdev_t *vd, const range_seg_t *in, range_seg_t *res) | |
219 | { | |
220 | res->rs_start = in->rs_start; | |
221 | res->rs_end = in->rs_end; | |
222 | } | |
223 | ||
cc99f275 DB |
224 | /* |
225 | * Derive the enumerated alloction bias from string input. | |
226 | * String origin is either the per-vdev zap or zpool(1M). | |
227 | */ | |
228 | static vdev_alloc_bias_t | |
229 | vdev_derive_alloc_bias(const char *bias) | |
230 | { | |
231 | vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE; | |
232 | ||
233 | if (strcmp(bias, VDEV_ALLOC_BIAS_LOG) == 0) | |
234 | alloc_bias = VDEV_BIAS_LOG; | |
235 | else if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) | |
236 | alloc_bias = VDEV_BIAS_SPECIAL; | |
237 | else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0) | |
238 | alloc_bias = VDEV_BIAS_DEDUP; | |
239 | ||
240 | return (alloc_bias); | |
241 | } | |
242 | ||
34dc7c2f BB |
243 | /* |
244 | * Default asize function: return the MAX of psize with the asize of | |
245 | * all children. This is what's used by anything other than RAID-Z. | |
246 | */ | |
247 | uint64_t | |
248 | vdev_default_asize(vdev_t *vd, uint64_t psize) | |
249 | { | |
250 | uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); | |
251 | uint64_t csize; | |
34dc7c2f | 252 | |
1c27024e | 253 | for (int c = 0; c < vd->vdev_children; c++) { |
34dc7c2f BB |
254 | csize = vdev_psize_to_asize(vd->vdev_child[c], psize); |
255 | asize = MAX(asize, csize); | |
256 | } | |
257 | ||
258 | return (asize); | |
259 | } | |
260 | ||
261 | /* | |
9babb374 BB |
262 | * Get the minimum allocatable size. We define the allocatable size as |
263 | * the vdev's asize rounded to the nearest metaslab. This allows us to | |
264 | * replace or attach devices which don't have the same physical size but | |
265 | * can still satisfy the same number of allocations. | |
34dc7c2f BB |
266 | */ |
267 | uint64_t | |
9babb374 | 268 | vdev_get_min_asize(vdev_t *vd) |
34dc7c2f | 269 | { |
9babb374 | 270 | vdev_t *pvd = vd->vdev_parent; |
34dc7c2f | 271 | |
9babb374 | 272 | /* |
1bd201e7 | 273 | * If our parent is NULL (inactive spare or cache) or is the root, |
9babb374 BB |
274 | * just return our own asize. |
275 | */ | |
276 | if (pvd == NULL) | |
277 | return (vd->vdev_asize); | |
34dc7c2f BB |
278 | |
279 | /* | |
9babb374 BB |
280 | * The top-level vdev just returns the allocatable size rounded |
281 | * to the nearest metaslab. | |
34dc7c2f | 282 | */ |
9babb374 BB |
283 | if (vd == vd->vdev_top) |
284 | return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift)); | |
34dc7c2f | 285 | |
9babb374 BB |
286 | /* |
287 | * The allocatable space for a raidz vdev is N * sizeof(smallest child), | |
288 | * so each child must provide at least 1/Nth of its asize. | |
289 | */ | |
290 | if (pvd->vdev_ops == &vdev_raidz_ops) | |
2e215fec SH |
291 | return ((pvd->vdev_min_asize + pvd->vdev_children - 1) / |
292 | pvd->vdev_children); | |
34dc7c2f | 293 | |
9babb374 BB |
294 | return (pvd->vdev_min_asize); |
295 | } | |
296 | ||
297 | void | |
298 | vdev_set_min_asize(vdev_t *vd) | |
299 | { | |
300 | vd->vdev_min_asize = vdev_get_min_asize(vd); | |
34dc7c2f | 301 | |
1c27024e | 302 | for (int c = 0; c < vd->vdev_children; c++) |
9babb374 | 303 | vdev_set_min_asize(vd->vdev_child[c]); |
34dc7c2f BB |
304 | } |
305 | ||
306 | vdev_t * | |
307 | vdev_lookup_top(spa_t *spa, uint64_t vdev) | |
308 | { | |
309 | vdev_t *rvd = spa->spa_root_vdev; | |
310 | ||
b128c09f | 311 | ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); |
34dc7c2f | 312 | |
b128c09f BB |
313 | if (vdev < rvd->vdev_children) { |
314 | ASSERT(rvd->vdev_child[vdev] != NULL); | |
34dc7c2f | 315 | return (rvd->vdev_child[vdev]); |
b128c09f | 316 | } |
34dc7c2f BB |
317 | |
318 | return (NULL); | |
319 | } | |
320 | ||
321 | vdev_t * | |
322 | vdev_lookup_by_guid(vdev_t *vd, uint64_t guid) | |
323 | { | |
34dc7c2f BB |
324 | vdev_t *mvd; |
325 | ||
326 | if (vd->vdev_guid == guid) | |
327 | return (vd); | |
328 | ||
1c27024e | 329 | for (int c = 0; c < vd->vdev_children; c++) |
34dc7c2f BB |
330 | if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != |
331 | NULL) | |
332 | return (mvd); | |
333 | ||
334 | return (NULL); | |
335 | } | |
336 | ||
9c43027b AJ |
337 | static int |
338 | vdev_count_leaves_impl(vdev_t *vd) | |
339 | { | |
340 | int n = 0; | |
9c43027b AJ |
341 | |
342 | if (vd->vdev_ops->vdev_op_leaf) | |
343 | return (1); | |
344 | ||
1c27024e | 345 | for (int c = 0; c < vd->vdev_children; c++) |
9c43027b AJ |
346 | n += vdev_count_leaves_impl(vd->vdev_child[c]); |
347 | ||
348 | return (n); | |
349 | } | |
350 | ||
351 | int | |
352 | vdev_count_leaves(spa_t *spa) | |
353 | { | |
743253df OF |
354 | int rc; |
355 | ||
356 | spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); | |
357 | rc = vdev_count_leaves_impl(spa->spa_root_vdev); | |
358 | spa_config_exit(spa, SCL_VDEV, FTAG); | |
359 | ||
360 | return (rc); | |
9c43027b AJ |
361 | } |
362 | ||
34dc7c2f BB |
363 | void |
364 | vdev_add_child(vdev_t *pvd, vdev_t *cvd) | |
365 | { | |
366 | size_t oldsize, newsize; | |
367 | uint64_t id = cvd->vdev_id; | |
368 | vdev_t **newchild; | |
369 | ||
44de2f02 | 370 | ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
34dc7c2f BB |
371 | ASSERT(cvd->vdev_parent == NULL); |
372 | ||
373 | cvd->vdev_parent = pvd; | |
374 | ||
375 | if (pvd == NULL) | |
376 | return; | |
377 | ||
378 | ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); | |
379 | ||
380 | oldsize = pvd->vdev_children * sizeof (vdev_t *); | |
381 | pvd->vdev_children = MAX(pvd->vdev_children, id + 1); | |
382 | newsize = pvd->vdev_children * sizeof (vdev_t *); | |
383 | ||
79c76d5b | 384 | newchild = kmem_alloc(newsize, KM_SLEEP); |
34dc7c2f BB |
385 | if (pvd->vdev_child != NULL) { |
386 | bcopy(pvd->vdev_child, newchild, oldsize); | |
387 | kmem_free(pvd->vdev_child, oldsize); | |
388 | } | |
389 | ||
390 | pvd->vdev_child = newchild; | |
391 | pvd->vdev_child[id] = cvd; | |
392 | ||
393 | cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); | |
394 | ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); | |
395 | ||
396 | /* | |
397 | * Walk up all ancestors to update guid sum. | |
398 | */ | |
399 | for (; pvd != NULL; pvd = pvd->vdev_parent) | |
400 | pvd->vdev_guid_sum += cvd->vdev_guid_sum; | |
34dc7c2f BB |
401 | } |
402 | ||
403 | void | |
404 | vdev_remove_child(vdev_t *pvd, vdev_t *cvd) | |
405 | { | |
406 | int c; | |
407 | uint_t id = cvd->vdev_id; | |
408 | ||
409 | ASSERT(cvd->vdev_parent == pvd); | |
410 | ||
411 | if (pvd == NULL) | |
412 | return; | |
413 | ||
414 | ASSERT(id < pvd->vdev_children); | |
415 | ASSERT(pvd->vdev_child[id] == cvd); | |
416 | ||
417 | pvd->vdev_child[id] = NULL; | |
418 | cvd->vdev_parent = NULL; | |
419 | ||
420 | for (c = 0; c < pvd->vdev_children; c++) | |
421 | if (pvd->vdev_child[c]) | |
422 | break; | |
423 | ||
424 | if (c == pvd->vdev_children) { | |
425 | kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); | |
426 | pvd->vdev_child = NULL; | |
427 | pvd->vdev_children = 0; | |
428 | } | |
429 | ||
430 | /* | |
431 | * Walk up all ancestors to update guid sum. | |
432 | */ | |
433 | for (; pvd != NULL; pvd = pvd->vdev_parent) | |
434 | pvd->vdev_guid_sum -= cvd->vdev_guid_sum; | |
34dc7c2f BB |
435 | } |
436 | ||
437 | /* | |
438 | * Remove any holes in the child array. | |
439 | */ | |
440 | void | |
441 | vdev_compact_children(vdev_t *pvd) | |
442 | { | |
443 | vdev_t **newchild, *cvd; | |
444 | int oldc = pvd->vdev_children; | |
9babb374 | 445 | int newc; |
34dc7c2f | 446 | |
b128c09f | 447 | ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
34dc7c2f | 448 | |
a1d477c2 MA |
449 | if (oldc == 0) |
450 | return; | |
451 | ||
1c27024e | 452 | for (int c = newc = 0; c < oldc; c++) |
34dc7c2f BB |
453 | if (pvd->vdev_child[c]) |
454 | newc++; | |
455 | ||
a1d477c2 MA |
456 | if (newc > 0) { |
457 | newchild = kmem_zalloc(newc * sizeof (vdev_t *), KM_SLEEP); | |
34dc7c2f | 458 | |
a1d477c2 MA |
459 | for (int c = newc = 0; c < oldc; c++) { |
460 | if ((cvd = pvd->vdev_child[c]) != NULL) { | |
461 | newchild[newc] = cvd; | |
462 | cvd->vdev_id = newc++; | |
463 | } | |
34dc7c2f | 464 | } |
a1d477c2 MA |
465 | } else { |
466 | newchild = NULL; | |
34dc7c2f BB |
467 | } |
468 | ||
469 | kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); | |
470 | pvd->vdev_child = newchild; | |
471 | pvd->vdev_children = newc; | |
472 | } | |
473 | ||
474 | /* | |
475 | * Allocate and minimally initialize a vdev_t. | |
476 | */ | |
428870ff | 477 | vdev_t * |
34dc7c2f BB |
478 | vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) |
479 | { | |
480 | vdev_t *vd; | |
a1d477c2 | 481 | vdev_indirect_config_t *vic; |
34dc7c2f | 482 | |
79c76d5b | 483 | vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); |
a1d477c2 | 484 | vic = &vd->vdev_indirect_config; |
34dc7c2f BB |
485 | |
486 | if (spa->spa_root_vdev == NULL) { | |
487 | ASSERT(ops == &vdev_root_ops); | |
488 | spa->spa_root_vdev = vd; | |
3541dc6d | 489 | spa->spa_load_guid = spa_generate_guid(NULL); |
34dc7c2f BB |
490 | } |
491 | ||
428870ff | 492 | if (guid == 0 && ops != &vdev_hole_ops) { |
34dc7c2f BB |
493 | if (spa->spa_root_vdev == vd) { |
494 | /* | |
495 | * The root vdev's guid will also be the pool guid, | |
496 | * which must be unique among all pools. | |
497 | */ | |
428870ff | 498 | guid = spa_generate_guid(NULL); |
34dc7c2f BB |
499 | } else { |
500 | /* | |
501 | * Any other vdev's guid must be unique within the pool. | |
502 | */ | |
428870ff | 503 | guid = spa_generate_guid(spa); |
34dc7c2f BB |
504 | } |
505 | ASSERT(!spa_guid_exists(spa_guid(spa), guid)); | |
506 | } | |
507 | ||
508 | vd->vdev_spa = spa; | |
509 | vd->vdev_id = id; | |
510 | vd->vdev_guid = guid; | |
511 | vd->vdev_guid_sum = guid; | |
512 | vd->vdev_ops = ops; | |
513 | vd->vdev_state = VDEV_STATE_CLOSED; | |
428870ff | 514 | vd->vdev_ishole = (ops == &vdev_hole_ops); |
a1d477c2 MA |
515 | vic->vic_prev_indirect_vdev = UINT64_MAX; |
516 | ||
517 | rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL); | |
518 | mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL); | |
519 | vd->vdev_obsolete_segments = range_tree_create(NULL, NULL); | |
34dc7c2f | 520 | |
6078881a TH |
521 | /* |
522 | * Initialize rate limit structs for events. We rate limit ZIO delay | |
523 | * and checksum events so that we don't overwhelm ZED with thousands | |
524 | * of events when a disk is acting up. | |
525 | */ | |
ad796b8a TH |
526 | zfs_ratelimit_init(&vd->vdev_delay_rl, &zfs_slow_io_events_per_second, |
527 | 1); | |
528 | zfs_ratelimit_init(&vd->vdev_checksum_rl, | |
529 | &zfs_checksum_events_per_second, 1); | |
6078881a | 530 | |
98f72a53 BB |
531 | list_link_init(&vd->vdev_config_dirty_node); |
532 | list_link_init(&vd->vdev_state_dirty_node); | |
c10d37dd | 533 | list_link_init(&vd->vdev_initialize_node); |
448d7aaa | 534 | mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_NOLOCKDEP, NULL); |
34dc7c2f | 535 | mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); |
b128c09f | 536 | mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL); |
d4a72f23 | 537 | mutex_init(&vd->vdev_scan_io_queue_lock, NULL, MUTEX_DEFAULT, NULL); |
619f0976 GW |
538 | mutex_init(&vd->vdev_initialize_lock, NULL, MUTEX_DEFAULT, NULL); |
539 | mutex_init(&vd->vdev_initialize_io_lock, NULL, MUTEX_DEFAULT, NULL); | |
540 | cv_init(&vd->vdev_initialize_cv, NULL, CV_DEFAULT, NULL); | |
541 | cv_init(&vd->vdev_initialize_io_cv, NULL, CV_DEFAULT, NULL); | |
6078881a | 542 | |
1c27024e | 543 | for (int t = 0; t < DTL_TYPES; t++) { |
a1d477c2 | 544 | vd->vdev_dtl[t] = range_tree_create(NULL, NULL); |
fb5f0bc8 | 545 | } |
4747a7d3 | 546 | txg_list_create(&vd->vdev_ms_list, spa, |
34dc7c2f | 547 | offsetof(struct metaslab, ms_txg_node)); |
4747a7d3 | 548 | txg_list_create(&vd->vdev_dtl_list, spa, |
34dc7c2f BB |
549 | offsetof(struct vdev, vdev_dtl_node)); |
550 | vd->vdev_stat.vs_timestamp = gethrtime(); | |
551 | vdev_queue_init(vd); | |
552 | vdev_cache_init(vd); | |
553 | ||
554 | return (vd); | |
555 | } | |
556 | ||
557 | /* | |
558 | * Allocate a new vdev. The 'alloctype' is used to control whether we are | |
559 | * creating a new vdev or loading an existing one - the behavior is slightly | |
560 | * different for each case. | |
561 | */ | |
562 | int | |
563 | vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, | |
564 | int alloctype) | |
565 | { | |
566 | vdev_ops_t *ops; | |
567 | char *type; | |
568 | uint64_t guid = 0, islog, nparity; | |
569 | vdev_t *vd; | |
a1d477c2 | 570 | vdev_indirect_config_t *vic; |
4a283c7f TH |
571 | char *tmp = NULL; |
572 | int rc; | |
cc99f275 DB |
573 | vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE; |
574 | boolean_t top_level = (parent && !parent->vdev_parent); | |
34dc7c2f | 575 | |
b128c09f | 576 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
34dc7c2f BB |
577 | |
578 | if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) | |
2e528b49 | 579 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
580 | |
581 | if ((ops = vdev_getops(type)) == NULL) | |
2e528b49 | 582 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
583 | |
584 | /* | |
585 | * If this is a load, get the vdev guid from the nvlist. | |
586 | * Otherwise, vdev_alloc_common() will generate one for us. | |
587 | */ | |
588 | if (alloctype == VDEV_ALLOC_LOAD) { | |
589 | uint64_t label_id; | |
590 | ||
591 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || | |
592 | label_id != id) | |
2e528b49 | 593 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
594 | |
595 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) | |
2e528b49 | 596 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
597 | } else if (alloctype == VDEV_ALLOC_SPARE) { |
598 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) | |
2e528b49 | 599 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
600 | } else if (alloctype == VDEV_ALLOC_L2CACHE) { |
601 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) | |
2e528b49 | 602 | return (SET_ERROR(EINVAL)); |
9babb374 BB |
603 | } else if (alloctype == VDEV_ALLOC_ROOTPOOL) { |
604 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) | |
2e528b49 | 605 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
606 | } |
607 | ||
608 | /* | |
609 | * The first allocated vdev must be of type 'root'. | |
610 | */ | |
611 | if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) | |
2e528b49 | 612 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
613 | |
614 | /* | |
615 | * Determine whether we're a log vdev. | |
616 | */ | |
617 | islog = 0; | |
618 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); | |
619 | if (islog && spa_version(spa) < SPA_VERSION_SLOGS) | |
2e528b49 | 620 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f | 621 | |
428870ff | 622 | if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES) |
2e528b49 | 623 | return (SET_ERROR(ENOTSUP)); |
428870ff | 624 | |
34dc7c2f BB |
625 | /* |
626 | * Set the nparity property for RAID-Z vdevs. | |
627 | */ | |
628 | nparity = -1ULL; | |
629 | if (ops == &vdev_raidz_ops) { | |
630 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, | |
631 | &nparity) == 0) { | |
428870ff | 632 | if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY) |
2e528b49 | 633 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 634 | /* |
45d1cae3 BB |
635 | * Previous versions could only support 1 or 2 parity |
636 | * device. | |
34dc7c2f | 637 | */ |
45d1cae3 BB |
638 | if (nparity > 1 && |
639 | spa_version(spa) < SPA_VERSION_RAIDZ2) | |
2e528b49 | 640 | return (SET_ERROR(ENOTSUP)); |
45d1cae3 BB |
641 | if (nparity > 2 && |
642 | spa_version(spa) < SPA_VERSION_RAIDZ3) | |
2e528b49 | 643 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f BB |
644 | } else { |
645 | /* | |
646 | * We require the parity to be specified for SPAs that | |
647 | * support multiple parity levels. | |
648 | */ | |
45d1cae3 | 649 | if (spa_version(spa) >= SPA_VERSION_RAIDZ2) |
2e528b49 | 650 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
651 | /* |
652 | * Otherwise, we default to 1 parity device for RAID-Z. | |
653 | */ | |
654 | nparity = 1; | |
655 | } | |
656 | } else { | |
657 | nparity = 0; | |
658 | } | |
659 | ASSERT(nparity != -1ULL); | |
660 | ||
cc99f275 DB |
661 | /* |
662 | * If creating a top-level vdev, check for allocation classes input | |
663 | */ | |
664 | if (top_level && alloctype == VDEV_ALLOC_ADD) { | |
665 | char *bias; | |
666 | ||
667 | if (nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS, | |
668 | &bias) == 0) { | |
669 | alloc_bias = vdev_derive_alloc_bias(bias); | |
670 | ||
671 | /* spa_vdev_add() expects feature to be enabled */ | |
672 | if (spa->spa_load_state != SPA_LOAD_CREATE && | |
673 | !spa_feature_is_enabled(spa, | |
674 | SPA_FEATURE_ALLOCATION_CLASSES)) { | |
675 | return (SET_ERROR(ENOTSUP)); | |
676 | } | |
677 | } | |
678 | } | |
679 | ||
34dc7c2f | 680 | vd = vdev_alloc_common(spa, id, guid, ops); |
a1d477c2 | 681 | vic = &vd->vdev_indirect_config; |
34dc7c2f BB |
682 | |
683 | vd->vdev_islog = islog; | |
684 | vd->vdev_nparity = nparity; | |
cc99f275 DB |
685 | if (top_level && alloc_bias != VDEV_BIAS_NONE) |
686 | vd->vdev_alloc_bias = alloc_bias; | |
34dc7c2f BB |
687 | |
688 | if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) | |
689 | vd->vdev_path = spa_strdup(vd->vdev_path); | |
4a283c7f TH |
690 | |
691 | /* | |
692 | * ZPOOL_CONFIG_AUX_STATE = "external" means we previously forced a | |
693 | * fault on a vdev and want it to persist across imports (like with | |
694 | * zpool offline -f). | |
695 | */ | |
696 | rc = nvlist_lookup_string(nv, ZPOOL_CONFIG_AUX_STATE, &tmp); | |
697 | if (rc == 0 && tmp != NULL && strcmp(tmp, "external") == 0) { | |
698 | vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL; | |
699 | vd->vdev_faulted = 1; | |
700 | vd->vdev_label_aux = VDEV_AUX_EXTERNAL; | |
701 | } | |
702 | ||
34dc7c2f BB |
703 | if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) |
704 | vd->vdev_devid = spa_strdup(vd->vdev_devid); | |
705 | if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, | |
706 | &vd->vdev_physpath) == 0) | |
707 | vd->vdev_physpath = spa_strdup(vd->vdev_physpath); | |
1bbd8770 TH |
708 | |
709 | if (nvlist_lookup_string(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH, | |
710 | &vd->vdev_enc_sysfs_path) == 0) | |
711 | vd->vdev_enc_sysfs_path = spa_strdup(vd->vdev_enc_sysfs_path); | |
712 | ||
9babb374 BB |
713 | if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0) |
714 | vd->vdev_fru = spa_strdup(vd->vdev_fru); | |
34dc7c2f BB |
715 | |
716 | /* | |
717 | * Set the whole_disk property. If it's not specified, leave the value | |
718 | * as -1. | |
719 | */ | |
720 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, | |
721 | &vd->vdev_wholedisk) != 0) | |
722 | vd->vdev_wholedisk = -1ULL; | |
723 | ||
a1d477c2 MA |
724 | ASSERT0(vic->vic_mapping_object); |
725 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT, | |
726 | &vic->vic_mapping_object); | |
727 | ASSERT0(vic->vic_births_object); | |
728 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS, | |
729 | &vic->vic_births_object); | |
730 | ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX); | |
731 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV, | |
732 | &vic->vic_prev_indirect_vdev); | |
733 | ||
34dc7c2f BB |
734 | /* |
735 | * Look for the 'not present' flag. This will only be set if the device | |
736 | * was not present at the time of import. | |
737 | */ | |
9babb374 BB |
738 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, |
739 | &vd->vdev_not_present); | |
34dc7c2f BB |
740 | |
741 | /* | |
742 | * Get the alignment requirement. | |
743 | */ | |
744 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); | |
745 | ||
428870ff BB |
746 | /* |
747 | * Retrieve the vdev creation time. | |
748 | */ | |
749 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, | |
750 | &vd->vdev_crtxg); | |
751 | ||
34dc7c2f BB |
752 | /* |
753 | * If we're a top-level vdev, try to load the allocation parameters. | |
754 | */ | |
cc99f275 | 755 | if (top_level && |
428870ff | 756 | (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { |
34dc7c2f BB |
757 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, |
758 | &vd->vdev_ms_array); | |
759 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, | |
760 | &vd->vdev_ms_shift); | |
761 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, | |
762 | &vd->vdev_asize); | |
428870ff BB |
763 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING, |
764 | &vd->vdev_removing); | |
e0ab3ab5 JS |
765 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP, |
766 | &vd->vdev_top_zap); | |
767 | } else { | |
768 | ASSERT0(vd->vdev_top_zap); | |
428870ff BB |
769 | } |
770 | ||
cc99f275 | 771 | if (top_level && alloctype != VDEV_ALLOC_ATTACH) { |
428870ff BB |
772 | ASSERT(alloctype == VDEV_ALLOC_LOAD || |
773 | alloctype == VDEV_ALLOC_ADD || | |
774 | alloctype == VDEV_ALLOC_SPLIT || | |
775 | alloctype == VDEV_ALLOC_ROOTPOOL); | |
cc99f275 | 776 | /* Note: metaslab_group_create() is now deferred */ |
34dc7c2f BB |
777 | } |
778 | ||
e0ab3ab5 JS |
779 | if (vd->vdev_ops->vdev_op_leaf && |
780 | (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { | |
781 | (void) nvlist_lookup_uint64(nv, | |
782 | ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap); | |
783 | } else { | |
784 | ASSERT0(vd->vdev_leaf_zap); | |
785 | } | |
786 | ||
34dc7c2f BB |
787 | /* |
788 | * If we're a leaf vdev, try to load the DTL object and other state. | |
789 | */ | |
e0ab3ab5 | 790 | |
b128c09f | 791 | if (vd->vdev_ops->vdev_op_leaf && |
9babb374 BB |
792 | (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE || |
793 | alloctype == VDEV_ALLOC_ROOTPOOL)) { | |
b128c09f BB |
794 | if (alloctype == VDEV_ALLOC_LOAD) { |
795 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, | |
93cf2076 | 796 | &vd->vdev_dtl_object); |
b128c09f BB |
797 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, |
798 | &vd->vdev_unspare); | |
799 | } | |
9babb374 BB |
800 | |
801 | if (alloctype == VDEV_ALLOC_ROOTPOOL) { | |
802 | uint64_t spare = 0; | |
803 | ||
804 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, | |
805 | &spare) == 0 && spare) | |
806 | spa_spare_add(vd); | |
807 | } | |
808 | ||
34dc7c2f BB |
809 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, |
810 | &vd->vdev_offline); | |
b128c09f | 811 | |
5d1f7fb6 GW |
812 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG, |
813 | &vd->vdev_resilver_txg); | |
572e2857 | 814 | |
80a91e74 TC |
815 | if (nvlist_exists(nv, ZPOOL_CONFIG_RESILVER_DEFER)) |
816 | vdev_set_deferred_resilver(spa, vd); | |
817 | ||
34dc7c2f | 818 | /* |
4a283c7f TH |
819 | * In general, when importing a pool we want to ignore the |
820 | * persistent fault state, as the diagnosis made on another | |
821 | * system may not be valid in the current context. The only | |
822 | * exception is if we forced a vdev to a persistently faulted | |
823 | * state with 'zpool offline -f'. The persistent fault will | |
824 | * remain across imports until cleared. | |
825 | * | |
826 | * Local vdevs will remain in the faulted state. | |
34dc7c2f | 827 | */ |
4a283c7f TH |
828 | if (spa_load_state(spa) == SPA_LOAD_OPEN || |
829 | spa_load_state(spa) == SPA_LOAD_IMPORT) { | |
34dc7c2f BB |
830 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, |
831 | &vd->vdev_faulted); | |
832 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, | |
833 | &vd->vdev_degraded); | |
834 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, | |
835 | &vd->vdev_removed); | |
428870ff BB |
836 | |
837 | if (vd->vdev_faulted || vd->vdev_degraded) { | |
838 | char *aux; | |
839 | ||
840 | vd->vdev_label_aux = | |
841 | VDEV_AUX_ERR_EXCEEDED; | |
842 | if (nvlist_lookup_string(nv, | |
843 | ZPOOL_CONFIG_AUX_STATE, &aux) == 0 && | |
844 | strcmp(aux, "external") == 0) | |
845 | vd->vdev_label_aux = VDEV_AUX_EXTERNAL; | |
a0ad7ca5 TC |
846 | else |
847 | vd->vdev_faulted = 0ULL; | |
428870ff | 848 | } |
34dc7c2f BB |
849 | } |
850 | } | |
851 | ||
852 | /* | |
853 | * Add ourselves to the parent's list of children. | |
854 | */ | |
855 | vdev_add_child(parent, vd); | |
856 | ||
857 | *vdp = vd; | |
858 | ||
859 | return (0); | |
860 | } | |
861 | ||
862 | void | |
863 | vdev_free(vdev_t *vd) | |
864 | { | |
34dc7c2f | 865 | spa_t *spa = vd->vdev_spa; |
619f0976 | 866 | ASSERT3P(vd->vdev_initialize_thread, ==, NULL); |
34dc7c2f | 867 | |
d4a72f23 TC |
868 | /* |
869 | * Scan queues are normally destroyed at the end of a scan. If the | |
870 | * queue exists here, that implies the vdev is being removed while | |
871 | * the scan is still running. | |
872 | */ | |
873 | if (vd->vdev_scan_io_queue != NULL) { | |
874 | mutex_enter(&vd->vdev_scan_io_queue_lock); | |
875 | dsl_scan_io_queue_destroy(vd->vdev_scan_io_queue); | |
876 | vd->vdev_scan_io_queue = NULL; | |
877 | mutex_exit(&vd->vdev_scan_io_queue_lock); | |
878 | } | |
879 | ||
34dc7c2f BB |
880 | /* |
881 | * vdev_free() implies closing the vdev first. This is simpler than | |
882 | * trying to ensure complicated semantics for all callers. | |
883 | */ | |
884 | vdev_close(vd); | |
885 | ||
b128c09f | 886 | ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); |
428870ff | 887 | ASSERT(!list_link_active(&vd->vdev_state_dirty_node)); |
34dc7c2f BB |
888 | |
889 | /* | |
890 | * Free all children. | |
891 | */ | |
1c27024e | 892 | for (int c = 0; c < vd->vdev_children; c++) |
34dc7c2f BB |
893 | vdev_free(vd->vdev_child[c]); |
894 | ||
895 | ASSERT(vd->vdev_child == NULL); | |
896 | ASSERT(vd->vdev_guid_sum == vd->vdev_guid); | |
619f0976 | 897 | ASSERT(vd->vdev_initialize_thread == NULL); |
34dc7c2f BB |
898 | |
899 | /* | |
900 | * Discard allocation state. | |
901 | */ | |
428870ff | 902 | if (vd->vdev_mg != NULL) { |
34dc7c2f | 903 | vdev_metaslab_fini(vd); |
428870ff BB |
904 | metaslab_group_destroy(vd->vdev_mg); |
905 | } | |
34dc7c2f | 906 | |
c99c9001 MS |
907 | ASSERT0(vd->vdev_stat.vs_space); |
908 | ASSERT0(vd->vdev_stat.vs_dspace); | |
909 | ASSERT0(vd->vdev_stat.vs_alloc); | |
34dc7c2f BB |
910 | |
911 | /* | |
912 | * Remove this vdev from its parent's child list. | |
913 | */ | |
914 | vdev_remove_child(vd->vdev_parent, vd); | |
915 | ||
916 | ASSERT(vd->vdev_parent == NULL); | |
917 | ||
918 | /* | |
919 | * Clean up vdev structure. | |
920 | */ | |
921 | vdev_queue_fini(vd); | |
922 | vdev_cache_fini(vd); | |
923 | ||
924 | if (vd->vdev_path) | |
925 | spa_strfree(vd->vdev_path); | |
926 | if (vd->vdev_devid) | |
927 | spa_strfree(vd->vdev_devid); | |
928 | if (vd->vdev_physpath) | |
929 | spa_strfree(vd->vdev_physpath); | |
1bbd8770 TH |
930 | |
931 | if (vd->vdev_enc_sysfs_path) | |
932 | spa_strfree(vd->vdev_enc_sysfs_path); | |
933 | ||
9babb374 BB |
934 | if (vd->vdev_fru) |
935 | spa_strfree(vd->vdev_fru); | |
34dc7c2f BB |
936 | |
937 | if (vd->vdev_isspare) | |
938 | spa_spare_remove(vd); | |
939 | if (vd->vdev_isl2cache) | |
940 | spa_l2cache_remove(vd); | |
941 | ||
942 | txg_list_destroy(&vd->vdev_ms_list); | |
943 | txg_list_destroy(&vd->vdev_dtl_list); | |
fb5f0bc8 | 944 | |
34dc7c2f | 945 | mutex_enter(&vd->vdev_dtl_lock); |
93cf2076 | 946 | space_map_close(vd->vdev_dtl_sm); |
1c27024e | 947 | for (int t = 0; t < DTL_TYPES; t++) { |
93cf2076 GW |
948 | range_tree_vacate(vd->vdev_dtl[t], NULL, NULL); |
949 | range_tree_destroy(vd->vdev_dtl[t]); | |
fb5f0bc8 | 950 | } |
34dc7c2f | 951 | mutex_exit(&vd->vdev_dtl_lock); |
fb5f0bc8 | 952 | |
a1d477c2 MA |
953 | EQUIV(vd->vdev_indirect_births != NULL, |
954 | vd->vdev_indirect_mapping != NULL); | |
955 | if (vd->vdev_indirect_births != NULL) { | |
956 | vdev_indirect_mapping_close(vd->vdev_indirect_mapping); | |
957 | vdev_indirect_births_close(vd->vdev_indirect_births); | |
958 | } | |
959 | ||
960 | if (vd->vdev_obsolete_sm != NULL) { | |
961 | ASSERT(vd->vdev_removing || | |
962 | vd->vdev_ops == &vdev_indirect_ops); | |
963 | space_map_close(vd->vdev_obsolete_sm); | |
964 | vd->vdev_obsolete_sm = NULL; | |
965 | } | |
966 | range_tree_destroy(vd->vdev_obsolete_segments); | |
967 | rw_destroy(&vd->vdev_indirect_rwlock); | |
968 | mutex_destroy(&vd->vdev_obsolete_lock); | |
969 | ||
34dc7c2f BB |
970 | mutex_destroy(&vd->vdev_dtl_lock); |
971 | mutex_destroy(&vd->vdev_stat_lock); | |
b128c09f | 972 | mutex_destroy(&vd->vdev_probe_lock); |
d4a72f23 | 973 | mutex_destroy(&vd->vdev_scan_io_queue_lock); |
619f0976 GW |
974 | mutex_destroy(&vd->vdev_initialize_lock); |
975 | mutex_destroy(&vd->vdev_initialize_io_lock); | |
976 | cv_destroy(&vd->vdev_initialize_io_cv); | |
977 | cv_destroy(&vd->vdev_initialize_cv); | |
34dc7c2f | 978 | |
c17486b2 GN |
979 | zfs_ratelimit_fini(&vd->vdev_delay_rl); |
980 | zfs_ratelimit_fini(&vd->vdev_checksum_rl); | |
981 | ||
34dc7c2f BB |
982 | if (vd == spa->spa_root_vdev) |
983 | spa->spa_root_vdev = NULL; | |
984 | ||
985 | kmem_free(vd, sizeof (vdev_t)); | |
986 | } | |
987 | ||
988 | /* | |
989 | * Transfer top-level vdev state from svd to tvd. | |
990 | */ | |
991 | static void | |
992 | vdev_top_transfer(vdev_t *svd, vdev_t *tvd) | |
993 | { | |
994 | spa_t *spa = svd->vdev_spa; | |
995 | metaslab_t *msp; | |
996 | vdev_t *vd; | |
997 | int t; | |
998 | ||
999 | ASSERT(tvd == tvd->vdev_top); | |
1000 | ||
77943bc1 | 1001 | tvd->vdev_pending_fastwrite = svd->vdev_pending_fastwrite; |
34dc7c2f BB |
1002 | tvd->vdev_ms_array = svd->vdev_ms_array; |
1003 | tvd->vdev_ms_shift = svd->vdev_ms_shift; | |
1004 | tvd->vdev_ms_count = svd->vdev_ms_count; | |
e0ab3ab5 | 1005 | tvd->vdev_top_zap = svd->vdev_top_zap; |
34dc7c2f BB |
1006 | |
1007 | svd->vdev_ms_array = 0; | |
1008 | svd->vdev_ms_shift = 0; | |
1009 | svd->vdev_ms_count = 0; | |
e0ab3ab5 | 1010 | svd->vdev_top_zap = 0; |
34dc7c2f | 1011 | |
5ffb9d1d GW |
1012 | if (tvd->vdev_mg) |
1013 | ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg); | |
34dc7c2f BB |
1014 | tvd->vdev_mg = svd->vdev_mg; |
1015 | tvd->vdev_ms = svd->vdev_ms; | |
1016 | ||
1017 | svd->vdev_mg = NULL; | |
1018 | svd->vdev_ms = NULL; | |
1019 | ||
1020 | if (tvd->vdev_mg != NULL) | |
1021 | tvd->vdev_mg->mg_vd = tvd; | |
1022 | ||
d2734cce SD |
1023 | tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm; |
1024 | svd->vdev_checkpoint_sm = NULL; | |
1025 | ||
cc99f275 DB |
1026 | tvd->vdev_alloc_bias = svd->vdev_alloc_bias; |
1027 | svd->vdev_alloc_bias = VDEV_BIAS_NONE; | |
1028 | ||
34dc7c2f BB |
1029 | tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; |
1030 | tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; | |
1031 | tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; | |
1032 | ||
1033 | svd->vdev_stat.vs_alloc = 0; | |
1034 | svd->vdev_stat.vs_space = 0; | |
1035 | svd->vdev_stat.vs_dspace = 0; | |
1036 | ||
9e052db4 MA |
1037 | /* |
1038 | * State which may be set on a top-level vdev that's in the | |
1039 | * process of being removed. | |
1040 | */ | |
1041 | ASSERT0(tvd->vdev_indirect_config.vic_births_object); | |
1042 | ASSERT0(tvd->vdev_indirect_config.vic_mapping_object); | |
1043 | ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL); | |
1044 | ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL); | |
1045 | ASSERT3P(tvd->vdev_indirect_births, ==, NULL); | |
1046 | ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL); | |
1047 | ASSERT0(tvd->vdev_removing); | |
1048 | tvd->vdev_removing = svd->vdev_removing; | |
1049 | tvd->vdev_indirect_config = svd->vdev_indirect_config; | |
1050 | tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping; | |
1051 | tvd->vdev_indirect_births = svd->vdev_indirect_births; | |
1052 | range_tree_swap(&svd->vdev_obsolete_segments, | |
1053 | &tvd->vdev_obsolete_segments); | |
1054 | tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm; | |
1055 | svd->vdev_indirect_config.vic_mapping_object = 0; | |
1056 | svd->vdev_indirect_config.vic_births_object = 0; | |
1057 | svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL; | |
1058 | svd->vdev_indirect_mapping = NULL; | |
1059 | svd->vdev_indirect_births = NULL; | |
1060 | svd->vdev_obsolete_sm = NULL; | |
1061 | svd->vdev_removing = 0; | |
1062 | ||
34dc7c2f BB |
1063 | for (t = 0; t < TXG_SIZE; t++) { |
1064 | while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) | |
1065 | (void) txg_list_add(&tvd->vdev_ms_list, msp, t); | |
1066 | while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) | |
1067 | (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); | |
1068 | if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) | |
1069 | (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); | |
1070 | } | |
1071 | ||
b128c09f | 1072 | if (list_link_active(&svd->vdev_config_dirty_node)) { |
34dc7c2f BB |
1073 | vdev_config_clean(svd); |
1074 | vdev_config_dirty(tvd); | |
1075 | } | |
1076 | ||
b128c09f BB |
1077 | if (list_link_active(&svd->vdev_state_dirty_node)) { |
1078 | vdev_state_clean(svd); | |
1079 | vdev_state_dirty(tvd); | |
1080 | } | |
1081 | ||
34dc7c2f BB |
1082 | tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; |
1083 | svd->vdev_deflate_ratio = 0; | |
1084 | ||
1085 | tvd->vdev_islog = svd->vdev_islog; | |
1086 | svd->vdev_islog = 0; | |
d4a72f23 TC |
1087 | |
1088 | dsl_scan_io_queue_vdev_xfer(svd, tvd); | |
34dc7c2f BB |
1089 | } |
1090 | ||
1091 | static void | |
1092 | vdev_top_update(vdev_t *tvd, vdev_t *vd) | |
1093 | { | |
34dc7c2f BB |
1094 | if (vd == NULL) |
1095 | return; | |
1096 | ||
1097 | vd->vdev_top = tvd; | |
1098 | ||
1c27024e | 1099 | for (int c = 0; c < vd->vdev_children; c++) |
34dc7c2f BB |
1100 | vdev_top_update(tvd, vd->vdev_child[c]); |
1101 | } | |
1102 | ||
1103 | /* | |
1104 | * Add a mirror/replacing vdev above an existing vdev. | |
1105 | */ | |
1106 | vdev_t * | |
1107 | vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) | |
1108 | { | |
1109 | spa_t *spa = cvd->vdev_spa; | |
1110 | vdev_t *pvd = cvd->vdev_parent; | |
1111 | vdev_t *mvd; | |
1112 | ||
b128c09f | 1113 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
34dc7c2f BB |
1114 | |
1115 | mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); | |
1116 | ||
1117 | mvd->vdev_asize = cvd->vdev_asize; | |
9babb374 | 1118 | mvd->vdev_min_asize = cvd->vdev_min_asize; |
1bd201e7 | 1119 | mvd->vdev_max_asize = cvd->vdev_max_asize; |
a1d477c2 | 1120 | mvd->vdev_psize = cvd->vdev_psize; |
34dc7c2f BB |
1121 | mvd->vdev_ashift = cvd->vdev_ashift; |
1122 | mvd->vdev_state = cvd->vdev_state; | |
428870ff | 1123 | mvd->vdev_crtxg = cvd->vdev_crtxg; |
34dc7c2f BB |
1124 | |
1125 | vdev_remove_child(pvd, cvd); | |
1126 | vdev_add_child(pvd, mvd); | |
1127 | cvd->vdev_id = mvd->vdev_children; | |
1128 | vdev_add_child(mvd, cvd); | |
1129 | vdev_top_update(cvd->vdev_top, cvd->vdev_top); | |
1130 | ||
1131 | if (mvd == mvd->vdev_top) | |
1132 | vdev_top_transfer(cvd, mvd); | |
1133 | ||
1134 | return (mvd); | |
1135 | } | |
1136 | ||
1137 | /* | |
1138 | * Remove a 1-way mirror/replacing vdev from the tree. | |
1139 | */ | |
1140 | void | |
1141 | vdev_remove_parent(vdev_t *cvd) | |
1142 | { | |
1143 | vdev_t *mvd = cvd->vdev_parent; | |
1144 | vdev_t *pvd = mvd->vdev_parent; | |
1145 | ||
b128c09f | 1146 | ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
34dc7c2f BB |
1147 | |
1148 | ASSERT(mvd->vdev_children == 1); | |
1149 | ASSERT(mvd->vdev_ops == &vdev_mirror_ops || | |
1150 | mvd->vdev_ops == &vdev_replacing_ops || | |
1151 | mvd->vdev_ops == &vdev_spare_ops); | |
1152 | cvd->vdev_ashift = mvd->vdev_ashift; | |
1153 | ||
1154 | vdev_remove_child(mvd, cvd); | |
1155 | vdev_remove_child(pvd, mvd); | |
fb5f0bc8 | 1156 | |
34dc7c2f | 1157 | /* |
b128c09f BB |
1158 | * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. |
1159 | * Otherwise, we could have detached an offline device, and when we | |
1160 | * go to import the pool we'll think we have two top-level vdevs, | |
1161 | * instead of a different version of the same top-level vdev. | |
34dc7c2f | 1162 | */ |
fb5f0bc8 BB |
1163 | if (mvd->vdev_top == mvd) { |
1164 | uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; | |
428870ff | 1165 | cvd->vdev_orig_guid = cvd->vdev_guid; |
fb5f0bc8 BB |
1166 | cvd->vdev_guid += guid_delta; |
1167 | cvd->vdev_guid_sum += guid_delta; | |
61e99a73 AB |
1168 | |
1169 | /* | |
1170 | * If pool not set for autoexpand, we need to also preserve | |
1171 | * mvd's asize to prevent automatic expansion of cvd. | |
1172 | * Otherwise if we are adjusting the mirror by attaching and | |
1173 | * detaching children of non-uniform sizes, the mirror could | |
1174 | * autoexpand, unexpectedly requiring larger devices to | |
1175 | * re-establish the mirror. | |
1176 | */ | |
1177 | if (!cvd->vdev_spa->spa_autoexpand) | |
1178 | cvd->vdev_asize = mvd->vdev_asize; | |
fb5f0bc8 | 1179 | } |
b128c09f BB |
1180 | cvd->vdev_id = mvd->vdev_id; |
1181 | vdev_add_child(pvd, cvd); | |
34dc7c2f BB |
1182 | vdev_top_update(cvd->vdev_top, cvd->vdev_top); |
1183 | ||
1184 | if (cvd == cvd->vdev_top) | |
1185 | vdev_top_transfer(mvd, cvd); | |
1186 | ||
1187 | ASSERT(mvd->vdev_children == 0); | |
1188 | vdev_free(mvd); | |
1189 | } | |
1190 | ||
cc99f275 DB |
1191 | static void |
1192 | vdev_metaslab_group_create(vdev_t *vd) | |
1193 | { | |
1194 | spa_t *spa = vd->vdev_spa; | |
1195 | ||
1196 | /* | |
1197 | * metaslab_group_create was delayed until allocation bias was available | |
1198 | */ | |
1199 | if (vd->vdev_mg == NULL) { | |
1200 | metaslab_class_t *mc; | |
1201 | ||
1202 | if (vd->vdev_islog && vd->vdev_alloc_bias == VDEV_BIAS_NONE) | |
1203 | vd->vdev_alloc_bias = VDEV_BIAS_LOG; | |
1204 | ||
1205 | ASSERT3U(vd->vdev_islog, ==, | |
1206 | (vd->vdev_alloc_bias == VDEV_BIAS_LOG)); | |
1207 | ||
1208 | switch (vd->vdev_alloc_bias) { | |
1209 | case VDEV_BIAS_LOG: | |
1210 | mc = spa_log_class(spa); | |
1211 | break; | |
1212 | case VDEV_BIAS_SPECIAL: | |
1213 | mc = spa_special_class(spa); | |
1214 | break; | |
1215 | case VDEV_BIAS_DEDUP: | |
1216 | mc = spa_dedup_class(spa); | |
1217 | break; | |
1218 | default: | |
1219 | mc = spa_normal_class(spa); | |
1220 | } | |
1221 | ||
1222 | vd->vdev_mg = metaslab_group_create(mc, vd, | |
1223 | spa->spa_alloc_count); | |
1224 | ||
1225 | /* | |
1226 | * The spa ashift values currently only reflect the | |
1227 | * general vdev classes. Class destination is late | |
1228 | * binding so ashift checking had to wait until now | |
1229 | */ | |
1230 | if (vd->vdev_top == vd && vd->vdev_ashift != 0 && | |
1231 | mc == spa_normal_class(spa) && vd->vdev_aux == NULL) { | |
1232 | if (vd->vdev_ashift > spa->spa_max_ashift) | |
1233 | spa->spa_max_ashift = vd->vdev_ashift; | |
1234 | if (vd->vdev_ashift < spa->spa_min_ashift) | |
1235 | spa->spa_min_ashift = vd->vdev_ashift; | |
1236 | } | |
1237 | } | |
1238 | } | |
1239 | ||
34dc7c2f BB |
1240 | int |
1241 | vdev_metaslab_init(vdev_t *vd, uint64_t txg) | |
1242 | { | |
1243 | spa_t *spa = vd->vdev_spa; | |
1244 | objset_t *mos = spa->spa_meta_objset; | |
34dc7c2f BB |
1245 | uint64_t m; |
1246 | uint64_t oldc = vd->vdev_ms_count; | |
1247 | uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; | |
1248 | metaslab_t **mspp; | |
1249 | int error; | |
cc99f275 | 1250 | boolean_t expanding = (oldc != 0); |
34dc7c2f | 1251 | |
428870ff BB |
1252 | ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER)); |
1253 | ||
1254 | /* | |
1255 | * This vdev is not being allocated from yet or is a hole. | |
1256 | */ | |
1257 | if (vd->vdev_ms_shift == 0) | |
34dc7c2f BB |
1258 | return (0); |
1259 | ||
428870ff BB |
1260 | ASSERT(!vd->vdev_ishole); |
1261 | ||
34dc7c2f BB |
1262 | ASSERT(oldc <= newc); |
1263 | ||
bffb68a2 | 1264 | mspp = vmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); |
34dc7c2f | 1265 | |
cc99f275 | 1266 | if (expanding) { |
34dc7c2f | 1267 | bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); |
bffb68a2 | 1268 | vmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); |
34dc7c2f BB |
1269 | } |
1270 | ||
1271 | vd->vdev_ms = mspp; | |
1272 | vd->vdev_ms_count = newc; | |
34dc7c2f | 1273 | for (m = oldc; m < newc; m++) { |
93cf2076 GW |
1274 | uint64_t object = 0; |
1275 | ||
a1d477c2 MA |
1276 | /* |
1277 | * vdev_ms_array may be 0 if we are creating the "fake" | |
1278 | * metaslabs for an indirect vdev for zdb's leak detection. | |
1279 | * See zdb_leak_init(). | |
1280 | */ | |
1281 | if (txg == 0 && vd->vdev_ms_array != 0) { | |
34dc7c2f | 1282 | error = dmu_read(mos, vd->vdev_ms_array, |
9babb374 BB |
1283 | m * sizeof (uint64_t), sizeof (uint64_t), &object, |
1284 | DMU_READ_PREFETCH); | |
4a0ee12a PZ |
1285 | if (error != 0) { |
1286 | vdev_dbgmsg(vd, "unable to read the metaslab " | |
1287 | "array [error=%d]", error); | |
34dc7c2f | 1288 | return (error); |
4a0ee12a | 1289 | } |
34dc7c2f | 1290 | } |
fb42a493 | 1291 | |
cc99f275 DB |
1292 | #ifndef _KERNEL |
1293 | /* | |
1294 | * To accomodate zdb_leak_init() fake indirect | |
1295 | * metaslabs, we allocate a metaslab group for | |
1296 | * indirect vdevs which normally don't have one. | |
1297 | */ | |
1298 | if (vd->vdev_mg == NULL) { | |
1299 | ASSERT0(vdev_is_concrete(vd)); | |
1300 | vdev_metaslab_group_create(vd); | |
1301 | } | |
1302 | #endif | |
fb42a493 PS |
1303 | error = metaslab_init(vd->vdev_mg, m, object, txg, |
1304 | &(vd->vdev_ms[m])); | |
4a0ee12a PZ |
1305 | if (error != 0) { |
1306 | vdev_dbgmsg(vd, "metaslab_init failed [error=%d]", | |
1307 | error); | |
fb42a493 | 1308 | return (error); |
4a0ee12a | 1309 | } |
34dc7c2f BB |
1310 | } |
1311 | ||
428870ff BB |
1312 | if (txg == 0) |
1313 | spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER); | |
1314 | ||
1315 | /* | |
1316 | * If the vdev is being removed we don't activate | |
1317 | * the metaslabs since we want to ensure that no new | |
1318 | * allocations are performed on this device. | |
1319 | */ | |
cc99f275 | 1320 | if (!expanding && !vd->vdev_removing) { |
428870ff | 1321 | metaslab_group_activate(vd->vdev_mg); |
cc99f275 | 1322 | } |
428870ff BB |
1323 | |
1324 | if (txg == 0) | |
1325 | spa_config_exit(spa, SCL_ALLOC, FTAG); | |
1326 | ||
34dc7c2f BB |
1327 | return (0); |
1328 | } | |
1329 | ||
1330 | void | |
1331 | vdev_metaslab_fini(vdev_t *vd) | |
1332 | { | |
d2734cce SD |
1333 | if (vd->vdev_checkpoint_sm != NULL) { |
1334 | ASSERT(spa_feature_is_active(vd->vdev_spa, | |
1335 | SPA_FEATURE_POOL_CHECKPOINT)); | |
1336 | space_map_close(vd->vdev_checkpoint_sm); | |
1337 | /* | |
1338 | * Even though we close the space map, we need to set its | |
1339 | * pointer to NULL. The reason is that vdev_metaslab_fini() | |
1340 | * may be called multiple times for certain operations | |
1341 | * (i.e. when destroying a pool) so we need to ensure that | |
1342 | * this clause never executes twice. This logic is similar | |
1343 | * to the one used for the vdev_ms clause below. | |
1344 | */ | |
1345 | vd->vdev_checkpoint_sm = NULL; | |
1346 | } | |
1347 | ||
34dc7c2f | 1348 | if (vd->vdev_ms != NULL) { |
a1d477c2 MA |
1349 | uint64_t count = vd->vdev_ms_count; |
1350 | ||
428870ff | 1351 | metaslab_group_passivate(vd->vdev_mg); |
a1d477c2 | 1352 | for (uint64_t m = 0; m < count; m++) { |
93cf2076 GW |
1353 | metaslab_t *msp = vd->vdev_ms[m]; |
1354 | ||
1355 | if (msp != NULL) | |
1356 | metaslab_fini(msp); | |
1357 | } | |
bffb68a2 | 1358 | vmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); |
34dc7c2f | 1359 | vd->vdev_ms = NULL; |
920dd524 | 1360 | |
a1d477c2 MA |
1361 | vd->vdev_ms_count = 0; |
1362 | } | |
1363 | ASSERT0(vd->vdev_ms_count); | |
920dd524 | 1364 | ASSERT3U(vd->vdev_pending_fastwrite, ==, 0); |
34dc7c2f BB |
1365 | } |
1366 | ||
b128c09f BB |
1367 | typedef struct vdev_probe_stats { |
1368 | boolean_t vps_readable; | |
1369 | boolean_t vps_writeable; | |
1370 | int vps_flags; | |
b128c09f BB |
1371 | } vdev_probe_stats_t; |
1372 | ||
1373 | static void | |
1374 | vdev_probe_done(zio_t *zio) | |
34dc7c2f | 1375 | { |
fb5f0bc8 | 1376 | spa_t *spa = zio->io_spa; |
d164b209 | 1377 | vdev_t *vd = zio->io_vd; |
b128c09f | 1378 | vdev_probe_stats_t *vps = zio->io_private; |
d164b209 BB |
1379 | |
1380 | ASSERT(vd->vdev_probe_zio != NULL); | |
b128c09f BB |
1381 | |
1382 | if (zio->io_type == ZIO_TYPE_READ) { | |
b128c09f BB |
1383 | if (zio->io_error == 0) |
1384 | vps->vps_readable = 1; | |
fb5f0bc8 | 1385 | if (zio->io_error == 0 && spa_writeable(spa)) { |
d164b209 | 1386 | zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, |
a6255b7f | 1387 | zio->io_offset, zio->io_size, zio->io_abd, |
b128c09f BB |
1388 | ZIO_CHECKSUM_OFF, vdev_probe_done, vps, |
1389 | ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); | |
1390 | } else { | |
a6255b7f | 1391 | abd_free(zio->io_abd); |
b128c09f BB |
1392 | } |
1393 | } else if (zio->io_type == ZIO_TYPE_WRITE) { | |
b128c09f BB |
1394 | if (zio->io_error == 0) |
1395 | vps->vps_writeable = 1; | |
a6255b7f | 1396 | abd_free(zio->io_abd); |
b128c09f | 1397 | } else if (zio->io_type == ZIO_TYPE_NULL) { |
d164b209 | 1398 | zio_t *pio; |
3dfb57a3 | 1399 | zio_link_t *zl; |
b128c09f BB |
1400 | |
1401 | vd->vdev_cant_read |= !vps->vps_readable; | |
1402 | vd->vdev_cant_write |= !vps->vps_writeable; | |
1403 | ||
1404 | if (vdev_readable(vd) && | |
fb5f0bc8 | 1405 | (vdev_writeable(vd) || !spa_writeable(spa))) { |
b128c09f BB |
1406 | zio->io_error = 0; |
1407 | } else { | |
1408 | ASSERT(zio->io_error != 0); | |
4a0ee12a | 1409 | vdev_dbgmsg(vd, "failed probe"); |
b128c09f | 1410 | zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, |
b5256303 | 1411 | spa, vd, NULL, NULL, 0, 0); |
2e528b49 | 1412 | zio->io_error = SET_ERROR(ENXIO); |
b128c09f | 1413 | } |
d164b209 BB |
1414 | |
1415 | mutex_enter(&vd->vdev_probe_lock); | |
1416 | ASSERT(vd->vdev_probe_zio == zio); | |
1417 | vd->vdev_probe_zio = NULL; | |
1418 | mutex_exit(&vd->vdev_probe_lock); | |
1419 | ||
3dfb57a3 DB |
1420 | zl = NULL; |
1421 | while ((pio = zio_walk_parents(zio, &zl)) != NULL) | |
d164b209 | 1422 | if (!vdev_accessible(vd, pio)) |
2e528b49 | 1423 | pio->io_error = SET_ERROR(ENXIO); |
d164b209 | 1424 | |
b128c09f BB |
1425 | kmem_free(vps, sizeof (*vps)); |
1426 | } | |
1427 | } | |
34dc7c2f | 1428 | |
b128c09f | 1429 | /* |
d3cc8b15 WA |
1430 | * Determine whether this device is accessible. |
1431 | * | |
1432 | * Read and write to several known locations: the pad regions of each | |
1433 | * vdev label but the first, which we leave alone in case it contains | |
1434 | * a VTOC. | |
b128c09f BB |
1435 | */ |
1436 | zio_t * | |
d164b209 | 1437 | vdev_probe(vdev_t *vd, zio_t *zio) |
b128c09f BB |
1438 | { |
1439 | spa_t *spa = vd->vdev_spa; | |
d164b209 BB |
1440 | vdev_probe_stats_t *vps = NULL; |
1441 | zio_t *pio; | |
1442 | ||
1443 | ASSERT(vd->vdev_ops->vdev_op_leaf); | |
34dc7c2f | 1444 | |
d164b209 BB |
1445 | /* |
1446 | * Don't probe the probe. | |
1447 | */ | |
1448 | if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) | |
1449 | return (NULL); | |
b128c09f | 1450 | |
d164b209 BB |
1451 | /* |
1452 | * To prevent 'probe storms' when a device fails, we create | |
1453 | * just one probe i/o at a time. All zios that want to probe | |
1454 | * this vdev will become parents of the probe io. | |
1455 | */ | |
1456 | mutex_enter(&vd->vdev_probe_lock); | |
b128c09f | 1457 | |
d164b209 | 1458 | if ((pio = vd->vdev_probe_zio) == NULL) { |
79c76d5b | 1459 | vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); |
d164b209 BB |
1460 | |
1461 | vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | | |
1462 | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | | |
9babb374 | 1463 | ZIO_FLAG_TRYHARD; |
d164b209 BB |
1464 | |
1465 | if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { | |
1466 | /* | |
1467 | * vdev_cant_read and vdev_cant_write can only | |
1468 | * transition from TRUE to FALSE when we have the | |
1469 | * SCL_ZIO lock as writer; otherwise they can only | |
1470 | * transition from FALSE to TRUE. This ensures that | |
1471 | * any zio looking at these values can assume that | |
1472 | * failures persist for the life of the I/O. That's | |
1473 | * important because when a device has intermittent | |
1474 | * connectivity problems, we want to ensure that | |
1475 | * they're ascribed to the device (ENXIO) and not | |
1476 | * the zio (EIO). | |
1477 | * | |
1478 | * Since we hold SCL_ZIO as writer here, clear both | |
1479 | * values so the probe can reevaluate from first | |
1480 | * principles. | |
1481 | */ | |
1482 | vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; | |
1483 | vd->vdev_cant_read = B_FALSE; | |
1484 | vd->vdev_cant_write = B_FALSE; | |
1485 | } | |
1486 | ||
1487 | vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, | |
1488 | vdev_probe_done, vps, | |
1489 | vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); | |
1490 | ||
428870ff BB |
1491 | /* |
1492 | * We can't change the vdev state in this context, so we | |
1493 | * kick off an async task to do it on our behalf. | |
1494 | */ | |
d164b209 BB |
1495 | if (zio != NULL) { |
1496 | vd->vdev_probe_wanted = B_TRUE; | |
1497 | spa_async_request(spa, SPA_ASYNC_PROBE); | |
1498 | } | |
b128c09f BB |
1499 | } |
1500 | ||
d164b209 BB |
1501 | if (zio != NULL) |
1502 | zio_add_child(zio, pio); | |
b128c09f | 1503 | |
d164b209 | 1504 | mutex_exit(&vd->vdev_probe_lock); |
b128c09f | 1505 | |
d164b209 BB |
1506 | if (vps == NULL) { |
1507 | ASSERT(zio != NULL); | |
1508 | return (NULL); | |
1509 | } | |
b128c09f | 1510 | |
1c27024e | 1511 | for (int l = 1; l < VDEV_LABELS; l++) { |
d164b209 | 1512 | zio_nowait(zio_read_phys(pio, vd, |
b128c09f | 1513 | vdev_label_offset(vd->vdev_psize, l, |
a6255b7f DQ |
1514 | offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE, |
1515 | abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE), | |
b128c09f BB |
1516 | ZIO_CHECKSUM_OFF, vdev_probe_done, vps, |
1517 | ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); | |
1518 | } | |
1519 | ||
d164b209 BB |
1520 | if (zio == NULL) |
1521 | return (pio); | |
1522 | ||
1523 | zio_nowait(pio); | |
1524 | return (NULL); | |
34dc7c2f BB |
1525 | } |
1526 | ||
45d1cae3 BB |
1527 | static void |
1528 | vdev_open_child(void *arg) | |
1529 | { | |
1530 | vdev_t *vd = arg; | |
1531 | ||
1532 | vd->vdev_open_thread = curthread; | |
1533 | vd->vdev_open_error = vdev_open(vd); | |
1534 | vd->vdev_open_thread = NULL; | |
1535 | } | |
1536 | ||
6c285672 | 1537 | static boolean_t |
428870ff BB |
1538 | vdev_uses_zvols(vdev_t *vd) |
1539 | { | |
6c285672 JL |
1540 | #ifdef _KERNEL |
1541 | if (zvol_is_zvol(vd->vdev_path)) | |
428870ff | 1542 | return (B_TRUE); |
6c285672 JL |
1543 | #endif |
1544 | ||
1c27024e | 1545 | for (int c = 0; c < vd->vdev_children; c++) |
428870ff BB |
1546 | if (vdev_uses_zvols(vd->vdev_child[c])) |
1547 | return (B_TRUE); | |
6c285672 | 1548 | |
428870ff BB |
1549 | return (B_FALSE); |
1550 | } | |
1551 | ||
45d1cae3 BB |
1552 | void |
1553 | vdev_open_children(vdev_t *vd) | |
1554 | { | |
1555 | taskq_t *tq; | |
1556 | int children = vd->vdev_children; | |
1557 | ||
428870ff BB |
1558 | /* |
1559 | * in order to handle pools on top of zvols, do the opens | |
1560 | * in a single thread so that the same thread holds the | |
1561 | * spa_namespace_lock | |
1562 | */ | |
1563 | if (vdev_uses_zvols(vd)) { | |
13d9a004 | 1564 | retry_sync: |
1c27024e | 1565 | for (int c = 0; c < children; c++) |
428870ff BB |
1566 | vd->vdev_child[c]->vdev_open_error = |
1567 | vdev_open(vd->vdev_child[c]); | |
4770aa06 HJ |
1568 | } else { |
1569 | tq = taskq_create("vdev_open", children, minclsyspri, | |
1570 | children, children, TASKQ_PREPOPULATE); | |
13d9a004 BB |
1571 | if (tq == NULL) |
1572 | goto retry_sync; | |
45d1cae3 | 1573 | |
1c27024e | 1574 | for (int c = 0; c < children; c++) |
4770aa06 | 1575 | VERIFY(taskq_dispatch(tq, vdev_open_child, |
48d3eb40 | 1576 | vd->vdev_child[c], TQ_SLEEP) != TASKQID_INVALID); |
45d1cae3 | 1577 | |
4770aa06 HJ |
1578 | taskq_destroy(tq); |
1579 | } | |
1580 | ||
1581 | vd->vdev_nonrot = B_TRUE; | |
fb40095f | 1582 | |
1c27024e | 1583 | for (int c = 0; c < children; c++) |
fb40095f | 1584 | vd->vdev_nonrot &= vd->vdev_child[c]->vdev_nonrot; |
45d1cae3 BB |
1585 | } |
1586 | ||
a1d477c2 MA |
1587 | /* |
1588 | * Compute the raidz-deflation ratio. Note, we hard-code | |
1589 | * in 128k (1 << 17) because it is the "typical" blocksize. | |
1590 | * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change, | |
1591 | * otherwise it would inconsistently account for existing bp's. | |
1592 | */ | |
1593 | static void | |
1594 | vdev_set_deflate_ratio(vdev_t *vd) | |
1595 | { | |
1596 | if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) { | |
1597 | vd->vdev_deflate_ratio = (1 << 17) / | |
1598 | (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT); | |
1599 | } | |
1600 | } | |
1601 | ||
34dc7c2f BB |
1602 | /* |
1603 | * Prepare a virtual device for access. | |
1604 | */ | |
1605 | int | |
1606 | vdev_open(vdev_t *vd) | |
1607 | { | |
fb5f0bc8 | 1608 | spa_t *spa = vd->vdev_spa; |
34dc7c2f | 1609 | int error; |
34dc7c2f | 1610 | uint64_t osize = 0; |
1bd201e7 CS |
1611 | uint64_t max_osize = 0; |
1612 | uint64_t asize, max_asize, psize; | |
34dc7c2f BB |
1613 | uint64_t ashift = 0; |
1614 | ||
45d1cae3 BB |
1615 | ASSERT(vd->vdev_open_thread == curthread || |
1616 | spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); | |
34dc7c2f BB |
1617 | ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || |
1618 | vd->vdev_state == VDEV_STATE_CANT_OPEN || | |
1619 | vd->vdev_state == VDEV_STATE_OFFLINE); | |
1620 | ||
34dc7c2f | 1621 | vd->vdev_stat.vs_aux = VDEV_AUX_NONE; |
9babb374 BB |
1622 | vd->vdev_cant_read = B_FALSE; |
1623 | vd->vdev_cant_write = B_FALSE; | |
1624 | vd->vdev_min_asize = vdev_get_min_asize(vd); | |
34dc7c2f | 1625 | |
428870ff BB |
1626 | /* |
1627 | * If this vdev is not removed, check its fault status. If it's | |
1628 | * faulted, bail out of the open. | |
1629 | */ | |
34dc7c2f BB |
1630 | if (!vd->vdev_removed && vd->vdev_faulted) { |
1631 | ASSERT(vd->vdev_children == 0); | |
428870ff BB |
1632 | ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || |
1633 | vd->vdev_label_aux == VDEV_AUX_EXTERNAL); | |
34dc7c2f | 1634 | vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, |
428870ff | 1635 | vd->vdev_label_aux); |
2e528b49 | 1636 | return (SET_ERROR(ENXIO)); |
34dc7c2f BB |
1637 | } else if (vd->vdev_offline) { |
1638 | ASSERT(vd->vdev_children == 0); | |
1639 | vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); | |
2e528b49 | 1640 | return (SET_ERROR(ENXIO)); |
34dc7c2f BB |
1641 | } |
1642 | ||
1bd201e7 | 1643 | error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift); |
34dc7c2f | 1644 | |
428870ff BB |
1645 | /* |
1646 | * Reset the vdev_reopening flag so that we actually close | |
1647 | * the vdev on error. | |
1648 | */ | |
1649 | vd->vdev_reopening = B_FALSE; | |
34dc7c2f | 1650 | if (zio_injection_enabled && error == 0) |
9babb374 | 1651 | error = zio_handle_device_injection(vd, NULL, ENXIO); |
34dc7c2f BB |
1652 | |
1653 | if (error) { | |
1654 | if (vd->vdev_removed && | |
1655 | vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) | |
1656 | vd->vdev_removed = B_FALSE; | |
1657 | ||
6cb8e530 PZ |
1658 | if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) { |
1659 | vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, | |
1660 | vd->vdev_stat.vs_aux); | |
1661 | } else { | |
1662 | vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, | |
1663 | vd->vdev_stat.vs_aux); | |
1664 | } | |
34dc7c2f BB |
1665 | return (error); |
1666 | } | |
1667 | ||
1668 | vd->vdev_removed = B_FALSE; | |
1669 | ||
428870ff BB |
1670 | /* |
1671 | * Recheck the faulted flag now that we have confirmed that | |
1672 | * the vdev is accessible. If we're faulted, bail. | |
1673 | */ | |
1674 | if (vd->vdev_faulted) { | |
1675 | ASSERT(vd->vdev_children == 0); | |
1676 | ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || | |
1677 | vd->vdev_label_aux == VDEV_AUX_EXTERNAL); | |
1678 | vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, | |
1679 | vd->vdev_label_aux); | |
2e528b49 | 1680 | return (SET_ERROR(ENXIO)); |
428870ff BB |
1681 | } |
1682 | ||
34dc7c2f BB |
1683 | if (vd->vdev_degraded) { |
1684 | ASSERT(vd->vdev_children == 0); | |
1685 | vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, | |
1686 | VDEV_AUX_ERR_EXCEEDED); | |
1687 | } else { | |
428870ff | 1688 | vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0); |
34dc7c2f BB |
1689 | } |
1690 | ||
428870ff BB |
1691 | /* |
1692 | * For hole or missing vdevs we just return success. | |
1693 | */ | |
1694 | if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) | |
1695 | return (0); | |
1696 | ||
1c27024e | 1697 | for (int c = 0; c < vd->vdev_children; c++) { |
34dc7c2f BB |
1698 | if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { |
1699 | vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, | |
1700 | VDEV_AUX_NONE); | |
1701 | break; | |
1702 | } | |
9babb374 | 1703 | } |
34dc7c2f BB |
1704 | |
1705 | osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); | |
1bd201e7 | 1706 | max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t)); |
34dc7c2f BB |
1707 | |
1708 | if (vd->vdev_children == 0) { | |
1709 | if (osize < SPA_MINDEVSIZE) { | |
1710 | vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, | |
1711 | VDEV_AUX_TOO_SMALL); | |
2e528b49 | 1712 | return (SET_ERROR(EOVERFLOW)); |
34dc7c2f BB |
1713 | } |
1714 | psize = osize; | |
1715 | asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); | |
1bd201e7 CS |
1716 | max_asize = max_osize - (VDEV_LABEL_START_SIZE + |
1717 | VDEV_LABEL_END_SIZE); | |
34dc7c2f BB |
1718 | } else { |
1719 | if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - | |
1720 | (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { | |
1721 | vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, | |
1722 | VDEV_AUX_TOO_SMALL); | |
2e528b49 | 1723 | return (SET_ERROR(EOVERFLOW)); |
34dc7c2f BB |
1724 | } |
1725 | psize = 0; | |
1726 | asize = osize; | |
1bd201e7 | 1727 | max_asize = max_osize; |
34dc7c2f BB |
1728 | } |
1729 | ||
9d3f7b87 OF |
1730 | /* |
1731 | * If the vdev was expanded, record this so that we can re-create the | |
1732 | * uberblock rings in labels {2,3}, during the next sync. | |
1733 | */ | |
1734 | if ((psize > vd->vdev_psize) && (vd->vdev_psize != 0)) | |
1735 | vd->vdev_copy_uberblocks = B_TRUE; | |
1736 | ||
34dc7c2f BB |
1737 | vd->vdev_psize = psize; |
1738 | ||
9babb374 | 1739 | /* |
2e215fec | 1740 | * Make sure the allocatable size hasn't shrunk too much. |
9babb374 BB |
1741 | */ |
1742 | if (asize < vd->vdev_min_asize) { | |
1743 | vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, | |
1744 | VDEV_AUX_BAD_LABEL); | |
2e528b49 | 1745 | return (SET_ERROR(EINVAL)); |
9babb374 BB |
1746 | } |
1747 | ||
34dc7c2f BB |
1748 | if (vd->vdev_asize == 0) { |
1749 | /* | |
1750 | * This is the first-ever open, so use the computed values. | |
b28e57cb | 1751 | * For compatibility, a different ashift can be requested. |
34dc7c2f BB |
1752 | */ |
1753 | vd->vdev_asize = asize; | |
1bd201e7 | 1754 | vd->vdev_max_asize = max_asize; |
ff61d1a4 | 1755 | if (vd->vdev_ashift == 0) { |
1756 | vd->vdev_ashift = ashift; /* use detected value */ | |
1757 | } | |
1758 | if (vd->vdev_ashift != 0 && (vd->vdev_ashift < ASHIFT_MIN || | |
1759 | vd->vdev_ashift > ASHIFT_MAX)) { | |
1760 | vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, | |
1761 | VDEV_AUX_BAD_ASHIFT); | |
1762 | return (SET_ERROR(EDOM)); | |
1763 | } | |
34dc7c2f BB |
1764 | } else { |
1765 | /* | |
32a9872b GW |
1766 | * Detect if the alignment requirement has increased. |
1767 | * We don't want to make the pool unavailable, just | |
1768 | * post an event instead. | |
34dc7c2f | 1769 | */ |
32a9872b GW |
1770 | if (ashift > vd->vdev_top->vdev_ashift && |
1771 | vd->vdev_ops->vdev_op_leaf) { | |
1772 | zfs_ereport_post(FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT, | |
b5256303 | 1773 | spa, vd, NULL, NULL, 0, 0); |
34dc7c2f | 1774 | } |
32a9872b | 1775 | |
1bd201e7 | 1776 | vd->vdev_max_asize = max_asize; |
9babb374 | 1777 | } |
34dc7c2f | 1778 | |
9babb374 | 1779 | /* |
2e215fec SH |
1780 | * If all children are healthy we update asize if either: |
1781 | * The asize has increased, due to a device expansion caused by dynamic | |
1782 | * LUN growth or vdev replacement, and automatic expansion is enabled; | |
1783 | * making the additional space available. | |
1784 | * | |
1785 | * The asize has decreased, due to a device shrink usually caused by a | |
1786 | * vdev replace with a smaller device. This ensures that calculations | |
1787 | * based of max_asize and asize e.g. esize are always valid. It's safe | |
1788 | * to do this as we've already validated that asize is greater than | |
1789 | * vdev_min_asize. | |
9babb374 | 1790 | */ |
2e215fec SH |
1791 | if (vd->vdev_state == VDEV_STATE_HEALTHY && |
1792 | ((asize > vd->vdev_asize && | |
1793 | (vd->vdev_expanding || spa->spa_autoexpand)) || | |
1794 | (asize < vd->vdev_asize))) | |
9babb374 | 1795 | vd->vdev_asize = asize; |
34dc7c2f | 1796 | |
9babb374 | 1797 | vdev_set_min_asize(vd); |
34dc7c2f BB |
1798 | |
1799 | /* | |
1800 | * Ensure we can issue some IO before declaring the | |
1801 | * vdev open for business. | |
1802 | */ | |
b128c09f BB |
1803 | if (vd->vdev_ops->vdev_op_leaf && |
1804 | (error = zio_wait(vdev_probe(vd, NULL))) != 0) { | |
428870ff BB |
1805 | vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, |
1806 | VDEV_AUX_ERR_EXCEEDED); | |
34dc7c2f BB |
1807 | return (error); |
1808 | } | |
1809 | ||
c3520e7f MA |
1810 | /* |
1811 | * Track the min and max ashift values for normal data devices. | |
cc99f275 DB |
1812 | * |
1813 | * DJB - TBD these should perhaps be tracked per allocation class | |
1814 | * (e.g. spa_min_ashift is used to round up post compression buffers) | |
c3520e7f MA |
1815 | */ |
1816 | if (vd->vdev_top == vd && vd->vdev_ashift != 0 && | |
cc99f275 DB |
1817 | vd->vdev_alloc_bias == VDEV_BIAS_NONE && |
1818 | vd->vdev_aux == NULL) { | |
c3520e7f MA |
1819 | if (vd->vdev_ashift > spa->spa_max_ashift) |
1820 | spa->spa_max_ashift = vd->vdev_ashift; | |
1821 | if (vd->vdev_ashift < spa->spa_min_ashift) | |
1822 | spa->spa_min_ashift = vd->vdev_ashift; | |
1823 | } | |
1824 | ||
34dc7c2f | 1825 | /* |
b128c09f | 1826 | * If a leaf vdev has a DTL, and seems healthy, then kick off a |
fb5f0bc8 BB |
1827 | * resilver. But don't do this if we are doing a reopen for a scrub, |
1828 | * since this would just restart the scrub we are already doing. | |
34dc7c2f | 1829 | */ |
fb5f0bc8 | 1830 | if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen && |
80a91e74 TC |
1831 | vdev_resilver_needed(vd, NULL, NULL)) { |
1832 | if (dsl_scan_resilvering(spa->spa_dsl_pool) && | |
1833 | spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) | |
1834 | vdev_set_deferred_resilver(spa, vd); | |
1835 | else | |
1836 | spa_async_request(spa, SPA_ASYNC_RESILVER); | |
1837 | } | |
34dc7c2f BB |
1838 | |
1839 | return (0); | |
1840 | } | |
1841 | ||
1842 | /* | |
1843 | * Called once the vdevs are all opened, this routine validates the label | |
6cb8e530 | 1844 | * contents. This needs to be done before vdev_load() so that we don't |
34dc7c2f BB |
1845 | * inadvertently do repair I/Os to the wrong device. |
1846 | * | |
1847 | * This function will only return failure if one of the vdevs indicates that it | |
1848 | * has since been destroyed or exported. This is only possible if | |
1849 | * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state | |
1850 | * will be updated but the function will return 0. | |
1851 | */ | |
1852 | int | |
6cb8e530 | 1853 | vdev_validate(vdev_t *vd) |
34dc7c2f BB |
1854 | { |
1855 | spa_t *spa = vd->vdev_spa; | |
34dc7c2f | 1856 | nvlist_t *label; |
6cb8e530 | 1857 | uint64_t guid = 0, aux_guid = 0, top_guid; |
34dc7c2f | 1858 | uint64_t state; |
6cb8e530 PZ |
1859 | nvlist_t *nvl; |
1860 | uint64_t txg; | |
34dc7c2f | 1861 | |
6cb8e530 PZ |
1862 | if (vdev_validate_skip) |
1863 | return (0); | |
1864 | ||
1865 | for (uint64_t c = 0; c < vd->vdev_children; c++) | |
1866 | if (vdev_validate(vd->vdev_child[c]) != 0) | |
2e528b49 | 1867 | return (SET_ERROR(EBADF)); |
34dc7c2f BB |
1868 | |
1869 | /* | |
1870 | * If the device has already failed, or was marked offline, don't do | |
1871 | * any further validation. Otherwise, label I/O will fail and we will | |
1872 | * overwrite the previous state. | |
1873 | */ | |
6cb8e530 PZ |
1874 | if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd)) |
1875 | return (0); | |
34dc7c2f | 1876 | |
6cb8e530 PZ |
1877 | /* |
1878 | * If we are performing an extreme rewind, we allow for a label that | |
1879 | * was modified at a point after the current txg. | |
a11c7aae PZ |
1880 | * If config lock is not held do not check for the txg. spa_sync could |
1881 | * be updating the vdev's label before updating spa_last_synced_txg. | |
6cb8e530 | 1882 | */ |
a11c7aae PZ |
1883 | if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 || |
1884 | spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG) | |
6cb8e530 PZ |
1885 | txg = UINT64_MAX; |
1886 | else | |
1887 | txg = spa_last_synced_txg(spa); | |
34dc7c2f | 1888 | |
6cb8e530 PZ |
1889 | if ((label = vdev_label_read_config(vd, txg)) == NULL) { |
1890 | vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, | |
1891 | VDEV_AUX_BAD_LABEL); | |
38a19edd PZ |
1892 | vdev_dbgmsg(vd, "vdev_validate: failed reading config for " |
1893 | "txg %llu", (u_longlong_t)txg); | |
6cb8e530 PZ |
1894 | return (0); |
1895 | } | |
428870ff | 1896 | |
6cb8e530 PZ |
1897 | /* |
1898 | * Determine if this vdev has been split off into another | |
1899 | * pool. If so, then refuse to open it. | |
1900 | */ | |
1901 | if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID, | |
1902 | &aux_guid) == 0 && aux_guid == spa_guid(spa)) { | |
1903 | vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, | |
1904 | VDEV_AUX_SPLIT_POOL); | |
1905 | nvlist_free(label); | |
1906 | vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool"); | |
1907 | return (0); | |
1908 | } | |
34dc7c2f | 1909 | |
6cb8e530 PZ |
1910 | if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) { |
1911 | vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, | |
1912 | VDEV_AUX_CORRUPT_DATA); | |
1913 | nvlist_free(label); | |
1914 | vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", | |
1915 | ZPOOL_CONFIG_POOL_GUID); | |
1916 | return (0); | |
1917 | } | |
428870ff | 1918 | |
6cb8e530 PZ |
1919 | /* |
1920 | * If config is not trusted then ignore the spa guid check. This is | |
1921 | * necessary because if the machine crashed during a re-guid the new | |
1922 | * guid might have been written to all of the vdev labels, but not the | |
1923 | * cached config. The check will be performed again once we have the | |
1924 | * trusted config from the MOS. | |
1925 | */ | |
1926 | if (spa->spa_trust_config && guid != spa_guid(spa)) { | |
1927 | vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, | |
1928 | VDEV_AUX_CORRUPT_DATA); | |
1929 | nvlist_free(label); | |
1930 | vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't " | |
1931 | "match config (%llu != %llu)", (u_longlong_t)guid, | |
1932 | (u_longlong_t)spa_guid(spa)); | |
1933 | return (0); | |
1934 | } | |
1935 | ||
1936 | if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl) | |
1937 | != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID, | |
1938 | &aux_guid) != 0) | |
1939 | aux_guid = 0; | |
1940 | ||
1941 | if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) { | |
1942 | vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, | |
1943 | VDEV_AUX_CORRUPT_DATA); | |
1944 | nvlist_free(label); | |
1945 | vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", | |
1946 | ZPOOL_CONFIG_GUID); | |
1947 | return (0); | |
1948 | } | |
1949 | ||
1950 | if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid) | |
1951 | != 0) { | |
1952 | vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, | |
1953 | VDEV_AUX_CORRUPT_DATA); | |
1954 | nvlist_free(label); | |
1955 | vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", | |
1956 | ZPOOL_CONFIG_TOP_GUID); | |
1957 | return (0); | |
1958 | } | |
1959 | ||
1960 | /* | |
1961 | * If this vdev just became a top-level vdev because its sibling was | |
1962 | * detached, it will have adopted the parent's vdev guid -- but the | |
1963 | * label may or may not be on disk yet. Fortunately, either version | |
1964 | * of the label will have the same top guid, so if we're a top-level | |
1965 | * vdev, we can safely compare to that instead. | |
1966 | * However, if the config comes from a cachefile that failed to update | |
1967 | * after the detach, a top-level vdev will appear as a non top-level | |
1968 | * vdev in the config. Also relax the constraints if we perform an | |
1969 | * extreme rewind. | |
1970 | * | |
1971 | * If we split this vdev off instead, then we also check the | |
1972 | * original pool's guid. We don't want to consider the vdev | |
1973 | * corrupt if it is partway through a split operation. | |
1974 | */ | |
1975 | if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) { | |
1976 | boolean_t mismatch = B_FALSE; | |
1977 | if (spa->spa_trust_config && !spa->spa_extreme_rewind) { | |
1978 | if (vd != vd->vdev_top || vd->vdev_guid != top_guid) | |
1979 | mismatch = B_TRUE; | |
1980 | } else { | |
1981 | if (vd->vdev_guid != top_guid && | |
1982 | vd->vdev_top->vdev_guid != guid) | |
1983 | mismatch = B_TRUE; | |
34dc7c2f BB |
1984 | } |
1985 | ||
6cb8e530 | 1986 | if (mismatch) { |
34dc7c2f BB |
1987 | vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, |
1988 | VDEV_AUX_CORRUPT_DATA); | |
1989 | nvlist_free(label); | |
6cb8e530 PZ |
1990 | vdev_dbgmsg(vd, "vdev_validate: config guid " |
1991 | "doesn't match label guid"); | |
1992 | vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu", | |
1993 | (u_longlong_t)vd->vdev_guid, | |
1994 | (u_longlong_t)vd->vdev_top->vdev_guid); | |
1995 | vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, " | |
1996 | "aux_guid %llu", (u_longlong_t)guid, | |
1997 | (u_longlong_t)top_guid, (u_longlong_t)aux_guid); | |
34dc7c2f BB |
1998 | return (0); |
1999 | } | |
6cb8e530 | 2000 | } |
34dc7c2f | 2001 | |
6cb8e530 PZ |
2002 | if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, |
2003 | &state) != 0) { | |
2004 | vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, | |
2005 | VDEV_AUX_CORRUPT_DATA); | |
34dc7c2f | 2006 | nvlist_free(label); |
6cb8e530 PZ |
2007 | vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", |
2008 | ZPOOL_CONFIG_POOL_STATE); | |
2009 | return (0); | |
2010 | } | |
34dc7c2f | 2011 | |
6cb8e530 PZ |
2012 | nvlist_free(label); |
2013 | ||
2014 | /* | |
2015 | * If this is a verbatim import, no need to check the | |
2016 | * state of the pool. | |
2017 | */ | |
2018 | if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) && | |
2019 | spa_load_state(spa) == SPA_LOAD_OPEN && | |
2020 | state != POOL_STATE_ACTIVE) { | |
2021 | vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) " | |
2022 | "for spa %s", (u_longlong_t)state, spa->spa_name); | |
2023 | return (SET_ERROR(EBADF)); | |
2024 | } | |
2025 | ||
2026 | /* | |
2027 | * If we were able to open and validate a vdev that was | |
2028 | * previously marked permanently unavailable, clear that state | |
2029 | * now. | |
2030 | */ | |
2031 | if (vd->vdev_not_present) | |
2032 | vd->vdev_not_present = 0; | |
2033 | ||
2034 | return (0); | |
2035 | } | |
2036 | ||
2037 | static void | |
2038 | vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd) | |
2039 | { | |
2040 | if (svd->vdev_path != NULL && dvd->vdev_path != NULL) { | |
2041 | if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) { | |
2042 | zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed " | |
2043 | "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid, | |
2044 | dvd->vdev_path, svd->vdev_path); | |
2045 | spa_strfree(dvd->vdev_path); | |
2046 | dvd->vdev_path = spa_strdup(svd->vdev_path); | |
4a0ee12a | 2047 | } |
6cb8e530 PZ |
2048 | } else if (svd->vdev_path != NULL) { |
2049 | dvd->vdev_path = spa_strdup(svd->vdev_path); | |
2050 | zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'", | |
2051 | (u_longlong_t)dvd->vdev_guid, dvd->vdev_path); | |
2052 | } | |
2053 | } | |
34dc7c2f | 2054 | |
6cb8e530 PZ |
2055 | /* |
2056 | * Recursively copy vdev paths from one vdev to another. Source and destination | |
2057 | * vdev trees must have same geometry otherwise return error. Intended to copy | |
2058 | * paths from userland config into MOS config. | |
2059 | */ | |
2060 | int | |
2061 | vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd) | |
2062 | { | |
2063 | if ((svd->vdev_ops == &vdev_missing_ops) || | |
2064 | (svd->vdev_ishole && dvd->vdev_ishole) || | |
2065 | (dvd->vdev_ops == &vdev_indirect_ops)) | |
2066 | return (0); | |
2067 | ||
2068 | if (svd->vdev_ops != dvd->vdev_ops) { | |
2069 | vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s", | |
2070 | svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type); | |
2071 | return (SET_ERROR(EINVAL)); | |
2072 | } | |
2073 | ||
2074 | if (svd->vdev_guid != dvd->vdev_guid) { | |
2075 | vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != " | |
2076 | "%llu)", (u_longlong_t)svd->vdev_guid, | |
2077 | (u_longlong_t)dvd->vdev_guid); | |
2078 | return (SET_ERROR(EINVAL)); | |
b128c09f | 2079 | } |
34dc7c2f | 2080 | |
6cb8e530 PZ |
2081 | if (svd->vdev_children != dvd->vdev_children) { |
2082 | vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: " | |
2083 | "%llu != %llu", (u_longlong_t)svd->vdev_children, | |
2084 | (u_longlong_t)dvd->vdev_children); | |
2085 | return (SET_ERROR(EINVAL)); | |
2086 | } | |
2087 | ||
2088 | for (uint64_t i = 0; i < svd->vdev_children; i++) { | |
2089 | int error = vdev_copy_path_strict(svd->vdev_child[i], | |
2090 | dvd->vdev_child[i]); | |
2091 | if (error != 0) | |
2092 | return (error); | |
2093 | } | |
2094 | ||
2095 | if (svd->vdev_ops->vdev_op_leaf) | |
2096 | vdev_copy_path_impl(svd, dvd); | |
2097 | ||
34dc7c2f BB |
2098 | return (0); |
2099 | } | |
2100 | ||
6cb8e530 PZ |
2101 | static void |
2102 | vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd) | |
2103 | { | |
2104 | ASSERT(stvd->vdev_top == stvd); | |
2105 | ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id); | |
2106 | ||
2107 | for (uint64_t i = 0; i < dvd->vdev_children; i++) { | |
2108 | vdev_copy_path_search(stvd, dvd->vdev_child[i]); | |
2109 | } | |
2110 | ||
2111 | if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd)) | |
2112 | return; | |
2113 | ||
2114 | /* | |
2115 | * The idea here is that while a vdev can shift positions within | |
2116 | * a top vdev (when replacing, attaching mirror, etc.) it cannot | |
2117 | * step outside of it. | |
2118 | */ | |
2119 | vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid); | |
2120 | ||
2121 | if (vd == NULL || vd->vdev_ops != dvd->vdev_ops) | |
2122 | return; | |
2123 | ||
2124 | ASSERT(vd->vdev_ops->vdev_op_leaf); | |
2125 | ||
2126 | vdev_copy_path_impl(vd, dvd); | |
2127 | } | |
2128 | ||
2129 | /* | |
2130 | * Recursively copy vdev paths from one root vdev to another. Source and | |
2131 | * destination vdev trees may differ in geometry. For each destination leaf | |
2132 | * vdev, search a vdev with the same guid and top vdev id in the source. | |
2133 | * Intended to copy paths from userland config into MOS config. | |
2134 | */ | |
2135 | void | |
2136 | vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd) | |
2137 | { | |
2138 | uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children); | |
2139 | ASSERT(srvd->vdev_ops == &vdev_root_ops); | |
2140 | ASSERT(drvd->vdev_ops == &vdev_root_ops); | |
2141 | ||
2142 | for (uint64_t i = 0; i < children; i++) { | |
2143 | vdev_copy_path_search(srvd->vdev_child[i], | |
2144 | drvd->vdev_child[i]); | |
2145 | } | |
2146 | } | |
2147 | ||
34dc7c2f BB |
2148 | /* |
2149 | * Close a virtual device. | |
2150 | */ | |
2151 | void | |
2152 | vdev_close(vdev_t *vd) | |
2153 | { | |
428870ff | 2154 | vdev_t *pvd = vd->vdev_parent; |
1fde1e37 | 2155 | ASSERTV(spa_t *spa = vd->vdev_spa); |
fb5f0bc8 BB |
2156 | |
2157 | ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); | |
2158 | ||
428870ff BB |
2159 | /* |
2160 | * If our parent is reopening, then we are as well, unless we are | |
2161 | * going offline. | |
2162 | */ | |
2163 | if (pvd != NULL && pvd->vdev_reopening) | |
2164 | vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline); | |
2165 | ||
34dc7c2f BB |
2166 | vd->vdev_ops->vdev_op_close(vd); |
2167 | ||
2168 | vdev_cache_purge(vd); | |
2169 | ||
2170 | /* | |
9babb374 | 2171 | * We record the previous state before we close it, so that if we are |
34dc7c2f BB |
2172 | * doing a reopen(), we don't generate FMA ereports if we notice that |
2173 | * it's still faulted. | |
2174 | */ | |
2175 | vd->vdev_prevstate = vd->vdev_state; | |
2176 | ||
2177 | if (vd->vdev_offline) | |
2178 | vd->vdev_state = VDEV_STATE_OFFLINE; | |
2179 | else | |
2180 | vd->vdev_state = VDEV_STATE_CLOSED; | |
2181 | vd->vdev_stat.vs_aux = VDEV_AUX_NONE; | |
2182 | } | |
2183 | ||
428870ff BB |
2184 | void |
2185 | vdev_hold(vdev_t *vd) | |
2186 | { | |
2187 | spa_t *spa = vd->vdev_spa; | |
2188 | ||
2189 | ASSERT(spa_is_root(spa)); | |
2190 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) | |
2191 | return; | |
2192 | ||
1c27024e | 2193 | for (int c = 0; c < vd->vdev_children; c++) |
428870ff BB |
2194 | vdev_hold(vd->vdev_child[c]); |
2195 | ||
2196 | if (vd->vdev_ops->vdev_op_leaf) | |
2197 | vd->vdev_ops->vdev_op_hold(vd); | |
2198 | } | |
2199 | ||
2200 | void | |
2201 | vdev_rele(vdev_t *vd) | |
2202 | { | |
d6320ddb | 2203 | ASSERT(spa_is_root(vd->vdev_spa)); |
1c27024e | 2204 | for (int c = 0; c < vd->vdev_children; c++) |
428870ff BB |
2205 | vdev_rele(vd->vdev_child[c]); |
2206 | ||
2207 | if (vd->vdev_ops->vdev_op_leaf) | |
2208 | vd->vdev_ops->vdev_op_rele(vd); | |
2209 | } | |
2210 | ||
2211 | /* | |
2212 | * Reopen all interior vdevs and any unopened leaves. We don't actually | |
2213 | * reopen leaf vdevs which had previously been opened as they might deadlock | |
2214 | * on the spa_config_lock. Instead we only obtain the leaf's physical size. | |
2215 | * If the leaf has never been opened then open it, as usual. | |
2216 | */ | |
34dc7c2f BB |
2217 | void |
2218 | vdev_reopen(vdev_t *vd) | |
2219 | { | |
2220 | spa_t *spa = vd->vdev_spa; | |
2221 | ||
b128c09f | 2222 | ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); |
34dc7c2f | 2223 | |
428870ff BB |
2224 | /* set the reopening flag unless we're taking the vdev offline */ |
2225 | vd->vdev_reopening = !vd->vdev_offline; | |
34dc7c2f BB |
2226 | vdev_close(vd); |
2227 | (void) vdev_open(vd); | |
2228 | ||
2229 | /* | |
2230 | * Call vdev_validate() here to make sure we have the same device. | |
2231 | * Otherwise, a device with an invalid label could be successfully | |
2232 | * opened in response to vdev_reopen(). | |
2233 | */ | |
b128c09f BB |
2234 | if (vd->vdev_aux) { |
2235 | (void) vdev_validate_aux(vd); | |
2236 | if (vdev_readable(vd) && vdev_writeable(vd) && | |
9babb374 BB |
2237 | vd->vdev_aux == &spa->spa_l2cache && |
2238 | !l2arc_vdev_present(vd)) | |
2239 | l2arc_add_vdev(spa, vd); | |
b128c09f | 2240 | } else { |
6cb8e530 | 2241 | (void) vdev_validate(vd); |
b128c09f | 2242 | } |
34dc7c2f BB |
2243 | |
2244 | /* | |
2245 | * Reassess parent vdev's health. | |
2246 | */ | |
2247 | vdev_propagate_state(vd); | |
2248 | } | |
2249 | ||
2250 | int | |
2251 | vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) | |
2252 | { | |
2253 | int error; | |
2254 | ||
2255 | /* | |
2256 | * Normally, partial opens (e.g. of a mirror) are allowed. | |
2257 | * For a create, however, we want to fail the request if | |
2258 | * there are any components we can't open. | |
2259 | */ | |
2260 | error = vdev_open(vd); | |
2261 | ||
2262 | if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { | |
2263 | vdev_close(vd); | |
2264 | return (error ? error : ENXIO); | |
2265 | } | |
2266 | ||
2267 | /* | |
93cf2076 | 2268 | * Recursively load DTLs and initialize all labels. |
34dc7c2f | 2269 | */ |
93cf2076 GW |
2270 | if ((error = vdev_dtl_load(vd)) != 0 || |
2271 | (error = vdev_label_init(vd, txg, isreplacing ? | |
34dc7c2f BB |
2272 | VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { |
2273 | vdev_close(vd); | |
2274 | return (error); | |
2275 | } | |
2276 | ||
2277 | return (0); | |
2278 | } | |
2279 | ||
34dc7c2f | 2280 | void |
9babb374 | 2281 | vdev_metaslab_set_size(vdev_t *vd) |
34dc7c2f | 2282 | { |
d2734cce | 2283 | uint64_t asize = vd->vdev_asize; |
c853f382 | 2284 | uint64_t ms_count = asize >> zfs_vdev_default_ms_shift; |
e4e94ca3 | 2285 | uint64_t ms_shift; |
d2734cce | 2286 | |
34dc7c2f | 2287 | /* |
e4e94ca3 DB |
2288 | * There are two dimensions to the metaslab sizing calculation: |
2289 | * the size of the metaslab and the count of metaslabs per vdev. | |
e4e94ca3 | 2290 | * |
c853f382 SD |
2291 | * The default values used below are a good balance between memory |
2292 | * usage (larger metaslab size means more memory needed for loaded | |
2293 | * metaslabs; more metaslabs means more memory needed for the | |
2294 | * metaslab_t structs), metaslab load time (larger metaslabs take | |
2295 | * longer to load), and metaslab sync time (more metaslabs means | |
2296 | * more time spent syncing all of them). | |
2297 | * | |
2298 | * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs. | |
2299 | * The range of the dimensions are as follows: | |
2300 | * | |
2301 | * 2^29 <= ms_size <= 2^34 | |
e4e94ca3 DB |
2302 | * 16 <= ms_count <= 131,072 |
2303 | * | |
2304 | * On the lower end of vdev sizes, we aim for metaslabs sizes of | |
2305 | * at least 512MB (2^29) to minimize fragmentation effects when | |
2306 | * testing with smaller devices. However, the count constraint | |
2307 | * of at least 16 metaslabs will override this minimum size goal. | |
2308 | * | |
2309 | * On the upper end of vdev sizes, we aim for a maximum metaslab | |
c853f382 SD |
2310 | * size of 16GB. However, we will cap the total count to 2^17 |
2311 | * metaslabs to keep our memory footprint in check and let the | |
2312 | * metaslab size grow from there if that limit is hit. | |
e4e94ca3 DB |
2313 | * |
2314 | * The net effect of applying above constrains is summarized below. | |
2315 | * | |
c853f382 SD |
2316 | * vdev size metaslab count |
2317 | * --------------|----------------- | |
2318 | * < 8GB ~16 | |
2319 | * 8GB - 100GB one per 512MB | |
2320 | * 100GB - 3TB ~200 | |
2321 | * 3TB - 2PB one per 16GB | |
2322 | * > 2PB ~131,072 | |
2323 | * -------------------------------- | |
2324 | * | |
2325 | * Finally, note that all of the above calculate the initial | |
2326 | * number of metaslabs. Expanding a top-level vdev will result | |
2327 | * in additional metaslabs being allocated making it possible | |
2328 | * to exceed the zfs_vdev_ms_count_limit. | |
34dc7c2f | 2329 | */ |
d2734cce | 2330 | |
c853f382 SD |
2331 | if (ms_count < zfs_vdev_min_ms_count) |
2332 | ms_shift = highbit64(asize / zfs_vdev_min_ms_count); | |
2333 | else if (ms_count > zfs_vdev_default_ms_count) | |
2334 | ms_shift = highbit64(asize / zfs_vdev_default_ms_count); | |
e4e94ca3 | 2335 | else |
c853f382 | 2336 | ms_shift = zfs_vdev_default_ms_shift; |
e4e94ca3 DB |
2337 | |
2338 | if (ms_shift < SPA_MAXBLOCKSHIFT) { | |
2339 | ms_shift = SPA_MAXBLOCKSHIFT; | |
c853f382 SD |
2340 | } else if (ms_shift > zfs_vdev_max_ms_shift) { |
2341 | ms_shift = zfs_vdev_max_ms_shift; | |
e4e94ca3 | 2342 | /* cap the total count to constrain memory footprint */ |
c853f382 SD |
2343 | if ((asize >> ms_shift) > zfs_vdev_ms_count_limit) |
2344 | ms_shift = highbit64(asize / zfs_vdev_ms_count_limit); | |
d2734cce SD |
2345 | } |
2346 | ||
2347 | vd->vdev_ms_shift = ms_shift; | |
2348 | ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT); | |
34dc7c2f BB |
2349 | } |
2350 | ||
2351 | void | |
2352 | vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) | |
2353 | { | |
2354 | ASSERT(vd == vd->vdev_top); | |
a1d477c2 MA |
2355 | /* indirect vdevs don't have metaslabs or dtls */ |
2356 | ASSERT(vdev_is_concrete(vd) || flags == 0); | |
34dc7c2f | 2357 | ASSERT(ISP2(flags)); |
572e2857 | 2358 | ASSERT(spa_writeable(vd->vdev_spa)); |
34dc7c2f BB |
2359 | |
2360 | if (flags & VDD_METASLAB) | |
2361 | (void) txg_list_add(&vd->vdev_ms_list, arg, txg); | |
2362 | ||
2363 | if (flags & VDD_DTL) | |
2364 | (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); | |
2365 | ||
2366 | (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); | |
2367 | } | |
2368 | ||
93cf2076 GW |
2369 | void |
2370 | vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg) | |
2371 | { | |
1c27024e | 2372 | for (int c = 0; c < vd->vdev_children; c++) |
93cf2076 GW |
2373 | vdev_dirty_leaves(vd->vdev_child[c], flags, txg); |
2374 | ||
2375 | if (vd->vdev_ops->vdev_op_leaf) | |
2376 | vdev_dirty(vd->vdev_top, flags, vd, txg); | |
2377 | } | |
2378 | ||
fb5f0bc8 BB |
2379 | /* |
2380 | * DTLs. | |
2381 | * | |
2382 | * A vdev's DTL (dirty time log) is the set of transaction groups for which | |
428870ff | 2383 | * the vdev has less than perfect replication. There are four kinds of DTL: |
fb5f0bc8 BB |
2384 | * |
2385 | * DTL_MISSING: txgs for which the vdev has no valid copies of the data | |
2386 | * | |
2387 | * DTL_PARTIAL: txgs for which data is available, but not fully replicated | |
2388 | * | |
2389 | * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon | |
2390 | * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of | |
2391 | * txgs that was scrubbed. | |
2392 | * | |
2393 | * DTL_OUTAGE: txgs which cannot currently be read, whether due to | |
2394 | * persistent errors or just some device being offline. | |
2395 | * Unlike the other three, the DTL_OUTAGE map is not generally | |
2396 | * maintained; it's only computed when needed, typically to | |
2397 | * determine whether a device can be detached. | |
2398 | * | |
2399 | * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device | |
2400 | * either has the data or it doesn't. | |
2401 | * | |
2402 | * For interior vdevs such as mirror and RAID-Z the picture is more complex. | |
2403 | * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because | |
2404 | * if any child is less than fully replicated, then so is its parent. | |
2405 | * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, | |
2406 | * comprising only those txgs which appear in 'maxfaults' or more children; | |
2407 | * those are the txgs we don't have enough replication to read. For example, | |
2408 | * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); | |
2409 | * thus, its DTL_MISSING consists of the set of txgs that appear in more than | |
2410 | * two child DTL_MISSING maps. | |
2411 | * | |
2412 | * It should be clear from the above that to compute the DTLs and outage maps | |
2413 | * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. | |
2414 | * Therefore, that is all we keep on disk. When loading the pool, or after | |
2415 | * a configuration change, we generate all other DTLs from first principles. | |
2416 | */ | |
34dc7c2f | 2417 | void |
fb5f0bc8 | 2418 | vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) |
34dc7c2f | 2419 | { |
93cf2076 | 2420 | range_tree_t *rt = vd->vdev_dtl[t]; |
fb5f0bc8 BB |
2421 | |
2422 | ASSERT(t < DTL_TYPES); | |
2423 | ASSERT(vd != vd->vdev_spa->spa_root_vdev); | |
572e2857 | 2424 | ASSERT(spa_writeable(vd->vdev_spa)); |
fb5f0bc8 | 2425 | |
a1d477c2 | 2426 | mutex_enter(&vd->vdev_dtl_lock); |
93cf2076 GW |
2427 | if (!range_tree_contains(rt, txg, size)) |
2428 | range_tree_add(rt, txg, size); | |
a1d477c2 | 2429 | mutex_exit(&vd->vdev_dtl_lock); |
34dc7c2f BB |
2430 | } |
2431 | ||
fb5f0bc8 BB |
2432 | boolean_t |
2433 | vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) | |
34dc7c2f | 2434 | { |
93cf2076 | 2435 | range_tree_t *rt = vd->vdev_dtl[t]; |
fb5f0bc8 | 2436 | boolean_t dirty = B_FALSE; |
34dc7c2f | 2437 | |
fb5f0bc8 BB |
2438 | ASSERT(t < DTL_TYPES); |
2439 | ASSERT(vd != vd->vdev_spa->spa_root_vdev); | |
34dc7c2f | 2440 | |
a1d477c2 MA |
2441 | /* |
2442 | * While we are loading the pool, the DTLs have not been loaded yet. | |
2443 | * Ignore the DTLs and try all devices. This avoids a recursive | |
2444 | * mutex enter on the vdev_dtl_lock, and also makes us try hard | |
2445 | * when loading the pool (relying on the checksum to ensure that | |
2446 | * we get the right data -- note that we while loading, we are | |
2447 | * only reading the MOS, which is always checksummed). | |
2448 | */ | |
2449 | if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE) | |
2450 | return (B_FALSE); | |
2451 | ||
2452 | mutex_enter(&vd->vdev_dtl_lock); | |
d2734cce | 2453 | if (!range_tree_is_empty(rt)) |
93cf2076 | 2454 | dirty = range_tree_contains(rt, txg, size); |
a1d477c2 | 2455 | mutex_exit(&vd->vdev_dtl_lock); |
34dc7c2f BB |
2456 | |
2457 | return (dirty); | |
2458 | } | |
2459 | ||
fb5f0bc8 BB |
2460 | boolean_t |
2461 | vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) | |
2462 | { | |
93cf2076 | 2463 | range_tree_t *rt = vd->vdev_dtl[t]; |
fb5f0bc8 BB |
2464 | boolean_t empty; |
2465 | ||
a1d477c2 | 2466 | mutex_enter(&vd->vdev_dtl_lock); |
d2734cce | 2467 | empty = range_tree_is_empty(rt); |
a1d477c2 | 2468 | mutex_exit(&vd->vdev_dtl_lock); |
fb5f0bc8 BB |
2469 | |
2470 | return (empty); | |
2471 | } | |
2472 | ||
3d6da72d IH |
2473 | /* |
2474 | * Returns B_TRUE if vdev determines offset needs to be resilvered. | |
2475 | */ | |
2476 | boolean_t | |
2477 | vdev_dtl_need_resilver(vdev_t *vd, uint64_t offset, size_t psize) | |
2478 | { | |
2479 | ASSERT(vd != vd->vdev_spa->spa_root_vdev); | |
2480 | ||
2481 | if (vd->vdev_ops->vdev_op_need_resilver == NULL || | |
2482 | vd->vdev_ops->vdev_op_leaf) | |
2483 | return (B_TRUE); | |
2484 | ||
2485 | return (vd->vdev_ops->vdev_op_need_resilver(vd, offset, psize)); | |
2486 | } | |
2487 | ||
5d1f7fb6 GW |
2488 | /* |
2489 | * Returns the lowest txg in the DTL range. | |
2490 | */ | |
2491 | static uint64_t | |
2492 | vdev_dtl_min(vdev_t *vd) | |
2493 | { | |
93cf2076 | 2494 | range_seg_t *rs; |
5d1f7fb6 GW |
2495 | |
2496 | ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); | |
93cf2076 | 2497 | ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); |
5d1f7fb6 GW |
2498 | ASSERT0(vd->vdev_children); |
2499 | ||
93cf2076 GW |
2500 | rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root); |
2501 | return (rs->rs_start - 1); | |
5d1f7fb6 GW |
2502 | } |
2503 | ||
2504 | /* | |
2505 | * Returns the highest txg in the DTL. | |
2506 | */ | |
2507 | static uint64_t | |
2508 | vdev_dtl_max(vdev_t *vd) | |
2509 | { | |
93cf2076 | 2510 | range_seg_t *rs; |
5d1f7fb6 GW |
2511 | |
2512 | ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); | |
93cf2076 | 2513 | ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); |
5d1f7fb6 GW |
2514 | ASSERT0(vd->vdev_children); |
2515 | ||
93cf2076 GW |
2516 | rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root); |
2517 | return (rs->rs_end); | |
5d1f7fb6 GW |
2518 | } |
2519 | ||
2520 | /* | |
2521 | * Determine if a resilvering vdev should remove any DTL entries from | |
2522 | * its range. If the vdev was resilvering for the entire duration of the | |
2523 | * scan then it should excise that range from its DTLs. Otherwise, this | |
2524 | * vdev is considered partially resilvered and should leave its DTL | |
2525 | * entries intact. The comment in vdev_dtl_reassess() describes how we | |
2526 | * excise the DTLs. | |
2527 | */ | |
2528 | static boolean_t | |
2529 | vdev_dtl_should_excise(vdev_t *vd) | |
2530 | { | |
2531 | spa_t *spa = vd->vdev_spa; | |
2532 | dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; | |
2533 | ||
2534 | ASSERT0(scn->scn_phys.scn_errors); | |
2535 | ASSERT0(vd->vdev_children); | |
2536 | ||
335b251a MA |
2537 | if (vd->vdev_state < VDEV_STATE_DEGRADED) |
2538 | return (B_FALSE); | |
2539 | ||
80a91e74 TC |
2540 | if (vd->vdev_resilver_deferred) |
2541 | return (B_FALSE); | |
2542 | ||
5d1f7fb6 | 2543 | if (vd->vdev_resilver_txg == 0 || |
d2734cce | 2544 | range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) |
5d1f7fb6 GW |
2545 | return (B_TRUE); |
2546 | ||
2547 | /* | |
2548 | * When a resilver is initiated the scan will assign the scn_max_txg | |
2549 | * value to the highest txg value that exists in all DTLs. If this | |
2550 | * device's max DTL is not part of this scan (i.e. it is not in | |
2551 | * the range (scn_min_txg, scn_max_txg] then it is not eligible | |
2552 | * for excision. | |
2553 | */ | |
2554 | if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) { | |
2555 | ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd)); | |
2556 | ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg); | |
2557 | ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg); | |
2558 | return (B_TRUE); | |
2559 | } | |
2560 | return (B_FALSE); | |
2561 | } | |
2562 | ||
34dc7c2f | 2563 | /* |
fde25c0a TC |
2564 | * Reassess DTLs after a config change or scrub completion. If txg == 0 no |
2565 | * write operations will be issued to the pool. | |
34dc7c2f BB |
2566 | */ |
2567 | void | |
2568 | vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) | |
2569 | { | |
2570 | spa_t *spa = vd->vdev_spa; | |
fb5f0bc8 | 2571 | avl_tree_t reftree; |
1c27024e | 2572 | int minref; |
34dc7c2f | 2573 | |
fb5f0bc8 | 2574 | ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); |
34dc7c2f | 2575 | |
1c27024e | 2576 | for (int c = 0; c < vd->vdev_children; c++) |
fb5f0bc8 BB |
2577 | vdev_dtl_reassess(vd->vdev_child[c], txg, |
2578 | scrub_txg, scrub_done); | |
2579 | ||
a1d477c2 | 2580 | if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux) |
fb5f0bc8 BB |
2581 | return; |
2582 | ||
2583 | if (vd->vdev_ops->vdev_op_leaf) { | |
428870ff BB |
2584 | dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; |
2585 | ||
34dc7c2f | 2586 | mutex_enter(&vd->vdev_dtl_lock); |
5d1f7fb6 | 2587 | |
02638a30 TC |
2588 | /* |
2589 | * If requested, pretend the scan completed cleanly. | |
2590 | */ | |
2591 | if (zfs_scan_ignore_errors && scn) | |
2592 | scn->scn_phys.scn_errors = 0; | |
2593 | ||
5d1f7fb6 GW |
2594 | /* |
2595 | * If we've completed a scan cleanly then determine | |
2596 | * if this vdev should remove any DTLs. We only want to | |
2597 | * excise regions on vdevs that were available during | |
2598 | * the entire duration of this scan. | |
2599 | */ | |
b128c09f | 2600 | if (scrub_txg != 0 && |
428870ff | 2601 | (spa->spa_scrub_started || |
5d1f7fb6 GW |
2602 | (scn != NULL && scn->scn_phys.scn_errors == 0)) && |
2603 | vdev_dtl_should_excise(vd)) { | |
b128c09f BB |
2604 | /* |
2605 | * We completed a scrub up to scrub_txg. If we | |
2606 | * did it without rebooting, then the scrub dtl | |
2607 | * will be valid, so excise the old region and | |
2608 | * fold in the scrub dtl. Otherwise, leave the | |
2609 | * dtl as-is if there was an error. | |
fb5f0bc8 BB |
2610 | * |
2611 | * There's little trick here: to excise the beginning | |
2612 | * of the DTL_MISSING map, we put it into a reference | |
2613 | * tree and then add a segment with refcnt -1 that | |
2614 | * covers the range [0, scrub_txg). This means | |
2615 | * that each txg in that range has refcnt -1 or 0. | |
2616 | * We then add DTL_SCRUB with a refcnt of 2, so that | |
2617 | * entries in the range [0, scrub_txg) will have a | |
2618 | * positive refcnt -- either 1 or 2. We then convert | |
2619 | * the reference tree into the new DTL_MISSING map. | |
b128c09f | 2620 | */ |
93cf2076 GW |
2621 | space_reftree_create(&reftree); |
2622 | space_reftree_add_map(&reftree, | |
2623 | vd->vdev_dtl[DTL_MISSING], 1); | |
2624 | space_reftree_add_seg(&reftree, 0, scrub_txg, -1); | |
2625 | space_reftree_add_map(&reftree, | |
2626 | vd->vdev_dtl[DTL_SCRUB], 2); | |
2627 | space_reftree_generate_map(&reftree, | |
2628 | vd->vdev_dtl[DTL_MISSING], 1); | |
2629 | space_reftree_destroy(&reftree); | |
34dc7c2f | 2630 | } |
93cf2076 GW |
2631 | range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); |
2632 | range_tree_walk(vd->vdev_dtl[DTL_MISSING], | |
2633 | range_tree_add, vd->vdev_dtl[DTL_PARTIAL]); | |
34dc7c2f | 2634 | if (scrub_done) |
93cf2076 GW |
2635 | range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL); |
2636 | range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); | |
fb5f0bc8 | 2637 | if (!vdev_readable(vd)) |
93cf2076 | 2638 | range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); |
fb5f0bc8 | 2639 | else |
93cf2076 GW |
2640 | range_tree_walk(vd->vdev_dtl[DTL_MISSING], |
2641 | range_tree_add, vd->vdev_dtl[DTL_OUTAGE]); | |
5d1f7fb6 GW |
2642 | |
2643 | /* | |
2644 | * If the vdev was resilvering and no longer has any | |
d14fa5db | 2645 | * DTLs then reset its resilvering flag and dirty |
2646 | * the top level so that we persist the change. | |
5d1f7fb6 | 2647 | */ |
fde25c0a | 2648 | if (txg != 0 && vd->vdev_resilver_txg != 0 && |
d2734cce SD |
2649 | range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && |
2650 | range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) { | |
5d1f7fb6 | 2651 | vd->vdev_resilver_txg = 0; |
d14fa5db | 2652 | vdev_config_dirty(vd->vdev_top); |
2653 | } | |
5d1f7fb6 | 2654 | |
34dc7c2f | 2655 | mutex_exit(&vd->vdev_dtl_lock); |
b128c09f | 2656 | |
34dc7c2f BB |
2657 | if (txg != 0) |
2658 | vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); | |
2659 | return; | |
2660 | } | |
2661 | ||
34dc7c2f | 2662 | mutex_enter(&vd->vdev_dtl_lock); |
1c27024e | 2663 | for (int t = 0; t < DTL_TYPES; t++) { |
428870ff BB |
2664 | /* account for child's outage in parent's missing map */ |
2665 | int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; | |
fb5f0bc8 BB |
2666 | if (t == DTL_SCRUB) |
2667 | continue; /* leaf vdevs only */ | |
2668 | if (t == DTL_PARTIAL) | |
2669 | minref = 1; /* i.e. non-zero */ | |
2670 | else if (vd->vdev_nparity != 0) | |
2671 | minref = vd->vdev_nparity + 1; /* RAID-Z */ | |
2672 | else | |
2673 | minref = vd->vdev_children; /* any kind of mirror */ | |
93cf2076 | 2674 | space_reftree_create(&reftree); |
1c27024e | 2675 | for (int c = 0; c < vd->vdev_children; c++) { |
fb5f0bc8 BB |
2676 | vdev_t *cvd = vd->vdev_child[c]; |
2677 | mutex_enter(&cvd->vdev_dtl_lock); | |
93cf2076 | 2678 | space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1); |
fb5f0bc8 BB |
2679 | mutex_exit(&cvd->vdev_dtl_lock); |
2680 | } | |
93cf2076 GW |
2681 | space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref); |
2682 | space_reftree_destroy(&reftree); | |
34dc7c2f | 2683 | } |
fb5f0bc8 | 2684 | mutex_exit(&vd->vdev_dtl_lock); |
34dc7c2f BB |
2685 | } |
2686 | ||
93cf2076 | 2687 | int |
34dc7c2f BB |
2688 | vdev_dtl_load(vdev_t *vd) |
2689 | { | |
2690 | spa_t *spa = vd->vdev_spa; | |
34dc7c2f | 2691 | objset_t *mos = spa->spa_meta_objset; |
93cf2076 | 2692 | int error = 0; |
34dc7c2f | 2693 | |
93cf2076 | 2694 | if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) { |
a1d477c2 | 2695 | ASSERT(vdev_is_concrete(vd)); |
34dc7c2f | 2696 | |
93cf2076 | 2697 | error = space_map_open(&vd->vdev_dtl_sm, mos, |
a1d477c2 | 2698 | vd->vdev_dtl_object, 0, -1ULL, 0); |
93cf2076 GW |
2699 | if (error) |
2700 | return (error); | |
2701 | ASSERT(vd->vdev_dtl_sm != NULL); | |
34dc7c2f | 2702 | |
93cf2076 | 2703 | mutex_enter(&vd->vdev_dtl_lock); |
428870ff | 2704 | |
93cf2076 GW |
2705 | /* |
2706 | * Now that we've opened the space_map we need to update | |
2707 | * the in-core DTL. | |
2708 | */ | |
2709 | space_map_update(vd->vdev_dtl_sm); | |
34dc7c2f | 2710 | |
93cf2076 GW |
2711 | error = space_map_load(vd->vdev_dtl_sm, |
2712 | vd->vdev_dtl[DTL_MISSING], SM_ALLOC); | |
2713 | mutex_exit(&vd->vdev_dtl_lock); | |
34dc7c2f | 2714 | |
93cf2076 GW |
2715 | return (error); |
2716 | } | |
2717 | ||
1c27024e | 2718 | for (int c = 0; c < vd->vdev_children; c++) { |
93cf2076 GW |
2719 | error = vdev_dtl_load(vd->vdev_child[c]); |
2720 | if (error != 0) | |
2721 | break; | |
2722 | } | |
34dc7c2f BB |
2723 | |
2724 | return (error); | |
2725 | } | |
2726 | ||
cc99f275 DB |
2727 | static void |
2728 | vdev_zap_allocation_data(vdev_t *vd, dmu_tx_t *tx) | |
2729 | { | |
2730 | spa_t *spa = vd->vdev_spa; | |
2731 | objset_t *mos = spa->spa_meta_objset; | |
2732 | vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias; | |
2733 | const char *string; | |
2734 | ||
2735 | ASSERT(alloc_bias != VDEV_BIAS_NONE); | |
2736 | ||
2737 | string = | |
2738 | (alloc_bias == VDEV_BIAS_LOG) ? VDEV_ALLOC_BIAS_LOG : | |
2739 | (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL : | |
2740 | (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : NULL; | |
2741 | ||
2742 | ASSERT(string != NULL); | |
2743 | VERIFY0(zap_add(mos, vd->vdev_top_zap, VDEV_TOP_ZAP_ALLOCATION_BIAS, | |
2744 | 1, strlen(string) + 1, string, tx)); | |
2745 | ||
2746 | if (alloc_bias == VDEV_BIAS_SPECIAL || alloc_bias == VDEV_BIAS_DEDUP) { | |
2747 | spa_activate_allocation_classes(spa, tx); | |
2748 | } | |
2749 | } | |
2750 | ||
e0ab3ab5 JS |
2751 | void |
2752 | vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx) | |
2753 | { | |
2754 | spa_t *spa = vd->vdev_spa; | |
2755 | ||
2756 | VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx)); | |
2757 | VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, | |
2758 | zapobj, tx)); | |
2759 | } | |
2760 | ||
2761 | uint64_t | |
2762 | vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx) | |
2763 | { | |
2764 | spa_t *spa = vd->vdev_spa; | |
2765 | uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA, | |
2766 | DMU_OT_NONE, 0, tx); | |
2767 | ||
2768 | ASSERT(zap != 0); | |
2769 | VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, | |
2770 | zap, tx)); | |
2771 | ||
2772 | return (zap); | |
2773 | } | |
2774 | ||
2775 | void | |
2776 | vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx) | |
2777 | { | |
e0ab3ab5 JS |
2778 | if (vd->vdev_ops != &vdev_hole_ops && |
2779 | vd->vdev_ops != &vdev_missing_ops && | |
2780 | vd->vdev_ops != &vdev_root_ops && | |
2781 | !vd->vdev_top->vdev_removing) { | |
2782 | if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) { | |
2783 | vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx); | |
2784 | } | |
2785 | if (vd == vd->vdev_top && vd->vdev_top_zap == 0) { | |
2786 | vd->vdev_top_zap = vdev_create_link_zap(vd, tx); | |
cc99f275 DB |
2787 | if (vd->vdev_alloc_bias != VDEV_BIAS_NONE) |
2788 | vdev_zap_allocation_data(vd, tx); | |
e0ab3ab5 JS |
2789 | } |
2790 | } | |
cc99f275 | 2791 | |
1c27024e | 2792 | for (uint64_t i = 0; i < vd->vdev_children; i++) { |
e0ab3ab5 JS |
2793 | vdev_construct_zaps(vd->vdev_child[i], tx); |
2794 | } | |
2795 | } | |
2796 | ||
34dc7c2f BB |
2797 | void |
2798 | vdev_dtl_sync(vdev_t *vd, uint64_t txg) | |
2799 | { | |
2800 | spa_t *spa = vd->vdev_spa; | |
93cf2076 | 2801 | range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; |
34dc7c2f | 2802 | objset_t *mos = spa->spa_meta_objset; |
93cf2076 | 2803 | range_tree_t *rtsync; |
34dc7c2f | 2804 | dmu_tx_t *tx; |
93cf2076 | 2805 | uint64_t object = space_map_object(vd->vdev_dtl_sm); |
34dc7c2f | 2806 | |
a1d477c2 | 2807 | ASSERT(vdev_is_concrete(vd)); |
93cf2076 | 2808 | ASSERT(vd->vdev_ops->vdev_op_leaf); |
428870ff | 2809 | |
34dc7c2f BB |
2810 | tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); |
2811 | ||
93cf2076 GW |
2812 | if (vd->vdev_detached || vd->vdev_top->vdev_removing) { |
2813 | mutex_enter(&vd->vdev_dtl_lock); | |
2814 | space_map_free(vd->vdev_dtl_sm, tx); | |
2815 | space_map_close(vd->vdev_dtl_sm); | |
2816 | vd->vdev_dtl_sm = NULL; | |
2817 | mutex_exit(&vd->vdev_dtl_lock); | |
e0ab3ab5 JS |
2818 | |
2819 | /* | |
2820 | * We only destroy the leaf ZAP for detached leaves or for | |
2821 | * removed log devices. Removed data devices handle leaf ZAP | |
2822 | * cleanup later, once cancellation is no longer possible. | |
2823 | */ | |
2824 | if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached || | |
2825 | vd->vdev_top->vdev_islog)) { | |
2826 | vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx); | |
2827 | vd->vdev_leaf_zap = 0; | |
2828 | } | |
2829 | ||
34dc7c2f | 2830 | dmu_tx_commit(tx); |
34dc7c2f BB |
2831 | return; |
2832 | } | |
2833 | ||
93cf2076 GW |
2834 | if (vd->vdev_dtl_sm == NULL) { |
2835 | uint64_t new_object; | |
2836 | ||
d2734cce | 2837 | new_object = space_map_alloc(mos, vdev_dtl_sm_blksz, tx); |
93cf2076 GW |
2838 | VERIFY3U(new_object, !=, 0); |
2839 | ||
2840 | VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, | |
a1d477c2 | 2841 | 0, -1ULL, 0)); |
93cf2076 | 2842 | ASSERT(vd->vdev_dtl_sm != NULL); |
34dc7c2f BB |
2843 | } |
2844 | ||
a1d477c2 | 2845 | rtsync = range_tree_create(NULL, NULL); |
34dc7c2f BB |
2846 | |
2847 | mutex_enter(&vd->vdev_dtl_lock); | |
93cf2076 | 2848 | range_tree_walk(rt, range_tree_add, rtsync); |
34dc7c2f BB |
2849 | mutex_exit(&vd->vdev_dtl_lock); |
2850 | ||
d2734cce | 2851 | space_map_truncate(vd->vdev_dtl_sm, vdev_dtl_sm_blksz, tx); |
4d044c4c | 2852 | space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx); |
93cf2076 | 2853 | range_tree_vacate(rtsync, NULL, NULL); |
34dc7c2f | 2854 | |
93cf2076 | 2855 | range_tree_destroy(rtsync); |
34dc7c2f | 2856 | |
93cf2076 GW |
2857 | /* |
2858 | * If the object for the space map has changed then dirty | |
2859 | * the top level so that we update the config. | |
2860 | */ | |
2861 | if (object != space_map_object(vd->vdev_dtl_sm)) { | |
4a0ee12a PZ |
2862 | vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, " |
2863 | "new object %llu", (u_longlong_t)txg, spa_name(spa), | |
2864 | (u_longlong_t)object, | |
2865 | (u_longlong_t)space_map_object(vd->vdev_dtl_sm)); | |
93cf2076 GW |
2866 | vdev_config_dirty(vd->vdev_top); |
2867 | } | |
34dc7c2f BB |
2868 | |
2869 | dmu_tx_commit(tx); | |
93cf2076 GW |
2870 | |
2871 | mutex_enter(&vd->vdev_dtl_lock); | |
2872 | space_map_update(vd->vdev_dtl_sm); | |
2873 | mutex_exit(&vd->vdev_dtl_lock); | |
34dc7c2f BB |
2874 | } |
2875 | ||
fb5f0bc8 BB |
2876 | /* |
2877 | * Determine whether the specified vdev can be offlined/detached/removed | |
2878 | * without losing data. | |
2879 | */ | |
2880 | boolean_t | |
2881 | vdev_dtl_required(vdev_t *vd) | |
2882 | { | |
2883 | spa_t *spa = vd->vdev_spa; | |
2884 | vdev_t *tvd = vd->vdev_top; | |
2885 | uint8_t cant_read = vd->vdev_cant_read; | |
2886 | boolean_t required; | |
2887 | ||
2888 | ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); | |
2889 | ||
2890 | if (vd == spa->spa_root_vdev || vd == tvd) | |
2891 | return (B_TRUE); | |
2892 | ||
2893 | /* | |
2894 | * Temporarily mark the device as unreadable, and then determine | |
2895 | * whether this results in any DTL outages in the top-level vdev. | |
2896 | * If not, we can safely offline/detach/remove the device. | |
2897 | */ | |
2898 | vd->vdev_cant_read = B_TRUE; | |
2899 | vdev_dtl_reassess(tvd, 0, 0, B_FALSE); | |
2900 | required = !vdev_dtl_empty(tvd, DTL_OUTAGE); | |
2901 | vd->vdev_cant_read = cant_read; | |
2902 | vdev_dtl_reassess(tvd, 0, 0, B_FALSE); | |
2903 | ||
572e2857 BB |
2904 | if (!required && zio_injection_enabled) |
2905 | required = !!zio_handle_device_injection(vd, NULL, ECHILD); | |
2906 | ||
fb5f0bc8 BB |
2907 | return (required); |
2908 | } | |
2909 | ||
b128c09f BB |
2910 | /* |
2911 | * Determine if resilver is needed, and if so the txg range. | |
2912 | */ | |
2913 | boolean_t | |
2914 | vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) | |
2915 | { | |
2916 | boolean_t needed = B_FALSE; | |
2917 | uint64_t thismin = UINT64_MAX; | |
2918 | uint64_t thismax = 0; | |
2919 | ||
2920 | if (vd->vdev_children == 0) { | |
2921 | mutex_enter(&vd->vdev_dtl_lock); | |
d2734cce | 2922 | if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && |
fb5f0bc8 | 2923 | vdev_writeable(vd)) { |
b128c09f | 2924 | |
5d1f7fb6 GW |
2925 | thismin = vdev_dtl_min(vd); |
2926 | thismax = vdev_dtl_max(vd); | |
b128c09f BB |
2927 | needed = B_TRUE; |
2928 | } | |
2929 | mutex_exit(&vd->vdev_dtl_lock); | |
2930 | } else { | |
1c27024e | 2931 | for (int c = 0; c < vd->vdev_children; c++) { |
b128c09f BB |
2932 | vdev_t *cvd = vd->vdev_child[c]; |
2933 | uint64_t cmin, cmax; | |
2934 | ||
2935 | if (vdev_resilver_needed(cvd, &cmin, &cmax)) { | |
2936 | thismin = MIN(thismin, cmin); | |
2937 | thismax = MAX(thismax, cmax); | |
2938 | needed = B_TRUE; | |
2939 | } | |
2940 | } | |
2941 | } | |
2942 | ||
2943 | if (needed && minp) { | |
2944 | *minp = thismin; | |
2945 | *maxp = thismax; | |
2946 | } | |
2947 | return (needed); | |
2948 | } | |
2949 | ||
d2734cce | 2950 | /* |
27f80e85 BB |
2951 | * Gets the checkpoint space map object from the vdev's ZAP. On success sm_obj |
2952 | * will contain either the checkpoint spacemap object or zero if none exists. | |
2953 | * All other errors are returned to the caller. | |
d2734cce SD |
2954 | */ |
2955 | int | |
27f80e85 | 2956 | vdev_checkpoint_sm_object(vdev_t *vd, uint64_t *sm_obj) |
d2734cce SD |
2957 | { |
2958 | ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER)); | |
27f80e85 | 2959 | |
d2734cce | 2960 | if (vd->vdev_top_zap == 0) { |
27f80e85 | 2961 | *sm_obj = 0; |
d2734cce SD |
2962 | return (0); |
2963 | } | |
2964 | ||
27f80e85 BB |
2965 | int error = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap, |
2966 | VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, sm_obj); | |
2967 | if (error == ENOENT) { | |
2968 | *sm_obj = 0; | |
2969 | error = 0; | |
2970 | } | |
d2734cce | 2971 | |
27f80e85 | 2972 | return (error); |
d2734cce SD |
2973 | } |
2974 | ||
a1d477c2 | 2975 | int |
34dc7c2f BB |
2976 | vdev_load(vdev_t *vd) |
2977 | { | |
a1d477c2 MA |
2978 | int error = 0; |
2979 | ||
34dc7c2f BB |
2980 | /* |
2981 | * Recursively load all children. | |
2982 | */ | |
a1d477c2 MA |
2983 | for (int c = 0; c < vd->vdev_children; c++) { |
2984 | error = vdev_load(vd->vdev_child[c]); | |
2985 | if (error != 0) { | |
2986 | return (error); | |
2987 | } | |
2988 | } | |
2989 | ||
2990 | vdev_set_deflate_ratio(vd); | |
34dc7c2f | 2991 | |
cc99f275 DB |
2992 | /* |
2993 | * On spa_load path, grab the allocation bias from our zap | |
2994 | */ | |
2995 | if (vd == vd->vdev_top && vd->vdev_top_zap != 0) { | |
2996 | spa_t *spa = vd->vdev_spa; | |
2997 | char bias_str[64]; | |
2998 | ||
2999 | if (zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap, | |
3000 | VDEV_TOP_ZAP_ALLOCATION_BIAS, 1, sizeof (bias_str), | |
3001 | bias_str) == 0) { | |
3002 | ASSERT(vd->vdev_alloc_bias == VDEV_BIAS_NONE); | |
3003 | vd->vdev_alloc_bias = vdev_derive_alloc_bias(bias_str); | |
3004 | } | |
3005 | } | |
3006 | ||
34dc7c2f BB |
3007 | /* |
3008 | * If this is a top-level vdev, initialize its metaslabs. | |
3009 | */ | |
a1d477c2 | 3010 | if (vd == vd->vdev_top && vdev_is_concrete(vd)) { |
cc99f275 DB |
3011 | vdev_metaslab_group_create(vd); |
3012 | ||
a1d477c2 MA |
3013 | if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) { |
3014 | vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, | |
3015 | VDEV_AUX_CORRUPT_DATA); | |
4a0ee12a PZ |
3016 | vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, " |
3017 | "asize=%llu", (u_longlong_t)vd->vdev_ashift, | |
3018 | (u_longlong_t)vd->vdev_asize); | |
a1d477c2 MA |
3019 | return (SET_ERROR(ENXIO)); |
3020 | } else if ((error = vdev_metaslab_init(vd, 0)) != 0) { | |
4a0ee12a PZ |
3021 | vdev_dbgmsg(vd, "vdev_load: metaslab_init failed " |
3022 | "[error=%d]", error); | |
a1d477c2 MA |
3023 | vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, |
3024 | VDEV_AUX_CORRUPT_DATA); | |
3025 | return (error); | |
3026 | } | |
d2734cce | 3027 | |
27f80e85 BB |
3028 | uint64_t checkpoint_sm_obj; |
3029 | error = vdev_checkpoint_sm_object(vd, &checkpoint_sm_obj); | |
3030 | if (error == 0 && checkpoint_sm_obj != 0) { | |
d2734cce SD |
3031 | objset_t *mos = spa_meta_objset(vd->vdev_spa); |
3032 | ASSERT(vd->vdev_asize != 0); | |
3033 | ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL); | |
3034 | ||
3035 | if ((error = space_map_open(&vd->vdev_checkpoint_sm, | |
3036 | mos, checkpoint_sm_obj, 0, vd->vdev_asize, | |
3037 | vd->vdev_ashift))) { | |
3038 | vdev_dbgmsg(vd, "vdev_load: space_map_open " | |
3039 | "failed for checkpoint spacemap (obj %llu) " | |
3040 | "[error=%d]", | |
3041 | (u_longlong_t)checkpoint_sm_obj, error); | |
3042 | return (error); | |
3043 | } | |
3044 | ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL); | |
3045 | space_map_update(vd->vdev_checkpoint_sm); | |
3046 | ||
3047 | /* | |
3048 | * Since the checkpoint_sm contains free entries | |
3049 | * exclusively we can use sm_alloc to indicate the | |
27f80e85 | 3050 | * cumulative checkpointed space that has been freed. |
d2734cce SD |
3051 | */ |
3052 | vd->vdev_stat.vs_checkpoint_space = | |
3053 | -vd->vdev_checkpoint_sm->sm_alloc; | |
3054 | vd->vdev_spa->spa_checkpoint_info.sci_dspace += | |
3055 | vd->vdev_stat.vs_checkpoint_space; | |
27f80e85 BB |
3056 | } else if (error != 0) { |
3057 | vdev_dbgmsg(vd, "vdev_load: failed to retrieve " | |
3058 | "checkpoint space map object from vdev ZAP " | |
3059 | "[error=%d]", error); | |
3060 | return (error); | |
d2734cce | 3061 | } |
a1d477c2 MA |
3062 | } |
3063 | ||
34dc7c2f BB |
3064 | /* |
3065 | * If this is a leaf vdev, load its DTL. | |
3066 | */ | |
a1d477c2 | 3067 | if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) { |
34dc7c2f BB |
3068 | vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, |
3069 | VDEV_AUX_CORRUPT_DATA); | |
4a0ee12a PZ |
3070 | vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed " |
3071 | "[error=%d]", error); | |
a1d477c2 MA |
3072 | return (error); |
3073 | } | |
3074 | ||
27f80e85 BB |
3075 | uint64_t obsolete_sm_object; |
3076 | error = vdev_obsolete_sm_object(vd, &obsolete_sm_object); | |
3077 | if (error == 0 && obsolete_sm_object != 0) { | |
a1d477c2 MA |
3078 | objset_t *mos = vd->vdev_spa->spa_meta_objset; |
3079 | ASSERT(vd->vdev_asize != 0); | |
d2734cce | 3080 | ASSERT3P(vd->vdev_obsolete_sm, ==, NULL); |
a1d477c2 MA |
3081 | |
3082 | if ((error = space_map_open(&vd->vdev_obsolete_sm, mos, | |
3083 | obsolete_sm_object, 0, vd->vdev_asize, 0))) { | |
3084 | vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, | |
3085 | VDEV_AUX_CORRUPT_DATA); | |
4a0ee12a PZ |
3086 | vdev_dbgmsg(vd, "vdev_load: space_map_open failed for " |
3087 | "obsolete spacemap (obj %llu) [error=%d]", | |
3088 | (u_longlong_t)obsolete_sm_object, error); | |
a1d477c2 MA |
3089 | return (error); |
3090 | } | |
3091 | space_map_update(vd->vdev_obsolete_sm); | |
27f80e85 BB |
3092 | } else if (error != 0) { |
3093 | vdev_dbgmsg(vd, "vdev_load: failed to retrieve obsolete " | |
3094 | "space map object from vdev ZAP [error=%d]", error); | |
3095 | return (error); | |
a1d477c2 MA |
3096 | } |
3097 | ||
3098 | return (0); | |
34dc7c2f BB |
3099 | } |
3100 | ||
3101 | /* | |
3102 | * The special vdev case is used for hot spares and l2cache devices. Its | |
3103 | * sole purpose it to set the vdev state for the associated vdev. To do this, | |
3104 | * we make sure that we can open the underlying device, then try to read the | |
3105 | * label, and make sure that the label is sane and that it hasn't been | |
3106 | * repurposed to another pool. | |
3107 | */ | |
3108 | int | |
3109 | vdev_validate_aux(vdev_t *vd) | |
3110 | { | |
3111 | nvlist_t *label; | |
3112 | uint64_t guid, version; | |
3113 | uint64_t state; | |
3114 | ||
b128c09f BB |
3115 | if (!vdev_readable(vd)) |
3116 | return (0); | |
3117 | ||
3bc7e0fb | 3118 | if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { |
34dc7c2f BB |
3119 | vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, |
3120 | VDEV_AUX_CORRUPT_DATA); | |
3121 | return (-1); | |
3122 | } | |
3123 | ||
3124 | if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || | |
9ae529ec | 3125 | !SPA_VERSION_IS_SUPPORTED(version) || |
34dc7c2f BB |
3126 | nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || |
3127 | guid != vd->vdev_guid || | |
3128 | nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { | |
3129 | vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, | |
3130 | VDEV_AUX_CORRUPT_DATA); | |
3131 | nvlist_free(label); | |
3132 | return (-1); | |
3133 | } | |
3134 | ||
3135 | /* | |
3136 | * We don't actually check the pool state here. If it's in fact in | |
3137 | * use by another pool, we update this fact on the fly when requested. | |
3138 | */ | |
3139 | nvlist_free(label); | |
3140 | return (0); | |
3141 | } | |
3142 | ||
a1d477c2 MA |
3143 | /* |
3144 | * Free the objects used to store this vdev's spacemaps, and the array | |
3145 | * that points to them. | |
3146 | */ | |
428870ff | 3147 | void |
a1d477c2 MA |
3148 | vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx) |
3149 | { | |
3150 | if (vd->vdev_ms_array == 0) | |
3151 | return; | |
3152 | ||
3153 | objset_t *mos = vd->vdev_spa->spa_meta_objset; | |
3154 | uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift; | |
3155 | size_t array_bytes = array_count * sizeof (uint64_t); | |
3156 | uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP); | |
3157 | VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0, | |
3158 | array_bytes, smobj_array, 0)); | |
3159 | ||
3160 | for (uint64_t i = 0; i < array_count; i++) { | |
3161 | uint64_t smobj = smobj_array[i]; | |
3162 | if (smobj == 0) | |
3163 | continue; | |
3164 | ||
3165 | space_map_free_obj(mos, smobj, tx); | |
3166 | } | |
3167 | ||
3168 | kmem_free(smobj_array, array_bytes); | |
3169 | VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx)); | |
3170 | vd->vdev_ms_array = 0; | |
3171 | } | |
3172 | ||
3173 | static void | |
ee900344 | 3174 | vdev_remove_empty_log(vdev_t *vd, uint64_t txg) |
428870ff BB |
3175 | { |
3176 | spa_t *spa = vd->vdev_spa; | |
428870ff | 3177 | |
ee900344 | 3178 | ASSERT(vd->vdev_islog); |
e0ab3ab5 JS |
3179 | ASSERT(vd == vd->vdev_top); |
3180 | ASSERT3U(txg, ==, spa_syncing_txg(spa)); | |
428870ff | 3181 | |
428870ff | 3182 | if (vd->vdev_ms != NULL) { |
f3a7f661 GW |
3183 | metaslab_group_t *mg = vd->vdev_mg; |
3184 | ||
3185 | metaslab_group_histogram_verify(mg); | |
3186 | metaslab_class_histogram_verify(mg->mg_class); | |
3187 | ||
1c27024e | 3188 | for (int m = 0; m < vd->vdev_ms_count; m++) { |
428870ff BB |
3189 | metaslab_t *msp = vd->vdev_ms[m]; |
3190 | ||
93cf2076 | 3191 | if (msp == NULL || msp->ms_sm == NULL) |
428870ff BB |
3192 | continue; |
3193 | ||
93cf2076 | 3194 | mutex_enter(&msp->ms_lock); |
f3a7f661 GW |
3195 | /* |
3196 | * If the metaslab was not loaded when the vdev | |
3197 | * was removed then the histogram accounting may | |
3198 | * not be accurate. Update the histogram information | |
3199 | * here so that we ensure that the metaslab group | |
3200 | * and metaslab class are up-to-date. | |
3201 | */ | |
3202 | metaslab_group_histogram_remove(mg, msp); | |
3203 | ||
93cf2076 | 3204 | VERIFY0(space_map_allocated(msp->ms_sm)); |
93cf2076 GW |
3205 | space_map_close(msp->ms_sm); |
3206 | msp->ms_sm = NULL; | |
3207 | mutex_exit(&msp->ms_lock); | |
428870ff | 3208 | } |
f3a7f661 | 3209 | |
d2734cce SD |
3210 | if (vd->vdev_checkpoint_sm != NULL) { |
3211 | ASSERT(spa_has_checkpoint(spa)); | |
3212 | space_map_close(vd->vdev_checkpoint_sm); | |
3213 | vd->vdev_checkpoint_sm = NULL; | |
3214 | } | |
3215 | ||
f3a7f661 GW |
3216 | metaslab_group_histogram_verify(mg); |
3217 | metaslab_class_histogram_verify(mg->mg_class); | |
cc99f275 | 3218 | |
1c27024e | 3219 | for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) |
f3a7f661 | 3220 | ASSERT0(mg->mg_histogram[i]); |
428870ff BB |
3221 | } |
3222 | ||
ee900344 | 3223 | dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); |
e0ab3ab5 | 3224 | |
ee900344 SD |
3225 | vdev_destroy_spacemaps(vd, tx); |
3226 | if (vd->vdev_top_zap != 0) { | |
e0ab3ab5 JS |
3227 | vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx); |
3228 | vd->vdev_top_zap = 0; | |
3229 | } | |
ee900344 | 3230 | |
428870ff BB |
3231 | dmu_tx_commit(tx); |
3232 | } | |
3233 | ||
34dc7c2f BB |
3234 | void |
3235 | vdev_sync_done(vdev_t *vd, uint64_t txg) | |
3236 | { | |
3237 | metaslab_t *msp; | |
428870ff BB |
3238 | boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); |
3239 | ||
a1d477c2 | 3240 | ASSERT(vdev_is_concrete(vd)); |
34dc7c2f | 3241 | |
619f0976 GW |
3242 | while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) |
3243 | != NULL) | |
34dc7c2f | 3244 | metaslab_sync_done(msp, txg); |
428870ff BB |
3245 | |
3246 | if (reassess) | |
3247 | metaslab_sync_reassess(vd->vdev_mg); | |
34dc7c2f BB |
3248 | } |
3249 | ||
3250 | void | |
3251 | vdev_sync(vdev_t *vd, uint64_t txg) | |
3252 | { | |
3253 | spa_t *spa = vd->vdev_spa; | |
3254 | vdev_t *lvd; | |
3255 | metaslab_t *msp; | |
3256 | dmu_tx_t *tx; | |
3257 | ||
a1d477c2 MA |
3258 | if (range_tree_space(vd->vdev_obsolete_segments) > 0) { |
3259 | dmu_tx_t *tx; | |
428870ff | 3260 | |
a1d477c2 MA |
3261 | ASSERT(vd->vdev_removing || |
3262 | vd->vdev_ops == &vdev_indirect_ops); | |
3263 | ||
3264 | tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); | |
3265 | vdev_indirect_sync_obsolete(vd, tx); | |
3266 | dmu_tx_commit(tx); | |
3267 | ||
3268 | /* | |
3269 | * If the vdev is indirect, it can't have dirty | |
3270 | * metaslabs or DTLs. | |
3271 | */ | |
3272 | if (vd->vdev_ops == &vdev_indirect_ops) { | |
3273 | ASSERT(txg_list_empty(&vd->vdev_ms_list, txg)); | |
3274 | ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg)); | |
3275 | return; | |
3276 | } | |
3277 | } | |
3278 | ||
3279 | ASSERT(vdev_is_concrete(vd)); | |
3280 | ||
3281 | if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 && | |
3282 | !vd->vdev_removing) { | |
34dc7c2f | 3283 | ASSERT(vd == vd->vdev_top); |
a1d477c2 | 3284 | ASSERT0(vd->vdev_indirect_config.vic_mapping_object); |
34dc7c2f BB |
3285 | tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); |
3286 | vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, | |
3287 | DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); | |
3288 | ASSERT(vd->vdev_ms_array != 0); | |
3289 | vdev_config_dirty(vd); | |
3290 | dmu_tx_commit(tx); | |
3291 | } | |
3292 | ||
3293 | while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { | |
3294 | metaslab_sync(msp, txg); | |
3295 | (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); | |
3296 | } | |
3297 | ||
3298 | while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) | |
3299 | vdev_dtl_sync(lvd, txg); | |
3300 | ||
a1d477c2 | 3301 | /* |
ee900344 SD |
3302 | * If this is an empty log device being removed, destroy the |
3303 | * metadata associated with it. | |
a1d477c2 | 3304 | */ |
ee900344 SD |
3305 | if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) |
3306 | vdev_remove_empty_log(vd, txg); | |
a1d477c2 | 3307 | |
34dc7c2f BB |
3308 | (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); |
3309 | } | |
3310 | ||
3311 | uint64_t | |
3312 | vdev_psize_to_asize(vdev_t *vd, uint64_t psize) | |
3313 | { | |
3314 | return (vd->vdev_ops->vdev_op_asize(vd, psize)); | |
3315 | } | |
3316 | ||
34dc7c2f BB |
3317 | /* |
3318 | * Mark the given vdev faulted. A faulted vdev behaves as if the device could | |
3319 | * not be opened, and no I/O is attempted. | |
3320 | */ | |
3321 | int | |
428870ff | 3322 | vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) |
34dc7c2f | 3323 | { |
572e2857 | 3324 | vdev_t *vd, *tvd; |
34dc7c2f | 3325 | |
428870ff | 3326 | spa_vdev_state_enter(spa, SCL_NONE); |
34dc7c2f | 3327 | |
b128c09f BB |
3328 | if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) |
3329 | return (spa_vdev_state_exit(spa, NULL, ENODEV)); | |
34dc7c2f | 3330 | |
34dc7c2f | 3331 | if (!vd->vdev_ops->vdev_op_leaf) |
b128c09f | 3332 | return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); |
34dc7c2f | 3333 | |
572e2857 BB |
3334 | tvd = vd->vdev_top; |
3335 | ||
4a283c7f TH |
3336 | /* |
3337 | * If user did a 'zpool offline -f' then make the fault persist across | |
3338 | * reboots. | |
3339 | */ | |
3340 | if (aux == VDEV_AUX_EXTERNAL_PERSIST) { | |
3341 | /* | |
3342 | * There are two kinds of forced faults: temporary and | |
3343 | * persistent. Temporary faults go away at pool import, while | |
3344 | * persistent faults stay set. Both types of faults can be | |
3345 | * cleared with a zpool clear. | |
3346 | * | |
3347 | * We tell if a vdev is persistently faulted by looking at the | |
3348 | * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at | |
3349 | * import then it's a persistent fault. Otherwise, it's | |
3350 | * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external" | |
3351 | * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This | |
3352 | * tells vdev_config_generate() (which gets run later) to set | |
3353 | * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist. | |
3354 | */ | |
3355 | vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL; | |
3356 | vd->vdev_tmpoffline = B_FALSE; | |
3357 | aux = VDEV_AUX_EXTERNAL; | |
3358 | } else { | |
3359 | vd->vdev_tmpoffline = B_TRUE; | |
3360 | } | |
3361 | ||
428870ff BB |
3362 | /* |
3363 | * We don't directly use the aux state here, but if we do a | |
3364 | * vdev_reopen(), we need this value to be present to remember why we | |
3365 | * were faulted. | |
3366 | */ | |
3367 | vd->vdev_label_aux = aux; | |
3368 | ||
34dc7c2f BB |
3369 | /* |
3370 | * Faulted state takes precedence over degraded. | |
3371 | */ | |
428870ff | 3372 | vd->vdev_delayed_close = B_FALSE; |
34dc7c2f BB |
3373 | vd->vdev_faulted = 1ULL; |
3374 | vd->vdev_degraded = 0ULL; | |
428870ff | 3375 | vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); |
34dc7c2f BB |
3376 | |
3377 | /* | |
428870ff BB |
3378 | * If this device has the only valid copy of the data, then |
3379 | * back off and simply mark the vdev as degraded instead. | |
34dc7c2f | 3380 | */ |
572e2857 | 3381 | if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { |
34dc7c2f BB |
3382 | vd->vdev_degraded = 1ULL; |
3383 | vd->vdev_faulted = 0ULL; | |
3384 | ||
3385 | /* | |
3386 | * If we reopen the device and it's not dead, only then do we | |
3387 | * mark it degraded. | |
3388 | */ | |
572e2857 | 3389 | vdev_reopen(tvd); |
34dc7c2f | 3390 | |
428870ff BB |
3391 | if (vdev_readable(vd)) |
3392 | vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); | |
34dc7c2f BB |
3393 | } |
3394 | ||
b128c09f | 3395 | return (spa_vdev_state_exit(spa, vd, 0)); |
34dc7c2f BB |
3396 | } |
3397 | ||
3398 | /* | |
3399 | * Mark the given vdev degraded. A degraded vdev is purely an indication to the | |
3400 | * user that something is wrong. The vdev continues to operate as normal as far | |
3401 | * as I/O is concerned. | |
3402 | */ | |
3403 | int | |
428870ff | 3404 | vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) |
34dc7c2f | 3405 | { |
b128c09f | 3406 | vdev_t *vd; |
34dc7c2f | 3407 | |
428870ff | 3408 | spa_vdev_state_enter(spa, SCL_NONE); |
34dc7c2f | 3409 | |
b128c09f BB |
3410 | if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) |
3411 | return (spa_vdev_state_exit(spa, NULL, ENODEV)); | |
34dc7c2f | 3412 | |
34dc7c2f | 3413 | if (!vd->vdev_ops->vdev_op_leaf) |
b128c09f | 3414 | return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); |
34dc7c2f BB |
3415 | |
3416 | /* | |
3417 | * If the vdev is already faulted, then don't do anything. | |
3418 | */ | |
b128c09f BB |
3419 | if (vd->vdev_faulted || vd->vdev_degraded) |
3420 | return (spa_vdev_state_exit(spa, NULL, 0)); | |
34dc7c2f BB |
3421 | |
3422 | vd->vdev_degraded = 1ULL; | |
3423 | if (!vdev_is_dead(vd)) | |
3424 | vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, | |
428870ff | 3425 | aux); |
34dc7c2f | 3426 | |
b128c09f | 3427 | return (spa_vdev_state_exit(spa, vd, 0)); |
34dc7c2f BB |
3428 | } |
3429 | ||
3430 | /* | |
d3cc8b15 WA |
3431 | * Online the given vdev. |
3432 | * | |
3433 | * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached | |
3434 | * spare device should be detached when the device finishes resilvering. | |
3435 | * Second, the online should be treated like a 'test' online case, so no FMA | |
3436 | * events are generated if the device fails to open. | |
34dc7c2f BB |
3437 | */ |
3438 | int | |
b128c09f | 3439 | vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) |
34dc7c2f | 3440 | { |
9babb374 | 3441 | vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; |
153b2285 YP |
3442 | boolean_t wasoffline; |
3443 | vdev_state_t oldstate; | |
34dc7c2f | 3444 | |
428870ff | 3445 | spa_vdev_state_enter(spa, SCL_NONE); |
34dc7c2f | 3446 | |
b128c09f BB |
3447 | if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) |
3448 | return (spa_vdev_state_exit(spa, NULL, ENODEV)); | |
34dc7c2f BB |
3449 | |
3450 | if (!vd->vdev_ops->vdev_op_leaf) | |
b128c09f | 3451 | return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); |
34dc7c2f | 3452 | |
153b2285 YP |
3453 | wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline); |
3454 | oldstate = vd->vdev_state; | |
fb390aaf | 3455 | |
9babb374 | 3456 | tvd = vd->vdev_top; |
34dc7c2f BB |
3457 | vd->vdev_offline = B_FALSE; |
3458 | vd->vdev_tmpoffline = B_FALSE; | |
b128c09f BB |
3459 | vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); |
3460 | vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); | |
9babb374 BB |
3461 | |
3462 | /* XXX - L2ARC 1.0 does not support expansion */ | |
3463 | if (!vd->vdev_aux) { | |
3464 | for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) | |
d441e85d BB |
3465 | pvd->vdev_expanding = !!((flags & ZFS_ONLINE_EXPAND) || |
3466 | spa->spa_autoexpand); | |
d48091de | 3467 | vd->vdev_expansion_time = gethrestime_sec(); |
9babb374 BB |
3468 | } |
3469 | ||
3470 | vdev_reopen(tvd); | |
34dc7c2f BB |
3471 | vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; |
3472 | ||
9babb374 BB |
3473 | if (!vd->vdev_aux) { |
3474 | for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) | |
3475 | pvd->vdev_expanding = B_FALSE; | |
3476 | } | |
3477 | ||
34dc7c2f BB |
3478 | if (newstate) |
3479 | *newstate = vd->vdev_state; | |
3480 | if ((flags & ZFS_ONLINE_UNSPARE) && | |
3481 | !vdev_is_dead(vd) && vd->vdev_parent && | |
3482 | vd->vdev_parent->vdev_ops == &vdev_spare_ops && | |
3483 | vd->vdev_parent->vdev_child[0] == vd) | |
3484 | vd->vdev_unspare = B_TRUE; | |
3485 | ||
9babb374 BB |
3486 | if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { |
3487 | ||
3488 | /* XXX - L2ARC 1.0 does not support expansion */ | |
3489 | if (vd->vdev_aux) | |
3490 | return (spa_vdev_state_exit(spa, vd, ENOTSUP)); | |
3491 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
3492 | } | |
fb390aaf | 3493 | |
619f0976 GW |
3494 | /* Restart initializing if necessary */ |
3495 | mutex_enter(&vd->vdev_initialize_lock); | |
3496 | if (vdev_writeable(vd) && | |
3497 | vd->vdev_initialize_thread == NULL && | |
3498 | vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) { | |
3499 | (void) vdev_initialize(vd); | |
3500 | } | |
3501 | mutex_exit(&vd->vdev_initialize_lock); | |
3502 | ||
153b2285 YP |
3503 | if (wasoffline || |
3504 | (oldstate < VDEV_STATE_DEGRADED && | |
3505 | vd->vdev_state >= VDEV_STATE_DEGRADED)) | |
12fa0466 | 3506 | spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE); |
fb390aaf | 3507 | |
fb5f0bc8 | 3508 | return (spa_vdev_state_exit(spa, vd, 0)); |
34dc7c2f BB |
3509 | } |
3510 | ||
428870ff BB |
3511 | static int |
3512 | vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) | |
34dc7c2f | 3513 | { |
9babb374 | 3514 | vdev_t *vd, *tvd; |
428870ff BB |
3515 | int error = 0; |
3516 | uint64_t generation; | |
3517 | metaslab_group_t *mg; | |
34dc7c2f | 3518 | |
428870ff BB |
3519 | top: |
3520 | spa_vdev_state_enter(spa, SCL_ALLOC); | |
34dc7c2f | 3521 | |
b128c09f BB |
3522 | if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) |
3523 | return (spa_vdev_state_exit(spa, NULL, ENODEV)); | |
34dc7c2f BB |
3524 | |
3525 | if (!vd->vdev_ops->vdev_op_leaf) | |
b128c09f | 3526 | return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); |
34dc7c2f | 3527 | |
9babb374 | 3528 | tvd = vd->vdev_top; |
428870ff BB |
3529 | mg = tvd->vdev_mg; |
3530 | generation = spa->spa_config_generation + 1; | |
9babb374 | 3531 | |
34dc7c2f BB |
3532 | /* |
3533 | * If the device isn't already offline, try to offline it. | |
3534 | */ | |
3535 | if (!vd->vdev_offline) { | |
3536 | /* | |
fb5f0bc8 | 3537 | * If this device has the only valid copy of some data, |
9babb374 BB |
3538 | * don't allow it to be offlined. Log devices are always |
3539 | * expendable. | |
34dc7c2f | 3540 | */ |
9babb374 BB |
3541 | if (!tvd->vdev_islog && vd->vdev_aux == NULL && |
3542 | vdev_dtl_required(vd)) | |
b128c09f | 3543 | return (spa_vdev_state_exit(spa, NULL, EBUSY)); |
34dc7c2f | 3544 | |
428870ff BB |
3545 | /* |
3546 | * If the top-level is a slog and it has had allocations | |
3547 | * then proceed. We check that the vdev's metaslab group | |
3548 | * is not NULL since it's possible that we may have just | |
3549 | * added this vdev but not yet initialized its metaslabs. | |
3550 | */ | |
3551 | if (tvd->vdev_islog && mg != NULL) { | |
3552 | /* | |
3553 | * Prevent any future allocations. | |
3554 | */ | |
3555 | metaslab_group_passivate(mg); | |
3556 | (void) spa_vdev_state_exit(spa, vd, 0); | |
3557 | ||
a1d477c2 | 3558 | error = spa_reset_logs(spa); |
428870ff | 3559 | |
d2734cce SD |
3560 | /* |
3561 | * If the log device was successfully reset but has | |
3562 | * checkpointed data, do not offline it. | |
3563 | */ | |
3564 | if (error == 0 && | |
3565 | tvd->vdev_checkpoint_sm != NULL) { | |
3566 | ASSERT3U(tvd->vdev_checkpoint_sm->sm_alloc, | |
3567 | !=, 0); | |
3568 | error = ZFS_ERR_CHECKPOINT_EXISTS; | |
3569 | } | |
3570 | ||
428870ff BB |
3571 | spa_vdev_state_enter(spa, SCL_ALLOC); |
3572 | ||
3573 | /* | |
3574 | * Check to see if the config has changed. | |
3575 | */ | |
3576 | if (error || generation != spa->spa_config_generation) { | |
3577 | metaslab_group_activate(mg); | |
3578 | if (error) | |
3579 | return (spa_vdev_state_exit(spa, | |
3580 | vd, error)); | |
3581 | (void) spa_vdev_state_exit(spa, vd, 0); | |
3582 | goto top; | |
3583 | } | |
c99c9001 | 3584 | ASSERT0(tvd->vdev_stat.vs_alloc); |
428870ff BB |
3585 | } |
3586 | ||
34dc7c2f BB |
3587 | /* |
3588 | * Offline this device and reopen its top-level vdev. | |
9babb374 BB |
3589 | * If the top-level vdev is a log device then just offline |
3590 | * it. Otherwise, if this action results in the top-level | |
3591 | * vdev becoming unusable, undo it and fail the request. | |
34dc7c2f BB |
3592 | */ |
3593 | vd->vdev_offline = B_TRUE; | |
9babb374 BB |
3594 | vdev_reopen(tvd); |
3595 | ||
3596 | if (!tvd->vdev_islog && vd->vdev_aux == NULL && | |
3597 | vdev_is_dead(tvd)) { | |
34dc7c2f | 3598 | vd->vdev_offline = B_FALSE; |
9babb374 | 3599 | vdev_reopen(tvd); |
b128c09f | 3600 | return (spa_vdev_state_exit(spa, NULL, EBUSY)); |
34dc7c2f | 3601 | } |
428870ff BB |
3602 | |
3603 | /* | |
3604 | * Add the device back into the metaslab rotor so that | |
3605 | * once we online the device it's open for business. | |
3606 | */ | |
3607 | if (tvd->vdev_islog && mg != NULL) | |
3608 | metaslab_group_activate(mg); | |
34dc7c2f BB |
3609 | } |
3610 | ||
b128c09f | 3611 | vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); |
34dc7c2f | 3612 | |
428870ff BB |
3613 | return (spa_vdev_state_exit(spa, vd, 0)); |
3614 | } | |
9babb374 | 3615 | |
428870ff BB |
3616 | int |
3617 | vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) | |
3618 | { | |
3619 | int error; | |
9babb374 | 3620 | |
428870ff BB |
3621 | mutex_enter(&spa->spa_vdev_top_lock); |
3622 | error = vdev_offline_locked(spa, guid, flags); | |
3623 | mutex_exit(&spa->spa_vdev_top_lock); | |
3624 | ||
3625 | return (error); | |
34dc7c2f BB |
3626 | } |
3627 | ||
3628 | /* | |
3629 | * Clear the error counts associated with this vdev. Unlike vdev_online() and | |
3630 | * vdev_offline(), we assume the spa config is locked. We also clear all | |
3631 | * children. If 'vd' is NULL, then the user wants to clear all vdevs. | |
34dc7c2f BB |
3632 | */ |
3633 | void | |
b128c09f | 3634 | vdev_clear(spa_t *spa, vdev_t *vd) |
34dc7c2f | 3635 | { |
b128c09f BB |
3636 | vdev_t *rvd = spa->spa_root_vdev; |
3637 | ||
3638 | ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); | |
34dc7c2f BB |
3639 | |
3640 | if (vd == NULL) | |
b128c09f | 3641 | vd = rvd; |
34dc7c2f BB |
3642 | |
3643 | vd->vdev_stat.vs_read_errors = 0; | |
3644 | vd->vdev_stat.vs_write_errors = 0; | |
3645 | vd->vdev_stat.vs_checksum_errors = 0; | |
ad796b8a | 3646 | vd->vdev_stat.vs_slow_ios = 0; |
34dc7c2f | 3647 | |
1c27024e | 3648 | for (int c = 0; c < vd->vdev_children; c++) |
b128c09f | 3649 | vdev_clear(spa, vd->vdev_child[c]); |
34dc7c2f | 3650 | |
a1d477c2 MA |
3651 | /* |
3652 | * It makes no sense to "clear" an indirect vdev. | |
3653 | */ | |
3654 | if (!vdev_is_concrete(vd)) | |
3655 | return; | |
3656 | ||
34dc7c2f | 3657 | /* |
b128c09f BB |
3658 | * If we're in the FAULTED state or have experienced failed I/O, then |
3659 | * clear the persistent state and attempt to reopen the device. We | |
3660 | * also mark the vdev config dirty, so that the new faulted state is | |
3661 | * written out to disk. | |
34dc7c2f | 3662 | */ |
b128c09f BB |
3663 | if (vd->vdev_faulted || vd->vdev_degraded || |
3664 | !vdev_readable(vd) || !vdev_writeable(vd)) { | |
428870ff | 3665 | /* |
4e33ba4c | 3666 | * When reopening in response to a clear event, it may be due to |
428870ff BB |
3667 | * a fmadm repair request. In this case, if the device is |
3668 | * still broken, we want to still post the ereport again. | |
3669 | */ | |
3670 | vd->vdev_forcefault = B_TRUE; | |
3671 | ||
572e2857 | 3672 | vd->vdev_faulted = vd->vdev_degraded = 0ULL; |
b128c09f BB |
3673 | vd->vdev_cant_read = B_FALSE; |
3674 | vd->vdev_cant_write = B_FALSE; | |
4a283c7f | 3675 | vd->vdev_stat.vs_aux = 0; |
b128c09f | 3676 | |
572e2857 | 3677 | vdev_reopen(vd == rvd ? rvd : vd->vdev_top); |
34dc7c2f | 3678 | |
428870ff BB |
3679 | vd->vdev_forcefault = B_FALSE; |
3680 | ||
572e2857 | 3681 | if (vd != rvd && vdev_writeable(vd->vdev_top)) |
b128c09f BB |
3682 | vdev_state_dirty(vd->vdev_top); |
3683 | ||
80a91e74 TC |
3684 | if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) { |
3685 | if (dsl_scan_resilvering(spa->spa_dsl_pool) && | |
3686 | spa_feature_is_enabled(spa, | |
3687 | SPA_FEATURE_RESILVER_DEFER)) | |
3688 | vdev_set_deferred_resilver(spa, vd); | |
3689 | else | |
3690 | spa_async_request(spa, SPA_ASYNC_RESILVER); | |
3691 | } | |
34dc7c2f | 3692 | |
12fa0466 | 3693 | spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR); |
34dc7c2f | 3694 | } |
428870ff BB |
3695 | |
3696 | /* | |
3697 | * When clearing a FMA-diagnosed fault, we always want to | |
3698 | * unspare the device, as we assume that the original spare was | |
3699 | * done in response to the FMA fault. | |
3700 | */ | |
3701 | if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && | |
3702 | vd->vdev_parent->vdev_ops == &vdev_spare_ops && | |
3703 | vd->vdev_parent->vdev_child[0] == vd) | |
3704 | vd->vdev_unspare = B_TRUE; | |
34dc7c2f BB |
3705 | } |
3706 | ||
b128c09f BB |
3707 | boolean_t |
3708 | vdev_is_dead(vdev_t *vd) | |
3709 | { | |
428870ff BB |
3710 | /* |
3711 | * Holes and missing devices are always considered "dead". | |
3712 | * This simplifies the code since we don't have to check for | |
3713 | * these types of devices in the various code paths. | |
3714 | * Instead we rely on the fact that we skip over dead devices | |
3715 | * before issuing I/O to them. | |
3716 | */ | |
a1d477c2 MA |
3717 | return (vd->vdev_state < VDEV_STATE_DEGRADED || |
3718 | vd->vdev_ops == &vdev_hole_ops || | |
428870ff | 3719 | vd->vdev_ops == &vdev_missing_ops); |
b128c09f BB |
3720 | } |
3721 | ||
3722 | boolean_t | |
34dc7c2f BB |
3723 | vdev_readable(vdev_t *vd) |
3724 | { | |
b128c09f | 3725 | return (!vdev_is_dead(vd) && !vd->vdev_cant_read); |
34dc7c2f BB |
3726 | } |
3727 | ||
b128c09f | 3728 | boolean_t |
34dc7c2f BB |
3729 | vdev_writeable(vdev_t *vd) |
3730 | { | |
a1d477c2 MA |
3731 | return (!vdev_is_dead(vd) && !vd->vdev_cant_write && |
3732 | vdev_is_concrete(vd)); | |
34dc7c2f BB |
3733 | } |
3734 | ||
b128c09f BB |
3735 | boolean_t |
3736 | vdev_allocatable(vdev_t *vd) | |
34dc7c2f | 3737 | { |
fb5f0bc8 BB |
3738 | uint64_t state = vd->vdev_state; |
3739 | ||
b128c09f | 3740 | /* |
fb5f0bc8 | 3741 | * We currently allow allocations from vdevs which may be in the |
b128c09f BB |
3742 | * process of reopening (i.e. VDEV_STATE_CLOSED). If the device |
3743 | * fails to reopen then we'll catch it later when we're holding | |
fb5f0bc8 BB |
3744 | * the proper locks. Note that we have to get the vdev state |
3745 | * in a local variable because although it changes atomically, | |
3746 | * we're asking two separate questions about it. | |
b128c09f | 3747 | */ |
fb5f0bc8 | 3748 | return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && |
a1d477c2 | 3749 | !vd->vdev_cant_write && vdev_is_concrete(vd) && |
3dfb57a3 | 3750 | vd->vdev_mg->mg_initialized); |
34dc7c2f BB |
3751 | } |
3752 | ||
b128c09f BB |
3753 | boolean_t |
3754 | vdev_accessible(vdev_t *vd, zio_t *zio) | |
34dc7c2f | 3755 | { |
b128c09f | 3756 | ASSERT(zio->io_vd == vd); |
34dc7c2f | 3757 | |
b128c09f BB |
3758 | if (vdev_is_dead(vd) || vd->vdev_remove_wanted) |
3759 | return (B_FALSE); | |
34dc7c2f | 3760 | |
b128c09f BB |
3761 | if (zio->io_type == ZIO_TYPE_READ) |
3762 | return (!vd->vdev_cant_read); | |
34dc7c2f | 3763 | |
b128c09f BB |
3764 | if (zio->io_type == ZIO_TYPE_WRITE) |
3765 | return (!vd->vdev_cant_write); | |
34dc7c2f | 3766 | |
b128c09f | 3767 | return (B_TRUE); |
34dc7c2f BB |
3768 | } |
3769 | ||
193a37cb TH |
3770 | static void |
3771 | vdev_get_child_stat(vdev_t *cvd, vdev_stat_t *vs, vdev_stat_t *cvs) | |
34dc7c2f | 3772 | { |
193a37cb TH |
3773 | int t; |
3774 | for (t = 0; t < ZIO_TYPES; t++) { | |
3775 | vs->vs_ops[t] += cvs->vs_ops[t]; | |
3776 | vs->vs_bytes[t] += cvs->vs_bytes[t]; | |
3777 | } | |
34dc7c2f | 3778 | |
193a37cb TH |
3779 | cvs->vs_scan_removing = cvd->vdev_removing; |
3780 | } | |
f3a7f661 | 3781 | |
193a37cb TH |
3782 | /* |
3783 | * Get extended stats | |
3784 | */ | |
3785 | static void | |
3786 | vdev_get_child_stat_ex(vdev_t *cvd, vdev_stat_ex_t *vsx, vdev_stat_ex_t *cvsx) | |
3787 | { | |
3788 | int t, b; | |
3789 | for (t = 0; t < ZIO_TYPES; t++) { | |
7e945072 | 3790 | for (b = 0; b < ARRAY_SIZE(vsx->vsx_disk_histo[0]); b++) |
193a37cb | 3791 | vsx->vsx_disk_histo[t][b] += cvsx->vsx_disk_histo[t][b]; |
7e945072 TH |
3792 | |
3793 | for (b = 0; b < ARRAY_SIZE(vsx->vsx_total_histo[0]); b++) { | |
193a37cb TH |
3794 | vsx->vsx_total_histo[t][b] += |
3795 | cvsx->vsx_total_histo[t][b]; | |
3796 | } | |
f38dfec3 | 3797 | } |
34dc7c2f | 3798 | |
193a37cb | 3799 | for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) { |
7e945072 | 3800 | for (b = 0; b < ARRAY_SIZE(vsx->vsx_queue_histo[0]); b++) { |
193a37cb TH |
3801 | vsx->vsx_queue_histo[t][b] += |
3802 | cvsx->vsx_queue_histo[t][b]; | |
3803 | } | |
3804 | vsx->vsx_active_queue[t] += cvsx->vsx_active_queue[t]; | |
3805 | vsx->vsx_pend_queue[t] += cvsx->vsx_pend_queue[t]; | |
7e945072 TH |
3806 | |
3807 | for (b = 0; b < ARRAY_SIZE(vsx->vsx_ind_histo[0]); b++) | |
3808 | vsx->vsx_ind_histo[t][b] += cvsx->vsx_ind_histo[t][b]; | |
3809 | ||
3810 | for (b = 0; b < ARRAY_SIZE(vsx->vsx_agg_histo[0]); b++) | |
3811 | vsx->vsx_agg_histo[t][b] += cvsx->vsx_agg_histo[t][b]; | |
193a37cb | 3812 | } |
7e945072 | 3813 | |
193a37cb TH |
3814 | } |
3815 | ||
d2734cce SD |
3816 | boolean_t |
3817 | vdev_is_spacemap_addressable(vdev_t *vd) | |
3818 | { | |
419ba591 SD |
3819 | if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2)) |
3820 | return (B_TRUE); | |
3821 | ||
d2734cce | 3822 | /* |
419ba591 SD |
3823 | * If double-word space map entries are not enabled we assume |
3824 | * 47 bits of the space map entry are dedicated to the entry's | |
3825 | * offset (see SM_OFFSET_BITS in space_map.h). We then use that | |
3826 | * to calculate the maximum address that can be described by a | |
3827 | * space map entry for the given device. | |
d2734cce | 3828 | */ |
419ba591 | 3829 | uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS; |
d2734cce SD |
3830 | |
3831 | if (shift >= 63) /* detect potential overflow */ | |
3832 | return (B_TRUE); | |
3833 | ||
3834 | return (vd->vdev_asize < (1ULL << shift)); | |
3835 | } | |
3836 | ||
193a37cb TH |
3837 | /* |
3838 | * Get statistics for the given vdev. | |
3839 | */ | |
3840 | static void | |
3841 | vdev_get_stats_ex_impl(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx) | |
3842 | { | |
1c27024e | 3843 | int t; |
34dc7c2f BB |
3844 | /* |
3845 | * If we're getting stats on the root vdev, aggregate the I/O counts | |
3846 | * over all top-level vdevs (i.e. the direct children of the root). | |
3847 | */ | |
193a37cb TH |
3848 | if (!vd->vdev_ops->vdev_op_leaf) { |
3849 | if (vs) { | |
3850 | memset(vs->vs_ops, 0, sizeof (vs->vs_ops)); | |
3851 | memset(vs->vs_bytes, 0, sizeof (vs->vs_bytes)); | |
3852 | } | |
3853 | if (vsx) | |
3854 | memset(vsx, 0, sizeof (*vsx)); | |
3855 | ||
1c27024e | 3856 | for (int c = 0; c < vd->vdev_children; c++) { |
193a37cb | 3857 | vdev_t *cvd = vd->vdev_child[c]; |
34dc7c2f | 3858 | vdev_stat_t *cvs = &cvd->vdev_stat; |
193a37cb TH |
3859 | vdev_stat_ex_t *cvsx = &cvd->vdev_stat_ex; |
3860 | ||
3861 | vdev_get_stats_ex_impl(cvd, cvs, cvsx); | |
3862 | if (vs) | |
3863 | vdev_get_child_stat(cvd, vs, cvs); | |
3864 | if (vsx) | |
3865 | vdev_get_child_stat_ex(cvd, vsx, cvsx); | |
34dc7c2f | 3866 | |
193a37cb TH |
3867 | } |
3868 | } else { | |
3869 | /* | |
3870 | * We're a leaf. Just copy our ZIO active queue stats in. The | |
3871 | * other leaf stats are updated in vdev_stat_update(). | |
3872 | */ | |
3873 | if (!vsx) | |
3874 | return; | |
3875 | ||
3876 | memcpy(vsx, &vd->vdev_stat_ex, sizeof (vd->vdev_stat_ex)); | |
3877 | ||
3878 | for (t = 0; t < ARRAY_SIZE(vd->vdev_queue.vq_class); t++) { | |
3879 | vsx->vsx_active_queue[t] = | |
3880 | vd->vdev_queue.vq_class[t].vqc_active; | |
3881 | vsx->vsx_pend_queue[t] = avl_numnodes( | |
3882 | &vd->vdev_queue.vq_class[t].vqc_queued_tree); | |
34dc7c2f BB |
3883 | } |
3884 | } | |
193a37cb TH |
3885 | } |
3886 | ||
3887 | void | |
3888 | vdev_get_stats_ex(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx) | |
3889 | { | |
0f676dc2 | 3890 | vdev_t *tvd = vd->vdev_top; |
193a37cb TH |
3891 | mutex_enter(&vd->vdev_stat_lock); |
3892 | if (vs) { | |
3893 | bcopy(&vd->vdev_stat, vs, sizeof (*vs)); | |
3894 | vs->vs_timestamp = gethrtime() - vs->vs_timestamp; | |
3895 | vs->vs_state = vd->vdev_state; | |
3896 | vs->vs_rsize = vdev_get_min_asize(vd); | |
619f0976 | 3897 | if (vd->vdev_ops->vdev_op_leaf) { |
193a37cb TH |
3898 | vs->vs_rsize += VDEV_LABEL_START_SIZE + |
3899 | VDEV_LABEL_END_SIZE; | |
619f0976 GW |
3900 | /* |
3901 | * Report intializing progress. Since we don't | |
3902 | * have the initializing locks held, this is only | |
3903 | * an estimate (although a fairly accurate one). | |
3904 | */ | |
3905 | vs->vs_initialize_bytes_done = | |
3906 | vd->vdev_initialize_bytes_done; | |
3907 | vs->vs_initialize_bytes_est = | |
3908 | vd->vdev_initialize_bytes_est; | |
3909 | vs->vs_initialize_state = vd->vdev_initialize_state; | |
3910 | vs->vs_initialize_action_time = | |
3911 | vd->vdev_initialize_action_time; | |
3912 | } | |
0f676dc2 GM |
3913 | /* |
3914 | * Report expandable space on top-level, non-auxillary devices | |
3915 | * only. The expandable space is reported in terms of metaslab | |
3916 | * sized units since that determines how much space the pool | |
3917 | * can expand. | |
3918 | */ | |
3919 | if (vd->vdev_aux == NULL && tvd != NULL) { | |
3920 | vs->vs_esize = P2ALIGN( | |
3921 | vd->vdev_max_asize - vd->vdev_asize, | |
3922 | 1ULL << tvd->vdev_ms_shift); | |
3923 | } | |
193a37cb | 3924 | if (vd->vdev_aux == NULL && vd == vd->vdev_top && |
a1d477c2 | 3925 | vdev_is_concrete(vd)) { |
cc99f275 DB |
3926 | vs->vs_fragmentation = (vd->vdev_mg != NULL) ? |
3927 | vd->vdev_mg->mg_fragmentation : 0; | |
193a37cb | 3928 | } |
80a91e74 TC |
3929 | if (vd->vdev_ops->vdev_op_leaf) |
3930 | vs->vs_resilver_deferred = vd->vdev_resilver_deferred; | |
193a37cb TH |
3931 | } |
3932 | ||
3933 | ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_READER) != 0); | |
3934 | vdev_get_stats_ex_impl(vd, vs, vsx); | |
f3a7f661 | 3935 | mutex_exit(&vd->vdev_stat_lock); |
34dc7c2f BB |
3936 | } |
3937 | ||
193a37cb TH |
3938 | void |
3939 | vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) | |
3940 | { | |
3941 | return (vdev_get_stats_ex(vd, vs, NULL)); | |
3942 | } | |
3943 | ||
34dc7c2f BB |
3944 | void |
3945 | vdev_clear_stats(vdev_t *vd) | |
3946 | { | |
3947 | mutex_enter(&vd->vdev_stat_lock); | |
3948 | vd->vdev_stat.vs_space = 0; | |
3949 | vd->vdev_stat.vs_dspace = 0; | |
3950 | vd->vdev_stat.vs_alloc = 0; | |
3951 | mutex_exit(&vd->vdev_stat_lock); | |
3952 | } | |
3953 | ||
428870ff BB |
3954 | void |
3955 | vdev_scan_stat_init(vdev_t *vd) | |
3956 | { | |
3957 | vdev_stat_t *vs = &vd->vdev_stat; | |
3958 | ||
1c27024e | 3959 | for (int c = 0; c < vd->vdev_children; c++) |
428870ff BB |
3960 | vdev_scan_stat_init(vd->vdev_child[c]); |
3961 | ||
3962 | mutex_enter(&vd->vdev_stat_lock); | |
3963 | vs->vs_scan_processed = 0; | |
3964 | mutex_exit(&vd->vdev_stat_lock); | |
3965 | } | |
3966 | ||
34dc7c2f | 3967 | void |
b128c09f | 3968 | vdev_stat_update(zio_t *zio, uint64_t psize) |
34dc7c2f | 3969 | { |
fb5f0bc8 BB |
3970 | spa_t *spa = zio->io_spa; |
3971 | vdev_t *rvd = spa->spa_root_vdev; | |
b128c09f | 3972 | vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; |
34dc7c2f BB |
3973 | vdev_t *pvd; |
3974 | uint64_t txg = zio->io_txg; | |
3975 | vdev_stat_t *vs = &vd->vdev_stat; | |
193a37cb | 3976 | vdev_stat_ex_t *vsx = &vd->vdev_stat_ex; |
34dc7c2f BB |
3977 | zio_type_t type = zio->io_type; |
3978 | int flags = zio->io_flags; | |
3979 | ||
b128c09f BB |
3980 | /* |
3981 | * If this i/o is a gang leader, it didn't do any actual work. | |
3982 | */ | |
3983 | if (zio->io_gang_tree) | |
3984 | return; | |
3985 | ||
34dc7c2f | 3986 | if (zio->io_error == 0) { |
b128c09f BB |
3987 | /* |
3988 | * If this is a root i/o, don't count it -- we've already | |
3989 | * counted the top-level vdevs, and vdev_get_stats() will | |
3990 | * aggregate them when asked. This reduces contention on | |
3991 | * the root vdev_stat_lock and implicitly handles blocks | |
3992 | * that compress away to holes, for which there is no i/o. | |
3993 | * (Holes never create vdev children, so all the counters | |
3994 | * remain zero, which is what we want.) | |
3995 | * | |
3996 | * Note: this only applies to successful i/o (io_error == 0) | |
3997 | * because unlike i/o counts, errors are not additive. | |
3998 | * When reading a ditto block, for example, failure of | |
3999 | * one top-level vdev does not imply a root-level error. | |
4000 | */ | |
4001 | if (vd == rvd) | |
4002 | return; | |
4003 | ||
4004 | ASSERT(vd == zio->io_vd); | |
fb5f0bc8 BB |
4005 | |
4006 | if (flags & ZIO_FLAG_IO_BYPASS) | |
4007 | return; | |
4008 | ||
4009 | mutex_enter(&vd->vdev_stat_lock); | |
4010 | ||
b128c09f | 4011 | if (flags & ZIO_FLAG_IO_REPAIR) { |
572e2857 | 4012 | if (flags & ZIO_FLAG_SCAN_THREAD) { |
428870ff BB |
4013 | dsl_scan_phys_t *scn_phys = |
4014 | &spa->spa_dsl_pool->dp_scan->scn_phys; | |
4015 | uint64_t *processed = &scn_phys->scn_processed; | |
4016 | ||
4017 | /* XXX cleanup? */ | |
4018 | if (vd->vdev_ops->vdev_op_leaf) | |
4019 | atomic_add_64(processed, psize); | |
4020 | vs->vs_scan_processed += psize; | |
4021 | } | |
4022 | ||
fb5f0bc8 | 4023 | if (flags & ZIO_FLAG_SELF_HEAL) |
b128c09f | 4024 | vs->vs_self_healed += psize; |
34dc7c2f | 4025 | } |
fb5f0bc8 | 4026 | |
193a37cb TH |
4027 | /* |
4028 | * The bytes/ops/histograms are recorded at the leaf level and | |
4029 | * aggregated into the higher level vdevs in vdev_get_stats(). | |
4030 | */ | |
4eb0db42 TH |
4031 | if (vd->vdev_ops->vdev_op_leaf && |
4032 | (zio->io_priority < ZIO_PRIORITY_NUM_QUEUEABLE)) { | |
193a37cb TH |
4033 | |
4034 | vs->vs_ops[type]++; | |
4035 | vs->vs_bytes[type] += psize; | |
4036 | ||
7e945072 TH |
4037 | if (flags & ZIO_FLAG_DELEGATED) { |
4038 | vsx->vsx_agg_histo[zio->io_priority] | |
4039 | [RQ_HISTO(zio->io_size)]++; | |
4040 | } else { | |
4041 | vsx->vsx_ind_histo[zio->io_priority] | |
4042 | [RQ_HISTO(zio->io_size)]++; | |
4043 | } | |
4044 | ||
193a37cb TH |
4045 | if (zio->io_delta && zio->io_delay) { |
4046 | vsx->vsx_queue_histo[zio->io_priority] | |
7e945072 | 4047 | [L_HISTO(zio->io_delta - zio->io_delay)]++; |
193a37cb | 4048 | vsx->vsx_disk_histo[type] |
7e945072 | 4049 | [L_HISTO(zio->io_delay)]++; |
193a37cb | 4050 | vsx->vsx_total_histo[type] |
7e945072 | 4051 | [L_HISTO(zio->io_delta)]++; |
193a37cb TH |
4052 | } |
4053 | } | |
fb5f0bc8 BB |
4054 | |
4055 | mutex_exit(&vd->vdev_stat_lock); | |
34dc7c2f BB |
4056 | return; |
4057 | } | |
4058 | ||
4059 | if (flags & ZIO_FLAG_SPECULATIVE) | |
4060 | return; | |
4061 | ||
9babb374 BB |
4062 | /* |
4063 | * If this is an I/O error that is going to be retried, then ignore the | |
4064 | * error. Otherwise, the user may interpret B_FAILFAST I/O errors as | |
4065 | * hard errors, when in reality they can happen for any number of | |
4066 | * innocuous reasons (bus resets, MPxIO link failure, etc). | |
4067 | */ | |
4068 | if (zio->io_error == EIO && | |
4069 | !(zio->io_flags & ZIO_FLAG_IO_RETRY)) | |
4070 | return; | |
4071 | ||
428870ff BB |
4072 | /* |
4073 | * Intent logs writes won't propagate their error to the root | |
4074 | * I/O so don't mark these types of failures as pool-level | |
4075 | * errors. | |
4076 | */ | |
4077 | if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) | |
4078 | return; | |
4079 | ||
b128c09f | 4080 | mutex_enter(&vd->vdev_stat_lock); |
9babb374 | 4081 | if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { |
b128c09f BB |
4082 | if (zio->io_error == ECKSUM) |
4083 | vs->vs_checksum_errors++; | |
4084 | else | |
4085 | vs->vs_read_errors++; | |
34dc7c2f | 4086 | } |
9babb374 | 4087 | if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) |
b128c09f BB |
4088 | vs->vs_write_errors++; |
4089 | mutex_exit(&vd->vdev_stat_lock); | |
34dc7c2f | 4090 | |
a1d477c2 MA |
4091 | if (spa->spa_load_state == SPA_LOAD_NONE && |
4092 | type == ZIO_TYPE_WRITE && txg != 0 && | |
fb5f0bc8 | 4093 | (!(flags & ZIO_FLAG_IO_REPAIR) || |
572e2857 | 4094 | (flags & ZIO_FLAG_SCAN_THREAD) || |
428870ff | 4095 | spa->spa_claiming)) { |
fb5f0bc8 | 4096 | /* |
428870ff BB |
4097 | * This is either a normal write (not a repair), or it's |
4098 | * a repair induced by the scrub thread, or it's a repair | |
4099 | * made by zil_claim() during spa_load() in the first txg. | |
4100 | * In the normal case, we commit the DTL change in the same | |
4101 | * txg as the block was born. In the scrub-induced repair | |
4102 | * case, we know that scrubs run in first-pass syncing context, | |
4103 | * so we commit the DTL change in spa_syncing_txg(spa). | |
4104 | * In the zil_claim() case, we commit in spa_first_txg(spa). | |
fb5f0bc8 BB |
4105 | * |
4106 | * We currently do not make DTL entries for failed spontaneous | |
4107 | * self-healing writes triggered by normal (non-scrubbing) | |
4108 | * reads, because we have no transactional context in which to | |
4109 | * do so -- and it's not clear that it'd be desirable anyway. | |
4110 | */ | |
4111 | if (vd->vdev_ops->vdev_op_leaf) { | |
4112 | uint64_t commit_txg = txg; | |
572e2857 | 4113 | if (flags & ZIO_FLAG_SCAN_THREAD) { |
fb5f0bc8 BB |
4114 | ASSERT(flags & ZIO_FLAG_IO_REPAIR); |
4115 | ASSERT(spa_sync_pass(spa) == 1); | |
4116 | vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); | |
428870ff BB |
4117 | commit_txg = spa_syncing_txg(spa); |
4118 | } else if (spa->spa_claiming) { | |
4119 | ASSERT(flags & ZIO_FLAG_IO_REPAIR); | |
4120 | commit_txg = spa_first_txg(spa); | |
fb5f0bc8 | 4121 | } |
428870ff | 4122 | ASSERT(commit_txg >= spa_syncing_txg(spa)); |
fb5f0bc8 | 4123 | if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) |
34dc7c2f | 4124 | return; |
fb5f0bc8 BB |
4125 | for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) |
4126 | vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); | |
4127 | vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); | |
34dc7c2f | 4128 | } |
fb5f0bc8 BB |
4129 | if (vd != rvd) |
4130 | vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); | |
34dc7c2f BB |
4131 | } |
4132 | } | |
4133 | ||
cc99f275 DB |
4134 | int64_t |
4135 | vdev_deflated_space(vdev_t *vd, int64_t space) | |
4136 | { | |
4137 | ASSERT((space & (SPA_MINBLOCKSIZE-1)) == 0); | |
4138 | ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); | |
4139 | ||
4140 | return ((space >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio); | |
4141 | } | |
4142 | ||
34dc7c2f | 4143 | /* |
cc99f275 | 4144 | * Update the in-core space usage stats for this vdev and the root vdev. |
34dc7c2f BB |
4145 | */ |
4146 | void | |
428870ff BB |
4147 | vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, |
4148 | int64_t space_delta) | |
34dc7c2f | 4149 | { |
cc99f275 | 4150 | int64_t dspace_delta; |
34dc7c2f BB |
4151 | spa_t *spa = vd->vdev_spa; |
4152 | vdev_t *rvd = spa->spa_root_vdev; | |
4153 | ||
4154 | ASSERT(vd == vd->vdev_top); | |
4155 | ||
4156 | /* | |
4157 | * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion | |
4158 | * factor. We must calculate this here and not at the root vdev | |
4159 | * because the root vdev's psize-to-asize is simply the max of its | |
4160 | * childrens', thus not accurate enough for us. | |
4161 | */ | |
cc99f275 | 4162 | dspace_delta = vdev_deflated_space(vd, space_delta); |
34dc7c2f BB |
4163 | |
4164 | mutex_enter(&vd->vdev_stat_lock); | |
7558997d SD |
4165 | /* ensure we won't underflow */ |
4166 | if (alloc_delta < 0) { | |
4167 | ASSERT3U(vd->vdev_stat.vs_alloc, >=, -alloc_delta); | |
4168 | } | |
4169 | ||
34dc7c2f | 4170 | vd->vdev_stat.vs_alloc += alloc_delta; |
428870ff | 4171 | vd->vdev_stat.vs_space += space_delta; |
34dc7c2f BB |
4172 | vd->vdev_stat.vs_dspace += dspace_delta; |
4173 | mutex_exit(&vd->vdev_stat_lock); | |
4174 | ||
cc99f275 DB |
4175 | /* every class but log contributes to root space stats */ |
4176 | if (vd->vdev_mg != NULL && !vd->vdev_islog) { | |
7558997d | 4177 | ASSERT(!vd->vdev_isl2cache); |
34dc7c2f | 4178 | mutex_enter(&rvd->vdev_stat_lock); |
34dc7c2f | 4179 | rvd->vdev_stat.vs_alloc += alloc_delta; |
428870ff | 4180 | rvd->vdev_stat.vs_space += space_delta; |
34dc7c2f BB |
4181 | rvd->vdev_stat.vs_dspace += dspace_delta; |
4182 | mutex_exit(&rvd->vdev_stat_lock); | |
4183 | } | |
cc99f275 | 4184 | /* Note: metaslab_class_space_update moved to metaslab_space_update */ |
34dc7c2f BB |
4185 | } |
4186 | ||
4187 | /* | |
4188 | * Mark a top-level vdev's config as dirty, placing it on the dirty list | |
4189 | * so that it will be written out next time the vdev configuration is synced. | |
4190 | * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. | |
4191 | */ | |
4192 | void | |
4193 | vdev_config_dirty(vdev_t *vd) | |
4194 | { | |
4195 | spa_t *spa = vd->vdev_spa; | |
4196 | vdev_t *rvd = spa->spa_root_vdev; | |
4197 | int c; | |
4198 | ||
572e2857 BB |
4199 | ASSERT(spa_writeable(spa)); |
4200 | ||
34dc7c2f | 4201 | /* |
9babb374 BB |
4202 | * If this is an aux vdev (as with l2cache and spare devices), then we |
4203 | * update the vdev config manually and set the sync flag. | |
b128c09f BB |
4204 | */ |
4205 | if (vd->vdev_aux != NULL) { | |
4206 | spa_aux_vdev_t *sav = vd->vdev_aux; | |
4207 | nvlist_t **aux; | |
4208 | uint_t naux; | |
4209 | ||
4210 | for (c = 0; c < sav->sav_count; c++) { | |
4211 | if (sav->sav_vdevs[c] == vd) | |
4212 | break; | |
4213 | } | |
4214 | ||
4215 | if (c == sav->sav_count) { | |
4216 | /* | |
4217 | * We're being removed. There's nothing more to do. | |
4218 | */ | |
4219 | ASSERT(sav->sav_sync == B_TRUE); | |
4220 | return; | |
4221 | } | |
4222 | ||
4223 | sav->sav_sync = B_TRUE; | |
4224 | ||
9babb374 BB |
4225 | if (nvlist_lookup_nvlist_array(sav->sav_config, |
4226 | ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { | |
4227 | VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, | |
4228 | ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); | |
4229 | } | |
b128c09f BB |
4230 | |
4231 | ASSERT(c < naux); | |
4232 | ||
4233 | /* | |
4234 | * Setting the nvlist in the middle if the array is a little | |
4235 | * sketchy, but it will work. | |
4236 | */ | |
4237 | nvlist_free(aux[c]); | |
428870ff | 4238 | aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); |
b128c09f BB |
4239 | |
4240 | return; | |
4241 | } | |
4242 | ||
4243 | /* | |
4244 | * The dirty list is protected by the SCL_CONFIG lock. The caller | |
4245 | * must either hold SCL_CONFIG as writer, or must be the sync thread | |
4246 | * (which holds SCL_CONFIG as reader). There's only one sync thread, | |
34dc7c2f BB |
4247 | * so this is sufficient to ensure mutual exclusion. |
4248 | */ | |
b128c09f BB |
4249 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || |
4250 | (dsl_pool_sync_context(spa_get_dsl(spa)) && | |
4251 | spa_config_held(spa, SCL_CONFIG, RW_READER))); | |
34dc7c2f BB |
4252 | |
4253 | if (vd == rvd) { | |
4254 | for (c = 0; c < rvd->vdev_children; c++) | |
4255 | vdev_config_dirty(rvd->vdev_child[c]); | |
4256 | } else { | |
4257 | ASSERT(vd == vd->vdev_top); | |
4258 | ||
428870ff | 4259 | if (!list_link_active(&vd->vdev_config_dirty_node) && |
a1d477c2 | 4260 | vdev_is_concrete(vd)) { |
b128c09f | 4261 | list_insert_head(&spa->spa_config_dirty_list, vd); |
a1d477c2 | 4262 | } |
34dc7c2f BB |
4263 | } |
4264 | } | |
4265 | ||
4266 | void | |
4267 | vdev_config_clean(vdev_t *vd) | |
4268 | { | |
4269 | spa_t *spa = vd->vdev_spa; | |
4270 | ||
b128c09f BB |
4271 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || |
4272 | (dsl_pool_sync_context(spa_get_dsl(spa)) && | |
4273 | spa_config_held(spa, SCL_CONFIG, RW_READER))); | |
34dc7c2f | 4274 | |
b128c09f BB |
4275 | ASSERT(list_link_active(&vd->vdev_config_dirty_node)); |
4276 | list_remove(&spa->spa_config_dirty_list, vd); | |
34dc7c2f BB |
4277 | } |
4278 | ||
b128c09f BB |
4279 | /* |
4280 | * Mark a top-level vdev's state as dirty, so that the next pass of | |
4281 | * spa_sync() can convert this into vdev_config_dirty(). We distinguish | |
4282 | * the state changes from larger config changes because they require | |
4283 | * much less locking, and are often needed for administrative actions. | |
4284 | */ | |
4285 | void | |
4286 | vdev_state_dirty(vdev_t *vd) | |
4287 | { | |
4288 | spa_t *spa = vd->vdev_spa; | |
4289 | ||
572e2857 | 4290 | ASSERT(spa_writeable(spa)); |
b128c09f BB |
4291 | ASSERT(vd == vd->vdev_top); |
4292 | ||
4293 | /* | |
4294 | * The state list is protected by the SCL_STATE lock. The caller | |
4295 | * must either hold SCL_STATE as writer, or must be the sync thread | |
4296 | * (which holds SCL_STATE as reader). There's only one sync thread, | |
4297 | * so this is sufficient to ensure mutual exclusion. | |
4298 | */ | |
4299 | ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || | |
4300 | (dsl_pool_sync_context(spa_get_dsl(spa)) && | |
4301 | spa_config_held(spa, SCL_STATE, RW_READER))); | |
4302 | ||
a1d477c2 MA |
4303 | if (!list_link_active(&vd->vdev_state_dirty_node) && |
4304 | vdev_is_concrete(vd)) | |
b128c09f BB |
4305 | list_insert_head(&spa->spa_state_dirty_list, vd); |
4306 | } | |
4307 | ||
4308 | void | |
4309 | vdev_state_clean(vdev_t *vd) | |
4310 | { | |
4311 | spa_t *spa = vd->vdev_spa; | |
4312 | ||
4313 | ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || | |
4314 | (dsl_pool_sync_context(spa_get_dsl(spa)) && | |
4315 | spa_config_held(spa, SCL_STATE, RW_READER))); | |
4316 | ||
4317 | ASSERT(list_link_active(&vd->vdev_state_dirty_node)); | |
4318 | list_remove(&spa->spa_state_dirty_list, vd); | |
4319 | } | |
4320 | ||
4321 | /* | |
4322 | * Propagate vdev state up from children to parent. | |
4323 | */ | |
34dc7c2f BB |
4324 | void |
4325 | vdev_propagate_state(vdev_t *vd) | |
4326 | { | |
fb5f0bc8 BB |
4327 | spa_t *spa = vd->vdev_spa; |
4328 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f BB |
4329 | int degraded = 0, faulted = 0; |
4330 | int corrupted = 0; | |
34dc7c2f BB |
4331 | vdev_t *child; |
4332 | ||
4333 | if (vd->vdev_children > 0) { | |
1c27024e | 4334 | for (int c = 0; c < vd->vdev_children; c++) { |
34dc7c2f | 4335 | child = vd->vdev_child[c]; |
b128c09f | 4336 | |
428870ff | 4337 | /* |
a1d477c2 MA |
4338 | * Don't factor holes or indirect vdevs into the |
4339 | * decision. | |
428870ff | 4340 | */ |
a1d477c2 | 4341 | if (!vdev_is_concrete(child)) |
428870ff BB |
4342 | continue; |
4343 | ||
b128c09f | 4344 | if (!vdev_readable(child) || |
fb5f0bc8 | 4345 | (!vdev_writeable(child) && spa_writeable(spa))) { |
b128c09f BB |
4346 | /* |
4347 | * Root special: if there is a top-level log | |
4348 | * device, treat the root vdev as if it were | |
4349 | * degraded. | |
4350 | */ | |
4351 | if (child->vdev_islog && vd == rvd) | |
4352 | degraded++; | |
4353 | else | |
4354 | faulted++; | |
4355 | } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { | |
34dc7c2f | 4356 | degraded++; |
b128c09f | 4357 | } |
34dc7c2f BB |
4358 | |
4359 | if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) | |
4360 | corrupted++; | |
4361 | } | |
4362 | ||
4363 | vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); | |
4364 | ||
4365 | /* | |
b128c09f | 4366 | * Root special: if there is a top-level vdev that cannot be |
34dc7c2f BB |
4367 | * opened due to corrupted metadata, then propagate the root |
4368 | * vdev's aux state as 'corrupt' rather than 'insufficient | |
4369 | * replicas'. | |
4370 | */ | |
4371 | if (corrupted && vd == rvd && | |
4372 | rvd->vdev_state == VDEV_STATE_CANT_OPEN) | |
4373 | vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, | |
4374 | VDEV_AUX_CORRUPT_DATA); | |
4375 | } | |
4376 | ||
b128c09f | 4377 | if (vd->vdev_parent) |
34dc7c2f BB |
4378 | vdev_propagate_state(vd->vdev_parent); |
4379 | } | |
4380 | ||
4381 | /* | |
4382 | * Set a vdev's state. If this is during an open, we don't update the parent | |
4383 | * state, because we're in the process of opening children depth-first. | |
4384 | * Otherwise, we propagate the change to the parent. | |
4385 | * | |
4386 | * If this routine places a device in a faulted state, an appropriate ereport is | |
4387 | * generated. | |
4388 | */ | |
4389 | void | |
4390 | vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) | |
4391 | { | |
4392 | uint64_t save_state; | |
b128c09f | 4393 | spa_t *spa = vd->vdev_spa; |
34dc7c2f BB |
4394 | |
4395 | if (state == vd->vdev_state) { | |
976246fa DB |
4396 | /* |
4397 | * Since vdev_offline() code path is already in an offline | |
4398 | * state we can miss a statechange event to OFFLINE. Check | |
4399 | * the previous state to catch this condition. | |
4400 | */ | |
4401 | if (vd->vdev_ops->vdev_op_leaf && | |
4402 | (state == VDEV_STATE_OFFLINE) && | |
4403 | (vd->vdev_prevstate >= VDEV_STATE_FAULTED)) { | |
4404 | /* post an offline state change */ | |
4405 | zfs_post_state_change(spa, vd, vd->vdev_prevstate); | |
4406 | } | |
34dc7c2f BB |
4407 | vd->vdev_stat.vs_aux = aux; |
4408 | return; | |
4409 | } | |
4410 | ||
4411 | save_state = vd->vdev_state; | |
4412 | ||
4413 | vd->vdev_state = state; | |
4414 | vd->vdev_stat.vs_aux = aux; | |
4415 | ||
4416 | /* | |
4417 | * If we are setting the vdev state to anything but an open state, then | |
428870ff BB |
4418 | * always close the underlying device unless the device has requested |
4419 | * a delayed close (i.e. we're about to remove or fault the device). | |
4420 | * Otherwise, we keep accessible but invalid devices open forever. | |
4421 | * We don't call vdev_close() itself, because that implies some extra | |
4422 | * checks (offline, etc) that we don't want here. This is limited to | |
4423 | * leaf devices, because otherwise closing the device will affect other | |
4424 | * children. | |
34dc7c2f | 4425 | */ |
428870ff BB |
4426 | if (!vd->vdev_delayed_close && vdev_is_dead(vd) && |
4427 | vd->vdev_ops->vdev_op_leaf) | |
34dc7c2f BB |
4428 | vd->vdev_ops->vdev_op_close(vd); |
4429 | ||
4430 | if (vd->vdev_removed && | |
4431 | state == VDEV_STATE_CANT_OPEN && | |
4432 | (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { | |
4433 | /* | |
4434 | * If the previous state is set to VDEV_STATE_REMOVED, then this | |
4435 | * device was previously marked removed and someone attempted to | |
4436 | * reopen it. If this failed due to a nonexistent device, then | |
4437 | * keep the device in the REMOVED state. We also let this be if | |
4438 | * it is one of our special test online cases, which is only | |
4439 | * attempting to online the device and shouldn't generate an FMA | |
4440 | * fault. | |
4441 | */ | |
4442 | vd->vdev_state = VDEV_STATE_REMOVED; | |
4443 | vd->vdev_stat.vs_aux = VDEV_AUX_NONE; | |
4444 | } else if (state == VDEV_STATE_REMOVED) { | |
34dc7c2f BB |
4445 | vd->vdev_removed = B_TRUE; |
4446 | } else if (state == VDEV_STATE_CANT_OPEN) { | |
4447 | /* | |
572e2857 BB |
4448 | * If we fail to open a vdev during an import or recovery, we |
4449 | * mark it as "not available", which signifies that it was | |
4450 | * never there to begin with. Failure to open such a device | |
4451 | * is not considered an error. | |
34dc7c2f | 4452 | */ |
572e2857 BB |
4453 | if ((spa_load_state(spa) == SPA_LOAD_IMPORT || |
4454 | spa_load_state(spa) == SPA_LOAD_RECOVER) && | |
34dc7c2f BB |
4455 | vd->vdev_ops->vdev_op_leaf) |
4456 | vd->vdev_not_present = 1; | |
4457 | ||
4458 | /* | |
4459 | * Post the appropriate ereport. If the 'prevstate' field is | |
4460 | * set to something other than VDEV_STATE_UNKNOWN, it indicates | |
4461 | * that this is part of a vdev_reopen(). In this case, we don't | |
4462 | * want to post the ereport if the device was already in the | |
4463 | * CANT_OPEN state beforehand. | |
4464 | * | |
4465 | * If the 'checkremove' flag is set, then this is an attempt to | |
4466 | * online the device in response to an insertion event. If we | |
4467 | * hit this case, then we have detected an insertion event for a | |
4468 | * faulted or offline device that wasn't in the removed state. | |
4469 | * In this scenario, we don't post an ereport because we are | |
4470 | * about to replace the device, or attempt an online with | |
4471 | * vdev_forcefault, which will generate the fault for us. | |
4472 | */ | |
4473 | if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && | |
4474 | !vd->vdev_not_present && !vd->vdev_checkremove && | |
b128c09f | 4475 | vd != spa->spa_root_vdev) { |
34dc7c2f BB |
4476 | const char *class; |
4477 | ||
4478 | switch (aux) { | |
4479 | case VDEV_AUX_OPEN_FAILED: | |
4480 | class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; | |
4481 | break; | |
4482 | case VDEV_AUX_CORRUPT_DATA: | |
4483 | class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; | |
4484 | break; | |
4485 | case VDEV_AUX_NO_REPLICAS: | |
4486 | class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; | |
4487 | break; | |
4488 | case VDEV_AUX_BAD_GUID_SUM: | |
4489 | class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; | |
4490 | break; | |
4491 | case VDEV_AUX_TOO_SMALL: | |
4492 | class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; | |
4493 | break; | |
4494 | case VDEV_AUX_BAD_LABEL: | |
4495 | class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; | |
4496 | break; | |
ff61d1a4 | 4497 | case VDEV_AUX_BAD_ASHIFT: |
4498 | class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT; | |
4499 | break; | |
34dc7c2f BB |
4500 | default: |
4501 | class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; | |
4502 | } | |
4503 | ||
b5256303 TC |
4504 | zfs_ereport_post(class, spa, vd, NULL, NULL, |
4505 | save_state, 0); | |
34dc7c2f BB |
4506 | } |
4507 | ||
4508 | /* Erase any notion of persistent removed state */ | |
4509 | vd->vdev_removed = B_FALSE; | |
4510 | } else { | |
4511 | vd->vdev_removed = B_FALSE; | |
4512 | } | |
4513 | ||
d02ca379 DB |
4514 | /* |
4515 | * Notify ZED of any significant state-change on a leaf vdev. | |
4516 | * | |
d02ca379 | 4517 | */ |
6078881a TH |
4518 | if (vd->vdev_ops->vdev_op_leaf) { |
4519 | /* preserve original state from a vdev_reopen() */ | |
4520 | if ((vd->vdev_prevstate != VDEV_STATE_UNKNOWN) && | |
4521 | (vd->vdev_prevstate != vd->vdev_state) && | |
4522 | (save_state <= VDEV_STATE_CLOSED)) | |
4523 | save_state = vd->vdev_prevstate; | |
4524 | ||
4525 | /* filter out state change due to initial vdev_open */ | |
4526 | if (save_state > VDEV_STATE_CLOSED) | |
4527 | zfs_post_state_change(spa, vd, save_state); | |
d02ca379 DB |
4528 | } |
4529 | ||
9babb374 BB |
4530 | if (!isopen && vd->vdev_parent) |
4531 | vdev_propagate_state(vd->vdev_parent); | |
34dc7c2f | 4532 | } |
b128c09f | 4533 | |
6cb8e530 PZ |
4534 | boolean_t |
4535 | vdev_children_are_offline(vdev_t *vd) | |
4536 | { | |
4537 | ASSERT(!vd->vdev_ops->vdev_op_leaf); | |
4538 | ||
4539 | for (uint64_t i = 0; i < vd->vdev_children; i++) { | |
4540 | if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE) | |
4541 | return (B_FALSE); | |
4542 | } | |
4543 | ||
4544 | return (B_TRUE); | |
4545 | } | |
4546 | ||
b128c09f BB |
4547 | /* |
4548 | * Check the vdev configuration to ensure that it's capable of supporting | |
e550644f | 4549 | * a root pool. We do not support partial configuration. |
b128c09f BB |
4550 | */ |
4551 | boolean_t | |
4552 | vdev_is_bootable(vdev_t *vd) | |
4553 | { | |
b128c09f | 4554 | if (!vd->vdev_ops->vdev_op_leaf) { |
e550644f | 4555 | const char *vdev_type = vd->vdev_ops->vdev_op_type; |
b128c09f | 4556 | |
a1d477c2 MA |
4557 | if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 || |
4558 | strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) { | |
b128c09f | 4559 | return (B_FALSE); |
a1d477c2 | 4560 | } |
b128c09f BB |
4561 | } |
4562 | ||
e550644f | 4563 | for (int c = 0; c < vd->vdev_children; c++) { |
b128c09f BB |
4564 | if (!vdev_is_bootable(vd->vdev_child[c])) |
4565 | return (B_FALSE); | |
4566 | } | |
4567 | return (B_TRUE); | |
4568 | } | |
9babb374 | 4569 | |
a1d477c2 MA |
4570 | boolean_t |
4571 | vdev_is_concrete(vdev_t *vd) | |
4572 | { | |
4573 | vdev_ops_t *ops = vd->vdev_ops; | |
4574 | if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops || | |
4575 | ops == &vdev_missing_ops || ops == &vdev_root_ops) { | |
4576 | return (B_FALSE); | |
4577 | } else { | |
4578 | return (B_TRUE); | |
4579 | } | |
4580 | } | |
4581 | ||
572e2857 BB |
4582 | /* |
4583 | * Determine if a log device has valid content. If the vdev was | |
4584 | * removed or faulted in the MOS config then we know that | |
4585 | * the content on the log device has already been written to the pool. | |
4586 | */ | |
4587 | boolean_t | |
4588 | vdev_log_state_valid(vdev_t *vd) | |
4589 | { | |
4590 | if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && | |
4591 | !vd->vdev_removed) | |
4592 | return (B_TRUE); | |
4593 | ||
1c27024e | 4594 | for (int c = 0; c < vd->vdev_children; c++) |
572e2857 BB |
4595 | if (vdev_log_state_valid(vd->vdev_child[c])) |
4596 | return (B_TRUE); | |
4597 | ||
4598 | return (B_FALSE); | |
4599 | } | |
4600 | ||
9babb374 BB |
4601 | /* |
4602 | * Expand a vdev if possible. | |
4603 | */ | |
4604 | void | |
4605 | vdev_expand(vdev_t *vd, uint64_t txg) | |
4606 | { | |
4607 | ASSERT(vd->vdev_top == vd); | |
4608 | ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); | |
7637ef8d | 4609 | ASSERT(vdev_is_concrete(vd)); |
9babb374 | 4610 | |
a1d477c2 MA |
4611 | vdev_set_deflate_ratio(vd); |
4612 | ||
cc99f275 DB |
4613 | if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count && |
4614 | vdev_is_concrete(vd)) { | |
4615 | vdev_metaslab_group_create(vd); | |
9babb374 BB |
4616 | VERIFY(vdev_metaslab_init(vd, txg) == 0); |
4617 | vdev_config_dirty(vd); | |
4618 | } | |
4619 | } | |
428870ff BB |
4620 | |
4621 | /* | |
4622 | * Split a vdev. | |
4623 | */ | |
4624 | void | |
4625 | vdev_split(vdev_t *vd) | |
4626 | { | |
4627 | vdev_t *cvd, *pvd = vd->vdev_parent; | |
4628 | ||
4629 | vdev_remove_child(pvd, vd); | |
4630 | vdev_compact_children(pvd); | |
4631 | ||
4632 | cvd = pvd->vdev_child[0]; | |
4633 | if (pvd->vdev_children == 1) { | |
4634 | vdev_remove_parent(cvd); | |
4635 | cvd->vdev_splitting = B_TRUE; | |
4636 | } | |
4637 | vdev_propagate_state(cvd); | |
4638 | } | |
c28b2279 | 4639 | |
cc92e9d0 | 4640 | void |
8fb1ede1 | 4641 | vdev_deadman(vdev_t *vd, char *tag) |
cc92e9d0 | 4642 | { |
1c27024e | 4643 | for (int c = 0; c < vd->vdev_children; c++) { |
cc92e9d0 GW |
4644 | vdev_t *cvd = vd->vdev_child[c]; |
4645 | ||
8fb1ede1 | 4646 | vdev_deadman(cvd, tag); |
cc92e9d0 GW |
4647 | } |
4648 | ||
4649 | if (vd->vdev_ops->vdev_op_leaf) { | |
4650 | vdev_queue_t *vq = &vd->vdev_queue; | |
4651 | ||
4652 | mutex_enter(&vq->vq_lock); | |
e8b96c60 | 4653 | if (avl_numnodes(&vq->vq_active_tree) > 0) { |
cc92e9d0 GW |
4654 | spa_t *spa = vd->vdev_spa; |
4655 | zio_t *fio; | |
4656 | uint64_t delta; | |
4657 | ||
8fb1ede1 BB |
4658 | zfs_dbgmsg("slow vdev: %s has %d active IOs", |
4659 | vd->vdev_path, avl_numnodes(&vq->vq_active_tree)); | |
4660 | ||
cc92e9d0 GW |
4661 | /* |
4662 | * Look at the head of all the pending queues, | |
4663 | * if any I/O has been outstanding for longer than | |
8fb1ede1 | 4664 | * the spa_deadman_synctime invoke the deadman logic. |
cc92e9d0 | 4665 | */ |
e8b96c60 | 4666 | fio = avl_first(&vq->vq_active_tree); |
cb682a17 | 4667 | delta = gethrtime() - fio->io_timestamp; |
8fb1ede1 BB |
4668 | if (delta > spa_deadman_synctime(spa)) |
4669 | zio_deadman(fio, tag); | |
cc92e9d0 GW |
4670 | } |
4671 | mutex_exit(&vq->vq_lock); | |
4672 | } | |
4673 | } | |
4674 | ||
80a91e74 TC |
4675 | void |
4676 | vdev_set_deferred_resilver(spa_t *spa, vdev_t *vd) | |
4677 | { | |
4021ba4c TC |
4678 | for (uint64_t i = 0; i < vd->vdev_children; i++) |
4679 | vdev_set_deferred_resilver(spa, vd->vdev_child[i]); | |
4680 | ||
4681 | if (!vd->vdev_ops->vdev_op_leaf || !vdev_writeable(vd) || | |
4682 | range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) { | |
4683 | return; | |
4684 | } | |
4685 | ||
80a91e74 TC |
4686 | vd->vdev_resilver_deferred = B_TRUE; |
4687 | spa->spa_resilver_deferred = B_TRUE; | |
4688 | } | |
4689 | ||
93ce2b4c | 4690 | #if defined(_KERNEL) |
c28b2279 BB |
4691 | EXPORT_SYMBOL(vdev_fault); |
4692 | EXPORT_SYMBOL(vdev_degrade); | |
4693 | EXPORT_SYMBOL(vdev_online); | |
4694 | EXPORT_SYMBOL(vdev_offline); | |
4695 | EXPORT_SYMBOL(vdev_clear); | |
4ea3f864 | 4696 | /* BEGIN CSTYLED */ |
c853f382 SD |
4697 | module_param(zfs_vdev_default_ms_count, int, 0644); |
4698 | MODULE_PARM_DESC(zfs_vdev_default_ms_count, | |
e4e94ca3 | 4699 | "Target number of metaslabs per top-level vdev"); |
80d52c39 | 4700 | |
c853f382 SD |
4701 | module_param(zfs_vdev_min_ms_count, int, 0644); |
4702 | MODULE_PARM_DESC(zfs_vdev_min_ms_count, | |
d2734cce SD |
4703 | "Minimum number of metaslabs per top-level vdev"); |
4704 | ||
c853f382 SD |
4705 | module_param(zfs_vdev_ms_count_limit, int, 0644); |
4706 | MODULE_PARM_DESC(zfs_vdev_ms_count_limit, | |
e4e94ca3 DB |
4707 | "Practical upper limit of total metaslabs per top-level vdev"); |
4708 | ||
ad796b8a TH |
4709 | module_param(zfs_slow_io_events_per_second, uint, 0644); |
4710 | MODULE_PARM_DESC(zfs_slow_io_events_per_second, | |
4711 | "Rate limit slow IO (delay) events to this many per second"); | |
80d52c39 | 4712 | |
ad796b8a TH |
4713 | module_param(zfs_checksum_events_per_second, uint, 0644); |
4714 | MODULE_PARM_DESC(zfs_checksum_events_per_second, "Rate limit checksum events " | |
80d52c39 TH |
4715 | "to this many checksum errors per second (do not set below zed" |
4716 | "threshold)."); | |
02638a30 TC |
4717 | |
4718 | module_param(zfs_scan_ignore_errors, int, 0644); | |
4719 | MODULE_PARM_DESC(zfs_scan_ignore_errors, | |
4720 | "Ignore errors during resilver/scrub"); | |
6cb8e530 PZ |
4721 | |
4722 | module_param(vdev_validate_skip, int, 0644); | |
4723 | MODULE_PARM_DESC(vdev_validate_skip, | |
4724 | "Bypass vdev_validate()"); | |
53b1f5ea PS |
4725 | |
4726 | module_param(zfs_nocacheflush, int, 0644); | |
4727 | MODULE_PARM_DESC(zfs_nocacheflush, "Disable cache flushes"); | |
4ea3f864 | 4728 | /* END CSTYLED */ |
c28b2279 | 4729 | #endif |