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