Skip to contentSkip to conversation

ext4: Implement FALLOC_FL_COLLAPSE_RANGE

PATCH Superseded Namjae Jeon 2 participants 3 messages Jul 31, 2013 – Aug 1, 2013

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 3/3 ext4: Implement FALLOC_FL_COLLAPSE_RANGE

From: Namjae Jeon <[email protected]>

New fallocate flag FALLOC_FL_COLLAPSE_RANGE implementation for Ext4

---
 fs/ext4/ext4.h              |    3 +
 fs/ext4/extents.c           |  255 ++++++++++++++++++++++++++++++++++++++++++-
 fs/ext4/move_extent.c       |    2 +-
 include/trace/events/ext4.h |   25 +++++
 4 files changed, 283 insertions(+), 2 deletions(-)
fs/ext4/ext4.h +3 -0
index b577e45..35eed6a 100644
@@ -2692,6 +2692,7 @@ extern int ext4_find_delalloc_range(struct inode *inode,
 extern int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk);
 extern int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
 			__u64 start, __u64 len);
+extern int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len);
 
 
 /* move_extent.c */
@@ -2704,6 +2705,8 @@ void ext4_inode_double_unlock(struct inode *inode1, struct inode *inode2);
 extern int ext4_move_extents(struct file *o_filp, struct file *d_filp,
 			     __u64 start_orig, __u64 start_donor,
 			     __u64 len, __u64 *moved_len);
+extern int mext_next_extent(struct inode *inode, struct ext4_ext_path *path,
+			    struct ext4_extent **extent);
 
 /* page-io.c */
 extern int __init ext4_init_pageio(void);
fs/ext4/extents.c +254 -1

269 lines not on this page · raw message

fs/ext4/move_extent.c +1 -1
index e86dddb..c3a5ccf 100644
@@ -76,7 +76,7 @@ copy_extent_status(struct ext4_extent *src, struct ext4_extent *dest)
  * ext4_ext_path structure refers to the last extent, or a negative error
  * value on failure.
  */
-static int
+int
 mext_next_extent(struct inode *inode, struct ext4_ext_path *path,
 		      struct ext4_extent **extent)
 {
include/trace/events/ext4.h +25 -0
index 2068db2..a1f357c 100644
@@ -2399,6 +2399,31 @@ TRACE_EVENT(ext4_es_shrink_exit,
 		  __entry->shrunk_nr, __entry->cache_cnt)
 );
 
+TRACE_EVENT(ext4_collapse_range,
+	TP_PROTO(struct inode *inode, loff_t offset, loff_t len),
+
+	TP_ARGS(inode, offset, len),
+
+	TP_STRUCT__entry(
+		__field(dev_t,	dev)
+		__field(ino_t,	ino)
+		__field(loff_t,	offset)
+		__field(loff_t, len)
+	),
+
+	TP_fast_assign(
+		__entry->dev	= inode->i_sb->s_dev;
+		__entry->ino	= inode->i_ino;
+		__entry->offset	= offset;
+		__entry->len	= len;
+	),
+
+	TP_printk("dev %d,%d ino %lu offset %lld len %lld",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  (unsigned long) __entry->ino,
+		  __entry->offset, __entry->len)
+);
+
 #endif /* _TRACE_EXT4_H */
 
 /* This part must be outside protection */
Signature · 6 lines
1.7.9.5

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#1 lore raw
Quoted 3 lines · Namjae Jeon

From: Namjae Jeon <[email protected]>

New fallocate flag FALLOC_FL_COLLAPSE_RANGE implementation for Ext4

.....

Quoted 6 lines
fs/ext4/extents.c +6 -0
+
+	punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
+	punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
+
+	rounding = max_t(uint, 1 << EXT4_BLOCK_SIZE_BITS(sb), PAGE_CACHE_SIZE);
+	ioffset = offset & ~(rounding - 1);

fs/ext4/extents.c int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, · line 4975

Quoted 10 lines
fs/ext4/extents.c +10 -0
+
+	/* Write out all dirty pages */
+	ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, -1);
+	if (ret)
+		return ret;
+
+	/* Take mutex lock */
+	mutex_lock(&inode->i_mutex);
+
+	truncate_pagecache_range(inode, ioffset, -1);

Ted, that's invalidating the page cache from the start of the collaspse range to the end of the file. So the ext4 code is doing this bit correctly. Why isn't this in the XFS patches? Clearly the need for this was understood, and, well, this code is obviously copied from the XFS hole punching code. i.e. from xfs_free_file_space().

fs/ext4/extents.c int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, · line 4979

Quoted 3 lines
fs/ext4/extents.c +3 -0
+	/* Wait for existing dio to complete */
+	ext4_inode_block_unlocked_dio(inode);
+	inode_dio_wait(inode);

That should be done before invalidating the pagecache....

Quoted 9 lines
fs/ext4/extents.c +9 -0
+	credits = ext4_writepage_trans_blocks(inode);
+	handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
+	if (IS_ERR(handle)) {
+		ret = PTR_ERR(handle);
+		goto out_dio;
+	}
+
+	down_write(&EXT4_I(inode)->i_data_sem);
+

fs/ext4/extents.c int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, · line 4999

Quoted 10 lines
fs/ext4/extents.c +10 -0
+	ext4_discard_preallocations(inode);
+
+	ret = ext4_es_remove_extent(inode, punch_start,
+				    EXT_MAX_BLOCKS - punch_start - 1);
+	if (ret)
+		goto journal_stop;
+
+	ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
+	if (ret)
+		goto journal_stop;

So, this code punches out the existing space in the file so that the extent shifting is moving extents into a hole. Why is this in the ext4 code, but not the XFS code?

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#2 lore raw

2013/8/1, Dave Chinner <[email protected]>:

Quoted 20 lines · Namjae Jeon
Quoted 3 lines

From: Namjae Jeon <[email protected]>

New fallocate flag FALLOC_FL_COLLAPSE_RANGE implementation for Ext4

.....

Quoted 15 lines
fs/ext4/extents.c +15 -0
+
+	punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
+	punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
+
+	rounding = max_t(uint, 1 << EXT4_BLOCK_SIZE_BITS(sb), PAGE_CACHE_SIZE);
+	ioffset = offset & ~(rounding - 1);
+
+	/* Write out all dirty pages */
+	ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, -1);
+	if (ret)
+		return ret;
+
+	/* Take mutex lock */
+	mutex_lock(&inode->i_mutex);
+
Quoted 8 lines

fs/ext4/extents.c int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, · line 4975

Quoted 1 line
fs/ext4/extents.c +1 -0
+	truncate_pagecache_range(inode, ioffset, -1);

Ted, that's invalidating the page cache from the start of the collaspse range to the end of the file. So the ext4 code is doing this bit correctly. Why isn't this in the XFS patches? Clearly the need for this was understood, and, well, this code is obviously copied from the XFS hole punching code. i.e. from xfs_free_file_space().

we already called xfs_free_file_space for collpase range in XFS patch. So invalidate page cache has been calling from xfs_free_file_space in this patch :)

Quoted 6 lines

fs/ext4/extents.c int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, · line 4979

Quoted 4 lines
fs/ext4/extents.c +3 -0
+	/* Wait for existing dio to complete */
+	ext4_inode_block_unlocked_dio(inode);
+	inode_dio_wait(inode);

That should be done before invalidating the pagecache....

Ah, correct. will move it before invalidating.

Quoted 16 lines
Quoted 16 lines
fs/ext4/extents.c +15 -0
+	credits = ext4_writepage_trans_blocks(inode);
+	handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
+	if (IS_ERR(handle)) {
+		ret = PTR_ERR(handle);
+		goto out_dio;
+	}
+
+	down_write(&EXT4_I(inode)->i_data_sem);
+
+	ext4_discard_preallocations(inode);
+
+	ret = ext4_es_remove_extent(inode, punch_start,
+				    EXT_MAX_BLOCKS - punch_start - 1);
+	if (ret)
+		goto journal_stop;
Quoted 8 lines

fs/ext4/extents.c int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, · line 4999

Quoted 4 lines
fs/ext4/extents.c +4 -0
+
+	ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
+	if (ret)
+		goto journal_stop;

So, this code punches out the existing space in the file so that the extent shifting is moving extents into a hole. Why is this in the ext4 code, but not the XFS code?

for Ext4, we are calling ext4_ext_remove_space directly from collapse range function while in xfs we are using its punch hole functionality. This is because Ext4 punch hole does not work beyond EOF. Moreover, there is i_mutex acquired within ext4_punch_hole which can leads to race.

Something like:

ext4_fallocate {
    ext4_punch_hole{
        grab i_mutex;
        do punching;
        release i_mutex:
        }
    ext4_collapse_space{
        sync dirty pages
        grab i_mutex;
        update extent;
        release i_mutex;
    }
} // ext4_fallocate_ends

XFS has no such problem as xfs_iolock is taken after entering in xfs_file_fallocate and released on exit.

Thanks for review :)

Quoted 8 lines

Cheers,

Dave.
--
Dave Chinner
[email protected]