rd.c 14 KB

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