splice.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "filetable.h"
  13. #include "io_uring.h"
  14. #include "splice.h"
  15. struct io_splice {
  16. struct file *file_out;
  17. loff_t off_out;
  18. loff_t off_in;
  19. u64 len;
  20. int splice_fd_in;
  21. unsigned int flags;
  22. struct io_rsrc_node *rsrc_node;
  23. };
  24. static int __io_splice_prep(struct io_kiocb *req,
  25. const struct io_uring_sqe *sqe)
  26. {
  27. struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
  28. unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
  29. sp->len = READ_ONCE(sqe->len);
  30. sp->flags = READ_ONCE(sqe->splice_flags);
  31. if (unlikely(sp->flags & ~valid_flags))
  32. return -EINVAL;
  33. sp->splice_fd_in = READ_ONCE(sqe->splice_fd_in);
  34. sp->rsrc_node = NULL;
  35. req->flags |= REQ_F_FORCE_ASYNC;
  36. return 0;
  37. }
  38. int io_tee_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  39. {
  40. if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
  41. return -EINVAL;
  42. return __io_splice_prep(req, sqe);
  43. }
  44. void io_splice_cleanup(struct io_kiocb *req)
  45. {
  46. struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
  47. if (sp->rsrc_node)
  48. io_put_rsrc_node(req->ctx, sp->rsrc_node);
  49. }
  50. static struct file *io_splice_get_file(struct io_kiocb *req,
  51. unsigned int issue_flags)
  52. {
  53. struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
  54. struct io_ring_ctx *ctx = req->ctx;
  55. struct io_rsrc_node *node;
  56. struct file *file = NULL;
  57. if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
  58. return io_file_get_normal(req, sp->splice_fd_in);
  59. io_ring_submit_lock(ctx, issue_flags);
  60. node = io_rsrc_node_lookup(&ctx->file_table.data, sp->splice_fd_in);
  61. if (node) {
  62. node->refs++;
  63. sp->rsrc_node = node;
  64. file = io_slot_file(node);
  65. req->flags |= REQ_F_NEED_CLEANUP;
  66. }
  67. io_ring_submit_unlock(ctx, issue_flags);
  68. return file;
  69. }
  70. int io_tee(struct io_kiocb *req, unsigned int issue_flags)
  71. {
  72. struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
  73. struct file *out = sp->file_out;
  74. unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
  75. struct file *in;
  76. ssize_t ret = 0;
  77. WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
  78. in = io_splice_get_file(req, issue_flags);
  79. if (!in) {
  80. ret = -EBADF;
  81. goto done;
  82. }
  83. if (sp->len)
  84. ret = do_tee(in, out, sp->len, flags);
  85. if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
  86. fput(in);
  87. done:
  88. if (ret != sp->len)
  89. req_set_fail(req);
  90. io_req_set_res(req, ret, 0);
  91. return IOU_COMPLETE;
  92. }
  93. int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  94. {
  95. struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
  96. sp->off_in = READ_ONCE(sqe->splice_off_in);
  97. sp->off_out = READ_ONCE(sqe->off);
  98. return __io_splice_prep(req, sqe);
  99. }
  100. int io_splice(struct io_kiocb *req, unsigned int issue_flags)
  101. {
  102. struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
  103. struct file *out = sp->file_out;
  104. unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
  105. loff_t *poff_in, *poff_out;
  106. struct file *in;
  107. ssize_t ret = 0;
  108. WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
  109. in = io_splice_get_file(req, issue_flags);
  110. if (!in) {
  111. ret = -EBADF;
  112. goto done;
  113. }
  114. poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
  115. poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
  116. if (sp->len)
  117. ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
  118. if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
  119. fput(in);
  120. done:
  121. if (ret != sp->len)
  122. req_set_fail(req);
  123. io_req_set_res(req, ret, 0);
  124. return IOU_COMPLETE;
  125. }