~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/io_uring/fs.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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/io_uring.h>
 12 
 13 #include "../fs/internal.h"
 14 
 15 #include "io_uring.h"
 16 #include "fs.h"
 17 
 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;
 25 };
 26 
 27 struct io_unlink {
 28         struct file                     *file;
 29         int                             dfd;
 30         int                             flags;
 31         struct filename                 *filename;
 32 };
 33 
 34 struct io_mkdir {
 35         struct file                     *file;
 36         int                             dfd;
 37         umode_t                         mode;
 38         struct filename                 *filename;
 39 };
 40 
 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;
 48 };
 49 
 50 int io_renameat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 51 {
 52         struct io_rename *ren = io_kiocb_to_cmd(req, struct io_rename);
 53         const char __user *oldf, *newf;
 54 
 55         if (sqe->buf_index || sqe->splice_fd_in)
 56                 return -EINVAL;
 57         if (unlikely(req->flags & REQ_F_FIXED_FILE))
 58                 return -EBADF;
 59 
 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);
 65 
 66         ren->oldpath = getname(oldf);
 67         if (IS_ERR(ren->oldpath))
 68                 return PTR_ERR(ren->oldpath);
 69 
 70         ren->newpath = getname(newf);
 71         if (IS_ERR(ren->newpath)) {
 72                 putname(ren->oldpath);
 73                 return PTR_ERR(ren->newpath);
 74         }
 75 
 76         req->flags |= REQ_F_NEED_CLEANUP;
 77         req->flags |= REQ_F_FORCE_ASYNC;
 78         return 0;
 79 }
 80 
 81 int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
 82 {
 83         struct io_rename *ren = io_kiocb_to_cmd(req, struct io_rename);
 84         int ret;
 85 
 86         WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
 87 
 88         ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
 89                                 ren->newpath, ren->flags);
 90 
 91         req->flags &= ~REQ_F_NEED_CLEANUP;
 92         io_req_set_res(req, ret, 0);
 93         return IOU_OK;
 94 }
 95 
 96 void io_renameat_cleanup(struct io_kiocb *req)
 97 {
 98         struct io_rename *ren = io_kiocb_to_cmd(req, struct io_rename);
 99 
100         putname(ren->oldpath);
101         putname(ren->newpath);
102 }
103 
104 int io_unlinkat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
105 {
106         struct io_unlink *un = io_kiocb_to_cmd(req, struct io_unlink);
107         const char __user *fname;
108 
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;
113 
114         un->dfd = READ_ONCE(sqe->fd);
115 
116         un->flags = READ_ONCE(sqe->unlink_flags);
117         if (un->flags & ~AT_REMOVEDIR)
118                 return -EINVAL;
119 
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);
124 
125         req->flags |= REQ_F_NEED_CLEANUP;
126         req->flags |= REQ_F_FORCE_ASYNC;
127         return 0;
128 }
129 
130 int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
131 {
132         struct io_unlink *un = io_kiocb_to_cmd(req, struct io_unlink);
133         int ret;
134 
135         WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
136 
137         if (un->flags & AT_REMOVEDIR)
138                 ret = do_rmdir(un->dfd, un->filename);
139         else
140                 ret = do_unlinkat(un->dfd, un->filename);
141 
142         req->flags &= ~REQ_F_NEED_CLEANUP;
143         io_req_set_res(req, ret, 0);
144         return IOU_OK;
145 }
146 
147 void io_unlinkat_cleanup(struct io_kiocb *req)
148 {
149         struct io_unlink *ul = io_kiocb_to_cmd(req, struct io_unlink);
150 
151         putname(ul->filename);
152 }
153 
154 int io_mkdirat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
155 {
156         struct io_mkdir *mkd = io_kiocb_to_cmd(req, struct io_mkdir);
157         const char __user *fname;
158 
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;
163 
164         mkd->dfd = READ_ONCE(sqe->fd);
165         mkd->mode = READ_ONCE(sqe->len);
166 
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);
171 
172         req->flags |= REQ_F_NEED_CLEANUP;
173         req->flags |= REQ_F_FORCE_ASYNC;
174         return 0;
175 }
176 
177 int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags)
178 {
179         struct io_mkdir *mkd = io_kiocb_to_cmd(req, struct io_mkdir);
180         int ret;
181 
182         WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
183 
184         ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode);
185 
186         req->flags &= ~REQ_F_NEED_CLEANUP;
187         io_req_set_res(req, ret, 0);
188         return IOU_OK;
189 }
190 
191 void io_mkdirat_cleanup(struct io_kiocb *req)
192 {
193         struct io_mkdir *md = io_kiocb_to_cmd(req, struct io_mkdir);
194 
195         putname(md->filename);
196 }
197 
198 int io_symlinkat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
199 {
200         struct io_link *sl = io_kiocb_to_cmd(req, struct io_link);
201         const char __user *oldpath, *newpath;
202 
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;
207 
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));
211 
212         sl->oldpath = getname(oldpath);
213         if (IS_ERR(sl->oldpath))
214                 return PTR_ERR(sl->oldpath);
215 
216         sl->newpath = getname(newpath);
217         if (IS_ERR(sl->newpath)) {
218                 putname(sl->oldpath);
219                 return PTR_ERR(sl->newpath);
220         }
221 
222         req->flags |= REQ_F_NEED_CLEANUP;
223         req->flags |= REQ_F_FORCE_ASYNC;
224         return 0;
225 }
226 
227 int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags)
228 {
229         struct io_link *sl = io_kiocb_to_cmd(req, struct io_link);
230         int ret;
231 
232         WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
233 
234         ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath);
235 
236         req->flags &= ~REQ_F_NEED_CLEANUP;
237         io_req_set_res(req, ret, 0);
238         return IOU_OK;
239 }
240 
241 int io_linkat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
242 {
243         struct io_link *lnk = io_kiocb_to_cmd(req, struct io_link);
244         const char __user *oldf, *newf;
245 
246         if (sqe->buf_index || sqe->splice_fd_in)
247                 return -EINVAL;
248         if (unlikely(req->flags & REQ_F_FIXED_FILE))
249                 return -EBADF;
250 
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);
256 
257         lnk->oldpath = getname_uflags(oldf, lnk->flags);
258         if (IS_ERR(lnk->oldpath))
259                 return PTR_ERR(lnk->oldpath);
260 
261         lnk->newpath = getname(newf);
262         if (IS_ERR(lnk->newpath)) {
263                 putname(lnk->oldpath);
264                 return PTR_ERR(lnk->newpath);
265         }
266 
267         req->flags |= REQ_F_NEED_CLEANUP;
268         req->flags |= REQ_F_FORCE_ASYNC;
269         return 0;
270 }
271 
272 int io_linkat(struct io_kiocb *req, unsigned int issue_flags)
273 {
274         struct io_link *lnk = io_kiocb_to_cmd(req, struct io_link);
275         int ret;
276 
277         WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
278 
279         ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd,
280                                 lnk->newpath, lnk->flags);
281 
282         req->flags &= ~REQ_F_NEED_CLEANUP;
283         io_req_set_res(req, ret, 0);
284         return IOU_OK;
285 }
286 
287 void io_link_cleanup(struct io_kiocb *req)
288 {
289         struct io_link *sl = io_kiocb_to_cmd(req, struct io_link);
290 
291         putname(sl->oldpath);
292         putname(sl->newpath);
293 }
294 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php