2 * Copyright (C) 2013 Fusion IO. All rights reserved.
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.
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.
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.
19 #include "btrfs-tests.h"
21 #include "../btrfs_inode.h"
22 #include "../disk-io.h"
23 #include "../extent_io.h"
24 #include "../volumes.h"
26 static void insert_extent(struct btrfs_root
*root
, u64 start
, u64 len
,
27 u64 ram_bytes
, u64 offset
, u64 disk_bytenr
,
28 u64 disk_len
, u32 type
, u8 compression
, int slot
)
30 struct btrfs_path path
;
31 struct btrfs_file_extent_item
*fi
;
32 struct extent_buffer
*leaf
= root
->node
;
34 u32 value_len
= sizeof(struct btrfs_file_extent_item
);
36 if (type
== BTRFS_FILE_EXTENT_INLINE
)
38 memset(&path
, 0, sizeof(path
));
43 key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
44 key
.type
= BTRFS_EXTENT_DATA_KEY
;
47 setup_items_for_insert(root
, &path
, &key
, &value_len
, value_len
,
48 value_len
+ sizeof(struct btrfs_item
), 1);
49 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
50 btrfs_set_file_extent_generation(leaf
, fi
, 1);
51 btrfs_set_file_extent_type(leaf
, fi
, type
);
52 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
53 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_len
);
54 btrfs_set_file_extent_offset(leaf
, fi
, offset
);
55 btrfs_set_file_extent_num_bytes(leaf
, fi
, len
);
56 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
57 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
58 btrfs_set_file_extent_encryption(leaf
, fi
, 0);
59 btrfs_set_file_extent_other_encoding(leaf
, fi
, 0);
62 static void insert_inode_item_key(struct btrfs_root
*root
)
64 struct btrfs_path path
;
65 struct extent_buffer
*leaf
= root
->node
;
69 memset(&path
, 0, sizeof(path
));
74 key
.objectid
= BTRFS_INODE_ITEM_KEY
;
75 key
.type
= BTRFS_INODE_ITEM_KEY
;
78 setup_items_for_insert(root
, &path
, &key
, &value_len
, value_len
,
79 value_len
+ sizeof(struct btrfs_item
), 1);
83 * Build the most complicated map of extents the earth has ever seen. We want
84 * this so we can test all of the corner cases of btrfs_get_extent. Here is a
85 * diagram of how the extents will look though this may not be possible we still
86 * want to make sure everything acts normally (the last number is not inclusive)
88 * [0 - 5][5 - 6][6 - 10][10 - 4096][ 4096 - 8192 ][8192 - 12288]
89 * [hole ][inline][ hole ][ regular ][regular1 split][ hole ]
91 * [ 12288 - 20480][20480 - 24576][ 24576 - 28672 ][28672 - 36864][36864 - 45056]
92 * [regular1 split][ prealloc1 ][prealloc1 written][ prealloc1 ][ compressed ]
94 * [45056 - 49152][49152-53248][53248-61440][61440-65536][ 65536+81920 ]
95 * [ compressed1 ][ regular ][compressed1][ regular ][ hole but no extent]
100 static void setup_file_extents(struct btrfs_root
*root
)
103 u64 disk_bytenr
= 1 * 1024 * 1024;
106 /* First we want a hole */
107 insert_extent(root
, offset
, 5, 5, 0, 0, 0, BTRFS_FILE_EXTENT_REG
, 0,
113 * Now we want an inline extent, I don't think this is possible but hey
114 * why not? Also keep in mind if we have an inline extent it counts as
115 * the whole first page. If we were to expand it we would have to cow
116 * and we wouldn't have an inline extent anymore.
118 insert_extent(root
, offset
, 1, 1, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE
, 0,
123 /* Now another hole */
124 insert_extent(root
, offset
, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG
, 0,
129 /* Now for a regular extent */
130 insert_extent(root
, offset
, 4095, 4095, 0, disk_bytenr
, 4096,
131 BTRFS_FILE_EXTENT_REG
, 0, slot
);
137 * Now for 3 extents that were split from a hole punch so we test
140 insert_extent(root
, offset
, 4096, 16384, 0, disk_bytenr
, 16384,
141 BTRFS_FILE_EXTENT_REG
, 0, slot
);
144 insert_extent(root
, offset
, 4096, 4096, 0, 0, 0, BTRFS_FILE_EXTENT_REG
,
148 insert_extent(root
, offset
, 8192, 16384, 8192, disk_bytenr
, 16384,
149 BTRFS_FILE_EXTENT_REG
, 0, slot
);
152 disk_bytenr
+= 16384;
154 /* Now for a unwritten prealloc extent */
155 insert_extent(root
, offset
, 4096, 4096, 0, disk_bytenr
, 4096,
156 BTRFS_FILE_EXTENT_PREALLOC
, 0, slot
);
161 * We want to jack up disk_bytenr a little more so the em stuff doesn't
167 * Now for a partially written prealloc extent, basically the same as
168 * the hole punch example above. Ram_bytes never changes when you mark
169 * extents written btw.
171 insert_extent(root
, offset
, 4096, 16384, 0, disk_bytenr
, 16384,
172 BTRFS_FILE_EXTENT_PREALLOC
, 0, slot
);
175 insert_extent(root
, offset
, 4096, 16384, 4096, disk_bytenr
, 16384,
176 BTRFS_FILE_EXTENT_REG
, 0, slot
);
179 insert_extent(root
, offset
, 8192, 16384, 8192, disk_bytenr
, 16384,
180 BTRFS_FILE_EXTENT_PREALLOC
, 0, slot
);
183 disk_bytenr
+= 16384;
185 /* Now a normal compressed extent */
186 insert_extent(root
, offset
, 8192, 8192, 0, disk_bytenr
, 4096,
187 BTRFS_FILE_EXTENT_REG
, BTRFS_COMPRESS_ZLIB
, slot
);
193 /* Now a split compressed extent */
194 insert_extent(root
, offset
, 4096, 16384, 0, disk_bytenr
, 4096,
195 BTRFS_FILE_EXTENT_REG
, BTRFS_COMPRESS_ZLIB
, slot
);
198 insert_extent(root
, offset
, 4096, 4096, 0, disk_bytenr
+ 4096, 4096,
199 BTRFS_FILE_EXTENT_REG
, 0, slot
);
202 insert_extent(root
, offset
, 8192, 16384, 8192, disk_bytenr
, 4096,
203 BTRFS_FILE_EXTENT_REG
, BTRFS_COMPRESS_ZLIB
, slot
);
208 /* Now extents that have a hole but no hole extent */
209 insert_extent(root
, offset
, 4096, 4096, 0, disk_bytenr
, 4096,
210 BTRFS_FILE_EXTENT_REG
, 0, slot
);
214 insert_extent(root
, offset
, 4096, 4096, 0, disk_bytenr
, 4096,
215 BTRFS_FILE_EXTENT_REG
, 0, slot
);
218 static unsigned long prealloc_only
= 0;
219 static unsigned long compressed_only
= 0;
220 static unsigned long vacancy_only
= 0;
222 static noinline
int test_btrfs_get_extent(void)
224 struct inode
*inode
= NULL
;
225 struct btrfs_root
*root
= NULL
;
226 struct extent_map
*em
= NULL
;
232 inode
= btrfs_new_test_inode();
234 test_msg("Couldn't allocate inode\n");
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 root
= btrfs_alloc_dummy_root();
244 test_msg("Couldn't allocate root\n");
249 * We do this since btrfs_get_extent wants to assign em->bdev to
250 * root->fs_info->fs_devices->latest_bdev.
252 root
->fs_info
= btrfs_alloc_dummy_fs_info();
253 if (!root
->fs_info
) {
254 test_msg("Couldn't allocate dummy fs info\n");
258 root
->node
= alloc_dummy_extent_buffer(NULL
, 4096);
260 test_msg("Couldn't allocate dummy buffer\n");
265 * We will just free a dummy node if it's ref count is 2 so we need an
266 * extra ref so our searches don't accidently release our page.
268 extent_buffer_get(root
->node
);
269 btrfs_set_header_nritems(root
->node
, 0);
270 btrfs_set_header_level(root
->node
, 0);
273 /* First with no extents */
274 BTRFS_I(inode
)->root
= root
;
275 em
= btrfs_get_extent(inode
, NULL
, 0, 0, 4096, 0);
278 test_msg("Got an error when we shouldn't have\n");
281 if (em
->block_start
!= EXTENT_MAP_HOLE
) {
282 test_msg("Expected a hole, got %llu\n", em
->block_start
);
285 if (!test_bit(EXTENT_FLAG_VACANCY
, &em
->flags
)) {
286 test_msg("Vacancy flag wasn't set properly\n");
290 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
293 * All of the magic numbers are based on the mapping setup in
294 * setup_file_extents, so if you change anything there you need to
295 * update the comment and update the expected values below.
297 setup_file_extents(root
);
299 em
= btrfs_get_extent(inode
, NULL
, 0, 0, (u64
)-1, 0);
301 test_msg("Got an error when we shouldn't have\n");
304 if (em
->block_start
!= EXTENT_MAP_HOLE
) {
305 test_msg("Expected a hole, got %llu\n", em
->block_start
);
308 if (em
->start
!= 0 || em
->len
!= 5) {
309 test_msg("Unexpected extent wanted start 0 len 5, got start "
310 "%llu len %llu\n", em
->start
, em
->len
);
313 if (em
->flags
!= 0) {
314 test_msg("Unexpected flags set, want 0 have %lu\n", em
->flags
);
317 offset
= em
->start
+ em
->len
;
320 em
= btrfs_get_extent(inode
, NULL
, 0, offset
, 4096, 0);
322 test_msg("Got an error when we shouldn't have\n");
325 if (em
->block_start
!= EXTENT_MAP_INLINE
) {
326 test_msg("Expected an inline, got %llu\n", em
->block_start
);
329 if (em
->start
!= offset
|| em
->len
!= 4091) {
330 test_msg("Unexpected extent wanted start %llu len 1, got start "
331 "%llu len %llu\n", offset
, em
->start
, em
->len
);
334 if (em
->flags
!= 0) {
335 test_msg("Unexpected flags set, want 0 have %lu\n", em
->flags
);
339 * We don't test anything else for inline since it doesn't get set
340 * unless we have a page for it to write into. Maybe we should change
343 offset
= em
->start
+ em
->len
;
346 em
= btrfs_get_extent(inode
, NULL
, 0, offset
, 4096, 0);
348 test_msg("Got an error when we shouldn't have\n");
351 if (em
->block_start
!= EXTENT_MAP_HOLE
) {
352 test_msg("Expected a hole, got %llu\n", em
->block_start
);
355 if (em
->start
!= offset
|| em
->len
!= 4) {
356 test_msg("Unexpected extent wanted start %llu len 4, got start "
357 "%llu len %llu\n", offset
, em
->start
, em
->len
);
360 if (em
->flags
!= 0) {
361 test_msg("Unexpected flags set, want 0 have %lu\n", em
->flags
);
364 offset
= em
->start
+ em
->len
;
368 em
= btrfs_get_extent(inode
, NULL
, 0, offset
, 4096, 0);
370 test_msg("Got an error when we shouldn't have\n");
373 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
374 test_msg("Expected a real extent, got %llu\n", em
->block_start
);
377 if (em
->start
!= offset
|| em
->len
!= 4095) {
378 test_msg("Unexpected extent wanted start %llu len 4095, got "
379 "start %llu len %llu\n", offset
, em
->start
, em
->len
);
382 if (em
->flags
!= 0) {
383 test_msg("Unexpected flags set, want 0 have %lu\n", em
->flags
);
386 if (em
->orig_start
!= em
->start
) {
387 test_msg("Wrong orig offset, want %llu, have %llu\n", em
->start
,
391 offset
= em
->start
+ em
->len
;
394 /* The next 3 are split extents */
395 em
= btrfs_get_extent(inode
, NULL
, 0, offset
, 4096, 0);
397 test_msg("Got an error when we shouldn't have\n");
400 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
401 test_msg("Expected a real extent, got %llu\n", em
->block_start
);
404 if (em
->start
!= offset
|| em
->len
!= 4096) {
405 test_msg("Unexpected extent wanted start %llu len 4096, got "
406 "start %llu len %llu\n", offset
, em
->start
, em
->len
);
409 if (em
->flags
!= 0) {
410 test_msg("Unexpected flags set, want 0 have %lu\n", em
->flags
);
413 if (em
->orig_start
!= em
->start
) {
414 test_msg("Wrong orig offset, want %llu, have %llu\n", em
->start
,
418 disk_bytenr
= em
->block_start
;
419 orig_start
= em
->start
;
420 offset
= em
->start
+ em
->len
;
423 em
= btrfs_get_extent(inode
, NULL
, 0, offset
, 4096, 0);
425 test_msg("Got an error when we shouldn't have\n");
428 if (em
->block_start
!= EXTENT_MAP_HOLE
) {
429 test_msg("Expected a hole, got %llu\n", em
->block_start
);
432 if (em
->start
!= offset
|| em
->len
!= 4096) {
433 test_msg("Unexpected extent wanted start %llu len 4096, got "
434 "start %llu len %llu\n", offset
, em
->start
, em
->len
);
437 if (em
->flags
!= 0) {
438 test_msg("Unexpected flags set, want 0 have %lu\n", em
->flags
);
441 offset
= em
->start
+ em
->len
;
444 em
= btrfs_get_extent(inode
, NULL
, 0, offset
, 4096, 0);
446 test_msg("Got an error when we shouldn't have\n");
449 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
450 test_msg("Expected a real extent, got %llu\n", em
->block_start
);
453 if (em
->start
!= offset
|| em
->len
!= 8192) {
454 test_msg("Unexpected extent wanted start %llu len 8192, got "
455 "start %llu len %llu\n", offset
, em
->start
, em
->len
);
458 if (em
->flags
!= 0) {
459 test_msg("Unexpected flags set, want 0 have %lu\n", em
->flags
);
462 if (em
->orig_start
!= orig_start
) {
463 test_msg("Wrong orig offset, want %llu, have %llu\n",
464 orig_start
, em
->orig_start
);
467 disk_bytenr
+= (em
->start
- orig_start
);
468 if (em
->block_start
!= disk_bytenr
) {
469 test_msg("Wrong block start, want %llu, have %llu\n",
470 disk_bytenr
, em
->block_start
);
473 offset
= em
->start
+ em
->len
;
476 /* Prealloc extent */
477 em
= btrfs_get_extent(inode
, NULL
, 0, offset
, 4096, 0);
479 test_msg("Got an error when we shouldn't have\n");
482 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
483 test_msg("Expected a real extent, got %llu\n", em
->block_start
);
486 if (em
->start
!= offset
|| em
->len
!= 4096) {
487 test_msg("Unexpected extent wanted start %llu len 4096, got "
488 "start %llu len %llu\n", offset
, em
->start
, em
->len
);
491 if (em
->flags
!= prealloc_only
) {
492 test_msg("Unexpected flags set, want %lu have %lu\n",
493 prealloc_only
, em
->flags
);
496 if (em
->orig_start
!= em
->start
) {
497 test_msg("Wrong orig offset, want %llu, have %llu\n", em
->start
,
501 offset
= em
->start
+ em
->len
;
504 /* The next 3 are a half written prealloc extent */
505 em
= btrfs_get_extent(inode
, NULL
, 0, offset
, 4096, 0);
507 test_msg("Got an error when we shouldn't have\n");
510 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
511 test_msg("Expected a real extent, got %llu\n", em
->block_start
);
514 if (em
->start
!= offset
|| em
->len
!= 4096) {
515 test_msg("Unexpected extent wanted start %llu len 4096, got "
516 "start %llu len %llu\n", offset
, em
->start
, em
->len
);
519 if (em
->flags
!= prealloc_only
) {
520 test_msg("Unexpected flags set, want %lu have %lu\n",
521 prealloc_only
, em
->flags
);
524 if (em
->orig_start
!= em
->start
) {
525 test_msg("Wrong orig offset, want %llu, have %llu\n", em
->start
,
529 disk_bytenr
= em
->block_start
;
530 orig_start
= em
->start
;
531 offset
= em
->start
+ em
->len
;
534 em
= btrfs_get_extent(inode
, NULL
, 0, offset
, 4096, 0);
536 test_msg("Got an error when we shouldn't have\n");
539 if (em
->block_start
>= EXTENT_MAP_HOLE
) {
540 test_msg("Expected a real extent, got %llu\n", em
->block_start
);
543 if (em
->start
!= offset
|| em
->len
!= 4096) {
544 test_msg("Unexpected extent wanted start %llu len 4096, got "
545 "start %llu len %llu\n", offset
, em
->start
, em
->len
);
548 if (em
->flags
!= 0) {
549 test_msg("Unexpected flags set, want 0 have %lu\n", em
->flags
);
552 if (em
->orig_start
!= orig_start
) {
553 test_msg("Unexpected orig offset, wanted %llu, have %llu\n",
554 orig_start
, em
->orig_start
);
557 if (em
->block_start
!= (disk_bytenr
+ (em
->start
- em
->orig_start
))) {
558 test_msg("Unexpected block start, wanted %llu, have %llu\n",
559 disk_bytenr
+ (em
->start
- em
->orig_start
),
563 offset
= em
->start
+ em
->len
;
566 em
= btrfs_get_extent(inode
, NULL
, 0, offset
, 4096, 0);
568 test_msg("Got an error when we shouldn't have\n");
571 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
572 test_msg("Expected a real extent, got %llu\n", em
->block_start
);
575 if (em
->start
!= offset
|| em
->len
!= 8192) {
576 test_msg("Unexpected extent wanted start %llu len 8192, got "
577 "start %llu len %llu\n", offset
, em
->start
, em
->len
);
580 if (em
->flags
!= prealloc_only
) {
581 test_msg("Unexpected flags set, want %lu have %lu\n",
582 prealloc_only
, em
->flags
);
585 if (em
->orig_start
!= orig_start
) {
586 test_msg("Wrong orig offset, want %llu, have %llu\n", orig_start
,
590 if (em
->block_start
!= (disk_bytenr
+ (em
->start
- em
->orig_start
))) {
591 test_msg("Unexpected block start, wanted %llu, have %llu\n",
592 disk_bytenr
+ (em
->start
- em
->orig_start
),
596 offset
= em
->start
+ em
->len
;
599 /* Now for the compressed extent */
600 em
= btrfs_get_extent(inode
, NULL
, 0, offset
, 4096, 0);
602 test_msg("Got an error when we shouldn't have\n");
605 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
606 test_msg("Expected a real extent, got %llu\n", em
->block_start
);
609 if (em
->start
!= offset
|| em
->len
!= 8192) {
610 test_msg("Unexpected extent wanted start %llu len 8192, got "
611 "start %llu len %llu\n", offset
, em
->start
, em
->len
);
614 if (em
->flags
!= compressed_only
) {
615 test_msg("Unexpected flags set, want %lu have %lu\n",
616 compressed_only
, em
->flags
);
619 if (em
->orig_start
!= em
->start
) {
620 test_msg("Wrong orig offset, want %llu, have %llu\n",
621 em
->start
, em
->orig_start
);
624 if (em
->compress_type
!= BTRFS_COMPRESS_ZLIB
) {
625 test_msg("Unexpected compress type, wanted %d, got %d\n",
626 BTRFS_COMPRESS_ZLIB
, em
->compress_type
);
629 offset
= em
->start
+ em
->len
;
632 /* Split compressed extent */
633 em
= btrfs_get_extent(inode
, NULL
, 0, offset
, 4096, 0);
635 test_msg("Got an error when we shouldn't have\n");
638 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
639 test_msg("Expected a real extent, got %llu\n", em
->block_start
);
642 if (em
->start
!= offset
|| em
->len
!= 4096) {
643 test_msg("Unexpected extent wanted start %llu len 4096, got "
644 "start %llu len %llu\n", offset
, em
->start
, em
->len
);
647 if (em
->flags
!= compressed_only
) {
648 test_msg("Unexpected flags set, want %lu have %lu\n",
649 compressed_only
, em
->flags
);
652 if (em
->orig_start
!= em
->start
) {
653 test_msg("Wrong orig offset, want %llu, have %llu\n",
654 em
->start
, em
->orig_start
);
657 if (em
->compress_type
!= BTRFS_COMPRESS_ZLIB
) {
658 test_msg("Unexpected compress type, wanted %d, got %d\n",
659 BTRFS_COMPRESS_ZLIB
, em
->compress_type
);
662 disk_bytenr
= em
->block_start
;
663 orig_start
= em
->start
;
664 offset
= em
->start
+ em
->len
;
667 em
= btrfs_get_extent(inode
, NULL
, 0, offset
, 4096, 0);
669 test_msg("Got an error when we shouldn't have\n");
672 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
673 test_msg("Expected a real extent, got %llu\n", em
->block_start
);
676 if (em
->start
!= offset
|| em
->len
!= 4096) {
677 test_msg("Unexpected extent wanted start %llu len 4096, got "
678 "start %llu len %llu\n", offset
, em
->start
, em
->len
);
681 if (em
->flags
!= 0) {
682 test_msg("Unexpected flags set, want 0 have %lu\n", em
->flags
);
685 if (em
->orig_start
!= em
->start
) {
686 test_msg("Wrong orig offset, want %llu, have %llu\n", em
->start
,
690 offset
= em
->start
+ em
->len
;
693 em
= btrfs_get_extent(inode
, NULL
, 0, offset
, 4096, 0);
695 test_msg("Got an error when we shouldn't have\n");
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
);
703 if (em
->start
!= offset
|| em
->len
!= 8192) {
704 test_msg("Unexpected extent wanted start %llu len 8192, got "
705 "start %llu len %llu\n", offset
, em
->start
, em
->len
);
708 if (em
->flags
!= compressed_only
) {
709 test_msg("Unexpected flags set, want %lu have %lu\n",
710 compressed_only
, em
->flags
);
713 if (em
->orig_start
!= orig_start
) {
714 test_msg("Wrong orig offset, want %llu, have %llu\n",
715 em
->start
, orig_start
);
718 if (em
->compress_type
!= BTRFS_COMPRESS_ZLIB
) {
719 test_msg("Unexpected compress type, wanted %d, got %d\n",
720 BTRFS_COMPRESS_ZLIB
, em
->compress_type
);
723 offset
= em
->start
+ em
->len
;
726 /* A hole between regular extents but no hole extent */
727 em
= btrfs_get_extent(inode
, NULL
, 0, offset
+ 6, 4096, 0);
729 test_msg("Got an error when we shouldn't have\n");
732 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
733 test_msg("Expected a real extent, got %llu\n", em
->block_start
);
736 if (em
->start
!= offset
|| em
->len
!= 4096) {
737 test_msg("Unexpected extent wanted start %llu len 4096, got "
738 "start %llu len %llu\n", offset
, em
->start
, em
->len
);
741 if (em
->flags
!= 0) {
742 test_msg("Unexpected flags set, want 0 have %lu\n", em
->flags
);
745 if (em
->orig_start
!= em
->start
) {
746 test_msg("Wrong orig offset, want %llu, have %llu\n", em
->start
,
750 offset
= em
->start
+ em
->len
;
753 em
= btrfs_get_extent(inode
, NULL
, 0, offset
, 4096 * 1024, 0);
755 test_msg("Got an error when we shouldn't have\n");
758 if (em
->block_start
!= EXTENT_MAP_HOLE
) {
759 test_msg("Expected a hole extent, got %llu\n", em
->block_start
);
763 * Currently we just return a length that we requested rather than the
764 * length of the actual hole, if this changes we'll have to change this
767 if (em
->start
!= offset
|| em
->len
!= 12288) {
768 test_msg("Unexpected extent wanted start %llu len 12288, got "
769 "start %llu len %llu\n", offset
, em
->start
, em
->len
);
772 if (em
->flags
!= vacancy_only
) {
773 test_msg("Unexpected flags set, want %lu have %lu\n",
774 vacancy_only
, em
->flags
);
777 if (em
->orig_start
!= em
->start
) {
778 test_msg("Wrong orig offset, want %llu, have %llu\n", em
->start
,
782 offset
= em
->start
+ em
->len
;
785 em
= btrfs_get_extent(inode
, NULL
, 0, offset
, 4096, 0);
787 test_msg("Got an error when we shouldn't have\n");
790 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
791 test_msg("Expected a real extent, got %llu\n", em
->block_start
);
794 if (em
->start
!= offset
|| em
->len
!= 4096) {
795 test_msg("Unexpected extent wanted start %llu len 4096, got "
796 "start %llu len %llu\n", offset
, em
->start
, em
->len
);
799 if (em
->flags
!= 0) {
800 test_msg("Unexpected flags set, want 0 have %lu\n", em
->flags
);
803 if (em
->orig_start
!= em
->start
) {
804 test_msg("Wrong orig offset, want %llu, have %llu\n", em
->start
,
813 btrfs_free_dummy_root(root
);
817 static int test_hole_first(void)
819 struct inode
*inode
= NULL
;
820 struct btrfs_root
*root
= NULL
;
821 struct extent_map
*em
= NULL
;
824 inode
= btrfs_new_test_inode();
826 test_msg("Couldn't allocate inode\n");
830 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
831 BTRFS_I(inode
)->location
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
832 BTRFS_I(inode
)->location
.offset
= 0;
834 root
= btrfs_alloc_dummy_root();
836 test_msg("Couldn't allocate root\n");
840 root
->fs_info
= btrfs_alloc_dummy_fs_info();
841 if (!root
->fs_info
) {
842 test_msg("Couldn't allocate dummy fs info\n");
846 root
->node
= alloc_dummy_extent_buffer(NULL
, 4096);
848 test_msg("Couldn't allocate dummy buffer\n");
852 extent_buffer_get(root
->node
);
853 btrfs_set_header_nritems(root
->node
, 0);
854 btrfs_set_header_level(root
->node
, 0);
855 BTRFS_I(inode
)->root
= root
;
859 * Need a blank inode item here just so we don't confuse
862 insert_inode_item_key(root
);
863 insert_extent(root
, 4096, 4096, 4096, 0, 4096, 4096,
864 BTRFS_FILE_EXTENT_REG
, 0, 1);
865 em
= btrfs_get_extent(inode
, NULL
, 0, 0, 8192, 0);
867 test_msg("Got an error when we shouldn't have\n");
870 if (em
->block_start
!= EXTENT_MAP_HOLE
) {
871 test_msg("Expected a hole, got %llu\n", em
->block_start
);
874 if (em
->start
!= 0 || em
->len
!= 4096) {
875 test_msg("Unexpected extent wanted start 0 len 4096, got start "
876 "%llu len %llu\n", em
->start
, em
->len
);
879 if (em
->flags
!= vacancy_only
) {
880 test_msg("Wrong flags, wanted %lu, have %lu\n", vacancy_only
,
886 em
= btrfs_get_extent(inode
, NULL
, 0, 4096, 8192, 0);
888 test_msg("Got an error when we shouldn't have\n");
891 if (em
->block_start
!= 4096) {
892 test_msg("Expected a real extent, got %llu\n", em
->block_start
);
895 if (em
->start
!= 4096 || em
->len
!= 4096) {
896 test_msg("Unexpected extent wanted start 4096 len 4096, got "
897 "start %llu len %llu\n", em
->start
, em
->len
);
900 if (em
->flags
!= 0) {
901 test_msg("Unexpected flags set, wanted 0 got %lu\n",
910 btrfs_free_dummy_root(root
);
914 static int test_extent_accounting(void)
916 struct inode
*inode
= NULL
;
917 struct btrfs_root
*root
= NULL
;
920 inode
= btrfs_new_test_inode();
922 test_msg("Couldn't allocate inode\n");
926 root
= btrfs_alloc_dummy_root();
928 test_msg("Couldn't allocate root\n");
932 root
->fs_info
= btrfs_alloc_dummy_fs_info();
933 if (!root
->fs_info
) {
934 test_msg("Couldn't allocate dummy fs info\n");
938 BTRFS_I(inode
)->root
= root
;
939 btrfs_test_inode_set_ops(inode
);
941 /* [BTRFS_MAX_EXTENT_SIZE] */
942 BTRFS_I(inode
)->outstanding_extents
++;
943 ret
= btrfs_set_extent_delalloc(inode
, 0, BTRFS_MAX_EXTENT_SIZE
- 1,
946 test_msg("btrfs_set_extent_delalloc returned %d\n", ret
);
949 if (BTRFS_I(inode
)->outstanding_extents
!= 1) {
951 test_msg("Miscount, wanted 1, got %u\n",
952 BTRFS_I(inode
)->outstanding_extents
);
956 /* [BTRFS_MAX_EXTENT_SIZE][4k] */
957 BTRFS_I(inode
)->outstanding_extents
++;
958 ret
= btrfs_set_extent_delalloc(inode
, BTRFS_MAX_EXTENT_SIZE
,
959 BTRFS_MAX_EXTENT_SIZE
+ 4095, NULL
);
961 test_msg("btrfs_set_extent_delalloc returned %d\n", ret
);
964 if (BTRFS_I(inode
)->outstanding_extents
!= 2) {
966 test_msg("Miscount, wanted 2, got %u\n",
967 BTRFS_I(inode
)->outstanding_extents
);
971 /* [BTRFS_MAX_EXTENT_SIZE/2][4K HOLE][the rest] */
972 ret
= clear_extent_bit(&BTRFS_I(inode
)->io_tree
,
973 BTRFS_MAX_EXTENT_SIZE
>> 1,
974 (BTRFS_MAX_EXTENT_SIZE
>> 1) + 4095,
975 EXTENT_DELALLOC
| EXTENT_DIRTY
|
976 EXTENT_UPTODATE
| EXTENT_DO_ACCOUNTING
, 0, 0,
979 test_msg("clear_extent_bit returned %d\n", ret
);
982 if (BTRFS_I(inode
)->outstanding_extents
!= 2) {
984 test_msg("Miscount, wanted 2, got %u\n",
985 BTRFS_I(inode
)->outstanding_extents
);
989 /* [BTRFS_MAX_EXTENT_SIZE][4K] */
990 BTRFS_I(inode
)->outstanding_extents
++;
991 ret
= btrfs_set_extent_delalloc(inode
, BTRFS_MAX_EXTENT_SIZE
>> 1,
992 (BTRFS_MAX_EXTENT_SIZE
>> 1) + 4095,
995 test_msg("btrfs_set_extent_delalloc returned %d\n", ret
);
998 if (BTRFS_I(inode
)->outstanding_extents
!= 2) {
1000 test_msg("Miscount, wanted 2, got %u\n",
1001 BTRFS_I(inode
)->outstanding_extents
);
1006 * [BTRFS_MAX_EXTENT_SIZE+4K][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4K]
1008 * I'm artificially adding 2 to outstanding_extents because in the
1009 * buffered IO case we'd add things up as we go, but I don't feel like
1010 * doing that here, this isn't the interesting case we want to test.
1012 BTRFS_I(inode
)->outstanding_extents
+= 2;
1013 ret
= btrfs_set_extent_delalloc(inode
, BTRFS_MAX_EXTENT_SIZE
+ 8192,
1014 (BTRFS_MAX_EXTENT_SIZE
<< 1) + 12287,
1017 test_msg("btrfs_set_extent_delalloc returned %d\n", ret
);
1020 if (BTRFS_I(inode
)->outstanding_extents
!= 4) {
1022 test_msg("Miscount, wanted 4, got %u\n",
1023 BTRFS_I(inode
)->outstanding_extents
);
1027 /* [BTRFS_MAX_EXTENT_SIZE+4k][4k][BTRFS_MAX_EXTENT_SIZE+4k] */
1028 BTRFS_I(inode
)->outstanding_extents
++;
1029 ret
= btrfs_set_extent_delalloc(inode
, BTRFS_MAX_EXTENT_SIZE
+4096,
1030 BTRFS_MAX_EXTENT_SIZE
+8191, NULL
);
1032 test_msg("btrfs_set_extent_delalloc returned %d\n", ret
);
1035 if (BTRFS_I(inode
)->outstanding_extents
!= 3) {
1037 test_msg("Miscount, wanted 3, got %u\n",
1038 BTRFS_I(inode
)->outstanding_extents
);
1042 /* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */
1043 ret
= clear_extent_bit(&BTRFS_I(inode
)->io_tree
,
1044 BTRFS_MAX_EXTENT_SIZE
+4096,
1045 BTRFS_MAX_EXTENT_SIZE
+8191,
1046 EXTENT_DIRTY
| EXTENT_DELALLOC
|
1047 EXTENT_DO_ACCOUNTING
| EXTENT_UPTODATE
, 0, 0,
1050 test_msg("clear_extent_bit returned %d\n", ret
);
1053 if (BTRFS_I(inode
)->outstanding_extents
!= 4) {
1055 test_msg("Miscount, wanted 4, got %u\n",
1056 BTRFS_I(inode
)->outstanding_extents
);
1061 * Refill the hole again just for good measure, because I thought it
1062 * might fail and I'd rather satisfy my paranoia at this point.
1064 BTRFS_I(inode
)->outstanding_extents
++;
1065 ret
= btrfs_set_extent_delalloc(inode
, BTRFS_MAX_EXTENT_SIZE
+4096,
1066 BTRFS_MAX_EXTENT_SIZE
+8191, NULL
);
1068 test_msg("btrfs_set_extent_delalloc returned %d\n", ret
);
1071 if (BTRFS_I(inode
)->outstanding_extents
!= 3) {
1073 test_msg("Miscount, wanted 3, got %u\n",
1074 BTRFS_I(inode
)->outstanding_extents
);
1079 ret
= clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, (u64
)-1,
1080 EXTENT_DIRTY
| EXTENT_DELALLOC
|
1081 EXTENT_DO_ACCOUNTING
| EXTENT_UPTODATE
, 0, 0,
1084 test_msg("clear_extent_bit returned %d\n", ret
);
1087 if (BTRFS_I(inode
)->outstanding_extents
) {
1089 test_msg("Miscount, wanted 0, got %u\n",
1090 BTRFS_I(inode
)->outstanding_extents
);
1096 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, (u64
)-1,
1097 EXTENT_DIRTY
| EXTENT_DELALLOC
|
1098 EXTENT_DO_ACCOUNTING
| EXTENT_UPTODATE
, 0, 0,
1101 btrfs_free_dummy_root(root
);
1105 int btrfs_test_inodes(void)
1109 set_bit(EXTENT_FLAG_COMPRESSED
, &compressed_only
);
1110 set_bit(EXTENT_FLAG_VACANCY
, &vacancy_only
);
1111 set_bit(EXTENT_FLAG_PREALLOC
, &prealloc_only
);
1113 test_msg("Running btrfs_get_extent tests\n");
1114 ret
= test_btrfs_get_extent();
1117 test_msg("Running hole first btrfs_get_extent test\n");
1118 ret
= test_hole_first();
1121 test_msg("Running outstanding_extents tests\n");
1122 return test_extent_accounting();