2022-05-25 03:19:47 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/namei.h>
|
|
|
|
#include <linux/io_uring.h>
|
|
|
|
#include <linux/splice.h>
|
|
|
|
|
|
|
|
#include <uapi/linux/io_uring.h>
|
|
|
|
|
|
|
|
#include "io_uring.h"
|
|
|
|
#include "splice.h"
|
|
|
|
|
|
|
|
struct io_splice {
|
|
|
|
struct file *file_out;
|
|
|
|
loff_t off_out;
|
|
|
|
loff_t off_in;
|
|
|
|
u64 len;
|
|
|
|
int splice_fd_in;
|
|
|
|
unsigned int flags;
|
2024-10-28 14:03:04 +00:00
|
|
|
struct io_rsrc_node *rsrc_node;
|
2022-05-25 03:19:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int __io_splice_prep(struct io_kiocb *req,
|
|
|
|
const struct io_uring_sqe *sqe)
|
|
|
|
{
|
2022-08-11 07:11:15 +00:00
|
|
|
struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
|
2022-05-25 03:19:47 +00:00
|
|
|
unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
|
|
|
|
|
|
|
|
sp->len = READ_ONCE(sqe->len);
|
|
|
|
sp->flags = READ_ONCE(sqe->splice_flags);
|
|
|
|
if (unlikely(sp->flags & ~valid_flags))
|
|
|
|
return -EINVAL;
|
|
|
|
sp->splice_fd_in = READ_ONCE(sqe->splice_fd_in);
|
2024-10-28 14:03:04 +00:00
|
|
|
sp->rsrc_node = NULL;
|
2023-01-27 13:52:25 +00:00
|
|
|
req->flags |= REQ_F_FORCE_ASYNC;
|
2022-05-25 03:19:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int io_tee_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
|
|
|
|
{
|
|
|
|
if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
|
|
|
|
return -EINVAL;
|
|
|
|
return __io_splice_prep(req, sqe);
|
|
|
|
}
|
|
|
|
|
2024-10-28 14:03:04 +00:00
|
|
|
void io_splice_cleanup(struct io_kiocb *req)
|
|
|
|
{
|
|
|
|
struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
|
|
|
|
|
|
|
|
io_put_rsrc_node(req->ctx, sp->rsrc_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct file *io_splice_get_file(struct io_kiocb *req,
|
|
|
|
unsigned int issue_flags)
|
|
|
|
{
|
|
|
|
struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
|
|
|
|
struct io_ring_ctx *ctx = req->ctx;
|
|
|
|
struct io_fixed_file *slot;
|
|
|
|
struct file *file = NULL;
|
|
|
|
|
|
|
|
if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
|
|
|
|
return io_file_get_normal(req, sp->splice_fd_in);
|
|
|
|
|
|
|
|
io_ring_submit_lock(ctx, issue_flags);
|
|
|
|
if (unlikely(sp->splice_fd_in >= ctx->nr_user_files))
|
|
|
|
goto out;
|
|
|
|
sp->splice_fd_in = array_index_nospec(sp->splice_fd_in, ctx->nr_user_files);
|
|
|
|
slot = &ctx->file_table.files[sp->splice_fd_in];
|
|
|
|
if (!req->rsrc_node)
|
|
|
|
__io_req_set_rsrc_node(req, ctx);
|
|
|
|
file = io_slot_file(slot);
|
|
|
|
req->flags |= REQ_F_NEED_CLEANUP;
|
|
|
|
out:
|
|
|
|
io_ring_submit_unlock(ctx, issue_flags);
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2022-05-25 03:19:47 +00:00
|
|
|
int io_tee(struct io_kiocb *req, unsigned int issue_flags)
|
|
|
|
{
|
2022-08-11 07:11:15 +00:00
|
|
|
struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
|
2022-05-25 03:19:47 +00:00
|
|
|
struct file *out = sp->file_out;
|
|
|
|
unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
|
|
|
|
struct file *in;
|
2023-12-12 09:44:36 +00:00
|
|
|
ssize_t ret = 0;
|
2022-05-25 03:19:47 +00:00
|
|
|
|
2023-01-27 13:52:25 +00:00
|
|
|
WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
|
2022-05-25 03:19:47 +00:00
|
|
|
|
2024-10-28 14:03:04 +00:00
|
|
|
in = io_splice_get_file(req, issue_flags);
|
2022-05-25 03:19:47 +00:00
|
|
|
if (!in) {
|
|
|
|
ret = -EBADF;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sp->len)
|
|
|
|
ret = do_tee(in, out, sp->len, flags);
|
|
|
|
|
|
|
|
if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
|
2023-07-07 17:11:58 +00:00
|
|
|
fput(in);
|
2022-05-25 03:19:47 +00:00
|
|
|
done:
|
|
|
|
if (ret != sp->len)
|
|
|
|
req_set_fail(req);
|
|
|
|
io_req_set_res(req, ret, 0);
|
|
|
|
return IOU_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
|
|
|
|
{
|
2022-08-11 07:11:15 +00:00
|
|
|
struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
|
2022-05-25 03:19:47 +00:00
|
|
|
|
|
|
|
sp->off_in = READ_ONCE(sqe->splice_off_in);
|
|
|
|
sp->off_out = READ_ONCE(sqe->off);
|
|
|
|
return __io_splice_prep(req, sqe);
|
|
|
|
}
|
|
|
|
|
|
|
|
int io_splice(struct io_kiocb *req, unsigned int issue_flags)
|
|
|
|
{
|
2022-08-11 07:11:15 +00:00
|
|
|
struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
|
2022-05-25 03:19:47 +00:00
|
|
|
struct file *out = sp->file_out;
|
|
|
|
unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
|
|
|
|
loff_t *poff_in, *poff_out;
|
|
|
|
struct file *in;
|
2023-12-12 09:44:36 +00:00
|
|
|
ssize_t ret = 0;
|
2022-05-25 03:19:47 +00:00
|
|
|
|
2023-01-27 13:52:25 +00:00
|
|
|
WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
|
2022-05-25 03:19:47 +00:00
|
|
|
|
2024-10-28 14:03:04 +00:00
|
|
|
in = io_splice_get_file(req, issue_flags);
|
2022-05-25 03:19:47 +00:00
|
|
|
if (!in) {
|
|
|
|
ret = -EBADF;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
|
|
|
|
poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
|
|
|
|
|
|
|
|
if (sp->len)
|
|
|
|
ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
|
|
|
|
|
|
|
|
if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
|
2023-07-07 17:11:58 +00:00
|
|
|
fput(in);
|
2022-05-25 03:19:47 +00:00
|
|
|
done:
|
|
|
|
if (ret != sp->len)
|
|
|
|
req_set_fail(req);
|
|
|
|
io_req_set_res(req, ret, 0);
|
|
|
|
return IOU_OK;
|
|
|
|
}
|