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