btrfs-progs: temporarily set zoned flag for initial tree reading

Functions to read data/metadata e.g. read_extent_from_disk() now depend on
the fs_info->zoned flag to determine if they do direct-IO or not.

The flag (and zone_size) is not known before reading the chunk tree and it
set to 0 while in the initial chunk tree setup process. That will cause
btrfs_pread() to fail because it does not align the buffer.

Use fcntl() to find out the file descriptor is opened with O_DIRECT or not,
and if it is, set the zoned flag to 1 temporally for this initial process.

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Naohiro Aota 2021-10-05 15:23:04 +09:00 committed by David Sterba
parent ae0dfb246d
commit 4a8d85f730

View File

@ -1302,10 +1302,22 @@ static struct btrfs_fs_info *__open_ctree_fd(int fp, struct open_ctree_flags *oc
if (ret)
goto out_devices;
/*
* fs_info->zone_size (and zoned) are not known before reading the
* chunk tree, so it's 0 at this point. But, fs_info->zoned == 0
* will cause btrfs_pread() not to use an aligned bounce buffer,
* causing EINVAL when the file is opened with O_DIRECT. Temporarily
* set zoned = 1 in that case.
*/
if (fcntl(fp, F_GETFL) & O_DIRECT)
fs_info->zoned = 1;
ret = btrfs_setup_chunk_tree_and_device_map(fs_info, ocf->chunk_tree_bytenr);
if (ret)
goto out_chunk;
fs_info->zoned = 0;
/* Chunk tree root is unable to read, return directly */
if (!fs_info->chunk_root)
return fs_info;