1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2013 Fusion IO. All rights reserved.
6 #include <linux/types.h>
7 #include "btrfs-tests.h"
9 #include "../btrfs_inode.h"
10 #include "../disk-io.h"
11 #include "../extent_io.h"
12 #include "../volumes.h"
13 #include "../compression.h"
15 static 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
)
19 struct btrfs_path path
;
20 struct btrfs_file_extent_item
*fi
;
21 struct extent_buffer
*leaf
= root
->node
;
23 u32 value_len
= sizeof(struct btrfs_file_extent_item
);
25 if (type
== BTRFS_FILE_EXTENT_INLINE
)
27 memset(&path
, 0, sizeof(path
));
32 key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
33 key
.type
= BTRFS_EXTENT_DATA_KEY
;
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);
51 static void insert_inode_item_key(struct btrfs_root
*root
)
53 struct btrfs_path path
;
54 struct extent_buffer
*leaf
= root
->node
;
58 memset(&path
, 0, sizeof(path
));
63 key
.objectid
= BTRFS_INODE_ITEM_KEY
;
64 key
.type
= BTRFS_INODE_ITEM_KEY
;
67 setup_items_for_insert(root
, &path
, &key
, &value_len
, value_len
,
68 value_len
+ sizeof(struct btrfs_item
), 1);
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)
77 * [0 - 5][5 - 6][ 6 - 4096 ][ 4096 - 4100][4100 - 8195][8195 - 12291]
78 * [hole ][inline][hole but no extent][ hole ][ regular ][regular1 split]
80 * [12291 - 16387][16387 - 24579][24579 - 28675][ 28675 - 32771][32771 - 36867 ]
81 * [ hole ][regular1 split][ prealloc ][ prealloc1 ][prealloc1 written]
83 * [36867 - 45059][45059 - 53251][53251 - 57347][57347 - 61443][61443- 69635]
84 * [ prealloc1 ][ compressed ][ compressed1 ][ regular ][ compressed1]
86 * [69635-73731][ 73731 - 86019 ][86019-90115]
87 * [ regular ][ hole but no extent][ regular ]
89 static void setup_file_extents(struct btrfs_root
*root
, u32 sectorsize
)
92 u64 disk_bytenr
= SZ_1M
;
95 /* First we want a hole */
96 insert_extent(root
, offset
, 5, 5, 0, 0, 0, BTRFS_FILE_EXTENT_REG
, 0,
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.
107 insert_extent(root
, offset
, 1, 1, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE
, 0,
112 /* Now another hole */
113 insert_extent(root
, offset
, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG
, 0,
118 /* Now for a regular extent */
119 insert_extent(root
, offset
, sectorsize
- 1, sectorsize
- 1, 0,
120 disk_bytenr
, sectorsize
, BTRFS_FILE_EXTENT_REG
, 0, slot
);
122 disk_bytenr
+= sectorsize
;
123 offset
+= sectorsize
- 1;
126 * Now for 3 extents that were split from a hole punch so we test
129 insert_extent(root
, offset
, sectorsize
, 4 * sectorsize
, 0, disk_bytenr
,
130 4 * sectorsize
, BTRFS_FILE_EXTENT_REG
, 0, slot
);
132 offset
+= sectorsize
;
133 insert_extent(root
, offset
, sectorsize
, sectorsize
, 0, 0, 0,
134 BTRFS_FILE_EXTENT_REG
, 0, slot
);
136 offset
+= sectorsize
;
137 insert_extent(root
, offset
, 2 * sectorsize
, 4 * sectorsize
,
138 2 * sectorsize
, disk_bytenr
, 4 * sectorsize
,
139 BTRFS_FILE_EXTENT_REG
, 0, slot
);
141 offset
+= 2 * sectorsize
;
142 disk_bytenr
+= 4 * sectorsize
;
144 /* Now for a unwritten prealloc extent */
145 insert_extent(root
, offset
, sectorsize
, sectorsize
, 0, disk_bytenr
,
146 sectorsize
, BTRFS_FILE_EXTENT_PREALLOC
, 0, slot
);
148 offset
+= sectorsize
;
151 * We want to jack up disk_bytenr a little more so the em stuff doesn't
154 disk_bytenr
+= 2 * sectorsize
;
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.
161 insert_extent(root
, offset
, sectorsize
, 4 * sectorsize
, 0, disk_bytenr
,
162 4 * sectorsize
, BTRFS_FILE_EXTENT_PREALLOC
, 0, slot
);
164 offset
+= sectorsize
;
165 insert_extent(root
, offset
, sectorsize
, 4 * sectorsize
, sectorsize
,
166 disk_bytenr
, 4 * sectorsize
, BTRFS_FILE_EXTENT_REG
, 0,
169 offset
+= sectorsize
;
170 insert_extent(root
, offset
, 2 * sectorsize
, 4 * sectorsize
,
171 2 * sectorsize
, disk_bytenr
, 4 * sectorsize
,
172 BTRFS_FILE_EXTENT_PREALLOC
, 0, slot
);
174 offset
+= 2 * sectorsize
;
175 disk_bytenr
+= 4 * sectorsize
;
177 /* Now a normal compressed extent */
178 insert_extent(root
, offset
, 2 * sectorsize
, 2 * sectorsize
, 0,
179 disk_bytenr
, sectorsize
, BTRFS_FILE_EXTENT_REG
,
180 BTRFS_COMPRESS_ZLIB
, slot
);
182 offset
+= 2 * sectorsize
;
184 disk_bytenr
+= 2 * sectorsize
;
186 /* Now a split compressed extent */
187 insert_extent(root
, offset
, sectorsize
, 4 * sectorsize
, 0, disk_bytenr
,
188 sectorsize
, BTRFS_FILE_EXTENT_REG
,
189 BTRFS_COMPRESS_ZLIB
, slot
);
191 offset
+= sectorsize
;
192 insert_extent(root
, offset
, sectorsize
, sectorsize
, 0,
193 disk_bytenr
+ sectorsize
, sectorsize
,
194 BTRFS_FILE_EXTENT_REG
, 0, slot
);
196 offset
+= sectorsize
;
197 insert_extent(root
, offset
, 2 * sectorsize
, 4 * sectorsize
,
198 2 * sectorsize
, disk_bytenr
, sectorsize
,
199 BTRFS_FILE_EXTENT_REG
, BTRFS_COMPRESS_ZLIB
, slot
);
201 offset
+= 2 * sectorsize
;
202 disk_bytenr
+= 2 * sectorsize
;
204 /* Now extents that have a hole but no hole extent */
205 insert_extent(root
, offset
, sectorsize
, sectorsize
, 0, disk_bytenr
,
206 sectorsize
, BTRFS_FILE_EXTENT_REG
, 0, slot
);
208 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
);
214 static unsigned long prealloc_only
= 0;
215 static unsigned long compressed_only
= 0;
216 static unsigned long vacancy_only
= 0;
218 static noinline
int test_btrfs_get_extent(u32 sectorsize
, u32 nodesize
)
220 struct btrfs_fs_info
*fs_info
= NULL
;
221 struct inode
*inode
= NULL
;
222 struct btrfs_root
*root
= NULL
;
223 struct extent_map
*em
= NULL
;
229 test_msg("running btrfs_get_extent tests");
231 inode
= btrfs_new_test_inode();
233 test_std_err(TEST_ALLOC_INODE
);
237 inode
->i_mode
= S_IFREG
;
238 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
239 BTRFS_I(inode
)->location
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
240 BTRFS_I(inode
)->location
.offset
= 0;
242 fs_info
= btrfs_alloc_dummy_fs_info(nodesize
, sectorsize
);
244 test_std_err(TEST_ALLOC_FS_INFO
);
248 root
= btrfs_alloc_dummy_root(fs_info
);
250 test_std_err(TEST_ALLOC_ROOT
);
254 root
->node
= alloc_dummy_extent_buffer(fs_info
, nodesize
);
256 test_std_err(TEST_ALLOC_ROOT
);
260 btrfs_set_header_nritems(root
->node
, 0);
261 btrfs_set_header_level(root
->node
, 0);
264 /* First with no extents */
265 BTRFS_I(inode
)->root
= root
;
266 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, 0, sectorsize
);
269 test_err("got an error when we shouldn't have");
272 if (em
->block_start
!= EXTENT_MAP_HOLE
) {
273 test_err("expected a hole, got %llu", em
->block_start
);
277 btrfs_drop_extent_cache(BTRFS_I(inode
), 0, (u64
)-1, 0);
280 * All of the magic numbers are based on the mapping setup in
281 * setup_file_extents, so if you change anything there you need to
282 * update the comment and update the expected values below.
284 setup_file_extents(root
, sectorsize
);
286 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, 0, (u64
)-1);
288 test_err("got an error when we shouldn't have");
291 if (em
->block_start
!= EXTENT_MAP_HOLE
) {
292 test_err("expected a hole, got %llu", em
->block_start
);
295 if (em
->start
!= 0 || em
->len
!= 5) {
297 "unexpected extent wanted start 0 len 5, got start %llu len %llu",
301 if (em
->flags
!= 0) {
302 test_err("unexpected flags set, want 0 have %lu", em
->flags
);
305 offset
= em
->start
+ em
->len
;
308 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, offset
, sectorsize
);
310 test_err("got an error when we shouldn't have");
313 if (em
->block_start
!= EXTENT_MAP_INLINE
) {
314 test_err("expected an inline, got %llu", em
->block_start
);
318 if (em
->start
!= offset
|| em
->len
!= (sectorsize
- 5)) {
320 "unexpected extent wanted start %llu len 1, got start %llu len %llu",
321 offset
, em
->start
, em
->len
);
324 if (em
->flags
!= 0) {
325 test_err("unexpected flags set, want 0 have %lu", em
->flags
);
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
333 offset
= em
->start
+ em
->len
;
336 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, offset
, sectorsize
);
338 test_err("got an error when we shouldn't have");
341 if (em
->block_start
!= EXTENT_MAP_HOLE
) {
342 test_err("expected a hole, got %llu", em
->block_start
);
345 if (em
->start
!= offset
|| em
->len
!= 4) {
347 "unexpected extent wanted start %llu len 4, got start %llu len %llu",
348 offset
, em
->start
, em
->len
);
351 if (em
->flags
!= 0) {
352 test_err("unexpected flags set, want 0 have %lu", em
->flags
);
355 offset
= em
->start
+ em
->len
;
359 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, offset
, sectorsize
);
361 test_err("got an error when we shouldn't have");
364 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
365 test_err("expected a real extent, got %llu", em
->block_start
);
368 if (em
->start
!= offset
|| em
->len
!= sectorsize
- 1) {
370 "unexpected extent wanted start %llu len 4095, got start %llu len %llu",
371 offset
, em
->start
, em
->len
);
374 if (em
->flags
!= 0) {
375 test_err("unexpected flags set, want 0 have %lu", em
->flags
);
378 if (em
->orig_start
!= em
->start
) {
379 test_err("wrong orig offset, want %llu, have %llu", em
->start
,
383 offset
= em
->start
+ em
->len
;
386 /* The next 3 are split extents */
387 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, offset
, sectorsize
);
389 test_err("got an error when we shouldn't have");
392 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
393 test_err("expected a real extent, got %llu", em
->block_start
);
396 if (em
->start
!= offset
|| em
->len
!= sectorsize
) {
398 "unexpected extent start %llu len %u, got start %llu len %llu",
399 offset
, sectorsize
, em
->start
, em
->len
);
402 if (em
->flags
!= 0) {
403 test_err("unexpected flags set, want 0 have %lu", em
->flags
);
406 if (em
->orig_start
!= em
->start
) {
407 test_err("wrong orig offset, want %llu, have %llu", em
->start
,
411 disk_bytenr
= em
->block_start
;
412 orig_start
= em
->start
;
413 offset
= em
->start
+ em
->len
;
416 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, offset
, sectorsize
);
418 test_err("got an error when we shouldn't have");
421 if (em
->block_start
!= EXTENT_MAP_HOLE
) {
422 test_err("expected a hole, got %llu", em
->block_start
);
425 if (em
->start
!= offset
|| em
->len
!= sectorsize
) {
427 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
428 offset
, sectorsize
, em
->start
, em
->len
);
431 if (em
->flags
!= 0) {
432 test_err("unexpected flags set, want 0 have %lu", em
->flags
);
435 offset
= em
->start
+ em
->len
;
438 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, offset
, sectorsize
);
440 test_err("got an error when we shouldn't have");
443 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
444 test_err("expected a real extent, got %llu", em
->block_start
);
447 if (em
->start
!= offset
|| em
->len
!= 2 * sectorsize
) {
449 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
450 offset
, 2 * sectorsize
, em
->start
, em
->len
);
453 if (em
->flags
!= 0) {
454 test_err("unexpected flags set, want 0 have %lu", em
->flags
);
457 if (em
->orig_start
!= orig_start
) {
458 test_err("wrong orig offset, want %llu, have %llu",
459 orig_start
, em
->orig_start
);
462 disk_bytenr
+= (em
->start
- orig_start
);
463 if (em
->block_start
!= disk_bytenr
) {
464 test_err("wrong block start, want %llu, have %llu",
465 disk_bytenr
, em
->block_start
);
468 offset
= em
->start
+ em
->len
;
471 /* Prealloc extent */
472 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, offset
, sectorsize
);
474 test_err("got an error when we shouldn't have");
477 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
478 test_err("expected a real extent, got %llu", em
->block_start
);
481 if (em
->start
!= offset
|| em
->len
!= sectorsize
) {
483 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
484 offset
, sectorsize
, em
->start
, em
->len
);
487 if (em
->flags
!= prealloc_only
) {
488 test_err("unexpected flags set, want %lu have %lu",
489 prealloc_only
, em
->flags
);
492 if (em
->orig_start
!= em
->start
) {
493 test_err("wrong orig offset, want %llu, have %llu", em
->start
,
497 offset
= em
->start
+ em
->len
;
500 /* The next 3 are a half written prealloc extent */
501 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, offset
, sectorsize
);
503 test_err("got an error when we shouldn't have");
506 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
507 test_err("expected a real extent, got %llu", em
->block_start
);
510 if (em
->start
!= offset
|| em
->len
!= sectorsize
) {
512 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
513 offset
, sectorsize
, em
->start
, em
->len
);
516 if (em
->flags
!= prealloc_only
) {
517 test_err("unexpected flags set, want %lu have %lu",
518 prealloc_only
, em
->flags
);
521 if (em
->orig_start
!= em
->start
) {
522 test_err("wrong orig offset, want %llu, have %llu", em
->start
,
526 disk_bytenr
= em
->block_start
;
527 orig_start
= em
->start
;
528 offset
= em
->start
+ em
->len
;
531 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, offset
, sectorsize
);
533 test_err("got an error when we shouldn't have");
536 if (em
->block_start
>= EXTENT_MAP_HOLE
) {
537 test_err("expected a real extent, got %llu", em
->block_start
);
540 if (em
->start
!= offset
|| em
->len
!= sectorsize
) {
542 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
543 offset
, sectorsize
, em
->start
, em
->len
);
546 if (em
->flags
!= 0) {
547 test_err("unexpected flags set, want 0 have %lu", em
->flags
);
550 if (em
->orig_start
!= orig_start
) {
551 test_err("unexpected orig offset, wanted %llu, have %llu",
552 orig_start
, em
->orig_start
);
555 if (em
->block_start
!= (disk_bytenr
+ (em
->start
- em
->orig_start
))) {
556 test_err("unexpected block start, wanted %llu, have %llu",
557 disk_bytenr
+ (em
->start
- em
->orig_start
),
561 offset
= em
->start
+ em
->len
;
564 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, offset
, sectorsize
);
566 test_err("got an error when we shouldn't have");
569 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
570 test_err("expected a real extent, got %llu", em
->block_start
);
573 if (em
->start
!= offset
|| em
->len
!= 2 * sectorsize
) {
575 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
576 offset
, 2 * sectorsize
, em
->start
, em
->len
);
579 if (em
->flags
!= prealloc_only
) {
580 test_err("unexpected flags set, want %lu have %lu",
581 prealloc_only
, em
->flags
);
584 if (em
->orig_start
!= orig_start
) {
585 test_err("wrong orig offset, want %llu, have %llu", orig_start
,
589 if (em
->block_start
!= (disk_bytenr
+ (em
->start
- em
->orig_start
))) {
590 test_err("unexpected block start, wanted %llu, have %llu",
591 disk_bytenr
+ (em
->start
- em
->orig_start
),
595 offset
= em
->start
+ em
->len
;
598 /* Now for the compressed extent */
599 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, offset
, sectorsize
);
601 test_err("got an error when we shouldn't have");
604 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
605 test_err("expected a real extent, got %llu", em
->block_start
);
608 if (em
->start
!= offset
|| em
->len
!= 2 * sectorsize
) {
610 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
611 offset
, 2 * sectorsize
, em
->start
, em
->len
);
614 if (em
->flags
!= compressed_only
) {
615 test_err("unexpected flags set, want %lu have %lu",
616 compressed_only
, em
->flags
);
619 if (em
->orig_start
!= em
->start
) {
620 test_err("wrong orig offset, want %llu, have %llu",
621 em
->start
, em
->orig_start
);
624 if (em
->compress_type
!= BTRFS_COMPRESS_ZLIB
) {
625 test_err("unexpected compress type, wanted %d, got %d",
626 BTRFS_COMPRESS_ZLIB
, em
->compress_type
);
629 offset
= em
->start
+ em
->len
;
632 /* Split compressed extent */
633 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, offset
, sectorsize
);
635 test_err("got an error when we shouldn't have");
638 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
639 test_err("expected a real extent, got %llu", em
->block_start
);
642 if (em
->start
!= offset
|| em
->len
!= sectorsize
) {
644 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
645 offset
, sectorsize
, em
->start
, em
->len
);
648 if (em
->flags
!= compressed_only
) {
649 test_err("unexpected flags set, want %lu have %lu",
650 compressed_only
, em
->flags
);
653 if (em
->orig_start
!= em
->start
) {
654 test_err("wrong orig offset, want %llu, have %llu",
655 em
->start
, em
->orig_start
);
658 if (em
->compress_type
!= BTRFS_COMPRESS_ZLIB
) {
659 test_err("unexpected compress type, wanted %d, got %d",
660 BTRFS_COMPRESS_ZLIB
, em
->compress_type
);
663 disk_bytenr
= em
->block_start
;
664 orig_start
= em
->start
;
665 offset
= em
->start
+ em
->len
;
668 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, offset
, sectorsize
);
670 test_err("got an error when we shouldn't have");
673 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
674 test_err("expected a real extent, got %llu", em
->block_start
);
677 if (em
->start
!= offset
|| em
->len
!= sectorsize
) {
679 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
680 offset
, sectorsize
, em
->start
, em
->len
);
683 if (em
->flags
!= 0) {
684 test_err("unexpected flags set, want 0 have %lu", em
->flags
);
687 if (em
->orig_start
!= em
->start
) {
688 test_err("wrong orig offset, want %llu, have %llu", em
->start
,
692 offset
= em
->start
+ em
->len
;
695 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, offset
, sectorsize
);
697 test_err("got an error when we shouldn't have");
700 if (em
->block_start
!= disk_bytenr
) {
701 test_err("block start does not match, want %llu got %llu",
702 disk_bytenr
, em
->block_start
);
705 if (em
->start
!= offset
|| em
->len
!= 2 * sectorsize
) {
707 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
708 offset
, 2 * sectorsize
, em
->start
, em
->len
);
711 if (em
->flags
!= compressed_only
) {
712 test_err("unexpected flags set, want %lu have %lu",
713 compressed_only
, em
->flags
);
716 if (em
->orig_start
!= orig_start
) {
717 test_err("wrong orig offset, want %llu, have %llu",
718 em
->start
, orig_start
);
721 if (em
->compress_type
!= BTRFS_COMPRESS_ZLIB
) {
722 test_err("unexpected compress type, wanted %d, got %d",
723 BTRFS_COMPRESS_ZLIB
, em
->compress_type
);
726 offset
= em
->start
+ em
->len
;
729 /* A hole between regular extents but no hole extent */
730 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, offset
+ 6, sectorsize
);
732 test_err("got an error when we shouldn't have");
735 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
736 test_err("expected a real extent, got %llu", em
->block_start
);
739 if (em
->start
!= offset
|| em
->len
!= sectorsize
) {
741 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
742 offset
, sectorsize
, em
->start
, em
->len
);
745 if (em
->flags
!= 0) {
746 test_err("unexpected flags set, want 0 have %lu", em
->flags
);
749 if (em
->orig_start
!= em
->start
) {
750 test_err("wrong orig offset, want %llu, have %llu", em
->start
,
754 offset
= em
->start
+ em
->len
;
757 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, offset
, SZ_4M
);
759 test_err("got an error when we shouldn't have");
762 if (em
->block_start
!= EXTENT_MAP_HOLE
) {
763 test_err("expected a hole extent, got %llu", em
->block_start
);
767 * Currently we just return a length that we requested rather than the
768 * length of the actual hole, if this changes we'll have to change this
771 if (em
->start
!= offset
|| em
->len
!= 3 * sectorsize
) {
773 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
774 offset
, 3 * sectorsize
, em
->start
, em
->len
);
777 if (em
->flags
!= vacancy_only
) {
778 test_err("unexpected flags set, want %lu have %lu",
779 vacancy_only
, em
->flags
);
782 if (em
->orig_start
!= em
->start
) {
783 test_err("wrong orig offset, want %llu, have %llu", em
->start
,
787 offset
= em
->start
+ em
->len
;
790 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, offset
, sectorsize
);
792 test_err("got an error when we shouldn't have");
795 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
796 test_err("expected a real extent, got %llu", em
->block_start
);
799 if (em
->start
!= offset
|| em
->len
!= sectorsize
) {
801 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
802 offset
, sectorsize
, em
->start
, em
->len
);
805 if (em
->flags
!= 0) {
806 test_err("unexpected flags set, want 0 have %lu", em
->flags
);
809 if (em
->orig_start
!= em
->start
) {
810 test_err("wrong orig offset, want %llu, have %llu", em
->start
,
819 btrfs_free_dummy_root(root
);
820 btrfs_free_dummy_fs_info(fs_info
);
824 static int test_hole_first(u32 sectorsize
, u32 nodesize
)
826 struct btrfs_fs_info
*fs_info
= NULL
;
827 struct inode
*inode
= NULL
;
828 struct btrfs_root
*root
= NULL
;
829 struct extent_map
*em
= NULL
;
832 test_msg("running hole first btrfs_get_extent test");
834 inode
= btrfs_new_test_inode();
836 test_std_err(TEST_ALLOC_INODE
);
840 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
841 BTRFS_I(inode
)->location
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
842 BTRFS_I(inode
)->location
.offset
= 0;
844 fs_info
= btrfs_alloc_dummy_fs_info(nodesize
, sectorsize
);
846 test_std_err(TEST_ALLOC_FS_INFO
);
850 root
= btrfs_alloc_dummy_root(fs_info
);
852 test_std_err(TEST_ALLOC_ROOT
);
856 root
->node
= alloc_dummy_extent_buffer(fs_info
, nodesize
);
858 test_std_err(TEST_ALLOC_ROOT
);
862 btrfs_set_header_nritems(root
->node
, 0);
863 btrfs_set_header_level(root
->node
, 0);
864 BTRFS_I(inode
)->root
= root
;
868 * Need a blank inode item here just so we don't confuse
871 insert_inode_item_key(root
);
872 insert_extent(root
, sectorsize
, sectorsize
, sectorsize
, 0, sectorsize
,
873 sectorsize
, BTRFS_FILE_EXTENT_REG
, 0, 1);
874 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, 0, 2 * sectorsize
);
876 test_err("got an error when we shouldn't have");
879 if (em
->block_start
!= EXTENT_MAP_HOLE
) {
880 test_err("expected a hole, got %llu", em
->block_start
);
883 if (em
->start
!= 0 || em
->len
!= sectorsize
) {
885 "unexpected extent wanted start 0 len %u, got start %llu len %llu",
886 sectorsize
, em
->start
, em
->len
);
889 if (em
->flags
!= vacancy_only
) {
890 test_err("wrong flags, wanted %lu, have %lu", vacancy_only
,
896 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, sectorsize
, 2 * sectorsize
);
898 test_err("got an error when we shouldn't have");
901 if (em
->block_start
!= sectorsize
) {
902 test_err("expected a real extent, got %llu", em
->block_start
);
905 if (em
->start
!= sectorsize
|| em
->len
!= sectorsize
) {
907 "unexpected extent wanted start %u len %u, got start %llu len %llu",
908 sectorsize
, sectorsize
, em
->start
, em
->len
);
911 if (em
->flags
!= 0) {
912 test_err("unexpected flags set, wanted 0 got %lu",
921 btrfs_free_dummy_root(root
);
922 btrfs_free_dummy_fs_info(fs_info
);
926 static int test_extent_accounting(u32 sectorsize
, u32 nodesize
)
928 struct btrfs_fs_info
*fs_info
= NULL
;
929 struct inode
*inode
= NULL
;
930 struct btrfs_root
*root
= NULL
;
933 test_msg("running outstanding_extents tests");
935 inode
= btrfs_new_test_inode();
937 test_std_err(TEST_ALLOC_INODE
);
941 fs_info
= btrfs_alloc_dummy_fs_info(nodesize
, sectorsize
);
943 test_std_err(TEST_ALLOC_FS_INFO
);
947 root
= btrfs_alloc_dummy_root(fs_info
);
949 test_std_err(TEST_ALLOC_ROOT
);
953 BTRFS_I(inode
)->root
= root
;
954 btrfs_test_inode_set_ops(inode
);
956 /* [BTRFS_MAX_EXTENT_SIZE] */
957 ret
= btrfs_set_extent_delalloc(inode
, 0, BTRFS_MAX_EXTENT_SIZE
- 1, 0,
960 test_err("btrfs_set_extent_delalloc returned %d", ret
);
963 if (BTRFS_I(inode
)->outstanding_extents
!= 1) {
965 test_err("miscount, wanted 1, got %u",
966 BTRFS_I(inode
)->outstanding_extents
);
970 /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
971 ret
= btrfs_set_extent_delalloc(inode
, BTRFS_MAX_EXTENT_SIZE
,
972 BTRFS_MAX_EXTENT_SIZE
+ sectorsize
- 1,
975 test_err("btrfs_set_extent_delalloc returned %d", ret
);
978 if (BTRFS_I(inode
)->outstanding_extents
!= 2) {
980 test_err("miscount, wanted 2, got %u",
981 BTRFS_I(inode
)->outstanding_extents
);
985 /* [BTRFS_MAX_EXTENT_SIZE/2][sectorsize HOLE][the rest] */
986 ret
= clear_extent_bit(&BTRFS_I(inode
)->io_tree
,
987 BTRFS_MAX_EXTENT_SIZE
>> 1,
988 (BTRFS_MAX_EXTENT_SIZE
>> 1) + sectorsize
- 1,
989 EXTENT_DELALLOC
| EXTENT_UPTODATE
, 0, 0, NULL
);
991 test_err("clear_extent_bit returned %d", ret
);
994 if (BTRFS_I(inode
)->outstanding_extents
!= 2) {
996 test_err("miscount, wanted 2, got %u",
997 BTRFS_I(inode
)->outstanding_extents
);
1001 /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
1002 ret
= btrfs_set_extent_delalloc(inode
, BTRFS_MAX_EXTENT_SIZE
>> 1,
1003 (BTRFS_MAX_EXTENT_SIZE
>> 1)
1007 test_err("btrfs_set_extent_delalloc returned %d", ret
);
1010 if (BTRFS_I(inode
)->outstanding_extents
!= 2) {
1012 test_err("miscount, wanted 2, got %u",
1013 BTRFS_I(inode
)->outstanding_extents
);
1018 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize]
1020 ret
= btrfs_set_extent_delalloc(inode
,
1021 BTRFS_MAX_EXTENT_SIZE
+ 2 * sectorsize
,
1022 (BTRFS_MAX_EXTENT_SIZE
<< 1) + 3 * sectorsize
- 1,
1025 test_err("btrfs_set_extent_delalloc returned %d", ret
);
1028 if (BTRFS_I(inode
)->outstanding_extents
!= 4) {
1030 test_err("miscount, wanted 4, got %u",
1031 BTRFS_I(inode
)->outstanding_extents
);
1036 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize]
1038 ret
= btrfs_set_extent_delalloc(inode
,
1039 BTRFS_MAX_EXTENT_SIZE
+ sectorsize
,
1040 BTRFS_MAX_EXTENT_SIZE
+ 2 * sectorsize
- 1, 0, NULL
);
1042 test_err("btrfs_set_extent_delalloc returned %d", ret
);
1045 if (BTRFS_I(inode
)->outstanding_extents
!= 3) {
1047 test_err("miscount, wanted 3, got %u",
1048 BTRFS_I(inode
)->outstanding_extents
);
1052 /* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */
1053 ret
= clear_extent_bit(&BTRFS_I(inode
)->io_tree
,
1054 BTRFS_MAX_EXTENT_SIZE
+ sectorsize
,
1055 BTRFS_MAX_EXTENT_SIZE
+ 2 * sectorsize
- 1,
1056 EXTENT_DELALLOC
| EXTENT_UPTODATE
, 0, 0, NULL
);
1058 test_err("clear_extent_bit returned %d", ret
);
1061 if (BTRFS_I(inode
)->outstanding_extents
!= 4) {
1063 test_err("miscount, wanted 4, got %u",
1064 BTRFS_I(inode
)->outstanding_extents
);
1069 * Refill the hole again just for good measure, because I thought it
1070 * might fail and I'd rather satisfy my paranoia at this point.
1072 ret
= btrfs_set_extent_delalloc(inode
,
1073 BTRFS_MAX_EXTENT_SIZE
+ sectorsize
,
1074 BTRFS_MAX_EXTENT_SIZE
+ 2 * sectorsize
- 1, 0, NULL
);
1076 test_err("btrfs_set_extent_delalloc returned %d", ret
);
1079 if (BTRFS_I(inode
)->outstanding_extents
!= 3) {
1081 test_err("miscount, wanted 3, got %u",
1082 BTRFS_I(inode
)->outstanding_extents
);
1087 ret
= clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, (u64
)-1,
1088 EXTENT_DELALLOC
| EXTENT_UPTODATE
, 0, 0, NULL
);
1090 test_err("clear_extent_bit returned %d", ret
);
1093 if (BTRFS_I(inode
)->outstanding_extents
) {
1095 test_err("miscount, wanted 0, got %u",
1096 BTRFS_I(inode
)->outstanding_extents
);
1102 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, (u64
)-1,
1103 EXTENT_DELALLOC
| EXTENT_UPTODATE
, 0, 0, NULL
);
1105 btrfs_free_dummy_root(root
);
1106 btrfs_free_dummy_fs_info(fs_info
);
1110 int btrfs_test_inodes(u32 sectorsize
, u32 nodesize
)
1114 test_msg("running inode tests");
1116 set_bit(EXTENT_FLAG_COMPRESSED
, &compressed_only
);
1117 set_bit(EXTENT_FLAG_PREALLOC
, &prealloc_only
);
1119 ret
= test_btrfs_get_extent(sectorsize
, nodesize
);
1122 ret
= test_hole_first(sectorsize
, nodesize
);
1125 return test_extent_accounting(sectorsize
, nodesize
);