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 */ 4141a2ee75SJosef Bacik void btrfs_inode_safe_disk_i_size_write(struct inode *inode, u64 new_i_size) 4241a2ee75SJosef Bacik { 4341a2ee75SJosef Bacik struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 4441a2ee75SJosef Bacik u64 start, end, i_size; 4541a2ee75SJosef Bacik int ret; 4641a2ee75SJosef Bacik 4741a2ee75SJosef Bacik i_size = new_i_size ?: i_size_read(inode); 4841a2ee75SJosef Bacik if (btrfs_fs_incompat(fs_info, NO_HOLES)) { 4941a2ee75SJosef Bacik BTRFS_I(inode)->disk_i_size = i_size; 5041a2ee75SJosef Bacik return; 5141a2ee75SJosef Bacik } 5241a2ee75SJosef Bacik 5341a2ee75SJosef Bacik spin_lock(&BTRFS_I(inode)->lock); 5441a2ee75SJosef Bacik ret = find_contiguous_extent_bit(&BTRFS_I(inode)->file_extent_tree, 0, 5541a2ee75SJosef Bacik &start, &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; 6041a2ee75SJosef Bacik BTRFS_I(inode)->disk_i_size = i_size; 6141a2ee75SJosef Bacik spin_unlock(&BTRFS_I(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 145b9473439SChris Mason path->leave_spinning = 1; 1465caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 147dee26a9fSChris Mason sizeof(*item)); 14854aa1f4dSChris Mason if (ret < 0) 14954aa1f4dSChris Mason goto out; 15079787eaaSJeff Mahoney BUG_ON(ret); /* Can't happen */ 1515f39d397SChris Mason leaf = path->nodes[0]; 1525f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], 153dee26a9fSChris Mason struct btrfs_file_extent_item); 154f2eb0a24SSage Weil btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset); 155db94535dSChris Mason btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes); 156f2eb0a24SSage Weil btrfs_set_file_extent_offset(leaf, item, offset); 157db94535dSChris Mason btrfs_set_file_extent_num_bytes(leaf, item, num_bytes); 158c8b97818SChris Mason btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes); 1595f39d397SChris Mason btrfs_set_file_extent_generation(leaf, item, trans->transid); 1605f39d397SChris Mason btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); 161c8b97818SChris Mason btrfs_set_file_extent_compression(leaf, item, compression); 162c8b97818SChris Mason btrfs_set_file_extent_encryption(leaf, item, encryption); 163c8b97818SChris Mason btrfs_set_file_extent_other_encoding(leaf, item, other_encoding); 164c8b97818SChris Mason 1655f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 16654aa1f4dSChris Mason out: 1675caf2a00SChris Mason btrfs_free_path(path); 16854aa1f4dSChris Mason return ret; 1699f5fae2fSChris Mason } 170dee26a9fSChris Mason 17148a3b636SEric Sandeen static struct btrfs_csum_item * 17248a3b636SEric Sandeen btrfs_lookup_csum(struct btrfs_trans_handle *trans, 173b18c6685SChris Mason struct btrfs_root *root, 1746567e837SChris Mason struct btrfs_path *path, 175d20f7043SChris Mason u64 bytenr, int cow) 1766567e837SChris Mason { 1770b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1786567e837SChris Mason int ret; 1796567e837SChris Mason struct btrfs_key file_key; 1806567e837SChris Mason struct btrfs_key found_key; 1816567e837SChris Mason struct btrfs_csum_item *item; 1825f39d397SChris Mason struct extent_buffer *leaf; 1836567e837SChris Mason u64 csum_offset = 0; 1840b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 185a429e513SChris Mason int csums_in_item; 1866567e837SChris Mason 187d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 188d20f7043SChris Mason file_key.offset = bytenr; 189962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_CSUM_KEY; 190b18c6685SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); 1916567e837SChris Mason if (ret < 0) 1926567e837SChris Mason goto fail; 1935f39d397SChris Mason leaf = path->nodes[0]; 1946567e837SChris Mason if (ret > 0) { 1956567e837SChris Mason ret = 1; 19670b2befdSChris Mason if (path->slots[0] == 0) 1976567e837SChris Mason goto fail; 1986567e837SChris Mason path->slots[0]--; 1995f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 200962a298fSDavid Sterba if (found_key.type != BTRFS_EXTENT_CSUM_KEY) 2016567e837SChris Mason goto fail; 202d20f7043SChris Mason 203d20f7043SChris Mason csum_offset = (bytenr - found_key.offset) >> 2040b246afaSJeff Mahoney fs_info->sb->s_blocksize_bits; 2055f39d397SChris Mason csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]); 206607d432dSJosef Bacik csums_in_item /= csum_size; 207a429e513SChris Mason 20882d130ffSMiao Xie if (csum_offset == csums_in_item) { 209a429e513SChris Mason ret = -EFBIG; 2106567e837SChris Mason goto fail; 21182d130ffSMiao Xie } else if (csum_offset > csums_in_item) { 21282d130ffSMiao Xie goto fail; 2136567e837SChris Mason } 2146567e837SChris Mason } 2156567e837SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 216509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 217607d432dSJosef Bacik csum_offset * csum_size); 2186567e837SChris Mason return item; 2196567e837SChris Mason fail: 2206567e837SChris Mason if (ret > 0) 221b18c6685SChris Mason ret = -ENOENT; 2226567e837SChris Mason return ERR_PTR(ret); 2236567e837SChris Mason } 2246567e837SChris Mason 225dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, 226dee26a9fSChris Mason struct btrfs_root *root, 227dee26a9fSChris Mason struct btrfs_path *path, u64 objectid, 2289773a788SChris Mason u64 offset, int mod) 229dee26a9fSChris Mason { 230dee26a9fSChris Mason int ret; 231dee26a9fSChris Mason struct btrfs_key file_key; 232dee26a9fSChris Mason int ins_len = mod < 0 ? -1 : 0; 233dee26a9fSChris Mason int cow = mod != 0; 234dee26a9fSChris Mason 235dee26a9fSChris Mason file_key.objectid = objectid; 23670b2befdSChris Mason file_key.offset = offset; 237962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_DATA_KEY; 238dee26a9fSChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); 239dee26a9fSChris Mason return ret; 240dee26a9fSChris Mason } 241f254e52cSChris Mason 242e62958fcSOmar Sandoval /** 243e62958fcSOmar Sandoval * btrfs_lookup_bio_sums - Look up checksums for a bio. 244e62958fcSOmar Sandoval * @inode: inode that the bio is for. 245e62958fcSOmar Sandoval * @bio: bio embedded in btrfs_io_bio. 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. 248e62958fcSOmar Sandoval * @dst: Buffer of size btrfs_super_csum_size() used to return checksum. If 249e62958fcSOmar Sandoval * NULL, the checksum is returned in btrfs_io_bio(bio)->csum instead. 250e62958fcSOmar Sandoval * 251e62958fcSOmar Sandoval * Return: BLK_STS_RESOURCE if allocating memory fails, BLK_STS_OK otherwise. 252e62958fcSOmar Sandoval */ 253e62958fcSOmar Sandoval blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, 254db72e47fSOmar Sandoval u64 offset, u8 *dst) 25561b49440SChris Mason { 2560b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 25717347cecSLiu Bo struct bio_vec bvec; 25817347cecSLiu Bo struct bvec_iter iter; 259facc8a22SMiao Xie struct btrfs_io_bio *btrfs_bio = btrfs_io_bio(bio); 260facc8a22SMiao Xie struct btrfs_csum_item *item = NULL; 261facc8a22SMiao Xie struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 262facc8a22SMiao Xie struct btrfs_path *path; 263db72e47fSOmar Sandoval const bool page_offsets = (offset == (u64)-1); 264facc8a22SMiao Xie u8 *csum; 26561b49440SChris Mason u64 item_start_offset = 0; 26661b49440SChris Mason u64 item_last_offset = 0; 267d20f7043SChris Mason u64 disk_bytenr; 268c40a3d38SChandan Rajendra u64 page_bytes_left; 26961b49440SChris Mason u32 diff; 270facc8a22SMiao Xie int nblocks; 27117347cecSLiu Bo int count = 0; 2720b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 27361b49440SChris Mason 27461b49440SChris Mason path = btrfs_alloc_path(); 275c2db1073STsutomu Itoh if (!path) 2764e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 277facc8a22SMiao Xie 2784f024f37SKent Overstreet nblocks = bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits; 279facc8a22SMiao Xie if (!dst) { 280facc8a22SMiao Xie if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) { 28131fecccbSDavid Sterba btrfs_bio->csum = kmalloc_array(nblocks, csum_size, 28231fecccbSDavid Sterba GFP_NOFS); 28331fecccbSDavid Sterba if (!btrfs_bio->csum) { 284facc8a22SMiao Xie btrfs_free_path(path); 2854e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 286facc8a22SMiao Xie } 287facc8a22SMiao Xie } else { 288facc8a22SMiao Xie btrfs_bio->csum = btrfs_bio->csum_inline; 289facc8a22SMiao Xie } 290facc8a22SMiao Xie csum = btrfs_bio->csum; 291facc8a22SMiao Xie } else { 29210fe6ca8SJohannes Thumshirn csum = dst; 293facc8a22SMiao Xie } 294facc8a22SMiao Xie 29509cbfeafSKirill A. Shutemov if (bio->bi_iter.bi_size > PAGE_SIZE * 8) 296e4058b54SDavid Sterba path->reada = READA_FORWARD; 29761b49440SChris Mason 2982cf8572dSChris Mason /* 2992cf8572dSChris Mason * the free space stuff is only read when it hasn't been 3002cf8572dSChris Mason * updated in the current transaction. So, we can safely 3012cf8572dSChris Mason * read from the commit root and sidestep a nasty deadlock 3022cf8572dSChris Mason * between reading the free space cache and updating the csum tree. 3032cf8572dSChris Mason */ 30470ddc553SNikolay Borisov if (btrfs_is_free_space_inode(BTRFS_I(inode))) { 3052cf8572dSChris Mason path->search_commit_root = 1; 306ddf23b3fSJosef Bacik path->skip_locking = 1; 307ddf23b3fSJosef Bacik } 3082cf8572dSChris Mason 3094f024f37SKent Overstreet disk_bytenr = (u64)bio->bi_iter.bi_sector << 9; 310c40a3d38SChandan Rajendra 31117347cecSLiu Bo bio_for_each_segment(bvec, bio, iter) { 31217347cecSLiu Bo page_bytes_left = bvec.bv_len; 3134989d277SChristoph Hellwig if (count) 3144989d277SChristoph Hellwig goto next; 3154989d277SChristoph Hellwig 316db72e47fSOmar Sandoval if (page_offsets) 31717347cecSLiu Bo offset = page_offset(bvec.bv_page) + bvec.bv_offset; 318facc8a22SMiao Xie count = btrfs_find_ordered_sum(inode, offset, disk_bytenr, 3191e25a2e3SJohannes Thumshirn csum, nblocks); 320e4100d98SMiao Xie if (count) 32161b49440SChris Mason goto found; 32261b49440SChris Mason 323d20f7043SChris Mason if (!item || disk_bytenr < item_start_offset || 324d20f7043SChris Mason disk_bytenr >= item_last_offset) { 32561b49440SChris Mason struct btrfs_key found_key; 32661b49440SChris Mason u32 item_size; 32761b49440SChris Mason 32861b49440SChris Mason if (item) 329b3b4aa74SDavid Sterba btrfs_release_path(path); 3300b246afaSJeff Mahoney item = btrfs_lookup_csum(NULL, fs_info->csum_root, 331d20f7043SChris Mason path, disk_bytenr, 0); 33261b49440SChris Mason if (IS_ERR(item)) { 333e4100d98SMiao Xie count = 1; 334facc8a22SMiao Xie memset(csum, 0, csum_size); 33517d217feSYan Zheng if (BTRFS_I(inode)->root->root_key.objectid == 33617d217feSYan Zheng BTRFS_DATA_RELOC_TREE_OBJECTID) { 33717d217feSYan Zheng set_extent_bits(io_tree, offset, 3380b246afaSJeff Mahoney offset + fs_info->sectorsize - 1, 339ceeb0ae7SDavid Sterba EXTENT_NODATASUM); 34017d217feSYan Zheng } else { 3410b246afaSJeff Mahoney btrfs_info_rl(fs_info, 342efe120a0SFrank Holton "no csum found for inode %llu start %llu", 3434a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(inode)), offset); 34417d217feSYan Zheng } 3456dab8157SChris Mason item = NULL; 346b3b4aa74SDavid Sterba btrfs_release_path(path); 34761b49440SChris Mason goto found; 34861b49440SChris Mason } 34961b49440SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key, 35061b49440SChris Mason path->slots[0]); 35161b49440SChris Mason 35261b49440SChris Mason item_start_offset = found_key.offset; 35361b49440SChris Mason item_size = btrfs_item_size_nr(path->nodes[0], 35461b49440SChris Mason path->slots[0]); 35561b49440SChris Mason item_last_offset = item_start_offset + 356607d432dSJosef Bacik (item_size / csum_size) * 3570b246afaSJeff Mahoney fs_info->sectorsize; 35861b49440SChris Mason item = btrfs_item_ptr(path->nodes[0], path->slots[0], 35961b49440SChris Mason struct btrfs_csum_item); 36061b49440SChris Mason } 36161b49440SChris Mason /* 36261b49440SChris Mason * this byte range must be able to fit inside 36361b49440SChris Mason * a single leaf so it will also fit inside a u32 36461b49440SChris Mason */ 365d20f7043SChris Mason diff = disk_bytenr - item_start_offset; 3660b246afaSJeff Mahoney diff = diff / fs_info->sectorsize; 367607d432dSJosef Bacik diff = diff * csum_size; 368facc8a22SMiao Xie count = min_t(int, nblocks, (item_last_offset - disk_bytenr) >> 369e4100d98SMiao Xie inode->i_sb->s_blocksize_bits); 370facc8a22SMiao Xie read_extent_buffer(path->nodes[0], csum, 3713de9d6b6SChris Mason ((unsigned long)item) + diff, 372e4100d98SMiao Xie csum_size * count); 37361b49440SChris Mason found: 374facc8a22SMiao Xie csum += count * csum_size; 375facc8a22SMiao Xie nblocks -= count; 3764989d277SChristoph Hellwig next: 3774babad10SDavid Sterba while (count > 0) { 3784babad10SDavid Sterba count--; 3790b246afaSJeff Mahoney disk_bytenr += fs_info->sectorsize; 3800b246afaSJeff Mahoney offset += fs_info->sectorsize; 3810b246afaSJeff Mahoney page_bytes_left -= fs_info->sectorsize; 3824989d277SChristoph Hellwig if (!page_bytes_left) 3834989d277SChristoph Hellwig break; /* move to next bio */ 3844989d277SChristoph Hellwig } 3854989d277SChristoph Hellwig } 3864989d277SChristoph Hellwig 387389f239cSChris Mason WARN_ON_ONCE(count); 38861b49440SChris Mason btrfs_free_path(path); 389e62958fcSOmar Sandoval return BLK_STS_OK; 3904b46fce2SJosef Bacik } 3914b46fce2SJosef Bacik 39217d217feSYan Zheng int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, 393a2de733cSArne Jansen struct list_head *list, int search_commit) 39417d217feSYan Zheng { 3950b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 39617d217feSYan Zheng struct btrfs_key key; 39717d217feSYan Zheng struct btrfs_path *path; 39817d217feSYan Zheng struct extent_buffer *leaf; 39917d217feSYan Zheng struct btrfs_ordered_sum *sums; 40017d217feSYan Zheng struct btrfs_csum_item *item; 4010678b618SMark Fasheh LIST_HEAD(tmplist); 40217d217feSYan Zheng unsigned long offset; 40317d217feSYan Zheng int ret; 40417d217feSYan Zheng size_t size; 40517d217feSYan Zheng u64 csum_end; 4060b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 40717d217feSYan Zheng 4080b246afaSJeff Mahoney ASSERT(IS_ALIGNED(start, fs_info->sectorsize) && 4090b246afaSJeff Mahoney IS_ALIGNED(end + 1, fs_info->sectorsize)); 4104277a9c3SJosef Bacik 41117d217feSYan Zheng path = btrfs_alloc_path(); 412d8926bb3SMark Fasheh if (!path) 413d8926bb3SMark Fasheh return -ENOMEM; 41417d217feSYan Zheng 415a2de733cSArne Jansen if (search_commit) { 416a2de733cSArne Jansen path->skip_locking = 1; 417e4058b54SDavid Sterba path->reada = READA_FORWARD; 418a2de733cSArne Jansen path->search_commit_root = 1; 419a2de733cSArne Jansen } 420a2de733cSArne Jansen 42117d217feSYan Zheng key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 42217d217feSYan Zheng key.offset = start; 42317d217feSYan Zheng key.type = BTRFS_EXTENT_CSUM_KEY; 42417d217feSYan Zheng 42507d400a6SYan Zheng ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 42617d217feSYan Zheng if (ret < 0) 42717d217feSYan Zheng goto fail; 42817d217feSYan Zheng if (ret > 0 && path->slots[0] > 0) { 42917d217feSYan Zheng leaf = path->nodes[0]; 43017d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); 43117d217feSYan Zheng if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID && 43217d217feSYan Zheng key.type == BTRFS_EXTENT_CSUM_KEY) { 43317d217feSYan Zheng offset = (start - key.offset) >> 4340b246afaSJeff Mahoney fs_info->sb->s_blocksize_bits; 43517d217feSYan Zheng if (offset * csum_size < 43617d217feSYan Zheng btrfs_item_size_nr(leaf, path->slots[0] - 1)) 43717d217feSYan Zheng path->slots[0]--; 43817d217feSYan Zheng } 43917d217feSYan Zheng } 44017d217feSYan Zheng 44117d217feSYan Zheng while (start <= end) { 44217d217feSYan Zheng leaf = path->nodes[0]; 44317d217feSYan Zheng if (path->slots[0] >= btrfs_header_nritems(leaf)) { 44407d400a6SYan Zheng ret = btrfs_next_leaf(root, path); 44517d217feSYan Zheng if (ret < 0) 44617d217feSYan Zheng goto fail; 44717d217feSYan Zheng if (ret > 0) 44817d217feSYan Zheng break; 44917d217feSYan Zheng leaf = path->nodes[0]; 45017d217feSYan Zheng } 45117d217feSYan Zheng 45217d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 45317d217feSYan Zheng if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 454628c8282SZhi Yong Wu key.type != BTRFS_EXTENT_CSUM_KEY || 455628c8282SZhi Yong Wu key.offset > end) 45617d217feSYan Zheng break; 45717d217feSYan Zheng 45817d217feSYan Zheng if (key.offset > start) 45917d217feSYan Zheng start = key.offset; 46017d217feSYan Zheng 46117d217feSYan Zheng size = btrfs_item_size_nr(leaf, path->slots[0]); 4620b246afaSJeff Mahoney csum_end = key.offset + (size / csum_size) * fs_info->sectorsize; 46387b29b20SYan Zheng if (csum_end <= start) { 46487b29b20SYan Zheng path->slots[0]++; 46587b29b20SYan Zheng continue; 46687b29b20SYan Zheng } 46717d217feSYan Zheng 46807d400a6SYan Zheng csum_end = min(csum_end, end + 1); 46907d400a6SYan Zheng item = btrfs_item_ptr(path->nodes[0], path->slots[0], 47007d400a6SYan Zheng struct btrfs_csum_item); 47107d400a6SYan Zheng while (start < csum_end) { 47207d400a6SYan Zheng size = min_t(size_t, csum_end - start, 4731e25a2e3SJohannes Thumshirn max_ordered_sum_bytes(fs_info, csum_size)); 4740b246afaSJeff Mahoney sums = kzalloc(btrfs_ordered_sum_size(fs_info, size), 47507d400a6SYan Zheng GFP_NOFS); 4760678b618SMark Fasheh if (!sums) { 4770678b618SMark Fasheh ret = -ENOMEM; 4780678b618SMark Fasheh goto fail; 4790678b618SMark Fasheh } 48017d217feSYan Zheng 48117d217feSYan Zheng sums->bytenr = start; 482f51a4a18SMiao Xie sums->len = (int)size; 48317d217feSYan Zheng 48417d217feSYan Zheng offset = (start - key.offset) >> 4850b246afaSJeff Mahoney fs_info->sb->s_blocksize_bits; 48617d217feSYan Zheng offset *= csum_size; 4870b246afaSJeff Mahoney size >>= fs_info->sb->s_blocksize_bits; 48817d217feSYan Zheng 48907d400a6SYan Zheng read_extent_buffer(path->nodes[0], 490f51a4a18SMiao Xie sums->sums, 491f51a4a18SMiao Xie ((unsigned long)item) + offset, 492f51a4a18SMiao Xie csum_size * size); 49317d217feSYan Zheng 4940b246afaSJeff Mahoney start += fs_info->sectorsize * size; 4950678b618SMark Fasheh list_add_tail(&sums->list, &tmplist); 49607d400a6SYan Zheng } 49717d217feSYan Zheng path->slots[0]++; 49817d217feSYan Zheng } 49917d217feSYan Zheng ret = 0; 50017d217feSYan Zheng fail: 5010678b618SMark Fasheh while (ret < 0 && !list_empty(&tmplist)) { 5026e5aafb2SChris Mason sums = list_entry(tmplist.next, struct btrfs_ordered_sum, list); 5030678b618SMark Fasheh list_del(&sums->list); 5040678b618SMark Fasheh kfree(sums); 5050678b618SMark Fasheh } 5060678b618SMark Fasheh list_splice_tail(&tmplist, list); 5070678b618SMark Fasheh 50817d217feSYan Zheng btrfs_free_path(path); 50917d217feSYan Zheng return ret; 51017d217feSYan Zheng } 51117d217feSYan Zheng 51251d470aeSNikolay Borisov /* 51351d470aeSNikolay Borisov * btrfs_csum_one_bio - Calculates checksums of the data contained inside a bio 51451d470aeSNikolay Borisov * @inode: Owner of the data inside the bio 51551d470aeSNikolay Borisov * @bio: Contains the data to be checksummed 51651d470aeSNikolay Borisov * @file_start: offset in file this bio begins to describe 51751d470aeSNikolay Borisov * @contig: Boolean. If true/1 means all bio vecs in this bio are 51851d470aeSNikolay Borisov * contiguous and they begin at @file_start in the file. False/0 51951d470aeSNikolay Borisov * means this bio can contains potentially discontigous bio vecs 52051d470aeSNikolay Borisov * so the logical offset of each should be calculated separately. 52151d470aeSNikolay Borisov */ 5224e4cbee9SChristoph Hellwig blk_status_t btrfs_csum_one_bio(struct inode *inode, struct bio *bio, 5232ff7e61eSJeff Mahoney u64 file_start, int contig) 524e015640fSChris Mason { 5250b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 526d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); 527e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums; 5286cd7ce49SChristoph Hellwig struct btrfs_ordered_extent *ordered = NULL; 529e015640fSChris Mason char *data; 53017347cecSLiu Bo struct bvec_iter iter; 53117347cecSLiu Bo struct bio_vec bvec; 532f51a4a18SMiao Xie int index; 533c40a3d38SChandan Rajendra int nr_sectors; 5343edf7d33SChris Mason unsigned long total_bytes = 0; 5353edf7d33SChris Mason unsigned long this_sum_bytes = 0; 53617347cecSLiu Bo int i; 5373edf7d33SChris Mason u64 offset; 538a3d46aeaSNikolay Borisov unsigned nofs_flag; 5391e25a2e3SJohannes Thumshirn const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 540e015640fSChris Mason 541a3d46aeaSNikolay Borisov nofs_flag = memalloc_nofs_save(); 542a3d46aeaSNikolay Borisov sums = kvzalloc(btrfs_ordered_sum_size(fs_info, bio->bi_iter.bi_size), 543a3d46aeaSNikolay Borisov GFP_KERNEL); 544a3d46aeaSNikolay Borisov memalloc_nofs_restore(nofs_flag); 545a3d46aeaSNikolay Borisov 546e015640fSChris Mason if (!sums) 5474e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 5483edf7d33SChris Mason 5494f024f37SKent Overstreet sums->len = bio->bi_iter.bi_size; 550e6dcd2dcSChris Mason INIT_LIST_HEAD(&sums->list); 551d20f7043SChris Mason 552d20f7043SChris Mason if (contig) 553d20f7043SChris Mason offset = file_start; 554d20f7043SChris Mason else 5556cd7ce49SChristoph Hellwig offset = 0; /* shut up gcc */ 556d20f7043SChris Mason 5574f024f37SKent Overstreet sums->bytenr = (u64)bio->bi_iter.bi_sector << 9; 558f51a4a18SMiao Xie index = 0; 559e015640fSChris Mason 560d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash; 561d5178578SJohannes Thumshirn 56217347cecSLiu Bo bio_for_each_segment(bvec, bio, iter) { 563d20f7043SChris Mason if (!contig) 56417347cecSLiu Bo offset = page_offset(bvec.bv_page) + bvec.bv_offset; 565d20f7043SChris Mason 5666cd7ce49SChristoph Hellwig if (!ordered) { 5676cd7ce49SChristoph Hellwig ordered = btrfs_lookup_ordered_extent(inode, offset); 5686cd7ce49SChristoph Hellwig BUG_ON(!ordered); /* Logic error */ 5696cd7ce49SChristoph Hellwig } 5706cd7ce49SChristoph Hellwig 5710b246afaSJeff Mahoney nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, 57217347cecSLiu Bo bvec.bv_len + fs_info->sectorsize 573c40a3d38SChandan Rajendra - 1); 574c40a3d38SChandan Rajendra 575c40a3d38SChandan Rajendra for (i = 0; i < nr_sectors; i++) { 576bffe633eSOmar Sandoval if (offset >= ordered->file_offset + ordered->num_bytes || 577e58dd74bSJosef Bacik offset < ordered->file_offset) { 5783edf7d33SChris Mason unsigned long bytes_left; 579c40a3d38SChandan Rajendra 5803edf7d33SChris Mason sums->len = this_sum_bytes; 5813edf7d33SChris Mason this_sum_bytes = 0; 582f9756261SNikolay Borisov btrfs_add_ordered_sum(ordered, sums); 5833edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 5843edf7d33SChris Mason 5854f024f37SKent Overstreet bytes_left = bio->bi_iter.bi_size - total_bytes; 5863edf7d33SChris Mason 587a3d46aeaSNikolay Borisov nofs_flag = memalloc_nofs_save(); 588a3d46aeaSNikolay Borisov sums = kvzalloc(btrfs_ordered_sum_size(fs_info, 589a3d46aeaSNikolay Borisov bytes_left), GFP_KERNEL); 590a3d46aeaSNikolay Borisov memalloc_nofs_restore(nofs_flag); 59179787eaaSJeff Mahoney BUG_ON(!sums); /* -ENOMEM */ 5923edf7d33SChris Mason sums->len = bytes_left; 593c40a3d38SChandan Rajendra ordered = btrfs_lookup_ordered_extent(inode, 594c40a3d38SChandan Rajendra offset); 595c40a3d38SChandan Rajendra ASSERT(ordered); /* Logic error */ 596c40a3d38SChandan Rajendra sums->bytenr = ((u64)bio->bi_iter.bi_sector << 9) 597c40a3d38SChandan Rajendra + total_bytes; 598f51a4a18SMiao Xie index = 0; 599c40a3d38SChandan Rajendra } 600c40a3d38SChandan Rajendra 601d5178578SJohannes Thumshirn crypto_shash_init(shash); 602443c8e2aSJohannes Thumshirn data = kmap_atomic(bvec.bv_page); 603d5178578SJohannes Thumshirn crypto_shash_update(shash, data + bvec.bv_offset 6040b246afaSJeff Mahoney + (i * fs_info->sectorsize), 6050b246afaSJeff Mahoney fs_info->sectorsize); 606443c8e2aSJohannes Thumshirn kunmap_atomic(data); 607d5178578SJohannes Thumshirn crypto_shash_final(shash, (char *)(sums->sums + index)); 6081e25a2e3SJohannes Thumshirn index += csum_size; 6090b246afaSJeff Mahoney offset += fs_info->sectorsize; 6100b246afaSJeff Mahoney this_sum_bytes += fs_info->sectorsize; 6110b246afaSJeff Mahoney total_bytes += fs_info->sectorsize; 612c40a3d38SChandan Rajendra } 613c40a3d38SChandan Rajendra 614e015640fSChris Mason } 615ed98b56aSChris Mason this_sum_bytes = 0; 616f9756261SNikolay Borisov btrfs_add_ordered_sum(ordered, sums); 6173edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 618e015640fSChris Mason return 0; 619e015640fSChris Mason } 620e015640fSChris Mason 621459931ecSChris Mason /* 622459931ecSChris Mason * helper function for csum removal, this expects the 623459931ecSChris Mason * key to describe the csum pointed to by the path, and it expects 624459931ecSChris Mason * the csum to overlap the range [bytenr, len] 625459931ecSChris Mason * 626459931ecSChris Mason * The csum should not be entirely contained in the range and the 627459931ecSChris Mason * range should not be entirely contained in the csum. 628459931ecSChris Mason * 629459931ecSChris Mason * This calls btrfs_truncate_item with the correct args based on the 630459931ecSChris Mason * overlap, and fixes up the key as required. 631459931ecSChris Mason */ 6322ff7e61eSJeff Mahoney static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info, 633459931ecSChris Mason struct btrfs_path *path, 634459931ecSChris Mason struct btrfs_key *key, 635459931ecSChris Mason u64 bytenr, u64 len) 636459931ecSChris Mason { 637459931ecSChris Mason struct extent_buffer *leaf; 6380b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 639459931ecSChris Mason u64 csum_end; 640459931ecSChris Mason u64 end_byte = bytenr + len; 6410b246afaSJeff Mahoney u32 blocksize_bits = fs_info->sb->s_blocksize_bits; 642459931ecSChris Mason 643459931ecSChris Mason leaf = path->nodes[0]; 644459931ecSChris Mason csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; 6450b246afaSJeff Mahoney csum_end <<= fs_info->sb->s_blocksize_bits; 646459931ecSChris Mason csum_end += key->offset; 647459931ecSChris Mason 648459931ecSChris Mason if (key->offset < bytenr && csum_end <= end_byte) { 649459931ecSChris Mason /* 650459931ecSChris Mason * [ bytenr - len ] 651459931ecSChris Mason * [ ] 652459931ecSChris Mason * [csum ] 653459931ecSChris Mason * A simple truncate off the end of the item 654459931ecSChris Mason */ 655459931ecSChris Mason u32 new_size = (bytenr - key->offset) >> blocksize_bits; 656459931ecSChris Mason new_size *= csum_size; 65778ac4f9eSDavid Sterba btrfs_truncate_item(path, new_size, 1); 658459931ecSChris Mason } else if (key->offset >= bytenr && csum_end > end_byte && 659459931ecSChris Mason end_byte > key->offset) { 660459931ecSChris Mason /* 661459931ecSChris Mason * [ bytenr - len ] 662459931ecSChris Mason * [ ] 663459931ecSChris Mason * [csum ] 664459931ecSChris Mason * we need to truncate from the beginning of the csum 665459931ecSChris Mason */ 666459931ecSChris Mason u32 new_size = (csum_end - end_byte) >> blocksize_bits; 667459931ecSChris Mason new_size *= csum_size; 668459931ecSChris Mason 66978ac4f9eSDavid Sterba btrfs_truncate_item(path, new_size, 0); 670459931ecSChris Mason 671459931ecSChris Mason key->offset = end_byte; 6720b246afaSJeff Mahoney btrfs_set_item_key_safe(fs_info, path, key); 673459931ecSChris Mason } else { 674459931ecSChris Mason BUG(); 675459931ecSChris Mason } 676459931ecSChris Mason } 677459931ecSChris Mason 678459931ecSChris Mason /* 679459931ecSChris Mason * deletes the csum items from the csum tree for a given 680459931ecSChris Mason * range of bytes. 681459931ecSChris Mason */ 682459931ecSChris Mason int btrfs_del_csums(struct btrfs_trans_handle *trans, 68340e046acSFilipe Manana struct btrfs_root *root, u64 bytenr, u64 len) 684459931ecSChris Mason { 68540e046acSFilipe Manana struct btrfs_fs_info *fs_info = trans->fs_info; 686459931ecSChris Mason struct btrfs_path *path; 687459931ecSChris Mason struct btrfs_key key; 688459931ecSChris Mason u64 end_byte = bytenr + len; 689459931ecSChris Mason u64 csum_end; 690459931ecSChris Mason struct extent_buffer *leaf; 691459931ecSChris Mason int ret; 6920b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 6930b246afaSJeff Mahoney int blocksize_bits = fs_info->sb->s_blocksize_bits; 694459931ecSChris Mason 69540e046acSFilipe Manana ASSERT(root == fs_info->csum_root || 69640e046acSFilipe Manana root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); 69740e046acSFilipe Manana 698459931ecSChris Mason path = btrfs_alloc_path(); 6992a29edc6Sliubo if (!path) 7002a29edc6Sliubo return -ENOMEM; 701459931ecSChris Mason 702459931ecSChris Mason while (1) { 703459931ecSChris Mason key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 704459931ecSChris Mason key.offset = end_byte - 1; 705459931ecSChris Mason key.type = BTRFS_EXTENT_CSUM_KEY; 706459931ecSChris Mason 707b9473439SChris Mason path->leave_spinning = 1; 708459931ecSChris Mason ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 709459931ecSChris Mason if (ret > 0) { 710459931ecSChris Mason if (path->slots[0] == 0) 71165a246c5STsutomu Itoh break; 712459931ecSChris Mason path->slots[0]--; 713ad0397a7SJosef Bacik } else if (ret < 0) { 71465a246c5STsutomu Itoh break; 715459931ecSChris Mason } 716ad0397a7SJosef Bacik 717459931ecSChris Mason leaf = path->nodes[0]; 718459931ecSChris Mason btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 719459931ecSChris Mason 720459931ecSChris Mason if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 721459931ecSChris Mason key.type != BTRFS_EXTENT_CSUM_KEY) { 722459931ecSChris Mason break; 723459931ecSChris Mason } 724459931ecSChris Mason 725459931ecSChris Mason if (key.offset >= end_byte) 726459931ecSChris Mason break; 727459931ecSChris Mason 728459931ecSChris Mason csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; 729459931ecSChris Mason csum_end <<= blocksize_bits; 730459931ecSChris Mason csum_end += key.offset; 731459931ecSChris Mason 732459931ecSChris Mason /* this csum ends before we start, we're done */ 733459931ecSChris Mason if (csum_end <= bytenr) 734459931ecSChris Mason break; 735459931ecSChris Mason 736459931ecSChris Mason /* delete the entire item, it is inside our range */ 737459931ecSChris Mason if (key.offset >= bytenr && csum_end <= end_byte) { 7386f546216SFilipe Manana int del_nr = 1; 7396f546216SFilipe Manana 7406f546216SFilipe Manana /* 7416f546216SFilipe Manana * Check how many csum items preceding this one in this 7426f546216SFilipe Manana * leaf correspond to our range and then delete them all 7436f546216SFilipe Manana * at once. 7446f546216SFilipe Manana */ 7456f546216SFilipe Manana if (key.offset > bytenr && path->slots[0] > 0) { 7466f546216SFilipe Manana int slot = path->slots[0] - 1; 7476f546216SFilipe Manana 7486f546216SFilipe Manana while (slot >= 0) { 7496f546216SFilipe Manana struct btrfs_key pk; 7506f546216SFilipe Manana 7516f546216SFilipe Manana btrfs_item_key_to_cpu(leaf, &pk, slot); 7526f546216SFilipe Manana if (pk.offset < bytenr || 7536f546216SFilipe Manana pk.type != BTRFS_EXTENT_CSUM_KEY || 7546f546216SFilipe Manana pk.objectid != 7556f546216SFilipe Manana BTRFS_EXTENT_CSUM_OBJECTID) 7566f546216SFilipe Manana break; 7576f546216SFilipe Manana path->slots[0] = slot; 7586f546216SFilipe Manana del_nr++; 7596f546216SFilipe Manana key.offset = pk.offset; 7606f546216SFilipe Manana slot--; 7616f546216SFilipe Manana } 7626f546216SFilipe Manana } 7636f546216SFilipe Manana ret = btrfs_del_items(trans, root, path, 7646f546216SFilipe Manana path->slots[0], del_nr); 76565a246c5STsutomu Itoh if (ret) 76665a246c5STsutomu Itoh goto out; 767dcbdd4dcSChris Mason if (key.offset == bytenr) 768dcbdd4dcSChris Mason break; 769459931ecSChris Mason } else if (key.offset < bytenr && csum_end > end_byte) { 770459931ecSChris Mason unsigned long offset; 771459931ecSChris Mason unsigned long shift_len; 772459931ecSChris Mason unsigned long item_offset; 773459931ecSChris Mason /* 774459931ecSChris Mason * [ bytenr - len ] 775459931ecSChris Mason * [csum ] 776459931ecSChris Mason * 777459931ecSChris Mason * Our bytes are in the middle of the csum, 778459931ecSChris Mason * we need to split this item and insert a new one. 779459931ecSChris Mason * 780459931ecSChris Mason * But we can't drop the path because the 781459931ecSChris Mason * csum could change, get removed, extended etc. 782459931ecSChris Mason * 783459931ecSChris Mason * The trick here is the max size of a csum item leaves 784459931ecSChris Mason * enough room in the tree block for a single 785459931ecSChris Mason * item header. So, we split the item in place, 786459931ecSChris Mason * adding a new header pointing to the existing 787459931ecSChris Mason * bytes. Then we loop around again and we have 788459931ecSChris Mason * a nicely formed csum item that we can neatly 789459931ecSChris Mason * truncate. 790459931ecSChris Mason */ 791459931ecSChris Mason offset = (bytenr - key.offset) >> blocksize_bits; 792459931ecSChris Mason offset *= csum_size; 793459931ecSChris Mason 794459931ecSChris Mason shift_len = (len >> blocksize_bits) * csum_size; 795459931ecSChris Mason 796459931ecSChris Mason item_offset = btrfs_item_ptr_offset(leaf, 797459931ecSChris Mason path->slots[0]); 798459931ecSChris Mason 799b159fa28SDavid Sterba memzero_extent_buffer(leaf, item_offset + offset, 800459931ecSChris Mason shift_len); 801459931ecSChris Mason key.offset = bytenr; 802459931ecSChris Mason 803459931ecSChris Mason /* 804459931ecSChris Mason * btrfs_split_item returns -EAGAIN when the 805459931ecSChris Mason * item changed size or key 806459931ecSChris Mason */ 807459931ecSChris Mason ret = btrfs_split_item(trans, root, path, &key, offset); 80879787eaaSJeff Mahoney if (ret && ret != -EAGAIN) { 80966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 81079787eaaSJeff Mahoney goto out; 81179787eaaSJeff Mahoney } 812459931ecSChris Mason 813459931ecSChris Mason key.offset = end_byte - 1; 814459931ecSChris Mason } else { 8152ff7e61eSJeff Mahoney truncate_one_csum(fs_info, path, &key, bytenr, len); 816dcbdd4dcSChris Mason if (key.offset < bytenr) 817dcbdd4dcSChris Mason break; 818459931ecSChris Mason } 819b3b4aa74SDavid Sterba btrfs_release_path(path); 820459931ecSChris Mason } 82165a246c5STsutomu Itoh ret = 0; 822459931ecSChris Mason out: 823459931ecSChris Mason btrfs_free_path(path); 82465a246c5STsutomu Itoh return ret; 825459931ecSChris Mason } 826459931ecSChris Mason 827065631f6SChris Mason int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, 828d20f7043SChris Mason struct btrfs_root *root, 829e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums) 830f254e52cSChris Mason { 8310b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 832f254e52cSChris Mason struct btrfs_key file_key; 8336567e837SChris Mason struct btrfs_key found_key; 8345caf2a00SChris Mason struct btrfs_path *path; 835f254e52cSChris Mason struct btrfs_csum_item *item; 836065631f6SChris Mason struct btrfs_csum_item *item_end; 837ff79f819SChris Mason struct extent_buffer *leaf = NULL; 838f51a4a18SMiao Xie u64 next_offset; 839f51a4a18SMiao Xie u64 total_bytes = 0; 8406567e837SChris Mason u64 csum_offset; 841f51a4a18SMiao Xie u64 bytenr; 842f578d4bdSChris Mason u32 nritems; 843f578d4bdSChris Mason u32 ins_size; 844f51a4a18SMiao Xie int index = 0; 845f51a4a18SMiao Xie int found_next; 846f51a4a18SMiao Xie int ret; 8470b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 8486e92f5e6SChris Mason 8495caf2a00SChris Mason path = btrfs_alloc_path(); 850d8926bb3SMark Fasheh if (!path) 851d8926bb3SMark Fasheh return -ENOMEM; 852065631f6SChris Mason again: 853065631f6SChris Mason next_offset = (u64)-1; 854065631f6SChris Mason found_next = 0; 855f51a4a18SMiao Xie bytenr = sums->bytenr + total_bytes; 856d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 857f51a4a18SMiao Xie file_key.offset = bytenr; 858962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_CSUM_KEY; 859a429e513SChris Mason 860f51a4a18SMiao Xie item = btrfs_lookup_csum(trans, root, path, bytenr, 1); 861ff79f819SChris Mason if (!IS_ERR(item)) { 862639cb586SChris Mason ret = 0; 863f51a4a18SMiao Xie leaf = path->nodes[0]; 864f51a4a18SMiao Xie item_end = btrfs_item_ptr(leaf, path->slots[0], 865f51a4a18SMiao Xie struct btrfs_csum_item); 866f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((char *)item_end + 867f51a4a18SMiao Xie btrfs_item_size_nr(leaf, path->slots[0])); 868a429e513SChris Mason goto found; 869ff79f819SChris Mason } 870a429e513SChris Mason ret = PTR_ERR(item); 8714a500fd1SYan, Zheng if (ret != -EFBIG && ret != -ENOENT) 8724a500fd1SYan, Zheng goto fail_unlock; 8734a500fd1SYan, Zheng 874a429e513SChris Mason if (ret == -EFBIG) { 875a429e513SChris Mason u32 item_size; 876a429e513SChris Mason /* we found one, but it isn't big enough yet */ 8775f39d397SChris Mason leaf = path->nodes[0]; 8785f39d397SChris Mason item_size = btrfs_item_size_nr(leaf, path->slots[0]); 879607d432dSJosef Bacik if ((item_size / csum_size) >= 8800b246afaSJeff Mahoney MAX_CSUM_ITEMS(fs_info, csum_size)) { 881a429e513SChris Mason /* already at max size, make a new one */ 882a429e513SChris Mason goto insert; 883a429e513SChris Mason } 884a429e513SChris Mason } else { 885f578d4bdSChris Mason int slot = path->slots[0] + 1; 886a429e513SChris Mason /* we didn't find a csum item, insert one */ 887f578d4bdSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 88835045bf2SFilipe Manana if (!nritems || (path->slots[0] >= nritems - 1)) { 889f578d4bdSChris Mason ret = btrfs_next_leaf(root, path); 890b56baf5bSYan if (ret == 1) 891f578d4bdSChris Mason found_next = 1; 892b56baf5bSYan if (ret != 0) 893f578d4bdSChris Mason goto insert; 89427b9a812SFilipe Manana slot = path->slots[0]; 895f578d4bdSChris Mason } 896f578d4bdSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); 897d20f7043SChris Mason if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 898d20f7043SChris Mason found_key.type != BTRFS_EXTENT_CSUM_KEY) { 899f578d4bdSChris Mason found_next = 1; 900f578d4bdSChris Mason goto insert; 901f578d4bdSChris Mason } 902f578d4bdSChris Mason next_offset = found_key.offset; 903f578d4bdSChris Mason found_next = 1; 904a429e513SChris Mason goto insert; 905a429e513SChris Mason } 906a429e513SChris Mason 907a429e513SChris Mason /* 908a429e513SChris Mason * at this point, we know the tree has an item, but it isn't big 909a429e513SChris Mason * enough yet to put our csum in. Grow it 910a429e513SChris Mason */ 911b3b4aa74SDavid Sterba btrfs_release_path(path); 9126567e837SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 913607d432dSJosef Bacik csum_size, 1); 9146567e837SChris Mason if (ret < 0) 91553863232SChris Mason goto fail_unlock; 916459931ecSChris Mason 917459931ecSChris Mason if (ret > 0) { 918459931ecSChris Mason if (path->slots[0] == 0) 9196567e837SChris Mason goto insert; 9206567e837SChris Mason path->slots[0]--; 921459931ecSChris Mason } 922459931ecSChris Mason 9235f39d397SChris Mason leaf = path->nodes[0]; 9245f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 925d20f7043SChris Mason csum_offset = (bytenr - found_key.offset) >> 9260b246afaSJeff Mahoney fs_info->sb->s_blocksize_bits; 927459931ecSChris Mason 928962a298fSDavid Sterba if (found_key.type != BTRFS_EXTENT_CSUM_KEY || 929d20f7043SChris Mason found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 9300b246afaSJeff Mahoney csum_offset >= MAX_CSUM_ITEMS(fs_info, csum_size)) { 9316567e837SChris Mason goto insert; 9326567e837SChris Mason } 933459931ecSChris Mason 9342f697dc6SLiu Bo if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) / 935607d432dSJosef Bacik csum_size) { 9362f697dc6SLiu Bo int extend_nr; 9372f697dc6SLiu Bo u64 tmp; 9382f697dc6SLiu Bo u32 diff; 9392f697dc6SLiu Bo u32 free_space; 940459931ecSChris Mason 941e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < 9422f697dc6SLiu Bo sizeof(struct btrfs_item) + csum_size * 2) 9432f697dc6SLiu Bo goto insert; 9442f697dc6SLiu Bo 945e902baacSDavid Sterba free_space = btrfs_leaf_free_space(leaf) - 9462f697dc6SLiu Bo sizeof(struct btrfs_item) - csum_size; 947f51a4a18SMiao Xie tmp = sums->len - total_bytes; 9480b246afaSJeff Mahoney tmp >>= fs_info->sb->s_blocksize_bits; 9492f697dc6SLiu Bo WARN_ON(tmp < 1); 9502f697dc6SLiu Bo 9512f697dc6SLiu Bo extend_nr = max_t(int, 1, (int)tmp); 9522f697dc6SLiu Bo diff = (csum_offset + extend_nr) * csum_size; 9530b246afaSJeff Mahoney diff = min(diff, 9540b246afaSJeff Mahoney MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size); 955459931ecSChris Mason 9565f39d397SChris Mason diff = diff - btrfs_item_size_nr(leaf, path->slots[0]); 9572f697dc6SLiu Bo diff = min(free_space, diff); 9582f697dc6SLiu Bo diff /= csum_size; 9592f697dc6SLiu Bo diff *= csum_size; 960459931ecSChris Mason 961c71dd880SDavid Sterba btrfs_extend_item(path, diff); 962f51a4a18SMiao Xie ret = 0; 9636567e837SChris Mason goto csum; 9646567e837SChris Mason } 9656567e837SChris Mason 9666567e837SChris Mason insert: 967b3b4aa74SDavid Sterba btrfs_release_path(path); 9686567e837SChris Mason csum_offset = 0; 969f578d4bdSChris Mason if (found_next) { 9702f697dc6SLiu Bo u64 tmp; 971d20f7043SChris Mason 972f51a4a18SMiao Xie tmp = sums->len - total_bytes; 9730b246afaSJeff Mahoney tmp >>= fs_info->sb->s_blocksize_bits; 9742f697dc6SLiu Bo tmp = min(tmp, (next_offset - file_key.offset) >> 9750b246afaSJeff Mahoney fs_info->sb->s_blocksize_bits); 9762f697dc6SLiu Bo 97750d0446eSSeraphime Kirkovski tmp = max_t(u64, 1, tmp); 97850d0446eSSeraphime Kirkovski tmp = min_t(u64, tmp, MAX_CSUM_ITEMS(fs_info, csum_size)); 979607d432dSJosef Bacik ins_size = csum_size * tmp; 980f578d4bdSChris Mason } else { 981607d432dSJosef Bacik ins_size = csum_size; 982f578d4bdSChris Mason } 983b9473439SChris Mason path->leave_spinning = 1; 9845caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 985f578d4bdSChris Mason ins_size); 986b9473439SChris Mason path->leave_spinning = 0; 98754aa1f4dSChris Mason if (ret < 0) 98853863232SChris Mason goto fail_unlock; 989fae7f21cSDulshani Gunawardhana if (WARN_ON(ret != 0)) 99053863232SChris Mason goto fail_unlock; 9915f39d397SChris Mason leaf = path->nodes[0]; 992f51a4a18SMiao Xie csum: 9935f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 994f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((unsigned char *)item + 995f51a4a18SMiao Xie btrfs_item_size_nr(leaf, path->slots[0])); 996509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 997607d432dSJosef Bacik csum_offset * csum_size); 998b18c6685SChris Mason found: 999f51a4a18SMiao Xie ins_size = (u32)(sums->len - total_bytes) >> 10000b246afaSJeff Mahoney fs_info->sb->s_blocksize_bits; 1001f51a4a18SMiao Xie ins_size *= csum_size; 1002f51a4a18SMiao Xie ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item, 1003f51a4a18SMiao Xie ins_size); 1004f51a4a18SMiao Xie write_extent_buffer(leaf, sums->sums + index, (unsigned long)item, 1005f51a4a18SMiao Xie ins_size); 1006aadfeb6eSChris Mason 10071e25a2e3SJohannes Thumshirn index += ins_size; 1008f51a4a18SMiao Xie ins_size /= csum_size; 10090b246afaSJeff Mahoney total_bytes += ins_size * fs_info->sectorsize; 1010a6591715SChris Mason 10115caf2a00SChris Mason btrfs_mark_buffer_dirty(path->nodes[0]); 1012e6dcd2dcSChris Mason if (total_bytes < sums->len) { 1013b3b4aa74SDavid Sterba btrfs_release_path(path); 1014b9473439SChris Mason cond_resched(); 1015065631f6SChris Mason goto again; 1016065631f6SChris Mason } 101753863232SChris Mason out: 10185caf2a00SChris Mason btrfs_free_path(path); 1019f254e52cSChris Mason return ret; 102053863232SChris Mason 102153863232SChris Mason fail_unlock: 102253863232SChris Mason goto out; 1023f254e52cSChris Mason } 10247ffbb598SFilipe Manana 10259cdc5124SNikolay Borisov void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, 10267ffbb598SFilipe Manana const struct btrfs_path *path, 10277ffbb598SFilipe Manana struct btrfs_file_extent_item *fi, 10287ffbb598SFilipe Manana const bool new_inline, 10297ffbb598SFilipe Manana struct extent_map *em) 10307ffbb598SFilipe Manana { 10313ffbd68cSDavid Sterba struct btrfs_fs_info *fs_info = inode->root->fs_info; 10329cdc5124SNikolay Borisov struct btrfs_root *root = inode->root; 10337ffbb598SFilipe Manana struct extent_buffer *leaf = path->nodes[0]; 10347ffbb598SFilipe Manana const int slot = path->slots[0]; 10357ffbb598SFilipe Manana struct btrfs_key key; 10367ffbb598SFilipe Manana u64 extent_start, extent_end; 10377ffbb598SFilipe Manana u64 bytenr; 10387ffbb598SFilipe Manana u8 type = btrfs_file_extent_type(leaf, fi); 10397ffbb598SFilipe Manana int compress_type = btrfs_file_extent_compression(leaf, fi); 10407ffbb598SFilipe Manana 10417ffbb598SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot); 10427ffbb598SFilipe Manana extent_start = key.offset; 1043*a5eeb3d1SFilipe Manana extent_end = btrfs_file_extent_end(path); 10447ffbb598SFilipe Manana em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); 10457ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_REG || 10467ffbb598SFilipe Manana type == BTRFS_FILE_EXTENT_PREALLOC) { 10477ffbb598SFilipe Manana em->start = extent_start; 10487ffbb598SFilipe Manana em->len = extent_end - extent_start; 10497ffbb598SFilipe Manana em->orig_start = extent_start - 10507ffbb598SFilipe Manana btrfs_file_extent_offset(leaf, fi); 10517ffbb598SFilipe Manana em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); 10527ffbb598SFilipe Manana bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); 10537ffbb598SFilipe Manana if (bytenr == 0) { 10547ffbb598SFilipe Manana em->block_start = EXTENT_MAP_HOLE; 10557ffbb598SFilipe Manana return; 10567ffbb598SFilipe Manana } 10577ffbb598SFilipe Manana if (compress_type != BTRFS_COMPRESS_NONE) { 10587ffbb598SFilipe Manana set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 10597ffbb598SFilipe Manana em->compress_type = compress_type; 10607ffbb598SFilipe Manana em->block_start = bytenr; 10617ffbb598SFilipe Manana em->block_len = em->orig_block_len; 10627ffbb598SFilipe Manana } else { 10637ffbb598SFilipe Manana bytenr += btrfs_file_extent_offset(leaf, fi); 10647ffbb598SFilipe Manana em->block_start = bytenr; 10657ffbb598SFilipe Manana em->block_len = em->len; 10667ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_PREALLOC) 10677ffbb598SFilipe Manana set_bit(EXTENT_FLAG_PREALLOC, &em->flags); 10687ffbb598SFilipe Manana } 10697ffbb598SFilipe Manana } else if (type == BTRFS_FILE_EXTENT_INLINE) { 10707ffbb598SFilipe Manana em->block_start = EXTENT_MAP_INLINE; 10717ffbb598SFilipe Manana em->start = extent_start; 10727ffbb598SFilipe Manana em->len = extent_end - extent_start; 10737ffbb598SFilipe Manana /* 10747ffbb598SFilipe Manana * Initialize orig_start and block_len with the same values 10757ffbb598SFilipe Manana * as in inode.c:btrfs_get_extent(). 10767ffbb598SFilipe Manana */ 10777ffbb598SFilipe Manana em->orig_start = EXTENT_MAP_HOLE; 10787ffbb598SFilipe Manana em->block_len = (u64)-1; 10797ffbb598SFilipe Manana if (!new_inline && compress_type != BTRFS_COMPRESS_NONE) { 10807ffbb598SFilipe Manana set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 10817ffbb598SFilipe Manana em->compress_type = compress_type; 10827ffbb598SFilipe Manana } 10837ffbb598SFilipe Manana } else { 10840b246afaSJeff Mahoney btrfs_err(fs_info, 10859cdc5124SNikolay Borisov "unknown file extent item type %d, inode %llu, offset %llu, " 10869cdc5124SNikolay Borisov "root %llu", type, btrfs_ino(inode), extent_start, 10877ffbb598SFilipe Manana root->root_key.objectid); 10887ffbb598SFilipe Manana } 10897ffbb598SFilipe Manana } 1090*a5eeb3d1SFilipe Manana 1091*a5eeb3d1SFilipe Manana /* 1092*a5eeb3d1SFilipe Manana * Returns the end offset (non inclusive) of the file extent item the given path 1093*a5eeb3d1SFilipe Manana * points to. If it points to an inline extent, the returned offset is rounded 1094*a5eeb3d1SFilipe Manana * up to the sector size. 1095*a5eeb3d1SFilipe Manana */ 1096*a5eeb3d1SFilipe Manana u64 btrfs_file_extent_end(const struct btrfs_path *path) 1097*a5eeb3d1SFilipe Manana { 1098*a5eeb3d1SFilipe Manana const struct extent_buffer *leaf = path->nodes[0]; 1099*a5eeb3d1SFilipe Manana const int slot = path->slots[0]; 1100*a5eeb3d1SFilipe Manana struct btrfs_file_extent_item *fi; 1101*a5eeb3d1SFilipe Manana struct btrfs_key key; 1102*a5eeb3d1SFilipe Manana u64 end; 1103*a5eeb3d1SFilipe Manana 1104*a5eeb3d1SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot); 1105*a5eeb3d1SFilipe Manana ASSERT(key.type == BTRFS_EXTENT_DATA_KEY); 1106*a5eeb3d1SFilipe Manana fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); 1107*a5eeb3d1SFilipe Manana 1108*a5eeb3d1SFilipe Manana if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) { 1109*a5eeb3d1SFilipe Manana end = btrfs_file_extent_ram_bytes(leaf, fi); 1110*a5eeb3d1SFilipe Manana end = ALIGN(key.offset + end, leaf->fs_info->sectorsize); 1111*a5eeb3d1SFilipe Manana } else { 1112*a5eeb3d1SFilipe Manana end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); 1113*a5eeb3d1SFilipe Manana } 1114*a5eeb3d1SFilipe Manana 1115*a5eeb3d1SFilipe Manana return end; 1116*a5eeb3d1SFilipe Manana } 1117