xref: /openbmc/linux/fs/btrfs/file-item.c (revision 66642832f06a4351e23cea6cf254967c227f8224)
16cbd5570SChris Mason /*
26cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
19065631f6SChris Mason #include <linux/bio.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
21065631f6SChris Mason #include <linux/pagemap.h>
22065631f6SChris Mason #include <linux/highmem.h>
231e1d2701SChris Mason #include "ctree.h"
24dee26a9fSChris Mason #include "disk-io.h"
259f5fae2fSChris Mason #include "transaction.h"
26facc8a22SMiao Xie #include "volumes.h"
271de037a4SChris Mason #include "print-tree.h"
28ebb8765bSAnand Jain #include "compression.h"
291e1d2701SChris Mason 
3014a1e067SJeff Mahoney #define __MAX_CSUM_ITEMS(r, size) \
3114a1e067SJeff Mahoney 	((unsigned long)(((BTRFS_MAX_ITEM_SIZE(r) * 2) / size) - 1))
3207d400a6SYan Zheng 
33221b8318SZach Brown #define MAX_CSUM_ITEMS(r, size) (min_t(u32, __MAX_CSUM_ITEMS(r, size), \
3409cbfeafSKirill A. Shutemov 				       PAGE_SIZE))
357ca4be45SChris Mason 
3607d400a6SYan Zheng #define MAX_ORDERED_SUM_BYTES(r) ((PAGE_SIZE - \
3707d400a6SYan Zheng 				   sizeof(struct btrfs_ordered_sum)) / \
38f51a4a18SMiao Xie 				   sizeof(u32) * (r)->sectorsize)
3907d400a6SYan Zheng 
40b18c6685SChris Mason int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
41dee26a9fSChris Mason 			     struct btrfs_root *root,
42b18c6685SChris Mason 			     u64 objectid, u64 pos,
43f2eb0a24SSage Weil 			     u64 disk_offset, u64 disk_num_bytes,
44c8b97818SChris Mason 			     u64 num_bytes, u64 offset, u64 ram_bytes,
45c8b97818SChris Mason 			     u8 compression, u8 encryption, u16 other_encoding)
469f5fae2fSChris Mason {
47dee26a9fSChris Mason 	int ret = 0;
48dee26a9fSChris Mason 	struct btrfs_file_extent_item *item;
49dee26a9fSChris Mason 	struct btrfs_key file_key;
505caf2a00SChris Mason 	struct btrfs_path *path;
515f39d397SChris Mason 	struct extent_buffer *leaf;
52dee26a9fSChris Mason 
535caf2a00SChris Mason 	path = btrfs_alloc_path();
54db5b493aSTsutomu Itoh 	if (!path)
55db5b493aSTsutomu Itoh 		return -ENOMEM;
56dee26a9fSChris Mason 	file_key.objectid = objectid;
57b18c6685SChris Mason 	file_key.offset = pos;
58962a298fSDavid Sterba 	file_key.type = BTRFS_EXTENT_DATA_KEY;
59dee26a9fSChris Mason 
60b9473439SChris Mason 	path->leave_spinning = 1;
615caf2a00SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, &file_key,
62dee26a9fSChris Mason 				      sizeof(*item));
6354aa1f4dSChris Mason 	if (ret < 0)
6454aa1f4dSChris Mason 		goto out;
6579787eaaSJeff Mahoney 	BUG_ON(ret); /* Can't happen */
665f39d397SChris Mason 	leaf = path->nodes[0];
675f39d397SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0],
68dee26a9fSChris Mason 			      struct btrfs_file_extent_item);
69f2eb0a24SSage Weil 	btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset);
70db94535dSChris Mason 	btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
71f2eb0a24SSage Weil 	btrfs_set_file_extent_offset(leaf, item, offset);
72db94535dSChris Mason 	btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
73c8b97818SChris Mason 	btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes);
745f39d397SChris Mason 	btrfs_set_file_extent_generation(leaf, item, trans->transid);
755f39d397SChris Mason 	btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
76c8b97818SChris Mason 	btrfs_set_file_extent_compression(leaf, item, compression);
77c8b97818SChris Mason 	btrfs_set_file_extent_encryption(leaf, item, encryption);
78c8b97818SChris Mason 	btrfs_set_file_extent_other_encoding(leaf, item, other_encoding);
79c8b97818SChris Mason 
805f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
8154aa1f4dSChris Mason out:
825caf2a00SChris Mason 	btrfs_free_path(path);
8354aa1f4dSChris Mason 	return ret;
849f5fae2fSChris Mason }
85dee26a9fSChris Mason 
8648a3b636SEric Sandeen static struct btrfs_csum_item *
8748a3b636SEric Sandeen btrfs_lookup_csum(struct btrfs_trans_handle *trans,
88b18c6685SChris Mason 		  struct btrfs_root *root,
896567e837SChris Mason 		  struct btrfs_path *path,
90d20f7043SChris Mason 		  u64 bytenr, int cow)
916567e837SChris Mason {
926567e837SChris Mason 	int ret;
936567e837SChris Mason 	struct btrfs_key file_key;
946567e837SChris Mason 	struct btrfs_key found_key;
956567e837SChris Mason 	struct btrfs_csum_item *item;
965f39d397SChris Mason 	struct extent_buffer *leaf;
976567e837SChris Mason 	u64 csum_offset = 0;
986c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
99a429e513SChris Mason 	int csums_in_item;
1006567e837SChris Mason 
101d20f7043SChris Mason 	file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
102d20f7043SChris Mason 	file_key.offset = bytenr;
103962a298fSDavid Sterba 	file_key.type = BTRFS_EXTENT_CSUM_KEY;
104b18c6685SChris Mason 	ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
1056567e837SChris Mason 	if (ret < 0)
1066567e837SChris Mason 		goto fail;
1075f39d397SChris Mason 	leaf = path->nodes[0];
1086567e837SChris Mason 	if (ret > 0) {
1096567e837SChris Mason 		ret = 1;
11070b2befdSChris Mason 		if (path->slots[0] == 0)
1116567e837SChris Mason 			goto fail;
1126567e837SChris Mason 		path->slots[0]--;
1135f39d397SChris Mason 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
114962a298fSDavid Sterba 		if (found_key.type != BTRFS_EXTENT_CSUM_KEY)
1156567e837SChris Mason 			goto fail;
116d20f7043SChris Mason 
117d20f7043SChris Mason 		csum_offset = (bytenr - found_key.offset) >>
1186567e837SChris Mason 				root->fs_info->sb->s_blocksize_bits;
1195f39d397SChris Mason 		csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
120607d432dSJosef Bacik 		csums_in_item /= csum_size;
121a429e513SChris Mason 
12282d130ffSMiao Xie 		if (csum_offset == csums_in_item) {
123a429e513SChris Mason 			ret = -EFBIG;
1246567e837SChris Mason 			goto fail;
12582d130ffSMiao Xie 		} else if (csum_offset > csums_in_item) {
12682d130ffSMiao Xie 			goto fail;
1276567e837SChris Mason 		}
1286567e837SChris Mason 	}
1296567e837SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
130509659cdSChris Mason 	item = (struct btrfs_csum_item *)((unsigned char *)item +
131607d432dSJosef Bacik 					  csum_offset * csum_size);
1326567e837SChris Mason 	return item;
1336567e837SChris Mason fail:
1346567e837SChris Mason 	if (ret > 0)
135b18c6685SChris Mason 		ret = -ENOENT;
1366567e837SChris Mason 	return ERR_PTR(ret);
1376567e837SChris Mason }
1386567e837SChris Mason 
139dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
140dee26a9fSChris Mason 			     struct btrfs_root *root,
141dee26a9fSChris Mason 			     struct btrfs_path *path, u64 objectid,
1429773a788SChris Mason 			     u64 offset, int mod)
143dee26a9fSChris Mason {
144dee26a9fSChris Mason 	int ret;
145dee26a9fSChris Mason 	struct btrfs_key file_key;
146dee26a9fSChris Mason 	int ins_len = mod < 0 ? -1 : 0;
147dee26a9fSChris Mason 	int cow = mod != 0;
148dee26a9fSChris Mason 
149dee26a9fSChris Mason 	file_key.objectid = objectid;
15070b2befdSChris Mason 	file_key.offset = offset;
151962a298fSDavid Sterba 	file_key.type = BTRFS_EXTENT_DATA_KEY;
152dee26a9fSChris Mason 	ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
153dee26a9fSChris Mason 	return ret;
154dee26a9fSChris Mason }
155f254e52cSChris Mason 
156facc8a22SMiao Xie static void btrfs_io_bio_endio_readpage(struct btrfs_io_bio *bio, int err)
157facc8a22SMiao Xie {
158facc8a22SMiao Xie 	kfree(bio->csum_allocated);
159facc8a22SMiao Xie }
160facc8a22SMiao Xie 
1614b46fce2SJosef Bacik static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
1624b46fce2SJosef Bacik 				   struct inode *inode, struct bio *bio,
1634b46fce2SJosef Bacik 				   u64 logical_offset, u32 *dst, int dio)
16461b49440SChris Mason {
16561b49440SChris Mason 	struct bio_vec *bvec = bio->bi_io_vec;
166facc8a22SMiao Xie 	struct btrfs_io_bio *btrfs_bio = btrfs_io_bio(bio);
167facc8a22SMiao Xie 	struct btrfs_csum_item *item = NULL;
168facc8a22SMiao Xie 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
169facc8a22SMiao Xie 	struct btrfs_path *path;
170facc8a22SMiao Xie 	u8 *csum;
1714b46fce2SJosef Bacik 	u64 offset = 0;
17261b49440SChris Mason 	u64 item_start_offset = 0;
17361b49440SChris Mason 	u64 item_last_offset = 0;
174d20f7043SChris Mason 	u64 disk_bytenr;
175c40a3d38SChandan Rajendra 	u64 page_bytes_left;
17661b49440SChris Mason 	u32 diff;
177facc8a22SMiao Xie 	int nblocks;
178facc8a22SMiao Xie 	int bio_index = 0;
179e4100d98SMiao Xie 	int count;
180facc8a22SMiao Xie 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
18161b49440SChris Mason 
18261b49440SChris Mason 	path = btrfs_alloc_path();
183c2db1073STsutomu Itoh 	if (!path)
184c2db1073STsutomu Itoh 		return -ENOMEM;
185facc8a22SMiao Xie 
1864f024f37SKent Overstreet 	nblocks = bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
187facc8a22SMiao Xie 	if (!dst) {
188facc8a22SMiao Xie 		if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) {
18931e818feSDavid Sterba 			btrfs_bio->csum_allocated = kmalloc_array(nblocks,
19031e818feSDavid Sterba 					csum_size, GFP_NOFS);
191facc8a22SMiao Xie 			if (!btrfs_bio->csum_allocated) {
192facc8a22SMiao Xie 				btrfs_free_path(path);
193facc8a22SMiao Xie 				return -ENOMEM;
194facc8a22SMiao Xie 			}
195facc8a22SMiao Xie 			btrfs_bio->csum = btrfs_bio->csum_allocated;
196facc8a22SMiao Xie 			btrfs_bio->end_io = btrfs_io_bio_endio_readpage;
197facc8a22SMiao Xie 		} else {
198facc8a22SMiao Xie 			btrfs_bio->csum = btrfs_bio->csum_inline;
199facc8a22SMiao Xie 		}
200facc8a22SMiao Xie 		csum = btrfs_bio->csum;
201facc8a22SMiao Xie 	} else {
202facc8a22SMiao Xie 		csum = (u8 *)dst;
203facc8a22SMiao Xie 	}
204facc8a22SMiao Xie 
20509cbfeafSKirill A. Shutemov 	if (bio->bi_iter.bi_size > PAGE_SIZE * 8)
206e4058b54SDavid Sterba 		path->reada = READA_FORWARD;
20761b49440SChris Mason 
20861b49440SChris Mason 	WARN_ON(bio->bi_vcnt <= 0);
20961b49440SChris Mason 
2102cf8572dSChris Mason 	/*
2112cf8572dSChris Mason 	 * the free space stuff is only read when it hasn't been
2122cf8572dSChris Mason 	 * updated in the current transaction.  So, we can safely
2132cf8572dSChris Mason 	 * read from the commit root and sidestep a nasty deadlock
2142cf8572dSChris Mason 	 * between reading the free space cache and updating the csum tree.
2152cf8572dSChris Mason 	 */
21683eea1f1SLiu Bo 	if (btrfs_is_free_space_inode(inode)) {
2172cf8572dSChris Mason 		path->search_commit_root = 1;
218ddf23b3fSJosef Bacik 		path->skip_locking = 1;
219ddf23b3fSJosef Bacik 	}
2202cf8572dSChris Mason 
2214f024f37SKent Overstreet 	disk_bytenr = (u64)bio->bi_iter.bi_sector << 9;
2224b46fce2SJosef Bacik 	if (dio)
2234b46fce2SJosef Bacik 		offset = logical_offset;
224c40a3d38SChandan Rajendra 
225c40a3d38SChandan Rajendra 	page_bytes_left = bvec->bv_len;
22661b49440SChris Mason 	while (bio_index < bio->bi_vcnt) {
2274b46fce2SJosef Bacik 		if (!dio)
22861b49440SChris Mason 			offset = page_offset(bvec->bv_page) + bvec->bv_offset;
229facc8a22SMiao Xie 		count = btrfs_find_ordered_sum(inode, offset, disk_bytenr,
230facc8a22SMiao Xie 					       (u32 *)csum, nblocks);
231e4100d98SMiao Xie 		if (count)
23261b49440SChris Mason 			goto found;
23361b49440SChris Mason 
234d20f7043SChris Mason 		if (!item || disk_bytenr < item_start_offset ||
235d20f7043SChris Mason 		    disk_bytenr >= item_last_offset) {
23661b49440SChris Mason 			struct btrfs_key found_key;
23761b49440SChris Mason 			u32 item_size;
23861b49440SChris Mason 
23961b49440SChris Mason 			if (item)
240b3b4aa74SDavid Sterba 				btrfs_release_path(path);
241d20f7043SChris Mason 			item = btrfs_lookup_csum(NULL, root->fs_info->csum_root,
242d20f7043SChris Mason 						 path, disk_bytenr, 0);
24361b49440SChris Mason 			if (IS_ERR(item)) {
244e4100d98SMiao Xie 				count = 1;
245facc8a22SMiao Xie 				memset(csum, 0, csum_size);
24617d217feSYan Zheng 				if (BTRFS_I(inode)->root->root_key.objectid ==
24717d217feSYan Zheng 				    BTRFS_DATA_RELOC_TREE_OBJECTID) {
24817d217feSYan Zheng 					set_extent_bits(io_tree, offset,
249c40a3d38SChandan Rajendra 						offset + root->sectorsize - 1,
250ceeb0ae7SDavid Sterba 						EXTENT_NODATASUM);
25117d217feSYan Zheng 				} else {
252aee133afSNikolay Borisov 					btrfs_info_rl(BTRFS_I(inode)->root->fs_info,
253efe120a0SFrank Holton 						   "no csum found for inode %llu start %llu",
254c1c9ff7cSGeert Uytterhoeven 					       btrfs_ino(inode), offset);
25517d217feSYan Zheng 				}
2566dab8157SChris Mason 				item = NULL;
257b3b4aa74SDavid Sterba 				btrfs_release_path(path);
25861b49440SChris Mason 				goto found;
25961b49440SChris Mason 			}
26061b49440SChris Mason 			btrfs_item_key_to_cpu(path->nodes[0], &found_key,
26161b49440SChris Mason 					      path->slots[0]);
26261b49440SChris Mason 
26361b49440SChris Mason 			item_start_offset = found_key.offset;
26461b49440SChris Mason 			item_size = btrfs_item_size_nr(path->nodes[0],
26561b49440SChris Mason 						       path->slots[0]);
26661b49440SChris Mason 			item_last_offset = item_start_offset +
267607d432dSJosef Bacik 				(item_size / csum_size) *
26861b49440SChris Mason 				root->sectorsize;
26961b49440SChris Mason 			item = btrfs_item_ptr(path->nodes[0], path->slots[0],
27061b49440SChris Mason 					      struct btrfs_csum_item);
27161b49440SChris Mason 		}
27261b49440SChris Mason 		/*
27361b49440SChris Mason 		 * this byte range must be able to fit inside
27461b49440SChris Mason 		 * a single leaf so it will also fit inside a u32
27561b49440SChris Mason 		 */
276d20f7043SChris Mason 		diff = disk_bytenr - item_start_offset;
27761b49440SChris Mason 		diff = diff / root->sectorsize;
278607d432dSJosef Bacik 		diff = diff * csum_size;
279facc8a22SMiao Xie 		count = min_t(int, nblocks, (item_last_offset - disk_bytenr) >>
280e4100d98SMiao Xie 					    inode->i_sb->s_blocksize_bits);
281facc8a22SMiao Xie 		read_extent_buffer(path->nodes[0], csum,
2823de9d6b6SChris Mason 				   ((unsigned long)item) + diff,
283e4100d98SMiao Xie 				   csum_size * count);
28461b49440SChris Mason found:
285facc8a22SMiao Xie 		csum += count * csum_size;
286facc8a22SMiao Xie 		nblocks -= count;
287c40a3d38SChandan Rajendra 
288e4100d98SMiao Xie 		while (count--) {
289c40a3d38SChandan Rajendra 			disk_bytenr += root->sectorsize;
290c40a3d38SChandan Rajendra 			offset += root->sectorsize;
291c40a3d38SChandan Rajendra 			page_bytes_left -= root->sectorsize;
292c40a3d38SChandan Rajendra 			if (!page_bytes_left) {
293c40a3d38SChandan Rajendra 				bio_index++;
294389f239cSChris Mason 				/*
295389f239cSChris Mason 				 * make sure we're still inside the
296389f239cSChris Mason 				 * bio before we update page_bytes_left
297389f239cSChris Mason 				 */
298389f239cSChris Mason 				if (bio_index >= bio->bi_vcnt) {
299389f239cSChris Mason 					WARN_ON_ONCE(count);
300389f239cSChris Mason 					goto done;
301389f239cSChris Mason 				}
30261b49440SChris Mason 				bvec++;
303c40a3d38SChandan Rajendra 				page_bytes_left = bvec->bv_len;
304c40a3d38SChandan Rajendra 			}
305c40a3d38SChandan Rajendra 
30661b49440SChris Mason 		}
307e4100d98SMiao Xie 	}
308389f239cSChris Mason 
309389f239cSChris Mason done:
31061b49440SChris Mason 	btrfs_free_path(path);
31161b49440SChris Mason 	return 0;
31261b49440SChris Mason }
31361b49440SChris Mason 
3144b46fce2SJosef Bacik int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
3154b46fce2SJosef Bacik 			  struct bio *bio, u32 *dst)
3164b46fce2SJosef Bacik {
3174b46fce2SJosef Bacik 	return __btrfs_lookup_bio_sums(root, inode, bio, 0, dst, 0);
3184b46fce2SJosef Bacik }
3194b46fce2SJosef Bacik 
3204b46fce2SJosef Bacik int btrfs_lookup_bio_sums_dio(struct btrfs_root *root, struct inode *inode,
32123ea8e5aSMiao Xie 			      struct bio *bio, u64 offset)
3224b46fce2SJosef Bacik {
32323ea8e5aSMiao Xie 	return __btrfs_lookup_bio_sums(root, inode, bio, offset, NULL, 1);
3244b46fce2SJosef Bacik }
3254b46fce2SJosef Bacik 
32617d217feSYan Zheng int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
327a2de733cSArne Jansen 			     struct list_head *list, int search_commit)
32817d217feSYan Zheng {
32917d217feSYan Zheng 	struct btrfs_key key;
33017d217feSYan Zheng 	struct btrfs_path *path;
33117d217feSYan Zheng 	struct extent_buffer *leaf;
33217d217feSYan Zheng 	struct btrfs_ordered_sum *sums;
33317d217feSYan Zheng 	struct btrfs_csum_item *item;
3340678b618SMark Fasheh 	LIST_HEAD(tmplist);
33517d217feSYan Zheng 	unsigned long offset;
33617d217feSYan Zheng 	int ret;
33717d217feSYan Zheng 	size_t size;
33817d217feSYan Zheng 	u64 csum_end;
3396c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
34017d217feSYan Zheng 
341d1b00a47SSatoru Takeuchi 	ASSERT(IS_ALIGNED(start, root->sectorsize) &&
342d1b00a47SSatoru Takeuchi 	       IS_ALIGNED(end + 1, root->sectorsize));
3434277a9c3SJosef Bacik 
34417d217feSYan Zheng 	path = btrfs_alloc_path();
345d8926bb3SMark Fasheh 	if (!path)
346d8926bb3SMark Fasheh 		return -ENOMEM;
34717d217feSYan Zheng 
348a2de733cSArne Jansen 	if (search_commit) {
349a2de733cSArne Jansen 		path->skip_locking = 1;
350e4058b54SDavid Sterba 		path->reada = READA_FORWARD;
351a2de733cSArne Jansen 		path->search_commit_root = 1;
352a2de733cSArne Jansen 	}
353a2de733cSArne Jansen 
35417d217feSYan Zheng 	key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
35517d217feSYan Zheng 	key.offset = start;
35617d217feSYan Zheng 	key.type = BTRFS_EXTENT_CSUM_KEY;
35717d217feSYan Zheng 
35807d400a6SYan Zheng 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
35917d217feSYan Zheng 	if (ret < 0)
36017d217feSYan Zheng 		goto fail;
36117d217feSYan Zheng 	if (ret > 0 && path->slots[0] > 0) {
36217d217feSYan Zheng 		leaf = path->nodes[0];
36317d217feSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
36417d217feSYan Zheng 		if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
36517d217feSYan Zheng 		    key.type == BTRFS_EXTENT_CSUM_KEY) {
36617d217feSYan Zheng 			offset = (start - key.offset) >>
36717d217feSYan Zheng 				 root->fs_info->sb->s_blocksize_bits;
36817d217feSYan Zheng 			if (offset * csum_size <
36917d217feSYan Zheng 			    btrfs_item_size_nr(leaf, path->slots[0] - 1))
37017d217feSYan Zheng 				path->slots[0]--;
37117d217feSYan Zheng 		}
37217d217feSYan Zheng 	}
37317d217feSYan Zheng 
37417d217feSYan Zheng 	while (start <= end) {
37517d217feSYan Zheng 		leaf = path->nodes[0];
37617d217feSYan Zheng 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
37707d400a6SYan Zheng 			ret = btrfs_next_leaf(root, path);
37817d217feSYan Zheng 			if (ret < 0)
37917d217feSYan Zheng 				goto fail;
38017d217feSYan Zheng 			if (ret > 0)
38117d217feSYan Zheng 				break;
38217d217feSYan Zheng 			leaf = path->nodes[0];
38317d217feSYan Zheng 		}
38417d217feSYan Zheng 
38517d217feSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
38617d217feSYan Zheng 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
387628c8282SZhi Yong Wu 		    key.type != BTRFS_EXTENT_CSUM_KEY ||
388628c8282SZhi Yong Wu 		    key.offset > end)
38917d217feSYan Zheng 			break;
39017d217feSYan Zheng 
39117d217feSYan Zheng 		if (key.offset > start)
39217d217feSYan Zheng 			start = key.offset;
39317d217feSYan Zheng 
39417d217feSYan Zheng 		size = btrfs_item_size_nr(leaf, path->slots[0]);
39517d217feSYan Zheng 		csum_end = key.offset + (size / csum_size) * root->sectorsize;
39687b29b20SYan Zheng 		if (csum_end <= start) {
39787b29b20SYan Zheng 			path->slots[0]++;
39887b29b20SYan Zheng 			continue;
39987b29b20SYan Zheng 		}
40017d217feSYan Zheng 
40107d400a6SYan Zheng 		csum_end = min(csum_end, end + 1);
40207d400a6SYan Zheng 		item = btrfs_item_ptr(path->nodes[0], path->slots[0],
40307d400a6SYan Zheng 				      struct btrfs_csum_item);
40407d400a6SYan Zheng 		while (start < csum_end) {
40507d400a6SYan Zheng 			size = min_t(size_t, csum_end - start,
40607d400a6SYan Zheng 				     MAX_ORDERED_SUM_BYTES(root));
40707d400a6SYan Zheng 			sums = kzalloc(btrfs_ordered_sum_size(root, size),
40807d400a6SYan Zheng 				       GFP_NOFS);
4090678b618SMark Fasheh 			if (!sums) {
4100678b618SMark Fasheh 				ret = -ENOMEM;
4110678b618SMark Fasheh 				goto fail;
4120678b618SMark Fasheh 			}
41317d217feSYan Zheng 
41417d217feSYan Zheng 			sums->bytenr = start;
415f51a4a18SMiao Xie 			sums->len = (int)size;
41617d217feSYan Zheng 
41717d217feSYan Zheng 			offset = (start - key.offset) >>
41817d217feSYan Zheng 				root->fs_info->sb->s_blocksize_bits;
41917d217feSYan Zheng 			offset *= csum_size;
420f51a4a18SMiao Xie 			size >>= root->fs_info->sb->s_blocksize_bits;
42117d217feSYan Zheng 
42207d400a6SYan Zheng 			read_extent_buffer(path->nodes[0],
423f51a4a18SMiao Xie 					   sums->sums,
424f51a4a18SMiao Xie 					   ((unsigned long)item) + offset,
425f51a4a18SMiao Xie 					   csum_size * size);
42617d217feSYan Zheng 
427f51a4a18SMiao Xie 			start += root->sectorsize * size;
4280678b618SMark Fasheh 			list_add_tail(&sums->list, &tmplist);
42907d400a6SYan Zheng 		}
43017d217feSYan Zheng 		path->slots[0]++;
43117d217feSYan Zheng 	}
43217d217feSYan Zheng 	ret = 0;
43317d217feSYan Zheng fail:
4340678b618SMark Fasheh 	while (ret < 0 && !list_empty(&tmplist)) {
4356e5aafb2SChris Mason 		sums = list_entry(tmplist.next, struct btrfs_ordered_sum, list);
4360678b618SMark Fasheh 		list_del(&sums->list);
4370678b618SMark Fasheh 		kfree(sums);
4380678b618SMark Fasheh 	}
4390678b618SMark Fasheh 	list_splice_tail(&tmplist, list);
4400678b618SMark Fasheh 
44117d217feSYan Zheng 	btrfs_free_path(path);
44217d217feSYan Zheng 	return ret;
44317d217feSYan Zheng }
44417d217feSYan Zheng 
4453edf7d33SChris Mason int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
446d20f7043SChris Mason 		       struct bio *bio, u64 file_start, int contig)
447e015640fSChris Mason {
448e6dcd2dcSChris Mason 	struct btrfs_ordered_sum *sums;
4493edf7d33SChris Mason 	struct btrfs_ordered_extent *ordered;
450e015640fSChris Mason 	char *data;
451e015640fSChris Mason 	struct bio_vec *bvec = bio->bi_io_vec;
452e015640fSChris Mason 	int bio_index = 0;
453f51a4a18SMiao Xie 	int index;
454c40a3d38SChandan Rajendra 	int nr_sectors;
455c40a3d38SChandan Rajendra 	int i;
4563edf7d33SChris Mason 	unsigned long total_bytes = 0;
4573edf7d33SChris Mason 	unsigned long this_sum_bytes = 0;
4583edf7d33SChris Mason 	u64 offset;
459e015640fSChris Mason 
460e6dcd2dcSChris Mason 	WARN_ON(bio->bi_vcnt <= 0);
4614f024f37SKent Overstreet 	sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_iter.bi_size),
4624f024f37SKent Overstreet 		       GFP_NOFS);
463e015640fSChris Mason 	if (!sums)
464e015640fSChris Mason 		return -ENOMEM;
4653edf7d33SChris Mason 
4664f024f37SKent Overstreet 	sums->len = bio->bi_iter.bi_size;
467e6dcd2dcSChris Mason 	INIT_LIST_HEAD(&sums->list);
468d20f7043SChris Mason 
469d20f7043SChris Mason 	if (contig)
470d20f7043SChris Mason 		offset = file_start;
471d20f7043SChris Mason 	else
472d20f7043SChris Mason 		offset = page_offset(bvec->bv_page) + bvec->bv_offset;
473d20f7043SChris Mason 
474d20f7043SChris Mason 	ordered = btrfs_lookup_ordered_extent(inode, offset);
47579787eaaSJeff Mahoney 	BUG_ON(!ordered); /* Logic error */
4764f024f37SKent Overstreet 	sums->bytenr = (u64)bio->bi_iter.bi_sector << 9;
477f51a4a18SMiao Xie 	index = 0;
478e015640fSChris Mason 
479e015640fSChris Mason 	while (bio_index < bio->bi_vcnt) {
480d20f7043SChris Mason 		if (!contig)
4813edf7d33SChris Mason 			offset = page_offset(bvec->bv_page) + bvec->bv_offset;
482d20f7043SChris Mason 
483c40a3d38SChandan Rajendra 		data = kmap_atomic(bvec->bv_page);
484c40a3d38SChandan Rajendra 
485c40a3d38SChandan Rajendra 		nr_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info,
486c40a3d38SChandan Rajendra 						bvec->bv_len + root->sectorsize
487c40a3d38SChandan Rajendra 						- 1);
488c40a3d38SChandan Rajendra 
489c40a3d38SChandan Rajendra 		for (i = 0; i < nr_sectors; i++) {
490e58dd74bSJosef Bacik 			if (offset >= ordered->file_offset + ordered->len ||
491e58dd74bSJosef Bacik 				offset < ordered->file_offset) {
4923edf7d33SChris Mason 				unsigned long bytes_left;
493c40a3d38SChandan Rajendra 
494c40a3d38SChandan Rajendra 				kunmap_atomic(data);
4953edf7d33SChris Mason 				sums->len = this_sum_bytes;
4963edf7d33SChris Mason 				this_sum_bytes = 0;
4973edf7d33SChris Mason 				btrfs_add_ordered_sum(inode, ordered, sums);
4983edf7d33SChris Mason 				btrfs_put_ordered_extent(ordered);
4993edf7d33SChris Mason 
5004f024f37SKent Overstreet 				bytes_left = bio->bi_iter.bi_size - total_bytes;
5013edf7d33SChris Mason 
5023edf7d33SChris Mason 				sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
5033edf7d33SChris Mason 					GFP_NOFS);
50479787eaaSJeff Mahoney 				BUG_ON(!sums); /* -ENOMEM */
5053edf7d33SChris Mason 				sums->len = bytes_left;
506c40a3d38SChandan Rajendra 				ordered = btrfs_lookup_ordered_extent(inode,
507c40a3d38SChandan Rajendra 								offset);
508c40a3d38SChandan Rajendra 				ASSERT(ordered); /* Logic error */
509c40a3d38SChandan Rajendra 				sums->bytenr = ((u64)bio->bi_iter.bi_sector << 9)
510c40a3d38SChandan Rajendra 					+ total_bytes;
511f51a4a18SMiao Xie 				index = 0;
5123edf7d33SChris Mason 
5137ac687d9SCong Wang 				data = kmap_atomic(bvec->bv_page);
514c40a3d38SChandan Rajendra 			}
515c40a3d38SChandan Rajendra 
516f51a4a18SMiao Xie 			sums->sums[index] = ~(u32)0;
517c40a3d38SChandan Rajendra 			sums->sums[index]
518c40a3d38SChandan Rajendra 				= btrfs_csum_data(data + bvec->bv_offset
519c40a3d38SChandan Rajendra 						+ (i * root->sectorsize),
520f51a4a18SMiao Xie 						sums->sums[index],
521c40a3d38SChandan Rajendra 						root->sectorsize);
522f51a4a18SMiao Xie 			btrfs_csum_final(sums->sums[index],
523f51a4a18SMiao Xie 					(char *)(sums->sums + index));
524c40a3d38SChandan Rajendra 			index++;
525c40a3d38SChandan Rajendra 			offset += root->sectorsize;
526c40a3d38SChandan Rajendra 			this_sum_bytes += root->sectorsize;
527c40a3d38SChandan Rajendra 			total_bytes += root->sectorsize;
528c40a3d38SChandan Rajendra 		}
529c40a3d38SChandan Rajendra 
530c40a3d38SChandan Rajendra 		kunmap_atomic(data);
531ed98b56aSChris Mason 
532e015640fSChris Mason 		bio_index++;
533e015640fSChris Mason 		bvec++;
534e015640fSChris Mason 	}
535ed98b56aSChris Mason 	this_sum_bytes = 0;
5363edf7d33SChris Mason 	btrfs_add_ordered_sum(inode, ordered, sums);
5373edf7d33SChris Mason 	btrfs_put_ordered_extent(ordered);
538e015640fSChris Mason 	return 0;
539e015640fSChris Mason }
540e015640fSChris Mason 
541459931ecSChris Mason /*
542459931ecSChris Mason  * helper function for csum removal, this expects the
543459931ecSChris Mason  * key to describe the csum pointed to by the path, and it expects
544459931ecSChris Mason  * the csum to overlap the range [bytenr, len]
545459931ecSChris Mason  *
546459931ecSChris Mason  * The csum should not be entirely contained in the range and the
547459931ecSChris Mason  * range should not be entirely contained in the csum.
548459931ecSChris Mason  *
549459931ecSChris Mason  * This calls btrfs_truncate_item with the correct args based on the
550459931ecSChris Mason  * overlap, and fixes up the key as required.
551459931ecSChris Mason  */
552afe5fea7STsutomu Itoh static noinline void truncate_one_csum(struct btrfs_root *root,
553459931ecSChris Mason 				       struct btrfs_path *path,
554459931ecSChris Mason 				       struct btrfs_key *key,
555459931ecSChris Mason 				       u64 bytenr, u64 len)
556459931ecSChris Mason {
557459931ecSChris Mason 	struct extent_buffer *leaf;
5586c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
559459931ecSChris Mason 	u64 csum_end;
560459931ecSChris Mason 	u64 end_byte = bytenr + len;
561459931ecSChris Mason 	u32 blocksize_bits = root->fs_info->sb->s_blocksize_bits;
562459931ecSChris Mason 
563459931ecSChris Mason 	leaf = path->nodes[0];
564459931ecSChris Mason 	csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
565459931ecSChris Mason 	csum_end <<= root->fs_info->sb->s_blocksize_bits;
566459931ecSChris Mason 	csum_end += key->offset;
567459931ecSChris Mason 
568459931ecSChris Mason 	if (key->offset < bytenr && csum_end <= end_byte) {
569459931ecSChris Mason 		/*
570459931ecSChris Mason 		 *         [ bytenr - len ]
571459931ecSChris Mason 		 *         [   ]
572459931ecSChris Mason 		 *   [csum     ]
573459931ecSChris Mason 		 *   A simple truncate off the end of the item
574459931ecSChris Mason 		 */
575459931ecSChris Mason 		u32 new_size = (bytenr - key->offset) >> blocksize_bits;
576459931ecSChris Mason 		new_size *= csum_size;
577afe5fea7STsutomu Itoh 		btrfs_truncate_item(root, path, new_size, 1);
578459931ecSChris Mason 	} else if (key->offset >= bytenr && csum_end > end_byte &&
579459931ecSChris Mason 		   end_byte > key->offset) {
580459931ecSChris Mason 		/*
581459931ecSChris Mason 		 *         [ bytenr - len ]
582459931ecSChris Mason 		 *                 [ ]
583459931ecSChris Mason 		 *                 [csum     ]
584459931ecSChris Mason 		 * we need to truncate from the beginning of the csum
585459931ecSChris Mason 		 */
586459931ecSChris Mason 		u32 new_size = (csum_end - end_byte) >> blocksize_bits;
587459931ecSChris Mason 		new_size *= csum_size;
588459931ecSChris Mason 
589afe5fea7STsutomu Itoh 		btrfs_truncate_item(root, path, new_size, 0);
590459931ecSChris Mason 
591459931ecSChris Mason 		key->offset = end_byte;
592b7a0365eSDaniel Dressler 		btrfs_set_item_key_safe(root->fs_info, path, key);
593459931ecSChris Mason 	} else {
594459931ecSChris Mason 		BUG();
595459931ecSChris Mason 	}
596459931ecSChris Mason }
597459931ecSChris Mason 
598459931ecSChris Mason /*
599459931ecSChris Mason  * deletes the csum items from the csum tree for a given
600459931ecSChris Mason  * range of bytes.
601459931ecSChris Mason  */
602459931ecSChris Mason int btrfs_del_csums(struct btrfs_trans_handle *trans,
603459931ecSChris Mason 		    struct btrfs_root *root, u64 bytenr, u64 len)
604459931ecSChris Mason {
605459931ecSChris Mason 	struct btrfs_path *path;
606459931ecSChris Mason 	struct btrfs_key key;
607459931ecSChris Mason 	u64 end_byte = bytenr + len;
608459931ecSChris Mason 	u64 csum_end;
609459931ecSChris Mason 	struct extent_buffer *leaf;
610459931ecSChris Mason 	int ret;
6116c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
612459931ecSChris Mason 	int blocksize_bits = root->fs_info->sb->s_blocksize_bits;
613459931ecSChris Mason 
614459931ecSChris Mason 	root = root->fs_info->csum_root;
615459931ecSChris Mason 
616459931ecSChris Mason 	path = btrfs_alloc_path();
6172a29edc6Sliubo 	if (!path)
6182a29edc6Sliubo 		return -ENOMEM;
619459931ecSChris Mason 
620459931ecSChris Mason 	while (1) {
621459931ecSChris Mason 		key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
622459931ecSChris Mason 		key.offset = end_byte - 1;
623459931ecSChris Mason 		key.type = BTRFS_EXTENT_CSUM_KEY;
624459931ecSChris Mason 
625b9473439SChris Mason 		path->leave_spinning = 1;
626459931ecSChris Mason 		ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
627459931ecSChris Mason 		if (ret > 0) {
628459931ecSChris Mason 			if (path->slots[0] == 0)
62965a246c5STsutomu Itoh 				break;
630459931ecSChris Mason 			path->slots[0]--;
631ad0397a7SJosef Bacik 		} else if (ret < 0) {
63265a246c5STsutomu Itoh 			break;
633459931ecSChris Mason 		}
634ad0397a7SJosef Bacik 
635459931ecSChris Mason 		leaf = path->nodes[0];
636459931ecSChris Mason 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
637459931ecSChris Mason 
638459931ecSChris Mason 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
639459931ecSChris Mason 		    key.type != BTRFS_EXTENT_CSUM_KEY) {
640459931ecSChris Mason 			break;
641459931ecSChris Mason 		}
642459931ecSChris Mason 
643459931ecSChris Mason 		if (key.offset >= end_byte)
644459931ecSChris Mason 			break;
645459931ecSChris Mason 
646459931ecSChris Mason 		csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
647459931ecSChris Mason 		csum_end <<= blocksize_bits;
648459931ecSChris Mason 		csum_end += key.offset;
649459931ecSChris Mason 
650459931ecSChris Mason 		/* this csum ends before we start, we're done */
651459931ecSChris Mason 		if (csum_end <= bytenr)
652459931ecSChris Mason 			break;
653459931ecSChris Mason 
654459931ecSChris Mason 		/* delete the entire item, it is inside our range */
655459931ecSChris Mason 		if (key.offset >= bytenr && csum_end <= end_byte) {
656459931ecSChris Mason 			ret = btrfs_del_item(trans, root, path);
65765a246c5STsutomu Itoh 			if (ret)
65865a246c5STsutomu Itoh 				goto out;
659dcbdd4dcSChris Mason 			if (key.offset == bytenr)
660dcbdd4dcSChris Mason 				break;
661459931ecSChris Mason 		} else if (key.offset < bytenr && csum_end > end_byte) {
662459931ecSChris Mason 			unsigned long offset;
663459931ecSChris Mason 			unsigned long shift_len;
664459931ecSChris Mason 			unsigned long item_offset;
665459931ecSChris Mason 			/*
666459931ecSChris Mason 			 *        [ bytenr - len ]
667459931ecSChris Mason 			 *     [csum                ]
668459931ecSChris Mason 			 *
669459931ecSChris Mason 			 * Our bytes are in the middle of the csum,
670459931ecSChris Mason 			 * we need to split this item and insert a new one.
671459931ecSChris Mason 			 *
672459931ecSChris Mason 			 * But we can't drop the path because the
673459931ecSChris Mason 			 * csum could change, get removed, extended etc.
674459931ecSChris Mason 			 *
675459931ecSChris Mason 			 * The trick here is the max size of a csum item leaves
676459931ecSChris Mason 			 * enough room in the tree block for a single
677459931ecSChris Mason 			 * item header.  So, we split the item in place,
678459931ecSChris Mason 			 * adding a new header pointing to the existing
679459931ecSChris Mason 			 * bytes.  Then we loop around again and we have
680459931ecSChris Mason 			 * a nicely formed csum item that we can neatly
681459931ecSChris Mason 			 * truncate.
682459931ecSChris Mason 			 */
683459931ecSChris Mason 			offset = (bytenr - key.offset) >> blocksize_bits;
684459931ecSChris Mason 			offset *= csum_size;
685459931ecSChris Mason 
686459931ecSChris Mason 			shift_len = (len >> blocksize_bits) * csum_size;
687459931ecSChris Mason 
688459931ecSChris Mason 			item_offset = btrfs_item_ptr_offset(leaf,
689459931ecSChris Mason 							    path->slots[0]);
690459931ecSChris Mason 
691459931ecSChris Mason 			memset_extent_buffer(leaf, 0, item_offset + offset,
692459931ecSChris Mason 					     shift_len);
693459931ecSChris Mason 			key.offset = bytenr;
694459931ecSChris Mason 
695459931ecSChris Mason 			/*
696459931ecSChris Mason 			 * btrfs_split_item returns -EAGAIN when the
697459931ecSChris Mason 			 * item changed size or key
698459931ecSChris Mason 			 */
699459931ecSChris Mason 			ret = btrfs_split_item(trans, root, path, &key, offset);
70079787eaaSJeff Mahoney 			if (ret && ret != -EAGAIN) {
701*66642832SJeff Mahoney 				btrfs_abort_transaction(trans, ret);
70279787eaaSJeff Mahoney 				goto out;
70379787eaaSJeff Mahoney 			}
704459931ecSChris Mason 
705459931ecSChris Mason 			key.offset = end_byte - 1;
706459931ecSChris Mason 		} else {
707afe5fea7STsutomu Itoh 			truncate_one_csum(root, path, &key, bytenr, len);
708dcbdd4dcSChris Mason 			if (key.offset < bytenr)
709dcbdd4dcSChris Mason 				break;
710459931ecSChris Mason 		}
711b3b4aa74SDavid Sterba 		btrfs_release_path(path);
712459931ecSChris Mason 	}
71365a246c5STsutomu Itoh 	ret = 0;
714459931ecSChris Mason out:
715459931ecSChris Mason 	btrfs_free_path(path);
71665a246c5STsutomu Itoh 	return ret;
717459931ecSChris Mason }
718459931ecSChris Mason 
719065631f6SChris Mason int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
720d20f7043SChris Mason 			   struct btrfs_root *root,
721e6dcd2dcSChris Mason 			   struct btrfs_ordered_sum *sums)
722f254e52cSChris Mason {
723f254e52cSChris Mason 	struct btrfs_key file_key;
7246567e837SChris Mason 	struct btrfs_key found_key;
7255caf2a00SChris Mason 	struct btrfs_path *path;
726f254e52cSChris Mason 	struct btrfs_csum_item *item;
727065631f6SChris Mason 	struct btrfs_csum_item *item_end;
728ff79f819SChris Mason 	struct extent_buffer *leaf = NULL;
729f51a4a18SMiao Xie 	u64 next_offset;
730f51a4a18SMiao Xie 	u64 total_bytes = 0;
7316567e837SChris Mason 	u64 csum_offset;
732f51a4a18SMiao Xie 	u64 bytenr;
733f578d4bdSChris Mason 	u32 nritems;
734f578d4bdSChris Mason 	u32 ins_size;
735f51a4a18SMiao Xie 	int index = 0;
736f51a4a18SMiao Xie 	int found_next;
737f51a4a18SMiao Xie 	int ret;
7386c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7396e92f5e6SChris Mason 
7405caf2a00SChris Mason 	path = btrfs_alloc_path();
741d8926bb3SMark Fasheh 	if (!path)
742d8926bb3SMark Fasheh 		return -ENOMEM;
743065631f6SChris Mason again:
744065631f6SChris Mason 	next_offset = (u64)-1;
745065631f6SChris Mason 	found_next = 0;
746f51a4a18SMiao Xie 	bytenr = sums->bytenr + total_bytes;
747d20f7043SChris Mason 	file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
748f51a4a18SMiao Xie 	file_key.offset = bytenr;
749962a298fSDavid Sterba 	file_key.type = BTRFS_EXTENT_CSUM_KEY;
750a429e513SChris Mason 
751f51a4a18SMiao Xie 	item = btrfs_lookup_csum(trans, root, path, bytenr, 1);
752ff79f819SChris Mason 	if (!IS_ERR(item)) {
753639cb586SChris Mason 		ret = 0;
754f51a4a18SMiao Xie 		leaf = path->nodes[0];
755f51a4a18SMiao Xie 		item_end = btrfs_item_ptr(leaf, path->slots[0],
756f51a4a18SMiao Xie 					  struct btrfs_csum_item);
757f51a4a18SMiao Xie 		item_end = (struct btrfs_csum_item *)((char *)item_end +
758f51a4a18SMiao Xie 			   btrfs_item_size_nr(leaf, path->slots[0]));
759a429e513SChris Mason 		goto found;
760ff79f819SChris Mason 	}
761a429e513SChris Mason 	ret = PTR_ERR(item);
7624a500fd1SYan, Zheng 	if (ret != -EFBIG && ret != -ENOENT)
7634a500fd1SYan, Zheng 		goto fail_unlock;
7644a500fd1SYan, Zheng 
765a429e513SChris Mason 	if (ret == -EFBIG) {
766a429e513SChris Mason 		u32 item_size;
767a429e513SChris Mason 		/* we found one, but it isn't big enough yet */
7685f39d397SChris Mason 		leaf = path->nodes[0];
7695f39d397SChris Mason 		item_size = btrfs_item_size_nr(leaf, path->slots[0]);
770607d432dSJosef Bacik 		if ((item_size / csum_size) >=
771607d432dSJosef Bacik 		    MAX_CSUM_ITEMS(root, csum_size)) {
772a429e513SChris Mason 			/* already at max size, make a new one */
773a429e513SChris Mason 			goto insert;
774a429e513SChris Mason 		}
775a429e513SChris Mason 	} else {
776f578d4bdSChris Mason 		int slot = path->slots[0] + 1;
777a429e513SChris Mason 		/* we didn't find a csum item, insert one */
778f578d4bdSChris Mason 		nritems = btrfs_header_nritems(path->nodes[0]);
77935045bf2SFilipe Manana 		if (!nritems || (path->slots[0] >= nritems - 1)) {
780f578d4bdSChris Mason 			ret = btrfs_next_leaf(root, path);
781b56baf5bSYan 			if (ret == 1)
782f578d4bdSChris Mason 				found_next = 1;
783b56baf5bSYan 			if (ret != 0)
784f578d4bdSChris Mason 				goto insert;
78527b9a812SFilipe Manana 			slot = path->slots[0];
786f578d4bdSChris Mason 		}
787f578d4bdSChris Mason 		btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
788d20f7043SChris Mason 		if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
789d20f7043SChris Mason 		    found_key.type != BTRFS_EXTENT_CSUM_KEY) {
790f578d4bdSChris Mason 			found_next = 1;
791f578d4bdSChris Mason 			goto insert;
792f578d4bdSChris Mason 		}
793f578d4bdSChris Mason 		next_offset = found_key.offset;
794f578d4bdSChris Mason 		found_next = 1;
795a429e513SChris Mason 		goto insert;
796a429e513SChris Mason 	}
797a429e513SChris Mason 
798a429e513SChris Mason 	/*
799a429e513SChris Mason 	 * at this point, we know the tree has an item, but it isn't big
800a429e513SChris Mason 	 * enough yet to put our csum in.  Grow it
801a429e513SChris Mason 	 */
802b3b4aa74SDavid Sterba 	btrfs_release_path(path);
8036567e837SChris Mason 	ret = btrfs_search_slot(trans, root, &file_key, path,
804607d432dSJosef Bacik 				csum_size, 1);
8056567e837SChris Mason 	if (ret < 0)
80653863232SChris Mason 		goto fail_unlock;
807459931ecSChris Mason 
808459931ecSChris Mason 	if (ret > 0) {
809459931ecSChris Mason 		if (path->slots[0] == 0)
8106567e837SChris Mason 			goto insert;
8116567e837SChris Mason 		path->slots[0]--;
812459931ecSChris Mason 	}
813459931ecSChris Mason 
8145f39d397SChris Mason 	leaf = path->nodes[0];
8155f39d397SChris Mason 	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
816d20f7043SChris Mason 	csum_offset = (bytenr - found_key.offset) >>
8176567e837SChris Mason 			root->fs_info->sb->s_blocksize_bits;
818459931ecSChris Mason 
819962a298fSDavid Sterba 	if (found_key.type != BTRFS_EXTENT_CSUM_KEY ||
820d20f7043SChris Mason 	    found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
821607d432dSJosef Bacik 	    csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) {
8226567e837SChris Mason 		goto insert;
8236567e837SChris Mason 	}
824459931ecSChris Mason 
8252f697dc6SLiu Bo 	if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) /
826607d432dSJosef Bacik 	    csum_size) {
8272f697dc6SLiu Bo 		int extend_nr;
8282f697dc6SLiu Bo 		u64 tmp;
8292f697dc6SLiu Bo 		u32 diff;
8302f697dc6SLiu Bo 		u32 free_space;
831459931ecSChris Mason 
8322f697dc6SLiu Bo 		if (btrfs_leaf_free_space(root, leaf) <
8332f697dc6SLiu Bo 				 sizeof(struct btrfs_item) + csum_size * 2)
8342f697dc6SLiu Bo 			goto insert;
8352f697dc6SLiu Bo 
8362f697dc6SLiu Bo 		free_space = btrfs_leaf_free_space(root, leaf) -
8372f697dc6SLiu Bo 					 sizeof(struct btrfs_item) - csum_size;
838f51a4a18SMiao Xie 		tmp = sums->len - total_bytes;
8392f697dc6SLiu Bo 		tmp >>= root->fs_info->sb->s_blocksize_bits;
8402f697dc6SLiu Bo 		WARN_ON(tmp < 1);
8412f697dc6SLiu Bo 
8422f697dc6SLiu Bo 		extend_nr = max_t(int, 1, (int)tmp);
8432f697dc6SLiu Bo 		diff = (csum_offset + extend_nr) * csum_size;
8442f697dc6SLiu Bo 		diff = min(diff, MAX_CSUM_ITEMS(root, csum_size) * csum_size);
845459931ecSChris Mason 
8465f39d397SChris Mason 		diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
8472f697dc6SLiu Bo 		diff = min(free_space, diff);
8482f697dc6SLiu Bo 		diff /= csum_size;
8492f697dc6SLiu Bo 		diff *= csum_size;
850459931ecSChris Mason 
8514b90c680STsutomu Itoh 		btrfs_extend_item(root, path, diff);
852f51a4a18SMiao Xie 		ret = 0;
8536567e837SChris Mason 		goto csum;
8546567e837SChris Mason 	}
8556567e837SChris Mason 
8566567e837SChris Mason insert:
857b3b4aa74SDavid Sterba 	btrfs_release_path(path);
8586567e837SChris Mason 	csum_offset = 0;
859f578d4bdSChris Mason 	if (found_next) {
8602f697dc6SLiu Bo 		u64 tmp;
861d20f7043SChris Mason 
862f51a4a18SMiao Xie 		tmp = sums->len - total_bytes;
863f578d4bdSChris Mason 		tmp >>= root->fs_info->sb->s_blocksize_bits;
8642f697dc6SLiu Bo 		tmp = min(tmp, (next_offset - file_key.offset) >>
8652f697dc6SLiu Bo 					 root->fs_info->sb->s_blocksize_bits);
8662f697dc6SLiu Bo 
867f578d4bdSChris Mason 		tmp = max((u64)1, tmp);
868607d432dSJosef Bacik 		tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size));
869607d432dSJosef Bacik 		ins_size = csum_size * tmp;
870f578d4bdSChris Mason 	} else {
871607d432dSJosef Bacik 		ins_size = csum_size;
872f578d4bdSChris Mason 	}
873b9473439SChris Mason 	path->leave_spinning = 1;
8745caf2a00SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, &file_key,
875f578d4bdSChris Mason 				      ins_size);
876b9473439SChris Mason 	path->leave_spinning = 0;
87754aa1f4dSChris Mason 	if (ret < 0)
87853863232SChris Mason 		goto fail_unlock;
879fae7f21cSDulshani Gunawardhana 	if (WARN_ON(ret != 0))
88053863232SChris Mason 		goto fail_unlock;
8815f39d397SChris Mason 	leaf = path->nodes[0];
882f51a4a18SMiao Xie csum:
8835f39d397SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
884f51a4a18SMiao Xie 	item_end = (struct btrfs_csum_item *)((unsigned char *)item +
885f51a4a18SMiao Xie 				      btrfs_item_size_nr(leaf, path->slots[0]));
886509659cdSChris Mason 	item = (struct btrfs_csum_item *)((unsigned char *)item +
887607d432dSJosef Bacik 					  csum_offset * csum_size);
888b18c6685SChris Mason found:
889f51a4a18SMiao Xie 	ins_size = (u32)(sums->len - total_bytes) >>
890f51a4a18SMiao Xie 		   root->fs_info->sb->s_blocksize_bits;
891f51a4a18SMiao Xie 	ins_size *= csum_size;
892f51a4a18SMiao Xie 	ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item,
893f51a4a18SMiao Xie 			      ins_size);
894f51a4a18SMiao Xie 	write_extent_buffer(leaf, sums->sums + index, (unsigned long)item,
895f51a4a18SMiao Xie 			    ins_size);
896aadfeb6eSChris Mason 
897f51a4a18SMiao Xie 	ins_size /= csum_size;
898f51a4a18SMiao Xie 	total_bytes += ins_size * root->sectorsize;
899f51a4a18SMiao Xie 	index += ins_size;
900a6591715SChris Mason 
9015caf2a00SChris Mason 	btrfs_mark_buffer_dirty(path->nodes[0]);
902e6dcd2dcSChris Mason 	if (total_bytes < sums->len) {
903b3b4aa74SDavid Sterba 		btrfs_release_path(path);
904b9473439SChris Mason 		cond_resched();
905065631f6SChris Mason 		goto again;
906065631f6SChris Mason 	}
90753863232SChris Mason out:
9085caf2a00SChris Mason 	btrfs_free_path(path);
909f254e52cSChris Mason 	return ret;
91053863232SChris Mason 
91153863232SChris Mason fail_unlock:
91253863232SChris Mason 	goto out;
913f254e52cSChris Mason }
9147ffbb598SFilipe Manana 
9157ffbb598SFilipe Manana void btrfs_extent_item_to_extent_map(struct inode *inode,
9167ffbb598SFilipe Manana 				     const struct btrfs_path *path,
9177ffbb598SFilipe Manana 				     struct btrfs_file_extent_item *fi,
9187ffbb598SFilipe Manana 				     const bool new_inline,
9197ffbb598SFilipe Manana 				     struct extent_map *em)
9207ffbb598SFilipe Manana {
9217ffbb598SFilipe Manana 	struct btrfs_root *root = BTRFS_I(inode)->root;
9227ffbb598SFilipe Manana 	struct extent_buffer *leaf = path->nodes[0];
9237ffbb598SFilipe Manana 	const int slot = path->slots[0];
9247ffbb598SFilipe Manana 	struct btrfs_key key;
9257ffbb598SFilipe Manana 	u64 extent_start, extent_end;
9267ffbb598SFilipe Manana 	u64 bytenr;
9277ffbb598SFilipe Manana 	u8 type = btrfs_file_extent_type(leaf, fi);
9287ffbb598SFilipe Manana 	int compress_type = btrfs_file_extent_compression(leaf, fi);
9297ffbb598SFilipe Manana 
9307ffbb598SFilipe Manana 	em->bdev = root->fs_info->fs_devices->latest_bdev;
9317ffbb598SFilipe Manana 	btrfs_item_key_to_cpu(leaf, &key, slot);
9327ffbb598SFilipe Manana 	extent_start = key.offset;
9337ffbb598SFilipe Manana 
9347ffbb598SFilipe Manana 	if (type == BTRFS_FILE_EXTENT_REG ||
9357ffbb598SFilipe Manana 	    type == BTRFS_FILE_EXTENT_PREALLOC) {
9367ffbb598SFilipe Manana 		extent_end = extent_start +
9377ffbb598SFilipe Manana 			btrfs_file_extent_num_bytes(leaf, fi);
9387ffbb598SFilipe Manana 	} else if (type == BTRFS_FILE_EXTENT_INLINE) {
9397ffbb598SFilipe Manana 		size_t size;
9407ffbb598SFilipe Manana 		size = btrfs_file_extent_inline_len(leaf, slot, fi);
9417ffbb598SFilipe Manana 		extent_end = ALIGN(extent_start + size, root->sectorsize);
9427ffbb598SFilipe Manana 	}
9437ffbb598SFilipe Manana 
9447ffbb598SFilipe Manana 	em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
9457ffbb598SFilipe Manana 	if (type == BTRFS_FILE_EXTENT_REG ||
9467ffbb598SFilipe Manana 	    type == BTRFS_FILE_EXTENT_PREALLOC) {
9477ffbb598SFilipe Manana 		em->start = extent_start;
9487ffbb598SFilipe Manana 		em->len = extent_end - extent_start;
9497ffbb598SFilipe Manana 		em->orig_start = extent_start -
9507ffbb598SFilipe Manana 			btrfs_file_extent_offset(leaf, fi);
9517ffbb598SFilipe Manana 		em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
9527ffbb598SFilipe Manana 		bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
9537ffbb598SFilipe Manana 		if (bytenr == 0) {
9547ffbb598SFilipe Manana 			em->block_start = EXTENT_MAP_HOLE;
9557ffbb598SFilipe Manana 			return;
9567ffbb598SFilipe Manana 		}
9577ffbb598SFilipe Manana 		if (compress_type != BTRFS_COMPRESS_NONE) {
9587ffbb598SFilipe Manana 			set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
9597ffbb598SFilipe Manana 			em->compress_type = compress_type;
9607ffbb598SFilipe Manana 			em->block_start = bytenr;
9617ffbb598SFilipe Manana 			em->block_len = em->orig_block_len;
9627ffbb598SFilipe Manana 		} else {
9637ffbb598SFilipe Manana 			bytenr += btrfs_file_extent_offset(leaf, fi);
9647ffbb598SFilipe Manana 			em->block_start = bytenr;
9657ffbb598SFilipe Manana 			em->block_len = em->len;
9667ffbb598SFilipe Manana 			if (type == BTRFS_FILE_EXTENT_PREALLOC)
9677ffbb598SFilipe Manana 				set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9687ffbb598SFilipe Manana 		}
9697ffbb598SFilipe Manana 	} else if (type == BTRFS_FILE_EXTENT_INLINE) {
9707ffbb598SFilipe Manana 		em->block_start = EXTENT_MAP_INLINE;
9717ffbb598SFilipe Manana 		em->start = extent_start;
9727ffbb598SFilipe Manana 		em->len = extent_end - extent_start;
9737ffbb598SFilipe Manana 		/*
9747ffbb598SFilipe Manana 		 * Initialize orig_start and block_len with the same values
9757ffbb598SFilipe Manana 		 * as in inode.c:btrfs_get_extent().
9767ffbb598SFilipe Manana 		 */
9777ffbb598SFilipe Manana 		em->orig_start = EXTENT_MAP_HOLE;
9787ffbb598SFilipe Manana 		em->block_len = (u64)-1;
9797ffbb598SFilipe Manana 		if (!new_inline && compress_type != BTRFS_COMPRESS_NONE) {
9807ffbb598SFilipe Manana 			set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
9817ffbb598SFilipe Manana 			em->compress_type = compress_type;
9827ffbb598SFilipe Manana 		}
9837ffbb598SFilipe Manana 	} else {
9847ffbb598SFilipe Manana 		btrfs_err(root->fs_info,
9857ffbb598SFilipe Manana 			  "unknown file extent item type %d, inode %llu, offset %llu, root %llu",
9867ffbb598SFilipe Manana 			  type, btrfs_ino(inode), extent_start,
9877ffbb598SFilipe Manana 			  root->root_key.objectid);
9887ffbb598SFilipe Manana 	}
9897ffbb598SFilipe Manana }
990