xref: /openbmc/linux/fs/btrfs/reflink.c (revision 2a9462de)
16a177381SFilipe Manana // SPDX-License-Identifier: GPL-2.0
26a177381SFilipe Manana 
305a5a762SFilipe Manana #include <linux/blkdev.h>
46a177381SFilipe Manana #include <linux/iversion.h>
5ec8eb376SJosef Bacik #include "ctree.h"
6ec8eb376SJosef Bacik #include "fs.h"
79b569ea0SJosef Bacik #include "messages.h"
805a5a762SFilipe Manana #include "compression.h"
905a5a762SFilipe Manana #include "delalloc-space.h"
106fe81a3aSFilipe Manana #include "disk-io.h"
116a177381SFilipe Manana #include "reflink.h"
126a177381SFilipe Manana #include "transaction.h"
133115deb3SQu Wenruo #include "subpage.h"
1407e81dc9SJosef Bacik #include "accessors.h"
157c8ede16SJosef Bacik #include "file-item.h"
16af142b6fSJosef Bacik #include "file.h"
177f0add25SJosef Bacik #include "super.h"
186a177381SFilipe Manana 
196a177381SFilipe Manana #define BTRFS_MAX_DEDUPE_LEN	SZ_16M
206a177381SFilipe Manana 
clone_finish_inode_update(struct btrfs_trans_handle * trans,struct inode * inode,u64 endoff,const u64 destoff,const u64 olen,int no_time_update)216a177381SFilipe Manana static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
226a177381SFilipe Manana 				     struct inode *inode,
236a177381SFilipe Manana 				     u64 endoff,
246a177381SFilipe Manana 				     const u64 destoff,
256a177381SFilipe Manana 				     const u64 olen,
266a177381SFilipe Manana 				     int no_time_update)
276a177381SFilipe Manana {
286a177381SFilipe Manana 	struct btrfs_root *root = BTRFS_I(inode)->root;
296a177381SFilipe Manana 	int ret;
306a177381SFilipe Manana 
316a177381SFilipe Manana 	inode_inc_iversion(inode);
32c1867eb3SDavid Sterba 	if (!no_time_update) {
33*2a9462deSJeff Layton 		inode->i_mtime = inode_set_ctime_current(inode);
34c1867eb3SDavid Sterba 	}
356a177381SFilipe Manana 	/*
366a177381SFilipe Manana 	 * We round up to the block size at eof when determining which
376a177381SFilipe Manana 	 * extents to clone above, but shouldn't round up the file size.
386a177381SFilipe Manana 	 */
396a177381SFilipe Manana 	if (endoff > destoff + olen)
406a177381SFilipe Manana 		endoff = destoff + olen;
416a177381SFilipe Manana 	if (endoff > inode->i_size) {
426a177381SFilipe Manana 		i_size_write(inode, endoff);
4376aea537SNikolay Borisov 		btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
446a177381SFilipe Manana 	}
456a177381SFilipe Manana 
469a56fcd1SNikolay Borisov 	ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
476a177381SFilipe Manana 	if (ret) {
486a177381SFilipe Manana 		btrfs_abort_transaction(trans, ret);
496a177381SFilipe Manana 		btrfs_end_transaction(trans);
506a177381SFilipe Manana 		goto out;
516a177381SFilipe Manana 	}
526a177381SFilipe Manana 	ret = btrfs_end_transaction(trans);
536a177381SFilipe Manana out:
546a177381SFilipe Manana 	return ret;
556a177381SFilipe Manana }
566a177381SFilipe Manana 
copy_inline_to_page(struct btrfs_inode * inode,const u64 file_offset,char * inline_data,const u64 size,const u64 datal,const u8 comp_type)57998acfe8SNikolay Borisov static int copy_inline_to_page(struct btrfs_inode *inode,
5805a5a762SFilipe Manana 			       const u64 file_offset,
5905a5a762SFilipe Manana 			       char *inline_data,
6005a5a762SFilipe Manana 			       const u64 size,
6105a5a762SFilipe Manana 			       const u64 datal,
6205a5a762SFilipe Manana 			       const u8 comp_type)
6305a5a762SFilipe Manana {
643115deb3SQu Wenruo 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
653115deb3SQu Wenruo 	const u32 block_size = fs_info->sectorsize;
6605a5a762SFilipe Manana 	const u64 range_end = file_offset + block_size - 1;
6705a5a762SFilipe Manana 	const size_t inline_size = size - btrfs_file_extent_calc_inline_size(0);
6805a5a762SFilipe Manana 	char *data_start = inline_data + btrfs_file_extent_calc_inline_size(0);
6905a5a762SFilipe Manana 	struct extent_changeset *data_reserved = NULL;
7005a5a762SFilipe Manana 	struct page *page = NULL;
71998acfe8SNikolay Borisov 	struct address_space *mapping = inode->vfs_inode.i_mapping;
7205a5a762SFilipe Manana 	int ret;
7305a5a762SFilipe Manana 
7405a5a762SFilipe Manana 	ASSERT(IS_ALIGNED(file_offset, block_size));
7505a5a762SFilipe Manana 
766a177381SFilipe Manana 	/*
7705a5a762SFilipe Manana 	 * We have flushed and locked the ranges of the source and destination
7805a5a762SFilipe Manana 	 * inodes, we also have locked the inodes, so we are safe to do a
7905a5a762SFilipe Manana 	 * reservation here. Also we must not do the reservation while holding
8005a5a762SFilipe Manana 	 * a transaction open, otherwise we would deadlock.
8105a5a762SFilipe Manana 	 */
82998acfe8SNikolay Borisov 	ret = btrfs_delalloc_reserve_space(inode, &data_reserved, file_offset,
83998acfe8SNikolay Borisov 					   block_size);
8405a5a762SFilipe Manana 	if (ret)
8505a5a762SFilipe Manana 		goto out;
8605a5a762SFilipe Manana 
87998acfe8SNikolay Borisov 	page = find_or_create_page(mapping, file_offset >> PAGE_SHIFT,
88998acfe8SNikolay Borisov 				   btrfs_alloc_write_mask(mapping));
8905a5a762SFilipe Manana 	if (!page) {
9005a5a762SFilipe Manana 		ret = -ENOMEM;
9105a5a762SFilipe Manana 		goto out_unlock;
9205a5a762SFilipe Manana 	}
9305a5a762SFilipe Manana 
9432443de3SQu Wenruo 	ret = set_page_extent_mapped(page);
9532443de3SQu Wenruo 	if (ret < 0)
9632443de3SQu Wenruo 		goto out_unlock;
9732443de3SQu Wenruo 
98998acfe8SNikolay Borisov 	clear_extent_bit(&inode->io_tree, file_offset, range_end,
9905a5a762SFilipe Manana 			 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
100bd015294SJosef Bacik 			 NULL);
101998acfe8SNikolay Borisov 	ret = btrfs_set_extent_delalloc(inode, file_offset, range_end, 0, NULL);
10205a5a762SFilipe Manana 	if (ret)
10305a5a762SFilipe Manana 		goto out_unlock;
10405a5a762SFilipe Manana 
1053d45f221SFilipe Manana 	/*
1063d45f221SFilipe Manana 	 * After dirtying the page our caller will need to start a transaction,
1073d45f221SFilipe Manana 	 * and if we are low on metadata free space, that can cause flushing of
1083d45f221SFilipe Manana 	 * delalloc for all inodes in order to get metadata space released.
1093d45f221SFilipe Manana 	 * However we are holding the range locked for the whole duration of
1103d45f221SFilipe Manana 	 * the clone/dedupe operation, so we may deadlock if that happens and no
1113d45f221SFilipe Manana 	 * other task releases enough space. So mark this inode as not being
1123d45f221SFilipe Manana 	 * possible to flush to avoid such deadlock. We will clear that flag
1133d45f221SFilipe Manana 	 * when we finish cloning all extents, since a transaction is started
1143d45f221SFilipe Manana 	 * after finding each extent to clone.
1153d45f221SFilipe Manana 	 */
1163d45f221SFilipe Manana 	set_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &inode->runtime_flags);
1173d45f221SFilipe Manana 
11805a5a762SFilipe Manana 	if (comp_type == BTRFS_COMPRESS_NONE) {
1193115deb3SQu Wenruo 		memcpy_to_page(page, offset_in_page(file_offset), data_start,
1203115deb3SQu Wenruo 			       datal);
12105a5a762SFilipe Manana 	} else {
1223115deb3SQu Wenruo 		ret = btrfs_decompress(comp_type, data_start, page,
1233115deb3SQu Wenruo 				       offset_in_page(file_offset),
12405a5a762SFilipe Manana 				       inline_size, datal);
12505a5a762SFilipe Manana 		if (ret)
12605a5a762SFilipe Manana 			goto out_unlock;
12705a5a762SFilipe Manana 		flush_dcache_page(page);
12805a5a762SFilipe Manana 	}
12905a5a762SFilipe Manana 
13005a5a762SFilipe Manana 	/*
13105a5a762SFilipe Manana 	 * If our inline data is smaller then the block/page size, then the
13205a5a762SFilipe Manana 	 * remaining of the block/page is equivalent to zeroes. We had something
13305a5a762SFilipe Manana 	 * like the following done:
1346a177381SFilipe Manana 	 *
13505a5a762SFilipe Manana 	 * $ xfs_io -f -c "pwrite -S 0xab 0 500" file
13605a5a762SFilipe Manana 	 * $ sync  # (or fsync)
13705a5a762SFilipe Manana 	 * $ xfs_io -c "falloc 0 4K" file
13805a5a762SFilipe Manana 	 * $ xfs_io -c "pwrite -S 0xcd 4K 4K"
1396a177381SFilipe Manana 	 *
14005a5a762SFilipe Manana 	 * So what's in the range [500, 4095] corresponds to zeroes.
14105a5a762SFilipe Manana 	 */
14221a8935eSDavid Sterba 	if (datal < block_size)
143d048b9c2SIra Weiny 		memzero_page(page, datal, block_size - datal);
14405a5a762SFilipe Manana 
1453115deb3SQu Wenruo 	btrfs_page_set_uptodate(fs_info, page, file_offset, block_size);
146e4f94347SQu Wenruo 	btrfs_page_clear_checked(fs_info, page, file_offset, block_size);
1473115deb3SQu Wenruo 	btrfs_page_set_dirty(fs_info, page, file_offset, block_size);
14805a5a762SFilipe Manana out_unlock:
14905a5a762SFilipe Manana 	if (page) {
15005a5a762SFilipe Manana 		unlock_page(page);
15105a5a762SFilipe Manana 		put_page(page);
15205a5a762SFilipe Manana 	}
15305a5a762SFilipe Manana 	if (ret)
154998acfe8SNikolay Borisov 		btrfs_delalloc_release_space(inode, data_reserved, file_offset,
155998acfe8SNikolay Borisov 					     block_size, true);
156998acfe8SNikolay Borisov 	btrfs_delalloc_release_extents(inode, block_size);
15705a5a762SFilipe Manana out:
15805a5a762SFilipe Manana 	extent_changeset_free(data_reserved);
15905a5a762SFilipe Manana 
16005a5a762SFilipe Manana 	return ret;
16105a5a762SFilipe Manana }
16205a5a762SFilipe Manana 
16305a5a762SFilipe Manana /*
16405a5a762SFilipe Manana  * Deal with cloning of inline extents. We try to copy the inline extent from
16505a5a762SFilipe Manana  * the source inode to destination inode when possible. When not possible we
16605a5a762SFilipe Manana  * copy the inline extent's data into the respective page of the inode.
1676a177381SFilipe Manana  */
clone_copy_inline_extent(struct inode * dst,struct btrfs_path * path,struct btrfs_key * new_key,const u64 drop_start,const u64 datal,const u64 size,const u8 comp_type,char * inline_data,struct btrfs_trans_handle ** trans_out)1686a177381SFilipe Manana static int clone_copy_inline_extent(struct inode *dst,
1696a177381SFilipe Manana 				    struct btrfs_path *path,
1706a177381SFilipe Manana 				    struct btrfs_key *new_key,
1716a177381SFilipe Manana 				    const u64 drop_start,
1726a177381SFilipe Manana 				    const u64 datal,
1736a177381SFilipe Manana 				    const u64 size,
17405a5a762SFilipe Manana 				    const u8 comp_type,
17505a5a762SFilipe Manana 				    char *inline_data,
17605a5a762SFilipe Manana 				    struct btrfs_trans_handle **trans_out)
1776a177381SFilipe Manana {
1786a177381SFilipe Manana 	struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
1796a177381SFilipe Manana 	struct btrfs_root *root = BTRFS_I(dst)->root;
1806a177381SFilipe Manana 	const u64 aligned_end = ALIGN(new_key->offset + datal,
1816a177381SFilipe Manana 				      fs_info->sectorsize);
18205a5a762SFilipe Manana 	struct btrfs_trans_handle *trans = NULL;
1835893dfb9SFilipe Manana 	struct btrfs_drop_extents_args drop_args = { 0 };
1846a177381SFilipe Manana 	int ret;
1856a177381SFilipe Manana 	struct btrfs_key key;
1866a177381SFilipe Manana 
18705a5a762SFilipe Manana 	if (new_key->offset > 0) {
188998acfe8SNikolay Borisov 		ret = copy_inline_to_page(BTRFS_I(dst), new_key->offset,
189998acfe8SNikolay Borisov 					  inline_data, size, datal, comp_type);
19005a5a762SFilipe Manana 		goto out;
19105a5a762SFilipe Manana 	}
1926a177381SFilipe Manana 
1936a177381SFilipe Manana 	key.objectid = btrfs_ino(BTRFS_I(dst));
1946a177381SFilipe Manana 	key.type = BTRFS_EXTENT_DATA_KEY;
1956a177381SFilipe Manana 	key.offset = 0;
1966a177381SFilipe Manana 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1976a177381SFilipe Manana 	if (ret < 0) {
1986a177381SFilipe Manana 		return ret;
1996a177381SFilipe Manana 	} else if (ret > 0) {
2006a177381SFilipe Manana 		if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2016a177381SFilipe Manana 			ret = btrfs_next_leaf(root, path);
2026a177381SFilipe Manana 			if (ret < 0)
2036a177381SFilipe Manana 				return ret;
2046a177381SFilipe Manana 			else if (ret > 0)
2056a177381SFilipe Manana 				goto copy_inline_extent;
2066a177381SFilipe Manana 		}
2076a177381SFilipe Manana 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2086a177381SFilipe Manana 		if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
2096a177381SFilipe Manana 		    key.type == BTRFS_EXTENT_DATA_KEY) {
21005a5a762SFilipe Manana 			/*
21105a5a762SFilipe Manana 			 * There's an implicit hole at file offset 0, copy the
21205a5a762SFilipe Manana 			 * inline extent's data to the page.
21305a5a762SFilipe Manana 			 */
2146a177381SFilipe Manana 			ASSERT(key.offset > 0);
21576a6d5cdSFilipe Manana 			goto copy_to_page;
2166a177381SFilipe Manana 		}
2176a177381SFilipe Manana 	} else if (i_size_read(dst) <= datal) {
2186a177381SFilipe Manana 		struct btrfs_file_extent_item *ei;
2196a177381SFilipe Manana 
2206a177381SFilipe Manana 		ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
2216a177381SFilipe Manana 				    struct btrfs_file_extent_item);
2226a177381SFilipe Manana 		/*
22305a5a762SFilipe Manana 		 * If it's an inline extent replace it with the source inline
22405a5a762SFilipe Manana 		 * extent, otherwise copy the source inline extent data into
22505a5a762SFilipe Manana 		 * the respective page at the destination inode.
2266a177381SFilipe Manana 		 */
2276a177381SFilipe Manana 		if (btrfs_file_extent_type(path->nodes[0], ei) ==
2286a177381SFilipe Manana 		    BTRFS_FILE_EXTENT_INLINE)
2296a177381SFilipe Manana 			goto copy_inline_extent;
2306a177381SFilipe Manana 
23176a6d5cdSFilipe Manana 		goto copy_to_page;
2326a177381SFilipe Manana 	}
2336a177381SFilipe Manana 
2346a177381SFilipe Manana copy_inline_extent:
2356a177381SFilipe Manana 	/*
2366a177381SFilipe Manana 	 * We have no extent items, or we have an extent at offset 0 which may
2376a177381SFilipe Manana 	 * or may not be inlined. All these cases are dealt the same way.
2386a177381SFilipe Manana 	 */
2396a177381SFilipe Manana 	if (i_size_read(dst) > datal) {
2406a177381SFilipe Manana 		/*
24105a5a762SFilipe Manana 		 * At the destination offset 0 we have either a hole, a regular
24205a5a762SFilipe Manana 		 * extent or an inline extent larger then the one we want to
24305a5a762SFilipe Manana 		 * clone. Deal with all these cases by copying the inline extent
24405a5a762SFilipe Manana 		 * data into the respective page at the destination inode.
2456a177381SFilipe Manana 		 */
24676a6d5cdSFilipe Manana 		goto copy_to_page;
2476a177381SFilipe Manana 	}
2486a177381SFilipe Manana 
24976a6d5cdSFilipe Manana 	/*
25076a6d5cdSFilipe Manana 	 * Release path before starting a new transaction so we don't hold locks
25176a6d5cdSFilipe Manana 	 * that would confuse lockdep.
25276a6d5cdSFilipe Manana 	 */
2536a177381SFilipe Manana 	btrfs_release_path(path);
25405a5a762SFilipe Manana 	/*
25505a5a762SFilipe Manana 	 * If we end up here it means were copy the inline extent into a leaf
25605a5a762SFilipe Manana 	 * of the destination inode. We know we will drop or adjust at most one
25705a5a762SFilipe Manana 	 * extent item in the destination root.
25805a5a762SFilipe Manana 	 *
25905a5a762SFilipe Manana 	 * 1 unit - adjusting old extent (we may have to split it)
26005a5a762SFilipe Manana 	 * 1 unit - add new extent
26105a5a762SFilipe Manana 	 * 1 unit - inode update
26205a5a762SFilipe Manana 	 */
26305a5a762SFilipe Manana 	trans = btrfs_start_transaction(root, 3);
26405a5a762SFilipe Manana 	if (IS_ERR(trans)) {
26505a5a762SFilipe Manana 		ret = PTR_ERR(trans);
26605a5a762SFilipe Manana 		trans = NULL;
26705a5a762SFilipe Manana 		goto out;
26805a5a762SFilipe Manana 	}
2695893dfb9SFilipe Manana 	drop_args.path = path;
2705893dfb9SFilipe Manana 	drop_args.start = drop_start;
2715893dfb9SFilipe Manana 	drop_args.end = aligned_end;
2725893dfb9SFilipe Manana 	drop_args.drop_cache = true;
2735893dfb9SFilipe Manana 	ret = btrfs_drop_extents(trans, root, BTRFS_I(dst), &drop_args);
2746a177381SFilipe Manana 	if (ret)
27505a5a762SFilipe Manana 		goto out;
2766a177381SFilipe Manana 	ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
2776a177381SFilipe Manana 	if (ret)
27805a5a762SFilipe Manana 		goto out;
2796a177381SFilipe Manana 
2806a177381SFilipe Manana 	write_extent_buffer(path->nodes[0], inline_data,
2816a177381SFilipe Manana 			    btrfs_item_ptr_offset(path->nodes[0],
2826a177381SFilipe Manana 						  path->slots[0]),
2836a177381SFilipe Manana 			    size);
2842766ff61SFilipe Manana 	btrfs_update_inode_bytes(BTRFS_I(dst), datal, drop_args.bytes_found);
28523e3337fSFilipe Manana 	btrfs_set_inode_full_sync(BTRFS_I(dst));
2864fdb688cSFilipe Manana 	ret = btrfs_inode_set_file_extent_range(BTRFS_I(dst), 0, aligned_end);
28705a5a762SFilipe Manana out:
28805a5a762SFilipe Manana 	if (!ret && !trans) {
28905a5a762SFilipe Manana 		/*
29005a5a762SFilipe Manana 		 * No transaction here means we copied the inline extent into a
29105a5a762SFilipe Manana 		 * page of the destination inode.
29205a5a762SFilipe Manana 		 *
29305a5a762SFilipe Manana 		 * 1 unit to update inode item
29405a5a762SFilipe Manana 		 */
29505a5a762SFilipe Manana 		trans = btrfs_start_transaction(root, 1);
29605a5a762SFilipe Manana 		if (IS_ERR(trans)) {
29705a5a762SFilipe Manana 			ret = PTR_ERR(trans);
29805a5a762SFilipe Manana 			trans = NULL;
29905a5a762SFilipe Manana 		}
30005a5a762SFilipe Manana 	}
30105a5a762SFilipe Manana 	if (ret && trans) {
30205a5a762SFilipe Manana 		btrfs_abort_transaction(trans, ret);
30305a5a762SFilipe Manana 		btrfs_end_transaction(trans);
30405a5a762SFilipe Manana 	}
30505a5a762SFilipe Manana 	if (!ret)
30605a5a762SFilipe Manana 		*trans_out = trans;
3076a177381SFilipe Manana 
30805a5a762SFilipe Manana 	return ret;
30976a6d5cdSFilipe Manana 
31076a6d5cdSFilipe Manana copy_to_page:
31176a6d5cdSFilipe Manana 	/*
31276a6d5cdSFilipe Manana 	 * Release our path because we don't need it anymore and also because
31376a6d5cdSFilipe Manana 	 * copy_inline_to_page() needs to reserve data and metadata, which may
31476a6d5cdSFilipe Manana 	 * need to flush delalloc when we are low on available space and
31576a6d5cdSFilipe Manana 	 * therefore cause a deadlock if writeback of an inline extent needs to
31676a6d5cdSFilipe Manana 	 * write to the same leaf or an ordered extent completion needs to write
31776a6d5cdSFilipe Manana 	 * to the same leaf.
31876a6d5cdSFilipe Manana 	 */
31976a6d5cdSFilipe Manana 	btrfs_release_path(path);
32076a6d5cdSFilipe Manana 
32176a6d5cdSFilipe Manana 	ret = copy_inline_to_page(BTRFS_I(dst), new_key->offset,
32276a6d5cdSFilipe Manana 				  inline_data, size, datal, comp_type);
32376a6d5cdSFilipe Manana 	goto out;
3246a177381SFilipe Manana }
3256a177381SFilipe Manana 
32643dd529aSDavid Sterba /*
32743dd529aSDavid Sterba  * Clone a range from inode file to another.
3286a177381SFilipe Manana  *
3296a177381SFilipe Manana  * @src:             Inode to clone from
3306a177381SFilipe Manana  * @inode:           Inode to clone to
3316a177381SFilipe Manana  * @off:             Offset within source to start clone from
3326a177381SFilipe Manana  * @olen:            Original length, passed by user, of range to clone
3336a177381SFilipe Manana  * @olen_aligned:    Block-aligned value of olen
3346a177381SFilipe Manana  * @destoff:         Offset within @inode to start clone
3356a177381SFilipe Manana  * @no_time_update:  Whether to update mtime/ctime on the target inode
3366a177381SFilipe Manana  */
btrfs_clone(struct inode * src,struct inode * inode,const u64 off,const u64 olen,const u64 olen_aligned,const u64 destoff,int no_time_update)3376a177381SFilipe Manana static int btrfs_clone(struct inode *src, struct inode *inode,
3386a177381SFilipe Manana 		       const u64 off, const u64 olen, const u64 olen_aligned,
3396a177381SFilipe Manana 		       const u64 destoff, int no_time_update)
3406a177381SFilipe Manana {
3416a177381SFilipe Manana 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3426a177381SFilipe Manana 	struct btrfs_path *path = NULL;
3436a177381SFilipe Manana 	struct extent_buffer *leaf;
3446a177381SFilipe Manana 	struct btrfs_trans_handle *trans;
3456a177381SFilipe Manana 	char *buf = NULL;
3466a177381SFilipe Manana 	struct btrfs_key key;
3476a177381SFilipe Manana 	u32 nritems;
3486a177381SFilipe Manana 	int slot;
3496a177381SFilipe Manana 	int ret;
3506a177381SFilipe Manana 	const u64 len = olen_aligned;
3516a177381SFilipe Manana 	u64 last_dest_end = destoff;
352d4597898SFilipe Manana 	u64 prev_extent_end = off;
3536a177381SFilipe Manana 
3546a177381SFilipe Manana 	ret = -ENOMEM;
3556a177381SFilipe Manana 	buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3566a177381SFilipe Manana 	if (!buf)
3576a177381SFilipe Manana 		return ret;
3586a177381SFilipe Manana 
3596a177381SFilipe Manana 	path = btrfs_alloc_path();
3606a177381SFilipe Manana 	if (!path) {
3616a177381SFilipe Manana 		kvfree(buf);
3626a177381SFilipe Manana 		return ret;
3636a177381SFilipe Manana 	}
3646a177381SFilipe Manana 
3656a177381SFilipe Manana 	path->reada = READA_FORWARD;
3666a177381SFilipe Manana 	/* Clone data */
3676a177381SFilipe Manana 	key.objectid = btrfs_ino(BTRFS_I(src));
3686a177381SFilipe Manana 	key.type = BTRFS_EXTENT_DATA_KEY;
3696a177381SFilipe Manana 	key.offset = off;
3706a177381SFilipe Manana 
3716a177381SFilipe Manana 	while (1) {
3726a177381SFilipe Manana 		struct btrfs_file_extent_item *extent;
3733ebac17cSFilipe Manana 		u64 extent_gen;
3746a177381SFilipe Manana 		int type;
3756a177381SFilipe Manana 		u32 size;
3766a177381SFilipe Manana 		struct btrfs_key new_key;
3776a177381SFilipe Manana 		u64 disko = 0, diskl = 0;
3786a177381SFilipe Manana 		u64 datao = 0, datal = 0;
37905a5a762SFilipe Manana 		u8 comp;
3806a177381SFilipe Manana 		u64 drop_start;
3816a177381SFilipe Manana 
3826a177381SFilipe Manana 		/* Note the key will change type as we walk through the tree */
3836a177381SFilipe Manana 		ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3846a177381SFilipe Manana 				0, 0);
3856a177381SFilipe Manana 		if (ret < 0)
3866a177381SFilipe Manana 			goto out;
3876a177381SFilipe Manana 		/*
3886a177381SFilipe Manana 		 * First search, if no extent item that starts at offset off was
3896a177381SFilipe Manana 		 * found but the previous item is an extent item, it's possible
3906a177381SFilipe Manana 		 * it might overlap our target range, therefore process it.
3916a177381SFilipe Manana 		 */
3926a177381SFilipe Manana 		if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3936a177381SFilipe Manana 			btrfs_item_key_to_cpu(path->nodes[0], &key,
3946a177381SFilipe Manana 					      path->slots[0] - 1);
3956a177381SFilipe Manana 			if (key.type == BTRFS_EXTENT_DATA_KEY)
3966a177381SFilipe Manana 				path->slots[0]--;
3976a177381SFilipe Manana 		}
3986a177381SFilipe Manana 
3996a177381SFilipe Manana 		nritems = btrfs_header_nritems(path->nodes[0]);
4006a177381SFilipe Manana process_slot:
4016a177381SFilipe Manana 		if (path->slots[0] >= nritems) {
4026a177381SFilipe Manana 			ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
4036a177381SFilipe Manana 			if (ret < 0)
4046a177381SFilipe Manana 				goto out;
4056a177381SFilipe Manana 			if (ret > 0)
4066a177381SFilipe Manana 				break;
4076a177381SFilipe Manana 			nritems = btrfs_header_nritems(path->nodes[0]);
4086a177381SFilipe Manana 		}
4096a177381SFilipe Manana 		leaf = path->nodes[0];
4106a177381SFilipe Manana 		slot = path->slots[0];
4116a177381SFilipe Manana 
4126a177381SFilipe Manana 		btrfs_item_key_to_cpu(leaf, &key, slot);
4136a177381SFilipe Manana 		if (key.type > BTRFS_EXTENT_DATA_KEY ||
4146a177381SFilipe Manana 		    key.objectid != btrfs_ino(BTRFS_I(src)))
4156a177381SFilipe Manana 			break;
4166a177381SFilipe Manana 
4176a177381SFilipe Manana 		ASSERT(key.type == BTRFS_EXTENT_DATA_KEY);
4186a177381SFilipe Manana 
4196a177381SFilipe Manana 		extent = btrfs_item_ptr(leaf, slot,
4206a177381SFilipe Manana 					struct btrfs_file_extent_item);
4213ebac17cSFilipe Manana 		extent_gen = btrfs_file_extent_generation(leaf, extent);
42205a5a762SFilipe Manana 		comp = btrfs_file_extent_compression(leaf, extent);
4236a177381SFilipe Manana 		type = btrfs_file_extent_type(leaf, extent);
4246a177381SFilipe Manana 		if (type == BTRFS_FILE_EXTENT_REG ||
4256a177381SFilipe Manana 		    type == BTRFS_FILE_EXTENT_PREALLOC) {
4266a177381SFilipe Manana 			disko = btrfs_file_extent_disk_bytenr(leaf, extent);
4276a177381SFilipe Manana 			diskl = btrfs_file_extent_disk_num_bytes(leaf, extent);
4286a177381SFilipe Manana 			datao = btrfs_file_extent_offset(leaf, extent);
4296a177381SFilipe Manana 			datal = btrfs_file_extent_num_bytes(leaf, extent);
4306a177381SFilipe Manana 		} else if (type == BTRFS_FILE_EXTENT_INLINE) {
4316a177381SFilipe Manana 			/* Take upper bound, may be compressed */
4326a177381SFilipe Manana 			datal = btrfs_file_extent_ram_bytes(leaf, extent);
4336a177381SFilipe Manana 		}
4346a177381SFilipe Manana 
4356a177381SFilipe Manana 		/*
4366a177381SFilipe Manana 		 * The first search might have left us at an extent item that
4376a177381SFilipe Manana 		 * ends before our target range's start, can happen if we have
4386a177381SFilipe Manana 		 * holes and NO_HOLES feature enabled.
439d4597898SFilipe Manana 		 *
440d4597898SFilipe Manana 		 * Subsequent searches may leave us on a file range we have
441d4597898SFilipe Manana 		 * processed before - this happens due to a race with ordered
442d4597898SFilipe Manana 		 * extent completion for a file range that is outside our source
443d4597898SFilipe Manana 		 * range, but that range was part of a file extent item that
444d4597898SFilipe Manana 		 * also covered a leading part of our source range.
4456a177381SFilipe Manana 		 */
446d4597898SFilipe Manana 		if (key.offset + datal <= prev_extent_end) {
4476a177381SFilipe Manana 			path->slots[0]++;
4486a177381SFilipe Manana 			goto process_slot;
4496a177381SFilipe Manana 		} else if (key.offset >= off + len) {
4506a177381SFilipe Manana 			break;
4516a177381SFilipe Manana 		}
452d4597898SFilipe Manana 
453d4597898SFilipe Manana 		prev_extent_end = key.offset + datal;
4543212fa14SJosef Bacik 		size = btrfs_item_size(leaf, slot);
4556a177381SFilipe Manana 		read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, slot),
4566a177381SFilipe Manana 				   size);
4576a177381SFilipe Manana 
4586a177381SFilipe Manana 		btrfs_release_path(path);
4596a177381SFilipe Manana 
4606a177381SFilipe Manana 		memcpy(&new_key, &key, sizeof(new_key));
4616a177381SFilipe Manana 		new_key.objectid = btrfs_ino(BTRFS_I(inode));
4626a177381SFilipe Manana 		if (off <= key.offset)
4636a177381SFilipe Manana 			new_key.offset = key.offset + destoff - off;
4646a177381SFilipe Manana 		else
4656a177381SFilipe Manana 			new_key.offset = destoff;
4666a177381SFilipe Manana 
4676a177381SFilipe Manana 		/*
4686a177381SFilipe Manana 		 * Deal with a hole that doesn't have an extent item that
4696a177381SFilipe Manana 		 * represents it (NO_HOLES feature enabled).
4706a177381SFilipe Manana 		 * This hole is either in the middle of the cloning range or at
4716a177381SFilipe Manana 		 * the beginning (fully overlaps it or partially overlaps it).
4726a177381SFilipe Manana 		 */
4736a177381SFilipe Manana 		if (new_key.offset != last_dest_end)
4746a177381SFilipe Manana 			drop_start = last_dest_end;
4756a177381SFilipe Manana 		else
4766a177381SFilipe Manana 			drop_start = new_key.offset;
4776a177381SFilipe Manana 
4786a177381SFilipe Manana 		if (type == BTRFS_FILE_EXTENT_REG ||
4796a177381SFilipe Manana 		    type == BTRFS_FILE_EXTENT_PREALLOC) {
480bf385648SFilipe Manana 			struct btrfs_replace_extent_info clone_info;
4816a177381SFilipe Manana 
4826a177381SFilipe Manana 			/*
4836a177381SFilipe Manana 			 *    a  | --- range to clone ---|  b
4846a177381SFilipe Manana 			 * | ------------- extent ------------- |
4856a177381SFilipe Manana 			 */
4866a177381SFilipe Manana 
4876a177381SFilipe Manana 			/* Subtract range b */
4886a177381SFilipe Manana 			if (key.offset + datal > off + len)
4896a177381SFilipe Manana 				datal = off + len - key.offset;
4906a177381SFilipe Manana 
4916a177381SFilipe Manana 			/* Subtract range a */
4926a177381SFilipe Manana 			if (off > key.offset) {
4936a177381SFilipe Manana 				datao += off - key.offset;
4946a177381SFilipe Manana 				datal -= off - key.offset;
4956a177381SFilipe Manana 			}
4966a177381SFilipe Manana 
4976a177381SFilipe Manana 			clone_info.disk_offset = disko;
4986a177381SFilipe Manana 			clone_info.disk_len = diskl;
4996a177381SFilipe Manana 			clone_info.data_offset = datao;
5006a177381SFilipe Manana 			clone_info.data_len = datal;
5016a177381SFilipe Manana 			clone_info.file_offset = new_key.offset;
5026a177381SFilipe Manana 			clone_info.extent_buf = buf;
5038fccebfaSFilipe Manana 			clone_info.is_new_extent = false;
504983d8209SFilipe Manana 			clone_info.update_times = !no_time_update;
505bfc78479SNikolay Borisov 			ret = btrfs_replace_file_extents(BTRFS_I(inode), path,
506bfc78479SNikolay Borisov 					drop_start, new_key.offset + datal - 1,
507bfc78479SNikolay Borisov 					&clone_info, &trans);
5086a177381SFilipe Manana 			if (ret)
5096a177381SFilipe Manana 				goto out;
510b2d9f2dcSFilipe Manana 		} else {
511b2d9f2dcSFilipe Manana 			ASSERT(type == BTRFS_FILE_EXTENT_INLINE);
512a61e1e0dSFilipe Manana 			/*
513a61e1e0dSFilipe Manana 			 * Inline extents always have to start at file offset 0
514a61e1e0dSFilipe Manana 			 * and can never be bigger then the sector size. We can
515a61e1e0dSFilipe Manana 			 * never clone only parts of an inline extent, since all
516a61e1e0dSFilipe Manana 			 * reflink operations must start at a sector size aligned
517a61e1e0dSFilipe Manana 			 * offset, and the length must be aligned too or end at
518a61e1e0dSFilipe Manana 			 * the i_size (which implies the whole inlined data).
519a61e1e0dSFilipe Manana 			 */
520a61e1e0dSFilipe Manana 			ASSERT(key.offset == 0);
521a61e1e0dSFilipe Manana 			ASSERT(datal <= fs_info->sectorsize);
522b2d9f2dcSFilipe Manana 			if (WARN_ON(type != BTRFS_FILE_EXTENT_INLINE) ||
523b2d9f2dcSFilipe Manana 			    WARN_ON(key.offset != 0) ||
5241f4613cdSFilipe Manana 			    WARN_ON(datal > fs_info->sectorsize)) {
5251f4613cdSFilipe Manana 				ret = -EUCLEAN;
5261f4613cdSFilipe Manana 				goto out;
5271f4613cdSFilipe Manana 			}
5286a177381SFilipe Manana 
52905a5a762SFilipe Manana 			ret = clone_copy_inline_extent(inode, path, &new_key,
53005a5a762SFilipe Manana 						       drop_start, datal, size,
53105a5a762SFilipe Manana 						       comp, buf, &trans);
53205a5a762SFilipe Manana 			if (ret)
5336a177381SFilipe Manana 				goto out;
5346a177381SFilipe Manana 		}
5356a177381SFilipe Manana 
5366a177381SFilipe Manana 		btrfs_release_path(path);
5376a177381SFilipe Manana 
5383ebac17cSFilipe Manana 		/*
5397f30c072SFilipe Manana 		 * Whenever we share an extent we update the last_reflink_trans
5407f30c072SFilipe Manana 		 * of each inode to the current transaction. This is needed to
5417f30c072SFilipe Manana 		 * make sure fsync does not log multiple checksum items with
5427f30c072SFilipe Manana 		 * overlapping ranges (because some extent items might refer
5437f30c072SFilipe Manana 		 * only to sections of the original extent). For the destination
5447f30c072SFilipe Manana 		 * inode we do this regardless of the generation of the extents
5457f30c072SFilipe Manana 		 * or even if they are inline extents or explicit holes, to make
5467f30c072SFilipe Manana 		 * sure a full fsync does not skip them. For the source inode,
5477f30c072SFilipe Manana 		 * we only need to update last_reflink_trans in case it's a new
5487f30c072SFilipe Manana 		 * extent that is not a hole or an inline extent, to deal with
5497f30c072SFilipe Manana 		 * the checksums problem on fsync.
5503ebac17cSFilipe Manana 		 */
5517f30c072SFilipe Manana 		if (extent_gen == trans->transid && disko > 0)
5523ebac17cSFilipe Manana 			BTRFS_I(src)->last_reflink_trans = trans->transid;
5537f30c072SFilipe Manana 
5543ebac17cSFilipe Manana 		BTRFS_I(inode)->last_reflink_trans = trans->transid;
5553ebac17cSFilipe Manana 
5566a177381SFilipe Manana 		last_dest_end = ALIGN(new_key.offset + datal,
5576a177381SFilipe Manana 				      fs_info->sectorsize);
5586a177381SFilipe Manana 		ret = clone_finish_inode_update(trans, inode, last_dest_end,
5596a177381SFilipe Manana 						destoff, olen, no_time_update);
5606a177381SFilipe Manana 		if (ret)
5616a177381SFilipe Manana 			goto out;
5626a177381SFilipe Manana 		if (new_key.offset + datal >= destoff + len)
5636a177381SFilipe Manana 			break;
5646a177381SFilipe Manana 
5656a177381SFilipe Manana 		btrfs_release_path(path);
566d4597898SFilipe Manana 		key.offset = prev_extent_end;
5676a177381SFilipe Manana 
5686a177381SFilipe Manana 		if (fatal_signal_pending(current)) {
5696a177381SFilipe Manana 			ret = -EINTR;
5706a177381SFilipe Manana 			goto out;
5716a177381SFilipe Manana 		}
5726b613cc9SJohannes Thumshirn 
5736b613cc9SJohannes Thumshirn 		cond_resched();
5746a177381SFilipe Manana 	}
5756a177381SFilipe Manana 	ret = 0;
5766a177381SFilipe Manana 
5776a177381SFilipe Manana 	if (last_dest_end < destoff + len) {
5786a177381SFilipe Manana 		/*
5796a177381SFilipe Manana 		 * We have an implicit hole that fully or partially overlaps our
5806a177381SFilipe Manana 		 * cloning range at its end. This means that we either have the
5816a177381SFilipe Manana 		 * NO_HOLES feature enabled or the implicit hole happened due to
5826a177381SFilipe Manana 		 * mixing buffered and direct IO writes against this file.
5836a177381SFilipe Manana 		 */
5846a177381SFilipe Manana 		btrfs_release_path(path);
5856a177381SFilipe Manana 
5863660d0bcSFilipe Manana 		/*
5873660d0bcSFilipe Manana 		 * When using NO_HOLES and we are cloning a range that covers
5883660d0bcSFilipe Manana 		 * only a hole (no extents) into a range beyond the current
5893660d0bcSFilipe Manana 		 * i_size, punching a hole in the target range will not create
5903660d0bcSFilipe Manana 		 * an extent map defining a hole, because the range starts at or
5913660d0bcSFilipe Manana 		 * beyond current i_size. If the file previously had an i_size
5923660d0bcSFilipe Manana 		 * greater than the new i_size set by this clone operation, we
5933660d0bcSFilipe Manana 		 * need to make sure the next fsync is a full fsync, so that it
5943660d0bcSFilipe Manana 		 * detects and logs a hole covering a range from the current
5953660d0bcSFilipe Manana 		 * i_size to the new i_size. If the clone range covers extents,
5963660d0bcSFilipe Manana 		 * besides a hole, then we know the full sync flag was already
5973660d0bcSFilipe Manana 		 * set by previous calls to btrfs_replace_file_extents() that
5983660d0bcSFilipe Manana 		 * replaced file extent items.
5993660d0bcSFilipe Manana 		 */
6003660d0bcSFilipe Manana 		if (last_dest_end >= i_size_read(inode))
60123e3337fSFilipe Manana 			btrfs_set_inode_full_sync(BTRFS_I(inode));
6023660d0bcSFilipe Manana 
603bfc78479SNikolay Borisov 		ret = btrfs_replace_file_extents(BTRFS_I(inode), path,
604bfc78479SNikolay Borisov 				last_dest_end, destoff + len - 1, NULL, &trans);
6056a177381SFilipe Manana 		if (ret)
6066a177381SFilipe Manana 			goto out;
6076a177381SFilipe Manana 
6086a177381SFilipe Manana 		ret = clone_finish_inode_update(trans, inode, destoff + len,
6096a177381SFilipe Manana 						destoff, olen, no_time_update);
6106a177381SFilipe Manana 	}
6116a177381SFilipe Manana 
6126a177381SFilipe Manana out:
6136a177381SFilipe Manana 	btrfs_free_path(path);
6146a177381SFilipe Manana 	kvfree(buf);
6153d45f221SFilipe Manana 	clear_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &BTRFS_I(inode)->runtime_flags);
6163d45f221SFilipe Manana 
6176a177381SFilipe Manana 	return ret;
6186a177381SFilipe Manana }
6196a177381SFilipe Manana 
btrfs_double_extent_unlock(struct inode * inode1,u64 loff1,struct inode * inode2,u64 loff2,u64 len)6206a177381SFilipe Manana static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
6216a177381SFilipe Manana 				       struct inode *inode2, u64 loff2, u64 len)
6226a177381SFilipe Manana {
623570eb97bSJosef Bacik 	unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1, NULL);
624570eb97bSJosef Bacik 	unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1, NULL);
6256a177381SFilipe Manana }
6266a177381SFilipe Manana 
btrfs_double_extent_lock(struct inode * inode1,u64 loff1,struct inode * inode2,u64 loff2,u64 len)6276a177381SFilipe Manana static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
6286a177381SFilipe Manana 				     struct inode *inode2, u64 loff2, u64 len)
6296a177381SFilipe Manana {
63063c34cb4SFilipe Manana 	u64 range1_end = loff1 + len - 1;
63163c34cb4SFilipe Manana 	u64 range2_end = loff2 + len - 1;
63263c34cb4SFilipe Manana 
6336a177381SFilipe Manana 	if (inode1 < inode2) {
6346a177381SFilipe Manana 		swap(inode1, inode2);
6356a177381SFilipe Manana 		swap(loff1, loff2);
63663c34cb4SFilipe Manana 		swap(range1_end, range2_end);
6376a177381SFilipe Manana 	} else if (inode1 == inode2 && loff2 < loff1) {
6386a177381SFilipe Manana 		swap(loff1, loff2);
63963c34cb4SFilipe Manana 		swap(range1_end, range2_end);
6406a177381SFilipe Manana 	}
64163c34cb4SFilipe Manana 
642570eb97bSJosef Bacik 	lock_extent(&BTRFS_I(inode1)->io_tree, loff1, range1_end, NULL);
643570eb97bSJosef Bacik 	lock_extent(&BTRFS_I(inode2)->io_tree, loff2, range2_end, NULL);
64463c34cb4SFilipe Manana 
64563c34cb4SFilipe Manana 	btrfs_assert_inode_range_clean(BTRFS_I(inode1), loff1, range1_end);
64663c34cb4SFilipe Manana 	btrfs_assert_inode_range_clean(BTRFS_I(inode2), loff2, range2_end);
6476a177381SFilipe Manana }
6486a177381SFilipe Manana 
btrfs_double_mmap_lock(struct inode * inode1,struct inode * inode2)6498c99516aSJosef Bacik static void btrfs_double_mmap_lock(struct inode *inode1, struct inode *inode2)
6508c99516aSJosef Bacik {
6518c99516aSJosef Bacik 	if (inode1 < inode2)
6528c99516aSJosef Bacik 		swap(inode1, inode2);
6538c99516aSJosef Bacik 	down_write(&BTRFS_I(inode1)->i_mmap_lock);
6548c99516aSJosef Bacik 	down_write_nested(&BTRFS_I(inode2)->i_mmap_lock, SINGLE_DEPTH_NESTING);
6558c99516aSJosef Bacik }
6568c99516aSJosef Bacik 
btrfs_double_mmap_unlock(struct inode * inode1,struct inode * inode2)6578c99516aSJosef Bacik static void btrfs_double_mmap_unlock(struct inode *inode1, struct inode *inode2)
6588c99516aSJosef Bacik {
6598c99516aSJosef Bacik 	up_write(&BTRFS_I(inode1)->i_mmap_lock);
6608c99516aSJosef Bacik 	up_write(&BTRFS_I(inode2)->i_mmap_lock);
6618c99516aSJosef Bacik }
6628c99516aSJosef Bacik 
btrfs_extent_same_range(struct inode * src,u64 loff,u64 len,struct inode * dst,u64 dst_loff)6636a177381SFilipe Manana static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len,
6646a177381SFilipe Manana 				   struct inode *dst, u64 dst_loff)
6656a177381SFilipe Manana {
6666fe81a3aSFilipe Manana 	struct btrfs_fs_info *fs_info = BTRFS_I(src)->root->fs_info;
6676fe81a3aSFilipe Manana 	const u64 bs = fs_info->sb->s_blocksize;
6686a177381SFilipe Manana 	int ret;
6696a177381SFilipe Manana 
6706a177381SFilipe Manana 	/*
671704528d8SMatthew Wilcox (Oracle) 	 * Lock destination range to serialize with concurrent readahead() and
6726a177381SFilipe Manana 	 * source range to serialize with relocation.
6736a177381SFilipe Manana 	 */
6746a177381SFilipe Manana 	btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
6756a177381SFilipe Manana 	ret = btrfs_clone(src, dst, loff, len, ALIGN(len, bs), dst_loff, 1);
6766a177381SFilipe Manana 	btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
6776a177381SFilipe Manana 
6786fe81a3aSFilipe Manana 	btrfs_btree_balance_dirty(fs_info);
6796fe81a3aSFilipe Manana 
6806a177381SFilipe Manana 	return ret;
6816a177381SFilipe Manana }
6826a177381SFilipe Manana 
btrfs_extent_same(struct inode * src,u64 loff,u64 olen,struct inode * dst,u64 dst_loff)6836a177381SFilipe Manana static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
6846a177381SFilipe Manana 			     struct inode *dst, u64 dst_loff)
6856a177381SFilipe Manana {
68644bee215SSidong Yang 	int ret = 0;
6876a177381SFilipe Manana 	u64 i, tail_len, chunk_count;
6886a177381SFilipe Manana 	struct btrfs_root *root_dst = BTRFS_I(dst)->root;
6896a177381SFilipe Manana 
6906a177381SFilipe Manana 	spin_lock(&root_dst->root_item_lock);
6916a177381SFilipe Manana 	if (root_dst->send_in_progress) {
6926a177381SFilipe Manana 		btrfs_warn_rl(root_dst->fs_info,
6936a177381SFilipe Manana "cannot deduplicate to root %llu while send operations are using it (%d in progress)",
6946a177381SFilipe Manana 			      root_dst->root_key.objectid,
6956a177381SFilipe Manana 			      root_dst->send_in_progress);
6966a177381SFilipe Manana 		spin_unlock(&root_dst->root_item_lock);
6976a177381SFilipe Manana 		return -EAGAIN;
6986a177381SFilipe Manana 	}
6996a177381SFilipe Manana 	root_dst->dedupe_in_progress++;
7006a177381SFilipe Manana 	spin_unlock(&root_dst->root_item_lock);
7016a177381SFilipe Manana 
7026a177381SFilipe Manana 	tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
7036a177381SFilipe Manana 	chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
7046a177381SFilipe Manana 
7056a177381SFilipe Manana 	for (i = 0; i < chunk_count; i++) {
7066a177381SFilipe Manana 		ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
7076a177381SFilipe Manana 					      dst, dst_loff);
7086a177381SFilipe Manana 		if (ret)
7096a177381SFilipe Manana 			goto out;
7106a177381SFilipe Manana 
7116a177381SFilipe Manana 		loff += BTRFS_MAX_DEDUPE_LEN;
7126a177381SFilipe Manana 		dst_loff += BTRFS_MAX_DEDUPE_LEN;
7136a177381SFilipe Manana 	}
7146a177381SFilipe Manana 
7156a177381SFilipe Manana 	if (tail_len > 0)
7166a177381SFilipe Manana 		ret = btrfs_extent_same_range(src, loff, tail_len, dst, dst_loff);
7176a177381SFilipe Manana out:
7186a177381SFilipe Manana 	spin_lock(&root_dst->root_item_lock);
7196a177381SFilipe Manana 	root_dst->dedupe_in_progress--;
7206a177381SFilipe Manana 	spin_unlock(&root_dst->root_item_lock);
7216a177381SFilipe Manana 
7226a177381SFilipe Manana 	return ret;
7236a177381SFilipe Manana }
7246a177381SFilipe Manana 
btrfs_clone_files(struct file * file,struct file * file_src,u64 off,u64 olen,u64 destoff)7256a177381SFilipe Manana static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
7266a177381SFilipe Manana 					u64 off, u64 olen, u64 destoff)
7276a177381SFilipe Manana {
7286a177381SFilipe Manana 	struct inode *inode = file_inode(file);
7296a177381SFilipe Manana 	struct inode *src = file_inode(file_src);
7306a177381SFilipe Manana 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7316a177381SFilipe Manana 	int ret;
73205a5a762SFilipe Manana 	int wb_ret;
7336a177381SFilipe Manana 	u64 len = olen;
7346a177381SFilipe Manana 	u64 bs = fs_info->sb->s_blocksize;
7356a177381SFilipe Manana 
7366a177381SFilipe Manana 	/*
7376a177381SFilipe Manana 	 * VFS's generic_remap_file_range_prep() protects us from cloning the
7386a177381SFilipe Manana 	 * eof block into the middle of a file, which would result in corruption
7396a177381SFilipe Manana 	 * if the file size is not blocksize aligned. So we don't need to check
7406a177381SFilipe Manana 	 * for that case here.
7416a177381SFilipe Manana 	 */
7426a177381SFilipe Manana 	if (off + len == src->i_size)
7436a177381SFilipe Manana 		len = ALIGN(src->i_size, bs) - off;
7446a177381SFilipe Manana 
7456a177381SFilipe Manana 	if (destoff > inode->i_size) {
7466a177381SFilipe Manana 		const u64 wb_start = ALIGN_DOWN(inode->i_size, bs);
7476a177381SFilipe Manana 
748b06359a3SNikolay Borisov 		ret = btrfs_cont_expand(BTRFS_I(inode), inode->i_size, destoff);
7496a177381SFilipe Manana 		if (ret)
7506a177381SFilipe Manana 			return ret;
7516a177381SFilipe Manana 		/*
7526a177381SFilipe Manana 		 * We may have truncated the last block if the inode's size is
7536a177381SFilipe Manana 		 * not sector size aligned, so we need to wait for writeback to
7546a177381SFilipe Manana 		 * complete before proceeding further, otherwise we can race
7556a177381SFilipe Manana 		 * with cloning and attempt to increment a reference to an
7566a177381SFilipe Manana 		 * extent that no longer exists (writeback completed right after
7576a177381SFilipe Manana 		 * we found the previous extent covering eof and before we
7586a177381SFilipe Manana 		 * attempted to increment its reference count).
7596a177381SFilipe Manana 		 */
7606a177381SFilipe Manana 		ret = btrfs_wait_ordered_range(inode, wb_start,
7616a177381SFilipe Manana 					       destoff - wb_start);
7626a177381SFilipe Manana 		if (ret)
7636a177381SFilipe Manana 			return ret;
7646a177381SFilipe Manana 	}
7656a177381SFilipe Manana 
7666a177381SFilipe Manana 	/*
767704528d8SMatthew Wilcox (Oracle) 	 * Lock destination range to serialize with concurrent readahead() and
7686a177381SFilipe Manana 	 * source range to serialize with relocation.
7696a177381SFilipe Manana 	 */
7706a177381SFilipe Manana 	btrfs_double_extent_lock(src, off, inode, destoff, len);
7716a177381SFilipe Manana 	ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
7726a177381SFilipe Manana 	btrfs_double_extent_unlock(src, off, inode, destoff, len);
77305a5a762SFilipe Manana 
77405a5a762SFilipe Manana 	/*
77505a5a762SFilipe Manana 	 * We may have copied an inline extent into a page of the destination
77605a5a762SFilipe Manana 	 * range, so wait for writeback to complete before truncating pages
77705a5a762SFilipe Manana 	 * from the page cache. This is a rare case.
77805a5a762SFilipe Manana 	 */
77905a5a762SFilipe Manana 	wb_ret = btrfs_wait_ordered_range(inode, destoff, len);
78005a5a762SFilipe Manana 	ret = ret ? ret : wb_ret;
7816a177381SFilipe Manana 	/*
7826a177381SFilipe Manana 	 * Truncate page cache pages so that future reads will see the cloned
7836a177381SFilipe Manana 	 * data immediately and not the previous data.
7846a177381SFilipe Manana 	 */
7856a177381SFilipe Manana 	truncate_inode_pages_range(&inode->i_data,
7866a177381SFilipe Manana 				round_down(destoff, PAGE_SIZE),
7876a177381SFilipe Manana 				round_up(destoff + len, PAGE_SIZE) - 1);
7886a177381SFilipe Manana 
7896fe81a3aSFilipe Manana 	btrfs_btree_balance_dirty(fs_info);
7906fe81a3aSFilipe Manana 
7916a177381SFilipe Manana 	return ret;
7926a177381SFilipe Manana }
7936a177381SFilipe Manana 
btrfs_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)7946a177381SFilipe Manana static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
7956a177381SFilipe Manana 				       struct file *file_out, loff_t pos_out,
7966a177381SFilipe Manana 				       loff_t *len, unsigned int remap_flags)
7976a177381SFilipe Manana {
7986a177381SFilipe Manana 	struct inode *inode_in = file_inode(file_in);
7996a177381SFilipe Manana 	struct inode *inode_out = file_inode(file_out);
8006a177381SFilipe Manana 	u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize;
8016a177381SFilipe Manana 	u64 wb_len;
8026a177381SFilipe Manana 	int ret;
8036a177381SFilipe Manana 
8046a177381SFilipe Manana 	if (!(remap_flags & REMAP_FILE_DEDUP)) {
8056a177381SFilipe Manana 		struct btrfs_root *root_out = BTRFS_I(inode_out)->root;
8066a177381SFilipe Manana 
8076a177381SFilipe Manana 		if (btrfs_root_readonly(root_out))
8086a177381SFilipe Manana 			return -EROFS;
8096a177381SFilipe Manana 
810ae460f05SJosef Bacik 		ASSERT(inode_in->i_sb == inode_out->i_sb);
8116a177381SFilipe Manana 	}
8126a177381SFilipe Manana 
8136a177381SFilipe Manana 	/* Don't make the dst file partly checksummed */
8146a177381SFilipe Manana 	if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) !=
8156a177381SFilipe Manana 	    (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) {
8166a177381SFilipe Manana 		return -EINVAL;
8176a177381SFilipe Manana 	}
8186a177381SFilipe Manana 
8196a177381SFilipe Manana 	/*
8206a177381SFilipe Manana 	 * Now that the inodes are locked, we need to start writeback ourselves
8216a177381SFilipe Manana 	 * and can not rely on the writeback from the VFS's generic helper
8226a177381SFilipe Manana 	 * generic_remap_file_range_prep() because:
8236a177381SFilipe Manana 	 *
8246a177381SFilipe Manana 	 * 1) For compression we must call filemap_fdatawrite_range() range
8256a177381SFilipe Manana 	 *    twice (btrfs_fdatawrite_range() does it for us), and the generic
8266a177381SFilipe Manana 	 *    helper only calls it once;
8276a177381SFilipe Manana 	 *
8286a177381SFilipe Manana 	 * 2) filemap_fdatawrite_range(), called by the generic helper only
8296a177381SFilipe Manana 	 *    waits for the writeback to complete, i.e. for IO to be done, and
8306a177381SFilipe Manana 	 *    not for the ordered extents to complete. We need to wait for them
8316a177381SFilipe Manana 	 *    to complete so that new file extent items are in the fs tree.
8326a177381SFilipe Manana 	 */
8336a177381SFilipe Manana 	if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP))
8346a177381SFilipe Manana 		wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs);
8356a177381SFilipe Manana 	else
8366a177381SFilipe Manana 		wb_len = ALIGN(*len, bs);
8376a177381SFilipe Manana 
8386a177381SFilipe Manana 	/*
8396a177381SFilipe Manana 	 * Workaround to make sure NOCOW buffered write reach disk as NOCOW.
8406a177381SFilipe Manana 	 *
8416a177381SFilipe Manana 	 * Btrfs' back references do not have a block level granularity, they
8426a177381SFilipe Manana 	 * work at the whole extent level.
8436a177381SFilipe Manana 	 * NOCOW buffered write without data space reserved may not be able
8446a177381SFilipe Manana 	 * to fall back to CoW due to lack of data space, thus could cause
8456a177381SFilipe Manana 	 * data loss.
8466a177381SFilipe Manana 	 *
8476a177381SFilipe Manana 	 * Here we take a shortcut by flushing the whole inode, so that all
8486a177381SFilipe Manana 	 * nocow write should reach disk as nocow before we increase the
8496a177381SFilipe Manana 	 * reference of the extent. We could do better by only flushing NOCOW
8506a177381SFilipe Manana 	 * data, but that needs extra accounting.
8516a177381SFilipe Manana 	 *
8526a177381SFilipe Manana 	 * Also we don't need to check ASYNC_EXTENT, as async extent will be
8536a177381SFilipe Manana 	 * CoWed anyway, not affecting nocow part.
8546a177381SFilipe Manana 	 */
8556a177381SFilipe Manana 	ret = filemap_flush(inode_in->i_mapping);
8566a177381SFilipe Manana 	if (ret < 0)
8576a177381SFilipe Manana 		return ret;
8586a177381SFilipe Manana 
8596a177381SFilipe Manana 	ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs),
8606a177381SFilipe Manana 				       wb_len);
8616a177381SFilipe Manana 	if (ret < 0)
8626a177381SFilipe Manana 		return ret;
8636a177381SFilipe Manana 	ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs),
8646a177381SFilipe Manana 				       wb_len);
8656a177381SFilipe Manana 	if (ret < 0)
8666a177381SFilipe Manana 		return ret;
8676a177381SFilipe Manana 
8686a177381SFilipe Manana 	return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
8696a177381SFilipe Manana 					    len, remap_flags);
8706a177381SFilipe Manana }
8716a177381SFilipe Manana 
file_sync_write(const struct file * file)872b7a7a834SFilipe Manana static bool file_sync_write(const struct file *file)
873b7a7a834SFilipe Manana {
874b7a7a834SFilipe Manana 	if (file->f_flags & (__O_SYNC | O_DSYNC))
875b7a7a834SFilipe Manana 		return true;
876b7a7a834SFilipe Manana 	if (IS_SYNC(file_inode(file)))
877b7a7a834SFilipe Manana 		return true;
878b7a7a834SFilipe Manana 
879b7a7a834SFilipe Manana 	return false;
880b7a7a834SFilipe Manana }
881b7a7a834SFilipe Manana 
btrfs_remap_file_range(struct file * src_file,loff_t off,struct file * dst_file,loff_t destoff,loff_t len,unsigned int remap_flags)8826a177381SFilipe Manana loff_t btrfs_remap_file_range(struct file *src_file, loff_t off,
8836a177381SFilipe Manana 		struct file *dst_file, loff_t destoff, loff_t len,
8846a177381SFilipe Manana 		unsigned int remap_flags)
8856a177381SFilipe Manana {
8866a177381SFilipe Manana 	struct inode *src_inode = file_inode(src_file);
8876a177381SFilipe Manana 	struct inode *dst_inode = file_inode(dst_file);
8886a177381SFilipe Manana 	bool same_inode = dst_inode == src_inode;
8896a177381SFilipe Manana 	int ret;
8906a177381SFilipe Manana 
8916a177381SFilipe Manana 	if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
8926a177381SFilipe Manana 		return -EINVAL;
8936a177381SFilipe Manana 
8948c99516aSJosef Bacik 	if (same_inode) {
89529b6352bSDavid Sterba 		btrfs_inode_lock(BTRFS_I(src_inode), BTRFS_ILOCK_MMAP);
8968c99516aSJosef Bacik 	} else {
8976a177381SFilipe Manana 		lock_two_nondirectories(src_inode, dst_inode);
8988c99516aSJosef Bacik 		btrfs_double_mmap_lock(src_inode, dst_inode);
8998c99516aSJosef Bacik 	}
9006a177381SFilipe Manana 
9016a177381SFilipe Manana 	ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff,
9026a177381SFilipe Manana 					  &len, remap_flags);
9036a177381SFilipe Manana 	if (ret < 0 || len == 0)
9046a177381SFilipe Manana 		goto out_unlock;
9056a177381SFilipe Manana 
9066a177381SFilipe Manana 	if (remap_flags & REMAP_FILE_DEDUP)
9076a177381SFilipe Manana 		ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff);
9086a177381SFilipe Manana 	else
9096a177381SFilipe Manana 		ret = btrfs_clone_files(dst_file, src_file, off, len, destoff);
9106a177381SFilipe Manana 
9116a177381SFilipe Manana out_unlock:
9128c99516aSJosef Bacik 	if (same_inode) {
913e5d4d75bSDavid Sterba 		btrfs_inode_unlock(BTRFS_I(src_inode), BTRFS_ILOCK_MMAP);
9148c99516aSJosef Bacik 	} else {
9158c99516aSJosef Bacik 		btrfs_double_mmap_unlock(src_inode, dst_inode);
9166a177381SFilipe Manana 		unlock_two_nondirectories(src_inode, dst_inode);
9178c99516aSJosef Bacik 	}
9186a177381SFilipe Manana 
919b7a7a834SFilipe Manana 	/*
920b7a7a834SFilipe Manana 	 * If either the source or the destination file was opened with O_SYNC,
921b7a7a834SFilipe Manana 	 * O_DSYNC or has the S_SYNC attribute, fsync both the destination and
922b7a7a834SFilipe Manana 	 * source files/ranges, so that after a successful return (0) followed
923b7a7a834SFilipe Manana 	 * by a power failure results in the reflinked data to be readable from
924b7a7a834SFilipe Manana 	 * both files/ranges.
925b7a7a834SFilipe Manana 	 */
926b7a7a834SFilipe Manana 	if (ret == 0 && len > 0 &&
927b7a7a834SFilipe Manana 	    (file_sync_write(src_file) || file_sync_write(dst_file))) {
928b7a7a834SFilipe Manana 		ret = btrfs_sync_file(src_file, off, off + len - 1, 0);
929b7a7a834SFilipe Manana 		if (ret == 0)
930b7a7a834SFilipe Manana 			ret = btrfs_sync_file(dst_file, destoff,
931b7a7a834SFilipe Manana 					      destoff + len - 1, 0);
932b7a7a834SFilipe Manana 	}
933b7a7a834SFilipe Manana 
9346a177381SFilipe Manana 	return ret < 0 ? ret : len;
9356a177381SFilipe Manana }
936