xref: /freebsd/sys/geom/raid/tr_raid1.c (revision 61ba55bcf70f2340f9c943c9571113b3fd8eda69)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/bio.h>
32 #include <sys/endian.h>
33 #include <sys/kernel.h>
34 #include <sys/kobj.h>
35 #include <sys/limits.h>
36 #include <sys/lock.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/geom_dbg.h>
43 #include "geom/raid/g_raid.h"
44 #include "g_raid_tr_if.h"
45 
46 SYSCTL_DECL(_kern_geom_raid_raid1);
47 
48 #define RAID1_REBUILD_SLAB	(1 << 20) /* One transation in a rebuild */
49 static int g_raid1_rebuild_slab = RAID1_REBUILD_SLAB;
50 SYSCTL_UINT(_kern_geom_raid_raid1, OID_AUTO, rebuild_slab_size, CTLFLAG_RWTUN,
51     &g_raid1_rebuild_slab, 0,
52     "Amount of the disk to rebuild each read/write cycle of the rebuild.");
53 
54 #define RAID1_REBUILD_FAIR_IO 20 /* use 1/x of the available I/O */
55 static int g_raid1_rebuild_fair_io = RAID1_REBUILD_FAIR_IO;
56 SYSCTL_UINT(_kern_geom_raid_raid1, OID_AUTO, rebuild_fair_io, CTLFLAG_RWTUN,
57     &g_raid1_rebuild_fair_io, 0,
58     "Fraction of the I/O bandwidth to use when disk busy for rebuild.");
59 
60 #define RAID1_REBUILD_CLUSTER_IDLE 100
61 static int g_raid1_rebuild_cluster_idle = RAID1_REBUILD_CLUSTER_IDLE;
62 SYSCTL_UINT(_kern_geom_raid_raid1, OID_AUTO, rebuild_cluster_idle, CTLFLAG_RWTUN,
63     &g_raid1_rebuild_cluster_idle, 0,
64     "Number of slabs to do each time we trigger a rebuild cycle");
65 
66 #define RAID1_REBUILD_META_UPDATE 1024 /* update meta data every 1GB or so */
67 static int g_raid1_rebuild_meta_update = RAID1_REBUILD_META_UPDATE;
68 SYSCTL_UINT(_kern_geom_raid_raid1, OID_AUTO, rebuild_meta_update, CTLFLAG_RWTUN,
69     &g_raid1_rebuild_meta_update, 0,
70     "When to update the meta data.");
71 
72 static MALLOC_DEFINE(M_TR_RAID1, "tr_raid1_data", "GEOM_RAID RAID1 data");
73 
74 #define TR_RAID1_NONE 0
75 #define TR_RAID1_REBUILD 1
76 #define TR_RAID1_RESYNC 2
77 
78 #define TR_RAID1_F_DOING_SOME	0x1
79 #define TR_RAID1_F_LOCKED	0x2
80 #define TR_RAID1_F_ABORT	0x4
81 
82 struct g_raid_tr_raid1_object {
83 	struct g_raid_tr_object	 trso_base;
84 	int			 trso_starting;
85 	int			 trso_stopping;
86 	int			 trso_type;
87 	int			 trso_recover_slabs; /* slabs before rest */
88 	int			 trso_fair_io;
89 	int			 trso_meta_update;
90 	int			 trso_flags;
91 	struct g_raid_subdisk	*trso_failed_sd; /* like per volume */
92 	void			*trso_buffer;	 /* Buffer space */
93 	struct bio		 trso_bio;
94 };
95 
96 static g_raid_tr_taste_t g_raid_tr_taste_raid1;
97 static g_raid_tr_event_t g_raid_tr_event_raid1;
98 static g_raid_tr_start_t g_raid_tr_start_raid1;
99 static g_raid_tr_stop_t g_raid_tr_stop_raid1;
100 static g_raid_tr_iostart_t g_raid_tr_iostart_raid1;
101 static g_raid_tr_iodone_t g_raid_tr_iodone_raid1;
102 static g_raid_tr_kerneldump_t g_raid_tr_kerneldump_raid1;
103 static g_raid_tr_locked_t g_raid_tr_locked_raid1;
104 static g_raid_tr_idle_t g_raid_tr_idle_raid1;
105 static g_raid_tr_free_t g_raid_tr_free_raid1;
106 
107 static kobj_method_t g_raid_tr_raid1_methods[] = {
108 	KOBJMETHOD(g_raid_tr_taste,	g_raid_tr_taste_raid1),
109 	KOBJMETHOD(g_raid_tr_event,	g_raid_tr_event_raid1),
110 	KOBJMETHOD(g_raid_tr_start,	g_raid_tr_start_raid1),
111 	KOBJMETHOD(g_raid_tr_stop,	g_raid_tr_stop_raid1),
112 	KOBJMETHOD(g_raid_tr_iostart,	g_raid_tr_iostart_raid1),
113 	KOBJMETHOD(g_raid_tr_iodone,	g_raid_tr_iodone_raid1),
114 	KOBJMETHOD(g_raid_tr_kerneldump, g_raid_tr_kerneldump_raid1),
115 	KOBJMETHOD(g_raid_tr_locked,	g_raid_tr_locked_raid1),
116 	KOBJMETHOD(g_raid_tr_idle,	g_raid_tr_idle_raid1),
117 	KOBJMETHOD(g_raid_tr_free,	g_raid_tr_free_raid1),
118 	{ 0, 0 }
119 };
120 
121 static struct g_raid_tr_class g_raid_tr_raid1_class = {
122 	"RAID1",
123 	g_raid_tr_raid1_methods,
124 	sizeof(struct g_raid_tr_raid1_object),
125 	.trc_enable = 1,
126 	.trc_priority = 100,
127 	.trc_accept_unmapped = 1
128 };
129 
130 static void g_raid_tr_raid1_rebuild_abort(struct g_raid_tr_object *tr);
131 static void g_raid_tr_raid1_maybe_rebuild(struct g_raid_tr_object *tr,
132     struct g_raid_subdisk *sd);
133 
134 static int
135 g_raid_tr_taste_raid1(struct g_raid_tr_object *tr, struct g_raid_volume *vol)
136 {
137 	struct g_raid_tr_raid1_object *trs;
138 
139 	trs = (struct g_raid_tr_raid1_object *)tr;
140 	if (tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_RAID1 ||
141 	    (tr->tro_volume->v_raid_level_qualifier != G_RAID_VOLUME_RLQ_R1SM &&
142 	     tr->tro_volume->v_raid_level_qualifier != G_RAID_VOLUME_RLQ_R1MM))
143 		return (G_RAID_TR_TASTE_FAIL);
144 	trs->trso_starting = 1;
145 	return (G_RAID_TR_TASTE_SUCCEED);
146 }
147 
148 static int
149 g_raid_tr_update_state_raid1(struct g_raid_volume *vol,
150     struct g_raid_subdisk *sd)
151 {
152 	struct g_raid_tr_raid1_object *trs;
153 	struct g_raid_softc *sc;
154 	struct g_raid_subdisk *tsd, *bestsd;
155 	u_int s;
156 	int i, na, ns;
157 
158 	sc = vol->v_softc;
159 	trs = (struct g_raid_tr_raid1_object *)vol->v_tr;
160 	if (trs->trso_stopping &&
161 	    (trs->trso_flags & TR_RAID1_F_DOING_SOME) == 0)
162 		s = G_RAID_VOLUME_S_STOPPED;
163 	else if (trs->trso_starting)
164 		s = G_RAID_VOLUME_S_STARTING;
165 	else {
166 		/* Make sure we have at least one ACTIVE disk. */
167 		na = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE);
168 		if (na == 0) {
169 			/*
170 			 * Critical situation! We have no any active disk!
171 			 * Choose the best disk we have to make it active.
172 			 */
173 			bestsd = &vol->v_subdisks[0];
174 			for (i = 1; i < vol->v_disks_count; i++) {
175 				tsd = &vol->v_subdisks[i];
176 				if (tsd->sd_state > bestsd->sd_state)
177 					bestsd = tsd;
178 				else if (tsd->sd_state == bestsd->sd_state &&
179 				    (tsd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
180 				     tsd->sd_state == G_RAID_SUBDISK_S_RESYNC) &&
181 				    tsd->sd_rebuild_pos > bestsd->sd_rebuild_pos)
182 					bestsd = tsd;
183 			}
184 			if (bestsd->sd_state >= G_RAID_SUBDISK_S_UNINITIALIZED) {
185 				/* We found reasonable candidate. */
186 				G_RAID_DEBUG1(1, sc,
187 				    "Promote subdisk %s:%d from %s to ACTIVE.",
188 				    vol->v_name, bestsd->sd_pos,
189 				    g_raid_subdisk_state2str(bestsd->sd_state));
190 				g_raid_change_subdisk_state(bestsd,
191 				    G_RAID_SUBDISK_S_ACTIVE);
192 				g_raid_write_metadata(sc,
193 				    vol, bestsd, bestsd->sd_disk);
194 			}
195 		}
196 		na = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE);
197 		ns = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_STALE) +
198 		     g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_RESYNC);
199 		if (na == vol->v_disks_count)
200 			s = G_RAID_VOLUME_S_OPTIMAL;
201 		else if (na + ns == vol->v_disks_count)
202 			s = G_RAID_VOLUME_S_SUBOPTIMAL;
203 		else if (na > 0)
204 			s = G_RAID_VOLUME_S_DEGRADED;
205 		else
206 			s = G_RAID_VOLUME_S_BROKEN;
207 		g_raid_tr_raid1_maybe_rebuild(vol->v_tr, sd);
208 	}
209 	if (s != vol->v_state) {
210 		g_raid_event_send(vol, G_RAID_VOLUME_S_ALIVE(s) ?
211 		    G_RAID_VOLUME_E_UP : G_RAID_VOLUME_E_DOWN,
212 		    G_RAID_EVENT_VOLUME);
213 		g_raid_change_volume_state(vol, s);
214 		if (!trs->trso_starting && !trs->trso_stopping)
215 			g_raid_write_metadata(sc, vol, NULL, NULL);
216 	}
217 	return (0);
218 }
219 
220 static void
221 g_raid_tr_raid1_fail_disk(struct g_raid_softc *sc, struct g_raid_subdisk *sd,
222     struct g_raid_disk *disk)
223 {
224 	/*
225 	 * We don't fail the last disk in the pack, since it still has decent
226 	 * data on it and that's better than failing the disk if it is the root
227 	 * file system.
228 	 *
229 	 * XXX should this be controlled via a tunable?  It makes sense for
230 	 * the volume that has / on it.  I can't think of a case where we'd
231 	 * want the volume to go away on this kind of event.
232 	 */
233 	if (g_raid_nsubdisks(sd->sd_volume, G_RAID_SUBDISK_S_ACTIVE) == 1 &&
234 	    g_raid_get_subdisk(sd->sd_volume, G_RAID_SUBDISK_S_ACTIVE) == sd)
235 		return;
236 	g_raid_fail_disk(sc, sd, disk);
237 }
238 
239 static void
240 g_raid_tr_raid1_rebuild_some(struct g_raid_tr_object *tr)
241 {
242 	struct g_raid_tr_raid1_object *trs;
243 	struct g_raid_subdisk *sd, *good_sd;
244 	struct bio *bp;
245 
246 	trs = (struct g_raid_tr_raid1_object *)tr;
247 	if (trs->trso_flags & TR_RAID1_F_DOING_SOME)
248 		return;
249 	sd = trs->trso_failed_sd;
250 	good_sd = g_raid_get_subdisk(sd->sd_volume, G_RAID_SUBDISK_S_ACTIVE);
251 	if (good_sd == NULL) {
252 		g_raid_tr_raid1_rebuild_abort(tr);
253 		return;
254 	}
255 	bp = &trs->trso_bio;
256 	memset(bp, 0, sizeof(*bp));
257 	bp->bio_offset = sd->sd_rebuild_pos;
258 	bp->bio_length = MIN(g_raid1_rebuild_slab,
259 	    sd->sd_size - sd->sd_rebuild_pos);
260 	bp->bio_data = trs->trso_buffer;
261 	bp->bio_cmd = BIO_READ;
262 	bp->bio_cflags = G_RAID_BIO_FLAG_SYNC;
263 	bp->bio_caller1 = good_sd;
264 	trs->trso_flags |= TR_RAID1_F_DOING_SOME;
265 	trs->trso_flags |= TR_RAID1_F_LOCKED;
266 	g_raid_lock_range(sd->sd_volume,	/* Lock callback starts I/O */
267 	   bp->bio_offset, bp->bio_length, NULL, bp);
268 }
269 
270 static void
271 g_raid_tr_raid1_rebuild_done(struct g_raid_tr_raid1_object *trs)
272 {
273 	struct g_raid_volume *vol;
274 	struct g_raid_subdisk *sd;
275 
276 	vol = trs->trso_base.tro_volume;
277 	sd = trs->trso_failed_sd;
278 	g_raid_write_metadata(vol->v_softc, vol, sd, sd->sd_disk);
279 	free(trs->trso_buffer, M_TR_RAID1);
280 	trs->trso_buffer = NULL;
281 	trs->trso_flags &= ~TR_RAID1_F_DOING_SOME;
282 	trs->trso_type = TR_RAID1_NONE;
283 	trs->trso_recover_slabs = 0;
284 	trs->trso_failed_sd = NULL;
285 	g_raid_tr_update_state_raid1(vol, NULL);
286 }
287 
288 static void
289 g_raid_tr_raid1_rebuild_finish(struct g_raid_tr_object *tr)
290 {
291 	struct g_raid_tr_raid1_object *trs;
292 	struct g_raid_subdisk *sd;
293 
294 	trs = (struct g_raid_tr_raid1_object *)tr;
295 	sd = trs->trso_failed_sd;
296 	G_RAID_DEBUG1(0, tr->tro_volume->v_softc,
297 	    "Subdisk %s:%d-%s rebuild completed.",
298 	    sd->sd_volume->v_name, sd->sd_pos,
299 	    sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]");
300 	g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_ACTIVE);
301 	sd->sd_rebuild_pos = 0;
302 	g_raid_tr_raid1_rebuild_done(trs);
303 }
304 
305 static void
306 g_raid_tr_raid1_rebuild_abort(struct g_raid_tr_object *tr)
307 {
308 	struct g_raid_tr_raid1_object *trs;
309 	struct g_raid_subdisk *sd;
310 	struct g_raid_volume *vol;
311 	off_t len;
312 
313 	vol = tr->tro_volume;
314 	trs = (struct g_raid_tr_raid1_object *)tr;
315 	sd = trs->trso_failed_sd;
316 	if (trs->trso_flags & TR_RAID1_F_DOING_SOME) {
317 		G_RAID_DEBUG1(1, vol->v_softc,
318 		    "Subdisk %s:%d-%s rebuild is aborting.",
319 		    sd->sd_volume->v_name, sd->sd_pos,
320 		    sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]");
321 		trs->trso_flags |= TR_RAID1_F_ABORT;
322 	} else {
323 		G_RAID_DEBUG1(0, vol->v_softc,
324 		    "Subdisk %s:%d-%s rebuild aborted.",
325 		    sd->sd_volume->v_name, sd->sd_pos,
326 		    sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]");
327 		trs->trso_flags &= ~TR_RAID1_F_ABORT;
328 		if (trs->trso_flags & TR_RAID1_F_LOCKED) {
329 			trs->trso_flags &= ~TR_RAID1_F_LOCKED;
330 			len = MIN(g_raid1_rebuild_slab,
331 			    sd->sd_size - sd->sd_rebuild_pos);
332 			g_raid_unlock_range(tr->tro_volume,
333 			    sd->sd_rebuild_pos, len);
334 		}
335 		g_raid_tr_raid1_rebuild_done(trs);
336 	}
337 }
338 
339 static void
340 g_raid_tr_raid1_rebuild_start(struct g_raid_tr_object *tr)
341 {
342 	struct g_raid_volume *vol;
343 	struct g_raid_tr_raid1_object *trs;
344 	struct g_raid_subdisk *sd, *fsd;
345 
346 	vol = tr->tro_volume;
347 	trs = (struct g_raid_tr_raid1_object *)tr;
348 	if (trs->trso_failed_sd) {
349 		G_RAID_DEBUG1(1, vol->v_softc,
350 		    "Already rebuild in start rebuild. pos %jd\n",
351 		    (intmax_t)trs->trso_failed_sd->sd_rebuild_pos);
352 		return;
353 	}
354 	sd = g_raid_get_subdisk(vol, G_RAID_SUBDISK_S_ACTIVE);
355 	if (sd == NULL) {
356 		G_RAID_DEBUG1(1, vol->v_softc,
357 		    "No active disk to rebuild.  night night.");
358 		return;
359 	}
360 	fsd = g_raid_get_subdisk(vol, G_RAID_SUBDISK_S_RESYNC);
361 	if (fsd == NULL)
362 		fsd = g_raid_get_subdisk(vol, G_RAID_SUBDISK_S_REBUILD);
363 	if (fsd == NULL) {
364 		fsd = g_raid_get_subdisk(vol, G_RAID_SUBDISK_S_STALE);
365 		if (fsd != NULL) {
366 			fsd->sd_rebuild_pos = 0;
367 			g_raid_change_subdisk_state(fsd,
368 			    G_RAID_SUBDISK_S_RESYNC);
369 			g_raid_write_metadata(vol->v_softc, vol, fsd, NULL);
370 		} else {
371 			fsd = g_raid_get_subdisk(vol,
372 			    G_RAID_SUBDISK_S_UNINITIALIZED);
373 			if (fsd == NULL)
374 				fsd = g_raid_get_subdisk(vol,
375 				    G_RAID_SUBDISK_S_NEW);
376 			if (fsd != NULL) {
377 				fsd->sd_rebuild_pos = 0;
378 				g_raid_change_subdisk_state(fsd,
379 				    G_RAID_SUBDISK_S_REBUILD);
380 				g_raid_write_metadata(vol->v_softc,
381 				    vol, fsd, NULL);
382 			}
383 		}
384 	}
385 	if (fsd == NULL) {
386 		G_RAID_DEBUG1(1, vol->v_softc,
387 		    "No failed disk to rebuild.  night night.");
388 		return;
389 	}
390 	trs->trso_failed_sd = fsd;
391 	G_RAID_DEBUG1(0, vol->v_softc,
392 	    "Subdisk %s:%d-%s rebuild start at %jd.",
393 	    fsd->sd_volume->v_name, fsd->sd_pos,
394 	    fsd->sd_disk ? g_raid_get_diskname(fsd->sd_disk) : "[none]",
395 	    trs->trso_failed_sd->sd_rebuild_pos);
396 	trs->trso_type = TR_RAID1_REBUILD;
397 	trs->trso_buffer = malloc(g_raid1_rebuild_slab, M_TR_RAID1, M_WAITOK);
398 	trs->trso_meta_update = g_raid1_rebuild_meta_update;
399 	g_raid_tr_raid1_rebuild_some(tr);
400 }
401 
402 static void
403 g_raid_tr_raid1_maybe_rebuild(struct g_raid_tr_object *tr,
404     struct g_raid_subdisk *sd)
405 {
406 	struct g_raid_volume *vol;
407 	struct g_raid_tr_raid1_object *trs;
408 	int na, nr;
409 
410 	/*
411 	 * If we're stopping, don't do anything.  If we don't have at least one
412 	 * good disk and one bad disk, we don't do anything.  And if there's a
413 	 * 'good disk' stored in the trs, then we're in progress and we punt.
414 	 * If we make it past all these checks, we need to rebuild.
415 	 */
416 	vol = tr->tro_volume;
417 	trs = (struct g_raid_tr_raid1_object *)tr;
418 	if (trs->trso_stopping)
419 		return;
420 	na = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE);
421 	nr = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_REBUILD) +
422 	    g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_RESYNC);
423 	switch(trs->trso_type) {
424 	case TR_RAID1_NONE:
425 		if (na == 0)
426 			return;
427 		if (nr == 0) {
428 			nr = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_NEW) +
429 			    g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_STALE) +
430 			    g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_UNINITIALIZED);
431 			if (nr == 0)
432 				return;
433 		}
434 		g_raid_tr_raid1_rebuild_start(tr);
435 		break;
436 	case TR_RAID1_REBUILD:
437 		if (na == 0 || nr == 0 || trs->trso_failed_sd == sd)
438 			g_raid_tr_raid1_rebuild_abort(tr);
439 		break;
440 	case TR_RAID1_RESYNC:
441 		break;
442 	}
443 }
444 
445 static int
446 g_raid_tr_event_raid1(struct g_raid_tr_object *tr,
447     struct g_raid_subdisk *sd, u_int event)
448 {
449 
450 	g_raid_tr_update_state_raid1(tr->tro_volume, sd);
451 	return (0);
452 }
453 
454 static int
455 g_raid_tr_start_raid1(struct g_raid_tr_object *tr)
456 {
457 	struct g_raid_tr_raid1_object *trs;
458 	struct g_raid_volume *vol;
459 
460 	trs = (struct g_raid_tr_raid1_object *)tr;
461 	vol = tr->tro_volume;
462 	trs->trso_starting = 0;
463 	g_raid_tr_update_state_raid1(vol, NULL);
464 	return (0);
465 }
466 
467 static int
468 g_raid_tr_stop_raid1(struct g_raid_tr_object *tr)
469 {
470 	struct g_raid_tr_raid1_object *trs;
471 	struct g_raid_volume *vol;
472 
473 	trs = (struct g_raid_tr_raid1_object *)tr;
474 	vol = tr->tro_volume;
475 	trs->trso_starting = 0;
476 	trs->trso_stopping = 1;
477 	g_raid_tr_update_state_raid1(vol, NULL);
478 	return (0);
479 }
480 
481 /*
482  * Select the disk to read from.  Take into account: subdisk state, running
483  * error recovery, average disk load, head position and possible cache hits.
484  */
485 #define ABS(x)		(((x) >= 0) ? (x) : (-(x)))
486 static struct g_raid_subdisk *
487 g_raid_tr_raid1_select_read_disk(struct g_raid_volume *vol, struct bio *bp,
488     u_int mask)
489 {
490 	struct g_raid_subdisk *sd, *best;
491 	int i, prio, bestprio;
492 
493 	best = NULL;
494 	bestprio = INT_MAX;
495 	for (i = 0; i < vol->v_disks_count; i++) {
496 		sd = &vol->v_subdisks[i];
497 		if (sd->sd_state != G_RAID_SUBDISK_S_ACTIVE &&
498 		    ((sd->sd_state != G_RAID_SUBDISK_S_REBUILD &&
499 		      sd->sd_state != G_RAID_SUBDISK_S_RESYNC) ||
500 		     bp->bio_offset + bp->bio_length > sd->sd_rebuild_pos))
501 			continue;
502 		if ((mask & (1 << i)) != 0)
503 			continue;
504 		prio = G_RAID_SUBDISK_LOAD(sd);
505 		prio += min(sd->sd_recovery, 255) << 22;
506 		prio += (G_RAID_SUBDISK_S_ACTIVE - sd->sd_state) << 16;
507 		/* If disk head is precisely in position - highly prefer it. */
508 		if (G_RAID_SUBDISK_POS(sd) == bp->bio_offset)
509 			prio -= 2 * G_RAID_SUBDISK_LOAD_SCALE;
510 		else
511 		/* If disk head is close to position - prefer it. */
512 		if (ABS(G_RAID_SUBDISK_POS(sd) - bp->bio_offset) <
513 		    G_RAID_SUBDISK_TRACK_SIZE)
514 			prio -= 1 * G_RAID_SUBDISK_LOAD_SCALE;
515 		if (prio < bestprio) {
516 			best = sd;
517 			bestprio = prio;
518 		}
519 	}
520 	return (best);
521 }
522 
523 static void
524 g_raid_tr_iostart_raid1_read(struct g_raid_tr_object *tr, struct bio *bp)
525 {
526 	struct g_raid_subdisk *sd;
527 	struct bio *cbp;
528 
529 	sd = g_raid_tr_raid1_select_read_disk(tr->tro_volume, bp, 0);
530 	KASSERT(sd != NULL, ("No active disks in volume %s.",
531 		tr->tro_volume->v_name));
532 
533 	cbp = g_clone_bio(bp);
534 	if (cbp == NULL) {
535 		g_raid_iodone(bp, ENOMEM);
536 		return;
537 	}
538 
539 	g_raid_subdisk_iostart(sd, cbp);
540 }
541 
542 static void
543 g_raid_tr_iostart_raid1_write(struct g_raid_tr_object *tr, struct bio *bp)
544 {
545 	struct g_raid_volume *vol;
546 	struct g_raid_subdisk *sd;
547 	struct bio_queue_head queue;
548 	struct bio *cbp;
549 	int i;
550 
551 	vol = tr->tro_volume;
552 
553 	/*
554 	 * Allocate all bios before sending any request, so we can return
555 	 * ENOMEM in nice and clean way.
556 	 */
557 	bioq_init(&queue);
558 	for (i = 0; i < vol->v_disks_count; i++) {
559 		sd = &vol->v_subdisks[i];
560 		switch (sd->sd_state) {
561 		case G_RAID_SUBDISK_S_ACTIVE:
562 			break;
563 		case G_RAID_SUBDISK_S_REBUILD:
564 			/*
565 			 * When rebuilding, only part of this subdisk is
566 			 * writable, the rest will be written as part of the
567 			 * that process.
568 			 */
569 			if (bp->bio_offset >= sd->sd_rebuild_pos)
570 				continue;
571 			break;
572 		case G_RAID_SUBDISK_S_STALE:
573 		case G_RAID_SUBDISK_S_RESYNC:
574 			/*
575 			 * Resyncing still writes on the theory that the
576 			 * resync'd disk is very close and writing it will
577 			 * keep it that way better if we keep up while
578 			 * resyncing.
579 			 */
580 			break;
581 		default:
582 			continue;
583 		}
584 		cbp = g_clone_bio(bp);
585 		if (cbp == NULL)
586 			goto failure;
587 		cbp->bio_caller1 = sd;
588 		bioq_insert_tail(&queue, cbp);
589 	}
590 	while ((cbp = bioq_takefirst(&queue)) != NULL) {
591 		sd = cbp->bio_caller1;
592 		cbp->bio_caller1 = NULL;
593 		g_raid_subdisk_iostart(sd, cbp);
594 	}
595 	return;
596 failure:
597 	while ((cbp = bioq_takefirst(&queue)) != NULL)
598 		g_destroy_bio(cbp);
599 	if (bp->bio_error == 0)
600 		bp->bio_error = ENOMEM;
601 	g_raid_iodone(bp, bp->bio_error);
602 }
603 
604 static void
605 g_raid_tr_iostart_raid1(struct g_raid_tr_object *tr, struct bio *bp)
606 {
607 	struct g_raid_volume *vol;
608 	struct g_raid_tr_raid1_object *trs;
609 
610 	vol = tr->tro_volume;
611 	trs = (struct g_raid_tr_raid1_object *)tr;
612 	if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL &&
613 	    vol->v_state != G_RAID_VOLUME_S_SUBOPTIMAL &&
614 	    vol->v_state != G_RAID_VOLUME_S_DEGRADED) {
615 		g_raid_iodone(bp, EIO);
616 		return;
617 	}
618 	/*
619 	 * If we're rebuilding, squeeze in rebuild activity every so often,
620 	 * even when the disk is busy.  Be sure to only count real I/O
621 	 * to the disk.  All 'SPECIAL' I/O is traffic generated to the disk
622 	 * by this module.
623 	 */
624 	if (trs->trso_failed_sd != NULL &&
625 	    !(bp->bio_cflags & G_RAID_BIO_FLAG_SPECIAL)) {
626 		/* Make this new or running now round short. */
627 		trs->trso_recover_slabs = 0;
628 		if (--trs->trso_fair_io <= 0) {
629 			trs->trso_fair_io = g_raid1_rebuild_fair_io;
630 			g_raid_tr_raid1_rebuild_some(tr);
631 		}
632 	}
633 	switch (bp->bio_cmd) {
634 	case BIO_READ:
635 		g_raid_tr_iostart_raid1_read(tr, bp);
636 		break;
637 	case BIO_WRITE:
638 	case BIO_DELETE:
639 		g_raid_tr_iostart_raid1_write(tr, bp);
640 		break;
641 	case BIO_SPEEDUP:
642 	case BIO_FLUSH:
643 		g_raid_tr_flush_common(tr, bp);
644 		break;
645 	default:
646 		KASSERT(1 == 0, ("Invalid command here: %u (volume=%s)",
647 		    bp->bio_cmd, vol->v_name));
648 		break;
649 	}
650 }
651 
652 static void
653 g_raid_tr_iodone_raid1(struct g_raid_tr_object *tr,
654     struct g_raid_subdisk *sd, struct bio *bp)
655 {
656 	struct bio *cbp;
657 	struct g_raid_subdisk *nsd;
658 	struct g_raid_volume *vol;
659 	struct bio *pbp;
660 	struct g_raid_tr_raid1_object *trs;
661 	uintptr_t *mask;
662 	int error, do_write;
663 
664 	trs = (struct g_raid_tr_raid1_object *)tr;
665 	vol = tr->tro_volume;
666 	if (bp->bio_cflags & G_RAID_BIO_FLAG_SYNC) {
667 		/*
668 		 * This operation is part of a rebuild or resync operation.
669 		 * See what work just got done, then schedule the next bit of
670 		 * work, if any.  Rebuild/resync is done a little bit at a
671 		 * time.  Either when a timeout happens, or after we get a
672 		 * bunch of I/Os to the disk (to make sure an active system
673 		 * will complete in a sane amount of time).
674 		 *
675 		 * We are setup to do differing amounts of work for each of
676 		 * these cases.  so long as the slabs is smallish (less than
677 		 * 50 or so, I'd guess, but that's just a WAG), we shouldn't
678 		 * have any bio starvation issues.  For active disks, we do
679 		 * 5MB of data, for inactive ones, we do 50MB.
680 		 */
681 		if (trs->trso_type == TR_RAID1_REBUILD) {
682 			if (bp->bio_cmd == BIO_READ) {
683 				/* Immediately abort rebuild, if requested. */
684 				if (trs->trso_flags & TR_RAID1_F_ABORT) {
685 					trs->trso_flags &= ~TR_RAID1_F_DOING_SOME;
686 					g_raid_tr_raid1_rebuild_abort(tr);
687 					return;
688 				}
689 
690 				/* On read error, skip and cross fingers. */
691 				if (bp->bio_error != 0) {
692 					G_RAID_LOGREQ(0, bp,
693 					    "Read error during rebuild (%d), "
694 					    "possible data loss!",
695 					    bp->bio_error);
696 					goto rebuild_round_done;
697 				}
698 
699 				/*
700 				 * The read operation finished, queue the
701 				 * write and get out.
702 				 */
703 				G_RAID_LOGREQ(4, bp, "rebuild read done. %d",
704 				    bp->bio_error);
705 				bp->bio_cmd = BIO_WRITE;
706 				bp->bio_cflags = G_RAID_BIO_FLAG_SYNC;
707 				G_RAID_LOGREQ(4, bp, "Queueing rebuild write.");
708 				g_raid_subdisk_iostart(trs->trso_failed_sd, bp);
709 			} else {
710 				/*
711 				 * The write operation just finished.  Do
712 				 * another.  We keep cloning the master bio
713 				 * since it has the right buffers allocated to
714 				 * it.
715 				 */
716 				G_RAID_LOGREQ(4, bp,
717 				    "rebuild write done. Error %d",
718 				    bp->bio_error);
719 				nsd = trs->trso_failed_sd;
720 				if (bp->bio_error != 0 ||
721 				    trs->trso_flags & TR_RAID1_F_ABORT) {
722 					if ((trs->trso_flags &
723 					    TR_RAID1_F_ABORT) == 0) {
724 						g_raid_tr_raid1_fail_disk(sd->sd_softc,
725 						    nsd, nsd->sd_disk);
726 					}
727 					trs->trso_flags &= ~TR_RAID1_F_DOING_SOME;
728 					g_raid_tr_raid1_rebuild_abort(tr);
729 					return;
730 				}
731 rebuild_round_done:
732 				nsd = trs->trso_failed_sd;
733 				trs->trso_flags &= ~TR_RAID1_F_LOCKED;
734 				g_raid_unlock_range(sd->sd_volume,
735 				    bp->bio_offset, bp->bio_length);
736 				nsd->sd_rebuild_pos += bp->bio_length;
737 				if (nsd->sd_rebuild_pos >= nsd->sd_size) {
738 					g_raid_tr_raid1_rebuild_finish(tr);
739 					return;
740 				}
741 
742 				/* Abort rebuild if we are stopping */
743 				if (trs->trso_stopping) {
744 					trs->trso_flags &= ~TR_RAID1_F_DOING_SOME;
745 					g_raid_tr_raid1_rebuild_abort(tr);
746 					return;
747 				}
748 
749 				if (--trs->trso_meta_update <= 0) {
750 					g_raid_write_metadata(vol->v_softc,
751 					    vol, nsd, nsd->sd_disk);
752 					trs->trso_meta_update =
753 					    g_raid1_rebuild_meta_update;
754 				}
755 				trs->trso_flags &= ~TR_RAID1_F_DOING_SOME;
756 				if (--trs->trso_recover_slabs <= 0)
757 					return;
758 				g_raid_tr_raid1_rebuild_some(tr);
759 			}
760 		} else if (trs->trso_type == TR_RAID1_RESYNC) {
761 			/*
762 			 * read good sd, read bad sd in parallel.  when both
763 			 * done, compare the buffers.  write good to the bad
764 			 * if different.  do the next bit of work.
765 			 */
766 			panic("Somehow, we think we're doing a resync");
767 		}
768 		return;
769 	}
770 	pbp = bp->bio_parent;
771 	pbp->bio_inbed++;
772 	if (bp->bio_cmd == BIO_READ && bp->bio_error != 0) {
773 		/*
774 		 * Read failed on first drive.  Retry the read error on
775 		 * another disk drive, if available, before erroring out the
776 		 * read.
777 		 */
778 		sd->sd_disk->d_read_errs++;
779 		G_RAID_LOGREQ(0, bp,
780 		    "Read error (%d), %d read errors total",
781 		    bp->bio_error, sd->sd_disk->d_read_errs);
782 
783 		/*
784 		 * If there are too many read errors, we move to degraded.
785 		 * XXX Do we want to FAIL the drive (eg, make the user redo
786 		 * everything to get it back in sync), or just degrade the
787 		 * drive, which kicks off a resync?
788 		 */
789 		do_write = 1;
790 		if (sd->sd_disk->d_read_errs > g_raid_read_err_thresh) {
791 			g_raid_tr_raid1_fail_disk(sd->sd_softc, sd, sd->sd_disk);
792 			if (pbp->bio_children == 1)
793 				do_write = 0;
794 		}
795 
796 		/*
797 		 * Find the other disk, and try to do the I/O to it.
798 		 */
799 		mask = (uintptr_t *)(&pbp->bio_driver2);
800 		if (pbp->bio_children == 1) {
801 			/* Save original subdisk. */
802 			pbp->bio_driver1 = do_write ? sd : NULL;
803 			*mask = 0;
804 		}
805 		*mask |= 1 << sd->sd_pos;
806 		nsd = g_raid_tr_raid1_select_read_disk(vol, pbp, *mask);
807 		if (nsd != NULL && (cbp = g_clone_bio(pbp)) != NULL) {
808 			g_destroy_bio(bp);
809 			G_RAID_LOGREQ(2, cbp, "Retrying read from %d",
810 			    nsd->sd_pos);
811 			if (pbp->bio_children == 2 && do_write) {
812 				sd->sd_recovery++;
813 				cbp->bio_caller1 = nsd;
814 				pbp->bio_pflags = G_RAID_BIO_FLAG_LOCKED;
815 				/* Lock callback starts I/O */
816 				g_raid_lock_range(sd->sd_volume,
817 				    cbp->bio_offset, cbp->bio_length, pbp, cbp);
818 			} else {
819 				g_raid_subdisk_iostart(nsd, cbp);
820 			}
821 			return;
822 		}
823 		/*
824 		 * We can't retry.  Return the original error by falling
825 		 * through.  This will happen when there's only one good disk.
826 		 * We don't need to fail the raid, since its actual state is
827 		 * based on the state of the subdisks.
828 		 */
829 		G_RAID_LOGREQ(2, bp, "Couldn't retry read, failing it");
830 	}
831 	if (bp->bio_cmd == BIO_READ &&
832 	    bp->bio_error == 0 &&
833 	    pbp->bio_children > 1 &&
834 	    pbp->bio_driver1 != NULL) {
835 		/*
836 		 * If it was a read, and bio_children is >1, then we just
837 		 * recovered the data from the second drive.  We should try to
838 		 * write that data to the first drive if sector remapping is
839 		 * enabled.  A write should put the data in a new place on the
840 		 * disk, remapping the bad sector.  Do we need to do that by
841 		 * queueing a request to the main worker thread?  It doesn't
842 		 * affect the return code of this current read, and can be
843 		 * done at our leisure.  However, to make the code simpler, it
844 		 * is done synchronously.
845 		 */
846 		G_RAID_LOGREQ(3, bp, "Recovered data from other drive");
847 		cbp = g_clone_bio(pbp);
848 		if (cbp != NULL) {
849 			g_destroy_bio(bp);
850 			cbp->bio_cmd = BIO_WRITE;
851 			cbp->bio_cflags = G_RAID_BIO_FLAG_REMAP;
852 			G_RAID_LOGREQ(2, cbp,
853 			    "Attempting bad sector remap on failing drive.");
854 			g_raid_subdisk_iostart(pbp->bio_driver1, cbp);
855 			return;
856 		}
857 	}
858 	if (pbp->bio_pflags & G_RAID_BIO_FLAG_LOCKED) {
859 		/*
860 		 * We're done with a recovery, mark the range as unlocked.
861 		 * For any write errors, we aggressively fail the disk since
862 		 * there was both a READ and a WRITE error at this location.
863 		 * Both types of errors generally indicates the drive is on
864 		 * the verge of total failure anyway.  Better to stop trusting
865 		 * it now.  However, we need to reset error to 0 in that case
866 		 * because we're not failing the original I/O which succeeded.
867 		 */
868 		if (bp->bio_cmd == BIO_WRITE && bp->bio_error) {
869 			G_RAID_LOGREQ(0, bp, "Remap write failed: "
870 			    "failing subdisk.");
871 			g_raid_tr_raid1_fail_disk(sd->sd_softc, sd, sd->sd_disk);
872 			bp->bio_error = 0;
873 		}
874 		if (pbp->bio_driver1 != NULL) {
875 			((struct g_raid_subdisk *)pbp->bio_driver1)
876 			    ->sd_recovery--;
877 		}
878 		G_RAID_LOGREQ(2, bp, "REMAP done %d.", bp->bio_error);
879 		g_raid_unlock_range(sd->sd_volume, bp->bio_offset,
880 		    bp->bio_length);
881 	}
882 	if (pbp->bio_cmd != BIO_READ) {
883 		if (pbp->bio_inbed == 1 || pbp->bio_error != 0)
884 			pbp->bio_error = bp->bio_error;
885 		if (pbp->bio_cmd == BIO_WRITE && bp->bio_error != 0) {
886 			G_RAID_LOGREQ(0, bp, "Write failed: failing subdisk.");
887 			g_raid_tr_raid1_fail_disk(sd->sd_softc, sd, sd->sd_disk);
888 		}
889 		error = pbp->bio_error;
890 	} else
891 		error = bp->bio_error;
892 	g_destroy_bio(bp);
893 	if (pbp->bio_children == pbp->bio_inbed) {
894 		pbp->bio_completed = pbp->bio_length;
895 		g_raid_iodone(pbp, error);
896 	}
897 }
898 
899 static int
900 g_raid_tr_kerneldump_raid1(struct g_raid_tr_object *tr, void *virtual,
901     off_t offset, size_t length)
902 {
903 	struct g_raid_volume *vol;
904 	struct g_raid_subdisk *sd;
905 	int error, i, ok;
906 
907 	vol = tr->tro_volume;
908 	error = 0;
909 	ok = 0;
910 	for (i = 0; i < vol->v_disks_count; i++) {
911 		sd = &vol->v_subdisks[i];
912 		switch (sd->sd_state) {
913 		case G_RAID_SUBDISK_S_ACTIVE:
914 			break;
915 		case G_RAID_SUBDISK_S_REBUILD:
916 			/*
917 			 * When rebuilding, only part of this subdisk is
918 			 * writable, the rest will be written as part of the
919 			 * that process.
920 			 */
921 			if (offset >= sd->sd_rebuild_pos)
922 				continue;
923 			break;
924 		case G_RAID_SUBDISK_S_STALE:
925 		case G_RAID_SUBDISK_S_RESYNC:
926 			/*
927 			 * Resyncing still writes on the theory that the
928 			 * resync'd disk is very close and writing it will
929 			 * keep it that way better if we keep up while
930 			 * resyncing.
931 			 */
932 			break;
933 		default:
934 			continue;
935 		}
936 		error = g_raid_subdisk_kerneldump(sd, virtual, offset, length);
937 		if (error == 0)
938 			ok++;
939 	}
940 	return (ok > 0 ? 0 : error);
941 }
942 
943 static int
944 g_raid_tr_locked_raid1(struct g_raid_tr_object *tr, void *argp)
945 {
946 	struct bio *bp;
947 	struct g_raid_subdisk *sd;
948 
949 	bp = (struct bio *)argp;
950 	sd = (struct g_raid_subdisk *)bp->bio_caller1;
951 	g_raid_subdisk_iostart(sd, bp);
952 
953 	return (0);
954 }
955 
956 static int
957 g_raid_tr_idle_raid1(struct g_raid_tr_object *tr)
958 {
959 	struct g_raid_tr_raid1_object *trs;
960 
961 	trs = (struct g_raid_tr_raid1_object *)tr;
962 	trs->trso_fair_io = g_raid1_rebuild_fair_io;
963 	trs->trso_recover_slabs = g_raid1_rebuild_cluster_idle;
964 	if (trs->trso_type == TR_RAID1_REBUILD)
965 		g_raid_tr_raid1_rebuild_some(tr);
966 	return (0);
967 }
968 
969 static int
970 g_raid_tr_free_raid1(struct g_raid_tr_object *tr)
971 {
972 	struct g_raid_tr_raid1_object *trs;
973 
974 	trs = (struct g_raid_tr_raid1_object *)tr;
975 
976 	if (trs->trso_buffer != NULL) {
977 		free(trs->trso_buffer, M_TR_RAID1);
978 		trs->trso_buffer = NULL;
979 	}
980 	return (0);
981 }
982 
983 G_RAID_TR_DECLARE(raid1, "RAID1");
984