ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / block / rd.c
1 /*
2  * ramdisk.c - Multiple RAM disk driver - gzip-loading version - v. 0.8 beta.
3  *
4  * (C) Chad Page, Theodore Ts'o, et. al, 1995.
5  *
6  * This RAM disk is designed to have filesystems created on it and mounted
7  * just like a regular floppy disk.
8  *
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.
12  *
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.
18  *
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.
26  *
27  * Loadable module support added by Tom Dyas.
28  *
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
33  *
34  *  Added initrd: Werner Almesberger & Hans Lermen, Feb '96
35  *
36  * 4/25/96 : Made RAM disk size a parameter (default is now 4 MB)
37  *              - Chad Page
38  *
39  * Add support for fs images split across >1 disk, Paul Gortmaker, Mar '98
40  *
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
43  */
44
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>
61
62 /* The RAM disk size is now a parameter */
63 #define NUM_RAMDISKS 16         /* This cannot be overridden (yet) */
64
65 /* Various static variables go here.  Most are used only in the RAM disk code.
66  */
67
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];
71
72 /*
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
76  * information).
77  */
78 int rd_size = CONFIG_BLK_DEV_RAM_SIZE;          /* Size of the RAM disks */
79 /*
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).
89  */
90 int rd_blocksize = BLOCK_SIZE;                  /* blocksize of the RAM disks */
91
92 /*
93  * Copyright (C) 2000 Linus Torvalds.
94  *               2000 Transmeta Corp.
95  * aops copied from ramfs.
96  */
97 static int ramdisk_readpage(struct file *file, struct page *page)
98 {
99         if (!PageUptodate(page)) {
100                 void *kaddr = kmap_atomic(page, KM_USER0);
101
102                 memset(kaddr, 0, PAGE_CACHE_SIZE);
103                 flush_dcache_page(page);
104                 kunmap_atomic(kaddr, KM_USER0);
105                 SetPageUptodate(page);
106         }
107         unlock_page(page);
108         return 0;
109 }
110
111 static int ramdisk_prepare_write(struct file *file, struct page *page,
112                                 unsigned offset, unsigned to)
113 {
114         if (!PageUptodate(page)) {
115                 void *kaddr = kmap_atomic(page, KM_USER0);
116
117                 memset(kaddr, 0, PAGE_CACHE_SIZE);
118                 flush_dcache_page(page);
119                 kunmap_atomic(kaddr, KM_USER0);
120                 SetPageUptodate(page);
121         }
122         SetPageDirty(page);
123         return 0;
124 }
125
126 static int ramdisk_commit_write(struct file *file, struct page *page,
127                                 unsigned offset, unsigned to)
128 {
129         return 0;
130 }
131
132 static struct address_space_operations ramdisk_aops = {
133         .readpage = ramdisk_readpage,
134         .prepare_write = ramdisk_prepare_write,
135         .commit_write = ramdisk_commit_write,
136 };
137
138 static int rd_blkdev_pagecache_IO(int rw, struct bio_vec *vec, sector_t sector,
139                                 struct address_space *mapping)
140 {
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;
145         int err = 0;
146
147         do {
148                 int count;
149                 struct page * page;
150                 char * src, * dst;
151                 int unlock = 0;
152
153                 count = PAGE_CACHE_SIZE - offset;
154                 if (count > size)
155                         count = size;
156                 size -= count;
157
158                 page = find_get_page(mapping, index);
159                 if (!page) {
160                         page = grab_cache_page(mapping, index);
161                         err = -ENOMEM;
162                         if (!page)
163                                 goto out;
164                         err = 0;
165
166                         if (!PageUptodate(page)) {
167                                 void *kaddr = kmap_atomic(page, KM_USER0);
168
169                                 memset(kaddr, 0, PAGE_CACHE_SIZE);
170                                 flush_dcache_page(page);
171                                 kunmap_atomic(kaddr, KM_USER0);
172                                 SetPageUptodate(page);
173                         }
174
175                         unlock = 1;
176                 }
177
178                 index++;
179
180                 if (rw == READ) {
181                         src = kmap(page) + offset;
182                         dst = kmap(vec->bv_page) + vec_offset;
183                 } else {
184                         dst = kmap(page) + offset;
185                         src = kmap(vec->bv_page) + vec_offset;
186                 }
187                 offset = 0;
188                 vec_offset += count;
189
190                 memcpy(dst, src, count);
191
192                 kunmap(page);
193                 kunmap(vec->bv_page);
194
195                 if (rw == READ) {
196                         flush_dcache_page(vec->bv_page);
197                 } else {
198                         SetPageDirty(page);
199                 }
200                 if (unlock)
201                         unlock_page(page);
202                 __free_page(page);
203         } while (size);
204
205  out:
206         return err;
207 }
208
209 /*
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...
213  *
214  * 19-JAN-1998  Richard Gooch <rgooch@atnf.csiro.au>  Added devfs support
215  *
216  */
217 static int rd_make_request(request_queue_t *q, struct bio *bio)
218 {
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;
225         int ret = 0, i;
226
227         if (sector + len > get_capacity(bdev->bd_disk))
228                 goto fail;
229
230         if (rw==READA)
231                 rw=READ;
232
233         bio_for_each_segment(bvec, bio, i) {
234                 ret |= rd_blkdev_pagecache_IO(rw, bvec, sector, mapping);
235                 sector += bvec->bv_len >> 9;
236         }
237         if (ret)
238                 goto fail;
239
240         bio_endio(bio, bio->bi_size, 0);
241         return 0;
242 fail:
243         bio_io_error(bio, bio->bi_size);
244         return 0;
245
246
247 static int rd_ioctl(struct inode *inode, struct file *file,
248                         unsigned int cmd, unsigned long arg)
249 {
250         int error;
251         struct block_device *bdev = inode->i_bdev;
252
253         if (cmd != BLKFLSBUF)
254                 return -EINVAL;
255
256         /*
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
259          * cache
260          */
261         error = -EBUSY;
262         down(&bdev->bd_sem);
263         if (bdev->bd_openers <= 2) {
264                 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
265                 error = 0;
266         }
267         up(&bdev->bd_sem);
268         return error;
269 }
270
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,
275 };
276
277 static int rd_open(struct inode *inode, struct file *filp)
278 {
279         unsigned unit = iminor(inode);
280
281         /*
282          * Immunize device against invalidate_buffers() and prune_icache().
283          */
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;
288                 bdev->bd_openers++;
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;
293         }
294
295         return 0;
296 }
297
298 static struct block_device_operations rd_bd_op = {
299         .owner =        THIS_MODULE,
300         .open =         rd_open,
301         .ioctl =        rd_ioctl,
302 };
303
304 /*
305  * Before freeing the module, invalidate all of the protected buffers!
306  */
307 static void __exit rd_cleanup(void)
308 {
309         int i;
310
311         for (i = 0; i < NUM_RAMDISKS; i++) {
312                 struct block_device *bdev = rd_bdev[i];
313                 rd_bdev[i] = NULL;
314                 if (bdev) {
315                         invalidate_bdev(bdev, 1);
316                         blkdev_put(bdev);
317                 }
318                 del_gendisk(rd_disks[i]);
319                 put_disk(rd_disks[i]);
320                 blk_cleanup_queue(rd_queue[i]);
321         }
322         devfs_remove("rd");
323         unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
324 }
325
326 /*
327  * This is the registration and initialization section of the RAM disk driver
328  */
329 static int __init rd_init(void)
330 {
331         int i;
332         int err = -ENOMEM;
333
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",
337                        rd_blocksize);
338                 rd_blocksize = BLOCK_SIZE;
339         }
340
341         for (i = 0; i < NUM_RAMDISKS; i++) {
342                 rd_disks[i] = alloc_disk(1);
343                 if (!rd_disks[i])
344                         goto out;
345         }
346
347         if (register_blkdev(RAMDISK_MAJOR, "ramdisk")) {
348                 err = -EIO;
349                 goto out;
350         }
351
352         devfs_mk_dir("rd");
353
354         for (i = 0; i < NUM_RAMDISKS; i++) {
355                 struct gendisk *disk = rd_disks[i];
356
357                 rd_queue[i] = blk_alloc_queue(GFP_KERNEL);
358                 if (!rd_queue[i])
359                         goto out_queue;
360
361                 blk_queue_make_request(rd_queue[i], &rd_make_request);
362
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]);
373         }
374
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);
379
380         return 0;
381 out_queue:
382         unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
383 out:
384         while (i--) {
385                 put_disk(rd_disks[i]);
386                 blk_cleanup_queue(rd_queue[i]);
387         }
388         return err;
389 }
390
391 module_init(rd_init);
392 module_exit(rd_cleanup);
393
394 /* options - nonmodular */
395 #ifndef MODULE
396 static int __init ramdisk_size(char *str)
397 {
398         rd_size = simple_strtol(str,NULL,0);
399         return 1;
400 }
401 static int __init ramdisk_size2(char *str)      /* kludge */
402 {
403         return ramdisk_size(str);
404 }
405 static int __init ramdisk_blocksize(char *str)
406 {
407         rd_blocksize = simple_strtol(str,NULL,0);
408         return 1;
409 }
410 __setup("ramdisk=", ramdisk_size);
411 __setup("ramdisk_size=", ramdisk_size2);
412 __setup("ramdisk_blocksize=", ramdisk_blocksize);
413 #endif
414
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.");
420
421 MODULE_LICENSE("GPL");