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