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