xref: /openbmc/linux/fs/btrfs/file-item.c (revision ebb8765b2ded869b75bf5154b048119eb52571f7)
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"
28*ebb8765bSAnand Jain #include "compression.h"
291e1d2701SChris Mason 
30995e01b7SJan Schmidt #define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \
31a429e513SChris Mason 				   sizeof(struct btrfs_item) * 2) / \
32607d432dSJosef Bacik 				  size) - 1))
3307d400a6SYan Zheng 
34221b8318SZach Brown #define MAX_CSUM_ITEMS(r, size) (min_t(u32, __MAX_CSUM_ITEMS(r, size), \
35221b8318SZach Brown 				       PAGE_CACHE_SIZE))
367ca4be45SChris Mason 
3707d400a6SYan Zheng #define MAX_ORDERED_SUM_BYTES(r) ((PAGE_SIZE - \
3807d400a6SYan Zheng 				   sizeof(struct btrfs_ordered_sum)) / \
39f51a4a18SMiao Xie 				   sizeof(u32) * (r)->sectorsize)
4007d400a6SYan Zheng 
41b18c6685SChris Mason int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
42dee26a9fSChris Mason 			     struct btrfs_root *root,
43b18c6685SChris Mason 			     u64 objectid, u64 pos,
44f2eb0a24SSage Weil 			     u64 disk_offset, u64 disk_num_bytes,
45c8b97818SChris Mason 			     u64 num_bytes, u64 offset, u64 ram_bytes,
46c8b97818SChris Mason 			     u8 compression, u8 encryption, u16 other_encoding)
479f5fae2fSChris Mason {
48dee26a9fSChris Mason 	int ret = 0;
49dee26a9fSChris Mason 	struct btrfs_file_extent_item *item;
50dee26a9fSChris Mason 	struct btrfs_key file_key;
515caf2a00SChris Mason 	struct btrfs_path *path;
525f39d397SChris Mason 	struct extent_buffer *leaf;
53dee26a9fSChris Mason 
545caf2a00SChris Mason 	path = btrfs_alloc_path();
55db5b493aSTsutomu Itoh 	if (!path)
56db5b493aSTsutomu Itoh 		return -ENOMEM;
57dee26a9fSChris Mason 	file_key.objectid = objectid;
58b18c6685SChris Mason 	file_key.offset = pos;
59962a298fSDavid Sterba 	file_key.type = BTRFS_EXTENT_DATA_KEY;
60dee26a9fSChris Mason 
61b9473439SChris Mason 	path->leave_spinning = 1;
625caf2a00SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, &file_key,
63dee26a9fSChris Mason 				      sizeof(*item));
6454aa1f4dSChris Mason 	if (ret < 0)
6554aa1f4dSChris Mason 		goto out;
6679787eaaSJeff Mahoney 	BUG_ON(ret); /* Can't happen */
675f39d397SChris Mason 	leaf = path->nodes[0];
685f39d397SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0],
69dee26a9fSChris Mason 			      struct btrfs_file_extent_item);
70f2eb0a24SSage Weil 	btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset);
71db94535dSChris Mason 	btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
72f2eb0a24SSage Weil 	btrfs_set_file_extent_offset(leaf, item, offset);
73db94535dSChris Mason 	btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
74c8b97818SChris Mason 	btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes);
755f39d397SChris Mason 	btrfs_set_file_extent_generation(leaf, item, trans->transid);
765f39d397SChris Mason 	btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
77c8b97818SChris Mason 	btrfs_set_file_extent_compression(leaf, item, compression);
78c8b97818SChris Mason 	btrfs_set_file_extent_encryption(leaf, item, encryption);
79c8b97818SChris Mason 	btrfs_set_file_extent_other_encoding(leaf, item, other_encoding);
80c8b97818SChris Mason 
815f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
8254aa1f4dSChris Mason out:
835caf2a00SChris Mason 	btrfs_free_path(path);
8454aa1f4dSChris Mason 	return ret;
859f5fae2fSChris Mason }
86dee26a9fSChris Mason 
8748a3b636SEric Sandeen static struct btrfs_csum_item *
8848a3b636SEric Sandeen btrfs_lookup_csum(struct btrfs_trans_handle *trans,
89b18c6685SChris Mason 		  struct btrfs_root *root,
906567e837SChris Mason 		  struct btrfs_path *path,
91d20f7043SChris Mason 		  u64 bytenr, int cow)
926567e837SChris Mason {
936567e837SChris Mason 	int ret;
946567e837SChris Mason 	struct btrfs_key file_key;
956567e837SChris Mason 	struct btrfs_key found_key;
966567e837SChris Mason 	struct btrfs_csum_item *item;
975f39d397SChris Mason 	struct extent_buffer *leaf;
986567e837SChris Mason 	u64 csum_offset = 0;
996c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
100a429e513SChris Mason 	int csums_in_item;
1016567e837SChris Mason 
102d20f7043SChris Mason 	file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
103d20f7043SChris Mason 	file_key.offset = bytenr;
104962a298fSDavid Sterba 	file_key.type = BTRFS_EXTENT_CSUM_KEY;
105b18c6685SChris Mason 	ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
1066567e837SChris Mason 	if (ret < 0)
1076567e837SChris Mason 		goto fail;
1085f39d397SChris Mason 	leaf = path->nodes[0];
1096567e837SChris Mason 	if (ret > 0) {
1106567e837SChris Mason 		ret = 1;
11170b2befdSChris Mason 		if (path->slots[0] == 0)
1126567e837SChris Mason 			goto fail;
1136567e837SChris Mason 		path->slots[0]--;
1145f39d397SChris Mason 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
115962a298fSDavid Sterba 		if (found_key.type != BTRFS_EXTENT_CSUM_KEY)
1166567e837SChris Mason 			goto fail;
117d20f7043SChris Mason 
118d20f7043SChris Mason 		csum_offset = (bytenr - found_key.offset) >>
1196567e837SChris Mason 				root->fs_info->sb->s_blocksize_bits;
1205f39d397SChris Mason 		csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
121607d432dSJosef Bacik 		csums_in_item /= csum_size;
122a429e513SChris Mason 
12382d130ffSMiao Xie 		if (csum_offset == csums_in_item) {
124a429e513SChris Mason 			ret = -EFBIG;
1256567e837SChris Mason 			goto fail;
12682d130ffSMiao Xie 		} else if (csum_offset > csums_in_item) {
12782d130ffSMiao Xie 			goto fail;
1286567e837SChris Mason 		}
1296567e837SChris Mason 	}
1306567e837SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
131509659cdSChris Mason 	item = (struct btrfs_csum_item *)((unsigned char *)item +
132607d432dSJosef Bacik 					  csum_offset * csum_size);
1336567e837SChris Mason 	return item;
1346567e837SChris Mason fail:
1356567e837SChris Mason 	if (ret > 0)
136b18c6685SChris Mason 		ret = -ENOENT;
1376567e837SChris Mason 	return ERR_PTR(ret);
1386567e837SChris Mason }
1396567e837SChris Mason 
140dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
141dee26a9fSChris Mason 			     struct btrfs_root *root,
142dee26a9fSChris Mason 			     struct btrfs_path *path, u64 objectid,
1439773a788SChris Mason 			     u64 offset, int mod)
144dee26a9fSChris Mason {
145dee26a9fSChris Mason 	int ret;
146dee26a9fSChris Mason 	struct btrfs_key file_key;
147dee26a9fSChris Mason 	int ins_len = mod < 0 ? -1 : 0;
148dee26a9fSChris Mason 	int cow = mod != 0;
149dee26a9fSChris Mason 
150dee26a9fSChris Mason 	file_key.objectid = objectid;
15170b2befdSChris Mason 	file_key.offset = offset;
152962a298fSDavid Sterba 	file_key.type = BTRFS_EXTENT_DATA_KEY;
153dee26a9fSChris Mason 	ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
154dee26a9fSChris Mason 	return ret;
155dee26a9fSChris Mason }
156f254e52cSChris Mason 
157facc8a22SMiao Xie static void btrfs_io_bio_endio_readpage(struct btrfs_io_bio *bio, int err)
158facc8a22SMiao Xie {
159facc8a22SMiao Xie 	kfree(bio->csum_allocated);
160facc8a22SMiao Xie }
161facc8a22SMiao Xie 
1624b46fce2SJosef Bacik static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
1634b46fce2SJosef Bacik 				   struct inode *inode, struct bio *bio,
1644b46fce2SJosef Bacik 				   u64 logical_offset, u32 *dst, int dio)
16561b49440SChris Mason {
16661b49440SChris Mason 	struct bio_vec *bvec = bio->bi_io_vec;
167facc8a22SMiao Xie 	struct btrfs_io_bio *btrfs_bio = btrfs_io_bio(bio);
168facc8a22SMiao Xie 	struct btrfs_csum_item *item = NULL;
169facc8a22SMiao Xie 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
170facc8a22SMiao Xie 	struct btrfs_path *path;
171facc8a22SMiao Xie 	u8 *csum;
1724b46fce2SJosef Bacik 	u64 offset = 0;
17361b49440SChris Mason 	u64 item_start_offset = 0;
17461b49440SChris Mason 	u64 item_last_offset = 0;
175d20f7043SChris Mason 	u64 disk_bytenr;
176c40a3d38SChandan Rajendra 	u64 page_bytes_left;
17761b49440SChris Mason 	u32 diff;
178facc8a22SMiao Xie 	int nblocks;
179facc8a22SMiao Xie 	int bio_index = 0;
180e4100d98SMiao Xie 	int count;
181facc8a22SMiao Xie 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
18261b49440SChris Mason 
18361b49440SChris Mason 	path = btrfs_alloc_path();
184c2db1073STsutomu Itoh 	if (!path)
185c2db1073STsutomu Itoh 		return -ENOMEM;
186facc8a22SMiao Xie 
1874f024f37SKent Overstreet 	nblocks = bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
188facc8a22SMiao Xie 	if (!dst) {
189facc8a22SMiao Xie 		if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) {
19031e818feSDavid Sterba 			btrfs_bio->csum_allocated = kmalloc_array(nblocks,
19131e818feSDavid Sterba 					csum_size, GFP_NOFS);
192facc8a22SMiao Xie 			if (!btrfs_bio->csum_allocated) {
193facc8a22SMiao Xie 				btrfs_free_path(path);
194facc8a22SMiao Xie 				return -ENOMEM;
195facc8a22SMiao Xie 			}
196facc8a22SMiao Xie 			btrfs_bio->csum = btrfs_bio->csum_allocated;
197facc8a22SMiao Xie 			btrfs_bio->end_io = btrfs_io_bio_endio_readpage;
198facc8a22SMiao Xie 		} else {
199facc8a22SMiao Xie 			btrfs_bio->csum = btrfs_bio->csum_inline;
200facc8a22SMiao Xie 		}
201facc8a22SMiao Xie 		csum = btrfs_bio->csum;
202facc8a22SMiao Xie 	} else {
203facc8a22SMiao Xie 		csum = (u8 *)dst;
204facc8a22SMiao Xie 	}
205facc8a22SMiao Xie 
2064f024f37SKent Overstreet 	if (bio->bi_iter.bi_size > PAGE_CACHE_SIZE * 8)
207e4058b54SDavid Sterba 		path->reada = READA_FORWARD;
20861b49440SChris Mason 
20961b49440SChris Mason 	WARN_ON(bio->bi_vcnt <= 0);
21061b49440SChris Mason 
2112cf8572dSChris Mason 	/*
2122cf8572dSChris Mason 	 * the free space stuff is only read when it hasn't been
2132cf8572dSChris Mason 	 * updated in the current transaction.  So, we can safely
2142cf8572dSChris Mason 	 * read from the commit root and sidestep a nasty deadlock
2152cf8572dSChris Mason 	 * between reading the free space cache and updating the csum tree.
2162cf8572dSChris Mason 	 */
21783eea1f1SLiu Bo 	if (btrfs_is_free_space_inode(inode)) {
2182cf8572dSChris Mason 		path->search_commit_root = 1;
219ddf23b3fSJosef Bacik 		path->skip_locking = 1;
220ddf23b3fSJosef Bacik 	}
2212cf8572dSChris Mason 
2224f024f37SKent Overstreet 	disk_bytenr = (u64)bio->bi_iter.bi_sector << 9;
2234b46fce2SJosef Bacik 	if (dio)
2244b46fce2SJosef Bacik 		offset = logical_offset;
225c40a3d38SChandan Rajendra 
226c40a3d38SChandan Rajendra 	page_bytes_left = bvec->bv_len;
22761b49440SChris Mason 	while (bio_index < bio->bi_vcnt) {
2284b46fce2SJosef Bacik 		if (!dio)
22961b49440SChris Mason 			offset = page_offset(bvec->bv_page) + bvec->bv_offset;
230facc8a22SMiao Xie 		count = btrfs_find_ordered_sum(inode, offset, disk_bytenr,
231facc8a22SMiao Xie 					       (u32 *)csum, nblocks);
232e4100d98SMiao Xie 		if (count)
23361b49440SChris Mason 			goto found;
23461b49440SChris Mason 
235d20f7043SChris Mason 		if (!item || disk_bytenr < item_start_offset ||
236d20f7043SChris Mason 		    disk_bytenr >= item_last_offset) {
23761b49440SChris Mason 			struct btrfs_key found_key;
23861b49440SChris Mason 			u32 item_size;
23961b49440SChris Mason 
24061b49440SChris Mason 			if (item)
241b3b4aa74SDavid Sterba 				btrfs_release_path(path);
242d20f7043SChris Mason 			item = btrfs_lookup_csum(NULL, root->fs_info->csum_root,
243d20f7043SChris Mason 						 path, disk_bytenr, 0);
24461b49440SChris Mason 			if (IS_ERR(item)) {
245e4100d98SMiao Xie 				count = 1;
246facc8a22SMiao Xie 				memset(csum, 0, csum_size);
24717d217feSYan Zheng 				if (BTRFS_I(inode)->root->root_key.objectid ==
24817d217feSYan Zheng 				    BTRFS_DATA_RELOC_TREE_OBJECTID) {
24917d217feSYan Zheng 					set_extent_bits(io_tree, offset,
250c40a3d38SChandan Rajendra 						offset + root->sectorsize - 1,
25117d217feSYan Zheng 						EXTENT_NODATASUM, GFP_NOFS);
25217d217feSYan Zheng 				} else {
253efe120a0SFrank Holton 					btrfs_info(BTRFS_I(inode)->root->fs_info,
254efe120a0SFrank Holton 						   "no csum found for inode %llu start %llu",
255c1c9ff7cSGeert Uytterhoeven 					       btrfs_ino(inode), offset);
25617d217feSYan Zheng 				}
2576dab8157SChris Mason 				item = NULL;
258b3b4aa74SDavid Sterba 				btrfs_release_path(path);
25961b49440SChris Mason 				goto found;
26061b49440SChris Mason 			}
26161b49440SChris Mason 			btrfs_item_key_to_cpu(path->nodes[0], &found_key,
26261b49440SChris Mason 					      path->slots[0]);
26361b49440SChris Mason 
26461b49440SChris Mason 			item_start_offset = found_key.offset;
26561b49440SChris Mason 			item_size = btrfs_item_size_nr(path->nodes[0],
26661b49440SChris Mason 						       path->slots[0]);
26761b49440SChris Mason 			item_last_offset = item_start_offset +
268607d432dSJosef Bacik 				(item_size / csum_size) *
26961b49440SChris Mason 				root->sectorsize;
27061b49440SChris Mason 			item = btrfs_item_ptr(path->nodes[0], path->slots[0],
27161b49440SChris Mason 					      struct btrfs_csum_item);
27261b49440SChris Mason 		}
27361b49440SChris Mason 		/*
27461b49440SChris Mason 		 * this byte range must be able to fit inside
27561b49440SChris Mason 		 * a single leaf so it will also fit inside a u32
27661b49440SChris Mason 		 */
277d20f7043SChris Mason 		diff = disk_bytenr - item_start_offset;
27861b49440SChris Mason 		diff = diff / root->sectorsize;
279607d432dSJosef Bacik 		diff = diff * csum_size;
280facc8a22SMiao Xie 		count = min_t(int, nblocks, (item_last_offset - disk_bytenr) >>
281e4100d98SMiao Xie 					    inode->i_sb->s_blocksize_bits);
282facc8a22SMiao Xie 		read_extent_buffer(path->nodes[0], csum,
2833de9d6b6SChris Mason 				   ((unsigned long)item) + diff,
284e4100d98SMiao Xie 				   csum_size * count);
28561b49440SChris Mason found:
286facc8a22SMiao Xie 		csum += count * csum_size;
287facc8a22SMiao Xie 		nblocks -= count;
288c40a3d38SChandan Rajendra 
289e4100d98SMiao Xie 		while (count--) {
290c40a3d38SChandan Rajendra 			disk_bytenr += root->sectorsize;
291c40a3d38SChandan Rajendra 			offset += root->sectorsize;
292c40a3d38SChandan Rajendra 			page_bytes_left -= root->sectorsize;
293c40a3d38SChandan Rajendra 			if (!page_bytes_left) {
294c40a3d38SChandan Rajendra 				bio_index++;
29561b49440SChris Mason 				bvec++;
296c40a3d38SChandan Rajendra 				page_bytes_left = bvec->bv_len;
297c40a3d38SChandan Rajendra 			}
298c40a3d38SChandan Rajendra 
29961b49440SChris Mason 		}
300e4100d98SMiao Xie 	}
30161b49440SChris Mason 	btrfs_free_path(path);
30261b49440SChris Mason 	return 0;
30361b49440SChris Mason }
30461b49440SChris Mason 
3054b46fce2SJosef Bacik int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
3064b46fce2SJosef Bacik 			  struct bio *bio, u32 *dst)
3074b46fce2SJosef Bacik {
3084b46fce2SJosef Bacik 	return __btrfs_lookup_bio_sums(root, inode, bio, 0, dst, 0);
3094b46fce2SJosef Bacik }
3104b46fce2SJosef Bacik 
3114b46fce2SJosef Bacik int btrfs_lookup_bio_sums_dio(struct btrfs_root *root, struct inode *inode,
31223ea8e5aSMiao Xie 			      struct bio *bio, u64 offset)
3134b46fce2SJosef Bacik {
31423ea8e5aSMiao Xie 	return __btrfs_lookup_bio_sums(root, inode, bio, offset, NULL, 1);
3154b46fce2SJosef Bacik }
3164b46fce2SJosef Bacik 
31717d217feSYan Zheng int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
318a2de733cSArne Jansen 			     struct list_head *list, int search_commit)
31917d217feSYan Zheng {
32017d217feSYan Zheng 	struct btrfs_key key;
32117d217feSYan Zheng 	struct btrfs_path *path;
32217d217feSYan Zheng 	struct extent_buffer *leaf;
32317d217feSYan Zheng 	struct btrfs_ordered_sum *sums;
32417d217feSYan Zheng 	struct btrfs_csum_item *item;
3250678b618SMark Fasheh 	LIST_HEAD(tmplist);
32617d217feSYan Zheng 	unsigned long offset;
32717d217feSYan Zheng 	int ret;
32817d217feSYan Zheng 	size_t size;
32917d217feSYan Zheng 	u64 csum_end;
3306c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
33117d217feSYan Zheng 
332d1b00a47SSatoru Takeuchi 	ASSERT(IS_ALIGNED(start, root->sectorsize) &&
333d1b00a47SSatoru Takeuchi 	       IS_ALIGNED(end + 1, root->sectorsize));
3344277a9c3SJosef Bacik 
33517d217feSYan Zheng 	path = btrfs_alloc_path();
336d8926bb3SMark Fasheh 	if (!path)
337d8926bb3SMark Fasheh 		return -ENOMEM;
33817d217feSYan Zheng 
339a2de733cSArne Jansen 	if (search_commit) {
340a2de733cSArne Jansen 		path->skip_locking = 1;
341e4058b54SDavid Sterba 		path->reada = READA_FORWARD;
342a2de733cSArne Jansen 		path->search_commit_root = 1;
343a2de733cSArne Jansen 	}
344a2de733cSArne Jansen 
34517d217feSYan Zheng 	key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
34617d217feSYan Zheng 	key.offset = start;
34717d217feSYan Zheng 	key.type = BTRFS_EXTENT_CSUM_KEY;
34817d217feSYan Zheng 
34907d400a6SYan Zheng 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
35017d217feSYan Zheng 	if (ret < 0)
35117d217feSYan Zheng 		goto fail;
35217d217feSYan Zheng 	if (ret > 0 && path->slots[0] > 0) {
35317d217feSYan Zheng 		leaf = path->nodes[0];
35417d217feSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
35517d217feSYan Zheng 		if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
35617d217feSYan Zheng 		    key.type == BTRFS_EXTENT_CSUM_KEY) {
35717d217feSYan Zheng 			offset = (start - key.offset) >>
35817d217feSYan Zheng 				 root->fs_info->sb->s_blocksize_bits;
35917d217feSYan Zheng 			if (offset * csum_size <
36017d217feSYan Zheng 			    btrfs_item_size_nr(leaf, path->slots[0] - 1))
36117d217feSYan Zheng 				path->slots[0]--;
36217d217feSYan Zheng 		}
36317d217feSYan Zheng 	}
36417d217feSYan Zheng 
36517d217feSYan Zheng 	while (start <= end) {
36617d217feSYan Zheng 		leaf = path->nodes[0];
36717d217feSYan Zheng 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
36807d400a6SYan Zheng 			ret = btrfs_next_leaf(root, path);
36917d217feSYan Zheng 			if (ret < 0)
37017d217feSYan Zheng 				goto fail;
37117d217feSYan Zheng 			if (ret > 0)
37217d217feSYan Zheng 				break;
37317d217feSYan Zheng 			leaf = path->nodes[0];
37417d217feSYan Zheng 		}
37517d217feSYan Zheng 
37617d217feSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
37717d217feSYan Zheng 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
378628c8282SZhi Yong Wu 		    key.type != BTRFS_EXTENT_CSUM_KEY ||
379628c8282SZhi Yong Wu 		    key.offset > end)
38017d217feSYan Zheng 			break;
38117d217feSYan Zheng 
38217d217feSYan Zheng 		if (key.offset > start)
38317d217feSYan Zheng 			start = key.offset;
38417d217feSYan Zheng 
38517d217feSYan Zheng 		size = btrfs_item_size_nr(leaf, path->slots[0]);
38617d217feSYan Zheng 		csum_end = key.offset + (size / csum_size) * root->sectorsize;
38787b29b20SYan Zheng 		if (csum_end <= start) {
38887b29b20SYan Zheng 			path->slots[0]++;
38987b29b20SYan Zheng 			continue;
39087b29b20SYan Zheng 		}
39117d217feSYan Zheng 
39207d400a6SYan Zheng 		csum_end = min(csum_end, end + 1);
39307d400a6SYan Zheng 		item = btrfs_item_ptr(path->nodes[0], path->slots[0],
39407d400a6SYan Zheng 				      struct btrfs_csum_item);
39507d400a6SYan Zheng 		while (start < csum_end) {
39607d400a6SYan Zheng 			size = min_t(size_t, csum_end - start,
39707d400a6SYan Zheng 				     MAX_ORDERED_SUM_BYTES(root));
39807d400a6SYan Zheng 			sums = kzalloc(btrfs_ordered_sum_size(root, size),
39907d400a6SYan Zheng 				       GFP_NOFS);
4000678b618SMark Fasheh 			if (!sums) {
4010678b618SMark Fasheh 				ret = -ENOMEM;
4020678b618SMark Fasheh 				goto fail;
4030678b618SMark Fasheh 			}
40417d217feSYan Zheng 
40517d217feSYan Zheng 			sums->bytenr = start;
406f51a4a18SMiao Xie 			sums->len = (int)size;
40717d217feSYan Zheng 
40817d217feSYan Zheng 			offset = (start - key.offset) >>
40917d217feSYan Zheng 				root->fs_info->sb->s_blocksize_bits;
41017d217feSYan Zheng 			offset *= csum_size;
411f51a4a18SMiao Xie 			size >>= root->fs_info->sb->s_blocksize_bits;
41217d217feSYan Zheng 
41307d400a6SYan Zheng 			read_extent_buffer(path->nodes[0],
414f51a4a18SMiao Xie 					   sums->sums,
415f51a4a18SMiao Xie 					   ((unsigned long)item) + offset,
416f51a4a18SMiao Xie 					   csum_size * size);
41717d217feSYan Zheng 
418f51a4a18SMiao Xie 			start += root->sectorsize * size;
4190678b618SMark Fasheh 			list_add_tail(&sums->list, &tmplist);
42007d400a6SYan Zheng 		}
42117d217feSYan Zheng 		path->slots[0]++;
42217d217feSYan Zheng 	}
42317d217feSYan Zheng 	ret = 0;
42417d217feSYan Zheng fail:
4250678b618SMark Fasheh 	while (ret < 0 && !list_empty(&tmplist)) {
4266e5aafb2SChris Mason 		sums = list_entry(tmplist.next, struct btrfs_ordered_sum, list);
4270678b618SMark Fasheh 		list_del(&sums->list);
4280678b618SMark Fasheh 		kfree(sums);
4290678b618SMark Fasheh 	}
4300678b618SMark Fasheh 	list_splice_tail(&tmplist, list);
4310678b618SMark Fasheh 
43217d217feSYan Zheng 	btrfs_free_path(path);
43317d217feSYan Zheng 	return ret;
43417d217feSYan Zheng }
43517d217feSYan Zheng 
4363edf7d33SChris Mason int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
437d20f7043SChris Mason 		       struct bio *bio, u64 file_start, int contig)
438e015640fSChris Mason {
439e6dcd2dcSChris Mason 	struct btrfs_ordered_sum *sums;
4403edf7d33SChris Mason 	struct btrfs_ordered_extent *ordered;
441e015640fSChris Mason 	char *data;
442e015640fSChris Mason 	struct bio_vec *bvec = bio->bi_io_vec;
443e015640fSChris Mason 	int bio_index = 0;
444f51a4a18SMiao Xie 	int index;
445c40a3d38SChandan Rajendra 	int nr_sectors;
446c40a3d38SChandan Rajendra 	int i;
4473edf7d33SChris Mason 	unsigned long total_bytes = 0;
4483edf7d33SChris Mason 	unsigned long this_sum_bytes = 0;
4493edf7d33SChris Mason 	u64 offset;
450e015640fSChris Mason 
451e6dcd2dcSChris Mason 	WARN_ON(bio->bi_vcnt <= 0);
4524f024f37SKent Overstreet 	sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_iter.bi_size),
4534f024f37SKent Overstreet 		       GFP_NOFS);
454e015640fSChris Mason 	if (!sums)
455e015640fSChris Mason 		return -ENOMEM;
4563edf7d33SChris Mason 
4574f024f37SKent Overstreet 	sums->len = bio->bi_iter.bi_size;
458e6dcd2dcSChris Mason 	INIT_LIST_HEAD(&sums->list);
459d20f7043SChris Mason 
460d20f7043SChris Mason 	if (contig)
461d20f7043SChris Mason 		offset = file_start;
462d20f7043SChris Mason 	else
463d20f7043SChris Mason 		offset = page_offset(bvec->bv_page) + bvec->bv_offset;
464d20f7043SChris Mason 
465d20f7043SChris Mason 	ordered = btrfs_lookup_ordered_extent(inode, offset);
46679787eaaSJeff Mahoney 	BUG_ON(!ordered); /* Logic error */
4674f024f37SKent Overstreet 	sums->bytenr = (u64)bio->bi_iter.bi_sector << 9;
468f51a4a18SMiao Xie 	index = 0;
469e015640fSChris Mason 
470e015640fSChris Mason 	while (bio_index < bio->bi_vcnt) {
471d20f7043SChris Mason 		if (!contig)
4723edf7d33SChris Mason 			offset = page_offset(bvec->bv_page) + bvec->bv_offset;
473d20f7043SChris Mason 
474c40a3d38SChandan Rajendra 		data = kmap_atomic(bvec->bv_page);
475c40a3d38SChandan Rajendra 
476c40a3d38SChandan Rajendra 		nr_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info,
477c40a3d38SChandan Rajendra 						bvec->bv_len + root->sectorsize
478c40a3d38SChandan Rajendra 						- 1);
479c40a3d38SChandan Rajendra 
480c40a3d38SChandan Rajendra 		for (i = 0; i < nr_sectors; i++) {
481e58dd74bSJosef Bacik 			if (offset >= ordered->file_offset + ordered->len ||
482e58dd74bSJosef Bacik 				offset < ordered->file_offset) {
4833edf7d33SChris Mason 				unsigned long bytes_left;
484c40a3d38SChandan Rajendra 
485c40a3d38SChandan Rajendra 				kunmap_atomic(data);
4863edf7d33SChris Mason 				sums->len = this_sum_bytes;
4873edf7d33SChris Mason 				this_sum_bytes = 0;
4883edf7d33SChris Mason 				btrfs_add_ordered_sum(inode, ordered, sums);
4893edf7d33SChris Mason 				btrfs_put_ordered_extent(ordered);
4903edf7d33SChris Mason 
4914f024f37SKent Overstreet 				bytes_left = bio->bi_iter.bi_size - total_bytes;
4923edf7d33SChris Mason 
4933edf7d33SChris Mason 				sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
4943edf7d33SChris Mason 					GFP_NOFS);
49579787eaaSJeff Mahoney 				BUG_ON(!sums); /* -ENOMEM */
4963edf7d33SChris Mason 				sums->len = bytes_left;
497c40a3d38SChandan Rajendra 				ordered = btrfs_lookup_ordered_extent(inode,
498c40a3d38SChandan Rajendra 								offset);
499c40a3d38SChandan Rajendra 				ASSERT(ordered); /* Logic error */
500c40a3d38SChandan Rajendra 				sums->bytenr = ((u64)bio->bi_iter.bi_sector << 9)
501c40a3d38SChandan Rajendra 					+ total_bytes;
502f51a4a18SMiao Xie 				index = 0;
5033edf7d33SChris Mason 
5047ac687d9SCong Wang 				data = kmap_atomic(bvec->bv_page);
505c40a3d38SChandan Rajendra 			}
506c40a3d38SChandan Rajendra 
507f51a4a18SMiao Xie 			sums->sums[index] = ~(u32)0;
508c40a3d38SChandan Rajendra 			sums->sums[index]
509c40a3d38SChandan Rajendra 				= btrfs_csum_data(data + bvec->bv_offset
510c40a3d38SChandan Rajendra 						+ (i * root->sectorsize),
511f51a4a18SMiao Xie 						sums->sums[index],
512c40a3d38SChandan Rajendra 						root->sectorsize);
513f51a4a18SMiao Xie 			btrfs_csum_final(sums->sums[index],
514f51a4a18SMiao Xie 					(char *)(sums->sums + index));
515c40a3d38SChandan Rajendra 			index++;
516c40a3d38SChandan Rajendra 			offset += root->sectorsize;
517c40a3d38SChandan Rajendra 			this_sum_bytes += root->sectorsize;
518c40a3d38SChandan Rajendra 			total_bytes += root->sectorsize;
519c40a3d38SChandan Rajendra 		}
520c40a3d38SChandan Rajendra 
521c40a3d38SChandan Rajendra 		kunmap_atomic(data);
522ed98b56aSChris Mason 
523e015640fSChris Mason 		bio_index++;
524e015640fSChris Mason 		bvec++;
525e015640fSChris Mason 	}
526ed98b56aSChris Mason 	this_sum_bytes = 0;
5273edf7d33SChris Mason 	btrfs_add_ordered_sum(inode, ordered, sums);
5283edf7d33SChris Mason 	btrfs_put_ordered_extent(ordered);
529e015640fSChris Mason 	return 0;
530e015640fSChris Mason }
531e015640fSChris Mason 
532459931ecSChris Mason /*
533459931ecSChris Mason  * helper function for csum removal, this expects the
534459931ecSChris Mason  * key to describe the csum pointed to by the path, and it expects
535459931ecSChris Mason  * the csum to overlap the range [bytenr, len]
536459931ecSChris Mason  *
537459931ecSChris Mason  * The csum should not be entirely contained in the range and the
538459931ecSChris Mason  * range should not be entirely contained in the csum.
539459931ecSChris Mason  *
540459931ecSChris Mason  * This calls btrfs_truncate_item with the correct args based on the
541459931ecSChris Mason  * overlap, and fixes up the key as required.
542459931ecSChris Mason  */
543afe5fea7STsutomu Itoh static noinline void truncate_one_csum(struct btrfs_root *root,
544459931ecSChris Mason 				       struct btrfs_path *path,
545459931ecSChris Mason 				       struct btrfs_key *key,
546459931ecSChris Mason 				       u64 bytenr, u64 len)
547459931ecSChris Mason {
548459931ecSChris Mason 	struct extent_buffer *leaf;
5496c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
550459931ecSChris Mason 	u64 csum_end;
551459931ecSChris Mason 	u64 end_byte = bytenr + len;
552459931ecSChris Mason 	u32 blocksize_bits = root->fs_info->sb->s_blocksize_bits;
553459931ecSChris Mason 
554459931ecSChris Mason 	leaf = path->nodes[0];
555459931ecSChris Mason 	csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
556459931ecSChris Mason 	csum_end <<= root->fs_info->sb->s_blocksize_bits;
557459931ecSChris Mason 	csum_end += key->offset;
558459931ecSChris Mason 
559459931ecSChris Mason 	if (key->offset < bytenr && csum_end <= end_byte) {
560459931ecSChris Mason 		/*
561459931ecSChris Mason 		 *         [ bytenr - len ]
562459931ecSChris Mason 		 *         [   ]
563459931ecSChris Mason 		 *   [csum     ]
564459931ecSChris Mason 		 *   A simple truncate off the end of the item
565459931ecSChris Mason 		 */
566459931ecSChris Mason 		u32 new_size = (bytenr - key->offset) >> blocksize_bits;
567459931ecSChris Mason 		new_size *= csum_size;
568afe5fea7STsutomu Itoh 		btrfs_truncate_item(root, path, new_size, 1);
569459931ecSChris Mason 	} else if (key->offset >= bytenr && csum_end > end_byte &&
570459931ecSChris Mason 		   end_byte > key->offset) {
571459931ecSChris Mason 		/*
572459931ecSChris Mason 		 *         [ bytenr - len ]
573459931ecSChris Mason 		 *                 [ ]
574459931ecSChris Mason 		 *                 [csum     ]
575459931ecSChris Mason 		 * we need to truncate from the beginning of the csum
576459931ecSChris Mason 		 */
577459931ecSChris Mason 		u32 new_size = (csum_end - end_byte) >> blocksize_bits;
578459931ecSChris Mason 		new_size *= csum_size;
579459931ecSChris Mason 
580afe5fea7STsutomu Itoh 		btrfs_truncate_item(root, path, new_size, 0);
581459931ecSChris Mason 
582459931ecSChris Mason 		key->offset = end_byte;
583b7a0365eSDaniel Dressler 		btrfs_set_item_key_safe(root->fs_info, path, key);
584459931ecSChris Mason 	} else {
585459931ecSChris Mason 		BUG();
586459931ecSChris Mason 	}
587459931ecSChris Mason }
588459931ecSChris Mason 
589459931ecSChris Mason /*
590459931ecSChris Mason  * deletes the csum items from the csum tree for a given
591459931ecSChris Mason  * range of bytes.
592459931ecSChris Mason  */
593459931ecSChris Mason int btrfs_del_csums(struct btrfs_trans_handle *trans,
594459931ecSChris Mason 		    struct btrfs_root *root, u64 bytenr, u64 len)
595459931ecSChris Mason {
596459931ecSChris Mason 	struct btrfs_path *path;
597459931ecSChris Mason 	struct btrfs_key key;
598459931ecSChris Mason 	u64 end_byte = bytenr + len;
599459931ecSChris Mason 	u64 csum_end;
600459931ecSChris Mason 	struct extent_buffer *leaf;
601459931ecSChris Mason 	int ret;
6026c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
603459931ecSChris Mason 	int blocksize_bits = root->fs_info->sb->s_blocksize_bits;
604459931ecSChris Mason 
605459931ecSChris Mason 	root = root->fs_info->csum_root;
606459931ecSChris Mason 
607459931ecSChris Mason 	path = btrfs_alloc_path();
6082a29edc6Sliubo 	if (!path)
6092a29edc6Sliubo 		return -ENOMEM;
610459931ecSChris Mason 
611459931ecSChris Mason 	while (1) {
612459931ecSChris Mason 		key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
613459931ecSChris Mason 		key.offset = end_byte - 1;
614459931ecSChris Mason 		key.type = BTRFS_EXTENT_CSUM_KEY;
615459931ecSChris Mason 
616b9473439SChris Mason 		path->leave_spinning = 1;
617459931ecSChris Mason 		ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
618459931ecSChris Mason 		if (ret > 0) {
619459931ecSChris Mason 			if (path->slots[0] == 0)
62065a246c5STsutomu Itoh 				break;
621459931ecSChris Mason 			path->slots[0]--;
622ad0397a7SJosef Bacik 		} else if (ret < 0) {
62365a246c5STsutomu Itoh 			break;
624459931ecSChris Mason 		}
625ad0397a7SJosef Bacik 
626459931ecSChris Mason 		leaf = path->nodes[0];
627459931ecSChris Mason 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
628459931ecSChris Mason 
629459931ecSChris Mason 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
630459931ecSChris Mason 		    key.type != BTRFS_EXTENT_CSUM_KEY) {
631459931ecSChris Mason 			break;
632459931ecSChris Mason 		}
633459931ecSChris Mason 
634459931ecSChris Mason 		if (key.offset >= end_byte)
635459931ecSChris Mason 			break;
636459931ecSChris Mason 
637459931ecSChris Mason 		csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
638459931ecSChris Mason 		csum_end <<= blocksize_bits;
639459931ecSChris Mason 		csum_end += key.offset;
640459931ecSChris Mason 
641459931ecSChris Mason 		/* this csum ends before we start, we're done */
642459931ecSChris Mason 		if (csum_end <= bytenr)
643459931ecSChris Mason 			break;
644459931ecSChris Mason 
645459931ecSChris Mason 		/* delete the entire item, it is inside our range */
646459931ecSChris Mason 		if (key.offset >= bytenr && csum_end <= end_byte) {
647459931ecSChris Mason 			ret = btrfs_del_item(trans, root, path);
64865a246c5STsutomu Itoh 			if (ret)
64965a246c5STsutomu Itoh 				goto out;
650dcbdd4dcSChris Mason 			if (key.offset == bytenr)
651dcbdd4dcSChris Mason 				break;
652459931ecSChris Mason 		} else if (key.offset < bytenr && csum_end > end_byte) {
653459931ecSChris Mason 			unsigned long offset;
654459931ecSChris Mason 			unsigned long shift_len;
655459931ecSChris Mason 			unsigned long item_offset;
656459931ecSChris Mason 			/*
657459931ecSChris Mason 			 *        [ bytenr - len ]
658459931ecSChris Mason 			 *     [csum                ]
659459931ecSChris Mason 			 *
660459931ecSChris Mason 			 * Our bytes are in the middle of the csum,
661459931ecSChris Mason 			 * we need to split this item and insert a new one.
662459931ecSChris Mason 			 *
663459931ecSChris Mason 			 * But we can't drop the path because the
664459931ecSChris Mason 			 * csum could change, get removed, extended etc.
665459931ecSChris Mason 			 *
666459931ecSChris Mason 			 * The trick here is the max size of a csum item leaves
667459931ecSChris Mason 			 * enough room in the tree block for a single
668459931ecSChris Mason 			 * item header.  So, we split the item in place,
669459931ecSChris Mason 			 * adding a new header pointing to the existing
670459931ecSChris Mason 			 * bytes.  Then we loop around again and we have
671459931ecSChris Mason 			 * a nicely formed csum item that we can neatly
672459931ecSChris Mason 			 * truncate.
673459931ecSChris Mason 			 */
674459931ecSChris Mason 			offset = (bytenr - key.offset) >> blocksize_bits;
675459931ecSChris Mason 			offset *= csum_size;
676459931ecSChris Mason 
677459931ecSChris Mason 			shift_len = (len >> blocksize_bits) * csum_size;
678459931ecSChris Mason 
679459931ecSChris Mason 			item_offset = btrfs_item_ptr_offset(leaf,
680459931ecSChris Mason 							    path->slots[0]);
681459931ecSChris Mason 
682459931ecSChris Mason 			memset_extent_buffer(leaf, 0, item_offset + offset,
683459931ecSChris Mason 					     shift_len);
684459931ecSChris Mason 			key.offset = bytenr;
685459931ecSChris Mason 
686459931ecSChris Mason 			/*
687459931ecSChris Mason 			 * btrfs_split_item returns -EAGAIN when the
688459931ecSChris Mason 			 * item changed size or key
689459931ecSChris Mason 			 */
690459931ecSChris Mason 			ret = btrfs_split_item(trans, root, path, &key, offset);
69179787eaaSJeff Mahoney 			if (ret && ret != -EAGAIN) {
69279787eaaSJeff Mahoney 				btrfs_abort_transaction(trans, root, ret);
69379787eaaSJeff Mahoney 				goto out;
69479787eaaSJeff Mahoney 			}
695459931ecSChris Mason 
696459931ecSChris Mason 			key.offset = end_byte - 1;
697459931ecSChris Mason 		} else {
698afe5fea7STsutomu Itoh 			truncate_one_csum(root, path, &key, bytenr, len);
699dcbdd4dcSChris Mason 			if (key.offset < bytenr)
700dcbdd4dcSChris Mason 				break;
701459931ecSChris Mason 		}
702b3b4aa74SDavid Sterba 		btrfs_release_path(path);
703459931ecSChris Mason 	}
70465a246c5STsutomu Itoh 	ret = 0;
705459931ecSChris Mason out:
706459931ecSChris Mason 	btrfs_free_path(path);
70765a246c5STsutomu Itoh 	return ret;
708459931ecSChris Mason }
709459931ecSChris Mason 
710065631f6SChris Mason int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
711d20f7043SChris Mason 			   struct btrfs_root *root,
712e6dcd2dcSChris Mason 			   struct btrfs_ordered_sum *sums)
713f254e52cSChris Mason {
714f254e52cSChris Mason 	struct btrfs_key file_key;
7156567e837SChris Mason 	struct btrfs_key found_key;
7165caf2a00SChris Mason 	struct btrfs_path *path;
717f254e52cSChris Mason 	struct btrfs_csum_item *item;
718065631f6SChris Mason 	struct btrfs_csum_item *item_end;
719ff79f819SChris Mason 	struct extent_buffer *leaf = NULL;
720f51a4a18SMiao Xie 	u64 next_offset;
721f51a4a18SMiao Xie 	u64 total_bytes = 0;
7226567e837SChris Mason 	u64 csum_offset;
723f51a4a18SMiao Xie 	u64 bytenr;
724f578d4bdSChris Mason 	u32 nritems;
725f578d4bdSChris Mason 	u32 ins_size;
726f51a4a18SMiao Xie 	int index = 0;
727f51a4a18SMiao Xie 	int found_next;
728f51a4a18SMiao Xie 	int ret;
7296c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7306e92f5e6SChris Mason 
7315caf2a00SChris Mason 	path = btrfs_alloc_path();
732d8926bb3SMark Fasheh 	if (!path)
733d8926bb3SMark Fasheh 		return -ENOMEM;
734065631f6SChris Mason again:
735065631f6SChris Mason 	next_offset = (u64)-1;
736065631f6SChris Mason 	found_next = 0;
737f51a4a18SMiao Xie 	bytenr = sums->bytenr + total_bytes;
738d20f7043SChris Mason 	file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
739f51a4a18SMiao Xie 	file_key.offset = bytenr;
740962a298fSDavid Sterba 	file_key.type = BTRFS_EXTENT_CSUM_KEY;
741a429e513SChris Mason 
742f51a4a18SMiao Xie 	item = btrfs_lookup_csum(trans, root, path, bytenr, 1);
743ff79f819SChris Mason 	if (!IS_ERR(item)) {
744639cb586SChris Mason 		ret = 0;
745f51a4a18SMiao Xie 		leaf = path->nodes[0];
746f51a4a18SMiao Xie 		item_end = btrfs_item_ptr(leaf, path->slots[0],
747f51a4a18SMiao Xie 					  struct btrfs_csum_item);
748f51a4a18SMiao Xie 		item_end = (struct btrfs_csum_item *)((char *)item_end +
749f51a4a18SMiao Xie 			   btrfs_item_size_nr(leaf, path->slots[0]));
750a429e513SChris Mason 		goto found;
751ff79f819SChris Mason 	}
752a429e513SChris Mason 	ret = PTR_ERR(item);
7534a500fd1SYan, Zheng 	if (ret != -EFBIG && ret != -ENOENT)
7544a500fd1SYan, Zheng 		goto fail_unlock;
7554a500fd1SYan, Zheng 
756a429e513SChris Mason 	if (ret == -EFBIG) {
757a429e513SChris Mason 		u32 item_size;
758a429e513SChris Mason 		/* we found one, but it isn't big enough yet */
7595f39d397SChris Mason 		leaf = path->nodes[0];
7605f39d397SChris Mason 		item_size = btrfs_item_size_nr(leaf, path->slots[0]);
761607d432dSJosef Bacik 		if ((item_size / csum_size) >=
762607d432dSJosef Bacik 		    MAX_CSUM_ITEMS(root, csum_size)) {
763a429e513SChris Mason 			/* already at max size, make a new one */
764a429e513SChris Mason 			goto insert;
765a429e513SChris Mason 		}
766a429e513SChris Mason 	} else {
767f578d4bdSChris Mason 		int slot = path->slots[0] + 1;
768a429e513SChris Mason 		/* we didn't find a csum item, insert one */
769f578d4bdSChris Mason 		nritems = btrfs_header_nritems(path->nodes[0]);
77035045bf2SFilipe Manana 		if (!nritems || (path->slots[0] >= nritems - 1)) {
771f578d4bdSChris Mason 			ret = btrfs_next_leaf(root, path);
772b56baf5bSYan 			if (ret == 1)
773f578d4bdSChris Mason 				found_next = 1;
774b56baf5bSYan 			if (ret != 0)
775f578d4bdSChris Mason 				goto insert;
77627b9a812SFilipe Manana 			slot = path->slots[0];
777f578d4bdSChris Mason 		}
778f578d4bdSChris Mason 		btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
779d20f7043SChris Mason 		if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
780d20f7043SChris Mason 		    found_key.type != BTRFS_EXTENT_CSUM_KEY) {
781f578d4bdSChris Mason 			found_next = 1;
782f578d4bdSChris Mason 			goto insert;
783f578d4bdSChris Mason 		}
784f578d4bdSChris Mason 		next_offset = found_key.offset;
785f578d4bdSChris Mason 		found_next = 1;
786a429e513SChris Mason 		goto insert;
787a429e513SChris Mason 	}
788a429e513SChris Mason 
789a429e513SChris Mason 	/*
790a429e513SChris Mason 	 * at this point, we know the tree has an item, but it isn't big
791a429e513SChris Mason 	 * enough yet to put our csum in.  Grow it
792a429e513SChris Mason 	 */
793b3b4aa74SDavid Sterba 	btrfs_release_path(path);
7946567e837SChris Mason 	ret = btrfs_search_slot(trans, root, &file_key, path,
795607d432dSJosef Bacik 				csum_size, 1);
7966567e837SChris Mason 	if (ret < 0)
79753863232SChris Mason 		goto fail_unlock;
798459931ecSChris Mason 
799459931ecSChris Mason 	if (ret > 0) {
800459931ecSChris Mason 		if (path->slots[0] == 0)
8016567e837SChris Mason 			goto insert;
8026567e837SChris Mason 		path->slots[0]--;
803459931ecSChris Mason 	}
804459931ecSChris Mason 
8055f39d397SChris Mason 	leaf = path->nodes[0];
8065f39d397SChris Mason 	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
807d20f7043SChris Mason 	csum_offset = (bytenr - found_key.offset) >>
8086567e837SChris Mason 			root->fs_info->sb->s_blocksize_bits;
809459931ecSChris Mason 
810962a298fSDavid Sterba 	if (found_key.type != BTRFS_EXTENT_CSUM_KEY ||
811d20f7043SChris Mason 	    found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
812607d432dSJosef Bacik 	    csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) {
8136567e837SChris Mason 		goto insert;
8146567e837SChris Mason 	}
815459931ecSChris Mason 
8162f697dc6SLiu Bo 	if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) /
817607d432dSJosef Bacik 	    csum_size) {
8182f697dc6SLiu Bo 		int extend_nr;
8192f697dc6SLiu Bo 		u64 tmp;
8202f697dc6SLiu Bo 		u32 diff;
8212f697dc6SLiu Bo 		u32 free_space;
822459931ecSChris Mason 
8232f697dc6SLiu Bo 		if (btrfs_leaf_free_space(root, leaf) <
8242f697dc6SLiu Bo 				 sizeof(struct btrfs_item) + csum_size * 2)
8252f697dc6SLiu Bo 			goto insert;
8262f697dc6SLiu Bo 
8272f697dc6SLiu Bo 		free_space = btrfs_leaf_free_space(root, leaf) -
8282f697dc6SLiu Bo 					 sizeof(struct btrfs_item) - csum_size;
829f51a4a18SMiao Xie 		tmp = sums->len - total_bytes;
8302f697dc6SLiu Bo 		tmp >>= root->fs_info->sb->s_blocksize_bits;
8312f697dc6SLiu Bo 		WARN_ON(tmp < 1);
8322f697dc6SLiu Bo 
8332f697dc6SLiu Bo 		extend_nr = max_t(int, 1, (int)tmp);
8342f697dc6SLiu Bo 		diff = (csum_offset + extend_nr) * csum_size;
8352f697dc6SLiu Bo 		diff = min(diff, MAX_CSUM_ITEMS(root, csum_size) * csum_size);
836459931ecSChris Mason 
8375f39d397SChris Mason 		diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
8382f697dc6SLiu Bo 		diff = min(free_space, diff);
8392f697dc6SLiu Bo 		diff /= csum_size;
8402f697dc6SLiu Bo 		diff *= csum_size;
841459931ecSChris Mason 
8424b90c680STsutomu Itoh 		btrfs_extend_item(root, path, diff);
843f51a4a18SMiao Xie 		ret = 0;
8446567e837SChris Mason 		goto csum;
8456567e837SChris Mason 	}
8466567e837SChris Mason 
8476567e837SChris Mason insert:
848b3b4aa74SDavid Sterba 	btrfs_release_path(path);
8496567e837SChris Mason 	csum_offset = 0;
850f578d4bdSChris Mason 	if (found_next) {
8512f697dc6SLiu Bo 		u64 tmp;
852d20f7043SChris Mason 
853f51a4a18SMiao Xie 		tmp = sums->len - total_bytes;
854f578d4bdSChris Mason 		tmp >>= root->fs_info->sb->s_blocksize_bits;
8552f697dc6SLiu Bo 		tmp = min(tmp, (next_offset - file_key.offset) >>
8562f697dc6SLiu Bo 					 root->fs_info->sb->s_blocksize_bits);
8572f697dc6SLiu Bo 
858f578d4bdSChris Mason 		tmp = max((u64)1, tmp);
859607d432dSJosef Bacik 		tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size));
860607d432dSJosef Bacik 		ins_size = csum_size * tmp;
861f578d4bdSChris Mason 	} else {
862607d432dSJosef Bacik 		ins_size = csum_size;
863f578d4bdSChris Mason 	}
864b9473439SChris Mason 	path->leave_spinning = 1;
8655caf2a00SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, &file_key,
866f578d4bdSChris Mason 				      ins_size);
867b9473439SChris Mason 	path->leave_spinning = 0;
86854aa1f4dSChris Mason 	if (ret < 0)
86953863232SChris Mason 		goto fail_unlock;
870fae7f21cSDulshani Gunawardhana 	if (WARN_ON(ret != 0))
87153863232SChris Mason 		goto fail_unlock;
8725f39d397SChris Mason 	leaf = path->nodes[0];
873f51a4a18SMiao Xie csum:
8745f39d397SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
875f51a4a18SMiao Xie 	item_end = (struct btrfs_csum_item *)((unsigned char *)item +
876f51a4a18SMiao Xie 				      btrfs_item_size_nr(leaf, path->slots[0]));
877509659cdSChris Mason 	item = (struct btrfs_csum_item *)((unsigned char *)item +
878607d432dSJosef Bacik 					  csum_offset * csum_size);
879b18c6685SChris Mason found:
880f51a4a18SMiao Xie 	ins_size = (u32)(sums->len - total_bytes) >>
881f51a4a18SMiao Xie 		   root->fs_info->sb->s_blocksize_bits;
882f51a4a18SMiao Xie 	ins_size *= csum_size;
883f51a4a18SMiao Xie 	ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item,
884f51a4a18SMiao Xie 			      ins_size);
885f51a4a18SMiao Xie 	write_extent_buffer(leaf, sums->sums + index, (unsigned long)item,
886f51a4a18SMiao Xie 			    ins_size);
887aadfeb6eSChris Mason 
888f51a4a18SMiao Xie 	ins_size /= csum_size;
889f51a4a18SMiao Xie 	total_bytes += ins_size * root->sectorsize;
890f51a4a18SMiao Xie 	index += ins_size;
891a6591715SChris Mason 
8925caf2a00SChris Mason 	btrfs_mark_buffer_dirty(path->nodes[0]);
893e6dcd2dcSChris Mason 	if (total_bytes < sums->len) {
894b3b4aa74SDavid Sterba 		btrfs_release_path(path);
895b9473439SChris Mason 		cond_resched();
896065631f6SChris Mason 		goto again;
897065631f6SChris Mason 	}
89853863232SChris Mason out:
8995caf2a00SChris Mason 	btrfs_free_path(path);
900f254e52cSChris Mason 	return ret;
90153863232SChris Mason 
90253863232SChris Mason fail_unlock:
90353863232SChris Mason 	goto out;
904f254e52cSChris Mason }
9057ffbb598SFilipe Manana 
9067ffbb598SFilipe Manana void btrfs_extent_item_to_extent_map(struct inode *inode,
9077ffbb598SFilipe Manana 				     const struct btrfs_path *path,
9087ffbb598SFilipe Manana 				     struct btrfs_file_extent_item *fi,
9097ffbb598SFilipe Manana 				     const bool new_inline,
9107ffbb598SFilipe Manana 				     struct extent_map *em)
9117ffbb598SFilipe Manana {
9127ffbb598SFilipe Manana 	struct btrfs_root *root = BTRFS_I(inode)->root;
9137ffbb598SFilipe Manana 	struct extent_buffer *leaf = path->nodes[0];
9147ffbb598SFilipe Manana 	const int slot = path->slots[0];
9157ffbb598SFilipe Manana 	struct btrfs_key key;
9167ffbb598SFilipe Manana 	u64 extent_start, extent_end;
9177ffbb598SFilipe Manana 	u64 bytenr;
9187ffbb598SFilipe Manana 	u8 type = btrfs_file_extent_type(leaf, fi);
9197ffbb598SFilipe Manana 	int compress_type = btrfs_file_extent_compression(leaf, fi);
9207ffbb598SFilipe Manana 
9217ffbb598SFilipe Manana 	em->bdev = root->fs_info->fs_devices->latest_bdev;
9227ffbb598SFilipe Manana 	btrfs_item_key_to_cpu(leaf, &key, slot);
9237ffbb598SFilipe Manana 	extent_start = key.offset;
9247ffbb598SFilipe Manana 
9257ffbb598SFilipe Manana 	if (type == BTRFS_FILE_EXTENT_REG ||
9267ffbb598SFilipe Manana 	    type == BTRFS_FILE_EXTENT_PREALLOC) {
9277ffbb598SFilipe Manana 		extent_end = extent_start +
9287ffbb598SFilipe Manana 			btrfs_file_extent_num_bytes(leaf, fi);
9297ffbb598SFilipe Manana 	} else if (type == BTRFS_FILE_EXTENT_INLINE) {
9307ffbb598SFilipe Manana 		size_t size;
9317ffbb598SFilipe Manana 		size = btrfs_file_extent_inline_len(leaf, slot, fi);
9327ffbb598SFilipe Manana 		extent_end = ALIGN(extent_start + size, root->sectorsize);
9337ffbb598SFilipe Manana 	}
9347ffbb598SFilipe Manana 
9357ffbb598SFilipe Manana 	em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
9367ffbb598SFilipe Manana 	if (type == BTRFS_FILE_EXTENT_REG ||
9377ffbb598SFilipe Manana 	    type == BTRFS_FILE_EXTENT_PREALLOC) {
9387ffbb598SFilipe Manana 		em->start = extent_start;
9397ffbb598SFilipe Manana 		em->len = extent_end - extent_start;
9407ffbb598SFilipe Manana 		em->orig_start = extent_start -
9417ffbb598SFilipe Manana 			btrfs_file_extent_offset(leaf, fi);
9427ffbb598SFilipe Manana 		em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
9437ffbb598SFilipe Manana 		bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
9447ffbb598SFilipe Manana 		if (bytenr == 0) {
9457ffbb598SFilipe Manana 			em->block_start = EXTENT_MAP_HOLE;
9467ffbb598SFilipe Manana 			return;
9477ffbb598SFilipe Manana 		}
9487ffbb598SFilipe Manana 		if (compress_type != BTRFS_COMPRESS_NONE) {
9497ffbb598SFilipe Manana 			set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
9507ffbb598SFilipe Manana 			em->compress_type = compress_type;
9517ffbb598SFilipe Manana 			em->block_start = bytenr;
9527ffbb598SFilipe Manana 			em->block_len = em->orig_block_len;
9537ffbb598SFilipe Manana 		} else {
9547ffbb598SFilipe Manana 			bytenr += btrfs_file_extent_offset(leaf, fi);
9557ffbb598SFilipe Manana 			em->block_start = bytenr;
9567ffbb598SFilipe Manana 			em->block_len = em->len;
9577ffbb598SFilipe Manana 			if (type == BTRFS_FILE_EXTENT_PREALLOC)
9587ffbb598SFilipe Manana 				set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9597ffbb598SFilipe Manana 		}
9607ffbb598SFilipe Manana 	} else if (type == BTRFS_FILE_EXTENT_INLINE) {
9617ffbb598SFilipe Manana 		em->block_start = EXTENT_MAP_INLINE;
9627ffbb598SFilipe Manana 		em->start = extent_start;
9637ffbb598SFilipe Manana 		em->len = extent_end - extent_start;
9647ffbb598SFilipe Manana 		/*
9657ffbb598SFilipe Manana 		 * Initialize orig_start and block_len with the same values
9667ffbb598SFilipe Manana 		 * as in inode.c:btrfs_get_extent().
9677ffbb598SFilipe Manana 		 */
9687ffbb598SFilipe Manana 		em->orig_start = EXTENT_MAP_HOLE;
9697ffbb598SFilipe Manana 		em->block_len = (u64)-1;
9707ffbb598SFilipe Manana 		if (!new_inline && compress_type != BTRFS_COMPRESS_NONE) {
9717ffbb598SFilipe Manana 			set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
9727ffbb598SFilipe Manana 			em->compress_type = compress_type;
9737ffbb598SFilipe Manana 		}
9747ffbb598SFilipe Manana 	} else {
9757ffbb598SFilipe Manana 		btrfs_err(root->fs_info,
9767ffbb598SFilipe Manana 			  "unknown file extent item type %d, inode %llu, offset %llu, root %llu",
9777ffbb598SFilipe Manana 			  type, btrfs_ino(inode), extent_start,
9787ffbb598SFilipe Manana 			  root->root_key.objectid);
9797ffbb598SFilipe Manana 	}
9807ffbb598SFilipe Manana }
981