rd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 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. /*
  178. * releasepage is called by pagevec_strip/try_to_release_page if
  179. * buffers_heads_over_limit is true. Without a releasepage function
  180. * try_to_free_buffers is called instead. That can unset the dirty
  181. * bit of our ram disk pages, which will be eventually freed, even
  182. * if the page is still in use.
  183. */
  184. static int ramdisk_releasepage(struct page *page, gfp_t dummy)
  185. {
  186. return 0;
  187. }
  188. static const struct address_space_operations ramdisk_aops = {
  189. .readpage = ramdisk_readpage,
  190. .prepare_write = ramdisk_prepare_write,
  191. .commit_write = ramdisk_commit_write,
  192. .writepage = ramdisk_writepage,
  193. .set_page_dirty = ramdisk_set_page_dirty,
  194. .writepages = ramdisk_writepages,
  195. .releasepage = ramdisk_releasepage,
  196. };
  197. static int rd_blkdev_pagecache_IO(int rw, struct bio_vec *vec, sector_t sector,
  198. struct address_space *mapping)
  199. {
  200. pgoff_t index = sector >> (PAGE_CACHE_SHIFT - 9);
  201. unsigned int vec_offset = vec->bv_offset;
  202. int offset = (sector << 9) & ~PAGE_CACHE_MASK;
  203. int size = vec->bv_len;
  204. int err = 0;
  205. do {
  206. int count;
  207. struct page *page;
  208. char *src;
  209. char *dst;
  210. count = PAGE_CACHE_SIZE - offset;
  211. if (count > size)
  212. count = size;
  213. size -= count;
  214. page = grab_cache_page(mapping, index);
  215. if (!page) {
  216. err = -ENOMEM;
  217. goto out;
  218. }
  219. if (!PageUptodate(page))
  220. make_page_uptodate(page);
  221. index++;
  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. memcpy(dst, src, count);
  232. kunmap_atomic(src, KM_USER0);
  233. kunmap_atomic(dst, KM_USER1);
  234. if (rw == READ)
  235. flush_dcache_page(vec->bv_page);
  236. else
  237. set_page_dirty(page);
  238. unlock_page(page);
  239. put_page(page);
  240. } while (size);
  241. out:
  242. return err;
  243. }
  244. /*
  245. * Basically, my strategy here is to set up a buffer-head which can't be
  246. * deleted, and make that my Ramdisk. If the request is outside of the
  247. * allocated size, we must get rid of it...
  248. *
  249. * 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Added devfs support
  250. *
  251. */
  252. static int rd_make_request(struct request_queue *q, struct bio *bio)
  253. {
  254. struct block_device *bdev = bio->bi_bdev;
  255. struct address_space * mapping = bdev->bd_inode->i_mapping;
  256. sector_t sector = bio->bi_sector;
  257. unsigned long len = bio->bi_size >> 9;
  258. int rw = bio_data_dir(bio);
  259. struct bio_vec *bvec;
  260. int ret = 0, i;
  261. if (sector + len > get_capacity(bdev->bd_disk))
  262. goto fail;
  263. if (rw==READA)
  264. rw=READ;
  265. bio_for_each_segment(bvec, bio, i) {
  266. ret |= rd_blkdev_pagecache_IO(rw, bvec, sector, mapping);
  267. sector += bvec->bv_len >> 9;
  268. }
  269. if (ret)
  270. goto fail;
  271. bio_endio(bio, 0);
  272. return 0;
  273. fail:
  274. bio_io_error(bio);
  275. return 0;
  276. }
  277. static int rd_ioctl(struct inode *inode, struct file *file,
  278. unsigned int cmd, unsigned long arg)
  279. {
  280. int error;
  281. struct block_device *bdev = inode->i_bdev;
  282. if (cmd != BLKFLSBUF)
  283. return -ENOTTY;
  284. /*
  285. * special: we want to release the ramdisk memory, it's not like with
  286. * the other blockdevices where this ioctl only flushes away the buffer
  287. * cache
  288. */
  289. error = -EBUSY;
  290. mutex_lock(&bdev->bd_mutex);
  291. if (bdev->bd_openers <= 2) {
  292. truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
  293. error = 0;
  294. }
  295. mutex_unlock(&bdev->bd_mutex);
  296. return error;
  297. }
  298. /*
  299. * This is the backing_dev_info for the blockdev inode itself. It doesn't need
  300. * writeback and it does not contribute to dirty memory accounting.
  301. */
  302. static struct backing_dev_info rd_backing_dev_info = {
  303. .ra_pages = 0, /* No readahead */
  304. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK | BDI_CAP_MAP_COPY,
  305. .unplug_io_fn = default_unplug_io_fn,
  306. };
  307. /*
  308. * This is the backing_dev_info for the files which live atop the ramdisk
  309. * "device". These files do need writeback and they do contribute to dirty
  310. * memory accounting.
  311. */
  312. static struct backing_dev_info rd_file_backing_dev_info = {
  313. .ra_pages = 0, /* No readahead */
  314. .capabilities = BDI_CAP_MAP_COPY, /* Does contribute to dirty memory */
  315. .unplug_io_fn = default_unplug_io_fn,
  316. };
  317. static int rd_open(struct inode *inode, struct file *filp)
  318. {
  319. unsigned unit = iminor(inode);
  320. if (rd_bdev[unit] == NULL) {
  321. struct block_device *bdev = inode->i_bdev;
  322. struct address_space *mapping;
  323. unsigned bsize;
  324. gfp_t gfp_mask;
  325. inode = igrab(bdev->bd_inode);
  326. rd_bdev[unit] = bdev;
  327. bdev->bd_openers++;
  328. bsize = bdev_hardsect_size(bdev);
  329. bdev->bd_block_size = bsize;
  330. inode->i_blkbits = blksize_bits(bsize);
  331. inode->i_size = get_capacity(bdev->bd_disk)<<9;
  332. mapping = inode->i_mapping;
  333. mapping->a_ops = &ramdisk_aops;
  334. mapping->backing_dev_info = &rd_backing_dev_info;
  335. bdev->bd_inode_backing_dev_info = &rd_file_backing_dev_info;
  336. /*
  337. * Deep badness. rd_blkdev_pagecache_IO() needs to allocate
  338. * pagecache pages within a request_fn. We cannot recur back
  339. * into the filesystem which is mounted atop the ramdisk, because
  340. * that would deadlock on fs locks. And we really don't want
  341. * to reenter rd_blkdev_pagecache_IO when we're already within
  342. * that function.
  343. *
  344. * So we turn off __GFP_FS and __GFP_IO.
  345. *
  346. * And to give this thing a hope of working, turn on __GFP_HIGH.
  347. * Hopefully, there's enough regular memory allocation going on
  348. * for the page allocator emergency pools to keep the ramdisk
  349. * driver happy.
  350. */
  351. gfp_mask = mapping_gfp_mask(mapping);
  352. gfp_mask &= ~(__GFP_FS|__GFP_IO);
  353. gfp_mask |= __GFP_HIGH;
  354. mapping_set_gfp_mask(mapping, gfp_mask);
  355. }
  356. return 0;
  357. }
  358. static struct block_device_operations rd_bd_op = {
  359. .owner = THIS_MODULE,
  360. .open = rd_open,
  361. .ioctl = rd_ioctl,
  362. };
  363. /*
  364. * Before freeing the module, invalidate all of the protected buffers!
  365. */
  366. static void __exit rd_cleanup(void)
  367. {
  368. int i;
  369. for (i = 0; i < CONFIG_BLK_DEV_RAM_COUNT; i++) {
  370. struct block_device *bdev = rd_bdev[i];
  371. rd_bdev[i] = NULL;
  372. if (bdev) {
  373. invalidate_bdev(bdev);
  374. blkdev_put(bdev);
  375. }
  376. del_gendisk(rd_disks[i]);
  377. put_disk(rd_disks[i]);
  378. blk_cleanup_queue(rd_queue[i]);
  379. }
  380. unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
  381. bdi_destroy(&rd_file_backing_dev_info);
  382. bdi_destroy(&rd_backing_dev_info);
  383. }
  384. /*
  385. * This is the registration and initialization section of the RAM disk driver
  386. */
  387. static int __init rd_init(void)
  388. {
  389. int i;
  390. int err;
  391. err = bdi_init(&rd_backing_dev_info);
  392. if (err)
  393. goto out2;
  394. err = bdi_init(&rd_file_backing_dev_info);
  395. if (err) {
  396. bdi_destroy(&rd_backing_dev_info);
  397. goto out2;
  398. }
  399. err = -ENOMEM;
  400. if (rd_blocksize > PAGE_SIZE || rd_blocksize < 512 ||
  401. (rd_blocksize & (rd_blocksize-1))) {
  402. printk("RAMDISK: wrong blocksize %d, reverting to defaults\n",
  403. rd_blocksize);
  404. rd_blocksize = BLOCK_SIZE;
  405. }
  406. for (i = 0; i < CONFIG_BLK_DEV_RAM_COUNT; i++) {
  407. rd_disks[i] = alloc_disk(1);
  408. if (!rd_disks[i])
  409. goto out;
  410. rd_queue[i] = blk_alloc_queue(GFP_KERNEL);
  411. if (!rd_queue[i]) {
  412. put_disk(rd_disks[i]);
  413. goto out;
  414. }
  415. }
  416. if (register_blkdev(RAMDISK_MAJOR, "ramdisk")) {
  417. err = -EIO;
  418. goto out;
  419. }
  420. for (i = 0; i < CONFIG_BLK_DEV_RAM_COUNT; i++) {
  421. struct gendisk *disk = rd_disks[i];
  422. blk_queue_make_request(rd_queue[i], &rd_make_request);
  423. blk_queue_hardsect_size(rd_queue[i], rd_blocksize);
  424. /* rd_size is given in kB */
  425. disk->major = RAMDISK_MAJOR;
  426. disk->first_minor = i;
  427. disk->fops = &rd_bd_op;
  428. disk->queue = rd_queue[i];
  429. disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
  430. sprintf(disk->disk_name, "ram%d", i);
  431. set_capacity(disk, rd_size * 2);
  432. add_disk(rd_disks[i]);
  433. }
  434. /* rd_size is given in kB */
  435. printk("RAMDISK driver initialized: "
  436. "%d RAM disks of %dK size %d blocksize\n",
  437. CONFIG_BLK_DEV_RAM_COUNT, rd_size, rd_blocksize);
  438. return 0;
  439. out:
  440. while (i--) {
  441. put_disk(rd_disks[i]);
  442. blk_cleanup_queue(rd_queue[i]);
  443. }
  444. bdi_destroy(&rd_backing_dev_info);
  445. bdi_destroy(&rd_file_backing_dev_info);
  446. out2:
  447. return err;
  448. }
  449. module_init(rd_init);
  450. module_exit(rd_cleanup);
  451. /* options - nonmodular */
  452. #ifndef MODULE
  453. static int __init ramdisk_size(char *str)
  454. {
  455. rd_size = simple_strtol(str,NULL,0);
  456. return 1;
  457. }
  458. static int __init ramdisk_blocksize(char *str)
  459. {
  460. rd_blocksize = simple_strtol(str,NULL,0);
  461. return 1;
  462. }
  463. __setup("ramdisk_size=", ramdisk_size);
  464. __setup("ramdisk_blocksize=", ramdisk_blocksize);
  465. #endif
  466. /* options - modular */
  467. module_param(rd_size, int, 0);
  468. MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");
  469. module_param(rd_blocksize, int, 0);
  470. MODULE_PARM_DESC(rd_blocksize, "Blocksize of each RAM disk in bytes.");
  471. MODULE_ALIAS_BLOCKDEV_MAJOR(RAMDISK_MAJOR);
  472. MODULE_LICENSE("GPL");