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 matche with the alignment of holes. So stop asserting the assumption on the holes and tolerate them being unaligned. Signed-off-by: Akihiko Odaki <[email protected]> --- block/file-posix.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index ec95b748696b..7b686ce6817d 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -3307,22 +3307,21 @@ 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 may have allocation unaligned with the requested alignment due to + * the following reaons: + * - 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 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); - } + *pnum = ROUND_UP(*pnum, bs->bl.request_alignment); ret = BDRV_BLOCK_DATA; } else { --- base-commit: f0737158b483e7ec2b2512145aeab888b85cc1f7 change-id: 20250528-dio-db04a66a7848 Best regards, -- Akihiko Odaki <[email protected]>
