This commit was manufactured by cvs2svn to create tag
[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/buffer_head.h>          /* for invalidate_bdev() */
57 #include <linux/backing-dev.h>
58 #include <linux/blkpg.h>
59 #include <linux/writeback.h>
60
61 #include <asm/uaccess.h>
62
63 /* The RAM disk size is now a parameter */
64 #define NUM_RAMDISKS 16         /* This cannot be overridden (yet) */
65
66 /* Various static variables go here.  Most are used only in the RAM disk code.
67  */
68
69 static struct gendisk *rd_disks[NUM_RAMDISKS];
70 static struct block_device *rd_bdev[NUM_RAMDISKS];/* Protected device data */
71 static struct request_queue *rd_queue[NUM_RAMDISKS];
72
73 /*
74  * Parameters for the boot-loading of the RAM disk.  These are set by
75  * init/main.c (from arguments to the kernel command line) or from the
76  * architecture-specific setup routine (from the stored boot sector
77  * information).
78  */
79 int rd_size = CONFIG_BLK_DEV_RAM_SIZE;          /* Size of the RAM disks */
80 /*
81  * It would be very desirable to have a soft-blocksize (that in the case
82  * of the ramdisk driver is also the hardblocksize ;) of PAGE_SIZE because
83  * doing that we'll achieve a far better MM footprint. Using a rd_blocksize of
84  * BLOCK_SIZE in the worst case we'll make PAGE_SIZE/BLOCK_SIZE buffer-pages
85  * unfreeable. With a rd_blocksize of PAGE_SIZE instead we are sure that only
86  * 1 page will be protected. Depending on the size of the ramdisk you
87  * may want to change the ramdisk blocksize to achieve a better or worse MM
88  * behaviour. The default is still BLOCK_SIZE (needed by rd_load_image that
89  * supposes the filesystem in the image uses a BLOCK_SIZE blocksize).
90  */
91 int rd_blocksize = BLOCK_SIZE;                  /* blocksize of the RAM disks */
92
93 /*
94  * Copyright (C) 2000 Linus Torvalds.
95  *               2000 Transmeta Corp.
96  * aops copied from ramfs.
97  */
98
99 /*
100  * If a ramdisk page has buffers, some may be uptodate and some may be not.
101  * To bring the page uptodate we zero out the non-uptodate buffers.  The
102  * page must be locked.
103  */
104 static void make_page_uptodate(struct page *page)
105 {
106         if (page_has_buffers(page)) {
107                 struct buffer_head *bh = page_buffers(page);
108                 struct buffer_head *head = bh;
109
110                 do {
111                         if (!buffer_uptodate(bh))
112                                 memset(bh->b_data, 0, bh->b_size);
113                 } while ((bh = bh->b_this_page) != head);
114         } else {
115                 memset(page_address(page), 0, PAGE_CACHE_SIZE);
116         }
117         flush_dcache_page(page);
118         SetPageUptodate(page);
119 }
120
121 static int ramdisk_readpage(struct file *file, struct page *page)
122 {
123         if (!PageUptodate(page))
124                 make_page_uptodate(page);
125         unlock_page(page);
126         return 0;
127 }
128
129 static int ramdisk_prepare_write(struct file *file, struct page *page,
130                                 unsigned offset, unsigned to)
131 {
132         if (!PageUptodate(page))
133                 make_page_uptodate(page);
134         return 0;
135 }
136
137 static int ramdisk_commit_write(struct file *file, struct page *page,
138                                 unsigned offset, unsigned to)
139 {
140         set_page_dirty(page);
141         return 0;
142 }
143
144 /*
145  * ->writepage to the the blockdev's mapping has to redirty the page so that the
146  * VM doesn't go and steal it.  We return WRITEPAGE_ACTIVATE so that the VM
147  * won't try to (pointlessly) write the page again for a while.
148  *
149  * Really, these pages should not be on the LRU at all.
150  */
151 static int ramdisk_writepage(struct page *page, struct writeback_control *wbc)
152 {
153         if (!PageUptodate(page))
154                 make_page_uptodate(page);
155         SetPageDirty(page);
156         if (wbc->for_reclaim)
157                 return WRITEPAGE_ACTIVATE;
158         unlock_page(page);
159         return 0;
160 }
161
162 /*
163  * This is a little speedup thing: short-circuit attempts to write back the
164  * ramdisk blockdev inode to its non-existent backing store.
165  */
166 static int ramdisk_writepages(struct address_space *mapping,
167                                 struct writeback_control *wbc)
168 {
169         return 0;
170 }
171
172 /*
173  * ramdisk blockdev pages have their own ->set_page_dirty() because we don't
174  * want them to contribute to dirty memory accounting.
175  */
176 static int ramdisk_set_page_dirty(struct page *page)
177 {
178         SetPageDirty(page);
179         return 0;
180 }
181
182 static struct address_space_operations ramdisk_aops = {
183         .readpage       = ramdisk_readpage,
184         .prepare_write  = ramdisk_prepare_write,
185         .commit_write   = ramdisk_commit_write,
186         .writepage      = ramdisk_writepage,
187         .set_page_dirty = ramdisk_set_page_dirty,
188         .writepages     = ramdisk_writepages,
189 };
190
191 static int rd_blkdev_pagecache_IO(int rw, struct bio_vec *vec, sector_t sector,
192                                 struct address_space *mapping)
193 {
194         pgoff_t index = sector >> (PAGE_CACHE_SHIFT - 9);
195         unsigned int vec_offset = vec->bv_offset;
196         int offset = (sector << 9) & ~PAGE_CACHE_MASK;
197         int size = vec->bv_len;
198         int err = 0;
199
200         do {
201                 int count;
202                 struct page *page;
203                 char *src;
204                 char *dst;
205
206                 count = PAGE_CACHE_SIZE - offset;
207                 if (count > size)
208                         count = size;
209                 size -= count;
210
211                 page = grab_cache_page(mapping, index);
212                 if (!page) {
213                         err = -ENOMEM;
214                         goto out;
215                 }
216
217                 if (!PageUptodate(page))
218                         make_page_uptodate(page);
219
220                 index++;
221
222                 if (rw == READ) {
223                         src = kmap_atomic(page, KM_USER0) + offset;
224                         dst = kmap_atomic(vec->bv_page, KM_USER1) + vec_offset;
225                 } else {
226                         src = kmap_atomic(vec->bv_page, KM_USER0) + vec_offset;
227                         dst = kmap_atomic(page, KM_USER1) + offset;
228                 }
229                 offset = 0;
230                 vec_offset += count;
231
232                 memcpy(dst, src, count);
233
234                 kunmap_atomic(src, KM_USER0);
235                 kunmap_atomic(dst, KM_USER1);
236
237                 if (rw == READ)
238                         flush_dcache_page(vec->bv_page);
239                 else
240                         set_page_dirty(page);
241                 unlock_page(page);
242                 put_page(page);
243         } while (size);
244
245  out:
246         return err;
247 }
248
249 /*
250  *  Basically, my strategy here is to set up a buffer-head which can't be
251  *  deleted, and make that my Ramdisk.  If the request is outside of the
252  *  allocated size, we must get rid of it...
253  *
254  * 19-JAN-1998  Richard Gooch <rgooch@atnf.csiro.au>  Added devfs support
255  *
256  */
257 static int rd_make_request(request_queue_t *q, struct bio *bio)
258 {
259         struct block_device *bdev = bio->bi_bdev;
260         struct address_space * mapping = bdev->bd_inode->i_mapping;
261         sector_t sector = bio->bi_sector;
262         unsigned long len = bio->bi_size >> 9;
263         int rw = bio_data_dir(bio);
264         struct bio_vec *bvec;
265         int ret = 0, i;
266
267         if (sector + len > get_capacity(bdev->bd_disk))
268                 goto fail;
269
270         if (rw==READA)
271                 rw=READ;
272
273         bio_for_each_segment(bvec, bio, i) {
274                 ret |= rd_blkdev_pagecache_IO(rw, bvec, sector, mapping);
275                 sector += bvec->bv_len >> 9;
276         }
277         if (ret)
278                 goto fail;
279
280         bio_endio(bio, bio->bi_size, 0);
281         return 0;
282 fail:
283         bio_io_error(bio, bio->bi_size);
284         return 0;
285
286
287 static int rd_ioctl(struct inode *inode, struct file *file,
288                         unsigned int cmd, unsigned long arg)
289 {
290         int error;
291         struct block_device *bdev = inode->i_bdev;
292
293         if (cmd != BLKFLSBUF)
294                 return -ENOTTY;
295
296         /*
297          * special: we want to release the ramdisk memory, it's not like with
298          * the other blockdevices where this ioctl only flushes away the buffer
299          * cache
300          */
301         error = -EBUSY;
302         down(&bdev->bd_sem);
303         if (bdev->bd_openers <= 2) {
304                 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
305                 error = 0;
306         }
307         up(&bdev->bd_sem);
308         return error;
309 }
310
311 /*
312  * This is the backing_dev_info for the blockdev inode itself.  It doesn't need
313  * writeback and it does not contribute to dirty memory accounting.
314  */
315 static struct backing_dev_info rd_backing_dev_info = {
316         .ra_pages       = 0,    /* No readahead */
317         .memory_backed  = 1,    /* Does not contribute to dirty memory */
318         .unplug_io_fn   = default_unplug_io_fn,
319 };
320
321 /*
322  * This is the backing_dev_info for the files which live atop the ramdisk
323  * "device".  These files do need writeback and they do contribute to dirty
324  * memory accounting.
325  */
326 static struct backing_dev_info rd_file_backing_dev_info = {
327         .ra_pages       = 0,    /* No readahead */
328         .memory_backed  = 0,    /* Does contribute to dirty memory */
329         .unplug_io_fn   = default_unplug_io_fn,
330 };
331
332 static int rd_open(struct inode *inode, struct file *filp)
333 {
334         unsigned unit = iminor(inode);
335
336         if (rd_bdev[unit] == NULL) {
337                 struct block_device *bdev = inode->i_bdev;
338                 struct address_space *mapping;
339                 int gfp_mask;
340
341                 inode = igrab(bdev->bd_inode);
342                 rd_bdev[unit] = bdev;
343                 bdev->bd_openers++;
344                 bdev->bd_block_size = rd_blocksize;
345                 inode->i_size = get_capacity(rd_disks[unit])<<9;
346                 mapping = inode->i_mapping;
347                 mapping->a_ops = &ramdisk_aops;
348                 mapping->backing_dev_info = &rd_backing_dev_info;
349                 bdev->bd_inode_backing_dev_info = &rd_file_backing_dev_info;
350
351                 /*
352                  * Deep badness.  rd_blkdev_pagecache_IO() needs to allocate
353                  * pagecache pages within a request_fn.  We cannot recur back
354                  * into the filesytem which is mounted atop the ramdisk, because
355                  * that would deadlock on fs locks.  And we really don't want
356                  * to reenter rd_blkdev_pagecache_IO when we're already within
357                  * that function.
358                  *
359                  * So we turn off __GFP_FS and __GFP_IO.
360                  *
361                  * And to give this thing a hope of working, turn on __GFP_HIGH.
362                  * Hopefully, there's enough regular memory allocation going on
363                  * for the page allocator emergency pools to keep the ramdisk
364                  * driver happy.
365                  */
366                 gfp_mask = mapping_gfp_mask(mapping);
367                 gfp_mask &= ~(__GFP_FS|__GFP_IO);
368                 gfp_mask |= __GFP_HIGH;
369                 mapping_set_gfp_mask(mapping, gfp_mask);
370         }
371
372         return 0;
373 }
374
375 static struct block_device_operations rd_bd_op = {
376         .owner =        THIS_MODULE,
377         .open =         rd_open,
378         .ioctl =        rd_ioctl,
379 };
380
381 /*
382  * Before freeing the module, invalidate all of the protected buffers!
383  */
384 static void __exit rd_cleanup(void)
385 {
386         int i;
387
388         for (i = 0; i < NUM_RAMDISKS; i++) {
389                 struct block_device *bdev = rd_bdev[i];
390                 rd_bdev[i] = NULL;
391                 if (bdev) {
392                         invalidate_bdev(bdev, 1);
393                         blkdev_put(bdev);
394                 }
395                 del_gendisk(rd_disks[i]);
396                 put_disk(rd_disks[i]);
397                 blk_cleanup_queue(rd_queue[i]);
398         }
399         devfs_remove("rd");
400         unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
401 }
402
403 /*
404  * This is the registration and initialization section of the RAM disk driver
405  */
406 static int __init rd_init(void)
407 {
408         int i;
409         int err = -ENOMEM;
410
411         if (rd_blocksize > PAGE_SIZE || rd_blocksize < 512 ||
412                         (rd_blocksize & (rd_blocksize-1))) {
413                 printk("RAMDISK: wrong blocksize %d, reverting to defaults\n",
414                        rd_blocksize);
415                 rd_blocksize = BLOCK_SIZE;
416         }
417
418         for (i = 0; i < NUM_RAMDISKS; i++) {
419                 rd_disks[i] = alloc_disk(1);
420                 if (!rd_disks[i])
421                         goto out;
422         }
423
424         if (register_blkdev(RAMDISK_MAJOR, "ramdisk")) {
425                 err = -EIO;
426                 goto out;
427         }
428
429         devfs_mk_dir("rd");
430
431         for (i = 0; i < NUM_RAMDISKS; i++) {
432                 struct gendisk *disk = rd_disks[i];
433
434                 rd_queue[i] = blk_alloc_queue(GFP_KERNEL);
435                 if (!rd_queue[i])
436                         goto out_queue;
437
438                 blk_queue_make_request(rd_queue[i], &rd_make_request);
439
440                 /* rd_size is given in kB */
441                 disk->major = RAMDISK_MAJOR;
442                 disk->first_minor = i;
443                 disk->fops = &rd_bd_op;
444                 disk->queue = rd_queue[i];
445                 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
446                 sprintf(disk->disk_name, "ram%d", i);
447                 sprintf(disk->devfs_name, "rd/%d", i);
448                 set_capacity(disk, rd_size * 2);
449                 add_disk(rd_disks[i]);
450         }
451
452         /* rd_size is given in kB */
453         printk("RAMDISK driver initialized: "
454                 "%d RAM disks of %dK size %d blocksize\n",
455                 NUM_RAMDISKS, rd_size, rd_blocksize);
456
457         return 0;
458 out_queue:
459         unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
460 out:
461         while (i--) {
462                 put_disk(rd_disks[i]);
463                 blk_cleanup_queue(rd_queue[i]);
464         }
465         return err;
466 }
467
468 module_init(rd_init);
469 module_exit(rd_cleanup);
470
471 /* options - nonmodular */
472 #ifndef MODULE
473 static int __init ramdisk_size(char *str)
474 {
475         rd_size = simple_strtol(str,NULL,0);
476         return 1;
477 }
478 static int __init ramdisk_size2(char *str)      /* kludge */
479 {
480         return ramdisk_size(str);
481 }
482 static int __init ramdisk_blocksize(char *str)
483 {
484         rd_blocksize = simple_strtol(str,NULL,0);
485         return 1;
486 }
487 __setup("ramdisk=", ramdisk_size);
488 __setup("ramdisk_size=", ramdisk_size2);
489 __setup("ramdisk_blocksize=", ramdisk_blocksize);
490 #endif
491
492 /* options - modular */
493 MODULE_PARM     (rd_size, "1i");
494 MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");
495 MODULE_PARM     (rd_blocksize, "i");
496 MODULE_PARM_DESC(rd_blocksize, "Blocksize of each RAM disk in bytes.");
497
498 MODULE_LICENSE("GPL");