file-posix used to assume that existing holes satisfy the requested alignment, which equals to the estimated direct I/O alignment requirement if direct I/O is requested, and assert the assumption unless it is at EOF.
However, the estimation of direct I/O alignment requirement is sometimes inexact and can be overly strict. For example, I observed that QEMU estimated the alignment requirement as 16K while the real requirement is 4K when Btrfs is used on Linux 6.14.6 and the host page size is 16K. For direct I/O alignment, open(2) sugguests as follows: > Since Linux 6.1, O_DIRECT support and alignment restrictions for a > file can be queried using statx(2), using the STATX_DIOALIGN flag. > Support for STATX_DIOALIGN varies by filesystem; see statx(2). > > Some filesystems provide their own interfaces for querying O_DIRECT > alignment restrictions, for example the XFS_IOC_DIOINFO operation in > xfsctl(3). STATX_DIOALIGN should be used instead when it is available. > > If none of the above is available, then direct I/O support and > alignment restrictions can only be assumed from known characteristics > of the filesystem, the individual file, the underlying storage > device(s), and the kernel version. In Linux 2.4, most filesystems > based on block devices require that the file offset and the length and > memory address of all I/O segments be multiples of the filesystem > block size (typically 4096 bytes). In Linux 2.6.0, this was relaxed to > the logical block size of the block device (typically 512 bytes). A > block device's logical block size can be determined using the ioctl(2) > BLKSSZGET operation or from the shell using the command: Apparently Btrfs doesn't support STATX_DIOALIGN nor provide its own interface for querying the requirement. Using BLKSSZGET brings another problem to determine the underlying block device, which also involves heuristics. Moreover, even if we could figure out the direct I/O alignment requirement, I could not find a documentation saying it will exactly match with the alignment of holes. So stop asserting the assumption on the holes and handle unaligned holes properly. Signed-off-by: Akihiko Odaki <[email protected]> Reviewed-by: Eric Blake <[email protected]> --- Changes in v4: - Fixed a typo pointed out by Eric Blake. - Link to v3: https://lore.kernel.org/qemu-devel/[email protected] Changes in v3: - Stop iterating until finding an aligned location. I think I did too much with the last version; some caller (e.g., is_zero() in block/qcow2.c) only cares if there is *any* allocation in a range, and extra iterations only add I/O overhead in such a case. The code to iterate should be added only if it is proved to bring more benefits than overheads. - Link to v2: https://lore.kernel.org/qemu-devel/[email protected] Changes in v2: - Changed to round the number also when the specified offset in a hole. - Changed to iterate until finding an aligned location. - Link to v1: https://lore.kernel.org/qemu-devel/[email protected] --- block/file-posix.c | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index 8c738674cedb..ee97c24ed928 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -3315,29 +3315,38 @@ static int coroutine_fn raw_co_block_status(BlockDriverState *bs, *pnum = bytes; ret = BDRV_BLOCK_DATA; } else if (data == offset) { - /* On a data extent, compute bytes to the end of the extent, - * possibly including a partial sector at EOF. */ + /* On a data extent, compute bytes to the end of the extent. */ *pnum = hole - offset; - /* - * We are not allowed to return partial sectors, though, so - * round up if necessary. - */ - if (!QEMU_IS_ALIGNED(*pnum, bs->bl.request_alignment)) { - int64_t file_length = raw_getlength(bs); - if (file_length > 0) { - /* Ignore errors, this is just a safeguard */ - assert(hole == file_length); - } - *pnum = ROUND_UP(*pnum, bs->bl.request_alignment); - } - + /* + * We may have allocation unaligned with the requested + * alignment due to the following reasons: + * - unaligned file size + * - inexact direct I/O alignment requirement estimation + * - mismatches between the allocation size and + * direct I/O alignment requirement. + * + * We are not allowed to return partial sectors, though, so + * round up the end of allocation if necessary. + */ + *pnum = ROUND_UP(*pnum, bs->bl.request_alignment); ret = BDRV_BLOCK_DATA; } else { /* On a hole, compute bytes to the beginning of the next extent. */ assert(hole == offset); *pnum = data - offset; - ret = BDRV_BLOCK_ZERO; + + /* + * We may have allocation unaligned, so round down the beginning + * of allocation if necessary. + */ + if (*pnum < bs->bl.request_alignment) { + *pnum = bs->bl.request_alignment; + ret = BDRV_BLOCK_DATA; + } else { + *pnum = ROUND_DOWN(*pnum, bs->bl.request_alignment); + ret = BDRV_BLOCK_ZERO; + } } *map = offset; *file = bs; --- base-commit: 36076d24f04ea9dc3357c0fbe7bb14917375819c change-id: 20250528-dio-db04a66a7848 Best regards, -- Akihiko Odaki <[email protected]>
