xref: /openbmc/linux/fs/btrfs/file-item.c (revision 5cfe76f8)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
26cbd5570SChris Mason /*
36cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
46cbd5570SChris Mason  */
56cbd5570SChris Mason 
6065631f6SChris Mason #include <linux/bio.h>
75a0e3ad6STejun Heo #include <linux/slab.h>
8065631f6SChris Mason #include <linux/pagemap.h>
9065631f6SChris Mason #include <linux/highmem.h>
10a3d46aeaSNikolay Borisov #include <linux/sched/mm.h>
11d5178578SJohannes Thumshirn #include <crypto/hash.h>
129b569ea0SJosef Bacik #include "messages.h"
13cea62800SJohannes Thumshirn #include "misc.h"
141e1d2701SChris Mason #include "ctree.h"
15dee26a9fSChris Mason #include "disk-io.h"
169f5fae2fSChris Mason #include "transaction.h"
17103c1972SChristoph Hellwig #include "bio.h"
181de037a4SChris Mason #include "print-tree.h"
19ebb8765bSAnand Jain #include "compression.h"
20c7f13d42SJosef Bacik #include "fs.h"
2107e81dc9SJosef Bacik #include "accessors.h"
227c8ede16SJosef Bacik #include "file-item.h"
237f0add25SJosef Bacik #include "super.h"
241e1d2701SChris Mason 
2542049bf6SChris Mason #define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \
2642049bf6SChris Mason 				   sizeof(struct btrfs_item) * 2) / \
2742049bf6SChris Mason 				  size) - 1))
2807d400a6SYan Zheng 
29221b8318SZach Brown #define MAX_CSUM_ITEMS(r, size) (min_t(u32, __MAX_CSUM_ITEMS(r, size), \
3009cbfeafSKirill A. Shutemov 				       PAGE_SIZE))
317ca4be45SChris Mason 
3243dd529aSDavid Sterba /*
3343dd529aSDavid Sterba  * Set inode's size according to filesystem options.
34ca4207aeSNikolay Borisov  *
35ca4207aeSNikolay Borisov  * @inode:      inode we want to update the disk_i_size for
36ca4207aeSNikolay Borisov  * @new_i_size: i_size we want to set to, 0 if we use i_size
3741a2ee75SJosef Bacik  *
3841a2ee75SJosef Bacik  * With NO_HOLES set this simply sets the disk_is_size to whatever i_size_read()
3941a2ee75SJosef Bacik  * returns as it is perfectly fine with a file that has holes without hole file
4041a2ee75SJosef Bacik  * extent items.
4141a2ee75SJosef Bacik  *
4241a2ee75SJosef Bacik  * However without NO_HOLES we need to only return the area that is contiguous
4341a2ee75SJosef Bacik  * from the 0 offset of the file.  Otherwise we could end up adjust i_size up
4441a2ee75SJosef Bacik  * to an extent that has a gap in between.
4541a2ee75SJosef Bacik  *
4641a2ee75SJosef Bacik  * Finally new_i_size should only be set in the case of truncate where we're not
4741a2ee75SJosef Bacik  * ready to use i_size_read() as the limiter yet.
4841a2ee75SJosef Bacik  */
4976aea537SNikolay Borisov void btrfs_inode_safe_disk_i_size_write(struct btrfs_inode *inode, u64 new_i_size)
5041a2ee75SJosef Bacik {
5176aea537SNikolay Borisov 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
5241a2ee75SJosef Bacik 	u64 start, end, i_size;
5341a2ee75SJosef Bacik 	int ret;
5441a2ee75SJosef Bacik 
55e7db9e5cSBoris Burkov 	spin_lock(&inode->lock);
5676aea537SNikolay Borisov 	i_size = new_i_size ?: i_size_read(&inode->vfs_inode);
5741a2ee75SJosef Bacik 	if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
5876aea537SNikolay Borisov 		inode->disk_i_size = i_size;
59e7db9e5cSBoris Burkov 		goto out_unlock;
6041a2ee75SJosef Bacik 	}
6141a2ee75SJosef Bacik 
6276aea537SNikolay Borisov 	ret = find_contiguous_extent_bit(&inode->file_extent_tree, 0, &start,
6376aea537SNikolay Borisov 					 &end, EXTENT_DIRTY);
6441a2ee75SJosef Bacik 	if (!ret && start == 0)
6541a2ee75SJosef Bacik 		i_size = min(i_size, end + 1);
6641a2ee75SJosef Bacik 	else
6741a2ee75SJosef Bacik 		i_size = 0;
6876aea537SNikolay Borisov 	inode->disk_i_size = i_size;
69e7db9e5cSBoris Burkov out_unlock:
7076aea537SNikolay Borisov 	spin_unlock(&inode->lock);
7141a2ee75SJosef Bacik }
7241a2ee75SJosef Bacik 
7343dd529aSDavid Sterba /*
7443dd529aSDavid Sterba  * Mark range within a file as having a new extent inserted.
75ca4207aeSNikolay Borisov  *
76ca4207aeSNikolay Borisov  * @inode: inode being modified
77ca4207aeSNikolay Borisov  * @start: start file offset of the file extent we've inserted
78ca4207aeSNikolay Borisov  * @len:   logical length of the file extent item
7941a2ee75SJosef Bacik  *
8041a2ee75SJosef Bacik  * Call when we are inserting a new file extent where there was none before.
8141a2ee75SJosef Bacik  * Does not need to call this in the case where we're replacing an existing file
8241a2ee75SJosef Bacik  * extent, however if not sure it's fine to call this multiple times.
8341a2ee75SJosef Bacik  *
8441a2ee75SJosef Bacik  * The start and len must match the file extent item, so thus must be sectorsize
8541a2ee75SJosef Bacik  * aligned.
8641a2ee75SJosef Bacik  */
8741a2ee75SJosef Bacik int btrfs_inode_set_file_extent_range(struct btrfs_inode *inode, u64 start,
8841a2ee75SJosef Bacik 				      u64 len)
8941a2ee75SJosef Bacik {
9041a2ee75SJosef Bacik 	if (len == 0)
9141a2ee75SJosef Bacik 		return 0;
9241a2ee75SJosef Bacik 
9341a2ee75SJosef Bacik 	ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize));
9441a2ee75SJosef Bacik 
9541a2ee75SJosef Bacik 	if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES))
9641a2ee75SJosef Bacik 		return 0;
970acd32c2SDavid Sterba 	return set_extent_bit(&inode->file_extent_tree, start, start + len - 1,
981d126800SDavid Sterba 			      EXTENT_DIRTY, NULL);
9941a2ee75SJosef Bacik }
10041a2ee75SJosef Bacik 
10143dd529aSDavid Sterba /*
10243dd529aSDavid Sterba  * Mark an inode range as not having a backing extent.
103ca4207aeSNikolay Borisov  *
104ca4207aeSNikolay Borisov  * @inode: inode being modified
105ca4207aeSNikolay Borisov  * @start: start file offset of the file extent we've inserted
106ca4207aeSNikolay Borisov  * @len:   logical length of the file extent item
10741a2ee75SJosef Bacik  *
10841a2ee75SJosef Bacik  * Called when we drop a file extent, for example when we truncate.  Doesn't
10941a2ee75SJosef Bacik  * need to be called for cases where we're replacing a file extent, like when
11041a2ee75SJosef Bacik  * we've COWed a file extent.
11141a2ee75SJosef Bacik  *
11241a2ee75SJosef Bacik  * The start and len must match the file extent item, so thus must be sectorsize
11341a2ee75SJosef Bacik  * aligned.
11441a2ee75SJosef Bacik  */
11541a2ee75SJosef Bacik int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start,
11641a2ee75SJosef Bacik 					u64 len)
11741a2ee75SJosef Bacik {
11841a2ee75SJosef Bacik 	if (len == 0)
11941a2ee75SJosef Bacik 		return 0;
12041a2ee75SJosef Bacik 
12141a2ee75SJosef Bacik 	ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize) ||
12241a2ee75SJosef Bacik 	       len == (u64)-1);
12341a2ee75SJosef Bacik 
12441a2ee75SJosef Bacik 	if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES))
12541a2ee75SJosef Bacik 		return 0;
12641a2ee75SJosef Bacik 	return clear_extent_bit(&inode->file_extent_tree, start,
127bd015294SJosef Bacik 				start + len - 1, EXTENT_DIRTY, NULL);
12841a2ee75SJosef Bacik }
12941a2ee75SJosef Bacik 
130cb649e81SQu Wenruo static size_t bytes_to_csum_size(const struct btrfs_fs_info *fs_info, u32 bytes)
1311e25a2e3SJohannes Thumshirn {
132cb649e81SQu Wenruo 	ASSERT(IS_ALIGNED(bytes, fs_info->sectorsize));
1331e25a2e3SJohannes Thumshirn 
134cb649e81SQu Wenruo 	return (bytes >> fs_info->sectorsize_bits) * fs_info->csum_size;
135cb649e81SQu Wenruo }
136cb649e81SQu Wenruo 
137cb649e81SQu Wenruo static size_t csum_size_to_bytes(const struct btrfs_fs_info *fs_info, u32 csum_size)
138cb649e81SQu Wenruo {
139cb649e81SQu Wenruo 	ASSERT(IS_ALIGNED(csum_size, fs_info->csum_size));
140cb649e81SQu Wenruo 
141cb649e81SQu Wenruo 	return (csum_size / fs_info->csum_size) << fs_info->sectorsize_bits;
142cb649e81SQu Wenruo }
143cb649e81SQu Wenruo 
144cb649e81SQu Wenruo static inline u32 max_ordered_sum_bytes(const struct btrfs_fs_info *fs_info)
145cb649e81SQu Wenruo {
146cb649e81SQu Wenruo 	u32 max_csum_size = round_down(PAGE_SIZE - sizeof(struct btrfs_ordered_sum),
147cb649e81SQu Wenruo 				       fs_info->csum_size);
148cb649e81SQu Wenruo 
149cb649e81SQu Wenruo 	return csum_size_to_bytes(fs_info, max_csum_size);
1501e25a2e3SJohannes Thumshirn }
15107d400a6SYan Zheng 
1522b6433c7SJosef Bacik /*
1532b6433c7SJosef Bacik  * Calculate the total size needed to allocate for an ordered sum structure
1542b6433c7SJosef Bacik  * spanning @bytes in the file.
1552b6433c7SJosef Bacik  */
1562b6433c7SJosef Bacik static int btrfs_ordered_sum_size(struct btrfs_fs_info *fs_info, unsigned long bytes)
1572b6433c7SJosef Bacik {
158cb649e81SQu Wenruo 	return sizeof(struct btrfs_ordered_sum) + bytes_to_csum_size(fs_info, bytes);
1592b6433c7SJosef Bacik }
1602b6433c7SJosef Bacik 
161d1f68ba0SOmar Sandoval int btrfs_insert_hole_extent(struct btrfs_trans_handle *trans,
162dee26a9fSChris Mason 			     struct btrfs_root *root,
163d1f68ba0SOmar Sandoval 			     u64 objectid, u64 pos, u64 num_bytes)
1649f5fae2fSChris Mason {
165dee26a9fSChris Mason 	int ret = 0;
166dee26a9fSChris Mason 	struct btrfs_file_extent_item *item;
167dee26a9fSChris Mason 	struct btrfs_key file_key;
1685caf2a00SChris Mason 	struct btrfs_path *path;
1695f39d397SChris Mason 	struct extent_buffer *leaf;
170dee26a9fSChris Mason 
1715caf2a00SChris Mason 	path = btrfs_alloc_path();
172db5b493aSTsutomu Itoh 	if (!path)
173db5b493aSTsutomu Itoh 		return -ENOMEM;
174dee26a9fSChris Mason 	file_key.objectid = objectid;
175b18c6685SChris Mason 	file_key.offset = pos;
176962a298fSDavid Sterba 	file_key.type = BTRFS_EXTENT_DATA_KEY;
177dee26a9fSChris Mason 
1785caf2a00SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, &file_key,
179dee26a9fSChris Mason 				      sizeof(*item));
18054aa1f4dSChris Mason 	if (ret < 0)
18154aa1f4dSChris Mason 		goto out;
18279787eaaSJeff Mahoney 	BUG_ON(ret); /* Can't happen */
1835f39d397SChris Mason 	leaf = path->nodes[0];
1845f39d397SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0],
185dee26a9fSChris Mason 			      struct btrfs_file_extent_item);
186d1f68ba0SOmar Sandoval 	btrfs_set_file_extent_disk_bytenr(leaf, item, 0);
187d1f68ba0SOmar Sandoval 	btrfs_set_file_extent_disk_num_bytes(leaf, item, 0);
188d1f68ba0SOmar Sandoval 	btrfs_set_file_extent_offset(leaf, item, 0);
189db94535dSChris Mason 	btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
190d1f68ba0SOmar Sandoval 	btrfs_set_file_extent_ram_bytes(leaf, item, num_bytes);
1915f39d397SChris Mason 	btrfs_set_file_extent_generation(leaf, item, trans->transid);
1925f39d397SChris Mason 	btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
193d1f68ba0SOmar Sandoval 	btrfs_set_file_extent_compression(leaf, item, 0);
194d1f68ba0SOmar Sandoval 	btrfs_set_file_extent_encryption(leaf, item, 0);
195d1f68ba0SOmar Sandoval 	btrfs_set_file_extent_other_encoding(leaf, item, 0);
196c8b97818SChris Mason 
1975f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
19854aa1f4dSChris Mason out:
1995caf2a00SChris Mason 	btrfs_free_path(path);
20054aa1f4dSChris Mason 	return ret;
2019f5fae2fSChris Mason }
202dee26a9fSChris Mason 
20348a3b636SEric Sandeen static struct btrfs_csum_item *
20448a3b636SEric Sandeen btrfs_lookup_csum(struct btrfs_trans_handle *trans,
205b18c6685SChris Mason 		  struct btrfs_root *root,
2066567e837SChris Mason 		  struct btrfs_path *path,
207d20f7043SChris Mason 		  u64 bytenr, int cow)
2086567e837SChris Mason {
2090b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
2106567e837SChris Mason 	int ret;
2116567e837SChris Mason 	struct btrfs_key file_key;
2126567e837SChris Mason 	struct btrfs_key found_key;
2136567e837SChris Mason 	struct btrfs_csum_item *item;
2145f39d397SChris Mason 	struct extent_buffer *leaf;
2156567e837SChris Mason 	u64 csum_offset = 0;
216223486c2SDavid Sterba 	const u32 csum_size = fs_info->csum_size;
217a429e513SChris Mason 	int csums_in_item;
2186567e837SChris Mason 
219d20f7043SChris Mason 	file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
220d20f7043SChris Mason 	file_key.offset = bytenr;
221962a298fSDavid Sterba 	file_key.type = BTRFS_EXTENT_CSUM_KEY;
222b18c6685SChris Mason 	ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
2236567e837SChris Mason 	if (ret < 0)
2246567e837SChris Mason 		goto fail;
2255f39d397SChris Mason 	leaf = path->nodes[0];
2266567e837SChris Mason 	if (ret > 0) {
2276567e837SChris Mason 		ret = 1;
22870b2befdSChris Mason 		if (path->slots[0] == 0)
2296567e837SChris Mason 			goto fail;
2306567e837SChris Mason 		path->slots[0]--;
2315f39d397SChris Mason 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
232962a298fSDavid Sterba 		if (found_key.type != BTRFS_EXTENT_CSUM_KEY)
2336567e837SChris Mason 			goto fail;
234d20f7043SChris Mason 
235d20f7043SChris Mason 		csum_offset = (bytenr - found_key.offset) >>
236265fdfa6SDavid Sterba 				fs_info->sectorsize_bits;
2373212fa14SJosef Bacik 		csums_in_item = btrfs_item_size(leaf, path->slots[0]);
238607d432dSJosef Bacik 		csums_in_item /= csum_size;
239a429e513SChris Mason 
24082d130ffSMiao Xie 		if (csum_offset == csums_in_item) {
241a429e513SChris Mason 			ret = -EFBIG;
2426567e837SChris Mason 			goto fail;
24382d130ffSMiao Xie 		} else if (csum_offset > csums_in_item) {
24482d130ffSMiao Xie 			goto fail;
2456567e837SChris Mason 		}
2466567e837SChris Mason 	}
2476567e837SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
248509659cdSChris Mason 	item = (struct btrfs_csum_item *)((unsigned char *)item +
249607d432dSJosef Bacik 					  csum_offset * csum_size);
2506567e837SChris Mason 	return item;
2516567e837SChris Mason fail:
2526567e837SChris Mason 	if (ret > 0)
253b18c6685SChris Mason 		ret = -ENOENT;
2546567e837SChris Mason 	return ERR_PTR(ret);
2556567e837SChris Mason }
2566567e837SChris Mason 
257dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
258dee26a9fSChris Mason 			     struct btrfs_root *root,
259dee26a9fSChris Mason 			     struct btrfs_path *path, u64 objectid,
2609773a788SChris Mason 			     u64 offset, int mod)
261dee26a9fSChris Mason {
262dee26a9fSChris Mason 	struct btrfs_key file_key;
263dee26a9fSChris Mason 	int ins_len = mod < 0 ? -1 : 0;
264dee26a9fSChris Mason 	int cow = mod != 0;
265dee26a9fSChris Mason 
266dee26a9fSChris Mason 	file_key.objectid = objectid;
26770b2befdSChris Mason 	file_key.offset = offset;
268962a298fSDavid Sterba 	file_key.type = BTRFS_EXTENT_DATA_KEY;
269f8ee80deSMarcos Paulo de Souza 
270f8ee80deSMarcos Paulo de Souza 	return btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
271dee26a9fSChris Mason }
272f254e52cSChris Mason 
2736275193eSQu Wenruo /*
2746275193eSQu Wenruo  * Find checksums for logical bytenr range [disk_bytenr, disk_bytenr + len) and
27543dd529aSDavid Sterba  * store the result to @dst.
2766275193eSQu Wenruo  *
2776275193eSQu Wenruo  * Return >0 for the number of sectors we found.
2786275193eSQu Wenruo  * Return 0 for the range [disk_bytenr, disk_bytenr + sectorsize) has no csum
2796275193eSQu Wenruo  * for it. Caller may want to try next sector until one range is hit.
2806275193eSQu Wenruo  * Return <0 for fatal error.
2816275193eSQu Wenruo  */
2826275193eSQu Wenruo static int search_csum_tree(struct btrfs_fs_info *fs_info,
2836275193eSQu Wenruo 			    struct btrfs_path *path, u64 disk_bytenr,
2846275193eSQu Wenruo 			    u64 len, u8 *dst)
2856275193eSQu Wenruo {
286fc28b25eSJosef Bacik 	struct btrfs_root *csum_root;
2876275193eSQu Wenruo 	struct btrfs_csum_item *item = NULL;
2886275193eSQu Wenruo 	struct btrfs_key key;
2896275193eSQu Wenruo 	const u32 sectorsize = fs_info->sectorsize;
2906275193eSQu Wenruo 	const u32 csum_size = fs_info->csum_size;
2916275193eSQu Wenruo 	u32 itemsize;
2926275193eSQu Wenruo 	int ret;
2936275193eSQu Wenruo 	u64 csum_start;
2946275193eSQu Wenruo 	u64 csum_len;
2956275193eSQu Wenruo 
2966275193eSQu Wenruo 	ASSERT(IS_ALIGNED(disk_bytenr, sectorsize) &&
2976275193eSQu Wenruo 	       IS_ALIGNED(len, sectorsize));
2986275193eSQu Wenruo 
2996275193eSQu Wenruo 	/* Check if the current csum item covers disk_bytenr */
3006275193eSQu Wenruo 	if (path->nodes[0]) {
3016275193eSQu Wenruo 		item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3026275193eSQu Wenruo 				      struct btrfs_csum_item);
3036275193eSQu Wenruo 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3043212fa14SJosef Bacik 		itemsize = btrfs_item_size(path->nodes[0], path->slots[0]);
3056275193eSQu Wenruo 
3066275193eSQu Wenruo 		csum_start = key.offset;
3076275193eSQu Wenruo 		csum_len = (itemsize / csum_size) * sectorsize;
3086275193eSQu Wenruo 
3096275193eSQu Wenruo 		if (in_range(disk_bytenr, csum_start, csum_len))
3106275193eSQu Wenruo 			goto found;
3116275193eSQu Wenruo 	}
3126275193eSQu Wenruo 
3136275193eSQu Wenruo 	/* Current item doesn't contain the desired range, search again */
3146275193eSQu Wenruo 	btrfs_release_path(path);
315fc28b25eSJosef Bacik 	csum_root = btrfs_csum_root(fs_info, disk_bytenr);
316fc28b25eSJosef Bacik 	item = btrfs_lookup_csum(NULL, csum_root, path, disk_bytenr, 0);
3176275193eSQu Wenruo 	if (IS_ERR(item)) {
3186275193eSQu Wenruo 		ret = PTR_ERR(item);
3196275193eSQu Wenruo 		goto out;
3206275193eSQu Wenruo 	}
3216275193eSQu Wenruo 	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3223212fa14SJosef Bacik 	itemsize = btrfs_item_size(path->nodes[0], path->slots[0]);
3236275193eSQu Wenruo 
3246275193eSQu Wenruo 	csum_start = key.offset;
3256275193eSQu Wenruo 	csum_len = (itemsize / csum_size) * sectorsize;
3266275193eSQu Wenruo 	ASSERT(in_range(disk_bytenr, csum_start, csum_len));
3276275193eSQu Wenruo 
3286275193eSQu Wenruo found:
3296275193eSQu Wenruo 	ret = (min(csum_start + csum_len, disk_bytenr + len) -
3306275193eSQu Wenruo 		   disk_bytenr) >> fs_info->sectorsize_bits;
3316275193eSQu Wenruo 	read_extent_buffer(path->nodes[0], dst, (unsigned long)item,
3326275193eSQu Wenruo 			ret * csum_size);
3336275193eSQu Wenruo out:
33403ddb19dSJosef Bacik 	if (ret == -ENOENT || ret == -EFBIG)
3356275193eSQu Wenruo 		ret = 0;
3366275193eSQu Wenruo 	return ret;
3376275193eSQu Wenruo }
3386275193eSQu Wenruo 
3396275193eSQu Wenruo /*
3406275193eSQu Wenruo  * Lookup the checksum for the read bio in csum tree.
3419e46458aSQu Wenruo  *
342e62958fcSOmar Sandoval  * Return: BLK_STS_RESOURCE if allocating memory fails, BLK_STS_OK otherwise.
343e62958fcSOmar Sandoval  */
3444ae2edf1SChristoph Hellwig blk_status_t btrfs_lookup_bio_sums(struct btrfs_bio *bbio)
34561b49440SChris Mason {
3464ae2edf1SChristoph Hellwig 	struct btrfs_inode *inode = bbio->inode;
3474ae2edf1SChristoph Hellwig 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
3484ae2edf1SChristoph Hellwig 	struct bio *bio = &bbio->bio;
349facc8a22SMiao Xie 	struct btrfs_path *path;
3506275193eSQu Wenruo 	const u32 sectorsize = fs_info->sectorsize;
351223486c2SDavid Sterba 	const u32 csum_size = fs_info->csum_size;
3526275193eSQu Wenruo 	u32 orig_len = bio->bi_iter.bi_size;
3536275193eSQu Wenruo 	u64 orig_disk_bytenr = bio->bi_iter.bi_sector << SECTOR_SHIFT;
3546275193eSQu Wenruo 	const unsigned int nblocks = orig_len >> fs_info->sectorsize_bits;
3551784b7d5SJosef Bacik 	blk_status_t ret = BLK_STS_OK;
356e2eb0248SChristoph Hellwig 	u32 bio_offset = 0;
35761b49440SChris Mason 
3584ae2edf1SChristoph Hellwig 	if ((inode->flags & BTRFS_INODE_NODATASUM) ||
359056c8311SJosef Bacik 	    test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state))
360334c16d8SJosef Bacik 		return BLK_STS_OK;
361334c16d8SJosef Bacik 
3629e46458aSQu Wenruo 	/*
3639e46458aSQu Wenruo 	 * This function is only called for read bio.
3649e46458aSQu Wenruo 	 *
3659e46458aSQu Wenruo 	 * This means two things:
3669e46458aSQu Wenruo 	 * - All our csums should only be in csum tree
3679e46458aSQu Wenruo 	 *   No ordered extents csums, as ordered extents are only for write
3689e46458aSQu Wenruo 	 *   path.
3696275193eSQu Wenruo 	 * - No need to bother any other info from bvec
3706275193eSQu Wenruo 	 *   Since we're looking up csums, the only important info is the
3716275193eSQu Wenruo 	 *   disk_bytenr and the length, which can be extracted from bi_iter
3726275193eSQu Wenruo 	 *   directly.
3739e46458aSQu Wenruo 	 */
3749e46458aSQu Wenruo 	ASSERT(bio_op(bio) == REQ_OP_READ);
37561b49440SChris Mason 	path = btrfs_alloc_path();
376c2db1073STsutomu Itoh 	if (!path)
3774e4cbee9SChristoph Hellwig 		return BLK_STS_RESOURCE;
378facc8a22SMiao Xie 
379facc8a22SMiao Xie 	if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) {
380c3a3b19bSQu Wenruo 		bbio->csum = kmalloc_array(nblocks, csum_size, GFP_NOFS);
381c3a3b19bSQu Wenruo 		if (!bbio->csum) {
382facc8a22SMiao Xie 			btrfs_free_path(path);
3834e4cbee9SChristoph Hellwig 			return BLK_STS_RESOURCE;
384facc8a22SMiao Xie 		}
385facc8a22SMiao Xie 	} else {
386c3a3b19bSQu Wenruo 		bbio->csum = bbio->csum_inline;
387facc8a22SMiao Xie 	}
388facc8a22SMiao Xie 
38935478d05SQu Wenruo 	/*
39035478d05SQu Wenruo 	 * If requested number of sectors is larger than one leaf can contain,
39135478d05SQu Wenruo 	 * kick the readahead for csum tree.
39235478d05SQu Wenruo 	 */
39335478d05SQu Wenruo 	if (nblocks > fs_info->csums_per_leaf)
394e4058b54SDavid Sterba 		path->reada = READA_FORWARD;
39561b49440SChris Mason 
3962cf8572dSChris Mason 	/*
3972cf8572dSChris Mason 	 * the free space stuff is only read when it hasn't been
3982cf8572dSChris Mason 	 * updated in the current transaction.  So, we can safely
3992cf8572dSChris Mason 	 * read from the commit root and sidestep a nasty deadlock
4002cf8572dSChris Mason 	 * between reading the free space cache and updating the csum tree.
4012cf8572dSChris Mason 	 */
4024ae2edf1SChristoph Hellwig 	if (btrfs_is_free_space_inode(inode)) {
4032cf8572dSChris Mason 		path->search_commit_root = 1;
404ddf23b3fSJosef Bacik 		path->skip_locking = 1;
405ddf23b3fSJosef Bacik 	}
4062cf8572dSChris Mason 
407e2eb0248SChristoph Hellwig 	while (bio_offset < orig_len) {
408e2eb0248SChristoph Hellwig 		int count;
409e2eb0248SChristoph Hellwig 		u64 cur_disk_bytenr = orig_disk_bytenr + bio_offset;
410e2eb0248SChristoph Hellwig 		u8 *csum_dst = bbio->csum +
411e2eb0248SChristoph Hellwig 			(bio_offset >> fs_info->sectorsize_bits) * csum_size;
4124989d277SChristoph Hellwig 
4136275193eSQu Wenruo 		count = search_csum_tree(fs_info, path, cur_disk_bytenr,
414e2eb0248SChristoph Hellwig 					 orig_len - bio_offset, csum_dst);
4151784b7d5SJosef Bacik 		if (count < 0) {
4161784b7d5SJosef Bacik 			ret = errno_to_blk_status(count);
4174ae2edf1SChristoph Hellwig 			if (bbio->csum != bbio->csum_inline)
4184ae2edf1SChristoph Hellwig 				kfree(bbio->csum);
4194ae2edf1SChristoph Hellwig 			bbio->csum = NULL;
4201784b7d5SJosef Bacik 			break;
4211784b7d5SJosef Bacik 		}
4221784b7d5SJosef Bacik 
4236275193eSQu Wenruo 		/*
4241784b7d5SJosef Bacik 		 * We didn't find a csum for this range.  We need to make sure
4251784b7d5SJosef Bacik 		 * we complain loudly about this, because we are not NODATASUM.
4261784b7d5SJosef Bacik 		 *
4271784b7d5SJosef Bacik 		 * However for the DATA_RELOC inode we could potentially be
4281784b7d5SJosef Bacik 		 * relocating data extents for a NODATASUM inode, so the inode
4291784b7d5SJosef Bacik 		 * itself won't be marked with NODATASUM, but the extent we're
4301784b7d5SJosef Bacik 		 * copying is in fact NODATASUM.  If we don't find a csum we
4311784b7d5SJosef Bacik 		 * assume this is the case.
4326275193eSQu Wenruo 		 */
4331784b7d5SJosef Bacik 		if (count == 0) {
4346275193eSQu Wenruo 			memset(csum_dst, 0, csum_size);
435e4100d98SMiao Xie 			count = 1;
4366275193eSQu Wenruo 
4374ae2edf1SChristoph Hellwig 			if (inode->root->root_key.objectid ==
43817d217feSYan Zheng 			    BTRFS_DATA_RELOC_TREE_OBJECTID) {
439e2eb0248SChristoph Hellwig 				u64 file_offset = bbio->file_offset + bio_offset;
4406275193eSQu Wenruo 
4410acd32c2SDavid Sterba 				set_extent_bit(&inode->io_tree, file_offset,
4426275193eSQu Wenruo 					       file_offset + sectorsize - 1,
4431d126800SDavid Sterba 					       EXTENT_NODATASUM, NULL);
44417d217feSYan Zheng 			} else {
4456275193eSQu Wenruo 				btrfs_warn_rl(fs_info,
4466275193eSQu Wenruo 			"csum hole found for disk bytenr range [%llu, %llu)",
4476275193eSQu Wenruo 				cur_disk_bytenr, cur_disk_bytenr + sectorsize);
44817d217feSYan Zheng 			}
4494989d277SChristoph Hellwig 		}
450e2eb0248SChristoph Hellwig 		bio_offset += count * sectorsize;
4514989d277SChristoph Hellwig 	}
4524989d277SChristoph Hellwig 
45361b49440SChris Mason 	btrfs_free_path(path);
4541784b7d5SJosef Bacik 	return ret;
4554b46fce2SJosef Bacik }
4564b46fce2SJosef Bacik 
45797e38239SQu Wenruo int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end,
45826ce9114SJosef Bacik 			    struct list_head *list, int search_commit,
45926ce9114SJosef Bacik 			    bool nowait)
46017d217feSYan Zheng {
4610b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
46217d217feSYan Zheng 	struct btrfs_key key;
46317d217feSYan Zheng 	struct btrfs_path *path;
46417d217feSYan Zheng 	struct extent_buffer *leaf;
46517d217feSYan Zheng 	struct btrfs_ordered_sum *sums;
46617d217feSYan Zheng 	struct btrfs_csum_item *item;
4670678b618SMark Fasheh 	LIST_HEAD(tmplist);
46817d217feSYan Zheng 	int ret;
46917d217feSYan Zheng 
4700b246afaSJeff Mahoney 	ASSERT(IS_ALIGNED(start, fs_info->sectorsize) &&
4710b246afaSJeff Mahoney 	       IS_ALIGNED(end + 1, fs_info->sectorsize));
4724277a9c3SJosef Bacik 
47317d217feSYan Zheng 	path = btrfs_alloc_path();
474d8926bb3SMark Fasheh 	if (!path)
475d8926bb3SMark Fasheh 		return -ENOMEM;
47617d217feSYan Zheng 
47726ce9114SJosef Bacik 	path->nowait = nowait;
478a2de733cSArne Jansen 	if (search_commit) {
479a2de733cSArne Jansen 		path->skip_locking = 1;
480e4058b54SDavid Sterba 		path->reada = READA_FORWARD;
481a2de733cSArne Jansen 		path->search_commit_root = 1;
482a2de733cSArne Jansen 	}
483a2de733cSArne Jansen 
48417d217feSYan Zheng 	key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
48517d217feSYan Zheng 	key.offset = start;
48617d217feSYan Zheng 	key.type = BTRFS_EXTENT_CSUM_KEY;
48717d217feSYan Zheng 
48807d400a6SYan Zheng 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
48917d217feSYan Zheng 	if (ret < 0)
49017d217feSYan Zheng 		goto fail;
49117d217feSYan Zheng 	if (ret > 0 && path->slots[0] > 0) {
49217d217feSYan Zheng 		leaf = path->nodes[0];
49317d217feSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
494cb649e81SQu Wenruo 
495cb649e81SQu Wenruo 		/*
496cb649e81SQu Wenruo 		 * There are two cases we can hit here for the previous csum
497cb649e81SQu Wenruo 		 * item:
498cb649e81SQu Wenruo 		 *
499cb649e81SQu Wenruo 		 *		|<- search range ->|
500cb649e81SQu Wenruo 		 *	|<- csum item ->|
501cb649e81SQu Wenruo 		 *
502cb649e81SQu Wenruo 		 * Or
503cb649e81SQu Wenruo 		 *				|<- search range ->|
504cb649e81SQu Wenruo 		 *	|<- csum item ->|
505cb649e81SQu Wenruo 		 *
506cb649e81SQu Wenruo 		 * Check if the previous csum item covers the leading part of
507cb649e81SQu Wenruo 		 * the search range.  If so we have to start from previous csum
508cb649e81SQu Wenruo 		 * item.
509cb649e81SQu Wenruo 		 */
51017d217feSYan Zheng 		if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
51117d217feSYan Zheng 		    key.type == BTRFS_EXTENT_CSUM_KEY) {
512cb649e81SQu Wenruo 			if (bytes_to_csum_size(fs_info, start - key.offset) <
5133212fa14SJosef Bacik 			    btrfs_item_size(leaf, path->slots[0] - 1))
51417d217feSYan Zheng 				path->slots[0]--;
51517d217feSYan Zheng 		}
51617d217feSYan Zheng 	}
51717d217feSYan Zheng 
51817d217feSYan Zheng 	while (start <= end) {
519cb649e81SQu Wenruo 		u64 csum_end;
520cb649e81SQu Wenruo 
52117d217feSYan Zheng 		leaf = path->nodes[0];
52217d217feSYan Zheng 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
52307d400a6SYan Zheng 			ret = btrfs_next_leaf(root, path);
52417d217feSYan Zheng 			if (ret < 0)
52517d217feSYan Zheng 				goto fail;
52617d217feSYan Zheng 			if (ret > 0)
52717d217feSYan Zheng 				break;
52817d217feSYan Zheng 			leaf = path->nodes[0];
52917d217feSYan Zheng 		}
53017d217feSYan Zheng 
53117d217feSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
53217d217feSYan Zheng 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
533628c8282SZhi Yong Wu 		    key.type != BTRFS_EXTENT_CSUM_KEY ||
534628c8282SZhi Yong Wu 		    key.offset > end)
53517d217feSYan Zheng 			break;
53617d217feSYan Zheng 
53717d217feSYan Zheng 		if (key.offset > start)
53817d217feSYan Zheng 			start = key.offset;
53917d217feSYan Zheng 
540cb649e81SQu Wenruo 		csum_end = key.offset + csum_size_to_bytes(fs_info,
541cb649e81SQu Wenruo 					btrfs_item_size(leaf, path->slots[0]));
54287b29b20SYan Zheng 		if (csum_end <= start) {
54387b29b20SYan Zheng 			path->slots[0]++;
54487b29b20SYan Zheng 			continue;
54587b29b20SYan Zheng 		}
54617d217feSYan Zheng 
54707d400a6SYan Zheng 		csum_end = min(csum_end, end + 1);
54807d400a6SYan Zheng 		item = btrfs_item_ptr(path->nodes[0], path->slots[0],
54907d400a6SYan Zheng 				      struct btrfs_csum_item);
55007d400a6SYan Zheng 		while (start < csum_end) {
551cb649e81SQu Wenruo 			unsigned long offset;
552cb649e81SQu Wenruo 			size_t size;
553cb649e81SQu Wenruo 
55407d400a6SYan Zheng 			size = min_t(size_t, csum_end - start,
555cb649e81SQu Wenruo 				     max_ordered_sum_bytes(fs_info));
5560b246afaSJeff Mahoney 			sums = kzalloc(btrfs_ordered_sum_size(fs_info, size),
55707d400a6SYan Zheng 				       GFP_NOFS);
5580678b618SMark Fasheh 			if (!sums) {
5590678b618SMark Fasheh 				ret = -ENOMEM;
5600678b618SMark Fasheh 				goto fail;
5610678b618SMark Fasheh 			}
56217d217feSYan Zheng 
563*5cfe76f8SChristoph Hellwig 			sums->logical = start;
5646e4b2479SChristoph Hellwig 			sums->len = size;
56517d217feSYan Zheng 
566cb649e81SQu Wenruo 			offset = bytes_to_csum_size(fs_info, start - key.offset);
56717d217feSYan Zheng 
56807d400a6SYan Zheng 			read_extent_buffer(path->nodes[0],
569f51a4a18SMiao Xie 					   sums->sums,
570f51a4a18SMiao Xie 					   ((unsigned long)item) + offset,
571cb649e81SQu Wenruo 					   bytes_to_csum_size(fs_info, size));
57217d217feSYan Zheng 
573cb649e81SQu Wenruo 			start += size;
5740678b618SMark Fasheh 			list_add_tail(&sums->list, &tmplist);
57507d400a6SYan Zheng 		}
57617d217feSYan Zheng 		path->slots[0]++;
57717d217feSYan Zheng 	}
57817d217feSYan Zheng 	ret = 0;
57917d217feSYan Zheng fail:
5800678b618SMark Fasheh 	while (ret < 0 && !list_empty(&tmplist)) {
5816e5aafb2SChris Mason 		sums = list_entry(tmplist.next, struct btrfs_ordered_sum, list);
5820678b618SMark Fasheh 		list_del(&sums->list);
5830678b618SMark Fasheh 		kfree(sums);
5840678b618SMark Fasheh 	}
5850678b618SMark Fasheh 	list_splice_tail(&tmplist, list);
5860678b618SMark Fasheh 
58717d217feSYan Zheng 	btrfs_free_path(path);
58817d217feSYan Zheng 	return ret;
58917d217feSYan Zheng }
59017d217feSYan Zheng 
59143dd529aSDavid Sterba /*
59297e38239SQu Wenruo  * Do the same work as btrfs_lookup_csums_list(), the difference is in how
59397e38239SQu Wenruo  * we return the result.
59497e38239SQu Wenruo  *
59597e38239SQu Wenruo  * This version will set the corresponding bits in @csum_bitmap to represent
59697e38239SQu Wenruo  * that there is a csum found.
59797e38239SQu Wenruo  * Each bit represents a sector. Thus caller should ensure @csum_buf passed
59897e38239SQu Wenruo  * in is large enough to contain all csums.
59997e38239SQu Wenruo  */
60097e38239SQu Wenruo int btrfs_lookup_csums_bitmap(struct btrfs_root *root, u64 start, u64 end,
601b9795475SQu Wenruo 			      u8 *csum_buf, unsigned long *csum_bitmap,
602b9795475SQu Wenruo 			      bool search_commit)
60397e38239SQu Wenruo {
60497e38239SQu Wenruo 	struct btrfs_fs_info *fs_info = root->fs_info;
60597e38239SQu Wenruo 	struct btrfs_key key;
60697e38239SQu Wenruo 	struct btrfs_path *path;
60797e38239SQu Wenruo 	struct extent_buffer *leaf;
60897e38239SQu Wenruo 	struct btrfs_csum_item *item;
60997e38239SQu Wenruo 	const u64 orig_start = start;
61097e38239SQu Wenruo 	int ret;
61197e38239SQu Wenruo 
61297e38239SQu Wenruo 	ASSERT(IS_ALIGNED(start, fs_info->sectorsize) &&
61397e38239SQu Wenruo 	       IS_ALIGNED(end + 1, fs_info->sectorsize));
61497e38239SQu Wenruo 
61597e38239SQu Wenruo 	path = btrfs_alloc_path();
61697e38239SQu Wenruo 	if (!path)
61797e38239SQu Wenruo 		return -ENOMEM;
61897e38239SQu Wenruo 
619b9795475SQu Wenruo 	if (search_commit) {
620b9795475SQu Wenruo 		path->skip_locking = 1;
621b9795475SQu Wenruo 		path->reada = READA_FORWARD;
622b9795475SQu Wenruo 		path->search_commit_root = 1;
623b9795475SQu Wenruo 	}
624b9795475SQu Wenruo 
62597e38239SQu Wenruo 	key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
62697e38239SQu Wenruo 	key.type = BTRFS_EXTENT_CSUM_KEY;
62797e38239SQu Wenruo 	key.offset = start;
62897e38239SQu Wenruo 
62997e38239SQu Wenruo 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
63097e38239SQu Wenruo 	if (ret < 0)
63197e38239SQu Wenruo 		goto fail;
63297e38239SQu Wenruo 	if (ret > 0 && path->slots[0] > 0) {
63397e38239SQu Wenruo 		leaf = path->nodes[0];
63497e38239SQu Wenruo 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
63597e38239SQu Wenruo 
63697e38239SQu Wenruo 		/*
63797e38239SQu Wenruo 		 * There are two cases we can hit here for the previous csum
63897e38239SQu Wenruo 		 * item:
63997e38239SQu Wenruo 		 *
64097e38239SQu Wenruo 		 *		|<- search range ->|
64197e38239SQu Wenruo 		 *	|<- csum item ->|
64297e38239SQu Wenruo 		 *
64397e38239SQu Wenruo 		 * Or
64497e38239SQu Wenruo 		 *				|<- search range ->|
64597e38239SQu Wenruo 		 *	|<- csum item ->|
64697e38239SQu Wenruo 		 *
64797e38239SQu Wenruo 		 * Check if the previous csum item covers the leading part of
64897e38239SQu Wenruo 		 * the search range.  If so we have to start from previous csum
64997e38239SQu Wenruo 		 * item.
65097e38239SQu Wenruo 		 */
65197e38239SQu Wenruo 		if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
65297e38239SQu Wenruo 		    key.type == BTRFS_EXTENT_CSUM_KEY) {
65397e38239SQu Wenruo 			if (bytes_to_csum_size(fs_info, start - key.offset) <
65497e38239SQu Wenruo 			    btrfs_item_size(leaf, path->slots[0] - 1))
65597e38239SQu Wenruo 				path->slots[0]--;
65697e38239SQu Wenruo 		}
65797e38239SQu Wenruo 	}
65897e38239SQu Wenruo 
65997e38239SQu Wenruo 	while (start <= end) {
66097e38239SQu Wenruo 		u64 csum_end;
66197e38239SQu Wenruo 
66297e38239SQu Wenruo 		leaf = path->nodes[0];
66397e38239SQu Wenruo 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
66497e38239SQu Wenruo 			ret = btrfs_next_leaf(root, path);
66597e38239SQu Wenruo 			if (ret < 0)
66697e38239SQu Wenruo 				goto fail;
66797e38239SQu Wenruo 			if (ret > 0)
66897e38239SQu Wenruo 				break;
66997e38239SQu Wenruo 			leaf = path->nodes[0];
67097e38239SQu Wenruo 		}
67197e38239SQu Wenruo 
67297e38239SQu Wenruo 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
67397e38239SQu Wenruo 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
67497e38239SQu Wenruo 		    key.type != BTRFS_EXTENT_CSUM_KEY ||
67597e38239SQu Wenruo 		    key.offset > end)
67697e38239SQu Wenruo 			break;
67797e38239SQu Wenruo 
67897e38239SQu Wenruo 		if (key.offset > start)
67997e38239SQu Wenruo 			start = key.offset;
68097e38239SQu Wenruo 
68197e38239SQu Wenruo 		csum_end = key.offset + csum_size_to_bytes(fs_info,
68297e38239SQu Wenruo 					btrfs_item_size(leaf, path->slots[0]));
68397e38239SQu Wenruo 		if (csum_end <= start) {
68497e38239SQu Wenruo 			path->slots[0]++;
68597e38239SQu Wenruo 			continue;
68697e38239SQu Wenruo 		}
68797e38239SQu Wenruo 
68897e38239SQu Wenruo 		csum_end = min(csum_end, end + 1);
68997e38239SQu Wenruo 		item = btrfs_item_ptr(path->nodes[0], path->slots[0],
69097e38239SQu Wenruo 				      struct btrfs_csum_item);
69197e38239SQu Wenruo 		while (start < csum_end) {
69297e38239SQu Wenruo 			unsigned long offset;
69397e38239SQu Wenruo 			size_t size;
69497e38239SQu Wenruo 			u8 *csum_dest = csum_buf + bytes_to_csum_size(fs_info,
69597e38239SQu Wenruo 						start - orig_start);
69697e38239SQu Wenruo 
69797e38239SQu Wenruo 			size = min_t(size_t, csum_end - start, end + 1 - start);
69897e38239SQu Wenruo 
69997e38239SQu Wenruo 			offset = bytes_to_csum_size(fs_info, start - key.offset);
70097e38239SQu Wenruo 
70197e38239SQu Wenruo 			read_extent_buffer(path->nodes[0], csum_dest,
70297e38239SQu Wenruo 					   ((unsigned long)item) + offset,
70397e38239SQu Wenruo 					   bytes_to_csum_size(fs_info, size));
70497e38239SQu Wenruo 
70597e38239SQu Wenruo 			bitmap_set(csum_bitmap,
70697e38239SQu Wenruo 				(start - orig_start) >> fs_info->sectorsize_bits,
70797e38239SQu Wenruo 				size >> fs_info->sectorsize_bits);
70897e38239SQu Wenruo 
70997e38239SQu Wenruo 			start += size;
71097e38239SQu Wenruo 		}
71197e38239SQu Wenruo 		path->slots[0]++;
71297e38239SQu Wenruo 	}
71397e38239SQu Wenruo 	ret = 0;
71497e38239SQu Wenruo fail:
71597e38239SQu Wenruo 	btrfs_free_path(path);
71697e38239SQu Wenruo 	return ret;
71797e38239SQu Wenruo }
71897e38239SQu Wenruo 
71997e38239SQu Wenruo /*
72043dd529aSDavid Sterba  * Calculate checksums of the data contained inside a bio.
72151d470aeSNikolay Borisov  */
722f8c44673SChristoph Hellwig blk_status_t btrfs_csum_one_bio(struct btrfs_bio *bbio)
723e015640fSChris Mason {
724f8c44673SChristoph Hellwig 	struct btrfs_inode *inode = bbio->inode;
725c3504372SNikolay Borisov 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
726d5178578SJohannes Thumshirn 	SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
727f8c44673SChristoph Hellwig 	struct bio *bio = &bbio->bio;
728f8c44673SChristoph Hellwig 	u64 offset = bbio->file_offset;
729e6dcd2dcSChris Mason 	struct btrfs_ordered_sum *sums;
7306cd7ce49SChristoph Hellwig 	struct btrfs_ordered_extent *ordered = NULL;
731e015640fSChris Mason 	char *data;
73217347cecSLiu Bo 	struct bvec_iter iter;
73317347cecSLiu Bo 	struct bio_vec bvec;
734f51a4a18SMiao Xie 	int index;
735e331f6b1SOmar Sandoval 	unsigned int blockcount;
7363edf7d33SChris Mason 	unsigned long total_bytes = 0;
7373edf7d33SChris Mason 	unsigned long this_sum_bytes = 0;
73817347cecSLiu Bo 	int i;
739a3d46aeaSNikolay Borisov 	unsigned nofs_flag;
740e015640fSChris Mason 
741a3d46aeaSNikolay Borisov 	nofs_flag = memalloc_nofs_save();
742a3d46aeaSNikolay Borisov 	sums = kvzalloc(btrfs_ordered_sum_size(fs_info, bio->bi_iter.bi_size),
743a3d46aeaSNikolay Borisov 		       GFP_KERNEL);
744a3d46aeaSNikolay Borisov 	memalloc_nofs_restore(nofs_flag);
745a3d46aeaSNikolay Borisov 
746e015640fSChris Mason 	if (!sums)
7474e4cbee9SChristoph Hellwig 		return BLK_STS_RESOURCE;
7483edf7d33SChris Mason 
7494f024f37SKent Overstreet 	sums->len = bio->bi_iter.bi_size;
750e6dcd2dcSChris Mason 	INIT_LIST_HEAD(&sums->list);
751d20f7043SChris Mason 
752*5cfe76f8SChristoph Hellwig 	sums->logical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
753f51a4a18SMiao Xie 	index = 0;
754e015640fSChris Mason 
755d5178578SJohannes Thumshirn 	shash->tfm = fs_info->csum_shash;
756d5178578SJohannes Thumshirn 
75717347cecSLiu Bo 	bio_for_each_segment(bvec, bio, iter) {
7586cd7ce49SChristoph Hellwig 		if (!ordered) {
7596cd7ce49SChristoph Hellwig 			ordered = btrfs_lookup_ordered_extent(inode, offset);
760bbc9a6ebSQu Wenruo 			/*
761bbc9a6ebSQu Wenruo 			 * The bio range is not covered by any ordered extent,
762bbc9a6ebSQu Wenruo 			 * must be a code logic error.
763bbc9a6ebSQu Wenruo 			 */
764bbc9a6ebSQu Wenruo 			if (unlikely(!ordered)) {
765bbc9a6ebSQu Wenruo 				WARN(1, KERN_WARNING
766bbc9a6ebSQu Wenruo 			"no ordered extent for root %llu ino %llu offset %llu\n",
767bbc9a6ebSQu Wenruo 				     inode->root->root_key.objectid,
768bbc9a6ebSQu Wenruo 				     btrfs_ino(inode), offset);
769bbc9a6ebSQu Wenruo 				kvfree(sums);
770bbc9a6ebSQu Wenruo 				return BLK_STS_IOERR;
771bbc9a6ebSQu Wenruo 			}
7726cd7ce49SChristoph Hellwig 		}
7736cd7ce49SChristoph Hellwig 
774e331f6b1SOmar Sandoval 		blockcount = BTRFS_BYTES_TO_BLKS(fs_info,
77517347cecSLiu Bo 						 bvec.bv_len + fs_info->sectorsize
776c40a3d38SChandan Rajendra 						 - 1);
777c40a3d38SChandan Rajendra 
778e331f6b1SOmar Sandoval 		for (i = 0; i < blockcount; i++) {
779f8c44673SChristoph Hellwig 			if (!(bio->bi_opf & REQ_BTRFS_ONE_ORDERED) &&
780e331f6b1SOmar Sandoval 			    !in_range(offset, ordered->file_offset,
781e331f6b1SOmar Sandoval 				      ordered->num_bytes)) {
7823edf7d33SChris Mason 				unsigned long bytes_left;
783c40a3d38SChandan Rajendra 
7843edf7d33SChris Mason 				sums->len = this_sum_bytes;
7853edf7d33SChris Mason 				this_sum_bytes = 0;
786f9756261SNikolay Borisov 				btrfs_add_ordered_sum(ordered, sums);
7873edf7d33SChris Mason 				btrfs_put_ordered_extent(ordered);
7883edf7d33SChris Mason 
7894f024f37SKent Overstreet 				bytes_left = bio->bi_iter.bi_size - total_bytes;
7903edf7d33SChris Mason 
791a3d46aeaSNikolay Borisov 				nofs_flag = memalloc_nofs_save();
792a3d46aeaSNikolay Borisov 				sums = kvzalloc(btrfs_ordered_sum_size(fs_info,
793a3d46aeaSNikolay Borisov 						      bytes_left), GFP_KERNEL);
794a3d46aeaSNikolay Borisov 				memalloc_nofs_restore(nofs_flag);
795806570c0SJohannes Thumshirn 				if (!sums)
796806570c0SJohannes Thumshirn 					return BLK_STS_RESOURCE;
797806570c0SJohannes Thumshirn 
7983edf7d33SChris Mason 				sums->len = bytes_left;
799c40a3d38SChandan Rajendra 				ordered = btrfs_lookup_ordered_extent(inode,
800c40a3d38SChandan Rajendra 								offset);
801c40a3d38SChandan Rajendra 				ASSERT(ordered); /* Logic error */
802*5cfe76f8SChristoph Hellwig 				sums->logical = (bio->bi_iter.bi_sector << SECTOR_SHIFT)
803c40a3d38SChandan Rajendra 					+ total_bytes;
804f51a4a18SMiao Xie 				index = 0;
805c40a3d38SChandan Rajendra 			}
806c40a3d38SChandan Rajendra 
8073dcfbcceSChristoph Hellwig 			data = bvec_kmap_local(&bvec);
8083dcfbcceSChristoph Hellwig 			crypto_shash_digest(shash,
8093dcfbcceSChristoph Hellwig 					    data + (i * fs_info->sectorsize),
810fd08001fSEric Biggers 					    fs_info->sectorsize,
811fd08001fSEric Biggers 					    sums->sums + index);
8123dcfbcceSChristoph Hellwig 			kunmap_local(data);
813713cebfbSDavid Sterba 			index += fs_info->csum_size;
8140b246afaSJeff Mahoney 			offset += fs_info->sectorsize;
8150b246afaSJeff Mahoney 			this_sum_bytes += fs_info->sectorsize;
8160b246afaSJeff Mahoney 			total_bytes += fs_info->sectorsize;
817c40a3d38SChandan Rajendra 		}
818c40a3d38SChandan Rajendra 
819e015640fSChris Mason 	}
820ed98b56aSChris Mason 	this_sum_bytes = 0;
821f9756261SNikolay Borisov 	btrfs_add_ordered_sum(ordered, sums);
8223edf7d33SChris Mason 	btrfs_put_ordered_extent(ordered);
823e015640fSChris Mason 	return 0;
824e015640fSChris Mason }
825e015640fSChris Mason 
826459931ecSChris Mason /*
82743dd529aSDavid Sterba  * Remove one checksum overlapping a range.
828459931ecSChris Mason  *
82943dd529aSDavid Sterba  * This expects the key to describe the csum pointed to by the path, and it
83043dd529aSDavid Sterba  * expects the csum to overlap the range [bytenr, len]
831459931ecSChris Mason  *
83243dd529aSDavid Sterba  * The csum should not be entirely contained in the range and the range should
83343dd529aSDavid Sterba  * not be entirely contained in the csum.
83443dd529aSDavid Sterba  *
83543dd529aSDavid Sterba  * This calls btrfs_truncate_item with the correct args based on the overlap,
83643dd529aSDavid Sterba  * and fixes up the key as required.
837459931ecSChris Mason  */
8382ff7e61eSJeff Mahoney static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info,
839459931ecSChris Mason 				       struct btrfs_path *path,
840459931ecSChris Mason 				       struct btrfs_key *key,
841459931ecSChris Mason 				       u64 bytenr, u64 len)
842459931ecSChris Mason {
843459931ecSChris Mason 	struct extent_buffer *leaf;
844223486c2SDavid Sterba 	const u32 csum_size = fs_info->csum_size;
845459931ecSChris Mason 	u64 csum_end;
846459931ecSChris Mason 	u64 end_byte = bytenr + len;
847265fdfa6SDavid Sterba 	u32 blocksize_bits = fs_info->sectorsize_bits;
848459931ecSChris Mason 
849459931ecSChris Mason 	leaf = path->nodes[0];
8503212fa14SJosef Bacik 	csum_end = btrfs_item_size(leaf, path->slots[0]) / csum_size;
851265fdfa6SDavid Sterba 	csum_end <<= blocksize_bits;
852459931ecSChris Mason 	csum_end += key->offset;
853459931ecSChris Mason 
854459931ecSChris Mason 	if (key->offset < bytenr && csum_end <= end_byte) {
855459931ecSChris Mason 		/*
856459931ecSChris Mason 		 *         [ bytenr - len ]
857459931ecSChris Mason 		 *         [   ]
858459931ecSChris Mason 		 *   [csum     ]
859459931ecSChris Mason 		 *   A simple truncate off the end of the item
860459931ecSChris Mason 		 */
861459931ecSChris Mason 		u32 new_size = (bytenr - key->offset) >> blocksize_bits;
862459931ecSChris Mason 		new_size *= csum_size;
86378ac4f9eSDavid Sterba 		btrfs_truncate_item(path, new_size, 1);
864459931ecSChris Mason 	} else if (key->offset >= bytenr && csum_end > end_byte &&
865459931ecSChris Mason 		   end_byte > key->offset) {
866459931ecSChris Mason 		/*
867459931ecSChris Mason 		 *         [ bytenr - len ]
868459931ecSChris Mason 		 *                 [ ]
869459931ecSChris Mason 		 *                 [csum     ]
870459931ecSChris Mason 		 * we need to truncate from the beginning of the csum
871459931ecSChris Mason 		 */
872459931ecSChris Mason 		u32 new_size = (csum_end - end_byte) >> blocksize_bits;
873459931ecSChris Mason 		new_size *= csum_size;
874459931ecSChris Mason 
87578ac4f9eSDavid Sterba 		btrfs_truncate_item(path, new_size, 0);
876459931ecSChris Mason 
877459931ecSChris Mason 		key->offset = end_byte;
8780b246afaSJeff Mahoney 		btrfs_set_item_key_safe(fs_info, path, key);
879459931ecSChris Mason 	} else {
880459931ecSChris Mason 		BUG();
881459931ecSChris Mason 	}
882459931ecSChris Mason }
883459931ecSChris Mason 
884459931ecSChris Mason /*
88543dd529aSDavid Sterba  * Delete the csum items from the csum tree for a given range of bytes.
886459931ecSChris Mason  */
887459931ecSChris Mason int btrfs_del_csums(struct btrfs_trans_handle *trans,
88840e046acSFilipe Manana 		    struct btrfs_root *root, u64 bytenr, u64 len)
889459931ecSChris Mason {
89040e046acSFilipe Manana 	struct btrfs_fs_info *fs_info = trans->fs_info;
891459931ecSChris Mason 	struct btrfs_path *path;
892459931ecSChris Mason 	struct btrfs_key key;
893459931ecSChris Mason 	u64 end_byte = bytenr + len;
894459931ecSChris Mason 	u64 csum_end;
895459931ecSChris Mason 	struct extent_buffer *leaf;
896b86652beSJosef Bacik 	int ret = 0;
897223486c2SDavid Sterba 	const u32 csum_size = fs_info->csum_size;
898265fdfa6SDavid Sterba 	u32 blocksize_bits = fs_info->sectorsize_bits;
899459931ecSChris Mason 
90084d2d6c7SJosef Bacik 	ASSERT(root->root_key.objectid == BTRFS_CSUM_TREE_OBJECTID ||
90140e046acSFilipe Manana 	       root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
90240e046acSFilipe Manana 
903459931ecSChris Mason 	path = btrfs_alloc_path();
9042a29edc6Sliubo 	if (!path)
9052a29edc6Sliubo 		return -ENOMEM;
906459931ecSChris Mason 
907459931ecSChris Mason 	while (1) {
908459931ecSChris Mason 		key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
909459931ecSChris Mason 		key.offset = end_byte - 1;
910459931ecSChris Mason 		key.type = BTRFS_EXTENT_CSUM_KEY;
911459931ecSChris Mason 
912459931ecSChris Mason 		ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
913459931ecSChris Mason 		if (ret > 0) {
914b86652beSJosef Bacik 			ret = 0;
915459931ecSChris Mason 			if (path->slots[0] == 0)
91665a246c5STsutomu Itoh 				break;
917459931ecSChris Mason 			path->slots[0]--;
918ad0397a7SJosef Bacik 		} else if (ret < 0) {
91965a246c5STsutomu Itoh 			break;
920459931ecSChris Mason 		}
921ad0397a7SJosef Bacik 
922459931ecSChris Mason 		leaf = path->nodes[0];
923459931ecSChris Mason 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
924459931ecSChris Mason 
925459931ecSChris Mason 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
926459931ecSChris Mason 		    key.type != BTRFS_EXTENT_CSUM_KEY) {
927459931ecSChris Mason 			break;
928459931ecSChris Mason 		}
929459931ecSChris Mason 
930459931ecSChris Mason 		if (key.offset >= end_byte)
931459931ecSChris Mason 			break;
932459931ecSChris Mason 
9333212fa14SJosef Bacik 		csum_end = btrfs_item_size(leaf, path->slots[0]) / csum_size;
934459931ecSChris Mason 		csum_end <<= blocksize_bits;
935459931ecSChris Mason 		csum_end += key.offset;
936459931ecSChris Mason 
937459931ecSChris Mason 		/* this csum ends before we start, we're done */
938459931ecSChris Mason 		if (csum_end <= bytenr)
939459931ecSChris Mason 			break;
940459931ecSChris Mason 
941459931ecSChris Mason 		/* delete the entire item, it is inside our range */
942459931ecSChris Mason 		if (key.offset >= bytenr && csum_end <= end_byte) {
9436f546216SFilipe Manana 			int del_nr = 1;
9446f546216SFilipe Manana 
9456f546216SFilipe Manana 			/*
9466f546216SFilipe Manana 			 * Check how many csum items preceding this one in this
9476f546216SFilipe Manana 			 * leaf correspond to our range and then delete them all
9486f546216SFilipe Manana 			 * at once.
9496f546216SFilipe Manana 			 */
9506f546216SFilipe Manana 			if (key.offset > bytenr && path->slots[0] > 0) {
9516f546216SFilipe Manana 				int slot = path->slots[0] - 1;
9526f546216SFilipe Manana 
9536f546216SFilipe Manana 				while (slot >= 0) {
9546f546216SFilipe Manana 					struct btrfs_key pk;
9556f546216SFilipe Manana 
9566f546216SFilipe Manana 					btrfs_item_key_to_cpu(leaf, &pk, slot);
9576f546216SFilipe Manana 					if (pk.offset < bytenr ||
9586f546216SFilipe Manana 					    pk.type != BTRFS_EXTENT_CSUM_KEY ||
9596f546216SFilipe Manana 					    pk.objectid !=
9606f546216SFilipe Manana 					    BTRFS_EXTENT_CSUM_OBJECTID)
9616f546216SFilipe Manana 						break;
9626f546216SFilipe Manana 					path->slots[0] = slot;
9636f546216SFilipe Manana 					del_nr++;
9646f546216SFilipe Manana 					key.offset = pk.offset;
9656f546216SFilipe Manana 					slot--;
9666f546216SFilipe Manana 				}
9676f546216SFilipe Manana 			}
9686f546216SFilipe Manana 			ret = btrfs_del_items(trans, root, path,
9696f546216SFilipe Manana 					      path->slots[0], del_nr);
97065a246c5STsutomu Itoh 			if (ret)
971b86652beSJosef Bacik 				break;
972dcbdd4dcSChris Mason 			if (key.offset == bytenr)
973dcbdd4dcSChris Mason 				break;
974459931ecSChris Mason 		} else if (key.offset < bytenr && csum_end > end_byte) {
975459931ecSChris Mason 			unsigned long offset;
976459931ecSChris Mason 			unsigned long shift_len;
977459931ecSChris Mason 			unsigned long item_offset;
978459931ecSChris Mason 			/*
979459931ecSChris Mason 			 *        [ bytenr - len ]
980459931ecSChris Mason 			 *     [csum                ]
981459931ecSChris Mason 			 *
982459931ecSChris Mason 			 * Our bytes are in the middle of the csum,
983459931ecSChris Mason 			 * we need to split this item and insert a new one.
984459931ecSChris Mason 			 *
985459931ecSChris Mason 			 * But we can't drop the path because the
986459931ecSChris Mason 			 * csum could change, get removed, extended etc.
987459931ecSChris Mason 			 *
988459931ecSChris Mason 			 * The trick here is the max size of a csum item leaves
989459931ecSChris Mason 			 * enough room in the tree block for a single
990459931ecSChris Mason 			 * item header.  So, we split the item in place,
991459931ecSChris Mason 			 * adding a new header pointing to the existing
992459931ecSChris Mason 			 * bytes.  Then we loop around again and we have
993459931ecSChris Mason 			 * a nicely formed csum item that we can neatly
994459931ecSChris Mason 			 * truncate.
995459931ecSChris Mason 			 */
996459931ecSChris Mason 			offset = (bytenr - key.offset) >> blocksize_bits;
997459931ecSChris Mason 			offset *= csum_size;
998459931ecSChris Mason 
999459931ecSChris Mason 			shift_len = (len >> blocksize_bits) * csum_size;
1000459931ecSChris Mason 
1001459931ecSChris Mason 			item_offset = btrfs_item_ptr_offset(leaf,
1002459931ecSChris Mason 							    path->slots[0]);
1003459931ecSChris Mason 
1004b159fa28SDavid Sterba 			memzero_extent_buffer(leaf, item_offset + offset,
1005459931ecSChris Mason 					     shift_len);
1006459931ecSChris Mason 			key.offset = bytenr;
1007459931ecSChris Mason 
1008459931ecSChris Mason 			/*
1009459931ecSChris Mason 			 * btrfs_split_item returns -EAGAIN when the
1010459931ecSChris Mason 			 * item changed size or key
1011459931ecSChris Mason 			 */
1012459931ecSChris Mason 			ret = btrfs_split_item(trans, root, path, &key, offset);
101379787eaaSJeff Mahoney 			if (ret && ret != -EAGAIN) {
101466642832SJeff Mahoney 				btrfs_abort_transaction(trans, ret);
1015b86652beSJosef Bacik 				break;
101679787eaaSJeff Mahoney 			}
1017b86652beSJosef Bacik 			ret = 0;
1018459931ecSChris Mason 
1019459931ecSChris Mason 			key.offset = end_byte - 1;
1020459931ecSChris Mason 		} else {
10212ff7e61eSJeff Mahoney 			truncate_one_csum(fs_info, path, &key, bytenr, len);
1022dcbdd4dcSChris Mason 			if (key.offset < bytenr)
1023dcbdd4dcSChris Mason 				break;
1024459931ecSChris Mason 		}
1025b3b4aa74SDavid Sterba 		btrfs_release_path(path);
1026459931ecSChris Mason 	}
1027459931ecSChris Mason 	btrfs_free_path(path);
102865a246c5STsutomu Itoh 	return ret;
1029459931ecSChris Mason }
1030459931ecSChris Mason 
1031ea7036deSFilipe Manana static int find_next_csum_offset(struct btrfs_root *root,
1032ea7036deSFilipe Manana 				 struct btrfs_path *path,
1033ea7036deSFilipe Manana 				 u64 *next_offset)
1034ea7036deSFilipe Manana {
1035ea7036deSFilipe Manana 	const u32 nritems = btrfs_header_nritems(path->nodes[0]);
1036ea7036deSFilipe Manana 	struct btrfs_key found_key;
1037ea7036deSFilipe Manana 	int slot = path->slots[0] + 1;
1038ea7036deSFilipe Manana 	int ret;
1039ea7036deSFilipe Manana 
1040ea7036deSFilipe Manana 	if (nritems == 0 || slot >= nritems) {
1041ea7036deSFilipe Manana 		ret = btrfs_next_leaf(root, path);
1042ea7036deSFilipe Manana 		if (ret < 0) {
1043ea7036deSFilipe Manana 			return ret;
1044ea7036deSFilipe Manana 		} else if (ret > 0) {
1045ea7036deSFilipe Manana 			*next_offset = (u64)-1;
1046ea7036deSFilipe Manana 			return 0;
1047ea7036deSFilipe Manana 		}
1048ea7036deSFilipe Manana 		slot = path->slots[0];
1049ea7036deSFilipe Manana 	}
1050ea7036deSFilipe Manana 
1051ea7036deSFilipe Manana 	btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
1052ea7036deSFilipe Manana 
1053ea7036deSFilipe Manana 	if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1054ea7036deSFilipe Manana 	    found_key.type != BTRFS_EXTENT_CSUM_KEY)
1055ea7036deSFilipe Manana 		*next_offset = (u64)-1;
1056ea7036deSFilipe Manana 	else
1057ea7036deSFilipe Manana 		*next_offset = found_key.offset;
1058ea7036deSFilipe Manana 
1059ea7036deSFilipe Manana 	return 0;
1060ea7036deSFilipe Manana }
1061ea7036deSFilipe Manana 
1062065631f6SChris Mason int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
1063d20f7043SChris Mason 			   struct btrfs_root *root,
1064e6dcd2dcSChris Mason 			   struct btrfs_ordered_sum *sums)
1065f254e52cSChris Mason {
10660b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
1067f254e52cSChris Mason 	struct btrfs_key file_key;
10686567e837SChris Mason 	struct btrfs_key found_key;
10695caf2a00SChris Mason 	struct btrfs_path *path;
1070f254e52cSChris Mason 	struct btrfs_csum_item *item;
1071065631f6SChris Mason 	struct btrfs_csum_item *item_end;
1072ff79f819SChris Mason 	struct extent_buffer *leaf = NULL;
1073f51a4a18SMiao Xie 	u64 next_offset;
1074f51a4a18SMiao Xie 	u64 total_bytes = 0;
10756567e837SChris Mason 	u64 csum_offset;
1076f51a4a18SMiao Xie 	u64 bytenr;
1077f578d4bdSChris Mason 	u32 ins_size;
1078f51a4a18SMiao Xie 	int index = 0;
1079f51a4a18SMiao Xie 	int found_next;
1080f51a4a18SMiao Xie 	int ret;
1081223486c2SDavid Sterba 	const u32 csum_size = fs_info->csum_size;
10826e92f5e6SChris Mason 
10835caf2a00SChris Mason 	path = btrfs_alloc_path();
1084d8926bb3SMark Fasheh 	if (!path)
1085d8926bb3SMark Fasheh 		return -ENOMEM;
1086065631f6SChris Mason again:
1087065631f6SChris Mason 	next_offset = (u64)-1;
1088065631f6SChris Mason 	found_next = 0;
1089*5cfe76f8SChristoph Hellwig 	bytenr = sums->logical + total_bytes;
1090d20f7043SChris Mason 	file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1091f51a4a18SMiao Xie 	file_key.offset = bytenr;
1092962a298fSDavid Sterba 	file_key.type = BTRFS_EXTENT_CSUM_KEY;
1093a429e513SChris Mason 
1094f51a4a18SMiao Xie 	item = btrfs_lookup_csum(trans, root, path, bytenr, 1);
1095ff79f819SChris Mason 	if (!IS_ERR(item)) {
1096639cb586SChris Mason 		ret = 0;
1097f51a4a18SMiao Xie 		leaf = path->nodes[0];
1098f51a4a18SMiao Xie 		item_end = btrfs_item_ptr(leaf, path->slots[0],
1099f51a4a18SMiao Xie 					  struct btrfs_csum_item);
1100f51a4a18SMiao Xie 		item_end = (struct btrfs_csum_item *)((char *)item_end +
11013212fa14SJosef Bacik 			   btrfs_item_size(leaf, path->slots[0]));
1102a429e513SChris Mason 		goto found;
1103ff79f819SChris Mason 	}
1104a429e513SChris Mason 	ret = PTR_ERR(item);
11054a500fd1SYan, Zheng 	if (ret != -EFBIG && ret != -ENOENT)
1106918cdf44SFilipe Manana 		goto out;
11074a500fd1SYan, Zheng 
1108a429e513SChris Mason 	if (ret == -EFBIG) {
1109a429e513SChris Mason 		u32 item_size;
1110a429e513SChris Mason 		/* we found one, but it isn't big enough yet */
11115f39d397SChris Mason 		leaf = path->nodes[0];
11123212fa14SJosef Bacik 		item_size = btrfs_item_size(leaf, path->slots[0]);
1113607d432dSJosef Bacik 		if ((item_size / csum_size) >=
11140b246afaSJeff Mahoney 		    MAX_CSUM_ITEMS(fs_info, csum_size)) {
1115a429e513SChris Mason 			/* already at max size, make a new one */
1116a429e513SChris Mason 			goto insert;
1117a429e513SChris Mason 		}
1118a429e513SChris Mason 	} else {
1119ea7036deSFilipe Manana 		/* We didn't find a csum item, insert one. */
1120ea7036deSFilipe Manana 		ret = find_next_csum_offset(root, path, &next_offset);
1121ea7036deSFilipe Manana 		if (ret < 0)
11227e4a3f7eSFilipe Manana 			goto out;
1123f578d4bdSChris Mason 		found_next = 1;
1124a429e513SChris Mason 		goto insert;
1125a429e513SChris Mason 	}
1126a429e513SChris Mason 
1127a429e513SChris Mason 	/*
1128cc14600cSFilipe Manana 	 * At this point, we know the tree has a checksum item that ends at an
1129cc14600cSFilipe Manana 	 * offset matching the start of the checksum range we want to insert.
1130cc14600cSFilipe Manana 	 * We try to extend that item as much as possible and then add as many
1131cc14600cSFilipe Manana 	 * checksums to it as they fit.
1132cc14600cSFilipe Manana 	 *
1133cc14600cSFilipe Manana 	 * First check if the leaf has enough free space for at least one
1134cc14600cSFilipe Manana 	 * checksum. If it has go directly to the item extension code, otherwise
1135cc14600cSFilipe Manana 	 * release the path and do a search for insertion before the extension.
1136a429e513SChris Mason 	 */
1137cc14600cSFilipe Manana 	if (btrfs_leaf_free_space(leaf) >= csum_size) {
1138cc14600cSFilipe Manana 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1139cc14600cSFilipe Manana 		csum_offset = (bytenr - found_key.offset) >>
1140265fdfa6SDavid Sterba 			fs_info->sectorsize_bits;
1141cc14600cSFilipe Manana 		goto extend_csum;
1142cc14600cSFilipe Manana 	}
1143cc14600cSFilipe Manana 
1144b3b4aa74SDavid Sterba 	btrfs_release_path(path);
11459a664971Sethanwu 	path->search_for_extension = 1;
11466567e837SChris Mason 	ret = btrfs_search_slot(trans, root, &file_key, path,
1147607d432dSJosef Bacik 				csum_size, 1);
11489a664971Sethanwu 	path->search_for_extension = 0;
11496567e837SChris Mason 	if (ret < 0)
1150918cdf44SFilipe Manana 		goto out;
1151459931ecSChris Mason 
1152459931ecSChris Mason 	if (ret > 0) {
1153459931ecSChris Mason 		if (path->slots[0] == 0)
11546567e837SChris Mason 			goto insert;
11556567e837SChris Mason 		path->slots[0]--;
1156459931ecSChris Mason 	}
1157459931ecSChris Mason 
11585f39d397SChris Mason 	leaf = path->nodes[0];
11595f39d397SChris Mason 	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1160265fdfa6SDavid Sterba 	csum_offset = (bytenr - found_key.offset) >> fs_info->sectorsize_bits;
1161459931ecSChris Mason 
1162962a298fSDavid Sterba 	if (found_key.type != BTRFS_EXTENT_CSUM_KEY ||
1163d20f7043SChris Mason 	    found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
11640b246afaSJeff Mahoney 	    csum_offset >= MAX_CSUM_ITEMS(fs_info, csum_size)) {
11656567e837SChris Mason 		goto insert;
11666567e837SChris Mason 	}
1167459931ecSChris Mason 
1168cc14600cSFilipe Manana extend_csum:
11693212fa14SJosef Bacik 	if (csum_offset == btrfs_item_size(leaf, path->slots[0]) /
1170607d432dSJosef Bacik 	    csum_size) {
11712f697dc6SLiu Bo 		int extend_nr;
11722f697dc6SLiu Bo 		u64 tmp;
11732f697dc6SLiu Bo 		u32 diff;
1174459931ecSChris Mason 
1175f51a4a18SMiao Xie 		tmp = sums->len - total_bytes;
1176265fdfa6SDavid Sterba 		tmp >>= fs_info->sectorsize_bits;
11772f697dc6SLiu Bo 		WARN_ON(tmp < 1);
1178ea7036deSFilipe Manana 		extend_nr = max_t(int, 1, tmp);
11792f697dc6SLiu Bo 
1180ea7036deSFilipe Manana 		/*
1181ea7036deSFilipe Manana 		 * A log tree can already have checksum items with a subset of
1182ea7036deSFilipe Manana 		 * the checksums we are trying to log. This can happen after
1183ea7036deSFilipe Manana 		 * doing a sequence of partial writes into prealloc extents and
1184ea7036deSFilipe Manana 		 * fsyncs in between, with a full fsync logging a larger subrange
1185ea7036deSFilipe Manana 		 * of an extent for which a previous fast fsync logged a smaller
1186ea7036deSFilipe Manana 		 * subrange. And this happens in particular due to merging file
1187ea7036deSFilipe Manana 		 * extent items when we complete an ordered extent for a range
1188ea7036deSFilipe Manana 		 * covered by a prealloc extent - this is done at
1189ea7036deSFilipe Manana 		 * btrfs_mark_extent_written().
1190ea7036deSFilipe Manana 		 *
1191ea7036deSFilipe Manana 		 * So if we try to extend the previous checksum item, which has
1192ea7036deSFilipe Manana 		 * a range that ends at the start of the range we want to insert,
1193ea7036deSFilipe Manana 		 * make sure we don't extend beyond the start offset of the next
1194ea7036deSFilipe Manana 		 * checksum item. If we are at the last item in the leaf, then
1195ea7036deSFilipe Manana 		 * forget the optimization of extending and add a new checksum
1196ea7036deSFilipe Manana 		 * item - it is not worth the complexity of releasing the path,
1197ea7036deSFilipe Manana 		 * getting the first key for the next leaf, repeat the btree
1198ea7036deSFilipe Manana 		 * search, etc, because log trees are temporary anyway and it
1199ea7036deSFilipe Manana 		 * would only save a few bytes of leaf space.
1200ea7036deSFilipe Manana 		 */
1201ea7036deSFilipe Manana 		if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
1202ea7036deSFilipe Manana 			if (path->slots[0] + 1 >=
1203ea7036deSFilipe Manana 			    btrfs_header_nritems(path->nodes[0])) {
1204ea7036deSFilipe Manana 				ret = find_next_csum_offset(root, path, &next_offset);
1205ea7036deSFilipe Manana 				if (ret < 0)
1206ea7036deSFilipe Manana 					goto out;
1207ea7036deSFilipe Manana 				found_next = 1;
1208ea7036deSFilipe Manana 				goto insert;
1209ea7036deSFilipe Manana 			}
1210ea7036deSFilipe Manana 
1211ea7036deSFilipe Manana 			ret = find_next_csum_offset(root, path, &next_offset);
1212ea7036deSFilipe Manana 			if (ret < 0)
1213ea7036deSFilipe Manana 				goto out;
1214ea7036deSFilipe Manana 
1215ea7036deSFilipe Manana 			tmp = (next_offset - bytenr) >> fs_info->sectorsize_bits;
1216ea7036deSFilipe Manana 			if (tmp <= INT_MAX)
1217ea7036deSFilipe Manana 				extend_nr = min_t(int, extend_nr, tmp);
1218ea7036deSFilipe Manana 		}
1219ea7036deSFilipe Manana 
12202f697dc6SLiu Bo 		diff = (csum_offset + extend_nr) * csum_size;
12210b246afaSJeff Mahoney 		diff = min(diff,
12220b246afaSJeff Mahoney 			   MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size);
1223459931ecSChris Mason 
12243212fa14SJosef Bacik 		diff = diff - btrfs_item_size(leaf, path->slots[0]);
1225cc14600cSFilipe Manana 		diff = min_t(u32, btrfs_leaf_free_space(leaf), diff);
12262f697dc6SLiu Bo 		diff /= csum_size;
12272f697dc6SLiu Bo 		diff *= csum_size;
1228459931ecSChris Mason 
1229c71dd880SDavid Sterba 		btrfs_extend_item(path, diff);
1230f51a4a18SMiao Xie 		ret = 0;
12316567e837SChris Mason 		goto csum;
12326567e837SChris Mason 	}
12336567e837SChris Mason 
12346567e837SChris Mason insert:
1235b3b4aa74SDavid Sterba 	btrfs_release_path(path);
12366567e837SChris Mason 	csum_offset = 0;
1237f578d4bdSChris Mason 	if (found_next) {
12382f697dc6SLiu Bo 		u64 tmp;
1239d20f7043SChris Mason 
1240f51a4a18SMiao Xie 		tmp = sums->len - total_bytes;
1241265fdfa6SDavid Sterba 		tmp >>= fs_info->sectorsize_bits;
12422f697dc6SLiu Bo 		tmp = min(tmp, (next_offset - file_key.offset) >>
1243265fdfa6SDavid Sterba 					 fs_info->sectorsize_bits);
12442f697dc6SLiu Bo 
124550d0446eSSeraphime Kirkovski 		tmp = max_t(u64, 1, tmp);
124650d0446eSSeraphime Kirkovski 		tmp = min_t(u64, tmp, MAX_CSUM_ITEMS(fs_info, csum_size));
1247607d432dSJosef Bacik 		ins_size = csum_size * tmp;
1248f578d4bdSChris Mason 	} else {
1249607d432dSJosef Bacik 		ins_size = csum_size;
1250f578d4bdSChris Mason 	}
12515caf2a00SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, &file_key,
1252f578d4bdSChris Mason 				      ins_size);
125354aa1f4dSChris Mason 	if (ret < 0)
1254918cdf44SFilipe Manana 		goto out;
1255fae7f21cSDulshani Gunawardhana 	if (WARN_ON(ret != 0))
1256918cdf44SFilipe Manana 		goto out;
12575f39d397SChris Mason 	leaf = path->nodes[0];
1258f51a4a18SMiao Xie csum:
12595f39d397SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
1260f51a4a18SMiao Xie 	item_end = (struct btrfs_csum_item *)((unsigned char *)item +
12613212fa14SJosef Bacik 				      btrfs_item_size(leaf, path->slots[0]));
1262509659cdSChris Mason 	item = (struct btrfs_csum_item *)((unsigned char *)item +
1263607d432dSJosef Bacik 					  csum_offset * csum_size);
1264b18c6685SChris Mason found:
1265265fdfa6SDavid Sterba 	ins_size = (u32)(sums->len - total_bytes) >> fs_info->sectorsize_bits;
1266f51a4a18SMiao Xie 	ins_size *= csum_size;
1267f51a4a18SMiao Xie 	ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item,
1268f51a4a18SMiao Xie 			      ins_size);
1269f51a4a18SMiao Xie 	write_extent_buffer(leaf, sums->sums + index, (unsigned long)item,
1270f51a4a18SMiao Xie 			    ins_size);
1271aadfeb6eSChris Mason 
12721e25a2e3SJohannes Thumshirn 	index += ins_size;
1273f51a4a18SMiao Xie 	ins_size /= csum_size;
12740b246afaSJeff Mahoney 	total_bytes += ins_size * fs_info->sectorsize;
1275a6591715SChris Mason 
12765caf2a00SChris Mason 	btrfs_mark_buffer_dirty(path->nodes[0]);
1277e6dcd2dcSChris Mason 	if (total_bytes < sums->len) {
1278b3b4aa74SDavid Sterba 		btrfs_release_path(path);
1279b9473439SChris Mason 		cond_resched();
1280065631f6SChris Mason 		goto again;
1281065631f6SChris Mason 	}
128253863232SChris Mason out:
12835caf2a00SChris Mason 	btrfs_free_path(path);
1284f254e52cSChris Mason 	return ret;
1285f254e52cSChris Mason }
12867ffbb598SFilipe Manana 
12879cdc5124SNikolay Borisov void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
12887ffbb598SFilipe Manana 				     const struct btrfs_path *path,
12897ffbb598SFilipe Manana 				     struct btrfs_file_extent_item *fi,
12907ffbb598SFilipe Manana 				     struct extent_map *em)
12917ffbb598SFilipe Manana {
12923ffbd68cSDavid Sterba 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
12939cdc5124SNikolay Borisov 	struct btrfs_root *root = inode->root;
12947ffbb598SFilipe Manana 	struct extent_buffer *leaf = path->nodes[0];
12957ffbb598SFilipe Manana 	const int slot = path->slots[0];
12967ffbb598SFilipe Manana 	struct btrfs_key key;
12977ffbb598SFilipe Manana 	u64 extent_start, extent_end;
12987ffbb598SFilipe Manana 	u64 bytenr;
12997ffbb598SFilipe Manana 	u8 type = btrfs_file_extent_type(leaf, fi);
13007ffbb598SFilipe Manana 	int compress_type = btrfs_file_extent_compression(leaf, fi);
13017ffbb598SFilipe Manana 
13027ffbb598SFilipe Manana 	btrfs_item_key_to_cpu(leaf, &key, slot);
13037ffbb598SFilipe Manana 	extent_start = key.offset;
1304a5eeb3d1SFilipe Manana 	extent_end = btrfs_file_extent_end(path);
13057ffbb598SFilipe Manana 	em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
130640e7efe0SQu Wenruo 	em->generation = btrfs_file_extent_generation(leaf, fi);
13077ffbb598SFilipe Manana 	if (type == BTRFS_FILE_EXTENT_REG ||
13087ffbb598SFilipe Manana 	    type == BTRFS_FILE_EXTENT_PREALLOC) {
13097ffbb598SFilipe Manana 		em->start = extent_start;
13107ffbb598SFilipe Manana 		em->len = extent_end - extent_start;
13117ffbb598SFilipe Manana 		em->orig_start = extent_start -
13127ffbb598SFilipe Manana 			btrfs_file_extent_offset(leaf, fi);
13137ffbb598SFilipe Manana 		em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
13147ffbb598SFilipe Manana 		bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
13157ffbb598SFilipe Manana 		if (bytenr == 0) {
13167ffbb598SFilipe Manana 			em->block_start = EXTENT_MAP_HOLE;
13177ffbb598SFilipe Manana 			return;
13187ffbb598SFilipe Manana 		}
13197ffbb598SFilipe Manana 		if (compress_type != BTRFS_COMPRESS_NONE) {
13207ffbb598SFilipe Manana 			set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
13217ffbb598SFilipe Manana 			em->compress_type = compress_type;
13227ffbb598SFilipe Manana 			em->block_start = bytenr;
13237ffbb598SFilipe Manana 			em->block_len = em->orig_block_len;
13247ffbb598SFilipe Manana 		} else {
13257ffbb598SFilipe Manana 			bytenr += btrfs_file_extent_offset(leaf, fi);
13267ffbb598SFilipe Manana 			em->block_start = bytenr;
13277ffbb598SFilipe Manana 			em->block_len = em->len;
13287ffbb598SFilipe Manana 			if (type == BTRFS_FILE_EXTENT_PREALLOC)
13297ffbb598SFilipe Manana 				set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
13307ffbb598SFilipe Manana 		}
13317ffbb598SFilipe Manana 	} else if (type == BTRFS_FILE_EXTENT_INLINE) {
13327ffbb598SFilipe Manana 		em->block_start = EXTENT_MAP_INLINE;
13337ffbb598SFilipe Manana 		em->start = extent_start;
13347ffbb598SFilipe Manana 		em->len = extent_end - extent_start;
13357ffbb598SFilipe Manana 		/*
13367ffbb598SFilipe Manana 		 * Initialize orig_start and block_len with the same values
13377ffbb598SFilipe Manana 		 * as in inode.c:btrfs_get_extent().
13387ffbb598SFilipe Manana 		 */
13397ffbb598SFilipe Manana 		em->orig_start = EXTENT_MAP_HOLE;
13407ffbb598SFilipe Manana 		em->block_len = (u64)-1;
13417ffbb598SFilipe Manana 		em->compress_type = compress_type;
1342280f15cbSQu Wenruo 		if (compress_type != BTRFS_COMPRESS_NONE)
1343280f15cbSQu Wenruo 			set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
13447ffbb598SFilipe Manana 	} else {
13450b246afaSJeff Mahoney 		btrfs_err(fs_info,
13469cdc5124SNikolay Borisov 			  "unknown file extent item type %d, inode %llu, offset %llu, "
13479cdc5124SNikolay Borisov 			  "root %llu", type, btrfs_ino(inode), extent_start,
13487ffbb598SFilipe Manana 			  root->root_key.objectid);
13497ffbb598SFilipe Manana 	}
13507ffbb598SFilipe Manana }
1351a5eeb3d1SFilipe Manana 
1352a5eeb3d1SFilipe Manana /*
1353a5eeb3d1SFilipe Manana  * Returns the end offset (non inclusive) of the file extent item the given path
1354a5eeb3d1SFilipe Manana  * points to. If it points to an inline extent, the returned offset is rounded
1355a5eeb3d1SFilipe Manana  * up to the sector size.
1356a5eeb3d1SFilipe Manana  */
1357a5eeb3d1SFilipe Manana u64 btrfs_file_extent_end(const struct btrfs_path *path)
1358a5eeb3d1SFilipe Manana {
1359a5eeb3d1SFilipe Manana 	const struct extent_buffer *leaf = path->nodes[0];
1360a5eeb3d1SFilipe Manana 	const int slot = path->slots[0];
1361a5eeb3d1SFilipe Manana 	struct btrfs_file_extent_item *fi;
1362a5eeb3d1SFilipe Manana 	struct btrfs_key key;
1363a5eeb3d1SFilipe Manana 	u64 end;
1364a5eeb3d1SFilipe Manana 
1365a5eeb3d1SFilipe Manana 	btrfs_item_key_to_cpu(leaf, &key, slot);
1366a5eeb3d1SFilipe Manana 	ASSERT(key.type == BTRFS_EXTENT_DATA_KEY);
1367a5eeb3d1SFilipe Manana 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
1368a5eeb3d1SFilipe Manana 
1369a5eeb3d1SFilipe Manana 	if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
1370a5eeb3d1SFilipe Manana 		end = btrfs_file_extent_ram_bytes(leaf, fi);
1371a5eeb3d1SFilipe Manana 		end = ALIGN(key.offset + end, leaf->fs_info->sectorsize);
1372a5eeb3d1SFilipe Manana 	} else {
1373a5eeb3d1SFilipe Manana 		end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
1374a5eeb3d1SFilipe Manana 	}
1375a5eeb3d1SFilipe Manana 
1376a5eeb3d1SFilipe Manana 	return end;
1377a5eeb3d1SFilipe Manana }
1378