Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Fusion IO. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 19 | #include <linux/types.h> |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 20 | #include "btrfs-tests.h" |
| 21 | #include "../ctree.h" |
| 22 | #include "../btrfs_inode.h" |
| 23 | #include "../disk-io.h" |
| 24 | #include "../extent_io.h" |
| 25 | #include "../volumes.h" |
Anand Jain | ebb8765 | 2016-03-10 17:26:59 +0800 | [diff] [blame] | 26 | #include "../compression.h" |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 27 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 28 | static void insert_extent(struct btrfs_root *root, u64 start, u64 len, |
| 29 | u64 ram_bytes, u64 offset, u64 disk_bytenr, |
| 30 | u64 disk_len, u32 type, u8 compression, int slot) |
| 31 | { |
| 32 | struct btrfs_path path; |
| 33 | struct btrfs_file_extent_item *fi; |
| 34 | struct extent_buffer *leaf = root->node; |
| 35 | struct btrfs_key key; |
| 36 | u32 value_len = sizeof(struct btrfs_file_extent_item); |
| 37 | |
| 38 | if (type == BTRFS_FILE_EXTENT_INLINE) |
| 39 | value_len += len; |
| 40 | memset(&path, 0, sizeof(path)); |
| 41 | |
| 42 | path.nodes[0] = leaf; |
| 43 | path.slots[0] = slot; |
| 44 | |
| 45 | key.objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 46 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 47 | key.offset = start; |
| 48 | |
| 49 | setup_items_for_insert(root, &path, &key, &value_len, value_len, |
| 50 | value_len + sizeof(struct btrfs_item), 1); |
| 51 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 52 | btrfs_set_file_extent_generation(leaf, fi, 1); |
| 53 | btrfs_set_file_extent_type(leaf, fi, type); |
| 54 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 55 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_len); |
| 56 | btrfs_set_file_extent_offset(leaf, fi, offset); |
| 57 | btrfs_set_file_extent_num_bytes(leaf, fi, len); |
| 58 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 59 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 60 | btrfs_set_file_extent_encryption(leaf, fi, 0); |
| 61 | btrfs_set_file_extent_other_encoding(leaf, fi, 0); |
| 62 | } |
| 63 | |
Josef Bacik | 0e30db8 | 2013-10-14 13:15:02 -0400 | [diff] [blame] | 64 | static void insert_inode_item_key(struct btrfs_root *root) |
| 65 | { |
| 66 | struct btrfs_path path; |
| 67 | struct extent_buffer *leaf = root->node; |
| 68 | struct btrfs_key key; |
| 69 | u32 value_len = 0; |
| 70 | |
| 71 | memset(&path, 0, sizeof(path)); |
| 72 | |
| 73 | path.nodes[0] = leaf; |
| 74 | path.slots[0] = 0; |
| 75 | |
| 76 | key.objectid = BTRFS_INODE_ITEM_KEY; |
| 77 | key.type = BTRFS_INODE_ITEM_KEY; |
| 78 | key.offset = 0; |
| 79 | |
| 80 | setup_items_for_insert(root, &path, &key, &value_len, value_len, |
| 81 | value_len + sizeof(struct btrfs_item), 1); |
| 82 | } |
| 83 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 84 | /* |
| 85 | * Build the most complicated map of extents the earth has ever seen. We want |
| 86 | * this so we can test all of the corner cases of btrfs_get_extent. Here is a |
| 87 | * diagram of how the extents will look though this may not be possible we still |
| 88 | * want to make sure everything acts normally (the last number is not inclusive) |
| 89 | * |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 90 | * [0 - 5][5 - 6][ 6 - 4096 ][ 4096 - 4100][4100 - 8195][8195 - 12291] |
| 91 | * [hole ][inline][hole but no extent][ hole ][ regular ][regular1 split] |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 92 | * |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 93 | * [12291 - 16387][16387 - 24579][24579 - 28675][ 28675 - 32771][32771 - 36867 ] |
| 94 | * [ hole ][regular1 split][ prealloc ][ prealloc1 ][prealloc1 written] |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 95 | * |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 96 | * [36867 - 45059][45059 - 53251][53251 - 57347][57347 - 61443][61443- 69635] |
| 97 | * [ prealloc1 ][ compressed ][ compressed1 ][ regular ][ compressed1] |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 98 | * |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 99 | * [69635-73731][ 73731 - 86019 ][86019-90115] |
| 100 | * [ regular ][ hole but no extent][ regular ] |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 101 | */ |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 102 | static void setup_file_extents(struct btrfs_root *root, u32 sectorsize) |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 103 | { |
| 104 | int slot = 0; |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 105 | u64 disk_bytenr = SZ_1M; |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 106 | u64 offset = 0; |
| 107 | |
| 108 | /* First we want a hole */ |
| 109 | insert_extent(root, offset, 5, 5, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0, |
| 110 | slot); |
| 111 | slot++; |
| 112 | offset += 5; |
| 113 | |
| 114 | /* |
| 115 | * Now we want an inline extent, I don't think this is possible but hey |
| 116 | * why not? Also keep in mind if we have an inline extent it counts as |
| 117 | * the whole first page. If we were to expand it we would have to cow |
| 118 | * and we wouldn't have an inline extent anymore. |
| 119 | */ |
| 120 | insert_extent(root, offset, 1, 1, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE, 0, |
| 121 | slot); |
| 122 | slot++; |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 123 | offset = sectorsize; |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 124 | |
| 125 | /* Now another hole */ |
| 126 | insert_extent(root, offset, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0, |
| 127 | slot); |
| 128 | slot++; |
| 129 | offset += 4; |
| 130 | |
| 131 | /* Now for a regular extent */ |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 132 | insert_extent(root, offset, sectorsize - 1, sectorsize - 1, 0, |
| 133 | disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 134 | slot++; |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 135 | disk_bytenr += sectorsize; |
| 136 | offset += sectorsize - 1; |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 137 | |
| 138 | /* |
| 139 | * Now for 3 extents that were split from a hole punch so we test |
| 140 | * offsets properly. |
| 141 | */ |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 142 | insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr, |
| 143 | 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot); |
| 144 | slot++; |
| 145 | offset += sectorsize; |
| 146 | insert_extent(root, offset, sectorsize, sectorsize, 0, 0, 0, |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 147 | BTRFS_FILE_EXTENT_REG, 0, slot); |
| 148 | slot++; |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 149 | offset += sectorsize; |
| 150 | insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize, |
| 151 | 2 * sectorsize, disk_bytenr, 4 * sectorsize, |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 152 | BTRFS_FILE_EXTENT_REG, 0, slot); |
| 153 | slot++; |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 154 | offset += 2 * sectorsize; |
| 155 | disk_bytenr += 4 * sectorsize; |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 156 | |
| 157 | /* Now for a unwritten prealloc extent */ |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 158 | insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr, |
| 159 | sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 160 | slot++; |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 161 | offset += sectorsize; |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | * We want to jack up disk_bytenr a little more so the em stuff doesn't |
| 165 | * merge our records. |
| 166 | */ |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 167 | disk_bytenr += 2 * sectorsize; |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 168 | |
| 169 | /* |
| 170 | * Now for a partially written prealloc extent, basically the same as |
| 171 | * the hole punch example above. Ram_bytes never changes when you mark |
| 172 | * extents written btw. |
| 173 | */ |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 174 | insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr, |
| 175 | 4 * sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot); |
| 176 | slot++; |
| 177 | offset += sectorsize; |
| 178 | insert_extent(root, offset, sectorsize, 4 * sectorsize, sectorsize, |
| 179 | disk_bytenr, 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0, |
| 180 | slot); |
| 181 | slot++; |
| 182 | offset += sectorsize; |
| 183 | insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize, |
| 184 | 2 * sectorsize, disk_bytenr, 4 * sectorsize, |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 185 | BTRFS_FILE_EXTENT_PREALLOC, 0, slot); |
| 186 | slot++; |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 187 | offset += 2 * sectorsize; |
| 188 | disk_bytenr += 4 * sectorsize; |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 189 | |
| 190 | /* Now a normal compressed extent */ |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 191 | insert_extent(root, offset, 2 * sectorsize, 2 * sectorsize, 0, |
| 192 | disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG, |
| 193 | BTRFS_COMPRESS_ZLIB, slot); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 194 | slot++; |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 195 | offset += 2 * sectorsize; |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 196 | /* No merges */ |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 197 | disk_bytenr += 2 * sectorsize; |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 198 | |
| 199 | /* Now a split compressed extent */ |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 200 | insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr, |
| 201 | sectorsize, BTRFS_FILE_EXTENT_REG, |
| 202 | BTRFS_COMPRESS_ZLIB, slot); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 203 | slot++; |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 204 | offset += sectorsize; |
| 205 | insert_extent(root, offset, sectorsize, sectorsize, 0, |
| 206 | disk_bytenr + sectorsize, sectorsize, |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 207 | BTRFS_FILE_EXTENT_REG, 0, slot); |
| 208 | slot++; |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 209 | offset += sectorsize; |
| 210 | insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize, |
| 211 | 2 * sectorsize, disk_bytenr, sectorsize, |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 212 | BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot); |
| 213 | slot++; |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 214 | offset += 2 * sectorsize; |
| 215 | disk_bytenr += 2 * sectorsize; |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 216 | |
| 217 | /* Now extents that have a hole but no hole extent */ |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 218 | insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr, |
| 219 | sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 220 | slot++; |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 221 | offset += 4 * sectorsize; |
| 222 | disk_bytenr += sectorsize; |
| 223 | insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr, |
| 224 | sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static unsigned long prealloc_only = 0; |
| 228 | static unsigned long compressed_only = 0; |
| 229 | static unsigned long vacancy_only = 0; |
| 230 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 231 | static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 232 | { |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 233 | struct btrfs_fs_info *fs_info = NULL; |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 234 | struct inode *inode = NULL; |
| 235 | struct btrfs_root *root = NULL; |
| 236 | struct extent_map *em = NULL; |
| 237 | u64 orig_start; |
| 238 | u64 disk_bytenr; |
| 239 | u64 offset; |
| 240 | int ret = -ENOMEM; |
| 241 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 242 | inode = btrfs_new_test_inode(); |
| 243 | if (!inode) { |
| 244 | test_msg("Couldn't allocate inode\n"); |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 249 | BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 250 | BTRFS_I(inode)->location.offset = 0; |
| 251 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 252 | fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 253 | if (!fs_info) { |
| 254 | test_msg("Couldn't allocate dummy fs info\n"); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 255 | goto out; |
| 256 | } |
| 257 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 258 | root = btrfs_alloc_dummy_root(fs_info); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 259 | if (IS_ERR(root)) { |
| 260 | test_msg("Couldn't allocate root\n"); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 261 | goto out; |
| 262 | } |
| 263 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 264 | root->node = alloc_dummy_extent_buffer(fs_info, nodesize); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 265 | if (!root->node) { |
| 266 | test_msg("Couldn't allocate dummy buffer\n"); |
| 267 | goto out; |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * We will just free a dummy node if it's ref count is 2 so we need an |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 272 | * extra ref so our searches don't accidentally release our page. |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 273 | */ |
| 274 | extent_buffer_get(root->node); |
| 275 | btrfs_set_header_nritems(root->node, 0); |
| 276 | btrfs_set_header_level(root->node, 0); |
| 277 | ret = -EINVAL; |
| 278 | |
| 279 | /* First with no extents */ |
| 280 | BTRFS_I(inode)->root = root; |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 281 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, sectorsize, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 282 | if (IS_ERR(em)) { |
| 283 | em = NULL; |
| 284 | test_msg("Got an error when we shouldn't have\n"); |
| 285 | goto out; |
| 286 | } |
| 287 | if (em->block_start != EXTENT_MAP_HOLE) { |
| 288 | test_msg("Expected a hole, got %llu\n", em->block_start); |
| 289 | goto out; |
| 290 | } |
| 291 | if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags)) { |
| 292 | test_msg("Vacancy flag wasn't set properly\n"); |
| 293 | goto out; |
| 294 | } |
| 295 | free_extent_map(em); |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 296 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 297 | |
| 298 | /* |
| 299 | * All of the magic numbers are based on the mapping setup in |
| 300 | * setup_file_extents, so if you change anything there you need to |
| 301 | * update the comment and update the expected values below. |
| 302 | */ |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 303 | setup_file_extents(root, sectorsize); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 304 | |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 305 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, (u64)-1, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 306 | if (IS_ERR(em)) { |
| 307 | test_msg("Got an error when we shouldn't have\n"); |
| 308 | goto out; |
| 309 | } |
| 310 | if (em->block_start != EXTENT_MAP_HOLE) { |
| 311 | test_msg("Expected a hole, got %llu\n", em->block_start); |
| 312 | goto out; |
| 313 | } |
| 314 | if (em->start != 0 || em->len != 5) { |
| 315 | test_msg("Unexpected extent wanted start 0 len 5, got start " |
| 316 | "%llu len %llu\n", em->start, em->len); |
| 317 | goto out; |
| 318 | } |
| 319 | if (em->flags != 0) { |
| 320 | test_msg("Unexpected flags set, want 0 have %lu\n", em->flags); |
| 321 | goto out; |
| 322 | } |
| 323 | offset = em->start + em->len; |
| 324 | free_extent_map(em); |
| 325 | |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 326 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 327 | if (IS_ERR(em)) { |
| 328 | test_msg("Got an error when we shouldn't have\n"); |
| 329 | goto out; |
| 330 | } |
| 331 | if (em->block_start != EXTENT_MAP_INLINE) { |
| 332 | test_msg("Expected an inline, got %llu\n", em->block_start); |
| 333 | goto out; |
| 334 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 335 | |
| 336 | if (em->start != offset || em->len != (sectorsize - 5)) { |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 337 | test_msg("Unexpected extent wanted start %llu len 1, got start " |
| 338 | "%llu len %llu\n", offset, em->start, em->len); |
| 339 | goto out; |
| 340 | } |
| 341 | if (em->flags != 0) { |
| 342 | test_msg("Unexpected flags set, want 0 have %lu\n", em->flags); |
| 343 | goto out; |
| 344 | } |
| 345 | /* |
| 346 | * We don't test anything else for inline since it doesn't get set |
| 347 | * unless we have a page for it to write into. Maybe we should change |
| 348 | * this? |
| 349 | */ |
| 350 | offset = em->start + em->len; |
| 351 | free_extent_map(em); |
| 352 | |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 353 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 354 | if (IS_ERR(em)) { |
| 355 | test_msg("Got an error when we shouldn't have\n"); |
| 356 | goto out; |
| 357 | } |
| 358 | if (em->block_start != EXTENT_MAP_HOLE) { |
| 359 | test_msg("Expected a hole, got %llu\n", em->block_start); |
| 360 | goto out; |
| 361 | } |
| 362 | if (em->start != offset || em->len != 4) { |
| 363 | test_msg("Unexpected extent wanted start %llu len 4, got start " |
| 364 | "%llu len %llu\n", offset, em->start, em->len); |
| 365 | goto out; |
| 366 | } |
| 367 | if (em->flags != 0) { |
| 368 | test_msg("Unexpected flags set, want 0 have %lu\n", em->flags); |
| 369 | goto out; |
| 370 | } |
| 371 | offset = em->start + em->len; |
| 372 | free_extent_map(em); |
| 373 | |
| 374 | /* Regular extent */ |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 375 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 376 | if (IS_ERR(em)) { |
| 377 | test_msg("Got an error when we shouldn't have\n"); |
| 378 | goto out; |
| 379 | } |
| 380 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 381 | test_msg("Expected a real extent, got %llu\n", em->block_start); |
| 382 | goto out; |
| 383 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 384 | if (em->start != offset || em->len != sectorsize - 1) { |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 385 | test_msg("Unexpected extent wanted start %llu len 4095, got " |
| 386 | "start %llu len %llu\n", offset, em->start, em->len); |
| 387 | goto out; |
| 388 | } |
| 389 | if (em->flags != 0) { |
| 390 | test_msg("Unexpected flags set, want 0 have %lu\n", em->flags); |
| 391 | goto out; |
| 392 | } |
| 393 | if (em->orig_start != em->start) { |
| 394 | test_msg("Wrong orig offset, want %llu, have %llu\n", em->start, |
| 395 | em->orig_start); |
| 396 | goto out; |
| 397 | } |
| 398 | offset = em->start + em->len; |
| 399 | free_extent_map(em); |
| 400 | |
| 401 | /* The next 3 are split extents */ |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 402 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 403 | if (IS_ERR(em)) { |
| 404 | test_msg("Got an error when we shouldn't have\n"); |
| 405 | goto out; |
| 406 | } |
| 407 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 408 | test_msg("Expected a real extent, got %llu\n", em->block_start); |
| 409 | goto out; |
| 410 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 411 | if (em->start != offset || em->len != sectorsize) { |
| 412 | test_msg("Unexpected extent start %llu len %u, " |
| 413 | "got start %llu len %llu\n", |
| 414 | offset, sectorsize, em->start, em->len); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 415 | goto out; |
| 416 | } |
| 417 | if (em->flags != 0) { |
| 418 | test_msg("Unexpected flags set, want 0 have %lu\n", em->flags); |
| 419 | goto out; |
| 420 | } |
| 421 | if (em->orig_start != em->start) { |
| 422 | test_msg("Wrong orig offset, want %llu, have %llu\n", em->start, |
| 423 | em->orig_start); |
| 424 | goto out; |
| 425 | } |
| 426 | disk_bytenr = em->block_start; |
| 427 | orig_start = em->start; |
| 428 | offset = em->start + em->len; |
| 429 | free_extent_map(em); |
| 430 | |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 431 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 432 | if (IS_ERR(em)) { |
| 433 | test_msg("Got an error when we shouldn't have\n"); |
| 434 | goto out; |
| 435 | } |
| 436 | if (em->block_start != EXTENT_MAP_HOLE) { |
| 437 | test_msg("Expected a hole, got %llu\n", em->block_start); |
| 438 | goto out; |
| 439 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 440 | if (em->start != offset || em->len != sectorsize) { |
| 441 | test_msg("Unexpected extent wanted start %llu len %u, " |
| 442 | "got start %llu len %llu\n", |
| 443 | offset, sectorsize, em->start, em->len); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 444 | goto out; |
| 445 | } |
| 446 | if (em->flags != 0) { |
| 447 | test_msg("Unexpected flags set, want 0 have %lu\n", em->flags); |
| 448 | goto out; |
| 449 | } |
| 450 | offset = em->start + em->len; |
| 451 | free_extent_map(em); |
| 452 | |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 453 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 454 | if (IS_ERR(em)) { |
| 455 | test_msg("Got an error when we shouldn't have\n"); |
| 456 | goto out; |
| 457 | } |
| 458 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 459 | test_msg("Expected a real extent, got %llu\n", em->block_start); |
| 460 | goto out; |
| 461 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 462 | if (em->start != offset || em->len != 2 * sectorsize) { |
| 463 | test_msg("Unexpected extent wanted start %llu len %u, " |
| 464 | "got start %llu len %llu\n", |
| 465 | offset, 2 * sectorsize, em->start, em->len); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 466 | goto out; |
| 467 | } |
| 468 | if (em->flags != 0) { |
| 469 | test_msg("Unexpected flags set, want 0 have %lu\n", em->flags); |
| 470 | goto out; |
| 471 | } |
| 472 | if (em->orig_start != orig_start) { |
| 473 | test_msg("Wrong orig offset, want %llu, have %llu\n", |
| 474 | orig_start, em->orig_start); |
| 475 | goto out; |
| 476 | } |
| 477 | disk_bytenr += (em->start - orig_start); |
| 478 | if (em->block_start != disk_bytenr) { |
| 479 | test_msg("Wrong block start, want %llu, have %llu\n", |
| 480 | disk_bytenr, em->block_start); |
| 481 | goto out; |
| 482 | } |
| 483 | offset = em->start + em->len; |
| 484 | free_extent_map(em); |
| 485 | |
| 486 | /* Prealloc extent */ |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 487 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 488 | if (IS_ERR(em)) { |
| 489 | test_msg("Got an error when we shouldn't have\n"); |
| 490 | goto out; |
| 491 | } |
| 492 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 493 | test_msg("Expected a real extent, got %llu\n", em->block_start); |
| 494 | goto out; |
| 495 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 496 | if (em->start != offset || em->len != sectorsize) { |
| 497 | test_msg("Unexpected extent wanted start %llu len %u, " |
| 498 | "got start %llu len %llu\n", |
| 499 | offset, sectorsize, em->start, em->len); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 500 | goto out; |
| 501 | } |
| 502 | if (em->flags != prealloc_only) { |
| 503 | test_msg("Unexpected flags set, want %lu have %lu\n", |
| 504 | prealloc_only, em->flags); |
| 505 | goto out; |
| 506 | } |
| 507 | if (em->orig_start != em->start) { |
| 508 | test_msg("Wrong orig offset, want %llu, have %llu\n", em->start, |
| 509 | em->orig_start); |
| 510 | goto out; |
| 511 | } |
| 512 | offset = em->start + em->len; |
| 513 | free_extent_map(em); |
| 514 | |
| 515 | /* The next 3 are a half written prealloc extent */ |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 516 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 517 | if (IS_ERR(em)) { |
| 518 | test_msg("Got an error when we shouldn't have\n"); |
| 519 | goto out; |
| 520 | } |
| 521 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 522 | test_msg("Expected a real extent, got %llu\n", em->block_start); |
| 523 | goto out; |
| 524 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 525 | if (em->start != offset || em->len != sectorsize) { |
| 526 | test_msg("Unexpected extent wanted start %llu len %u, " |
| 527 | "got start %llu len %llu\n", |
| 528 | offset, sectorsize, em->start, em->len); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 529 | goto out; |
| 530 | } |
| 531 | if (em->flags != prealloc_only) { |
| 532 | test_msg("Unexpected flags set, want %lu have %lu\n", |
| 533 | prealloc_only, em->flags); |
| 534 | goto out; |
| 535 | } |
| 536 | if (em->orig_start != em->start) { |
| 537 | test_msg("Wrong orig offset, want %llu, have %llu\n", em->start, |
| 538 | em->orig_start); |
| 539 | goto out; |
| 540 | } |
| 541 | disk_bytenr = em->block_start; |
| 542 | orig_start = em->start; |
| 543 | offset = em->start + em->len; |
| 544 | free_extent_map(em); |
| 545 | |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 546 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 547 | if (IS_ERR(em)) { |
| 548 | test_msg("Got an error when we shouldn't have\n"); |
| 549 | goto out; |
| 550 | } |
| 551 | if (em->block_start >= EXTENT_MAP_HOLE) { |
| 552 | test_msg("Expected a real extent, got %llu\n", em->block_start); |
| 553 | goto out; |
| 554 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 555 | if (em->start != offset || em->len != sectorsize) { |
| 556 | test_msg("Unexpected extent wanted start %llu len %u, " |
| 557 | "got start %llu len %llu\n", |
| 558 | offset, sectorsize, em->start, em->len); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 559 | goto out; |
| 560 | } |
| 561 | if (em->flags != 0) { |
| 562 | test_msg("Unexpected flags set, want 0 have %lu\n", em->flags); |
| 563 | goto out; |
| 564 | } |
| 565 | if (em->orig_start != orig_start) { |
| 566 | test_msg("Unexpected orig offset, wanted %llu, have %llu\n", |
| 567 | orig_start, em->orig_start); |
| 568 | goto out; |
| 569 | } |
| 570 | if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) { |
| 571 | test_msg("Unexpected block start, wanted %llu, have %llu\n", |
| 572 | disk_bytenr + (em->start - em->orig_start), |
| 573 | em->block_start); |
| 574 | goto out; |
| 575 | } |
| 576 | offset = em->start + em->len; |
| 577 | free_extent_map(em); |
| 578 | |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 579 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 580 | if (IS_ERR(em)) { |
| 581 | test_msg("Got an error when we shouldn't have\n"); |
| 582 | goto out; |
| 583 | } |
| 584 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 585 | test_msg("Expected a real extent, got %llu\n", em->block_start); |
| 586 | goto out; |
| 587 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 588 | if (em->start != offset || em->len != 2 * sectorsize) { |
| 589 | test_msg("Unexpected extent wanted start %llu len %u, " |
| 590 | "got start %llu len %llu\n", |
| 591 | offset, 2 * sectorsize, em->start, em->len); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 592 | goto out; |
| 593 | } |
| 594 | if (em->flags != prealloc_only) { |
| 595 | test_msg("Unexpected flags set, want %lu have %lu\n", |
| 596 | prealloc_only, em->flags); |
| 597 | goto out; |
| 598 | } |
| 599 | if (em->orig_start != orig_start) { |
| 600 | test_msg("Wrong orig offset, want %llu, have %llu\n", orig_start, |
| 601 | em->orig_start); |
| 602 | goto out; |
| 603 | } |
| 604 | if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) { |
| 605 | test_msg("Unexpected block start, wanted %llu, have %llu\n", |
| 606 | disk_bytenr + (em->start - em->orig_start), |
| 607 | em->block_start); |
| 608 | goto out; |
| 609 | } |
| 610 | offset = em->start + em->len; |
| 611 | free_extent_map(em); |
| 612 | |
| 613 | /* Now for the compressed extent */ |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 614 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 615 | if (IS_ERR(em)) { |
| 616 | test_msg("Got an error when we shouldn't have\n"); |
| 617 | goto out; |
| 618 | } |
| 619 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 620 | test_msg("Expected a real extent, got %llu\n", em->block_start); |
| 621 | goto out; |
| 622 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 623 | if (em->start != offset || em->len != 2 * sectorsize) { |
| 624 | test_msg("Unexpected extent wanted start %llu len %u," |
| 625 | "got start %llu len %llu\n", |
| 626 | offset, 2 * sectorsize, em->start, em->len); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 627 | goto out; |
| 628 | } |
| 629 | if (em->flags != compressed_only) { |
| 630 | test_msg("Unexpected flags set, want %lu have %lu\n", |
| 631 | compressed_only, em->flags); |
| 632 | goto out; |
| 633 | } |
| 634 | if (em->orig_start != em->start) { |
| 635 | test_msg("Wrong orig offset, want %llu, have %llu\n", |
| 636 | em->start, em->orig_start); |
| 637 | goto out; |
| 638 | } |
| 639 | if (em->compress_type != BTRFS_COMPRESS_ZLIB) { |
| 640 | test_msg("Unexpected compress type, wanted %d, got %d\n", |
| 641 | BTRFS_COMPRESS_ZLIB, em->compress_type); |
| 642 | goto out; |
| 643 | } |
| 644 | offset = em->start + em->len; |
| 645 | free_extent_map(em); |
| 646 | |
| 647 | /* Split compressed extent */ |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 648 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 649 | if (IS_ERR(em)) { |
| 650 | test_msg("Got an error when we shouldn't have\n"); |
| 651 | goto out; |
| 652 | } |
| 653 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 654 | test_msg("Expected a real extent, got %llu\n", em->block_start); |
| 655 | goto out; |
| 656 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 657 | if (em->start != offset || em->len != sectorsize) { |
| 658 | test_msg("Unexpected extent wanted start %llu len %u," |
| 659 | "got start %llu len %llu\n", |
| 660 | offset, sectorsize, em->start, em->len); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 661 | goto out; |
| 662 | } |
| 663 | if (em->flags != compressed_only) { |
| 664 | test_msg("Unexpected flags set, want %lu have %lu\n", |
| 665 | compressed_only, em->flags); |
| 666 | goto out; |
| 667 | } |
| 668 | if (em->orig_start != em->start) { |
| 669 | test_msg("Wrong orig offset, want %llu, have %llu\n", |
| 670 | em->start, em->orig_start); |
| 671 | goto out; |
| 672 | } |
| 673 | if (em->compress_type != BTRFS_COMPRESS_ZLIB) { |
| 674 | test_msg("Unexpected compress type, wanted %d, got %d\n", |
| 675 | BTRFS_COMPRESS_ZLIB, em->compress_type); |
| 676 | goto out; |
| 677 | } |
| 678 | disk_bytenr = em->block_start; |
| 679 | orig_start = em->start; |
| 680 | offset = em->start + em->len; |
| 681 | free_extent_map(em); |
| 682 | |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 683 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 684 | if (IS_ERR(em)) { |
| 685 | test_msg("Got an error when we shouldn't have\n"); |
| 686 | goto out; |
| 687 | } |
| 688 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 689 | test_msg("Expected a real extent, got %llu\n", em->block_start); |
| 690 | goto out; |
| 691 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 692 | if (em->start != offset || em->len != sectorsize) { |
| 693 | test_msg("Unexpected extent wanted start %llu len %u, " |
| 694 | "got start %llu len %llu\n", |
| 695 | offset, sectorsize, em->start, em->len); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 696 | goto out; |
| 697 | } |
| 698 | if (em->flags != 0) { |
| 699 | test_msg("Unexpected flags set, want 0 have %lu\n", em->flags); |
| 700 | goto out; |
| 701 | } |
| 702 | if (em->orig_start != em->start) { |
| 703 | test_msg("Wrong orig offset, want %llu, have %llu\n", em->start, |
| 704 | em->orig_start); |
| 705 | goto out; |
| 706 | } |
| 707 | offset = em->start + em->len; |
| 708 | free_extent_map(em); |
| 709 | |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 710 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 711 | if (IS_ERR(em)) { |
| 712 | test_msg("Got an error when we shouldn't have\n"); |
| 713 | goto out; |
| 714 | } |
| 715 | if (em->block_start != disk_bytenr) { |
| 716 | test_msg("Block start does not match, want %llu got %llu\n", |
| 717 | disk_bytenr, em->block_start); |
| 718 | goto out; |
| 719 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 720 | if (em->start != offset || em->len != 2 * sectorsize) { |
| 721 | test_msg("Unexpected extent wanted start %llu len %u, " |
| 722 | "got start %llu len %llu\n", |
| 723 | offset, 2 * sectorsize, em->start, em->len); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 724 | goto out; |
| 725 | } |
| 726 | if (em->flags != compressed_only) { |
| 727 | test_msg("Unexpected flags set, want %lu have %lu\n", |
| 728 | compressed_only, em->flags); |
| 729 | goto out; |
| 730 | } |
| 731 | if (em->orig_start != orig_start) { |
| 732 | test_msg("Wrong orig offset, want %llu, have %llu\n", |
| 733 | em->start, orig_start); |
| 734 | goto out; |
| 735 | } |
| 736 | if (em->compress_type != BTRFS_COMPRESS_ZLIB) { |
| 737 | test_msg("Unexpected compress type, wanted %d, got %d\n", |
| 738 | BTRFS_COMPRESS_ZLIB, em->compress_type); |
| 739 | goto out; |
| 740 | } |
| 741 | offset = em->start + em->len; |
| 742 | free_extent_map(em); |
| 743 | |
| 744 | /* A hole between regular extents but no hole extent */ |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 745 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset + 6, |
| 746 | sectorsize, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 747 | if (IS_ERR(em)) { |
| 748 | test_msg("Got an error when we shouldn't have\n"); |
| 749 | goto out; |
| 750 | } |
| 751 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 752 | test_msg("Expected a real extent, got %llu\n", em->block_start); |
| 753 | goto out; |
| 754 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 755 | if (em->start != offset || em->len != sectorsize) { |
| 756 | test_msg("Unexpected extent wanted start %llu len %u, " |
| 757 | "got start %llu len %llu\n", |
| 758 | offset, sectorsize, em->start, em->len); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 759 | goto out; |
| 760 | } |
| 761 | if (em->flags != 0) { |
| 762 | test_msg("Unexpected flags set, want 0 have %lu\n", em->flags); |
| 763 | goto out; |
| 764 | } |
| 765 | if (em->orig_start != em->start) { |
| 766 | test_msg("Wrong orig offset, want %llu, have %llu\n", em->start, |
| 767 | em->orig_start); |
| 768 | goto out; |
| 769 | } |
| 770 | offset = em->start + em->len; |
| 771 | free_extent_map(em); |
| 772 | |
Nikolay Borisov | d4417e2 | 2017-10-16 16:48:40 +0300 | [diff] [blame^] | 773 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, SZ_4M, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 774 | if (IS_ERR(em)) { |
| 775 | test_msg("Got an error when we shouldn't have\n"); |
| 776 | goto out; |
| 777 | } |
| 778 | if (em->block_start != EXTENT_MAP_HOLE) { |
| 779 | test_msg("Expected a hole extent, got %llu\n", em->block_start); |
| 780 | goto out; |
| 781 | } |
| 782 | /* |
| 783 | * Currently we just return a length that we requested rather than the |
| 784 | * length of the actual hole, if this changes we'll have to change this |
| 785 | * test. |
| 786 | */ |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 787 | if (em->start != offset || em->len != 3 * sectorsize) { |
| 788 | test_msg("Unexpected extent wanted start %llu len %u, " |
| 789 | "got start %llu len %llu\n", |
| 790 | offset, 3 * sectorsize, em->start, em->len); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 791 | goto out; |
| 792 | } |
| 793 | if (em->flags != vacancy_only) { |
| 794 | test_msg("Unexpected flags set, want %lu have %lu\n", |
| 795 | vacancy_only, em->flags); |
| 796 | goto out; |
| 797 | } |
| 798 | if (em->orig_start != em->start) { |
| 799 | test_msg("Wrong orig offset, want %llu, have %llu\n", em->start, |
| 800 | em->orig_start); |
| 801 | goto out; |
| 802 | } |
| 803 | offset = em->start + em->len; |
| 804 | free_extent_map(em); |
| 805 | |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 806 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 807 | if (IS_ERR(em)) { |
| 808 | test_msg("Got an error when we shouldn't have\n"); |
| 809 | goto out; |
| 810 | } |
| 811 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 812 | test_msg("Expected a real extent, got %llu\n", em->block_start); |
| 813 | goto out; |
| 814 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 815 | if (em->start != offset || em->len != sectorsize) { |
| 816 | test_msg("Unexpected extent wanted start %llu len %u," |
| 817 | "got start %llu len %llu\n", |
| 818 | offset, sectorsize, em->start, em->len); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 819 | goto out; |
| 820 | } |
| 821 | if (em->flags != 0) { |
| 822 | test_msg("Unexpected flags set, want 0 have %lu\n", em->flags); |
| 823 | goto out; |
| 824 | } |
| 825 | if (em->orig_start != em->start) { |
| 826 | test_msg("Wrong orig offset, want %llu, have %llu\n", em->start, |
| 827 | em->orig_start); |
| 828 | goto out; |
| 829 | } |
| 830 | ret = 0; |
| 831 | out: |
| 832 | if (!IS_ERR(em)) |
| 833 | free_extent_map(em); |
| 834 | iput(inode); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 835 | btrfs_free_dummy_root(root); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 836 | btrfs_free_dummy_fs_info(fs_info); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 837 | return ret; |
| 838 | } |
| 839 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 840 | static int test_hole_first(u32 sectorsize, u32 nodesize) |
Josef Bacik | 0e30db8 | 2013-10-14 13:15:02 -0400 | [diff] [blame] | 841 | { |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 842 | struct btrfs_fs_info *fs_info = NULL; |
Josef Bacik | 0e30db8 | 2013-10-14 13:15:02 -0400 | [diff] [blame] | 843 | struct inode *inode = NULL; |
| 844 | struct btrfs_root *root = NULL; |
| 845 | struct extent_map *em = NULL; |
| 846 | int ret = -ENOMEM; |
| 847 | |
| 848 | inode = btrfs_new_test_inode(); |
| 849 | if (!inode) { |
| 850 | test_msg("Couldn't allocate inode\n"); |
| 851 | return ret; |
| 852 | } |
| 853 | |
| 854 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 855 | BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 856 | BTRFS_I(inode)->location.offset = 0; |
| 857 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 858 | fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 859 | if (!fs_info) { |
| 860 | test_msg("Couldn't allocate dummy fs info\n"); |
Josef Bacik | 0e30db8 | 2013-10-14 13:15:02 -0400 | [diff] [blame] | 861 | goto out; |
| 862 | } |
| 863 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 864 | root = btrfs_alloc_dummy_root(fs_info); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 865 | if (IS_ERR(root)) { |
| 866 | test_msg("Couldn't allocate root\n"); |
Josef Bacik | 0e30db8 | 2013-10-14 13:15:02 -0400 | [diff] [blame] | 867 | goto out; |
| 868 | } |
| 869 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 870 | root->node = alloc_dummy_extent_buffer(fs_info, nodesize); |
Josef Bacik | 0e30db8 | 2013-10-14 13:15:02 -0400 | [diff] [blame] | 871 | if (!root->node) { |
| 872 | test_msg("Couldn't allocate dummy buffer\n"); |
| 873 | goto out; |
| 874 | } |
| 875 | |
| 876 | extent_buffer_get(root->node); |
| 877 | btrfs_set_header_nritems(root->node, 0); |
| 878 | btrfs_set_header_level(root->node, 0); |
| 879 | BTRFS_I(inode)->root = root; |
| 880 | ret = -EINVAL; |
| 881 | |
| 882 | /* |
| 883 | * Need a blank inode item here just so we don't confuse |
| 884 | * btrfs_get_extent. |
| 885 | */ |
| 886 | insert_inode_item_key(root); |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 887 | insert_extent(root, sectorsize, sectorsize, sectorsize, 0, sectorsize, |
| 888 | sectorsize, BTRFS_FILE_EXTENT_REG, 0, 1); |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 889 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, 2 * sectorsize, 0); |
Josef Bacik | 0e30db8 | 2013-10-14 13:15:02 -0400 | [diff] [blame] | 890 | if (IS_ERR(em)) { |
| 891 | test_msg("Got an error when we shouldn't have\n"); |
| 892 | goto out; |
| 893 | } |
| 894 | if (em->block_start != EXTENT_MAP_HOLE) { |
| 895 | test_msg("Expected a hole, got %llu\n", em->block_start); |
| 896 | goto out; |
| 897 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 898 | if (em->start != 0 || em->len != sectorsize) { |
| 899 | test_msg("Unexpected extent wanted start 0 len %u, " |
| 900 | "got start %llu len %llu\n", |
| 901 | sectorsize, em->start, em->len); |
Josef Bacik | 0e30db8 | 2013-10-14 13:15:02 -0400 | [diff] [blame] | 902 | goto out; |
| 903 | } |
| 904 | if (em->flags != vacancy_only) { |
| 905 | test_msg("Wrong flags, wanted %lu, have %lu\n", vacancy_only, |
| 906 | em->flags); |
| 907 | goto out; |
| 908 | } |
| 909 | free_extent_map(em); |
| 910 | |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 911 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, sectorsize, |
| 912 | 2 * sectorsize, 0); |
Josef Bacik | 0e30db8 | 2013-10-14 13:15:02 -0400 | [diff] [blame] | 913 | if (IS_ERR(em)) { |
| 914 | test_msg("Got an error when we shouldn't have\n"); |
| 915 | goto out; |
| 916 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 917 | if (em->block_start != sectorsize) { |
Josef Bacik | 0e30db8 | 2013-10-14 13:15:02 -0400 | [diff] [blame] | 918 | test_msg("Expected a real extent, got %llu\n", em->block_start); |
| 919 | goto out; |
| 920 | } |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 921 | if (em->start != sectorsize || em->len != sectorsize) { |
| 922 | test_msg("Unexpected extent wanted start %u len %u, " |
| 923 | "got start %llu len %llu\n", |
| 924 | sectorsize, sectorsize, em->start, em->len); |
Josef Bacik | 0e30db8 | 2013-10-14 13:15:02 -0400 | [diff] [blame] | 925 | goto out; |
| 926 | } |
| 927 | if (em->flags != 0) { |
| 928 | test_msg("Unexpected flags set, wanted 0 got %lu\n", |
| 929 | em->flags); |
| 930 | goto out; |
| 931 | } |
| 932 | ret = 0; |
| 933 | out: |
| 934 | if (!IS_ERR(em)) |
| 935 | free_extent_map(em); |
| 936 | iput(inode); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 937 | btrfs_free_dummy_root(root); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 938 | btrfs_free_dummy_fs_info(fs_info); |
Josef Bacik | 0e30db8 | 2013-10-14 13:15:02 -0400 | [diff] [blame] | 939 | return ret; |
| 940 | } |
| 941 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 942 | static int test_extent_accounting(u32 sectorsize, u32 nodesize) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 943 | { |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 944 | struct btrfs_fs_info *fs_info = NULL; |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 945 | struct inode *inode = NULL; |
| 946 | struct btrfs_root *root = NULL; |
| 947 | int ret = -ENOMEM; |
| 948 | |
| 949 | inode = btrfs_new_test_inode(); |
| 950 | if (!inode) { |
| 951 | test_msg("Couldn't allocate inode\n"); |
| 952 | return ret; |
| 953 | } |
| 954 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 955 | fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 956 | if (!fs_info) { |
| 957 | test_msg("Couldn't allocate dummy fs info\n"); |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 958 | goto out; |
| 959 | } |
| 960 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 961 | root = btrfs_alloc_dummy_root(fs_info); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 962 | if (IS_ERR(root)) { |
| 963 | test_msg("Couldn't allocate root\n"); |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 964 | goto out; |
| 965 | } |
| 966 | |
| 967 | BTRFS_I(inode)->root = root; |
| 968 | btrfs_test_inode_set_ops(inode); |
| 969 | |
| 970 | /* [BTRFS_MAX_EXTENT_SIZE] */ |
| 971 | BTRFS_I(inode)->outstanding_extents++; |
| 972 | ret = btrfs_set_extent_delalloc(inode, 0, BTRFS_MAX_EXTENT_SIZE - 1, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 973 | NULL, 0); |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 974 | if (ret) { |
| 975 | test_msg("btrfs_set_extent_delalloc returned %d\n", ret); |
| 976 | goto out; |
| 977 | } |
| 978 | if (BTRFS_I(inode)->outstanding_extents != 1) { |
| 979 | ret = -EINVAL; |
| 980 | test_msg("Miscount, wanted 1, got %u\n", |
| 981 | BTRFS_I(inode)->outstanding_extents); |
| 982 | goto out; |
| 983 | } |
| 984 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 985 | /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */ |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 986 | BTRFS_I(inode)->outstanding_extents++; |
| 987 | ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE, |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 988 | BTRFS_MAX_EXTENT_SIZE + sectorsize - 1, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 989 | NULL, 0); |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 990 | if (ret) { |
| 991 | test_msg("btrfs_set_extent_delalloc returned %d\n", ret); |
| 992 | goto out; |
| 993 | } |
| 994 | if (BTRFS_I(inode)->outstanding_extents != 2) { |
| 995 | ret = -EINVAL; |
| 996 | test_msg("Miscount, wanted 2, got %u\n", |
| 997 | BTRFS_I(inode)->outstanding_extents); |
| 998 | goto out; |
| 999 | } |
| 1000 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 1001 | /* [BTRFS_MAX_EXTENT_SIZE/2][sectorsize HOLE][the rest] */ |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1002 | ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, |
| 1003 | BTRFS_MAX_EXTENT_SIZE >> 1, |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 1004 | (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1, |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1005 | EXTENT_DELALLOC | EXTENT_DIRTY | |
| 1006 | EXTENT_UPTODATE | EXTENT_DO_ACCOUNTING, 0, 0, |
David Sterba | 8cce83b | 2016-01-22 10:28:24 +0100 | [diff] [blame] | 1007 | NULL, GFP_KERNEL); |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1008 | if (ret) { |
| 1009 | test_msg("clear_extent_bit returned %d\n", ret); |
| 1010 | goto out; |
| 1011 | } |
| 1012 | if (BTRFS_I(inode)->outstanding_extents != 2) { |
| 1013 | ret = -EINVAL; |
| 1014 | test_msg("Miscount, wanted 2, got %u\n", |
| 1015 | BTRFS_I(inode)->outstanding_extents); |
| 1016 | goto out; |
| 1017 | } |
| 1018 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 1019 | /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */ |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1020 | BTRFS_I(inode)->outstanding_extents++; |
| 1021 | ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE >> 1, |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 1022 | (BTRFS_MAX_EXTENT_SIZE >> 1) |
| 1023 | + sectorsize - 1, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1024 | NULL, 0); |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1025 | if (ret) { |
| 1026 | test_msg("btrfs_set_extent_delalloc returned %d\n", ret); |
| 1027 | goto out; |
| 1028 | } |
| 1029 | if (BTRFS_I(inode)->outstanding_extents != 2) { |
| 1030 | ret = -EINVAL; |
| 1031 | test_msg("Miscount, wanted 2, got %u\n", |
| 1032 | BTRFS_I(inode)->outstanding_extents); |
| 1033 | goto out; |
| 1034 | } |
| 1035 | |
| 1036 | /* |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 1037 | * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize] |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1038 | * |
| 1039 | * I'm artificially adding 2 to outstanding_extents because in the |
| 1040 | * buffered IO case we'd add things up as we go, but I don't feel like |
| 1041 | * doing that here, this isn't the interesting case we want to test. |
| 1042 | */ |
| 1043 | BTRFS_I(inode)->outstanding_extents += 2; |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 1044 | ret = btrfs_set_extent_delalloc(inode, |
| 1045 | BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize, |
| 1046 | (BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1047 | NULL, 0); |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1048 | if (ret) { |
| 1049 | test_msg("btrfs_set_extent_delalloc returned %d\n", ret); |
| 1050 | goto out; |
| 1051 | } |
| 1052 | if (BTRFS_I(inode)->outstanding_extents != 4) { |
| 1053 | ret = -EINVAL; |
| 1054 | test_msg("Miscount, wanted 4, got %u\n", |
| 1055 | BTRFS_I(inode)->outstanding_extents); |
| 1056 | goto out; |
| 1057 | } |
| 1058 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 1059 | /* |
| 1060 | * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize] |
| 1061 | */ |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1062 | BTRFS_I(inode)->outstanding_extents++; |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 1063 | ret = btrfs_set_extent_delalloc(inode, |
| 1064 | BTRFS_MAX_EXTENT_SIZE + sectorsize, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1065 | BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, NULL, 0); |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1066 | if (ret) { |
| 1067 | test_msg("btrfs_set_extent_delalloc returned %d\n", ret); |
| 1068 | goto out; |
| 1069 | } |
| 1070 | if (BTRFS_I(inode)->outstanding_extents != 3) { |
| 1071 | ret = -EINVAL; |
| 1072 | test_msg("Miscount, wanted 3, got %u\n", |
| 1073 | BTRFS_I(inode)->outstanding_extents); |
| 1074 | goto out; |
| 1075 | } |
| 1076 | |
| 1077 | /* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */ |
| 1078 | ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 1079 | BTRFS_MAX_EXTENT_SIZE + sectorsize, |
| 1080 | BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1081 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 1082 | EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0, |
David Sterba | 8cce83b | 2016-01-22 10:28:24 +0100 | [diff] [blame] | 1083 | NULL, GFP_KERNEL); |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1084 | if (ret) { |
| 1085 | test_msg("clear_extent_bit returned %d\n", ret); |
| 1086 | goto out; |
| 1087 | } |
| 1088 | if (BTRFS_I(inode)->outstanding_extents != 4) { |
| 1089 | ret = -EINVAL; |
| 1090 | test_msg("Miscount, wanted 4, got %u\n", |
| 1091 | BTRFS_I(inode)->outstanding_extents); |
| 1092 | goto out; |
| 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * Refill the hole again just for good measure, because I thought it |
| 1097 | * might fail and I'd rather satisfy my paranoia at this point. |
| 1098 | */ |
| 1099 | BTRFS_I(inode)->outstanding_extents++; |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 1100 | ret = btrfs_set_extent_delalloc(inode, |
| 1101 | BTRFS_MAX_EXTENT_SIZE + sectorsize, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1102 | BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, NULL, 0); |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1103 | if (ret) { |
| 1104 | test_msg("btrfs_set_extent_delalloc returned %d\n", ret); |
| 1105 | goto out; |
| 1106 | } |
| 1107 | if (BTRFS_I(inode)->outstanding_extents != 3) { |
| 1108 | ret = -EINVAL; |
| 1109 | test_msg("Miscount, wanted 3, got %u\n", |
| 1110 | BTRFS_I(inode)->outstanding_extents); |
| 1111 | goto out; |
| 1112 | } |
| 1113 | |
| 1114 | /* Empty */ |
| 1115 | ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1, |
| 1116 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 1117 | EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0, |
David Sterba | 8cce83b | 2016-01-22 10:28:24 +0100 | [diff] [blame] | 1118 | NULL, GFP_KERNEL); |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1119 | if (ret) { |
| 1120 | test_msg("clear_extent_bit returned %d\n", ret); |
| 1121 | goto out; |
| 1122 | } |
| 1123 | if (BTRFS_I(inode)->outstanding_extents) { |
| 1124 | ret = -EINVAL; |
| 1125 | test_msg("Miscount, wanted 0, got %u\n", |
| 1126 | BTRFS_I(inode)->outstanding_extents); |
| 1127 | goto out; |
| 1128 | } |
| 1129 | ret = 0; |
| 1130 | out: |
| 1131 | if (ret) |
| 1132 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1, |
| 1133 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 1134 | EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0, |
David Sterba | 8cce83b | 2016-01-22 10:28:24 +0100 | [diff] [blame] | 1135 | NULL, GFP_KERNEL); |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1136 | iput(inode); |
| 1137 | btrfs_free_dummy_root(root); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 1138 | btrfs_free_dummy_fs_info(fs_info); |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1139 | return ret; |
| 1140 | } |
| 1141 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 1142 | int btrfs_test_inodes(u32 sectorsize, u32 nodesize) |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 1143 | { |
Josef Bacik | 0e30db8 | 2013-10-14 13:15:02 -0400 | [diff] [blame] | 1144 | int ret; |
| 1145 | |
| 1146 | set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only); |
| 1147 | set_bit(EXTENT_FLAG_VACANCY, &vacancy_only); |
| 1148 | set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only); |
| 1149 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 1150 | test_msg("Running btrfs_get_extent tests\n"); |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 1151 | ret = test_btrfs_get_extent(sectorsize, nodesize); |
Josef Bacik | 0e30db8 | 2013-10-14 13:15:02 -0400 | [diff] [blame] | 1152 | if (ret) |
| 1153 | return ret; |
| 1154 | test_msg("Running hole first btrfs_get_extent test\n"); |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 1155 | ret = test_hole_first(sectorsize, nodesize); |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1156 | if (ret) |
| 1157 | return ret; |
| 1158 | test_msg("Running outstanding_extents tests\n"); |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 1159 | return test_extent_accounting(sectorsize, nodesize); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 1160 | } |