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);
48 SYSCTL_NODE(_kern_geom_raid, OID_AUTO, raid1e, CTLFLAG_RW, 0,
51 #define RAID1E_REBUILD_SLAB (1 << 20) /* One transation in a rebuild */
52 static int g_raid1e_rebuild_slab = RAID1E_REBUILD_SLAB;
53 TUNABLE_INT("kern.geom.raid.raid1e.rebuild_slab_size",
54 &g_raid1e_rebuild_slab);
55 SYSCTL_UINT(_kern_geom_raid_raid1e, OID_AUTO, rebuild_slab_size, CTLFLAG_RW,
56 &g_raid1e_rebuild_slab, 0,
57 "Amount of the disk to rebuild each read/write cycle of the rebuild.");
59 #define RAID1E_REBUILD_FAIR_IO 20 /* use 1/x of the available I/O */
60 static int g_raid1e_rebuild_fair_io = RAID1E_REBUILD_FAIR_IO;
61 TUNABLE_INT("kern.geom.raid.raid1e.rebuild_fair_io",
62 &g_raid1e_rebuild_fair_io);
63 SYSCTL_UINT(_kern_geom_raid_raid1e, OID_AUTO, rebuild_fair_io, CTLFLAG_RW,
64 &g_raid1e_rebuild_fair_io, 0,
65 "Fraction of the I/O bandwidth to use when disk busy for rebuild.");
67 #define RAID1E_REBUILD_CLUSTER_IDLE 100
68 static int g_raid1e_rebuild_cluster_idle = RAID1E_REBUILD_CLUSTER_IDLE;
69 TUNABLE_INT("kern.geom.raid.raid1e.rebuild_cluster_idle",
70 &g_raid1e_rebuild_cluster_idle);
71 SYSCTL_UINT(_kern_geom_raid_raid1e, OID_AUTO, rebuild_cluster_idle, CTLFLAG_RW,
72 &g_raid1e_rebuild_cluster_idle, 0,
73 "Number of slabs to do each time we trigger a rebuild cycle");
75 #define RAID1E_REBUILD_META_UPDATE 1024 /* update meta data every 1GB or so */
76 static int g_raid1e_rebuild_meta_update = RAID1E_REBUILD_META_UPDATE;
77 TUNABLE_INT("kern.geom.raid.raid1e.rebuild_meta_update",
78 &g_raid1e_rebuild_meta_update);
79 SYSCTL_UINT(_kern_geom_raid_raid1e, OID_AUTO, rebuild_meta_update, CTLFLAG_RW,
80 &g_raid1e_rebuild_meta_update, 0,
81 "When to update the meta data.");
83 static MALLOC_DEFINE(M_TR_RAID1E, "tr_raid1e_data", "GEOM_RAID RAID1E data");
85 #define TR_RAID1E_NONE 0
86 #define TR_RAID1E_REBUILD 1
87 #define TR_RAID1E_RESYNC 2
89 #define TR_RAID1E_F_DOING_SOME 0x1
90 #define TR_RAID1E_F_LOCKED 0x2
91 #define TR_RAID1E_F_ABORT 0x4
93 struct g_raid_tr_raid1e_object {
94 struct g_raid_tr_object trso_base;
98 int trso_recover_slabs; /* slabs before rest */
100 int trso_meta_update;
102 struct g_raid_subdisk *trso_failed_sd; /* like per volume */
103 void *trso_buffer; /* Buffer space */
104 off_t trso_lock_pos; /* Locked range start. */
105 off_t trso_lock_len; /* Locked range length. */
109 static g_raid_tr_taste_t g_raid_tr_taste_raid1e;
110 static g_raid_tr_event_t g_raid_tr_event_raid1e;
111 static g_raid_tr_start_t g_raid_tr_start_raid1e;
112 static g_raid_tr_stop_t g_raid_tr_stop_raid1e;
113 static g_raid_tr_iostart_t g_raid_tr_iostart_raid1e;
114 static g_raid_tr_iodone_t g_raid_tr_iodone_raid1e;
115 static g_raid_tr_kerneldump_t g_raid_tr_kerneldump_raid1e;
116 static g_raid_tr_locked_t g_raid_tr_locked_raid1e;
117 static g_raid_tr_idle_t g_raid_tr_idle_raid1e;
118 static g_raid_tr_free_t g_raid_tr_free_raid1e;
120 static kobj_method_t g_raid_tr_raid1e_methods[] = {
121 KOBJMETHOD(g_raid_tr_taste, g_raid_tr_taste_raid1e),
122 KOBJMETHOD(g_raid_tr_event, g_raid_tr_event_raid1e),
123 KOBJMETHOD(g_raid_tr_start, g_raid_tr_start_raid1e),
124 KOBJMETHOD(g_raid_tr_stop, g_raid_tr_stop_raid1e),
125 KOBJMETHOD(g_raid_tr_iostart, g_raid_tr_iostart_raid1e),
126 KOBJMETHOD(g_raid_tr_iodone, g_raid_tr_iodone_raid1e),
127 KOBJMETHOD(g_raid_tr_kerneldump, g_raid_tr_kerneldump_raid1e),
128 KOBJMETHOD(g_raid_tr_locked, g_raid_tr_locked_raid1e),
129 KOBJMETHOD(g_raid_tr_idle, g_raid_tr_idle_raid1e),
130 KOBJMETHOD(g_raid_tr_free, g_raid_tr_free_raid1e),
134 static struct g_raid_tr_class g_raid_tr_raid1e_class = {
136 g_raid_tr_raid1e_methods,
137 sizeof(struct g_raid_tr_raid1e_object),
141 static void g_raid_tr_raid1e_rebuild_abort(struct g_raid_tr_object *tr);
142 static void g_raid_tr_raid1e_maybe_rebuild(struct g_raid_tr_object *tr,
143 struct g_raid_subdisk *sd);
144 static int g_raid_tr_raid1e_select_read_disk(struct g_raid_volume *vol,
145 int no, off_t off, off_t len, u_int mask);
148 V2P(struct g_raid_volume *vol, off_t virt,
149 int *disk, off_t *offset, off_t *start)
154 strip_size = vol->v_strip_size;
156 nstrip = virt / strip_size;
157 /* Start position in strip. */
158 *start = virt % strip_size;
160 *disk = (nstrip * N) % vol->v_disks_count;
161 /* Strip start position in disk. */
162 *offset = ((nstrip * N) / vol->v_disks_count) * strip_size;
166 P2V(struct g_raid_volume *vol, int disk, off_t offset,
167 off_t *virt, int *copy)
172 strip_size = vol->v_strip_size;
173 /* Start position in strip. */
174 start = offset % strip_size;
175 /* Physical strip number. */
176 nstrip = (offset / strip_size) * vol->v_disks_count + disk;
177 /* Number of physical strip (copy) inside virtual strip. */
179 /* Offset in virtual space. */
180 *virt = (nstrip / N) * strip_size + start;
184 g_raid_tr_taste_raid1e(struct g_raid_tr_object *tr, struct g_raid_volume *vol)
186 struct g_raid_tr_raid1e_object *trs;
188 trs = (struct g_raid_tr_raid1e_object *)tr;
189 if (tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_RAID1E ||
190 tr->tro_volume->v_raid_level_qualifier != G_RAID_VOLUME_RLQ_NONE)
191 return (G_RAID_TR_TASTE_FAIL);
192 trs->trso_starting = 1;
193 return (G_RAID_TR_TASTE_SUCCEED);
197 g_raid_tr_update_state_raid1e_even(struct g_raid_volume *vol)
199 struct g_raid_softc *sc;
200 struct g_raid_subdisk *sd, *bestsd, *worstsd;
201 int i, j, state, sstate;
204 state = G_RAID_VOLUME_S_OPTIMAL;
205 for (i = 0; i < vol->v_disks_count / N; i++) {
206 bestsd = &vol->v_subdisks[i * N];
207 for (j = 1; j < N; j++) {
208 sd = &vol->v_subdisks[i * N + j];
209 if (sd->sd_state > bestsd->sd_state)
211 else if (sd->sd_state == bestsd->sd_state &&
212 (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
213 sd->sd_state == G_RAID_SUBDISK_S_RESYNC) &&
214 sd->sd_rebuild_pos > bestsd->sd_rebuild_pos)
217 if (bestsd->sd_state >= G_RAID_SUBDISK_S_UNINITIALIZED &&
218 bestsd->sd_state != G_RAID_SUBDISK_S_ACTIVE) {
219 /* We found reasonable candidate. */
221 "Promote subdisk %s:%d from %s to ACTIVE.",
222 vol->v_name, bestsd->sd_pos,
223 g_raid_subdisk_state2str(bestsd->sd_state));
224 g_raid_change_subdisk_state(bestsd,
225 G_RAID_SUBDISK_S_ACTIVE);
226 g_raid_write_metadata(sc,
227 vol, bestsd, bestsd->sd_disk);
229 worstsd = &vol->v_subdisks[i * N];
230 for (j = 1; j < N; j++) {
231 sd = &vol->v_subdisks[i * N + j];
232 if (sd->sd_state < worstsd->sd_state)
235 if (worstsd->sd_state == G_RAID_SUBDISK_S_ACTIVE)
236 sstate = G_RAID_VOLUME_S_OPTIMAL;
237 else if (worstsd->sd_state >= G_RAID_SUBDISK_S_STALE)
238 sstate = G_RAID_VOLUME_S_SUBOPTIMAL;
239 else if (bestsd->sd_state == G_RAID_SUBDISK_S_ACTIVE)
240 sstate = G_RAID_VOLUME_S_DEGRADED;
242 sstate = G_RAID_VOLUME_S_BROKEN;
250 g_raid_tr_update_state_raid1e_odd(struct g_raid_volume *vol)
252 struct g_raid_softc *sc;
253 struct g_raid_subdisk *sd, *bestsd, *worstsd;
254 int i, j, state, sstate;
257 if (g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE) ==
259 return (G_RAID_VOLUME_S_OPTIMAL);
260 for (i = 0; i < vol->v_disks_count; i++) {
261 sd = &vol->v_subdisks[i];
262 if (sd->sd_state == G_RAID_SUBDISK_S_UNINITIALIZED) {
263 /* We found reasonable candidate. */
265 "Promote subdisk %s:%d from %s to STALE.",
266 vol->v_name, sd->sd_pos,
267 g_raid_subdisk_state2str(sd->sd_state));
268 g_raid_change_subdisk_state(sd,
269 G_RAID_SUBDISK_S_STALE);
270 g_raid_write_metadata(sc, vol, sd, sd->sd_disk);
273 state = G_RAID_VOLUME_S_OPTIMAL;
274 for (i = 0; i < vol->v_disks_count; i++) {
275 bestsd = &vol->v_subdisks[i];
276 worstsd = &vol->v_subdisks[i];
277 for (j = 1; j < N; j++) {
278 sd = &vol->v_subdisks[(i + j) % vol->v_disks_count];
279 if (sd->sd_state > bestsd->sd_state)
281 else if (sd->sd_state == bestsd->sd_state &&
282 (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
283 sd->sd_state == G_RAID_SUBDISK_S_RESYNC) &&
284 sd->sd_rebuild_pos > bestsd->sd_rebuild_pos)
286 if (sd->sd_state < worstsd->sd_state)
289 if (worstsd->sd_state == G_RAID_SUBDISK_S_ACTIVE)
290 sstate = G_RAID_VOLUME_S_OPTIMAL;
291 else if (worstsd->sd_state >= G_RAID_SUBDISK_S_STALE)
292 sstate = G_RAID_VOLUME_S_SUBOPTIMAL;
293 else if (bestsd->sd_state >= G_RAID_SUBDISK_S_STALE)
294 sstate = G_RAID_VOLUME_S_DEGRADED;
296 sstate = G_RAID_VOLUME_S_BROKEN;
304 g_raid_tr_update_state_raid1e(struct g_raid_volume *vol,
305 struct g_raid_subdisk *sd)
307 struct g_raid_tr_raid1e_object *trs;
308 struct g_raid_softc *sc;
312 trs = (struct g_raid_tr_raid1e_object *)vol->v_tr;
313 if (trs->trso_stopping &&
314 (trs->trso_flags & TR_RAID1E_F_DOING_SOME) == 0)
315 s = G_RAID_VOLUME_S_STOPPED;
316 else if (trs->trso_starting)
317 s = G_RAID_VOLUME_S_STARTING;
319 if ((vol->v_disks_count % N) == 0)
320 s = g_raid_tr_update_state_raid1e_even(vol);
322 s = g_raid_tr_update_state_raid1e_odd(vol);
324 if (s != vol->v_state) {
325 g_raid_event_send(vol, G_RAID_VOLUME_S_ALIVE(s) ?
326 G_RAID_VOLUME_E_UP : G_RAID_VOLUME_E_DOWN,
327 G_RAID_EVENT_VOLUME);
328 g_raid_change_volume_state(vol, s);
329 if (!trs->trso_starting && !trs->trso_stopping)
330 g_raid_write_metadata(sc, vol, NULL, NULL);
332 if (!trs->trso_starting && !trs->trso_stopping)
333 g_raid_tr_raid1e_maybe_rebuild(vol->v_tr, sd);
338 g_raid_tr_raid1e_fail_disk(struct g_raid_softc *sc, struct g_raid_subdisk *sd,
339 struct g_raid_disk *disk)
342 * We don't fail the last disk in the pack, since it still has decent
343 * data on it and that's better than failing the disk if it is the root
346 * XXX should this be controlled via a tunable? It makes sense for
347 * the volume that has / on it. I can't think of a case where we'd
348 * want the volume to go away on this kind of event.
350 if (g_raid_nsubdisks(sd->sd_volume, G_RAID_SUBDISK_S_ACTIVE) == 1 &&
351 g_raid_get_subdisk(sd->sd_volume, G_RAID_SUBDISK_S_ACTIVE) == sd)
353 g_raid_fail_disk(sc, sd, disk);
357 g_raid_tr_raid1e_rebuild_done(struct g_raid_tr_raid1e_object *trs)
359 struct g_raid_volume *vol;
360 struct g_raid_subdisk *sd;
362 vol = trs->trso_base.tro_volume;
363 sd = trs->trso_failed_sd;
364 g_raid_write_metadata(vol->v_softc, vol, sd, sd->sd_disk);
365 free(trs->trso_buffer, M_TR_RAID1E);
366 trs->trso_buffer = NULL;
367 trs->trso_flags &= ~TR_RAID1E_F_DOING_SOME;
368 trs->trso_type = TR_RAID1E_NONE;
369 trs->trso_recover_slabs = 0;
370 trs->trso_failed_sd = NULL;
371 g_raid_tr_update_state_raid1e(vol, NULL);
375 g_raid_tr_raid1e_rebuild_finish(struct g_raid_tr_object *tr)
377 struct g_raid_tr_raid1e_object *trs;
378 struct g_raid_subdisk *sd;
380 trs = (struct g_raid_tr_raid1e_object *)tr;
381 sd = trs->trso_failed_sd;
382 G_RAID_DEBUG1(0, tr->tro_volume->v_softc,
383 "Subdisk %s:%d-%s rebuild completed.",
384 sd->sd_volume->v_name, sd->sd_pos,
385 sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]");
386 g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_ACTIVE);
387 sd->sd_rebuild_pos = 0;
388 g_raid_tr_raid1e_rebuild_done(trs);
392 g_raid_tr_raid1e_rebuild_abort(struct g_raid_tr_object *tr)
394 struct g_raid_tr_raid1e_object *trs;
395 struct g_raid_subdisk *sd;
396 struct g_raid_volume *vol;
398 vol = tr->tro_volume;
399 trs = (struct g_raid_tr_raid1e_object *)tr;
400 sd = trs->trso_failed_sd;
401 if (trs->trso_flags & TR_RAID1E_F_DOING_SOME) {
402 G_RAID_DEBUG1(1, vol->v_softc,
403 "Subdisk %s:%d-%s rebuild is aborting.",
404 sd->sd_volume->v_name, sd->sd_pos,
405 sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]");
406 trs->trso_flags |= TR_RAID1E_F_ABORT;
408 G_RAID_DEBUG1(0, vol->v_softc,
409 "Subdisk %s:%d-%s rebuild aborted.",
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;
413 if (trs->trso_flags & TR_RAID1E_F_LOCKED) {
414 trs->trso_flags &= ~TR_RAID1E_F_LOCKED;
415 g_raid_unlock_range(tr->tro_volume,
416 trs->trso_lock_pos, trs->trso_lock_len);
418 g_raid_tr_raid1e_rebuild_done(trs);
423 g_raid_tr_raid1e_rebuild_some(struct g_raid_tr_object *tr)
425 struct g_raid_tr_raid1e_object *trs;
426 struct g_raid_softc *sc;
427 struct g_raid_volume *vol;
428 struct g_raid_subdisk *sd;
430 off_t len, virtual, vend, offset, start;
431 int disk, copy, best;
433 trs = (struct g_raid_tr_raid1e_object *)tr;
434 if (trs->trso_flags & TR_RAID1E_F_DOING_SOME)
436 vol = tr->tro_volume;
438 sd = trs->trso_failed_sd;
441 if (sd->sd_rebuild_pos >= sd->sd_size) {
442 g_raid_tr_raid1e_rebuild_finish(tr);
445 /* Get virtual offset from physical rebuild position. */
446 P2V(vol, sd->sd_pos, sd->sd_rebuild_pos, &virtual, ©);
447 /* Get physical offset back to get first stripe position. */
448 V2P(vol, virtual, &disk, &offset, &start);
449 /* Calculate contignous data length. */
450 len = MIN(g_raid1e_rebuild_slab,
451 sd->sd_size - sd->sd_rebuild_pos);
452 if ((vol->v_disks_count % N) != 0)
453 len = MIN(len, vol->v_strip_size - start);
454 /* Find disk with most accurate data. */
455 best = g_raid_tr_raid1e_select_read_disk(vol, disk,
456 offset + start, len, 0);
458 /* There is no any valid disk. */
459 g_raid_tr_raid1e_rebuild_abort(tr);
461 } else if (best != copy) {
462 /* Some other disk has better data. */
465 /* We have the most accurate data. Skip the range. */
466 G_RAID_DEBUG1(3, sc, "Skipping rebuild for range %ju - %ju",
467 sd->sd_rebuild_pos, sd->sd_rebuild_pos + len);
468 sd->sd_rebuild_pos += len;
472 memset(bp, 0, sizeof(*bp));
473 bp->bio_offset = offset + start +
474 ((disk + best >= vol->v_disks_count) ? vol->v_strip_size : 0);
475 bp->bio_length = len;
476 bp->bio_data = trs->trso_buffer;
477 bp->bio_cmd = BIO_READ;
478 bp->bio_cflags = G_RAID_BIO_FLAG_SYNC;
479 bp->bio_caller1 = &vol->v_subdisks[(disk + best) % vol->v_disks_count];
480 G_RAID_LOGREQ(3, bp, "Queueing rebuild read");
482 * If we are crossing stripe boundary, correct affected virtual
483 * range we should lock.
485 if (start + len > vol->v_strip_size) {
486 P2V(vol, sd->sd_pos, sd->sd_rebuild_pos + len, &vend, ©);
487 len = vend - virtual;
489 trs->trso_flags |= TR_RAID1E_F_DOING_SOME;
490 trs->trso_flags |= TR_RAID1E_F_LOCKED;
491 trs->trso_lock_pos = virtual;
492 trs->trso_lock_len = len;
493 /* Lock callback starts I/O */
494 g_raid_lock_range(sd->sd_volume, virtual, len, NULL, bp);
498 g_raid_tr_raid1e_rebuild_start(struct g_raid_tr_object *tr)
500 struct g_raid_volume *vol;
501 struct g_raid_tr_raid1e_object *trs;
502 struct g_raid_subdisk *sd;
504 vol = tr->tro_volume;
505 trs = (struct g_raid_tr_raid1e_object *)tr;
506 if (trs->trso_failed_sd) {
507 G_RAID_DEBUG1(1, vol->v_softc,
508 "Already rebuild in start rebuild. pos %jd\n",
509 (intmax_t)trs->trso_failed_sd->sd_rebuild_pos);
512 sd = g_raid_get_subdisk(vol, G_RAID_SUBDISK_S_RESYNC);
514 sd = g_raid_get_subdisk(vol, G_RAID_SUBDISK_S_REBUILD);
516 sd = g_raid_get_subdisk(vol, G_RAID_SUBDISK_S_STALE);
518 sd->sd_rebuild_pos = 0;
519 g_raid_change_subdisk_state(sd,
520 G_RAID_SUBDISK_S_RESYNC);
521 g_raid_write_metadata(vol->v_softc, vol, sd, NULL);
523 sd = g_raid_get_subdisk(vol,
524 G_RAID_SUBDISK_S_UNINITIALIZED);
526 sd = g_raid_get_subdisk(vol,
527 G_RAID_SUBDISK_S_NEW);
529 sd->sd_rebuild_pos = 0;
530 g_raid_change_subdisk_state(sd,
531 G_RAID_SUBDISK_S_REBUILD);
532 g_raid_write_metadata(vol->v_softc,
538 G_RAID_DEBUG1(1, vol->v_softc,
539 "No failed disk to rebuild. night night.");
542 trs->trso_failed_sd = sd;
543 G_RAID_DEBUG1(0, vol->v_softc,
544 "Subdisk %s:%d-%s rebuild start at %jd.",
545 sd->sd_volume->v_name, sd->sd_pos,
546 sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]",
547 trs->trso_failed_sd->sd_rebuild_pos);
548 trs->trso_type = TR_RAID1E_REBUILD;
549 trs->trso_buffer = malloc(g_raid1e_rebuild_slab, M_TR_RAID1E, M_WAITOK);
550 trs->trso_meta_update = g_raid1e_rebuild_meta_update;
551 g_raid_tr_raid1e_rebuild_some(tr);
555 g_raid_tr_raid1e_maybe_rebuild(struct g_raid_tr_object *tr,
556 struct g_raid_subdisk *sd)
558 struct g_raid_volume *vol;
559 struct g_raid_tr_raid1e_object *trs;
562 vol = tr->tro_volume;
563 trs = (struct g_raid_tr_raid1e_object *)tr;
564 if (trs->trso_stopping)
566 nr = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_REBUILD) +
567 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_RESYNC);
568 switch(trs->trso_type) {
570 if (vol->v_state < G_RAID_VOLUME_S_DEGRADED)
573 nr = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_NEW) +
574 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_STALE) +
575 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_UNINITIALIZED);
579 g_raid_tr_raid1e_rebuild_start(tr);
581 case TR_RAID1E_REBUILD:
582 if (vol->v_state < G_RAID_VOLUME_S_DEGRADED || nr == 0 ||
583 trs->trso_failed_sd == sd)
584 g_raid_tr_raid1e_rebuild_abort(tr);
586 case TR_RAID1E_RESYNC:
592 g_raid_tr_event_raid1e(struct g_raid_tr_object *tr,
593 struct g_raid_subdisk *sd, u_int event)
596 g_raid_tr_update_state_raid1e(tr->tro_volume, sd);
601 g_raid_tr_start_raid1e(struct g_raid_tr_object *tr)
603 struct g_raid_tr_raid1e_object *trs;
604 struct g_raid_volume *vol;
606 trs = (struct g_raid_tr_raid1e_object *)tr;
607 vol = tr->tro_volume;
608 trs->trso_starting = 0;
609 g_raid_tr_update_state_raid1e(vol, NULL);
614 g_raid_tr_stop_raid1e(struct g_raid_tr_object *tr)
616 struct g_raid_tr_raid1e_object *trs;
617 struct g_raid_volume *vol;
619 trs = (struct g_raid_tr_raid1e_object *)tr;
620 vol = tr->tro_volume;
621 trs->trso_starting = 0;
622 trs->trso_stopping = 1;
623 g_raid_tr_update_state_raid1e(vol, NULL);
628 * Select the disk to read from. Take into account: subdisk state, running
629 * error recovery, average disk load, head position and possible cache hits.
631 #define ABS(x) (((x) >= 0) ? (x) : (-(x)))
633 g_raid_tr_raid1e_select_read_disk(struct g_raid_volume *vol,
634 int no, off_t off, off_t len, u_int mask)
636 struct g_raid_subdisk *sd;
638 int i, best, prio, bestprio;
642 for (i = 0; i < N; i++) {
643 sd = &vol->v_subdisks[(no + i) % vol->v_disks_count];
645 if (no + i >= vol->v_disks_count)
646 offset += vol->v_strip_size;
648 prio = G_RAID_SUBDISK_LOAD(sd);
649 if ((mask & (1 << sd->sd_pos)) != 0)
651 switch (sd->sd_state) {
652 case G_RAID_SUBDISK_S_ACTIVE:
654 case G_RAID_SUBDISK_S_RESYNC:
655 if (offset + off < sd->sd_rebuild_pos)
658 case G_RAID_SUBDISK_S_STALE:
661 case G_RAID_SUBDISK_S_REBUILD:
662 if (offset + off < sd->sd_rebuild_pos)
668 prio += min(sd->sd_recovery, 255) << 16;
669 /* If disk head is precisely in position - highly prefer it. */
670 if (G_RAID_SUBDISK_POS(sd) == offset)
671 prio -= 2 * G_RAID_SUBDISK_LOAD_SCALE;
673 /* If disk head is close to position - prefer it. */
674 if (ABS(G_RAID_SUBDISK_POS(sd) - offset) <
675 G_RAID_SUBDISK_TRACK_SIZE)
676 prio -= 1 * G_RAID_SUBDISK_LOAD_SCALE;
677 if (prio < bestprio) {
686 g_raid_tr_iostart_raid1e_read(struct g_raid_tr_object *tr, struct bio *bp)
688 struct g_raid_volume *vol;
689 struct g_raid_subdisk *sd;
690 struct bio_queue_head queue;
693 off_t offset, start, length, remain;
694 u_int no, strip_size;
697 vol = tr->tro_volume;
699 strip_size = vol->v_strip_size;
700 V2P(vol, bp->bio_offset, &no, &offset, &start);
701 remain = bp->bio_length;
704 length = MIN(strip_size - start, remain);
705 best = g_raid_tr_raid1e_select_read_disk(vol,
706 no, offset, length, 0);
707 KASSERT(best >= 0, ("No readable disk in volume %s!",
710 if (no >= vol->v_disks_count) {
711 no -= vol->v_disks_count;
712 offset += strip_size;
714 cbp = g_clone_bio(bp);
717 cbp->bio_offset = offset + start;
718 cbp->bio_data = addr;
719 cbp->bio_length = length;
720 cbp->bio_caller1 = &vol->v_subdisks[no];
721 bioq_insert_tail(&queue, cbp);
723 if (no >= vol->v_disks_count) {
724 no -= vol->v_disks_count;
725 offset += strip_size;
731 for (cbp = bioq_first(&queue); cbp != NULL;
732 cbp = bioq_first(&queue)) {
733 bioq_remove(&queue, cbp);
734 sd = cbp->bio_caller1;
735 cbp->bio_caller1 = NULL;
736 g_raid_subdisk_iostart(sd, cbp);
740 for (cbp = bioq_first(&queue); cbp != NULL;
741 cbp = bioq_first(&queue)) {
742 bioq_remove(&queue, cbp);
745 if (bp->bio_error == 0)
746 bp->bio_error = ENOMEM;
747 g_raid_iodone(bp, bp->bio_error);
751 g_raid_tr_iostart_raid1e_write(struct g_raid_tr_object *tr, struct bio *bp)
753 struct g_raid_volume *vol;
754 struct g_raid_subdisk *sd;
755 struct bio_queue_head queue;
758 off_t offset, start, length, remain;
759 u_int no, strip_size;
762 vol = tr->tro_volume;
764 strip_size = vol->v_strip_size;
765 V2P(vol, bp->bio_offset, &no, &offset, &start);
766 remain = bp->bio_length;
769 length = MIN(strip_size - start, remain);
770 for (i = 0; i < N; i++) {
771 sd = &vol->v_subdisks[no];
772 switch (sd->sd_state) {
773 case G_RAID_SUBDISK_S_ACTIVE:
774 case G_RAID_SUBDISK_S_STALE:
775 case G_RAID_SUBDISK_S_RESYNC:
777 case G_RAID_SUBDISK_S_REBUILD:
778 if (offset + start >= sd->sd_rebuild_pos)
784 cbp = g_clone_bio(bp);
787 cbp->bio_offset = offset + start;
788 cbp->bio_data = addr;
789 cbp->bio_length = length;
790 cbp->bio_caller1 = sd;
791 bioq_insert_tail(&queue, cbp);
793 if (++no >= vol->v_disks_count) {
795 offset += strip_size;
802 for (cbp = bioq_first(&queue); cbp != NULL;
803 cbp = bioq_first(&queue)) {
804 bioq_remove(&queue, cbp);
805 sd = cbp->bio_caller1;
806 cbp->bio_caller1 = NULL;
807 g_raid_subdisk_iostart(sd, cbp);
811 for (cbp = bioq_first(&queue); cbp != NULL;
812 cbp = bioq_first(&queue)) {
813 bioq_remove(&queue, cbp);
816 if (bp->bio_error == 0)
817 bp->bio_error = ENOMEM;
818 g_raid_iodone(bp, bp->bio_error);
822 g_raid_tr_iostart_raid1e(struct g_raid_tr_object *tr, struct bio *bp)
824 struct g_raid_volume *vol;
825 struct g_raid_tr_raid1e_object *trs;
827 vol = tr->tro_volume;
828 trs = (struct g_raid_tr_raid1e_object *)tr;
829 if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL &&
830 vol->v_state != G_RAID_VOLUME_S_SUBOPTIMAL &&
831 vol->v_state != G_RAID_VOLUME_S_DEGRADED) {
832 g_raid_iodone(bp, EIO);
836 * If we're rebuilding, squeeze in rebuild activity every so often,
837 * even when the disk is busy. Be sure to only count real I/O
838 * to the disk. All 'SPECIAL' I/O is traffic generated to the disk
841 if (trs->trso_failed_sd != NULL &&
842 !(bp->bio_cflags & G_RAID_BIO_FLAG_SPECIAL)) {
843 /* Make this new or running now round short. */
844 trs->trso_recover_slabs = 0;
845 if (--trs->trso_fair_io <= 0) {
846 trs->trso_fair_io = g_raid1e_rebuild_fair_io;
847 g_raid_tr_raid1e_rebuild_some(tr);
850 switch (bp->bio_cmd) {
852 g_raid_tr_iostart_raid1e_read(tr, bp);
855 g_raid_tr_iostart_raid1e_write(tr, bp);
858 g_raid_iodone(bp, EIO);
861 g_raid_tr_flush_common(tr, bp);
864 KASSERT(1 == 0, ("Invalid command here: %u (volume=%s)",
865 bp->bio_cmd, vol->v_name));
871 g_raid_tr_iodone_raid1e(struct g_raid_tr_object *tr,
872 struct g_raid_subdisk *sd, struct bio *bp)
875 struct g_raid_subdisk *nsd;
876 struct g_raid_volume *vol;
878 struct g_raid_tr_raid1e_object *trs;
879 off_t virtual, offset, start;
881 int error, do_write, copy, disk, best;
883 trs = (struct g_raid_tr_raid1e_object *)tr;
884 vol = tr->tro_volume;
885 if (bp->bio_cflags & G_RAID_BIO_FLAG_SYNC) {
886 if (trs->trso_type == TR_RAID1E_REBUILD) {
887 nsd = trs->trso_failed_sd;
888 if (bp->bio_cmd == BIO_READ) {
890 /* Immediately abort rebuild, if requested. */
891 if (trs->trso_flags & TR_RAID1E_F_ABORT) {
892 trs->trso_flags &= ~TR_RAID1E_F_DOING_SOME;
893 g_raid_tr_raid1e_rebuild_abort(tr);
897 /* On read error, skip and cross fingers. */
898 if (bp->bio_error != 0) {
900 "Read error during rebuild (%d), "
901 "possible data loss!",
903 goto rebuild_round_done;
907 * The read operation finished, queue the
910 G_RAID_LOGREQ(3, bp, "Rebuild read done: %d",
912 bp->bio_cmd = BIO_WRITE;
913 bp->bio_cflags = G_RAID_BIO_FLAG_SYNC;
914 bp->bio_offset = nsd->sd_rebuild_pos;
915 G_RAID_LOGREQ(3, bp, "Queueing rebuild write.");
916 g_raid_subdisk_iostart(nsd, bp);
919 * The write operation just finished. Do
920 * another. We keep cloning the master bio
921 * since it has the right buffers allocated to
924 G_RAID_LOGREQ(3, bp, "Rebuild write done: %d",
926 if (bp->bio_error != 0 ||
927 trs->trso_flags & TR_RAID1E_F_ABORT) {
928 if ((trs->trso_flags &
929 TR_RAID1E_F_ABORT) == 0) {
930 g_raid_tr_raid1e_fail_disk(sd->sd_softc,
933 trs->trso_flags &= ~TR_RAID1E_F_DOING_SOME;
934 g_raid_tr_raid1e_rebuild_abort(tr);
938 trs->trso_flags &= ~TR_RAID1E_F_LOCKED;
939 g_raid_unlock_range(tr->tro_volume,
940 trs->trso_lock_pos, trs->trso_lock_len);
941 nsd->sd_rebuild_pos += bp->bio_length;
942 if (nsd->sd_rebuild_pos >= nsd->sd_size) {
943 g_raid_tr_raid1e_rebuild_finish(tr);
947 /* Abort rebuild if we are stopping */
948 if (trs->trso_stopping) {
949 trs->trso_flags &= ~TR_RAID1E_F_DOING_SOME;
950 g_raid_tr_raid1e_rebuild_abort(tr);
954 if (--trs->trso_meta_update <= 0) {
955 g_raid_write_metadata(vol->v_softc,
956 vol, nsd, nsd->sd_disk);
957 trs->trso_meta_update =
958 g_raid1e_rebuild_meta_update;
959 /* Compensate short rebuild I/Os. */
960 if ((vol->v_disks_count % N) != 0 &&
962 g_raid1e_rebuild_slab) {
963 trs->trso_meta_update *=
964 g_raid1e_rebuild_slab;
965 trs->trso_meta_update /=
969 trs->trso_flags &= ~TR_RAID1E_F_DOING_SOME;
970 if (--trs->trso_recover_slabs <= 0)
972 /* Run next rebuild iteration. */
973 g_raid_tr_raid1e_rebuild_some(tr);
975 } else if (trs->trso_type == TR_RAID1E_RESYNC) {
977 * read good sd, read bad sd in parallel. when both
978 * done, compare the buffers. write good to the bad
979 * if different. do the next bit of work.
981 panic("Somehow, we think we're doing a resync");
985 pbp = bp->bio_parent;
987 mask = (intptr_t)bp->bio_caller2;
988 if (bp->bio_cmd == BIO_READ && bp->bio_error != 0) {
990 * Read failed on first drive. Retry the read error on
991 * another disk drive, if available, before erroring out the
994 sd->sd_disk->d_read_errs++;
996 "Read error (%d), %d read errors total",
997 bp->bio_error, sd->sd_disk->d_read_errs);
1000 * If there are too many read errors, we move to degraded.
1001 * XXX Do we want to FAIL the drive (eg, make the user redo
1002 * everything to get it back in sync), or just degrade the
1003 * drive, which kicks off a resync?
1006 if (sd->sd_disk->d_read_errs > g_raid_read_err_thresh)
1007 g_raid_tr_raid1e_fail_disk(sd->sd_softc, sd, sd->sd_disk);
1011 /* Restore what we were doing. */
1012 P2V(vol, sd->sd_pos, bp->bio_offset, &virtual, ©);
1013 V2P(vol, virtual, &disk, &offset, &start);
1015 /* Find the other disk, and try to do the I/O to it. */
1017 best = g_raid_tr_raid1e_select_read_disk(vol,
1018 disk, offset, start, mask);
1019 if (best >= 0 && (cbp = g_clone_bio(pbp)) != NULL) {
1021 if (disk >= vol->v_disks_count) {
1022 disk -= vol->v_disks_count;
1023 offset += vol->v_strip_size;
1025 cbp->bio_offset = offset + start;
1026 cbp->bio_length = bp->bio_length;
1027 cbp->bio_data = bp->bio_data;
1029 nsd = &vol->v_subdisks[disk];
1030 G_RAID_LOGREQ(2, cbp, "Retrying read from %d",
1034 if ((mask & (1 << 31)) != 0)
1036 cbp->bio_caller2 = (void *)mask;
1038 cbp->bio_caller1 = nsd;
1039 /* Lock callback starts I/O */
1040 g_raid_lock_range(sd->sd_volume,
1041 virtual, cbp->bio_length, pbp, cbp);
1043 g_raid_subdisk_iostart(nsd, cbp);
1048 * We can't retry. Return the original error by falling
1049 * through. This will happen when there's only one good disk.
1050 * We don't need to fail the raid, since its actual state is
1051 * based on the state of the subdisks.
1053 G_RAID_LOGREQ(2, bp, "Couldn't retry read, failing it");
1055 if (bp->bio_cmd == BIO_READ &&
1056 bp->bio_error == 0 &&
1057 (mask & (1 << 31)) != 0) {
1058 G_RAID_LOGREQ(3, bp, "Recovered data from other drive");
1060 /* Restore what we were doing. */
1061 P2V(vol, sd->sd_pos, bp->bio_offset, &virtual, ©);
1062 V2P(vol, virtual, &disk, &offset, &start);
1064 /* Find best disk to write. */
1065 best = g_raid_tr_raid1e_select_read_disk(vol,
1066 disk, offset, start, ~mask);
1067 if (best >= 0 && (cbp = g_clone_bio(pbp)) != NULL) {
1069 if (disk >= vol->v_disks_count) {
1070 disk -= vol->v_disks_count;
1071 offset += vol->v_strip_size;
1073 cbp->bio_offset = offset + start;
1074 cbp->bio_length = bp->bio_length;
1075 cbp->bio_data = bp->bio_data;
1076 cbp->bio_cmd = BIO_WRITE;
1077 cbp->bio_cflags = G_RAID_BIO_FLAG_REMAP;
1078 cbp->bio_caller2 = (void *)mask;
1080 G_RAID_LOGREQ(2, cbp,
1081 "Attempting bad sector remap on failing drive.");
1082 g_raid_subdisk_iostart(&vol->v_subdisks[disk], cbp);
1086 if ((mask & (1 << 31)) != 0) {
1088 * We're done with a recovery, mark the range as unlocked.
1089 * For any write errors, we agressively fail the disk since
1090 * there was both a READ and a WRITE error at this location.
1091 * Both types of errors generally indicates the drive is on
1092 * the verge of total failure anyway. Better to stop trusting
1093 * it now. However, we need to reset error to 0 in that case
1094 * because we're not failing the original I/O which succeeded.
1097 /* Restore what we were doing. */
1098 P2V(vol, sd->sd_pos, bp->bio_offset, &virtual, ©);
1099 V2P(vol, virtual, &disk, &offset, &start);
1101 for (copy = 0; copy < N; copy++) {
1102 if ((mask & (1 << copy) ) != 0)
1103 vol->v_subdisks[(disk + copy) %
1104 vol->v_disks_count].sd_recovery--;
1107 if (bp->bio_cmd == BIO_WRITE && bp->bio_error) {
1108 G_RAID_LOGREQ(0, bp, "Remap write failed: "
1109 "failing subdisk.");
1110 g_raid_tr_raid1e_fail_disk(sd->sd_softc, sd, sd->sd_disk);
1113 G_RAID_LOGREQ(2, bp, "REMAP done %d.", bp->bio_error);
1114 g_raid_unlock_range(sd->sd_volume, virtual, bp->bio_length);
1116 error = bp->bio_error;
1118 if (pbp->bio_children == pbp->bio_inbed) {
1119 pbp->bio_completed = pbp->bio_length;
1120 g_raid_iodone(pbp, error);
1125 g_raid_tr_kerneldump_raid1e(struct g_raid_tr_object *tr,
1126 void *virtual, vm_offset_t physical, off_t boffset, size_t blength)
1128 struct g_raid_volume *vol;
1129 struct g_raid_subdisk *sd;
1130 struct bio_queue_head queue;
1132 off_t offset, start, length, remain;
1133 u_int no, strip_size;
1136 vol = tr->tro_volume;
1138 strip_size = vol->v_strip_size;
1139 V2P(vol, boffset, &no, &offset, &start);
1142 while (remain > 0) {
1143 length = MIN(strip_size - start, remain);
1144 for (i = 0; i < N; i++) {
1145 sd = &vol->v_subdisks[no];
1146 switch (sd->sd_state) {
1147 case G_RAID_SUBDISK_S_ACTIVE:
1148 case G_RAID_SUBDISK_S_STALE:
1149 case G_RAID_SUBDISK_S_RESYNC:
1151 case G_RAID_SUBDISK_S_REBUILD:
1152 if (offset + start >= sd->sd_rebuild_pos)
1158 error = g_raid_subdisk_kerneldump(sd,
1159 addr, 0, offset + start, length);
1163 if (++no >= vol->v_disks_count) {
1165 offset += strip_size;
1176 g_raid_tr_locked_raid1e(struct g_raid_tr_object *tr, void *argp)
1179 struct g_raid_subdisk *sd;
1181 bp = (struct bio *)argp;
1182 sd = (struct g_raid_subdisk *)bp->bio_caller1;
1183 g_raid_subdisk_iostart(sd, bp);
1189 g_raid_tr_idle_raid1e(struct g_raid_tr_object *tr)
1191 struct g_raid_tr_raid1e_object *trs;
1192 struct g_raid_volume *vol;
1194 vol = tr->tro_volume;
1195 trs = (struct g_raid_tr_raid1e_object *)tr;
1196 trs->trso_fair_io = g_raid1e_rebuild_fair_io;
1197 trs->trso_recover_slabs = g_raid1e_rebuild_cluster_idle;
1198 /* Compensate short rebuild I/Os. */
1199 if ((vol->v_disks_count % N) != 0 &&
1200 vol->v_strip_size < g_raid1e_rebuild_slab) {
1201 trs->trso_recover_slabs *= g_raid1e_rebuild_slab;
1202 trs->trso_recover_slabs /= vol->v_strip_size;
1204 if (trs->trso_type == TR_RAID1E_REBUILD)
1205 g_raid_tr_raid1e_rebuild_some(tr);
1210 g_raid_tr_free_raid1e(struct g_raid_tr_object *tr)
1212 struct g_raid_tr_raid1e_object *trs;
1214 trs = (struct g_raid_tr_raid1e_object *)tr;
1216 if (trs->trso_buffer != NULL) {
1217 free(trs->trso_buffer, M_TR_RAID1E);
1218 trs->trso_buffer = NULL;
1223 G_RAID_TR_DECLARE(g_raid_tr_raid1e);