xref: /openbmc/linux/fs/btrfs/file-item.c (revision 7ac687d9e047b3fa335f04e18c7188db6a170334)
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"
261de037a4SChris Mason #include "print-tree.h"
271e1d2701SChris Mason 
28607d432dSJosef Bacik #define MAX_CSUM_ITEMS(r, size) ((((BTRFS_LEAF_DATA_SIZE(r) - \
29a429e513SChris Mason 				   sizeof(struct btrfs_item) * 2) / \
30607d432dSJosef Bacik 				  size) - 1))
3107d400a6SYan Zheng 
3207d400a6SYan Zheng #define MAX_ORDERED_SUM_BYTES(r) ((PAGE_SIZE - \
3307d400a6SYan Zheng 				   sizeof(struct btrfs_ordered_sum)) / \
3407d400a6SYan Zheng 				   sizeof(struct btrfs_sector_sum) * \
3507d400a6SYan Zheng 				   (r)->sectorsize - (r)->sectorsize)
3607d400a6SYan Zheng 
37b18c6685SChris Mason int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
38dee26a9fSChris Mason 			     struct btrfs_root *root,
39b18c6685SChris Mason 			     u64 objectid, u64 pos,
40f2eb0a24SSage Weil 			     u64 disk_offset, u64 disk_num_bytes,
41c8b97818SChris Mason 			     u64 num_bytes, u64 offset, u64 ram_bytes,
42c8b97818SChris Mason 			     u8 compression, u8 encryption, u16 other_encoding)
439f5fae2fSChris Mason {
44dee26a9fSChris Mason 	int ret = 0;
45dee26a9fSChris Mason 	struct btrfs_file_extent_item *item;
46dee26a9fSChris Mason 	struct btrfs_key file_key;
475caf2a00SChris Mason 	struct btrfs_path *path;
485f39d397SChris Mason 	struct extent_buffer *leaf;
49dee26a9fSChris Mason 
505caf2a00SChris Mason 	path = btrfs_alloc_path();
51db5b493aSTsutomu Itoh 	if (!path)
52db5b493aSTsutomu Itoh 		return -ENOMEM;
53dee26a9fSChris Mason 	file_key.objectid = objectid;
54b18c6685SChris Mason 	file_key.offset = pos;
55dee26a9fSChris Mason 	btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
56dee26a9fSChris Mason 
57b9473439SChris Mason 	path->leave_spinning = 1;
585caf2a00SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, &file_key,
59dee26a9fSChris Mason 				      sizeof(*item));
6054aa1f4dSChris Mason 	if (ret < 0)
6154aa1f4dSChris Mason 		goto out;
629773a788SChris Mason 	BUG_ON(ret);
635f39d397SChris Mason 	leaf = path->nodes[0];
645f39d397SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0],
65dee26a9fSChris Mason 			      struct btrfs_file_extent_item);
66f2eb0a24SSage Weil 	btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset);
67db94535dSChris Mason 	btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
68f2eb0a24SSage Weil 	btrfs_set_file_extent_offset(leaf, item, offset);
69db94535dSChris Mason 	btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
70c8b97818SChris Mason 	btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes);
715f39d397SChris Mason 	btrfs_set_file_extent_generation(leaf, item, trans->transid);
725f39d397SChris Mason 	btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
73c8b97818SChris Mason 	btrfs_set_file_extent_compression(leaf, item, compression);
74c8b97818SChris Mason 	btrfs_set_file_extent_encryption(leaf, item, encryption);
75c8b97818SChris Mason 	btrfs_set_file_extent_other_encoding(leaf, item, other_encoding);
76c8b97818SChris Mason 
775f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
7854aa1f4dSChris Mason out:
795caf2a00SChris Mason 	btrfs_free_path(path);
8054aa1f4dSChris Mason 	return ret;
819f5fae2fSChris Mason }
82dee26a9fSChris Mason 
83b18c6685SChris Mason struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
84b18c6685SChris Mason 					  struct btrfs_root *root,
856567e837SChris Mason 					  struct btrfs_path *path,
86d20f7043SChris Mason 					  u64 bytenr, int cow)
876567e837SChris Mason {
886567e837SChris Mason 	int ret;
896567e837SChris Mason 	struct btrfs_key file_key;
906567e837SChris Mason 	struct btrfs_key found_key;
916567e837SChris Mason 	struct btrfs_csum_item *item;
925f39d397SChris Mason 	struct extent_buffer *leaf;
936567e837SChris Mason 	u64 csum_offset = 0;
946c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
95a429e513SChris Mason 	int csums_in_item;
966567e837SChris Mason 
97d20f7043SChris Mason 	file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
98d20f7043SChris Mason 	file_key.offset = bytenr;
99d20f7043SChris Mason 	btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
100b18c6685SChris Mason 	ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
1016567e837SChris Mason 	if (ret < 0)
1026567e837SChris Mason 		goto fail;
1035f39d397SChris Mason 	leaf = path->nodes[0];
1046567e837SChris Mason 	if (ret > 0) {
1056567e837SChris Mason 		ret = 1;
10670b2befdSChris Mason 		if (path->slots[0] == 0)
1076567e837SChris Mason 			goto fail;
1086567e837SChris Mason 		path->slots[0]--;
1095f39d397SChris Mason 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
110d20f7043SChris Mason 		if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY)
1116567e837SChris Mason 			goto fail;
112d20f7043SChris Mason 
113d20f7043SChris Mason 		csum_offset = (bytenr - found_key.offset) >>
1146567e837SChris Mason 				root->fs_info->sb->s_blocksize_bits;
1155f39d397SChris Mason 		csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
116607d432dSJosef Bacik 		csums_in_item /= csum_size;
117a429e513SChris Mason 
118a429e513SChris Mason 		if (csum_offset >= csums_in_item) {
119a429e513SChris Mason 			ret = -EFBIG;
1206567e837SChris Mason 			goto fail;
1216567e837SChris Mason 		}
1226567e837SChris Mason 	}
1236567e837SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
124509659cdSChris Mason 	item = (struct btrfs_csum_item *)((unsigned char *)item +
125607d432dSJosef Bacik 					  csum_offset * csum_size);
1266567e837SChris Mason 	return item;
1276567e837SChris Mason fail:
1286567e837SChris Mason 	if (ret > 0)
129b18c6685SChris Mason 		ret = -ENOENT;
1306567e837SChris Mason 	return ERR_PTR(ret);
1316567e837SChris Mason }
1326567e837SChris Mason 
1336567e837SChris Mason 
134dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
135dee26a9fSChris Mason 			     struct btrfs_root *root,
136dee26a9fSChris Mason 			     struct btrfs_path *path, u64 objectid,
1379773a788SChris Mason 			     u64 offset, int mod)
138dee26a9fSChris Mason {
139dee26a9fSChris Mason 	int ret;
140dee26a9fSChris Mason 	struct btrfs_key file_key;
141dee26a9fSChris Mason 	int ins_len = mod < 0 ? -1 : 0;
142dee26a9fSChris Mason 	int cow = mod != 0;
143dee26a9fSChris Mason 
144dee26a9fSChris Mason 	file_key.objectid = objectid;
14570b2befdSChris Mason 	file_key.offset = offset;
146dee26a9fSChris Mason 	btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
147dee26a9fSChris Mason 	ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
148dee26a9fSChris Mason 	return ret;
149dee26a9fSChris Mason }
150f254e52cSChris Mason 
15117d217feSYan Zheng 
1524b46fce2SJosef Bacik static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
1534b46fce2SJosef Bacik 				   struct inode *inode, struct bio *bio,
1544b46fce2SJosef Bacik 				   u64 logical_offset, u32 *dst, int dio)
15561b49440SChris Mason {
15661b49440SChris Mason 	u32 sum;
15761b49440SChris Mason 	struct bio_vec *bvec = bio->bi_io_vec;
15861b49440SChris Mason 	int bio_index = 0;
1594b46fce2SJosef Bacik 	u64 offset = 0;
16061b49440SChris Mason 	u64 item_start_offset = 0;
16161b49440SChris Mason 	u64 item_last_offset = 0;
162d20f7043SChris Mason 	u64 disk_bytenr;
16361b49440SChris Mason 	u32 diff;
1646c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
16561b49440SChris Mason 	int ret;
16661b49440SChris Mason 	struct btrfs_path *path;
16761b49440SChris Mason 	struct btrfs_csum_item *item = NULL;
16861b49440SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
16961b49440SChris Mason 
17061b49440SChris Mason 	path = btrfs_alloc_path();
171c2db1073STsutomu Itoh 	if (!path)
172c2db1073STsutomu Itoh 		return -ENOMEM;
1734d1b5fb4SChris Mason 	if (bio->bi_size > PAGE_CACHE_SIZE * 8)
1744d1b5fb4SChris Mason 		path->reada = 2;
17561b49440SChris Mason 
17661b49440SChris Mason 	WARN_ON(bio->bi_vcnt <= 0);
17761b49440SChris Mason 
1782cf8572dSChris Mason 	/*
1792cf8572dSChris Mason 	 * the free space stuff is only read when it hasn't been
1802cf8572dSChris Mason 	 * updated in the current transaction.  So, we can safely
1812cf8572dSChris Mason 	 * read from the commit root and sidestep a nasty deadlock
1822cf8572dSChris Mason 	 * between reading the free space cache and updating the csum tree.
1832cf8572dSChris Mason 	 */
184ddf23b3fSJosef Bacik 	if (btrfs_is_free_space_inode(root, inode)) {
1852cf8572dSChris Mason 		path->search_commit_root = 1;
186ddf23b3fSJosef Bacik 		path->skip_locking = 1;
187ddf23b3fSJosef Bacik 	}
1882cf8572dSChris Mason 
189d20f7043SChris Mason 	disk_bytenr = (u64)bio->bi_sector << 9;
1904b46fce2SJosef Bacik 	if (dio)
1914b46fce2SJosef Bacik 		offset = logical_offset;
19261b49440SChris Mason 	while (bio_index < bio->bi_vcnt) {
1934b46fce2SJosef Bacik 		if (!dio)
19461b49440SChris Mason 			offset = page_offset(bvec->bv_page) + bvec->bv_offset;
195d20f7043SChris Mason 		ret = btrfs_find_ordered_sum(inode, offset, disk_bytenr, &sum);
19661b49440SChris Mason 		if (ret == 0)
19761b49440SChris Mason 			goto found;
19861b49440SChris Mason 
199d20f7043SChris Mason 		if (!item || disk_bytenr < item_start_offset ||
200d20f7043SChris Mason 		    disk_bytenr >= item_last_offset) {
20161b49440SChris Mason 			struct btrfs_key found_key;
20261b49440SChris Mason 			u32 item_size;
20361b49440SChris Mason 
20461b49440SChris Mason 			if (item)
205b3b4aa74SDavid Sterba 				btrfs_release_path(path);
206d20f7043SChris Mason 			item = btrfs_lookup_csum(NULL, root->fs_info->csum_root,
207d20f7043SChris Mason 						 path, disk_bytenr, 0);
20861b49440SChris Mason 			if (IS_ERR(item)) {
20961b49440SChris Mason 				ret = PTR_ERR(item);
21061b49440SChris Mason 				if (ret == -ENOENT || ret == -EFBIG)
21161b49440SChris Mason 					ret = 0;
21261b49440SChris Mason 				sum = 0;
21317d217feSYan Zheng 				if (BTRFS_I(inode)->root->root_key.objectid ==
21417d217feSYan Zheng 				    BTRFS_DATA_RELOC_TREE_OBJECTID) {
21517d217feSYan Zheng 					set_extent_bits(io_tree, offset,
21617d217feSYan Zheng 						offset + bvec->bv_len - 1,
21717d217feSYan Zheng 						EXTENT_NODATASUM, GFP_NOFS);
21817d217feSYan Zheng 				} else {
219d397712bSChris Mason 					printk(KERN_INFO "btrfs no csum found "
22033345d01SLi Zefan 					       "for inode %llu start %llu\n",
22133345d01SLi Zefan 					       (unsigned long long)
22233345d01SLi Zefan 					       btrfs_ino(inode),
22361b49440SChris Mason 					       (unsigned long long)offset);
22417d217feSYan Zheng 				}
2256dab8157SChris Mason 				item = NULL;
226b3b4aa74SDavid Sterba 				btrfs_release_path(path);
22761b49440SChris Mason 				goto found;
22861b49440SChris Mason 			}
22961b49440SChris Mason 			btrfs_item_key_to_cpu(path->nodes[0], &found_key,
23061b49440SChris Mason 					      path->slots[0]);
23161b49440SChris Mason 
23261b49440SChris Mason 			item_start_offset = found_key.offset;
23361b49440SChris Mason 			item_size = btrfs_item_size_nr(path->nodes[0],
23461b49440SChris Mason 						       path->slots[0]);
23561b49440SChris Mason 			item_last_offset = item_start_offset +
236607d432dSJosef Bacik 				(item_size / csum_size) *
23761b49440SChris Mason 				root->sectorsize;
23861b49440SChris Mason 			item = btrfs_item_ptr(path->nodes[0], path->slots[0],
23961b49440SChris Mason 					      struct btrfs_csum_item);
24061b49440SChris Mason 		}
24161b49440SChris Mason 		/*
24261b49440SChris Mason 		 * this byte range must be able to fit inside
24361b49440SChris Mason 		 * a single leaf so it will also fit inside a u32
24461b49440SChris Mason 		 */
245d20f7043SChris Mason 		diff = disk_bytenr - item_start_offset;
24661b49440SChris Mason 		diff = diff / root->sectorsize;
247607d432dSJosef Bacik 		diff = diff * csum_size;
24861b49440SChris Mason 
24961b49440SChris Mason 		read_extent_buffer(path->nodes[0], &sum,
2503de9d6b6SChris Mason 				   ((unsigned long)item) + diff,
251607d432dSJosef Bacik 				   csum_size);
25261b49440SChris Mason found:
253d20f7043SChris Mason 		if (dst)
254d20f7043SChris Mason 			*dst++ = sum;
255d20f7043SChris Mason 		else
25661b49440SChris Mason 			set_state_private(io_tree, offset, sum);
257d20f7043SChris Mason 		disk_bytenr += bvec->bv_len;
2584b46fce2SJosef Bacik 		offset += bvec->bv_len;
25961b49440SChris Mason 		bio_index++;
26061b49440SChris Mason 		bvec++;
26161b49440SChris Mason 	}
26261b49440SChris Mason 	btrfs_free_path(path);
26361b49440SChris Mason 	return 0;
26461b49440SChris Mason }
26561b49440SChris Mason 
2664b46fce2SJosef Bacik int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
2674b46fce2SJosef Bacik 			  struct bio *bio, u32 *dst)
2684b46fce2SJosef Bacik {
2694b46fce2SJosef Bacik 	return __btrfs_lookup_bio_sums(root, inode, bio, 0, dst, 0);
2704b46fce2SJosef Bacik }
2714b46fce2SJosef Bacik 
2724b46fce2SJosef Bacik int btrfs_lookup_bio_sums_dio(struct btrfs_root *root, struct inode *inode,
2734b46fce2SJosef Bacik 			      struct bio *bio, u64 offset, u32 *dst)
2744b46fce2SJosef Bacik {
2754b46fce2SJosef Bacik 	return __btrfs_lookup_bio_sums(root, inode, bio, offset, dst, 1);
2764b46fce2SJosef Bacik }
2774b46fce2SJosef Bacik 
27817d217feSYan Zheng int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
279a2de733cSArne Jansen 			     struct list_head *list, int search_commit)
28017d217feSYan Zheng {
28117d217feSYan Zheng 	struct btrfs_key key;
28217d217feSYan Zheng 	struct btrfs_path *path;
28317d217feSYan Zheng 	struct extent_buffer *leaf;
28417d217feSYan Zheng 	struct btrfs_ordered_sum *sums;
28517d217feSYan Zheng 	struct btrfs_sector_sum *sector_sum;
28617d217feSYan Zheng 	struct btrfs_csum_item *item;
28717d217feSYan Zheng 	unsigned long offset;
28817d217feSYan Zheng 	int ret;
28917d217feSYan Zheng 	size_t size;
29017d217feSYan Zheng 	u64 csum_end;
2916c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
29217d217feSYan Zheng 
29317d217feSYan Zheng 	path = btrfs_alloc_path();
294d8926bb3SMark Fasheh 	if (!path)
295d8926bb3SMark Fasheh 		return -ENOMEM;
29617d217feSYan Zheng 
297a2de733cSArne Jansen 	if (search_commit) {
298a2de733cSArne Jansen 		path->skip_locking = 1;
299a2de733cSArne Jansen 		path->reada = 2;
300a2de733cSArne Jansen 		path->search_commit_root = 1;
301a2de733cSArne Jansen 	}
302a2de733cSArne Jansen 
30317d217feSYan Zheng 	key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
30417d217feSYan Zheng 	key.offset = start;
30517d217feSYan Zheng 	key.type = BTRFS_EXTENT_CSUM_KEY;
30617d217feSYan Zheng 
30707d400a6SYan Zheng 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
30817d217feSYan Zheng 	if (ret < 0)
30917d217feSYan Zheng 		goto fail;
31017d217feSYan Zheng 	if (ret > 0 && path->slots[0] > 0) {
31117d217feSYan Zheng 		leaf = path->nodes[0];
31217d217feSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
31317d217feSYan Zheng 		if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
31417d217feSYan Zheng 		    key.type == BTRFS_EXTENT_CSUM_KEY) {
31517d217feSYan Zheng 			offset = (start - key.offset) >>
31617d217feSYan Zheng 				 root->fs_info->sb->s_blocksize_bits;
31717d217feSYan Zheng 			if (offset * csum_size <
31817d217feSYan Zheng 			    btrfs_item_size_nr(leaf, path->slots[0] - 1))
31917d217feSYan Zheng 				path->slots[0]--;
32017d217feSYan Zheng 		}
32117d217feSYan Zheng 	}
32217d217feSYan Zheng 
32317d217feSYan Zheng 	while (start <= end) {
32417d217feSYan Zheng 		leaf = path->nodes[0];
32517d217feSYan Zheng 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
32607d400a6SYan Zheng 			ret = btrfs_next_leaf(root, path);
32717d217feSYan Zheng 			if (ret < 0)
32817d217feSYan Zheng 				goto fail;
32917d217feSYan Zheng 			if (ret > 0)
33017d217feSYan Zheng 				break;
33117d217feSYan Zheng 			leaf = path->nodes[0];
33217d217feSYan Zheng 		}
33317d217feSYan Zheng 
33417d217feSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
33517d217feSYan Zheng 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
33617d217feSYan Zheng 		    key.type != BTRFS_EXTENT_CSUM_KEY)
33717d217feSYan Zheng 			break;
33817d217feSYan Zheng 
33917d217feSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
34017d217feSYan Zheng 		if (key.offset > end)
34117d217feSYan Zheng 			break;
34217d217feSYan Zheng 
34317d217feSYan Zheng 		if (key.offset > start)
34417d217feSYan Zheng 			start = key.offset;
34517d217feSYan Zheng 
34617d217feSYan Zheng 		size = btrfs_item_size_nr(leaf, path->slots[0]);
34717d217feSYan Zheng 		csum_end = key.offset + (size / csum_size) * root->sectorsize;
34887b29b20SYan Zheng 		if (csum_end <= start) {
34987b29b20SYan Zheng 			path->slots[0]++;
35087b29b20SYan Zheng 			continue;
35187b29b20SYan Zheng 		}
35217d217feSYan Zheng 
35307d400a6SYan Zheng 		csum_end = min(csum_end, end + 1);
35407d400a6SYan Zheng 		item = btrfs_item_ptr(path->nodes[0], path->slots[0],
35507d400a6SYan Zheng 				      struct btrfs_csum_item);
35607d400a6SYan Zheng 		while (start < csum_end) {
35707d400a6SYan Zheng 			size = min_t(size_t, csum_end - start,
35807d400a6SYan Zheng 					MAX_ORDERED_SUM_BYTES(root));
35907d400a6SYan Zheng 			sums = kzalloc(btrfs_ordered_sum_size(root, size),
36007d400a6SYan Zheng 					GFP_NOFS);
36117d217feSYan Zheng 			BUG_ON(!sums);
36217d217feSYan Zheng 
36317d217feSYan Zheng 			sector_sum = sums->sums;
36417d217feSYan Zheng 			sums->bytenr = start;
36517d217feSYan Zheng 			sums->len = size;
36617d217feSYan Zheng 
36717d217feSYan Zheng 			offset = (start - key.offset) >>
36817d217feSYan Zheng 				root->fs_info->sb->s_blocksize_bits;
36917d217feSYan Zheng 			offset *= csum_size;
37017d217feSYan Zheng 
37117d217feSYan Zheng 			while (size > 0) {
37207d400a6SYan Zheng 				read_extent_buffer(path->nodes[0],
37307d400a6SYan Zheng 						&sector_sum->sum,
37407d400a6SYan Zheng 						((unsigned long)item) +
37507d400a6SYan Zheng 						offset, csum_size);
37617d217feSYan Zheng 				sector_sum->bytenr = start;
37717d217feSYan Zheng 
37817d217feSYan Zheng 				size -= root->sectorsize;
37917d217feSYan Zheng 				start += root->sectorsize;
38017d217feSYan Zheng 				offset += csum_size;
38117d217feSYan Zheng 				sector_sum++;
38217d217feSYan Zheng 			}
38317d217feSYan Zheng 			list_add_tail(&sums->list, list);
38407d400a6SYan Zheng 		}
38517d217feSYan Zheng 		path->slots[0]++;
38617d217feSYan Zheng 	}
38717d217feSYan Zheng 	ret = 0;
38817d217feSYan Zheng fail:
38917d217feSYan Zheng 	btrfs_free_path(path);
39017d217feSYan Zheng 	return ret;
39117d217feSYan Zheng }
39217d217feSYan Zheng 
3933edf7d33SChris Mason int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
394d20f7043SChris Mason 		       struct bio *bio, u64 file_start, int contig)
395e015640fSChris Mason {
396e6dcd2dcSChris Mason 	struct btrfs_ordered_sum *sums;
397e6dcd2dcSChris Mason 	struct btrfs_sector_sum *sector_sum;
3983edf7d33SChris Mason 	struct btrfs_ordered_extent *ordered;
399e015640fSChris Mason 	char *data;
400e015640fSChris Mason 	struct bio_vec *bvec = bio->bi_io_vec;
401e015640fSChris Mason 	int bio_index = 0;
4023edf7d33SChris Mason 	unsigned long total_bytes = 0;
4033edf7d33SChris Mason 	unsigned long this_sum_bytes = 0;
4043edf7d33SChris Mason 	u64 offset;
405d20f7043SChris Mason 	u64 disk_bytenr;
406e015640fSChris Mason 
407e6dcd2dcSChris Mason 	WARN_ON(bio->bi_vcnt <= 0);
408e6dcd2dcSChris Mason 	sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS);
409e015640fSChris Mason 	if (!sums)
410e015640fSChris Mason 		return -ENOMEM;
4113edf7d33SChris Mason 
412ed98b56aSChris Mason 	sector_sum = sums->sums;
413d20f7043SChris Mason 	disk_bytenr = (u64)bio->bi_sector << 9;
414e6dcd2dcSChris Mason 	sums->len = bio->bi_size;
415e6dcd2dcSChris Mason 	INIT_LIST_HEAD(&sums->list);
416d20f7043SChris Mason 
417d20f7043SChris Mason 	if (contig)
418d20f7043SChris Mason 		offset = file_start;
419d20f7043SChris Mason 	else
420d20f7043SChris Mason 		offset = page_offset(bvec->bv_page) + bvec->bv_offset;
421d20f7043SChris Mason 
422d20f7043SChris Mason 	ordered = btrfs_lookup_ordered_extent(inode, offset);
4233edf7d33SChris Mason 	BUG_ON(!ordered);
424d20f7043SChris Mason 	sums->bytenr = ordered->start;
425e015640fSChris Mason 
426e015640fSChris Mason 	while (bio_index < bio->bi_vcnt) {
427d20f7043SChris Mason 		if (!contig)
4283edf7d33SChris Mason 			offset = page_offset(bvec->bv_page) + bvec->bv_offset;
429d20f7043SChris Mason 
430d20f7043SChris Mason 		if (!contig && (offset >= ordered->file_offset + ordered->len ||
431d20f7043SChris Mason 		    offset < ordered->file_offset)) {
4323edf7d33SChris Mason 			unsigned long bytes_left;
4333edf7d33SChris Mason 			sums->len = this_sum_bytes;
4343edf7d33SChris Mason 			this_sum_bytes = 0;
4353edf7d33SChris Mason 			btrfs_add_ordered_sum(inode, ordered, sums);
4363edf7d33SChris Mason 			btrfs_put_ordered_extent(ordered);
4373edf7d33SChris Mason 
4383edf7d33SChris Mason 			bytes_left = bio->bi_size - total_bytes;
4393edf7d33SChris Mason 
4403edf7d33SChris Mason 			sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
4413edf7d33SChris Mason 				       GFP_NOFS);
4423edf7d33SChris Mason 			BUG_ON(!sums);
443ed98b56aSChris Mason 			sector_sum = sums->sums;
4443edf7d33SChris Mason 			sums->len = bytes_left;
445d20f7043SChris Mason 			ordered = btrfs_lookup_ordered_extent(inode, offset);
4463edf7d33SChris Mason 			BUG_ON(!ordered);
447d20f7043SChris Mason 			sums->bytenr = ordered->start;
4483edf7d33SChris Mason 		}
4493edf7d33SChris Mason 
450*7ac687d9SCong Wang 		data = kmap_atomic(bvec->bv_page);
451e6dcd2dcSChris Mason 		sector_sum->sum = ~(u32)0;
452e6dcd2dcSChris Mason 		sector_sum->sum = btrfs_csum_data(root,
453e6dcd2dcSChris Mason 						  data + bvec->bv_offset,
454e6dcd2dcSChris Mason 						  sector_sum->sum,
455e6dcd2dcSChris Mason 						  bvec->bv_len);
456*7ac687d9SCong Wang 		kunmap_atomic(data);
457e6dcd2dcSChris Mason 		btrfs_csum_final(sector_sum->sum,
458e6dcd2dcSChris Mason 				 (char *)&sector_sum->sum);
459d20f7043SChris Mason 		sector_sum->bytenr = disk_bytenr;
460ed98b56aSChris Mason 
461e6dcd2dcSChris Mason 		sector_sum++;
462e015640fSChris Mason 		bio_index++;
4633edf7d33SChris Mason 		total_bytes += bvec->bv_len;
4643edf7d33SChris Mason 		this_sum_bytes += bvec->bv_len;
465d20f7043SChris Mason 		disk_bytenr += bvec->bv_len;
466d20f7043SChris Mason 		offset += bvec->bv_len;
467e015640fSChris Mason 		bvec++;
468e015640fSChris Mason 	}
469ed98b56aSChris Mason 	this_sum_bytes = 0;
4703edf7d33SChris Mason 	btrfs_add_ordered_sum(inode, ordered, sums);
4713edf7d33SChris Mason 	btrfs_put_ordered_extent(ordered);
472e015640fSChris Mason 	return 0;
473e015640fSChris Mason }
474e015640fSChris Mason 
475459931ecSChris Mason /*
476459931ecSChris Mason  * helper function for csum removal, this expects the
477459931ecSChris Mason  * key to describe the csum pointed to by the path, and it expects
478459931ecSChris Mason  * the csum to overlap the range [bytenr, len]
479459931ecSChris Mason  *
480459931ecSChris Mason  * The csum should not be entirely contained in the range and the
481459931ecSChris Mason  * range should not be entirely contained in the csum.
482459931ecSChris Mason  *
483459931ecSChris Mason  * This calls btrfs_truncate_item with the correct args based on the
484459931ecSChris Mason  * overlap, and fixes up the key as required.
485459931ecSChris Mason  */
486459931ecSChris Mason static noinline int truncate_one_csum(struct btrfs_trans_handle *trans,
487459931ecSChris Mason 				      struct btrfs_root *root,
488459931ecSChris Mason 				      struct btrfs_path *path,
489459931ecSChris Mason 				      struct btrfs_key *key,
490459931ecSChris Mason 				      u64 bytenr, u64 len)
491459931ecSChris Mason {
492459931ecSChris Mason 	struct extent_buffer *leaf;
4936c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
494459931ecSChris Mason 	u64 csum_end;
495459931ecSChris Mason 	u64 end_byte = bytenr + len;
496459931ecSChris Mason 	u32 blocksize_bits = root->fs_info->sb->s_blocksize_bits;
497459931ecSChris Mason 	int ret;
498459931ecSChris Mason 
499459931ecSChris Mason 	leaf = path->nodes[0];
500459931ecSChris Mason 	csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
501459931ecSChris Mason 	csum_end <<= root->fs_info->sb->s_blocksize_bits;
502459931ecSChris Mason 	csum_end += key->offset;
503459931ecSChris Mason 
504459931ecSChris Mason 	if (key->offset < bytenr && csum_end <= end_byte) {
505459931ecSChris Mason 		/*
506459931ecSChris Mason 		 *         [ bytenr - len ]
507459931ecSChris Mason 		 *         [   ]
508459931ecSChris Mason 		 *   [csum     ]
509459931ecSChris Mason 		 *   A simple truncate off the end of the item
510459931ecSChris Mason 		 */
511459931ecSChris Mason 		u32 new_size = (bytenr - key->offset) >> blocksize_bits;
512459931ecSChris Mason 		new_size *= csum_size;
513459931ecSChris Mason 		ret = btrfs_truncate_item(trans, root, path, new_size, 1);
514459931ecSChris Mason 	} else if (key->offset >= bytenr && csum_end > end_byte &&
515459931ecSChris Mason 		   end_byte > key->offset) {
516459931ecSChris Mason 		/*
517459931ecSChris Mason 		 *         [ bytenr - len ]
518459931ecSChris Mason 		 *                 [ ]
519459931ecSChris Mason 		 *                 [csum     ]
520459931ecSChris Mason 		 * we need to truncate from the beginning of the csum
521459931ecSChris Mason 		 */
522459931ecSChris Mason 		u32 new_size = (csum_end - end_byte) >> blocksize_bits;
523459931ecSChris Mason 		new_size *= csum_size;
524459931ecSChris Mason 
525459931ecSChris Mason 		ret = btrfs_truncate_item(trans, root, path, new_size, 0);
526459931ecSChris Mason 
527459931ecSChris Mason 		key->offset = end_byte;
528459931ecSChris Mason 		ret = btrfs_set_item_key_safe(trans, root, path, key);
529459931ecSChris Mason 		BUG_ON(ret);
530459931ecSChris Mason 	} else {
531459931ecSChris Mason 		BUG();
532459931ecSChris Mason 	}
533459931ecSChris Mason 	return 0;
534459931ecSChris Mason }
535459931ecSChris Mason 
536459931ecSChris Mason /*
537459931ecSChris Mason  * deletes the csum items from the csum tree for a given
538459931ecSChris Mason  * range of bytes.
539459931ecSChris Mason  */
540459931ecSChris Mason int btrfs_del_csums(struct btrfs_trans_handle *trans,
541459931ecSChris Mason 		    struct btrfs_root *root, u64 bytenr, u64 len)
542459931ecSChris Mason {
543459931ecSChris Mason 	struct btrfs_path *path;
544459931ecSChris Mason 	struct btrfs_key key;
545459931ecSChris Mason 	u64 end_byte = bytenr + len;
546459931ecSChris Mason 	u64 csum_end;
547459931ecSChris Mason 	struct extent_buffer *leaf;
548459931ecSChris Mason 	int ret;
5496c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
550459931ecSChris Mason 	int blocksize_bits = root->fs_info->sb->s_blocksize_bits;
551459931ecSChris Mason 
552459931ecSChris Mason 	root = root->fs_info->csum_root;
553459931ecSChris Mason 
554459931ecSChris Mason 	path = btrfs_alloc_path();
5552a29edc6Sliubo 	if (!path)
5562a29edc6Sliubo 		return -ENOMEM;
557459931ecSChris Mason 
558459931ecSChris Mason 	while (1) {
559459931ecSChris Mason 		key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
560459931ecSChris Mason 		key.offset = end_byte - 1;
561459931ecSChris Mason 		key.type = BTRFS_EXTENT_CSUM_KEY;
562459931ecSChris Mason 
563b9473439SChris Mason 		path->leave_spinning = 1;
564459931ecSChris Mason 		ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
565459931ecSChris Mason 		if (ret > 0) {
566459931ecSChris Mason 			if (path->slots[0] == 0)
56765a246c5STsutomu Itoh 				break;
568459931ecSChris Mason 			path->slots[0]--;
569ad0397a7SJosef Bacik 		} else if (ret < 0) {
57065a246c5STsutomu Itoh 			break;
571459931ecSChris Mason 		}
572ad0397a7SJosef Bacik 
573459931ecSChris Mason 		leaf = path->nodes[0];
574459931ecSChris Mason 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
575459931ecSChris Mason 
576459931ecSChris Mason 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
577459931ecSChris Mason 		    key.type != BTRFS_EXTENT_CSUM_KEY) {
578459931ecSChris Mason 			break;
579459931ecSChris Mason 		}
580459931ecSChris Mason 
581459931ecSChris Mason 		if (key.offset >= end_byte)
582459931ecSChris Mason 			break;
583459931ecSChris Mason 
584459931ecSChris Mason 		csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
585459931ecSChris Mason 		csum_end <<= blocksize_bits;
586459931ecSChris Mason 		csum_end += key.offset;
587459931ecSChris Mason 
588459931ecSChris Mason 		/* this csum ends before we start, we're done */
589459931ecSChris Mason 		if (csum_end <= bytenr)
590459931ecSChris Mason 			break;
591459931ecSChris Mason 
592459931ecSChris Mason 		/* delete the entire item, it is inside our range */
593459931ecSChris Mason 		if (key.offset >= bytenr && csum_end <= end_byte) {
594459931ecSChris Mason 			ret = btrfs_del_item(trans, root, path);
59565a246c5STsutomu Itoh 			if (ret)
59665a246c5STsutomu Itoh 				goto out;
597dcbdd4dcSChris Mason 			if (key.offset == bytenr)
598dcbdd4dcSChris Mason 				break;
599459931ecSChris Mason 		} else if (key.offset < bytenr && csum_end > end_byte) {
600459931ecSChris Mason 			unsigned long offset;
601459931ecSChris Mason 			unsigned long shift_len;
602459931ecSChris Mason 			unsigned long item_offset;
603459931ecSChris Mason 			/*
604459931ecSChris Mason 			 *        [ bytenr - len ]
605459931ecSChris Mason 			 *     [csum                ]
606459931ecSChris Mason 			 *
607459931ecSChris Mason 			 * Our bytes are in the middle of the csum,
608459931ecSChris Mason 			 * we need to split this item and insert a new one.
609459931ecSChris Mason 			 *
610459931ecSChris Mason 			 * But we can't drop the path because the
611459931ecSChris Mason 			 * csum could change, get removed, extended etc.
612459931ecSChris Mason 			 *
613459931ecSChris Mason 			 * The trick here is the max size of a csum item leaves
614459931ecSChris Mason 			 * enough room in the tree block for a single
615459931ecSChris Mason 			 * item header.  So, we split the item in place,
616459931ecSChris Mason 			 * adding a new header pointing to the existing
617459931ecSChris Mason 			 * bytes.  Then we loop around again and we have
618459931ecSChris Mason 			 * a nicely formed csum item that we can neatly
619459931ecSChris Mason 			 * truncate.
620459931ecSChris Mason 			 */
621459931ecSChris Mason 			offset = (bytenr - key.offset) >> blocksize_bits;
622459931ecSChris Mason 			offset *= csum_size;
623459931ecSChris Mason 
624459931ecSChris Mason 			shift_len = (len >> blocksize_bits) * csum_size;
625459931ecSChris Mason 
626459931ecSChris Mason 			item_offset = btrfs_item_ptr_offset(leaf,
627459931ecSChris Mason 							    path->slots[0]);
628459931ecSChris Mason 
629459931ecSChris Mason 			memset_extent_buffer(leaf, 0, item_offset + offset,
630459931ecSChris Mason 					     shift_len);
631459931ecSChris Mason 			key.offset = bytenr;
632459931ecSChris Mason 
633459931ecSChris Mason 			/*
634459931ecSChris Mason 			 * btrfs_split_item returns -EAGAIN when the
635459931ecSChris Mason 			 * item changed size or key
636459931ecSChris Mason 			 */
637459931ecSChris Mason 			ret = btrfs_split_item(trans, root, path, &key, offset);
638459931ecSChris Mason 			BUG_ON(ret && ret != -EAGAIN);
639459931ecSChris Mason 
640459931ecSChris Mason 			key.offset = end_byte - 1;
641459931ecSChris Mason 		} else {
642459931ecSChris Mason 			ret = truncate_one_csum(trans, root, path,
643459931ecSChris Mason 						&key, bytenr, len);
644459931ecSChris Mason 			BUG_ON(ret);
645dcbdd4dcSChris Mason 			if (key.offset < bytenr)
646dcbdd4dcSChris Mason 				break;
647459931ecSChris Mason 		}
648b3b4aa74SDavid Sterba 		btrfs_release_path(path);
649459931ecSChris Mason 	}
65065a246c5STsutomu Itoh 	ret = 0;
651459931ecSChris Mason out:
652459931ecSChris Mason 	btrfs_free_path(path);
65365a246c5STsutomu Itoh 	return ret;
654459931ecSChris Mason }
655459931ecSChris Mason 
656065631f6SChris Mason int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
657d20f7043SChris Mason 			   struct btrfs_root *root,
658e6dcd2dcSChris Mason 			   struct btrfs_ordered_sum *sums)
659f254e52cSChris Mason {
660d20f7043SChris Mason 	u64 bytenr;
661f254e52cSChris Mason 	int ret;
662f254e52cSChris Mason 	struct btrfs_key file_key;
6636567e837SChris Mason 	struct btrfs_key found_key;
664065631f6SChris Mason 	u64 next_offset;
665e6dcd2dcSChris Mason 	u64 total_bytes = 0;
666065631f6SChris Mason 	int found_next;
6675caf2a00SChris Mason 	struct btrfs_path *path;
668f254e52cSChris Mason 	struct btrfs_csum_item *item;
669065631f6SChris Mason 	struct btrfs_csum_item *item_end;
670ff79f819SChris Mason 	struct extent_buffer *leaf = NULL;
6716567e837SChris Mason 	u64 csum_offset;
672e6dcd2dcSChris Mason 	struct btrfs_sector_sum *sector_sum;
673f578d4bdSChris Mason 	u32 nritems;
674f578d4bdSChris Mason 	u32 ins_size;
6756c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
6766e92f5e6SChris Mason 
6775caf2a00SChris Mason 	path = btrfs_alloc_path();
678d8926bb3SMark Fasheh 	if (!path)
679d8926bb3SMark Fasheh 		return -ENOMEM;
680d8926bb3SMark Fasheh 
681ed98b56aSChris Mason 	sector_sum = sums->sums;
682065631f6SChris Mason again:
683065631f6SChris Mason 	next_offset = (u64)-1;
684065631f6SChris Mason 	found_next = 0;
685d20f7043SChris Mason 	file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
686d20f7043SChris Mason 	file_key.offset = sector_sum->bytenr;
687d20f7043SChris Mason 	bytenr = sector_sum->bytenr;
688d20f7043SChris Mason 	btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
689a429e513SChris Mason 
690d20f7043SChris Mason 	item = btrfs_lookup_csum(trans, root, path, sector_sum->bytenr, 1);
691ff79f819SChris Mason 	if (!IS_ERR(item)) {
692ff79f819SChris Mason 		leaf = path->nodes[0];
693639cb586SChris Mason 		ret = 0;
694a429e513SChris Mason 		goto found;
695ff79f819SChris Mason 	}
696a429e513SChris Mason 	ret = PTR_ERR(item);
6974a500fd1SYan, Zheng 	if (ret != -EFBIG && ret != -ENOENT)
6984a500fd1SYan, Zheng 		goto fail_unlock;
6994a500fd1SYan, Zheng 
700a429e513SChris Mason 	if (ret == -EFBIG) {
701a429e513SChris Mason 		u32 item_size;
702a429e513SChris Mason 		/* we found one, but it isn't big enough yet */
7035f39d397SChris Mason 		leaf = path->nodes[0];
7045f39d397SChris Mason 		item_size = btrfs_item_size_nr(leaf, path->slots[0]);
705607d432dSJosef Bacik 		if ((item_size / csum_size) >=
706607d432dSJosef Bacik 		    MAX_CSUM_ITEMS(root, csum_size)) {
707a429e513SChris Mason 			/* already at max size, make a new one */
708a429e513SChris Mason 			goto insert;
709a429e513SChris Mason 		}
710a429e513SChris Mason 	} else {
711f578d4bdSChris Mason 		int slot = path->slots[0] + 1;
712a429e513SChris Mason 		/* we didn't find a csum item, insert one */
713f578d4bdSChris Mason 		nritems = btrfs_header_nritems(path->nodes[0]);
714f578d4bdSChris Mason 		if (path->slots[0] >= nritems - 1) {
715f578d4bdSChris Mason 			ret = btrfs_next_leaf(root, path);
716b56baf5bSYan 			if (ret == 1)
717f578d4bdSChris Mason 				found_next = 1;
718b56baf5bSYan 			if (ret != 0)
719f578d4bdSChris Mason 				goto insert;
720b56baf5bSYan 			slot = 0;
721f578d4bdSChris Mason 		}
722f578d4bdSChris Mason 		btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
723d20f7043SChris Mason 		if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
724d20f7043SChris Mason 		    found_key.type != BTRFS_EXTENT_CSUM_KEY) {
725f578d4bdSChris Mason 			found_next = 1;
726f578d4bdSChris Mason 			goto insert;
727f578d4bdSChris Mason 		}
728f578d4bdSChris Mason 		next_offset = found_key.offset;
729f578d4bdSChris Mason 		found_next = 1;
730a429e513SChris Mason 		goto insert;
731a429e513SChris Mason 	}
732a429e513SChris Mason 
733a429e513SChris Mason 	/*
734a429e513SChris Mason 	 * at this point, we know the tree has an item, but it isn't big
735a429e513SChris Mason 	 * enough yet to put our csum in.  Grow it
736a429e513SChris Mason 	 */
737b3b4aa74SDavid Sterba 	btrfs_release_path(path);
7386567e837SChris Mason 	ret = btrfs_search_slot(trans, root, &file_key, path,
739607d432dSJosef Bacik 				csum_size, 1);
7406567e837SChris Mason 	if (ret < 0)
74153863232SChris Mason 		goto fail_unlock;
742459931ecSChris Mason 
743459931ecSChris Mason 	if (ret > 0) {
744459931ecSChris Mason 		if (path->slots[0] == 0)
7456567e837SChris Mason 			goto insert;
7466567e837SChris Mason 		path->slots[0]--;
747459931ecSChris Mason 	}
748459931ecSChris Mason 
7495f39d397SChris Mason 	leaf = path->nodes[0];
7505f39d397SChris Mason 	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
751d20f7043SChris Mason 	csum_offset = (bytenr - found_key.offset) >>
7526567e837SChris Mason 			root->fs_info->sb->s_blocksize_bits;
753459931ecSChris Mason 
754d20f7043SChris Mason 	if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY ||
755d20f7043SChris Mason 	    found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
756607d432dSJosef Bacik 	    csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) {
7576567e837SChris Mason 		goto insert;
7586567e837SChris Mason 	}
759459931ecSChris Mason 
7605f39d397SChris Mason 	if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
761607d432dSJosef Bacik 	    csum_size) {
762607d432dSJosef Bacik 		u32 diff = (csum_offset + 1) * csum_size;
763459931ecSChris Mason 
764459931ecSChris Mason 		/*
765459931ecSChris Mason 		 * is the item big enough already?  we dropped our lock
766459931ecSChris Mason 		 * before and need to recheck
767459931ecSChris Mason 		 */
768459931ecSChris Mason 		if (diff < btrfs_item_size_nr(leaf, path->slots[0]))
769459931ecSChris Mason 			goto csum;
770459931ecSChris Mason 
7715f39d397SChris Mason 		diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
772d397712bSChris Mason 		if (diff != csum_size)
7733a686375SChris Mason 			goto insert;
774459931ecSChris Mason 
775a429e513SChris Mason 		ret = btrfs_extend_item(trans, root, path, diff);
7766567e837SChris Mason 		goto csum;
7776567e837SChris Mason 	}
7786567e837SChris Mason 
7796567e837SChris Mason insert:
780b3b4aa74SDavid Sterba 	btrfs_release_path(path);
7816567e837SChris Mason 	csum_offset = 0;
782f578d4bdSChris Mason 	if (found_next) {
783d20f7043SChris Mason 		u64 tmp = total_bytes + root->sectorsize;
784d20f7043SChris Mason 		u64 next_sector = sector_sum->bytenr;
785d20f7043SChris Mason 		struct btrfs_sector_sum *next = sector_sum + 1;
786d20f7043SChris Mason 
787d20f7043SChris Mason 		while (tmp < sums->len) {
788d20f7043SChris Mason 			if (next_sector + root->sectorsize != next->bytenr)
789d20f7043SChris Mason 				break;
790d20f7043SChris Mason 			tmp += root->sectorsize;
791d20f7043SChris Mason 			next_sector = next->bytenr;
792d20f7043SChris Mason 			next++;
793d20f7043SChris Mason 		}
794d20f7043SChris Mason 		tmp = min(tmp, next_offset - file_key.offset);
795f578d4bdSChris Mason 		tmp >>= root->fs_info->sb->s_blocksize_bits;
796f578d4bdSChris Mason 		tmp = max((u64)1, tmp);
797607d432dSJosef Bacik 		tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size));
798607d432dSJosef Bacik 		ins_size = csum_size * tmp;
799f578d4bdSChris Mason 	} else {
800607d432dSJosef Bacik 		ins_size = csum_size;
801f578d4bdSChris Mason 	}
802b9473439SChris Mason 	path->leave_spinning = 1;
8035caf2a00SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, &file_key,
804f578d4bdSChris Mason 				      ins_size);
805b9473439SChris Mason 	path->leave_spinning = 0;
80654aa1f4dSChris Mason 	if (ret < 0)
80753863232SChris Mason 		goto fail_unlock;
808a429e513SChris Mason 	if (ret != 0) {
809a429e513SChris Mason 		WARN_ON(1);
81053863232SChris Mason 		goto fail_unlock;
811a429e513SChris Mason 	}
8126567e837SChris Mason csum:
8135f39d397SChris Mason 	leaf = path->nodes[0];
8145f39d397SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
815f254e52cSChris Mason 	ret = 0;
816509659cdSChris Mason 	item = (struct btrfs_csum_item *)((unsigned char *)item +
817607d432dSJosef Bacik 					  csum_offset * csum_size);
818b18c6685SChris Mason found:
819065631f6SChris Mason 	item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
820065631f6SChris Mason 	item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
821065631f6SChris Mason 				      btrfs_item_size_nr(leaf, path->slots[0]));
822e6dcd2dcSChris Mason next_sector:
823aadfeb6eSChris Mason 
824a6591715SChris Mason 	write_extent_buffer(leaf, &sector_sum->sum, (unsigned long)item, csum_size);
8257f3c74fbSChris Mason 
826e6dcd2dcSChris Mason 	total_bytes += root->sectorsize;
827e6dcd2dcSChris Mason 	sector_sum++;
828e6dcd2dcSChris Mason 	if (total_bytes < sums->len) {
8296e92f5e6SChris Mason 		item = (struct btrfs_csum_item *)((char *)item +
830607d432dSJosef Bacik 						  csum_size);
831d20f7043SChris Mason 		if (item < item_end && bytenr + PAGE_CACHE_SIZE ==
832d20f7043SChris Mason 		    sector_sum->bytenr) {
833d20f7043SChris Mason 			bytenr = sector_sum->bytenr;
834e6dcd2dcSChris Mason 			goto next_sector;
835065631f6SChris Mason 		}
8362e1a992eSChris Mason 	}
837a6591715SChris Mason 
8385caf2a00SChris Mason 	btrfs_mark_buffer_dirty(path->nodes[0]);
839e6dcd2dcSChris Mason 	if (total_bytes < sums->len) {
840b3b4aa74SDavid Sterba 		btrfs_release_path(path);
841b9473439SChris Mason 		cond_resched();
842065631f6SChris Mason 		goto again;
843065631f6SChris Mason 	}
84453863232SChris Mason out:
8455caf2a00SChris Mason 	btrfs_free_path(path);
846f254e52cSChris Mason 	return ret;
84753863232SChris Mason 
84853863232SChris Mason fail_unlock:
84953863232SChris Mason 	goto out;
850f254e52cSChris Mason }
851