ccd.c revision 1.127.6.4 1 /* $NetBSD: ccd.c,v 1.127.6.4 2009/01/17 13:28:52 mjf Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997, 1998, 1999, 2007 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.
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) 1990, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * This code is derived from software contributed to Berkeley by
37 * the Systems Programming Group of the University of Utah Computer
38 * Science Department.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * from: Utah $Hdr: cd.c 1.6 90/11/28$
65 *
66 * @(#)cd.c 8.2 (Berkeley) 11/16/93
67 */
68
69 /*
70 * Copyright (c) 1988 University of Utah.
71 *
72 * This code is derived from software contributed to Berkeley by
73 * the Systems Programming Group of the University of Utah Computer
74 * Science Department.
75 *
76 * Redistribution and use in source and binary forms, with or without
77 * modification, are permitted provided that the following conditions
78 * are met:
79 * 1. Redistributions of source code must retain the above copyright
80 * notice, this list of conditions and the following disclaimer.
81 * 2. Redistributions in binary form must reproduce the above copyright
82 * notice, this list of conditions and the following disclaimer in the
83 * documentation and/or other materials provided with the distribution.
84 * 3. All advertising materials mentioning features or use of this software
85 * must display the following acknowledgement:
86 * This product includes software developed by the University of
87 * California, Berkeley and its contributors.
88 * 4. Neither the name of the University nor the names of its contributors
89 * may be used to endorse or promote products derived from this software
90 * without specific prior written permission.
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
93 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
95 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
96 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
97 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
98 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
100 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
101 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
102 * SUCH DAMAGE.
103 *
104 * from: Utah $Hdr: cd.c 1.6 90/11/28$
105 *
106 * @(#)cd.c 8.2 (Berkeley) 11/16/93
107 */
108
109 /*
110 * "Concatenated" disk driver.
111 *
112 * Dynamic configuration and disklabel support by:
113 * Jason R. Thorpe <thorpej (at) nas.nasa.gov>
114 * Numerical Aerodynamic Simulation Facility
115 * Mail Stop 258-6
116 * NASA Ames Research Center
117 * Moffett Field, CA 94035
118 */
119
120 #include <sys/cdefs.h>
121 __KERNEL_RCSID(0, "$NetBSD: ccd.c,v 1.127.6.4 2009/01/17 13:28:52 mjf Exp $");
122
123 #include <sys/param.h>
124 #include <sys/systm.h>
125 #include <sys/proc.h>
126 #include <sys/errno.h>
127 #include <sys/buf.h>
128 #include <sys/bufq.h>
129 #include <sys/malloc.h>
130 #include <sys/pool.h>
131 #include <sys/namei.h>
132 #include <sys/stat.h>
133 #include <sys/ioctl.h>
134 #include <sys/disklabel.h>
135 #include <sys/device.h>
136 #include <sys/disk.h>
137 #include <sys/syslog.h>
138 #include <sys/fcntl.h>
139 #include <sys/vnode.h>
140 #include <sys/conf.h>
141 #include <sys/mutex.h>
142 #include <sys/queue.h>
143 #include <sys/kauth.h>
144
145 #include <dev/ccdvar.h>
146 #include <dev/dkvar.h>
147
148 #if defined(CCDDEBUG) && !defined(DEBUG)
149 #define DEBUG
150 #endif
151
152 #ifdef DEBUG
153 #define CCDB_FOLLOW 0x01
154 #define CCDB_INIT 0x02
155 #define CCDB_IO 0x04
156 #define CCDB_LABEL 0x08
157 #define CCDB_VNODE 0x10
158 int ccddebug = 0x00;
159 #endif
160
161 #define ccdunit(x) DISKUNIT(x)
162
163 struct ccdbuf {
164 struct buf cb_buf; /* new I/O buf */
165 struct buf *cb_obp; /* ptr. to original I/O buf */
166 struct ccd_softc *cb_sc; /* pointer to ccd softc */
167 int cb_comp; /* target component */
168 SIMPLEQ_ENTRY(ccdbuf) cb_q; /* fifo of component buffers */
169 };
170
171 /* component buffer pool */
172 static struct pool ccd_cbufpool;
173
174 #define CCD_GETBUF() pool_get(&ccd_cbufpool, PR_NOWAIT)
175 #define CCD_PUTBUF(cbp) pool_put(&ccd_cbufpool, cbp)
176
177 #define CCDLABELDEV(dev) \
178 (MAKEDISKDEV(major((dev)), ccdunit((dev)), RAW_PART))
179
180 /* called by main() at boot time */
181 void ccdattach(int);
182
183 /* called by biodone() at interrupt time */
184 static void ccdiodone(struct buf *);
185
186 static void ccdstart(struct ccd_softc *);
187 static void ccdinterleave(struct ccd_softc *);
188 static void ccdintr(struct ccd_softc *, struct buf *);
189 static int ccdinit(struct ccd_softc *, char **, struct vnode **,
190 struct lwp *);
191 static struct ccdbuf *ccdbuffer(struct ccd_softc *, struct buf *,
192 daddr_t, void *, long);
193 static void ccdgetdefaultlabel(struct ccd_softc *, struct disklabel *);
194 static void ccdgetdisklabel(dev_t);
195 static void ccdmakedisklabel(struct ccd_softc *);
196
197 static dev_type_open(ccdopen);
198 static dev_type_close(ccdclose);
199 static dev_type_read(ccdread);
200 static dev_type_write(ccdwrite);
201 static dev_type_ioctl(ccdioctl);
202 static dev_type_strategy(ccdstrategy);
203 static dev_type_dump(ccddump);
204 static dev_type_size(ccdsize);
205
206 const struct bdevsw ccd_bdevsw = {
207 ccdopen, ccdclose, ccdstrategy, ccdioctl, ccddump, ccdsize, D_DISK
208 };
209
210 const struct cdevsw ccd_cdevsw = {
211 ccdopen, ccdclose, ccdread, ccdwrite, ccdioctl,
212 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
213 };
214
215 #ifdef DEBUG
216 static void printiinfo(struct ccdiinfo *);
217 #endif
218
219 /* Publically visible for the benefit of libkvm and ccdconfig(8). */
220 struct ccd_softc *ccd_softc;
221 const int ccd_softc_elemsize = sizeof(struct ccd_softc);
222 int numccd = 0;
223
224 /*
225 * Called by main() during pseudo-device attachment. All we need
226 * to do is allocate enough space for devices to be configured later.
227 */
228 void
229 ccdattach(int num)
230 {
231 struct ccd_softc *cs;
232 int i, j, cmaj, bmaj;
233
234 if (num <= 0) {
235 #ifdef DIAGNOSTIC
236 panic("ccdattach: count <= 0");
237 #endif
238 return;
239 }
240
241 ccd_softc = (struct ccd_softc *)malloc(num * ccd_softc_elemsize,
242 M_DEVBUF, M_NOWAIT|M_ZERO);
243 if (ccd_softc == NULL) {
244 printf("WARNING: no memory for concatenated disks\n");
245 return;
246 }
247 numccd = num;
248
249 /* Initialize the component buffer pool. */
250 pool_init(&ccd_cbufpool, sizeof(struct ccdbuf), 0,
251 0, 0, "ccdpl", NULL, IPL_BIO);
252
253 bmaj = bdevsw_lookup_major(&ccd_bdevsw);
254 cmaj = cdevsw_lookup_major(&ccd_cdevsw);
255
256 /* Initialize per-softc structures. */
257 for (i = 0; i < num; i++) {
258 cs = &ccd_softc[i];
259 snprintf(cs->sc_xname, sizeof(cs->sc_xname), "ccd%d", i);
260 mutex_init(&cs->sc_lock, MUTEX_DEFAULT, IPL_NONE);
261 disk_init(&cs->sc_dkdev, cs->sc_xname, NULL); /* XXX */
262 for (j = 0; j < MAXPARTITIONS; j++) {
263 device_register_name(MAKEDISKDEV(bmaj, i, j),
264 NULL, false, DEV_DISK, "ccd%d%c", i, 'a' + j);
265 device_register_name(MAKEDISKDEV(cmaj, i, j),
266 NULL, false, DEV_DISK, "rccd%d%c", i, 'a' + j);
267 }
268 }
269
270 }
271
272 static int
273 ccdinit(struct ccd_softc *cs, char **cpaths, struct vnode **vpp,
274 struct lwp *l)
275 {
276 struct ccdcinfo *ci = NULL;
277 size_t size;
278 int ix;
279 struct vattr va;
280 size_t minsize;
281 int maxsecsize;
282 struct partinfo dpart;
283 struct ccdgeom *ccg = &cs->sc_geom;
284 char *tmppath;
285 int error, path_alloced;
286
287 #ifdef DEBUG
288 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
289 printf("%s: ccdinit\n", cs->sc_xname);
290 #endif
291
292 /* Allocate space for the component info. */
293 cs->sc_cinfo = malloc(cs->sc_nccdisks * sizeof(struct ccdcinfo),
294 M_DEVBUF, M_WAITOK);
295
296 tmppath = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
297
298 cs->sc_size = 0;
299
300 /*
301 * Verify that each component piece exists and record
302 * relevant information about it.
303 */
304 maxsecsize = 0;
305 minsize = 0;
306 for (ix = 0, path_alloced = 0; ix < cs->sc_nccdisks; ix++) {
307 ci = &cs->sc_cinfo[ix];
308 ci->ci_vp = vpp[ix];
309
310 /*
311 * Copy in the pathname of the component.
312 */
313 memset(tmppath, 0, sizeof(tmppath)); /* sanity */
314 error = copyinstr(cpaths[ix], tmppath,
315 MAXPATHLEN, &ci->ci_pathlen);
316 if (error) {
317 #ifdef DEBUG
318 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
319 printf("%s: can't copy path, error = %d\n",
320 cs->sc_xname, error);
321 #endif
322 goto out;
323 }
324 ci->ci_path = malloc(ci->ci_pathlen, M_DEVBUF, M_WAITOK);
325 memcpy(ci->ci_path, tmppath, ci->ci_pathlen);
326 path_alloced++;
327
328 /*
329 * XXX: Cache the component's dev_t.
330 */
331 if ((error = VOP_GETATTR(vpp[ix], &va, l->l_cred)) != 0) {
332 #ifdef DEBUG
333 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
334 printf("%s: %s: getattr failed %s = %d\n",
335 cs->sc_xname, ci->ci_path,
336 "error", error);
337 #endif
338 goto out;
339 }
340 ci->ci_dev = va.va_rdev;
341
342 /*
343 * Get partition information for the component.
344 */
345 error = VOP_IOCTL(vpp[ix], DIOCGPART, &dpart,
346 FREAD, l->l_cred);
347 if (error) {
348 #ifdef DEBUG
349 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
350 printf("%s: %s: ioctl failed, error = %d\n",
351 cs->sc_xname, ci->ci_path, error);
352 #endif
353 goto out;
354 }
355
356 /*
357 * This diagnostic test is disabled (for now?) since not all port supports
358 * on-disk BSD disklabel.
359 */
360 #if 0 /* def DIAGNOSTIC */
361 /* Check fstype field of component. */
362 if (dpart.part->p_fstype != FS_CCD)
363 printf("%s: WARNING: %s: fstype %d != FS_CCD\n",
364 cs->sc_xname, ci->ci_path, dpart.part->p_fstype);
365 #endif
366
367 /*
368 * Calculate the size, truncating to an interleave
369 * boundary if necessary.
370 */
371 maxsecsize =
372 ((dpart.disklab->d_secsize > maxsecsize) ?
373 dpart.disklab->d_secsize : maxsecsize);
374 size = dpart.part->p_size;
375 if (cs->sc_ileave > 1)
376 size -= size % cs->sc_ileave;
377
378 if (size == 0) {
379 #ifdef DEBUG
380 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
381 printf("%s: %s: size == 0\n",
382 cs->sc_xname, ci->ci_path);
383 #endif
384 error = ENODEV;
385 goto out;
386 }
387
388 if (minsize == 0 || size < minsize)
389 minsize = size;
390 ci->ci_size = size;
391 cs->sc_size += size;
392 }
393
394 /*
395 * Don't allow the interleave to be smaller than
396 * the biggest component sector.
397 */
398 if ((cs->sc_ileave > 0) &&
399 (cs->sc_ileave < (maxsecsize / DEV_BSIZE))) {
400 #ifdef DEBUG
401 if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
402 printf("%s: interleave must be at least %d\n",
403 cs->sc_xname, (maxsecsize / DEV_BSIZE));
404 #endif
405 error = EINVAL;
406 goto out;
407 }
408
409 /*
410 * If uniform interleave is desired set all sizes to that of
411 * the smallest component.
412 */
413 if (cs->sc_flags & CCDF_UNIFORM) {
414 for (ci = cs->sc_cinfo;
415 ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
416 ci->ci_size = minsize;
417
418 cs->sc_size = cs->sc_nccdisks * minsize;
419 }
420
421 /*
422 * Construct the interleave table.
423 */
424 ccdinterleave(cs);
425
426 /*
427 * Create pseudo-geometry based on 1MB cylinders. It's
428 * pretty close.
429 */
430 ccg->ccg_secsize = DEV_BSIZE;
431 ccg->ccg_ntracks = 1;
432 ccg->ccg_nsectors = 1024 * (1024 / ccg->ccg_secsize);
433 ccg->ccg_ncylinders = cs->sc_size / ccg->ccg_nsectors;
434
435 cs->sc_flags |= CCDF_INITED;
436
437 free(tmppath, M_TEMP);
438
439 return (0);
440
441 out:
442 for (ix = 0; ix < path_alloced; ix++)
443 free(cs->sc_cinfo[ix].ci_path, M_DEVBUF);
444 free(cs->sc_cinfo, M_DEVBUF);
445 free(tmppath, M_TEMP);
446 return (error);
447 }
448
449 static void
450 ccdinterleave(struct ccd_softc *cs)
451 {
452 struct ccdcinfo *ci, *smallci;
453 struct ccdiinfo *ii;
454 daddr_t bn, lbn;
455 int ix;
456 u_long size;
457
458 #ifdef DEBUG
459 if (ccddebug & CCDB_INIT)
460 printf("ccdinterleave(%p): ileave %d\n", cs, cs->sc_ileave);
461 #endif
462 /*
463 * Allocate an interleave table.
464 * Chances are this is too big, but we don't care.
465 */
466 size = (cs->sc_nccdisks + 1) * sizeof(struct ccdiinfo);
467 cs->sc_itable = (struct ccdiinfo *)malloc(size, M_DEVBUF,
468 M_WAITOK|M_ZERO);
469
470 /*
471 * Trivial case: no interleave (actually interleave of disk size).
472 * Each table entry represents a single component in its entirety.
473 */
474 if (cs->sc_ileave == 0) {
475 bn = 0;
476 ii = cs->sc_itable;
477
478 for (ix = 0; ix < cs->sc_nccdisks; ix++) {
479 /* Allocate space for ii_index. */
480 ii->ii_index = malloc(sizeof(int), M_DEVBUF, M_WAITOK);
481 ii->ii_ndisk = 1;
482 ii->ii_startblk = bn;
483 ii->ii_startoff = 0;
484 ii->ii_index[0] = ix;
485 bn += cs->sc_cinfo[ix].ci_size;
486 ii++;
487 }
488 ii->ii_ndisk = 0;
489 #ifdef DEBUG
490 if (ccddebug & CCDB_INIT)
491 printiinfo(cs->sc_itable);
492 #endif
493 return;
494 }
495
496 /*
497 * The following isn't fast or pretty; it doesn't have to be.
498 */
499 size = 0;
500 bn = lbn = 0;
501 for (ii = cs->sc_itable; ; ii++) {
502 /* Allocate space for ii_index. */
503 ii->ii_index = malloc((sizeof(int) * cs->sc_nccdisks),
504 M_DEVBUF, M_WAITOK);
505
506 /*
507 * Locate the smallest of the remaining components
508 */
509 smallci = NULL;
510 for (ci = cs->sc_cinfo;
511 ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
512 if (ci->ci_size > size &&
513 (smallci == NULL ||
514 ci->ci_size < smallci->ci_size))
515 smallci = ci;
516
517 /*
518 * Nobody left, all done
519 */
520 if (smallci == NULL) {
521 ii->ii_ndisk = 0;
522 break;
523 }
524
525 /*
526 * Record starting logical block and component offset
527 */
528 ii->ii_startblk = bn / cs->sc_ileave;
529 ii->ii_startoff = lbn;
530
531 /*
532 * Determine how many disks take part in this interleave
533 * and record their indices.
534 */
535 ix = 0;
536 for (ci = cs->sc_cinfo;
537 ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
538 if (ci->ci_size >= smallci->ci_size)
539 ii->ii_index[ix++] = ci - cs->sc_cinfo;
540 ii->ii_ndisk = ix;
541 bn += ix * (smallci->ci_size - size);
542 lbn = smallci->ci_size / cs->sc_ileave;
543 size = smallci->ci_size;
544 }
545 #ifdef DEBUG
546 if (ccddebug & CCDB_INIT)
547 printiinfo(cs->sc_itable);
548 #endif
549 }
550
551 /* ARGSUSED */
552 static int
553 ccdopen(dev_t dev, int flags, int fmt, struct lwp *l)
554 {
555 int unit = ccdunit(dev);
556 struct ccd_softc *cs;
557 struct disklabel *lp;
558 int error = 0, part, pmask;
559
560 #ifdef DEBUG
561 if (ccddebug & CCDB_FOLLOW)
562 printf("ccdopen(0x%"PRIx64", 0x%x)\n", dev, flags);
563 #endif
564 if (unit >= numccd)
565 return (ENXIO);
566 cs = &ccd_softc[unit];
567
568 mutex_enter(&cs->sc_lock);
569
570 lp = cs->sc_dkdev.dk_label;
571
572 part = DISKPART(dev);
573 pmask = (1 << part);
574
575 /*
576 * If we're initialized, check to see if there are any other
577 * open partitions. If not, then it's safe to update
578 * the in-core disklabel. Only read the disklabel if it is
579 * not already valid.
580 */
581 if ((cs->sc_flags & (CCDF_INITED|CCDF_VLABEL)) == CCDF_INITED &&
582 cs->sc_dkdev.dk_openmask == 0)
583 ccdgetdisklabel(dev);
584
585 /* Check that the partition exists. */
586 if (part != RAW_PART) {
587 if (((cs->sc_flags & CCDF_INITED) == 0) ||
588 ((part >= lp->d_npartitions) ||
589 (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
590 error = ENXIO;
591 goto done;
592 }
593 }
594
595 /* Prevent our unit from being unconfigured while open. */
596 switch (fmt) {
597 case S_IFCHR:
598 cs->sc_dkdev.dk_copenmask |= pmask;
599 break;
600
601 case S_IFBLK:
602 cs->sc_dkdev.dk_bopenmask |= pmask;
603 break;
604 }
605 cs->sc_dkdev.dk_openmask =
606 cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
607
608 done:
609 mutex_exit(&cs->sc_lock);
610 return (error);
611 }
612
613 /* ARGSUSED */
614 static int
615 ccdclose(dev_t dev, int flags, int fmt, struct lwp *l)
616 {
617 int unit = ccdunit(dev);
618 struct ccd_softc *cs;
619 int part;
620
621 #ifdef DEBUG
622 if (ccddebug & CCDB_FOLLOW)
623 printf("ccdclose(0x%"PRIx64", 0x%x)\n", dev, flags);
624 #endif
625
626 if (unit >= numccd)
627 return (ENXIO);
628 cs = &ccd_softc[unit];
629
630 mutex_enter(&cs->sc_lock);
631
632 part = DISKPART(dev);
633
634 /* ...that much closer to allowing unconfiguration... */
635 switch (fmt) {
636 case S_IFCHR:
637 cs->sc_dkdev.dk_copenmask &= ~(1 << part);
638 break;
639
640 case S_IFBLK:
641 cs->sc_dkdev.dk_bopenmask &= ~(1 << part);
642 break;
643 }
644 cs->sc_dkdev.dk_openmask =
645 cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
646
647 if (cs->sc_dkdev.dk_openmask == 0) {
648 if ((cs->sc_flags & CCDF_KLABEL) == 0)
649 cs->sc_flags &= ~CCDF_VLABEL;
650 }
651
652 mutex_exit(&cs->sc_lock);
653 return (0);
654 }
655
656 static void
657 ccdstrategy(struct buf *bp)
658 {
659 int unit = ccdunit(bp->b_dev);
660 struct ccd_softc *cs = &ccd_softc[unit];
661 daddr_t blkno;
662 int s;
663 int wlabel;
664 struct disklabel *lp;
665
666 #ifdef DEBUG
667 if (ccddebug & CCDB_FOLLOW)
668 printf("ccdstrategy(%p): unit %d\n", bp, unit);
669 #endif
670 if ((cs->sc_flags & CCDF_INITED) == 0) {
671 #ifdef DEBUG
672 if (ccddebug & CCDB_FOLLOW)
673 printf("ccdstrategy: unit %d: not inited\n", unit);
674 #endif
675 bp->b_error = ENXIO;
676 goto done;
677 }
678
679 /* If it's a nil transfer, wake up the top half now. */
680 if (bp->b_bcount == 0)
681 goto done;
682
683 lp = cs->sc_dkdev.dk_label;
684
685 /*
686 * Do bounds checking and adjust transfer. If there's an
687 * error, the bounds check will flag that for us. Convert
688 * the partition relative block number to an absolute.
689 */
690 blkno = bp->b_blkno;
691 wlabel = cs->sc_flags & (CCDF_WLABEL|CCDF_LABELLING);
692 if (DISKPART(bp->b_dev) != RAW_PART) {
693 if (bounds_check_with_label(&cs->sc_dkdev, bp, wlabel) <= 0)
694 goto done;
695 blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
696 }
697 bp->b_rawblkno = blkno;
698
699 /* Place it in the queue and start I/O on the unit. */
700 s = splbio();
701 bufq_put(cs->sc_bufq, bp);
702 ccdstart(cs);
703 splx(s);
704 return;
705
706 done:
707 bp->b_resid = bp->b_bcount;
708 biodone(bp);
709 }
710
711 static void
712 ccdstart(struct ccd_softc *cs)
713 {
714 long bcount, rcount;
715 struct buf *bp;
716 struct ccdbuf *cbp;
717 char *addr;
718 daddr_t bn;
719 SIMPLEQ_HEAD(, ccdbuf) cbufq;
720
721 #ifdef DEBUG
722 if (ccddebug & CCDB_FOLLOW)
723 printf("ccdstart(%p)\n", cs);
724 #endif
725
726 /* See if there is work for us to do. */
727 while ((bp = bufq_peek(cs->sc_bufq)) != NULL) {
728 /* Instrumentation. */
729 disk_busy(&cs->sc_dkdev);
730
731 bp->b_resid = bp->b_bcount;
732 bn = bp->b_rawblkno;
733
734 /* Allocate the component buffers. */
735 SIMPLEQ_INIT(&cbufq);
736 addr = bp->b_data;
737 for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
738 cbp = ccdbuffer(cs, bp, bn, addr, bcount);
739 if (cbp == NULL) {
740 /*
741 * Can't allocate a component buffer; just
742 * defer the job until later.
743 *
744 * XXX We might consider a watchdog timer
745 * XXX to make sure we are kicked into action,
746 * XXX or consider a low-water mark for our
747 * XXX component buffer pool.
748 */
749 while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
750 SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
751 CCD_PUTBUF(cbp);
752 }
753 disk_unbusy(&cs->sc_dkdev, 0, 0);
754 return;
755 }
756 SIMPLEQ_INSERT_TAIL(&cbufq, cbp, cb_q);
757 rcount = cbp->cb_buf.b_bcount;
758 bn += btodb(rcount);
759 addr += rcount;
760 }
761
762 /* Transfer all set up, remove job from the queue. */
763 (void) bufq_get(cs->sc_bufq);
764
765 /* Now fire off the requests. */
766 while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
767 SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
768 if ((cbp->cb_buf.b_flags & B_READ) == 0)
769 cbp->cb_buf.b_vp->v_numoutput++;
770 bdev_strategy(&cbp->cb_buf);
771 }
772 }
773 }
774
775 /*
776 * Build a component buffer header.
777 */
778 static struct ccdbuf *
779 ccdbuffer(struct ccd_softc *cs, struct buf *bp, daddr_t bn, void *addr,
780 long bcount)
781 {
782 struct ccdcinfo *ci;
783 struct ccdbuf *cbp;
784 daddr_t cbn, cboff;
785 u_int64_t cbc;
786 int ccdisk;
787
788 #ifdef DEBUG
789 if (ccddebug & CCDB_IO)
790 printf("ccdbuffer(%p, %p, %" PRId64 ", %p, %ld)\n",
791 cs, bp, bn, addr, bcount);
792 #endif
793 /*
794 * Determine which component bn falls in.
795 */
796 cbn = bn;
797 cboff = 0;
798
799 /*
800 * Serially concatenated
801 */
802 if (cs->sc_ileave == 0) {
803 daddr_t sblk;
804
805 sblk = 0;
806 for (ccdisk = 0, ci = &cs->sc_cinfo[ccdisk];
807 cbn >= sblk + ci->ci_size;
808 ccdisk++, ci = &cs->sc_cinfo[ccdisk])
809 sblk += ci->ci_size;
810 cbn -= sblk;
811 }
812 /*
813 * Interleaved
814 */
815 else {
816 struct ccdiinfo *ii;
817 int off;
818
819 cboff = cbn % cs->sc_ileave;
820 cbn /= cs->sc_ileave;
821 for (ii = cs->sc_itable; ii->ii_ndisk; ii++)
822 if (ii->ii_startblk > cbn)
823 break;
824 ii--;
825 off = cbn - ii->ii_startblk;
826 if (ii->ii_ndisk == 1) {
827 ccdisk = ii->ii_index[0];
828 cbn = ii->ii_startoff + off;
829 } else {
830 ccdisk = ii->ii_index[off % ii->ii_ndisk];
831 cbn = ii->ii_startoff + off / ii->ii_ndisk;
832 }
833 cbn *= cs->sc_ileave;
834 ci = &cs->sc_cinfo[ccdisk];
835 }
836
837 /*
838 * Fill in the component buf structure.
839 */
840 cbp = CCD_GETBUF();
841 if (cbp == NULL)
842 return (NULL);
843 buf_init(&cbp->cb_buf);
844 cbp->cb_buf.b_flags = bp->b_flags;
845 cbp->cb_buf.b_oflags = bp->b_oflags;
846 cbp->cb_buf.b_cflags = bp->b_cflags;
847 cbp->cb_buf.b_iodone = ccdiodone;
848 cbp->cb_buf.b_proc = bp->b_proc;
849 cbp->cb_buf.b_dev = ci->ci_dev;
850 cbp->cb_buf.b_blkno = cbn + cboff;
851 cbp->cb_buf.b_data = addr;
852 cbp->cb_buf.b_vp = ci->ci_vp;
853 cbp->cb_buf.b_objlock = &ci->ci_vp->v_interlock;
854 if (cs->sc_ileave == 0)
855 cbc = dbtob((u_int64_t)(ci->ci_size - cbn));
856 else
857 cbc = dbtob((u_int64_t)(cs->sc_ileave - cboff));
858 cbp->cb_buf.b_bcount = cbc < bcount ? cbc : bcount;
859
860 /*
861 * context for ccdiodone
862 */
863 cbp->cb_obp = bp;
864 cbp->cb_sc = cs;
865 cbp->cb_comp = ccdisk;
866
867 BIO_COPYPRIO(&cbp->cb_buf, bp);
868
869 #ifdef DEBUG
870 if (ccddebug & CCDB_IO)
871 printf(" dev 0x%"PRIx64"(u%lu): cbp %p bn %" PRId64 " addr %p"
872 " bcnt %d\n",
873 ci->ci_dev, (unsigned long) (ci-cs->sc_cinfo), cbp,
874 cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
875 cbp->cb_buf.b_bcount);
876 #endif
877
878 return (cbp);
879 }
880
881 static void
882 ccdintr(struct ccd_softc *cs, struct buf *bp)
883 {
884
885 #ifdef DEBUG
886 if (ccddebug & CCDB_FOLLOW)
887 printf("ccdintr(%p, %p)\n", cs, bp);
888 #endif
889 /*
890 * Request is done for better or worse, wakeup the top half.
891 */
892 if (bp->b_error != 0)
893 bp->b_resid = bp->b_bcount;
894 disk_unbusy(&cs->sc_dkdev, (bp->b_bcount - bp->b_resid),
895 (bp->b_flags & B_READ));
896 biodone(bp);
897 }
898
899 /*
900 * Called at interrupt time.
901 * Mark the component as done and if all components are done,
902 * take a ccd interrupt.
903 */
904 static void
905 ccdiodone(struct buf *vbp)
906 {
907 struct ccdbuf *cbp = (struct ccdbuf *) vbp;
908 struct buf *bp = cbp->cb_obp;
909 struct ccd_softc *cs = cbp->cb_sc;
910 int count, s;
911
912 s = splbio();
913 #ifdef DEBUG
914 if (ccddebug & CCDB_FOLLOW)
915 printf("ccdiodone(%p)\n", cbp);
916 if (ccddebug & CCDB_IO) {
917 printf("ccdiodone: bp %p bcount %d resid %d\n",
918 bp, bp->b_bcount, bp->b_resid);
919 printf(" dev 0x%"PRIx64"(u%d), cbp %p bn %" PRId64 " addr %p"
920 " bcnt %d\n",
921 cbp->cb_buf.b_dev, cbp->cb_comp, cbp,
922 cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
923 cbp->cb_buf.b_bcount);
924 }
925 #endif
926
927 if (cbp->cb_buf.b_error != 0) {
928 bp->b_error = cbp->cb_buf.b_error;
929 printf("%s: error %d on component %d\n",
930 cs->sc_xname, bp->b_error, cbp->cb_comp);
931 }
932 count = cbp->cb_buf.b_bcount;
933 buf_destroy(&cbp->cb_buf);
934 CCD_PUTBUF(cbp);
935
936 /*
937 * If all done, "interrupt".
938 */
939 bp->b_resid -= count;
940 if (bp->b_resid < 0)
941 panic("ccdiodone: count");
942 if (bp->b_resid == 0)
943 ccdintr(cs, bp);
944 splx(s);
945 }
946
947 /* ARGSUSED */
948 static int
949 ccdread(dev_t dev, struct uio *uio, int flags)
950 {
951 int unit = ccdunit(dev);
952 struct ccd_softc *cs;
953
954 #ifdef DEBUG
955 if (ccddebug & CCDB_FOLLOW)
956 printf("ccdread(0x%"PRIx64", %p)\n", dev, uio);
957 #endif
958 if (unit >= numccd)
959 return (ENXIO);
960 cs = &ccd_softc[unit];
961
962 if ((cs->sc_flags & CCDF_INITED) == 0)
963 return (ENXIO);
964
965 return (physio(ccdstrategy, NULL, dev, B_READ, minphys, uio));
966 }
967
968 /* ARGSUSED */
969 static int
970 ccdwrite(dev_t dev, struct uio *uio, int flags)
971 {
972 int unit = ccdunit(dev);
973 struct ccd_softc *cs;
974
975 #ifdef DEBUG
976 if (ccddebug & CCDB_FOLLOW)
977 printf("ccdwrite(0x%"PRIx64", %p)\n", dev, uio);
978 #endif
979 if (unit >= numccd)
980 return (ENXIO);
981 cs = &ccd_softc[unit];
982
983 if ((cs->sc_flags & CCDF_INITED) == 0)
984 return (ENXIO);
985
986 return (physio(ccdstrategy, NULL, dev, B_WRITE, minphys, uio));
987 }
988
989 static int
990 ccdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
991 {
992 int unit = ccdunit(dev);
993 int s, i, j, lookedup = 0, error = 0;
994 int part, pmask;
995 struct ccd_softc *cs;
996 struct ccd_ioctl *ccio = (struct ccd_ioctl *)data;
997 kauth_cred_t uc;
998 char **cpp;
999 struct vnode **vpp;
1000 #ifdef __HAVE_OLD_DISKLABEL
1001 struct disklabel newlabel;
1002 #endif
1003
1004 if (unit >= numccd)
1005 return (ENXIO);
1006 cs = &ccd_softc[unit];
1007
1008 uc = (l != NULL) ? l->l_cred : NOCRED;
1009
1010 /* Must be open for writes for these commands... */
1011 switch (cmd) {
1012 case CCDIOCSET:
1013 case CCDIOCCLR:
1014 case DIOCSDINFO:
1015 case DIOCWDINFO:
1016 #ifdef __HAVE_OLD_DISKLABEL
1017 case ODIOCSDINFO:
1018 case ODIOCWDINFO:
1019 #endif
1020 case DIOCKLABEL:
1021 case DIOCWLABEL:
1022 if ((flag & FWRITE) == 0)
1023 return (EBADF);
1024 }
1025
1026 mutex_enter(&cs->sc_lock);
1027
1028 /* Must be initialized for these... */
1029 switch (cmd) {
1030 case CCDIOCCLR:
1031 case DIOCGDINFO:
1032 case DIOCCACHESYNC:
1033 case DIOCSDINFO:
1034 case DIOCWDINFO:
1035 case DIOCGPART:
1036 case DIOCWLABEL:
1037 case DIOCKLABEL:
1038 case DIOCGDEFLABEL:
1039 #ifdef __HAVE_OLD_DISKLABEL
1040 case ODIOCGDINFO:
1041 case ODIOCSDINFO:
1042 case ODIOCWDINFO:
1043 case ODIOCGDEFLABEL:
1044 #endif
1045 if ((cs->sc_flags & CCDF_INITED) == 0) {
1046 error = ENXIO;
1047 goto out;
1048 }
1049 }
1050
1051 switch (cmd) {
1052 case CCDIOCSET:
1053 if (cs->sc_flags & CCDF_INITED) {
1054 error = EBUSY;
1055 goto out;
1056 }
1057
1058 /* Validate the flags. */
1059 if ((ccio->ccio_flags & CCDF_USERMASK) != ccio->ccio_flags) {
1060 error = EINVAL;
1061 goto out;
1062 }
1063
1064 if (ccio->ccio_ndisks > CCD_MAXNDISKS) {
1065 error = EINVAL;
1066 goto out;
1067 }
1068
1069 /* Fill in some important bits. */
1070 cs->sc_ileave = ccio->ccio_ileave;
1071 cs->sc_nccdisks = ccio->ccio_ndisks;
1072 cs->sc_flags = ccio->ccio_flags & CCDF_USERMASK;
1073
1074 /*
1075 * Allocate space for and copy in the array of
1076 * componet pathnames and device numbers.
1077 */
1078 cpp = malloc(ccio->ccio_ndisks * sizeof(char *),
1079 M_DEVBUF, M_WAITOK);
1080 vpp = malloc(ccio->ccio_ndisks * sizeof(struct vnode *),
1081 M_DEVBUF, M_WAITOK);
1082
1083 error = copyin(ccio->ccio_disks, cpp,
1084 ccio->ccio_ndisks * sizeof(char **));
1085 if (error) {
1086 free(vpp, M_DEVBUF);
1087 free(cpp, M_DEVBUF);
1088 goto out;
1089 }
1090
1091 #ifdef DEBUG
1092 if (ccddebug & CCDB_INIT)
1093 for (i = 0; i < ccio->ccio_ndisks; ++i)
1094 printf("ccdioctl: component %d: %p\n",
1095 i, cpp[i]);
1096 #endif
1097
1098 for (i = 0; i < ccio->ccio_ndisks; ++i) {
1099 #ifdef DEBUG
1100 if (ccddebug & CCDB_INIT)
1101 printf("ccdioctl: lookedup = %d\n", lookedup);
1102 #endif
1103 if ((error = dk_lookup(cpp[i], l, &vpp[i],
1104 UIO_USERSPACE)) != 0) {
1105 for (j = 0; j < lookedup; ++j)
1106 (void)vn_close(vpp[j], FREAD|FWRITE,
1107 uc);
1108 free(vpp, M_DEVBUF);
1109 free(cpp, M_DEVBUF);
1110 goto out;
1111 }
1112 ++lookedup;
1113 }
1114
1115 /*
1116 * Initialize the ccd. Fills in the softc for us.
1117 */
1118 if ((error = ccdinit(cs, cpp, vpp, l)) != 0) {
1119 for (j = 0; j < lookedup; ++j)
1120 (void)vn_close(vpp[j], FREAD|FWRITE,
1121 uc);
1122 free(vpp, M_DEVBUF);
1123 free(cpp, M_DEVBUF);
1124 goto out;
1125 }
1126
1127 /* We can free the temporary variables now. */
1128 free(vpp, M_DEVBUF);
1129 free(cpp, M_DEVBUF);
1130
1131 /*
1132 * The ccd has been successfully initialized, so
1133 * we can place it into the array. Don't try to
1134 * read the disklabel until the disk has been attached,
1135 * because space for the disklabel is allocated
1136 * in disk_attach();
1137 */
1138 ccio->ccio_unit = unit;
1139 ccio->ccio_size = cs->sc_size;
1140
1141 bufq_alloc(&cs->sc_bufq, "fcfs", 0);
1142
1143 /* Attach the disk. */
1144 disk_attach(&cs->sc_dkdev);
1145
1146 /* Try and read the disklabel. */
1147 ccdgetdisklabel(dev);
1148 break;
1149
1150 case CCDIOCCLR:
1151 /*
1152 * Don't unconfigure if any other partitions are open
1153 * or if both the character and block flavors of this
1154 * partition are open.
1155 */
1156 part = DISKPART(dev);
1157 pmask = (1 << part);
1158 if ((cs->sc_dkdev.dk_openmask & ~pmask) ||
1159 ((cs->sc_dkdev.dk_bopenmask & pmask) &&
1160 (cs->sc_dkdev.dk_copenmask & pmask))) {
1161 error = EBUSY;
1162 goto out;
1163 }
1164
1165 /* Kill off any queued buffers. */
1166 s = splbio();
1167 bufq_drain(cs->sc_bufq);
1168 splx(s);
1169
1170 bufq_free(cs->sc_bufq);
1171
1172 /*
1173 * Free ccd_softc information and clear entry.
1174 */
1175
1176 /* Close the components and free their pathnames. */
1177 for (i = 0; i < cs->sc_nccdisks; ++i) {
1178 /*
1179 * XXX: this close could potentially fail and
1180 * cause Bad Things. Maybe we need to force
1181 * the close to happen?
1182 */
1183 #ifdef DEBUG
1184 if (ccddebug & CCDB_VNODE)
1185 vprint("CCDIOCCLR: vnode info",
1186 cs->sc_cinfo[i].ci_vp);
1187 #endif
1188 (void)vn_close(cs->sc_cinfo[i].ci_vp, FREAD|FWRITE,
1189 uc);
1190 free(cs->sc_cinfo[i].ci_path, M_DEVBUF);
1191 }
1192
1193 /* Free interleave index. */
1194 for (i = 0; cs->sc_itable[i].ii_ndisk; ++i)
1195 free(cs->sc_itable[i].ii_index, M_DEVBUF);
1196
1197 /* Free component info and interleave table. */
1198 free(cs->sc_cinfo, M_DEVBUF);
1199 free(cs->sc_itable, M_DEVBUF);
1200 cs->sc_flags &= ~(CCDF_INITED|CCDF_VLABEL);
1201
1202 /* Detatch the disk. */
1203 disk_detach(&cs->sc_dkdev);
1204 break;
1205
1206 case DIOCGDINFO:
1207 *(struct disklabel *)data = *(cs->sc_dkdev.dk_label);
1208 break;
1209 #ifdef __HAVE_OLD_DISKLABEL
1210 case ODIOCGDINFO:
1211 newlabel = *(cs->sc_dkdev.dk_label);
1212 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1213 return ENOTTY;
1214 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1215 break;
1216 #endif
1217
1218 case DIOCGPART:
1219 ((struct partinfo *)data)->disklab = cs->sc_dkdev.dk_label;
1220 ((struct partinfo *)data)->part =
1221 &cs->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
1222 break;
1223
1224 case DIOCCACHESYNC:
1225 /*
1226 * XXX Do we really need to care about having a writable
1227 * file descriptor here?
1228 */
1229 if ((flag & FWRITE) == 0)
1230 return (EBADF);
1231
1232 /*
1233 * We pass this call down to all components and report
1234 * the first error we encounter.
1235 */
1236 for (error = 0, i = 0; i < cs->sc_nccdisks; i++) {
1237 j = VOP_IOCTL(cs->sc_cinfo[i].ci_vp, cmd, data,
1238 flag, uc);
1239 if (j != 0 && error == 0)
1240 error = j;
1241 }
1242 break;
1243
1244 case DIOCWDINFO:
1245 case DIOCSDINFO:
1246 #ifdef __HAVE_OLD_DISKLABEL
1247 case ODIOCWDINFO:
1248 case ODIOCSDINFO:
1249 #endif
1250 {
1251 struct disklabel *lp;
1252 #ifdef __HAVE_OLD_DISKLABEL
1253 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1254 memset(&newlabel, 0, sizeof newlabel);
1255 memcpy(&newlabel, data, sizeof (struct olddisklabel));
1256 lp = &newlabel;
1257 } else
1258 #endif
1259 lp = (struct disklabel *)data;
1260
1261 cs->sc_flags |= CCDF_LABELLING;
1262
1263 error = setdisklabel(cs->sc_dkdev.dk_label,
1264 lp, 0, cs->sc_dkdev.dk_cpulabel);
1265 if (error == 0) {
1266 if (cmd == DIOCWDINFO
1267 #ifdef __HAVE_OLD_DISKLABEL
1268 || cmd == ODIOCWDINFO
1269 #endif
1270 )
1271 error = writedisklabel(CCDLABELDEV(dev),
1272 ccdstrategy, cs->sc_dkdev.dk_label,
1273 cs->sc_dkdev.dk_cpulabel);
1274 }
1275
1276 cs->sc_flags &= ~CCDF_LABELLING;
1277 break;
1278 }
1279
1280 case DIOCKLABEL:
1281 if (*(int *)data != 0)
1282 cs->sc_flags |= CCDF_KLABEL;
1283 else
1284 cs->sc_flags &= ~CCDF_KLABEL;
1285 break;
1286
1287 case DIOCWLABEL:
1288 if (*(int *)data != 0)
1289 cs->sc_flags |= CCDF_WLABEL;
1290 else
1291 cs->sc_flags &= ~CCDF_WLABEL;
1292 break;
1293
1294 case DIOCGDEFLABEL:
1295 ccdgetdefaultlabel(cs, (struct disklabel *)data);
1296 break;
1297
1298 #ifdef __HAVE_OLD_DISKLABEL
1299 case ODIOCGDEFLABEL:
1300 ccdgetdefaultlabel(cs, &newlabel);
1301 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1302 return ENOTTY;
1303 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1304 break;
1305 #endif
1306
1307 default:
1308 error = ENOTTY;
1309 }
1310
1311 out:
1312 mutex_exit(&cs->sc_lock);
1313 return (error);
1314 }
1315
1316 static int
1317 ccdsize(dev_t dev)
1318 {
1319 struct ccd_softc *cs;
1320 struct disklabel *lp;
1321 int part, unit, omask, size;
1322
1323 unit = ccdunit(dev);
1324 if (unit >= numccd)
1325 return (-1);
1326 cs = &ccd_softc[unit];
1327
1328 if ((cs->sc_flags & CCDF_INITED) == 0)
1329 return (-1);
1330
1331 part = DISKPART(dev);
1332 omask = cs->sc_dkdev.dk_openmask & (1 << part);
1333 lp = cs->sc_dkdev.dk_label;
1334
1335 if (omask == 0 && ccdopen(dev, 0, S_IFBLK, curlwp))
1336 return (-1);
1337
1338 if (lp->d_partitions[part].p_fstype != FS_SWAP)
1339 size = -1;
1340 else
1341 size = lp->d_partitions[part].p_size *
1342 (lp->d_secsize / DEV_BSIZE);
1343
1344 if (omask == 0 && ccdclose(dev, 0, S_IFBLK, curlwp))
1345 return (-1);
1346
1347 return (size);
1348 }
1349
1350 static int
1351 ccddump(dev_t dev, daddr_t blkno, void *va,
1352 size_t size)
1353 {
1354
1355 /* Not implemented. */
1356 return ENXIO;
1357 }
1358
1359 static void
1360 ccdgetdefaultlabel(struct ccd_softc *cs, struct disklabel *lp)
1361 {
1362 struct ccdgeom *ccg = &cs->sc_geom;
1363
1364 memset(lp, 0, sizeof(*lp));
1365
1366 lp->d_secperunit = cs->sc_size;
1367 lp->d_secsize = ccg->ccg_secsize;
1368 lp->d_nsectors = ccg->ccg_nsectors;
1369 lp->d_ntracks = ccg->ccg_ntracks;
1370 lp->d_ncylinders = ccg->ccg_ncylinders;
1371 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1372
1373 strncpy(lp->d_typename, "ccd", sizeof(lp->d_typename));
1374 lp->d_type = DTYPE_CCD;
1375 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1376 lp->d_rpm = 3600;
1377 lp->d_interleave = 1;
1378 lp->d_flags = 0;
1379
1380 lp->d_partitions[RAW_PART].p_offset = 0;
1381 lp->d_partitions[RAW_PART].p_size = cs->sc_size;
1382 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1383 lp->d_npartitions = RAW_PART + 1;
1384
1385 lp->d_magic = DISKMAGIC;
1386 lp->d_magic2 = DISKMAGIC;
1387 lp->d_checksum = dkcksum(cs->sc_dkdev.dk_label);
1388 }
1389
1390 /*
1391 * Read the disklabel from the ccd. If one is not present, fake one
1392 * up.
1393 */
1394 static void
1395 ccdgetdisklabel(dev_t dev)
1396 {
1397 int unit = ccdunit(dev);
1398 struct ccd_softc *cs = &ccd_softc[unit];
1399 const char *errstring;
1400 struct disklabel *lp = cs->sc_dkdev.dk_label;
1401 struct cpu_disklabel *clp = cs->sc_dkdev.dk_cpulabel;
1402
1403 memset(clp, 0, sizeof(*clp));
1404
1405 ccdgetdefaultlabel(cs, lp);
1406
1407 /*
1408 * Call the generic disklabel extraction routine.
1409 */
1410 if ((cs->sc_flags & CCDF_NOLABEL) != 0)
1411 errstring = "CCDF_NOLABEL set; ignoring on-disk label";
1412 else
1413 errstring = readdisklabel(CCDLABELDEV(dev), ccdstrategy,
1414 cs->sc_dkdev.dk_label, cs->sc_dkdev.dk_cpulabel);
1415 if (errstring)
1416 ccdmakedisklabel(cs);
1417 else {
1418 int i;
1419 struct partition *pp;
1420
1421 /*
1422 * Sanity check whether the found disklabel is valid.
1423 *
1424 * This is necessary since total size of ccd may vary
1425 * when an interleave is changed even though exactly
1426 * same componets are used, and old disklabel may used
1427 * if that is found.
1428 */
1429 if (lp->d_secperunit != cs->sc_size)
1430 printf("WARNING: %s: "
1431 "total sector size in disklabel (%d) != "
1432 "the size of ccd (%lu)\n", cs->sc_xname,
1433 lp->d_secperunit, (u_long)cs->sc_size);
1434 for (i = 0; i < lp->d_npartitions; i++) {
1435 pp = &lp->d_partitions[i];
1436 if (pp->p_offset + pp->p_size > cs->sc_size)
1437 printf("WARNING: %s: end of partition `%c' "
1438 "exceeds the size of ccd (%lu)\n",
1439 cs->sc_xname, 'a' + i, (u_long)cs->sc_size);
1440 }
1441 }
1442
1443 #ifdef DEBUG
1444 /* It's actually extremely common to have unlabeled ccds. */
1445 if (ccddebug & CCDB_LABEL)
1446 if (errstring != NULL)
1447 printf("%s: %s\n", cs->sc_xname, errstring);
1448 #endif
1449
1450 /* In-core label now valid. */
1451 cs->sc_flags |= CCDF_VLABEL;
1452 }
1453
1454 /*
1455 * Take care of things one might want to take care of in the event
1456 * that a disklabel isn't present.
1457 */
1458 static void
1459 ccdmakedisklabel(struct ccd_softc *cs)
1460 {
1461 struct disklabel *lp = cs->sc_dkdev.dk_label;
1462
1463 /*
1464 * For historical reasons, if there's no disklabel present
1465 * the raw partition must be marked FS_BSDFFS.
1466 */
1467 lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
1468
1469 strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
1470
1471 lp->d_checksum = dkcksum(lp);
1472 }
1473
1474 #ifdef DEBUG
1475 static void
1476 printiinfo(struct ccdiinfo *ii)
1477 {
1478 int ix, i;
1479
1480 for (ix = 0; ii->ii_ndisk; ix++, ii++) {
1481 printf(" itab[%d]: #dk %d sblk %" PRId64 " soff %" PRId64,
1482 ix, ii->ii_ndisk, ii->ii_startblk, ii->ii_startoff);
1483 for (i = 0; i < ii->ii_ndisk; i++)
1484 printf(" %d", ii->ii_index[i]);
1485 printf("\n");
1486 }
1487 }
1488 #endif
1489