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