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