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