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