ext4: Add support FALLOC_FL_COLLAPSE_RANGE for fallocate
Subsystems (3)
PATCH v7 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
v7:
- As pointed by Huge Dickins, use truncate_setsize instead if i_size_write so that page cache can be truncated properly.
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 non 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
@@ -2757,6 +2757,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,
@@ -2766,6 +2767,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
@@ -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
@@ -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 · 1 line
1.7.11-rc0
[email protected]
maintainerI just noticed I had the v6 version of your patch in my tree. I've updated it to be the v7 version, thanks.
- Ted
A message that is not in the archive (sent off-list or not yet received): CAKYAXd-EO0pp=wz_Tg+2NEw9Nyu23sKXc-3n-fZUieES3+xt7w@mail.gmail.com
Theodore Ts'o
maintainerSome final testing luckily caught a bug in your patch. I'll fix it up, but for future reference, it's important to check to see if the handle is valid (i.e., we are not in no-journal mode) before using any of the journalling functions. Otherwise, you will crash if you try using the code path when the file system does not have a journal.
BTW, I would strongly recommend using the automated regression test system found at:
git://git.kernel.org/pub/scm/fs/ext2/xfstests-bld.git
It finds these sorts of problems very quickly. After you build a kernel, running "kvm-xfstests -g quick" would have found the problem very quickly. The fastest reproducer for the bug that I found is:
"kvm-xfstests -c nojournal shared/001".
Cheers,
- Ted
fs/ext4/extents.c +3 -0
@@ -4902,6 +4902,9 @@ ext4_access_path(handle_t *handle, struct inode *inode, {int credits, err; +if (!ext4_handle_valid (handle)) +return 0 ; +/* * Check if need to extend journal credits * 3 for leaf, sb, and inode plus 2 (bmap and group
Hi Ted.
2014-04-03 1:42 GMT+09:00 Theodore Ts'o <[email protected]>:
Quoted 5 lines
Some final testing luckily caught a bug in your patch. I'll fix it up, but for future reference, it's important to check to see if the handle is valid (i.e., we are not in no-journal mode) before using any of the journalling functions. Otherwise, you will crash if you try using the code path when the file system does not have a journal.
Thanks for finding bug. I missed no journal case..
Quoted 10 lines
BTW, I would strongly recommend using the automated regression test system found at:
git://git.kernel.org/pub/scm/fs/ext2/xfstests-bld.gitIt finds these sorts of problems very quickly. After you build a kernel, running "kvm-xfstests -g quick" would have found the problem very quickly. The fastest reproducer for the bug that I found is:
"kvm-xfstests -c nojournal shared/001".
Okay, I will do the way you guided next time.
Thanks!
Quoted 21 lines
Cheers,
- Ted
fs/ext4/extents.c +3
-0@@ -4902,6 +4902,9 @@ ext4_access_path(handle_t *handle, struct inode *inode, {int credits, err; +if (!ext4_handle_valid (handle)) +return 0 ; +/* * Check if need to extend journal credits * 3 for leaf, sb, and inode plus 2 (bmap and group
Quoted 9 lines · Theodore Ts'o
Some final testing luckily caught a bug in your patch. I'll fix it up, but for future reference, it's important to check to see if the handle is valid (i.e., we are not in no-journal mode) before using any of the journalling functions. Otherwise, you will crash if you try using the code path when the file system does not have a journal.
BTW, I would strongly recommend using the automated regression test system found at:
Quoted 6 lines
git://git.kernel.org/pub/scm/fs/ext2/xfstests-bld.gitIt finds these sorts of problems very quickly. After you build a kernel, running "kvm-xfstests -g quick" would have found the problem very quickly. The fastest reproducer for the bug that I found is:
"kvm-xfstests -c nojournal shared/001".
Hi Ted,
If you check the xfstests commit history, I think you'll find that Namjae wrote the xfstests that triggered the failure you are seeing.
You're just testing more esoteric configurations than most people do
- same as I do for XFS. We can't expect every developer to test their code on every possible configuration before they submit it.... ;)
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Theodore Ts'o
maintainerQuoted 9 lines · Dave Chinner
If you check the xfstests commit history, I think you'll find that Namjae wrote the xfstests that triggered the failure you are seeing.
You're just testing more esoteric configurations than most people do
- same as I do for XFS. We can't expect every developer to test their code on every possible configuration before they submit it.... ;)
"kvm-xfstests -g smoke" takes about 4-5 hours to run[1], and allows all of the ext4 developers to run a much wider set of configurations. The more work they can do, the more I scale. This is one of the reasons why I'm pushing the testing automation so heavily. :-)
- Ted
[1] It's even faster if you run kvm-xfstests on a sacrificial ssd, or if you have a big enough machine to run using a series of 5g ramdisks. "kvm-xfstests -g auto" takes about 14 hours to run with spinning platters, so it's much more of a commitment.