[PATCH 4/5] block: add example ioctl

Jens Axboe
2021-01-27T21:27:05Z

Example code, to issue BLKBSZGET through IORING_OP_URING_CMD:

struct block_uring_cmd {
__u16 op;
__u16 pad;
union {
__u32 size;
__u32 ioctl_cmd;
};
__u64 addr;
__u64 unused[2];
__u64 reserved; /* can never be used */
__u64 unused2;
};

static int get_bs(struct io_uring *ring, const char *dev)
{
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
struct block_uring_cmd *cmd;
int ret, fd;

fd = open(dev, O_RDONLY);
if (fd < 0) {
perror("open");
return 1;
}

sqe = io_uring_get_sqe(ring);
if (!sqe) {
fprintf(stderr, "get sqe failed\n");
goto err;
}

memset(sqe, 0, sizeof(*sqe));
sqe->opcode = IORING_OP_URING_CMD;
sqe->fd = fd;
cmd = (void *) &sqe->off;
cmd->op = BLOCK_URING_OP_IOCTL;
cmd->ioctl_cmd = BLKBSZGET;
sqe->user_data = 0x1234;

ret = io_uring_submit(ring);
if (ret <= 0) {
fprintf(stderr, "sqe submit failed: %d\n", ret);
goto err;
}

ret = io_uring_wait_cqe(ring, &cqe);
if (ret < 0) {
fprintf(stderr, "wait completion %d\n", ret);
goto err;
}
printf("bs=%d\n", cqe->res);
io_uring_cqe_seen(ring, cqe);
return 0;
err:
return 1;
}

Signed-off-by: Jens Axboe <axboe@kernel.dk>
---
fs/block_dev.c | 19 +++++++++++++++++++
include/linux/blkdev.h | 17 +++++++++++++++++
2 files changed, 36 insertions(+)

diff --git a/fs/block_dev.c b/fs/block_dev.c
index c837912c1d72..7cb1b24ebbb5 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -302,10 +302,29 @@ struct blkdev_dio {

static struct bio_set blkdev_dio_pool;

+static int blkdev_uring_ioctl(struct block_device *bdev,
+ struct block_uring_cmd *bcmd)
+{
+ switch (bcmd->ioctl_cmd) {
+ case BLKBSZGET:
+ return block_size(bdev);
+ default:
+ return -ENOTTY;
+ }
+}
+
static int blkdev_uring_cmd(struct io_uring_cmd *cmd,
enum io_uring_cmd_flags flags)
{
struct block_device *bdev = I_BDEV(cmd->file->f_mapping->host);
+ struct block_uring_cmd *bcmd = (struct block_uring_cmd *) &cmd->pdu;
+
+ switch (bcmd->op) {
+ case BLOCK_URING_OP_IOCTL:
+ return blkdev_uring_ioctl(bdev, bcmd);
+ default:
+ break;
+ }

return blk_uring_cmd(bdev, cmd, flags);
}
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index d5af592d73fe..48ac8ccbffe2 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -44,6 +44,23 @@ struct blk_queue_stats;
struct blk_stat_callback;
struct blk_keyslot_manager;

+enum {
+ BLOCK_URING_OP_IOCTL = 1,
+};
+
+struct block_uring_cmd {
+ __u16 op;
+ __u16 pad;
+ union {
+ __u32 size;
+ __u32 ioctl_cmd;
+ };
+ __u64 addr;
+ __u64 unused[2];
+ __u64 reserved; /* can never be used */
+ __u64 unused2;
+};
+
#define BLKDEV_MIN_RQ 4
#define BLKDEV_MAX_RQ 128 /* Default maximum */

--
2.30.0