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