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