xref: /openbmc/linux/fs/btrfs/file-item.c (revision da8269a3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2007 Oracle.  All rights reserved.
4  */
5 
6 #include <linux/bio.h>
7 #include <linux/slab.h>
8 #include <linux/pagemap.h>
9 #include <linux/highmem.h>
10 #include <linux/sched/mm.h>
11 #include <crypto/hash.h>
12 #include "messages.h"
13 #include "misc.h"
14 #include "ctree.h"
15 #include "disk-io.h"
16 #include "transaction.h"
17 #include "bio.h"
18 #include "print-tree.h"
19 #include "compression.h"
20 #include "fs.h"
21 #include "accessors.h"
22 #include "file-item.h"
23 #include "super.h"
24 
25 #define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \
26 				   sizeof(struct btrfs_item) * 2) / \
27 				  size) - 1))
28 
29 #define MAX_CSUM_ITEMS(r, size) (min_t(u32, __MAX_CSUM_ITEMS(r, size), \
30 				       PAGE_SIZE))
31 
32 /*
33  * Set inode's size according to filesystem options.
34  *
35  * @inode:      inode we want to update the disk_i_size for
36  * @new_i_size: i_size we want to set to, 0 if we use i_size
37  *
38  * With NO_HOLES set this simply sets the disk_is_size to whatever i_size_read()
39  * returns as it is perfectly fine with a file that has holes without hole file
40  * extent items.
41  *
42  * However without NO_HOLES we need to only return the area that is contiguous
43  * from the 0 offset of the file.  Otherwise we could end up adjust i_size up
44  * to an extent that has a gap in between.
45  *
46  * Finally new_i_size should only be set in the case of truncate where we're not
47  * ready to use i_size_read() as the limiter yet.
48  */
49 void btrfs_inode_safe_disk_i_size_write(struct btrfs_inode *inode, u64 new_i_size)
50 {
51 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
52 	u64 start, end, i_size;
53 	int ret;
54 
55 	i_size = new_i_size ?: i_size_read(&inode->vfs_inode);
56 	if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
57 		inode->disk_i_size = i_size;
58 		return;
59 	}
60 
61 	spin_lock(&inode->lock);
62 	ret = find_contiguous_extent_bit(&inode->file_extent_tree, 0, &start,
63 					 &end, EXTENT_DIRTY);
64 	if (!ret && start == 0)
65 		i_size = min(i_size, end + 1);
66 	else
67 		i_size = 0;
68 	inode->disk_i_size = i_size;
69 	spin_unlock(&inode->lock);
70 }
71 
72 /*
73  * Mark range within a file as having a new extent inserted.
74  *
75  * @inode: inode being modified
76  * @start: start file offset of the file extent we've inserted
77  * @len:   logical length of the file extent item
78  *
79  * Call when we are inserting a new file extent where there was none before.
80  * Does not need to call this in the case where we're replacing an existing file
81  * extent, however if not sure it's fine to call this multiple times.
82  *
83  * The start and len must match the file extent item, so thus must be sectorsize
84  * aligned.
85  */
86 int btrfs_inode_set_file_extent_range(struct btrfs_inode *inode, u64 start,
87 				      u64 len)
88 {
89 	if (len == 0)
90 		return 0;
91 
92 	ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize));
93 
94 	if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES))
95 		return 0;
96 	return set_extent_bits(&inode->file_extent_tree, start, start + len - 1,
97 			       EXTENT_DIRTY);
98 }
99 
100 /*
101  * Mark an inode range as not having a backing extent.
102  *
103  * @inode: inode being modified
104  * @start: start file offset of the file extent we've inserted
105  * @len:   logical length of the file extent item
106  *
107  * Called when we drop a file extent, for example when we truncate.  Doesn't
108  * need to be called for cases where we're replacing a file extent, like when
109  * we've COWed a file extent.
110  *
111  * The start and len must match the file extent item, so thus must be sectorsize
112  * aligned.
113  */
114 int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start,
115 					u64 len)
116 {
117 	if (len == 0)
118 		return 0;
119 
120 	ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize) ||
121 	       len == (u64)-1);
122 
123 	if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES))
124 		return 0;
125 	return clear_extent_bit(&inode->file_extent_tree, start,
126 				start + len - 1, EXTENT_DIRTY, NULL);
127 }
128 
129 static size_t bytes_to_csum_size(const struct btrfs_fs_info *fs_info, u32 bytes)
130 {
131 	ASSERT(IS_ALIGNED(bytes, fs_info->sectorsize));
132 
133 	return (bytes >> fs_info->sectorsize_bits) * fs_info->csum_size;
134 }
135 
136 static size_t csum_size_to_bytes(const struct btrfs_fs_info *fs_info, u32 csum_size)
137 {
138 	ASSERT(IS_ALIGNED(csum_size, fs_info->csum_size));
139 
140 	return (csum_size / fs_info->csum_size) << fs_info->sectorsize_bits;
141 }
142 
143 static inline u32 max_ordered_sum_bytes(const struct btrfs_fs_info *fs_info)
144 {
145 	u32 max_csum_size = round_down(PAGE_SIZE - sizeof(struct btrfs_ordered_sum),
146 				       fs_info->csum_size);
147 
148 	return csum_size_to_bytes(fs_info, max_csum_size);
149 }
150 
151 /*
152  * Calculate the total size needed to allocate for an ordered sum structure
153  * spanning @bytes in the file.
154  */
155 static int btrfs_ordered_sum_size(struct btrfs_fs_info *fs_info, unsigned long bytes)
156 {
157 	return sizeof(struct btrfs_ordered_sum) + bytes_to_csum_size(fs_info, bytes);
158 }
159 
160 int btrfs_insert_hole_extent(struct btrfs_trans_handle *trans,
161 			     struct btrfs_root *root,
162 			     u64 objectid, u64 pos, u64 num_bytes)
163 {
164 	int ret = 0;
165 	struct btrfs_file_extent_item *item;
166 	struct btrfs_key file_key;
167 	struct btrfs_path *path;
168 	struct extent_buffer *leaf;
169 
170 	path = btrfs_alloc_path();
171 	if (!path)
172 		return -ENOMEM;
173 	file_key.objectid = objectid;
174 	file_key.offset = pos;
175 	file_key.type = BTRFS_EXTENT_DATA_KEY;
176 
177 	ret = btrfs_insert_empty_item(trans, root, path, &file_key,
178 				      sizeof(*item));
179 	if (ret < 0)
180 		goto out;
181 	BUG_ON(ret); /* Can't happen */
182 	leaf = path->nodes[0];
183 	item = btrfs_item_ptr(leaf, path->slots[0],
184 			      struct btrfs_file_extent_item);
185 	btrfs_set_file_extent_disk_bytenr(leaf, item, 0);
186 	btrfs_set_file_extent_disk_num_bytes(leaf, item, 0);
187 	btrfs_set_file_extent_offset(leaf, item, 0);
188 	btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
189 	btrfs_set_file_extent_ram_bytes(leaf, item, num_bytes);
190 	btrfs_set_file_extent_generation(leaf, item, trans->transid);
191 	btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
192 	btrfs_set_file_extent_compression(leaf, item, 0);
193 	btrfs_set_file_extent_encryption(leaf, item, 0);
194 	btrfs_set_file_extent_other_encoding(leaf, item, 0);
195 
196 	btrfs_mark_buffer_dirty(leaf);
197 out:
198 	btrfs_free_path(path);
199 	return ret;
200 }
201 
202 static struct btrfs_csum_item *
203 btrfs_lookup_csum(struct btrfs_trans_handle *trans,
204 		  struct btrfs_root *root,
205 		  struct btrfs_path *path,
206 		  u64 bytenr, int cow)
207 {
208 	struct btrfs_fs_info *fs_info = root->fs_info;
209 	int ret;
210 	struct btrfs_key file_key;
211 	struct btrfs_key found_key;
212 	struct btrfs_csum_item *item;
213 	struct extent_buffer *leaf;
214 	u64 csum_offset = 0;
215 	const u32 csum_size = fs_info->csum_size;
216 	int csums_in_item;
217 
218 	file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
219 	file_key.offset = bytenr;
220 	file_key.type = BTRFS_EXTENT_CSUM_KEY;
221 	ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
222 	if (ret < 0)
223 		goto fail;
224 	leaf = path->nodes[0];
225 	if (ret > 0) {
226 		ret = 1;
227 		if (path->slots[0] == 0)
228 			goto fail;
229 		path->slots[0]--;
230 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
231 		if (found_key.type != BTRFS_EXTENT_CSUM_KEY)
232 			goto fail;
233 
234 		csum_offset = (bytenr - found_key.offset) >>
235 				fs_info->sectorsize_bits;
236 		csums_in_item = btrfs_item_size(leaf, path->slots[0]);
237 		csums_in_item /= csum_size;
238 
239 		if (csum_offset == csums_in_item) {
240 			ret = -EFBIG;
241 			goto fail;
242 		} else if (csum_offset > csums_in_item) {
243 			goto fail;
244 		}
245 	}
246 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
247 	item = (struct btrfs_csum_item *)((unsigned char *)item +
248 					  csum_offset * csum_size);
249 	return item;
250 fail:
251 	if (ret > 0)
252 		ret = -ENOENT;
253 	return ERR_PTR(ret);
254 }
255 
256 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
257 			     struct btrfs_root *root,
258 			     struct btrfs_path *path, u64 objectid,
259 			     u64 offset, int mod)
260 {
261 	struct btrfs_key file_key;
262 	int ins_len = mod < 0 ? -1 : 0;
263 	int cow = mod != 0;
264 
265 	file_key.objectid = objectid;
266 	file_key.offset = offset;
267 	file_key.type = BTRFS_EXTENT_DATA_KEY;
268 
269 	return btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
270 }
271 
272 /*
273  * Find checksums for logical bytenr range [disk_bytenr, disk_bytenr + len) and
274  * store the result to @dst.
275  *
276  * Return >0 for the number of sectors we found.
277  * Return 0 for the range [disk_bytenr, disk_bytenr + sectorsize) has no csum
278  * for it. Caller may want to try next sector until one range is hit.
279  * Return <0 for fatal error.
280  */
281 static int search_csum_tree(struct btrfs_fs_info *fs_info,
282 			    struct btrfs_path *path, u64 disk_bytenr,
283 			    u64 len, u8 *dst)
284 {
285 	struct btrfs_root *csum_root;
286 	struct btrfs_csum_item *item = NULL;
287 	struct btrfs_key key;
288 	const u32 sectorsize = fs_info->sectorsize;
289 	const u32 csum_size = fs_info->csum_size;
290 	u32 itemsize;
291 	int ret;
292 	u64 csum_start;
293 	u64 csum_len;
294 
295 	ASSERT(IS_ALIGNED(disk_bytenr, sectorsize) &&
296 	       IS_ALIGNED(len, sectorsize));
297 
298 	/* Check if the current csum item covers disk_bytenr */
299 	if (path->nodes[0]) {
300 		item = btrfs_item_ptr(path->nodes[0], path->slots[0],
301 				      struct btrfs_csum_item);
302 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
303 		itemsize = btrfs_item_size(path->nodes[0], path->slots[0]);
304 
305 		csum_start = key.offset;
306 		csum_len = (itemsize / csum_size) * sectorsize;
307 
308 		if (in_range(disk_bytenr, csum_start, csum_len))
309 			goto found;
310 	}
311 
312 	/* Current item doesn't contain the desired range, search again */
313 	btrfs_release_path(path);
314 	csum_root = btrfs_csum_root(fs_info, disk_bytenr);
315 	item = btrfs_lookup_csum(NULL, csum_root, path, disk_bytenr, 0);
316 	if (IS_ERR(item)) {
317 		ret = PTR_ERR(item);
318 		goto out;
319 	}
320 	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
321 	itemsize = btrfs_item_size(path->nodes[0], path->slots[0]);
322 
323 	csum_start = key.offset;
324 	csum_len = (itemsize / csum_size) * sectorsize;
325 	ASSERT(in_range(disk_bytenr, csum_start, csum_len));
326 
327 found:
328 	ret = (min(csum_start + csum_len, disk_bytenr + len) -
329 		   disk_bytenr) >> fs_info->sectorsize_bits;
330 	read_extent_buffer(path->nodes[0], dst, (unsigned long)item,
331 			ret * csum_size);
332 out:
333 	if (ret == -ENOENT || ret == -EFBIG)
334 		ret = 0;
335 	return ret;
336 }
337 
338 /*
339  * Locate the file_offset of @cur_disk_bytenr of a @bio.
340  *
341  * Bio of btrfs represents read range of
342  * [bi_sector << 9, bi_sector << 9 + bi_size).
343  * Knowing this, we can iterate through each bvec to locate the page belong to
344  * @cur_disk_bytenr and get the file offset.
345  *
346  * @inode is used to determine if the bvec page really belongs to @inode.
347  *
348  * Return 0 if we can't find the file offset
349  * Return >0 if we find the file offset and restore it to @file_offset_ret
350  */
351 static int search_file_offset_in_bio(struct bio *bio, struct inode *inode,
352 				     u64 disk_bytenr, u64 *file_offset_ret)
353 {
354 	struct bvec_iter iter;
355 	struct bio_vec bvec;
356 	u64 cur = bio->bi_iter.bi_sector << SECTOR_SHIFT;
357 	int ret = 0;
358 
359 	bio_for_each_segment(bvec, bio, iter) {
360 		struct page *page = bvec.bv_page;
361 
362 		if (cur > disk_bytenr)
363 			break;
364 		if (cur + bvec.bv_len <= disk_bytenr) {
365 			cur += bvec.bv_len;
366 			continue;
367 		}
368 		ASSERT(in_range(disk_bytenr, cur, bvec.bv_len));
369 		if (page->mapping && page->mapping->host &&
370 		    page->mapping->host == inode) {
371 			ret = 1;
372 			*file_offset_ret = page_offset(page) + bvec.bv_offset +
373 					   disk_bytenr - cur;
374 			break;
375 		}
376 	}
377 	return ret;
378 }
379 
380 /*
381  * Lookup the checksum for the read bio in csum tree.
382  *
383  * Return: BLK_STS_RESOURCE if allocating memory fails, BLK_STS_OK otherwise.
384  */
385 blk_status_t btrfs_lookup_bio_sums(struct btrfs_bio *bbio)
386 {
387 	struct btrfs_inode *inode = bbio->inode;
388 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
389 	struct extent_io_tree *io_tree = &inode->io_tree;
390 	struct bio *bio = &bbio->bio;
391 	struct btrfs_path *path;
392 	const u32 sectorsize = fs_info->sectorsize;
393 	const u32 csum_size = fs_info->csum_size;
394 	u32 orig_len = bio->bi_iter.bi_size;
395 	u64 orig_disk_bytenr = bio->bi_iter.bi_sector << SECTOR_SHIFT;
396 	u64 cur_disk_bytenr;
397 	const unsigned int nblocks = orig_len >> fs_info->sectorsize_bits;
398 	int count = 0;
399 	blk_status_t ret = BLK_STS_OK;
400 
401 	if ((inode->flags & BTRFS_INODE_NODATASUM) ||
402 	    test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state))
403 		return BLK_STS_OK;
404 
405 	/*
406 	 * This function is only called for read bio.
407 	 *
408 	 * This means two things:
409 	 * - All our csums should only be in csum tree
410 	 *   No ordered extents csums, as ordered extents are only for write
411 	 *   path.
412 	 * - No need to bother any other info from bvec
413 	 *   Since we're looking up csums, the only important info is the
414 	 *   disk_bytenr and the length, which can be extracted from bi_iter
415 	 *   directly.
416 	 */
417 	ASSERT(bio_op(bio) == REQ_OP_READ);
418 	path = btrfs_alloc_path();
419 	if (!path)
420 		return BLK_STS_RESOURCE;
421 
422 	if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) {
423 		bbio->csum = kmalloc_array(nblocks, csum_size, GFP_NOFS);
424 		if (!bbio->csum) {
425 			btrfs_free_path(path);
426 			return BLK_STS_RESOURCE;
427 		}
428 	} else {
429 		bbio->csum = bbio->csum_inline;
430 	}
431 
432 	/*
433 	 * If requested number of sectors is larger than one leaf can contain,
434 	 * kick the readahead for csum tree.
435 	 */
436 	if (nblocks > fs_info->csums_per_leaf)
437 		path->reada = READA_FORWARD;
438 
439 	/*
440 	 * the free space stuff is only read when it hasn't been
441 	 * updated in the current transaction.  So, we can safely
442 	 * read from the commit root and sidestep a nasty deadlock
443 	 * between reading the free space cache and updating the csum tree.
444 	 */
445 	if (btrfs_is_free_space_inode(inode)) {
446 		path->search_commit_root = 1;
447 		path->skip_locking = 1;
448 	}
449 
450 	for (cur_disk_bytenr = orig_disk_bytenr;
451 	     cur_disk_bytenr < orig_disk_bytenr + orig_len;
452 	     cur_disk_bytenr += (count * sectorsize)) {
453 		u64 search_len = orig_disk_bytenr + orig_len - cur_disk_bytenr;
454 		unsigned int sector_offset;
455 		u8 *csum_dst;
456 
457 		/*
458 		 * Although both cur_disk_bytenr and orig_disk_bytenr is u64,
459 		 * we're calculating the offset to the bio start.
460 		 *
461 		 * Bio size is limited to UINT_MAX, thus unsigned int is large
462 		 * enough to contain the raw result, not to mention the right
463 		 * shifted result.
464 		 */
465 		ASSERT(cur_disk_bytenr - orig_disk_bytenr < UINT_MAX);
466 		sector_offset = (cur_disk_bytenr - orig_disk_bytenr) >>
467 				fs_info->sectorsize_bits;
468 		csum_dst = bbio->csum + sector_offset * csum_size;
469 
470 		count = search_csum_tree(fs_info, path, cur_disk_bytenr,
471 					 search_len, csum_dst);
472 		if (count < 0) {
473 			ret = errno_to_blk_status(count);
474 			if (bbio->csum != bbio->csum_inline)
475 				kfree(bbio->csum);
476 			bbio->csum = NULL;
477 			break;
478 		}
479 
480 		/*
481 		 * We didn't find a csum for this range.  We need to make sure
482 		 * we complain loudly about this, because we are not NODATASUM.
483 		 *
484 		 * However for the DATA_RELOC inode we could potentially be
485 		 * relocating data extents for a NODATASUM inode, so the inode
486 		 * itself won't be marked with NODATASUM, but the extent we're
487 		 * copying is in fact NODATASUM.  If we don't find a csum we
488 		 * assume this is the case.
489 		 */
490 		if (count == 0) {
491 			memset(csum_dst, 0, csum_size);
492 			count = 1;
493 
494 			if (inode->root->root_key.objectid ==
495 			    BTRFS_DATA_RELOC_TREE_OBJECTID) {
496 				u64 file_offset;
497 
498 				if (search_file_offset_in_bio(bio,
499 							      &inode->vfs_inode,
500 							      cur_disk_bytenr,
501 							      &file_offset))
502 					set_extent_bits(io_tree, file_offset,
503 						file_offset + sectorsize - 1,
504 						EXTENT_NODATASUM);
505 			} else {
506 				btrfs_warn_rl(fs_info,
507 			"csum hole found for disk bytenr range [%llu, %llu)",
508 				cur_disk_bytenr, cur_disk_bytenr + sectorsize);
509 			}
510 		}
511 	}
512 
513 	btrfs_free_path(path);
514 	return ret;
515 }
516 
517 int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end,
518 			    struct list_head *list, int search_commit,
519 			    bool nowait)
520 {
521 	struct btrfs_fs_info *fs_info = root->fs_info;
522 	struct btrfs_key key;
523 	struct btrfs_path *path;
524 	struct extent_buffer *leaf;
525 	struct btrfs_ordered_sum *sums;
526 	struct btrfs_csum_item *item;
527 	LIST_HEAD(tmplist);
528 	int ret;
529 
530 	ASSERT(IS_ALIGNED(start, fs_info->sectorsize) &&
531 	       IS_ALIGNED(end + 1, fs_info->sectorsize));
532 
533 	path = btrfs_alloc_path();
534 	if (!path)
535 		return -ENOMEM;
536 
537 	path->nowait = nowait;
538 	if (search_commit) {
539 		path->skip_locking = 1;
540 		path->reada = READA_FORWARD;
541 		path->search_commit_root = 1;
542 	}
543 
544 	key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
545 	key.offset = start;
546 	key.type = BTRFS_EXTENT_CSUM_KEY;
547 
548 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
549 	if (ret < 0)
550 		goto fail;
551 	if (ret > 0 && path->slots[0] > 0) {
552 		leaf = path->nodes[0];
553 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
554 
555 		/*
556 		 * There are two cases we can hit here for the previous csum
557 		 * item:
558 		 *
559 		 *		|<- search range ->|
560 		 *	|<- csum item ->|
561 		 *
562 		 * Or
563 		 *				|<- search range ->|
564 		 *	|<- csum item ->|
565 		 *
566 		 * Check if the previous csum item covers the leading part of
567 		 * the search range.  If so we have to start from previous csum
568 		 * item.
569 		 */
570 		if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
571 		    key.type == BTRFS_EXTENT_CSUM_KEY) {
572 			if (bytes_to_csum_size(fs_info, start - key.offset) <
573 			    btrfs_item_size(leaf, path->slots[0] - 1))
574 				path->slots[0]--;
575 		}
576 	}
577 
578 	while (start <= end) {
579 		u64 csum_end;
580 
581 		leaf = path->nodes[0];
582 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
583 			ret = btrfs_next_leaf(root, path);
584 			if (ret < 0)
585 				goto fail;
586 			if (ret > 0)
587 				break;
588 			leaf = path->nodes[0];
589 		}
590 
591 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
592 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
593 		    key.type != BTRFS_EXTENT_CSUM_KEY ||
594 		    key.offset > end)
595 			break;
596 
597 		if (key.offset > start)
598 			start = key.offset;
599 
600 		csum_end = key.offset + csum_size_to_bytes(fs_info,
601 					btrfs_item_size(leaf, path->slots[0]));
602 		if (csum_end <= start) {
603 			path->slots[0]++;
604 			continue;
605 		}
606 
607 		csum_end = min(csum_end, end + 1);
608 		item = btrfs_item_ptr(path->nodes[0], path->slots[0],
609 				      struct btrfs_csum_item);
610 		while (start < csum_end) {
611 			unsigned long offset;
612 			size_t size;
613 
614 			size = min_t(size_t, csum_end - start,
615 				     max_ordered_sum_bytes(fs_info));
616 			sums = kzalloc(btrfs_ordered_sum_size(fs_info, size),
617 				       GFP_NOFS);
618 			if (!sums) {
619 				ret = -ENOMEM;
620 				goto fail;
621 			}
622 
623 			sums->bytenr = start;
624 			sums->len = (int)size;
625 
626 			offset = bytes_to_csum_size(fs_info, start - key.offset);
627 
628 			read_extent_buffer(path->nodes[0],
629 					   sums->sums,
630 					   ((unsigned long)item) + offset,
631 					   bytes_to_csum_size(fs_info, size));
632 
633 			start += size;
634 			list_add_tail(&sums->list, &tmplist);
635 		}
636 		path->slots[0]++;
637 	}
638 	ret = 0;
639 fail:
640 	while (ret < 0 && !list_empty(&tmplist)) {
641 		sums = list_entry(tmplist.next, struct btrfs_ordered_sum, list);
642 		list_del(&sums->list);
643 		kfree(sums);
644 	}
645 	list_splice_tail(&tmplist, list);
646 
647 	btrfs_free_path(path);
648 	return ret;
649 }
650 
651 /*
652  * Do the same work as btrfs_lookup_csums_list(), the difference is in how
653  * we return the result.
654  *
655  * This version will set the corresponding bits in @csum_bitmap to represent
656  * that there is a csum found.
657  * Each bit represents a sector. Thus caller should ensure @csum_buf passed
658  * in is large enough to contain all csums.
659  */
660 int btrfs_lookup_csums_bitmap(struct btrfs_root *root, u64 start, u64 end,
661 			      u8 *csum_buf, unsigned long *csum_bitmap)
662 {
663 	struct btrfs_fs_info *fs_info = root->fs_info;
664 	struct btrfs_key key;
665 	struct btrfs_path *path;
666 	struct extent_buffer *leaf;
667 	struct btrfs_csum_item *item;
668 	const u64 orig_start = start;
669 	int ret;
670 
671 	ASSERT(IS_ALIGNED(start, fs_info->sectorsize) &&
672 	       IS_ALIGNED(end + 1, fs_info->sectorsize));
673 
674 	path = btrfs_alloc_path();
675 	if (!path)
676 		return -ENOMEM;
677 
678 	key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
679 	key.type = BTRFS_EXTENT_CSUM_KEY;
680 	key.offset = start;
681 
682 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
683 	if (ret < 0)
684 		goto fail;
685 	if (ret > 0 && path->slots[0] > 0) {
686 		leaf = path->nodes[0];
687 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
688 
689 		/*
690 		 * There are two cases we can hit here for the previous csum
691 		 * item:
692 		 *
693 		 *		|<- search range ->|
694 		 *	|<- csum item ->|
695 		 *
696 		 * Or
697 		 *				|<- search range ->|
698 		 *	|<- csum item ->|
699 		 *
700 		 * Check if the previous csum item covers the leading part of
701 		 * the search range.  If so we have to start from previous csum
702 		 * item.
703 		 */
704 		if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
705 		    key.type == BTRFS_EXTENT_CSUM_KEY) {
706 			if (bytes_to_csum_size(fs_info, start - key.offset) <
707 			    btrfs_item_size(leaf, path->slots[0] - 1))
708 				path->slots[0]--;
709 		}
710 	}
711 
712 	while (start <= end) {
713 		u64 csum_end;
714 
715 		leaf = path->nodes[0];
716 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
717 			ret = btrfs_next_leaf(root, path);
718 			if (ret < 0)
719 				goto fail;
720 			if (ret > 0)
721 				break;
722 			leaf = path->nodes[0];
723 		}
724 
725 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
726 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
727 		    key.type != BTRFS_EXTENT_CSUM_KEY ||
728 		    key.offset > end)
729 			break;
730 
731 		if (key.offset > start)
732 			start = key.offset;
733 
734 		csum_end = key.offset + csum_size_to_bytes(fs_info,
735 					btrfs_item_size(leaf, path->slots[0]));
736 		if (csum_end <= start) {
737 			path->slots[0]++;
738 			continue;
739 		}
740 
741 		csum_end = min(csum_end, end + 1);
742 		item = btrfs_item_ptr(path->nodes[0], path->slots[0],
743 				      struct btrfs_csum_item);
744 		while (start < csum_end) {
745 			unsigned long offset;
746 			size_t size;
747 			u8 *csum_dest = csum_buf + bytes_to_csum_size(fs_info,
748 						start - orig_start);
749 
750 			size = min_t(size_t, csum_end - start, end + 1 - start);
751 
752 			offset = bytes_to_csum_size(fs_info, start - key.offset);
753 
754 			read_extent_buffer(path->nodes[0], csum_dest,
755 					   ((unsigned long)item) + offset,
756 					   bytes_to_csum_size(fs_info, size));
757 
758 			bitmap_set(csum_bitmap,
759 				(start - orig_start) >> fs_info->sectorsize_bits,
760 				size >> fs_info->sectorsize_bits);
761 
762 			start += size;
763 		}
764 		path->slots[0]++;
765 	}
766 	ret = 0;
767 fail:
768 	btrfs_free_path(path);
769 	return ret;
770 }
771 
772 /*
773  * Calculate checksums of the data contained inside a bio.
774  */
775 blk_status_t btrfs_csum_one_bio(struct btrfs_bio *bbio)
776 {
777 	struct btrfs_inode *inode = bbio->inode;
778 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
779 	SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
780 	struct bio *bio = &bbio->bio;
781 	u64 offset = bbio->file_offset;
782 	struct btrfs_ordered_sum *sums;
783 	struct btrfs_ordered_extent *ordered = NULL;
784 	char *data;
785 	struct bvec_iter iter;
786 	struct bio_vec bvec;
787 	int index;
788 	unsigned int blockcount;
789 	unsigned long total_bytes = 0;
790 	unsigned long this_sum_bytes = 0;
791 	int i;
792 	unsigned nofs_flag;
793 
794 	nofs_flag = memalloc_nofs_save();
795 	sums = kvzalloc(btrfs_ordered_sum_size(fs_info, bio->bi_iter.bi_size),
796 		       GFP_KERNEL);
797 	memalloc_nofs_restore(nofs_flag);
798 
799 	if (!sums)
800 		return BLK_STS_RESOURCE;
801 
802 	sums->len = bio->bi_iter.bi_size;
803 	INIT_LIST_HEAD(&sums->list);
804 
805 	sums->bytenr = bio->bi_iter.bi_sector << 9;
806 	index = 0;
807 
808 	shash->tfm = fs_info->csum_shash;
809 
810 	bio_for_each_segment(bvec, bio, iter) {
811 		if (!ordered) {
812 			ordered = btrfs_lookup_ordered_extent(inode, offset);
813 			/*
814 			 * The bio range is not covered by any ordered extent,
815 			 * must be a code logic error.
816 			 */
817 			if (unlikely(!ordered)) {
818 				WARN(1, KERN_WARNING
819 			"no ordered extent for root %llu ino %llu offset %llu\n",
820 				     inode->root->root_key.objectid,
821 				     btrfs_ino(inode), offset);
822 				kvfree(sums);
823 				return BLK_STS_IOERR;
824 			}
825 		}
826 
827 		blockcount = BTRFS_BYTES_TO_BLKS(fs_info,
828 						 bvec.bv_len + fs_info->sectorsize
829 						 - 1);
830 
831 		for (i = 0; i < blockcount; i++) {
832 			if (!(bio->bi_opf & REQ_BTRFS_ONE_ORDERED) &&
833 			    !in_range(offset, ordered->file_offset,
834 				      ordered->num_bytes)) {
835 				unsigned long bytes_left;
836 
837 				sums->len = this_sum_bytes;
838 				this_sum_bytes = 0;
839 				btrfs_add_ordered_sum(ordered, sums);
840 				btrfs_put_ordered_extent(ordered);
841 
842 				bytes_left = bio->bi_iter.bi_size - total_bytes;
843 
844 				nofs_flag = memalloc_nofs_save();
845 				sums = kvzalloc(btrfs_ordered_sum_size(fs_info,
846 						      bytes_left), GFP_KERNEL);
847 				memalloc_nofs_restore(nofs_flag);
848 				BUG_ON(!sums); /* -ENOMEM */
849 				sums->len = bytes_left;
850 				ordered = btrfs_lookup_ordered_extent(inode,
851 								offset);
852 				ASSERT(ordered); /* Logic error */
853 				sums->bytenr = (bio->bi_iter.bi_sector << 9)
854 					+ total_bytes;
855 				index = 0;
856 			}
857 
858 			data = bvec_kmap_local(&bvec);
859 			crypto_shash_digest(shash,
860 					    data + (i * fs_info->sectorsize),
861 					    fs_info->sectorsize,
862 					    sums->sums + index);
863 			kunmap_local(data);
864 			index += fs_info->csum_size;
865 			offset += fs_info->sectorsize;
866 			this_sum_bytes += fs_info->sectorsize;
867 			total_bytes += fs_info->sectorsize;
868 		}
869 
870 	}
871 	this_sum_bytes = 0;
872 	btrfs_add_ordered_sum(ordered, sums);
873 	btrfs_put_ordered_extent(ordered);
874 	return 0;
875 }
876 
877 /*
878  * Remove one checksum overlapping a range.
879  *
880  * This expects the key to describe the csum pointed to by the path, and it
881  * expects the csum to overlap the range [bytenr, len]
882  *
883  * The csum should not be entirely contained in the range and the range should
884  * not be entirely contained in the csum.
885  *
886  * This calls btrfs_truncate_item with the correct args based on the overlap,
887  * and fixes up the key as required.
888  */
889 static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info,
890 				       struct btrfs_path *path,
891 				       struct btrfs_key *key,
892 				       u64 bytenr, u64 len)
893 {
894 	struct extent_buffer *leaf;
895 	const u32 csum_size = fs_info->csum_size;
896 	u64 csum_end;
897 	u64 end_byte = bytenr + len;
898 	u32 blocksize_bits = fs_info->sectorsize_bits;
899 
900 	leaf = path->nodes[0];
901 	csum_end = btrfs_item_size(leaf, path->slots[0]) / csum_size;
902 	csum_end <<= blocksize_bits;
903 	csum_end += key->offset;
904 
905 	if (key->offset < bytenr && csum_end <= end_byte) {
906 		/*
907 		 *         [ bytenr - len ]
908 		 *         [   ]
909 		 *   [csum     ]
910 		 *   A simple truncate off the end of the item
911 		 */
912 		u32 new_size = (bytenr - key->offset) >> blocksize_bits;
913 		new_size *= csum_size;
914 		btrfs_truncate_item(path, new_size, 1);
915 	} else if (key->offset >= bytenr && csum_end > end_byte &&
916 		   end_byte > key->offset) {
917 		/*
918 		 *         [ bytenr - len ]
919 		 *                 [ ]
920 		 *                 [csum     ]
921 		 * we need to truncate from the beginning of the csum
922 		 */
923 		u32 new_size = (csum_end - end_byte) >> blocksize_bits;
924 		new_size *= csum_size;
925 
926 		btrfs_truncate_item(path, new_size, 0);
927 
928 		key->offset = end_byte;
929 		btrfs_set_item_key_safe(fs_info, path, key);
930 	} else {
931 		BUG();
932 	}
933 }
934 
935 /*
936  * Delete the csum items from the csum tree for a given range of bytes.
937  */
938 int btrfs_del_csums(struct btrfs_trans_handle *trans,
939 		    struct btrfs_root *root, u64 bytenr, u64 len)
940 {
941 	struct btrfs_fs_info *fs_info = trans->fs_info;
942 	struct btrfs_path *path;
943 	struct btrfs_key key;
944 	u64 end_byte = bytenr + len;
945 	u64 csum_end;
946 	struct extent_buffer *leaf;
947 	int ret = 0;
948 	const u32 csum_size = fs_info->csum_size;
949 	u32 blocksize_bits = fs_info->sectorsize_bits;
950 
951 	ASSERT(root->root_key.objectid == BTRFS_CSUM_TREE_OBJECTID ||
952 	       root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
953 
954 	path = btrfs_alloc_path();
955 	if (!path)
956 		return -ENOMEM;
957 
958 	while (1) {
959 		key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
960 		key.offset = end_byte - 1;
961 		key.type = BTRFS_EXTENT_CSUM_KEY;
962 
963 		ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
964 		if (ret > 0) {
965 			ret = 0;
966 			if (path->slots[0] == 0)
967 				break;
968 			path->slots[0]--;
969 		} else if (ret < 0) {
970 			break;
971 		}
972 
973 		leaf = path->nodes[0];
974 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
975 
976 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
977 		    key.type != BTRFS_EXTENT_CSUM_KEY) {
978 			break;
979 		}
980 
981 		if (key.offset >= end_byte)
982 			break;
983 
984 		csum_end = btrfs_item_size(leaf, path->slots[0]) / csum_size;
985 		csum_end <<= blocksize_bits;
986 		csum_end += key.offset;
987 
988 		/* this csum ends before we start, we're done */
989 		if (csum_end <= bytenr)
990 			break;
991 
992 		/* delete the entire item, it is inside our range */
993 		if (key.offset >= bytenr && csum_end <= end_byte) {
994 			int del_nr = 1;
995 
996 			/*
997 			 * Check how many csum items preceding this one in this
998 			 * leaf correspond to our range and then delete them all
999 			 * at once.
1000 			 */
1001 			if (key.offset > bytenr && path->slots[0] > 0) {
1002 				int slot = path->slots[0] - 1;
1003 
1004 				while (slot >= 0) {
1005 					struct btrfs_key pk;
1006 
1007 					btrfs_item_key_to_cpu(leaf, &pk, slot);
1008 					if (pk.offset < bytenr ||
1009 					    pk.type != BTRFS_EXTENT_CSUM_KEY ||
1010 					    pk.objectid !=
1011 					    BTRFS_EXTENT_CSUM_OBJECTID)
1012 						break;
1013 					path->slots[0] = slot;
1014 					del_nr++;
1015 					key.offset = pk.offset;
1016 					slot--;
1017 				}
1018 			}
1019 			ret = btrfs_del_items(trans, root, path,
1020 					      path->slots[0], del_nr);
1021 			if (ret)
1022 				break;
1023 			if (key.offset == bytenr)
1024 				break;
1025 		} else if (key.offset < bytenr && csum_end > end_byte) {
1026 			unsigned long offset;
1027 			unsigned long shift_len;
1028 			unsigned long item_offset;
1029 			/*
1030 			 *        [ bytenr - len ]
1031 			 *     [csum                ]
1032 			 *
1033 			 * Our bytes are in the middle of the csum,
1034 			 * we need to split this item and insert a new one.
1035 			 *
1036 			 * But we can't drop the path because the
1037 			 * csum could change, get removed, extended etc.
1038 			 *
1039 			 * The trick here is the max size of a csum item leaves
1040 			 * enough room in the tree block for a single
1041 			 * item header.  So, we split the item in place,
1042 			 * adding a new header pointing to the existing
1043 			 * bytes.  Then we loop around again and we have
1044 			 * a nicely formed csum item that we can neatly
1045 			 * truncate.
1046 			 */
1047 			offset = (bytenr - key.offset) >> blocksize_bits;
1048 			offset *= csum_size;
1049 
1050 			shift_len = (len >> blocksize_bits) * csum_size;
1051 
1052 			item_offset = btrfs_item_ptr_offset(leaf,
1053 							    path->slots[0]);
1054 
1055 			memzero_extent_buffer(leaf, item_offset + offset,
1056 					     shift_len);
1057 			key.offset = bytenr;
1058 
1059 			/*
1060 			 * btrfs_split_item returns -EAGAIN when the
1061 			 * item changed size or key
1062 			 */
1063 			ret = btrfs_split_item(trans, root, path, &key, offset);
1064 			if (ret && ret != -EAGAIN) {
1065 				btrfs_abort_transaction(trans, ret);
1066 				break;
1067 			}
1068 			ret = 0;
1069 
1070 			key.offset = end_byte - 1;
1071 		} else {
1072 			truncate_one_csum(fs_info, path, &key, bytenr, len);
1073 			if (key.offset < bytenr)
1074 				break;
1075 		}
1076 		btrfs_release_path(path);
1077 	}
1078 	btrfs_free_path(path);
1079 	return ret;
1080 }
1081 
1082 static int find_next_csum_offset(struct btrfs_root *root,
1083 				 struct btrfs_path *path,
1084 				 u64 *next_offset)
1085 {
1086 	const u32 nritems = btrfs_header_nritems(path->nodes[0]);
1087 	struct btrfs_key found_key;
1088 	int slot = path->slots[0] + 1;
1089 	int ret;
1090 
1091 	if (nritems == 0 || slot >= nritems) {
1092 		ret = btrfs_next_leaf(root, path);
1093 		if (ret < 0) {
1094 			return ret;
1095 		} else if (ret > 0) {
1096 			*next_offset = (u64)-1;
1097 			return 0;
1098 		}
1099 		slot = path->slots[0];
1100 	}
1101 
1102 	btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
1103 
1104 	if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1105 	    found_key.type != BTRFS_EXTENT_CSUM_KEY)
1106 		*next_offset = (u64)-1;
1107 	else
1108 		*next_offset = found_key.offset;
1109 
1110 	return 0;
1111 }
1112 
1113 int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
1114 			   struct btrfs_root *root,
1115 			   struct btrfs_ordered_sum *sums)
1116 {
1117 	struct btrfs_fs_info *fs_info = root->fs_info;
1118 	struct btrfs_key file_key;
1119 	struct btrfs_key found_key;
1120 	struct btrfs_path *path;
1121 	struct btrfs_csum_item *item;
1122 	struct btrfs_csum_item *item_end;
1123 	struct extent_buffer *leaf = NULL;
1124 	u64 next_offset;
1125 	u64 total_bytes = 0;
1126 	u64 csum_offset;
1127 	u64 bytenr;
1128 	u32 ins_size;
1129 	int index = 0;
1130 	int found_next;
1131 	int ret;
1132 	const u32 csum_size = fs_info->csum_size;
1133 
1134 	path = btrfs_alloc_path();
1135 	if (!path)
1136 		return -ENOMEM;
1137 again:
1138 	next_offset = (u64)-1;
1139 	found_next = 0;
1140 	bytenr = sums->bytenr + total_bytes;
1141 	file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1142 	file_key.offset = bytenr;
1143 	file_key.type = BTRFS_EXTENT_CSUM_KEY;
1144 
1145 	item = btrfs_lookup_csum(trans, root, path, bytenr, 1);
1146 	if (!IS_ERR(item)) {
1147 		ret = 0;
1148 		leaf = path->nodes[0];
1149 		item_end = btrfs_item_ptr(leaf, path->slots[0],
1150 					  struct btrfs_csum_item);
1151 		item_end = (struct btrfs_csum_item *)((char *)item_end +
1152 			   btrfs_item_size(leaf, path->slots[0]));
1153 		goto found;
1154 	}
1155 	ret = PTR_ERR(item);
1156 	if (ret != -EFBIG && ret != -ENOENT)
1157 		goto out;
1158 
1159 	if (ret == -EFBIG) {
1160 		u32 item_size;
1161 		/* we found one, but it isn't big enough yet */
1162 		leaf = path->nodes[0];
1163 		item_size = btrfs_item_size(leaf, path->slots[0]);
1164 		if ((item_size / csum_size) >=
1165 		    MAX_CSUM_ITEMS(fs_info, csum_size)) {
1166 			/* already at max size, make a new one */
1167 			goto insert;
1168 		}
1169 	} else {
1170 		/* We didn't find a csum item, insert one. */
1171 		ret = find_next_csum_offset(root, path, &next_offset);
1172 		if (ret < 0)
1173 			goto out;
1174 		found_next = 1;
1175 		goto insert;
1176 	}
1177 
1178 	/*
1179 	 * At this point, we know the tree has a checksum item that ends at an
1180 	 * offset matching the start of the checksum range we want to insert.
1181 	 * We try to extend that item as much as possible and then add as many
1182 	 * checksums to it as they fit.
1183 	 *
1184 	 * First check if the leaf has enough free space for at least one
1185 	 * checksum. If it has go directly to the item extension code, otherwise
1186 	 * release the path and do a search for insertion before the extension.
1187 	 */
1188 	if (btrfs_leaf_free_space(leaf) >= csum_size) {
1189 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1190 		csum_offset = (bytenr - found_key.offset) >>
1191 			fs_info->sectorsize_bits;
1192 		goto extend_csum;
1193 	}
1194 
1195 	btrfs_release_path(path);
1196 	path->search_for_extension = 1;
1197 	ret = btrfs_search_slot(trans, root, &file_key, path,
1198 				csum_size, 1);
1199 	path->search_for_extension = 0;
1200 	if (ret < 0)
1201 		goto out;
1202 
1203 	if (ret > 0) {
1204 		if (path->slots[0] == 0)
1205 			goto insert;
1206 		path->slots[0]--;
1207 	}
1208 
1209 	leaf = path->nodes[0];
1210 	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1211 	csum_offset = (bytenr - found_key.offset) >> fs_info->sectorsize_bits;
1212 
1213 	if (found_key.type != BTRFS_EXTENT_CSUM_KEY ||
1214 	    found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1215 	    csum_offset >= MAX_CSUM_ITEMS(fs_info, csum_size)) {
1216 		goto insert;
1217 	}
1218 
1219 extend_csum:
1220 	if (csum_offset == btrfs_item_size(leaf, path->slots[0]) /
1221 	    csum_size) {
1222 		int extend_nr;
1223 		u64 tmp;
1224 		u32 diff;
1225 
1226 		tmp = sums->len - total_bytes;
1227 		tmp >>= fs_info->sectorsize_bits;
1228 		WARN_ON(tmp < 1);
1229 		extend_nr = max_t(int, 1, tmp);
1230 
1231 		/*
1232 		 * A log tree can already have checksum items with a subset of
1233 		 * the checksums we are trying to log. This can happen after
1234 		 * doing a sequence of partial writes into prealloc extents and
1235 		 * fsyncs in between, with a full fsync logging a larger subrange
1236 		 * of an extent for which a previous fast fsync logged a smaller
1237 		 * subrange. And this happens in particular due to merging file
1238 		 * extent items when we complete an ordered extent for a range
1239 		 * covered by a prealloc extent - this is done at
1240 		 * btrfs_mark_extent_written().
1241 		 *
1242 		 * So if we try to extend the previous checksum item, which has
1243 		 * a range that ends at the start of the range we want to insert,
1244 		 * make sure we don't extend beyond the start offset of the next
1245 		 * checksum item. If we are at the last item in the leaf, then
1246 		 * forget the optimization of extending and add a new checksum
1247 		 * item - it is not worth the complexity of releasing the path,
1248 		 * getting the first key for the next leaf, repeat the btree
1249 		 * search, etc, because log trees are temporary anyway and it
1250 		 * would only save a few bytes of leaf space.
1251 		 */
1252 		if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
1253 			if (path->slots[0] + 1 >=
1254 			    btrfs_header_nritems(path->nodes[0])) {
1255 				ret = find_next_csum_offset(root, path, &next_offset);
1256 				if (ret < 0)
1257 					goto out;
1258 				found_next = 1;
1259 				goto insert;
1260 			}
1261 
1262 			ret = find_next_csum_offset(root, path, &next_offset);
1263 			if (ret < 0)
1264 				goto out;
1265 
1266 			tmp = (next_offset - bytenr) >> fs_info->sectorsize_bits;
1267 			if (tmp <= INT_MAX)
1268 				extend_nr = min_t(int, extend_nr, tmp);
1269 		}
1270 
1271 		diff = (csum_offset + extend_nr) * csum_size;
1272 		diff = min(diff,
1273 			   MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size);
1274 
1275 		diff = diff - btrfs_item_size(leaf, path->slots[0]);
1276 		diff = min_t(u32, btrfs_leaf_free_space(leaf), diff);
1277 		diff /= csum_size;
1278 		diff *= csum_size;
1279 
1280 		btrfs_extend_item(path, diff);
1281 		ret = 0;
1282 		goto csum;
1283 	}
1284 
1285 insert:
1286 	btrfs_release_path(path);
1287 	csum_offset = 0;
1288 	if (found_next) {
1289 		u64 tmp;
1290 
1291 		tmp = sums->len - total_bytes;
1292 		tmp >>= fs_info->sectorsize_bits;
1293 		tmp = min(tmp, (next_offset - file_key.offset) >>
1294 					 fs_info->sectorsize_bits);
1295 
1296 		tmp = max_t(u64, 1, tmp);
1297 		tmp = min_t(u64, tmp, MAX_CSUM_ITEMS(fs_info, csum_size));
1298 		ins_size = csum_size * tmp;
1299 	} else {
1300 		ins_size = csum_size;
1301 	}
1302 	ret = btrfs_insert_empty_item(trans, root, path, &file_key,
1303 				      ins_size);
1304 	if (ret < 0)
1305 		goto out;
1306 	if (WARN_ON(ret != 0))
1307 		goto out;
1308 	leaf = path->nodes[0];
1309 csum:
1310 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
1311 	item_end = (struct btrfs_csum_item *)((unsigned char *)item +
1312 				      btrfs_item_size(leaf, path->slots[0]));
1313 	item = (struct btrfs_csum_item *)((unsigned char *)item +
1314 					  csum_offset * csum_size);
1315 found:
1316 	ins_size = (u32)(sums->len - total_bytes) >> fs_info->sectorsize_bits;
1317 	ins_size *= csum_size;
1318 	ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item,
1319 			      ins_size);
1320 	write_extent_buffer(leaf, sums->sums + index, (unsigned long)item,
1321 			    ins_size);
1322 
1323 	index += ins_size;
1324 	ins_size /= csum_size;
1325 	total_bytes += ins_size * fs_info->sectorsize;
1326 
1327 	btrfs_mark_buffer_dirty(path->nodes[0]);
1328 	if (total_bytes < sums->len) {
1329 		btrfs_release_path(path);
1330 		cond_resched();
1331 		goto again;
1332 	}
1333 out:
1334 	btrfs_free_path(path);
1335 	return ret;
1336 }
1337 
1338 void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
1339 				     const struct btrfs_path *path,
1340 				     struct btrfs_file_extent_item *fi,
1341 				     struct extent_map *em)
1342 {
1343 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
1344 	struct btrfs_root *root = inode->root;
1345 	struct extent_buffer *leaf = path->nodes[0];
1346 	const int slot = path->slots[0];
1347 	struct btrfs_key key;
1348 	u64 extent_start, extent_end;
1349 	u64 bytenr;
1350 	u8 type = btrfs_file_extent_type(leaf, fi);
1351 	int compress_type = btrfs_file_extent_compression(leaf, fi);
1352 
1353 	btrfs_item_key_to_cpu(leaf, &key, slot);
1354 	extent_start = key.offset;
1355 	extent_end = btrfs_file_extent_end(path);
1356 	em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
1357 	em->generation = btrfs_file_extent_generation(leaf, fi);
1358 	if (type == BTRFS_FILE_EXTENT_REG ||
1359 	    type == BTRFS_FILE_EXTENT_PREALLOC) {
1360 		em->start = extent_start;
1361 		em->len = extent_end - extent_start;
1362 		em->orig_start = extent_start -
1363 			btrfs_file_extent_offset(leaf, fi);
1364 		em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
1365 		bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1366 		if (bytenr == 0) {
1367 			em->block_start = EXTENT_MAP_HOLE;
1368 			return;
1369 		}
1370 		if (compress_type != BTRFS_COMPRESS_NONE) {
1371 			set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
1372 			em->compress_type = compress_type;
1373 			em->block_start = bytenr;
1374 			em->block_len = em->orig_block_len;
1375 		} else {
1376 			bytenr += btrfs_file_extent_offset(leaf, fi);
1377 			em->block_start = bytenr;
1378 			em->block_len = em->len;
1379 			if (type == BTRFS_FILE_EXTENT_PREALLOC)
1380 				set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
1381 		}
1382 	} else if (type == BTRFS_FILE_EXTENT_INLINE) {
1383 		em->block_start = EXTENT_MAP_INLINE;
1384 		em->start = extent_start;
1385 		em->len = extent_end - extent_start;
1386 		/*
1387 		 * Initialize orig_start and block_len with the same values
1388 		 * as in inode.c:btrfs_get_extent().
1389 		 */
1390 		em->orig_start = EXTENT_MAP_HOLE;
1391 		em->block_len = (u64)-1;
1392 		em->compress_type = compress_type;
1393 		if (compress_type != BTRFS_COMPRESS_NONE)
1394 			set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
1395 	} else {
1396 		btrfs_err(fs_info,
1397 			  "unknown file extent item type %d, inode %llu, offset %llu, "
1398 			  "root %llu", type, btrfs_ino(inode), extent_start,
1399 			  root->root_key.objectid);
1400 	}
1401 }
1402 
1403 /*
1404  * Returns the end offset (non inclusive) of the file extent item the given path
1405  * points to. If it points to an inline extent, the returned offset is rounded
1406  * up to the sector size.
1407  */
1408 u64 btrfs_file_extent_end(const struct btrfs_path *path)
1409 {
1410 	const struct extent_buffer *leaf = path->nodes[0];
1411 	const int slot = path->slots[0];
1412 	struct btrfs_file_extent_item *fi;
1413 	struct btrfs_key key;
1414 	u64 end;
1415 
1416 	btrfs_item_key_to_cpu(leaf, &key, slot);
1417 	ASSERT(key.type == BTRFS_EXTENT_DATA_KEY);
1418 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
1419 
1420 	if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
1421 		end = btrfs_file_extent_ram_bytes(leaf, fi);
1422 		end = ALIGN(key.offset + end, leaf->fs_info->sectorsize);
1423 	} else {
1424 		end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
1425 	}
1426 
1427 	return end;
1428 }
1429