Skip to content

Commit 00e2370

Browse files
committed
iov_iter: Use accessor function
Use accessor functions to access an iterator's type and direction. This allows for the possibility of using some other method of determining the type of iterator than if-chains with bitwise-AND conditions. Signed-off-by: David Howells <dhowells@redhat.com>
1 parent 1fcb748 commit 00e2370

File tree

14 files changed

+87
-60
lines changed

14 files changed

+87
-60
lines changed

block/bio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ struct bio *bio_copy_user_iov(struct request_queue *q,
12551255
/*
12561256
* success
12571257
*/
1258-
if (((iter->type & WRITE) && (!map_data || !map_data->null_mapped)) ||
1258+
if ((iov_iter_rw(iter) == WRITE && (!map_data || !map_data->null_mapped)) ||
12591259
(map_data && map_data->from_user)) {
12601260
ret = bio_copy_from_iter(bio, iter);
12611261
if (ret)

fs/block_dev.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages)
349349

350350
dio->size = 0;
351351
dio->multi_bio = false;
352-
dio->should_dirty = is_read && (iter->type == ITER_IOVEC);
352+
dio->should_dirty = is_read && iter_is_iovec(iter);
353353

354354
blk_start_plug(&plug);
355355
for (;;) {

fs/ceph/file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *to,
658658
if (ret < 0)
659659
return ret;
660660

661-
if (unlikely(to->type & ITER_PIPE)) {
661+
if (unlikely(iov_iter_is_pipe(to))) {
662662
size_t page_off;
663663
ret = iov_iter_get_pages_alloc(to, &pages, len,
664664
&page_off);

fs/cifs/file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2990,7 +2990,7 @@ cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)
29902990
size_t copy = min_t(size_t, remaining, PAGE_SIZE);
29912991
size_t written;
29922992

2993-
if (unlikely(iter->type & ITER_PIPE)) {
2993+
if (unlikely(iov_iter_is_pipe(iter))) {
29942994
void *addr = kmap_atomic(page);
29952995

29962996
written = copy_to_iter(addr, copy, iter);
@@ -3302,7 +3302,7 @@ ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
33023302
if (!is_sync_kiocb(iocb))
33033303
ctx->iocb = iocb;
33043304

3305-
if (to->type == ITER_IOVEC)
3305+
if (iter_is_iovec(to))
33063306
ctx->should_dirty = true;
33073307

33083308
rc = setup_aio_ctx_iter(ctx, to, READ);

fs/cifs/misc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ setup_aio_ctx_iter(struct cifs_aio_ctx *ctx, struct iov_iter *iter, int rw)
786786
struct page **pages = NULL;
787787
struct bio_vec *bv = NULL;
788788

789-
if (iter->type & ITER_KVEC) {
789+
if (iov_iter_is_kvec(iter)) {
790790
memcpy(&ctx->iter, iter, sizeof(struct iov_iter));
791791
ctx->len = count;
792792
iov_iter_advance(iter, count);

fs/cifs/smbdirect.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,14 +2054,22 @@ int smbd_recv(struct smbd_connection *info, struct msghdr *msg)
20542054

20552055
info->smbd_recv_pending++;
20562056

2057-
switch (msg->msg_iter.type) {
2058-
case READ | ITER_KVEC:
2057+
if (iov_iter_rw(&msg->msg_iter) == WRITE) {
2058+
/* It's a bug in upper layer to get there */
2059+
cifs_dbg(VFS, "CIFS: invalid msg iter dir %u\n",
2060+
iov_iter_rw(&msg->msg_iter));
2061+
rc = -EINVAL;
2062+
goto out;
2063+
}
2064+
2065+
switch (iov_iter_type(&msg->msg_iter)) {
2066+
case ITER_KVEC:
20592067
buf = msg->msg_iter.kvec->iov_base;
20602068
to_read = msg->msg_iter.kvec->iov_len;
20612069
rc = smbd_recv_buf(info, buf, to_read);
20622070
break;
20632071

2064-
case READ | ITER_BVEC:
2072+
case ITER_BVEC:
20652073
page = msg->msg_iter.bvec->bv_page;
20662074
page_offset = msg->msg_iter.bvec->bv_offset;
20672075
to_read = msg->msg_iter.bvec->bv_len;
@@ -2071,10 +2079,11 @@ int smbd_recv(struct smbd_connection *info, struct msghdr *msg)
20712079
default:
20722080
/* It's a bug in upper layer to get there */
20732081
cifs_dbg(VFS, "CIFS: invalid msg type %d\n",
2074-
msg->msg_iter.type);
2082+
iov_iter_type(&msg->msg_iter));
20752083
rc = -EINVAL;
20762084
}
20772085

2086+
out:
20782087
info->smbd_recv_pending--;
20792088
wake_up(&info->wait_smbd_recv_pending);
20802089

fs/direct-io.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,7 @@ do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,
13131313
spin_lock_init(&dio->bio_lock);
13141314
dio->refcount = 1;
13151315

1316-
dio->should_dirty = (iter->type == ITER_IOVEC);
1316+
dio->should_dirty = iter_is_iovec(iter) && iov_iter_rw(iter) == READ;
13171317
sdio.iter = iter;
13181318
sdio.final_block_in_request = end >> blkbits;
13191319

fs/fuse/file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
12711271
ssize_t ret = 0;
12721272

12731273
/* Special case for kernel I/O: can copy directly into the buffer */
1274-
if (ii->type & ITER_KVEC) {
1274+
if (iov_iter_is_kvec(ii)) {
12751275
unsigned long user_addr = fuse_get_user_addr(ii);
12761276
size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
12771277

fs/iomap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1795,7 +1795,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
17951795
if (pos >= dio->i_size)
17961796
goto out_free_dio;
17971797

1798-
if (iter->type == ITER_IOVEC)
1798+
if (iter_is_iovec(iter) && iov_iter_rw(iter) == READ)
17991799
dio->flags |= IOMAP_DIO_DIRTY;
18001800
} else {
18011801
flags |= IOMAP_WRITE;

include/linux/uio.h

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct kvec {
2121
size_t iov_len;
2222
};
2323

24-
enum {
24+
enum iter_type {
2525
ITER_IOVEC = 0,
2626
ITER_KVEC = 2,
2727
ITER_BVEC = 4,
@@ -47,6 +47,36 @@ struct iov_iter {
4747
};
4848
};
4949

50+
static inline enum iter_type iov_iter_type(const struct iov_iter *i)
51+
{
52+
return i->type & ~(READ | WRITE);
53+
}
54+
55+
static inline bool iter_is_iovec(const struct iov_iter *i)
56+
{
57+
return iov_iter_type(i) == ITER_IOVEC;
58+
}
59+
60+
static inline bool iov_iter_is_kvec(const struct iov_iter *i)
61+
{
62+
return iov_iter_type(i) == ITER_KVEC;
63+
}
64+
65+
static inline bool iov_iter_is_bvec(const struct iov_iter *i)
66+
{
67+
return iov_iter_type(i) == ITER_BVEC;
68+
}
69+
70+
static inline bool iov_iter_is_pipe(const struct iov_iter *i)
71+
{
72+
return iov_iter_type(i) == ITER_PIPE;
73+
}
74+
75+
static inline unsigned char iov_iter_rw(const struct iov_iter *i)
76+
{
77+
return i->type & (READ | WRITE);
78+
}
79+
5080
/*
5181
* Total number of bytes covered by an iovec.
5282
*
@@ -74,7 +104,8 @@ static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
74104
}
75105

76106
#define iov_for_each(iov, iter, start) \
77-
if (!((start).type & (ITER_BVEC | ITER_PIPE))) \
107+
if (iov_iter_type(start) == ITER_IOVEC || \
108+
iov_iter_type(start) == ITER_KVEC) \
78109
for (iter = (start); \
79110
(iter).count && \
80111
((iov = iov_iter_iovec(&(iter))), 1); \
@@ -202,19 +233,6 @@ static inline size_t iov_iter_count(const struct iov_iter *i)
202233
return i->count;
203234
}
204235

205-
static inline bool iter_is_iovec(const struct iov_iter *i)
206-
{
207-
return !(i->type & (ITER_BVEC | ITER_KVEC | ITER_PIPE));
208-
}
209-
210-
/*
211-
* Get one of READ or WRITE out of iter->type without any other flags OR'd in
212-
* with it.
213-
*
214-
* The ?: is just for type safety.
215-
*/
216-
#define iov_iter_rw(i) ((0 ? (struct iov_iter *)0 : (i))->type & (READ | WRITE))
217-
218236
/*
219237
* Cap the iov_iter by given limit; note that the second argument is
220238
* *not* the new size - it's upper limit for such. Passing it a value

0 commit comments

Comments
 (0)