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