1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
5 #include <linux/file.h>
7 #include <linux/slab.h>
8 #include <linux/namei.h>
9 #include <linux/io_uring.h>
10 #include <linux/xattr.h>
12 #include <uapi/linux/io_uring.h>
14 #include "../fs/internal.h"
22 struct filename *filename;
25 void io_xattr_cleanup(struct io_kiocb *req)
27 struct io_xattr *ix = io_kiocb_to_cmd(req);
30 putname(ix->filename);
33 kvfree(ix->ctx.kvalue);
36 static void io_xattr_finish(struct io_kiocb *req, int ret)
38 req->flags &= ~REQ_F_NEED_CLEANUP;
40 io_xattr_cleanup(req);
41 io_req_set_res(req, ret, 0);
44 static int __io_getxattr_prep(struct io_kiocb *req,
45 const struct io_uring_sqe *sqe)
47 struct io_xattr *ix = io_kiocb_to_cmd(req);
48 const char __user *name;
51 if (unlikely(req->flags & REQ_F_FIXED_FILE))
55 ix->ctx.kvalue = NULL;
56 name = u64_to_user_ptr(READ_ONCE(sqe->addr));
57 ix->ctx.cvalue = u64_to_user_ptr(READ_ONCE(sqe->addr2));
58 ix->ctx.size = READ_ONCE(sqe->len);
59 ix->ctx.flags = READ_ONCE(sqe->xattr_flags);
64 ix->ctx.kname = kmalloc(sizeof(*ix->ctx.kname), GFP_KERNEL);
68 ret = strncpy_from_user(ix->ctx.kname->name, name,
69 sizeof(ix->ctx.kname->name));
70 if (!ret || ret == sizeof(ix->ctx.kname->name))
77 req->flags |= REQ_F_NEED_CLEANUP;
81 int io_fgetxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
83 return __io_getxattr_prep(req, sqe);
86 int io_getxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
88 struct io_xattr *ix = io_kiocb_to_cmd(req);
89 const char __user *path;
92 ret = __io_getxattr_prep(req, sqe);
96 path = u64_to_user_ptr(READ_ONCE(sqe->addr3));
98 ix->filename = getname_flags(path, LOOKUP_FOLLOW, NULL);
99 if (IS_ERR(ix->filename)) {
100 ret = PTR_ERR(ix->filename);
107 int io_fgetxattr(struct io_kiocb *req, unsigned int issue_flags)
109 struct io_xattr *ix = io_kiocb_to_cmd(req);
112 if (issue_flags & IO_URING_F_NONBLOCK)
115 ret = do_getxattr(mnt_user_ns(req->file->f_path.mnt),
116 req->file->f_path.dentry,
119 io_xattr_finish(req, ret);
123 int io_getxattr(struct io_kiocb *req, unsigned int issue_flags)
125 struct io_xattr *ix = io_kiocb_to_cmd(req);
126 unsigned int lookup_flags = LOOKUP_FOLLOW;
130 if (issue_flags & IO_URING_F_NONBLOCK)
134 ret = filename_lookup(AT_FDCWD, ix->filename, lookup_flags, &path, NULL);
136 ret = do_getxattr(mnt_user_ns(path.mnt),
141 if (retry_estale(ret, lookup_flags)) {
142 lookup_flags |= LOOKUP_REVAL;
147 io_xattr_finish(req, ret);
151 static int __io_setxattr_prep(struct io_kiocb *req,
152 const struct io_uring_sqe *sqe)
154 struct io_xattr *ix = io_kiocb_to_cmd(req);
155 const char __user *name;
158 if (unlikely(req->flags & REQ_F_FIXED_FILE))
162 name = u64_to_user_ptr(READ_ONCE(sqe->addr));
163 ix->ctx.cvalue = u64_to_user_ptr(READ_ONCE(sqe->addr2));
164 ix->ctx.kvalue = NULL;
165 ix->ctx.size = READ_ONCE(sqe->len);
166 ix->ctx.flags = READ_ONCE(sqe->xattr_flags);
168 ix->ctx.kname = kmalloc(sizeof(*ix->ctx.kname), GFP_KERNEL);
172 ret = setxattr_copy(name, &ix->ctx);
174 kfree(ix->ctx.kname);
178 req->flags |= REQ_F_NEED_CLEANUP;
182 int io_setxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
184 struct io_xattr *ix = io_kiocb_to_cmd(req);
185 const char __user *path;
188 ret = __io_setxattr_prep(req, sqe);
192 path = u64_to_user_ptr(READ_ONCE(sqe->addr3));
194 ix->filename = getname_flags(path, LOOKUP_FOLLOW, NULL);
195 if (IS_ERR(ix->filename)) {
196 ret = PTR_ERR(ix->filename);
203 int io_fsetxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
205 return __io_setxattr_prep(req, sqe);
208 static int __io_setxattr(struct io_kiocb *req, unsigned int issue_flags,
211 struct io_xattr *ix = io_kiocb_to_cmd(req);
214 ret = mnt_want_write(path->mnt);
216 ret = do_setxattr(mnt_user_ns(path->mnt), path->dentry, &ix->ctx);
217 mnt_drop_write(path->mnt);
223 int io_fsetxattr(struct io_kiocb *req, unsigned int issue_flags)
227 if (issue_flags & IO_URING_F_NONBLOCK)
230 ret = __io_setxattr(req, issue_flags, &req->file->f_path);
231 io_xattr_finish(req, ret);
235 int io_setxattr(struct io_kiocb *req, unsigned int issue_flags)
237 struct io_xattr *ix = io_kiocb_to_cmd(req);
238 unsigned int lookup_flags = LOOKUP_FOLLOW;
242 if (issue_flags & IO_URING_F_NONBLOCK)
246 ret = filename_lookup(AT_FDCWD, ix->filename, lookup_flags, &path, NULL);
248 ret = __io_setxattr(req, issue_flags, &path);
250 if (retry_estale(ret, lookup_flags)) {
251 lookup_flags |= LOOKUP_REVAL;
256 io_xattr_finish(req, ret);