io_uring: split out splice related operations
[linux-block.git] / io_uring / splice.c
CommitLineData
531113bb
JA
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_types.h"
15#include "io_uring.h"
16#include "splice.h"
17
18struct io_splice {
19 struct file *file_out;
20 loff_t off_out;
21 loff_t off_in;
22 u64 len;
23 int splice_fd_in;
24 unsigned int flags;
25};
26
27static 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);
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 return 0;
39}
40
41int io_tee_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
42{
43 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
44 return -EINVAL;
45 return __io_splice_prep(req, sqe);
46}
47
48int io_tee(struct io_kiocb *req, unsigned int issue_flags)
49{
50 struct io_splice *sp = io_kiocb_to_cmd(req);
51 struct file *out = sp->file_out;
52 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
53 struct file *in;
54 long ret = 0;
55
56 if (issue_flags & IO_URING_F_NONBLOCK)
57 return -EAGAIN;
58
59 if (sp->flags & SPLICE_F_FD_IN_FIXED)
60 in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags);
61 else
62 in = io_file_get_normal(req, sp->splice_fd_in);
63 if (!in) {
64 ret = -EBADF;
65 goto done;
66 }
67
68 if (sp->len)
69 ret = do_tee(in, out, sp->len, flags);
70
71 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
72 io_put_file(in);
73done:
74 if (ret != sp->len)
75 req_set_fail(req);
76 io_req_set_res(req, ret, 0);
77 return IOU_OK;
78}
79
80int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
81{
82 struct io_splice *sp = io_kiocb_to_cmd(req);
83
84 sp->off_in = READ_ONCE(sqe->splice_off_in);
85 sp->off_out = READ_ONCE(sqe->off);
86 return __io_splice_prep(req, sqe);
87}
88
89int io_splice(struct io_kiocb *req, unsigned int issue_flags)
90{
91 struct io_splice *sp = io_kiocb_to_cmd(req);
92 struct file *out = sp->file_out;
93 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
94 loff_t *poff_in, *poff_out;
95 struct file *in;
96 long ret = 0;
97
98 if (issue_flags & IO_URING_F_NONBLOCK)
99 return -EAGAIN;
100
101 if (sp->flags & SPLICE_F_FD_IN_FIXED)
102 in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags);
103 else
104 in = io_file_get_normal(req, sp->splice_fd_in);
105 if (!in) {
106 ret = -EBADF;
107 goto done;
108 }
109
110 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
111 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
112
113 if (sp->len)
114 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
115
116 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
117 io_put_file(in);
118done:
119 if (ret != sp->len)
120 req_set_fail(req);
121 io_req_set_res(req, ret, 0);
122 return IOU_OK;
123}