|
| 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_types.h" |
| 16 | +#include "io_uring.h" |
| 17 | +#include "fs.h" |
| 18 | + |
| 19 | +struct io_rename { |
| 20 | + struct file *file; |
| 21 | + int old_dfd; |
| 22 | + int new_dfd; |
| 23 | + struct filename *oldpath; |
| 24 | + struct filename *newpath; |
| 25 | + int flags; |
| 26 | +}; |
| 27 | + |
| 28 | +struct io_unlink { |
| 29 | + struct file *file; |
| 30 | + int dfd; |
| 31 | + int flags; |
| 32 | + struct filename *filename; |
| 33 | +}; |
| 34 | + |
| 35 | +struct io_mkdir { |
| 36 | + struct file *file; |
| 37 | + int dfd; |
| 38 | + umode_t mode; |
| 39 | + struct filename *filename; |
| 40 | +}; |
| 41 | + |
| 42 | +struct io_link { |
| 43 | + struct file *file; |
| 44 | + int old_dfd; |
| 45 | + int new_dfd; |
| 46 | + struct filename *oldpath; |
| 47 | + struct filename *newpath; |
| 48 | + int flags; |
| 49 | +}; |
| 50 | + |
| 51 | +int io_renameat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 52 | +{ |
| 53 | + struct io_rename *ren = io_kiocb_to_cmd(req); |
| 54 | + const char __user *oldf, *newf; |
| 55 | + |
| 56 | + if (sqe->buf_index || sqe->splice_fd_in) |
| 57 | + return -EINVAL; |
| 58 | + if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 59 | + return -EBADF; |
| 60 | + |
| 61 | + ren->old_dfd = READ_ONCE(sqe->fd); |
| 62 | + oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 63 | + newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 64 | + ren->new_dfd = READ_ONCE(sqe->len); |
| 65 | + ren->flags = READ_ONCE(sqe->rename_flags); |
| 66 | + |
| 67 | + ren->oldpath = getname(oldf); |
| 68 | + if (IS_ERR(ren->oldpath)) |
| 69 | + return PTR_ERR(ren->oldpath); |
| 70 | + |
| 71 | + ren->newpath = getname(newf); |
| 72 | + if (IS_ERR(ren->newpath)) { |
| 73 | + putname(ren->oldpath); |
| 74 | + return PTR_ERR(ren->newpath); |
| 75 | + } |
| 76 | + |
| 77 | + req->flags |= REQ_F_NEED_CLEANUP; |
| 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); |
| 84 | + int ret; |
| 85 | + |
| 86 | + if (issue_flags & IO_URING_F_NONBLOCK) |
| 87 | + return -EAGAIN; |
| 88 | + |
| 89 | + ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 90 | + ren->newpath, ren->flags); |
| 91 | + |
| 92 | + req->flags &= ~REQ_F_NEED_CLEANUP; |
| 93 | + io_req_set_res(req, ret, 0); |
| 94 | + return IOU_OK; |
| 95 | +} |
| 96 | + |
| 97 | +void io_renameat_cleanup(struct io_kiocb *req) |
| 98 | +{ |
| 99 | + struct io_rename *ren = io_kiocb_to_cmd(req); |
| 100 | + |
| 101 | + putname(ren->oldpath); |
| 102 | + putname(ren->newpath); |
| 103 | +} |
| 104 | + |
| 105 | +int io_unlinkat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 106 | +{ |
| 107 | + struct io_unlink *un = io_kiocb_to_cmd(req); |
| 108 | + const char __user *fname; |
| 109 | + |
| 110 | + if (sqe->off || sqe->len || sqe->buf_index || sqe->splice_fd_in) |
| 111 | + return -EINVAL; |
| 112 | + if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 113 | + return -EBADF; |
| 114 | + |
| 115 | + un->dfd = READ_ONCE(sqe->fd); |
| 116 | + |
| 117 | + un->flags = READ_ONCE(sqe->unlink_flags); |
| 118 | + if (un->flags & ~AT_REMOVEDIR) |
| 119 | + return -EINVAL; |
| 120 | + |
| 121 | + fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 122 | + un->filename = getname(fname); |
| 123 | + if (IS_ERR(un->filename)) |
| 124 | + return PTR_ERR(un->filename); |
| 125 | + |
| 126 | + req->flags |= REQ_F_NEED_CLEANUP; |
| 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); |
| 133 | + int ret; |
| 134 | + |
| 135 | + if (issue_flags & IO_URING_F_NONBLOCK) |
| 136 | + return -EAGAIN; |
| 137 | + |
| 138 | + if (un->flags & AT_REMOVEDIR) |
| 139 | + ret = do_rmdir(un->dfd, un->filename); |
| 140 | + else |
| 141 | + ret = do_unlinkat(un->dfd, un->filename); |
| 142 | + |
| 143 | + req->flags &= ~REQ_F_NEED_CLEANUP; |
| 144 | + io_req_set_res(req, ret, 0); |
| 145 | + return IOU_OK; |
| 146 | +} |
| 147 | + |
| 148 | +void io_unlinkat_cleanup(struct io_kiocb *req) |
| 149 | +{ |
| 150 | + struct io_unlink *ul = io_kiocb_to_cmd(req); |
| 151 | + |
| 152 | + putname(ul->filename); |
| 153 | +} |
| 154 | + |
| 155 | +int io_mkdirat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 156 | +{ |
| 157 | + struct io_mkdir *mkd = io_kiocb_to_cmd(req); |
| 158 | + const char __user *fname; |
| 159 | + |
| 160 | + if (sqe->off || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) |
| 161 | + return -EINVAL; |
| 162 | + if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 163 | + return -EBADF; |
| 164 | + |
| 165 | + mkd->dfd = READ_ONCE(sqe->fd); |
| 166 | + mkd->mode = READ_ONCE(sqe->len); |
| 167 | + |
| 168 | + fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 169 | + mkd->filename = getname(fname); |
| 170 | + if (IS_ERR(mkd->filename)) |
| 171 | + return PTR_ERR(mkd->filename); |
| 172 | + |
| 173 | + req->flags |= REQ_F_NEED_CLEANUP; |
| 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); |
| 180 | + int ret; |
| 181 | + |
| 182 | + if (issue_flags & IO_URING_F_NONBLOCK) |
| 183 | + return -EAGAIN; |
| 184 | + |
| 185 | + ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); |
| 186 | + |
| 187 | + req->flags &= ~REQ_F_NEED_CLEANUP; |
| 188 | + io_req_set_res(req, ret, 0); |
| 189 | + return IOU_OK; |
| 190 | +} |
| 191 | + |
| 192 | +void io_mkdirat_cleanup(struct io_kiocb *req) |
| 193 | +{ |
| 194 | + struct io_mkdir *md = io_kiocb_to_cmd(req); |
| 195 | + |
| 196 | + putname(md->filename); |
| 197 | +} |
| 198 | + |
| 199 | +int io_symlinkat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 200 | +{ |
| 201 | + struct io_link *sl = io_kiocb_to_cmd(req); |
| 202 | + const char __user *oldpath, *newpath; |
| 203 | + |
| 204 | + if (sqe->len || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) |
| 205 | + return -EINVAL; |
| 206 | + if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 207 | + return -EBADF; |
| 208 | + |
| 209 | + sl->new_dfd = READ_ONCE(sqe->fd); |
| 210 | + oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 211 | + newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 212 | + |
| 213 | + sl->oldpath = getname(oldpath); |
| 214 | + if (IS_ERR(sl->oldpath)) |
| 215 | + return PTR_ERR(sl->oldpath); |
| 216 | + |
| 217 | + sl->newpath = getname(newpath); |
| 218 | + if (IS_ERR(sl->newpath)) { |
| 219 | + putname(sl->oldpath); |
| 220 | + return PTR_ERR(sl->newpath); |
| 221 | + } |
| 222 | + |
| 223 | + req->flags |= REQ_F_NEED_CLEANUP; |
| 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); |
| 230 | + int ret; |
| 231 | + |
| 232 | + if (issue_flags & IO_URING_F_NONBLOCK) |
| 233 | + return -EAGAIN; |
| 234 | + |
| 235 | + ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); |
| 236 | + |
| 237 | + req->flags &= ~REQ_F_NEED_CLEANUP; |
| 238 | + io_req_set_res(req, ret, 0); |
| 239 | + return IOU_OK; |
| 240 | +} |
| 241 | + |
| 242 | +int io_linkat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 243 | +{ |
| 244 | + struct io_link *lnk = io_kiocb_to_cmd(req); |
| 245 | + const char __user *oldf, *newf; |
| 246 | + |
| 247 | + if (sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) |
| 248 | + return -EINVAL; |
| 249 | + if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 250 | + return -EBADF; |
| 251 | + |
| 252 | + lnk->old_dfd = READ_ONCE(sqe->fd); |
| 253 | + lnk->new_dfd = READ_ONCE(sqe->len); |
| 254 | + oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 255 | + newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 256 | + lnk->flags = READ_ONCE(sqe->hardlink_flags); |
| 257 | + |
| 258 | + lnk->oldpath = getname(oldf); |
| 259 | + if (IS_ERR(lnk->oldpath)) |
| 260 | + return PTR_ERR(lnk->oldpath); |
| 261 | + |
| 262 | + lnk->newpath = getname(newf); |
| 263 | + if (IS_ERR(lnk->newpath)) { |
| 264 | + putname(lnk->oldpath); |
| 265 | + return PTR_ERR(lnk->newpath); |
| 266 | + } |
| 267 | + |
| 268 | + req->flags |= REQ_F_NEED_CLEANUP; |
| 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); |
| 275 | + int ret; |
| 276 | + |
| 277 | + if (issue_flags & IO_URING_F_NONBLOCK) |
| 278 | + return -EAGAIN; |
| 279 | + |
| 280 | + ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, |
| 281 | + lnk->newpath, lnk->flags); |
| 282 | + |
| 283 | + req->flags &= ~REQ_F_NEED_CLEANUP; |
| 284 | + io_req_set_res(req, ret, 0); |
| 285 | + return IOU_OK; |
| 286 | +} |
| 287 | + |
| 288 | +void io_link_cleanup(struct io_kiocb *req) |
| 289 | +{ |
| 290 | + struct io_link *sl = io_kiocb_to_cmd(req); |
| 291 | + |
| 292 | + putname(sl->oldpath); |
| 293 | + putname(sl->newpath); |
| 294 | +} |
0 commit comments