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 <linux/module.h>
49 #include <linux/init.h>
50 #include <linux/devfs_fs_kernel.h>
51 #include <linux/smp_lock.h>
52 #include <asm/uaccess.h>
55 * 35 has been officially registered as the RAMDISK major number, but
56 * so is the original MAJOR number of 1. We're using 1 in
57 * include/linux/major.h for now
59 #define MAJOR_NR RAMDISK_MAJOR
60 #include <linux/blk.h>
61 #include <linux/blkpg.h>
63 /* The RAM disk size is now a parameter */
64 #define NUM_RAMDISKS 16 /* This cannot be overridden (yet) */
66 #ifdef CONFIG_BLK_DEV_INITRD
67 static int initrd_users;
68 unsigned long initrd_start, initrd_end;
69 int initrd_below_start_ok;
72 /* Various static variables go here. Most are used only in the RAM disk code.
75 static unsigned long rd_length[NUM_RAMDISKS]; /* Size of RAM disks in bytes */
76 static int rd_hardsec[NUM_RAMDISKS]; /* Size of real blocks in bytes */
77 static int rd_blocksizes[NUM_RAMDISKS]; /* Size of 1024 byte blocks :) */
78 static int rd_kbsize[NUM_RAMDISKS]; /* Size in blocks of 1024 bytes */
79 static devfs_handle_t devfs_handle;
80 static struct block_device *rd_bdev[NUM_RAMDISKS];/* Protected device data */
83 * Parameters for the boot-loading of the RAM disk. These are set by
84 * init/main.c (from arguments to the kernel command line) or from the
85 * architecture-specific setup routine (from the stored boot sector
88 int rd_size = CONFIG_BLK_DEV_RAM_SIZE; /* Size of the RAM disks */
90 * It would be very desirable to have a soft-blocksize (that in the case
91 * of the ramdisk driver is also the hardblocksize ;) of PAGE_SIZE because
92 * doing that we'll achieve a far better MM footprint. Using a rd_blocksize of
93 * BLOCK_SIZE in the worst case we'll make PAGE_SIZE/BLOCK_SIZE buffer-pages
94 * unfreeable. With a rd_blocksize of PAGE_SIZE instead we are sure that only
95 * 1 page will be protected. Depending on the size of the ramdisk you
96 * may want to change the ramdisk blocksize to achieve a better or worse MM
97 * behaviour. The default is still BLOCK_SIZE (needed by rd_load_image that
98 * supposes the filesystem in the image uses a BLOCK_SIZE blocksize).
100 int rd_blocksize = BLOCK_SIZE; /* blocksize of the RAM disks */
103 * Copyright (C) 2000 Linus Torvalds.
104 * 2000 Transmeta Corp.
105 * aops copied from ramfs.
107 static void ramdisk_updatepage(struct page * page, int need_kmap)
109 if (!Page_Uptodate(page)) {
110 struct buffer_head *bh = page->buffers;
115 address = page_address(page);
117 struct buffer_head *tmp = bh;
119 if (!buffer_uptodate(tmp)) {
120 memset(address, 0, tmp->b_size);
121 mark_buffer_uptodate(tmp, 1);
123 address += tmp->b_size;
124 tmp = tmp->b_this_page;
127 memset(address, 0, PAGE_CACHE_SIZE);
130 flush_dcache_page(page);
131 SetPageUptodate(page);
135 static int ramdisk_readpage(struct file *file, struct page * page)
137 ramdisk_updatepage(page, 1);
142 static int ramdisk_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
144 ramdisk_updatepage(page, 0);
149 static int ramdisk_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
154 static struct address_space_operations ramdisk_aops = {
155 readpage: ramdisk_readpage,
156 writepage: fail_writepage,
157 prepare_write: ramdisk_prepare_write,
158 commit_write: ramdisk_commit_write,
161 static int rd_blkdev_pagecache_IO(int rw, struct buffer_head * sbh, int minor)
163 struct address_space * mapping;
165 int offset, size, err;
168 mapping = rd_bdev[minor]->bd_inode->i_mapping;
170 /* writing a buffer cache not uptodate must not clear it */
171 if (sbh->b_page->mapping == mapping) {
173 mark_buffer_uptodate(sbh, 1);
174 SetPageDirty(sbh->b_page);
179 index = sbh->b_rsector >> (PAGE_CACHE_SHIFT - 9);
180 offset = (sbh->b_rsector << 9) & ~PAGE_CACHE_MASK;
188 count = PAGE_CACHE_SIZE - offset;
193 page = grab_cache_page(mapping, index);
199 ramdisk_updatepage(page, 1);
214 memcpy(dst, src, count);
220 flush_dcache_page(sbh->b_page);
233 * Basically, my strategy here is to set up a buffer-head which can't be
234 * deleted, and make that my Ramdisk. If the request is outside of the
235 * allocated size, we must get rid of it...
237 * 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Added devfs support
240 static int rd_make_request(request_queue_t * q, int rw, struct buffer_head *sbh)
243 unsigned long offset, len;
245 minor = MINOR(sbh->b_rdev);
247 if (minor >= NUM_RAMDISKS)
251 offset = sbh->b_rsector << 9;
254 if ((offset + len) > rd_length[minor])
259 if ((rw != READ) && (rw != WRITE)) {
260 printk(KERN_INFO "RAMDISK: bad command: %d\n", rw);
264 if (rd_blkdev_pagecache_IO(rw, sbh, minor))
267 sbh->b_end_io(sbh,1);
270 buffer_IO_error(sbh);
274 static int rd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
279 if (!inode || !inode->i_rdev)
282 minor = MINOR(inode->i_rdev);
286 if (!capable(CAP_SYS_ADMIN))
288 /* special: we want to release the ramdisk memory,
289 it's not like with the other blockdevices where
290 this ioctl only flushes away the buffer cache. */
292 down(&inode->i_bdev->bd_sem);
293 if (inode->i_bdev->bd_openers <= 2) {
294 truncate_inode_pages(inode->i_mapping, 0);
297 up(&inode->i_bdev->bd_sem);
298 invalidate_buffers(inode->i_rdev);
300 case BLKGETSIZE: /* Return device size */
303 error = put_user(rd_kbsize[minor] << 1, (unsigned long *) arg);
306 error = put_user((u64)rd_kbsize[minor]<<10, (u64*)arg);
311 error = blk_ioctl(inode->i_rdev, cmd, arg);
318 #ifdef CONFIG_BLK_DEV_INITRD
320 static ssize_t initrd_read(struct file *file, char *buf,
321 size_t count, loff_t *ppos)
325 left = initrd_end - initrd_start - *ppos;
326 if (count > left) count = left;
327 if (count == 0) return 0;
328 if (copy_to_user(buf, (char *)initrd_start + *ppos, count))
335 static int initrd_release(struct inode *inode,struct file *file)
337 extern void free_initrd_mem(unsigned long, unsigned long);
340 if (!--initrd_users) {
341 free_initrd_mem(initrd_start, initrd_end);
345 blkdev_put(inode->i_bdev, BDEV_FILE);
350 static struct file_operations initrd_fops = {
352 release: initrd_release,
358 static int rd_open(struct inode * inode, struct file * filp)
360 int unit = DEVICE_NR(inode->i_rdev);
362 #ifdef CONFIG_BLK_DEV_INITRD
363 if (unit == INITRD_MINOR) {
364 if (!initrd_start) return -ENODEV;
366 filp->f_op = &initrd_fops;
371 if (unit >= NUM_RAMDISKS)
375 * Immunize device against invalidate_buffers() and prune_icache().
377 if (rd_bdev[unit] == NULL) {
378 rd_bdev[unit] = bdget(kdev_t_to_nr(inode->i_rdev));
379 rd_bdev[unit]->bd_openers++;
380 rd_bdev[unit]->bd_inode->i_mapping->a_ops = &ramdisk_aops;
386 static struct block_device_operations rd_bd_op = {
392 /* Before freeing the module, invalidate all of the protected buffers! */
393 static void __exit rd_cleanup (void)
397 for (i = 0 ; i < NUM_RAMDISKS; i++) {
398 struct block_device *bdev = rd_bdev[i];
401 blkdev_put(bdev, BDEV_FILE);
402 destroy_buffers(MKDEV(MAJOR_NR, i));
405 devfs_unregister (devfs_handle);
406 unregister_blkdev( MAJOR_NR, "ramdisk" );
407 hardsect_size[MAJOR_NR] = NULL;
408 blksize_size[MAJOR_NR] = NULL;
409 blk_size[MAJOR_NR] = NULL;
412 /* This is the registration and initialization section of the RAM disk driver */
413 static int __init rd_init (void)
417 if (rd_blocksize > PAGE_SIZE || rd_blocksize < 512 ||
418 (rd_blocksize & (rd_blocksize-1)))
420 printk("RAMDISK: wrong blocksize %d, reverting to defaults\n",
422 rd_blocksize = BLOCK_SIZE;
425 if (register_blkdev(MAJOR_NR, "ramdisk", &rd_bd_op)) {
426 printk("RAMDISK: Could not get major %d", MAJOR_NR);
430 blk_queue_make_request(BLK_DEFAULT_QUEUE(MAJOR_NR), &rd_make_request);
432 for (i = 0; i < NUM_RAMDISKS; i++) {
433 /* rd_size is given in kB */
434 rd_length[i] = rd_size << 10;
435 rd_hardsec[i] = rd_blocksize;
436 rd_blocksizes[i] = rd_blocksize;
437 rd_kbsize[i] = rd_size;
439 devfs_handle = devfs_mk_dir (NULL, "rd", NULL);
440 devfs_register_series (devfs_handle, "%u", NUM_RAMDISKS,
441 DEVFS_FL_DEFAULT, MAJOR_NR, 0,
442 S_IFBLK | S_IRUSR | S_IWUSR,
445 for (i = 0; i < NUM_RAMDISKS; i++)
446 register_disk(NULL, MKDEV(MAJOR_NR,i), 1, &rd_bd_op, rd_size<<1);
448 #ifdef CONFIG_BLK_DEV_INITRD
449 /* We ought to separate initrd operations here */
450 register_disk(NULL, MKDEV(MAJOR_NR,INITRD_MINOR), 1, &rd_bd_op, rd_size<<1);
451 devfs_register(devfs_handle, "initrd", DEVFS_FL_DEFAULT, MAJOR_NR,
452 INITRD_MINOR, S_IFBLK | S_IRUSR, &rd_bd_op, NULL);
455 hardsect_size[MAJOR_NR] = rd_hardsec; /* Size of the RAM disk blocks */
456 blksize_size[MAJOR_NR] = rd_blocksizes; /* Avoid set_blocksize() check */
457 blk_size[MAJOR_NR] = rd_kbsize; /* Size of the RAM disk in kB */
459 /* rd_size is given in kB */
460 printk("RAMDISK driver initialized: "
461 "%d RAM disks of %dK size %d blocksize\n",
462 NUM_RAMDISKS, rd_size, rd_blocksize);
467 module_init(rd_init);
468 module_exit(rd_cleanup);
470 /* options - nonmodular */
472 static int __init ramdisk_size(char *str)
474 rd_size = simple_strtol(str,NULL,0);
477 static int __init ramdisk_size2(char *str) /* kludge */
479 return ramdisk_size(str);
481 static int __init ramdisk_blocksize(char *str)
483 rd_blocksize = simple_strtol(str,NULL,0);
486 __setup("ramdisk=", ramdisk_size);
487 __setup("ramdisk_size=", ramdisk_size2);
488 __setup("ramdisk_blocksize=", ramdisk_blocksize);
491 /* options - modular */
492 MODULE_PARM (rd_size, "1i");
493 MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");
494 MODULE_PARM (rd_blocksize, "i");
495 MODULE_PARM_DESC(rd_blocksize, "Blocksize of each RAM disk in bytes.");
497 MODULE_LICENSE("GPL");