Home | History | Annotate | Line # | Download | only in dev
md.c revision 1.60
      1 /*	$NetBSD: md.c,v 1.60 2009/07/28 17:55:27 dyoung 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.60 2009/07/28 17:55:27 dyoung Exp $");
     50 
     51 #include "opt_md.h"
     52 #include "opt_tftproot.h"
     53 
     54 #include <sys/param.h>
     55 #include <sys/kernel.h>
     56 #include <sys/malloc.h>
     57 #include <sys/systm.h>
     58 #include <sys/buf.h>
     59 #include <sys/bufq.h>
     60 #include <sys/device.h>
     61 #include <sys/disk.h>
     62 #include <sys/stat.h>
     63 #include <sys/proc.h>
     64 #include <sys/conf.h>
     65 #include <sys/disklabel.h>
     66 
     67 #include <uvm/uvm_extern.h>
     68 
     69 #include <dev/md.h>
     70 
     71 /*
     72  * The user-space functionality is included by default.
     73  * Use  `options MEMORY_DISK_SERVER=0' to turn it off.
     74  */
     75 #ifndef MEMORY_DISK_SERVER
     76 #error MEMORY_DISK_SERVER should be defined by opt_md.h
     77 #endif	/* MEMORY_DISK_SERVER */
     78 
     79 /*
     80  * We should use the raw partition for ioctl.
     81  */
     82 #define MD_UNIT(unit)	DISKUNIT(unit)
     83 
     84 /* autoconfig stuff... */
     85 
     86 struct md_softc {
     87 	struct disk sc_dkdev;	/* hook for generic disk handling */
     88 	struct md_conf sc_md;
     89 	struct bufq_state *sc_buflist;
     90 };
     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
     95 
     96 void	mdattach(int);
     97 
     98 static void	md_attach(device_t, device_t, void *);
     99 static int	md_detach(device_t, int);
    100 
    101 static dev_type_open(mdopen);
    102 static dev_type_close(mdclose);
    103 static dev_type_read(mdread);
    104 static dev_type_write(mdwrite);
    105 static dev_type_ioctl(mdioctl);
    106 static dev_type_strategy(mdstrategy);
    107 static dev_type_size(mdsize);
    108 
    109 const struct bdevsw md_bdevsw = {
    110 	mdopen, mdclose, mdstrategy, mdioctl, nodump, mdsize, D_DISK
    111 };
    112 
    113 const struct cdevsw md_cdevsw = {
    114 	mdopen, mdclose, mdread, mdwrite, mdioctl,
    115 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    116 };
    117 
    118 static struct dkdriver mddkdriver = { mdstrategy, NULL };
    119 
    120 extern struct cfdriver md_cd;
    121 CFATTACH_DECL3_NEW(md, sizeof(struct md_softc),
    122 	0, md_attach, md_detach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
    123 
    124 extern size_t md_root_size;
    125 
    126 /*
    127  * This is called if we are configured as a pseudo-device
    128  */
    129 void
    130 mdattach(int n)
    131 {
    132 	int i;
    133 	cfdata_t cf;
    134 
    135 #ifdef TFTPROOT
    136 	/*
    137 	 * Attachement of md0 must be done after md_root_setconf(),
    138 	 * because the RAMdisk is not loaded yet.
    139 	 */
    140 	if (md_root_size == 0)
    141 		return;
    142 #endif
    143 	if (config_cfattach_attach("md", &md_ca)) {
    144 		printf("md: cfattach_attach failed\n");
    145 		return;
    146 	}
    147 
    148 	/* XXX:  Are we supposed to provide a default? */
    149 	if (n <= 1)
    150 		n = 1;
    151 
    152 	/* Attach as if by autoconfig. */
    153 	for (i = 0; i < n; i++) {
    154 		cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
    155 		cf->cf_name = "md";
    156 		cf->cf_atname = "md";
    157 		cf->cf_unit = i;
    158 		cf->cf_fstate = FSTATE_NOTFOUND;
    159 		(void)config_attach_pseudo(cf);
    160 	}
    161 }
    162 
    163 static void
    164 md_attach(device_t parent, device_t self, void *aux)
    165 {
    166 	struct md_softc *sc = device_private(self);
    167 
    168 	bufq_alloc(&sc->sc_buflist, "fcfs", 0);
    169 
    170 	/* XXX - Could accept aux info here to set the config. */
    171 #ifdef	MEMORY_DISK_HOOKS
    172 	/*
    173 	 * This external function might setup a pre-loaded disk.
    174 	 * All it would need to do is setup the md_conf struct.
    175 	 * See sys/dev/md_root.c for an example.
    176 	 */
    177 	md_attach_hook(device_unit(self), &sc->sc_md);
    178 #endif
    179 
    180 	/*
    181 	 * Initialize and attach the disk structure.
    182 	 */
    183 	disk_init(&sc->sc_dkdev, device_xname(self), &mddkdriver);
    184 	disk_attach(&sc->sc_dkdev);
    185 
    186 	if (!pmf_device_register(self, NULL, NULL))
    187 		aprint_error_dev(self, "couldn't establish power handler\n");
    188 }
    189 
    190 static int
    191 md_detach(device_t self, int flags)
    192 {
    193 	struct md_softc *sc = device_private(self);
    194 	int rc;
    195 
    196 	rc = 0;
    197 	mutex_enter(&sc->sc_dkdev.dk_openlock);
    198 	if (sc->sc_dkdev.dk_openmask == 0)
    199 		;	/* nothing to do */
    200 	else if ((flags & DETACH_FORCE) == 0)
    201 		rc = EBUSY;
    202 	mutex_exit(&sc->sc_dkdev.dk_openlock);
    203 
    204 	if (rc != 0)
    205 		return rc;
    206 
    207 	pmf_device_deregister(self);
    208 	disk_detach(&sc->sc_dkdev);
    209 	disk_destroy(&sc->sc_dkdev);
    210 	bufq_free(sc->sc_buflist);
    211 	return 0;
    212 }
    213 
    214 /*
    215  * operational routines:
    216  * open, close, read, write, strategy,
    217  * ioctl, dump, size
    218  */
    219 
    220 #if MEMORY_DISK_SERVER
    221 static int	md_server_loop(struct md_softc *sc);
    222 static int	md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
    223 		    struct lwp *l);
    224 #endif	/* MEMORY_DISK_SERVER */
    225 static int	md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
    226 		    struct lwp *l);
    227 
    228 static int
    229 mdsize(dev_t dev)
    230 {
    231 	struct md_softc *sc;
    232 
    233 	sc = device_lookup_private(&md_cd, MD_UNIT(dev));
    234 	if (sc == NULL)
    235 		return 0;
    236 
    237 	if (sc->sc_type == MD_UNCONFIGURED)
    238 		return 0;
    239 
    240 	return (sc->sc_size >> DEV_BSHIFT);
    241 }
    242 
    243 static int
    244 mdopen(dev_t dev, int flag, int fmt, struct lwp *l)
    245 {
    246 	int unit;
    247 	int part = DISKPART(dev);
    248 	int pmask = 1 << part;
    249 	struct md_softc *sc;
    250 	struct disk *dk;
    251 
    252 	unit = MD_UNIT(dev);
    253 	sc = device_lookup_private(&md_cd, unit);
    254 	if (sc == NULL)
    255 		return ENXIO;
    256 
    257 	dk = &sc->sc_dkdev;
    258 
    259 	/*
    260 	 * The raw partition is used for ioctl to configure.
    261 	 */
    262 	if (part == RAW_PART)
    263 		goto ok;
    264 
    265 #ifdef	MEMORY_DISK_HOOKS
    266 	/* Call the open hook to allow loading the device. */
    267 	md_open_hook(unit, &sc->sc_md);
    268 #endif
    269 
    270 	/*
    271 	 * This is a normal, "slave" device, so
    272 	 * enforce initialized.
    273 	 */
    274 	if (sc->sc_type == MD_UNCONFIGURED)
    275 		return ENXIO;
    276 
    277 ok:
    278 	/* XXX duplicates code in dk_open().  Call dk_open(), instead? */
    279 	mutex_enter(&dk->dk_openlock);
    280 	/* Mark our unit as open. */
    281 	switch (fmt) {
    282 	case S_IFCHR:
    283 		dk->dk_copenmask |= pmask;
    284 		break;
    285 	case S_IFBLK:
    286 		dk->dk_bopenmask |= pmask;
    287 		break;
    288 	}
    289 
    290 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    291 
    292 	mutex_exit(&dk->dk_openlock);
    293 	return 0;
    294 }
    295 
    296 static int
    297 mdclose(dev_t dev, int flag, int fmt, struct lwp *l)
    298 {
    299 	int part = DISKPART(dev);
    300 	int pmask = 1 << part;
    301 	struct md_softc *sc;
    302 	struct disk *dk;
    303 
    304 	sc = device_lookup_private(&md_cd, MD_UNIT(dev));
    305 	if (sc == NULL)
    306 		return ENXIO;
    307 
    308 	dk = &sc->sc_dkdev;
    309 
    310 	mutex_enter(&dk->dk_openlock);
    311 
    312 	switch (fmt) {
    313 	case S_IFCHR:
    314 		dk->dk_copenmask &= ~pmask;
    315 		break;
    316 	case S_IFBLK:
    317 		dk->dk_bopenmask &= ~pmask;
    318 		break;
    319 	}
    320 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    321 
    322 	mutex_exit(&dk->dk_openlock);
    323 	return 0;
    324 }
    325 
    326 static int
    327 mdread(dev_t dev, struct uio *uio, int flags)
    328 {
    329 	struct md_softc *sc;
    330 
    331 	sc = device_lookup_private(&md_cd, MD_UNIT(dev));
    332 
    333 	if (sc->sc_type == MD_UNCONFIGURED)
    334 		return ENXIO;
    335 
    336 	return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
    337 }
    338 
    339 static int
    340 mdwrite(dev_t dev, struct uio *uio, int flags)
    341 {
    342 	struct md_softc *sc;
    343 
    344 	sc = device_lookup_private(&md_cd, MD_UNIT(dev));
    345 
    346 	if (sc->sc_type == MD_UNCONFIGURED)
    347 		return ENXIO;
    348 
    349 	return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
    350 }
    351 
    352 /*
    353  * Handle I/O requests, either directly, or
    354  * by passing them to the server process.
    355  */
    356 static void
    357 mdstrategy(struct buf *bp)
    358 {
    359 	struct md_softc	*sc;
    360 	void *	addr;
    361 	size_t off, xfer;
    362 
    363 	sc = device_lookup_private(&md_cd, MD_UNIT(bp->b_dev));
    364 
    365 	if (sc->sc_type == MD_UNCONFIGURED) {
    366 		bp->b_error = ENXIO;
    367 		goto done;
    368 	}
    369 
    370 	switch (sc->sc_type) {
    371 #if MEMORY_DISK_SERVER
    372 	case MD_UMEM_SERVER:
    373 		/* Just add this job to the server's queue. */
    374 		bufq_put(sc->sc_buflist, bp);
    375 		wakeup((void *)sc);
    376 		/* see md_server_loop() */
    377 		/* no biodone in this case */
    378 		return;
    379 #endif	/* MEMORY_DISK_SERVER */
    380 
    381 	case MD_KMEM_FIXED:
    382 	case MD_KMEM_ALLOCATED:
    383 		/* These are in kernel space.  Access directly. */
    384 		bp->b_resid = bp->b_bcount;
    385 		off = (bp->b_blkno << DEV_BSHIFT);
    386 		if (off >= sc->sc_size) {
    387 			if (bp->b_flags & B_READ)
    388 				break;	/* EOF */
    389 			goto set_eio;
    390 		}
    391 		xfer = bp->b_resid;
    392 		if (xfer > (sc->sc_size - off))
    393 			xfer = (sc->sc_size - off);
    394 		addr = (char *)sc->sc_addr + off;
    395 		if (bp->b_flags & B_READ)
    396 			memcpy(bp->b_data, addr, xfer);
    397 		else
    398 			memcpy(addr, bp->b_data, xfer);
    399 		bp->b_resid -= xfer;
    400 		break;
    401 
    402 	default:
    403 		bp->b_resid = bp->b_bcount;
    404 	set_eio:
    405 		bp->b_error = EIO;
    406 		break;
    407 	}
    408  done:
    409 	biodone(bp);
    410 }
    411 
    412 static int
    413 mdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    414 {
    415 	struct md_softc *sc;
    416 	struct md_conf *umd;
    417 
    418 	sc = device_lookup_private(&md_cd, MD_UNIT(dev));
    419 
    420 	/* If this is not the raw partition, punt! */
    421 	if (DISKPART(dev) != RAW_PART)
    422 		return ENOTTY;
    423 
    424 	umd = (struct md_conf *)data;
    425 	switch (cmd) {
    426 	case MD_GETCONF:
    427 		*umd = sc->sc_md;
    428 		return 0;
    429 
    430 	case MD_SETCONF:
    431 		/* Can only set it once. */
    432 		if (sc->sc_type != MD_UNCONFIGURED)
    433 			break;
    434 		switch (umd->md_type) {
    435 		case MD_KMEM_ALLOCATED:
    436 			return md_ioctl_kalloc(sc, umd, l);
    437 #if MEMORY_DISK_SERVER
    438 		case MD_UMEM_SERVER:
    439 			return md_ioctl_server(sc, umd, l);
    440 #endif	/* MEMORY_DISK_SERVER */
    441 		default:
    442 			break;
    443 		}
    444 		break;
    445 	}
    446 	return EINVAL;
    447 }
    448 
    449 /*
    450  * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
    451  * Just allocate some kernel memory and return.
    452  */
    453 static int
    454 md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
    455     struct lwp *l)
    456 {
    457 	vaddr_t addr;
    458 	vsize_t size;
    459 
    460 	/* Sanity check the size. */
    461 	size = umd->md_size;
    462 	addr = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
    463 	if (!addr)
    464 		return ENOMEM;
    465 
    466 	/* This unit is now configured. */
    467 	sc->sc_addr = (void *)addr; 	/* kernel space */
    468 	sc->sc_size = (size_t)size;
    469 	sc->sc_type = MD_KMEM_ALLOCATED;
    470 	return 0;
    471 }
    472 
    473 #if MEMORY_DISK_SERVER
    474 
    475 /*
    476  * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
    477  * Set config, then become the I/O server for this unit.
    478  */
    479 static int
    480 md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
    481     struct lwp *l)
    482 {
    483 	vaddr_t end;
    484 	int error;
    485 
    486 	/* Sanity check addr, size. */
    487 	end = (vaddr_t) ((char *)umd->md_addr + umd->md_size);
    488 
    489 	if ((end >= VM_MAXUSER_ADDRESS) ||
    490 		(end < ((vaddr_t) umd->md_addr)) )
    491 		return EINVAL;
    492 
    493 	/* This unit is now configured. */
    494 	sc->sc_addr = umd->md_addr; 	/* user space */
    495 	sc->sc_size = umd->md_size;
    496 	sc->sc_type = MD_UMEM_SERVER;
    497 
    498 	/* Become the server daemon */
    499 	error = md_server_loop(sc);
    500 
    501 	/* This server is now going away! */
    502 	sc->sc_type = MD_UNCONFIGURED;
    503 	sc->sc_addr = 0;
    504 	sc->sc_size = 0;
    505 
    506 	return (error);
    507 }
    508 
    509 static int md_sleep_pri = PWAIT | PCATCH;
    510 
    511 static int
    512 md_server_loop(struct md_softc *sc)
    513 {
    514 	struct buf *bp;
    515 	void *addr;	/* user space address */
    516 	size_t off;	/* offset into "device" */
    517 	size_t xfer;	/* amount to transfer */
    518 	int error;
    519 
    520 	for (;;) {
    521 		/* Wait for some work to arrive. */
    522 		while ((bp = bufq_get(sc->sc_buflist)) == NULL) {
    523 			error = tsleep((void *)sc, md_sleep_pri, "md_idle", 0);
    524 			if (error)
    525 				return error;
    526 		}
    527 
    528 		/* Do the transfer to/from user space. */
    529 		error = 0;
    530 		bp->b_resid = bp->b_bcount;
    531 		off = (bp->b_blkno << DEV_BSHIFT);
    532 		if (off >= sc->sc_size) {
    533 			if (bp->b_flags & B_READ)
    534 				goto done;	/* EOF (not an error) */
    535 			error = EIO;
    536 			goto done;
    537 		}
    538 		xfer = bp->b_resid;
    539 		if (xfer > (sc->sc_size - off))
    540 			xfer = (sc->sc_size - off);
    541 		addr = (char *)sc->sc_addr + off;
    542 		if (bp->b_flags & B_READ)
    543 			error = copyin(addr, bp->b_data, xfer);
    544 		else
    545 			error = copyout(bp->b_data, addr, xfer);
    546 		if (!error)
    547 			bp->b_resid -= xfer;
    548 
    549 	done:
    550 		if (error) {
    551 			bp->b_error = error;
    552 		}
    553 		biodone(bp);
    554 	}
    555 }
    556 #endif	/* MEMORY_DISK_SERVER */
    557