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