[PATCH v4 1/7] iov_iter: Change the direction macros into an enum

From: David Howells
Date: Fri Jan 06 2023 - 19:34:42 EST


Change the ITER_SOURCE and ITER_DEST direction macros into an enum and
provide three new helper functions:

iov_iter_dir() - returns the iterator direction
iov_iter_is_dest() - returns true if it's an ITER_DEST iterator
iov_iter_is_source() - returns true if it's an ITER_SOURCE iterator

Signed-off-by: David Howells <dhowells@xxxxxxxxxx>
cc: Al Viro <viro@xxxxxxxxxxxxxxxxxx>
---

include/linux/uio.h | 32 +++++++++++++++++++++++++++-----
1 file changed, 27 insertions(+), 5 deletions(-)

diff --git a/include/linux/uio.h b/include/linux/uio.h
index 9f158238edba..4b0f4a773d90 100644
--- a/include/linux/uio.h
+++ b/include/linux/uio.h
@@ -29,8 +29,10 @@ enum iter_type {
ITER_UBUF,
};

-#define ITER_SOURCE 1 // == WRITE
-#define ITER_DEST 0 // == READ
+enum iter_dir {
+ ITER_DEST = 0, // == READ
+ ITER_SOURCE = 1, // == WRITE
+} __mode(byte);

struct iov_iter_state {
size_t iov_offset;
@@ -39,9 +41,9 @@ struct iov_iter_state {
};

struct iov_iter {
- u8 iter_type;
+ enum iter_type iter_type __mode(byte);
bool nofault;
- bool data_source;
+ enum iter_dir data_source;
bool user_backed;
union {
size_t iov_offset;
@@ -114,9 +116,29 @@ static inline bool iov_iter_is_xarray(const struct iov_iter *i)
return iov_iter_type(i) == ITER_XARRAY;
}

+static inline enum iter_dir iov_iter_dir(const struct iov_iter *i)
+{
+ return i->data_source;
+}
+
+static inline bool iov_iter_is_source(const struct iov_iter *i)
+{
+ return iov_iter_dir(i) == ITER_SOURCE; /* ie. WRITE */
+}
+
+static inline bool iov_iter_is_dest(const struct iov_iter *i)
+{
+ return iov_iter_dir(i) == ITER_DEST; /* ie. READ */
+}
+
+static inline bool iov_iter_dir_valid(enum iter_dir direction)
+{
+ return direction == ITER_DEST || direction == ITER_SOURCE;
+}
+
static inline unsigned char iov_iter_rw(const struct iov_iter *i)
{
- return i->data_source ? WRITE : READ;
+ return iov_iter_dir(i) == ITER_SOURCE ? WRITE : READ;
}

static inline bool user_backed_iter(const struct iov_iter *i)