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> 20065631f6SChris Mason #include <linux/pagemap.h> 21065631f6SChris Mason #include <linux/highmem.h> 221e1d2701SChris Mason #include "ctree.h" 23dee26a9fSChris Mason #include "disk-io.h" 249f5fae2fSChris Mason #include "transaction.h" 251de037a4SChris Mason #include "print-tree.h" 261e1d2701SChris Mason 276567e837SChris Mason #define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \ 28a429e513SChris Mason sizeof(struct btrfs_item) * 2) / \ 29509659cdSChris Mason BTRFS_CRC32_SIZE) - 1)) 30b18c6685SChris Mason int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, 31dee26a9fSChris Mason struct btrfs_root *root, 32b18c6685SChris Mason u64 objectid, u64 pos, 33f2eb0a24SSage Weil u64 disk_offset, u64 disk_num_bytes, 34f2eb0a24SSage Weil u64 num_bytes, u64 offset) 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(); 435caf2a00SChris Mason BUG_ON(!path); 44dee26a9fSChris Mason file_key.objectid = objectid; 45b18c6685SChris Mason file_key.offset = pos; 46dee26a9fSChris Mason btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); 47dee26a9fSChris Mason 485caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 49dee26a9fSChris Mason sizeof(*item)); 5054aa1f4dSChris Mason if (ret < 0) 5154aa1f4dSChris Mason goto out; 529773a788SChris Mason BUG_ON(ret); 535f39d397SChris Mason leaf = path->nodes[0]; 545f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], 55dee26a9fSChris Mason struct btrfs_file_extent_item); 56f2eb0a24SSage Weil btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset); 57db94535dSChris Mason btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes); 58f2eb0a24SSage Weil btrfs_set_file_extent_offset(leaf, item, offset); 59db94535dSChris Mason btrfs_set_file_extent_num_bytes(leaf, item, num_bytes); 605f39d397SChris Mason btrfs_set_file_extent_generation(leaf, item, trans->transid); 615f39d397SChris Mason btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); 625f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 6354aa1f4dSChris Mason out: 645caf2a00SChris Mason btrfs_free_path(path); 6554aa1f4dSChris Mason return ret; 669f5fae2fSChris Mason } 67dee26a9fSChris Mason 68b18c6685SChris Mason struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans, 69b18c6685SChris Mason struct btrfs_root *root, 706567e837SChris Mason struct btrfs_path *path, 71b18c6685SChris Mason u64 objectid, u64 offset, 72b18c6685SChris Mason int cow) 736567e837SChris Mason { 746567e837SChris Mason int ret; 756567e837SChris Mason struct btrfs_key file_key; 766567e837SChris Mason struct btrfs_key found_key; 776567e837SChris Mason struct btrfs_csum_item *item; 785f39d397SChris Mason struct extent_buffer *leaf; 796567e837SChris Mason u64 csum_offset = 0; 80a429e513SChris Mason int csums_in_item; 816567e837SChris Mason 826567e837SChris Mason file_key.objectid = objectid; 836567e837SChris Mason file_key.offset = offset; 846567e837SChris Mason btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); 85b18c6685SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); 866567e837SChris Mason if (ret < 0) 876567e837SChris Mason goto fail; 885f39d397SChris Mason leaf = path->nodes[0]; 896567e837SChris Mason if (ret > 0) { 906567e837SChris Mason ret = 1; 9170b2befdSChris Mason if (path->slots[0] == 0) 926567e837SChris Mason goto fail; 936567e837SChris Mason path->slots[0]--; 945f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 956567e837SChris Mason if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || 966567e837SChris Mason found_key.objectid != objectid) { 976567e837SChris Mason goto fail; 986567e837SChris Mason } 996567e837SChris Mason csum_offset = (offset - found_key.offset) >> 1006567e837SChris Mason root->fs_info->sb->s_blocksize_bits; 1015f39d397SChris Mason csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]); 102509659cdSChris Mason csums_in_item /= BTRFS_CRC32_SIZE; 103a429e513SChris Mason 104a429e513SChris Mason if (csum_offset >= csums_in_item) { 105a429e513SChris Mason ret = -EFBIG; 1066567e837SChris Mason goto fail; 1076567e837SChris Mason } 1086567e837SChris Mason } 1096567e837SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 110509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 111509659cdSChris Mason csum_offset * BTRFS_CRC32_SIZE); 1126567e837SChris Mason return item; 1136567e837SChris Mason fail: 1146567e837SChris Mason if (ret > 0) 115b18c6685SChris Mason ret = -ENOENT; 1166567e837SChris Mason return ERR_PTR(ret); 1176567e837SChris Mason } 1186567e837SChris Mason 1196567e837SChris Mason 120dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, 121dee26a9fSChris Mason struct btrfs_root *root, 122dee26a9fSChris Mason struct btrfs_path *path, u64 objectid, 1239773a788SChris Mason u64 offset, int mod) 124dee26a9fSChris Mason { 125dee26a9fSChris Mason int ret; 126dee26a9fSChris Mason struct btrfs_key file_key; 127dee26a9fSChris Mason int ins_len = mod < 0 ? -1 : 0; 128dee26a9fSChris Mason int cow = mod != 0; 129dee26a9fSChris Mason 130dee26a9fSChris Mason file_key.objectid = objectid; 13170b2befdSChris Mason file_key.offset = offset; 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 1373de9d6b6SChris Mason #if 0 /* broken */ 13861b49440SChris Mason int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode, 13961b49440SChris Mason struct bio *bio) 14061b49440SChris Mason { 14161b49440SChris Mason u32 sum; 14261b49440SChris Mason struct bio_vec *bvec = bio->bi_io_vec; 14361b49440SChris Mason int bio_index = 0; 14461b49440SChris Mason u64 offset; 14561b49440SChris Mason u64 item_start_offset = 0; 14661b49440SChris Mason u64 item_last_offset = 0; 14761b49440SChris Mason u32 diff; 14861b49440SChris Mason int ret; 14961b49440SChris Mason struct btrfs_path *path; 15061b49440SChris Mason struct btrfs_csum_item *item = NULL; 15161b49440SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 15261b49440SChris Mason 15361b49440SChris Mason path = btrfs_alloc_path(); 15461b49440SChris Mason 15561b49440SChris Mason WARN_ON(bio->bi_vcnt <= 0); 15661b49440SChris Mason 15761b49440SChris Mason while(bio_index < bio->bi_vcnt) { 15861b49440SChris Mason offset = page_offset(bvec->bv_page) + bvec->bv_offset; 15961b49440SChris Mason ret = btrfs_find_ordered_sum(inode, offset, &sum); 16061b49440SChris Mason if (ret == 0) 16161b49440SChris Mason goto found; 16261b49440SChris Mason 16361b49440SChris Mason if (!item || offset < item_start_offset || 16461b49440SChris Mason offset >= item_last_offset) { 16561b49440SChris Mason struct btrfs_key found_key; 16661b49440SChris Mason u32 item_size; 16761b49440SChris Mason 16861b49440SChris Mason if (item) 16961b49440SChris Mason btrfs_release_path(root, path); 17061b49440SChris Mason item = btrfs_lookup_csum(NULL, root, path, 17161b49440SChris Mason inode->i_ino, offset, 0); 17261b49440SChris Mason if (IS_ERR(item)) { 17361b49440SChris Mason ret = PTR_ERR(item); 17461b49440SChris Mason if (ret == -ENOENT || ret == -EFBIG) 17561b49440SChris Mason ret = 0; 17661b49440SChris Mason sum = 0; 17761b49440SChris Mason printk("no csum found for inode %lu start " 17861b49440SChris Mason "%llu\n", inode->i_ino, 17961b49440SChris Mason (unsigned long long)offset); 1806dab8157SChris Mason item = NULL; 18161b49440SChris Mason goto found; 18261b49440SChris Mason } 18361b49440SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key, 18461b49440SChris Mason path->slots[0]); 18561b49440SChris Mason 18661b49440SChris Mason item_start_offset = found_key.offset; 18761b49440SChris Mason item_size = btrfs_item_size_nr(path->nodes[0], 18861b49440SChris Mason path->slots[0]); 18961b49440SChris Mason item_last_offset = item_start_offset + 19061b49440SChris Mason (item_size / BTRFS_CRC32_SIZE) * 19161b49440SChris Mason root->sectorsize; 19261b49440SChris Mason item = btrfs_item_ptr(path->nodes[0], path->slots[0], 19361b49440SChris Mason struct btrfs_csum_item); 19461b49440SChris Mason } 19561b49440SChris Mason /* 19661b49440SChris Mason * this byte range must be able to fit inside 19761b49440SChris Mason * a single leaf so it will also fit inside a u32 19861b49440SChris Mason */ 19961b49440SChris Mason diff = offset - item_start_offset; 20061b49440SChris Mason diff = diff / root->sectorsize; 20161b49440SChris Mason diff = diff * BTRFS_CRC32_SIZE; 20261b49440SChris Mason 20361b49440SChris Mason read_extent_buffer(path->nodes[0], &sum, 2043de9d6b6SChris Mason ((unsigned long)item) + diff, 20561b49440SChris Mason BTRFS_CRC32_SIZE); 20661b49440SChris Mason found: 20761b49440SChris Mason set_state_private(io_tree, offset, sum); 20861b49440SChris Mason bio_index++; 20961b49440SChris Mason bvec++; 21061b49440SChris Mason } 21161b49440SChris Mason btrfs_free_path(path); 21261b49440SChris Mason return 0; 21361b49440SChris Mason } 2143de9d6b6SChris Mason #endif 21561b49440SChris Mason 2163edf7d33SChris Mason int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode, 2173edf7d33SChris Mason struct bio *bio) 218e015640fSChris Mason { 219e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums; 220e6dcd2dcSChris Mason struct btrfs_sector_sum *sector_sum; 2213edf7d33SChris Mason struct btrfs_ordered_extent *ordered; 222e015640fSChris Mason char *data; 223e015640fSChris Mason struct bio_vec *bvec = bio->bi_io_vec; 224e015640fSChris Mason int bio_index = 0; 2253edf7d33SChris Mason unsigned long total_bytes = 0; 2263edf7d33SChris Mason unsigned long this_sum_bytes = 0; 2273edf7d33SChris Mason u64 offset; 228e015640fSChris Mason 229e6dcd2dcSChris Mason WARN_ON(bio->bi_vcnt <= 0); 230e6dcd2dcSChris Mason sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS); 231e015640fSChris Mason if (!sums) 232e015640fSChris Mason return -ENOMEM; 2333edf7d33SChris Mason 234ed98b56aSChris Mason sector_sum = sums->sums; 2353edf7d33SChris Mason sums->file_offset = page_offset(bvec->bv_page) + bvec->bv_offset; 236e6dcd2dcSChris Mason sums->len = bio->bi_size; 237e6dcd2dcSChris Mason INIT_LIST_HEAD(&sums->list); 2383edf7d33SChris Mason ordered = btrfs_lookup_ordered_extent(inode, sums->file_offset); 2393edf7d33SChris Mason BUG_ON(!ordered); 240e015640fSChris Mason 241e015640fSChris Mason while(bio_index < bio->bi_vcnt) { 2423edf7d33SChris Mason offset = page_offset(bvec->bv_page) + bvec->bv_offset; 243e5a2217eSChris Mason if (offset >= ordered->file_offset + ordered->len || 244e5a2217eSChris Mason offset < ordered->file_offset) { 2453edf7d33SChris Mason unsigned long bytes_left; 2463edf7d33SChris Mason sums->len = this_sum_bytes; 2473edf7d33SChris Mason this_sum_bytes = 0; 2483edf7d33SChris Mason btrfs_add_ordered_sum(inode, ordered, sums); 2493edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 2503edf7d33SChris Mason 2513edf7d33SChris Mason bytes_left = bio->bi_size - total_bytes; 2523edf7d33SChris Mason 2533edf7d33SChris Mason sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left), 2543edf7d33SChris Mason GFP_NOFS); 2553edf7d33SChris Mason BUG_ON(!sums); 256ed98b56aSChris Mason sector_sum = sums->sums; 2573edf7d33SChris Mason sums->len = bytes_left; 2583edf7d33SChris Mason sums->file_offset = offset; 2593edf7d33SChris Mason ordered = btrfs_lookup_ordered_extent(inode, 2603edf7d33SChris Mason sums->file_offset); 2613edf7d33SChris Mason BUG_ON(!ordered); 2623edf7d33SChris Mason } 2633edf7d33SChris Mason 264e015640fSChris Mason data = kmap_atomic(bvec->bv_page, KM_USER0); 265e6dcd2dcSChris Mason sector_sum->sum = ~(u32)0; 266e6dcd2dcSChris Mason sector_sum->sum = btrfs_csum_data(root, 267e6dcd2dcSChris Mason data + bvec->bv_offset, 268e6dcd2dcSChris Mason sector_sum->sum, 269e6dcd2dcSChris Mason bvec->bv_len); 270e015640fSChris Mason kunmap_atomic(data, KM_USER0); 271e6dcd2dcSChris Mason btrfs_csum_final(sector_sum->sum, 272e6dcd2dcSChris Mason (char *)§or_sum->sum); 273e6dcd2dcSChris Mason sector_sum->offset = page_offset(bvec->bv_page) + 274e6dcd2dcSChris Mason bvec->bv_offset; 275ed98b56aSChris Mason 276e6dcd2dcSChris Mason sector_sum++; 277e015640fSChris Mason bio_index++; 2783edf7d33SChris Mason total_bytes += bvec->bv_len; 2793edf7d33SChris Mason this_sum_bytes += bvec->bv_len; 280e015640fSChris Mason bvec++; 281e015640fSChris Mason } 282ed98b56aSChris Mason this_sum_bytes = 0; 2833edf7d33SChris Mason btrfs_add_ordered_sum(inode, ordered, sums); 2843edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 285e015640fSChris Mason return 0; 286e015640fSChris Mason } 287e015640fSChris Mason 288065631f6SChris Mason int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, 289065631f6SChris Mason struct btrfs_root *root, struct inode *inode, 290e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums) 291f254e52cSChris Mason { 292065631f6SChris Mason u64 objectid = inode->i_ino; 293065631f6SChris Mason u64 offset; 294f254e52cSChris Mason int ret; 295f254e52cSChris Mason struct btrfs_key file_key; 2966567e837SChris Mason struct btrfs_key found_key; 297065631f6SChris Mason u64 next_offset; 298e6dcd2dcSChris Mason u64 total_bytes = 0; 299065631f6SChris Mason int found_next; 3005caf2a00SChris Mason struct btrfs_path *path; 301f254e52cSChris Mason struct btrfs_csum_item *item; 302065631f6SChris Mason struct btrfs_csum_item *item_end; 303ff79f819SChris Mason struct extent_buffer *leaf = NULL; 3046567e837SChris Mason u64 csum_offset; 305e6dcd2dcSChris Mason struct btrfs_sector_sum *sector_sum; 306f578d4bdSChris Mason u32 nritems; 307f578d4bdSChris Mason u32 ins_size; 3086e92f5e6SChris Mason char *eb_map; 3096e92f5e6SChris Mason char *eb_token; 3106e92f5e6SChris Mason unsigned long map_len; 3116e92f5e6SChris Mason unsigned long map_start; 3126e92f5e6SChris Mason 3135caf2a00SChris Mason path = btrfs_alloc_path(); 3145caf2a00SChris Mason BUG_ON(!path); 315ed98b56aSChris Mason sector_sum = sums->sums; 316065631f6SChris Mason again: 317065631f6SChris Mason next_offset = (u64)-1; 318065631f6SChris Mason found_next = 0; 319e6dcd2dcSChris Mason offset = sector_sum->offset; 320f254e52cSChris Mason file_key.objectid = objectid; 321f254e52cSChris Mason file_key.offset = offset; 322f254e52cSChris Mason btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); 323a429e513SChris Mason 324*53863232SChris Mason mutex_lock(&BTRFS_I(inode)->csum_mutex); 325a429e513SChris Mason item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1); 326ff79f819SChris Mason if (!IS_ERR(item)) { 327ff79f819SChris Mason leaf = path->nodes[0]; 328a429e513SChris Mason goto found; 329ff79f819SChris Mason } 330a429e513SChris Mason ret = PTR_ERR(item); 331a429e513SChris Mason if (ret == -EFBIG) { 332a429e513SChris Mason u32 item_size; 333a429e513SChris Mason /* we found one, but it isn't big enough yet */ 3345f39d397SChris Mason leaf = path->nodes[0]; 3355f39d397SChris Mason item_size = btrfs_item_size_nr(leaf, path->slots[0]); 336509659cdSChris Mason if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) { 337a429e513SChris Mason /* already at max size, make a new one */ 338a429e513SChris Mason goto insert; 339a429e513SChris Mason } 340a429e513SChris Mason } else { 341f578d4bdSChris Mason int slot = path->slots[0] + 1; 342a429e513SChris Mason /* we didn't find a csum item, insert one */ 343f578d4bdSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 344f578d4bdSChris Mason if (path->slots[0] >= nritems - 1) { 345f578d4bdSChris Mason ret = btrfs_next_leaf(root, path); 346b56baf5bSYan if (ret == 1) 347f578d4bdSChris Mason found_next = 1; 348b56baf5bSYan if (ret != 0) 349f578d4bdSChris Mason goto insert; 350b56baf5bSYan slot = 0; 351f578d4bdSChris Mason } 352f578d4bdSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); 353f578d4bdSChris Mason if (found_key.objectid != objectid || 354f578d4bdSChris Mason found_key.type != BTRFS_CSUM_ITEM_KEY) { 355f578d4bdSChris Mason found_next = 1; 356f578d4bdSChris Mason goto insert; 357f578d4bdSChris Mason } 358f578d4bdSChris Mason next_offset = found_key.offset; 359f578d4bdSChris Mason found_next = 1; 360a429e513SChris Mason goto insert; 361a429e513SChris Mason } 362a429e513SChris Mason 363a429e513SChris Mason /* 364a429e513SChris Mason * at this point, we know the tree has an item, but it isn't big 365a429e513SChris Mason * enough yet to put our csum in. Grow it 366a429e513SChris Mason */ 367a429e513SChris Mason btrfs_release_path(root, path); 3686567e837SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 369509659cdSChris Mason BTRFS_CRC32_SIZE, 1); 3706567e837SChris Mason if (ret < 0) 371*53863232SChris Mason goto fail_unlock; 3726567e837SChris Mason if (ret == 0) { 373b18c6685SChris Mason BUG(); 3746567e837SChris Mason } 3756567e837SChris Mason if (path->slots[0] == 0) { 3766567e837SChris Mason goto insert; 3776567e837SChris Mason } 3786567e837SChris Mason path->slots[0]--; 3795f39d397SChris Mason leaf = path->nodes[0]; 3805f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 3816567e837SChris Mason csum_offset = (offset - found_key.offset) >> 3826567e837SChris Mason root->fs_info->sb->s_blocksize_bits; 3836567e837SChris Mason if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || 3846567e837SChris Mason found_key.objectid != objectid || 3856567e837SChris Mason csum_offset >= MAX_CSUM_ITEMS(root)) { 3866567e837SChris Mason goto insert; 3876567e837SChris Mason } 3885f39d397SChris Mason if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) / 389509659cdSChris Mason BTRFS_CRC32_SIZE) { 390509659cdSChris Mason u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE; 3915f39d397SChris Mason diff = diff - btrfs_item_size_nr(leaf, path->slots[0]); 3923a686375SChris Mason if (diff != BTRFS_CRC32_SIZE) 3933a686375SChris Mason goto insert; 394a429e513SChris Mason ret = btrfs_extend_item(trans, root, path, diff); 3956567e837SChris Mason BUG_ON(ret); 3966567e837SChris Mason goto csum; 3976567e837SChris Mason } 3986567e837SChris Mason 3996567e837SChris Mason insert: 400a429e513SChris Mason btrfs_release_path(root, path); 4016567e837SChris Mason csum_offset = 0; 402f578d4bdSChris Mason if (found_next) { 403f578d4bdSChris Mason u64 tmp = min((u64)i_size_read(inode), next_offset); 404b56baf5bSYan tmp -= offset & ~((u64)root->sectorsize -1); 405f578d4bdSChris Mason tmp >>= root->fs_info->sb->s_blocksize_bits; 406f578d4bdSChris Mason tmp = max((u64)1, tmp); 407f578d4bdSChris Mason tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root)); 408f578d4bdSChris Mason ins_size = BTRFS_CRC32_SIZE * tmp; 409f578d4bdSChris Mason } else { 410f578d4bdSChris Mason ins_size = BTRFS_CRC32_SIZE; 411f578d4bdSChris Mason } 4125caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 413f578d4bdSChris Mason ins_size); 41454aa1f4dSChris Mason if (ret < 0) 415*53863232SChris Mason goto fail_unlock; 416a429e513SChris Mason if (ret != 0) { 417a429e513SChris Mason WARN_ON(1); 418*53863232SChris Mason goto fail_unlock; 419a429e513SChris Mason } 4206567e837SChris Mason csum: 4215f39d397SChris Mason leaf = path->nodes[0]; 4225f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 423f254e52cSChris Mason ret = 0; 424509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 425509659cdSChris Mason csum_offset * BTRFS_CRC32_SIZE); 426b18c6685SChris Mason found: 427065631f6SChris Mason item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 428065631f6SChris Mason item_end = (struct btrfs_csum_item *)((unsigned char *)item_end + 429065631f6SChris Mason btrfs_item_size_nr(leaf, path->slots[0])); 4306e92f5e6SChris Mason eb_token = NULL; 431*53863232SChris Mason mutex_unlock(&BTRFS_I(inode)->csum_mutex); 432*53863232SChris Mason cond_resched(); 433e6dcd2dcSChris Mason next_sector: 434aadfeb6eSChris Mason 4356e92f5e6SChris Mason if (!eb_token || 4366e92f5e6SChris Mason (unsigned long)item + BTRFS_CRC32_SIZE >= map_start + map_len) { 4376e92f5e6SChris Mason int err; 4386e92f5e6SChris Mason 4396e92f5e6SChris Mason if (eb_token) 440eb20978fSChris Mason unmap_extent_buffer(leaf, eb_token, KM_USER1); 4416e92f5e6SChris Mason eb_token = NULL; 4426e92f5e6SChris Mason err = map_private_extent_buffer(leaf, (unsigned long)item, 4436e92f5e6SChris Mason BTRFS_CRC32_SIZE, 4446e92f5e6SChris Mason &eb_token, &eb_map, 445eb20978fSChris Mason &map_start, &map_len, KM_USER1); 4466e92f5e6SChris Mason if (err) 4476e92f5e6SChris Mason eb_token = NULL; 4486e92f5e6SChris Mason } 4496e92f5e6SChris Mason if (eb_token) { 4506e92f5e6SChris Mason memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)), 451e6dcd2dcSChris Mason §or_sum->sum, BTRFS_CRC32_SIZE); 4526e92f5e6SChris Mason } else { 453e6dcd2dcSChris Mason write_extent_buffer(leaf, §or_sum->sum, 454e6dcd2dcSChris Mason (unsigned long)item, BTRFS_CRC32_SIZE); 4556e92f5e6SChris Mason } 4567f3c74fbSChris Mason 457e6dcd2dcSChris Mason total_bytes += root->sectorsize; 458e6dcd2dcSChris Mason sector_sum++; 459e6dcd2dcSChris Mason if (total_bytes < sums->len) { 4606e92f5e6SChris Mason item = (struct btrfs_csum_item *)((char *)item + 4616e92f5e6SChris Mason BTRFS_CRC32_SIZE); 4622e1a992eSChris Mason if (item < item_end && offset + PAGE_CACHE_SIZE == 463e6dcd2dcSChris Mason sector_sum->offset) { 464e6dcd2dcSChris Mason offset = sector_sum->offset; 465e6dcd2dcSChris Mason goto next_sector; 466065631f6SChris Mason } 4672e1a992eSChris Mason } 4686e92f5e6SChris Mason if (eb_token) { 469eb20978fSChris Mason unmap_extent_buffer(leaf, eb_token, KM_USER1); 4706e92f5e6SChris Mason eb_token = NULL; 4716e92f5e6SChris Mason } 4725caf2a00SChris Mason btrfs_mark_buffer_dirty(path->nodes[0]); 473*53863232SChris Mason cond_resched(); 474e6dcd2dcSChris Mason if (total_bytes < sums->len) { 4755caf2a00SChris Mason btrfs_release_path(root, path); 476065631f6SChris Mason goto again; 477065631f6SChris Mason } 478*53863232SChris Mason out: 4795caf2a00SChris Mason btrfs_free_path(path); 480f254e52cSChris Mason return ret; 481*53863232SChris Mason 482*53863232SChris Mason fail_unlock: 483*53863232SChris Mason mutex_unlock(&BTRFS_I(inode)->csum_mutex); 484*53863232SChris Mason goto out; 485f254e52cSChris Mason } 486f254e52cSChris Mason 4871de037a4SChris Mason int btrfs_csum_truncate(struct btrfs_trans_handle *trans, 4881de037a4SChris Mason struct btrfs_root *root, struct btrfs_path *path, 4891de037a4SChris Mason u64 isize) 4901de037a4SChris Mason { 4911de037a4SChris Mason struct btrfs_key key; 4925f39d397SChris Mason struct extent_buffer *leaf = path->nodes[0]; 4931de037a4SChris Mason int slot = path->slots[0]; 4941de037a4SChris Mason int ret; 4951de037a4SChris Mason u32 new_item_size; 4961de037a4SChris Mason u64 new_item_span; 4971de037a4SChris Mason u64 blocks; 4981de037a4SChris Mason 4995f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &key, slot); 5001de037a4SChris Mason if (isize <= key.offset) 5011de037a4SChris Mason return 0; 5021de037a4SChris Mason new_item_span = isize - key.offset; 5035f39d397SChris Mason blocks = (new_item_span + root->sectorsize - 1) >> 50484f54cfaSChris Mason root->fs_info->sb->s_blocksize_bits; 5051de037a4SChris Mason new_item_size = blocks * BTRFS_CRC32_SIZE; 5065f39d397SChris Mason if (new_item_size >= btrfs_item_size_nr(leaf, slot)) 5071de037a4SChris Mason return 0; 508179e29e4SChris Mason ret = btrfs_truncate_item(trans, root, path, new_item_size, 1); 5091de037a4SChris Mason BUG_ON(ret); 5101de037a4SChris Mason return ret; 5111de037a4SChris Mason } 512