Commit | Line | Data |
---|---|---|
cd40cae2 JA |
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/fdtable.h> | |
7 | #include <linux/fsnotify.h> | |
8 | #include <linux/namei.h> | |
9 | #include <linux/io_uring.h> | |
10 | ||
11 | #include <uapi/linux/io_uring.h> | |
12 | ||
13 | #include "../fs/internal.h" | |
14 | ||
cd40cae2 | 15 | #include "io_uring.h" |
73572984 | 16 | #include "rsrc.h" |
cd40cae2 JA |
17 | #include "openclose.h" |
18 | ||
19 | struct io_open { | |
20 | struct file *file; | |
21 | int dfd; | |
22 | u32 file_slot; | |
23 | struct filename *filename; | |
24 | struct open_how how; | |
25 | unsigned long nofile; | |
26 | }; | |
27 | ||
28 | struct io_close { | |
29 | struct file *file; | |
30 | int fd; | |
31 | u32 file_slot; | |
32 | }; | |
33 | ||
0ffae640 DY |
34 | static bool io_openat_force_async(struct io_open *open) |
35 | { | |
36 | /* | |
37 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
72dbde0f AS |
38 | * it'll always -EAGAIN. Note that we test for __O_TMPFILE because |
39 | * O_TMPFILE includes O_DIRECTORY, which isn't a flag we need to force | |
40 | * async for. | |
0ffae640 | 41 | */ |
72dbde0f | 42 | return open->how.flags & (O_TRUNC | O_CREAT | __O_TMPFILE); |
0ffae640 DY |
43 | } |
44 | ||
cd40cae2 JA |
45 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
46 | { | |
f2ccb5ae | 47 | struct io_open *open = io_kiocb_to_cmd(req, struct io_open); |
cd40cae2 JA |
48 | const char __user *fname; |
49 | int ret; | |
50 | ||
51 | if (unlikely(sqe->buf_index)) | |
52 | return -EINVAL; | |
53 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
54 | return -EBADF; | |
55 | ||
56 | /* open.how should be already initialised */ | |
57 | if (!(open->how.flags & O_PATH) && force_o_largefile()) | |
58 | open->how.flags |= O_LARGEFILE; | |
59 | ||
60 | open->dfd = READ_ONCE(sqe->fd); | |
61 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
62 | open->filename = getname(fname); | |
63 | if (IS_ERR(open->filename)) { | |
64 | ret = PTR_ERR(open->filename); | |
65 | open->filename = NULL; | |
66 | return ret; | |
67 | } | |
68 | ||
69 | open->file_slot = READ_ONCE(sqe->file_index); | |
70 | if (open->file_slot && (open->how.flags & O_CLOEXEC)) | |
71 | return -EINVAL; | |
72 | ||
73 | open->nofile = rlimit(RLIMIT_NOFILE); | |
74 | req->flags |= REQ_F_NEED_CLEANUP; | |
0ffae640 DY |
75 | if (io_openat_force_async(open)) |
76 | req->flags |= REQ_F_FORCE_ASYNC; | |
cd40cae2 JA |
77 | return 0; |
78 | } | |
79 | ||
80 | int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
81 | { | |
f2ccb5ae | 82 | struct io_open *open = io_kiocb_to_cmd(req, struct io_open); |
cd40cae2 JA |
83 | u64 mode = READ_ONCE(sqe->len); |
84 | u64 flags = READ_ONCE(sqe->open_flags); | |
85 | ||
86 | open->how = build_open_how(flags, mode); | |
87 | return __io_openat_prep(req, sqe); | |
88 | } | |
89 | ||
90 | int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
91 | { | |
f2ccb5ae | 92 | struct io_open *open = io_kiocb_to_cmd(req, struct io_open); |
cd40cae2 JA |
93 | struct open_how __user *how; |
94 | size_t len; | |
95 | int ret; | |
96 | ||
97 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
98 | len = READ_ONCE(sqe->len); | |
99 | if (len < OPEN_HOW_SIZE_VER0) | |
100 | return -EINVAL; | |
101 | ||
102 | ret = copy_struct_from_user(&open->how, sizeof(open->how), how, len); | |
103 | if (ret) | |
104 | return ret; | |
105 | ||
106 | return __io_openat_prep(req, sqe); | |
107 | } | |
108 | ||
109 | int io_openat2(struct io_kiocb *req, unsigned int issue_flags) | |
110 | { | |
f2ccb5ae | 111 | struct io_open *open = io_kiocb_to_cmd(req, struct io_open); |
cd40cae2 JA |
112 | struct open_flags op; |
113 | struct file *file; | |
114 | bool resolve_nonblock, nonblock_set; | |
115 | bool fixed = !!open->file_slot; | |
116 | int ret; | |
117 | ||
118 | ret = build_open_flags(&open->how, &op); | |
119 | if (ret) | |
120 | goto err; | |
121 | nonblock_set = op.open_flag & O_NONBLOCK; | |
122 | resolve_nonblock = open->how.resolve & RESOLVE_CACHED; | |
123 | if (issue_flags & IO_URING_F_NONBLOCK) { | |
0ffae640 | 124 | WARN_ON_ONCE(io_openat_force_async(open)); |
cd40cae2 JA |
125 | op.lookup_flags |= LOOKUP_CACHED; |
126 | op.open_flag |= O_NONBLOCK; | |
127 | } | |
128 | ||
129 | if (!fixed) { | |
130 | ret = __get_unused_fd_flags(open->how.flags, open->nofile); | |
131 | if (ret < 0) | |
132 | goto err; | |
133 | } | |
134 | ||
135 | file = do_filp_open(open->dfd, open->filename, &op); | |
136 | if (IS_ERR(file)) { | |
137 | /* | |
138 | * We could hang on to this 'fd' on retrying, but seems like | |
139 | * marginal gain for something that is now known to be a slower | |
140 | * path. So just put it, and we'll get a new one when we retry. | |
141 | */ | |
142 | if (!fixed) | |
143 | put_unused_fd(ret); | |
144 | ||
145 | ret = PTR_ERR(file); | |
146 | /* only retry if RESOLVE_CACHED wasn't already set by application */ | |
147 | if (ret == -EAGAIN && | |
148 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
149 | return -EAGAIN; | |
150 | goto err; | |
151 | } | |
152 | ||
153 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
154 | file->f_flags &= ~O_NONBLOCK; | |
cd40cae2 JA |
155 | |
156 | if (!fixed) | |
157 | fd_install(ret, file); | |
158 | else | |
159 | ret = io_fixed_fd_install(req, issue_flags, file, | |
160 | open->file_slot); | |
161 | err: | |
162 | putname(open->filename); | |
163 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
164 | if (ret < 0) | |
165 | req_set_fail(req); | |
166 | io_req_set_res(req, ret, 0); | |
167 | return IOU_OK; | |
168 | } | |
169 | ||
170 | int io_openat(struct io_kiocb *req, unsigned int issue_flags) | |
171 | { | |
172 | return io_openat2(req, issue_flags); | |
173 | } | |
174 | ||
175 | void io_open_cleanup(struct io_kiocb *req) | |
176 | { | |
f2ccb5ae | 177 | struct io_open *open = io_kiocb_to_cmd(req, struct io_open); |
cd40cae2 JA |
178 | |
179 | if (open->filename) | |
180 | putname(open->filename); | |
181 | } | |
182 | ||
f110ed84 | 183 | int __io_close_fixed(struct io_ring_ctx *ctx, unsigned int issue_flags, |
cd40cae2 JA |
184 | unsigned int offset) |
185 | { | |
cd40cae2 JA |
186 | int ret; |
187 | ||
188 | io_ring_submit_lock(ctx, issue_flags); | |
f110ed84 | 189 | ret = io_fixed_fd_remove(ctx, offset); |
cd40cae2 | 190 | io_ring_submit_unlock(ctx, issue_flags); |
f110ed84 | 191 | |
cd40cae2 JA |
192 | return ret; |
193 | } | |
194 | ||
195 | static inline int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) | |
196 | { | |
f2ccb5ae | 197 | struct io_close *close = io_kiocb_to_cmd(req, struct io_close); |
cd40cae2 | 198 | |
f110ed84 | 199 | return __io_close_fixed(req->ctx, issue_flags, close->file_slot - 1); |
cd40cae2 JA |
200 | } |
201 | ||
202 | int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
203 | { | |
f2ccb5ae | 204 | struct io_close *close = io_kiocb_to_cmd(req, struct io_close); |
cd40cae2 JA |
205 | |
206 | if (sqe->off || sqe->addr || sqe->len || sqe->rw_flags || sqe->buf_index) | |
207 | return -EINVAL; | |
208 | if (req->flags & REQ_F_FIXED_FILE) | |
209 | return -EBADF; | |
210 | ||
211 | close->fd = READ_ONCE(sqe->fd); | |
212 | close->file_slot = READ_ONCE(sqe->file_index); | |
213 | if (close->file_slot && close->fd) | |
214 | return -EINVAL; | |
215 | ||
216 | return 0; | |
217 | } | |
218 | ||
219 | int io_close(struct io_kiocb *req, unsigned int issue_flags) | |
220 | { | |
221 | struct files_struct *files = current->files; | |
f2ccb5ae | 222 | struct io_close *close = io_kiocb_to_cmd(req, struct io_close); |
cd40cae2 JA |
223 | struct file *file; |
224 | int ret = -EBADF; | |
225 | ||
226 | if (close->file_slot) { | |
227 | ret = io_close_fixed(req, issue_flags); | |
228 | goto err; | |
229 | } | |
230 | ||
231 | spin_lock(&files->file_lock); | |
50d910d2 | 232 | file = files_lookup_fd_locked(files, close->fd); |
cd40cae2 JA |
233 | if (!file || io_is_uring_fops(file)) { |
234 | spin_unlock(&files->file_lock); | |
235 | goto err; | |
236 | } | |
237 | ||
238 | /* if the file has a flush method, be safe and punt to async */ | |
239 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { | |
240 | spin_unlock(&files->file_lock); | |
241 | return -EAGAIN; | |
242 | } | |
243 | ||
244 | file = __close_fd_get_file(close->fd); | |
245 | spin_unlock(&files->file_lock); | |
246 | if (!file) | |
247 | goto err; | |
248 | ||
249 | /* No ->flush() or already async, safely close from here */ | |
250 | ret = filp_close(file, current->files); | |
251 | err: | |
252 | if (ret < 0) | |
253 | req_set_fail(req); | |
254 | io_req_set_res(req, ret, 0); | |
255 | return IOU_OK; | |
256 | } |