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" 26facc8a22SMiao Xie #include "volumes.h" 271de037a4SChris Mason #include "print-tree.h" 28ebb8765bSAnand Jain #include "compression.h" 291e1d2701SChris Mason 3042049bf6SChris Mason #define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \ 3142049bf6SChris Mason sizeof(struct btrfs_item) * 2) / \ 3242049bf6SChris Mason size) - 1)) 3307d400a6SYan Zheng 34221b8318SZach Brown #define MAX_CSUM_ITEMS(r, size) (min_t(u32, __MAX_CSUM_ITEMS(r, size), \ 3509cbfeafSKirill A. Shutemov PAGE_SIZE)) 367ca4be45SChris Mason 37da17066cSJeff Mahoney #define MAX_ORDERED_SUM_BYTES(fs_info) ((PAGE_SIZE - \ 3807d400a6SYan Zheng sizeof(struct btrfs_ordered_sum)) / \ 39da17066cSJeff Mahoney sizeof(u32) * (fs_info)->sectorsize) 4007d400a6SYan Zheng 41b18c6685SChris Mason int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, 42dee26a9fSChris Mason struct btrfs_root *root, 43b18c6685SChris Mason u64 objectid, u64 pos, 44f2eb0a24SSage Weil u64 disk_offset, u64 disk_num_bytes, 45c8b97818SChris Mason u64 num_bytes, u64 offset, u64 ram_bytes, 46c8b97818SChris Mason u8 compression, u8 encryption, u16 other_encoding) 479f5fae2fSChris Mason { 48dee26a9fSChris Mason int ret = 0; 49dee26a9fSChris Mason struct btrfs_file_extent_item *item; 50dee26a9fSChris Mason struct btrfs_key file_key; 515caf2a00SChris Mason struct btrfs_path *path; 525f39d397SChris Mason struct extent_buffer *leaf; 53dee26a9fSChris Mason 545caf2a00SChris Mason path = btrfs_alloc_path(); 55db5b493aSTsutomu Itoh if (!path) 56db5b493aSTsutomu Itoh return -ENOMEM; 57dee26a9fSChris Mason file_key.objectid = objectid; 58b18c6685SChris Mason file_key.offset = pos; 59962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_DATA_KEY; 60dee26a9fSChris Mason 61b9473439SChris Mason path->leave_spinning = 1; 625caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 63dee26a9fSChris Mason sizeof(*item)); 6454aa1f4dSChris Mason if (ret < 0) 6554aa1f4dSChris Mason goto out; 6679787eaaSJeff Mahoney BUG_ON(ret); /* Can't happen */ 675f39d397SChris Mason leaf = path->nodes[0]; 685f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], 69dee26a9fSChris Mason struct btrfs_file_extent_item); 70f2eb0a24SSage Weil btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset); 71db94535dSChris Mason btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes); 72f2eb0a24SSage Weil btrfs_set_file_extent_offset(leaf, item, offset); 73db94535dSChris Mason btrfs_set_file_extent_num_bytes(leaf, item, num_bytes); 74c8b97818SChris Mason btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes); 755f39d397SChris Mason btrfs_set_file_extent_generation(leaf, item, trans->transid); 765f39d397SChris Mason btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); 77c8b97818SChris Mason btrfs_set_file_extent_compression(leaf, item, compression); 78c8b97818SChris Mason btrfs_set_file_extent_encryption(leaf, item, encryption); 79c8b97818SChris Mason btrfs_set_file_extent_other_encoding(leaf, item, other_encoding); 80c8b97818SChris Mason 815f39d397SChris Mason btrfs_mark_buffer_dirty(leaf); 8254aa1f4dSChris Mason out: 835caf2a00SChris Mason btrfs_free_path(path); 8454aa1f4dSChris Mason return ret; 859f5fae2fSChris Mason } 86dee26a9fSChris Mason 8748a3b636SEric Sandeen static struct btrfs_csum_item * 8848a3b636SEric Sandeen btrfs_lookup_csum(struct btrfs_trans_handle *trans, 89b18c6685SChris Mason struct btrfs_root *root, 906567e837SChris Mason struct btrfs_path *path, 91d20f7043SChris Mason u64 bytenr, int cow) 926567e837SChris Mason { 930b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 946567e837SChris Mason int ret; 956567e837SChris Mason struct btrfs_key file_key; 966567e837SChris Mason struct btrfs_key found_key; 976567e837SChris Mason struct btrfs_csum_item *item; 985f39d397SChris Mason struct extent_buffer *leaf; 996567e837SChris Mason u64 csum_offset = 0; 1000b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 101a429e513SChris Mason int csums_in_item; 1026567e837SChris Mason 103d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 104d20f7043SChris Mason file_key.offset = bytenr; 105962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_CSUM_KEY; 106b18c6685SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); 1076567e837SChris Mason if (ret < 0) 1086567e837SChris Mason goto fail; 1095f39d397SChris Mason leaf = path->nodes[0]; 1106567e837SChris Mason if (ret > 0) { 1116567e837SChris Mason ret = 1; 11270b2befdSChris Mason if (path->slots[0] == 0) 1136567e837SChris Mason goto fail; 1146567e837SChris Mason path->slots[0]--; 1155f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 116962a298fSDavid Sterba if (found_key.type != BTRFS_EXTENT_CSUM_KEY) 1176567e837SChris Mason goto fail; 118d20f7043SChris Mason 119d20f7043SChris Mason csum_offset = (bytenr - found_key.offset) >> 1200b246afaSJeff Mahoney fs_info->sb->s_blocksize_bits; 1215f39d397SChris Mason csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]); 122607d432dSJosef Bacik csums_in_item /= csum_size; 123a429e513SChris Mason 12482d130ffSMiao Xie if (csum_offset == csums_in_item) { 125a429e513SChris Mason ret = -EFBIG; 1266567e837SChris Mason goto fail; 12782d130ffSMiao Xie } else if (csum_offset > csums_in_item) { 12882d130ffSMiao Xie goto fail; 1296567e837SChris Mason } 1306567e837SChris Mason } 1316567e837SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 132509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 133607d432dSJosef Bacik csum_offset * csum_size); 1346567e837SChris Mason return item; 1356567e837SChris Mason fail: 1366567e837SChris Mason if (ret > 0) 137b18c6685SChris Mason ret = -ENOENT; 1386567e837SChris Mason return ERR_PTR(ret); 1396567e837SChris Mason } 1406567e837SChris Mason 141dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, 142dee26a9fSChris Mason struct btrfs_root *root, 143dee26a9fSChris Mason struct btrfs_path *path, u64 objectid, 1449773a788SChris Mason u64 offset, int mod) 145dee26a9fSChris Mason { 146dee26a9fSChris Mason int ret; 147dee26a9fSChris Mason struct btrfs_key file_key; 148dee26a9fSChris Mason int ins_len = mod < 0 ? -1 : 0; 149dee26a9fSChris Mason int cow = mod != 0; 150dee26a9fSChris Mason 151dee26a9fSChris Mason file_key.objectid = objectid; 15270b2befdSChris Mason file_key.offset = offset; 153962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_DATA_KEY; 154dee26a9fSChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); 155dee26a9fSChris Mason return ret; 156dee26a9fSChris Mason } 157f254e52cSChris Mason 158facc8a22SMiao Xie static void btrfs_io_bio_endio_readpage(struct btrfs_io_bio *bio, int err) 159facc8a22SMiao Xie { 160facc8a22SMiao Xie kfree(bio->csum_allocated); 161facc8a22SMiao Xie } 162facc8a22SMiao Xie 1632ff7e61eSJeff Mahoney static int __btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, 1644b46fce2SJosef Bacik u64 logical_offset, u32 *dst, int dio) 16561b49440SChris Mason { 1660b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 167*17347cecSLiu Bo struct bio_vec bvec; 168*17347cecSLiu Bo struct bvec_iter iter; 169facc8a22SMiao Xie struct btrfs_io_bio *btrfs_bio = btrfs_io_bio(bio); 170facc8a22SMiao Xie struct btrfs_csum_item *item = NULL; 171facc8a22SMiao Xie struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 172facc8a22SMiao Xie struct btrfs_path *path; 173facc8a22SMiao Xie u8 *csum; 1744b46fce2SJosef Bacik u64 offset = 0; 17561b49440SChris Mason u64 item_start_offset = 0; 17661b49440SChris Mason u64 item_last_offset = 0; 177d20f7043SChris Mason u64 disk_bytenr; 178c40a3d38SChandan Rajendra u64 page_bytes_left; 17961b49440SChris Mason u32 diff; 180facc8a22SMiao Xie int nblocks; 181*17347cecSLiu Bo int count = 0; 1820b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 18361b49440SChris Mason 18461b49440SChris Mason path = btrfs_alloc_path(); 185c2db1073STsutomu Itoh if (!path) 186c2db1073STsutomu Itoh return -ENOMEM; 187facc8a22SMiao Xie 1884f024f37SKent Overstreet nblocks = bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits; 189facc8a22SMiao Xie if (!dst) { 190facc8a22SMiao Xie if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) { 19131e818feSDavid Sterba btrfs_bio->csum_allocated = kmalloc_array(nblocks, 19231e818feSDavid Sterba csum_size, GFP_NOFS); 193facc8a22SMiao Xie if (!btrfs_bio->csum_allocated) { 194facc8a22SMiao Xie btrfs_free_path(path); 195facc8a22SMiao Xie return -ENOMEM; 196facc8a22SMiao Xie } 197facc8a22SMiao Xie btrfs_bio->csum = btrfs_bio->csum_allocated; 198facc8a22SMiao Xie btrfs_bio->end_io = btrfs_io_bio_endio_readpage; 199facc8a22SMiao Xie } else { 200facc8a22SMiao Xie btrfs_bio->csum = btrfs_bio->csum_inline; 201facc8a22SMiao Xie } 202facc8a22SMiao Xie csum = btrfs_bio->csum; 203facc8a22SMiao Xie } else { 204facc8a22SMiao Xie csum = (u8 *)dst; 205facc8a22SMiao Xie } 206facc8a22SMiao Xie 20709cbfeafSKirill A. Shutemov if (bio->bi_iter.bi_size > PAGE_SIZE * 8) 208e4058b54SDavid Sterba path->reada = READA_FORWARD; 20961b49440SChris Mason 2102cf8572dSChris Mason /* 2112cf8572dSChris Mason * the free space stuff is only read when it hasn't been 2122cf8572dSChris Mason * updated in the current transaction. So, we can safely 2132cf8572dSChris Mason * read from the commit root and sidestep a nasty deadlock 2142cf8572dSChris Mason * between reading the free space cache and updating the csum tree. 2152cf8572dSChris Mason */ 21670ddc553SNikolay Borisov if (btrfs_is_free_space_inode(BTRFS_I(inode))) { 2172cf8572dSChris Mason path->search_commit_root = 1; 218ddf23b3fSJosef Bacik path->skip_locking = 1; 219ddf23b3fSJosef Bacik } 2202cf8572dSChris Mason 2214f024f37SKent Overstreet disk_bytenr = (u64)bio->bi_iter.bi_sector << 9; 2224b46fce2SJosef Bacik if (dio) 2234b46fce2SJosef Bacik offset = logical_offset; 224c40a3d38SChandan Rajendra 225*17347cecSLiu Bo bio_for_each_segment(bvec, bio, iter) { 226*17347cecSLiu Bo page_bytes_left = bvec.bv_len; 2274989d277SChristoph Hellwig if (count) 2284989d277SChristoph Hellwig goto next; 2294989d277SChristoph Hellwig 2304b46fce2SJosef Bacik if (!dio) 231*17347cecSLiu Bo offset = page_offset(bvec.bv_page) + bvec.bv_offset; 232facc8a22SMiao Xie count = btrfs_find_ordered_sum(inode, offset, disk_bytenr, 233facc8a22SMiao Xie (u32 *)csum, nblocks); 234e4100d98SMiao Xie if (count) 23561b49440SChris Mason goto found; 23661b49440SChris Mason 237d20f7043SChris Mason if (!item || disk_bytenr < item_start_offset || 238d20f7043SChris Mason disk_bytenr >= item_last_offset) { 23961b49440SChris Mason struct btrfs_key found_key; 24061b49440SChris Mason u32 item_size; 24161b49440SChris Mason 24261b49440SChris Mason if (item) 243b3b4aa74SDavid Sterba btrfs_release_path(path); 2440b246afaSJeff Mahoney item = btrfs_lookup_csum(NULL, fs_info->csum_root, 245d20f7043SChris Mason path, disk_bytenr, 0); 24661b49440SChris Mason if (IS_ERR(item)) { 247e4100d98SMiao Xie count = 1; 248facc8a22SMiao Xie memset(csum, 0, csum_size); 24917d217feSYan Zheng if (BTRFS_I(inode)->root->root_key.objectid == 25017d217feSYan Zheng BTRFS_DATA_RELOC_TREE_OBJECTID) { 25117d217feSYan Zheng set_extent_bits(io_tree, offset, 2520b246afaSJeff Mahoney offset + fs_info->sectorsize - 1, 253ceeb0ae7SDavid Sterba EXTENT_NODATASUM); 25417d217feSYan Zheng } else { 2550b246afaSJeff Mahoney btrfs_info_rl(fs_info, 256efe120a0SFrank Holton "no csum found for inode %llu start %llu", 2574a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(inode)), offset); 25817d217feSYan Zheng } 2596dab8157SChris Mason item = NULL; 260b3b4aa74SDavid Sterba btrfs_release_path(path); 26161b49440SChris Mason goto found; 26261b49440SChris Mason } 26361b49440SChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key, 26461b49440SChris Mason path->slots[0]); 26561b49440SChris Mason 26661b49440SChris Mason item_start_offset = found_key.offset; 26761b49440SChris Mason item_size = btrfs_item_size_nr(path->nodes[0], 26861b49440SChris Mason path->slots[0]); 26961b49440SChris Mason item_last_offset = item_start_offset + 270607d432dSJosef Bacik (item_size / csum_size) * 2710b246afaSJeff Mahoney fs_info->sectorsize; 27261b49440SChris Mason item = btrfs_item_ptr(path->nodes[0], path->slots[0], 27361b49440SChris Mason struct btrfs_csum_item); 27461b49440SChris Mason } 27561b49440SChris Mason /* 27661b49440SChris Mason * this byte range must be able to fit inside 27761b49440SChris Mason * a single leaf so it will also fit inside a u32 27861b49440SChris Mason */ 279d20f7043SChris Mason diff = disk_bytenr - item_start_offset; 2800b246afaSJeff Mahoney diff = diff / fs_info->sectorsize; 281607d432dSJosef Bacik diff = diff * csum_size; 282facc8a22SMiao Xie count = min_t(int, nblocks, (item_last_offset - disk_bytenr) >> 283e4100d98SMiao Xie inode->i_sb->s_blocksize_bits); 284facc8a22SMiao Xie read_extent_buffer(path->nodes[0], csum, 2853de9d6b6SChris Mason ((unsigned long)item) + diff, 286e4100d98SMiao Xie csum_size * count); 28761b49440SChris Mason found: 288facc8a22SMiao Xie csum += count * csum_size; 289facc8a22SMiao Xie nblocks -= count; 2904989d277SChristoph Hellwig next: 291e4100d98SMiao Xie while (count--) { 2920b246afaSJeff Mahoney disk_bytenr += fs_info->sectorsize; 2930b246afaSJeff Mahoney offset += fs_info->sectorsize; 2940b246afaSJeff Mahoney page_bytes_left -= fs_info->sectorsize; 2954989d277SChristoph Hellwig if (!page_bytes_left) 2964989d277SChristoph Hellwig break; /* move to next bio */ 2974989d277SChristoph Hellwig } 2984989d277SChristoph Hellwig } 2994989d277SChristoph Hellwig 300389f239cSChris Mason WARN_ON_ONCE(count); 30161b49440SChris Mason btrfs_free_path(path); 30261b49440SChris Mason return 0; 30361b49440SChris Mason } 30461b49440SChris Mason 3052ff7e61eSJeff Mahoney int btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, u32 *dst) 3064b46fce2SJosef Bacik { 3072ff7e61eSJeff Mahoney return __btrfs_lookup_bio_sums(inode, bio, 0, dst, 0); 3084b46fce2SJosef Bacik } 3094b46fce2SJosef Bacik 3102ff7e61eSJeff Mahoney int btrfs_lookup_bio_sums_dio(struct inode *inode, struct bio *bio, u64 offset) 3114b46fce2SJosef Bacik { 3122ff7e61eSJeff Mahoney return __btrfs_lookup_bio_sums(inode, bio, offset, NULL, 1); 3134b46fce2SJosef Bacik } 3144b46fce2SJosef Bacik 31517d217feSYan Zheng int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, 316a2de733cSArne Jansen struct list_head *list, int search_commit) 31717d217feSYan Zheng { 3180b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 31917d217feSYan Zheng struct btrfs_key key; 32017d217feSYan Zheng struct btrfs_path *path; 32117d217feSYan Zheng struct extent_buffer *leaf; 32217d217feSYan Zheng struct btrfs_ordered_sum *sums; 32317d217feSYan Zheng struct btrfs_csum_item *item; 3240678b618SMark Fasheh LIST_HEAD(tmplist); 32517d217feSYan Zheng unsigned long offset; 32617d217feSYan Zheng int ret; 32717d217feSYan Zheng size_t size; 32817d217feSYan Zheng u64 csum_end; 3290b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 33017d217feSYan Zheng 3310b246afaSJeff Mahoney ASSERT(IS_ALIGNED(start, fs_info->sectorsize) && 3320b246afaSJeff Mahoney IS_ALIGNED(end + 1, fs_info->sectorsize)); 3334277a9c3SJosef Bacik 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; 340e4058b54SDavid Sterba path->reada = READA_FORWARD; 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) >> 3570b246afaSJeff Mahoney 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]); 3850b246afaSJeff Mahoney csum_end = key.offset + (size / csum_size) * fs_info->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, 3960b246afaSJeff Mahoney MAX_ORDERED_SUM_BYTES(fs_info)); 3970b246afaSJeff Mahoney sums = kzalloc(btrfs_ordered_sum_size(fs_info, 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) >> 4080b246afaSJeff Mahoney fs_info->sb->s_blocksize_bits; 40917d217feSYan Zheng offset *= csum_size; 4100b246afaSJeff Mahoney size >>= 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 4170b246afaSJeff Mahoney start += fs_info->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)) { 4256e5aafb2SChris Mason sums = list_entry(tmplist.next, 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 4352ff7e61eSJeff Mahoney int btrfs_csum_one_bio(struct inode *inode, struct bio *bio, 4362ff7e61eSJeff Mahoney u64 file_start, int contig) 437e015640fSChris Mason { 4380b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 439e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums; 4406cd7ce49SChristoph Hellwig struct btrfs_ordered_extent *ordered = NULL; 441e015640fSChris Mason char *data; 442*17347cecSLiu Bo struct bvec_iter iter; 443*17347cecSLiu Bo struct bio_vec bvec; 444f51a4a18SMiao Xie int index; 445c40a3d38SChandan Rajendra int nr_sectors; 4463edf7d33SChris Mason unsigned long total_bytes = 0; 4473edf7d33SChris Mason unsigned long this_sum_bytes = 0; 448*17347cecSLiu Bo int i; 4493edf7d33SChris Mason u64 offset; 450e015640fSChris Mason 4510b246afaSJeff Mahoney sums = kzalloc(btrfs_ordered_sum_size(fs_info, bio->bi_iter.bi_size), 4524f024f37SKent Overstreet GFP_NOFS); 453e015640fSChris Mason if (!sums) 454e015640fSChris Mason return -ENOMEM; 4553edf7d33SChris Mason 4564f024f37SKent Overstreet sums->len = bio->bi_iter.bi_size; 457e6dcd2dcSChris Mason INIT_LIST_HEAD(&sums->list); 458d20f7043SChris Mason 459d20f7043SChris Mason if (contig) 460d20f7043SChris Mason offset = file_start; 461d20f7043SChris Mason else 4626cd7ce49SChristoph Hellwig offset = 0; /* shut up gcc */ 463d20f7043SChris Mason 4644f024f37SKent Overstreet sums->bytenr = (u64)bio->bi_iter.bi_sector << 9; 465f51a4a18SMiao Xie index = 0; 466e015640fSChris Mason 467*17347cecSLiu Bo bio_for_each_segment(bvec, bio, iter) { 468d20f7043SChris Mason if (!contig) 469*17347cecSLiu Bo offset = page_offset(bvec.bv_page) + bvec.bv_offset; 470d20f7043SChris Mason 4716cd7ce49SChristoph Hellwig if (!ordered) { 4726cd7ce49SChristoph Hellwig ordered = btrfs_lookup_ordered_extent(inode, offset); 4736cd7ce49SChristoph Hellwig BUG_ON(!ordered); /* Logic error */ 4746cd7ce49SChristoph Hellwig } 4756cd7ce49SChristoph Hellwig 476*17347cecSLiu Bo data = kmap_atomic(bvec.bv_page); 477c40a3d38SChandan Rajendra 4780b246afaSJeff Mahoney nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, 479*17347cecSLiu Bo bvec.bv_len + fs_info->sectorsize 480c40a3d38SChandan Rajendra - 1); 481c40a3d38SChandan Rajendra 482c40a3d38SChandan Rajendra for (i = 0; i < nr_sectors; i++) { 483e58dd74bSJosef Bacik if (offset >= ordered->file_offset + ordered->len || 484e58dd74bSJosef Bacik offset < ordered->file_offset) { 4853edf7d33SChris Mason unsigned long bytes_left; 486c40a3d38SChandan Rajendra 487c40a3d38SChandan Rajendra kunmap_atomic(data); 4883edf7d33SChris Mason sums->len = this_sum_bytes; 4893edf7d33SChris Mason this_sum_bytes = 0; 4903edf7d33SChris Mason btrfs_add_ordered_sum(inode, ordered, sums); 4913edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 4923edf7d33SChris Mason 4934f024f37SKent Overstreet bytes_left = bio->bi_iter.bi_size - total_bytes; 4943edf7d33SChris Mason 4950b246afaSJeff Mahoney sums = kzalloc(btrfs_ordered_sum_size(fs_info, bytes_left), 4963edf7d33SChris Mason GFP_NOFS); 49779787eaaSJeff Mahoney BUG_ON(!sums); /* -ENOMEM */ 4983edf7d33SChris Mason sums->len = bytes_left; 499c40a3d38SChandan Rajendra ordered = btrfs_lookup_ordered_extent(inode, 500c40a3d38SChandan Rajendra offset); 501c40a3d38SChandan Rajendra ASSERT(ordered); /* Logic error */ 502c40a3d38SChandan Rajendra sums->bytenr = ((u64)bio->bi_iter.bi_sector << 9) 503c40a3d38SChandan Rajendra + total_bytes; 504f51a4a18SMiao Xie index = 0; 5053edf7d33SChris Mason 506*17347cecSLiu Bo data = kmap_atomic(bvec.bv_page); 507c40a3d38SChandan Rajendra } 508c40a3d38SChandan Rajendra 509f51a4a18SMiao Xie sums->sums[index] = ~(u32)0; 510c40a3d38SChandan Rajendra sums->sums[index] 511*17347cecSLiu Bo = btrfs_csum_data(data + bvec.bv_offset 5120b246afaSJeff Mahoney + (i * fs_info->sectorsize), 513f51a4a18SMiao Xie sums->sums[index], 5140b246afaSJeff Mahoney fs_info->sectorsize); 515f51a4a18SMiao Xie btrfs_csum_final(sums->sums[index], 516f51a4a18SMiao Xie (char *)(sums->sums + index)); 517c40a3d38SChandan Rajendra index++; 5180b246afaSJeff Mahoney offset += fs_info->sectorsize; 5190b246afaSJeff Mahoney this_sum_bytes += fs_info->sectorsize; 5200b246afaSJeff Mahoney total_bytes += fs_info->sectorsize; 521c40a3d38SChandan Rajendra } 522c40a3d38SChandan Rajendra 523c40a3d38SChandan Rajendra kunmap_atomic(data); 524e015640fSChris Mason } 525ed98b56aSChris Mason this_sum_bytes = 0; 5263edf7d33SChris Mason btrfs_add_ordered_sum(inode, ordered, sums); 5273edf7d33SChris Mason btrfs_put_ordered_extent(ordered); 528e015640fSChris Mason return 0; 529e015640fSChris Mason } 530e015640fSChris Mason 531459931ecSChris Mason /* 532459931ecSChris Mason * helper function for csum removal, this expects the 533459931ecSChris Mason * key to describe the csum pointed to by the path, and it expects 534459931ecSChris Mason * the csum to overlap the range [bytenr, len] 535459931ecSChris Mason * 536459931ecSChris Mason * The csum should not be entirely contained in the range and the 537459931ecSChris Mason * range should not be entirely contained in the csum. 538459931ecSChris Mason * 539459931ecSChris Mason * This calls btrfs_truncate_item with the correct args based on the 540459931ecSChris Mason * overlap, and fixes up the key as required. 541459931ecSChris Mason */ 5422ff7e61eSJeff Mahoney static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info, 543459931ecSChris Mason struct btrfs_path *path, 544459931ecSChris Mason struct btrfs_key *key, 545459931ecSChris Mason u64 bytenr, u64 len) 546459931ecSChris Mason { 547459931ecSChris Mason struct extent_buffer *leaf; 5480b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 549459931ecSChris Mason u64 csum_end; 550459931ecSChris Mason u64 end_byte = bytenr + len; 5510b246afaSJeff Mahoney u32 blocksize_bits = fs_info->sb->s_blocksize_bits; 552459931ecSChris Mason 553459931ecSChris Mason leaf = path->nodes[0]; 554459931ecSChris Mason csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; 5550b246afaSJeff Mahoney csum_end <<= fs_info->sb->s_blocksize_bits; 556459931ecSChris Mason csum_end += key->offset; 557459931ecSChris Mason 558459931ecSChris Mason if (key->offset < bytenr && csum_end <= end_byte) { 559459931ecSChris Mason /* 560459931ecSChris Mason * [ bytenr - len ] 561459931ecSChris Mason * [ ] 562459931ecSChris Mason * [csum ] 563459931ecSChris Mason * A simple truncate off the end of the item 564459931ecSChris Mason */ 565459931ecSChris Mason u32 new_size = (bytenr - key->offset) >> blocksize_bits; 566459931ecSChris Mason new_size *= csum_size; 5672ff7e61eSJeff Mahoney btrfs_truncate_item(fs_info, path, new_size, 1); 568459931ecSChris Mason } else if (key->offset >= bytenr && csum_end > end_byte && 569459931ecSChris Mason end_byte > key->offset) { 570459931ecSChris Mason /* 571459931ecSChris Mason * [ bytenr - len ] 572459931ecSChris Mason * [ ] 573459931ecSChris Mason * [csum ] 574459931ecSChris Mason * we need to truncate from the beginning of the csum 575459931ecSChris Mason */ 576459931ecSChris Mason u32 new_size = (csum_end - end_byte) >> blocksize_bits; 577459931ecSChris Mason new_size *= csum_size; 578459931ecSChris Mason 5792ff7e61eSJeff Mahoney btrfs_truncate_item(fs_info, path, new_size, 0); 580459931ecSChris Mason 581459931ecSChris Mason key->offset = end_byte; 5820b246afaSJeff Mahoney btrfs_set_item_key_safe(fs_info, path, key); 583459931ecSChris Mason } else { 584459931ecSChris Mason BUG(); 585459931ecSChris Mason } 586459931ecSChris Mason } 587459931ecSChris Mason 588459931ecSChris Mason /* 589459931ecSChris Mason * deletes the csum items from the csum tree for a given 590459931ecSChris Mason * range of bytes. 591459931ecSChris Mason */ 592459931ecSChris Mason int btrfs_del_csums(struct btrfs_trans_handle *trans, 5935b4aacefSJeff Mahoney struct btrfs_fs_info *fs_info, u64 bytenr, u64 len) 594459931ecSChris Mason { 5955b4aacefSJeff Mahoney struct btrfs_root *root = fs_info->csum_root; 596459931ecSChris Mason struct btrfs_path *path; 597459931ecSChris Mason struct btrfs_key key; 598459931ecSChris Mason u64 end_byte = bytenr + len; 599459931ecSChris Mason u64 csum_end; 600459931ecSChris Mason struct extent_buffer *leaf; 601459931ecSChris Mason int ret; 6020b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 6030b246afaSJeff Mahoney int blocksize_bits = fs_info->sb->s_blocksize_bits; 604459931ecSChris Mason 605459931ecSChris Mason path = btrfs_alloc_path(); 6062a29edc6Sliubo if (!path) 6072a29edc6Sliubo return -ENOMEM; 608459931ecSChris Mason 609459931ecSChris Mason while (1) { 610459931ecSChris Mason key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 611459931ecSChris Mason key.offset = end_byte - 1; 612459931ecSChris Mason key.type = BTRFS_EXTENT_CSUM_KEY; 613459931ecSChris Mason 614b9473439SChris Mason path->leave_spinning = 1; 615459931ecSChris Mason ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 616459931ecSChris Mason if (ret > 0) { 617459931ecSChris Mason if (path->slots[0] == 0) 61865a246c5STsutomu Itoh break; 619459931ecSChris Mason path->slots[0]--; 620ad0397a7SJosef Bacik } else if (ret < 0) { 62165a246c5STsutomu Itoh break; 622459931ecSChris Mason } 623ad0397a7SJosef Bacik 624459931ecSChris Mason leaf = path->nodes[0]; 625459931ecSChris Mason btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 626459931ecSChris Mason 627459931ecSChris Mason if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 628459931ecSChris Mason key.type != BTRFS_EXTENT_CSUM_KEY) { 629459931ecSChris Mason break; 630459931ecSChris Mason } 631459931ecSChris Mason 632459931ecSChris Mason if (key.offset >= end_byte) 633459931ecSChris Mason break; 634459931ecSChris Mason 635459931ecSChris Mason csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; 636459931ecSChris Mason csum_end <<= blocksize_bits; 637459931ecSChris Mason csum_end += key.offset; 638459931ecSChris Mason 639459931ecSChris Mason /* this csum ends before we start, we're done */ 640459931ecSChris Mason if (csum_end <= bytenr) 641459931ecSChris Mason break; 642459931ecSChris Mason 643459931ecSChris Mason /* delete the entire item, it is inside our range */ 644459931ecSChris Mason if (key.offset >= bytenr && csum_end <= end_byte) { 6456f546216SFilipe Manana int del_nr = 1; 6466f546216SFilipe Manana 6476f546216SFilipe Manana /* 6486f546216SFilipe Manana * Check how many csum items preceding this one in this 6496f546216SFilipe Manana * leaf correspond to our range and then delete them all 6506f546216SFilipe Manana * at once. 6516f546216SFilipe Manana */ 6526f546216SFilipe Manana if (key.offset > bytenr && path->slots[0] > 0) { 6536f546216SFilipe Manana int slot = path->slots[0] - 1; 6546f546216SFilipe Manana 6556f546216SFilipe Manana while (slot >= 0) { 6566f546216SFilipe Manana struct btrfs_key pk; 6576f546216SFilipe Manana 6586f546216SFilipe Manana btrfs_item_key_to_cpu(leaf, &pk, slot); 6596f546216SFilipe Manana if (pk.offset < bytenr || 6606f546216SFilipe Manana pk.type != BTRFS_EXTENT_CSUM_KEY || 6616f546216SFilipe Manana pk.objectid != 6626f546216SFilipe Manana BTRFS_EXTENT_CSUM_OBJECTID) 6636f546216SFilipe Manana break; 6646f546216SFilipe Manana path->slots[0] = slot; 6656f546216SFilipe Manana del_nr++; 6666f546216SFilipe Manana key.offset = pk.offset; 6676f546216SFilipe Manana slot--; 6686f546216SFilipe Manana } 6696f546216SFilipe Manana } 6706f546216SFilipe Manana ret = btrfs_del_items(trans, root, path, 6716f546216SFilipe Manana path->slots[0], del_nr); 67265a246c5STsutomu Itoh if (ret) 67365a246c5STsutomu Itoh goto out; 674dcbdd4dcSChris Mason if (key.offset == bytenr) 675dcbdd4dcSChris Mason break; 676459931ecSChris Mason } else if (key.offset < bytenr && csum_end > end_byte) { 677459931ecSChris Mason unsigned long offset; 678459931ecSChris Mason unsigned long shift_len; 679459931ecSChris Mason unsigned long item_offset; 680459931ecSChris Mason /* 681459931ecSChris Mason * [ bytenr - len ] 682459931ecSChris Mason * [csum ] 683459931ecSChris Mason * 684459931ecSChris Mason * Our bytes are in the middle of the csum, 685459931ecSChris Mason * we need to split this item and insert a new one. 686459931ecSChris Mason * 687459931ecSChris Mason * But we can't drop the path because the 688459931ecSChris Mason * csum could change, get removed, extended etc. 689459931ecSChris Mason * 690459931ecSChris Mason * The trick here is the max size of a csum item leaves 691459931ecSChris Mason * enough room in the tree block for a single 692459931ecSChris Mason * item header. So, we split the item in place, 693459931ecSChris Mason * adding a new header pointing to the existing 694459931ecSChris Mason * bytes. Then we loop around again and we have 695459931ecSChris Mason * a nicely formed csum item that we can neatly 696459931ecSChris Mason * truncate. 697459931ecSChris Mason */ 698459931ecSChris Mason offset = (bytenr - key.offset) >> blocksize_bits; 699459931ecSChris Mason offset *= csum_size; 700459931ecSChris Mason 701459931ecSChris Mason shift_len = (len >> blocksize_bits) * csum_size; 702459931ecSChris Mason 703459931ecSChris Mason item_offset = btrfs_item_ptr_offset(leaf, 704459931ecSChris Mason path->slots[0]); 705459931ecSChris Mason 706b159fa28SDavid Sterba memzero_extent_buffer(leaf, item_offset + offset, 707459931ecSChris Mason shift_len); 708459931ecSChris Mason key.offset = bytenr; 709459931ecSChris Mason 710459931ecSChris Mason /* 711459931ecSChris Mason * btrfs_split_item returns -EAGAIN when the 712459931ecSChris Mason * item changed size or key 713459931ecSChris Mason */ 714459931ecSChris Mason ret = btrfs_split_item(trans, root, path, &key, offset); 71579787eaaSJeff Mahoney if (ret && ret != -EAGAIN) { 71666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 71779787eaaSJeff Mahoney goto out; 71879787eaaSJeff Mahoney } 719459931ecSChris Mason 720459931ecSChris Mason key.offset = end_byte - 1; 721459931ecSChris Mason } else { 7222ff7e61eSJeff Mahoney truncate_one_csum(fs_info, path, &key, bytenr, len); 723dcbdd4dcSChris Mason if (key.offset < bytenr) 724dcbdd4dcSChris Mason break; 725459931ecSChris Mason } 726b3b4aa74SDavid Sterba btrfs_release_path(path); 727459931ecSChris Mason } 72865a246c5STsutomu Itoh ret = 0; 729459931ecSChris Mason out: 730459931ecSChris Mason btrfs_free_path(path); 73165a246c5STsutomu Itoh return ret; 732459931ecSChris Mason } 733459931ecSChris Mason 734065631f6SChris Mason int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, 735d20f7043SChris Mason struct btrfs_root *root, 736e6dcd2dcSChris Mason struct btrfs_ordered_sum *sums) 737f254e52cSChris Mason { 7380b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 739f254e52cSChris Mason struct btrfs_key file_key; 7406567e837SChris Mason struct btrfs_key found_key; 7415caf2a00SChris Mason struct btrfs_path *path; 742f254e52cSChris Mason struct btrfs_csum_item *item; 743065631f6SChris Mason struct btrfs_csum_item *item_end; 744ff79f819SChris Mason struct extent_buffer *leaf = NULL; 745f51a4a18SMiao Xie u64 next_offset; 746f51a4a18SMiao Xie u64 total_bytes = 0; 7476567e837SChris Mason u64 csum_offset; 748f51a4a18SMiao Xie u64 bytenr; 749f578d4bdSChris Mason u32 nritems; 750f578d4bdSChris Mason u32 ins_size; 751f51a4a18SMiao Xie int index = 0; 752f51a4a18SMiao Xie int found_next; 753f51a4a18SMiao Xie int ret; 7540b246afaSJeff Mahoney u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 7556e92f5e6SChris Mason 7565caf2a00SChris Mason path = btrfs_alloc_path(); 757d8926bb3SMark Fasheh if (!path) 758d8926bb3SMark Fasheh return -ENOMEM; 759065631f6SChris Mason again: 760065631f6SChris Mason next_offset = (u64)-1; 761065631f6SChris Mason found_next = 0; 762f51a4a18SMiao Xie bytenr = sums->bytenr + total_bytes; 763d20f7043SChris Mason file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 764f51a4a18SMiao Xie file_key.offset = bytenr; 765962a298fSDavid Sterba file_key.type = BTRFS_EXTENT_CSUM_KEY; 766a429e513SChris Mason 767f51a4a18SMiao Xie item = btrfs_lookup_csum(trans, root, path, bytenr, 1); 768ff79f819SChris Mason if (!IS_ERR(item)) { 769639cb586SChris Mason ret = 0; 770f51a4a18SMiao Xie leaf = path->nodes[0]; 771f51a4a18SMiao Xie item_end = btrfs_item_ptr(leaf, path->slots[0], 772f51a4a18SMiao Xie struct btrfs_csum_item); 773f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((char *)item_end + 774f51a4a18SMiao Xie btrfs_item_size_nr(leaf, path->slots[0])); 775a429e513SChris Mason goto found; 776ff79f819SChris Mason } 777a429e513SChris Mason ret = PTR_ERR(item); 7784a500fd1SYan, Zheng if (ret != -EFBIG && ret != -ENOENT) 7794a500fd1SYan, Zheng goto fail_unlock; 7804a500fd1SYan, Zheng 781a429e513SChris Mason if (ret == -EFBIG) { 782a429e513SChris Mason u32 item_size; 783a429e513SChris Mason /* we found one, but it isn't big enough yet */ 7845f39d397SChris Mason leaf = path->nodes[0]; 7855f39d397SChris Mason item_size = btrfs_item_size_nr(leaf, path->slots[0]); 786607d432dSJosef Bacik if ((item_size / csum_size) >= 7870b246afaSJeff Mahoney MAX_CSUM_ITEMS(fs_info, csum_size)) { 788a429e513SChris Mason /* already at max size, make a new one */ 789a429e513SChris Mason goto insert; 790a429e513SChris Mason } 791a429e513SChris Mason } else { 792f578d4bdSChris Mason int slot = path->slots[0] + 1; 793a429e513SChris Mason /* we didn't find a csum item, insert one */ 794f578d4bdSChris Mason nritems = btrfs_header_nritems(path->nodes[0]); 79535045bf2SFilipe Manana if (!nritems || (path->slots[0] >= nritems - 1)) { 796f578d4bdSChris Mason ret = btrfs_next_leaf(root, path); 797b56baf5bSYan if (ret == 1) 798f578d4bdSChris Mason found_next = 1; 799b56baf5bSYan if (ret != 0) 800f578d4bdSChris Mason goto insert; 80127b9a812SFilipe Manana slot = path->slots[0]; 802f578d4bdSChris Mason } 803f578d4bdSChris Mason btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); 804d20f7043SChris Mason if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 805d20f7043SChris Mason found_key.type != BTRFS_EXTENT_CSUM_KEY) { 806f578d4bdSChris Mason found_next = 1; 807f578d4bdSChris Mason goto insert; 808f578d4bdSChris Mason } 809f578d4bdSChris Mason next_offset = found_key.offset; 810f578d4bdSChris Mason found_next = 1; 811a429e513SChris Mason goto insert; 812a429e513SChris Mason } 813a429e513SChris Mason 814a429e513SChris Mason /* 815a429e513SChris Mason * at this point, we know the tree has an item, but it isn't big 816a429e513SChris Mason * enough yet to put our csum in. Grow it 817a429e513SChris Mason */ 818b3b4aa74SDavid Sterba btrfs_release_path(path); 8196567e837SChris Mason ret = btrfs_search_slot(trans, root, &file_key, path, 820607d432dSJosef Bacik csum_size, 1); 8216567e837SChris Mason if (ret < 0) 82253863232SChris Mason goto fail_unlock; 823459931ecSChris Mason 824459931ecSChris Mason if (ret > 0) { 825459931ecSChris Mason if (path->slots[0] == 0) 8266567e837SChris Mason goto insert; 8276567e837SChris Mason path->slots[0]--; 828459931ecSChris Mason } 829459931ecSChris Mason 8305f39d397SChris Mason leaf = path->nodes[0]; 8315f39d397SChris Mason btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 832d20f7043SChris Mason csum_offset = (bytenr - found_key.offset) >> 8330b246afaSJeff Mahoney fs_info->sb->s_blocksize_bits; 834459931ecSChris Mason 835962a298fSDavid Sterba if (found_key.type != BTRFS_EXTENT_CSUM_KEY || 836d20f7043SChris Mason found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 8370b246afaSJeff Mahoney csum_offset >= MAX_CSUM_ITEMS(fs_info, csum_size)) { 8386567e837SChris Mason goto insert; 8396567e837SChris Mason } 840459931ecSChris Mason 8412f697dc6SLiu Bo if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) / 842607d432dSJosef Bacik csum_size) { 8432f697dc6SLiu Bo int extend_nr; 8442f697dc6SLiu Bo u64 tmp; 8452f697dc6SLiu Bo u32 diff; 8462f697dc6SLiu Bo u32 free_space; 847459931ecSChris Mason 8482ff7e61eSJeff Mahoney if (btrfs_leaf_free_space(fs_info, leaf) < 8492f697dc6SLiu Bo sizeof(struct btrfs_item) + csum_size * 2) 8502f697dc6SLiu Bo goto insert; 8512f697dc6SLiu Bo 8522ff7e61eSJeff Mahoney free_space = btrfs_leaf_free_space(fs_info, leaf) - 8532f697dc6SLiu Bo sizeof(struct btrfs_item) - csum_size; 854f51a4a18SMiao Xie tmp = sums->len - total_bytes; 8550b246afaSJeff Mahoney tmp >>= fs_info->sb->s_blocksize_bits; 8562f697dc6SLiu Bo WARN_ON(tmp < 1); 8572f697dc6SLiu Bo 8582f697dc6SLiu Bo extend_nr = max_t(int, 1, (int)tmp); 8592f697dc6SLiu Bo diff = (csum_offset + extend_nr) * csum_size; 8600b246afaSJeff Mahoney diff = min(diff, 8610b246afaSJeff Mahoney MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size); 862459931ecSChris Mason 8635f39d397SChris Mason diff = diff - btrfs_item_size_nr(leaf, path->slots[0]); 8642f697dc6SLiu Bo diff = min(free_space, diff); 8652f697dc6SLiu Bo diff /= csum_size; 8662f697dc6SLiu Bo diff *= csum_size; 867459931ecSChris Mason 8682ff7e61eSJeff Mahoney btrfs_extend_item(fs_info, path, diff); 869f51a4a18SMiao Xie ret = 0; 8706567e837SChris Mason goto csum; 8716567e837SChris Mason } 8726567e837SChris Mason 8736567e837SChris Mason insert: 874b3b4aa74SDavid Sterba btrfs_release_path(path); 8756567e837SChris Mason csum_offset = 0; 876f578d4bdSChris Mason if (found_next) { 8772f697dc6SLiu Bo u64 tmp; 878d20f7043SChris Mason 879f51a4a18SMiao Xie tmp = sums->len - total_bytes; 8800b246afaSJeff Mahoney tmp >>= fs_info->sb->s_blocksize_bits; 8812f697dc6SLiu Bo tmp = min(tmp, (next_offset - file_key.offset) >> 8820b246afaSJeff Mahoney fs_info->sb->s_blocksize_bits); 8832f697dc6SLiu Bo 88450d0446eSSeraphime Kirkovski tmp = max_t(u64, 1, tmp); 88550d0446eSSeraphime Kirkovski tmp = min_t(u64, tmp, MAX_CSUM_ITEMS(fs_info, csum_size)); 886607d432dSJosef Bacik ins_size = csum_size * tmp; 887f578d4bdSChris Mason } else { 888607d432dSJosef Bacik ins_size = csum_size; 889f578d4bdSChris Mason } 890b9473439SChris Mason path->leave_spinning = 1; 8915caf2a00SChris Mason ret = btrfs_insert_empty_item(trans, root, path, &file_key, 892f578d4bdSChris Mason ins_size); 893b9473439SChris Mason path->leave_spinning = 0; 89454aa1f4dSChris Mason if (ret < 0) 89553863232SChris Mason goto fail_unlock; 896fae7f21cSDulshani Gunawardhana if (WARN_ON(ret != 0)) 89753863232SChris Mason goto fail_unlock; 8985f39d397SChris Mason leaf = path->nodes[0]; 899f51a4a18SMiao Xie csum: 9005f39d397SChris Mason item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 901f51a4a18SMiao Xie item_end = (struct btrfs_csum_item *)((unsigned char *)item + 902f51a4a18SMiao Xie btrfs_item_size_nr(leaf, path->slots[0])); 903509659cdSChris Mason item = (struct btrfs_csum_item *)((unsigned char *)item + 904607d432dSJosef Bacik csum_offset * csum_size); 905b18c6685SChris Mason found: 906f51a4a18SMiao Xie ins_size = (u32)(sums->len - total_bytes) >> 9070b246afaSJeff Mahoney fs_info->sb->s_blocksize_bits; 908f51a4a18SMiao Xie ins_size *= csum_size; 909f51a4a18SMiao Xie ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item, 910f51a4a18SMiao Xie ins_size); 911f51a4a18SMiao Xie write_extent_buffer(leaf, sums->sums + index, (unsigned long)item, 912f51a4a18SMiao Xie ins_size); 913aadfeb6eSChris Mason 914f51a4a18SMiao Xie ins_size /= csum_size; 9150b246afaSJeff Mahoney total_bytes += ins_size * fs_info->sectorsize; 916f51a4a18SMiao Xie index += ins_size; 917a6591715SChris Mason 9185caf2a00SChris Mason btrfs_mark_buffer_dirty(path->nodes[0]); 919e6dcd2dcSChris Mason if (total_bytes < sums->len) { 920b3b4aa74SDavid Sterba btrfs_release_path(path); 921b9473439SChris Mason cond_resched(); 922065631f6SChris Mason goto again; 923065631f6SChris Mason } 92453863232SChris Mason out: 9255caf2a00SChris Mason btrfs_free_path(path); 926f254e52cSChris Mason return ret; 92753863232SChris Mason 92853863232SChris Mason fail_unlock: 92953863232SChris Mason goto out; 930f254e52cSChris Mason } 9317ffbb598SFilipe Manana 9329cdc5124SNikolay Borisov void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, 9337ffbb598SFilipe Manana const struct btrfs_path *path, 9347ffbb598SFilipe Manana struct btrfs_file_extent_item *fi, 9357ffbb598SFilipe Manana const bool new_inline, 9367ffbb598SFilipe Manana struct extent_map *em) 9377ffbb598SFilipe Manana { 9389cdc5124SNikolay Borisov struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); 9399cdc5124SNikolay Borisov struct btrfs_root *root = inode->root; 9407ffbb598SFilipe Manana struct extent_buffer *leaf = path->nodes[0]; 9417ffbb598SFilipe Manana const int slot = path->slots[0]; 9427ffbb598SFilipe Manana struct btrfs_key key; 9437ffbb598SFilipe Manana u64 extent_start, extent_end; 9447ffbb598SFilipe Manana u64 bytenr; 9457ffbb598SFilipe Manana u8 type = btrfs_file_extent_type(leaf, fi); 9467ffbb598SFilipe Manana int compress_type = btrfs_file_extent_compression(leaf, fi); 9477ffbb598SFilipe Manana 9480b246afaSJeff Mahoney em->bdev = fs_info->fs_devices->latest_bdev; 9497ffbb598SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot); 9507ffbb598SFilipe Manana extent_start = key.offset; 9517ffbb598SFilipe Manana 9527ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_REG || 9537ffbb598SFilipe Manana type == BTRFS_FILE_EXTENT_PREALLOC) { 9547ffbb598SFilipe Manana extent_end = extent_start + 9557ffbb598SFilipe Manana btrfs_file_extent_num_bytes(leaf, fi); 9567ffbb598SFilipe Manana } else if (type == BTRFS_FILE_EXTENT_INLINE) { 9577ffbb598SFilipe Manana size_t size; 9587ffbb598SFilipe Manana size = btrfs_file_extent_inline_len(leaf, slot, fi); 959da17066cSJeff Mahoney extent_end = ALIGN(extent_start + size, 9600b246afaSJeff Mahoney fs_info->sectorsize); 9617ffbb598SFilipe Manana } 9627ffbb598SFilipe Manana 9637ffbb598SFilipe Manana em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); 9647ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_REG || 9657ffbb598SFilipe Manana type == BTRFS_FILE_EXTENT_PREALLOC) { 9667ffbb598SFilipe Manana em->start = extent_start; 9677ffbb598SFilipe Manana em->len = extent_end - extent_start; 9687ffbb598SFilipe Manana em->orig_start = extent_start - 9697ffbb598SFilipe Manana btrfs_file_extent_offset(leaf, fi); 9707ffbb598SFilipe Manana em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); 9717ffbb598SFilipe Manana bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); 9727ffbb598SFilipe Manana if (bytenr == 0) { 9737ffbb598SFilipe Manana em->block_start = EXTENT_MAP_HOLE; 9747ffbb598SFilipe Manana return; 9757ffbb598SFilipe Manana } 9767ffbb598SFilipe Manana if (compress_type != BTRFS_COMPRESS_NONE) { 9777ffbb598SFilipe Manana set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 9787ffbb598SFilipe Manana em->compress_type = compress_type; 9797ffbb598SFilipe Manana em->block_start = bytenr; 9807ffbb598SFilipe Manana em->block_len = em->orig_block_len; 9817ffbb598SFilipe Manana } else { 9827ffbb598SFilipe Manana bytenr += btrfs_file_extent_offset(leaf, fi); 9837ffbb598SFilipe Manana em->block_start = bytenr; 9847ffbb598SFilipe Manana em->block_len = em->len; 9857ffbb598SFilipe Manana if (type == BTRFS_FILE_EXTENT_PREALLOC) 9867ffbb598SFilipe Manana set_bit(EXTENT_FLAG_PREALLOC, &em->flags); 9877ffbb598SFilipe Manana } 9887ffbb598SFilipe Manana } else if (type == BTRFS_FILE_EXTENT_INLINE) { 9897ffbb598SFilipe Manana em->block_start = EXTENT_MAP_INLINE; 9907ffbb598SFilipe Manana em->start = extent_start; 9917ffbb598SFilipe Manana em->len = extent_end - extent_start; 9927ffbb598SFilipe Manana /* 9937ffbb598SFilipe Manana * Initialize orig_start and block_len with the same values 9947ffbb598SFilipe Manana * as in inode.c:btrfs_get_extent(). 9957ffbb598SFilipe Manana */ 9967ffbb598SFilipe Manana em->orig_start = EXTENT_MAP_HOLE; 9977ffbb598SFilipe Manana em->block_len = (u64)-1; 9987ffbb598SFilipe Manana if (!new_inline && compress_type != BTRFS_COMPRESS_NONE) { 9997ffbb598SFilipe Manana set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 10007ffbb598SFilipe Manana em->compress_type = compress_type; 10017ffbb598SFilipe Manana } 10027ffbb598SFilipe Manana } else { 10030b246afaSJeff Mahoney btrfs_err(fs_info, 10049cdc5124SNikolay Borisov "unknown file extent item type %d, inode %llu, offset %llu, " 10059cdc5124SNikolay Borisov "root %llu", type, btrfs_ino(inode), extent_start, 10067ffbb598SFilipe Manana root->root_key.objectid); 10077ffbb598SFilipe Manana } 10087ffbb598SFilipe Manana } 1009