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