Home | History | Annotate | Line # | Download | only in raidframe
rf_netbsdkintf.c revision 1.6
      1 /*	$NetBSD: rf_netbsdkintf.c,v 1.6 1999/01/14 22:49:05 thorpej Exp $	*/
      2 /*-
      3  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Greg Oster; Jason R. Thorpe.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *        This product includes software developed by the NetBSD
     20  *        Foundation, Inc. and its contributors.
     21  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  *    contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright (c) 1988 University of Utah.
     40  * Copyright (c) 1990, 1993
     41  *      The Regents of the University of California.  All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * the Systems Programming Group of the University of Utah Computer
     45  * Science Department.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. All advertising materials mentioning features or use of this software
     56  *    must display the following acknowledgement:
     57  *      This product includes software developed by the University of
     58  *      California, Berkeley and its contributors.
     59  * 4. Neither the name of the University nor the names of its contributors
     60  *    may be used to endorse or promote products derived from this software
     61  *    without specific prior written permission.
     62  *
     63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     73  * SUCH DAMAGE.
     74  *
     75  * from: Utah $Hdr: cd.c 1.6 90/11/28$
     76  *
     77  *      @(#)cd.c        8.2 (Berkeley) 11/16/93
     78  */
     79 
     80 
     81 
     82 
     83 /*
     84  * Copyright (c) 1995 Carnegie-Mellon University.
     85  * All rights reserved.
     86  *
     87  * Authors: Mark Holland, Jim Zelenka
     88  *
     89  * Permission to use, copy, modify and distribute this software and
     90  * its documentation is hereby granted, provided that both the copyright
     91  * notice and this permission notice appear in all copies of the
     92  * software, derivative works or modified versions, and any portions
     93  * thereof, and that both notices appear in supporting documentation.
     94  *
     95  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     96  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     97  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     98  *
     99  * Carnegie Mellon requests users of this software to return to
    100  *
    101  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
    102  *  School of Computer Science
    103  *  Carnegie Mellon University
    104  *  Pittsburgh PA 15213-3890
    105  *
    106  * any improvements or extensions that they make and grant Carnegie the
    107  * rights to redistribute these changes.
    108  */
    109 
    110 /***********************************************************
    111  *
    112  * rf_kintf.c -- the kernel interface routines for RAIDframe
    113  *
    114  ***********************************************************/
    115 /*
    116  * :
    117  * Log: rf_kintf.c,v
    118  * Revision 1.57  1996/07/19 16:12:20  jimz
    119  * remove addition of protectedSectors in InitBP- it's already
    120  * done in the diskqueue code
    121  *
    122  * Revision 1.56  1996/07/17  21:00:58  jimz
    123  * clean up timer interface, tracing
    124  *
    125  * Revision 1.55  1996/06/17  03:00:54  jimz
    126  * Change RAIDFRAME_GET_INFO interface to do its own copyout()
    127  * (because size of device config structure now exceeds 8k)
    128  *
    129  * Revision 1.54  1996/06/09  02:36:46  jimz
    130  * lots of little crufty cleanup- fixup whitespace
    131  * issues, comment #ifdefs, improve typing in some
    132  * places (esp size-related)
    133  *
    134  * Revision 1.53  1996/06/07  21:33:04  jimz
    135  * begin using consistent types for sector numbers,
    136  * stripe numbers, row+col numbers, recon unit numbers
    137  *
    138  * Revision 1.52  1996/06/06  17:28:08  jimz
    139  * track sector number of last I/O dequeued
    140  *
    141  * Revision 1.51  1996/06/05  18:06:02  jimz
    142  * Major code cleanup. The Great Renaming is now done.
    143  * Better modularity. Better typing. Fixed a bunch of
    144  * synchronization bugs. Made a lot of global stuff
    145  * per-desc or per-array. Removed dead code.
    146  *
    147  * Revision 1.50  1996/06/03  23:28:26  jimz
    148  * more bugfixes
    149  * check in tree to sync for IPDS runs with current bugfixes
    150  * there still may be a problem with threads in the script test
    151  * getting I/Os stuck- not trivially reproducible (runs ~50 times
    152  * in a row without getting stuck)
    153  *
    154  * Revision 1.49  1996/06/02  17:31:48  jimz
    155  * Moved a lot of global stuff into array structure, where it belongs.
    156  * Fixed up paritylogging, pss modules in this manner. Some general
    157  * code cleanup. Removed lots of dead code, some dead files.
    158  *
    159  * Revision 1.48  1996/05/31  22:26:54  jimz
    160  * fix a lot of mapping problems, memory allocation problems
    161  * found some weird lock issues, fixed 'em
    162  * more code cleanup
    163  *
    164  * Revision 1.47  1996/05/30  12:59:18  jimz
    165  * make etimer happier, more portable
    166  *
    167  * Revision 1.46  1996/05/30  11:29:41  jimz
    168  * Numerous bug fixes. Stripe lock release code disagreed with the taking code
    169  * about when stripes should be locked (I made it consistent: no parity, no lock)
    170  * There was a lot of extra serialization of I/Os which I've removed- a lot of
    171  * it was to calculate values for the cache code, which is no longer with us.
    172  * More types, function, macro cleanup. Added code to properly quiesce the array
    173  * on shutdown. Made a lot of stuff array-specific which was (bogusly) general
    174  * before. Fixed memory allocation, freeing bugs.
    175  *
    176  * Revision 1.45  1996/05/27  18:56:37  jimz
    177  * more code cleanup
    178  * better typing
    179  * compiles in all 3 environments
    180  *
    181  * Revision 1.44  1996/05/24  22:17:04  jimz
    182  * continue code + namespace cleanup
    183  * typed a bunch of flags
    184  *
    185  * Revision 1.43  1996/05/24  01:59:45  jimz
    186  * another checkpoint in code cleanup for release
    187  * time to sync kernel tree
    188  *
    189  * Revision 1.42  1996/05/23  22:17:54  jimz
    190  * fix sector size hardcoding problems
    191  *
    192  * Revision 1.41  1996/05/23  21:46:35  jimz
    193  * checkpoint in code cleanup (release prep)
    194  * lots of types, function names have been fixed
    195  *
    196  * Revision 1.40  1996/05/23  13:18:07  jimz
    197  * tracing_mutex -> rf_tracing_mutex
    198  *
    199  * Revision 1.39  1996/05/23  00:33:23  jimz
    200  * code cleanup: move all debug decls to rf_options.c, all extern
    201  * debug decls to rf_options.h, all debug vars preceded by rf_
    202  *
    203  * Revision 1.38  1996/05/20  16:15:32  jimz
    204  * switch to rf_{mutex,cond}_{init,destroy}
    205  *
    206  * Revision 1.37  1996/05/10  16:23:47  jimz
    207  * RF_offset -> RF_Offset
    208  *
    209  * Revision 1.36  1996/05/08  21:01:24  jimz
    210  * fixed up enum type names that were conflicting with other
    211  * enums and function names (ie, "panic")
    212  * future naming trends will be towards RF_ and rf_ for
    213  * everything raidframe-related
    214  *
    215  * Revision 1.35  1996/05/03  19:10:48  jimz
    216  * change sanity checking for bogus I/Os to return more appropriate
    217  * values (to make some user-level utilities happer with RAIDframe)
    218  *
    219  * Revision 1.34  1996/05/02  22:17:00  jimz
    220  * When using DKUSAGE, send a bogus IO after configuring to let DKUSAGE know
    221  * that we exist. This will let user-level programs doing group stats on the
    222  * RF device function without error before RF gets its first IO
    223  *
    224  * Changed rf_device_config devs and spares fields to RF_RaidDisk_t
    225  *
    226  * Inc numOutstanding for the disk queue in rf_DispatchKernelIO if
    227  * type is IO_TYPE_NOP. I'm not sure this is right, but it seems to be,
    228  * because the disk IO completion routine wants to dec it, and doesn't
    229  * care if there was no such IO.
    230  *
    231  * Revision 1.33  1996/05/02  15:05:44  jimz
    232  * for now, rf_DoAccessKernel will reject non-sector-sized I/Os
    233  * eventually, it should do something more clever...
    234  * (and do it in DoAccess(), not just DoAccessKernel())
    235  *
    236  * Revision 1.32  1996/05/01  16:28:39  jimz
    237  * get rid of uses of ccmn_ functions
    238  *
    239  * Revision 1.31  1996/05/01  15:42:17  jimz
    240  * ccmn_* memory management is on the way out. This is an archival checkpoint-
    241  * both the old and new code are in place (all the ccmn_ calls are #if 0). After
    242  * this, the ccmn_ code will no longer appear.
    243  *
    244  * Revision 1.30  1996/04/22  15:53:13  jimz
    245  * MAX_RAIDS -> NRAIDFRAME
    246  *
    247  * Revision 1.29  1995/12/12  18:10:06  jimz
    248  * MIN -> RF_MIN, MAX -> RF_MAX, ASSERT -> RF_ASSERT
    249  * fix 80-column brain damage in comments
    250  *
    251  * Revision 1.28  1995/12/01  19:11:01  root
    252  * added copyright info
    253  *
    254  * Revision 1.27  1995/11/28  18:56:40  wvcii
    255  * disabled buffer copy in rf_write
    256  *
    257  * Revision 1.26  1995/10/06  16:37:08  jimz
    258  * get struct bufs from ubc, not cam
    259  * copy all write data, and operate on copy
    260  * (temporary hack to get around dags in PQ that want
    261  * to Xor into user write buffers)
    262  *
    263  * Revision 1.25  1995/09/30  22:23:08  jimz
    264  * do not require raid to be active to perform ACCTOTAL ioctl
    265  *
    266  * Revision 1.24  1995/09/30  20:39:08  jimz
    267  * added new ioctls:
    268  *   RAIDFRAME_RESET_ACCTOTALS
    269  *   RAIDFRAME_GET_ACCTOTALS
    270  *   RAIDFRAME_KEEP_ACCTOTALS
    271  *
    272  * Revision 1.23  1995/09/20  21:11:59  jimz
    273  * include dfstrace.h in KERNEL block
    274  * (even though it's a kernel-only file, this makes the depend process
    275  * at user-level happy. Why the user-level Makefile wants to depend
    276  * kintf.c is less clear, but this is a workaround).
    277  *
    278  * Revision 1.22  1995/09/19  23:19:03  jimz
    279  * added DKUSAGE support
    280  *
    281  */
    282 
    283 
    284 
    285 
    286 #ifdef _KERNEL
    287 #define KERNEL
    288 #endif
    289 
    290 
    291 
    292 #ifdef KERNEL
    293 
    294 #include <sys/errno.h>
    295 
    296 #ifdef __NetBSD__
    297 #include "raid.h"
    298 #include <sys/param.h>
    299 #include <sys/pool.h>
    300 #include <sys/queue.h>
    301 #include <sys/disk.h>
    302 #include <sys/device.h>
    303 #include <sys/stat.h>
    304 #include <sys/ioctl.h>
    305 #include <sys/fcntl.h>
    306 #include <sys/systm.h>
    307 #include <sys/namei.h>
    308 #include <sys/vnode.h>
    309 #endif
    310 
    311 #include <sys/param.h>
    312 #include <sys/types.h>
    313 
    314 #include <machine/types.h>
    315 
    316 #include <sys/disklabel.h>
    317 
    318 #include <sys/conf.h>
    319 
    320 
    321 #ifdef __NetBSD__
    322 #include <sys/lock.h>
    323 #endif /* __NetBSD__ */
    324 
    325 
    326 #include <sys/buf.h>
    327 #include <sys/user.h>
    328 #include "rf_raid.h"
    329 #include "rf_raidframe.h"
    330 #include "rf_dag.h"
    331 #include "rf_dagflags.h"
    332 #include "rf_diskqueue.h"
    333 #include "rf_acctrace.h"
    334 #include "rf_etimer.h"
    335 #include "rf_general.h"
    336 #include "rf_debugMem.h"
    337 #include "rf_kintf.h"
    338 #include "rf_options.h"
    339 #include "rf_driver.h"
    340 #include "rf_parityscan.h"
    341 #include "rf_debugprint.h"
    342 #include "rf_threadstuff.h"
    343 
    344 int rf_kdebug_level = 0;
    345 
    346 #define RFK_BOOT_NONE 0
    347 #define RFK_BOOT_GOOD 1
    348 #define RFK_BOOT_BAD  2
    349 static int rf_kbooted = RFK_BOOT_NONE;
    350 
    351 #ifdef DEBUG
    352 #define db0_printf(a) printf a
    353 #define db_printf(a) if (rf_kdebug_level > 0) printf a
    354 #define db1_printf(a) if (rf_kdebug_level > 0) printf a
    355 #define db2_printf(a) if (rf_kdebug_level > 1) printf a
    356 #define db3_printf(a) if (rf_kdebug_level > 2) printf a
    357 #define db4_printf(a) if (rf_kdebug_level > 3) printf a
    358 #define db5_printf(a) if (rf_kdebug_level > 4) printf a
    359 #else /* DEBUG */
    360 #define db0_printf(a) printf a
    361 #define db1_printf(a) { }
    362 #define db2_printf(a) { }
    363 #define db3_printf(a) { }
    364 #define db4_printf(a) { }
    365 #define db5_printf(a) { }
    366 #endif /* DEBUG */
    367 
    368 static RF_Raid_t **raidPtrs; /* global raid device descriptors */
    369 
    370 static int rf_pending_testaccs;
    371 
    372 RF_DECLARE_STATIC_MUTEX(rf_sparet_wait_mutex)
    373 RF_DECLARE_STATIC_MUTEX(rf_async_done_q_mutex)
    374 static RF_SparetWait_t *rf_sparet_wait_queue;      /* requests to install a spare table */
    375 static RF_SparetWait_t *rf_sparet_resp_queue;      /* responses from installation process */
    376 static struct rf_test_acc    *rf_async_done_qh, *rf_async_done_qt;
    377 
    378 static struct rf_recon_req *recon_queue = NULL;          /* used to communicate reconstruction requests */
    379 
    380 
    381 decl_simple_lock_data(,recon_queue_mutex)
    382 
    383 
    384 #define LOCK_RECON_Q_MUTEX() simple_lock(&recon_queue_mutex)
    385 #define UNLOCK_RECON_Q_MUTEX() simple_unlock(&recon_queue_mutex)
    386 
    387 /* prototypes */
    388 static void KernelWakeupFunc(struct buf  *bp);
    389 static void InitBP(struct buf *bp, struct vnode *, unsigned rw_flag, dev_t dev,
    390 	RF_SectorNum_t startSect, RF_SectorCount_t numSect, caddr_t buf,
    391 	void (*cbFunc)(struct buf *), void *cbArg, int logBytesPerSector,
    392 	struct proc *b_proc);
    393 
    394 #define Dprintf0(s)       if (rf_queueDebug) rf_debug_printf(s,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)
    395 #define Dprintf1(s,a)     if (rf_queueDebug) rf_debug_printf(s,a,NULL,NULL,NULL,NULL,NULL,NULL,NULL)
    396 #define Dprintf2(s,a,b)   if (rf_queueDebug) rf_debug_printf(s,a,b,NULL,NULL,NULL,NULL,NULL,NULL)
    397 #define Dprintf3(s,a,b,c) if (rf_queueDebug) rf_debug_printf(s,a,b,c,NULL,NULL,NULL,NULL,NULL)
    398 
    399 
    400 /* this is so that we can compile under 2.0 as well as 3.2 */
    401 #ifndef proc_to_task
    402 #define proc_to_task(x) ((x)->task)
    403 #endif /* !proc_to_task */
    404 
    405 void	raidattach __P((int));
    406 int	raidsize __P((dev_t));
    407 
    408 void	rf_DiskIOComplete(RF_DiskQueue_t *, RF_DiskQueueData_t *, int);
    409 void	rf_CopybackReconstructedData(RF_Raid_t *raidPtr);
    410 static	int raidinit __P((dev_t,RF_Raid_t *,int));
    411 
    412 int	raidopen __P((dev_t, int, int, struct proc *));
    413 int	raidclose __P((dev_t, int, int, struct proc *));
    414 int	raidioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
    415 int	raidwrite __P((dev_t, struct uio *, int));
    416 int	raidread __P((dev_t, struct uio *, int));
    417 void	raidstrategy __P((struct buf *));
    418 int	raiddump __P((dev_t, daddr_t, caddr_t, size_t));
    419 
    420 /*
    421  * Pilfered from ccd.c
    422  */
    423 
    424 struct raidbuf {
    425         struct buf	rf_buf;		/* new I/O buf.  MUST BE FIRST!!! */
    426 	struct buf	*rf_obp;	/* ptr. to original I/O buf */
    427 	int		rf_flags;	/* misc. flags */
    428 	RF_DiskQueueData_t  *req;       /* the request that this was part of.. */
    429 };
    430 
    431 
    432 #define RAIDGETBUF(rs) pool_get(&(rs)->sc_cbufpool, PR_NOWAIT)
    433 #define	RAIDPUTBUF(rs, cbp) pool_put(&(rs)->sc_cbufpool, cbp)
    434 
    435 /* XXX Not sure if the following should be replacing the raidPtrs above,
    436 or if it should be used in conjunction with that... */
    437 
    438 struct raid_softc {
    439 	int		 sc_unit;		/* logical unit number */
    440 	int		 sc_flags;		/* flags */
    441 	int		 sc_cflags;		/* configuration flags */
    442 	size_t		 sc_size;		/* size of the raid device */
    443 	dev_t            sc_dev;                /* our device..*/
    444 	char		 sc_xname[20];		/* XXX external name */
    445 	struct disk	 sc_dkdev;		/* generic disk device info */
    446 	struct pool      sc_cbufpool;           /* component buffer pool */
    447 };
    448 
    449 /* sc_flags */
    450 #define RAIDF_INITED	0x01	/* unit has been initialized */
    451 #define RAIDF_WLABEL	0x02	/* label area is writable */
    452 #define RAIDF_LABELLING	0x04	/* unit is currently being labelled */
    453 #define RAIDF_WANTED	0x40	/* someone is waiting to obtain a lock */
    454 #define RAIDF_LOCKED	0x80	/* unit is locked */
    455 
    456 #define	raidunit(x)	DISKUNIT(x)
    457 static int numraid=0;
    458 
    459 #define RAIDLABELDEV(dev)	\
    460 	(MAKEDISKDEV(major((dev)), raidunit((dev)), RAW_PART))
    461 
    462 /* declared here, and made public, for the benefit of KVM stuff.. */
    463 struct raid_softc *raid_softc;
    464 
    465 static	void raidgetdefaultlabel __P((RF_Raid_t *, struct raid_softc *, struct disklabel *));
    466 static	void raidgetdisklabel __P((dev_t));
    467 static	void raidmakedisklabel __P((struct raid_softc *));
    468 
    469 static	int raidlock __P((struct raid_softc *));
    470 static	void raidunlock __P((struct raid_softc *));
    471 int raidlookup __P((char *, struct proc *p, struct vnode **));
    472 
    473 
    474 void
    475 raidattach(num)
    476 	int num;
    477 {
    478 	int raidID;
    479 
    480 #ifdef DEBUG
    481 	printf("raidattach: Asked for %d units\n",num);
    482 #endif
    483 
    484 	if (num <= 0) {
    485 #ifdef DIAGNOSTIC
    486 		panic("raidattach: count <= 0");
    487 #endif
    488 		return;
    489 	}
    490 	/*
    491 	   This is where all the initialization stuff gets done.
    492 	 */
    493 
    494 	/* Make some space for requested number of units... */
    495 
    496 	RF_Calloc(raidPtrs, num, sizeof(RF_Raid_t *), (RF_Raid_t **));
    497 	if (raidPtrs == NULL) {
    498 		panic("raidPtrs is NULL!!\n");
    499 	}
    500 
    501 
    502 
    503 	rf_kbooted = rf_boot();
    504 	if (rf_kbooted) {
    505 		panic("Serious error booting RAID!!\n");
    506 	}
    507 
    508 	rf_kbooted = RFK_BOOT_GOOD;
    509 
    510 	/*
    511 	   put together some datastructures like the CCD device does..
    512 	   This lets us lock the device and what-not when it gets opened.
    513         */
    514 
    515 	raid_softc = (struct raid_softc *)
    516 		malloc(num * sizeof(struct raid_softc),
    517 		       M_RAIDFRAME, M_NOWAIT);
    518 	if (raid_softc == NULL) {
    519 		printf("WARNING: no memory for RAIDframe driver\n");
    520 		return;
    521 	}
    522 	numraid = num;
    523 	bzero(raid_softc, num * sizeof(struct raid_softc));
    524 
    525 	for(raidID=0;raidID < num;raidID++) {
    526 		RF_Calloc(raidPtrs[raidID], 1, sizeof(RF_Raid_t),
    527 			  (RF_Raid_t *));
    528 		if (raidPtrs[raidID]==NULL) {
    529 			printf("raidPtrs[%d] is NULL\n",raidID);
    530 		}
    531 	}
    532 }
    533 
    534 
    535 int
    536 raidsize(dev)
    537 	dev_t dev;
    538 {
    539 	struct raid_softc *rs;
    540 	struct disklabel *lp;
    541 	int part, unit, omask, size;
    542 
    543 	unit = raidunit(dev);
    544 	if (unit >= numraid)
    545 		return (-1);
    546 	rs = &raid_softc[unit];
    547 
    548 	if ((rs->sc_flags & RAIDF_INITED) == 0)
    549 		return (-1);
    550 
    551 	part = DISKPART(dev);
    552 	omask = rs->sc_dkdev.dk_openmask & (1 << part);
    553 	lp = rs->sc_dkdev.dk_label;
    554 
    555 	if (omask == 0 && raidopen(dev, 0, S_IFBLK, curproc))
    556 		return (-1);
    557 
    558 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
    559 		size = -1;
    560 	else
    561 		size = lp->d_partitions[part].p_size *
    562 		    (lp->d_secsize / DEV_BSIZE);
    563 
    564 	if (omask == 0 && raidclose(dev, 0, S_IFBLK, curproc))
    565 		return (-1);
    566 
    567 	return (size);
    568 
    569 }
    570 
    571 int
    572 raiddump(dev, blkno, va, size)
    573 	dev_t dev;
    574 	daddr_t blkno;
    575 	caddr_t va;
    576 	size_t size;
    577 {
    578 	/* Not implemented. */
    579 	return ENXIO;
    580 }
    581 
    582 /* ARGSUSED */
    583 int
    584 raidopen(dev, flags, fmt, p)
    585 	dev_t dev;
    586 	int flags, fmt;
    587 	struct proc *p;
    588 {
    589 	int unit = raidunit(dev);
    590 	struct raid_softc *rs;
    591 	struct disklabel *lp;
    592 	int part,pmask;
    593 	unsigned int raidID;
    594 	int rc;
    595 	int error = 0;
    596 
    597 	/* This whole next chunk of code is somewhat suspect... Not sure
    598 	   it's needed here at all... XXX */
    599 
    600 	if (rf_kbooted == RFK_BOOT_NONE) {
    601 		printf("Doing restart on raidopen.\n");
    602 		rf_kbooted = RFK_BOOT_GOOD;
    603 		rc = rf_boot();
    604 		if (rc) {
    605 			rf_kbooted = RFK_BOOT_BAD;
    606 			printf("Someone is unhappy...\n");
    607 			return(rc);
    608 		}
    609 	}
    610 
    611 	if (unit >= numraid)
    612 		return (ENXIO);
    613 	rs = &raid_softc[unit];
    614 
    615 	if ((error = raidlock(rs)) != 0)
    616 		return(error);
    617 	lp = rs->sc_dkdev.dk_label;
    618 
    619 	raidID = raidunit(dev);
    620 
    621 	part = DISKPART(dev);
    622 	pmask = (1 << part);
    623 
    624 	db1_printf(("Opening raid device number: %d partition: %d\n",
    625 		    raidID,part));
    626 
    627 
    628 	if ((rs->sc_flags & RAIDF_INITED) &&
    629 	    (rs->sc_dkdev.dk_openmask == 0))
    630 		raidgetdisklabel(dev);
    631 
    632 	/* make sure that this partition exists */
    633 
    634 	if (part != RAW_PART) {
    635 		db1_printf(("Not a raw partition..\n"));
    636 		if (((rs->sc_flags & RAIDF_INITED) == 0) ||
    637 		    ((part >= lp->d_npartitions) ||
    638 		     (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
    639 			error = ENXIO;
    640 			raidunlock(rs);
    641 			db1_printf(("Bailing out...\n"));
    642 			return(error);
    643 		}
    644 	}
    645 
    646 	/* Prevent this unit from being unconfigured while open. */
    647 	switch (fmt) {
    648 	case S_IFCHR:
    649 		rs->sc_dkdev.dk_copenmask |= pmask;
    650 		break;
    651 
    652 	case S_IFBLK:
    653 		rs->sc_dkdev.dk_bopenmask |= pmask;
    654 		break;
    655 	}
    656 	rs->sc_dkdev.dk_openmask =
    657 	    rs->sc_dkdev.dk_copenmask | rs->sc_dkdev.dk_bopenmask;
    658 
    659 	raidunlock(rs);
    660 
    661 	return(error);
    662 
    663 
    664 }
    665 
    666 /* ARGSUSED */
    667 int
    668 raidclose(dev, flags, fmt, p)
    669 	dev_t dev;
    670 	int flags, fmt;
    671 	struct proc *p;
    672 {
    673 	int unit = raidunit(dev);
    674 	struct raid_softc *rs;
    675 	int error = 0;
    676 	int part;
    677 
    678 	if (unit >= numraid)
    679 		return (ENXIO);
    680 	rs = &raid_softc[unit];
    681 
    682 	if ((error = raidlock(rs)) != 0)
    683 		return (error);
    684 
    685 	part = DISKPART(dev);
    686 
    687 	/* ...that much closer to allowing unconfiguration... */
    688 	switch (fmt) {
    689 	case S_IFCHR:
    690 		rs->sc_dkdev.dk_copenmask &= ~(1 << part);
    691 		break;
    692 
    693 	case S_IFBLK:
    694 		rs->sc_dkdev.dk_bopenmask &= ~(1 << part);
    695 		break;
    696 	}
    697 	rs->sc_dkdev.dk_openmask =
    698 	    rs->sc_dkdev.dk_copenmask | rs->sc_dkdev.dk_bopenmask;
    699 
    700 	raidunlock(rs);
    701 	return (0);
    702 
    703 }
    704 
    705 void
    706 raidstrategy(bp)
    707 	register struct buf *bp;
    708 {
    709 	register int s;
    710 
    711 	unsigned int raidID = raidunit(bp->b_dev);
    712 	RF_Raid_t *raidPtr;
    713 	struct raid_softc *rs = &raid_softc[raidID];
    714 	struct disklabel *lp;
    715 	int wlabel;
    716 
    717 #if 0
    718 	db1_printf(("Strategy: 0x%x 0x%x\n",bp,bp->b_data));
    719 	db1_printf(("Strategy(2): bp->b_bufsize%d\n", (int)bp->b_bufsize));
    720 	db1_printf(("bp->b_count=%d\n",(int)bp->b_bcount));
    721 	db1_printf(("bp->b_resid=%d\n",(int)bp->b_resid));
    722 	db1_printf(("bp->b_blkno=%d\n",(int)bp->b_blkno));
    723 
    724 	if (bp->b_flags&B_READ)
    725 		db1_printf(("READ\n"));
    726 	else
    727 		db1_printf(("WRITE\n"));
    728 #endif
    729 	if (rf_kbooted != RFK_BOOT_GOOD)
    730 		return;
    731 	if (raidID >= numraid || !raidPtrs[raidID]) {
    732 		bp->b_error = ENODEV;
    733 		bp->b_flags |= B_ERROR;
    734 		bp->b_resid = bp->b_bcount;
    735 		biodone(bp);
    736 		return;
    737 	}
    738 	raidPtr = raidPtrs[raidID];
    739 	if (!raidPtr->valid) {
    740 		bp->b_error = ENODEV;
    741 		bp->b_flags |= B_ERROR;
    742 		bp->b_resid = bp->b_bcount;
    743 		biodone(bp);
    744 		return;
    745 	}
    746 	if (bp->b_bcount == 0) {
    747 		db1_printf(("b_bcount is zero..\n"));
    748 		biodone(bp);
    749 		return;
    750 	}
    751 	lp = rs->sc_dkdev.dk_label;
    752 
    753 	/*
    754 	 * Do bounds checking and adjust transfer.  If there's an
    755 	 * error, the bounds check will flag that for us.
    756 	 */
    757 
    758 	wlabel = rs->sc_flags & (RAIDF_WLABEL|RAIDF_LABELLING);
    759 	if (DISKPART(bp->b_dev) != RAW_PART)
    760 		if (bounds_check_with_label(bp, lp, wlabel) <= 0) {
    761 			db1_printf(("Bounds check failed!!:%d %d\n",
    762 			       (int)bp->b_blkno,(int)wlabel));
    763 			biodone(bp);
    764 			return;
    765 		}
    766 
    767 	s = splbio(); /* XXX Needed? */
    768 	db1_printf(("Beginning strategy...\n"));
    769 
    770 	bp->b_resid = 0;
    771 	bp->b_error = rf_DoAccessKernel(raidPtrs[raidID], bp,
    772 					NULL, NULL, NULL);
    773 	if (bp->b_error) {
    774 		bp->b_flags |= B_ERROR;
    775 		db1_printf(("bp->b_flags HAS B_ERROR SET!!!: %d\n",
    776 			    bp->b_error));
    777 	}
    778 	splx(s);
    779 #if 0
    780 	db1_printf(("Strategy exiting: 0x%x 0x%x %d %d\n",
    781 		    bp,bp->b_data,
    782 		    (int)bp->b_bcount,(int)bp->b_resid));
    783 #endif
    784 }
    785 
    786 /* ARGSUSED */
    787 int
    788 raidread(dev, uio, flags)
    789 	dev_t dev;
    790 	struct uio *uio;
    791 	int flags;
    792 {
    793 	int unit = raidunit(dev);
    794 	struct raid_softc *rs;
    795 	int result;
    796 	int part;
    797 
    798 	if (unit >= numraid)
    799 		return (ENXIO);
    800 	rs = &raid_softc[unit];
    801 
    802 	if ((rs->sc_flags & RAIDF_INITED) == 0)
    803 		return (ENXIO);
    804 	part = DISKPART(dev);
    805 
    806 	db1_printf(("raidread: unit: %d partition: %d\n",unit,part));
    807 
    808 #if 0
    809 	return (physio(raidstrategy, NULL, dev, B_READ, minphys, uio));
    810 #endif
    811 	result=physio(raidstrategy, NULL, dev, B_READ, minphys, uio);
    812 	db1_printf(("raidread done.  Result is %d %d\n",
    813 		    result,uio->uio_resid));
    814 	return(result);
    815 
    816 }
    817 
    818 /* ARGSUSED */
    819 int
    820 raidwrite(dev, uio, flags)
    821 	dev_t dev;
    822 	struct uio *uio;
    823 	int flags;
    824 {
    825 	int unit = raidunit(dev);
    826 	struct raid_softc *rs;
    827 
    828 	if (unit >= numraid)
    829 		return (ENXIO);
    830 	rs = &raid_softc[unit];
    831 
    832 	if ((rs->sc_flags & RAIDF_INITED) == 0)
    833 		return (ENXIO);
    834 	db1_printf(("raidwrite\n"));
    835 	return (physio(raidstrategy, NULL, dev, B_WRITE, minphys, uio));
    836 
    837 
    838 }
    839 
    840 int
    841 raidioctl(dev, cmd, data, flag, p)
    842 	dev_t dev;
    843 	u_long cmd;
    844 	caddr_t data;
    845 	int flag;
    846 	struct proc *p;
    847 {
    848 	int unit = raidunit(dev);
    849 	int error = 0;
    850 	int part, pmask;
    851 	struct raid_softc *rs;
    852 #if 0
    853 	int r,c;
    854 #endif
    855 	/* 	struct raid_ioctl *ccio = (struct ccd_ioctl *)data; */
    856 
    857 	/* 	struct ccdbuf *cbp; */
    858 	/* 	struct raidbuf *raidbp; */
    859 	RF_Config_t *k_cfg, *u_cfg;
    860 	u_char *specific_buf;
    861 	int retcode = 0;
    862 
    863 	int row;
    864 	struct rf_recon_req *rrcopy, *rr;
    865 #if 0
    866 	int nbytes, spl, rw, row;
    867 	struct rf_test_acc *ta;
    868 	struct buf *bp;
    869 	RF_SparetWait_t *waitreq;
    870 	struct rf_test_acc *ta_p, *ta_copy;
    871 #endif
    872 
    873 	if (unit >= numraid)
    874 		return (ENXIO);
    875 	rs = &raid_softc[unit];
    876 
    877 	db1_printf(("raidioctl: %d %d %d %d\n",(int)dev,
    878 		    (int)DISKPART(dev),(int)unit,(int)cmd));
    879 
    880 	/* Must be open for writes for these commands... */
    881 	switch (cmd) {
    882 	case DIOCSDINFO:
    883 	case DIOCWDINFO:
    884 	case DIOCWLABEL:
    885 		if ((flag & FWRITE) == 0)
    886 			return (EBADF);
    887 	}
    888 
    889 	/* Must be initialized for these... */
    890 	switch (cmd) {
    891 	case DIOCGDINFO:
    892 	case DIOCSDINFO:
    893 	case DIOCWDINFO:
    894 	case DIOCGPART:
    895 	case DIOCWLABEL:
    896 	case DIOCGDEFLABEL:
    897 	case RAIDFRAME_SHUTDOWN:
    898 	case RAIDFRAME_REWRITEPARITY:
    899 	case RAIDFRAME_GET_INFO:
    900 	case RAIDFRAME_RESET_ACCTOTALS:
    901 	case RAIDFRAME_GET_ACCTOTALS:
    902 	case RAIDFRAME_KEEP_ACCTOTALS:
    903 	case RAIDFRAME_GET_SIZE:
    904 	case RAIDFRAME_FAIL_DISK:
    905 	case RAIDFRAME_COPYBACK:
    906 	case RAIDFRAME_CHECKRECON:
    907 		if ((rs->sc_flags & RAIDF_INITED) == 0)
    908 			return (ENXIO);
    909 	}
    910 
    911 	switch (cmd) {
    912 
    913 
    914 		/* configure the system */
    915 	case RAIDFRAME_CONFIGURE:
    916 
    917 		db3_printf(("rf_ioctl: RAIDFRAME_CONFIGURE\n"));
    918 		/* copy-in the configuration information */
    919 		/* data points to a pointer to the configuration structure */
    920 		u_cfg = *((RF_Config_t **) data);
    921 		RF_Malloc(k_cfg,sizeof(RF_Config_t),(RF_Config_t *));
    922 		if (k_cfg == NULL) {
    923 			db3_printf(("rf_ioctl: ENOMEM for config. Code is %d\n", retcode));
    924 			return(ENOMEM);
    925 		}
    926 		retcode = copyin((caddr_t) u_cfg, (caddr_t) k_cfg,
    927 				 sizeof(RF_Config_t));
    928 		if (retcode) {
    929 			db3_printf(("rf_ioctl: retcode=%d copyin.1\n",
    930 				    retcode));
    931 			return(retcode);
    932 		}
    933 
    934 		/* allocate a buffer for the layout-specific data,
    935 		   and copy it in */
    936 		if (k_cfg->layoutSpecificSize) {
    937 			if (k_cfg->layoutSpecificSize > 10000) {
    938 				/* sanity check */
    939 				db3_printf(("rf_ioctl: EINVAL %d\n", retcode));
    940 				return(EINVAL);
    941 			}
    942 			RF_Malloc(specific_buf,k_cfg->layoutSpecificSize,
    943 				  (u_char *));
    944 			if (specific_buf == NULL) {
    945 				RF_Free(k_cfg,sizeof(RF_Config_t));
    946 				db3_printf(("rf_ioctl: ENOMEM %d\n", retcode));
    947 				return(ENOMEM);
    948 			}
    949 			retcode = copyin(k_cfg->layoutSpecific,
    950 					 (caddr_t) specific_buf,
    951 					 k_cfg->layoutSpecificSize);
    952 			if (retcode) {
    953 				db3_printf(("rf_ioctl: retcode=%d copyin.2\n",
    954 					    retcode));
    955 				return(retcode);
    956 			}
    957 		} else specific_buf = NULL;
    958 		k_cfg->layoutSpecific = specific_buf;
    959 
    960 		/* should do some kind of sanity check on the configuration.
    961 		   Store the sum of all the bytes in the last byte?
    962 		   */
    963 
    964 #if 0
    965 		db1_printf(("Considering configuring the system.:%d 0x%x\n",
    966 			    unit,p));
    967 #endif
    968 
    969 		/* We need the pointer to this a little deeper, so
    970 		   stash it here... */
    971 
    972 		raidPtrs[unit]->proc = p;
    973 
    974 		/* configure the system */
    975 		rf_pending_testaccs = 0;
    976 
    977 
    978 		raidPtrs[unit]->raidid = unit;
    979 		retcode = rf_Configure(raidPtrs[unit], k_cfg);
    980 
    981 
    982 		if (retcode == 0) {
    983 			retcode = raidinit(dev, raidPtrs[unit],unit);
    984 		}
    985 
    986 		/* free the buffers.  No return code here. */
    987 		if (k_cfg->layoutSpecificSize) {
    988 			RF_Free(specific_buf,k_cfg->layoutSpecificSize);
    989 		}
    990 		RF_Free(k_cfg,sizeof(RF_Config_t));
    991 
    992 		db3_printf(("rf_ioctl: retcode=%d RAIDFRAME_CONFIGURE\n",
    993 			    retcode));
    994 		return(retcode);
    995 
    996 		/* shutdown the system */
    997 	case RAIDFRAME_SHUTDOWN:
    998 
    999 		if ((error = raidlock(rs)) != 0)
   1000 			return(error);
   1001 
   1002 		/*
   1003 		 * If somebody has a partition mounted, we shouldn't
   1004 		 * shutdown.
   1005 		 */
   1006 
   1007 		part = DISKPART(dev);
   1008 		pmask = (1 << part);
   1009                 if ((rs->sc_dkdev.dk_openmask & ~pmask) ||
   1010                     ((rs->sc_dkdev.dk_bopenmask & pmask) &&
   1011 		     (rs->sc_dkdev.dk_copenmask & pmask))) {
   1012                         raidunlock(rs);
   1013                         return (EBUSY);
   1014                 }
   1015 
   1016 		/* the intention here was to disallow shutdowns while
   1017 		   raidframe is mounted, but it doesn't work because the
   1018 		   shutdown ioctl calls rf_open
   1019 		   */
   1020 		if (rf_pending_testaccs > 0) {
   1021 			printf("RAIDFRAME:  Can't shutdown because there are %d pending test accs\n",
   1022 			       rf_pending_testaccs);
   1023 			return(EINVAL);
   1024 		}
   1025 		if (rf_debugKernelAccess) {
   1026 			printf("call shutdown\n");
   1027 		}
   1028 		raidPtrs[unit]->proc = p; /* XXX  necessary evil */
   1029 		retcode = rf_Shutdown(raidPtrs[unit]);
   1030 
   1031 		db1_printf(("Done main shutdown\n"));
   1032 
   1033 		pool_destroy(&rs->sc_cbufpool);
   1034 		db1_printf(("Done freeing component buffer freelist\n"));
   1035 
   1036 		/* It's no longer initialized... */
   1037 		rs->sc_flags &= ~RAIDF_INITED;
   1038 
   1039                 /* Detach the disk. */
   1040                 disk_detach(&rs->sc_dkdev);
   1041 
   1042 		raidunlock(rs);
   1043 
   1044 		return(retcode);
   1045 
   1046 		/* initialize all parity */
   1047 	case RAIDFRAME_REWRITEPARITY:
   1048 
   1049 		if (raidPtrs[unit]->Layout.map->faultsTolerated == 0)
   1050 			return(EINVAL);
   1051 		/* borrow the thread of the requesting process */
   1052 		raidPtrs[unit]->proc = p; /* Blah... :-p GO */
   1053 		retcode = rf_RewriteParity(raidPtrs[unit]);
   1054 		/* return I/O Error if the parity rewrite fails */
   1055 
   1056 		if (retcode)
   1057 			retcode = EIO;
   1058 		return(retcode);
   1059 
   1060 		/* issue a test-unit-ready through raidframe to the
   1061 		   indicated device */
   1062 #if 0 /* XXX not supported yet (ever?) */
   1063 	case RAIDFRAME_TUR:
   1064 		/* debug only */
   1065 		retcode = rf_SCSI_DoTUR(0, 0, 0, 0, *(dev_t *) data);
   1066 		return(retcode);
   1067 #endif
   1068 	case RAIDFRAME_GET_INFO:
   1069 		{
   1070 			RF_Raid_t *raid = raidPtrs[unit];
   1071 			RF_DeviceConfig_t *cfg, **ucfgp;
   1072 			int i, j, d;
   1073 
   1074 			if (!raid->valid)
   1075 				return(ENODEV);
   1076 			ucfgp = (RF_DeviceConfig_t **)data;
   1077 			RF_Malloc(cfg,sizeof(RF_DeviceConfig_t),
   1078 				  (RF_DeviceConfig_t *));
   1079 			if (cfg == NULL)
   1080 				return(ENOMEM);
   1081 			bzero((char *)cfg, sizeof(RF_DeviceConfig_t));
   1082 			cfg->rows = raid->numRow;
   1083 			cfg->cols = raid->numCol;
   1084 			cfg->ndevs = raid->numRow * raid->numCol;
   1085 			if (cfg->ndevs >= RF_MAX_DISKS) {
   1086 				cfg->ndevs = 0;
   1087 				return(ENOMEM);
   1088 			}
   1089 			cfg->nspares = raid->numSpare;
   1090 			if (cfg->nspares >= RF_MAX_DISKS) {
   1091 				cfg->nspares = 0;
   1092 				return(ENOMEM);
   1093 			}
   1094 			cfg->maxqdepth = raid->maxQueueDepth;
   1095 			d = 0;
   1096 			for(i=0;i<cfg->rows;i++) {
   1097 				for(j=0;j<cfg->cols;j++) {
   1098 					cfg->devs[d] = raid->Disks[i][j];
   1099 					d++;
   1100 				}
   1101 			}
   1102 			for(j=cfg->cols,i=0;i<cfg->nspares;i++,j++) {
   1103 				cfg->spares[i] = raid->Disks[0][j];
   1104 			}
   1105 			retcode = copyout((caddr_t)cfg, (caddr_t)*ucfgp,
   1106 					  sizeof(RF_DeviceConfig_t));
   1107 			RF_Free(cfg,sizeof(RF_DeviceConfig_t));
   1108 
   1109 			return(retcode);
   1110 		}
   1111 	break;
   1112 
   1113 	case RAIDFRAME_RESET_ACCTOTALS:
   1114 		{
   1115 			RF_Raid_t *raid = raidPtrs[unit];
   1116 
   1117 			bzero(&raid->acc_totals, sizeof(raid->acc_totals));
   1118 			return(0);
   1119 		}
   1120 	break;
   1121 
   1122 	case RAIDFRAME_GET_ACCTOTALS:
   1123 		{
   1124 			RF_AccTotals_t *totals = (RF_AccTotals_t *)data;
   1125 			RF_Raid_t *raid = raidPtrs[unit];
   1126 
   1127 			*totals = raid->acc_totals;
   1128 			return(0);
   1129 		}
   1130 	break;
   1131 
   1132 	case RAIDFRAME_KEEP_ACCTOTALS:
   1133 		{
   1134 			RF_Raid_t *raid = raidPtrs[unit];
   1135 			int *keep = (int *)data;
   1136 
   1137 			raid->keep_acc_totals = *keep;
   1138 			return(0);
   1139 		}
   1140 	break;
   1141 
   1142 	case RAIDFRAME_GET_SIZE:
   1143 		*(int *) data = raidPtrs[unit]->totalSectors;
   1144 		return(0);
   1145 
   1146 #define RAIDFRAME_RECON 1
   1147 		/* XXX The above should probably be set somewhere else!! GO */
   1148 #if RAIDFRAME_RECON > 0
   1149 
   1150 		/* fail a disk & optionally start reconstruction */
   1151 	case RAIDFRAME_FAIL_DISK:
   1152 		rr = (struct rf_recon_req *) data;
   1153 
   1154 		if (rr->row < 0 || rr->row >= raidPtrs[unit]->numRow
   1155 		    || rr->col < 0 || rr->col >= raidPtrs[unit]->numCol)
   1156 			return(EINVAL);
   1157 
   1158 		printf("Failing the disk: row: %d col: %d\n",rr->row,rr->col);
   1159 
   1160 		/* make a copy of the recon request so that we don't
   1161 		   rely on the user's buffer */
   1162 		RF_Malloc(rrcopy, sizeof(*rrcopy), (struct rf_recon_req *));
   1163 		bcopy(rr, rrcopy, sizeof(*rr));
   1164 		rrcopy->raidPtr = (void *) raidPtrs[unit];
   1165 
   1166 		LOCK_RECON_Q_MUTEX();
   1167 		rrcopy->next = recon_queue;
   1168 		recon_queue = rrcopy;
   1169 		wakeup(&recon_queue);
   1170 		UNLOCK_RECON_Q_MUTEX();
   1171 
   1172 		return(0);
   1173 
   1174 		/* invoke a copyback operation after recon on whatever
   1175 		   disk needs it, if any */
   1176 	case RAIDFRAME_COPYBACK:
   1177 		/* borrow the current thread to get this done */
   1178 		raidPtrs[unit]->proc = p; /* ICK.. but needed :-p  GO */
   1179 		rf_CopybackReconstructedData(raidPtrs[unit]);
   1180 		return(0);
   1181 
   1182 		/* return the percentage completion of reconstruction */
   1183 	case RAIDFRAME_CHECKRECON:
   1184 		row = *(int *) data;
   1185 		if (row < 0 || row >= raidPtrs[unit]->numRow)
   1186 			return(EINVAL);
   1187 		if (raidPtrs[unit]->status[row] != rf_rs_reconstructing)
   1188 			*(int *) data = 100;
   1189 		else
   1190 			*(int *) data = raidPtrs[unit]->reconControl[row]->percentComplete;
   1191 		return(0);
   1192 
   1193 		/* the sparetable daemon calls this to wait for the
   1194 		   kernel to need a spare table.
   1195 		   * this ioctl does not return until a spare table is needed.
   1196 		   * XXX -- calling mpsleep here in the ioctl code is almost
   1197 		   certainly wrong and evil. -- XXX
   1198 		   * XXX -- I should either compute the spare table in the
   1199 		   kernel, or have a different -- XXX
   1200 		   * XXX -- interface (a different character device) for
   1201 		   delivering the table          -- XXX
   1202 		  */
   1203 #if 0
   1204 	case RAIDFRAME_SPARET_WAIT:
   1205 		RF_LOCK_MUTEX(rf_sparet_wait_mutex);
   1206 		while (!rf_sparet_wait_queue) mpsleep(&rf_sparet_wait_queue, (PZERO+1)|PCATCH, "sparet wait", 0, (void *) simple_lock_addr(rf_sparet_wait_mutex), MS_LOCK_SIMPLE);
   1207 		waitreq = rf_sparet_wait_queue;
   1208 		rf_sparet_wait_queue = rf_sparet_wait_queue->next;
   1209 		RF_UNLOCK_MUTEX(rf_sparet_wait_mutex);
   1210 
   1211 		*((RF_SparetWait_t *) data) = *waitreq;              /* structure assignment */
   1212 
   1213 		RF_Free(waitreq, sizeof(*waitreq));
   1214 		return(0);
   1215 
   1216 
   1217 		/* wakes up a process waiting on SPARET_WAIT and puts an
   1218 		   error code in it that will cause the dameon to exit */
   1219 	case RAIDFRAME_ABORT_SPARET_WAIT:
   1220 		RF_Malloc(waitreq, sizeof(*waitreq), (RF_SparetWait_t *));
   1221 		waitreq->fcol = -1;
   1222 		RF_LOCK_MUTEX(rf_sparet_wait_mutex);
   1223 		waitreq->next = rf_sparet_wait_queue;
   1224 		rf_sparet_wait_queue = waitreq;
   1225 		RF_UNLOCK_MUTEX(rf_sparet_wait_mutex);
   1226 		wakeup(&rf_sparet_wait_queue);
   1227 		return(0);
   1228 
   1229 		/* used by the spare table daemon to deliver a spare table
   1230 		   into the kernel */
   1231 	case RAIDFRAME_SEND_SPARET:
   1232 
   1233 		/* install the spare table */
   1234 		retcode = rf_SetSpareTable(raidPtrs[unit],*(void **) data);
   1235 
   1236 		/* respond to the requestor.  the return status of the
   1237 		   spare table installation is passed in the "fcol" field */
   1238 		RF_Malloc(waitreq, sizeof(*waitreq), (RF_SparetWait_t *));
   1239 		waitreq->fcol = retcode;
   1240 		RF_LOCK_MUTEX(rf_sparet_wait_mutex);
   1241 		waitreq->next = rf_sparet_resp_queue;
   1242 		rf_sparet_resp_queue = waitreq;
   1243 		wakeup(&rf_sparet_resp_queue);
   1244 		RF_UNLOCK_MUTEX(rf_sparet_wait_mutex);
   1245 
   1246 		return(retcode);
   1247 #endif
   1248 
   1249 
   1250 #endif   /* RAIDFRAME_RECON > 0 */
   1251 
   1252 	default:  break;               /* fall through to the os-specific code below */
   1253 
   1254 	}
   1255 
   1256 	if (!raidPtrs[unit]->valid)
   1257 		return(EINVAL);
   1258 
   1259 	/*
   1260 	 * Add support for "regular" device ioctls here.
   1261 	 */
   1262 
   1263 	switch (cmd) {
   1264 	case DIOCGDINFO:
   1265 		db1_printf(("DIOCGDINFO %d %d\n",(int)dev,(int)DISKPART(dev)));
   1266 		*(struct disklabel *)data = *(rs->sc_dkdev.dk_label);
   1267 		break;
   1268 
   1269 	case DIOCGPART:
   1270 		db1_printf(("DIOCGPART: %d %d\n",(int)dev,(int)DISKPART(dev)));
   1271 		((struct partinfo *)data)->disklab = rs->sc_dkdev.dk_label;
   1272 		((struct partinfo *)data)->part =
   1273 		    &rs->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
   1274 		break;
   1275 
   1276 	case DIOCWDINFO:
   1277 		db1_printf(("DIOCWDINFO\n"));
   1278 	case DIOCSDINFO:
   1279 		db1_printf(("DIOCSDINFO\n"));
   1280 		if ((error = raidlock(rs)) != 0)
   1281 			return (error);
   1282 
   1283 		rs->sc_flags |= RAIDF_LABELLING;
   1284 
   1285 		error = setdisklabel(rs->sc_dkdev.dk_label,
   1286 		    (struct disklabel *)data, 0, rs->sc_dkdev.dk_cpulabel);
   1287 		if (error == 0) {
   1288 			if (cmd == DIOCWDINFO)
   1289 				error = writedisklabel(RAIDLABELDEV(dev),
   1290 				    raidstrategy, rs->sc_dkdev.dk_label,
   1291 				    rs->sc_dkdev.dk_cpulabel);
   1292 		}
   1293 
   1294 		rs->sc_flags &= ~RAIDF_LABELLING;
   1295 
   1296 		raidunlock(rs);
   1297 
   1298 		if (error)
   1299 			return (error);
   1300 		break;
   1301 
   1302 	case DIOCWLABEL:
   1303 		db1_printf(("DIOCWLABEL\n"));
   1304 		if (*(int *)data != 0)
   1305 			rs->sc_flags |= RAIDF_WLABEL;
   1306 		else
   1307 			rs->sc_flags &= ~RAIDF_WLABEL;
   1308 		break;
   1309 
   1310 	case DIOCGDEFLABEL:
   1311 		db1_printf(("DIOCGDEFLABEL\n"));
   1312 		raidgetdefaultlabel(raidPtrs[unit], rs,
   1313 					 (struct disklabel *)data);
   1314 		break;
   1315 
   1316 	default:
   1317 		retcode = ENOTTY; /* XXXX ?? OR EINVAL ? */
   1318 	}
   1319 	return(retcode);
   1320 
   1321 }
   1322 
   1323 
   1324 /* raidinit -- complete the rest of the initialization for the
   1325    RAIDframe device.  */
   1326 
   1327 
   1328 static int
   1329 raidinit(dev, raidPtr,unit)
   1330 	dev_t dev;
   1331 	RF_Raid_t *raidPtr;
   1332 	int unit;
   1333 {
   1334 	int retcode;
   1335 	/* 	int ix; */
   1336 	/* 	struct raidbuf *raidbp; */
   1337 	struct raid_softc *rs;
   1338 
   1339 	retcode = 0;
   1340 
   1341 	rs = &raid_softc[unit];
   1342 	pool_init(&rs->sc_cbufpool, sizeof(struct raidbuf), 0,
   1343 		  0, 0, "raidpl", 0, NULL, NULL, M_RAIDFRAME);
   1344 
   1345 
   1346 	/* XXX should check return code first... */
   1347 	rs->sc_flags |= RAIDF_INITED;
   1348 
   1349 	sprintf(rs->sc_xname, "raid%d", unit); /* XXX doesn't check bounds.*/
   1350 
   1351 	rs->sc_dkdev.dk_name = rs->sc_xname;
   1352 	/* disk_attach actually creates space for the CPU disklabel, among
   1353 	   other things, so it's critical to call this *BEFORE* we
   1354 	   try putzing with disklabels. */
   1355 	disk_attach(&rs->sc_dkdev);
   1356 
   1357 	/* XXX There may be a weird interaction here between this, and
   1358 	   protectedSectors, as used in RAIDframe.  */
   1359 	rs->sc_size = raidPtr->totalSectors;
   1360 	rs->sc_dev = dev;
   1361 	return(retcode);
   1362 }
   1363 
   1364 
   1365 /*********************************************************
   1366  *
   1367  * initialization code called at boot time (startup.c)
   1368  *
   1369  ********************************************************/
   1370 int rf_boot()
   1371 {
   1372   int i, rc;
   1373 
   1374   rc = rf_mutex_init(&rf_sparet_wait_mutex);
   1375   if (rc) {
   1376     RF_PANIC();
   1377   }
   1378   rc = rf_mutex_init(&rf_async_done_q_mutex);
   1379   if (rc) {
   1380     RF_PANIC();
   1381   }
   1382   rf_sparet_wait_queue = rf_sparet_resp_queue = NULL;
   1383   recon_queue = NULL;
   1384   rf_async_done_qh = rf_async_done_qt = NULL;
   1385   for (i=0; i<numraid; i++)
   1386     raidPtrs[i] = NULL;
   1387   rc = rf_BootRaidframe();
   1388   if (rc == 0)
   1389     printf("Kernelized RAIDframe activated\n");
   1390   else
   1391     rf_kbooted = RFK_BOOT_BAD;
   1392   return(rc);
   1393 }
   1394 
   1395 /*
   1396  * This kernel thread never exits.  It is created once, and persists
   1397  * until the system reboots.
   1398  */
   1399 void rf_ReconKernelThread()
   1400 {
   1401   struct rf_recon_req *req;
   1402   int s;
   1403 
   1404   /* XXX not sure what spl() level we should be at here... probably splbio() */
   1405   s=splbio();
   1406 
   1407   while (1) {
   1408     /* grab the next reconstruction request from the queue */
   1409     LOCK_RECON_Q_MUTEX();
   1410     while (!recon_queue) {
   1411 	    UNLOCK_RECON_Q_MUTEX();
   1412 	    tsleep(&recon_queue, PRIBIO | PCATCH, "raidframe recon", 0);
   1413 	    LOCK_RECON_Q_MUTEX();
   1414     }
   1415     req = recon_queue;
   1416     recon_queue = recon_queue->next;
   1417     UNLOCK_RECON_Q_MUTEX();
   1418 
   1419     /*
   1420      * If flags specifies that we should start recon, this call
   1421      * will not return until reconstruction completes, fails, or is aborted.
   1422      */
   1423     rf_FailDisk((RF_Raid_t *) req->raidPtr, req->row, req->col,
   1424         ((req->flags&RF_FDFLAGS_RECON) ? 1 : 0));
   1425 
   1426     RF_Free(req, sizeof(*req));
   1427   }
   1428 }
   1429 /* wake up the daemon & tell it to get us a spare table
   1430  * XXX
   1431  * the entries in the queues should be tagged with the raidPtr
   1432  * so that in the extremely rare case that two recons happen at once, we know for
   1433  * which device were requesting a spare table
   1434  * XXX
   1435  */
   1436 int rf_GetSpareTableFromDaemon(req)
   1437   RF_SparetWait_t  *req;
   1438 {
   1439   int retcode;
   1440 
   1441   RF_LOCK_MUTEX(rf_sparet_wait_mutex);
   1442   req->next = rf_sparet_wait_queue;
   1443   rf_sparet_wait_queue = req;
   1444   wakeup(&rf_sparet_wait_queue);
   1445 
   1446   /* mpsleep unlocks the mutex */
   1447   while (!rf_sparet_resp_queue) {
   1448 	  tsleep(&rf_sparet_resp_queue, PRIBIO | PCATCH,
   1449 		 "raidframe getsparetable", 0);
   1450 #if 0
   1451 	  mpsleep(&rf_sparet_resp_queue, PZERO, "sparet resp", 0, (void *) simple_lock_addr(rf_sparet_wait_mutex), MS_LOCK_SIMPLE);
   1452 #endif
   1453   }
   1454   req = rf_sparet_resp_queue;
   1455   rf_sparet_resp_queue = req->next;
   1456   RF_UNLOCK_MUTEX(rf_sparet_wait_mutex);
   1457 
   1458   retcode = req->fcol;
   1459   RF_Free(req, sizeof(*req));   /* this is not the same req as we alloc'd */
   1460   return(retcode);
   1461 }
   1462 
   1463 /* a wrapper around rf_DoAccess that extracts appropriate info from the bp & passes it down.
   1464  * any calls originating in the kernel must use non-blocking I/O
   1465  * do some extra sanity checking to return "appropriate" error values for
   1466  * certain conditions (to make some standard utilities work)
   1467  */
   1468 int rf_DoAccessKernel(raidPtr, bp, flags, cbFunc, cbArg)
   1469   RF_Raid_t              *raidPtr;
   1470   struct buf             *bp;
   1471   RF_RaidAccessFlags_t    flags;
   1472   void                  (*cbFunc)(struct buf *);
   1473   void                   *cbArg;
   1474 {
   1475 	RF_SectorCount_t num_blocks, pb, sum;
   1476 	RF_RaidAddr_t raid_addr;
   1477 	int retcode;
   1478 	struct partition *pp;
   1479 	daddr_t blocknum;
   1480 	int unit;
   1481 	struct raid_softc *rs;
   1482 
   1483 	/* XXX The dev_t used here should be for /dev/[r]raid* !!! */
   1484 
   1485 	unit = raidPtr->raidid;
   1486 	rs = &raid_softc[unit];
   1487 
   1488 	/* Ok, for the bp we have here, bp->b_blkno is relative to the
   1489 	   partition.. Need to make it absolute to the underlying
   1490 	   device.. */
   1491 
   1492 	blocknum = bp->b_blkno;
   1493 	if (DISKPART(bp->b_dev) != RAW_PART) {
   1494 		pp = &rs->sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
   1495 		blocknum += pp->p_offset;
   1496 		db1_printf(("updated: %d %d\n",DISKPART(bp->b_dev),
   1497 			    pp->p_offset));
   1498 	} else {
   1499 		db1_printf(("Is raw..\n"));
   1500 	}
   1501 	db1_printf(("Blocks: %d, %d\n", (int) bp->b_blkno, (int) blocknum));
   1502 
   1503 	db1_printf(("bp->b_bcount = %d\n",(int)bp->b_bcount));
   1504 	db1_printf(("bp->b_resid = %d\n",(int)bp->b_resid));
   1505 
   1506 	/* *THIS* is where we adjust what block we're going to... but
   1507 	   DO NOT TOUCH bp->b_blkno!!! */
   1508 	raid_addr = blocknum;
   1509 
   1510 	num_blocks = bp->b_bcount >> raidPtr->logBytesPerSector;
   1511 	pb = (bp->b_bcount&raidPtr->sectorMask) ? 1 : 0;
   1512 	sum = raid_addr + num_blocks + pb;
   1513 	if (1 || rf_debugKernelAccess) {
   1514 		db1_printf(("raid_addr=%d sum=%d num_blocks=%d(+%d) (%d)\n",
   1515 			    (int)raid_addr, (int)sum,(int)num_blocks,
   1516 			    (int)pb,(int)bp->b_resid));
   1517 	}
   1518 
   1519 
   1520 	if ((sum > raidPtr->totalSectors) || (sum < raid_addr)
   1521 			|| (sum < num_blocks) || (sum < pb))
   1522 	{
   1523 		bp->b_error = ENOSPC;
   1524 		bp->b_flags |= B_ERROR;
   1525 		bp->b_resid = bp->b_bcount;
   1526 		biodone(bp);
   1527 		return(bp->b_error);
   1528 	}
   1529 
   1530 	/*
   1531 	 * XXX rf_DoAccess() should do this, not just DoAccessKernel()
   1532 	 */
   1533 
   1534 	if (bp->b_bcount & raidPtr->sectorMask) {
   1535 		bp->b_error = EINVAL;
   1536 		bp->b_flags |= B_ERROR;
   1537 		bp->b_resid = bp->b_bcount;
   1538 		biodone(bp);
   1539 		return(bp->b_error);
   1540 	}
   1541 	db1_printf(("Calling DoAccess..\n"));
   1542 
   1543 	/* don't ever condition on bp->b_flags & B_WRITE.
   1544 	   always condition on B_READ instead */
   1545 	retcode = rf_DoAccess(raidPtr, (bp->b_flags & B_READ) ?
   1546 			      RF_IO_TYPE_READ : RF_IO_TYPE_WRITE,
   1547 			      0, raid_addr, num_blocks, bp->b_un.b_addr,
   1548 			      bp, NULL, NULL, RF_DAG_NONBLOCKING_IO|flags,
   1549 			      NULL, cbFunc, cbArg);
   1550 #if 0
   1551 	db1_printf(("After call to DoAccess: 0x%x 0x%x %d\n",bp,
   1552 	       bp->b_data,(int)bp->b_resid));
   1553 #endif
   1554 	return(retcode);
   1555 }
   1556 
   1557 /* invoke an I/O from kernel mode.  Disk queue should be locked upon entry */
   1558 
   1559 int rf_DispatchKernelIO(queue, req)
   1560 	RF_DiskQueue_t      *queue;
   1561 	RF_DiskQueueData_t  *req;
   1562 {
   1563 	int op = (req->type == RF_IO_TYPE_READ) ? B_READ : B_WRITE;
   1564 	struct buf *bp;
   1565 	struct raidbuf *raidbp=NULL;
   1566 	struct raid_softc *rs;
   1567 	int unit;
   1568 
   1569 	/* XXX along with the vnode, we also need the softc associated with
   1570 	   this device.. */
   1571 
   1572 	req->queue = queue;
   1573 
   1574 	unit = queue->raidPtr->raidid;
   1575 
   1576  	db1_printf(("DispatchKernelIO unit: %d\n",unit));
   1577 
   1578      	if (unit >= numraid) {
   1579 		printf("Invalid unit number: %d %d\n",unit,numraid);
   1580 		panic("Invalid Unit number in rf_DispatchKernelIO\n");
   1581 	}
   1582 
   1583 	rs = &raid_softc[unit];
   1584 
   1585 	/* XXX is this the right place? */
   1586 	disk_busy(&rs->sc_dkdev);
   1587 
   1588 	bp = req->bp;
   1589 
   1590 	/*
   1591 	   XXX when there is a physical disk failure, someone is passing
   1592 	   us a buffer that contains old stuff!!  Attempt to deal with
   1593 	   this problem without taking a performance hit...
   1594 	   (not sure where the real bug is.  It's buried in RAIDframe
   1595 	   somewhere) :-(  GO )
   1596 	 */
   1597 
   1598 	if (bp->b_flags & B_ERROR) {
   1599 		bp->b_flags &= ~B_ERROR;
   1600 	}
   1601 	if (bp->b_error!=0) {
   1602 		bp->b_error = 0;
   1603 	}
   1604 
   1605 	raidbp = RAIDGETBUF(rs);
   1606 
   1607 	raidbp->rf_flags = 0; /* XXX not really used anywhere... */
   1608 
   1609 	/*
   1610 	 * context for raidiodone
   1611 	 */
   1612 	raidbp->rf_obp = bp;
   1613 	raidbp->req = req;
   1614 
   1615 	switch (req->type) {
   1616 	case RF_IO_TYPE_NOP:   /* used primarily to unlock a locked queue */
   1617 		/*
   1618 		Dprintf2("rf_DispatchKernelIO: NOP to r %d c %d\n",
   1619 			 queue->row, queue->col);
   1620 			 */
   1621 		/* XXX need to do something extra here.. */
   1622 		/* I'm leaving this in, as I've never actually seen it
   1623 		   used, and I'd like folks to report it... GO */
   1624 		printf(("WAKEUP CALLED\n"));
   1625 		queue->numOutstanding++;
   1626 
   1627 		/* XXX need to glue the original buffer into this??  */
   1628 
   1629 		KernelWakeupFunc(&raidbp->rf_buf);
   1630 		break;
   1631 
   1632 	case RF_IO_TYPE_READ:
   1633 	case RF_IO_TYPE_WRITE:
   1634 
   1635 		if (req->tracerec) {
   1636 			RF_ETIMER_START(req->tracerec->timer);
   1637 		}
   1638 
   1639 
   1640 		InitBP(&raidbp->rf_buf, queue->rf_cinfo->ci_vp,
   1641 		       op | bp->b_flags, queue->rf_cinfo->ci_dev,
   1642 		       req->sectorOffset, req->numSector,
   1643 		       req->buf, KernelWakeupFunc, (void *) req,
   1644 		       queue->raidPtr->logBytesPerSector, req->b_proc);
   1645 
   1646 		if (rf_debugKernelAccess) {
   1647 			db1_printf(("dispatch: bp->b_blkno = %ld\n",
   1648 				    (long) bp->b_blkno));
   1649 		}
   1650 		queue->numOutstanding++;
   1651 		queue->last_deq_sector = req->sectorOffset;
   1652 		/* acc wouldn't have been let in if there were any
   1653 		   pending reqs at any other priority */
   1654 		queue->curPriority = req->priority;
   1655 		/*
   1656 		Dprintf3("rf_DispatchKernelIO: %c to row %d col %d\n",
   1657 			 req->type, queue->row, queue->col);
   1658 			 */
   1659 
   1660 		db1_printf(("Going for %c to unit %d row %d col %d\n",
   1661 		       req->type, unit, queue->row, queue->col));
   1662 		db1_printf(("sector %d count %d (%d bytes) %d\n",
   1663 		       (int) req->sectorOffset, (int) req->numSector,
   1664 		       (int) (req->numSector <<
   1665 			      queue->raidPtr->logBytesPerSector),
   1666 		       (int) queue->raidPtr->logBytesPerSector));
   1667 		if ((raidbp->rf_buf.b_flags & B_READ) == 0) {
   1668 			raidbp->rf_buf.b_vp->v_numoutput++;
   1669 		}
   1670 
   1671 		VOP_STRATEGY(&raidbp->rf_buf);
   1672 
   1673 		break;
   1674 
   1675 	default:
   1676 		panic("bad req->type in rf_DispatchKernelIO");
   1677 	}
   1678 	db1_printf(("Exiting from DispatchKernelIO\n"));
   1679 	return(0);
   1680 }
   1681 
   1682 /* this is the callback function associated with a I/O invoked from
   1683    kernel code.
   1684  */
   1685 static void KernelWakeupFunc(vbp)
   1686   struct buf  *vbp;
   1687 {
   1688   RF_DiskQueueData_t *req = NULL;
   1689   RF_DiskQueue_t *queue;
   1690   struct raidbuf *raidbp = (struct raidbuf *)vbp;
   1691   struct buf *bp;
   1692   struct raid_softc *rs;
   1693   int unit;
   1694   register int s;
   1695 
   1696   s=splbio(); /* XXX */
   1697   db1_printf(("recovering the request queue:\n"));
   1698   req = raidbp->req;
   1699 
   1700   bp = raidbp->rf_obp;
   1701 #if 0
   1702   db1_printf(("bp=0x%x\n",bp));
   1703 #endif
   1704 
   1705   queue = (RF_DiskQueue_t *) req->queue;
   1706 
   1707   if (raidbp->rf_buf.b_flags & B_ERROR) {
   1708 #if 0
   1709 	  printf("Setting bp->b_flags!!! %d\n",raidbp->rf_buf.b_error);
   1710 #endif
   1711 	  bp->b_flags |= B_ERROR;
   1712 	  bp->b_error = raidbp->rf_buf.b_error ?
   1713 		  raidbp->rf_buf.b_error : EIO;
   1714   }
   1715 
   1716 #if 0
   1717   db1_printf(("raidbp->rf_buf.b_bcount=%d\n",(int)raidbp->rf_buf.b_bcount));
   1718   db1_printf(("raidbp->rf_buf.b_bufsize=%d\n",(int)raidbp->rf_buf.b_bufsize));
   1719   db1_printf(("raidbp->rf_buf.b_resid=%d\n",(int)raidbp->rf_buf.b_resid));
   1720   db1_printf(("raidbp->rf_buf.b_data=0x%x\n",raidbp->rf_buf.b_data));
   1721 #endif
   1722 
   1723   /* XXX methinks this could be wrong... */
   1724 #if 1
   1725   bp->b_resid = raidbp->rf_buf.b_resid;
   1726 #endif
   1727 
   1728   if (req->tracerec) {
   1729 	RF_ETIMER_STOP(req->tracerec->timer);
   1730 	RF_ETIMER_EVAL(req->tracerec->timer);
   1731     RF_LOCK_MUTEX(rf_tracing_mutex);
   1732     req->tracerec->diskwait_us += RF_ETIMER_VAL_US(req->tracerec->timer);
   1733     req->tracerec->phys_io_us += RF_ETIMER_VAL_US(req->tracerec->timer);
   1734     req->tracerec->num_phys_ios++;
   1735     RF_UNLOCK_MUTEX(rf_tracing_mutex);
   1736   }
   1737 
   1738   bp->b_bcount = raidbp->rf_buf.b_bcount;/* XXXX ?? */
   1739 
   1740   unit = queue->raidPtr->raidid; /* *Much* simpler :-> */
   1741 
   1742 
   1743   /* XXX Ok, let's get aggressive... If B_ERROR is set, let's go ballistic,
   1744      and mark the component as hosed... */
   1745 #if 1
   1746   if (bp->b_flags&B_ERROR) {
   1747 	  /* Mark the disk as dead */
   1748 	  /* but only mark it once... */
   1749 	  if (queue->raidPtr->Disks[queue->row][queue->col].status ==
   1750 	      rf_ds_optimal) {
   1751 		  printf("raid%d: IO Error.  Marking %s as failed.\n",
   1752 			 unit, queue->raidPtr->Disks[queue->row][queue->col].devname );
   1753 		  queue->raidPtr->Disks[queue->row][queue->col].status =
   1754 			  rf_ds_failed;
   1755 		  queue->raidPtr->status[queue->row] = rf_rs_degraded;
   1756 		  queue->raidPtr->numFailures++;
   1757 	  } else {  /* Disk is already dead... */
   1758 		  /*  printf("Disk already marked as dead!\n"); */
   1759 	  }
   1760 
   1761   }
   1762 #endif
   1763 
   1764   rs = &raid_softc[unit];
   1765   RAIDPUTBUF(rs,raidbp);
   1766 
   1767 
   1768   if (bp->b_resid==0) {
   1769 	  db1_printf(("Disk is no longer busy for this buffer... %d %ld %ld\n",
   1770 		 unit, bp->b_resid, bp->b_bcount));
   1771 	  /* XXX is this the right place for a disk_unbusy()??!??!?!? */
   1772 	  disk_unbusy(&rs->sc_dkdev, (bp->b_bcount - bp->b_resid));
   1773   } else {
   1774 	  db1_printf(("b_resid is still %ld\n",bp->b_resid));
   1775   }
   1776 
   1777   rf_DiskIOComplete(queue, req, (bp->b_flags & B_ERROR) ? 1 : 0);
   1778   (req->CompleteFunc)(req->argument, (bp->b_flags & B_ERROR) ? 1 : 0);
   1779   /*   printf("Exiting KernelWakeupFunc\n"); */
   1780 
   1781   splx(s); /* XXX */
   1782 }
   1783 
   1784 
   1785 
   1786 /*
   1787  * initialize a buf structure for doing an I/O in the kernel.
   1788  */
   1789 static void InitBP(
   1790   struct buf         *bp,
   1791   struct vnode       *b_vp,
   1792   unsigned            rw_flag,
   1793   dev_t               dev,
   1794   RF_SectorNum_t      startSect,
   1795   RF_SectorCount_t    numSect,
   1796   caddr_t             buf,
   1797   void              (*cbFunc)(struct buf *),
   1798   void               *cbArg,
   1799   int                 logBytesPerSector,
   1800   struct proc        *b_proc)
   1801 {
   1802 	/*   bp->b_flags       = B_PHYS | rw_flag; */
   1803 	bp->b_flags       = B_CALL | rw_flag; /* XXX need B_PHYS here too??? */
   1804 	bp->b_bcount      = numSect << logBytesPerSector;
   1805 	bp->b_bufsize     = bp->b_bcount;
   1806 	bp->b_error       = 0;
   1807 	bp->b_dev         = dev;
   1808 	db1_printf(("bp->b_dev is %d\n", dev));
   1809 	bp->b_un.b_addr   = buf;
   1810 #if 0
   1811 	db1_printf(("bp->b_data=0x%x\n",bp->b_data));
   1812 #endif
   1813 
   1814 	bp->b_blkno       = startSect;
   1815 	bp->b_resid       = bp->b_bcount; /* XXX is this right!??!?!! */
   1816 	db1_printf(("b_bcount is: %d\n",(int)bp->b_bcount));
   1817 	if (bp->b_bcount == 0) {
   1818 		panic("bp->b_bcount is zero in InitBP!!\n");
   1819 	}
   1820 	bp->b_proc        = b_proc;
   1821 	bp->b_iodone      = cbFunc;
   1822 	bp->b_vp          = b_vp;
   1823 
   1824 }
   1825 #endif /* KERNEL */
   1826 
   1827 /* Extras... */
   1828 
   1829 unsigned int rpcc()
   1830 {
   1831 	/* XXX no clue what this is supposed to do.. my guess is
   1832 	   that it's supposed to read the CPU cycle counter... */
   1833 	/* 	db1_printf("this is supposed to do something useful too!??\n"); */
   1834 	return(0);
   1835 }
   1836 
   1837 #if 0
   1838 int rf_GetSpareTableFromDaemon(req)
   1839   RF_SparetWait_t  *req;
   1840 {
   1841   int retcode=1;
   1842   printf("This is supposed to do something useful!!\n"); /* XXX */
   1843 
   1844   return(retcode);
   1845 
   1846 }
   1847 #endif
   1848 
   1849 static void
   1850 raidgetdefaultlabel(raidPtr, rs, lp)
   1851 	RF_Raid_t *raidPtr;
   1852 	struct raid_softc *rs;
   1853 	struct disklabel *lp;
   1854 {
   1855 	db1_printf(("Building a default label...\n"));
   1856 	bzero(lp, sizeof(*lp));
   1857 
   1858 	/* fabricate a label... */
   1859 	lp->d_secperunit = raidPtr->totalSectors;
   1860 	lp->d_secsize = raidPtr->bytesPerSector;
   1861 	lp->d_nsectors = 1024 * (1024 / raidPtr->bytesPerSector);
   1862 	lp->d_ntracks = 1;
   1863 	lp->d_ncylinders = raidPtr->totalSectors / lp->d_nsectors;
   1864 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1865 
   1866 	strncpy(lp->d_typename, "raid", sizeof(lp->d_typename));
   1867 	lp->d_type = DTYPE_RAID;
   1868 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   1869 	lp->d_rpm = 3600;
   1870 	lp->d_interleave = 1;
   1871 	lp->d_flags = 0;
   1872 
   1873 	lp->d_partitions[RAW_PART].p_offset = 0;
   1874 	lp->d_partitions[RAW_PART].p_size = raidPtr->totalSectors;
   1875 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1876 	lp->d_npartitions = RAW_PART + 1;
   1877 
   1878 	lp->d_magic = DISKMAGIC;
   1879 	lp->d_magic2 = DISKMAGIC;
   1880 	lp->d_checksum = dkcksum(rs->sc_dkdev.dk_label);
   1881 
   1882 }
   1883 
   1884 /*
   1885  * Read the disklabel from the raid device.  If one is not present, fake one
   1886  * up.
   1887  */
   1888 static void
   1889 raidgetdisklabel(dev)
   1890 	dev_t dev;
   1891 {
   1892 	int unit = raidunit(dev);
   1893 	struct raid_softc *rs = &raid_softc[unit];
   1894 	char *errstring;
   1895 	struct disklabel *lp = rs->sc_dkdev.dk_label;
   1896 	struct cpu_disklabel *clp = rs->sc_dkdev.dk_cpulabel;
   1897 	RF_Raid_t *raidPtr;
   1898 
   1899 	db1_printf(("Getting the disklabel...\n"));
   1900 
   1901 	bzero(clp, sizeof(*clp));
   1902 
   1903 	raidPtr = raidPtrs[unit];
   1904 
   1905 	raidgetdefaultlabel(raidPtr, rs, lp);
   1906 
   1907 	/*
   1908 	 * Call the generic disklabel extraction routine.
   1909 	 */
   1910 	errstring = readdisklabel(RAIDLABELDEV(dev), raidstrategy,
   1911 	    rs->sc_dkdev.dk_label, rs->sc_dkdev.dk_cpulabel);
   1912 	if (errstring)
   1913 		raidmakedisklabel(rs);
   1914 	else {
   1915 		int i;
   1916 		struct partition *pp;
   1917 
   1918 		/*
   1919 		 * Sanity check whether the found disklabel is valid.
   1920 		 *
   1921 		 * This is necessary since total size of the raid device
   1922 		 * may vary when an interleave is changed even though exactly
   1923 		 * same componets are used, and old disklabel may used
   1924 		 * if that is found.
   1925 		 */
   1926 		if (lp->d_secperunit != rs->sc_size)
   1927 			printf("WARNING: %s: "
   1928 			    "total sector size in disklabel (%d) != "
   1929 			    "the size of raid (%d)\n", rs->sc_xname,
   1930 			    lp->d_secperunit, rs->sc_size);
   1931 		for (i = 0; i < lp->d_npartitions; i++) {
   1932 			pp = &lp->d_partitions[i];
   1933 			if (pp->p_offset + pp->p_size > rs->sc_size)
   1934 				printf("WARNING: %s: end of partition `%c' "
   1935 				    "exceeds the size of raid (%d)\n",
   1936 				    rs->sc_xname, 'a' + i, rs->sc_size);
   1937 		}
   1938 	}
   1939 
   1940 }
   1941 
   1942 /*
   1943  * Take care of things one might want to take care of in the event
   1944  * that a disklabel isn't present.
   1945  */
   1946 static void
   1947 raidmakedisklabel(rs)
   1948 	struct raid_softc *rs;
   1949 {
   1950 	struct disklabel *lp = rs->sc_dkdev.dk_label;
   1951 	db1_printf(("Making a label..\n"));
   1952 
   1953 	/*
   1954 	 * For historical reasons, if there's no disklabel present
   1955 	 * the raw partition must be marked FS_BSDFFS.
   1956 	 */
   1957 
   1958 	lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
   1959 
   1960 	strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
   1961 
   1962 	lp->d_checksum = dkcksum(lp);
   1963 }
   1964 
   1965 /*
   1966  * Lookup the provided name in the filesystem.  If the file exists,
   1967  * is a valid block device, and isn't being used by anyone else,
   1968  * set *vpp to the file's vnode.
   1969  * You'll find the original of this in ccd.c
   1970  */
   1971 int
   1972 raidlookup(path, p, vpp)
   1973 	char *path;
   1974 	struct proc *p;
   1975 	struct vnode **vpp;	/* result */
   1976 {
   1977 	struct nameidata nd;
   1978 	struct vnode *vp;
   1979 	struct vattr va;
   1980 	int error;
   1981 
   1982 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, p);
   1983 	if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
   1984 #ifdef DEBUG
   1985 			printf("RAIDframe: vn_open returned %d\n",error);
   1986 #endif
   1987 		return (error);
   1988 	}
   1989 	vp = nd.ni_vp;
   1990 	if (vp->v_usecount > 1) {
   1991 		VOP_UNLOCK(vp, 0);
   1992 		(void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
   1993 		return (EBUSY);
   1994 	}
   1995 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0) {
   1996 		VOP_UNLOCK(vp, 0);
   1997 		(void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
   1998 		return (error);
   1999 	}
   2000 	/* XXX: eventually we should handle VREG, too. */
   2001 	if (va.va_type != VBLK) {
   2002 		VOP_UNLOCK(vp, 0);
   2003 		(void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
   2004 		return (ENOTBLK);
   2005 	}
   2006 	VOP_UNLOCK(vp, 0);
   2007 	*vpp = vp;
   2008 	return (0);
   2009 }
   2010 
   2011 /*
   2012  * Wait interruptibly for an exclusive lock.
   2013  *
   2014  * XXX
   2015  * Several drivers do this; it should be abstracted and made MP-safe.
   2016  * (Hmm... where have we seen this warning before :->  GO )
   2017  */
   2018 static int
   2019 raidlock(rs)
   2020 	struct raid_softc *rs;
   2021 {
   2022 	int error;
   2023 
   2024 	while ((rs->sc_flags & RAIDF_LOCKED) != 0) {
   2025 		rs->sc_flags |= RAIDF_WANTED;
   2026 		if ((error =
   2027 		     tsleep(rs, PRIBIO | PCATCH, "raidlck", 0)) != 0)
   2028 			return (error);
   2029 	}
   2030 	rs->sc_flags |= RAIDF_LOCKED;
   2031 	return (0);
   2032 }
   2033 
   2034 /*
   2035  * Unlock and wake up any waiters.
   2036  */
   2037 static void
   2038 raidunlock(rs)
   2039 	struct raid_softc *rs;
   2040 {
   2041 
   2042 	rs->sc_flags &= ~RAIDF_LOCKED;
   2043 	if ((rs->sc_flags & RAIDF_WANTED) != 0) {
   2044 		rs->sc_flags &= ~RAIDF_WANTED;
   2045 		wakeup(rs);
   2046 	}
   2047 }
   2048