18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci#include <linux/blkdev.h>
48c2ecf20Sopenharmony_ci#include <linux/iversion.h>
58c2ecf20Sopenharmony_ci#include "compression.h"
68c2ecf20Sopenharmony_ci#include "ctree.h"
78c2ecf20Sopenharmony_ci#include "delalloc-space.h"
88c2ecf20Sopenharmony_ci#include "reflink.h"
98c2ecf20Sopenharmony_ci#include "transaction.h"
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#define BTRFS_MAX_DEDUPE_LEN	SZ_16M
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistatic int clone_finish_inode_update(struct btrfs_trans_handle *trans,
148c2ecf20Sopenharmony_ci				     struct inode *inode,
158c2ecf20Sopenharmony_ci				     u64 endoff,
168c2ecf20Sopenharmony_ci				     const u64 destoff,
178c2ecf20Sopenharmony_ci				     const u64 olen,
188c2ecf20Sopenharmony_ci				     int no_time_update)
198c2ecf20Sopenharmony_ci{
208c2ecf20Sopenharmony_ci	struct btrfs_root *root = BTRFS_I(inode)->root;
218c2ecf20Sopenharmony_ci	int ret;
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	inode_inc_iversion(inode);
248c2ecf20Sopenharmony_ci	if (!no_time_update)
258c2ecf20Sopenharmony_ci		inode->i_mtime = inode->i_ctime = current_time(inode);
268c2ecf20Sopenharmony_ci	/*
278c2ecf20Sopenharmony_ci	 * We round up to the block size at eof when determining which
288c2ecf20Sopenharmony_ci	 * extents to clone above, but shouldn't round up the file size.
298c2ecf20Sopenharmony_ci	 */
308c2ecf20Sopenharmony_ci	if (endoff > destoff + olen)
318c2ecf20Sopenharmony_ci		endoff = destoff + olen;
328c2ecf20Sopenharmony_ci	if (endoff > inode->i_size) {
338c2ecf20Sopenharmony_ci		i_size_write(inode, endoff);
348c2ecf20Sopenharmony_ci		btrfs_inode_safe_disk_i_size_write(inode, 0);
358c2ecf20Sopenharmony_ci	}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	ret = btrfs_update_inode(trans, root, inode);
388c2ecf20Sopenharmony_ci	if (ret) {
398c2ecf20Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
408c2ecf20Sopenharmony_ci		btrfs_end_transaction(trans);
418c2ecf20Sopenharmony_ci		goto out;
428c2ecf20Sopenharmony_ci	}
438c2ecf20Sopenharmony_ci	ret = btrfs_end_transaction(trans);
448c2ecf20Sopenharmony_ciout:
458c2ecf20Sopenharmony_ci	return ret;
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic int copy_inline_to_page(struct btrfs_inode *inode,
498c2ecf20Sopenharmony_ci			       const u64 file_offset,
508c2ecf20Sopenharmony_ci			       char *inline_data,
518c2ecf20Sopenharmony_ci			       const u64 size,
528c2ecf20Sopenharmony_ci			       const u64 datal,
538c2ecf20Sopenharmony_ci			       const u8 comp_type)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	const u64 block_size = btrfs_inode_sectorsize(inode);
568c2ecf20Sopenharmony_ci	const u64 range_end = file_offset + block_size - 1;
578c2ecf20Sopenharmony_ci	const size_t inline_size = size - btrfs_file_extent_calc_inline_size(0);
588c2ecf20Sopenharmony_ci	char *data_start = inline_data + btrfs_file_extent_calc_inline_size(0);
598c2ecf20Sopenharmony_ci	struct extent_changeset *data_reserved = NULL;
608c2ecf20Sopenharmony_ci	struct page *page = NULL;
618c2ecf20Sopenharmony_ci	struct address_space *mapping = inode->vfs_inode.i_mapping;
628c2ecf20Sopenharmony_ci	int ret;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	ASSERT(IS_ALIGNED(file_offset, block_size));
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	/*
678c2ecf20Sopenharmony_ci	 * We have flushed and locked the ranges of the source and destination
688c2ecf20Sopenharmony_ci	 * inodes, we also have locked the inodes, so we are safe to do a
698c2ecf20Sopenharmony_ci	 * reservation here. Also we must not do the reservation while holding
708c2ecf20Sopenharmony_ci	 * a transaction open, otherwise we would deadlock.
718c2ecf20Sopenharmony_ci	 */
728c2ecf20Sopenharmony_ci	ret = btrfs_delalloc_reserve_space(inode, &data_reserved, file_offset,
738c2ecf20Sopenharmony_ci					   block_size);
748c2ecf20Sopenharmony_ci	if (ret)
758c2ecf20Sopenharmony_ci		goto out;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	page = find_or_create_page(mapping, file_offset >> PAGE_SHIFT,
788c2ecf20Sopenharmony_ci				   btrfs_alloc_write_mask(mapping));
798c2ecf20Sopenharmony_ci	if (!page) {
808c2ecf20Sopenharmony_ci		ret = -ENOMEM;
818c2ecf20Sopenharmony_ci		goto out_unlock;
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	set_page_extent_mapped(page);
858c2ecf20Sopenharmony_ci	clear_extent_bit(&inode->io_tree, file_offset, range_end,
868c2ecf20Sopenharmony_ci			 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
878c2ecf20Sopenharmony_ci			 0, 0, NULL);
888c2ecf20Sopenharmony_ci	ret = btrfs_set_extent_delalloc(inode, file_offset, range_end, 0, NULL);
898c2ecf20Sopenharmony_ci	if (ret)
908c2ecf20Sopenharmony_ci		goto out_unlock;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	/*
938c2ecf20Sopenharmony_ci	 * After dirtying the page our caller will need to start a transaction,
948c2ecf20Sopenharmony_ci	 * and if we are low on metadata free space, that can cause flushing of
958c2ecf20Sopenharmony_ci	 * delalloc for all inodes in order to get metadata space released.
968c2ecf20Sopenharmony_ci	 * However we are holding the range locked for the whole duration of
978c2ecf20Sopenharmony_ci	 * the clone/dedupe operation, so we may deadlock if that happens and no
988c2ecf20Sopenharmony_ci	 * other task releases enough space. So mark this inode as not being
998c2ecf20Sopenharmony_ci	 * possible to flush to avoid such deadlock. We will clear that flag
1008c2ecf20Sopenharmony_ci	 * when we finish cloning all extents, since a transaction is started
1018c2ecf20Sopenharmony_ci	 * after finding each extent to clone.
1028c2ecf20Sopenharmony_ci	 */
1038c2ecf20Sopenharmony_ci	set_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &inode->runtime_flags);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	if (comp_type == BTRFS_COMPRESS_NONE) {
1068c2ecf20Sopenharmony_ci		char *map;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci		map = kmap(page);
1098c2ecf20Sopenharmony_ci		memcpy(map, data_start, datal);
1108c2ecf20Sopenharmony_ci		flush_dcache_page(page);
1118c2ecf20Sopenharmony_ci		kunmap(page);
1128c2ecf20Sopenharmony_ci	} else {
1138c2ecf20Sopenharmony_ci		ret = btrfs_decompress(comp_type, data_start, page, 0,
1148c2ecf20Sopenharmony_ci				       inline_size, datal);
1158c2ecf20Sopenharmony_ci		if (ret)
1168c2ecf20Sopenharmony_ci			goto out_unlock;
1178c2ecf20Sopenharmony_ci		flush_dcache_page(page);
1188c2ecf20Sopenharmony_ci	}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/*
1218c2ecf20Sopenharmony_ci	 * If our inline data is smaller then the block/page size, then the
1228c2ecf20Sopenharmony_ci	 * remaining of the block/page is equivalent to zeroes. We had something
1238c2ecf20Sopenharmony_ci	 * like the following done:
1248c2ecf20Sopenharmony_ci	 *
1258c2ecf20Sopenharmony_ci	 * $ xfs_io -f -c "pwrite -S 0xab 0 500" file
1268c2ecf20Sopenharmony_ci	 * $ sync  # (or fsync)
1278c2ecf20Sopenharmony_ci	 * $ xfs_io -c "falloc 0 4K" file
1288c2ecf20Sopenharmony_ci	 * $ xfs_io -c "pwrite -S 0xcd 4K 4K"
1298c2ecf20Sopenharmony_ci	 *
1308c2ecf20Sopenharmony_ci	 * So what's in the range [500, 4095] corresponds to zeroes.
1318c2ecf20Sopenharmony_ci	 */
1328c2ecf20Sopenharmony_ci	if (datal < block_size) {
1338c2ecf20Sopenharmony_ci		char *map;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci		map = kmap(page);
1368c2ecf20Sopenharmony_ci		memset(map + datal, 0, block_size - datal);
1378c2ecf20Sopenharmony_ci		flush_dcache_page(page);
1388c2ecf20Sopenharmony_ci		kunmap(page);
1398c2ecf20Sopenharmony_ci	}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	SetPageUptodate(page);
1428c2ecf20Sopenharmony_ci	ClearPageChecked(page);
1438c2ecf20Sopenharmony_ci	set_page_dirty(page);
1448c2ecf20Sopenharmony_ciout_unlock:
1458c2ecf20Sopenharmony_ci	if (page) {
1468c2ecf20Sopenharmony_ci		unlock_page(page);
1478c2ecf20Sopenharmony_ci		put_page(page);
1488c2ecf20Sopenharmony_ci	}
1498c2ecf20Sopenharmony_ci	if (ret)
1508c2ecf20Sopenharmony_ci		btrfs_delalloc_release_space(inode, data_reserved, file_offset,
1518c2ecf20Sopenharmony_ci					     block_size, true);
1528c2ecf20Sopenharmony_ci	btrfs_delalloc_release_extents(inode, block_size);
1538c2ecf20Sopenharmony_ciout:
1548c2ecf20Sopenharmony_ci	extent_changeset_free(data_reserved);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	return ret;
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/*
1608c2ecf20Sopenharmony_ci * Deal with cloning of inline extents. We try to copy the inline extent from
1618c2ecf20Sopenharmony_ci * the source inode to destination inode when possible. When not possible we
1628c2ecf20Sopenharmony_ci * copy the inline extent's data into the respective page of the inode.
1638c2ecf20Sopenharmony_ci */
1648c2ecf20Sopenharmony_cistatic int clone_copy_inline_extent(struct inode *dst,
1658c2ecf20Sopenharmony_ci				    struct btrfs_path *path,
1668c2ecf20Sopenharmony_ci				    struct btrfs_key *new_key,
1678c2ecf20Sopenharmony_ci				    const u64 drop_start,
1688c2ecf20Sopenharmony_ci				    const u64 datal,
1698c2ecf20Sopenharmony_ci				    const u64 size,
1708c2ecf20Sopenharmony_ci				    const u8 comp_type,
1718c2ecf20Sopenharmony_ci				    char *inline_data,
1728c2ecf20Sopenharmony_ci				    struct btrfs_trans_handle **trans_out)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
1758c2ecf20Sopenharmony_ci	struct btrfs_root *root = BTRFS_I(dst)->root;
1768c2ecf20Sopenharmony_ci	const u64 aligned_end = ALIGN(new_key->offset + datal,
1778c2ecf20Sopenharmony_ci				      fs_info->sectorsize);
1788c2ecf20Sopenharmony_ci	struct btrfs_trans_handle *trans = NULL;
1798c2ecf20Sopenharmony_ci	int ret;
1808c2ecf20Sopenharmony_ci	struct btrfs_key key;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	if (new_key->offset > 0) {
1838c2ecf20Sopenharmony_ci		ret = copy_inline_to_page(BTRFS_I(dst), new_key->offset,
1848c2ecf20Sopenharmony_ci					  inline_data, size, datal, comp_type);
1858c2ecf20Sopenharmony_ci		goto out;
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	key.objectid = btrfs_ino(BTRFS_I(dst));
1898c2ecf20Sopenharmony_ci	key.type = BTRFS_EXTENT_DATA_KEY;
1908c2ecf20Sopenharmony_ci	key.offset = 0;
1918c2ecf20Sopenharmony_ci	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1928c2ecf20Sopenharmony_ci	if (ret < 0) {
1938c2ecf20Sopenharmony_ci		return ret;
1948c2ecf20Sopenharmony_ci	} else if (ret > 0) {
1958c2ecf20Sopenharmony_ci		if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
1968c2ecf20Sopenharmony_ci			ret = btrfs_next_leaf(root, path);
1978c2ecf20Sopenharmony_ci			if (ret < 0)
1988c2ecf20Sopenharmony_ci				return ret;
1998c2ecf20Sopenharmony_ci			else if (ret > 0)
2008c2ecf20Sopenharmony_ci				goto copy_inline_extent;
2018c2ecf20Sopenharmony_ci		}
2028c2ecf20Sopenharmony_ci		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2038c2ecf20Sopenharmony_ci		if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
2048c2ecf20Sopenharmony_ci		    key.type == BTRFS_EXTENT_DATA_KEY) {
2058c2ecf20Sopenharmony_ci			/*
2068c2ecf20Sopenharmony_ci			 * There's an implicit hole at file offset 0, copy the
2078c2ecf20Sopenharmony_ci			 * inline extent's data to the page.
2088c2ecf20Sopenharmony_ci			 */
2098c2ecf20Sopenharmony_ci			ASSERT(key.offset > 0);
2108c2ecf20Sopenharmony_ci			goto copy_to_page;
2118c2ecf20Sopenharmony_ci		}
2128c2ecf20Sopenharmony_ci	} else if (i_size_read(dst) <= datal) {
2138c2ecf20Sopenharmony_ci		struct btrfs_file_extent_item *ei;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci		ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
2168c2ecf20Sopenharmony_ci				    struct btrfs_file_extent_item);
2178c2ecf20Sopenharmony_ci		/*
2188c2ecf20Sopenharmony_ci		 * If it's an inline extent replace it with the source inline
2198c2ecf20Sopenharmony_ci		 * extent, otherwise copy the source inline extent data into
2208c2ecf20Sopenharmony_ci		 * the respective page at the destination inode.
2218c2ecf20Sopenharmony_ci		 */
2228c2ecf20Sopenharmony_ci		if (btrfs_file_extent_type(path->nodes[0], ei) ==
2238c2ecf20Sopenharmony_ci		    BTRFS_FILE_EXTENT_INLINE)
2248c2ecf20Sopenharmony_ci			goto copy_inline_extent;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci		goto copy_to_page;
2278c2ecf20Sopenharmony_ci	}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_cicopy_inline_extent:
2308c2ecf20Sopenharmony_ci	/*
2318c2ecf20Sopenharmony_ci	 * We have no extent items, or we have an extent at offset 0 which may
2328c2ecf20Sopenharmony_ci	 * or may not be inlined. All these cases are dealt the same way.
2338c2ecf20Sopenharmony_ci	 */
2348c2ecf20Sopenharmony_ci	if (i_size_read(dst) > datal) {
2358c2ecf20Sopenharmony_ci		/*
2368c2ecf20Sopenharmony_ci		 * At the destination offset 0 we have either a hole, a regular
2378c2ecf20Sopenharmony_ci		 * extent or an inline extent larger then the one we want to
2388c2ecf20Sopenharmony_ci		 * clone. Deal with all these cases by copying the inline extent
2398c2ecf20Sopenharmony_ci		 * data into the respective page at the destination inode.
2408c2ecf20Sopenharmony_ci		 */
2418c2ecf20Sopenharmony_ci		goto copy_to_page;
2428c2ecf20Sopenharmony_ci	}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	/*
2458c2ecf20Sopenharmony_ci	 * Release path before starting a new transaction so we don't hold locks
2468c2ecf20Sopenharmony_ci	 * that would confuse lockdep.
2478c2ecf20Sopenharmony_ci	 */
2488c2ecf20Sopenharmony_ci	btrfs_release_path(path);
2498c2ecf20Sopenharmony_ci	/*
2508c2ecf20Sopenharmony_ci	 * If we end up here it means were copy the inline extent into a leaf
2518c2ecf20Sopenharmony_ci	 * of the destination inode. We know we will drop or adjust at most one
2528c2ecf20Sopenharmony_ci	 * extent item in the destination root.
2538c2ecf20Sopenharmony_ci	 *
2548c2ecf20Sopenharmony_ci	 * 1 unit - adjusting old extent (we may have to split it)
2558c2ecf20Sopenharmony_ci	 * 1 unit - add new extent
2568c2ecf20Sopenharmony_ci	 * 1 unit - inode update
2578c2ecf20Sopenharmony_ci	 */
2588c2ecf20Sopenharmony_ci	trans = btrfs_start_transaction(root, 3);
2598c2ecf20Sopenharmony_ci	if (IS_ERR(trans)) {
2608c2ecf20Sopenharmony_ci		ret = PTR_ERR(trans);
2618c2ecf20Sopenharmony_ci		trans = NULL;
2628c2ecf20Sopenharmony_ci		goto out;
2638c2ecf20Sopenharmony_ci	}
2648c2ecf20Sopenharmony_ci	ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
2658c2ecf20Sopenharmony_ci	if (ret)
2668c2ecf20Sopenharmony_ci		goto out;
2678c2ecf20Sopenharmony_ci	ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
2688c2ecf20Sopenharmony_ci	if (ret)
2698c2ecf20Sopenharmony_ci		goto out;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	write_extent_buffer(path->nodes[0], inline_data,
2728c2ecf20Sopenharmony_ci			    btrfs_item_ptr_offset(path->nodes[0],
2738c2ecf20Sopenharmony_ci						  path->slots[0]),
2748c2ecf20Sopenharmony_ci			    size);
2758c2ecf20Sopenharmony_ci	inode_add_bytes(dst, datal);
2768c2ecf20Sopenharmony_ci	set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(dst)->runtime_flags);
2778c2ecf20Sopenharmony_ci	ret = btrfs_inode_set_file_extent_range(BTRFS_I(dst), 0, aligned_end);
2788c2ecf20Sopenharmony_ciout:
2798c2ecf20Sopenharmony_ci	if (!ret && !trans) {
2808c2ecf20Sopenharmony_ci		/*
2818c2ecf20Sopenharmony_ci		 * No transaction here means we copied the inline extent into a
2828c2ecf20Sopenharmony_ci		 * page of the destination inode.
2838c2ecf20Sopenharmony_ci		 *
2848c2ecf20Sopenharmony_ci		 * 1 unit to update inode item
2858c2ecf20Sopenharmony_ci		 */
2868c2ecf20Sopenharmony_ci		trans = btrfs_start_transaction(root, 1);
2878c2ecf20Sopenharmony_ci		if (IS_ERR(trans)) {
2888c2ecf20Sopenharmony_ci			ret = PTR_ERR(trans);
2898c2ecf20Sopenharmony_ci			trans = NULL;
2908c2ecf20Sopenharmony_ci		}
2918c2ecf20Sopenharmony_ci	}
2928c2ecf20Sopenharmony_ci	if (ret && trans) {
2938c2ecf20Sopenharmony_ci		btrfs_abort_transaction(trans, ret);
2948c2ecf20Sopenharmony_ci		btrfs_end_transaction(trans);
2958c2ecf20Sopenharmony_ci	}
2968c2ecf20Sopenharmony_ci	if (!ret)
2978c2ecf20Sopenharmony_ci		*trans_out = trans;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	return ret;
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cicopy_to_page:
3028c2ecf20Sopenharmony_ci	/*
3038c2ecf20Sopenharmony_ci	 * Release our path because we don't need it anymore and also because
3048c2ecf20Sopenharmony_ci	 * copy_inline_to_page() needs to reserve data and metadata, which may
3058c2ecf20Sopenharmony_ci	 * need to flush delalloc when we are low on available space and
3068c2ecf20Sopenharmony_ci	 * therefore cause a deadlock if writeback of an inline extent needs to
3078c2ecf20Sopenharmony_ci	 * write to the same leaf or an ordered extent completion needs to write
3088c2ecf20Sopenharmony_ci	 * to the same leaf.
3098c2ecf20Sopenharmony_ci	 */
3108c2ecf20Sopenharmony_ci	btrfs_release_path(path);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	ret = copy_inline_to_page(BTRFS_I(dst), new_key->offset,
3138c2ecf20Sopenharmony_ci				  inline_data, size, datal, comp_type);
3148c2ecf20Sopenharmony_ci	goto out;
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci/**
3188c2ecf20Sopenharmony_ci * btrfs_clone() - clone a range from inode file to another
3198c2ecf20Sopenharmony_ci *
3208c2ecf20Sopenharmony_ci * @src: Inode to clone from
3218c2ecf20Sopenharmony_ci * @inode: Inode to clone to
3228c2ecf20Sopenharmony_ci * @off: Offset within source to start clone from
3238c2ecf20Sopenharmony_ci * @olen: Original length, passed by user, of range to clone
3248c2ecf20Sopenharmony_ci * @olen_aligned: Block-aligned value of olen
3258c2ecf20Sopenharmony_ci * @destoff: Offset within @inode to start clone
3268c2ecf20Sopenharmony_ci * @no_time_update: Whether to update mtime/ctime on the target inode
3278c2ecf20Sopenharmony_ci */
3288c2ecf20Sopenharmony_cistatic int btrfs_clone(struct inode *src, struct inode *inode,
3298c2ecf20Sopenharmony_ci		       const u64 off, const u64 olen, const u64 olen_aligned,
3308c2ecf20Sopenharmony_ci		       const u64 destoff, int no_time_update)
3318c2ecf20Sopenharmony_ci{
3328c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3338c2ecf20Sopenharmony_ci	struct btrfs_path *path = NULL;
3348c2ecf20Sopenharmony_ci	struct extent_buffer *leaf;
3358c2ecf20Sopenharmony_ci	struct btrfs_trans_handle *trans;
3368c2ecf20Sopenharmony_ci	char *buf = NULL;
3378c2ecf20Sopenharmony_ci	struct btrfs_key key;
3388c2ecf20Sopenharmony_ci	u32 nritems;
3398c2ecf20Sopenharmony_ci	int slot;
3408c2ecf20Sopenharmony_ci	int ret;
3418c2ecf20Sopenharmony_ci	const u64 len = olen_aligned;
3428c2ecf20Sopenharmony_ci	u64 last_dest_end = destoff;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	ret = -ENOMEM;
3458c2ecf20Sopenharmony_ci	buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3468c2ecf20Sopenharmony_ci	if (!buf)
3478c2ecf20Sopenharmony_ci		return ret;
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	path = btrfs_alloc_path();
3508c2ecf20Sopenharmony_ci	if (!path) {
3518c2ecf20Sopenharmony_ci		kvfree(buf);
3528c2ecf20Sopenharmony_ci		return ret;
3538c2ecf20Sopenharmony_ci	}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	path->reada = READA_FORWARD;
3568c2ecf20Sopenharmony_ci	/* Clone data */
3578c2ecf20Sopenharmony_ci	key.objectid = btrfs_ino(BTRFS_I(src));
3588c2ecf20Sopenharmony_ci	key.type = BTRFS_EXTENT_DATA_KEY;
3598c2ecf20Sopenharmony_ci	key.offset = off;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	while (1) {
3628c2ecf20Sopenharmony_ci		u64 next_key_min_offset = key.offset + 1;
3638c2ecf20Sopenharmony_ci		struct btrfs_file_extent_item *extent;
3648c2ecf20Sopenharmony_ci		u64 extent_gen;
3658c2ecf20Sopenharmony_ci		int type;
3668c2ecf20Sopenharmony_ci		u32 size;
3678c2ecf20Sopenharmony_ci		struct btrfs_key new_key;
3688c2ecf20Sopenharmony_ci		u64 disko = 0, diskl = 0;
3698c2ecf20Sopenharmony_ci		u64 datao = 0, datal = 0;
3708c2ecf20Sopenharmony_ci		u8 comp;
3718c2ecf20Sopenharmony_ci		u64 drop_start;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci		/* Note the key will change type as we walk through the tree */
3748c2ecf20Sopenharmony_ci		path->leave_spinning = 1;
3758c2ecf20Sopenharmony_ci		ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3768c2ecf20Sopenharmony_ci				0, 0);
3778c2ecf20Sopenharmony_ci		if (ret < 0)
3788c2ecf20Sopenharmony_ci			goto out;
3798c2ecf20Sopenharmony_ci		/*
3808c2ecf20Sopenharmony_ci		 * First search, if no extent item that starts at offset off was
3818c2ecf20Sopenharmony_ci		 * found but the previous item is an extent item, it's possible
3828c2ecf20Sopenharmony_ci		 * it might overlap our target range, therefore process it.
3838c2ecf20Sopenharmony_ci		 */
3848c2ecf20Sopenharmony_ci		if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3858c2ecf20Sopenharmony_ci			btrfs_item_key_to_cpu(path->nodes[0], &key,
3868c2ecf20Sopenharmony_ci					      path->slots[0] - 1);
3878c2ecf20Sopenharmony_ci			if (key.type == BTRFS_EXTENT_DATA_KEY)
3888c2ecf20Sopenharmony_ci				path->slots[0]--;
3898c2ecf20Sopenharmony_ci		}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci		nritems = btrfs_header_nritems(path->nodes[0]);
3928c2ecf20Sopenharmony_ciprocess_slot:
3938c2ecf20Sopenharmony_ci		if (path->slots[0] >= nritems) {
3948c2ecf20Sopenharmony_ci			ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
3958c2ecf20Sopenharmony_ci			if (ret < 0)
3968c2ecf20Sopenharmony_ci				goto out;
3978c2ecf20Sopenharmony_ci			if (ret > 0)
3988c2ecf20Sopenharmony_ci				break;
3998c2ecf20Sopenharmony_ci			nritems = btrfs_header_nritems(path->nodes[0]);
4008c2ecf20Sopenharmony_ci		}
4018c2ecf20Sopenharmony_ci		leaf = path->nodes[0];
4028c2ecf20Sopenharmony_ci		slot = path->slots[0];
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci		btrfs_item_key_to_cpu(leaf, &key, slot);
4058c2ecf20Sopenharmony_ci		if (key.type > BTRFS_EXTENT_DATA_KEY ||
4068c2ecf20Sopenharmony_ci		    key.objectid != btrfs_ino(BTRFS_I(src)))
4078c2ecf20Sopenharmony_ci			break;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci		ASSERT(key.type == BTRFS_EXTENT_DATA_KEY);
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci		extent = btrfs_item_ptr(leaf, slot,
4128c2ecf20Sopenharmony_ci					struct btrfs_file_extent_item);
4138c2ecf20Sopenharmony_ci		extent_gen = btrfs_file_extent_generation(leaf, extent);
4148c2ecf20Sopenharmony_ci		comp = btrfs_file_extent_compression(leaf, extent);
4158c2ecf20Sopenharmony_ci		type = btrfs_file_extent_type(leaf, extent);
4168c2ecf20Sopenharmony_ci		if (type == BTRFS_FILE_EXTENT_REG ||
4178c2ecf20Sopenharmony_ci		    type == BTRFS_FILE_EXTENT_PREALLOC) {
4188c2ecf20Sopenharmony_ci			disko = btrfs_file_extent_disk_bytenr(leaf, extent);
4198c2ecf20Sopenharmony_ci			diskl = btrfs_file_extent_disk_num_bytes(leaf, extent);
4208c2ecf20Sopenharmony_ci			datao = btrfs_file_extent_offset(leaf, extent);
4218c2ecf20Sopenharmony_ci			datal = btrfs_file_extent_num_bytes(leaf, extent);
4228c2ecf20Sopenharmony_ci		} else if (type == BTRFS_FILE_EXTENT_INLINE) {
4238c2ecf20Sopenharmony_ci			/* Take upper bound, may be compressed */
4248c2ecf20Sopenharmony_ci			datal = btrfs_file_extent_ram_bytes(leaf, extent);
4258c2ecf20Sopenharmony_ci		}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci		/*
4288c2ecf20Sopenharmony_ci		 * The first search might have left us at an extent item that
4298c2ecf20Sopenharmony_ci		 * ends before our target range's start, can happen if we have
4308c2ecf20Sopenharmony_ci		 * holes and NO_HOLES feature enabled.
4318c2ecf20Sopenharmony_ci		 */
4328c2ecf20Sopenharmony_ci		if (key.offset + datal <= off) {
4338c2ecf20Sopenharmony_ci			path->slots[0]++;
4348c2ecf20Sopenharmony_ci			goto process_slot;
4358c2ecf20Sopenharmony_ci		} else if (key.offset >= off + len) {
4368c2ecf20Sopenharmony_ci			break;
4378c2ecf20Sopenharmony_ci		}
4388c2ecf20Sopenharmony_ci		next_key_min_offset = key.offset + datal;
4398c2ecf20Sopenharmony_ci		size = btrfs_item_size_nr(leaf, slot);
4408c2ecf20Sopenharmony_ci		read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, slot),
4418c2ecf20Sopenharmony_ci				   size);
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci		btrfs_release_path(path);
4448c2ecf20Sopenharmony_ci		path->leave_spinning = 0;
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci		memcpy(&new_key, &key, sizeof(new_key));
4478c2ecf20Sopenharmony_ci		new_key.objectid = btrfs_ino(BTRFS_I(inode));
4488c2ecf20Sopenharmony_ci		if (off <= key.offset)
4498c2ecf20Sopenharmony_ci			new_key.offset = key.offset + destoff - off;
4508c2ecf20Sopenharmony_ci		else
4518c2ecf20Sopenharmony_ci			new_key.offset = destoff;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci		/*
4548c2ecf20Sopenharmony_ci		 * Deal with a hole that doesn't have an extent item that
4558c2ecf20Sopenharmony_ci		 * represents it (NO_HOLES feature enabled).
4568c2ecf20Sopenharmony_ci		 * This hole is either in the middle of the cloning range or at
4578c2ecf20Sopenharmony_ci		 * the beginning (fully overlaps it or partially overlaps it).
4588c2ecf20Sopenharmony_ci		 */
4598c2ecf20Sopenharmony_ci		if (new_key.offset != last_dest_end)
4608c2ecf20Sopenharmony_ci			drop_start = last_dest_end;
4618c2ecf20Sopenharmony_ci		else
4628c2ecf20Sopenharmony_ci			drop_start = new_key.offset;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci		if (type == BTRFS_FILE_EXTENT_REG ||
4658c2ecf20Sopenharmony_ci		    type == BTRFS_FILE_EXTENT_PREALLOC) {
4668c2ecf20Sopenharmony_ci			struct btrfs_replace_extent_info clone_info;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci			/*
4698c2ecf20Sopenharmony_ci			 *    a  | --- range to clone ---|  b
4708c2ecf20Sopenharmony_ci			 * | ------------- extent ------------- |
4718c2ecf20Sopenharmony_ci			 */
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci			/* Subtract range b */
4748c2ecf20Sopenharmony_ci			if (key.offset + datal > off + len)
4758c2ecf20Sopenharmony_ci				datal = off + len - key.offset;
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci			/* Subtract range a */
4788c2ecf20Sopenharmony_ci			if (off > key.offset) {
4798c2ecf20Sopenharmony_ci				datao += off - key.offset;
4808c2ecf20Sopenharmony_ci				datal -= off - key.offset;
4818c2ecf20Sopenharmony_ci			}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci			clone_info.disk_offset = disko;
4848c2ecf20Sopenharmony_ci			clone_info.disk_len = diskl;
4858c2ecf20Sopenharmony_ci			clone_info.data_offset = datao;
4868c2ecf20Sopenharmony_ci			clone_info.data_len = datal;
4878c2ecf20Sopenharmony_ci			clone_info.file_offset = new_key.offset;
4888c2ecf20Sopenharmony_ci			clone_info.extent_buf = buf;
4898c2ecf20Sopenharmony_ci			clone_info.is_new_extent = false;
4908c2ecf20Sopenharmony_ci			ret = btrfs_replace_file_extents(inode, path, drop_start,
4918c2ecf20Sopenharmony_ci					new_key.offset + datal - 1, &clone_info,
4928c2ecf20Sopenharmony_ci					&trans);
4938c2ecf20Sopenharmony_ci			if (ret)
4948c2ecf20Sopenharmony_ci				goto out;
4958c2ecf20Sopenharmony_ci		} else if (type == BTRFS_FILE_EXTENT_INLINE) {
4968c2ecf20Sopenharmony_ci			/*
4978c2ecf20Sopenharmony_ci			 * Inline extents always have to start at file offset 0
4988c2ecf20Sopenharmony_ci			 * and can never be bigger then the sector size. We can
4998c2ecf20Sopenharmony_ci			 * never clone only parts of an inline extent, since all
5008c2ecf20Sopenharmony_ci			 * reflink operations must start at a sector size aligned
5018c2ecf20Sopenharmony_ci			 * offset, and the length must be aligned too or end at
5028c2ecf20Sopenharmony_ci			 * the i_size (which implies the whole inlined data).
5038c2ecf20Sopenharmony_ci			 */
5048c2ecf20Sopenharmony_ci			ASSERT(key.offset == 0);
5058c2ecf20Sopenharmony_ci			ASSERT(datal <= fs_info->sectorsize);
5068c2ecf20Sopenharmony_ci			if (WARN_ON(key.offset != 0) ||
5078c2ecf20Sopenharmony_ci			    WARN_ON(datal > fs_info->sectorsize)) {
5088c2ecf20Sopenharmony_ci				ret = -EUCLEAN;
5098c2ecf20Sopenharmony_ci				goto out;
5108c2ecf20Sopenharmony_ci			}
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci			ret = clone_copy_inline_extent(inode, path, &new_key,
5138c2ecf20Sopenharmony_ci						       drop_start, datal, size,
5148c2ecf20Sopenharmony_ci						       comp, buf, &trans);
5158c2ecf20Sopenharmony_ci			if (ret)
5168c2ecf20Sopenharmony_ci				goto out;
5178c2ecf20Sopenharmony_ci		}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci		btrfs_release_path(path);
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci		/*
5228c2ecf20Sopenharmony_ci		 * If this is a new extent update the last_reflink_trans of both
5238c2ecf20Sopenharmony_ci		 * inodes. This is used by fsync to make sure it does not log
5248c2ecf20Sopenharmony_ci		 * multiple checksum items with overlapping ranges. For older
5258c2ecf20Sopenharmony_ci		 * extents we don't need to do it since inode logging skips the
5268c2ecf20Sopenharmony_ci		 * checksums for older extents. Also ignore holes and inline
5278c2ecf20Sopenharmony_ci		 * extents because they don't have checksums in the csum tree.
5288c2ecf20Sopenharmony_ci		 */
5298c2ecf20Sopenharmony_ci		if (extent_gen == trans->transid && disko > 0) {
5308c2ecf20Sopenharmony_ci			BTRFS_I(src)->last_reflink_trans = trans->transid;
5318c2ecf20Sopenharmony_ci			BTRFS_I(inode)->last_reflink_trans = trans->transid;
5328c2ecf20Sopenharmony_ci		}
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci		last_dest_end = ALIGN(new_key.offset + datal,
5358c2ecf20Sopenharmony_ci				      fs_info->sectorsize);
5368c2ecf20Sopenharmony_ci		ret = clone_finish_inode_update(trans, inode, last_dest_end,
5378c2ecf20Sopenharmony_ci						destoff, olen, no_time_update);
5388c2ecf20Sopenharmony_ci		if (ret)
5398c2ecf20Sopenharmony_ci			goto out;
5408c2ecf20Sopenharmony_ci		if (new_key.offset + datal >= destoff + len)
5418c2ecf20Sopenharmony_ci			break;
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci		btrfs_release_path(path);
5448c2ecf20Sopenharmony_ci		key.offset = next_key_min_offset;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci		if (fatal_signal_pending(current)) {
5478c2ecf20Sopenharmony_ci			ret = -EINTR;
5488c2ecf20Sopenharmony_ci			goto out;
5498c2ecf20Sopenharmony_ci		}
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci		cond_resched();
5528c2ecf20Sopenharmony_ci	}
5538c2ecf20Sopenharmony_ci	ret = 0;
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	if (last_dest_end < destoff + len) {
5568c2ecf20Sopenharmony_ci		/*
5578c2ecf20Sopenharmony_ci		 * We have an implicit hole that fully or partially overlaps our
5588c2ecf20Sopenharmony_ci		 * cloning range at its end. This means that we either have the
5598c2ecf20Sopenharmony_ci		 * NO_HOLES feature enabled or the implicit hole happened due to
5608c2ecf20Sopenharmony_ci		 * mixing buffered and direct IO writes against this file.
5618c2ecf20Sopenharmony_ci		 */
5628c2ecf20Sopenharmony_ci		btrfs_release_path(path);
5638c2ecf20Sopenharmony_ci		path->leave_spinning = 0;
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci		/*
5668c2ecf20Sopenharmony_ci		 * When using NO_HOLES and we are cloning a range that covers
5678c2ecf20Sopenharmony_ci		 * only a hole (no extents) into a range beyond the current
5688c2ecf20Sopenharmony_ci		 * i_size, punching a hole in the target range will not create
5698c2ecf20Sopenharmony_ci		 * an extent map defining a hole, because the range starts at or
5708c2ecf20Sopenharmony_ci		 * beyond current i_size. If the file previously had an i_size
5718c2ecf20Sopenharmony_ci		 * greater than the new i_size set by this clone operation, we
5728c2ecf20Sopenharmony_ci		 * need to make sure the next fsync is a full fsync, so that it
5738c2ecf20Sopenharmony_ci		 * detects and logs a hole covering a range from the current
5748c2ecf20Sopenharmony_ci		 * i_size to the new i_size. If the clone range covers extents,
5758c2ecf20Sopenharmony_ci		 * besides a hole, then we know the full sync flag was already
5768c2ecf20Sopenharmony_ci		 * set by previous calls to btrfs_replace_file_extents() that
5778c2ecf20Sopenharmony_ci		 * replaced file extent items.
5788c2ecf20Sopenharmony_ci		 */
5798c2ecf20Sopenharmony_ci		if (last_dest_end >= i_size_read(inode))
5808c2ecf20Sopenharmony_ci			set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5818c2ecf20Sopenharmony_ci				&BTRFS_I(inode)->runtime_flags);
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci		ret = btrfs_replace_file_extents(inode, path, last_dest_end,
5848c2ecf20Sopenharmony_ci				destoff + len - 1, NULL, &trans);
5858c2ecf20Sopenharmony_ci		if (ret)
5868c2ecf20Sopenharmony_ci			goto out;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci		ret = clone_finish_inode_update(trans, inode, destoff + len,
5898c2ecf20Sopenharmony_ci						destoff, olen, no_time_update);
5908c2ecf20Sopenharmony_ci	}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ciout:
5938c2ecf20Sopenharmony_ci	btrfs_free_path(path);
5948c2ecf20Sopenharmony_ci	kvfree(buf);
5958c2ecf20Sopenharmony_ci	clear_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &BTRFS_I(inode)->runtime_flags);
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	return ret;
5988c2ecf20Sopenharmony_ci}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_cistatic void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
6018c2ecf20Sopenharmony_ci				       struct inode *inode2, u64 loff2, u64 len)
6028c2ecf20Sopenharmony_ci{
6038c2ecf20Sopenharmony_ci	unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
6048c2ecf20Sopenharmony_ci	unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
6058c2ecf20Sopenharmony_ci}
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_cistatic void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
6088c2ecf20Sopenharmony_ci				     struct inode *inode2, u64 loff2, u64 len)
6098c2ecf20Sopenharmony_ci{
6108c2ecf20Sopenharmony_ci	if (inode1 < inode2) {
6118c2ecf20Sopenharmony_ci		swap(inode1, inode2);
6128c2ecf20Sopenharmony_ci		swap(loff1, loff2);
6138c2ecf20Sopenharmony_ci	} else if (inode1 == inode2 && loff2 < loff1) {
6148c2ecf20Sopenharmony_ci		swap(loff1, loff2);
6158c2ecf20Sopenharmony_ci	}
6168c2ecf20Sopenharmony_ci	lock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
6178c2ecf20Sopenharmony_ci	lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_cistatic int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len,
6218c2ecf20Sopenharmony_ci				   struct inode *dst, u64 dst_loff)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	const u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
6248c2ecf20Sopenharmony_ci	int ret;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	/*
6278c2ecf20Sopenharmony_ci	 * Lock destination range to serialize with concurrent readpages() and
6288c2ecf20Sopenharmony_ci	 * source range to serialize with relocation.
6298c2ecf20Sopenharmony_ci	 */
6308c2ecf20Sopenharmony_ci	btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
6318c2ecf20Sopenharmony_ci	ret = btrfs_clone(src, dst, loff, len, ALIGN(len, bs), dst_loff, 1);
6328c2ecf20Sopenharmony_ci	btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	return ret;
6358c2ecf20Sopenharmony_ci}
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_cistatic int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
6388c2ecf20Sopenharmony_ci			     struct inode *dst, u64 dst_loff)
6398c2ecf20Sopenharmony_ci{
6408c2ecf20Sopenharmony_ci	int ret = 0;
6418c2ecf20Sopenharmony_ci	u64 i, tail_len, chunk_count;
6428c2ecf20Sopenharmony_ci	struct btrfs_root *root_dst = BTRFS_I(dst)->root;
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	spin_lock(&root_dst->root_item_lock);
6458c2ecf20Sopenharmony_ci	if (root_dst->send_in_progress) {
6468c2ecf20Sopenharmony_ci		btrfs_warn_rl(root_dst->fs_info,
6478c2ecf20Sopenharmony_ci"cannot deduplicate to root %llu while send operations are using it (%d in progress)",
6488c2ecf20Sopenharmony_ci			      root_dst->root_key.objectid,
6498c2ecf20Sopenharmony_ci			      root_dst->send_in_progress);
6508c2ecf20Sopenharmony_ci		spin_unlock(&root_dst->root_item_lock);
6518c2ecf20Sopenharmony_ci		return -EAGAIN;
6528c2ecf20Sopenharmony_ci	}
6538c2ecf20Sopenharmony_ci	root_dst->dedupe_in_progress++;
6548c2ecf20Sopenharmony_ci	spin_unlock(&root_dst->root_item_lock);
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
6578c2ecf20Sopenharmony_ci	chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	for (i = 0; i < chunk_count; i++) {
6608c2ecf20Sopenharmony_ci		ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
6618c2ecf20Sopenharmony_ci					      dst, dst_loff);
6628c2ecf20Sopenharmony_ci		if (ret)
6638c2ecf20Sopenharmony_ci			goto out;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci		loff += BTRFS_MAX_DEDUPE_LEN;
6668c2ecf20Sopenharmony_ci		dst_loff += BTRFS_MAX_DEDUPE_LEN;
6678c2ecf20Sopenharmony_ci	}
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	if (tail_len > 0)
6708c2ecf20Sopenharmony_ci		ret = btrfs_extent_same_range(src, loff, tail_len, dst, dst_loff);
6718c2ecf20Sopenharmony_ciout:
6728c2ecf20Sopenharmony_ci	spin_lock(&root_dst->root_item_lock);
6738c2ecf20Sopenharmony_ci	root_dst->dedupe_in_progress--;
6748c2ecf20Sopenharmony_ci	spin_unlock(&root_dst->root_item_lock);
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	return ret;
6778c2ecf20Sopenharmony_ci}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_cistatic noinline int btrfs_clone_files(struct file *file, struct file *file_src,
6808c2ecf20Sopenharmony_ci					u64 off, u64 olen, u64 destoff)
6818c2ecf20Sopenharmony_ci{
6828c2ecf20Sopenharmony_ci	struct inode *inode = file_inode(file);
6838c2ecf20Sopenharmony_ci	struct inode *src = file_inode(file_src);
6848c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6858c2ecf20Sopenharmony_ci	int ret;
6868c2ecf20Sopenharmony_ci	int wb_ret;
6878c2ecf20Sopenharmony_ci	u64 len = olen;
6888c2ecf20Sopenharmony_ci	u64 bs = fs_info->sb->s_blocksize;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	/*
6918c2ecf20Sopenharmony_ci	 * VFS's generic_remap_file_range_prep() protects us from cloning the
6928c2ecf20Sopenharmony_ci	 * eof block into the middle of a file, which would result in corruption
6938c2ecf20Sopenharmony_ci	 * if the file size is not blocksize aligned. So we don't need to check
6948c2ecf20Sopenharmony_ci	 * for that case here.
6958c2ecf20Sopenharmony_ci	 */
6968c2ecf20Sopenharmony_ci	if (off + len == src->i_size)
6978c2ecf20Sopenharmony_ci		len = ALIGN(src->i_size, bs) - off;
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	if (destoff > inode->i_size) {
7008c2ecf20Sopenharmony_ci		const u64 wb_start = ALIGN_DOWN(inode->i_size, bs);
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci		ret = btrfs_cont_expand(inode, inode->i_size, destoff);
7038c2ecf20Sopenharmony_ci		if (ret)
7048c2ecf20Sopenharmony_ci			return ret;
7058c2ecf20Sopenharmony_ci		/*
7068c2ecf20Sopenharmony_ci		 * We may have truncated the last block if the inode's size is
7078c2ecf20Sopenharmony_ci		 * not sector size aligned, so we need to wait for writeback to
7088c2ecf20Sopenharmony_ci		 * complete before proceeding further, otherwise we can race
7098c2ecf20Sopenharmony_ci		 * with cloning and attempt to increment a reference to an
7108c2ecf20Sopenharmony_ci		 * extent that no longer exists (writeback completed right after
7118c2ecf20Sopenharmony_ci		 * we found the previous extent covering eof and before we
7128c2ecf20Sopenharmony_ci		 * attempted to increment its reference count).
7138c2ecf20Sopenharmony_ci		 */
7148c2ecf20Sopenharmony_ci		ret = btrfs_wait_ordered_range(inode, wb_start,
7158c2ecf20Sopenharmony_ci					       destoff - wb_start);
7168c2ecf20Sopenharmony_ci		if (ret)
7178c2ecf20Sopenharmony_ci			return ret;
7188c2ecf20Sopenharmony_ci	}
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	/*
7218c2ecf20Sopenharmony_ci	 * Lock destination range to serialize with concurrent readpages() and
7228c2ecf20Sopenharmony_ci	 * source range to serialize with relocation.
7238c2ecf20Sopenharmony_ci	 */
7248c2ecf20Sopenharmony_ci	btrfs_double_extent_lock(src, off, inode, destoff, len);
7258c2ecf20Sopenharmony_ci	ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
7268c2ecf20Sopenharmony_ci	btrfs_double_extent_unlock(src, off, inode, destoff, len);
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	/*
7298c2ecf20Sopenharmony_ci	 * We may have copied an inline extent into a page of the destination
7308c2ecf20Sopenharmony_ci	 * range, so wait for writeback to complete before truncating pages
7318c2ecf20Sopenharmony_ci	 * from the page cache. This is a rare case.
7328c2ecf20Sopenharmony_ci	 */
7338c2ecf20Sopenharmony_ci	wb_ret = btrfs_wait_ordered_range(inode, destoff, len);
7348c2ecf20Sopenharmony_ci	ret = ret ? ret : wb_ret;
7358c2ecf20Sopenharmony_ci	/*
7368c2ecf20Sopenharmony_ci	 * Truncate page cache pages so that future reads will see the cloned
7378c2ecf20Sopenharmony_ci	 * data immediately and not the previous data.
7388c2ecf20Sopenharmony_ci	 */
7398c2ecf20Sopenharmony_ci	truncate_inode_pages_range(&inode->i_data,
7408c2ecf20Sopenharmony_ci				round_down(destoff, PAGE_SIZE),
7418c2ecf20Sopenharmony_ci				round_up(destoff + len, PAGE_SIZE) - 1);
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	return ret;
7448c2ecf20Sopenharmony_ci}
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_cistatic int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
7478c2ecf20Sopenharmony_ci				       struct file *file_out, loff_t pos_out,
7488c2ecf20Sopenharmony_ci				       loff_t *len, unsigned int remap_flags)
7498c2ecf20Sopenharmony_ci{
7508c2ecf20Sopenharmony_ci	struct inode *inode_in = file_inode(file_in);
7518c2ecf20Sopenharmony_ci	struct inode *inode_out = file_inode(file_out);
7528c2ecf20Sopenharmony_ci	u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize;
7538c2ecf20Sopenharmony_ci	bool same_inode = inode_out == inode_in;
7548c2ecf20Sopenharmony_ci	u64 wb_len;
7558c2ecf20Sopenharmony_ci	int ret;
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	if (!(remap_flags & REMAP_FILE_DEDUP)) {
7588c2ecf20Sopenharmony_ci		struct btrfs_root *root_out = BTRFS_I(inode_out)->root;
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci		if (btrfs_root_readonly(root_out))
7618c2ecf20Sopenharmony_ci			return -EROFS;
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci		if (file_in->f_path.mnt != file_out->f_path.mnt ||
7648c2ecf20Sopenharmony_ci		    inode_in->i_sb != inode_out->i_sb)
7658c2ecf20Sopenharmony_ci			return -EXDEV;
7668c2ecf20Sopenharmony_ci	}
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	/* Don't make the dst file partly checksummed */
7698c2ecf20Sopenharmony_ci	if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) !=
7708c2ecf20Sopenharmony_ci	    (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) {
7718c2ecf20Sopenharmony_ci		return -EINVAL;
7728c2ecf20Sopenharmony_ci	}
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	/*
7758c2ecf20Sopenharmony_ci	 * Now that the inodes are locked, we need to start writeback ourselves
7768c2ecf20Sopenharmony_ci	 * and can not rely on the writeback from the VFS's generic helper
7778c2ecf20Sopenharmony_ci	 * generic_remap_file_range_prep() because:
7788c2ecf20Sopenharmony_ci	 *
7798c2ecf20Sopenharmony_ci	 * 1) For compression we must call filemap_fdatawrite_range() range
7808c2ecf20Sopenharmony_ci	 *    twice (btrfs_fdatawrite_range() does it for us), and the generic
7818c2ecf20Sopenharmony_ci	 *    helper only calls it once;
7828c2ecf20Sopenharmony_ci	 *
7838c2ecf20Sopenharmony_ci	 * 2) filemap_fdatawrite_range(), called by the generic helper only
7848c2ecf20Sopenharmony_ci	 *    waits for the writeback to complete, i.e. for IO to be done, and
7858c2ecf20Sopenharmony_ci	 *    not for the ordered extents to complete. We need to wait for them
7868c2ecf20Sopenharmony_ci	 *    to complete so that new file extent items are in the fs tree.
7878c2ecf20Sopenharmony_ci	 */
7888c2ecf20Sopenharmony_ci	if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP))
7898c2ecf20Sopenharmony_ci		wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs);
7908c2ecf20Sopenharmony_ci	else
7918c2ecf20Sopenharmony_ci		wb_len = ALIGN(*len, bs);
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	/*
7948c2ecf20Sopenharmony_ci	 * Since we don't lock ranges, wait for ongoing lockless dio writes (as
7958c2ecf20Sopenharmony_ci	 * any in progress could create its ordered extents after we wait for
7968c2ecf20Sopenharmony_ci	 * existing ordered extents below).
7978c2ecf20Sopenharmony_ci	 */
7988c2ecf20Sopenharmony_ci	inode_dio_wait(inode_in);
7998c2ecf20Sopenharmony_ci	if (!same_inode)
8008c2ecf20Sopenharmony_ci		inode_dio_wait(inode_out);
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	/*
8038c2ecf20Sopenharmony_ci	 * Workaround to make sure NOCOW buffered write reach disk as NOCOW.
8048c2ecf20Sopenharmony_ci	 *
8058c2ecf20Sopenharmony_ci	 * Btrfs' back references do not have a block level granularity, they
8068c2ecf20Sopenharmony_ci	 * work at the whole extent level.
8078c2ecf20Sopenharmony_ci	 * NOCOW buffered write without data space reserved may not be able
8088c2ecf20Sopenharmony_ci	 * to fall back to CoW due to lack of data space, thus could cause
8098c2ecf20Sopenharmony_ci	 * data loss.
8108c2ecf20Sopenharmony_ci	 *
8118c2ecf20Sopenharmony_ci	 * Here we take a shortcut by flushing the whole inode, so that all
8128c2ecf20Sopenharmony_ci	 * nocow write should reach disk as nocow before we increase the
8138c2ecf20Sopenharmony_ci	 * reference of the extent. We could do better by only flushing NOCOW
8148c2ecf20Sopenharmony_ci	 * data, but that needs extra accounting.
8158c2ecf20Sopenharmony_ci	 *
8168c2ecf20Sopenharmony_ci	 * Also we don't need to check ASYNC_EXTENT, as async extent will be
8178c2ecf20Sopenharmony_ci	 * CoWed anyway, not affecting nocow part.
8188c2ecf20Sopenharmony_ci	 */
8198c2ecf20Sopenharmony_ci	ret = filemap_flush(inode_in->i_mapping);
8208c2ecf20Sopenharmony_ci	if (ret < 0)
8218c2ecf20Sopenharmony_ci		return ret;
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs),
8248c2ecf20Sopenharmony_ci				       wb_len);
8258c2ecf20Sopenharmony_ci	if (ret < 0)
8268c2ecf20Sopenharmony_ci		return ret;
8278c2ecf20Sopenharmony_ci	ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs),
8288c2ecf20Sopenharmony_ci				       wb_len);
8298c2ecf20Sopenharmony_ci	if (ret < 0)
8308c2ecf20Sopenharmony_ci		return ret;
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
8338c2ecf20Sopenharmony_ci					    len, remap_flags);
8348c2ecf20Sopenharmony_ci}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ciloff_t btrfs_remap_file_range(struct file *src_file, loff_t off,
8378c2ecf20Sopenharmony_ci		struct file *dst_file, loff_t destoff, loff_t len,
8388c2ecf20Sopenharmony_ci		unsigned int remap_flags)
8398c2ecf20Sopenharmony_ci{
8408c2ecf20Sopenharmony_ci	struct inode *src_inode = file_inode(src_file);
8418c2ecf20Sopenharmony_ci	struct inode *dst_inode = file_inode(dst_file);
8428c2ecf20Sopenharmony_ci	bool same_inode = dst_inode == src_inode;
8438c2ecf20Sopenharmony_ci	int ret;
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
8468c2ecf20Sopenharmony_ci		return -EINVAL;
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	if (same_inode)
8498c2ecf20Sopenharmony_ci		inode_lock(src_inode);
8508c2ecf20Sopenharmony_ci	else
8518c2ecf20Sopenharmony_ci		lock_two_nondirectories(src_inode, dst_inode);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff,
8548c2ecf20Sopenharmony_ci					  &len, remap_flags);
8558c2ecf20Sopenharmony_ci	if (ret < 0 || len == 0)
8568c2ecf20Sopenharmony_ci		goto out_unlock;
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci	if (remap_flags & REMAP_FILE_DEDUP)
8598c2ecf20Sopenharmony_ci		ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff);
8608c2ecf20Sopenharmony_ci	else
8618c2ecf20Sopenharmony_ci		ret = btrfs_clone_files(dst_file, src_file, off, len, destoff);
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ciout_unlock:
8648c2ecf20Sopenharmony_ci	if (same_inode)
8658c2ecf20Sopenharmony_ci		inode_unlock(src_inode);
8668c2ecf20Sopenharmony_ci	else
8678c2ecf20Sopenharmony_ci		unlock_two_nondirectories(src_inode, dst_inode);
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	return ret < 0 ? ret : len;
8708c2ecf20Sopenharmony_ci}
871