Skip to content

Commit ec65fea

Browse files
isilenceaxboe
authored andcommitted
io_uring: deduplicate io_openat{,2}_prep()
io_openat_prep() and io_openat2_prep() are identical except for how struct open_how is built. Deduplicate it with a helper. Signed-off-by: Pavel Begunkov <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 25e72d1 commit ec65fea

File tree

1 file changed

+19
-36
lines changed

1 file changed

+19
-36
lines changed

fs/io_uring.c

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2990,26 +2990,21 @@ static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
29902990
return 0;
29912991
}
29922992

2993-
static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2993+
static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
29942994
{
29952995
const char __user *fname;
2996-
u64 flags, mode;
29972996
int ret;
29982997

29992998
if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
30002999
return -EINVAL;
3001-
if (sqe->ioprio || sqe->buf_index)
3000+
if (unlikely(sqe->ioprio || sqe->buf_index))
30023001
return -EINVAL;
3003-
if (req->flags & REQ_F_FIXED_FILE)
3002+
if (unlikely(req->flags & REQ_F_FIXED_FILE))
30043003
return -EBADF;
3005-
if (req->flags & REQ_F_NEED_CLEANUP)
3006-
return 0;
30073004

3008-
mode = READ_ONCE(sqe->len);
3009-
flags = READ_ONCE(sqe->open_flags);
3010-
if (force_o_largefile())
3011-
flags |= O_LARGEFILE;
3012-
req->open.how = build_open_how(flags, mode);
3005+
/* open.how should be already initialised */
3006+
if (!(req->open.how.flags & O_PATH) && force_o_largefile())
3007+
req->open.how.flags |= O_LARGEFILE;
30133008

30143009
req->open.dfd = READ_ONCE(sqe->fd);
30153010
fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
@@ -3019,33 +3014,33 @@ static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
30193014
req->open.filename = NULL;
30203015
return ret;
30213016
}
3022-
30233017
req->open.nofile = rlimit(RLIMIT_NOFILE);
30243018
req->flags |= REQ_F_NEED_CLEANUP;
30253019
return 0;
30263020
}
30273021

3022+
static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3023+
{
3024+
u64 flags, mode;
3025+
3026+
if (req->flags & REQ_F_NEED_CLEANUP)
3027+
return 0;
3028+
mode = READ_ONCE(sqe->len);
3029+
flags = READ_ONCE(sqe->open_flags);
3030+
req->open.how = build_open_how(flags, mode);
3031+
return __io_openat_prep(req, sqe);
3032+
}
3033+
30283034
static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
30293035
{
30303036
struct open_how __user *how;
3031-
const char __user *fname;
30323037
size_t len;
30333038
int ret;
30343039

3035-
if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3036-
return -EINVAL;
3037-
if (sqe->ioprio || sqe->buf_index)
3038-
return -EINVAL;
3039-
if (req->flags & REQ_F_FIXED_FILE)
3040-
return -EBADF;
30413040
if (req->flags & REQ_F_NEED_CLEANUP)
30423041
return 0;
3043-
3044-
req->open.dfd = READ_ONCE(sqe->fd);
3045-
fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
30463042
how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
30473043
len = READ_ONCE(sqe->len);
3048-
30493044
if (len < OPEN_HOW_SIZE_VER0)
30503045
return -EINVAL;
30513046

@@ -3054,19 +3049,7 @@ static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
30543049
if (ret)
30553050
return ret;
30563051

3057-
if (!(req->open.how.flags & O_PATH) && force_o_largefile())
3058-
req->open.how.flags |= O_LARGEFILE;
3059-
3060-
req->open.filename = getname(fname);
3061-
if (IS_ERR(req->open.filename)) {
3062-
ret = PTR_ERR(req->open.filename);
3063-
req->open.filename = NULL;
3064-
return ret;
3065-
}
3066-
3067-
req->open.nofile = rlimit(RLIMIT_NOFILE);
3068-
req->flags |= REQ_F_NEED_CLEANUP;
3069-
return 0;
3052+
return __io_openat_prep(req, sqe);
30703053
}
30713054

30723055
static int io_openat2(struct io_kiocb *req, bool force_nonblock)

0 commit comments

Comments
 (0)