1 /* $NetBSD: md.c,v 1.37 2004/04/21 18:40:37 itojun Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Gordon W. Ross, Leo Weppelman. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 4. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by 20 * Gordon W. Ross and Leo Weppelman. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * This implements a general-purpose memory-disk. 36 * See md.h for notes on the config types. 37 * 38 * Note that this driver provides the same functionality 39 * as the MFS filesystem hack, but this is better because 40 * you can use this for any filesystem type you'd like! 41 * 42 * Credit for most of the kmem ramdisk code goes to: 43 * Leo Weppelman (atari) and Phil Nelson (pc532) 44 * Credit for the ideas behind the "user space memory" code goes 45 * to the authors of the MFS implementation. 46 */ 47 48 #include <sys/cdefs.h> 49 __KERNEL_RCSID(0, "$NetBSD: md.c,v 1.37 2004/04/21 18:40:37 itojun Exp $"); 50 51 #include "opt_md.h" 52 53 #include <sys/param.h> 54 #include <sys/kernel.h> 55 #include <sys/malloc.h> 56 #include <sys/systm.h> 57 #include <sys/buf.h> 58 #include <sys/device.h> 59 #include <sys/disk.h> 60 #include <sys/proc.h> 61 #include <sys/conf.h> 62 #include <sys/disklabel.h> 63 64 #include <uvm/uvm_extern.h> 65 66 #include <dev/md.h> 67 68 /* 69 * By default, include the user-space functionality. 70 * Use `options MEMORY_DISK_SERVER=0' to turn it off. 71 */ 72 #ifndef MEMORY_DISK_SERVER 73 #define MEMORY_DISK_SERVER 1 74 #endif /* MEMORY_DISK_SERVER */ 75 76 /* 77 * We should use the raw partition for ioctl. 78 */ 79 #define MD_MAX_UNITS 0x10 80 #define MD_UNIT(unit) DISKUNIT(unit) 81 82 /* autoconfig stuff... */ 83 84 struct md_softc { 85 struct device sc_dev; /* REQUIRED first entry */ 86 struct disk sc_dkdev; /* hook for generic disk handling */ 87 struct md_conf sc_md; 88 struct bufq_state sc_buflist; 89 }; 90 /* shorthand for fields in sc_md: */ 91 #define sc_addr sc_md.md_addr 92 #define sc_size sc_md.md_size 93 #define sc_type sc_md.md_type 94 95 void mdattach __P((int)); 96 static void md_attach __P((struct device *, struct device *, void *)); 97 98 dev_type_open(mdopen); 99 dev_type_close(mdclose); 100 dev_type_read(mdread); 101 dev_type_write(mdwrite); 102 dev_type_ioctl(mdioctl); 103 dev_type_strategy(mdstrategy); 104 dev_type_size(mdsize); 105 106 const struct bdevsw md_bdevsw = { 107 mdopen, mdclose, mdstrategy, mdioctl, nodump, mdsize, D_DISK 108 }; 109 110 const struct cdevsw md_cdevsw = { 111 mdopen, mdclose, mdread, mdwrite, mdioctl, 112 nostop, notty, nopoll, nommap, nokqfilter, D_DISK 113 }; 114 115 struct dkdriver mddkdriver = { mdstrategy }; 116 117 static int ramdisk_ndevs; 118 static void *ramdisk_devs[MD_MAX_UNITS]; 119 120 /* 121 * This is called if we are configured as a pseudo-device 122 */ 123 void 124 mdattach(n) 125 int n; 126 { 127 struct md_softc *sc; 128 int i; 129 130 #ifdef DIAGNOSTIC 131 if (ramdisk_ndevs) { 132 aprint_error("ramdisk: multiple attach calls?\n"); 133 return; 134 } 135 #endif 136 137 /* XXX: Are we supposed to provide a default? */ 138 if (n <= 1) 139 n = 1; 140 if (n > MD_MAX_UNITS) 141 n = MD_MAX_UNITS; 142 ramdisk_ndevs = n; 143 144 /* Attach as if by autoconfig. */ 145 for (i = 0; i < n; i++) { 146 147 sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT|M_ZERO); 148 if (!sc) { 149 aprint_error("ramdisk: malloc for attach failed!\n"); 150 return; 151 } 152 ramdisk_devs[i] = sc; 153 sc->sc_dev.dv_unit = i; 154 snprintf(sc->sc_dev.dv_xname, sizeof(sc->sc_dev.dv_xname), 155 "md%d", i); 156 md_attach(NULL, &sc->sc_dev, NULL); 157 } 158 } 159 160 static void 161 md_attach(parent, self, aux) 162 struct device *parent, *self; 163 void *aux; 164 { 165 struct md_softc *sc = (struct md_softc *)self; 166 167 bufq_alloc(&sc->sc_buflist, BUFQ_FCFS); 168 169 /* XXX - Could accept aux info here to set the config. */ 170 #ifdef MEMORY_DISK_HOOKS 171 /* 172 * This external function might setup a pre-loaded disk. 173 * All it would need to do is setup the md_conf struct. 174 * See sys/dev/md_root.c for an example. 175 */ 176 md_attach_hook(sc->sc_dev.dv_unit, &sc->sc_md); 177 #endif 178 179 /* 180 * Initialize and attach the disk structure. 181 */ 182 sc->sc_dkdev.dk_driver = &mddkdriver; 183 sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname; 184 disk_attach(&sc->sc_dkdev); 185 } 186 187 /* 188 * operational routines: 189 * open, close, read, write, strategy, 190 * ioctl, dump, size 191 */ 192 193 #if MEMORY_DISK_SERVER 194 static int md_server_loop __P((struct md_softc *sc)); 195 static int md_ioctl_server __P((struct md_softc *sc, 196 struct md_conf *umd, struct proc *proc)); 197 #endif /* MEMORY_DISK_SERVER */ 198 static int md_ioctl_kalloc __P((struct md_softc *sc, 199 struct md_conf *umd, struct proc *proc)); 200 201 int 202 mdsize(dev_t dev) 203 { 204 int unit; 205 struct md_softc *sc; 206 207 unit = MD_UNIT(dev); 208 if (unit >= ramdisk_ndevs) 209 return 0; 210 sc = ramdisk_devs[unit]; 211 if (sc == NULL) 212 return 0; 213 214 if (sc->sc_type == MD_UNCONFIGURED) 215 return 0; 216 217 return (sc->sc_size >> DEV_BSHIFT); 218 } 219 220 int 221 mdopen(dev, flag, fmt, proc) 222 dev_t dev; 223 int flag, fmt; 224 struct proc *proc; 225 { 226 int unit; 227 struct md_softc *sc; 228 229 unit = MD_UNIT(dev); 230 if (unit >= ramdisk_ndevs) 231 return ENXIO; 232 sc = ramdisk_devs[unit]; 233 if (sc == NULL) 234 return ENXIO; 235 236 /* 237 * The raw partition is used for ioctl to configure. 238 */ 239 if (DISKPART(dev) == RAW_PART) 240 return 0; 241 242 #ifdef MEMORY_DISK_HOOKS 243 /* Call the open hook to allow loading the device. */ 244 md_open_hook(unit, &sc->sc_md); 245 #endif 246 247 /* 248 * This is a normal, "slave" device, so 249 * enforce initialized. 250 */ 251 if (sc->sc_type == MD_UNCONFIGURED) 252 return ENXIO; 253 254 return 0; 255 } 256 257 int 258 mdclose(dev, flag, fmt, proc) 259 dev_t dev; 260 int flag, fmt; 261 struct proc *proc; 262 { 263 int unit; 264 265 unit = MD_UNIT(dev); 266 267 if (unit >= ramdisk_ndevs) 268 return ENXIO; 269 270 return 0; 271 } 272 273 int 274 mdread(dev, uio, flags) 275 dev_t dev; 276 struct uio *uio; 277 int flags; 278 { 279 int unit; 280 struct md_softc *sc; 281 282 unit = MD_UNIT(dev); 283 284 if (unit >= ramdisk_ndevs) 285 return ENXIO; 286 287 sc = ramdisk_devs[unit]; 288 289 if (sc->sc_type == MD_UNCONFIGURED) 290 return ENXIO; 291 292 return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio)); 293 } 294 295 int 296 mdwrite(dev, uio, flags) 297 dev_t dev; 298 struct uio *uio; 299 int flags; 300 { 301 int unit; 302 struct md_softc *sc; 303 304 unit = MD_UNIT(dev); 305 306 if (unit >= ramdisk_ndevs) 307 return ENXIO; 308 309 sc = ramdisk_devs[unit]; 310 311 if (sc->sc_type == MD_UNCONFIGURED) 312 return ENXIO; 313 314 return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio)); 315 } 316 317 /* 318 * Handle I/O requests, either directly, or 319 * by passing them to the server process. 320 */ 321 void 322 mdstrategy(bp) 323 struct buf *bp; 324 { 325 int unit; 326 struct md_softc *sc; 327 caddr_t addr; 328 size_t off, xfer; 329 330 unit = MD_UNIT(bp->b_dev); 331 sc = ramdisk_devs[unit]; 332 333 if (sc->sc_type == MD_UNCONFIGURED) { 334 bp->b_error = ENXIO; 335 bp->b_flags |= B_ERROR; 336 goto done; 337 } 338 339 switch (sc->sc_type) { 340 #if MEMORY_DISK_SERVER 341 case MD_UMEM_SERVER: 342 /* Just add this job to the server's queue. */ 343 BUFQ_PUT(&sc->sc_buflist, bp); 344 wakeup((caddr_t)sc); 345 /* see md_server_loop() */ 346 /* no biodone in this case */ 347 return; 348 #endif /* MEMORY_DISK_SERVER */ 349 350 case MD_KMEM_FIXED: 351 case MD_KMEM_ALLOCATED: 352 /* These are in kernel space. Access directly. */ 353 bp->b_resid = bp->b_bcount; 354 off = (bp->b_blkno << DEV_BSHIFT); 355 if (off >= sc->sc_size) { 356 if (bp->b_flags & B_READ) 357 break; /* EOF */ 358 goto set_eio; 359 } 360 xfer = bp->b_resid; 361 if (xfer > (sc->sc_size - off)) 362 xfer = (sc->sc_size - off); 363 addr = sc->sc_addr + off; 364 if (bp->b_flags & B_READ) 365 memcpy(bp->b_data, addr, xfer); 366 else 367 memcpy(addr, bp->b_data, xfer); 368 bp->b_resid -= xfer; 369 break; 370 371 default: 372 bp->b_resid = bp->b_bcount; 373 set_eio: 374 bp->b_error = EIO; 375 bp->b_flags |= B_ERROR; 376 break; 377 } 378 done: 379 biodone(bp); 380 } 381 382 int 383 mdioctl(dev, cmd, data, flag, proc) 384 dev_t dev; 385 u_long cmd; 386 int flag; 387 caddr_t data; 388 struct proc *proc; 389 { 390 int unit; 391 struct md_softc *sc; 392 struct md_conf *umd; 393 394 unit = MD_UNIT(dev); 395 sc = ramdisk_devs[unit]; 396 397 /* If this is not the raw partition, punt! */ 398 if (DISKPART(dev) != RAW_PART) 399 return ENOTTY; 400 401 umd = (struct md_conf *)data; 402 switch (cmd) { 403 case MD_GETCONF: 404 *umd = sc->sc_md; 405 return 0; 406 407 case MD_SETCONF: 408 /* Can only set it once. */ 409 if (sc->sc_type != MD_UNCONFIGURED) 410 break; 411 switch (umd->md_type) { 412 case MD_KMEM_ALLOCATED: 413 return md_ioctl_kalloc(sc, umd, proc); 414 #if MEMORY_DISK_SERVER 415 case MD_UMEM_SERVER: 416 return md_ioctl_server(sc, umd, proc); 417 #endif /* MEMORY_DISK_SERVER */ 418 default: 419 break; 420 } 421 break; 422 } 423 return EINVAL; 424 } 425 426 /* 427 * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED) 428 * Just allocate some kernel memory and return. 429 */ 430 static int 431 md_ioctl_kalloc(sc, umd, proc) 432 struct md_softc *sc; 433 struct md_conf *umd; 434 struct proc *proc; 435 { 436 vaddr_t addr; 437 vsize_t size; 438 439 /* Sanity check the size. */ 440 size = umd->md_size; 441 addr = uvm_km_zalloc(kernel_map, size); 442 if (!addr) 443 return ENOMEM; 444 445 /* This unit is now configured. */ 446 sc->sc_addr = (caddr_t)addr; /* kernel space */ 447 sc->sc_size = (size_t)size; 448 sc->sc_type = MD_KMEM_ALLOCATED; 449 return 0; 450 } 451 452 #if MEMORY_DISK_SERVER 453 454 /* 455 * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER) 456 * Set config, then become the I/O server for this unit. 457 */ 458 static int 459 md_ioctl_server(sc, umd, proc) 460 struct md_softc *sc; 461 struct md_conf *umd; 462 struct proc *proc; 463 { 464 vaddr_t end; 465 int error; 466 467 /* Sanity check addr, size. */ 468 end = (vaddr_t) (umd->md_addr + umd->md_size); 469 470 if ((end >= VM_MAXUSER_ADDRESS) || 471 (end < ((vaddr_t) umd->md_addr)) ) 472 return EINVAL; 473 474 /* This unit is now configured. */ 475 sc->sc_addr = umd->md_addr; /* user space */ 476 sc->sc_size = umd->md_size; 477 sc->sc_type = MD_UMEM_SERVER; 478 479 /* Become the server daemon */ 480 error = md_server_loop(sc); 481 482 /* This server is now going away! */ 483 sc->sc_type = MD_UNCONFIGURED; 484 sc->sc_addr = 0; 485 sc->sc_size = 0; 486 487 return (error); 488 } 489 490 int md_sleep_pri = PWAIT | PCATCH; 491 492 static int 493 md_server_loop(sc) 494 struct md_softc *sc; 495 { 496 struct buf *bp; 497 caddr_t addr; /* user space address */ 498 size_t off; /* offset into "device" */ 499 size_t xfer; /* amount to transfer */ 500 int error; 501 502 for (;;) { 503 /* Wait for some work to arrive. */ 504 while ((bp = BUFQ_GET(&sc->sc_buflist)) == NULL) { 505 error = tsleep((caddr_t)sc, md_sleep_pri, "md_idle", 0); 506 if (error) 507 return error; 508 } 509 510 /* Do the transfer to/from user space. */ 511 error = 0; 512 bp->b_resid = bp->b_bcount; 513 off = (bp->b_blkno << DEV_BSHIFT); 514 if (off >= sc->sc_size) { 515 if (bp->b_flags & B_READ) 516 goto done; /* EOF (not an error) */ 517 error = EIO; 518 goto done; 519 } 520 xfer = bp->b_resid; 521 if (xfer > (sc->sc_size - off)) 522 xfer = (sc->sc_size - off); 523 addr = sc->sc_addr + off; 524 if (bp->b_flags & B_READ) 525 error = copyin(addr, bp->b_data, xfer); 526 else 527 error = copyout(bp->b_data, addr, xfer); 528 if (!error) 529 bp->b_resid -= xfer; 530 531 done: 532 if (error) { 533 bp->b_error = error; 534 bp->b_flags |= B_ERROR; 535 } 536 biodone(bp); 537 } 538 } 539 #endif /* MEMORY_DISK_SERVER */ 540