Skip to contentSkip to conversation

BUG with delayed allocation

DISCUSSION Applied Aneesh Kumar K.V 4 participants 9 messages Mar 19, 2008 – Mar 20, 2008 ext4 file system

lore
Share & tools
lore
Applied
#1 lore raw
Quoted 7 lines · Aneesh Kumar K.V

Hi,

Eric actually observed it yesterday. I am able to reproduce it locally. With delayed allocation we are observing wrong value of i_size.

kenrel: 2.6.25-rc5 +
With all the patches in the patch queue

...

I also see this with only:

patches/delalloc-vfs.patch
patches/delalloc-ext4.patch

applied.

-Eric

#4 lore raw
In reply to Aneesh Kumar K.V
Quoted 5 lines · Aneesh Kumar K.V

Hi,

Eric actually observed it yesterday. I am able to reproduce it locally. With delayed allocation we are observing wrong value of i_size.

The problem is current delalloc does not update on-disk i_size until writeout time, the in-core i_size is updated though.

Could you try the following patch? It updates the i_disksize at the write_end time.

---
 fs/ext4/inode.c |   39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
fs/ext4/inode.c +38 -1
Index: linux-2.6.25-rc5/fs/ext4/inode.c
===================================================================
@@ -1355,6 +1355,43 @@ static int ext4_writeback_write_end(stru
 	return ret ? ret : copied;
 }
 
+static int ext4_da_write_end(struct file *file,
+				struct address_space *mapping,
+				loff_t pos, unsigned len, unsigned copied,
+				struct page *page, void *fsdata)
+{
+	handle_t *handle;
+	struct inode *inode = file->f_mapping->host;
+	int needed_blocks = ext4_writepage_trans_blocks(inode);
+	int ret = 0, ret2;
+	loff_t new_i_size;
+
+	handle = ext4_journal_start(inode, needed_blocks);
+	if (IS_ERR(handle)) {
+		unlock_page(page);
+		page_cache_release(page);
+		ret = PTR_ERR(handle);
+		return ret;
+	}
+
+	new_i_size = pos + copied;
+	if (new_i_size > EXT4_I(inode)->i_disksize)
+		EXT4_I(inode)->i_disksize = new_i_size;
+
+	copied = ext4_generic_write_end(file, mapping, pos, len, copied,
+							page, fsdata);
+	if (copied < 0)
+		ret = copied;
+
+	ret2 = ext4_journal_stop(handle);
+	if (!ret)
+		ret = ret2;
+	unlock_page(page);
+	page_cache_release(page);
+
+	return ret ? ret : copied;
+}
+
 static int ext4_journalled_write_end(struct file *file,
 				struct address_space *mapping,
 				loff_t pos, unsigned len, unsigned copied,
@@ -2020,7 +2057,7 @@ static const struct address_space_operat
 	.writepages	= ext4_da_writepages,
 	.sync_page	= block_sync_page,
 	.write_begin	= ext4_da_write_begin,
-	.write_end	= generic_write_end,
+	.write_end	= ext4_da_write_end,
 	.bmap		= ext4_bmap,
 	.invalidatepage	= ext4_da_invalidatepage,
 	.releasepage	= ext4_releasepage,
#3 lore raw
In reply to Aneesh Kumar K.V

This mail didn't seem to reach the list.

-aneesh

Quoted 81 lines · Aneesh Kumar K.V
Quoted 9 lines · Eric Sandeen
Quoted 7 lines · Aneesh Kumar K.V

Hi,

Eric actually observed it yesterday. I am able to reproduce it locally. With delayed allocation we are observing wrong value of i_size.

kenrel: 2.6.25-rc5 +
With all the patches in the patch queue

Quoted 8 lines

...

I also see this with only:

patches/delalloc-vfs.patch
patches/delalloc-ext4.patch

applied.

This one seems to work for me. Need to think a bit more about parallel truncate.

(quoted diff) +0 -1
-aneesh
fs/ext4/inode.c +38 -1
index 79930df..b74426d 100644
@@ -1488,6 +1488,43 @@ out:
 
 	return ret;
 }
+static int ext4_da_writepage(struct page *page,
+				struct writeback_control *wbc)
+{
+	struct inode *inode = page->mapping->host;
+	handle_t *handle = NULL;
+	int ret = 0;
+	int err;
+
+	if (ext4_journal_current_handle())
+		goto out_fail;
+
+	handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
+	if (IS_ERR(handle)) {
+		ret = PTR_ERR(handle);
+		goto out_fail;
+	}
+
+	if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode))
+		ret = nobh_writepage(page, ext4_get_block, wbc);
+	else
+		ret = block_write_full_page(page, ext4_get_block, wbc);
+
+	if (!ret && inode->i_size > EXT4_I(inode)->i_disksize) {
+		EXT4_I(inode)->i_disksize = inode->i_size;
+		ext4_mark_inode_dirty(handle, inode);
+	}
+
+	err = ext4_journal_stop(handle);
+	if (!ret)
+		ret = err;
+	return ret;
+
+out_fail:
+	redirty_page_for_writepage(wbc, page);
+	unlock_page(page);
+	return ret;
+}
 
 static int ext4_da_writepages(struct address_space *mapping,
 				struct writeback_control *wbc)
@@ -2015,7 +2052,7 @@ static const struct address_space_operations ext4_journalled_aops = {
 static const struct address_space_operations ext4_da_aops = {
 	.readpage	= ext4_readpage,
 	.readpages	= ext4_readpages,
-	.writepage	= ext4_writeback_writepage,
+	.writepage	= ext4_da_writepage,
 	.writepages	= ext4_da_writepages,
 	.sync_page	= block_sync_page,
 	.write_begin	= ext4_da_write_begin,
#5 lore raw
In reply to Mingming Cao
Quoted 10 lines · Mingming Cao
Quoted 5 lines · Aneesh Kumar K.V

Hi,

Eric actually observed it yesterday. I am able to reproduce it locally. With delayed allocation we are observing wrong value of i_size.

The problem is current delalloc does not update on-disk i_size until writeout time, the in-core i_size is updated though.

ext4_da_writepages actually update the i_disksize during writeout time. The writepage callback for delalloc was using ext4_writeback_writepage, which didn't update the i_disksize. That is why some of the files have correct size while some doesn't. I tested the below change and sent this to list. But i appears vger dropped the mail to the list.

fs/ext4/inode.c +38 -1
index 79930df..b74426d 100644
@@ -1488,6 +1488,43 @@ out:
 
 	return ret;
 }
+static int ext4_da_writepage(struct page *page,
+				struct writeback_control *wbc)
+{
+	struct inode *inode = page->mapping->host;
+	handle_t *handle = NULL;
+	int ret = 0;
+	int err;
+
+	if (ext4_journal_current_handle())
+		goto out_fail;
+
+	handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
+	if (IS_ERR(handle)) {
+		ret = PTR_ERR(handle);
+		goto out_fail;
+	}
+
+	if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode))
+		ret = nobh_writepage(page, ext4_get_block, wbc);
+	else
+		ret = block_write_full_page(page, ext4_get_block, wbc);
+
+	if (!ret && inode->i_size > EXT4_I(inode)->i_disksize) {
+		EXT4_I(inode)->i_disksize = inode->i_size;
+		ext4_mark_inode_dirty(handle, inode);
+	}
+
+	err = ext4_journal_stop(handle);
+	if (!ret)
+		ret = err;
+	return ret;
+
+out_fail:
+	redirty_page_for_writepage(wbc, page);
+	unlock_page(page);
+	return ret;
+}
 
 static int ext4_da_writepages(struct address_space *mapping,
 				struct writeback_control *wbc)
@@ -2015,7 +2052,7 @@ static const struct address_space_operations ext4_journalled_aops = {
 static const struct address_space_operations ext4_da_aops = {
 	.readpage	= ext4_readpage,
 	.readpages	= ext4_readpages,
-	.writepage	= ext4_writeback_writepage,
+	.writepage	= ext4_da_writepage,
 	.writepages	= ext4_da_writepages,
 	.sync_page	= block_sync_page,
 	.write_begin	= ext4_da_write_begin,
Quoted 3 lines

Could you try the following patch? It updates the i_disksize at the write_end time.

I will test the patch and update you. BTW shouldn't we update i_disksize only after actual block got allocated ?

Quoted 57 lines
---
 fs/ext4/inode.c |   39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

Index: linux-2.6.25-rc5/fs/ext4/inode.c
===================================================================

fs/ext4/inode.c +37 -0
@@ -1355,6 +1355,43 @@ static int ext4_writeback_write_end(stru
 	return ret ? ret : copied;
 }
 
+static int ext4_da_write_end(struct file *file,
+				struct address_space *mapping,
+				loff_t pos, unsigned len, unsigned copied,
+				struct page *page, void *fsdata)
+{
+	handle_t *handle;
+	struct inode *inode = file->f_mapping->host;
+	int needed_blocks = ext4_writepage_trans_blocks(inode);
+	int ret = 0, ret2;
+	loff_t new_i_size;
+
+	handle = ext4_journal_start(inode, needed_blocks);
+	if (IS_ERR(handle)) {
+		unlock_page(page);
+		page_cache_release(page);
+		ret = PTR_ERR(handle);
+		return ret;
+	}
+
+	new_i_size = pos + copied;
+	if (new_i_size > EXT4_I(inode)->i_disksize)
+		EXT4_I(inode)->i_disksize = new_i_size;
+
+	copied = ext4_generic_write_end(file, mapping, pos, len, copied,
+							page, fsdata);
+	if (copied < 0)
+		ret = copied;
+
+	ret2 = ext4_journal_stop(handle);
+	if (!ret)
+		ret = ret2;
+	unlock_page(page);
+	page_cache_release(page);
+
+	return ret ? ret : copied;
+}
+
 static int ext4_journalled_write_end(struct file *file,
 				struct address_space *mapping,
 				loff_t pos, unsigned len, unsigned copied,

fs/ext4/inode.c static const struct address_space_operat · line 2063

Quoted 8 lines
fs/ext4/inode.c +1 -1
@@ -2020,7 +2057,7 @@ static const struct address_space_operat
 	.writepages	= ext4_da_writepages,
 	.sync_page	= block_sync_page,
 	.write_begin	= ext4_da_write_begin,
-	.write_end	= generic_write_end,
+	.write_end	= ext4_da_write_end,
 	.bmap		= ext4_bmap,
 	.invalidatepage	= ext4_da_invalidatepage,
 	.releasepage	= ext4_releasepage,

-aneesh

#6 lore raw
Quoted 8 lines · Aneesh Kumar K.V
Quoted 3 lines

Could you try the following patch? It updates the i_disksize at the write_end time.

I will test the patch and update you. BTW shouldn't we update i_disksize only after actual block got allocated ?

Hmm...I am not 100% sure but I think we should not to change the behavior that the on-disk inode size should be updated when write() returns to user. Right now the in-memory inode size is updated, user would expecting the same when they run e2fsck, but e2fsck reads inode size from disk. Pushing the inode i_disksize update at the writeout (allocation) time will cause the window that i_size is different than the i_disksize being enlarged quite big.

Mingming

Quoted 71 lines
Quoted 57 lines
---
 fs/ext4/inode.c |   39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

Index: linux-2.6.25-rc5/fs/ext4/inode.c
===================================================================

fs/ext4/inode.c +37 -0
@@ -1355,6 +1355,43 @@ static int ext4_writeback_write_end(stru
 	return ret ? ret : copied;
 }
 
+static int ext4_da_write_end(struct file *file,
+				struct address_space *mapping,
+				loff_t pos, unsigned len, unsigned copied,
+				struct page *page, void *fsdata)
+{
+	handle_t *handle;
+	struct inode *inode = file->f_mapping->host;
+	int needed_blocks = ext4_writepage_trans_blocks(inode);
+	int ret = 0, ret2;
+	loff_t new_i_size;
+
+	handle = ext4_journal_start(inode, needed_blocks);
+	if (IS_ERR(handle)) {
+		unlock_page(page);
+		page_cache_release(page);
+		ret = PTR_ERR(handle);
+		return ret;
+	}
+
+	new_i_size = pos + copied;
+	if (new_i_size > EXT4_I(inode)->i_disksize)
+		EXT4_I(inode)->i_disksize = new_i_size;
+
+	copied = ext4_generic_write_end(file, mapping, pos, len, copied,
+							page, fsdata);
+	if (copied < 0)
+		ret = copied;
+
+	ret2 = ext4_journal_stop(handle);
+	if (!ret)
+		ret = ret2;
+	unlock_page(page);
+	page_cache_release(page);
+
+	return ret ? ret : copied;
+}
+
 static int ext4_journalled_write_end(struct file *file,
 				struct address_space *mapping,
 				loff_t pos, unsigned len, unsigned copied,

fs/ext4/inode.c static const struct address_space_operat · line 2063

Quoted 8 lines
fs/ext4/inode.c +1 -1
@@ -2020,7 +2057,7 @@ static const struct address_space_operat
 	.writepages	= ext4_da_writepages,
 	.sync_page	= block_sync_page,
 	.write_begin	= ext4_da_write_begin,
-	.write_end	= generic_write_end,
+	.write_end	= ext4_da_write_end,
 	.bmap		= ext4_bmap,
 	.invalidatepage	= ext4_da_invalidatepage,
 	.releasepage	= ext4_releasepage,

-aneesh
--
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

#7 lore raw
Quoted 10 lines · Mingming Cao
Quoted 8 lines · Aneesh Kumar K.V
Quoted 3 lines

Could you try the following patch? It updates the i_disksize at the write_end time.

I will test the patch and update you. BTW shouldn't we update i_disksize only after actual block got allocated ?

Quoted 8 lines

Hmm...I am not 100% sure but I think we should not to change the behavior that the on-disk inode size should be updated when write() returns to user. Right now the in-memory inode size is updated, user would expecting the same when they run e2fsck, but e2fsck reads inode size from disk. Pushing the inode i_disksize update at the writeout (allocation) time will cause the window that i_size is different than the i_disksize being enlarged quite big.

If we are updating i_disksize during write_end and if we crash before actually allocating the blocks e2fsck will find errors because the inode doesn't really have that many blocks right ?

-aneesh

#8 lore raw
Quoted 16 lines · Aneesh Kumar K.V
Quoted 7 lines · Mingming Cao
Quoted 5 lines · Aneesh Kumar K.V
Quoted 2 lines

Could you try the following patch? It updates the i_disksize at the write_end time.

I will test the patch and update you. BTW shouldn't we update i_disksize only after actual block got allocated ?

Quoted 7 lines

Hmm...I am not 100% sure but I think we should not to change the behavior that the on-disk inode size should be updated when write() returns to user. Right now the in-memory inode size is updated, user would expecting the same when they run e2fsck, but e2fsck reads inode size from disk. Pushing the inode i_disksize update at the writeout (allocation) time will cause the window that i_size is different than the i_disksize being enlarged quite big.

Quoted 3 lines

If we are updating i_disksize during write_end and if we crash before actually allocating the blocks e2fsck will find errors because the inode doesn't really have that many blocks right ?

No, it would just think the file is sparse and return \0 for the reads.

That said, I don't agree with Mingming - the i_disksize should only be increased at the time the blocks are allocated on disk and not when the file is extended in memory. Even if the window where i_size is different than i_disksize is large, this is only important after a crash, and at that time ordered mode users want the file to have a shorter i_disksize and the file contains only valid data, instead of the extended i_size but the file contains \0 bytes at the end.

Cheers, Andreas
--
Andreas Dilger
Sr. Staff Engineer, Lustre Group
Sun Microsystems of Canada, Inc.