drm/panthor: Don't add write fences to the shared BOs
[drm/drm-misc.git] / io_uring / fs.c
blobeccea851dd5a2858936f8f0d9acc6c417614358a
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>
11 #include <uapi/linux/io_uring.h>
13 #include "../fs/internal.h"
15 #include "io_uring.h"
16 #include "fs.h"
18 struct io_rename {
19 struct file *file;
20 int old_dfd;
21 int new_dfd;
22 struct filename *oldpath;
23 struct filename *newpath;
24 int flags;
27 struct io_unlink {
28 struct file *file;
29 int dfd;
30 int flags;
31 struct filename *filename;
34 struct io_mkdir {
35 struct file *file;
36 int dfd;
37 umode_t mode;
38 struct filename *filename;
41 struct io_link {
42 struct file *file;
43 int old_dfd;
44 int new_dfd;
45 struct filename *oldpath;
46 struct filename *newpath;
47 int flags;
50 int io_renameat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
52 struct io_rename *ren = io_kiocb_to_cmd(req, struct io_rename);
53 const char __user *oldf, *newf;
55 if (sqe->buf_index || sqe->splice_fd_in)
56 return -EINVAL;
57 if (unlikely(req->flags & REQ_F_FIXED_FILE))
58 return -EBADF;
60 ren->old_dfd = READ_ONCE(sqe->fd);
61 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
62 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
63 ren->new_dfd = READ_ONCE(sqe->len);
64 ren->flags = READ_ONCE(sqe->rename_flags);
66 ren->oldpath = getname(oldf);
67 if (IS_ERR(ren->oldpath))
68 return PTR_ERR(ren->oldpath);
70 ren->newpath = getname(newf);
71 if (IS_ERR(ren->newpath)) {
72 putname(ren->oldpath);
73 return PTR_ERR(ren->newpath);
76 req->flags |= REQ_F_NEED_CLEANUP;
77 req->flags |= REQ_F_FORCE_ASYNC;
78 return 0;
81 int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
83 struct io_rename *ren = io_kiocb_to_cmd(req, struct io_rename);
84 int ret;
86 WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
88 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
89 ren->newpath, ren->flags);
91 req->flags &= ~REQ_F_NEED_CLEANUP;
92 io_req_set_res(req, ret, 0);
93 return IOU_OK;
96 void io_renameat_cleanup(struct io_kiocb *req)
98 struct io_rename *ren = io_kiocb_to_cmd(req, struct io_rename);
100 putname(ren->oldpath);
101 putname(ren->newpath);
104 int io_unlinkat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
106 struct io_unlink *un = io_kiocb_to_cmd(req, struct io_unlink);
107 const char __user *fname;
109 if (sqe->off || sqe->len || sqe->buf_index || sqe->splice_fd_in)
110 return -EINVAL;
111 if (unlikely(req->flags & REQ_F_FIXED_FILE))
112 return -EBADF;
114 un->dfd = READ_ONCE(sqe->fd);
116 un->flags = READ_ONCE(sqe->unlink_flags);
117 if (un->flags & ~AT_REMOVEDIR)
118 return -EINVAL;
120 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
121 un->filename = getname(fname);
122 if (IS_ERR(un->filename))
123 return PTR_ERR(un->filename);
125 req->flags |= REQ_F_NEED_CLEANUP;
126 req->flags |= REQ_F_FORCE_ASYNC;
127 return 0;
130 int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
132 struct io_unlink *un = io_kiocb_to_cmd(req, struct io_unlink);
133 int ret;
135 WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
137 if (un->flags & AT_REMOVEDIR)
138 ret = do_rmdir(un->dfd, un->filename);
139 else
140 ret = do_unlinkat(un->dfd, un->filename);
142 req->flags &= ~REQ_F_NEED_CLEANUP;
143 io_req_set_res(req, ret, 0);
144 return IOU_OK;
147 void io_unlinkat_cleanup(struct io_kiocb *req)
149 struct io_unlink *ul = io_kiocb_to_cmd(req, struct io_unlink);
151 putname(ul->filename);
154 int io_mkdirat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
156 struct io_mkdir *mkd = io_kiocb_to_cmd(req, struct io_mkdir);
157 const char __user *fname;
159 if (sqe->off || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
160 return -EINVAL;
161 if (unlikely(req->flags & REQ_F_FIXED_FILE))
162 return -EBADF;
164 mkd->dfd = READ_ONCE(sqe->fd);
165 mkd->mode = READ_ONCE(sqe->len);
167 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
168 mkd->filename = getname(fname);
169 if (IS_ERR(mkd->filename))
170 return PTR_ERR(mkd->filename);
172 req->flags |= REQ_F_NEED_CLEANUP;
173 req->flags |= REQ_F_FORCE_ASYNC;
174 return 0;
177 int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags)
179 struct io_mkdir *mkd = io_kiocb_to_cmd(req, struct io_mkdir);
180 int ret;
182 WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
184 ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode);
186 req->flags &= ~REQ_F_NEED_CLEANUP;
187 io_req_set_res(req, ret, 0);
188 return IOU_OK;
191 void io_mkdirat_cleanup(struct io_kiocb *req)
193 struct io_mkdir *md = io_kiocb_to_cmd(req, struct io_mkdir);
195 putname(md->filename);
198 int io_symlinkat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
200 struct io_link *sl = io_kiocb_to_cmd(req, struct io_link);
201 const char __user *oldpath, *newpath;
203 if (sqe->len || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
204 return -EINVAL;
205 if (unlikely(req->flags & REQ_F_FIXED_FILE))
206 return -EBADF;
208 sl->new_dfd = READ_ONCE(sqe->fd);
209 oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr));
210 newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2));
212 sl->oldpath = getname(oldpath);
213 if (IS_ERR(sl->oldpath))
214 return PTR_ERR(sl->oldpath);
216 sl->newpath = getname(newpath);
217 if (IS_ERR(sl->newpath)) {
218 putname(sl->oldpath);
219 return PTR_ERR(sl->newpath);
222 req->flags |= REQ_F_NEED_CLEANUP;
223 req->flags |= REQ_F_FORCE_ASYNC;
224 return 0;
227 int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags)
229 struct io_link *sl = io_kiocb_to_cmd(req, struct io_link);
230 int ret;
232 WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
234 ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath);
236 req->flags &= ~REQ_F_NEED_CLEANUP;
237 io_req_set_res(req, ret, 0);
238 return IOU_OK;
241 int io_linkat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
243 struct io_link *lnk = io_kiocb_to_cmd(req, struct io_link);
244 const char __user *oldf, *newf;
246 if (sqe->buf_index || sqe->splice_fd_in)
247 return -EINVAL;
248 if (unlikely(req->flags & REQ_F_FIXED_FILE))
249 return -EBADF;
251 lnk->old_dfd = READ_ONCE(sqe->fd);
252 lnk->new_dfd = READ_ONCE(sqe->len);
253 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
254 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
255 lnk->flags = READ_ONCE(sqe->hardlink_flags);
257 lnk->oldpath = getname_uflags(oldf, lnk->flags);
258 if (IS_ERR(lnk->oldpath))
259 return PTR_ERR(lnk->oldpath);
261 lnk->newpath = getname(newf);
262 if (IS_ERR(lnk->newpath)) {
263 putname(lnk->oldpath);
264 return PTR_ERR(lnk->newpath);
267 req->flags |= REQ_F_NEED_CLEANUP;
268 req->flags |= REQ_F_FORCE_ASYNC;
269 return 0;
272 int io_linkat(struct io_kiocb *req, unsigned int issue_flags)
274 struct io_link *lnk = io_kiocb_to_cmd(req, struct io_link);
275 int ret;
277 WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
279 ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd,
280 lnk->newpath, lnk->flags);
282 req->flags &= ~REQ_F_NEED_CLEANUP;
283 io_req_set_res(req, ret, 0);
284 return IOU_OK;
287 void io_link_cleanup(struct io_kiocb *req)
289 struct io_link *sl = io_kiocb_to_cmd(req, struct io_link);
291 putname(sl->oldpath);
292 putname(sl->newpath);