Skip to contentSkip to conversation

Fix some fiemap problems

PATCH Applied Lukas Czerner 2 participants 5 messages Nov 22, 2012 – Nov 28, 2012 ext4 file system

lore
Share & tools
lore
#0 lore raw

v5 cover Fix some fiemap problems

This patchset fixes mainly two problems in fiemap in ext4. First of all we could hit BUG_ON() if the extent tree is changed while we are traversing it. This particular problem has been revealed by xfstest 225 on s390x architecture.

The other problem is that we potentially might miss some delayed extents with fiemap if they are written after we've searched the extent tree and before we're going to search for delayed extent in extent status tree.

Both problems are fixed by the first patch of the patch set. The second contains just some cleanups.

This has been based on top of the ext4 git tree containing extent status tree. It has been tested with xfstests on s390x.

Please review carefully since fiemap bugs might cause problems with cp possibly corrupting user data.

Thanks!
-Lukas

---
v5: Change check for existing delayed extent. Update comments and description

[PATCH 1/2 v5] ext4: Prevent race while walking extent tree [PATCH 2/2 v5] ext4: simple cleanup in fiemap codepath

fs/ext4/ext4_extents.h | 14 ----
fs/ext4/extents.c | 156 ++++++++++++++++++++++++++---------------------

2 files changed, 86 insertions(+), 84 deletions(-)
#3 lore raw

PATCH v5 2/2 ext4: simple cleanup in fiemap codepath

This commit is simple cleanup of fiemap codepath which has not been included in previous commit to make the changes clearer. In this commit we rename cbex variable to newex in ext4_fill_fiemap_extents() because callback is no longer present

---
v5: update description

 fs/ext4/extents.c |   32 ++++++++++++++++----------------
 1 files changed, 16 insertions(+), 16 deletions(-)
fs/ext4/extents.c +16 -16
index fbe7dc2..5625146 100644
@@ -1967,7 +1967,7 @@ static int ext4_fill_fiemap_extents(struct inode *inode,
 				    struct fiemap_extent_info *fieinfo)
 {
 	struct ext4_ext_path *path = NULL;
-	struct ext4_ext_cache cbex;
+	struct ext4_ext_cache newex;
 	struct ext4_extent *ex;
 	ext4_lblk_t next, next_del, start = 0, end = 0;
 	ext4_lblk_t last = block + num;
@@ -2042,31 +2042,31 @@ static int ext4_fill_fiemap_extents(struct inode *inode,
 		BUG_ON(end <= start);
 
 		if (!exists) {
-			cbex.ec_block = start;
-			cbex.ec_len = end - start;
-			cbex.ec_start = 0;
+			newex.ec_block = start;
+			newex.ec_len = end - start;
+			newex.ec_start = 0;
 		} else {
-			cbex.ec_block = le32_to_cpu(ex->ee_block);
-			cbex.ec_len = ext4_ext_get_actual_len(ex);
-			cbex.ec_start = ext4_ext_pblock(ex);
+			newex.ec_block = le32_to_cpu(ex->ee_block);
+			newex.ec_len = ext4_ext_get_actual_len(ex);
+			newex.ec_start = ext4_ext_pblock(ex);
 			if (ext4_ext_is_uninitialized(ex))
 				flags |= FIEMAP_EXTENT_UNWRITTEN;
 		}
 
 		/*
-		 * Find delayed extent and update cbex accordingly. We call
-		 * it even in !exists case to find out whether cbex is the
+		 * Find delayed extent and update newex accordingly. We call
+		 * it even in !exists case to find out whether newex is the
 		 * last existing extent or not.
 		 */
-		next_del = ext4_find_delayed_extent(inode, &cbex);
+		next_del = ext4_find_delayed_extent(inode, &newex);
 		if (!exists && next_del) {
 			exists = 1;
 			flags |= FIEMAP_EXTENT_DELALLOC;
 		}
 		up_read(&EXT4_I(inode)->i_data_sem);
 
-		if (unlikely(cbex.ec_len == 0)) {
-			EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
+		if (unlikely(newex.ec_len == 0)) {
+			EXT4_ERROR_INODE(inode, "newex.ec_len == 0");
 			err = -EIO;
 			break;
 		}
@@ -2087,9 +2087,9 @@ static int ext4_fill_fiemap_extents(struct inode *inode,
 
 		if (exists) {
 			err = fiemap_fill_next_extent(fieinfo,
-				(__u64)cbex.ec_block << blksize_bits,
-				(__u64)cbex.ec_start << blksize_bits,
-				(__u64)cbex.ec_len << blksize_bits,
+				(__u64)newex.ec_block << blksize_bits,
+				(__u64)newex.ec_start << blksize_bits,
+				(__u64)newex.ec_len << blksize_bits,
 				flags);
 			if (err < 0)
 				break;
@@ -2099,7 +2099,7 @@ static int ext4_fill_fiemap_extents(struct inode *inode,
 			}
 		}
 
-		block = cbex.ec_block + cbex.ec_len;
+		block = newex.ec_block + newex.ec_len;
 	}
 
 	if (path) {
Signature · 1 line
1.7.7.6
#1 lore raw

PATCH v5 1/2 ext4: Prevent race while walking extent tree

In reply to Lukas Czerner

Currently ext4_ext_walk_space() only takes i_data_sem for read when searching for the extent at given block with ext4_ext_find_extent(). Then it drops the lock and the extent tree can be changed at will. However later on we're searching for the 'next' extent, but the extent tree might already have changed, so the information might not be accurate.

In fact we can hit BUG_ON(end <= start) if the extent got inserted into the tree after the one we found and before the block we were searching for. This has been reproduced by running xfstests 225 in loop on s390x architecture, but theoretically we could hit this on any other architecture as well, but probably not as often.

Moreover the extent currently in delayed allocation might be allocated after we search the extent tree and before we search extent status tree delayed buffers resulting in those delayed buffers being completely missed, even though completely written and allocated.

We fix all those problems in several steps:

1. remove unnecessary callback indirection
2. rename functions

        ext4_ext_walk_space -> ext4_fill_fiemap_extents
        ext4_ext_fiemap_cb -> ext4_find_delayed_extent
 3. move fiemap_fill_next_extent() into ext4_fill_fiemap_extents()
 4. hold the i_data_sem for:
        ext4_ext_find_extent()
        ext4_ext_next_allocated_block()
        ext4_find_delayed_extent()

5. call fiemap_fill_next_extent after releasing the i_data_sem
6. move path reinitialization into the critical section.

---
v4: Reworked
v5: Change check for existing delayed extent. Update comments

 fs/ext4/ext4_extents.h |   14 -----
 fs/ext4/extents.c      |  136 +++++++++++++++++++++++++++---------------------
 2 files changed, 76 insertions(+), 74 deletions(-)
fs/ext4/ext4_extents.h +0 -14
index 603bb11..173b6c5 100644
@@ -144,20 +144,6 @@ struct ext4_ext_path {
  */
 
 /*
- * to be called by ext4_ext_walk_space()
- * negative retcode - error
- * positive retcode - signal for ext4_ext_walk_space(), see below
- * callback must return valid extent (passed or newly created)
- */
-typedef int (*ext_prepare_callback)(struct inode *, ext4_lblk_t,
-					struct ext4_ext_cache *,
-					struct ext4_extent *, void *);
-
-#define EXT_CONTINUE   0
-#define EXT_BREAK      1
-#define EXT_REPEAT     2
-
-/*
  * Maximum number of logical blocks in a file; ext4_extent's ee_block is
  * __le32.
  */
fs/ext4/extents.c +76 -60

232 lines not on this page · raw message

Signature · 1 line
1.7.7.6
maintainer Applied
#2 lore raw

Re: [PATCH 1/2 v5] ext4: Prevent race while walking extent tree

Thanks, applied.

- Ted