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