ccd.c revision 1.73.2.1 1 /* $NetBSD: ccd.c,v 1.73.2.1 2001/09/07 04:45:22 thorpej 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 = ccdunit(devvp->v_rdev);
500 struct ccd_softc *cs;
501 struct disklabel *lp;
502 int error = 0, part, pmask;
503
504 #ifdef DEBUG
505 if (ccddebug & CCDB_FOLLOW)
506 printf("ccdopen(0x%x, 0x%x)\n", devvp->v_rdev, flags);
507 #endif
508 if (unit >= numccd)
509 return (ENXIO);
510 cs = &ccd_softc[unit];
511 devvp->v_devcookie = cs;
512
513 if ((error = lockmgr(&cs->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
514 return (error);
515
516 lp = cs->sc_dkdev.dk_label;
517
518 part = DISKPART(devvp->v_rdev);
519 pmask = (1 << part);
520
521 /*
522 * If we're initialized, check to see if there are any other
523 * open partitions. If not, then it's safe to update
524 * the in-core disklabel.
525 */
526 if ((cs->sc_flags & CCDF_INITED) && (cs->sc_dkdev.dk_openmask == 0))
527 ccdgetdisklabel(devvp);
528
529 /* Check that the partition exists. */
530 if (part != RAW_PART) {
531 if (((cs->sc_flags & CCDF_INITED) == 0) ||
532 ((part >= lp->d_npartitions) ||
533 (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
534 error = ENXIO;
535 goto done;
536 }
537 }
538
539 /* Prevent our unit from being unconfigured while open. */
540 switch (fmt) {
541 case S_IFCHR:
542 cs->sc_dkdev.dk_copenmask |= pmask;
543 break;
544
545 case S_IFBLK:
546 cs->sc_dkdev.dk_bopenmask |= pmask;
547 break;
548 }
549 cs->sc_dkdev.dk_openmask =
550 cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
551
552 done:
553 (void) lockmgr(&cs->sc_lock, LK_RELEASE, NULL);
554 return (error);
555 }
556
557 /* ARGSUSED */
558 int
559 ccdclose(devvp, flags, fmt, p)
560 struct vnode *devvp;
561 int flags, fmt;
562 struct proc *p;
563 {
564 struct ccd_softc *cs = devvp->v_devcookie;
565 int error = 0, part;
566
567 #ifdef DEBUG
568 if (ccddebug & CCDB_FOLLOW)
569 printf("ccdclose(0x%x, 0x%x)\n", devvp->v_rdev, flags);
570 #endif
571
572 if ((error = lockmgr(&cs->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
573 return (error);
574
575 part = DISKPART(devvp->v_rdev);
576
577 /* ...that much closer to allowing unconfiguration... */
578 switch (fmt) {
579 case S_IFCHR:
580 cs->sc_dkdev.dk_copenmask &= ~(1 << part);
581 break;
582
583 case S_IFBLK:
584 cs->sc_dkdev.dk_bopenmask &= ~(1 << part);
585 break;
586 }
587 cs->sc_dkdev.dk_openmask =
588 cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
589
590 (void) lockmgr(&cs->sc_lock, LK_RELEASE, NULL);
591 return (0);
592 }
593
594 void
595 ccdstrategy(bp)
596 struct buf *bp;
597 {
598 struct ccd_softc *cs = bp->b_devvp->v_devcookie;
599 int s;
600 int wlabel;
601 struct disklabel *lp;
602
603 #ifdef DEBUG
604 if (ccddebug & CCDB_FOLLOW)
605 printf("ccdstrategy(%p): unit %d\n", bp,
606 DISKUNIT(bp->b_devvp->v_rdev));
607 #endif
608 if ((cs->sc_flags & CCDF_INITED) == 0) {
609 #ifdef DEBUG
610 if (ccddebug & CCDB_FOLLOW)
611 printf("ccdstrategy: unit %d: not inited\n",
612 DISKUNIT(bp->b_devvp->v_rdev));
613 #endif
614 bp->b_error = ENXIO;
615 bp->b_flags |= B_ERROR;
616 goto done;
617 }
618
619 /* If it's a nil transfer, wake up the top half now. */
620 if (bp->b_bcount == 0)
621 goto done;
622
623 lp = cs->sc_dkdev.dk_label;
624
625 /*
626 * Do bounds checking and adjust transfer. If there's an
627 * error, the bounds check will flag that for us.
628 */
629 wlabel = cs->sc_flags & (CCDF_WLABEL|CCDF_LABELLING);
630 if (DISKPART(bp->b_devvp->v_rdev) != RAW_PART &&
631 (bp->b_flags & B_DKLABEL) == 0)
632 if (bounds_check_with_label(bp, lp, wlabel) <= 0)
633 goto done;
634
635 bp->b_resid = bp->b_bcount;
636
637 /*
638 * "Start" the unit.
639 */
640 s = splbio();
641 ccdstart(cs, bp);
642 splx(s);
643 return;
644 done:
645 biodone(bp);
646 }
647
648 static void
649 ccdstart(cs, bp)
650 struct ccd_softc *cs;
651 struct buf *bp;
652 {
653 long bcount, rcount;
654 struct ccdbuf *cbp;
655 caddr_t addr;
656 daddr_t bn;
657 struct partition *pp;
658 SIMPLEQ_HEAD(, ccdbuf) cbufq;
659
660 #ifdef DEBUG
661 if (ccddebug & CCDB_FOLLOW)
662 printf("ccdstart(%p, %p)\n", cs, bp);
663 #endif
664
665 /* Instrumentation. */
666 disk_busy(&cs->sc_dkdev);
667
668 /*
669 * Translate the partition-relative block number to an absolute.
670 */
671 bn = bp->b_blkno;
672 if (DISKPART(bp->b_devvp->v_rdev) != RAW_PART &&
673 (bp->b_flags & B_DKLABEL) == 0) {
674 pp = &cs->sc_dkdev.dk_label->d_partitions[
675 DISKPART(bp->b_devvp->v_rdev)];
676 bn += pp->p_offset;
677 }
678
679 /*
680 * Allocate the component buffers.
681 */
682 SIMPLEQ_INIT(&cbufq);
683 addr = bp->b_data;
684 for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
685 cbp = ccdbuffer(cs, bp, bn, addr, bcount);
686 if (cbp == NULL) {
687 /* Free the already allocated component buffers. */
688 while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
689 SIMPLEQ_REMOVE_HEAD(&cbufq, cbp, cb_q);
690 CCD_PUTBUF(cbp);
691 }
692
693 /* Notify the upper layer we are out of memory. */
694 bp->b_error = ENOMEM;
695 bp->b_flags |= B_ERROR;
696 biodone(bp);
697 disk_unbusy(&cs->sc_dkdev, 0);
698 return;
699 }
700 SIMPLEQ_INSERT_TAIL(&cbufq, cbp, cb_q);
701 rcount = cbp->cb_buf.b_bcount;
702 bn += btodb(rcount);
703 addr += rcount;
704 }
705
706 /* Now fire off the requests. */
707 while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
708 SIMPLEQ_REMOVE_HEAD(&cbufq, cbp, cb_q);
709 if ((cbp->cb_buf.b_flags & B_READ) == 0)
710 cbp->cb_buf.b_vp->v_numoutput++;
711 VOP_STRATEGY(&cbp->cb_buf);
712 }
713 }
714
715 /*
716 * Build a component buffer header.
717 */
718 static struct ccdbuf *
719 ccdbuffer(cs, bp, bn, addr, bcount)
720 struct ccd_softc *cs;
721 struct buf *bp;
722 daddr_t bn;
723 caddr_t addr;
724 long bcount;
725 {
726 struct ccdcinfo *ci;
727 struct ccdbuf *cbp;
728 daddr_t cbn, cboff;
729 u_int64_t cbc;
730 int ccdisk;
731
732 #ifdef DEBUG
733 if (ccddebug & CCDB_IO)
734 printf("ccdbuffer(%p, %p, %d, %p, %ld)\n",
735 cs, bp, bn, addr, bcount);
736 #endif
737 /*
738 * Determine which component bn falls in.
739 */
740 cbn = bn;
741 cboff = 0;
742
743 /*
744 * Serially concatenated
745 */
746 if (cs->sc_ileave == 0) {
747 daddr_t sblk;
748
749 sblk = 0;
750 for (ccdisk = 0, ci = &cs->sc_cinfo[ccdisk];
751 cbn >= sblk + ci->ci_size;
752 ccdisk++, ci = &cs->sc_cinfo[ccdisk])
753 sblk += ci->ci_size;
754 cbn -= sblk;
755 }
756 /*
757 * Interleaved
758 */
759 else {
760 struct ccdiinfo *ii;
761 int off;
762
763 cboff = cbn % cs->sc_ileave;
764 cbn /= cs->sc_ileave;
765 for (ii = cs->sc_itable; ii->ii_ndisk; ii++)
766 if (ii->ii_startblk > cbn)
767 break;
768 ii--;
769 off = cbn - ii->ii_startblk;
770 if (ii->ii_ndisk == 1) {
771 ccdisk = ii->ii_index[0];
772 cbn = ii->ii_startoff + off;
773 } else {
774 ccdisk = ii->ii_index[off % ii->ii_ndisk];
775 cbn = ii->ii_startoff + off / ii->ii_ndisk;
776 }
777 cbn *= cs->sc_ileave;
778 ci = &cs->sc_cinfo[ccdisk];
779 }
780
781 /*
782 * Fill in the component buf structure.
783 */
784 cbp = CCD_GETBUF();
785 if (cbp == NULL)
786 return (NULL);
787 cbp->cb_buf.b_flags = bp->b_flags | B_CALL;
788 cbp->cb_buf.b_iodone = ccdiodone;
789 cbp->cb_buf.b_proc = bp->b_proc;
790 cbp->cb_buf.b_devvp = ci->ci_vp;
791 cbp->cb_buf.b_blkno = cbn + cboff;
792 cbp->cb_buf.b_data = addr;
793 cbp->cb_buf.b_vp = ci->ci_vp;
794 LIST_INIT(&cbp->cb_buf.b_dep);
795 if (cs->sc_ileave == 0)
796 cbc = dbtob((u_int64_t)(ci->ci_size - cbn));
797 else
798 cbc = dbtob((u_int64_t)(cs->sc_ileave - cboff));
799 cbp->cb_buf.b_bcount = cbc < bcount ? cbc : bcount;
800
801 /*
802 * context for ccdiodone
803 */
804 cbp->cb_obp = bp;
805 cbp->cb_sc = cs;
806 cbp->cb_comp = ccdisk;
807
808 #ifdef DEBUG
809 if (ccddebug & CCDB_IO)
810 printf(" dev 0x%x(u%lu): cbp %p bn %d addr %p bcnt %ld\n",
811 ci->ci_vp->v_rdev, (unsigned long) (ci-cs->sc_cinfo), cbp,
812 cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
813 cbp->cb_buf.b_bcount);
814 #endif
815
816 return (cbp);
817 }
818
819 static void
820 ccdintr(cs, bp)
821 struct ccd_softc *cs;
822 struct buf *bp;
823 {
824
825 #ifdef DEBUG
826 if (ccddebug & CCDB_FOLLOW)
827 printf("ccdintr(%p, %p)\n", cs, bp);
828 #endif
829 /*
830 * Request is done for better or worse, wakeup the top half.
831 */
832 if (bp->b_flags & B_ERROR)
833 bp->b_resid = bp->b_bcount;
834 disk_unbusy(&cs->sc_dkdev, (bp->b_bcount - bp->b_resid));
835 biodone(bp);
836 }
837
838 /*
839 * Called at interrupt time.
840 * Mark the component as done and if all components are done,
841 * take a ccd interrupt.
842 */
843 void
844 ccdiodone(vbp)
845 struct buf *vbp;
846 {
847 struct ccdbuf *cbp = (struct ccdbuf *) vbp;
848 struct buf *bp = cbp->cb_obp;
849 struct ccd_softc *cs = cbp->cb_sc;
850 int count, s;
851
852 s = splbio();
853 #ifdef DEBUG
854 if (ccddebug & CCDB_FOLLOW)
855 printf("ccdiodone(%p)\n", cbp);
856 if (ccddebug & CCDB_IO) {
857 printf("ccdiodone: bp %p bcount %ld resid %ld\n",
858 bp, bp->b_bcount, bp->b_resid);
859 printf(" dev 0x%x(u%d), cbp %p bn %d addr %p bcnt %ld\n",
860 cbp->cb_buf.b_devvp->v_rdev, cbp->cb_comp, cbp,
861 cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
862 cbp->cb_buf.b_bcount);
863 }
864 #endif
865
866 if (cbp->cb_buf.b_flags & B_ERROR) {
867 bp->b_flags |= B_ERROR;
868 bp->b_error = cbp->cb_buf.b_error ?
869 cbp->cb_buf.b_error : EIO;
870
871 printf("%s: error %d on component %d\n",
872 cs->sc_xname, bp->b_error, cbp->cb_comp);
873 }
874 count = cbp->cb_buf.b_bcount;
875 CCD_PUTBUF(cbp);
876
877 /*
878 * If all done, "interrupt".
879 */
880 bp->b_resid -= count;
881 if (bp->b_resid < 0)
882 panic("ccdiodone: count");
883 if (bp->b_resid == 0)
884 ccdintr(cs, bp);
885 splx(s);
886 }
887
888 /* ARGSUSED */
889 int
890 ccdread(devvp, uio, flags)
891 struct vnode *devvp;
892 struct uio *uio;
893 int flags;
894 {
895 struct ccd_softc *cs = devvp->v_devcookie;
896
897 #ifdef DEBUG
898 if (ccddebug & CCDB_FOLLOW)
899 printf("ccdread(0x%x, %p)\n", devvp->v_rdev, uio);
900 #endif
901 if ((cs->sc_flags & CCDF_INITED) == 0)
902 return (ENXIO);
903
904 /*
905 * XXX: It's not clear that using minphys() is completely safe,
906 * in particular, for raw I/O. Underlying devices might have some
907 * non-obvious limits, because of the copy to user-space.
908 */
909 return (physio(ccdstrategy, NULL, devvp, B_READ, minphys, uio));
910 }
911
912 /* ARGSUSED */
913 int
914 ccdwrite(devvp, uio, flags)
915 struct vnode *devvp;
916 struct uio *uio;
917 int flags;
918 {
919 struct ccd_softc *cs = devvp->v_devcookie;
920
921 #ifdef DEBUG
922 if (ccddebug & CCDB_FOLLOW)
923 printf("ccdwrite(0x%x, %p)\n", devvp->v_rdev, uio);
924 #endif
925 if ((cs->sc_flags & CCDF_INITED) == 0)
926 return (ENXIO);
927
928 /*
929 * XXX: It's not clear that using minphys() is completely safe,
930 * in particular, for raw I/O. Underlying devices might have some
931 * non-obvious limits, because of the copy to user-space.
932 */
933 return (physio(ccdstrategy, NULL, devvp, B_WRITE, minphys, uio));
934 }
935
936 int
937 ccdioctl(devvp, cmd, data, flag, p)
938 struct vnode *devvp;
939 u_long cmd;
940 caddr_t data;
941 int flag;
942 struct proc *p;
943 {
944 struct ccd_softc *cs = devvp->v_devcookie;
945 struct ccd_ioctl *ccio = (struct ccd_ioctl *)data;
946 int i, j, lookedup = 0, error;
947 int part, pmask;
948 char **cpp;
949 struct vnode **vpp;
950 #ifdef __HAVE_OLD_DISKLABEL
951 struct disklabel newlabel;
952 #endif
953
954 /* Must be open for writes for these commands... */
955 switch (cmd) {
956 case CCDIOCSET:
957 case CCDIOCCLR:
958 case DIOCSDINFO:
959 case DIOCWDINFO:
960 #ifdef __HAVE_OLD_DISKLABEL
961 case ODIOCSDINFO:
962 case ODIOCWDINFO:
963 #endif
964 case DIOCWLABEL:
965 if ((flag & FWRITE) == 0)
966 return (EBADF);
967 }
968
969 if ((error = lockmgr(&cs->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
970 return (error);
971
972 /* Must be initialized for these... */
973 switch (cmd) {
974 case CCDIOCCLR:
975 case DIOCGDINFO:
976 case DIOCSDINFO:
977 case DIOCWDINFO:
978 case DIOCGPART:
979 case DIOCWLABEL:
980 case DIOCGDEFLABEL:
981 #ifdef __HAVE_OLD_DISKLABEL
982 case ODIOCGDINFO:
983 case ODIOCSDINFO:
984 case ODIOCWDINFO:
985 case ODIOCGDEFLABEL:
986 #endif
987 if ((cs->sc_flags & CCDF_INITED) == 0) {
988 error = ENXIO;
989 goto out;
990 }
991 }
992
993 switch (cmd) {
994 case CCDIOCSET:
995 if (cs->sc_flags & CCDF_INITED) {
996 error = EBUSY;
997 goto out;
998 }
999
1000 /* Validate the flags. */
1001 if ((ccio->ccio_flags & CCDF_USERMASK) != ccio->ccio_flags) {
1002 error = EINVAL;
1003 goto out;
1004 }
1005
1006 if (ccio->ccio_ndisks > CCD_MAXNDISKS) {
1007 error = EINVAL;
1008 goto out;
1009 }
1010
1011 /* Fill in some important bits. */
1012 cs->sc_ileave = ccio->ccio_ileave;
1013 cs->sc_nccdisks = ccio->ccio_ndisks;
1014 cs->sc_flags = ccio->ccio_flags & CCDF_USERMASK;
1015
1016 /*
1017 * Allocate space for and copy in the array of
1018 * componet pathnames and device numbers.
1019 */
1020 cpp = malloc(ccio->ccio_ndisks * sizeof(char *),
1021 M_DEVBUF, M_WAITOK);
1022 vpp = malloc(ccio->ccio_ndisks * sizeof(struct vnode *),
1023 M_DEVBUF, M_WAITOK);
1024
1025 error = copyin((caddr_t)ccio->ccio_disks, (caddr_t)cpp,
1026 ccio->ccio_ndisks * sizeof(char **));
1027 if (error) {
1028 free(vpp, M_DEVBUF);
1029 free(cpp, M_DEVBUF);
1030 goto out;
1031 }
1032
1033 #ifdef DEBUG
1034 if (ccddebug & CCDB_INIT)
1035 for (i = 0; i < ccio->ccio_ndisks; ++i)
1036 printf("ccdioctl: component %d: 0x%p\n",
1037 i, cpp[i]);
1038 #endif
1039
1040 for (i = 0; i < ccio->ccio_ndisks; ++i) {
1041 #ifdef DEBUG
1042 if (ccddebug & CCDB_INIT)
1043 printf("ccdioctl: lookedup = %d\n", lookedup);
1044 #endif
1045 if ((error = ccdlookup(cpp[i], p, &vpp[i])) != 0) {
1046 for (j = 0; j < lookedup; ++j)
1047 (void)vn_close(vpp[j], FREAD|FWRITE,
1048 p->p_ucred, p);
1049 free(vpp, M_DEVBUF);
1050 free(cpp, M_DEVBUF);
1051 goto out;
1052 }
1053 ++lookedup;
1054 }
1055
1056 /*
1057 * Initialize the ccd. Fills in the softc for us.
1058 */
1059 if ((error = ccdinit(cs, cpp, vpp, p)) != 0) {
1060 for (j = 0; j < lookedup; ++j)
1061 (void)vn_close(vpp[j], FREAD|FWRITE,
1062 p->p_ucred, p);
1063 free(vpp, M_DEVBUF);
1064 free(cpp, M_DEVBUF);
1065 goto out;
1066 }
1067
1068 /* We can free the temporary variables now. */
1069 free(vpp, M_DEVBUF);
1070 free(cpp, M_DEVBUF);
1071
1072 /*
1073 * The ccd has been successfully initialized, so
1074 * we can place it into the array. Don't try to
1075 * read the disklabel until the disk has been attached,
1076 * because space for the disklabel is allocated
1077 * in disk_attach();
1078 */
1079 ccio->ccio_unit = DISKUNIT(devvp->v_rdev);
1080 ccio->ccio_size = cs->sc_size;
1081
1082 /* Attach the disk. */
1083 disk_attach(&cs->sc_dkdev);
1084
1085 /* Try and read the disklabel. */
1086 ccdgetdisklabel(devvp);
1087 break;
1088
1089 case CCDIOCCLR:
1090 /*
1091 * Don't unconfigure if any other partitions are open
1092 * or if both the character and block flavors of this
1093 * partition are open.
1094 */
1095 part = DISKPART(devvp->v_rdev);
1096 pmask = (1 << part);
1097 if ((cs->sc_dkdev.dk_openmask & ~pmask) ||
1098 ((cs->sc_dkdev.dk_bopenmask & pmask) &&
1099 (cs->sc_dkdev.dk_copenmask & pmask))) {
1100 error = EBUSY;
1101 goto out;
1102 }
1103
1104 /*
1105 * Free ccd_softc information and clear entry.
1106 */
1107
1108 /* Close the components and free their pathnames. */
1109 for (i = 0; i < cs->sc_nccdisks; ++i) {
1110 /*
1111 * XXX: this close could potentially fail and
1112 * cause Bad Things. Maybe we need to force
1113 * the close to happen?
1114 */
1115 #ifdef DEBUG
1116 if (ccddebug & CCDB_VNODE)
1117 vprint("CCDIOCCLR: vnode info",
1118 cs->sc_cinfo[i].ci_vp);
1119 #endif
1120 (void)vn_close(cs->sc_cinfo[i].ci_vp, FREAD|FWRITE,
1121 p->p_ucred, p);
1122 free(cs->sc_cinfo[i].ci_path, M_DEVBUF);
1123 }
1124
1125 /* Free interleave index. */
1126 for (i = 0; cs->sc_itable[i].ii_ndisk; ++i)
1127 free(cs->sc_itable[i].ii_index, M_DEVBUF);
1128
1129 /* Free component info and interleave table. */
1130 free(cs->sc_cinfo, M_DEVBUF);
1131 free(cs->sc_itable, M_DEVBUF);
1132 cs->sc_flags &= ~CCDF_INITED;
1133
1134 /* Detatch the disk. */
1135 disk_detach(&cs->sc_dkdev);
1136 break;
1137
1138 case DIOCGDINFO:
1139 *(struct disklabel *)data = *(cs->sc_dkdev.dk_label);
1140 break;
1141 #ifdef __HAVE_OLD_DISKLABEL
1142 case ODIOCGDINFO:
1143 newlabel = *(cs->sc_dkdev.dk_label);
1144 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1145 return ENOTTY;
1146 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1147 break;
1148 #endif
1149
1150 case DIOCGPART:
1151 ((struct partinfo *)data)->disklab = cs->sc_dkdev.dk_label;
1152 ((struct partinfo *)data)->part =
1153 &cs->sc_dkdev.dk_label->d_partitions[DISKPART(devvp->v_rdev)];
1154 break;
1155
1156 case DIOCWDINFO:
1157 case DIOCSDINFO:
1158 #ifdef __HAVE_OLD_DISKLABEL
1159 case ODIOCWDINFO:
1160 case ODIOCSDINFO:
1161 #endif
1162 {
1163 struct disklabel *lp;
1164 #ifdef __HAVE_OLD_DISKLABEL
1165 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1166 memset(&newlabel, 0, sizeof newlabel);
1167 memcpy(&newlabel, data, sizeof (struct olddisklabel));
1168 lp = &newlabel;
1169 } else
1170 #endif
1171 lp = (struct disklabel *)data;
1172
1173 cs->sc_flags |= CCDF_LABELLING;
1174
1175 error = setdisklabel(cs->sc_dkdev.dk_label,
1176 lp, 0, cs->sc_dkdev.dk_cpulabel);
1177 if (error == 0) {
1178 if (cmd == DIOCWDINFO
1179 #ifdef __HAVE_OLD_DISKLABEL
1180 || cmd == ODIOCWDINFO
1181 #endif
1182 )
1183 error = writedisklabel(devvp, ccdstrategy,
1184 cs->sc_dkdev.dk_label,
1185 cs->sc_dkdev.dk_cpulabel);
1186 }
1187
1188 cs->sc_flags &= ~CCDF_LABELLING;
1189 break;
1190 }
1191
1192 case DIOCWLABEL:
1193 if (*(int *)data != 0)
1194 cs->sc_flags |= CCDF_WLABEL;
1195 else
1196 cs->sc_flags &= ~CCDF_WLABEL;
1197 break;
1198
1199 case DIOCGDEFLABEL:
1200 ccdgetdefaultlabel(cs, (struct disklabel *)data);
1201 break;
1202
1203 #ifdef __HAVE_OLD_DISKLABEL
1204 case ODIOCGDEFLABEL:
1205 ccdgetdefaultlabel(cs, &newlabel);
1206 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1207 return ENOTTY;
1208 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1209 break;
1210 #endif
1211
1212 default:
1213 error = ENOTTY;
1214 }
1215
1216 out:
1217 (void) lockmgr(&cs->sc_lock, LK_RELEASE, NULL);
1218 return (error);
1219 }
1220
1221 int
1222 ccdsize(dev)
1223 dev_t dev;
1224 {
1225 #if 1 /* XXXthorpej */
1226 return (-1);
1227 #else
1228 struct ccd_softc *cs;
1229 struct disklabel *lp;
1230 int part, unit, omask, size;
1231
1232 unit = ccdunit(dev);
1233 if (unit >= numccd)
1234 return (-1);
1235 cs = &ccd_softc[unit];
1236
1237 if ((cs->sc_flags & CCDF_INITED) == 0)
1238 return (-1);
1239
1240 part = DISKPART(dev);
1241 omask = cs->sc_dkdev.dk_openmask & (1 << part);
1242 lp = cs->sc_dkdev.dk_label;
1243
1244 if (omask == 0 && ccdopen(dev, 0, S_IFBLK, curproc))
1245 return (-1);
1246
1247 if (lp->d_partitions[part].p_fstype != FS_SWAP)
1248 size = -1;
1249 else
1250 size = lp->d_partitions[part].p_size *
1251 (lp->d_secsize / DEV_BSIZE);
1252
1253 if (omask == 0 && ccdclose(dev, 0, S_IFBLK, curproc))
1254 return (-1);
1255
1256 return (size);
1257 #endif
1258 }
1259
1260 int
1261 ccddump(dev, blkno, va, size)
1262 dev_t dev;
1263 daddr_t blkno;
1264 caddr_t va;
1265 size_t size;
1266 {
1267
1268 /* Not implemented. */
1269 return ENXIO;
1270 }
1271
1272 /*
1273 * Lookup the provided name in the filesystem. If the file exists,
1274 * is a valid block device, and isn't being used by anyone else,
1275 * set *vpp to the file's vnode.
1276 */
1277 static int
1278 ccdlookup(path, p, vpp)
1279 char *path;
1280 struct proc *p;
1281 struct vnode **vpp; /* result */
1282 {
1283 struct nameidata nd;
1284 struct vnode *vp;
1285 struct vattr va;
1286 int error;
1287
1288 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, path, p);
1289 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
1290 #ifdef DEBUG
1291 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
1292 printf("ccdlookup: vn_open error = %d\n", error);
1293 #endif
1294 return (error);
1295 }
1296 vp = nd.ni_vp;
1297
1298 if (vp->v_usecount > 1) {
1299 VOP_UNLOCK(vp, 0);
1300 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
1301 return (EBUSY);
1302 }
1303
1304 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0) {
1305 #ifdef DEBUG
1306 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
1307 printf("ccdlookup: getattr error = %d\n", error);
1308 #endif
1309 VOP_UNLOCK(vp, 0);
1310 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
1311 return (error);
1312 }
1313
1314 /* XXX: eventually we should handle VREG, too. */
1315 if (va.va_type != VBLK) {
1316 VOP_UNLOCK(vp, 0);
1317 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
1318 return (ENOTBLK);
1319 }
1320
1321 #ifdef DEBUG
1322 if (ccddebug & CCDB_VNODE)
1323 vprint("ccdlookup: vnode info", vp);
1324 #endif
1325
1326 VOP_UNLOCK(vp, 0);
1327 *vpp = vp;
1328 return (0);
1329 }
1330
1331 static void
1332 ccdgetdefaultlabel(cs, lp)
1333 struct ccd_softc *cs;
1334 struct disklabel *lp;
1335 {
1336 struct ccdgeom *ccg = &cs->sc_geom;
1337
1338 memset(lp, 0, sizeof(*lp));
1339
1340 lp->d_secperunit = cs->sc_size;
1341 lp->d_secsize = ccg->ccg_secsize;
1342 lp->d_nsectors = ccg->ccg_nsectors;
1343 lp->d_ntracks = ccg->ccg_ntracks;
1344 lp->d_ncylinders = ccg->ccg_ncylinders;
1345 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1346
1347 strncpy(lp->d_typename, "ccd", sizeof(lp->d_typename));
1348 lp->d_type = DTYPE_CCD;
1349 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1350 lp->d_rpm = 3600;
1351 lp->d_interleave = 1;
1352 lp->d_flags = 0;
1353
1354 lp->d_partitions[RAW_PART].p_offset = 0;
1355 lp->d_partitions[RAW_PART].p_size = cs->sc_size;
1356 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1357 lp->d_npartitions = RAW_PART + 1;
1358
1359 lp->d_magic = DISKMAGIC;
1360 lp->d_magic2 = DISKMAGIC;
1361 lp->d_checksum = dkcksum(cs->sc_dkdev.dk_label);
1362 }
1363
1364 /*
1365 * Read the disklabel from the ccd. If one is not present, fake one
1366 * up.
1367 */
1368 static void
1369 ccdgetdisklabel(devvp)
1370 struct vnode *devvp;
1371 {
1372 struct ccd_softc *cs = devvp->v_devcookie;
1373 char *errstring;
1374 struct disklabel *lp = cs->sc_dkdev.dk_label;
1375 struct cpu_disklabel *clp = cs->sc_dkdev.dk_cpulabel;
1376
1377 memset(clp, 0, sizeof(*clp));
1378
1379 ccdgetdefaultlabel(cs, lp);
1380
1381 /*
1382 * Call the generic disklabel extraction routine.
1383 */
1384 errstring = readdisklabel(devvp, ccdstrategy,
1385 cs->sc_dkdev.dk_label, cs->sc_dkdev.dk_cpulabel);
1386 if (errstring)
1387 ccdmakedisklabel(cs);
1388 else {
1389 int i;
1390 struct partition *pp;
1391
1392 /*
1393 * Sanity check whether the found disklabel is valid.
1394 *
1395 * This is necessary since total size of ccd may vary
1396 * when an interleave is changed even though exactly
1397 * same componets are used, and old disklabel may used
1398 * if that is found.
1399 */
1400 if (lp->d_secperunit != cs->sc_size)
1401 printf("WARNING: %s: "
1402 "total sector size in disklabel (%d) != "
1403 "the size of ccd (%lu)\n", cs->sc_xname,
1404 lp->d_secperunit, (u_long)cs->sc_size);
1405 for (i = 0; i < lp->d_npartitions; i++) {
1406 pp = &lp->d_partitions[i];
1407 if (pp->p_offset + pp->p_size > cs->sc_size)
1408 printf("WARNING: %s: end of partition `%c' "
1409 "exceeds the size of ccd (%lu)\n",
1410 cs->sc_xname, 'a' + i, (u_long)cs->sc_size);
1411 }
1412 }
1413
1414 #ifdef DEBUG
1415 /* It's actually extremely common to have unlabeled ccds. */
1416 if (ccddebug & CCDB_LABEL)
1417 if (errstring != NULL)
1418 printf("%s: %s\n", cs->sc_xname, errstring);
1419 #endif
1420 }
1421
1422 /*
1423 * Take care of things one might want to take care of in the event
1424 * that a disklabel isn't present.
1425 */
1426 static void
1427 ccdmakedisklabel(cs)
1428 struct ccd_softc *cs;
1429 {
1430 struct disklabel *lp = cs->sc_dkdev.dk_label;
1431
1432 /*
1433 * For historical reasons, if there's no disklabel present
1434 * the raw partition must be marked FS_BSDFFS.
1435 */
1436 lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
1437
1438 strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
1439
1440 lp->d_checksum = dkcksum(lp);
1441 }
1442
1443 #ifdef DEBUG
1444 static void
1445 printiinfo(ii)
1446 struct ccdiinfo *ii;
1447 {
1448 int ix, i;
1449
1450 for (ix = 0; ii->ii_ndisk; ix++, ii++) {
1451 printf(" itab[%d]: #dk %d sblk %d soff %d",
1452 ix, ii->ii_ndisk, ii->ii_startblk, ii->ii_startoff);
1453 for (i = 0; i < ii->ii_ndisk; i++)
1454 printf(" %d", ii->ii_index[i]);
1455 printf("\n");
1456 }
1457 }
1458 #endif
1459