Skip to contentSkip to conversation

ext4: truncate block allocated on a failed ext4_write_begin

PATCH New Aneesh Kumar K.V 3 participants 7 messages Aug 28, 2008 – Aug 29, 2008 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ext4: truncate block allocated on a failed ext4_write_begin

For blocksize < pagesize we need to remove blocks that got allocte in block_wirte_begin if we fail with ENOSPC for later blocks. block_write_begin internally does this if it allocated page locally. This make sure we don't have blocks outisde inode.i_size during ENOSPC

---
 fs/ext4/inode.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
fs/ext4/inode.c +7 -0
index 473d888..a8c6bc8 100644
@@ -1394,6 +1394,13 @@ static int ext4_write_begin(struct file *file, struct address_space *mapping,
  		unlock_page(page);
 		ext4_journal_stop(handle);
  		page_cache_release(page);
+		/*
+		 * block_write_begin may have instantiated a few blocks
+		 * outside i_size.  Trim these off again. Don't need
+		 * i_size_read because we hold i_mutex.
+		 */
+		if (pos + len > inode->i_size)
+			vmtruncate(inode, inode->i_size);
 	}
 
 	if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Signature · 1 line
1.6.0.1.90.g27a6e
#1 lore raw

PATCH 1/1 ext3: truncate block allocated on a failed ext3_write_begin

For blocksize < pagesize we need to remove blocks that got allocte in block_wirte_begin if we fail with ENOSPC for later blocks. block_write_begin internally does this if it allocated page locally. This make sure we don't have blocks outisde inode.i_size during ENOSPC

---
 fs/ext3/inode.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
fs/ext3/inode.c +7 -0
index ebfec4d..f8424ad 100644
@@ -1186,6 +1186,13 @@ static int ext3_write_begin(struct file *file, struct address_space *mapping,
 		ext3_journal_stop(handle);
 		unlock_page(page);
 		page_cache_release(page);
+		/*
+		 * block_write_begin may have instantiated a few blocks
+		 * outside i_size.  Trim these off again. Don't need
+		 * i_size_read because we hold i_mutex.
+		 */
+		if (pos + len > inode->i_size)
+			vmtruncate(inode, inode->i_size);
 	}
 	if (ret == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
 		goto retry;
Signature · 1 line
1.6.0.1.90.g27a6e
#2 lore raw

PATCH 1/1 ext4: Properly update i_disksize.

With delayed allocation we use i_data_sem to update i_disksize. We need to update i_disksize only if the new size specified is greater than the current value and we need to make sure we don't race with other i_disksize update. With delayed allocation we will switch to nondelalloc write_begin if we are low on free blocks. That means nondelalloc write_begin also need to use the same locking.

We also need to check and update i_disksize even if the new size is less that inode.i_size because of delayed allocation.

---
 fs/ext4/ext4.h    |   11 +++++++++
 fs/ext4/extents.c |    9 ++++---
 fs/ext4/inode.c   |   61 ++++++++++++++++++++++++++++++++--------------------
 3 files changed, 53 insertions(+), 28 deletions(-)
fs/ext4/ext4.h +11 -0
index 13c69ed..bc856e3 100644
@@ -1307,6 +1307,17 @@ do {								\
 #define EXT4_FREEBLOCKS_WATERMARK 0
 #endif
 
+static inline void ext4_update_i_disksize(struct inode *inode, loff_t newsize)
+{
+	/*
+	 * XXX: replace with spinlock if seen contended -bzzz
+	 */
+	down_write(&EXT4_I(inode)->i_data_sem);
+	if (newsize > EXT4_I(inode)->i_disksize)
+		EXT4_I(inode)->i_disksize = newsize;
+	up_write(&EXT4_I(inode)->i_data_sem);
+	return ;
+}
 
 /*
  * Inodes and files operations
fs/ext4/extents.c +5 -4
index 268e96d..7def792 100644
@@ -3018,10 +3018,11 @@ static void ext4_falloc_update_inode(struct inode *inode,
 	 * Update only when preallocation was requested beyond
 	 * the file size.
 	 */
-	if (!(mode & FALLOC_FL_KEEP_SIZE) &&
-				new_size > i_size_read(inode)) {
-		i_size_write(inode, new_size);
-		EXT4_I(inode)->i_disksize = new_size;
+	if (!(mode & FALLOC_FL_KEEP_SIZE)) {
+		if (new_size > i_size_read(inode))
+			i_size_write(inode, new_size);
+		if (new_size > EXT4_I(inode)->i_disksize)
+			ext4_update_i_disksize(inode, new_size);
 	}
 
 }
fs/ext4/inode.c +37 -24

108 lines not on this page · raw message

Signature · 1 line
1.6.0.1.90.g27a6e
Acked-by on v1 1/1
#3 lore raw

Re: [PATCH] ext3: truncate block allocated on a failed ext3_write_begin

In reply to Aneesh Kumar K.V
Quoted 6 lines · Aneesh Kumar K.V

For blocksize < pagesize we need to remove blocks that got allocte in block_wirte_begin if we fail with ENOSPC for later blocks. block_write_begin internally does this if it allocated page locally. This make sure we don't have blocks outisde inode.i_size during ENOSPC

I think this is good; here's an easy testcase:

# dd if=/dev/zero of=fsfile bs=1k count=2048
# mkfs.ext3 -F fsfile
# mkdir mnt
# mount -o loop fsfile mnt/
# dd if=/dev/zero of=mnt/1kfile bs=1k count=1
# cd mnt/
# dd if=/dev/zero of=bigfile bs=4k
# cd ..
# umount mnt
# e2fsck -f fsfile
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Inode 13, i_size is 974848, should be 976896.  Fix<y>?
...

can you test with that, unless you already have a testcase you've used?

Assuming it fixes it (it should) you can add:

-Eric

Quoted 23 lines
---
 fs/ext3/inode.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
fs/ext3/inode.c +7 -0
index ebfec4d..f8424ad 100644
@@ -1186,6 +1186,13 @@ static int ext3_write_begin(struct file *file, struct address_space *mapping,
 		ext3_journal_stop(handle);
 		unlock_page(page);
 		page_cache_release(page);
+		/*
+		 * block_write_begin may have instantiated a few blocks
+		 * outside i_size.  Trim these off again. Don't need
+		 * i_size_read because we hold i_mutex.
+		 */
+		if (pos + len > inode->i_size)
+			vmtruncate(inode, inode->i_size);
 	}
 	if (ret == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
 		goto retry;
#4 lore raw

Re: [PATCH] ext3: truncate block allocated on a failed ext3_write_begin

Quoted 25 lines · Eric Sandeen
Quoted 6 lines · Aneesh Kumar K.V

For blocksize < pagesize we need to remove blocks that got allocte in block_wirte_begin if we fail with ENOSPC for later blocks. block_write_begin internally does this if it allocated page locally. This make sure we don't have blocks outisde inode.i_size during ENOSPC

I think this is good; here's an easy testcase:

# dd if=/dev/zero of=fsfile bs=1k count=2048
# mkfs.ext3 -F fsfile
# mkdir mnt
# mount -o loop fsfile mnt/
# dd if=/dev/zero of=mnt/1kfile bs=1k count=1
# cd mnt/
# dd if=/dev/zero of=bigfile bs=4k
# cd ..
# umount mnt
# e2fsck -f fsfile
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Inode 13, i_size is 974848, should be 976896.  Fix<y>?
...
Quoted 1 line

can you test with that, unless you already have a testcase you've used?

I tested the above and it works fine. The problem appeared during fsstress run and I used fsstress to debug and fix.

Quoted 5 lines

Assuming it fixes it (it should) you can add:

-aneesh

#5 lore raw

Re: [PATCH] ext3: truncate block allocated on a failed ext3_write_begin

In reply to Aneesh Kumar K.V
Quoted 6 lines · Aneesh Kumar K.V

For blocksize < pagesize we need to remove blocks that got allocte in block_wirte_begin if we fail with ENOSPC for later blocks. block_write_begin internally does this if it allocated page locally. This make sure we don't have blocks outisde inode.i_size during ENOSPC

BTW why this check was moved from generic_XXX_write to fs speciffic code?

Quoted 31 lines
---
 fs/ext3/inode.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
fs/ext3/inode.c +7 -0
index ebfec4d..f8424ad 100644
@@ -1186,6 +1186,13 @@ static int ext3_write_begin(struct file *file, struct address_space *mapping,
 		ext3_journal_stop(handle);
 		unlock_page(page);
 		page_cache_release(page);
+		/*
+		 * block_write_begin may have instantiated a few blocks
+		 * outside i_size.  Trim these off again. Don't need
+		 * i_size_read because we hold i_mutex.
+		 */
+		if (pos + len > inode->i_size)
+			vmtruncate(inode, inode->i_size);
 	}
 	if (ret == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
 		goto retry;
Signature · 6 lines
1.6.0.1.90.g27a6e

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#6 lore raw

Re: [PATCH] ext3: truncate block allocated on a failed ext3_write_begin

Quoted 9 lines · Dmitri Monakhov
Quoted 6 lines · Aneesh Kumar K.V

For blocksize < pagesize we need to remove blocks that got allocte in block_wirte_begin if we fail with ENOSPC for later blocks. block_write_begin internally does this if it allocated page locally. This make sure we don't have blocks outisde inode.i_size during ENOSPC

BTW why this check was moved from generic_XXX_write to fs speciffic code?

Not quite sure what you mean by that ? block_write_begin generic code already does the vmtruncate if it had allocated page locally. ext3/4 allocate/grab_cache_page in write_begin and pass the page pointer to block_write_begin. That implies block_write_begin won't do the truncate. So we have to do it in filesystem write_begin. also we have to do it after unlocking the page.

Quoted 31 lines
Quoted 31 lines
---
 fs/ext3/inode.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
fs/ext3/inode.c +7 -0
index ebfec4d..f8424ad 100644
@@ -1186,6 +1186,13 @@ static int ext3_write_begin(struct file *file, struct address_space *mapping,
 		ext3_journal_stop(handle);
 		unlock_page(page);
 		page_cache_release(page);
+		/*
+		 * block_write_begin may have instantiated a few blocks
+		 * outside i_size.  Trim these off again. Don't need
+		 * i_size_read because we hold i_mutex.
+		 */
+		if (pos + len > inode->i_size)
+			vmtruncate(inode, inode->i_size);
 	}
 	if (ret == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
 		goto retry;
Signature · 6 lines
1.6.0.1.90.g27a6e

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html