rd.c 15 KB

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