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