From patchwork Mon May 7 08:21:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 10383419 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 2982D602D8 for ; Mon, 7 May 2018 08:22:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1DE6827031 for ; Mon, 7 May 2018 08:22:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 12DA8287F3; Mon, 7 May 2018 08:22:02 +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 B6BF227031 for ; Mon, 7 May 2018 08:22:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752064AbeEGIVh (ORCPT ); Mon, 7 May 2018 04:21:37 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:38380 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752022AbeEGIVS (ORCPT ); Mon, 7 May 2018 04:21:18 -0400 Received: by mail-wm0-f65.google.com with SMTP id m198-v6so12958370wmg.3 for ; Mon, 07 May 2018 01:21:17 -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=XVgWjpdsjRB17Og1OCU0m7DYRAuDIRV8q+Iy9w/kqMg=; b=CTvnSzhsdL/L17UBunpJIWDhYcNXNIi3BGqAToJcP4MuriPvjQN8EOVhsdNz+eiOkZ ttuTMnRBlGOe4T8QED/ruWQhJ0kXlThVrrDcd7nO/gLSHrevCWB6gFovt1sBwn1RfOza h2zkRs4G0Aomp7J+wpwZuOq1IemufqyHVgQkfPTmsHhcanHofDcQIRAomnRUqMKLWiBt QkagPWbzrxsLzM3K3qh8a86oSuoKMNcMDuCLNJk8wUHKYQPOM53iksidmogiZ8g+tV6n bjlQW50i9s7G8ERht2jGDhQX9MsRCfIir3doJBRqMu9+/qKvMVqtiS4+bmZHelXzMGHC 7aRw== X-Gm-Message-State: ALKqPwfaxdcI1bRNtrxYIqW4VtPBczBBCK0V0jDEeUf1TR40nN8erKEW 3XVLInyllNdgGJMXGTqSFVebDw== X-Google-Smtp-Source: AB8JxZqrKqtUcsuo63zKkxz5LCNue3l6liHQ404hqTnr0WOjniTA8RoHO6KH5SIpmayBbWL0lPQUlg== X-Received: by 2002:a1c:7e8d:: with SMTP id z135-v6mr146948wmc.6.1525681277117; Mon, 07 May 2018 01:21:17 -0700 (PDT) Received: from veci.piliscsaba.redhat.com (C2B0E321.catv.pool.telekom.hu. [194.176.227.33]) by smtp.gmail.com with ESMTPSA id w6-v6sm26991776wra.16.2018.05.07.01.21.16 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 May 2018 01:21:16 -0700 (PDT) From: Miklos Szeredi To: Al Viro Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Amir Goldstein Subject: [PATCH 3/3] vfs: dedupe: extract helper for a single dedup Date: Mon, 7 May 2018 10:21:08 +0200 Message-Id: <20180507082108.28186-4-mszeredi@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180507082108.28186-1-mszeredi@redhat.com> References: <20180507082108.28186-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 Extract vfs_dedupe_file_range_one() helper to deal with a single dedup request. Signed-off-by: Miklos Szeredi --- fs/read_write.c | 89 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 49 insertions(+), 40 deletions(-) diff --git a/fs/read_write.c b/fs/read_write.c index 0cdef381d9d9..023df230e2a0 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -1964,6 +1964,44 @@ int vfs_dedupe_file_range_compare(struct inode *src, loff_t srcoff, } EXPORT_SYMBOL(vfs_dedupe_file_range_compare); +static s64 vfs_dedupe_file_range_one(struct file *src_file, loff_t src_pos, + struct file *dst_file, loff_t dst_pos, + u64 len) +{ + s64 ret; + + ret = mnt_want_write_file(dst_file); + if (ret) + return ret; + + ret = clone_verify_area(dst_file, dst_pos, len, true); + if (ret < 0) + goto out_drop_write; + + ret = -EINVAL; + if (!(capable(CAP_SYS_ADMIN) || (dst_file->f_mode & FMODE_WRITE))) + goto out_drop_write; + + ret = -EXDEV; + if (src_file->f_path.mnt != dst_file->f_path.mnt) + goto out_drop_write; + + ret = -EISDIR; + if (S_ISDIR(file_inode(dst_file)->i_mode)) + goto out_drop_write; + + ret = -EINVAL; + if (!dst_file->f_op->dedupe_file_range) + goto out_drop_write; + + ret = dst_file->f_op->dedupe_file_range(src_file, src_pos, + dst_file, dst_pos, len); +out_drop_write: + mnt_drop_write_file(dst_file); + + return ret; +} + int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same) { struct file_dedupe_range_info *info; @@ -1972,10 +2010,7 @@ int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same) u64 len; int i; int ret; - bool is_admin = capable(CAP_SYS_ADMIN); u16 count = same->dest_count; - struct file *dst_file; - loff_t dst_off; s64 deduped; if (!(file->f_mode & FMODE_READ)) @@ -2010,54 +2045,28 @@ int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same) } for (i = 0, info = same->info; i < count; i++, info++) { - struct inode *dst; struct fd dst_fd = fdget(info->dest_fd); + struct file *dst_file = dst_fd.file; - dst_file = dst_fd.file; if (!dst_file) { info->status = -EBADF; goto next_loop; } - dst = file_inode(dst_file); - - ret = mnt_want_write_file(dst_file); - if (ret) { - info->status = ret; - goto next_loop; - } - - dst_off = info->dest_offset; - ret = clone_verify_area(dst_file, dst_off, len, true); - if (ret < 0) { - info->status = ret; - goto next_file; - } - ret = 0; if (info->reserved) { info->status = -EINVAL; - } else if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) { - info->status = -EINVAL; - } else if (file->f_path.mnt != dst_file->f_path.mnt) { - info->status = -EXDEV; - } else if (S_ISDIR(dst->i_mode)) { - info->status = -EISDIR; - } else if (dst_file->f_op->dedupe_file_range == NULL) { - info->status = -EINVAL; - } else { - deduped = dst_file->f_op->dedupe_file_range(file, off, - dst_file, - info->dest_offset, len); - if (deduped == -EBADE) - info->status = FILE_DEDUPE_RANGE_DIFFERS; - else if (deduped < 0) - info->status = deduped; - else - info->bytes_deduped += deduped; + goto next_loop; } -next_file: - mnt_drop_write_file(dst_file); + deduped = vfs_dedupe_file_range_one(file, off, dst_file, + info->dest_offset, len); + if (deduped == -EBADE) + info->status = FILE_DEDUPE_RANGE_DIFFERS; + else if (deduped < 0) + info->status = deduped; + else + info->bytes_deduped += deduped; + next_loop: fdput(dst_fd);