drivers/block/ublk_drv.c | 145 ++++++++++++++++++++++++++++++++-- include/uapi/linux/ublk_cmd.h | 18 +++++ 2 files changed, 157 insertions(+), 6 deletions(-)
Add zoned storage support to ublk: report_zones and operations:
- REQ_OP_ZONE_OPEN
- REQ_OP_ZONE_CLOSE
- REQ_OP_ZONE_FINISH
- REQ_OP_ZONE_RESET
This allows implementation of zoned storage devices in user space. An
example user space implementation based on ubdsrv is available [1].
[1] https://github.com/metaspace/ubdsrv/commit/14a2b708f74f70cfecb076d92e680dc718cc1f6d
Signed-off-by: Andreas Hindborg <a.hindborg@samsung.com>
---
drivers/block/ublk_drv.c | 145 ++++++++++++++++++++++++++++++++--
include/uapi/linux/ublk_cmd.h | 18 +++++
2 files changed, 157 insertions(+), 6 deletions(-)
diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c
index 6368b56eacf1..9741b4b1647b 100644
--- a/drivers/block/ublk_drv.c
+++ b/drivers/block/ublk_drv.c
@@ -20,6 +20,7 @@
#include <linux/major.h>
#include <linux/wait.h>
#include <linux/blkdev.h>
+#include <linux/blkzoned.h>
#include <linux/init.h>
#include <linux/swap.h>
#include <linux/slab.h>
@@ -51,10 +52,12 @@
| UBLK_F_URING_CMD_COMP_IN_TASK \
| UBLK_F_NEED_GET_DATA \
| UBLK_F_USER_RECOVERY \
- | UBLK_F_USER_RECOVERY_REISSUE)
+ | UBLK_F_USER_RECOVERY_REISSUE \
+ | UBLK_F_ZONED)
/* All UBLK_PARAM_TYPE_* should be included here */
-#define UBLK_PARAM_TYPE_ALL (UBLK_PARAM_TYPE_BASIC | UBLK_PARAM_TYPE_DISCARD)
+#define UBLK_PARAM_TYPE_ALL (UBLK_PARAM_TYPE_BASIC | UBLK_PARAM_TYPE_DISCARD \
+ | UBLK_PARAM_TYPE_ZONED)
struct ublk_rq_data {
struct llist_node node;
@@ -212,6 +215,11 @@ static void ublk_dev_param_basic_apply(struct ublk_device *ub)
set_disk_ro(ub->ub_disk, true);
set_capacity(ub->ub_disk, p->dev_sectors);
+
+ if (IS_ENABLED(CONFIG_BLK_DEV_ZONED) &&
+ ub->dev_info.flags & UBLK_F_ZONED && p->chunk_sectors) {
+ ub->ub_disk->nr_zones = p->dev_sectors / p->chunk_sectors;
+ }
}
static void ublk_dev_param_discard_apply(struct ublk_device *ub)
@@ -227,6 +235,19 @@ static void ublk_dev_param_discard_apply(struct ublk_device *ub)
blk_queue_max_discard_segments(q, p->max_discard_segments);
}
+static void ublk_dev_param_zoned_apply(struct ublk_device *ub)
+{
+ const struct ublk_param_zoned *p = &ub->params.zoned;
+
+ if (IS_ENABLED(CONFIG_BLK_DEV_ZONED) &&
+ ub->dev_info.flags & UBLK_F_ZONED) {
+ disk_set_max_active_zones(ub->ub_disk, p->max_active_zones);
+ disk_set_max_open_zones(ub->ub_disk, p->max_open_zones);
+ /* We do not support zone append yet */
+ //blk_queue_max_zone_append_sectors(q, zone_size);
+ }
+}
+
static int ublk_validate_params(const struct ublk_device *ub)
{
/* basic param is the only one which must be set */
@@ -268,6 +289,9 @@ static int ublk_apply_params(struct ublk_device *ub)
if (ub->params.types & UBLK_PARAM_TYPE_DISCARD)
ublk_dev_param_discard_apply(ub);
+ if (ub->params.types & UBLK_PARAM_TYPE_ZONED)
+ ublk_dev_param_zoned_apply(ub);
+
return 0;
}
@@ -361,9 +385,18 @@ static void ublk_free_disk(struct gendisk *disk)
put_device(&ub->cdev_dev);
}
+#if IS_ENABLED(CONFIG_BLK_DEV_ZONED)
+static int ublk_report_zones(struct gendisk *disk, sector_t sector,
+ unsigned int nr_zones, report_zones_cb cb,
+ void *data);
+#endif
+
static const struct block_device_operations ub_fops = {
- .owner = THIS_MODULE,
- .free_disk = ublk_free_disk,
+ .owner = THIS_MODULE,
+ .free_disk = ublk_free_disk,
+#if IS_ENABLED(CONFIG_BLK_DEV_ZONED)
+ .report_zones = ublk_report_zones,
+#endif
};
#define UBLK_MAX_PIN_PAGES 32
@@ -499,7 +532,7 @@ static int ublk_unmap_io(const struct ublk_queue *ubq,
{
const unsigned int rq_bytes = blk_rq_bytes(req);
- if (req_op(req) == REQ_OP_READ && ublk_rq_has_data(req)) {
+ if ((req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_DRV_IN) && ublk_rq_has_data(req)) {
struct ublk_map_data data = {
.ubq = ubq,
.rq = req,
@@ -566,6 +599,26 @@ static blk_status_t ublk_setup_iod(struct ublk_queue *ubq, struct request *req)
case REQ_OP_WRITE_ZEROES:
ublk_op = UBLK_IO_OP_WRITE_ZEROES;
break;
+#ifdef CONFIG_BLK_DEV_ZONED
+ case REQ_OP_ZONE_OPEN:
+ ublk_op = UBLK_IO_OP_ZONE_OPEN;
+ break;
+ case REQ_OP_ZONE_CLOSE:
+ ublk_op = UBLK_IO_OP_ZONE_CLOSE;
+ break;
+ case REQ_OP_ZONE_FINISH:
+ ublk_op = UBLK_IO_OP_ZONE_FINISH;
+ break;
+ case REQ_OP_ZONE_RESET:
+ ublk_op = UBLK_IO_OP_ZONE_RESET;
+ break;
+ case REQ_OP_DRV_IN:
+ ublk_op = UBLK_IO_OP_DRV_IN;
+ break;
+ case REQ_OP_ZONE_APPEND:
+ /* We do not support zone append yet */
+ fallthrough;
+#endif
default:
return BLK_STS_IOERR;
}
@@ -612,7 +665,8 @@ static void ublk_complete_rq(struct request *req)
*
* Both the two needn't unmap.
*/
- if (req_op(req) != REQ_OP_READ && req_op(req) != REQ_OP_WRITE) {
+ if (req_op(req) != REQ_OP_READ && req_op(req) != REQ_OP_WRITE &&
+ req_op(req) != REQ_OP_DRV_IN) {
blk_mq_end_request(req, BLK_STS_OK);
return;
}
@@ -1493,6 +1547,73 @@ static struct ublk_device *ublk_get_device_from_id(int idx)
return ub;
}
+#ifdef CONFIG_BLK_DEV_ZONED
+static int ublk_report_zones(struct gendisk *disk, sector_t sector,
+ unsigned int nr_zones, report_zones_cb cb,
+ void *data)
+{
+ struct ublk_device *ub;
+ unsigned int zone_size;
+ unsigned int first_zone;
+ int ret = 0;
+
+ ub = disk->private_data;
+
+ if (!(ub->dev_info.flags & UBLK_F_ZONED))
+ return -EINVAL;
+
+ zone_size = disk->queue->limits.chunk_sectors;
+ first_zone = sector >> ilog2(zone_size);
+
+ nr_zones = min(ub->ub_disk->nr_zones - first_zone, nr_zones);
+
+ for (unsigned int i = 0; i < nr_zones; i++) {
+
+ struct request *req;
+ blk_status_t status;
+ struct blk_zone info;
+
+ req = blk_mq_alloc_request(disk->queue, REQ_OP_DRV_IN, 0);
+
+ if (IS_ERR(req)) {
+ ret = PTR_ERR(req);
+ goto out;
+ }
+
+ req->__sector = sector;
+
+ ret = blk_rq_map_kern(disk->queue, req, &info, sizeof(info),
+ GFP_KERNEL);
+
+ if (ret)
+ goto out;
+
+ status = blk_execute_rq(req, 0);
+ ret = blk_status_to_errno(status);
+ if (ret)
+ goto out;
+
+ blk_mq_free_request(req);
+
+ ret = cb(&info, i, data);
+ if (ret)
+ goto out;
+
+ /* A zero length zone means don't ask for more zones */
+ if (!info.len) {
+ nr_zones = i;
+ break;
+ }
+
+ sector += zone_size;
+ }
+ ret = nr_zones;
+
+ out:
+ return ret;
+}
+#endif
+
static int ublk_ctrl_start_dev(struct io_uring_cmd *cmd)
{
struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
@@ -1535,6 +1656,15 @@ static int ublk_ctrl_start_dev(struct io_uring_cmd *cmd)
if (ret)
goto out_put_disk;
+ if (IS_ENABLED(CONFIG_BLK_DEV_ZONED) &&
+ ub->dev_info.flags & UBLK_F_ZONED) {
+ disk_set_zoned(disk, BLK_ZONED_HM);
+ blk_queue_required_elevator_features(disk->queue, ELEVATOR_F_ZBD_SEQ_WRITE);
+ ret = blk_revalidate_disk_zones(disk, NULL);
+ if (ret)
+ goto out_put_disk;
+ }
+
get_device(&ub->cdev_dev);
ret = add_disk(disk);
if (ret) {
@@ -1673,6 +1803,9 @@ static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd)
if (!IS_BUILTIN(CONFIG_BLK_DEV_UBLK))
ub->dev_info.flags |= UBLK_F_URING_CMD_COMP_IN_TASK;
+ if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED))
+ ub->dev_info.flags &= ~UBLK_F_ZONED;
+
/* We are not ready to support zero copy */
ub->dev_info.flags &= ~UBLK_F_SUPPORT_ZERO_COPY;
diff --git a/include/uapi/linux/ublk_cmd.h b/include/uapi/linux/ublk_cmd.h
index 8f88e3a29998..074b97821575 100644
--- a/include/uapi/linux/ublk_cmd.h
+++ b/include/uapi/linux/ublk_cmd.h
@@ -78,6 +78,10 @@
#define UBLK_F_USER_RECOVERY (1UL << 3)
#define UBLK_F_USER_RECOVERY_REISSUE (1UL << 4)
+/*
+ * Enable zoned device support
+ */
+#define UBLK_F_ZONED (1ULL << 5)
/* device state */
#define UBLK_S_DEV_DEAD 0
@@ -129,6 +133,12 @@ struct ublksrv_ctrl_dev_info {
#define UBLK_IO_OP_DISCARD 3
#define UBLK_IO_OP_WRITE_SAME 4
#define UBLK_IO_OP_WRITE_ZEROES 5
+#define UBLK_IO_OP_ZONE_OPEN 10
+#define UBLK_IO_OP_ZONE_CLOSE 11
+#define UBLK_IO_OP_ZONE_FINISH 12
+#define UBLK_IO_OP_ZONE_APPEND 13
+#define UBLK_IO_OP_ZONE_RESET 15
+#define UBLK_IO_OP_DRV_IN 34
#define UBLK_IO_F_FAILFAST_DEV (1U << 8)
#define UBLK_IO_F_FAILFAST_TRANSPORT (1U << 9)
@@ -214,6 +224,12 @@ struct ublk_param_discard {
__u16 reserved0;
};
+struct ublk_param_zoned {
+ __u64 max_open_zones;
+ __u64 max_active_zones;
+ __u64 max_append_size;
+};
+
struct ublk_params {
/*
* Total length of parameters, userspace has to set 'len' for both
@@ -224,10 +240,12 @@ struct ublk_params {
__u32 len;
#define UBLK_PARAM_TYPE_BASIC (1 << 0)
#define UBLK_PARAM_TYPE_DISCARD (1 << 1)
+#define UBLK_PARAM_TYPE_ZONED (1 << 2)
__u32 types; /* types of parameter included */
struct ublk_param_basic basic;
struct ublk_param_discard discard;
+ struct ublk_param_zoned zoned;
};
#endif
--
2.39.2
Hi Andreas, Thank you for the patch! Yet something to improve: [auto build test ERROR on v6.2] [cannot apply to axboe-block/for-next linus/master next-20230224] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/Andreas-Hindborg/block-ublk-enable-zoned-storage-support/20230224-210205 patch link: https://lore.kernel.org/r/20230224125950.214779-1-nmi%40metaspace.dk patch subject: [PATCH] block: ublk: enable zoned storage support config: mips-allyesconfig (https://download.01.org/0day-ci/archive/20230225/202302250349.1eKfsTvO-lkp@intel.com/config) compiler: mips-linux-gcc (GCC) 12.1.0 reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # https://github.com/intel-lab-lkp/linux/commit/6d088fc1b115a63e8888b12fa47aabd45be97460 git remote add linux-review https://github.com/intel-lab-lkp/linux git fetch --no-tags linux-review Andreas-Hindborg/block-ublk-enable-zoned-storage-support/20230224-210205 git checkout 6d088fc1b115a63e8888b12fa47aabd45be97460 # save the config file mkdir build_dir && cp config build_dir/.config COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=mips olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=mips SHELL=/bin/bash If you fix the issue, kindly add following tag where applicable | Reported-by: kernel test robot <lkp@intel.com> | Link: https://lore.kernel.org/oe-kbuild-all/202302250349.1eKfsTvO-lkp@intel.com/ All errors (new ones prefixed by >>): arch/mips/kernel/head.o: in function `kernel_entry': (.ref.text+0xac): relocation truncated to fit: R_MIPS_26 against `start_kernel' init/main.o: in function `set_reset_devices': main.c:(.init.text+0x20): relocation truncated to fit: R_MIPS_26 against `_mcount' main.c:(.init.text+0x30): relocation truncated to fit: R_MIPS_26 against `__sanitizer_cov_trace_pc' init/main.o: in function `debug_kernel': main.c:(.init.text+0xa4): relocation truncated to fit: R_MIPS_26 against `_mcount' main.c:(.init.text+0xb4): relocation truncated to fit: R_MIPS_26 against `__sanitizer_cov_trace_pc' init/main.o: in function `quiet_kernel': main.c:(.init.text+0x128): relocation truncated to fit: R_MIPS_26 against `_mcount' main.c:(.init.text+0x138): relocation truncated to fit: R_MIPS_26 against `__sanitizer_cov_trace_pc' init/main.o: in function `warn_bootconfig': main.c:(.init.text+0x1ac): relocation truncated to fit: R_MIPS_26 against `_mcount' main.c:(.init.text+0x1bc): relocation truncated to fit: R_MIPS_26 against `__sanitizer_cov_trace_pc' init/main.o: in function `init_setup': main.c:(.init.text+0x234): relocation truncated to fit: R_MIPS_26 against `_mcount' main.c:(.init.text+0x254): additional relocation overflows omitted from the output mips-linux-ld: drivers/block/ublk_drv.o: in function `ublk_dev_param_basic_apply': >> ublk_drv.c:(.text.ublk_dev_param_basic_apply+0x2d8): undefined reference to `__udivdi3' -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
Hi Andreas, Thank you for the patch! Yet something to improve: [auto build test ERROR on v6.2] [cannot apply to axboe-block/for-next linus/master next-20230224] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/Andreas-Hindborg/block-ublk-enable-zoned-storage-support/20230224-210205 patch link: https://lore.kernel.org/r/20230224125950.214779-1-nmi%40metaspace.dk patch subject: [PATCH] block: ublk: enable zoned storage support config: ia64-buildonly-randconfig-r006-20230222 (https://download.01.org/0day-ci/archive/20230225/202302250222.XOrw9j6z-lkp@intel.com/config) compiler: ia64-linux-gcc (GCC) 12.1.0 reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # https://github.com/intel-lab-lkp/linux/commit/6d088fc1b115a63e8888b12fa47aabd45be97460 git remote add linux-review https://github.com/intel-lab-lkp/linux git fetch --no-tags linux-review Andreas-Hindborg/block-ublk-enable-zoned-storage-support/20230224-210205 git checkout 6d088fc1b115a63e8888b12fa47aabd45be97460 # save the config file mkdir build_dir && cp config build_dir/.config COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=ia64 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=ia64 SHELL=/bin/bash drivers/ If you fix the issue, kindly add following tag where applicable | Reported-by: kernel test robot <lkp@intel.com> | Link: https://lore.kernel.org/oe-kbuild-all/202302250222.XOrw9j6z-lkp@intel.com/ All errors (new ones prefixed by >>): drivers/block/ublk_drv.c: In function 'ublk_dev_param_basic_apply': >> drivers/block/ublk_drv.c:221:28: error: 'struct gendisk' has no member named 'nr_zones' 221 | ub->ub_disk->nr_zones = p->dev_sectors / p->chunk_sectors; | ^~ drivers/block/ublk_drv.c: In function 'ublk_dev_param_zoned_apply': >> drivers/block/ublk_drv.c:244:17: error: implicit declaration of function 'disk_set_max_active_zones'; did you mean 'bdev_max_active_zones'? [-Werror=implicit-function-declaration] 244 | disk_set_max_active_zones(ub->ub_disk, p->max_active_zones); | ^~~~~~~~~~~~~~~~~~~~~~~~~ | bdev_max_active_zones >> drivers/block/ublk_drv.c:245:17: error: implicit declaration of function 'disk_set_max_open_zones'; did you mean 'bdev_max_open_zones'? [-Werror=implicit-function-declaration] 245 | disk_set_max_open_zones(ub->ub_disk, p->max_open_zones); | ^~~~~~~~~~~~~~~~~~~~~~~ | bdev_max_open_zones drivers/block/ublk_drv.c: In function 'ublk_ctrl_start_dev': >> drivers/block/ublk_drv.c:1663:23: error: implicit declaration of function 'blk_revalidate_disk_zones' [-Werror=implicit-function-declaration] 1663 | ret = blk_revalidate_disk_zones(disk, NULL); | ^~~~~~~~~~~~~~~~~~~~~~~~~ cc1: some warnings being treated as errors vim +221 drivers/block/ublk_drv.c 192 193 static void ublk_dev_param_basic_apply(struct ublk_device *ub) 194 { 195 struct request_queue *q = ub->ub_disk->queue; 196 const struct ublk_param_basic *p = &ub->params.basic; 197 198 blk_queue_logical_block_size(q, 1 << p->logical_bs_shift); 199 blk_queue_physical_block_size(q, 1 << p->physical_bs_shift); 200 blk_queue_io_min(q, 1 << p->io_min_shift); 201 blk_queue_io_opt(q, 1 << p->io_opt_shift); 202 203 blk_queue_write_cache(q, p->attrs & UBLK_ATTR_VOLATILE_CACHE, 204 p->attrs & UBLK_ATTR_FUA); 205 if (p->attrs & UBLK_ATTR_ROTATIONAL) 206 blk_queue_flag_clear(QUEUE_FLAG_NONROT, q); 207 else 208 blk_queue_flag_set(QUEUE_FLAG_NONROT, q); 209 210 blk_queue_max_hw_sectors(q, p->max_sectors); 211 blk_queue_chunk_sectors(q, p->chunk_sectors); 212 blk_queue_virt_boundary(q, p->virt_boundary_mask); 213 214 if (p->attrs & UBLK_ATTR_READ_ONLY) 215 set_disk_ro(ub->ub_disk, true); 216 217 set_capacity(ub->ub_disk, p->dev_sectors); 218 219 if (IS_ENABLED(CONFIG_BLK_DEV_ZONED) && 220 ub->dev_info.flags & UBLK_F_ZONED && p->chunk_sectors) { > 221 ub->ub_disk->nr_zones = p->dev_sectors / p->chunk_sectors; 222 } 223 } 224 225 static void ublk_dev_param_discard_apply(struct ublk_device *ub) 226 { 227 struct request_queue *q = ub->ub_disk->queue; 228 const struct ublk_param_discard *p = &ub->params.discard; 229 230 q->limits.discard_alignment = p->discard_alignment; 231 q->limits.discard_granularity = p->discard_granularity; 232 blk_queue_max_discard_sectors(q, p->max_discard_sectors); 233 blk_queue_max_write_zeroes_sectors(q, 234 p->max_write_zeroes_sectors); 235 blk_queue_max_discard_segments(q, p->max_discard_segments); 236 } 237 238 static void ublk_dev_param_zoned_apply(struct ublk_device *ub) 239 { 240 const struct ublk_param_zoned *p = &ub->params.zoned; 241 242 if (IS_ENABLED(CONFIG_BLK_DEV_ZONED) && 243 ub->dev_info.flags & UBLK_F_ZONED) { > 244 disk_set_max_active_zones(ub->ub_disk, p->max_active_zones); > 245 disk_set_max_open_zones(ub->ub_disk, p->max_open_zones); 246 /* We do not support zone append yet */ 247 //blk_queue_max_zone_append_sectors(q, zone_size); 248 } 249 } 250 -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
Hello Andreas, On Fri, Feb 24, 2023 at 01:59:50PM +0100, Andreas Hindborg wrote: > Add zoned storage support to ublk: report_zones and operations: > - REQ_OP_ZONE_OPEN > - REQ_OP_ZONE_CLOSE > - REQ_OP_ZONE_FINISH > - REQ_OP_ZONE_RESET > > This allows implementation of zoned storage devices in user space. An > example user space implementation based on ubdsrv is available [1]. > > [1] https://github.com/metaspace/ubdsrv/commit/14a2b708f74f70cfecb076d92e680dc718cc1f6d > > Signed-off-by: Andreas Hindborg <a.hindborg@samsung.com> > --- Did you try to build this patch with CONFIG_BLK_DEV_ZONED disabled? I got the following build errors when building it on top of vanilla v6.2 when CONFIG_BLK_DEV_ZONED is disabled: drivers/block/ublk_drv.c: In function ‘ublk_dev_param_basic_apply’: drivers/block/ublk_drv.c:221:28: error: ‘struct gendisk’ has no member named ‘nr_zones’ 221 | ub->ub_disk->nr_zones = p->dev_sectors / p->chunk_sectors; | ^~ drivers/block/ublk_drv.c: In function ‘ublk_dev_param_zoned_apply’: drivers/block/ublk_drv.c:244:17: error: implicit declaration of function ‘disk_set_max_active_zones’; did you mean ‘bdev_max_active_zones’? [-Werror=implicit-function-declaration] 244 | disk_set_max_active_zones(ub->ub_disk, p->max_active_zones); | ^~~~~~~~~~~~~~~~~~~~~~~~~ | bdev_max_active_zones drivers/block/ublk_drv.c:245:17: error: implicit declaration of function ‘disk_set_max_open_zones’; did you mean ‘bdev_max_open_zones’? [-Werror=implicit-function-declaration] 245 | disk_set_max_open_zones(ub->ub_disk, p->max_open_zones); > drivers/block/ublk_drv.c | 145 ++++++++++++++++++++++++++++++++-- > include/uapi/linux/ublk_cmd.h | 18 +++++ > 2 files changed, 157 insertions(+), 6 deletions(-) > > diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c > index 6368b56eacf1..9741b4b1647b 100644 > --- a/drivers/block/ublk_drv.c > +++ b/drivers/block/ublk_drv.c > @@ -20,6 +20,7 @@ > #include <linux/major.h> > #include <linux/wait.h> > #include <linux/blkdev.h> > +#include <linux/blkzoned.h> > #include <linux/init.h> > #include <linux/swap.h> > #include <linux/slab.h> > @@ -51,10 +52,12 @@ > | UBLK_F_URING_CMD_COMP_IN_TASK \ > | UBLK_F_NEED_GET_DATA \ > | UBLK_F_USER_RECOVERY \ > - | UBLK_F_USER_RECOVERY_REISSUE) > + | UBLK_F_USER_RECOVERY_REISSUE \ > + | UBLK_F_ZONED) > > /* All UBLK_PARAM_TYPE_* should be included here */ > -#define UBLK_PARAM_TYPE_ALL (UBLK_PARAM_TYPE_BASIC | UBLK_PARAM_TYPE_DISCARD) > +#define UBLK_PARAM_TYPE_ALL (UBLK_PARAM_TYPE_BASIC | UBLK_PARAM_TYPE_DISCARD \ > + | UBLK_PARAM_TYPE_ZONED) > > struct ublk_rq_data { > struct llist_node node; > @@ -212,6 +215,11 @@ static void ublk_dev_param_basic_apply(struct ublk_device *ub) > set_disk_ro(ub->ub_disk, true); > > set_capacity(ub->ub_disk, p->dev_sectors); > + > + if (IS_ENABLED(CONFIG_BLK_DEV_ZONED) && > + ub->dev_info.flags & UBLK_F_ZONED && p->chunk_sectors) { > + ub->ub_disk->nr_zones = p->dev_sectors / p->chunk_sectors; > + } Kernel coding style says to not unnecessarily use braces where a single statement will do: https://www.kernel.org/doc/html/latest/process/coding-style.html#placing-braces-and-spaces > } > > static void ublk_dev_param_discard_apply(struct ublk_device *ub) > @@ -227,6 +235,19 @@ static void ublk_dev_param_discard_apply(struct ublk_device *ub) > blk_queue_max_discard_segments(q, p->max_discard_segments); > } > > +static void ublk_dev_param_zoned_apply(struct ublk_device *ub) > +{ > + const struct ublk_param_zoned *p = &ub->params.zoned; > + > + if (IS_ENABLED(CONFIG_BLK_DEV_ZONED) && > + ub->dev_info.flags & UBLK_F_ZONED) { > + disk_set_max_active_zones(ub->ub_disk, p->max_active_zones); > + disk_set_max_open_zones(ub->ub_disk, p->max_open_zones); > + /* We do not support zone append yet */ > + //blk_queue_max_zone_append_sectors(q, zone_size); Remove commented out code. It can be added once that feature is added. > + } > +} > + > static int ublk_validate_params(const struct ublk_device *ub) > { > /* basic param is the only one which must be set */ > @@ -268,6 +289,9 @@ static int ublk_apply_params(struct ublk_device *ub) > if (ub->params.types & UBLK_PARAM_TYPE_DISCARD) > ublk_dev_param_discard_apply(ub); > > + if (ub->params.types & UBLK_PARAM_TYPE_ZONED) > + ublk_dev_param_zoned_apply(ub); > + > return 0; > } > > @@ -361,9 +385,18 @@ static void ublk_free_disk(struct gendisk *disk) > put_device(&ub->cdev_dev); > } > > +#if IS_ENABLED(CONFIG_BLK_DEV_ZONED) Here you use "#if IS_ENABLED(CONFIG_BLK_DEV_ZONED)" yet further down you use "#ifdef CONFIG_BLK_DEV_ZONED". IS_ENABLED() is usually used when testing for a Kconfig within a normal C conditional, e.g. if (IS_ENABLED()), where possible, in order to avoid ugly C preprocessor conditionals completely. However, if you have to use C preprocessor conditionals, then #ifdef CONFIG_SOMETHING is the most common way to conditionally include something. > +static int ublk_report_zones(struct gendisk *disk, sector_t sector, > + unsigned int nr_zones, report_zones_cb cb, > + void *data); > +#endif > + > static const struct block_device_operations ub_fops = { > - .owner = THIS_MODULE, > - .free_disk = ublk_free_disk, > + .owner = THIS_MODULE, > + .free_disk = ublk_free_disk, > +#if IS_ENABLED(CONFIG_BLK_DEV_ZONED) > + .report_zones = ublk_report_zones, > +#endif > }; > > #define UBLK_MAX_PIN_PAGES 32 > @@ -499,7 +532,7 @@ static int ublk_unmap_io(const struct ublk_queue *ubq, > { > const unsigned int rq_bytes = blk_rq_bytes(req); > > - if (req_op(req) == REQ_OP_READ && ublk_rq_has_data(req)) { > + if ((req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_DRV_IN) && ublk_rq_has_data(req)) { > struct ublk_map_data data = { > .ubq = ubq, > .rq = req, > @@ -566,6 +599,26 @@ static blk_status_t ublk_setup_iod(struct ublk_queue *ubq, struct request *req) > case REQ_OP_WRITE_ZEROES: > ublk_op = UBLK_IO_OP_WRITE_ZEROES; > break; > +#ifdef CONFIG_BLK_DEV_ZONED > + case REQ_OP_ZONE_OPEN: > + ublk_op = UBLK_IO_OP_ZONE_OPEN; > + break; > + case REQ_OP_ZONE_CLOSE: > + ublk_op = UBLK_IO_OP_ZONE_CLOSE; > + break; > + case REQ_OP_ZONE_FINISH: > + ublk_op = UBLK_IO_OP_ZONE_FINISH; > + break; > + case REQ_OP_ZONE_RESET: > + ublk_op = UBLK_IO_OP_ZONE_RESET; > + break; > + case REQ_OP_DRV_IN: > + ublk_op = UBLK_IO_OP_DRV_IN; > + break; > + case REQ_OP_ZONE_APPEND: > + /* We do not support zone append yet */ > + fallthrough; > +#endif > default: > return BLK_STS_IOERR; > } > @@ -612,7 +665,8 @@ static void ublk_complete_rq(struct request *req) > * > * Both the two needn't unmap. > */ > - if (req_op(req) != REQ_OP_READ && req_op(req) != REQ_OP_WRITE) { > + if (req_op(req) != REQ_OP_READ && req_op(req) != REQ_OP_WRITE && > + req_op(req) != REQ_OP_DRV_IN) { > blk_mq_end_request(req, BLK_STS_OK); > return; > } > @@ -1493,6 +1547,73 @@ static struct ublk_device *ublk_get_device_from_id(int idx) > return ub; > } > > +#ifdef CONFIG_BLK_DEV_ZONED > +static int ublk_report_zones(struct gendisk *disk, sector_t sector, > + unsigned int nr_zones, report_zones_cb cb, > + void *data) > +{ > + struct ublk_device *ub; > + unsigned int zone_size; > + unsigned int first_zone; > + int ret = 0; > + > + ub = disk->private_data; > + > + if (!(ub->dev_info.flags & UBLK_F_ZONED)) > + return -EINVAL; > + > + zone_size = disk->queue->limits.chunk_sectors; > + first_zone = sector >> ilog2(zone_size); > + > + nr_zones = min(ub->ub_disk->nr_zones - first_zone, nr_zones); > + > + for (unsigned int i = 0; i < nr_zones; i++) { > + No need for empty line here. > + struct request *req; > + blk_status_t status; > + struct blk_zone info; Since this is allocated on the stack, does it need memset()? null_blk seems to memset blk_zone before the loop, but not inside the loop: https://github.com/torvalds/linux/blob/v6.2/drivers/block/null_blk/zoned.c#L206 So I'm a bit confused... If e.g. cb() is blkdev_copy_zone_to_user() https://github.com/torvalds/linux/blob/v6.2/block/blk-zoned.c#L318 It will copy whatever the kernel allocated. So I guess that since you can't guarantee that the ublk user space driver will fill in all fields in the zone when receiving a REQ_OP_DRV_IN, I assume that you do need to do a memset() in order to not potentially leak kernel space data. > + > + req = blk_mq_alloc_request(disk->queue, REQ_OP_DRV_IN, 0); > + > + if (IS_ERR(req)) { > + ret = PTR_ERR(req); > + goto out; > + } > + > + req->__sector = sector; > + > + ret = blk_rq_map_kern(disk->queue, req, &info, sizeof(info), > + GFP_KERNEL); > + > + if (ret) > + goto out; > + > + status = blk_execute_rq(req, 0); > + ret = blk_status_to_errno(status); > + if (ret) > + goto out; > + > + blk_mq_free_request(req); > + > + ret = cb(&info, i, data); > + if (ret) > + goto out; > + > + /* A zero length zone means don't ask for more zones */ > + if (!info.len) { > + nr_zones = i; > + break; > + } > + > + sector += zone_size; > + } > + ret = nr_zones; > + > + out: > + return ret; > +} > +#endif > + > static int ublk_ctrl_start_dev(struct io_uring_cmd *cmd) > { > struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; > @@ -1535,6 +1656,15 @@ static int ublk_ctrl_start_dev(struct io_uring_cmd *cmd) > if (ret) > goto out_put_disk; > > + if (IS_ENABLED(CONFIG_BLK_DEV_ZONED) && > + ub->dev_info.flags & UBLK_F_ZONED) { > + disk_set_zoned(disk, BLK_ZONED_HM); > + blk_queue_required_elevator_features(disk->queue, ELEVATOR_F_ZBD_SEQ_WRITE); > + ret = blk_revalidate_disk_zones(disk, NULL); > + if (ret) > + goto out_put_disk; > + } > + > get_device(&ub->cdev_dev); > ret = add_disk(disk); > if (ret) { > @@ -1673,6 +1803,9 @@ static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd) > if (!IS_BUILTIN(CONFIG_BLK_DEV_UBLK)) > ub->dev_info.flags |= UBLK_F_URING_CMD_COMP_IN_TASK; > > + if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED)) > + ub->dev_info.flags &= ~UBLK_F_ZONED; > + > /* We are not ready to support zero copy */ > ub->dev_info.flags &= ~UBLK_F_SUPPORT_ZERO_COPY; > > diff --git a/include/uapi/linux/ublk_cmd.h b/include/uapi/linux/ublk_cmd.h > index 8f88e3a29998..074b97821575 100644 > --- a/include/uapi/linux/ublk_cmd.h > +++ b/include/uapi/linux/ublk_cmd.h > @@ -78,6 +78,10 @@ > #define UBLK_F_USER_RECOVERY (1UL << 3) > > #define UBLK_F_USER_RECOVERY_REISSUE (1UL << 4) > +/* > + * Enable zoned device support > + */ > +#define UBLK_F_ZONED (1ULL << 5) > > /* device state */ > #define UBLK_S_DEV_DEAD 0 > @@ -129,6 +133,12 @@ struct ublksrv_ctrl_dev_info { > #define UBLK_IO_OP_DISCARD 3 > #define UBLK_IO_OP_WRITE_SAME 4 > #define UBLK_IO_OP_WRITE_ZEROES 5 > +#define UBLK_IO_OP_ZONE_OPEN 10 > +#define UBLK_IO_OP_ZONE_CLOSE 11 > +#define UBLK_IO_OP_ZONE_FINISH 12 > +#define UBLK_IO_OP_ZONE_APPEND 13 > +#define UBLK_IO_OP_ZONE_RESET 15 > +#define UBLK_IO_OP_DRV_IN 34 > > #define UBLK_IO_F_FAILFAST_DEV (1U << 8) > #define UBLK_IO_F_FAILFAST_TRANSPORT (1U << 9) > @@ -214,6 +224,12 @@ struct ublk_param_discard { > __u16 reserved0; > }; > > +struct ublk_param_zoned { > + __u64 max_open_zones; > + __u64 max_active_zones; > + __u64 max_append_size; > +}; > + > struct ublk_params { > /* > * Total length of parameters, userspace has to set 'len' for both > @@ -224,10 +240,12 @@ struct ublk_params { > __u32 len; > #define UBLK_PARAM_TYPE_BASIC (1 << 0) > #define UBLK_PARAM_TYPE_DISCARD (1 << 1) > +#define UBLK_PARAM_TYPE_ZONED (1 << 2) > __u32 types; /* types of parameter included */ > > struct ublk_param_basic basic; > struct ublk_param_discard discard; > + struct ublk_param_zoned zoned; > }; > > #endif > -- > 2.39.2 >
On Fri, Feb 24, 2023 at 04:53:52PM +0100, Niklas Cassel wrote: > Hello Andreas, > > On Fri, Feb 24, 2023 at 01:59:50PM +0100, Andreas Hindborg wrote: > > Add zoned storage support to ublk: report_zones and operations: > > - REQ_OP_ZONE_OPEN > > - REQ_OP_ZONE_CLOSE > > - REQ_OP_ZONE_FINISH > > - REQ_OP_ZONE_RESET > > > > This allows implementation of zoned storage devices in user space. An > > example user space implementation based on ubdsrv is available [1]. > > > > [1] https://github.com/metaspace/ubdsrv/commit/14a2b708f74f70cfecb076d92e680dc718cc1f6d > > > > Signed-off-by: Andreas Hindborg <a.hindborg@samsung.com> > > --- > > Did you try to build this patch with CONFIG_BLK_DEV_ZONED disabled? > > I got the following build errors when building it on top of vanilla v6.2 > when CONFIG_BLK_DEV_ZONED is disabled: > > drivers/block/ublk_drv.c: In function ‘ublk_dev_param_basic_apply’: > drivers/block/ublk_drv.c:221:28: error: ‘struct gendisk’ has no member named ‘nr_zones’ > 221 | ub->ub_disk->nr_zones = p->dev_sectors / p->chunk_sectors; > | ^~ > drivers/block/ublk_drv.c: In function ‘ublk_dev_param_zoned_apply’: > drivers/block/ublk_drv.c:244:17: error: implicit declaration of function ‘disk_set_max_active_zones’; did you mean ‘bdev_max_active_zones’? [-Werror=implicit-function-declaration] > 244 | disk_set_max_active_zones(ub->ub_disk, p->max_active_zones); > | ^~~~~~~~~~~~~~~~~~~~~~~~~ > | bdev_max_active_zones > drivers/block/ublk_drv.c:245:17: error: implicit declaration of function ‘disk_set_max_open_zones’; did you mean ‘bdev_max_open_zones’? [-Werror=implicit-function-declaration] > 245 | disk_set_max_open_zones(ub->ub_disk, p->max_open_zones); > The problem here is probably that blkdev.h does not have dummy functions for disk_set_max_active_zones() and disk_set_max_open_zones() in the: #else /* CONFIG_BLK_DEV_ZONED */ case. I do see dummy functions in blkdev.h for disk_nr_zones(), disk_zone_is_seq() and disk_zone_no() when CONFIG_BLK_DEV_ZONED is not set. Kind regards, Niklas
Niklas Cassel <Niklas.Cassel@wdc.com> writes: > On Fri, Feb 24, 2023 at 04:53:52PM +0100, Niklas Cassel wrote: >> Hello Andreas, >> >> On Fri, Feb 24, 2023 at 01:59:50PM +0100, Andreas Hindborg wrote: >> > Add zoned storage support to ublk: report_zones and operations: >> > - REQ_OP_ZONE_OPEN >> > - REQ_OP_ZONE_CLOSE >> > - REQ_OP_ZONE_FINISH >> > - REQ_OP_ZONE_RESET >> > >> > This allows implementation of zoned storage devices in user space. An >> > example user space implementation based on ubdsrv is available [1]. >> > >> > [1] https://github.com/metaspace/ubdsrv/commit/14a2b708f74f70cfecb076d92e680dc718cc1f6d >> > >> > Signed-off-by: Andreas Hindborg <a.hindborg@samsung.com> >> > --- >> >> Did you try to build this patch with CONFIG_BLK_DEV_ZONED disabled? >> >> I got the following build errors when building it on top of vanilla v6.2 >> when CONFIG_BLK_DEV_ZONED is disabled: >> >> drivers/block/ublk_drv.c: In function ‘ublk_dev_param_basic_apply’: >> drivers/block/ublk_drv.c:221:28: error: ‘struct gendisk’ has no member named ‘nr_zones’ >> 221 | ub->ub_disk->nr_zones = p->dev_sectors / p->chunk_sectors; >> | ^~ >> drivers/block/ublk_drv.c: In function ‘ublk_dev_param_zoned_apply’: >> drivers/block/ublk_drv.c:244:17: error: implicit declaration of function ‘disk_set_max_active_zones’; did you mean ‘bdev_max_active_zones’? [-Werror=implicit-function-declaration] >> 244 | disk_set_max_active_zones(ub->ub_disk, p->max_active_zones); >> | ^~~~~~~~~~~~~~~~~~~~~~~~~ >> | bdev_max_active_zones >> drivers/block/ublk_drv.c:245:17: error: implicit declaration of function ‘disk_set_max_open_zones’; did you mean ‘bdev_max_open_zones’? [-Werror=implicit-function-declaration] >> 245 | disk_set_max_open_zones(ub->ub_disk, p->max_open_zones); >> > > The problem here is probably that blkdev.h does not have dummy functions for > disk_set_max_active_zones() and disk_set_max_open_zones() > > in the: > #else /* CONFIG_BLK_DEV_ZONED */ > case. > > I do see dummy functions in blkdev.h for disk_nr_zones(), disk_zone_is_seq() > and disk_zone_no() when CONFIG_BLK_DEV_ZONED is not set. Thanks for the comments Niklas :) I completely forgot to test without CONFIG_BLK_DEV_ZONED 🤦 What is the preferred resolution here? Conditionally remove the lines with #ifdef rather than use if(IS_ENABLED(...))? Dummy functions would resolve lines 244 and 245, but would not help with assignment of nr_zones at 221 as that will not exist when CONFIG_BLK_DEV_ZONED is disabled. BR Andreas
© 2016 - 2025 Red Hat, Inc.