]>
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 | * Copyright 2007 Sun Microsystems, Inc. All rights reserved. | |
23 | * Use is subject to license terms. | |
24 | */ | |
25 | ||
26 | #pragma ident "@(#)vdev_mirror.c 1.9 07/11/27 SMI" | |
27 | ||
28 | #include <sys/zfs_context.h> | |
29 | #include <sys/spa.h> | |
30 | #include <sys/vdev_impl.h> | |
31 | #include <sys/zio.h> | |
32 | #include <sys/fs/zfs.h> | |
33 | ||
34 | /* | |
35 | * Virtual device vector for mirroring. | |
36 | */ | |
37 | ||
38 | typedef struct mirror_child { | |
39 | vdev_t *mc_vd; | |
40 | uint64_t mc_offset; | |
41 | int mc_error; | |
42 | short mc_tried; | |
43 | short mc_skipped; | |
44 | } mirror_child_t; | |
45 | ||
46 | typedef struct mirror_map { | |
47 | int mm_children; | |
48 | int mm_replacing; | |
49 | int mm_preferred; | |
50 | int mm_root; | |
51 | mirror_child_t mm_child[1]; | |
52 | } mirror_map_t; | |
53 | ||
54 | int vdev_mirror_shift = 21; | |
55 | ||
56 | static mirror_map_t * | |
57 | vdev_mirror_map_alloc(zio_t *zio) | |
58 | { | |
59 | mirror_map_t *mm = NULL; | |
60 | mirror_child_t *mc; | |
61 | vdev_t *vd = zio->io_vd; | |
62 | int c, d; | |
63 | ||
64 | if (vd == NULL) { | |
65 | dva_t *dva = zio->io_bp->blk_dva; | |
66 | spa_t *spa = zio->io_spa; | |
67 | ||
68 | c = BP_GET_NDVAS(zio->io_bp); | |
69 | ||
70 | mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP); | |
71 | mm->mm_children = c; | |
72 | mm->mm_replacing = B_FALSE; | |
73 | mm->mm_preferred = spa_get_random(c); | |
74 | mm->mm_root = B_TRUE; | |
75 | ||
76 | /* | |
77 | * Check the other, lower-index DVAs to see if they're on | |
78 | * the same vdev as the child we picked. If they are, use | |
79 | * them since they are likely to have been allocated from | |
80 | * the primary metaslab in use at the time, and hence are | |
81 | * more likely to have locality with single-copy data. | |
82 | */ | |
83 | for (c = mm->mm_preferred, d = c - 1; d >= 0; d--) { | |
84 | if (DVA_GET_VDEV(&dva[d]) == DVA_GET_VDEV(&dva[c])) | |
85 | mm->mm_preferred = d; | |
86 | } | |
87 | ||
88 | for (c = 0; c < mm->mm_children; c++) { | |
89 | mc = &mm->mm_child[c]; | |
90 | ||
91 | mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c])); | |
92 | mc->mc_offset = DVA_GET_OFFSET(&dva[c]); | |
93 | } | |
94 | } else { | |
95 | c = vd->vdev_children; | |
96 | ||
97 | mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP); | |
98 | mm->mm_children = c; | |
99 | mm->mm_replacing = (vd->vdev_ops == &vdev_replacing_ops || | |
100 | vd->vdev_ops == &vdev_spare_ops); | |
101 | mm->mm_preferred = mm->mm_replacing ? 0 : | |
102 | (zio->io_offset >> vdev_mirror_shift) % c; | |
103 | mm->mm_root = B_FALSE; | |
104 | ||
105 | for (c = 0; c < mm->mm_children; c++) { | |
106 | mc = &mm->mm_child[c]; | |
107 | mc->mc_vd = vd->vdev_child[c]; | |
108 | mc->mc_offset = zio->io_offset; | |
109 | } | |
110 | } | |
111 | ||
112 | zio->io_vsd = mm; | |
113 | return (mm); | |
114 | } | |
115 | ||
116 | static void | |
117 | vdev_mirror_map_free(zio_t *zio) | |
118 | { | |
119 | mirror_map_t *mm = zio->io_vsd; | |
120 | ||
121 | kmem_free(mm, offsetof(mirror_map_t, mm_child[mm->mm_children])); | |
122 | zio->io_vsd = NULL; | |
123 | } | |
124 | ||
125 | static int | |
126 | vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *ashift) | |
127 | { | |
128 | vdev_t *cvd; | |
129 | uint64_t c; | |
130 | int numerrors = 0; | |
131 | int ret, lasterror = 0; | |
132 | ||
133 | if (vd->vdev_children == 0) { | |
134 | vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; | |
135 | return (EINVAL); | |
136 | } | |
137 | ||
138 | for (c = 0; c < vd->vdev_children; c++) { | |
139 | cvd = vd->vdev_child[c]; | |
140 | ||
141 | if ((ret = vdev_open(cvd)) != 0) { | |
142 | lasterror = ret; | |
143 | numerrors++; | |
144 | continue; | |
145 | } | |
146 | ||
147 | *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1; | |
148 | *ashift = MAX(*ashift, cvd->vdev_ashift); | |
149 | } | |
150 | ||
151 | if (numerrors == vd->vdev_children) { | |
152 | vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS; | |
153 | return (lasterror); | |
154 | } | |
155 | ||
156 | return (0); | |
157 | } | |
158 | ||
159 | static void | |
160 | vdev_mirror_close(vdev_t *vd) | |
161 | { | |
162 | uint64_t c; | |
163 | ||
164 | for (c = 0; c < vd->vdev_children; c++) | |
165 | vdev_close(vd->vdev_child[c]); | |
166 | } | |
167 | ||
168 | static void | |
169 | vdev_mirror_child_done(zio_t *zio) | |
170 | { | |
171 | mirror_child_t *mc = zio->io_private; | |
172 | ||
173 | mc->mc_error = zio->io_error; | |
174 | mc->mc_tried = 1; | |
175 | mc->mc_skipped = 0; | |
176 | } | |
177 | ||
178 | static void | |
179 | vdev_mirror_scrub_done(zio_t *zio) | |
180 | { | |
181 | mirror_child_t *mc = zio->io_private; | |
182 | ||
183 | if (zio->io_error == 0) { | |
184 | zio_t *pio = zio->io_parent; | |
185 | mutex_enter(&pio->io_lock); | |
186 | ASSERT3U(zio->io_size, >=, pio->io_size); | |
187 | bcopy(zio->io_data, pio->io_data, pio->io_size); | |
188 | mutex_exit(&pio->io_lock); | |
189 | } | |
190 | ||
191 | zio_buf_free(zio->io_data, zio->io_size); | |
192 | ||
193 | mc->mc_error = zio->io_error; | |
194 | mc->mc_tried = 1; | |
195 | mc->mc_skipped = 0; | |
196 | } | |
197 | ||
198 | static void | |
199 | vdev_mirror_repair_done(zio_t *zio) | |
200 | { | |
201 | ASSERT(zio->io_private == zio->io_parent); | |
202 | vdev_mirror_map_free(zio->io_private); | |
203 | } | |
204 | ||
205 | /* | |
206 | * Try to find a child whose DTL doesn't contain the block we want to read. | |
207 | * If we can't, try the read on any vdev we haven't already tried. | |
208 | */ | |
209 | static int | |
210 | vdev_mirror_child_select(zio_t *zio) | |
211 | { | |
212 | mirror_map_t *mm = zio->io_vsd; | |
213 | mirror_child_t *mc; | |
214 | uint64_t txg = zio->io_txg; | |
215 | int i, c; | |
216 | ||
217 | ASSERT(zio->io_bp == NULL || zio->io_bp->blk_birth == txg); | |
218 | ||
219 | /* | |
220 | * Try to find a child whose DTL doesn't contain the block to read. | |
221 | * If a child is known to be completely inaccessible (indicated by | |
222 | * vdev_readable() returning B_FALSE), don't even try. | |
223 | */ | |
224 | for (i = 0, c = mm->mm_preferred; i < mm->mm_children; i++, c++) { | |
225 | if (c >= mm->mm_children) | |
226 | c = 0; | |
227 | mc = &mm->mm_child[c]; | |
228 | if (mc->mc_tried || mc->mc_skipped) | |
229 | continue; | |
230 | if (vdev_is_dead(mc->mc_vd) && !vdev_readable(mc->mc_vd)) { | |
231 | mc->mc_error = ENXIO; | |
232 | mc->mc_tried = 1; /* don't even try */ | |
233 | mc->mc_skipped = 1; | |
234 | continue; | |
235 | } | |
236 | if (!vdev_dtl_contains(&mc->mc_vd->vdev_dtl_map, txg, 1)) | |
237 | return (c); | |
238 | mc->mc_error = ESTALE; | |
239 | mc->mc_skipped = 1; | |
240 | } | |
241 | ||
242 | /* | |
243 | * Every device is either missing or has this txg in its DTL. | |
244 | * Look for any child we haven't already tried before giving up. | |
245 | */ | |
246 | for (c = 0; c < mm->mm_children; c++) | |
247 | if (!mm->mm_child[c].mc_tried) | |
248 | return (c); | |
249 | ||
250 | /* | |
251 | * Every child failed. There's no place left to look. | |
252 | */ | |
253 | return (-1); | |
254 | } | |
255 | ||
256 | static int | |
257 | vdev_mirror_io_start(zio_t *zio) | |
258 | { | |
259 | mirror_map_t *mm; | |
260 | mirror_child_t *mc; | |
261 | int c, children; | |
262 | ||
263 | mm = vdev_mirror_map_alloc(zio); | |
264 | ||
265 | if (zio->io_type == ZIO_TYPE_READ) { | |
266 | if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing) { | |
267 | /* | |
268 | * For scrubbing reads we need to allocate a read | |
269 | * buffer for each child and issue reads to all | |
270 | * children. If any child succeeds, it will copy its | |
271 | * data into zio->io_data in vdev_mirror_scrub_done. | |
272 | */ | |
273 | for (c = 0; c < mm->mm_children; c++) { | |
274 | mc = &mm->mm_child[c]; | |
275 | zio_nowait(zio_vdev_child_io(zio, zio->io_bp, | |
276 | mc->mc_vd, mc->mc_offset, | |
277 | zio_buf_alloc(zio->io_size), zio->io_size, | |
278 | zio->io_type, zio->io_priority, | |
279 | ZIO_FLAG_CANFAIL, | |
280 | vdev_mirror_scrub_done, mc)); | |
281 | } | |
282 | return (zio_wait_for_children_done(zio)); | |
283 | } | |
284 | /* | |
285 | * For normal reads just pick one child. | |
286 | */ | |
287 | c = vdev_mirror_child_select(zio); | |
288 | children = (c >= 0); | |
289 | } else { | |
290 | ASSERT(zio->io_type == ZIO_TYPE_WRITE); | |
291 | ||
292 | /* | |
293 | * If this is a resilvering I/O to a replacing vdev, | |
294 | * only the last child should be written -- unless the | |
295 | * first child happens to have a DTL entry here as well. | |
296 | * All other writes go to all children. | |
297 | */ | |
298 | if ((zio->io_flags & ZIO_FLAG_RESILVER) && mm->mm_replacing && | |
299 | !vdev_dtl_contains(&mm->mm_child[0].mc_vd->vdev_dtl_map, | |
300 | zio->io_txg, 1)) { | |
301 | c = mm->mm_children - 1; | |
302 | children = 1; | |
303 | } else { | |
304 | c = 0; | |
305 | children = mm->mm_children; | |
306 | } | |
307 | } | |
308 | ||
309 | while (children--) { | |
310 | mc = &mm->mm_child[c]; | |
311 | zio_nowait(zio_vdev_child_io(zio, zio->io_bp, | |
312 | mc->mc_vd, mc->mc_offset, | |
313 | zio->io_data, zio->io_size, zio->io_type, zio->io_priority, | |
314 | ZIO_FLAG_CANFAIL, vdev_mirror_child_done, mc)); | |
315 | c++; | |
316 | } | |
317 | ||
318 | return (zio_wait_for_children_done(zio)); | |
319 | } | |
320 | ||
321 | static int | |
322 | vdev_mirror_io_done(zio_t *zio) | |
323 | { | |
324 | mirror_map_t *mm = zio->io_vsd; | |
325 | mirror_child_t *mc; | |
326 | int c; | |
327 | int good_copies = 0; | |
328 | int unexpected_errors = 0; | |
329 | ||
330 | zio->io_error = 0; | |
331 | zio->io_numerrors = 0; | |
332 | ||
333 | for (c = 0; c < mm->mm_children; c++) { | |
334 | mc = &mm->mm_child[c]; | |
335 | ||
336 | if (mc->mc_tried && mc->mc_error == 0) { | |
337 | good_copies++; | |
338 | continue; | |
339 | } | |
340 | ||
341 | /* | |
342 | * We preserve any EIOs because those may be worth retrying; | |
343 | * whereas ECKSUM and ENXIO are more likely to be persistent. | |
344 | */ | |
345 | if (mc->mc_error) { | |
346 | if (zio->io_error != EIO) | |
347 | zio->io_error = mc->mc_error; | |
348 | if (!mc->mc_skipped) | |
349 | unexpected_errors++; | |
350 | zio->io_numerrors++; | |
351 | } | |
352 | } | |
353 | ||
354 | if (zio->io_type == ZIO_TYPE_WRITE) { | |
355 | /* | |
356 | * XXX -- for now, treat partial writes as success. | |
357 | * XXX -- For a replacing vdev, we need to make sure the | |
358 | * new child succeeds. | |
359 | */ | |
360 | /* XXPOLICY */ | |
361 | if (good_copies != 0) | |
362 | zio->io_error = 0; | |
363 | vdev_mirror_map_free(zio); | |
364 | return (ZIO_PIPELINE_CONTINUE); | |
365 | } | |
366 | ||
367 | ASSERT(zio->io_type == ZIO_TYPE_READ); | |
368 | ||
369 | /* | |
370 | * If we don't have a good copy yet, keep trying other children. | |
371 | */ | |
372 | /* XXPOLICY */ | |
373 | if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) { | |
374 | ASSERT(c >= 0 && c < mm->mm_children); | |
375 | mc = &mm->mm_child[c]; | |
376 | dprintf("retrying i/o (err=%d) on child %s\n", | |
377 | zio->io_error, vdev_description(mc->mc_vd)); | |
378 | zio->io_error = 0; | |
379 | zio_vdev_io_redone(zio); | |
380 | zio_nowait(zio_vdev_child_io(zio, zio->io_bp, | |
381 | mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size, | |
382 | ZIO_TYPE_READ, zio->io_priority, ZIO_FLAG_CANFAIL, | |
383 | vdev_mirror_child_done, mc)); | |
384 | return (zio_wait_for_children_done(zio)); | |
385 | } | |
386 | ||
387 | /* XXPOLICY */ | |
388 | if (good_copies) | |
389 | zio->io_error = 0; | |
390 | else | |
391 | ASSERT(zio->io_error != 0); | |
392 | ||
393 | if (good_copies && (spa_mode & FWRITE) && | |
394 | (unexpected_errors || | |
395 | (zio->io_flags & ZIO_FLAG_RESILVER) || | |
396 | ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) { | |
397 | zio_t *rio; | |
398 | ||
399 | /* | |
400 | * Use the good data we have in hand to repair damaged children. | |
401 | * | |
402 | * We issue all repair I/Os as children of 'rio' to arrange | |
403 | * that vdev_mirror_map_free(zio) will be invoked after all | |
404 | * repairs complete, but before we advance to the next stage. | |
405 | */ | |
406 | rio = zio_null(zio, zio->io_spa, | |
407 | vdev_mirror_repair_done, zio, ZIO_FLAG_CANFAIL); | |
408 | ||
409 | for (c = 0; c < mm->mm_children; c++) { | |
410 | /* | |
411 | * Don't rewrite known good children. | |
412 | * Not only is it unnecessary, it could | |
413 | * actually be harmful: if the system lost | |
414 | * power while rewriting the only good copy, | |
415 | * there would be no good copies left! | |
416 | */ | |
417 | mc = &mm->mm_child[c]; | |
418 | ||
419 | if (mc->mc_error == 0) { | |
420 | if (mc->mc_tried) | |
421 | continue; | |
422 | if (!(zio->io_flags & ZIO_FLAG_SCRUB) && | |
423 | !vdev_dtl_contains(&mc->mc_vd->vdev_dtl_map, | |
424 | zio->io_txg, 1)) | |
425 | continue; | |
426 | mc->mc_error = ESTALE; | |
427 | } | |
428 | ||
429 | dprintf("resilvered %s @ 0x%llx error %d\n", | |
430 | vdev_description(mc->mc_vd), mc->mc_offset, | |
431 | mc->mc_error); | |
432 | ||
433 | zio_nowait(zio_vdev_child_io(rio, zio->io_bp, mc->mc_vd, | |
434 | mc->mc_offset, zio->io_data, zio->io_size, | |
435 | ZIO_TYPE_WRITE, zio->io_priority, | |
436 | ZIO_FLAG_IO_REPAIR | ZIO_FLAG_CANFAIL | | |
437 | ZIO_FLAG_DONT_PROPAGATE, NULL, NULL)); | |
438 | } | |
439 | ||
440 | zio_nowait(rio); | |
441 | ||
442 | return (zio_wait_for_children_done(zio)); | |
443 | } | |
444 | ||
445 | vdev_mirror_map_free(zio); | |
446 | ||
447 | return (ZIO_PIPELINE_CONTINUE); | |
448 | } | |
449 | ||
450 | static void | |
451 | vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded) | |
452 | { | |
453 | if (faulted == vd->vdev_children) | |
454 | vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, | |
455 | VDEV_AUX_NO_REPLICAS); | |
456 | else if (degraded + faulted != 0) | |
457 | vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE); | |
458 | else | |
459 | vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE); | |
460 | } | |
461 | ||
462 | vdev_ops_t vdev_mirror_ops = { | |
463 | vdev_mirror_open, | |
464 | vdev_mirror_close, | |
465 | NULL, | |
466 | vdev_default_asize, | |
467 | vdev_mirror_io_start, | |
468 | vdev_mirror_io_done, | |
469 | vdev_mirror_state_change, | |
470 | VDEV_TYPE_MIRROR, /* name of this vdev type */ | |
471 | B_FALSE /* not a leaf vdev */ | |
472 | }; | |
473 | ||
474 | vdev_ops_t vdev_replacing_ops = { | |
475 | vdev_mirror_open, | |
476 | vdev_mirror_close, | |
477 | NULL, | |
478 | vdev_default_asize, | |
479 | vdev_mirror_io_start, | |
480 | vdev_mirror_io_done, | |
481 | vdev_mirror_state_change, | |
482 | VDEV_TYPE_REPLACING, /* name of this vdev type */ | |
483 | B_FALSE /* not a leaf vdev */ | |
484 | }; | |
485 | ||
486 | vdev_ops_t vdev_spare_ops = { | |
487 | vdev_mirror_open, | |
488 | vdev_mirror_close, | |
489 | NULL, | |
490 | vdev_default_asize, | |
491 | vdev_mirror_io_start, | |
492 | vdev_mirror_io_done, | |
493 | vdev_mirror_state_change, | |
494 | VDEV_TYPE_SPARE, /* name of this vdev type */ | |
495 | B_FALSE /* not a leaf vdev */ | |
496 | }; |