btrfs: allow btrfs_truncate_block() to fallback to nocow for data space reservation
v4 cover btrfs: allow btrfs_truncate_block() to fallback to nocow for data space reservation
Before this patch, btrfs_truncate_block() never checks the NODATACOW bit, thus when we run out of data space, we can return ENOSPC for truncate for NODATACOW inode.
This patchset will address this problem by doing the same behavior as buffered write to address it.
Changelog:
v2:
- Rebased to misc-next
Only one minor conflict in ctree.h
v3:
- Added two new patches
- Refactor check_can_nocow()
Since the introduction of nowait, check_can_nocow() are in fact split into two usage patterns: check_can_nocow(nowait = false) with btrfs_drew_write_unlock(), and single check_can_nocow(nowait = true). Refactor them into two functions: start_nocow_check() paired with end_nocow_check(), and single try_nocow_check(). With comment added.
- Rebased to latest misc-next
- Added btrfs_assert_drew_write_locked() for btrfs_end_nocow_check() This is a little concerning one, as it's in the hot path of buffered write.
It has percpu_counter_sum() called in that hot path, causing obvious performance drop for CONFIG_BTRFS_DEBUG build. Not sure if the assert is worthy since there aren't any other users.
v4:
- Rebased to latest misc-next
- Comment update to follow the relaxed kernel-doc format
- Re-order the patches
So the fix comes first, then refactors.
- Naming updates for the refactor patch
Now the exported pair is btrfs_check_nocow_lock() and btrfs_check_nocow_unlock().
- Remove the btrfs_assert_drew_write_lock()
It's extremely slow for btrfs/187, and we only have two call sites so far, thus it's not really worthy.
Qu Wenruo (3):
btrfs: allow btrfs_truncate_block() to fallback to nocow for data
space reservation
btrfs: add comments for btrfs_check_can_nocow() and can_nocow_extent()
btrfs: refactor btrfs_check_can_nocow() into two variants
fs/btrfs/ctree.h | 3 +++ fs/btrfs/file.c | 62 ++++++++++++++++++++++++++++++++++++----------- fs/btrfs/inode.c | 63 +++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 105 insertions(+), 23 deletions(-)
Signature · 1 line
2.27.0
PATCH v4 1/3 btrfs: allow btrfs_truncate_block() to fallback to nocow for data space reservation
[BUG]
When the data space is exhausted, even the inode has NOCOW attribute, btrfs will still refuse to truncate unaligned range due to ENOSPC.
The following script can reproduce it pretty easily:
#!/bin/bash
dev=/dev/test/test
mnt=/mnt/btrfs
umount $dev &> /dev/null
umount $mnt&> /dev/null
mkfs.btrfs -f $dev -b 1G
mount -o nospace_cache $dev $mnt
touch $mnt/foobar
chattr +C $mnt/foobar
xfs_io -f -c "pwrite -b 4k 0 4k" $mnt/foobar > /dev/null xfs_io -f -c "pwrite -b 4k 0 1G" $mnt/padding &> /dev/null sync
xfs_io -c "fpunch 0 2k" $mnt/foobar
umount $mnt
Current btrfs will fail at the fpunch part.
[CAUSE]
Because btrfs_truncate_block() always reserve space without checking the NOCOW attribute.
Since the writeback path follows NOCOW bit, we only need to bother the space reservation code in btrfs_truncate_block().
[FIX]
Make btrfs_truncate_block() to follow btrfs_buffered_write() to try to reserve data space first, and falls back to NOCOW check only when we don't have enough space.
Such always-try-reserve is an optimization introduced in btrfs_buffered_write(), to avoid expensive btrfs_check_can_nocow() call.
This patch will export check_can_nocow() as btrfs_check_can_nocow(), and use it in btrfs_truncate_block() to fix the problem.
--- fs/btrfs/ctree.h | 2 ++ fs/btrfs/file.c | 12 ++++++------ fs/btrfs/inode.c | 44 +++++++++++++++++++++++++++++++++++++------- 3 files changed, 45 insertions(+), 13 deletions(-)
fs/btrfs/ctree.h +2 -0
@@ -3035,6 +3035,8 @@ int btrfs_dirty_pages(struct inode *inode, struct page **pages, size_t num_pages, loff_t pos, size_t write_bytes, struct extent_state **cached);int btrfs_fdatawrite_range (struct inode *inode,loff_t start,loff_t end); +int btrfs_check_can_nocow (struct btrfs_inode *inode,loff_t pos, +size_t *write_bytes,bool nowait);/* tree-defrag.c */ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
fs/btrfs/file.c +6 -6
@@ -1533,8 +1533,8 @@ lock_and_cleanup_extent_if_need(struct btrfs_inode *inode, struct page **pages,return ret; } -static noinline int check_can_nocow(struct btrfs_inode *inode, loff_t pos, -size_t *write_bytes,bool nowait) +int btrfs_check_can_nocow (struct btrfs_inode *inode,loff_t pos, +size_t *write_bytes,bool nowait) {struct btrfs_fs_info *fs_info = inode->root->fs_info;struct btrfs_root *root = inode->root;
@@ -1649,8 +1649,8 @@ static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb,if (ret <0 ) {if ((BTRFS_I (inode)->flags & (BTRFS_INODE_NODATACOW |BTRFS_INODE_PREALLOC )) && -check_can_nocow (BTRFS_I (inode), pos, - &write_bytes, false) >0 ) { +btrfs_check_can_nocow (BTRFS_I (inode), pos, + &write_bytes, false) >0 ) {/* * For nodata cow case, no need to reserve * data space.
@@ -1927,8 +1927,8 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb,*/ if (!(BTRFS_I (inode)->flags & (BTRFS_INODE_NODATACOW |BTRFS_INODE_PREALLOC )) || -check_can_nocow (BTRFS_I (inode), pos, &nocow_bytes, - true) <=0 ) { +btrfs_check_can_nocow (BTRFS_I (inode), pos, &nocow_bytes, + true) <=0 ) {inode_unlock (inode);return -EAGAIN ; }
fs/btrfs/inode.c +37 -7
73 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v4 2/3 btrfs: add comments for btrfs_check_can_nocow() and can_nocow_extent()
In reply to Qu WenruoThese two functions have extra conditions that their callers need to meet, and some not-that-common parameters used for return value.
So adding some comments may save reviewers some time.
--- fs/btrfs/file.c | 21 +++++++++++++++++++++ fs/btrfs/inode.c | 21 +++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-)
fs/btrfs/file.c +21 -0
@@ -1533,6 +1533,27 @@ lock_and_cleanup_extent_if_need(struct btrfs_inode *inode, struct page **pages,return ret; } +/* +* Check if we can do nocow write into the range [@pos, @pos + @write_bytes) +* +* @pos: File offset +* @write_bytes: The length to write, will be updated to the nocow writeable +* range +* @nowait: Whether this function could sleep. +* +* This function will flush ordered extents in the range to ensure proper +* nocow checks for (nowait == false) case. +* +* Return: +* >0 and update @write_bytes if we can do nocow write. +* 0 if we can't do nocow write. +* -EAGAIN if we can't get the needed lock or there are ordered extents for +* (nowait == true) case. +* <0 if other error happened. +* +* NOTE: For wait (nowait==false) calls, callers need to release the drew write +* lock of inode->root->snapshot_lock when return value > 0. +*/ int btrfs_check_can_nocow (struct btrfs_inode *inode,loff_t pos,size_t *write_bytes,bool nowait) {
fs/btrfs/inode.c +19 -2
@@ -6952,8 +6952,25 @@ static struct extent_map *btrfs_new_extent_direct(struct inode *inode, }/* -* returns 1 when the nocow is safe, < 1 on error, 0 if the -* block must be cow'd +* Check if we can do nocow write into the range [@offset, @offset+ @len) +* +* @offset: File offset +* @len: The length to write, will be updated to the nocow writeable +* range +* @orig_start: (Optional) Return the original file offset of the file extent +* @orig_len: (Optional) Return the original on-disk length of the file extent +* @ram_bytes: (Optional) Return the ram_bytes of the file extent +* +* This function will flush ordered extents in the range to ensure proper +* nocow checks for (nowait == false) case. +* +* Return: +* >0 and update @len if we can do nocow write. +* 0 if we can't do nocow write. +* <0 if error happened. +* +* NOTE: This only checks the file extents, caller is responsible to wait for +* any ordered extents. */ noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, u64 *orig_start, u64 *orig_block_len,
Signature · 1 line
2.27.0
Re: [PATCH v4 2/3] btrfs: add comments for btrfs_check_can_nocow() and can_nocow_extent()
Looks good,
PATCH v4 3/3 btrfs: refactor btrfs_check_can_nocow() into two variants
In reply to Qu WenruoThe function btrfs_check_can_nocow() now has two completely different call patterns.
For nowait variant, callers don't need to do any cleanup. While for wait variant, callers need to release the lock if they can do nocow write.
This is somehow confusing, and is already a problem for the exported btrfs_check_can_nocow().
So this patch will separate the different patterns into different functions.
For nowait variant, the function will be called check_nocow_nolock(). For wait variant, the function pair will be btrfs_check_nocow_lock() btrfs_check_nocow_unlock().
--- fs/btrfs/ctree.h | 5 +-- fs/btrfs/file.c | 83 ++++++++++++++++++++++++++++-------------------- fs/btrfs/inode.c | 6 ++-- 3 files changed, 53 insertions(+), 41 deletions(-)
fs/btrfs/ctree.h +3 -2
@@ -3035,8 +3035,9 @@ int btrfs_dirty_pages(struct inode *inode, struct page **pages, size_t num_pages, loff_t pos, size_t write_bytes, struct extent_state **cached);int btrfs_fdatawrite_range (struct inode *inode,loff_t start,loff_t end); -int btrfs_check_can_nocow (struct btrfs_inode *inode,loff_t pos, -size_t *write_bytes,bool nowait); +int btrfs_check_nocow_lock (struct btrfs_inode *inode,loff_t pos, +size_t *write_bytes); +void btrfs_check_nocow_unlock (struct btrfs_inode *inode);/* tree-defrag.c */ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
fs/btrfs/file.c +48 -35
137 lines not on this page · raw message
fs/btrfs/inode.c +2 -4
@@ -4541,10 +4541,8 @@ int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, ret =btrfs_check_data_free_space (inode, &data_reserved, block_start, blocksize);if (ret <0 ) { -if ((BTRFS_I (inode)->flags & (BTRFS_INODE_NODATACOW | -BTRFS_INODE_PREALLOC )) && -btrfs_check_can_nocow (BTRFS_I (inode), block_start, - &write_bytes, false) >0 ) { +if (btrfs_check_nocow_lock (BTRFS_I (inode), block_start, + &write_bytes) >0 ) {/* For nocow case, no need to reserve data space. */ only_release_metadata = true; }else {
Signature · 1 line
2.27.0
Re: [PATCH v4 3/3] btrfs: refactor btrfs_check_can_nocow() into two variants
Looks good,
Re: [PATCH v4 3/3] btrfs: refactor btrfs_check_can_nocow() into two variants
In reply to Qu WenruoQuoted 5 lines
fs/btrfs/file.c +2
-0+static int check_nocow_nolock (struct btrfs_inode *inode,loff_t pos, +size_t *write_bytes)
fs/btrfs/file.c int btrfs_check_can_nocow(struct btrfs_inode *inode, loff_t pos, · line 1619
Quoted 10 lines
fs/btrfs/file.c +8
-0+{ +return check_can_nocow (inode, pos, write_bytes, false); +} +int btrfs_check_nocow_lock (struct btrfs_inode *inode,loff_t pos, +size_t *write_bytes) +{ +return check_can_nocow (inode, pos, write_bytes, false); +}
Both functions are same. Something obviously not ok.
Thanks, Anand
Re: [PATCH v4 3/3] btrfs: refactor btrfs_check_can_nocow() into two variants
Quoted 10 lines · Anand Jain
Quoted 8 lines
fs/btrfs/file.c +5
-0+static int check_nocow_nolock (struct btrfs_inode *inode,loff_t pos, +size_t *write_bytes) +{ +return check_can_nocow (inode, pos, write_bytes, false); +}
Quoted 8 lines
fs/btrfs/file.c int btrfs_check_can_nocow(struct btrfs_inode *inode, loff_t pos, · line 1619
Quoted 5 lines
fs/btrfs/file.c +5
-0+int btrfs_check_nocow_lock (struct btrfs_inode *inode,loff_t pos, +size_t *write_bytes) +{ +return check_can_nocow (inode, pos, write_bytes, false); +}Both functions are same. Something obviously not ok.
Oh right, how could I have missed that. The _lock will need 'true' for the 3rd parameter.
Re: [PATCH v4 3/3] btrfs: refactor btrfs_check_can_nocow() into two variants
Quoted 20 lines · Johannes Thumshirn
Quoted 10 lines · Anand Jain
Quoted 8 lines
fs/btrfs/file.c +5
-0+static int check_nocow_nolock (struct btrfs_inode *inode,loff_t pos, +size_t *write_bytes) +{ +return check_can_nocow (inode, pos, write_bytes, false); +}Quoted 8 lines
fs/btrfs/file.c int btrfs_check_can_nocow(struct btrfs_inode *inode, loff_t pos, · line 1619
Quoted 5 lines
fs/btrfs/file.c +5
-0+int btrfs_check_nocow_lock (struct btrfs_inode *inode,loff_t pos, +size_t *write_bytes) +{ +return check_can_nocow (inode, pos, write_bytes, false); +}Both functions are same. Something obviously not ok.
Quoted 3 lines
Oh right, how could I have missed that. The
_lockwill need 'true' for the 3rd parameter.
Holly sh*t, how could the test passed without extra failures.
Definitely the most embarrassing bug...
Thanks for pointing it out,
Qu