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 struct btrfs_fs_info *alloc_dummy_fs_info(void)
28 struct btrfs_fs_info *fs_info = kzalloc(sizeof(struct btrfs_fs_info),
32 fs_info->fs_devices = kzalloc(sizeof(struct btrfs_fs_devices),
34 if (!fs_info->fs_devices) {
40 static void free_dummy_root(struct btrfs_root *root)
45 kfree(root->fs_info->fs_devices);
49 free_extent_buffer(root->node);
53 static void insert_extent(struct btrfs_root *root, u64 start, u64 len,
54 u64 ram_bytes, u64 offset, u64 disk_bytenr,
55 u64 disk_len, u32 type, u8 compression, int slot)
57 struct btrfs_path path;
58 struct btrfs_file_extent_item *fi;
59 struct extent_buffer *leaf = root->node;
61 u32 value_len = sizeof(struct btrfs_file_extent_item);
63 if (type == BTRFS_FILE_EXTENT_INLINE)
65 memset(&path, 0, sizeof(path));
70 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
71 key.type = BTRFS_EXTENT_DATA_KEY;
74 setup_items_for_insert(root, &path, &key, &value_len, value_len,
75 value_len + sizeof(struct btrfs_item), 1);
76 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
77 btrfs_set_file_extent_generation(leaf, fi, 1);
78 btrfs_set_file_extent_type(leaf, fi, type);
79 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
80 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_len);
81 btrfs_set_file_extent_offset(leaf, fi, offset);
82 btrfs_set_file_extent_num_bytes(leaf, fi, len);
83 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
84 btrfs_set_file_extent_compression(leaf, fi, compression);
85 btrfs_set_file_extent_encryption(leaf, fi, 0);
86 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
89 static void insert_inode_item_key(struct btrfs_root *root)
91 struct btrfs_path path;
92 struct extent_buffer *leaf = root->node;
96 memset(&path, 0, sizeof(path));
101 key.objectid = BTRFS_INODE_ITEM_KEY;
102 key.type = BTRFS_INODE_ITEM_KEY;
105 setup_items_for_insert(root, &path, &key, &value_len, value_len,
106 value_len + sizeof(struct btrfs_item), 1);
110 * Build the most complicated map of extents the earth has ever seen. We want
111 * this so we can test all of the corner cases of btrfs_get_extent. Here is a
112 * diagram of how the extents will look though this may not be possible we still
113 * want to make sure everything acts normally (the last number is not inclusive)
115 * [0 - 5][5 - 6][6 - 10][10 - 4096][ 4096 - 8192 ][8192 - 12288]
116 * [hole ][inline][ hole ][ regular ][regular1 split][ hole ]
118 * [ 12288 - 20480][20480 - 24576][ 24576 - 28672 ][28672 - 36864][36864 - 45056]
119 * [regular1 split][ prealloc1 ][prealloc1 written][ prealloc1 ][ compressed ]
121 * [45056 - 49152][49152-53248][53248-61440][61440-65536][ 65536+81920 ]
122 * [ compressed1 ][ regular ][compressed1][ regular ][ hole but no extent]
127 static void setup_file_extents(struct btrfs_root *root)
130 u64 disk_bytenr = 1 * 1024 * 1024;
133 /* First we want a hole */
134 insert_extent(root, offset, 5, 5, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
140 * Now we want an inline extent, I don't think this is possible but hey
141 * why not? Also keep in mind if we have an inline extent it counts as
142 * the whole first page. If we were to expand it we would have to cow
143 * and we wouldn't have an inline extent anymore.
145 insert_extent(root, offset, 1, 1, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE, 0,
150 /* Now another hole */
151 insert_extent(root, offset, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
156 /* Now for a regular extent */
157 insert_extent(root, offset, 4095, 4095, 0, disk_bytenr, 4096,
158 BTRFS_FILE_EXTENT_REG, 0, slot);
164 * Now for 3 extents that were split from a hole punch so we test
167 insert_extent(root, offset, 4096, 16384, 0, disk_bytenr, 16384,
168 BTRFS_FILE_EXTENT_REG, 0, slot);
171 insert_extent(root, offset, 4096, 4096, 0, 0, 0, BTRFS_FILE_EXTENT_REG,
175 insert_extent(root, offset, 8192, 16384, 8192, disk_bytenr, 16384,
176 BTRFS_FILE_EXTENT_REG, 0, slot);
179 disk_bytenr += 16384;
181 /* Now for a unwritten prealloc extent */
182 insert_extent(root, offset, 4096, 4096, 0, disk_bytenr, 4096,
183 BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
188 * We want to jack up disk_bytenr a little more so the em stuff doesn't
194 * Now for a partially written prealloc extent, basically the same as
195 * the hole punch example above. Ram_bytes never changes when you mark
196 * extents written btw.
198 insert_extent(root, offset, 4096, 16384, 0, disk_bytenr, 16384,
199 BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
202 insert_extent(root, offset, 4096, 16384, 4096, disk_bytenr, 16384,
203 BTRFS_FILE_EXTENT_REG, 0, slot);
206 insert_extent(root, offset, 8192, 16384, 8192, disk_bytenr, 16384,
207 BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
210 disk_bytenr += 16384;
212 /* Now a normal compressed extent */
213 insert_extent(root, offset, 8192, 8192, 0, disk_bytenr, 4096,
214 BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
220 /* Now a split compressed extent */
221 insert_extent(root, offset, 4096, 16384, 0, disk_bytenr, 4096,
222 BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
225 insert_extent(root, offset, 4096, 4096, 0, disk_bytenr + 4096, 4096,
226 BTRFS_FILE_EXTENT_REG, 0, slot);
229 insert_extent(root, offset, 8192, 16384, 8192, disk_bytenr, 4096,
230 BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
235 /* Now extents that have a hole but no hole extent */
236 insert_extent(root, offset, 4096, 4096, 0, disk_bytenr, 4096,
237 BTRFS_FILE_EXTENT_REG, 0, slot);
241 insert_extent(root, offset, 4096, 4096, 0, disk_bytenr, 4096,
242 BTRFS_FILE_EXTENT_REG, 0, slot);
245 static unsigned long prealloc_only = 0;
246 static unsigned long compressed_only = 0;
247 static unsigned long vacancy_only = 0;
249 static noinline int test_btrfs_get_extent(void)
251 struct inode *inode = NULL;
252 struct btrfs_root *root = NULL;
253 struct extent_map *em = NULL;
259 inode = btrfs_new_test_inode();
261 test_msg("Couldn't allocate inode\n");
265 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
266 BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
267 BTRFS_I(inode)->location.offset = 0;
269 root = btrfs_alloc_dummy_root();
271 test_msg("Couldn't allocate root\n");
276 * We do this since btrfs_get_extent wants to assign em->bdev to
277 * root->fs_info->fs_devices->latest_bdev.
279 root->fs_info = alloc_dummy_fs_info();
280 if (!root->fs_info) {
281 test_msg("Couldn't allocate dummy fs info\n");
285 root->node = alloc_dummy_extent_buffer(0, 4096);
287 test_msg("Couldn't allocate dummy buffer\n");
292 * We will just free a dummy node if it's ref count is 2 so we need an
293 * extra ref so our searches don't accidently release our page.
295 extent_buffer_get(root->node);
296 btrfs_set_header_nritems(root->node, 0);
297 btrfs_set_header_level(root->node, 0);
300 /* First with no extents */
301 BTRFS_I(inode)->root = root;
302 em = btrfs_get_extent(inode, NULL, 0, 0, 4096, 0);
305 test_msg("Got an error when we shouldn't have\n");
308 if (em->block_start != EXTENT_MAP_HOLE) {
309 test_msg("Expected a hole, got %llu\n", em->block_start);
312 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
313 test_msg("Vacancy flag wasn't set properly\n");
317 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
320 * All of the magic numbers are based on the mapping setup in
321 * setup_file_extents, so if you change anything there you need to
322 * update the comment and update the expected values below.
324 setup_file_extents(root);
326 em = btrfs_get_extent(inode, NULL, 0, 0, (u64)-1, 0);
328 test_msg("Got an error when we shouldn't have\n");
331 if (em->block_start != EXTENT_MAP_HOLE) {
332 test_msg("Expected a hole, got %llu\n", em->block_start);
335 if (em->start != 0 || em->len != 5) {
336 test_msg("Unexpected extent wanted start 0 len 5, got start "
337 "%llu len %llu\n", em->start, em->len);
340 if (em->flags != 0) {
341 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
344 offset = em->start + em->len;
347 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
349 test_msg("Got an error when we shouldn't have\n");
352 if (em->block_start != EXTENT_MAP_INLINE) {
353 test_msg("Expected an inline, got %llu\n", em->block_start);
356 if (em->start != offset || em->len != 4091) {
357 test_msg("Unexpected extent wanted start %llu len 1, got start "
358 "%llu len %llu\n", offset, em->start, em->len);
361 if (em->flags != 0) {
362 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
366 * We don't test anything else for inline since it doesn't get set
367 * unless we have a page for it to write into. Maybe we should change
370 offset = em->start + em->len;
373 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
375 test_msg("Got an error when we shouldn't have\n");
378 if (em->block_start != EXTENT_MAP_HOLE) {
379 test_msg("Expected a hole, got %llu\n", em->block_start);
382 if (em->start != offset || em->len != 4) {
383 test_msg("Unexpected extent wanted start %llu len 4, got start "
384 "%llu len %llu\n", offset, em->start, em->len);
387 if (em->flags != 0) {
388 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
391 offset = em->start + em->len;
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 != 4095) {
405 test_msg("Unexpected extent wanted start %llu len 4095, 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 offset = em->start + em->len;
421 /* The next 3 are split extents */
422 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
424 test_msg("Got an error when we shouldn't have\n");
427 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
428 test_msg("Expected a real extent, got %llu\n", em->block_start);
431 if (em->start != offset || em->len != 4096) {
432 test_msg("Unexpected extent wanted start %llu len 4096, got "
433 "start %llu len %llu\n", offset, em->start, em->len);
436 if (em->flags != 0) {
437 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
440 if (em->orig_start != em->start) {
441 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
445 disk_bytenr = em->block_start;
446 orig_start = em->start;
447 offset = em->start + em->len;
450 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
452 test_msg("Got an error when we shouldn't have\n");
455 if (em->block_start != EXTENT_MAP_HOLE) {
456 test_msg("Expected a hole, got %llu\n", em->block_start);
459 if (em->start != offset || em->len != 4096) {
460 test_msg("Unexpected extent wanted start %llu len 4096, got "
461 "start %llu len %llu\n", offset, em->start, em->len);
464 if (em->flags != 0) {
465 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
468 offset = em->start + em->len;
471 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
473 test_msg("Got an error when we shouldn't have\n");
476 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
477 test_msg("Expected a real extent, got %llu\n", em->block_start);
480 if (em->start != offset || em->len != 8192) {
481 test_msg("Unexpected extent wanted start %llu len 8192, got "
482 "start %llu len %llu\n", offset, em->start, em->len);
485 if (em->flags != 0) {
486 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
489 if (em->orig_start != orig_start) {
490 test_msg("Wrong orig offset, want %llu, have %llu\n",
491 orig_start, em->orig_start);
494 disk_bytenr += (em->start - orig_start);
495 if (em->block_start != disk_bytenr) {
496 test_msg("Wrong block start, want %llu, have %llu\n",
497 disk_bytenr, em->block_start);
500 offset = em->start + em->len;
503 /* Prealloc extent */
504 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
506 test_msg("Got an error when we shouldn't have\n");
509 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
510 test_msg("Expected a real extent, got %llu\n", em->block_start);
513 if (em->start != offset || em->len != 4096) {
514 test_msg("Unexpected extent wanted start %llu len 4096, got "
515 "start %llu len %llu\n", offset, em->start, em->len);
518 if (em->flags != prealloc_only) {
519 test_msg("Unexpected flags set, want %lu have %lu\n",
520 prealloc_only, em->flags);
523 if (em->orig_start != em->start) {
524 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
528 offset = em->start + em->len;
531 /* The next 3 are a half written prealloc extent */
532 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
534 test_msg("Got an error when we shouldn't have\n");
537 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
538 test_msg("Expected a real extent, got %llu\n", em->block_start);
541 if (em->start != offset || em->len != 4096) {
542 test_msg("Unexpected extent wanted start %llu len 4096, got "
543 "start %llu len %llu\n", offset, em->start, em->len);
546 if (em->flags != prealloc_only) {
547 test_msg("Unexpected flags set, want %lu have %lu\n",
548 prealloc_only, em->flags);
551 if (em->orig_start != em->start) {
552 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
556 disk_bytenr = em->block_start;
557 orig_start = em->start;
558 offset = em->start + em->len;
561 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
563 test_msg("Got an error when we shouldn't have\n");
566 if (em->block_start >= EXTENT_MAP_HOLE) {
567 test_msg("Expected a real extent, got %llu\n", em->block_start);
570 if (em->start != offset || em->len != 4096) {
571 test_msg("Unexpected extent wanted start %llu len 4096, got "
572 "start %llu len %llu\n", offset, em->start, em->len);
575 if (em->flags != 0) {
576 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
579 if (em->orig_start != orig_start) {
580 test_msg("Unexpected orig offset, wanted %llu, have %llu\n",
581 orig_start, em->orig_start);
584 if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
585 test_msg("Unexpected block start, wanted %llu, have %llu\n",
586 disk_bytenr + (em->start - em->orig_start),
590 offset = em->start + em->len;
593 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
595 test_msg("Got an error when we shouldn't have\n");
598 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
599 test_msg("Expected a real extent, got %llu\n", em->block_start);
602 if (em->start != offset || em->len != 8192) {
603 test_msg("Unexpected extent wanted start %llu len 8192, got "
604 "start %llu len %llu\n", offset, em->start, em->len);
607 if (em->flags != prealloc_only) {
608 test_msg("Unexpected flags set, want %lu have %lu\n",
609 prealloc_only, em->flags);
612 if (em->orig_start != orig_start) {
613 test_msg("Wrong orig offset, want %llu, have %llu\n", orig_start,
617 if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
618 test_msg("Unexpected block start, wanted %llu, have %llu\n",
619 disk_bytenr + (em->start - em->orig_start),
623 offset = em->start + em->len;
626 /* Now for the compressed extent */
627 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
629 test_msg("Got an error when we shouldn't have\n");
632 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
633 test_msg("Expected a real extent, got %llu\n", em->block_start);
636 if (em->start != offset || em->len != 8192) {
637 test_msg("Unexpected extent wanted start %llu len 8192, got "
638 "start %llu len %llu\n", offset, em->start, em->len);
641 if (em->flags != compressed_only) {
642 test_msg("Unexpected flags set, want %lu have %lu\n",
643 compressed_only, em->flags);
646 if (em->orig_start != em->start) {
647 test_msg("Wrong orig offset, want %llu, have %llu\n",
648 em->start, em->orig_start);
651 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
652 test_msg("Unexpected compress type, wanted %d, got %d\n",
653 BTRFS_COMPRESS_ZLIB, em->compress_type);
656 offset = em->start + em->len;
659 /* Split compressed extent */
660 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
662 test_msg("Got an error when we shouldn't have\n");
665 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
666 test_msg("Expected a real extent, got %llu\n", em->block_start);
669 if (em->start != offset || em->len != 4096) {
670 test_msg("Unexpected extent wanted start %llu len 4096, got "
671 "start %llu len %llu\n", offset, em->start, em->len);
674 if (em->flags != compressed_only) {
675 test_msg("Unexpected flags set, want %lu have %lu\n",
676 compressed_only, em->flags);
679 if (em->orig_start != em->start) {
680 test_msg("Wrong orig offset, want %llu, have %llu\n",
681 em->start, em->orig_start);
684 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
685 test_msg("Unexpected compress type, wanted %d, got %d\n",
686 BTRFS_COMPRESS_ZLIB, em->compress_type);
689 disk_bytenr = em->block_start;
690 orig_start = em->start;
691 offset = em->start + em->len;
694 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
696 test_msg("Got an error when we shouldn't have\n");
699 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
700 test_msg("Expected a real extent, got %llu\n", em->block_start);
703 if (em->start != offset || em->len != 4096) {
704 test_msg("Unexpected extent wanted start %llu len 4096, got "
705 "start %llu len %llu\n", offset, em->start, em->len);
708 if (em->flags != 0) {
709 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
712 if (em->orig_start != em->start) {
713 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
717 offset = em->start + em->len;
720 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
722 test_msg("Got an error when we shouldn't have\n");
725 if (em->block_start != disk_bytenr) {
726 test_msg("Block start does not match, want %llu got %llu\n",
727 disk_bytenr, em->block_start);
730 if (em->start != offset || em->len != 8192) {
731 test_msg("Unexpected extent wanted start %llu len 8192, got "
732 "start %llu len %llu\n", offset, em->start, em->len);
735 if (em->flags != compressed_only) {
736 test_msg("Unexpected flags set, want %lu have %lu\n",
737 compressed_only, em->flags);
740 if (em->orig_start != orig_start) {
741 test_msg("Wrong orig offset, want %llu, have %llu\n",
742 em->start, orig_start);
745 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
746 test_msg("Unexpected compress type, wanted %d, got %d\n",
747 BTRFS_COMPRESS_ZLIB, em->compress_type);
750 offset = em->start + em->len;
753 /* A hole between regular extents but no hole extent */
754 em = btrfs_get_extent(inode, NULL, 0, offset + 6, 4096, 0);
756 test_msg("Got an error when we shouldn't have\n");
759 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
760 test_msg("Expected a real extent, got %llu\n", em->block_start);
763 if (em->start != offset || em->len != 4096) {
764 test_msg("Unexpected extent wanted start %llu len 4096, got "
765 "start %llu len %llu\n", offset, em->start, em->len);
768 if (em->flags != 0) {
769 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
772 if (em->orig_start != em->start) {
773 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
777 offset = em->start + em->len;
780 em = btrfs_get_extent(inode, NULL, 0, offset, 4096 * 1024, 0);
782 test_msg("Got an error when we shouldn't have\n");
785 if (em->block_start != EXTENT_MAP_HOLE) {
786 test_msg("Expected a hole extent, got %llu\n", em->block_start);
790 * Currently we just return a length that we requested rather than the
791 * length of the actual hole, if this changes we'll have to change this
794 if (em->start != offset || em->len != 12288) {
795 test_msg("Unexpected extent wanted start %llu len 12288, got "
796 "start %llu len %llu\n", offset, em->start, em->len);
799 if (em->flags != vacancy_only) {
800 test_msg("Unexpected flags set, want %lu have %lu\n",
801 vacancy_only, em->flags);
804 if (em->orig_start != em->start) {
805 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
809 offset = em->start + em->len;
812 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
814 test_msg("Got an error when we shouldn't have\n");
817 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
818 test_msg("Expected a real extent, got %llu\n", em->block_start);
821 if (em->start != offset || em->len != 4096) {
822 test_msg("Unexpected extent wanted start %llu len 4096, got "
823 "start %llu len %llu\n", offset, em->start, em->len);
826 if (em->flags != 0) {
827 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
830 if (em->orig_start != em->start) {
831 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
840 free_dummy_root(root);
844 static int test_hole_first(void)
846 struct inode *inode = NULL;
847 struct btrfs_root *root = NULL;
848 struct extent_map *em = NULL;
851 inode = btrfs_new_test_inode();
853 test_msg("Couldn't allocate inode\n");
857 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
858 BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
859 BTRFS_I(inode)->location.offset = 0;
861 root = btrfs_alloc_dummy_root();
863 test_msg("Couldn't allocate root\n");
867 root->fs_info = alloc_dummy_fs_info();
868 if (!root->fs_info) {
869 test_msg("Couldn't allocate dummy fs info\n");
873 root->node = alloc_dummy_extent_buffer(0, 4096);
875 test_msg("Couldn't allocate dummy buffer\n");
879 extent_buffer_get(root->node);
880 btrfs_set_header_nritems(root->node, 0);
881 btrfs_set_header_level(root->node, 0);
882 BTRFS_I(inode)->root = root;
886 * Need a blank inode item here just so we don't confuse
889 insert_inode_item_key(root);
890 insert_extent(root, 4096, 4096, 4096, 0, 4096, 4096,
891 BTRFS_FILE_EXTENT_REG, 0, 1);
892 em = btrfs_get_extent(inode, NULL, 0, 0, 8192, 0);
894 test_msg("Got an error when we shouldn't have\n");
897 if (em->block_start != EXTENT_MAP_HOLE) {
898 test_msg("Expected a hole, got %llu\n", em->block_start);
901 if (em->start != 0 || em->len != 4096) {
902 test_msg("Unexpected extent wanted start 0 len 4096, got start "
903 "%llu len %llu\n", em->start, em->len);
906 if (em->flags != vacancy_only) {
907 test_msg("Wrong flags, wanted %lu, have %lu\n", vacancy_only,
913 em = btrfs_get_extent(inode, NULL, 0, 4096, 8192, 0);
915 test_msg("Got an error when we shouldn't have\n");
918 if (em->block_start != 4096) {
919 test_msg("Expected a real extent, got %llu\n", em->block_start);
922 if (em->start != 4096 || em->len != 4096) {
923 test_msg("Unexpected extent wanted start 4096 len 4096, got "
924 "start %llu len %llu\n", em->start, em->len);
927 if (em->flags != 0) {
928 test_msg("Unexpected flags set, wanted 0 got %lu\n",
937 free_dummy_root(root);
941 int btrfs_test_inodes(void)
945 set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
946 set_bit(EXTENT_FLAG_VACANCY, &vacancy_only);
947 set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
949 test_msg("Running btrfs_get_extent tests\n");
950 ret = test_btrfs_get_extent();
953 test_msg("Running hole first btrfs_get_extent test\n");
954 return test_hole_first();