From 8a4ed0d1b16d8932362ffecf8f6f79e6340a585f Mon Sep 17 00:00:00 2001 From: Ekaterina Tumanova Date: Mon, 16 Feb 2015 12:47:55 +0100 Subject: [PATCH] raw-posix: Factor block size detection out of raw_probe_alignment() Put it in new probe_logical_blocksize(). Signed-off-by: Ekaterina Tumanova Reviewed-by: Markus Armbruster Reviewed-by: Stefan Hajnoczi Message-id: 1424087278-49393-3-git-send-email-tumanova@linux.vnet.ibm.com Signed-off-by: Stefan Hajnoczi Signed-off-by: Kevin Wolf --- block/raw-posix.c | 53 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/block/raw-posix.c b/block/raw-posix.c index c0b46cac9e..34d403d391 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -218,11 +218,43 @@ static int raw_normalize_devicepath(const char **filename) } #endif +/* + * Get logical block size via ioctl. On success store it in @sector_size_p. + */ +static int probe_logical_blocksize(int fd, unsigned int *sector_size_p) +{ + unsigned int sector_size; + bool success = false; + + errno = ENOTSUP; + + /* Try a few ioctls to get the right size */ +#ifdef BLKSSZGET + if (ioctl(fd, BLKSSZGET, §or_size) >= 0) { + *sector_size_p = sector_size; + success = true; + } +#endif +#ifdef DKIOCGETBLOCKSIZE + if (ioctl(fd, DKIOCGETBLOCKSIZE, §or_size) >= 0) { + *sector_size_p = sector_size; + success = true; + } +#endif +#ifdef DIOCGSECTORSIZE + if (ioctl(fd, DIOCGSECTORSIZE, §or_size) >= 0) { + *sector_size_p = sector_size; + success = true; + } +#endif + + return success ? 0 : -errno; +} + static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp) { BDRVRawState *s = bs->opaque; char *buf; - unsigned int sector_size; /* For /dev/sg devices the alignment is not really used. With buffered I/O, we don't have any restrictions. */ @@ -232,25 +264,12 @@ static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp) return; } - /* Try a few ioctls to get the right size */ bs->request_alignment = 0; s->buf_align = 0; - -#ifdef BLKSSZGET - if (ioctl(fd, BLKSSZGET, §or_size) >= 0) { - bs->request_alignment = sector_size; + /* Let's try to use the logical blocksize for the alignment. */ + if (probe_logical_blocksize(fd, &bs->request_alignment) < 0) { + bs->request_alignment = 0; } -#endif -#ifdef DKIOCGETBLOCKSIZE - if (ioctl(fd, DKIOCGETBLOCKSIZE, §or_size) >= 0) { - bs->request_alignment = sector_size; - } -#endif -#ifdef DIOCGSECTORSIZE - if (ioctl(fd, DIOCGSECTORSIZE, §or_size) >= 0) { - bs->request_alignment = sector_size; - } -#endif #ifdef CONFIG_XFS if (s->is_xfs) { struct dioattr da;