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, unsigned offset, unsigned to)
113 if (!PageUptodate(page)) {
114 void *kaddr = kmap_atomic(page, KM_USER0);
116 memset(kaddr, 0, PAGE_CACHE_SIZE);
117 flush_dcache_page(page);
118 kunmap_atomic(kaddr, KM_USER0);
119 SetPageUptodate(page);
125 static int ramdisk_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
130 static struct address_space_operations ramdisk_aops = {
131 .readpage = ramdisk_readpage,
132 .prepare_write = ramdisk_prepare_write,
133 .commit_write = ramdisk_commit_write,
136 static int rd_blkdev_pagecache_IO(int rw, struct bio_vec *vec, sector_t sector,
137 struct address_space *mapping)
139 unsigned long index = sector >> (PAGE_CACHE_SHIFT - 9);
140 unsigned int vec_offset = vec->bv_offset;
141 int offset = (sector << 9) & ~PAGE_CACHE_MASK;
142 int size = vec->bv_len;
151 count = PAGE_CACHE_SIZE - offset;
156 page = find_get_page(mapping, index);
158 page = grab_cache_page(mapping, index);
164 if (!PageUptodate(page)) {
165 void *kaddr = kmap_atomic(page, KM_USER0);
167 memset(kaddr, 0, PAGE_CACHE_SIZE);
168 flush_dcache_page(page);
169 kunmap_atomic(kaddr, KM_USER0);
170 SetPageUptodate(page);
179 src = kmap(page) + offset;
180 dst = kmap(vec->bv_page) + vec_offset;
182 dst = kmap(page) + offset;
183 src = kmap(vec->bv_page) + vec_offset;
188 memcpy(dst, src, count);
191 kunmap(vec->bv_page);
194 flush_dcache_page(vec->bv_page);
208 * Basically, my strategy here is to set up a buffer-head which can't be
209 * deleted, and make that my Ramdisk. If the request is outside of the
210 * allocated size, we must get rid of it...
212 * 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Added devfs support
215 static int rd_make_request(request_queue_t * q, struct bio *bio)
217 struct block_device *bdev = bio->bi_bdev;
218 struct address_space * mapping = bdev->bd_inode->i_mapping;
219 sector_t sector = bio->bi_sector;
220 unsigned long len = bio->bi_size >> 9;
221 int rw = bio_data_dir(bio);
222 struct bio_vec *bvec;
225 if (sector + len > get_capacity(bdev->bd_disk))
231 bio_for_each_segment(bvec, bio, i) {
232 ret |= rd_blkdev_pagecache_IO(rw, bvec, sector, mapping);
233 sector += bvec->bv_len >> 9;
238 bio_endio(bio, bio->bi_size, 0);
241 bio_io_error(bio, bio->bi_size);
245 static int rd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
248 struct block_device *bdev = inode->i_bdev;
250 if (cmd != BLKFLSBUF)
253 /* special: we want to release the ramdisk memory,
254 it's not like with the other blockdevices where
255 this ioctl only flushes away the buffer cache. */
258 if (bdev->bd_openers <= 2) {
259 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
266 static struct backing_dev_info rd_backing_dev_info = {
267 .ra_pages = 0, /* No readahead */
268 .memory_backed = 1, /* Does not contribute to dirty memory */
271 static int rd_open(struct inode * inode, struct file * filp)
273 unsigned unit = iminor(inode);
276 * Immunize device against invalidate_buffers() and prune_icache().
278 if (rd_bdev[unit] == NULL) {
279 struct block_device *bdev = inode->i_bdev;
280 inode = igrab(bdev->bd_inode);
281 rd_bdev[unit] = bdev;
283 bdev->bd_block_size = rd_blocksize;
284 inode->i_size = get_capacity(rd_disks[unit])<<9;
285 inode->i_mapping->a_ops = &ramdisk_aops;
286 inode->i_mapping->backing_dev_info = &rd_backing_dev_info;
292 static struct block_device_operations rd_bd_op = {
293 .owner = THIS_MODULE,
298 /* Before freeing the module, invalidate all of the protected buffers! */
299 static void __exit rd_cleanup (void)
303 for (i = 0 ; i < NUM_RAMDISKS; i++) {
304 struct block_device *bdev = rd_bdev[i];
307 invalidate_bdev(bdev, 1);
308 blkdev_put(bdev, BDEV_FILE);
310 del_gendisk(rd_disks[i]);
311 put_disk(rd_disks[i]);
314 unregister_blkdev(RAMDISK_MAJOR, "ramdisk" );
317 /* This is the registration and initialization section of the RAM disk driver */
318 static int __init rd_init (void)
323 if (rd_blocksize > PAGE_SIZE || rd_blocksize < 512 ||
324 (rd_blocksize & (rd_blocksize-1))) {
325 printk("RAMDISK: wrong blocksize %d, reverting to defaults\n",
327 rd_blocksize = BLOCK_SIZE;
330 for (i = 0; i < NUM_RAMDISKS; i++) {
331 rd_disks[i] = alloc_disk(1);
336 if (register_blkdev(RAMDISK_MAJOR, "ramdisk")) {
343 for (i = 0; i < NUM_RAMDISKS; i++) {
344 struct gendisk *disk = rd_disks[i];
346 rd_queue[i] = blk_alloc_queue(GFP_KERNEL);
350 blk_queue_make_request(rd_queue[i], &rd_make_request);
352 /* rd_size is given in kB */
353 disk->major = RAMDISK_MAJOR;
354 disk->first_minor = i;
355 disk->fops = &rd_bd_op;
356 disk->queue = rd_queue[i];
357 sprintf(disk->disk_name, "ram%d", i);
358 sprintf(disk->devfs_name, "rd/%d", i);
359 set_capacity(disk, rd_size * 2);
360 add_disk(rd_disks[i]);
363 /* rd_size is given in kB */
364 printk("RAMDISK driver initialized: "
365 "%d RAM disks of %dK size %d blocksize\n",
366 NUM_RAMDISKS, rd_size, rd_blocksize);
370 unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
373 put_disk(rd_disks[i]);
377 module_init(rd_init);
378 module_exit(rd_cleanup);
380 /* options - nonmodular */
382 static int __init ramdisk_size(char *str)
384 rd_size = simple_strtol(str,NULL,0);
387 static int __init ramdisk_size2(char *str) /* kludge */
389 return ramdisk_size(str);
391 static int __init ramdisk_blocksize(char *str)
393 rd_blocksize = simple_strtol(str,NULL,0);
396 __setup("ramdisk=", ramdisk_size);
397 __setup("ramdisk_size=", ramdisk_size2);
398 __setup("ramdisk_blocksize=", ramdisk_blocksize);
401 /* options - modular */
402 MODULE_PARM (rd_size, "1i");
403 MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");
404 MODULE_PARM (rd_blocksize, "i");
405 MODULE_PARM_DESC(rd_blocksize, "Blocksize of each RAM disk in bytes.");
407 MODULE_LICENSE("GPL");