blob: e0ba799536b48d55d30be6febdbe317917413233 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Josef Bacikaaedb552013-10-11 14:44:09 -04002/*
3 * Copyright (C) 2013 Fusion IO. All rights reserved.
Josef Bacikaaedb552013-10-11 14:44:09 -04004 */
5
Feifei Xub9ef22d2016-06-01 19:18:25 +08006#include <linux/types.h>
Josef Bacikaaedb552013-10-11 14:44:09 -04007#include "btrfs-tests.h"
8#include "../ctree.h"
9#include "../btrfs_inode.h"
10#include "../disk-io.h"
11#include "../extent_io.h"
12#include "../volumes.h"
Anand Jainebb87652016-03-10 17:26:59 +080013#include "../compression.h"
Josef Bacikaaedb552013-10-11 14:44:09 -040014
Josef Bacikaaedb552013-10-11 14:44:09 -040015static void insert_extent(struct btrfs_root *root, u64 start, u64 len,
16 u64 ram_bytes, u64 offset, u64 disk_bytenr,
17 u64 disk_len, u32 type, u8 compression, int slot)
18{
19 struct btrfs_path path;
20 struct btrfs_file_extent_item *fi;
21 struct extent_buffer *leaf = root->node;
22 struct btrfs_key key;
23 u32 value_len = sizeof(struct btrfs_file_extent_item);
24
25 if (type == BTRFS_FILE_EXTENT_INLINE)
26 value_len += len;
27 memset(&path, 0, sizeof(path));
28
29 path.nodes[0] = leaf;
30 path.slots[0] = slot;
31
32 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
33 key.type = BTRFS_EXTENT_DATA_KEY;
34 key.offset = start;
35
36 setup_items_for_insert(root, &path, &key, &value_len, value_len,
37 value_len + sizeof(struct btrfs_item), 1);
38 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
39 btrfs_set_file_extent_generation(leaf, fi, 1);
40 btrfs_set_file_extent_type(leaf, fi, type);
41 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
42 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_len);
43 btrfs_set_file_extent_offset(leaf, fi, offset);
44 btrfs_set_file_extent_num_bytes(leaf, fi, len);
45 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
46 btrfs_set_file_extent_compression(leaf, fi, compression);
47 btrfs_set_file_extent_encryption(leaf, fi, 0);
48 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
49}
50
Josef Bacik0e30db82013-10-14 13:15:02 -040051static void insert_inode_item_key(struct btrfs_root *root)
52{
53 struct btrfs_path path;
54 struct extent_buffer *leaf = root->node;
55 struct btrfs_key key;
56 u32 value_len = 0;
57
58 memset(&path, 0, sizeof(path));
59
60 path.nodes[0] = leaf;
61 path.slots[0] = 0;
62
63 key.objectid = BTRFS_INODE_ITEM_KEY;
64 key.type = BTRFS_INODE_ITEM_KEY;
65 key.offset = 0;
66
67 setup_items_for_insert(root, &path, &key, &value_len, value_len,
68 value_len + sizeof(struct btrfs_item), 1);
69}
70
Josef Bacikaaedb552013-10-11 14:44:09 -040071/*
72 * Build the most complicated map of extents the earth has ever seen. We want
73 * this so we can test all of the corner cases of btrfs_get_extent. Here is a
74 * diagram of how the extents will look though this may not be possible we still
75 * want to make sure everything acts normally (the last number is not inclusive)
76 *
Feifei Xub9ef22d2016-06-01 19:18:25 +080077 * [0 - 5][5 - 6][ 6 - 4096 ][ 4096 - 4100][4100 - 8195][8195 - 12291]
78 * [hole ][inline][hole but no extent][ hole ][ regular ][regular1 split]
Josef Bacikaaedb552013-10-11 14:44:09 -040079 *
Feifei Xub9ef22d2016-06-01 19:18:25 +080080 * [12291 - 16387][16387 - 24579][24579 - 28675][ 28675 - 32771][32771 - 36867 ]
81 * [ hole ][regular1 split][ prealloc ][ prealloc1 ][prealloc1 written]
Josef Bacikaaedb552013-10-11 14:44:09 -040082 *
Feifei Xub9ef22d2016-06-01 19:18:25 +080083 * [36867 - 45059][45059 - 53251][53251 - 57347][57347 - 61443][61443- 69635]
84 * [ prealloc1 ][ compressed ][ compressed1 ][ regular ][ compressed1]
Josef Bacikaaedb552013-10-11 14:44:09 -040085 *
Feifei Xub9ef22d2016-06-01 19:18:25 +080086 * [69635-73731][ 73731 - 86019 ][86019-90115]
87 * [ regular ][ hole but no extent][ regular ]
Josef Bacikaaedb552013-10-11 14:44:09 -040088 */
Feifei Xub9ef22d2016-06-01 19:18:25 +080089static void setup_file_extents(struct btrfs_root *root, u32 sectorsize)
Josef Bacikaaedb552013-10-11 14:44:09 -040090{
91 int slot = 0;
Byongho Leeee221842015-12-15 01:42:10 +090092 u64 disk_bytenr = SZ_1M;
Josef Bacikaaedb552013-10-11 14:44:09 -040093 u64 offset = 0;
94
95 /* First we want a hole */
96 insert_extent(root, offset, 5, 5, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
97 slot);
98 slot++;
99 offset += 5;
100
101 /*
102 * Now we want an inline extent, I don't think this is possible but hey
103 * why not? Also keep in mind if we have an inline extent it counts as
104 * the whole first page. If we were to expand it we would have to cow
105 * and we wouldn't have an inline extent anymore.
106 */
107 insert_extent(root, offset, 1, 1, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE, 0,
108 slot);
109 slot++;
Feifei Xub9ef22d2016-06-01 19:18:25 +0800110 offset = sectorsize;
Josef Bacikaaedb552013-10-11 14:44:09 -0400111
112 /* Now another hole */
113 insert_extent(root, offset, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
114 slot);
115 slot++;
116 offset += 4;
117
118 /* Now for a regular extent */
Feifei Xub9ef22d2016-06-01 19:18:25 +0800119 insert_extent(root, offset, sectorsize - 1, sectorsize - 1, 0,
120 disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
Josef Bacikaaedb552013-10-11 14:44:09 -0400121 slot++;
Feifei Xub9ef22d2016-06-01 19:18:25 +0800122 disk_bytenr += sectorsize;
123 offset += sectorsize - 1;
Josef Bacikaaedb552013-10-11 14:44:09 -0400124
125 /*
126 * Now for 3 extents that were split from a hole punch so we test
127 * offsets properly.
128 */
Feifei Xub9ef22d2016-06-01 19:18:25 +0800129 insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
130 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
131 slot++;
132 offset += sectorsize;
133 insert_extent(root, offset, sectorsize, sectorsize, 0, 0, 0,
Josef Bacikaaedb552013-10-11 14:44:09 -0400134 BTRFS_FILE_EXTENT_REG, 0, slot);
135 slot++;
Feifei Xub9ef22d2016-06-01 19:18:25 +0800136 offset += sectorsize;
137 insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
138 2 * sectorsize, disk_bytenr, 4 * sectorsize,
Josef Bacikaaedb552013-10-11 14:44:09 -0400139 BTRFS_FILE_EXTENT_REG, 0, slot);
140 slot++;
Feifei Xub9ef22d2016-06-01 19:18:25 +0800141 offset += 2 * sectorsize;
142 disk_bytenr += 4 * sectorsize;
Josef Bacikaaedb552013-10-11 14:44:09 -0400143
144 /* Now for a unwritten prealloc extent */
Feifei Xub9ef22d2016-06-01 19:18:25 +0800145 insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
146 sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
Josef Bacikaaedb552013-10-11 14:44:09 -0400147 slot++;
Feifei Xub9ef22d2016-06-01 19:18:25 +0800148 offset += sectorsize;
Josef Bacikaaedb552013-10-11 14:44:09 -0400149
150 /*
151 * We want to jack up disk_bytenr a little more so the em stuff doesn't
152 * merge our records.
153 */
Feifei Xub9ef22d2016-06-01 19:18:25 +0800154 disk_bytenr += 2 * sectorsize;
Josef Bacikaaedb552013-10-11 14:44:09 -0400155
156 /*
157 * Now for a partially written prealloc extent, basically the same as
158 * the hole punch example above. Ram_bytes never changes when you mark
159 * extents written btw.
160 */
Feifei Xub9ef22d2016-06-01 19:18:25 +0800161 insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
162 4 * sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
163 slot++;
164 offset += sectorsize;
165 insert_extent(root, offset, sectorsize, 4 * sectorsize, sectorsize,
166 disk_bytenr, 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0,
167 slot);
168 slot++;
169 offset += sectorsize;
170 insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
171 2 * sectorsize, disk_bytenr, 4 * sectorsize,
Josef Bacikaaedb552013-10-11 14:44:09 -0400172 BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
173 slot++;
Feifei Xub9ef22d2016-06-01 19:18:25 +0800174 offset += 2 * sectorsize;
175 disk_bytenr += 4 * sectorsize;
Josef Bacikaaedb552013-10-11 14:44:09 -0400176
177 /* Now a normal compressed extent */
Feifei Xub9ef22d2016-06-01 19:18:25 +0800178 insert_extent(root, offset, 2 * sectorsize, 2 * sectorsize, 0,
179 disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG,
180 BTRFS_COMPRESS_ZLIB, slot);
Josef Bacikaaedb552013-10-11 14:44:09 -0400181 slot++;
Feifei Xub9ef22d2016-06-01 19:18:25 +0800182 offset += 2 * sectorsize;
Josef Bacikaaedb552013-10-11 14:44:09 -0400183 /* No merges */
Feifei Xub9ef22d2016-06-01 19:18:25 +0800184 disk_bytenr += 2 * sectorsize;
Josef Bacikaaedb552013-10-11 14:44:09 -0400185
186 /* Now a split compressed extent */
Feifei Xub9ef22d2016-06-01 19:18:25 +0800187 insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
188 sectorsize, BTRFS_FILE_EXTENT_REG,
189 BTRFS_COMPRESS_ZLIB, slot);
Josef Bacikaaedb552013-10-11 14:44:09 -0400190 slot++;
Feifei Xub9ef22d2016-06-01 19:18:25 +0800191 offset += sectorsize;
192 insert_extent(root, offset, sectorsize, sectorsize, 0,
193 disk_bytenr + sectorsize, sectorsize,
Josef Bacikaaedb552013-10-11 14:44:09 -0400194 BTRFS_FILE_EXTENT_REG, 0, slot);
195 slot++;
Feifei Xub9ef22d2016-06-01 19:18:25 +0800196 offset += sectorsize;
197 insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
198 2 * sectorsize, disk_bytenr, sectorsize,
Josef Bacikaaedb552013-10-11 14:44:09 -0400199 BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
200 slot++;
Feifei Xub9ef22d2016-06-01 19:18:25 +0800201 offset += 2 * sectorsize;
202 disk_bytenr += 2 * sectorsize;
Josef Bacikaaedb552013-10-11 14:44:09 -0400203
204 /* Now extents that have a hole but no hole extent */
Feifei Xub9ef22d2016-06-01 19:18:25 +0800205 insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
206 sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
Josef Bacikaaedb552013-10-11 14:44:09 -0400207 slot++;
Feifei Xub9ef22d2016-06-01 19:18:25 +0800208 offset += 4 * sectorsize;
209 disk_bytenr += sectorsize;
210 insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
211 sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
Josef Bacikaaedb552013-10-11 14:44:09 -0400212}
213
214static unsigned long prealloc_only = 0;
215static unsigned long compressed_only = 0;
216static unsigned long vacancy_only = 0;
217
Feifei Xub9ef22d2016-06-01 19:18:25 +0800218static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize)
Josef Bacikaaedb552013-10-11 14:44:09 -0400219{
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400220 struct btrfs_fs_info *fs_info = NULL;
Josef Bacikaaedb552013-10-11 14:44:09 -0400221 struct inode *inode = NULL;
222 struct btrfs_root *root = NULL;
223 struct extent_map *em = NULL;
224 u64 orig_start;
225 u64 disk_bytenr;
226 u64 offset;
227 int ret = -ENOMEM;
228
Josef Bacikaaedb552013-10-11 14:44:09 -0400229 inode = btrfs_new_test_inode();
230 if (!inode) {
231 test_msg("Couldn't allocate inode\n");
232 return ret;
233 }
234
235 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
236 BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
237 BTRFS_I(inode)->location.offset = 0;
238
Jeff Mahoneyda170662016-06-15 09:22:56 -0400239 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400240 if (!fs_info) {
241 test_msg("Couldn't allocate dummy fs info\n");
Josef Bacikaaedb552013-10-11 14:44:09 -0400242 goto out;
243 }
244
Jeff Mahoneyda170662016-06-15 09:22:56 -0400245 root = btrfs_alloc_dummy_root(fs_info);
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400246 if (IS_ERR(root)) {
247 test_msg("Couldn't allocate root\n");
Josef Bacikaaedb552013-10-11 14:44:09 -0400248 goto out;
249 }
250
Jeff Mahoneyda170662016-06-15 09:22:56 -0400251 root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
Josef Bacikaaedb552013-10-11 14:44:09 -0400252 if (!root->node) {
253 test_msg("Couldn't allocate dummy buffer\n");
254 goto out;
255 }
256
257 /*
258 * We will just free a dummy node if it's ref count is 2 so we need an
Nicholas D Steeves01327612016-05-19 21:18:45 -0400259 * extra ref so our searches don't accidentally release our page.
Josef Bacikaaedb552013-10-11 14:44:09 -0400260 */
261 extent_buffer_get(root->node);
262 btrfs_set_header_nritems(root->node, 0);
263 btrfs_set_header_level(root->node, 0);
264 ret = -EINVAL;
265
266 /* First with no extents */
267 BTRFS_I(inode)->root = root;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200268 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, sectorsize, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400269 if (IS_ERR(em)) {
270 em = NULL;
271 test_msg("Got an error when we shouldn't have\n");
272 goto out;
273 }
274 if (em->block_start != EXTENT_MAP_HOLE) {
275 test_msg("Expected a hole, got %llu\n", em->block_start);
276 goto out;
277 }
Josef Bacikaaedb552013-10-11 14:44:09 -0400278 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200279 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400280
281 /*
282 * All of the magic numbers are based on the mapping setup in
283 * setup_file_extents, so if you change anything there you need to
284 * update the comment and update the expected values below.
285 */
Feifei Xub9ef22d2016-06-01 19:18:25 +0800286 setup_file_extents(root, sectorsize);
Josef Bacikaaedb552013-10-11 14:44:09 -0400287
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200288 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400289 if (IS_ERR(em)) {
290 test_msg("Got an error when we shouldn't have\n");
291 goto out;
292 }
293 if (em->block_start != EXTENT_MAP_HOLE) {
294 test_msg("Expected a hole, got %llu\n", em->block_start);
295 goto out;
296 }
297 if (em->start != 0 || em->len != 5) {
298 test_msg("Unexpected extent wanted start 0 len 5, got start "
299 "%llu len %llu\n", em->start, em->len);
300 goto out;
301 }
302 if (em->flags != 0) {
303 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
304 goto out;
305 }
306 offset = em->start + em->len;
307 free_extent_map(em);
308
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200309 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400310 if (IS_ERR(em)) {
311 test_msg("Got an error when we shouldn't have\n");
312 goto out;
313 }
314 if (em->block_start != EXTENT_MAP_INLINE) {
315 test_msg("Expected an inline, got %llu\n", em->block_start);
316 goto out;
317 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800318
319 if (em->start != offset || em->len != (sectorsize - 5)) {
Josef Bacikaaedb552013-10-11 14:44:09 -0400320 test_msg("Unexpected extent wanted start %llu len 1, got start "
321 "%llu len %llu\n", offset, em->start, em->len);
322 goto out;
323 }
324 if (em->flags != 0) {
325 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
326 goto out;
327 }
328 /*
329 * We don't test anything else for inline since it doesn't get set
330 * unless we have a page for it to write into. Maybe we should change
331 * this?
332 */
333 offset = em->start + em->len;
334 free_extent_map(em);
335
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200336 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400337 if (IS_ERR(em)) {
338 test_msg("Got an error when we shouldn't have\n");
339 goto out;
340 }
341 if (em->block_start != EXTENT_MAP_HOLE) {
342 test_msg("Expected a hole, got %llu\n", em->block_start);
343 goto out;
344 }
345 if (em->start != offset || em->len != 4) {
346 test_msg("Unexpected extent wanted start %llu len 4, got start "
347 "%llu len %llu\n", offset, em->start, em->len);
348 goto out;
349 }
350 if (em->flags != 0) {
351 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
352 goto out;
353 }
354 offset = em->start + em->len;
355 free_extent_map(em);
356
357 /* Regular extent */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200358 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400359 if (IS_ERR(em)) {
360 test_msg("Got an error when we shouldn't have\n");
361 goto out;
362 }
363 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
364 test_msg("Expected a real extent, got %llu\n", em->block_start);
365 goto out;
366 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800367 if (em->start != offset || em->len != sectorsize - 1) {
Josef Bacikaaedb552013-10-11 14:44:09 -0400368 test_msg("Unexpected extent wanted start %llu len 4095, got "
369 "start %llu len %llu\n", offset, em->start, em->len);
370 goto out;
371 }
372 if (em->flags != 0) {
373 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
374 goto out;
375 }
376 if (em->orig_start != em->start) {
377 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
378 em->orig_start);
379 goto out;
380 }
381 offset = em->start + em->len;
382 free_extent_map(em);
383
384 /* The next 3 are split extents */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200385 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400386 if (IS_ERR(em)) {
387 test_msg("Got an error when we shouldn't have\n");
388 goto out;
389 }
390 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
391 test_msg("Expected a real extent, got %llu\n", em->block_start);
392 goto out;
393 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800394 if (em->start != offset || em->len != sectorsize) {
395 test_msg("Unexpected extent start %llu len %u, "
396 "got start %llu len %llu\n",
397 offset, sectorsize, em->start, em->len);
Josef Bacikaaedb552013-10-11 14:44:09 -0400398 goto out;
399 }
400 if (em->flags != 0) {
401 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
402 goto out;
403 }
404 if (em->orig_start != em->start) {
405 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
406 em->orig_start);
407 goto out;
408 }
409 disk_bytenr = em->block_start;
410 orig_start = em->start;
411 offset = em->start + em->len;
412 free_extent_map(em);
413
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200414 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400415 if (IS_ERR(em)) {
416 test_msg("Got an error when we shouldn't have\n");
417 goto out;
418 }
419 if (em->block_start != EXTENT_MAP_HOLE) {
420 test_msg("Expected a hole, got %llu\n", em->block_start);
421 goto out;
422 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800423 if (em->start != offset || em->len != sectorsize) {
424 test_msg("Unexpected extent wanted start %llu len %u, "
425 "got start %llu len %llu\n",
426 offset, sectorsize, em->start, em->len);
Josef Bacikaaedb552013-10-11 14:44:09 -0400427 goto out;
428 }
429 if (em->flags != 0) {
430 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
431 goto out;
432 }
433 offset = em->start + em->len;
434 free_extent_map(em);
435
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200436 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400437 if (IS_ERR(em)) {
438 test_msg("Got an error when we shouldn't have\n");
439 goto out;
440 }
441 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
442 test_msg("Expected a real extent, got %llu\n", em->block_start);
443 goto out;
444 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800445 if (em->start != offset || em->len != 2 * sectorsize) {
446 test_msg("Unexpected extent wanted start %llu len %u, "
447 "got start %llu len %llu\n",
448 offset, 2 * sectorsize, em->start, em->len);
Josef Bacikaaedb552013-10-11 14:44:09 -0400449 goto out;
450 }
451 if (em->flags != 0) {
452 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
453 goto out;
454 }
455 if (em->orig_start != orig_start) {
456 test_msg("Wrong orig offset, want %llu, have %llu\n",
457 orig_start, em->orig_start);
458 goto out;
459 }
460 disk_bytenr += (em->start - orig_start);
461 if (em->block_start != disk_bytenr) {
462 test_msg("Wrong block start, want %llu, have %llu\n",
463 disk_bytenr, em->block_start);
464 goto out;
465 }
466 offset = em->start + em->len;
467 free_extent_map(em);
468
469 /* Prealloc extent */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200470 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400471 if (IS_ERR(em)) {
472 test_msg("Got an error when we shouldn't have\n");
473 goto out;
474 }
475 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
476 test_msg("Expected a real extent, got %llu\n", em->block_start);
477 goto out;
478 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800479 if (em->start != offset || em->len != sectorsize) {
480 test_msg("Unexpected extent wanted start %llu len %u, "
481 "got start %llu len %llu\n",
482 offset, sectorsize, em->start, em->len);
Josef Bacikaaedb552013-10-11 14:44:09 -0400483 goto out;
484 }
485 if (em->flags != prealloc_only) {
486 test_msg("Unexpected flags set, want %lu have %lu\n",
487 prealloc_only, em->flags);
488 goto out;
489 }
490 if (em->orig_start != em->start) {
491 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
492 em->orig_start);
493 goto out;
494 }
495 offset = em->start + em->len;
496 free_extent_map(em);
497
498 /* The next 3 are a half written prealloc extent */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200499 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400500 if (IS_ERR(em)) {
501 test_msg("Got an error when we shouldn't have\n");
502 goto out;
503 }
504 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
505 test_msg("Expected a real extent, got %llu\n", em->block_start);
506 goto out;
507 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800508 if (em->start != offset || em->len != sectorsize) {
509 test_msg("Unexpected extent wanted start %llu len %u, "
510 "got start %llu len %llu\n",
511 offset, sectorsize, em->start, em->len);
Josef Bacikaaedb552013-10-11 14:44:09 -0400512 goto out;
513 }
514 if (em->flags != prealloc_only) {
515 test_msg("Unexpected flags set, want %lu have %lu\n",
516 prealloc_only, em->flags);
517 goto out;
518 }
519 if (em->orig_start != em->start) {
520 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
521 em->orig_start);
522 goto out;
523 }
524 disk_bytenr = em->block_start;
525 orig_start = em->start;
526 offset = em->start + em->len;
527 free_extent_map(em);
528
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200529 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400530 if (IS_ERR(em)) {
531 test_msg("Got an error when we shouldn't have\n");
532 goto out;
533 }
534 if (em->block_start >= EXTENT_MAP_HOLE) {
535 test_msg("Expected a real extent, got %llu\n", em->block_start);
536 goto out;
537 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800538 if (em->start != offset || em->len != sectorsize) {
539 test_msg("Unexpected extent wanted start %llu len %u, "
540 "got start %llu len %llu\n",
541 offset, sectorsize, em->start, em->len);
Josef Bacikaaedb552013-10-11 14:44:09 -0400542 goto out;
543 }
544 if (em->flags != 0) {
545 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
546 goto out;
547 }
548 if (em->orig_start != orig_start) {
549 test_msg("Unexpected orig offset, wanted %llu, have %llu\n",
550 orig_start, em->orig_start);
551 goto out;
552 }
553 if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
554 test_msg("Unexpected block start, wanted %llu, have %llu\n",
555 disk_bytenr + (em->start - em->orig_start),
556 em->block_start);
557 goto out;
558 }
559 offset = em->start + em->len;
560 free_extent_map(em);
561
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200562 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400563 if (IS_ERR(em)) {
564 test_msg("Got an error when we shouldn't have\n");
565 goto out;
566 }
567 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
568 test_msg("Expected a real extent, got %llu\n", em->block_start);
569 goto out;
570 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800571 if (em->start != offset || em->len != 2 * sectorsize) {
572 test_msg("Unexpected extent wanted start %llu len %u, "
573 "got start %llu len %llu\n",
574 offset, 2 * sectorsize, em->start, em->len);
Josef Bacikaaedb552013-10-11 14:44:09 -0400575 goto out;
576 }
577 if (em->flags != prealloc_only) {
578 test_msg("Unexpected flags set, want %lu have %lu\n",
579 prealloc_only, em->flags);
580 goto out;
581 }
582 if (em->orig_start != orig_start) {
583 test_msg("Wrong orig offset, want %llu, have %llu\n", orig_start,
584 em->orig_start);
585 goto out;
586 }
587 if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
588 test_msg("Unexpected block start, wanted %llu, have %llu\n",
589 disk_bytenr + (em->start - em->orig_start),
590 em->block_start);
591 goto out;
592 }
593 offset = em->start + em->len;
594 free_extent_map(em);
595
596 /* Now for the compressed extent */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200597 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400598 if (IS_ERR(em)) {
599 test_msg("Got an error when we shouldn't have\n");
600 goto out;
601 }
602 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
603 test_msg("Expected a real extent, got %llu\n", em->block_start);
604 goto out;
605 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800606 if (em->start != offset || em->len != 2 * sectorsize) {
607 test_msg("Unexpected extent wanted start %llu len %u,"
608 "got start %llu len %llu\n",
609 offset, 2 * sectorsize, em->start, em->len);
Josef Bacikaaedb552013-10-11 14:44:09 -0400610 goto out;
611 }
612 if (em->flags != compressed_only) {
613 test_msg("Unexpected flags set, want %lu have %lu\n",
614 compressed_only, em->flags);
615 goto out;
616 }
617 if (em->orig_start != em->start) {
618 test_msg("Wrong orig offset, want %llu, have %llu\n",
619 em->start, em->orig_start);
620 goto out;
621 }
622 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
623 test_msg("Unexpected compress type, wanted %d, got %d\n",
624 BTRFS_COMPRESS_ZLIB, em->compress_type);
625 goto out;
626 }
627 offset = em->start + em->len;
628 free_extent_map(em);
629
630 /* Split compressed extent */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200631 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400632 if (IS_ERR(em)) {
633 test_msg("Got an error when we shouldn't have\n");
634 goto out;
635 }
636 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
637 test_msg("Expected a real extent, got %llu\n", em->block_start);
638 goto out;
639 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800640 if (em->start != offset || em->len != sectorsize) {
641 test_msg("Unexpected extent wanted start %llu len %u,"
642 "got start %llu len %llu\n",
643 offset, sectorsize, em->start, em->len);
Josef Bacikaaedb552013-10-11 14:44:09 -0400644 goto out;
645 }
646 if (em->flags != compressed_only) {
647 test_msg("Unexpected flags set, want %lu have %lu\n",
648 compressed_only, em->flags);
649 goto out;
650 }
651 if (em->orig_start != em->start) {
652 test_msg("Wrong orig offset, want %llu, have %llu\n",
653 em->start, em->orig_start);
654 goto out;
655 }
656 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
657 test_msg("Unexpected compress type, wanted %d, got %d\n",
658 BTRFS_COMPRESS_ZLIB, em->compress_type);
659 goto out;
660 }
661 disk_bytenr = em->block_start;
662 orig_start = em->start;
663 offset = em->start + em->len;
664 free_extent_map(em);
665
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200666 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400667 if (IS_ERR(em)) {
668 test_msg("Got an error when we shouldn't have\n");
669 goto out;
670 }
671 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
672 test_msg("Expected a real extent, got %llu\n", em->block_start);
673 goto out;
674 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800675 if (em->start != offset || em->len != sectorsize) {
676 test_msg("Unexpected extent wanted start %llu len %u, "
677 "got start %llu len %llu\n",
678 offset, sectorsize, em->start, em->len);
Josef Bacikaaedb552013-10-11 14:44:09 -0400679 goto out;
680 }
681 if (em->flags != 0) {
682 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
683 goto out;
684 }
685 if (em->orig_start != em->start) {
686 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
687 em->orig_start);
688 goto out;
689 }
690 offset = em->start + em->len;
691 free_extent_map(em);
692
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200693 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400694 if (IS_ERR(em)) {
695 test_msg("Got an error when we shouldn't have\n");
696 goto out;
697 }
698 if (em->block_start != disk_bytenr) {
699 test_msg("Block start does not match, want %llu got %llu\n",
700 disk_bytenr, em->block_start);
701 goto out;
702 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800703 if (em->start != offset || em->len != 2 * sectorsize) {
704 test_msg("Unexpected extent wanted start %llu len %u, "
705 "got start %llu len %llu\n",
706 offset, 2 * sectorsize, em->start, em->len);
Josef Bacikaaedb552013-10-11 14:44:09 -0400707 goto out;
708 }
709 if (em->flags != compressed_only) {
710 test_msg("Unexpected flags set, want %lu have %lu\n",
711 compressed_only, em->flags);
712 goto out;
713 }
714 if (em->orig_start != orig_start) {
715 test_msg("Wrong orig offset, want %llu, have %llu\n",
716 em->start, orig_start);
717 goto out;
718 }
719 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
720 test_msg("Unexpected compress type, wanted %d, got %d\n",
721 BTRFS_COMPRESS_ZLIB, em->compress_type);
722 goto out;
723 }
724 offset = em->start + em->len;
725 free_extent_map(em);
726
727 /* A hole between regular extents but no hole extent */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200728 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset + 6,
729 sectorsize, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400730 if (IS_ERR(em)) {
731 test_msg("Got an error when we shouldn't have\n");
732 goto out;
733 }
734 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
735 test_msg("Expected a real extent, got %llu\n", em->block_start);
736 goto out;
737 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800738 if (em->start != offset || em->len != sectorsize) {
739 test_msg("Unexpected extent wanted start %llu len %u, "
740 "got start %llu len %llu\n",
741 offset, sectorsize, em->start, em->len);
Josef Bacikaaedb552013-10-11 14:44:09 -0400742 goto out;
743 }
744 if (em->flags != 0) {
745 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
746 goto out;
747 }
748 if (em->orig_start != em->start) {
749 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
750 em->orig_start);
751 goto out;
752 }
753 offset = em->start + em->len;
754 free_extent_map(em);
755
Nikolay Borisovd4417e22017-10-16 16:48:40 +0300756 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, SZ_4M, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400757 if (IS_ERR(em)) {
758 test_msg("Got an error when we shouldn't have\n");
759 goto out;
760 }
761 if (em->block_start != EXTENT_MAP_HOLE) {
762 test_msg("Expected a hole extent, got %llu\n", em->block_start);
763 goto out;
764 }
765 /*
766 * Currently we just return a length that we requested rather than the
767 * length of the actual hole, if this changes we'll have to change this
768 * test.
769 */
Feifei Xub9ef22d2016-06-01 19:18:25 +0800770 if (em->start != offset || em->len != 3 * sectorsize) {
771 test_msg("Unexpected extent wanted start %llu len %u, "
772 "got start %llu len %llu\n",
773 offset, 3 * sectorsize, em->start, em->len);
Josef Bacikaaedb552013-10-11 14:44:09 -0400774 goto out;
775 }
776 if (em->flags != vacancy_only) {
777 test_msg("Unexpected flags set, want %lu have %lu\n",
778 vacancy_only, em->flags);
779 goto out;
780 }
781 if (em->orig_start != em->start) {
782 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
783 em->orig_start);
784 goto out;
785 }
786 offset = em->start + em->len;
787 free_extent_map(em);
788
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200789 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -0400790 if (IS_ERR(em)) {
791 test_msg("Got an error when we shouldn't have\n");
792 goto out;
793 }
794 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
795 test_msg("Expected a real extent, got %llu\n", em->block_start);
796 goto out;
797 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800798 if (em->start != offset || em->len != sectorsize) {
799 test_msg("Unexpected extent wanted start %llu len %u,"
800 "got start %llu len %llu\n",
801 offset, sectorsize, em->start, em->len);
Josef Bacikaaedb552013-10-11 14:44:09 -0400802 goto out;
803 }
804 if (em->flags != 0) {
805 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
806 goto out;
807 }
808 if (em->orig_start != em->start) {
809 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
810 em->orig_start);
811 goto out;
812 }
813 ret = 0;
814out:
815 if (!IS_ERR(em))
816 free_extent_map(em);
817 iput(inode);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400818 btrfs_free_dummy_root(root);
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400819 btrfs_free_dummy_fs_info(fs_info);
Josef Bacikaaedb552013-10-11 14:44:09 -0400820 return ret;
821}
822
Feifei Xub9ef22d2016-06-01 19:18:25 +0800823static int test_hole_first(u32 sectorsize, u32 nodesize)
Josef Bacik0e30db82013-10-14 13:15:02 -0400824{
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400825 struct btrfs_fs_info *fs_info = NULL;
Josef Bacik0e30db82013-10-14 13:15:02 -0400826 struct inode *inode = NULL;
827 struct btrfs_root *root = NULL;
828 struct extent_map *em = NULL;
829 int ret = -ENOMEM;
830
831 inode = btrfs_new_test_inode();
832 if (!inode) {
833 test_msg("Couldn't allocate inode\n");
834 return ret;
835 }
836
837 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
838 BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
839 BTRFS_I(inode)->location.offset = 0;
840
Jeff Mahoneyda170662016-06-15 09:22:56 -0400841 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400842 if (!fs_info) {
843 test_msg("Couldn't allocate dummy fs info\n");
Josef Bacik0e30db82013-10-14 13:15:02 -0400844 goto out;
845 }
846
Jeff Mahoneyda170662016-06-15 09:22:56 -0400847 root = btrfs_alloc_dummy_root(fs_info);
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400848 if (IS_ERR(root)) {
849 test_msg("Couldn't allocate root\n");
Josef Bacik0e30db82013-10-14 13:15:02 -0400850 goto out;
851 }
852
Jeff Mahoneyda170662016-06-15 09:22:56 -0400853 root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
Josef Bacik0e30db82013-10-14 13:15:02 -0400854 if (!root->node) {
855 test_msg("Couldn't allocate dummy buffer\n");
856 goto out;
857 }
858
859 extent_buffer_get(root->node);
860 btrfs_set_header_nritems(root->node, 0);
861 btrfs_set_header_level(root->node, 0);
862 BTRFS_I(inode)->root = root;
863 ret = -EINVAL;
864
865 /*
866 * Need a blank inode item here just so we don't confuse
867 * btrfs_get_extent.
868 */
869 insert_inode_item_key(root);
Feifei Xub9ef22d2016-06-01 19:18:25 +0800870 insert_extent(root, sectorsize, sectorsize, sectorsize, 0, sectorsize,
871 sectorsize, BTRFS_FILE_EXTENT_REG, 0, 1);
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200872 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, 2 * sectorsize, 0);
Josef Bacik0e30db82013-10-14 13:15:02 -0400873 if (IS_ERR(em)) {
874 test_msg("Got an error when we shouldn't have\n");
875 goto out;
876 }
877 if (em->block_start != EXTENT_MAP_HOLE) {
878 test_msg("Expected a hole, got %llu\n", em->block_start);
879 goto out;
880 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800881 if (em->start != 0 || em->len != sectorsize) {
882 test_msg("Unexpected extent wanted start 0 len %u, "
883 "got start %llu len %llu\n",
884 sectorsize, em->start, em->len);
Josef Bacik0e30db82013-10-14 13:15:02 -0400885 goto out;
886 }
887 if (em->flags != vacancy_only) {
888 test_msg("Wrong flags, wanted %lu, have %lu\n", vacancy_only,
889 em->flags);
890 goto out;
891 }
892 free_extent_map(em);
893
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200894 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, sectorsize,
895 2 * sectorsize, 0);
Josef Bacik0e30db82013-10-14 13:15:02 -0400896 if (IS_ERR(em)) {
897 test_msg("Got an error when we shouldn't have\n");
898 goto out;
899 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800900 if (em->block_start != sectorsize) {
Josef Bacik0e30db82013-10-14 13:15:02 -0400901 test_msg("Expected a real extent, got %llu\n", em->block_start);
902 goto out;
903 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800904 if (em->start != sectorsize || em->len != sectorsize) {
905 test_msg("Unexpected extent wanted start %u len %u, "
906 "got start %llu len %llu\n",
907 sectorsize, sectorsize, em->start, em->len);
Josef Bacik0e30db82013-10-14 13:15:02 -0400908 goto out;
909 }
910 if (em->flags != 0) {
911 test_msg("Unexpected flags set, wanted 0 got %lu\n",
912 em->flags);
913 goto out;
914 }
915 ret = 0;
916out:
917 if (!IS_ERR(em))
918 free_extent_map(em);
919 iput(inode);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400920 btrfs_free_dummy_root(root);
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400921 btrfs_free_dummy_fs_info(fs_info);
Josef Bacik0e30db82013-10-14 13:15:02 -0400922 return ret;
923}
924
Feifei Xub9ef22d2016-06-01 19:18:25 +0800925static int test_extent_accounting(u32 sectorsize, u32 nodesize)
Josef Bacik6a3891c2015-03-16 17:38:52 -0400926{
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400927 struct btrfs_fs_info *fs_info = NULL;
Josef Bacik6a3891c2015-03-16 17:38:52 -0400928 struct inode *inode = NULL;
929 struct btrfs_root *root = NULL;
930 int ret = -ENOMEM;
931
932 inode = btrfs_new_test_inode();
933 if (!inode) {
934 test_msg("Couldn't allocate inode\n");
935 return ret;
936 }
937
Jeff Mahoneyda170662016-06-15 09:22:56 -0400938 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400939 if (!fs_info) {
940 test_msg("Couldn't allocate dummy fs info\n");
Josef Bacik6a3891c2015-03-16 17:38:52 -0400941 goto out;
942 }
943
Jeff Mahoneyda170662016-06-15 09:22:56 -0400944 root = btrfs_alloc_dummy_root(fs_info);
Jeff Mahoney7c0260e2016-06-20 14:14:09 -0400945 if (IS_ERR(root)) {
946 test_msg("Couldn't allocate root\n");
Josef Bacik6a3891c2015-03-16 17:38:52 -0400947 goto out;
948 }
949
950 BTRFS_I(inode)->root = root;
951 btrfs_test_inode_set_ops(inode);
952
953 /* [BTRFS_MAX_EXTENT_SIZE] */
Filipe Mananae3b8a482017-11-04 00:16:59 +0000954 ret = btrfs_set_extent_delalloc(inode, 0, BTRFS_MAX_EXTENT_SIZE - 1, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800955 NULL, 0);
Josef Bacik6a3891c2015-03-16 17:38:52 -0400956 if (ret) {
957 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
958 goto out;
959 }
960 if (BTRFS_I(inode)->outstanding_extents != 1) {
961 ret = -EINVAL;
962 test_msg("Miscount, wanted 1, got %u\n",
963 BTRFS_I(inode)->outstanding_extents);
964 goto out;
965 }
966
Feifei Xub9ef22d2016-06-01 19:18:25 +0800967 /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
Josef Bacik6a3891c2015-03-16 17:38:52 -0400968 ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE,
Feifei Xub9ef22d2016-06-01 19:18:25 +0800969 BTRFS_MAX_EXTENT_SIZE + sectorsize - 1,
Filipe Mananae3b8a482017-11-04 00:16:59 +0000970 0, NULL, 0);
Josef Bacik6a3891c2015-03-16 17:38:52 -0400971 if (ret) {
972 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
973 goto out;
974 }
975 if (BTRFS_I(inode)->outstanding_extents != 2) {
976 ret = -EINVAL;
977 test_msg("Miscount, wanted 2, got %u\n",
978 BTRFS_I(inode)->outstanding_extents);
979 goto out;
980 }
981
Feifei Xub9ef22d2016-06-01 19:18:25 +0800982 /* [BTRFS_MAX_EXTENT_SIZE/2][sectorsize HOLE][the rest] */
Josef Bacik6a3891c2015-03-16 17:38:52 -0400983 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
984 BTRFS_MAX_EXTENT_SIZE >> 1,
Feifei Xub9ef22d2016-06-01 19:18:25 +0800985 (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1,
Josef Bacik6a3891c2015-03-16 17:38:52 -0400986 EXTENT_DELALLOC | EXTENT_DIRTY |
David Sterbaae0f1622017-10-31 16:37:52 +0100987 EXTENT_UPTODATE, 0, 0, NULL);
Josef Bacik6a3891c2015-03-16 17:38:52 -0400988 if (ret) {
989 test_msg("clear_extent_bit returned %d\n", ret);
990 goto out;
991 }
992 if (BTRFS_I(inode)->outstanding_extents != 2) {
993 ret = -EINVAL;
994 test_msg("Miscount, wanted 2, got %u\n",
995 BTRFS_I(inode)->outstanding_extents);
996 goto out;
997 }
998
Feifei Xub9ef22d2016-06-01 19:18:25 +0800999 /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
Josef Bacik6a3891c2015-03-16 17:38:52 -04001000 ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE >> 1,
Feifei Xub9ef22d2016-06-01 19:18:25 +08001001 (BTRFS_MAX_EXTENT_SIZE >> 1)
1002 + sectorsize - 1,
Filipe Mananae3b8a482017-11-04 00:16:59 +00001003 0, NULL, 0);
Josef Bacik6a3891c2015-03-16 17:38:52 -04001004 if (ret) {
1005 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
1006 goto out;
1007 }
1008 if (BTRFS_I(inode)->outstanding_extents != 2) {
1009 ret = -EINVAL;
1010 test_msg("Miscount, wanted 2, got %u\n",
1011 BTRFS_I(inode)->outstanding_extents);
1012 goto out;
1013 }
1014
1015 /*
Feifei Xub9ef22d2016-06-01 19:18:25 +08001016 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize]
Josef Bacik6a3891c2015-03-16 17:38:52 -04001017 */
Feifei Xub9ef22d2016-06-01 19:18:25 +08001018 ret = btrfs_set_extent_delalloc(inode,
1019 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize,
1020 (BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1,
Filipe Mananae3b8a482017-11-04 00:16:59 +00001021 0, NULL, 0);
Josef Bacik6a3891c2015-03-16 17:38:52 -04001022 if (ret) {
1023 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
1024 goto out;
1025 }
1026 if (BTRFS_I(inode)->outstanding_extents != 4) {
1027 ret = -EINVAL;
1028 test_msg("Miscount, wanted 4, got %u\n",
1029 BTRFS_I(inode)->outstanding_extents);
1030 goto out;
1031 }
1032
Feifei Xub9ef22d2016-06-01 19:18:25 +08001033 /*
1034 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize]
1035 */
Feifei Xub9ef22d2016-06-01 19:18:25 +08001036 ret = btrfs_set_extent_delalloc(inode,
1037 BTRFS_MAX_EXTENT_SIZE + sectorsize,
Filipe Mananae3b8a482017-11-04 00:16:59 +00001038 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL, 0);
Josef Bacik6a3891c2015-03-16 17:38:52 -04001039 if (ret) {
1040 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
1041 goto out;
1042 }
1043 if (BTRFS_I(inode)->outstanding_extents != 3) {
1044 ret = -EINVAL;
1045 test_msg("Miscount, wanted 3, got %u\n",
1046 BTRFS_I(inode)->outstanding_extents);
1047 goto out;
1048 }
1049
1050 /* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */
1051 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
Feifei Xub9ef22d2016-06-01 19:18:25 +08001052 BTRFS_MAX_EXTENT_SIZE + sectorsize,
1053 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1,
Josef Bacik6a3891c2015-03-16 17:38:52 -04001054 EXTENT_DIRTY | EXTENT_DELALLOC |
David Sterbaae0f1622017-10-31 16:37:52 +01001055 EXTENT_UPTODATE, 0, 0, NULL);
Josef Bacik6a3891c2015-03-16 17:38:52 -04001056 if (ret) {
1057 test_msg("clear_extent_bit returned %d\n", ret);
1058 goto out;
1059 }
1060 if (BTRFS_I(inode)->outstanding_extents != 4) {
1061 ret = -EINVAL;
1062 test_msg("Miscount, wanted 4, got %u\n",
1063 BTRFS_I(inode)->outstanding_extents);
1064 goto out;
1065 }
1066
1067 /*
1068 * Refill the hole again just for good measure, because I thought it
1069 * might fail and I'd rather satisfy my paranoia at this point.
1070 */
Feifei Xub9ef22d2016-06-01 19:18:25 +08001071 ret = btrfs_set_extent_delalloc(inode,
1072 BTRFS_MAX_EXTENT_SIZE + sectorsize,
Filipe Mananae3b8a482017-11-04 00:16:59 +00001073 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL, 0);
Josef Bacik6a3891c2015-03-16 17:38:52 -04001074 if (ret) {
1075 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
1076 goto out;
1077 }
1078 if (BTRFS_I(inode)->outstanding_extents != 3) {
1079 ret = -EINVAL;
1080 test_msg("Miscount, wanted 3, got %u\n",
1081 BTRFS_I(inode)->outstanding_extents);
1082 goto out;
1083 }
1084
1085 /* Empty */
1086 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1087 EXTENT_DIRTY | EXTENT_DELALLOC |
David Sterbaae0f1622017-10-31 16:37:52 +01001088 EXTENT_UPTODATE, 0, 0, NULL);
Josef Bacik6a3891c2015-03-16 17:38:52 -04001089 if (ret) {
1090 test_msg("clear_extent_bit returned %d\n", ret);
1091 goto out;
1092 }
1093 if (BTRFS_I(inode)->outstanding_extents) {
1094 ret = -EINVAL;
1095 test_msg("Miscount, wanted 0, got %u\n",
1096 BTRFS_I(inode)->outstanding_extents);
1097 goto out;
1098 }
1099 ret = 0;
1100out:
1101 if (ret)
1102 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1103 EXTENT_DIRTY | EXTENT_DELALLOC |
David Sterbaae0f1622017-10-31 16:37:52 +01001104 EXTENT_UPTODATE, 0, 0, NULL);
Josef Bacik6a3891c2015-03-16 17:38:52 -04001105 iput(inode);
1106 btrfs_free_dummy_root(root);
Jeff Mahoney7c0260e2016-06-20 14:14:09 -04001107 btrfs_free_dummy_fs_info(fs_info);
Josef Bacik6a3891c2015-03-16 17:38:52 -04001108 return ret;
1109}
1110
Feifei Xub9ef22d2016-06-01 19:18:25 +08001111int btrfs_test_inodes(u32 sectorsize, u32 nodesize)
Josef Bacikaaedb552013-10-11 14:44:09 -04001112{
Josef Bacik0e30db82013-10-14 13:15:02 -04001113 int ret;
1114
1115 set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
Josef Bacik0e30db82013-10-14 13:15:02 -04001116 set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
1117
Josef Bacikaaedb552013-10-11 14:44:09 -04001118 test_msg("Running btrfs_get_extent tests\n");
Feifei Xub9ef22d2016-06-01 19:18:25 +08001119 ret = test_btrfs_get_extent(sectorsize, nodesize);
Josef Bacik0e30db82013-10-14 13:15:02 -04001120 if (ret)
1121 return ret;
1122 test_msg("Running hole first btrfs_get_extent test\n");
Feifei Xub9ef22d2016-06-01 19:18:25 +08001123 ret = test_hole_first(sectorsize, nodesize);
Josef Bacik6a3891c2015-03-16 17:38:52 -04001124 if (ret)
1125 return ret;
1126 test_msg("Running outstanding_extents tests\n");
Feifei Xub9ef22d2016-06-01 19:18:25 +08001127 return test_extent_accounting(sectorsize, nodesize);
Josef Bacikaaedb552013-10-11 14:44:09 -04001128}