12e635a27SChris Mason #include <linux/module.h> 21e1d2701SChris Mason #include "ctree.h" 3dee26a9fSChris Mason #include "disk-io.h" 49f5fae2fSChris Mason #include "transaction.h" 5*1de037a4SChris Mason #include "print-tree.h" 61e1d2701SChris Mason 76567e837SChris Mason #define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \ 8a429e513SChris Mason sizeof(struct btrfs_item) * 2) / \ 9509659cdSChris Mason BTRFS_CRC32_SIZE) - 1)) 10b18c6685SChris Mason int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, 11dee26a9fSChris Mason struct btrfs_root *root, 12b18c6685SChris Mason u64 objectid, u64 pos, 133a686375SChris Mason u64 offset, u64 disk_num_blocks, 143a686375SChris Mason u64 num_blocks) 159f5fae2fSChris Mason { 16dee26a9fSChris Mason int ret = 0; 17dee26a9fSChris Mason struct btrfs_file_extent_item *item; 18dee26a9fSChris Mason struct btrfs_key file_key; 195caf2a00SChris Mason struct btrfs_path *path; 20dee26a9fSChris Mason 215caf2a00SChris Mason path = btrfs_alloc_path(); 225caf2a00SChris Mason BUG_ON(!path); 235caf2a00SChris Mason btrfs_init_path(path); 24dee26a9fSChris Mason file_key.objectid = objectid; 25b18c6685SChris Mason file_key.offset = pos; 26dee26a9fSChris Mason file_key.flags = 0; 27dee26a9fSChris Mason btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); 28dee26a9fSChris Mason 295caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 30dee26a9fSChris Mason sizeof(*item)); 31*1de037a4SChris Mason if (ret) { 32*1de037a4SChris Mason printk("failed to insert %Lu %Lu ret %d\n", objectid, pos, ret); 33*1de037a4SChris Mason btrfs_print_leaf(root, btrfs_buffer_leaf(path->nodes[0])); 34*1de037a4SChris Mason } 359773a788SChris Mason BUG_ON(ret); 365caf2a00SChris Mason item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], 37dee26a9fSChris Mason struct btrfs_file_extent_item); 38b18c6685SChris Mason btrfs_set_file_extent_disk_blocknr(item, offset); 393a686375SChris Mason btrfs_set_file_extent_disk_num_blocks(item, disk_num_blocks); 40dee26a9fSChris Mason btrfs_set_file_extent_offset(item, 0); 41b18c6685SChris Mason btrfs_set_file_extent_num_blocks(item, num_blocks); 4271951f35SChris Mason btrfs_set_file_extent_generation(item, trans->transid); 43236454dfSChris Mason btrfs_set_file_extent_type(item, BTRFS_FILE_EXTENT_REG); 445caf2a00SChris Mason btrfs_mark_buffer_dirty(path->nodes[0]); 45a429e513SChris Mason 465caf2a00SChris Mason btrfs_release_path(root, path); 475caf2a00SChris Mason btrfs_free_path(path); 489f5fae2fSChris Mason return 0; 499f5fae2fSChris Mason } 50dee26a9fSChris Mason 51b18c6685SChris Mason struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans, 52b18c6685SChris Mason struct btrfs_root *root, 536567e837SChris Mason struct btrfs_path *path, 54b18c6685SChris Mason u64 objectid, u64 offset, 55b18c6685SChris Mason int cow) 566567e837SChris Mason { 576567e837SChris Mason int ret; 586567e837SChris Mason struct btrfs_key file_key; 596567e837SChris Mason struct btrfs_key found_key; 606567e837SChris Mason struct btrfs_csum_item *item; 616567e837SChris Mason struct btrfs_leaf *leaf; 626567e837SChris Mason u64 csum_offset = 0; 63a429e513SChris Mason int csums_in_item; 646567e837SChris Mason 656567e837SChris Mason file_key.objectid = objectid; 666567e837SChris Mason file_key.offset = offset; 676567e837SChris Mason file_key.flags = 0; 686567e837SChris Mason btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); 69b18c6685SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); 706567e837SChris Mason if (ret < 0) 716567e837SChris Mason goto fail; 726567e837SChris Mason leaf = btrfs_buffer_leaf(path->nodes[0]); 736567e837SChris Mason if (ret > 0) { 746567e837SChris Mason ret = 1; 7570b2befdSChris Mason if (path->slots[0] == 0) 766567e837SChris Mason goto fail; 776567e837SChris Mason path->slots[0]--; 786567e837SChris Mason btrfs_disk_key_to_cpu(&found_key, 796567e837SChris Mason &leaf->items[path->slots[0]].key); 806567e837SChris Mason if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || 816567e837SChris Mason found_key.objectid != objectid) { 826567e837SChris Mason goto fail; 836567e837SChris Mason } 846567e837SChris Mason csum_offset = (offset - found_key.offset) >> 856567e837SChris Mason root->fs_info->sb->s_blocksize_bits; 86a429e513SChris Mason csums_in_item = btrfs_item_size(leaf->items + path->slots[0]); 87509659cdSChris Mason csums_in_item /= BTRFS_CRC32_SIZE; 88a429e513SChris Mason 89a429e513SChris Mason if (csum_offset >= csums_in_item) { 90a429e513SChris Mason ret = -EFBIG; 916567e837SChris Mason goto fail; 926567e837SChris Mason } 936567e837SChris Mason } 946567e837SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 95509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 96509659cdSChris Mason csum_offset * BTRFS_CRC32_SIZE); 976567e837SChris Mason return item; 986567e837SChris Mason fail: 996567e837SChris Mason if (ret > 0) 100b18c6685SChris Mason ret = -ENOENT; 1016567e837SChris Mason return ERR_PTR(ret); 1026567e837SChris Mason } 1036567e837SChris Mason 1046567e837SChris Mason 105dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, 106dee26a9fSChris Mason struct btrfs_root *root, 107dee26a9fSChris Mason struct btrfs_path *path, u64 objectid, 1089773a788SChris Mason u64 offset, int mod) 109dee26a9fSChris Mason { 110dee26a9fSChris Mason int ret; 111dee26a9fSChris Mason struct btrfs_key file_key; 112dee26a9fSChris Mason int ins_len = mod < 0 ? -1 : 0; 113dee26a9fSChris Mason int cow = mod != 0; 114dee26a9fSChris Mason 115dee26a9fSChris Mason file_key.objectid = objectid; 11670b2befdSChris Mason file_key.offset = offset; 117dee26a9fSChris Mason file_key.flags = 0; 118dee26a9fSChris Mason btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); 119dee26a9fSChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); 120dee26a9fSChris Mason return ret; 121dee26a9fSChris Mason } 122f254e52cSChris Mason 123f254e52cSChris Mason int btrfs_csum_file_block(struct btrfs_trans_handle *trans, 124f254e52cSChris Mason struct btrfs_root *root, 125f254e52cSChris Mason u64 objectid, u64 offset, 126f254e52cSChris Mason char *data, size_t len) 127f254e52cSChris Mason { 128f254e52cSChris Mason int ret; 129f254e52cSChris Mason struct btrfs_key file_key; 1306567e837SChris Mason struct btrfs_key found_key; 1315caf2a00SChris Mason struct btrfs_path *path; 132f254e52cSChris Mason struct btrfs_csum_item *item; 1336567e837SChris Mason struct btrfs_leaf *leaf; 1346567e837SChris Mason u64 csum_offset; 135f254e52cSChris Mason 1365caf2a00SChris Mason path = btrfs_alloc_path(); 1375caf2a00SChris Mason BUG_ON(!path); 138b18c6685SChris Mason 139f254e52cSChris Mason file_key.objectid = objectid; 140f254e52cSChris Mason file_key.offset = offset; 141f254e52cSChris Mason file_key.flags = 0; 142f254e52cSChris Mason btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); 143a429e513SChris Mason 144a429e513SChris Mason item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1); 145a429e513SChris Mason if (!IS_ERR(item)) 146a429e513SChris Mason goto found; 147a429e513SChris Mason ret = PTR_ERR(item); 148a429e513SChris Mason if (ret == -EFBIG) { 149a429e513SChris Mason u32 item_size; 150a429e513SChris Mason /* we found one, but it isn't big enough yet */ 151a429e513SChris Mason leaf = btrfs_buffer_leaf(path->nodes[0]); 152a429e513SChris Mason item_size = btrfs_item_size(leaf->items + path->slots[0]); 153509659cdSChris Mason if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) { 154a429e513SChris Mason /* already at max size, make a new one */ 155a429e513SChris Mason goto insert; 156a429e513SChris Mason } 157a429e513SChris Mason } else { 158a429e513SChris Mason /* we didn't find a csum item, insert one */ 159a429e513SChris Mason goto insert; 160a429e513SChris Mason } 161a429e513SChris Mason 162a429e513SChris Mason /* 163a429e513SChris Mason * at this point, we know the tree has an item, but it isn't big 164a429e513SChris Mason * enough yet to put our csum in. Grow it 165a429e513SChris Mason */ 166a429e513SChris Mason btrfs_release_path(root, path); 1676567e837SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 168509659cdSChris Mason BTRFS_CRC32_SIZE, 1); 1696567e837SChris Mason if (ret < 0) 1706567e837SChris Mason goto fail; 1716567e837SChris Mason if (ret == 0) { 172b18c6685SChris Mason BUG(); 1736567e837SChris Mason } 1746567e837SChris Mason if (path->slots[0] == 0) { 1756567e837SChris Mason goto insert; 1766567e837SChris Mason } 1776567e837SChris Mason path->slots[0]--; 1786567e837SChris Mason leaf = btrfs_buffer_leaf(path->nodes[0]); 1796567e837SChris Mason btrfs_disk_key_to_cpu(&found_key, &leaf->items[path->slots[0]].key); 1806567e837SChris Mason csum_offset = (offset - found_key.offset) >> 1816567e837SChris Mason root->fs_info->sb->s_blocksize_bits; 1826567e837SChris Mason if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || 1836567e837SChris Mason found_key.objectid != objectid || 1846567e837SChris Mason csum_offset >= MAX_CSUM_ITEMS(root)) { 1856567e837SChris Mason goto insert; 1866567e837SChris Mason } 1876567e837SChris Mason if (csum_offset >= btrfs_item_size(leaf->items + path->slots[0]) / 188509659cdSChris Mason BTRFS_CRC32_SIZE) { 189509659cdSChris Mason u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE; 190a429e513SChris Mason diff = diff - btrfs_item_size(leaf->items + path->slots[0]); 1913a686375SChris Mason if (diff != BTRFS_CRC32_SIZE) 1923a686375SChris Mason goto insert; 193a429e513SChris Mason ret = btrfs_extend_item(trans, root, path, diff); 1946567e837SChris Mason BUG_ON(ret); 1956567e837SChris Mason goto csum; 1966567e837SChris Mason } 1976567e837SChris Mason 1986567e837SChris Mason insert: 199a429e513SChris Mason btrfs_release_path(root, path); 2006567e837SChris Mason csum_offset = 0; 2015caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 202509659cdSChris Mason BTRFS_CRC32_SIZE); 203a429e513SChris Mason if (ret != 0) { 204a429e513SChris Mason printk("at insert for %Lu %u %Lu ret is %d\n", file_key.objectid, file_key.flags, file_key.offset, ret); 205a429e513SChris Mason WARN_ON(1); 206f254e52cSChris Mason goto fail; 207a429e513SChris Mason } 2086567e837SChris Mason csum: 2095caf2a00SChris Mason item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], 210f254e52cSChris Mason struct btrfs_csum_item); 211f254e52cSChris Mason ret = 0; 212509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 213509659cdSChris Mason csum_offset * BTRFS_CRC32_SIZE); 214b18c6685SChris Mason found: 215509659cdSChris Mason btrfs_check_bounds(&item->csum, BTRFS_CRC32_SIZE, 216509659cdSChris Mason path->nodes[0]->b_data, 217509659cdSChris Mason root->fs_info->sb->s_blocksize); 218509659cdSChris Mason ret = btrfs_csum_data(root, data, len, &item->csum); 2195caf2a00SChris Mason btrfs_mark_buffer_dirty(path->nodes[0]); 220f254e52cSChris Mason fail: 2215caf2a00SChris Mason btrfs_release_path(root, path); 2225caf2a00SChris Mason btrfs_free_path(path); 223f254e52cSChris Mason return ret; 224f254e52cSChris Mason } 225f254e52cSChris Mason 226*1de037a4SChris Mason int btrfs_csum_truncate(struct btrfs_trans_handle *trans, 227*1de037a4SChris Mason struct btrfs_root *root, struct btrfs_path *path, 228*1de037a4SChris Mason u64 isize) 229*1de037a4SChris Mason { 230*1de037a4SChris Mason struct btrfs_key key; 231*1de037a4SChris Mason struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[0]); 232*1de037a4SChris Mason int slot = path->slots[0]; 233*1de037a4SChris Mason int ret; 234*1de037a4SChris Mason u32 new_item_size; 235*1de037a4SChris Mason u64 new_item_span; 236*1de037a4SChris Mason u64 blocks; 237*1de037a4SChris Mason 238*1de037a4SChris Mason btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key); 239*1de037a4SChris Mason if (isize <= key.offset) 240*1de037a4SChris Mason return 0; 241*1de037a4SChris Mason new_item_span = isize - key.offset; 242*1de037a4SChris Mason blocks = (new_item_span + root->blocksize - 1) / root->blocksize; 243*1de037a4SChris Mason new_item_size = blocks * BTRFS_CRC32_SIZE; 244*1de037a4SChris Mason if (new_item_size >= btrfs_item_size(leaf->items + slot)) 245*1de037a4SChris Mason return 0; 246*1de037a4SChris Mason ret = btrfs_truncate_item(trans, root, path, new_item_size); 247*1de037a4SChris Mason BUG_ON(ret); 248*1de037a4SChris Mason return ret; 249*1de037a4SChris Mason } 250*1de037a4SChris Mason 251f254e52cSChris Mason int btrfs_csum_verify_file_block(struct btrfs_root *root, 252f254e52cSChris Mason u64 objectid, u64 offset, 253f254e52cSChris Mason char *data, size_t len) 254f254e52cSChris Mason { 255f254e52cSChris Mason int ret; 256f254e52cSChris Mason struct btrfs_key file_key; 2575caf2a00SChris Mason struct btrfs_path *path; 258f254e52cSChris Mason struct btrfs_csum_item *item; 259509659cdSChris Mason char result[BTRFS_CRC32_SIZE]; 260f254e52cSChris Mason 2615caf2a00SChris Mason path = btrfs_alloc_path(); 2625caf2a00SChris Mason BUG_ON(!path); 2635caf2a00SChris Mason btrfs_init_path(path); 264f254e52cSChris Mason file_key.objectid = objectid; 265f254e52cSChris Mason file_key.offset = offset; 266f254e52cSChris Mason file_key.flags = 0; 267f254e52cSChris Mason btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); 2682da566edSChris Mason mutex_lock(&root->fs_info->fs_mutex); 2696567e837SChris Mason 270b18c6685SChris Mason item = btrfs_lookup_csum(NULL, root, path, objectid, offset, 0); 2716567e837SChris Mason if (IS_ERR(item)) { 2726567e837SChris Mason ret = PTR_ERR(item); 273a429e513SChris Mason /* a csum that isn't present is a preallocated region. */ 274a429e513SChris Mason if (ret == -ENOENT || ret == -EFBIG) 2753a686375SChris Mason ret = -ENOENT; 276f254e52cSChris Mason goto fail; 2776567e837SChris Mason } 2786567e837SChris Mason 279f254e52cSChris Mason ret = btrfs_csum_data(root, data, len, result); 280f254e52cSChris Mason WARN_ON(ret); 281509659cdSChris Mason if (memcmp(result, &item->csum, BTRFS_CRC32_SIZE)) 282f254e52cSChris Mason ret = 1; 283f254e52cSChris Mason fail: 2845caf2a00SChris Mason btrfs_release_path(root, path); 2855caf2a00SChris Mason btrfs_free_path(path); 2862da566edSChris Mason mutex_unlock(&root->fs_info->fs_mutex); 287f254e52cSChris Mason return ret; 288f254e52cSChris Mason } 289f254e52cSChris Mason 290