1 /* $NetBSD: md.c,v 1.18 1998/09/01 06:13:33 enami 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 "opt_uvm.h" 49 50 #include <sys/param.h> 51 #include <sys/kernel.h> 52 #include <sys/malloc.h> 53 #include <sys/systm.h> 54 #include <sys/buf.h> 55 #include <sys/device.h> 56 #include <sys/disk.h> 57 #include <sys/proc.h> 58 #include <sys/conf.h> 59 #include <sys/disklabel.h> 60 61 #include <vm/vm.h> 62 #include <vm/vm_kern.h> 63 #include <vm/vm_extern.h> 64 65 #include <dev/md.h> 66 67 /* 68 * By default, include the user-space functionality. 69 * Use `options MEMORY_DISK_SERVER=0' to turn it off. 70 */ 71 #ifndef MEMORY_DISK_SERVER 72 #define MEMORY_DISK_SERVER 1 73 #endif 74 75 /* 76 * XXX: the "control" unit is (base unit + 16). 77 * We should just use the cdev as the "control", but 78 * that interferes with the security stuff preventing 79 * simulatneous use of raw and block devices. 80 * 81 * XXX Assumption: 16 memory-disks are enough! 82 */ 83 #define MD_MAX_UNITS 0x10 84 #define MD_IS_CTRL(unit) (unit & 0x10) 85 #define MD_UNIT(unit) (unit & 0xF) 86 87 /* autoconfig stuff... */ 88 89 struct md_softc { 90 struct device sc_dev; /* REQUIRED first entry */ 91 struct disk sc_dkdev; /* hook for generic disk handling */ 92 struct md_conf sc_md; 93 struct buf *sc_buflist; 94 int sc_flags; 95 }; 96 /* shorthand for fields in sc_md: */ 97 #define sc_addr sc_md.md_addr 98 #define sc_size sc_md.md_size 99 #define sc_type sc_md.md_type 100 /* flags */ 101 #define MD_ISOPEN 0x01 102 #define MD_SERVED 0x02 103 104 void mdattach __P((int)); 105 static void md_attach __P((struct device *, struct device *, void *)); 106 107 void mdstrategy __P((struct buf *bp)); 108 struct dkdriver mddkdriver = { mdstrategy }; 109 110 static int ramdisk_ndevs; 111 static void *ramdisk_devs[MD_MAX_UNITS]; 112 113 /* 114 * This is called if we are configured as a pseudo-device 115 */ 116 void 117 mdattach(n) 118 int n; 119 { 120 struct md_softc *sc; 121 int i; 122 123 #ifdef DIAGNOSTIC 124 if (ramdisk_ndevs) { 125 printf("ramdisk: multiple attach calls?\n"); 126 return; 127 } 128 #endif 129 130 /* XXX: Are we supposed to provide a default? */ 131 if (n <= 1) 132 n = 1; 133 if (n > MD_MAX_UNITS) 134 n = MD_MAX_UNITS; 135 ramdisk_ndevs = n; 136 137 /* Attach as if by autoconfig. */ 138 for (i = 0; i < n; i++) { 139 140 sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT); 141 if (!sc) { 142 printf("ramdisk: malloc for attach failed!\n"); 143 return; 144 } 145 bzero((caddr_t)sc, sizeof(*sc)); 146 ramdisk_devs[i] = sc; 147 sc->sc_dev.dv_unit = i; 148 sprintf(sc->sc_dev.dv_xname, "md%d", i); 149 md_attach(NULL, &sc->sc_dev, NULL); 150 } 151 } 152 153 static void 154 md_attach(parent, self, aux) 155 struct device *parent, *self; 156 void *aux; 157 { 158 struct md_softc *sc = (struct md_softc *)self; 159 160 /* XXX - Could accept aux info here to set the config. */ 161 #ifdef MEMORY_DISK_HOOKS 162 /* 163 * This external function might setup a pre-loaded disk. 164 * All it would need to do is setup the md_conf struct. 165 * See sys/arch/sun3/dev/md_root.c for an example. 166 */ 167 md_attach_hook(sc->sc_dev.dv_unit, &sc->sc_md); 168 #endif 169 170 /* 171 * Initialize and attach the disk structure. 172 */ 173 sc->sc_dkdev.dk_driver = &mddkdriver; 174 sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname; 175 disk_attach(&sc->sc_dkdev); 176 } 177 178 /* 179 * operational routines: 180 * open, close, read, write, strategy, 181 * ioctl, dump, size 182 */ 183 184 #if MEMORY_DISK_SERVER 185 static int md_server_loop __P((struct md_softc *sc)); 186 static int md_ioctl_server __P((struct md_softc *sc, 187 struct md_conf *umd, struct proc *proc)); 188 #endif 189 static int md_ioctl_kalloc __P((struct md_softc *sc, 190 struct md_conf *umd, struct proc *proc)); 191 192 dev_type_open(mdopen); 193 dev_type_close(mdclose); 194 dev_type_read(mdread); 195 dev_type_write(mdwrite); 196 dev_type_ioctl(mdioctl); 197 dev_type_size(mdsize); 198 dev_type_dump(mddump); 199 200 int mddump(dev, blkno, va, size) 201 dev_t dev; 202 daddr_t blkno; 203 caddr_t va; 204 size_t size; 205 { 206 return ENODEV; 207 } 208 209 int mdsize(dev_t dev) 210 { 211 int unit; 212 struct md_softc *sc; 213 214 /* Disallow control units. */ 215 unit = DISKUNIT(dev); 216 if (unit >= ramdisk_ndevs) 217 return 0; 218 sc = ramdisk_devs[unit]; 219 if (sc == NULL) 220 return 0; 221 222 if (sc->sc_type == MD_UNCONFIGURED) 223 return 0; 224 225 return (sc->sc_size >> DEV_BSHIFT); 226 } 227 228 int 229 mdopen(dev, flag, fmt, proc) 230 dev_t dev; 231 int flag, fmt; 232 struct proc *proc; 233 { 234 int md, unit; 235 struct md_softc *sc; 236 237 md = DISKUNIT(dev); 238 unit = MD_UNIT(md); 239 if (unit >= ramdisk_ndevs) 240 return ENXIO; 241 sc = ramdisk_devs[unit]; 242 if (sc == NULL) 243 return ENXIO; 244 245 /* 246 * The control device is not exclusive, and can 247 * open uninitialized units (so you can setconf). 248 */ 249 if (MD_IS_CTRL(md)) 250 return 0; 251 252 #ifdef MEMORY_DISK_HOOKS 253 /* Call the open hook to allow loading the device. */ 254 md_open_hook(unit, &sc->sc_md); 255 #endif 256 257 /* 258 * This is a normal, "slave" device, so 259 * enforce initialized, exclusive open. 260 */ 261 if (sc->sc_type == MD_UNCONFIGURED) 262 return ENXIO; 263 if (sc->sc_flags & MD_ISOPEN) 264 return EBUSY; 265 266 return 0; 267 } 268 269 int 270 mdclose(dev, flag, fmt, proc) 271 dev_t dev; 272 int flag, fmt; 273 struct proc *proc; 274 { 275 int md, unit; 276 struct md_softc *sc; 277 278 md = DISKUNIT(dev); 279 unit = MD_UNIT(md); 280 sc = ramdisk_devs[unit]; 281 282 if (MD_IS_CTRL(md)) 283 return 0; 284 285 /* Normal device. */ 286 sc->sc_flags = 0; 287 288 return 0; 289 } 290 291 int 292 mdread(dev, uio, flags) 293 dev_t dev; 294 struct uio *uio; 295 int flags; 296 { 297 return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio)); 298 } 299 300 int 301 mdwrite(dev, uio, flags) 302 dev_t dev; 303 struct uio *uio; 304 int flags; 305 { 306 return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio)); 307 } 308 309 /* 310 * Handle I/O requests, either directly, or 311 * by passing them to the server process. 312 */ 313 void 314 mdstrategy(bp) 315 struct buf *bp; 316 { 317 int md, unit; 318 struct md_softc *sc; 319 caddr_t addr; 320 size_t off, xfer; 321 322 md = DISKUNIT(bp->b_dev); 323 unit = MD_UNIT(md); 324 sc = ramdisk_devs[unit]; 325 326 switch (sc->sc_type) { 327 #if MEMORY_DISK_SERVER 328 case MD_UMEM_SERVER: 329 /* Just add this job to the server's queue. */ 330 bp->b_actf = sc->sc_buflist; 331 sc->sc_buflist = bp; 332 if (bp->b_actf == NULL) { 333 /* server queue was empty. */ 334 wakeup((caddr_t)sc); 335 /* see md_server_loop() */ 336 } 337 /* no biodone in this case */ 338 return; 339 #endif /* MEMORY_DISK_SERVER */ 340 341 case MD_KMEM_FIXED: 342 case MD_KMEM_ALLOCATED: 343 /* These are in kernel space. Access directly. */ 344 bp->b_resid = bp->b_bcount; 345 off = (bp->b_blkno << DEV_BSHIFT); 346 if (off >= sc->sc_size) { 347 if (bp->b_flags & B_READ) 348 break; /* EOF */ 349 goto set_eio; 350 } 351 xfer = bp->b_resid; 352 if (xfer > (sc->sc_size - off)) 353 xfer = (sc->sc_size - off); 354 addr = sc->sc_addr + off; 355 if (bp->b_flags & B_READ) 356 bcopy(addr, bp->b_data, xfer); 357 else 358 bcopy(bp->b_data, addr, xfer); 359 bp->b_resid -= xfer; 360 break; 361 362 default: 363 bp->b_resid = bp->b_bcount; 364 set_eio: 365 bp->b_error = EIO; 366 bp->b_flags |= B_ERROR; 367 break; 368 } 369 biodone(bp); 370 } 371 372 int 373 mdioctl(dev, cmd, data, flag, proc) 374 dev_t dev; 375 u_long cmd; 376 int flag; 377 caddr_t data; 378 struct proc *proc; 379 { 380 int md, unit; 381 struct md_softc *sc; 382 struct md_conf *umd; 383 384 md = DISKUNIT(dev); 385 unit = MD_UNIT(md); 386 sc = ramdisk_devs[unit]; 387 388 /* If this is not the control device, punt! */ 389 if (MD_IS_CTRL(md) == 0) 390 return ENOTTY; 391 392 umd = (struct md_conf *)data; 393 switch (cmd) { 394 case MD_GETCONF: 395 *umd = sc->sc_md; 396 return 0; 397 398 case MD_SETCONF: 399 /* Can only set it once. */ 400 if (sc->sc_type != MD_UNCONFIGURED) 401 break; 402 switch (umd->md_type) { 403 case MD_KMEM_ALLOCATED: 404 return md_ioctl_kalloc(sc, umd, proc); 405 #if MEMORY_DISK_SERVER 406 case MD_UMEM_SERVER: 407 return md_ioctl_server(sc, umd, proc); 408 #endif 409 default: 410 break; 411 } 412 break; 413 } 414 return EINVAL; 415 } 416 417 /* 418 * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED) 419 * Just allocate some kernel memory and return. 420 */ 421 static int 422 md_ioctl_kalloc(sc, umd, proc) 423 struct md_softc *sc; 424 struct md_conf *umd; 425 struct proc *proc; 426 { 427 vaddr_t addr; 428 vsize_t size; 429 430 /* Sanity check the size. */ 431 size = umd->md_size; 432 #if defined(UVM) 433 addr = uvm_km_zalloc(kernel_map, size); 434 #else 435 addr = kmem_alloc(kernel_map, size); 436 #endif 437 if (!addr) 438 return ENOMEM; 439 440 /* This unit is now configured. */ 441 sc->sc_addr = (caddr_t)addr; /* kernel space */ 442 sc->sc_size = (size_t)size; 443 sc->sc_type = MD_KMEM_ALLOCATED; 444 return 0; 445 } 446 447 #if MEMORY_DISK_SERVER 448 449 /* 450 * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER) 451 * Set config, then become the I/O server for this unit. 452 */ 453 static int 454 md_ioctl_server(sc, umd, proc) 455 struct md_softc *sc; 456 struct md_conf *umd; 457 struct proc *proc; 458 { 459 vaddr_t end; 460 int error; 461 462 /* Sanity check addr, size. */ 463 end = (vaddr_t) (umd->md_addr + umd->md_size); 464 465 if ((end >= VM_MAXUSER_ADDRESS) || 466 (end < ((vaddr_t) umd->md_addr)) ) 467 return EINVAL; 468 469 /* This unit is now configured. */ 470 sc->sc_addr = umd->md_addr; /* user space */ 471 sc->sc_size = umd->md_size; 472 sc->sc_type = MD_UMEM_SERVER; 473 474 /* Become the server daemon */ 475 error = md_server_loop(sc); 476 477 /* This server is now going away! */ 478 sc->sc_type = MD_UNCONFIGURED; 479 sc->sc_addr = 0; 480 sc->sc_size = 0; 481 482 return (error); 483 } 484 485 int md_sleep_pri = PWAIT | PCATCH; 486 487 static int 488 md_server_loop(sc) 489 struct md_softc *sc; 490 { 491 struct buf *bp; 492 caddr_t addr; /* user space address */ 493 size_t off; /* offset into "device" */ 494 size_t xfer; /* amount to transfer */ 495 int error; 496 497 for (;;) { 498 /* Wait for some work to arrive. */ 499 while (sc->sc_buflist == NULL) { 500 error = tsleep((caddr_t)sc, md_sleep_pri, "md_idle", 0); 501 if (error) 502 return error; 503 } 504 505 /* Unlink buf from head of list. */ 506 bp = sc->sc_buflist; 507 sc->sc_buflist = bp->b_actf; 508 bp->b_actf = NULL; 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