Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758270AbeAIGbO (ORCPT + 1 other); Tue, 9 Jan 2018 01:31:14 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:51896 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1755227AbeAIGbK (ORCPT ); Tue, 9 Jan 2018 01:31:10 -0500 From: Mike Rapoport To: Andrew Morton , Alexander Viro Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, criu@openvz.org, gdb@sourceware.org, devel@lists.open-mpi.org, rr-dev@mozilla.org, Arnd Bergmann , Pavel Emelyanov , Michael Kerrisk , Thomas Gleixner , Josh Triplett , Jann Horn , Greg KH , Andrei Vagin , Mike Rapoport Subject: [PATCH v5 1/4] fs/splice: introduce pages_to_pipe helper Date: Tue, 9 Jan 2018 08:30:50 +0200 X-Mailer: git-send-email 2.7.4 In-Reply-To: <1515479453-14672-1-git-send-email-rppt@linux.vnet.ibm.com> References: <1515479453-14672-1-git-send-email-rppt@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18010906-0008-0000-0000-000004BF72CB X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18010906-0009-0000-0000-00001E52C4B4 Message-Id: <1515479453-14672-2-git-send-email-rppt@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2018-01-09_04:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1801090091 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: Signed-off-by: Mike Rapoport --- fs/splice.c | 57 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/fs/splice.c b/fs/splice.c index 39e2dc01ac12..7f1ffc50ff1d 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -1185,6 +1185,36 @@ static long do_splice(struct file *in, loff_t __user *off_in, return -EINVAL; } +static int pages_to_pipe(struct page **pages, struct pipe_inode_info *pipe, + struct pipe_buffer *buf, size_t *total, + ssize_t copied, size_t start) +{ + bool failed = false; + size_t len = 0; + int ret = 0; + int n; + + for (n = 0; copied; n++, start = 0) { + int size = min_t(int, copied, PAGE_SIZE - start); + if (!failed) { + buf->page = pages[n]; + buf->offset = start; + buf->len = size; + ret = add_to_pipe(pipe, buf); + if (unlikely(ret < 0)) + failed = true; + else + len += ret; + } else { + put_page(pages[n]); + } + copied -= size; + } + + *total += len; + return failed ? ret : len; +} + static int iter_to_pipe(struct iov_iter *from, struct pipe_inode_info *pipe, unsigned flags) @@ -1195,13 +1225,11 @@ static int iter_to_pipe(struct iov_iter *from, }; size_t total = 0; int ret = 0; - bool failed = false; - while (iov_iter_count(from) && !failed) { + while (iov_iter_count(from)) { struct page *pages[16]; ssize_t copied; size_t start; - int n; copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start); if (copied <= 0) { @@ -1209,24 +1237,11 @@ static int iter_to_pipe(struct iov_iter *from, break; } - for (n = 0; copied; n++, start = 0) { - int size = min_t(int, copied, PAGE_SIZE - start); - if (!failed) { - buf.page = pages[n]; - buf.offset = start; - buf.len = size; - ret = add_to_pipe(pipe, &buf); - if (unlikely(ret < 0)) { - failed = true; - } else { - iov_iter_advance(from, ret); - total += ret; - } - } else { - put_page(pages[n]); - } - copied -= size; - } + ret = pages_to_pipe(pages, pipe, &buf, &total, copied, start); + if (unlikely(ret < 0)) + break; + + iov_iter_advance(from, ret); } return total ? total : ret; } -- 2.7.4