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> 129b569ea0SJosef Bacik #include "messages.h" 13cea62800SJohannes Thumshirn #include "misc.h" 141e1d2701SChris Mason #include "ctree.h" 15dee26a9fSChris Mason #include "disk-io.h" 169f5fae2fSChris Mason #include "transaction.h" 17facc8a22SMiao Xie #include "volumes.h" 181de037a4SChris Mason #include "print-tree.h" 19ebb8765bSAnand Jain #include "compression.h" 20c7f13d42SJosef Bacik #include "fs.h" 2107e81dc9SJosef Bacik #include "accessors.h" 22*7c8ede16SJosef Bacik #include "file-item.h" 231e1d2701SChris Mason 2442049bf6SChris Mason #define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \ 2542049bf6SChris Mason sizeof(struct btrfs_item) * 2) / \ 2642049bf6SChris Mason size) - 1)) 2707d400a6SYan Zheng 28221b8318SZach Brown #define MAX_CSUM_ITEMS(r, size) (min_t(u32, __MAX_CSUM_ITEMS(r, size), \ 2909cbfeafSKirill A. Shutemov PAGE_SIZE)) 307ca4be45SChris Mason 3143dd529aSDavid Sterba /* 3243dd529aSDavid Sterba * Set inode's size according to filesystem options. 33ca4207aeSNikolay Borisov * 34ca4207aeSNikolay Borisov * @inode: inode we want to update the disk_i_size for 35ca4207aeSNikolay Borisov * @new_i_size: i_size we want to set to, 0 if we use i_size 3641a2ee75SJosef Bacik * 3741a2ee75SJosef Bacik * With NO_HOLES set this simply sets the disk_is_size to whatever i_size_read() 3841a2ee75SJosef Bacik * returns as it is perfectly fine with a file that has holes without hole file 3941a2ee75SJosef Bacik * extent items. 4041a2ee75SJosef Bacik * 4141a2ee75SJosef Bacik * However without NO_HOLES we need to only return the area that is contiguous 4241a2ee75SJosef Bacik * from the 0 offset of the file. Otherwise we could end up adjust i_size up 4341a2ee75SJosef Bacik * to an extent that has a gap in between. 4441a2ee75SJosef Bacik * 4541a2ee75SJosef Bacik * Finally new_i_size should only be set in the case of truncate where we're not 4641a2ee75SJosef Bacik * ready to use i_size_read() as the limiter yet. 4741a2ee75SJosef Bacik */ 4876aea537SNikolay Borisov void btrfs_inode_safe_disk_i_size_write(struct btrfs_inode *inode, u64 new_i_size) 4941a2ee75SJosef Bacik { 5076aea537SNikolay Borisov struct btrfs_fs_info *fs_info = inode->root->fs_info; 5141a2ee75SJosef Bacik u64 start, end, i_size; 5241a2ee75SJosef Bacik int ret; 5341a2ee75SJosef Bacik 5476aea537SNikolay Borisov i_size = new_i_size ?: i_size_read(&inode->vfs_inode); 5541a2ee75SJosef Bacik if (btrfs_fs_incompat(fs_info, NO_HOLES)) { 5676aea537SNikolay Borisov inode->disk_i_size = i_size; 5741a2ee75SJosef Bacik return; 5841a2ee75SJosef Bacik } 5941a2ee75SJosef Bacik 6076aea537SNikolay Borisov spin_lock(&inode->lock); 6176aea537SNikolay Borisov ret = find_contiguous_extent_bit(&inode->file_extent_tree, 0, &start, 6276aea537SNikolay Borisov &end, EXTENT_DIRTY); 6341a2ee75SJosef Bacik if (!ret && start == 0) 6441a2ee75SJosef Bacik i_size = min(i_size, end + 1); 6541a2ee75SJosef Bacik else 6641a2ee75SJosef Bacik i_size = 0; 6776aea537SNikolay Borisov inode->disk_i_size = i_size; 6876aea537SNikolay Borisov spin_unlock(&inode->lock); 6941a2ee75SJosef Bacik } 7041a2ee75SJosef Bacik 7143dd529aSDavid Sterba /* 7243dd529aSDavid Sterba * Mark range within a file as having a new extent inserted. 73ca4207aeSNikolay Borisov * 74ca4207aeSNikolay Borisov * @inode: inode being modified 75ca4207aeSNikolay Borisov * @start: start file offset of the file extent we've inserted 76ca4207aeSNikolay Borisov * @len: logical length of the file extent item 7741a2ee75SJosef Bacik * 7841a2ee75SJosef Bacik * Call when we are inserting a new file extent where there was none before. 7941a2ee75SJosef Bacik * Does not need to call this in the case where we're replacing an existing file 8041a2ee75SJosef Bacik * extent, however if not sure it's fine to call this multiple times. 8141a2ee75SJosef Bacik * 8241a2ee75SJosef Bacik * The start and len must match the file extent item, so thus must be sectorsize 8341a2ee75SJosef Bacik * aligned. 8441a2ee75SJosef Bacik */ 8541a2ee75SJosef Bacik int btrfs_inode_set_file_extent_range(struct btrfs_inode *inode, u64 start, 8641a2ee75SJosef Bacik u64 len) 8741a2ee75SJosef Bacik { 8841a2ee75SJosef Bacik if (len == 0) 8941a2ee75SJosef Bacik return 0; 9041a2ee75SJosef Bacik 9141a2ee75SJosef Bacik ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize)); 9241a2ee75SJosef Bacik 9341a2ee75SJosef Bacik if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES)) 9441a2ee75SJosef Bacik return 0; 9541a2ee75SJosef Bacik return set_extent_bits(&inode->file_extent_tree, start, start + len - 1, 9641a2ee75SJosef Bacik EXTENT_DIRTY); 9741a2ee75SJosef Bacik } 9841a2ee75SJosef Bacik 9943dd529aSDavid Sterba /* 10043dd529aSDavid Sterba * Mark an inode range as not having a backing extent. 101ca4207aeSNikolay Borisov * 102ca4207aeSNikolay Borisov * @inode: inode being modified 103ca4207aeSNikolay Borisov * @start: start file offset of the file extent we've inserted 104ca4207aeSNikolay Borisov * @len: logical length of the file extent item 10541a2ee75SJosef Bacik * 10641a2ee75SJosef Bacik * Called when we drop a file extent, for example when we truncate. Doesn't 10741a2ee75SJosef Bacik * need to be called for cases where we're replacing a file extent, like when 10841a2ee75SJosef Bacik * we've COWed a file extent. 10941a2ee75SJosef Bacik * 11041a2ee75SJosef Bacik * The start and len must match the file extent item, so thus must be sectorsize 11141a2ee75SJosef Bacik * aligned. 11241a2ee75SJosef Bacik */ 11341a2ee75SJosef Bacik int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start, 11441a2ee75SJosef Bacik u64 len) 11541a2ee75SJosef Bacik { 11641a2ee75SJosef Bacik if (len == 0) 11741a2ee75SJosef Bacik return 0; 11841a2ee75SJosef Bacik 11941a2ee75SJosef Bacik ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize) || 12041a2ee75SJosef Bacik len == (u64)-1); 12141a2ee75SJosef Bacik 12241a2ee75SJosef Bacik if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES)) 12341a2ee75SJosef Bacik return 0; 12441a2ee75SJosef Bacik return clear_extent_bit(&inode->file_extent_tree, start, 125bd015294SJosef Bacik start + len - 1, EXTENT_DIRTY, NULL); 12641a2ee75SJosef Bacik } 12741a2ee75SJosef Bacik 1281e25a2e3SJohannes Thumshirn static inline u32 max_ordered_sum_bytes(struct btrfs_fs_info *fs_info, 1291e25a2e3SJohannes Thumshirn u16 csum_size) 1301e25a2e3SJohannes Thumshirn { 1311e25a2e3SJohannes Thumshirn u32 ncsums = (PAGE_SIZE - sizeof(struct btrfs_ordered_sum)) / csum_size; 1321e25a2e3SJohannes Thumshirn 1331e25a2e3SJohannes Thumshirn return ncsums * fs_info->sectorsize; 1341e25a2e3SJohannes Thumshirn } 13507d400a6SYan Zheng 1362b6433c7SJosef Bacik /* 1372b6433c7SJosef Bacik * Calculate the total size needed to allocate for an ordered sum structure 1382b6433c7SJosef Bacik * spanning @bytes in the file. 1392b6433c7SJosef Bacik */ 1402b6433c7SJosef Bacik static int btrfs_ordered_sum_size(struct btrfs_fs_info *fs_info, unsigned long bytes) 1412b6433c7SJosef Bacik { 1422b6433c7SJosef Bacik int num_sectors = (int)DIV_ROUND_UP(bytes, fs_info->sectorsize); 1432b6433c7SJosef Bacik 1442b6433c7SJosef Bacik return sizeof(struct btrfs_ordered_sum) + num_sectors * fs_info->csum_size; 1452b6433c7SJosef Bacik } 1462b6433c7SJosef Bacik 147d1f68ba0SOmar Sandoval int btrfs_insert_hole_extent(struct btrfs_trans_handle *trans, 148dee26a9fSChris Mason struct btrfs_root *root, 149d1f68ba0SOmar Sandoval u64 objectid, u64 pos, u64 num_bytes) 1509f5fae2fSChris Mason { 151dee26a9fSChris Mason int ret = 0; 152dee26a9fSChris Mason struct btrfs_file_extent_item *item; 153dee26a9fSChris Mason struct btrfs_key file_key; 1545caf2a00SChris Mason struct btrfs_path *path; 1555f39d397SChris Mason struct extent_buffer *leaf; 156dee26a9fSChris Mason 1575caf2a00SChris Mason path = btrfs_alloc_path(); 158db5b493aSTsutomu Itoh if (!path) 159db5b493aSTsutomu Itoh return -ENOMEM; 160dee26a9fSChris Mason file_key.objectid = objectid; 161b18c6685SChris Mason file_key.offset = pos; 162962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_DATA_KEY; 163dee26a9fSChris Mason 1645caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 165dee26a9fSChris Mason sizeof(*item)); 16654aa1f4dSChris Mason if (ret < 0) 16754aa1f4dSChris Mason goto out; 16879787eaaSJeff Mahoney BUG_ON(ret); /* Can't happen */ 1695f39d397SChris Mason leaf = path->nodes[0]; 1705f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], 171dee26a9fSChris Mason struct btrfs_file_extent_item); 172d1f68ba0SOmar Sandoval btrfs_set_file_extent_disk_bytenr(leaf, item, 0); 173d1f68ba0SOmar Sandoval btrfs_set_file_extent_disk_num_bytes(leaf, item, 0); 174d1f68ba0SOmar Sandoval btrfs_set_file_extent_offset(leaf, item, 0); 175db94535dSChris Mason btrfs_set_file_extent_num_bytes(leaf, item, num_bytes); 176d1f68ba0SOmar Sandoval btrfs_set_file_extent_ram_bytes(leaf, item, num_bytes); 1775f39d397SChris Mason btrfs_set_file_extent_generation(leaf, item, trans->transid); 1785f39d397SChris Mason btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); 179d1f68ba0SOmar Sandoval btrfs_set_file_extent_compression(leaf, item, 0); 180d1f68ba0SOmar Sandoval btrfs_set_file_extent_encryption(leaf, item, 0); 181d1f68ba0SOmar Sandoval btrfs_set_file_extent_other_encoding(leaf, item, 0); 182c8b97818SChris Mason 1835f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 18454aa1f4dSChris Mason out: 1855caf2a00SChris Mason btrfs_free_path(path); 18654aa1f4dSChris Mason return ret; 1879f5fae2fSChris Mason } 188dee26a9fSChris Mason 18948a3b636SEric Sandeen static struct btrfs_csum_item * 19048a3b636SEric Sandeen btrfs_lookup_csum(struct btrfs_trans_handle *trans, 191b18c6685SChris Mason struct btrfs_root *root, 1926567e837SChris Mason struct btrfs_path *path, 193d20f7043SChris Mason u64 bytenr, int cow) 1946567e837SChris Mason { 1950b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1966567e837SChris Mason int ret; 1976567e837SChris Mason struct btrfs_key file_key; 1986567e837SChris Mason struct btrfs_key found_key; 1996567e837SChris Mason struct btrfs_csum_item *item; 2005f39d397SChris Mason struct extent_buffer *leaf; 2016567e837SChris Mason u64 csum_offset = 0; 202223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 203a429e513SChris Mason int csums_in_item; 2046567e837SChris Mason 205d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 206d20f7043SChris Mason file_key.offset = bytenr; 207962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_CSUM_KEY; 208b18c6685SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); 2096567e837SChris Mason if (ret < 0) 2106567e837SChris Mason goto fail; 2115f39d397SChris Mason leaf = path->nodes[0]; 2126567e837SChris Mason if (ret > 0) { 2136567e837SChris Mason ret = 1; 21470b2befdSChris Mason if (path->slots[0] == 0) 2156567e837SChris Mason goto fail; 2166567e837SChris Mason path->slots[0]--; 2175f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 218962a298fSDavid Sterba if (found_key.type != BTRFS_EXTENT_CSUM_KEY) 2196567e837SChris Mason goto fail; 220d20f7043SChris Mason 221d20f7043SChris Mason csum_offset = (bytenr - found_key.offset) >> 222265fdfa6SDavid Sterba fs_info->sectorsize_bits; 2233212fa14SJosef Bacik csums_in_item = btrfs_item_size(leaf, path->slots[0]); 224607d432dSJosef Bacik csums_in_item /= csum_size; 225a429e513SChris Mason 22682d130ffSMiao Xie if (csum_offset == csums_in_item) { 227a429e513SChris Mason ret = -EFBIG; 2286567e837SChris Mason goto fail; 22982d130ffSMiao Xie } else if (csum_offset > csums_in_item) { 23082d130ffSMiao Xie goto fail; 2316567e837SChris Mason } 2326567e837SChris Mason } 2336567e837SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 234509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 235607d432dSJosef Bacik csum_offset * csum_size); 2366567e837SChris Mason return item; 2376567e837SChris Mason fail: 2386567e837SChris Mason if (ret > 0) 239b18c6685SChris Mason ret = -ENOENT; 2406567e837SChris Mason return ERR_PTR(ret); 2416567e837SChris Mason } 2426567e837SChris Mason 243dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, 244dee26a9fSChris Mason struct btrfs_root *root, 245dee26a9fSChris Mason struct btrfs_path *path, u64 objectid, 2469773a788SChris Mason u64 offset, int mod) 247dee26a9fSChris Mason { 248dee26a9fSChris Mason struct btrfs_key file_key; 249dee26a9fSChris Mason int ins_len = mod < 0 ? -1 : 0; 250dee26a9fSChris Mason int cow = mod != 0; 251dee26a9fSChris Mason 252dee26a9fSChris Mason file_key.objectid = objectid; 25370b2befdSChris Mason file_key.offset = offset; 254962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_DATA_KEY; 255f8ee80deSMarcos Paulo de Souza 256f8ee80deSMarcos Paulo de Souza return btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); 257dee26a9fSChris Mason } 258f254e52cSChris Mason 2596275193eSQu Wenruo /* 2606275193eSQu Wenruo * Find checksums for logical bytenr range [disk_bytenr, disk_bytenr + len) and 26143dd529aSDavid Sterba * store the result to @dst. 2626275193eSQu Wenruo * 2636275193eSQu Wenruo * Return >0 for the number of sectors we found. 2646275193eSQu Wenruo * Return 0 for the range [disk_bytenr, disk_bytenr + sectorsize) has no csum 2656275193eSQu Wenruo * for it. Caller may want to try next sector until one range is hit. 2666275193eSQu Wenruo * Return <0 for fatal error. 2676275193eSQu Wenruo */ 2686275193eSQu Wenruo static int search_csum_tree(struct btrfs_fs_info *fs_info, 2696275193eSQu Wenruo struct btrfs_path *path, u64 disk_bytenr, 2706275193eSQu Wenruo u64 len, u8 *dst) 2716275193eSQu Wenruo { 272fc28b25eSJosef Bacik struct btrfs_root *csum_root; 2736275193eSQu Wenruo struct btrfs_csum_item *item = NULL; 2746275193eSQu Wenruo struct btrfs_key key; 2756275193eSQu Wenruo const u32 sectorsize = fs_info->sectorsize; 2766275193eSQu Wenruo const u32 csum_size = fs_info->csum_size; 2776275193eSQu Wenruo u32 itemsize; 2786275193eSQu Wenruo int ret; 2796275193eSQu Wenruo u64 csum_start; 2806275193eSQu Wenruo u64 csum_len; 2816275193eSQu Wenruo 2826275193eSQu Wenruo ASSERT(IS_ALIGNED(disk_bytenr, sectorsize) && 2836275193eSQu Wenruo IS_ALIGNED(len, sectorsize)); 2846275193eSQu Wenruo 2856275193eSQu Wenruo /* Check if the current csum item covers disk_bytenr */ 2866275193eSQu Wenruo if (path->nodes[0]) { 2876275193eSQu Wenruo item = btrfs_item_ptr(path->nodes[0], path->slots[0], 2886275193eSQu Wenruo struct btrfs_csum_item); 2896275193eSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2903212fa14SJosef Bacik itemsize = btrfs_item_size(path->nodes[0], path->slots[0]); 2916275193eSQu Wenruo 2926275193eSQu Wenruo csum_start = key.offset; 2936275193eSQu Wenruo csum_len = (itemsize / csum_size) * sectorsize; 2946275193eSQu Wenruo 2956275193eSQu Wenruo if (in_range(disk_bytenr, csum_start, csum_len)) 2966275193eSQu Wenruo goto found; 2976275193eSQu Wenruo } 2986275193eSQu Wenruo 2996275193eSQu Wenruo /* Current item doesn't contain the desired range, search again */ 3006275193eSQu Wenruo btrfs_release_path(path); 301fc28b25eSJosef Bacik csum_root = btrfs_csum_root(fs_info, disk_bytenr); 302fc28b25eSJosef Bacik item = btrfs_lookup_csum(NULL, csum_root, path, disk_bytenr, 0); 3036275193eSQu Wenruo if (IS_ERR(item)) { 3046275193eSQu Wenruo ret = PTR_ERR(item); 3056275193eSQu Wenruo goto out; 3066275193eSQu Wenruo } 3076275193eSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 3083212fa14SJosef Bacik itemsize = btrfs_item_size(path->nodes[0], path->slots[0]); 3096275193eSQu Wenruo 3106275193eSQu Wenruo csum_start = key.offset; 3116275193eSQu Wenruo csum_len = (itemsize / csum_size) * sectorsize; 3126275193eSQu Wenruo ASSERT(in_range(disk_bytenr, csum_start, csum_len)); 3136275193eSQu Wenruo 3146275193eSQu Wenruo found: 3156275193eSQu Wenruo ret = (min(csum_start + csum_len, disk_bytenr + len) - 3166275193eSQu Wenruo disk_bytenr) >> fs_info->sectorsize_bits; 3176275193eSQu Wenruo read_extent_buffer(path->nodes[0], dst, (unsigned long)item, 3186275193eSQu Wenruo ret * csum_size); 3196275193eSQu Wenruo out: 32003ddb19dSJosef Bacik if (ret == -ENOENT || ret == -EFBIG) 3216275193eSQu Wenruo ret = 0; 3226275193eSQu Wenruo return ret; 3236275193eSQu Wenruo } 3246275193eSQu Wenruo 3256275193eSQu Wenruo /* 3266275193eSQu Wenruo * Locate the file_offset of @cur_disk_bytenr of a @bio. 3276275193eSQu Wenruo * 3286275193eSQu Wenruo * Bio of btrfs represents read range of 3296275193eSQu Wenruo * [bi_sector << 9, bi_sector << 9 + bi_size). 3306275193eSQu Wenruo * Knowing this, we can iterate through each bvec to locate the page belong to 3316275193eSQu Wenruo * @cur_disk_bytenr and get the file offset. 3326275193eSQu Wenruo * 3336275193eSQu Wenruo * @inode is used to determine if the bvec page really belongs to @inode. 3346275193eSQu Wenruo * 3356275193eSQu Wenruo * Return 0 if we can't find the file offset 3366275193eSQu Wenruo * Return >0 if we find the file offset and restore it to @file_offset_ret 3376275193eSQu Wenruo */ 3386275193eSQu Wenruo static int search_file_offset_in_bio(struct bio *bio, struct inode *inode, 3396275193eSQu Wenruo u64 disk_bytenr, u64 *file_offset_ret) 3406275193eSQu Wenruo { 3416275193eSQu Wenruo struct bvec_iter iter; 3426275193eSQu Wenruo struct bio_vec bvec; 3436275193eSQu Wenruo u64 cur = bio->bi_iter.bi_sector << SECTOR_SHIFT; 3446275193eSQu Wenruo int ret = 0; 3456275193eSQu Wenruo 3466275193eSQu Wenruo bio_for_each_segment(bvec, bio, iter) { 3476275193eSQu Wenruo struct page *page = bvec.bv_page; 3486275193eSQu Wenruo 3496275193eSQu Wenruo if (cur > disk_bytenr) 3506275193eSQu Wenruo break; 3516275193eSQu Wenruo if (cur + bvec.bv_len <= disk_bytenr) { 3526275193eSQu Wenruo cur += bvec.bv_len; 3536275193eSQu Wenruo continue; 3546275193eSQu Wenruo } 3556275193eSQu Wenruo ASSERT(in_range(disk_bytenr, cur, bvec.bv_len)); 3566275193eSQu Wenruo if (page->mapping && page->mapping->host && 3576275193eSQu Wenruo page->mapping->host == inode) { 3586275193eSQu Wenruo ret = 1; 3596275193eSQu Wenruo *file_offset_ret = page_offset(page) + bvec.bv_offset + 3606275193eSQu Wenruo disk_bytenr - cur; 3616275193eSQu Wenruo break; 3626275193eSQu Wenruo } 3636275193eSQu Wenruo } 3646275193eSQu Wenruo return ret; 3656275193eSQu Wenruo } 3666275193eSQu Wenruo 36743dd529aSDavid Sterba /* 3686275193eSQu Wenruo * Lookup the checksum for the read bio in csum tree. 3699e46458aSQu Wenruo * 370e62958fcSOmar Sandoval * @inode: inode that the bio is for. 371fb30f470SOmar Sandoval * @bio: bio to look up. 372fb30f470SOmar Sandoval * @dst: Buffer of size nblocks * btrfs_super_csum_size() used to return 373fb30f470SOmar Sandoval * checksum (nblocks = bio->bi_iter.bi_size / fs_info->sectorsize). If 374fb30f470SOmar Sandoval * NULL, the checksum buffer is allocated and returned in 375c3a3b19bSQu Wenruo * btrfs_bio(bio)->csum instead. 376e62958fcSOmar Sandoval * 377e62958fcSOmar Sandoval * Return: BLK_STS_RESOURCE if allocating memory fails, BLK_STS_OK otherwise. 378e62958fcSOmar Sandoval */ 3796275193eSQu Wenruo blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, u8 *dst) 38061b49440SChris Mason { 3810b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 382facc8a22SMiao Xie struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 3831784b7d5SJosef Bacik struct btrfs_bio *bbio = NULL; 384facc8a22SMiao Xie struct btrfs_path *path; 3856275193eSQu Wenruo const u32 sectorsize = fs_info->sectorsize; 386223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 3876275193eSQu Wenruo u32 orig_len = bio->bi_iter.bi_size; 3886275193eSQu Wenruo u64 orig_disk_bytenr = bio->bi_iter.bi_sector << SECTOR_SHIFT; 3896275193eSQu Wenruo u64 cur_disk_bytenr; 3906275193eSQu Wenruo u8 *csum; 3916275193eSQu Wenruo const unsigned int nblocks = orig_len >> fs_info->sectorsize_bits; 3926275193eSQu Wenruo int count = 0; 3931784b7d5SJosef Bacik blk_status_t ret = BLK_STS_OK; 39461b49440SChris Mason 395056c8311SJosef Bacik if ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) || 396056c8311SJosef Bacik test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state)) 397334c16d8SJosef Bacik return BLK_STS_OK; 398334c16d8SJosef Bacik 3999e46458aSQu Wenruo /* 4009e46458aSQu Wenruo * This function is only called for read bio. 4019e46458aSQu Wenruo * 4029e46458aSQu Wenruo * This means two things: 4039e46458aSQu Wenruo * - All our csums should only be in csum tree 4049e46458aSQu Wenruo * No ordered extents csums, as ordered extents are only for write 4059e46458aSQu Wenruo * path. 4066275193eSQu Wenruo * - No need to bother any other info from bvec 4076275193eSQu Wenruo * Since we're looking up csums, the only important info is the 4086275193eSQu Wenruo * disk_bytenr and the length, which can be extracted from bi_iter 4096275193eSQu Wenruo * directly. 4109e46458aSQu Wenruo */ 4119e46458aSQu Wenruo ASSERT(bio_op(bio) == REQ_OP_READ); 41261b49440SChris Mason path = btrfs_alloc_path(); 413c2db1073STsutomu Itoh if (!path) 4144e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 415facc8a22SMiao Xie 416facc8a22SMiao Xie if (!dst) { 4171784b7d5SJosef Bacik bbio = btrfs_bio(bio); 418fb30f470SOmar Sandoval 419facc8a22SMiao Xie if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) { 420c3a3b19bSQu Wenruo bbio->csum = kmalloc_array(nblocks, csum_size, GFP_NOFS); 421c3a3b19bSQu Wenruo if (!bbio->csum) { 422facc8a22SMiao Xie btrfs_free_path(path); 4234e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 424facc8a22SMiao Xie } 425facc8a22SMiao Xie } else { 426c3a3b19bSQu Wenruo bbio->csum = bbio->csum_inline; 427facc8a22SMiao Xie } 428c3a3b19bSQu Wenruo csum = bbio->csum; 429facc8a22SMiao Xie } else { 43010fe6ca8SJohannes Thumshirn csum = dst; 431facc8a22SMiao Xie } 432facc8a22SMiao Xie 43335478d05SQu Wenruo /* 43435478d05SQu Wenruo * If requested number of sectors is larger than one leaf can contain, 43535478d05SQu Wenruo * kick the readahead for csum tree. 43635478d05SQu Wenruo */ 43735478d05SQu Wenruo if (nblocks > fs_info->csums_per_leaf) 438e4058b54SDavid Sterba path->reada = READA_FORWARD; 43961b49440SChris Mason 4402cf8572dSChris Mason /* 4412cf8572dSChris Mason * the free space stuff is only read when it hasn't been 4422cf8572dSChris Mason * updated in the current transaction. So, we can safely 4432cf8572dSChris Mason * read from the commit root and sidestep a nasty deadlock 4442cf8572dSChris Mason * between reading the free space cache and updating the csum tree. 4452cf8572dSChris Mason */ 44670ddc553SNikolay Borisov if (btrfs_is_free_space_inode(BTRFS_I(inode))) { 4472cf8572dSChris Mason path->search_commit_root = 1; 448ddf23b3fSJosef Bacik path->skip_locking = 1; 449ddf23b3fSJosef Bacik } 4502cf8572dSChris Mason 4516275193eSQu Wenruo for (cur_disk_bytenr = orig_disk_bytenr; 4526275193eSQu Wenruo cur_disk_bytenr < orig_disk_bytenr + orig_len; 4536275193eSQu Wenruo cur_disk_bytenr += (count * sectorsize)) { 4546275193eSQu Wenruo u64 search_len = orig_disk_bytenr + orig_len - cur_disk_bytenr; 4556275193eSQu Wenruo unsigned int sector_offset; 4566275193eSQu Wenruo u8 *csum_dst; 457c40a3d38SChandan Rajendra 4586275193eSQu Wenruo /* 4596275193eSQu Wenruo * Although both cur_disk_bytenr and orig_disk_bytenr is u64, 4606275193eSQu Wenruo * we're calculating the offset to the bio start. 4616275193eSQu Wenruo * 4626275193eSQu Wenruo * Bio size is limited to UINT_MAX, thus unsigned int is large 4636275193eSQu Wenruo * enough to contain the raw result, not to mention the right 4646275193eSQu Wenruo * shifted result. 4656275193eSQu Wenruo */ 4666275193eSQu Wenruo ASSERT(cur_disk_bytenr - orig_disk_bytenr < UINT_MAX); 4676275193eSQu Wenruo sector_offset = (cur_disk_bytenr - orig_disk_bytenr) >> 4686275193eSQu Wenruo fs_info->sectorsize_bits; 4696275193eSQu Wenruo csum_dst = csum + sector_offset * csum_size; 4704989d277SChristoph Hellwig 4716275193eSQu Wenruo count = search_csum_tree(fs_info, path, cur_disk_bytenr, 4726275193eSQu Wenruo search_len, csum_dst); 4731784b7d5SJosef Bacik if (count < 0) { 4741784b7d5SJosef Bacik ret = errno_to_blk_status(count); 4751784b7d5SJosef Bacik if (bbio) 4761784b7d5SJosef Bacik btrfs_bio_free_csum(bbio); 4771784b7d5SJosef Bacik break; 4781784b7d5SJosef Bacik } 4791784b7d5SJosef Bacik 4806275193eSQu Wenruo /* 4811784b7d5SJosef Bacik * We didn't find a csum for this range. We need to make sure 4821784b7d5SJosef Bacik * we complain loudly about this, because we are not NODATASUM. 4831784b7d5SJosef Bacik * 4841784b7d5SJosef Bacik * However for the DATA_RELOC inode we could potentially be 4851784b7d5SJosef Bacik * relocating data extents for a NODATASUM inode, so the inode 4861784b7d5SJosef Bacik * itself won't be marked with NODATASUM, but the extent we're 4871784b7d5SJosef Bacik * copying is in fact NODATASUM. If we don't find a csum we 4881784b7d5SJosef Bacik * assume this is the case. 4896275193eSQu Wenruo */ 4901784b7d5SJosef Bacik if (count == 0) { 4916275193eSQu Wenruo memset(csum_dst, 0, csum_size); 492e4100d98SMiao Xie count = 1; 4936275193eSQu Wenruo 49417d217feSYan Zheng if (BTRFS_I(inode)->root->root_key.objectid == 49517d217feSYan Zheng BTRFS_DATA_RELOC_TREE_OBJECTID) { 4966275193eSQu Wenruo u64 file_offset; 4976275193eSQu Wenruo int ret; 4986275193eSQu Wenruo 4996275193eSQu Wenruo ret = search_file_offset_in_bio(bio, inode, 5006275193eSQu Wenruo cur_disk_bytenr, &file_offset); 5016275193eSQu Wenruo if (ret) 5026275193eSQu Wenruo set_extent_bits(io_tree, file_offset, 5036275193eSQu Wenruo file_offset + sectorsize - 1, 504ceeb0ae7SDavid Sterba EXTENT_NODATASUM); 50517d217feSYan Zheng } else { 5066275193eSQu Wenruo btrfs_warn_rl(fs_info, 5076275193eSQu Wenruo "csum hole found for disk bytenr range [%llu, %llu)", 5086275193eSQu Wenruo cur_disk_bytenr, cur_disk_bytenr + sectorsize); 50917d217feSYan Zheng } 5104989d277SChristoph Hellwig } 5114989d277SChristoph Hellwig } 5124989d277SChristoph Hellwig 51361b49440SChris Mason btrfs_free_path(path); 5141784b7d5SJosef Bacik return ret; 5154b46fce2SJosef Bacik } 5164b46fce2SJosef Bacik 51717d217feSYan Zheng int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, 51826ce9114SJosef Bacik struct list_head *list, int search_commit, 51926ce9114SJosef Bacik bool nowait) 52017d217feSYan Zheng { 5210b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 52217d217feSYan Zheng struct btrfs_key key; 52317d217feSYan Zheng struct btrfs_path *path; 52417d217feSYan Zheng struct extent_buffer *leaf; 52517d217feSYan Zheng struct btrfs_ordered_sum *sums; 52617d217feSYan Zheng struct btrfs_csum_item *item; 5270678b618SMark Fasheh LIST_HEAD(tmplist); 52817d217feSYan Zheng unsigned long offset; 52917d217feSYan Zheng int ret; 53017d217feSYan Zheng size_t size; 53117d217feSYan Zheng u64 csum_end; 532223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 53317d217feSYan Zheng 5340b246afaSJeff Mahoney ASSERT(IS_ALIGNED(start, fs_info->sectorsize) && 5350b246afaSJeff Mahoney IS_ALIGNED(end + 1, fs_info->sectorsize)); 5364277a9c3SJosef Bacik 53717d217feSYan Zheng path = btrfs_alloc_path(); 538d8926bb3SMark Fasheh if (!path) 539d8926bb3SMark Fasheh return -ENOMEM; 54017d217feSYan Zheng 54126ce9114SJosef Bacik path->nowait = nowait; 542a2de733cSArne Jansen if (search_commit) { 543a2de733cSArne Jansen path->skip_locking = 1; 544e4058b54SDavid Sterba path->reada = READA_FORWARD; 545a2de733cSArne Jansen path->search_commit_root = 1; 546a2de733cSArne Jansen } 547a2de733cSArne Jansen 54817d217feSYan Zheng key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 54917d217feSYan Zheng key.offset = start; 55017d217feSYan Zheng key.type = BTRFS_EXTENT_CSUM_KEY; 55117d217feSYan Zheng 55207d400a6SYan Zheng ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 55317d217feSYan Zheng if (ret < 0) 55417d217feSYan Zheng goto fail; 55517d217feSYan Zheng if (ret > 0 && path->slots[0] > 0) { 55617d217feSYan Zheng leaf = path->nodes[0]; 55717d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); 55817d217feSYan Zheng if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID && 55917d217feSYan Zheng key.type == BTRFS_EXTENT_CSUM_KEY) { 560265fdfa6SDavid Sterba offset = (start - key.offset) >> fs_info->sectorsize_bits; 56117d217feSYan Zheng if (offset * csum_size < 5623212fa14SJosef Bacik btrfs_item_size(leaf, path->slots[0] - 1)) 56317d217feSYan Zheng path->slots[0]--; 56417d217feSYan Zheng } 56517d217feSYan Zheng } 56617d217feSYan Zheng 56717d217feSYan Zheng while (start <= end) { 56817d217feSYan Zheng leaf = path->nodes[0]; 56917d217feSYan Zheng if (path->slots[0] >= btrfs_header_nritems(leaf)) { 57007d400a6SYan Zheng ret = btrfs_next_leaf(root, path); 57117d217feSYan Zheng if (ret < 0) 57217d217feSYan Zheng goto fail; 57317d217feSYan Zheng if (ret > 0) 57417d217feSYan Zheng break; 57517d217feSYan Zheng leaf = path->nodes[0]; 57617d217feSYan Zheng } 57717d217feSYan Zheng 57817d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 57917d217feSYan Zheng if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 580628c8282SZhi Yong Wu key.type != BTRFS_EXTENT_CSUM_KEY || 581628c8282SZhi Yong Wu key.offset > end) 58217d217feSYan Zheng break; 58317d217feSYan Zheng 58417d217feSYan Zheng if (key.offset > start) 58517d217feSYan Zheng start = key.offset; 58617d217feSYan Zheng 5873212fa14SJosef Bacik size = btrfs_item_size(leaf, path->slots[0]); 5880b246afaSJeff Mahoney csum_end = key.offset + (size / csum_size) * fs_info->sectorsize; 58987b29b20SYan Zheng if (csum_end <= start) { 59087b29b20SYan Zheng path->slots[0]++; 59187b29b20SYan Zheng continue; 59287b29b20SYan Zheng } 59317d217feSYan Zheng 59407d400a6SYan Zheng csum_end = min(csum_end, end + 1); 59507d400a6SYan Zheng item = btrfs_item_ptr(path->nodes[0], path->slots[0], 59607d400a6SYan Zheng struct btrfs_csum_item); 59707d400a6SYan Zheng while (start < csum_end) { 59807d400a6SYan Zheng size = min_t(size_t, csum_end - start, 5991e25a2e3SJohannes Thumshirn max_ordered_sum_bytes(fs_info, csum_size)); 6000b246afaSJeff Mahoney sums = kzalloc(btrfs_ordered_sum_size(fs_info, size), 60107d400a6SYan Zheng GFP_NOFS); 6020678b618SMark Fasheh if (!sums) { 6030678b618SMark Fasheh ret = -ENOMEM; 6040678b618SMark Fasheh goto fail; 6050678b618SMark Fasheh } 60617d217feSYan Zheng 60717d217feSYan Zheng sums->bytenr = start; 608f51a4a18SMiao Xie sums->len = (int)size; 60917d217feSYan Zheng 610265fdfa6SDavid Sterba offset = (start - key.offset) >> fs_info->sectorsize_bits; 61117d217feSYan Zheng offset *= csum_size; 612265fdfa6SDavid Sterba size >>= fs_info->sectorsize_bits; 61317d217feSYan Zheng 61407d400a6SYan Zheng read_extent_buffer(path->nodes[0], 615f51a4a18SMiao Xie sums->sums, 616f51a4a18SMiao Xie ((unsigned long)item) + offset, 617f51a4a18SMiao Xie csum_size * size); 61817d217feSYan Zheng 6190b246afaSJeff Mahoney start += fs_info->sectorsize * size; 6200678b618SMark Fasheh list_add_tail(&sums->list, &tmplist); 62107d400a6SYan Zheng } 62217d217feSYan Zheng path->slots[0]++; 62317d217feSYan Zheng } 62417d217feSYan Zheng ret = 0; 62517d217feSYan Zheng fail: 6260678b618SMark Fasheh while (ret < 0 && !list_empty(&tmplist)) { 6276e5aafb2SChris Mason sums = list_entry(tmplist.next, struct btrfs_ordered_sum, list); 6280678b618SMark Fasheh list_del(&sums->list); 6290678b618SMark Fasheh kfree(sums); 6300678b618SMark Fasheh } 6310678b618SMark Fasheh list_splice_tail(&tmplist, list); 6320678b618SMark Fasheh 63317d217feSYan Zheng btrfs_free_path(path); 63417d217feSYan Zheng return ret; 63517d217feSYan Zheng } 63617d217feSYan Zheng 63743dd529aSDavid Sterba /* 63843dd529aSDavid Sterba * Calculate checksums of the data contained inside a bio. 639e331f6b1SOmar Sandoval * 64051d470aeSNikolay Borisov * @inode: Owner of the data inside the bio 64151d470aeSNikolay Borisov * @bio: Contains the data to be checksummed 642e331f6b1SOmar Sandoval * @offset: If (u64)-1, @bio may contain discontiguous bio vecs, so the 643e331f6b1SOmar Sandoval * file offsets are determined from the page offsets in the bio. 644e331f6b1SOmar Sandoval * Otherwise, this is the starting file offset of the bio vecs in 645e331f6b1SOmar Sandoval * @bio, which must be contiguous. 646e331f6b1SOmar Sandoval * @one_ordered: If true, @bio only refers to one ordered extent. 64751d470aeSNikolay Borisov */ 648bd242a08SNikolay Borisov blk_status_t btrfs_csum_one_bio(struct btrfs_inode *inode, struct bio *bio, 649e331f6b1SOmar Sandoval u64 offset, bool one_ordered) 650e015640fSChris Mason { 651c3504372SNikolay Borisov struct btrfs_fs_info *fs_info = inode->root->fs_info; 652d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 653e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums; 6546cd7ce49SChristoph Hellwig struct btrfs_ordered_extent *ordered = NULL; 655e331f6b1SOmar Sandoval const bool use_page_offsets = (offset == (u64)-1); 656e015640fSChris Mason char *data; 65717347cecSLiu Bo struct bvec_iter iter; 65817347cecSLiu Bo struct bio_vec bvec; 659f51a4a18SMiao Xie int index; 660e331f6b1SOmar Sandoval unsigned int blockcount; 6613edf7d33SChris Mason unsigned long total_bytes = 0; 6623edf7d33SChris Mason unsigned long this_sum_bytes = 0; 66317347cecSLiu Bo int i; 664a3d46aeaSNikolay Borisov unsigned nofs_flag; 665e015640fSChris Mason 666a3d46aeaSNikolay Borisov nofs_flag = memalloc_nofs_save(); 667a3d46aeaSNikolay Borisov sums = kvzalloc(btrfs_ordered_sum_size(fs_info, bio->bi_iter.bi_size), 668a3d46aeaSNikolay Borisov GFP_KERNEL); 669a3d46aeaSNikolay Borisov memalloc_nofs_restore(nofs_flag); 670a3d46aeaSNikolay Borisov 671e015640fSChris Mason if (!sums) 6724e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 6733edf7d33SChris Mason 6744f024f37SKent Overstreet sums->len = bio->bi_iter.bi_size; 675e6dcd2dcSChris Mason INIT_LIST_HEAD(&sums->list); 676d20f7043SChris Mason 6771201b58bSDavid Sterba sums->bytenr = bio->bi_iter.bi_sector << 9; 678f51a4a18SMiao Xie index = 0; 679e015640fSChris Mason 680d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 681d5178578SJohannes Thumshirn 68217347cecSLiu Bo bio_for_each_segment(bvec, bio, iter) { 683e331f6b1SOmar Sandoval if (use_page_offsets) 68417347cecSLiu Bo offset = page_offset(bvec.bv_page) + bvec.bv_offset; 685d20f7043SChris Mason 6866cd7ce49SChristoph Hellwig if (!ordered) { 6876cd7ce49SChristoph Hellwig ordered = btrfs_lookup_ordered_extent(inode, offset); 688bbc9a6ebSQu Wenruo /* 689bbc9a6ebSQu Wenruo * The bio range is not covered by any ordered extent, 690bbc9a6ebSQu Wenruo * must be a code logic error. 691bbc9a6ebSQu Wenruo */ 692bbc9a6ebSQu Wenruo if (unlikely(!ordered)) { 693bbc9a6ebSQu Wenruo WARN(1, KERN_WARNING 694bbc9a6ebSQu Wenruo "no ordered extent for root %llu ino %llu offset %llu\n", 695bbc9a6ebSQu Wenruo inode->root->root_key.objectid, 696bbc9a6ebSQu Wenruo btrfs_ino(inode), offset); 697bbc9a6ebSQu Wenruo kvfree(sums); 698bbc9a6ebSQu Wenruo return BLK_STS_IOERR; 699bbc9a6ebSQu Wenruo } 7006cd7ce49SChristoph Hellwig } 7016cd7ce49SChristoph Hellwig 702e331f6b1SOmar Sandoval blockcount = BTRFS_BYTES_TO_BLKS(fs_info, 70317347cecSLiu Bo bvec.bv_len + fs_info->sectorsize 704c40a3d38SChandan Rajendra - 1); 705c40a3d38SChandan Rajendra 706e331f6b1SOmar Sandoval for (i = 0; i < blockcount; i++) { 707e331f6b1SOmar Sandoval if (!one_ordered && 708e331f6b1SOmar Sandoval !in_range(offset, ordered->file_offset, 709e331f6b1SOmar Sandoval ordered->num_bytes)) { 7103edf7d33SChris Mason unsigned long bytes_left; 711c40a3d38SChandan Rajendra 7123edf7d33SChris Mason sums->len = this_sum_bytes; 7133edf7d33SChris Mason this_sum_bytes = 0; 714f9756261SNikolay Borisov btrfs_add_ordered_sum(ordered, sums); 7153edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 7163edf7d33SChris Mason 7174f024f37SKent Overstreet bytes_left = bio->bi_iter.bi_size - total_bytes; 7183edf7d33SChris Mason 719a3d46aeaSNikolay Borisov nofs_flag = memalloc_nofs_save(); 720a3d46aeaSNikolay Borisov sums = kvzalloc(btrfs_ordered_sum_size(fs_info, 721a3d46aeaSNikolay Borisov bytes_left), GFP_KERNEL); 722a3d46aeaSNikolay Borisov memalloc_nofs_restore(nofs_flag); 72379787eaaSJeff Mahoney BUG_ON(!sums); /* -ENOMEM */ 7243edf7d33SChris Mason sums->len = bytes_left; 725c40a3d38SChandan Rajendra ordered = btrfs_lookup_ordered_extent(inode, 726c40a3d38SChandan Rajendra offset); 727c40a3d38SChandan Rajendra ASSERT(ordered); /* Logic error */ 7281201b58bSDavid Sterba sums->bytenr = (bio->bi_iter.bi_sector << 9) 729c40a3d38SChandan Rajendra + total_bytes; 730f51a4a18SMiao Xie index = 0; 731c40a3d38SChandan Rajendra } 732c40a3d38SChandan Rajendra 7333dcfbcceSChristoph Hellwig data = bvec_kmap_local(&bvec); 7343dcfbcceSChristoph Hellwig crypto_shash_digest(shash, 7353dcfbcceSChristoph Hellwig data + (i * fs_info->sectorsize), 736fd08001fSEric Biggers fs_info->sectorsize, 737fd08001fSEric Biggers sums->sums + index); 7383dcfbcceSChristoph Hellwig kunmap_local(data); 739713cebfbSDavid Sterba index += fs_info->csum_size; 7400b246afaSJeff Mahoney offset += fs_info->sectorsize; 7410b246afaSJeff Mahoney this_sum_bytes += fs_info->sectorsize; 7420b246afaSJeff Mahoney total_bytes += fs_info->sectorsize; 743c40a3d38SChandan Rajendra } 744c40a3d38SChandan Rajendra 745e015640fSChris Mason } 746ed98b56aSChris Mason this_sum_bytes = 0; 747f9756261SNikolay Borisov btrfs_add_ordered_sum(ordered, sums); 7483edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 749e015640fSChris Mason return 0; 750e015640fSChris Mason } 751e015640fSChris Mason 752459931ecSChris Mason /* 75343dd529aSDavid Sterba * Remove one checksum overlapping a range. 754459931ecSChris Mason * 75543dd529aSDavid Sterba * This expects the key to describe the csum pointed to by the path, and it 75643dd529aSDavid Sterba * expects the csum to overlap the range [bytenr, len] 757459931ecSChris Mason * 75843dd529aSDavid Sterba * The csum should not be entirely contained in the range and the range should 75943dd529aSDavid Sterba * not be entirely contained in the csum. 76043dd529aSDavid Sterba * 76143dd529aSDavid Sterba * This calls btrfs_truncate_item with the correct args based on the overlap, 76243dd529aSDavid Sterba * and fixes up the key as required. 763459931ecSChris Mason */ 7642ff7e61eSJeff Mahoney static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info, 765459931ecSChris Mason struct btrfs_path *path, 766459931ecSChris Mason struct btrfs_key *key, 767459931ecSChris Mason u64 bytenr, u64 len) 768459931ecSChris Mason { 769459931ecSChris Mason struct extent_buffer *leaf; 770223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 771459931ecSChris Mason u64 csum_end; 772459931ecSChris Mason u64 end_byte = bytenr + len; 773265fdfa6SDavid Sterba u32 blocksize_bits = fs_info->sectorsize_bits; 774459931ecSChris Mason 775459931ecSChris Mason leaf = path->nodes[0]; 7763212fa14SJosef Bacik csum_end = btrfs_item_size(leaf, path->slots[0]) / csum_size; 777265fdfa6SDavid Sterba csum_end <<= blocksize_bits; 778459931ecSChris Mason csum_end += key->offset; 779459931ecSChris Mason 780459931ecSChris Mason if (key->offset < bytenr && csum_end <= end_byte) { 781459931ecSChris Mason /* 782459931ecSChris Mason * [ bytenr - len ] 783459931ecSChris Mason * [ ] 784459931ecSChris Mason * [csum ] 785459931ecSChris Mason * A simple truncate off the end of the item 786459931ecSChris Mason */ 787459931ecSChris Mason u32 new_size = (bytenr - key->offset) >> blocksize_bits; 788459931ecSChris Mason new_size *= csum_size; 78978ac4f9eSDavid Sterba btrfs_truncate_item(path, new_size, 1); 790459931ecSChris Mason } else if (key->offset >= bytenr && csum_end > end_byte && 791459931ecSChris Mason end_byte > key->offset) { 792459931ecSChris Mason /* 793459931ecSChris Mason * [ bytenr - len ] 794459931ecSChris Mason * [ ] 795459931ecSChris Mason * [csum ] 796459931ecSChris Mason * we need to truncate from the beginning of the csum 797459931ecSChris Mason */ 798459931ecSChris Mason u32 new_size = (csum_end - end_byte) >> blocksize_bits; 799459931ecSChris Mason new_size *= csum_size; 800459931ecSChris Mason 80178ac4f9eSDavid Sterba btrfs_truncate_item(path, new_size, 0); 802459931ecSChris Mason 803459931ecSChris Mason key->offset = end_byte; 8040b246afaSJeff Mahoney btrfs_set_item_key_safe(fs_info, path, key); 805459931ecSChris Mason } else { 806459931ecSChris Mason BUG(); 807459931ecSChris Mason } 808459931ecSChris Mason } 809459931ecSChris Mason 810459931ecSChris Mason /* 81143dd529aSDavid Sterba * Delete the csum items from the csum tree for a given range of bytes. 812459931ecSChris Mason */ 813459931ecSChris Mason int btrfs_del_csums(struct btrfs_trans_handle *trans, 81440e046acSFilipe Manana struct btrfs_root *root, u64 bytenr, u64 len) 815459931ecSChris Mason { 81640e046acSFilipe Manana struct btrfs_fs_info *fs_info = trans->fs_info; 817459931ecSChris Mason struct btrfs_path *path; 818459931ecSChris Mason struct btrfs_key key; 819459931ecSChris Mason u64 end_byte = bytenr + len; 820459931ecSChris Mason u64 csum_end; 821459931ecSChris Mason struct extent_buffer *leaf; 822b86652beSJosef Bacik int ret = 0; 823223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 824265fdfa6SDavid Sterba u32 blocksize_bits = fs_info->sectorsize_bits; 825459931ecSChris Mason 82684d2d6c7SJosef Bacik ASSERT(root->root_key.objectid == BTRFS_CSUM_TREE_OBJECTID || 82740e046acSFilipe Manana root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); 82840e046acSFilipe Manana 829459931ecSChris Mason path = btrfs_alloc_path(); 8302a29edc6Sliubo if (!path) 8312a29edc6Sliubo return -ENOMEM; 832459931ecSChris Mason 833459931ecSChris Mason while (1) { 834459931ecSChris Mason key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 835459931ecSChris Mason key.offset = end_byte - 1; 836459931ecSChris Mason key.type = BTRFS_EXTENT_CSUM_KEY; 837459931ecSChris Mason 838459931ecSChris Mason ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 839459931ecSChris Mason if (ret > 0) { 840b86652beSJosef Bacik ret = 0; 841459931ecSChris Mason if (path->slots[0] == 0) 84265a246c5STsutomu Itoh break; 843459931ecSChris Mason path->slots[0]--; 844ad0397a7SJosef Bacik } else if (ret < 0) { 84565a246c5STsutomu Itoh break; 846459931ecSChris Mason } 847ad0397a7SJosef Bacik 848459931ecSChris Mason leaf = path->nodes[0]; 849459931ecSChris Mason btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 850459931ecSChris Mason 851459931ecSChris Mason if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 852459931ecSChris Mason key.type != BTRFS_EXTENT_CSUM_KEY) { 853459931ecSChris Mason break; 854459931ecSChris Mason } 855459931ecSChris Mason 856459931ecSChris Mason if (key.offset >= end_byte) 857459931ecSChris Mason break; 858459931ecSChris Mason 8593212fa14SJosef Bacik csum_end = btrfs_item_size(leaf, path->slots[0]) / csum_size; 860459931ecSChris Mason csum_end <<= blocksize_bits; 861459931ecSChris Mason csum_end += key.offset; 862459931ecSChris Mason 863459931ecSChris Mason /* this csum ends before we start, we're done */ 864459931ecSChris Mason if (csum_end <= bytenr) 865459931ecSChris Mason break; 866459931ecSChris Mason 867459931ecSChris Mason /* delete the entire item, it is inside our range */ 868459931ecSChris Mason if (key.offset >= bytenr && csum_end <= end_byte) { 8696f546216SFilipe Manana int del_nr = 1; 8706f546216SFilipe Manana 8716f546216SFilipe Manana /* 8726f546216SFilipe Manana * Check how many csum items preceding this one in this 8736f546216SFilipe Manana * leaf correspond to our range and then delete them all 8746f546216SFilipe Manana * at once. 8756f546216SFilipe Manana */ 8766f546216SFilipe Manana if (key.offset > bytenr && path->slots[0] > 0) { 8776f546216SFilipe Manana int slot = path->slots[0] - 1; 8786f546216SFilipe Manana 8796f546216SFilipe Manana while (slot >= 0) { 8806f546216SFilipe Manana struct btrfs_key pk; 8816f546216SFilipe Manana 8826f546216SFilipe Manana btrfs_item_key_to_cpu(leaf, &pk, slot); 8836f546216SFilipe Manana if (pk.offset < bytenr || 8846f546216SFilipe Manana pk.type != BTRFS_EXTENT_CSUM_KEY || 8856f546216SFilipe Manana pk.objectid != 8866f546216SFilipe Manana BTRFS_EXTENT_CSUM_OBJECTID) 8876f546216SFilipe Manana break; 8886f546216SFilipe Manana path->slots[0] = slot; 8896f546216SFilipe Manana del_nr++; 8906f546216SFilipe Manana key.offset = pk.offset; 8916f546216SFilipe Manana slot--; 8926f546216SFilipe Manana } 8936f546216SFilipe Manana } 8946f546216SFilipe Manana ret = btrfs_del_items(trans, root, path, 8956f546216SFilipe Manana path->slots[0], del_nr); 89665a246c5STsutomu Itoh if (ret) 897b86652beSJosef Bacik break; 898dcbdd4dcSChris Mason if (key.offset == bytenr) 899dcbdd4dcSChris Mason break; 900459931ecSChris Mason } else if (key.offset < bytenr && csum_end > end_byte) { 901459931ecSChris Mason unsigned long offset; 902459931ecSChris Mason unsigned long shift_len; 903459931ecSChris Mason unsigned long item_offset; 904459931ecSChris Mason /* 905459931ecSChris Mason * [ bytenr - len ] 906459931ecSChris Mason * [csum ] 907459931ecSChris Mason * 908459931ecSChris Mason * Our bytes are in the middle of the csum, 909459931ecSChris Mason * we need to split this item and insert a new one. 910459931ecSChris Mason * 911459931ecSChris Mason * But we can't drop the path because the 912459931ecSChris Mason * csum could change, get removed, extended etc. 913459931ecSChris Mason * 914459931ecSChris Mason * The trick here is the max size of a csum item leaves 915459931ecSChris Mason * enough room in the tree block for a single 916459931ecSChris Mason * item header. So, we split the item in place, 917459931ecSChris Mason * adding a new header pointing to the existing 918459931ecSChris Mason * bytes. Then we loop around again and we have 919459931ecSChris Mason * a nicely formed csum item that we can neatly 920459931ecSChris Mason * truncate. 921459931ecSChris Mason */ 922459931ecSChris Mason offset = (bytenr - key.offset) >> blocksize_bits; 923459931ecSChris Mason offset *= csum_size; 924459931ecSChris Mason 925459931ecSChris Mason shift_len = (len >> blocksize_bits) * csum_size; 926459931ecSChris Mason 927459931ecSChris Mason item_offset = btrfs_item_ptr_offset(leaf, 928459931ecSChris Mason path->slots[0]); 929459931ecSChris Mason 930b159fa28SDavid Sterba memzero_extent_buffer(leaf, item_offset + offset, 931459931ecSChris Mason shift_len); 932459931ecSChris Mason key.offset = bytenr; 933459931ecSChris Mason 934459931ecSChris Mason /* 935459931ecSChris Mason * btrfs_split_item returns -EAGAIN when the 936459931ecSChris Mason * item changed size or key 937459931ecSChris Mason */ 938459931ecSChris Mason ret = btrfs_split_item(trans, root, path, &key, offset); 93979787eaaSJeff Mahoney if (ret && ret != -EAGAIN) { 94066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 941b86652beSJosef Bacik break; 94279787eaaSJeff Mahoney } 943b86652beSJosef Bacik ret = 0; 944459931ecSChris Mason 945459931ecSChris Mason key.offset = end_byte - 1; 946459931ecSChris Mason } else { 9472ff7e61eSJeff Mahoney truncate_one_csum(fs_info, path, &key, bytenr, len); 948dcbdd4dcSChris Mason if (key.offset < bytenr) 949dcbdd4dcSChris Mason break; 950459931ecSChris Mason } 951b3b4aa74SDavid Sterba btrfs_release_path(path); 952459931ecSChris Mason } 953459931ecSChris Mason btrfs_free_path(path); 95465a246c5STsutomu Itoh return ret; 955459931ecSChris Mason } 956459931ecSChris Mason 957ea7036deSFilipe Manana static int find_next_csum_offset(struct btrfs_root *root, 958ea7036deSFilipe Manana struct btrfs_path *path, 959ea7036deSFilipe Manana u64 *next_offset) 960ea7036deSFilipe Manana { 961ea7036deSFilipe Manana const u32 nritems = btrfs_header_nritems(path->nodes[0]); 962ea7036deSFilipe Manana struct btrfs_key found_key; 963ea7036deSFilipe Manana int slot = path->slots[0] + 1; 964ea7036deSFilipe Manana int ret; 965ea7036deSFilipe Manana 966ea7036deSFilipe Manana if (nritems == 0 || slot >= nritems) { 967ea7036deSFilipe Manana ret = btrfs_next_leaf(root, path); 968ea7036deSFilipe Manana if (ret < 0) { 969ea7036deSFilipe Manana return ret; 970ea7036deSFilipe Manana } else if (ret > 0) { 971ea7036deSFilipe Manana *next_offset = (u64)-1; 972ea7036deSFilipe Manana return 0; 973ea7036deSFilipe Manana } 974ea7036deSFilipe Manana slot = path->slots[0]; 975ea7036deSFilipe Manana } 976ea7036deSFilipe Manana 977ea7036deSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); 978ea7036deSFilipe Manana 979ea7036deSFilipe Manana if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 980ea7036deSFilipe Manana found_key.type != BTRFS_EXTENT_CSUM_KEY) 981ea7036deSFilipe Manana *next_offset = (u64)-1; 982ea7036deSFilipe Manana else 983ea7036deSFilipe Manana *next_offset = found_key.offset; 984ea7036deSFilipe Manana 985ea7036deSFilipe Manana return 0; 986ea7036deSFilipe Manana } 987ea7036deSFilipe Manana 988065631f6SChris Mason int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, 989d20f7043SChris Mason struct btrfs_root *root, 990e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums) 991f254e52cSChris Mason { 9920b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 993f254e52cSChris Mason struct btrfs_key file_key; 9946567e837SChris Mason struct btrfs_key found_key; 9955caf2a00SChris Mason struct btrfs_path *path; 996f254e52cSChris Mason struct btrfs_csum_item *item; 997065631f6SChris Mason struct btrfs_csum_item *item_end; 998ff79f819SChris Mason struct extent_buffer *leaf = NULL; 999f51a4a18SMiao Xie u64 next_offset; 1000f51a4a18SMiao Xie u64 total_bytes = 0; 10016567e837SChris Mason u64 csum_offset; 1002f51a4a18SMiao Xie u64 bytenr; 1003f578d4bdSChris Mason u32 ins_size; 1004f51a4a18SMiao Xie int index = 0; 1005f51a4a18SMiao Xie int found_next; 1006f51a4a18SMiao Xie int ret; 1007223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 10086e92f5e6SChris Mason 10095caf2a00SChris Mason path = btrfs_alloc_path(); 1010d8926bb3SMark Fasheh if (!path) 1011d8926bb3SMark Fasheh return -ENOMEM; 1012065631f6SChris Mason again: 1013065631f6SChris Mason next_offset = (u64)-1; 1014065631f6SChris Mason found_next = 0; 1015f51a4a18SMiao Xie bytenr = sums->bytenr + total_bytes; 1016d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 1017f51a4a18SMiao Xie file_key.offset = bytenr; 1018962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_CSUM_KEY; 1019a429e513SChris Mason 1020f51a4a18SMiao Xie item = btrfs_lookup_csum(trans, root, path, bytenr, 1); 1021ff79f819SChris Mason if (!IS_ERR(item)) { 1022639cb586SChris Mason ret = 0; 1023f51a4a18SMiao Xie leaf = path->nodes[0]; 1024f51a4a18SMiao Xie item_end = btrfs_item_ptr(leaf, path->slots[0], 1025f51a4a18SMiao Xie struct btrfs_csum_item); 1026f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((char *)item_end + 10273212fa14SJosef Bacik btrfs_item_size(leaf, path->slots[0])); 1028a429e513SChris Mason goto found; 1029ff79f819SChris Mason } 1030a429e513SChris Mason ret = PTR_ERR(item); 10314a500fd1SYan, Zheng if (ret != -EFBIG && ret != -ENOENT) 1032918cdf44SFilipe Manana goto out; 10334a500fd1SYan, Zheng 1034a429e513SChris Mason if (ret == -EFBIG) { 1035a429e513SChris Mason u32 item_size; 1036a429e513SChris Mason /* we found one, but it isn't big enough yet */ 10375f39d397SChris Mason leaf = path->nodes[0]; 10383212fa14SJosef Bacik item_size = btrfs_item_size(leaf, path->slots[0]); 1039607d432dSJosef Bacik if ((item_size / csum_size) >= 10400b246afaSJeff Mahoney MAX_CSUM_ITEMS(fs_info, csum_size)) { 1041a429e513SChris Mason /* already at max size, make a new one */ 1042a429e513SChris Mason goto insert; 1043a429e513SChris Mason } 1044a429e513SChris Mason } else { 1045ea7036deSFilipe Manana /* We didn't find a csum item, insert one. */ 1046ea7036deSFilipe Manana ret = find_next_csum_offset(root, path, &next_offset); 1047ea7036deSFilipe Manana if (ret < 0) 10487e4a3f7eSFilipe Manana goto out; 1049f578d4bdSChris Mason found_next = 1; 1050a429e513SChris Mason goto insert; 1051a429e513SChris Mason } 1052a429e513SChris Mason 1053a429e513SChris Mason /* 1054cc14600cSFilipe Manana * At this point, we know the tree has a checksum item that ends at an 1055cc14600cSFilipe Manana * offset matching the start of the checksum range we want to insert. 1056cc14600cSFilipe Manana * We try to extend that item as much as possible and then add as many 1057cc14600cSFilipe Manana * checksums to it as they fit. 1058cc14600cSFilipe Manana * 1059cc14600cSFilipe Manana * First check if the leaf has enough free space for at least one 1060cc14600cSFilipe Manana * checksum. If it has go directly to the item extension code, otherwise 1061cc14600cSFilipe Manana * release the path and do a search for insertion before the extension. 1062a429e513SChris Mason */ 1063cc14600cSFilipe Manana if (btrfs_leaf_free_space(leaf) >= csum_size) { 1064cc14600cSFilipe Manana btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 1065cc14600cSFilipe Manana csum_offset = (bytenr - found_key.offset) >> 1066265fdfa6SDavid Sterba fs_info->sectorsize_bits; 1067cc14600cSFilipe Manana goto extend_csum; 1068cc14600cSFilipe Manana } 1069cc14600cSFilipe Manana 1070b3b4aa74SDavid Sterba btrfs_release_path(path); 10719a664971Sethanwu path->search_for_extension = 1; 10726567e837SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 1073607d432dSJosef Bacik csum_size, 1); 10749a664971Sethanwu path->search_for_extension = 0; 10756567e837SChris Mason if (ret < 0) 1076918cdf44SFilipe Manana goto out; 1077459931ecSChris Mason 1078459931ecSChris Mason if (ret > 0) { 1079459931ecSChris Mason if (path->slots[0] == 0) 10806567e837SChris Mason goto insert; 10816567e837SChris Mason path->slots[0]--; 1082459931ecSChris Mason } 1083459931ecSChris Mason 10845f39d397SChris Mason leaf = path->nodes[0]; 10855f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 1086265fdfa6SDavid Sterba csum_offset = (bytenr - found_key.offset) >> fs_info->sectorsize_bits; 1087459931ecSChris Mason 1088962a298fSDavid Sterba if (found_key.type != BTRFS_EXTENT_CSUM_KEY || 1089d20f7043SChris Mason found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 10900b246afaSJeff Mahoney csum_offset >= MAX_CSUM_ITEMS(fs_info, csum_size)) { 10916567e837SChris Mason goto insert; 10926567e837SChris Mason } 1093459931ecSChris Mason 1094cc14600cSFilipe Manana extend_csum: 10953212fa14SJosef Bacik if (csum_offset == btrfs_item_size(leaf, path->slots[0]) / 1096607d432dSJosef Bacik csum_size) { 10972f697dc6SLiu Bo int extend_nr; 10982f697dc6SLiu Bo u64 tmp; 10992f697dc6SLiu Bo u32 diff; 1100459931ecSChris Mason 1101f51a4a18SMiao Xie tmp = sums->len - total_bytes; 1102265fdfa6SDavid Sterba tmp >>= fs_info->sectorsize_bits; 11032f697dc6SLiu Bo WARN_ON(tmp < 1); 1104ea7036deSFilipe Manana extend_nr = max_t(int, 1, tmp); 11052f697dc6SLiu Bo 1106ea7036deSFilipe Manana /* 1107ea7036deSFilipe Manana * A log tree can already have checksum items with a subset of 1108ea7036deSFilipe Manana * the checksums we are trying to log. This can happen after 1109ea7036deSFilipe Manana * doing a sequence of partial writes into prealloc extents and 1110ea7036deSFilipe Manana * fsyncs in between, with a full fsync logging a larger subrange 1111ea7036deSFilipe Manana * of an extent for which a previous fast fsync logged a smaller 1112ea7036deSFilipe Manana * subrange. And this happens in particular due to merging file 1113ea7036deSFilipe Manana * extent items when we complete an ordered extent for a range 1114ea7036deSFilipe Manana * covered by a prealloc extent - this is done at 1115ea7036deSFilipe Manana * btrfs_mark_extent_written(). 1116ea7036deSFilipe Manana * 1117ea7036deSFilipe Manana * So if we try to extend the previous checksum item, which has 1118ea7036deSFilipe Manana * a range that ends at the start of the range we want to insert, 1119ea7036deSFilipe Manana * make sure we don't extend beyond the start offset of the next 1120ea7036deSFilipe Manana * checksum item. If we are at the last item in the leaf, then 1121ea7036deSFilipe Manana * forget the optimization of extending and add a new checksum 1122ea7036deSFilipe Manana * item - it is not worth the complexity of releasing the path, 1123ea7036deSFilipe Manana * getting the first key for the next leaf, repeat the btree 1124ea7036deSFilipe Manana * search, etc, because log trees are temporary anyway and it 1125ea7036deSFilipe Manana * would only save a few bytes of leaf space. 1126ea7036deSFilipe Manana */ 1127ea7036deSFilipe Manana if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { 1128ea7036deSFilipe Manana if (path->slots[0] + 1 >= 1129ea7036deSFilipe Manana btrfs_header_nritems(path->nodes[0])) { 1130ea7036deSFilipe Manana ret = find_next_csum_offset(root, path, &next_offset); 1131ea7036deSFilipe Manana if (ret < 0) 1132ea7036deSFilipe Manana goto out; 1133ea7036deSFilipe Manana found_next = 1; 1134ea7036deSFilipe Manana goto insert; 1135ea7036deSFilipe Manana } 1136ea7036deSFilipe Manana 1137ea7036deSFilipe Manana ret = find_next_csum_offset(root, path, &next_offset); 1138ea7036deSFilipe Manana if (ret < 0) 1139ea7036deSFilipe Manana goto out; 1140ea7036deSFilipe Manana 1141ea7036deSFilipe Manana tmp = (next_offset - bytenr) >> fs_info->sectorsize_bits; 1142ea7036deSFilipe Manana if (tmp <= INT_MAX) 1143ea7036deSFilipe Manana extend_nr = min_t(int, extend_nr, tmp); 1144ea7036deSFilipe Manana } 1145ea7036deSFilipe Manana 11462f697dc6SLiu Bo diff = (csum_offset + extend_nr) * csum_size; 11470b246afaSJeff Mahoney diff = min(diff, 11480b246afaSJeff Mahoney MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size); 1149459931ecSChris Mason 11503212fa14SJosef Bacik diff = diff - btrfs_item_size(leaf, path->slots[0]); 1151cc14600cSFilipe Manana diff = min_t(u32, btrfs_leaf_free_space(leaf), diff); 11522f697dc6SLiu Bo diff /= csum_size; 11532f697dc6SLiu Bo diff *= csum_size; 1154459931ecSChris Mason 1155c71dd880SDavid Sterba btrfs_extend_item(path, diff); 1156f51a4a18SMiao Xie ret = 0; 11576567e837SChris Mason goto csum; 11586567e837SChris Mason } 11596567e837SChris Mason 11606567e837SChris Mason insert: 1161b3b4aa74SDavid Sterba btrfs_release_path(path); 11626567e837SChris Mason csum_offset = 0; 1163f578d4bdSChris Mason if (found_next) { 11642f697dc6SLiu Bo u64 tmp; 1165d20f7043SChris Mason 1166f51a4a18SMiao Xie tmp = sums->len - total_bytes; 1167265fdfa6SDavid Sterba tmp >>= fs_info->sectorsize_bits; 11682f697dc6SLiu Bo tmp = min(tmp, (next_offset - file_key.offset) >> 1169265fdfa6SDavid Sterba fs_info->sectorsize_bits); 11702f697dc6SLiu Bo 117150d0446eSSeraphime Kirkovski tmp = max_t(u64, 1, tmp); 117250d0446eSSeraphime Kirkovski tmp = min_t(u64, tmp, MAX_CSUM_ITEMS(fs_info, csum_size)); 1173607d432dSJosef Bacik ins_size = csum_size * tmp; 1174f578d4bdSChris Mason } else { 1175607d432dSJosef Bacik ins_size = csum_size; 1176f578d4bdSChris Mason } 11775caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 1178f578d4bdSChris Mason ins_size); 117954aa1f4dSChris Mason if (ret < 0) 1180918cdf44SFilipe Manana goto out; 1181fae7f21cSDulshani Gunawardhana if (WARN_ON(ret != 0)) 1182918cdf44SFilipe Manana goto out; 11835f39d397SChris Mason leaf = path->nodes[0]; 1184f51a4a18SMiao Xie csum: 11855f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 1186f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((unsigned char *)item + 11873212fa14SJosef Bacik btrfs_item_size(leaf, path->slots[0])); 1188509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 1189607d432dSJosef Bacik csum_offset * csum_size); 1190b18c6685SChris Mason found: 1191265fdfa6SDavid Sterba ins_size = (u32)(sums->len - total_bytes) >> fs_info->sectorsize_bits; 1192f51a4a18SMiao Xie ins_size *= csum_size; 1193f51a4a18SMiao Xie ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item, 1194f51a4a18SMiao Xie ins_size); 1195f51a4a18SMiao Xie write_extent_buffer(leaf, sums->sums + index, (unsigned long)item, 1196f51a4a18SMiao Xie ins_size); 1197aadfeb6eSChris Mason 11981e25a2e3SJohannes Thumshirn index += ins_size; 1199f51a4a18SMiao Xie ins_size /= csum_size; 12000b246afaSJeff Mahoney total_bytes += ins_size * fs_info->sectorsize; 1201a6591715SChris Mason 12025caf2a00SChris Mason btrfs_mark_buffer_dirty(path->nodes[0]); 1203e6dcd2dcSChris Mason if (total_bytes < sums->len) { 1204b3b4aa74SDavid Sterba btrfs_release_path(path); 1205b9473439SChris Mason cond_resched(); 1206065631f6SChris Mason goto again; 1207065631f6SChris Mason } 120853863232SChris Mason out: 12095caf2a00SChris Mason btrfs_free_path(path); 1210f254e52cSChris Mason return ret; 1211f254e52cSChris Mason } 12127ffbb598SFilipe Manana 12139cdc5124SNikolay Borisov void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, 12147ffbb598SFilipe Manana const struct btrfs_path *path, 12157ffbb598SFilipe Manana struct btrfs_file_extent_item *fi, 12167ffbb598SFilipe Manana const bool new_inline, 12177ffbb598SFilipe Manana struct extent_map *em) 12187ffbb598SFilipe Manana { 12193ffbd68cSDavid Sterba struct btrfs_fs_info *fs_info = inode->root->fs_info; 12209cdc5124SNikolay Borisov struct btrfs_root *root = inode->root; 12217ffbb598SFilipe Manana struct extent_buffer *leaf = path->nodes[0]; 12227ffbb598SFilipe Manana const int slot = path->slots[0]; 12237ffbb598SFilipe Manana struct btrfs_key key; 12247ffbb598SFilipe Manana u64 extent_start, extent_end; 12257ffbb598SFilipe Manana u64 bytenr; 12267ffbb598SFilipe Manana u8 type = btrfs_file_extent_type(leaf, fi); 12277ffbb598SFilipe Manana int compress_type = btrfs_file_extent_compression(leaf, fi); 12287ffbb598SFilipe Manana 12297ffbb598SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot); 12307ffbb598SFilipe Manana extent_start = key.offset; 1231a5eeb3d1SFilipe Manana extent_end = btrfs_file_extent_end(path); 12327ffbb598SFilipe Manana em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); 123340e7efe0SQu Wenruo em->generation = btrfs_file_extent_generation(leaf, fi); 12347ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_REG || 12357ffbb598SFilipe Manana type == BTRFS_FILE_EXTENT_PREALLOC) { 12367ffbb598SFilipe Manana em->start = extent_start; 12377ffbb598SFilipe Manana em->len = extent_end - extent_start; 12387ffbb598SFilipe Manana em->orig_start = extent_start - 12397ffbb598SFilipe Manana btrfs_file_extent_offset(leaf, fi); 12407ffbb598SFilipe Manana em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); 12417ffbb598SFilipe Manana bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); 12427ffbb598SFilipe Manana if (bytenr == 0) { 12437ffbb598SFilipe Manana em->block_start = EXTENT_MAP_HOLE; 12447ffbb598SFilipe Manana return; 12457ffbb598SFilipe Manana } 12467ffbb598SFilipe Manana if (compress_type != BTRFS_COMPRESS_NONE) { 12477ffbb598SFilipe Manana set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 12487ffbb598SFilipe Manana em->compress_type = compress_type; 12497ffbb598SFilipe Manana em->block_start = bytenr; 12507ffbb598SFilipe Manana em->block_len = em->orig_block_len; 12517ffbb598SFilipe Manana } else { 12527ffbb598SFilipe Manana bytenr += btrfs_file_extent_offset(leaf, fi); 12537ffbb598SFilipe Manana em->block_start = bytenr; 12547ffbb598SFilipe Manana em->block_len = em->len; 12557ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_PREALLOC) 12567ffbb598SFilipe Manana set_bit(EXTENT_FLAG_PREALLOC, &em->flags); 12577ffbb598SFilipe Manana } 12587ffbb598SFilipe Manana } else if (type == BTRFS_FILE_EXTENT_INLINE) { 12597ffbb598SFilipe Manana em->block_start = EXTENT_MAP_INLINE; 12607ffbb598SFilipe Manana em->start = extent_start; 12617ffbb598SFilipe Manana em->len = extent_end - extent_start; 12627ffbb598SFilipe Manana /* 12637ffbb598SFilipe Manana * Initialize orig_start and block_len with the same values 12647ffbb598SFilipe Manana * as in inode.c:btrfs_get_extent(). 12657ffbb598SFilipe Manana */ 12667ffbb598SFilipe Manana em->orig_start = EXTENT_MAP_HOLE; 12677ffbb598SFilipe Manana em->block_len = (u64)-1; 12687ffbb598SFilipe Manana if (!new_inline && compress_type != BTRFS_COMPRESS_NONE) { 12697ffbb598SFilipe Manana set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 12707ffbb598SFilipe Manana em->compress_type = compress_type; 12717ffbb598SFilipe Manana } 12727ffbb598SFilipe Manana } else { 12730b246afaSJeff Mahoney btrfs_err(fs_info, 12749cdc5124SNikolay Borisov "unknown file extent item type %d, inode %llu, offset %llu, " 12759cdc5124SNikolay Borisov "root %llu", type, btrfs_ino(inode), extent_start, 12767ffbb598SFilipe Manana root->root_key.objectid); 12777ffbb598SFilipe Manana } 12787ffbb598SFilipe Manana } 1279a5eeb3d1SFilipe Manana 1280a5eeb3d1SFilipe Manana /* 1281a5eeb3d1SFilipe Manana * Returns the end offset (non inclusive) of the file extent item the given path 1282a5eeb3d1SFilipe Manana * points to. If it points to an inline extent, the returned offset is rounded 1283a5eeb3d1SFilipe Manana * up to the sector size. 1284a5eeb3d1SFilipe Manana */ 1285a5eeb3d1SFilipe Manana u64 btrfs_file_extent_end(const struct btrfs_path *path) 1286a5eeb3d1SFilipe Manana { 1287a5eeb3d1SFilipe Manana const struct extent_buffer *leaf = path->nodes[0]; 1288a5eeb3d1SFilipe Manana const int slot = path->slots[0]; 1289a5eeb3d1SFilipe Manana struct btrfs_file_extent_item *fi; 1290a5eeb3d1SFilipe Manana struct btrfs_key key; 1291a5eeb3d1SFilipe Manana u64 end; 1292a5eeb3d1SFilipe Manana 1293a5eeb3d1SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot); 1294a5eeb3d1SFilipe Manana ASSERT(key.type == BTRFS_EXTENT_DATA_KEY); 1295a5eeb3d1SFilipe Manana fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); 1296a5eeb3d1SFilipe Manana 1297a5eeb3d1SFilipe Manana if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) { 1298a5eeb3d1SFilipe Manana end = btrfs_file_extent_ram_bytes(leaf, fi); 1299a5eeb3d1SFilipe Manana end = ALIGN(key.offset + end, leaf->fs_info->sectorsize); 1300a5eeb3d1SFilipe Manana } else { 1301a5eeb3d1SFilipe Manana end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); 1302a5eeb3d1SFilipe Manana } 1303a5eeb3d1SFilipe Manana 1304a5eeb3d1SFilipe Manana return end; 1305a5eeb3d1SFilipe Manana } 1306