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>
49 #include <asm/atomic.h>
50 #include <linux/bio.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/devfs_fs_kernel.h>
54 #include <linux/pagemap.h>
55 #include <linux/blkdev.h>
56 #include <linux/genhd.h>
57 #include <linux/buffer_head.h> /* for invalidate_bdev() */
58 #include <linux/backing-dev.h>
59 #include <linux/blkpg.h>
60 #include <linux/writeback.h>
62 #include <asm/uaccess.h>
64 /* The RAM disk size is now a parameter */
65 #define NUM_RAMDISKS 16 /* This cannot be overridden (yet) */
67 /* Various static variables go here. Most are used only in the RAM disk code.
70 static struct gendisk
*rd_disks
[NUM_RAMDISKS
];
71 static struct block_device
*rd_bdev
[NUM_RAMDISKS
];/* Protected device data */
72 static struct request_queue
*rd_queue
[NUM_RAMDISKS
];
75 * Parameters for the boot-loading of the RAM disk. These are set by
76 * init/main.c (from arguments to the kernel command line) or from the
77 * architecture-specific setup routine (from the stored boot sector
80 int rd_size
= CONFIG_BLK_DEV_RAM_SIZE
; /* Size of the RAM disks */
82 * It would be very desirable to have a soft-blocksize (that in the case
83 * of the ramdisk driver is also the hardblocksize ;) of PAGE_SIZE because
84 * doing that we'll achieve a far better MM footprint. Using a rd_blocksize of
85 * BLOCK_SIZE in the worst case we'll make PAGE_SIZE/BLOCK_SIZE buffer-pages
86 * unfreeable. With a rd_blocksize of PAGE_SIZE instead we are sure that only
87 * 1 page will be protected. Depending on the size of the ramdisk you
88 * may want to change the ramdisk blocksize to achieve a better or worse MM
89 * behaviour. The default is still BLOCK_SIZE (needed by rd_load_image that
90 * supposes the filesystem in the image uses a BLOCK_SIZE blocksize).
92 int rd_blocksize
= BLOCK_SIZE
; /* blocksize of the RAM disks */
95 * Copyright (C) 2000 Linus Torvalds.
96 * 2000 Transmeta Corp.
97 * aops copied from ramfs.
101 * If a ramdisk page has buffers, some may be uptodate and some may be not.
102 * To bring the page uptodate we zero out the non-uptodate buffers. The
103 * page must be locked.
105 static void make_page_uptodate(struct page
*page
)
107 if (page_has_buffers(page
)) {
108 struct buffer_head
*bh
= page_buffers(page
);
109 struct buffer_head
*head
= bh
;
112 if (!buffer_uptodate(bh
)) {
113 memset(bh
->b_data
, 0, bh
->b_size
);
115 * akpm: I'm totally undecided about this. The
116 * buffer has just been magically brought "up to
117 * date", but nobody should want to be reading
118 * it anyway, because it hasn't been used for
119 * anything yet. It is still in a "not read
120 * from disk yet" state.
122 * But non-uptodate buffers against an uptodate
123 * page are against the rules. So do it anyway.
125 set_buffer_uptodate(bh
);
127 } while ((bh
= bh
->b_this_page
) != head
);
129 memset(page_address(page
), 0, PAGE_CACHE_SIZE
);
131 flush_dcache_page(page
);
132 SetPageUptodate(page
);
135 static int ramdisk_readpage(struct file
*file
, struct page
*page
)
137 if (!PageUptodate(page
))
138 make_page_uptodate(page
);
143 static int ramdisk_prepare_write(struct file
*file
, struct page
*page
,
144 unsigned offset
, unsigned to
)
146 if (!PageUptodate(page
))
147 make_page_uptodate(page
);
151 static int ramdisk_commit_write(struct file
*file
, struct page
*page
,
152 unsigned offset
, unsigned to
)
154 set_page_dirty(page
);
159 * ->writepage to the the blockdev's mapping has to redirty the page so that the
160 * VM doesn't go and steal it. We return WRITEPAGE_ACTIVATE so that the VM
161 * won't try to (pointlessly) write the page again for a while.
163 * Really, these pages should not be on the LRU at all.
165 static int ramdisk_writepage(struct page
*page
, struct writeback_control
*wbc
)
167 if (!PageUptodate(page
))
168 make_page_uptodate(page
);
170 if (wbc
->for_reclaim
)
171 return WRITEPAGE_ACTIVATE
;
177 * This is a little speedup thing: short-circuit attempts to write back the
178 * ramdisk blockdev inode to its non-existent backing store.
180 static int ramdisk_writepages(struct address_space
*mapping
,
181 struct writeback_control
*wbc
)
187 * ramdisk blockdev pages have their own ->set_page_dirty() because we don't
188 * want them to contribute to dirty memory accounting.
190 static int ramdisk_set_page_dirty(struct page
*page
)
196 static struct address_space_operations ramdisk_aops
= {
197 .readpage
= ramdisk_readpage
,
198 .prepare_write
= ramdisk_prepare_write
,
199 .commit_write
= ramdisk_commit_write
,
200 .writepage
= ramdisk_writepage
,
201 .set_page_dirty
= ramdisk_set_page_dirty
,
202 .writepages
= ramdisk_writepages
,
205 static int rd_blkdev_pagecache_IO(int rw
, struct bio_vec
*vec
, sector_t sector
,
206 struct address_space
*mapping
)
208 pgoff_t index
= sector
>> (PAGE_CACHE_SHIFT
- 9);
209 unsigned int vec_offset
= vec
->bv_offset
;
210 int offset
= (sector
<< 9) & ~PAGE_CACHE_MASK
;
211 int size
= vec
->bv_len
;
220 count
= PAGE_CACHE_SIZE
- offset
;
225 page
= grab_cache_page(mapping
, index
);
231 if (!PageUptodate(page
))
232 make_page_uptodate(page
);
237 src
= kmap_atomic(page
, KM_USER0
) + offset
;
238 dst
= kmap_atomic(vec
->bv_page
, KM_USER1
) + vec_offset
;
240 src
= kmap_atomic(vec
->bv_page
, KM_USER0
) + vec_offset
;
241 dst
= kmap_atomic(page
, KM_USER1
) + offset
;
246 memcpy(dst
, src
, count
);
248 kunmap_atomic(src
, KM_USER0
);
249 kunmap_atomic(dst
, KM_USER1
);
252 flush_dcache_page(vec
->bv_page
);
254 set_page_dirty(page
);
264 * Basically, my strategy here is to set up a buffer-head which can't be
265 * deleted, and make that my Ramdisk. If the request is outside of the
266 * allocated size, we must get rid of it...
268 * 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Added devfs support
271 static int rd_make_request(request_queue_t
*q
, struct bio
*bio
)
273 struct block_device
*bdev
= bio
->bi_bdev
;
274 struct address_space
* mapping
= bdev
->bd_inode
->i_mapping
;
275 sector_t sector
= bio
->bi_sector
;
276 unsigned long len
= bio
->bi_size
>> 9;
277 int rw
= bio_data_dir(bio
);
278 struct bio_vec
*bvec
;
281 if (sector
+ len
> get_capacity(bdev
->bd_disk
))
287 bio_for_each_segment(bvec
, bio
, i
) {
288 ret
|= rd_blkdev_pagecache_IO(rw
, bvec
, sector
, mapping
);
289 sector
+= bvec
->bv_len
>> 9;
294 bio_endio(bio
, bio
->bi_size
, 0);
297 bio_io_error(bio
, bio
->bi_size
);
301 static int rd_ioctl(struct inode
*inode
, struct file
*file
,
302 unsigned int cmd
, unsigned long arg
)
305 struct block_device
*bdev
= inode
->i_bdev
;
307 if (cmd
!= BLKFLSBUF
)
311 * special: we want to release the ramdisk memory, it's not like with
312 * the other blockdevices where this ioctl only flushes away the buffer
317 if (bdev
->bd_openers
<= 2) {
318 truncate_inode_pages(bdev
->bd_inode
->i_mapping
, 0);
326 * This is the backing_dev_info for the blockdev inode itself. It doesn't need
327 * writeback and it does not contribute to dirty memory accounting.
329 static struct backing_dev_info rd_backing_dev_info
= {
330 .ra_pages
= 0, /* No readahead */
331 .memory_backed
= 1, /* Does not contribute to dirty memory */
332 .unplug_io_fn
= default_unplug_io_fn
,
336 * This is the backing_dev_info for the files which live atop the ramdisk
337 * "device". These files do need writeback and they do contribute to dirty
340 static struct backing_dev_info rd_file_backing_dev_info
= {
341 .ra_pages
= 0, /* No readahead */
342 .memory_backed
= 0, /* Does contribute to dirty memory */
343 .unplug_io_fn
= default_unplug_io_fn
,
346 static int rd_open(struct inode
*inode
, struct file
*filp
)
348 unsigned unit
= iminor(inode
);
350 if (rd_bdev
[unit
] == NULL
) {
351 struct block_device
*bdev
= inode
->i_bdev
;
352 struct address_space
*mapping
;
356 inode
= igrab(bdev
->bd_inode
);
357 rd_bdev
[unit
] = bdev
;
359 bsize
= bdev_hardsect_size(bdev
);
360 bdev
->bd_block_size
= bsize
;
361 inode
->i_blkbits
= blksize_bits(bsize
);
362 inode
->i_size
= get_capacity(bdev
->bd_disk
)<<9;
364 mapping
= inode
->i_mapping
;
365 mapping
->a_ops
= &ramdisk_aops
;
366 mapping
->backing_dev_info
= &rd_backing_dev_info
;
367 bdev
->bd_inode_backing_dev_info
= &rd_file_backing_dev_info
;
370 * Deep badness. rd_blkdev_pagecache_IO() needs to allocate
371 * pagecache pages within a request_fn. We cannot recur back
372 * into the filesytem which is mounted atop the ramdisk, because
373 * that would deadlock on fs locks. And we really don't want
374 * to reenter rd_blkdev_pagecache_IO when we're already within
377 * So we turn off __GFP_FS and __GFP_IO.
379 * And to give this thing a hope of working, turn on __GFP_HIGH.
380 * Hopefully, there's enough regular memory allocation going on
381 * for the page allocator emergency pools to keep the ramdisk
384 gfp_mask
= mapping_gfp_mask(mapping
);
385 gfp_mask
&= ~(__GFP_FS
|__GFP_IO
);
386 gfp_mask
|= __GFP_HIGH
;
387 mapping_set_gfp_mask(mapping
, gfp_mask
);
393 static struct block_device_operations rd_bd_op
= {
394 .owner
= THIS_MODULE
,
400 * Before freeing the module, invalidate all of the protected buffers!
402 static void __exit
rd_cleanup(void)
406 for (i
= 0; i
< NUM_RAMDISKS
; i
++) {
407 struct block_device
*bdev
= rd_bdev
[i
];
410 invalidate_bdev(bdev
, 1);
413 del_gendisk(rd_disks
[i
]);
414 put_disk(rd_disks
[i
]);
415 blk_cleanup_queue(rd_queue
[i
]);
418 unregister_blkdev(RAMDISK_MAJOR
, "ramdisk");
422 * This is the registration and initialization section of the RAM disk driver
424 static int __init
rd_init(void)
429 if (rd_blocksize
> PAGE_SIZE
|| rd_blocksize
< 512 ||
430 (rd_blocksize
& (rd_blocksize
-1))) {
431 printk("RAMDISK: wrong blocksize %d, reverting to defaults\n",
433 rd_blocksize
= BLOCK_SIZE
;
436 for (i
= 0; i
< NUM_RAMDISKS
; i
++) {
437 rd_disks
[i
] = alloc_disk(1);
442 if (register_blkdev(RAMDISK_MAJOR
, "ramdisk")) {
449 for (i
= 0; i
< NUM_RAMDISKS
; i
++) {
450 struct gendisk
*disk
= rd_disks
[i
];
452 rd_queue
[i
] = blk_alloc_queue(GFP_KERNEL
);
456 blk_queue_make_request(rd_queue
[i
], &rd_make_request
);
457 blk_queue_hardsect_size(rd_queue
[i
], rd_blocksize
);
459 /* rd_size is given in kB */
460 disk
->major
= RAMDISK_MAJOR
;
461 disk
->first_minor
= i
;
462 disk
->fops
= &rd_bd_op
;
463 disk
->queue
= rd_queue
[i
];
464 disk
->flags
|= GENHD_FL_SUPPRESS_PARTITION_INFO
;
465 sprintf(disk
->disk_name
, "ram%d", i
);
466 sprintf(disk
->devfs_name
, "rd/%d", i
);
467 set_capacity(disk
, rd_size
* 2);
468 add_disk(rd_disks
[i
]);
471 /* rd_size is given in kB */
472 printk("RAMDISK driver initialized: "
473 "%d RAM disks of %dK size %d blocksize\n",
474 NUM_RAMDISKS
, rd_size
, rd_blocksize
);
478 unregister_blkdev(RAMDISK_MAJOR
, "ramdisk");
481 put_disk(rd_disks
[i
]);
482 blk_cleanup_queue(rd_queue
[i
]);
487 module_init(rd_init
);
488 module_exit(rd_cleanup
);
490 /* options - nonmodular */
492 static int __init
ramdisk_size(char *str
)
494 rd_size
= simple_strtol(str
,NULL
,0);
497 static int __init
ramdisk_size2(char *str
) /* kludge */
499 return ramdisk_size(str
);
501 static int __init
ramdisk_blocksize(char *str
)
503 rd_blocksize
= simple_strtol(str
,NULL
,0);
506 __setup("ramdisk=", ramdisk_size
);
507 __setup("ramdisk_size=", ramdisk_size2
);
508 __setup("ramdisk_blocksize=", ramdisk_blocksize
);
511 /* options - modular */
512 MODULE_PARM (rd_size
, "1i");
513 MODULE_PARM_DESC(rd_size
, "Size of each RAM disk in kbytes.");
514 MODULE_PARM (rd_blocksize
, "i");
515 MODULE_PARM_DESC(rd_blocksize
, "Blocksize of each RAM disk in bytes.");
517 MODULE_LICENSE("GPL");