2 * ramdisk.c - Multiple RAM disk driver - gzip-loading version - v. 0.8 beta.
4 * (C) Chad Page, Theodore Ts'o, et. al, 1995.
6 * This RAM disk is designed to have filesystems created on it and mounted
7 * just like a regular floppy disk.
9 * It also does something suggested by Linus: use the buffer cache as the
10 * RAM disk data. This makes it possible to dynamically allocate the RAM disk
11 * buffer - with some consequences I have to deal with as I write this.
13 * This code is based on the original ramdisk.c, written mostly by
14 * Theodore Ts'o (TYT) in 1991. The code was largely rewritten by
15 * Chad Page to use the buffer cache to store the RAM disk data in
16 * 1995; Theodore then took over the driver again, and cleaned it up
17 * for inclusion in the mainline kernel.
19 * The original CRAMDISK code was written by Richard Lyons, and
20 * adapted by Chad Page to use the new RAM disk interface. Theodore
21 * Ts'o rewrote it so that both the compressed RAM disk loader and the
22 * kernel decompressor uses the same inflate.c codebase. The RAM disk
23 * loader now also loads into a dynamic (buffer cache based) RAM disk,
24 * not the old static RAM disk. Support for the old static RAM disk has
25 * been completely removed.
27 * Loadable module support added by Tom Dyas.
29 * Further cleanups by Chad Page (page0588@sundance.sjsu.edu):
30 * Cosmetic changes in #ifdef MODULE, code movement, etc.
31 * When the RAM disk module is removed, free the protected buffers
32 * Default RAM disk size changed to 2.88 MB
34 * Added initrd: Werner Almesberger & Hans Lermen, Feb '96
36 * 4/25/96 : Made RAM disk size a parameter (default is now 4 MB)
39 * Add support for fs images split across >1 disk, Paul Gortmaker, Mar '98
41 * Make block size and block size shift for RAM disks a global macro
42 * and set blk_size for -ENOSPC, Werner Fink <werner@suse.de>, Apr '99
45 #include <linux/config.h>
46 #include <linux/string.h>
47 #include <linux/slab.h>
48 #include <asm/atomic.h>
49 #include <linux/bio.h>
50 #include <linux/module.h>
51 #include <linux/init.h>
52 #include <linux/devfs_fs_kernel.h>
53 #include <linux/pagemap.h>
54 #include <linux/blkdev.h>
55 #include <linux/genhd.h>
56 #include <linux/bio.h>
57 #include <linux/buffer_head.h> /* for invalidate_bdev() */
58 #include <linux/backing-dev.h>
59 #include <linux/blkpg.h>
60 #include <asm/uaccess.h>
62 /* The RAM disk size is now a parameter */
63 #define NUM_RAMDISKS 16 /* This cannot be overridden (yet) */
65 /* Various static variables go here. Most are used only in the RAM disk code.
68 static struct gendisk *rd_disks[NUM_RAMDISKS];
69 static struct block_device *rd_bdev[NUM_RAMDISKS];/* Protected device data */
70 static struct request_queue *rd_queue[NUM_RAMDISKS];
73 * Parameters for the boot-loading of the RAM disk. These are set by
74 * init/main.c (from arguments to the kernel command line) or from the
75 * architecture-specific setup routine (from the stored boot sector
78 int rd_size = CONFIG_BLK_DEV_RAM_SIZE; /* Size of the RAM disks */
80 * It would be very desirable to have a soft-blocksize (that in the case
81 * of the ramdisk driver is also the hardblocksize ;) of PAGE_SIZE because
82 * doing that we'll achieve a far better MM footprint. Using a rd_blocksize of
83 * BLOCK_SIZE in the worst case we'll make PAGE_SIZE/BLOCK_SIZE buffer-pages
84 * unfreeable. With a rd_blocksize of PAGE_SIZE instead we are sure that only
85 * 1 page will be protected. Depending on the size of the ramdisk you
86 * may want to change the ramdisk blocksize to achieve a better or worse MM
87 * behaviour. The default is still BLOCK_SIZE (needed by rd_load_image that
88 * supposes the filesystem in the image uses a BLOCK_SIZE blocksize).
90 int rd_blocksize = BLOCK_SIZE; /* blocksize of the RAM disks */
93 * Copyright (C) 2000 Linus Torvalds.
94 * 2000 Transmeta Corp.
95 * aops copied from ramfs.
97 static int ramdisk_readpage(struct file *file, struct page *page)
99 if (!PageUptodate(page)) {
100 void *kaddr = kmap_atomic(page, KM_USER0);
102 memset(kaddr, 0, PAGE_CACHE_SIZE);
103 flush_dcache_page(page);
104 kunmap_atomic(kaddr, KM_USER0);
105 SetPageUptodate(page);
111 static int ramdisk_prepare_write(struct file *file, struct page *page,
112 unsigned offset, unsigned to)
114 if (!PageUptodate(page)) {
115 void *kaddr = kmap_atomic(page, KM_USER0);
117 memset(kaddr, 0, PAGE_CACHE_SIZE);
118 flush_dcache_page(page);
119 kunmap_atomic(kaddr, KM_USER0);
120 SetPageUptodate(page);
126 static int ramdisk_commit_write(struct file *file, struct page *page,
127 unsigned offset, unsigned to)
132 static struct address_space_operations ramdisk_aops = {
133 .readpage = ramdisk_readpage,
134 .prepare_write = ramdisk_prepare_write,
135 .commit_write = ramdisk_commit_write,
138 static int rd_blkdev_pagecache_IO(int rw, struct bio_vec *vec, sector_t sector,
139 struct address_space *mapping)
141 unsigned long index = sector >> (PAGE_CACHE_SHIFT - 9);
142 unsigned int vec_offset = vec->bv_offset;
143 int offset = (sector << 9) & ~PAGE_CACHE_MASK;
144 int size = vec->bv_len;
153 count = PAGE_CACHE_SIZE - offset;
158 page = find_get_page(mapping, index);
160 page = grab_cache_page(mapping, index);
166 if (!PageUptodate(page)) {
167 void *kaddr = kmap_atomic(page, KM_USER0);
169 memset(kaddr, 0, PAGE_CACHE_SIZE);
170 flush_dcache_page(page);
171 kunmap_atomic(kaddr, KM_USER0);
172 SetPageUptodate(page);
181 src = kmap(page) + offset;
182 dst = kmap(vec->bv_page) + vec_offset;
184 dst = kmap(page) + offset;
185 src = kmap(vec->bv_page) + vec_offset;
190 memcpy(dst, src, count);
193 kunmap(vec->bv_page);
196 flush_dcache_page(vec->bv_page);
210 * Basically, my strategy here is to set up a buffer-head which can't be
211 * deleted, and make that my Ramdisk. If the request is outside of the
212 * allocated size, we must get rid of it...
214 * 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Added devfs support
217 static int rd_make_request(request_queue_t *q, struct bio *bio)
219 struct block_device *bdev = bio->bi_bdev;
220 struct address_space * mapping = bdev->bd_inode->i_mapping;
221 sector_t sector = bio->bi_sector;
222 unsigned long len = bio->bi_size >> 9;
223 int rw = bio_data_dir(bio);
224 struct bio_vec *bvec;
227 if (sector + len > get_capacity(bdev->bd_disk))
233 bio_for_each_segment(bvec, bio, i) {
234 ret |= rd_blkdev_pagecache_IO(rw, bvec, sector, mapping);
235 sector += bvec->bv_len >> 9;
240 bio_endio(bio, bio->bi_size, 0);
243 bio_io_error(bio, bio->bi_size);
247 static int rd_ioctl(struct inode *inode, struct file *file,
248 unsigned int cmd, unsigned long arg)
251 struct block_device *bdev = inode->i_bdev;
253 if (cmd != BLKFLSBUF)
257 * special: we want to release the ramdisk memory, it's not like with
258 * the other blockdevices where this ioctl only flushes away the buffer
263 if (bdev->bd_openers <= 2) {
264 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
271 static struct backing_dev_info rd_backing_dev_info = {
272 .ra_pages = 0, /* No readahead */
273 .memory_backed = 1, /* Does not contribute to dirty memory */
274 .unplug_io_fn = default_unplug_io_fn,
277 static int rd_open(struct inode *inode, struct file *filp)
279 unsigned unit = iminor(inode);
282 * Immunize device against invalidate_buffers() and prune_icache().
284 if (rd_bdev[unit] == NULL) {
285 struct block_device *bdev = inode->i_bdev;
286 inode = igrab(bdev->bd_inode);
287 rd_bdev[unit] = bdev;
289 bdev->bd_block_size = rd_blocksize;
290 inode->i_size = get_capacity(rd_disks[unit])<<9;
291 inode->i_mapping->a_ops = &ramdisk_aops;
292 inode->i_mapping->backing_dev_info = &rd_backing_dev_info;
298 static struct block_device_operations rd_bd_op = {
299 .owner = THIS_MODULE,
305 * Before freeing the module, invalidate all of the protected buffers!
307 static void __exit rd_cleanup(void)
311 for (i = 0; i < NUM_RAMDISKS; i++) {
312 struct block_device *bdev = rd_bdev[i];
315 invalidate_bdev(bdev, 1);
318 del_gendisk(rd_disks[i]);
319 put_disk(rd_disks[i]);
320 blk_cleanup_queue(rd_queue[i]);
323 unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
327 * This is the registration and initialization section of the RAM disk driver
329 static int __init rd_init(void)
334 if (rd_blocksize > PAGE_SIZE || rd_blocksize < 512 ||
335 (rd_blocksize & (rd_blocksize-1))) {
336 printk("RAMDISK: wrong blocksize %d, reverting to defaults\n",
338 rd_blocksize = BLOCK_SIZE;
341 for (i = 0; i < NUM_RAMDISKS; i++) {
342 rd_disks[i] = alloc_disk(1);
347 if (register_blkdev(RAMDISK_MAJOR, "ramdisk")) {
354 for (i = 0; i < NUM_RAMDISKS; i++) {
355 struct gendisk *disk = rd_disks[i];
357 rd_queue[i] = blk_alloc_queue(GFP_KERNEL);
361 blk_queue_make_request(rd_queue[i], &rd_make_request);
363 /* rd_size is given in kB */
364 disk->major = RAMDISK_MAJOR;
365 disk->first_minor = i;
366 disk->fops = &rd_bd_op;
367 disk->queue = rd_queue[i];
368 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
369 sprintf(disk->disk_name, "ram%d", i);
370 sprintf(disk->devfs_name, "rd/%d", i);
371 set_capacity(disk, rd_size * 2);
372 add_disk(rd_disks[i]);
375 /* rd_size is given in kB */
376 printk("RAMDISK driver initialized: "
377 "%d RAM disks of %dK size %d blocksize\n",
378 NUM_RAMDISKS, rd_size, rd_blocksize);
382 unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
385 put_disk(rd_disks[i]);
386 blk_cleanup_queue(rd_queue[i]);
391 module_init(rd_init);
392 module_exit(rd_cleanup);
394 /* options - nonmodular */
396 static int __init ramdisk_size(char *str)
398 rd_size = simple_strtol(str,NULL,0);
401 static int __init ramdisk_size2(char *str) /* kludge */
403 return ramdisk_size(str);
405 static int __init ramdisk_blocksize(char *str)
407 rd_blocksize = simple_strtol(str,NULL,0);
410 __setup("ramdisk=", ramdisk_size);
411 __setup("ramdisk_size=", ramdisk_size2);
412 __setup("ramdisk_blocksize=", ramdisk_blocksize);
415 /* options - modular */
416 MODULE_PARM (rd_size, "1i");
417 MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");
418 MODULE_PARM (rd_blocksize, "i");
419 MODULE_PARM_DESC(rd_blocksize, "Blocksize of each RAM disk in bytes.");
421 MODULE_LICENSE("GPL");