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" 26*facc8a22SMiao Xie #include "volumes.h" 271de037a4SChris Mason #include "print-tree.h" 281e1d2701SChris Mason 29995e01b7SJan Schmidt #define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \ 30a429e513SChris Mason sizeof(struct btrfs_item) * 2) / \ 31607d432dSJosef Bacik size) - 1)) 3207d400a6SYan Zheng 33221b8318SZach Brown #define MAX_CSUM_ITEMS(r, size) (min_t(u32, __MAX_CSUM_ITEMS(r, size), \ 34221b8318SZach Brown PAGE_CACHE_SIZE)) 357ca4be45SChris Mason 3607d400a6SYan Zheng #define MAX_ORDERED_SUM_BYTES(r) ((PAGE_SIZE - \ 3707d400a6SYan Zheng sizeof(struct btrfs_ordered_sum)) / \ 38f51a4a18SMiao Xie sizeof(u32) * (r)->sectorsize) 3907d400a6SYan Zheng 40b18c6685SChris Mason int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, 41dee26a9fSChris Mason struct btrfs_root *root, 42b18c6685SChris Mason u64 objectid, u64 pos, 43f2eb0a24SSage Weil u64 disk_offset, u64 disk_num_bytes, 44c8b97818SChris Mason u64 num_bytes, u64 offset, u64 ram_bytes, 45c8b97818SChris Mason u8 compression, u8 encryption, u16 other_encoding) 469f5fae2fSChris Mason { 47dee26a9fSChris Mason int ret = 0; 48dee26a9fSChris Mason struct btrfs_file_extent_item *item; 49dee26a9fSChris Mason struct btrfs_key file_key; 505caf2a00SChris Mason struct btrfs_path *path; 515f39d397SChris Mason struct extent_buffer *leaf; 52dee26a9fSChris Mason 535caf2a00SChris Mason path = btrfs_alloc_path(); 54db5b493aSTsutomu Itoh if (!path) 55db5b493aSTsutomu Itoh return -ENOMEM; 56dee26a9fSChris Mason file_key.objectid = objectid; 57b18c6685SChris Mason file_key.offset = pos; 58dee26a9fSChris Mason btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); 59dee26a9fSChris Mason 60b9473439SChris Mason path->leave_spinning = 1; 615caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 62dee26a9fSChris Mason sizeof(*item)); 6354aa1f4dSChris Mason if (ret < 0) 6454aa1f4dSChris Mason goto out; 6579787eaaSJeff Mahoney BUG_ON(ret); /* Can't happen */ 665f39d397SChris Mason leaf = path->nodes[0]; 675f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], 68dee26a9fSChris Mason struct btrfs_file_extent_item); 69f2eb0a24SSage Weil btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset); 70db94535dSChris Mason btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes); 71f2eb0a24SSage Weil btrfs_set_file_extent_offset(leaf, item, offset); 72db94535dSChris Mason btrfs_set_file_extent_num_bytes(leaf, item, num_bytes); 73c8b97818SChris Mason btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes); 745f39d397SChris Mason btrfs_set_file_extent_generation(leaf, item, trans->transid); 755f39d397SChris Mason btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); 76c8b97818SChris Mason btrfs_set_file_extent_compression(leaf, item, compression); 77c8b97818SChris Mason btrfs_set_file_extent_encryption(leaf, item, encryption); 78c8b97818SChris Mason btrfs_set_file_extent_other_encoding(leaf, item, other_encoding); 79c8b97818SChris Mason 805f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 8154aa1f4dSChris Mason out: 825caf2a00SChris Mason btrfs_free_path(path); 8354aa1f4dSChris Mason return ret; 849f5fae2fSChris Mason } 85dee26a9fSChris Mason 8648a3b636SEric Sandeen static struct btrfs_csum_item * 8748a3b636SEric Sandeen btrfs_lookup_csum(struct btrfs_trans_handle *trans, 88b18c6685SChris Mason struct btrfs_root *root, 896567e837SChris Mason struct btrfs_path *path, 90d20f7043SChris Mason u64 bytenr, int cow) 916567e837SChris Mason { 926567e837SChris Mason int ret; 936567e837SChris Mason struct btrfs_key file_key; 946567e837SChris Mason struct btrfs_key found_key; 956567e837SChris Mason struct btrfs_csum_item *item; 965f39d397SChris Mason struct extent_buffer *leaf; 976567e837SChris Mason u64 csum_offset = 0; 986c41761fSDavid Sterba u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 99a429e513SChris Mason int csums_in_item; 1006567e837SChris Mason 101d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 102d20f7043SChris Mason file_key.offset = bytenr; 103d20f7043SChris Mason btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY); 104b18c6685SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); 1056567e837SChris Mason if (ret < 0) 1066567e837SChris Mason goto fail; 1075f39d397SChris Mason leaf = path->nodes[0]; 1086567e837SChris Mason if (ret > 0) { 1096567e837SChris Mason ret = 1; 11070b2befdSChris Mason if (path->slots[0] == 0) 1116567e837SChris Mason goto fail; 1126567e837SChris Mason path->slots[0]--; 1135f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 114d20f7043SChris Mason if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY) 1156567e837SChris Mason goto fail; 116d20f7043SChris Mason 117d20f7043SChris Mason csum_offset = (bytenr - found_key.offset) >> 1186567e837SChris Mason root->fs_info->sb->s_blocksize_bits; 1195f39d397SChris Mason csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]); 120607d432dSJosef Bacik csums_in_item /= csum_size; 121a429e513SChris Mason 12282d130ffSMiao Xie if (csum_offset == csums_in_item) { 123a429e513SChris Mason ret = -EFBIG; 1246567e837SChris Mason goto fail; 12582d130ffSMiao Xie } else if (csum_offset > csums_in_item) { 12682d130ffSMiao Xie goto fail; 1276567e837SChris Mason } 1286567e837SChris Mason } 1296567e837SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 130509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 131607d432dSJosef Bacik csum_offset * csum_size); 1326567e837SChris Mason return item; 1336567e837SChris Mason fail: 1346567e837SChris Mason if (ret > 0) 135b18c6685SChris Mason ret = -ENOENT; 1366567e837SChris Mason return ERR_PTR(ret); 1376567e837SChris Mason } 1386567e837SChris Mason 139dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, 140dee26a9fSChris Mason struct btrfs_root *root, 141dee26a9fSChris Mason struct btrfs_path *path, u64 objectid, 1429773a788SChris Mason u64 offset, int mod) 143dee26a9fSChris Mason { 144dee26a9fSChris Mason int ret; 145dee26a9fSChris Mason struct btrfs_key file_key; 146dee26a9fSChris Mason int ins_len = mod < 0 ? -1 : 0; 147dee26a9fSChris Mason int cow = mod != 0; 148dee26a9fSChris Mason 149dee26a9fSChris Mason file_key.objectid = objectid; 15070b2befdSChris Mason file_key.offset = offset; 151dee26a9fSChris Mason btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); 152dee26a9fSChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); 153dee26a9fSChris Mason return ret; 154dee26a9fSChris Mason } 155f254e52cSChris Mason 156*facc8a22SMiao Xie static void btrfs_io_bio_endio_readpage(struct btrfs_io_bio *bio, int err) 157*facc8a22SMiao Xie { 158*facc8a22SMiao Xie kfree(bio->csum_allocated); 159*facc8a22SMiao Xie } 160*facc8a22SMiao Xie 1614b46fce2SJosef Bacik static int __btrfs_lookup_bio_sums(struct btrfs_root *root, 1624b46fce2SJosef Bacik struct inode *inode, struct bio *bio, 1634b46fce2SJosef Bacik u64 logical_offset, u32 *dst, int dio) 16461b49440SChris Mason { 16561b49440SChris Mason struct bio_vec *bvec = bio->bi_io_vec; 166*facc8a22SMiao Xie struct btrfs_io_bio *btrfs_bio = btrfs_io_bio(bio); 167*facc8a22SMiao Xie struct btrfs_csum_item *item = NULL; 168*facc8a22SMiao Xie struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 169*facc8a22SMiao Xie struct btrfs_path *path; 170*facc8a22SMiao Xie u8 *csum; 1714b46fce2SJosef Bacik u64 offset = 0; 17261b49440SChris Mason u64 item_start_offset = 0; 17361b49440SChris Mason u64 item_last_offset = 0; 174d20f7043SChris Mason u64 disk_bytenr; 17561b49440SChris Mason u32 diff; 176*facc8a22SMiao Xie int nblocks; 177*facc8a22SMiao Xie int bio_index = 0; 178e4100d98SMiao Xie int count; 179*facc8a22SMiao Xie u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 18061b49440SChris Mason 18161b49440SChris Mason path = btrfs_alloc_path(); 182c2db1073STsutomu Itoh if (!path) 183c2db1073STsutomu Itoh return -ENOMEM; 184*facc8a22SMiao Xie 185*facc8a22SMiao Xie nblocks = bio->bi_size >> inode->i_sb->s_blocksize_bits; 186*facc8a22SMiao Xie if (!dst) { 187*facc8a22SMiao Xie if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) { 188*facc8a22SMiao Xie btrfs_bio->csum_allocated = kmalloc(nblocks * csum_size, 189*facc8a22SMiao Xie GFP_NOFS); 190*facc8a22SMiao Xie if (!btrfs_bio->csum_allocated) { 191*facc8a22SMiao Xie btrfs_free_path(path); 192*facc8a22SMiao Xie return -ENOMEM; 193*facc8a22SMiao Xie } 194*facc8a22SMiao Xie btrfs_bio->csum = btrfs_bio->csum_allocated; 195*facc8a22SMiao Xie btrfs_bio->end_io = btrfs_io_bio_endio_readpage; 196*facc8a22SMiao Xie } else { 197*facc8a22SMiao Xie btrfs_bio->csum = btrfs_bio->csum_inline; 198*facc8a22SMiao Xie } 199*facc8a22SMiao Xie csum = btrfs_bio->csum; 200*facc8a22SMiao Xie } else { 201*facc8a22SMiao Xie csum = (u8 *)dst; 202*facc8a22SMiao Xie } 203*facc8a22SMiao Xie 2044d1b5fb4SChris Mason if (bio->bi_size > PAGE_CACHE_SIZE * 8) 2054d1b5fb4SChris Mason path->reada = 2; 20661b49440SChris Mason 20761b49440SChris Mason WARN_ON(bio->bi_vcnt <= 0); 20861b49440SChris Mason 2092cf8572dSChris Mason /* 2102cf8572dSChris Mason * the free space stuff is only read when it hasn't been 2112cf8572dSChris Mason * updated in the current transaction. So, we can safely 2122cf8572dSChris Mason * read from the commit root and sidestep a nasty deadlock 2132cf8572dSChris Mason * between reading the free space cache and updating the csum tree. 2142cf8572dSChris Mason */ 21583eea1f1SLiu Bo if (btrfs_is_free_space_inode(inode)) { 2162cf8572dSChris Mason path->search_commit_root = 1; 217ddf23b3fSJosef Bacik path->skip_locking = 1; 218ddf23b3fSJosef Bacik } 2192cf8572dSChris Mason 220d20f7043SChris Mason disk_bytenr = (u64)bio->bi_sector << 9; 2214b46fce2SJosef Bacik if (dio) 2224b46fce2SJosef Bacik offset = logical_offset; 22361b49440SChris Mason while (bio_index < bio->bi_vcnt) { 2244b46fce2SJosef Bacik if (!dio) 22561b49440SChris Mason offset = page_offset(bvec->bv_page) + bvec->bv_offset; 226*facc8a22SMiao Xie count = btrfs_find_ordered_sum(inode, offset, disk_bytenr, 227*facc8a22SMiao Xie (u32 *)csum, nblocks); 228e4100d98SMiao Xie if (count) 22961b49440SChris Mason goto found; 23061b49440SChris Mason 231d20f7043SChris Mason if (!item || disk_bytenr < item_start_offset || 232d20f7043SChris Mason disk_bytenr >= item_last_offset) { 23361b49440SChris Mason struct btrfs_key found_key; 23461b49440SChris Mason u32 item_size; 23561b49440SChris Mason 23661b49440SChris Mason if (item) 237b3b4aa74SDavid Sterba btrfs_release_path(path); 238d20f7043SChris Mason item = btrfs_lookup_csum(NULL, root->fs_info->csum_root, 239d20f7043SChris Mason path, disk_bytenr, 0); 24061b49440SChris Mason if (IS_ERR(item)) { 241e4100d98SMiao Xie count = 1; 242*facc8a22SMiao Xie memset(csum, 0, csum_size); 24317d217feSYan Zheng if (BTRFS_I(inode)->root->root_key.objectid == 24417d217feSYan Zheng BTRFS_DATA_RELOC_TREE_OBJECTID) { 24517d217feSYan Zheng set_extent_bits(io_tree, offset, 24617d217feSYan Zheng offset + bvec->bv_len - 1, 24717d217feSYan Zheng EXTENT_NODATASUM, GFP_NOFS); 24817d217feSYan Zheng } else { 249d397712bSChris Mason printk(KERN_INFO "btrfs no csum found " 25033345d01SLi Zefan "for inode %llu start %llu\n", 25133345d01SLi Zefan (unsigned long long) 25233345d01SLi Zefan btrfs_ino(inode), 25361b49440SChris Mason (unsigned long long)offset); 25417d217feSYan Zheng } 2556dab8157SChris Mason item = NULL; 256b3b4aa74SDavid Sterba btrfs_release_path(path); 25761b49440SChris Mason goto found; 25861b49440SChris Mason } 25961b49440SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key, 26061b49440SChris Mason path->slots[0]); 26161b49440SChris Mason 26261b49440SChris Mason item_start_offset = found_key.offset; 26361b49440SChris Mason item_size = btrfs_item_size_nr(path->nodes[0], 26461b49440SChris Mason path->slots[0]); 26561b49440SChris Mason item_last_offset = item_start_offset + 266607d432dSJosef Bacik (item_size / csum_size) * 26761b49440SChris Mason root->sectorsize; 26861b49440SChris Mason item = btrfs_item_ptr(path->nodes[0], path->slots[0], 26961b49440SChris Mason struct btrfs_csum_item); 27061b49440SChris Mason } 27161b49440SChris Mason /* 27261b49440SChris Mason * this byte range must be able to fit inside 27361b49440SChris Mason * a single leaf so it will also fit inside a u32 27461b49440SChris Mason */ 275d20f7043SChris Mason diff = disk_bytenr - item_start_offset; 27661b49440SChris Mason diff = diff / root->sectorsize; 277607d432dSJosef Bacik diff = diff * csum_size; 278*facc8a22SMiao Xie count = min_t(int, nblocks, (item_last_offset - disk_bytenr) >> 279e4100d98SMiao Xie inode->i_sb->s_blocksize_bits); 280*facc8a22SMiao Xie read_extent_buffer(path->nodes[0], csum, 2813de9d6b6SChris Mason ((unsigned long)item) + diff, 282e4100d98SMiao Xie csum_size * count); 28361b49440SChris Mason found: 284*facc8a22SMiao Xie csum += count * csum_size; 285*facc8a22SMiao Xie nblocks -= count; 286e4100d98SMiao Xie while (count--) { 287d20f7043SChris Mason disk_bytenr += bvec->bv_len; 2884b46fce2SJosef Bacik offset += bvec->bv_len; 28961b49440SChris Mason bio_index++; 29061b49440SChris Mason bvec++; 29161b49440SChris Mason } 292e4100d98SMiao Xie } 29361b49440SChris Mason btrfs_free_path(path); 29461b49440SChris Mason return 0; 29561b49440SChris Mason } 29661b49440SChris Mason 2974b46fce2SJosef Bacik int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode, 2984b46fce2SJosef Bacik struct bio *bio, u32 *dst) 2994b46fce2SJosef Bacik { 3004b46fce2SJosef Bacik return __btrfs_lookup_bio_sums(root, inode, bio, 0, dst, 0); 3014b46fce2SJosef Bacik } 3024b46fce2SJosef Bacik 3034b46fce2SJosef Bacik int btrfs_lookup_bio_sums_dio(struct btrfs_root *root, struct inode *inode, 304*facc8a22SMiao Xie struct btrfs_dio_private *dip, struct bio *bio, 305*facc8a22SMiao Xie u64 offset) 3064b46fce2SJosef Bacik { 307*facc8a22SMiao Xie int len = (bio->bi_sector << 9) - dip->disk_bytenr; 308*facc8a22SMiao Xie u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 309*facc8a22SMiao Xie int ret; 310*facc8a22SMiao Xie 311*facc8a22SMiao Xie len >>= inode->i_sb->s_blocksize_bits; 312*facc8a22SMiao Xie len *= csum_size; 313*facc8a22SMiao Xie 314*facc8a22SMiao Xie ret = __btrfs_lookup_bio_sums(root, inode, bio, offset, 315*facc8a22SMiao Xie (u32 *)(dip->csum + len), 1); 316*facc8a22SMiao Xie return ret; 3174b46fce2SJosef Bacik } 3184b46fce2SJosef Bacik 31917d217feSYan Zheng int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, 320a2de733cSArne Jansen struct list_head *list, int search_commit) 32117d217feSYan Zheng { 32217d217feSYan Zheng struct btrfs_key key; 32317d217feSYan Zheng struct btrfs_path *path; 32417d217feSYan Zheng struct extent_buffer *leaf; 32517d217feSYan Zheng struct btrfs_ordered_sum *sums; 32617d217feSYan Zheng struct btrfs_csum_item *item; 3270678b618SMark Fasheh LIST_HEAD(tmplist); 32817d217feSYan Zheng unsigned long offset; 32917d217feSYan Zheng int ret; 33017d217feSYan Zheng size_t size; 33117d217feSYan Zheng u64 csum_end; 3326c41761fSDavid Sterba u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 33317d217feSYan Zheng 33417d217feSYan Zheng path = btrfs_alloc_path(); 335d8926bb3SMark Fasheh if (!path) 336d8926bb3SMark Fasheh return -ENOMEM; 33717d217feSYan Zheng 338a2de733cSArne Jansen if (search_commit) { 339a2de733cSArne Jansen path->skip_locking = 1; 340a2de733cSArne Jansen path->reada = 2; 341a2de733cSArne Jansen path->search_commit_root = 1; 342a2de733cSArne Jansen } 343a2de733cSArne Jansen 34417d217feSYan Zheng key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 34517d217feSYan Zheng key.offset = start; 34617d217feSYan Zheng key.type = BTRFS_EXTENT_CSUM_KEY; 34717d217feSYan Zheng 34807d400a6SYan Zheng ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 34917d217feSYan Zheng if (ret < 0) 35017d217feSYan Zheng goto fail; 35117d217feSYan Zheng if (ret > 0 && path->slots[0] > 0) { 35217d217feSYan Zheng leaf = path->nodes[0]; 35317d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); 35417d217feSYan Zheng if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID && 35517d217feSYan Zheng key.type == BTRFS_EXTENT_CSUM_KEY) { 35617d217feSYan Zheng offset = (start - key.offset) >> 35717d217feSYan Zheng root->fs_info->sb->s_blocksize_bits; 35817d217feSYan Zheng if (offset * csum_size < 35917d217feSYan Zheng btrfs_item_size_nr(leaf, path->slots[0] - 1)) 36017d217feSYan Zheng path->slots[0]--; 36117d217feSYan Zheng } 36217d217feSYan Zheng } 36317d217feSYan Zheng 36417d217feSYan Zheng while (start <= end) { 36517d217feSYan Zheng leaf = path->nodes[0]; 36617d217feSYan Zheng if (path->slots[0] >= btrfs_header_nritems(leaf)) { 36707d400a6SYan Zheng ret = btrfs_next_leaf(root, path); 36817d217feSYan Zheng if (ret < 0) 36917d217feSYan Zheng goto fail; 37017d217feSYan Zheng if (ret > 0) 37117d217feSYan Zheng break; 37217d217feSYan Zheng leaf = path->nodes[0]; 37317d217feSYan Zheng } 37417d217feSYan Zheng 37517d217feSYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 37617d217feSYan Zheng if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 377628c8282SZhi Yong Wu key.type != BTRFS_EXTENT_CSUM_KEY || 378628c8282SZhi Yong Wu key.offset > end) 37917d217feSYan Zheng break; 38017d217feSYan Zheng 38117d217feSYan Zheng if (key.offset > start) 38217d217feSYan Zheng start = key.offset; 38317d217feSYan Zheng 38417d217feSYan Zheng size = btrfs_item_size_nr(leaf, path->slots[0]); 38517d217feSYan Zheng csum_end = key.offset + (size / csum_size) * root->sectorsize; 38687b29b20SYan Zheng if (csum_end <= start) { 38787b29b20SYan Zheng path->slots[0]++; 38887b29b20SYan Zheng continue; 38987b29b20SYan Zheng } 39017d217feSYan Zheng 39107d400a6SYan Zheng csum_end = min(csum_end, end + 1); 39207d400a6SYan Zheng item = btrfs_item_ptr(path->nodes[0], path->slots[0], 39307d400a6SYan Zheng struct btrfs_csum_item); 39407d400a6SYan Zheng while (start < csum_end) { 39507d400a6SYan Zheng size = min_t(size_t, csum_end - start, 39607d400a6SYan Zheng MAX_ORDERED_SUM_BYTES(root)); 39707d400a6SYan Zheng sums = kzalloc(btrfs_ordered_sum_size(root, size), 39807d400a6SYan Zheng GFP_NOFS); 3990678b618SMark Fasheh if (!sums) { 4000678b618SMark Fasheh ret = -ENOMEM; 4010678b618SMark Fasheh goto fail; 4020678b618SMark Fasheh } 40317d217feSYan Zheng 40417d217feSYan Zheng sums->bytenr = start; 405f51a4a18SMiao Xie sums->len = (int)size; 40617d217feSYan Zheng 40717d217feSYan Zheng offset = (start - key.offset) >> 40817d217feSYan Zheng root->fs_info->sb->s_blocksize_bits; 40917d217feSYan Zheng offset *= csum_size; 410f51a4a18SMiao Xie size >>= root->fs_info->sb->s_blocksize_bits; 41117d217feSYan Zheng 41207d400a6SYan Zheng read_extent_buffer(path->nodes[0], 413f51a4a18SMiao Xie sums->sums, 414f51a4a18SMiao Xie ((unsigned long)item) + offset, 415f51a4a18SMiao Xie csum_size * size); 41617d217feSYan Zheng 417f51a4a18SMiao Xie start += root->sectorsize * size; 4180678b618SMark Fasheh list_add_tail(&sums->list, &tmplist); 41907d400a6SYan Zheng } 42017d217feSYan Zheng path->slots[0]++; 42117d217feSYan Zheng } 42217d217feSYan Zheng ret = 0; 42317d217feSYan Zheng fail: 4240678b618SMark Fasheh while (ret < 0 && !list_empty(&tmplist)) { 4250678b618SMark Fasheh sums = list_entry(&tmplist, struct btrfs_ordered_sum, list); 4260678b618SMark Fasheh list_del(&sums->list); 4270678b618SMark Fasheh kfree(sums); 4280678b618SMark Fasheh } 4290678b618SMark Fasheh list_splice_tail(&tmplist, list); 4300678b618SMark Fasheh 43117d217feSYan Zheng btrfs_free_path(path); 43217d217feSYan Zheng return ret; 43317d217feSYan Zheng } 43417d217feSYan Zheng 4353edf7d33SChris Mason int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode, 436d20f7043SChris Mason struct bio *bio, u64 file_start, int contig) 437e015640fSChris Mason { 438e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums; 4393edf7d33SChris Mason struct btrfs_ordered_extent *ordered; 440e015640fSChris Mason char *data; 441e015640fSChris Mason struct bio_vec *bvec = bio->bi_io_vec; 442e015640fSChris Mason int bio_index = 0; 443f51a4a18SMiao Xie int index; 4443edf7d33SChris Mason unsigned long total_bytes = 0; 4453edf7d33SChris Mason unsigned long this_sum_bytes = 0; 4463edf7d33SChris Mason u64 offset; 447e015640fSChris Mason 448e6dcd2dcSChris Mason WARN_ON(bio->bi_vcnt <= 0); 449e6dcd2dcSChris Mason sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS); 450e015640fSChris Mason if (!sums) 451e015640fSChris Mason return -ENOMEM; 4523edf7d33SChris Mason 453e6dcd2dcSChris Mason sums->len = bio->bi_size; 454e6dcd2dcSChris Mason INIT_LIST_HEAD(&sums->list); 455d20f7043SChris Mason 456d20f7043SChris Mason if (contig) 457d20f7043SChris Mason offset = file_start; 458d20f7043SChris Mason else 459d20f7043SChris Mason offset = page_offset(bvec->bv_page) + bvec->bv_offset; 460d20f7043SChris Mason 461d20f7043SChris Mason ordered = btrfs_lookup_ordered_extent(inode, offset); 46279787eaaSJeff Mahoney BUG_ON(!ordered); /* Logic error */ 463f51a4a18SMiao Xie sums->bytenr = (u64)bio->bi_sector << 9; 464f51a4a18SMiao Xie index = 0; 465e015640fSChris Mason 466e015640fSChris Mason while (bio_index < bio->bi_vcnt) { 467d20f7043SChris Mason if (!contig) 4683edf7d33SChris Mason offset = page_offset(bvec->bv_page) + bvec->bv_offset; 469d20f7043SChris Mason 470e58dd74bSJosef Bacik if (offset >= ordered->file_offset + ordered->len || 471e58dd74bSJosef Bacik offset < ordered->file_offset) { 4723edf7d33SChris Mason unsigned long bytes_left; 4733edf7d33SChris Mason sums->len = this_sum_bytes; 4743edf7d33SChris Mason this_sum_bytes = 0; 4753edf7d33SChris Mason btrfs_add_ordered_sum(inode, ordered, sums); 4763edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 4773edf7d33SChris Mason 4783edf7d33SChris Mason bytes_left = bio->bi_size - total_bytes; 4793edf7d33SChris Mason 4803edf7d33SChris Mason sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left), 4813edf7d33SChris Mason GFP_NOFS); 48279787eaaSJeff Mahoney BUG_ON(!sums); /* -ENOMEM */ 4833edf7d33SChris Mason sums->len = bytes_left; 484d20f7043SChris Mason ordered = btrfs_lookup_ordered_extent(inode, offset); 48579787eaaSJeff Mahoney BUG_ON(!ordered); /* Logic error */ 486f51a4a18SMiao Xie sums->bytenr = ((u64)bio->bi_sector << 9) + 487f51a4a18SMiao Xie total_bytes; 488f51a4a18SMiao Xie index = 0; 4893edf7d33SChris Mason } 4903edf7d33SChris Mason 4917ac687d9SCong Wang data = kmap_atomic(bvec->bv_page); 492f51a4a18SMiao Xie sums->sums[index] = ~(u32)0; 493f51a4a18SMiao Xie sums->sums[index] = btrfs_csum_data(data + bvec->bv_offset, 494f51a4a18SMiao Xie sums->sums[index], 495e6dcd2dcSChris Mason bvec->bv_len); 4967ac687d9SCong Wang kunmap_atomic(data); 497f51a4a18SMiao Xie btrfs_csum_final(sums->sums[index], 498f51a4a18SMiao Xie (char *)(sums->sums + index)); 499ed98b56aSChris Mason 500e015640fSChris Mason bio_index++; 501f51a4a18SMiao Xie index++; 5023edf7d33SChris Mason total_bytes += bvec->bv_len; 5033edf7d33SChris Mason this_sum_bytes += bvec->bv_len; 504d20f7043SChris Mason offset += bvec->bv_len; 505e015640fSChris Mason bvec++; 506e015640fSChris Mason } 507ed98b56aSChris Mason this_sum_bytes = 0; 5083edf7d33SChris Mason btrfs_add_ordered_sum(inode, ordered, sums); 5093edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 510e015640fSChris Mason return 0; 511e015640fSChris Mason } 512e015640fSChris Mason 513459931ecSChris Mason /* 514459931ecSChris Mason * helper function for csum removal, this expects the 515459931ecSChris Mason * key to describe the csum pointed to by the path, and it expects 516459931ecSChris Mason * the csum to overlap the range [bytenr, len] 517459931ecSChris Mason * 518459931ecSChris Mason * The csum should not be entirely contained in the range and the 519459931ecSChris Mason * range should not be entirely contained in the csum. 520459931ecSChris Mason * 521459931ecSChris Mason * This calls btrfs_truncate_item with the correct args based on the 522459931ecSChris Mason * overlap, and fixes up the key as required. 523459931ecSChris Mason */ 524afe5fea7STsutomu Itoh static noinline void truncate_one_csum(struct btrfs_root *root, 525459931ecSChris Mason struct btrfs_path *path, 526459931ecSChris Mason struct btrfs_key *key, 527459931ecSChris Mason u64 bytenr, u64 len) 528459931ecSChris Mason { 529459931ecSChris Mason struct extent_buffer *leaf; 5306c41761fSDavid Sterba u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 531459931ecSChris Mason u64 csum_end; 532459931ecSChris Mason u64 end_byte = bytenr + len; 533459931ecSChris Mason u32 blocksize_bits = root->fs_info->sb->s_blocksize_bits; 534459931ecSChris Mason 535459931ecSChris Mason leaf = path->nodes[0]; 536459931ecSChris Mason csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; 537459931ecSChris Mason csum_end <<= root->fs_info->sb->s_blocksize_bits; 538459931ecSChris Mason csum_end += key->offset; 539459931ecSChris Mason 540459931ecSChris Mason if (key->offset < bytenr && csum_end <= end_byte) { 541459931ecSChris Mason /* 542459931ecSChris Mason * [ bytenr - len ] 543459931ecSChris Mason * [ ] 544459931ecSChris Mason * [csum ] 545459931ecSChris Mason * A simple truncate off the end of the item 546459931ecSChris Mason */ 547459931ecSChris Mason u32 new_size = (bytenr - key->offset) >> blocksize_bits; 548459931ecSChris Mason new_size *= csum_size; 549afe5fea7STsutomu Itoh btrfs_truncate_item(root, path, new_size, 1); 550459931ecSChris Mason } else if (key->offset >= bytenr && csum_end > end_byte && 551459931ecSChris Mason end_byte > key->offset) { 552459931ecSChris Mason /* 553459931ecSChris Mason * [ bytenr - len ] 554459931ecSChris Mason * [ ] 555459931ecSChris Mason * [csum ] 556459931ecSChris Mason * we need to truncate from the beginning of the csum 557459931ecSChris Mason */ 558459931ecSChris Mason u32 new_size = (csum_end - end_byte) >> blocksize_bits; 559459931ecSChris Mason new_size *= csum_size; 560459931ecSChris Mason 561afe5fea7STsutomu Itoh btrfs_truncate_item(root, path, new_size, 0); 562459931ecSChris Mason 563459931ecSChris Mason key->offset = end_byte; 564afe5fea7STsutomu Itoh btrfs_set_item_key_safe(root, path, key); 565459931ecSChris Mason } else { 566459931ecSChris Mason BUG(); 567459931ecSChris Mason } 568459931ecSChris Mason } 569459931ecSChris Mason 570459931ecSChris Mason /* 571459931ecSChris Mason * deletes the csum items from the csum tree for a given 572459931ecSChris Mason * range of bytes. 573459931ecSChris Mason */ 574459931ecSChris Mason int btrfs_del_csums(struct btrfs_trans_handle *trans, 575459931ecSChris Mason struct btrfs_root *root, u64 bytenr, u64 len) 576459931ecSChris Mason { 577459931ecSChris Mason struct btrfs_path *path; 578459931ecSChris Mason struct btrfs_key key; 579459931ecSChris Mason u64 end_byte = bytenr + len; 580459931ecSChris Mason u64 csum_end; 581459931ecSChris Mason struct extent_buffer *leaf; 582459931ecSChris Mason int ret; 5836c41761fSDavid Sterba u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 584459931ecSChris Mason int blocksize_bits = root->fs_info->sb->s_blocksize_bits; 585459931ecSChris Mason 586459931ecSChris Mason root = root->fs_info->csum_root; 587459931ecSChris Mason 588459931ecSChris Mason path = btrfs_alloc_path(); 5892a29edc6Sliubo if (!path) 5902a29edc6Sliubo return -ENOMEM; 591459931ecSChris Mason 592459931ecSChris Mason while (1) { 593459931ecSChris Mason key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 594459931ecSChris Mason key.offset = end_byte - 1; 595459931ecSChris Mason key.type = BTRFS_EXTENT_CSUM_KEY; 596459931ecSChris Mason 597b9473439SChris Mason path->leave_spinning = 1; 598459931ecSChris Mason ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 599459931ecSChris Mason if (ret > 0) { 600459931ecSChris Mason if (path->slots[0] == 0) 60165a246c5STsutomu Itoh break; 602459931ecSChris Mason path->slots[0]--; 603ad0397a7SJosef Bacik } else if (ret < 0) { 60465a246c5STsutomu Itoh break; 605459931ecSChris Mason } 606ad0397a7SJosef Bacik 607459931ecSChris Mason leaf = path->nodes[0]; 608459931ecSChris Mason btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 609459931ecSChris Mason 610459931ecSChris Mason if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 611459931ecSChris Mason key.type != BTRFS_EXTENT_CSUM_KEY) { 612459931ecSChris Mason break; 613459931ecSChris Mason } 614459931ecSChris Mason 615459931ecSChris Mason if (key.offset >= end_byte) 616459931ecSChris Mason break; 617459931ecSChris Mason 618459931ecSChris Mason csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; 619459931ecSChris Mason csum_end <<= blocksize_bits; 620459931ecSChris Mason csum_end += key.offset; 621459931ecSChris Mason 622459931ecSChris Mason /* this csum ends before we start, we're done */ 623459931ecSChris Mason if (csum_end <= bytenr) 624459931ecSChris Mason break; 625459931ecSChris Mason 626459931ecSChris Mason /* delete the entire item, it is inside our range */ 627459931ecSChris Mason if (key.offset >= bytenr && csum_end <= end_byte) { 628459931ecSChris Mason ret = btrfs_del_item(trans, root, path); 62965a246c5STsutomu Itoh if (ret) 63065a246c5STsutomu Itoh goto out; 631dcbdd4dcSChris Mason if (key.offset == bytenr) 632dcbdd4dcSChris Mason break; 633459931ecSChris Mason } else if (key.offset < bytenr && csum_end > end_byte) { 634459931ecSChris Mason unsigned long offset; 635459931ecSChris Mason unsigned long shift_len; 636459931ecSChris Mason unsigned long item_offset; 637459931ecSChris Mason /* 638459931ecSChris Mason * [ bytenr - len ] 639459931ecSChris Mason * [csum ] 640459931ecSChris Mason * 641459931ecSChris Mason * Our bytes are in the middle of the csum, 642459931ecSChris Mason * we need to split this item and insert a new one. 643459931ecSChris Mason * 644459931ecSChris Mason * But we can't drop the path because the 645459931ecSChris Mason * csum could change, get removed, extended etc. 646459931ecSChris Mason * 647459931ecSChris Mason * The trick here is the max size of a csum item leaves 648459931ecSChris Mason * enough room in the tree block for a single 649459931ecSChris Mason * item header. So, we split the item in place, 650459931ecSChris Mason * adding a new header pointing to the existing 651459931ecSChris Mason * bytes. Then we loop around again and we have 652459931ecSChris Mason * a nicely formed csum item that we can neatly 653459931ecSChris Mason * truncate. 654459931ecSChris Mason */ 655459931ecSChris Mason offset = (bytenr - key.offset) >> blocksize_bits; 656459931ecSChris Mason offset *= csum_size; 657459931ecSChris Mason 658459931ecSChris Mason shift_len = (len >> blocksize_bits) * csum_size; 659459931ecSChris Mason 660459931ecSChris Mason item_offset = btrfs_item_ptr_offset(leaf, 661459931ecSChris Mason path->slots[0]); 662459931ecSChris Mason 663459931ecSChris Mason memset_extent_buffer(leaf, 0, item_offset + offset, 664459931ecSChris Mason shift_len); 665459931ecSChris Mason key.offset = bytenr; 666459931ecSChris Mason 667459931ecSChris Mason /* 668459931ecSChris Mason * btrfs_split_item returns -EAGAIN when the 669459931ecSChris Mason * item changed size or key 670459931ecSChris Mason */ 671459931ecSChris Mason ret = btrfs_split_item(trans, root, path, &key, offset); 67279787eaaSJeff Mahoney if (ret && ret != -EAGAIN) { 67379787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 67479787eaaSJeff Mahoney goto out; 67579787eaaSJeff Mahoney } 676459931ecSChris Mason 677459931ecSChris Mason key.offset = end_byte - 1; 678459931ecSChris Mason } else { 679afe5fea7STsutomu Itoh truncate_one_csum(root, path, &key, bytenr, len); 680dcbdd4dcSChris Mason if (key.offset < bytenr) 681dcbdd4dcSChris Mason break; 682459931ecSChris Mason } 683b3b4aa74SDavid Sterba btrfs_release_path(path); 684459931ecSChris Mason } 68565a246c5STsutomu Itoh ret = 0; 686459931ecSChris Mason out: 687459931ecSChris Mason btrfs_free_path(path); 68865a246c5STsutomu Itoh return ret; 689459931ecSChris Mason } 690459931ecSChris Mason 691065631f6SChris Mason int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, 692d20f7043SChris Mason struct btrfs_root *root, 693e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums) 694f254e52cSChris Mason { 695f254e52cSChris Mason struct btrfs_key file_key; 6966567e837SChris Mason struct btrfs_key found_key; 6975caf2a00SChris Mason struct btrfs_path *path; 698f254e52cSChris Mason struct btrfs_csum_item *item; 699065631f6SChris Mason struct btrfs_csum_item *item_end; 700ff79f819SChris Mason struct extent_buffer *leaf = NULL; 701f51a4a18SMiao Xie u64 next_offset; 702f51a4a18SMiao Xie u64 total_bytes = 0; 7036567e837SChris Mason u64 csum_offset; 704f51a4a18SMiao Xie u64 bytenr; 705f578d4bdSChris Mason u32 nritems; 706f578d4bdSChris Mason u32 ins_size; 707f51a4a18SMiao Xie int index = 0; 708f51a4a18SMiao Xie int found_next; 709f51a4a18SMiao Xie int ret; 7106c41761fSDavid Sterba u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 7116e92f5e6SChris Mason 7125caf2a00SChris Mason path = btrfs_alloc_path(); 713d8926bb3SMark Fasheh if (!path) 714d8926bb3SMark Fasheh return -ENOMEM; 715065631f6SChris Mason again: 716065631f6SChris Mason next_offset = (u64)-1; 717065631f6SChris Mason found_next = 0; 718f51a4a18SMiao Xie bytenr = sums->bytenr + total_bytes; 719d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 720f51a4a18SMiao Xie file_key.offset = bytenr; 721d20f7043SChris Mason btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY); 722a429e513SChris Mason 723f51a4a18SMiao Xie item = btrfs_lookup_csum(trans, root, path, bytenr, 1); 724ff79f819SChris Mason if (!IS_ERR(item)) { 725639cb586SChris Mason ret = 0; 726f51a4a18SMiao Xie leaf = path->nodes[0]; 727f51a4a18SMiao Xie item_end = btrfs_item_ptr(leaf, path->slots[0], 728f51a4a18SMiao Xie struct btrfs_csum_item); 729f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((char *)item_end + 730f51a4a18SMiao Xie btrfs_item_size_nr(leaf, path->slots[0])); 731a429e513SChris Mason goto found; 732ff79f819SChris Mason } 733a429e513SChris Mason ret = PTR_ERR(item); 7344a500fd1SYan, Zheng if (ret != -EFBIG && ret != -ENOENT) 7354a500fd1SYan, Zheng goto fail_unlock; 7364a500fd1SYan, Zheng 737a429e513SChris Mason if (ret == -EFBIG) { 738a429e513SChris Mason u32 item_size; 739a429e513SChris Mason /* we found one, but it isn't big enough yet */ 7405f39d397SChris Mason leaf = path->nodes[0]; 7415f39d397SChris Mason item_size = btrfs_item_size_nr(leaf, path->slots[0]); 742607d432dSJosef Bacik if ((item_size / csum_size) >= 743607d432dSJosef Bacik MAX_CSUM_ITEMS(root, csum_size)) { 744a429e513SChris Mason /* already at max size, make a new one */ 745a429e513SChris Mason goto insert; 746a429e513SChris Mason } 747a429e513SChris Mason } else { 748f578d4bdSChris Mason int slot = path->slots[0] + 1; 749a429e513SChris Mason /* we didn't find a csum item, insert one */ 750f578d4bdSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 751f578d4bdSChris Mason if (path->slots[0] >= nritems - 1) { 752f578d4bdSChris Mason ret = btrfs_next_leaf(root, path); 753b56baf5bSYan if (ret == 1) 754f578d4bdSChris Mason found_next = 1; 755b56baf5bSYan if (ret != 0) 756f578d4bdSChris Mason goto insert; 757b56baf5bSYan slot = 0; 758f578d4bdSChris Mason } 759f578d4bdSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); 760d20f7043SChris Mason if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 761d20f7043SChris Mason found_key.type != BTRFS_EXTENT_CSUM_KEY) { 762f578d4bdSChris Mason found_next = 1; 763f578d4bdSChris Mason goto insert; 764f578d4bdSChris Mason } 765f578d4bdSChris Mason next_offset = found_key.offset; 766f578d4bdSChris Mason found_next = 1; 767a429e513SChris Mason goto insert; 768a429e513SChris Mason } 769a429e513SChris Mason 770a429e513SChris Mason /* 771a429e513SChris Mason * at this point, we know the tree has an item, but it isn't big 772a429e513SChris Mason * enough yet to put our csum in. Grow it 773a429e513SChris Mason */ 774b3b4aa74SDavid Sterba btrfs_release_path(path); 7756567e837SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 776607d432dSJosef Bacik csum_size, 1); 7776567e837SChris Mason if (ret < 0) 77853863232SChris Mason goto fail_unlock; 779459931ecSChris Mason 780459931ecSChris Mason if (ret > 0) { 781459931ecSChris Mason if (path->slots[0] == 0) 7826567e837SChris Mason goto insert; 7836567e837SChris Mason path->slots[0]--; 784459931ecSChris Mason } 785459931ecSChris Mason 7865f39d397SChris Mason leaf = path->nodes[0]; 7875f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 788d20f7043SChris Mason csum_offset = (bytenr - found_key.offset) >> 7896567e837SChris Mason root->fs_info->sb->s_blocksize_bits; 790459931ecSChris Mason 791d20f7043SChris Mason if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY || 792d20f7043SChris Mason found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 793607d432dSJosef Bacik csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) { 7946567e837SChris Mason goto insert; 7956567e837SChris Mason } 796459931ecSChris Mason 7972f697dc6SLiu Bo if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) / 798607d432dSJosef Bacik csum_size) { 7992f697dc6SLiu Bo int extend_nr; 8002f697dc6SLiu Bo u64 tmp; 8012f697dc6SLiu Bo u32 diff; 8022f697dc6SLiu Bo u32 free_space; 803459931ecSChris Mason 8042f697dc6SLiu Bo if (btrfs_leaf_free_space(root, leaf) < 8052f697dc6SLiu Bo sizeof(struct btrfs_item) + csum_size * 2) 8062f697dc6SLiu Bo goto insert; 8072f697dc6SLiu Bo 8082f697dc6SLiu Bo free_space = btrfs_leaf_free_space(root, leaf) - 8092f697dc6SLiu Bo sizeof(struct btrfs_item) - csum_size; 810f51a4a18SMiao Xie tmp = sums->len - total_bytes; 8112f697dc6SLiu Bo tmp >>= root->fs_info->sb->s_blocksize_bits; 8122f697dc6SLiu Bo WARN_ON(tmp < 1); 8132f697dc6SLiu Bo 8142f697dc6SLiu Bo extend_nr = max_t(int, 1, (int)tmp); 8152f697dc6SLiu Bo diff = (csum_offset + extend_nr) * csum_size; 8162f697dc6SLiu Bo diff = min(diff, MAX_CSUM_ITEMS(root, csum_size) * csum_size); 817459931ecSChris Mason 8185f39d397SChris Mason diff = diff - btrfs_item_size_nr(leaf, path->slots[0]); 8192f697dc6SLiu Bo diff = min(free_space, diff); 8202f697dc6SLiu Bo diff /= csum_size; 8212f697dc6SLiu Bo diff *= csum_size; 822459931ecSChris Mason 8234b90c680STsutomu Itoh btrfs_extend_item(root, path, diff); 824f51a4a18SMiao Xie ret = 0; 8256567e837SChris Mason goto csum; 8266567e837SChris Mason } 8276567e837SChris Mason 8286567e837SChris Mason insert: 829b3b4aa74SDavid Sterba btrfs_release_path(path); 8306567e837SChris Mason csum_offset = 0; 831f578d4bdSChris Mason if (found_next) { 8322f697dc6SLiu Bo u64 tmp; 833d20f7043SChris Mason 834f51a4a18SMiao Xie tmp = sums->len - total_bytes; 835f578d4bdSChris Mason tmp >>= root->fs_info->sb->s_blocksize_bits; 8362f697dc6SLiu Bo tmp = min(tmp, (next_offset - file_key.offset) >> 8372f697dc6SLiu Bo root->fs_info->sb->s_blocksize_bits); 8382f697dc6SLiu Bo 839f578d4bdSChris Mason tmp = max((u64)1, tmp); 840607d432dSJosef Bacik tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size)); 841607d432dSJosef Bacik ins_size = csum_size * tmp; 842f578d4bdSChris Mason } else { 843607d432dSJosef Bacik ins_size = csum_size; 844f578d4bdSChris Mason } 845b9473439SChris Mason path->leave_spinning = 1; 8465caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 847f578d4bdSChris Mason ins_size); 848b9473439SChris Mason path->leave_spinning = 0; 84954aa1f4dSChris Mason if (ret < 0) 85053863232SChris Mason goto fail_unlock; 851a429e513SChris Mason if (ret != 0) { 852a429e513SChris Mason WARN_ON(1); 85353863232SChris Mason goto fail_unlock; 854a429e513SChris Mason } 8555f39d397SChris Mason leaf = path->nodes[0]; 856f51a4a18SMiao Xie csum: 8575f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 858f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((unsigned char *)item + 859f51a4a18SMiao Xie btrfs_item_size_nr(leaf, path->slots[0])); 860509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 861607d432dSJosef Bacik csum_offset * csum_size); 862b18c6685SChris Mason found: 863f51a4a18SMiao Xie ins_size = (u32)(sums->len - total_bytes) >> 864f51a4a18SMiao Xie root->fs_info->sb->s_blocksize_bits; 865f51a4a18SMiao Xie ins_size *= csum_size; 866f51a4a18SMiao Xie ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item, 867f51a4a18SMiao Xie ins_size); 868f51a4a18SMiao Xie write_extent_buffer(leaf, sums->sums + index, (unsigned long)item, 869f51a4a18SMiao Xie ins_size); 870aadfeb6eSChris Mason 871f51a4a18SMiao Xie ins_size /= csum_size; 872f51a4a18SMiao Xie total_bytes += ins_size * root->sectorsize; 873f51a4a18SMiao Xie index += ins_size; 874a6591715SChris Mason 8755caf2a00SChris Mason btrfs_mark_buffer_dirty(path->nodes[0]); 876e6dcd2dcSChris Mason if (total_bytes < sums->len) { 877b3b4aa74SDavid Sterba btrfs_release_path(path); 878b9473439SChris Mason cond_resched(); 879065631f6SChris Mason goto again; 880065631f6SChris Mason } 88153863232SChris Mason out: 8825caf2a00SChris Mason btrfs_free_path(path); 883f254e52cSChris Mason return ret; 88453863232SChris Mason 88553863232SChris Mason fail_unlock: 88653863232SChris Mason goto out; 887f254e52cSChris Mason } 888