summaryrefslogtreecommitdiffstats
path: root/sys/dev/vinum/vinumrevive.c
blob: f02c5e86ac0b114b0119f506a7c2c3f6930e2242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*-
 * Copyright (c) 1997, 1998, 1999
 *	Nan Yang Computer Services Limited.  All rights reserved.
 *
 *  Parts copyright (c) 1997, 1998 Cybernet Corporation, NetMAX project.
 *
 *  Written by Greg Lehey
 *
 *  This software is distributed under the so-called ``Berkeley
 *  License'':
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Nan Yang Computer
 *      Services Limited.
 * 4. Neither the name of the Company nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * This software is provided ``as is'', and any express or implied
 * warranties, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose are disclaimed.
 * In no event shall the company or contributors be liable for any
 * direct, indirect, incidental, special, exemplary, or consequential
 * damages (including, but not limited to, procurement of substitute
 * goods or services; loss of use, data, or profits; or business
 * interruption) however caused and on any theory of liability, whether
 * in contract, strict liability, or tort (including negligence or
 * otherwise) arising in any way out of the use of this software, even if
 * advised of the possibility of such damage.
 *
 * $Id: vinumrevive.c,v 1.10 2000/01/03 03:40:54 grog Exp grog $
 * $FreeBSD$
 */

#include <dev/vinum/vinumhdr.h>
#include <dev/vinum/request.h>

/*
 * Revive a block of a subdisk.  Return an error
 * indication.  EAGAIN means successful copy, but
 * that more blocks remain to be copied.  EINVAL
 * means that the subdisk isn't associated with a
 * plex (which means a programming error if we get
 * here at all; FIXME).
 */

int
revive_block(int sdno)
{
    int s;						    /* priority level */
    struct sd *sd;
    struct plex *plex;
    struct volume *vol;
    struct buf *bp;
    int error = EAGAIN;
    int size;						    /* size of revive block, bytes */
    daddr_t plexblkno;					    /* lblkno in plex */
    int psd;						    /* parity subdisk number */
    u_int64_t stripe;					    /* stripe number */
    int paritysd = 0;					    /* set if this is the parity stripe */
    struct rangelock *lock;				    /* for locking */
    daddr_t stripeoffset;				    /* offset in stripe */

    plexblkno = 0;					    /* to keep the compiler happy */
    sd = &SD[sdno];
    lock = NULL;
    if (sd->plexno < 0)					    /* no plex? */
	return EINVAL;
    plex = &PLEX[sd->plexno];				    /* point to plex */
    if (plex->volno >= 0)
	vol = &VOL[plex->volno];
    else
	vol = NULL;

    if ((sd->revive_blocksize == 0)			    /* no block size */
    ||(sd->revive_blocksize & ((1 << DEV_BSHIFT) - 1))) {   /* or invalid block size */
	if (plex->stripesize != 0)			    /* we're striped, don't revive more than */
	    sd->revive_blocksize = min(DEFAULT_REVIVE_BLOCKSIZE, /* one block at a time */
		plex->stripesize << DEV_BSHIFT);
	else
	    sd->revive_blocksize = DEFAULT_REVIVE_BLOCKSIZE;
    } else if (sd->revive_blocksize > MAX_REVIVE_BLOCKSIZE)
	sd->revive_blocksize = MAX_REVIVE_BLOCKSIZE;
    size = min(sd->revive_blocksize >> DEV_BSHIFT, sd->sectors - sd->revived) << DEV_BSHIFT;
    sd->reviver = curproc->p_pid;			    /* note who last had a bash at it */

    /* Now decide where to read from */
    switch (plex->organization) {
    case plex_concat:
	plexblkno = sd->revived + sd->plexoffset;	    /* corresponding address in plex */
	break;

    case plex_striped:
	stripeoffset = sd->revived % plex->stripesize;	    /* offset from beginning of stripe */
	plexblkno = sd->plexoffset			    /* base */
	    + (sd->revived - stripeoffset) * plex->subdisks /* offset to beginning of stripe */
	    + sd->revived % plex->stripesize;		    /* offset from beginning of stripe */
	break;

    case plex_raid4:
    case plex_raid5:
	stripeoffset = sd->revived % plex->stripesize;	    /* offset from beginning of stripe */
	plexblkno = sd->plexoffset			    /* base */
	    + (sd->revived - stripeoffset) * (plex->subdisks - 1) /* offset to beginning of stripe */
	    +stripeoffset;				    /* offset from beginning of stripe */
	stripe = (sd->revived / plex->stripesize);	    /* stripe number */

	/* Make sure we don't go beyond the end of the band. */
	size = min(size, (plex->stripesize - stripeoffset) << DEV_BSHIFT);
	if (plex->organization == plex_raid4)
	    psd = plex->subdisks - 1;			    /* parity subdisk for this stripe */
	else
	    psd = plex->subdisks - 1 - stripe % plex->subdisks;	/* parity subdisk for this stripe */
	paritysd = plex->sdnos[psd] == sdno;		    /* note if it's the parity subdisk */

	/*
	 * Now adjust for the strangenesses
	 * in RAID-4 and RAID-5 striping.
	 */
	if (sd->plexsdno > psd)				    /* beyond the parity stripe, */
	    plexblkno -= plex->stripesize;		    /* one stripe less */
	else if (paritysd)
	    plexblkno -= plex->stripesize * sd->plexsdno;   /* go back to the beginning of the band */
	break;

    case plex_disorg:					    /* to keep the compiler happy */
    }

    if (paritysd) {					    /* we're reviving a parity block, */
	bp = parityrebuild(plex, sd->revived, size, rebuildparity, &lock, NULL); /* do the grunt work */
	if (bp == NULL)					    /* no buffer space */
	    return ENOMEM;				    /* chicken out */
    } else {						    /* data block */
	s = splbio();
	bp = geteblk(size);				    /* Get a buffer */
	splx(s);
	if (bp == NULL)
	    return ENOMEM;

	/*
	 * Amount to transfer: block size, unless it
	 * would overlap the end.
	 */
	bp->b_bcount = size;
	bp->b_resid = bp->b_bcount;
	bp->b_blkno = plexblkno;			    /* start here */
	if (isstriped(plex))				    /* we need to lock striped plexes */
	    lock = lockrange(plexblkno << DEV_BSHIFT, bp, plex); /* lock it */
	if (vol != NULL)				    /* it's part of a volume, */
	    /*
	       * First, read the data from the volume.  We
	       * don't care which plex, that's bre's job.
	     */
	    bp->b_dev = VINUMDEV(plex->volno, 0, 0, VINUM_VOLUME_TYPE);	/* create the device number */
	else						    /* it's an unattached plex */
	    bp->b_dev = VINUM_PLEX(sd->plexno);		    /* create the device number */

	bp->b_iocmd = BIO_READ;				    /* either way, read it */
	bp->b_flags = 0;
	vinumstart(bp, 1);
	bufwait(bp);
    }

    if (bp->b_ioflags & BIO_ERROR)
	error = bp->b_error;
    else
	/* Now write to the subdisk */
    {
	bp->b_dev = VINUM_SD(sdno);			    /* create the device number */
	bp->b_flags &= ~B_DONE;				    /* no longer done */
	bp->b_ioflags = BIO_ORDERED;			    /* and make this an ordered write */
	bp->b_iocmd = BIO_WRITE;
	BUF_LOCKINIT(bp);				    /* get a lock for the buffer */
	BUF_LOCK(bp, LK_EXCLUSIVE);			    /* and lock it */
	bp->b_resid = bp->b_bcount;
	bp->b_blkno = sd->revived;			    /* write it to here */
	sdio(bp);					    /* perform the I/O */
	bufwait(bp);
	if (bp->b_ioflags & BIO_ERROR)
	    error = bp->b_error;
	else {
	    sd->revived += bp->b_bcount >> DEV_BSHIFT;	    /* moved this much further down */
	    if (sd->revived >= sd->sectors) {		    /* finished */
		sd->revived = 0;
		set_sd_state(sdno, sd_up, setstate_force);  /* bring the sd up */
		log(LOG_INFO, "vinum: %s is %s\n", sd->name, sd_state(sd->state));
		save_config();				    /* and save the updated configuration */
		error = 0;				    /* we're done */
	    }
	}
	if (lock)					    /* we took a lock, */
	    unlockrange(sd->plexno, lock);		    /* give it back */
	while (sd->waitlist) {				    /* we have waiting requests */
#if VINUMDEBUG
	    struct request *rq = sd->waitlist;

	    if (debug & DEBUG_REVIVECONFLICT)
		log(LOG_DEBUG,
		    "Relaunch revive conflict sd %d: %p\n%s dev %d.%d, offset 0x%x, length %ld\n",
		    rq->sdno,
		    rq,
		    rq->bp->b_iocmd == BIO_READ ? "Read" : "Write",
		    major(rq->bp->b_dev),
		    minor(rq->bp->b_dev),
		    rq->bp->b_blkno,
		    rq->bp->b_bcount);
#endif
	    launch_requests(sd->waitlist, 1);		    /* do them now */
	    sd->waitlist = sd->waitlist->next;		    /* and move on to the next */
	}
    }
    if (bp->b_qindex == 0) {				    /* not on a queue, */
	bp->b_flags |= B_INVAL;
	bp->b_ioflags &= ~BIO_ERROR;
	brelse(bp);					    /* is this kosher? */
    }
    return error;
}

/*
 * Check or rebuild the parity blocks of a RAID-4
 * or RAID-5 plex.
 *
 * The variables plex->checkblock and
 * plex->rebuildblock represent the
 * subdisk-relative address of the stripe we're
 * looking at, not the plex-relative address.  We
 * store it in the plex and not as a local
 * variable because this function could be
 * stopped, and we don't want to repeat the part
 * we've already done.  This is also the reason
 * why we don't initialize it here except at the
 * end.  It gets initialized with the plex on
 * creation.
 *
 * Each call to this function processes at most
 * one stripe.  We can't loop in this function,
 * because we're unstoppable, so we have to be
 * called repeatedly from userland.
 */
void
parityops(struct vinum_ioctl_msg *data)
{
    int plexno;
    struct plex *plex;
    int size;						    /* I/O transfer size, bytes */
    int stripe;						    /* stripe number in plex */
    int psd;						    /* parity subdisk number */
    struct rangelock *lock;				    /* lock on stripe */
    struct _ioctl_reply *reply;
    off_t pstripe;					    /* pointer to our stripe counter */
    struct buf *pbp;
    off_t errorloc;					    /* offset of parity error */
    enum parityop op;					    /* operation to perform */

    plexno = data->index;
    op = data->op;
    pbp = NULL;
    reply = (struct _ioctl_reply *) data;
    reply->error = EAGAIN;				    /* expect to repeat this call */
    plex = &PLEX[plexno];
    if (!isparity(plex)) {				    /* not RAID-4 or RAID-5 */
	reply->error = EINVAL;
	return;
    } else if (plex->state < plex_flaky) {
	reply->error = EIO;
	strcpy(reply->msg, "Plex is not completely accessible\n");
	return;
    }
    pstripe = data->offset;
    stripe = pstripe / plex->stripesize;		    /* stripe number */
    psd = plex->subdisks - 1 - stripe % plex->subdisks;	    /* parity subdisk for this stripe */
    size = min(DEFAULT_REVIVE_BLOCKSIZE,		    /* one block at a time */
	plex->stripesize << DEV_BSHIFT);

    pbp = parityrebuild(plex, pstripe, size, op, &lock, &errorloc); /* do the grunt work */
    if (pbp == NULL) {					    /* no buffer space */
	reply->error = ENOMEM;
	return;						    /* chicken out */
    }
    /*
     * Now we have a result in the data buffer of
     * the parity buffer header, which we have kept.
     * Decide what to do with it.
     */
    reply->msg[0] = '\0';				    /* until shown otherwise */
    if ((pbp->b_ioflags & BIO_ERROR) == 0) {		    /* no error */
	if ((op == rebuildparity)
	    || (op == rebuildandcheckparity)) {
	    pbp->b_iocmd = BIO_WRITE;
	    pbp->b_resid = pbp->b_bcount;
	    BUF_LOCKINIT(pbp);				    /* get a lock for the buffer */
	    BUF_LOCK(pbp, LK_EXCLUSIVE);		    /* and lock it */
	    sdio(pbp);					    /* write the parity block */
	    bufwait(pbp);
	}
	if (((op == checkparity)
		|| (op == rebuildandcheckparity))
	    && (errorloc != -1)) {
	    if (op == checkparity)
		reply->error = EIO;
	    sprintf(reply->msg,
		"Parity incorrect at offset 0x%llx\n",
		errorloc);
	}
	if (reply->error == EAGAIN) {			    /* still OK, */
	    plex->checkblock = pstripe + (pbp->b_bcount >> DEV_BSHIFT);	/* moved this much further down */
	    if (plex->checkblock >= SD[plex->sdnos[0]].sectors) { /* finished */
		plex->checkblock = 0;
		reply->error = 0;
	    }
	}
    }
    if (pbp->b_ioflags & BIO_ERROR)
	reply->error = pbp->b_error;
    pbp->b_flags |= B_INVAL;
    pbp->b_ioflags &= ~BIO_ERROR;
    brelse(pbp);
    unlockrange(plexno, lock);
}

/*
 * Rebuild a parity stripe.  Return pointer to
 * parity bp.  On return,
 *
 * 1.  The band is locked.  The caller must unlock
 *     the band and release the buffer header.
 *
 * 2.  All buffer headers except php have been
 *     released.  The caller must release pbp.
 *
 * 3.  For checkparity and rebuildandcheckparity,
 *     the parity is compared with the current
 *     parity block.  If it's different, the
 *     offset of the error is returned to
 *     errorloc.  The caller can set the value of
 *     the pointer to NULL if this is called for
 *     rebuilding parity.
 *
 * pstripe is the subdisk-relative base address of
 * the data to be reconstructed, size is the size
 * of the transfer in bytes.
 */
struct buf *
parityrebuild(struct plex *plex,
    u_int64_t pstripe,
    int size,
    enum parityop op,
    struct rangelock **lockp,
    off_t * errorloc)
{
    int error;
    int s;
    int sdno;
    u_int64_t stripe;					    /* stripe number */
    int *parity_buf;					    /* buffer address for current parity block */
    int *newparity_buf;					    /* and for new parity block */
    int mysize;						    /* I/O transfer size for this transfer */
    int isize;						    /* mysize in ints */
    int i;
    int psd;						    /* parity subdisk number */
    int newpsd;						    /* and "subdisk number" of new parity */
    struct buf **bpp;					    /* pointers to our bps */
    struct buf *pbp;					    /* buffer header for parity stripe */
    int *sbuf;
    int bufcount;					    /* number of buffers we need */

    stripe = pstripe / plex->stripesize;		    /* stripe number */
    psd = plex->subdisks - 1 - stripe % plex->subdisks;	    /* parity subdisk for this stripe */
    parity_buf = NULL;					    /* to keep the compiler happy */
    error = 0;

    /*
     * It's possible that the default transfer size
     * we chose is not a factor of the stripe size.
     * We *must* limit this operation to a single
     * stripe, at least for RAID-5 rebuild, since
     * the parity subdisk changes between stripes,
     * so in this case we need to perform a short
     * transfer.  Set variable mysize to reflect
     * this.
     */
    mysize = min(size, (plex->stripesize * (stripe + 1) - pstripe) << DEV_BSHIFT);
    isize = mysize / (sizeof(int));			    /* number of ints in the buffer */
    bufcount = plex->subdisks + 1;			    /* sd buffers plus result buffer */
    newpsd = plex->subdisks;
    bpp = (struct buf **) Malloc(bufcount * sizeof(struct buf *)); /* array of pointers to bps */

    /* First, build requests for all subdisks */
    for (sdno = 0; sdno < bufcount; sdno++) {		    /* for each subdisk */
	if ((sdno != psd) || (op != rebuildparity)) {
	    /* Get a buffer header and initialize it. */
	    s = splbio();
	    bpp[sdno] = geteblk(mysize);		    /* Get a buffer */
	    if (bpp[sdno] == NULL) {
		while (sdno-- > 0) {			    /* release the ones we got */
		    bpp[sdno]->b_flags |= B_INVAL;
		    brelse(bpp[sdno]);			    /* give back our resources */
		}
		splx(s);
		printf("vinum: can't allocate buffer space for parity op.\n");
		return NULL;				    /* no bpps */
	    }
	    splx(s);
	    if (sdno == psd)
		parity_buf = (int *) bpp[sdno]->b_data;
	    if (sdno == newpsd)				    /* the new one? */
		bpp[sdno]->b_dev = VINUM_SD(plex->sdnos[psd]); /* write back to the parity SD */
	    else
		bpp[sdno]->b_dev = VINUM_SD(plex->sdnos[sdno]);	/* device number */
	    bpp[sdno]->b_iocmd = BIO_READ;		    /* either way, read it */
	    bpp[sdno]->b_flags = 0;
	    bpp[sdno]->b_bcount = mysize;
	    bpp[sdno]->b_resid = bpp[sdno]->b_bcount;
	    bpp[sdno]->b_blkno = pstripe;		    /* transfer from here */
	}
    }

    /* Initialize result buffer */
    pbp = bpp[newpsd];
    newparity_buf = (int *) bpp[newpsd]->b_data;
    bzero(newparity_buf, mysize);

    /*
     * Now lock the stripe with the first non-parity
     * bp as locking bp.
     */
    *lockp = lockrange(pstripe * plex->stripesize * (plex->subdisks - 1),
	bpp[psd ? 0 : 1],
	plex);

    /*
     * Then issue requests for all subdisks in
     * parallel.  Don't transfer the parity stripe
     * if we're rebuilding parity, unless we also
     * want to check it.
     */
    for (sdno = 0; sdno < plex->subdisks; sdno++) {	    /* for each real subdisk */
	if ((sdno != psd) || (op != rebuildparity)) {
	    BUF_LOCKINIT(bpp[sdno]);			    /* get a lock for the buffer */
	    BUF_LOCK(bpp[sdno], LK_EXCLUSIVE);		    /* and lock it */
	    sdio(bpp[sdno]);
	}
    }

    /*
     * Next, wait for the requests to complete.
     * We wait in the order in which they were
     * issued, which isn't necessarily the order in
     * which they complete, but we don't have a
     * convenient way of doing the latter, and the
     * delay is minimal.
     */
    for (sdno = 0; sdno < plex->subdisks; sdno++) {	    /* for each subdisk */
	if ((sdno != psd) || (op != rebuildparity)) {
	    bufwait(bpp[sdno]);
	    if (bpp[sdno]->b_ioflags & BIO_ERROR)	    /* can't read, */
		error = bpp[sdno]->b_error;
	    else if (sdno != psd) {			    /* update parity */
		sbuf = (int *) bpp[sdno]->b_data;
		for (i = 0; i < isize; i++)
		    ((int *) newparity_buf)[i] ^= sbuf[i];  /* xor in the buffer */
	    }
	}
	if (sdno != psd) {				    /* release all bps except parity */
	    bpp[sdno]->b_flags |= B_INVAL;
	    brelse(bpp[sdno]);				    /* give back our resources */
	}
    }

    /*
     * If we're checking, compare the calculated
     * and the read parity block.  If they're
     * different, return the plex-relative offset;
     * otherwise return -1.
     */
    if ((op == checkparity)
	|| (op == rebuildandcheckparity)) {
	*errorloc = -1;					    /* no error yet */
	for (i = 0; i < isize; i++) {
	    if (parity_buf[i] != newparity_buf[i]) {
		*errorloc = (u_long) (pstripe << DEV_BSHIFT) * (plex->subdisks - 1)
		    + i * sizeof(int);
		break;
	    }
	}
	bpp[psd]->b_flags |= B_INVAL;
	brelse(bpp[psd]);				    /* give back our resources */
    }
    /* release our resources */
    Free(bpp);
    if (error) {
	pbp->b_ioflags |= BIO_ERROR;
	pbp->b_error = error;
    }
    return pbp;
}

/*
 * Initialize a subdisk by writing zeroes to the
 * complete address space.  If verify is set,
 * check each transfer for correctness.
 *
 * Each call to this function writes (and maybe
 * checks) a single block.
 */
int
initsd(int sdno, int verify)
{
    int s;						    /* priority level */
    struct sd *sd;
    struct plex *plex;
    struct volume *vol;
    struct buf *bp;
    int error;
    int size;						    /* size of init block, bytes */
    daddr_t plexblkno;					    /* lblkno in plex */
    int verified;					    /* set when we're happy with what we wrote */

    error = 0;
    plexblkno = 0;					    /* to keep the compiler happy */
    sd = &SD[sdno];
    if (sd->plexno < 0)					    /* no plex? */
	return EINVAL;
    plex = &PLEX[sd->plexno];				    /* point to plex */
    if (plex->volno >= 0)
	vol = &VOL[plex->volno];
    else
	vol = NULL;

    if (sd->init_blocksize == 0) {
	if (plex->stripesize != 0)			    /* we're striped, don't init more than */
	    sd->init_blocksize = min(DEFAULT_REVIVE_BLOCKSIZE, /* one block at a time */
		plex->stripesize << DEV_BSHIFT);
	else
	    sd->init_blocksize = DEFAULT_REVIVE_BLOCKSIZE;
    } else if (sd->init_blocksize > MAX_REVIVE_BLOCKSIZE)
	sd->init_blocksize = MAX_REVIVE_BLOCKSIZE;

    size = min(sd->init_blocksize >> DEV_BSHIFT, sd->sectors - sd->initialized) << DEV_BSHIFT;

    verified = 0;
    while (!verified) {					    /* until we're happy with it, */
	s = splbio();
	bp = geteblk(size);				    /* Get a buffer */
	splx(s);
	if (bp == NULL)
	    return ENOMEM;

	bp->b_bcount = size;
	bp->b_resid = bp->b_bcount;
	bp->b_blkno = sd->initialized;			    /* write it to here */
	bzero(bp->b_data, bp->b_bcount);
	bp->b_dev = VINUM_SD(sdno);			    /* create the device number */
	BUF_LOCKINIT(bp);				    /* get a lock for the buffer */
	BUF_LOCK(bp, LK_EXCLUSIVE);			    /* and lock it */
	bp->b_iocmd = BIO_WRITE;
	sdio(bp);					    /* perform the I/O */
	bufwait(bp);
	if (bp->b_ioflags & BIO_ERROR)
	    error = bp->b_error;
	if (bp->b_qindex == 0) {			    /* not on a queue, */
	    bp->b_flags |= B_INVAL;
	    bp->b_ioflags &= ~BIO_ERROR;
	    brelse(bp);					    /* is this kosher? */
	}
	if ((error == 0) && verify) {			    /* check that it got there */
	    s = splbio();
	    bp = geteblk(size);				    /* get a buffer */
	    if (bp == NULL) {
		splx(s);
		error = ENOMEM;
	    } else {
		bp->b_bcount = size;
		bp->b_resid = bp->b_bcount;
		bp->b_blkno = sd->initialized;		    /* read from here */
		bp->b_dev = VINUM_SD(sdno);		    /* create the device number */
		bp->b_iocmd = BIO_READ;			    /* read it back */
		splx(s);
		BUF_LOCKINIT(bp);			    /* get a lock for the buffer */
		BUF_LOCK(bp, LK_EXCLUSIVE);		    /* and lock it */
		sdio(bp);
		bufwait(bp);
		/*
		 * XXX Bug fix code.  This is hopefully no
		 * longer needed (21 February 2000).
		 */
		if (bp->b_ioflags & BIO_ERROR)
		    error = bp->b_error;
		else if ((*bp->b_data != 0)		    /* first word spammed */
		||(bcmp(bp->b_data, &bp->b_data[1], bp->b_bcount - 1))) { /* or one of the others */
		    printf("vinum: init error on %s, offset 0x%llx sectors\n",
			sd->name,
			(long long) sd->initialized);
		    verified = 0;
		} else
		    verified = 1;
		if (bp->b_qindex == 0) {		    /* not on a queue, */
		    bp->b_flags |= B_INVAL;
		    bp->b_ioflags &= ~BIO_ERROR;
		    brelse(bp);				    /* is this kosher? */
		}
	    }
	} else
	    verified = 1;
    }
    if (error == 0) {					    /* did it, */
	sd->initialized += size >> DEV_BSHIFT;		    /* moved this much further down */
	if (sd->initialized >= sd->sectors) {		    /* finished */
	    sd->initialized = 0;
	    set_sd_state(sdno, sd_initialized, setstate_force);	/* bring the sd up */
	    log(LOG_INFO, "vinum: %s is %s\n", sd->name, sd_state(sd->state));
	    save_config();				    /* and save the updated configuration */
	} else						    /* more to go, */
	    error = EAGAIN;				    /* ya'll come back, see? */
    }
    return error;
}

/* Local Variables: */
/* fill-column: 50 */
/* End: */
OpenPOWER on IntegriCloud