2 * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/kernel.h>
35 #include <sys/limits.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
40 #include <sys/systm.h>
41 #include <geom/geom.h>
42 #include "geom/raid/g_raid.h"
43 #include "g_raid_tr_if.h"
47 SYSCTL_DECL(_kern_geom_raid_raid1e);
49 #define RAID1E_REBUILD_SLAB (1 << 20) /* One transation in a rebuild */
50 static int g_raid1e_rebuild_slab = RAID1E_REBUILD_SLAB;
51 TUNABLE_INT("kern.geom.raid.raid1e.rebuild_slab_size",
52 &g_raid1e_rebuild_slab);
53 SYSCTL_UINT(_kern_geom_raid_raid1e, OID_AUTO, rebuild_slab_size, CTLFLAG_RW,
54 &g_raid1e_rebuild_slab, 0,
55 "Amount of the disk to rebuild each read/write cycle of the rebuild.");
57 #define RAID1E_REBUILD_FAIR_IO 20 /* use 1/x of the available I/O */
58 static int g_raid1e_rebuild_fair_io = RAID1E_REBUILD_FAIR_IO;
59 TUNABLE_INT("kern.geom.raid.raid1e.rebuild_fair_io",
60 &g_raid1e_rebuild_fair_io);
61 SYSCTL_UINT(_kern_geom_raid_raid1e, OID_AUTO, rebuild_fair_io, CTLFLAG_RW,
62 &g_raid1e_rebuild_fair_io, 0,
63 "Fraction of the I/O bandwidth to use when disk busy for rebuild.");
65 #define RAID1E_REBUILD_CLUSTER_IDLE 100
66 static int g_raid1e_rebuild_cluster_idle = RAID1E_REBUILD_CLUSTER_IDLE;
67 TUNABLE_INT("kern.geom.raid.raid1e.rebuild_cluster_idle",
68 &g_raid1e_rebuild_cluster_idle);
69 SYSCTL_UINT(_kern_geom_raid_raid1e, OID_AUTO, rebuild_cluster_idle, CTLFLAG_RW,
70 &g_raid1e_rebuild_cluster_idle, 0,
71 "Number of slabs to do each time we trigger a rebuild cycle");
73 #define RAID1E_REBUILD_META_UPDATE 1024 /* update meta data every 1GB or so */
74 static int g_raid1e_rebuild_meta_update = RAID1E_REBUILD_META_UPDATE;
75 TUNABLE_INT("kern.geom.raid.raid1e.rebuild_meta_update",
76 &g_raid1e_rebuild_meta_update);
77 SYSCTL_UINT(_kern_geom_raid_raid1e, OID_AUTO, rebuild_meta_update, CTLFLAG_RW,
78 &g_raid1e_rebuild_meta_update, 0,
79 "When to update the meta data.");
81 static MALLOC_DEFINE(M_TR_RAID1E, "tr_raid1e_data", "GEOM_RAID RAID1E data");
83 #define TR_RAID1E_NONE 0
84 #define TR_RAID1E_REBUILD 1
85 #define TR_RAID1E_RESYNC 2
87 #define TR_RAID1E_F_DOING_SOME 0x1
88 #define TR_RAID1E_F_LOCKED 0x2
89 #define TR_RAID1E_F_ABORT 0x4
91 struct g_raid_tr_raid1e_object {
92 struct g_raid_tr_object trso_base;
96 int trso_recover_slabs; /* slabs before rest */
100 struct g_raid_subdisk *trso_failed_sd; /* like per volume */
101 void *trso_buffer; /* Buffer space */
102 off_t trso_lock_pos; /* Locked range start. */
103 off_t trso_lock_len; /* Locked range length. */
107 static g_raid_tr_taste_t g_raid_tr_taste_raid1e;
108 static g_raid_tr_event_t g_raid_tr_event_raid1e;
109 static g_raid_tr_start_t g_raid_tr_start_raid1e;
110 static g_raid_tr_stop_t g_raid_tr_stop_raid1e;
111 static g_raid_tr_iostart_t g_raid_tr_iostart_raid1e;
112 static g_raid_tr_iodone_t g_raid_tr_iodone_raid1e;
113 static g_raid_tr_kerneldump_t g_raid_tr_kerneldump_raid1e;
114 static g_raid_tr_locked_t g_raid_tr_locked_raid1e;
115 static g_raid_tr_idle_t g_raid_tr_idle_raid1e;
116 static g_raid_tr_free_t g_raid_tr_free_raid1e;
118 static kobj_method_t g_raid_tr_raid1e_methods[] = {
119 KOBJMETHOD(g_raid_tr_taste, g_raid_tr_taste_raid1e),
120 KOBJMETHOD(g_raid_tr_event, g_raid_tr_event_raid1e),
121 KOBJMETHOD(g_raid_tr_start, g_raid_tr_start_raid1e),
122 KOBJMETHOD(g_raid_tr_stop, g_raid_tr_stop_raid1e),
123 KOBJMETHOD(g_raid_tr_iostart, g_raid_tr_iostart_raid1e),
124 KOBJMETHOD(g_raid_tr_iodone, g_raid_tr_iodone_raid1e),
125 KOBJMETHOD(g_raid_tr_kerneldump, g_raid_tr_kerneldump_raid1e),
126 KOBJMETHOD(g_raid_tr_locked, g_raid_tr_locked_raid1e),
127 KOBJMETHOD(g_raid_tr_idle, g_raid_tr_idle_raid1e),
128 KOBJMETHOD(g_raid_tr_free, g_raid_tr_free_raid1e),
132 static struct g_raid_tr_class g_raid_tr_raid1e_class = {
134 g_raid_tr_raid1e_methods,
135 sizeof(struct g_raid_tr_raid1e_object),
140 static void g_raid_tr_raid1e_rebuild_abort(struct g_raid_tr_object *tr);
141 static void g_raid_tr_raid1e_maybe_rebuild(struct g_raid_tr_object *tr,
142 struct g_raid_subdisk *sd);
143 static int g_raid_tr_raid1e_select_read_disk(struct g_raid_volume *vol,
144 int no, off_t off, off_t len, u_int mask);
147 V2P(struct g_raid_volume *vol, off_t virt,
148 int *disk, off_t *offset, off_t *start)
153 strip_size = vol->v_strip_size;
155 nstrip = virt / strip_size;
156 /* Start position in strip. */
157 *start = virt % strip_size;
159 *disk = (nstrip * N) % vol->v_disks_count;
160 /* Strip start position in disk. */
161 *offset = ((nstrip * N) / vol->v_disks_count) * strip_size;
165 P2V(struct g_raid_volume *vol, int disk, off_t offset,
166 off_t *virt, int *copy)
171 strip_size = vol->v_strip_size;
172 /* Start position in strip. */
173 start = offset % strip_size;
174 /* Physical strip number. */
175 nstrip = (offset / strip_size) * vol->v_disks_count + disk;
176 /* Number of physical strip (copy) inside virtual strip. */
178 /* Offset in virtual space. */
179 *virt = (nstrip / N) * strip_size + start;
183 g_raid_tr_taste_raid1e(struct g_raid_tr_object *tr, struct g_raid_volume *vol)
185 struct g_raid_tr_raid1e_object *trs;
187 trs = (struct g_raid_tr_raid1e_object *)tr;
188 if (tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_RAID1E ||
189 tr->tro_volume->v_raid_level_qualifier != G_RAID_VOLUME_RLQ_R1EA)
190 return (G_RAID_TR_TASTE_FAIL);
191 trs->trso_starting = 1;
192 return (G_RAID_TR_TASTE_SUCCEED);
196 g_raid_tr_update_state_raid1e_even(struct g_raid_volume *vol)
198 struct g_raid_softc *sc;
199 struct g_raid_subdisk *sd, *bestsd, *worstsd;
200 int i, j, state, sstate;
203 state = G_RAID_VOLUME_S_OPTIMAL;
204 for (i = 0; i < vol->v_disks_count / N; i++) {
205 bestsd = &vol->v_subdisks[i * N];
206 for (j = 1; j < N; j++) {
207 sd = &vol->v_subdisks[i * N + j];
208 if (sd->sd_state > bestsd->sd_state)
210 else if (sd->sd_state == bestsd->sd_state &&
211 (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
212 sd->sd_state == G_RAID_SUBDISK_S_RESYNC) &&
213 sd->sd_rebuild_pos > bestsd->sd_rebuild_pos)
216 if (bestsd->sd_state >= G_RAID_SUBDISK_S_UNINITIALIZED &&
217 bestsd->sd_state != G_RAID_SUBDISK_S_ACTIVE) {
218 /* We found reasonable candidate. */
220 "Promote subdisk %s:%d from %s to ACTIVE.",
221 vol->v_name, bestsd->sd_pos,
222 g_raid_subdisk_state2str(bestsd->sd_state));
223 g_raid_change_subdisk_state(bestsd,
224 G_RAID_SUBDISK_S_ACTIVE);
225 g_raid_write_metadata(sc,
226 vol, bestsd, bestsd->sd_disk);
228 worstsd = &vol->v_subdisks[i * N];
229 for (j = 1; j < N; j++) {
230 sd = &vol->v_subdisks[i * N + j];
231 if (sd->sd_state < worstsd->sd_state)
234 if (worstsd->sd_state == G_RAID_SUBDISK_S_ACTIVE)
235 sstate = G_RAID_VOLUME_S_OPTIMAL;
236 else if (worstsd->sd_state >= G_RAID_SUBDISK_S_STALE)
237 sstate = G_RAID_VOLUME_S_SUBOPTIMAL;
238 else if (bestsd->sd_state == G_RAID_SUBDISK_S_ACTIVE)
239 sstate = G_RAID_VOLUME_S_DEGRADED;
241 sstate = G_RAID_VOLUME_S_BROKEN;
249 g_raid_tr_update_state_raid1e_odd(struct g_raid_volume *vol)
251 struct g_raid_softc *sc;
252 struct g_raid_subdisk *sd, *bestsd, *worstsd;
253 int i, j, state, sstate;
256 if (g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE) ==
258 return (G_RAID_VOLUME_S_OPTIMAL);
259 for (i = 0; i < vol->v_disks_count; i++) {
260 sd = &vol->v_subdisks[i];
261 if (sd->sd_state == G_RAID_SUBDISK_S_UNINITIALIZED) {
262 /* We found reasonable candidate. */
264 "Promote subdisk %s:%d from %s to STALE.",
265 vol->v_name, sd->sd_pos,
266 g_raid_subdisk_state2str(sd->sd_state));
267 g_raid_change_subdisk_state(sd,
268 G_RAID_SUBDISK_S_STALE);
269 g_raid_write_metadata(sc, vol, sd, sd->sd_disk);
272 state = G_RAID_VOLUME_S_OPTIMAL;
273 for (i = 0; i < vol->v_disks_count; i++) {
274 bestsd = &vol->v_subdisks[i];
275 worstsd = &vol->v_subdisks[i];
276 for (j = 1; j < N; j++) {
277 sd = &vol->v_subdisks[(i + j) % vol->v_disks_count];
278 if (sd->sd_state > bestsd->sd_state)
280 else if (sd->sd_state == bestsd->sd_state &&
281 (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
282 sd->sd_state == G_RAID_SUBDISK_S_RESYNC) &&
283 sd->sd_rebuild_pos > bestsd->sd_rebuild_pos)
285 if (sd->sd_state < worstsd->sd_state)
288 if (worstsd->sd_state == G_RAID_SUBDISK_S_ACTIVE)
289 sstate = G_RAID_VOLUME_S_OPTIMAL;
290 else if (worstsd->sd_state >= G_RAID_SUBDISK_S_STALE)
291 sstate = G_RAID_VOLUME_S_SUBOPTIMAL;
292 else if (bestsd->sd_state >= G_RAID_SUBDISK_S_STALE)
293 sstate = G_RAID_VOLUME_S_DEGRADED;
295 sstate = G_RAID_VOLUME_S_BROKEN;
303 g_raid_tr_update_state_raid1e(struct g_raid_volume *vol,
304 struct g_raid_subdisk *sd)
306 struct g_raid_tr_raid1e_object *trs;
307 struct g_raid_softc *sc;
311 trs = (struct g_raid_tr_raid1e_object *)vol->v_tr;
312 if (trs->trso_stopping &&
313 (trs->trso_flags & TR_RAID1E_F_DOING_SOME) == 0)
314 s = G_RAID_VOLUME_S_STOPPED;
315 else if (trs->trso_starting)
316 s = G_RAID_VOLUME_S_STARTING;
318 if ((vol->v_disks_count % N) == 0)
319 s = g_raid_tr_update_state_raid1e_even(vol);
321 s = g_raid_tr_update_state_raid1e_odd(vol);
323 if (s != vol->v_state) {
324 g_raid_event_send(vol, G_RAID_VOLUME_S_ALIVE(s) ?
325 G_RAID_VOLUME_E_UP : G_RAID_VOLUME_E_DOWN,
326 G_RAID_EVENT_VOLUME);
327 g_raid_change_volume_state(vol, s);
328 if (!trs->trso_starting && !trs->trso_stopping)
329 g_raid_write_metadata(sc, vol, NULL, NULL);
331 if (!trs->trso_starting && !trs->trso_stopping)
332 g_raid_tr_raid1e_maybe_rebuild(vol->v_tr, sd);
337 g_raid_tr_raid1e_fail_disk(struct g_raid_softc *sc, struct g_raid_subdisk *sd,
338 struct g_raid_disk *disk)
340 struct g_raid_volume *vol;
344 * We don't fail the last disk in the pack, since it still has decent
345 * data on it and that's better than failing the disk if it is the root
348 * XXX should this be controlled via a tunable? It makes sense for
349 * the volume that has / on it. I can't think of a case where we'd
350 * want the volume to go away on this kind of event.
352 if ((g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE) +
353 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_RESYNC) +
354 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_STALE) +
355 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_UNINITIALIZED) <
356 vol->v_disks_count) &&
357 (sd->sd_state >= G_RAID_SUBDISK_S_UNINITIALIZED))
359 g_raid_fail_disk(sc, sd, disk);
363 g_raid_tr_raid1e_rebuild_done(struct g_raid_tr_raid1e_object *trs)
365 struct g_raid_volume *vol;
366 struct g_raid_subdisk *sd;
368 vol = trs->trso_base.tro_volume;
369 sd = trs->trso_failed_sd;
370 g_raid_write_metadata(vol->v_softc, vol, sd, sd->sd_disk);
371 free(trs->trso_buffer, M_TR_RAID1E);
372 trs->trso_buffer = NULL;
373 trs->trso_flags &= ~TR_RAID1E_F_DOING_SOME;
374 trs->trso_type = TR_RAID1E_NONE;
375 trs->trso_recover_slabs = 0;
376 trs->trso_failed_sd = NULL;
377 g_raid_tr_update_state_raid1e(vol, NULL);
381 g_raid_tr_raid1e_rebuild_finish(struct g_raid_tr_object *tr)
383 struct g_raid_tr_raid1e_object *trs;
384 struct g_raid_subdisk *sd;
386 trs = (struct g_raid_tr_raid1e_object *)tr;
387 sd = trs->trso_failed_sd;
388 G_RAID_DEBUG1(0, tr->tro_volume->v_softc,
389 "Subdisk %s:%d-%s rebuild completed.",
390 sd->sd_volume->v_name, sd->sd_pos,
391 sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]");
392 g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_ACTIVE);
393 sd->sd_rebuild_pos = 0;
394 g_raid_tr_raid1e_rebuild_done(trs);
398 g_raid_tr_raid1e_rebuild_abort(struct g_raid_tr_object *tr)
400 struct g_raid_tr_raid1e_object *trs;
401 struct g_raid_subdisk *sd;
402 struct g_raid_volume *vol;
404 vol = tr->tro_volume;
405 trs = (struct g_raid_tr_raid1e_object *)tr;
406 sd = trs->trso_failed_sd;
407 if (trs->trso_flags & TR_RAID1E_F_DOING_SOME) {
408 G_RAID_DEBUG1(1, vol->v_softc,
409 "Subdisk %s:%d-%s rebuild is aborting.",
410 sd->sd_volume->v_name, sd->sd_pos,
411 sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]");
412 trs->trso_flags |= TR_RAID1E_F_ABORT;
414 G_RAID_DEBUG1(0, vol->v_softc,
415 "Subdisk %s:%d-%s rebuild aborted.",
416 sd->sd_volume->v_name, sd->sd_pos,
417 sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]");
418 trs->trso_flags &= ~TR_RAID1E_F_ABORT;
419 if (trs->trso_flags & TR_RAID1E_F_LOCKED) {
420 trs->trso_flags &= ~TR_RAID1E_F_LOCKED;
421 g_raid_unlock_range(tr->tro_volume,
422 trs->trso_lock_pos, trs->trso_lock_len);
424 g_raid_tr_raid1e_rebuild_done(trs);
429 g_raid_tr_raid1e_rebuild_some(struct g_raid_tr_object *tr)
431 struct g_raid_tr_raid1e_object *trs;
432 struct g_raid_softc *sc;
433 struct g_raid_volume *vol;
434 struct g_raid_subdisk *sd;
436 off_t len, virtual, vend, offset, start;
437 int disk, copy, best;
439 trs = (struct g_raid_tr_raid1e_object *)tr;
440 if (trs->trso_flags & TR_RAID1E_F_DOING_SOME)
442 vol = tr->tro_volume;
444 sd = trs->trso_failed_sd;
447 if (sd->sd_rebuild_pos >= sd->sd_size) {
448 g_raid_tr_raid1e_rebuild_finish(tr);
451 /* Get virtual offset from physical rebuild position. */
452 P2V(vol, sd->sd_pos, sd->sd_rebuild_pos, &virtual, ©);
453 /* Get physical offset back to get first stripe position. */
454 V2P(vol, virtual, &disk, &offset, &start);
455 /* Calculate contignous data length. */
456 len = MIN(g_raid1e_rebuild_slab,
457 sd->sd_size - sd->sd_rebuild_pos);
458 if ((vol->v_disks_count % N) != 0)
459 len = MIN(len, vol->v_strip_size - start);
460 /* Find disk with most accurate data. */
461 best = g_raid_tr_raid1e_select_read_disk(vol, disk,
462 offset + start, len, 0);
464 /* There is no any valid disk. */
465 g_raid_tr_raid1e_rebuild_abort(tr);
467 } else if (best != copy) {
468 /* Some other disk has better data. */
471 /* We have the most accurate data. Skip the range. */
472 G_RAID_DEBUG1(3, sc, "Skipping rebuild for range %ju - %ju",
473 sd->sd_rebuild_pos, sd->sd_rebuild_pos + len);
474 sd->sd_rebuild_pos += len;
478 memset(bp, 0, sizeof(*bp));
479 bp->bio_offset = offset + start +
480 ((disk + best >= vol->v_disks_count) ? vol->v_strip_size : 0);
481 bp->bio_length = len;
482 bp->bio_data = trs->trso_buffer;
483 bp->bio_cmd = BIO_READ;
484 bp->bio_cflags = G_RAID_BIO_FLAG_SYNC;
485 bp->bio_caller1 = &vol->v_subdisks[(disk + best) % vol->v_disks_count];
486 G_RAID_LOGREQ(3, bp, "Queueing rebuild read");
488 * If we are crossing stripe boundary, correct affected virtual
489 * range we should lock.
491 if (start + len > vol->v_strip_size) {
492 P2V(vol, sd->sd_pos, sd->sd_rebuild_pos + len, &vend, ©);
493 len = vend - virtual;
495 trs->trso_flags |= TR_RAID1E_F_DOING_SOME;
496 trs->trso_flags |= TR_RAID1E_F_LOCKED;
497 trs->trso_lock_pos = virtual;
498 trs->trso_lock_len = len;
499 /* Lock callback starts I/O */
500 g_raid_lock_range(sd->sd_volume, virtual, len, NULL, bp);
504 g_raid_tr_raid1e_rebuild_start(struct g_raid_tr_object *tr)
506 struct g_raid_volume *vol;
507 struct g_raid_tr_raid1e_object *trs;
508 struct g_raid_subdisk *sd;
510 vol = tr->tro_volume;
511 trs = (struct g_raid_tr_raid1e_object *)tr;
512 if (trs->trso_failed_sd) {
513 G_RAID_DEBUG1(1, vol->v_softc,
514 "Already rebuild in start rebuild. pos %jd\n",
515 (intmax_t)trs->trso_failed_sd->sd_rebuild_pos);
518 sd = g_raid_get_subdisk(vol, G_RAID_SUBDISK_S_RESYNC);
520 sd = g_raid_get_subdisk(vol, G_RAID_SUBDISK_S_REBUILD);
522 sd = g_raid_get_subdisk(vol, G_RAID_SUBDISK_S_STALE);
524 sd->sd_rebuild_pos = 0;
525 g_raid_change_subdisk_state(sd,
526 G_RAID_SUBDISK_S_RESYNC);
527 g_raid_write_metadata(vol->v_softc, vol, sd, NULL);
529 sd = g_raid_get_subdisk(vol,
530 G_RAID_SUBDISK_S_UNINITIALIZED);
532 sd = g_raid_get_subdisk(vol,
533 G_RAID_SUBDISK_S_NEW);
535 sd->sd_rebuild_pos = 0;
536 g_raid_change_subdisk_state(sd,
537 G_RAID_SUBDISK_S_REBUILD);
538 g_raid_write_metadata(vol->v_softc,
544 G_RAID_DEBUG1(1, vol->v_softc,
545 "No failed disk to rebuild. night night.");
548 trs->trso_failed_sd = sd;
549 G_RAID_DEBUG1(0, vol->v_softc,
550 "Subdisk %s:%d-%s rebuild start at %jd.",
551 sd->sd_volume->v_name, sd->sd_pos,
552 sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]",
553 trs->trso_failed_sd->sd_rebuild_pos);
554 trs->trso_type = TR_RAID1E_REBUILD;
555 trs->trso_buffer = malloc(g_raid1e_rebuild_slab, M_TR_RAID1E, M_WAITOK);
556 trs->trso_meta_update = g_raid1e_rebuild_meta_update;
557 g_raid_tr_raid1e_rebuild_some(tr);
561 g_raid_tr_raid1e_maybe_rebuild(struct g_raid_tr_object *tr,
562 struct g_raid_subdisk *sd)
564 struct g_raid_volume *vol;
565 struct g_raid_tr_raid1e_object *trs;
568 vol = tr->tro_volume;
569 trs = (struct g_raid_tr_raid1e_object *)tr;
570 if (trs->trso_stopping)
572 nr = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_REBUILD) +
573 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_RESYNC);
574 switch(trs->trso_type) {
576 if (vol->v_state < G_RAID_VOLUME_S_DEGRADED)
579 nr = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_NEW) +
580 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_STALE) +
581 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_UNINITIALIZED);
585 g_raid_tr_raid1e_rebuild_start(tr);
587 case TR_RAID1E_REBUILD:
588 if (vol->v_state < G_RAID_VOLUME_S_DEGRADED || nr == 0 ||
589 trs->trso_failed_sd == sd)
590 g_raid_tr_raid1e_rebuild_abort(tr);
592 case TR_RAID1E_RESYNC:
598 g_raid_tr_event_raid1e(struct g_raid_tr_object *tr,
599 struct g_raid_subdisk *sd, u_int event)
602 g_raid_tr_update_state_raid1e(tr->tro_volume, sd);
607 g_raid_tr_start_raid1e(struct g_raid_tr_object *tr)
609 struct g_raid_tr_raid1e_object *trs;
610 struct g_raid_volume *vol;
612 trs = (struct g_raid_tr_raid1e_object *)tr;
613 vol = tr->tro_volume;
614 trs->trso_starting = 0;
615 g_raid_tr_update_state_raid1e(vol, NULL);
620 g_raid_tr_stop_raid1e(struct g_raid_tr_object *tr)
622 struct g_raid_tr_raid1e_object *trs;
623 struct g_raid_volume *vol;
625 trs = (struct g_raid_tr_raid1e_object *)tr;
626 vol = tr->tro_volume;
627 trs->trso_starting = 0;
628 trs->trso_stopping = 1;
629 g_raid_tr_update_state_raid1e(vol, NULL);
634 * Select the disk to read from. Take into account: subdisk state, running
635 * error recovery, average disk load, head position and possible cache hits.
637 #define ABS(x) (((x) >= 0) ? (x) : (-(x)))
639 g_raid_tr_raid1e_select_read_disk(struct g_raid_volume *vol,
640 int no, off_t off, off_t len, u_int mask)
642 struct g_raid_subdisk *sd;
644 int i, best, prio, bestprio;
648 for (i = 0; i < N; i++) {
649 sd = &vol->v_subdisks[(no + i) % vol->v_disks_count];
651 if (no + i >= vol->v_disks_count)
652 offset += vol->v_strip_size;
654 prio = G_RAID_SUBDISK_LOAD(sd);
655 if ((mask & (1 << sd->sd_pos)) != 0)
657 switch (sd->sd_state) {
658 case G_RAID_SUBDISK_S_ACTIVE:
660 case G_RAID_SUBDISK_S_RESYNC:
661 if (offset + off < sd->sd_rebuild_pos)
664 case G_RAID_SUBDISK_S_STALE:
667 case G_RAID_SUBDISK_S_REBUILD:
668 if (offset + off < sd->sd_rebuild_pos)
674 prio += min(sd->sd_recovery, 255) << 16;
675 /* If disk head is precisely in position - highly prefer it. */
676 if (G_RAID_SUBDISK_POS(sd) == offset)
677 prio -= 2 * G_RAID_SUBDISK_LOAD_SCALE;
679 /* If disk head is close to position - prefer it. */
680 if (ABS(G_RAID_SUBDISK_POS(sd) - offset) <
681 G_RAID_SUBDISK_TRACK_SIZE)
682 prio -= 1 * G_RAID_SUBDISK_LOAD_SCALE;
683 if (prio < bestprio) {
692 g_raid_tr_iostart_raid1e_read(struct g_raid_tr_object *tr, struct bio *bp)
694 struct g_raid_volume *vol;
695 struct g_raid_subdisk *sd;
696 struct bio_queue_head queue;
699 off_t offset, start, length, remain;
700 u_int no, strip_size;
703 vol = tr->tro_volume;
705 strip_size = vol->v_strip_size;
706 V2P(vol, bp->bio_offset, &no, &offset, &start);
707 remain = bp->bio_length;
710 length = MIN(strip_size - start, remain);
711 best = g_raid_tr_raid1e_select_read_disk(vol,
712 no, offset, length, 0);
713 KASSERT(best >= 0, ("No readable disk in volume %s!",
716 if (no >= vol->v_disks_count) {
717 no -= vol->v_disks_count;
718 offset += strip_size;
720 cbp = g_clone_bio(bp);
723 cbp->bio_offset = offset + start;
724 cbp->bio_data = addr;
725 cbp->bio_length = length;
726 cbp->bio_caller1 = &vol->v_subdisks[no];
727 bioq_insert_tail(&queue, cbp);
729 if (no >= vol->v_disks_count) {
730 no -= vol->v_disks_count;
731 offset += strip_size;
737 for (cbp = bioq_first(&queue); cbp != NULL;
738 cbp = bioq_first(&queue)) {
739 bioq_remove(&queue, cbp);
740 sd = cbp->bio_caller1;
741 cbp->bio_caller1 = NULL;
742 g_raid_subdisk_iostart(sd, cbp);
746 for (cbp = bioq_first(&queue); cbp != NULL;
747 cbp = bioq_first(&queue)) {
748 bioq_remove(&queue, cbp);
751 if (bp->bio_error == 0)
752 bp->bio_error = ENOMEM;
753 g_raid_iodone(bp, bp->bio_error);
757 g_raid_tr_iostart_raid1e_write(struct g_raid_tr_object *tr, struct bio *bp)
759 struct g_raid_volume *vol;
760 struct g_raid_subdisk *sd;
761 struct bio_queue_head queue;
764 off_t offset, start, length, remain;
765 u_int no, strip_size;
768 vol = tr->tro_volume;
770 strip_size = vol->v_strip_size;
771 V2P(vol, bp->bio_offset, &no, &offset, &start);
772 remain = bp->bio_length;
775 length = MIN(strip_size - start, remain);
776 for (i = 0; i < N; i++) {
777 sd = &vol->v_subdisks[no];
778 switch (sd->sd_state) {
779 case G_RAID_SUBDISK_S_ACTIVE:
780 case G_RAID_SUBDISK_S_STALE:
781 case G_RAID_SUBDISK_S_RESYNC:
783 case G_RAID_SUBDISK_S_REBUILD:
784 if (offset + start >= sd->sd_rebuild_pos)
790 cbp = g_clone_bio(bp);
793 cbp->bio_offset = offset + start;
794 cbp->bio_data = addr;
795 cbp->bio_length = length;
796 cbp->bio_caller1 = sd;
797 bioq_insert_tail(&queue, cbp);
799 if (++no >= vol->v_disks_count) {
801 offset += strip_size;
805 if (bp->bio_cmd != BIO_DELETE)
809 for (cbp = bioq_first(&queue); cbp != NULL;
810 cbp = bioq_first(&queue)) {
811 bioq_remove(&queue, cbp);
812 sd = cbp->bio_caller1;
813 cbp->bio_caller1 = NULL;
814 g_raid_subdisk_iostart(sd, cbp);
818 for (cbp = bioq_first(&queue); cbp != NULL;
819 cbp = bioq_first(&queue)) {
820 bioq_remove(&queue, cbp);
823 if (bp->bio_error == 0)
824 bp->bio_error = ENOMEM;
825 g_raid_iodone(bp, bp->bio_error);
829 g_raid_tr_iostart_raid1e(struct g_raid_tr_object *tr, struct bio *bp)
831 struct g_raid_volume *vol;
832 struct g_raid_tr_raid1e_object *trs;
834 vol = tr->tro_volume;
835 trs = (struct g_raid_tr_raid1e_object *)tr;
836 if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL &&
837 vol->v_state != G_RAID_VOLUME_S_SUBOPTIMAL &&
838 vol->v_state != G_RAID_VOLUME_S_DEGRADED) {
839 g_raid_iodone(bp, EIO);
843 * If we're rebuilding, squeeze in rebuild activity every so often,
844 * even when the disk is busy. Be sure to only count real I/O
845 * to the disk. All 'SPECIAL' I/O is traffic generated to the disk
848 if (trs->trso_failed_sd != NULL &&
849 !(bp->bio_cflags & G_RAID_BIO_FLAG_SPECIAL)) {
850 /* Make this new or running now round short. */
851 trs->trso_recover_slabs = 0;
852 if (--trs->trso_fair_io <= 0) {
853 trs->trso_fair_io = g_raid1e_rebuild_fair_io;
854 g_raid_tr_raid1e_rebuild_some(tr);
857 switch (bp->bio_cmd) {
859 g_raid_tr_iostart_raid1e_read(tr, bp);
863 g_raid_tr_iostart_raid1e_write(tr, bp);
866 g_raid_tr_flush_common(tr, bp);
869 KASSERT(1 == 0, ("Invalid command here: %u (volume=%s)",
870 bp->bio_cmd, vol->v_name));
876 g_raid_tr_iodone_raid1e(struct g_raid_tr_object *tr,
877 struct g_raid_subdisk *sd, struct bio *bp)
880 struct g_raid_subdisk *nsd;
881 struct g_raid_volume *vol;
883 struct g_raid_tr_raid1e_object *trs;
884 off_t virtual, offset, start;
886 int error, do_write, copy, disk, best;
888 trs = (struct g_raid_tr_raid1e_object *)tr;
889 vol = tr->tro_volume;
890 if (bp->bio_cflags & G_RAID_BIO_FLAG_SYNC) {
891 if (trs->trso_type == TR_RAID1E_REBUILD) {
892 nsd = trs->trso_failed_sd;
893 if (bp->bio_cmd == BIO_READ) {
895 /* Immediately abort rebuild, if requested. */
896 if (trs->trso_flags & TR_RAID1E_F_ABORT) {
897 trs->trso_flags &= ~TR_RAID1E_F_DOING_SOME;
898 g_raid_tr_raid1e_rebuild_abort(tr);
902 /* On read error, skip and cross fingers. */
903 if (bp->bio_error != 0) {
905 "Read error during rebuild (%d), "
906 "possible data loss!",
908 goto rebuild_round_done;
912 * The read operation finished, queue the
915 G_RAID_LOGREQ(3, bp, "Rebuild read done: %d",
917 bp->bio_cmd = BIO_WRITE;
918 bp->bio_cflags = G_RAID_BIO_FLAG_SYNC;
919 bp->bio_offset = nsd->sd_rebuild_pos;
920 G_RAID_LOGREQ(3, bp, "Queueing rebuild write.");
921 g_raid_subdisk_iostart(nsd, bp);
924 * The write operation just finished. Do
925 * another. We keep cloning the master bio
926 * since it has the right buffers allocated to
929 G_RAID_LOGREQ(3, bp, "Rebuild write done: %d",
931 if (bp->bio_error != 0 ||
932 trs->trso_flags & TR_RAID1E_F_ABORT) {
933 if ((trs->trso_flags &
934 TR_RAID1E_F_ABORT) == 0) {
935 g_raid_tr_raid1e_fail_disk(sd->sd_softc,
938 trs->trso_flags &= ~TR_RAID1E_F_DOING_SOME;
939 g_raid_tr_raid1e_rebuild_abort(tr);
943 trs->trso_flags &= ~TR_RAID1E_F_LOCKED;
944 g_raid_unlock_range(tr->tro_volume,
945 trs->trso_lock_pos, trs->trso_lock_len);
946 nsd->sd_rebuild_pos += bp->bio_length;
947 if (nsd->sd_rebuild_pos >= nsd->sd_size) {
948 g_raid_tr_raid1e_rebuild_finish(tr);
952 /* Abort rebuild if we are stopping */
953 if (trs->trso_stopping) {
954 trs->trso_flags &= ~TR_RAID1E_F_DOING_SOME;
955 g_raid_tr_raid1e_rebuild_abort(tr);
959 if (--trs->trso_meta_update <= 0) {
960 g_raid_write_metadata(vol->v_softc,
961 vol, nsd, nsd->sd_disk);
962 trs->trso_meta_update =
963 g_raid1e_rebuild_meta_update;
964 /* Compensate short rebuild I/Os. */
965 if ((vol->v_disks_count % N) != 0 &&
967 g_raid1e_rebuild_slab) {
968 trs->trso_meta_update *=
969 g_raid1e_rebuild_slab;
970 trs->trso_meta_update /=
974 trs->trso_flags &= ~TR_RAID1E_F_DOING_SOME;
975 if (--trs->trso_recover_slabs <= 0)
977 /* Run next rebuild iteration. */
978 g_raid_tr_raid1e_rebuild_some(tr);
980 } else if (trs->trso_type == TR_RAID1E_RESYNC) {
982 * read good sd, read bad sd in parallel. when both
983 * done, compare the buffers. write good to the bad
984 * if different. do the next bit of work.
986 panic("Somehow, we think we're doing a resync");
990 pbp = bp->bio_parent;
992 mask = (intptr_t)bp->bio_caller2;
993 if (bp->bio_cmd == BIO_READ && bp->bio_error != 0) {
995 * Read failed on first drive. Retry the read error on
996 * another disk drive, if available, before erroring out the
999 sd->sd_disk->d_read_errs++;
1000 G_RAID_LOGREQ(0, bp,
1001 "Read error (%d), %d read errors total",
1002 bp->bio_error, sd->sd_disk->d_read_errs);
1005 * If there are too many read errors, we move to degraded.
1006 * XXX Do we want to FAIL the drive (eg, make the user redo
1007 * everything to get it back in sync), or just degrade the
1008 * drive, which kicks off a resync?
1011 if (sd->sd_disk->d_read_errs > g_raid_read_err_thresh)
1012 g_raid_tr_raid1e_fail_disk(sd->sd_softc, sd, sd->sd_disk);
1016 /* Restore what we were doing. */
1017 P2V(vol, sd->sd_pos, bp->bio_offset, &virtual, ©);
1018 V2P(vol, virtual, &disk, &offset, &start);
1020 /* Find the other disk, and try to do the I/O to it. */
1022 best = g_raid_tr_raid1e_select_read_disk(vol,
1023 disk, offset, start, mask);
1024 if (best >= 0 && (cbp = g_clone_bio(pbp)) != NULL) {
1026 if (disk >= vol->v_disks_count) {
1027 disk -= vol->v_disks_count;
1028 offset += vol->v_strip_size;
1030 cbp->bio_offset = offset + start;
1031 cbp->bio_length = bp->bio_length;
1032 cbp->bio_data = bp->bio_data;
1034 nsd = &vol->v_subdisks[disk];
1035 G_RAID_LOGREQ(2, cbp, "Retrying read from %d",
1039 if ((mask & (1 << 31)) != 0)
1041 cbp->bio_caller2 = (void *)mask;
1043 cbp->bio_caller1 = nsd;
1044 /* Lock callback starts I/O */
1045 g_raid_lock_range(sd->sd_volume,
1046 virtual, cbp->bio_length, pbp, cbp);
1048 g_raid_subdisk_iostart(nsd, cbp);
1053 * We can't retry. Return the original error by falling
1054 * through. This will happen when there's only one good disk.
1055 * We don't need to fail the raid, since its actual state is
1056 * based on the state of the subdisks.
1058 G_RAID_LOGREQ(2, bp, "Couldn't retry read, failing it");
1060 if (bp->bio_cmd == BIO_READ &&
1061 bp->bio_error == 0 &&
1062 (mask & (1 << 31)) != 0) {
1063 G_RAID_LOGREQ(3, bp, "Recovered data from other drive");
1065 /* Restore what we were doing. */
1066 P2V(vol, sd->sd_pos, bp->bio_offset, &virtual, ©);
1067 V2P(vol, virtual, &disk, &offset, &start);
1069 /* Find best disk to write. */
1070 best = g_raid_tr_raid1e_select_read_disk(vol,
1071 disk, offset, start, ~mask);
1072 if (best >= 0 && (cbp = g_clone_bio(pbp)) != NULL) {
1074 if (disk >= vol->v_disks_count) {
1075 disk -= vol->v_disks_count;
1076 offset += vol->v_strip_size;
1078 cbp->bio_offset = offset + start;
1079 cbp->bio_cmd = BIO_WRITE;
1080 cbp->bio_cflags = G_RAID_BIO_FLAG_REMAP;
1081 cbp->bio_caller2 = (void *)mask;
1083 G_RAID_LOGREQ(2, cbp,
1084 "Attempting bad sector remap on failing drive.");
1085 g_raid_subdisk_iostart(&vol->v_subdisks[disk], cbp);
1089 if ((mask & (1 << 31)) != 0) {
1091 * We're done with a recovery, mark the range as unlocked.
1092 * For any write errors, we agressively fail the disk since
1093 * there was both a READ and a WRITE error at this location.
1094 * Both types of errors generally indicates the drive is on
1095 * the verge of total failure anyway. Better to stop trusting
1096 * it now. However, we need to reset error to 0 in that case
1097 * because we're not failing the original I/O which succeeded.
1100 /* Restore what we were doing. */
1101 P2V(vol, sd->sd_pos, bp->bio_offset, &virtual, ©);
1102 V2P(vol, virtual, &disk, &offset, &start);
1104 for (copy = 0; copy < N; copy++) {
1105 if ((mask & (1 << copy) ) != 0)
1106 vol->v_subdisks[(disk + copy) %
1107 vol->v_disks_count].sd_recovery--;
1110 if (bp->bio_cmd == BIO_WRITE && bp->bio_error) {
1111 G_RAID_LOGREQ(0, bp, "Remap write failed: "
1112 "failing subdisk.");
1113 g_raid_tr_raid1e_fail_disk(sd->sd_softc, sd, sd->sd_disk);
1116 G_RAID_LOGREQ(2, bp, "REMAP done %d.", bp->bio_error);
1117 g_raid_unlock_range(sd->sd_volume, virtual, bp->bio_length);
1119 if (pbp->bio_cmd != BIO_READ) {
1120 if (pbp->bio_inbed == 1 || pbp->bio_error != 0)
1121 pbp->bio_error = bp->bio_error;
1122 if (pbp->bio_cmd == BIO_WRITE && bp->bio_error != 0) {
1123 G_RAID_LOGREQ(0, bp, "Write failed: failing subdisk.");
1124 g_raid_tr_raid1e_fail_disk(sd->sd_softc, sd, sd->sd_disk);
1126 error = pbp->bio_error;
1128 error = bp->bio_error;
1130 if (pbp->bio_children == pbp->bio_inbed) {
1131 pbp->bio_completed = pbp->bio_length;
1132 g_raid_iodone(pbp, error);
1137 g_raid_tr_kerneldump_raid1e(struct g_raid_tr_object *tr,
1138 void *virtual, vm_offset_t physical, off_t boffset, size_t blength)
1140 struct g_raid_volume *vol;
1141 struct g_raid_subdisk *sd;
1142 struct bio_queue_head queue;
1144 off_t offset, start, length, remain;
1145 u_int no, strip_size;
1148 vol = tr->tro_volume;
1150 strip_size = vol->v_strip_size;
1151 V2P(vol, boffset, &no, &offset, &start);
1154 while (remain > 0) {
1155 length = MIN(strip_size - start, remain);
1156 for (i = 0; i < N; i++) {
1157 sd = &vol->v_subdisks[no];
1158 switch (sd->sd_state) {
1159 case G_RAID_SUBDISK_S_ACTIVE:
1160 case G_RAID_SUBDISK_S_STALE:
1161 case G_RAID_SUBDISK_S_RESYNC:
1163 case G_RAID_SUBDISK_S_REBUILD:
1164 if (offset + start >= sd->sd_rebuild_pos)
1170 error = g_raid_subdisk_kerneldump(sd,
1171 addr, 0, offset + start, length);
1175 if (++no >= vol->v_disks_count) {
1177 offset += strip_size;
1188 g_raid_tr_locked_raid1e(struct g_raid_tr_object *tr, void *argp)
1191 struct g_raid_subdisk *sd;
1193 bp = (struct bio *)argp;
1194 sd = (struct g_raid_subdisk *)bp->bio_caller1;
1195 g_raid_subdisk_iostart(sd, bp);
1201 g_raid_tr_idle_raid1e(struct g_raid_tr_object *tr)
1203 struct g_raid_tr_raid1e_object *trs;
1204 struct g_raid_volume *vol;
1206 vol = tr->tro_volume;
1207 trs = (struct g_raid_tr_raid1e_object *)tr;
1208 trs->trso_fair_io = g_raid1e_rebuild_fair_io;
1209 trs->trso_recover_slabs = g_raid1e_rebuild_cluster_idle;
1210 /* Compensate short rebuild I/Os. */
1211 if ((vol->v_disks_count % N) != 0 &&
1212 vol->v_strip_size < g_raid1e_rebuild_slab) {
1213 trs->trso_recover_slabs *= g_raid1e_rebuild_slab;
1214 trs->trso_recover_slabs /= vol->v_strip_size;
1216 if (trs->trso_type == TR_RAID1E_REBUILD)
1217 g_raid_tr_raid1e_rebuild_some(tr);
1222 g_raid_tr_free_raid1e(struct g_raid_tr_object *tr)
1224 struct g_raid_tr_raid1e_object *trs;
1226 trs = (struct g_raid_tr_raid1e_object *)tr;
1228 if (trs->trso_buffer != NULL) {
1229 free(trs->trso_buffer, M_TR_RAID1E);
1230 trs->trso_buffer = NULL;
1235 G_RAID_TR_DECLARE(raid1e, "RAID1E");