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