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