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