rd.c 14 KB

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