1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
5 #include <linux/file.h>
7 #include <linux/slab.h>
8 #include <linux/namei.h>
9 #include <linux/io_uring.h>
10 #include <linux/splice.h>
12 #include <uapi/linux/io_uring.h>
18 struct file *file_out;
26 static int __io_splice_prep(struct io_kiocb *req,
27 const struct io_uring_sqe *sqe)
29 struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
30 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
32 sp->len = READ_ONCE(sqe->len);
33 sp->flags = READ_ONCE(sqe->splice_flags);
34 if (unlikely(sp->flags & ~valid_flags))
36 sp->splice_fd_in = READ_ONCE(sqe->splice_fd_in);
40 int io_tee_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
42 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
44 return __io_splice_prep(req, sqe);
47 int io_tee(struct io_kiocb *req, unsigned int issue_flags)
49 struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
50 struct file *out = sp->file_out;
51 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
55 if (issue_flags & IO_URING_F_NONBLOCK)
58 if (sp->flags & SPLICE_F_FD_IN_FIXED)
59 in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags);
61 in = io_file_get_normal(req, sp->splice_fd_in);
68 ret = do_tee(in, out, sp->len, flags);
70 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
75 io_req_set_res(req, ret, 0);
79 int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
81 struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
83 sp->off_in = READ_ONCE(sqe->splice_off_in);
84 sp->off_out = READ_ONCE(sqe->off);
85 return __io_splice_prep(req, sqe);
88 int io_splice(struct io_kiocb *req, unsigned int issue_flags)
90 struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
91 struct file *out = sp->file_out;
92 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
93 loff_t *poff_in, *poff_out;
97 if (issue_flags & IO_URING_F_NONBLOCK)
100 if (sp->flags & SPLICE_F_FD_IN_FIXED)
101 in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags);
103 in = io_file_get_normal(req, sp->splice_fd_in);
109 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
110 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
113 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
115 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
120 io_req_set_res(req, ret, 0);