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