rf_netbsdkintf.c revision 1.186.2.3 1 /* $NetBSD: rf_netbsdkintf.c,v 1.186.2.3 2006/01/11 17:18:00 tron 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) 1990, 1993
40 * The Regents of the University of California. All rights reserved.
41 *
42 * This code is derived from software contributed to Berkeley by
43 * the Systems Programming Group of the University of Utah Computer
44 * Science Department.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * from: Utah $Hdr: cd.c 1.6 90/11/28$
71 *
72 * @(#)cd.c 8.2 (Berkeley) 11/16/93
73 */
74
75 /*
76 * Copyright (c) 1988 University of Utah.
77 *
78 * This code is derived from software contributed to Berkeley by
79 * the Systems Programming Group of the University of Utah Computer
80 * Science Department.
81 *
82 * Redistribution and use in source and binary forms, with or without
83 * modification, are permitted provided that the following conditions
84 * are met:
85 * 1. Redistributions of source code must retain the above copyright
86 * notice, this list of conditions and the following disclaimer.
87 * 2. Redistributions in binary form must reproduce the above copyright
88 * notice, this list of conditions and the following disclaimer in the
89 * documentation and/or other materials provided with the distribution.
90 * 3. All advertising materials mentioning features or use of this software
91 * must display the following acknowledgement:
92 * This product includes software developed by the University of
93 * California, Berkeley and its contributors.
94 * 4. Neither the name of the University nor the names of its contributors
95 * may be used to endorse or promote products derived from this software
96 * without specific prior written permission.
97 *
98 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
99 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
102 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108 * SUCH DAMAGE.
109 *
110 * from: Utah $Hdr: cd.c 1.6 90/11/28$
111 *
112 * @(#)cd.c 8.2 (Berkeley) 11/16/93
113 */
114
115 /*
116 * Copyright (c) 1995 Carnegie-Mellon University.
117 * All rights reserved.
118 *
119 * Authors: Mark Holland, Jim Zelenka
120 *
121 * Permission to use, copy, modify and distribute this software and
122 * its documentation is hereby granted, provided that both the copyright
123 * notice and this permission notice appear in all copies of the
124 * software, derivative works or modified versions, and any portions
125 * thereof, and that both notices appear in supporting documentation.
126 *
127 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
128 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
129 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
130 *
131 * Carnegie Mellon requests users of this software to return to
132 *
133 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
134 * School of Computer Science
135 * Carnegie Mellon University
136 * Pittsburgh PA 15213-3890
137 *
138 * any improvements or extensions that they make and grant Carnegie the
139 * rights to redistribute these changes.
140 */
141
142 /***********************************************************
143 *
144 * rf_kintf.c -- the kernel interface routines for RAIDframe
145 *
146 ***********************************************************/
147
148 #include <sys/cdefs.h>
149 __KERNEL_RCSID(0, "$NetBSD: rf_netbsdkintf.c,v 1.186.2.3 2006/01/11 17:18:00 tron Exp $");
150
151 #include <sys/param.h>
152 #include <sys/errno.h>
153 #include <sys/pool.h>
154 #include <sys/proc.h>
155 #include <sys/queue.h>
156 #include <sys/disk.h>
157 #include <sys/device.h>
158 #include <sys/stat.h>
159 #include <sys/ioctl.h>
160 #include <sys/fcntl.h>
161 #include <sys/systm.h>
162 #include <sys/namei.h>
163 #include <sys/vnode.h>
164 #include <sys/disklabel.h>
165 #include <sys/conf.h>
166 #include <sys/lock.h>
167 #include <sys/buf.h>
168 #include <sys/bufq.h>
169 #include <sys/user.h>
170 #include <sys/reboot.h>
171
172 #include <dev/raidframe/raidframevar.h>
173 #include <dev/raidframe/raidframeio.h>
174 #include "raid.h"
175 #include "opt_raid_autoconfig.h"
176 #include "rf_raid.h"
177 #include "rf_copyback.h"
178 #include "rf_dag.h"
179 #include "rf_dagflags.h"
180 #include "rf_desc.h"
181 #include "rf_diskqueue.h"
182 #include "rf_etimer.h"
183 #include "rf_general.h"
184 #include "rf_kintf.h"
185 #include "rf_options.h"
186 #include "rf_driver.h"
187 #include "rf_parityscan.h"
188 #include "rf_threadstuff.h"
189
190 #ifdef DEBUG
191 int rf_kdebug_level = 0;
192 #define db1_printf(a) if (rf_kdebug_level > 0) printf a
193 #else /* DEBUG */
194 #define db1_printf(a) { }
195 #endif /* DEBUG */
196
197 static RF_Raid_t **raidPtrs; /* global raid device descriptors */
198
199 RF_DECLARE_STATIC_MUTEX(rf_sparet_wait_mutex)
200
201 static RF_SparetWait_t *rf_sparet_wait_queue; /* requests to install a
202 * spare table */
203 static RF_SparetWait_t *rf_sparet_resp_queue; /* responses from
204 * installation process */
205
206 MALLOC_DEFINE(M_RAIDFRAME, "RAIDframe", "RAIDframe structures");
207
208 /* prototypes */
209 static void KernelWakeupFunc(struct buf *);
210 static void InitBP(struct buf *, struct vnode *, unsigned,
211 dev_t, RF_SectorNum_t, RF_SectorCount_t, caddr_t, void (*) (struct buf *),
212 void *, int, struct proc *);
213 static void raidinit(RF_Raid_t *);
214
215 void raidattach(int);
216
217 dev_type_open(raidopen);
218 dev_type_close(raidclose);
219 dev_type_read(raidread);
220 dev_type_write(raidwrite);
221 dev_type_ioctl(raidioctl);
222 dev_type_strategy(raidstrategy);
223 dev_type_dump(raiddump);
224 dev_type_size(raidsize);
225
226 const struct bdevsw raid_bdevsw = {
227 raidopen, raidclose, raidstrategy, raidioctl,
228 raiddump, raidsize, D_DISK
229 };
230
231 const struct cdevsw raid_cdevsw = {
232 raidopen, raidclose, raidread, raidwrite, raidioctl,
233 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
234 };
235
236 /*
237 * Pilfered from ccd.c
238 */
239
240 struct raidbuf {
241 struct buf rf_buf; /* new I/O buf. MUST BE FIRST!!! */
242 struct buf *rf_obp; /* ptr. to original I/O buf */
243 RF_DiskQueueData_t *req;/* the request that this was part of.. */
244 };
245
246 /* XXX Not sure if the following should be replacing the raidPtrs above,
247 or if it should be used in conjunction with that...
248 */
249
250 struct raid_softc {
251 int sc_flags; /* flags */
252 int sc_cflags; /* configuration flags */
253 size_t sc_size; /* size of the raid device */
254 char sc_xname[20]; /* XXX external name */
255 struct disk sc_dkdev; /* generic disk device info */
256 struct bufq_state buf_queue; /* used for the device queue */
257 };
258 /* sc_flags */
259 #define RAIDF_INITED 0x01 /* unit has been initialized */
260 #define RAIDF_WLABEL 0x02 /* label area is writable */
261 #define RAIDF_LABELLING 0x04 /* unit is currently being labelled */
262 #define RAIDF_WANTED 0x40 /* someone is waiting to obtain a lock */
263 #define RAIDF_LOCKED 0x80 /* unit is locked */
264
265 #define raidunit(x) DISKUNIT(x)
266 int numraid = 0;
267
268 /*
269 * Allow RAIDOUTSTANDING number of simultaneous IO's to this RAID device.
270 * Be aware that large numbers can allow the driver to consume a lot of
271 * kernel memory, especially on writes, and in degraded mode reads.
272 *
273 * For example: with a stripe width of 64 blocks (32k) and 5 disks,
274 * a single 64K write will typically require 64K for the old data,
275 * 64K for the old parity, and 64K for the new parity, for a total
276 * of 192K (if the parity buffer is not re-used immediately).
277 * Even it if is used immediately, that's still 128K, which when multiplied
278 * by say 10 requests, is 1280K, *on top* of the 640K of incoming data.
279 *
280 * Now in degraded mode, for example, a 64K read on the above setup may
281 * require data reconstruction, which will require *all* of the 4 remaining
282 * disks to participate -- 4 * 32K/disk == 128K again.
283 */
284
285 #ifndef RAIDOUTSTANDING
286 #define RAIDOUTSTANDING 6
287 #endif
288
289 #define RAIDLABELDEV(dev) \
290 (MAKEDISKDEV(major((dev)), raidunit((dev)), RAW_PART))
291
292 /* declared here, and made public, for the benefit of KVM stuff.. */
293 struct raid_softc *raid_softc;
294
295 static void raidgetdefaultlabel(RF_Raid_t *, struct raid_softc *,
296 struct disklabel *);
297 static void raidgetdisklabel(dev_t);
298 static void raidmakedisklabel(struct raid_softc *);
299
300 static int raidlock(struct raid_softc *);
301 static void raidunlock(struct raid_softc *);
302
303 static void rf_markalldirty(RF_Raid_t *);
304
305 struct device *raidrootdev;
306
307 void rf_ReconThread(struct rf_recon_req *);
308 void rf_RewriteParityThread(RF_Raid_t *raidPtr);
309 void rf_CopybackThread(RF_Raid_t *raidPtr);
310 void rf_ReconstructInPlaceThread(struct rf_recon_req *);
311 int rf_autoconfig(struct device *self);
312 void rf_buildroothack(RF_ConfigSet_t *);
313
314 RF_AutoConfig_t *rf_find_raid_components(void);
315 RF_ConfigSet_t *rf_create_auto_sets(RF_AutoConfig_t *);
316 static int rf_does_it_fit(RF_ConfigSet_t *,RF_AutoConfig_t *);
317 static int rf_reasonable_label(RF_ComponentLabel_t *);
318 void rf_create_configuration(RF_AutoConfig_t *,RF_Config_t *, RF_Raid_t *);
319 int rf_set_autoconfig(RF_Raid_t *, int);
320 int rf_set_rootpartition(RF_Raid_t *, int);
321 void rf_release_all_vps(RF_ConfigSet_t *);
322 void rf_cleanup_config_set(RF_ConfigSet_t *);
323 int rf_have_enough_components(RF_ConfigSet_t *);
324 int rf_auto_config_set(RF_ConfigSet_t *, int *);
325
326 static int raidautoconfig = 0; /* Debugging, mostly. Set to 0 to not
327 allow autoconfig to take place.
328 Note that this is overridden by having
329 RAID_AUTOCONFIG as an option in the
330 kernel config file. */
331
332 struct RF_Pools_s rf_pools;
333
334 void
335 raidattach(int num)
336 {
337 int raidID;
338 int i, rc;
339
340 #ifdef DEBUG
341 printf("raidattach: Asked for %d units\n", num);
342 #endif
343
344 if (num <= 0) {
345 #ifdef DIAGNOSTIC
346 panic("raidattach: count <= 0");
347 #endif
348 return;
349 }
350 /* This is where all the initialization stuff gets done. */
351
352 numraid = num;
353
354 /* Make some space for requested number of units... */
355
356 RF_Malloc(raidPtrs, num * sizeof(RF_Raid_t *), (RF_Raid_t **));
357 if (raidPtrs == NULL) {
358 panic("raidPtrs is NULL!!");
359 }
360
361 /* Initialize the component buffer pool. */
362 rf_pool_init(&rf_pools.cbuf, sizeof(struct raidbuf),
363 "raidpl", num * RAIDOUTSTANDING,
364 2 * num * RAIDOUTSTANDING);
365
366 rf_mutex_init(&rf_sparet_wait_mutex);
367
368 rf_sparet_wait_queue = rf_sparet_resp_queue = NULL;
369
370 for (i = 0; i < num; i++)
371 raidPtrs[i] = NULL;
372 rc = rf_BootRaidframe();
373 if (rc == 0)
374 printf("Kernelized RAIDframe activated\n");
375 else
376 panic("Serious error booting RAID!!");
377
378 /* put together some datastructures like the CCD device does.. This
379 * lets us lock the device and what-not when it gets opened. */
380
381 raid_softc = (struct raid_softc *)
382 malloc(num * sizeof(struct raid_softc),
383 M_RAIDFRAME, M_NOWAIT);
384 if (raid_softc == NULL) {
385 printf("WARNING: no memory for RAIDframe driver\n");
386 return;
387 }
388
389 memset(raid_softc, 0, num * sizeof(struct raid_softc));
390
391 raidrootdev = (struct device *)malloc(num * sizeof(struct device),
392 M_RAIDFRAME, M_NOWAIT);
393 if (raidrootdev == NULL) {
394 panic("No memory for RAIDframe driver!!?!?!");
395 }
396
397 for (raidID = 0; raidID < num; raidID++) {
398 bufq_alloc(&raid_softc[raidID].buf_queue, BUFQ_FCFS);
399
400 raidrootdev[raidID].dv_class = DV_DISK;
401 raidrootdev[raidID].dv_cfdata = NULL;
402 raidrootdev[raidID].dv_unit = raidID;
403 raidrootdev[raidID].dv_parent = NULL;
404 raidrootdev[raidID].dv_flags = 0;
405 snprintf(raidrootdev[raidID].dv_xname,
406 sizeof(raidrootdev[raidID].dv_xname), "raid%d", raidID);
407
408 RF_Malloc(raidPtrs[raidID], sizeof(RF_Raid_t),
409 (RF_Raid_t *));
410 if (raidPtrs[raidID] == NULL) {
411 printf("WARNING: raidPtrs[%d] is NULL\n", raidID);
412 numraid = raidID;
413 return;
414 }
415 }
416
417 #ifdef RAID_AUTOCONFIG
418 raidautoconfig = 1;
419 #endif
420
421 /*
422 * Register a finalizer which will be used to auto-config RAID
423 * sets once all real hardware devices have been found.
424 */
425 if (config_finalize_register(NULL, rf_autoconfig) != 0)
426 printf("WARNING: unable to register RAIDframe finalizer\n");
427 }
428
429 int
430 rf_autoconfig(struct device *self)
431 {
432 RF_AutoConfig_t *ac_list;
433 RF_ConfigSet_t *config_sets;
434
435 if (raidautoconfig == 0)
436 return (0);
437
438 /* XXX This code can only be run once. */
439 raidautoconfig = 0;
440
441 /* 1. locate all RAID components on the system */
442 #ifdef DEBUG
443 printf("Searching for RAID components...\n");
444 #endif
445 ac_list = rf_find_raid_components();
446
447 /* 2. Sort them into their respective sets. */
448 config_sets = rf_create_auto_sets(ac_list);
449
450 /*
451 * 3. Evaluate each set andconfigure the valid ones.
452 * This gets done in rf_buildroothack().
453 */
454 rf_buildroothack(config_sets);
455
456 return (1);
457 }
458
459 void
460 rf_buildroothack(RF_ConfigSet_t *config_sets)
461 {
462 RF_ConfigSet_t *cset;
463 RF_ConfigSet_t *next_cset;
464 int retcode;
465 int raidID;
466 int rootID;
467 int num_root;
468
469 rootID = 0;
470 num_root = 0;
471 cset = config_sets;
472 while(cset != NULL ) {
473 next_cset = cset->next;
474 if (rf_have_enough_components(cset) &&
475 cset->ac->clabel->autoconfigure==1) {
476 retcode = rf_auto_config_set(cset,&raidID);
477 if (!retcode) {
478 if (cset->rootable) {
479 rootID = raidID;
480 num_root++;
481 }
482 } else {
483 /* The autoconfig didn't work :( */
484 #if DEBUG
485 printf("Autoconfig failed with code %d for raid%d\n", retcode, raidID);
486 #endif
487 rf_release_all_vps(cset);
488 }
489 } else {
490 /* we're not autoconfiguring this set...
491 release the associated resources */
492 rf_release_all_vps(cset);
493 }
494 /* cleanup */
495 rf_cleanup_config_set(cset);
496 cset = next_cset;
497 }
498
499 /* we found something bootable... */
500
501 if (num_root == 1) {
502 booted_device = &raidrootdev[rootID];
503 } else if (num_root > 1) {
504 /* we can't guess.. require the user to answer... */
505 boothowto |= RB_ASKNAME;
506 }
507 }
508
509
510 int
511 raidsize(dev_t dev)
512 {
513 struct raid_softc *rs;
514 struct disklabel *lp;
515 int part, unit, omask, size;
516
517 unit = raidunit(dev);
518 if (unit >= numraid)
519 return (-1);
520 rs = &raid_softc[unit];
521
522 if ((rs->sc_flags & RAIDF_INITED) == 0)
523 return (-1);
524
525 part = DISKPART(dev);
526 omask = rs->sc_dkdev.dk_openmask & (1 << part);
527 lp = rs->sc_dkdev.dk_label;
528
529 if (omask == 0 && raidopen(dev, 0, S_IFBLK, curproc))
530 return (-1);
531
532 if (lp->d_partitions[part].p_fstype != FS_SWAP)
533 size = -1;
534 else
535 size = lp->d_partitions[part].p_size *
536 (lp->d_secsize / DEV_BSIZE);
537
538 if (omask == 0 && raidclose(dev, 0, S_IFBLK, curproc))
539 return (-1);
540
541 return (size);
542
543 }
544
545 int
546 raiddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
547 {
548 /* Not implemented. */
549 return ENXIO;
550 }
551 /* ARGSUSED */
552 int
553 raidopen(dev_t dev, int flags, int fmt, struct proc *p)
554 {
555 int unit = raidunit(dev);
556 struct raid_softc *rs;
557 struct disklabel *lp;
558 int part, pmask;
559 int error = 0;
560
561 if (unit >= numraid)
562 return (ENXIO);
563 rs = &raid_softc[unit];
564
565 if ((error = raidlock(rs)) != 0)
566 return (error);
567 lp = rs->sc_dkdev.dk_label;
568
569 part = DISKPART(dev);
570 pmask = (1 << part);
571
572 if ((rs->sc_flags & RAIDF_INITED) &&
573 (rs->sc_dkdev.dk_openmask == 0))
574 raidgetdisklabel(dev);
575
576 /* make sure that this partition exists */
577
578 if (part != RAW_PART) {
579 if (((rs->sc_flags & RAIDF_INITED) == 0) ||
580 ((part >= lp->d_npartitions) ||
581 (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
582 error = ENXIO;
583 raidunlock(rs);
584 return (error);
585 }
586 }
587 /* Prevent this unit from being unconfigured while open. */
588 switch (fmt) {
589 case S_IFCHR:
590 rs->sc_dkdev.dk_copenmask |= pmask;
591 break;
592
593 case S_IFBLK:
594 rs->sc_dkdev.dk_bopenmask |= pmask;
595 break;
596 }
597
598 if ((rs->sc_dkdev.dk_openmask == 0) &&
599 ((rs->sc_flags & RAIDF_INITED) != 0)) {
600 /* First one... mark things as dirty... Note that we *MUST*
601 have done a configure before this. I DO NOT WANT TO BE
602 SCRIBBLING TO RANDOM COMPONENTS UNTIL IT'S BEEN DETERMINED
603 THAT THEY BELONG TOGETHER!!!!! */
604 /* XXX should check to see if we're only open for reading
605 here... If so, we needn't do this, but then need some
606 other way of keeping track of what's happened.. */
607
608 rf_markalldirty( raidPtrs[unit] );
609 }
610
611
612 rs->sc_dkdev.dk_openmask =
613 rs->sc_dkdev.dk_copenmask | rs->sc_dkdev.dk_bopenmask;
614
615 raidunlock(rs);
616
617 return (error);
618
619
620 }
621 /* ARGSUSED */
622 int
623 raidclose(dev_t dev, int flags, int fmt, struct proc *p)
624 {
625 int unit = raidunit(dev);
626 struct raid_softc *rs;
627 int error = 0;
628 int part;
629
630 if (unit >= numraid)
631 return (ENXIO);
632 rs = &raid_softc[unit];
633
634 if ((error = raidlock(rs)) != 0)
635 return (error);
636
637 part = DISKPART(dev);
638
639 /* ...that much closer to allowing unconfiguration... */
640 switch (fmt) {
641 case S_IFCHR:
642 rs->sc_dkdev.dk_copenmask &= ~(1 << part);
643 break;
644
645 case S_IFBLK:
646 rs->sc_dkdev.dk_bopenmask &= ~(1 << part);
647 break;
648 }
649 rs->sc_dkdev.dk_openmask =
650 rs->sc_dkdev.dk_copenmask | rs->sc_dkdev.dk_bopenmask;
651
652 if ((rs->sc_dkdev.dk_openmask == 0) &&
653 ((rs->sc_flags & RAIDF_INITED) != 0)) {
654 /* Last one... device is not unconfigured yet.
655 Device shutdown has taken care of setting the
656 clean bits if RAIDF_INITED is not set
657 mark things as clean... */
658
659 rf_update_component_labels(raidPtrs[unit],
660 RF_FINAL_COMPONENT_UPDATE);
661 if (doing_shutdown) {
662 /* last one, and we're going down, so
663 lights out for this RAID set too. */
664 error = rf_Shutdown(raidPtrs[unit]);
665
666 /* It's no longer initialized... */
667 rs->sc_flags &= ~RAIDF_INITED;
668
669 /* Detach the disk. */
670 disk_detach(&rs->sc_dkdev);
671 }
672 }
673
674 raidunlock(rs);
675 return (0);
676
677 }
678
679 void
680 raidstrategy(struct buf *bp)
681 {
682 int s;
683
684 unsigned int raidID = raidunit(bp->b_dev);
685 RF_Raid_t *raidPtr;
686 struct raid_softc *rs = &raid_softc[raidID];
687 int wlabel;
688
689 if ((rs->sc_flags & RAIDF_INITED) ==0) {
690 bp->b_error = ENXIO;
691 bp->b_flags |= B_ERROR;
692 bp->b_resid = bp->b_bcount;
693 biodone(bp);
694 return;
695 }
696 if (raidID >= numraid || !raidPtrs[raidID]) {
697 bp->b_error = ENODEV;
698 bp->b_flags |= B_ERROR;
699 bp->b_resid = bp->b_bcount;
700 biodone(bp);
701 return;
702 }
703 raidPtr = raidPtrs[raidID];
704 if (!raidPtr->valid) {
705 bp->b_error = ENODEV;
706 bp->b_flags |= B_ERROR;
707 bp->b_resid = bp->b_bcount;
708 biodone(bp);
709 return;
710 }
711 if (bp->b_bcount == 0) {
712 db1_printf(("b_bcount is zero..\n"));
713 biodone(bp);
714 return;
715 }
716
717 /*
718 * Do bounds checking and adjust transfer. If there's an
719 * error, the bounds check will flag that for us.
720 */
721
722 wlabel = rs->sc_flags & (RAIDF_WLABEL | RAIDF_LABELLING);
723 if (DISKPART(bp->b_dev) != RAW_PART)
724 if (bounds_check_with_label(&rs->sc_dkdev, bp, wlabel) <= 0) {
725 db1_printf(("Bounds check failed!!:%d %d\n",
726 (int) bp->b_blkno, (int) wlabel));
727 biodone(bp);
728 return;
729 }
730 s = splbio();
731
732 bp->b_resid = 0;
733
734 /* stuff it onto our queue */
735 BUFQ_PUT(&rs->buf_queue, bp);
736
737 /* scheduled the IO to happen at the next convenient time */
738 wakeup(&(raidPtrs[raidID]->iodone));
739
740 splx(s);
741 }
742 /* ARGSUSED */
743 int
744 raidread(dev_t dev, struct uio *uio, int flags)
745 {
746 int unit = raidunit(dev);
747 struct raid_softc *rs;
748
749 if (unit >= numraid)
750 return (ENXIO);
751 rs = &raid_softc[unit];
752
753 if ((rs->sc_flags & RAIDF_INITED) == 0)
754 return (ENXIO);
755
756 return (physio(raidstrategy, NULL, dev, B_READ, minphys, uio));
757
758 }
759 /* ARGSUSED */
760 int
761 raidwrite(dev_t dev, struct uio *uio, int flags)
762 {
763 int unit = raidunit(dev);
764 struct raid_softc *rs;
765
766 if (unit >= numraid)
767 return (ENXIO);
768 rs = &raid_softc[unit];
769
770 if ((rs->sc_flags & RAIDF_INITED) == 0)
771 return (ENXIO);
772
773 return (physio(raidstrategy, NULL, dev, B_WRITE, minphys, uio));
774
775 }
776
777 int
778 raidioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
779 {
780 int unit = raidunit(dev);
781 int error = 0;
782 int part, pmask;
783 struct raid_softc *rs;
784 RF_Config_t *k_cfg, *u_cfg;
785 RF_Raid_t *raidPtr;
786 RF_RaidDisk_t *diskPtr;
787 RF_AccTotals_t *totals;
788 RF_DeviceConfig_t *d_cfg, **ucfgp;
789 u_char *specific_buf;
790 int retcode = 0;
791 int column;
792 int raidid;
793 struct rf_recon_req *rrcopy, *rr;
794 RF_ComponentLabel_t *clabel;
795 RF_ComponentLabel_t ci_label;
796 RF_ComponentLabel_t **clabel_ptr;
797 RF_SingleComponent_t *sparePtr,*componentPtr;
798 RF_SingleComponent_t hot_spare;
799 RF_SingleComponent_t component;
800 RF_ProgressInfo_t progressInfo, **progressInfoPtr;
801 int i, j, d;
802 #ifdef __HAVE_OLD_DISKLABEL
803 struct disklabel newlabel;
804 #endif
805
806 if (unit >= numraid)
807 return (ENXIO);
808 rs = &raid_softc[unit];
809 raidPtr = raidPtrs[unit];
810
811 db1_printf(("raidioctl: %d %d %d %d\n", (int) dev,
812 (int) DISKPART(dev), (int) unit, (int) cmd));
813
814 /* Must be open for writes for these commands... */
815 switch (cmd) {
816 case DIOCSDINFO:
817 case DIOCWDINFO:
818 #ifdef __HAVE_OLD_DISKLABEL
819 case ODIOCWDINFO:
820 case ODIOCSDINFO:
821 #endif
822 case DIOCWLABEL:
823 if ((flag & FWRITE) == 0)
824 return (EBADF);
825 }
826
827 /* Must be initialized for these... */
828 switch (cmd) {
829 case DIOCGDINFO:
830 case DIOCSDINFO:
831 case DIOCWDINFO:
832 #ifdef __HAVE_OLD_DISKLABEL
833 case ODIOCGDINFO:
834 case ODIOCWDINFO:
835 case ODIOCSDINFO:
836 case ODIOCGDEFLABEL:
837 #endif
838 case DIOCGPART:
839 case DIOCWLABEL:
840 case DIOCGDEFLABEL:
841 case RAIDFRAME_SHUTDOWN:
842 case RAIDFRAME_REWRITEPARITY:
843 case RAIDFRAME_GET_INFO:
844 case RAIDFRAME_RESET_ACCTOTALS:
845 case RAIDFRAME_GET_ACCTOTALS:
846 case RAIDFRAME_KEEP_ACCTOTALS:
847 case RAIDFRAME_GET_SIZE:
848 case RAIDFRAME_FAIL_DISK:
849 case RAIDFRAME_COPYBACK:
850 case RAIDFRAME_CHECK_RECON_STATUS:
851 case RAIDFRAME_CHECK_RECON_STATUS_EXT:
852 case RAIDFRAME_GET_COMPONENT_LABEL:
853 case RAIDFRAME_SET_COMPONENT_LABEL:
854 case RAIDFRAME_ADD_HOT_SPARE:
855 case RAIDFRAME_REMOVE_HOT_SPARE:
856 case RAIDFRAME_INIT_LABELS:
857 case RAIDFRAME_REBUILD_IN_PLACE:
858 case RAIDFRAME_CHECK_PARITY:
859 case RAIDFRAME_CHECK_PARITYREWRITE_STATUS:
860 case RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT:
861 case RAIDFRAME_CHECK_COPYBACK_STATUS:
862 case RAIDFRAME_CHECK_COPYBACK_STATUS_EXT:
863 case RAIDFRAME_SET_AUTOCONFIG:
864 case RAIDFRAME_SET_ROOT:
865 case RAIDFRAME_DELETE_COMPONENT:
866 case RAIDFRAME_INCORPORATE_HOT_SPARE:
867 if ((rs->sc_flags & RAIDF_INITED) == 0)
868 return (ENXIO);
869 }
870
871 switch (cmd) {
872
873 /* configure the system */
874 case RAIDFRAME_CONFIGURE:
875
876 if (raidPtr->valid) {
877 /* There is a valid RAID set running on this unit! */
878 printf("raid%d: Device already configured!\n",unit);
879 return(EINVAL);
880 }
881
882 /* copy-in the configuration information */
883 /* data points to a pointer to the configuration structure */
884
885 u_cfg = *((RF_Config_t **) data);
886 RF_Malloc(k_cfg, sizeof(RF_Config_t), (RF_Config_t *));
887 if (k_cfg == NULL) {
888 return (ENOMEM);
889 }
890 retcode = copyin(u_cfg, k_cfg, sizeof(RF_Config_t));
891 if (retcode) {
892 RF_Free(k_cfg, sizeof(RF_Config_t));
893 db1_printf(("rf_ioctl: retcode=%d copyin.1\n",
894 retcode));
895 return (retcode);
896 }
897 /* allocate a buffer for the layout-specific data, and copy it
898 * in */
899 if (k_cfg->layoutSpecificSize) {
900 if (k_cfg->layoutSpecificSize > 10000) {
901 /* sanity check */
902 RF_Free(k_cfg, sizeof(RF_Config_t));
903 return (EINVAL);
904 }
905 RF_Malloc(specific_buf, k_cfg->layoutSpecificSize,
906 (u_char *));
907 if (specific_buf == NULL) {
908 RF_Free(k_cfg, sizeof(RF_Config_t));
909 return (ENOMEM);
910 }
911 retcode = copyin(k_cfg->layoutSpecific, specific_buf,
912 k_cfg->layoutSpecificSize);
913 if (retcode) {
914 RF_Free(k_cfg, sizeof(RF_Config_t));
915 RF_Free(specific_buf,
916 k_cfg->layoutSpecificSize);
917 db1_printf(("rf_ioctl: retcode=%d copyin.2\n",
918 retcode));
919 return (retcode);
920 }
921 } else
922 specific_buf = NULL;
923 k_cfg->layoutSpecific = specific_buf;
924
925 /* should do some kind of sanity check on the configuration.
926 * Store the sum of all the bytes in the last byte? */
927
928 /* configure the system */
929
930 /*
931 * Clear the entire RAID descriptor, just to make sure
932 * there is no stale data left in the case of a
933 * reconfiguration
934 */
935 memset((char *) raidPtr, 0, sizeof(RF_Raid_t));
936 raidPtr->raidid = unit;
937
938 retcode = rf_Configure(raidPtr, k_cfg, NULL);
939
940 if (retcode == 0) {
941
942 /* allow this many simultaneous IO's to
943 this RAID device */
944 raidPtr->openings = RAIDOUTSTANDING;
945
946 raidinit(raidPtr);
947 rf_markalldirty(raidPtr);
948 }
949 /* free the buffers. No return code here. */
950 if (k_cfg->layoutSpecificSize) {
951 RF_Free(specific_buf, k_cfg->layoutSpecificSize);
952 }
953 RF_Free(k_cfg, sizeof(RF_Config_t));
954
955 return (retcode);
956
957 /* shutdown the system */
958 case RAIDFRAME_SHUTDOWN:
959
960 if ((error = raidlock(rs)) != 0)
961 return (error);
962
963 /*
964 * If somebody has a partition mounted, we shouldn't
965 * shutdown.
966 */
967
968 part = DISKPART(dev);
969 pmask = (1 << part);
970 if ((rs->sc_dkdev.dk_openmask & ~pmask) ||
971 ((rs->sc_dkdev.dk_bopenmask & pmask) &&
972 (rs->sc_dkdev.dk_copenmask & pmask))) {
973 raidunlock(rs);
974 return (EBUSY);
975 }
976
977 retcode = rf_Shutdown(raidPtr);
978
979 /* It's no longer initialized... */
980 rs->sc_flags &= ~RAIDF_INITED;
981
982 /* Detach the disk. */
983 disk_detach(&rs->sc_dkdev);
984
985 raidunlock(rs);
986
987 return (retcode);
988 case RAIDFRAME_GET_COMPONENT_LABEL:
989 clabel_ptr = (RF_ComponentLabel_t **) data;
990 /* need to read the component label for the disk indicated
991 by row,column in clabel */
992
993 /* For practice, let's get it directly fromdisk, rather
994 than from the in-core copy */
995 RF_Malloc( clabel, sizeof( RF_ComponentLabel_t ),
996 (RF_ComponentLabel_t *));
997 if (clabel == NULL)
998 return (ENOMEM);
999
1000 memset((char *) clabel, 0, sizeof(RF_ComponentLabel_t));
1001
1002 retcode = copyin( *clabel_ptr, clabel,
1003 sizeof(RF_ComponentLabel_t));
1004
1005 if (retcode) {
1006 RF_Free( clabel, sizeof(RF_ComponentLabel_t));
1007 return(retcode);
1008 }
1009
1010 clabel->row = 0; /* Don't allow looking at anything else.*/
1011
1012 column = clabel->column;
1013
1014 if ((column < 0) || (column >= raidPtr->numCol +
1015 raidPtr->numSpare)) {
1016 RF_Free( clabel, sizeof(RF_ComponentLabel_t));
1017 return(EINVAL);
1018 }
1019
1020 raidread_component_label(raidPtr->Disks[column].dev,
1021 raidPtr->raid_cinfo[column].ci_vp,
1022 clabel );
1023
1024 retcode = copyout(clabel, *clabel_ptr,
1025 sizeof(RF_ComponentLabel_t));
1026 RF_Free(clabel, sizeof(RF_ComponentLabel_t));
1027 return (retcode);
1028
1029 case RAIDFRAME_SET_COMPONENT_LABEL:
1030 clabel = (RF_ComponentLabel_t *) data;
1031
1032 /* XXX check the label for valid stuff... */
1033 /* Note that some things *should not* get modified --
1034 the user should be re-initing the labels instead of
1035 trying to patch things.
1036 */
1037
1038 raidid = raidPtr->raidid;
1039 #if DEBUG
1040 printf("raid%d: Got component label:\n", raidid);
1041 printf("raid%d: Version: %d\n", raidid, clabel->version);
1042 printf("raid%d: Serial Number: %d\n", raidid, clabel->serial_number);
1043 printf("raid%d: Mod counter: %d\n", raidid, clabel->mod_counter);
1044 printf("raid%d: Column: %d\n", raidid, clabel->column);
1045 printf("raid%d: Num Columns: %d\n", raidid, clabel->num_columns);
1046 printf("raid%d: Clean: %d\n", raidid, clabel->clean);
1047 printf("raid%d: Status: %d\n", raidid, clabel->status);
1048 #endif
1049 clabel->row = 0;
1050 column = clabel->column;
1051
1052 if ((column < 0) || (column >= raidPtr->numCol)) {
1053 return(EINVAL);
1054 }
1055
1056 /* XXX this isn't allowed to do anything for now :-) */
1057
1058 /* XXX and before it is, we need to fill in the rest
1059 of the fields!?!?!?! */
1060 #if 0
1061 raidwrite_component_label(
1062 raidPtr->Disks[column].dev,
1063 raidPtr->raid_cinfo[column].ci_vp,
1064 clabel );
1065 #endif
1066 return (0);
1067
1068 case RAIDFRAME_INIT_LABELS:
1069 clabel = (RF_ComponentLabel_t *) data;
1070 /*
1071 we only want the serial number from
1072 the above. We get all the rest of the information
1073 from the config that was used to create this RAID
1074 set.
1075 */
1076
1077 raidPtr->serial_number = clabel->serial_number;
1078
1079 raid_init_component_label(raidPtr, &ci_label);
1080 ci_label.serial_number = clabel->serial_number;
1081 ci_label.row = 0; /* we dont' pretend to support more */
1082
1083 for(column=0;column<raidPtr->numCol;column++) {
1084 diskPtr = &raidPtr->Disks[column];
1085 if (!RF_DEAD_DISK(diskPtr->status)) {
1086 ci_label.partitionSize = diskPtr->partitionSize;
1087 ci_label.column = column;
1088 raidwrite_component_label(
1089 raidPtr->Disks[column].dev,
1090 raidPtr->raid_cinfo[column].ci_vp,
1091 &ci_label );
1092 }
1093 }
1094
1095 return (retcode);
1096 case RAIDFRAME_SET_AUTOCONFIG:
1097 d = rf_set_autoconfig(raidPtr, *(int *) data);
1098 printf("raid%d: New autoconfig value is: %d\n",
1099 raidPtr->raidid, d);
1100 *(int *) data = d;
1101 return (retcode);
1102
1103 case RAIDFRAME_SET_ROOT:
1104 d = rf_set_rootpartition(raidPtr, *(int *) data);
1105 printf("raid%d: New rootpartition value is: %d\n",
1106 raidPtr->raidid, d);
1107 *(int *) data = d;
1108 return (retcode);
1109
1110 /* initialize all parity */
1111 case RAIDFRAME_REWRITEPARITY:
1112
1113 if (raidPtr->Layout.map->faultsTolerated == 0) {
1114 /* Parity for RAID 0 is trivially correct */
1115 raidPtr->parity_good = RF_RAID_CLEAN;
1116 return(0);
1117 }
1118
1119 if (raidPtr->parity_rewrite_in_progress == 1) {
1120 /* Re-write is already in progress! */
1121 return(EINVAL);
1122 }
1123
1124 retcode = RF_CREATE_THREAD(raidPtr->parity_rewrite_thread,
1125 rf_RewriteParityThread,
1126 raidPtr,"raid_parity");
1127 return (retcode);
1128
1129
1130 case RAIDFRAME_ADD_HOT_SPARE:
1131 sparePtr = (RF_SingleComponent_t *) data;
1132 memcpy( &hot_spare, sparePtr, sizeof(RF_SingleComponent_t));
1133 retcode = rf_add_hot_spare(raidPtr, &hot_spare);
1134 return(retcode);
1135
1136 case RAIDFRAME_REMOVE_HOT_SPARE:
1137 return(retcode);
1138
1139 case RAIDFRAME_DELETE_COMPONENT:
1140 componentPtr = (RF_SingleComponent_t *)data;
1141 memcpy( &component, componentPtr,
1142 sizeof(RF_SingleComponent_t));
1143 retcode = rf_delete_component(raidPtr, &component);
1144 return(retcode);
1145
1146 case RAIDFRAME_INCORPORATE_HOT_SPARE:
1147 componentPtr = (RF_SingleComponent_t *)data;
1148 memcpy( &component, componentPtr,
1149 sizeof(RF_SingleComponent_t));
1150 retcode = rf_incorporate_hot_spare(raidPtr, &component);
1151 return(retcode);
1152
1153 case RAIDFRAME_REBUILD_IN_PLACE:
1154
1155 if (raidPtr->Layout.map->faultsTolerated == 0) {
1156 /* Can't do this on a RAID 0!! */
1157 return(EINVAL);
1158 }
1159
1160 if (raidPtr->recon_in_progress == 1) {
1161 /* a reconstruct is already in progress! */
1162 return(EINVAL);
1163 }
1164
1165 componentPtr = (RF_SingleComponent_t *) data;
1166 memcpy( &component, componentPtr,
1167 sizeof(RF_SingleComponent_t));
1168 component.row = 0; /* we don't support any more */
1169 column = component.column;
1170
1171 if ((column < 0) || (column >= raidPtr->numCol)) {
1172 return(EINVAL);
1173 }
1174
1175 RF_LOCK_MUTEX(raidPtr->mutex);
1176 if ((raidPtr->Disks[column].status == rf_ds_optimal) &&
1177 (raidPtr->numFailures > 0)) {
1178 /* XXX 0 above shouldn't be constant!!! */
1179 /* some component other than this has failed.
1180 Let's not make things worse than they already
1181 are... */
1182 printf("raid%d: Unable to reconstruct to disk at:\n",
1183 raidPtr->raidid);
1184 printf("raid%d: Col: %d Too many failures.\n",
1185 raidPtr->raidid, column);
1186 RF_UNLOCK_MUTEX(raidPtr->mutex);
1187 return (EINVAL);
1188 }
1189 if (raidPtr->Disks[column].status ==
1190 rf_ds_reconstructing) {
1191 printf("raid%d: Unable to reconstruct to disk at:\n",
1192 raidPtr->raidid);
1193 printf("raid%d: Col: %d Reconstruction already occuring!\n", raidPtr->raidid, column);
1194
1195 RF_UNLOCK_MUTEX(raidPtr->mutex);
1196 return (EINVAL);
1197 }
1198 if (raidPtr->Disks[column].status == rf_ds_spared) {
1199 RF_UNLOCK_MUTEX(raidPtr->mutex);
1200 return (EINVAL);
1201 }
1202 RF_UNLOCK_MUTEX(raidPtr->mutex);
1203
1204 RF_Malloc(rrcopy, sizeof(*rrcopy), (struct rf_recon_req *));
1205 if (rrcopy == NULL)
1206 return(ENOMEM);
1207
1208 rrcopy->raidPtr = (void *) raidPtr;
1209 rrcopy->col = column;
1210
1211 retcode = RF_CREATE_THREAD(raidPtr->recon_thread,
1212 rf_ReconstructInPlaceThread,
1213 rrcopy,"raid_reconip");
1214 return(retcode);
1215
1216 case RAIDFRAME_GET_INFO:
1217 if (!raidPtr->valid)
1218 return (ENODEV);
1219 ucfgp = (RF_DeviceConfig_t **) data;
1220 RF_Malloc(d_cfg, sizeof(RF_DeviceConfig_t),
1221 (RF_DeviceConfig_t *));
1222 if (d_cfg == NULL)
1223 return (ENOMEM);
1224 memset((char *) d_cfg, 0, sizeof(RF_DeviceConfig_t));
1225 d_cfg->rows = 1; /* there is only 1 row now */
1226 d_cfg->cols = raidPtr->numCol;
1227 d_cfg->ndevs = raidPtr->numCol;
1228 if (d_cfg->ndevs >= RF_MAX_DISKS) {
1229 RF_Free(d_cfg, sizeof(RF_DeviceConfig_t));
1230 return (ENOMEM);
1231 }
1232 d_cfg->nspares = raidPtr->numSpare;
1233 if (d_cfg->nspares >= RF_MAX_DISKS) {
1234 RF_Free(d_cfg, sizeof(RF_DeviceConfig_t));
1235 return (ENOMEM);
1236 }
1237 d_cfg->maxqdepth = raidPtr->maxQueueDepth;
1238 d = 0;
1239 for (j = 0; j < d_cfg->cols; j++) {
1240 d_cfg->devs[d] = raidPtr->Disks[j];
1241 d++;
1242 }
1243 for (j = d_cfg->cols, i = 0; i < d_cfg->nspares; i++, j++) {
1244 d_cfg->spares[i] = raidPtr->Disks[j];
1245 }
1246 retcode = copyout(d_cfg, *ucfgp, sizeof(RF_DeviceConfig_t));
1247 RF_Free(d_cfg, sizeof(RF_DeviceConfig_t));
1248
1249 return (retcode);
1250
1251 case RAIDFRAME_CHECK_PARITY:
1252 *(int *) data = raidPtr->parity_good;
1253 return (0);
1254
1255 case RAIDFRAME_RESET_ACCTOTALS:
1256 memset(&raidPtr->acc_totals, 0, sizeof(raidPtr->acc_totals));
1257 return (0);
1258
1259 case RAIDFRAME_GET_ACCTOTALS:
1260 totals = (RF_AccTotals_t *) data;
1261 *totals = raidPtr->acc_totals;
1262 return (0);
1263
1264 case RAIDFRAME_KEEP_ACCTOTALS:
1265 raidPtr->keep_acc_totals = *(int *)data;
1266 return (0);
1267
1268 case RAIDFRAME_GET_SIZE:
1269 *(int *) data = raidPtr->totalSectors;
1270 return (0);
1271
1272 /* fail a disk & optionally start reconstruction */
1273 case RAIDFRAME_FAIL_DISK:
1274
1275 if (raidPtr->Layout.map->faultsTolerated == 0) {
1276 /* Can't do this on a RAID 0!! */
1277 return(EINVAL);
1278 }
1279
1280 rr = (struct rf_recon_req *) data;
1281 rr->row = 0;
1282 if (rr->col < 0 || rr->col >= raidPtr->numCol)
1283 return (EINVAL);
1284
1285
1286 RF_LOCK_MUTEX(raidPtr->mutex);
1287 if (raidPtr->status == rf_rs_reconstructing) {
1288 /* you can't fail a disk while we're reconstructing! */
1289 /* XXX wrong for RAID6 */
1290 RF_UNLOCK_MUTEX(raidPtr->mutex);
1291 return (EINVAL);
1292 }
1293 if ((raidPtr->Disks[rr->col].status ==
1294 rf_ds_optimal) && (raidPtr->numFailures > 0)) {
1295 /* some other component has failed. Let's not make
1296 things worse. XXX wrong for RAID6 */
1297 RF_UNLOCK_MUTEX(raidPtr->mutex);
1298 return (EINVAL);
1299 }
1300 if (raidPtr->Disks[rr->col].status == rf_ds_spared) {
1301 /* Can't fail a spared disk! */
1302 RF_UNLOCK_MUTEX(raidPtr->mutex);
1303 return (EINVAL);
1304 }
1305 RF_UNLOCK_MUTEX(raidPtr->mutex);
1306
1307 /* make a copy of the recon request so that we don't rely on
1308 * the user's buffer */
1309 RF_Malloc(rrcopy, sizeof(*rrcopy), (struct rf_recon_req *));
1310 if (rrcopy == NULL)
1311 return(ENOMEM);
1312 memcpy(rrcopy, rr, sizeof(*rr));
1313 rrcopy->raidPtr = (void *) raidPtr;
1314
1315 retcode = RF_CREATE_THREAD(raidPtr->recon_thread,
1316 rf_ReconThread,
1317 rrcopy,"raid_recon");
1318 return (0);
1319
1320 /* invoke a copyback operation after recon on whatever disk
1321 * needs it, if any */
1322 case RAIDFRAME_COPYBACK:
1323
1324 if (raidPtr->Layout.map->faultsTolerated == 0) {
1325 /* This makes no sense on a RAID 0!! */
1326 return(EINVAL);
1327 }
1328
1329 if (raidPtr->copyback_in_progress == 1) {
1330 /* Copyback is already in progress! */
1331 return(EINVAL);
1332 }
1333
1334 retcode = RF_CREATE_THREAD(raidPtr->copyback_thread,
1335 rf_CopybackThread,
1336 raidPtr,"raid_copyback");
1337 return (retcode);
1338
1339 /* return the percentage completion of reconstruction */
1340 case RAIDFRAME_CHECK_RECON_STATUS:
1341 if (raidPtr->Layout.map->faultsTolerated == 0) {
1342 /* This makes no sense on a RAID 0, so tell the
1343 user it's done. */
1344 *(int *) data = 100;
1345 return(0);
1346 }
1347 if (raidPtr->status != rf_rs_reconstructing)
1348 *(int *) data = 100;
1349 else {
1350 if (raidPtr->reconControl->numRUsTotal > 0) {
1351 *(int *) data = (raidPtr->reconControl->numRUsComplete * 100 / raidPtr->reconControl->numRUsTotal);
1352 } else {
1353 *(int *) data = 0;
1354 }
1355 }
1356 return (0);
1357 case RAIDFRAME_CHECK_RECON_STATUS_EXT:
1358 progressInfoPtr = (RF_ProgressInfo_t **) data;
1359 if (raidPtr->status != rf_rs_reconstructing) {
1360 progressInfo.remaining = 0;
1361 progressInfo.completed = 100;
1362 progressInfo.total = 100;
1363 } else {
1364 progressInfo.total =
1365 raidPtr->reconControl->numRUsTotal;
1366 progressInfo.completed =
1367 raidPtr->reconControl->numRUsComplete;
1368 progressInfo.remaining = progressInfo.total -
1369 progressInfo.completed;
1370 }
1371 retcode = copyout(&progressInfo, *progressInfoPtr,
1372 sizeof(RF_ProgressInfo_t));
1373 return (retcode);
1374
1375 case RAIDFRAME_CHECK_PARITYREWRITE_STATUS:
1376 if (raidPtr->Layout.map->faultsTolerated == 0) {
1377 /* This makes no sense on a RAID 0, so tell the
1378 user it's done. */
1379 *(int *) data = 100;
1380 return(0);
1381 }
1382 if (raidPtr->parity_rewrite_in_progress == 1) {
1383 *(int *) data = 100 *
1384 raidPtr->parity_rewrite_stripes_done /
1385 raidPtr->Layout.numStripe;
1386 } else {
1387 *(int *) data = 100;
1388 }
1389 return (0);
1390
1391 case RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT:
1392 progressInfoPtr = (RF_ProgressInfo_t **) data;
1393 if (raidPtr->parity_rewrite_in_progress == 1) {
1394 progressInfo.total = raidPtr->Layout.numStripe;
1395 progressInfo.completed =
1396 raidPtr->parity_rewrite_stripes_done;
1397 progressInfo.remaining = progressInfo.total -
1398 progressInfo.completed;
1399 } else {
1400 progressInfo.remaining = 0;
1401 progressInfo.completed = 100;
1402 progressInfo.total = 100;
1403 }
1404 retcode = copyout(&progressInfo, *progressInfoPtr,
1405 sizeof(RF_ProgressInfo_t));
1406 return (retcode);
1407
1408 case RAIDFRAME_CHECK_COPYBACK_STATUS:
1409 if (raidPtr->Layout.map->faultsTolerated == 0) {
1410 /* This makes no sense on a RAID 0 */
1411 *(int *) data = 100;
1412 return(0);
1413 }
1414 if (raidPtr->copyback_in_progress == 1) {
1415 *(int *) data = 100 * raidPtr->copyback_stripes_done /
1416 raidPtr->Layout.numStripe;
1417 } else {
1418 *(int *) data = 100;
1419 }
1420 return (0);
1421
1422 case RAIDFRAME_CHECK_COPYBACK_STATUS_EXT:
1423 progressInfoPtr = (RF_ProgressInfo_t **) data;
1424 if (raidPtr->copyback_in_progress == 1) {
1425 progressInfo.total = raidPtr->Layout.numStripe;
1426 progressInfo.completed =
1427 raidPtr->copyback_stripes_done;
1428 progressInfo.remaining = progressInfo.total -
1429 progressInfo.completed;
1430 } else {
1431 progressInfo.remaining = 0;
1432 progressInfo.completed = 100;
1433 progressInfo.total = 100;
1434 }
1435 retcode = copyout(&progressInfo, *progressInfoPtr,
1436 sizeof(RF_ProgressInfo_t));
1437 return (retcode);
1438
1439 /* the sparetable daemon calls this to wait for the kernel to
1440 * need a spare table. this ioctl does not return until a
1441 * spare table is needed. XXX -- calling mpsleep here in the
1442 * ioctl code is almost certainly wrong and evil. -- XXX XXX
1443 * -- I should either compute the spare table in the kernel,
1444 * or have a different -- XXX XXX -- interface (a different
1445 * character device) for delivering the table -- XXX */
1446 #if 0
1447 case RAIDFRAME_SPARET_WAIT:
1448 RF_LOCK_MUTEX(rf_sparet_wait_mutex);
1449 while (!rf_sparet_wait_queue)
1450 mpsleep(&rf_sparet_wait_queue, (PZERO + 1) | PCATCH, "sparet wait", 0, (void *) simple_lock_addr(rf_sparet_wait_mutex), MS_LOCK_SIMPLE);
1451 waitreq = rf_sparet_wait_queue;
1452 rf_sparet_wait_queue = rf_sparet_wait_queue->next;
1453 RF_UNLOCK_MUTEX(rf_sparet_wait_mutex);
1454
1455 /* structure assignment */
1456 *((RF_SparetWait_t *) data) = *waitreq;
1457
1458 RF_Free(waitreq, sizeof(*waitreq));
1459 return (0);
1460
1461 /* wakes up a process waiting on SPARET_WAIT and puts an error
1462 * code in it that will cause the dameon to exit */
1463 case RAIDFRAME_ABORT_SPARET_WAIT:
1464 RF_Malloc(waitreq, sizeof(*waitreq), (RF_SparetWait_t *));
1465 waitreq->fcol = -1;
1466 RF_LOCK_MUTEX(rf_sparet_wait_mutex);
1467 waitreq->next = rf_sparet_wait_queue;
1468 rf_sparet_wait_queue = waitreq;
1469 RF_UNLOCK_MUTEX(rf_sparet_wait_mutex);
1470 wakeup(&rf_sparet_wait_queue);
1471 return (0);
1472
1473 /* used by the spare table daemon to deliver a spare table
1474 * into the kernel */
1475 case RAIDFRAME_SEND_SPARET:
1476
1477 /* install the spare table */
1478 retcode = rf_SetSpareTable(raidPtr, *(void **) data);
1479
1480 /* respond to the requestor. the return status of the spare
1481 * table installation is passed in the "fcol" field */
1482 RF_Malloc(waitreq, sizeof(*waitreq), (RF_SparetWait_t *));
1483 waitreq->fcol = retcode;
1484 RF_LOCK_MUTEX(rf_sparet_wait_mutex);
1485 waitreq->next = rf_sparet_resp_queue;
1486 rf_sparet_resp_queue = waitreq;
1487 wakeup(&rf_sparet_resp_queue);
1488 RF_UNLOCK_MUTEX(rf_sparet_wait_mutex);
1489
1490 return (retcode);
1491 #endif
1492
1493 default:
1494 break; /* fall through to the os-specific code below */
1495
1496 }
1497
1498 if (!raidPtr->valid)
1499 return (EINVAL);
1500
1501 /*
1502 * Add support for "regular" device ioctls here.
1503 */
1504
1505 switch (cmd) {
1506 case DIOCGDINFO:
1507 *(struct disklabel *) data = *(rs->sc_dkdev.dk_label);
1508 break;
1509 #ifdef __HAVE_OLD_DISKLABEL
1510 case ODIOCGDINFO:
1511 newlabel = *(rs->sc_dkdev.dk_label);
1512 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1513 return ENOTTY;
1514 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1515 break;
1516 #endif
1517
1518 case DIOCGPART:
1519 ((struct partinfo *) data)->disklab = rs->sc_dkdev.dk_label;
1520 ((struct partinfo *) data)->part =
1521 &rs->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
1522 break;
1523
1524 case DIOCWDINFO:
1525 case DIOCSDINFO:
1526 #ifdef __HAVE_OLD_DISKLABEL
1527 case ODIOCWDINFO:
1528 case ODIOCSDINFO:
1529 #endif
1530 {
1531 struct disklabel *lp;
1532 #ifdef __HAVE_OLD_DISKLABEL
1533 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1534 memset(&newlabel, 0, sizeof newlabel);
1535 memcpy(&newlabel, data, sizeof (struct olddisklabel));
1536 lp = &newlabel;
1537 } else
1538 #endif
1539 lp = (struct disklabel *)data;
1540
1541 if ((error = raidlock(rs)) != 0)
1542 return (error);
1543
1544 rs->sc_flags |= RAIDF_LABELLING;
1545
1546 error = setdisklabel(rs->sc_dkdev.dk_label,
1547 lp, 0, rs->sc_dkdev.dk_cpulabel);
1548 if (error == 0) {
1549 if (cmd == DIOCWDINFO
1550 #ifdef __HAVE_OLD_DISKLABEL
1551 || cmd == ODIOCWDINFO
1552 #endif
1553 )
1554 error = writedisklabel(RAIDLABELDEV(dev),
1555 raidstrategy, rs->sc_dkdev.dk_label,
1556 rs->sc_dkdev.dk_cpulabel);
1557 }
1558 rs->sc_flags &= ~RAIDF_LABELLING;
1559
1560 raidunlock(rs);
1561
1562 if (error)
1563 return (error);
1564 break;
1565 }
1566
1567 case DIOCWLABEL:
1568 if (*(int *) data != 0)
1569 rs->sc_flags |= RAIDF_WLABEL;
1570 else
1571 rs->sc_flags &= ~RAIDF_WLABEL;
1572 break;
1573
1574 case DIOCGDEFLABEL:
1575 raidgetdefaultlabel(raidPtr, rs, (struct disklabel *) data);
1576 break;
1577
1578 #ifdef __HAVE_OLD_DISKLABEL
1579 case ODIOCGDEFLABEL:
1580 raidgetdefaultlabel(raidPtr, rs, &newlabel);
1581 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1582 return ENOTTY;
1583 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1584 break;
1585 #endif
1586
1587 default:
1588 retcode = ENOTTY;
1589 }
1590 return (retcode);
1591
1592 }
1593
1594
1595 /* raidinit -- complete the rest of the initialization for the
1596 RAIDframe device. */
1597
1598
1599 static void
1600 raidinit(RF_Raid_t *raidPtr)
1601 {
1602 struct raid_softc *rs;
1603 int unit;
1604
1605 unit = raidPtr->raidid;
1606
1607 rs = &raid_softc[unit];
1608
1609 /* XXX should check return code first... */
1610 rs->sc_flags |= RAIDF_INITED;
1611
1612 /* XXX doesn't check bounds. */
1613 snprintf(rs->sc_xname, sizeof(rs->sc_xname), "raid%d", unit);
1614
1615 rs->sc_dkdev.dk_name = rs->sc_xname;
1616
1617 /* disk_attach actually creates space for the CPU disklabel, among
1618 * other things, so it's critical to call this *BEFORE* we try putzing
1619 * with disklabels. */
1620
1621 disk_attach(&rs->sc_dkdev);
1622
1623 /* XXX There may be a weird interaction here between this, and
1624 * protectedSectors, as used in RAIDframe. */
1625
1626 rs->sc_size = raidPtr->totalSectors;
1627 }
1628 #if (RF_INCLUDE_PARITY_DECLUSTERING_DS > 0)
1629 /* wake up the daemon & tell it to get us a spare table
1630 * XXX
1631 * the entries in the queues should be tagged with the raidPtr
1632 * so that in the extremely rare case that two recons happen at once,
1633 * we know for which device were requesting a spare table
1634 * XXX
1635 *
1636 * XXX This code is not currently used. GO
1637 */
1638 int
1639 rf_GetSpareTableFromDaemon(RF_SparetWait_t *req)
1640 {
1641 int retcode;
1642
1643 RF_LOCK_MUTEX(rf_sparet_wait_mutex);
1644 req->next = rf_sparet_wait_queue;
1645 rf_sparet_wait_queue = req;
1646 wakeup(&rf_sparet_wait_queue);
1647
1648 /* mpsleep unlocks the mutex */
1649 while (!rf_sparet_resp_queue) {
1650 tsleep(&rf_sparet_resp_queue, PRIBIO,
1651 "raidframe getsparetable", 0);
1652 }
1653 req = rf_sparet_resp_queue;
1654 rf_sparet_resp_queue = req->next;
1655 RF_UNLOCK_MUTEX(rf_sparet_wait_mutex);
1656
1657 retcode = req->fcol;
1658 RF_Free(req, sizeof(*req)); /* this is not the same req as we
1659 * alloc'd */
1660 return (retcode);
1661 }
1662 #endif
1663
1664 /* a wrapper around rf_DoAccess that extracts appropriate info from the
1665 * bp & passes it down.
1666 * any calls originating in the kernel must use non-blocking I/O
1667 * do some extra sanity checking to return "appropriate" error values for
1668 * certain conditions (to make some standard utilities work)
1669 *
1670 * Formerly known as: rf_DoAccessKernel
1671 */
1672 void
1673 raidstart(RF_Raid_t *raidPtr)
1674 {
1675 RF_SectorCount_t num_blocks, pb, sum;
1676 RF_RaidAddr_t raid_addr;
1677 struct partition *pp;
1678 daddr_t blocknum;
1679 int unit;
1680 struct raid_softc *rs;
1681 int do_async;
1682 struct buf *bp;
1683 int rc;
1684
1685 unit = raidPtr->raidid;
1686 rs = &raid_softc[unit];
1687
1688 /* quick check to see if anything has died recently */
1689 RF_LOCK_MUTEX(raidPtr->mutex);
1690 if (raidPtr->numNewFailures > 0) {
1691 RF_UNLOCK_MUTEX(raidPtr->mutex);
1692 rf_update_component_labels(raidPtr,
1693 RF_NORMAL_COMPONENT_UPDATE);
1694 RF_LOCK_MUTEX(raidPtr->mutex);
1695 raidPtr->numNewFailures--;
1696 }
1697
1698 /* Check to see if we're at the limit... */
1699 while (raidPtr->openings > 0) {
1700 RF_UNLOCK_MUTEX(raidPtr->mutex);
1701
1702 /* get the next item, if any, from the queue */
1703 if ((bp = BUFQ_GET(&rs->buf_queue)) == NULL) {
1704 /* nothing more to do */
1705 return;
1706 }
1707
1708 /* Ok, for the bp we have here, bp->b_blkno is relative to the
1709 * partition.. Need to make it absolute to the underlying
1710 * device.. */
1711
1712 blocknum = bp->b_blkno;
1713 if (DISKPART(bp->b_dev) != RAW_PART) {
1714 pp = &rs->sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
1715 blocknum += pp->p_offset;
1716 }
1717
1718 db1_printf(("Blocks: %d, %d\n", (int) bp->b_blkno,
1719 (int) blocknum));
1720
1721 db1_printf(("bp->b_bcount = %d\n", (int) bp->b_bcount));
1722 db1_printf(("bp->b_resid = %d\n", (int) bp->b_resid));
1723
1724 /* *THIS* is where we adjust what block we're going to...
1725 * but DO NOT TOUCH bp->b_blkno!!! */
1726 raid_addr = blocknum;
1727
1728 num_blocks = bp->b_bcount >> raidPtr->logBytesPerSector;
1729 pb = (bp->b_bcount & raidPtr->sectorMask) ? 1 : 0;
1730 sum = raid_addr + num_blocks + pb;
1731 if (1 || rf_debugKernelAccess) {
1732 db1_printf(("raid_addr=%d sum=%d num_blocks=%d(+%d) (%d)\n",
1733 (int) raid_addr, (int) sum, (int) num_blocks,
1734 (int) pb, (int) bp->b_resid));
1735 }
1736 if ((sum > raidPtr->totalSectors) || (sum < raid_addr)
1737 || (sum < num_blocks) || (sum < pb)) {
1738 bp->b_error = ENOSPC;
1739 bp->b_flags |= B_ERROR;
1740 bp->b_resid = bp->b_bcount;
1741 biodone(bp);
1742 RF_LOCK_MUTEX(raidPtr->mutex);
1743 continue;
1744 }
1745 /*
1746 * XXX rf_DoAccess() should do this, not just DoAccessKernel()
1747 */
1748
1749 if (bp->b_bcount & raidPtr->sectorMask) {
1750 bp->b_error = EINVAL;
1751 bp->b_flags |= B_ERROR;
1752 bp->b_resid = bp->b_bcount;
1753 biodone(bp);
1754 RF_LOCK_MUTEX(raidPtr->mutex);
1755 continue;
1756
1757 }
1758 db1_printf(("Calling DoAccess..\n"));
1759
1760
1761 RF_LOCK_MUTEX(raidPtr->mutex);
1762 raidPtr->openings--;
1763 RF_UNLOCK_MUTEX(raidPtr->mutex);
1764
1765 /*
1766 * Everything is async.
1767 */
1768 do_async = 1;
1769
1770 disk_busy(&rs->sc_dkdev);
1771
1772 /* XXX we're still at splbio() here... do we *really*
1773 need to be? */
1774
1775 /* don't ever condition on bp->b_flags & B_WRITE.
1776 * always condition on B_READ instead */
1777
1778 rc = rf_DoAccess(raidPtr, (bp->b_flags & B_READ) ?
1779 RF_IO_TYPE_READ : RF_IO_TYPE_WRITE,
1780 do_async, raid_addr, num_blocks,
1781 bp->b_data, bp, RF_DAG_NONBLOCKING_IO);
1782
1783 if (rc) {
1784 bp->b_error = rc;
1785 bp->b_flags |= B_ERROR;
1786 bp->b_resid = bp->b_bcount;
1787 biodone(bp);
1788 /* continue loop */
1789 }
1790
1791 RF_LOCK_MUTEX(raidPtr->mutex);
1792 }
1793 RF_UNLOCK_MUTEX(raidPtr->mutex);
1794 }
1795
1796
1797
1798
1799 /* invoke an I/O from kernel mode. Disk queue should be locked upon entry */
1800
1801 int
1802 rf_DispatchKernelIO(RF_DiskQueue_t *queue, RF_DiskQueueData_t *req)
1803 {
1804 int op = (req->type == RF_IO_TYPE_READ) ? B_READ : B_WRITE;
1805 struct buf *bp;
1806 struct raidbuf *raidbp = NULL;
1807
1808 req->queue = queue;
1809
1810 #if DIAGNOSTIC
1811 if (queue->raidPtr->raidid >= numraid) {
1812 printf("Invalid unit number: %d %d\n", queue->raidPtr->raidid,
1813 numraid);
1814 panic("Invalid Unit number in rf_DispatchKernelIO");
1815 }
1816 #endif
1817
1818 bp = req->bp;
1819 #if 1
1820 /* XXX when there is a physical disk failure, someone is passing us a
1821 * buffer that contains old stuff!! Attempt to deal with this problem
1822 * without taking a performance hit... (not sure where the real bug
1823 * is. It's buried in RAIDframe somewhere) :-( GO ) */
1824
1825 if (bp->b_flags & B_ERROR) {
1826 bp->b_flags &= ~B_ERROR;
1827 }
1828 if (bp->b_error != 0) {
1829 bp->b_error = 0;
1830 }
1831 #endif
1832 raidbp = pool_get(&rf_pools.cbuf, PR_NOWAIT);
1833 if (raidbp == NULL) {
1834 bp->b_flags |= B_ERROR;
1835 bp->b_error = ENOMEM;
1836 return (ENOMEM);
1837 }
1838 BUF_INIT(&raidbp->rf_buf);
1839
1840 /*
1841 * context for raidiodone
1842 */
1843 raidbp->rf_obp = bp;
1844 raidbp->req = req;
1845
1846 BIO_COPYPRIO(&raidbp->rf_buf, bp);
1847
1848 switch (req->type) {
1849 case RF_IO_TYPE_NOP: /* used primarily to unlock a locked queue */
1850 /* XXX need to do something extra here.. */
1851 /* I'm leaving this in, as I've never actually seen it used,
1852 * and I'd like folks to report it... GO */
1853 printf(("WAKEUP CALLED\n"));
1854 queue->numOutstanding++;
1855
1856 /* XXX need to glue the original buffer into this?? */
1857
1858 KernelWakeupFunc(&raidbp->rf_buf);
1859 break;
1860
1861 case RF_IO_TYPE_READ:
1862 case RF_IO_TYPE_WRITE:
1863 #if RF_ACC_TRACE > 0
1864 if (req->tracerec) {
1865 RF_ETIMER_START(req->tracerec->timer);
1866 }
1867 #endif
1868 InitBP(&raidbp->rf_buf, queue->rf_cinfo->ci_vp,
1869 op | bp->b_flags, queue->rf_cinfo->ci_dev,
1870 req->sectorOffset, req->numSector,
1871 req->buf, KernelWakeupFunc, (void *) req,
1872 queue->raidPtr->logBytesPerSector, req->b_proc);
1873
1874 if (rf_debugKernelAccess) {
1875 db1_printf(("dispatch: bp->b_blkno = %ld\n",
1876 (long) bp->b_blkno));
1877 }
1878 queue->numOutstanding++;
1879 queue->last_deq_sector = req->sectorOffset;
1880 /* acc wouldn't have been let in if there were any pending
1881 * reqs at any other priority */
1882 queue->curPriority = req->priority;
1883
1884 db1_printf(("Going for %c to unit %d col %d\n",
1885 req->type, queue->raidPtr->raidid,
1886 queue->col));
1887 db1_printf(("sector %d count %d (%d bytes) %d\n",
1888 (int) req->sectorOffset, (int) req->numSector,
1889 (int) (req->numSector <<
1890 queue->raidPtr->logBytesPerSector),
1891 (int) queue->raidPtr->logBytesPerSector));
1892 if ((raidbp->rf_buf.b_flags & B_READ) == 0) {
1893 raidbp->rf_buf.b_vp->v_numoutput++;
1894 }
1895 VOP_STRATEGY(raidbp->rf_buf.b_vp, &raidbp->rf_buf);
1896
1897 break;
1898
1899 default:
1900 panic("bad req->type in rf_DispatchKernelIO");
1901 }
1902 db1_printf(("Exiting from DispatchKernelIO\n"));
1903
1904 return (0);
1905 }
1906 /* this is the callback function associated with a I/O invoked from
1907 kernel code.
1908 */
1909 static void
1910 KernelWakeupFunc(struct buf *vbp)
1911 {
1912 RF_DiskQueueData_t *req = NULL;
1913 RF_DiskQueue_t *queue;
1914 struct raidbuf *raidbp = (struct raidbuf *) vbp;
1915 struct buf *bp;
1916 int s;
1917
1918 s = splbio();
1919 db1_printf(("recovering the request queue:\n"));
1920 req = raidbp->req;
1921
1922 bp = raidbp->rf_obp;
1923
1924 queue = (RF_DiskQueue_t *) req->queue;
1925
1926 if (raidbp->rf_buf.b_flags & B_ERROR) {
1927 bp->b_flags |= B_ERROR;
1928 bp->b_error = raidbp->rf_buf.b_error ?
1929 raidbp->rf_buf.b_error : EIO;
1930 }
1931
1932 /* XXX methinks this could be wrong... */
1933 #if 1
1934 bp->b_resid = raidbp->rf_buf.b_resid;
1935 #endif
1936 #if RF_ACC_TRACE > 0
1937 if (req->tracerec) {
1938 RF_ETIMER_STOP(req->tracerec->timer);
1939 RF_ETIMER_EVAL(req->tracerec->timer);
1940 RF_LOCK_MUTEX(rf_tracing_mutex);
1941 req->tracerec->diskwait_us += RF_ETIMER_VAL_US(req->tracerec->timer);
1942 req->tracerec->phys_io_us += RF_ETIMER_VAL_US(req->tracerec->timer);
1943 req->tracerec->num_phys_ios++;
1944 RF_UNLOCK_MUTEX(rf_tracing_mutex);
1945 }
1946 #endif
1947 bp->b_bcount = raidbp->rf_buf.b_bcount; /* XXXX ?? */
1948
1949 /* XXX Ok, let's get aggressive... If B_ERROR is set, let's go
1950 * ballistic, and mark the component as hosed... */
1951
1952 if (bp->b_flags & B_ERROR) {
1953 /* Mark the disk as dead */
1954 /* but only mark it once... */
1955 /* and only if it wouldn't leave this RAID set
1956 completely broken */
1957 if (((queue->raidPtr->Disks[queue->col].status ==
1958 rf_ds_optimal) ||
1959 (queue->raidPtr->Disks[queue->col].status ==
1960 rf_ds_used_spare)) &&
1961 (queue->raidPtr->numFailures <
1962 queue->raidPtr->Layout.map->faultsTolerated)) {
1963 printf("raid%d: IO Error. Marking %s as failed.\n",
1964 queue->raidPtr->raidid,
1965 queue->raidPtr->Disks[queue->col].devname);
1966 queue->raidPtr->Disks[queue->col].status =
1967 rf_ds_failed;
1968 queue->raidPtr->status = rf_rs_degraded;
1969 queue->raidPtr->numFailures++;
1970 queue->raidPtr->numNewFailures++;
1971 } else { /* Disk is already dead... */
1972 /* printf("Disk already marked as dead!\n"); */
1973 }
1974
1975 }
1976
1977 pool_put(&rf_pools.cbuf, raidbp);
1978
1979 /* Fill in the error value */
1980
1981 req->error = (bp->b_flags & B_ERROR) ? bp->b_error : 0;
1982
1983 simple_lock(&queue->raidPtr->iodone_lock);
1984
1985 /* Drop this one on the "finished" queue... */
1986 TAILQ_INSERT_TAIL(&(queue->raidPtr->iodone), req, iodone_entries);
1987
1988 /* Let the raidio thread know there is work to be done. */
1989 wakeup(&(queue->raidPtr->iodone));
1990
1991 simple_unlock(&queue->raidPtr->iodone_lock);
1992
1993 splx(s);
1994 }
1995
1996
1997
1998 /*
1999 * initialize a buf structure for doing an I/O in the kernel.
2000 */
2001 static void
2002 InitBP(struct buf *bp, struct vnode *b_vp, unsigned rw_flag, dev_t dev,
2003 RF_SectorNum_t startSect, RF_SectorCount_t numSect, caddr_t bf,
2004 void (*cbFunc) (struct buf *), void *cbArg, int logBytesPerSector,
2005 struct proc *b_proc)
2006 {
2007 /* bp->b_flags = B_PHYS | rw_flag; */
2008 bp->b_flags = B_CALL | rw_flag; /* XXX need B_PHYS here too??? */
2009 bp->b_bcount = numSect << logBytesPerSector;
2010 bp->b_bufsize = bp->b_bcount;
2011 bp->b_error = 0;
2012 bp->b_dev = dev;
2013 bp->b_data = bf;
2014 bp->b_blkno = startSect;
2015 bp->b_resid = bp->b_bcount; /* XXX is this right!??!?!! */
2016 if (bp->b_bcount == 0) {
2017 panic("bp->b_bcount is zero in InitBP!!");
2018 }
2019 bp->b_proc = b_proc;
2020 bp->b_iodone = cbFunc;
2021 bp->b_vp = b_vp;
2022
2023 }
2024
2025 static void
2026 raidgetdefaultlabel(RF_Raid_t *raidPtr, struct raid_softc *rs,
2027 struct disklabel *lp)
2028 {
2029 memset(lp, 0, sizeof(*lp));
2030
2031 /* fabricate a label... */
2032 lp->d_secperunit = raidPtr->totalSectors;
2033 lp->d_secsize = raidPtr->bytesPerSector;
2034 lp->d_nsectors = raidPtr->Layout.dataSectorsPerStripe;
2035 lp->d_ntracks = 4 * raidPtr->numCol;
2036 lp->d_ncylinders = raidPtr->totalSectors /
2037 (lp->d_nsectors * lp->d_ntracks);
2038 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
2039
2040 strncpy(lp->d_typename, "raid", sizeof(lp->d_typename));
2041 lp->d_type = DTYPE_RAID;
2042 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
2043 lp->d_rpm = 3600;
2044 lp->d_interleave = 1;
2045 lp->d_flags = 0;
2046
2047 lp->d_partitions[RAW_PART].p_offset = 0;
2048 lp->d_partitions[RAW_PART].p_size = raidPtr->totalSectors;
2049 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
2050 lp->d_npartitions = RAW_PART + 1;
2051
2052 lp->d_magic = DISKMAGIC;
2053 lp->d_magic2 = DISKMAGIC;
2054 lp->d_checksum = dkcksum(rs->sc_dkdev.dk_label);
2055
2056 }
2057 /*
2058 * Read the disklabel from the raid device. If one is not present, fake one
2059 * up.
2060 */
2061 static void
2062 raidgetdisklabel(dev_t dev)
2063 {
2064 int unit = raidunit(dev);
2065 struct raid_softc *rs = &raid_softc[unit];
2066 const char *errstring;
2067 struct disklabel *lp = rs->sc_dkdev.dk_label;
2068 struct cpu_disklabel *clp = rs->sc_dkdev.dk_cpulabel;
2069 RF_Raid_t *raidPtr;
2070
2071 db1_printf(("Getting the disklabel...\n"));
2072
2073 memset(clp, 0, sizeof(*clp));
2074
2075 raidPtr = raidPtrs[unit];
2076
2077 raidgetdefaultlabel(raidPtr, rs, lp);
2078
2079 /*
2080 * Call the generic disklabel extraction routine.
2081 */
2082 errstring = readdisklabel(RAIDLABELDEV(dev), raidstrategy,
2083 rs->sc_dkdev.dk_label, rs->sc_dkdev.dk_cpulabel);
2084 if (errstring)
2085 raidmakedisklabel(rs);
2086 else {
2087 int i;
2088 struct partition *pp;
2089
2090 /*
2091 * Sanity check whether the found disklabel is valid.
2092 *
2093 * This is necessary since total size of the raid device
2094 * may vary when an interleave is changed even though exactly
2095 * same componets are used, and old disklabel may used
2096 * if that is found.
2097 */
2098 if (lp->d_secperunit != rs->sc_size)
2099 printf("raid%d: WARNING: %s: "
2100 "total sector size in disklabel (%d) != "
2101 "the size of raid (%ld)\n", unit, rs->sc_xname,
2102 lp->d_secperunit, (long) rs->sc_size);
2103 for (i = 0; i < lp->d_npartitions; i++) {
2104 pp = &lp->d_partitions[i];
2105 if (pp->p_offset + pp->p_size > rs->sc_size)
2106 printf("raid%d: WARNING: %s: end of partition `%c' "
2107 "exceeds the size of raid (%ld)\n",
2108 unit, rs->sc_xname, 'a' + i, (long) rs->sc_size);
2109 }
2110 }
2111
2112 }
2113 /*
2114 * Take care of things one might want to take care of in the event
2115 * that a disklabel isn't present.
2116 */
2117 static void
2118 raidmakedisklabel(struct raid_softc *rs)
2119 {
2120 struct disklabel *lp = rs->sc_dkdev.dk_label;
2121 db1_printf(("Making a label..\n"));
2122
2123 /*
2124 * For historical reasons, if there's no disklabel present
2125 * the raw partition must be marked FS_BSDFFS.
2126 */
2127
2128 lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
2129
2130 strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
2131
2132 lp->d_checksum = dkcksum(lp);
2133 }
2134 /*
2135 * Lookup the provided name in the filesystem. If the file exists,
2136 * is a valid block device, and isn't being used by anyone else,
2137 * set *vpp to the file's vnode.
2138 * You'll find the original of this in ccd.c
2139 */
2140 int
2141 raidlookup(char *path, struct proc *p, struct vnode **vpp)
2142 {
2143 struct nameidata nd;
2144 struct vnode *vp;
2145 struct vattr va;
2146 int error;
2147
2148 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, p);
2149 if ((error = vn_open(&nd, FREAD | FWRITE, 0)) != 0) {
2150 return (error);
2151 }
2152 vp = nd.ni_vp;
2153 if (vp->v_usecount > 1) {
2154 VOP_UNLOCK(vp, 0);
2155 (void) vn_close(vp, FREAD | FWRITE, p->p_ucred, p);
2156 return (EBUSY);
2157 }
2158 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0) {
2159 VOP_UNLOCK(vp, 0);
2160 (void) vn_close(vp, FREAD | FWRITE, p->p_ucred, p);
2161 return (error);
2162 }
2163 /* XXX: eventually we should handle VREG, too. */
2164 if (va.va_type != VBLK) {
2165 VOP_UNLOCK(vp, 0);
2166 (void) vn_close(vp, FREAD | FWRITE, p->p_ucred, p);
2167 return (ENOTBLK);
2168 }
2169 VOP_UNLOCK(vp, 0);
2170 *vpp = vp;
2171 return (0);
2172 }
2173 /*
2174 * Wait interruptibly for an exclusive lock.
2175 *
2176 * XXX
2177 * Several drivers do this; it should be abstracted and made MP-safe.
2178 * (Hmm... where have we seen this warning before :-> GO )
2179 */
2180 static int
2181 raidlock(struct raid_softc *rs)
2182 {
2183 int error;
2184
2185 while ((rs->sc_flags & RAIDF_LOCKED) != 0) {
2186 rs->sc_flags |= RAIDF_WANTED;
2187 if ((error =
2188 tsleep(rs, PRIBIO | PCATCH, "raidlck", 0)) != 0)
2189 return (error);
2190 }
2191 rs->sc_flags |= RAIDF_LOCKED;
2192 return (0);
2193 }
2194 /*
2195 * Unlock and wake up any waiters.
2196 */
2197 static void
2198 raidunlock(struct raid_softc *rs)
2199 {
2200
2201 rs->sc_flags &= ~RAIDF_LOCKED;
2202 if ((rs->sc_flags & RAIDF_WANTED) != 0) {
2203 rs->sc_flags &= ~RAIDF_WANTED;
2204 wakeup(rs);
2205 }
2206 }
2207
2208
2209 #define RF_COMPONENT_INFO_OFFSET 16384 /* bytes */
2210 #define RF_COMPONENT_INFO_SIZE 1024 /* bytes */
2211
2212 int
2213 raidmarkclean(dev_t dev, struct vnode *b_vp, int mod_counter)
2214 {
2215 RF_ComponentLabel_t clabel;
2216 raidread_component_label(dev, b_vp, &clabel);
2217 clabel.mod_counter = mod_counter;
2218 clabel.clean = RF_RAID_CLEAN;
2219 raidwrite_component_label(dev, b_vp, &clabel);
2220 return(0);
2221 }
2222
2223
2224 int
2225 raidmarkdirty(dev_t dev, struct vnode *b_vp, int mod_counter)
2226 {
2227 RF_ComponentLabel_t clabel;
2228 raidread_component_label(dev, b_vp, &clabel);
2229 clabel.mod_counter = mod_counter;
2230 clabel.clean = RF_RAID_DIRTY;
2231 raidwrite_component_label(dev, b_vp, &clabel);
2232 return(0);
2233 }
2234
2235 /* ARGSUSED */
2236 int
2237 raidread_component_label(dev_t dev, struct vnode *b_vp,
2238 RF_ComponentLabel_t *clabel)
2239 {
2240 struct buf *bp;
2241 const struct bdevsw *bdev;
2242 int error;
2243
2244 /* XXX should probably ensure that we don't try to do this if
2245 someone has changed rf_protected_sectors. */
2246
2247 if (b_vp == NULL) {
2248 /* For whatever reason, this component is not valid.
2249 Don't try to read a component label from it. */
2250 return(EINVAL);
2251 }
2252
2253 /* get a block of the appropriate size... */
2254 bp = geteblk((int)RF_COMPONENT_INFO_SIZE);
2255 bp->b_dev = dev;
2256
2257 /* get our ducks in a row for the read */
2258 bp->b_blkno = RF_COMPONENT_INFO_OFFSET / DEV_BSIZE;
2259 bp->b_bcount = RF_COMPONENT_INFO_SIZE;
2260 bp->b_flags |= B_READ;
2261 bp->b_resid = RF_COMPONENT_INFO_SIZE / DEV_BSIZE;
2262
2263 bdev = bdevsw_lookup(bp->b_dev);
2264 if (bdev == NULL)
2265 return (ENXIO);
2266 (*bdev->d_strategy)(bp);
2267
2268 error = biowait(bp);
2269
2270 if (!error) {
2271 memcpy(clabel, bp->b_data,
2272 sizeof(RF_ComponentLabel_t));
2273 }
2274
2275 brelse(bp);
2276 return(error);
2277 }
2278 /* ARGSUSED */
2279 int
2280 raidwrite_component_label(dev_t dev, struct vnode *b_vp,
2281 RF_ComponentLabel_t *clabel)
2282 {
2283 struct buf *bp;
2284 const struct bdevsw *bdev;
2285 int error;
2286
2287 /* get a block of the appropriate size... */
2288 bp = geteblk((int)RF_COMPONENT_INFO_SIZE);
2289 bp->b_dev = dev;
2290
2291 /* get our ducks in a row for the write */
2292 bp->b_blkno = RF_COMPONENT_INFO_OFFSET / DEV_BSIZE;
2293 bp->b_bcount = RF_COMPONENT_INFO_SIZE;
2294 bp->b_flags |= B_WRITE;
2295 bp->b_resid = RF_COMPONENT_INFO_SIZE / DEV_BSIZE;
2296
2297 memset(bp->b_data, 0, RF_COMPONENT_INFO_SIZE );
2298
2299 memcpy(bp->b_data, clabel, sizeof(RF_ComponentLabel_t));
2300
2301 bdev = bdevsw_lookup(bp->b_dev);
2302 if (bdev == NULL)
2303 return (ENXIO);
2304 (*bdev->d_strategy)(bp);
2305 error = biowait(bp);
2306 brelse(bp);
2307 if (error) {
2308 #if 1
2309 printf("Failed to write RAID component info!\n");
2310 #endif
2311 }
2312
2313 return(error);
2314 }
2315
2316 void
2317 rf_markalldirty(RF_Raid_t *raidPtr)
2318 {
2319 RF_ComponentLabel_t clabel;
2320 int sparecol;
2321 int c;
2322 int j;
2323 int scol = -1;
2324
2325 raidPtr->mod_counter++;
2326 for (c = 0; c < raidPtr->numCol; c++) {
2327 /* we don't want to touch (at all) a disk that has
2328 failed */
2329 if (!RF_DEAD_DISK(raidPtr->Disks[c].status)) {
2330 raidread_component_label(
2331 raidPtr->Disks[c].dev,
2332 raidPtr->raid_cinfo[c].ci_vp,
2333 &clabel);
2334 if (clabel.status == rf_ds_spared) {
2335 /* XXX do something special...
2336 but whatever you do, don't
2337 try to access it!! */
2338 } else {
2339 raidmarkdirty(
2340 raidPtr->Disks[c].dev,
2341 raidPtr->raid_cinfo[c].ci_vp,
2342 raidPtr->mod_counter);
2343 }
2344 }
2345 }
2346
2347 for( c = 0; c < raidPtr->numSpare ; c++) {
2348 sparecol = raidPtr->numCol + c;
2349 if (raidPtr->Disks[sparecol].status == rf_ds_used_spare) {
2350 /*
2351
2352 we claim this disk is "optimal" if it's
2353 rf_ds_used_spare, as that means it should be
2354 directly substitutable for the disk it replaced.
2355 We note that too...
2356
2357 */
2358
2359 for(j=0;j<raidPtr->numCol;j++) {
2360 if (raidPtr->Disks[j].spareCol == sparecol) {
2361 scol = j;
2362 break;
2363 }
2364 }
2365
2366 raidread_component_label(
2367 raidPtr->Disks[sparecol].dev,
2368 raidPtr->raid_cinfo[sparecol].ci_vp,
2369 &clabel);
2370 /* make sure status is noted */
2371
2372 raid_init_component_label(raidPtr, &clabel);
2373
2374 clabel.row = 0;
2375 clabel.column = scol;
2376 /* Note: we *don't* change status from rf_ds_used_spare
2377 to rf_ds_optimal */
2378 /* clabel.status = rf_ds_optimal; */
2379
2380 raidmarkdirty(raidPtr->Disks[sparecol].dev,
2381 raidPtr->raid_cinfo[sparecol].ci_vp,
2382 raidPtr->mod_counter);
2383 }
2384 }
2385 }
2386
2387
2388 void
2389 rf_update_component_labels(RF_Raid_t *raidPtr, int final)
2390 {
2391 RF_ComponentLabel_t clabel;
2392 int sparecol;
2393 int c;
2394 int j;
2395 int scol;
2396
2397 scol = -1;
2398
2399 /* XXX should do extra checks to make sure things really are clean,
2400 rather than blindly setting the clean bit... */
2401
2402 raidPtr->mod_counter++;
2403
2404 for (c = 0; c < raidPtr->numCol; c++) {
2405 if (raidPtr->Disks[c].status == rf_ds_optimal) {
2406 raidread_component_label(
2407 raidPtr->Disks[c].dev,
2408 raidPtr->raid_cinfo[c].ci_vp,
2409 &clabel);
2410 /* make sure status is noted */
2411 clabel.status = rf_ds_optimal;
2412 /* bump the counter */
2413 clabel.mod_counter = raidPtr->mod_counter;
2414
2415 raidwrite_component_label(
2416 raidPtr->Disks[c].dev,
2417 raidPtr->raid_cinfo[c].ci_vp,
2418 &clabel);
2419 if (final == RF_FINAL_COMPONENT_UPDATE) {
2420 if (raidPtr->parity_good == RF_RAID_CLEAN) {
2421 raidmarkclean(
2422 raidPtr->Disks[c].dev,
2423 raidPtr->raid_cinfo[c].ci_vp,
2424 raidPtr->mod_counter);
2425 }
2426 }
2427 }
2428 /* else we don't touch it.. */
2429 }
2430
2431 for( c = 0; c < raidPtr->numSpare ; c++) {
2432 sparecol = raidPtr->numCol + c;
2433 /* Need to ensure that the reconstruct actually completed! */
2434 if (raidPtr->Disks[sparecol].status == rf_ds_used_spare) {
2435 /*
2436
2437 we claim this disk is "optimal" if it's
2438 rf_ds_used_spare, as that means it should be
2439 directly substitutable for the disk it replaced.
2440 We note that too...
2441
2442 */
2443
2444 for(j=0;j<raidPtr->numCol;j++) {
2445 if (raidPtr->Disks[j].spareCol == sparecol) {
2446 scol = j;
2447 break;
2448 }
2449 }
2450
2451 /* XXX shouldn't *really* need this... */
2452 raidread_component_label(
2453 raidPtr->Disks[sparecol].dev,
2454 raidPtr->raid_cinfo[sparecol].ci_vp,
2455 &clabel);
2456 /* make sure status is noted */
2457
2458 raid_init_component_label(raidPtr, &clabel);
2459
2460 clabel.mod_counter = raidPtr->mod_counter;
2461 clabel.column = scol;
2462 clabel.status = rf_ds_optimal;
2463
2464 raidwrite_component_label(
2465 raidPtr->Disks[sparecol].dev,
2466 raidPtr->raid_cinfo[sparecol].ci_vp,
2467 &clabel);
2468 if (final == RF_FINAL_COMPONENT_UPDATE) {
2469 if (raidPtr->parity_good == RF_RAID_CLEAN) {
2470 raidmarkclean( raidPtr->Disks[sparecol].dev,
2471 raidPtr->raid_cinfo[sparecol].ci_vp,
2472 raidPtr->mod_counter);
2473 }
2474 }
2475 }
2476 }
2477 }
2478
2479 void
2480 rf_close_component(RF_Raid_t *raidPtr, struct vnode *vp, int auto_configured)
2481 {
2482 struct proc *p;
2483
2484 p = raidPtr->engine_thread;
2485
2486 if (vp != NULL) {
2487 if (auto_configured == 1) {
2488 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2489 VOP_CLOSE(vp, FREAD | FWRITE, NOCRED, 0);
2490 vput(vp);
2491
2492 } else {
2493 (void) vn_close(vp, FREAD | FWRITE, p->p_ucred, p);
2494 }
2495 }
2496 }
2497
2498
2499 void
2500 rf_UnconfigureVnodes(RF_Raid_t *raidPtr)
2501 {
2502 int r,c;
2503 struct vnode *vp;
2504 int acd;
2505
2506
2507 /* We take this opportunity to close the vnodes like we should.. */
2508
2509 for (c = 0; c < raidPtr->numCol; c++) {
2510 vp = raidPtr->raid_cinfo[c].ci_vp;
2511 acd = raidPtr->Disks[c].auto_configured;
2512 rf_close_component(raidPtr, vp, acd);
2513 raidPtr->raid_cinfo[c].ci_vp = NULL;
2514 raidPtr->Disks[c].auto_configured = 0;
2515 }
2516
2517 for (r = 0; r < raidPtr->numSpare; r++) {
2518 vp = raidPtr->raid_cinfo[raidPtr->numCol + r].ci_vp;
2519 acd = raidPtr->Disks[raidPtr->numCol + r].auto_configured;
2520 rf_close_component(raidPtr, vp, acd);
2521 raidPtr->raid_cinfo[raidPtr->numCol + r].ci_vp = NULL;
2522 raidPtr->Disks[raidPtr->numCol + r].auto_configured = 0;
2523 }
2524 }
2525
2526
2527 void
2528 rf_ReconThread(struct rf_recon_req *req)
2529 {
2530 int s;
2531 RF_Raid_t *raidPtr;
2532
2533 s = splbio();
2534 raidPtr = (RF_Raid_t *) req->raidPtr;
2535 raidPtr->recon_in_progress = 1;
2536
2537 rf_FailDisk((RF_Raid_t *) req->raidPtr, req->col,
2538 ((req->flags & RF_FDFLAGS_RECON) ? 1 : 0));
2539
2540 RF_Free(req, sizeof(*req));
2541
2542 raidPtr->recon_in_progress = 0;
2543 splx(s);
2544
2545 /* That's all... */
2546 kthread_exit(0); /* does not return */
2547 }
2548
2549 void
2550 rf_RewriteParityThread(RF_Raid_t *raidPtr)
2551 {
2552 int retcode;
2553 int s;
2554
2555 raidPtr->parity_rewrite_stripes_done = 0;
2556 raidPtr->parity_rewrite_in_progress = 1;
2557 s = splbio();
2558 retcode = rf_RewriteParity(raidPtr);
2559 splx(s);
2560 if (retcode) {
2561 printf("raid%d: Error re-writing parity!\n",raidPtr->raidid);
2562 } else {
2563 /* set the clean bit! If we shutdown correctly,
2564 the clean bit on each component label will get
2565 set */
2566 raidPtr->parity_good = RF_RAID_CLEAN;
2567 }
2568 raidPtr->parity_rewrite_in_progress = 0;
2569
2570 /* Anyone waiting for us to stop? If so, inform them... */
2571 if (raidPtr->waitShutdown) {
2572 wakeup(&raidPtr->parity_rewrite_in_progress);
2573 }
2574
2575 /* That's all... */
2576 kthread_exit(0); /* does not return */
2577 }
2578
2579
2580 void
2581 rf_CopybackThread(RF_Raid_t *raidPtr)
2582 {
2583 int s;
2584
2585 raidPtr->copyback_in_progress = 1;
2586 s = splbio();
2587 rf_CopybackReconstructedData(raidPtr);
2588 splx(s);
2589 raidPtr->copyback_in_progress = 0;
2590
2591 /* That's all... */
2592 kthread_exit(0); /* does not return */
2593 }
2594
2595
2596 void
2597 rf_ReconstructInPlaceThread(struct rf_recon_req *req)
2598 {
2599 int s;
2600 RF_Raid_t *raidPtr;
2601
2602 s = splbio();
2603 raidPtr = req->raidPtr;
2604 raidPtr->recon_in_progress = 1;
2605 rf_ReconstructInPlace(raidPtr, req->col);
2606 RF_Free(req, sizeof(*req));
2607 raidPtr->recon_in_progress = 0;
2608 splx(s);
2609
2610 /* That's all... */
2611 kthread_exit(0); /* does not return */
2612 }
2613
2614 RF_AutoConfig_t *
2615 rf_find_raid_components()
2616 {
2617 struct vnode *vp;
2618 struct disklabel label;
2619 struct device *dv;
2620 dev_t dev;
2621 int bmajor;
2622 int error;
2623 int i;
2624 int good_one;
2625 RF_ComponentLabel_t *clabel;
2626 RF_AutoConfig_t *ac_list;
2627 RF_AutoConfig_t *ac;
2628
2629
2630 /* initialize the AutoConfig list */
2631 ac_list = NULL;
2632
2633 /* we begin by trolling through *all* the devices on the system */
2634
2635 for (dv = alldevs.tqh_first; dv != NULL;
2636 dv = dv->dv_list.tqe_next) {
2637
2638 /* we are only interested in disks... */
2639 if (dv->dv_class != DV_DISK)
2640 continue;
2641
2642 /* we don't care about floppies... */
2643 if (!strcmp(dv->dv_cfdata->cf_name,"fd")) {
2644 continue;
2645 }
2646
2647 /* we don't care about CD's... */
2648 if (!strcmp(dv->dv_cfdata->cf_name,"cd")) {
2649 continue;
2650 }
2651
2652 /* hdfd is the Atari/Hades floppy driver */
2653 if (!strcmp(dv->dv_cfdata->cf_name,"hdfd")) {
2654 continue;
2655 }
2656 /* fdisa is the Atari/Milan floppy driver */
2657 if (!strcmp(dv->dv_cfdata->cf_name,"fdisa")) {
2658 continue;
2659 }
2660
2661 /* need to find the device_name_to_block_device_major stuff */
2662 bmajor = devsw_name2blk(dv->dv_xname, NULL, 0);
2663
2664 /* get a vnode for the raw partition of this disk */
2665
2666 dev = MAKEDISKDEV(bmajor, dv->dv_unit, RAW_PART);
2667 if (bdevvp(dev, &vp))
2668 panic("RAID can't alloc vnode");
2669
2670 error = VOP_OPEN(vp, FREAD, NOCRED, 0);
2671
2672 if (error) {
2673 /* "Who cares." Continue looking
2674 for something that exists*/
2675 vput(vp);
2676 continue;
2677 }
2678
2679 /* Ok, the disk exists. Go get the disklabel. */
2680 error = VOP_IOCTL(vp, DIOCGDINFO, &label, FREAD, NOCRED, 0);
2681 if (error) {
2682 /*
2683 * XXX can't happen - open() would
2684 * have errored out (or faked up one)
2685 */
2686 if (error != ENOTTY)
2687 printf("RAIDframe: can't get label for dev "
2688 "%s (%d)\n", dv->dv_xname, error);
2689 }
2690
2691 /* don't need this any more. We'll allocate it again
2692 a little later if we really do... */
2693 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2694 VOP_CLOSE(vp, FREAD | FWRITE, NOCRED, 0);
2695 vput(vp);
2696
2697 if (error)
2698 continue;
2699
2700 for (i=0; i < label.d_npartitions; i++) {
2701 /* We only support partitions marked as RAID */
2702 if (label.d_partitions[i].p_fstype != FS_RAID)
2703 continue;
2704
2705 dev = MAKEDISKDEV(bmajor, dv->dv_unit, i);
2706 if (bdevvp(dev, &vp))
2707 panic("RAID can't alloc vnode");
2708
2709 error = VOP_OPEN(vp, FREAD, NOCRED, 0);
2710 if (error) {
2711 /* Whatever... */
2712 vput(vp);
2713 continue;
2714 }
2715
2716 good_one = 0;
2717
2718 clabel = (RF_ComponentLabel_t *)
2719 malloc(sizeof(RF_ComponentLabel_t),
2720 M_RAIDFRAME, M_NOWAIT);
2721 if (clabel == NULL) {
2722 /* XXX CLEANUP HERE */
2723 printf("RAID auto config: out of memory!\n");
2724 return(NULL); /* XXX probably should panic? */
2725 }
2726
2727 if (!raidread_component_label(dev, vp, clabel)) {
2728 /* Got the label. Does it look reasonable? */
2729 if (rf_reasonable_label(clabel) &&
2730 (clabel->partitionSize <=
2731 label.d_partitions[i].p_size)) {
2732 #if DEBUG
2733 printf("Component on: %s%c: %d\n",
2734 dv->dv_xname, 'a'+i,
2735 label.d_partitions[i].p_size);
2736 rf_print_component_label(clabel);
2737 #endif
2738 /* if it's reasonable, add it,
2739 else ignore it. */
2740 ac = (RF_AutoConfig_t *)
2741 malloc(sizeof(RF_AutoConfig_t),
2742 M_RAIDFRAME,
2743 M_NOWAIT);
2744 if (ac == NULL) {
2745 /* XXX should panic?? */
2746 return(NULL);
2747 }
2748
2749 snprintf(ac->devname,
2750 sizeof(ac->devname), "%s%c",
2751 dv->dv_xname, 'a'+i);
2752 ac->dev = dev;
2753 ac->vp = vp;
2754 ac->clabel = clabel;
2755 ac->next = ac_list;
2756 ac_list = ac;
2757 good_one = 1;
2758 }
2759 }
2760 if (!good_one) {
2761 /* cleanup */
2762 free(clabel, M_RAIDFRAME);
2763 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2764 VOP_CLOSE(vp, FREAD | FWRITE, NOCRED, 0);
2765 vput(vp);
2766 }
2767 }
2768 }
2769 return(ac_list);
2770 }
2771
2772 static int
2773 rf_reasonable_label(RF_ComponentLabel_t *clabel)
2774 {
2775
2776 if (((clabel->version==RF_COMPONENT_LABEL_VERSION_1) ||
2777 (clabel->version==RF_COMPONENT_LABEL_VERSION)) &&
2778 ((clabel->clean == RF_RAID_CLEAN) ||
2779 (clabel->clean == RF_RAID_DIRTY)) &&
2780 clabel->row >=0 &&
2781 clabel->column >= 0 &&
2782 clabel->num_rows > 0 &&
2783 clabel->num_columns > 0 &&
2784 clabel->row < clabel->num_rows &&
2785 clabel->column < clabel->num_columns &&
2786 clabel->blockSize > 0 &&
2787 clabel->numBlocks > 0) {
2788 /* label looks reasonable enough... */
2789 return(1);
2790 }
2791 return(0);
2792 }
2793
2794
2795 #if DEBUG
2796 void
2797 rf_print_component_label(RF_ComponentLabel_t *clabel)
2798 {
2799 printf(" Row: %d Column: %d Num Rows: %d Num Columns: %d\n",
2800 clabel->row, clabel->column,
2801 clabel->num_rows, clabel->num_columns);
2802 printf(" Version: %d Serial Number: %d Mod Counter: %d\n",
2803 clabel->version, clabel->serial_number,
2804 clabel->mod_counter);
2805 printf(" Clean: %s Status: %d\n",
2806 clabel->clean ? "Yes" : "No", clabel->status );
2807 printf(" sectPerSU: %d SUsPerPU: %d SUsPerRU: %d\n",
2808 clabel->sectPerSU, clabel->SUsPerPU, clabel->SUsPerRU);
2809 printf(" RAID Level: %c blocksize: %d numBlocks: %d\n",
2810 (char) clabel->parityConfig, clabel->blockSize,
2811 clabel->numBlocks);
2812 printf(" Autoconfig: %s\n", clabel->autoconfigure ? "Yes" : "No" );
2813 printf(" Contains root partition: %s\n",
2814 clabel->root_partition ? "Yes" : "No" );
2815 printf(" Last configured as: raid%d\n", clabel->last_unit );
2816 #if 0
2817 printf(" Config order: %d\n", clabel->config_order);
2818 #endif
2819
2820 }
2821 #endif
2822
2823 RF_ConfigSet_t *
2824 rf_create_auto_sets(RF_AutoConfig_t *ac_list)
2825 {
2826 RF_AutoConfig_t *ac;
2827 RF_ConfigSet_t *config_sets;
2828 RF_ConfigSet_t *cset;
2829 RF_AutoConfig_t *ac_next;
2830
2831
2832 config_sets = NULL;
2833
2834 /* Go through the AutoConfig list, and figure out which components
2835 belong to what sets. */
2836 ac = ac_list;
2837 while(ac!=NULL) {
2838 /* we're going to putz with ac->next, so save it here
2839 for use at the end of the loop */
2840 ac_next = ac->next;
2841
2842 if (config_sets == NULL) {
2843 /* will need at least this one... */
2844 config_sets = (RF_ConfigSet_t *)
2845 malloc(sizeof(RF_ConfigSet_t),
2846 M_RAIDFRAME, M_NOWAIT);
2847 if (config_sets == NULL) {
2848 panic("rf_create_auto_sets: No memory!");
2849 }
2850 /* this one is easy :) */
2851 config_sets->ac = ac;
2852 config_sets->next = NULL;
2853 config_sets->rootable = 0;
2854 ac->next = NULL;
2855 } else {
2856 /* which set does this component fit into? */
2857 cset = config_sets;
2858 while(cset!=NULL) {
2859 if (rf_does_it_fit(cset, ac)) {
2860 /* looks like it matches... */
2861 ac->next = cset->ac;
2862 cset->ac = ac;
2863 break;
2864 }
2865 cset = cset->next;
2866 }
2867 if (cset==NULL) {
2868 /* didn't find a match above... new set..*/
2869 cset = (RF_ConfigSet_t *)
2870 malloc(sizeof(RF_ConfigSet_t),
2871 M_RAIDFRAME, M_NOWAIT);
2872 if (cset == NULL) {
2873 panic("rf_create_auto_sets: No memory!");
2874 }
2875 cset->ac = ac;
2876 ac->next = NULL;
2877 cset->next = config_sets;
2878 cset->rootable = 0;
2879 config_sets = cset;
2880 }
2881 }
2882 ac = ac_next;
2883 }
2884
2885
2886 return(config_sets);
2887 }
2888
2889 static int
2890 rf_does_it_fit(RF_ConfigSet_t *cset, RF_AutoConfig_t *ac)
2891 {
2892 RF_ComponentLabel_t *clabel1, *clabel2;
2893
2894 /* If this one matches the *first* one in the set, that's good
2895 enough, since the other members of the set would have been
2896 through here too... */
2897 /* note that we are not checking partitionSize here..
2898
2899 Note that we are also not checking the mod_counters here.
2900 If everything else matches execpt the mod_counter, that's
2901 good enough for this test. We will deal with the mod_counters
2902 a little later in the autoconfiguration process.
2903
2904 (clabel1->mod_counter == clabel2->mod_counter) &&
2905
2906 The reason we don't check for this is that failed disks
2907 will have lower modification counts. If those disks are
2908 not added to the set they used to belong to, then they will
2909 form their own set, which may result in 2 different sets,
2910 for example, competing to be configured at raid0, and
2911 perhaps competing to be the root filesystem set. If the
2912 wrong ones get configured, or both attempt to become /,
2913 weird behaviour and or serious lossage will occur. Thus we
2914 need to bring them into the fold here, and kick them out at
2915 a later point.
2916
2917 */
2918
2919 clabel1 = cset->ac->clabel;
2920 clabel2 = ac->clabel;
2921 if ((clabel1->version == clabel2->version) &&
2922 (clabel1->serial_number == clabel2->serial_number) &&
2923 (clabel1->num_rows == clabel2->num_rows) &&
2924 (clabel1->num_columns == clabel2->num_columns) &&
2925 (clabel1->sectPerSU == clabel2->sectPerSU) &&
2926 (clabel1->SUsPerPU == clabel2->SUsPerPU) &&
2927 (clabel1->SUsPerRU == clabel2->SUsPerRU) &&
2928 (clabel1->parityConfig == clabel2->parityConfig) &&
2929 (clabel1->maxOutstanding == clabel2->maxOutstanding) &&
2930 (clabel1->blockSize == clabel2->blockSize) &&
2931 (clabel1->numBlocks == clabel2->numBlocks) &&
2932 (clabel1->autoconfigure == clabel2->autoconfigure) &&
2933 (clabel1->root_partition == clabel2->root_partition) &&
2934 (clabel1->last_unit == clabel2->last_unit) &&
2935 (clabel1->config_order == clabel2->config_order)) {
2936 /* if it get's here, it almost *has* to be a match */
2937 } else {
2938 /* it's not consistent with somebody in the set..
2939 punt */
2940 return(0);
2941 }
2942 /* all was fine.. it must fit... */
2943 return(1);
2944 }
2945
2946 int
2947 rf_have_enough_components(RF_ConfigSet_t *cset)
2948 {
2949 RF_AutoConfig_t *ac;
2950 RF_AutoConfig_t *auto_config;
2951 RF_ComponentLabel_t *clabel;
2952 int c;
2953 int num_cols;
2954 int num_missing;
2955 int mod_counter;
2956 int mod_counter_found;
2957 int even_pair_failed;
2958 char parity_type;
2959
2960
2961 /* check to see that we have enough 'live' components
2962 of this set. If so, we can configure it if necessary */
2963
2964 num_cols = cset->ac->clabel->num_columns;
2965 parity_type = cset->ac->clabel->parityConfig;
2966
2967 /* XXX Check for duplicate components!?!?!? */
2968
2969 /* Determine what the mod_counter is supposed to be for this set. */
2970
2971 mod_counter_found = 0;
2972 mod_counter = 0;
2973 ac = cset->ac;
2974 while(ac!=NULL) {
2975 if (mod_counter_found==0) {
2976 mod_counter = ac->clabel->mod_counter;
2977 mod_counter_found = 1;
2978 } else {
2979 if (ac->clabel->mod_counter > mod_counter) {
2980 mod_counter = ac->clabel->mod_counter;
2981 }
2982 }
2983 ac = ac->next;
2984 }
2985
2986 num_missing = 0;
2987 auto_config = cset->ac;
2988
2989 even_pair_failed = 0;
2990 for(c=0; c<num_cols; c++) {
2991 ac = auto_config;
2992 while(ac!=NULL) {
2993 if ((ac->clabel->column == c) &&
2994 (ac->clabel->mod_counter == mod_counter)) {
2995 /* it's this one... */
2996 #if DEBUG
2997 printf("Found: %s at %d\n",
2998 ac->devname,c);
2999 #endif
3000 break;
3001 }
3002 ac=ac->next;
3003 }
3004 if (ac==NULL) {
3005 /* Didn't find one here! */
3006 /* special case for RAID 1, especially
3007 where there are more than 2
3008 components (where RAIDframe treats
3009 things a little differently :( ) */
3010 if (parity_type == '1') {
3011 if (c%2 == 0) { /* even component */
3012 even_pair_failed = 1;
3013 } else { /* odd component. If
3014 we're failed, and
3015 so is the even
3016 component, it's
3017 "Good Night, Charlie" */
3018 if (even_pair_failed == 1) {
3019 return(0);
3020 }
3021 }
3022 } else {
3023 /* normal accounting */
3024 num_missing++;
3025 }
3026 }
3027 if ((parity_type == '1') && (c%2 == 1)) {
3028 /* Just did an even component, and we didn't
3029 bail.. reset the even_pair_failed flag,
3030 and go on to the next component.... */
3031 even_pair_failed = 0;
3032 }
3033 }
3034
3035 clabel = cset->ac->clabel;
3036
3037 if (((clabel->parityConfig == '0') && (num_missing > 0)) ||
3038 ((clabel->parityConfig == '4') && (num_missing > 1)) ||
3039 ((clabel->parityConfig == '5') && (num_missing > 1))) {
3040 /* XXX this needs to be made *much* more general */
3041 /* Too many failures */
3042 return(0);
3043 }
3044 /* otherwise, all is well, and we've got enough to take a kick
3045 at autoconfiguring this set */
3046 return(1);
3047 }
3048
3049 void
3050 rf_create_configuration(RF_AutoConfig_t *ac, RF_Config_t *config,
3051 RF_Raid_t *raidPtr)
3052 {
3053 RF_ComponentLabel_t *clabel;
3054 int i;
3055
3056 clabel = ac->clabel;
3057
3058 /* 1. Fill in the common stuff */
3059 config->numRow = clabel->num_rows = 1;
3060 config->numCol = clabel->num_columns;
3061 config->numSpare = 0; /* XXX should this be set here? */
3062 config->sectPerSU = clabel->sectPerSU;
3063 config->SUsPerPU = clabel->SUsPerPU;
3064 config->SUsPerRU = clabel->SUsPerRU;
3065 config->parityConfig = clabel->parityConfig;
3066 /* XXX... */
3067 strcpy(config->diskQueueType,"fifo");
3068 config->maxOutstandingDiskReqs = clabel->maxOutstanding;
3069 config->layoutSpecificSize = 0; /* XXX ?? */
3070
3071 while(ac!=NULL) {
3072 /* row/col values will be in range due to the checks
3073 in reasonable_label() */
3074 strcpy(config->devnames[0][ac->clabel->column],
3075 ac->devname);
3076 ac = ac->next;
3077 }
3078
3079 for(i=0;i<RF_MAXDBGV;i++) {
3080 config->debugVars[i][0] = 0;
3081 }
3082 }
3083
3084 int
3085 rf_set_autoconfig(RF_Raid_t *raidPtr, int new_value)
3086 {
3087 RF_ComponentLabel_t clabel;
3088 struct vnode *vp;
3089 dev_t dev;
3090 int column;
3091 int sparecol;
3092
3093 raidPtr->autoconfigure = new_value;
3094
3095 for(column=0; column<raidPtr->numCol; column++) {
3096 if (raidPtr->Disks[column].status == rf_ds_optimal) {
3097 dev = raidPtr->Disks[column].dev;
3098 vp = raidPtr->raid_cinfo[column].ci_vp;
3099 raidread_component_label(dev, vp, &clabel);
3100 clabel.autoconfigure = new_value;
3101 raidwrite_component_label(dev, vp, &clabel);
3102 }
3103 }
3104 for(column = 0; column < raidPtr->numSpare ; column++) {
3105 sparecol = raidPtr->numCol + column;
3106 if (raidPtr->Disks[sparecol].status == rf_ds_used_spare) {
3107 dev = raidPtr->Disks[sparecol].dev;
3108 vp = raidPtr->raid_cinfo[sparecol].ci_vp;
3109 raidread_component_label(dev, vp, &clabel);
3110 clabel.autoconfigure = new_value;
3111 raidwrite_component_label(dev, vp, &clabel);
3112 }
3113 }
3114 return(new_value);
3115 }
3116
3117 int
3118 rf_set_rootpartition(RF_Raid_t *raidPtr, int new_value)
3119 {
3120 RF_ComponentLabel_t clabel;
3121 struct vnode *vp;
3122 dev_t dev;
3123 int column;
3124 int sparecol;
3125
3126 raidPtr->root_partition = new_value;
3127 for(column=0; column<raidPtr->numCol; column++) {
3128 if (raidPtr->Disks[column].status == rf_ds_optimal) {
3129 dev = raidPtr->Disks[column].dev;
3130 vp = raidPtr->raid_cinfo[column].ci_vp;
3131 raidread_component_label(dev, vp, &clabel);
3132 clabel.root_partition = new_value;
3133 raidwrite_component_label(dev, vp, &clabel);
3134 }
3135 }
3136 for(column = 0; column < raidPtr->numSpare ; column++) {
3137 sparecol = raidPtr->numCol + column;
3138 if (raidPtr->Disks[sparecol].status == rf_ds_used_spare) {
3139 dev = raidPtr->Disks[sparecol].dev;
3140 vp = raidPtr->raid_cinfo[sparecol].ci_vp;
3141 raidread_component_label(dev, vp, &clabel);
3142 clabel.root_partition = new_value;
3143 raidwrite_component_label(dev, vp, &clabel);
3144 }
3145 }
3146 return(new_value);
3147 }
3148
3149 void
3150 rf_release_all_vps(RF_ConfigSet_t *cset)
3151 {
3152 RF_AutoConfig_t *ac;
3153
3154 ac = cset->ac;
3155 while(ac!=NULL) {
3156 /* Close the vp, and give it back */
3157 if (ac->vp) {
3158 vn_lock(ac->vp, LK_EXCLUSIVE | LK_RETRY);
3159 VOP_CLOSE(ac->vp, FREAD, NOCRED, 0);
3160 vput(ac->vp);
3161 ac->vp = NULL;
3162 }
3163 ac = ac->next;
3164 }
3165 }
3166
3167
3168 void
3169 rf_cleanup_config_set(RF_ConfigSet_t *cset)
3170 {
3171 RF_AutoConfig_t *ac;
3172 RF_AutoConfig_t *next_ac;
3173
3174 ac = cset->ac;
3175 while(ac!=NULL) {
3176 next_ac = ac->next;
3177 /* nuke the label */
3178 free(ac->clabel, M_RAIDFRAME);
3179 /* cleanup the config structure */
3180 free(ac, M_RAIDFRAME);
3181 /* "next.." */
3182 ac = next_ac;
3183 }
3184 /* and, finally, nuke the config set */
3185 free(cset, M_RAIDFRAME);
3186 }
3187
3188
3189 void
3190 raid_init_component_label(RF_Raid_t *raidPtr, RF_ComponentLabel_t *clabel)
3191 {
3192 /* current version number */
3193 clabel->version = RF_COMPONENT_LABEL_VERSION;
3194 clabel->serial_number = raidPtr->serial_number;
3195 clabel->mod_counter = raidPtr->mod_counter;
3196 clabel->num_rows = 1;
3197 clabel->num_columns = raidPtr->numCol;
3198 clabel->clean = RF_RAID_DIRTY; /* not clean */
3199 clabel->status = rf_ds_optimal; /* "It's good!" */
3200
3201 clabel->sectPerSU = raidPtr->Layout.sectorsPerStripeUnit;
3202 clabel->SUsPerPU = raidPtr->Layout.SUsPerPU;
3203 clabel->SUsPerRU = raidPtr->Layout.SUsPerRU;
3204
3205 clabel->blockSize = raidPtr->bytesPerSector;
3206 clabel->numBlocks = raidPtr->sectorsPerDisk;
3207
3208 /* XXX not portable */
3209 clabel->parityConfig = raidPtr->Layout.map->parityConfig;
3210 clabel->maxOutstanding = raidPtr->maxOutstanding;
3211 clabel->autoconfigure = raidPtr->autoconfigure;
3212 clabel->root_partition = raidPtr->root_partition;
3213 clabel->last_unit = raidPtr->raidid;
3214 clabel->config_order = raidPtr->config_order;
3215 }
3216
3217 int
3218 rf_auto_config_set(RF_ConfigSet_t *cset, int *unit)
3219 {
3220 RF_Raid_t *raidPtr;
3221 RF_Config_t *config;
3222 int raidID;
3223 int retcode;
3224
3225 #if DEBUG
3226 printf("RAID autoconfigure\n");
3227 #endif
3228
3229 retcode = 0;
3230 *unit = -1;
3231
3232 /* 1. Create a config structure */
3233
3234 config = (RF_Config_t *)malloc(sizeof(RF_Config_t),
3235 M_RAIDFRAME,
3236 M_NOWAIT);
3237 if (config==NULL) {
3238 printf("Out of mem!?!?\n");
3239 /* XXX do something more intelligent here. */
3240 return(1);
3241 }
3242
3243 memset(config, 0, sizeof(RF_Config_t));
3244
3245 /*
3246 2. Figure out what RAID ID this one is supposed to live at
3247 See if we can get the same RAID dev that it was configured
3248 on last time..
3249 */
3250
3251 raidID = cset->ac->clabel->last_unit;
3252 if ((raidID < 0) || (raidID >= numraid)) {
3253 /* let's not wander off into lala land. */
3254 raidID = numraid - 1;
3255 }
3256 if (raidPtrs[raidID]->valid != 0) {
3257
3258 /*
3259 Nope... Go looking for an alternative...
3260 Start high so we don't immediately use raid0 if that's
3261 not taken.
3262 */
3263
3264 for(raidID = numraid - 1; raidID >= 0; raidID--) {
3265 if (raidPtrs[raidID]->valid == 0) {
3266 /* can use this one! */
3267 break;
3268 }
3269 }
3270 }
3271
3272 if (raidID < 0) {
3273 /* punt... */
3274 printf("Unable to auto configure this set!\n");
3275 printf("(Out of RAID devs!)\n");
3276 return(1);
3277 }
3278
3279 #if DEBUG
3280 printf("Configuring raid%d:\n",raidID);
3281 #endif
3282
3283 raidPtr = raidPtrs[raidID];
3284
3285 /* XXX all this stuff should be done SOMEWHERE ELSE! */
3286 raidPtr->raidid = raidID;
3287 raidPtr->openings = RAIDOUTSTANDING;
3288
3289 /* 3. Build the configuration structure */
3290 rf_create_configuration(cset->ac, config, raidPtr);
3291
3292 /* 4. Do the configuration */
3293 retcode = rf_Configure(raidPtr, config, cset->ac);
3294
3295 if (retcode == 0) {
3296
3297 raidinit(raidPtrs[raidID]);
3298
3299 rf_markalldirty(raidPtrs[raidID]);
3300 raidPtrs[raidID]->autoconfigure = 1; /* XXX do this here? */
3301 if (cset->ac->clabel->root_partition==1) {
3302 /* everything configured just fine. Make a note
3303 that this set is eligible to be root. */
3304 cset->rootable = 1;
3305 /* XXX do this here? */
3306 raidPtrs[raidID]->root_partition = 1;
3307 }
3308 }
3309
3310 /* 5. Cleanup */
3311 free(config, M_RAIDFRAME);
3312
3313 *unit = raidID;
3314 return(retcode);
3315 }
3316
3317 void
3318 rf_disk_unbusy(RF_RaidAccessDesc_t *desc)
3319 {
3320 struct buf *bp;
3321
3322 bp = (struct buf *)desc->bp;
3323 disk_unbusy(&raid_softc[desc->raidPtr->raidid].sc_dkdev,
3324 (bp->b_bcount - bp->b_resid), (bp->b_flags & B_READ));
3325 }
3326
3327 void
3328 rf_pool_init(struct pool *p, size_t size, const char *w_chan,
3329 size_t xmin, size_t xmax)
3330 {
3331 pool_init(p, size, 0, 0, 0, w_chan, NULL);
3332 pool_sethiwat(p, xmax);
3333 pool_prime(p, xmin);
3334 pool_setlowat(p, xmin);
3335 }
3336
3337 /*
3338 * rf_buf_queue_check(int raidid) -- looks into the buf_queue to see
3339 * if there is IO pending and if that IO could possibly be done for a
3340 * given RAID set. Returns 0 if IO is waiting and can be done, 1
3341 * otherwise.
3342 *
3343 */
3344
3345 int
3346 rf_buf_queue_check(int raidid)
3347 {
3348 if ((BUFQ_PEEK(&(raid_softc[raidid].buf_queue)) != NULL) &&
3349 raidPtrs[raidid]->openings > 0) {
3350 /* there is work to do */
3351 return 0;
3352 }
3353 /* default is nothing to do */
3354 return 1;
3355 }
3356