1*6cbd5570SChris Mason /* 2*6cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 3*6cbd5570SChris Mason * 4*6cbd5570SChris Mason * This program is free software; you can redistribute it and/or 5*6cbd5570SChris Mason * modify it under the terms of the GNU General Public 6*6cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 7*6cbd5570SChris Mason * 8*6cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 9*6cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 10*6cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11*6cbd5570SChris Mason * General Public License for more details. 12*6cbd5570SChris Mason * 13*6cbd5570SChris Mason * You should have received a copy of the GNU General Public 14*6cbd5570SChris Mason * License along with this program; if not, write to the 15*6cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16*6cbd5570SChris Mason * Boston, MA 021110-1307, USA. 17*6cbd5570SChris Mason */ 18*6cbd5570SChris Mason 192e635a27SChris Mason #include <linux/module.h> 201e1d2701SChris Mason #include "ctree.h" 21dee26a9fSChris Mason #include "disk-io.h" 229f5fae2fSChris Mason #include "transaction.h" 231de037a4SChris Mason #include "print-tree.h" 241e1d2701SChris Mason 256567e837SChris Mason #define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \ 26a429e513SChris Mason sizeof(struct btrfs_item) * 2) / \ 27509659cdSChris Mason BTRFS_CRC32_SIZE) - 1)) 28b18c6685SChris Mason int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, 29dee26a9fSChris Mason struct btrfs_root *root, 30b18c6685SChris Mason u64 objectid, u64 pos, 313a686375SChris Mason u64 offset, u64 disk_num_blocks, 323a686375SChris Mason u64 num_blocks) 339f5fae2fSChris Mason { 34dee26a9fSChris Mason int ret = 0; 35dee26a9fSChris Mason struct btrfs_file_extent_item *item; 36dee26a9fSChris Mason struct btrfs_key file_key; 375caf2a00SChris Mason struct btrfs_path *path; 38dee26a9fSChris Mason 395caf2a00SChris Mason path = btrfs_alloc_path(); 405caf2a00SChris Mason BUG_ON(!path); 415caf2a00SChris Mason btrfs_init_path(path); 42dee26a9fSChris Mason file_key.objectid = objectid; 43b18c6685SChris Mason file_key.offset = pos; 44dee26a9fSChris Mason file_key.flags = 0; 45dee26a9fSChris Mason btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); 46dee26a9fSChris Mason 475caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 48dee26a9fSChris Mason sizeof(*item)); 499773a788SChris Mason BUG_ON(ret); 505caf2a00SChris Mason item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], 51dee26a9fSChris Mason struct btrfs_file_extent_item); 52b18c6685SChris Mason btrfs_set_file_extent_disk_blocknr(item, offset); 533a686375SChris Mason btrfs_set_file_extent_disk_num_blocks(item, disk_num_blocks); 54dee26a9fSChris Mason btrfs_set_file_extent_offset(item, 0); 55b18c6685SChris Mason btrfs_set_file_extent_num_blocks(item, num_blocks); 5671951f35SChris Mason btrfs_set_file_extent_generation(item, trans->transid); 57236454dfSChris Mason btrfs_set_file_extent_type(item, BTRFS_FILE_EXTENT_REG); 585caf2a00SChris Mason btrfs_mark_buffer_dirty(path->nodes[0]); 59a429e513SChris Mason 605caf2a00SChris Mason btrfs_release_path(root, path); 615caf2a00SChris Mason btrfs_free_path(path); 629f5fae2fSChris Mason return 0; 639f5fae2fSChris Mason } 64dee26a9fSChris Mason 65b18c6685SChris Mason struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans, 66b18c6685SChris Mason struct btrfs_root *root, 676567e837SChris Mason struct btrfs_path *path, 68b18c6685SChris Mason u64 objectid, u64 offset, 69b18c6685SChris Mason int cow) 706567e837SChris Mason { 716567e837SChris Mason int ret; 726567e837SChris Mason struct btrfs_key file_key; 736567e837SChris Mason struct btrfs_key found_key; 746567e837SChris Mason struct btrfs_csum_item *item; 756567e837SChris Mason struct btrfs_leaf *leaf; 766567e837SChris Mason u64 csum_offset = 0; 77a429e513SChris Mason int csums_in_item; 786567e837SChris Mason 796567e837SChris Mason file_key.objectid = objectid; 806567e837SChris Mason file_key.offset = offset; 816567e837SChris Mason file_key.flags = 0; 826567e837SChris Mason btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); 83b18c6685SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); 846567e837SChris Mason if (ret < 0) 856567e837SChris Mason goto fail; 866567e837SChris Mason leaf = btrfs_buffer_leaf(path->nodes[0]); 876567e837SChris Mason if (ret > 0) { 886567e837SChris Mason ret = 1; 8970b2befdSChris Mason if (path->slots[0] == 0) 906567e837SChris Mason goto fail; 916567e837SChris Mason path->slots[0]--; 926567e837SChris Mason btrfs_disk_key_to_cpu(&found_key, 936567e837SChris Mason &leaf->items[path->slots[0]].key); 946567e837SChris Mason if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || 956567e837SChris Mason found_key.objectid != objectid) { 966567e837SChris Mason goto fail; 976567e837SChris Mason } 986567e837SChris Mason csum_offset = (offset - found_key.offset) >> 996567e837SChris Mason root->fs_info->sb->s_blocksize_bits; 100a429e513SChris Mason csums_in_item = btrfs_item_size(leaf->items + path->slots[0]); 101509659cdSChris Mason csums_in_item /= BTRFS_CRC32_SIZE; 102a429e513SChris Mason 103a429e513SChris Mason if (csum_offset >= csums_in_item) { 104a429e513SChris Mason ret = -EFBIG; 1056567e837SChris Mason goto fail; 1066567e837SChris Mason } 1076567e837SChris Mason } 1086567e837SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 109509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 110509659cdSChris Mason csum_offset * BTRFS_CRC32_SIZE); 1116567e837SChris Mason return item; 1126567e837SChris Mason fail: 1136567e837SChris Mason if (ret > 0) 114b18c6685SChris Mason ret = -ENOENT; 1156567e837SChris Mason return ERR_PTR(ret); 1166567e837SChris Mason } 1176567e837SChris Mason 1186567e837SChris Mason 119dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, 120dee26a9fSChris Mason struct btrfs_root *root, 121dee26a9fSChris Mason struct btrfs_path *path, u64 objectid, 1229773a788SChris Mason u64 offset, int mod) 123dee26a9fSChris Mason { 124dee26a9fSChris Mason int ret; 125dee26a9fSChris Mason struct btrfs_key file_key; 126dee26a9fSChris Mason int ins_len = mod < 0 ? -1 : 0; 127dee26a9fSChris Mason int cow = mod != 0; 128dee26a9fSChris Mason 129dee26a9fSChris Mason file_key.objectid = objectid; 13070b2befdSChris Mason file_key.offset = offset; 131dee26a9fSChris Mason file_key.flags = 0; 132dee26a9fSChris Mason btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); 133dee26a9fSChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); 134dee26a9fSChris Mason return ret; 135dee26a9fSChris Mason } 136f254e52cSChris Mason 137f254e52cSChris Mason int btrfs_csum_file_block(struct btrfs_trans_handle *trans, 138f254e52cSChris Mason struct btrfs_root *root, 139f254e52cSChris Mason u64 objectid, u64 offset, 140f254e52cSChris Mason char *data, size_t len) 141f254e52cSChris Mason { 142f254e52cSChris Mason int ret; 143f254e52cSChris Mason struct btrfs_key file_key; 1446567e837SChris Mason struct btrfs_key found_key; 1455caf2a00SChris Mason struct btrfs_path *path; 146f254e52cSChris Mason struct btrfs_csum_item *item; 1476567e837SChris Mason struct btrfs_leaf *leaf; 1486567e837SChris Mason u64 csum_offset; 149f254e52cSChris Mason 1505caf2a00SChris Mason path = btrfs_alloc_path(); 1515caf2a00SChris Mason BUG_ON(!path); 152b18c6685SChris Mason 153f254e52cSChris Mason file_key.objectid = objectid; 154f254e52cSChris Mason file_key.offset = offset; 155f254e52cSChris Mason file_key.flags = 0; 156f254e52cSChris Mason btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); 157a429e513SChris Mason 158a429e513SChris Mason item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1); 159a429e513SChris Mason if (!IS_ERR(item)) 160a429e513SChris Mason goto found; 161a429e513SChris Mason ret = PTR_ERR(item); 162a429e513SChris Mason if (ret == -EFBIG) { 163a429e513SChris Mason u32 item_size; 164a429e513SChris Mason /* we found one, but it isn't big enough yet */ 165a429e513SChris Mason leaf = btrfs_buffer_leaf(path->nodes[0]); 166a429e513SChris Mason item_size = btrfs_item_size(leaf->items + path->slots[0]); 167509659cdSChris Mason if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) { 168a429e513SChris Mason /* already at max size, make a new one */ 169a429e513SChris Mason goto insert; 170a429e513SChris Mason } 171a429e513SChris Mason } else { 172a429e513SChris Mason /* we didn't find a csum item, insert one */ 173a429e513SChris Mason goto insert; 174a429e513SChris Mason } 175a429e513SChris Mason 176a429e513SChris Mason /* 177a429e513SChris Mason * at this point, we know the tree has an item, but it isn't big 178a429e513SChris Mason * enough yet to put our csum in. Grow it 179a429e513SChris Mason */ 180a429e513SChris Mason btrfs_release_path(root, path); 1816567e837SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 182509659cdSChris Mason BTRFS_CRC32_SIZE, 1); 1836567e837SChris Mason if (ret < 0) 1846567e837SChris Mason goto fail; 1856567e837SChris Mason if (ret == 0) { 186b18c6685SChris Mason BUG(); 1876567e837SChris Mason } 1886567e837SChris Mason if (path->slots[0] == 0) { 1896567e837SChris Mason goto insert; 1906567e837SChris Mason } 1916567e837SChris Mason path->slots[0]--; 1926567e837SChris Mason leaf = btrfs_buffer_leaf(path->nodes[0]); 1936567e837SChris Mason btrfs_disk_key_to_cpu(&found_key, &leaf->items[path->slots[0]].key); 1946567e837SChris Mason csum_offset = (offset - found_key.offset) >> 1956567e837SChris Mason root->fs_info->sb->s_blocksize_bits; 1966567e837SChris Mason if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || 1976567e837SChris Mason found_key.objectid != objectid || 1986567e837SChris Mason csum_offset >= MAX_CSUM_ITEMS(root)) { 1996567e837SChris Mason goto insert; 2006567e837SChris Mason } 2016567e837SChris Mason if (csum_offset >= btrfs_item_size(leaf->items + path->slots[0]) / 202509659cdSChris Mason BTRFS_CRC32_SIZE) { 203509659cdSChris Mason u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE; 204a429e513SChris Mason diff = diff - btrfs_item_size(leaf->items + path->slots[0]); 2053a686375SChris Mason if (diff != BTRFS_CRC32_SIZE) 2063a686375SChris Mason goto insert; 207a429e513SChris Mason ret = btrfs_extend_item(trans, root, path, diff); 2086567e837SChris Mason BUG_ON(ret); 2096567e837SChris Mason goto csum; 2106567e837SChris Mason } 2116567e837SChris Mason 2126567e837SChris Mason insert: 213a429e513SChris Mason btrfs_release_path(root, path); 2146567e837SChris Mason csum_offset = 0; 2155caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 216509659cdSChris Mason BTRFS_CRC32_SIZE); 217a429e513SChris Mason if (ret != 0) { 218a429e513SChris Mason WARN_ON(1); 219f254e52cSChris Mason goto fail; 220a429e513SChris Mason } 2216567e837SChris Mason csum: 2225caf2a00SChris Mason item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], 223f254e52cSChris Mason struct btrfs_csum_item); 224f254e52cSChris Mason ret = 0; 225509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 226509659cdSChris Mason csum_offset * BTRFS_CRC32_SIZE); 227b18c6685SChris Mason found: 228509659cdSChris Mason btrfs_check_bounds(&item->csum, BTRFS_CRC32_SIZE, 229509659cdSChris Mason path->nodes[0]->b_data, 230509659cdSChris Mason root->fs_info->sb->s_blocksize); 231509659cdSChris Mason ret = btrfs_csum_data(root, data, len, &item->csum); 2325caf2a00SChris Mason btrfs_mark_buffer_dirty(path->nodes[0]); 233f254e52cSChris Mason fail: 2345caf2a00SChris Mason btrfs_release_path(root, path); 2355caf2a00SChris Mason btrfs_free_path(path); 236f254e52cSChris Mason return ret; 237f254e52cSChris Mason } 238f254e52cSChris Mason 2391de037a4SChris Mason int btrfs_csum_truncate(struct btrfs_trans_handle *trans, 2401de037a4SChris Mason struct btrfs_root *root, struct btrfs_path *path, 2411de037a4SChris Mason u64 isize) 2421de037a4SChris Mason { 2431de037a4SChris Mason struct btrfs_key key; 2441de037a4SChris Mason struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[0]); 2451de037a4SChris Mason int slot = path->slots[0]; 2461de037a4SChris Mason int ret; 2471de037a4SChris Mason u32 new_item_size; 2481de037a4SChris Mason u64 new_item_span; 2491de037a4SChris Mason u64 blocks; 2501de037a4SChris Mason 2511de037a4SChris Mason btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key); 2521de037a4SChris Mason if (isize <= key.offset) 2531de037a4SChris Mason return 0; 2541de037a4SChris Mason new_item_span = isize - key.offset; 25584f54cfaSChris Mason blocks = (new_item_span + root->blocksize - 1) >> 25684f54cfaSChris Mason root->fs_info->sb->s_blocksize_bits; 2571de037a4SChris Mason new_item_size = blocks * BTRFS_CRC32_SIZE; 2581de037a4SChris Mason if (new_item_size >= btrfs_item_size(leaf->items + slot)) 2591de037a4SChris Mason return 0; 2601de037a4SChris Mason ret = btrfs_truncate_item(trans, root, path, new_item_size); 2611de037a4SChris Mason BUG_ON(ret); 2621de037a4SChris Mason return ret; 2631de037a4SChris Mason } 2641de037a4SChris Mason 265f254e52cSChris Mason int btrfs_csum_verify_file_block(struct btrfs_root *root, 266f254e52cSChris Mason u64 objectid, u64 offset, 267f254e52cSChris Mason char *data, size_t len) 268f254e52cSChris Mason { 269f254e52cSChris Mason int ret; 270f254e52cSChris Mason struct btrfs_key file_key; 2715caf2a00SChris Mason struct btrfs_path *path; 272f254e52cSChris Mason struct btrfs_csum_item *item; 273509659cdSChris Mason char result[BTRFS_CRC32_SIZE]; 274f254e52cSChris Mason 2755caf2a00SChris Mason path = btrfs_alloc_path(); 2765caf2a00SChris Mason BUG_ON(!path); 2775caf2a00SChris Mason btrfs_init_path(path); 278f254e52cSChris Mason file_key.objectid = objectid; 279f254e52cSChris Mason file_key.offset = offset; 280f254e52cSChris Mason file_key.flags = 0; 281f254e52cSChris Mason btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); 2822da566edSChris Mason mutex_lock(&root->fs_info->fs_mutex); 2836567e837SChris Mason 284b18c6685SChris Mason item = btrfs_lookup_csum(NULL, root, path, objectid, offset, 0); 2856567e837SChris Mason if (IS_ERR(item)) { 2866567e837SChris Mason ret = PTR_ERR(item); 287a429e513SChris Mason /* a csum that isn't present is a preallocated region. */ 288a429e513SChris Mason if (ret == -ENOENT || ret == -EFBIG) 2893a686375SChris Mason ret = -ENOENT; 290f254e52cSChris Mason goto fail; 2916567e837SChris Mason } 2926567e837SChris Mason 293f254e52cSChris Mason ret = btrfs_csum_data(root, data, len, result); 294f254e52cSChris Mason WARN_ON(ret); 295509659cdSChris Mason if (memcmp(result, &item->csum, BTRFS_CRC32_SIZE)) 296f254e52cSChris Mason ret = 1; 297f254e52cSChris Mason fail: 2985caf2a00SChris Mason btrfs_release_path(root, path); 2995caf2a00SChris Mason btrfs_free_path(path); 3002da566edSChris Mason mutex_unlock(&root->fs_info->fs_mutex); 301f254e52cSChris Mason return ret; 302f254e52cSChris Mason } 303f254e52cSChris Mason 304