[PATCH] f2fs: support cold file defragementation

From: Daejun Park
Date: Wed May 12 2021 - 05:09:40 EST


F2FS_IOC_DEFRAGMENT performs defragmentation of a file. In
f2fs_defragment_range(), it checks whether in-place update policy is
applied, for avoiding unnecessary write operations.
The cold file may be fragmented according to the write order. However,
in the case of a cold file, the in-place update policy is applied, so it
is can not be defragmented.
This patch temporarily clears the cold bit so that the file can be
defragmented.

Signed-off-by: Daejun Park <daejun7.park@xxxxxxxxxxx>
---
fs/f2fs/file.c | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index ceb575f99048..c38e9727ce25 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -2536,11 +2536,23 @@ static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
unsigned int total = 0, sec_num;
block_t blk_end = 0;
bool fragmented = false;
+ bool is_cold = file_is_cold(inode);
int err;

+ /*
+ * Cold file can be fragmented by write order. So we clear cold bit from
+ * the file temporarily.
+ */
+ if (is_cold)
+ file_clear_cold(inode);
+
/* if in-place-update policy is enabled, don't waste time here */
- if (f2fs_should_update_inplace(inode, NULL))
+ if (f2fs_should_update_inplace(inode, NULL)) {
+ /* restore file temperature */
+ if (is_cold)
+ file_set_cold(inode);
return -EINVAL;
+ }

pg_start = range->start >> PAGE_SHIFT;
pg_end = (range->start + range->len) >> PAGE_SHIFT;
@@ -2665,6 +2677,9 @@ static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
clear_inode_flag(inode, FI_DO_DEFRAG);
out:
inode_unlock(inode);
+ /* restore file temperature */
+ if (is_cold)
+ file_set_cold(inode);
if (!err)
range->len = (u64)total << PAGE_SHIFT;
return err;
--
2.25.1