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
, struct io_xattr
);
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
, struct io_xattr
);
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
;
78 req
->flags
|= REQ_F_FORCE_ASYNC
;
82 int io_fgetxattr_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
84 return __io_getxattr_prep(req
, sqe
);
87 int io_getxattr_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
89 struct io_xattr
*ix
= io_kiocb_to_cmd(req
, struct io_xattr
);
90 const char __user
*path
;
93 ret
= __io_getxattr_prep(req
, sqe
);
97 path
= u64_to_user_ptr(READ_ONCE(sqe
->addr3
));
99 ix
->filename
= getname_flags(path
, LOOKUP_FOLLOW
);
100 if (IS_ERR(ix
->filename
)) {
101 ret
= PTR_ERR(ix
->filename
);
108 int io_fgetxattr(struct io_kiocb
*req
, unsigned int issue_flags
)
110 struct io_xattr
*ix
= io_kiocb_to_cmd(req
, struct io_xattr
);
113 WARN_ON_ONCE(issue_flags
& IO_URING_F_NONBLOCK
);
115 ret
= do_getxattr(file_mnt_idmap(req
->file
),
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
, struct io_xattr
);
126 unsigned int lookup_flags
= LOOKUP_FOLLOW
;
130 WARN_ON_ONCE(issue_flags
& IO_URING_F_NONBLOCK
);
133 ret
= filename_lookup(AT_FDCWD
, ix
->filename
, lookup_flags
, &path
, NULL
);
135 ret
= do_getxattr(mnt_idmap(path
.mnt
), path
.dentry
, &ix
->ctx
);
138 if (retry_estale(ret
, lookup_flags
)) {
139 lookup_flags
|= LOOKUP_REVAL
;
144 io_xattr_finish(req
, ret
);
148 static int __io_setxattr_prep(struct io_kiocb
*req
,
149 const struct io_uring_sqe
*sqe
)
151 struct io_xattr
*ix
= io_kiocb_to_cmd(req
, struct io_xattr
);
152 const char __user
*name
;
155 if (unlikely(req
->flags
& REQ_F_FIXED_FILE
))
159 name
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
160 ix
->ctx
.cvalue
= u64_to_user_ptr(READ_ONCE(sqe
->addr2
));
161 ix
->ctx
.kvalue
= NULL
;
162 ix
->ctx
.size
= READ_ONCE(sqe
->len
);
163 ix
->ctx
.flags
= READ_ONCE(sqe
->xattr_flags
);
165 ix
->ctx
.kname
= kmalloc(sizeof(*ix
->ctx
.kname
), GFP_KERNEL
);
169 ret
= setxattr_copy(name
, &ix
->ctx
);
171 kfree(ix
->ctx
.kname
);
175 req
->flags
|= REQ_F_NEED_CLEANUP
;
176 req
->flags
|= REQ_F_FORCE_ASYNC
;
180 int io_setxattr_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
182 struct io_xattr
*ix
= io_kiocb_to_cmd(req
, struct io_xattr
);
183 const char __user
*path
;
186 ret
= __io_setxattr_prep(req
, sqe
);
190 path
= u64_to_user_ptr(READ_ONCE(sqe
->addr3
));
192 ix
->filename
= getname_flags(path
, LOOKUP_FOLLOW
);
193 if (IS_ERR(ix
->filename
)) {
194 ret
= PTR_ERR(ix
->filename
);
201 int io_fsetxattr_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
203 return __io_setxattr_prep(req
, sqe
);
206 static int __io_setxattr(struct io_kiocb
*req
, unsigned int issue_flags
,
207 const struct path
*path
)
209 struct io_xattr
*ix
= io_kiocb_to_cmd(req
, struct io_xattr
);
212 ret
= mnt_want_write(path
->mnt
);
214 ret
= do_setxattr(mnt_idmap(path
->mnt
), path
->dentry
, &ix
->ctx
);
215 mnt_drop_write(path
->mnt
);
221 int io_fsetxattr(struct io_kiocb
*req
, unsigned int issue_flags
)
225 WARN_ON_ONCE(issue_flags
& IO_URING_F_NONBLOCK
);
227 ret
= __io_setxattr(req
, issue_flags
, &req
->file
->f_path
);
228 io_xattr_finish(req
, ret
);
232 int io_setxattr(struct io_kiocb
*req
, unsigned int issue_flags
)
234 struct io_xattr
*ix
= io_kiocb_to_cmd(req
, struct io_xattr
);
235 unsigned int lookup_flags
= LOOKUP_FOLLOW
;
239 WARN_ON_ONCE(issue_flags
& IO_URING_F_NONBLOCK
);
242 ret
= filename_lookup(AT_FDCWD
, ix
->filename
, lookup_flags
, &path
, NULL
);
244 ret
= __io_setxattr(req
, issue_flags
, &path
);
246 if (retry_estale(ret
, lookup_flags
)) {
247 lookup_flags
|= LOOKUP_REVAL
;
252 io_xattr_finish(req
, ret
);