提交 41435602 编写于 作者: P Pavel Begunkov 提交者: Xiaoguang Wang

io_uring: add interface for getting files

to #28170604

commit 8da11c19940ddbc22fc835bce3f361f4d2417fb0 upstream

Preparation without functional changes. Adds io_get_file(), that allows
to grab files not only into req->file.
Signed-off-by: NPavel Begunkov <asml.silence@gmail.com>
Signed-off-by: NJens Axboe <axboe@kernel.dk>
Acked-by: NJoseph Qi <joseph.qi@linux.alibaba.com>
Signed-off-by: NXiaoguang Wang <xiaoguang.wang@linux.alibaba.com>
上级 cab55dd4
...@@ -1256,6 +1256,15 @@ static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, ...@@ -1256,6 +1256,15 @@ static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
return NULL; return NULL;
} }
static inline void io_put_file(struct io_kiocb *req, struct file *file,
bool fixed)
{
if (fixed)
percpu_ref_put(&req->ctx->file_data->refs);
else
fput(file);
}
static void __io_req_do_free(struct io_kiocb *req) static void __io_req_do_free(struct io_kiocb *req)
{ {
if (likely(!io_is_fallback_req(req))) if (likely(!io_is_fallback_req(req)))
...@@ -1266,18 +1275,12 @@ static void __io_req_do_free(struct io_kiocb *req) ...@@ -1266,18 +1275,12 @@ static void __io_req_do_free(struct io_kiocb *req)
static void __io_req_aux_free(struct io_kiocb *req) static void __io_req_aux_free(struct io_kiocb *req)
{ {
struct io_ring_ctx *ctx = req->ctx;
if (req->flags & REQ_F_NEED_CLEANUP) if (req->flags & REQ_F_NEED_CLEANUP)
io_cleanup_req(req); io_cleanup_req(req);
kfree(req->io); kfree(req->io);
if (req->file) { if (req->file)
if (req->flags & REQ_F_FIXED_FILE) io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
percpu_ref_put(&ctx->file_data->refs);
else
fput(req->file);
}
io_req_work_drop_env(req); io_req_work_drop_env(req);
} }
...@@ -1853,7 +1856,7 @@ static void io_file_put(struct io_submit_state *state) ...@@ -1853,7 +1856,7 @@ static void io_file_put(struct io_submit_state *state)
* assuming most submissions are for one file, or at least that each file * assuming most submissions are for one file, or at least that each file
* has more than one submission. * has more than one submission.
*/ */
static struct file *io_file_get(struct io_submit_state *state, int fd) static struct file *__io_file_get(struct io_submit_state *state, int fd)
{ {
if (!state) if (!state)
return fget(fd); return fget(fd);
...@@ -4590,41 +4593,52 @@ static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, ...@@ -4590,41 +4593,52 @@ static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
return table->files[index & IORING_FILE_TABLE_MASK];; return table->files[index & IORING_FILE_TABLE_MASK];;
} }
static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
const struct io_uring_sqe *sqe) int fd, struct file **out_file, bool fixed)
{ {
struct io_ring_ctx *ctx = req->ctx; struct io_ring_ctx *ctx = req->ctx;
unsigned flags; struct file *file;
int fd;
flags = READ_ONCE(sqe->flags);
fd = READ_ONCE(sqe->fd);
if (!io_req_needs_file(req, fd))
return 0;
if (flags & IOSQE_FIXED_FILE) { if (fixed) {
if (unlikely(!ctx->file_data || if (unlikely(!ctx->file_data ||
(unsigned) fd >= ctx->nr_user_files)) (unsigned) fd >= ctx->nr_user_files))
return -EBADF; return -EBADF;
fd = array_index_nospec(fd, ctx->nr_user_files); fd = array_index_nospec(fd, ctx->nr_user_files);
req->file = io_file_from_index(ctx, fd); file = io_file_from_index(ctx, fd);
if (!req->file) if (!file)
return -EBADF; return -EBADF;
req->flags |= REQ_F_FIXED_FILE;
percpu_ref_get(&ctx->file_data->refs); percpu_ref_get(&ctx->file_data->refs);
} else { } else {
if (req->needs_fixed_file)
return -EBADF;
trace_io_uring_file_get(ctx, fd); trace_io_uring_file_get(ctx, fd);
req->file = io_file_get(state, fd); file = __io_file_get(state, fd);
if (unlikely(!req->file)) if (unlikely(!file))
return -EBADF; return -EBADF;
} }
*out_file = file;
return 0; return 0;
} }
static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
const struct io_uring_sqe *sqe)
{
unsigned flags;
int fd;
bool fixed;
flags = READ_ONCE(sqe->flags);
fd = READ_ONCE(sqe->fd);
if (!io_req_needs_file(req, fd))
return 0;
fixed = (flags & IOSQE_FIXED_FILE);
if (unlikely(!fixed && req->needs_fixed_file))
return -EBADF;
return io_file_get(state, req, fd, &req->file, fixed);
}
static int io_grab_files(struct io_kiocb *req) static int io_grab_files(struct io_kiocb *req)
{ {
int ret = -EBADF; int ret = -EBADF;
...@@ -4869,8 +4883,8 @@ static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, ...@@ -4869,8 +4883,8 @@ static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
} }
/* same numerical values with corresponding REQ_F_*, safe to copy */ /* same numerical values with corresponding REQ_F_*, safe to copy */
req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK| req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
IOSQE_ASYNC); IOSQE_ASYNC | IOSQE_FIXED_FILE);
ret = io_req_set_file(state, req, sqe); ret = io_req_set_file(state, req, sqe);
if (unlikely(ret)) { if (unlikely(ret)) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册