1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 26cbd5570SChris Mason /* 36cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 46cbd5570SChris Mason */ 56cbd5570SChris Mason 6065631f6SChris Mason #include <linux/bio.h> 75a0e3ad6STejun Heo #include <linux/slab.h> 8065631f6SChris Mason #include <linux/pagemap.h> 9065631f6SChris Mason #include <linux/highmem.h> 10a3d46aeaSNikolay Borisov #include <linux/sched/mm.h> 11d5178578SJohannes Thumshirn #include <crypto/hash.h> 12*cea62800SJohannes Thumshirn #include "misc.h" 131e1d2701SChris Mason #include "ctree.h" 14dee26a9fSChris Mason #include "disk-io.h" 159f5fae2fSChris Mason #include "transaction.h" 16facc8a22SMiao Xie #include "volumes.h" 171de037a4SChris Mason #include "print-tree.h" 18ebb8765bSAnand Jain #include "compression.h" 191e1d2701SChris Mason 2042049bf6SChris Mason #define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \ 2142049bf6SChris Mason sizeof(struct btrfs_item) * 2) / \ 2242049bf6SChris Mason size) - 1)) 2307d400a6SYan Zheng 24221b8318SZach Brown #define MAX_CSUM_ITEMS(r, size) (min_t(u32, __MAX_CSUM_ITEMS(r, size), \ 2509cbfeafSKirill A. Shutemov PAGE_SIZE)) 267ca4be45SChris Mason 2741a2ee75SJosef Bacik /** 28ca4207aeSNikolay Borisov * Set inode's size according to filesystem options 29ca4207aeSNikolay Borisov * 30ca4207aeSNikolay Borisov * @inode: inode we want to update the disk_i_size for 31ca4207aeSNikolay Borisov * @new_i_size: i_size we want to set to, 0 if we use i_size 3241a2ee75SJosef Bacik * 3341a2ee75SJosef Bacik * With NO_HOLES set this simply sets the disk_is_size to whatever i_size_read() 3441a2ee75SJosef Bacik * returns as it is perfectly fine with a file that has holes without hole file 3541a2ee75SJosef Bacik * extent items. 3641a2ee75SJosef Bacik * 3741a2ee75SJosef Bacik * However without NO_HOLES we need to only return the area that is contiguous 3841a2ee75SJosef Bacik * from the 0 offset of the file. Otherwise we could end up adjust i_size up 3941a2ee75SJosef Bacik * to an extent that has a gap in between. 4041a2ee75SJosef Bacik * 4141a2ee75SJosef Bacik * Finally new_i_size should only be set in the case of truncate where we're not 4241a2ee75SJosef Bacik * ready to use i_size_read() as the limiter yet. 4341a2ee75SJosef Bacik */ 4476aea537SNikolay Borisov void btrfs_inode_safe_disk_i_size_write(struct btrfs_inode *inode, u64 new_i_size) 4541a2ee75SJosef Bacik { 4676aea537SNikolay Borisov struct btrfs_fs_info *fs_info = inode->root->fs_info; 4741a2ee75SJosef Bacik u64 start, end, i_size; 4841a2ee75SJosef Bacik int ret; 4941a2ee75SJosef Bacik 5076aea537SNikolay Borisov i_size = new_i_size ?: i_size_read(&inode->vfs_inode); 5141a2ee75SJosef Bacik if (btrfs_fs_incompat(fs_info, NO_HOLES)) { 5276aea537SNikolay Borisov inode->disk_i_size = i_size; 5341a2ee75SJosef Bacik return; 5441a2ee75SJosef Bacik } 5541a2ee75SJosef Bacik 5676aea537SNikolay Borisov spin_lock(&inode->lock); 5776aea537SNikolay Borisov ret = find_contiguous_extent_bit(&inode->file_extent_tree, 0, &start, 5876aea537SNikolay Borisov &end, EXTENT_DIRTY); 5941a2ee75SJosef Bacik if (!ret && start == 0) 6041a2ee75SJosef Bacik i_size = min(i_size, end + 1); 6141a2ee75SJosef Bacik else 6241a2ee75SJosef Bacik i_size = 0; 6376aea537SNikolay Borisov inode->disk_i_size = i_size; 6476aea537SNikolay Borisov spin_unlock(&inode->lock); 6541a2ee75SJosef Bacik } 6641a2ee75SJosef Bacik 6741a2ee75SJosef Bacik /** 68ca4207aeSNikolay Borisov * Mark range within a file as having a new extent inserted 69ca4207aeSNikolay Borisov * 70ca4207aeSNikolay Borisov * @inode: inode being modified 71ca4207aeSNikolay Borisov * @start: start file offset of the file extent we've inserted 72ca4207aeSNikolay Borisov * @len: logical length of the file extent item 7341a2ee75SJosef Bacik * 7441a2ee75SJosef Bacik * Call when we are inserting a new file extent where there was none before. 7541a2ee75SJosef Bacik * Does not need to call this in the case where we're replacing an existing file 7641a2ee75SJosef Bacik * extent, however if not sure it's fine to call this multiple times. 7741a2ee75SJosef Bacik * 7841a2ee75SJosef Bacik * The start and len must match the file extent item, so thus must be sectorsize 7941a2ee75SJosef Bacik * aligned. 8041a2ee75SJosef Bacik */ 8141a2ee75SJosef Bacik int btrfs_inode_set_file_extent_range(struct btrfs_inode *inode, u64 start, 8241a2ee75SJosef Bacik u64 len) 8341a2ee75SJosef Bacik { 8441a2ee75SJosef Bacik if (len == 0) 8541a2ee75SJosef Bacik return 0; 8641a2ee75SJosef Bacik 8741a2ee75SJosef Bacik ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize)); 8841a2ee75SJosef Bacik 8941a2ee75SJosef Bacik if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES)) 9041a2ee75SJosef Bacik return 0; 9141a2ee75SJosef Bacik return set_extent_bits(&inode->file_extent_tree, start, start + len - 1, 9241a2ee75SJosef Bacik EXTENT_DIRTY); 9341a2ee75SJosef Bacik } 9441a2ee75SJosef Bacik 9541a2ee75SJosef Bacik /** 96ca4207aeSNikolay Borisov * Marks an inode range as not having a backing extent 97ca4207aeSNikolay Borisov * 98ca4207aeSNikolay Borisov * @inode: inode being modified 99ca4207aeSNikolay Borisov * @start: start file offset of the file extent we've inserted 100ca4207aeSNikolay Borisov * @len: logical length of the file extent item 10141a2ee75SJosef Bacik * 10241a2ee75SJosef Bacik * Called when we drop a file extent, for example when we truncate. Doesn't 10341a2ee75SJosef Bacik * need to be called for cases where we're replacing a file extent, like when 10441a2ee75SJosef Bacik * we've COWed a file extent. 10541a2ee75SJosef Bacik * 10641a2ee75SJosef Bacik * The start and len must match the file extent item, so thus must be sectorsize 10741a2ee75SJosef Bacik * aligned. 10841a2ee75SJosef Bacik */ 10941a2ee75SJosef Bacik int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start, 11041a2ee75SJosef Bacik u64 len) 11141a2ee75SJosef Bacik { 11241a2ee75SJosef Bacik if (len == 0) 11341a2ee75SJosef Bacik return 0; 11441a2ee75SJosef Bacik 11541a2ee75SJosef Bacik ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize) || 11641a2ee75SJosef Bacik len == (u64)-1); 11741a2ee75SJosef Bacik 11841a2ee75SJosef Bacik if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES)) 11941a2ee75SJosef Bacik return 0; 12041a2ee75SJosef Bacik return clear_extent_bit(&inode->file_extent_tree, start, 12141a2ee75SJosef Bacik start + len - 1, EXTENT_DIRTY, 0, 0, NULL); 12241a2ee75SJosef Bacik } 12341a2ee75SJosef Bacik 1241e25a2e3SJohannes Thumshirn static inline u32 max_ordered_sum_bytes(struct btrfs_fs_info *fs_info, 1251e25a2e3SJohannes Thumshirn u16 csum_size) 1261e25a2e3SJohannes Thumshirn { 1271e25a2e3SJohannes Thumshirn u32 ncsums = (PAGE_SIZE - sizeof(struct btrfs_ordered_sum)) / csum_size; 1281e25a2e3SJohannes Thumshirn 1291e25a2e3SJohannes Thumshirn return ncsums * fs_info->sectorsize; 1301e25a2e3SJohannes Thumshirn } 13107d400a6SYan Zheng 132b18c6685SChris Mason int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, 133dee26a9fSChris Mason struct btrfs_root *root, 134b18c6685SChris Mason u64 objectid, u64 pos, 135f2eb0a24SSage Weil u64 disk_offset, u64 disk_num_bytes, 136c8b97818SChris Mason u64 num_bytes, u64 offset, u64 ram_bytes, 137c8b97818SChris Mason u8 compression, u8 encryption, u16 other_encoding) 1389f5fae2fSChris Mason { 139dee26a9fSChris Mason int ret = 0; 140dee26a9fSChris Mason struct btrfs_file_extent_item *item; 141dee26a9fSChris Mason struct btrfs_key file_key; 1425caf2a00SChris Mason struct btrfs_path *path; 1435f39d397SChris Mason struct extent_buffer *leaf; 144dee26a9fSChris Mason 1455caf2a00SChris Mason path = btrfs_alloc_path(); 146db5b493aSTsutomu Itoh if (!path) 147db5b493aSTsutomu Itoh return -ENOMEM; 148dee26a9fSChris Mason file_key.objectid = objectid; 149b18c6685SChris Mason file_key.offset = pos; 150962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_DATA_KEY; 151dee26a9fSChris Mason 1525caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 153dee26a9fSChris Mason sizeof(*item)); 15454aa1f4dSChris Mason if (ret < 0) 15554aa1f4dSChris Mason goto out; 15679787eaaSJeff Mahoney BUG_ON(ret); /* Can't happen */ 1575f39d397SChris Mason leaf = path->nodes[0]; 1585f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], 159dee26a9fSChris Mason struct btrfs_file_extent_item); 160f2eb0a24SSage Weil btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset); 161db94535dSChris Mason btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes); 162f2eb0a24SSage Weil btrfs_set_file_extent_offset(leaf, item, offset); 163db94535dSChris Mason btrfs_set_file_extent_num_bytes(leaf, item, num_bytes); 164c8b97818SChris Mason btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes); 1655f39d397SChris Mason btrfs_set_file_extent_generation(leaf, item, trans->transid); 1665f39d397SChris Mason btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); 167c8b97818SChris Mason btrfs_set_file_extent_compression(leaf, item, compression); 168c8b97818SChris Mason btrfs_set_file_extent_encryption(leaf, item, encryption); 169c8b97818SChris Mason btrfs_set_file_extent_other_encoding(leaf, item, other_encoding); 170c8b97818SChris Mason 1715f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 17254aa1f4dSChris Mason out: 1735caf2a00SChris Mason btrfs_free_path(path); 17454aa1f4dSChris Mason return ret; 1759f5fae2fSChris Mason } 176dee26a9fSChris Mason 17748a3b636SEric Sandeen static struct btrfs_csum_item * 17848a3b636SEric Sandeen btrfs_lookup_csum(struct btrfs_trans_handle *trans, 179b18c6685SChris Mason struct btrfs_root *root, 1806567e837SChris Mason struct btrfs_path *path, 181d20f7043SChris Mason u64 bytenr, int cow) 1826567e837SChris Mason { 1830b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1846567e837SChris Mason int ret; 1856567e837SChris Mason struct btrfs_key file_key; 1866567e837SChris Mason struct btrfs_key found_key; 1876567e837SChris Mason struct btrfs_csum_item *item; 1885f39d397SChris Mason struct extent_buffer *leaf; 1896567e837SChris Mason u64 csum_offset = 0; 190223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 191a429e513SChris Mason int csums_in_item; 1926567e837SChris Mason 193d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 194d20f7043SChris Mason file_key.offset = bytenr; 195962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_CSUM_KEY; 196b18c6685SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); 1976567e837SChris Mason if (ret < 0) 1986567e837SChris Mason goto fail; 1995f39d397SChris Mason leaf = path->nodes[0]; 2006567e837SChris Mason if (ret > 0) { 2016567e837SChris Mason ret = 1; 20270b2befdSChris Mason if (path->slots[0] == 0) 2036567e837SChris Mason goto fail; 2046567e837SChris Mason path->slots[0]--; 2055f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 206962a298fSDavid Sterba if (found_key.type != BTRFS_EXTENT_CSUM_KEY) 2076567e837SChris Mason goto fail; 208d20f7043SChris Mason 209d20f7043SChris Mason csum_offset = (bytenr - found_key.offset) >> 210265fdfa6SDavid Sterba fs_info->sectorsize_bits; 2115f39d397SChris Mason csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]); 212607d432dSJosef Bacik csums_in_item /= csum_size; 213a429e513SChris Mason 21482d130ffSMiao Xie if (csum_offset == csums_in_item) { 215a429e513SChris Mason ret = -EFBIG; 2166567e837SChris Mason goto fail; 21782d130ffSMiao Xie } else if (csum_offset > csums_in_item) { 21882d130ffSMiao Xie goto fail; 2196567e837SChris Mason } 2206567e837SChris Mason } 2216567e837SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 222509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 223607d432dSJosef Bacik csum_offset * csum_size); 2246567e837SChris Mason return item; 2256567e837SChris Mason fail: 2266567e837SChris Mason if (ret > 0) 227b18c6685SChris Mason ret = -ENOENT; 2286567e837SChris Mason return ERR_PTR(ret); 2296567e837SChris Mason } 2306567e837SChris Mason 231dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, 232dee26a9fSChris Mason struct btrfs_root *root, 233dee26a9fSChris Mason struct btrfs_path *path, u64 objectid, 2349773a788SChris Mason u64 offset, int mod) 235dee26a9fSChris Mason { 236dee26a9fSChris Mason int ret; 237dee26a9fSChris Mason struct btrfs_key file_key; 238dee26a9fSChris Mason int ins_len = mod < 0 ? -1 : 0; 239dee26a9fSChris Mason int cow = mod != 0; 240dee26a9fSChris Mason 241dee26a9fSChris Mason file_key.objectid = objectid; 24270b2befdSChris Mason file_key.offset = offset; 243962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_DATA_KEY; 244dee26a9fSChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); 245dee26a9fSChris Mason return ret; 246dee26a9fSChris Mason } 247f254e52cSChris Mason 2486275193eSQu Wenruo /* 2496275193eSQu Wenruo * Find checksums for logical bytenr range [disk_bytenr, disk_bytenr + len) and 2506275193eSQu Wenruo * estore the result to @dst. 2516275193eSQu Wenruo * 2526275193eSQu Wenruo * Return >0 for the number of sectors we found. 2536275193eSQu Wenruo * Return 0 for the range [disk_bytenr, disk_bytenr + sectorsize) has no csum 2546275193eSQu Wenruo * for it. Caller may want to try next sector until one range is hit. 2556275193eSQu Wenruo * Return <0 for fatal error. 2566275193eSQu Wenruo */ 2576275193eSQu Wenruo static int search_csum_tree(struct btrfs_fs_info *fs_info, 2586275193eSQu Wenruo struct btrfs_path *path, u64 disk_bytenr, 2596275193eSQu Wenruo u64 len, u8 *dst) 2606275193eSQu Wenruo { 2616275193eSQu Wenruo struct btrfs_csum_item *item = NULL; 2626275193eSQu Wenruo struct btrfs_key key; 2636275193eSQu Wenruo const u32 sectorsize = fs_info->sectorsize; 2646275193eSQu Wenruo const u32 csum_size = fs_info->csum_size; 2656275193eSQu Wenruo u32 itemsize; 2666275193eSQu Wenruo int ret; 2676275193eSQu Wenruo u64 csum_start; 2686275193eSQu Wenruo u64 csum_len; 2696275193eSQu Wenruo 2706275193eSQu Wenruo ASSERT(IS_ALIGNED(disk_bytenr, sectorsize) && 2716275193eSQu Wenruo IS_ALIGNED(len, sectorsize)); 2726275193eSQu Wenruo 2736275193eSQu Wenruo /* Check if the current csum item covers disk_bytenr */ 2746275193eSQu Wenruo if (path->nodes[0]) { 2756275193eSQu Wenruo item = btrfs_item_ptr(path->nodes[0], path->slots[0], 2766275193eSQu Wenruo struct btrfs_csum_item); 2776275193eSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2786275193eSQu Wenruo itemsize = btrfs_item_size_nr(path->nodes[0], path->slots[0]); 2796275193eSQu Wenruo 2806275193eSQu Wenruo csum_start = key.offset; 2816275193eSQu Wenruo csum_len = (itemsize / csum_size) * sectorsize; 2826275193eSQu Wenruo 2836275193eSQu Wenruo if (in_range(disk_bytenr, csum_start, csum_len)) 2846275193eSQu Wenruo goto found; 2856275193eSQu Wenruo } 2866275193eSQu Wenruo 2876275193eSQu Wenruo /* Current item doesn't contain the desired range, search again */ 2886275193eSQu Wenruo btrfs_release_path(path); 2896275193eSQu Wenruo item = btrfs_lookup_csum(NULL, fs_info->csum_root, path, disk_bytenr, 0); 2906275193eSQu Wenruo if (IS_ERR(item)) { 2916275193eSQu Wenruo ret = PTR_ERR(item); 2926275193eSQu Wenruo goto out; 2936275193eSQu Wenruo } 2946275193eSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2956275193eSQu Wenruo itemsize = btrfs_item_size_nr(path->nodes[0], path->slots[0]); 2966275193eSQu Wenruo 2976275193eSQu Wenruo csum_start = key.offset; 2986275193eSQu Wenruo csum_len = (itemsize / csum_size) * sectorsize; 2996275193eSQu Wenruo ASSERT(in_range(disk_bytenr, csum_start, csum_len)); 3006275193eSQu Wenruo 3016275193eSQu Wenruo found: 3026275193eSQu Wenruo ret = (min(csum_start + csum_len, disk_bytenr + len) - 3036275193eSQu Wenruo disk_bytenr) >> fs_info->sectorsize_bits; 3046275193eSQu Wenruo read_extent_buffer(path->nodes[0], dst, (unsigned long)item, 3056275193eSQu Wenruo ret * csum_size); 3066275193eSQu Wenruo out: 3076275193eSQu Wenruo if (ret == -ENOENT) 3086275193eSQu Wenruo ret = 0; 3096275193eSQu Wenruo return ret; 3106275193eSQu Wenruo } 3116275193eSQu Wenruo 3126275193eSQu Wenruo /* 3136275193eSQu Wenruo * Locate the file_offset of @cur_disk_bytenr of a @bio. 3146275193eSQu Wenruo * 3156275193eSQu Wenruo * Bio of btrfs represents read range of 3166275193eSQu Wenruo * [bi_sector << 9, bi_sector << 9 + bi_size). 3176275193eSQu Wenruo * Knowing this, we can iterate through each bvec to locate the page belong to 3186275193eSQu Wenruo * @cur_disk_bytenr and get the file offset. 3196275193eSQu Wenruo * 3206275193eSQu Wenruo * @inode is used to determine if the bvec page really belongs to @inode. 3216275193eSQu Wenruo * 3226275193eSQu Wenruo * Return 0 if we can't find the file offset 3236275193eSQu Wenruo * Return >0 if we find the file offset and restore it to @file_offset_ret 3246275193eSQu Wenruo */ 3256275193eSQu Wenruo static int search_file_offset_in_bio(struct bio *bio, struct inode *inode, 3266275193eSQu Wenruo u64 disk_bytenr, u64 *file_offset_ret) 3276275193eSQu Wenruo { 3286275193eSQu Wenruo struct bvec_iter iter; 3296275193eSQu Wenruo struct bio_vec bvec; 3306275193eSQu Wenruo u64 cur = bio->bi_iter.bi_sector << SECTOR_SHIFT; 3316275193eSQu Wenruo int ret = 0; 3326275193eSQu Wenruo 3336275193eSQu Wenruo bio_for_each_segment(bvec, bio, iter) { 3346275193eSQu Wenruo struct page *page = bvec.bv_page; 3356275193eSQu Wenruo 3366275193eSQu Wenruo if (cur > disk_bytenr) 3376275193eSQu Wenruo break; 3386275193eSQu Wenruo if (cur + bvec.bv_len <= disk_bytenr) { 3396275193eSQu Wenruo cur += bvec.bv_len; 3406275193eSQu Wenruo continue; 3416275193eSQu Wenruo } 3426275193eSQu Wenruo ASSERT(in_range(disk_bytenr, cur, bvec.bv_len)); 3436275193eSQu Wenruo if (page->mapping && page->mapping->host && 3446275193eSQu Wenruo page->mapping->host == inode) { 3456275193eSQu Wenruo ret = 1; 3466275193eSQu Wenruo *file_offset_ret = page_offset(page) + bvec.bv_offset + 3476275193eSQu Wenruo disk_bytenr - cur; 3486275193eSQu Wenruo break; 3496275193eSQu Wenruo } 3506275193eSQu Wenruo } 3516275193eSQu Wenruo return ret; 3526275193eSQu Wenruo } 3536275193eSQu Wenruo 354e62958fcSOmar Sandoval /** 3556275193eSQu Wenruo * Lookup the checksum for the read bio in csum tree. 3569e46458aSQu Wenruo * 357e62958fcSOmar Sandoval * @inode: inode that the bio is for. 358fb30f470SOmar Sandoval * @bio: bio to look up. 359fb30f470SOmar Sandoval * @dst: Buffer of size nblocks * btrfs_super_csum_size() used to return 360fb30f470SOmar Sandoval * checksum (nblocks = bio->bi_iter.bi_size / fs_info->sectorsize). If 361fb30f470SOmar Sandoval * NULL, the checksum buffer is allocated and returned in 362fb30f470SOmar Sandoval * btrfs_io_bio(bio)->csum instead. 363e62958fcSOmar Sandoval * 364e62958fcSOmar Sandoval * Return: BLK_STS_RESOURCE if allocating memory fails, BLK_STS_OK otherwise. 365e62958fcSOmar Sandoval */ 3666275193eSQu Wenruo blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, u8 *dst) 36761b49440SChris Mason { 3680b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 369facc8a22SMiao Xie struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 370facc8a22SMiao Xie struct btrfs_path *path; 3716275193eSQu Wenruo const u32 sectorsize = fs_info->sectorsize; 372223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 3736275193eSQu Wenruo u32 orig_len = bio->bi_iter.bi_size; 3746275193eSQu Wenruo u64 orig_disk_bytenr = bio->bi_iter.bi_sector << SECTOR_SHIFT; 3756275193eSQu Wenruo u64 cur_disk_bytenr; 3766275193eSQu Wenruo u8 *csum; 3776275193eSQu Wenruo const unsigned int nblocks = orig_len >> fs_info->sectorsize_bits; 3786275193eSQu Wenruo int count = 0; 37961b49440SChris Mason 38042437a63SJosef Bacik if (!fs_info->csum_root || (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) 381334c16d8SJosef Bacik return BLK_STS_OK; 382334c16d8SJosef Bacik 3839e46458aSQu Wenruo /* 3849e46458aSQu Wenruo * This function is only called for read bio. 3859e46458aSQu Wenruo * 3869e46458aSQu Wenruo * This means two things: 3879e46458aSQu Wenruo * - All our csums should only be in csum tree 3889e46458aSQu Wenruo * No ordered extents csums, as ordered extents are only for write 3899e46458aSQu Wenruo * path. 3906275193eSQu Wenruo * - No need to bother any other info from bvec 3916275193eSQu Wenruo * Since we're looking up csums, the only important info is the 3926275193eSQu Wenruo * disk_bytenr and the length, which can be extracted from bi_iter 3936275193eSQu Wenruo * directly. 3949e46458aSQu Wenruo */ 3959e46458aSQu Wenruo ASSERT(bio_op(bio) == REQ_OP_READ); 39661b49440SChris Mason path = btrfs_alloc_path(); 397c2db1073STsutomu Itoh if (!path) 3984e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 399facc8a22SMiao Xie 400facc8a22SMiao Xie if (!dst) { 401fb30f470SOmar Sandoval struct btrfs_io_bio *btrfs_bio = btrfs_io_bio(bio); 402fb30f470SOmar Sandoval 403facc8a22SMiao Xie if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) { 40431fecccbSDavid Sterba btrfs_bio->csum = kmalloc_array(nblocks, csum_size, 40531fecccbSDavid Sterba GFP_NOFS); 40631fecccbSDavid Sterba if (!btrfs_bio->csum) { 407facc8a22SMiao Xie btrfs_free_path(path); 4084e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 409facc8a22SMiao Xie } 410facc8a22SMiao Xie } else { 411facc8a22SMiao Xie btrfs_bio->csum = btrfs_bio->csum_inline; 412facc8a22SMiao Xie } 413facc8a22SMiao Xie csum = btrfs_bio->csum; 414facc8a22SMiao Xie } else { 41510fe6ca8SJohannes Thumshirn csum = dst; 416facc8a22SMiao Xie } 417facc8a22SMiao Xie 41835478d05SQu Wenruo /* 41935478d05SQu Wenruo * If requested number of sectors is larger than one leaf can contain, 42035478d05SQu Wenruo * kick the readahead for csum tree. 42135478d05SQu Wenruo */ 42235478d05SQu Wenruo if (nblocks > fs_info->csums_per_leaf) 423e4058b54SDavid Sterba path->reada = READA_FORWARD; 42461b49440SChris Mason 4252cf8572dSChris Mason /* 4262cf8572dSChris Mason * the free space stuff is only read when it hasn't been 4272cf8572dSChris Mason * updated in the current transaction. So, we can safely 4282cf8572dSChris Mason * read from the commit root and sidestep a nasty deadlock 4292cf8572dSChris Mason * between reading the free space cache and updating the csum tree. 4302cf8572dSChris Mason */ 43170ddc553SNikolay Borisov if (btrfs_is_free_space_inode(BTRFS_I(inode))) { 4322cf8572dSChris Mason path->search_commit_root = 1; 433ddf23b3fSJosef Bacik path->skip_locking = 1; 434ddf23b3fSJosef Bacik } 4352cf8572dSChris Mason 4366275193eSQu Wenruo for (cur_disk_bytenr = orig_disk_bytenr; 4376275193eSQu Wenruo cur_disk_bytenr < orig_disk_bytenr + orig_len; 4386275193eSQu Wenruo cur_disk_bytenr += (count * sectorsize)) { 4396275193eSQu Wenruo u64 search_len = orig_disk_bytenr + orig_len - cur_disk_bytenr; 4406275193eSQu Wenruo unsigned int sector_offset; 4416275193eSQu Wenruo u8 *csum_dst; 442c40a3d38SChandan Rajendra 4436275193eSQu Wenruo /* 4446275193eSQu Wenruo * Although both cur_disk_bytenr and orig_disk_bytenr is u64, 4456275193eSQu Wenruo * we're calculating the offset to the bio start. 4466275193eSQu Wenruo * 4476275193eSQu Wenruo * Bio size is limited to UINT_MAX, thus unsigned int is large 4486275193eSQu Wenruo * enough to contain the raw result, not to mention the right 4496275193eSQu Wenruo * shifted result. 4506275193eSQu Wenruo */ 4516275193eSQu Wenruo ASSERT(cur_disk_bytenr - orig_disk_bytenr < UINT_MAX); 4526275193eSQu Wenruo sector_offset = (cur_disk_bytenr - orig_disk_bytenr) >> 4536275193eSQu Wenruo fs_info->sectorsize_bits; 4546275193eSQu Wenruo csum_dst = csum + sector_offset * csum_size; 4554989d277SChristoph Hellwig 4566275193eSQu Wenruo count = search_csum_tree(fs_info, path, cur_disk_bytenr, 4576275193eSQu Wenruo search_len, csum_dst); 4586275193eSQu Wenruo if (count <= 0) { 4596275193eSQu Wenruo /* 4606275193eSQu Wenruo * Either we hit a critical error or we didn't find 4616275193eSQu Wenruo * the csum. 4626275193eSQu Wenruo * Either way, we put zero into the csums dst, and skip 4636275193eSQu Wenruo * to the next sector. 4646275193eSQu Wenruo */ 4656275193eSQu Wenruo memset(csum_dst, 0, csum_size); 466e4100d98SMiao Xie count = 1; 4676275193eSQu Wenruo 4686275193eSQu Wenruo /* 4696275193eSQu Wenruo * For data reloc inode, we need to mark the range 4706275193eSQu Wenruo * NODATASUM so that balance won't report false csum 4716275193eSQu Wenruo * error. 4726275193eSQu Wenruo */ 47317d217feSYan Zheng if (BTRFS_I(inode)->root->root_key.objectid == 47417d217feSYan Zheng BTRFS_DATA_RELOC_TREE_OBJECTID) { 4756275193eSQu Wenruo u64 file_offset; 4766275193eSQu Wenruo int ret; 4776275193eSQu Wenruo 4786275193eSQu Wenruo ret = search_file_offset_in_bio(bio, inode, 4796275193eSQu Wenruo cur_disk_bytenr, &file_offset); 4806275193eSQu Wenruo if (ret) 4816275193eSQu Wenruo set_extent_bits(io_tree, file_offset, 4826275193eSQu Wenruo file_offset + sectorsize - 1, 483ceeb0ae7SDavid Sterba EXTENT_NODATASUM); 48417d217feSYan Zheng } else { 4856275193eSQu Wenruo btrfs_warn_rl(fs_info, 4866275193eSQu Wenruo "csum hole found for disk bytenr range [%llu, %llu)", 4876275193eSQu Wenruo cur_disk_bytenr, cur_disk_bytenr + sectorsize); 48817d217feSYan Zheng } 4894989d277SChristoph Hellwig } 4904989d277SChristoph Hellwig } 4914989d277SChristoph Hellwig 49261b49440SChris Mason btrfs_free_path(path); 493e62958fcSOmar Sandoval return BLK_STS_OK; 4944b46fce2SJosef Bacik } 4954b46fce2SJosef Bacik 49617d217feSYan Zheng int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, 497a2de733cSArne Jansen struct list_head *list, int search_commit) 49817d217feSYan Zheng { 4990b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 50017d217feSYan Zheng struct btrfs_key key; 50117d217feSYan Zheng struct btrfs_path *path; 50217d217feSYan Zheng struct extent_buffer *leaf; 50317d217feSYan Zheng struct btrfs_ordered_sum *sums; 50417d217feSYan Zheng struct btrfs_csum_item *item; 5050678b618SMark Fasheh LIST_HEAD(tmplist); 50617d217feSYan Zheng unsigned long offset; 50717d217feSYan Zheng int ret; 50817d217feSYan Zheng size_t size; 50917d217feSYan Zheng u64 csum_end; 510223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 51117d217feSYan Zheng 5120b246afaSJeff Mahoney ASSERT(IS_ALIGNED(start, fs_info->sectorsize) && 5130b246afaSJeff Mahoney IS_ALIGNED(end + 1, fs_info->sectorsize)); 5144277a9c3SJosef Bacik 51517d217feSYan Zheng path = btrfs_alloc_path(); 516d8926bb3SMark Fasheh if (!path) 517d8926bb3SMark Fasheh return -ENOMEM; 51817d217feSYan Zheng 519a2de733cSArne Jansen if (search_commit) { 520a2de733cSArne Jansen path->skip_locking = 1; 521e4058b54SDavid Sterba path->reada = READA_FORWARD; 522a2de733cSArne Jansen path->search_commit_root = 1; 523a2de733cSArne Jansen } 524a2de733cSArne Jansen 52517d217feSYan Zheng key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 52617d217feSYan Zheng key.offset = start; 52717d217feSYan Zheng key.type = BTRFS_EXTENT_CSUM_KEY; 52817d217feSYan Zheng 52907d400a6SYan Zheng ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 53017d217feSYan Zheng if (ret < 0) 53117d217feSYan Zheng goto fail; 53217d217feSYan Zheng if (ret > 0 && path->slots[0] > 0) { 53317d217feSYan Zheng leaf = path->nodes[0]; 53417d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); 53517d217feSYan Zheng if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID && 53617d217feSYan Zheng key.type == BTRFS_EXTENT_CSUM_KEY) { 537265fdfa6SDavid Sterba offset = (start - key.offset) >> fs_info->sectorsize_bits; 53817d217feSYan Zheng if (offset * csum_size < 53917d217feSYan Zheng btrfs_item_size_nr(leaf, path->slots[0] - 1)) 54017d217feSYan Zheng path->slots[0]--; 54117d217feSYan Zheng } 54217d217feSYan Zheng } 54317d217feSYan Zheng 54417d217feSYan Zheng while (start <= end) { 54517d217feSYan Zheng leaf = path->nodes[0]; 54617d217feSYan Zheng if (path->slots[0] >= btrfs_header_nritems(leaf)) { 54707d400a6SYan Zheng ret = btrfs_next_leaf(root, path); 54817d217feSYan Zheng if (ret < 0) 54917d217feSYan Zheng goto fail; 55017d217feSYan Zheng if (ret > 0) 55117d217feSYan Zheng break; 55217d217feSYan Zheng leaf = path->nodes[0]; 55317d217feSYan Zheng } 55417d217feSYan Zheng 55517d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 55617d217feSYan Zheng if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 557628c8282SZhi Yong Wu key.type != BTRFS_EXTENT_CSUM_KEY || 558628c8282SZhi Yong Wu key.offset > end) 55917d217feSYan Zheng break; 56017d217feSYan Zheng 56117d217feSYan Zheng if (key.offset > start) 56217d217feSYan Zheng start = key.offset; 56317d217feSYan Zheng 56417d217feSYan Zheng size = btrfs_item_size_nr(leaf, path->slots[0]); 5650b246afaSJeff Mahoney csum_end = key.offset + (size / csum_size) * fs_info->sectorsize; 56687b29b20SYan Zheng if (csum_end <= start) { 56787b29b20SYan Zheng path->slots[0]++; 56887b29b20SYan Zheng continue; 56987b29b20SYan Zheng } 57017d217feSYan Zheng 57107d400a6SYan Zheng csum_end = min(csum_end, end + 1); 57207d400a6SYan Zheng item = btrfs_item_ptr(path->nodes[0], path->slots[0], 57307d400a6SYan Zheng struct btrfs_csum_item); 57407d400a6SYan Zheng while (start < csum_end) { 57507d400a6SYan Zheng size = min_t(size_t, csum_end - start, 5761e25a2e3SJohannes Thumshirn max_ordered_sum_bytes(fs_info, csum_size)); 5770b246afaSJeff Mahoney sums = kzalloc(btrfs_ordered_sum_size(fs_info, size), 57807d400a6SYan Zheng GFP_NOFS); 5790678b618SMark Fasheh if (!sums) { 5800678b618SMark Fasheh ret = -ENOMEM; 5810678b618SMark Fasheh goto fail; 5820678b618SMark Fasheh } 58317d217feSYan Zheng 58417d217feSYan Zheng sums->bytenr = start; 585f51a4a18SMiao Xie sums->len = (int)size; 58617d217feSYan Zheng 587265fdfa6SDavid Sterba offset = (start - key.offset) >> fs_info->sectorsize_bits; 58817d217feSYan Zheng offset *= csum_size; 589265fdfa6SDavid Sterba size >>= fs_info->sectorsize_bits; 59017d217feSYan Zheng 59107d400a6SYan Zheng read_extent_buffer(path->nodes[0], 592f51a4a18SMiao Xie sums->sums, 593f51a4a18SMiao Xie ((unsigned long)item) + offset, 594f51a4a18SMiao Xie csum_size * size); 59517d217feSYan Zheng 5960b246afaSJeff Mahoney start += fs_info->sectorsize * size; 5970678b618SMark Fasheh list_add_tail(&sums->list, &tmplist); 59807d400a6SYan Zheng } 59917d217feSYan Zheng path->slots[0]++; 60017d217feSYan Zheng } 60117d217feSYan Zheng ret = 0; 60217d217feSYan Zheng fail: 6030678b618SMark Fasheh while (ret < 0 && !list_empty(&tmplist)) { 6046e5aafb2SChris Mason sums = list_entry(tmplist.next, struct btrfs_ordered_sum, list); 6050678b618SMark Fasheh list_del(&sums->list); 6060678b618SMark Fasheh kfree(sums); 6070678b618SMark Fasheh } 6080678b618SMark Fasheh list_splice_tail(&tmplist, list); 6090678b618SMark Fasheh 61017d217feSYan Zheng btrfs_free_path(path); 61117d217feSYan Zheng return ret; 61217d217feSYan Zheng } 61317d217feSYan Zheng 61451d470aeSNikolay Borisov /* 61551d470aeSNikolay Borisov * btrfs_csum_one_bio - Calculates checksums of the data contained inside a bio 61651d470aeSNikolay Borisov * @inode: Owner of the data inside the bio 61751d470aeSNikolay Borisov * @bio: Contains the data to be checksummed 61851d470aeSNikolay Borisov * @file_start: offset in file this bio begins to describe 61951d470aeSNikolay Borisov * @contig: Boolean. If true/1 means all bio vecs in this bio are 62051d470aeSNikolay Borisov * contiguous and they begin at @file_start in the file. False/0 62151d470aeSNikolay Borisov * means this bio can contains potentially discontigous bio vecs 62251d470aeSNikolay Borisov * so the logical offset of each should be calculated separately. 62351d470aeSNikolay Borisov */ 624bd242a08SNikolay Borisov blk_status_t btrfs_csum_one_bio(struct btrfs_inode *inode, struct bio *bio, 6252ff7e61eSJeff Mahoney u64 file_start, int contig) 626e015640fSChris Mason { 627c3504372SNikolay Borisov struct btrfs_fs_info *fs_info = inode->root->fs_info; 628d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 629e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums; 6306cd7ce49SChristoph Hellwig struct btrfs_ordered_extent *ordered = NULL; 631e015640fSChris Mason char *data; 63217347cecSLiu Bo struct bvec_iter iter; 63317347cecSLiu Bo struct bio_vec bvec; 634f51a4a18SMiao Xie int index; 635c40a3d38SChandan Rajendra int nr_sectors; 6363edf7d33SChris Mason unsigned long total_bytes = 0; 6373edf7d33SChris Mason unsigned long this_sum_bytes = 0; 63817347cecSLiu Bo int i; 6393edf7d33SChris Mason u64 offset; 640a3d46aeaSNikolay Borisov unsigned nofs_flag; 641e015640fSChris Mason 642a3d46aeaSNikolay Borisov nofs_flag = memalloc_nofs_save(); 643a3d46aeaSNikolay Borisov sums = kvzalloc(btrfs_ordered_sum_size(fs_info, bio->bi_iter.bi_size), 644a3d46aeaSNikolay Borisov GFP_KERNEL); 645a3d46aeaSNikolay Borisov memalloc_nofs_restore(nofs_flag); 646a3d46aeaSNikolay Borisov 647e015640fSChris Mason if (!sums) 6484e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 6493edf7d33SChris Mason 6504f024f37SKent Overstreet sums->len = bio->bi_iter.bi_size; 651e6dcd2dcSChris Mason INIT_LIST_HEAD(&sums->list); 652d20f7043SChris Mason 653d20f7043SChris Mason if (contig) 654d20f7043SChris Mason offset = file_start; 655d20f7043SChris Mason else 6566cd7ce49SChristoph Hellwig offset = 0; /* shut up gcc */ 657d20f7043SChris Mason 6581201b58bSDavid Sterba sums->bytenr = bio->bi_iter.bi_sector << 9; 659f51a4a18SMiao Xie index = 0; 660e015640fSChris Mason 661d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 662d5178578SJohannes Thumshirn 66317347cecSLiu Bo bio_for_each_segment(bvec, bio, iter) { 664d20f7043SChris Mason if (!contig) 66517347cecSLiu Bo offset = page_offset(bvec.bv_page) + bvec.bv_offset; 666d20f7043SChris Mason 6676cd7ce49SChristoph Hellwig if (!ordered) { 6686cd7ce49SChristoph Hellwig ordered = btrfs_lookup_ordered_extent(inode, offset); 6696cd7ce49SChristoph Hellwig BUG_ON(!ordered); /* Logic error */ 6706cd7ce49SChristoph Hellwig } 6716cd7ce49SChristoph Hellwig 6720b246afaSJeff Mahoney nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, 67317347cecSLiu Bo bvec.bv_len + fs_info->sectorsize 674c40a3d38SChandan Rajendra - 1); 675c40a3d38SChandan Rajendra 676c40a3d38SChandan Rajendra for (i = 0; i < nr_sectors; i++) { 677bffe633eSOmar Sandoval if (offset >= ordered->file_offset + ordered->num_bytes || 678e58dd74bSJosef Bacik offset < ordered->file_offset) { 6793edf7d33SChris Mason unsigned long bytes_left; 680c40a3d38SChandan Rajendra 6813edf7d33SChris Mason sums->len = this_sum_bytes; 6823edf7d33SChris Mason this_sum_bytes = 0; 683f9756261SNikolay Borisov btrfs_add_ordered_sum(ordered, sums); 6843edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 6853edf7d33SChris Mason 6864f024f37SKent Overstreet bytes_left = bio->bi_iter.bi_size - total_bytes; 6873edf7d33SChris Mason 688a3d46aeaSNikolay Borisov nofs_flag = memalloc_nofs_save(); 689a3d46aeaSNikolay Borisov sums = kvzalloc(btrfs_ordered_sum_size(fs_info, 690a3d46aeaSNikolay Borisov bytes_left), GFP_KERNEL); 691a3d46aeaSNikolay Borisov memalloc_nofs_restore(nofs_flag); 69279787eaaSJeff Mahoney BUG_ON(!sums); /* -ENOMEM */ 6933edf7d33SChris Mason sums->len = bytes_left; 694c40a3d38SChandan Rajendra ordered = btrfs_lookup_ordered_extent(inode, 695c40a3d38SChandan Rajendra offset); 696c40a3d38SChandan Rajendra ASSERT(ordered); /* Logic error */ 6971201b58bSDavid Sterba sums->bytenr = (bio->bi_iter.bi_sector << 9) 698c40a3d38SChandan Rajendra + total_bytes; 699f51a4a18SMiao Xie index = 0; 700c40a3d38SChandan Rajendra } 701c40a3d38SChandan Rajendra 702443c8e2aSJohannes Thumshirn data = kmap_atomic(bvec.bv_page); 703fd08001fSEric Biggers crypto_shash_digest(shash, data + bvec.bv_offset 7040b246afaSJeff Mahoney + (i * fs_info->sectorsize), 705fd08001fSEric Biggers fs_info->sectorsize, 706fd08001fSEric Biggers sums->sums + index); 707443c8e2aSJohannes Thumshirn kunmap_atomic(data); 708713cebfbSDavid Sterba index += fs_info->csum_size; 7090b246afaSJeff Mahoney offset += fs_info->sectorsize; 7100b246afaSJeff Mahoney this_sum_bytes += fs_info->sectorsize; 7110b246afaSJeff Mahoney total_bytes += fs_info->sectorsize; 712c40a3d38SChandan Rajendra } 713c40a3d38SChandan Rajendra 714e015640fSChris Mason } 715ed98b56aSChris Mason this_sum_bytes = 0; 716f9756261SNikolay Borisov btrfs_add_ordered_sum(ordered, sums); 7173edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 718e015640fSChris Mason return 0; 719e015640fSChris Mason } 720e015640fSChris Mason 721459931ecSChris Mason /* 722459931ecSChris Mason * helper function for csum removal, this expects the 723459931ecSChris Mason * key to describe the csum pointed to by the path, and it expects 724459931ecSChris Mason * the csum to overlap the range [bytenr, len] 725459931ecSChris Mason * 726459931ecSChris Mason * The csum should not be entirely contained in the range and the 727459931ecSChris Mason * range should not be entirely contained in the csum. 728459931ecSChris Mason * 729459931ecSChris Mason * This calls btrfs_truncate_item with the correct args based on the 730459931ecSChris Mason * overlap, and fixes up the key as required. 731459931ecSChris Mason */ 7322ff7e61eSJeff Mahoney static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info, 733459931ecSChris Mason struct btrfs_path *path, 734459931ecSChris Mason struct btrfs_key *key, 735459931ecSChris Mason u64 bytenr, u64 len) 736459931ecSChris Mason { 737459931ecSChris Mason struct extent_buffer *leaf; 738223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 739459931ecSChris Mason u64 csum_end; 740459931ecSChris Mason u64 end_byte = bytenr + len; 741265fdfa6SDavid Sterba u32 blocksize_bits = fs_info->sectorsize_bits; 742459931ecSChris Mason 743459931ecSChris Mason leaf = path->nodes[0]; 744459931ecSChris Mason csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; 745265fdfa6SDavid Sterba csum_end <<= blocksize_bits; 746459931ecSChris Mason csum_end += key->offset; 747459931ecSChris Mason 748459931ecSChris Mason if (key->offset < bytenr && csum_end <= end_byte) { 749459931ecSChris Mason /* 750459931ecSChris Mason * [ bytenr - len ] 751459931ecSChris Mason * [ ] 752459931ecSChris Mason * [csum ] 753459931ecSChris Mason * A simple truncate off the end of the item 754459931ecSChris Mason */ 755459931ecSChris Mason u32 new_size = (bytenr - key->offset) >> blocksize_bits; 756459931ecSChris Mason new_size *= csum_size; 75778ac4f9eSDavid Sterba btrfs_truncate_item(path, new_size, 1); 758459931ecSChris Mason } else if (key->offset >= bytenr && csum_end > end_byte && 759459931ecSChris Mason end_byte > key->offset) { 760459931ecSChris Mason /* 761459931ecSChris Mason * [ bytenr - len ] 762459931ecSChris Mason * [ ] 763459931ecSChris Mason * [csum ] 764459931ecSChris Mason * we need to truncate from the beginning of the csum 765459931ecSChris Mason */ 766459931ecSChris Mason u32 new_size = (csum_end - end_byte) >> blocksize_bits; 767459931ecSChris Mason new_size *= csum_size; 768459931ecSChris Mason 76978ac4f9eSDavid Sterba btrfs_truncate_item(path, new_size, 0); 770459931ecSChris Mason 771459931ecSChris Mason key->offset = end_byte; 7720b246afaSJeff Mahoney btrfs_set_item_key_safe(fs_info, path, key); 773459931ecSChris Mason } else { 774459931ecSChris Mason BUG(); 775459931ecSChris Mason } 776459931ecSChris Mason } 777459931ecSChris Mason 778459931ecSChris Mason /* 779459931ecSChris Mason * deletes the csum items from the csum tree for a given 780459931ecSChris Mason * range of bytes. 781459931ecSChris Mason */ 782459931ecSChris Mason int btrfs_del_csums(struct btrfs_trans_handle *trans, 78340e046acSFilipe Manana struct btrfs_root *root, u64 bytenr, u64 len) 784459931ecSChris Mason { 78540e046acSFilipe Manana struct btrfs_fs_info *fs_info = trans->fs_info; 786459931ecSChris Mason struct btrfs_path *path; 787459931ecSChris Mason struct btrfs_key key; 788459931ecSChris Mason u64 end_byte = bytenr + len; 789459931ecSChris Mason u64 csum_end; 790459931ecSChris Mason struct extent_buffer *leaf; 791459931ecSChris Mason int ret; 792223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 793265fdfa6SDavid Sterba u32 blocksize_bits = fs_info->sectorsize_bits; 794459931ecSChris Mason 79540e046acSFilipe Manana ASSERT(root == fs_info->csum_root || 79640e046acSFilipe Manana root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); 79740e046acSFilipe Manana 798459931ecSChris Mason path = btrfs_alloc_path(); 7992a29edc6Sliubo if (!path) 8002a29edc6Sliubo return -ENOMEM; 801459931ecSChris Mason 802459931ecSChris Mason while (1) { 803459931ecSChris Mason key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 804459931ecSChris Mason key.offset = end_byte - 1; 805459931ecSChris Mason key.type = BTRFS_EXTENT_CSUM_KEY; 806459931ecSChris Mason 807459931ecSChris Mason ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 808459931ecSChris Mason if (ret > 0) { 809459931ecSChris Mason if (path->slots[0] == 0) 81065a246c5STsutomu Itoh break; 811459931ecSChris Mason path->slots[0]--; 812ad0397a7SJosef Bacik } else if (ret < 0) { 81365a246c5STsutomu Itoh break; 814459931ecSChris Mason } 815ad0397a7SJosef Bacik 816459931ecSChris Mason leaf = path->nodes[0]; 817459931ecSChris Mason btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 818459931ecSChris Mason 819459931ecSChris Mason if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 820459931ecSChris Mason key.type != BTRFS_EXTENT_CSUM_KEY) { 821459931ecSChris Mason break; 822459931ecSChris Mason } 823459931ecSChris Mason 824459931ecSChris Mason if (key.offset >= end_byte) 825459931ecSChris Mason break; 826459931ecSChris Mason 827459931ecSChris Mason csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; 828459931ecSChris Mason csum_end <<= blocksize_bits; 829459931ecSChris Mason csum_end += key.offset; 830459931ecSChris Mason 831459931ecSChris Mason /* this csum ends before we start, we're done */ 832459931ecSChris Mason if (csum_end <= bytenr) 833459931ecSChris Mason break; 834459931ecSChris Mason 835459931ecSChris Mason /* delete the entire item, it is inside our range */ 836459931ecSChris Mason if (key.offset >= bytenr && csum_end <= end_byte) { 8376f546216SFilipe Manana int del_nr = 1; 8386f546216SFilipe Manana 8396f546216SFilipe Manana /* 8406f546216SFilipe Manana * Check how many csum items preceding this one in this 8416f546216SFilipe Manana * leaf correspond to our range and then delete them all 8426f546216SFilipe Manana * at once. 8436f546216SFilipe Manana */ 8446f546216SFilipe Manana if (key.offset > bytenr && path->slots[0] > 0) { 8456f546216SFilipe Manana int slot = path->slots[0] - 1; 8466f546216SFilipe Manana 8476f546216SFilipe Manana while (slot >= 0) { 8486f546216SFilipe Manana struct btrfs_key pk; 8496f546216SFilipe Manana 8506f546216SFilipe Manana btrfs_item_key_to_cpu(leaf, &pk, slot); 8516f546216SFilipe Manana if (pk.offset < bytenr || 8526f546216SFilipe Manana pk.type != BTRFS_EXTENT_CSUM_KEY || 8536f546216SFilipe Manana pk.objectid != 8546f546216SFilipe Manana BTRFS_EXTENT_CSUM_OBJECTID) 8556f546216SFilipe Manana break; 8566f546216SFilipe Manana path->slots[0] = slot; 8576f546216SFilipe Manana del_nr++; 8586f546216SFilipe Manana key.offset = pk.offset; 8596f546216SFilipe Manana slot--; 8606f546216SFilipe Manana } 8616f546216SFilipe Manana } 8626f546216SFilipe Manana ret = btrfs_del_items(trans, root, path, 8636f546216SFilipe Manana path->slots[0], del_nr); 86465a246c5STsutomu Itoh if (ret) 86565a246c5STsutomu Itoh goto out; 866dcbdd4dcSChris Mason if (key.offset == bytenr) 867dcbdd4dcSChris Mason break; 868459931ecSChris Mason } else if (key.offset < bytenr && csum_end > end_byte) { 869459931ecSChris Mason unsigned long offset; 870459931ecSChris Mason unsigned long shift_len; 871459931ecSChris Mason unsigned long item_offset; 872459931ecSChris Mason /* 873459931ecSChris Mason * [ bytenr - len ] 874459931ecSChris Mason * [csum ] 875459931ecSChris Mason * 876459931ecSChris Mason * Our bytes are in the middle of the csum, 877459931ecSChris Mason * we need to split this item and insert a new one. 878459931ecSChris Mason * 879459931ecSChris Mason * But we can't drop the path because the 880459931ecSChris Mason * csum could change, get removed, extended etc. 881459931ecSChris Mason * 882459931ecSChris Mason * The trick here is the max size of a csum item leaves 883459931ecSChris Mason * enough room in the tree block for a single 884459931ecSChris Mason * item header. So, we split the item in place, 885459931ecSChris Mason * adding a new header pointing to the existing 886459931ecSChris Mason * bytes. Then we loop around again and we have 887459931ecSChris Mason * a nicely formed csum item that we can neatly 888459931ecSChris Mason * truncate. 889459931ecSChris Mason */ 890459931ecSChris Mason offset = (bytenr - key.offset) >> blocksize_bits; 891459931ecSChris Mason offset *= csum_size; 892459931ecSChris Mason 893459931ecSChris Mason shift_len = (len >> blocksize_bits) * csum_size; 894459931ecSChris Mason 895459931ecSChris Mason item_offset = btrfs_item_ptr_offset(leaf, 896459931ecSChris Mason path->slots[0]); 897459931ecSChris Mason 898b159fa28SDavid Sterba memzero_extent_buffer(leaf, item_offset + offset, 899459931ecSChris Mason shift_len); 900459931ecSChris Mason key.offset = bytenr; 901459931ecSChris Mason 902459931ecSChris Mason /* 903459931ecSChris Mason * btrfs_split_item returns -EAGAIN when the 904459931ecSChris Mason * item changed size or key 905459931ecSChris Mason */ 906459931ecSChris Mason ret = btrfs_split_item(trans, root, path, &key, offset); 90779787eaaSJeff Mahoney if (ret && ret != -EAGAIN) { 90866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 90979787eaaSJeff Mahoney goto out; 91079787eaaSJeff Mahoney } 911459931ecSChris Mason 912459931ecSChris Mason key.offset = end_byte - 1; 913459931ecSChris Mason } else { 9142ff7e61eSJeff Mahoney truncate_one_csum(fs_info, path, &key, bytenr, len); 915dcbdd4dcSChris Mason if (key.offset < bytenr) 916dcbdd4dcSChris Mason break; 917459931ecSChris Mason } 918b3b4aa74SDavid Sterba btrfs_release_path(path); 919459931ecSChris Mason } 92065a246c5STsutomu Itoh ret = 0; 921459931ecSChris Mason out: 922459931ecSChris Mason btrfs_free_path(path); 92365a246c5STsutomu Itoh return ret; 924459931ecSChris Mason } 925459931ecSChris Mason 926065631f6SChris Mason int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, 927d20f7043SChris Mason struct btrfs_root *root, 928e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums) 929f254e52cSChris Mason { 9300b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 931f254e52cSChris Mason struct btrfs_key file_key; 9326567e837SChris Mason struct btrfs_key found_key; 9335caf2a00SChris Mason struct btrfs_path *path; 934f254e52cSChris Mason struct btrfs_csum_item *item; 935065631f6SChris Mason struct btrfs_csum_item *item_end; 936ff79f819SChris Mason struct extent_buffer *leaf = NULL; 937f51a4a18SMiao Xie u64 next_offset; 938f51a4a18SMiao Xie u64 total_bytes = 0; 9396567e837SChris Mason u64 csum_offset; 940f51a4a18SMiao Xie u64 bytenr; 941f578d4bdSChris Mason u32 nritems; 942f578d4bdSChris Mason u32 ins_size; 943f51a4a18SMiao Xie int index = 0; 944f51a4a18SMiao Xie int found_next; 945f51a4a18SMiao Xie int ret; 946223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 9476e92f5e6SChris Mason 9485caf2a00SChris Mason path = btrfs_alloc_path(); 949d8926bb3SMark Fasheh if (!path) 950d8926bb3SMark Fasheh return -ENOMEM; 951065631f6SChris Mason again: 952065631f6SChris Mason next_offset = (u64)-1; 953065631f6SChris Mason found_next = 0; 954f51a4a18SMiao Xie bytenr = sums->bytenr + total_bytes; 955d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 956f51a4a18SMiao Xie file_key.offset = bytenr; 957962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_CSUM_KEY; 958a429e513SChris Mason 959f51a4a18SMiao Xie item = btrfs_lookup_csum(trans, root, path, bytenr, 1); 960ff79f819SChris Mason if (!IS_ERR(item)) { 961639cb586SChris Mason ret = 0; 962f51a4a18SMiao Xie leaf = path->nodes[0]; 963f51a4a18SMiao Xie item_end = btrfs_item_ptr(leaf, path->slots[0], 964f51a4a18SMiao Xie struct btrfs_csum_item); 965f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((char *)item_end + 966f51a4a18SMiao Xie btrfs_item_size_nr(leaf, path->slots[0])); 967a429e513SChris Mason goto found; 968ff79f819SChris Mason } 969a429e513SChris Mason ret = PTR_ERR(item); 9704a500fd1SYan, Zheng if (ret != -EFBIG && ret != -ENOENT) 971918cdf44SFilipe Manana goto out; 9724a500fd1SYan, Zheng 973a429e513SChris Mason if (ret == -EFBIG) { 974a429e513SChris Mason u32 item_size; 975a429e513SChris Mason /* we found one, but it isn't big enough yet */ 9765f39d397SChris Mason leaf = path->nodes[0]; 9775f39d397SChris Mason item_size = btrfs_item_size_nr(leaf, path->slots[0]); 978607d432dSJosef Bacik if ((item_size / csum_size) >= 9790b246afaSJeff Mahoney MAX_CSUM_ITEMS(fs_info, csum_size)) { 980a429e513SChris Mason /* already at max size, make a new one */ 981a429e513SChris Mason goto insert; 982a429e513SChris Mason } 983a429e513SChris Mason } else { 984f578d4bdSChris Mason int slot = path->slots[0] + 1; 985a429e513SChris Mason /* we didn't find a csum item, insert one */ 986f578d4bdSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 98735045bf2SFilipe Manana if (!nritems || (path->slots[0] >= nritems - 1)) { 988f578d4bdSChris Mason ret = btrfs_next_leaf(root, path); 9897e4a3f7eSFilipe Manana if (ret < 0) { 9907e4a3f7eSFilipe Manana goto out; 9917e4a3f7eSFilipe Manana } else if (ret > 0) { 992f578d4bdSChris Mason found_next = 1; 993f578d4bdSChris Mason goto insert; 9947e4a3f7eSFilipe Manana } 99527b9a812SFilipe Manana slot = path->slots[0]; 996f578d4bdSChris Mason } 997f578d4bdSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); 998d20f7043SChris Mason if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 999d20f7043SChris Mason found_key.type != BTRFS_EXTENT_CSUM_KEY) { 1000f578d4bdSChris Mason found_next = 1; 1001f578d4bdSChris Mason goto insert; 1002f578d4bdSChris Mason } 1003f578d4bdSChris Mason next_offset = found_key.offset; 1004f578d4bdSChris Mason found_next = 1; 1005a429e513SChris Mason goto insert; 1006a429e513SChris Mason } 1007a429e513SChris Mason 1008a429e513SChris Mason /* 1009cc14600cSFilipe Manana * At this point, we know the tree has a checksum item that ends at an 1010cc14600cSFilipe Manana * offset matching the start of the checksum range we want to insert. 1011cc14600cSFilipe Manana * We try to extend that item as much as possible and then add as many 1012cc14600cSFilipe Manana * checksums to it as they fit. 1013cc14600cSFilipe Manana * 1014cc14600cSFilipe Manana * First check if the leaf has enough free space for at least one 1015cc14600cSFilipe Manana * checksum. If it has go directly to the item extension code, otherwise 1016cc14600cSFilipe Manana * release the path and do a search for insertion before the extension. 1017a429e513SChris Mason */ 1018cc14600cSFilipe Manana if (btrfs_leaf_free_space(leaf) >= csum_size) { 1019cc14600cSFilipe Manana btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 1020cc14600cSFilipe Manana csum_offset = (bytenr - found_key.offset) >> 1021265fdfa6SDavid Sterba fs_info->sectorsize_bits; 1022cc14600cSFilipe Manana goto extend_csum; 1023cc14600cSFilipe Manana } 1024cc14600cSFilipe Manana 1025b3b4aa74SDavid Sterba btrfs_release_path(path); 10269a664971Sethanwu path->search_for_extension = 1; 10276567e837SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 1028607d432dSJosef Bacik csum_size, 1); 10299a664971Sethanwu path->search_for_extension = 0; 10306567e837SChris Mason if (ret < 0) 1031918cdf44SFilipe Manana goto out; 1032459931ecSChris Mason 1033459931ecSChris Mason if (ret > 0) { 1034459931ecSChris Mason if (path->slots[0] == 0) 10356567e837SChris Mason goto insert; 10366567e837SChris Mason path->slots[0]--; 1037459931ecSChris Mason } 1038459931ecSChris Mason 10395f39d397SChris Mason leaf = path->nodes[0]; 10405f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 1041265fdfa6SDavid Sterba csum_offset = (bytenr - found_key.offset) >> fs_info->sectorsize_bits; 1042459931ecSChris Mason 1043962a298fSDavid Sterba if (found_key.type != BTRFS_EXTENT_CSUM_KEY || 1044d20f7043SChris Mason found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 10450b246afaSJeff Mahoney csum_offset >= MAX_CSUM_ITEMS(fs_info, csum_size)) { 10466567e837SChris Mason goto insert; 10476567e837SChris Mason } 1048459931ecSChris Mason 1049cc14600cSFilipe Manana extend_csum: 10502f697dc6SLiu Bo if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) / 1051607d432dSJosef Bacik csum_size) { 10522f697dc6SLiu Bo int extend_nr; 10532f697dc6SLiu Bo u64 tmp; 10542f697dc6SLiu Bo u32 diff; 1055459931ecSChris Mason 1056f51a4a18SMiao Xie tmp = sums->len - total_bytes; 1057265fdfa6SDavid Sterba tmp >>= fs_info->sectorsize_bits; 10582f697dc6SLiu Bo WARN_ON(tmp < 1); 10592f697dc6SLiu Bo 10602f697dc6SLiu Bo extend_nr = max_t(int, 1, (int)tmp); 10612f697dc6SLiu Bo diff = (csum_offset + extend_nr) * csum_size; 10620b246afaSJeff Mahoney diff = min(diff, 10630b246afaSJeff Mahoney MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size); 1064459931ecSChris Mason 10655f39d397SChris Mason diff = diff - btrfs_item_size_nr(leaf, path->slots[0]); 1066cc14600cSFilipe Manana diff = min_t(u32, btrfs_leaf_free_space(leaf), diff); 10672f697dc6SLiu Bo diff /= csum_size; 10682f697dc6SLiu Bo diff *= csum_size; 1069459931ecSChris Mason 1070c71dd880SDavid Sterba btrfs_extend_item(path, diff); 1071f51a4a18SMiao Xie ret = 0; 10726567e837SChris Mason goto csum; 10736567e837SChris Mason } 10746567e837SChris Mason 10756567e837SChris Mason insert: 1076b3b4aa74SDavid Sterba btrfs_release_path(path); 10776567e837SChris Mason csum_offset = 0; 1078f578d4bdSChris Mason if (found_next) { 10792f697dc6SLiu Bo u64 tmp; 1080d20f7043SChris Mason 1081f51a4a18SMiao Xie tmp = sums->len - total_bytes; 1082265fdfa6SDavid Sterba tmp >>= fs_info->sectorsize_bits; 10832f697dc6SLiu Bo tmp = min(tmp, (next_offset - file_key.offset) >> 1084265fdfa6SDavid Sterba fs_info->sectorsize_bits); 10852f697dc6SLiu Bo 108650d0446eSSeraphime Kirkovski tmp = max_t(u64, 1, tmp); 108750d0446eSSeraphime Kirkovski tmp = min_t(u64, tmp, MAX_CSUM_ITEMS(fs_info, csum_size)); 1088607d432dSJosef Bacik ins_size = csum_size * tmp; 1089f578d4bdSChris Mason } else { 1090607d432dSJosef Bacik ins_size = csum_size; 1091f578d4bdSChris Mason } 10925caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 1093f578d4bdSChris Mason ins_size); 109454aa1f4dSChris Mason if (ret < 0) 1095918cdf44SFilipe Manana goto out; 1096fae7f21cSDulshani Gunawardhana if (WARN_ON(ret != 0)) 1097918cdf44SFilipe Manana goto out; 10985f39d397SChris Mason leaf = path->nodes[0]; 1099f51a4a18SMiao Xie csum: 11005f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 1101f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((unsigned char *)item + 1102f51a4a18SMiao Xie btrfs_item_size_nr(leaf, path->slots[0])); 1103509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 1104607d432dSJosef Bacik csum_offset * csum_size); 1105b18c6685SChris Mason found: 1106265fdfa6SDavid Sterba ins_size = (u32)(sums->len - total_bytes) >> fs_info->sectorsize_bits; 1107f51a4a18SMiao Xie ins_size *= csum_size; 1108f51a4a18SMiao Xie ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item, 1109f51a4a18SMiao Xie ins_size); 1110f51a4a18SMiao Xie write_extent_buffer(leaf, sums->sums + index, (unsigned long)item, 1111f51a4a18SMiao Xie ins_size); 1112aadfeb6eSChris Mason 11131e25a2e3SJohannes Thumshirn index += ins_size; 1114f51a4a18SMiao Xie ins_size /= csum_size; 11150b246afaSJeff Mahoney total_bytes += ins_size * fs_info->sectorsize; 1116a6591715SChris Mason 11175caf2a00SChris Mason btrfs_mark_buffer_dirty(path->nodes[0]); 1118e6dcd2dcSChris Mason if (total_bytes < sums->len) { 1119b3b4aa74SDavid Sterba btrfs_release_path(path); 1120b9473439SChris Mason cond_resched(); 1121065631f6SChris Mason goto again; 1122065631f6SChris Mason } 112353863232SChris Mason out: 11245caf2a00SChris Mason btrfs_free_path(path); 1125f254e52cSChris Mason return ret; 1126f254e52cSChris Mason } 11277ffbb598SFilipe Manana 11289cdc5124SNikolay Borisov void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, 11297ffbb598SFilipe Manana const struct btrfs_path *path, 11307ffbb598SFilipe Manana struct btrfs_file_extent_item *fi, 11317ffbb598SFilipe Manana const bool new_inline, 11327ffbb598SFilipe Manana struct extent_map *em) 11337ffbb598SFilipe Manana { 11343ffbd68cSDavid Sterba struct btrfs_fs_info *fs_info = inode->root->fs_info; 11359cdc5124SNikolay Borisov struct btrfs_root *root = inode->root; 11367ffbb598SFilipe Manana struct extent_buffer *leaf = path->nodes[0]; 11377ffbb598SFilipe Manana const int slot = path->slots[0]; 11387ffbb598SFilipe Manana struct btrfs_key key; 11397ffbb598SFilipe Manana u64 extent_start, extent_end; 11407ffbb598SFilipe Manana u64 bytenr; 11417ffbb598SFilipe Manana u8 type = btrfs_file_extent_type(leaf, fi); 11427ffbb598SFilipe Manana int compress_type = btrfs_file_extent_compression(leaf, fi); 11437ffbb598SFilipe Manana 11447ffbb598SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot); 11457ffbb598SFilipe Manana extent_start = key.offset; 1146a5eeb3d1SFilipe Manana extent_end = btrfs_file_extent_end(path); 11477ffbb598SFilipe Manana em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); 11487ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_REG || 11497ffbb598SFilipe Manana type == BTRFS_FILE_EXTENT_PREALLOC) { 11507ffbb598SFilipe Manana em->start = extent_start; 11517ffbb598SFilipe Manana em->len = extent_end - extent_start; 11527ffbb598SFilipe Manana em->orig_start = extent_start - 11537ffbb598SFilipe Manana btrfs_file_extent_offset(leaf, fi); 11547ffbb598SFilipe Manana em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); 11557ffbb598SFilipe Manana bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); 11567ffbb598SFilipe Manana if (bytenr == 0) { 11577ffbb598SFilipe Manana em->block_start = EXTENT_MAP_HOLE; 11587ffbb598SFilipe Manana return; 11597ffbb598SFilipe Manana } 11607ffbb598SFilipe Manana if (compress_type != BTRFS_COMPRESS_NONE) { 11617ffbb598SFilipe Manana set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 11627ffbb598SFilipe Manana em->compress_type = compress_type; 11637ffbb598SFilipe Manana em->block_start = bytenr; 11647ffbb598SFilipe Manana em->block_len = em->orig_block_len; 11657ffbb598SFilipe Manana } else { 11667ffbb598SFilipe Manana bytenr += btrfs_file_extent_offset(leaf, fi); 11677ffbb598SFilipe Manana em->block_start = bytenr; 11687ffbb598SFilipe Manana em->block_len = em->len; 11697ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_PREALLOC) 11707ffbb598SFilipe Manana set_bit(EXTENT_FLAG_PREALLOC, &em->flags); 11717ffbb598SFilipe Manana } 11727ffbb598SFilipe Manana } else if (type == BTRFS_FILE_EXTENT_INLINE) { 11737ffbb598SFilipe Manana em->block_start = EXTENT_MAP_INLINE; 11747ffbb598SFilipe Manana em->start = extent_start; 11757ffbb598SFilipe Manana em->len = extent_end - extent_start; 11767ffbb598SFilipe Manana /* 11777ffbb598SFilipe Manana * Initialize orig_start and block_len with the same values 11787ffbb598SFilipe Manana * as in inode.c:btrfs_get_extent(). 11797ffbb598SFilipe Manana */ 11807ffbb598SFilipe Manana em->orig_start = EXTENT_MAP_HOLE; 11817ffbb598SFilipe Manana em->block_len = (u64)-1; 11827ffbb598SFilipe Manana if (!new_inline && compress_type != BTRFS_COMPRESS_NONE) { 11837ffbb598SFilipe Manana set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 11847ffbb598SFilipe Manana em->compress_type = compress_type; 11857ffbb598SFilipe Manana } 11867ffbb598SFilipe Manana } else { 11870b246afaSJeff Mahoney btrfs_err(fs_info, 11889cdc5124SNikolay Borisov "unknown file extent item type %d, inode %llu, offset %llu, " 11899cdc5124SNikolay Borisov "root %llu", type, btrfs_ino(inode), extent_start, 11907ffbb598SFilipe Manana root->root_key.objectid); 11917ffbb598SFilipe Manana } 11927ffbb598SFilipe Manana } 1193a5eeb3d1SFilipe Manana 1194a5eeb3d1SFilipe Manana /* 1195a5eeb3d1SFilipe Manana * Returns the end offset (non inclusive) of the file extent item the given path 1196a5eeb3d1SFilipe Manana * points to. If it points to an inline extent, the returned offset is rounded 1197a5eeb3d1SFilipe Manana * up to the sector size. 1198a5eeb3d1SFilipe Manana */ 1199a5eeb3d1SFilipe Manana u64 btrfs_file_extent_end(const struct btrfs_path *path) 1200a5eeb3d1SFilipe Manana { 1201a5eeb3d1SFilipe Manana const struct extent_buffer *leaf = path->nodes[0]; 1202a5eeb3d1SFilipe Manana const int slot = path->slots[0]; 1203a5eeb3d1SFilipe Manana struct btrfs_file_extent_item *fi; 1204a5eeb3d1SFilipe Manana struct btrfs_key key; 1205a5eeb3d1SFilipe Manana u64 end; 1206a5eeb3d1SFilipe Manana 1207a5eeb3d1SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot); 1208a5eeb3d1SFilipe Manana ASSERT(key.type == BTRFS_EXTENT_DATA_KEY); 1209a5eeb3d1SFilipe Manana fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); 1210a5eeb3d1SFilipe Manana 1211a5eeb3d1SFilipe Manana if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) { 1212a5eeb3d1SFilipe Manana end = btrfs_file_extent_ram_bytes(leaf, fi); 1213a5eeb3d1SFilipe Manana end = ALIGN(key.offset + end, leaf->fs_info->sectorsize); 1214a5eeb3d1SFilipe Manana } else { 1215a5eeb3d1SFilipe Manana end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); 1216a5eeb3d1SFilipe Manana } 1217a5eeb3d1SFilipe Manana 1218a5eeb3d1SFilipe Manana return end; 1219a5eeb3d1SFilipe Manana } 1220