xref: /openbmc/linux/fs/btrfs/file-item.c (revision 4b90c68015a7c0863292d6306501552d4ffa33ff)
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 
28995e01b7SJan Schmidt #define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \
29a429e513SChris Mason 				   sizeof(struct btrfs_item) * 2) / \
30607d432dSJosef Bacik 				  size) - 1))
3107d400a6SYan Zheng 
32221b8318SZach Brown #define MAX_CSUM_ITEMS(r, size) (min_t(u32, __MAX_CSUM_ITEMS(r, size), \
33221b8318SZach Brown 				       PAGE_CACHE_SIZE))
347ca4be45SChris Mason 
3507d400a6SYan Zheng #define MAX_ORDERED_SUM_BYTES(r) ((PAGE_SIZE - \
3607d400a6SYan Zheng 				   sizeof(struct btrfs_ordered_sum)) / \
3707d400a6SYan Zheng 				   sizeof(struct btrfs_sector_sum) * \
3807d400a6SYan Zheng 				   (r)->sectorsize - (r)->sectorsize)
3907d400a6SYan Zheng 
40b18c6685SChris Mason int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
41dee26a9fSChris Mason 			     struct btrfs_root *root,
42b18c6685SChris Mason 			     u64 objectid, u64 pos,
43f2eb0a24SSage Weil 			     u64 disk_offset, u64 disk_num_bytes,
44c8b97818SChris Mason 			     u64 num_bytes, u64 offset, u64 ram_bytes,
45c8b97818SChris Mason 			     u8 compression, u8 encryption, u16 other_encoding)
469f5fae2fSChris Mason {
47dee26a9fSChris Mason 	int ret = 0;
48dee26a9fSChris Mason 	struct btrfs_file_extent_item *item;
49dee26a9fSChris Mason 	struct btrfs_key file_key;
505caf2a00SChris Mason 	struct btrfs_path *path;
515f39d397SChris Mason 	struct extent_buffer *leaf;
52dee26a9fSChris Mason 
535caf2a00SChris Mason 	path = btrfs_alloc_path();
54db5b493aSTsutomu Itoh 	if (!path)
55db5b493aSTsutomu Itoh 		return -ENOMEM;
56dee26a9fSChris Mason 	file_key.objectid = objectid;
57b18c6685SChris Mason 	file_key.offset = pos;
58dee26a9fSChris Mason 	btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
59dee26a9fSChris Mason 
60b9473439SChris Mason 	path->leave_spinning = 1;
615caf2a00SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, &file_key,
62dee26a9fSChris Mason 				      sizeof(*item));
6354aa1f4dSChris Mason 	if (ret < 0)
6454aa1f4dSChris Mason 		goto out;
6579787eaaSJeff Mahoney 	BUG_ON(ret); /* Can't happen */
665f39d397SChris Mason 	leaf = path->nodes[0];
675f39d397SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0],
68dee26a9fSChris Mason 			      struct btrfs_file_extent_item);
69f2eb0a24SSage Weil 	btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset);
70db94535dSChris Mason 	btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
71f2eb0a24SSage Weil 	btrfs_set_file_extent_offset(leaf, item, offset);
72db94535dSChris Mason 	btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
73c8b97818SChris Mason 	btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes);
745f39d397SChris Mason 	btrfs_set_file_extent_generation(leaf, item, trans->transid);
755f39d397SChris Mason 	btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
76c8b97818SChris Mason 	btrfs_set_file_extent_compression(leaf, item, compression);
77c8b97818SChris Mason 	btrfs_set_file_extent_encryption(leaf, item, encryption);
78c8b97818SChris Mason 	btrfs_set_file_extent_other_encoding(leaf, item, other_encoding);
79c8b97818SChris Mason 
805f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
8154aa1f4dSChris Mason out:
825caf2a00SChris Mason 	btrfs_free_path(path);
8354aa1f4dSChris Mason 	return ret;
849f5fae2fSChris Mason }
85dee26a9fSChris Mason 
86b18c6685SChris Mason struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
87b18c6685SChris Mason 					  struct btrfs_root *root,
886567e837SChris Mason 					  struct btrfs_path *path,
89d20f7043SChris Mason 					  u64 bytenr, int cow)
906567e837SChris Mason {
916567e837SChris Mason 	int ret;
926567e837SChris Mason 	struct btrfs_key file_key;
936567e837SChris Mason 	struct btrfs_key found_key;
946567e837SChris Mason 	struct btrfs_csum_item *item;
955f39d397SChris Mason 	struct extent_buffer *leaf;
966567e837SChris Mason 	u64 csum_offset = 0;
976c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
98a429e513SChris Mason 	int csums_in_item;
996567e837SChris Mason 
100d20f7043SChris Mason 	file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
101d20f7043SChris Mason 	file_key.offset = bytenr;
102d20f7043SChris Mason 	btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
103b18c6685SChris Mason 	ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
1046567e837SChris Mason 	if (ret < 0)
1056567e837SChris Mason 		goto fail;
1065f39d397SChris Mason 	leaf = path->nodes[0];
1076567e837SChris Mason 	if (ret > 0) {
1086567e837SChris Mason 		ret = 1;
10970b2befdSChris Mason 		if (path->slots[0] == 0)
1106567e837SChris Mason 			goto fail;
1116567e837SChris Mason 		path->slots[0]--;
1125f39d397SChris Mason 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
113d20f7043SChris Mason 		if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY)
1146567e837SChris Mason 			goto fail;
115d20f7043SChris Mason 
116d20f7043SChris Mason 		csum_offset = (bytenr - found_key.offset) >>
1176567e837SChris Mason 				root->fs_info->sb->s_blocksize_bits;
1185f39d397SChris Mason 		csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
119607d432dSJosef Bacik 		csums_in_item /= csum_size;
120a429e513SChris Mason 
12182d130ffSMiao Xie 		if (csum_offset == csums_in_item) {
122a429e513SChris Mason 			ret = -EFBIG;
1236567e837SChris Mason 			goto fail;
12482d130ffSMiao Xie 		} else if (csum_offset > csums_in_item) {
12582d130ffSMiao Xie 			goto fail;
1266567e837SChris Mason 		}
1276567e837SChris Mason 	}
1286567e837SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
129509659cdSChris Mason 	item = (struct btrfs_csum_item *)((unsigned char *)item +
130607d432dSJosef Bacik 					  csum_offset * csum_size);
1316567e837SChris Mason 	return item;
1326567e837SChris Mason fail:
1336567e837SChris Mason 	if (ret > 0)
134b18c6685SChris Mason 		ret = -ENOENT;
1356567e837SChris Mason 	return ERR_PTR(ret);
1366567e837SChris Mason }
1376567e837SChris Mason 
138dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
139dee26a9fSChris Mason 			     struct btrfs_root *root,
140dee26a9fSChris Mason 			     struct btrfs_path *path, u64 objectid,
1419773a788SChris Mason 			     u64 offset, int mod)
142dee26a9fSChris Mason {
143dee26a9fSChris Mason 	int ret;
144dee26a9fSChris Mason 	struct btrfs_key file_key;
145dee26a9fSChris Mason 	int ins_len = mod < 0 ? -1 : 0;
146dee26a9fSChris Mason 	int cow = mod != 0;
147dee26a9fSChris Mason 
148dee26a9fSChris Mason 	file_key.objectid = objectid;
14970b2befdSChris Mason 	file_key.offset = offset;
150dee26a9fSChris Mason 	btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
151dee26a9fSChris Mason 	ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
152dee26a9fSChris Mason 	return ret;
153dee26a9fSChris Mason }
154f254e52cSChris Mason 
155315a9850SMiao Xie u64 btrfs_file_extent_length(struct btrfs_path *path)
156315a9850SMiao Xie {
157315a9850SMiao Xie 	int extent_type;
158315a9850SMiao Xie 	struct btrfs_file_extent_item *fi;
159315a9850SMiao Xie 	u64 len;
160315a9850SMiao Xie 
161315a9850SMiao Xie 	fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
162315a9850SMiao Xie 			    struct btrfs_file_extent_item);
163315a9850SMiao Xie 	extent_type = btrfs_file_extent_type(path->nodes[0], fi);
164315a9850SMiao Xie 
165315a9850SMiao Xie 	if (extent_type == BTRFS_FILE_EXTENT_REG ||
166315a9850SMiao Xie 	    extent_type == BTRFS_FILE_EXTENT_PREALLOC)
167315a9850SMiao Xie 		len = btrfs_file_extent_num_bytes(path->nodes[0], fi);
168315a9850SMiao Xie 	else if (extent_type == BTRFS_FILE_EXTENT_INLINE)
169315a9850SMiao Xie 		len = btrfs_file_extent_inline_len(path->nodes[0], fi);
170315a9850SMiao Xie 	else
171315a9850SMiao Xie 		BUG();
172315a9850SMiao Xie 
173315a9850SMiao Xie 	return len;
174315a9850SMiao Xie }
17517d217feSYan Zheng 
1764b46fce2SJosef Bacik static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
1774b46fce2SJosef Bacik 				   struct inode *inode, struct bio *bio,
1784b46fce2SJosef Bacik 				   u64 logical_offset, u32 *dst, int dio)
17961b49440SChris Mason {
180e4100d98SMiao Xie 	u32 sum[16];
181e4100d98SMiao Xie 	int len;
18261b49440SChris Mason 	struct bio_vec *bvec = bio->bi_io_vec;
18361b49440SChris Mason 	int bio_index = 0;
1844b46fce2SJosef Bacik 	u64 offset = 0;
18561b49440SChris Mason 	u64 item_start_offset = 0;
18661b49440SChris Mason 	u64 item_last_offset = 0;
187d20f7043SChris Mason 	u64 disk_bytenr;
18861b49440SChris Mason 	u32 diff;
1896c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
190e4100d98SMiao Xie 	int count;
19161b49440SChris Mason 	struct btrfs_path *path;
19261b49440SChris Mason 	struct btrfs_csum_item *item = NULL;
19361b49440SChris Mason 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
19461b49440SChris Mason 
19561b49440SChris Mason 	path = btrfs_alloc_path();
196c2db1073STsutomu Itoh 	if (!path)
197c2db1073STsutomu Itoh 		return -ENOMEM;
1984d1b5fb4SChris Mason 	if (bio->bi_size > PAGE_CACHE_SIZE * 8)
1994d1b5fb4SChris Mason 		path->reada = 2;
20061b49440SChris Mason 
20161b49440SChris Mason 	WARN_ON(bio->bi_vcnt <= 0);
20261b49440SChris Mason 
2032cf8572dSChris Mason 	/*
2042cf8572dSChris Mason 	 * the free space stuff is only read when it hasn't been
2052cf8572dSChris Mason 	 * updated in the current transaction.  So, we can safely
2062cf8572dSChris Mason 	 * read from the commit root and sidestep a nasty deadlock
2072cf8572dSChris Mason 	 * between reading the free space cache and updating the csum tree.
2082cf8572dSChris Mason 	 */
20983eea1f1SLiu Bo 	if (btrfs_is_free_space_inode(inode)) {
2102cf8572dSChris Mason 		path->search_commit_root = 1;
211ddf23b3fSJosef Bacik 		path->skip_locking = 1;
212ddf23b3fSJosef Bacik 	}
2132cf8572dSChris Mason 
214d20f7043SChris Mason 	disk_bytenr = (u64)bio->bi_sector << 9;
2154b46fce2SJosef Bacik 	if (dio)
2164b46fce2SJosef Bacik 		offset = logical_offset;
21761b49440SChris Mason 	while (bio_index < bio->bi_vcnt) {
218e4100d98SMiao Xie 		len = min_t(int, ARRAY_SIZE(sum), bio->bi_vcnt - bio_index);
2194b46fce2SJosef Bacik 		if (!dio)
22061b49440SChris Mason 			offset = page_offset(bvec->bv_page) + bvec->bv_offset;
221e4100d98SMiao Xie 		count = btrfs_find_ordered_sum(inode, offset, disk_bytenr, sum,
222e4100d98SMiao Xie 					       len);
223e4100d98SMiao Xie 		if (count)
22461b49440SChris Mason 			goto found;
22561b49440SChris Mason 
226d20f7043SChris Mason 		if (!item || disk_bytenr < item_start_offset ||
227d20f7043SChris Mason 		    disk_bytenr >= item_last_offset) {
22861b49440SChris Mason 			struct btrfs_key found_key;
22961b49440SChris Mason 			u32 item_size;
23061b49440SChris Mason 
23161b49440SChris Mason 			if (item)
232b3b4aa74SDavid Sterba 				btrfs_release_path(path);
233d20f7043SChris Mason 			item = btrfs_lookup_csum(NULL, root->fs_info->csum_root,
234d20f7043SChris Mason 						 path, disk_bytenr, 0);
23561b49440SChris Mason 			if (IS_ERR(item)) {
236e4100d98SMiao Xie 				count = 1;
237e4100d98SMiao Xie 				sum[0] = 0;
23817d217feSYan Zheng 				if (BTRFS_I(inode)->root->root_key.objectid ==
23917d217feSYan Zheng 				    BTRFS_DATA_RELOC_TREE_OBJECTID) {
24017d217feSYan Zheng 					set_extent_bits(io_tree, offset,
24117d217feSYan Zheng 						offset + bvec->bv_len - 1,
24217d217feSYan Zheng 						EXTENT_NODATASUM, GFP_NOFS);
24317d217feSYan Zheng 				} else {
244d397712bSChris Mason 					printk(KERN_INFO "btrfs no csum found "
24533345d01SLi Zefan 					       "for inode %llu start %llu\n",
24633345d01SLi Zefan 					       (unsigned long long)
24733345d01SLi Zefan 					       btrfs_ino(inode),
24861b49440SChris Mason 					       (unsigned long long)offset);
24917d217feSYan Zheng 				}
2506dab8157SChris Mason 				item = NULL;
251b3b4aa74SDavid Sterba 				btrfs_release_path(path);
25261b49440SChris Mason 				goto found;
25361b49440SChris Mason 			}
25461b49440SChris Mason 			btrfs_item_key_to_cpu(path->nodes[0], &found_key,
25561b49440SChris Mason 					      path->slots[0]);
25661b49440SChris Mason 
25761b49440SChris Mason 			item_start_offset = found_key.offset;
25861b49440SChris Mason 			item_size = btrfs_item_size_nr(path->nodes[0],
25961b49440SChris Mason 						       path->slots[0]);
26061b49440SChris Mason 			item_last_offset = item_start_offset +
261607d432dSJosef Bacik 				(item_size / csum_size) *
26261b49440SChris Mason 				root->sectorsize;
26361b49440SChris Mason 			item = btrfs_item_ptr(path->nodes[0], path->slots[0],
26461b49440SChris Mason 					      struct btrfs_csum_item);
26561b49440SChris Mason 		}
26661b49440SChris Mason 		/*
26761b49440SChris Mason 		 * this byte range must be able to fit inside
26861b49440SChris Mason 		 * a single leaf so it will also fit inside a u32
26961b49440SChris Mason 		 */
270d20f7043SChris Mason 		diff = disk_bytenr - item_start_offset;
27161b49440SChris Mason 		diff = diff / root->sectorsize;
272607d432dSJosef Bacik 		diff = diff * csum_size;
273e4100d98SMiao Xie 		count = min_t(int, len, (item_last_offset - disk_bytenr) >>
274e4100d98SMiao Xie 					inode->i_sb->s_blocksize_bits);
275e4100d98SMiao Xie 		read_extent_buffer(path->nodes[0], sum,
2763de9d6b6SChris Mason 				   ((unsigned long)item) + diff,
277e4100d98SMiao Xie 				   csum_size * count);
27861b49440SChris Mason found:
279e4100d98SMiao Xie 		if (dst) {
280e4100d98SMiao Xie 			memcpy(dst, sum, count * csum_size);
281e4100d98SMiao Xie 			dst += count;
282e4100d98SMiao Xie 		} else {
283e4100d98SMiao Xie 			if (dio)
284e4100d98SMiao Xie 				extent_cache_csums_dio(io_tree, offset, sum,
285e4100d98SMiao Xie 						       count);
286d20f7043SChris Mason 			else
287e4100d98SMiao Xie 				extent_cache_csums(io_tree, bio, bio_index, sum,
288e4100d98SMiao Xie 					    count);
289e4100d98SMiao Xie 		}
290e4100d98SMiao Xie 		while (count--) {
291d20f7043SChris Mason 			disk_bytenr += bvec->bv_len;
2924b46fce2SJosef Bacik 			offset += bvec->bv_len;
29361b49440SChris Mason 			bio_index++;
29461b49440SChris Mason 			bvec++;
29561b49440SChris Mason 		}
296e4100d98SMiao Xie 	}
29761b49440SChris Mason 	btrfs_free_path(path);
29861b49440SChris Mason 	return 0;
29961b49440SChris Mason }
30061b49440SChris Mason 
3014b46fce2SJosef Bacik int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
3024b46fce2SJosef Bacik 			  struct bio *bio, u32 *dst)
3034b46fce2SJosef Bacik {
3044b46fce2SJosef Bacik 	return __btrfs_lookup_bio_sums(root, inode, bio, 0, dst, 0);
3054b46fce2SJosef Bacik }
3064b46fce2SJosef Bacik 
3074b46fce2SJosef Bacik int btrfs_lookup_bio_sums_dio(struct btrfs_root *root, struct inode *inode,
308c329861dSJosef Bacik 			      struct bio *bio, u64 offset)
3094b46fce2SJosef Bacik {
310c329861dSJosef Bacik 	return __btrfs_lookup_bio_sums(root, inode, bio, offset, NULL, 1);
3114b46fce2SJosef Bacik }
3124b46fce2SJosef Bacik 
31317d217feSYan Zheng int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
314a2de733cSArne Jansen 			     struct list_head *list, int search_commit)
31517d217feSYan Zheng {
31617d217feSYan Zheng 	struct btrfs_key key;
31717d217feSYan Zheng 	struct btrfs_path *path;
31817d217feSYan Zheng 	struct extent_buffer *leaf;
31917d217feSYan Zheng 	struct btrfs_ordered_sum *sums;
32017d217feSYan Zheng 	struct btrfs_sector_sum *sector_sum;
32117d217feSYan Zheng 	struct btrfs_csum_item *item;
3220678b618SMark Fasheh 	LIST_HEAD(tmplist);
32317d217feSYan Zheng 	unsigned long offset;
32417d217feSYan Zheng 	int ret;
32517d217feSYan Zheng 	size_t size;
32617d217feSYan Zheng 	u64 csum_end;
3276c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
32817d217feSYan Zheng 
32917d217feSYan Zheng 	path = btrfs_alloc_path();
330d8926bb3SMark Fasheh 	if (!path)
331d8926bb3SMark Fasheh 		return -ENOMEM;
33217d217feSYan Zheng 
333a2de733cSArne Jansen 	if (search_commit) {
334a2de733cSArne Jansen 		path->skip_locking = 1;
335a2de733cSArne Jansen 		path->reada = 2;
336a2de733cSArne Jansen 		path->search_commit_root = 1;
337a2de733cSArne Jansen 	}
338a2de733cSArne Jansen 
33917d217feSYan Zheng 	key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
34017d217feSYan Zheng 	key.offset = start;
34117d217feSYan Zheng 	key.type = BTRFS_EXTENT_CSUM_KEY;
34217d217feSYan Zheng 
34307d400a6SYan Zheng 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
34417d217feSYan Zheng 	if (ret < 0)
34517d217feSYan Zheng 		goto fail;
34617d217feSYan Zheng 	if (ret > 0 && path->slots[0] > 0) {
34717d217feSYan Zheng 		leaf = path->nodes[0];
34817d217feSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
34917d217feSYan Zheng 		if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
35017d217feSYan Zheng 		    key.type == BTRFS_EXTENT_CSUM_KEY) {
35117d217feSYan Zheng 			offset = (start - key.offset) >>
35217d217feSYan Zheng 				 root->fs_info->sb->s_blocksize_bits;
35317d217feSYan Zheng 			if (offset * csum_size <
35417d217feSYan Zheng 			    btrfs_item_size_nr(leaf, path->slots[0] - 1))
35517d217feSYan Zheng 				path->slots[0]--;
35617d217feSYan Zheng 		}
35717d217feSYan Zheng 	}
35817d217feSYan Zheng 
35917d217feSYan Zheng 	while (start <= end) {
36017d217feSYan Zheng 		leaf = path->nodes[0];
36117d217feSYan Zheng 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
36207d400a6SYan Zheng 			ret = btrfs_next_leaf(root, path);
36317d217feSYan Zheng 			if (ret < 0)
36417d217feSYan Zheng 				goto fail;
36517d217feSYan Zheng 			if (ret > 0)
36617d217feSYan Zheng 				break;
36717d217feSYan Zheng 			leaf = path->nodes[0];
36817d217feSYan Zheng 		}
36917d217feSYan Zheng 
37017d217feSYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
37117d217feSYan Zheng 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
372628c8282SZhi Yong Wu 		    key.type != BTRFS_EXTENT_CSUM_KEY ||
373628c8282SZhi Yong Wu 		    key.offset > end)
37417d217feSYan Zheng 			break;
37517d217feSYan Zheng 
37617d217feSYan Zheng 		if (key.offset > start)
37717d217feSYan Zheng 			start = key.offset;
37817d217feSYan Zheng 
37917d217feSYan Zheng 		size = btrfs_item_size_nr(leaf, path->slots[0]);
38017d217feSYan Zheng 		csum_end = key.offset + (size / csum_size) * root->sectorsize;
38187b29b20SYan Zheng 		if (csum_end <= start) {
38287b29b20SYan Zheng 			path->slots[0]++;
38387b29b20SYan Zheng 			continue;
38487b29b20SYan Zheng 		}
38517d217feSYan Zheng 
38607d400a6SYan Zheng 		csum_end = min(csum_end, end + 1);
38707d400a6SYan Zheng 		item = btrfs_item_ptr(path->nodes[0], path->slots[0],
38807d400a6SYan Zheng 				      struct btrfs_csum_item);
38907d400a6SYan Zheng 		while (start < csum_end) {
39007d400a6SYan Zheng 			size = min_t(size_t, csum_end - start,
39107d400a6SYan Zheng 					MAX_ORDERED_SUM_BYTES(root));
39207d400a6SYan Zheng 			sums = kzalloc(btrfs_ordered_sum_size(root, size),
39307d400a6SYan Zheng 					GFP_NOFS);
3940678b618SMark Fasheh 			if (!sums) {
3950678b618SMark Fasheh 				ret = -ENOMEM;
3960678b618SMark Fasheh 				goto fail;
3970678b618SMark Fasheh 			}
39817d217feSYan Zheng 
39917d217feSYan Zheng 			sector_sum = sums->sums;
40017d217feSYan Zheng 			sums->bytenr = start;
40117d217feSYan Zheng 			sums->len = size;
40217d217feSYan Zheng 
40317d217feSYan Zheng 			offset = (start - key.offset) >>
40417d217feSYan Zheng 				root->fs_info->sb->s_blocksize_bits;
40517d217feSYan Zheng 			offset *= csum_size;
40617d217feSYan Zheng 
40717d217feSYan Zheng 			while (size > 0) {
40807d400a6SYan Zheng 				read_extent_buffer(path->nodes[0],
40907d400a6SYan Zheng 						&sector_sum->sum,
41007d400a6SYan Zheng 						((unsigned long)item) +
41107d400a6SYan Zheng 						offset, csum_size);
41217d217feSYan Zheng 				sector_sum->bytenr = start;
41317d217feSYan Zheng 
41417d217feSYan Zheng 				size -= root->sectorsize;
41517d217feSYan Zheng 				start += root->sectorsize;
41617d217feSYan Zheng 				offset += csum_size;
41717d217feSYan Zheng 				sector_sum++;
41817d217feSYan Zheng 			}
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)) {
4260678b618SMark Fasheh 		sums = list_entry(&tmplist, 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;
440e6dcd2dcSChris Mason 	struct btrfs_sector_sum *sector_sum;
4413edf7d33SChris Mason 	struct btrfs_ordered_extent *ordered;
442e015640fSChris Mason 	char *data;
443e015640fSChris Mason 	struct bio_vec *bvec = bio->bi_io_vec;
444e015640fSChris Mason 	int bio_index = 0;
4453edf7d33SChris Mason 	unsigned long total_bytes = 0;
4463edf7d33SChris Mason 	unsigned long this_sum_bytes = 0;
4473edf7d33SChris Mason 	u64 offset;
448d20f7043SChris Mason 	u64 disk_bytenr;
449e015640fSChris Mason 
450e6dcd2dcSChris Mason 	WARN_ON(bio->bi_vcnt <= 0);
451e6dcd2dcSChris Mason 	sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS);
452e015640fSChris Mason 	if (!sums)
453e015640fSChris Mason 		return -ENOMEM;
4543edf7d33SChris Mason 
455ed98b56aSChris Mason 	sector_sum = sums->sums;
456d20f7043SChris Mason 	disk_bytenr = (u64)bio->bi_sector << 9;
457e6dcd2dcSChris Mason 	sums->len = bio->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 */
467d20f7043SChris Mason 	sums->bytenr = ordered->start;
468e015640fSChris Mason 
469e015640fSChris Mason 	while (bio_index < bio->bi_vcnt) {
470d20f7043SChris Mason 		if (!contig)
4713edf7d33SChris Mason 			offset = page_offset(bvec->bv_page) + bvec->bv_offset;
472d20f7043SChris Mason 
473e58dd74bSJosef Bacik 		if (offset >= ordered->file_offset + ordered->len ||
474e58dd74bSJosef Bacik 		    offset < ordered->file_offset) {
4753edf7d33SChris Mason 			unsigned long bytes_left;
4763edf7d33SChris Mason 			sums->len = this_sum_bytes;
4773edf7d33SChris Mason 			this_sum_bytes = 0;
4783edf7d33SChris Mason 			btrfs_add_ordered_sum(inode, ordered, sums);
4793edf7d33SChris Mason 			btrfs_put_ordered_extent(ordered);
4803edf7d33SChris Mason 
4813edf7d33SChris Mason 			bytes_left = bio->bi_size - total_bytes;
4823edf7d33SChris Mason 
4833edf7d33SChris Mason 			sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
4843edf7d33SChris Mason 				       GFP_NOFS);
48579787eaaSJeff Mahoney 			BUG_ON(!sums); /* -ENOMEM */
486ed98b56aSChris Mason 			sector_sum = sums->sums;
4873edf7d33SChris Mason 			sums->len = bytes_left;
488d20f7043SChris Mason 			ordered = btrfs_lookup_ordered_extent(inode, offset);
48979787eaaSJeff Mahoney 			BUG_ON(!ordered); /* Logic error */
490d20f7043SChris Mason 			sums->bytenr = ordered->start;
4913edf7d33SChris Mason 		}
4923edf7d33SChris Mason 
4937ac687d9SCong Wang 		data = kmap_atomic(bvec->bv_page);
494e6dcd2dcSChris Mason 		sector_sum->sum = ~(u32)0;
495b0496686SLiu Bo 		sector_sum->sum = btrfs_csum_data(data + bvec->bv_offset,
496e6dcd2dcSChris Mason 						  sector_sum->sum,
497e6dcd2dcSChris Mason 						  bvec->bv_len);
4987ac687d9SCong Wang 		kunmap_atomic(data);
499e6dcd2dcSChris Mason 		btrfs_csum_final(sector_sum->sum,
500e6dcd2dcSChris Mason 				 (char *)&sector_sum->sum);
501d20f7043SChris Mason 		sector_sum->bytenr = disk_bytenr;
502ed98b56aSChris Mason 
503e6dcd2dcSChris Mason 		sector_sum++;
504e015640fSChris Mason 		bio_index++;
5053edf7d33SChris Mason 		total_bytes += bvec->bv_len;
5063edf7d33SChris Mason 		this_sum_bytes += bvec->bv_len;
507d20f7043SChris Mason 		disk_bytenr += bvec->bv_len;
508d20f7043SChris Mason 		offset += bvec->bv_len;
509e015640fSChris Mason 		bvec++;
510e015640fSChris Mason 	}
511ed98b56aSChris Mason 	this_sum_bytes = 0;
5123edf7d33SChris Mason 	btrfs_add_ordered_sum(inode, ordered, sums);
5133edf7d33SChris Mason 	btrfs_put_ordered_extent(ordered);
514e015640fSChris Mason 	return 0;
515e015640fSChris Mason }
516e015640fSChris Mason 
517459931ecSChris Mason /*
518459931ecSChris Mason  * helper function for csum removal, this expects the
519459931ecSChris Mason  * key to describe the csum pointed to by the path, and it expects
520459931ecSChris Mason  * the csum to overlap the range [bytenr, len]
521459931ecSChris Mason  *
522459931ecSChris Mason  * The csum should not be entirely contained in the range and the
523459931ecSChris Mason  * range should not be entirely contained in the csum.
524459931ecSChris Mason  *
525459931ecSChris Mason  * This calls btrfs_truncate_item with the correct args based on the
526459931ecSChris Mason  * overlap, and fixes up the key as required.
527459931ecSChris Mason  */
528afe5fea7STsutomu Itoh static noinline void truncate_one_csum(struct btrfs_root *root,
529459931ecSChris Mason 				       struct btrfs_path *path,
530459931ecSChris Mason 				       struct btrfs_key *key,
531459931ecSChris Mason 				       u64 bytenr, u64 len)
532459931ecSChris Mason {
533459931ecSChris Mason 	struct extent_buffer *leaf;
5346c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
535459931ecSChris Mason 	u64 csum_end;
536459931ecSChris Mason 	u64 end_byte = bytenr + len;
537459931ecSChris Mason 	u32 blocksize_bits = root->fs_info->sb->s_blocksize_bits;
538459931ecSChris Mason 
539459931ecSChris Mason 	leaf = path->nodes[0];
540459931ecSChris Mason 	csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
541459931ecSChris Mason 	csum_end <<= root->fs_info->sb->s_blocksize_bits;
542459931ecSChris Mason 	csum_end += key->offset;
543459931ecSChris Mason 
544459931ecSChris Mason 	if (key->offset < bytenr && csum_end <= end_byte) {
545459931ecSChris Mason 		/*
546459931ecSChris Mason 		 *         [ bytenr - len ]
547459931ecSChris Mason 		 *         [   ]
548459931ecSChris Mason 		 *   [csum     ]
549459931ecSChris Mason 		 *   A simple truncate off the end of the item
550459931ecSChris Mason 		 */
551459931ecSChris Mason 		u32 new_size = (bytenr - key->offset) >> blocksize_bits;
552459931ecSChris Mason 		new_size *= csum_size;
553afe5fea7STsutomu Itoh 		btrfs_truncate_item(root, path, new_size, 1);
554459931ecSChris Mason 	} else if (key->offset >= bytenr && csum_end > end_byte &&
555459931ecSChris Mason 		   end_byte > key->offset) {
556459931ecSChris Mason 		/*
557459931ecSChris Mason 		 *         [ bytenr - len ]
558459931ecSChris Mason 		 *                 [ ]
559459931ecSChris Mason 		 *                 [csum     ]
560459931ecSChris Mason 		 * we need to truncate from the beginning of the csum
561459931ecSChris Mason 		 */
562459931ecSChris Mason 		u32 new_size = (csum_end - end_byte) >> blocksize_bits;
563459931ecSChris Mason 		new_size *= csum_size;
564459931ecSChris Mason 
565afe5fea7STsutomu Itoh 		btrfs_truncate_item(root, path, new_size, 0);
566459931ecSChris Mason 
567459931ecSChris Mason 		key->offset = end_byte;
568afe5fea7STsutomu Itoh 		btrfs_set_item_key_safe(root, path, key);
569459931ecSChris Mason 	} else {
570459931ecSChris Mason 		BUG();
571459931ecSChris Mason 	}
572459931ecSChris Mason }
573459931ecSChris Mason 
574459931ecSChris Mason /*
575459931ecSChris Mason  * deletes the csum items from the csum tree for a given
576459931ecSChris Mason  * range of bytes.
577459931ecSChris Mason  */
578459931ecSChris Mason int btrfs_del_csums(struct btrfs_trans_handle *trans,
579459931ecSChris Mason 		    struct btrfs_root *root, u64 bytenr, u64 len)
580459931ecSChris Mason {
581459931ecSChris Mason 	struct btrfs_path *path;
582459931ecSChris Mason 	struct btrfs_key key;
583459931ecSChris Mason 	u64 end_byte = bytenr + len;
584459931ecSChris Mason 	u64 csum_end;
585459931ecSChris Mason 	struct extent_buffer *leaf;
586459931ecSChris Mason 	int ret;
5876c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
588459931ecSChris Mason 	int blocksize_bits = root->fs_info->sb->s_blocksize_bits;
589459931ecSChris Mason 
590459931ecSChris Mason 	root = root->fs_info->csum_root;
591459931ecSChris Mason 
592459931ecSChris Mason 	path = btrfs_alloc_path();
5932a29edc6Sliubo 	if (!path)
5942a29edc6Sliubo 		return -ENOMEM;
595459931ecSChris Mason 
596459931ecSChris Mason 	while (1) {
597459931ecSChris Mason 		key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
598459931ecSChris Mason 		key.offset = end_byte - 1;
599459931ecSChris Mason 		key.type = BTRFS_EXTENT_CSUM_KEY;
600459931ecSChris Mason 
601b9473439SChris Mason 		path->leave_spinning = 1;
602459931ecSChris Mason 		ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
603459931ecSChris Mason 		if (ret > 0) {
604459931ecSChris Mason 			if (path->slots[0] == 0)
60565a246c5STsutomu Itoh 				break;
606459931ecSChris Mason 			path->slots[0]--;
607ad0397a7SJosef Bacik 		} else if (ret < 0) {
60865a246c5STsutomu Itoh 			break;
609459931ecSChris Mason 		}
610ad0397a7SJosef Bacik 
611459931ecSChris Mason 		leaf = path->nodes[0];
612459931ecSChris Mason 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
613459931ecSChris Mason 
614459931ecSChris Mason 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
615459931ecSChris Mason 		    key.type != BTRFS_EXTENT_CSUM_KEY) {
616459931ecSChris Mason 			break;
617459931ecSChris Mason 		}
618459931ecSChris Mason 
619459931ecSChris Mason 		if (key.offset >= end_byte)
620459931ecSChris Mason 			break;
621459931ecSChris Mason 
622459931ecSChris Mason 		csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
623459931ecSChris Mason 		csum_end <<= blocksize_bits;
624459931ecSChris Mason 		csum_end += key.offset;
625459931ecSChris Mason 
626459931ecSChris Mason 		/* this csum ends before we start, we're done */
627459931ecSChris Mason 		if (csum_end <= bytenr)
628459931ecSChris Mason 			break;
629459931ecSChris Mason 
630459931ecSChris Mason 		/* delete the entire item, it is inside our range */
631459931ecSChris Mason 		if (key.offset >= bytenr && csum_end <= end_byte) {
632459931ecSChris Mason 			ret = btrfs_del_item(trans, root, path);
63365a246c5STsutomu Itoh 			if (ret)
63465a246c5STsutomu Itoh 				goto out;
635dcbdd4dcSChris Mason 			if (key.offset == bytenr)
636dcbdd4dcSChris Mason 				break;
637459931ecSChris Mason 		} else if (key.offset < bytenr && csum_end > end_byte) {
638459931ecSChris Mason 			unsigned long offset;
639459931ecSChris Mason 			unsigned long shift_len;
640459931ecSChris Mason 			unsigned long item_offset;
641459931ecSChris Mason 			/*
642459931ecSChris Mason 			 *        [ bytenr - len ]
643459931ecSChris Mason 			 *     [csum                ]
644459931ecSChris Mason 			 *
645459931ecSChris Mason 			 * Our bytes are in the middle of the csum,
646459931ecSChris Mason 			 * we need to split this item and insert a new one.
647459931ecSChris Mason 			 *
648459931ecSChris Mason 			 * But we can't drop the path because the
649459931ecSChris Mason 			 * csum could change, get removed, extended etc.
650459931ecSChris Mason 			 *
651459931ecSChris Mason 			 * The trick here is the max size of a csum item leaves
652459931ecSChris Mason 			 * enough room in the tree block for a single
653459931ecSChris Mason 			 * item header.  So, we split the item in place,
654459931ecSChris Mason 			 * adding a new header pointing to the existing
655459931ecSChris Mason 			 * bytes.  Then we loop around again and we have
656459931ecSChris Mason 			 * a nicely formed csum item that we can neatly
657459931ecSChris Mason 			 * truncate.
658459931ecSChris Mason 			 */
659459931ecSChris Mason 			offset = (bytenr - key.offset) >> blocksize_bits;
660459931ecSChris Mason 			offset *= csum_size;
661459931ecSChris Mason 
662459931ecSChris Mason 			shift_len = (len >> blocksize_bits) * csum_size;
663459931ecSChris Mason 
664459931ecSChris Mason 			item_offset = btrfs_item_ptr_offset(leaf,
665459931ecSChris Mason 							    path->slots[0]);
666459931ecSChris Mason 
667459931ecSChris Mason 			memset_extent_buffer(leaf, 0, item_offset + offset,
668459931ecSChris Mason 					     shift_len);
669459931ecSChris Mason 			key.offset = bytenr;
670459931ecSChris Mason 
671459931ecSChris Mason 			/*
672459931ecSChris Mason 			 * btrfs_split_item returns -EAGAIN when the
673459931ecSChris Mason 			 * item changed size or key
674459931ecSChris Mason 			 */
675459931ecSChris Mason 			ret = btrfs_split_item(trans, root, path, &key, offset);
67679787eaaSJeff Mahoney 			if (ret && ret != -EAGAIN) {
67779787eaaSJeff Mahoney 				btrfs_abort_transaction(trans, root, ret);
67879787eaaSJeff Mahoney 				goto out;
67979787eaaSJeff Mahoney 			}
680459931ecSChris Mason 
681459931ecSChris Mason 			key.offset = end_byte - 1;
682459931ecSChris Mason 		} else {
683afe5fea7STsutomu Itoh 			truncate_one_csum(root, path, &key, bytenr, len);
684dcbdd4dcSChris Mason 			if (key.offset < bytenr)
685dcbdd4dcSChris Mason 				break;
686459931ecSChris Mason 		}
687b3b4aa74SDavid Sterba 		btrfs_release_path(path);
688459931ecSChris Mason 	}
68965a246c5STsutomu Itoh 	ret = 0;
690459931ecSChris Mason out:
691459931ecSChris Mason 	btrfs_free_path(path);
69265a246c5STsutomu Itoh 	return ret;
693459931ecSChris Mason }
694459931ecSChris Mason 
6952f697dc6SLiu Bo static u64 btrfs_sector_sum_left(struct btrfs_ordered_sum *sums,
6962f697dc6SLiu Bo 				 struct btrfs_sector_sum *sector_sum,
6972f697dc6SLiu Bo 				 u64 total_bytes, u64 sectorsize)
6982f697dc6SLiu Bo {
6992f697dc6SLiu Bo 	u64 tmp = sectorsize;
7002f697dc6SLiu Bo 	u64 next_sector = sector_sum->bytenr;
7012f697dc6SLiu Bo 	struct btrfs_sector_sum *next = sector_sum + 1;
7022f697dc6SLiu Bo 
7032f697dc6SLiu Bo 	while ((tmp + total_bytes) < sums->len) {
7042f697dc6SLiu Bo 		if (next_sector + sectorsize != next->bytenr)
7052f697dc6SLiu Bo 			break;
7062f697dc6SLiu Bo 		tmp += sectorsize;
7072f697dc6SLiu Bo 		next_sector = next->bytenr;
7082f697dc6SLiu Bo 		next++;
7092f697dc6SLiu Bo 	}
7102f697dc6SLiu Bo 	return tmp;
7112f697dc6SLiu Bo }
7122f697dc6SLiu Bo 
713065631f6SChris Mason int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
714d20f7043SChris Mason 			   struct btrfs_root *root,
715e6dcd2dcSChris Mason 			   struct btrfs_ordered_sum *sums)
716f254e52cSChris Mason {
717d20f7043SChris Mason 	u64 bytenr;
718f254e52cSChris Mason 	int ret;
719f254e52cSChris Mason 	struct btrfs_key file_key;
7206567e837SChris Mason 	struct btrfs_key found_key;
721065631f6SChris Mason 	u64 next_offset;
722e6dcd2dcSChris Mason 	u64 total_bytes = 0;
723065631f6SChris Mason 	int found_next;
7245caf2a00SChris Mason 	struct btrfs_path *path;
725f254e52cSChris Mason 	struct btrfs_csum_item *item;
726065631f6SChris Mason 	struct btrfs_csum_item *item_end;
727ff79f819SChris Mason 	struct extent_buffer *leaf = NULL;
7286567e837SChris Mason 	u64 csum_offset;
729e6dcd2dcSChris Mason 	struct btrfs_sector_sum *sector_sum;
730f578d4bdSChris Mason 	u32 nritems;
731f578d4bdSChris Mason 	u32 ins_size;
7326c41761fSDavid Sterba 	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7336e92f5e6SChris Mason 
7345caf2a00SChris Mason 	path = btrfs_alloc_path();
735d8926bb3SMark Fasheh 	if (!path)
736d8926bb3SMark Fasheh 		return -ENOMEM;
737d8926bb3SMark Fasheh 
738ed98b56aSChris Mason 	sector_sum = sums->sums;
739065631f6SChris Mason again:
740065631f6SChris Mason 	next_offset = (u64)-1;
741065631f6SChris Mason 	found_next = 0;
742d20f7043SChris Mason 	file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
743d20f7043SChris Mason 	file_key.offset = sector_sum->bytenr;
744d20f7043SChris Mason 	bytenr = sector_sum->bytenr;
745d20f7043SChris Mason 	btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
746a429e513SChris Mason 
747d20f7043SChris Mason 	item = btrfs_lookup_csum(trans, root, path, sector_sum->bytenr, 1);
748ff79f819SChris Mason 	if (!IS_ERR(item)) {
749ff79f819SChris Mason 		leaf = path->nodes[0];
750639cb586SChris Mason 		ret = 0;
751a429e513SChris Mason 		goto found;
752ff79f819SChris Mason 	}
753a429e513SChris Mason 	ret = PTR_ERR(item);
7544a500fd1SYan, Zheng 	if (ret != -EFBIG && ret != -ENOENT)
7554a500fd1SYan, Zheng 		goto fail_unlock;
7564a500fd1SYan, Zheng 
757a429e513SChris Mason 	if (ret == -EFBIG) {
758a429e513SChris Mason 		u32 item_size;
759a429e513SChris Mason 		/* we found one, but it isn't big enough yet */
7605f39d397SChris Mason 		leaf = path->nodes[0];
7615f39d397SChris Mason 		item_size = btrfs_item_size_nr(leaf, path->slots[0]);
762607d432dSJosef Bacik 		if ((item_size / csum_size) >=
763607d432dSJosef Bacik 		    MAX_CSUM_ITEMS(root, csum_size)) {
764a429e513SChris Mason 			/* already at max size, make a new one */
765a429e513SChris Mason 			goto insert;
766a429e513SChris Mason 		}
767a429e513SChris Mason 	} else {
768f578d4bdSChris Mason 		int slot = path->slots[0] + 1;
769a429e513SChris Mason 		/* we didn't find a csum item, insert one */
770f578d4bdSChris Mason 		nritems = btrfs_header_nritems(path->nodes[0]);
771f578d4bdSChris Mason 		if (path->slots[0] >= nritems - 1) {
772f578d4bdSChris Mason 			ret = btrfs_next_leaf(root, path);
773b56baf5bSYan 			if (ret == 1)
774f578d4bdSChris Mason 				found_next = 1;
775b56baf5bSYan 			if (ret != 0)
776f578d4bdSChris Mason 				goto insert;
777b56baf5bSYan 			slot = 0;
778f578d4bdSChris Mason 		}
779f578d4bdSChris Mason 		btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
780d20f7043SChris Mason 		if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
781d20f7043SChris Mason 		    found_key.type != BTRFS_EXTENT_CSUM_KEY) {
782f578d4bdSChris Mason 			found_next = 1;
783f578d4bdSChris Mason 			goto insert;
784f578d4bdSChris Mason 		}
785f578d4bdSChris Mason 		next_offset = found_key.offset;
786f578d4bdSChris Mason 		found_next = 1;
787a429e513SChris Mason 		goto insert;
788a429e513SChris Mason 	}
789a429e513SChris Mason 
790a429e513SChris Mason 	/*
791a429e513SChris Mason 	 * at this point, we know the tree has an item, but it isn't big
792a429e513SChris Mason 	 * enough yet to put our csum in.  Grow it
793a429e513SChris Mason 	 */
794b3b4aa74SDavid Sterba 	btrfs_release_path(path);
7956567e837SChris Mason 	ret = btrfs_search_slot(trans, root, &file_key, path,
796607d432dSJosef Bacik 				csum_size, 1);
7976567e837SChris Mason 	if (ret < 0)
79853863232SChris Mason 		goto fail_unlock;
799459931ecSChris Mason 
800459931ecSChris Mason 	if (ret > 0) {
801459931ecSChris Mason 		if (path->slots[0] == 0)
8026567e837SChris Mason 			goto insert;
8036567e837SChris Mason 		path->slots[0]--;
804459931ecSChris Mason 	}
805459931ecSChris Mason 
8065f39d397SChris Mason 	leaf = path->nodes[0];
8075f39d397SChris Mason 	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
808d20f7043SChris Mason 	csum_offset = (bytenr - found_key.offset) >>
8096567e837SChris Mason 			root->fs_info->sb->s_blocksize_bits;
810459931ecSChris Mason 
811d20f7043SChris Mason 	if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY ||
812d20f7043SChris Mason 	    found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
813607d432dSJosef Bacik 	    csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) {
8146567e837SChris Mason 		goto insert;
8156567e837SChris Mason 	}
816459931ecSChris Mason 
8172f697dc6SLiu Bo 	if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) /
818607d432dSJosef Bacik 	    csum_size) {
8192f697dc6SLiu Bo 		int extend_nr;
8202f697dc6SLiu Bo 		u64 tmp;
8212f697dc6SLiu Bo 		u32 diff;
8222f697dc6SLiu Bo 		u32 free_space;
823459931ecSChris Mason 
8242f697dc6SLiu Bo 		if (btrfs_leaf_free_space(root, leaf) <
8252f697dc6SLiu Bo 				 sizeof(struct btrfs_item) + csum_size * 2)
8262f697dc6SLiu Bo 			goto insert;
8272f697dc6SLiu Bo 
8282f697dc6SLiu Bo 		free_space = btrfs_leaf_free_space(root, leaf) -
8292f697dc6SLiu Bo 					 sizeof(struct btrfs_item) - csum_size;
8302f697dc6SLiu Bo 		tmp = btrfs_sector_sum_left(sums, sector_sum, total_bytes,
8312f697dc6SLiu Bo 					    root->sectorsize);
8322f697dc6SLiu Bo 		tmp >>= root->fs_info->sb->s_blocksize_bits;
8332f697dc6SLiu Bo 		WARN_ON(tmp < 1);
8342f697dc6SLiu Bo 
8352f697dc6SLiu Bo 		extend_nr = max_t(int, 1, (int)tmp);
8362f697dc6SLiu Bo 		diff = (csum_offset + extend_nr) * csum_size;
8372f697dc6SLiu Bo 		diff = min(diff, MAX_CSUM_ITEMS(root, csum_size) * csum_size);
838459931ecSChris Mason 
8395f39d397SChris Mason 		diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
8402f697dc6SLiu Bo 		diff = min(free_space, diff);
8412f697dc6SLiu Bo 		diff /= csum_size;
8422f697dc6SLiu Bo 		diff *= csum_size;
843459931ecSChris Mason 
844*4b90c680STsutomu Itoh 		btrfs_extend_item(root, path, diff);
8456567e837SChris Mason 		goto csum;
8466567e837SChris Mason 	}
8476567e837SChris Mason 
8486567e837SChris Mason insert:
849b3b4aa74SDavid Sterba 	btrfs_release_path(path);
8506567e837SChris Mason 	csum_offset = 0;
851f578d4bdSChris Mason 	if (found_next) {
8522f697dc6SLiu Bo 		u64 tmp;
853d20f7043SChris Mason 
8542f697dc6SLiu Bo 		tmp = btrfs_sector_sum_left(sums, sector_sum, total_bytes,
8552f697dc6SLiu Bo 					    root->sectorsize);
856f578d4bdSChris Mason 		tmp >>= root->fs_info->sb->s_blocksize_bits;
8572f697dc6SLiu Bo 		tmp = min(tmp, (next_offset - file_key.offset) >>
8582f697dc6SLiu Bo 					 root->fs_info->sb->s_blocksize_bits);
8592f697dc6SLiu Bo 
860f578d4bdSChris Mason 		tmp = max((u64)1, tmp);
861607d432dSJosef Bacik 		tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size));
862607d432dSJosef Bacik 		ins_size = csum_size * tmp;
863f578d4bdSChris Mason 	} else {
864607d432dSJosef Bacik 		ins_size = csum_size;
865f578d4bdSChris Mason 	}
866b9473439SChris Mason 	path->leave_spinning = 1;
8675caf2a00SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, &file_key,
868f578d4bdSChris Mason 				      ins_size);
869b9473439SChris Mason 	path->leave_spinning = 0;
87054aa1f4dSChris Mason 	if (ret < 0)
87153863232SChris Mason 		goto fail_unlock;
872a429e513SChris Mason 	if (ret != 0) {
873a429e513SChris Mason 		WARN_ON(1);
87453863232SChris Mason 		goto fail_unlock;
875a429e513SChris Mason 	}
8766567e837SChris Mason csum:
8775f39d397SChris Mason 	leaf = path->nodes[0];
8785f39d397SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
879f254e52cSChris Mason 	ret = 0;
880509659cdSChris Mason 	item = (struct btrfs_csum_item *)((unsigned char *)item +
881607d432dSJosef Bacik 					  csum_offset * csum_size);
882b18c6685SChris Mason found:
883065631f6SChris Mason 	item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
884065631f6SChris Mason 	item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
885065631f6SChris Mason 				      btrfs_item_size_nr(leaf, path->slots[0]));
886e6dcd2dcSChris Mason next_sector:
887aadfeb6eSChris Mason 
888a6591715SChris Mason 	write_extent_buffer(leaf, &sector_sum->sum, (unsigned long)item, csum_size);
8897f3c74fbSChris Mason 
890e6dcd2dcSChris Mason 	total_bytes += root->sectorsize;
891e6dcd2dcSChris Mason 	sector_sum++;
892e6dcd2dcSChris Mason 	if (total_bytes < sums->len) {
8936e92f5e6SChris Mason 		item = (struct btrfs_csum_item *)((char *)item +
894607d432dSJosef Bacik 						  csum_size);
895d20f7043SChris Mason 		if (item < item_end && bytenr + PAGE_CACHE_SIZE ==
896d20f7043SChris Mason 		    sector_sum->bytenr) {
897d20f7043SChris Mason 			bytenr = sector_sum->bytenr;
898e6dcd2dcSChris Mason 			goto next_sector;
899065631f6SChris Mason 		}
9002e1a992eSChris Mason 	}
901a6591715SChris Mason 
9025caf2a00SChris Mason 	btrfs_mark_buffer_dirty(path->nodes[0]);
903e6dcd2dcSChris Mason 	if (total_bytes < sums->len) {
904b3b4aa74SDavid Sterba 		btrfs_release_path(path);
905b9473439SChris Mason 		cond_resched();
906065631f6SChris Mason 		goto again;
907065631f6SChris Mason 	}
90853863232SChris Mason out:
9095caf2a00SChris Mason 	btrfs_free_path(path);
910f254e52cSChris Mason 	return ret;
91153863232SChris Mason 
91253863232SChris Mason fail_unlock:
91353863232SChris Mason 	goto out;
914f254e52cSChris Mason }
915