Btrfs: fix data enospc check overflow

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Linux Kernel Mailing List
Date: Monday, April 12, 2010 - 7:59 pm

Gitweb:     http://git.kernel.org/linus/ab6e24103cbd215e922938a4f58c75194761a60e
Commit:     ab6e24103cbd215e922938a4f58c75194761a60e
Parent:     109f6aef5fc436f355ad027f4d97bd696df2049a
Author:     Josef Bacik <josef@redhat.com>
AuthorDate: Fri Mar 19 14:38:13 2010 +0000
Committer:  Chris Mason <chris.mason@oracle.com>
CommitDate: Mon Apr 5 16:04:50 2010 -0400

    Btrfs: fix data enospc check overflow
    
    Because we account for reserved space we get from the allocator before we
    actually account for allocating delalloc space, we can have a small window where
    the amount of "used" space in a space_info is more than the total amount of
    space in the space_info.  This will cause a overflow in our check, so it will
    seem like we have _tons_ of free space, and we'll allow reservations to occur
    that will end up larger than the amount of space we have.  I've seen users
    report ENOSPC panic's in cow_file_range a few times recently, so I tried to
    reproduce this problem and found I could reproduce it if I ran one of my tests
    in a loop for like 20 minutes.  With this patch my test ran all night without
    issues.  Thanks,
    
    Signed-off-by: Josef Bacik <josef@redhat.com>
    Signed-off-by: Chris Mason <chris.mason@oracle.com>
---
 fs/btrfs/extent-tree.c |   20 +++++++++++++++-----
 1 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 101041d..786899d 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -3234,7 +3234,8 @@ int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
 				u64 bytes)
 {
 	struct btrfs_space_info *data_sinfo;
-	int ret = 0, committed = 0;
+	u64 used;
+	int ret = 0, committed = 0, flushed = 0;
 
 	/* make sure bytes are sectorsize aligned */
 	bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
@@ -3246,12 +3247,21 @@ int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
 again:
 	/* make sure we have enough space to handle the data first */
 	spin_lock(&data_sinfo->lock);
-	if (data_sinfo->total_bytes - data_sinfo->bytes_used -
-	    data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
-	    data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
-	    data_sinfo->bytes_may_use - data_sinfo->bytes_super < bytes) {
+	used = data_sinfo->bytes_used + data_sinfo->bytes_delalloc +
+		data_sinfo->bytes_reserved + data_sinfo->bytes_pinned +
+		data_sinfo->bytes_readonly + data_sinfo->bytes_may_use +
+		data_sinfo->bytes_super;
+
+	if (used + bytes > data_sinfo->total_bytes) {
 		struct btrfs_trans_handle *trans;
 
+		if (!flushed) {
+			spin_unlock(&data_sinfo->lock);
+			flush_delalloc(root, data_sinfo);
+			flushed = 1;
+			goto again;
+		}
+
 		/*
 		 * if we don't have enough free bytes in this space then we need
 		 * to alloc a new chunk.
--
To unsubscribe from this list: send the line "unsubscribe git-commits-head" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Btrfs: fix data enospc check overflow, Linux Kernel Mailing ..., (Mon Apr 12, 7:59 pm)