[net.sources] RAM-disk driver for 4.2bsd

speck@cit-vax (Don Speck) (11/20/84)

    More a beginner's exercise in device drivers than anything else,
this pseudo-device provides a disk-like interface to a statically
allocated kernel array of size RAMSIZE.  Though originally intended
for shadowing a frame buffer, those suffering from an excess of RAM
may want to mkfs the block device and mount /tmp in real memory.
Actually, the memory would do more good if put in the buffer cache.
    Install the following file in /sys/sys/ (machine-independent!),
add a line to /sys/conf/files, add ram{open,read,write,strategy,size}
to the block & character device switches in /sys/machine/conf.c, and
add "pseudo-device ram" to the config file.  Create /dev entries.
    To use it as a filesystem (I have tried this and it works):
	/etc/mkfs /dev/ram0c 128 8 1 4096 1024 16 10 100 8192
	/etc/mount /dev/ram0c /aux
						In fun,
						Don Speck
--------/sys/sys/ram.c---------
/*  RAM pseudo-device to let data live in real memory (a
 *  statically allocated kernel array).  Really trivial.
 */

#include "ram.h"
#if NRAM > 0

#include "../h/param.h" 		/* Includes "../h/types.h" */
#include "../h/errno.h"

#define RAMSIZE (128*512)
char ram[NRAM][RAMSIZE];

ramopen(dev,wrtflag) dev_t dev; int wrtflag; {
	return(minor(dev) >= NRAM ? ENXIO : 0);
}

ramsize(dev) dev_t dev; {
	return(minor(dev) >= NRAM ? -1 : btodb(RAMSIZE));
}

#include "../h/uio.h"

ramread(dev,uio) dev_t dev; register struct uio *uio; {
	if ((unsigned)uio->uio_offset > RAMSIZE) return(EINVAL);
	return(uiomove(ram[minor(dev)]+uio->uio_offset,
		MIN(uio->uio_resid, RAMSIZE - uio->uio_offset),
		UIO_READ, uio));
}

ramwrite(dev,uio) dev_t dev; register struct uio *uio; {
	if ((unsigned)uio->uio_offset > RAMSIZE) return(EINVAL);
	return(uiomove(ram[minor(dev)]+uio->uio_offset,
		MIN(uio->uio_resid, RAMSIZE - uio->uio_offset),
		UIO_WRITE, uio));
}

#include "../h/buf.h"

ramstrategy(bp) register struct buf *bp; {
	register long offset = dbtob(bp->b_blkno);

	if ((u_long)offset > RAMSIZE) {
		bp->b_error = EINVAL;
		bp->b_flags |= B_ERROR;
	} else {
		caddr_t raddr = ram[minor(bp->b_dev)]+offset;
		unsigned nbytes = MIN(bp->b_bcount, RAMSIZE-offset);

		if (bp->b_flags&B_READ)
			bcopy(raddr, bp->b_un.b_addr, nbytes);
		else	bcopy(bp->b_un.b_addr, raddr, nbytes);
		bp->b_resid = bp->b_bcount - nbytes;
	}
	iodone(bp);
}
#endif
--------end of /sys/sys/ram.c--------