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"
14 #include "../accessors.h"
16 static void insert_extent(struct btrfs_root *root, u64 start, u64 len,
17 u64 ram_bytes, u64 offset, u64 disk_bytenr,
18 u64 disk_len, u32 type, u8 compression, int slot)
20 struct btrfs_path path;
21 struct btrfs_file_extent_item *fi;
22 struct extent_buffer *leaf = root->node;
24 u32 value_len = sizeof(struct btrfs_file_extent_item);
26 if (type == BTRFS_FILE_EXTENT_INLINE)
28 memset(&path, 0, sizeof(path));
33 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
34 key.type = BTRFS_EXTENT_DATA_KEY;
37 btrfs_setup_item_for_insert(root, &path, &key, value_len);
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 btrfs_setup_item_for_insert(root, &path, &key, value_len);
71 * Build the most complicated map of extents the earth has ever seen. We want
72 * this so we can test all of the corner cases of btrfs_get_extent. Here is a
73 * diagram of how the extents will look though this may not be possible we still
74 * want to make sure everything acts normally (the last number is not inclusive)
76 * [0 - 6][ 6 - 4096 ][ 4096 - 4100][4100 - 8195][8195 - 12291]
77 * [inline][hole but no extent][ hole ][ regular ][regular1 split]
79 * [12291 - 16387][16387 - 24579][24579 - 28675][ 28675 - 32771][32771 - 36867 ]
80 * [ hole ][regular1 split][ prealloc ][ prealloc1 ][prealloc1 written]
82 * [36867 - 45059][45059 - 53251][53251 - 57347][57347 - 61443][61443- 69635]
83 * [ prealloc1 ][ compressed ][ compressed1 ][ regular ][ compressed1]
85 * [69635-73731][ 73731 - 86019 ][86019-90115]
86 * [ regular ][ hole but no extent][ regular ]
88 static void setup_file_extents(struct btrfs_root *root, u32 sectorsize)
91 u64 disk_bytenr = SZ_1M;
95 * Tree-checker has strict limits on inline extents that they can only
96 * exist at file offset 0, thus we can only have one inline file extent
99 insert_extent(root, offset, 6, 6, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE, 0,
104 /* Now another hole */
105 insert_extent(root, offset, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
110 /* Now for a regular extent */
111 insert_extent(root, offset, sectorsize - 1, sectorsize - 1, 0,
112 disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
114 disk_bytenr += sectorsize;
115 offset += sectorsize - 1;
118 * Now for 3 extents that were split from a hole punch so we test
121 insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
122 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
124 offset += sectorsize;
125 insert_extent(root, offset, sectorsize, sectorsize, 0, 0, 0,
126 BTRFS_FILE_EXTENT_REG, 0, slot);
128 offset += sectorsize;
129 insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
130 2 * sectorsize, disk_bytenr, 4 * sectorsize,
131 BTRFS_FILE_EXTENT_REG, 0, slot);
133 offset += 2 * sectorsize;
134 disk_bytenr += 4 * sectorsize;
136 /* Now for a unwritten prealloc extent */
137 insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
138 sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
140 offset += sectorsize;
143 * We want to jack up disk_bytenr a little more so the em stuff doesn't
146 disk_bytenr += 2 * sectorsize;
149 * Now for a partially written prealloc extent, basically the same as
150 * the hole punch example above. Ram_bytes never changes when you mark
151 * extents written btw.
153 insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
154 4 * sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
156 offset += sectorsize;
157 insert_extent(root, offset, sectorsize, 4 * sectorsize, sectorsize,
158 disk_bytenr, 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0,
161 offset += sectorsize;
162 insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
163 2 * sectorsize, disk_bytenr, 4 * sectorsize,
164 BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
166 offset += 2 * sectorsize;
167 disk_bytenr += 4 * sectorsize;
169 /* Now a normal compressed extent */
170 insert_extent(root, offset, 2 * sectorsize, 2 * sectorsize, 0,
171 disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG,
172 BTRFS_COMPRESS_ZLIB, slot);
174 offset += 2 * sectorsize;
176 disk_bytenr += 2 * sectorsize;
178 /* Now a split compressed extent */
179 insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
180 sectorsize, BTRFS_FILE_EXTENT_REG,
181 BTRFS_COMPRESS_ZLIB, slot);
183 offset += sectorsize;
184 insert_extent(root, offset, sectorsize, sectorsize, 0,
185 disk_bytenr + sectorsize, sectorsize,
186 BTRFS_FILE_EXTENT_REG, 0, slot);
188 offset += sectorsize;
189 insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
190 2 * sectorsize, disk_bytenr, sectorsize,
191 BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
193 offset += 2 * sectorsize;
194 disk_bytenr += 2 * sectorsize;
196 /* Now extents that have a hole but no hole extent */
197 insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
198 sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
200 offset += 4 * sectorsize;
201 disk_bytenr += sectorsize;
202 insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
203 sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
206 static unsigned long prealloc_only = 0;
207 static unsigned long compressed_only = 0;
208 static unsigned long vacancy_only = 0;
210 static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize)
212 struct btrfs_fs_info *fs_info = NULL;
213 struct inode *inode = NULL;
214 struct btrfs_root *root = NULL;
215 struct extent_map *em = NULL;
221 test_msg("running btrfs_get_extent tests");
223 inode = btrfs_new_test_inode();
225 test_std_err(TEST_ALLOC_INODE);
229 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
231 test_std_err(TEST_ALLOC_FS_INFO);
235 root = btrfs_alloc_dummy_root(fs_info);
237 test_std_err(TEST_ALLOC_ROOT);
241 root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
243 test_std_err(TEST_ALLOC_ROOT);
247 btrfs_set_header_nritems(root->node, 0);
248 btrfs_set_header_level(root->node, 0);
251 /* First with no extents */
252 BTRFS_I(inode)->root = root;
253 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, sectorsize);
256 test_err("got an error when we shouldn't have");
259 if (em->block_start != EXTENT_MAP_HOLE) {
260 test_err("expected a hole, got %llu", em->block_start);
264 btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false);
267 * All of the magic numbers are based on the mapping setup in
268 * setup_file_extents, so if you change anything there you need to
269 * update the comment and update the expected values below.
271 setup_file_extents(root, sectorsize);
273 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, (u64)-1);
275 test_err("got an error when we shouldn't have");
278 if (em->block_start != EXTENT_MAP_INLINE) {
279 test_err("expected an inline, got %llu", em->block_start);
284 * For inline extent, we always round up the em to sectorsize, as
288 * The range will be zeroed at inline extent read time.
290 * b) a file extent with unaligned bytenr
291 * Tree checker will reject it.
293 if (em->start != 0 || em->len != sectorsize) {
295 "unexpected extent wanted start 0 len %u, got start %llu len %llu",
296 sectorsize, em->start, em->len);
299 if (em->flags != 0) {
300 test_err("unexpected flags set, want 0 have %lu", em->flags);
304 * We don't test anything else for inline since it doesn't get set
305 * unless we have a page for it to write into. Maybe we should change
308 offset = em->start + em->len;
311 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
313 test_err("got an error when we shouldn't have");
316 if (em->block_start != EXTENT_MAP_HOLE) {
317 test_err("expected a hole, got %llu", em->block_start);
320 if (em->start != offset || em->len != 4) {
322 "unexpected extent wanted start %llu len 4, got start %llu len %llu",
323 offset, em->start, em->len);
326 if (em->flags != 0) {
327 test_err("unexpected flags set, want 0 have %lu", em->flags);
330 offset = em->start + em->len;
334 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
336 test_err("got an error when we shouldn't have");
339 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
340 test_err("expected a real extent, got %llu", em->block_start);
343 if (em->start != offset || em->len != sectorsize - 1) {
345 "unexpected extent wanted start %llu len 4095, got start %llu len %llu",
346 offset, em->start, em->len);
349 if (em->flags != 0) {
350 test_err("unexpected flags set, want 0 have %lu", em->flags);
353 if (em->orig_start != em->start) {
354 test_err("wrong orig offset, want %llu, have %llu", em->start,
358 offset = em->start + em->len;
361 /* The next 3 are split extents */
362 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
364 test_err("got an error when we shouldn't have");
367 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
368 test_err("expected a real extent, got %llu", em->block_start);
371 if (em->start != offset || em->len != sectorsize) {
373 "unexpected extent start %llu len %u, got start %llu len %llu",
374 offset, sectorsize, em->start, em->len);
377 if (em->flags != 0) {
378 test_err("unexpected flags set, want 0 have %lu", em->flags);
381 if (em->orig_start != em->start) {
382 test_err("wrong orig offset, want %llu, have %llu", em->start,
386 disk_bytenr = em->block_start;
387 orig_start = em->start;
388 offset = em->start + em->len;
391 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
393 test_err("got an error when we shouldn't have");
396 if (em->block_start != EXTENT_MAP_HOLE) {
397 test_err("expected a hole, got %llu", em->block_start);
400 if (em->start != offset || em->len != sectorsize) {
402 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
403 offset, sectorsize, em->start, em->len);
406 if (em->flags != 0) {
407 test_err("unexpected flags set, want 0 have %lu", em->flags);
410 offset = em->start + em->len;
413 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
415 test_err("got an error when we shouldn't have");
418 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
419 test_err("expected a real extent, got %llu", em->block_start);
422 if (em->start != offset || em->len != 2 * sectorsize) {
424 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
425 offset, 2 * sectorsize, em->start, em->len);
428 if (em->flags != 0) {
429 test_err("unexpected flags set, want 0 have %lu", em->flags);
432 if (em->orig_start != orig_start) {
433 test_err("wrong orig offset, want %llu, have %llu",
434 orig_start, em->orig_start);
437 disk_bytenr += (em->start - orig_start);
438 if (em->block_start != disk_bytenr) {
439 test_err("wrong block start, want %llu, have %llu",
440 disk_bytenr, em->block_start);
443 offset = em->start + em->len;
446 /* Prealloc extent */
447 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
449 test_err("got an error when we shouldn't have");
452 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
453 test_err("expected a real extent, got %llu", em->block_start);
456 if (em->start != offset || em->len != sectorsize) {
458 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
459 offset, sectorsize, em->start, em->len);
462 if (em->flags != prealloc_only) {
463 test_err("unexpected flags set, want %lu have %lu",
464 prealloc_only, em->flags);
467 if (em->orig_start != em->start) {
468 test_err("wrong orig offset, want %llu, have %llu", em->start,
472 offset = em->start + em->len;
475 /* The next 3 are a half written prealloc extent */
476 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
478 test_err("got an error when we shouldn't have");
481 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
482 test_err("expected a real extent, got %llu", em->block_start);
485 if (em->start != offset || em->len != sectorsize) {
487 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
488 offset, sectorsize, em->start, em->len);
491 if (em->flags != prealloc_only) {
492 test_err("unexpected flags set, want %lu have %lu",
493 prealloc_only, em->flags);
496 if (em->orig_start != em->start) {
497 test_err("wrong orig offset, want %llu, have %llu", em->start,
501 disk_bytenr = em->block_start;
502 orig_start = em->start;
503 offset = em->start + em->len;
506 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
508 test_err("got an error when we shouldn't have");
511 if (em->block_start >= EXTENT_MAP_HOLE) {
512 test_err("expected a real extent, got %llu", em->block_start);
515 if (em->start != offset || em->len != sectorsize) {
517 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
518 offset, sectorsize, em->start, em->len);
521 if (em->flags != 0) {
522 test_err("unexpected flags set, want 0 have %lu", em->flags);
525 if (em->orig_start != orig_start) {
526 test_err("unexpected orig offset, wanted %llu, have %llu",
527 orig_start, em->orig_start);
530 if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
531 test_err("unexpected block start, wanted %llu, have %llu",
532 disk_bytenr + (em->start - em->orig_start),
536 offset = em->start + em->len;
539 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
541 test_err("got an error when we shouldn't have");
544 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
545 test_err("expected a real extent, got %llu", em->block_start);
548 if (em->start != offset || em->len != 2 * sectorsize) {
550 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
551 offset, 2 * sectorsize, em->start, em->len);
554 if (em->flags != prealloc_only) {
555 test_err("unexpected flags set, want %lu have %lu",
556 prealloc_only, em->flags);
559 if (em->orig_start != orig_start) {
560 test_err("wrong orig offset, want %llu, have %llu", orig_start,
564 if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
565 test_err("unexpected block start, wanted %llu, have %llu",
566 disk_bytenr + (em->start - em->orig_start),
570 offset = em->start + em->len;
573 /* Now for the compressed extent */
574 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
576 test_err("got an error when we shouldn't have");
579 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
580 test_err("expected a real extent, got %llu", em->block_start);
583 if (em->start != offset || em->len != 2 * sectorsize) {
585 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
586 offset, 2 * sectorsize, em->start, em->len);
589 if (em->flags != compressed_only) {
590 test_err("unexpected flags set, want %lu have %lu",
591 compressed_only, em->flags);
594 if (em->orig_start != em->start) {
595 test_err("wrong orig offset, want %llu, have %llu",
596 em->start, em->orig_start);
599 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
600 test_err("unexpected compress type, wanted %d, got %d",
601 BTRFS_COMPRESS_ZLIB, em->compress_type);
604 offset = em->start + em->len;
607 /* Split compressed extent */
608 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
610 test_err("got an error when we shouldn't have");
613 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
614 test_err("expected a real extent, got %llu", em->block_start);
617 if (em->start != offset || em->len != sectorsize) {
619 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
620 offset, sectorsize, em->start, em->len);
623 if (em->flags != compressed_only) {
624 test_err("unexpected flags set, want %lu have %lu",
625 compressed_only, em->flags);
628 if (em->orig_start != em->start) {
629 test_err("wrong orig offset, want %llu, have %llu",
630 em->start, em->orig_start);
633 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
634 test_err("unexpected compress type, wanted %d, got %d",
635 BTRFS_COMPRESS_ZLIB, em->compress_type);
638 disk_bytenr = em->block_start;
639 orig_start = em->start;
640 offset = em->start + em->len;
643 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
645 test_err("got an error when we shouldn't have");
648 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
649 test_err("expected a real extent, got %llu", em->block_start);
652 if (em->start != offset || em->len != sectorsize) {
654 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
655 offset, sectorsize, em->start, em->len);
658 if (em->flags != 0) {
659 test_err("unexpected flags set, want 0 have %lu", em->flags);
662 if (em->orig_start != em->start) {
663 test_err("wrong orig offset, want %llu, have %llu", em->start,
667 offset = em->start + em->len;
670 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
672 test_err("got an error when we shouldn't have");
675 if (em->block_start != disk_bytenr) {
676 test_err("block start does not match, want %llu got %llu",
677 disk_bytenr, em->block_start);
680 if (em->start != offset || em->len != 2 * sectorsize) {
682 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
683 offset, 2 * sectorsize, em->start, em->len);
686 if (em->flags != compressed_only) {
687 test_err("unexpected flags set, want %lu have %lu",
688 compressed_only, em->flags);
691 if (em->orig_start != orig_start) {
692 test_err("wrong orig offset, want %llu, have %llu",
693 em->start, orig_start);
696 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
697 test_err("unexpected compress type, wanted %d, got %d",
698 BTRFS_COMPRESS_ZLIB, em->compress_type);
701 offset = em->start + em->len;
704 /* A hole between regular extents but no hole extent */
705 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset + 6, sectorsize);
707 test_err("got an error when we shouldn't have");
710 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
711 test_err("expected a real extent, got %llu", em->block_start);
714 if (em->start != offset || em->len != sectorsize) {
716 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
717 offset, sectorsize, em->start, em->len);
720 if (em->flags != 0) {
721 test_err("unexpected flags set, want 0 have %lu", em->flags);
724 if (em->orig_start != em->start) {
725 test_err("wrong orig offset, want %llu, have %llu", em->start,
729 offset = em->start + em->len;
732 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, SZ_4M);
734 test_err("got an error when we shouldn't have");
737 if (em->block_start != EXTENT_MAP_HOLE) {
738 test_err("expected a hole extent, got %llu", em->block_start);
742 * Currently we just return a length that we requested rather than the
743 * length of the actual hole, if this changes we'll have to change this
746 if (em->start != offset || em->len != 3 * sectorsize) {
748 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
749 offset, 3 * sectorsize, em->start, em->len);
752 if (em->flags != vacancy_only) {
753 test_err("unexpected flags set, want %lu have %lu",
754 vacancy_only, em->flags);
757 if (em->orig_start != em->start) {
758 test_err("wrong orig offset, want %llu, have %llu", em->start,
762 offset = em->start + em->len;
765 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
767 test_err("got an error when we shouldn't have");
770 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
771 test_err("expected a real extent, got %llu", em->block_start);
774 if (em->start != offset || em->len != sectorsize) {
776 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
777 offset, sectorsize, em->start, em->len);
780 if (em->flags != 0) {
781 test_err("unexpected flags set, want 0 have %lu", em->flags);
784 if (em->orig_start != em->start) {
785 test_err("wrong orig offset, want %llu, have %llu", em->start,
794 btrfs_free_dummy_root(root);
795 btrfs_free_dummy_fs_info(fs_info);
799 static int test_hole_first(u32 sectorsize, u32 nodesize)
801 struct btrfs_fs_info *fs_info = NULL;
802 struct inode *inode = NULL;
803 struct btrfs_root *root = NULL;
804 struct extent_map *em = NULL;
807 test_msg("running hole first btrfs_get_extent test");
809 inode = btrfs_new_test_inode();
811 test_std_err(TEST_ALLOC_INODE);
815 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
817 test_std_err(TEST_ALLOC_FS_INFO);
821 root = btrfs_alloc_dummy_root(fs_info);
823 test_std_err(TEST_ALLOC_ROOT);
827 root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
829 test_std_err(TEST_ALLOC_ROOT);
833 btrfs_set_header_nritems(root->node, 0);
834 btrfs_set_header_level(root->node, 0);
835 BTRFS_I(inode)->root = root;
839 * Need a blank inode item here just so we don't confuse
842 insert_inode_item_key(root);
843 insert_extent(root, sectorsize, sectorsize, sectorsize, 0, sectorsize,
844 sectorsize, BTRFS_FILE_EXTENT_REG, 0, 1);
845 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, 2 * sectorsize);
847 test_err("got an error when we shouldn't have");
850 if (em->block_start != EXTENT_MAP_HOLE) {
851 test_err("expected a hole, got %llu", em->block_start);
854 if (em->start != 0 || em->len != sectorsize) {
856 "unexpected extent wanted start 0 len %u, got start %llu len %llu",
857 sectorsize, em->start, em->len);
860 if (em->flags != vacancy_only) {
861 test_err("wrong flags, wanted %lu, have %lu", vacancy_only,
867 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, sectorsize, 2 * sectorsize);
869 test_err("got an error when we shouldn't have");
872 if (em->block_start != sectorsize) {
873 test_err("expected a real extent, got %llu", em->block_start);
876 if (em->start != sectorsize || em->len != sectorsize) {
878 "unexpected extent wanted start %u len %u, got start %llu len %llu",
879 sectorsize, sectorsize, em->start, em->len);
882 if (em->flags != 0) {
883 test_err("unexpected flags set, wanted 0 got %lu",
892 btrfs_free_dummy_root(root);
893 btrfs_free_dummy_fs_info(fs_info);
897 static int test_extent_accounting(u32 sectorsize, u32 nodesize)
899 struct btrfs_fs_info *fs_info = NULL;
900 struct inode *inode = NULL;
901 struct btrfs_root *root = NULL;
904 test_msg("running outstanding_extents tests");
906 inode = btrfs_new_test_inode();
908 test_std_err(TEST_ALLOC_INODE);
912 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
914 test_std_err(TEST_ALLOC_FS_INFO);
918 root = btrfs_alloc_dummy_root(fs_info);
920 test_std_err(TEST_ALLOC_ROOT);
924 BTRFS_I(inode)->root = root;
926 /* [BTRFS_MAX_EXTENT_SIZE] */
927 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), 0,
928 BTRFS_MAX_EXTENT_SIZE - 1, 0, NULL);
930 test_err("btrfs_set_extent_delalloc returned %d", ret);
933 if (BTRFS_I(inode)->outstanding_extents != 1) {
935 test_err("miscount, wanted 1, got %u",
936 BTRFS_I(inode)->outstanding_extents);
940 /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
941 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), BTRFS_MAX_EXTENT_SIZE,
942 BTRFS_MAX_EXTENT_SIZE + sectorsize - 1,
945 test_err("btrfs_set_extent_delalloc returned %d", ret);
948 if (BTRFS_I(inode)->outstanding_extents != 2) {
950 test_err("miscount, wanted 2, got %u",
951 BTRFS_I(inode)->outstanding_extents);
955 /* [BTRFS_MAX_EXTENT_SIZE/2][sectorsize HOLE][the rest] */
956 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
957 BTRFS_MAX_EXTENT_SIZE >> 1,
958 (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1,
959 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
960 EXTENT_UPTODATE, NULL);
962 test_err("clear_extent_bit returned %d", ret);
965 if (BTRFS_I(inode)->outstanding_extents != 2) {
967 test_err("miscount, wanted 2, got %u",
968 BTRFS_I(inode)->outstanding_extents);
972 /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
973 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), BTRFS_MAX_EXTENT_SIZE >> 1,
974 (BTRFS_MAX_EXTENT_SIZE >> 1)
978 test_err("btrfs_set_extent_delalloc returned %d", ret);
981 if (BTRFS_I(inode)->outstanding_extents != 2) {
983 test_err("miscount, wanted 2, got %u",
984 BTRFS_I(inode)->outstanding_extents);
989 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize]
991 ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
992 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize,
993 (BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1,
996 test_err("btrfs_set_extent_delalloc returned %d", ret);
999 if (BTRFS_I(inode)->outstanding_extents != 4) {
1001 test_err("miscount, wanted 4, got %u",
1002 BTRFS_I(inode)->outstanding_extents);
1007 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize]
1009 ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
1010 BTRFS_MAX_EXTENT_SIZE + sectorsize,
1011 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL);
1013 test_err("btrfs_set_extent_delalloc returned %d", ret);
1016 if (BTRFS_I(inode)->outstanding_extents != 3) {
1018 test_err("miscount, wanted 3, got %u",
1019 BTRFS_I(inode)->outstanding_extents);
1023 /* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */
1024 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
1025 BTRFS_MAX_EXTENT_SIZE + sectorsize,
1026 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1,
1027 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1028 EXTENT_UPTODATE, NULL);
1030 test_err("clear_extent_bit returned %d", ret);
1033 if (BTRFS_I(inode)->outstanding_extents != 4) {
1035 test_err("miscount, wanted 4, got %u",
1036 BTRFS_I(inode)->outstanding_extents);
1041 * Refill the hole again just for good measure, because I thought it
1042 * might fail and I'd rather satisfy my paranoia at this point.
1044 ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
1045 BTRFS_MAX_EXTENT_SIZE + sectorsize,
1046 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL);
1048 test_err("btrfs_set_extent_delalloc returned %d", ret);
1051 if (BTRFS_I(inode)->outstanding_extents != 3) {
1053 test_err("miscount, wanted 3, got %u",
1054 BTRFS_I(inode)->outstanding_extents);
1059 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1060 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1061 EXTENT_UPTODATE, NULL);
1063 test_err("clear_extent_bit returned %d", ret);
1066 if (BTRFS_I(inode)->outstanding_extents) {
1068 test_err("miscount, wanted 0, got %u",
1069 BTRFS_I(inode)->outstanding_extents);
1075 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1076 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1077 EXTENT_UPTODATE, NULL);
1079 btrfs_free_dummy_root(root);
1080 btrfs_free_dummy_fs_info(fs_info);
1084 int btrfs_test_inodes(u32 sectorsize, u32 nodesize)
1088 test_msg("running inode tests");
1090 set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
1091 set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
1093 ret = test_btrfs_get_extent(sectorsize, nodesize);
1096 ret = test_hole_first(sectorsize, nodesize);
1099 return test_extent_accounting(sectorsize, nodesize);