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