Skip to content

Commit f4c163d

Browse files
committed
io_uring: split out fadvise/madvise operations
Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 0d58472 commit f4c163d

File tree

4 files changed

+109
-85
lines changed

4 files changed

+109
-85
lines changed

io_uring/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
# Makefile for io_uring
44

55
obj-$(CONFIG_IO_URING) += io_uring.o xattr.o nop.o fs.o splice.o \
6-
sync.o
6+
sync.o advise.o
77
obj-$(CONFIG_IO_WQ) += io-wq.o

io_uring/advise.c

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/kernel.h>
3+
#include <linux/errno.h>
4+
#include <linux/fs.h>
5+
#include <linux/file.h>
6+
#include <linux/mm.h>
7+
#include <linux/slab.h>
8+
#include <linux/namei.h>
9+
#include <linux/io_uring.h>
10+
11+
#include <uapi/linux/fadvise.h>
12+
#include <uapi/linux/io_uring.h>
13+
14+
#include "io_uring_types.h"
15+
#include "io_uring.h"
16+
#include "advise.h"
17+
18+
struct io_fadvise {
19+
struct file *file;
20+
u64 offset;
21+
u32 len;
22+
u32 advice;
23+
};
24+
25+
struct io_madvise {
26+
struct file *file;
27+
u64 addr;
28+
u32 len;
29+
u32 advice;
30+
};
31+
32+
int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
33+
{
34+
#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
35+
struct io_madvise *ma = io_kiocb_to_cmd(req);
36+
37+
if (sqe->buf_index || sqe->off || sqe->splice_fd_in)
38+
return -EINVAL;
39+
40+
ma->addr = READ_ONCE(sqe->addr);
41+
ma->len = READ_ONCE(sqe->len);
42+
ma->advice = READ_ONCE(sqe->fadvise_advice);
43+
return 0;
44+
#else
45+
return -EOPNOTSUPP;
46+
#endif
47+
}
48+
49+
int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
50+
{
51+
#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
52+
struct io_madvise *ma = io_kiocb_to_cmd(req);
53+
int ret;
54+
55+
if (issue_flags & IO_URING_F_NONBLOCK)
56+
return -EAGAIN;
57+
58+
ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
59+
io_req_set_res(req, ret, 0);
60+
return IOU_OK;
61+
#else
62+
return -EOPNOTSUPP;
63+
#endif
64+
}
65+
66+
int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
67+
{
68+
struct io_fadvise *fa = io_kiocb_to_cmd(req);
69+
70+
if (sqe->buf_index || sqe->addr || sqe->splice_fd_in)
71+
return -EINVAL;
72+
73+
fa->offset = READ_ONCE(sqe->off);
74+
fa->len = READ_ONCE(sqe->len);
75+
fa->advice = READ_ONCE(sqe->fadvise_advice);
76+
return 0;
77+
}
78+
79+
int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
80+
{
81+
struct io_fadvise *fa = io_kiocb_to_cmd(req);
82+
int ret;
83+
84+
if (issue_flags & IO_URING_F_NONBLOCK) {
85+
switch (fa->advice) {
86+
case POSIX_FADV_NORMAL:
87+
case POSIX_FADV_RANDOM:
88+
case POSIX_FADV_SEQUENTIAL:
89+
break;
90+
default:
91+
return -EAGAIN;
92+
}
93+
}
94+
95+
ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
96+
if (ret < 0)
97+
req_set_fail(req);
98+
io_req_set_res(req, ret, 0);
99+
return IOU_OK;
100+
}

io_uring/advise.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
4+
int io_madvise(struct io_kiocb *req, unsigned int issue_flags);
5+
6+
int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
7+
int io_fadvise(struct io_kiocb *req, unsigned int issue_flags);

io_uring/io_uring.c

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
#include "fs.h"
9898
#include "splice.h"
9999
#include "sync.h"
100+
#include "advise.h"
100101

101102
#define IORING_MAX_ENTRIES 32768
102103
#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
@@ -408,20 +409,6 @@ struct io_rsrc_update {
408409
u32 offset;
409410
};
410411

411-
struct io_fadvise {
412-
struct file *file;
413-
u64 offset;
414-
u32 len;
415-
u32 advice;
416-
};
417-
418-
struct io_madvise {
419-
struct file *file;
420-
u64 addr;
421-
u32 len;
422-
u32 advice;
423-
};
424-
425412
struct io_epoll {
426413
struct file *file;
427414
int epfd;
@@ -4428,76 +4415,6 @@ static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
44284415
#endif
44294416
}
44304417

4431-
static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4432-
{
4433-
#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4434-
struct io_madvise *ma = io_kiocb_to_cmd(req);
4435-
4436-
if (sqe->buf_index || sqe->off || sqe->splice_fd_in)
4437-
return -EINVAL;
4438-
4439-
ma->addr = READ_ONCE(sqe->addr);
4440-
ma->len = READ_ONCE(sqe->len);
4441-
ma->advice = READ_ONCE(sqe->fadvise_advice);
4442-
return 0;
4443-
#else
4444-
return -EOPNOTSUPP;
4445-
#endif
4446-
}
4447-
4448-
static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
4449-
{
4450-
#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4451-
struct io_madvise *ma = io_kiocb_to_cmd(req);
4452-
int ret;
4453-
4454-
if (issue_flags & IO_URING_F_NONBLOCK)
4455-
return -EAGAIN;
4456-
4457-
ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
4458-
io_req_set_res(req, ret, 0);
4459-
return IOU_OK;
4460-
#else
4461-
return -EOPNOTSUPP;
4462-
#endif
4463-
}
4464-
4465-
static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4466-
{
4467-
struct io_fadvise *fa = io_kiocb_to_cmd(req);
4468-
4469-
if (sqe->buf_index || sqe->addr || sqe->splice_fd_in)
4470-
return -EINVAL;
4471-
4472-
fa->offset = READ_ONCE(sqe->off);
4473-
fa->len = READ_ONCE(sqe->len);
4474-
fa->advice = READ_ONCE(sqe->fadvise_advice);
4475-
return 0;
4476-
}
4477-
4478-
static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
4479-
{
4480-
struct io_fadvise *fa = io_kiocb_to_cmd(req);
4481-
int ret;
4482-
4483-
if (issue_flags & IO_URING_F_NONBLOCK) {
4484-
switch (fa->advice) {
4485-
case POSIX_FADV_NORMAL:
4486-
case POSIX_FADV_RANDOM:
4487-
case POSIX_FADV_SEQUENTIAL:
4488-
break;
4489-
default:
4490-
return -EAGAIN;
4491-
}
4492-
}
4493-
4494-
ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4495-
if (ret < 0)
4496-
req_set_fail(req);
4497-
io_req_set_res(req, ret, 0);
4498-
return IOU_OK;
4499-
}
4500-
45014418
static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
45024419
{
45034420
struct io_statx *sx = io_kiocb_to_cmd(req);

0 commit comments

Comments
 (0)