775b0d42908e2881a94602236e6bdde7c1cffee5
[linux-flexiantxendom0-3.2.10.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, unsigned offset, unsigned to)
112 {
113         if (!PageUptodate(page)) {
114                 void *kaddr = kmap_atomic(page, KM_USER0);
115
116                 memset(kaddr, 0, PAGE_CACHE_SIZE);
117                 flush_dcache_page(page);
118                 kunmap_atomic(kaddr, KM_USER0);
119                 SetPageUptodate(page);
120         }
121         SetPageDirty(page);
122         return 0;
123 }
124
125 static int ramdisk_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
126 {
127         return 0;
128 }
129
130 static struct address_space_operations ramdisk_aops = {
131         .readpage = ramdisk_readpage,
132         .prepare_write = ramdisk_prepare_write,
133         .commit_write = ramdisk_commit_write,
134 };
135
136 static int rd_blkdev_pagecache_IO(int rw, struct bio_vec *vec, sector_t sector,
137                                 struct address_space *mapping)
138 {
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;
143         int err = 0;
144
145         do {
146                 int count;
147                 struct page * page;
148                 char * src, * dst;
149                 int unlock = 0;
150
151                 count = PAGE_CACHE_SIZE - offset;
152                 if (count > size)
153                         count = size;
154                 size -= count;
155
156                 page = find_get_page(mapping, index);
157                 if (!page) {
158                         page = grab_cache_page(mapping, index);
159                         err = -ENOMEM;
160                         if (!page)
161                                 goto out;
162                         err = 0;
163
164                         if (!PageUptodate(page)) {
165                                 void *kaddr = kmap_atomic(page, KM_USER0);
166
167                                 memset(kaddr, 0, PAGE_CACHE_SIZE);
168                                 flush_dcache_page(page);
169                                 kunmap_atomic(kaddr, KM_USER0);
170                                 SetPageUptodate(page);
171                         }
172
173                         unlock = 1;
174                 }
175
176                 index++;
177
178                 if (rw == READ) {
179                         src = kmap(page) + offset;
180                         dst = kmap(vec->bv_page) + vec_offset;
181                 } else {
182                         dst = kmap(page) + offset;
183                         src = kmap(vec->bv_page) + vec_offset;
184                 }
185                 offset = 0;
186                 vec_offset += count;
187
188                 memcpy(dst, src, count);
189
190                 kunmap(page);
191                 kunmap(vec->bv_page);
192
193                 if (rw == READ) {
194                         flush_dcache_page(vec->bv_page);
195                 } else {
196                         SetPageDirty(page);
197                 }
198                 if (unlock)
199                         unlock_page(page);
200                 __free_page(page);
201         } while (size);
202
203  out:
204         return err;
205 }
206
207 /*
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...
211  *
212  * 19-JAN-1998  Richard Gooch <rgooch@atnf.csiro.au>  Added devfs support
213  *
214  */
215 static int rd_make_request(request_queue_t * q, struct bio *bio)
216 {
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;
223         int ret = 0, i;
224
225         if (sector + len > get_capacity(bdev->bd_disk))
226                 goto fail;
227
228         if (rw==READA)
229                 rw=READ;
230
231         bio_for_each_segment(bvec, bio, i) {
232                 ret |= rd_blkdev_pagecache_IO(rw, bvec, sector, mapping);
233                 sector += bvec->bv_len >> 9;
234         }
235         if (ret)
236                 goto fail;
237
238         bio_endio(bio, bio->bi_size, 0);
239         return 0;
240 fail:
241         bio_io_error(bio, bio->bi_size);
242         return 0;
243
244
245 static int rd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
246 {
247         int error;
248         struct block_device *bdev = inode->i_bdev;
249
250         if (cmd != BLKFLSBUF)
251                 return -EINVAL;
252
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. */
256         error = -EBUSY;
257         down(&bdev->bd_sem);
258         if (bdev->bd_openers <= 2) {
259                 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
260                 error = 0;
261         }
262         up(&bdev->bd_sem);
263         return error;
264 }
265
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 */
269 };
270
271 static int rd_open(struct inode * inode, struct file * filp)
272 {
273         unsigned unit = iminor(inode);
274
275         /*
276          * Immunize device against invalidate_buffers() and prune_icache().
277          */
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;
282                 bdev->bd_openers++;
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;
287         }
288
289         return 0;
290 }
291
292 static struct block_device_operations rd_bd_op = {
293         .owner =        THIS_MODULE,
294         .open =         rd_open,
295         .ioctl =        rd_ioctl,
296 };
297
298 /* Before freeing the module, invalidate all of the protected buffers! */
299 static void __exit rd_cleanup (void)
300 {
301         int i;
302
303         for (i = 0 ; i < NUM_RAMDISKS; i++) {
304                 struct block_device *bdev = rd_bdev[i];
305                 rd_bdev[i] = NULL;
306                 if (bdev) {
307                         invalidate_bdev(bdev, 1);
308                         blkdev_put(bdev, BDEV_FILE);
309                 }
310                 del_gendisk(rd_disks[i]);
311                 put_disk(rd_disks[i]);
312         }
313         devfs_remove("rd");
314         unregister_blkdev(RAMDISK_MAJOR, "ramdisk" );
315 }
316
317 /* This is the registration and initialization section of the RAM disk driver */
318 static int __init rd_init (void)
319 {
320         int i;
321         int err = -ENOMEM;
322
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",
326                        rd_blocksize);
327                 rd_blocksize = BLOCK_SIZE;
328         }
329
330         for (i = 0; i < NUM_RAMDISKS; i++) {
331                 rd_disks[i] = alloc_disk(1);
332                 if (!rd_disks[i])
333                         goto out;
334         }
335
336         if (register_blkdev(RAMDISK_MAJOR, "ramdisk")) {
337                 err = -EIO;
338                 goto out;
339         }
340
341         devfs_mk_dir("rd");
342
343         for (i = 0; i < NUM_RAMDISKS; i++) {
344                 struct gendisk *disk = rd_disks[i];
345
346                 rd_queue[i] = blk_alloc_queue(GFP_KERNEL);
347                 if (!rd_queue[i])
348                         goto out_queue;
349
350                 blk_queue_make_request(rd_queue[i], &rd_make_request);
351
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]);
361         }
362
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);
367
368         return 0;
369 out_queue:
370         unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
371 out:
372         while (i--)
373                 put_disk(rd_disks[i]);
374         return err;
375 }
376
377 module_init(rd_init);
378 module_exit(rd_cleanup);
379
380 /* options - nonmodular */
381 #ifndef MODULE
382 static int __init ramdisk_size(char *str)
383 {
384         rd_size = simple_strtol(str,NULL,0);
385         return 1;
386 }
387 static int __init ramdisk_size2(char *str)      /* kludge */
388 {
389         return ramdisk_size(str);
390 }
391 static int __init ramdisk_blocksize(char *str)
392 {
393         rd_blocksize = simple_strtol(str,NULL,0);
394         return 1;
395 }
396 __setup("ramdisk=", ramdisk_size);
397 __setup("ramdisk_size=", ramdisk_size2);
398 __setup("ramdisk_blocksize=", ramdisk_blocksize);
399 #endif
400
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.");
406
407 MODULE_LICENSE("GPL");