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