Add a helper for io_uring block discard commands. Since the discard opcode is in newly added linux/blkdev.h we need to do some configure magic defining it ourselves if the header is missing from the system. Signed-off-by: Pavel Begunkov <asml.silence@xxxxxxxxx> --- configure | 32 ++++++++++++++++++++++++++++++++ src/include/liburing.h | 10 ++++++++++ 2 files changed, 42 insertions(+) diff --git a/configure b/configure index e2221d3..6c9cea0 100755 --- a/configure +++ b/configure @@ -417,6 +417,21 @@ if compile_prog "" "" "futexv"; then fi print_config "futex waitv support" "$futexv" +########################################## +# Check block discard cmd support +discard_cmd="no" +cat > $TMPC << EOF +#include <linux/blkdev.h> +int main(void) +{ + return BLOCK_URING_CMD_DISCARD; +} +EOF +if compile_prog "" "" "discard command"; then + discard_cmd="yes" +fi +print_config "io_uring discard command support" "$discard_cmd" + ########################################## # Check idtype_t support has_idtype_t="no" @@ -651,6 +666,23 @@ typedef enum } idtype_t; EOF fi + +if test "$discard_cmd" != "yes"; then +cat >> $compat_h << EOF + +#include <linux/ioctl.h> + +#ifndef BLOCK_URING_CMD_DISCARD +#define BLOCK_URING_CMD_DISCARD _IO(0x12, 0) +#endif + +EOF +else cat >> $discard_cmd << EOF +#include <linux/blkdev.h> + +EOF +fi + cat >> $compat_h << EOF #endif EOF diff --git a/src/include/liburing.h b/src/include/liburing.h index c5a2fda..f5903d7 100644 --- a/src/include/liburing.h +++ b/src/include/liburing.h @@ -1292,6 +1292,16 @@ IOURINGINLINE void io_uring_prep_ftruncate(struct io_uring_sqe *sqe, } #endif +IOURINGINLINE void io_uring_prep_cmd_discard(struct io_uring_sqe *sqe, + int fd, + uint64_t offset, uint64_t nbytes) +{ + io_uring_prep_rw(IORING_OP_URING_CMD, sqe, fd, 0, 0, 0); + sqe->cmd_op = BLOCK_URING_CMD_DISCARD; + sqe->addr = offset; + sqe->addr3 = nbytes; +} + /* * Returns number of unconsumed (if SQPOLL) or unsubmitted entries exist in * the SQ ring -- 2.46.0