From patchwork Wed Jan 11 09:51:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Thumshirn X-Patchwork-Id: 9509719 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9C00160231 for ; Wed, 11 Jan 2017 09:54:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 95B3828584 for ; Wed, 11 Jan 2017 09:54:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8A48728596; Wed, 11 Jan 2017 09:54:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 10B3C28584 for ; Wed, 11 Jan 2017 09:54:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935267AbdAKJyB (ORCPT ); Wed, 11 Jan 2017 04:54:01 -0500 Received: from mx2.suse.de ([195.135.220.15]:49997 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935082AbdAKJyB (ORCPT ); Wed, 11 Jan 2017 04:54:01 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id E6E96AAC8; Wed, 11 Jan 2017 09:53:58 +0000 (UTC) From: Johannes Thumshirn To: Alexander Viro Cc: Jeff Layton , "J . Bruce Fields" , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, mbenes@suse.cz, jack@suse.cz, Johannes Thumshirn Subject: [PATCH RESEND] splice: introduce FMODE_SPLICE_READ and FMODE_SPLICE_WRITE Date: Wed, 11 Jan 2017 10:51:39 +0100 Message-Id: <20170111095139.17783-1-jthumshirn@suse.de> X-Mailer: git-send-email 2.10.2 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Introduce FMODE_SPLICE_READ and FMODE_SPLICE_WRITE. These modes check whether it is legal to read or write a file using splice. Both get automatically set on regular files and are not checked when a 'struct fileoperations' includes the splice_{read,write} methods. Suggested-by: Linus Torvalds Cc: Al Viro Signed-off-by: Johannes Thumshirn --- fs/open.c | 4 ++++ fs/splice.c | 6 ++++++ include/linux/fs.h | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/fs/open.c b/fs/open.c index 9921f70..b71259c 100644 --- a/fs/open.c +++ b/fs/open.c @@ -733,6 +733,10 @@ static int do_dentry_open(struct file *f, if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) f->f_mode |= FMODE_ATOMIC_POS; + if (S_ISREG(inode->i_mode)) + f->f_mode |= FMODE_SPLICE_WRITE | FMODE_SPLICE_READ; + + f->f_op = fops_get(inode->i_fop); if (unlikely(WARN_ON(!f->f_op))) { error = -ENODEV; diff --git a/fs/splice.c b/fs/splice.c index 873d831..b0cfcb2 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -397,6 +397,9 @@ static ssize_t default_file_splice_read(struct file *in, loff_t *ppos, if (pipe->nrbufs == pipe->buffers) return -EAGAIN; + if (unlikely(!(in->f_mode & FMODE_SPLICE_READ))) + return -EINVAL; + /* * Try to keep page boundaries matching to source pagecache ones - * it probably won't be much help, but... @@ -825,6 +828,9 @@ static ssize_t default_file_splice_write(struct pipe_inode_info *pipe, { ssize_t ret; + if (unlikely(!(out->f_mode & FMODE_SPLICE_WRITE))) + return -EINVAL; + ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf); if (ret > 0) *ppos += ret; diff --git a/include/linux/fs.h b/include/linux/fs.h index 2ba0743..30477c5 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -143,6 +143,11 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset, /* File was opened by fanotify and shouldn't generate fanotify events */ #define FMODE_NONOTIFY ((__force fmode_t)0x4000000) +/* File can be read using splice */ +#define FMODE_SPLICE_READ ((__force fmode_t)0x8000000) +/* File can be written using splice */ +#define FMODE_SPLICE_WRITE ((__force fmode_t)0x10000000) + /* * Flag for rw_copy_check_uvector and compat_rw_copy_check_uvector * that indicates that they should check the contents of the iovec are