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>
Thu, 28 Jan 2021 01:56:14 +0000 (18:56 -0700)
commitcd8938fb8eff96bcafa8bff0ec6ca17a727d2a9c
tree51da62736d2ea71c447fe0dd83c44557c0e22967
parent0b6002c51e6e5632551edab8dd96c32cecea50e6
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[2];
};

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