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