No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / ccd.c
bloba983330667b7c4bc8f2cb7627be2e7cfad2139e4
1 /* $NetBSD: ccd.c,v 1.133 2009/04/04 08:29:39 ad Exp $ */
3 /*-
4 * Copyright (c) 1996, 1997, 1998, 1999, 2007, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe, and by Andrew Doran.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1990, 1993
34 * The Regents of the University of California. All rights reserved.
36 * This code is derived from software contributed to Berkeley by
37 * the Systems Programming Group of the University of Utah Computer
38 * Science Department.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
64 * from: Utah $Hdr: cd.c 1.6 90/11/28$
66 * @(#)cd.c 8.2 (Berkeley) 11/16/93
70 * Copyright (c) 1988 University of Utah.
72 * This code is derived from software contributed to Berkeley by
73 * the Systems Programming Group of the University of Utah Computer
74 * Science Department.
76 * Redistribution and use in source and binary forms, with or without
77 * modification, are permitted provided that the following conditions
78 * are met:
79 * 1. Redistributions of source code must retain the above copyright
80 * notice, this list of conditions and the following disclaimer.
81 * 2. Redistributions in binary form must reproduce the above copyright
82 * notice, this list of conditions and the following disclaimer in the
83 * documentation and/or other materials provided with the distribution.
84 * 3. All advertising materials mentioning features or use of this software
85 * must display the following acknowledgement:
86 * This product includes software developed by the University of
87 * California, Berkeley and its contributors.
88 * 4. Neither the name of the University nor the names of its contributors
89 * may be used to endorse or promote products derived from this software
90 * without specific prior written permission.
92 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
93 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
95 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
96 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
97 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
98 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
100 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
101 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
102 * SUCH DAMAGE.
104 * from: Utah $Hdr: cd.c 1.6 90/11/28$
106 * @(#)cd.c 8.2 (Berkeley) 11/16/93
110 * "Concatenated" disk driver.
112 * Notes on concurrency:
114 * => sc_dvlock serializes access to the device nodes, excluding block I/O.
116 * => sc_iolock serializes access to (sc_flags & CCDF_INITED), disk stats,
117 * sc_stop, sc_bufq and b_resid from master buffers.
119 * => a combination of CCDF_INITED, sc_inflight, and sc_iolock is used to
120 * serialize I/O and configuration changes.
122 * => the in-core disk label does not change while the device is open.
124 * On memory consumption: ccd fans out I/O requests and so needs to
125 * allocate memory. If the system is desperately low on memory, we
126 * single thread I/O.
129 #include <sys/cdefs.h>
130 __KERNEL_RCSID(0, "$NetBSD: ccd.c,v 1.133 2009/04/04 08:29:39 ad Exp $");
132 #include <sys/param.h>
133 #include <sys/systm.h>
134 #include <sys/kernel.h>
135 #include <sys/proc.h>
136 #include <sys/errno.h>
137 #include <sys/buf.h>
138 #include <sys/kmem.h>
139 #include <sys/pool.h>
140 #include <sys/namei.h>
141 #include <sys/stat.h>
142 #include <sys/ioctl.h>
143 #include <sys/disklabel.h>
144 #include <sys/device.h>
145 #include <sys/disk.h>
146 #include <sys/syslog.h>
147 #include <sys/fcntl.h>
148 #include <sys/vnode.h>
149 #include <sys/conf.h>
150 #include <sys/mutex.h>
151 #include <sys/queue.h>
152 #include <sys/kauth.h>
153 #include <sys/kthread.h>
154 #include <sys/bufq.h>
156 #include <dev/ccdvar.h>
157 #include <dev/dkvar.h>
159 #if defined(CCDDEBUG) && !defined(DEBUG)
160 #define DEBUG
161 #endif
163 #ifdef DEBUG
164 #define CCDB_FOLLOW 0x01
165 #define CCDB_INIT 0x02
166 #define CCDB_IO 0x04
167 #define CCDB_LABEL 0x08
168 #define CCDB_VNODE 0x10
169 int ccddebug = 0x00;
170 #endif
172 #define ccdunit(x) DISKUNIT(x)
174 struct ccdbuf {
175 struct buf cb_buf; /* new I/O buf */
176 struct buf *cb_obp; /* ptr. to original I/O buf */
177 struct ccd_softc *cb_sc; /* pointer to ccd softc */
178 int cb_comp; /* target component */
179 SIMPLEQ_ENTRY(ccdbuf) cb_q; /* fifo of component buffers */
182 /* component buffer pool */
183 static pool_cache_t ccd_cache;
185 #define CCD_GETBUF() pool_cache_get(ccd_cache, PR_WAITOK)
186 #define CCD_PUTBUF(cbp) pool_cache_put(ccd_cache, cbp)
188 #define CCDLABELDEV(dev) \
189 (MAKEDISKDEV(major((dev)), ccdunit((dev)), RAW_PART))
191 /* called by main() at boot time */
192 void ccdattach(int);
194 /* called by biodone() at interrupt time */
195 static void ccdiodone(struct buf *);
197 static void ccdinterleave(struct ccd_softc *);
198 static int ccdinit(struct ccd_softc *, char **, struct vnode **,
199 struct lwp *);
200 static struct ccdbuf *ccdbuffer(struct ccd_softc *, struct buf *,
201 daddr_t, void *, long);
202 static void ccdgetdefaultlabel(struct ccd_softc *, struct disklabel *);
203 static void ccdgetdisklabel(dev_t);
204 static void ccdmakedisklabel(struct ccd_softc *);
205 static void ccdstart(struct ccd_softc *);
206 static void ccdthread(void *);
208 static dev_type_open(ccdopen);
209 static dev_type_close(ccdclose);
210 static dev_type_read(ccdread);
211 static dev_type_write(ccdwrite);
212 static dev_type_ioctl(ccdioctl);
213 static dev_type_strategy(ccdstrategy);
214 static dev_type_size(ccdsize);
216 const struct bdevsw ccd_bdevsw = {
217 .d_open = ccdopen,
218 .d_close = ccdclose,
219 .d_strategy = ccdstrategy,
220 .d_ioctl = ccdioctl,
221 .d_dump = nodump,
222 .d_psize = ccdsize,
223 .d_flag = D_DISK | D_MPSAFE
226 const struct cdevsw ccd_cdevsw = {
227 .d_open = ccdopen,
228 .d_close = ccdclose,
229 .d_read = ccdread,
230 .d_write = ccdwrite,
231 .d_ioctl = ccdioctl,
232 .d_stop = nostop,
233 .d_tty = notty,
234 .d_poll = nopoll,
235 .d_mmap = nommap,
236 .d_kqfilter = nokqfilter,
237 .d_flag = D_DISK | D_MPSAFE
240 #ifdef DEBUG
241 static void printiinfo(struct ccdiinfo *);
242 #endif
244 /* Publically visible for the benefit of libkvm and ccdconfig(8). */
245 struct ccd_softc *ccd_softc;
246 const int ccd_softc_elemsize = sizeof(struct ccd_softc);
247 int numccd = 0;
250 * Called by main() during pseudo-device attachment. All we need
251 * to do is allocate enough space for devices to be configured later.
253 void
254 ccdattach(int num)
256 struct ccd_softc *cs;
257 int i;
259 if (num <= 0) {
260 #ifdef DIAGNOSTIC
261 panic("ccdattach: count <= 0");
262 #endif
263 return;
266 ccd_softc = kmem_zalloc(num * ccd_softc_elemsize, KM_SLEEP);
267 if (ccd_softc == NULL) {
268 printf("WARNING: no memory for concatenated disks\n");
269 return;
271 numccd = num;
273 /* Initialize the component buffer pool. */
274 ccd_cache = pool_cache_init(sizeof(struct ccdbuf), 0,
275 0, 0, "ccdbuf", NULL, IPL_BIO, NULL, NULL, NULL);
277 /* Initialize per-softc structures. */
278 for (i = 0; i < num; i++) {
279 cs = &ccd_softc[i];
280 snprintf(cs->sc_xname, sizeof(cs->sc_xname), "ccd%d", i);
281 mutex_init(&cs->sc_dvlock, MUTEX_DEFAULT, IPL_NONE);
282 cs->sc_iolock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
283 cv_init(&cs->sc_stop, "ccdstop");
284 cv_init(&cs->sc_push, "ccdthr");
285 disk_init(&cs->sc_dkdev, cs->sc_xname, NULL); /* XXX */
289 static int
290 ccdinit(struct ccd_softc *cs, char **cpaths, struct vnode **vpp,
291 struct lwp *l)
293 struct ccdcinfo *ci = NULL;
294 size_t size;
295 int ix;
296 struct vattr va;
297 size_t minsize;
298 int maxsecsize;
299 struct partinfo dpart;
300 struct ccdgeom *ccg = &cs->sc_geom;
301 char *tmppath;
302 int error, path_alloced;
304 #ifdef DEBUG
305 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
306 printf("%s: ccdinit\n", cs->sc_xname);
307 #endif
309 /* Allocate space for the component info. */
310 cs->sc_cinfo = kmem_alloc(cs->sc_nccdisks * sizeof(*cs->sc_cinfo),
311 KM_SLEEP);
312 tmppath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
314 cs->sc_size = 0;
317 * Verify that each component piece exists and record
318 * relevant information about it.
320 maxsecsize = 0;
321 minsize = 0;
322 for (ix = 0, path_alloced = 0; ix < cs->sc_nccdisks; ix++) {
323 ci = &cs->sc_cinfo[ix];
324 ci->ci_vp = vpp[ix];
327 * Copy in the pathname of the component.
329 memset(tmppath, 0, sizeof(tmppath)); /* sanity */
330 error = copyinstr(cpaths[ix], tmppath,
331 MAXPATHLEN, &ci->ci_pathlen);
332 if (ci->ci_pathlen == 0)
333 error = EINVAL;
334 if (error) {
335 #ifdef DEBUG
336 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
337 printf("%s: can't copy path, error = %d\n",
338 cs->sc_xname, error);
339 #endif
340 goto out;
342 ci->ci_path = kmem_alloc(ci->ci_pathlen, KM_SLEEP);
343 memcpy(ci->ci_path, tmppath, ci->ci_pathlen);
344 path_alloced++;
347 * XXX: Cache the component's dev_t.
349 if ((error = VOP_GETATTR(vpp[ix], &va, l->l_cred)) != 0) {
350 #ifdef DEBUG
351 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
352 printf("%s: %s: getattr failed %s = %d\n",
353 cs->sc_xname, ci->ci_path,
354 "error", error);
355 #endif
356 goto out;
358 ci->ci_dev = va.va_rdev;
361 * Get partition information for the component.
363 error = VOP_IOCTL(vpp[ix], DIOCGPART, &dpart,
364 FREAD, l->l_cred);
365 if (error) {
366 #ifdef DEBUG
367 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
368 printf("%s: %s: ioctl failed, error = %d\n",
369 cs->sc_xname, ci->ci_path, error);
370 #endif
371 goto out;
375 * This diagnostic test is disabled (for now?) since not all port supports
376 * on-disk BSD disklabel.
378 #if 0 /* def DIAGNOSTIC */
379 /* Check fstype field of component. */
380 if (dpart.part->p_fstype != FS_CCD)
381 printf("%s: WARNING: %s: fstype %d != FS_CCD\n",
382 cs->sc_xname, ci->ci_path, dpart.part->p_fstype);
383 #endif
386 * Calculate the size, truncating to an interleave
387 * boundary if necessary.
389 maxsecsize =
390 ((dpart.disklab->d_secsize > maxsecsize) ?
391 dpart.disklab->d_secsize : maxsecsize);
392 size = dpart.part->p_size;
393 if (cs->sc_ileave > 1)
394 size -= size % cs->sc_ileave;
396 if (size == 0) {
397 #ifdef DEBUG
398 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
399 printf("%s: %s: size == 0\n",
400 cs->sc_xname, ci->ci_path);
401 #endif
402 error = ENODEV;
403 goto out;
406 if (minsize == 0 || size < minsize)
407 minsize = size;
408 ci->ci_size = size;
409 cs->sc_size += size;
413 * Don't allow the interleave to be smaller than
414 * the biggest component sector.
416 if ((cs->sc_ileave > 0) &&
417 (cs->sc_ileave < (maxsecsize / DEV_BSIZE))) {
418 #ifdef DEBUG
419 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
420 printf("%s: interleave must be at least %d\n",
421 cs->sc_xname, (maxsecsize / DEV_BSIZE));
422 #endif
423 error = EINVAL;
424 goto out;
428 * If uniform interleave is desired set all sizes to that of
429 * the smallest component.
431 if (cs->sc_flags & CCDF_UNIFORM) {
432 for (ci = cs->sc_cinfo;
433 ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
434 ci->ci_size = minsize;
436 cs->sc_size = cs->sc_nccdisks * minsize;
440 * Construct the interleave table.
442 ccdinterleave(cs);
445 * Create pseudo-geometry based on 1MB cylinders. It's
446 * pretty close.
448 ccg->ccg_secsize = DEV_BSIZE;
449 ccg->ccg_ntracks = 1;
450 ccg->ccg_nsectors = 1024 * (1024 / ccg->ccg_secsize);
451 ccg->ccg_ncylinders = cs->sc_size / ccg->ccg_nsectors;
454 * Create thread to handle deferred I/O.
456 cs->sc_zap = false;
457 error = kthread_create(PRI_BIO, KTHREAD_MPSAFE, NULL, ccdthread,
458 cs, &cs->sc_thread, "%s", cs->sc_xname);
459 if (error) {
460 printf("ccdinit: can't create thread: %d\n", error);
461 goto out;
465 * Only now that everything is set up can we enable the device.
467 mutex_enter(cs->sc_iolock);
468 cs->sc_flags |= CCDF_INITED;
469 mutex_exit(cs->sc_iolock);
470 kmem_free(tmppath, MAXPATHLEN);
471 return (0);
473 out:
474 for (ix = 0; ix < path_alloced; ix++) {
475 kmem_free(cs->sc_cinfo[ix].ci_path,
476 cs->sc_cinfo[ix].ci_pathlen);
478 kmem_free(cs->sc_cinfo, cs->sc_nccdisks * sizeof(struct ccdcinfo));
479 kmem_free(tmppath, MAXPATHLEN);
480 return (error);
483 static void
484 ccdinterleave(struct ccd_softc *cs)
486 struct ccdcinfo *ci, *smallci;
487 struct ccdiinfo *ii;
488 daddr_t bn, lbn;
489 int ix;
490 u_long size;
492 #ifdef DEBUG
493 if (ccddebug & CCDB_INIT)
494 printf("ccdinterleave(%p): ileave %d\n", cs, cs->sc_ileave);
495 #endif
497 * Allocate an interleave table.
498 * Chances are this is too big, but we don't care.
500 size = (cs->sc_nccdisks + 1) * sizeof(struct ccdiinfo);
501 cs->sc_itable = kmem_zalloc(size, KM_SLEEP);
504 * Trivial case: no interleave (actually interleave of disk size).
505 * Each table entry represents a single component in its entirety.
507 if (cs->sc_ileave == 0) {
508 bn = 0;
509 ii = cs->sc_itable;
511 for (ix = 0; ix < cs->sc_nccdisks; ix++) {
512 /* Allocate space for ii_index. */
513 ii->ii_indexsz = sizeof(int);
514 ii->ii_index = kmem_alloc(ii->ii_indexsz, KM_SLEEP);
515 ii->ii_ndisk = 1;
516 ii->ii_startblk = bn;
517 ii->ii_startoff = 0;
518 ii->ii_index[0] = ix;
519 bn += cs->sc_cinfo[ix].ci_size;
520 ii++;
522 ii->ii_ndisk = 0;
523 #ifdef DEBUG
524 if (ccddebug & CCDB_INIT)
525 printiinfo(cs->sc_itable);
526 #endif
527 return;
531 * The following isn't fast or pretty; it doesn't have to be.
533 size = 0;
534 bn = lbn = 0;
535 for (ii = cs->sc_itable; ; ii++) {
536 /* Allocate space for ii_index. */
537 ii->ii_indexsz = sizeof(int) * cs->sc_nccdisks;
538 ii->ii_index = kmem_alloc(ii->ii_indexsz, KM_SLEEP);
541 * Locate the smallest of the remaining components
543 smallci = NULL;
544 for (ci = cs->sc_cinfo;
545 ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
546 if (ci->ci_size > size &&
547 (smallci == NULL ||
548 ci->ci_size < smallci->ci_size))
549 smallci = ci;
552 * Nobody left, all done
554 if (smallci == NULL) {
555 ii->ii_ndisk = 0;
556 break;
560 * Record starting logical block and component offset
562 ii->ii_startblk = bn / cs->sc_ileave;
563 ii->ii_startoff = lbn;
566 * Determine how many disks take part in this interleave
567 * and record their indices.
569 ix = 0;
570 for (ci = cs->sc_cinfo;
571 ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
572 if (ci->ci_size >= smallci->ci_size)
573 ii->ii_index[ix++] = ci - cs->sc_cinfo;
574 ii->ii_ndisk = ix;
575 bn += ix * (smallci->ci_size - size);
576 lbn = smallci->ci_size / cs->sc_ileave;
577 size = smallci->ci_size;
579 #ifdef DEBUG
580 if (ccddebug & CCDB_INIT)
581 printiinfo(cs->sc_itable);
582 #endif
585 /* ARGSUSED */
586 static int
587 ccdopen(dev_t dev, int flags, int fmt, struct lwp *l)
589 int unit = ccdunit(dev);
590 struct ccd_softc *cs;
591 struct disklabel *lp;
592 int error = 0, part, pmask;
594 #ifdef DEBUG
595 if (ccddebug & CCDB_FOLLOW)
596 printf("ccdopen(0x%"PRIx64", 0x%x)\n", dev, flags);
597 #endif
598 if (unit >= numccd)
599 return (ENXIO);
600 cs = &ccd_softc[unit];
602 mutex_enter(&cs->sc_dvlock);
604 lp = cs->sc_dkdev.dk_label;
606 part = DISKPART(dev);
607 pmask = (1 << part);
610 * If we're initialized, check to see if there are any other
611 * open partitions. If not, then it's safe to update
612 * the in-core disklabel. Only read the disklabel if it is
613 * not already valid.
615 if ((cs->sc_flags & (CCDF_INITED|CCDF_VLABEL)) == CCDF_INITED &&
616 cs->sc_dkdev.dk_openmask == 0)
617 ccdgetdisklabel(dev);
619 /* Check that the partition exists. */
620 if (part != RAW_PART) {
621 if (((cs->sc_flags & CCDF_INITED) == 0) ||
622 ((part >= lp->d_npartitions) ||
623 (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
624 error = ENXIO;
625 goto done;
629 /* Prevent our unit from being unconfigured while open. */
630 switch (fmt) {
631 case S_IFCHR:
632 cs->sc_dkdev.dk_copenmask |= pmask;
633 break;
635 case S_IFBLK:
636 cs->sc_dkdev.dk_bopenmask |= pmask;
637 break;
639 cs->sc_dkdev.dk_openmask =
640 cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
642 done:
643 mutex_exit(&cs->sc_dvlock);
644 return (error);
647 /* ARGSUSED */
648 static int
649 ccdclose(dev_t dev, int flags, int fmt, struct lwp *l)
651 int unit = ccdunit(dev);
652 struct ccd_softc *cs;
653 int part;
655 #ifdef DEBUG
656 if (ccddebug & CCDB_FOLLOW)
657 printf("ccdclose(0x%"PRIx64", 0x%x)\n", dev, flags);
658 #endif
660 if (unit >= numccd)
661 return (ENXIO);
662 cs = &ccd_softc[unit];
664 mutex_enter(&cs->sc_dvlock);
666 part = DISKPART(dev);
668 /* ...that much closer to allowing unconfiguration... */
669 switch (fmt) {
670 case S_IFCHR:
671 cs->sc_dkdev.dk_copenmask &= ~(1 << part);
672 break;
674 case S_IFBLK:
675 cs->sc_dkdev.dk_bopenmask &= ~(1 << part);
676 break;
678 cs->sc_dkdev.dk_openmask =
679 cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
681 if (cs->sc_dkdev.dk_openmask == 0) {
682 if ((cs->sc_flags & CCDF_KLABEL) == 0)
683 cs->sc_flags &= ~CCDF_VLABEL;
686 mutex_exit(&cs->sc_dvlock);
687 return (0);
690 static bool
691 ccdbackoff(struct ccd_softc *cs)
694 /* XXX Arbitrary, should be a uvm call. */
695 return uvmexp.free < (uvmexp.freemin >> 1) &&
696 disk_isbusy(&cs->sc_dkdev);
699 static void
700 ccdthread(void *cookie)
702 struct ccd_softc *cs;
704 cs = cookie;
706 #ifdef DEBUG
707 if (ccddebug & CCDB_FOLLOW)
708 printf("ccdthread: hello\n");
709 #endif
711 mutex_enter(cs->sc_iolock);
712 while (__predict_true(!cs->sc_zap)) {
713 if (bufq_peek(cs->sc_bufq) == NULL) {
714 /* Nothing to do. */
715 cv_wait(&cs->sc_push, cs->sc_iolock);
716 continue;
718 if (ccdbackoff(cs)) {
719 /* Wait for memory to become available. */
720 (void)cv_timedwait(&cs->sc_push, cs->sc_iolock, 1);
721 continue;
723 #ifdef DEBUG
724 if (ccddebug & CCDB_FOLLOW)
725 printf("ccdthread: dispatching I/O\n");
726 #endif
727 ccdstart(cs);
728 mutex_enter(cs->sc_iolock);
730 cs->sc_thread = NULL;
731 mutex_exit(cs->sc_iolock);
732 #ifdef DEBUG
733 if (ccddebug & CCDB_FOLLOW)
734 printf("ccdthread: goodbye\n");
735 #endif
736 kthread_exit(0);
739 static void
740 ccdstrategy(struct buf *bp)
742 int unit = ccdunit(bp->b_dev);
743 struct ccd_softc *cs = &ccd_softc[unit];
745 /* Must be open or reading label. */
746 KASSERT(cs->sc_dkdev.dk_openmask != 0 ||
747 (cs->sc_flags & CCDF_RLABEL) != 0);
749 mutex_enter(cs->sc_iolock);
750 /* Synchronize with device init/uninit. */
751 if (__predict_false((cs->sc_flags & CCDF_INITED) == 0)) {
752 mutex_exit(cs->sc_iolock);
753 #ifdef DEBUG
754 if (ccddebug & CCDB_FOLLOW)
755 printf("ccdstrategy: unit %d: not inited\n", unit);
756 #endif
757 bp->b_error = ENXIO;
758 bp->b_resid = bp->b_bcount;
759 biodone(bp);
760 return;
763 /* Defer to thread if system is low on memory. */
764 bufq_put(cs->sc_bufq, bp);
765 if (__predict_false(ccdbackoff(cs))) {
766 mutex_exit(cs->sc_iolock);
767 #ifdef DEBUG
768 if (ccddebug & CCDB_FOLLOW)
769 printf("ccdstrategy: holding off on I/O\n");
770 #endif
771 return;
773 ccdstart(cs);
776 static void
777 ccdstart(struct ccd_softc *cs)
779 daddr_t blkno;
780 int wlabel;
781 struct disklabel *lp;
782 long bcount, rcount;
783 struct ccdbuf *cbp;
784 char *addr;
785 daddr_t bn;
786 vnode_t *vp;
787 buf_t *bp;
789 KASSERT(mutex_owned(cs->sc_iolock));
791 disk_busy(&cs->sc_dkdev);
792 bp = bufq_get(cs->sc_bufq);
793 KASSERT(bp != NULL);
795 #ifdef DEBUG
796 if (ccddebug & CCDB_FOLLOW)
797 printf("ccdstart(%s, %p)\n", cs->sc_xname, bp);
798 #endif
800 /* If it's a nil transfer, wake up the top half now. */
801 if (bp->b_bcount == 0)
802 goto done;
804 lp = cs->sc_dkdev.dk_label;
807 * Do bounds checking and adjust transfer. If there's an
808 * error, the bounds check will flag that for us. Convert
809 * the partition relative block number to an absolute.
811 blkno = bp->b_blkno;
812 wlabel = cs->sc_flags & (CCDF_WLABEL|CCDF_LABELLING);
813 if (DISKPART(bp->b_dev) != RAW_PART) {
814 if (bounds_check_with_label(&cs->sc_dkdev, bp, wlabel) <= 0)
815 goto done;
816 blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
818 mutex_exit(cs->sc_iolock);
819 bp->b_rawblkno = blkno;
821 /* Allocate the component buffers and start I/O! */
822 bp->b_resid = bp->b_bcount;
823 bn = bp->b_rawblkno;
824 addr = bp->b_data;
825 for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
826 cbp = ccdbuffer(cs, bp, bn, addr, bcount);
827 rcount = cbp->cb_buf.b_bcount;
828 bn += btodb(rcount);
829 addr += rcount;
830 vp = cbp->cb_buf.b_vp;
831 if ((cbp->cb_buf.b_flags & B_READ) == 0) {
832 mutex_enter(&vp->v_interlock);
833 vp->v_numoutput++;
834 mutex_exit(&vp->v_interlock);
836 (void)VOP_STRATEGY(vp, &cbp->cb_buf);
838 return;
840 done:
841 disk_unbusy(&cs->sc_dkdev, 0, 0);
842 cv_broadcast(&cs->sc_stop);
843 cv_broadcast(&cs->sc_push);
844 mutex_exit(cs->sc_iolock);
845 bp->b_resid = bp->b_bcount;
846 biodone(bp);
850 * Build a component buffer header.
852 static struct ccdbuf *
853 ccdbuffer(struct ccd_softc *cs, struct buf *bp, daddr_t bn, void *addr,
854 long bcount)
856 struct ccdcinfo *ci;
857 struct ccdbuf *cbp;
858 daddr_t cbn, cboff;
859 u_int64_t cbc;
860 int ccdisk;
862 #ifdef DEBUG
863 if (ccddebug & CCDB_IO)
864 printf("ccdbuffer(%p, %p, %" PRId64 ", %p, %ld)\n",
865 cs, bp, bn, addr, bcount);
866 #endif
868 * Determine which component bn falls in.
870 cbn = bn;
871 cboff = 0;
874 * Serially concatenated
876 if (cs->sc_ileave == 0) {
877 daddr_t sblk;
879 sblk = 0;
880 for (ccdisk = 0, ci = &cs->sc_cinfo[ccdisk];
881 cbn >= sblk + ci->ci_size;
882 ccdisk++, ci = &cs->sc_cinfo[ccdisk])
883 sblk += ci->ci_size;
884 cbn -= sblk;
887 * Interleaved
889 else {
890 struct ccdiinfo *ii;
891 int off;
893 cboff = cbn % cs->sc_ileave;
894 cbn /= cs->sc_ileave;
895 for (ii = cs->sc_itable; ii->ii_ndisk; ii++)
896 if (ii->ii_startblk > cbn)
897 break;
898 ii--;
899 off = cbn - ii->ii_startblk;
900 if (ii->ii_ndisk == 1) {
901 ccdisk = ii->ii_index[0];
902 cbn = ii->ii_startoff + off;
903 } else {
904 ccdisk = ii->ii_index[off % ii->ii_ndisk];
905 cbn = ii->ii_startoff + off / ii->ii_ndisk;
907 cbn *= cs->sc_ileave;
908 ci = &cs->sc_cinfo[ccdisk];
912 * Fill in the component buf structure.
914 cbp = CCD_GETBUF();
915 KASSERT(cbp != NULL);
916 buf_init(&cbp->cb_buf);
917 cbp->cb_buf.b_flags = bp->b_flags;
918 cbp->cb_buf.b_oflags = bp->b_oflags;
919 cbp->cb_buf.b_cflags = bp->b_cflags;
920 cbp->cb_buf.b_iodone = ccdiodone;
921 cbp->cb_buf.b_proc = bp->b_proc;
922 cbp->cb_buf.b_dev = ci->ci_dev;
923 cbp->cb_buf.b_blkno = cbn + cboff;
924 cbp->cb_buf.b_data = addr;
925 cbp->cb_buf.b_vp = ci->ci_vp;
926 cbp->cb_buf.b_objlock = &ci->ci_vp->v_interlock;
927 if (cs->sc_ileave == 0)
928 cbc = dbtob((u_int64_t)(ci->ci_size - cbn));
929 else
930 cbc = dbtob((u_int64_t)(cs->sc_ileave - cboff));
931 cbp->cb_buf.b_bcount = cbc < bcount ? cbc : bcount;
934 * context for ccdiodone
936 cbp->cb_obp = bp;
937 cbp->cb_sc = cs;
938 cbp->cb_comp = ccdisk;
940 BIO_COPYPRIO(&cbp->cb_buf, bp);
942 #ifdef DEBUG
943 if (ccddebug & CCDB_IO)
944 printf(" dev 0x%"PRIx64"(u%lu): cbp %p bn %" PRId64 " addr %p"
945 " bcnt %d\n",
946 ci->ci_dev, (unsigned long) (ci-cs->sc_cinfo), cbp,
947 cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
948 cbp->cb_buf.b_bcount);
949 #endif
951 return (cbp);
955 * Called at interrupt time.
956 * Mark the component as done and if all components are done,
957 * take a ccd interrupt.
959 static void
960 ccdiodone(struct buf *vbp)
962 struct ccdbuf *cbp = (struct ccdbuf *) vbp;
963 struct buf *bp = cbp->cb_obp;
964 struct ccd_softc *cs = cbp->cb_sc;
965 int count;
967 #ifdef DEBUG
968 if (ccddebug & CCDB_FOLLOW)
969 printf("ccdiodone(%p)\n", cbp);
970 if (ccddebug & CCDB_IO) {
971 printf("ccdiodone: bp %p bcount %d resid %d\n",
972 bp, bp->b_bcount, bp->b_resid);
973 printf(" dev 0x%"PRIx64"(u%d), cbp %p bn %" PRId64 " addr %p"
974 " bcnt %d\n",
975 cbp->cb_buf.b_dev, cbp->cb_comp, cbp,
976 cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
977 cbp->cb_buf.b_bcount);
979 #endif
981 if (cbp->cb_buf.b_error != 0) {
982 bp->b_error = cbp->cb_buf.b_error;
983 printf("%s: error %d on component %d\n",
984 cs->sc_xname, bp->b_error, cbp->cb_comp);
986 count = cbp->cb_buf.b_bcount;
987 buf_destroy(&cbp->cb_buf);
988 CCD_PUTBUF(cbp);
991 * If all done, "interrupt".
993 mutex_enter(cs->sc_iolock);
994 bp->b_resid -= count;
995 if (bp->b_resid < 0)
996 panic("ccdiodone: count");
997 if (bp->b_resid == 0) {
999 * Request is done for better or worse, wakeup the top half.
1001 if (bp->b_error != 0)
1002 bp->b_resid = bp->b_bcount;
1003 disk_unbusy(&cs->sc_dkdev, (bp->b_bcount - bp->b_resid),
1004 (bp->b_flags & B_READ));
1005 if (!disk_isbusy(&cs->sc_dkdev)) {
1006 if (bufq_peek(cs->sc_bufq) != NULL) {
1007 cv_broadcast(&cs->sc_push);
1009 cv_broadcast(&cs->sc_stop);
1011 mutex_exit(cs->sc_iolock);
1012 biodone(bp);
1013 } else
1014 mutex_exit(cs->sc_iolock);
1017 /* ARGSUSED */
1018 static int
1019 ccdread(dev_t dev, struct uio *uio, int flags)
1021 int unit = ccdunit(dev);
1022 struct ccd_softc *cs;
1024 #ifdef DEBUG
1025 if (ccddebug & CCDB_FOLLOW)
1026 printf("ccdread(0x%"PRIx64", %p)\n", dev, uio);
1027 #endif
1028 if (unit >= numccd)
1029 return (ENXIO);
1030 cs = &ccd_softc[unit];
1032 /* Unlocked advisory check, ccdstrategy check is synchronous. */
1033 if ((cs->sc_flags & CCDF_INITED) == 0)
1034 return (ENXIO);
1036 return (physio(ccdstrategy, NULL, dev, B_READ, minphys, uio));
1039 /* ARGSUSED */
1040 static int
1041 ccdwrite(dev_t dev, struct uio *uio, int flags)
1043 int unit = ccdunit(dev);
1044 struct ccd_softc *cs;
1046 #ifdef DEBUG
1047 if (ccddebug & CCDB_FOLLOW)
1048 printf("ccdwrite(0x%"PRIx64", %p)\n", dev, uio);
1049 #endif
1050 if (unit >= numccd)
1051 return (ENXIO);
1052 cs = &ccd_softc[unit];
1054 /* Unlocked advisory check, ccdstrategy check is synchronous. */
1055 if ((cs->sc_flags & CCDF_INITED) == 0)
1056 return (ENXIO);
1058 return (physio(ccdstrategy, NULL, dev, B_WRITE, minphys, uio));
1061 static int
1062 ccdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1064 int unit = ccdunit(dev);
1065 int i, j, lookedup = 0, error = 0;
1066 int part, pmask;
1067 struct ccd_softc *cs;
1068 struct ccd_ioctl *ccio = (struct ccd_ioctl *)data;
1069 kauth_cred_t uc;
1070 char **cpp;
1071 struct vnode **vpp;
1072 #ifdef __HAVE_OLD_DISKLABEL
1073 struct disklabel newlabel;
1074 #endif
1076 if (unit >= numccd)
1077 return (ENXIO);
1078 cs = &ccd_softc[unit];
1079 uc = kauth_cred_get();
1081 /* Must be open for writes for these commands... */
1082 switch (cmd) {
1083 case CCDIOCSET:
1084 case CCDIOCCLR:
1085 case DIOCSDINFO:
1086 case DIOCWDINFO:
1087 #ifdef __HAVE_OLD_DISKLABEL
1088 case ODIOCSDINFO:
1089 case ODIOCWDINFO:
1090 #endif
1091 case DIOCKLABEL:
1092 case DIOCWLABEL:
1093 if ((flag & FWRITE) == 0)
1094 return (EBADF);
1097 mutex_enter(&cs->sc_dvlock);
1099 /* Must be initialized for these... */
1100 switch (cmd) {
1101 case CCDIOCCLR:
1102 case DIOCGDINFO:
1103 case DIOCCACHESYNC:
1104 case DIOCSDINFO:
1105 case DIOCWDINFO:
1106 case DIOCGPART:
1107 case DIOCWLABEL:
1108 case DIOCKLABEL:
1109 case DIOCGDEFLABEL:
1110 #ifdef __HAVE_OLD_DISKLABEL
1111 case ODIOCGDINFO:
1112 case ODIOCSDINFO:
1113 case ODIOCWDINFO:
1114 case ODIOCGDEFLABEL:
1115 #endif
1116 if ((cs->sc_flags & CCDF_INITED) == 0) {
1117 error = ENXIO;
1118 goto out;
1122 switch (cmd) {
1123 case CCDIOCSET:
1124 if (cs->sc_flags & CCDF_INITED) {
1125 error = EBUSY;
1126 goto out;
1129 /* Validate the flags. */
1130 if ((ccio->ccio_flags & CCDF_USERMASK) != ccio->ccio_flags) {
1131 error = EINVAL;
1132 goto out;
1135 if (ccio->ccio_ndisks > CCD_MAXNDISKS ||
1136 ccio->ccio_ndisks == 0) {
1137 error = EINVAL;
1138 goto out;
1141 /* Fill in some important bits. */
1142 cs->sc_ileave = ccio->ccio_ileave;
1143 cs->sc_nccdisks = ccio->ccio_ndisks;
1144 cs->sc_flags = ccio->ccio_flags & CCDF_USERMASK;
1147 * Allocate space for and copy in the array of
1148 * componet pathnames and device numbers.
1150 cpp = kmem_alloc(ccio->ccio_ndisks * sizeof(*cpp), KM_SLEEP);
1151 vpp = kmem_alloc(ccio->ccio_ndisks * sizeof(*vpp), KM_SLEEP);
1152 error = copyin(ccio->ccio_disks, cpp,
1153 ccio->ccio_ndisks * sizeof(*cpp));
1154 if (error) {
1155 kmem_free(vpp, ccio->ccio_ndisks * sizeof(*vpp));
1156 kmem_free(cpp, ccio->ccio_ndisks * sizeof(*cpp));
1157 goto out;
1160 #ifdef DEBUG
1161 if (ccddebug & CCDB_INIT)
1162 for (i = 0; i < ccio->ccio_ndisks; ++i)
1163 printf("ccdioctl: component %d: %p\n",
1164 i, cpp[i]);
1165 #endif
1167 for (i = 0; i < ccio->ccio_ndisks; ++i) {
1168 #ifdef DEBUG
1169 if (ccddebug & CCDB_INIT)
1170 printf("ccdioctl: lookedup = %d\n", lookedup);
1171 #endif
1172 if ((error = dk_lookup(cpp[i], l, &vpp[i],
1173 UIO_USERSPACE)) != 0) {
1174 for (j = 0; j < lookedup; ++j)
1175 (void)vn_close(vpp[j], FREAD|FWRITE,
1176 uc);
1177 kmem_free(vpp, ccio->ccio_ndisks *
1178 sizeof(*vpp));
1179 kmem_free(cpp, ccio->ccio_ndisks *
1180 sizeof(*cpp));
1181 goto out;
1183 ++lookedup;
1186 /* Attach the disk. */
1187 disk_attach(&cs->sc_dkdev);
1188 bufq_alloc(&cs->sc_bufq, "fcfs", 0);
1191 * Initialize the ccd. Fills in the softc for us.
1193 if ((error = ccdinit(cs, cpp, vpp, l)) != 0) {
1194 for (j = 0; j < lookedup; ++j)
1195 (void)vn_close(vpp[j], FREAD|FWRITE,
1196 uc);
1197 kmem_free(vpp, ccio->ccio_ndisks * sizeof(*vpp));
1198 kmem_free(cpp, ccio->ccio_ndisks * sizeof(*cpp));
1199 disk_detach(&cs->sc_dkdev);
1200 bufq_free(cs->sc_bufq);
1201 goto out;
1204 /* We can free the temporary variables now. */
1205 kmem_free(vpp, ccio->ccio_ndisks * sizeof(*vpp));
1206 kmem_free(cpp, ccio->ccio_ndisks * sizeof(*cpp));
1209 * The ccd has been successfully initialized, so
1210 * we can place it into the array. Don't try to
1211 * read the disklabel until the disk has been attached,
1212 * because space for the disklabel is allocated
1213 * in disk_attach();
1215 ccio->ccio_unit = unit;
1216 ccio->ccio_size = cs->sc_size;
1218 /* Try and read the disklabel. */
1219 ccdgetdisklabel(dev);
1220 break;
1222 case CCDIOCCLR:
1224 * Don't unconfigure if any other partitions are open
1225 * or if both the character and block flavors of this
1226 * partition are open.
1228 part = DISKPART(dev);
1229 pmask = (1 << part);
1230 if ((cs->sc_dkdev.dk_openmask & ~pmask) ||
1231 ((cs->sc_dkdev.dk_bopenmask & pmask) &&
1232 (cs->sc_dkdev.dk_copenmask & pmask))) {
1233 error = EBUSY;
1234 goto out;
1237 /* Stop new I/O, wait for in-flight I/O to complete. */
1238 mutex_enter(cs->sc_iolock);
1239 cs->sc_flags &= ~(CCDF_INITED|CCDF_VLABEL);
1240 cs->sc_zap = true;
1241 while (disk_isbusy(&cs->sc_dkdev) ||
1242 bufq_peek(cs->sc_bufq) != NULL ||
1243 cs->sc_thread != NULL) {
1244 cv_broadcast(&cs->sc_push);
1245 (void)cv_timedwait(&cs->sc_stop, cs->sc_iolock, hz);
1247 mutex_exit(cs->sc_iolock);
1250 * Free ccd_softc information and clear entry.
1253 /* Close the components and free their pathnames. */
1254 for (i = 0; i < cs->sc_nccdisks; ++i) {
1256 * XXX: this close could potentially fail and
1257 * cause Bad Things. Maybe we need to force
1258 * the close to happen?
1260 #ifdef DEBUG
1261 if (ccddebug & CCDB_VNODE)
1262 vprint("CCDIOCCLR: vnode info",
1263 cs->sc_cinfo[i].ci_vp);
1264 #endif
1265 (void)vn_close(cs->sc_cinfo[i].ci_vp, FREAD|FWRITE,
1266 uc);
1267 kmem_free(cs->sc_cinfo[i].ci_path,
1268 cs->sc_cinfo[i].ci_pathlen);
1271 /* Free interleave index. */
1272 for (i = 0; cs->sc_itable[i].ii_ndisk; ++i) {
1273 kmem_free(cs->sc_itable[i].ii_index,
1274 cs->sc_itable[i].ii_indexsz);
1277 /* Free component info and interleave table. */
1278 kmem_free(cs->sc_cinfo, cs->sc_nccdisks *
1279 sizeof(struct ccdcinfo));
1280 kmem_free(cs->sc_itable, (cs->sc_nccdisks + 1) *
1281 sizeof(struct ccdiinfo));
1283 /* Detatch the disk. */
1284 disk_detach(&cs->sc_dkdev);
1285 bufq_free(cs->sc_bufq);
1286 break;
1288 case DIOCGDINFO:
1289 *(struct disklabel *)data = *(cs->sc_dkdev.dk_label);
1290 break;
1292 #ifdef __HAVE_OLD_DISKLABEL
1293 case ODIOCGDINFO:
1294 newlabel = *(cs->sc_dkdev.dk_label);
1295 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1296 return ENOTTY;
1297 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1298 break;
1299 #endif
1301 case DIOCGPART:
1302 ((struct partinfo *)data)->disklab = cs->sc_dkdev.dk_label;
1303 ((struct partinfo *)data)->part =
1304 &cs->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
1305 break;
1307 case DIOCCACHESYNC:
1309 * XXX Do we really need to care about having a writable
1310 * file descriptor here?
1312 if ((flag & FWRITE) == 0)
1313 return (EBADF);
1316 * We pass this call down to all components and report
1317 * the first error we encounter.
1319 for (error = 0, i = 0; i < cs->sc_nccdisks; i++) {
1320 j = VOP_IOCTL(cs->sc_cinfo[i].ci_vp, cmd, data,
1321 flag, uc);
1322 if (j != 0 && error == 0)
1323 error = j;
1325 break;
1327 case DIOCWDINFO:
1328 case DIOCSDINFO:
1329 #ifdef __HAVE_OLD_DISKLABEL
1330 case ODIOCWDINFO:
1331 case ODIOCSDINFO:
1332 #endif
1334 struct disklabel *lp;
1335 #ifdef __HAVE_OLD_DISKLABEL
1336 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1337 memset(&newlabel, 0, sizeof newlabel);
1338 memcpy(&newlabel, data, sizeof (struct olddisklabel));
1339 lp = &newlabel;
1340 } else
1341 #endif
1342 lp = (struct disklabel *)data;
1344 cs->sc_flags |= CCDF_LABELLING;
1346 error = setdisklabel(cs->sc_dkdev.dk_label,
1347 lp, 0, cs->sc_dkdev.dk_cpulabel);
1348 if (error == 0) {
1349 if (cmd == DIOCWDINFO
1350 #ifdef __HAVE_OLD_DISKLABEL
1351 || cmd == ODIOCWDINFO
1352 #endif
1354 error = writedisklabel(CCDLABELDEV(dev),
1355 ccdstrategy, cs->sc_dkdev.dk_label,
1356 cs->sc_dkdev.dk_cpulabel);
1359 cs->sc_flags &= ~CCDF_LABELLING;
1360 break;
1363 case DIOCKLABEL:
1364 if (*(int *)data != 0)
1365 cs->sc_flags |= CCDF_KLABEL;
1366 else
1367 cs->sc_flags &= ~CCDF_KLABEL;
1368 break;
1370 case DIOCWLABEL:
1371 if (*(int *)data != 0)
1372 cs->sc_flags |= CCDF_WLABEL;
1373 else
1374 cs->sc_flags &= ~CCDF_WLABEL;
1375 break;
1377 case DIOCGDEFLABEL:
1378 ccdgetdefaultlabel(cs, (struct disklabel *)data);
1379 break;
1381 #ifdef __HAVE_OLD_DISKLABEL
1382 case ODIOCGDEFLABEL:
1383 ccdgetdefaultlabel(cs, &newlabel);
1384 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1385 return ENOTTY;
1386 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1387 break;
1388 #endif
1390 default:
1391 error = ENOTTY;
1394 out:
1395 mutex_exit(&cs->sc_dvlock);
1396 return (error);
1399 static int
1400 ccdsize(dev_t dev)
1402 struct ccd_softc *cs;
1403 struct disklabel *lp;
1404 int part, unit, omask, size;
1406 unit = ccdunit(dev);
1407 if (unit >= numccd)
1408 return (-1);
1409 cs = &ccd_softc[unit];
1411 if ((cs->sc_flags & CCDF_INITED) == 0)
1412 return (-1);
1414 part = DISKPART(dev);
1415 omask = cs->sc_dkdev.dk_openmask & (1 << part);
1416 lp = cs->sc_dkdev.dk_label;
1418 if (omask == 0 && ccdopen(dev, 0, S_IFBLK, curlwp))
1419 return (-1);
1421 if (lp->d_partitions[part].p_fstype != FS_SWAP)
1422 size = -1;
1423 else
1424 size = lp->d_partitions[part].p_size *
1425 (lp->d_secsize / DEV_BSIZE);
1427 if (omask == 0 && ccdclose(dev, 0, S_IFBLK, curlwp))
1428 return (-1);
1430 return (size);
1433 static void
1434 ccdgetdefaultlabel(struct ccd_softc *cs, struct disklabel *lp)
1436 struct ccdgeom *ccg = &cs->sc_geom;
1438 memset(lp, 0, sizeof(*lp));
1440 lp->d_secperunit = cs->sc_size;
1441 lp->d_secsize = ccg->ccg_secsize;
1442 lp->d_nsectors = ccg->ccg_nsectors;
1443 lp->d_ntracks = ccg->ccg_ntracks;
1444 lp->d_ncylinders = ccg->ccg_ncylinders;
1445 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1447 strncpy(lp->d_typename, "ccd", sizeof(lp->d_typename));
1448 lp->d_type = DTYPE_CCD;
1449 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1450 lp->d_rpm = 3600;
1451 lp->d_interleave = 1;
1452 lp->d_flags = 0;
1454 lp->d_partitions[RAW_PART].p_offset = 0;
1455 lp->d_partitions[RAW_PART].p_size = cs->sc_size;
1456 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1457 lp->d_npartitions = RAW_PART + 1;
1459 lp->d_magic = DISKMAGIC;
1460 lp->d_magic2 = DISKMAGIC;
1461 lp->d_checksum = dkcksum(cs->sc_dkdev.dk_label);
1465 * Read the disklabel from the ccd. If one is not present, fake one
1466 * up.
1468 static void
1469 ccdgetdisklabel(dev_t dev)
1471 int unit = ccdunit(dev);
1472 struct ccd_softc *cs = &ccd_softc[unit];
1473 const char *errstring;
1474 struct disklabel *lp = cs->sc_dkdev.dk_label;
1475 struct cpu_disklabel *clp = cs->sc_dkdev.dk_cpulabel;
1477 KASSERT(mutex_owned(&cs->sc_dvlock));
1479 memset(clp, 0, sizeof(*clp));
1481 ccdgetdefaultlabel(cs, lp);
1484 * Call the generic disklabel extraction routine.
1486 cs->sc_flags |= CCDF_RLABEL;
1487 if ((cs->sc_flags & CCDF_NOLABEL) != 0)
1488 errstring = "CCDF_NOLABEL set; ignoring on-disk label";
1489 else
1490 errstring = readdisklabel(CCDLABELDEV(dev), ccdstrategy,
1491 cs->sc_dkdev.dk_label, cs->sc_dkdev.dk_cpulabel);
1492 if (errstring)
1493 ccdmakedisklabel(cs);
1494 else {
1495 int i;
1496 struct partition *pp;
1499 * Sanity check whether the found disklabel is valid.
1501 * This is necessary since total size of ccd may vary
1502 * when an interleave is changed even though exactly
1503 * same componets are used, and old disklabel may used
1504 * if that is found.
1506 if (lp->d_secperunit != cs->sc_size)
1507 printf("WARNING: %s: "
1508 "total sector size in disklabel (%d) != "
1509 "the size of ccd (%lu)\n", cs->sc_xname,
1510 lp->d_secperunit, (u_long)cs->sc_size);
1511 for (i = 0; i < lp->d_npartitions; i++) {
1512 pp = &lp->d_partitions[i];
1513 if (pp->p_offset + pp->p_size > cs->sc_size)
1514 printf("WARNING: %s: end of partition `%c' "
1515 "exceeds the size of ccd (%lu)\n",
1516 cs->sc_xname, 'a' + i, (u_long)cs->sc_size);
1520 #ifdef DEBUG
1521 /* It's actually extremely common to have unlabeled ccds. */
1522 if (ccddebug & CCDB_LABEL)
1523 if (errstring != NULL)
1524 printf("%s: %s\n", cs->sc_xname, errstring);
1525 #endif
1527 /* In-core label now valid. */
1528 cs->sc_flags = (cs->sc_flags | CCDF_VLABEL) & ~CCDF_RLABEL;
1532 * Take care of things one might want to take care of in the event
1533 * that a disklabel isn't present.
1535 static void
1536 ccdmakedisklabel(struct ccd_softc *cs)
1538 struct disklabel *lp = cs->sc_dkdev.dk_label;
1541 * For historical reasons, if there's no disklabel present
1542 * the raw partition must be marked FS_BSDFFS.
1544 lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
1546 strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
1548 lp->d_checksum = dkcksum(lp);
1551 #ifdef DEBUG
1552 static void
1553 printiinfo(struct ccdiinfo *ii)
1555 int ix, i;
1557 for (ix = 0; ii->ii_ndisk; ix++, ii++) {
1558 printf(" itab[%d]: #dk %d sblk %" PRId64 " soff %" PRId64,
1559 ix, ii->ii_ndisk, ii->ii_startblk, ii->ii_startoff);
1560 for (i = 0; i < ii->ii_ndisk; i++)
1561 printf(" %d", ii->ii_index[i]);
1562 printf("\n");
1565 #endif
1567 #ifdef _MODULE
1569 #include <sys/module.h>
1571 MODULE(MODULE_CLASS_DRIVER, ccd, NULL);
1573 static int
1574 ccd_modcmd(modcmd_t cmd, void *arg)
1576 int bmajor = -1, cmajor = -1, error = 0;
1578 switch (cmd) {
1579 case MODULE_CMD_INIT:
1580 ccdattach(4);
1582 return devsw_attach("ccd", &ccd_bdevsw, &bmajor,
1583 &ccd_cdevsw, &cmajor);
1584 break;
1586 case MODULE_CMD_FINI:
1587 return devsw_detach(&ccd_bdevsw, &ccd_cdevsw);
1588 break;
1590 case MODULE_CMD_STAT:
1591 return ENOTTY;
1593 default:
1594 return ENOTTY;
1597 return error;
1600 #endif