linux/blkdev.c: Fall back to buffered IO when O_DIRECT fails

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2022-11-19 18:59:55 -05:00
parent 7e196f5399
commit 36db254cec

View File

@ -180,13 +180,26 @@ struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
if (mode & FMODE_EXCL)
flags |= O_EXCL;
#endif
buffered_fd = open(path, flags);
if (buffered_fd < 0)
return ERR_PTR(-errno);
fd = open(path, flags|O_DIRECT);
if (fd < 0)
fd = dup(buffered_fd);
if (fd < 0) {
close(buffered_fd);
return ERR_PTR(-errno);
}
sync_fd = xopen(path, flags|O_DIRECT|O_SYNC);
buffered_fd = xopen(path, flags);
sync_fd = open(path, flags|O_DIRECT|O_SYNC);
if (sync_fd < 0)
sync_fd = open(path, flags|O_SYNC);
if (sync_fd < 0) {
close(fd);
close(buffered_fd);
return ERR_PTR(-errno);
}
bdev = malloc(sizeof(*bdev));
memset(bdev, 0, sizeof(*bdev));