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>
Sat, 19 Dec 2020 03:06:09 +0000 (20:06 -0700)
commit4e86778464b953f286897c458d04c50e78d08739
tree28659bea4a68903d1e1f78f9eda878eda2cfa431
parent13bd4f517da918a15ae0a7f98b7aaa6debc918ce
block: add example ioctl

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
include/linux/blkdev.h