From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.0 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 88CCDC433EB for ; Fri, 7 Aug 2020 13:14:09 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 48D93221E2 for ; Fri, 7 Aug 2020 13:14:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 48D93221E2 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=cn.fujitsu.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 050848D00A9; Fri, 7 Aug 2020 09:14:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 000EC8D003B; Fri, 7 Aug 2020 09:14:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E586D8D00A9; Fri, 7 Aug 2020 09:14:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0068.hostedemail.com [216.40.44.68]) by kanga.kvack.org (Postfix) with ESMTP id CD7288D003B for ; Fri, 7 Aug 2020 09:14:02 -0400 (EDT) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 8E7A82490 for ; Fri, 7 Aug 2020 13:14:02 +0000 (UTC) X-FDA: 77123815524.19.can14_3e0831526fc0 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin19.hostedemail.com (Postfix) with ESMTP id 52BD91AD1B9 for ; Fri, 7 Aug 2020 13:14:02 +0000 (UTC) X-HE-Tag: can14_3e0831526fc0 X-Filterd-Recvd-Size: 10190 Received: from heian.cn.fujitsu.com (mail.cn.fujitsu.com [183.91.158.132]) by imf22.hostedemail.com (Postfix) with ESMTP for ; Fri, 7 Aug 2020 13:13:59 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.75,445,1589212800"; d="scan'208";a="97774935" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 07 Aug 2020 21:13:54 +0800 Received: from G08CNEXMBPEKD06.g08.fujitsu.local (unknown [10.167.33.206]) by cn.fujitsu.com (Postfix) with ESMTP id C0FA64CE34EF; Fri, 7 Aug 2020 21:13:51 +0800 (CST) Received: from G08CNEXJMPEKD02.g08.fujitsu.local (10.167.33.202) by G08CNEXMBPEKD06.g08.fujitsu.local (10.167.33.206) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Fri, 7 Aug 2020 21:13:42 +0800 Received: from G08CNEXCHPEKD06.g08.fujitsu.local (10.167.33.205) by G08CNEXJMPEKD02.g08.fujitsu.local (10.167.33.202) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Fri, 7 Aug 2020 21:13:43 +0800 Received: from localhost.localdomain (10.167.225.141) by G08CNEXCHPEKD06.g08.fujitsu.local (10.167.33.209) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Fri, 7 Aug 2020 21:13:41 +0800 From: Shiyang Ruan To: , , CC: , , , , , , , , , Goldwyn Rodrigues Subject: [RFC PATCH 6/8] fsdax: dedup file range to use a compare function Date: Fri, 7 Aug 2020 21:13:34 +0800 Message-ID: <20200807131336.318774-7-ruansy.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200807131336.318774-1-ruansy.fnst@cn.fujitsu.com> References: <20200807131336.318774-1-ruansy.fnst@cn.fujitsu.com> MIME-Version: 1.0 Content-Type: text/plain X-yoursite-MailScanner-ID: C0FA64CE34EF.AE9E3 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: ruansy.fnst@cn.fujitsu.com X-Rspamd-Queue-Id: 52BD91AD1B9 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: With dax we cannot deal with readpage() etc. So, we create a funciton callback to perform the file data comparison and pass it to generic_remap_file_range_prep() so it can use iomap-based functions. This may not be the best way to solve this. Suggestions welcome. Signed-off-by: Goldwyn Rodrigues Signed-off-by: Shiyang Ruan --- fs/btrfs/reflink.c | 3 +- fs/dax.c | 67 ++++++++++++++++++++++++++++++++++++++++++++ fs/ocfs2/file.c | 2 +- fs/read_write.c | 11 ++++---- fs/xfs/xfs_reflink.c | 2 +- include/linux/dax.h | 5 ++++ include/linux/fs.h | 9 +++++- 7 files changed, 90 insertions(+), 9 deletions(-) diff --git a/fs/btrfs/reflink.c b/fs/btrfs/reflink.c index 040009d1cc31..d0412bc338da 100644 --- a/fs/btrfs/reflink.c +++ b/fs/btrfs/reflink.c @@ -765,7 +765,8 @@ static int btrfs_remap_file_range_prep(struct file *f= ile_in, loff_t pos_in, return ret; =20 return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out= , - len, remap_flags); + len, remap_flags, + vfs_dedupe_file_range_compare); } =20 loff_t btrfs_remap_file_range(struct file *src_file, loff_t off, diff --git a/fs/dax.c b/fs/dax.c index 28b8e23b11ac..80a9946fd25a 100644 --- a/fs/dax.c +++ b/fs/dax.c @@ -30,6 +30,8 @@ #define CREATE_TRACE_POINTS #include =20 +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) + static inline unsigned int pe_order(enum page_entry_size pe_size) { if (pe_size =3D=3D PE_SIZE_PTE) @@ -1874,3 +1876,68 @@ vm_fault_t dax_finish_sync_fault(struct vm_fault *= vmf, return dax_insert_pfn_mkwrite(vmf, pfn, order); } EXPORT_SYMBOL_GPL(dax_finish_sync_fault); + +int dax_file_range_compare(struct inode *src, loff_t srcoff, struct inod= e *dest, + loff_t destoff, loff_t len, bool *is_same, + const struct iomap_ops *ops) +{ + void *saddr, *daddr; + struct iomap smap =3D { 0 }; + struct iomap dmap =3D { 0 }; + bool same =3D false; + loff_t cmp_len; + int id, ret =3D 0; + + id =3D dax_read_lock(); + while (len) { + ret =3D ops->iomap_begin(src, srcoff, len, 0, &smap, NULL); + if (ret < 0) + goto out_src; + cmp_len =3D MIN(len, smap.offset + smap.length - srcoff); + + ret =3D ops->iomap_begin(dest, destoff, cmp_len, 0, &dmap, NULL); + if (ret < 0) + goto out_dest; + cmp_len =3D MIN(cmp_len, dmap.offset + dmap.length - destoff); + + if (smap.type =3D=3D IOMAP_HOLE && dmap.type =3D=3D IOMAP_HOLE) + goto next; + + if (smap.type =3D=3D IOMAP_HOLE || dmap.type =3D=3D IOMAP_HOLE) { + same =3D false; + goto next; + } + + ret =3D dax_iomap_direct_access(&smap, srcoff, + ALIGN(srcoff + cmp_len, PAGE_SIZE), NULL, &saddr); + if (ret < 0) + goto out_dest; + + ret =3D dax_iomap_direct_access(&dmap, destoff, + ALIGN(destoff + cmp_len, PAGE_SIZE), NULL, &daddr); + if (ret < 0) + goto out_dest; + + same =3D !memcmp(saddr, daddr, cmp_len); + if (!same) + break; +next: + len -=3D cmp_len; + srcoff +=3D cmp_len; + destoff +=3D cmp_len; +out_dest: + if (ops->iomap_end) + ops->iomap_end(dest, destoff, cmp_len, 0, 0, &dmap); +out_src: + if (ops->iomap_end) + ops->iomap_end(src, srcoff, len, 0, 0, &smap); + + if (ret < 0) + goto out; + } + *is_same =3D same; +out: + dax_read_unlock(id); + return ret; +} +EXPORT_SYMBOL_GPL(dax_file_range_compare); diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c index 85979e2214b3..9d101f129d16 100644 --- a/fs/ocfs2/file.c +++ b/fs/ocfs2/file.c @@ -2591,7 +2591,7 @@ static loff_t ocfs2_remap_file_range(struct file *f= ile_in, loff_t pos_in, goto out_unlock; =20 ret =3D generic_remap_file_range_prep(file_in, pos_in, file_out, pos_ou= t, - &len, remap_flags); + &len, remap_flags, vfs_dedupe_file_range_compare); if (ret < 0 || len =3D=3D 0) goto out_unlock; =20 diff --git a/fs/read_write.c b/fs/read_write.c index 4fb797822567..2974a624f232 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -1927,7 +1927,7 @@ static void vfs_unlock_two_pages(struct page *page1= , struct page *page2) * Compare extents of two files to see if they are the same. * Caller must have locked both inodes to prevent write races. */ -static int vfs_dedupe_file_range_compare(struct inode *src, loff_t srcof= f, +int vfs_dedupe_file_range_compare(struct inode *src, loff_t srcoff, struct inode *dest, loff_t destoff, loff_t len, bool *is_same) { @@ -2008,6 +2008,7 @@ static int vfs_dedupe_file_range_compare(struct ino= de *src, loff_t srcoff, out_error: return error; } +EXPORT_SYMBOL_GPL(vfs_dedupe_file_range_compare); =20 /* * Check that the two inodes are eligible for cloning, the ranges make @@ -2019,7 +2020,8 @@ static int vfs_dedupe_file_range_compare(struct ino= de *src, loff_t srcoff, */ int generic_remap_file_range_prep(struct file *file_in, loff_t pos_in, struct file *file_out, loff_t pos_out, - loff_t *len, unsigned int remap_flags) + loff_t *len, unsigned int remap_flags, + compare_range_t compare) { struct inode *inode_in =3D file_inode(file_in); struct inode *inode_out =3D file_inode(file_out); @@ -2078,9 +2080,8 @@ int generic_remap_file_range_prep(struct file *file= _in, loff_t pos_in, */ if (remap_flags & REMAP_FILE_DEDUP) { bool is_same =3D false; - - ret =3D vfs_dedupe_file_range_compare(inode_in, pos_in, - inode_out, pos_out, *len, &is_same); + ret =3D (*compare)(inode_in, pos_in, + inode_out, pos_out, *len, &is_same); if (ret) return ret; if (!is_same) diff --git a/fs/xfs/xfs_reflink.c b/fs/xfs/xfs_reflink.c index 107bf2a2f344..792217cd1e64 100644 --- a/fs/xfs/xfs_reflink.c +++ b/fs/xfs/xfs_reflink.c @@ -1338,7 +1338,7 @@ xfs_reflink_remap_prep( goto out_unlock; =20 ret =3D generic_remap_file_range_prep(file_in, pos_in, file_out, pos_ou= t, - len, remap_flags); + len, remap_flags, vfs_dedupe_file_range_compare); if (ret < 0 || *len =3D=3D 0) goto out_unlock; =20 diff --git a/include/linux/dax.h b/include/linux/dax.h index d6d9f4b721bc..607e21933928 100644 --- a/include/linux/dax.h +++ b/include/linux/dax.h @@ -223,6 +223,11 @@ int dax_invalidate_mapping_entry_sync(struct address= _space *mapping, pgoff_t index); int dax_iomap_zero(loff_t pos, unsigned offset, unsigned size, struct iomap *iomap); +int dax_file_range_compare(struct inode *src, loff_t srcoff, + struct inode *dest, loff_t destoff, + loff_t len, bool *is_same, + const struct iomap_ops *ops); + static inline bool dax_mapping(struct address_space *mapping) { return mapping->host && IS_DAX(mapping->host); diff --git a/include/linux/fs.h b/include/linux/fs.h index 81de3d2739b9..c2d985295db5 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1924,13 +1924,20 @@ extern ssize_t vfs_readv(struct file *, const str= uct iovec __user *, unsigned long, loff_t *, rwf_t); extern ssize_t vfs_copy_file_range(struct file *, loff_t , struct file *= , loff_t, size_t, unsigned int); +extern int vfs_dedupe_file_range_compare(struct inode *src, loff_t srcof= f, + struct inode *dest, loff_t destoff, + loff_t len, bool *is_same); +typedef int (*compare_range_t)(struct inode *src, loff_t srcpos, + struct inode *dest, loff_t destpos, + loff_t len, bool *is_same); extern ssize_t generic_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); extern int generic_remap_file_range_prep(struct file *file_in, loff_t po= s_in, struct file *file_out, loff_t pos_out, loff_t *count, - unsigned int remap_flags); + unsigned int remap_flags, + compare_range_t cmp); extern loff_t do_clone_file_range(struct file *file_in, loff_t pos_in, struct file *file_out, loff_t pos_out, loff_t len, unsigned int remap_flags); --=20 2.27.0