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