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> 111e1d2701SChris Mason #include "ctree.h" 12dee26a9fSChris Mason #include "disk-io.h" 139f5fae2fSChris Mason #include "transaction.h" 14facc8a22SMiao Xie #include "volumes.h" 151de037a4SChris Mason #include "print-tree.h" 16ebb8765bSAnand Jain #include "compression.h" 171e1d2701SChris Mason 1842049bf6SChris Mason #define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \ 1942049bf6SChris Mason sizeof(struct btrfs_item) * 2) / \ 2042049bf6SChris Mason size) - 1)) 2107d400a6SYan Zheng 22221b8318SZach Brown #define MAX_CSUM_ITEMS(r, size) (min_t(u32, __MAX_CSUM_ITEMS(r, size), \ 2309cbfeafSKirill A. Shutemov PAGE_SIZE)) 247ca4be45SChris Mason 25da17066cSJeff Mahoney #define MAX_ORDERED_SUM_BYTES(fs_info) ((PAGE_SIZE - \ 2607d400a6SYan Zheng sizeof(struct btrfs_ordered_sum)) / \ 27da17066cSJeff Mahoney sizeof(u32) * (fs_info)->sectorsize) 2807d400a6SYan Zheng 29b18c6685SChris Mason int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, 30dee26a9fSChris Mason struct btrfs_root *root, 31b18c6685SChris Mason u64 objectid, u64 pos, 32f2eb0a24SSage Weil u64 disk_offset, u64 disk_num_bytes, 33c8b97818SChris Mason u64 num_bytes, u64 offset, u64 ram_bytes, 34c8b97818SChris Mason u8 compression, u8 encryption, u16 other_encoding) 359f5fae2fSChris Mason { 36dee26a9fSChris Mason int ret = 0; 37dee26a9fSChris Mason struct btrfs_file_extent_item *item; 38dee26a9fSChris Mason struct btrfs_key file_key; 395caf2a00SChris Mason struct btrfs_path *path; 405f39d397SChris Mason struct extent_buffer *leaf; 41dee26a9fSChris Mason 425caf2a00SChris Mason path = btrfs_alloc_path(); 43db5b493aSTsutomu Itoh if (!path) 44db5b493aSTsutomu Itoh return -ENOMEM; 45dee26a9fSChris Mason file_key.objectid = objectid; 46b18c6685SChris Mason file_key.offset = pos; 47962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_DATA_KEY; 48dee26a9fSChris Mason 49b9473439SChris Mason path->leave_spinning = 1; 505caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 51dee26a9fSChris Mason sizeof(*item)); 5254aa1f4dSChris Mason if (ret < 0) 5354aa1f4dSChris Mason goto out; 5479787eaaSJeff Mahoney BUG_ON(ret); /* Can't happen */ 555f39d397SChris Mason leaf = path->nodes[0]; 565f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], 57dee26a9fSChris Mason struct btrfs_file_extent_item); 58f2eb0a24SSage Weil btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset); 59db94535dSChris Mason btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes); 60f2eb0a24SSage Weil btrfs_set_file_extent_offset(leaf, item, offset); 61db94535dSChris Mason btrfs_set_file_extent_num_bytes(leaf, item, num_bytes); 62c8b97818SChris Mason btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes); 635f39d397SChris Mason btrfs_set_file_extent_generation(leaf, item, trans->transid); 645f39d397SChris Mason btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); 65c8b97818SChris Mason btrfs_set_file_extent_compression(leaf, item, compression); 66c8b97818SChris Mason btrfs_set_file_extent_encryption(leaf, item, encryption); 67c8b97818SChris Mason btrfs_set_file_extent_other_encoding(leaf, item, other_encoding); 68c8b97818SChris Mason 695f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 7054aa1f4dSChris Mason out: 715caf2a00SChris Mason btrfs_free_path(path); 7254aa1f4dSChris Mason return ret; 739f5fae2fSChris Mason } 74dee26a9fSChris Mason 7548a3b636SEric Sandeen static struct btrfs_csum_item * 7648a3b636SEric Sandeen btrfs_lookup_csum(struct btrfs_trans_handle *trans, 77b18c6685SChris Mason struct btrfs_root *root, 786567e837SChris Mason struct btrfs_path *path, 79d20f7043SChris Mason u64 bytenr, int cow) 806567e837SChris Mason { 810b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 826567e837SChris Mason int ret; 836567e837SChris Mason struct btrfs_key file_key; 846567e837SChris Mason struct btrfs_key found_key; 856567e837SChris Mason struct btrfs_csum_item *item; 865f39d397SChris Mason struct extent_buffer *leaf; 876567e837SChris Mason u64 csum_offset = 0; 880b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 89a429e513SChris Mason int csums_in_item; 906567e837SChris Mason 91d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 92d20f7043SChris Mason file_key.offset = bytenr; 93962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_CSUM_KEY; 94b18c6685SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); 956567e837SChris Mason if (ret < 0) 966567e837SChris Mason goto fail; 975f39d397SChris Mason leaf = path->nodes[0]; 986567e837SChris Mason if (ret > 0) { 996567e837SChris Mason ret = 1; 10070b2befdSChris Mason if (path->slots[0] == 0) 1016567e837SChris Mason goto fail; 1026567e837SChris Mason path->slots[0]--; 1035f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 104962a298fSDavid Sterba if (found_key.type != BTRFS_EXTENT_CSUM_KEY) 1056567e837SChris Mason goto fail; 106d20f7043SChris Mason 107d20f7043SChris Mason csum_offset = (bytenr - found_key.offset) >> 1080b246afaSJeff Mahoney fs_info->sb->s_blocksize_bits; 1095f39d397SChris Mason csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]); 110607d432dSJosef Bacik csums_in_item /= csum_size; 111a429e513SChris Mason 11282d130ffSMiao Xie if (csum_offset == csums_in_item) { 113a429e513SChris Mason ret = -EFBIG; 1146567e837SChris Mason goto fail; 11582d130ffSMiao Xie } else if (csum_offset > csums_in_item) { 11682d130ffSMiao Xie goto fail; 1176567e837SChris Mason } 1186567e837SChris Mason } 1196567e837SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 120509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 121607d432dSJosef Bacik csum_offset * csum_size); 1226567e837SChris Mason return item; 1236567e837SChris Mason fail: 1246567e837SChris Mason if (ret > 0) 125b18c6685SChris Mason ret = -ENOENT; 1266567e837SChris Mason return ERR_PTR(ret); 1276567e837SChris Mason } 1286567e837SChris Mason 129dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, 130dee26a9fSChris Mason struct btrfs_root *root, 131dee26a9fSChris Mason struct btrfs_path *path, u64 objectid, 1329773a788SChris Mason u64 offset, int mod) 133dee26a9fSChris Mason { 134dee26a9fSChris Mason int ret; 135dee26a9fSChris Mason struct btrfs_key file_key; 136dee26a9fSChris Mason int ins_len = mod < 0 ? -1 : 0; 137dee26a9fSChris Mason int cow = mod != 0; 138dee26a9fSChris Mason 139dee26a9fSChris Mason file_key.objectid = objectid; 14070b2befdSChris Mason file_key.offset = offset; 141962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_DATA_KEY; 142dee26a9fSChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); 143dee26a9fSChris Mason return ret; 144dee26a9fSChris Mason } 145f254e52cSChris Mason 1464e4cbee9SChristoph Hellwig static blk_status_t __btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, 1474b46fce2SJosef Bacik u64 logical_offset, u32 *dst, int dio) 14861b49440SChris Mason { 1490b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 15017347cecSLiu Bo struct bio_vec bvec; 15117347cecSLiu Bo struct bvec_iter iter; 152facc8a22SMiao Xie struct btrfs_io_bio *btrfs_bio = btrfs_io_bio(bio); 153facc8a22SMiao Xie struct btrfs_csum_item *item = NULL; 154facc8a22SMiao Xie struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 155facc8a22SMiao Xie struct btrfs_path *path; 156facc8a22SMiao Xie u8 *csum; 1574b46fce2SJosef Bacik u64 offset = 0; 15861b49440SChris Mason u64 item_start_offset = 0; 15961b49440SChris Mason u64 item_last_offset = 0; 160d20f7043SChris Mason u64 disk_bytenr; 161c40a3d38SChandan Rajendra u64 page_bytes_left; 16261b49440SChris Mason u32 diff; 163facc8a22SMiao Xie int nblocks; 16417347cecSLiu Bo int count = 0; 1650b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 16661b49440SChris Mason 16761b49440SChris Mason path = btrfs_alloc_path(); 168c2db1073STsutomu Itoh if (!path) 1694e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 170facc8a22SMiao Xie 1714f024f37SKent Overstreet nblocks = bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits; 172facc8a22SMiao Xie if (!dst) { 173facc8a22SMiao Xie if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) { 17431fecccbSDavid Sterba btrfs_bio->csum = kmalloc_array(nblocks, csum_size, 17531fecccbSDavid Sterba GFP_NOFS); 17631fecccbSDavid Sterba if (!btrfs_bio->csum) { 177facc8a22SMiao Xie btrfs_free_path(path); 1784e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 179facc8a22SMiao Xie } 180facc8a22SMiao Xie } else { 181facc8a22SMiao Xie btrfs_bio->csum = btrfs_bio->csum_inline; 182facc8a22SMiao Xie } 183facc8a22SMiao Xie csum = btrfs_bio->csum; 184facc8a22SMiao Xie } else { 185facc8a22SMiao Xie csum = (u8 *)dst; 186facc8a22SMiao Xie } 187facc8a22SMiao Xie 18809cbfeafSKirill A. Shutemov if (bio->bi_iter.bi_size > PAGE_SIZE * 8) 189e4058b54SDavid Sterba path->reada = READA_FORWARD; 19061b49440SChris Mason 1912cf8572dSChris Mason /* 1922cf8572dSChris Mason * the free space stuff is only read when it hasn't been 1932cf8572dSChris Mason * updated in the current transaction. So, we can safely 1942cf8572dSChris Mason * read from the commit root and sidestep a nasty deadlock 1952cf8572dSChris Mason * between reading the free space cache and updating the csum tree. 1962cf8572dSChris Mason */ 19770ddc553SNikolay Borisov if (btrfs_is_free_space_inode(BTRFS_I(inode))) { 1982cf8572dSChris Mason path->search_commit_root = 1; 199ddf23b3fSJosef Bacik path->skip_locking = 1; 200ddf23b3fSJosef Bacik } 2012cf8572dSChris Mason 2024f024f37SKent Overstreet disk_bytenr = (u64)bio->bi_iter.bi_sector << 9; 2034b46fce2SJosef Bacik if (dio) 2044b46fce2SJosef Bacik offset = logical_offset; 205c40a3d38SChandan Rajendra 20617347cecSLiu Bo bio_for_each_segment(bvec, bio, iter) { 20717347cecSLiu Bo page_bytes_left = bvec.bv_len; 2084989d277SChristoph Hellwig if (count) 2094989d277SChristoph Hellwig goto next; 2104989d277SChristoph Hellwig 2114b46fce2SJosef Bacik if (!dio) 21217347cecSLiu Bo offset = page_offset(bvec.bv_page) + bvec.bv_offset; 213facc8a22SMiao Xie count = btrfs_find_ordered_sum(inode, offset, disk_bytenr, 214facc8a22SMiao Xie (u32 *)csum, nblocks); 215e4100d98SMiao Xie if (count) 21661b49440SChris Mason goto found; 21761b49440SChris Mason 218d20f7043SChris Mason if (!item || disk_bytenr < item_start_offset || 219d20f7043SChris Mason disk_bytenr >= item_last_offset) { 22061b49440SChris Mason struct btrfs_key found_key; 22161b49440SChris Mason u32 item_size; 22261b49440SChris Mason 22361b49440SChris Mason if (item) 224b3b4aa74SDavid Sterba btrfs_release_path(path); 2250b246afaSJeff Mahoney item = btrfs_lookup_csum(NULL, fs_info->csum_root, 226d20f7043SChris Mason path, disk_bytenr, 0); 22761b49440SChris Mason if (IS_ERR(item)) { 228e4100d98SMiao Xie count = 1; 229facc8a22SMiao Xie memset(csum, 0, csum_size); 23017d217feSYan Zheng if (BTRFS_I(inode)->root->root_key.objectid == 23117d217feSYan Zheng BTRFS_DATA_RELOC_TREE_OBJECTID) { 23217d217feSYan Zheng set_extent_bits(io_tree, offset, 2330b246afaSJeff Mahoney offset + fs_info->sectorsize - 1, 234ceeb0ae7SDavid Sterba EXTENT_NODATASUM); 23517d217feSYan Zheng } else { 2360b246afaSJeff Mahoney btrfs_info_rl(fs_info, 237efe120a0SFrank Holton "no csum found for inode %llu start %llu", 2384a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(inode)), offset); 23917d217feSYan Zheng } 2406dab8157SChris Mason item = NULL; 241b3b4aa74SDavid Sterba btrfs_release_path(path); 24261b49440SChris Mason goto found; 24361b49440SChris Mason } 24461b49440SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key, 24561b49440SChris Mason path->slots[0]); 24661b49440SChris Mason 24761b49440SChris Mason item_start_offset = found_key.offset; 24861b49440SChris Mason item_size = btrfs_item_size_nr(path->nodes[0], 24961b49440SChris Mason path->slots[0]); 25061b49440SChris Mason item_last_offset = item_start_offset + 251607d432dSJosef Bacik (item_size / csum_size) * 2520b246afaSJeff Mahoney fs_info->sectorsize; 25361b49440SChris Mason item = btrfs_item_ptr(path->nodes[0], path->slots[0], 25461b49440SChris Mason struct btrfs_csum_item); 25561b49440SChris Mason } 25661b49440SChris Mason /* 25761b49440SChris Mason * this byte range must be able to fit inside 25861b49440SChris Mason * a single leaf so it will also fit inside a u32 25961b49440SChris Mason */ 260d20f7043SChris Mason diff = disk_bytenr - item_start_offset; 2610b246afaSJeff Mahoney diff = diff / fs_info->sectorsize; 262607d432dSJosef Bacik diff = diff * csum_size; 263facc8a22SMiao Xie count = min_t(int, nblocks, (item_last_offset - disk_bytenr) >> 264e4100d98SMiao Xie inode->i_sb->s_blocksize_bits); 265facc8a22SMiao Xie read_extent_buffer(path->nodes[0], csum, 2663de9d6b6SChris Mason ((unsigned long)item) + diff, 267e4100d98SMiao Xie csum_size * count); 26861b49440SChris Mason found: 269facc8a22SMiao Xie csum += count * csum_size; 270facc8a22SMiao Xie nblocks -= count; 2714989d277SChristoph Hellwig next: 272e4100d98SMiao Xie while (count--) { 2730b246afaSJeff Mahoney disk_bytenr += fs_info->sectorsize; 2740b246afaSJeff Mahoney offset += fs_info->sectorsize; 2750b246afaSJeff Mahoney page_bytes_left -= fs_info->sectorsize; 2764989d277SChristoph Hellwig if (!page_bytes_left) 2774989d277SChristoph Hellwig break; /* move to next bio */ 2784989d277SChristoph Hellwig } 2794989d277SChristoph Hellwig } 2804989d277SChristoph Hellwig 281389f239cSChris Mason WARN_ON_ONCE(count); 28261b49440SChris Mason btrfs_free_path(path); 28361b49440SChris Mason return 0; 28461b49440SChris Mason } 28561b49440SChris Mason 2864e4cbee9SChristoph Hellwig blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, u32 *dst) 2874b46fce2SJosef Bacik { 2882ff7e61eSJeff Mahoney return __btrfs_lookup_bio_sums(inode, bio, 0, dst, 0); 2894b46fce2SJosef Bacik } 2904b46fce2SJosef Bacik 2914e4cbee9SChristoph Hellwig blk_status_t btrfs_lookup_bio_sums_dio(struct inode *inode, struct bio *bio, u64 offset) 2924b46fce2SJosef Bacik { 2932ff7e61eSJeff Mahoney return __btrfs_lookup_bio_sums(inode, bio, offset, NULL, 1); 2944b46fce2SJosef Bacik } 2954b46fce2SJosef Bacik 29617d217feSYan Zheng int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, 297a2de733cSArne Jansen struct list_head *list, int search_commit) 29817d217feSYan Zheng { 2990b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 30017d217feSYan Zheng struct btrfs_key key; 30117d217feSYan Zheng struct btrfs_path *path; 30217d217feSYan Zheng struct extent_buffer *leaf; 30317d217feSYan Zheng struct btrfs_ordered_sum *sums; 30417d217feSYan Zheng struct btrfs_csum_item *item; 3050678b618SMark Fasheh LIST_HEAD(tmplist); 30617d217feSYan Zheng unsigned long offset; 30717d217feSYan Zheng int ret; 30817d217feSYan Zheng size_t size; 30917d217feSYan Zheng u64 csum_end; 3100b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 31117d217feSYan Zheng 3120b246afaSJeff Mahoney ASSERT(IS_ALIGNED(start, fs_info->sectorsize) && 3130b246afaSJeff Mahoney IS_ALIGNED(end + 1, fs_info->sectorsize)); 3144277a9c3SJosef Bacik 31517d217feSYan Zheng path = btrfs_alloc_path(); 316d8926bb3SMark Fasheh if (!path) 317d8926bb3SMark Fasheh return -ENOMEM; 31817d217feSYan Zheng 319a2de733cSArne Jansen if (search_commit) { 320a2de733cSArne Jansen path->skip_locking = 1; 321e4058b54SDavid Sterba path->reada = READA_FORWARD; 322a2de733cSArne Jansen path->search_commit_root = 1; 323a2de733cSArne Jansen } 324a2de733cSArne Jansen 32517d217feSYan Zheng key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 32617d217feSYan Zheng key.offset = start; 32717d217feSYan Zheng key.type = BTRFS_EXTENT_CSUM_KEY; 32817d217feSYan Zheng 32907d400a6SYan Zheng ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 33017d217feSYan Zheng if (ret < 0) 33117d217feSYan Zheng goto fail; 33217d217feSYan Zheng if (ret > 0 && path->slots[0] > 0) { 33317d217feSYan Zheng leaf = path->nodes[0]; 33417d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); 33517d217feSYan Zheng if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID && 33617d217feSYan Zheng key.type == BTRFS_EXTENT_CSUM_KEY) { 33717d217feSYan Zheng offset = (start - key.offset) >> 3380b246afaSJeff Mahoney fs_info->sb->s_blocksize_bits; 33917d217feSYan Zheng if (offset * csum_size < 34017d217feSYan Zheng btrfs_item_size_nr(leaf, path->slots[0] - 1)) 34117d217feSYan Zheng path->slots[0]--; 34217d217feSYan Zheng } 34317d217feSYan Zheng } 34417d217feSYan Zheng 34517d217feSYan Zheng while (start <= end) { 34617d217feSYan Zheng leaf = path->nodes[0]; 34717d217feSYan Zheng if (path->slots[0] >= btrfs_header_nritems(leaf)) { 34807d400a6SYan Zheng ret = btrfs_next_leaf(root, path); 34917d217feSYan Zheng if (ret < 0) 35017d217feSYan Zheng goto fail; 35117d217feSYan Zheng if (ret > 0) 35217d217feSYan Zheng break; 35317d217feSYan Zheng leaf = path->nodes[0]; 35417d217feSYan Zheng } 35517d217feSYan Zheng 35617d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 35717d217feSYan Zheng if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 358628c8282SZhi Yong Wu key.type != BTRFS_EXTENT_CSUM_KEY || 359628c8282SZhi Yong Wu key.offset > end) 36017d217feSYan Zheng break; 36117d217feSYan Zheng 36217d217feSYan Zheng if (key.offset > start) 36317d217feSYan Zheng start = key.offset; 36417d217feSYan Zheng 36517d217feSYan Zheng size = btrfs_item_size_nr(leaf, path->slots[0]); 3660b246afaSJeff Mahoney csum_end = key.offset + (size / csum_size) * fs_info->sectorsize; 36787b29b20SYan Zheng if (csum_end <= start) { 36887b29b20SYan Zheng path->slots[0]++; 36987b29b20SYan Zheng continue; 37087b29b20SYan Zheng } 37117d217feSYan Zheng 37207d400a6SYan Zheng csum_end = min(csum_end, end + 1); 37307d400a6SYan Zheng item = btrfs_item_ptr(path->nodes[0], path->slots[0], 37407d400a6SYan Zheng struct btrfs_csum_item); 37507d400a6SYan Zheng while (start < csum_end) { 37607d400a6SYan Zheng size = min_t(size_t, csum_end - start, 3770b246afaSJeff Mahoney MAX_ORDERED_SUM_BYTES(fs_info)); 3780b246afaSJeff Mahoney sums = kzalloc(btrfs_ordered_sum_size(fs_info, size), 37907d400a6SYan Zheng GFP_NOFS); 3800678b618SMark Fasheh if (!sums) { 3810678b618SMark Fasheh ret = -ENOMEM; 3820678b618SMark Fasheh goto fail; 3830678b618SMark Fasheh } 38417d217feSYan Zheng 38517d217feSYan Zheng sums->bytenr = start; 386f51a4a18SMiao Xie sums->len = (int)size; 38717d217feSYan Zheng 38817d217feSYan Zheng offset = (start - key.offset) >> 3890b246afaSJeff Mahoney fs_info->sb->s_blocksize_bits; 39017d217feSYan Zheng offset *= csum_size; 3910b246afaSJeff Mahoney size >>= fs_info->sb->s_blocksize_bits; 39217d217feSYan Zheng 39307d400a6SYan Zheng read_extent_buffer(path->nodes[0], 394f51a4a18SMiao Xie sums->sums, 395f51a4a18SMiao Xie ((unsigned long)item) + offset, 396f51a4a18SMiao Xie csum_size * size); 39717d217feSYan Zheng 3980b246afaSJeff Mahoney start += fs_info->sectorsize * size; 3990678b618SMark Fasheh list_add_tail(&sums->list, &tmplist); 40007d400a6SYan Zheng } 40117d217feSYan Zheng path->slots[0]++; 40217d217feSYan Zheng } 40317d217feSYan Zheng ret = 0; 40417d217feSYan Zheng fail: 4050678b618SMark Fasheh while (ret < 0 && !list_empty(&tmplist)) { 4066e5aafb2SChris Mason sums = list_entry(tmplist.next, struct btrfs_ordered_sum, list); 4070678b618SMark Fasheh list_del(&sums->list); 4080678b618SMark Fasheh kfree(sums); 4090678b618SMark Fasheh } 4100678b618SMark Fasheh list_splice_tail(&tmplist, list); 4110678b618SMark Fasheh 41217d217feSYan Zheng btrfs_free_path(path); 41317d217feSYan Zheng return ret; 41417d217feSYan Zheng } 41517d217feSYan Zheng 4164e4cbee9SChristoph Hellwig blk_status_t btrfs_csum_one_bio(struct inode *inode, struct bio *bio, 4172ff7e61eSJeff Mahoney u64 file_start, int contig) 418e015640fSChris Mason { 4190b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 420e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums; 4216cd7ce49SChristoph Hellwig struct btrfs_ordered_extent *ordered = NULL; 422e015640fSChris Mason char *data; 42317347cecSLiu Bo struct bvec_iter iter; 42417347cecSLiu Bo struct bio_vec bvec; 425f51a4a18SMiao Xie int index; 426c40a3d38SChandan Rajendra int nr_sectors; 4273edf7d33SChris Mason unsigned long total_bytes = 0; 4283edf7d33SChris Mason unsigned long this_sum_bytes = 0; 42917347cecSLiu Bo int i; 4303edf7d33SChris Mason u64 offset; 431a3d46aeaSNikolay Borisov unsigned nofs_flag; 432e015640fSChris Mason 433a3d46aeaSNikolay Borisov nofs_flag = memalloc_nofs_save(); 434a3d46aeaSNikolay Borisov sums = kvzalloc(btrfs_ordered_sum_size(fs_info, bio->bi_iter.bi_size), 435a3d46aeaSNikolay Borisov GFP_KERNEL); 436a3d46aeaSNikolay Borisov memalloc_nofs_restore(nofs_flag); 437a3d46aeaSNikolay Borisov 438e015640fSChris Mason if (!sums) 4394e4cbee9SChristoph Hellwig return BLK_STS_RESOURCE; 4403edf7d33SChris Mason 4414f024f37SKent Overstreet sums->len = bio->bi_iter.bi_size; 442e6dcd2dcSChris Mason INIT_LIST_HEAD(&sums->list); 443d20f7043SChris Mason 444d20f7043SChris Mason if (contig) 445d20f7043SChris Mason offset = file_start; 446d20f7043SChris Mason else 4476cd7ce49SChristoph Hellwig offset = 0; /* shut up gcc */ 448d20f7043SChris Mason 4494f024f37SKent Overstreet sums->bytenr = (u64)bio->bi_iter.bi_sector << 9; 450f51a4a18SMiao Xie index = 0; 451e015640fSChris Mason 45217347cecSLiu Bo bio_for_each_segment(bvec, bio, iter) { 453d20f7043SChris Mason if (!contig) 45417347cecSLiu Bo offset = page_offset(bvec.bv_page) + bvec.bv_offset; 455d20f7043SChris Mason 4566cd7ce49SChristoph Hellwig if (!ordered) { 4576cd7ce49SChristoph Hellwig ordered = btrfs_lookup_ordered_extent(inode, offset); 4586cd7ce49SChristoph Hellwig BUG_ON(!ordered); /* Logic error */ 4596cd7ce49SChristoph Hellwig } 4606cd7ce49SChristoph Hellwig 4610b246afaSJeff Mahoney nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, 46217347cecSLiu Bo bvec.bv_len + fs_info->sectorsize 463c40a3d38SChandan Rajendra - 1); 464c40a3d38SChandan Rajendra 465c40a3d38SChandan Rajendra for (i = 0; i < nr_sectors; i++) { 466e58dd74bSJosef Bacik if (offset >= ordered->file_offset + ordered->len || 467e58dd74bSJosef Bacik offset < ordered->file_offset) { 4683edf7d33SChris Mason unsigned long bytes_left; 469c40a3d38SChandan Rajendra 4703edf7d33SChris Mason sums->len = this_sum_bytes; 4713edf7d33SChris Mason this_sum_bytes = 0; 472*f9756261SNikolay Borisov btrfs_add_ordered_sum(ordered, sums); 4733edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 4743edf7d33SChris Mason 4754f024f37SKent Overstreet bytes_left = bio->bi_iter.bi_size - total_bytes; 4763edf7d33SChris Mason 477a3d46aeaSNikolay Borisov nofs_flag = memalloc_nofs_save(); 478a3d46aeaSNikolay Borisov sums = kvzalloc(btrfs_ordered_sum_size(fs_info, 479a3d46aeaSNikolay Borisov bytes_left), GFP_KERNEL); 480a3d46aeaSNikolay Borisov memalloc_nofs_restore(nofs_flag); 48179787eaaSJeff Mahoney BUG_ON(!sums); /* -ENOMEM */ 4823edf7d33SChris Mason sums->len = bytes_left; 483c40a3d38SChandan Rajendra ordered = btrfs_lookup_ordered_extent(inode, 484c40a3d38SChandan Rajendra offset); 485c40a3d38SChandan Rajendra ASSERT(ordered); /* Logic error */ 486c40a3d38SChandan Rajendra sums->bytenr = ((u64)bio->bi_iter.bi_sector << 9) 487c40a3d38SChandan Rajendra + total_bytes; 488f51a4a18SMiao Xie index = 0; 489c40a3d38SChandan Rajendra } 490c40a3d38SChandan Rajendra 491f51a4a18SMiao Xie sums->sums[index] = ~(u32)0; 492443c8e2aSJohannes Thumshirn data = kmap_atomic(bvec.bv_page); 493c40a3d38SChandan Rajendra sums->sums[index] 49417347cecSLiu Bo = btrfs_csum_data(data + bvec.bv_offset 4950b246afaSJeff Mahoney + (i * fs_info->sectorsize), 496f51a4a18SMiao Xie sums->sums[index], 4970b246afaSJeff Mahoney fs_info->sectorsize); 498443c8e2aSJohannes Thumshirn kunmap_atomic(data); 499f51a4a18SMiao Xie btrfs_csum_final(sums->sums[index], 500f51a4a18SMiao Xie (char *)(sums->sums + index)); 501c40a3d38SChandan Rajendra index++; 5020b246afaSJeff Mahoney offset += fs_info->sectorsize; 5030b246afaSJeff Mahoney this_sum_bytes += fs_info->sectorsize; 5040b246afaSJeff Mahoney total_bytes += fs_info->sectorsize; 505c40a3d38SChandan Rajendra } 506c40a3d38SChandan Rajendra 507e015640fSChris Mason } 508ed98b56aSChris Mason this_sum_bytes = 0; 509*f9756261SNikolay Borisov btrfs_add_ordered_sum(ordered, sums); 5103edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 511e015640fSChris Mason return 0; 512e015640fSChris Mason } 513e015640fSChris Mason 514459931ecSChris Mason /* 515459931ecSChris Mason * helper function for csum removal, this expects the 516459931ecSChris Mason * key to describe the csum pointed to by the path, and it expects 517459931ecSChris Mason * the csum to overlap the range [bytenr, len] 518459931ecSChris Mason * 519459931ecSChris Mason * The csum should not be entirely contained in the range and the 520459931ecSChris Mason * range should not be entirely contained in the csum. 521459931ecSChris Mason * 522459931ecSChris Mason * This calls btrfs_truncate_item with the correct args based on the 523459931ecSChris Mason * overlap, and fixes up the key as required. 524459931ecSChris Mason */ 5252ff7e61eSJeff Mahoney static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info, 526459931ecSChris Mason struct btrfs_path *path, 527459931ecSChris Mason struct btrfs_key *key, 528459931ecSChris Mason u64 bytenr, u64 len) 529459931ecSChris Mason { 530459931ecSChris Mason struct extent_buffer *leaf; 5310b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 532459931ecSChris Mason u64 csum_end; 533459931ecSChris Mason u64 end_byte = bytenr + len; 5340b246afaSJeff Mahoney u32 blocksize_bits = fs_info->sb->s_blocksize_bits; 535459931ecSChris Mason 536459931ecSChris Mason leaf = path->nodes[0]; 537459931ecSChris Mason csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; 5380b246afaSJeff Mahoney csum_end <<= fs_info->sb->s_blocksize_bits; 539459931ecSChris Mason csum_end += key->offset; 540459931ecSChris Mason 541459931ecSChris Mason if (key->offset < bytenr && csum_end <= end_byte) { 542459931ecSChris Mason /* 543459931ecSChris Mason * [ bytenr - len ] 544459931ecSChris Mason * [ ] 545459931ecSChris Mason * [csum ] 546459931ecSChris Mason * A simple truncate off the end of the item 547459931ecSChris Mason */ 548459931ecSChris Mason u32 new_size = (bytenr - key->offset) >> blocksize_bits; 549459931ecSChris Mason new_size *= csum_size; 5502ff7e61eSJeff Mahoney btrfs_truncate_item(fs_info, path, new_size, 1); 551459931ecSChris Mason } else if (key->offset >= bytenr && csum_end > end_byte && 552459931ecSChris Mason end_byte > key->offset) { 553459931ecSChris Mason /* 554459931ecSChris Mason * [ bytenr - len ] 555459931ecSChris Mason * [ ] 556459931ecSChris Mason * [csum ] 557459931ecSChris Mason * we need to truncate from the beginning of the csum 558459931ecSChris Mason */ 559459931ecSChris Mason u32 new_size = (csum_end - end_byte) >> blocksize_bits; 560459931ecSChris Mason new_size *= csum_size; 561459931ecSChris Mason 5622ff7e61eSJeff Mahoney btrfs_truncate_item(fs_info, path, new_size, 0); 563459931ecSChris Mason 564459931ecSChris Mason key->offset = end_byte; 5650b246afaSJeff Mahoney btrfs_set_item_key_safe(fs_info, path, key); 566459931ecSChris Mason } else { 567459931ecSChris Mason BUG(); 568459931ecSChris Mason } 569459931ecSChris Mason } 570459931ecSChris Mason 571459931ecSChris Mason /* 572459931ecSChris Mason * deletes the csum items from the csum tree for a given 573459931ecSChris Mason * range of bytes. 574459931ecSChris Mason */ 575459931ecSChris Mason int btrfs_del_csums(struct btrfs_trans_handle *trans, 5765b4aacefSJeff Mahoney struct btrfs_fs_info *fs_info, u64 bytenr, u64 len) 577459931ecSChris Mason { 5785b4aacefSJeff Mahoney struct btrfs_root *root = fs_info->csum_root; 579459931ecSChris Mason struct btrfs_path *path; 580459931ecSChris Mason struct btrfs_key key; 581459931ecSChris Mason u64 end_byte = bytenr + len; 582459931ecSChris Mason u64 csum_end; 583459931ecSChris Mason struct extent_buffer *leaf; 584459931ecSChris Mason int ret; 5850b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 5860b246afaSJeff Mahoney int blocksize_bits = fs_info->sb->s_blocksize_bits; 587459931ecSChris Mason 588459931ecSChris Mason path = btrfs_alloc_path(); 5892a29edc6Sliubo if (!path) 5902a29edc6Sliubo return -ENOMEM; 591459931ecSChris Mason 592459931ecSChris Mason while (1) { 593459931ecSChris Mason key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 594459931ecSChris Mason key.offset = end_byte - 1; 595459931ecSChris Mason key.type = BTRFS_EXTENT_CSUM_KEY; 596459931ecSChris Mason 597b9473439SChris Mason path->leave_spinning = 1; 598459931ecSChris Mason ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 599459931ecSChris Mason if (ret > 0) { 600459931ecSChris Mason if (path->slots[0] == 0) 60165a246c5STsutomu Itoh break; 602459931ecSChris Mason path->slots[0]--; 603ad0397a7SJosef Bacik } else if (ret < 0) { 60465a246c5STsutomu Itoh break; 605459931ecSChris Mason } 606ad0397a7SJosef Bacik 607459931ecSChris Mason leaf = path->nodes[0]; 608459931ecSChris Mason btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 609459931ecSChris Mason 610459931ecSChris Mason if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 611459931ecSChris Mason key.type != BTRFS_EXTENT_CSUM_KEY) { 612459931ecSChris Mason break; 613459931ecSChris Mason } 614459931ecSChris Mason 615459931ecSChris Mason if (key.offset >= end_byte) 616459931ecSChris Mason break; 617459931ecSChris Mason 618459931ecSChris Mason csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; 619459931ecSChris Mason csum_end <<= blocksize_bits; 620459931ecSChris Mason csum_end += key.offset; 621459931ecSChris Mason 622459931ecSChris Mason /* this csum ends before we start, we're done */ 623459931ecSChris Mason if (csum_end <= bytenr) 624459931ecSChris Mason break; 625459931ecSChris Mason 626459931ecSChris Mason /* delete the entire item, it is inside our range */ 627459931ecSChris Mason if (key.offset >= bytenr && csum_end <= end_byte) { 6286f546216SFilipe Manana int del_nr = 1; 6296f546216SFilipe Manana 6306f546216SFilipe Manana /* 6316f546216SFilipe Manana * Check how many csum items preceding this one in this 6326f546216SFilipe Manana * leaf correspond to our range and then delete them all 6336f546216SFilipe Manana * at once. 6346f546216SFilipe Manana */ 6356f546216SFilipe Manana if (key.offset > bytenr && path->slots[0] > 0) { 6366f546216SFilipe Manana int slot = path->slots[0] - 1; 6376f546216SFilipe Manana 6386f546216SFilipe Manana while (slot >= 0) { 6396f546216SFilipe Manana struct btrfs_key pk; 6406f546216SFilipe Manana 6416f546216SFilipe Manana btrfs_item_key_to_cpu(leaf, &pk, slot); 6426f546216SFilipe Manana if (pk.offset < bytenr || 6436f546216SFilipe Manana pk.type != BTRFS_EXTENT_CSUM_KEY || 6446f546216SFilipe Manana pk.objectid != 6456f546216SFilipe Manana BTRFS_EXTENT_CSUM_OBJECTID) 6466f546216SFilipe Manana break; 6476f546216SFilipe Manana path->slots[0] = slot; 6486f546216SFilipe Manana del_nr++; 6496f546216SFilipe Manana key.offset = pk.offset; 6506f546216SFilipe Manana slot--; 6516f546216SFilipe Manana } 6526f546216SFilipe Manana } 6536f546216SFilipe Manana ret = btrfs_del_items(trans, root, path, 6546f546216SFilipe Manana path->slots[0], del_nr); 65565a246c5STsutomu Itoh if (ret) 65665a246c5STsutomu Itoh goto out; 657dcbdd4dcSChris Mason if (key.offset == bytenr) 658dcbdd4dcSChris Mason break; 659459931ecSChris Mason } else if (key.offset < bytenr && csum_end > end_byte) { 660459931ecSChris Mason unsigned long offset; 661459931ecSChris Mason unsigned long shift_len; 662459931ecSChris Mason unsigned long item_offset; 663459931ecSChris Mason /* 664459931ecSChris Mason * [ bytenr - len ] 665459931ecSChris Mason * [csum ] 666459931ecSChris Mason * 667459931ecSChris Mason * Our bytes are in the middle of the csum, 668459931ecSChris Mason * we need to split this item and insert a new one. 669459931ecSChris Mason * 670459931ecSChris Mason * But we can't drop the path because the 671459931ecSChris Mason * csum could change, get removed, extended etc. 672459931ecSChris Mason * 673459931ecSChris Mason * The trick here is the max size of a csum item leaves 674459931ecSChris Mason * enough room in the tree block for a single 675459931ecSChris Mason * item header. So, we split the item in place, 676459931ecSChris Mason * adding a new header pointing to the existing 677459931ecSChris Mason * bytes. Then we loop around again and we have 678459931ecSChris Mason * a nicely formed csum item that we can neatly 679459931ecSChris Mason * truncate. 680459931ecSChris Mason */ 681459931ecSChris Mason offset = (bytenr - key.offset) >> blocksize_bits; 682459931ecSChris Mason offset *= csum_size; 683459931ecSChris Mason 684459931ecSChris Mason shift_len = (len >> blocksize_bits) * csum_size; 685459931ecSChris Mason 686459931ecSChris Mason item_offset = btrfs_item_ptr_offset(leaf, 687459931ecSChris Mason path->slots[0]); 688459931ecSChris Mason 689b159fa28SDavid Sterba memzero_extent_buffer(leaf, item_offset + offset, 690459931ecSChris Mason shift_len); 691459931ecSChris Mason key.offset = bytenr; 692459931ecSChris Mason 693459931ecSChris Mason /* 694459931ecSChris Mason * btrfs_split_item returns -EAGAIN when the 695459931ecSChris Mason * item changed size or key 696459931ecSChris Mason */ 697459931ecSChris Mason ret = btrfs_split_item(trans, root, path, &key, offset); 69879787eaaSJeff Mahoney if (ret && ret != -EAGAIN) { 69966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 70079787eaaSJeff Mahoney goto out; 70179787eaaSJeff Mahoney } 702459931ecSChris Mason 703459931ecSChris Mason key.offset = end_byte - 1; 704459931ecSChris Mason } else { 7052ff7e61eSJeff Mahoney truncate_one_csum(fs_info, path, &key, bytenr, len); 706dcbdd4dcSChris Mason if (key.offset < bytenr) 707dcbdd4dcSChris Mason break; 708459931ecSChris Mason } 709b3b4aa74SDavid Sterba btrfs_release_path(path); 710459931ecSChris Mason } 71165a246c5STsutomu Itoh ret = 0; 712459931ecSChris Mason out: 713459931ecSChris Mason btrfs_free_path(path); 71465a246c5STsutomu Itoh return ret; 715459931ecSChris Mason } 716459931ecSChris Mason 717065631f6SChris Mason int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, 718d20f7043SChris Mason struct btrfs_root *root, 719e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums) 720f254e52cSChris Mason { 7210b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 722f254e52cSChris Mason struct btrfs_key file_key; 7236567e837SChris Mason struct btrfs_key found_key; 7245caf2a00SChris Mason struct btrfs_path *path; 725f254e52cSChris Mason struct btrfs_csum_item *item; 726065631f6SChris Mason struct btrfs_csum_item *item_end; 727ff79f819SChris Mason struct extent_buffer *leaf = NULL; 728f51a4a18SMiao Xie u64 next_offset; 729f51a4a18SMiao Xie u64 total_bytes = 0; 7306567e837SChris Mason u64 csum_offset; 731f51a4a18SMiao Xie u64 bytenr; 732f578d4bdSChris Mason u32 nritems; 733f578d4bdSChris Mason u32 ins_size; 734f51a4a18SMiao Xie int index = 0; 735f51a4a18SMiao Xie int found_next; 736f51a4a18SMiao Xie int ret; 7370b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 7386e92f5e6SChris Mason 7395caf2a00SChris Mason path = btrfs_alloc_path(); 740d8926bb3SMark Fasheh if (!path) 741d8926bb3SMark Fasheh return -ENOMEM; 742065631f6SChris Mason again: 743065631f6SChris Mason next_offset = (u64)-1; 744065631f6SChris Mason found_next = 0; 745f51a4a18SMiao Xie bytenr = sums->bytenr + total_bytes; 746d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 747f51a4a18SMiao Xie file_key.offset = bytenr; 748962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_CSUM_KEY; 749a429e513SChris Mason 750f51a4a18SMiao Xie item = btrfs_lookup_csum(trans, root, path, bytenr, 1); 751ff79f819SChris Mason if (!IS_ERR(item)) { 752639cb586SChris Mason ret = 0; 753f51a4a18SMiao Xie leaf = path->nodes[0]; 754f51a4a18SMiao Xie item_end = btrfs_item_ptr(leaf, path->slots[0], 755f51a4a18SMiao Xie struct btrfs_csum_item); 756f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((char *)item_end + 757f51a4a18SMiao Xie btrfs_item_size_nr(leaf, path->slots[0])); 758a429e513SChris Mason goto found; 759ff79f819SChris Mason } 760a429e513SChris Mason ret = PTR_ERR(item); 7614a500fd1SYan, Zheng if (ret != -EFBIG && ret != -ENOENT) 7624a500fd1SYan, Zheng goto fail_unlock; 7634a500fd1SYan, Zheng 764a429e513SChris Mason if (ret == -EFBIG) { 765a429e513SChris Mason u32 item_size; 766a429e513SChris Mason /* we found one, but it isn't big enough yet */ 7675f39d397SChris Mason leaf = path->nodes[0]; 7685f39d397SChris Mason item_size = btrfs_item_size_nr(leaf, path->slots[0]); 769607d432dSJosef Bacik if ((item_size / csum_size) >= 7700b246afaSJeff Mahoney MAX_CSUM_ITEMS(fs_info, csum_size)) { 771a429e513SChris Mason /* already at max size, make a new one */ 772a429e513SChris Mason goto insert; 773a429e513SChris Mason } 774a429e513SChris Mason } else { 775f578d4bdSChris Mason int slot = path->slots[0] + 1; 776a429e513SChris Mason /* we didn't find a csum item, insert one */ 777f578d4bdSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 77835045bf2SFilipe Manana if (!nritems || (path->slots[0] >= nritems - 1)) { 779f578d4bdSChris Mason ret = btrfs_next_leaf(root, path); 780b56baf5bSYan if (ret == 1) 781f578d4bdSChris Mason found_next = 1; 782b56baf5bSYan if (ret != 0) 783f578d4bdSChris Mason goto insert; 78427b9a812SFilipe Manana slot = path->slots[0]; 785f578d4bdSChris Mason } 786f578d4bdSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); 787d20f7043SChris Mason if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 788d20f7043SChris Mason found_key.type != BTRFS_EXTENT_CSUM_KEY) { 789f578d4bdSChris Mason found_next = 1; 790f578d4bdSChris Mason goto insert; 791f578d4bdSChris Mason } 792f578d4bdSChris Mason next_offset = found_key.offset; 793f578d4bdSChris Mason found_next = 1; 794a429e513SChris Mason goto insert; 795a429e513SChris Mason } 796a429e513SChris Mason 797a429e513SChris Mason /* 798a429e513SChris Mason * at this point, we know the tree has an item, but it isn't big 799a429e513SChris Mason * enough yet to put our csum in. Grow it 800a429e513SChris Mason */ 801b3b4aa74SDavid Sterba btrfs_release_path(path); 8026567e837SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 803607d432dSJosef Bacik csum_size, 1); 8046567e837SChris Mason if (ret < 0) 80553863232SChris Mason goto fail_unlock; 806459931ecSChris Mason 807459931ecSChris Mason if (ret > 0) { 808459931ecSChris Mason if (path->slots[0] == 0) 8096567e837SChris Mason goto insert; 8106567e837SChris Mason path->slots[0]--; 811459931ecSChris Mason } 812459931ecSChris Mason 8135f39d397SChris Mason leaf = path->nodes[0]; 8145f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 815d20f7043SChris Mason csum_offset = (bytenr - found_key.offset) >> 8160b246afaSJeff Mahoney fs_info->sb->s_blocksize_bits; 817459931ecSChris Mason 818962a298fSDavid Sterba if (found_key.type != BTRFS_EXTENT_CSUM_KEY || 819d20f7043SChris Mason found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 8200b246afaSJeff Mahoney csum_offset >= MAX_CSUM_ITEMS(fs_info, csum_size)) { 8216567e837SChris Mason goto insert; 8226567e837SChris Mason } 823459931ecSChris Mason 8242f697dc6SLiu Bo if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) / 825607d432dSJosef Bacik csum_size) { 8262f697dc6SLiu Bo int extend_nr; 8272f697dc6SLiu Bo u64 tmp; 8282f697dc6SLiu Bo u32 diff; 8292f697dc6SLiu Bo u32 free_space; 830459931ecSChris Mason 831e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < 8322f697dc6SLiu Bo sizeof(struct btrfs_item) + csum_size * 2) 8332f697dc6SLiu Bo goto insert; 8342f697dc6SLiu Bo 835e902baacSDavid Sterba free_space = btrfs_leaf_free_space(leaf) - 8362f697dc6SLiu Bo sizeof(struct btrfs_item) - csum_size; 837f51a4a18SMiao Xie tmp = sums->len - total_bytes; 8380b246afaSJeff Mahoney tmp >>= fs_info->sb->s_blocksize_bits; 8392f697dc6SLiu Bo WARN_ON(tmp < 1); 8402f697dc6SLiu Bo 8412f697dc6SLiu Bo extend_nr = max_t(int, 1, (int)tmp); 8422f697dc6SLiu Bo diff = (csum_offset + extend_nr) * csum_size; 8430b246afaSJeff Mahoney diff = min(diff, 8440b246afaSJeff Mahoney MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size); 845459931ecSChris Mason 8465f39d397SChris Mason diff = diff - btrfs_item_size_nr(leaf, path->slots[0]); 8472f697dc6SLiu Bo diff = min(free_space, diff); 8482f697dc6SLiu Bo diff /= csum_size; 8492f697dc6SLiu Bo diff *= csum_size; 850459931ecSChris Mason 8512ff7e61eSJeff Mahoney btrfs_extend_item(fs_info, path, diff); 852f51a4a18SMiao Xie ret = 0; 8536567e837SChris Mason goto csum; 8546567e837SChris Mason } 8556567e837SChris Mason 8566567e837SChris Mason insert: 857b3b4aa74SDavid Sterba btrfs_release_path(path); 8586567e837SChris Mason csum_offset = 0; 859f578d4bdSChris Mason if (found_next) { 8602f697dc6SLiu Bo u64 tmp; 861d20f7043SChris Mason 862f51a4a18SMiao Xie tmp = sums->len - total_bytes; 8630b246afaSJeff Mahoney tmp >>= fs_info->sb->s_blocksize_bits; 8642f697dc6SLiu Bo tmp = min(tmp, (next_offset - file_key.offset) >> 8650b246afaSJeff Mahoney fs_info->sb->s_blocksize_bits); 8662f697dc6SLiu Bo 86750d0446eSSeraphime Kirkovski tmp = max_t(u64, 1, tmp); 86850d0446eSSeraphime Kirkovski tmp = min_t(u64, tmp, MAX_CSUM_ITEMS(fs_info, csum_size)); 869607d432dSJosef Bacik ins_size = csum_size * tmp; 870f578d4bdSChris Mason } else { 871607d432dSJosef Bacik ins_size = csum_size; 872f578d4bdSChris Mason } 873b9473439SChris Mason path->leave_spinning = 1; 8745caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 875f578d4bdSChris Mason ins_size); 876b9473439SChris Mason path->leave_spinning = 0; 87754aa1f4dSChris Mason if (ret < 0) 87853863232SChris Mason goto fail_unlock; 879fae7f21cSDulshani Gunawardhana if (WARN_ON(ret != 0)) 88053863232SChris Mason goto fail_unlock; 8815f39d397SChris Mason leaf = path->nodes[0]; 882f51a4a18SMiao Xie csum: 8835f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 884f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((unsigned char *)item + 885f51a4a18SMiao Xie btrfs_item_size_nr(leaf, path->slots[0])); 886509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 887607d432dSJosef Bacik csum_offset * csum_size); 888b18c6685SChris Mason found: 889f51a4a18SMiao Xie ins_size = (u32)(sums->len - total_bytes) >> 8900b246afaSJeff Mahoney fs_info->sb->s_blocksize_bits; 891f51a4a18SMiao Xie ins_size *= csum_size; 892f51a4a18SMiao Xie ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item, 893f51a4a18SMiao Xie ins_size); 894f51a4a18SMiao Xie write_extent_buffer(leaf, sums->sums + index, (unsigned long)item, 895f51a4a18SMiao Xie ins_size); 896aadfeb6eSChris Mason 897f51a4a18SMiao Xie ins_size /= csum_size; 8980b246afaSJeff Mahoney total_bytes += ins_size * fs_info->sectorsize; 899f51a4a18SMiao Xie index += ins_size; 900a6591715SChris Mason 9015caf2a00SChris Mason btrfs_mark_buffer_dirty(path->nodes[0]); 902e6dcd2dcSChris Mason if (total_bytes < sums->len) { 903b3b4aa74SDavid Sterba btrfs_release_path(path); 904b9473439SChris Mason cond_resched(); 905065631f6SChris Mason goto again; 906065631f6SChris Mason } 90753863232SChris Mason out: 9085caf2a00SChris Mason btrfs_free_path(path); 909f254e52cSChris Mason return ret; 91053863232SChris Mason 91153863232SChris Mason fail_unlock: 91253863232SChris Mason goto out; 913f254e52cSChris Mason } 9147ffbb598SFilipe Manana 9159cdc5124SNikolay Borisov void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, 9167ffbb598SFilipe Manana const struct btrfs_path *path, 9177ffbb598SFilipe Manana struct btrfs_file_extent_item *fi, 9187ffbb598SFilipe Manana const bool new_inline, 9197ffbb598SFilipe Manana struct extent_map *em) 9207ffbb598SFilipe Manana { 9213ffbd68cSDavid Sterba struct btrfs_fs_info *fs_info = inode->root->fs_info; 9229cdc5124SNikolay Borisov struct btrfs_root *root = inode->root; 9237ffbb598SFilipe Manana struct extent_buffer *leaf = path->nodes[0]; 9247ffbb598SFilipe Manana const int slot = path->slots[0]; 9257ffbb598SFilipe Manana struct btrfs_key key; 9267ffbb598SFilipe Manana u64 extent_start, extent_end; 9277ffbb598SFilipe Manana u64 bytenr; 9287ffbb598SFilipe Manana u8 type = btrfs_file_extent_type(leaf, fi); 9297ffbb598SFilipe Manana int compress_type = btrfs_file_extent_compression(leaf, fi); 9307ffbb598SFilipe Manana 9310b246afaSJeff Mahoney em->bdev = fs_info->fs_devices->latest_bdev; 9327ffbb598SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot); 9337ffbb598SFilipe Manana extent_start = key.offset; 9347ffbb598SFilipe Manana 9357ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_REG || 9367ffbb598SFilipe Manana type == BTRFS_FILE_EXTENT_PREALLOC) { 9377ffbb598SFilipe Manana extent_end = extent_start + 9387ffbb598SFilipe Manana btrfs_file_extent_num_bytes(leaf, fi); 9397ffbb598SFilipe Manana } else if (type == BTRFS_FILE_EXTENT_INLINE) { 9407ffbb598SFilipe Manana size_t size; 941e41ca589SQu Wenruo size = btrfs_file_extent_ram_bytes(leaf, fi); 942da17066cSJeff Mahoney extent_end = ALIGN(extent_start + size, 9430b246afaSJeff Mahoney fs_info->sectorsize); 9447ffbb598SFilipe Manana } 9457ffbb598SFilipe Manana 9467ffbb598SFilipe Manana em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); 9477ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_REG || 9487ffbb598SFilipe Manana type == BTRFS_FILE_EXTENT_PREALLOC) { 9497ffbb598SFilipe Manana em->start = extent_start; 9507ffbb598SFilipe Manana em->len = extent_end - extent_start; 9517ffbb598SFilipe Manana em->orig_start = extent_start - 9527ffbb598SFilipe Manana btrfs_file_extent_offset(leaf, fi); 9537ffbb598SFilipe Manana em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); 9547ffbb598SFilipe Manana bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); 9557ffbb598SFilipe Manana if (bytenr == 0) { 9567ffbb598SFilipe Manana em->block_start = EXTENT_MAP_HOLE; 9577ffbb598SFilipe Manana return; 9587ffbb598SFilipe Manana } 9597ffbb598SFilipe Manana if (compress_type != BTRFS_COMPRESS_NONE) { 9607ffbb598SFilipe Manana set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 9617ffbb598SFilipe Manana em->compress_type = compress_type; 9627ffbb598SFilipe Manana em->block_start = bytenr; 9637ffbb598SFilipe Manana em->block_len = em->orig_block_len; 9647ffbb598SFilipe Manana } else { 9657ffbb598SFilipe Manana bytenr += btrfs_file_extent_offset(leaf, fi); 9667ffbb598SFilipe Manana em->block_start = bytenr; 9677ffbb598SFilipe Manana em->block_len = em->len; 9687ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_PREALLOC) 9697ffbb598SFilipe Manana set_bit(EXTENT_FLAG_PREALLOC, &em->flags); 9707ffbb598SFilipe Manana } 9717ffbb598SFilipe Manana } else if (type == BTRFS_FILE_EXTENT_INLINE) { 9727ffbb598SFilipe Manana em->block_start = EXTENT_MAP_INLINE; 9737ffbb598SFilipe Manana em->start = extent_start; 9747ffbb598SFilipe Manana em->len = extent_end - extent_start; 9757ffbb598SFilipe Manana /* 9767ffbb598SFilipe Manana * Initialize orig_start and block_len with the same values 9777ffbb598SFilipe Manana * as in inode.c:btrfs_get_extent(). 9787ffbb598SFilipe Manana */ 9797ffbb598SFilipe Manana em->orig_start = EXTENT_MAP_HOLE; 9807ffbb598SFilipe Manana em->block_len = (u64)-1; 9817ffbb598SFilipe Manana if (!new_inline && compress_type != BTRFS_COMPRESS_NONE) { 9827ffbb598SFilipe Manana set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 9837ffbb598SFilipe Manana em->compress_type = compress_type; 9847ffbb598SFilipe Manana } 9857ffbb598SFilipe Manana } else { 9860b246afaSJeff Mahoney btrfs_err(fs_info, 9879cdc5124SNikolay Borisov "unknown file extent item type %d, inode %llu, offset %llu, " 9889cdc5124SNikolay Borisov "root %llu", type, btrfs_ino(inode), extent_start, 9897ffbb598SFilipe Manana root->root_key.objectid); 9907ffbb598SFilipe Manana } 9917ffbb598SFilipe Manana } 992