12e635a27SChris Mason #include <linux/module.h> 21e1d2701SChris Mason #include "ctree.h" 3dee26a9fSChris Mason #include "disk-io.h" 49f5fae2fSChris Mason #include "transaction.h" 51e1d2701SChris Mason 66567e837SChris Mason #define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \ 76567e837SChris Mason sizeof(struct btrfs_item)) / \ 86567e837SChris Mason sizeof(struct btrfs_csum_item)) - 1)) 9b18c6685SChris Mason int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, 10dee26a9fSChris Mason struct btrfs_root *root, 11b18c6685SChris Mason u64 objectid, u64 pos, 12b18c6685SChris Mason u64 offset, u64 num_blocks) 139f5fae2fSChris Mason { 14dee26a9fSChris Mason int ret = 0; 15dee26a9fSChris Mason struct btrfs_file_extent_item *item; 16dee26a9fSChris Mason struct btrfs_key file_key; 175caf2a00SChris Mason struct btrfs_path *path; 18dee26a9fSChris Mason 195caf2a00SChris Mason path = btrfs_alloc_path(); 205caf2a00SChris Mason BUG_ON(!path); 215caf2a00SChris Mason btrfs_init_path(path); 22b18c6685SChris Mason /* 23dee26a9fSChris Mason ret = btrfs_alloc_extent(trans, root, num_blocks, hint_block, 24d0dbc624SChris Mason (u64)-1, &ins); 25b18c6685SChris Mason */ 26dee26a9fSChris Mason BUG_ON(ret); 27dee26a9fSChris Mason file_key.objectid = objectid; 28b18c6685SChris Mason file_key.offset = pos; 29dee26a9fSChris Mason file_key.flags = 0; 30dee26a9fSChris Mason btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); 31dee26a9fSChris Mason 325caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 33dee26a9fSChris Mason sizeof(*item)); 349773a788SChris Mason BUG_ON(ret); 355caf2a00SChris Mason item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], 36dee26a9fSChris Mason struct btrfs_file_extent_item); 37b18c6685SChris Mason btrfs_set_file_extent_disk_blocknr(item, offset); 38b18c6685SChris Mason btrfs_set_file_extent_disk_num_blocks(item, num_blocks); 39dee26a9fSChris Mason btrfs_set_file_extent_offset(item, 0); 40b18c6685SChris Mason btrfs_set_file_extent_num_blocks(item, num_blocks); 4171951f35SChris Mason btrfs_set_file_extent_generation(item, trans->transid); 425caf2a00SChris Mason btrfs_mark_buffer_dirty(path->nodes[0]); 435caf2a00SChris Mason btrfs_release_path(root, path); 445caf2a00SChris Mason btrfs_free_path(path); 459f5fae2fSChris Mason return 0; 469f5fae2fSChris Mason } 47dee26a9fSChris Mason 48b18c6685SChris Mason struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans, 49b18c6685SChris Mason struct btrfs_root *root, 506567e837SChris Mason struct btrfs_path *path, 51b18c6685SChris Mason u64 objectid, u64 offset, 52b18c6685SChris Mason int cow) 536567e837SChris Mason { 546567e837SChris Mason int ret; 556567e837SChris Mason struct btrfs_key file_key; 566567e837SChris Mason struct btrfs_key found_key; 576567e837SChris Mason struct btrfs_csum_item *item; 586567e837SChris Mason struct btrfs_leaf *leaf; 596567e837SChris Mason u64 csum_offset = 0; 606567e837SChris Mason 616567e837SChris Mason file_key.objectid = objectid; 626567e837SChris Mason file_key.offset = offset; 636567e837SChris Mason file_key.flags = 0; 646567e837SChris Mason btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); 65b18c6685SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); 666567e837SChris Mason if (ret < 0) 676567e837SChris Mason goto fail; 686567e837SChris Mason leaf = btrfs_buffer_leaf(path->nodes[0]); 696567e837SChris Mason if (ret > 0) { 706567e837SChris Mason ret = 1; 71*70b2befdSChris Mason if (path->slots[0] == 0) 726567e837SChris Mason goto fail; 736567e837SChris Mason path->slots[0]--; 746567e837SChris Mason btrfs_disk_key_to_cpu(&found_key, 756567e837SChris Mason &leaf->items[path->slots[0]].key); 766567e837SChris Mason if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || 776567e837SChris Mason found_key.objectid != objectid) { 786567e837SChris Mason goto fail; 796567e837SChris Mason } 806567e837SChris Mason csum_offset = (offset - found_key.offset) >> 816567e837SChris Mason root->fs_info->sb->s_blocksize_bits; 826567e837SChris Mason if (csum_offset >= 836567e837SChris Mason btrfs_item_size(leaf->items + path->slots[0]) / 846567e837SChris Mason sizeof(struct btrfs_csum_item)) { 856567e837SChris Mason goto fail; 866567e837SChris Mason } 876567e837SChris Mason } 886567e837SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 896567e837SChris Mason item += csum_offset; 906567e837SChris Mason return item; 916567e837SChris Mason fail: 926567e837SChris Mason if (ret > 0) 93b18c6685SChris Mason ret = -ENOENT; 946567e837SChris Mason return ERR_PTR(ret); 956567e837SChris Mason } 966567e837SChris Mason 976567e837SChris Mason 98dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, 99dee26a9fSChris Mason struct btrfs_root *root, 100dee26a9fSChris Mason struct btrfs_path *path, u64 objectid, 1019773a788SChris Mason u64 offset, int mod) 102dee26a9fSChris Mason { 103dee26a9fSChris Mason int ret; 104dee26a9fSChris Mason struct btrfs_key file_key; 105dee26a9fSChris Mason int ins_len = mod < 0 ? -1 : 0; 106dee26a9fSChris Mason int cow = mod != 0; 107dee26a9fSChris Mason 108dee26a9fSChris Mason file_key.objectid = objectid; 109*70b2befdSChris Mason file_key.offset = offset; 110dee26a9fSChris Mason file_key.flags = 0; 111dee26a9fSChris Mason btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); 112dee26a9fSChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); 113dee26a9fSChris Mason return ret; 114dee26a9fSChris Mason } 115f254e52cSChris Mason 116f254e52cSChris Mason int btrfs_csum_file_block(struct btrfs_trans_handle *trans, 117f254e52cSChris Mason struct btrfs_root *root, 118f254e52cSChris Mason u64 objectid, u64 offset, 119f254e52cSChris Mason char *data, size_t len) 120f254e52cSChris Mason { 121f254e52cSChris Mason int ret; 122f254e52cSChris Mason struct btrfs_key file_key; 1236567e837SChris Mason struct btrfs_key found_key; 1245caf2a00SChris Mason struct btrfs_path *path; 125f254e52cSChris Mason struct btrfs_csum_item *item; 1266567e837SChris Mason struct btrfs_leaf *leaf; 1276567e837SChris Mason u64 csum_offset; 128f254e52cSChris Mason 1295caf2a00SChris Mason path = btrfs_alloc_path(); 1305caf2a00SChris Mason BUG_ON(!path); 1315caf2a00SChris Mason btrfs_init_path(path); 132b18c6685SChris Mason 133b18c6685SChris Mason item = btrfs_lookup_csum(trans, root, path, objectid, offset, 0); 134b18c6685SChris Mason if (!IS_ERR(item)) 135b18c6685SChris Mason goto found; 136b18c6685SChris Mason btrfs_release_path(root, path); 137f254e52cSChris Mason file_key.objectid = objectid; 138f254e52cSChris Mason file_key.offset = offset; 139f254e52cSChris Mason file_key.flags = 0; 140f254e52cSChris Mason btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); 1416567e837SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 1426567e837SChris Mason sizeof(struct btrfs_csum_item), 1); 1436567e837SChris Mason if (ret < 0) 1446567e837SChris Mason goto fail; 1456567e837SChris Mason if (ret == 0) { 146b18c6685SChris Mason BUG(); 1476567e837SChris Mason } 1486567e837SChris Mason if (path->slots[0] == 0) { 1496567e837SChris Mason btrfs_release_path(root, path); 1506567e837SChris Mason goto insert; 1516567e837SChris Mason } 1526567e837SChris Mason path->slots[0]--; 1536567e837SChris Mason leaf = btrfs_buffer_leaf(path->nodes[0]); 1546567e837SChris Mason btrfs_disk_key_to_cpu(&found_key, &leaf->items[path->slots[0]].key); 1556567e837SChris Mason csum_offset = (offset - found_key.offset) >> 1566567e837SChris Mason root->fs_info->sb->s_blocksize_bits; 1576567e837SChris Mason if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || 1586567e837SChris Mason found_key.objectid != objectid || 1596567e837SChris Mason csum_offset >= MAX_CSUM_ITEMS(root)) { 1606567e837SChris Mason btrfs_release_path(root, path); 1616567e837SChris Mason goto insert; 1626567e837SChris Mason } 1636567e837SChris Mason if (csum_offset >= btrfs_item_size(leaf->items + path->slots[0]) / 1646567e837SChris Mason sizeof(struct btrfs_csum_item)) { 1656567e837SChris Mason ret = btrfs_extend_item(trans, root, path, 1666567e837SChris Mason sizeof(struct btrfs_csum_item)); 1676567e837SChris Mason BUG_ON(ret); 1686567e837SChris Mason goto csum; 1696567e837SChris Mason } 1706567e837SChris Mason 1716567e837SChris Mason insert: 1726567e837SChris Mason csum_offset = 0; 1735caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 1746567e837SChris Mason sizeof(struct btrfs_csum_item)); 175f254e52cSChris Mason if (ret != 0 && ret != -EEXIST) 176f254e52cSChris Mason goto fail; 1776567e837SChris Mason csum: 1785caf2a00SChris Mason item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], 179f254e52cSChris Mason struct btrfs_csum_item); 180f254e52cSChris Mason ret = 0; 1816567e837SChris Mason item += csum_offset; 182b18c6685SChris Mason found: 183f254e52cSChris Mason ret = btrfs_csum_data(root, data, len, item->csum); 1845caf2a00SChris Mason btrfs_mark_buffer_dirty(path->nodes[0]); 185f254e52cSChris Mason fail: 1865caf2a00SChris Mason btrfs_release_path(root, path); 1875caf2a00SChris Mason btrfs_free_path(path); 188f254e52cSChris Mason return ret; 189f254e52cSChris Mason } 190f254e52cSChris Mason 191f254e52cSChris Mason int btrfs_csum_verify_file_block(struct btrfs_root *root, 192f254e52cSChris Mason u64 objectid, u64 offset, 193f254e52cSChris Mason char *data, size_t len) 194f254e52cSChris Mason { 195f254e52cSChris Mason int ret; 196f254e52cSChris Mason struct btrfs_key file_key; 1975caf2a00SChris Mason struct btrfs_path *path; 198f254e52cSChris Mason struct btrfs_csum_item *item; 199f254e52cSChris Mason char result[BTRFS_CSUM_SIZE]; 200f254e52cSChris Mason 2015caf2a00SChris Mason path = btrfs_alloc_path(); 2025caf2a00SChris Mason BUG_ON(!path); 2035caf2a00SChris Mason btrfs_init_path(path); 204f254e52cSChris Mason file_key.objectid = objectid; 205f254e52cSChris Mason file_key.offset = offset; 206f254e52cSChris Mason file_key.flags = 0; 207f254e52cSChris Mason btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); 2082da566edSChris Mason mutex_lock(&root->fs_info->fs_mutex); 2096567e837SChris Mason 210b18c6685SChris Mason item = btrfs_lookup_csum(NULL, root, path, objectid, offset, 0); 2116567e837SChris Mason if (IS_ERR(item)) { 2126567e837SChris Mason ret = PTR_ERR(item); 213f254e52cSChris Mason goto fail; 2146567e837SChris Mason } 2156567e837SChris Mason 216f254e52cSChris Mason ret = btrfs_csum_data(root, data, len, result); 217f254e52cSChris Mason WARN_ON(ret); 218f254e52cSChris Mason if (memcmp(result, item->csum, BTRFS_CSUM_SIZE)) 219f254e52cSChris Mason ret = 1; 220f254e52cSChris Mason fail: 2215caf2a00SChris Mason btrfs_release_path(root, path); 2225caf2a00SChris Mason btrfs_free_path(path); 2232da566edSChris Mason mutex_unlock(&root->fs_info->fs_mutex); 224f254e52cSChris Mason return ret; 225f254e52cSChris Mason } 226f254e52cSChris Mason 227