diff mbox

Btrfs: check crc area early in io_ctl_init

Message ID 50b84c2d.c44d320a.520e.ffffbea6@mx.google.com (mailing list archive)
State New, archived
Headers show

Commit Message

Wang Sheng-Hui Nov. 30, 2012, 6:02 a.m. UTC
From: Wang Sheng-Hui <shhuiw@gmail.com>

When use crc area, we should check if it can host the desired num of
crcs. Add the check in init stage.

And the check should be more strict: the first page has sizeof(u64)*2
cannot used for crc.

Signed-off-by: Wang Sheng-Hui <shhuiw@gmail.com>
---
 fs/btrfs/free-space-cache.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)
diff mbox

Patch

diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index 4ea66d4..058fc9b 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -286,8 +286,14 @@  static int io_ctl_init(struct io_ctl *io_ctl, struct inode *inode,
 	if (!io_ctl->pages)
 		return -ENOMEM;
 	io_ctl->root = root;
-	if (btrfs_ino(inode) != BTRFS_FREE_INO_OBJECTID)
+	if (btrfs_ino(inode) != BTRFS_FREE_INO_OBJECTID) {
 		io_ctl->check_crcs = 1;
+		if ((io_ctl.num_pages * sizeof(u32)) >
+		    (PAGE_CACHE_SIZE - sizeof(u64) * 2)) {
+			WARN_ON(1);
+			return -1;
+		}
+	}
 	return 0;
 }
 
@@ -917,7 +923,8 @@  int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
 
 	/* Make sure we can fit our crcs into the first page */
 	if (io_ctl.check_crcs &&
-	    (io_ctl.num_pages * sizeof(u32)) >= PAGE_CACHE_SIZE) {
+	    (io_ctl.num_pages * sizeof(u32)) >
+	    (PAGE_CACHE_SIZE - sizeof(u64) * 2)) {
 		WARN_ON(1);
 		goto out_nospc;
 	}