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