xattr.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #include <linux/xattr.h>
  11. #include <uapi/linux/io_uring.h>
  12. #include "../fs/internal.h"
  13. #include "io_uring.h"
  14. #include "xattr.h"
  15. struct io_xattr {
  16. struct file *file;
  17. struct xattr_ctx ctx;
  18. struct filename *filename;
  19. };
  20. void io_xattr_cleanup(struct io_kiocb *req)
  21. {
  22. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  23. if (ix->filename)
  24. putname(ix->filename);
  25. kfree(ix->ctx.kname);
  26. kvfree(ix->ctx.kvalue);
  27. }
  28. static void io_xattr_finish(struct io_kiocb *req, int ret)
  29. {
  30. req->flags &= ~REQ_F_NEED_CLEANUP;
  31. io_xattr_cleanup(req);
  32. io_req_set_res(req, ret, 0);
  33. }
  34. static int __io_getxattr_prep(struct io_kiocb *req,
  35. const struct io_uring_sqe *sqe)
  36. {
  37. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  38. const char __user *name;
  39. int ret;
  40. if (unlikely(req->flags & REQ_F_FIXED_FILE))
  41. return -EBADF;
  42. ix->filename = NULL;
  43. ix->ctx.kvalue = NULL;
  44. name = u64_to_user_ptr(READ_ONCE(sqe->addr));
  45. ix->ctx.cvalue = u64_to_user_ptr(READ_ONCE(sqe->addr2));
  46. ix->ctx.size = READ_ONCE(sqe->len);
  47. ix->ctx.flags = READ_ONCE(sqe->xattr_flags);
  48. if (ix->ctx.flags)
  49. return -EINVAL;
  50. ix->ctx.kname = kmalloc(sizeof(*ix->ctx.kname), GFP_KERNEL);
  51. if (!ix->ctx.kname)
  52. return -ENOMEM;
  53. ret = strncpy_from_user(ix->ctx.kname->name, name,
  54. sizeof(ix->ctx.kname->name));
  55. if (!ret || ret == sizeof(ix->ctx.kname->name))
  56. ret = -ERANGE;
  57. if (ret < 0) {
  58. kfree(ix->ctx.kname);
  59. return ret;
  60. }
  61. req->flags |= REQ_F_NEED_CLEANUP;
  62. req->flags |= REQ_F_FORCE_ASYNC;
  63. return 0;
  64. }
  65. int io_fgetxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  66. {
  67. return __io_getxattr_prep(req, sqe);
  68. }
  69. int io_getxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  70. {
  71. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  72. const char __user *path;
  73. int ret;
  74. ret = __io_getxattr_prep(req, sqe);
  75. if (ret)
  76. return ret;
  77. path = u64_to_user_ptr(READ_ONCE(sqe->addr3));
  78. ix->filename = getname_flags(path, LOOKUP_FOLLOW);
  79. if (IS_ERR(ix->filename)) {
  80. ret = PTR_ERR(ix->filename);
  81. ix->filename = NULL;
  82. }
  83. return ret;
  84. }
  85. int io_fgetxattr(struct io_kiocb *req, unsigned int issue_flags)
  86. {
  87. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  88. int ret;
  89. WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
  90. ret = do_getxattr(file_mnt_idmap(req->file),
  91. req->file->f_path.dentry,
  92. &ix->ctx);
  93. io_xattr_finish(req, ret);
  94. return IOU_OK;
  95. }
  96. int io_getxattr(struct io_kiocb *req, unsigned int issue_flags)
  97. {
  98. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  99. unsigned int lookup_flags = LOOKUP_FOLLOW;
  100. struct path path;
  101. int ret;
  102. WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
  103. retry:
  104. ret = filename_lookup(AT_FDCWD, ix->filename, lookup_flags, &path, NULL);
  105. if (!ret) {
  106. ret = do_getxattr(mnt_idmap(path.mnt), path.dentry, &ix->ctx);
  107. path_put(&path);
  108. if (retry_estale(ret, lookup_flags)) {
  109. lookup_flags |= LOOKUP_REVAL;
  110. goto retry;
  111. }
  112. }
  113. io_xattr_finish(req, ret);
  114. return IOU_OK;
  115. }
  116. static int __io_setxattr_prep(struct io_kiocb *req,
  117. const struct io_uring_sqe *sqe)
  118. {
  119. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  120. const char __user *name;
  121. int ret;
  122. if (unlikely(req->flags & REQ_F_FIXED_FILE))
  123. return -EBADF;
  124. ix->filename = NULL;
  125. name = u64_to_user_ptr(READ_ONCE(sqe->addr));
  126. ix->ctx.cvalue = u64_to_user_ptr(READ_ONCE(sqe->addr2));
  127. ix->ctx.kvalue = NULL;
  128. ix->ctx.size = READ_ONCE(sqe->len);
  129. ix->ctx.flags = READ_ONCE(sqe->xattr_flags);
  130. ix->ctx.kname = kmalloc(sizeof(*ix->ctx.kname), GFP_KERNEL);
  131. if (!ix->ctx.kname)
  132. return -ENOMEM;
  133. ret = setxattr_copy(name, &ix->ctx);
  134. if (ret) {
  135. kfree(ix->ctx.kname);
  136. return ret;
  137. }
  138. req->flags |= REQ_F_NEED_CLEANUP;
  139. req->flags |= REQ_F_FORCE_ASYNC;
  140. return 0;
  141. }
  142. int io_setxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  143. {
  144. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  145. const char __user *path;
  146. int ret;
  147. ret = __io_setxattr_prep(req, sqe);
  148. if (ret)
  149. return ret;
  150. path = u64_to_user_ptr(READ_ONCE(sqe->addr3));
  151. ix->filename = getname_flags(path, LOOKUP_FOLLOW);
  152. if (IS_ERR(ix->filename)) {
  153. ret = PTR_ERR(ix->filename);
  154. ix->filename = NULL;
  155. }
  156. return ret;
  157. }
  158. int io_fsetxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  159. {
  160. return __io_setxattr_prep(req, sqe);
  161. }
  162. static int __io_setxattr(struct io_kiocb *req, unsigned int issue_flags,
  163. const struct path *path)
  164. {
  165. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  166. int ret;
  167. ret = mnt_want_write(path->mnt);
  168. if (!ret) {
  169. ret = do_setxattr(mnt_idmap(path->mnt), path->dentry, &ix->ctx);
  170. mnt_drop_write(path->mnt);
  171. }
  172. return ret;
  173. }
  174. int io_fsetxattr(struct io_kiocb *req, unsigned int issue_flags)
  175. {
  176. int ret;
  177. WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
  178. ret = __io_setxattr(req, issue_flags, &req->file->f_path);
  179. io_xattr_finish(req, ret);
  180. return IOU_OK;
  181. }
  182. int io_setxattr(struct io_kiocb *req, unsigned int issue_flags)
  183. {
  184. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  185. unsigned int lookup_flags = LOOKUP_FOLLOW;
  186. struct path path;
  187. int ret;
  188. WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
  189. retry:
  190. ret = filename_lookup(AT_FDCWD, ix->filename, lookup_flags, &path, NULL);
  191. if (!ret) {
  192. ret = __io_setxattr(req, issue_flags, &path);
  193. path_put(&path);
  194. if (retry_estale(ret, lookup_flags)) {
  195. lookup_flags |= LOOKUP_REVAL;
  196. goto retry;
  197. }
  198. }
  199. io_xattr_finish(req, ret);
  200. return IOU_OK;
  201. }