Skip to contentSkip to conversation

ext4: Add support FALLOC_FL_COLLAPSE_RANGE for fallocate

PATCH Superseded Namjae Jeon 3 participants 6 messages Feb 20, 2014 – Feb 27, 2014

Subsystems (3)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 ext4: Add support FALLOC_FL_COLLAPSE_RANGE for fallocate

From: Namjae Jeon <[email protected]>

This patch implements fallocate's FALLOC_FL_COLLAPSE_RANGE for Ext4.

The semantics of this flag are following:
1) It collapses the range lying between offset and length by removing any data blocks which are present in this range and than updates all the logical offsets of extents beyond "offset + len" to nullify the hole created by removing blocks. In short, it does not leave a hole.
2) It should be used exclusively. No other fallocate flag in combination.
3) Offset and length supplied to fallocate should be fs block size aligned in case of xfs and ext4.
4) Collaspe range does not work beyond i_size.

---

Changelog
v6:
- Fix compile error issue.

v5: Fix review comments from Lukas Czerner.
- Added checks for immutable files and swap files.
- Move inode flag checks under imutex.
- Remove an obsolete checking.
- truncate_pagecache_range can work on page size aligned byte offsets so alignment is not required.

v4:
- move block size aligned check from VFS layer to FS specific layer.

v3:
- fix checkpatch.pl warning

v2:
- Added check to make sure before shifting that the hole is big enough to contian the shift.
- Added code to make merging of extent where possible.
- Change semantics of collapse range and contain it within EOF.

 fs/ext4/ext4.h              |   3 +
 fs/ext4/extents.c           | 304 +++++++++++++++++++++++++++++++++++++++++++-
 fs/ext4/move_extent.c       |   2 +-
 include/trace/events/ext4.h |  25 ++++
 4 files changed, 332 insertions(+), 2 deletions(-)
fs/ext4/ext4.h +3 -0
index ece5556..ca9b115 100644
@@ -2755,6 +2755,7 @@ 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_ext_precache(struct inode *inode);
+extern int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len);
 
 /* move_extent.c */
 extern void ext4_double_down_write_data_sem(struct inode *first,
@@ -2764,6 +2765,8 @@ extern void ext4_double_up_write_data_sem(struct inode *orig_inode,
 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 +303 -1

318 lines not on this page · raw message

fs/ext4/move_extent.c +1 -1
index f39a88a..58ee7dc 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 197d312..90e2f71 100644
@@ -2410,6 +2410,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 · 7 lines
1.7.11-rc0


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

From: Namjae Jeon <[email protected]>

This patch implements fallocate's FALLOC_FL_COLLAPSE_RANGE for Ext4.

The semantics of this flag are following:
1) It collapses the range lying between offset and length by removing any data blocks which are present in this range and than updates all the logical offsets of extents beyond "offset + len" to nullify the hole created by removing blocks. In short, it does not leave a hole.
2) It should be used exclusively. No other fallocate flag in combination.
3) Offset and length supplied to fallocate should be fs block size aligned in case of xfs and ext4.
4) Collaspe range does not work beyond i_size.

Quoted 3 lines

In terms of how to get this upstream, it looks like if we do something like this, we can let this patch go via the ext4 tree and we don't need to worry about whether the vfs level changes have gone in our not.

fs/ext4/extents.c +4 -0
index ad13359..d7a78ed 100644
@@ -46,6 +46,10 @@
 
 #include <trace/events/ext4.h>
 
+#ifndef FALLOC_FL_COLLAPSE_RANGE
+#define FALLOC_FL_COLLAPSE_RANGE	0x08
+#endif
+
 /*
  * used by extent splitting.
  */

Review

Quoted 6 lines
(quoted diff) +6 -0
+	ret = ext4_es_remove_extent(inode, punch_start,
+				    EXT_MAX_BLOCKS - punch_start - 1);
+	if (ret) {
+		up_write(&EXT4_I(inode)->i_data_sem);
+		goto out_stop;
+	}

Doing this at first is probably a bad idea; you should do this at the end, and then completely invalidate the es cache for that inode. That way, the right thing happens if you get an error in the middle releasing the boxes and shifting the extents:

Review

Quoted 13 lines
(quoted diff) +13 -0
+
+	ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
+	if (ret) {
+		up_write(&EXT4_I(inode)->i_data_sem);
+		goto out_stop;
+	}
+
+	ret = ext4_ext_shift_extents(inode, handle, punch_stop,
+				     punch_stop - punch_start);
+	if (ret) {
+		up_write(&EXT4_I(inode)->i_data_sem);
+		goto out_stop;
+	}

The fact that you are doing these two as two separate steps is dangerous; what if you've already released the blocks in ext4_ext_remove_space(), and ext4_ext_shift_extents() fails in the middle of the processing? That will leave the file system inconsistent, which would be bad.

Making sure that the right happens if there is a failure in the middle of the operation is Really Important....

- Ted

#2 lore raw
Quoted 29 lines · Theodore Ts'o
Quoted 14 lines · Namjae Jeon

From: Namjae Jeon <[email protected]>

This patch implements fallocate's FALLOC_FL_COLLAPSE_RANGE for Ext4.

The semantics of this flag are following:
1) It collapses the range lying between offset and length by removing any data blocks which are present in this range and than updates all the logical offsets of extents beyond "offset + len" to nullify the hole created by removing blocks. In short, it does not leave a hole.
2) It should be used exclusively. No other fallocate flag in combination.
3) Offset and length supplied to fallocate should be fs block size aligned in case of xfs and ext4.
4) Collaspe range does not work beyond i_size.

Quoted 3 lines

In terms of how to get this upstream, it looks like if we do something like this, we can let this patch go via the ext4 tree and we don't need to worry about whether the vfs level changes have gone in our not.

fs/ext4/extents.c +0 -0
index ad13359..d7a78ed 100644

fs/ext4/extents.c line 55

Quoted 10 lines
fs/ext4/extents.c +4 -0
@@ -46,6 +46,10 @@
 
 #include <trace/events/ext4.h>
 
+#ifndef FALLOC_FL_COLLAPSE_RANGE
+#define FALLOC_FL_COLLAPSE_RANGE	0x08
+#endif
+
 /*
  * used by extent splitting.
  */

You're more than welcome to do that, Ted. Just wait until we get the VFS part into the XFS tree first ;)

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#3 lore raw
In reply to Theodore Ts'o

2014-02-23 2:09 GMT+09:00, Theodore Ts'o <[email protected]>:

Quoted 19 lines · Namjae Jeon
Quoted 18 lines

From: Namjae Jeon <[email protected]>

This patch implements fallocate's FALLOC_FL_COLLAPSE_RANGE for Ext4.

The semantics of this flag are following:
1) It collapses the range lying between offset and length by removing any data
blocks which are present in this range and than updates all the
logical
offsets of extents beyond "offset + len" to nullify the hole created
by
removing blocks. In short, it does not leave a hole.
2) It should be used exclusively. No other fallocate flag in combination.
3) Offset and length supplied to fallocate should be fs block size aligned
in case of xfs and ext4.
4) Collaspe range does not work beyond i_size.

Quoted 8 lines
Quoted 3 lines

In terms of how to get this upstream, it looks like if we do something like this, we can let this patch go via the ext4 tree and we don't need to worry about whether the vfs level changes have gone in our not.

Okay, Dave already answered.

Quoted 18 lines
fs/ext4/extents.c +4 -0
index ad13359..d7a78ed 100644
@@ -46,6 +46,10 @@
 
 #include <trace/events/ext4.h>
 
+#ifndef FALLOC_FL_COLLAPSE_RANGE
+#define FALLOC_FL_COLLAPSE_RANGE	0x08
+#endif
+
 /*
  * used by extent splitting.
  */
Quoted 11 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+	ret = ext4_es_remove_extent(inode, punch_start,
+				    EXT_MAX_BLOCKS - punch_start - 1);
+	if (ret) {
+		up_write(&EXT4_I(inode)->i_data_sem);
+		goto out_stop;
+	}

Doing this at first is probably a bad idea; you should do this at the end, and then completely invalidate the es cache for that inode. That way, the right thing happens if you get an error in the middle releasing the boxes and shifting the extents:

Okay, I see.

Quoted 15 lines
Quoted 4 lines
(quoted diff) +3 -0
+
+	ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
+	if (ret) {

Review

Quoted 10 lines
(quoted diff) +10 -0
+		up_write(&EXT4_I(inode)->i_data_sem);
+		goto out_stop;
+	}
+
+	ret = ext4_ext_shift_extents(inode, handle, punch_stop,
+				     punch_stop - punch_start);
+	if (ret) {
+		up_write(&EXT4_I(inode)->i_data_sem);
+		goto out_stop;
+	}

Hi Ted.

Quoted 5 lines

The fact that you are doing these two as two separate steps is dangerous; what if you've already released the blocks in ext4_ext_remove_space(), and ext4_ext_shift_extents() fails in the middle of the processing? That will leave the file system inconsistent, which would be bad.

If there is error in the middle of extent shifting, the hole will present between the last shifted extent and the extent at which error happen so this will be consistent state.
IMHO even if there is error in between the shift, filesystem will be in consistent state.
Am I missing something?

Quoted 6 lines

Making sure that the right happens if there is a failure in the middle of the operation is Really Important....

- Ted

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

maintainer
#4 lore raw
Quoted 11 lines · Namjae Jeon
Quoted 11 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+	ret = ext4_es_remove_extent(inode, punch_start,
+				    EXT_MAX_BLOCKS - punch_start - 1);
+	if (ret) {
+		up_write(&EXT4_I(inode)->i_data_sem);
+		goto out_stop;
+	}

Doing this at first is probably a bad idea; you should do this at the end, and then completely invalidate the es cache for that inode. That way, the right thing happens if you get an error in the middle releasing the boxes and shifting the extents:

Quoted 1 line

Okay, I see.

Actually, thinking about this some more, we do want to do this first, since if we error out, we do need to make sure the extent cache is flushed.

Quoted 6 lines

If there is error in the middle of extent shifting, the hole will present between the last shifted extent and the extent at which error happen so this will be consistent state.
IMHO even if there is error in between the shift, filesystem will be in consistent state.
Am I missing something?

No, I was wrong about that; you're right. The file will be in an inconsistent statement, which will probably be highly confusing for the application, but the file system will be fine.

So I withdraw my complaints. I'll do a bit more testing, but so far the patch looks fine to me. Thanks for your reply and your work!

- Ted

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

#5 lore raw

2014-02-27 1:48 GMT+09:00, Theodore Ts'o <[email protected]>:

Quoted 12 lines · Namjae Jeon
Quoted 11 lines
Quoted 11 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+	ret = ext4_es_remove_extent(inode, punch_start,
+				    EXT_MAX_BLOCKS - punch_start - 1);
+	if (ret) {
+		up_write(&EXT4_I(inode)->i_data_sem);
+		goto out_stop;
+	}

Doing this at first is probably a bad idea; you should do this at the end, and then completely invalidate the es cache for that inode. That way, the right thing happens if you get an error in the middle releasing the boxes and shifting the extents:

Quoted 5 lines
Quoted 1 line

Okay, I see.

Actually, thinking about this some more, we do want to do this first, since if we error out, we do need to make sure the extent cache is flushed.

Okay.

Quoted 8 lines
Quoted 7 lines

If there is error in the middle of extent shifting, the hole will present between the last shifted extent and the extent at which error happen so this will be consistent state.
IMHO even if there is error in between the shift, filesystem will be in consistent state.
Am I missing something?

Quoted 6 lines

No, I was wrong about that; you're right. The file will be in an inconsistent statement, which will probably be highly confusing for the application, but the file system will be fine.

So I withdraw my complaints. I'll do a bit more testing, but so far the patch looks fine to me. Thanks for your reply and your work!

Thanks for your review! I will fix these include Hugh's review points.

Quoted 3 lines

- Ted

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