splice.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/splice.h>
  11. #include <uapi/linux/io_uring.h>
  12. #include "io_uring.h"
  13. #include "splice.h"
  14. struct io_splice {
  15. struct file *file_out;
  16. loff_t off_out;
  17. loff_t off_in;
  18. u64 len;
  19. int splice_fd_in;
  20. unsigned int flags;
  21. };
  22. static int __io_splice_prep(struct io_kiocb *req,
  23. const struct io_uring_sqe *sqe)
  24. {
  25. struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
  26. unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
  27. sp->len = READ_ONCE(sqe->len);
  28. sp->flags = READ_ONCE(sqe->splice_flags);
  29. if (unlikely(sp->flags & ~valid_flags))
  30. return -EINVAL;
  31. sp->splice_fd_in = READ_ONCE(sqe->splice_fd_in);
  32. req->flags |= REQ_F_FORCE_ASYNC;
  33. return 0;
  34. }
  35. int io_tee_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  36. {
  37. if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
  38. return -EINVAL;
  39. return __io_splice_prep(req, sqe);
  40. }
  41. int io_tee(struct io_kiocb *req, unsigned int issue_flags)
  42. {
  43. struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
  44. struct file *out = sp->file_out;
  45. unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
  46. struct file *in;
  47. ssize_t ret = 0;
  48. WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
  49. if (sp->flags & SPLICE_F_FD_IN_FIXED)
  50. in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags);
  51. else
  52. in = io_file_get_normal(req, sp->splice_fd_in);
  53. if (!in) {
  54. ret = -EBADF;
  55. goto done;
  56. }
  57. if (sp->len)
  58. ret = do_tee(in, out, sp->len, flags);
  59. if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
  60. fput(in);
  61. done:
  62. if (ret != sp->len)
  63. req_set_fail(req);
  64. io_req_set_res(req, ret, 0);
  65. return IOU_OK;
  66. }
  67. int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  68. {
  69. struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
  70. sp->off_in = READ_ONCE(sqe->splice_off_in);
  71. sp->off_out = READ_ONCE(sqe->off);
  72. return __io_splice_prep(req, sqe);
  73. }
  74. int io_splice(struct io_kiocb *req, unsigned int issue_flags)
  75. {
  76. struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
  77. struct file *out = sp->file_out;
  78. unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
  79. loff_t *poff_in, *poff_out;
  80. struct file *in;
  81. ssize_t ret = 0;
  82. WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
  83. if (sp->flags & SPLICE_F_FD_IN_FIXED)
  84. in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags);
  85. else
  86. in = io_file_get_normal(req, sp->splice_fd_in);
  87. if (!in) {
  88. ret = -EBADF;
  89. goto done;
  90. }
  91. poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
  92. poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
  93. if (sp->len)
  94. ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
  95. if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
  96. fput(in);
  97. done:
  98. if (ret != sp->len)
  99. req_set_fail(req);
  100. io_req_set_res(req, ret, 0);
  101. return IOU_OK;
  102. }