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