block: add example ioctl
authorJens Axboe <axboe@kernel.dk>
Sat, 19 Dec 2020 00:12:39 +0000 (17:12 -0700)
committerJens Axboe <axboe@kernel.dk>
Tue, 16 Mar 2021 15:55:33 +0000 (09:55 -0600)
commita67cb15faa230b3caa05e6edd44f2de4d1ae06a2
treeedb6a662cb4f8c1f7947e8832a89b3a09c64fd4f
parentadf1139f2750aac0d691f4547bf5b5320ad19350
block: add example ioctl

Grab op == 1, BLOCK_URING_OP_IOCTL, and use it to implement basic
ioctl functionality.

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[4];
};

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->cmd_pdu_start;
cmd->op = BLOCK_URING_OP_IOCTL;
cmd->ioctl_cmd = BLKBSZGET;
sqe->cmd_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
include/linux/blkdev.h