1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 2aaedb55bSJosef Bacik /* 3aaedb55bSJosef Bacik * Copyright (C) 2013 Fusion IO. All rights reserved. 4aaedb55bSJosef Bacik */ 5aaedb55bSJosef Bacik 6b9ef22deSFeifei Xu #include <linux/types.h> 7aaedb55bSJosef Bacik #include "btrfs-tests.h" 8aaedb55bSJosef Bacik #include "../ctree.h" 9aaedb55bSJosef Bacik #include "../btrfs_inode.h" 10aaedb55bSJosef Bacik #include "../disk-io.h" 11aaedb55bSJosef Bacik #include "../extent_io.h" 12aaedb55bSJosef Bacik #include "../volumes.h" 13ebb8765bSAnand Jain #include "../compression.h" 14aaedb55bSJosef Bacik 15aaedb55bSJosef Bacik static void insert_extent(struct btrfs_root *root, u64 start, u64 len, 16aaedb55bSJosef Bacik u64 ram_bytes, u64 offset, u64 disk_bytenr, 17aaedb55bSJosef Bacik u64 disk_len, u32 type, u8 compression, int slot) 18aaedb55bSJosef Bacik { 19aaedb55bSJosef Bacik struct btrfs_path path; 20aaedb55bSJosef Bacik struct btrfs_file_extent_item *fi; 21aaedb55bSJosef Bacik struct extent_buffer *leaf = root->node; 22aaedb55bSJosef Bacik struct btrfs_key key; 23aaedb55bSJosef Bacik u32 value_len = sizeof(struct btrfs_file_extent_item); 24aaedb55bSJosef Bacik 25aaedb55bSJosef Bacik if (type == BTRFS_FILE_EXTENT_INLINE) 26aaedb55bSJosef Bacik value_len += len; 27aaedb55bSJosef Bacik memset(&path, 0, sizeof(path)); 28aaedb55bSJosef Bacik 29aaedb55bSJosef Bacik path.nodes[0] = leaf; 30aaedb55bSJosef Bacik path.slots[0] = slot; 31aaedb55bSJosef Bacik 32aaedb55bSJosef Bacik key.objectid = BTRFS_FIRST_FREE_OBJECTID; 33aaedb55bSJosef Bacik key.type = BTRFS_EXTENT_DATA_KEY; 34aaedb55bSJosef Bacik key.offset = start; 35aaedb55bSJosef Bacik 36aaedb55bSJosef Bacik setup_items_for_insert(root, &path, &key, &value_len, value_len, 37aaedb55bSJosef Bacik value_len + sizeof(struct btrfs_item), 1); 38aaedb55bSJosef Bacik fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); 39aaedb55bSJosef Bacik btrfs_set_file_extent_generation(leaf, fi, 1); 40aaedb55bSJosef Bacik btrfs_set_file_extent_type(leaf, fi, type); 41aaedb55bSJosef Bacik btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); 42aaedb55bSJosef Bacik btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_len); 43aaedb55bSJosef Bacik btrfs_set_file_extent_offset(leaf, fi, offset); 44aaedb55bSJosef Bacik btrfs_set_file_extent_num_bytes(leaf, fi, len); 45aaedb55bSJosef Bacik btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); 46aaedb55bSJosef Bacik btrfs_set_file_extent_compression(leaf, fi, compression); 47aaedb55bSJosef Bacik btrfs_set_file_extent_encryption(leaf, fi, 0); 48aaedb55bSJosef Bacik btrfs_set_file_extent_other_encoding(leaf, fi, 0); 49aaedb55bSJosef Bacik } 50aaedb55bSJosef Bacik 510e30db86SJosef Bacik static void insert_inode_item_key(struct btrfs_root *root) 520e30db86SJosef Bacik { 530e30db86SJosef Bacik struct btrfs_path path; 540e30db86SJosef Bacik struct extent_buffer *leaf = root->node; 550e30db86SJosef Bacik struct btrfs_key key; 560e30db86SJosef Bacik u32 value_len = 0; 570e30db86SJosef Bacik 580e30db86SJosef Bacik memset(&path, 0, sizeof(path)); 590e30db86SJosef Bacik 600e30db86SJosef Bacik path.nodes[0] = leaf; 610e30db86SJosef Bacik path.slots[0] = 0; 620e30db86SJosef Bacik 630e30db86SJosef Bacik key.objectid = BTRFS_INODE_ITEM_KEY; 640e30db86SJosef Bacik key.type = BTRFS_INODE_ITEM_KEY; 650e30db86SJosef Bacik key.offset = 0; 660e30db86SJosef Bacik 670e30db86SJosef Bacik setup_items_for_insert(root, &path, &key, &value_len, value_len, 680e30db86SJosef Bacik value_len + sizeof(struct btrfs_item), 1); 690e30db86SJosef Bacik } 700e30db86SJosef Bacik 71aaedb55bSJosef Bacik /* 72aaedb55bSJosef Bacik * Build the most complicated map of extents the earth has ever seen. We want 73aaedb55bSJosef Bacik * this so we can test all of the corner cases of btrfs_get_extent. Here is a 74aaedb55bSJosef Bacik * diagram of how the extents will look though this may not be possible we still 75aaedb55bSJosef Bacik * want to make sure everything acts normally (the last number is not inclusive) 76aaedb55bSJosef Bacik * 77b9ef22deSFeifei Xu * [0 - 5][5 - 6][ 6 - 4096 ][ 4096 - 4100][4100 - 8195][8195 - 12291] 78b9ef22deSFeifei Xu * [hole ][inline][hole but no extent][ hole ][ regular ][regular1 split] 79aaedb55bSJosef Bacik * 80b9ef22deSFeifei Xu * [12291 - 16387][16387 - 24579][24579 - 28675][ 28675 - 32771][32771 - 36867 ] 81b9ef22deSFeifei Xu * [ hole ][regular1 split][ prealloc ][ prealloc1 ][prealloc1 written] 82aaedb55bSJosef Bacik * 83b9ef22deSFeifei Xu * [36867 - 45059][45059 - 53251][53251 - 57347][57347 - 61443][61443- 69635] 84b9ef22deSFeifei Xu * [ prealloc1 ][ compressed ][ compressed1 ][ regular ][ compressed1] 85aaedb55bSJosef Bacik * 86b9ef22deSFeifei Xu * [69635-73731][ 73731 - 86019 ][86019-90115] 87b9ef22deSFeifei Xu * [ regular ][ hole but no extent][ regular ] 88aaedb55bSJosef Bacik */ 89b9ef22deSFeifei Xu static void setup_file_extents(struct btrfs_root *root, u32 sectorsize) 90aaedb55bSJosef Bacik { 91aaedb55bSJosef Bacik int slot = 0; 92ee22184bSByongho Lee u64 disk_bytenr = SZ_1M; 93aaedb55bSJosef Bacik u64 offset = 0; 94aaedb55bSJosef Bacik 95aaedb55bSJosef Bacik /* First we want a hole */ 96aaedb55bSJosef Bacik insert_extent(root, offset, 5, 5, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0, 97aaedb55bSJosef Bacik slot); 98aaedb55bSJosef Bacik slot++; 99aaedb55bSJosef Bacik offset += 5; 100aaedb55bSJosef Bacik 101aaedb55bSJosef Bacik /* 102aaedb55bSJosef Bacik * Now we want an inline extent, I don't think this is possible but hey 103aaedb55bSJosef Bacik * why not? Also keep in mind if we have an inline extent it counts as 104aaedb55bSJosef Bacik * the whole first page. If we were to expand it we would have to cow 105aaedb55bSJosef Bacik * and we wouldn't have an inline extent anymore. 106aaedb55bSJosef Bacik */ 107aaedb55bSJosef Bacik insert_extent(root, offset, 1, 1, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE, 0, 108aaedb55bSJosef Bacik slot); 109aaedb55bSJosef Bacik slot++; 110b9ef22deSFeifei Xu offset = sectorsize; 111aaedb55bSJosef Bacik 112aaedb55bSJosef Bacik /* Now another hole */ 113aaedb55bSJosef Bacik insert_extent(root, offset, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0, 114aaedb55bSJosef Bacik slot); 115aaedb55bSJosef Bacik slot++; 116aaedb55bSJosef Bacik offset += 4; 117aaedb55bSJosef Bacik 118aaedb55bSJosef Bacik /* Now for a regular extent */ 119b9ef22deSFeifei Xu insert_extent(root, offset, sectorsize - 1, sectorsize - 1, 0, 120b9ef22deSFeifei Xu disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot); 121aaedb55bSJosef Bacik slot++; 122b9ef22deSFeifei Xu disk_bytenr += sectorsize; 123b9ef22deSFeifei Xu offset += sectorsize - 1; 124aaedb55bSJosef Bacik 125aaedb55bSJosef Bacik /* 126aaedb55bSJosef Bacik * Now for 3 extents that were split from a hole punch so we test 127aaedb55bSJosef Bacik * offsets properly. 128aaedb55bSJosef Bacik */ 129b9ef22deSFeifei Xu insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr, 130b9ef22deSFeifei Xu 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot); 131b9ef22deSFeifei Xu slot++; 132b9ef22deSFeifei Xu offset += sectorsize; 133b9ef22deSFeifei Xu insert_extent(root, offset, sectorsize, sectorsize, 0, 0, 0, 134aaedb55bSJosef Bacik BTRFS_FILE_EXTENT_REG, 0, slot); 135aaedb55bSJosef Bacik slot++; 136b9ef22deSFeifei Xu offset += sectorsize; 137b9ef22deSFeifei Xu insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize, 138b9ef22deSFeifei Xu 2 * sectorsize, disk_bytenr, 4 * sectorsize, 139aaedb55bSJosef Bacik BTRFS_FILE_EXTENT_REG, 0, slot); 140aaedb55bSJosef Bacik slot++; 141b9ef22deSFeifei Xu offset += 2 * sectorsize; 142b9ef22deSFeifei Xu disk_bytenr += 4 * sectorsize; 143aaedb55bSJosef Bacik 144aaedb55bSJosef Bacik /* Now for a unwritten prealloc extent */ 145b9ef22deSFeifei Xu insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr, 146b9ef22deSFeifei Xu sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot); 147aaedb55bSJosef Bacik slot++; 148b9ef22deSFeifei Xu offset += sectorsize; 149aaedb55bSJosef Bacik 150aaedb55bSJosef Bacik /* 151aaedb55bSJosef Bacik * We want to jack up disk_bytenr a little more so the em stuff doesn't 152aaedb55bSJosef Bacik * merge our records. 153aaedb55bSJosef Bacik */ 154b9ef22deSFeifei Xu disk_bytenr += 2 * sectorsize; 155aaedb55bSJosef Bacik 156aaedb55bSJosef Bacik /* 157aaedb55bSJosef Bacik * Now for a partially written prealloc extent, basically the same as 158aaedb55bSJosef Bacik * the hole punch example above. Ram_bytes never changes when you mark 159aaedb55bSJosef Bacik * extents written btw. 160aaedb55bSJosef Bacik */ 161b9ef22deSFeifei Xu insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr, 162b9ef22deSFeifei Xu 4 * sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot); 163b9ef22deSFeifei Xu slot++; 164b9ef22deSFeifei Xu offset += sectorsize; 165b9ef22deSFeifei Xu insert_extent(root, offset, sectorsize, 4 * sectorsize, sectorsize, 166b9ef22deSFeifei Xu disk_bytenr, 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0, 167b9ef22deSFeifei Xu slot); 168b9ef22deSFeifei Xu slot++; 169b9ef22deSFeifei Xu offset += sectorsize; 170b9ef22deSFeifei Xu insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize, 171b9ef22deSFeifei Xu 2 * sectorsize, disk_bytenr, 4 * sectorsize, 172aaedb55bSJosef Bacik BTRFS_FILE_EXTENT_PREALLOC, 0, slot); 173aaedb55bSJosef Bacik slot++; 174b9ef22deSFeifei Xu offset += 2 * sectorsize; 175b9ef22deSFeifei Xu disk_bytenr += 4 * sectorsize; 176aaedb55bSJosef Bacik 177aaedb55bSJosef Bacik /* Now a normal compressed extent */ 178b9ef22deSFeifei Xu insert_extent(root, offset, 2 * sectorsize, 2 * sectorsize, 0, 179b9ef22deSFeifei Xu disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG, 180b9ef22deSFeifei Xu BTRFS_COMPRESS_ZLIB, slot); 181aaedb55bSJosef Bacik slot++; 182b9ef22deSFeifei Xu offset += 2 * sectorsize; 183aaedb55bSJosef Bacik /* No merges */ 184b9ef22deSFeifei Xu disk_bytenr += 2 * sectorsize; 185aaedb55bSJosef Bacik 186aaedb55bSJosef Bacik /* Now a split compressed extent */ 187b9ef22deSFeifei Xu insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr, 188b9ef22deSFeifei Xu sectorsize, BTRFS_FILE_EXTENT_REG, 189b9ef22deSFeifei Xu BTRFS_COMPRESS_ZLIB, slot); 190aaedb55bSJosef Bacik slot++; 191b9ef22deSFeifei Xu offset += sectorsize; 192b9ef22deSFeifei Xu insert_extent(root, offset, sectorsize, sectorsize, 0, 193b9ef22deSFeifei Xu disk_bytenr + sectorsize, sectorsize, 194aaedb55bSJosef Bacik BTRFS_FILE_EXTENT_REG, 0, slot); 195aaedb55bSJosef Bacik slot++; 196b9ef22deSFeifei Xu offset += sectorsize; 197b9ef22deSFeifei Xu insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize, 198b9ef22deSFeifei Xu 2 * sectorsize, disk_bytenr, sectorsize, 199aaedb55bSJosef Bacik BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot); 200aaedb55bSJosef Bacik slot++; 201b9ef22deSFeifei Xu offset += 2 * sectorsize; 202b9ef22deSFeifei Xu disk_bytenr += 2 * sectorsize; 203aaedb55bSJosef Bacik 204aaedb55bSJosef Bacik /* Now extents that have a hole but no hole extent */ 205b9ef22deSFeifei Xu insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr, 206b9ef22deSFeifei Xu sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot); 207aaedb55bSJosef Bacik slot++; 208b9ef22deSFeifei Xu offset += 4 * sectorsize; 209b9ef22deSFeifei Xu disk_bytenr += sectorsize; 210b9ef22deSFeifei Xu insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr, 211b9ef22deSFeifei Xu sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot); 212aaedb55bSJosef Bacik } 213aaedb55bSJosef Bacik 214aaedb55bSJosef Bacik static unsigned long prealloc_only = 0; 215aaedb55bSJosef Bacik static unsigned long compressed_only = 0; 216aaedb55bSJosef Bacik static unsigned long vacancy_only = 0; 217aaedb55bSJosef Bacik 218b9ef22deSFeifei Xu static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) 219aaedb55bSJosef Bacik { 2207c0260eeSJeff Mahoney struct btrfs_fs_info *fs_info = NULL; 221aaedb55bSJosef Bacik struct inode *inode = NULL; 222aaedb55bSJosef Bacik struct btrfs_root *root = NULL; 223aaedb55bSJosef Bacik struct extent_map *em = NULL; 224aaedb55bSJosef Bacik u64 orig_start; 225aaedb55bSJosef Bacik u64 disk_bytenr; 226aaedb55bSJosef Bacik u64 offset; 227aaedb55bSJosef Bacik int ret = -ENOMEM; 228aaedb55bSJosef Bacik 229aaedb55bSJosef Bacik inode = btrfs_new_test_inode(); 230aaedb55bSJosef Bacik if (!inode) { 2313c7251f2SDavid Sterba test_err("couldn't allocate inode"); 232aaedb55bSJosef Bacik return ret; 233aaedb55bSJosef Bacik } 234aaedb55bSJosef Bacik 235aaedb55bSJosef Bacik BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; 236aaedb55bSJosef Bacik BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID; 237aaedb55bSJosef Bacik BTRFS_I(inode)->location.offset = 0; 238aaedb55bSJosef Bacik 239da17066cSJeff Mahoney fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize); 2407c0260eeSJeff Mahoney if (!fs_info) { 24137b2a7bcSDavid Sterba test_std_err(TEST_ALLOC_FS_INFO); 242aaedb55bSJosef Bacik goto out; 243aaedb55bSJosef Bacik } 244aaedb55bSJosef Bacik 245da17066cSJeff Mahoney root = btrfs_alloc_dummy_root(fs_info); 2467c0260eeSJeff Mahoney if (IS_ERR(root)) { 24752ab7bcaSDavid Sterba test_std_err(TEST_ALLOC_ROOT); 248aaedb55bSJosef Bacik goto out; 249aaedb55bSJosef Bacik } 250aaedb55bSJosef Bacik 251da17066cSJeff Mahoney root->node = alloc_dummy_extent_buffer(fs_info, nodesize); 252aaedb55bSJosef Bacik if (!root->node) { 2539e3d9f84SDavid Sterba test_std_err(TEST_ALLOC_ROOT); 254aaedb55bSJosef Bacik goto out; 255aaedb55bSJosef Bacik } 256aaedb55bSJosef Bacik 257aaedb55bSJosef Bacik btrfs_set_header_nritems(root->node, 0); 258aaedb55bSJosef Bacik btrfs_set_header_level(root->node, 0); 259aaedb55bSJosef Bacik ret = -EINVAL; 260aaedb55bSJosef Bacik 261aaedb55bSJosef Bacik /* First with no extents */ 262aaedb55bSJosef Bacik BTRFS_I(inode)->root = root; 263fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, sectorsize, 0); 264aaedb55bSJosef Bacik if (IS_ERR(em)) { 265aaedb55bSJosef Bacik em = NULL; 2663c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 267aaedb55bSJosef Bacik goto out; 268aaedb55bSJosef Bacik } 269aaedb55bSJosef Bacik if (em->block_start != EXTENT_MAP_HOLE) { 2703c7251f2SDavid Sterba test_err("expected a hole, got %llu", em->block_start); 271aaedb55bSJosef Bacik goto out; 272aaedb55bSJosef Bacik } 273aaedb55bSJosef Bacik free_extent_map(em); 274dcdbc059SNikolay Borisov btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); 275aaedb55bSJosef Bacik 276aaedb55bSJosef Bacik /* 277aaedb55bSJosef Bacik * All of the magic numbers are based on the mapping setup in 278aaedb55bSJosef Bacik * setup_file_extents, so if you change anything there you need to 279aaedb55bSJosef Bacik * update the comment and update the expected values below. 280aaedb55bSJosef Bacik */ 281b9ef22deSFeifei Xu setup_file_extents(root, sectorsize); 282aaedb55bSJosef Bacik 283fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, (u64)-1, 0); 284aaedb55bSJosef Bacik if (IS_ERR(em)) { 2853c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 286aaedb55bSJosef Bacik goto out; 287aaedb55bSJosef Bacik } 288aaedb55bSJosef Bacik if (em->block_start != EXTENT_MAP_HOLE) { 2893c7251f2SDavid Sterba test_err("expected a hole, got %llu", em->block_start); 290aaedb55bSJosef Bacik goto out; 291aaedb55bSJosef Bacik } 292aaedb55bSJosef Bacik if (em->start != 0 || em->len != 5) { 2933c7251f2SDavid Sterba test_err( 2943c7251f2SDavid Sterba "unexpected extent wanted start 0 len 5, got start %llu len %llu", 2953c7251f2SDavid Sterba em->start, em->len); 296aaedb55bSJosef Bacik goto out; 297aaedb55bSJosef Bacik } 298aaedb55bSJosef Bacik if (em->flags != 0) { 2993c7251f2SDavid Sterba test_err("unexpected flags set, want 0 have %lu", em->flags); 300aaedb55bSJosef Bacik goto out; 301aaedb55bSJosef Bacik } 302aaedb55bSJosef Bacik offset = em->start + em->len; 303aaedb55bSJosef Bacik free_extent_map(em); 304aaedb55bSJosef Bacik 305fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 306aaedb55bSJosef Bacik if (IS_ERR(em)) { 3073c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 308aaedb55bSJosef Bacik goto out; 309aaedb55bSJosef Bacik } 310aaedb55bSJosef Bacik if (em->block_start != EXTENT_MAP_INLINE) { 3113c7251f2SDavid Sterba test_err("expected an inline, got %llu", em->block_start); 312aaedb55bSJosef Bacik goto out; 313aaedb55bSJosef Bacik } 314b9ef22deSFeifei Xu 315b9ef22deSFeifei Xu if (em->start != offset || em->len != (sectorsize - 5)) { 3163c7251f2SDavid Sterba test_err( 3173c7251f2SDavid Sterba "unexpected extent wanted start %llu len 1, got start %llu len %llu", 3183c7251f2SDavid Sterba offset, em->start, em->len); 319aaedb55bSJosef Bacik goto out; 320aaedb55bSJosef Bacik } 321aaedb55bSJosef Bacik if (em->flags != 0) { 3223c7251f2SDavid Sterba test_err("unexpected flags set, want 0 have %lu", em->flags); 323aaedb55bSJosef Bacik goto out; 324aaedb55bSJosef Bacik } 325aaedb55bSJosef Bacik /* 326aaedb55bSJosef Bacik * We don't test anything else for inline since it doesn't get set 327aaedb55bSJosef Bacik * unless we have a page for it to write into. Maybe we should change 328aaedb55bSJosef Bacik * this? 329aaedb55bSJosef Bacik */ 330aaedb55bSJosef Bacik offset = em->start + em->len; 331aaedb55bSJosef Bacik free_extent_map(em); 332aaedb55bSJosef Bacik 333fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 334aaedb55bSJosef Bacik if (IS_ERR(em)) { 3353c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 336aaedb55bSJosef Bacik goto out; 337aaedb55bSJosef Bacik } 338aaedb55bSJosef Bacik if (em->block_start != EXTENT_MAP_HOLE) { 3393c7251f2SDavid Sterba test_err("expected a hole, got %llu", em->block_start); 340aaedb55bSJosef Bacik goto out; 341aaedb55bSJosef Bacik } 342aaedb55bSJosef Bacik if (em->start != offset || em->len != 4) { 3433c7251f2SDavid Sterba test_err( 3443c7251f2SDavid Sterba "unexpected extent wanted start %llu len 4, got start %llu len %llu", 3453c7251f2SDavid Sterba offset, em->start, em->len); 346aaedb55bSJosef Bacik goto out; 347aaedb55bSJosef Bacik } 348aaedb55bSJosef Bacik if (em->flags != 0) { 3493c7251f2SDavid Sterba test_err("unexpected flags set, want 0 have %lu", em->flags); 350aaedb55bSJosef Bacik goto out; 351aaedb55bSJosef Bacik } 352aaedb55bSJosef Bacik offset = em->start + em->len; 353aaedb55bSJosef Bacik free_extent_map(em); 354aaedb55bSJosef Bacik 355aaedb55bSJosef Bacik /* Regular extent */ 356fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 357aaedb55bSJosef Bacik if (IS_ERR(em)) { 3583c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 359aaedb55bSJosef Bacik goto out; 360aaedb55bSJosef Bacik } 361aaedb55bSJosef Bacik if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 3623c7251f2SDavid Sterba test_err("expected a real extent, got %llu", em->block_start); 363aaedb55bSJosef Bacik goto out; 364aaedb55bSJosef Bacik } 365b9ef22deSFeifei Xu if (em->start != offset || em->len != sectorsize - 1) { 3663c7251f2SDavid Sterba test_err( 3673c7251f2SDavid Sterba "unexpected extent wanted start %llu len 4095, got start %llu len %llu", 3683c7251f2SDavid Sterba offset, em->start, em->len); 369aaedb55bSJosef Bacik goto out; 370aaedb55bSJosef Bacik } 371aaedb55bSJosef Bacik if (em->flags != 0) { 3723c7251f2SDavid Sterba test_err("unexpected flags set, want 0 have %lu", em->flags); 373aaedb55bSJosef Bacik goto out; 374aaedb55bSJosef Bacik } 375aaedb55bSJosef Bacik if (em->orig_start != em->start) { 3763c7251f2SDavid Sterba test_err("wrong orig offset, want %llu, have %llu", em->start, 377aaedb55bSJosef Bacik em->orig_start); 378aaedb55bSJosef Bacik goto out; 379aaedb55bSJosef Bacik } 380aaedb55bSJosef Bacik offset = em->start + em->len; 381aaedb55bSJosef Bacik free_extent_map(em); 382aaedb55bSJosef Bacik 383aaedb55bSJosef Bacik /* The next 3 are split extents */ 384fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 385aaedb55bSJosef Bacik if (IS_ERR(em)) { 3863c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 387aaedb55bSJosef Bacik goto out; 388aaedb55bSJosef Bacik } 389aaedb55bSJosef Bacik if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 3903c7251f2SDavid Sterba test_err("expected a real extent, got %llu", em->block_start); 391aaedb55bSJosef Bacik goto out; 392aaedb55bSJosef Bacik } 393b9ef22deSFeifei Xu if (em->start != offset || em->len != sectorsize) { 3943c7251f2SDavid Sterba test_err( 3953c7251f2SDavid Sterba "unexpected extent start %llu len %u, got start %llu len %llu", 396b9ef22deSFeifei Xu offset, sectorsize, em->start, em->len); 397aaedb55bSJosef Bacik goto out; 398aaedb55bSJosef Bacik } 399aaedb55bSJosef Bacik if (em->flags != 0) { 4003c7251f2SDavid Sterba test_err("unexpected flags set, want 0 have %lu", em->flags); 401aaedb55bSJosef Bacik goto out; 402aaedb55bSJosef Bacik } 403aaedb55bSJosef Bacik if (em->orig_start != em->start) { 4043c7251f2SDavid Sterba test_err("wrong orig offset, want %llu, have %llu", em->start, 405aaedb55bSJosef Bacik em->orig_start); 406aaedb55bSJosef Bacik goto out; 407aaedb55bSJosef Bacik } 408aaedb55bSJosef Bacik disk_bytenr = em->block_start; 409aaedb55bSJosef Bacik orig_start = em->start; 410aaedb55bSJosef Bacik offset = em->start + em->len; 411aaedb55bSJosef Bacik free_extent_map(em); 412aaedb55bSJosef Bacik 413fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 414aaedb55bSJosef Bacik if (IS_ERR(em)) { 4153c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 416aaedb55bSJosef Bacik goto out; 417aaedb55bSJosef Bacik } 418aaedb55bSJosef Bacik if (em->block_start != EXTENT_MAP_HOLE) { 4193c7251f2SDavid Sterba test_err("expected a hole, got %llu", em->block_start); 420aaedb55bSJosef Bacik goto out; 421aaedb55bSJosef Bacik } 422b9ef22deSFeifei Xu if (em->start != offset || em->len != sectorsize) { 4233c7251f2SDavid Sterba test_err( 4243c7251f2SDavid Sterba "unexpected extent wanted start %llu len %u, got start %llu len %llu", 425b9ef22deSFeifei Xu offset, sectorsize, em->start, em->len); 426aaedb55bSJosef Bacik goto out; 427aaedb55bSJosef Bacik } 428aaedb55bSJosef Bacik if (em->flags != 0) { 4293c7251f2SDavid Sterba test_err("unexpected flags set, want 0 have %lu", em->flags); 430aaedb55bSJosef Bacik goto out; 431aaedb55bSJosef Bacik } 432aaedb55bSJosef Bacik offset = em->start + em->len; 433aaedb55bSJosef Bacik free_extent_map(em); 434aaedb55bSJosef Bacik 435fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 436aaedb55bSJosef Bacik if (IS_ERR(em)) { 4373c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 438aaedb55bSJosef Bacik goto out; 439aaedb55bSJosef Bacik } 440aaedb55bSJosef Bacik if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 4413c7251f2SDavid Sterba test_err("expected a real extent, got %llu", em->block_start); 442aaedb55bSJosef Bacik goto out; 443aaedb55bSJosef Bacik } 444b9ef22deSFeifei Xu if (em->start != offset || em->len != 2 * sectorsize) { 4453c7251f2SDavid Sterba test_err( 4463c7251f2SDavid Sterba "unexpected extent wanted start %llu len %u, got start %llu len %llu", 447b9ef22deSFeifei Xu offset, 2 * sectorsize, em->start, em->len); 448aaedb55bSJosef Bacik goto out; 449aaedb55bSJosef Bacik } 450aaedb55bSJosef Bacik if (em->flags != 0) { 4513c7251f2SDavid Sterba test_err("unexpected flags set, want 0 have %lu", em->flags); 452aaedb55bSJosef Bacik goto out; 453aaedb55bSJosef Bacik } 454aaedb55bSJosef Bacik if (em->orig_start != orig_start) { 4553c7251f2SDavid Sterba test_err("wrong orig offset, want %llu, have %llu", 456aaedb55bSJosef Bacik orig_start, em->orig_start); 457aaedb55bSJosef Bacik goto out; 458aaedb55bSJosef Bacik } 459aaedb55bSJosef Bacik disk_bytenr += (em->start - orig_start); 460aaedb55bSJosef Bacik if (em->block_start != disk_bytenr) { 4613c7251f2SDavid Sterba test_err("wrong block start, want %llu, have %llu", 462aaedb55bSJosef Bacik disk_bytenr, em->block_start); 463aaedb55bSJosef Bacik goto out; 464aaedb55bSJosef Bacik } 465aaedb55bSJosef Bacik offset = em->start + em->len; 466aaedb55bSJosef Bacik free_extent_map(em); 467aaedb55bSJosef Bacik 468aaedb55bSJosef Bacik /* Prealloc extent */ 469fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 470aaedb55bSJosef Bacik if (IS_ERR(em)) { 4713c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 472aaedb55bSJosef Bacik goto out; 473aaedb55bSJosef Bacik } 474aaedb55bSJosef Bacik if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 4753c7251f2SDavid Sterba test_err("expected a real extent, got %llu", em->block_start); 476aaedb55bSJosef Bacik goto out; 477aaedb55bSJosef Bacik } 478b9ef22deSFeifei Xu if (em->start != offset || em->len != sectorsize) { 4793c7251f2SDavid Sterba test_err( 4803c7251f2SDavid Sterba "unexpected extent wanted start %llu len %u, got start %llu len %llu", 481b9ef22deSFeifei Xu offset, sectorsize, em->start, em->len); 482aaedb55bSJosef Bacik goto out; 483aaedb55bSJosef Bacik } 484aaedb55bSJosef Bacik if (em->flags != prealloc_only) { 4853c7251f2SDavid Sterba test_err("unexpected flags set, want %lu have %lu", 486aaedb55bSJosef Bacik prealloc_only, em->flags); 487aaedb55bSJosef Bacik goto out; 488aaedb55bSJosef Bacik } 489aaedb55bSJosef Bacik if (em->orig_start != em->start) { 4903c7251f2SDavid Sterba test_err("wrong orig offset, want %llu, have %llu", em->start, 491aaedb55bSJosef Bacik em->orig_start); 492aaedb55bSJosef Bacik goto out; 493aaedb55bSJosef Bacik } 494aaedb55bSJosef Bacik offset = em->start + em->len; 495aaedb55bSJosef Bacik free_extent_map(em); 496aaedb55bSJosef Bacik 497aaedb55bSJosef Bacik /* The next 3 are a half written prealloc extent */ 498fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 499aaedb55bSJosef Bacik if (IS_ERR(em)) { 5003c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 501aaedb55bSJosef Bacik goto out; 502aaedb55bSJosef Bacik } 503aaedb55bSJosef Bacik if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 5043c7251f2SDavid Sterba test_err("expected a real extent, got %llu", em->block_start); 505aaedb55bSJosef Bacik goto out; 506aaedb55bSJosef Bacik } 507b9ef22deSFeifei Xu if (em->start != offset || em->len != sectorsize) { 5083c7251f2SDavid Sterba test_err( 5093c7251f2SDavid Sterba "unexpected extent wanted start %llu len %u, got start %llu len %llu", 510b9ef22deSFeifei Xu offset, sectorsize, em->start, em->len); 511aaedb55bSJosef Bacik goto out; 512aaedb55bSJosef Bacik } 513aaedb55bSJosef Bacik if (em->flags != prealloc_only) { 5143c7251f2SDavid Sterba test_err("unexpected flags set, want %lu have %lu", 515aaedb55bSJosef Bacik prealloc_only, em->flags); 516aaedb55bSJosef Bacik goto out; 517aaedb55bSJosef Bacik } 518aaedb55bSJosef Bacik if (em->orig_start != em->start) { 5193c7251f2SDavid Sterba test_err("wrong orig offset, want %llu, have %llu", em->start, 520aaedb55bSJosef Bacik em->orig_start); 521aaedb55bSJosef Bacik goto out; 522aaedb55bSJosef Bacik } 523aaedb55bSJosef Bacik disk_bytenr = em->block_start; 524aaedb55bSJosef Bacik orig_start = em->start; 525aaedb55bSJosef Bacik offset = em->start + em->len; 526aaedb55bSJosef Bacik free_extent_map(em); 527aaedb55bSJosef Bacik 528fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 529aaedb55bSJosef Bacik if (IS_ERR(em)) { 5303c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 531aaedb55bSJosef Bacik goto out; 532aaedb55bSJosef Bacik } 533aaedb55bSJosef Bacik if (em->block_start >= EXTENT_MAP_HOLE) { 5343c7251f2SDavid Sterba test_err("expected a real extent, got %llu", em->block_start); 535aaedb55bSJosef Bacik goto out; 536aaedb55bSJosef Bacik } 537b9ef22deSFeifei Xu if (em->start != offset || em->len != sectorsize) { 5383c7251f2SDavid Sterba test_err( 5393c7251f2SDavid Sterba "unexpected extent wanted start %llu len %u, got start %llu len %llu", 540b9ef22deSFeifei Xu offset, sectorsize, em->start, em->len); 541aaedb55bSJosef Bacik goto out; 542aaedb55bSJosef Bacik } 543aaedb55bSJosef Bacik if (em->flags != 0) { 5443c7251f2SDavid Sterba test_err("unexpected flags set, want 0 have %lu", em->flags); 545aaedb55bSJosef Bacik goto out; 546aaedb55bSJosef Bacik } 547aaedb55bSJosef Bacik if (em->orig_start != orig_start) { 5483c7251f2SDavid Sterba test_err("unexpected orig offset, wanted %llu, have %llu", 549aaedb55bSJosef Bacik orig_start, em->orig_start); 550aaedb55bSJosef Bacik goto out; 551aaedb55bSJosef Bacik } 552aaedb55bSJosef Bacik if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) { 5533c7251f2SDavid Sterba test_err("unexpected block start, wanted %llu, have %llu", 554aaedb55bSJosef Bacik disk_bytenr + (em->start - em->orig_start), 555aaedb55bSJosef Bacik em->block_start); 556aaedb55bSJosef Bacik goto out; 557aaedb55bSJosef Bacik } 558aaedb55bSJosef Bacik offset = em->start + em->len; 559aaedb55bSJosef Bacik free_extent_map(em); 560aaedb55bSJosef Bacik 561fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 562aaedb55bSJosef Bacik if (IS_ERR(em)) { 5633c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 564aaedb55bSJosef Bacik goto out; 565aaedb55bSJosef Bacik } 566aaedb55bSJosef Bacik if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 5673c7251f2SDavid Sterba test_err("expected a real extent, got %llu", em->block_start); 568aaedb55bSJosef Bacik goto out; 569aaedb55bSJosef Bacik } 570b9ef22deSFeifei Xu if (em->start != offset || em->len != 2 * sectorsize) { 5713c7251f2SDavid Sterba test_err( 5723c7251f2SDavid Sterba "unexpected extent wanted start %llu len %u, got start %llu len %llu", 573b9ef22deSFeifei Xu offset, 2 * sectorsize, em->start, em->len); 574aaedb55bSJosef Bacik goto out; 575aaedb55bSJosef Bacik } 576aaedb55bSJosef Bacik if (em->flags != prealloc_only) { 5773c7251f2SDavid Sterba test_err("unexpected flags set, want %lu have %lu", 578aaedb55bSJosef Bacik prealloc_only, em->flags); 579aaedb55bSJosef Bacik goto out; 580aaedb55bSJosef Bacik } 581aaedb55bSJosef Bacik if (em->orig_start != orig_start) { 5823c7251f2SDavid Sterba test_err("wrong orig offset, want %llu, have %llu", orig_start, 583aaedb55bSJosef Bacik em->orig_start); 584aaedb55bSJosef Bacik goto out; 585aaedb55bSJosef Bacik } 586aaedb55bSJosef Bacik if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) { 5873c7251f2SDavid Sterba test_err("unexpected block start, wanted %llu, have %llu", 588aaedb55bSJosef Bacik disk_bytenr + (em->start - em->orig_start), 589aaedb55bSJosef Bacik em->block_start); 590aaedb55bSJosef Bacik goto out; 591aaedb55bSJosef Bacik } 592aaedb55bSJosef Bacik offset = em->start + em->len; 593aaedb55bSJosef Bacik free_extent_map(em); 594aaedb55bSJosef Bacik 595aaedb55bSJosef Bacik /* Now for the compressed extent */ 596fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 597aaedb55bSJosef Bacik if (IS_ERR(em)) { 5983c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 599aaedb55bSJosef Bacik goto out; 600aaedb55bSJosef Bacik } 601aaedb55bSJosef Bacik if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 6023c7251f2SDavid Sterba test_err("expected a real extent, got %llu", em->block_start); 603aaedb55bSJosef Bacik goto out; 604aaedb55bSJosef Bacik } 605b9ef22deSFeifei Xu if (em->start != offset || em->len != 2 * sectorsize) { 6063c7251f2SDavid Sterba test_err( 6073c7251f2SDavid Sterba "unexpected extent wanted start %llu len %u, got start %llu len %llu", 608b9ef22deSFeifei Xu offset, 2 * sectorsize, em->start, em->len); 609aaedb55bSJosef Bacik goto out; 610aaedb55bSJosef Bacik } 611aaedb55bSJosef Bacik if (em->flags != compressed_only) { 6123c7251f2SDavid Sterba test_err("unexpected flags set, want %lu have %lu", 613aaedb55bSJosef Bacik compressed_only, em->flags); 614aaedb55bSJosef Bacik goto out; 615aaedb55bSJosef Bacik } 616aaedb55bSJosef Bacik if (em->orig_start != em->start) { 6173c7251f2SDavid Sterba test_err("wrong orig offset, want %llu, have %llu", 618aaedb55bSJosef Bacik em->start, em->orig_start); 619aaedb55bSJosef Bacik goto out; 620aaedb55bSJosef Bacik } 621aaedb55bSJosef Bacik if (em->compress_type != BTRFS_COMPRESS_ZLIB) { 6223c7251f2SDavid Sterba test_err("unexpected compress type, wanted %d, got %d", 623aaedb55bSJosef Bacik BTRFS_COMPRESS_ZLIB, em->compress_type); 624aaedb55bSJosef Bacik goto out; 625aaedb55bSJosef Bacik } 626aaedb55bSJosef Bacik offset = em->start + em->len; 627aaedb55bSJosef Bacik free_extent_map(em); 628aaedb55bSJosef Bacik 629aaedb55bSJosef Bacik /* Split compressed extent */ 630fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 631aaedb55bSJosef Bacik if (IS_ERR(em)) { 6323c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 633aaedb55bSJosef Bacik goto out; 634aaedb55bSJosef Bacik } 635aaedb55bSJosef Bacik if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 6363c7251f2SDavid Sterba test_err("expected a real extent, got %llu", em->block_start); 637aaedb55bSJosef Bacik goto out; 638aaedb55bSJosef Bacik } 639b9ef22deSFeifei Xu if (em->start != offset || em->len != sectorsize) { 6403c7251f2SDavid Sterba test_err( 6413c7251f2SDavid Sterba "unexpected extent wanted start %llu len %u, got start %llu len %llu", 642b9ef22deSFeifei Xu offset, sectorsize, em->start, em->len); 643aaedb55bSJosef Bacik goto out; 644aaedb55bSJosef Bacik } 645aaedb55bSJosef Bacik if (em->flags != compressed_only) { 6463c7251f2SDavid Sterba test_err("unexpected flags set, want %lu have %lu", 647aaedb55bSJosef Bacik compressed_only, em->flags); 648aaedb55bSJosef Bacik goto out; 649aaedb55bSJosef Bacik } 650aaedb55bSJosef Bacik if (em->orig_start != em->start) { 6513c7251f2SDavid Sterba test_err("wrong orig offset, want %llu, have %llu", 652aaedb55bSJosef Bacik em->start, em->orig_start); 653aaedb55bSJosef Bacik goto out; 654aaedb55bSJosef Bacik } 655aaedb55bSJosef Bacik if (em->compress_type != BTRFS_COMPRESS_ZLIB) { 6563c7251f2SDavid Sterba test_err("unexpected compress type, wanted %d, got %d", 657aaedb55bSJosef Bacik BTRFS_COMPRESS_ZLIB, em->compress_type); 658aaedb55bSJosef Bacik goto out; 659aaedb55bSJosef Bacik } 660aaedb55bSJosef Bacik disk_bytenr = em->block_start; 661aaedb55bSJosef Bacik orig_start = em->start; 662aaedb55bSJosef Bacik offset = em->start + em->len; 663aaedb55bSJosef Bacik free_extent_map(em); 664aaedb55bSJosef Bacik 665fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 666aaedb55bSJosef Bacik if (IS_ERR(em)) { 6673c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 668aaedb55bSJosef Bacik goto out; 669aaedb55bSJosef Bacik } 670aaedb55bSJosef Bacik if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 6713c7251f2SDavid Sterba test_err("expected a real extent, got %llu", em->block_start); 672aaedb55bSJosef Bacik goto out; 673aaedb55bSJosef Bacik } 674b9ef22deSFeifei Xu if (em->start != offset || em->len != sectorsize) { 6753c7251f2SDavid Sterba test_err( 6763c7251f2SDavid Sterba "unexpected extent wanted start %llu len %u, got start %llu len %llu", 677b9ef22deSFeifei Xu offset, sectorsize, em->start, em->len); 678aaedb55bSJosef Bacik goto out; 679aaedb55bSJosef Bacik } 680aaedb55bSJosef Bacik if (em->flags != 0) { 6813c7251f2SDavid Sterba test_err("unexpected flags set, want 0 have %lu", em->flags); 682aaedb55bSJosef Bacik goto out; 683aaedb55bSJosef Bacik } 684aaedb55bSJosef Bacik if (em->orig_start != em->start) { 6853c7251f2SDavid Sterba test_err("wrong orig offset, want %llu, have %llu", em->start, 686aaedb55bSJosef Bacik em->orig_start); 687aaedb55bSJosef Bacik goto out; 688aaedb55bSJosef Bacik } 689aaedb55bSJosef Bacik offset = em->start + em->len; 690aaedb55bSJosef Bacik free_extent_map(em); 691aaedb55bSJosef Bacik 692fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 693aaedb55bSJosef Bacik if (IS_ERR(em)) { 6943c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 695aaedb55bSJosef Bacik goto out; 696aaedb55bSJosef Bacik } 697aaedb55bSJosef Bacik if (em->block_start != disk_bytenr) { 6983c7251f2SDavid Sterba test_err("block start does not match, want %llu got %llu", 699aaedb55bSJosef Bacik disk_bytenr, em->block_start); 700aaedb55bSJosef Bacik goto out; 701aaedb55bSJosef Bacik } 702b9ef22deSFeifei Xu if (em->start != offset || em->len != 2 * sectorsize) { 7033c7251f2SDavid Sterba test_err( 7043c7251f2SDavid Sterba "unexpected extent wanted start %llu len %u, got start %llu len %llu", 705b9ef22deSFeifei Xu offset, 2 * sectorsize, em->start, em->len); 706aaedb55bSJosef Bacik goto out; 707aaedb55bSJosef Bacik } 708aaedb55bSJosef Bacik if (em->flags != compressed_only) { 7093c7251f2SDavid Sterba test_err("unexpected flags set, want %lu have %lu", 710aaedb55bSJosef Bacik compressed_only, em->flags); 711aaedb55bSJosef Bacik goto out; 712aaedb55bSJosef Bacik } 713aaedb55bSJosef Bacik if (em->orig_start != orig_start) { 7143c7251f2SDavid Sterba test_err("wrong orig offset, want %llu, have %llu", 715aaedb55bSJosef Bacik em->start, orig_start); 716aaedb55bSJosef Bacik goto out; 717aaedb55bSJosef Bacik } 718aaedb55bSJosef Bacik if (em->compress_type != BTRFS_COMPRESS_ZLIB) { 7193c7251f2SDavid Sterba test_err("unexpected compress type, wanted %d, got %d", 720aaedb55bSJosef Bacik BTRFS_COMPRESS_ZLIB, em->compress_type); 721aaedb55bSJosef Bacik goto out; 722aaedb55bSJosef Bacik } 723aaedb55bSJosef Bacik offset = em->start + em->len; 724aaedb55bSJosef Bacik free_extent_map(em); 725aaedb55bSJosef Bacik 726aaedb55bSJosef Bacik /* A hole between regular extents but no hole extent */ 727fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset + 6, 728fc4f21b1SNikolay Borisov sectorsize, 0); 729aaedb55bSJosef Bacik if (IS_ERR(em)) { 7303c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 731aaedb55bSJosef Bacik goto out; 732aaedb55bSJosef Bacik } 733aaedb55bSJosef Bacik if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 7343c7251f2SDavid Sterba test_err("expected a real extent, got %llu", em->block_start); 735aaedb55bSJosef Bacik goto out; 736aaedb55bSJosef Bacik } 737b9ef22deSFeifei Xu if (em->start != offset || em->len != sectorsize) { 7383c7251f2SDavid Sterba test_err( 7393c7251f2SDavid Sterba "unexpected extent wanted start %llu len %u, got start %llu len %llu", 740b9ef22deSFeifei Xu offset, sectorsize, em->start, em->len); 741aaedb55bSJosef Bacik goto out; 742aaedb55bSJosef Bacik } 743aaedb55bSJosef Bacik if (em->flags != 0) { 7443c7251f2SDavid Sterba test_err("unexpected flags set, want 0 have %lu", em->flags); 745aaedb55bSJosef Bacik goto out; 746aaedb55bSJosef Bacik } 747aaedb55bSJosef Bacik if (em->orig_start != em->start) { 7483c7251f2SDavid Sterba test_err("wrong orig offset, want %llu, have %llu", em->start, 749aaedb55bSJosef Bacik em->orig_start); 750aaedb55bSJosef Bacik goto out; 751aaedb55bSJosef Bacik } 752aaedb55bSJosef Bacik offset = em->start + em->len; 753aaedb55bSJosef Bacik free_extent_map(em); 754aaedb55bSJosef Bacik 755d4417e22SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, SZ_4M, 0); 756aaedb55bSJosef Bacik if (IS_ERR(em)) { 7573c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 758aaedb55bSJosef Bacik goto out; 759aaedb55bSJosef Bacik } 760aaedb55bSJosef Bacik if (em->block_start != EXTENT_MAP_HOLE) { 7613c7251f2SDavid Sterba test_err("expected a hole extent, got %llu", em->block_start); 762aaedb55bSJosef Bacik goto out; 763aaedb55bSJosef Bacik } 764aaedb55bSJosef Bacik /* 765aaedb55bSJosef Bacik * Currently we just return a length that we requested rather than the 766aaedb55bSJosef Bacik * length of the actual hole, if this changes we'll have to change this 767aaedb55bSJosef Bacik * test. 768aaedb55bSJosef Bacik */ 769b9ef22deSFeifei Xu if (em->start != offset || em->len != 3 * sectorsize) { 7703c7251f2SDavid Sterba test_err( 7713c7251f2SDavid Sterba "unexpected extent wanted start %llu len %u, got start %llu len %llu", 772b9ef22deSFeifei Xu offset, 3 * sectorsize, em->start, em->len); 773aaedb55bSJosef Bacik goto out; 774aaedb55bSJosef Bacik } 775aaedb55bSJosef Bacik if (em->flags != vacancy_only) { 7763c7251f2SDavid Sterba test_err("unexpected flags set, want %lu have %lu", 777aaedb55bSJosef Bacik vacancy_only, em->flags); 778aaedb55bSJosef Bacik goto out; 779aaedb55bSJosef Bacik } 780aaedb55bSJosef Bacik if (em->orig_start != em->start) { 7813c7251f2SDavid Sterba test_err("wrong orig offset, want %llu, have %llu", em->start, 782aaedb55bSJosef Bacik em->orig_start); 783aaedb55bSJosef Bacik goto out; 784aaedb55bSJosef Bacik } 785aaedb55bSJosef Bacik offset = em->start + em->len; 786aaedb55bSJosef Bacik free_extent_map(em); 787aaedb55bSJosef Bacik 788fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 789aaedb55bSJosef Bacik if (IS_ERR(em)) { 7903c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 791aaedb55bSJosef Bacik goto out; 792aaedb55bSJosef Bacik } 793aaedb55bSJosef Bacik if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 7943c7251f2SDavid Sterba test_err("expected a real extent, got %llu", em->block_start); 795aaedb55bSJosef Bacik goto out; 796aaedb55bSJosef Bacik } 797b9ef22deSFeifei Xu if (em->start != offset || em->len != sectorsize) { 7983c7251f2SDavid Sterba test_err( 7993c7251f2SDavid Sterba "unexpected extent wanted start %llu len %u, got start %llu len %llu", 800b9ef22deSFeifei Xu offset, sectorsize, em->start, em->len); 801aaedb55bSJosef Bacik goto out; 802aaedb55bSJosef Bacik } 803aaedb55bSJosef Bacik if (em->flags != 0) { 8043c7251f2SDavid Sterba test_err("unexpected flags set, want 0 have %lu", em->flags); 805aaedb55bSJosef Bacik goto out; 806aaedb55bSJosef Bacik } 807aaedb55bSJosef Bacik if (em->orig_start != em->start) { 8083c7251f2SDavid Sterba test_err("wrong orig offset, want %llu, have %llu", em->start, 809aaedb55bSJosef Bacik em->orig_start); 810aaedb55bSJosef Bacik goto out; 811aaedb55bSJosef Bacik } 812aaedb55bSJosef Bacik ret = 0; 813aaedb55bSJosef Bacik out: 814aaedb55bSJosef Bacik if (!IS_ERR(em)) 815aaedb55bSJosef Bacik free_extent_map(em); 816aaedb55bSJosef Bacik iput(inode); 817faa2dbf0SJosef Bacik btrfs_free_dummy_root(root); 8187c0260eeSJeff Mahoney btrfs_free_dummy_fs_info(fs_info); 819aaedb55bSJosef Bacik return ret; 820aaedb55bSJosef Bacik } 821aaedb55bSJosef Bacik 822b9ef22deSFeifei Xu static int test_hole_first(u32 sectorsize, u32 nodesize) 8230e30db86SJosef Bacik { 8247c0260eeSJeff Mahoney struct btrfs_fs_info *fs_info = NULL; 8250e30db86SJosef Bacik struct inode *inode = NULL; 8260e30db86SJosef Bacik struct btrfs_root *root = NULL; 8270e30db86SJosef Bacik struct extent_map *em = NULL; 8280e30db86SJosef Bacik int ret = -ENOMEM; 8290e30db86SJosef Bacik 8300e30db86SJosef Bacik inode = btrfs_new_test_inode(); 8310e30db86SJosef Bacik if (!inode) { 8323c7251f2SDavid Sterba test_err("couldn't allocate inode"); 8330e30db86SJosef Bacik return ret; 8340e30db86SJosef Bacik } 8350e30db86SJosef Bacik 8360e30db86SJosef Bacik BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; 8370e30db86SJosef Bacik BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID; 8380e30db86SJosef Bacik BTRFS_I(inode)->location.offset = 0; 8390e30db86SJosef Bacik 840da17066cSJeff Mahoney fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize); 8417c0260eeSJeff Mahoney if (!fs_info) { 84237b2a7bcSDavid Sterba test_std_err(TEST_ALLOC_FS_INFO); 8430e30db86SJosef Bacik goto out; 8440e30db86SJosef Bacik } 8450e30db86SJosef Bacik 846da17066cSJeff Mahoney root = btrfs_alloc_dummy_root(fs_info); 8477c0260eeSJeff Mahoney if (IS_ERR(root)) { 84852ab7bcaSDavid Sterba test_std_err(TEST_ALLOC_ROOT); 8490e30db86SJosef Bacik goto out; 8500e30db86SJosef Bacik } 8510e30db86SJosef Bacik 852da17066cSJeff Mahoney root->node = alloc_dummy_extent_buffer(fs_info, nodesize); 8530e30db86SJosef Bacik if (!root->node) { 8549e3d9f84SDavid Sterba test_std_err(TEST_ALLOC_ROOT); 8550e30db86SJosef Bacik goto out; 8560e30db86SJosef Bacik } 8570e30db86SJosef Bacik 8580e30db86SJosef Bacik btrfs_set_header_nritems(root->node, 0); 8590e30db86SJosef Bacik btrfs_set_header_level(root->node, 0); 8600e30db86SJosef Bacik BTRFS_I(inode)->root = root; 8610e30db86SJosef Bacik ret = -EINVAL; 8620e30db86SJosef Bacik 8630e30db86SJosef Bacik /* 8640e30db86SJosef Bacik * Need a blank inode item here just so we don't confuse 8650e30db86SJosef Bacik * btrfs_get_extent. 8660e30db86SJosef Bacik */ 8670e30db86SJosef Bacik insert_inode_item_key(root); 868b9ef22deSFeifei Xu insert_extent(root, sectorsize, sectorsize, sectorsize, 0, sectorsize, 869b9ef22deSFeifei Xu sectorsize, BTRFS_FILE_EXTENT_REG, 0, 1); 870fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, 2 * sectorsize, 0); 8710e30db86SJosef Bacik if (IS_ERR(em)) { 8723c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 8730e30db86SJosef Bacik goto out; 8740e30db86SJosef Bacik } 8750e30db86SJosef Bacik if (em->block_start != EXTENT_MAP_HOLE) { 8763c7251f2SDavid Sterba test_err("expected a hole, got %llu", em->block_start); 8770e30db86SJosef Bacik goto out; 8780e30db86SJosef Bacik } 879b9ef22deSFeifei Xu if (em->start != 0 || em->len != sectorsize) { 8803c7251f2SDavid Sterba test_err( 8813c7251f2SDavid Sterba "unexpected extent wanted start 0 len %u, got start %llu len %llu", 882b9ef22deSFeifei Xu sectorsize, em->start, em->len); 8830e30db86SJosef Bacik goto out; 8840e30db86SJosef Bacik } 8850e30db86SJosef Bacik if (em->flags != vacancy_only) { 8863c7251f2SDavid Sterba test_err("wrong flags, wanted %lu, have %lu", vacancy_only, 8870e30db86SJosef Bacik em->flags); 8880e30db86SJosef Bacik goto out; 8890e30db86SJosef Bacik } 8900e30db86SJosef Bacik free_extent_map(em); 8910e30db86SJosef Bacik 892fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, sectorsize, 893fc4f21b1SNikolay Borisov 2 * sectorsize, 0); 8940e30db86SJosef Bacik if (IS_ERR(em)) { 8953c7251f2SDavid Sterba test_err("got an error when we shouldn't have"); 8960e30db86SJosef Bacik goto out; 8970e30db86SJosef Bacik } 898b9ef22deSFeifei Xu if (em->block_start != sectorsize) { 8993c7251f2SDavid Sterba test_err("expected a real extent, got %llu", em->block_start); 9000e30db86SJosef Bacik goto out; 9010e30db86SJosef Bacik } 902b9ef22deSFeifei Xu if (em->start != sectorsize || em->len != sectorsize) { 9033c7251f2SDavid Sterba test_err( 9043c7251f2SDavid Sterba "unexpected extent wanted start %u len %u, got start %llu len %llu", 905b9ef22deSFeifei Xu sectorsize, sectorsize, em->start, em->len); 9060e30db86SJosef Bacik goto out; 9070e30db86SJosef Bacik } 9080e30db86SJosef Bacik if (em->flags != 0) { 9093c7251f2SDavid Sterba test_err("unexpected flags set, wanted 0 got %lu", 9100e30db86SJosef Bacik em->flags); 9110e30db86SJosef Bacik goto out; 9120e30db86SJosef Bacik } 9130e30db86SJosef Bacik ret = 0; 9140e30db86SJosef Bacik out: 9150e30db86SJosef Bacik if (!IS_ERR(em)) 9160e30db86SJosef Bacik free_extent_map(em); 9170e30db86SJosef Bacik iput(inode); 918faa2dbf0SJosef Bacik btrfs_free_dummy_root(root); 9197c0260eeSJeff Mahoney btrfs_free_dummy_fs_info(fs_info); 9200e30db86SJosef Bacik return ret; 9210e30db86SJosef Bacik } 9220e30db86SJosef Bacik 923b9ef22deSFeifei Xu static int test_extent_accounting(u32 sectorsize, u32 nodesize) 9246a3891c5SJosef Bacik { 9257c0260eeSJeff Mahoney struct btrfs_fs_info *fs_info = NULL; 9266a3891c5SJosef Bacik struct inode *inode = NULL; 9276a3891c5SJosef Bacik struct btrfs_root *root = NULL; 9286a3891c5SJosef Bacik int ret = -ENOMEM; 9296a3891c5SJosef Bacik 9306a3891c5SJosef Bacik inode = btrfs_new_test_inode(); 9316a3891c5SJosef Bacik if (!inode) { 9323c7251f2SDavid Sterba test_err("couldn't allocate inode"); 9336a3891c5SJosef Bacik return ret; 9346a3891c5SJosef Bacik } 9356a3891c5SJosef Bacik 936da17066cSJeff Mahoney fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize); 9377c0260eeSJeff Mahoney if (!fs_info) { 93837b2a7bcSDavid Sterba test_std_err(TEST_ALLOC_FS_INFO); 9396a3891c5SJosef Bacik goto out; 9406a3891c5SJosef Bacik } 9416a3891c5SJosef Bacik 942da17066cSJeff Mahoney root = btrfs_alloc_dummy_root(fs_info); 9437c0260eeSJeff Mahoney if (IS_ERR(root)) { 94452ab7bcaSDavid Sterba test_std_err(TEST_ALLOC_ROOT); 9456a3891c5SJosef Bacik goto out; 9466a3891c5SJosef Bacik } 9476a3891c5SJosef Bacik 9486a3891c5SJosef Bacik BTRFS_I(inode)->root = root; 9496a3891c5SJosef Bacik btrfs_test_inode_set_ops(inode); 9506a3891c5SJosef Bacik 9516a3891c5SJosef Bacik /* [BTRFS_MAX_EXTENT_SIZE] */ 952e3b8a485SFilipe Manana ret = btrfs_set_extent_delalloc(inode, 0, BTRFS_MAX_EXTENT_SIZE - 1, 0, 953ba8b04c1SQu Wenruo NULL, 0); 9546a3891c5SJosef Bacik if (ret) { 9553c7251f2SDavid Sterba test_err("btrfs_set_extent_delalloc returned %d", ret); 9566a3891c5SJosef Bacik goto out; 9576a3891c5SJosef Bacik } 9586a3891c5SJosef Bacik if (BTRFS_I(inode)->outstanding_extents != 1) { 9596a3891c5SJosef Bacik ret = -EINVAL; 9603c7251f2SDavid Sterba test_err("miscount, wanted 1, got %u", 9616a3891c5SJosef Bacik BTRFS_I(inode)->outstanding_extents); 9626a3891c5SJosef Bacik goto out; 9636a3891c5SJosef Bacik } 9646a3891c5SJosef Bacik 965b9ef22deSFeifei Xu /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */ 9666a3891c5SJosef Bacik ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE, 967b9ef22deSFeifei Xu BTRFS_MAX_EXTENT_SIZE + sectorsize - 1, 968e3b8a485SFilipe Manana 0, NULL, 0); 9696a3891c5SJosef Bacik if (ret) { 9703c7251f2SDavid Sterba test_err("btrfs_set_extent_delalloc returned %d", ret); 9716a3891c5SJosef Bacik goto out; 9726a3891c5SJosef Bacik } 9736a3891c5SJosef Bacik if (BTRFS_I(inode)->outstanding_extents != 2) { 9746a3891c5SJosef Bacik ret = -EINVAL; 9753c7251f2SDavid Sterba test_err("miscount, wanted 2, got %u", 9766a3891c5SJosef Bacik BTRFS_I(inode)->outstanding_extents); 9776a3891c5SJosef Bacik goto out; 9786a3891c5SJosef Bacik } 9796a3891c5SJosef Bacik 980b9ef22deSFeifei Xu /* [BTRFS_MAX_EXTENT_SIZE/2][sectorsize HOLE][the rest] */ 9816a3891c5SJosef Bacik ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 9826a3891c5SJosef Bacik BTRFS_MAX_EXTENT_SIZE >> 1, 983b9ef22deSFeifei Xu (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1, 9846a3891c5SJosef Bacik EXTENT_DELALLOC | EXTENT_DIRTY | 985ae0f1625SDavid Sterba EXTENT_UPTODATE, 0, 0, NULL); 9866a3891c5SJosef Bacik if (ret) { 9873c7251f2SDavid Sterba test_err("clear_extent_bit returned %d", ret); 9886a3891c5SJosef Bacik goto out; 9896a3891c5SJosef Bacik } 9906a3891c5SJosef Bacik if (BTRFS_I(inode)->outstanding_extents != 2) { 9916a3891c5SJosef Bacik ret = -EINVAL; 9923c7251f2SDavid Sterba test_err("miscount, wanted 2, got %u", 9936a3891c5SJosef Bacik BTRFS_I(inode)->outstanding_extents); 9946a3891c5SJosef Bacik goto out; 9956a3891c5SJosef Bacik } 9966a3891c5SJosef Bacik 997b9ef22deSFeifei Xu /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */ 9986a3891c5SJosef Bacik ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE >> 1, 999b9ef22deSFeifei Xu (BTRFS_MAX_EXTENT_SIZE >> 1) 1000b9ef22deSFeifei Xu + sectorsize - 1, 1001e3b8a485SFilipe Manana 0, NULL, 0); 10026a3891c5SJosef Bacik if (ret) { 10033c7251f2SDavid Sterba test_err("btrfs_set_extent_delalloc returned %d", ret); 10046a3891c5SJosef Bacik goto out; 10056a3891c5SJosef Bacik } 10066a3891c5SJosef Bacik if (BTRFS_I(inode)->outstanding_extents != 2) { 10076a3891c5SJosef Bacik ret = -EINVAL; 10083c7251f2SDavid Sterba test_err("miscount, wanted 2, got %u", 10096a3891c5SJosef Bacik BTRFS_I(inode)->outstanding_extents); 10106a3891c5SJosef Bacik goto out; 10116a3891c5SJosef Bacik } 10126a3891c5SJosef Bacik 10136a3891c5SJosef Bacik /* 1014b9ef22deSFeifei Xu * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize] 10156a3891c5SJosef Bacik */ 1016b9ef22deSFeifei Xu ret = btrfs_set_extent_delalloc(inode, 1017b9ef22deSFeifei Xu BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize, 1018b9ef22deSFeifei Xu (BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1, 1019e3b8a485SFilipe Manana 0, NULL, 0); 10206a3891c5SJosef Bacik if (ret) { 10213c7251f2SDavid Sterba test_err("btrfs_set_extent_delalloc returned %d", ret); 10226a3891c5SJosef Bacik goto out; 10236a3891c5SJosef Bacik } 10246a3891c5SJosef Bacik if (BTRFS_I(inode)->outstanding_extents != 4) { 10256a3891c5SJosef Bacik ret = -EINVAL; 10263c7251f2SDavid Sterba test_err("miscount, wanted 4, got %u", 10276a3891c5SJosef Bacik BTRFS_I(inode)->outstanding_extents); 10286a3891c5SJosef Bacik goto out; 10296a3891c5SJosef Bacik } 10306a3891c5SJosef Bacik 1031b9ef22deSFeifei Xu /* 1032b9ef22deSFeifei Xu * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize] 1033b9ef22deSFeifei Xu */ 1034b9ef22deSFeifei Xu ret = btrfs_set_extent_delalloc(inode, 1035b9ef22deSFeifei Xu BTRFS_MAX_EXTENT_SIZE + sectorsize, 1036e3b8a485SFilipe Manana BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL, 0); 10376a3891c5SJosef Bacik if (ret) { 10383c7251f2SDavid Sterba test_err("btrfs_set_extent_delalloc returned %d", ret); 10396a3891c5SJosef Bacik goto out; 10406a3891c5SJosef Bacik } 10416a3891c5SJosef Bacik if (BTRFS_I(inode)->outstanding_extents != 3) { 10426a3891c5SJosef Bacik ret = -EINVAL; 10433c7251f2SDavid Sterba test_err("miscount, wanted 3, got %u", 10446a3891c5SJosef Bacik BTRFS_I(inode)->outstanding_extents); 10456a3891c5SJosef Bacik goto out; 10466a3891c5SJosef Bacik } 10476a3891c5SJosef Bacik 10486a3891c5SJosef Bacik /* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */ 10496a3891c5SJosef Bacik ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 1050b9ef22deSFeifei Xu BTRFS_MAX_EXTENT_SIZE + sectorsize, 1051b9ef22deSFeifei Xu BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 10526a3891c5SJosef Bacik EXTENT_DIRTY | EXTENT_DELALLOC | 1053ae0f1625SDavid Sterba EXTENT_UPTODATE, 0, 0, NULL); 10546a3891c5SJosef Bacik if (ret) { 10553c7251f2SDavid Sterba test_err("clear_extent_bit returned %d", ret); 10566a3891c5SJosef Bacik goto out; 10576a3891c5SJosef Bacik } 10586a3891c5SJosef Bacik if (BTRFS_I(inode)->outstanding_extents != 4) { 10596a3891c5SJosef Bacik ret = -EINVAL; 10603c7251f2SDavid Sterba test_err("miscount, wanted 4, got %u", 10616a3891c5SJosef Bacik BTRFS_I(inode)->outstanding_extents); 10626a3891c5SJosef Bacik goto out; 10636a3891c5SJosef Bacik } 10646a3891c5SJosef Bacik 10656a3891c5SJosef Bacik /* 10666a3891c5SJosef Bacik * Refill the hole again just for good measure, because I thought it 10676a3891c5SJosef Bacik * might fail and I'd rather satisfy my paranoia at this point. 10686a3891c5SJosef Bacik */ 1069b9ef22deSFeifei Xu ret = btrfs_set_extent_delalloc(inode, 1070b9ef22deSFeifei Xu BTRFS_MAX_EXTENT_SIZE + sectorsize, 1071e3b8a485SFilipe Manana BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL, 0); 10726a3891c5SJosef Bacik if (ret) { 10733c7251f2SDavid Sterba test_err("btrfs_set_extent_delalloc returned %d", ret); 10746a3891c5SJosef Bacik goto out; 10756a3891c5SJosef Bacik } 10766a3891c5SJosef Bacik if (BTRFS_I(inode)->outstanding_extents != 3) { 10776a3891c5SJosef Bacik ret = -EINVAL; 10783c7251f2SDavid Sterba test_err("miscount, wanted 3, got %u", 10796a3891c5SJosef Bacik BTRFS_I(inode)->outstanding_extents); 10806a3891c5SJosef Bacik goto out; 10816a3891c5SJosef Bacik } 10826a3891c5SJosef Bacik 10836a3891c5SJosef Bacik /* Empty */ 10846a3891c5SJosef Bacik ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1, 10856a3891c5SJosef Bacik EXTENT_DIRTY | EXTENT_DELALLOC | 1086ae0f1625SDavid Sterba EXTENT_UPTODATE, 0, 0, NULL); 10876a3891c5SJosef Bacik if (ret) { 10883c7251f2SDavid Sterba test_err("clear_extent_bit returned %d", ret); 10896a3891c5SJosef Bacik goto out; 10906a3891c5SJosef Bacik } 10916a3891c5SJosef Bacik if (BTRFS_I(inode)->outstanding_extents) { 10926a3891c5SJosef Bacik ret = -EINVAL; 10933c7251f2SDavid Sterba test_err("miscount, wanted 0, got %u", 10946a3891c5SJosef Bacik BTRFS_I(inode)->outstanding_extents); 10956a3891c5SJosef Bacik goto out; 10966a3891c5SJosef Bacik } 10976a3891c5SJosef Bacik ret = 0; 10986a3891c5SJosef Bacik out: 10996a3891c5SJosef Bacik if (ret) 11006a3891c5SJosef Bacik clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1, 11016a3891c5SJosef Bacik EXTENT_DIRTY | EXTENT_DELALLOC | 1102ae0f1625SDavid Sterba EXTENT_UPTODATE, 0, 0, NULL); 11036a3891c5SJosef Bacik iput(inode); 11046a3891c5SJosef Bacik btrfs_free_dummy_root(root); 11057c0260eeSJeff Mahoney btrfs_free_dummy_fs_info(fs_info); 11066a3891c5SJosef Bacik return ret; 11076a3891c5SJosef Bacik } 11086a3891c5SJosef Bacik 1109b9ef22deSFeifei Xu int btrfs_test_inodes(u32 sectorsize, u32 nodesize) 1110aaedb55bSJosef Bacik { 11110e30db86SJosef Bacik int ret; 11120e30db86SJosef Bacik 11130e30db86SJosef Bacik set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only); 11140e30db86SJosef Bacik set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only); 11150e30db86SJosef Bacik 1116315b76b4SDavid Sterba test_msg("running btrfs_get_extent tests"); 1117b9ef22deSFeifei Xu ret = test_btrfs_get_extent(sectorsize, nodesize); 11180e30db86SJosef Bacik if (ret) 11190e30db86SJosef Bacik return ret; 1120315b76b4SDavid Sterba test_msg("running hole first btrfs_get_extent test"); 1121b9ef22deSFeifei Xu ret = test_hole_first(sectorsize, nodesize); 11226a3891c5SJosef Bacik if (ret) 11236a3891c5SJosef Bacik return ret; 1124315b76b4SDavid Sterba test_msg("running outstanding_extents tests"); 1125b9ef22deSFeifei Xu return test_extent_accounting(sectorsize, nodesize); 1126aaedb55bSJosef Bacik } 1127