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