[PATCH] rd: Use a private inode for backing storage

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Eric W. Biederman
Date: Friday, October 19, 2007 - 3:51 pm

Currently the ramdisk tries to keep the block device page cache pages
from being marked clean and dropped from memory.  That fails for
filesystems that use the buffer cache because the buffer cache is not
an ordinary buffer cache user and depends on the generic block device
address space operations being used. 

To fix all of those associated problems this patch allocates a private
inode to store the ramdisk pages in.

The result is slightly more memory used for metadata, an extra copying
when reading or writing directly to the block device, and changing the
software block size does not loose the contents of the ramdisk.  Most
of all this ensures we don't loose data during normal use of the
ramdisk.

I deliberately avoid the cleanup that is now possible because this
patch is intended to be a bug fix.

Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
---
 drivers/block/rd.c |   19 ++++++++++++++++---
 1 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/drivers/block/rd.c b/drivers/block/rd.c
index 08176d2..a52f153 100644
--- a/drivers/block/rd.c
+++ b/drivers/block/rd.c
@@ -62,6 +62,7 @@
 /* Various static variables go here.  Most are used only in the RAM disk code.
  */
 
+static struct inode *rd_inode[CONFIG_BLK_DEV_RAM_COUNT];
 static struct gendisk *rd_disks[CONFIG_BLK_DEV_RAM_COUNT];
 static struct block_device *rd_bdev[CONFIG_BLK_DEV_RAM_COUNT];/* Protected device data */
 static struct request_queue *rd_queue[CONFIG_BLK_DEV_RAM_COUNT];
@@ -267,7 +268,7 @@ static int rd_blkdev_pagecache_IO(int rw, struct bio_vec *vec, sector_t sector,
 static int rd_make_request(struct request_queue *q, struct bio *bio)
 {
 	struct block_device *bdev = bio->bi_bdev;
-	struct address_space * mapping = bdev->bd_inode->i_mapping;
+	struct address_space * mapping = rd_inode[MINOR(bdev->bd_dev)]->i_mapping;
 	sector_t sector = bio->bi_sector;
 	unsigned long len = bio->bi_size >> 9;
 	int rw = bio_data_dir(bio);
@@ -312,6 +313,7 @@ static int rd_ioctl(struct inode *inode, struct file *file,
 	mutex_lock(&bdev->bd_mutex);
 	if (bdev->bd_openers <= 2) {
 		truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
+		truncate_inode_pages(rd_inode[iminor(inode)]->i_mapping, 0);
 		error = 0;
 	}
 	mutex_unlock(&bdev->bd_mutex);
@@ -344,20 +346,30 @@ static int rd_open(struct inode *inode, struct file *filp)
 	unsigned unit = iminor(inode);
 
 	if (rd_bdev[unit] == NULL) {
+		struct inode *ramdisk_inode;
 		struct block_device *bdev = inode->i_bdev;
 		struct address_space *mapping;
 		unsigned bsize;
 		gfp_t gfp_mask;
 
+		ramdisk_inode = new_inode(bdev->bd_inode->i_sb);
+		if (!ramdisk_inode)
+			return -ENOMEM;
+
 		inode = igrab(bdev->bd_inode);
 		rd_bdev[unit] = bdev;
+		rd_inode[unit] = ramdisk_inode;
 		bdev->bd_openers++;
 		bsize = bdev_hardsect_size(bdev);
 		bdev->bd_block_size = bsize;
 		inode->i_blkbits = blksize_bits(bsize);
 		inode->i_size = get_capacity(bdev->bd_disk)<<9;
 
-		mapping = inode->i_mapping;
+		ramdisk_inode->i_mode = S_IFBLK;
+		ramdisk_inode->i_bdev = bdev;
+		ramdisk_inode->i_rdev = bdev->bd_dev;
+
+		mapping = ramdisk_inode->i_mapping;
 		mapping->a_ops = &ramdisk_aops;
 		mapping->backing_dev_info = &rd_backing_dev_info;
 		bdev->bd_inode_backing_dev_info = &rd_file_backing_dev_info;
@@ -377,7 +389,7 @@ static int rd_open(struct inode *inode, struct file *filp)
 		 * for the page allocator emergency pools to keep the ramdisk
 		 * driver happy.
 		 */
-		gfp_mask = mapping_gfp_mask(mapping);
+		gfp_mask = GFP_USER;
 		gfp_mask &= ~(__GFP_FS|__GFP_IO);
 		gfp_mask |= __GFP_HIGH;
 		mapping_set_gfp_mask(mapping, gfp_mask);
@@ -409,6 +421,7 @@ static void __exit rd_cleanup(void)
 		del_gendisk(rd_disks[i]);
 		put_disk(rd_disks[i]);
 		blk_cleanup_queue(rd_queue[i]);
+		iput(rd_inode[i]);
 	}
 	unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
 
-- 
1.5.3.rc6.17.g1911

-
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH resend] ramdisk: fix zeroed ramdisk pages on memory ..., Christian Borntraeger, (Mon Oct 15, 1:28 am)
Re: [PATCH resend] ramdisk: fix zeroed ramdisk pages on me ..., Christian Borntraeger, (Mon Oct 15, 2:05 am)
Re: [PATCH resend] ramdisk: fix zeroed ramdisk pages on me ..., Eric W. Biederman, (Mon Oct 15, 11:38 am)
Re: [PATCH resend] ramdisk: fix zeroed ramdisk pages on me ..., Eric W. Biederman, (Mon Oct 15, 3:37 pm)
[PATCH] rd: Preserve the dirty bit in init_page_buffers(), Eric W. Biederman, (Mon Oct 15, 3:40 pm)
[PATCH] rd: Mark ramdisk buffers heads dirty, Eric W. Biederman, (Mon Oct 15, 3:42 pm)
Re: [PATCH resend] ramdisk: fix zeroed ramdisk pages on me ..., Eric W. Biederman, (Mon Oct 15, 8:14 pm)
Re: [PATCH resend] ramdisk: fix zeroed ramdisk pages on me ..., Eric W. Biederman, (Mon Oct 15, 9:57 pm)
[patch][rfc] rewrite ramdisk, Nick Piggin, (Tue Oct 16, 12:47 am)
Re: [patch][rfc] rewrite ramdisk, Jan Engelhardt, (Tue Oct 16, 12:52 am)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Christian Borntraeger, (Tue Oct 16, 12:56 am)
Re: Re: [PATCH] rd: Mark ramdisk buffers heads dirty, rubenjr_22, (Tue Oct 16, 1:05 am)
Re: [patch][rfc] rewrite ramdisk, Nick Piggin, (Tue Oct 16, 1:07 am)
Re: Re: [PATCH] rd: Mark ramdisk buffers heads dirty, rubenjr_22, (Tue Oct 16, 1:13 am)
Re: [patch][rfc] rewrite ramdisk, Jan Engelhardt, (Tue Oct 16, 1:17 am)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Nick Piggin, (Tue Oct 16, 1:19 am)
Re: [patch][rfc] rewrite ramdisk, Nick Piggin, (Tue Oct 16, 1:26 am)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Christian Borntraeger, (Tue Oct 16, 1:48 am)
Re: [patch][rfc] rewrite ramdisk, Jan Engelhardt, (Tue Oct 16, 1:53 am)
Re: [patch][rfc] rewrite ramdisk, Eric W. Biederman, (Tue Oct 16, 2:08 am)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Eric W. Biederman, (Tue Oct 16, 2:22 am)
Re: [PATCH] rd: Preserve the dirty bit in init_page_buffers(), Eric W. Biederman, (Tue Oct 16, 2:35 am)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Eric W. Biederman, (Tue Oct 16, 12:06 pm)
Re: [patch][rfc] rewrite ramdisk, Theodore Tso, (Tue Oct 16, 2:28 pm)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Nick Piggin, (Tue Oct 16, 3:06 pm)
Re: [patch][rfc] rewrite ramdisk, Nick Piggin, (Tue Oct 16, 3:08 pm)
Re: [patch][rfc] rewrite ramdisk, Eric W. Biederman, (Tue Oct 16, 4:48 pm)
Re: [patch][rfc] rewrite ramdisk, Nick Piggin, (Tue Oct 16, 5:28 pm)
Re: [patch][rfc] rewrite ramdisk, Eric W. Biederman, (Tue Oct 16, 6:13 pm)
Re: [patch][rfc] rewrite ramdisk, Nick Piggin, (Tue Oct 16, 6:47 pm)
Re: [patch][rfc] rewrite ramdisk, Eric W. Biederman, (Wed Oct 17, 3:30 am)
Re: [patch][rfc] rewrite ramdisk, Nick Piggin, (Wed Oct 17, 5:49 am)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Christian Borntraeger, (Wed Oct 17, 9:14 am)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Eric W. Biederman, (Wed Oct 17, 10:57 am)
Re: [patch][rfc] rewrite ramdisk, Eric W. Biederman, (Wed Oct 17, 11:45 am)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Chris Mason, (Wed Oct 17, 12:14 pm)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Eric W. Biederman, (Wed Oct 17, 1:29 pm)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Chris Mason, (Wed Oct 17, 1:54 pm)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Eric W. Biederman, (Wed Oct 17, 2:30 pm)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Christian Borntraeger, (Wed Oct 17, 2:48 pm)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Eric W. Biederman, (Wed Oct 17, 3:22 pm)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Chris Mason, (Wed Oct 17, 3:58 pm)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Eric W. Biederman, (Wed Oct 17, 4:28 pm)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Chris Mason, (Wed Oct 17, 5:03 pm)
Re: [patch][rfc] rewrite ramdisk, Nick Piggin, (Wed Oct 17, 6:06 pm)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Eric W. Biederman, (Wed Oct 17, 8:27 pm)
[RFC][PATCH] block: Isolate the buffer cache in it's own m ..., Eric W. Biederman, (Wed Oct 17, 8:59 pm)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Christian Borntraeger, (Thu Oct 18, 2:26 am)
Re: [RFC][PATCH] block: Isolate the buffer cache in it's o ..., Eric W. Biederman, (Fri Oct 19, 2:27 pm)
Re: [RFC][PATCH] block: Isolate the buffer cache in it's o ..., Eric W. Biederman, (Fri Oct 19, 2:35 pm)
Re: [PATCH] rd: Mark ramdisk buffers heads dirty, Eric W. Biederman, (Fri Oct 19, 3:46 pm)
[PATCH] rd: Use a private inode for backing storage, Eric W. Biederman, (Fri Oct 19, 3:51 pm)
Re: [RFC][PATCH] block: Isolate the buffer cache in it's o ..., Eric W. Biederman, (Sat Oct 20, 9:53 pm)
Re: [PATCH] rd: Use a private inode for backing storage, Eric W. Biederman, (Sat Oct 20, 10:10 pm)
Re: [PATCH] rd: Use a private inode for backing storage, Nick Piggin, (Sat Oct 20, 10:24 pm)
Re: [PATCH] rd: Use a private inode for backing storage, Eric W. Biederman, (Sat Oct 20, 11:48 pm)
Re: [RFC][PATCH] block: Isolate the buffer cache in it's o ..., Eric W. Biederman, (Sun Oct 21, 12:09 am)
Re: [PATCH] rd: Use a private inode for backing storage, Christian Borntraeger, (Sun Oct 21, 12:28 am)
Re: [PATCH] rd: Use a private inode for backing storage, Eric W. Biederman, (Sun Oct 21, 1:23 am)
Re: [PATCH] rd: Use a private inode for backing storage, Eric W. Biederman, (Sun Oct 21, 10:56 am)
Re: [PATCH] rd: Use a private inode for backing storage, Eric W. Biederman, (Sun Oct 21, 11:39 am)