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