From: Ernest Borowski btrfs_dio_iomap_begin() caps every mapping at the end of the extent map that contains its start. Reads and NOCOW writes need this, but COW writes allocate a new extent for the whole mapped length. A sequential direct IO rewrite of a fragmented file therefore allocates one small extent per old extent, and pays for an extent reservation, an ordered extent and a bio each time. Commit f85781fb505e ("btrfs: switch to iomap for direct IO") introduced this limit. Checksummed files avoided it from 6.15 to 7.2 through commit 968f19c5b1b7 ("btrfs: always fallback to buffered write if the inode requires checksum"), and hit it again in 7.3 through commit e9b7b9d9e78e ("btrfs: use IOMAP_DIO_BOUNCE flag instead of falling back to buffered IO"). Allow COW writes to extend their mapping across the following extent maps, stopping at preallocated, compressed or inline extents and at fs_info->max_extent_size. After a short allocation, cap the later mappings of the same write at that length, so fragmented free space does not cause repeated extent map lookups and failed allocations. Keep the single-extent limit for reads, NOCOW writes, and writes without a data space reservation (NOWAIT or no space left). Rewriting a 256 MiB file fragmented into 65536 extents with 1 MiB direct writes allocates 256 data extents instead of 65536 (257 file extent items afterwards instead of 65851). Fixes: f85781fb505e ("btrfs: switch to iomap for direct IO") Cc: stable@vger.kernel.org # 7.3+ Signed-off-by: Ernest Borowski --- fs/btrfs/direct-io.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/fs/btrfs/direct-io.c b/fs/btrfs/direct-io.c index ca0311e352a..b09a442eba7 100644 --- a/fs/btrfs/direct-io.c +++ b/fs/btrfs/direct-io.c @@ -22,6 +22,12 @@ struct btrfs_dio_data { bool data_space_reserved; bool nocow_done; bool updated_isize; + /* + * Length of the last COW allocation of this write that came back + * shorter than its mapping, 0 if none did. It survives the reset of + * the other fields for each mapping, see btrfs_dio_cow_write_len(). + */ + u64 cow_len_limit; }; struct btrfs_dio_private { @@ -339,9 +345,11 @@ static int btrfs_get_blocks_direct_write(struct extent_map **map, } *map = em; len = min(len, em->len - (start - em->start)); - if (len < prev_len) + if (len < prev_len) { btrfs_delalloc_release_metadata(BTRFS_I(inode), prev_len - len, true); + dio_data->cow_len_limit = len; + } } /* @@ -371,6 +379,61 @@ static int btrfs_get_blocks_direct_write(struct extent_map **map, return ret; } +/* + * Return how much of the range [@start, @start + @len) a COW direct write can + * map in one go, given the extent map @em that contains @start. + * + * A COW write allocates a new extent for the whole mapping, so it does not + * have to end where the extent map at @start ends. Ending there makes the new + * extents copy the boundaries of the old ones: a sequential rewrite of a file + * that was fragmented by small random writes allocates one small extent per + * old extent and stays as fragmented as it was. + * + * The mapping still ends before a preallocated extent, which the write must + * fill instead of allocating new space, and before a compressed or inline + * extent, for which btrfs_dio_iomap_begin() falls back to buffered IO. + * + * It also ends after fs_info->max_extent_size, the largest extent a buffered + * write allocates, and after dio_data->cow_len_limit once an allocation of + * this write came back shorter than its mapping. Free space is fragmented + * then, and a longer mapping would only cost extent map lookups and failed + * allocation attempts. Neither limit ends the mapping before the end of @em. + */ +static u64 btrfs_dio_cow_write_len(struct btrfs_inode *inode, + const struct btrfs_dio_data *dio_data, + const struct extent_map *em, u64 start, + u64 len) +{ + u64 limit = inode->root->fs_info->max_extent_size; + u64 cur = btrfs_extent_map_end(em); + u64 end; + + if (dio_data->cow_len_limit) + limit = min(limit, dio_data->cow_len_limit); + end = min(start + len, max(cur, start + limit)); + + while (cur < end) { + struct extent_map *next; + bool stop; + + next = btrfs_get_extent(inode, NULL, cur, end - cur); + /* The next mapping will look up the extent map again. */ + if (IS_ERR(next)) + break; + stop = (next->flags & EXTENT_FLAG_PREALLOC) || + btrfs_extent_map_is_compressed(next) || + next->disk_bytenr == EXTENT_MAP_INLINE; + if (!stop) + cur = btrfs_extent_map_end(next); + btrfs_free_extent_map(next); + if (stop) + break; + cond_resched(); + } + + return min(cur, end) - start; +} + static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, loff_t length, unsigned int flags, struct iomap *iomap, struct iomap *srcmap) @@ -386,6 +449,7 @@ static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, u64 len = length; const u64 data_alloc_len = length; u32 unlock_bits = EXTENT_LOCKED; + u64 cow_len_limit; /* * We could potentially fault if we have a buffer > PAGE_SIZE, and if @@ -443,7 +507,9 @@ static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, } } + cow_len_limit = dio_data->cow_len_limit; memset(dio_data, 0, sizeof(*dio_data)); + dio_data->cow_len_limit = cow_len_limit; /* * We always try to allocate data space and must do it before locking @@ -512,7 +578,20 @@ static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, goto unlock_err; } - len = min(len, em->len - (start - em->start)); + /* + * Reads and NOCOW writes (into a preallocated extent or an extent of a + * NODATACOW inode) use the existing extent and must stay within it. A + * COW write gets a new extent and can go past it. It needs the data + * space reserved above: without it (a NOWAIT write, or no space left) + * only a NOCOW write can succeed, see btrfs_get_blocks_direct_write(). + */ + if (dio_data->data_space_reserved && + !(em->flags & EXTENT_FLAG_PREALLOC) && + !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) + len = btrfs_dio_cow_write_len(BTRFS_I(inode), dio_data, em, + start, len); + else + len = min(len, em->len - (start - em->start)); /* * If we have a NOWAIT request and the range contains multiple extents -- 2.56.1