1 /* vinuminterrupt.c: bottom half of the driver */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999
5 * Nan Yang Computer Services Limited. All rights reserved.
6 *
7 * Parts copyright (c) 1997, 1998 Cybernet Corporation, NetMAX project.
8 *
9 * Written by Greg Lehey
10 *
11 * This software is distributed under the so-called ``Berkeley
12 * License'':
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by Nan Yang Computer
25 * Services Limited.
26 * 4. Neither the name of the Company nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * This software is provided ``as is'', and any express or implied
31 * warranties, including, but not limited to, the implied warranties of
32 * merchantability and fitness for a particular purpose are disclaimed.
33 * In no event shall the company or contributors be liable for any
34 * direct, indirect, incidental, special, exemplary, or consequential
35 * damages (including, but not limited to, procurement of substitute
36 * goods or services; loss of use, data, or profits; or business
37 * interruption) however caused and on any theory of liability, whether
38 * in contract, strict liability, or tort (including negligence or
39 * otherwise) arising in any way out of the use of this software, even if
40 * advised of the possibility of such damage.
41 *
42 * $Id: vinuminterrupt.c,v 1.2 2003/11/25 20:11:59 jdolecek Exp $
43 * $FreeBSD$
44 */
45
46 #include <dev/vinum/vinumhdr.h>
47 #include <dev/vinum/request.h>
48 #include <sys/resourcevar.h>
49
50 void complete_raid5_write(struct rqelement *);
51 void complete_rqe(struct buf *bp);
52 void sdio_done(struct buf *bp);
53
54 /*
55 * Take a completed buffer, transfer the data back if
56 * it's a read, and complete the high-level request
57 * if this is the last subrequest.
58 *
59 * The bp parameter is in fact a struct rqelement, which
60 * includes a couple of extras at the end.
61 */
62 void
63 complete_rqe(struct buf *bp)
64 {
65 struct rqelement *rqe;
66 struct request *rq;
67 struct rqgroup *rqg;
68 struct buf *ubp; /* user buffer */
69 struct drive *drive;
70 struct sd *sd;
71 char *gravity; /* for error messages */
72
73 rqe = (struct rqelement *) bp; /* point to the element element that completed */
74 rqg = rqe->rqg; /* and the request group */
75 rq = rqg->rq; /* and the complete request */
76 ubp = rq->bp; /* user buffer */
77
78 #ifdef VINUMDEBUG
79 if (debug & DEBUG_LASTREQS)
80 logrq(loginfo_iodone, (union rqinfou) rqe, ubp);
81 #endif
82 drive = &DRIVE[rqe->driveno];
83 drive->active--; /* one less outstanding I/O on this drive */
84 vinum_conf.active--; /* one less outstanding I/O globally */
85 if ((drive->active == (DRIVE_MAXACTIVE - 1)) /* we were at the drive limit */
86 ||(vinum_conf.active == VINUM_MAXACTIVE)) /* or the global limit */
87 wakeup(&launch_requests); /* let another one at it */
88 if ((bp->b_flags & B_ERROR) != 0) { /* transfer in error */
89 gravity = "";
90 sd = &SD[rqe->sdno];
91
92 if (bp->b_error != 0) /* did it return a number? */
93 rq->error = bp->b_error; /* yes, put it in. */
94 else if (rq->error == 0) /* no: do we have one already? */
95 rq->error = EIO; /* no: catchall "I/O error" */
96 sd->lasterror = rq->error;
97 if (bp->b_flags & B_READ) {
98 if ((rq->error == ENXIO) || (sd->flags & VF_RETRYERRORS) == 0) {
99 gravity = " fatal";
100 set_sd_state(rqe->sdno, sd_crashed, setstate_force); /* subdisk is crashed */
101 }
102 log(LOG_ERR,
103 "%s:%s read error, block %lld for %ld bytes\n",
104 gravity,
105 sd->name,
106 (long long int) bp->b_blkno,
107 bp->b_bcount);
108 } else { /* write operation */
109 if ((rq->error == ENXIO) || (sd->flags & VF_RETRYERRORS) == 0) {
110 gravity = "fatal ";
111 set_sd_state(rqe->sdno, sd_stale, setstate_force); /* subdisk is stale */
112 }
113 log(LOG_ERR,
114 "%s:%s write error, block %lld for %ld bytes\n",
115 gravity,
116 sd->name,
117 (long long int)bp->b_blkno,
118 bp->b_bcount);
119 }
120 log(LOG_ERR,
121 "%s: user buffer block %lld for %ld bytes\n",
122 sd->name,
123 (long long int)ubp->b_blkno,
124 ubp->b_bcount);
125 if (rq->error == ENXIO) { /* the drive's down too */
126 log(LOG_ERR,
127 "%s: fatal drive I/O error, block %lld for %ld bytes\n",
128 DRIVE[rqe->driveno].label.name,
129 (long long int)bp->b_blkno,
130 bp->b_bcount);
131 DRIVE[rqe->driveno].lasterror = rq->error;
132 set_drive_state(rqe->driveno, /* take the drive down */
133 drive_down,
134 setstate_force);
135 }
136 }
137 /* Now update the statistics */
138 if (bp->b_flags & B_READ) { /* read operation */
139 DRIVE[rqe->driveno].reads++;
140 DRIVE[rqe->driveno].bytes_read += bp->b_bcount;
141 SD[rqe->sdno].reads++;
142 SD[rqe->sdno].bytes_read += bp->b_bcount;
143 PLEX[rqe->rqg->plexno].reads++;
144 PLEX[rqe->rqg->plexno].bytes_read += bp->b_bcount;
145 if (PLEX[rqe->rqg->plexno].volno >= 0) { /* volume I/O, not plex */
146 VOL[PLEX[rqe->rqg->plexno].volno].reads++;
147 VOL[PLEX[rqe->rqg->plexno].volno].bytes_read += bp->b_bcount;
148 }
149 } else { /* write operation */
150 DRIVE[rqe->driveno].writes++;
151 DRIVE[rqe->driveno].bytes_written += bp->b_bcount;
152 SD[rqe->sdno].writes++;
153 SD[rqe->sdno].bytes_written += bp->b_bcount;
154 PLEX[rqe->rqg->plexno].writes++;
155 PLEX[rqe->rqg->plexno].bytes_written += bp->b_bcount;
156 if (PLEX[rqe->rqg->plexno].volno >= 0) { /* volume I/O, not plex */
157 VOL[PLEX[rqe->rqg->plexno].volno].writes++;
158 VOL[PLEX[rqe->rqg->plexno].volno].bytes_written += bp->b_bcount;
159 }
160 }
161 if (rqg->flags & XFR_RECOVERY_READ) { /* recovery read, */
162 int *sdata; /* source */
163 int *data; /* and group data */
164 int length; /* and count involved */
165 int count; /* loop counter */
166 struct rqelement *urqe = &rqg->rqe[rqg->badsdno]; /* rqe of the bad subdisk */
167
168 /* XOR destination is the user data */
169 sdata = (int *) &rqe->b.b_data[rqe->groupoffset << DEV_BSHIFT]; /* old data contents */
170 data = (int *) &urqe->b.b_data[urqe->groupoffset << DEV_BSHIFT]; /* destination */
171 length = urqe->grouplen * (DEV_BSIZE / sizeof(int)); /* and number of ints */
172
173 for (count = 0; count < length; count++)
174 data[count] ^= sdata[count];
175
176 /*
177 * In a normal read, we will normally read directly
178 * into the user buffer. This doesn't work if
179 * we're also doing a recovery, so we have to
180 * copy it
181 */
182 if (rqe->flags & XFR_NORMAL_READ) { /* normal read as well, */
183 char *src = &rqe->b.b_data[rqe->dataoffset << DEV_BSHIFT]; /* read data is here */
184 char *dst;
185
186 dst = (char *) ubp->b_data + (rqe->useroffset << DEV_BSHIFT); /* where to put it in user buffer */
187 length = rqe->datalen << DEV_BSHIFT; /* and count involved */
188 bcopy(src, dst, length); /* move it */
189 }
190 } else if ((rqg->flags & (XFR_NORMAL_WRITE | XFR_DEGRADED_WRITE)) /* RAID 4/5 group write operation */
191 &&(rqg->active == 1)) /* and this is the last active request */
192 complete_raid5_write(rqe);
193 /*
194 * This is the earliest place where we can be
195 * sure that the request has really finished,
196 * since complete_raid5_write can issue new
197 * requests.
198 */
199 rqg->active--; /* this request now finished */
200 if (rqg->active == 0) { /* request group finished, */
201 rq->active--; /* one less */
202 if (rqg->lock) { /* got a lock? */
203 unlockrange(rqg->plexno, rqg->lock); /* yes, free it */
204 rqg->lock = 0;
205 }
206 }
207 if (rq->active == 0) { /* request finished, */
208 #ifdef VINUMDEBUG
209 if (debug & DEBUG_RESID) {
210 if (ubp->b_resid != 0) /* still something to transfer? */
211 panic("resid");
212 }
213 #endif
214
215 if (rq->error) { /* did we have an error? */
216 if (rq->isplex) { /* plex operation, */
217 ubp->b_flags |= B_ERROR; /* yes, propagate to user */
218 ubp->b_error = rq->error;
219 } else /* try to recover */
220 queue_daemon_request(daemonrq_ioerror, (union daemoninfo) rq); /* let the daemon complete */
221 } else {
222 ubp->b_resid = 0; /* completed our transfer */
223 if (rq->isplex == 0) /* volume request, */
224 VOL[rq->volplex.volno].active--; /* another request finished */
225 if (rq->flags & XFR_COPYBUF) {
226 Free(ubp->b_data);
227 ubp->b_data = rq->save_data;
228 }
229 biodone(ubp); /* top level buffer completed */
230 freerq(rq); /* return the request storage */
231 }
232 }
233 }
234
235 /* Free a request block and anything hanging off it */
236 void
237 freerq(struct request *rq)
238 {
239 struct rqgroup *rqg;
240 struct rqgroup *nrqg; /* next in chain */
241 int rqno;
242
243 for (rqg = rq->rqg; rqg != NULL; rqg = nrqg) { /* through the whole request chain */
244 if (rqg->lock) /* got a lock? */
245 unlockrange(rqg->plexno, rqg->lock); /* yes, free it */
246 for (rqno = 0; rqno < rqg->count; rqno++) {
247 if ((rqg->rqe[rqno].flags & XFR_MALLOCED) /* data buffer was malloced, */
248 &&rqg->rqe[rqno].b.b_data) /* and the allocation succeeded */
249 Free(rqg->rqe[rqno].b.b_data); /* free it */
250 }
251 nrqg = rqg->next; /* note the next one */
252 Free(rqg); /* and free this one */
253 }
254 Free(rq); /* free the request itself */
255 }
256
257 /* I/O on subdisk completed */
258 void
259 sdio_done(struct buf *bp)
260 {
261 struct sdbuf *sbp;
262
263 sbp = (struct sdbuf *) bp;
264 if (sbp->b.b_flags & B_ERROR) { /* had an error */
265 sbp->bp->b_flags |= B_ERROR; /* propagate upwards */
266 sbp->bp->b_error = sbp->b.b_error;
267 }
268 #ifdef VINUMDEBUG
269 if (debug & DEBUG_LASTREQS)
270 logrq(loginfo_sdiodone, (union rqinfou) bp, bp);
271 #endif
272 sbp->bp->b_resid = sbp->b.b_resid; /* copy the resid field */
273 /* Now update the statistics */
274 if (bp->b_flags & B_READ) { /* read operation */
275 DRIVE[sbp->driveno].reads++;
276 DRIVE[sbp->driveno].bytes_read += sbp->b.b_bcount;
277 SD[sbp->sdno].reads++;
278 SD[sbp->sdno].bytes_read += sbp->b.b_bcount;
279 } else { /* write operation */
280 DRIVE[sbp->driveno].writes++;
281 DRIVE[sbp->driveno].bytes_written += sbp->b.b_bcount;
282 SD[sbp->sdno].writes++;
283 SD[sbp->sdno].bytes_written += sbp->b.b_bcount;
284 }
285 biodone(sbp->bp); /* complete the caller's I/O */
286 Free(sbp);
287 }
288
289 /* Start the second phase of a RAID-4 or RAID-5 group write operation. */
290 void
291 complete_raid5_write(struct rqelement *rqe)
292 {
293 int *sdata; /* source */
294 int *pdata; /* and parity block data */
295 int length; /* and count involved */
296 int count; /* loop counter */
297 int rqno; /* request index */
298 int rqoffset; /* offset of request data from parity data */
299 struct buf *ubp; /* user buffer header */
300 struct request *rq; /* pointer to our request */
301 struct rqgroup *rqg; /* and to the request group */
302 struct rqelement *prqe; /* point to the parity block */
303 struct drive *drive; /* drive to access */
304 const struct bdevsw *bdev; /* pointer to bdevsw entry */
305
306 rqg = rqe->rqg; /* and to our request group */
307 rq = rqg->rq; /* point to our request */
308 ubp = rq->bp; /* user's buffer header */
309 prqe = &rqg->rqe[0]; /* point to the parity block */
310
311 /*
312 * If we get to this function, we have normal or
313 * degraded writes, or a combination of both. We do
314 * the same thing in each case: we perform an
315 * exclusive or to the parity block. The only
316 * difference is the origin of the data and the
317 * address range.
318 */
319 if (rqe->flags & XFR_DEGRADED_WRITE) { /* do the degraded write stuff */
320 pdata = (int *) (&prqe->b.b_data[(prqe->groupoffset) << DEV_BSHIFT]); /* parity data pointer */
321 bzero(pdata, prqe->grouplen << DEV_BSHIFT); /* start with nothing in the parity block */
322
323 /* Now get what data we need from each block */
324 for (rqno = 1; rqno < rqg->count; rqno++) { /* for all the data blocks */
325 rqe = &rqg->rqe[rqno]; /* this request */
326 sdata = (int *) (&rqe->b.b_data[rqe->groupoffset << DEV_BSHIFT]); /* old data */
327 length = rqe->grouplen << (DEV_BSHIFT - 2); /* and count involved */
328
329 /*
330 * Add the data block to the parity block. Before
331 * we started the request, we zeroed the parity
332 * block, so the result of adding all the other
333 * blocks and the block we want to write will be
334 * the correct parity block.
335 */
336 for (count = 0; count < length; count++)
337 pdata[count] ^= sdata[count];
338 if ((rqe->flags & XFR_MALLOCED) /* the buffer was malloced, */
339 &&((rqg->flags & XFR_NORMAL_WRITE) == 0)) { /* and we have no normal write, */
340 Free(rqe->b.b_data); /* free it now */
341 rqe->flags &= ~XFR_MALLOCED;
342 }
343 }
344 }
345 if (rqg->flags & XFR_NORMAL_WRITE) { /* do normal write stuff */
346 /* Get what data we need from each block */
347 for (rqno = 1; rqno < rqg->count; rqno++) { /* for all the data blocks */
348 rqe = &rqg->rqe[rqno]; /* this request */
349 if ((rqe->flags & (XFR_DATA_BLOCK | XFR_BAD_SUBDISK | XFR_NORMAL_WRITE))
350 == (XFR_DATA_BLOCK | XFR_NORMAL_WRITE)) { /* good data block to write */
351 sdata = (int *) &rqe->b.b_data[rqe->dataoffset << DEV_BSHIFT]; /* old data contents */
352 rqoffset = rqe->dataoffset + rqe->sdoffset - prqe->sdoffset; /* corresponding parity block offset */
353 pdata = (int *) (&prqe->b.b_data[rqoffset << DEV_BSHIFT]); /* parity data pointer */
354 length = rqe->datalen * (DEV_BSIZE / sizeof(int)); /* and number of ints */
355
356 /*
357 * "remove" the old data block
358 * from the parity block
359 */
360 if ((pdata < ((int *) prqe->b.b_data))
361 || (&pdata[length] > ((int *) (prqe->b.b_data + prqe->b.b_bcount)))
362 || (sdata < ((int *) rqe->b.b_data))
363 || (&sdata[length] > ((int *) (rqe->b.b_data + rqe->b.b_bcount))))
364 panic("complete_raid5_write: bounds overflow");
365 for (count = 0; count < length; count++)
366 pdata[count] ^= sdata[count];
367
368 /* "add" the new data block */
369 sdata = (int *) (&ubp->b_data[rqe->useroffset << DEV_BSHIFT]); /* new data */
370 if ((sdata < ((int *) ubp->b_data))
371 || (&sdata[length] > ((int *) (ubp->b_data + ubp->b_bcount))))
372 panic("complete_raid5_write: bounds overflow");
373 for (count = 0; count < length; count++)
374 pdata[count] ^= sdata[count];
375
376 /* Free the malloced buffer */
377 if (rqe->flags & XFR_MALLOCED) { /* the buffer was malloced, */
378 Free(rqe->b.b_data); /* free it */
379 rqe->flags &= ~XFR_MALLOCED;
380 } else
381 panic("complete_raid5_write: malloc conflict");
382
383 if ((rqe->b.b_flags & B_READ) /* this was a read */
384 &&((rqe->flags & XFR_BAD_SUBDISK) == 0)) { /* and we can write this block */
385 rqe->b.b_flags &= ~(B_READ | B_DONE); /* we're writing now */
386 rqe->b.b_flags |= B_CALL; /* call us when you're done */
387 rqe->b.b_iodone = complete_rqe; /* by calling us here */
388 rqe->flags &= ~XFR_PARITYOP; /* reset flags that brought us here */
389 rqe->b.b_data = &ubp->b_data[rqe->useroffset << DEV_BSHIFT]; /* point to the user data */
390 rqe->b.b_bcount = rqe->datalen << DEV_BSHIFT; /* length to write */
391 rqe->b.b_bufsize = rqe->b.b_bcount; /* don't claim more */
392 rqe->b.b_resid = rqe->b.b_bcount; /* nothing transferred */
393 rqe->b.b_blkno += rqe->dataoffset; /* point to the correct block */
394 rqg->active++; /* another active request */
395 drive = &DRIVE[rqe->driveno]; /* drive to access */
396
397 /* We can't sleep here, so we just increment the counters. */
398 drive->active++;
399 if (drive->active >= drive->maxactive)
400 drive->maxactive = drive->active;
401 vinum_conf.active++;
402 if (vinum_conf.active >= vinum_conf.maxactive)
403 vinum_conf.maxactive = vinum_conf.active;
404 #ifdef VINUMDEBUG
405 if (debug & DEBUG_ADDRESSES)
406 log(LOG_DEBUG,
407 " %s dev %d.%d, sd %d, offset 0x%x, devoffset 0x%llx, length %ld\n",
408 rqe->b.b_flags & B_READ ? "Read" : "Write",
409 major(rqe->b.b_dev),
410 minor(rqe->b.b_dev),
411 rqe->sdno,
412 (u_int) (rqe->b.b_blkno - SD[rqe->sdno].driveoffset),
413 rqe->b.b_blkno,
414 rqe->b.b_bcount);
415 if (debug & DEBUG_LASTREQS)
416 logrq(loginfo_raid5_data, (union rqinfou) rqe, ubp);
417 #endif
418 bdev = bdevsw_lookup(rqe->b.b_dev);
419 if (bdev == NULL)
420 panic("lost our devsw"); /* XXX FIXME: take the drive down */
421 (*bdev->d_strategy) (&rqe->b);
422 }
423 }
424 }
425 }
426 /* Finally, write the parity block */
427 rqe = &rqg->rqe[0];
428 rqe->b.b_flags &= ~(B_READ | B_DONE); /* we're writing now */
429 rqe->b.b_flags |= B_CALL; /* tell us when you're done */
430 rqe->b.b_iodone = complete_rqe; /* by calling us here */
431 rqg->flags &= ~XFR_PARITYOP; /* reset flags that brought us here */
432 rqe->b.b_bcount = rqe->buflen << DEV_BSHIFT; /* length to write */
433 rqe->b.b_bufsize = rqe->b.b_bcount; /* don't claim we have more */
434 rqe->b.b_resid = rqe->b.b_bcount; /* nothing transferred */
435 rqg->active++; /* another active request */
436 drive = &DRIVE[rqe->driveno]; /* drive to access */
437
438 /* We can't sleep here, so we just increment the counters. */
439 drive->active++;
440 if (drive->active >= drive->maxactive)
441 drive->maxactive = drive->active;
442 vinum_conf.active++;
443 if (vinum_conf.active >= vinum_conf.maxactive)
444 vinum_conf.maxactive = vinum_conf.active;
445
446 #ifdef VINUMDEBUG
447 if (debug & DEBUG_ADDRESSES)
448 log(LOG_DEBUG,
449 " %s dev %d.%d, sd %d, offset 0x%x, devoffset 0x%llx, length %ld\n",
450 rqe->b.b_flags & B_READ ? "Read" : "Write",
451 major(rqe->b.b_dev),
452 minor(rqe->b.b_dev),
453 rqe->sdno,
454 (u_int) (rqe->b.b_blkno - SD[rqe->sdno].driveoffset),
455 rqe->b.b_blkno,
456 rqe->b.b_bcount);
457 if (debug & DEBUG_LASTREQS)
458 logrq(loginfo_raid5_parity, (union rqinfou) rqe, ubp);
459 #endif
460 bdev = bdevsw_lookup(rqe->b.b_dev);
461 if (bdev == NULL)
462 panic("no bdevsw"); /* XXX FIXME: take drive down or something */
463 (*bdev->d_strategy) (&rqe->b);
464 }
465
466 /* Local Variables: */
467 /* fill-column: 50 */
468 /* End: */
Cache object: 450d8e5ff7b9d93cb8afff65e347cb7a
|