Skip to content

Commit db2c5b9

Browse files
WenjieQiHUSTgregkh
authored andcommitted
f2fs: keep atomic write retry from zeroing original data
commit 6d874b6 upstream. A partial atomic write reserves a block in the COW inode before reading the original data page for the untouched bytes in that page. If that read fails, write_begin returns an error but leaves the COW inode entry as NEW_ADDR. A retry of the same partial write then finds the COW entry, treats it as existing COW data, and f2fs_write_begin() zeroes the whole folio because blkaddr is NEW_ADDR. If the retry is committed, the bytes outside the retried write range are committed as zeroes instead of preserving the original file contents. Only use the COW inode as the read source when it already has a real data block. If the COW entry is still NEW_ADDR, treat it as a reservation to reuse: keep reading the old data from the original inode and avoid reserving or accounting the same atomic block again. Cc: stable@kernel.org Fixes: 3db1de0 ("f2fs: change the current atomic write way") Signed-off-by: Wenjie Qi <qiwenjie@xiaomi.com> Reviewed-by: Chao Yu <chao@kernel.org> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 20190e4 commit db2c5b9

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

fs/f2fs/data.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3537,6 +3537,7 @@ static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi,
35373537
pgoff_t index = folio->index;
35383538
int err = 0;
35393539
block_t ori_blk_addr = NULL_ADDR;
3540+
bool cow_has_reserved_block = false;
35403541

35413542
/* If pos is beyond the end of file, reserve a new block in COW inode */
35423543
if ((pos & PAGE_MASK) >= i_size_read(inode))
@@ -3546,9 +3547,11 @@ static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi,
35463547
err = __find_data_block(cow_inode, index, blk_addr);
35473548
if (err) {
35483549
return err;
3549-
} else if (*blk_addr != NULL_ADDR) {
3550+
} else if (__is_valid_data_blkaddr(*blk_addr)) {
35503551
*use_cow = true;
35513552
return 0;
3553+
} else if (*blk_addr == NEW_ADDR) {
3554+
cow_has_reserved_block = true;
35523555
}
35533556

35543557
if (is_inode_flag_set(inode, FI_ATOMIC_REPLACE))
@@ -3561,10 +3564,13 @@ static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi,
35613564

35623565
reserve_block:
35633566
/* Finally, we should reserve a new block in COW inode for the update */
3564-
err = __reserve_data_block(cow_inode, index, blk_addr, node_changed);
3565-
if (err)
3566-
return err;
3567-
inc_atomic_write_cnt(inode);
3567+
if (!cow_has_reserved_block) {
3568+
err = __reserve_data_block(cow_inode, index, blk_addr,
3569+
node_changed);
3570+
if (err)
3571+
return err;
3572+
inc_atomic_write_cnt(inode);
3573+
}
35683574

35693575
if (ori_blk_addr != NULL_ADDR)
35703576
*blk_addr = ori_blk_addr;

0 commit comments

Comments
 (0)