1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 26cbd5570SChris Mason /* 36cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 46cbd5570SChris Mason */ 56cbd5570SChris Mason 6065631f6SChris Mason #include <linux/bio.h> 75a0e3ad6STejun Heo #include <linux/slab.h> 8065631f6SChris Mason #include <linux/pagemap.h> 9065631f6SChris Mason #include <linux/highmem.h> 10a3d46aeaSNikolay Borisov #include <linux/sched/mm.h> 11d5178578SJohannes Thumshirn #include <crypto/hash.h> 121e1d2701SChris Mason #include "ctree.h" 13dee26a9fSChris Mason #include "disk-io.h" 149f5fae2fSChris Mason #include "transaction.h" 15facc8a22SMiao Xie #include "volumes.h" 161de037a4SChris Mason #include "print-tree.h" 17ebb8765bSAnand Jain #include "compression.h" 181e1d2701SChris Mason 1942049bf6SChris Mason #define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \ 2042049bf6SChris Mason sizeof(struct btrfs_item) * 2) / \ 2142049bf6SChris Mason size) - 1)) 2207d400a6SYan Zheng 23221b8318SZach Brown #define MAX_CSUM_ITEMS(r, size) (min_t(u32, __MAX_CSUM_ITEMS(r, size), \ 2409cbfeafSKirill A. Shutemov PAGE_SIZE)) 257ca4be45SChris Mason 2641a2ee75SJosef Bacik /** 2741a2ee75SJosef Bacik * @inode - the inode we want to update the disk_i_size for 2841a2ee75SJosef Bacik * @new_i_size - the i_size we want to set to, 0 if we use i_size 2941a2ee75SJosef Bacik * 3041a2ee75SJosef Bacik * With NO_HOLES set this simply sets the disk_is_size to whatever i_size_read() 3141a2ee75SJosef Bacik * returns as it is perfectly fine with a file that has holes without hole file 3241a2ee75SJosef Bacik * extent items. 3341a2ee75SJosef Bacik * 3441a2ee75SJosef Bacik * However without NO_HOLES we need to only return the area that is contiguous 3541a2ee75SJosef Bacik * from the 0 offset of the file. Otherwise we could end up adjust i_size up 3641a2ee75SJosef Bacik * to an extent that has a gap in between. 3741a2ee75SJosef Bacik * 3841a2ee75SJosef Bacik * Finally new_i_size should only be set in the case of truncate where we're not 3941a2ee75SJosef Bacik * ready to use i_size_read() as the limiter yet. 4041a2ee75SJosef Bacik */ 4176aea537SNikolay Borisov void btrfs_inode_safe_disk_i_size_write(struct btrfs_inode *inode, u64 new_i_size) 4241a2ee75SJosef Bacik { 4376aea537SNikolay Borisov struct btrfs_fs_info *fs_info = inode->root->fs_info; 4441a2ee75SJosef Bacik u64 start, end, i_size; 4541a2ee75SJosef Bacik int ret; 4641a2ee75SJosef Bacik 4776aea537SNikolay Borisov i_size = new_i_size ?: i_size_read(&inode->vfs_inode); 4841a2ee75SJosef Bacik if (btrfs_fs_incompat(fs_info, NO_HOLES)) { 4976aea537SNikolay Borisov inode->disk_i_size = i_size; 5041a2ee75SJosef Bacik return; 5141a2ee75SJosef Bacik } 5241a2ee75SJosef Bacik 5376aea537SNikolay Borisov spin_lock(&inode->lock); 5476aea537SNikolay Borisov ret = find_contiguous_extent_bit(&inode->file_extent_tree, 0, &start, 5576aea537SNikolay Borisov &end, EXTENT_DIRTY); 5641a2ee75SJosef Bacik if (!ret && start == 0) 5741a2ee75SJosef Bacik i_size = min(i_size, end + 1); 5841a2ee75SJosef Bacik else 5941a2ee75SJosef Bacik i_size = 0; 6076aea537SNikolay Borisov inode->disk_i_size = i_size; 6176aea537SNikolay Borisov spin_unlock(&inode->lock); 6241a2ee75SJosef Bacik } 6341a2ee75SJosef Bacik 6441a2ee75SJosef Bacik /** 6541a2ee75SJosef Bacik * @inode - the inode we're modifying 6641a2ee75SJosef Bacik * @start - the start file offset of the file extent we've inserted 6741a2ee75SJosef Bacik * @len - the logical length of the file extent item 6841a2ee75SJosef Bacik * 6941a2ee75SJosef Bacik * Call when we are inserting a new file extent where there was none before. 7041a2ee75SJosef Bacik * Does not need to call this in the case where we're replacing an existing file 7141a2ee75SJosef Bacik * extent, however if not sure it's fine to call this multiple times. 7241a2ee75SJosef Bacik * 7341a2ee75SJosef Bacik * The start and len must match the file extent item, so thus must be sectorsize 7441a2ee75SJosef Bacik * aligned. 7541a2ee75SJosef Bacik */ 7641a2ee75SJosef Bacik int btrfs_inode_set_file_extent_range(struct btrfs_inode *inode, u64 start, 7741a2ee75SJosef Bacik u64 len) 7841a2ee75SJosef Bacik { 7941a2ee75SJosef Bacik if (len == 0) 8041a2ee75SJosef Bacik return 0; 8141a2ee75SJosef Bacik 8241a2ee75SJosef Bacik ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize)); 8341a2ee75SJosef Bacik 8441a2ee75SJosef Bacik if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES)) 8541a2ee75SJosef Bacik return 0; 8641a2ee75SJosef Bacik return set_extent_bits(&inode->file_extent_tree, start, start + len - 1, 8741a2ee75SJosef Bacik EXTENT_DIRTY); 8841a2ee75SJosef Bacik } 8941a2ee75SJosef Bacik 9041a2ee75SJosef Bacik /** 9141a2ee75SJosef Bacik * @inode - the inode we're modifying 9241a2ee75SJosef Bacik * @start - the start file offset of the file extent we've inserted 9341a2ee75SJosef Bacik * @len - the logical length of the file extent item 9441a2ee75SJosef Bacik * 9541a2ee75SJosef Bacik * Called when we drop a file extent, for example when we truncate. Doesn't 9641a2ee75SJosef Bacik * need to be called for cases where we're replacing a file extent, like when 9741a2ee75SJosef Bacik * we've COWed a file extent. 9841a2ee75SJosef Bacik * 9941a2ee75SJosef Bacik * The start and len must match the file extent item, so thus must be sectorsize 10041a2ee75SJosef Bacik * aligned. 10141a2ee75SJosef Bacik */ 10241a2ee75SJosef Bacik int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start, 10341a2ee75SJosef Bacik u64 len) 10441a2ee75SJosef Bacik { 10541a2ee75SJosef Bacik if (len == 0) 10641a2ee75SJosef Bacik return 0; 10741a2ee75SJosef Bacik 10841a2ee75SJosef Bacik ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize) || 10941a2ee75SJosef Bacik len == (u64)-1); 11041a2ee75SJosef Bacik 11141a2ee75SJosef Bacik if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES)) 11241a2ee75SJosef Bacik return 0; 11341a2ee75SJosef Bacik return clear_extent_bit(&inode->file_extent_tree, start, 11441a2ee75SJosef Bacik start + len - 1, EXTENT_DIRTY, 0, 0, NULL); 11541a2ee75SJosef Bacik } 11641a2ee75SJosef Bacik 1171e25a2e3SJohannes Thumshirn static inline u32 max_ordered_sum_bytes(struct btrfs_fs_info *fs_info, 1181e25a2e3SJohannes Thumshirn u16 csum_size) 1191e25a2e3SJohannes Thumshirn { 1201e25a2e3SJohannes Thumshirn u32 ncsums = (PAGE_SIZE - sizeof(struct btrfs_ordered_sum)) / csum_size; 1211e25a2e3SJohannes Thumshirn 1221e25a2e3SJohannes Thumshirn return ncsums * fs_info->sectorsize; 1231e25a2e3SJohannes Thumshirn } 12407d400a6SYan Zheng 125b18c6685SChris Mason int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, 126dee26a9fSChris Mason struct btrfs_root *root, 127b18c6685SChris Mason u64 objectid, u64 pos, 128f2eb0a24SSage Weil u64 disk_offset, u64 disk_num_bytes, 129c8b97818SChris Mason u64 num_bytes, u64 offset, u64 ram_bytes, 130c8b97818SChris Mason u8 compression, u8 encryption, u16 other_encoding) 1319f5fae2fSChris Mason { 132dee26a9fSChris Mason int ret = 0; 133dee26a9fSChris Mason struct btrfs_file_extent_item *item; 134dee26a9fSChris Mason struct btrfs_key file_key; 1355caf2a00SChris Mason struct btrfs_path *path; 1365f39d397SChris Mason struct extent_buffer *leaf; 137dee26a9fSChris Mason 1385caf2a00SChris Mason path = btrfs_alloc_path(); 139db5b493aSTsutomu Itoh if (!path) 140db5b493aSTsutomu Itoh return -ENOMEM; 141dee26a9fSChris Mason file_key.objectid = objectid; 142b18c6685SChris Mason file_key.offset = pos; 143962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_DATA_KEY; 144dee26a9fSChris Mason 1455caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 146dee26a9fSChris Mason sizeof(*item)); 14754aa1f4dSChris Mason if (ret < 0) 14854aa1f4dSChris Mason goto out; 14979787eaaSJeff Mahoney BUG_ON(ret); /* Can't happen */ 1505f39d397SChris Mason leaf = path->nodes[0]; 1515f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], 152dee26a9fSChris Mason struct btrfs_file_extent_item); 153f2eb0a24SSage Weil btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset); 154db94535dSChris Mason btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes); 155f2eb0a24SSage Weil btrfs_set_file_extent_offset(leaf, item, offset); 156db94535dSChris Mason btrfs_set_file_extent_num_bytes(leaf, item, num_bytes); 157c8b97818SChris Mason btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes); 1585f39d397SChris Mason btrfs_set_file_extent_generation(leaf, item, trans->transid); 1595f39d397SChris Mason btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); 160c8b97818SChris Mason btrfs_set_file_extent_compression(leaf, item, compression); 161c8b97818SChris Mason btrfs_set_file_extent_encryption(leaf, item, encryption); 162c8b97818SChris Mason btrfs_set_file_extent_other_encoding(leaf, item, other_encoding); 163c8b97818SChris Mason 1645f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 16554aa1f4dSChris Mason out: 1665caf2a00SChris Mason btrfs_free_path(path); 16754aa1f4dSChris Mason return ret; 1689f5fae2fSChris Mason } 169dee26a9fSChris Mason 17048a3b636SEric Sandeen static struct btrfs_csum_item * 17148a3b636SEric Sandeen btrfs_lookup_csum(struct btrfs_trans_handle *trans, 172b18c6685SChris Mason struct btrfs_root *root, 1736567e837SChris Mason struct btrfs_path *path, 174d20f7043SChris Mason u64 bytenr, int cow) 1756567e837SChris Mason { 1760b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1776567e837SChris Mason int ret; 1786567e837SChris Mason struct btrfs_key file_key; 1796567e837SChris Mason struct btrfs_key found_key; 1806567e837SChris Mason struct btrfs_csum_item *item; 1815f39d397SChris Mason struct extent_buffer *leaf; 1826567e837SChris Mason u64 csum_offset = 0; 183223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 184a429e513SChris Mason int csums_in_item; 1856567e837SChris Mason 186d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 187d20f7043SChris Mason file_key.offset = bytenr; 188962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_CSUM_KEY; 189b18c6685SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); 1906567e837SChris Mason if (ret < 0) 1916567e837SChris Mason goto fail; 1925f39d397SChris Mason leaf = path->nodes[0]; 1936567e837SChris Mason if (ret > 0) { 1946567e837SChris Mason ret = 1; 19570b2befdSChris Mason if (path->slots[0] == 0) 1966567e837SChris Mason goto fail; 1976567e837SChris Mason path->slots[0]--; 1985f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 199962a298fSDavid Sterba if (found_key.type != BTRFS_EXTENT_CSUM_KEY) 2006567e837SChris Mason goto fail; 201d20f7043SChris Mason 202d20f7043SChris Mason csum_offset = (bytenr - found_key.offset) >> 203265fdfa6SDavid Sterba fs_info->sectorsize_bits; 2045f39d397SChris Mason csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]); 205607d432dSJosef Bacik csums_in_item /= csum_size; 206a429e513SChris Mason 20782d130ffSMiao Xie if (csum_offset == csums_in_item) { 208a429e513SChris Mason ret = -EFBIG; 2096567e837SChris Mason goto fail; 21082d130ffSMiao Xie } else if (csum_offset > csums_in_item) { 21182d130ffSMiao Xie goto fail; 2126567e837SChris Mason } 2136567e837SChris Mason } 2146567e837SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 215509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 216607d432dSJosef Bacik csum_offset * csum_size); 2176567e837SChris Mason return item; 2186567e837SChris Mason fail: 2196567e837SChris Mason if (ret > 0) 220b18c6685SChris Mason ret = -ENOENT; 2216567e837SChris Mason return ERR_PTR(ret); 2226567e837SChris Mason } 2236567e837SChris Mason 224dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, 225dee26a9fSChris Mason struct btrfs_root *root, 226dee26a9fSChris Mason struct btrfs_path *path, u64 objectid, 2279773a788SChris Mason u64 offset, int mod) 228dee26a9fSChris Mason { 229dee26a9fSChris Mason int ret; 230dee26a9fSChris Mason struct btrfs_key file_key; 231dee26a9fSChris Mason int ins_len = mod < 0 ? -1 : 0; 232dee26a9fSChris Mason int cow = mod != 0; 233dee26a9fSChris Mason 234dee26a9fSChris Mason file_key.objectid = objectid; 23570b2befdSChris Mason file_key.offset = offset; 236962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_DATA_KEY; 237dee26a9fSChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); 238dee26a9fSChris Mason return ret; 239dee26a9fSChris Mason } 240f254e52cSChris Mason 241e62958fcSOmar Sandoval /** 242*9e46458aSQu Wenruo * btrfs_lookup_bio_sums - Look up checksums for a read bio. 243*9e46458aSQu Wenruo * 244e62958fcSOmar Sandoval * @inode: inode that the bio is for. 245fb30f470SOmar Sandoval * @bio: bio to look up. 246db72e47fSOmar Sandoval * @offset: Unless (u64)-1, look up checksums for this offset in the file. 247db72e47fSOmar Sandoval * If (u64)-1, use the page offsets from the bio instead. 248fb30f470SOmar Sandoval * @dst: Buffer of size nblocks * btrfs_super_csum_size() used to return 249fb30f470SOmar Sandoval * checksum (nblocks = bio->bi_iter.bi_size / fs_info->sectorsize). If 250fb30f470SOmar Sandoval * NULL, the checksum buffer is allocated and returned in 251fb30f470SOmar Sandoval * btrfs_io_bio(bio)->csum instead. 252e62958fcSOmar Sandoval * 253e62958fcSOmar Sandoval * Return: BLK_STS_RESOURCE if allocating memory fails, BLK_STS_OK otherwise. 254e62958fcSOmar Sandoval */ 255e62958fcSOmar Sandoval blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, 256db72e47fSOmar Sandoval u64 offset, u8 *dst) 25761b49440SChris Mason { 2580b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 25917347cecSLiu Bo struct bio_vec bvec; 26017347cecSLiu Bo struct bvec_iter iter; 261facc8a22SMiao Xie struct btrfs_csum_item *item = NULL; 262facc8a22SMiao Xie struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 263facc8a22SMiao Xie struct btrfs_path *path; 264db72e47fSOmar Sandoval const bool page_offsets = (offset == (u64)-1); 265facc8a22SMiao Xie u8 *csum; 26661b49440SChris Mason u64 item_start_offset = 0; 26761b49440SChris Mason u64 item_last_offset = 0; 268d20f7043SChris Mason u64 disk_bytenr; 269c40a3d38SChandan Rajendra u64 page_bytes_left; 27061b49440SChris Mason u32 diff; 271facc8a22SMiao Xie int nblocks; 27217347cecSLiu Bo int count = 0; 273223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 27461b49440SChris Mason 27542437a63SJosef Bacik if (!fs_info->csum_root || (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) 276334c16d8SJosef Bacik return BLK_STS_OK; 277334c16d8SJosef Bacik 278*9e46458aSQu Wenruo /* 279*9e46458aSQu Wenruo * This function is only called for read bio. 280*9e46458aSQu Wenruo * 281*9e46458aSQu Wenruo * This means two things: 282*9e46458aSQu Wenruo * - All our csums should only be in csum tree 283*9e46458aSQu Wenruo * No ordered extents csums, as ordered extents are only for write 284*9e46458aSQu Wenruo * path. 285*9e46458aSQu Wenruo */ 286*9e46458aSQu Wenruo ASSERT(bio_op(bio) == REQ_OP_READ); 28761b49440SChris Mason path = btrfs_alloc_path(); 288c2db1073STsutomu Itoh if (!path) 2894e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 290facc8a22SMiao Xie 291265fdfa6SDavid Sterba nblocks = bio->bi_iter.bi_size >> fs_info->sectorsize_bits; 292facc8a22SMiao Xie if (!dst) { 293fb30f470SOmar Sandoval struct btrfs_io_bio *btrfs_bio = btrfs_io_bio(bio); 294fb30f470SOmar Sandoval 295facc8a22SMiao Xie if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) { 29631fecccbSDavid Sterba btrfs_bio->csum = kmalloc_array(nblocks, csum_size, 29731fecccbSDavid Sterba GFP_NOFS); 29831fecccbSDavid Sterba if (!btrfs_bio->csum) { 299facc8a22SMiao Xie btrfs_free_path(path); 3004e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 301facc8a22SMiao Xie } 302facc8a22SMiao Xie } else { 303facc8a22SMiao Xie btrfs_bio->csum = btrfs_bio->csum_inline; 304facc8a22SMiao Xie } 305facc8a22SMiao Xie csum = btrfs_bio->csum; 306facc8a22SMiao Xie } else { 30710fe6ca8SJohannes Thumshirn csum = dst; 308facc8a22SMiao Xie } 309facc8a22SMiao Xie 31035478d05SQu Wenruo /* 31135478d05SQu Wenruo * If requested number of sectors is larger than one leaf can contain, 31235478d05SQu Wenruo * kick the readahead for csum tree. 31335478d05SQu Wenruo */ 31435478d05SQu Wenruo if (nblocks > fs_info->csums_per_leaf) 315e4058b54SDavid Sterba path->reada = READA_FORWARD; 31661b49440SChris Mason 3172cf8572dSChris Mason /* 3182cf8572dSChris Mason * the free space stuff is only read when it hasn't been 3192cf8572dSChris Mason * updated in the current transaction. So, we can safely 3202cf8572dSChris Mason * read from the commit root and sidestep a nasty deadlock 3212cf8572dSChris Mason * between reading the free space cache and updating the csum tree. 3222cf8572dSChris Mason */ 32370ddc553SNikolay Borisov if (btrfs_is_free_space_inode(BTRFS_I(inode))) { 3242cf8572dSChris Mason path->search_commit_root = 1; 325ddf23b3fSJosef Bacik path->skip_locking = 1; 326ddf23b3fSJosef Bacik } 3272cf8572dSChris Mason 3281201b58bSDavid Sterba disk_bytenr = bio->bi_iter.bi_sector << 9; 329c40a3d38SChandan Rajendra 33017347cecSLiu Bo bio_for_each_segment(bvec, bio, iter) { 33117347cecSLiu Bo page_bytes_left = bvec.bv_len; 3324989d277SChristoph Hellwig if (count) 3334989d277SChristoph Hellwig goto next; 3344989d277SChristoph Hellwig 335db72e47fSOmar Sandoval if (page_offsets) 33617347cecSLiu Bo offset = page_offset(bvec.bv_page) + bvec.bv_offset; 33761b49440SChris Mason 338d20f7043SChris Mason if (!item || disk_bytenr < item_start_offset || 339d20f7043SChris Mason disk_bytenr >= item_last_offset) { 34061b49440SChris Mason struct btrfs_key found_key; 34161b49440SChris Mason u32 item_size; 34261b49440SChris Mason 34361b49440SChris Mason if (item) 344b3b4aa74SDavid Sterba btrfs_release_path(path); 3450b246afaSJeff Mahoney item = btrfs_lookup_csum(NULL, fs_info->csum_root, 346d20f7043SChris Mason path, disk_bytenr, 0); 34761b49440SChris Mason if (IS_ERR(item)) { 348e4100d98SMiao Xie count = 1; 349facc8a22SMiao Xie memset(csum, 0, csum_size); 35017d217feSYan Zheng if (BTRFS_I(inode)->root->root_key.objectid == 35117d217feSYan Zheng BTRFS_DATA_RELOC_TREE_OBJECTID) { 35217d217feSYan Zheng set_extent_bits(io_tree, offset, 3530b246afaSJeff Mahoney offset + fs_info->sectorsize - 1, 354ceeb0ae7SDavid Sterba EXTENT_NODATASUM); 35517d217feSYan Zheng } else { 3560b246afaSJeff Mahoney btrfs_info_rl(fs_info, 357efe120a0SFrank Holton "no csum found for inode %llu start %llu", 3584a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(inode)), offset); 35917d217feSYan Zheng } 3606dab8157SChris Mason item = NULL; 361b3b4aa74SDavid Sterba btrfs_release_path(path); 36261b49440SChris Mason goto found; 36361b49440SChris Mason } 36461b49440SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key, 36561b49440SChris Mason path->slots[0]); 36661b49440SChris Mason 36761b49440SChris Mason item_start_offset = found_key.offset; 36861b49440SChris Mason item_size = btrfs_item_size_nr(path->nodes[0], 36961b49440SChris Mason path->slots[0]); 37061b49440SChris Mason item_last_offset = item_start_offset + 371607d432dSJosef Bacik (item_size / csum_size) * 3720b246afaSJeff Mahoney fs_info->sectorsize; 37361b49440SChris Mason item = btrfs_item_ptr(path->nodes[0], path->slots[0], 37461b49440SChris Mason struct btrfs_csum_item); 37561b49440SChris Mason } 37661b49440SChris Mason /* 37761b49440SChris Mason * this byte range must be able to fit inside 37861b49440SChris Mason * a single leaf so it will also fit inside a u32 37961b49440SChris Mason */ 380d20f7043SChris Mason diff = disk_bytenr - item_start_offset; 381ab108d99SDavid Sterba diff = diff >> fs_info->sectorsize_bits; 382607d432dSJosef Bacik diff = diff * csum_size; 383facc8a22SMiao Xie count = min_t(int, nblocks, (item_last_offset - disk_bytenr) >> 384265fdfa6SDavid Sterba fs_info->sectorsize_bits); 385facc8a22SMiao Xie read_extent_buffer(path->nodes[0], csum, 3863de9d6b6SChris Mason ((unsigned long)item) + diff, 387e4100d98SMiao Xie csum_size * count); 38861b49440SChris Mason found: 389facc8a22SMiao Xie csum += count * csum_size; 390facc8a22SMiao Xie nblocks -= count; 3914989d277SChristoph Hellwig next: 3924babad10SDavid Sterba while (count > 0) { 3934babad10SDavid Sterba count--; 3940b246afaSJeff Mahoney disk_bytenr += fs_info->sectorsize; 3950b246afaSJeff Mahoney offset += fs_info->sectorsize; 3960b246afaSJeff Mahoney page_bytes_left -= fs_info->sectorsize; 3974989d277SChristoph Hellwig if (!page_bytes_left) 3984989d277SChristoph Hellwig break; /* move to next bio */ 3994989d277SChristoph Hellwig } 4004989d277SChristoph Hellwig } 4014989d277SChristoph Hellwig 402389f239cSChris Mason WARN_ON_ONCE(count); 40361b49440SChris Mason btrfs_free_path(path); 404e62958fcSOmar Sandoval return BLK_STS_OK; 4054b46fce2SJosef Bacik } 4064b46fce2SJosef Bacik 40717d217feSYan Zheng int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, 408a2de733cSArne Jansen struct list_head *list, int search_commit) 40917d217feSYan Zheng { 4100b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 41117d217feSYan Zheng struct btrfs_key key; 41217d217feSYan Zheng struct btrfs_path *path; 41317d217feSYan Zheng struct extent_buffer *leaf; 41417d217feSYan Zheng struct btrfs_ordered_sum *sums; 41517d217feSYan Zheng struct btrfs_csum_item *item; 4160678b618SMark Fasheh LIST_HEAD(tmplist); 41717d217feSYan Zheng unsigned long offset; 41817d217feSYan Zheng int ret; 41917d217feSYan Zheng size_t size; 42017d217feSYan Zheng u64 csum_end; 421223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 42217d217feSYan Zheng 4230b246afaSJeff Mahoney ASSERT(IS_ALIGNED(start, fs_info->sectorsize) && 4240b246afaSJeff Mahoney IS_ALIGNED(end + 1, fs_info->sectorsize)); 4254277a9c3SJosef Bacik 42617d217feSYan Zheng path = btrfs_alloc_path(); 427d8926bb3SMark Fasheh if (!path) 428d8926bb3SMark Fasheh return -ENOMEM; 42917d217feSYan Zheng 430a2de733cSArne Jansen if (search_commit) { 431a2de733cSArne Jansen path->skip_locking = 1; 432e4058b54SDavid Sterba path->reada = READA_FORWARD; 433a2de733cSArne Jansen path->search_commit_root = 1; 434a2de733cSArne Jansen } 435a2de733cSArne Jansen 43617d217feSYan Zheng key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 43717d217feSYan Zheng key.offset = start; 43817d217feSYan Zheng key.type = BTRFS_EXTENT_CSUM_KEY; 43917d217feSYan Zheng 44007d400a6SYan Zheng ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 44117d217feSYan Zheng if (ret < 0) 44217d217feSYan Zheng goto fail; 44317d217feSYan Zheng if (ret > 0 && path->slots[0] > 0) { 44417d217feSYan Zheng leaf = path->nodes[0]; 44517d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); 44617d217feSYan Zheng if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID && 44717d217feSYan Zheng key.type == BTRFS_EXTENT_CSUM_KEY) { 448265fdfa6SDavid Sterba offset = (start - key.offset) >> fs_info->sectorsize_bits; 44917d217feSYan Zheng if (offset * csum_size < 45017d217feSYan Zheng btrfs_item_size_nr(leaf, path->slots[0] - 1)) 45117d217feSYan Zheng path->slots[0]--; 45217d217feSYan Zheng } 45317d217feSYan Zheng } 45417d217feSYan Zheng 45517d217feSYan Zheng while (start <= end) { 45617d217feSYan Zheng leaf = path->nodes[0]; 45717d217feSYan Zheng if (path->slots[0] >= btrfs_header_nritems(leaf)) { 45807d400a6SYan Zheng ret = btrfs_next_leaf(root, path); 45917d217feSYan Zheng if (ret < 0) 46017d217feSYan Zheng goto fail; 46117d217feSYan Zheng if (ret > 0) 46217d217feSYan Zheng break; 46317d217feSYan Zheng leaf = path->nodes[0]; 46417d217feSYan Zheng } 46517d217feSYan Zheng 46617d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 46717d217feSYan Zheng if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 468628c8282SZhi Yong Wu key.type != BTRFS_EXTENT_CSUM_KEY || 469628c8282SZhi Yong Wu key.offset > end) 47017d217feSYan Zheng break; 47117d217feSYan Zheng 47217d217feSYan Zheng if (key.offset > start) 47317d217feSYan Zheng start = key.offset; 47417d217feSYan Zheng 47517d217feSYan Zheng size = btrfs_item_size_nr(leaf, path->slots[0]); 4760b246afaSJeff Mahoney csum_end = key.offset + (size / csum_size) * fs_info->sectorsize; 47787b29b20SYan Zheng if (csum_end <= start) { 47887b29b20SYan Zheng path->slots[0]++; 47987b29b20SYan Zheng continue; 48087b29b20SYan Zheng } 48117d217feSYan Zheng 48207d400a6SYan Zheng csum_end = min(csum_end, end + 1); 48307d400a6SYan Zheng item = btrfs_item_ptr(path->nodes[0], path->slots[0], 48407d400a6SYan Zheng struct btrfs_csum_item); 48507d400a6SYan Zheng while (start < csum_end) { 48607d400a6SYan Zheng size = min_t(size_t, csum_end - start, 4871e25a2e3SJohannes Thumshirn max_ordered_sum_bytes(fs_info, csum_size)); 4880b246afaSJeff Mahoney sums = kzalloc(btrfs_ordered_sum_size(fs_info, size), 48907d400a6SYan Zheng GFP_NOFS); 4900678b618SMark Fasheh if (!sums) { 4910678b618SMark Fasheh ret = -ENOMEM; 4920678b618SMark Fasheh goto fail; 4930678b618SMark Fasheh } 49417d217feSYan Zheng 49517d217feSYan Zheng sums->bytenr = start; 496f51a4a18SMiao Xie sums->len = (int)size; 49717d217feSYan Zheng 498265fdfa6SDavid Sterba offset = (start - key.offset) >> fs_info->sectorsize_bits; 49917d217feSYan Zheng offset *= csum_size; 500265fdfa6SDavid Sterba size >>= fs_info->sectorsize_bits; 50117d217feSYan Zheng 50207d400a6SYan Zheng read_extent_buffer(path->nodes[0], 503f51a4a18SMiao Xie sums->sums, 504f51a4a18SMiao Xie ((unsigned long)item) + offset, 505f51a4a18SMiao Xie csum_size * size); 50617d217feSYan Zheng 5070b246afaSJeff Mahoney start += fs_info->sectorsize * size; 5080678b618SMark Fasheh list_add_tail(&sums->list, &tmplist); 50907d400a6SYan Zheng } 51017d217feSYan Zheng path->slots[0]++; 51117d217feSYan Zheng } 51217d217feSYan Zheng ret = 0; 51317d217feSYan Zheng fail: 5140678b618SMark Fasheh while (ret < 0 && !list_empty(&tmplist)) { 5156e5aafb2SChris Mason sums = list_entry(tmplist.next, struct btrfs_ordered_sum, list); 5160678b618SMark Fasheh list_del(&sums->list); 5170678b618SMark Fasheh kfree(sums); 5180678b618SMark Fasheh } 5190678b618SMark Fasheh list_splice_tail(&tmplist, list); 5200678b618SMark Fasheh 52117d217feSYan Zheng btrfs_free_path(path); 52217d217feSYan Zheng return ret; 52317d217feSYan Zheng } 52417d217feSYan Zheng 52551d470aeSNikolay Borisov /* 52651d470aeSNikolay Borisov * btrfs_csum_one_bio - Calculates checksums of the data contained inside a bio 52751d470aeSNikolay Borisov * @inode: Owner of the data inside the bio 52851d470aeSNikolay Borisov * @bio: Contains the data to be checksummed 52951d470aeSNikolay Borisov * @file_start: offset in file this bio begins to describe 53051d470aeSNikolay Borisov * @contig: Boolean. If true/1 means all bio vecs in this bio are 53151d470aeSNikolay Borisov * contiguous and they begin at @file_start in the file. False/0 53251d470aeSNikolay Borisov * means this bio can contains potentially discontigous bio vecs 53351d470aeSNikolay Borisov * so the logical offset of each should be calculated separately. 53451d470aeSNikolay Borisov */ 535bd242a08SNikolay Borisov blk_status_t btrfs_csum_one_bio(struct btrfs_inode *inode, struct bio *bio, 5362ff7e61eSJeff Mahoney u64 file_start, int contig) 537e015640fSChris Mason { 538c3504372SNikolay Borisov struct btrfs_fs_info *fs_info = inode->root->fs_info; 539d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 540e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums; 5416cd7ce49SChristoph Hellwig struct btrfs_ordered_extent *ordered = NULL; 542e015640fSChris Mason char *data; 54317347cecSLiu Bo struct bvec_iter iter; 54417347cecSLiu Bo struct bio_vec bvec; 545f51a4a18SMiao Xie int index; 546c40a3d38SChandan Rajendra int nr_sectors; 5473edf7d33SChris Mason unsigned long total_bytes = 0; 5483edf7d33SChris Mason unsigned long this_sum_bytes = 0; 54917347cecSLiu Bo int i; 5503edf7d33SChris Mason u64 offset; 551a3d46aeaSNikolay Borisov unsigned nofs_flag; 552e015640fSChris Mason 553a3d46aeaSNikolay Borisov nofs_flag = memalloc_nofs_save(); 554a3d46aeaSNikolay Borisov sums = kvzalloc(btrfs_ordered_sum_size(fs_info, bio->bi_iter.bi_size), 555a3d46aeaSNikolay Borisov GFP_KERNEL); 556a3d46aeaSNikolay Borisov memalloc_nofs_restore(nofs_flag); 557a3d46aeaSNikolay Borisov 558e015640fSChris Mason if (!sums) 5594e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 5603edf7d33SChris Mason 5614f024f37SKent Overstreet sums->len = bio->bi_iter.bi_size; 562e6dcd2dcSChris Mason INIT_LIST_HEAD(&sums->list); 563d20f7043SChris Mason 564d20f7043SChris Mason if (contig) 565d20f7043SChris Mason offset = file_start; 566d20f7043SChris Mason else 5676cd7ce49SChristoph Hellwig offset = 0; /* shut up gcc */ 568d20f7043SChris Mason 5691201b58bSDavid Sterba sums->bytenr = bio->bi_iter.bi_sector << 9; 570f51a4a18SMiao Xie index = 0; 571e015640fSChris Mason 572d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 573d5178578SJohannes Thumshirn 57417347cecSLiu Bo bio_for_each_segment(bvec, bio, iter) { 575d20f7043SChris Mason if (!contig) 57617347cecSLiu Bo offset = page_offset(bvec.bv_page) + bvec.bv_offset; 577d20f7043SChris Mason 5786cd7ce49SChristoph Hellwig if (!ordered) { 5796cd7ce49SChristoph Hellwig ordered = btrfs_lookup_ordered_extent(inode, offset); 5806cd7ce49SChristoph Hellwig BUG_ON(!ordered); /* Logic error */ 5816cd7ce49SChristoph Hellwig } 5826cd7ce49SChristoph Hellwig 5830b246afaSJeff Mahoney nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, 58417347cecSLiu Bo bvec.bv_len + fs_info->sectorsize 585c40a3d38SChandan Rajendra - 1); 586c40a3d38SChandan Rajendra 587c40a3d38SChandan Rajendra for (i = 0; i < nr_sectors; i++) { 588bffe633eSOmar Sandoval if (offset >= ordered->file_offset + ordered->num_bytes || 589e58dd74bSJosef Bacik offset < ordered->file_offset) { 5903edf7d33SChris Mason unsigned long bytes_left; 591c40a3d38SChandan Rajendra 5923edf7d33SChris Mason sums->len = this_sum_bytes; 5933edf7d33SChris Mason this_sum_bytes = 0; 594f9756261SNikolay Borisov btrfs_add_ordered_sum(ordered, sums); 5953edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 5963edf7d33SChris Mason 5974f024f37SKent Overstreet bytes_left = bio->bi_iter.bi_size - total_bytes; 5983edf7d33SChris Mason 599a3d46aeaSNikolay Borisov nofs_flag = memalloc_nofs_save(); 600a3d46aeaSNikolay Borisov sums = kvzalloc(btrfs_ordered_sum_size(fs_info, 601a3d46aeaSNikolay Borisov bytes_left), GFP_KERNEL); 602a3d46aeaSNikolay Borisov memalloc_nofs_restore(nofs_flag); 60379787eaaSJeff Mahoney BUG_ON(!sums); /* -ENOMEM */ 6043edf7d33SChris Mason sums->len = bytes_left; 605c40a3d38SChandan Rajendra ordered = btrfs_lookup_ordered_extent(inode, 606c40a3d38SChandan Rajendra offset); 607c40a3d38SChandan Rajendra ASSERT(ordered); /* Logic error */ 6081201b58bSDavid Sterba sums->bytenr = (bio->bi_iter.bi_sector << 9) 609c40a3d38SChandan Rajendra + total_bytes; 610f51a4a18SMiao Xie index = 0; 611c40a3d38SChandan Rajendra } 612c40a3d38SChandan Rajendra 613443c8e2aSJohannes Thumshirn data = kmap_atomic(bvec.bv_page); 614fd08001fSEric Biggers crypto_shash_digest(shash, data + bvec.bv_offset 6150b246afaSJeff Mahoney + (i * fs_info->sectorsize), 616fd08001fSEric Biggers fs_info->sectorsize, 617fd08001fSEric Biggers sums->sums + index); 618443c8e2aSJohannes Thumshirn kunmap_atomic(data); 619713cebfbSDavid Sterba index += fs_info->csum_size; 6200b246afaSJeff Mahoney offset += fs_info->sectorsize; 6210b246afaSJeff Mahoney this_sum_bytes += fs_info->sectorsize; 6220b246afaSJeff Mahoney total_bytes += fs_info->sectorsize; 623c40a3d38SChandan Rajendra } 624c40a3d38SChandan Rajendra 625e015640fSChris Mason } 626ed98b56aSChris Mason this_sum_bytes = 0; 627f9756261SNikolay Borisov btrfs_add_ordered_sum(ordered, sums); 6283edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 629e015640fSChris Mason return 0; 630e015640fSChris Mason } 631e015640fSChris Mason 632459931ecSChris Mason /* 633459931ecSChris Mason * helper function for csum removal, this expects the 634459931ecSChris Mason * key to describe the csum pointed to by the path, and it expects 635459931ecSChris Mason * the csum to overlap the range [bytenr, len] 636459931ecSChris Mason * 637459931ecSChris Mason * The csum should not be entirely contained in the range and the 638459931ecSChris Mason * range should not be entirely contained in the csum. 639459931ecSChris Mason * 640459931ecSChris Mason * This calls btrfs_truncate_item with the correct args based on the 641459931ecSChris Mason * overlap, and fixes up the key as required. 642459931ecSChris Mason */ 6432ff7e61eSJeff Mahoney static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info, 644459931ecSChris Mason struct btrfs_path *path, 645459931ecSChris Mason struct btrfs_key *key, 646459931ecSChris Mason u64 bytenr, u64 len) 647459931ecSChris Mason { 648459931ecSChris Mason struct extent_buffer *leaf; 649223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 650459931ecSChris Mason u64 csum_end; 651459931ecSChris Mason u64 end_byte = bytenr + len; 652265fdfa6SDavid Sterba u32 blocksize_bits = fs_info->sectorsize_bits; 653459931ecSChris Mason 654459931ecSChris Mason leaf = path->nodes[0]; 655459931ecSChris Mason csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; 656265fdfa6SDavid Sterba csum_end <<= blocksize_bits; 657459931ecSChris Mason csum_end += key->offset; 658459931ecSChris Mason 659459931ecSChris Mason if (key->offset < bytenr && csum_end <= end_byte) { 660459931ecSChris Mason /* 661459931ecSChris Mason * [ bytenr - len ] 662459931ecSChris Mason * [ ] 663459931ecSChris Mason * [csum ] 664459931ecSChris Mason * A simple truncate off the end of the item 665459931ecSChris Mason */ 666459931ecSChris Mason u32 new_size = (bytenr - key->offset) >> blocksize_bits; 667459931ecSChris Mason new_size *= csum_size; 66878ac4f9eSDavid Sterba btrfs_truncate_item(path, new_size, 1); 669459931ecSChris Mason } else if (key->offset >= bytenr && csum_end > end_byte && 670459931ecSChris Mason end_byte > key->offset) { 671459931ecSChris Mason /* 672459931ecSChris Mason * [ bytenr - len ] 673459931ecSChris Mason * [ ] 674459931ecSChris Mason * [csum ] 675459931ecSChris Mason * we need to truncate from the beginning of the csum 676459931ecSChris Mason */ 677459931ecSChris Mason u32 new_size = (csum_end - end_byte) >> blocksize_bits; 678459931ecSChris Mason new_size *= csum_size; 679459931ecSChris Mason 68078ac4f9eSDavid Sterba btrfs_truncate_item(path, new_size, 0); 681459931ecSChris Mason 682459931ecSChris Mason key->offset = end_byte; 6830b246afaSJeff Mahoney btrfs_set_item_key_safe(fs_info, path, key); 684459931ecSChris Mason } else { 685459931ecSChris Mason BUG(); 686459931ecSChris Mason } 687459931ecSChris Mason } 688459931ecSChris Mason 689459931ecSChris Mason /* 690459931ecSChris Mason * deletes the csum items from the csum tree for a given 691459931ecSChris Mason * range of bytes. 692459931ecSChris Mason */ 693459931ecSChris Mason int btrfs_del_csums(struct btrfs_trans_handle *trans, 69440e046acSFilipe Manana struct btrfs_root *root, u64 bytenr, u64 len) 695459931ecSChris Mason { 69640e046acSFilipe Manana struct btrfs_fs_info *fs_info = trans->fs_info; 697459931ecSChris Mason struct btrfs_path *path; 698459931ecSChris Mason struct btrfs_key key; 699459931ecSChris Mason u64 end_byte = bytenr + len; 700459931ecSChris Mason u64 csum_end; 701459931ecSChris Mason struct extent_buffer *leaf; 702459931ecSChris Mason int ret; 703223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 704265fdfa6SDavid Sterba u32 blocksize_bits = fs_info->sectorsize_bits; 705459931ecSChris Mason 70640e046acSFilipe Manana ASSERT(root == fs_info->csum_root || 70740e046acSFilipe Manana root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); 70840e046acSFilipe Manana 709459931ecSChris Mason path = btrfs_alloc_path(); 7102a29edc6Sliubo if (!path) 7112a29edc6Sliubo return -ENOMEM; 712459931ecSChris Mason 713459931ecSChris Mason while (1) { 714459931ecSChris Mason key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 715459931ecSChris Mason key.offset = end_byte - 1; 716459931ecSChris Mason key.type = BTRFS_EXTENT_CSUM_KEY; 717459931ecSChris Mason 718459931ecSChris Mason ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 719459931ecSChris Mason if (ret > 0) { 720459931ecSChris Mason if (path->slots[0] == 0) 72165a246c5STsutomu Itoh break; 722459931ecSChris Mason path->slots[0]--; 723ad0397a7SJosef Bacik } else if (ret < 0) { 72465a246c5STsutomu Itoh break; 725459931ecSChris Mason } 726ad0397a7SJosef Bacik 727459931ecSChris Mason leaf = path->nodes[0]; 728459931ecSChris Mason btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 729459931ecSChris Mason 730459931ecSChris Mason if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 731459931ecSChris Mason key.type != BTRFS_EXTENT_CSUM_KEY) { 732459931ecSChris Mason break; 733459931ecSChris Mason } 734459931ecSChris Mason 735459931ecSChris Mason if (key.offset >= end_byte) 736459931ecSChris Mason break; 737459931ecSChris Mason 738459931ecSChris Mason csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; 739459931ecSChris Mason csum_end <<= blocksize_bits; 740459931ecSChris Mason csum_end += key.offset; 741459931ecSChris Mason 742459931ecSChris Mason /* this csum ends before we start, we're done */ 743459931ecSChris Mason if (csum_end <= bytenr) 744459931ecSChris Mason break; 745459931ecSChris Mason 746459931ecSChris Mason /* delete the entire item, it is inside our range */ 747459931ecSChris Mason if (key.offset >= bytenr && csum_end <= end_byte) { 7486f546216SFilipe Manana int del_nr = 1; 7496f546216SFilipe Manana 7506f546216SFilipe Manana /* 7516f546216SFilipe Manana * Check how many csum items preceding this one in this 7526f546216SFilipe Manana * leaf correspond to our range and then delete them all 7536f546216SFilipe Manana * at once. 7546f546216SFilipe Manana */ 7556f546216SFilipe Manana if (key.offset > bytenr && path->slots[0] > 0) { 7566f546216SFilipe Manana int slot = path->slots[0] - 1; 7576f546216SFilipe Manana 7586f546216SFilipe Manana while (slot >= 0) { 7596f546216SFilipe Manana struct btrfs_key pk; 7606f546216SFilipe Manana 7616f546216SFilipe Manana btrfs_item_key_to_cpu(leaf, &pk, slot); 7626f546216SFilipe Manana if (pk.offset < bytenr || 7636f546216SFilipe Manana pk.type != BTRFS_EXTENT_CSUM_KEY || 7646f546216SFilipe Manana pk.objectid != 7656f546216SFilipe Manana BTRFS_EXTENT_CSUM_OBJECTID) 7666f546216SFilipe Manana break; 7676f546216SFilipe Manana path->slots[0] = slot; 7686f546216SFilipe Manana del_nr++; 7696f546216SFilipe Manana key.offset = pk.offset; 7706f546216SFilipe Manana slot--; 7716f546216SFilipe Manana } 7726f546216SFilipe Manana } 7736f546216SFilipe Manana ret = btrfs_del_items(trans, root, path, 7746f546216SFilipe Manana path->slots[0], del_nr); 77565a246c5STsutomu Itoh if (ret) 77665a246c5STsutomu Itoh goto out; 777dcbdd4dcSChris Mason if (key.offset == bytenr) 778dcbdd4dcSChris Mason break; 779459931ecSChris Mason } else if (key.offset < bytenr && csum_end > end_byte) { 780459931ecSChris Mason unsigned long offset; 781459931ecSChris Mason unsigned long shift_len; 782459931ecSChris Mason unsigned long item_offset; 783459931ecSChris Mason /* 784459931ecSChris Mason * [ bytenr - len ] 785459931ecSChris Mason * [csum ] 786459931ecSChris Mason * 787459931ecSChris Mason * Our bytes are in the middle of the csum, 788459931ecSChris Mason * we need to split this item and insert a new one. 789459931ecSChris Mason * 790459931ecSChris Mason * But we can't drop the path because the 791459931ecSChris Mason * csum could change, get removed, extended etc. 792459931ecSChris Mason * 793459931ecSChris Mason * The trick here is the max size of a csum item leaves 794459931ecSChris Mason * enough room in the tree block for a single 795459931ecSChris Mason * item header. So, we split the item in place, 796459931ecSChris Mason * adding a new header pointing to the existing 797459931ecSChris Mason * bytes. Then we loop around again and we have 798459931ecSChris Mason * a nicely formed csum item that we can neatly 799459931ecSChris Mason * truncate. 800459931ecSChris Mason */ 801459931ecSChris Mason offset = (bytenr - key.offset) >> blocksize_bits; 802459931ecSChris Mason offset *= csum_size; 803459931ecSChris Mason 804459931ecSChris Mason shift_len = (len >> blocksize_bits) * csum_size; 805459931ecSChris Mason 806459931ecSChris Mason item_offset = btrfs_item_ptr_offset(leaf, 807459931ecSChris Mason path->slots[0]); 808459931ecSChris Mason 809b159fa28SDavid Sterba memzero_extent_buffer(leaf, item_offset + offset, 810459931ecSChris Mason shift_len); 811459931ecSChris Mason key.offset = bytenr; 812459931ecSChris Mason 813459931ecSChris Mason /* 814459931ecSChris Mason * btrfs_split_item returns -EAGAIN when the 815459931ecSChris Mason * item changed size or key 816459931ecSChris Mason */ 817459931ecSChris Mason ret = btrfs_split_item(trans, root, path, &key, offset); 81879787eaaSJeff Mahoney if (ret && ret != -EAGAIN) { 81966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 82079787eaaSJeff Mahoney goto out; 82179787eaaSJeff Mahoney } 822459931ecSChris Mason 823459931ecSChris Mason key.offset = end_byte - 1; 824459931ecSChris Mason } else { 8252ff7e61eSJeff Mahoney truncate_one_csum(fs_info, path, &key, bytenr, len); 826dcbdd4dcSChris Mason if (key.offset < bytenr) 827dcbdd4dcSChris Mason break; 828459931ecSChris Mason } 829b3b4aa74SDavid Sterba btrfs_release_path(path); 830459931ecSChris Mason } 83165a246c5STsutomu Itoh ret = 0; 832459931ecSChris Mason out: 833459931ecSChris Mason btrfs_free_path(path); 83465a246c5STsutomu Itoh return ret; 835459931ecSChris Mason } 836459931ecSChris Mason 837065631f6SChris Mason int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, 838d20f7043SChris Mason struct btrfs_root *root, 839e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums) 840f254e52cSChris Mason { 8410b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 842f254e52cSChris Mason struct btrfs_key file_key; 8436567e837SChris Mason struct btrfs_key found_key; 8445caf2a00SChris Mason struct btrfs_path *path; 845f254e52cSChris Mason struct btrfs_csum_item *item; 846065631f6SChris Mason struct btrfs_csum_item *item_end; 847ff79f819SChris Mason struct extent_buffer *leaf = NULL; 848f51a4a18SMiao Xie u64 next_offset; 849f51a4a18SMiao Xie u64 total_bytes = 0; 8506567e837SChris Mason u64 csum_offset; 851f51a4a18SMiao Xie u64 bytenr; 852f578d4bdSChris Mason u32 nritems; 853f578d4bdSChris Mason u32 ins_size; 854f51a4a18SMiao Xie int index = 0; 855f51a4a18SMiao Xie int found_next; 856f51a4a18SMiao Xie int ret; 857223486c2SDavid Sterba const u32 csum_size = fs_info->csum_size; 8586e92f5e6SChris Mason 8595caf2a00SChris Mason path = btrfs_alloc_path(); 860d8926bb3SMark Fasheh if (!path) 861d8926bb3SMark Fasheh return -ENOMEM; 862065631f6SChris Mason again: 863065631f6SChris Mason next_offset = (u64)-1; 864065631f6SChris Mason found_next = 0; 865f51a4a18SMiao Xie bytenr = sums->bytenr + total_bytes; 866d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 867f51a4a18SMiao Xie file_key.offset = bytenr; 868962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_CSUM_KEY; 869a429e513SChris Mason 870f51a4a18SMiao Xie item = btrfs_lookup_csum(trans, root, path, bytenr, 1); 871ff79f819SChris Mason if (!IS_ERR(item)) { 872639cb586SChris Mason ret = 0; 873f51a4a18SMiao Xie leaf = path->nodes[0]; 874f51a4a18SMiao Xie item_end = btrfs_item_ptr(leaf, path->slots[0], 875f51a4a18SMiao Xie struct btrfs_csum_item); 876f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((char *)item_end + 877f51a4a18SMiao Xie btrfs_item_size_nr(leaf, path->slots[0])); 878a429e513SChris Mason goto found; 879ff79f819SChris Mason } 880a429e513SChris Mason ret = PTR_ERR(item); 8814a500fd1SYan, Zheng if (ret != -EFBIG && ret != -ENOENT) 882918cdf44SFilipe Manana goto out; 8834a500fd1SYan, Zheng 884a429e513SChris Mason if (ret == -EFBIG) { 885a429e513SChris Mason u32 item_size; 886a429e513SChris Mason /* we found one, but it isn't big enough yet */ 8875f39d397SChris Mason leaf = path->nodes[0]; 8885f39d397SChris Mason item_size = btrfs_item_size_nr(leaf, path->slots[0]); 889607d432dSJosef Bacik if ((item_size / csum_size) >= 8900b246afaSJeff Mahoney MAX_CSUM_ITEMS(fs_info, csum_size)) { 891a429e513SChris Mason /* already at max size, make a new one */ 892a429e513SChris Mason goto insert; 893a429e513SChris Mason } 894a429e513SChris Mason } else { 895f578d4bdSChris Mason int slot = path->slots[0] + 1; 896a429e513SChris Mason /* we didn't find a csum item, insert one */ 897f578d4bdSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 89835045bf2SFilipe Manana if (!nritems || (path->slots[0] >= nritems - 1)) { 899f578d4bdSChris Mason ret = btrfs_next_leaf(root, path); 9007e4a3f7eSFilipe Manana if (ret < 0) { 9017e4a3f7eSFilipe Manana goto out; 9027e4a3f7eSFilipe Manana } else if (ret > 0) { 903f578d4bdSChris Mason found_next = 1; 904f578d4bdSChris Mason goto insert; 9057e4a3f7eSFilipe Manana } 90627b9a812SFilipe Manana slot = path->slots[0]; 907f578d4bdSChris Mason } 908f578d4bdSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); 909d20f7043SChris Mason if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 910d20f7043SChris Mason found_key.type != BTRFS_EXTENT_CSUM_KEY) { 911f578d4bdSChris Mason found_next = 1; 912f578d4bdSChris Mason goto insert; 913f578d4bdSChris Mason } 914f578d4bdSChris Mason next_offset = found_key.offset; 915f578d4bdSChris Mason found_next = 1; 916a429e513SChris Mason goto insert; 917a429e513SChris Mason } 918a429e513SChris Mason 919a429e513SChris Mason /* 920cc14600cSFilipe Manana * At this point, we know the tree has a checksum item that ends at an 921cc14600cSFilipe Manana * offset matching the start of the checksum range we want to insert. 922cc14600cSFilipe Manana * We try to extend that item as much as possible and then add as many 923cc14600cSFilipe Manana * checksums to it as they fit. 924cc14600cSFilipe Manana * 925cc14600cSFilipe Manana * First check if the leaf has enough free space for at least one 926cc14600cSFilipe Manana * checksum. If it has go directly to the item extension code, otherwise 927cc14600cSFilipe Manana * release the path and do a search for insertion before the extension. 928a429e513SChris Mason */ 929cc14600cSFilipe Manana if (btrfs_leaf_free_space(leaf) >= csum_size) { 930cc14600cSFilipe Manana btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 931cc14600cSFilipe Manana csum_offset = (bytenr - found_key.offset) >> 932265fdfa6SDavid Sterba fs_info->sectorsize_bits; 933cc14600cSFilipe Manana goto extend_csum; 934cc14600cSFilipe Manana } 935cc14600cSFilipe Manana 936b3b4aa74SDavid Sterba btrfs_release_path(path); 9376567e837SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 938607d432dSJosef Bacik csum_size, 1); 9396567e837SChris Mason if (ret < 0) 940918cdf44SFilipe Manana goto out; 941459931ecSChris Mason 942459931ecSChris Mason if (ret > 0) { 943459931ecSChris Mason if (path->slots[0] == 0) 9446567e837SChris Mason goto insert; 9456567e837SChris Mason path->slots[0]--; 946459931ecSChris Mason } 947459931ecSChris Mason 9485f39d397SChris Mason leaf = path->nodes[0]; 9495f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 950265fdfa6SDavid Sterba csum_offset = (bytenr - found_key.offset) >> fs_info->sectorsize_bits; 951459931ecSChris Mason 952962a298fSDavid Sterba if (found_key.type != BTRFS_EXTENT_CSUM_KEY || 953d20f7043SChris Mason found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 9540b246afaSJeff Mahoney csum_offset >= MAX_CSUM_ITEMS(fs_info, csum_size)) { 9556567e837SChris Mason goto insert; 9566567e837SChris Mason } 957459931ecSChris Mason 958cc14600cSFilipe Manana extend_csum: 9592f697dc6SLiu Bo if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) / 960607d432dSJosef Bacik csum_size) { 9612f697dc6SLiu Bo int extend_nr; 9622f697dc6SLiu Bo u64 tmp; 9632f697dc6SLiu Bo u32 diff; 964459931ecSChris Mason 965f51a4a18SMiao Xie tmp = sums->len - total_bytes; 966265fdfa6SDavid Sterba tmp >>= fs_info->sectorsize_bits; 9672f697dc6SLiu Bo WARN_ON(tmp < 1); 9682f697dc6SLiu Bo 9692f697dc6SLiu Bo extend_nr = max_t(int, 1, (int)tmp); 9702f697dc6SLiu Bo diff = (csum_offset + extend_nr) * csum_size; 9710b246afaSJeff Mahoney diff = min(diff, 9720b246afaSJeff Mahoney MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size); 973459931ecSChris Mason 9745f39d397SChris Mason diff = diff - btrfs_item_size_nr(leaf, path->slots[0]); 975cc14600cSFilipe Manana diff = min_t(u32, btrfs_leaf_free_space(leaf), diff); 9762f697dc6SLiu Bo diff /= csum_size; 9772f697dc6SLiu Bo diff *= csum_size; 978459931ecSChris Mason 979c71dd880SDavid Sterba btrfs_extend_item(path, diff); 980f51a4a18SMiao Xie ret = 0; 9816567e837SChris Mason goto csum; 9826567e837SChris Mason } 9836567e837SChris Mason 9846567e837SChris Mason insert: 985b3b4aa74SDavid Sterba btrfs_release_path(path); 9866567e837SChris Mason csum_offset = 0; 987f578d4bdSChris Mason if (found_next) { 9882f697dc6SLiu Bo u64 tmp; 989d20f7043SChris Mason 990f51a4a18SMiao Xie tmp = sums->len - total_bytes; 991265fdfa6SDavid Sterba tmp >>= fs_info->sectorsize_bits; 9922f697dc6SLiu Bo tmp = min(tmp, (next_offset - file_key.offset) >> 993265fdfa6SDavid Sterba fs_info->sectorsize_bits); 9942f697dc6SLiu Bo 99550d0446eSSeraphime Kirkovski tmp = max_t(u64, 1, tmp); 99650d0446eSSeraphime Kirkovski tmp = min_t(u64, tmp, MAX_CSUM_ITEMS(fs_info, csum_size)); 997607d432dSJosef Bacik ins_size = csum_size * tmp; 998f578d4bdSChris Mason } else { 999607d432dSJosef Bacik ins_size = csum_size; 1000f578d4bdSChris Mason } 10015caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 1002f578d4bdSChris Mason ins_size); 100354aa1f4dSChris Mason if (ret < 0) 1004918cdf44SFilipe Manana goto out; 1005fae7f21cSDulshani Gunawardhana if (WARN_ON(ret != 0)) 1006918cdf44SFilipe Manana goto out; 10075f39d397SChris Mason leaf = path->nodes[0]; 1008f51a4a18SMiao Xie csum: 10095f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 1010f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((unsigned char *)item + 1011f51a4a18SMiao Xie btrfs_item_size_nr(leaf, path->slots[0])); 1012509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 1013607d432dSJosef Bacik csum_offset * csum_size); 1014b18c6685SChris Mason found: 1015265fdfa6SDavid Sterba ins_size = (u32)(sums->len - total_bytes) >> fs_info->sectorsize_bits; 1016f51a4a18SMiao Xie ins_size *= csum_size; 1017f51a4a18SMiao Xie ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item, 1018f51a4a18SMiao Xie ins_size); 1019f51a4a18SMiao Xie write_extent_buffer(leaf, sums->sums + index, (unsigned long)item, 1020f51a4a18SMiao Xie ins_size); 1021aadfeb6eSChris Mason 10221e25a2e3SJohannes Thumshirn index += ins_size; 1023f51a4a18SMiao Xie ins_size /= csum_size; 10240b246afaSJeff Mahoney total_bytes += ins_size * fs_info->sectorsize; 1025a6591715SChris Mason 10265caf2a00SChris Mason btrfs_mark_buffer_dirty(path->nodes[0]); 1027e6dcd2dcSChris Mason if (total_bytes < sums->len) { 1028b3b4aa74SDavid Sterba btrfs_release_path(path); 1029b9473439SChris Mason cond_resched(); 1030065631f6SChris Mason goto again; 1031065631f6SChris Mason } 103253863232SChris Mason out: 10335caf2a00SChris Mason btrfs_free_path(path); 1034f254e52cSChris Mason return ret; 1035f254e52cSChris Mason } 10367ffbb598SFilipe Manana 10379cdc5124SNikolay Borisov void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, 10387ffbb598SFilipe Manana const struct btrfs_path *path, 10397ffbb598SFilipe Manana struct btrfs_file_extent_item *fi, 10407ffbb598SFilipe Manana const bool new_inline, 10417ffbb598SFilipe Manana struct extent_map *em) 10427ffbb598SFilipe Manana { 10433ffbd68cSDavid Sterba struct btrfs_fs_info *fs_info = inode->root->fs_info; 10449cdc5124SNikolay Borisov struct btrfs_root *root = inode->root; 10457ffbb598SFilipe Manana struct extent_buffer *leaf = path->nodes[0]; 10467ffbb598SFilipe Manana const int slot = path->slots[0]; 10477ffbb598SFilipe Manana struct btrfs_key key; 10487ffbb598SFilipe Manana u64 extent_start, extent_end; 10497ffbb598SFilipe Manana u64 bytenr; 10507ffbb598SFilipe Manana u8 type = btrfs_file_extent_type(leaf, fi); 10517ffbb598SFilipe Manana int compress_type = btrfs_file_extent_compression(leaf, fi); 10527ffbb598SFilipe Manana 10537ffbb598SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot); 10547ffbb598SFilipe Manana extent_start = key.offset; 1055a5eeb3d1SFilipe Manana extent_end = btrfs_file_extent_end(path); 10567ffbb598SFilipe Manana em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); 10577ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_REG || 10587ffbb598SFilipe Manana type == BTRFS_FILE_EXTENT_PREALLOC) { 10597ffbb598SFilipe Manana em->start = extent_start; 10607ffbb598SFilipe Manana em->len = extent_end - extent_start; 10617ffbb598SFilipe Manana em->orig_start = extent_start - 10627ffbb598SFilipe Manana btrfs_file_extent_offset(leaf, fi); 10637ffbb598SFilipe Manana em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); 10647ffbb598SFilipe Manana bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); 10657ffbb598SFilipe Manana if (bytenr == 0) { 10667ffbb598SFilipe Manana em->block_start = EXTENT_MAP_HOLE; 10677ffbb598SFilipe Manana return; 10687ffbb598SFilipe Manana } 10697ffbb598SFilipe Manana if (compress_type != BTRFS_COMPRESS_NONE) { 10707ffbb598SFilipe Manana set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 10717ffbb598SFilipe Manana em->compress_type = compress_type; 10727ffbb598SFilipe Manana em->block_start = bytenr; 10737ffbb598SFilipe Manana em->block_len = em->orig_block_len; 10747ffbb598SFilipe Manana } else { 10757ffbb598SFilipe Manana bytenr += btrfs_file_extent_offset(leaf, fi); 10767ffbb598SFilipe Manana em->block_start = bytenr; 10777ffbb598SFilipe Manana em->block_len = em->len; 10787ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_PREALLOC) 10797ffbb598SFilipe Manana set_bit(EXTENT_FLAG_PREALLOC, &em->flags); 10807ffbb598SFilipe Manana } 10817ffbb598SFilipe Manana } else if (type == BTRFS_FILE_EXTENT_INLINE) { 10827ffbb598SFilipe Manana em->block_start = EXTENT_MAP_INLINE; 10837ffbb598SFilipe Manana em->start = extent_start; 10847ffbb598SFilipe Manana em->len = extent_end - extent_start; 10857ffbb598SFilipe Manana /* 10867ffbb598SFilipe Manana * Initialize orig_start and block_len with the same values 10877ffbb598SFilipe Manana * as in inode.c:btrfs_get_extent(). 10887ffbb598SFilipe Manana */ 10897ffbb598SFilipe Manana em->orig_start = EXTENT_MAP_HOLE; 10907ffbb598SFilipe Manana em->block_len = (u64)-1; 10917ffbb598SFilipe Manana if (!new_inline && compress_type != BTRFS_COMPRESS_NONE) { 10927ffbb598SFilipe Manana set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 10937ffbb598SFilipe Manana em->compress_type = compress_type; 10947ffbb598SFilipe Manana } 10957ffbb598SFilipe Manana } else { 10960b246afaSJeff Mahoney btrfs_err(fs_info, 10979cdc5124SNikolay Borisov "unknown file extent item type %d, inode %llu, offset %llu, " 10989cdc5124SNikolay Borisov "root %llu", type, btrfs_ino(inode), extent_start, 10997ffbb598SFilipe Manana root->root_key.objectid); 11007ffbb598SFilipe Manana } 11017ffbb598SFilipe Manana } 1102a5eeb3d1SFilipe Manana 1103a5eeb3d1SFilipe Manana /* 1104a5eeb3d1SFilipe Manana * Returns the end offset (non inclusive) of the file extent item the given path 1105a5eeb3d1SFilipe Manana * points to. If it points to an inline extent, the returned offset is rounded 1106a5eeb3d1SFilipe Manana * up to the sector size. 1107a5eeb3d1SFilipe Manana */ 1108a5eeb3d1SFilipe Manana u64 btrfs_file_extent_end(const struct btrfs_path *path) 1109a5eeb3d1SFilipe Manana { 1110a5eeb3d1SFilipe Manana const struct extent_buffer *leaf = path->nodes[0]; 1111a5eeb3d1SFilipe Manana const int slot = path->slots[0]; 1112a5eeb3d1SFilipe Manana struct btrfs_file_extent_item *fi; 1113a5eeb3d1SFilipe Manana struct btrfs_key key; 1114a5eeb3d1SFilipe Manana u64 end; 1115a5eeb3d1SFilipe Manana 1116a5eeb3d1SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot); 1117a5eeb3d1SFilipe Manana ASSERT(key.type == BTRFS_EXTENT_DATA_KEY); 1118a5eeb3d1SFilipe Manana fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); 1119a5eeb3d1SFilipe Manana 1120a5eeb3d1SFilipe Manana if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) { 1121a5eeb3d1SFilipe Manana end = btrfs_file_extent_ram_bytes(leaf, fi); 1122a5eeb3d1SFilipe Manana end = ALIGN(key.offset + end, leaf->fs_info->sectorsize); 1123a5eeb3d1SFilipe Manana } else { 1124a5eeb3d1SFilipe Manana end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); 1125a5eeb3d1SFilipe Manana } 1126a5eeb3d1SFilipe Manana 1127a5eeb3d1SFilipe Manana return end; 1128a5eeb3d1SFilipe Manana } 1129