2019-06-19 19:12:00 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
2022-10-19 14:50:49 +00:00
|
|
|
#include "messages.h"
|
2019-06-19 19:12:00 +00:00
|
|
|
#include "ctree.h"
|
|
|
|
#include "delalloc-space.h"
|
|
|
|
#include "block-rsv.h"
|
|
|
|
#include "btrfs_inode.h"
|
|
|
|
#include "space-info.h"
|
|
|
|
#include "qgroup.h"
|
2022-10-19 14:50:51 +00:00
|
|
|
#include "fs.h"
|
2019-06-19 19:12:00 +00:00
|
|
|
|
2020-02-04 18:18:55 +00:00
|
|
|
/*
|
|
|
|
* HOW DOES THIS WORK
|
|
|
|
*
|
|
|
|
* There are two stages to data reservations, one for data and one for metadata
|
|
|
|
* to handle the new extents and checksums generated by writing data.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* DATA RESERVATION
|
|
|
|
* The general flow of the data reservation is as follows
|
|
|
|
*
|
|
|
|
* -> Reserve
|
|
|
|
* We call into btrfs_reserve_data_bytes() for the user request bytes that
|
|
|
|
* they wish to write. We make this reservation and add it to
|
|
|
|
* space_info->bytes_may_use. We set EXTENT_DELALLOC on the inode io_tree
|
|
|
|
* for the range and carry on if this is buffered, or follow up trying to
|
|
|
|
* make a real allocation if we are pre-allocating or doing O_DIRECT.
|
|
|
|
*
|
|
|
|
* -> Use
|
|
|
|
* At writepages()/prealloc/O_DIRECT time we will call into
|
|
|
|
* btrfs_reserve_extent() for some part or all of this range of bytes. We
|
|
|
|
* will make the allocation and subtract space_info->bytes_may_use by the
|
|
|
|
* original requested length and increase the space_info->bytes_reserved by
|
|
|
|
* the allocated length. This distinction is important because compression
|
|
|
|
* may allocate a smaller on disk extent than we previously reserved.
|
|
|
|
*
|
|
|
|
* -> Allocation
|
|
|
|
* finish_ordered_io() will insert the new file extent item for this range,
|
|
|
|
* and then add a delayed ref update for the extent tree. Once that delayed
|
|
|
|
* ref is written the extent size is subtracted from
|
|
|
|
* space_info->bytes_reserved and added to space_info->bytes_used.
|
|
|
|
*
|
|
|
|
* Error handling
|
|
|
|
*
|
|
|
|
* -> By the reservation maker
|
|
|
|
* This is the simplest case, we haven't completed our operation and we know
|
|
|
|
* how much we reserved, we can simply call
|
|
|
|
* btrfs_free_reserved_data_space*() and it will be removed from
|
|
|
|
* space_info->bytes_may_use.
|
|
|
|
*
|
|
|
|
* -> After the reservation has been made, but before cow_file_range()
|
|
|
|
* This is specifically for the delalloc case. You must clear
|
|
|
|
* EXTENT_DELALLOC with the EXTENT_CLEAR_DATA_RESV bit, and the range will
|
|
|
|
* be subtracted from space_info->bytes_may_use.
|
|
|
|
*
|
|
|
|
* METADATA RESERVATION
|
|
|
|
* The general metadata reservation lifetimes are discussed elsewhere, this
|
|
|
|
* will just focus on how it is used for delalloc space.
|
|
|
|
*
|
|
|
|
* We keep track of two things on a per inode bases
|
|
|
|
*
|
|
|
|
* ->outstanding_extents
|
|
|
|
* This is the number of file extent items we'll need to handle all of the
|
|
|
|
* outstanding DELALLOC space we have in this inode. We limit the maximum
|
|
|
|
* size of an extent, so a large contiguous dirty area may require more than
|
|
|
|
* one outstanding_extent, which is why count_max_extents() is used to
|
|
|
|
* determine how many outstanding_extents get added.
|
|
|
|
*
|
|
|
|
* ->csum_bytes
|
|
|
|
* This is essentially how many dirty bytes we have for this inode, so we
|
|
|
|
* can calculate the number of checksum items we would have to add in order
|
|
|
|
* to checksum our outstanding data.
|
|
|
|
*
|
|
|
|
* We keep a per-inode block_rsv in order to make it easier to keep track of
|
|
|
|
* our reservation. We use btrfs_calculate_inode_block_rsv_size() to
|
|
|
|
* calculate the current theoretical maximum reservation we would need for the
|
|
|
|
* metadata for this inode. We call this and then adjust our reservation as
|
|
|
|
* necessary, either by attempting to reserve more space, or freeing up excess
|
|
|
|
* space.
|
|
|
|
*
|
|
|
|
* OUTSTANDING_EXTENTS HANDLING
|
|
|
|
*
|
|
|
|
* ->outstanding_extents is used for keeping track of how many extents we will
|
|
|
|
* need to use for this inode, and it will fluctuate depending on where you are
|
|
|
|
* in the life cycle of the dirty data. Consider the following normal case for
|
|
|
|
* a completely clean inode, with a num_bytes < our maximum allowed extent size
|
|
|
|
*
|
|
|
|
* -> reserve
|
|
|
|
* ->outstanding_extents += 1 (current value is 1)
|
|
|
|
*
|
|
|
|
* -> set_delalloc
|
2021-05-21 15:42:23 +00:00
|
|
|
* ->outstanding_extents += 1 (current value is 2)
|
2020-02-04 18:18:55 +00:00
|
|
|
*
|
|
|
|
* -> btrfs_delalloc_release_extents()
|
|
|
|
* ->outstanding_extents -= 1 (current value is 1)
|
|
|
|
*
|
|
|
|
* We must call this once we are done, as we hold our reservation for the
|
|
|
|
* duration of our operation, and then assume set_delalloc will update the
|
|
|
|
* counter appropriately.
|
|
|
|
*
|
|
|
|
* -> add ordered extent
|
|
|
|
* ->outstanding_extents += 1 (current value is 2)
|
|
|
|
*
|
|
|
|
* -> btrfs_clear_delalloc_extent
|
|
|
|
* ->outstanding_extents -= 1 (current value is 1)
|
|
|
|
*
|
|
|
|
* -> finish_ordered_io/btrfs_remove_ordered_extent
|
|
|
|
* ->outstanding_extents -= 1 (current value is 0)
|
|
|
|
*
|
|
|
|
* Each stage is responsible for their own accounting of the extent, thus
|
|
|
|
* making error handling and cleanup easier.
|
|
|
|
*/
|
|
|
|
|
2019-06-19 19:12:00 +00:00
|
|
|
int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
|
|
|
|
{
|
|
|
|
struct btrfs_root *root = inode->root;
|
|
|
|
struct btrfs_fs_info *fs_info = root->fs_info;
|
2020-07-21 14:22:25 +00:00
|
|
|
enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_DATA;
|
2019-06-19 19:12:00 +00:00
|
|
|
|
|
|
|
/* Make sure bytes are sectorsize aligned */
|
|
|
|
bytes = ALIGN(bytes, fs_info->sectorsize);
|
|
|
|
|
2020-07-21 14:22:25 +00:00
|
|
|
if (btrfs_is_free_space_inode(inode))
|
|
|
|
flush = BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE;
|
2019-06-19 19:12:00 +00:00
|
|
|
|
2020-07-21 14:22:25 +00:00
|
|
|
return btrfs_reserve_data_bytes(fs_info, bytes, flush);
|
2019-06-19 19:12:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 05:55:41 +00:00
|
|
|
int btrfs_check_data_free_space(struct btrfs_inode *inode,
|
2022-09-12 19:27:44 +00:00
|
|
|
struct extent_changeset **reserved, u64 start,
|
|
|
|
u64 len, bool noflush)
|
2019-06-19 19:12:00 +00:00
|
|
|
{
|
2020-06-03 05:55:41 +00:00
|
|
|
struct btrfs_fs_info *fs_info = inode->root->fs_info;
|
2022-09-12 19:27:44 +00:00
|
|
|
enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_DATA;
|
2019-06-19 19:12:00 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* align the range */
|
|
|
|
len = round_up(start + len, fs_info->sectorsize) -
|
|
|
|
round_down(start, fs_info->sectorsize);
|
|
|
|
start = round_down(start, fs_info->sectorsize);
|
|
|
|
|
2022-09-12 19:27:44 +00:00
|
|
|
if (noflush)
|
|
|
|
flush = BTRFS_RESERVE_NO_FLUSH;
|
|
|
|
else if (btrfs_is_free_space_inode(inode))
|
|
|
|
flush = BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE;
|
|
|
|
|
|
|
|
ret = btrfs_reserve_data_bytes(fs_info, len, flush);
|
2019-06-19 19:12:00 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
|
2020-06-03 05:55:41 +00:00
|
|
|
ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
|
2021-12-03 10:55:33 +00:00
|
|
|
if (ret < 0) {
|
2020-06-03 05:55:38 +00:00
|
|
|
btrfs_free_reserved_data_space_noquota(fs_info, len);
|
2021-12-03 10:55:33 +00:00
|
|
|
extent_changeset_free(*reserved);
|
|
|
|
*reserved = NULL;
|
|
|
|
} else {
|
2019-06-19 19:12:00 +00:00
|
|
|
ret = 0;
|
2021-12-03 10:55:33 +00:00
|
|
|
}
|
2019-06-19 19:12:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called if we need to clear a data reservation for this inode
|
|
|
|
* Normally in a error case.
|
|
|
|
*
|
|
|
|
* This one will *NOT* use accurate qgroup reserved space API, just for case
|
|
|
|
* which we can't sleep and is sure it won't affect qgroup reserved space.
|
|
|
|
* Like clear_bit_hook().
|
|
|
|
*/
|
2020-06-03 05:55:38 +00:00
|
|
|
void btrfs_free_reserved_data_space_noquota(struct btrfs_fs_info *fs_info,
|
2019-06-19 19:12:00 +00:00
|
|
|
u64 len)
|
|
|
|
{
|
|
|
|
struct btrfs_space_info *data_sinfo;
|
|
|
|
|
2020-06-09 10:19:33 +00:00
|
|
|
ASSERT(IS_ALIGNED(len, fs_info->sectorsize));
|
2019-06-19 19:12:00 +00:00
|
|
|
|
|
|
|
data_sinfo = fs_info->data_sinfo;
|
2020-07-21 14:22:20 +00:00
|
|
|
btrfs_space_info_free_bytes_may_use(fs_info, data_sinfo, len);
|
2019-06-19 19:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called if we need to clear a data reservation for this inode
|
|
|
|
* Normally in a error case.
|
|
|
|
*
|
|
|
|
* This one will handle the per-inode data rsv map for accurate reserved
|
|
|
|
* space framework.
|
|
|
|
*/
|
2020-06-03 05:55:39 +00:00
|
|
|
void btrfs_free_reserved_data_space(struct btrfs_inode *inode,
|
2019-06-19 19:12:00 +00:00
|
|
|
struct extent_changeset *reserved, u64 start, u64 len)
|
|
|
|
{
|
2020-06-03 05:55:39 +00:00
|
|
|
struct btrfs_fs_info *fs_info = inode->root->fs_info;
|
2019-06-19 19:12:00 +00:00
|
|
|
|
|
|
|
/* Make sure the range is aligned to sectorsize */
|
2020-06-03 05:55:39 +00:00
|
|
|
len = round_up(start + len, fs_info->sectorsize) -
|
|
|
|
round_down(start, fs_info->sectorsize);
|
|
|
|
start = round_down(start, fs_info->sectorsize);
|
2019-06-19 19:12:00 +00:00
|
|
|
|
2020-06-03 05:55:39 +00:00
|
|
|
btrfs_free_reserved_data_space_noquota(fs_info, len);
|
2023-12-01 21:00:10 +00:00
|
|
|
btrfs_qgroup_free_data(inode, reserved, start, len, NULL);
|
2019-06-19 19:12:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 12:21:42 +00:00
|
|
|
/*
|
|
|
|
* Release any excessive reservations for an inode.
|
2021-01-22 09:58:01 +00:00
|
|
|
*
|
|
|
|
* @inode: the inode we need to release from
|
|
|
|
* @qgroup_free: free or convert qgroup meta. Unlike normal operation, qgroup
|
|
|
|
* meta reservation needs to know if we are freeing qgroup
|
|
|
|
* reservation or just converting it into per-trans. Normally
|
|
|
|
* @qgroup_free is true for error handling, and false for normal
|
|
|
|
* release.
|
2019-06-19 19:12:00 +00:00
|
|
|
*
|
|
|
|
* This is the same as btrfs_block_rsv_release, except that it handles the
|
|
|
|
* tracepoint for the reservation.
|
|
|
|
*/
|
|
|
|
static void btrfs_inode_rsv_release(struct btrfs_inode *inode, bool qgroup_free)
|
|
|
|
{
|
|
|
|
struct btrfs_fs_info *fs_info = inode->root->fs_info;
|
|
|
|
struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
|
|
|
|
u64 released = 0;
|
|
|
|
u64 qgroup_to_release = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since we statically set the block_rsv->size we just want to say we
|
|
|
|
* are releasing 0 bytes, and then we'll just get the reservation over
|
|
|
|
* the size free'd.
|
|
|
|
*/
|
2020-03-10 08:59:31 +00:00
|
|
|
released = btrfs_block_rsv_release(fs_info, block_rsv, 0,
|
|
|
|
&qgroup_to_release);
|
2019-06-19 19:12:00 +00:00
|
|
|
if (released > 0)
|
|
|
|
trace_btrfs_space_reservation(fs_info, "delalloc",
|
|
|
|
btrfs_ino(inode), released, 0);
|
|
|
|
if (qgroup_free)
|
|
|
|
btrfs_qgroup_free_meta_prealloc(inode->root, qgroup_to_release);
|
|
|
|
else
|
|
|
|
btrfs_qgroup_convert_reserved_meta(inode->root,
|
|
|
|
qgroup_to_release);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
|
|
|
|
struct btrfs_inode *inode)
|
|
|
|
{
|
|
|
|
struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
|
|
|
|
u64 reserve_size = 0;
|
|
|
|
u64 qgroup_rsv_size = 0;
|
|
|
|
unsigned outstanding_extents;
|
|
|
|
|
|
|
|
lockdep_assert_held(&inode->lock);
|
|
|
|
outstanding_extents = inode->outstanding_extents;
|
2019-08-22 19:14:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert size for the number of outstanding extents, 1 normal size for
|
|
|
|
* updating the inode.
|
|
|
|
*/
|
|
|
|
if (outstanding_extents) {
|
2019-08-22 19:14:33 +00:00
|
|
|
reserve_size = btrfs_calc_insert_metadata_size(fs_info,
|
2019-08-22 19:14:34 +00:00
|
|
|
outstanding_extents);
|
|
|
|
reserve_size += btrfs_calc_metadata_size(fs_info, 1);
|
|
|
|
}
|
2024-01-31 17:18:04 +00:00
|
|
|
if (!(inode->flags & BTRFS_INODE_NODATASUM)) {
|
|
|
|
u64 csum_leaves;
|
|
|
|
|
|
|
|
csum_leaves = btrfs_csum_bytes_to_leaves(fs_info, inode->csum_bytes);
|
|
|
|
reserve_size += btrfs_calc_insert_metadata_size(fs_info, csum_leaves);
|
|
|
|
}
|
2019-06-19 19:12:00 +00:00
|
|
|
/*
|
|
|
|
* For qgroup rsv, the calculation is very simple:
|
|
|
|
* account one nodesize for each outstanding extent
|
|
|
|
*
|
|
|
|
* This is overestimating in most cases.
|
|
|
|
*/
|
|
|
|
qgroup_rsv_size = (u64)outstanding_extents * fs_info->nodesize;
|
|
|
|
|
|
|
|
spin_lock(&block_rsv->lock);
|
|
|
|
block_rsv->size = reserve_size;
|
|
|
|
block_rsv->qgroup_rsv_size = qgroup_rsv_size;
|
|
|
|
spin_unlock(&block_rsv->lock);
|
|
|
|
}
|
|
|
|
|
2024-01-31 17:18:04 +00:00
|
|
|
static void calc_inode_reservations(struct btrfs_inode *inode,
|
2019-11-19 06:45:55 +00:00
|
|
|
u64 num_bytes, u64 disk_num_bytes,
|
|
|
|
u64 *meta_reserve, u64 *qgroup_reserve)
|
2019-06-19 19:12:00 +00:00
|
|
|
{
|
2024-01-31 17:18:04 +00:00
|
|
|
struct btrfs_fs_info *fs_info = inode->root->fs_info;
|
2022-07-08 23:18:41 +00:00
|
|
|
u64 nr_extents = count_max_extents(fs_info, num_bytes);
|
2024-01-31 17:18:04 +00:00
|
|
|
u64 csum_leaves;
|
2019-08-22 19:14:34 +00:00
|
|
|
u64 inode_update = btrfs_calc_metadata_size(fs_info, 1);
|
2019-06-19 19:12:00 +00:00
|
|
|
|
2024-01-31 17:18:04 +00:00
|
|
|
if (inode->flags & BTRFS_INODE_NODATASUM)
|
|
|
|
csum_leaves = 0;
|
|
|
|
else
|
|
|
|
csum_leaves = btrfs_csum_bytes_to_leaves(fs_info, disk_num_bytes);
|
|
|
|
|
2019-08-22 19:14:33 +00:00
|
|
|
*meta_reserve = btrfs_calc_insert_metadata_size(fs_info,
|
2019-08-22 19:14:34 +00:00
|
|
|
nr_extents + csum_leaves);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* finish_ordered_io has to update the inode, so add the space required
|
|
|
|
* for an inode update.
|
|
|
|
*/
|
|
|
|
*meta_reserve += inode_update;
|
2019-06-19 19:12:00 +00:00
|
|
|
*qgroup_reserve = nr_extents * fs_info->nodesize;
|
|
|
|
}
|
|
|
|
|
2019-11-19 06:45:55 +00:00
|
|
|
int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes,
|
btrfs: avoid blocking on space revervation when doing nowait dio writes
When doing a NOWAIT direct IO write, if we can NOCOW then it means we can
proceed with the non-blocking, NOWAIT path. However reserving the metadata
space and qgroup meta space can often result in blocking - flushing
delalloc, wait for ordered extents to complete, trigger transaction
commits, etc, going against the semantics of a NOWAIT write.
So make the NOWAIT write path to try to reserve all the metadata it needs
without resulting in a blocking behaviour - if we get -ENOSPC or -EDQUOT
then return -EAGAIN to make the caller fallback to a blocking direct IO
write.
This is part of a patchset comprised of the following patches:
btrfs: avoid blocking on page locks with nowait dio on compressed range
btrfs: avoid blocking nowait dio when locking file range
btrfs: avoid double nocow check when doing nowait dio writes
btrfs: stop allocating a path when checking if cross reference exists
btrfs: free path at can_nocow_extent() before checking for checksum items
btrfs: release path earlier at can_nocow_extent()
btrfs: avoid blocking when allocating context for nowait dio read/write
btrfs: avoid blocking on space revervation when doing nowait dio writes
The following test was run before and after applying this patchset:
$ cat io-uring-nodatacow-test.sh
#!/bin/bash
DEV=/dev/sdc
MNT=/mnt/sdc
MOUNT_OPTIONS="-o ssd -o nodatacow"
MKFS_OPTIONS="-R free-space-tree -O no-holes"
NUM_JOBS=4
FILE_SIZE=8G
RUN_TIME=300
cat <<EOF > /tmp/fio-job.ini
[io_uring_rw]
rw=randrw
fsync=0
fallocate=posix
group_reporting=1
direct=1
ioengine=io_uring
iodepth=64
bssplit=4k/20:8k/20:16k/20:32k/10:64k/10:128k/5:256k/5:512k/5:1m/5
filesize=$FILE_SIZE
runtime=$RUN_TIME
time_based
filename=foobar
directory=$MNT
numjobs=$NUM_JOBS
thread
EOF
echo performance | \
tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
umount $MNT &> /dev/null
mkfs.btrfs -f $MKFS_OPTIONS $DEV &> /dev/null
mount $MOUNT_OPTIONS $DEV $MNT
fio /tmp/fio-job.ini
umount $MNT
The test was run a 12 cores box with 64G of ram, using a non-debug kernel
config (Debian's default config) and a spinning disk.
Result before the patchset:
READ: bw=407MiB/s (427MB/s), 407MiB/s-407MiB/s (427MB/s-427MB/s), io=119GiB (128GB), run=300175-300175msec
WRITE: bw=407MiB/s (427MB/s), 407MiB/s-407MiB/s (427MB/s-427MB/s), io=119GiB (128GB), run=300175-300175msec
Result after the patchset:
READ: bw=436MiB/s (457MB/s), 436MiB/s-436MiB/s (457MB/s-457MB/s), io=128GiB (137GB), run=300044-300044msec
WRITE: bw=435MiB/s (456MB/s), 435MiB/s-435MiB/s (456MB/s-456MB/s), io=128GiB (137GB), run=300044-300044msec
That's about +7.2% throughput for reads and +6.9% for writes.
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-03-23 16:19:30 +00:00
|
|
|
u64 disk_num_bytes, bool noflush)
|
2019-06-19 19:12:00 +00:00
|
|
|
{
|
|
|
|
struct btrfs_root *root = inode->root;
|
|
|
|
struct btrfs_fs_info *fs_info = root->fs_info;
|
|
|
|
struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
|
|
|
|
u64 meta_reserve, qgroup_reserve;
|
|
|
|
unsigned nr_extents;
|
|
|
|
enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are a free space inode we need to not flush since we will be in
|
|
|
|
* the middle of a transaction commit. We also don't need the delalloc
|
|
|
|
* mutex since we won't race with anybody. We need this mostly to make
|
|
|
|
* lockdep shut its filthy mouth.
|
|
|
|
*
|
|
|
|
* If we have a transaction open (can happen if we call truncate_block
|
|
|
|
* from truncate), then we need FLUSH_LIMIT so we don't deadlock.
|
|
|
|
*/
|
btrfs: avoid blocking on space revervation when doing nowait dio writes
When doing a NOWAIT direct IO write, if we can NOCOW then it means we can
proceed with the non-blocking, NOWAIT path. However reserving the metadata
space and qgroup meta space can often result in blocking - flushing
delalloc, wait for ordered extents to complete, trigger transaction
commits, etc, going against the semantics of a NOWAIT write.
So make the NOWAIT write path to try to reserve all the metadata it needs
without resulting in a blocking behaviour - if we get -ENOSPC or -EDQUOT
then return -EAGAIN to make the caller fallback to a blocking direct IO
write.
This is part of a patchset comprised of the following patches:
btrfs: avoid blocking on page locks with nowait dio on compressed range
btrfs: avoid blocking nowait dio when locking file range
btrfs: avoid double nocow check when doing nowait dio writes
btrfs: stop allocating a path when checking if cross reference exists
btrfs: free path at can_nocow_extent() before checking for checksum items
btrfs: release path earlier at can_nocow_extent()
btrfs: avoid blocking when allocating context for nowait dio read/write
btrfs: avoid blocking on space revervation when doing nowait dio writes
The following test was run before and after applying this patchset:
$ cat io-uring-nodatacow-test.sh
#!/bin/bash
DEV=/dev/sdc
MNT=/mnt/sdc
MOUNT_OPTIONS="-o ssd -o nodatacow"
MKFS_OPTIONS="-R free-space-tree -O no-holes"
NUM_JOBS=4
FILE_SIZE=8G
RUN_TIME=300
cat <<EOF > /tmp/fio-job.ini
[io_uring_rw]
rw=randrw
fsync=0
fallocate=posix
group_reporting=1
direct=1
ioengine=io_uring
iodepth=64
bssplit=4k/20:8k/20:16k/20:32k/10:64k/10:128k/5:256k/5:512k/5:1m/5
filesize=$FILE_SIZE
runtime=$RUN_TIME
time_based
filename=foobar
directory=$MNT
numjobs=$NUM_JOBS
thread
EOF
echo performance | \
tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
umount $MNT &> /dev/null
mkfs.btrfs -f $MKFS_OPTIONS $DEV &> /dev/null
mount $MOUNT_OPTIONS $DEV $MNT
fio /tmp/fio-job.ini
umount $MNT
The test was run a 12 cores box with 64G of ram, using a non-debug kernel
config (Debian's default config) and a spinning disk.
Result before the patchset:
READ: bw=407MiB/s (427MB/s), 407MiB/s-407MiB/s (427MB/s-427MB/s), io=119GiB (128GB), run=300175-300175msec
WRITE: bw=407MiB/s (427MB/s), 407MiB/s-407MiB/s (427MB/s-427MB/s), io=119GiB (128GB), run=300175-300175msec
Result after the patchset:
READ: bw=436MiB/s (457MB/s), 436MiB/s-436MiB/s (457MB/s-457MB/s), io=128GiB (137GB), run=300044-300044msec
WRITE: bw=435MiB/s (456MB/s), 435MiB/s-435MiB/s (456MB/s-456MB/s), io=128GiB (137GB), run=300044-300044msec
That's about +7.2% throughput for reads and +6.9% for writes.
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-03-23 16:19:30 +00:00
|
|
|
if (noflush || btrfs_is_free_space_inode(inode)) {
|
2019-06-19 19:12:00 +00:00
|
|
|
flush = BTRFS_RESERVE_NO_FLUSH;
|
|
|
|
} else {
|
|
|
|
if (current->journal_info)
|
|
|
|
flush = BTRFS_RESERVE_FLUSH_LIMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
|
2019-11-19 06:45:55 +00:00
|
|
|
disk_num_bytes = ALIGN(disk_num_bytes, fs_info->sectorsize);
|
2019-06-19 19:12:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We always want to do it this way, every other way is wrong and ends
|
|
|
|
* in tears. Pre-reserving the amount we are going to add will always
|
|
|
|
* be the right way, because otherwise if we have enough parallelism we
|
|
|
|
* could end up with thousands of inodes all holding little bits of
|
|
|
|
* reservations they were able to make previously and the only way to
|
|
|
|
* reclaim that space is to ENOSPC out the operations and clear
|
|
|
|
* everything out and try again, which is bad. This way we just
|
|
|
|
* over-reserve slightly, and clean up the mess when we are done.
|
|
|
|
*/
|
2024-01-31 17:18:04 +00:00
|
|
|
calc_inode_reservations(inode, num_bytes, disk_num_bytes,
|
2019-11-19 06:45:55 +00:00
|
|
|
&meta_reserve, &qgroup_reserve);
|
btrfs: avoid blocking on space revervation when doing nowait dio writes
When doing a NOWAIT direct IO write, if we can NOCOW then it means we can
proceed with the non-blocking, NOWAIT path. However reserving the metadata
space and qgroup meta space can often result in blocking - flushing
delalloc, wait for ordered extents to complete, trigger transaction
commits, etc, going against the semantics of a NOWAIT write.
So make the NOWAIT write path to try to reserve all the metadata it needs
without resulting in a blocking behaviour - if we get -ENOSPC or -EDQUOT
then return -EAGAIN to make the caller fallback to a blocking direct IO
write.
This is part of a patchset comprised of the following patches:
btrfs: avoid blocking on page locks with nowait dio on compressed range
btrfs: avoid blocking nowait dio when locking file range
btrfs: avoid double nocow check when doing nowait dio writes
btrfs: stop allocating a path when checking if cross reference exists
btrfs: free path at can_nocow_extent() before checking for checksum items
btrfs: release path earlier at can_nocow_extent()
btrfs: avoid blocking when allocating context for nowait dio read/write
btrfs: avoid blocking on space revervation when doing nowait dio writes
The following test was run before and after applying this patchset:
$ cat io-uring-nodatacow-test.sh
#!/bin/bash
DEV=/dev/sdc
MNT=/mnt/sdc
MOUNT_OPTIONS="-o ssd -o nodatacow"
MKFS_OPTIONS="-R free-space-tree -O no-holes"
NUM_JOBS=4
FILE_SIZE=8G
RUN_TIME=300
cat <<EOF > /tmp/fio-job.ini
[io_uring_rw]
rw=randrw
fsync=0
fallocate=posix
group_reporting=1
direct=1
ioengine=io_uring
iodepth=64
bssplit=4k/20:8k/20:16k/20:32k/10:64k/10:128k/5:256k/5:512k/5:1m/5
filesize=$FILE_SIZE
runtime=$RUN_TIME
time_based
filename=foobar
directory=$MNT
numjobs=$NUM_JOBS
thread
EOF
echo performance | \
tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
umount $MNT &> /dev/null
mkfs.btrfs -f $MKFS_OPTIONS $DEV &> /dev/null
mount $MOUNT_OPTIONS $DEV $MNT
fio /tmp/fio-job.ini
umount $MNT
The test was run a 12 cores box with 64G of ram, using a non-debug kernel
config (Debian's default config) and a spinning disk.
Result before the patchset:
READ: bw=407MiB/s (427MB/s), 407MiB/s-407MiB/s (427MB/s-427MB/s), io=119GiB (128GB), run=300175-300175msec
WRITE: bw=407MiB/s (427MB/s), 407MiB/s-407MiB/s (427MB/s-427MB/s), io=119GiB (128GB), run=300175-300175msec
Result after the patchset:
READ: bw=436MiB/s (457MB/s), 436MiB/s-436MiB/s (457MB/s-457MB/s), io=128GiB (137GB), run=300044-300044msec
WRITE: bw=435MiB/s (456MB/s), 435MiB/s-435MiB/s (456MB/s-456MB/s), io=128GiB (137GB), run=300044-300044msec
That's about +7.2% throughput for reads and +6.9% for writes.
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-03-23 16:19:30 +00:00
|
|
|
ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_reserve, true,
|
|
|
|
noflush);
|
2019-06-19 19:12:00 +00:00
|
|
|
if (ret)
|
Btrfs: remove unnecessary delalloc mutex for inodes
The inode delalloc mutex was added a long time ago by commit f248679e86fea
("Btrfs: add a delalloc mutex to inodes for delalloc reservations"), and
the reason for its introduction is not very clear from the change log. It
claims it solves bogus warnings from lockdep, however it lacks an example
report/warning from lockdep, or any explanation.
Since we have enough concurrentcy protection from the locks of the space
info and block reserve objects, and such lockdep warnings don't seem to
exist anymore (at least on a 5.3 kernel I couldn't get them with fstests,
ltp, fs_mark, etc), remove it, simplifying things a bit and decreasing
the size of the btrfs_inode structure. With some quick fio tests doing
direct IO and mmap writes I couldn't observe any significant performance
increase either (direct IO writes that don't increase the file's size
don't hold the inode's lock for their entire duration and mmap writes
don't hold the inode's lock at all), which are the only type of writes
that could see any performance gain due to less serialization.
Review feedback from Josef:
The problem was taking the i_mutex in mmap, which is how I was
protecting delalloc reservations originally. The delalloc mutex didn't
come with all of the other dependencies. That's what the lockdep
messages were about, removing the lock isn't going to make them appear
again.
We _had_ to lock around this because we used to do tricks to keep from
over-reserving, and if we didn't serialize delalloc reservations we'd
end up with ugly accounting problems when we tried to clean things up.
However with my recentish changes this isn't the case anymore. Every
operation is responsible for reserving its space, and then adding it to
the inode. Then cleaning up is straightforward and can't be mucked up
by other users. So we no longer need the delalloc mutex to safe us from
ourselves.
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2019-10-25 09:52:42 +00:00
|
|
|
return ret;
|
2023-09-08 17:20:20 +00:00
|
|
|
ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv->space_info,
|
|
|
|
meta_reserve, flush);
|
Btrfs: remove unnecessary delalloc mutex for inodes
The inode delalloc mutex was added a long time ago by commit f248679e86fea
("Btrfs: add a delalloc mutex to inodes for delalloc reservations"), and
the reason for its introduction is not very clear from the change log. It
claims it solves bogus warnings from lockdep, however it lacks an example
report/warning from lockdep, or any explanation.
Since we have enough concurrentcy protection from the locks of the space
info and block reserve objects, and such lockdep warnings don't seem to
exist anymore (at least on a 5.3 kernel I couldn't get them with fstests,
ltp, fs_mark, etc), remove it, simplifying things a bit and decreasing
the size of the btrfs_inode structure. With some quick fio tests doing
direct IO and mmap writes I couldn't observe any significant performance
increase either (direct IO writes that don't increase the file's size
don't hold the inode's lock for their entire duration and mmap writes
don't hold the inode's lock at all), which are the only type of writes
that could see any performance gain due to less serialization.
Review feedback from Josef:
The problem was taking the i_mutex in mmap, which is how I was
protecting delalloc reservations originally. The delalloc mutex didn't
come with all of the other dependencies. That's what the lockdep
messages were about, removing the lock isn't going to make them appear
again.
We _had_ to lock around this because we used to do tricks to keep from
over-reserving, and if we didn't serialize delalloc reservations we'd
end up with ugly accounting problems when we tried to clean things up.
However with my recentish changes this isn't the case anymore. Every
operation is responsible for reserving its space, and then adding it to
the inode. Then cleaning up is straightforward and can't be mucked up
by other users. So we no longer need the delalloc mutex to safe us from
ourselves.
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2019-10-25 09:52:42 +00:00
|
|
|
if (ret) {
|
|
|
|
btrfs_qgroup_free_meta_prealloc(root, qgroup_reserve);
|
|
|
|
return ret;
|
|
|
|
}
|
2019-06-19 19:12:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now we need to update our outstanding extents and csum bytes _first_
|
|
|
|
* and then add the reservation to the block_rsv. This keeps us from
|
|
|
|
* racing with an ordered completion or some such that would think it
|
|
|
|
* needs to free the reservation we just made.
|
|
|
|
*/
|
2022-07-08 23:18:41 +00:00
|
|
|
nr_extents = count_max_extents(fs_info, num_bytes);
|
2023-03-21 11:13:46 +00:00
|
|
|
spin_lock(&inode->lock);
|
2019-06-19 19:12:00 +00:00
|
|
|
btrfs_mod_outstanding_extents(inode, nr_extents);
|
2024-01-31 17:18:04 +00:00
|
|
|
if (!(inode->flags & BTRFS_INODE_NODATASUM))
|
|
|
|
inode->csum_bytes += disk_num_bytes;
|
2019-06-19 19:12:00 +00:00
|
|
|
btrfs_calculate_inode_block_rsv_size(fs_info, inode);
|
|
|
|
spin_unlock(&inode->lock);
|
|
|
|
|
|
|
|
/* Now we can safely add our space to our block rsv */
|
|
|
|
btrfs_block_rsv_add_bytes(block_rsv, meta_reserve, false);
|
|
|
|
trace_btrfs_space_reservation(root->fs_info, "delalloc",
|
|
|
|
btrfs_ino(inode), meta_reserve, 1);
|
|
|
|
|
|
|
|
spin_lock(&block_rsv->lock);
|
|
|
|
block_rsv->qgroup_rsv_reserved += qgroup_reserve;
|
|
|
|
spin_unlock(&block_rsv->lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-27 12:21:42 +00:00
|
|
|
/*
|
|
|
|
* Release a metadata reservation for an inode.
|
2021-01-22 09:58:01 +00:00
|
|
|
*
|
2022-10-27 12:21:42 +00:00
|
|
|
* @inode: the inode to release the reservation for.
|
|
|
|
* @num_bytes: the number of bytes we are releasing.
|
|
|
|
* @qgroup_free: free qgroup reservation or convert it to per-trans reservation
|
2019-06-19 19:12:00 +00:00
|
|
|
*
|
|
|
|
* This will release the metadata reservation for an inode. This can be called
|
|
|
|
* once we complete IO for a given set of bytes to release their metadata
|
|
|
|
* reservations, or on error for the same reason.
|
|
|
|
*/
|
|
|
|
void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
|
|
|
|
bool qgroup_free)
|
|
|
|
{
|
|
|
|
struct btrfs_fs_info *fs_info = inode->root->fs_info;
|
|
|
|
|
|
|
|
num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
|
|
|
|
spin_lock(&inode->lock);
|
2024-01-31 17:18:04 +00:00
|
|
|
if (!(inode->flags & BTRFS_INODE_NODATASUM))
|
|
|
|
inode->csum_bytes -= num_bytes;
|
2019-06-19 19:12:00 +00:00
|
|
|
btrfs_calculate_inode_block_rsv_size(fs_info, inode);
|
|
|
|
spin_unlock(&inode->lock);
|
|
|
|
|
|
|
|
if (btrfs_is_testing(fs_info))
|
|
|
|
return;
|
|
|
|
|
|
|
|
btrfs_inode_rsv_release(inode, qgroup_free);
|
|
|
|
}
|
|
|
|
|
2022-10-27 12:21:42 +00:00
|
|
|
/*
|
|
|
|
* Release our outstanding_extents for an inode.
|
|
|
|
*
|
|
|
|
* @inode: the inode to balance the reservation for.
|
|
|
|
* @num_bytes: the number of bytes we originally reserved with
|
2019-06-19 19:12:00 +00:00
|
|
|
*
|
|
|
|
* When we reserve space we increase outstanding_extents for the extents we may
|
|
|
|
* add. Once we've set the range as delalloc or created our ordered extents we
|
|
|
|
* have outstanding_extents to track the real usage, so we use this to free our
|
|
|
|
* temporarily tracked outstanding_extents. This _must_ be used in conjunction
|
|
|
|
* with btrfs_delalloc_reserve_metadata.
|
|
|
|
*/
|
btrfs: qgroup: Always free PREALLOC META reserve in btrfs_delalloc_release_extents()
[Background]
Btrfs qgroup uses two types of reserved space for METADATA space,
PERTRANS and PREALLOC.
PERTRANS is metadata space reserved for each transaction started by
btrfs_start_transaction().
While PREALLOC is for delalloc, where we reserve space before joining a
transaction, and finally it will be converted to PERTRANS after the
writeback is done.
[Inconsistency]
However there is inconsistency in how we handle PREALLOC metadata space.
The most obvious one is:
In btrfs_buffered_write():
btrfs_delalloc_release_extents(BTRFS_I(inode), reserve_bytes, true);
We always free qgroup PREALLOC meta space.
While in btrfs_truncate_block():
btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
We only free qgroup PREALLOC meta space when something went wrong.
[The Correct Behavior]
The correct behavior should be the one in btrfs_buffered_write(), we
should always free PREALLOC metadata space.
The reason is, the btrfs_delalloc_* mechanism works by:
- Reserve metadata first, even it's not necessary
In btrfs_delalloc_reserve_metadata()
- Free the unused metadata space
Normally in:
btrfs_delalloc_release_extents()
|- btrfs_inode_rsv_release()
Here we do calculation on whether we should release or not.
E.g. for 64K buffered write, the metadata rsv works like:
/* The first page */
reserve_meta: num_bytes=calc_inode_reservations()
free_meta: num_bytes=0
total: num_bytes=calc_inode_reservations()
/* The first page caused one outstanding extent, thus needs metadata
rsv */
/* The 2nd page */
reserve_meta: num_bytes=calc_inode_reservations()
free_meta: num_bytes=calc_inode_reservations()
total: not changed
/* The 2nd page doesn't cause new outstanding extent, needs no new meta
rsv, so we free what we have reserved */
/* The 3rd~16th pages */
reserve_meta: num_bytes=calc_inode_reservations()
free_meta: num_bytes=calc_inode_reservations()
total: not changed (still space for one outstanding extent)
This means, if btrfs_delalloc_release_extents() determines to free some
space, then those space should be freed NOW.
So for qgroup, we should call btrfs_qgroup_free_meta_prealloc() other
than btrfs_qgroup_convert_reserved_meta().
The good news is:
- The callers are not that hot
The hottest caller is in btrfs_buffered_write(), which is already
fixed by commit 336a8bb8e36a ("btrfs: Fix wrong
btrfs_delalloc_release_extents parameter"). Thus it's not that
easy to cause false EDQUOT.
- The trans commit in advance for qgroup would hide the bug
Since commit f5fef4593653 ("btrfs: qgroup: Make qgroup async transaction
commit more aggressive"), when btrfs qgroup metadata free space is slow,
it will try to commit transaction and free the wrongly converted
PERTRANS space, so it's not that easy to hit such bug.
[FIX]
So to fix the problem, remove the @qgroup_free parameter for
btrfs_delalloc_release_extents(), and always pass true to
btrfs_inode_rsv_release().
Reported-by: Filipe Manana <fdmanana@suse.com>
Fixes: 43b18595d660 ("btrfs: qgroup: Use separate meta reservation type for delalloc")
CC: stable@vger.kernel.org # 4.19+
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2019-10-14 06:34:51 +00:00
|
|
|
void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes)
|
2019-06-19 19:12:00 +00:00
|
|
|
{
|
|
|
|
struct btrfs_fs_info *fs_info = inode->root->fs_info;
|
|
|
|
unsigned num_extents;
|
|
|
|
|
|
|
|
spin_lock(&inode->lock);
|
2022-07-08 23:18:41 +00:00
|
|
|
num_extents = count_max_extents(fs_info, num_bytes);
|
2019-06-19 19:12:00 +00:00
|
|
|
btrfs_mod_outstanding_extents(inode, -num_extents);
|
|
|
|
btrfs_calculate_inode_block_rsv_size(fs_info, inode);
|
|
|
|
spin_unlock(&inode->lock);
|
|
|
|
|
|
|
|
if (btrfs_is_testing(fs_info))
|
|
|
|
return;
|
|
|
|
|
btrfs: qgroup: Always free PREALLOC META reserve in btrfs_delalloc_release_extents()
[Background]
Btrfs qgroup uses two types of reserved space for METADATA space,
PERTRANS and PREALLOC.
PERTRANS is metadata space reserved for each transaction started by
btrfs_start_transaction().
While PREALLOC is for delalloc, where we reserve space before joining a
transaction, and finally it will be converted to PERTRANS after the
writeback is done.
[Inconsistency]
However there is inconsistency in how we handle PREALLOC metadata space.
The most obvious one is:
In btrfs_buffered_write():
btrfs_delalloc_release_extents(BTRFS_I(inode), reserve_bytes, true);
We always free qgroup PREALLOC meta space.
While in btrfs_truncate_block():
btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
We only free qgroup PREALLOC meta space when something went wrong.
[The Correct Behavior]
The correct behavior should be the one in btrfs_buffered_write(), we
should always free PREALLOC metadata space.
The reason is, the btrfs_delalloc_* mechanism works by:
- Reserve metadata first, even it's not necessary
In btrfs_delalloc_reserve_metadata()
- Free the unused metadata space
Normally in:
btrfs_delalloc_release_extents()
|- btrfs_inode_rsv_release()
Here we do calculation on whether we should release or not.
E.g. for 64K buffered write, the metadata rsv works like:
/* The first page */
reserve_meta: num_bytes=calc_inode_reservations()
free_meta: num_bytes=0
total: num_bytes=calc_inode_reservations()
/* The first page caused one outstanding extent, thus needs metadata
rsv */
/* The 2nd page */
reserve_meta: num_bytes=calc_inode_reservations()
free_meta: num_bytes=calc_inode_reservations()
total: not changed
/* The 2nd page doesn't cause new outstanding extent, needs no new meta
rsv, so we free what we have reserved */
/* The 3rd~16th pages */
reserve_meta: num_bytes=calc_inode_reservations()
free_meta: num_bytes=calc_inode_reservations()
total: not changed (still space for one outstanding extent)
This means, if btrfs_delalloc_release_extents() determines to free some
space, then those space should be freed NOW.
So for qgroup, we should call btrfs_qgroup_free_meta_prealloc() other
than btrfs_qgroup_convert_reserved_meta().
The good news is:
- The callers are not that hot
The hottest caller is in btrfs_buffered_write(), which is already
fixed by commit 336a8bb8e36a ("btrfs: Fix wrong
btrfs_delalloc_release_extents parameter"). Thus it's not that
easy to cause false EDQUOT.
- The trans commit in advance for qgroup would hide the bug
Since commit f5fef4593653 ("btrfs: qgroup: Make qgroup async transaction
commit more aggressive"), when btrfs qgroup metadata free space is slow,
it will try to commit transaction and free the wrongly converted
PERTRANS space, so it's not that easy to hit such bug.
[FIX]
So to fix the problem, remove the @qgroup_free parameter for
btrfs_delalloc_release_extents(), and always pass true to
btrfs_inode_rsv_release().
Reported-by: Filipe Manana <fdmanana@suse.com>
Fixes: 43b18595d660 ("btrfs: qgroup: Use separate meta reservation type for delalloc")
CC: stable@vger.kernel.org # 4.19+
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2019-10-14 06:34:51 +00:00
|
|
|
btrfs_inode_rsv_release(inode, true);
|
2019-06-19 19:12:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 12:21:42 +00:00
|
|
|
/*
|
|
|
|
* Reserve data and metadata space for delalloc
|
|
|
|
*
|
|
|
|
* @inode: inode we're writing to
|
|
|
|
* @start: start range we are writing to
|
|
|
|
* @len: how long the range we are writing to
|
|
|
|
* @reserved: mandatory parameter, record actually reserved qgroup ranges of
|
|
|
|
* current reservation.
|
2019-06-19 19:12:00 +00:00
|
|
|
*
|
|
|
|
* This will do the following things
|
|
|
|
*
|
2022-10-27 12:21:42 +00:00
|
|
|
* - reserve space in data space info for num bytes and reserve precious
|
|
|
|
* corresponding qgroup space
|
2019-06-19 19:12:00 +00:00
|
|
|
* (Done in check_data_free_space)
|
|
|
|
*
|
|
|
|
* - reserve space for metadata space, based on the number of outstanding
|
2022-10-27 12:21:42 +00:00
|
|
|
* extents and how much csums will be needed also reserve metadata space in a
|
|
|
|
* per root over-reserve method.
|
2019-06-19 19:12:00 +00:00
|
|
|
* - add to the inodes->delalloc_bytes
|
|
|
|
* - add it to the fs_info's delalloc inodes list.
|
|
|
|
* (Above 3 all done in delalloc_reserve_metadata)
|
|
|
|
*
|
|
|
|
* Return 0 for success
|
2022-10-27 12:21:42 +00:00
|
|
|
* Return <0 for error(-ENOSPC or -EDQUOT)
|
2019-06-19 19:12:00 +00:00
|
|
|
*/
|
2020-06-03 05:55:42 +00:00
|
|
|
int btrfs_delalloc_reserve_space(struct btrfs_inode *inode,
|
2019-06-19 19:12:00 +00:00
|
|
|
struct extent_changeset **reserved, u64 start, u64 len)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2022-09-12 19:27:44 +00:00
|
|
|
ret = btrfs_check_data_free_space(inode, reserved, start, len, false);
|
2019-06-19 19:12:00 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
btrfs: avoid blocking on space revervation when doing nowait dio writes
When doing a NOWAIT direct IO write, if we can NOCOW then it means we can
proceed with the non-blocking, NOWAIT path. However reserving the metadata
space and qgroup meta space can often result in blocking - flushing
delalloc, wait for ordered extents to complete, trigger transaction
commits, etc, going against the semantics of a NOWAIT write.
So make the NOWAIT write path to try to reserve all the metadata it needs
without resulting in a blocking behaviour - if we get -ENOSPC or -EDQUOT
then return -EAGAIN to make the caller fallback to a blocking direct IO
write.
This is part of a patchset comprised of the following patches:
btrfs: avoid blocking on page locks with nowait dio on compressed range
btrfs: avoid blocking nowait dio when locking file range
btrfs: avoid double nocow check when doing nowait dio writes
btrfs: stop allocating a path when checking if cross reference exists
btrfs: free path at can_nocow_extent() before checking for checksum items
btrfs: release path earlier at can_nocow_extent()
btrfs: avoid blocking when allocating context for nowait dio read/write
btrfs: avoid blocking on space revervation when doing nowait dio writes
The following test was run before and after applying this patchset:
$ cat io-uring-nodatacow-test.sh
#!/bin/bash
DEV=/dev/sdc
MNT=/mnt/sdc
MOUNT_OPTIONS="-o ssd -o nodatacow"
MKFS_OPTIONS="-R free-space-tree -O no-holes"
NUM_JOBS=4
FILE_SIZE=8G
RUN_TIME=300
cat <<EOF > /tmp/fio-job.ini
[io_uring_rw]
rw=randrw
fsync=0
fallocate=posix
group_reporting=1
direct=1
ioengine=io_uring
iodepth=64
bssplit=4k/20:8k/20:16k/20:32k/10:64k/10:128k/5:256k/5:512k/5:1m/5
filesize=$FILE_SIZE
runtime=$RUN_TIME
time_based
filename=foobar
directory=$MNT
numjobs=$NUM_JOBS
thread
EOF
echo performance | \
tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
umount $MNT &> /dev/null
mkfs.btrfs -f $MKFS_OPTIONS $DEV &> /dev/null
mount $MOUNT_OPTIONS $DEV $MNT
fio /tmp/fio-job.ini
umount $MNT
The test was run a 12 cores box with 64G of ram, using a non-debug kernel
config (Debian's default config) and a spinning disk.
Result before the patchset:
READ: bw=407MiB/s (427MB/s), 407MiB/s-407MiB/s (427MB/s-427MB/s), io=119GiB (128GB), run=300175-300175msec
WRITE: bw=407MiB/s (427MB/s), 407MiB/s-407MiB/s (427MB/s-427MB/s), io=119GiB (128GB), run=300175-300175msec
Result after the patchset:
READ: bw=436MiB/s (457MB/s), 436MiB/s-436MiB/s (457MB/s-457MB/s), io=128GiB (137GB), run=300044-300044msec
WRITE: bw=435MiB/s (456MB/s), 435MiB/s-435MiB/s (456MB/s-456MB/s), io=128GiB (137GB), run=300044-300044msec
That's about +7.2% throughput for reads and +6.9% for writes.
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-03-23 16:19:30 +00:00
|
|
|
ret = btrfs_delalloc_reserve_metadata(inode, len, len, false);
|
2021-12-03 10:55:33 +00:00
|
|
|
if (ret < 0) {
|
2020-06-03 05:55:42 +00:00
|
|
|
btrfs_free_reserved_data_space(inode, *reserved, start, len);
|
2021-12-03 10:55:33 +00:00
|
|
|
extent_changeset_free(*reserved);
|
|
|
|
*reserved = NULL;
|
|
|
|
}
|
2019-06-19 19:12:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-10-27 12:21:42 +00:00
|
|
|
/*
|
2021-01-22 09:58:01 +00:00
|
|
|
* Release data and metadata space for delalloc
|
|
|
|
*
|
|
|
|
* @inode: inode we're releasing space for
|
|
|
|
* @reserved: list of changed/reserved ranges
|
|
|
|
* @start: start position of the space already reserved
|
|
|
|
* @len: length of the space already reserved
|
|
|
|
* @qgroup_free: should qgroup reserved-space also be freed
|
2019-06-19 19:12:00 +00:00
|
|
|
*
|
2022-10-27 12:21:42 +00:00
|
|
|
* Release the metadata space that was not used and will decrement
|
|
|
|
* ->delalloc_bytes and remove it from the fs_info->delalloc_inodes list if
|
|
|
|
* there are no delalloc bytes left. Also it will handle the qgroup reserved
|
|
|
|
* space.
|
2019-06-19 19:12:00 +00:00
|
|
|
*/
|
2020-06-03 05:55:40 +00:00
|
|
|
void btrfs_delalloc_release_space(struct btrfs_inode *inode,
|
2019-06-19 19:12:00 +00:00
|
|
|
struct extent_changeset *reserved,
|
|
|
|
u64 start, u64 len, bool qgroup_free)
|
|
|
|
{
|
2020-06-03 05:55:40 +00:00
|
|
|
btrfs_delalloc_release_metadata(inode, len, qgroup_free);
|
|
|
|
btrfs_free_reserved_data_space(inode, reserved, start, len);
|
2019-06-19 19:12:00 +00:00
|
|
|
}
|