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