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