xref: /openbsd-src/sys/dev/softraid_raid1.c (revision f6d8fcaed18e5ccb8e77952c23ec09be39e71cef)
1*f6d8fcaeSderaadt /* $OpenBSD: softraid_raid1.c,v 1.67 2021/05/16 15:12:37 deraadt Exp $ */
228ffa013Stedu /*
328ffa013Stedu  * Copyright (c) 2007 Marco Peereboom <marco@peereboom.us>
428ffa013Stedu  *
528ffa013Stedu  * Permission to use, copy, modify, and distribute this software for any
628ffa013Stedu  * purpose with or without fee is hereby granted, provided that the above
728ffa013Stedu  * copyright notice and this permission notice appear in all copies.
828ffa013Stedu  *
928ffa013Stedu  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1028ffa013Stedu  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1128ffa013Stedu  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1228ffa013Stedu  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1328ffa013Stedu  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1428ffa013Stedu  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1528ffa013Stedu  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1628ffa013Stedu  */
1728ffa013Stedu 
1828ffa013Stedu #include "bio.h"
1928ffa013Stedu 
2028ffa013Stedu #include <sys/param.h>
2128ffa013Stedu #include <sys/systm.h>
2228ffa013Stedu #include <sys/buf.h>
2328ffa013Stedu #include <sys/device.h>
2428ffa013Stedu #include <sys/ioctl.h>
2528ffa013Stedu #include <sys/malloc.h>
2628ffa013Stedu #include <sys/kernel.h>
2728ffa013Stedu #include <sys/disk.h>
2828ffa013Stedu #include <sys/rwlock.h>
2928ffa013Stedu #include <sys/queue.h>
3028ffa013Stedu #include <sys/fcntl.h>
3128ffa013Stedu #include <sys/mount.h>
3228ffa013Stedu #include <sys/sensors.h>
3328ffa013Stedu #include <sys/stat.h>
34e328a933Sjsing #include <sys/task.h>
3528ffa013Stedu #include <sys/conf.h>
3628ffa013Stedu #include <sys/uio.h>
3728ffa013Stedu 
3828ffa013Stedu #include <scsi/scsi_all.h>
3928ffa013Stedu #include <scsi/scsiconf.h>
4028ffa013Stedu #include <scsi/scsi_disk.h>
4128ffa013Stedu 
4228ffa013Stedu #include <dev/softraidvar.h>
4328ffa013Stedu 
44edc270aeSjsing /* RAID 1 functions. */
459847360bSjsing int	sr_raid1_create(struct sr_discipline *, struct bioc_createraid *,
469847360bSjsing 	    int, int64_t);
479847360bSjsing int	sr_raid1_assemble(struct sr_discipline *, struct bioc_createraid *,
487c003ea3Sjsing 	    int, void *);
49ceed1c06Sjsing int	sr_raid1_init(struct sr_discipline *sd);
50edc270aeSjsing int	sr_raid1_rw(struct sr_workunit *);
5138cf722eSjsing int	sr_raid1_wu_done(struct sr_workunit *);
5297a7a534Sjsing void	sr_raid1_set_chunk_state(struct sr_discipline *, int, int);
5397a7a534Sjsing void	sr_raid1_set_vol_state(struct sr_discipline *);
54edc270aeSjsing 
55edc270aeSjsing /* Discipline initialisation. */
56edc270aeSjsing void
sr_raid1_discipline_init(struct sr_discipline * sd)57edc270aeSjsing sr_raid1_discipline_init(struct sr_discipline *sd)
58edc270aeSjsing {
59edc270aeSjsing 	/* Fill out discipline members. */
60edc270aeSjsing 	sd->sd_type = SR_MD_RAID1;
616d81b338Sjsing 	strlcpy(sd->sd_name, "RAID 1", sizeof(sd->sd_name));
62ac774743Sjsing 	sd->sd_capabilities = SR_CAP_SYSTEM_DISK | SR_CAP_AUTO_ASSEMBLE |
634848162aSjsing 	    SR_CAP_REBUILD | SR_CAP_REDUNDANT;
64edc270aeSjsing 	sd->sd_max_wu = SR_RAID1_NOWU;
65edc270aeSjsing 
665c66ec22Sjsing 	/* Setup discipline specific function pointers. */
675c66ec22Sjsing 	sd->sd_assemble = sr_raid1_assemble;
685c66ec22Sjsing 	sd->sd_create = sr_raid1_create;
69edc270aeSjsing 	sd->sd_scsi_rw = sr_raid1_rw;
7038cf722eSjsing 	sd->sd_scsi_wu_done = sr_raid1_wu_done;
71edc270aeSjsing 	sd->sd_set_chunk_state = sr_raid1_set_chunk_state;
72edc270aeSjsing 	sd->sd_set_vol_state = sr_raid1_set_vol_state;
73edc270aeSjsing }
74edc270aeSjsing 
7528ffa013Stedu int
sr_raid1_create(struct sr_discipline * sd,struct bioc_createraid * bc,int no_chunk,int64_t coerced_size)769847360bSjsing sr_raid1_create(struct sr_discipline *sd, struct bioc_createraid *bc,
779847360bSjsing     int no_chunk, int64_t coerced_size)
789847360bSjsing {
795f082130Sjsing 	if (no_chunk < 2) {
80984c6b2dSjsing 		sr_error(sd->sd_sc, "%s requires two or more chunks",
81984c6b2dSjsing 		    sd->sd_name);
829847360bSjsing 		return EINVAL;
835f082130Sjsing 	}
849847360bSjsing 
859847360bSjsing 	sd->sd_meta->ssdi.ssd_size = coerced_size;
869847360bSjsing 
87ceed1c06Sjsing 	return sr_raid1_init(sd);
889847360bSjsing }
899847360bSjsing 
909847360bSjsing int
sr_raid1_assemble(struct sr_discipline * sd,struct bioc_createraid * bc,int no_chunk,void * data)919847360bSjsing sr_raid1_assemble(struct sr_discipline *sd, struct bioc_createraid *bc,
927c003ea3Sjsing     int no_chunk, void *data)
939847360bSjsing {
94ceed1c06Sjsing 	return sr_raid1_init(sd);
95ceed1c06Sjsing }
969847360bSjsing 
97ceed1c06Sjsing int
sr_raid1_init(struct sr_discipline * sd)98ceed1c06Sjsing sr_raid1_init(struct sr_discipline *sd)
99ceed1c06Sjsing {
1009847360bSjsing 	sd->sd_max_ccb_per_wu = sd->sd_meta->ssdi.ssd_chunk_no;
1019847360bSjsing 
1029847360bSjsing 	return 0;
1039847360bSjsing }
1049847360bSjsing 
105f09486a6Smarco void
sr_raid1_set_chunk_state(struct sr_discipline * sd,int c,int new_state)106f09486a6Smarco sr_raid1_set_chunk_state(struct sr_discipline *sd, int c, int new_state)
107f09486a6Smarco {
108f09486a6Smarco 	int			old_state, s;
109f09486a6Smarco 
1109c0431ceSstsp 	DNPRINTF(SR_D_STATE, "%s: %s: %s: sr_raid1_set_chunk_state %d -> %d\n",
111080cddd5Smarco 	    DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname,
112080cddd5Smarco 	    sd->sd_vol.sv_chunks[c]->src_meta.scmi.scm_devname, c, new_state);
113f09486a6Smarco 
114f09486a6Smarco 	/* ok to go to splbio since this only happens in error path */
115f09486a6Smarco 	s = splbio();
116f09486a6Smarco 	old_state = sd->sd_vol.sv_chunks[c]->src_meta.scm_status;
117f09486a6Smarco 
118f09486a6Smarco 	/* multiple IOs to the same chunk that fail will come through here */
119f09486a6Smarco 	if (old_state == new_state)
120f09486a6Smarco 		goto done;
121f09486a6Smarco 
122f09486a6Smarco 	switch (old_state) {
123f09486a6Smarco 	case BIOC_SDONLINE:
124f09486a6Smarco 		switch (new_state) {
125f09486a6Smarco 		case BIOC_SDOFFLINE:
126f09486a6Smarco 		case BIOC_SDSCRUB:
127f09486a6Smarco 			break;
128f09486a6Smarco 		default:
129f09486a6Smarco 			goto die;
130f09486a6Smarco 		}
131f09486a6Smarco 		break;
132f09486a6Smarco 
133f09486a6Smarco 	case BIOC_SDOFFLINE:
134914ebb4bSjsing 		switch (new_state) {
135914ebb4bSjsing 		case BIOC_SDREBUILD:
136914ebb4bSjsing 		case BIOC_SDHOTSPARE:
137914ebb4bSjsing 			break;
138914ebb4bSjsing 		default:
139f09486a6Smarco 			goto die;
140914ebb4bSjsing 		}
141f09486a6Smarco 		break;
142f09486a6Smarco 
143f09486a6Smarco 	case BIOC_SDSCRUB:
144f09486a6Smarco 		if (new_state == BIOC_SDONLINE) {
145f09486a6Smarco 			;
146f09486a6Smarco 		} else
147f09486a6Smarco 			goto die;
148f09486a6Smarco 		break;
149f09486a6Smarco 
150f09486a6Smarco 	case BIOC_SDREBUILD:
151914ebb4bSjsing 		switch (new_state) {
152914ebb4bSjsing 		case BIOC_SDONLINE:
1538989bbe9Sjsing 			break;
154914ebb4bSjsing 		case BIOC_SDOFFLINE:
1558989bbe9Sjsing 			/* Abort rebuild since the rebuild chunk disappeared. */
1568989bbe9Sjsing 			sd->sd_reb_abort = 1;
157914ebb4bSjsing 			break;
158914ebb4bSjsing 		default:
159f09486a6Smarco 			goto die;
160914ebb4bSjsing 		}
161f09486a6Smarco 		break;
162f09486a6Smarco 
163f09486a6Smarco 	case BIOC_SDHOTSPARE:
164914ebb4bSjsing 		switch (new_state) {
165914ebb4bSjsing 		case BIOC_SDOFFLINE:
166914ebb4bSjsing 		case BIOC_SDREBUILD:
167914ebb4bSjsing 			break;
168914ebb4bSjsing 		default:
169f09486a6Smarco 			goto die;
170914ebb4bSjsing 		}
171f09486a6Smarco 		break;
172f09486a6Smarco 
173f09486a6Smarco 	default:
174f09486a6Smarco die:
175f09486a6Smarco 		splx(s); /* XXX */
176*f6d8fcaeSderaadt 		panic("%s: %s: %s: invalid chunk state transition %d -> %d",
177*f6d8fcaeSderaadt 		    DEVNAME(sd->sd_sc),
178080cddd5Smarco 		    sd->sd_meta->ssd_devname,
179080cddd5Smarco 		    sd->sd_vol.sv_chunks[c]->src_meta.scmi.scm_devname,
180f09486a6Smarco 		    old_state, new_state);
181f09486a6Smarco 		/* NOTREACHED */
182f09486a6Smarco 	}
183f09486a6Smarco 
184f09486a6Smarco 	sd->sd_vol.sv_chunks[c]->src_meta.scm_status = new_state;
185f09486a6Smarco 	sd->sd_set_vol_state(sd);
186f09486a6Smarco 
187f09486a6Smarco 	sd->sd_must_flush = 1;
188e328a933Sjsing 	task_add(systq, &sd->sd_meta_save_task);
189f09486a6Smarco done:
190f09486a6Smarco 	splx(s);
191f09486a6Smarco }
192f09486a6Smarco 
193f09486a6Smarco void
sr_raid1_set_vol_state(struct sr_discipline * sd)194f09486a6Smarco sr_raid1_set_vol_state(struct sr_discipline *sd)
195f09486a6Smarco {
196f09486a6Smarco 	int			states[SR_MAX_STATES];
197f09486a6Smarco 	int			new_state, i, s, nd;
198080cddd5Smarco 	int			old_state = sd->sd_vol_status;
199f09486a6Smarco 
2009c0431ceSstsp 	DNPRINTF(SR_D_STATE, "%s: %s: sr_raid1_set_vol_state\n",
201080cddd5Smarco 	    DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname);
202f09486a6Smarco 
203080cddd5Smarco 	nd = sd->sd_meta->ssdi.ssd_chunk_no;
204f09486a6Smarco 
205930eb5e0Sjsing #ifdef SR_DEBUG
206930eb5e0Sjsing 	for (i = 0; i < nd; i++)
207930eb5e0Sjsing 		DNPRINTF(SR_D_STATE, "%s: chunk %d status = %u\n",
208930eb5e0Sjsing 		    DEVNAME(sd->sd_sc), i,
209930eb5e0Sjsing 		    sd->sd_vol.sv_chunks[i]->src_meta.scm_status);
210930eb5e0Sjsing #endif
211930eb5e0Sjsing 
212f09486a6Smarco 	for (i = 0; i < SR_MAX_STATES; i++)
213f09486a6Smarco 		states[i] = 0;
214f09486a6Smarco 
215f09486a6Smarco 	for (i = 0; i < nd; i++) {
216f09486a6Smarco 		s = sd->sd_vol.sv_chunks[i]->src_meta.scm_status;
217654bf55bSderaadt 		if (s >= SR_MAX_STATES)
218f09486a6Smarco 			panic("%s: %s: %s: invalid chunk state",
219f09486a6Smarco 			    DEVNAME(sd->sd_sc),
220080cddd5Smarco 			    sd->sd_meta->ssd_devname,
221080cddd5Smarco 			    sd->sd_vol.sv_chunks[i]->src_meta.scmi.scm_devname);
222f09486a6Smarco 		states[s]++;
223f09486a6Smarco 	}
224f09486a6Smarco 
225f09486a6Smarco 	if (states[BIOC_SDONLINE] == nd)
226f09486a6Smarco 		new_state = BIOC_SVONLINE;
227f09486a6Smarco 	else if (states[BIOC_SDONLINE] == 0)
228f09486a6Smarco 		new_state = BIOC_SVOFFLINE;
229f09486a6Smarco 	else if (states[BIOC_SDSCRUB] != 0)
230f09486a6Smarco 		new_state = BIOC_SVSCRUB;
231f09486a6Smarco 	else if (states[BIOC_SDREBUILD] != 0)
232f09486a6Smarco 		new_state = BIOC_SVREBUILD;
233f09486a6Smarco 	else if (states[BIOC_SDOFFLINE] != 0)
234f09486a6Smarco 		new_state = BIOC_SVDEGRADED;
235f09486a6Smarco 	else {
2366dfd565aSjsing 		DNPRINTF(SR_D_STATE, "%s: invalid volume state, old state "
2376dfd565aSjsing 		    "was %d\n", DEVNAME(sd->sd_sc), old_state);
2386dfd565aSjsing 		panic("invalid volume state");
239f09486a6Smarco 	}
240f09486a6Smarco 
2416dfd565aSjsing 	DNPRINTF(SR_D_STATE, "%s: %s: sr_raid1_set_vol_state %d -> %d\n",
242080cddd5Smarco 	    DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname,
243f09486a6Smarco 	    old_state, new_state);
244f09486a6Smarco 
245f09486a6Smarco 	switch (old_state) {
246f09486a6Smarco 	case BIOC_SVONLINE:
247f09486a6Smarco 		switch (new_state) {
248a1d8c2c6Smarco 		case BIOC_SVONLINE: /* can go to same state */
249f09486a6Smarco 		case BIOC_SVOFFLINE:
250f09486a6Smarco 		case BIOC_SVDEGRADED:
251fe9ba17fSmarco 		case BIOC_SVREBUILD: /* happens on boot */
252f09486a6Smarco 			break;
253f09486a6Smarco 		default:
254f09486a6Smarco 			goto die;
255f09486a6Smarco 		}
256f09486a6Smarco 		break;
257f09486a6Smarco 
258f09486a6Smarco 	case BIOC_SVOFFLINE:
259f09486a6Smarco 		/* XXX this might be a little too much */
260f09486a6Smarco 		goto die;
261f09486a6Smarco 
262f09486a6Smarco 	case BIOC_SVDEGRADED:
26397e41644Sjsing 		switch (new_state) {
26497e41644Sjsing 		case BIOC_SVOFFLINE:
26597e41644Sjsing 		case BIOC_SVREBUILD:
26697e41644Sjsing 		case BIOC_SVDEGRADED: /* can go to the same state */
267f09486a6Smarco 			break;
268f09486a6Smarco 		default:
269f09486a6Smarco 			goto die;
270f09486a6Smarco 		}
271f09486a6Smarco 		break;
272f09486a6Smarco 
273f09486a6Smarco 	case BIOC_SVBUILDING:
274f09486a6Smarco 		switch (new_state) {
275f09486a6Smarco 		case BIOC_SVONLINE:
276f09486a6Smarco 		case BIOC_SVOFFLINE:
277f09486a6Smarco 		case BIOC_SVBUILDING: /* can go to the same state */
278f09486a6Smarco 			break;
279f09486a6Smarco 		default:
280f09486a6Smarco 			goto die;
281f09486a6Smarco 		}
282f09486a6Smarco 		break;
283f09486a6Smarco 
28497e41644Sjsing 	case BIOC_SVSCRUB:
285f09486a6Smarco 		switch (new_state) {
286f09486a6Smarco 		case BIOC_SVONLINE:
287f09486a6Smarco 		case BIOC_SVOFFLINE:
288ae43ea2dSmarco 		case BIOC_SVDEGRADED:
28997e41644Sjsing 		case BIOC_SVSCRUB: /* can go to same state */
290f09486a6Smarco 			break;
291f09486a6Smarco 		default:
292f09486a6Smarco 			goto die;
293f09486a6Smarco 		}
294f09486a6Smarco 		break;
295f09486a6Smarco 
296f09486a6Smarco 	case BIOC_SVREBUILD:
29797e41644Sjsing 		switch (new_state) {
29897e41644Sjsing 		case BIOC_SVONLINE:
29997e41644Sjsing 		case BIOC_SVOFFLINE:
30097e41644Sjsing 		case BIOC_SVDEGRADED:
30197e41644Sjsing 		case BIOC_SVREBUILD: /* can go to the same state */
302f09486a6Smarco 			break;
303f09486a6Smarco 		default:
304f09486a6Smarco 			goto die;
305f09486a6Smarco 		}
306f09486a6Smarco 		break;
307f09486a6Smarco 
308f09486a6Smarco 	default:
309f09486a6Smarco die:
310*f6d8fcaeSderaadt 		panic("%s: %s: invalid volume state transition %d -> %d",
311*f6d8fcaeSderaadt 		    DEVNAME(sd->sd_sc),
312080cddd5Smarco 		    sd->sd_meta->ssd_devname,
313f09486a6Smarco 		    old_state, new_state);
314f09486a6Smarco 		/* NOTREACHED */
315f09486a6Smarco 	}
316f09486a6Smarco 
317080cddd5Smarco 	sd->sd_vol_status = new_state;
3186477f3e9Sjsing 
3196477f3e9Sjsing 	/* If we have just become degraded, look for a hotspare. */
3206477f3e9Sjsing 	if (new_state == BIOC_SVDEGRADED)
3213b545d8fSblambert 		task_add(systq, &sd->sd_hotspare_rebuild_task);
322f09486a6Smarco }
323f09486a6Smarco 
32428ffa013Stedu int
sr_raid1_rw(struct sr_workunit * wu)32528ffa013Stedu sr_raid1_rw(struct sr_workunit *wu)
32628ffa013Stedu {
32728ffa013Stedu 	struct sr_discipline	*sd = wu->swu_dis;
32828ffa013Stedu 	struct scsi_xfer	*xs = wu->swu_xs;
32928ffa013Stedu 	struct sr_ccb		*ccb;
33028ffa013Stedu 	struct sr_chunk		*scp;
33160781f8cSjsing 	int			ios, chunk, i, rt;
332d9ec6765Skrw 	daddr_t			blkno;
33328ffa013Stedu 
334d9ec6765Skrw 	/* blkno and scsi error will be handled by sr_validate_io */
335d9ec6765Skrw 	if (sr_validate_io(wu, &blkno, "sr_raid1_rw"))
33628ffa013Stedu 		goto bad;
33728ffa013Stedu 
33828ffa013Stedu 	if (xs->flags & SCSI_DATA_IN)
33928ffa013Stedu 		ios = 1;
34028ffa013Stedu 	else
341080cddd5Smarco 		ios = sd->sd_meta->ssdi.ssd_chunk_no;
34228ffa013Stedu 
34328ffa013Stedu 	for (i = 0; i < ios; i++) {
34428ffa013Stedu 		if (xs->flags & SCSI_DATA_IN) {
34528ffa013Stedu 			rt = 0;
34628ffa013Stedu ragain:
34728ffa013Stedu 			/* interleave reads */
348b0debbf9Sjsing 			chunk = sd->mds.mdd_raid1.sr1_counter++ %
349080cddd5Smarco 			    sd->sd_meta->ssdi.ssd_chunk_no;
350b0debbf9Sjsing 			scp = sd->sd_vol.sv_chunks[chunk];
35128ffa013Stedu 			switch (scp->src_meta.scm_status) {
35228ffa013Stedu 			case BIOC_SDONLINE:
35328ffa013Stedu 			case BIOC_SDSCRUB:
35428ffa013Stedu 				break;
35528ffa013Stedu 
35628ffa013Stedu 			case BIOC_SDOFFLINE:
35728ffa013Stedu 			case BIOC_SDREBUILD:
35828ffa013Stedu 			case BIOC_SDHOTSPARE:
359080cddd5Smarco 				if (rt++ < sd->sd_meta->ssdi.ssd_chunk_no)
36028ffa013Stedu 					goto ragain;
36128ffa013Stedu 
36228ffa013Stedu 				/* FALLTHROUGH */
36328ffa013Stedu 			default:
36428ffa013Stedu 				/* volume offline */
365b0debbf9Sjsing 				printf("%s: is offline, cannot read\n",
36628ffa013Stedu 				    DEVNAME(sd->sd_sc));
36728ffa013Stedu 				goto bad;
36828ffa013Stedu 			}
36928ffa013Stedu 		} else {
37028ffa013Stedu 			/* writes go on all working disks */
371b0debbf9Sjsing 			chunk = i;
372b0debbf9Sjsing 			scp = sd->sd_vol.sv_chunks[chunk];
37328ffa013Stedu 			switch (scp->src_meta.scm_status) {
37428ffa013Stedu 			case BIOC_SDONLINE:
37528ffa013Stedu 			case BIOC_SDSCRUB:
37628ffa013Stedu 			case BIOC_SDREBUILD:
37728ffa013Stedu 				break;
37828ffa013Stedu 
37928ffa013Stedu 			case BIOC_SDHOTSPARE: /* should never happen */
38028ffa013Stedu 			case BIOC_SDOFFLINE:
38128ffa013Stedu 				continue;
38228ffa013Stedu 
38328ffa013Stedu 			default:
38428ffa013Stedu 				goto bad;
38528ffa013Stedu 			}
38628ffa013Stedu 		}
38728ffa013Stedu 
388d9ec6765Skrw 		ccb = sr_ccb_rw(sd, chunk, blkno, xs->datalen, xs->data,
3891c83cc6fSjsing 		    xs->flags, 0);
390b0debbf9Sjsing 		if (!ccb) {
391b0debbf9Sjsing 			/* should never happen but handle more gracefully */
392b0debbf9Sjsing 			printf("%s: %s: too many ccbs queued\n",
393b0debbf9Sjsing 			    DEVNAME(sd->sd_sc),
394b0debbf9Sjsing 			    sd->sd_meta->ssd_devname);
395b0debbf9Sjsing 			goto bad;
396b0debbf9Sjsing 		}
3971c83cc6fSjsing 		sr_wu_enqueue_ccb(wu, ccb);
39828ffa013Stedu 	}
39928ffa013Stedu 
40060781f8cSjsing 	sr_schedule_wu(wu);
40128ffa013Stedu 
40228ffa013Stedu 	return (0);
40360781f8cSjsing 
40428ffa013Stedu bad:
405080cddd5Smarco 	/* wu is unwound by sr_wu_put */
40628ffa013Stedu 	return (1);
40728ffa013Stedu }
40828ffa013Stedu 
40938cf722eSjsing int
sr_raid1_wu_done(struct sr_workunit * wu)41038cf722eSjsing sr_raid1_wu_done(struct sr_workunit *wu)
41128ffa013Stedu {
41228ffa013Stedu 	struct sr_discipline	*sd = wu->swu_dis;
41328ffa013Stedu 	struct scsi_xfer	*xs = wu->swu_xs;
41428ffa013Stedu 
41538cf722eSjsing 	/* If at least one I/O succeeded, we are okay. */
41638cf722eSjsing 	if (wu->swu_ios_succeeded > 0) {
4172b14bff0Sjsing 		xs->error = XS_NOERROR;
41838cf722eSjsing 		return SR_WU_OK;
41938cf722eSjsing 	}
4202b14bff0Sjsing 
42138cf722eSjsing 	/* If all I/O failed, retry reads and give up on writes. */
42228ffa013Stedu 	if (xs->flags & SCSI_DATA_IN) {
42328ffa013Stedu 		printf("%s: retrying read on block %lld\n",
424bb4f4faeSkrw 		    sd->sd_meta->ssd_devname, (long long)wu->swu_blk_start);
4257af3f0fdSmarco 		if (wu->swu_cb_active == 1)
4267af3f0fdSmarco 			panic("%s: sr_raid1_intr_cb",
4277af3f0fdSmarco 			    DEVNAME(sd->sd_sc));
428f4e69389Sjsing 		sr_wu_release_ccbs(wu);
42928ffa013Stedu 		wu->swu_state = SR_WU_RESTART;
4302b14bff0Sjsing 		if (sd->sd_scsi_rw(wu) == 0)
43138cf722eSjsing 			return SR_WU_RESTART;
4322b14bff0Sjsing 	} else {
4332b14bff0Sjsing 		printf("%s: permanently failing write on block %lld\n",
434bb4f4faeSkrw 		    sd->sd_meta->ssd_devname, (long long)wu->swu_blk_start);
43538cf722eSjsing 	}
43638cf722eSjsing 
43738cf722eSjsing 	wu->swu_state = SR_WU_FAILED;
4382b14bff0Sjsing 	xs->error = XS_DRIVER_STUFFUP;
43928ffa013Stedu 
44038cf722eSjsing 	return SR_WU_FAILED;
44128ffa013Stedu }
442