xref: /openbmc/linux/fs/btrfs/file-item.c (revision e2eb0248)
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  * Lookup the checksum for the read bio in csum tree.
340  *
341  * Return: BLK_STS_RESOURCE if allocating memory fails, BLK_STS_OK otherwise.
342  */
343 blk_status_t btrfs_lookup_bio_sums(struct btrfs_bio *bbio)
344 {
345 	struct btrfs_inode *inode = bbio->inode;
346 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
347 	struct bio *bio = &bbio->bio;
348 	struct btrfs_path *path;
349 	const u32 sectorsize = fs_info->sectorsize;
350 	const u32 csum_size = fs_info->csum_size;
351 	u32 orig_len = bio->bi_iter.bi_size;
352 	u64 orig_disk_bytenr = bio->bi_iter.bi_sector << SECTOR_SHIFT;
353 	const unsigned int nblocks = orig_len >> fs_info->sectorsize_bits;
354 	blk_status_t ret = BLK_STS_OK;
355 	u32 bio_offset = 0;
356 
357 	if ((inode->flags & BTRFS_INODE_NODATASUM) ||
358 	    test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state))
359 		return BLK_STS_OK;
360 
361 	/*
362 	 * This function is only called for read bio.
363 	 *
364 	 * This means two things:
365 	 * - All our csums should only be in csum tree
366 	 *   No ordered extents csums, as ordered extents are only for write
367 	 *   path.
368 	 * - No need to bother any other info from bvec
369 	 *   Since we're looking up csums, the only important info is the
370 	 *   disk_bytenr and the length, which can be extracted from bi_iter
371 	 *   directly.
372 	 */
373 	ASSERT(bio_op(bio) == REQ_OP_READ);
374 	path = btrfs_alloc_path();
375 	if (!path)
376 		return BLK_STS_RESOURCE;
377 
378 	if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) {
379 		bbio->csum = kmalloc_array(nblocks, csum_size, GFP_NOFS);
380 		if (!bbio->csum) {
381 			btrfs_free_path(path);
382 			return BLK_STS_RESOURCE;
383 		}
384 	} else {
385 		bbio->csum = bbio->csum_inline;
386 	}
387 
388 	/*
389 	 * If requested number of sectors is larger than one leaf can contain,
390 	 * kick the readahead for csum tree.
391 	 */
392 	if (nblocks > fs_info->csums_per_leaf)
393 		path->reada = READA_FORWARD;
394 
395 	/*
396 	 * the free space stuff is only read when it hasn't been
397 	 * updated in the current transaction.  So, we can safely
398 	 * read from the commit root and sidestep a nasty deadlock
399 	 * between reading the free space cache and updating the csum tree.
400 	 */
401 	if (btrfs_is_free_space_inode(inode)) {
402 		path->search_commit_root = 1;
403 		path->skip_locking = 1;
404 	}
405 
406 	while (bio_offset < orig_len) {
407 		int count;
408 		u64 cur_disk_bytenr = orig_disk_bytenr + bio_offset;
409 		u8 *csum_dst = bbio->csum +
410 			(bio_offset >> fs_info->sectorsize_bits) * csum_size;
411 
412 		count = search_csum_tree(fs_info, path, cur_disk_bytenr,
413 					 orig_len - bio_offset, csum_dst);
414 		if (count < 0) {
415 			ret = errno_to_blk_status(count);
416 			if (bbio->csum != bbio->csum_inline)
417 				kfree(bbio->csum);
418 			bbio->csum = NULL;
419 			break;
420 		}
421 
422 		/*
423 		 * We didn't find a csum for this range.  We need to make sure
424 		 * we complain loudly about this, because we are not NODATASUM.
425 		 *
426 		 * However for the DATA_RELOC inode we could potentially be
427 		 * relocating data extents for a NODATASUM inode, so the inode
428 		 * itself won't be marked with NODATASUM, but the extent we're
429 		 * copying is in fact NODATASUM.  If we don't find a csum we
430 		 * assume this is the case.
431 		 */
432 		if (count == 0) {
433 			memset(csum_dst, 0, csum_size);
434 			count = 1;
435 
436 			if (inode->root->root_key.objectid ==
437 			    BTRFS_DATA_RELOC_TREE_OBJECTID) {
438 				u64 file_offset = bbio->file_offset + bio_offset;
439 
440 				set_extent_bits(&inode->io_tree, file_offset,
441 						file_offset + sectorsize - 1,
442 						EXTENT_NODATASUM);
443 			} else {
444 				btrfs_warn_rl(fs_info,
445 			"csum hole found for disk bytenr range [%llu, %llu)",
446 				cur_disk_bytenr, cur_disk_bytenr + sectorsize);
447 			}
448 		}
449 		bio_offset += count * sectorsize;
450 	}
451 
452 	btrfs_free_path(path);
453 	return ret;
454 }
455 
456 int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end,
457 			    struct list_head *list, int search_commit,
458 			    bool nowait)
459 {
460 	struct btrfs_fs_info *fs_info = root->fs_info;
461 	struct btrfs_key key;
462 	struct btrfs_path *path;
463 	struct extent_buffer *leaf;
464 	struct btrfs_ordered_sum *sums;
465 	struct btrfs_csum_item *item;
466 	LIST_HEAD(tmplist);
467 	int ret;
468 
469 	ASSERT(IS_ALIGNED(start, fs_info->sectorsize) &&
470 	       IS_ALIGNED(end + 1, fs_info->sectorsize));
471 
472 	path = btrfs_alloc_path();
473 	if (!path)
474 		return -ENOMEM;
475 
476 	path->nowait = nowait;
477 	if (search_commit) {
478 		path->skip_locking = 1;
479 		path->reada = READA_FORWARD;
480 		path->search_commit_root = 1;
481 	}
482 
483 	key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
484 	key.offset = start;
485 	key.type = BTRFS_EXTENT_CSUM_KEY;
486 
487 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
488 	if (ret < 0)
489 		goto fail;
490 	if (ret > 0 && path->slots[0] > 0) {
491 		leaf = path->nodes[0];
492 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
493 
494 		/*
495 		 * There are two cases we can hit here for the previous csum
496 		 * item:
497 		 *
498 		 *		|<- search range ->|
499 		 *	|<- csum item ->|
500 		 *
501 		 * Or
502 		 *				|<- search range ->|
503 		 *	|<- csum item ->|
504 		 *
505 		 * Check if the previous csum item covers the leading part of
506 		 * the search range.  If so we have to start from previous csum
507 		 * item.
508 		 */
509 		if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
510 		    key.type == BTRFS_EXTENT_CSUM_KEY) {
511 			if (bytes_to_csum_size(fs_info, start - key.offset) <
512 			    btrfs_item_size(leaf, path->slots[0] - 1))
513 				path->slots[0]--;
514 		}
515 	}
516 
517 	while (start <= end) {
518 		u64 csum_end;
519 
520 		leaf = path->nodes[0];
521 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
522 			ret = btrfs_next_leaf(root, path);
523 			if (ret < 0)
524 				goto fail;
525 			if (ret > 0)
526 				break;
527 			leaf = path->nodes[0];
528 		}
529 
530 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
531 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
532 		    key.type != BTRFS_EXTENT_CSUM_KEY ||
533 		    key.offset > end)
534 			break;
535 
536 		if (key.offset > start)
537 			start = key.offset;
538 
539 		csum_end = key.offset + csum_size_to_bytes(fs_info,
540 					btrfs_item_size(leaf, path->slots[0]));
541 		if (csum_end <= start) {
542 			path->slots[0]++;
543 			continue;
544 		}
545 
546 		csum_end = min(csum_end, end + 1);
547 		item = btrfs_item_ptr(path->nodes[0], path->slots[0],
548 				      struct btrfs_csum_item);
549 		while (start < csum_end) {
550 			unsigned long offset;
551 			size_t size;
552 
553 			size = min_t(size_t, csum_end - start,
554 				     max_ordered_sum_bytes(fs_info));
555 			sums = kzalloc(btrfs_ordered_sum_size(fs_info, size),
556 				       GFP_NOFS);
557 			if (!sums) {
558 				ret = -ENOMEM;
559 				goto fail;
560 			}
561 
562 			sums->bytenr = start;
563 			sums->len = (int)size;
564 
565 			offset = bytes_to_csum_size(fs_info, start - key.offset);
566 
567 			read_extent_buffer(path->nodes[0],
568 					   sums->sums,
569 					   ((unsigned long)item) + offset,
570 					   bytes_to_csum_size(fs_info, size));
571 
572 			start += size;
573 			list_add_tail(&sums->list, &tmplist);
574 		}
575 		path->slots[0]++;
576 	}
577 	ret = 0;
578 fail:
579 	while (ret < 0 && !list_empty(&tmplist)) {
580 		sums = list_entry(tmplist.next, struct btrfs_ordered_sum, list);
581 		list_del(&sums->list);
582 		kfree(sums);
583 	}
584 	list_splice_tail(&tmplist, list);
585 
586 	btrfs_free_path(path);
587 	return ret;
588 }
589 
590 /*
591  * Do the same work as btrfs_lookup_csums_list(), the difference is in how
592  * we return the result.
593  *
594  * This version will set the corresponding bits in @csum_bitmap to represent
595  * that there is a csum found.
596  * Each bit represents a sector. Thus caller should ensure @csum_buf passed
597  * in is large enough to contain all csums.
598  */
599 int btrfs_lookup_csums_bitmap(struct btrfs_root *root, u64 start, u64 end,
600 			      u8 *csum_buf, unsigned long *csum_bitmap)
601 {
602 	struct btrfs_fs_info *fs_info = root->fs_info;
603 	struct btrfs_key key;
604 	struct btrfs_path *path;
605 	struct extent_buffer *leaf;
606 	struct btrfs_csum_item *item;
607 	const u64 orig_start = start;
608 	int ret;
609 
610 	ASSERT(IS_ALIGNED(start, fs_info->sectorsize) &&
611 	       IS_ALIGNED(end + 1, fs_info->sectorsize));
612 
613 	path = btrfs_alloc_path();
614 	if (!path)
615 		return -ENOMEM;
616 
617 	key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
618 	key.type = BTRFS_EXTENT_CSUM_KEY;
619 	key.offset = start;
620 
621 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
622 	if (ret < 0)
623 		goto fail;
624 	if (ret > 0 && path->slots[0] > 0) {
625 		leaf = path->nodes[0];
626 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
627 
628 		/*
629 		 * There are two cases we can hit here for the previous csum
630 		 * item:
631 		 *
632 		 *		|<- search range ->|
633 		 *	|<- csum item ->|
634 		 *
635 		 * Or
636 		 *				|<- search range ->|
637 		 *	|<- csum item ->|
638 		 *
639 		 * Check if the previous csum item covers the leading part of
640 		 * the search range.  If so we have to start from previous csum
641 		 * item.
642 		 */
643 		if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
644 		    key.type == BTRFS_EXTENT_CSUM_KEY) {
645 			if (bytes_to_csum_size(fs_info, start - key.offset) <
646 			    btrfs_item_size(leaf, path->slots[0] - 1))
647 				path->slots[0]--;
648 		}
649 	}
650 
651 	while (start <= end) {
652 		u64 csum_end;
653 
654 		leaf = path->nodes[0];
655 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
656 			ret = btrfs_next_leaf(root, path);
657 			if (ret < 0)
658 				goto fail;
659 			if (ret > 0)
660 				break;
661 			leaf = path->nodes[0];
662 		}
663 
664 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
665 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
666 		    key.type != BTRFS_EXTENT_CSUM_KEY ||
667 		    key.offset > end)
668 			break;
669 
670 		if (key.offset > start)
671 			start = key.offset;
672 
673 		csum_end = key.offset + csum_size_to_bytes(fs_info,
674 					btrfs_item_size(leaf, path->slots[0]));
675 		if (csum_end <= start) {
676 			path->slots[0]++;
677 			continue;
678 		}
679 
680 		csum_end = min(csum_end, end + 1);
681 		item = btrfs_item_ptr(path->nodes[0], path->slots[0],
682 				      struct btrfs_csum_item);
683 		while (start < csum_end) {
684 			unsigned long offset;
685 			size_t size;
686 			u8 *csum_dest = csum_buf + bytes_to_csum_size(fs_info,
687 						start - orig_start);
688 
689 			size = min_t(size_t, csum_end - start, end + 1 - start);
690 
691 			offset = bytes_to_csum_size(fs_info, start - key.offset);
692 
693 			read_extent_buffer(path->nodes[0], csum_dest,
694 					   ((unsigned long)item) + offset,
695 					   bytes_to_csum_size(fs_info, size));
696 
697 			bitmap_set(csum_bitmap,
698 				(start - orig_start) >> fs_info->sectorsize_bits,
699 				size >> fs_info->sectorsize_bits);
700 
701 			start += size;
702 		}
703 		path->slots[0]++;
704 	}
705 	ret = 0;
706 fail:
707 	btrfs_free_path(path);
708 	return ret;
709 }
710 
711 /*
712  * Calculate checksums of the data contained inside a bio.
713  */
714 blk_status_t btrfs_csum_one_bio(struct btrfs_bio *bbio)
715 {
716 	struct btrfs_inode *inode = bbio->inode;
717 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
718 	SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
719 	struct bio *bio = &bbio->bio;
720 	u64 offset = bbio->file_offset;
721 	struct btrfs_ordered_sum *sums;
722 	struct btrfs_ordered_extent *ordered = NULL;
723 	char *data;
724 	struct bvec_iter iter;
725 	struct bio_vec bvec;
726 	int index;
727 	unsigned int blockcount;
728 	unsigned long total_bytes = 0;
729 	unsigned long this_sum_bytes = 0;
730 	int i;
731 	unsigned nofs_flag;
732 
733 	nofs_flag = memalloc_nofs_save();
734 	sums = kvzalloc(btrfs_ordered_sum_size(fs_info, bio->bi_iter.bi_size),
735 		       GFP_KERNEL);
736 	memalloc_nofs_restore(nofs_flag);
737 
738 	if (!sums)
739 		return BLK_STS_RESOURCE;
740 
741 	sums->len = bio->bi_iter.bi_size;
742 	INIT_LIST_HEAD(&sums->list);
743 
744 	sums->bytenr = bio->bi_iter.bi_sector << 9;
745 	index = 0;
746 
747 	shash->tfm = fs_info->csum_shash;
748 
749 	bio_for_each_segment(bvec, bio, iter) {
750 		if (!ordered) {
751 			ordered = btrfs_lookup_ordered_extent(inode, offset);
752 			/*
753 			 * The bio range is not covered by any ordered extent,
754 			 * must be a code logic error.
755 			 */
756 			if (unlikely(!ordered)) {
757 				WARN(1, KERN_WARNING
758 			"no ordered extent for root %llu ino %llu offset %llu\n",
759 				     inode->root->root_key.objectid,
760 				     btrfs_ino(inode), offset);
761 				kvfree(sums);
762 				return BLK_STS_IOERR;
763 			}
764 		}
765 
766 		blockcount = BTRFS_BYTES_TO_BLKS(fs_info,
767 						 bvec.bv_len + fs_info->sectorsize
768 						 - 1);
769 
770 		for (i = 0; i < blockcount; i++) {
771 			if (!(bio->bi_opf & REQ_BTRFS_ONE_ORDERED) &&
772 			    !in_range(offset, ordered->file_offset,
773 				      ordered->num_bytes)) {
774 				unsigned long bytes_left;
775 
776 				sums->len = this_sum_bytes;
777 				this_sum_bytes = 0;
778 				btrfs_add_ordered_sum(ordered, sums);
779 				btrfs_put_ordered_extent(ordered);
780 
781 				bytes_left = bio->bi_iter.bi_size - total_bytes;
782 
783 				nofs_flag = memalloc_nofs_save();
784 				sums = kvzalloc(btrfs_ordered_sum_size(fs_info,
785 						      bytes_left), GFP_KERNEL);
786 				memalloc_nofs_restore(nofs_flag);
787 				BUG_ON(!sums); /* -ENOMEM */
788 				sums->len = bytes_left;
789 				ordered = btrfs_lookup_ordered_extent(inode,
790 								offset);
791 				ASSERT(ordered); /* Logic error */
792 				sums->bytenr = (bio->bi_iter.bi_sector << 9)
793 					+ total_bytes;
794 				index = 0;
795 			}
796 
797 			data = bvec_kmap_local(&bvec);
798 			crypto_shash_digest(shash,
799 					    data + (i * fs_info->sectorsize),
800 					    fs_info->sectorsize,
801 					    sums->sums + index);
802 			kunmap_local(data);
803 			index += fs_info->csum_size;
804 			offset += fs_info->sectorsize;
805 			this_sum_bytes += fs_info->sectorsize;
806 			total_bytes += fs_info->sectorsize;
807 		}
808 
809 	}
810 	this_sum_bytes = 0;
811 	btrfs_add_ordered_sum(ordered, sums);
812 	btrfs_put_ordered_extent(ordered);
813 	return 0;
814 }
815 
816 /*
817  * Remove one checksum overlapping a range.
818  *
819  * This expects the key to describe the csum pointed to by the path, and it
820  * expects the csum to overlap the range [bytenr, len]
821  *
822  * The csum should not be entirely contained in the range and the range should
823  * not be entirely contained in the csum.
824  *
825  * This calls btrfs_truncate_item with the correct args based on the overlap,
826  * and fixes up the key as required.
827  */
828 static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info,
829 				       struct btrfs_path *path,
830 				       struct btrfs_key *key,
831 				       u64 bytenr, u64 len)
832 {
833 	struct extent_buffer *leaf;
834 	const u32 csum_size = fs_info->csum_size;
835 	u64 csum_end;
836 	u64 end_byte = bytenr + len;
837 	u32 blocksize_bits = fs_info->sectorsize_bits;
838 
839 	leaf = path->nodes[0];
840 	csum_end = btrfs_item_size(leaf, path->slots[0]) / csum_size;
841 	csum_end <<= blocksize_bits;
842 	csum_end += key->offset;
843 
844 	if (key->offset < bytenr && csum_end <= end_byte) {
845 		/*
846 		 *         [ bytenr - len ]
847 		 *         [   ]
848 		 *   [csum     ]
849 		 *   A simple truncate off the end of the item
850 		 */
851 		u32 new_size = (bytenr - key->offset) >> blocksize_bits;
852 		new_size *= csum_size;
853 		btrfs_truncate_item(path, new_size, 1);
854 	} else if (key->offset >= bytenr && csum_end > end_byte &&
855 		   end_byte > key->offset) {
856 		/*
857 		 *         [ bytenr - len ]
858 		 *                 [ ]
859 		 *                 [csum     ]
860 		 * we need to truncate from the beginning of the csum
861 		 */
862 		u32 new_size = (csum_end - end_byte) >> blocksize_bits;
863 		new_size *= csum_size;
864 
865 		btrfs_truncate_item(path, new_size, 0);
866 
867 		key->offset = end_byte;
868 		btrfs_set_item_key_safe(fs_info, path, key);
869 	} else {
870 		BUG();
871 	}
872 }
873 
874 /*
875  * Delete the csum items from the csum tree for a given range of bytes.
876  */
877 int btrfs_del_csums(struct btrfs_trans_handle *trans,
878 		    struct btrfs_root *root, u64 bytenr, u64 len)
879 {
880 	struct btrfs_fs_info *fs_info = trans->fs_info;
881 	struct btrfs_path *path;
882 	struct btrfs_key key;
883 	u64 end_byte = bytenr + len;
884 	u64 csum_end;
885 	struct extent_buffer *leaf;
886 	int ret = 0;
887 	const u32 csum_size = fs_info->csum_size;
888 	u32 blocksize_bits = fs_info->sectorsize_bits;
889 
890 	ASSERT(root->root_key.objectid == BTRFS_CSUM_TREE_OBJECTID ||
891 	       root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
892 
893 	path = btrfs_alloc_path();
894 	if (!path)
895 		return -ENOMEM;
896 
897 	while (1) {
898 		key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
899 		key.offset = end_byte - 1;
900 		key.type = BTRFS_EXTENT_CSUM_KEY;
901 
902 		ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
903 		if (ret > 0) {
904 			ret = 0;
905 			if (path->slots[0] == 0)
906 				break;
907 			path->slots[0]--;
908 		} else if (ret < 0) {
909 			break;
910 		}
911 
912 		leaf = path->nodes[0];
913 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
914 
915 		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
916 		    key.type != BTRFS_EXTENT_CSUM_KEY) {
917 			break;
918 		}
919 
920 		if (key.offset >= end_byte)
921 			break;
922 
923 		csum_end = btrfs_item_size(leaf, path->slots[0]) / csum_size;
924 		csum_end <<= blocksize_bits;
925 		csum_end += key.offset;
926 
927 		/* this csum ends before we start, we're done */
928 		if (csum_end <= bytenr)
929 			break;
930 
931 		/* delete the entire item, it is inside our range */
932 		if (key.offset >= bytenr && csum_end <= end_byte) {
933 			int del_nr = 1;
934 
935 			/*
936 			 * Check how many csum items preceding this one in this
937 			 * leaf correspond to our range and then delete them all
938 			 * at once.
939 			 */
940 			if (key.offset > bytenr && path->slots[0] > 0) {
941 				int slot = path->slots[0] - 1;
942 
943 				while (slot >= 0) {
944 					struct btrfs_key pk;
945 
946 					btrfs_item_key_to_cpu(leaf, &pk, slot);
947 					if (pk.offset < bytenr ||
948 					    pk.type != BTRFS_EXTENT_CSUM_KEY ||
949 					    pk.objectid !=
950 					    BTRFS_EXTENT_CSUM_OBJECTID)
951 						break;
952 					path->slots[0] = slot;
953 					del_nr++;
954 					key.offset = pk.offset;
955 					slot--;
956 				}
957 			}
958 			ret = btrfs_del_items(trans, root, path,
959 					      path->slots[0], del_nr);
960 			if (ret)
961 				break;
962 			if (key.offset == bytenr)
963 				break;
964 		} else if (key.offset < bytenr && csum_end > end_byte) {
965 			unsigned long offset;
966 			unsigned long shift_len;
967 			unsigned long item_offset;
968 			/*
969 			 *        [ bytenr - len ]
970 			 *     [csum                ]
971 			 *
972 			 * Our bytes are in the middle of the csum,
973 			 * we need to split this item and insert a new one.
974 			 *
975 			 * But we can't drop the path because the
976 			 * csum could change, get removed, extended etc.
977 			 *
978 			 * The trick here is the max size of a csum item leaves
979 			 * enough room in the tree block for a single
980 			 * item header.  So, we split the item in place,
981 			 * adding a new header pointing to the existing
982 			 * bytes.  Then we loop around again and we have
983 			 * a nicely formed csum item that we can neatly
984 			 * truncate.
985 			 */
986 			offset = (bytenr - key.offset) >> blocksize_bits;
987 			offset *= csum_size;
988 
989 			shift_len = (len >> blocksize_bits) * csum_size;
990 
991 			item_offset = btrfs_item_ptr_offset(leaf,
992 							    path->slots[0]);
993 
994 			memzero_extent_buffer(leaf, item_offset + offset,
995 					     shift_len);
996 			key.offset = bytenr;
997 
998 			/*
999 			 * btrfs_split_item returns -EAGAIN when the
1000 			 * item changed size or key
1001 			 */
1002 			ret = btrfs_split_item(trans, root, path, &key, offset);
1003 			if (ret && ret != -EAGAIN) {
1004 				btrfs_abort_transaction(trans, ret);
1005 				break;
1006 			}
1007 			ret = 0;
1008 
1009 			key.offset = end_byte - 1;
1010 		} else {
1011 			truncate_one_csum(fs_info, path, &key, bytenr, len);
1012 			if (key.offset < bytenr)
1013 				break;
1014 		}
1015 		btrfs_release_path(path);
1016 	}
1017 	btrfs_free_path(path);
1018 	return ret;
1019 }
1020 
1021 static int find_next_csum_offset(struct btrfs_root *root,
1022 				 struct btrfs_path *path,
1023 				 u64 *next_offset)
1024 {
1025 	const u32 nritems = btrfs_header_nritems(path->nodes[0]);
1026 	struct btrfs_key found_key;
1027 	int slot = path->slots[0] + 1;
1028 	int ret;
1029 
1030 	if (nritems == 0 || slot >= nritems) {
1031 		ret = btrfs_next_leaf(root, path);
1032 		if (ret < 0) {
1033 			return ret;
1034 		} else if (ret > 0) {
1035 			*next_offset = (u64)-1;
1036 			return 0;
1037 		}
1038 		slot = path->slots[0];
1039 	}
1040 
1041 	btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
1042 
1043 	if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1044 	    found_key.type != BTRFS_EXTENT_CSUM_KEY)
1045 		*next_offset = (u64)-1;
1046 	else
1047 		*next_offset = found_key.offset;
1048 
1049 	return 0;
1050 }
1051 
1052 int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
1053 			   struct btrfs_root *root,
1054 			   struct btrfs_ordered_sum *sums)
1055 {
1056 	struct btrfs_fs_info *fs_info = root->fs_info;
1057 	struct btrfs_key file_key;
1058 	struct btrfs_key found_key;
1059 	struct btrfs_path *path;
1060 	struct btrfs_csum_item *item;
1061 	struct btrfs_csum_item *item_end;
1062 	struct extent_buffer *leaf = NULL;
1063 	u64 next_offset;
1064 	u64 total_bytes = 0;
1065 	u64 csum_offset;
1066 	u64 bytenr;
1067 	u32 ins_size;
1068 	int index = 0;
1069 	int found_next;
1070 	int ret;
1071 	const u32 csum_size = fs_info->csum_size;
1072 
1073 	path = btrfs_alloc_path();
1074 	if (!path)
1075 		return -ENOMEM;
1076 again:
1077 	next_offset = (u64)-1;
1078 	found_next = 0;
1079 	bytenr = sums->bytenr + total_bytes;
1080 	file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1081 	file_key.offset = bytenr;
1082 	file_key.type = BTRFS_EXTENT_CSUM_KEY;
1083 
1084 	item = btrfs_lookup_csum(trans, root, path, bytenr, 1);
1085 	if (!IS_ERR(item)) {
1086 		ret = 0;
1087 		leaf = path->nodes[0];
1088 		item_end = btrfs_item_ptr(leaf, path->slots[0],
1089 					  struct btrfs_csum_item);
1090 		item_end = (struct btrfs_csum_item *)((char *)item_end +
1091 			   btrfs_item_size(leaf, path->slots[0]));
1092 		goto found;
1093 	}
1094 	ret = PTR_ERR(item);
1095 	if (ret != -EFBIG && ret != -ENOENT)
1096 		goto out;
1097 
1098 	if (ret == -EFBIG) {
1099 		u32 item_size;
1100 		/* we found one, but it isn't big enough yet */
1101 		leaf = path->nodes[0];
1102 		item_size = btrfs_item_size(leaf, path->slots[0]);
1103 		if ((item_size / csum_size) >=
1104 		    MAX_CSUM_ITEMS(fs_info, csum_size)) {
1105 			/* already at max size, make a new one */
1106 			goto insert;
1107 		}
1108 	} else {
1109 		/* We didn't find a csum item, insert one. */
1110 		ret = find_next_csum_offset(root, path, &next_offset);
1111 		if (ret < 0)
1112 			goto out;
1113 		found_next = 1;
1114 		goto insert;
1115 	}
1116 
1117 	/*
1118 	 * At this point, we know the tree has a checksum item that ends at an
1119 	 * offset matching the start of the checksum range we want to insert.
1120 	 * We try to extend that item as much as possible and then add as many
1121 	 * checksums to it as they fit.
1122 	 *
1123 	 * First check if the leaf has enough free space for at least one
1124 	 * checksum. If it has go directly to the item extension code, otherwise
1125 	 * release the path and do a search for insertion before the extension.
1126 	 */
1127 	if (btrfs_leaf_free_space(leaf) >= csum_size) {
1128 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1129 		csum_offset = (bytenr - found_key.offset) >>
1130 			fs_info->sectorsize_bits;
1131 		goto extend_csum;
1132 	}
1133 
1134 	btrfs_release_path(path);
1135 	path->search_for_extension = 1;
1136 	ret = btrfs_search_slot(trans, root, &file_key, path,
1137 				csum_size, 1);
1138 	path->search_for_extension = 0;
1139 	if (ret < 0)
1140 		goto out;
1141 
1142 	if (ret > 0) {
1143 		if (path->slots[0] == 0)
1144 			goto insert;
1145 		path->slots[0]--;
1146 	}
1147 
1148 	leaf = path->nodes[0];
1149 	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1150 	csum_offset = (bytenr - found_key.offset) >> fs_info->sectorsize_bits;
1151 
1152 	if (found_key.type != BTRFS_EXTENT_CSUM_KEY ||
1153 	    found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1154 	    csum_offset >= MAX_CSUM_ITEMS(fs_info, csum_size)) {
1155 		goto insert;
1156 	}
1157 
1158 extend_csum:
1159 	if (csum_offset == btrfs_item_size(leaf, path->slots[0]) /
1160 	    csum_size) {
1161 		int extend_nr;
1162 		u64 tmp;
1163 		u32 diff;
1164 
1165 		tmp = sums->len - total_bytes;
1166 		tmp >>= fs_info->sectorsize_bits;
1167 		WARN_ON(tmp < 1);
1168 		extend_nr = max_t(int, 1, tmp);
1169 
1170 		/*
1171 		 * A log tree can already have checksum items with a subset of
1172 		 * the checksums we are trying to log. This can happen after
1173 		 * doing a sequence of partial writes into prealloc extents and
1174 		 * fsyncs in between, with a full fsync logging a larger subrange
1175 		 * of an extent for which a previous fast fsync logged a smaller
1176 		 * subrange. And this happens in particular due to merging file
1177 		 * extent items when we complete an ordered extent for a range
1178 		 * covered by a prealloc extent - this is done at
1179 		 * btrfs_mark_extent_written().
1180 		 *
1181 		 * So if we try to extend the previous checksum item, which has
1182 		 * a range that ends at the start of the range we want to insert,
1183 		 * make sure we don't extend beyond the start offset of the next
1184 		 * checksum item. If we are at the last item in the leaf, then
1185 		 * forget the optimization of extending and add a new checksum
1186 		 * item - it is not worth the complexity of releasing the path,
1187 		 * getting the first key for the next leaf, repeat the btree
1188 		 * search, etc, because log trees are temporary anyway and it
1189 		 * would only save a few bytes of leaf space.
1190 		 */
1191 		if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
1192 			if (path->slots[0] + 1 >=
1193 			    btrfs_header_nritems(path->nodes[0])) {
1194 				ret = find_next_csum_offset(root, path, &next_offset);
1195 				if (ret < 0)
1196 					goto out;
1197 				found_next = 1;
1198 				goto insert;
1199 			}
1200 
1201 			ret = find_next_csum_offset(root, path, &next_offset);
1202 			if (ret < 0)
1203 				goto out;
1204 
1205 			tmp = (next_offset - bytenr) >> fs_info->sectorsize_bits;
1206 			if (tmp <= INT_MAX)
1207 				extend_nr = min_t(int, extend_nr, tmp);
1208 		}
1209 
1210 		diff = (csum_offset + extend_nr) * csum_size;
1211 		diff = min(diff,
1212 			   MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size);
1213 
1214 		diff = diff - btrfs_item_size(leaf, path->slots[0]);
1215 		diff = min_t(u32, btrfs_leaf_free_space(leaf), diff);
1216 		diff /= csum_size;
1217 		diff *= csum_size;
1218 
1219 		btrfs_extend_item(path, diff);
1220 		ret = 0;
1221 		goto csum;
1222 	}
1223 
1224 insert:
1225 	btrfs_release_path(path);
1226 	csum_offset = 0;
1227 	if (found_next) {
1228 		u64 tmp;
1229 
1230 		tmp = sums->len - total_bytes;
1231 		tmp >>= fs_info->sectorsize_bits;
1232 		tmp = min(tmp, (next_offset - file_key.offset) >>
1233 					 fs_info->sectorsize_bits);
1234 
1235 		tmp = max_t(u64, 1, tmp);
1236 		tmp = min_t(u64, tmp, MAX_CSUM_ITEMS(fs_info, csum_size));
1237 		ins_size = csum_size * tmp;
1238 	} else {
1239 		ins_size = csum_size;
1240 	}
1241 	ret = btrfs_insert_empty_item(trans, root, path, &file_key,
1242 				      ins_size);
1243 	if (ret < 0)
1244 		goto out;
1245 	if (WARN_ON(ret != 0))
1246 		goto out;
1247 	leaf = path->nodes[0];
1248 csum:
1249 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
1250 	item_end = (struct btrfs_csum_item *)((unsigned char *)item +
1251 				      btrfs_item_size(leaf, path->slots[0]));
1252 	item = (struct btrfs_csum_item *)((unsigned char *)item +
1253 					  csum_offset * csum_size);
1254 found:
1255 	ins_size = (u32)(sums->len - total_bytes) >> fs_info->sectorsize_bits;
1256 	ins_size *= csum_size;
1257 	ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item,
1258 			      ins_size);
1259 	write_extent_buffer(leaf, sums->sums + index, (unsigned long)item,
1260 			    ins_size);
1261 
1262 	index += ins_size;
1263 	ins_size /= csum_size;
1264 	total_bytes += ins_size * fs_info->sectorsize;
1265 
1266 	btrfs_mark_buffer_dirty(path->nodes[0]);
1267 	if (total_bytes < sums->len) {
1268 		btrfs_release_path(path);
1269 		cond_resched();
1270 		goto again;
1271 	}
1272 out:
1273 	btrfs_free_path(path);
1274 	return ret;
1275 }
1276 
1277 void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
1278 				     const struct btrfs_path *path,
1279 				     struct btrfs_file_extent_item *fi,
1280 				     struct extent_map *em)
1281 {
1282 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
1283 	struct btrfs_root *root = inode->root;
1284 	struct extent_buffer *leaf = path->nodes[0];
1285 	const int slot = path->slots[0];
1286 	struct btrfs_key key;
1287 	u64 extent_start, extent_end;
1288 	u64 bytenr;
1289 	u8 type = btrfs_file_extent_type(leaf, fi);
1290 	int compress_type = btrfs_file_extent_compression(leaf, fi);
1291 
1292 	btrfs_item_key_to_cpu(leaf, &key, slot);
1293 	extent_start = key.offset;
1294 	extent_end = btrfs_file_extent_end(path);
1295 	em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
1296 	em->generation = btrfs_file_extent_generation(leaf, fi);
1297 	if (type == BTRFS_FILE_EXTENT_REG ||
1298 	    type == BTRFS_FILE_EXTENT_PREALLOC) {
1299 		em->start = extent_start;
1300 		em->len = extent_end - extent_start;
1301 		em->orig_start = extent_start -
1302 			btrfs_file_extent_offset(leaf, fi);
1303 		em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
1304 		bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1305 		if (bytenr == 0) {
1306 			em->block_start = EXTENT_MAP_HOLE;
1307 			return;
1308 		}
1309 		if (compress_type != BTRFS_COMPRESS_NONE) {
1310 			set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
1311 			em->compress_type = compress_type;
1312 			em->block_start = bytenr;
1313 			em->block_len = em->orig_block_len;
1314 		} else {
1315 			bytenr += btrfs_file_extent_offset(leaf, fi);
1316 			em->block_start = bytenr;
1317 			em->block_len = em->len;
1318 			if (type == BTRFS_FILE_EXTENT_PREALLOC)
1319 				set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
1320 		}
1321 	} else if (type == BTRFS_FILE_EXTENT_INLINE) {
1322 		em->block_start = EXTENT_MAP_INLINE;
1323 		em->start = extent_start;
1324 		em->len = extent_end - extent_start;
1325 		/*
1326 		 * Initialize orig_start and block_len with the same values
1327 		 * as in inode.c:btrfs_get_extent().
1328 		 */
1329 		em->orig_start = EXTENT_MAP_HOLE;
1330 		em->block_len = (u64)-1;
1331 		em->compress_type = compress_type;
1332 		if (compress_type != BTRFS_COMPRESS_NONE)
1333 			set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
1334 	} else {
1335 		btrfs_err(fs_info,
1336 			  "unknown file extent item type %d, inode %llu, offset %llu, "
1337 			  "root %llu", type, btrfs_ino(inode), extent_start,
1338 			  root->root_key.objectid);
1339 	}
1340 }
1341 
1342 /*
1343  * Returns the end offset (non inclusive) of the file extent item the given path
1344  * points to. If it points to an inline extent, the returned offset is rounded
1345  * up to the sector size.
1346  */
1347 u64 btrfs_file_extent_end(const struct btrfs_path *path)
1348 {
1349 	const struct extent_buffer *leaf = path->nodes[0];
1350 	const int slot = path->slots[0];
1351 	struct btrfs_file_extent_item *fi;
1352 	struct btrfs_key key;
1353 	u64 end;
1354 
1355 	btrfs_item_key_to_cpu(leaf, &key, slot);
1356 	ASSERT(key.type == BTRFS_EXTENT_DATA_KEY);
1357 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
1358 
1359 	if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
1360 		end = btrfs_file_extent_ram_bytes(leaf, fi);
1361 		end = ALIGN(key.offset + end, leaf->fs_info->sectorsize);
1362 	} else {
1363 		end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
1364 	}
1365 
1366 	return end;
1367 }
1368