Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / raid / vinum / vinuminterrupt.c
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.12 2000/11/24 03:41:42 grog Exp grog $
43  * $FreeBSD: src/sys/dev/vinum/vinuminterrupt.c,v 1.25.2.3 2001/05/28 05:56:27 grog Exp $
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 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 %d for %ld bytes\n",
104                 gravity,
105                 sd->name,
106                 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 %d for %ld bytes\n",
115                 gravity,
116                 sd->name,
117                 bp->b_blkno,
118                 bp->b_bcount);
119         }
120         log(LOG_ERR,
121             "%s: user buffer block %d for %ld bytes\n",
122             sd->name,
123             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 %d for %ld bytes\n",
128                 DRIVE[rqe->driveno].label.name,
129                 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                 Debugger("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             biodone(ubp);                                   /* top level buffer completed */
226             freerq(rq);                                     /* return the request storage */
227         }
228     }
229 }
230
231 /* Free a request block and anything hanging off it */
232 void
233 freerq(struct request *rq)
234 {
235     struct rqgroup *rqg;
236     struct rqgroup *nrqg;                                   /* next in chain */
237     int rqno;
238
239     for (rqg = rq->rqg; rqg != NULL; rqg = nrqg) {          /* through the whole request chain */
240         if (rqg->lock)                                      /* got a lock? */
241             unlockrange(rqg->plexno, rqg->lock);            /* yes, free it */
242         for (rqno = 0; rqno < rqg->count; rqno++) {
243             if ((rqg->rqe[rqno].flags & XFR_MALLOCED)       /* data buffer was malloced, */
244             &&rqg->rqe[rqno].b.b_data)                      /* and the allocation succeeded */
245                 Free(rqg->rqe[rqno].b.b_data);              /* free it */
246             if (rqg->rqe[rqno].flags & XFR_BUFLOCKED) {     /* locked this buffer, */
247                 BUF_UNLOCK(&rqg->rqe[rqno].b);              /* unlock it again */
248                 BUF_LOCKFREE(&rqg->rqe[rqno].b);
249             }
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     BUF_UNLOCK(&sbp->b);
287     BUF_LOCKFREE(&sbp->b);
288     Free(sbp);
289 }
290
291 /* Start the second phase of a RAID-4 or RAID-5 group write operation. */
292 void
293 complete_raid5_write(struct rqelement *rqe)
294 {
295     int *sdata;                                             /* source */
296     int *pdata;                                             /* and parity block data */
297     int length;                                             /* and count involved */
298     int count;                                              /* loop counter */
299     int rqno;                                               /* request index */
300     int rqoffset;                                           /* offset of request data from parity data */
301     struct buf *ubp;                                        /* user buffer header */
302     struct request *rq;                                     /* pointer to our request */
303     struct rqgroup *rqg;                                    /* and to the request group */
304     struct rqelement *prqe;                                 /* point to the parity block */
305     struct drive *drive;                                    /* drive to access */
306
307     rqg = rqe->rqg;                                         /* and to our request group */
308     rq = rqg->rq;                                           /* point to our request */
309     ubp = rq->bp;                                           /* user's buffer header */
310     prqe = &rqg->rqe[0];                                    /* point to the parity block */
311
312     /*
313      * If we get to this function, we have normal or
314      * degraded writes, or a combination of both.  We do
315      * the same thing in each case: we perform an
316      * exclusive or to the parity block.  The only
317      * difference is the origin of the data and the
318      * address range.
319      */
320     if (rqe->flags & XFR_DEGRADED_WRITE) {                  /* do the degraded write stuff */
321         pdata = (int *) (&prqe->b.b_data[(prqe->groupoffset) << DEV_BSHIFT]); /* parity data pointer */
322         bzero(pdata, prqe->grouplen << DEV_BSHIFT);         /* start with nothing in the parity block */
323
324         /* Now get what data we need from each block */
325         for (rqno = 1; rqno < rqg->count; rqno++) {         /* for all the data blocks */
326             rqe = &rqg->rqe[rqno];                          /* this request */
327             sdata = (int *) (&rqe->b.b_data[rqe->groupoffset << DEV_BSHIFT]); /* old data */
328             length = rqe->grouplen << (DEV_BSHIFT - 2);     /* and count involved */
329
330             /*
331              * Add the data block to the parity block.  Before
332              * we started the request, we zeroed the parity
333              * block, so the result of adding all the other
334              * blocks and the block we want to write will be
335              * the correct parity block.
336              */
337             for (count = 0; count < length; count++)
338                 pdata[count] ^= sdata[count];
339             if ((rqe->flags & XFR_MALLOCED)                 /* the buffer was malloced, */
340             &&((rqg->flags & XFR_NORMAL_WRITE) == 0)) {     /* and we have no normal write, */
341                 Free(rqe->b.b_data);                        /* free it now */
342                 rqe->flags &= ~XFR_MALLOCED;
343             }
344         }
345     }
346     if (rqg->flags & XFR_NORMAL_WRITE) {                    /* do normal write stuff */
347         /* Get what data we need from each block */
348         for (rqno = 1; rqno < rqg->count; rqno++) {         /* for all the data blocks */
349             rqe = &rqg->rqe[rqno];                          /* this request */
350             if ((rqe->flags & (XFR_DATA_BLOCK | XFR_BAD_SUBDISK | XFR_NORMAL_WRITE))
351                 == (XFR_DATA_BLOCK | XFR_NORMAL_WRITE)) {   /* good data block to write */
352                 sdata = (int *) &rqe->b.b_data[rqe->dataoffset << DEV_BSHIFT]; /* old data contents */
353                 rqoffset = rqe->dataoffset + rqe->sdoffset - prqe->sdoffset; /* corresponding parity block offset */
354                 pdata = (int *) (&prqe->b.b_data[rqoffset << DEV_BSHIFT]); /* parity data pointer */
355                 length = rqe->datalen * (DEV_BSIZE / sizeof(int)); /* and number of ints */
356
357                 /*
358                  * "remove" the old data block
359                  * from the parity block
360                  */
361                 if ((pdata < ((int *) prqe->b.b_data))
362                     || (&pdata[length] > ((int *) (prqe->b.b_data + prqe->b.b_bcount)))
363                     || (sdata < ((int *) rqe->b.b_data))
364                     || (&sdata[length] > ((int *) (rqe->b.b_data + rqe->b.b_bcount))))
365                     panic("complete_raid5_write: bounds overflow");
366                 for (count = 0; count < length; count++)
367                     pdata[count] ^= sdata[count];
368
369                 /* "add" the new data block */
370                 sdata = (int *) (&ubp->b_data[rqe->useroffset << DEV_BSHIFT]); /* new data */
371                 if ((sdata < ((int *) ubp->b_data))
372                     || (&sdata[length] > ((int *) (ubp->b_data + ubp->b_bcount))))
373                     panic("complete_raid5_write: bounds overflow");
374                 for (count = 0; count < length; count++)
375                     pdata[count] ^= sdata[count];
376
377                 /* Free the malloced buffer */
378                 if (rqe->flags & XFR_MALLOCED) {            /* the buffer was malloced, */
379                     Free(rqe->b.b_data);                    /* free it */
380                     rqe->flags &= ~XFR_MALLOCED;
381                 } else
382                     panic("complete_raid5_write: malloc conflict");
383
384                 if ((rqe->b.b_flags & B_READ)               /* this was a read */
385                 &&((rqe->flags & XFR_BAD_SUBDISK) == 0)) {  /* and we can write this block */
386                     rqe->b.b_flags &= ~(B_READ | B_DONE);   /* we're writing now */
387                     rqe->b.b_flags |= B_CALL;               /* call us when you're done */
388                     rqe->b.b_iodone = complete_rqe;         /* by calling us here */
389                     rqe->flags &= ~XFR_PARITYOP;            /* reset flags that brought us here */
390                     rqe->b.b_data = &ubp->b_data[rqe->useroffset << DEV_BSHIFT]; /* point to the user data */
391                     rqe->b.b_bcount = rqe->datalen << DEV_BSHIFT; /* length to write */
392                     rqe->b.b_bufsize = rqe->b.b_bcount;     /* don't claim more */
393                     rqe->b.b_resid = rqe->b.b_bcount;       /* nothing transferred */
394                     rqe->b.b_blkno += rqe->dataoffset;      /* point to the correct block */
395                     rqg->active++;                          /* another active request */
396                     drive = &DRIVE[rqe->driveno];           /* drive to access */
397
398                                                             /* We can't sleep here, so we just increment the counters. */
399                     drive->active++;
400                     if (drive->active >= drive->maxactive)
401                         drive->maxactive = drive->active;
402                     vinum_conf.active++;
403                     if (vinum_conf.active >= vinum_conf.maxactive)
404                         vinum_conf.maxactive = vinum_conf.active;
405 #if VINUMDEBUG
406                     if (debug & DEBUG_ADDRESSES)
407                         log(LOG_DEBUG,
408                             "  %s dev %d.%d, sd %d, offset 0x%x, devoffset 0x%x, length %ld\n",
409                             rqe->b.b_flags & B_READ ? "Read" : "Write",
410                             major(rqe->b.b_dev),
411                             minor(rqe->b.b_dev),
412                             rqe->sdno,
413                             (u_int) (rqe->b.b_blkno - SD[rqe->sdno].driveoffset),
414                             rqe->b.b_blkno,
415                             rqe->b.b_bcount);
416                     if (debug & DEBUG_LASTREQS)
417                         logrq(loginfo_raid5_data, (union rqinfou) rqe, ubp);
418 #endif
419                     BUF_STRATEGY(&rqe->b, 0);
420                 }
421             }
422         }
423     }
424     /* Finally, write the parity block */
425     rqe = &rqg->rqe[0];
426     rqe->b.b_flags &= ~(B_READ | B_DONE);                   /* we're writing now */
427     rqe->b.b_flags |= B_CALL;                               /* tell us when you're done */
428     rqe->b.b_iodone = complete_rqe;                         /* by calling us here */
429     rqg->flags &= ~XFR_PARITYOP;                            /* reset flags that brought us here */
430     rqe->b.b_bcount = rqe->buflen << DEV_BSHIFT;            /* length to write */
431     rqe->b.b_bufsize = rqe->b.b_bcount;                     /* don't claim we have more */
432     rqe->b.b_resid = rqe->b.b_bcount;                       /* nothing transferred */
433     rqg->active++;                                          /* another active request */
434     drive = &DRIVE[rqe->driveno];                           /* drive to access */
435
436     /* We can't sleep here, so we just increment the counters. */
437     drive->active++;
438     if (drive->active >= drive->maxactive)
439         drive->maxactive = drive->active;
440     vinum_conf.active++;
441     if (vinum_conf.active >= vinum_conf.maxactive)
442         vinum_conf.maxactive = vinum_conf.active;
443
444 #if VINUMDEBUG
445     if (debug & DEBUG_ADDRESSES)
446         log(LOG_DEBUG,
447             "  %s dev %d.%d, sd %d, offset 0x%x, devoffset 0x%x, length %ld\n",
448             rqe->b.b_flags & B_READ ? "Read" : "Write",
449             major(rqe->b.b_dev),
450             minor(rqe->b.b_dev),
451             rqe->sdno,
452             (u_int) (rqe->b.b_blkno - SD[rqe->sdno].driveoffset),
453             rqe->b.b_blkno,
454             rqe->b.b_bcount);
455     if (debug & DEBUG_LASTREQS)
456         logrq(loginfo_raid5_parity, (union rqinfou) rqe, ubp);
457 #endif
458     BUF_STRATEGY(&rqe->b, 0);
459 }
460
461 /* Local Variables: */
462 /* fill-column: 50 */
463 /* End: */