xattr.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 kernel_xattr_ctx ctx;
  18. struct delayed_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. dismiss_delayed_filename(&ix->filename);
  24. kfree(ix->ctx.kname);
  25. kvfree(ix->ctx.kvalue);
  26. }
  27. static void io_xattr_finish(struct io_kiocb *req, int ret)
  28. {
  29. req->flags &= ~REQ_F_NEED_CLEANUP;
  30. io_xattr_cleanup(req);
  31. io_req_set_res(req, ret, 0);
  32. }
  33. static int __io_getxattr_prep(struct io_kiocb *req,
  34. const struct io_uring_sqe *sqe)
  35. {
  36. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  37. const char __user *name;
  38. int ret;
  39. INIT_DELAYED_FILENAME(&ix->filename);
  40. ix->ctx.kvalue = NULL;
  41. name = u64_to_user_ptr(READ_ONCE(sqe->addr));
  42. ix->ctx.value = u64_to_user_ptr(READ_ONCE(sqe->addr2));
  43. ix->ctx.size = READ_ONCE(sqe->len);
  44. ix->ctx.flags = READ_ONCE(sqe->xattr_flags);
  45. if (ix->ctx.flags)
  46. return -EINVAL;
  47. ix->ctx.kname = kmalloc_obj(*ix->ctx.kname);
  48. if (!ix->ctx.kname)
  49. return -ENOMEM;
  50. ret = import_xattr_name(ix->ctx.kname, name);
  51. if (ret) {
  52. kfree(ix->ctx.kname);
  53. return ret;
  54. }
  55. req->flags |= REQ_F_NEED_CLEANUP;
  56. req->flags |= REQ_F_FORCE_ASYNC;
  57. return 0;
  58. }
  59. int io_fgetxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  60. {
  61. return __io_getxattr_prep(req, sqe);
  62. }
  63. int io_getxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  64. {
  65. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  66. const char __user *path;
  67. int ret;
  68. if (unlikely(req->flags & REQ_F_FIXED_FILE))
  69. return -EBADF;
  70. ret = __io_getxattr_prep(req, sqe);
  71. if (ret)
  72. return ret;
  73. path = u64_to_user_ptr(READ_ONCE(sqe->addr3));
  74. return delayed_getname(&ix->filename, path);
  75. }
  76. int io_fgetxattr(struct io_kiocb *req, unsigned int issue_flags)
  77. {
  78. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  79. int ret;
  80. WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
  81. ret = file_getxattr(req->file, &ix->ctx);
  82. io_xattr_finish(req, ret);
  83. return IOU_COMPLETE;
  84. }
  85. int io_getxattr(struct io_kiocb *req, unsigned int issue_flags)
  86. {
  87. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  88. CLASS(filename_complete_delayed, name)(&ix->filename);
  89. int ret;
  90. WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
  91. ret = filename_getxattr(AT_FDCWD, name, LOOKUP_FOLLOW, &ix->ctx);
  92. io_xattr_finish(req, ret);
  93. return IOU_COMPLETE;
  94. }
  95. static int __io_setxattr_prep(struct io_kiocb *req,
  96. const struct io_uring_sqe *sqe)
  97. {
  98. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  99. const char __user *name;
  100. int ret;
  101. INIT_DELAYED_FILENAME(&ix->filename);
  102. name = u64_to_user_ptr(READ_ONCE(sqe->addr));
  103. ix->ctx.cvalue = u64_to_user_ptr(READ_ONCE(sqe->addr2));
  104. ix->ctx.kvalue = NULL;
  105. ix->ctx.size = READ_ONCE(sqe->len);
  106. ix->ctx.flags = READ_ONCE(sqe->xattr_flags);
  107. ix->ctx.kname = kmalloc_obj(*ix->ctx.kname);
  108. if (!ix->ctx.kname)
  109. return -ENOMEM;
  110. ret = setxattr_copy(name, &ix->ctx);
  111. if (ret) {
  112. kfree(ix->ctx.kname);
  113. return ret;
  114. }
  115. req->flags |= REQ_F_NEED_CLEANUP;
  116. req->flags |= REQ_F_FORCE_ASYNC;
  117. return 0;
  118. }
  119. int io_setxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  120. {
  121. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  122. const char __user *path;
  123. int ret;
  124. if (unlikely(req->flags & REQ_F_FIXED_FILE))
  125. return -EBADF;
  126. ret = __io_setxattr_prep(req, sqe);
  127. if (ret)
  128. return ret;
  129. path = u64_to_user_ptr(READ_ONCE(sqe->addr3));
  130. return delayed_getname(&ix->filename, path);
  131. }
  132. int io_fsetxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  133. {
  134. return __io_setxattr_prep(req, sqe);
  135. }
  136. int io_fsetxattr(struct io_kiocb *req, unsigned int issue_flags)
  137. {
  138. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  139. int ret;
  140. WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
  141. ret = file_setxattr(req->file, &ix->ctx);
  142. io_xattr_finish(req, ret);
  143. return IOU_COMPLETE;
  144. }
  145. int io_setxattr(struct io_kiocb *req, unsigned int issue_flags)
  146. {
  147. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  148. CLASS(filename_complete_delayed, name)(&ix->filename);
  149. int ret;
  150. WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
  151. ret = filename_setxattr(AT_FDCWD, name, LOOKUP_FOLLOW, &ix->ctx);
  152. io_xattr_finish(req, ret);
  153. return IOU_COMPLETE;
  154. }