From patchwork Mon May 7 08:37:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 10383445 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 5D80460353 for ; Mon, 7 May 2018 08:39:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5323E28A96 for ; Mon, 7 May 2018 08:39:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 45ED528B2C; Mon, 7 May 2018 08:39:17 +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=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, 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 5469528A96 for ; Mon, 7 May 2018 08:39:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752280AbeEGIjE (ORCPT ); Mon, 7 May 2018 04:39:04 -0400 Received: from mail-wr0-f193.google.com ([209.85.128.193]:42173 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752169AbeEGIih (ORCPT ); Mon, 7 May 2018 04:38:37 -0400 Received: by mail-wr0-f193.google.com with SMTP id v5-v6so27852311wrf.9 for ; Mon, 07 May 2018 01:38:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=t5kXP6p2/3VLK3mte+VZowkGXTpxEkdcDkJToJf4dBU=; b=pzsNFH1Nt+Ju/m1zgbnNsNsH7cS4kTOmglkipI+6kTPimKYhUtnPmVsSCGexs0hY3n M2W38fIgyh6UEzaEPg6iELXMjFdF2E1NeswMpdV+6DwFopd7o3f92rnrg7iO7Vo4HhIs t03EvFXn1rswsMfItmig/7q8fCwlJOpGm+gpiiOyVYoARps/2QwrroabKV1bYShWfvCs nGx+mz/sPmOR0ENcZHkBmy+g4/CpjIwQ5uxvF52MVp8BmZbxRs01jsEYb3TAOF4wgRkK ghCl+WyXU6P+dSGb2SzFC99dSz1694+Xe+nVhYvwLpxcc3jmeH1THUcokn4AknFtFQX7 Jt8A== X-Gm-Message-State: ALQs6tAeIuHEgXKlLKqhlzBmhToKzHwmPg+g+ykex8KsKvRV2Ytw1Fey yVyEixGSuW8uYFSxGNH4IgS9SQ== X-Google-Smtp-Source: AB8JxZqF0nvoTgh2nyJ5jVgyBrKXJ9vPMaD9aOxsAeJNE8q6UwBBIaw4nHrjAGOeoduQrNv+aGpy1A== X-Received: by 2002:adf:968b:: with SMTP id u11-v6mr30194110wrb.184.1525682316606; Mon, 07 May 2018 01:38:36 -0700 (PDT) Received: from veci.piliscsaba.redhat.com (C2B0E321.catv.pool.telekom.hu. [194.176.227.33]) by smtp.gmail.com with ESMTPSA id h8-v6sm5908050wmc.16.2018.05.07.01.38.35 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 May 2018 01:38:36 -0700 (PDT) From: Miklos Szeredi To: linux-unionfs@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 21/35] ovl: add reflink/copyfile/dedup support Date: Mon, 7 May 2018 10:37:53 +0200 Message-Id: <20180507083807.28792-22-mszeredi@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180507083807.28792-1-mszeredi@redhat.com> References: <20180507083807.28792-1-mszeredi@redhat.com> 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 Since set of arguments are so similar, handle in a common helper. Signed-off-by: Miklos Szeredi --- fs/overlayfs/file.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c index ce871a15e185..2ac95c95e8e6 100644 --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -382,6 +382,90 @@ static long ovl_compat_ioctl(struct file *file, unsigned int cmd, return ovl_ioctl(file, cmd, arg); } +enum ovl_copyop { + OVL_COPY, + OVL_CLONE, + OVL_DEDUPE, +}; + +static s64 ovl_copyfile(struct file *file_in, loff_t pos_in, + struct file *file_out, loff_t pos_out, + u64 len, unsigned int flags, enum ovl_copyop op) +{ + struct inode *inode_out = file_inode(file_out); + struct fd real_in, real_out; + const struct cred *old_cred; + s64 ret; + + ret = ovl_real_fdget(file_out, &real_out); + if (ret) + return ret; + + ret = ovl_real_fdget(file_in, &real_in); + if (ret) { + fdput(real_out); + return ret; + } + + old_cred = ovl_override_creds(file_inode(file_out)->i_sb); + switch (op) { + case OVL_COPY: + ret = vfs_copy_file_range(real_in.file, pos_in, + real_out.file, pos_out, len, flags); + break; + + case OVL_CLONE: + ret = vfs_clone_file_range(real_in.file, pos_in, + real_out.file, pos_out, len); + break; + + case OVL_DEDUPE: + ret = vfs_dedupe_file_range_one(real_in.file, pos_in, + real_out.file, pos_out, len); + break; + } + revert_creds(old_cred); + + /* Update size */ + ovl_copyattr(ovl_inode_real(inode_out), inode_out); + + fdput(real_in); + fdput(real_out); + + return ret; +} + +static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in, + struct file *file_out, loff_t pos_out, + size_t len, unsigned int flags) +{ + return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags, + OVL_COPY); +} + +static int ovl_clone_file_range(struct file *file_in, loff_t pos_in, + struct file *file_out, loff_t pos_out, u64 len) +{ + return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, 0, + OVL_CLONE); +} + +static s64 ovl_dedupe_file_range(struct file *file_in, loff_t pos_in, + struct file *file_out, loff_t pos_out, + u64 len) +{ + /* + * Don't copy up because of a dedupe request, this wouldn't make sense + * most of the time (data would be duplicated instead of deduplicated). + */ + if (!ovl_inode_upper(file_inode(file_in)) || + !ovl_inode_upper(file_inode(file_out))) + return -EPERM; + + return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, 0, + OVL_DEDUPE); +} + const struct file_operations ovl_file_operations = { .open = ovl_open, .release = ovl_release, @@ -393,4 +477,8 @@ const struct file_operations ovl_file_operations = { .fallocate = ovl_fallocate, .unlocked_ioctl = ovl_ioctl, .compat_ioctl = ovl_compat_ioctl, + + .copy_file_range = ovl_copy_file_range, + .clone_file_range = ovl_clone_file_range, + .dedupe_file_range = ovl_dedupe_file_range, };