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