advise.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <uapi/linux/fadvise.h>
  11. #include <uapi/linux/io_uring.h>
  12. #include "io_uring.h"
  13. #include "advise.h"
  14. struct io_fadvise {
  15. struct file *file;
  16. u64 offset;
  17. u64 len;
  18. u32 advice;
  19. };
  20. struct io_madvise {
  21. struct file *file;
  22. u64 addr;
  23. u64 len;
  24. u32 advice;
  25. };
  26. int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  27. {
  28. #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
  29. struct io_madvise *ma = io_kiocb_to_cmd(req, struct io_madvise);
  30. if (sqe->buf_index || sqe->splice_fd_in)
  31. return -EINVAL;
  32. ma->addr = READ_ONCE(sqe->addr);
  33. ma->len = READ_ONCE(sqe->off);
  34. if (!ma->len)
  35. ma->len = READ_ONCE(sqe->len);
  36. ma->advice = READ_ONCE(sqe->fadvise_advice);
  37. req->flags |= REQ_F_FORCE_ASYNC;
  38. return 0;
  39. #else
  40. return -EOPNOTSUPP;
  41. #endif
  42. }
  43. int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
  44. {
  45. #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
  46. struct io_madvise *ma = io_kiocb_to_cmd(req, struct io_madvise);
  47. int ret;
  48. WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
  49. ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
  50. io_req_set_res(req, ret, 0);
  51. return IOU_COMPLETE;
  52. #else
  53. return -EOPNOTSUPP;
  54. #endif
  55. }
  56. static bool io_fadvise_force_async(struct io_fadvise *fa)
  57. {
  58. switch (fa->advice) {
  59. case POSIX_FADV_NORMAL:
  60. case POSIX_FADV_RANDOM:
  61. case POSIX_FADV_SEQUENTIAL:
  62. return false;
  63. default:
  64. return true;
  65. }
  66. }
  67. int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  68. {
  69. struct io_fadvise *fa = io_kiocb_to_cmd(req, struct io_fadvise);
  70. if (sqe->buf_index || sqe->splice_fd_in)
  71. return -EINVAL;
  72. fa->offset = READ_ONCE(sqe->off);
  73. fa->len = READ_ONCE(sqe->addr);
  74. if (!fa->len)
  75. fa->len = READ_ONCE(sqe->len);
  76. fa->advice = READ_ONCE(sqe->fadvise_advice);
  77. if (io_fadvise_force_async(fa))
  78. req->flags |= REQ_F_FORCE_ASYNC;
  79. return 0;
  80. }
  81. int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
  82. {
  83. struct io_fadvise *fa = io_kiocb_to_cmd(req, struct io_fadvise);
  84. int ret;
  85. WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK && io_fadvise_force_async(fa));
  86. ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
  87. if (ret < 0)
  88. req_set_fail(req);
  89. io_req_set_res(req, ret, 0);
  90. return IOU_COMPLETE;
  91. }