diff mbox series

[liburing,1/3] Add io_uring_prep_cmd_discard

Message ID 45357cbdcbea27505bd43ed53e19722737fa4179.1728851862.git.asml.silence@gmail.com (mailing list archive)
State New
Headers show
Series support for discard block commands | expand

Commit Message

Pavel Begunkov Oct. 13, 2024, 8:45 p.m. UTC
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@gmail.com>
---
 configure              | 32 ++++++++++++++++++++++++++++++++
 src/include/liburing.h | 10 ++++++++++
 2 files changed, 42 insertions(+)
diff mbox series

Patch

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