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> 12cea62800SJohannes 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, 121*bd015294SJosef Bacik start + len - 1, EXTENT_DIRTY, 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 132d1f68ba0SOmar Sandoval int btrfs_insert_hole_extent(struct btrfs_trans_handle *trans, 133dee26a9fSChris Mason struct btrfs_root *root, 134d1f68ba0SOmar Sandoval u64 objectid, u64 pos, u64 num_bytes) 1359f5fae2fSChris Mason { 136dee26a9fSChris Mason int ret = 0; 137dee26a9fSChris Mason struct btrfs_file_extent_item *item; 138dee26a9fSChris Mason struct btrfs_key file_key; 1395caf2a00SChris Mason struct btrfs_path *path; 1405f39d397SChris Mason struct extent_buffer *leaf; 141dee26a9fSChris Mason 1425caf2a00SChris Mason path = btrfs_alloc_path(); 143db5b493aSTsutomu Itoh if (!path) 144db5b493aSTsutomu Itoh return -ENOMEM; 145dee26a9fSChris Mason file_key.objectid = objectid; 146b18c6685SChris Mason file_key.offset = pos; 147962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_DATA_KEY; 148dee26a9fSChris Mason 1495caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 150dee26a9fSChris Mason sizeof(*item)); 15154aa1f4dSChris Mason if (ret < 0) 15254aa1f4dSChris Mason goto out; 15379787eaaSJeff Mahoney BUG_ON(ret); /* Can't happen */ 1545f39d397SChris Mason leaf = path->nodes[0]; 1555f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], 156dee26a9fSChris Mason struct btrfs_file_extent_item); 157d1f68ba0SOmar Sandoval btrfs_set_file_extent_disk_bytenr(leaf, item, 0); 158d1f68ba0SOmar Sandoval btrfs_set_file_extent_disk_num_bytes(leaf, item, 0); 159d1f68ba0SOmar Sandoval btrfs_set_file_extent_offset(leaf, item, 0); 160db94535dSChris Mason btrfs_set_file_extent_num_bytes(leaf, item, num_bytes); 161d1f68ba0SOmar Sandoval btrfs_set_file_extent_ram_bytes(leaf, item, num_bytes); 1625f39d397SChris Mason btrfs_set_file_extent_generation(leaf, item, trans->transid); 1635f39d397SChris Mason btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); 164d1f68ba0SOmar Sandoval btrfs_set_file_extent_compression(leaf, item, 0); 165d1f68ba0SOmar Sandoval btrfs_set_file_extent_encryption(leaf, item, 0); 166d1f68ba0SOmar Sandoval btrfs_set_file_extent_other_encoding(leaf, item, 0); 167c8b97818SChris Mason 1685f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 16954aa1f4dSChris Mason out: 1705caf2a00SChris Mason btrfs_free_path(path); 17154aa1f4dSChris Mason return ret; 1729f5fae2fSChris Mason } 173dee26a9fSChris Mason 17448a3b636SEric Sandeen static struct btrfs_csum_item * 17548a3b636SEric Sandeen btrfs_lookup_csum(struct btrfs_trans_handle *trans, 176b18c6685SChris Mason struct btrfs_root *root, 1776567e837SChris Mason struct btrfs_path *path, 178d20f7043SChris Mason u64 bytenr, int cow) 1796567e837SChris Mason { 1800b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1816567e837SChris Mason int ret; 1826567e837SChris Mason struct btrfs_key file_key; 1836567e837SChris Mason struct btrfs_key found_key; 1846567e837SChris Mason struct btrfs_csum_item *item; 1855f39d397SChris Mason struct extent_buffer *leaf; 1866567e837SChris Mason u64 csum_offset = 0; 187223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 188a429e513SChris Mason int csums_in_item; 1896567e837SChris Mason 190d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 191d20f7043SChris Mason file_key.offset = bytenr; 192962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_CSUM_KEY; 193b18c6685SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); 1946567e837SChris Mason if (ret < 0) 1956567e837SChris Mason goto fail; 1965f39d397SChris Mason leaf = path->nodes[0]; 1976567e837SChris Mason if (ret > 0) { 1986567e837SChris Mason ret = 1; 19970b2befdSChris Mason if (path->slots[0] == 0) 2006567e837SChris Mason goto fail; 2016567e837SChris Mason path->slots[0]--; 2025f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 203962a298fSDavid Sterba if (found_key.type != BTRFS_EXTENT_CSUM_KEY) 2046567e837SChris Mason goto fail; 205d20f7043SChris Mason 206d20f7043SChris Mason csum_offset = (bytenr - found_key.offset) >> 207265fdfa6SDavid Sterba fs_info->sectorsize_bits; 2083212fa14SJosef Bacik csums_in_item = btrfs_item_size(leaf, path->slots[0]); 209607d432dSJosef Bacik csums_in_item /= csum_size; 210a429e513SChris Mason 21182d130ffSMiao Xie if (csum_offset == csums_in_item) { 212a429e513SChris Mason ret = -EFBIG; 2136567e837SChris Mason goto fail; 21482d130ffSMiao Xie } else if (csum_offset > csums_in_item) { 21582d130ffSMiao Xie goto fail; 2166567e837SChris Mason } 2176567e837SChris Mason } 2186567e837SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 219509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 220607d432dSJosef Bacik csum_offset * csum_size); 2216567e837SChris Mason return item; 2226567e837SChris Mason fail: 2236567e837SChris Mason if (ret > 0) 224b18c6685SChris Mason ret = -ENOENT; 2256567e837SChris Mason return ERR_PTR(ret); 2266567e837SChris Mason } 2276567e837SChris Mason 228dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, 229dee26a9fSChris Mason struct btrfs_root *root, 230dee26a9fSChris Mason struct btrfs_path *path, u64 objectid, 2319773a788SChris Mason u64 offset, int mod) 232dee26a9fSChris Mason { 233dee26a9fSChris Mason struct btrfs_key file_key; 234dee26a9fSChris Mason int ins_len = mod < 0 ? -1 : 0; 235dee26a9fSChris Mason int cow = mod != 0; 236dee26a9fSChris Mason 237dee26a9fSChris Mason file_key.objectid = objectid; 23870b2befdSChris Mason file_key.offset = offset; 239962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_DATA_KEY; 240f8ee80deSMarcos Paulo de Souza 241f8ee80deSMarcos Paulo de Souza return btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); 242dee26a9fSChris Mason } 243f254e52cSChris Mason 2446275193eSQu Wenruo /* 2456275193eSQu Wenruo * Find checksums for logical bytenr range [disk_bytenr, disk_bytenr + len) and 2466275193eSQu Wenruo * estore the result to @dst. 2476275193eSQu Wenruo * 2486275193eSQu Wenruo * Return >0 for the number of sectors we found. 2496275193eSQu Wenruo * Return 0 for the range [disk_bytenr, disk_bytenr + sectorsize) has no csum 2506275193eSQu Wenruo * for it. Caller may want to try next sector until one range is hit. 2516275193eSQu Wenruo * Return <0 for fatal error. 2526275193eSQu Wenruo */ 2536275193eSQu Wenruo static int search_csum_tree(struct btrfs_fs_info *fs_info, 2546275193eSQu Wenruo struct btrfs_path *path, u64 disk_bytenr, 2556275193eSQu Wenruo u64 len, u8 *dst) 2566275193eSQu Wenruo { 257fc28b25eSJosef Bacik struct btrfs_root *csum_root; 2586275193eSQu Wenruo struct btrfs_csum_item *item = NULL; 2596275193eSQu Wenruo struct btrfs_key key; 2606275193eSQu Wenruo const u32 sectorsize = fs_info->sectorsize; 2616275193eSQu Wenruo const u32 csum_size = fs_info->csum_size; 2626275193eSQu Wenruo u32 itemsize; 2636275193eSQu Wenruo int ret; 2646275193eSQu Wenruo u64 csum_start; 2656275193eSQu Wenruo u64 csum_len; 2666275193eSQu Wenruo 2676275193eSQu Wenruo ASSERT(IS_ALIGNED(disk_bytenr, sectorsize) && 2686275193eSQu Wenruo IS_ALIGNED(len, sectorsize)); 2696275193eSQu Wenruo 2706275193eSQu Wenruo /* Check if the current csum item covers disk_bytenr */ 2716275193eSQu Wenruo if (path->nodes[0]) { 2726275193eSQu Wenruo item = btrfs_item_ptr(path->nodes[0], path->slots[0], 2736275193eSQu Wenruo struct btrfs_csum_item); 2746275193eSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2753212fa14SJosef Bacik itemsize = btrfs_item_size(path->nodes[0], path->slots[0]); 2766275193eSQu Wenruo 2776275193eSQu Wenruo csum_start = key.offset; 2786275193eSQu Wenruo csum_len = (itemsize / csum_size) * sectorsize; 2796275193eSQu Wenruo 2806275193eSQu Wenruo if (in_range(disk_bytenr, csum_start, csum_len)) 2816275193eSQu Wenruo goto found; 2826275193eSQu Wenruo } 2836275193eSQu Wenruo 2846275193eSQu Wenruo /* Current item doesn't contain the desired range, search again */ 2856275193eSQu Wenruo btrfs_release_path(path); 286fc28b25eSJosef Bacik csum_root = btrfs_csum_root(fs_info, disk_bytenr); 287fc28b25eSJosef Bacik item = btrfs_lookup_csum(NULL, csum_root, path, disk_bytenr, 0); 2886275193eSQu Wenruo if (IS_ERR(item)) { 2896275193eSQu Wenruo ret = PTR_ERR(item); 2906275193eSQu Wenruo goto out; 2916275193eSQu Wenruo } 2926275193eSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2933212fa14SJosef Bacik itemsize = btrfs_item_size(path->nodes[0], path->slots[0]); 2946275193eSQu Wenruo 2956275193eSQu Wenruo csum_start = key.offset; 2966275193eSQu Wenruo csum_len = (itemsize / csum_size) * sectorsize; 2976275193eSQu Wenruo ASSERT(in_range(disk_bytenr, csum_start, csum_len)); 2986275193eSQu Wenruo 2996275193eSQu Wenruo found: 3006275193eSQu Wenruo ret = (min(csum_start + csum_len, disk_bytenr + len) - 3016275193eSQu Wenruo disk_bytenr) >> fs_info->sectorsize_bits; 3026275193eSQu Wenruo read_extent_buffer(path->nodes[0], dst, (unsigned long)item, 3036275193eSQu Wenruo ret * csum_size); 3046275193eSQu Wenruo out: 30503ddb19dSJosef Bacik if (ret == -ENOENT || ret == -EFBIG) 3066275193eSQu Wenruo ret = 0; 3076275193eSQu Wenruo return ret; 3086275193eSQu Wenruo } 3096275193eSQu Wenruo 3106275193eSQu Wenruo /* 3116275193eSQu Wenruo * Locate the file_offset of @cur_disk_bytenr of a @bio. 3126275193eSQu Wenruo * 3136275193eSQu Wenruo * Bio of btrfs represents read range of 3146275193eSQu Wenruo * [bi_sector << 9, bi_sector << 9 + bi_size). 3156275193eSQu Wenruo * Knowing this, we can iterate through each bvec to locate the page belong to 3166275193eSQu Wenruo * @cur_disk_bytenr and get the file offset. 3176275193eSQu Wenruo * 3186275193eSQu Wenruo * @inode is used to determine if the bvec page really belongs to @inode. 3196275193eSQu Wenruo * 3206275193eSQu Wenruo * Return 0 if we can't find the file offset 3216275193eSQu Wenruo * Return >0 if we find the file offset and restore it to @file_offset_ret 3226275193eSQu Wenruo */ 3236275193eSQu Wenruo static int search_file_offset_in_bio(struct bio *bio, struct inode *inode, 3246275193eSQu Wenruo u64 disk_bytenr, u64 *file_offset_ret) 3256275193eSQu Wenruo { 3266275193eSQu Wenruo struct bvec_iter iter; 3276275193eSQu Wenruo struct bio_vec bvec; 3286275193eSQu Wenruo u64 cur = bio->bi_iter.bi_sector << SECTOR_SHIFT; 3296275193eSQu Wenruo int ret = 0; 3306275193eSQu Wenruo 3316275193eSQu Wenruo bio_for_each_segment(bvec, bio, iter) { 3326275193eSQu Wenruo struct page *page = bvec.bv_page; 3336275193eSQu Wenruo 3346275193eSQu Wenruo if (cur > disk_bytenr) 3356275193eSQu Wenruo break; 3366275193eSQu Wenruo if (cur + bvec.bv_len <= disk_bytenr) { 3376275193eSQu Wenruo cur += bvec.bv_len; 3386275193eSQu Wenruo continue; 3396275193eSQu Wenruo } 3406275193eSQu Wenruo ASSERT(in_range(disk_bytenr, cur, bvec.bv_len)); 3416275193eSQu Wenruo if (page->mapping && page->mapping->host && 3426275193eSQu Wenruo page->mapping->host == inode) { 3436275193eSQu Wenruo ret = 1; 3446275193eSQu Wenruo *file_offset_ret = page_offset(page) + bvec.bv_offset + 3456275193eSQu Wenruo disk_bytenr - cur; 3466275193eSQu Wenruo break; 3476275193eSQu Wenruo } 3486275193eSQu Wenruo } 3496275193eSQu Wenruo return ret; 3506275193eSQu Wenruo } 3516275193eSQu Wenruo 352e62958fcSOmar Sandoval /** 3536275193eSQu Wenruo * Lookup the checksum for the read bio in csum tree. 3549e46458aSQu Wenruo * 355e62958fcSOmar Sandoval * @inode: inode that the bio is for. 356fb30f470SOmar Sandoval * @bio: bio to look up. 357fb30f470SOmar Sandoval * @dst: Buffer of size nblocks * btrfs_super_csum_size() used to return 358fb30f470SOmar Sandoval * checksum (nblocks = bio->bi_iter.bi_size / fs_info->sectorsize). If 359fb30f470SOmar Sandoval * NULL, the checksum buffer is allocated and returned in 360c3a3b19bSQu Wenruo * btrfs_bio(bio)->csum instead. 361e62958fcSOmar Sandoval * 362e62958fcSOmar Sandoval * Return: BLK_STS_RESOURCE if allocating memory fails, BLK_STS_OK otherwise. 363e62958fcSOmar Sandoval */ 3646275193eSQu Wenruo blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, u8 *dst) 36561b49440SChris Mason { 3660b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 367facc8a22SMiao Xie struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 3681784b7d5SJosef Bacik struct btrfs_bio *bbio = NULL; 369facc8a22SMiao Xie struct btrfs_path *path; 3706275193eSQu Wenruo const u32 sectorsize = fs_info->sectorsize; 371223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 3726275193eSQu Wenruo u32 orig_len = bio->bi_iter.bi_size; 3736275193eSQu Wenruo u64 orig_disk_bytenr = bio->bi_iter.bi_sector << SECTOR_SHIFT; 3746275193eSQu Wenruo u64 cur_disk_bytenr; 3756275193eSQu Wenruo u8 *csum; 3766275193eSQu Wenruo const unsigned int nblocks = orig_len >> fs_info->sectorsize_bits; 3776275193eSQu Wenruo int count = 0; 3781784b7d5SJosef Bacik blk_status_t ret = BLK_STS_OK; 37961b49440SChris Mason 380056c8311SJosef Bacik if ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) || 381056c8311SJosef Bacik test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state)) 382334c16d8SJosef Bacik return BLK_STS_OK; 383334c16d8SJosef Bacik 3849e46458aSQu Wenruo /* 3859e46458aSQu Wenruo * This function is only called for read bio. 3869e46458aSQu Wenruo * 3879e46458aSQu Wenruo * This means two things: 3889e46458aSQu Wenruo * - All our csums should only be in csum tree 3899e46458aSQu Wenruo * No ordered extents csums, as ordered extents are only for write 3909e46458aSQu Wenruo * path. 3916275193eSQu Wenruo * - No need to bother any other info from bvec 3926275193eSQu Wenruo * Since we're looking up csums, the only important info is the 3936275193eSQu Wenruo * disk_bytenr and the length, which can be extracted from bi_iter 3946275193eSQu Wenruo * directly. 3959e46458aSQu Wenruo */ 3969e46458aSQu Wenruo ASSERT(bio_op(bio) == REQ_OP_READ); 39761b49440SChris Mason path = btrfs_alloc_path(); 398c2db1073STsutomu Itoh if (!path) 3994e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 400facc8a22SMiao Xie 401facc8a22SMiao Xie if (!dst) { 4021784b7d5SJosef Bacik bbio = btrfs_bio(bio); 403fb30f470SOmar Sandoval 404facc8a22SMiao Xie if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) { 405c3a3b19bSQu Wenruo bbio->csum = kmalloc_array(nblocks, csum_size, GFP_NOFS); 406c3a3b19bSQu Wenruo if (!bbio->csum) { 407facc8a22SMiao Xie btrfs_free_path(path); 4084e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 409facc8a22SMiao Xie } 410facc8a22SMiao Xie } else { 411c3a3b19bSQu Wenruo bbio->csum = bbio->csum_inline; 412facc8a22SMiao Xie } 413c3a3b19bSQu Wenruo csum = bbio->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); 4581784b7d5SJosef Bacik if (count < 0) { 4591784b7d5SJosef Bacik ret = errno_to_blk_status(count); 4601784b7d5SJosef Bacik if (bbio) 4611784b7d5SJosef Bacik btrfs_bio_free_csum(bbio); 4621784b7d5SJosef Bacik break; 4631784b7d5SJosef Bacik } 4641784b7d5SJosef Bacik 4656275193eSQu Wenruo /* 4661784b7d5SJosef Bacik * We didn't find a csum for this range. We need to make sure 4671784b7d5SJosef Bacik * we complain loudly about this, because we are not NODATASUM. 4681784b7d5SJosef Bacik * 4691784b7d5SJosef Bacik * However for the DATA_RELOC inode we could potentially be 4701784b7d5SJosef Bacik * relocating data extents for a NODATASUM inode, so the inode 4711784b7d5SJosef Bacik * itself won't be marked with NODATASUM, but the extent we're 4721784b7d5SJosef Bacik * copying is in fact NODATASUM. If we don't find a csum we 4731784b7d5SJosef Bacik * assume this is the case. 4746275193eSQu Wenruo */ 4751784b7d5SJosef Bacik if (count == 0) { 4766275193eSQu Wenruo memset(csum_dst, 0, csum_size); 477e4100d98SMiao Xie count = 1; 4786275193eSQu Wenruo 47917d217feSYan Zheng if (BTRFS_I(inode)->root->root_key.objectid == 48017d217feSYan Zheng BTRFS_DATA_RELOC_TREE_OBJECTID) { 4816275193eSQu Wenruo u64 file_offset; 4826275193eSQu Wenruo int ret; 4836275193eSQu Wenruo 4846275193eSQu Wenruo ret = search_file_offset_in_bio(bio, inode, 4856275193eSQu Wenruo cur_disk_bytenr, &file_offset); 4866275193eSQu Wenruo if (ret) 4876275193eSQu Wenruo set_extent_bits(io_tree, file_offset, 4886275193eSQu Wenruo file_offset + sectorsize - 1, 489ceeb0ae7SDavid Sterba EXTENT_NODATASUM); 49017d217feSYan Zheng } else { 4916275193eSQu Wenruo btrfs_warn_rl(fs_info, 4926275193eSQu Wenruo "csum hole found for disk bytenr range [%llu, %llu)", 4936275193eSQu Wenruo cur_disk_bytenr, cur_disk_bytenr + sectorsize); 49417d217feSYan Zheng } 4954989d277SChristoph Hellwig } 4964989d277SChristoph Hellwig } 4974989d277SChristoph Hellwig 49861b49440SChris Mason btrfs_free_path(path); 4991784b7d5SJosef Bacik return ret; 5004b46fce2SJosef Bacik } 5014b46fce2SJosef Bacik 50217d217feSYan Zheng int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, 503a2de733cSArne Jansen struct list_head *list, int search_commit) 50417d217feSYan Zheng { 5050b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 50617d217feSYan Zheng struct btrfs_key key; 50717d217feSYan Zheng struct btrfs_path *path; 50817d217feSYan Zheng struct extent_buffer *leaf; 50917d217feSYan Zheng struct btrfs_ordered_sum *sums; 51017d217feSYan Zheng struct btrfs_csum_item *item; 5110678b618SMark Fasheh LIST_HEAD(tmplist); 51217d217feSYan Zheng unsigned long offset; 51317d217feSYan Zheng int ret; 51417d217feSYan Zheng size_t size; 51517d217feSYan Zheng u64 csum_end; 516223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 51717d217feSYan Zheng 5180b246afaSJeff Mahoney ASSERT(IS_ALIGNED(start, fs_info->sectorsize) && 5190b246afaSJeff Mahoney IS_ALIGNED(end + 1, fs_info->sectorsize)); 5204277a9c3SJosef Bacik 52117d217feSYan Zheng path = btrfs_alloc_path(); 522d8926bb3SMark Fasheh if (!path) 523d8926bb3SMark Fasheh return -ENOMEM; 52417d217feSYan Zheng 525a2de733cSArne Jansen if (search_commit) { 526a2de733cSArne Jansen path->skip_locking = 1; 527e4058b54SDavid Sterba path->reada = READA_FORWARD; 528a2de733cSArne Jansen path->search_commit_root = 1; 529a2de733cSArne Jansen } 530a2de733cSArne Jansen 53117d217feSYan Zheng key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 53217d217feSYan Zheng key.offset = start; 53317d217feSYan Zheng key.type = BTRFS_EXTENT_CSUM_KEY; 53417d217feSYan Zheng 53507d400a6SYan Zheng ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 53617d217feSYan Zheng if (ret < 0) 53717d217feSYan Zheng goto fail; 53817d217feSYan Zheng if (ret > 0 && path->slots[0] > 0) { 53917d217feSYan Zheng leaf = path->nodes[0]; 54017d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); 54117d217feSYan Zheng if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID && 54217d217feSYan Zheng key.type == BTRFS_EXTENT_CSUM_KEY) { 543265fdfa6SDavid Sterba offset = (start - key.offset) >> fs_info->sectorsize_bits; 54417d217feSYan Zheng if (offset * csum_size < 5453212fa14SJosef Bacik btrfs_item_size(leaf, path->slots[0] - 1)) 54617d217feSYan Zheng path->slots[0]--; 54717d217feSYan Zheng } 54817d217feSYan Zheng } 54917d217feSYan Zheng 55017d217feSYan Zheng while (start <= end) { 55117d217feSYan Zheng leaf = path->nodes[0]; 55217d217feSYan Zheng if (path->slots[0] >= btrfs_header_nritems(leaf)) { 55307d400a6SYan Zheng ret = btrfs_next_leaf(root, path); 55417d217feSYan Zheng if (ret < 0) 55517d217feSYan Zheng goto fail; 55617d217feSYan Zheng if (ret > 0) 55717d217feSYan Zheng break; 55817d217feSYan Zheng leaf = path->nodes[0]; 55917d217feSYan Zheng } 56017d217feSYan Zheng 56117d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 56217d217feSYan Zheng if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 563628c8282SZhi Yong Wu key.type != BTRFS_EXTENT_CSUM_KEY || 564628c8282SZhi Yong Wu key.offset > end) 56517d217feSYan Zheng break; 56617d217feSYan Zheng 56717d217feSYan Zheng if (key.offset > start) 56817d217feSYan Zheng start = key.offset; 56917d217feSYan Zheng 5703212fa14SJosef Bacik size = btrfs_item_size(leaf, path->slots[0]); 5710b246afaSJeff Mahoney csum_end = key.offset + (size / csum_size) * fs_info->sectorsize; 57287b29b20SYan Zheng if (csum_end <= start) { 57387b29b20SYan Zheng path->slots[0]++; 57487b29b20SYan Zheng continue; 57587b29b20SYan Zheng } 57617d217feSYan Zheng 57707d400a6SYan Zheng csum_end = min(csum_end, end + 1); 57807d400a6SYan Zheng item = btrfs_item_ptr(path->nodes[0], path->slots[0], 57907d400a6SYan Zheng struct btrfs_csum_item); 58007d400a6SYan Zheng while (start < csum_end) { 58107d400a6SYan Zheng size = min_t(size_t, csum_end - start, 5821e25a2e3SJohannes Thumshirn max_ordered_sum_bytes(fs_info, csum_size)); 5830b246afaSJeff Mahoney sums = kzalloc(btrfs_ordered_sum_size(fs_info, size), 58407d400a6SYan Zheng GFP_NOFS); 5850678b618SMark Fasheh if (!sums) { 5860678b618SMark Fasheh ret = -ENOMEM; 5870678b618SMark Fasheh goto fail; 5880678b618SMark Fasheh } 58917d217feSYan Zheng 59017d217feSYan Zheng sums->bytenr = start; 591f51a4a18SMiao Xie sums->len = (int)size; 59217d217feSYan Zheng 593265fdfa6SDavid Sterba offset = (start - key.offset) >> fs_info->sectorsize_bits; 59417d217feSYan Zheng offset *= csum_size; 595265fdfa6SDavid Sterba size >>= fs_info->sectorsize_bits; 59617d217feSYan Zheng 59707d400a6SYan Zheng read_extent_buffer(path->nodes[0], 598f51a4a18SMiao Xie sums->sums, 599f51a4a18SMiao Xie ((unsigned long)item) + offset, 600f51a4a18SMiao Xie csum_size * size); 60117d217feSYan Zheng 6020b246afaSJeff Mahoney start += fs_info->sectorsize * size; 6030678b618SMark Fasheh list_add_tail(&sums->list, &tmplist); 60407d400a6SYan Zheng } 60517d217feSYan Zheng path->slots[0]++; 60617d217feSYan Zheng } 60717d217feSYan Zheng ret = 0; 60817d217feSYan Zheng fail: 6090678b618SMark Fasheh while (ret < 0 && !list_empty(&tmplist)) { 6106e5aafb2SChris Mason sums = list_entry(tmplist.next, struct btrfs_ordered_sum, list); 6110678b618SMark Fasheh list_del(&sums->list); 6120678b618SMark Fasheh kfree(sums); 6130678b618SMark Fasheh } 6140678b618SMark Fasheh list_splice_tail(&tmplist, list); 6150678b618SMark Fasheh 61617d217feSYan Zheng btrfs_free_path(path); 61717d217feSYan Zheng return ret; 61817d217feSYan Zheng } 61917d217feSYan Zheng 620e331f6b1SOmar Sandoval /** 621e331f6b1SOmar Sandoval * Calculate checksums of the data contained inside a bio 622e331f6b1SOmar Sandoval * 62351d470aeSNikolay Borisov * @inode: Owner of the data inside the bio 62451d470aeSNikolay Borisov * @bio: Contains the data to be checksummed 625e331f6b1SOmar Sandoval * @offset: If (u64)-1, @bio may contain discontiguous bio vecs, so the 626e331f6b1SOmar Sandoval * file offsets are determined from the page offsets in the bio. 627e331f6b1SOmar Sandoval * Otherwise, this is the starting file offset of the bio vecs in 628e331f6b1SOmar Sandoval * @bio, which must be contiguous. 629e331f6b1SOmar Sandoval * @one_ordered: If true, @bio only refers to one ordered extent. 63051d470aeSNikolay Borisov */ 631bd242a08SNikolay Borisov blk_status_t btrfs_csum_one_bio(struct btrfs_inode *inode, struct bio *bio, 632e331f6b1SOmar Sandoval u64 offset, bool one_ordered) 633e015640fSChris Mason { 634c3504372SNikolay Borisov struct btrfs_fs_info *fs_info = inode->root->fs_info; 635d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 636e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums; 6376cd7ce49SChristoph Hellwig struct btrfs_ordered_extent *ordered = NULL; 638e331f6b1SOmar Sandoval const bool use_page_offsets = (offset == (u64)-1); 639e015640fSChris Mason char *data; 64017347cecSLiu Bo struct bvec_iter iter; 64117347cecSLiu Bo struct bio_vec bvec; 642f51a4a18SMiao Xie int index; 643e331f6b1SOmar Sandoval unsigned int blockcount; 6443edf7d33SChris Mason unsigned long total_bytes = 0; 6453edf7d33SChris Mason unsigned long this_sum_bytes = 0; 64617347cecSLiu Bo int i; 647a3d46aeaSNikolay Borisov unsigned nofs_flag; 648e015640fSChris Mason 649a3d46aeaSNikolay Borisov nofs_flag = memalloc_nofs_save(); 650a3d46aeaSNikolay Borisov sums = kvzalloc(btrfs_ordered_sum_size(fs_info, bio->bi_iter.bi_size), 651a3d46aeaSNikolay Borisov GFP_KERNEL); 652a3d46aeaSNikolay Borisov memalloc_nofs_restore(nofs_flag); 653a3d46aeaSNikolay Borisov 654e015640fSChris Mason if (!sums) 6554e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 6563edf7d33SChris Mason 6574f024f37SKent Overstreet sums->len = bio->bi_iter.bi_size; 658e6dcd2dcSChris Mason INIT_LIST_HEAD(&sums->list); 659d20f7043SChris Mason 6601201b58bSDavid Sterba sums->bytenr = bio->bi_iter.bi_sector << 9; 661f51a4a18SMiao Xie index = 0; 662e015640fSChris Mason 663d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 664d5178578SJohannes Thumshirn 66517347cecSLiu Bo bio_for_each_segment(bvec, bio, iter) { 666e331f6b1SOmar Sandoval if (use_page_offsets) 66717347cecSLiu Bo offset = page_offset(bvec.bv_page) + bvec.bv_offset; 668d20f7043SChris Mason 6696cd7ce49SChristoph Hellwig if (!ordered) { 6706cd7ce49SChristoph Hellwig ordered = btrfs_lookup_ordered_extent(inode, offset); 671bbc9a6ebSQu Wenruo /* 672bbc9a6ebSQu Wenruo * The bio range is not covered by any ordered extent, 673bbc9a6ebSQu Wenruo * must be a code logic error. 674bbc9a6ebSQu Wenruo */ 675bbc9a6ebSQu Wenruo if (unlikely(!ordered)) { 676bbc9a6ebSQu Wenruo WARN(1, KERN_WARNING 677bbc9a6ebSQu Wenruo "no ordered extent for root %llu ino %llu offset %llu\n", 678bbc9a6ebSQu Wenruo inode->root->root_key.objectid, 679bbc9a6ebSQu Wenruo btrfs_ino(inode), offset); 680bbc9a6ebSQu Wenruo kvfree(sums); 681bbc9a6ebSQu Wenruo return BLK_STS_IOERR; 682bbc9a6ebSQu Wenruo } 6836cd7ce49SChristoph Hellwig } 6846cd7ce49SChristoph Hellwig 685e331f6b1SOmar Sandoval blockcount = BTRFS_BYTES_TO_BLKS(fs_info, 68617347cecSLiu Bo bvec.bv_len + fs_info->sectorsize 687c40a3d38SChandan Rajendra - 1); 688c40a3d38SChandan Rajendra 689e331f6b1SOmar Sandoval for (i = 0; i < blockcount; i++) { 690e331f6b1SOmar Sandoval if (!one_ordered && 691e331f6b1SOmar Sandoval !in_range(offset, ordered->file_offset, 692e331f6b1SOmar Sandoval ordered->num_bytes)) { 6933edf7d33SChris Mason unsigned long bytes_left; 694c40a3d38SChandan Rajendra 6953edf7d33SChris Mason sums->len = this_sum_bytes; 6963edf7d33SChris Mason this_sum_bytes = 0; 697f9756261SNikolay Borisov btrfs_add_ordered_sum(ordered, sums); 6983edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 6993edf7d33SChris Mason 7004f024f37SKent Overstreet bytes_left = bio->bi_iter.bi_size - total_bytes; 7013edf7d33SChris Mason 702a3d46aeaSNikolay Borisov nofs_flag = memalloc_nofs_save(); 703a3d46aeaSNikolay Borisov sums = kvzalloc(btrfs_ordered_sum_size(fs_info, 704a3d46aeaSNikolay Borisov bytes_left), GFP_KERNEL); 705a3d46aeaSNikolay Borisov memalloc_nofs_restore(nofs_flag); 70679787eaaSJeff Mahoney BUG_ON(!sums); /* -ENOMEM */ 7073edf7d33SChris Mason sums->len = bytes_left; 708c40a3d38SChandan Rajendra ordered = btrfs_lookup_ordered_extent(inode, 709c40a3d38SChandan Rajendra offset); 710c40a3d38SChandan Rajendra ASSERT(ordered); /* Logic error */ 7111201b58bSDavid Sterba sums->bytenr = (bio->bi_iter.bi_sector << 9) 712c40a3d38SChandan Rajendra + total_bytes; 713f51a4a18SMiao Xie index = 0; 714c40a3d38SChandan Rajendra } 715c40a3d38SChandan Rajendra 7163dcfbcceSChristoph Hellwig data = bvec_kmap_local(&bvec); 7173dcfbcceSChristoph Hellwig crypto_shash_digest(shash, 7183dcfbcceSChristoph Hellwig data + (i * fs_info->sectorsize), 719fd08001fSEric Biggers fs_info->sectorsize, 720fd08001fSEric Biggers sums->sums + index); 7213dcfbcceSChristoph Hellwig kunmap_local(data); 722713cebfbSDavid Sterba index += fs_info->csum_size; 7230b246afaSJeff Mahoney offset += fs_info->sectorsize; 7240b246afaSJeff Mahoney this_sum_bytes += fs_info->sectorsize; 7250b246afaSJeff Mahoney total_bytes += fs_info->sectorsize; 726c40a3d38SChandan Rajendra } 727c40a3d38SChandan Rajendra 728e015640fSChris Mason } 729ed98b56aSChris Mason this_sum_bytes = 0; 730f9756261SNikolay Borisov btrfs_add_ordered_sum(ordered, sums); 7313edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 732e015640fSChris Mason return 0; 733e015640fSChris Mason } 734e015640fSChris Mason 735459931ecSChris Mason /* 736459931ecSChris Mason * helper function for csum removal, this expects the 737459931ecSChris Mason * key to describe the csum pointed to by the path, and it expects 738459931ecSChris Mason * the csum to overlap the range [bytenr, len] 739459931ecSChris Mason * 740459931ecSChris Mason * The csum should not be entirely contained in the range and the 741459931ecSChris Mason * range should not be entirely contained in the csum. 742459931ecSChris Mason * 743459931ecSChris Mason * This calls btrfs_truncate_item with the correct args based on the 744459931ecSChris Mason * overlap, and fixes up the key as required. 745459931ecSChris Mason */ 7462ff7e61eSJeff Mahoney static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info, 747459931ecSChris Mason struct btrfs_path *path, 748459931ecSChris Mason struct btrfs_key *key, 749459931ecSChris Mason u64 bytenr, u64 len) 750459931ecSChris Mason { 751459931ecSChris Mason struct extent_buffer *leaf; 752223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 753459931ecSChris Mason u64 csum_end; 754459931ecSChris Mason u64 end_byte = bytenr + len; 755265fdfa6SDavid Sterba u32 blocksize_bits = fs_info->sectorsize_bits; 756459931ecSChris Mason 757459931ecSChris Mason leaf = path->nodes[0]; 7583212fa14SJosef Bacik csum_end = btrfs_item_size(leaf, path->slots[0]) / csum_size; 759265fdfa6SDavid Sterba csum_end <<= blocksize_bits; 760459931ecSChris Mason csum_end += key->offset; 761459931ecSChris Mason 762459931ecSChris Mason if (key->offset < bytenr && csum_end <= end_byte) { 763459931ecSChris Mason /* 764459931ecSChris Mason * [ bytenr - len ] 765459931ecSChris Mason * [ ] 766459931ecSChris Mason * [csum ] 767459931ecSChris Mason * A simple truncate off the end of the item 768459931ecSChris Mason */ 769459931ecSChris Mason u32 new_size = (bytenr - key->offset) >> blocksize_bits; 770459931ecSChris Mason new_size *= csum_size; 77178ac4f9eSDavid Sterba btrfs_truncate_item(path, new_size, 1); 772459931ecSChris Mason } else if (key->offset >= bytenr && csum_end > end_byte && 773459931ecSChris Mason end_byte > key->offset) { 774459931ecSChris Mason /* 775459931ecSChris Mason * [ bytenr - len ] 776459931ecSChris Mason * [ ] 777459931ecSChris Mason * [csum ] 778459931ecSChris Mason * we need to truncate from the beginning of the csum 779459931ecSChris Mason */ 780459931ecSChris Mason u32 new_size = (csum_end - end_byte) >> blocksize_bits; 781459931ecSChris Mason new_size *= csum_size; 782459931ecSChris Mason 78378ac4f9eSDavid Sterba btrfs_truncate_item(path, new_size, 0); 784459931ecSChris Mason 785459931ecSChris Mason key->offset = end_byte; 7860b246afaSJeff Mahoney btrfs_set_item_key_safe(fs_info, path, key); 787459931ecSChris Mason } else { 788459931ecSChris Mason BUG(); 789459931ecSChris Mason } 790459931ecSChris Mason } 791459931ecSChris Mason 792459931ecSChris Mason /* 793459931ecSChris Mason * deletes the csum items from the csum tree for a given 794459931ecSChris Mason * range of bytes. 795459931ecSChris Mason */ 796459931ecSChris Mason int btrfs_del_csums(struct btrfs_trans_handle *trans, 79740e046acSFilipe Manana struct btrfs_root *root, u64 bytenr, u64 len) 798459931ecSChris Mason { 79940e046acSFilipe Manana struct btrfs_fs_info *fs_info = trans->fs_info; 800459931ecSChris Mason struct btrfs_path *path; 801459931ecSChris Mason struct btrfs_key key; 802459931ecSChris Mason u64 end_byte = bytenr + len; 803459931ecSChris Mason u64 csum_end; 804459931ecSChris Mason struct extent_buffer *leaf; 805b86652beSJosef Bacik int ret = 0; 806223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 807265fdfa6SDavid Sterba u32 blocksize_bits = fs_info->sectorsize_bits; 808459931ecSChris Mason 80984d2d6c7SJosef Bacik ASSERT(root->root_key.objectid == BTRFS_CSUM_TREE_OBJECTID || 81040e046acSFilipe Manana root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); 81140e046acSFilipe Manana 812459931ecSChris Mason path = btrfs_alloc_path(); 8132a29edc6Sliubo if (!path) 8142a29edc6Sliubo return -ENOMEM; 815459931ecSChris Mason 816459931ecSChris Mason while (1) { 817459931ecSChris Mason key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 818459931ecSChris Mason key.offset = end_byte - 1; 819459931ecSChris Mason key.type = BTRFS_EXTENT_CSUM_KEY; 820459931ecSChris Mason 821459931ecSChris Mason ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 822459931ecSChris Mason if (ret > 0) { 823b86652beSJosef Bacik ret = 0; 824459931ecSChris Mason if (path->slots[0] == 0) 82565a246c5STsutomu Itoh break; 826459931ecSChris Mason path->slots[0]--; 827ad0397a7SJosef Bacik } else if (ret < 0) { 82865a246c5STsutomu Itoh break; 829459931ecSChris Mason } 830ad0397a7SJosef Bacik 831459931ecSChris Mason leaf = path->nodes[0]; 832459931ecSChris Mason btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 833459931ecSChris Mason 834459931ecSChris Mason if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 835459931ecSChris Mason key.type != BTRFS_EXTENT_CSUM_KEY) { 836459931ecSChris Mason break; 837459931ecSChris Mason } 838459931ecSChris Mason 839459931ecSChris Mason if (key.offset >= end_byte) 840459931ecSChris Mason break; 841459931ecSChris Mason 8423212fa14SJosef Bacik csum_end = btrfs_item_size(leaf, path->slots[0]) / csum_size; 843459931ecSChris Mason csum_end <<= blocksize_bits; 844459931ecSChris Mason csum_end += key.offset; 845459931ecSChris Mason 846459931ecSChris Mason /* this csum ends before we start, we're done */ 847459931ecSChris Mason if (csum_end <= bytenr) 848459931ecSChris Mason break; 849459931ecSChris Mason 850459931ecSChris Mason /* delete the entire item, it is inside our range */ 851459931ecSChris Mason if (key.offset >= bytenr && csum_end <= end_byte) { 8526f546216SFilipe Manana int del_nr = 1; 8536f546216SFilipe Manana 8546f546216SFilipe Manana /* 8556f546216SFilipe Manana * Check how many csum items preceding this one in this 8566f546216SFilipe Manana * leaf correspond to our range and then delete them all 8576f546216SFilipe Manana * at once. 8586f546216SFilipe Manana */ 8596f546216SFilipe Manana if (key.offset > bytenr && path->slots[0] > 0) { 8606f546216SFilipe Manana int slot = path->slots[0] - 1; 8616f546216SFilipe Manana 8626f546216SFilipe Manana while (slot >= 0) { 8636f546216SFilipe Manana struct btrfs_key pk; 8646f546216SFilipe Manana 8656f546216SFilipe Manana btrfs_item_key_to_cpu(leaf, &pk, slot); 8666f546216SFilipe Manana if (pk.offset < bytenr || 8676f546216SFilipe Manana pk.type != BTRFS_EXTENT_CSUM_KEY || 8686f546216SFilipe Manana pk.objectid != 8696f546216SFilipe Manana BTRFS_EXTENT_CSUM_OBJECTID) 8706f546216SFilipe Manana break; 8716f546216SFilipe Manana path->slots[0] = slot; 8726f546216SFilipe Manana del_nr++; 8736f546216SFilipe Manana key.offset = pk.offset; 8746f546216SFilipe Manana slot--; 8756f546216SFilipe Manana } 8766f546216SFilipe Manana } 8776f546216SFilipe Manana ret = btrfs_del_items(trans, root, path, 8786f546216SFilipe Manana path->slots[0], del_nr); 87965a246c5STsutomu Itoh if (ret) 880b86652beSJosef Bacik break; 881dcbdd4dcSChris Mason if (key.offset == bytenr) 882dcbdd4dcSChris Mason break; 883459931ecSChris Mason } else if (key.offset < bytenr && csum_end > end_byte) { 884459931ecSChris Mason unsigned long offset; 885459931ecSChris Mason unsigned long shift_len; 886459931ecSChris Mason unsigned long item_offset; 887459931ecSChris Mason /* 888459931ecSChris Mason * [ bytenr - len ] 889459931ecSChris Mason * [csum ] 890459931ecSChris Mason * 891459931ecSChris Mason * Our bytes are in the middle of the csum, 892459931ecSChris Mason * we need to split this item and insert a new one. 893459931ecSChris Mason * 894459931ecSChris Mason * But we can't drop the path because the 895459931ecSChris Mason * csum could change, get removed, extended etc. 896459931ecSChris Mason * 897459931ecSChris Mason * The trick here is the max size of a csum item leaves 898459931ecSChris Mason * enough room in the tree block for a single 899459931ecSChris Mason * item header. So, we split the item in place, 900459931ecSChris Mason * adding a new header pointing to the existing 901459931ecSChris Mason * bytes. Then we loop around again and we have 902459931ecSChris Mason * a nicely formed csum item that we can neatly 903459931ecSChris Mason * truncate. 904459931ecSChris Mason */ 905459931ecSChris Mason offset = (bytenr - key.offset) >> blocksize_bits; 906459931ecSChris Mason offset *= csum_size; 907459931ecSChris Mason 908459931ecSChris Mason shift_len = (len >> blocksize_bits) * csum_size; 909459931ecSChris Mason 910459931ecSChris Mason item_offset = btrfs_item_ptr_offset(leaf, 911459931ecSChris Mason path->slots[0]); 912459931ecSChris Mason 913b159fa28SDavid Sterba memzero_extent_buffer(leaf, item_offset + offset, 914459931ecSChris Mason shift_len); 915459931ecSChris Mason key.offset = bytenr; 916459931ecSChris Mason 917459931ecSChris Mason /* 918459931ecSChris Mason * btrfs_split_item returns -EAGAIN when the 919459931ecSChris Mason * item changed size or key 920459931ecSChris Mason */ 921459931ecSChris Mason ret = btrfs_split_item(trans, root, path, &key, offset); 92279787eaaSJeff Mahoney if (ret && ret != -EAGAIN) { 92366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 924b86652beSJosef Bacik break; 92579787eaaSJeff Mahoney } 926b86652beSJosef Bacik ret = 0; 927459931ecSChris Mason 928459931ecSChris Mason key.offset = end_byte - 1; 929459931ecSChris Mason } else { 9302ff7e61eSJeff Mahoney truncate_one_csum(fs_info, path, &key, bytenr, len); 931dcbdd4dcSChris Mason if (key.offset < bytenr) 932dcbdd4dcSChris Mason break; 933459931ecSChris Mason } 934b3b4aa74SDavid Sterba btrfs_release_path(path); 935459931ecSChris Mason } 936459931ecSChris Mason btrfs_free_path(path); 93765a246c5STsutomu Itoh return ret; 938459931ecSChris Mason } 939459931ecSChris Mason 940ea7036deSFilipe Manana static int find_next_csum_offset(struct btrfs_root *root, 941ea7036deSFilipe Manana struct btrfs_path *path, 942ea7036deSFilipe Manana u64 *next_offset) 943ea7036deSFilipe Manana { 944ea7036deSFilipe Manana const u32 nritems = btrfs_header_nritems(path->nodes[0]); 945ea7036deSFilipe Manana struct btrfs_key found_key; 946ea7036deSFilipe Manana int slot = path->slots[0] + 1; 947ea7036deSFilipe Manana int ret; 948ea7036deSFilipe Manana 949ea7036deSFilipe Manana if (nritems == 0 || slot >= nritems) { 950ea7036deSFilipe Manana ret = btrfs_next_leaf(root, path); 951ea7036deSFilipe Manana if (ret < 0) { 952ea7036deSFilipe Manana return ret; 953ea7036deSFilipe Manana } else if (ret > 0) { 954ea7036deSFilipe Manana *next_offset = (u64)-1; 955ea7036deSFilipe Manana return 0; 956ea7036deSFilipe Manana } 957ea7036deSFilipe Manana slot = path->slots[0]; 958ea7036deSFilipe Manana } 959ea7036deSFilipe Manana 960ea7036deSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); 961ea7036deSFilipe Manana 962ea7036deSFilipe Manana if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 963ea7036deSFilipe Manana found_key.type != BTRFS_EXTENT_CSUM_KEY) 964ea7036deSFilipe Manana *next_offset = (u64)-1; 965ea7036deSFilipe Manana else 966ea7036deSFilipe Manana *next_offset = found_key.offset; 967ea7036deSFilipe Manana 968ea7036deSFilipe Manana return 0; 969ea7036deSFilipe Manana } 970ea7036deSFilipe Manana 971065631f6SChris Mason int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, 972d20f7043SChris Mason struct btrfs_root *root, 973e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums) 974f254e52cSChris Mason { 9750b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 976f254e52cSChris Mason struct btrfs_key file_key; 9776567e837SChris Mason struct btrfs_key found_key; 9785caf2a00SChris Mason struct btrfs_path *path; 979f254e52cSChris Mason struct btrfs_csum_item *item; 980065631f6SChris Mason struct btrfs_csum_item *item_end; 981ff79f819SChris Mason struct extent_buffer *leaf = NULL; 982f51a4a18SMiao Xie u64 next_offset; 983f51a4a18SMiao Xie u64 total_bytes = 0; 9846567e837SChris Mason u64 csum_offset; 985f51a4a18SMiao Xie u64 bytenr; 986f578d4bdSChris Mason u32 ins_size; 987f51a4a18SMiao Xie int index = 0; 988f51a4a18SMiao Xie int found_next; 989f51a4a18SMiao Xie int ret; 990223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 9916e92f5e6SChris Mason 9925caf2a00SChris Mason path = btrfs_alloc_path(); 993d8926bb3SMark Fasheh if (!path) 994d8926bb3SMark Fasheh return -ENOMEM; 995065631f6SChris Mason again: 996065631f6SChris Mason next_offset = (u64)-1; 997065631f6SChris Mason found_next = 0; 998f51a4a18SMiao Xie bytenr = sums->bytenr + total_bytes; 999d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 1000f51a4a18SMiao Xie file_key.offset = bytenr; 1001962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_CSUM_KEY; 1002a429e513SChris Mason 1003f51a4a18SMiao Xie item = btrfs_lookup_csum(trans, root, path, bytenr, 1); 1004ff79f819SChris Mason if (!IS_ERR(item)) { 1005639cb586SChris Mason ret = 0; 1006f51a4a18SMiao Xie leaf = path->nodes[0]; 1007f51a4a18SMiao Xie item_end = btrfs_item_ptr(leaf, path->slots[0], 1008f51a4a18SMiao Xie struct btrfs_csum_item); 1009f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((char *)item_end + 10103212fa14SJosef Bacik btrfs_item_size(leaf, path->slots[0])); 1011a429e513SChris Mason goto found; 1012ff79f819SChris Mason } 1013a429e513SChris Mason ret = PTR_ERR(item); 10144a500fd1SYan, Zheng if (ret != -EFBIG && ret != -ENOENT) 1015918cdf44SFilipe Manana goto out; 10164a500fd1SYan, Zheng 1017a429e513SChris Mason if (ret == -EFBIG) { 1018a429e513SChris Mason u32 item_size; 1019a429e513SChris Mason /* we found one, but it isn't big enough yet */ 10205f39d397SChris Mason leaf = path->nodes[0]; 10213212fa14SJosef Bacik item_size = btrfs_item_size(leaf, path->slots[0]); 1022607d432dSJosef Bacik if ((item_size / csum_size) >= 10230b246afaSJeff Mahoney MAX_CSUM_ITEMS(fs_info, csum_size)) { 1024a429e513SChris Mason /* already at max size, make a new one */ 1025a429e513SChris Mason goto insert; 1026a429e513SChris Mason } 1027a429e513SChris Mason } else { 1028ea7036deSFilipe Manana /* We didn't find a csum item, insert one. */ 1029ea7036deSFilipe Manana ret = find_next_csum_offset(root, path, &next_offset); 1030ea7036deSFilipe Manana if (ret < 0) 10317e4a3f7eSFilipe Manana goto out; 1032f578d4bdSChris Mason found_next = 1; 1033a429e513SChris Mason goto insert; 1034a429e513SChris Mason } 1035a429e513SChris Mason 1036a429e513SChris Mason /* 1037cc14600cSFilipe Manana * At this point, we know the tree has a checksum item that ends at an 1038cc14600cSFilipe Manana * offset matching the start of the checksum range we want to insert. 1039cc14600cSFilipe Manana * We try to extend that item as much as possible and then add as many 1040cc14600cSFilipe Manana * checksums to it as they fit. 1041cc14600cSFilipe Manana * 1042cc14600cSFilipe Manana * First check if the leaf has enough free space for at least one 1043cc14600cSFilipe Manana * checksum. If it has go directly to the item extension code, otherwise 1044cc14600cSFilipe Manana * release the path and do a search for insertion before the extension. 1045a429e513SChris Mason */ 1046cc14600cSFilipe Manana if (btrfs_leaf_free_space(leaf) >= csum_size) { 1047cc14600cSFilipe Manana btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 1048cc14600cSFilipe Manana csum_offset = (bytenr - found_key.offset) >> 1049265fdfa6SDavid Sterba fs_info->sectorsize_bits; 1050cc14600cSFilipe Manana goto extend_csum; 1051cc14600cSFilipe Manana } 1052cc14600cSFilipe Manana 1053b3b4aa74SDavid Sterba btrfs_release_path(path); 10549a664971Sethanwu path->search_for_extension = 1; 10556567e837SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 1056607d432dSJosef Bacik csum_size, 1); 10579a664971Sethanwu path->search_for_extension = 0; 10586567e837SChris Mason if (ret < 0) 1059918cdf44SFilipe Manana goto out; 1060459931ecSChris Mason 1061459931ecSChris Mason if (ret > 0) { 1062459931ecSChris Mason if (path->slots[0] == 0) 10636567e837SChris Mason goto insert; 10646567e837SChris Mason path->slots[0]--; 1065459931ecSChris Mason } 1066459931ecSChris Mason 10675f39d397SChris Mason leaf = path->nodes[0]; 10685f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 1069265fdfa6SDavid Sterba csum_offset = (bytenr - found_key.offset) >> fs_info->sectorsize_bits; 1070459931ecSChris Mason 1071962a298fSDavid Sterba if (found_key.type != BTRFS_EXTENT_CSUM_KEY || 1072d20f7043SChris Mason found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 10730b246afaSJeff Mahoney csum_offset >= MAX_CSUM_ITEMS(fs_info, csum_size)) { 10746567e837SChris Mason goto insert; 10756567e837SChris Mason } 1076459931ecSChris Mason 1077cc14600cSFilipe Manana extend_csum: 10783212fa14SJosef Bacik if (csum_offset == btrfs_item_size(leaf, path->slots[0]) / 1079607d432dSJosef Bacik csum_size) { 10802f697dc6SLiu Bo int extend_nr; 10812f697dc6SLiu Bo u64 tmp; 10822f697dc6SLiu Bo u32 diff; 1083459931ecSChris Mason 1084f51a4a18SMiao Xie tmp = sums->len - total_bytes; 1085265fdfa6SDavid Sterba tmp >>= fs_info->sectorsize_bits; 10862f697dc6SLiu Bo WARN_ON(tmp < 1); 1087ea7036deSFilipe Manana extend_nr = max_t(int, 1, tmp); 10882f697dc6SLiu Bo 1089ea7036deSFilipe Manana /* 1090ea7036deSFilipe Manana * A log tree can already have checksum items with a subset of 1091ea7036deSFilipe Manana * the checksums we are trying to log. This can happen after 1092ea7036deSFilipe Manana * doing a sequence of partial writes into prealloc extents and 1093ea7036deSFilipe Manana * fsyncs in between, with a full fsync logging a larger subrange 1094ea7036deSFilipe Manana * of an extent for which a previous fast fsync logged a smaller 1095ea7036deSFilipe Manana * subrange. And this happens in particular due to merging file 1096ea7036deSFilipe Manana * extent items when we complete an ordered extent for a range 1097ea7036deSFilipe Manana * covered by a prealloc extent - this is done at 1098ea7036deSFilipe Manana * btrfs_mark_extent_written(). 1099ea7036deSFilipe Manana * 1100ea7036deSFilipe Manana * So if we try to extend the previous checksum item, which has 1101ea7036deSFilipe Manana * a range that ends at the start of the range we want to insert, 1102ea7036deSFilipe Manana * make sure we don't extend beyond the start offset of the next 1103ea7036deSFilipe Manana * checksum item. If we are at the last item in the leaf, then 1104ea7036deSFilipe Manana * forget the optimization of extending and add a new checksum 1105ea7036deSFilipe Manana * item - it is not worth the complexity of releasing the path, 1106ea7036deSFilipe Manana * getting the first key for the next leaf, repeat the btree 1107ea7036deSFilipe Manana * search, etc, because log trees are temporary anyway and it 1108ea7036deSFilipe Manana * would only save a few bytes of leaf space. 1109ea7036deSFilipe Manana */ 1110ea7036deSFilipe Manana if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { 1111ea7036deSFilipe Manana if (path->slots[0] + 1 >= 1112ea7036deSFilipe Manana btrfs_header_nritems(path->nodes[0])) { 1113ea7036deSFilipe Manana ret = find_next_csum_offset(root, path, &next_offset); 1114ea7036deSFilipe Manana if (ret < 0) 1115ea7036deSFilipe Manana goto out; 1116ea7036deSFilipe Manana found_next = 1; 1117ea7036deSFilipe Manana goto insert; 1118ea7036deSFilipe Manana } 1119ea7036deSFilipe Manana 1120ea7036deSFilipe Manana ret = find_next_csum_offset(root, path, &next_offset); 1121ea7036deSFilipe Manana if (ret < 0) 1122ea7036deSFilipe Manana goto out; 1123ea7036deSFilipe Manana 1124ea7036deSFilipe Manana tmp = (next_offset - bytenr) >> fs_info->sectorsize_bits; 1125ea7036deSFilipe Manana if (tmp <= INT_MAX) 1126ea7036deSFilipe Manana extend_nr = min_t(int, extend_nr, tmp); 1127ea7036deSFilipe Manana } 1128ea7036deSFilipe Manana 11292f697dc6SLiu Bo diff = (csum_offset + extend_nr) * csum_size; 11300b246afaSJeff Mahoney diff = min(diff, 11310b246afaSJeff Mahoney MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size); 1132459931ecSChris Mason 11333212fa14SJosef Bacik diff = diff - btrfs_item_size(leaf, path->slots[0]); 1134cc14600cSFilipe Manana diff = min_t(u32, btrfs_leaf_free_space(leaf), diff); 11352f697dc6SLiu Bo diff /= csum_size; 11362f697dc6SLiu Bo diff *= csum_size; 1137459931ecSChris Mason 1138c71dd880SDavid Sterba btrfs_extend_item(path, diff); 1139f51a4a18SMiao Xie ret = 0; 11406567e837SChris Mason goto csum; 11416567e837SChris Mason } 11426567e837SChris Mason 11436567e837SChris Mason insert: 1144b3b4aa74SDavid Sterba btrfs_release_path(path); 11456567e837SChris Mason csum_offset = 0; 1146f578d4bdSChris Mason if (found_next) { 11472f697dc6SLiu Bo u64 tmp; 1148d20f7043SChris Mason 1149f51a4a18SMiao Xie tmp = sums->len - total_bytes; 1150265fdfa6SDavid Sterba tmp >>= fs_info->sectorsize_bits; 11512f697dc6SLiu Bo tmp = min(tmp, (next_offset - file_key.offset) >> 1152265fdfa6SDavid Sterba fs_info->sectorsize_bits); 11532f697dc6SLiu Bo 115450d0446eSSeraphime Kirkovski tmp = max_t(u64, 1, tmp); 115550d0446eSSeraphime Kirkovski tmp = min_t(u64, tmp, MAX_CSUM_ITEMS(fs_info, csum_size)); 1156607d432dSJosef Bacik ins_size = csum_size * tmp; 1157f578d4bdSChris Mason } else { 1158607d432dSJosef Bacik ins_size = csum_size; 1159f578d4bdSChris Mason } 11605caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 1161f578d4bdSChris Mason ins_size); 116254aa1f4dSChris Mason if (ret < 0) 1163918cdf44SFilipe Manana goto out; 1164fae7f21cSDulshani Gunawardhana if (WARN_ON(ret != 0)) 1165918cdf44SFilipe Manana goto out; 11665f39d397SChris Mason leaf = path->nodes[0]; 1167f51a4a18SMiao Xie csum: 11685f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 1169f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((unsigned char *)item + 11703212fa14SJosef Bacik btrfs_item_size(leaf, path->slots[0])); 1171509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 1172607d432dSJosef Bacik csum_offset * csum_size); 1173b18c6685SChris Mason found: 1174265fdfa6SDavid Sterba ins_size = (u32)(sums->len - total_bytes) >> fs_info->sectorsize_bits; 1175f51a4a18SMiao Xie ins_size *= csum_size; 1176f51a4a18SMiao Xie ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item, 1177f51a4a18SMiao Xie ins_size); 1178f51a4a18SMiao Xie write_extent_buffer(leaf, sums->sums + index, (unsigned long)item, 1179f51a4a18SMiao Xie ins_size); 1180aadfeb6eSChris Mason 11811e25a2e3SJohannes Thumshirn index += ins_size; 1182f51a4a18SMiao Xie ins_size /= csum_size; 11830b246afaSJeff Mahoney total_bytes += ins_size * fs_info->sectorsize; 1184a6591715SChris Mason 11855caf2a00SChris Mason btrfs_mark_buffer_dirty(path->nodes[0]); 1186e6dcd2dcSChris Mason if (total_bytes < sums->len) { 1187b3b4aa74SDavid Sterba btrfs_release_path(path); 1188b9473439SChris Mason cond_resched(); 1189065631f6SChris Mason goto again; 1190065631f6SChris Mason } 119153863232SChris Mason out: 11925caf2a00SChris Mason btrfs_free_path(path); 1193f254e52cSChris Mason return ret; 1194f254e52cSChris Mason } 11957ffbb598SFilipe Manana 11969cdc5124SNikolay Borisov void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, 11977ffbb598SFilipe Manana const struct btrfs_path *path, 11987ffbb598SFilipe Manana struct btrfs_file_extent_item *fi, 11997ffbb598SFilipe Manana const bool new_inline, 12007ffbb598SFilipe Manana struct extent_map *em) 12017ffbb598SFilipe Manana { 12023ffbd68cSDavid Sterba struct btrfs_fs_info *fs_info = inode->root->fs_info; 12039cdc5124SNikolay Borisov struct btrfs_root *root = inode->root; 12047ffbb598SFilipe Manana struct extent_buffer *leaf = path->nodes[0]; 12057ffbb598SFilipe Manana const int slot = path->slots[0]; 12067ffbb598SFilipe Manana struct btrfs_key key; 12077ffbb598SFilipe Manana u64 extent_start, extent_end; 12087ffbb598SFilipe Manana u64 bytenr; 12097ffbb598SFilipe Manana u8 type = btrfs_file_extent_type(leaf, fi); 12107ffbb598SFilipe Manana int compress_type = btrfs_file_extent_compression(leaf, fi); 12117ffbb598SFilipe Manana 12127ffbb598SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot); 12137ffbb598SFilipe Manana extent_start = key.offset; 1214a5eeb3d1SFilipe Manana extent_end = btrfs_file_extent_end(path); 12157ffbb598SFilipe Manana em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); 121640e7efe0SQu Wenruo em->generation = btrfs_file_extent_generation(leaf, fi); 12177ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_REG || 12187ffbb598SFilipe Manana type == BTRFS_FILE_EXTENT_PREALLOC) { 12197ffbb598SFilipe Manana em->start = extent_start; 12207ffbb598SFilipe Manana em->len = extent_end - extent_start; 12217ffbb598SFilipe Manana em->orig_start = extent_start - 12227ffbb598SFilipe Manana btrfs_file_extent_offset(leaf, fi); 12237ffbb598SFilipe Manana em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); 12247ffbb598SFilipe Manana bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); 12257ffbb598SFilipe Manana if (bytenr == 0) { 12267ffbb598SFilipe Manana em->block_start = EXTENT_MAP_HOLE; 12277ffbb598SFilipe Manana return; 12287ffbb598SFilipe Manana } 12297ffbb598SFilipe Manana if (compress_type != BTRFS_COMPRESS_NONE) { 12307ffbb598SFilipe Manana set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 12317ffbb598SFilipe Manana em->compress_type = compress_type; 12327ffbb598SFilipe Manana em->block_start = bytenr; 12337ffbb598SFilipe Manana em->block_len = em->orig_block_len; 12347ffbb598SFilipe Manana } else { 12357ffbb598SFilipe Manana bytenr += btrfs_file_extent_offset(leaf, fi); 12367ffbb598SFilipe Manana em->block_start = bytenr; 12377ffbb598SFilipe Manana em->block_len = em->len; 12387ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_PREALLOC) 12397ffbb598SFilipe Manana set_bit(EXTENT_FLAG_PREALLOC, &em->flags); 12407ffbb598SFilipe Manana } 12417ffbb598SFilipe Manana } else if (type == BTRFS_FILE_EXTENT_INLINE) { 12427ffbb598SFilipe Manana em->block_start = EXTENT_MAP_INLINE; 12437ffbb598SFilipe Manana em->start = extent_start; 12447ffbb598SFilipe Manana em->len = extent_end - extent_start; 12457ffbb598SFilipe Manana /* 12467ffbb598SFilipe Manana * Initialize orig_start and block_len with the same values 12477ffbb598SFilipe Manana * as in inode.c:btrfs_get_extent(). 12487ffbb598SFilipe Manana */ 12497ffbb598SFilipe Manana em->orig_start = EXTENT_MAP_HOLE; 12507ffbb598SFilipe Manana em->block_len = (u64)-1; 12517ffbb598SFilipe Manana if (!new_inline && compress_type != BTRFS_COMPRESS_NONE) { 12527ffbb598SFilipe Manana set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 12537ffbb598SFilipe Manana em->compress_type = compress_type; 12547ffbb598SFilipe Manana } 12557ffbb598SFilipe Manana } else { 12560b246afaSJeff Mahoney btrfs_err(fs_info, 12579cdc5124SNikolay Borisov "unknown file extent item type %d, inode %llu, offset %llu, " 12589cdc5124SNikolay Borisov "root %llu", type, btrfs_ino(inode), extent_start, 12597ffbb598SFilipe Manana root->root_key.objectid); 12607ffbb598SFilipe Manana } 12617ffbb598SFilipe Manana } 1262a5eeb3d1SFilipe Manana 1263a5eeb3d1SFilipe Manana /* 1264a5eeb3d1SFilipe Manana * Returns the end offset (non inclusive) of the file extent item the given path 1265a5eeb3d1SFilipe Manana * points to. If it points to an inline extent, the returned offset is rounded 1266a5eeb3d1SFilipe Manana * up to the sector size. 1267a5eeb3d1SFilipe Manana */ 1268a5eeb3d1SFilipe Manana u64 btrfs_file_extent_end(const struct btrfs_path *path) 1269a5eeb3d1SFilipe Manana { 1270a5eeb3d1SFilipe Manana const struct extent_buffer *leaf = path->nodes[0]; 1271a5eeb3d1SFilipe Manana const int slot = path->slots[0]; 1272a5eeb3d1SFilipe Manana struct btrfs_file_extent_item *fi; 1273a5eeb3d1SFilipe Manana struct btrfs_key key; 1274a5eeb3d1SFilipe Manana u64 end; 1275a5eeb3d1SFilipe Manana 1276a5eeb3d1SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot); 1277a5eeb3d1SFilipe Manana ASSERT(key.type == BTRFS_EXTENT_DATA_KEY); 1278a5eeb3d1SFilipe Manana fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); 1279a5eeb3d1SFilipe Manana 1280a5eeb3d1SFilipe Manana if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) { 1281a5eeb3d1SFilipe Manana end = btrfs_file_extent_ram_bytes(leaf, fi); 1282a5eeb3d1SFilipe Manana end = ALIGN(key.offset + end, leaf->fs_info->sectorsize); 1283a5eeb3d1SFilipe Manana } else { 1284a5eeb3d1SFilipe Manana end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); 1285a5eeb3d1SFilipe Manana } 1286a5eeb3d1SFilipe Manana 1287a5eeb3d1SFilipe Manana return end; 1288a5eeb3d1SFilipe Manana } 1289