16cbd5570SChris Mason /* 26cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 19065631f6SChris Mason #include <linux/bio.h> 205a0e3ad6STejun Heo #include <linux/slab.h> 21065631f6SChris Mason #include <linux/pagemap.h> 22065631f6SChris Mason #include <linux/highmem.h> 231e1d2701SChris Mason #include "ctree.h" 24dee26a9fSChris Mason #include "disk-io.h" 259f5fae2fSChris Mason #include "transaction.h" 261de037a4SChris Mason #include "print-tree.h" 271e1d2701SChris Mason 28607d432dSJosef Bacik #define MAX_CSUM_ITEMS(r, size) ((((BTRFS_LEAF_DATA_SIZE(r) - \ 29a429e513SChris Mason sizeof(struct btrfs_item) * 2) / \ 30607d432dSJosef Bacik size) - 1)) 3107d400a6SYan Zheng 3207d400a6SYan Zheng #define MAX_ORDERED_SUM_BYTES(r) ((PAGE_SIZE - \ 3307d400a6SYan Zheng sizeof(struct btrfs_ordered_sum)) / \ 3407d400a6SYan Zheng sizeof(struct btrfs_sector_sum) * \ 3507d400a6SYan Zheng (r)->sectorsize - (r)->sectorsize) 3607d400a6SYan Zheng 37b18c6685SChris Mason int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, 38dee26a9fSChris Mason struct btrfs_root *root, 39b18c6685SChris Mason u64 objectid, u64 pos, 40f2eb0a24SSage Weil u64 disk_offset, u64 disk_num_bytes, 41c8b97818SChris Mason u64 num_bytes, u64 offset, u64 ram_bytes, 42c8b97818SChris Mason u8 compression, u8 encryption, u16 other_encoding) 439f5fae2fSChris Mason { 44dee26a9fSChris Mason int ret = 0; 45dee26a9fSChris Mason struct btrfs_file_extent_item *item; 46dee26a9fSChris Mason struct btrfs_key file_key; 475caf2a00SChris Mason struct btrfs_path *path; 485f39d397SChris Mason struct extent_buffer *leaf; 49dee26a9fSChris Mason 505caf2a00SChris Mason path = btrfs_alloc_path(); 51db5b493aSTsutomu Itoh if (!path) 52db5b493aSTsutomu Itoh return -ENOMEM; 53dee26a9fSChris Mason file_key.objectid = objectid; 54b18c6685SChris Mason file_key.offset = pos; 55dee26a9fSChris Mason btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); 56dee26a9fSChris Mason 57b9473439SChris Mason path->leave_spinning = 1; 585caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 59dee26a9fSChris Mason sizeof(*item)); 6054aa1f4dSChris Mason if (ret < 0) 6154aa1f4dSChris Mason goto out; 629773a788SChris Mason BUG_ON(ret); 635f39d397SChris Mason leaf = path->nodes[0]; 645f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], 65dee26a9fSChris Mason struct btrfs_file_extent_item); 66f2eb0a24SSage Weil btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset); 67db94535dSChris Mason btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes); 68f2eb0a24SSage Weil btrfs_set_file_extent_offset(leaf, item, offset); 69db94535dSChris Mason btrfs_set_file_extent_num_bytes(leaf, item, num_bytes); 70c8b97818SChris Mason btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes); 715f39d397SChris Mason btrfs_set_file_extent_generation(leaf, item, trans->transid); 725f39d397SChris Mason btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); 73c8b97818SChris Mason btrfs_set_file_extent_compression(leaf, item, compression); 74c8b97818SChris Mason btrfs_set_file_extent_encryption(leaf, item, encryption); 75c8b97818SChris Mason btrfs_set_file_extent_other_encoding(leaf, item, other_encoding); 76c8b97818SChris Mason 775f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 7854aa1f4dSChris Mason out: 795caf2a00SChris Mason btrfs_free_path(path); 8054aa1f4dSChris Mason return ret; 819f5fae2fSChris Mason } 82dee26a9fSChris Mason 83b18c6685SChris Mason struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans, 84b18c6685SChris Mason struct btrfs_root *root, 856567e837SChris Mason struct btrfs_path *path, 86d20f7043SChris Mason u64 bytenr, int cow) 876567e837SChris Mason { 886567e837SChris Mason int ret; 896567e837SChris Mason struct btrfs_key file_key; 906567e837SChris Mason struct btrfs_key found_key; 916567e837SChris Mason struct btrfs_csum_item *item; 925f39d397SChris Mason struct extent_buffer *leaf; 936567e837SChris Mason u64 csum_offset = 0; 94607d432dSJosef Bacik u16 csum_size = 95607d432dSJosef Bacik btrfs_super_csum_size(&root->fs_info->super_copy); 96a429e513SChris Mason int csums_in_item; 976567e837SChris Mason 98d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 99d20f7043SChris Mason file_key.offset = bytenr; 100d20f7043SChris Mason btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY); 101b18c6685SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); 1026567e837SChris Mason if (ret < 0) 1036567e837SChris Mason goto fail; 1045f39d397SChris Mason leaf = path->nodes[0]; 1056567e837SChris Mason if (ret > 0) { 1066567e837SChris Mason ret = 1; 10770b2befdSChris Mason if (path->slots[0] == 0) 1086567e837SChris Mason goto fail; 1096567e837SChris Mason path->slots[0]--; 1105f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 111d20f7043SChris Mason if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY) 1126567e837SChris Mason goto fail; 113d20f7043SChris Mason 114d20f7043SChris Mason csum_offset = (bytenr - found_key.offset) >> 1156567e837SChris Mason root->fs_info->sb->s_blocksize_bits; 1165f39d397SChris Mason csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]); 117607d432dSJosef Bacik csums_in_item /= csum_size; 118a429e513SChris Mason 119a429e513SChris Mason if (csum_offset >= csums_in_item) { 120a429e513SChris Mason ret = -EFBIG; 1216567e837SChris Mason goto fail; 1226567e837SChris Mason } 1236567e837SChris Mason } 1246567e837SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 125509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 126607d432dSJosef Bacik csum_offset * csum_size); 1276567e837SChris Mason return item; 1286567e837SChris Mason fail: 1296567e837SChris Mason if (ret > 0) 130b18c6685SChris Mason ret = -ENOENT; 1316567e837SChris Mason return ERR_PTR(ret); 1326567e837SChris Mason } 1336567e837SChris Mason 1346567e837SChris Mason 135dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, 136dee26a9fSChris Mason struct btrfs_root *root, 137dee26a9fSChris Mason struct btrfs_path *path, u64 objectid, 1389773a788SChris Mason u64 offset, int mod) 139dee26a9fSChris Mason { 140dee26a9fSChris Mason int ret; 141dee26a9fSChris Mason struct btrfs_key file_key; 142dee26a9fSChris Mason int ins_len = mod < 0 ? -1 : 0; 143dee26a9fSChris Mason int cow = mod != 0; 144dee26a9fSChris Mason 145dee26a9fSChris Mason file_key.objectid = objectid; 14670b2befdSChris Mason file_key.offset = offset; 147dee26a9fSChris Mason btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); 148dee26a9fSChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); 149dee26a9fSChris Mason return ret; 150dee26a9fSChris Mason } 151f254e52cSChris Mason 15217d217feSYan Zheng 1534b46fce2SJosef Bacik static int __btrfs_lookup_bio_sums(struct btrfs_root *root, 1544b46fce2SJosef Bacik struct inode *inode, struct bio *bio, 1554b46fce2SJosef Bacik u64 logical_offset, u32 *dst, int dio) 15661b49440SChris Mason { 15761b49440SChris Mason u32 sum; 15861b49440SChris Mason struct bio_vec *bvec = bio->bi_io_vec; 15961b49440SChris Mason int bio_index = 0; 1604b46fce2SJosef Bacik u64 offset = 0; 16161b49440SChris Mason u64 item_start_offset = 0; 16261b49440SChris Mason u64 item_last_offset = 0; 163d20f7043SChris Mason u64 disk_bytenr; 16461b49440SChris Mason u32 diff; 165607d432dSJosef Bacik u16 csum_size = 166607d432dSJosef Bacik btrfs_super_csum_size(&root->fs_info->super_copy); 16761b49440SChris Mason int ret; 16861b49440SChris Mason struct btrfs_path *path; 16961b49440SChris Mason struct btrfs_csum_item *item = NULL; 17061b49440SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 17161b49440SChris Mason 17261b49440SChris Mason path = btrfs_alloc_path(); 173c2db1073STsutomu Itoh if (!path) 174c2db1073STsutomu Itoh return -ENOMEM; 1754d1b5fb4SChris Mason if (bio->bi_size > PAGE_CACHE_SIZE * 8) 1764d1b5fb4SChris Mason path->reada = 2; 17761b49440SChris Mason 17861b49440SChris Mason WARN_ON(bio->bi_vcnt <= 0); 17961b49440SChris Mason 180d20f7043SChris Mason disk_bytenr = (u64)bio->bi_sector << 9; 1814b46fce2SJosef Bacik if (dio) 1824b46fce2SJosef Bacik offset = logical_offset; 18361b49440SChris Mason while (bio_index < bio->bi_vcnt) { 1844b46fce2SJosef Bacik if (!dio) 18561b49440SChris Mason offset = page_offset(bvec->bv_page) + bvec->bv_offset; 186d20f7043SChris Mason ret = btrfs_find_ordered_sum(inode, offset, disk_bytenr, &sum); 18761b49440SChris Mason if (ret == 0) 18861b49440SChris Mason goto found; 18961b49440SChris Mason 190d20f7043SChris Mason if (!item || disk_bytenr < item_start_offset || 191d20f7043SChris Mason disk_bytenr >= item_last_offset) { 19261b49440SChris Mason struct btrfs_key found_key; 19361b49440SChris Mason u32 item_size; 19461b49440SChris Mason 19561b49440SChris Mason if (item) 19661b49440SChris Mason btrfs_release_path(root, path); 197d20f7043SChris Mason item = btrfs_lookup_csum(NULL, root->fs_info->csum_root, 198d20f7043SChris Mason path, disk_bytenr, 0); 19961b49440SChris Mason if (IS_ERR(item)) { 20061b49440SChris Mason ret = PTR_ERR(item); 20161b49440SChris Mason if (ret == -ENOENT || ret == -EFBIG) 20261b49440SChris Mason ret = 0; 20361b49440SChris Mason sum = 0; 20417d217feSYan Zheng if (BTRFS_I(inode)->root->root_key.objectid == 20517d217feSYan Zheng BTRFS_DATA_RELOC_TREE_OBJECTID) { 20617d217feSYan Zheng set_extent_bits(io_tree, offset, 20717d217feSYan Zheng offset + bvec->bv_len - 1, 20817d217feSYan Zheng EXTENT_NODATASUM, GFP_NOFS); 20917d217feSYan Zheng } else { 210d397712bSChris Mason printk(KERN_INFO "btrfs no csum found " 211d397712bSChris Mason "for inode %lu start %llu\n", 212d397712bSChris Mason inode->i_ino, 21361b49440SChris Mason (unsigned long long)offset); 21417d217feSYan Zheng } 2156dab8157SChris Mason item = NULL; 21639be25cdSChris Mason btrfs_release_path(root, path); 21761b49440SChris Mason goto found; 21861b49440SChris Mason } 21961b49440SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key, 22061b49440SChris Mason path->slots[0]); 22161b49440SChris Mason 22261b49440SChris Mason item_start_offset = found_key.offset; 22361b49440SChris Mason item_size = btrfs_item_size_nr(path->nodes[0], 22461b49440SChris Mason path->slots[0]); 22561b49440SChris Mason item_last_offset = item_start_offset + 226607d432dSJosef Bacik (item_size / csum_size) * 22761b49440SChris Mason root->sectorsize; 22861b49440SChris Mason item = btrfs_item_ptr(path->nodes[0], path->slots[0], 22961b49440SChris Mason struct btrfs_csum_item); 23061b49440SChris Mason } 23161b49440SChris Mason /* 23261b49440SChris Mason * this byte range must be able to fit inside 23361b49440SChris Mason * a single leaf so it will also fit inside a u32 23461b49440SChris Mason */ 235d20f7043SChris Mason diff = disk_bytenr - item_start_offset; 23661b49440SChris Mason diff = diff / root->sectorsize; 237607d432dSJosef Bacik diff = diff * csum_size; 23861b49440SChris Mason 23961b49440SChris Mason read_extent_buffer(path->nodes[0], &sum, 2403de9d6b6SChris Mason ((unsigned long)item) + diff, 241607d432dSJosef Bacik csum_size); 24261b49440SChris Mason found: 243d20f7043SChris Mason if (dst) 244d20f7043SChris Mason *dst++ = sum; 245d20f7043SChris Mason else 24661b49440SChris Mason set_state_private(io_tree, offset, sum); 247d20f7043SChris Mason disk_bytenr += bvec->bv_len; 2484b46fce2SJosef Bacik offset += bvec->bv_len; 24961b49440SChris Mason bio_index++; 25061b49440SChris Mason bvec++; 25161b49440SChris Mason } 25261b49440SChris Mason btrfs_free_path(path); 25361b49440SChris Mason return 0; 25461b49440SChris Mason } 25561b49440SChris Mason 2564b46fce2SJosef Bacik int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode, 2574b46fce2SJosef Bacik struct bio *bio, u32 *dst) 2584b46fce2SJosef Bacik { 2594b46fce2SJosef Bacik return __btrfs_lookup_bio_sums(root, inode, bio, 0, dst, 0); 2604b46fce2SJosef Bacik } 2614b46fce2SJosef Bacik 2624b46fce2SJosef Bacik int btrfs_lookup_bio_sums_dio(struct btrfs_root *root, struct inode *inode, 2634b46fce2SJosef Bacik struct bio *bio, u64 offset, u32 *dst) 2644b46fce2SJosef Bacik { 2654b46fce2SJosef Bacik return __btrfs_lookup_bio_sums(root, inode, bio, offset, dst, 1); 2664b46fce2SJosef Bacik } 2674b46fce2SJosef Bacik 26817d217feSYan Zheng int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, 26917d217feSYan Zheng struct list_head *list) 27017d217feSYan Zheng { 27117d217feSYan Zheng struct btrfs_key key; 27217d217feSYan Zheng struct btrfs_path *path; 27317d217feSYan Zheng struct extent_buffer *leaf; 27417d217feSYan Zheng struct btrfs_ordered_sum *sums; 27517d217feSYan Zheng struct btrfs_sector_sum *sector_sum; 27617d217feSYan Zheng struct btrfs_csum_item *item; 27717d217feSYan Zheng unsigned long offset; 27817d217feSYan Zheng int ret; 27917d217feSYan Zheng size_t size; 28017d217feSYan Zheng u64 csum_end; 28117d217feSYan Zheng u16 csum_size = btrfs_super_csum_size(&root->fs_info->super_copy); 28217d217feSYan Zheng 28317d217feSYan Zheng path = btrfs_alloc_path(); 28417d217feSYan Zheng BUG_ON(!path); 28517d217feSYan Zheng 28617d217feSYan Zheng key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 28717d217feSYan Zheng key.offset = start; 28817d217feSYan Zheng key.type = BTRFS_EXTENT_CSUM_KEY; 28917d217feSYan Zheng 29007d400a6SYan Zheng ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 29117d217feSYan Zheng if (ret < 0) 29217d217feSYan Zheng goto fail; 29317d217feSYan Zheng if (ret > 0 && path->slots[0] > 0) { 29417d217feSYan Zheng leaf = path->nodes[0]; 29517d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); 29617d217feSYan Zheng if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID && 29717d217feSYan Zheng key.type == BTRFS_EXTENT_CSUM_KEY) { 29817d217feSYan Zheng offset = (start - key.offset) >> 29917d217feSYan Zheng root->fs_info->sb->s_blocksize_bits; 30017d217feSYan Zheng if (offset * csum_size < 30117d217feSYan Zheng btrfs_item_size_nr(leaf, path->slots[0] - 1)) 30217d217feSYan Zheng path->slots[0]--; 30317d217feSYan Zheng } 30417d217feSYan Zheng } 30517d217feSYan Zheng 30617d217feSYan Zheng while (start <= end) { 30717d217feSYan Zheng leaf = path->nodes[0]; 30817d217feSYan Zheng if (path->slots[0] >= btrfs_header_nritems(leaf)) { 30907d400a6SYan Zheng ret = btrfs_next_leaf(root, path); 31017d217feSYan Zheng if (ret < 0) 31117d217feSYan Zheng goto fail; 31217d217feSYan Zheng if (ret > 0) 31317d217feSYan Zheng break; 31417d217feSYan Zheng leaf = path->nodes[0]; 31517d217feSYan Zheng } 31617d217feSYan Zheng 31717d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 31817d217feSYan Zheng if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 31917d217feSYan Zheng key.type != BTRFS_EXTENT_CSUM_KEY) 32017d217feSYan Zheng break; 32117d217feSYan Zheng 32217d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 32317d217feSYan Zheng if (key.offset > end) 32417d217feSYan Zheng break; 32517d217feSYan Zheng 32617d217feSYan Zheng if (key.offset > start) 32717d217feSYan Zheng start = key.offset; 32817d217feSYan Zheng 32917d217feSYan Zheng size = btrfs_item_size_nr(leaf, path->slots[0]); 33017d217feSYan Zheng csum_end = key.offset + (size / csum_size) * root->sectorsize; 33187b29b20SYan Zheng if (csum_end <= start) { 33287b29b20SYan Zheng path->slots[0]++; 33387b29b20SYan Zheng continue; 33487b29b20SYan Zheng } 33517d217feSYan Zheng 33607d400a6SYan Zheng csum_end = min(csum_end, end + 1); 33707d400a6SYan Zheng item = btrfs_item_ptr(path->nodes[0], path->slots[0], 33807d400a6SYan Zheng struct btrfs_csum_item); 33907d400a6SYan Zheng while (start < csum_end) { 34007d400a6SYan Zheng size = min_t(size_t, csum_end - start, 34107d400a6SYan Zheng MAX_ORDERED_SUM_BYTES(root)); 34207d400a6SYan Zheng sums = kzalloc(btrfs_ordered_sum_size(root, size), 34307d400a6SYan Zheng GFP_NOFS); 34417d217feSYan Zheng BUG_ON(!sums); 34517d217feSYan Zheng 34617d217feSYan Zheng sector_sum = sums->sums; 34717d217feSYan Zheng sums->bytenr = start; 34817d217feSYan Zheng sums->len = size; 34917d217feSYan Zheng 35017d217feSYan Zheng offset = (start - key.offset) >> 35117d217feSYan Zheng root->fs_info->sb->s_blocksize_bits; 35217d217feSYan Zheng offset *= csum_size; 35317d217feSYan Zheng 35417d217feSYan Zheng while (size > 0) { 35507d400a6SYan Zheng read_extent_buffer(path->nodes[0], 35607d400a6SYan Zheng §or_sum->sum, 35707d400a6SYan Zheng ((unsigned long)item) + 35807d400a6SYan Zheng offset, csum_size); 35917d217feSYan Zheng sector_sum->bytenr = start; 36017d217feSYan Zheng 36117d217feSYan Zheng size -= root->sectorsize; 36217d217feSYan Zheng start += root->sectorsize; 36317d217feSYan Zheng offset += csum_size; 36417d217feSYan Zheng sector_sum++; 36517d217feSYan Zheng } 36617d217feSYan Zheng list_add_tail(&sums->list, list); 36707d400a6SYan Zheng } 36817d217feSYan Zheng path->slots[0]++; 36917d217feSYan Zheng } 37017d217feSYan Zheng ret = 0; 37117d217feSYan Zheng fail: 37217d217feSYan Zheng btrfs_free_path(path); 37317d217feSYan Zheng return ret; 37417d217feSYan Zheng } 37517d217feSYan Zheng 3763edf7d33SChris Mason int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode, 377d20f7043SChris Mason struct bio *bio, u64 file_start, int contig) 378e015640fSChris Mason { 379e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums; 380e6dcd2dcSChris Mason struct btrfs_sector_sum *sector_sum; 3813edf7d33SChris Mason struct btrfs_ordered_extent *ordered; 382e015640fSChris Mason char *data; 383e015640fSChris Mason struct bio_vec *bvec = bio->bi_io_vec; 384e015640fSChris Mason int bio_index = 0; 3853edf7d33SChris Mason unsigned long total_bytes = 0; 3863edf7d33SChris Mason unsigned long this_sum_bytes = 0; 3873edf7d33SChris Mason u64 offset; 388d20f7043SChris Mason u64 disk_bytenr; 389e015640fSChris Mason 390e6dcd2dcSChris Mason WARN_ON(bio->bi_vcnt <= 0); 391e6dcd2dcSChris Mason sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS); 392e015640fSChris Mason if (!sums) 393e015640fSChris Mason return -ENOMEM; 3943edf7d33SChris Mason 395ed98b56aSChris Mason sector_sum = sums->sums; 396d20f7043SChris Mason disk_bytenr = (u64)bio->bi_sector << 9; 397e6dcd2dcSChris Mason sums->len = bio->bi_size; 398e6dcd2dcSChris Mason INIT_LIST_HEAD(&sums->list); 399d20f7043SChris Mason 400d20f7043SChris Mason if (contig) 401d20f7043SChris Mason offset = file_start; 402d20f7043SChris Mason else 403d20f7043SChris Mason offset = page_offset(bvec->bv_page) + bvec->bv_offset; 404d20f7043SChris Mason 405d20f7043SChris Mason ordered = btrfs_lookup_ordered_extent(inode, offset); 4063edf7d33SChris Mason BUG_ON(!ordered); 407d20f7043SChris Mason sums->bytenr = ordered->start; 408e015640fSChris Mason 409e015640fSChris Mason while (bio_index < bio->bi_vcnt) { 410d20f7043SChris Mason if (!contig) 4113edf7d33SChris Mason offset = page_offset(bvec->bv_page) + bvec->bv_offset; 412d20f7043SChris Mason 413d20f7043SChris Mason if (!contig && (offset >= ordered->file_offset + ordered->len || 414d20f7043SChris Mason offset < ordered->file_offset)) { 4153edf7d33SChris Mason unsigned long bytes_left; 4163edf7d33SChris Mason sums->len = this_sum_bytes; 4173edf7d33SChris Mason this_sum_bytes = 0; 4183edf7d33SChris Mason btrfs_add_ordered_sum(inode, ordered, sums); 4193edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 4203edf7d33SChris Mason 4213edf7d33SChris Mason bytes_left = bio->bi_size - total_bytes; 4223edf7d33SChris Mason 4233edf7d33SChris Mason sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left), 4243edf7d33SChris Mason GFP_NOFS); 4253edf7d33SChris Mason BUG_ON(!sums); 426ed98b56aSChris Mason sector_sum = sums->sums; 4273edf7d33SChris Mason sums->len = bytes_left; 428d20f7043SChris Mason ordered = btrfs_lookup_ordered_extent(inode, offset); 4293edf7d33SChris Mason BUG_ON(!ordered); 430d20f7043SChris Mason sums->bytenr = ordered->start; 4313edf7d33SChris Mason } 4323edf7d33SChris Mason 433e015640fSChris Mason data = kmap_atomic(bvec->bv_page, KM_USER0); 434e6dcd2dcSChris Mason sector_sum->sum = ~(u32)0; 435e6dcd2dcSChris Mason sector_sum->sum = btrfs_csum_data(root, 436e6dcd2dcSChris Mason data + bvec->bv_offset, 437e6dcd2dcSChris Mason sector_sum->sum, 438e6dcd2dcSChris Mason bvec->bv_len); 439e015640fSChris Mason kunmap_atomic(data, KM_USER0); 440e6dcd2dcSChris Mason btrfs_csum_final(sector_sum->sum, 441e6dcd2dcSChris Mason (char *)§or_sum->sum); 442d20f7043SChris Mason sector_sum->bytenr = disk_bytenr; 443ed98b56aSChris Mason 444e6dcd2dcSChris Mason sector_sum++; 445e015640fSChris Mason bio_index++; 4463edf7d33SChris Mason total_bytes += bvec->bv_len; 4473edf7d33SChris Mason this_sum_bytes += bvec->bv_len; 448d20f7043SChris Mason disk_bytenr += bvec->bv_len; 449d20f7043SChris Mason offset += bvec->bv_len; 450e015640fSChris Mason bvec++; 451e015640fSChris Mason } 452ed98b56aSChris Mason this_sum_bytes = 0; 4533edf7d33SChris Mason btrfs_add_ordered_sum(inode, ordered, sums); 4543edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 455e015640fSChris Mason return 0; 456e015640fSChris Mason } 457e015640fSChris Mason 458459931ecSChris Mason /* 459459931ecSChris Mason * helper function for csum removal, this expects the 460459931ecSChris Mason * key to describe the csum pointed to by the path, and it expects 461459931ecSChris Mason * the csum to overlap the range [bytenr, len] 462459931ecSChris Mason * 463459931ecSChris Mason * The csum should not be entirely contained in the range and the 464459931ecSChris Mason * range should not be entirely contained in the csum. 465459931ecSChris Mason * 466459931ecSChris Mason * This calls btrfs_truncate_item with the correct args based on the 467459931ecSChris Mason * overlap, and fixes up the key as required. 468459931ecSChris Mason */ 469459931ecSChris Mason static noinline int truncate_one_csum(struct btrfs_trans_handle *trans, 470459931ecSChris Mason struct btrfs_root *root, 471459931ecSChris Mason struct btrfs_path *path, 472459931ecSChris Mason struct btrfs_key *key, 473459931ecSChris Mason u64 bytenr, u64 len) 474459931ecSChris Mason { 475459931ecSChris Mason struct extent_buffer *leaf; 476459931ecSChris Mason u16 csum_size = 477459931ecSChris Mason btrfs_super_csum_size(&root->fs_info->super_copy); 478459931ecSChris Mason u64 csum_end; 479459931ecSChris Mason u64 end_byte = bytenr + len; 480459931ecSChris Mason u32 blocksize_bits = root->fs_info->sb->s_blocksize_bits; 481459931ecSChris Mason int ret; 482459931ecSChris Mason 483459931ecSChris Mason leaf = path->nodes[0]; 484459931ecSChris Mason csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; 485459931ecSChris Mason csum_end <<= root->fs_info->sb->s_blocksize_bits; 486459931ecSChris Mason csum_end += key->offset; 487459931ecSChris Mason 488459931ecSChris Mason if (key->offset < bytenr && csum_end <= end_byte) { 489459931ecSChris Mason /* 490459931ecSChris Mason * [ bytenr - len ] 491459931ecSChris Mason * [ ] 492459931ecSChris Mason * [csum ] 493459931ecSChris Mason * A simple truncate off the end of the item 494459931ecSChris Mason */ 495459931ecSChris Mason u32 new_size = (bytenr - key->offset) >> blocksize_bits; 496459931ecSChris Mason new_size *= csum_size; 497459931ecSChris Mason ret = btrfs_truncate_item(trans, root, path, new_size, 1); 498459931ecSChris Mason BUG_ON(ret); 499459931ecSChris Mason } else if (key->offset >= bytenr && csum_end > end_byte && 500459931ecSChris Mason end_byte > key->offset) { 501459931ecSChris Mason /* 502459931ecSChris Mason * [ bytenr - len ] 503459931ecSChris Mason * [ ] 504459931ecSChris Mason * [csum ] 505459931ecSChris Mason * we need to truncate from the beginning of the csum 506459931ecSChris Mason */ 507459931ecSChris Mason u32 new_size = (csum_end - end_byte) >> blocksize_bits; 508459931ecSChris Mason new_size *= csum_size; 509459931ecSChris Mason 510459931ecSChris Mason ret = btrfs_truncate_item(trans, root, path, new_size, 0); 511459931ecSChris Mason BUG_ON(ret); 512459931ecSChris Mason 513459931ecSChris Mason key->offset = end_byte; 514459931ecSChris Mason ret = btrfs_set_item_key_safe(trans, root, path, key); 515459931ecSChris Mason BUG_ON(ret); 516459931ecSChris Mason } else { 517459931ecSChris Mason BUG(); 518459931ecSChris Mason } 519459931ecSChris Mason return 0; 520459931ecSChris Mason } 521459931ecSChris Mason 522459931ecSChris Mason /* 523459931ecSChris Mason * deletes the csum items from the csum tree for a given 524459931ecSChris Mason * range of bytes. 525459931ecSChris Mason */ 526459931ecSChris Mason int btrfs_del_csums(struct btrfs_trans_handle *trans, 527459931ecSChris Mason struct btrfs_root *root, u64 bytenr, u64 len) 528459931ecSChris Mason { 529459931ecSChris Mason struct btrfs_path *path; 530459931ecSChris Mason struct btrfs_key key; 531459931ecSChris Mason u64 end_byte = bytenr + len; 532459931ecSChris Mason u64 csum_end; 533459931ecSChris Mason struct extent_buffer *leaf; 534459931ecSChris Mason int ret; 535459931ecSChris Mason u16 csum_size = 536459931ecSChris Mason btrfs_super_csum_size(&root->fs_info->super_copy); 537459931ecSChris Mason int blocksize_bits = root->fs_info->sb->s_blocksize_bits; 538459931ecSChris Mason 539459931ecSChris Mason root = root->fs_info->csum_root; 540459931ecSChris Mason 541459931ecSChris Mason path = btrfs_alloc_path(); 5422a29edc6Sliubo if (!path) 5432a29edc6Sliubo return -ENOMEM; 544459931ecSChris Mason 545459931ecSChris Mason while (1) { 546459931ecSChris Mason key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 547459931ecSChris Mason key.offset = end_byte - 1; 548459931ecSChris Mason key.type = BTRFS_EXTENT_CSUM_KEY; 549459931ecSChris Mason 550b9473439SChris Mason path->leave_spinning = 1; 551459931ecSChris Mason ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 552459931ecSChris Mason if (ret > 0) { 553459931ecSChris Mason if (path->slots[0] == 0) 554*65a246c5STsutomu Itoh break; 555459931ecSChris Mason path->slots[0]--; 556ad0397a7SJosef Bacik } else if (ret < 0) { 557*65a246c5STsutomu Itoh break; 558459931ecSChris Mason } 559ad0397a7SJosef Bacik 560459931ecSChris Mason leaf = path->nodes[0]; 561459931ecSChris Mason btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 562459931ecSChris Mason 563459931ecSChris Mason if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 564459931ecSChris Mason key.type != BTRFS_EXTENT_CSUM_KEY) { 565459931ecSChris Mason break; 566459931ecSChris Mason } 567459931ecSChris Mason 568459931ecSChris Mason if (key.offset >= end_byte) 569459931ecSChris Mason break; 570459931ecSChris Mason 571459931ecSChris Mason csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; 572459931ecSChris Mason csum_end <<= blocksize_bits; 573459931ecSChris Mason csum_end += key.offset; 574459931ecSChris Mason 575459931ecSChris Mason /* this csum ends before we start, we're done */ 576459931ecSChris Mason if (csum_end <= bytenr) 577459931ecSChris Mason break; 578459931ecSChris Mason 579459931ecSChris Mason /* delete the entire item, it is inside our range */ 580459931ecSChris Mason if (key.offset >= bytenr && csum_end <= end_byte) { 581459931ecSChris Mason ret = btrfs_del_item(trans, root, path); 582*65a246c5STsutomu Itoh if (ret) 583*65a246c5STsutomu Itoh goto out; 584dcbdd4dcSChris Mason if (key.offset == bytenr) 585dcbdd4dcSChris Mason break; 586459931ecSChris Mason } else if (key.offset < bytenr && csum_end > end_byte) { 587459931ecSChris Mason unsigned long offset; 588459931ecSChris Mason unsigned long shift_len; 589459931ecSChris Mason unsigned long item_offset; 590459931ecSChris Mason /* 591459931ecSChris Mason * [ bytenr - len ] 592459931ecSChris Mason * [csum ] 593459931ecSChris Mason * 594459931ecSChris Mason * Our bytes are in the middle of the csum, 595459931ecSChris Mason * we need to split this item and insert a new one. 596459931ecSChris Mason * 597459931ecSChris Mason * But we can't drop the path because the 598459931ecSChris Mason * csum could change, get removed, extended etc. 599459931ecSChris Mason * 600459931ecSChris Mason * The trick here is the max size of a csum item leaves 601459931ecSChris Mason * enough room in the tree block for a single 602459931ecSChris Mason * item header. So, we split the item in place, 603459931ecSChris Mason * adding a new header pointing to the existing 604459931ecSChris Mason * bytes. Then we loop around again and we have 605459931ecSChris Mason * a nicely formed csum item that we can neatly 606459931ecSChris Mason * truncate. 607459931ecSChris Mason */ 608459931ecSChris Mason offset = (bytenr - key.offset) >> blocksize_bits; 609459931ecSChris Mason offset *= csum_size; 610459931ecSChris Mason 611459931ecSChris Mason shift_len = (len >> blocksize_bits) * csum_size; 612459931ecSChris Mason 613459931ecSChris Mason item_offset = btrfs_item_ptr_offset(leaf, 614459931ecSChris Mason path->slots[0]); 615459931ecSChris Mason 616459931ecSChris Mason memset_extent_buffer(leaf, 0, item_offset + offset, 617459931ecSChris Mason shift_len); 618459931ecSChris Mason key.offset = bytenr; 619459931ecSChris Mason 620459931ecSChris Mason /* 621459931ecSChris Mason * btrfs_split_item returns -EAGAIN when the 622459931ecSChris Mason * item changed size or key 623459931ecSChris Mason */ 624459931ecSChris Mason ret = btrfs_split_item(trans, root, path, &key, offset); 625459931ecSChris Mason BUG_ON(ret && ret != -EAGAIN); 626459931ecSChris Mason 627459931ecSChris Mason key.offset = end_byte - 1; 628459931ecSChris Mason } else { 629459931ecSChris Mason ret = truncate_one_csum(trans, root, path, 630459931ecSChris Mason &key, bytenr, len); 631459931ecSChris Mason BUG_ON(ret); 632dcbdd4dcSChris Mason if (key.offset < bytenr) 633dcbdd4dcSChris Mason break; 634459931ecSChris Mason } 635459931ecSChris Mason btrfs_release_path(root, path); 636459931ecSChris Mason } 637*65a246c5STsutomu Itoh ret = 0; 638459931ecSChris Mason out: 639459931ecSChris Mason btrfs_free_path(path); 640*65a246c5STsutomu Itoh return ret; 641459931ecSChris Mason } 642459931ecSChris Mason 643065631f6SChris Mason int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, 644d20f7043SChris Mason struct btrfs_root *root, 645e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums) 646f254e52cSChris Mason { 647d20f7043SChris Mason u64 bytenr; 648f254e52cSChris Mason int ret; 649f254e52cSChris Mason struct btrfs_key file_key; 6506567e837SChris Mason struct btrfs_key found_key; 651065631f6SChris Mason u64 next_offset; 652e6dcd2dcSChris Mason u64 total_bytes = 0; 653065631f6SChris Mason int found_next; 6545caf2a00SChris Mason struct btrfs_path *path; 655f254e52cSChris Mason struct btrfs_csum_item *item; 656065631f6SChris Mason struct btrfs_csum_item *item_end; 657ff79f819SChris Mason struct extent_buffer *leaf = NULL; 6586567e837SChris Mason u64 csum_offset; 659e6dcd2dcSChris Mason struct btrfs_sector_sum *sector_sum; 660f578d4bdSChris Mason u32 nritems; 661f578d4bdSChris Mason u32 ins_size; 6626e92f5e6SChris Mason char *eb_map; 6636e92f5e6SChris Mason char *eb_token; 6646e92f5e6SChris Mason unsigned long map_len; 6656e92f5e6SChris Mason unsigned long map_start; 666607d432dSJosef Bacik u16 csum_size = 667607d432dSJosef Bacik btrfs_super_csum_size(&root->fs_info->super_copy); 6686e92f5e6SChris Mason 6695caf2a00SChris Mason path = btrfs_alloc_path(); 6705caf2a00SChris Mason BUG_ON(!path); 671ed98b56aSChris Mason sector_sum = sums->sums; 672065631f6SChris Mason again: 673065631f6SChris Mason next_offset = (u64)-1; 674065631f6SChris Mason found_next = 0; 675d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 676d20f7043SChris Mason file_key.offset = sector_sum->bytenr; 677d20f7043SChris Mason bytenr = sector_sum->bytenr; 678d20f7043SChris Mason btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY); 679a429e513SChris Mason 680d20f7043SChris Mason item = btrfs_lookup_csum(trans, root, path, sector_sum->bytenr, 1); 681ff79f819SChris Mason if (!IS_ERR(item)) { 682ff79f819SChris Mason leaf = path->nodes[0]; 683639cb586SChris Mason ret = 0; 684a429e513SChris Mason goto found; 685ff79f819SChris Mason } 686a429e513SChris Mason ret = PTR_ERR(item); 6874a500fd1SYan, Zheng if (ret != -EFBIG && ret != -ENOENT) 6884a500fd1SYan, Zheng goto fail_unlock; 6894a500fd1SYan, Zheng 690a429e513SChris Mason if (ret == -EFBIG) { 691a429e513SChris Mason u32 item_size; 692a429e513SChris Mason /* we found one, but it isn't big enough yet */ 6935f39d397SChris Mason leaf = path->nodes[0]; 6945f39d397SChris Mason item_size = btrfs_item_size_nr(leaf, path->slots[0]); 695607d432dSJosef Bacik if ((item_size / csum_size) >= 696607d432dSJosef Bacik MAX_CSUM_ITEMS(root, csum_size)) { 697a429e513SChris Mason /* already at max size, make a new one */ 698a429e513SChris Mason goto insert; 699a429e513SChris Mason } 700a429e513SChris Mason } else { 701f578d4bdSChris Mason int slot = path->slots[0] + 1; 702a429e513SChris Mason /* we didn't find a csum item, insert one */ 703f578d4bdSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 704f578d4bdSChris Mason if (path->slots[0] >= nritems - 1) { 705f578d4bdSChris Mason ret = btrfs_next_leaf(root, path); 706b56baf5bSYan if (ret == 1) 707f578d4bdSChris Mason found_next = 1; 708b56baf5bSYan if (ret != 0) 709f578d4bdSChris Mason goto insert; 710b56baf5bSYan slot = 0; 711f578d4bdSChris Mason } 712f578d4bdSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); 713d20f7043SChris Mason if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 714d20f7043SChris Mason found_key.type != BTRFS_EXTENT_CSUM_KEY) { 715f578d4bdSChris Mason found_next = 1; 716f578d4bdSChris Mason goto insert; 717f578d4bdSChris Mason } 718f578d4bdSChris Mason next_offset = found_key.offset; 719f578d4bdSChris Mason found_next = 1; 720a429e513SChris Mason goto insert; 721a429e513SChris Mason } 722a429e513SChris Mason 723a429e513SChris Mason /* 724a429e513SChris Mason * at this point, we know the tree has an item, but it isn't big 725a429e513SChris Mason * enough yet to put our csum in. Grow it 726a429e513SChris Mason */ 727a429e513SChris Mason btrfs_release_path(root, path); 7286567e837SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 729607d432dSJosef Bacik csum_size, 1); 7306567e837SChris Mason if (ret < 0) 73153863232SChris Mason goto fail_unlock; 732459931ecSChris Mason 733459931ecSChris Mason if (ret > 0) { 734459931ecSChris Mason if (path->slots[0] == 0) 7356567e837SChris Mason goto insert; 7366567e837SChris Mason path->slots[0]--; 737459931ecSChris Mason } 738459931ecSChris Mason 7395f39d397SChris Mason leaf = path->nodes[0]; 7405f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 741d20f7043SChris Mason csum_offset = (bytenr - found_key.offset) >> 7426567e837SChris Mason root->fs_info->sb->s_blocksize_bits; 743459931ecSChris Mason 744d20f7043SChris Mason if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY || 745d20f7043SChris Mason found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 746607d432dSJosef Bacik csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) { 7476567e837SChris Mason goto insert; 7486567e837SChris Mason } 749459931ecSChris Mason 7505f39d397SChris Mason if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) / 751607d432dSJosef Bacik csum_size) { 752607d432dSJosef Bacik u32 diff = (csum_offset + 1) * csum_size; 753459931ecSChris Mason 754459931ecSChris Mason /* 755459931ecSChris Mason * is the item big enough already? we dropped our lock 756459931ecSChris Mason * before and need to recheck 757459931ecSChris Mason */ 758459931ecSChris Mason if (diff < btrfs_item_size_nr(leaf, path->slots[0])) 759459931ecSChris Mason goto csum; 760459931ecSChris Mason 7615f39d397SChris Mason diff = diff - btrfs_item_size_nr(leaf, path->slots[0]); 762d397712bSChris Mason if (diff != csum_size) 7633a686375SChris Mason goto insert; 764459931ecSChris Mason 765a429e513SChris Mason ret = btrfs_extend_item(trans, root, path, diff); 7666567e837SChris Mason BUG_ON(ret); 7676567e837SChris Mason goto csum; 7686567e837SChris Mason } 7696567e837SChris Mason 7706567e837SChris Mason insert: 771a429e513SChris Mason btrfs_release_path(root, path); 7726567e837SChris Mason csum_offset = 0; 773f578d4bdSChris Mason if (found_next) { 774d20f7043SChris Mason u64 tmp = total_bytes + root->sectorsize; 775d20f7043SChris Mason u64 next_sector = sector_sum->bytenr; 776d20f7043SChris Mason struct btrfs_sector_sum *next = sector_sum + 1; 777d20f7043SChris Mason 778d20f7043SChris Mason while (tmp < sums->len) { 779d20f7043SChris Mason if (next_sector + root->sectorsize != next->bytenr) 780d20f7043SChris Mason break; 781d20f7043SChris Mason tmp += root->sectorsize; 782d20f7043SChris Mason next_sector = next->bytenr; 783d20f7043SChris Mason next++; 784d20f7043SChris Mason } 785d20f7043SChris Mason tmp = min(tmp, next_offset - file_key.offset); 786f578d4bdSChris Mason tmp >>= root->fs_info->sb->s_blocksize_bits; 787f578d4bdSChris Mason tmp = max((u64)1, tmp); 788607d432dSJosef Bacik tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size)); 789607d432dSJosef Bacik ins_size = csum_size * tmp; 790f578d4bdSChris Mason } else { 791607d432dSJosef Bacik ins_size = csum_size; 792f578d4bdSChris Mason } 793b9473439SChris Mason path->leave_spinning = 1; 7945caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 795f578d4bdSChris Mason ins_size); 796b9473439SChris Mason path->leave_spinning = 0; 79754aa1f4dSChris Mason if (ret < 0) 79853863232SChris Mason goto fail_unlock; 799a429e513SChris Mason if (ret != 0) { 800a429e513SChris Mason WARN_ON(1); 80153863232SChris Mason goto fail_unlock; 802a429e513SChris Mason } 8036567e837SChris Mason csum: 8045f39d397SChris Mason leaf = path->nodes[0]; 8055f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 806f254e52cSChris Mason ret = 0; 807509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 808607d432dSJosef Bacik csum_offset * csum_size); 809b18c6685SChris Mason found: 810065631f6SChris Mason item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 811065631f6SChris Mason item_end = (struct btrfs_csum_item *)((unsigned char *)item_end + 812065631f6SChris Mason btrfs_item_size_nr(leaf, path->slots[0])); 8136e92f5e6SChris Mason eb_token = NULL; 814e6dcd2dcSChris Mason next_sector: 815aadfeb6eSChris Mason 8166e92f5e6SChris Mason if (!eb_token || 817607d432dSJosef Bacik (unsigned long)item + csum_size >= map_start + map_len) { 8186e92f5e6SChris Mason int err; 8196e92f5e6SChris Mason 8206e92f5e6SChris Mason if (eb_token) 821eb20978fSChris Mason unmap_extent_buffer(leaf, eb_token, KM_USER1); 8226e92f5e6SChris Mason eb_token = NULL; 8236e92f5e6SChris Mason err = map_private_extent_buffer(leaf, (unsigned long)item, 824607d432dSJosef Bacik csum_size, 8256e92f5e6SChris Mason &eb_token, &eb_map, 826eb20978fSChris Mason &map_start, &map_len, KM_USER1); 8276e92f5e6SChris Mason if (err) 8286e92f5e6SChris Mason eb_token = NULL; 8296e92f5e6SChris Mason } 8306e92f5e6SChris Mason if (eb_token) { 8316e92f5e6SChris Mason memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)), 832607d432dSJosef Bacik §or_sum->sum, csum_size); 8336e92f5e6SChris Mason } else { 834e6dcd2dcSChris Mason write_extent_buffer(leaf, §or_sum->sum, 835607d432dSJosef Bacik (unsigned long)item, csum_size); 8366e92f5e6SChris Mason } 8377f3c74fbSChris Mason 838e6dcd2dcSChris Mason total_bytes += root->sectorsize; 839e6dcd2dcSChris Mason sector_sum++; 840e6dcd2dcSChris Mason if (total_bytes < sums->len) { 8416e92f5e6SChris Mason item = (struct btrfs_csum_item *)((char *)item + 842607d432dSJosef Bacik csum_size); 843d20f7043SChris Mason if (item < item_end && bytenr + PAGE_CACHE_SIZE == 844d20f7043SChris Mason sector_sum->bytenr) { 845d20f7043SChris Mason bytenr = sector_sum->bytenr; 846e6dcd2dcSChris Mason goto next_sector; 847065631f6SChris Mason } 8482e1a992eSChris Mason } 8496e92f5e6SChris Mason if (eb_token) { 850eb20978fSChris Mason unmap_extent_buffer(leaf, eb_token, KM_USER1); 8516e92f5e6SChris Mason eb_token = NULL; 8526e92f5e6SChris Mason } 8535caf2a00SChris Mason btrfs_mark_buffer_dirty(path->nodes[0]); 854e6dcd2dcSChris Mason if (total_bytes < sums->len) { 8555caf2a00SChris Mason btrfs_release_path(root, path); 856b9473439SChris Mason cond_resched(); 857065631f6SChris Mason goto again; 858065631f6SChris Mason } 85953863232SChris Mason out: 8605caf2a00SChris Mason btrfs_free_path(path); 861f254e52cSChris Mason return ret; 86253863232SChris Mason 86353863232SChris Mason fail_unlock: 86453863232SChris Mason goto out; 865f254e52cSChris Mason } 866