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