ccd.c revision 1.143.10.2 1 /* $NetBSD: ccd.c,v 1.143.10.2 2013/06/23 06:20:16 tls Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997, 1998, 1999, 2007, 2009 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, and by Andrew Doran.
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 *
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.
30 */
31
32 /*
33 * Copyright (c) 1988 University of Utah.
34 * Copyright (c) 1990, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * This code is derived from software contributed to Berkeley by
38 * the Systems Programming Group of the University of Utah Computer
39 * Science Department.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * from: Utah $Hdr: cd.c 1.6 90/11/28$
66 *
67 * @(#)cd.c 8.2 (Berkeley) 11/16/93
68 */
69
70 /*
71 * "Concatenated" disk driver.
72 *
73 * Notes on concurrency:
74 *
75 * => sc_dvlock serializes access to the device nodes, excluding block I/O.
76 *
77 * => sc_iolock serializes access to (sc_flags & CCDF_INITED), disk stats,
78 * sc_stop, sc_bufq and b_resid from master buffers.
79 *
80 * => a combination of CCDF_INITED, sc_inflight, and sc_iolock is used to
81 * serialize I/O and configuration changes.
82 *
83 * => the in-core disk label does not change while the device is open.
84 *
85 * On memory consumption: ccd fans out I/O requests and so needs to
86 * allocate memory. If the system is desperately low on memory, we
87 * single thread I/O.
88 */
89
90 #include <sys/cdefs.h>
91 __KERNEL_RCSID(0, "$NetBSD: ccd.c,v 1.143.10.2 2013/06/23 06:20:16 tls Exp $");
92
93 #include <sys/param.h>
94 #include <sys/systm.h>
95 #include <sys/kernel.h>
96 #include <sys/proc.h>
97 #include <sys/errno.h>
98 #include <sys/buf.h>
99 #include <sys/kmem.h>
100 #include <sys/pool.h>
101 #include <sys/module.h>
102 #include <sys/namei.h>
103 #include <sys/stat.h>
104 #include <sys/ioctl.h>
105 #include <sys/disklabel.h>
106 #include <sys/device.h>
107 #include <sys/disk.h>
108 #include <sys/syslog.h>
109 #include <sys/fcntl.h>
110 #include <sys/vnode.h>
111 #include <sys/conf.h>
112 #include <sys/mutex.h>
113 #include <sys/queue.h>
114 #include <sys/kauth.h>
115 #include <sys/kthread.h>
116 #include <sys/bufq.h>
117 #include <sys/sysctl.h>
118
119 #include <uvm/uvm_extern.h>
120
121 #include <dev/ccdvar.h>
122 #include <dev/dkvar.h>
123
124 #if defined(CCDDEBUG) && !defined(DEBUG)
125 #define DEBUG
126 #endif
127
128 #ifdef DEBUG
129 #define CCDB_FOLLOW 0x01
130 #define CCDB_INIT 0x02
131 #define CCDB_IO 0x04
132 #define CCDB_LABEL 0x08
133 #define CCDB_VNODE 0x10
134 int ccddebug = 0x00;
135 #endif
136
137 #define ccdunit(x) DISKUNIT(x)
138
139 struct ccdbuf {
140 struct buf cb_buf; /* new I/O buf */
141 struct buf *cb_obp; /* ptr. to original I/O buf */
142 struct ccd_softc *cb_sc; /* pointer to ccd softc */
143 int cb_comp; /* target component */
144 SIMPLEQ_ENTRY(ccdbuf) cb_q; /* fifo of component buffers */
145 };
146
147 /* component buffer pool */
148 static pool_cache_t ccd_cache;
149
150 #define CCD_GETBUF() pool_cache_get(ccd_cache, PR_WAITOK)
151 #define CCD_PUTBUF(cbp) pool_cache_put(ccd_cache, cbp)
152
153 #define CCDLABELDEV(dev) \
154 (MAKEDISKDEV(major((dev)), ccdunit((dev)), RAW_PART))
155
156 /* called by main() at boot time */
157 void ccdattach(int);
158
159 /* called by biodone() at interrupt time */
160 static void ccdiodone(struct buf *);
161
162 static void ccdinterleave(struct ccd_softc *);
163 static int ccdinit(struct ccd_softc *, char **, struct vnode **,
164 struct lwp *);
165 static struct ccdbuf *ccdbuffer(struct ccd_softc *, struct buf *,
166 daddr_t, void *, long);
167 static void ccdgetdefaultlabel(struct ccd_softc *, struct disklabel *);
168 static void ccdgetdisklabel(dev_t);
169 static void ccdmakedisklabel(struct ccd_softc *);
170 static void ccdstart(struct ccd_softc *);
171 static void ccdthread(void *);
172 static struct ccd_softc *ccdget(int);
173
174 static dev_type_open(ccdopen);
175 static dev_type_close(ccdclose);
176 static dev_type_read(ccdread);
177 static dev_type_write(ccdwrite);
178 static dev_type_ioctl(ccdioctl);
179 static dev_type_strategy(ccdstrategy);
180 static dev_type_size(ccdsize);
181
182 const struct bdevsw ccd_bdevsw = {
183 .d_open = ccdopen,
184 .d_close = ccdclose,
185 .d_strategy = ccdstrategy,
186 .d_ioctl = ccdioctl,
187 .d_dump = nodump,
188 .d_psize = ccdsize,
189 .d_flag = D_DISK | D_MPSAFE
190 };
191
192 const struct cdevsw ccd_cdevsw = {
193 .d_open = ccdopen,
194 .d_close = ccdclose,
195 .d_read = ccdread,
196 .d_write = ccdwrite,
197 .d_ioctl = ccdioctl,
198 .d_stop = nostop,
199 .d_tty = notty,
200 .d_poll = nopoll,
201 .d_mmap = nommap,
202 .d_kqfilter = nokqfilter,
203 .d_flag = D_DISK | D_MPSAFE
204 };
205
206 #ifdef DEBUG
207 static void printiinfo(struct ccdiinfo *);
208 #endif
209
210 static LIST_HEAD(, ccd_softc) ccds = LIST_HEAD_INITIALIZER(ccds);
211 static kmutex_t ccd_lock;
212
213 static void
214 ccdminphys(struct buf *bp)
215 {
216 struct ccd_softc *cs;
217 long xmax;
218 int unit = ccdunit(bp->b_dev);
219
220 cs = ccdget(unit);
221
222 if (__predict_false(cs == NULL)) {
223 panic("minphys called on missing ccd unit %d", unit);
224 }
225 xmax = cs->sc_maxphys;
226
227 if (bp->b_bcount > xmax)
228 bp->b_bcount = xmax;
229 }
230
231 const struct dkdriver ccd_dkdriver = { ccdstrategy, ccdminphys };
232
233 static struct ccd_softc *
234 ccdcreate(int unit) {
235 struct ccd_softc *sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
236 if (sc == NULL) {
237 #ifdef DIAGNOSTIC
238 printf("%s: out of memory\n", __func__);
239 #endif
240 return NULL;
241 }
242 /* Initialize per-softc structures. */
243 snprintf(sc->sc_xname, sizeof(sc->sc_xname), "ccd%d", unit);
244 mutex_init(&sc->sc_dvlock, MUTEX_DEFAULT, IPL_NONE);
245 sc->sc_iolock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
246 cv_init(&sc->sc_stop, "ccdstop");
247 cv_init(&sc->sc_push, "ccdthr");
248 disk_init(&sc->sc_dkdev, sc->sc_xname, &ccd_dkdriver); /* XXX */
249 return sc;
250 }
251
252 static void
253 ccddestroy(struct ccd_softc *sc) {
254 mutex_obj_free(sc->sc_iolock);
255 mutex_destroy(&sc->sc_dvlock);
256 cv_destroy(&sc->sc_stop);
257 cv_destroy(&sc->sc_push);
258 disk_destroy(&sc->sc_dkdev);
259 kmem_free(sc, sizeof(*sc));
260 }
261
262 static struct ccd_softc *
263 ccdget(int unit) {
264 struct ccd_softc *sc;
265 if (unit < 0) {
266 #ifdef DIAGNOSTIC
267 panic("%s: unit %d!", __func__, unit);
268 #endif
269 return NULL;
270 }
271 mutex_enter(&ccd_lock);
272 LIST_FOREACH(sc, &ccds, sc_link) {
273 if (sc->sc_unit == unit) {
274 mutex_exit(&ccd_lock);
275 return sc;
276 }
277 }
278 mutex_exit(&ccd_lock);
279 if ((sc = ccdcreate(unit)) == NULL)
280 return NULL;
281 mutex_enter(&ccd_lock);
282 LIST_INSERT_HEAD(&ccds, sc, sc_link);
283 mutex_exit(&ccd_lock);
284 return sc;
285 }
286
287 static void
288 ccdput(struct ccd_softc *sc) {
289 mutex_enter(&ccd_lock);
290 LIST_REMOVE(sc, sc_link);
291 mutex_exit(&ccd_lock);
292 ccddestroy(sc);
293 }
294
295 /*
296 * Called by main() during pseudo-device attachment. All we need
297 * to do is allocate enough space for devices to be configured later.
298 */
299 void
300 ccdattach(int num)
301 {
302 mutex_init(&ccd_lock, MUTEX_DEFAULT, IPL_NONE);
303
304 /* Initialize the component buffer pool. */
305 ccd_cache = pool_cache_init(sizeof(struct ccdbuf), 0,
306 0, 0, "ccdbuf", NULL, IPL_BIO, NULL, NULL, NULL);
307 }
308
309 static int
310 ccdinit(struct ccd_softc *cs, char **cpaths, struct vnode **vpp,
311 struct lwp *l)
312 {
313 struct ccdcinfo *ci = NULL;
314 int ix;
315 struct vattr va;
316 struct ccdgeom *ccg = &cs->sc_geom;
317 char *tmppath;
318 int error, path_alloced;
319 uint64_t psize, minsize;
320 unsigned secsize, maxsecsize;
321
322 #ifdef DEBUG
323 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
324 printf("%s: ccdinit\n", cs->sc_xname);
325 #endif
326
327 /* Allocate space for the component info. */
328 cs->sc_cinfo = kmem_alloc(cs->sc_nccdisks * sizeof(*cs->sc_cinfo),
329 KM_SLEEP);
330 tmppath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
331
332 cs->sc_size = 0;
333 cs->sc_maxphys = MACHINE_MAXPHYS;
334
335 /*
336 * Verify that each component piece exists and record
337 * relevant information about it.
338 */
339 maxsecsize = 0;
340 minsize = 0;
341 for (ix = 0, path_alloced = 0; ix < cs->sc_nccdisks; ix++) {
342 ci = &cs->sc_cinfo[ix];
343 ci->ci_vp = vpp[ix];
344 struct disk *diskp;
345
346 /*
347 * Copy in the pathname of the component.
348 */
349 memset(tmppath, 0, MAXPATHLEN); /* sanity */
350 error = copyinstr(cpaths[ix], tmppath,
351 MAXPATHLEN, &ci->ci_pathlen);
352 if (ci->ci_pathlen == 0)
353 error = EINVAL;
354 if (error) {
355 #ifdef DEBUG
356 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
357 printf("%s: can't copy path, error = %d\n",
358 cs->sc_xname, error);
359 #endif
360 goto out;
361 }
362 ci->ci_path = kmem_alloc(ci->ci_pathlen, KM_SLEEP);
363 memcpy(ci->ci_path, tmppath, ci->ci_pathlen);
364 path_alloced++;
365
366 /*
367 * XXX: Cache the component's dev_t.
368 */
369 vn_lock(vpp[ix], LK_SHARED | LK_RETRY);
370 error = VOP_GETATTR(vpp[ix], &va, l->l_cred);
371 VOP_UNLOCK(vpp[ix]);
372 if (error != 0) {
373 #ifdef DEBUG
374 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
375 printf("%s: %s: getattr failed %s = %d\n",
376 cs->sc_xname, ci->ci_path,
377 "error", error);
378 #endif
379 goto out;
380 }
381 ci->ci_dev = va.va_rdev;
382 if ((diskp = disk_find_blk(ci->ci_dev)) == NULL) {
383 panic("no disk for device %d %d", major(ci->ci_dev),
384 DISKUNIT(ci->ci_dev));
385 }
386 cs->sc_maxphys = MIN(cs->sc_maxphys, disk_maxphys(diskp));
387
388 /*
389 * Get partition information for the component.
390 */
391 error = getdisksize(vpp[ix], &psize, &secsize);
392 if (error) {
393 #ifdef DEBUG
394 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
395 printf("%s: %s: disksize failed, error = %d\n",
396 cs->sc_xname, ci->ci_path, error);
397 #endif
398 goto out;
399 }
400
401 /*
402 * Calculate the size, truncating to an interleave
403 * boundary if necessary.
404 */
405 maxsecsize = secsize > maxsecsize ? secsize : maxsecsize;
406 if (cs->sc_ileave > 1)
407 psize -= psize % cs->sc_ileave;
408
409 if (psize == 0) {
410 #ifdef DEBUG
411 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
412 printf("%s: %s: size == 0\n",
413 cs->sc_xname, ci->ci_path);
414 #endif
415 error = ENODEV;
416 goto out;
417 }
418
419 if (minsize == 0 || psize < minsize)
420 minsize = psize;
421 ci->ci_size = psize;
422 cs->sc_size += psize;
423 }
424
425 /*
426 * Don't allow the interleave to be smaller than
427 * the biggest component sector.
428 */
429 if ((cs->sc_ileave > 0) &&
430 (cs->sc_ileave < (maxsecsize / DEV_BSIZE))) {
431 #ifdef DEBUG
432 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
433 printf("%s: interleave must be at least %d\n",
434 cs->sc_xname, (maxsecsize / DEV_BSIZE));
435 #endif
436 error = EINVAL;
437 goto out;
438 }
439
440 /*
441 * If uniform interleave is desired set all sizes to that of
442 * the smallest component.
443 */
444 if (cs->sc_flags & CCDF_UNIFORM) {
445 for (ci = cs->sc_cinfo;
446 ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
447 ci->ci_size = minsize;
448
449 cs->sc_size = cs->sc_nccdisks * minsize;
450 }
451
452 /*
453 * Construct the interleave table.
454 */
455 ccdinterleave(cs);
456
457 /*
458 * Create pseudo-geometry based on 1MB cylinders. It's
459 * pretty close.
460 */
461 ccg->ccg_secsize = DEV_BSIZE;
462 ccg->ccg_ntracks = 1;
463 ccg->ccg_nsectors = 1024 * (1024 / ccg->ccg_secsize);
464 ccg->ccg_ncylinders = cs->sc_size / ccg->ccg_nsectors;
465
466 /*
467 * Create thread to handle deferred I/O.
468 */
469 cs->sc_zap = false;
470 error = kthread_create(PRI_BIO, KTHREAD_MPSAFE, NULL, ccdthread,
471 cs, &cs->sc_thread, "%s", cs->sc_xname);
472 if (error) {
473 printf("ccdinit: can't create thread: %d\n", error);
474 goto out;
475 }
476
477 /*
478 * Only now that everything is set up can we enable the device.
479 */
480 mutex_enter(cs->sc_iolock);
481 cs->sc_flags |= CCDF_INITED;
482 mutex_exit(cs->sc_iolock);
483 kmem_free(tmppath, MAXPATHLEN);
484 return (0);
485
486 out:
487 for (ix = 0; ix < path_alloced; ix++) {
488 kmem_free(cs->sc_cinfo[ix].ci_path,
489 cs->sc_cinfo[ix].ci_pathlen);
490 }
491 kmem_free(cs->sc_cinfo, cs->sc_nccdisks * sizeof(struct ccdcinfo));
492 kmem_free(tmppath, MAXPATHLEN);
493 return (error);
494 }
495
496 static void
497 ccdinterleave(struct ccd_softc *cs)
498 {
499 struct ccdcinfo *ci, *smallci;
500 struct ccdiinfo *ii;
501 daddr_t bn, lbn;
502 int ix;
503 u_long size;
504
505 #ifdef DEBUG
506 if (ccddebug & CCDB_INIT)
507 printf("ccdinterleave(%p): ileave %d\n", cs, cs->sc_ileave);
508 #endif
509 /*
510 * Allocate an interleave table.
511 * Chances are this is too big, but we don't care.
512 */
513 size = (cs->sc_nccdisks + 1) * sizeof(struct ccdiinfo);
514 cs->sc_itable = kmem_zalloc(size, KM_SLEEP);
515
516 /*
517 * Trivial case: no interleave (actually interleave of disk size).
518 * Each table entry represents a single component in its entirety.
519 */
520 if (cs->sc_ileave == 0) {
521 bn = 0;
522 ii = cs->sc_itable;
523
524 for (ix = 0; ix < cs->sc_nccdisks; ix++) {
525 /* Allocate space for ii_index. */
526 ii->ii_indexsz = sizeof(int);
527 ii->ii_index = kmem_alloc(ii->ii_indexsz, KM_SLEEP);
528 ii->ii_ndisk = 1;
529 ii->ii_startblk = bn;
530 ii->ii_startoff = 0;
531 ii->ii_index[0] = ix;
532 bn += cs->sc_cinfo[ix].ci_size;
533 ii++;
534 }
535 ii->ii_ndisk = 0;
536 #ifdef DEBUG
537 if (ccddebug & CCDB_INIT)
538 printiinfo(cs->sc_itable);
539 #endif
540 return;
541 }
542
543 /*
544 * The following isn't fast or pretty; it doesn't have to be.
545 */
546 size = 0;
547 bn = lbn = 0;
548 for (ii = cs->sc_itable; ; ii++) {
549 /* Allocate space for ii_index. */
550 ii->ii_indexsz = sizeof(int) * cs->sc_nccdisks;
551 ii->ii_index = kmem_alloc(ii->ii_indexsz, KM_SLEEP);
552
553 /*
554 * Locate the smallest of the remaining components
555 */
556 smallci = NULL;
557 for (ci = cs->sc_cinfo;
558 ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
559 if (ci->ci_size > size &&
560 (smallci == NULL ||
561 ci->ci_size < smallci->ci_size))
562 smallci = ci;
563
564 /*
565 * Nobody left, all done
566 */
567 if (smallci == NULL) {
568 ii->ii_ndisk = 0;
569 break;
570 }
571
572 /*
573 * Record starting logical block and component offset
574 */
575 ii->ii_startblk = bn / cs->sc_ileave;
576 ii->ii_startoff = lbn;
577
578 /*
579 * Determine how many disks take part in this interleave
580 * and record their indices.
581 */
582 ix = 0;
583 for (ci = cs->sc_cinfo;
584 ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
585 if (ci->ci_size >= smallci->ci_size)
586 ii->ii_index[ix++] = ci - cs->sc_cinfo;
587 ii->ii_ndisk = ix;
588 bn += ix * (smallci->ci_size - size);
589 lbn = smallci->ci_size / cs->sc_ileave;
590 size = smallci->ci_size;
591 }
592 #ifdef DEBUG
593 if (ccddebug & CCDB_INIT)
594 printiinfo(cs->sc_itable);
595 #endif
596 }
597
598 /* ARGSUSED */
599 static int
600 ccdopen(dev_t dev, int flags, int fmt, struct lwp *l)
601 {
602 int unit = ccdunit(dev);
603 struct ccd_softc *cs;
604 struct disklabel *lp;
605 int error = 0, part, pmask;
606
607 #ifdef DEBUG
608 if (ccddebug & CCDB_FOLLOW)
609 printf("ccdopen(0x%"PRIx64", 0x%x)\n", dev, flags);
610 #endif
611 if ((cs = ccdget(unit)) == NULL)
612 return ENXIO;
613
614 mutex_enter(&cs->sc_dvlock);
615
616 lp = cs->sc_dkdev.dk_label;
617
618 part = DISKPART(dev);
619 pmask = (1 << part);
620
621 /*
622 * If we're initialized, check to see if there are any other
623 * open partitions. If not, then it's safe to update
624 * the in-core disklabel. Only read the disklabel if it is
625 * not already valid.
626 */
627 if ((cs->sc_flags & (CCDF_INITED|CCDF_VLABEL)) == CCDF_INITED &&
628 cs->sc_dkdev.dk_openmask == 0)
629 ccdgetdisklabel(dev);
630
631 /* Check that the partition exists. */
632 if (part != RAW_PART) {
633 if (((cs->sc_flags & CCDF_INITED) == 0) ||
634 ((part >= lp->d_npartitions) ||
635 (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
636 error = ENXIO;
637 goto done;
638 }
639 }
640
641 /* Prevent our unit from being unconfigured while open. */
642 switch (fmt) {
643 case S_IFCHR:
644 cs->sc_dkdev.dk_copenmask |= pmask;
645 break;
646
647 case S_IFBLK:
648 cs->sc_dkdev.dk_bopenmask |= pmask;
649 break;
650 }
651 cs->sc_dkdev.dk_openmask =
652 cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
653
654 done:
655 mutex_exit(&cs->sc_dvlock);
656 return (error);
657 }
658
659 /* ARGSUSED */
660 static int
661 ccdclose(dev_t dev, int flags, int fmt, struct lwp *l)
662 {
663 int unit = ccdunit(dev);
664 struct ccd_softc *cs;
665 int part;
666
667 #ifdef DEBUG
668 if (ccddebug & CCDB_FOLLOW)
669 printf("ccdclose(0x%"PRIx64", 0x%x)\n", dev, flags);
670 #endif
671
672 if ((cs = ccdget(unit)) == NULL)
673 return ENXIO;
674
675 mutex_enter(&cs->sc_dvlock);
676
677 part = DISKPART(dev);
678
679 /* ...that much closer to allowing unconfiguration... */
680 switch (fmt) {
681 case S_IFCHR:
682 cs->sc_dkdev.dk_copenmask &= ~(1 << part);
683 break;
684
685 case S_IFBLK:
686 cs->sc_dkdev.dk_bopenmask &= ~(1 << part);
687 break;
688 }
689 cs->sc_dkdev.dk_openmask =
690 cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
691
692 if (cs->sc_dkdev.dk_openmask == 0) {
693 if ((cs->sc_flags & CCDF_KLABEL) == 0)
694 cs->sc_flags &= ~CCDF_VLABEL;
695 }
696
697 mutex_exit(&cs->sc_dvlock);
698 return (0);
699 }
700
701 static bool
702 ccdbackoff(struct ccd_softc *cs)
703 {
704
705 /* XXX Arbitrary, should be a uvm call. */
706 return uvmexp.free < (uvmexp.freemin >> 1) &&
707 disk_isbusy(&cs->sc_dkdev);
708 }
709
710 static void
711 ccdthread(void *cookie)
712 {
713 struct ccd_softc *cs;
714
715 cs = cookie;
716
717 #ifdef DEBUG
718 if (ccddebug & CCDB_FOLLOW)
719 printf("ccdthread: hello\n");
720 #endif
721
722 mutex_enter(cs->sc_iolock);
723 while (__predict_true(!cs->sc_zap)) {
724 if (bufq_peek(cs->sc_bufq) == NULL) {
725 /* Nothing to do. */
726 cv_wait(&cs->sc_push, cs->sc_iolock);
727 continue;
728 }
729 if (ccdbackoff(cs)) {
730 /* Wait for memory to become available. */
731 (void)cv_timedwait(&cs->sc_push, cs->sc_iolock, 1);
732 continue;
733 }
734 #ifdef DEBUG
735 if (ccddebug & CCDB_FOLLOW)
736 printf("ccdthread: dispatching I/O\n");
737 #endif
738 ccdstart(cs);
739 mutex_enter(cs->sc_iolock);
740 }
741 cs->sc_thread = NULL;
742 mutex_exit(cs->sc_iolock);
743 #ifdef DEBUG
744 if (ccddebug & CCDB_FOLLOW)
745 printf("ccdthread: goodbye\n");
746 #endif
747 kthread_exit(0);
748 }
749
750 static void
751 ccdstrategy(struct buf *bp)
752 {
753 int unit = ccdunit(bp->b_dev);
754 struct ccd_softc *cs;
755 if ((cs = ccdget(unit)) == NULL)
756 return;
757
758 /* Must be open or reading label. */
759 KASSERT(cs->sc_dkdev.dk_openmask != 0 ||
760 (cs->sc_flags & CCDF_RLABEL) != 0);
761
762 mutex_enter(cs->sc_iolock);
763 /* Synchronize with device init/uninit. */
764 if (__predict_false((cs->sc_flags & CCDF_INITED) == 0)) {
765 mutex_exit(cs->sc_iolock);
766 #ifdef DEBUG
767 if (ccddebug & CCDB_FOLLOW)
768 printf("ccdstrategy: unit %d: not inited\n", unit);
769 #endif
770 bp->b_error = ENXIO;
771 bp->b_resid = bp->b_bcount;
772 biodone(bp);
773 return;
774 }
775
776 /* Defer to thread if system is low on memory. */
777 bufq_put(cs->sc_bufq, bp);
778 if (__predict_false(ccdbackoff(cs))) {
779 mutex_exit(cs->sc_iolock);
780 #ifdef DEBUG
781 if (ccddebug & CCDB_FOLLOW)
782 printf("ccdstrategy: holding off on I/O\n");
783 #endif
784 return;
785 }
786 ccdstart(cs);
787 }
788
789 static void
790 ccdstart(struct ccd_softc *cs)
791 {
792 daddr_t blkno;
793 int wlabel;
794 struct disklabel *lp;
795 long bcount, rcount;
796 struct ccdbuf *cbp;
797 char *addr;
798 daddr_t bn;
799 vnode_t *vp;
800 buf_t *bp;
801
802 KASSERT(mutex_owned(cs->sc_iolock));
803
804 disk_busy(&cs->sc_dkdev);
805 bp = bufq_get(cs->sc_bufq);
806 KASSERT(bp != NULL);
807
808 #ifdef DEBUG
809 if (ccddebug & CCDB_FOLLOW)
810 printf("ccdstart(%s, %p)\n", cs->sc_xname, bp);
811 #endif
812
813 /* If it's a nil transfer, wake up the top half now. */
814 if (bp->b_bcount == 0)
815 goto done;
816
817 lp = cs->sc_dkdev.dk_label;
818
819 /*
820 * Do bounds checking and adjust transfer. If there's an
821 * error, the bounds check will flag that for us. Convert
822 * the partition relative block number to an absolute.
823 */
824 blkno = bp->b_blkno;
825 wlabel = cs->sc_flags & (CCDF_WLABEL|CCDF_LABELLING);
826 if (DISKPART(bp->b_dev) != RAW_PART) {
827 if (bounds_check_with_label(&cs->sc_dkdev, bp, wlabel) <= 0)
828 goto done;
829 blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
830 }
831 mutex_exit(cs->sc_iolock);
832 bp->b_rawblkno = blkno;
833
834 /* Allocate the component buffers and start I/O! */
835 bp->b_resid = bp->b_bcount;
836 bn = bp->b_rawblkno;
837 addr = bp->b_data;
838 for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
839 cbp = ccdbuffer(cs, bp, bn, addr, bcount);
840 rcount = cbp->cb_buf.b_bcount;
841 bn += btodb(rcount);
842 addr += rcount;
843 vp = cbp->cb_buf.b_vp;
844 if ((cbp->cb_buf.b_flags & B_READ) == 0) {
845 mutex_enter(vp->v_interlock);
846 vp->v_numoutput++;
847 mutex_exit(vp->v_interlock);
848 }
849 (void)VOP_STRATEGY(vp, &cbp->cb_buf);
850 }
851 return;
852
853 done:
854 disk_unbusy(&cs->sc_dkdev, 0, 0);
855 cv_broadcast(&cs->sc_stop);
856 cv_broadcast(&cs->sc_push);
857 mutex_exit(cs->sc_iolock);
858 bp->b_resid = bp->b_bcount;
859 biodone(bp);
860 }
861
862 /*
863 * Build a component buffer header.
864 */
865 static struct ccdbuf *
866 ccdbuffer(struct ccd_softc *cs, struct buf *bp, daddr_t bn, void *addr,
867 long bcount)
868 {
869 struct ccdcinfo *ci;
870 struct ccdbuf *cbp;
871 daddr_t cbn, cboff;
872 u_int64_t cbc;
873 int ccdisk;
874
875 #ifdef DEBUG
876 if (ccddebug & CCDB_IO)
877 printf("ccdbuffer(%p, %p, %" PRId64 ", %p, %ld)\n",
878 cs, bp, bn, addr, bcount);
879 #endif
880 /*
881 * Determine which component bn falls in.
882 */
883 cbn = bn;
884 cboff = 0;
885
886 /*
887 * Serially concatenated
888 */
889 if (cs->sc_ileave == 0) {
890 daddr_t sblk;
891
892 sblk = 0;
893 for (ccdisk = 0, ci = &cs->sc_cinfo[ccdisk];
894 cbn >= sblk + ci->ci_size;
895 ccdisk++, ci = &cs->sc_cinfo[ccdisk])
896 sblk += ci->ci_size;
897 cbn -= sblk;
898 }
899 /*
900 * Interleaved
901 */
902 else {
903 struct ccdiinfo *ii;
904 int off;
905
906 cboff = cbn % cs->sc_ileave;
907 cbn /= cs->sc_ileave;
908 for (ii = cs->sc_itable; ii->ii_ndisk; ii++)
909 if (ii->ii_startblk > cbn)
910 break;
911 ii--;
912 off = cbn - ii->ii_startblk;
913 if (ii->ii_ndisk == 1) {
914 ccdisk = ii->ii_index[0];
915 cbn = ii->ii_startoff + off;
916 } else {
917 ccdisk = ii->ii_index[off % ii->ii_ndisk];
918 cbn = ii->ii_startoff + off / ii->ii_ndisk;
919 }
920 cbn *= cs->sc_ileave;
921 ci = &cs->sc_cinfo[ccdisk];
922 }
923
924 /*
925 * Fill in the component buf structure.
926 */
927 cbp = CCD_GETBUF();
928 KASSERT(cbp != NULL);
929 buf_init(&cbp->cb_buf);
930 cbp->cb_buf.b_flags = bp->b_flags;
931 cbp->cb_buf.b_oflags = bp->b_oflags;
932 cbp->cb_buf.b_cflags = bp->b_cflags;
933 cbp->cb_buf.b_iodone = ccdiodone;
934 cbp->cb_buf.b_proc = bp->b_proc;
935 cbp->cb_buf.b_dev = ci->ci_dev;
936 cbp->cb_buf.b_blkno = cbn + cboff;
937 cbp->cb_buf.b_data = addr;
938 cbp->cb_buf.b_vp = ci->ci_vp;
939 cbp->cb_buf.b_objlock = ci->ci_vp->v_interlock;
940 if (cs->sc_ileave == 0)
941 cbc = dbtob((u_int64_t)(ci->ci_size - cbn));
942 else
943 cbc = dbtob((u_int64_t)(cs->sc_ileave - cboff));
944 cbp->cb_buf.b_bcount = cbc < bcount ? cbc : bcount;
945
946 /*
947 * context for ccdiodone
948 */
949 cbp->cb_obp = bp;
950 cbp->cb_sc = cs;
951 cbp->cb_comp = ccdisk;
952
953 BIO_COPYPRIO(&cbp->cb_buf, bp);
954
955 #ifdef DEBUG
956 if (ccddebug & CCDB_IO)
957 printf(" dev 0x%"PRIx64"(u%lu): cbp %p bn %" PRId64 " addr %p"
958 " bcnt %d\n",
959 ci->ci_dev, (unsigned long) (ci-cs->sc_cinfo), cbp,
960 cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
961 cbp->cb_buf.b_bcount);
962 #endif
963
964 return (cbp);
965 }
966
967 /*
968 * Called at interrupt time.
969 * Mark the component as done and if all components are done,
970 * take a ccd interrupt.
971 */
972 static void
973 ccdiodone(struct buf *vbp)
974 {
975 struct ccdbuf *cbp = (struct ccdbuf *) vbp;
976 struct buf *bp = cbp->cb_obp;
977 struct ccd_softc *cs = cbp->cb_sc;
978 int count;
979
980 #ifdef DEBUG
981 if (ccddebug & CCDB_FOLLOW)
982 printf("ccdiodone(%p)\n", cbp);
983 if (ccddebug & CCDB_IO) {
984 printf("ccdiodone: bp %p bcount %d resid %d\n",
985 bp, bp->b_bcount, bp->b_resid);
986 printf(" dev 0x%"PRIx64"(u%d), cbp %p bn %" PRId64 " addr %p"
987 " bcnt %d\n",
988 cbp->cb_buf.b_dev, cbp->cb_comp, cbp,
989 cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
990 cbp->cb_buf.b_bcount);
991 }
992 #endif
993
994 if (cbp->cb_buf.b_error != 0) {
995 bp->b_error = cbp->cb_buf.b_error;
996 printf("%s: error %d on component %d\n",
997 cs->sc_xname, bp->b_error, cbp->cb_comp);
998 }
999 count = cbp->cb_buf.b_bcount;
1000 buf_destroy(&cbp->cb_buf);
1001 CCD_PUTBUF(cbp);
1002
1003 /*
1004 * If all done, "interrupt".
1005 */
1006 mutex_enter(cs->sc_iolock);
1007 bp->b_resid -= count;
1008 if (bp->b_resid < 0)
1009 panic("ccdiodone: count");
1010 if (bp->b_resid == 0) {
1011 /*
1012 * Request is done for better or worse, wakeup the top half.
1013 */
1014 if (bp->b_error != 0)
1015 bp->b_resid = bp->b_bcount;
1016 disk_unbusy(&cs->sc_dkdev, (bp->b_bcount - bp->b_resid),
1017 (bp->b_flags & B_READ));
1018 if (!disk_isbusy(&cs->sc_dkdev)) {
1019 if (bufq_peek(cs->sc_bufq) != NULL) {
1020 cv_broadcast(&cs->sc_push);
1021 }
1022 cv_broadcast(&cs->sc_stop);
1023 }
1024 mutex_exit(cs->sc_iolock);
1025 biodone(bp);
1026 } else
1027 mutex_exit(cs->sc_iolock);
1028 }
1029
1030 /* ARGSUSED */
1031 static int
1032 ccdread(dev_t dev, struct uio *uio, int flags)
1033 {
1034 int unit = ccdunit(dev);
1035 struct ccd_softc *cs;
1036
1037 #ifdef DEBUG
1038 if (ccddebug & CCDB_FOLLOW)
1039 printf("ccdread(0x%"PRIx64", %p)\n", dev, uio);
1040 #endif
1041 if ((cs = ccdget(unit)) == NULL)
1042 return 0;
1043
1044 /* Unlocked advisory check, ccdstrategy check is synchronous. */
1045 if ((cs->sc_flags & CCDF_INITED) == 0)
1046 return (ENXIO);
1047
1048 return (physio(ccdstrategy, NULL, dev, B_READ, minphys, uio));
1049 }
1050
1051 /* ARGSUSED */
1052 static int
1053 ccdwrite(dev_t dev, struct uio *uio, int flags)
1054 {
1055 int unit = ccdunit(dev);
1056 struct ccd_softc *cs;
1057
1058 #ifdef DEBUG
1059 if (ccddebug & CCDB_FOLLOW)
1060 printf("ccdwrite(0x%"PRIx64", %p)\n", dev, uio);
1061 #endif
1062 if ((cs = ccdget(unit)) == NULL)
1063 return ENOENT;
1064
1065 /* Unlocked advisory check, ccdstrategy check is synchronous. */
1066 if ((cs->sc_flags & CCDF_INITED) == 0)
1067 return (ENXIO);
1068
1069 return (physio(ccdstrategy, NULL, dev, B_WRITE, minphys, uio));
1070 }
1071
1072 static int
1073 ccdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1074 {
1075 int unit = ccdunit(dev);
1076 int i, j, lookedup = 0, error = 0;
1077 int part, pmask;
1078 struct ccd_softc *cs;
1079 struct ccd_ioctl *ccio = (struct ccd_ioctl *)data;
1080 kauth_cred_t uc;
1081 char **cpp;
1082 struct pathbuf *pb;
1083 struct vnode **vpp;
1084 #ifdef __HAVE_OLD_DISKLABEL
1085 struct disklabel newlabel;
1086 #endif
1087
1088 if ((cs = ccdget(unit)) == NULL)
1089 return ENOENT;
1090 uc = kauth_cred_get();
1091
1092 /* Must be open for writes for these commands... */
1093 switch (cmd) {
1094 case CCDIOCSET:
1095 case CCDIOCCLR:
1096 case DIOCSDINFO:
1097 case DIOCWDINFO:
1098 #ifdef __HAVE_OLD_DISKLABEL
1099 case ODIOCSDINFO:
1100 case ODIOCWDINFO:
1101 #endif
1102 case DIOCKLABEL:
1103 case DIOCWLABEL:
1104 if ((flag & FWRITE) == 0)
1105 return (EBADF);
1106 }
1107
1108 mutex_enter(&cs->sc_dvlock);
1109
1110 /* Must be initialized for these... */
1111 switch (cmd) {
1112 case CCDIOCCLR:
1113 case DIOCGDINFO:
1114 case DIOCCACHESYNC:
1115 case DIOCSDINFO:
1116 case DIOCWDINFO:
1117 case DIOCGPART:
1118 case DIOCWLABEL:
1119 case DIOCKLABEL:
1120 case DIOCGDEFLABEL:
1121 #ifdef __HAVE_OLD_DISKLABEL
1122 case ODIOCGDINFO:
1123 case ODIOCSDINFO:
1124 case ODIOCWDINFO:
1125 case ODIOCGDEFLABEL:
1126 #endif
1127 if ((cs->sc_flags & CCDF_INITED) == 0) {
1128 error = ENXIO;
1129 goto out;
1130 }
1131 }
1132
1133 switch (cmd) {
1134 case CCDIOCSET:
1135 if (cs->sc_flags & CCDF_INITED) {
1136 error = EBUSY;
1137 goto out;
1138 }
1139
1140 /* Validate the flags. */
1141 if ((ccio->ccio_flags & CCDF_USERMASK) != ccio->ccio_flags) {
1142 error = EINVAL;
1143 goto out;
1144 }
1145
1146 if (ccio->ccio_ndisks > CCD_MAXNDISKS ||
1147 ccio->ccio_ndisks == 0) {
1148 error = EINVAL;
1149 goto out;
1150 }
1151
1152 /* Fill in some important bits. */
1153 cs->sc_ileave = ccio->ccio_ileave;
1154 cs->sc_nccdisks = ccio->ccio_ndisks;
1155 cs->sc_flags = ccio->ccio_flags & CCDF_USERMASK;
1156
1157 /*
1158 * Allocate space for and copy in the array of
1159 * componet pathnames and device numbers.
1160 */
1161 cpp = kmem_alloc(ccio->ccio_ndisks * sizeof(*cpp), KM_SLEEP);
1162 vpp = kmem_alloc(ccio->ccio_ndisks * sizeof(*vpp), KM_SLEEP);
1163 error = copyin(ccio->ccio_disks, cpp,
1164 ccio->ccio_ndisks * sizeof(*cpp));
1165 if (error) {
1166 kmem_free(vpp, ccio->ccio_ndisks * sizeof(*vpp));
1167 kmem_free(cpp, ccio->ccio_ndisks * sizeof(*cpp));
1168 goto out;
1169 }
1170
1171 #ifdef DEBUG
1172 if (ccddebug & CCDB_INIT)
1173 for (i = 0; i < ccio->ccio_ndisks; ++i)
1174 printf("ccdioctl: component %d: %p\n",
1175 i, cpp[i]);
1176 #endif
1177
1178 for (i = 0; i < ccio->ccio_ndisks; ++i) {
1179 #ifdef DEBUG
1180 if (ccddebug & CCDB_INIT)
1181 printf("ccdioctl: lookedup = %d\n", lookedup);
1182 #endif
1183 error = pathbuf_copyin(cpp[i], &pb);
1184 if (error == 0) {
1185 error = dk_lookup(pb, l, &vpp[i]);
1186 }
1187 pathbuf_destroy(pb);
1188 if (error != 0) {
1189 for (j = 0; j < lookedup; ++j)
1190 (void)vn_close(vpp[j], FREAD|FWRITE,
1191 uc);
1192 kmem_free(vpp, ccio->ccio_ndisks *
1193 sizeof(*vpp));
1194 kmem_free(cpp, ccio->ccio_ndisks *
1195 sizeof(*cpp));
1196 goto out;
1197 }
1198 ++lookedup;
1199 }
1200
1201 /* Attach the disk. */
1202 disk_attach(&cs->sc_dkdev);
1203 bufq_alloc(&cs->sc_bufq, "fcfs", 0);
1204
1205 /*
1206 * Initialize the ccd. Fills in the softc for us.
1207 */
1208 if ((error = ccdinit(cs, cpp, vpp, l)) != 0) {
1209 for (j = 0; j < lookedup; ++j)
1210 (void)vn_close(vpp[j], FREAD|FWRITE,
1211 uc);
1212 kmem_free(vpp, ccio->ccio_ndisks * sizeof(*vpp));
1213 kmem_free(cpp, ccio->ccio_ndisks * sizeof(*cpp));
1214 disk_detach(&cs->sc_dkdev);
1215 bufq_free(cs->sc_bufq);
1216 goto out;
1217 }
1218
1219 /* We can free the temporary variables now. */
1220 kmem_free(vpp, ccio->ccio_ndisks * sizeof(*vpp));
1221 kmem_free(cpp, ccio->ccio_ndisks * sizeof(*cpp));
1222
1223 /*
1224 * The ccd has been successfully initialized, so
1225 * we can place it into the array. Don't try to
1226 * read the disklabel until the disk has been attached,
1227 * because space for the disklabel is allocated
1228 * in disk_attach();
1229 */
1230 ccio->ccio_unit = unit;
1231 ccio->ccio_size = cs->sc_size;
1232
1233 /* Try and read the disklabel. */
1234 ccdgetdisklabel(dev);
1235 break;
1236
1237 case CCDIOCCLR:
1238 /*
1239 * Don't unconfigure if any other partitions are open
1240 * or if both the character and block flavors of this
1241 * partition are open.
1242 */
1243 part = DISKPART(dev);
1244 pmask = (1 << part);
1245 if ((cs->sc_dkdev.dk_openmask & ~pmask) ||
1246 ((cs->sc_dkdev.dk_bopenmask & pmask) &&
1247 (cs->sc_dkdev.dk_copenmask & pmask))) {
1248 error = EBUSY;
1249 goto out;
1250 }
1251
1252 /* Stop new I/O, wait for in-flight I/O to complete. */
1253 mutex_enter(cs->sc_iolock);
1254 cs->sc_flags &= ~(CCDF_INITED|CCDF_VLABEL);
1255 cs->sc_zap = true;
1256 while (disk_isbusy(&cs->sc_dkdev) ||
1257 bufq_peek(cs->sc_bufq) != NULL ||
1258 cs->sc_thread != NULL) {
1259 cv_broadcast(&cs->sc_push);
1260 (void)cv_timedwait(&cs->sc_stop, cs->sc_iolock, hz);
1261 }
1262 mutex_exit(cs->sc_iolock);
1263
1264 /*
1265 * Free ccd_softc information and clear entry.
1266 */
1267
1268 /* Close the components and free their pathnames. */
1269 for (i = 0; i < cs->sc_nccdisks; ++i) {
1270 /*
1271 * XXX: this close could potentially fail and
1272 * cause Bad Things. Maybe we need to force
1273 * the close to happen?
1274 */
1275 #ifdef DEBUG
1276 if (ccddebug & CCDB_VNODE)
1277 vprint("CCDIOCCLR: vnode info",
1278 cs->sc_cinfo[i].ci_vp);
1279 #endif
1280 (void)vn_close(cs->sc_cinfo[i].ci_vp, FREAD|FWRITE,
1281 uc);
1282 kmem_free(cs->sc_cinfo[i].ci_path,
1283 cs->sc_cinfo[i].ci_pathlen);
1284 }
1285
1286 /* Free interleave index. */
1287 for (i = 0; cs->sc_itable[i].ii_ndisk; ++i) {
1288 kmem_free(cs->sc_itable[i].ii_index,
1289 cs->sc_itable[i].ii_indexsz);
1290 }
1291
1292 /* Free component info and interleave table. */
1293 kmem_free(cs->sc_cinfo, cs->sc_nccdisks *
1294 sizeof(struct ccdcinfo));
1295 kmem_free(cs->sc_itable, (cs->sc_nccdisks + 1) *
1296 sizeof(struct ccdiinfo));
1297
1298 /* Detatch the disk. */
1299 disk_detach(&cs->sc_dkdev);
1300 bufq_free(cs->sc_bufq);
1301 ccdput(cs);
1302 break;
1303
1304 case DIOCGDINFO:
1305 *(struct disklabel *)data = *(cs->sc_dkdev.dk_label);
1306 break;
1307
1308 #ifdef __HAVE_OLD_DISKLABEL
1309 case ODIOCGDINFO:
1310 newlabel = *(cs->sc_dkdev.dk_label);
1311 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1312 return ENOTTY;
1313 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1314 break;
1315 #endif
1316
1317 case DIOCGPART:
1318 ((struct partinfo *)data)->disklab = cs->sc_dkdev.dk_label;
1319 ((struct partinfo *)data)->part =
1320 &cs->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
1321 break;
1322
1323 case DIOCCACHESYNC:
1324 /*
1325 * XXX Do we really need to care about having a writable
1326 * file descriptor here?
1327 */
1328 if ((flag & FWRITE) == 0)
1329 return (EBADF);
1330
1331 /*
1332 * We pass this call down to all components and report
1333 * the first error we encounter.
1334 */
1335 for (error = 0, i = 0; i < cs->sc_nccdisks; i++) {
1336 j = VOP_IOCTL(cs->sc_cinfo[i].ci_vp, cmd, data,
1337 flag, uc);
1338 if (j != 0 && error == 0)
1339 error = j;
1340 }
1341 break;
1342
1343 case DIOCWDINFO:
1344 case DIOCSDINFO:
1345 #ifdef __HAVE_OLD_DISKLABEL
1346 case ODIOCWDINFO:
1347 case ODIOCSDINFO:
1348 #endif
1349 {
1350 struct disklabel *lp;
1351 #ifdef __HAVE_OLD_DISKLABEL
1352 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1353 memset(&newlabel, 0, sizeof newlabel);
1354 memcpy(&newlabel, data, sizeof (struct olddisklabel));
1355 lp = &newlabel;
1356 } else
1357 #endif
1358 lp = (struct disklabel *)data;
1359
1360 cs->sc_flags |= CCDF_LABELLING;
1361
1362 error = setdisklabel(cs->sc_dkdev.dk_label,
1363 lp, 0, cs->sc_dkdev.dk_cpulabel);
1364 if (error == 0) {
1365 if (cmd == DIOCWDINFO
1366 #ifdef __HAVE_OLD_DISKLABEL
1367 || cmd == ODIOCWDINFO
1368 #endif
1369 )
1370 error = writedisklabel(CCDLABELDEV(dev),
1371 ccdstrategy, cs->sc_dkdev.dk_label,
1372 cs->sc_dkdev.dk_cpulabel);
1373 }
1374
1375 cs->sc_flags &= ~CCDF_LABELLING;
1376 break;
1377 }
1378
1379 case DIOCKLABEL:
1380 if (*(int *)data != 0)
1381 cs->sc_flags |= CCDF_KLABEL;
1382 else
1383 cs->sc_flags &= ~CCDF_KLABEL;
1384 break;
1385
1386 case DIOCWLABEL:
1387 if (*(int *)data != 0)
1388 cs->sc_flags |= CCDF_WLABEL;
1389 else
1390 cs->sc_flags &= ~CCDF_WLABEL;
1391 break;
1392
1393 case DIOCGDEFLABEL:
1394 ccdgetdefaultlabel(cs, (struct disklabel *)data);
1395 break;
1396
1397 #ifdef __HAVE_OLD_DISKLABEL
1398 case ODIOCGDEFLABEL:
1399 ccdgetdefaultlabel(cs, &newlabel);
1400 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1401 return ENOTTY;
1402 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1403 break;
1404 #endif
1405
1406 default:
1407 error = ENOTTY;
1408 }
1409
1410 out:
1411 mutex_exit(&cs->sc_dvlock);
1412 return (error);
1413 }
1414
1415 static int
1416 ccdsize(dev_t dev)
1417 {
1418 struct ccd_softc *cs;
1419 struct disklabel *lp;
1420 int part, unit, omask, size;
1421
1422 unit = ccdunit(dev);
1423 if ((cs = ccdget(unit)) == NULL)
1424 return -1;
1425
1426 if ((cs->sc_flags & CCDF_INITED) == 0)
1427 return (-1);
1428
1429 part = DISKPART(dev);
1430 omask = cs->sc_dkdev.dk_openmask & (1 << part);
1431 lp = cs->sc_dkdev.dk_label;
1432
1433 if (omask == 0 && ccdopen(dev, 0, S_IFBLK, curlwp))
1434 return (-1);
1435
1436 if (lp->d_partitions[part].p_fstype != FS_SWAP)
1437 size = -1;
1438 else
1439 size = lp->d_partitions[part].p_size *
1440 (lp->d_secsize / DEV_BSIZE);
1441
1442 if (omask == 0 && ccdclose(dev, 0, S_IFBLK, curlwp))
1443 return (-1);
1444
1445 return (size);
1446 }
1447
1448 static void
1449 ccdgetdefaultlabel(struct ccd_softc *cs, struct disklabel *lp)
1450 {
1451 struct ccdgeom *ccg = &cs->sc_geom;
1452
1453 memset(lp, 0, sizeof(*lp));
1454
1455 lp->d_secperunit = cs->sc_size;
1456 lp->d_secsize = ccg->ccg_secsize;
1457 lp->d_nsectors = ccg->ccg_nsectors;
1458 lp->d_ntracks = ccg->ccg_ntracks;
1459 lp->d_ncylinders = ccg->ccg_ncylinders;
1460 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1461
1462 strncpy(lp->d_typename, "ccd", sizeof(lp->d_typename));
1463 lp->d_type = DTYPE_CCD;
1464 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1465 lp->d_rpm = 3600;
1466 lp->d_interleave = 1;
1467 lp->d_flags = 0;
1468
1469 lp->d_partitions[RAW_PART].p_offset = 0;
1470 lp->d_partitions[RAW_PART].p_size = cs->sc_size;
1471 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1472 lp->d_npartitions = RAW_PART + 1;
1473
1474 lp->d_magic = DISKMAGIC;
1475 lp->d_magic2 = DISKMAGIC;
1476 lp->d_checksum = dkcksum(cs->sc_dkdev.dk_label);
1477 }
1478
1479 /*
1480 * Read the disklabel from the ccd. If one is not present, fake one
1481 * up.
1482 */
1483 static void
1484 ccdgetdisklabel(dev_t dev)
1485 {
1486 int unit = ccdunit(dev);
1487 struct ccd_softc *cs;
1488 const char *errstring;
1489 struct disklabel *lp;
1490 struct cpu_disklabel *clp;
1491
1492 if ((cs = ccdget(unit)) == NULL)
1493 return;
1494 lp = cs->sc_dkdev.dk_label;
1495 clp = cs->sc_dkdev.dk_cpulabel;
1496 KASSERT(mutex_owned(&cs->sc_dvlock));
1497
1498 memset(clp, 0, sizeof(*clp));
1499
1500 ccdgetdefaultlabel(cs, lp);
1501
1502 /*
1503 * Call the generic disklabel extraction routine.
1504 */
1505 cs->sc_flags |= CCDF_RLABEL;
1506 if ((cs->sc_flags & CCDF_NOLABEL) != 0)
1507 errstring = "CCDF_NOLABEL set; ignoring on-disk label";
1508 else
1509 errstring = readdisklabel(CCDLABELDEV(dev), ccdstrategy,
1510 cs->sc_dkdev.dk_label, cs->sc_dkdev.dk_cpulabel);
1511 if (errstring)
1512 ccdmakedisklabel(cs);
1513 else {
1514 int i;
1515 struct partition *pp;
1516
1517 /*
1518 * Sanity check whether the found disklabel is valid.
1519 *
1520 * This is necessary since total size of ccd may vary
1521 * when an interleave is changed even though exactly
1522 * same componets are used, and old disklabel may used
1523 * if that is found.
1524 */
1525 if (lp->d_secperunit != cs->sc_size)
1526 printf("WARNING: %s: "
1527 "total sector size in disklabel (%d) != "
1528 "the size of ccd (%lu)\n", cs->sc_xname,
1529 lp->d_secperunit, (u_long)cs->sc_size);
1530 for (i = 0; i < lp->d_npartitions; i++) {
1531 pp = &lp->d_partitions[i];
1532 if (pp->p_offset + pp->p_size > cs->sc_size)
1533 printf("WARNING: %s: end of partition `%c' "
1534 "exceeds the size of ccd (%lu)\n",
1535 cs->sc_xname, 'a' + i, (u_long)cs->sc_size);
1536 }
1537 }
1538
1539 #ifdef DEBUG
1540 /* It's actually extremely common to have unlabeled ccds. */
1541 if (ccddebug & CCDB_LABEL)
1542 if (errstring != NULL)
1543 printf("%s: %s\n", cs->sc_xname, errstring);
1544 #endif
1545
1546 /* In-core label now valid. */
1547 cs->sc_flags = (cs->sc_flags | CCDF_VLABEL) & ~CCDF_RLABEL;
1548 }
1549
1550 /*
1551 * Take care of things one might want to take care of in the event
1552 * that a disklabel isn't present.
1553 */
1554 static void
1555 ccdmakedisklabel(struct ccd_softc *cs)
1556 {
1557 struct disklabel *lp = cs->sc_dkdev.dk_label;
1558
1559 /*
1560 * For historical reasons, if there's no disklabel present
1561 * the raw partition must be marked FS_BSDFFS.
1562 */
1563 lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
1564
1565 strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
1566
1567 lp->d_checksum = dkcksum(lp);
1568 }
1569
1570 #ifdef DEBUG
1571 static void
1572 printiinfo(struct ccdiinfo *ii)
1573 {
1574 int ix, i;
1575
1576 for (ix = 0; ii->ii_ndisk; ix++, ii++) {
1577 printf(" itab[%d]: #dk %d sblk %" PRId64 " soff %" PRId64,
1578 ix, ii->ii_ndisk, ii->ii_startblk, ii->ii_startoff);
1579 for (i = 0; i < ii->ii_ndisk; i++)
1580 printf(" %d", ii->ii_index[i]);
1581 printf("\n");
1582 }
1583 }
1584 #endif
1585
1586 MODULE(MODULE_CLASS_DRIVER, ccd, NULL);
1587
1588 static int
1589 ccd_modcmd(modcmd_t cmd, void *arg)
1590 {
1591 int bmajor, cmajor, error = 0;
1592
1593 bmajor = cmajor = -1;
1594
1595 switch (cmd) {
1596 case MODULE_CMD_INIT:
1597 #ifdef _MODULE
1598 ccdattach(4);
1599
1600 return devsw_attach("ccd", &ccd_bdevsw, &bmajor,
1601 &ccd_cdevsw, &cmajor);
1602 #endif
1603 break;
1604
1605 case MODULE_CMD_FINI:
1606 #ifdef _MODULE
1607 return devsw_detach(&ccd_bdevsw, &ccd_cdevsw);
1608 #endif
1609 break;
1610
1611 case MODULE_CMD_STAT:
1612 return ENOTTY;
1613
1614 default:
1615 return ENOTTY;
1616 }
1617
1618 return error;
1619 }
1620
1621 static int
1622 ccd_units_sysctl(SYSCTLFN_ARGS)
1623 {
1624 struct sysctlnode node;
1625 struct ccd_softc *sc;
1626 int error, i, nccd, *units;
1627 size_t size;
1628
1629 nccd = 0;
1630 mutex_enter(&ccd_lock);
1631 LIST_FOREACH(sc, &ccds, sc_link)
1632 nccd++;
1633 mutex_exit(&ccd_lock);
1634
1635 if (nccd != 0) {
1636 size = nccd * sizeof(*units);
1637 units = kmem_zalloc(size, KM_SLEEP);
1638 if (units == NULL)
1639 return ENOMEM;
1640
1641 i = 0;
1642 mutex_enter(&ccd_lock);
1643 LIST_FOREACH(sc, &ccds, sc_link) {
1644 if (i >= nccd)
1645 break;
1646 units[i] = sc->sc_unit;
1647 }
1648 mutex_exit(&ccd_lock);
1649 } else {
1650 units = NULL;
1651 size = 0;
1652 }
1653
1654 node = *rnode;
1655 node.sysctl_data = units;
1656 node.sysctl_size = size;
1657
1658 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1659 if (units)
1660 kmem_free(units, size);
1661 return error;
1662 }
1663
1664 static int
1665 ccd_info_sysctl(SYSCTLFN_ARGS)
1666 {
1667 struct sysctlnode node;
1668 struct ccddiskinfo ccd;
1669 struct ccd_softc *sc;
1670 int unit;
1671
1672 if (newp == NULL || newlen != sizeof(int))
1673 return EINVAL;
1674
1675 unit = *(const int *)newp;
1676 newp = NULL;
1677 newlen = 0;
1678 ccd.ccd_ndisks = ~0;
1679 mutex_enter(&ccd_lock);
1680 LIST_FOREACH(sc, &ccds, sc_link) {
1681 if (sc->sc_unit == unit) {
1682 ccd.ccd_ileave = sc->sc_ileave;
1683 ccd.ccd_size = sc->sc_size;
1684 ccd.ccd_ndisks = sc->sc_nccdisks;
1685 ccd.ccd_flags = sc->sc_flags;
1686 break;
1687 }
1688 }
1689 mutex_exit(&ccd_lock);
1690
1691 if (ccd.ccd_ndisks == ~0)
1692 return ENOENT;
1693
1694 node = *rnode;
1695 node.sysctl_data = &ccd;
1696 node.sysctl_size = sizeof(ccd);
1697
1698 return sysctl_lookup(SYSCTLFN_CALL(&node));
1699 }
1700
1701 static int
1702 ccd_components_sysctl(SYSCTLFN_ARGS)
1703 {
1704 struct sysctlnode node;
1705 int error, unit;
1706 size_t size;
1707 char *names, *p, *ep;
1708 struct ccd_softc *sc;
1709
1710 if (newp == NULL || newlen != sizeof(int))
1711 return EINVAL;
1712
1713 size = 0;
1714 unit = *(const int *)newp;
1715 newp = NULL;
1716 newlen = 0;
1717 mutex_enter(&ccd_lock);
1718 LIST_FOREACH(sc, &ccds, sc_link)
1719 if (sc->sc_unit == unit) {
1720 for (size_t i = 0; i < sc->sc_nccdisks; i++)
1721 size += strlen(sc->sc_cinfo[i].ci_path) + 1;
1722 break;
1723 }
1724 mutex_exit(&ccd_lock);
1725
1726 if (size == 0)
1727 return ENOENT;
1728 names = kmem_zalloc(size, KM_SLEEP);
1729 if (names == NULL)
1730 return ENOMEM;
1731
1732 p = names;
1733 ep = names + size;
1734 mutex_enter(&ccd_lock);
1735 LIST_FOREACH(sc, &ccds, sc_link)
1736 if (sc->sc_unit == unit) {
1737 for (size_t i = 0; i < sc->sc_nccdisks; i++) {
1738 char *d = sc->sc_cinfo[i].ci_path;
1739 while (p < ep && (*p++ = *d++) != '\0')
1740 continue;
1741 }
1742 break;
1743 }
1744 mutex_exit(&ccd_lock);
1745
1746 node = *rnode;
1747 node.sysctl_data = names;
1748 node.sysctl_size = ep - names;
1749
1750 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1751 kmem_free(names, size);
1752 return error;
1753 }
1754
1755 SYSCTL_SETUP(sysctl_kern_ccd_setup, "sysctl kern.ccd subtree setup")
1756 {
1757 const struct sysctlnode *node = NULL;
1758
1759 /* Make sure net.key exists before we register nodes underneath it. */
1760 sysctl_createv(clog, 0, NULL, NULL,
1761 CTLFLAG_PERMANENT,
1762 CTLTYPE_NODE, "kern", NULL,
1763 NULL, 0, NULL, 0,
1764 CTL_KERN, CTL_EOL);
1765 sysctl_createv(clog, 0, NULL, &node,
1766 CTLFLAG_PERMANENT,
1767 CTLTYPE_NODE, "ccd",
1768 SYSCTL_DESCR("ConCatenated Disk state"),
1769 NULL, 0, NULL, 0,
1770 CTL_KERN, CTL_CREATE, CTL_EOL);
1771
1772 if (node == NULL)
1773 return;
1774
1775 sysctl_createv(clog, 0, &node, NULL,
1776 CTLFLAG_PERMANENT | CTLFLAG_READONLY,
1777 CTLTYPE_STRUCT, "units",
1778 SYSCTL_DESCR("List of ccd unit numbers"),
1779 ccd_units_sysctl, 0, NULL, 0,
1780 CTL_CREATE, CTL_EOL);
1781 sysctl_createv(clog, 0, &node, NULL,
1782 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1783 CTLTYPE_STRUCT, "info",
1784 SYSCTL_DESCR("Information about a CCD unit"),
1785 ccd_info_sysctl, 0, NULL, 0,
1786 CTL_CREATE, CTL_EOL);
1787 sysctl_createv(clog, 0, &node, NULL,
1788 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1789 CTLTYPE_STRUCT, "components",
1790 SYSCTL_DESCR("Information about CCD components"),
1791 ccd_components_sysctl, 0, NULL, 0,
1792 CTL_CREATE, CTL_EOL);
1793 }
1794