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> 121e1d2701SChris Mason #include "ctree.h" 13dee26a9fSChris Mason #include "disk-io.h" 149f5fae2fSChris Mason #include "transaction.h" 15facc8a22SMiao Xie #include "volumes.h" 161de037a4SChris Mason #include "print-tree.h" 17ebb8765bSAnand Jain #include "compression.h" 181e1d2701SChris Mason 1942049bf6SChris Mason #define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \ 2042049bf6SChris Mason sizeof(struct btrfs_item) * 2) / \ 2142049bf6SChris Mason size) - 1)) 2207d400a6SYan Zheng 23221b8318SZach Brown #define MAX_CSUM_ITEMS(r, size) (min_t(u32, __MAX_CSUM_ITEMS(r, size), \ 2409cbfeafSKirill A. Shutemov PAGE_SIZE)) 257ca4be45SChris Mason 2641a2ee75SJosef Bacik /** 2741a2ee75SJosef Bacik * @inode - the inode we want to update the disk_i_size for 2841a2ee75SJosef Bacik * @new_i_size - the i_size we want to set to, 0 if we use i_size 2941a2ee75SJosef Bacik * 3041a2ee75SJosef Bacik * With NO_HOLES set this simply sets the disk_is_size to whatever i_size_read() 3141a2ee75SJosef Bacik * returns as it is perfectly fine with a file that has holes without hole file 3241a2ee75SJosef Bacik * extent items. 3341a2ee75SJosef Bacik * 3441a2ee75SJosef Bacik * However without NO_HOLES we need to only return the area that is contiguous 3541a2ee75SJosef Bacik * from the 0 offset of the file. Otherwise we could end up adjust i_size up 3641a2ee75SJosef Bacik * to an extent that has a gap in between. 3741a2ee75SJosef Bacik * 3841a2ee75SJosef Bacik * Finally new_i_size should only be set in the case of truncate where we're not 3941a2ee75SJosef Bacik * ready to use i_size_read() as the limiter yet. 4041a2ee75SJosef Bacik */ 4176aea537SNikolay Borisov void btrfs_inode_safe_disk_i_size_write(struct btrfs_inode *inode, u64 new_i_size) 4241a2ee75SJosef Bacik { 4376aea537SNikolay Borisov struct btrfs_fs_info *fs_info = inode->root->fs_info; 4441a2ee75SJosef Bacik u64 start, end, i_size; 4541a2ee75SJosef Bacik int ret; 4641a2ee75SJosef Bacik 4776aea537SNikolay Borisov i_size = new_i_size ?: i_size_read(&inode->vfs_inode); 4841a2ee75SJosef Bacik if (btrfs_fs_incompat(fs_info, NO_HOLES)) { 4976aea537SNikolay Borisov inode->disk_i_size = i_size; 5041a2ee75SJosef Bacik return; 5141a2ee75SJosef Bacik } 5241a2ee75SJosef Bacik 5376aea537SNikolay Borisov spin_lock(&inode->lock); 5476aea537SNikolay Borisov ret = find_contiguous_extent_bit(&inode->file_extent_tree, 0, &start, 5576aea537SNikolay Borisov &end, EXTENT_DIRTY); 5641a2ee75SJosef Bacik if (!ret && start == 0) 5741a2ee75SJosef Bacik i_size = min(i_size, end + 1); 5841a2ee75SJosef Bacik else 5941a2ee75SJosef Bacik i_size = 0; 6076aea537SNikolay Borisov inode->disk_i_size = i_size; 6176aea537SNikolay Borisov spin_unlock(&inode->lock); 6241a2ee75SJosef Bacik } 6341a2ee75SJosef Bacik 6441a2ee75SJosef Bacik /** 6541a2ee75SJosef Bacik * @inode - the inode we're modifying 6641a2ee75SJosef Bacik * @start - the start file offset of the file extent we've inserted 6741a2ee75SJosef Bacik * @len - the logical length of the file extent item 6841a2ee75SJosef Bacik * 6941a2ee75SJosef Bacik * Call when we are inserting a new file extent where there was none before. 7041a2ee75SJosef Bacik * Does not need to call this in the case where we're replacing an existing file 7141a2ee75SJosef Bacik * extent, however if not sure it's fine to call this multiple times. 7241a2ee75SJosef Bacik * 7341a2ee75SJosef Bacik * The start and len must match the file extent item, so thus must be sectorsize 7441a2ee75SJosef Bacik * aligned. 7541a2ee75SJosef Bacik */ 7641a2ee75SJosef Bacik int btrfs_inode_set_file_extent_range(struct btrfs_inode *inode, u64 start, 7741a2ee75SJosef Bacik u64 len) 7841a2ee75SJosef Bacik { 7941a2ee75SJosef Bacik if (len == 0) 8041a2ee75SJosef Bacik return 0; 8141a2ee75SJosef Bacik 8241a2ee75SJosef Bacik ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize)); 8341a2ee75SJosef Bacik 8441a2ee75SJosef Bacik if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES)) 8541a2ee75SJosef Bacik return 0; 8641a2ee75SJosef Bacik return set_extent_bits(&inode->file_extent_tree, start, start + len - 1, 8741a2ee75SJosef Bacik EXTENT_DIRTY); 8841a2ee75SJosef Bacik } 8941a2ee75SJosef Bacik 9041a2ee75SJosef Bacik /** 9141a2ee75SJosef Bacik * @inode - the inode we're modifying 9241a2ee75SJosef Bacik * @start - the start file offset of the file extent we've inserted 9341a2ee75SJosef Bacik * @len - the logical length of the file extent item 9441a2ee75SJosef Bacik * 9541a2ee75SJosef Bacik * Called when we drop a file extent, for example when we truncate. Doesn't 9641a2ee75SJosef Bacik * need to be called for cases where we're replacing a file extent, like when 9741a2ee75SJosef Bacik * we've COWed a file extent. 9841a2ee75SJosef Bacik * 9941a2ee75SJosef Bacik * The start and len must match the file extent item, so thus must be sectorsize 10041a2ee75SJosef Bacik * aligned. 10141a2ee75SJosef Bacik */ 10241a2ee75SJosef Bacik int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start, 10341a2ee75SJosef Bacik u64 len) 10441a2ee75SJosef Bacik { 10541a2ee75SJosef Bacik if (len == 0) 10641a2ee75SJosef Bacik return 0; 10741a2ee75SJosef Bacik 10841a2ee75SJosef Bacik ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize) || 10941a2ee75SJosef Bacik len == (u64)-1); 11041a2ee75SJosef Bacik 11141a2ee75SJosef Bacik if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES)) 11241a2ee75SJosef Bacik return 0; 11341a2ee75SJosef Bacik return clear_extent_bit(&inode->file_extent_tree, start, 11441a2ee75SJosef Bacik start + len - 1, EXTENT_DIRTY, 0, 0, NULL); 11541a2ee75SJosef Bacik } 11641a2ee75SJosef Bacik 1171e25a2e3SJohannes Thumshirn static inline u32 max_ordered_sum_bytes(struct btrfs_fs_info *fs_info, 1181e25a2e3SJohannes Thumshirn u16 csum_size) 1191e25a2e3SJohannes Thumshirn { 1201e25a2e3SJohannes Thumshirn u32 ncsums = (PAGE_SIZE - sizeof(struct btrfs_ordered_sum)) / csum_size; 1211e25a2e3SJohannes Thumshirn 1221e25a2e3SJohannes Thumshirn return ncsums * fs_info->sectorsize; 1231e25a2e3SJohannes Thumshirn } 12407d400a6SYan Zheng 125b18c6685SChris Mason int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, 126dee26a9fSChris Mason struct btrfs_root *root, 127b18c6685SChris Mason u64 objectid, u64 pos, 128f2eb0a24SSage Weil u64 disk_offset, u64 disk_num_bytes, 129c8b97818SChris Mason u64 num_bytes, u64 offset, u64 ram_bytes, 130c8b97818SChris Mason u8 compression, u8 encryption, u16 other_encoding) 1319f5fae2fSChris Mason { 132dee26a9fSChris Mason int ret = 0; 133dee26a9fSChris Mason struct btrfs_file_extent_item *item; 134dee26a9fSChris Mason struct btrfs_key file_key; 1355caf2a00SChris Mason struct btrfs_path *path; 1365f39d397SChris Mason struct extent_buffer *leaf; 137dee26a9fSChris Mason 1385caf2a00SChris Mason path = btrfs_alloc_path(); 139db5b493aSTsutomu Itoh if (!path) 140db5b493aSTsutomu Itoh return -ENOMEM; 141dee26a9fSChris Mason file_key.objectid = objectid; 142b18c6685SChris Mason file_key.offset = pos; 143962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_DATA_KEY; 144dee26a9fSChris Mason 1455caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 146dee26a9fSChris Mason sizeof(*item)); 14754aa1f4dSChris Mason if (ret < 0) 14854aa1f4dSChris Mason goto out; 14979787eaaSJeff Mahoney BUG_ON(ret); /* Can't happen */ 1505f39d397SChris Mason leaf = path->nodes[0]; 1515f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], 152dee26a9fSChris Mason struct btrfs_file_extent_item); 153f2eb0a24SSage Weil btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset); 154db94535dSChris Mason btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes); 155f2eb0a24SSage Weil btrfs_set_file_extent_offset(leaf, item, offset); 156db94535dSChris Mason btrfs_set_file_extent_num_bytes(leaf, item, num_bytes); 157c8b97818SChris Mason btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes); 1585f39d397SChris Mason btrfs_set_file_extent_generation(leaf, item, trans->transid); 1595f39d397SChris Mason btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); 160c8b97818SChris Mason btrfs_set_file_extent_compression(leaf, item, compression); 161c8b97818SChris Mason btrfs_set_file_extent_encryption(leaf, item, encryption); 162c8b97818SChris Mason btrfs_set_file_extent_other_encoding(leaf, item, other_encoding); 163c8b97818SChris Mason 1645f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 16554aa1f4dSChris Mason out: 1665caf2a00SChris Mason btrfs_free_path(path); 16754aa1f4dSChris Mason return ret; 1689f5fae2fSChris Mason } 169dee26a9fSChris Mason 17048a3b636SEric Sandeen static struct btrfs_csum_item * 17148a3b636SEric Sandeen btrfs_lookup_csum(struct btrfs_trans_handle *trans, 172b18c6685SChris Mason struct btrfs_root *root, 1736567e837SChris Mason struct btrfs_path *path, 174d20f7043SChris Mason u64 bytenr, int cow) 1756567e837SChris Mason { 1760b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1776567e837SChris Mason int ret; 1786567e837SChris Mason struct btrfs_key file_key; 1796567e837SChris Mason struct btrfs_key found_key; 1806567e837SChris Mason struct btrfs_csum_item *item; 1815f39d397SChris Mason struct extent_buffer *leaf; 1826567e837SChris Mason u64 csum_offset = 0; 183223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 184a429e513SChris Mason int csums_in_item; 1856567e837SChris Mason 186d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 187d20f7043SChris Mason file_key.offset = bytenr; 188962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_CSUM_KEY; 189b18c6685SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); 1906567e837SChris Mason if (ret < 0) 1916567e837SChris Mason goto fail; 1925f39d397SChris Mason leaf = path->nodes[0]; 1936567e837SChris Mason if (ret > 0) { 1946567e837SChris Mason ret = 1; 19570b2befdSChris Mason if (path->slots[0] == 0) 1966567e837SChris Mason goto fail; 1976567e837SChris Mason path->slots[0]--; 1985f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 199962a298fSDavid Sterba if (found_key.type != BTRFS_EXTENT_CSUM_KEY) 2006567e837SChris Mason goto fail; 201d20f7043SChris Mason 202d20f7043SChris Mason csum_offset = (bytenr - found_key.offset) >> 203265fdfa6SDavid Sterba fs_info->sectorsize_bits; 2045f39d397SChris Mason csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]); 205607d432dSJosef Bacik csums_in_item /= csum_size; 206a429e513SChris Mason 20782d130ffSMiao Xie if (csum_offset == csums_in_item) { 208a429e513SChris Mason ret = -EFBIG; 2096567e837SChris Mason goto fail; 21082d130ffSMiao Xie } else if (csum_offset > csums_in_item) { 21182d130ffSMiao Xie goto fail; 2126567e837SChris Mason } 2136567e837SChris Mason } 2146567e837SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 215509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 216607d432dSJosef Bacik csum_offset * csum_size); 2176567e837SChris Mason return item; 2186567e837SChris Mason fail: 2196567e837SChris Mason if (ret > 0) 220b18c6685SChris Mason ret = -ENOENT; 2216567e837SChris Mason return ERR_PTR(ret); 2226567e837SChris Mason } 2236567e837SChris Mason 224dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, 225dee26a9fSChris Mason struct btrfs_root *root, 226dee26a9fSChris Mason struct btrfs_path *path, u64 objectid, 2279773a788SChris Mason u64 offset, int mod) 228dee26a9fSChris Mason { 229dee26a9fSChris Mason int ret; 230dee26a9fSChris Mason struct btrfs_key file_key; 231dee26a9fSChris Mason int ins_len = mod < 0 ? -1 : 0; 232dee26a9fSChris Mason int cow = mod != 0; 233dee26a9fSChris Mason 234dee26a9fSChris Mason file_key.objectid = objectid; 23570b2befdSChris Mason file_key.offset = offset; 236962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_DATA_KEY; 237dee26a9fSChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); 238dee26a9fSChris Mason return ret; 239dee26a9fSChris Mason } 240f254e52cSChris Mason 241*6275193eSQu Wenruo /* 242*6275193eSQu Wenruo * Find checksums for logical bytenr range [disk_bytenr, disk_bytenr + len) and 243*6275193eSQu Wenruo * estore the result to @dst. 244*6275193eSQu Wenruo * 245*6275193eSQu Wenruo * Return >0 for the number of sectors we found. 246*6275193eSQu Wenruo * Return 0 for the range [disk_bytenr, disk_bytenr + sectorsize) has no csum 247*6275193eSQu Wenruo * for it. Caller may want to try next sector until one range is hit. 248*6275193eSQu Wenruo * Return <0 for fatal error. 249*6275193eSQu Wenruo */ 250*6275193eSQu Wenruo static int search_csum_tree(struct btrfs_fs_info *fs_info, 251*6275193eSQu Wenruo struct btrfs_path *path, u64 disk_bytenr, 252*6275193eSQu Wenruo u64 len, u8 *dst) 253*6275193eSQu Wenruo { 254*6275193eSQu Wenruo struct btrfs_csum_item *item = NULL; 255*6275193eSQu Wenruo struct btrfs_key key; 256*6275193eSQu Wenruo const u32 sectorsize = fs_info->sectorsize; 257*6275193eSQu Wenruo const u32 csum_size = fs_info->csum_size; 258*6275193eSQu Wenruo u32 itemsize; 259*6275193eSQu Wenruo int ret; 260*6275193eSQu Wenruo u64 csum_start; 261*6275193eSQu Wenruo u64 csum_len; 262*6275193eSQu Wenruo 263*6275193eSQu Wenruo ASSERT(IS_ALIGNED(disk_bytenr, sectorsize) && 264*6275193eSQu Wenruo IS_ALIGNED(len, sectorsize)); 265*6275193eSQu Wenruo 266*6275193eSQu Wenruo /* Check if the current csum item covers disk_bytenr */ 267*6275193eSQu Wenruo if (path->nodes[0]) { 268*6275193eSQu Wenruo item = btrfs_item_ptr(path->nodes[0], path->slots[0], 269*6275193eSQu Wenruo struct btrfs_csum_item); 270*6275193eSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 271*6275193eSQu Wenruo itemsize = btrfs_item_size_nr(path->nodes[0], path->slots[0]); 272*6275193eSQu Wenruo 273*6275193eSQu Wenruo csum_start = key.offset; 274*6275193eSQu Wenruo csum_len = (itemsize / csum_size) * sectorsize; 275*6275193eSQu Wenruo 276*6275193eSQu Wenruo if (in_range(disk_bytenr, csum_start, csum_len)) 277*6275193eSQu Wenruo goto found; 278*6275193eSQu Wenruo } 279*6275193eSQu Wenruo 280*6275193eSQu Wenruo /* Current item doesn't contain the desired range, search again */ 281*6275193eSQu Wenruo btrfs_release_path(path); 282*6275193eSQu Wenruo item = btrfs_lookup_csum(NULL, fs_info->csum_root, path, disk_bytenr, 0); 283*6275193eSQu Wenruo if (IS_ERR(item)) { 284*6275193eSQu Wenruo ret = PTR_ERR(item); 285*6275193eSQu Wenruo goto out; 286*6275193eSQu Wenruo } 287*6275193eSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 288*6275193eSQu Wenruo itemsize = btrfs_item_size_nr(path->nodes[0], path->slots[0]); 289*6275193eSQu Wenruo 290*6275193eSQu Wenruo csum_start = key.offset; 291*6275193eSQu Wenruo csum_len = (itemsize / csum_size) * sectorsize; 292*6275193eSQu Wenruo ASSERT(in_range(disk_bytenr, csum_start, csum_len)); 293*6275193eSQu Wenruo 294*6275193eSQu Wenruo found: 295*6275193eSQu Wenruo ret = (min(csum_start + csum_len, disk_bytenr + len) - 296*6275193eSQu Wenruo disk_bytenr) >> fs_info->sectorsize_bits; 297*6275193eSQu Wenruo read_extent_buffer(path->nodes[0], dst, (unsigned long)item, 298*6275193eSQu Wenruo ret * csum_size); 299*6275193eSQu Wenruo out: 300*6275193eSQu Wenruo if (ret == -ENOENT) 301*6275193eSQu Wenruo ret = 0; 302*6275193eSQu Wenruo return ret; 303*6275193eSQu Wenruo } 304*6275193eSQu Wenruo 305*6275193eSQu Wenruo /* 306*6275193eSQu Wenruo * Locate the file_offset of @cur_disk_bytenr of a @bio. 307*6275193eSQu Wenruo * 308*6275193eSQu Wenruo * Bio of btrfs represents read range of 309*6275193eSQu Wenruo * [bi_sector << 9, bi_sector << 9 + bi_size). 310*6275193eSQu Wenruo * Knowing this, we can iterate through each bvec to locate the page belong to 311*6275193eSQu Wenruo * @cur_disk_bytenr and get the file offset. 312*6275193eSQu Wenruo * 313*6275193eSQu Wenruo * @inode is used to determine if the bvec page really belongs to @inode. 314*6275193eSQu Wenruo * 315*6275193eSQu Wenruo * Return 0 if we can't find the file offset 316*6275193eSQu Wenruo * Return >0 if we find the file offset and restore it to @file_offset_ret 317*6275193eSQu Wenruo */ 318*6275193eSQu Wenruo static int search_file_offset_in_bio(struct bio *bio, struct inode *inode, 319*6275193eSQu Wenruo u64 disk_bytenr, u64 *file_offset_ret) 320*6275193eSQu Wenruo { 321*6275193eSQu Wenruo struct bvec_iter iter; 322*6275193eSQu Wenruo struct bio_vec bvec; 323*6275193eSQu Wenruo u64 cur = bio->bi_iter.bi_sector << SECTOR_SHIFT; 324*6275193eSQu Wenruo int ret = 0; 325*6275193eSQu Wenruo 326*6275193eSQu Wenruo bio_for_each_segment(bvec, bio, iter) { 327*6275193eSQu Wenruo struct page *page = bvec.bv_page; 328*6275193eSQu Wenruo 329*6275193eSQu Wenruo if (cur > disk_bytenr) 330*6275193eSQu Wenruo break; 331*6275193eSQu Wenruo if (cur + bvec.bv_len <= disk_bytenr) { 332*6275193eSQu Wenruo cur += bvec.bv_len; 333*6275193eSQu Wenruo continue; 334*6275193eSQu Wenruo } 335*6275193eSQu Wenruo ASSERT(in_range(disk_bytenr, cur, bvec.bv_len)); 336*6275193eSQu Wenruo if (page->mapping && page->mapping->host && 337*6275193eSQu Wenruo page->mapping->host == inode) { 338*6275193eSQu Wenruo ret = 1; 339*6275193eSQu Wenruo *file_offset_ret = page_offset(page) + bvec.bv_offset + 340*6275193eSQu Wenruo disk_bytenr - cur; 341*6275193eSQu Wenruo break; 342*6275193eSQu Wenruo } 343*6275193eSQu Wenruo } 344*6275193eSQu Wenruo return ret; 345*6275193eSQu Wenruo } 346*6275193eSQu Wenruo 347e62958fcSOmar Sandoval /** 348*6275193eSQu Wenruo * Lookup the checksum for the read bio in csum tree. 3499e46458aSQu Wenruo * 350e62958fcSOmar Sandoval * @inode: inode that the bio is for. 351fb30f470SOmar Sandoval * @bio: bio to look up. 352fb30f470SOmar Sandoval * @dst: Buffer of size nblocks * btrfs_super_csum_size() used to return 353fb30f470SOmar Sandoval * checksum (nblocks = bio->bi_iter.bi_size / fs_info->sectorsize). If 354fb30f470SOmar Sandoval * NULL, the checksum buffer is allocated and returned in 355fb30f470SOmar Sandoval * btrfs_io_bio(bio)->csum instead. 356e62958fcSOmar Sandoval * 357e62958fcSOmar Sandoval * Return: BLK_STS_RESOURCE if allocating memory fails, BLK_STS_OK otherwise. 358e62958fcSOmar Sandoval */ 359*6275193eSQu Wenruo blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, u8 *dst) 36061b49440SChris Mason { 3610b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 362facc8a22SMiao Xie struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 363facc8a22SMiao Xie struct btrfs_path *path; 364*6275193eSQu Wenruo const u32 sectorsize = fs_info->sectorsize; 365223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 366*6275193eSQu Wenruo u32 orig_len = bio->bi_iter.bi_size; 367*6275193eSQu Wenruo u64 orig_disk_bytenr = bio->bi_iter.bi_sector << SECTOR_SHIFT; 368*6275193eSQu Wenruo u64 cur_disk_bytenr; 369*6275193eSQu Wenruo u8 *csum; 370*6275193eSQu Wenruo const unsigned int nblocks = orig_len >> fs_info->sectorsize_bits; 371*6275193eSQu Wenruo int count = 0; 37261b49440SChris Mason 37342437a63SJosef Bacik if (!fs_info->csum_root || (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) 374334c16d8SJosef Bacik return BLK_STS_OK; 375334c16d8SJosef Bacik 3769e46458aSQu Wenruo /* 3779e46458aSQu Wenruo * This function is only called for read bio. 3789e46458aSQu Wenruo * 3799e46458aSQu Wenruo * This means two things: 3809e46458aSQu Wenruo * - All our csums should only be in csum tree 3819e46458aSQu Wenruo * No ordered extents csums, as ordered extents are only for write 3829e46458aSQu Wenruo * path. 383*6275193eSQu Wenruo * - No need to bother any other info from bvec 384*6275193eSQu Wenruo * Since we're looking up csums, the only important info is the 385*6275193eSQu Wenruo * disk_bytenr and the length, which can be extracted from bi_iter 386*6275193eSQu Wenruo * directly. 3879e46458aSQu Wenruo */ 3889e46458aSQu Wenruo ASSERT(bio_op(bio) == REQ_OP_READ); 38961b49440SChris Mason path = btrfs_alloc_path(); 390c2db1073STsutomu Itoh if (!path) 3914e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 392facc8a22SMiao Xie 393facc8a22SMiao Xie if (!dst) { 394fb30f470SOmar Sandoval struct btrfs_io_bio *btrfs_bio = btrfs_io_bio(bio); 395fb30f470SOmar Sandoval 396facc8a22SMiao Xie if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) { 39731fecccbSDavid Sterba btrfs_bio->csum = kmalloc_array(nblocks, csum_size, 39831fecccbSDavid Sterba GFP_NOFS); 39931fecccbSDavid Sterba if (!btrfs_bio->csum) { 400facc8a22SMiao Xie btrfs_free_path(path); 4014e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 402facc8a22SMiao Xie } 403facc8a22SMiao Xie } else { 404facc8a22SMiao Xie btrfs_bio->csum = btrfs_bio->csum_inline; 405facc8a22SMiao Xie } 406facc8a22SMiao Xie csum = btrfs_bio->csum; 407facc8a22SMiao Xie } else { 40810fe6ca8SJohannes Thumshirn csum = dst; 409facc8a22SMiao Xie } 410facc8a22SMiao Xie 41135478d05SQu Wenruo /* 41235478d05SQu Wenruo * If requested number of sectors is larger than one leaf can contain, 41335478d05SQu Wenruo * kick the readahead for csum tree. 41435478d05SQu Wenruo */ 41535478d05SQu Wenruo if (nblocks > fs_info->csums_per_leaf) 416e4058b54SDavid Sterba path->reada = READA_FORWARD; 41761b49440SChris Mason 4182cf8572dSChris Mason /* 4192cf8572dSChris Mason * the free space stuff is only read when it hasn't been 4202cf8572dSChris Mason * updated in the current transaction. So, we can safely 4212cf8572dSChris Mason * read from the commit root and sidestep a nasty deadlock 4222cf8572dSChris Mason * between reading the free space cache and updating the csum tree. 4232cf8572dSChris Mason */ 42470ddc553SNikolay Borisov if (btrfs_is_free_space_inode(BTRFS_I(inode))) { 4252cf8572dSChris Mason path->search_commit_root = 1; 426ddf23b3fSJosef Bacik path->skip_locking = 1; 427ddf23b3fSJosef Bacik } 4282cf8572dSChris Mason 429*6275193eSQu Wenruo for (cur_disk_bytenr = orig_disk_bytenr; 430*6275193eSQu Wenruo cur_disk_bytenr < orig_disk_bytenr + orig_len; 431*6275193eSQu Wenruo cur_disk_bytenr += (count * sectorsize)) { 432*6275193eSQu Wenruo u64 search_len = orig_disk_bytenr + orig_len - cur_disk_bytenr; 433*6275193eSQu Wenruo unsigned int sector_offset; 434*6275193eSQu Wenruo u8 *csum_dst; 435c40a3d38SChandan Rajendra 436*6275193eSQu Wenruo /* 437*6275193eSQu Wenruo * Although both cur_disk_bytenr and orig_disk_bytenr is u64, 438*6275193eSQu Wenruo * we're calculating the offset to the bio start. 439*6275193eSQu Wenruo * 440*6275193eSQu Wenruo * Bio size is limited to UINT_MAX, thus unsigned int is large 441*6275193eSQu Wenruo * enough to contain the raw result, not to mention the right 442*6275193eSQu Wenruo * shifted result. 443*6275193eSQu Wenruo */ 444*6275193eSQu Wenruo ASSERT(cur_disk_bytenr - orig_disk_bytenr < UINT_MAX); 445*6275193eSQu Wenruo sector_offset = (cur_disk_bytenr - orig_disk_bytenr) >> 446*6275193eSQu Wenruo fs_info->sectorsize_bits; 447*6275193eSQu Wenruo csum_dst = csum + sector_offset * csum_size; 4484989d277SChristoph Hellwig 449*6275193eSQu Wenruo count = search_csum_tree(fs_info, path, cur_disk_bytenr, 450*6275193eSQu Wenruo search_len, csum_dst); 451*6275193eSQu Wenruo if (count <= 0) { 452*6275193eSQu Wenruo /* 453*6275193eSQu Wenruo * Either we hit a critical error or we didn't find 454*6275193eSQu Wenruo * the csum. 455*6275193eSQu Wenruo * Either way, we put zero into the csums dst, and skip 456*6275193eSQu Wenruo * to the next sector. 457*6275193eSQu Wenruo */ 458*6275193eSQu Wenruo memset(csum_dst, 0, csum_size); 459e4100d98SMiao Xie count = 1; 460*6275193eSQu Wenruo 461*6275193eSQu Wenruo /* 462*6275193eSQu Wenruo * For data reloc inode, we need to mark the range 463*6275193eSQu Wenruo * NODATASUM so that balance won't report false csum 464*6275193eSQu Wenruo * error. 465*6275193eSQu Wenruo */ 46617d217feSYan Zheng if (BTRFS_I(inode)->root->root_key.objectid == 46717d217feSYan Zheng BTRFS_DATA_RELOC_TREE_OBJECTID) { 468*6275193eSQu Wenruo u64 file_offset; 469*6275193eSQu Wenruo int ret; 470*6275193eSQu Wenruo 471*6275193eSQu Wenruo ret = search_file_offset_in_bio(bio, inode, 472*6275193eSQu Wenruo cur_disk_bytenr, &file_offset); 473*6275193eSQu Wenruo if (ret) 474*6275193eSQu Wenruo set_extent_bits(io_tree, file_offset, 475*6275193eSQu Wenruo file_offset + sectorsize - 1, 476ceeb0ae7SDavid Sterba EXTENT_NODATASUM); 47717d217feSYan Zheng } else { 478*6275193eSQu Wenruo btrfs_warn_rl(fs_info, 479*6275193eSQu Wenruo "csum hole found for disk bytenr range [%llu, %llu)", 480*6275193eSQu Wenruo cur_disk_bytenr, cur_disk_bytenr + sectorsize); 48117d217feSYan Zheng } 4824989d277SChristoph Hellwig } 4834989d277SChristoph Hellwig } 4844989d277SChristoph Hellwig 48561b49440SChris Mason btrfs_free_path(path); 486e62958fcSOmar Sandoval return BLK_STS_OK; 4874b46fce2SJosef Bacik } 4884b46fce2SJosef Bacik 48917d217feSYan Zheng int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, 490a2de733cSArne Jansen struct list_head *list, int search_commit) 49117d217feSYan Zheng { 4920b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 49317d217feSYan Zheng struct btrfs_key key; 49417d217feSYan Zheng struct btrfs_path *path; 49517d217feSYan Zheng struct extent_buffer *leaf; 49617d217feSYan Zheng struct btrfs_ordered_sum *sums; 49717d217feSYan Zheng struct btrfs_csum_item *item; 4980678b618SMark Fasheh LIST_HEAD(tmplist); 49917d217feSYan Zheng unsigned long offset; 50017d217feSYan Zheng int ret; 50117d217feSYan Zheng size_t size; 50217d217feSYan Zheng u64 csum_end; 503223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 50417d217feSYan Zheng 5050b246afaSJeff Mahoney ASSERT(IS_ALIGNED(start, fs_info->sectorsize) && 5060b246afaSJeff Mahoney IS_ALIGNED(end + 1, fs_info->sectorsize)); 5074277a9c3SJosef Bacik 50817d217feSYan Zheng path = btrfs_alloc_path(); 509d8926bb3SMark Fasheh if (!path) 510d8926bb3SMark Fasheh return -ENOMEM; 51117d217feSYan Zheng 512a2de733cSArne Jansen if (search_commit) { 513a2de733cSArne Jansen path->skip_locking = 1; 514e4058b54SDavid Sterba path->reada = READA_FORWARD; 515a2de733cSArne Jansen path->search_commit_root = 1; 516a2de733cSArne Jansen } 517a2de733cSArne Jansen 51817d217feSYan Zheng key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 51917d217feSYan Zheng key.offset = start; 52017d217feSYan Zheng key.type = BTRFS_EXTENT_CSUM_KEY; 52117d217feSYan Zheng 52207d400a6SYan Zheng ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 52317d217feSYan Zheng if (ret < 0) 52417d217feSYan Zheng goto fail; 52517d217feSYan Zheng if (ret > 0 && path->slots[0] > 0) { 52617d217feSYan Zheng leaf = path->nodes[0]; 52717d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); 52817d217feSYan Zheng if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID && 52917d217feSYan Zheng key.type == BTRFS_EXTENT_CSUM_KEY) { 530265fdfa6SDavid Sterba offset = (start - key.offset) >> fs_info->sectorsize_bits; 53117d217feSYan Zheng if (offset * csum_size < 53217d217feSYan Zheng btrfs_item_size_nr(leaf, path->slots[0] - 1)) 53317d217feSYan Zheng path->slots[0]--; 53417d217feSYan Zheng } 53517d217feSYan Zheng } 53617d217feSYan Zheng 53717d217feSYan Zheng while (start <= end) { 53817d217feSYan Zheng leaf = path->nodes[0]; 53917d217feSYan Zheng if (path->slots[0] >= btrfs_header_nritems(leaf)) { 54007d400a6SYan Zheng ret = btrfs_next_leaf(root, path); 54117d217feSYan Zheng if (ret < 0) 54217d217feSYan Zheng goto fail; 54317d217feSYan Zheng if (ret > 0) 54417d217feSYan Zheng break; 54517d217feSYan Zheng leaf = path->nodes[0]; 54617d217feSYan Zheng } 54717d217feSYan Zheng 54817d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 54917d217feSYan Zheng if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 550628c8282SZhi Yong Wu key.type != BTRFS_EXTENT_CSUM_KEY || 551628c8282SZhi Yong Wu key.offset > end) 55217d217feSYan Zheng break; 55317d217feSYan Zheng 55417d217feSYan Zheng if (key.offset > start) 55517d217feSYan Zheng start = key.offset; 55617d217feSYan Zheng 55717d217feSYan Zheng size = btrfs_item_size_nr(leaf, path->slots[0]); 5580b246afaSJeff Mahoney csum_end = key.offset + (size / csum_size) * fs_info->sectorsize; 55987b29b20SYan Zheng if (csum_end <= start) { 56087b29b20SYan Zheng path->slots[0]++; 56187b29b20SYan Zheng continue; 56287b29b20SYan Zheng } 56317d217feSYan Zheng 56407d400a6SYan Zheng csum_end = min(csum_end, end + 1); 56507d400a6SYan Zheng item = btrfs_item_ptr(path->nodes[0], path->slots[0], 56607d400a6SYan Zheng struct btrfs_csum_item); 56707d400a6SYan Zheng while (start < csum_end) { 56807d400a6SYan Zheng size = min_t(size_t, csum_end - start, 5691e25a2e3SJohannes Thumshirn max_ordered_sum_bytes(fs_info, csum_size)); 5700b246afaSJeff Mahoney sums = kzalloc(btrfs_ordered_sum_size(fs_info, size), 57107d400a6SYan Zheng GFP_NOFS); 5720678b618SMark Fasheh if (!sums) { 5730678b618SMark Fasheh ret = -ENOMEM; 5740678b618SMark Fasheh goto fail; 5750678b618SMark Fasheh } 57617d217feSYan Zheng 57717d217feSYan Zheng sums->bytenr = start; 578f51a4a18SMiao Xie sums->len = (int)size; 57917d217feSYan Zheng 580265fdfa6SDavid Sterba offset = (start - key.offset) >> fs_info->sectorsize_bits; 58117d217feSYan Zheng offset *= csum_size; 582265fdfa6SDavid Sterba size >>= fs_info->sectorsize_bits; 58317d217feSYan Zheng 58407d400a6SYan Zheng read_extent_buffer(path->nodes[0], 585f51a4a18SMiao Xie sums->sums, 586f51a4a18SMiao Xie ((unsigned long)item) + offset, 587f51a4a18SMiao Xie csum_size * size); 58817d217feSYan Zheng 5890b246afaSJeff Mahoney start += fs_info->sectorsize * size; 5900678b618SMark Fasheh list_add_tail(&sums->list, &tmplist); 59107d400a6SYan Zheng } 59217d217feSYan Zheng path->slots[0]++; 59317d217feSYan Zheng } 59417d217feSYan Zheng ret = 0; 59517d217feSYan Zheng fail: 5960678b618SMark Fasheh while (ret < 0 && !list_empty(&tmplist)) { 5976e5aafb2SChris Mason sums = list_entry(tmplist.next, struct btrfs_ordered_sum, list); 5980678b618SMark Fasheh list_del(&sums->list); 5990678b618SMark Fasheh kfree(sums); 6000678b618SMark Fasheh } 6010678b618SMark Fasheh list_splice_tail(&tmplist, list); 6020678b618SMark Fasheh 60317d217feSYan Zheng btrfs_free_path(path); 60417d217feSYan Zheng return ret; 60517d217feSYan Zheng } 60617d217feSYan Zheng 60751d470aeSNikolay Borisov /* 60851d470aeSNikolay Borisov * btrfs_csum_one_bio - Calculates checksums of the data contained inside a bio 60951d470aeSNikolay Borisov * @inode: Owner of the data inside the bio 61051d470aeSNikolay Borisov * @bio: Contains the data to be checksummed 61151d470aeSNikolay Borisov * @file_start: offset in file this bio begins to describe 61251d470aeSNikolay Borisov * @contig: Boolean. If true/1 means all bio vecs in this bio are 61351d470aeSNikolay Borisov * contiguous and they begin at @file_start in the file. False/0 61451d470aeSNikolay Borisov * means this bio can contains potentially discontigous bio vecs 61551d470aeSNikolay Borisov * so the logical offset of each should be calculated separately. 61651d470aeSNikolay Borisov */ 617bd242a08SNikolay Borisov blk_status_t btrfs_csum_one_bio(struct btrfs_inode *inode, struct bio *bio, 6182ff7e61eSJeff Mahoney u64 file_start, int contig) 619e015640fSChris Mason { 620c3504372SNikolay Borisov struct btrfs_fs_info *fs_info = inode->root->fs_info; 621d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 622e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums; 6236cd7ce49SChristoph Hellwig struct btrfs_ordered_extent *ordered = NULL; 624e015640fSChris Mason char *data; 62517347cecSLiu Bo struct bvec_iter iter; 62617347cecSLiu Bo struct bio_vec bvec; 627f51a4a18SMiao Xie int index; 628c40a3d38SChandan Rajendra int nr_sectors; 6293edf7d33SChris Mason unsigned long total_bytes = 0; 6303edf7d33SChris Mason unsigned long this_sum_bytes = 0; 63117347cecSLiu Bo int i; 6323edf7d33SChris Mason u64 offset; 633a3d46aeaSNikolay Borisov unsigned nofs_flag; 634e015640fSChris Mason 635a3d46aeaSNikolay Borisov nofs_flag = memalloc_nofs_save(); 636a3d46aeaSNikolay Borisov sums = kvzalloc(btrfs_ordered_sum_size(fs_info, bio->bi_iter.bi_size), 637a3d46aeaSNikolay Borisov GFP_KERNEL); 638a3d46aeaSNikolay Borisov memalloc_nofs_restore(nofs_flag); 639a3d46aeaSNikolay Borisov 640e015640fSChris Mason if (!sums) 6414e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 6423edf7d33SChris Mason 6434f024f37SKent Overstreet sums->len = bio->bi_iter.bi_size; 644e6dcd2dcSChris Mason INIT_LIST_HEAD(&sums->list); 645d20f7043SChris Mason 646d20f7043SChris Mason if (contig) 647d20f7043SChris Mason offset = file_start; 648d20f7043SChris Mason else 6496cd7ce49SChristoph Hellwig offset = 0; /* shut up gcc */ 650d20f7043SChris Mason 6511201b58bSDavid Sterba sums->bytenr = bio->bi_iter.bi_sector << 9; 652f51a4a18SMiao Xie index = 0; 653e015640fSChris Mason 654d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 655d5178578SJohannes Thumshirn 65617347cecSLiu Bo bio_for_each_segment(bvec, bio, iter) { 657d20f7043SChris Mason if (!contig) 65817347cecSLiu Bo offset = page_offset(bvec.bv_page) + bvec.bv_offset; 659d20f7043SChris Mason 6606cd7ce49SChristoph Hellwig if (!ordered) { 6616cd7ce49SChristoph Hellwig ordered = btrfs_lookup_ordered_extent(inode, offset); 6626cd7ce49SChristoph Hellwig BUG_ON(!ordered); /* Logic error */ 6636cd7ce49SChristoph Hellwig } 6646cd7ce49SChristoph Hellwig 6650b246afaSJeff Mahoney nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, 66617347cecSLiu Bo bvec.bv_len + fs_info->sectorsize 667c40a3d38SChandan Rajendra - 1); 668c40a3d38SChandan Rajendra 669c40a3d38SChandan Rajendra for (i = 0; i < nr_sectors; i++) { 670bffe633eSOmar Sandoval if (offset >= ordered->file_offset + ordered->num_bytes || 671e58dd74bSJosef Bacik offset < ordered->file_offset) { 6723edf7d33SChris Mason unsigned long bytes_left; 673c40a3d38SChandan Rajendra 6743edf7d33SChris Mason sums->len = this_sum_bytes; 6753edf7d33SChris Mason this_sum_bytes = 0; 676f9756261SNikolay Borisov btrfs_add_ordered_sum(ordered, sums); 6773edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 6783edf7d33SChris Mason 6794f024f37SKent Overstreet bytes_left = bio->bi_iter.bi_size - total_bytes; 6803edf7d33SChris Mason 681a3d46aeaSNikolay Borisov nofs_flag = memalloc_nofs_save(); 682a3d46aeaSNikolay Borisov sums = kvzalloc(btrfs_ordered_sum_size(fs_info, 683a3d46aeaSNikolay Borisov bytes_left), GFP_KERNEL); 684a3d46aeaSNikolay Borisov memalloc_nofs_restore(nofs_flag); 68579787eaaSJeff Mahoney BUG_ON(!sums); /* -ENOMEM */ 6863edf7d33SChris Mason sums->len = bytes_left; 687c40a3d38SChandan Rajendra ordered = btrfs_lookup_ordered_extent(inode, 688c40a3d38SChandan Rajendra offset); 689c40a3d38SChandan Rajendra ASSERT(ordered); /* Logic error */ 6901201b58bSDavid Sterba sums->bytenr = (bio->bi_iter.bi_sector << 9) 691c40a3d38SChandan Rajendra + total_bytes; 692f51a4a18SMiao Xie index = 0; 693c40a3d38SChandan Rajendra } 694c40a3d38SChandan Rajendra 695443c8e2aSJohannes Thumshirn data = kmap_atomic(bvec.bv_page); 696fd08001fSEric Biggers crypto_shash_digest(shash, data + bvec.bv_offset 6970b246afaSJeff Mahoney + (i * fs_info->sectorsize), 698fd08001fSEric Biggers fs_info->sectorsize, 699fd08001fSEric Biggers sums->sums + index); 700443c8e2aSJohannes Thumshirn kunmap_atomic(data); 701713cebfbSDavid Sterba index += fs_info->csum_size; 7020b246afaSJeff Mahoney offset += fs_info->sectorsize; 7030b246afaSJeff Mahoney this_sum_bytes += fs_info->sectorsize; 7040b246afaSJeff Mahoney total_bytes += fs_info->sectorsize; 705c40a3d38SChandan Rajendra } 706c40a3d38SChandan Rajendra 707e015640fSChris Mason } 708ed98b56aSChris Mason this_sum_bytes = 0; 709f9756261SNikolay Borisov btrfs_add_ordered_sum(ordered, sums); 7103edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 711e015640fSChris Mason return 0; 712e015640fSChris Mason } 713e015640fSChris Mason 714459931ecSChris Mason /* 715459931ecSChris Mason * helper function for csum removal, this expects the 716459931ecSChris Mason * key to describe the csum pointed to by the path, and it expects 717459931ecSChris Mason * the csum to overlap the range [bytenr, len] 718459931ecSChris Mason * 719459931ecSChris Mason * The csum should not be entirely contained in the range and the 720459931ecSChris Mason * range should not be entirely contained in the csum. 721459931ecSChris Mason * 722459931ecSChris Mason * This calls btrfs_truncate_item with the correct args based on the 723459931ecSChris Mason * overlap, and fixes up the key as required. 724459931ecSChris Mason */ 7252ff7e61eSJeff Mahoney static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info, 726459931ecSChris Mason struct btrfs_path *path, 727459931ecSChris Mason struct btrfs_key *key, 728459931ecSChris Mason u64 bytenr, u64 len) 729459931ecSChris Mason { 730459931ecSChris Mason struct extent_buffer *leaf; 731223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 732459931ecSChris Mason u64 csum_end; 733459931ecSChris Mason u64 end_byte = bytenr + len; 734265fdfa6SDavid Sterba u32 blocksize_bits = fs_info->sectorsize_bits; 735459931ecSChris Mason 736459931ecSChris Mason leaf = path->nodes[0]; 737459931ecSChris Mason csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; 738265fdfa6SDavid Sterba csum_end <<= blocksize_bits; 739459931ecSChris Mason csum_end += key->offset; 740459931ecSChris Mason 741459931ecSChris Mason if (key->offset < bytenr && csum_end <= end_byte) { 742459931ecSChris Mason /* 743459931ecSChris Mason * [ bytenr - len ] 744459931ecSChris Mason * [ ] 745459931ecSChris Mason * [csum ] 746459931ecSChris Mason * A simple truncate off the end of the item 747459931ecSChris Mason */ 748459931ecSChris Mason u32 new_size = (bytenr - key->offset) >> blocksize_bits; 749459931ecSChris Mason new_size *= csum_size; 75078ac4f9eSDavid Sterba btrfs_truncate_item(path, new_size, 1); 751459931ecSChris Mason } else if (key->offset >= bytenr && csum_end > end_byte && 752459931ecSChris Mason end_byte > key->offset) { 753459931ecSChris Mason /* 754459931ecSChris Mason * [ bytenr - len ] 755459931ecSChris Mason * [ ] 756459931ecSChris Mason * [csum ] 757459931ecSChris Mason * we need to truncate from the beginning of the csum 758459931ecSChris Mason */ 759459931ecSChris Mason u32 new_size = (csum_end - end_byte) >> blocksize_bits; 760459931ecSChris Mason new_size *= csum_size; 761459931ecSChris Mason 76278ac4f9eSDavid Sterba btrfs_truncate_item(path, new_size, 0); 763459931ecSChris Mason 764459931ecSChris Mason key->offset = end_byte; 7650b246afaSJeff Mahoney btrfs_set_item_key_safe(fs_info, path, key); 766459931ecSChris Mason } else { 767459931ecSChris Mason BUG(); 768459931ecSChris Mason } 769459931ecSChris Mason } 770459931ecSChris Mason 771459931ecSChris Mason /* 772459931ecSChris Mason * deletes the csum items from the csum tree for a given 773459931ecSChris Mason * range of bytes. 774459931ecSChris Mason */ 775459931ecSChris Mason int btrfs_del_csums(struct btrfs_trans_handle *trans, 77640e046acSFilipe Manana struct btrfs_root *root, u64 bytenr, u64 len) 777459931ecSChris Mason { 77840e046acSFilipe Manana struct btrfs_fs_info *fs_info = trans->fs_info; 779459931ecSChris Mason struct btrfs_path *path; 780459931ecSChris Mason struct btrfs_key key; 781459931ecSChris Mason u64 end_byte = bytenr + len; 782459931ecSChris Mason u64 csum_end; 783459931ecSChris Mason struct extent_buffer *leaf; 784459931ecSChris Mason int ret; 785223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 786265fdfa6SDavid Sterba u32 blocksize_bits = fs_info->sectorsize_bits; 787459931ecSChris Mason 78840e046acSFilipe Manana ASSERT(root == fs_info->csum_root || 78940e046acSFilipe Manana root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); 79040e046acSFilipe Manana 791459931ecSChris Mason path = btrfs_alloc_path(); 7922a29edc6Sliubo if (!path) 7932a29edc6Sliubo return -ENOMEM; 794459931ecSChris Mason 795459931ecSChris Mason while (1) { 796459931ecSChris Mason key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 797459931ecSChris Mason key.offset = end_byte - 1; 798459931ecSChris Mason key.type = BTRFS_EXTENT_CSUM_KEY; 799459931ecSChris Mason 800459931ecSChris Mason ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 801459931ecSChris Mason if (ret > 0) { 802459931ecSChris Mason if (path->slots[0] == 0) 80365a246c5STsutomu Itoh break; 804459931ecSChris Mason path->slots[0]--; 805ad0397a7SJosef Bacik } else if (ret < 0) { 80665a246c5STsutomu Itoh break; 807459931ecSChris Mason } 808ad0397a7SJosef Bacik 809459931ecSChris Mason leaf = path->nodes[0]; 810459931ecSChris Mason btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 811459931ecSChris Mason 812459931ecSChris Mason if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 813459931ecSChris Mason key.type != BTRFS_EXTENT_CSUM_KEY) { 814459931ecSChris Mason break; 815459931ecSChris Mason } 816459931ecSChris Mason 817459931ecSChris Mason if (key.offset >= end_byte) 818459931ecSChris Mason break; 819459931ecSChris Mason 820459931ecSChris Mason csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; 821459931ecSChris Mason csum_end <<= blocksize_bits; 822459931ecSChris Mason csum_end += key.offset; 823459931ecSChris Mason 824459931ecSChris Mason /* this csum ends before we start, we're done */ 825459931ecSChris Mason if (csum_end <= bytenr) 826459931ecSChris Mason break; 827459931ecSChris Mason 828459931ecSChris Mason /* delete the entire item, it is inside our range */ 829459931ecSChris Mason if (key.offset >= bytenr && csum_end <= end_byte) { 8306f546216SFilipe Manana int del_nr = 1; 8316f546216SFilipe Manana 8326f546216SFilipe Manana /* 8336f546216SFilipe Manana * Check how many csum items preceding this one in this 8346f546216SFilipe Manana * leaf correspond to our range and then delete them all 8356f546216SFilipe Manana * at once. 8366f546216SFilipe Manana */ 8376f546216SFilipe Manana if (key.offset > bytenr && path->slots[0] > 0) { 8386f546216SFilipe Manana int slot = path->slots[0] - 1; 8396f546216SFilipe Manana 8406f546216SFilipe Manana while (slot >= 0) { 8416f546216SFilipe Manana struct btrfs_key pk; 8426f546216SFilipe Manana 8436f546216SFilipe Manana btrfs_item_key_to_cpu(leaf, &pk, slot); 8446f546216SFilipe Manana if (pk.offset < bytenr || 8456f546216SFilipe Manana pk.type != BTRFS_EXTENT_CSUM_KEY || 8466f546216SFilipe Manana pk.objectid != 8476f546216SFilipe Manana BTRFS_EXTENT_CSUM_OBJECTID) 8486f546216SFilipe Manana break; 8496f546216SFilipe Manana path->slots[0] = slot; 8506f546216SFilipe Manana del_nr++; 8516f546216SFilipe Manana key.offset = pk.offset; 8526f546216SFilipe Manana slot--; 8536f546216SFilipe Manana } 8546f546216SFilipe Manana } 8556f546216SFilipe Manana ret = btrfs_del_items(trans, root, path, 8566f546216SFilipe Manana path->slots[0], del_nr); 85765a246c5STsutomu Itoh if (ret) 85865a246c5STsutomu Itoh goto out; 859dcbdd4dcSChris Mason if (key.offset == bytenr) 860dcbdd4dcSChris Mason break; 861459931ecSChris Mason } else if (key.offset < bytenr && csum_end > end_byte) { 862459931ecSChris Mason unsigned long offset; 863459931ecSChris Mason unsigned long shift_len; 864459931ecSChris Mason unsigned long item_offset; 865459931ecSChris Mason /* 866459931ecSChris Mason * [ bytenr - len ] 867459931ecSChris Mason * [csum ] 868459931ecSChris Mason * 869459931ecSChris Mason * Our bytes are in the middle of the csum, 870459931ecSChris Mason * we need to split this item and insert a new one. 871459931ecSChris Mason * 872459931ecSChris Mason * But we can't drop the path because the 873459931ecSChris Mason * csum could change, get removed, extended etc. 874459931ecSChris Mason * 875459931ecSChris Mason * The trick here is the max size of a csum item leaves 876459931ecSChris Mason * enough room in the tree block for a single 877459931ecSChris Mason * item header. So, we split the item in place, 878459931ecSChris Mason * adding a new header pointing to the existing 879459931ecSChris Mason * bytes. Then we loop around again and we have 880459931ecSChris Mason * a nicely formed csum item that we can neatly 881459931ecSChris Mason * truncate. 882459931ecSChris Mason */ 883459931ecSChris Mason offset = (bytenr - key.offset) >> blocksize_bits; 884459931ecSChris Mason offset *= csum_size; 885459931ecSChris Mason 886459931ecSChris Mason shift_len = (len >> blocksize_bits) * csum_size; 887459931ecSChris Mason 888459931ecSChris Mason item_offset = btrfs_item_ptr_offset(leaf, 889459931ecSChris Mason path->slots[0]); 890459931ecSChris Mason 891b159fa28SDavid Sterba memzero_extent_buffer(leaf, item_offset + offset, 892459931ecSChris Mason shift_len); 893459931ecSChris Mason key.offset = bytenr; 894459931ecSChris Mason 895459931ecSChris Mason /* 896459931ecSChris Mason * btrfs_split_item returns -EAGAIN when the 897459931ecSChris Mason * item changed size or key 898459931ecSChris Mason */ 899459931ecSChris Mason ret = btrfs_split_item(trans, root, path, &key, offset); 90079787eaaSJeff Mahoney if (ret && ret != -EAGAIN) { 90166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 90279787eaaSJeff Mahoney goto out; 90379787eaaSJeff Mahoney } 904459931ecSChris Mason 905459931ecSChris Mason key.offset = end_byte - 1; 906459931ecSChris Mason } else { 9072ff7e61eSJeff Mahoney truncate_one_csum(fs_info, path, &key, bytenr, len); 908dcbdd4dcSChris Mason if (key.offset < bytenr) 909dcbdd4dcSChris Mason break; 910459931ecSChris Mason } 911b3b4aa74SDavid Sterba btrfs_release_path(path); 912459931ecSChris Mason } 91365a246c5STsutomu Itoh ret = 0; 914459931ecSChris Mason out: 915459931ecSChris Mason btrfs_free_path(path); 91665a246c5STsutomu Itoh return ret; 917459931ecSChris Mason } 918459931ecSChris Mason 919065631f6SChris Mason int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, 920d20f7043SChris Mason struct btrfs_root *root, 921e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums) 922f254e52cSChris Mason { 9230b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 924f254e52cSChris Mason struct btrfs_key file_key; 9256567e837SChris Mason struct btrfs_key found_key; 9265caf2a00SChris Mason struct btrfs_path *path; 927f254e52cSChris Mason struct btrfs_csum_item *item; 928065631f6SChris Mason struct btrfs_csum_item *item_end; 929ff79f819SChris Mason struct extent_buffer *leaf = NULL; 930f51a4a18SMiao Xie u64 next_offset; 931f51a4a18SMiao Xie u64 total_bytes = 0; 9326567e837SChris Mason u64 csum_offset; 933f51a4a18SMiao Xie u64 bytenr; 934f578d4bdSChris Mason u32 nritems; 935f578d4bdSChris Mason u32 ins_size; 936f51a4a18SMiao Xie int index = 0; 937f51a4a18SMiao Xie int found_next; 938f51a4a18SMiao Xie int ret; 939223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 9406e92f5e6SChris Mason 9415caf2a00SChris Mason path = btrfs_alloc_path(); 942d8926bb3SMark Fasheh if (!path) 943d8926bb3SMark Fasheh return -ENOMEM; 944065631f6SChris Mason again: 945065631f6SChris Mason next_offset = (u64)-1; 946065631f6SChris Mason found_next = 0; 947f51a4a18SMiao Xie bytenr = sums->bytenr + total_bytes; 948d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 949f51a4a18SMiao Xie file_key.offset = bytenr; 950962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_CSUM_KEY; 951a429e513SChris Mason 952f51a4a18SMiao Xie item = btrfs_lookup_csum(trans, root, path, bytenr, 1); 953ff79f819SChris Mason if (!IS_ERR(item)) { 954639cb586SChris Mason ret = 0; 955f51a4a18SMiao Xie leaf = path->nodes[0]; 956f51a4a18SMiao Xie item_end = btrfs_item_ptr(leaf, path->slots[0], 957f51a4a18SMiao Xie struct btrfs_csum_item); 958f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((char *)item_end + 959f51a4a18SMiao Xie btrfs_item_size_nr(leaf, path->slots[0])); 960a429e513SChris Mason goto found; 961ff79f819SChris Mason } 962a429e513SChris Mason ret = PTR_ERR(item); 9634a500fd1SYan, Zheng if (ret != -EFBIG && ret != -ENOENT) 964918cdf44SFilipe Manana goto out; 9654a500fd1SYan, Zheng 966a429e513SChris Mason if (ret == -EFBIG) { 967a429e513SChris Mason u32 item_size; 968a429e513SChris Mason /* we found one, but it isn't big enough yet */ 9695f39d397SChris Mason leaf = path->nodes[0]; 9705f39d397SChris Mason item_size = btrfs_item_size_nr(leaf, path->slots[0]); 971607d432dSJosef Bacik if ((item_size / csum_size) >= 9720b246afaSJeff Mahoney MAX_CSUM_ITEMS(fs_info, csum_size)) { 973a429e513SChris Mason /* already at max size, make a new one */ 974a429e513SChris Mason goto insert; 975a429e513SChris Mason } 976a429e513SChris Mason } else { 977f578d4bdSChris Mason int slot = path->slots[0] + 1; 978a429e513SChris Mason /* we didn't find a csum item, insert one */ 979f578d4bdSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 98035045bf2SFilipe Manana if (!nritems || (path->slots[0] >= nritems - 1)) { 981f578d4bdSChris Mason ret = btrfs_next_leaf(root, path); 9827e4a3f7eSFilipe Manana if (ret < 0) { 9837e4a3f7eSFilipe Manana goto out; 9847e4a3f7eSFilipe Manana } else if (ret > 0) { 985f578d4bdSChris Mason found_next = 1; 986f578d4bdSChris Mason goto insert; 9877e4a3f7eSFilipe Manana } 98827b9a812SFilipe Manana slot = path->slots[0]; 989f578d4bdSChris Mason } 990f578d4bdSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); 991d20f7043SChris Mason if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 992d20f7043SChris Mason found_key.type != BTRFS_EXTENT_CSUM_KEY) { 993f578d4bdSChris Mason found_next = 1; 994f578d4bdSChris Mason goto insert; 995f578d4bdSChris Mason } 996f578d4bdSChris Mason next_offset = found_key.offset; 997f578d4bdSChris Mason found_next = 1; 998a429e513SChris Mason goto insert; 999a429e513SChris Mason } 1000a429e513SChris Mason 1001a429e513SChris Mason /* 1002cc14600cSFilipe Manana * At this point, we know the tree has a checksum item that ends at an 1003cc14600cSFilipe Manana * offset matching the start of the checksum range we want to insert. 1004cc14600cSFilipe Manana * We try to extend that item as much as possible and then add as many 1005cc14600cSFilipe Manana * checksums to it as they fit. 1006cc14600cSFilipe Manana * 1007cc14600cSFilipe Manana * First check if the leaf has enough free space for at least one 1008cc14600cSFilipe Manana * checksum. If it has go directly to the item extension code, otherwise 1009cc14600cSFilipe Manana * release the path and do a search for insertion before the extension. 1010a429e513SChris Mason */ 1011cc14600cSFilipe Manana if (btrfs_leaf_free_space(leaf) >= csum_size) { 1012cc14600cSFilipe Manana btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 1013cc14600cSFilipe Manana csum_offset = (bytenr - found_key.offset) >> 1014265fdfa6SDavid Sterba fs_info->sectorsize_bits; 1015cc14600cSFilipe Manana goto extend_csum; 1016cc14600cSFilipe Manana } 1017cc14600cSFilipe Manana 1018b3b4aa74SDavid Sterba btrfs_release_path(path); 10196567e837SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 1020607d432dSJosef Bacik csum_size, 1); 10216567e837SChris Mason if (ret < 0) 1022918cdf44SFilipe Manana goto out; 1023459931ecSChris Mason 1024459931ecSChris Mason if (ret > 0) { 1025459931ecSChris Mason if (path->slots[0] == 0) 10266567e837SChris Mason goto insert; 10276567e837SChris Mason path->slots[0]--; 1028459931ecSChris Mason } 1029459931ecSChris Mason 10305f39d397SChris Mason leaf = path->nodes[0]; 10315f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 1032265fdfa6SDavid Sterba csum_offset = (bytenr - found_key.offset) >> fs_info->sectorsize_bits; 1033459931ecSChris Mason 1034962a298fSDavid Sterba if (found_key.type != BTRFS_EXTENT_CSUM_KEY || 1035d20f7043SChris Mason found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 10360b246afaSJeff Mahoney csum_offset >= MAX_CSUM_ITEMS(fs_info, csum_size)) { 10376567e837SChris Mason goto insert; 10386567e837SChris Mason } 1039459931ecSChris Mason 1040cc14600cSFilipe Manana extend_csum: 10412f697dc6SLiu Bo if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) / 1042607d432dSJosef Bacik csum_size) { 10432f697dc6SLiu Bo int extend_nr; 10442f697dc6SLiu Bo u64 tmp; 10452f697dc6SLiu Bo u32 diff; 1046459931ecSChris Mason 1047f51a4a18SMiao Xie tmp = sums->len - total_bytes; 1048265fdfa6SDavid Sterba tmp >>= fs_info->sectorsize_bits; 10492f697dc6SLiu Bo WARN_ON(tmp < 1); 10502f697dc6SLiu Bo 10512f697dc6SLiu Bo extend_nr = max_t(int, 1, (int)tmp); 10522f697dc6SLiu Bo diff = (csum_offset + extend_nr) * csum_size; 10530b246afaSJeff Mahoney diff = min(diff, 10540b246afaSJeff Mahoney MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size); 1055459931ecSChris Mason 10565f39d397SChris Mason diff = diff - btrfs_item_size_nr(leaf, path->slots[0]); 1057cc14600cSFilipe Manana diff = min_t(u32, btrfs_leaf_free_space(leaf), diff); 10582f697dc6SLiu Bo diff /= csum_size; 10592f697dc6SLiu Bo diff *= csum_size; 1060459931ecSChris Mason 1061c71dd880SDavid Sterba btrfs_extend_item(path, diff); 1062f51a4a18SMiao Xie ret = 0; 10636567e837SChris Mason goto csum; 10646567e837SChris Mason } 10656567e837SChris Mason 10666567e837SChris Mason insert: 1067b3b4aa74SDavid Sterba btrfs_release_path(path); 10686567e837SChris Mason csum_offset = 0; 1069f578d4bdSChris Mason if (found_next) { 10702f697dc6SLiu Bo u64 tmp; 1071d20f7043SChris Mason 1072f51a4a18SMiao Xie tmp = sums->len - total_bytes; 1073265fdfa6SDavid Sterba tmp >>= fs_info->sectorsize_bits; 10742f697dc6SLiu Bo tmp = min(tmp, (next_offset - file_key.offset) >> 1075265fdfa6SDavid Sterba fs_info->sectorsize_bits); 10762f697dc6SLiu Bo 107750d0446eSSeraphime Kirkovski tmp = max_t(u64, 1, tmp); 107850d0446eSSeraphime Kirkovski tmp = min_t(u64, tmp, MAX_CSUM_ITEMS(fs_info, csum_size)); 1079607d432dSJosef Bacik ins_size = csum_size * tmp; 1080f578d4bdSChris Mason } else { 1081607d432dSJosef Bacik ins_size = csum_size; 1082f578d4bdSChris Mason } 10835caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 1084f578d4bdSChris Mason ins_size); 108554aa1f4dSChris Mason if (ret < 0) 1086918cdf44SFilipe Manana goto out; 1087fae7f21cSDulshani Gunawardhana if (WARN_ON(ret != 0)) 1088918cdf44SFilipe Manana goto out; 10895f39d397SChris Mason leaf = path->nodes[0]; 1090f51a4a18SMiao Xie csum: 10915f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 1092f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((unsigned char *)item + 1093f51a4a18SMiao Xie btrfs_item_size_nr(leaf, path->slots[0])); 1094509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 1095607d432dSJosef Bacik csum_offset * csum_size); 1096b18c6685SChris Mason found: 1097265fdfa6SDavid Sterba ins_size = (u32)(sums->len - total_bytes) >> fs_info->sectorsize_bits; 1098f51a4a18SMiao Xie ins_size *= csum_size; 1099f51a4a18SMiao Xie ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item, 1100f51a4a18SMiao Xie ins_size); 1101f51a4a18SMiao Xie write_extent_buffer(leaf, sums->sums + index, (unsigned long)item, 1102f51a4a18SMiao Xie ins_size); 1103aadfeb6eSChris Mason 11041e25a2e3SJohannes Thumshirn index += ins_size; 1105f51a4a18SMiao Xie ins_size /= csum_size; 11060b246afaSJeff Mahoney total_bytes += ins_size * fs_info->sectorsize; 1107a6591715SChris Mason 11085caf2a00SChris Mason btrfs_mark_buffer_dirty(path->nodes[0]); 1109e6dcd2dcSChris Mason if (total_bytes < sums->len) { 1110b3b4aa74SDavid Sterba btrfs_release_path(path); 1111b9473439SChris Mason cond_resched(); 1112065631f6SChris Mason goto again; 1113065631f6SChris Mason } 111453863232SChris Mason out: 11155caf2a00SChris Mason btrfs_free_path(path); 1116f254e52cSChris Mason return ret; 1117f254e52cSChris Mason } 11187ffbb598SFilipe Manana 11199cdc5124SNikolay Borisov void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, 11207ffbb598SFilipe Manana const struct btrfs_path *path, 11217ffbb598SFilipe Manana struct btrfs_file_extent_item *fi, 11227ffbb598SFilipe Manana const bool new_inline, 11237ffbb598SFilipe Manana struct extent_map *em) 11247ffbb598SFilipe Manana { 11253ffbd68cSDavid Sterba struct btrfs_fs_info *fs_info = inode->root->fs_info; 11269cdc5124SNikolay Borisov struct btrfs_root *root = inode->root; 11277ffbb598SFilipe Manana struct extent_buffer *leaf = path->nodes[0]; 11287ffbb598SFilipe Manana const int slot = path->slots[0]; 11297ffbb598SFilipe Manana struct btrfs_key key; 11307ffbb598SFilipe Manana u64 extent_start, extent_end; 11317ffbb598SFilipe Manana u64 bytenr; 11327ffbb598SFilipe Manana u8 type = btrfs_file_extent_type(leaf, fi); 11337ffbb598SFilipe Manana int compress_type = btrfs_file_extent_compression(leaf, fi); 11347ffbb598SFilipe Manana 11357ffbb598SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot); 11367ffbb598SFilipe Manana extent_start = key.offset; 1137a5eeb3d1SFilipe Manana extent_end = btrfs_file_extent_end(path); 11387ffbb598SFilipe Manana em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); 11397ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_REG || 11407ffbb598SFilipe Manana type == BTRFS_FILE_EXTENT_PREALLOC) { 11417ffbb598SFilipe Manana em->start = extent_start; 11427ffbb598SFilipe Manana em->len = extent_end - extent_start; 11437ffbb598SFilipe Manana em->orig_start = extent_start - 11447ffbb598SFilipe Manana btrfs_file_extent_offset(leaf, fi); 11457ffbb598SFilipe Manana em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); 11467ffbb598SFilipe Manana bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); 11477ffbb598SFilipe Manana if (bytenr == 0) { 11487ffbb598SFilipe Manana em->block_start = EXTENT_MAP_HOLE; 11497ffbb598SFilipe Manana return; 11507ffbb598SFilipe Manana } 11517ffbb598SFilipe Manana if (compress_type != BTRFS_COMPRESS_NONE) { 11527ffbb598SFilipe Manana set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 11537ffbb598SFilipe Manana em->compress_type = compress_type; 11547ffbb598SFilipe Manana em->block_start = bytenr; 11557ffbb598SFilipe Manana em->block_len = em->orig_block_len; 11567ffbb598SFilipe Manana } else { 11577ffbb598SFilipe Manana bytenr += btrfs_file_extent_offset(leaf, fi); 11587ffbb598SFilipe Manana em->block_start = bytenr; 11597ffbb598SFilipe Manana em->block_len = em->len; 11607ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_PREALLOC) 11617ffbb598SFilipe Manana set_bit(EXTENT_FLAG_PREALLOC, &em->flags); 11627ffbb598SFilipe Manana } 11637ffbb598SFilipe Manana } else if (type == BTRFS_FILE_EXTENT_INLINE) { 11647ffbb598SFilipe Manana em->block_start = EXTENT_MAP_INLINE; 11657ffbb598SFilipe Manana em->start = extent_start; 11667ffbb598SFilipe Manana em->len = extent_end - extent_start; 11677ffbb598SFilipe Manana /* 11687ffbb598SFilipe Manana * Initialize orig_start and block_len with the same values 11697ffbb598SFilipe Manana * as in inode.c:btrfs_get_extent(). 11707ffbb598SFilipe Manana */ 11717ffbb598SFilipe Manana em->orig_start = EXTENT_MAP_HOLE; 11727ffbb598SFilipe Manana em->block_len = (u64)-1; 11737ffbb598SFilipe Manana if (!new_inline && compress_type != BTRFS_COMPRESS_NONE) { 11747ffbb598SFilipe Manana set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 11757ffbb598SFilipe Manana em->compress_type = compress_type; 11767ffbb598SFilipe Manana } 11777ffbb598SFilipe Manana } else { 11780b246afaSJeff Mahoney btrfs_err(fs_info, 11799cdc5124SNikolay Borisov "unknown file extent item type %d, inode %llu, offset %llu, " 11809cdc5124SNikolay Borisov "root %llu", type, btrfs_ino(inode), extent_start, 11817ffbb598SFilipe Manana root->root_key.objectid); 11827ffbb598SFilipe Manana } 11837ffbb598SFilipe Manana } 1184a5eeb3d1SFilipe Manana 1185a5eeb3d1SFilipe Manana /* 1186a5eeb3d1SFilipe Manana * Returns the end offset (non inclusive) of the file extent item the given path 1187a5eeb3d1SFilipe Manana * points to. If it points to an inline extent, the returned offset is rounded 1188a5eeb3d1SFilipe Manana * up to the sector size. 1189a5eeb3d1SFilipe Manana */ 1190a5eeb3d1SFilipe Manana u64 btrfs_file_extent_end(const struct btrfs_path *path) 1191a5eeb3d1SFilipe Manana { 1192a5eeb3d1SFilipe Manana const struct extent_buffer *leaf = path->nodes[0]; 1193a5eeb3d1SFilipe Manana const int slot = path->slots[0]; 1194a5eeb3d1SFilipe Manana struct btrfs_file_extent_item *fi; 1195a5eeb3d1SFilipe Manana struct btrfs_key key; 1196a5eeb3d1SFilipe Manana u64 end; 1197a5eeb3d1SFilipe Manana 1198a5eeb3d1SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot); 1199a5eeb3d1SFilipe Manana ASSERT(key.type == BTRFS_EXTENT_DATA_KEY); 1200a5eeb3d1SFilipe Manana fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); 1201a5eeb3d1SFilipe Manana 1202a5eeb3d1SFilipe Manana if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) { 1203a5eeb3d1SFilipe Manana end = btrfs_file_extent_ram_bytes(leaf, fi); 1204a5eeb3d1SFilipe Manana end = ALIGN(key.offset + end, leaf->fs_info->sectorsize); 1205a5eeb3d1SFilipe Manana } else { 1206a5eeb3d1SFilipe Manana end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); 1207a5eeb3d1SFilipe Manana } 1208a5eeb3d1SFilipe Manana 1209a5eeb3d1SFilipe Manana return end; 1210a5eeb3d1SFilipe Manana } 1211