Pinned fallocate allocates one section at a time. If f2fs_map_blocks()
fails after allocating part of the current section, the error path still
adds the partial length to expanded and clears FADVISE_TRUNC_BIT. A failed
fallocate can then leave a pinned file with a non-section-aligned size.
Count only complete sections on failure and keep FADVISE_TRUNC_BIT set
until the failed section preallocation has been removed.
Fixes: f5a53edcf01e ("f2fs: support aligned pinned file")
Cc: stable@kernel.org
Signed-off-by: Wenjie Qi <qiwenjie@xiaomi.com>
---
fs/f2fs/file.c | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index c54897a25981..83af73f3831e 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -1966,11 +1966,12 @@ static int f2fs_expand_inode_data(struct inode *inode, loff_t offset,
map.m_seg_type = CURSEG_COLD_DATA_PINNED;
err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_DIO);
- file_dont_truncate(inode);
+ if (!err)
+ file_dont_truncate(inode);
f2fs_up_write(&sbi->pin_sem);
- expanded += map.m_len;
+ expanded += err ? rounddown(map.m_len, sec_blks) : map.m_len;
sec_len -= map.m_len;
map.m_lblk += map.m_len;
if (!err && sec_len)
@@ -1986,7 +1987,7 @@ static int f2fs_expand_inode_data(struct inode *inode, loff_t offset,
pgoff_t last_off;
if (!expanded)
- return err;
+ goto out;
last_off = pg_start + expanded - 1;
@@ -2004,6 +2005,16 @@ static int f2fs_expand_inode_data(struct inode *inode, loff_t offset,
f2fs_i_size_write(inode, new_size);
}
+out:
+ if (err && f2fs_is_pinned_file(inode) && file_should_truncate(inode)) {
+ f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
+ filemap_invalidate_lock(inode->i_mapping);
+ if (!f2fs_truncate(inode))
+ file_dont_truncate(inode);
+ filemap_invalidate_unlock(inode->i_mapping);
+ f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
+ }
+
return err;
}
--
2.43.0