ccd.c revision 1.24 1 /* $NetBSD: ccd.c,v 1.24 1996/02/01 20:47:13 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1995, 1996 Jason R. Thorpe.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project
18 * by Jason R. Thorpe.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*
36 * Copyright (c) 1988 University of Utah.
37 * Copyright (c) 1990, 1993
38 * The Regents of the University of California. All rights reserved.
39 *
40 * This code is derived from software contributed to Berkeley by
41 * the Systems Programming Group of the University of Utah Computer
42 * Science Department.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * from: Utah $Hdr: cd.c 1.6 90/11/28$
73 *
74 * @(#)cd.c 8.2 (Berkeley) 11/16/93
75 */
76
77 /*
78 * "Concatenated" disk driver.
79 *
80 * Dynamic configuration and disklabel support by:
81 * Jason R. Thorpe <thorpej (at) nas.nasa.gov>
82 * Numerical Aerodynamic Simulation Facility
83 * Mail Stop 258-6
84 * NASA Ames Research Center
85 * Moffett Field, CA 94035
86 *
87 * Mirroring support based on code written by Satoshi Asami
88 * and Nisha Talagala.
89 */
90
91 #include <sys/param.h>
92 #include <sys/systm.h>
93 #include <sys/proc.h>
94 #include <sys/errno.h>
95 #include <sys/buf.h>
96 #include <sys/malloc.h>
97 #include <sys/namei.h>
98 #include <sys/conf.h>
99 #include <sys/stat.h>
100 #include <sys/ioctl.h>
101 #include <sys/disklabel.h>
102 #include <sys/device.h>
103 #include <sys/disk.h>
104 #include <sys/syslog.h>
105 #include <sys/fcntl.h>
106 #include <sys/vnode.h>
107
108 #include <dev/ccdvar.h>
109
110 #if defined(CCDDEBUG) && !defined(DEBUG)
111 #define DEBUG
112 #endif
113
114 #ifdef DEBUG
115 #define CCDB_FOLLOW 0x01
116 #define CCDB_INIT 0x02
117 #define CCDB_IO 0x04
118 #define CCDB_LABEL 0x08
119 #define CCDB_VNODE 0x10
120 int ccddebug = 0x00;
121 #endif
122
123 #define ccdunit(x) DISKUNIT(x)
124
125 struct ccdbuf {
126 struct buf cb_buf; /* new I/O buf */
127 struct buf *cb_obp; /* ptr. to original I/O buf */
128 int cb_unit; /* target unit */
129 int cb_comp; /* target component */
130 int cb_flags; /* misc. flags */
131
132 #define CBF_MIRROR 0x01 /* we're for a mirror component */
133 };
134
135 #define getccdbuf() \
136 ((struct ccdbuf *)malloc(sizeof(struct ccdbuf), M_DEVBUF, M_WAITOK))
137 #define putccdbuf(cbp) \
138 free((caddr_t)(cbp), M_DEVBUF)
139
140 #define CCDLABELDEV(dev) \
141 (MAKEDISKDEV(major((dev)), ccdunit((dev)), RAW_PART))
142
143 /* {b,c}devsw[] function prototypes */
144 dev_type_open(ccdopen);
145 dev_type_close(ccdclose);
146 dev_type_strategy(ccdstrategy);
147 dev_type_ioctl(ccdioctl);
148 dev_type_read(ccdread);
149 dev_type_write(ccdwrite);
150
151 /* called by main() at boot time */
152 void ccdattach __P((int));
153
154 /* called by biodone() at interrupt time */
155 void ccdiodone __P((struct ccdbuf *cbp));
156
157 static void ccdstart __P((struct ccd_softc *, struct buf *));
158 static void ccdinterleave __P((struct ccd_softc *, int));
159 static void ccdintr __P((struct ccd_softc *, struct buf *));
160 static int ccdinit __P((struct ccddevice *, char **, struct proc *));
161 static int ccdlookup __P((char *, struct proc *p, struct vnode **));
162 static void ccdbuffer __P((struct ccd_softc *, struct buf *,
163 daddr_t, caddr_t, long, struct ccdbuf **));
164 static void ccdgetdisklabel __P((dev_t));
165 static void ccdmakedisklabel __P((struct ccd_softc *));
166 static int ccdlock __P((struct ccd_softc *));
167 static void ccdunlock __P((struct ccd_softc *));
168
169 #ifdef DEBUG
170 static void printiinfo __P((struct ccdiinfo *));
171 #endif
172
173 /* Non-private for the benefit of libkvm. */
174 struct ccd_softc *ccd_softc;
175 struct ccddevice *ccddevs;
176 int numccd = 0;
177
178 /*
179 * Called by main() during pseudo-device attachment. All we need
180 * to do is allocate enough space for devices to be configured later.
181 */
182 void
183 ccdattach(num)
184 int num;
185 {
186 int i;
187
188 if (num <= 0) {
189 #ifdef DIAGNOSTIC
190 panic("ccdattach: count <= 0");
191 #endif
192 return;
193 }
194
195 ccd_softc = (struct ccd_softc *)malloc(num * sizeof(struct ccd_softc),
196 M_DEVBUF, M_NOWAIT);
197 ccddevs = (struct ccddevice *)malloc(num * sizeof(struct ccddevice),
198 M_DEVBUF, M_NOWAIT);
199 if ((ccd_softc == NULL) || (ccddevs == NULL)) {
200 printf("WARNING: no memory for concatenated disks\n");
201 if (ccd_softc != NULL)
202 free(ccd_softc, M_DEVBUF);
203 if (ccddevs != NULL)
204 free(ccddevs, M_DEVBUF);
205 return;
206 }
207 numccd = num;
208 bzero(ccd_softc, num * sizeof(struct ccd_softc));
209 bzero(ccddevs, num * sizeof(struct ccddevice));
210 }
211
212 static int
213 ccdinit(ccd, cpaths, p)
214 struct ccddevice *ccd;
215 char **cpaths;
216 struct proc *p;
217 {
218 register struct ccd_softc *cs = &ccd_softc[ccd->ccd_unit];
219 register struct ccdcinfo *ci;
220 register size_t size;
221 register int ix;
222 struct vnode *vp;
223 struct vattr va;
224 size_t minsize;
225 int maxsecsize;
226 struct partinfo dpart;
227 struct ccdgeom *ccg = &cs->sc_geom;
228 char tmppath[MAXPATHLEN];
229 int error;
230
231 #ifdef DEBUG
232 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
233 printf("ccdinit: unit %d\n", ccd->ccd_unit);
234 #endif
235
236 cs->sc_size = 0;
237 cs->sc_ileave = ccd->ccd_interleave;
238 cs->sc_nccdisks = ccd->ccd_ndev;
239 sprintf(cs->sc_xname, "ccd%d", ccd->ccd_unit); /* XXX */
240
241 /* Allocate space for the component info. */
242 cs->sc_cinfo = malloc(cs->sc_nccdisks * sizeof(struct ccdcinfo),
243 M_DEVBUF, M_WAITOK);
244
245 /*
246 * Verify that each component piece exists and record
247 * relevant information about it.
248 */
249 maxsecsize = 0;
250 minsize = 0;
251 for (ix = 0; ix < cs->sc_nccdisks; ix++) {
252 vp = ccd->ccd_vpp[ix];
253 ci = &cs->sc_cinfo[ix];
254 ci->ci_vp = vp;
255
256 /*
257 * Copy in the pathname of the component.
258 */
259 bzero(tmppath, sizeof(tmppath)); /* sanity */
260 if (error = copyinstr(cpaths[ix], tmppath,
261 MAXPATHLEN, &ci->ci_pathlen)) {
262 #ifdef DEBUG
263 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
264 printf("%s: can't copy path, error = %d\n",
265 cs->sc_xname, error);
266 #endif
267 free(cs->sc_cinfo, M_DEVBUF);
268 return (error);
269 }
270 ci->ci_path = malloc(ci->ci_pathlen, M_DEVBUF, M_WAITOK);
271 bcopy(tmppath, ci->ci_path, ci->ci_pathlen);
272
273 /*
274 * XXX: Cache the component's dev_t.
275 */
276 if (error = VOP_GETATTR(vp, &va, p->p_ucred, p)) {
277 #ifdef DEBUG
278 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
279 printf("%s: %s: getattr failed %s = %d\n",
280 cs->sc_xname, ci->ci_path,
281 "error", error);
282 #endif
283 free(ci->ci_path, M_DEVBUF);
284 free(cs->sc_cinfo, M_DEVBUF);
285 return (error);
286 }
287 ci->ci_dev = va.va_rdev;
288
289 /*
290 * Get partition information for the component.
291 */
292 if (error = VOP_IOCTL(vp, DIOCGPART, (caddr_t)&dpart,
293 FREAD, p->p_ucred, p)) {
294 #ifdef DEBUG
295 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
296 printf("%s: %s: ioctl failed, error = %d\n",
297 cs->sc_xname, ci->ci_path, error);
298 #endif
299 free(ci->ci_path, M_DEVBUF);
300 free(cs->sc_cinfo, M_DEVBUF);
301 return (error);
302 }
303 if (dpart.part->p_fstype == FS_BSDFFS) {
304 maxsecsize =
305 ((dpart.disklab->d_secsize > maxsecsize) ?
306 dpart.disklab->d_secsize : maxsecsize);
307 size = dpart.part->p_size;
308 } else {
309 #ifdef DEBUG
310 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
311 printf("%s: %s: incorrect partition type\n",
312 cs->sc_xname, ci->ci_path);
313 #endif
314 free(ci->ci_path, M_DEVBUF);
315 free(cs->sc_cinfo, M_DEVBUF);
316 return (EFTYPE);
317 }
318
319 /*
320 * Calculate the size, truncating to an interleave
321 * boundary if necessary.
322 */
323 if (size < 0)
324 size = 0;
325
326 if (cs->sc_ileave > 1)
327 size -= size % cs->sc_ileave;
328
329 if (size == 0) {
330 #ifdef DEBUG
331 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
332 printf("%s: %s: size == 0\n",
333 cs->sc_xname, ci->ci_path);
334 #endif
335 free(ci->ci_path, M_DEVBUF);
336 free(cs->sc_cinfo, M_DEVBUF);
337 return (ENODEV);
338 }
339
340 if (minsize == 0 || size < minsize)
341 minsize = size;
342 ci->ci_size = size;
343 cs->sc_size += size;
344 }
345
346 /*
347 * Don't allow the interleave to be smaller than
348 * the biggest component sector.
349 */
350 if ((cs->sc_ileave > 0) &&
351 (cs->sc_ileave < (maxsecsize / DEV_BSIZE))) {
352 #ifdef DEBUG
353 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
354 printf("%s: interleave must be at least %d\n",
355 cs->sc_xname, (maxsecsize / DEV_BSIZE));
356 #endif
357 free(ci->ci_path, M_DEVBUF);
358 free(cs->sc_cinfo, M_DEVBUF);
359 return (EINVAL);
360 }
361
362 /*
363 * Mirroring support requires uniform interleave and
364 * and even number of components.
365 */
366 if (ccd->ccd_flags & CCDF_MIRROR) {
367 ccd->ccd_flags |= CCDF_UNIFORM;
368 if (cs->sc_ileave == 0) {
369 #ifdef DEBUG
370 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
371 printf("%s: mirroring requires interleave\n",
372 sc->sc_xname);
373 #endif
374 free(ci->ci_path, M_DEVBUF);
375 free(cs->sc_cinfo, M_DEVBUF);
376 return (EINVAL);
377 }
378 if (cs->sc_nccdisks % 2) {
379 #ifdef DEBUG
380 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
381 printf("%s: mirroring requires even # of components\n",
382 cs->sc_xname);
383 #endif
384 free(ci->ci_path, M_DEVBUF);
385 free(cs->sc_cinfo, M_DEVBUF);
386 return (EINVAL);
387 }
388 }
389
390 /*
391 * If uniform interleave is desired set all sizes to that of
392 * the smallest component.
393 */
394 if (ccd->ccd_flags & CCDF_UNIFORM) {
395 for (ci = cs->sc_cinfo;
396 ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
397 ci->ci_size = minsize;
398
399 if (ccd->ccd_flags & CCDF_MIRROR)
400 cs->sc_size = (cs->sc_nccdisks / 2) * minsize;
401 else
402 cs->sc_size = cs->sc_nccdisks * minsize;
403 }
404
405 /*
406 * Construct the interleave table.
407 */
408 ccdinterleave(cs, ccd->ccd_unit);
409
410 /*
411 * Create pseudo-geometry based on 1MB cylinders. It's
412 * pretty close.
413 */
414 ccg->ccg_secsize = DEV_BSIZE;
415 ccg->ccg_ntracks = 1;
416 ccg->ccg_nsectors = 1024 * (1024 / ccg->ccg_secsize);
417 ccg->ccg_ncylinders = cs->sc_size / ccg->ccg_nsectors;
418
419 cs->sc_flags |= CCDF_INITED;
420 cs->sc_cflags = ccd->ccd_flags; /* So we can find out later... */
421 cs->sc_unit = ccd->ccd_unit;
422
423 return (0);
424 }
425
426 static void
427 ccdinterleave(cs, unit)
428 register struct ccd_softc *cs;
429 int unit;
430 {
431 register struct ccdcinfo *ci, *smallci;
432 register struct ccdiinfo *ii;
433 register daddr_t bn, lbn;
434 register int ix;
435 u_long size;
436
437 #ifdef DEBUG
438 if (ccddebug & CCDB_INIT)
439 printf("ccdinterleave(%x): ileave %d\n", cs, cs->sc_ileave);
440 #endif
441 /*
442 * Allocate an interleave table.
443 * Chances are this is too big, but we don't care.
444 */
445 size = (cs->sc_nccdisks + 1) * sizeof(struct ccdiinfo);
446 cs->sc_itable = (struct ccdiinfo *)malloc(size, M_DEVBUF, M_WAITOK);
447 bzero((caddr_t)cs->sc_itable, size);
448
449 /*
450 * Trivial case: no interleave (actually interleave of disk size).
451 * Each table entry represents a single component in its entirety.
452 */
453 if (cs->sc_ileave == 0) {
454 bn = 0;
455 ii = cs->sc_itable;
456
457 for (ix = 0; ix < cs->sc_nccdisks; ix++) {
458 /* Allocate space for ii_index. */
459 ii->ii_index = malloc(sizeof(int), M_DEVBUF, M_WAITOK);
460 ii->ii_ndisk = 1;
461 ii->ii_startblk = bn;
462 ii->ii_startoff = 0;
463 ii->ii_index[0] = ix;
464 bn += cs->sc_cinfo[ix].ci_size;
465 ii++;
466 }
467 ii->ii_ndisk = 0;
468 #ifdef DEBUG
469 if (ccddebug & CCDB_INIT)
470 printiinfo(cs->sc_itable);
471 #endif
472 return;
473 }
474
475 /*
476 * The following isn't fast or pretty; it doesn't have to be.
477 */
478 size = 0;
479 bn = lbn = 0;
480 for (ii = cs->sc_itable; ; ii++) {
481 /* Allocate space for ii_index. */
482 ii->ii_index = malloc((sizeof(int) * cs->sc_nccdisks),
483 M_DEVBUF, M_WAITOK);
484
485 /*
486 * Locate the smallest of the remaining components
487 */
488 smallci = NULL;
489 for (ci = cs->sc_cinfo;
490 ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
491 if (ci->ci_size > size &&
492 (smallci == NULL ||
493 ci->ci_size < smallci->ci_size))
494 smallci = ci;
495
496 /*
497 * Nobody left, all done
498 */
499 if (smallci == NULL) {
500 ii->ii_ndisk = 0;
501 break;
502 }
503
504 /*
505 * Record starting logical block and component offset
506 */
507 ii->ii_startblk = bn / cs->sc_ileave;
508 ii->ii_startoff = lbn;
509
510 /*
511 * Determine how many disks take part in this interleave
512 * and record their indices.
513 */
514 ix = 0;
515 for (ci = cs->sc_cinfo;
516 ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
517 if (ci->ci_size >= smallci->ci_size)
518 ii->ii_index[ix++] = ci - cs->sc_cinfo;
519 ii->ii_ndisk = ix;
520 bn += ix * (smallci->ci_size - size);
521 lbn = smallci->ci_size / cs->sc_ileave;
522 size = smallci->ci_size;
523 }
524 #ifdef DEBUG
525 if (ccddebug & CCDB_INIT)
526 printiinfo(cs->sc_itable);
527 #endif
528 }
529
530 /* ARGSUSED */
531 int
532 ccdopen(dev, flags, fmt, p)
533 dev_t dev;
534 int flags, fmt;
535 struct proc *p;
536 {
537 int unit = ccdunit(dev);
538 struct ccd_softc *cs;
539 struct disklabel *lp;
540 int error = 0, part, pmask;
541
542 #ifdef DEBUG
543 if (ccddebug & CCDB_FOLLOW)
544 printf("ccdopen(%x, %x)\n", dev, flags);
545 #endif
546 if (unit >= numccd)
547 return (ENXIO);
548 cs = &ccd_softc[unit];
549
550 if (error = ccdlock(cs))
551 return (error);
552
553 lp = cs->sc_dkdev.dk_label;
554
555 part = DISKPART(dev);
556 pmask = (1 << part);
557
558 /*
559 * If we're initialized, check to see if there are any other
560 * open partitions. If not, then it's safe to update
561 * the in-core disklabel.
562 */
563 if ((cs->sc_flags & CCDF_INITED) && (cs->sc_dkdev.dk_openmask == 0))
564 ccdgetdisklabel(dev);
565
566 /* Check that the partition exists. */
567 if (part != RAW_PART && ((part > lp->d_npartitions) ||
568 (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
569 error = ENXIO;
570 goto done;
571 }
572
573 /* Prevent our unit from being unconfigured while open. */
574 switch (fmt) {
575 case S_IFCHR:
576 cs->sc_dkdev.dk_copenmask |= pmask;
577 break;
578
579 case S_IFBLK:
580 cs->sc_dkdev.dk_bopenmask |= pmask;
581 break;
582 }
583 cs->sc_dkdev.dk_openmask =
584 cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
585
586 done:
587 ccdunlock(cs);
588 return (0);
589 }
590
591 /* ARGSUSED */
592 int
593 ccdclose(dev, flags, fmt, p)
594 dev_t dev;
595 int flags, fmt;
596 struct proc *p;
597 {
598 int unit = ccdunit(dev);
599 struct ccd_softc *cs;
600 int error = 0, part;
601
602 #ifdef DEBUG
603 if (ccddebug & CCDB_FOLLOW)
604 printf("ccdclose(%x, %x)\n", dev, flags);
605 #endif
606
607 if (unit >= numccd)
608 return (ENXIO);
609 cs = &ccd_softc[unit];
610
611 if (error = ccdlock(cs))
612 return (error);
613
614 part = DISKPART(dev);
615
616 /* ...that much closer to allowing unconfiguration... */
617 switch (fmt) {
618 case S_IFCHR:
619 cs->sc_dkdev.dk_copenmask &= ~(1 << part);
620 break;
621
622 case S_IFBLK:
623 cs->sc_dkdev.dk_bopenmask &= ~(1 << part);
624 break;
625 }
626 cs->sc_dkdev.dk_openmask =
627 cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
628
629 ccdunlock(cs);
630 return (0);
631 }
632
633 void
634 ccdstrategy(bp)
635 register struct buf *bp;
636 {
637 register int unit = ccdunit(bp->b_dev);
638 register struct ccd_softc *cs = &ccd_softc[unit];
639 register daddr_t bn;
640 register int sz, s;
641 int wlabel;
642 struct disklabel *lp;
643
644 #ifdef DEBUG
645 if (ccddebug & CCDB_FOLLOW)
646 printf("ccdstrategy(%x): unit %d\n", bp, unit);
647 #endif
648 if ((cs->sc_flags & CCDF_INITED) == 0) {
649 bp->b_error = ENXIO;
650 bp->b_flags |= B_ERROR;
651 goto done;
652 }
653
654 /* If it's a nil transfer, wake up the top half now. */
655 if (bp->b_bcount == 0)
656 goto done;
657
658 lp = cs->sc_dkdev.dk_label;
659
660 /*
661 * Do bounds checking and adjust transfer. If there's an
662 * error, the bounds check will flag that for us.
663 */
664 wlabel = cs->sc_flags & (CCDF_WLABEL|CCDF_LABELLING);
665 if (DISKPART(bp->b_dev) != RAW_PART)
666 if (bounds_check_with_label(bp, lp, wlabel) <= 0)
667 goto done;
668
669 bp->b_resid = bp->b_bcount;
670
671 /*
672 * "Start" the unit.
673 */
674 s = splbio();
675 ccdstart(cs, bp);
676 splx(s);
677 return;
678 done:
679 biodone(bp);
680 }
681
682 static void
683 ccdstart(cs, bp)
684 register struct ccd_softc *cs;
685 register struct buf *bp;
686 {
687 register long bcount, rcount;
688 struct ccdbuf *cbp[4];
689 caddr_t addr;
690 daddr_t bn;
691 struct partition *pp;
692
693 #ifdef DEBUG
694 if (ccddebug & CCDB_FOLLOW)
695 printf("ccdstart(%x, %x)\n", cs, bp);
696 #endif
697
698 /* Instrumentation. */
699 disk_busy(&cs->sc_dkdev);
700
701 /*
702 * Translate the partition-relative block number to an absolute.
703 */
704 bn = bp->b_blkno;
705 if (DISKPART(bp->b_dev) != RAW_PART) {
706 pp = &cs->sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
707 bn += pp->p_offset;
708 }
709
710 /*
711 * Allocate component buffers and fire off the requests
712 */
713 addr = bp->b_data;
714 for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
715 ccdbuffer(cs, bp, bn, addr, bcount, cbp);
716 rcount = cbp[0]->cb_buf.b_bcount;
717 if ((cbp[0]->cb_buf.b_flags & B_READ) == 0)
718 cbp[0]->cb_buf.b_vp->v_numoutput++;
719 VOP_STRATEGY(&cbp[0]->cb_buf);
720
721 /*
722 * Mirror requires additional write.
723 */
724 if ((cs->sc_cflags & CCDF_MIRROR) &&
725 ((cbp[0]->cb_buf.b_flags & B_READ) == 0)) {
726 cbp[1]->cb_buf.b_vp->v_numoutput++;
727 VOP_STRATEGY(&cbp[1]->cb_buf);
728 }
729
730 bn += btodb(rcount);
731 addr += rcount;
732 }
733 }
734
735 /*
736 * Build a component buffer header.
737 */
738 static void
739 ccdbuffer(cs, bp, bn, addr, bcount, cbpp)
740 register struct ccd_softc *cs;
741 struct buf *bp;
742 daddr_t bn;
743 caddr_t addr;
744 long bcount;
745 struct ccdbuf **cbpp;
746 {
747 register struct ccdcinfo *ci, *ci2;
748 register struct ccdbuf *cbp;
749 register daddr_t cbn, cboff;
750
751 #ifdef DEBUG
752 if (ccddebug & CCDB_IO)
753 printf("ccdbuffer(%x, %x, %d, %x, %d)\n",
754 cs, bp, bn, addr, bcount);
755 #endif
756 /*
757 * Determine which component bn falls in.
758 */
759 cbn = bn;
760 cboff = 0;
761
762 /*
763 * Serially concatenated
764 */
765 if (cs->sc_ileave == 0) {
766 register daddr_t sblk;
767
768 sblk = 0;
769 for (ci = cs->sc_cinfo; cbn >= sblk + ci->ci_size; ci++)
770 sblk += ci->ci_size;
771 cbn -= sblk;
772 }
773 /*
774 * Interleaved
775 */
776 else {
777 register struct ccdiinfo *ii;
778 int ccdisk, off;
779
780 cboff = cbn % cs->sc_ileave;
781 cbn /= cs->sc_ileave;
782 for (ii = cs->sc_itable; ii->ii_ndisk; ii++)
783 if (ii->ii_startblk > cbn)
784 break;
785 ii--;
786 off = cbn - ii->ii_startblk;
787 if (ii->ii_ndisk == 1) {
788 ccdisk = ii->ii_index[0];
789 cbn = ii->ii_startoff + off;
790 } else {
791 if (cs->sc_cflags & CCDF_MIRROR) {
792 ccdisk =
793 ii->ii_index[off % (ii->ii_ndisk / 2)];
794 cbn = ii->ii_startoff +
795 (off / (ii->ii_ndisk / 2));
796 /* Mirrored data */
797 ci2 =
798 &cs->sc_cinfo[ccdisk + (ii->ii_ndisk / 2)];
799 } else {
800 /* Normal case. */
801 ccdisk = ii->ii_index[off % ii->ii_ndisk];
802 cbn = ii->ii_startoff + off / ii->ii_ndisk;
803 }
804 }
805 cbn *= cs->sc_ileave;
806 ci = &cs->sc_cinfo[ccdisk];
807 }
808
809 /*
810 * Fill in the component buf structure.
811 */
812 cbp = getccdbuf();
813 cbp->cb_flags = 0;
814 cbp->cb_buf.b_flags = bp->b_flags | B_CALL;
815 cbp->cb_buf.b_iodone = (void (*)())ccdiodone;
816 cbp->cb_buf.b_proc = bp->b_proc;
817 cbp->cb_buf.b_dev = ci->ci_dev; /* XXX */
818 cbp->cb_buf.b_blkno = cbn + cboff;
819 cbp->cb_buf.b_data = addr;
820 cbp->cb_buf.b_vp = ci->ci_vp;
821 if (cs->sc_ileave == 0)
822 cbp->cb_buf.b_bcount = dbtob(ci->ci_size - cbn);
823 else
824 cbp->cb_buf.b_bcount = dbtob(cs->sc_ileave - cboff);
825 if (cbp->cb_buf.b_bcount > bcount)
826 cbp->cb_buf.b_bcount = bcount;
827
828 /*
829 * context for ccdiodone
830 */
831 cbp->cb_obp = bp;
832 cbp->cb_unit = cs - ccd_softc;
833 cbp->cb_comp = ci - cs->sc_cinfo;
834
835 /* First buffer is dealt with. */
836 cbpp[0] = cbp;
837
838 #ifdef DEBUG
839 if (ccddebug & CCDB_IO)
840 printf(" dev %x(u%d): cbp %x bn %d addr %x bcnt %d\n",
841 ci->ci_dev, ci-cs->sc_cinfo, cbp, cbp->cb_buf.b_blkno,
842 cbp->cb_buf.b_data, cbp->cb_buf.b_bcount);
843 #endif
844
845 /*
846 * Mirrors have an additional write operation that is nearly
847 * identical to the first.
848 */
849 if ((cs->sc_cflags & CCDF_MIRROR) &&
850 ((cbp->cb_buf.b_flags & B_READ) == 0)) {
851 cbp = getccdbuf();
852 *cbp = *cbpp[0];
853 cbp->cb_flags = CBF_MIRROR;
854 cbp->cb_buf.b_dev = ci2->ci_dev; /* XXX */
855 cbp->cb_buf.b_vp = ci2->ci_vp;
856 cbp->cb_comp = ci2 - cs->sc_cinfo;
857 cbpp[1] = cbp;
858 }
859 }
860
861 static void
862 ccdintr(cs, bp)
863 register struct ccd_softc *cs;
864 register struct buf *bp;
865 {
866
867 #ifdef DEBUG
868 if (ccddebug & CCDB_FOLLOW)
869 printf("ccdintr(%x, %x)\n", cs, bp);
870 #endif
871 /*
872 * Request is done for better or worse, wakeup the top half.
873 */
874 if (bp->b_flags & B_ERROR)
875 bp->b_resid = bp->b_bcount;
876 disk_unbusy(&cs->sc_dkdev, (bp->b_bcount - bp->b_resid));
877 biodone(bp);
878 }
879
880 /*
881 * Called at interrupt time.
882 * Mark the component as done and if all components are done,
883 * take a ccd interrupt.
884 */
885 void
886 ccdiodone(cbp)
887 struct ccdbuf *cbp;
888 {
889 register struct buf *bp = cbp->cb_obp;
890 register int unit = cbp->cb_unit;
891 struct ccd_softc *cs = &ccd_softc[unit];
892 int count, cbflags, s;
893 char *comptype;
894
895 s = splbio();
896 #ifdef DEBUG
897 if (ccddebug & CCDB_FOLLOW)
898 printf("ccdiodone(%x)\n", cbp);
899 if (ccddebug & CCDB_IO) {
900 if (cbp->cb_flags & CBF_MIRROR)
901 printf("ccdiodone: mirror component\n");
902 else
903 printf("ccdiodone: bp %x bcount %d resid %d\n",
904 bp, bp->b_bcount, bp->b_resid);
905 printf(" dev %x(u%d), cbp %x bn %d addr %x bcnt %d\n",
906 cbp->cb_buf.b_dev, cbp->cb_comp, cbp,
907 cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
908 cbp->cb_buf.b_bcount);
909 }
910 #endif
911
912 if (cbp->cb_buf.b_flags & B_ERROR) {
913 if (cbp->cb_flags & CBF_MIRROR)
914 comptype = " (mirror)";
915 else {
916 bp->b_flags |= B_ERROR;
917 bp->b_error = cbp->cb_buf.b_error ?
918 cbp->cb_buf.b_error : EIO;
919 comptype = "";
920 }
921
922 printf("%s: error %d on component %d%s\n",
923 cs->sc_xname, bp->b_error, cbp->cb_comp, comptype);
924 }
925 count = cbp->cb_buf.b_bcount;
926 cbflags = cbp->cb_flags;
927 putccdbuf(cbp);
928
929 /*
930 * If all done, "interrupt".
931 *
932 * Note that mirror component buffers aren't counted against
933 * the original I/O buffer.
934 */
935 if ((cbflags & CBF_MIRROR) == 0) {
936 bp->b_resid -= count;
937 if (bp->b_resid < 0)
938 panic("ccdiodone: count");
939 if (bp->b_resid == 0)
940 ccdintr(&ccd_softc[unit], bp);
941 }
942 splx(s);
943 }
944
945 /* ARGSUSED */
946 int
947 ccdread(dev, uio, flags)
948 dev_t dev;
949 struct uio *uio;
950 int flags;
951 {
952 int unit = ccdunit(dev);
953 struct ccd_softc *cs;
954
955 #ifdef DEBUG
956 if (ccddebug & CCDB_FOLLOW)
957 printf("ccdread(%x, %x)\n", dev, uio);
958 #endif
959 if (unit >= numccd)
960 return (ENXIO);
961 cs = &ccd_softc[unit];
962
963 if ((cs->sc_flags & CCDF_INITED) == 0)
964 return (ENXIO);
965
966 /*
967 * XXX: It's not clear that using minphys() is completely safe,
968 * in particular, for raw I/O. Underlying devices might have some
969 * non-obvious limits, because of the copy to user-space.
970 */
971 return (physio(ccdstrategy, NULL, dev, B_READ, minphys, uio));
972 }
973
974 /* ARGSUSED */
975 int
976 ccdwrite(dev, uio, flags)
977 dev_t dev;
978 struct uio *uio;
979 int flags;
980 {
981 int unit = ccdunit(dev);
982 struct ccd_softc *cs;
983
984 #ifdef DEBUG
985 if (ccddebug & CCDB_FOLLOW)
986 printf("ccdwrite(%x, %x)\n", dev, uio);
987 #endif
988 if (unit >= numccd)
989 return (ENXIO);
990 cs = &ccd_softc[unit];
991
992 if ((cs->sc_flags & CCDF_INITED) == 0)
993 return (ENXIO);
994
995 /*
996 * XXX: It's not clear that using minphys() is completely safe,
997 * in particular, for raw I/O. Underlying devices might have some
998 * non-obvious limits, because of the copy to user-space.
999 */
1000 return (physio(ccdstrategy, NULL, dev, B_WRITE, minphys, uio));
1001 }
1002
1003 int
1004 ccdioctl(dev, cmd, data, flag, p)
1005 dev_t dev;
1006 u_long cmd;
1007 caddr_t data;
1008 int flag;
1009 struct proc *p;
1010 {
1011 int unit = ccdunit(dev);
1012 int i, j, lookedup = 0, error = 0;
1013 int part, pmask, s;
1014 struct ccd_softc *cs;
1015 struct ccd_ioctl *ccio = (struct ccd_ioctl *)data;
1016 struct ccddevice ccd;
1017 char **cpp;
1018 struct vnode **vpp;
1019
1020 if (unit >= numccd)
1021 return (ENXIO);
1022 cs = &ccd_softc[unit];
1023
1024 bzero(&ccd, sizeof(ccd));
1025
1026 switch (cmd) {
1027 case CCDIOCSET:
1028 if (cs->sc_flags & CCDF_INITED)
1029 return (EBUSY);
1030
1031 if ((flag & FWRITE) == 0)
1032 return (EBADF);
1033
1034 if (error = ccdlock(cs))
1035 return (error);
1036
1037 /* Fill in some important bits. */
1038 ccd.ccd_unit = unit;
1039 ccd.ccd_interleave = ccio->ccio_ileave;
1040 ccd.ccd_flags = ccio->ccio_flags & CCDF_USERMASK;
1041
1042 /*
1043 * Allocate space for and copy in the array of
1044 * componet pathnames and device numbers.
1045 */
1046 cpp = malloc(ccio->ccio_ndisks * sizeof(char *),
1047 M_DEVBUF, M_WAITOK);
1048 vpp = malloc(ccio->ccio_ndisks * sizeof(struct vnode *),
1049 M_DEVBUF, M_WAITOK);
1050
1051 error = copyin((caddr_t)ccio->ccio_disks, (caddr_t)cpp,
1052 ccio->ccio_ndisks * sizeof(char **));
1053 if (error) {
1054 free(vpp, M_DEVBUF);
1055 free(cpp, M_DEVBUF);
1056 ccdunlock(cs);
1057 return (error);
1058 }
1059
1060 #ifdef DEBUG
1061 if (ccddebug & CCDB_INIT)
1062 for (i = 0; i < ccio->ccio_ndisks; ++i)
1063 printf("ccdioctl: component %d: 0x%x\n",
1064 i, cpp[i]);
1065 #endif
1066
1067 for (i = 0; i < ccio->ccio_ndisks; ++i) {
1068 #ifdef DEBUG
1069 if (ccddebug & CCDB_INIT)
1070 printf("ccdioctl: lookedup = %d\n", lookedup);
1071 #endif
1072 if (error = ccdlookup(cpp[i], p, &vpp[i])) {
1073 for (j = 0; j < lookedup; ++j)
1074 (void)vn_close(vpp[j], FREAD|FWRITE,
1075 p->p_ucred, p);
1076 free(vpp, M_DEVBUF);
1077 free(cpp, M_DEVBUF);
1078 ccdunlock(cs);
1079 return (error);
1080 }
1081 ++lookedup;
1082 }
1083 ccd.ccd_cpp = cpp;
1084 ccd.ccd_vpp = vpp;
1085 ccd.ccd_ndev = ccio->ccio_ndisks;
1086
1087 /*
1088 * Initialize the ccd. Fills in the softc for us.
1089 */
1090 if (error = ccdinit(&ccd, cpp, p)) {
1091 for (j = 0; j < lookedup; ++j)
1092 (void)vn_close(vpp[j], FREAD|FWRITE,
1093 p->p_ucred, p);
1094 bzero(&ccd_softc[unit], sizeof(struct ccd_softc));
1095 free(vpp, M_DEVBUF);
1096 free(cpp, M_DEVBUF);
1097 ccdunlock(cs);
1098 return (error);
1099 }
1100
1101 /*
1102 * The ccd has been successfully initialized, so
1103 * we can place it into the array. Don't try to
1104 * read the disklabel until the disk has been attached,
1105 * because space for the disklabel is allocated
1106 * in disk_attach();
1107 */
1108 bcopy(&ccd, &ccddevs[unit], sizeof(ccd));
1109 ccio->ccio_unit = unit;
1110 ccio->ccio_size = cs->sc_size;
1111
1112 /* Attach the disk. */
1113 cs->sc_dkdev.dk_name = cs->sc_xname;
1114 disk_attach(&cs->sc_dkdev);
1115
1116 /* Try and read the disklabel. */
1117 ccdgetdisklabel(dev);
1118
1119 ccdunlock(cs);
1120
1121 break;
1122
1123 case CCDIOCCLR:
1124 if ((cs->sc_flags & CCDF_INITED) == 0)
1125 return (ENXIO);
1126
1127 if ((flag & FWRITE) == 0)
1128 return (EBADF);
1129
1130 if (error = ccdlock(cs))
1131 return (error);
1132
1133 /*
1134 * Don't unconfigure if any other partitions are open
1135 * or if both the character and block flavors of this
1136 * partition are open.
1137 */
1138 part = DISKPART(dev);
1139 pmask = (1 << part);
1140 if ((cs->sc_dkdev.dk_openmask & ~pmask) ||
1141 ((cs->sc_dkdev.dk_bopenmask & pmask) &&
1142 (cs->sc_dkdev.dk_copenmask & pmask))) {
1143 ccdunlock(cs);
1144 return (EBUSY);
1145 }
1146
1147 /*
1148 * Free ccd_softc information and clear entry.
1149 */
1150
1151 /* Close the components and free their pathnames. */
1152 for (i = 0; i < cs->sc_nccdisks; ++i) {
1153 /*
1154 * XXX: this close could potentially fail and
1155 * cause Bad Things. Maybe we need to force
1156 * the close to happen?
1157 */
1158 #ifdef DEBUG
1159 if (ccddebug & CCDB_VNODE)
1160 vprint("CCDIOCCLR: vnode info",
1161 cs->sc_cinfo[i].ci_vp);
1162 #endif
1163 (void)vn_close(cs->sc_cinfo[i].ci_vp, FREAD|FWRITE,
1164 p->p_ucred, p);
1165 free(cs->sc_cinfo[i].ci_path, M_DEVBUF);
1166 }
1167
1168 /* Free interleave index. */
1169 for (i = 0; cs->sc_itable[i].ii_ndisk; ++i)
1170 free(cs->sc_itable[i].ii_index, M_DEVBUF);
1171
1172 /* Free component info and interleave table. */
1173 free(cs->sc_cinfo, M_DEVBUF);
1174 free(cs->sc_itable, M_DEVBUF);
1175 cs->sc_flags &= ~CCDF_INITED;
1176
1177 /*
1178 * Free ccddevice information and clear entry.
1179 */
1180 free(ccddevs[unit].ccd_cpp, M_DEVBUF);
1181 free(ccddevs[unit].ccd_vpp, M_DEVBUF);
1182 bcopy(&ccd, &ccddevs[unit], sizeof(ccd));
1183
1184 /* Detatch the disk. */
1185 disk_detatch(&cs->sc_dkdev);
1186
1187 /* This must be atomic. */
1188 s = splhigh();
1189 ccdunlock(cs);
1190 bzero(cs, sizeof(struct ccd_softc));
1191 splx(s);
1192
1193 break;
1194
1195 case DIOCGDINFO:
1196 if ((cs->sc_flags & CCDF_INITED) == 0)
1197 return (ENXIO);
1198
1199 *(struct disklabel *)data = *(cs->sc_dkdev.dk_label);
1200 break;
1201
1202 case DIOCGPART:
1203 if ((cs->sc_flags & CCDF_INITED) == 0)
1204 return (ENXIO);
1205
1206 ((struct partinfo *)data)->disklab = cs->sc_dkdev.dk_label;
1207 ((struct partinfo *)data)->part =
1208 &cs->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
1209 break;
1210
1211 case DIOCWDINFO:
1212 case DIOCSDINFO:
1213 if ((cs->sc_flags & CCDF_INITED) == 0)
1214 return (ENXIO);
1215
1216 if ((flag & FWRITE) == 0)
1217 return (EBADF);
1218
1219 if (error = ccdlock(cs))
1220 return (error);
1221
1222 cs->sc_flags |= CCDF_LABELLING;
1223
1224 error = setdisklabel(cs->sc_dkdev.dk_label,
1225 (struct disklabel *)data, 0, cs->sc_dkdev.dk_cpulabel);
1226 if (error == 0) {
1227 if (cmd == DIOCWDINFO)
1228 error = writedisklabel(CCDLABELDEV(dev),
1229 ccdstrategy, cs->sc_dkdev.dk_label,
1230 cs->sc_dkdev.dk_cpulabel);
1231 }
1232
1233 cs->sc_flags &= ~CCDF_LABELLING;
1234
1235 ccdunlock(cs);
1236
1237 if (error)
1238 return (error);
1239 break;
1240
1241 case DIOCWLABEL:
1242 if ((cs->sc_flags & CCDF_INITED) == 0)
1243 return (ENXIO);
1244
1245 if ((flag & FWRITE) == 0)
1246 return (EBADF);
1247 if (*(int *)data != 0)
1248 cs->sc_flags |= CCDF_WLABEL;
1249 else
1250 cs->sc_flags &= ~CCDF_WLABEL;
1251 break;
1252
1253 default:
1254 return (ENOTTY);
1255 }
1256
1257 return (0);
1258 }
1259
1260 int
1261 ccdsize(dev)
1262 dev_t dev;
1263 {
1264 struct ccd_softc *cs;
1265 int part, size;
1266
1267 if (ccdopen(dev, 0, S_IFBLK, curproc))
1268 return (-1);
1269
1270 cs = &ccd_softc[ccdunit(dev)];
1271 part = DISKPART(dev);
1272
1273 if ((cs->sc_flags & CCDF_INITED) == 0)
1274 return (-1);
1275
1276 if (cs->sc_dkdev.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1277 size = -1;
1278 else
1279 size = cs->sc_dkdev.dk_label->d_partitions[part].p_size;
1280
1281 if (ccdclose(dev, 0, S_IFBLK, curproc))
1282 return (-1);
1283
1284 return (size);
1285 }
1286
1287 int
1288 ccddump(dev, blkno, va, size)
1289 dev_t dev;
1290 daddr_t blkno;
1291 caddr_t va;
1292 size_t size;
1293 {
1294
1295 /* Not implemented. */
1296 return ENXIO;
1297 }
1298
1299 /*
1300 * Lookup the provided name in the filesystem. If the file exists,
1301 * is a valid block device, and isn't being used by anyone else,
1302 * set *vpp to the file's vnode.
1303 */
1304 static int
1305 ccdlookup(path, p, vpp)
1306 char *path;
1307 struct proc *p;
1308 struct vnode **vpp; /* result */
1309 {
1310 struct nameidata nd;
1311 struct vnode *vp;
1312 struct vattr va;
1313 int error;
1314
1315 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, path, p);
1316 if (error = vn_open(&nd, FREAD|FWRITE, 0)) {
1317 #ifdef DEBUG
1318 if (ccddebug & CCDB_FOLLOW|CCDB_INIT)
1319 printf("ccdlookup: vn_open error = %d\n", error);
1320 #endif
1321 return (error);
1322 }
1323 vp = nd.ni_vp;
1324
1325 if (vp->v_usecount > 1) {
1326 VOP_UNLOCK(vp);
1327 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
1328 return (EBUSY);
1329 }
1330
1331 if (error = VOP_GETATTR(vp, &va, p->p_ucred, p)) {
1332 #ifdef DEBUG
1333 if (ccddebug & CCDB_FOLLOW|CCDB_INIT)
1334 printf("ccdlookup: getattr error = %d\n", error);
1335 #endif
1336 VOP_UNLOCK(vp);
1337 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
1338 return (error);
1339 }
1340
1341 /* XXX: eventually we should handle VREG, too. */
1342 if (va.va_type != VBLK) {
1343 VOP_UNLOCK(vp);
1344 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
1345 return (ENOTBLK);
1346 }
1347
1348 #ifdef DEBUG
1349 if (ccddebug & CCDB_VNODE)
1350 vprint("ccdlookup: vnode info", vp);
1351 #endif
1352
1353 VOP_UNLOCK(vp);
1354 *vpp = vp;
1355 return (0);
1356 }
1357
1358 /*
1359 * Read the disklabel from the ccd. If one is not present, fake one
1360 * up.
1361 */
1362 static void
1363 ccdgetdisklabel(dev)
1364 dev_t dev;
1365 {
1366 int unit = ccdunit(dev);
1367 struct ccd_softc *cs = &ccd_softc[unit];
1368 char *errstring;
1369 struct disklabel *lp = cs->sc_dkdev.dk_label;
1370 struct cpu_disklabel *clp = cs->sc_dkdev.dk_cpulabel;
1371 struct ccdgeom *ccg = &cs->sc_geom;
1372
1373 bzero(lp, sizeof(*lp));
1374 bzero(clp, sizeof(*clp));
1375
1376 lp->d_secperunit = cs->sc_size;
1377 lp->d_secsize = ccg->ccg_secsize;
1378 lp->d_nsectors = ccg->ccg_nsectors;
1379 lp->d_ntracks = ccg->ccg_ntracks;
1380 lp->d_ncylinders = ccg->ccg_ncylinders;
1381 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1382
1383 strncpy(lp->d_typename, "ccd", sizeof(lp->d_typename));
1384 lp->d_type = DTYPE_CCD;
1385 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1386 lp->d_rpm = 3600;
1387 lp->d_interleave = 1;
1388 lp->d_flags = 0;
1389
1390 lp->d_partitions[RAW_PART].p_offset = 0;
1391 lp->d_partitions[RAW_PART].p_size = cs->sc_size;
1392 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1393 lp->d_npartitions = RAW_PART + 1;
1394
1395 lp->d_magic = DISKMAGIC;
1396 lp->d_magic2 = DISKMAGIC;
1397 lp->d_checksum = dkcksum(cs->sc_dkdev.dk_label);
1398
1399 /*
1400 * Call the generic disklabel extraction routine.
1401 */
1402 if (errstring = readdisklabel(CCDLABELDEV(dev), ccdstrategy,
1403 cs->sc_dkdev.dk_label, cs->sc_dkdev.dk_cpulabel))
1404 ccdmakedisklabel(cs);
1405
1406 #ifdef DEBUG
1407 /* It's actually extremely common to have unlabeled ccds. */
1408 if (ccddebug & CCDB_LABEL)
1409 if (errstring != NULL)
1410 printf("%s: %s\n", cs->sc_xname, errstring);
1411 #endif
1412 }
1413
1414 /*
1415 * Take care of things one might want to take care of in the event
1416 * that a disklabel isn't present.
1417 */
1418 static void
1419 ccdmakedisklabel(cs)
1420 struct ccd_softc *cs;
1421 {
1422 struct disklabel *lp = cs->sc_dkdev.dk_label;
1423
1424 /*
1425 * For historical reasons, if there's no disklabel present
1426 * the raw partition must be marked FS_BSDFFS.
1427 */
1428 lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
1429
1430 strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
1431 }
1432
1433 /*
1434 * Wait interruptibly for an exclusive lock.
1435 *
1436 * XXX
1437 * Several drivers do this; it should be abstracted and made MP-safe.
1438 */
1439 static int
1440 ccdlock(cs)
1441 struct ccd_softc *cs;
1442 {
1443 int error;
1444
1445 while ((cs->sc_flags & CCDF_LOCKED) != 0) {
1446 cs->sc_flags |= CCDF_WANTED;
1447 if ((error = tsleep(cs, PRIBIO | PCATCH, "ccdlck", 0)) != 0)
1448 return (error);
1449 }
1450 cs->sc_flags |= CCDF_LOCKED;
1451 return (0);
1452 }
1453
1454 /*
1455 * Unlock and wake up any waiters.
1456 */
1457 static void
1458 ccdunlock(cs)
1459 struct ccd_softc *cs;
1460 {
1461
1462 cs->sc_flags &= ~CCDF_LOCKED;
1463 if ((cs->sc_flags & CCDF_WANTED) != 0) {
1464 cs->sc_flags &= ~CCDF_WANTED;
1465 wakeup(cs);
1466 }
1467 }
1468
1469 #ifdef DEBUG
1470 static void
1471 printiinfo(ii)
1472 struct ccdiinfo *ii;
1473 {
1474 register int ix, i;
1475
1476 for (ix = 0; ii->ii_ndisk; ix++, ii++) {
1477 printf(" itab[%d]: #dk %d sblk %d soff %d",
1478 ix, ii->ii_ndisk, ii->ii_startblk, ii->ii_startoff);
1479 for (i = 0; i < ii->ii_ndisk; i++)
1480 printf(" %d", ii->ii_index[i]);
1481 printf("\n");
1482 }
1483 }
1484 #endif
1485