ext4 (like any other journaling fs I guess) has a problem with estimating journal reservations required for particular fs operation. swapext is pretty complicated operation, its exact volume depends on set of extents currently present in the inodes. Even though existing estimate works, there is no warranty under some circumstances calculated credits are not enough to complete the transaction. This patch suggests a safe way to adjust credits in case initial estimate was wrong without fatal impact on journal: it checks credits before each pass, extends the handle if needed, and otherwise restarts the transaction and continues from the last completed pass.
The patch is suggested by Claude. It looks reasonable, the technique is borrowed from ext4_block_write_begin(). Affects: #VSTOR-148028 Signed-off-by: Alexey Kuznetsov <[email protected]> --- fs/ext4/extents.c | 33 +++++++++++++++++++++++++++++++++ fs/ext4/move_extent.c | 6 ++++++ 2 files changed, 39 insertions(+) diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index eca3f0312ac5..6ffee24cf386 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -5821,6 +5821,7 @@ ext4_swap_extents_2(handle_t *handle, struct inode *inode1, struct ext4_ext_path *path1 = NULL; struct ext4_ext_path *path2 = NULL; int replaced_count = 0; + int passes = 0; BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem)); BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem)); @@ -5834,6 +5835,26 @@ ext4_swap_extents_2(handle_t *handle, struct inode *inode1, int e1_len, e2_len, len; int split = 0; + /* + * A pass either splits at most one extent in each inode or + * swaps one pair of extents. Running out of credits in the + * middle of a pass aborts the journal, so before each pass + * (but the first one, which the caller has reserved for) make + * sure the handle can afford the worst case. If it cannot, + * return to the caller with -EAGAIN, it will restart the + * transaction and continue from lblk1 + replaced_count. + */ + if (passes++) { + int credits = ext4_chunk_trans_blocks(inode1, 1) + + ext4_chunk_trans_blocks(inode2, 1); + int ret = __ext4_journal_ensure_credits(handle, credits, + credits, 0); + if (ret) { + *erp = ret < 0 ? ret : -EAGAIN; + break; + } + } + path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE); if (IS_ERR(path1)) { *erp = PTR_ERR(path1); @@ -5993,6 +6014,7 @@ ext4_expose_extents_2(handle_t *handle, struct inode *inode, { struct ext4_ext_path *path = NULL; int replaced_count = 0; + int passes = 0; ext4_es_remove_extent(inode, lblk, count); @@ -6002,6 +6024,17 @@ ext4_expose_extents_2(handle_t *handle, struct inode *inode, int e_len, len; int split = 0; + /* See comment in ext4_swap_extents_2() */ + if (passes++) { + int credits = ext4_chunk_trans_blocks(inode, 1); + int ret = __ext4_journal_ensure_credits(handle, credits, + credits, 0); + if (ret) { + *erp = ret < 0 ? ret : -EAGAIN; + break; + } + } + path = ext4_find_extent(inode, lblk, NULL, EXT4_EX_NOCACHE); if (IS_ERR(path)) { *erp = PTR_ERR(path); diff --git a/fs/ext4/move_extent.c b/fs/ext4/move_extent.c index 0e8d1366da7a..dfbdb571105f 100644 --- a/fs/ext4/move_extent.c +++ b/fs/ext4/move_extent.c @@ -743,6 +743,9 @@ swap_one_extent(struct file *o_filp, struct inode *donor_inode, se->s_dirty += handle->h_requested_credits - handle->h_total_credits; ext4_journal_stop(handle); + /* Handle is out of credits, continue in a new transaction */ + if (*err == -EAGAIN) + goto again; if (*err == -ENOSPC && ext4_should_retry_alloc(sb, &retries)) goto again; @@ -782,6 +785,9 @@ expose_one_extent(struct inode *donor_inode, se->s_dirty += handle->h_requested_credits - handle->h_total_credits; ext4_journal_stop(handle); + /* Handle is out of credits, continue in a new transaction */ + if (*err == -EAGAIN) + goto again; if (*err == -ENOSPC && ext4_should_retry_alloc(sb, &retries)) goto again; -- 2.47.1 _______________________________________________ Devel mailing list [email protected] https://lists.openvz.org/mailman/listinfo/devel
