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