[Qemu-devel] [PATCH v4 2/4] raw: Recognize zoned backing devices
The purpose of this patch is to recognize a zoned block device (ZBD) when it is opened as a raw file. The new code initializes the zoned model propery introduced by the previous commit. This commit is Linux-specific as it gets the Zoned Block Device Model value (none/host-managed/host-aware) from sysfs on the host. In order to avoid code duplication in file-posix.c, a common helper function is added to read values of sysfs entries under /sys/block//queue. This way, the existing function that reads the value of "max_segments" entry and the the new function that reads "zoned" value both share the same helper code. Signed-off-by: Dmitry Fomichev --- block/file-posix.c | 74 ++ block/raw-format.c | 8 + 2 files changed, 70 insertions(+), 12 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index fbeb0068db..d9f2fc5e46 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1067,15 +1067,13 @@ static int sg_get_max_transfer_length(int fd) #endif } -static int sg_get_max_segments(int fd) +static int hdev_read_blk_queue_entry(int fd, const char *key, +char *buf, int buf_len) { #ifdef CONFIG_LINUX -char buf[32]; -const char *end; char *sysfspath = NULL; int ret; int sysfd = -1; -long max_segments; struct stat st; if (fstat(fd, &st)) { @@ -1083,23 +1081,45 @@ static int sg_get_max_segments(int fd) goto out; } -sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments", -major(st.st_rdev), minor(st.st_rdev)); +sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/%s", +major(st.st_rdev), minor(st.st_rdev), key); sysfd = open(sysfspath, O_RDONLY); if (sysfd == -1) { ret = -errno; goto out; } do { -ret = read(sysfd, buf, sizeof(buf) - 1); +ret = read(sysfd, buf, buf_len - 1); } while (ret == -1 && errno == EINTR); if (ret < 0) { ret = -errno; -goto out; } else if (ret == 0) { ret = -EIO; +} +out: +if (sysfd != -1) { +close(sysfd); +} +g_free(sysfspath); +return ret; +#else +return -ENOTSUP; +#endif +} + +static int sg_get_max_segments(int fd) +{ +#ifdef CONFIG_LINUX +char buf[32]; +const char *end; +int ret; +long max_segments; + +ret = hdev_read_blk_queue_entry(fd, "max_segments", buf, sizeof(buf)); +if (ret < 0) { goto out; } + buf[ret] = 0; /* The file is ended with '\n', pass 'end' to accept that. */ ret = qemu_strtol(buf, &end, 10, &max_segments); @@ -1108,10 +1128,33 @@ static int sg_get_max_segments(int fd) } out: -if (sysfd != -1) { -close(sysfd); +return ret; +#else +return -ENOTSUP; +#endif +} + +static int hdev_get_zoned_model(int fd) +{ +#ifdef CONFIG_LINUX +char buf[32]; +int ret; + +ret = hdev_read_blk_queue_entry(fd, "zoned", buf, sizeof(buf)); +if (ret < 0) { +ret = BLK_ZONED_MODEL_NONE; +goto out; } -g_free(sysfspath); + +buf[ret - 1] = 0; +ret = BLK_ZONED_MODEL_NONE; +if (strcmp(buf, "host-managed") == 0) { +ret = BLK_ZONED_MODEL_HM; +} else if (strcmp(buf, "host-aware") == 0) { +ret = BLK_ZONED_MODEL_HA; +} + +out: return ret; #else return -ENOTSUP; @@ -1121,9 +1164,10 @@ out: static void raw_refresh_limits(BlockDriverState *bs, Error **errp) { BDRVRawState *s = bs->opaque; +int ret; if (bs->sg) { -int ret = sg_get_max_transfer_length(s->fd); +ret = sg_get_max_transfer_length(s->fd); if (ret > 0 && ret <= BDRV_REQUEST_MAX_BYTES) { bs->bl.max_transfer = pow2floor(ret); @@ -1133,6 +1177,12 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp) if (ret > 0) { bs->bl.max_transfer = MIN(bs->bl.max_transfer, ret * getpagesize()); } + +} + +ret = hdev_get_zoned_model(s->fd); +if (ret >= 0) { +bs->bl.zoned_model = ret; } raw_probe_alignment(bs, s->fd, errp); diff --git a/block/raw-format.c b/block/raw-format.c index 42c28cc29a..a606e4a7fe 100644 --- a/block/raw-format.c +++ b/block/raw-format.c @@ -369,6 +369,13 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp) } } +static void raw_get_zoned_info(BlockDriverState *bs) +{ +if (!bs->probed) { +bs->bl.zoned_model = bs->file->bs->bl.zoned_model; +} +} + static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset, PreallocMode prealloc, Error **errp) { @@ -578,6 +585,7 @@ BlockDriver bdrv_raw = { .create_opts = &raw_create_opts, .bdrv_has_zero_init = &raw_has_zero_init, .bdrv_has_zero_init_truncate = &raw_has_zero_init_truncate, +.bdrv_get_zoned_info = &raw_get_zoned_info, .st
[Qemu-devel] [PATCH v4 2/4] raw: Recognize zoned backing devices
The purpose of this patch is to recognize a zoned block device (ZBD) when it is opened as a raw file. The new code initializes the zoned model propery introduced by the previous commit. This commit is Linux-specific as it gets the Zoned Block Device Model value (none/host-managed/host-aware) from sysfs on the host. In order to avoid code duplication in file-posix.c, a common helper function is added to read values of sysfs entries under /sys/block//queue. This way, the existing function that reads the value of "max_segments" entry and the the new function that reads "zoned" value both share the same helper code. Signed-off-by: Dmitry Fomichev --- block/file-posix.c | 74 ++ block/raw-format.c | 8 + 2 files changed, 70 insertions(+), 12 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index fbeb0068db..d9f2fc5e46 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1067,15 +1067,13 @@ static int sg_get_max_transfer_length(int fd) #endif } -static int sg_get_max_segments(int fd) +static int hdev_read_blk_queue_entry(int fd, const char *key, +char *buf, int buf_len) { #ifdef CONFIG_LINUX -char buf[32]; -const char *end; char *sysfspath = NULL; int ret; int sysfd = -1; -long max_segments; struct stat st; if (fstat(fd, &st)) { @@ -1083,23 +1081,45 @@ static int sg_get_max_segments(int fd) goto out; } -sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments", -major(st.st_rdev), minor(st.st_rdev)); +sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/%s", +major(st.st_rdev), minor(st.st_rdev), key); sysfd = open(sysfspath, O_RDONLY); if (sysfd == -1) { ret = -errno; goto out; } do { -ret = read(sysfd, buf, sizeof(buf) - 1); +ret = read(sysfd, buf, buf_len - 1); } while (ret == -1 && errno == EINTR); if (ret < 0) { ret = -errno; -goto out; } else if (ret == 0) { ret = -EIO; +} +out: +if (sysfd != -1) { +close(sysfd); +} +g_free(sysfspath); +return ret; +#else +return -ENOTSUP; +#endif +} + +static int sg_get_max_segments(int fd) +{ +#ifdef CONFIG_LINUX +char buf[32]; +const char *end; +int ret; +long max_segments; + +ret = hdev_read_blk_queue_entry(fd, "max_segments", buf, sizeof(buf)); +if (ret < 0) { goto out; } + buf[ret] = 0; /* The file is ended with '\n', pass 'end' to accept that. */ ret = qemu_strtol(buf, &end, 10, &max_segments); @@ -1108,10 +1128,33 @@ static int sg_get_max_segments(int fd) } out: -if (sysfd != -1) { -close(sysfd); +return ret; +#else +return -ENOTSUP; +#endif +} + +static int hdev_get_zoned_model(int fd) +{ +#ifdef CONFIG_LINUX +char buf[32]; +int ret; + +ret = hdev_read_blk_queue_entry(fd, "zoned", buf, sizeof(buf)); +if (ret < 0) { +ret = BLK_ZONED_MODEL_NONE; +goto out; } -g_free(sysfspath); + +buf[ret - 1] = 0; +ret = BLK_ZONED_MODEL_NONE; +if (strcmp(buf, "host-managed") == 0) { +ret = BLK_ZONED_MODEL_HM; +} else if (strcmp(buf, "host-aware") == 0) { +ret = BLK_ZONED_MODEL_HA; +} + +out: return ret; #else return -ENOTSUP; @@ -1121,9 +1164,10 @@ out: static void raw_refresh_limits(BlockDriverState *bs, Error **errp) { BDRVRawState *s = bs->opaque; +int ret; if (bs->sg) { -int ret = sg_get_max_transfer_length(s->fd); +ret = sg_get_max_transfer_length(s->fd); if (ret > 0 && ret <= BDRV_REQUEST_MAX_BYTES) { bs->bl.max_transfer = pow2floor(ret); @@ -1133,6 +1177,12 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp) if (ret > 0) { bs->bl.max_transfer = MIN(bs->bl.max_transfer, ret * getpagesize()); } + +} + +ret = hdev_get_zoned_model(s->fd); +if (ret >= 0) { +bs->bl.zoned_model = ret; } raw_probe_alignment(bs, s->fd, errp); diff --git a/block/raw-format.c b/block/raw-format.c index 42c28cc29a..a606e4a7fe 100644 --- a/block/raw-format.c +++ b/block/raw-format.c @@ -369,6 +369,13 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp) } } +static void raw_get_zoned_info(BlockDriverState *bs) +{ +if (!bs->probed) { +bs->bl.zoned_model = bs->file->bs->bl.zoned_model; +} +} + static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset, PreallocMode prealloc, Error **errp) { @@ -578,6 +585,7 @@ BlockDriver bdrv_raw = { .create_opts = &raw_create_opts, .bdrv_has_zero_init = &raw_has_zero_init, .bdrv_has_zero_init_truncate = &raw_has_zero_init_truncate, +.bdrv_get_zoned_info = &raw_get_zoned_info, .st