From patchwork Sat Feb 15 22:05:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 11384169 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8217714E3 for ; Sat, 15 Feb 2020 22:06:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 620672073A for ; Sat, 15 Feb 2020 22:06:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RJQ7YvF+" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727785AbgBOWGl (ORCPT ); Sat, 15 Feb 2020 17:06:41 -0500 Received: from mail-wm1-f68.google.com ([209.85.128.68]:50778 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726340AbgBOWGk (ORCPT ); Sat, 15 Feb 2020 17:06:40 -0500 Received: by mail-wm1-f68.google.com with SMTP id a5so13541446wmb.0; Sat, 15 Feb 2020 14:06:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=foKquxBAIMXjAR3tdf5BNsaZnzetsrzrXlbRi9LRBb4=; b=RJQ7YvF+CbdGHdO2L5RhrHlqH03JTtwPhIA9jRCzKopXjNJF94Cmc5ISajpdCL66T5 tVPj61kBmx3uFfxX9NTnk67CoutWZiTEQ/44Kq4Ci/hMnewIpCxzBTrjdCNLYHKkItB6 d5DR5urmh2BclVAvB0xhQmoV0aO+yJ61QR5wOo9ZIFWBQi52ixSF1hqJwriEQbOSTVA7 HA8oc6CCwSI286/9zI/p3kzkX3PzvBdzZ7CNSAaLNQzBIlPW2M0N87YjXo8+jys4G4Bc kwLsrI8RI6Yt1SX8P+qN9N+xbVEWs4Hvytfh8AAlgD/yO/IF2AguLDa/4qR4JifbybV8 bZ8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=foKquxBAIMXjAR3tdf5BNsaZnzetsrzrXlbRi9LRBb4=; b=UlDqrcTI44XHS4i7v9wG1y52yl90fIikrq8wlY2Rr/SG3Oj4fuoEDESSHDoF3TBulM 9pJAc5jIBrVICiYqwu0WBouH3lHRS72Be0VP7u0T01EGS9p/P/YWMmg+omsrtZkpAYRR wQ4PZCgHaoIT8MnmjCYTlMQmSLJ8JjRtFBL1mp3j3dCkByEX4dfD7Oepz0kBar7lYuaj gzx3Onm3rFHyba+8AI4fF5IHERT5Al5g9fzf30Cfqh33e9Kbs6A1tkvFedvMGZo8Hd/K R9i0QBIW88LHEJsYYKwB8b84Frw98EDON3vdQxptIhdBX+5mCctZgCeFY1/XZYsfSxrp AQVw== X-Gm-Message-State: APjAAAWnJri80cLQMYp7byLlrrOAodHb8/1rK7KKrrffFrVu7KDn5eyP is/Ey0RqP8QqrkWNThDqZsg= X-Google-Smtp-Source: APXvYqy+aZtSryCb2nmDg+j1J5RRUQ49wyVwSv46fP0J2qz81S110OlBHFDxVovPS4Jr4zeMth5jNA== X-Received: by 2002:a05:600c:2207:: with SMTP id z7mr12496803wml.138.1581804398587; Sat, 15 Feb 2020 14:06:38 -0800 (PST) Received: from localhost.localdomain ([109.126.146.5]) by smtp.gmail.com with ESMTPSA id v15sm13281923wrf.7.2020.02.15.14.06.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Feb 2020 14:06:38 -0800 (PST) From: Pavel Begunkov To: Jens Axboe , Alexander Viro , io-uring@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/3] splice: make do_splice public Date: Sun, 16 Feb 2020 01:05:39 +0300 Message-Id: X-Mailer: git-send-email 2.24.0 In-Reply-To: References: MIME-Version: 1.0 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Make do_splice(), so other kernel parts can reuse it Signed-off-by: Pavel Begunkov --- fs/splice.c | 6 +++--- include/linux/splice.h | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/fs/splice.c b/fs/splice.c index 3009652a41c8..6a6f30432688 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -1109,9 +1109,9 @@ static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, /* * Determine where to splice to/from. */ -static long do_splice(struct file *in, loff_t __user *off_in, - struct file *out, loff_t __user *off_out, - size_t len, unsigned int flags) +long do_splice(struct file *in, loff_t __user *off_in, + struct file *out, loff_t __user *off_out, + size_t len, unsigned int flags) { struct pipe_inode_info *ipipe; struct pipe_inode_info *opipe; diff --git a/include/linux/splice.h b/include/linux/splice.h index 74b4911ac16d..ebbbfea48aa0 100644 --- a/include/linux/splice.h +++ b/include/linux/splice.h @@ -78,6 +78,9 @@ extern ssize_t add_to_pipe(struct pipe_inode_info *, struct pipe_buffer *); extern ssize_t splice_direct_to_actor(struct file *, struct splice_desc *, splice_direct_actor *); +extern long do_splice(struct file *in, loff_t __user *off_in, + struct file *out, loff_t __user *off_out, + size_t len, unsigned int flags); /* * for dynamic pipe sizing From patchwork Sat Feb 15 22:05:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 11384167 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A588B1580 for ; Sat, 15 Feb 2020 22:06:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 868F02187F for ; Sat, 15 Feb 2020 22:06:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kPSdhig2" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727888AbgBOWGo (ORCPT ); Sat, 15 Feb 2020 17:06:44 -0500 Received: from mail-wr1-f68.google.com ([209.85.221.68]:34797 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727804AbgBOWGn (ORCPT ); Sat, 15 Feb 2020 17:06:43 -0500 Received: by mail-wr1-f68.google.com with SMTP id n10so13228148wrm.1; Sat, 15 Feb 2020 14:06:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=fc1GAnpw884550NPulN3RKMom4ysjGxVOe69KHdCWnw=; b=kPSdhig2gFchvsr1r3rAqyKoR0AGehdQtwyyYqAv6qBhxcOg4P2k/lzlfhK01lLjMv zfLvzVRwtNUWcVWx/BOyuAdZBhrK1jOgywqued2MA3ebpS5rAfk6G4fDLT65kqQMs+Pz poKWz+EFUStanQRdXaYcAv1eULA8IsJxrO8hXIJv9RR17hKspyJKUdLdlxbHFMI4bxwz MA6MSB43VcCno2tY5P4tK8D32QQydR5yWN3OzUrPghKuJl6+vIdHMaXA4pvRj0DUFJco PJolMLWgN+2rjMNF7vuYeo08KddxwuboID8w+s7YKyzEXUOtz+P6cfYa+G10jd0gKvZw AdeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fc1GAnpw884550NPulN3RKMom4ysjGxVOe69KHdCWnw=; b=ThVs1DcRbZBSy8fu7wLhYYb0PI2bDdAcLW539B1Xj6jiWU2xDPlZetKCN5Qpre/Xsr cxOc/1uzhDSTm3mqLS8DjesukH4E8NUWAm84eTCbk+qLi4rrd9+X0BNn+yQ4BZxHtRTZ EQ4pgqqA2sUcqoCx5zJ6hDaOtnCSbIJaLbyu/FK9d3/Qv9BgppevSewobbx3rq1MaG25 5a/LTM9G+1Lqm358XgYpww95QoAvwND3XDzCSEdcxSD/GaJQKG7tZxrmKtYYO/H42Pl4 tLq/sFApm+HDA4iEJPnBkdzf81TWx8/Wmcs4IzCmlxH7K0QA+Y74DfUp2pvTURIgrbw+ HEbQ== X-Gm-Message-State: APjAAAXa2aj7RW0nSGYvJBjMNl+5c3M7X0bKaWHsIKk60yqIpeXJQTwk JS42mKz1PsPeJgsP/c4zFLA= X-Google-Smtp-Source: APXvYqySxtg4e/nOZ1ShjkArnqXh4BaopPO8rXz6BEiVXZXuHBZi92RXrMySWTnqbMJmNRzNd8pMXg== X-Received: by 2002:a5d:534b:: with SMTP id t11mr11675488wrv.120.1581804401041; Sat, 15 Feb 2020 14:06:41 -0800 (PST) Received: from localhost.localdomain ([109.126.146.5]) by smtp.gmail.com with ESMTPSA id v15sm13281923wrf.7.2020.02.15.14.06.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Feb 2020 14:06:40 -0800 (PST) From: Pavel Begunkov To: Jens Axboe , Alexander Viro , io-uring@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/3] io_uring: add interface for getting files Date: Sun, 16 Feb 2020 01:05:40 +0300 Message-Id: X-Mailer: git-send-email 2.24.0 In-Reply-To: References: MIME-Version: 1.0 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Preparation without functional changes. Adds io_get_file(), that allows to grab files not only into req->file. Signed-off-by: Pavel Begunkov --- fs/io_uring.c | 66 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 5ff7c602ad4d..389db6f5568b 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -1255,6 +1255,15 @@ static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, return NULL; } +static inline void io_put_file(struct io_ring_ctx *ctx, struct file *file, + bool fixed) +{ + if (fixed) + percpu_ref_put(&ctx->file_data->refs); + else + fput(file); +} + static void __io_req_do_free(struct io_kiocb *req) { if (likely(!io_is_fallback_req(req))) @@ -1268,12 +1277,8 @@ static void __io_req_aux_free(struct io_kiocb *req) struct io_ring_ctx *ctx = req->ctx; kfree(req->io); - if (req->file) { - if (req->flags & REQ_F_FIXED_FILE) - percpu_ref_put(&ctx->file_data->refs); - else - fput(req->file); - } + if (req->file) + io_put_file(ctx, req->file, (req->flags & REQ_F_FIXED_FILE)); io_req_work_drop_env(req); } @@ -4573,41 +4578,52 @@ static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, return table->files[index & IORING_FILE_TABLE_MASK];; } -static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, - const struct io_uring_sqe *sqe) +static int io_get_file(struct io_submit_state *state, struct io_ring_ctx *ctx, + int fd, struct file **out_file, bool fixed) { - struct io_ring_ctx *ctx = req->ctx; - unsigned flags; - int fd; - - flags = READ_ONCE(sqe->flags); - fd = READ_ONCE(sqe->fd); - - if (!io_req_needs_file(req, fd)) - return 0; + struct file *file; - if (flags & IOSQE_FIXED_FILE) { + if (fixed) { if (unlikely(!ctx->file_data || (unsigned) fd >= ctx->nr_user_files)) return -EBADF; fd = array_index_nospec(fd, ctx->nr_user_files); - req->file = io_file_from_index(ctx, fd); - if (!req->file) + file = io_file_from_index(ctx, fd); + if (!file) return -EBADF; - req->flags |= REQ_F_FIXED_FILE; percpu_ref_get(&ctx->file_data->refs); } else { - if (req->needs_fixed_file) - return -EBADF; trace_io_uring_file_get(ctx, fd); - req->file = io_file_get(state, fd); - if (unlikely(!req->file)) + file = io_file_get(state, fd); + if (unlikely(!file)) return -EBADF; } + *out_file = file; return 0; } +static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, + const struct io_uring_sqe *sqe) +{ + struct io_ring_ctx *ctx = req->ctx; + unsigned flags; + int fd; + bool fixed; + + flags = READ_ONCE(sqe->flags); + fd = READ_ONCE(sqe->fd); + + if (!io_req_needs_file(req, fd)) + return 0; + + fixed = (flags & IOSQE_FIXED_FILE); + if (unlikely(!fixed && req->needs_fixed_file)) + return -EBADF; + + return io_get_file(state, ctx, fd, &req->file, fixed); +} + static int io_grab_files(struct io_kiocb *req) { int ret = -EBADF; From patchwork Sat Feb 15 22:05:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 11384165 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 06A4A1580 for ; Sat, 15 Feb 2020 22:06:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D0D9624649 for ; Sat, 15 Feb 2020 22:06:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="tGeMzW26" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727936AbgBOWGr (ORCPT ); Sat, 15 Feb 2020 17:06:47 -0500 Received: from mail-wr1-f67.google.com ([209.85.221.67]:40573 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726340AbgBOWGp (ORCPT ); Sat, 15 Feb 2020 17:06:45 -0500 Received: by mail-wr1-f67.google.com with SMTP id t3so15174132wru.7; Sat, 15 Feb 2020 14:06:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=bFoK/C15kDMpwiLm73hwFU4WVfAaaGpsowESXi5m1J8=; b=tGeMzW26uhZwXyYYBBcUp0k0w7sAsRoXEWKBDUIddpPHCKjo7Ts1SFewCKbTfZdcmp kaZg3YBPARwVyS0ebFBB218wDJ1htL+02kA7IkDl97Z6byjY6AwNYun6VHzbHRr3PUxA lBpjCf5cITL5o1bjglFw+BEL+Xx/wu1jphxGs6S6m8+RjVXPb3SVlVX61xn6WCd4vv+P tgP3F5leSd+661FrkRAfimZXwHaM835WgQNk39s4gRfKmyaqlreB0Y5dcKUJfaRm7xMk d9zZfJnOpKGw/uSqszh0sgsCSFEURmTXKBsuuXG7SYjrQKOD2+aqT8V3bo37Zo96R4UN MRBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bFoK/C15kDMpwiLm73hwFU4WVfAaaGpsowESXi5m1J8=; b=h1B+f1zFsKIff3HL7acYNPctWl3ytsSJnFwa/WzkcbjpTps+RVMzRvyNJ77viTSKm0 HosR9tTnMYzCVSoD6C3Jq6/r0UlVdm2h9aMA5u+9VAcThjQ+mkQeBQb+XrIe1DfCJ9rm rsnB4Y3Fh43PLO7+jgI7/Lhy3ge8L//DPV5WM5J0fcpIRFHjedbDv4NQTM0N9/dITodN IJ44bXzNHFDIjdTNVd6RM5Ze+MYO8HGBOV78D8eNxtdQvU9PuywSoKGjoPUbmT3Fyayg fjnQ3YOmXT0YH6SC3RVOXmDBYWOg3jCgbCifwhjn0lIw/SX6kA8RVei+4IXR23hQqnPq 5RXw== X-Gm-Message-State: APjAAAUvkKzVb0Bp5IpIs5SxfpFp4rMqAF8eCtMiuRVpdooovMi+RFvO ffKGXRJ/QVnGwVdBnCexkXk= X-Google-Smtp-Source: APXvYqxOTos5Hh/wlql/Rw7CRq9D0ze8S8dfwPn/mIttiSBi6SVQnlUVss8j2+rfjDuyDVh+jW74kg== X-Received: by 2002:a05:6000:118d:: with SMTP id g13mr11216448wrx.141.1581804402955; Sat, 15 Feb 2020 14:06:42 -0800 (PST) Received: from localhost.localdomain ([109.126.146.5]) by smtp.gmail.com with ESMTPSA id v15sm13281923wrf.7.2020.02.15.14.06.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Feb 2020 14:06:42 -0800 (PST) From: Pavel Begunkov To: Jens Axboe , Alexander Viro , io-uring@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/3] io_uring: add splice(2) support Date: Sun, 16 Feb 2020 01:05:41 +0300 Message-Id: X-Mailer: git-send-email 2.24.0 In-Reply-To: References: MIME-Version: 1.0 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Add support for splice(2). Out file is handled in generic path, input file owned cared by splice* bits only. Signed-off-by: Pavel Begunkov --- fs/io_uring.c | 106 ++++++++++++++++++++++++++++++++++ include/uapi/linux/io_uring.h | 14 ++++- 2 files changed, 119 insertions(+), 1 deletion(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 389db6f5568b..1c71d848c974 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -76,6 +76,7 @@ #include #include #include +#include #define CREATE_TRACE_POINTS #include @@ -433,6 +434,15 @@ struct io_epoll { struct epoll_event event; }; +struct io_splice { + struct file *file_out; + struct file *file_in; + loff_t off_out; + loff_t off_in; + u64 len; + unsigned int flags; +}; + struct io_async_connect { struct sockaddr_storage address; }; @@ -546,6 +556,7 @@ struct io_kiocb { struct io_fadvise fadvise; struct io_madvise madvise; struct io_epoll epoll; + struct io_splice splice; }; struct io_async_ctx *io; @@ -746,6 +757,11 @@ static const struct io_op_def io_op_defs[] = { .unbound_nonreg_file = 1, .file_table = 1, }, + [IORING_OP_SPLICE] = { + .needs_file = 1, + .hash_reg_file = 1, + .unbound_nonreg_file = 1, + } }; static void io_wq_submit_work(struct io_wq_work **workptr); @@ -760,6 +776,10 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx, static int io_grab_files(struct io_kiocb *req); static void io_ring_file_ref_flush(struct fixed_file_data *data); static void io_cleanup_req(struct io_kiocb *req); +static int io_get_file(struct io_submit_state *state, + struct io_ring_ctx *ctx, + int fd, struct file **out_file, + bool fixed); static struct kmem_cache *req_cachep; @@ -2412,6 +2432,77 @@ static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, return ret; } +static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) +{ + struct io_splice* sp = &req->splice; + unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; + int ret; + + if (req->flags & REQ_F_NEED_CLEANUP) + return 0; + + sp->file_in = NULL; + sp->off_in = READ_ONCE(sqe->off_in); + sp->off_out = READ_ONCE(sqe->off); + sp->len = READ_ONCE(sqe->len); + sp->flags = READ_ONCE(sqe->splice_flags); + + if (unlikely(READ_ONCE(sqe->ioprio) || (sp->flags & ~valid_flags))) + return -EINVAL; + + ret = io_get_file(NULL, req->ctx, READ_ONCE(sqe->splice_fd_in), + &sp->file_in, (sp->flags & SPLICE_F_FD_IN_FIXED)); + if (ret) + return ret; + req->flags |= REQ_F_NEED_CLEANUP; + + if (!S_ISREG(file_inode(sp->file_in)->i_mode)) + req->work.flags |= IO_WQ_WORK_UNBOUND; + + return 0; +} + +static bool io_splice_punt(struct file *file) +{ + if (get_pipe_info(file)) + return false; + if (!io_file_supports_async(file)) + return true; + return !(file->f_mode & O_NONBLOCK); +} + +static int io_splice(struct io_kiocb *req, struct io_kiocb **nxt, + bool force_nonblock) +{ + struct io_splice* sp = &req->splice; + struct file *in = sp->file_in; + struct file *out = sp->file_out; + unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; + loff_t *poff_in, *poff_out; + long ret; + + if (force_nonblock) { + if (io_splice_punt(in) || io_splice_punt(out)) + return -EAGAIN; + flags |= SPLICE_F_NONBLOCK; + } + + poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; + poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; + ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); + if (force_nonblock && ret == -EAGAIN) + return -EAGAIN; + + io_put_file(req->ctx, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); + req->flags &= ~REQ_F_NEED_CLEANUP; + + io_cqring_add_event(req, ret); + if (ret != sp->len) + req_set_fail_links(req); + io_put_req_find_next(req, nxt); + return 0; +} + /* * IORING_OP_NOP just posts a completion event, nothing else. */ @@ -4227,6 +4318,9 @@ static int io_req_defer_prep(struct io_kiocb *req, case IORING_OP_EPOLL_CTL: ret = io_epoll_ctl_prep(req, sqe); break; + case IORING_OP_SPLICE: + ret = io_splice_prep(req, sqe); + break; default: printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", req->opcode); @@ -4289,6 +4383,10 @@ static void io_cleanup_req(struct io_kiocb *req) case IORING_OP_STATX: putname(req->open.filename); break; + case IORING_OP_SPLICE: + io_put_file(req->ctx, req->splice.file_in, + (req->splice.flags & SPLICE_F_FD_IN_FIXED)); + break; } req->flags &= ~REQ_F_NEED_CLEANUP; @@ -4492,6 +4590,14 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, } ret = io_epoll_ctl(req, nxt, force_nonblock); break; + case IORING_OP_SPLICE: + if (sqe) { + ret = io_splice_prep(req, sqe); + if (ret < 0) + break; + } + ret = io_splice(req, nxt, force_nonblock); + break; default: ret = -EINVAL; break; diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h index 3f7961c1c243..bc2fe0281de7 100644 --- a/include/uapi/linux/io_uring.h +++ b/include/uapi/linux/io_uring.h @@ -23,7 +23,10 @@ struct io_uring_sqe { __u64 off; /* offset into file */ __u64 addr2; }; - __u64 addr; /* pointer to buffer or iovecs */ + union { + __u64 addr; /* pointer to buffer or iovecs */ + __u64 off_in; + }; __u32 len; /* buffer size or number of iovecs */ union { __kernel_rwf_t rw_flags; @@ -37,6 +40,7 @@ struct io_uring_sqe { __u32 open_flags; __u32 statx_flags; __u32 fadvise_advice; + __u32 splice_flags; }; __u64 user_data; /* data to be passed back at completion time */ union { @@ -45,6 +49,7 @@ struct io_uring_sqe { __u16 buf_index; /* personality to use, if used */ __u16 personality; + __u32 splice_fd_in; }; __u64 __pad2[3]; }; @@ -113,6 +118,7 @@ enum { IORING_OP_RECV, IORING_OP_OPENAT2, IORING_OP_EPOLL_CTL, + IORING_OP_SPLICE, /* this goes last, obviously */ IORING_OP_LAST, @@ -128,6 +134,12 @@ enum { */ #define IORING_TIMEOUT_ABS (1U << 0) +/* + * sqe->splice_flags + * extends splice(2) flags + */ +#define SPLICE_F_FD_IN_FIXED (1U << 31) /* the last bit of __u32 */ + /* * IO completion data structure (Completion Queue Entry) */