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;
38 * Passing a NULL trans handle is fine here, we have a dummy root eb
39 * and the tree is a single node (level 0).
41 btrfs_setup_item_for_insert(NULL, root, &path, &key, value_len);
42 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
43 btrfs_set_file_extent_generation(leaf, fi, 1);
44 btrfs_set_file_extent_type(leaf, fi, type);
45 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
46 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_len);
47 btrfs_set_file_extent_offset(leaf, fi, offset);
48 btrfs_set_file_extent_num_bytes(leaf, fi, len);
49 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
50 btrfs_set_file_extent_compression(leaf, fi, compression);
51 btrfs_set_file_extent_encryption(leaf, fi, 0);
52 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
55 static void insert_inode_item_key(struct btrfs_root *root)
57 struct btrfs_path path;
58 struct extent_buffer *leaf = root->node;
62 memset(&path, 0, sizeof(path));
67 key.objectid = BTRFS_INODE_ITEM_KEY;
68 key.type = BTRFS_INODE_ITEM_KEY;
72 * Passing a NULL trans handle is fine here, we have a dummy root eb
73 * and the tree is a single node (level 0).
75 btrfs_setup_item_for_insert(NULL, root, &path, &key, value_len);
79 * Build the most complicated map of extents the earth has ever seen. We want
80 * this so we can test all of the corner cases of btrfs_get_extent. Here is a
81 * diagram of how the extents will look though this may not be possible we still
82 * want to make sure everything acts normally (the last number is not inclusive)
84 * [0 - 6][ 6 - 4096 ][ 4096 - 4100][4100 - 8195][8195 - 12291]
85 * [inline][hole but no extent][ hole ][ regular ][regular1 split]
87 * [12291 - 16387][16387 - 24579][24579 - 28675][ 28675 - 32771][32771 - 36867 ]
88 * [ hole ][regular1 split][ prealloc ][ prealloc1 ][prealloc1 written]
90 * [36867 - 45059][45059 - 53251][53251 - 57347][57347 - 61443][61443- 69635]
91 * [ prealloc1 ][ compressed ][ compressed1 ][ regular ][ compressed1]
93 * [69635-73731][ 73731 - 86019 ][86019-90115]
94 * [ regular ][ hole but no extent][ regular ]
96 static void setup_file_extents(struct btrfs_root *root, u32 sectorsize)
99 u64 disk_bytenr = SZ_1M;
103 * Tree-checker has strict limits on inline extents that they can only
104 * exist at file offset 0, thus we can only have one inline file extent
107 insert_extent(root, offset, 6, 6, 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 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
239 test_std_err(TEST_ALLOC_FS_INFO);
243 root = btrfs_alloc_dummy_root(fs_info);
245 test_std_err(TEST_ALLOC_ROOT);
249 root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
251 test_std_err(TEST_ALLOC_ROOT);
255 btrfs_set_header_nritems(root->node, 0);
256 btrfs_set_header_level(root->node, 0);
259 /* First with no extents */
260 BTRFS_I(inode)->root = root;
261 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, sectorsize);
264 test_err("got an error when we shouldn't have");
267 if (em->block_start != EXTENT_MAP_HOLE) {
268 test_err("expected a hole, got %llu", em->block_start);
272 btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false);
275 * All of the magic numbers are based on the mapping setup in
276 * setup_file_extents, so if you change anything there you need to
277 * update the comment and update the expected values below.
279 setup_file_extents(root, sectorsize);
281 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, (u64)-1);
283 test_err("got an error when we shouldn't have");
286 if (em->block_start != EXTENT_MAP_INLINE) {
287 test_err("expected an inline, got %llu", em->block_start);
292 * For inline extent, we always round up the em to sectorsize, as
296 * The range will be zeroed at inline extent read time.
298 * b) a file extent with unaligned bytenr
299 * Tree checker will reject it.
301 if (em->start != 0 || em->len != sectorsize) {
303 "unexpected extent wanted start 0 len %u, got start %llu len %llu",
304 sectorsize, em->start, em->len);
307 if (em->flags != 0) {
308 test_err("unexpected flags set, want 0 have %lu", em->flags);
312 * We don't test anything else for inline since it doesn't get set
313 * unless we have a page for it to write into. Maybe we should change
316 offset = em->start + em->len;
319 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
321 test_err("got an error when we shouldn't have");
324 if (em->block_start != EXTENT_MAP_HOLE) {
325 test_err("expected a hole, got %llu", em->block_start);
328 if (em->start != offset || em->len != 4) {
330 "unexpected extent wanted start %llu len 4, got start %llu len %llu",
331 offset, em->start, em->len);
334 if (em->flags != 0) {
335 test_err("unexpected flags set, want 0 have %lu", em->flags);
338 offset = em->start + em->len;
342 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
344 test_err("got an error when we shouldn't have");
347 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
348 test_err("expected a real extent, got %llu", em->block_start);
351 if (em->start != offset || em->len != sectorsize - 1) {
353 "unexpected extent wanted start %llu len 4095, got start %llu len %llu",
354 offset, em->start, em->len);
357 if (em->flags != 0) {
358 test_err("unexpected flags set, want 0 have %lu", em->flags);
361 if (em->orig_start != em->start) {
362 test_err("wrong orig offset, want %llu, have %llu", em->start,
366 offset = em->start + em->len;
369 /* The next 3 are split extents */
370 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
372 test_err("got an error when we shouldn't have");
375 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
376 test_err("expected a real extent, got %llu", em->block_start);
379 if (em->start != offset || em->len != sectorsize) {
381 "unexpected extent start %llu len %u, got start %llu len %llu",
382 offset, sectorsize, em->start, em->len);
385 if (em->flags != 0) {
386 test_err("unexpected flags set, want 0 have %lu", em->flags);
389 if (em->orig_start != em->start) {
390 test_err("wrong orig offset, want %llu, have %llu", em->start,
394 disk_bytenr = em->block_start;
395 orig_start = em->start;
396 offset = em->start + em->len;
399 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
401 test_err("got an error when we shouldn't have");
404 if (em->block_start != EXTENT_MAP_HOLE) {
405 test_err("expected a hole, got %llu", em->block_start);
408 if (em->start != offset || em->len != sectorsize) {
410 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
411 offset, sectorsize, em->start, em->len);
414 if (em->flags != 0) {
415 test_err("unexpected flags set, want 0 have %lu", em->flags);
418 offset = em->start + em->len;
421 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
423 test_err("got an error when we shouldn't have");
426 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
427 test_err("expected a real extent, got %llu", em->block_start);
430 if (em->start != offset || em->len != 2 * sectorsize) {
432 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
433 offset, 2 * sectorsize, em->start, em->len);
436 if (em->flags != 0) {
437 test_err("unexpected flags set, want 0 have %lu", em->flags);
440 if (em->orig_start != orig_start) {
441 test_err("wrong orig offset, want %llu, have %llu",
442 orig_start, em->orig_start);
445 disk_bytenr += (em->start - orig_start);
446 if (em->block_start != disk_bytenr) {
447 test_err("wrong block start, want %llu, have %llu",
448 disk_bytenr, em->block_start);
451 offset = em->start + em->len;
454 /* Prealloc extent */
455 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
457 test_err("got an error when we shouldn't have");
460 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
461 test_err("expected a real extent, got %llu", em->block_start);
464 if (em->start != offset || em->len != sectorsize) {
466 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
467 offset, sectorsize, em->start, em->len);
470 if (em->flags != prealloc_only) {
471 test_err("unexpected flags set, want %lu have %lu",
472 prealloc_only, em->flags);
475 if (em->orig_start != em->start) {
476 test_err("wrong orig offset, want %llu, have %llu", em->start,
480 offset = em->start + em->len;
483 /* The next 3 are a half written prealloc extent */
484 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
486 test_err("got an error when we shouldn't have");
489 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
490 test_err("expected a real extent, got %llu", em->block_start);
493 if (em->start != offset || em->len != sectorsize) {
495 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
496 offset, sectorsize, em->start, em->len);
499 if (em->flags != prealloc_only) {
500 test_err("unexpected flags set, want %lu have %lu",
501 prealloc_only, em->flags);
504 if (em->orig_start != em->start) {
505 test_err("wrong orig offset, want %llu, have %llu", em->start,
509 disk_bytenr = em->block_start;
510 orig_start = em->start;
511 offset = em->start + em->len;
514 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
516 test_err("got an error when we shouldn't have");
519 if (em->block_start >= EXTENT_MAP_HOLE) {
520 test_err("expected a real extent, got %llu", em->block_start);
523 if (em->start != offset || em->len != sectorsize) {
525 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
526 offset, sectorsize, em->start, em->len);
529 if (em->flags != 0) {
530 test_err("unexpected flags set, want 0 have %lu", em->flags);
533 if (em->orig_start != orig_start) {
534 test_err("unexpected orig offset, wanted %llu, have %llu",
535 orig_start, em->orig_start);
538 if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
539 test_err("unexpected block start, wanted %llu, have %llu",
540 disk_bytenr + (em->start - em->orig_start),
544 offset = em->start + em->len;
547 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
549 test_err("got an error when we shouldn't have");
552 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
553 test_err("expected a real extent, got %llu", em->block_start);
556 if (em->start != offset || em->len != 2 * sectorsize) {
558 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
559 offset, 2 * sectorsize, em->start, em->len);
562 if (em->flags != prealloc_only) {
563 test_err("unexpected flags set, want %lu have %lu",
564 prealloc_only, em->flags);
567 if (em->orig_start != orig_start) {
568 test_err("wrong orig offset, want %llu, have %llu", orig_start,
572 if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
573 test_err("unexpected block start, wanted %llu, have %llu",
574 disk_bytenr + (em->start - em->orig_start),
578 offset = em->start + em->len;
581 /* Now for the compressed extent */
582 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
584 test_err("got an error when we shouldn't have");
587 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
588 test_err("expected a real extent, got %llu", em->block_start);
591 if (em->start != offset || em->len != 2 * sectorsize) {
593 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
594 offset, 2 * sectorsize, em->start, em->len);
597 if (em->flags != compressed_only) {
598 test_err("unexpected flags set, want %lu have %lu",
599 compressed_only, em->flags);
602 if (em->orig_start != em->start) {
603 test_err("wrong orig offset, want %llu, have %llu",
604 em->start, em->orig_start);
607 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
608 test_err("unexpected compress type, wanted %d, got %d",
609 BTRFS_COMPRESS_ZLIB, em->compress_type);
612 offset = em->start + em->len;
615 /* Split compressed extent */
616 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
618 test_err("got an error when we shouldn't have");
621 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
622 test_err("expected a real extent, got %llu", em->block_start);
625 if (em->start != offset || em->len != sectorsize) {
627 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
628 offset, sectorsize, em->start, em->len);
631 if (em->flags != compressed_only) {
632 test_err("unexpected flags set, want %lu have %lu",
633 compressed_only, em->flags);
636 if (em->orig_start != em->start) {
637 test_err("wrong orig offset, want %llu, have %llu",
638 em->start, em->orig_start);
641 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
642 test_err("unexpected compress type, wanted %d, got %d",
643 BTRFS_COMPRESS_ZLIB, em->compress_type);
646 disk_bytenr = em->block_start;
647 orig_start = em->start;
648 offset = em->start + em->len;
651 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
653 test_err("got an error when we shouldn't have");
656 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
657 test_err("expected a real extent, got %llu", em->block_start);
660 if (em->start != offset || em->len != sectorsize) {
662 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
663 offset, sectorsize, em->start, em->len);
666 if (em->flags != 0) {
667 test_err("unexpected flags set, want 0 have %lu", em->flags);
670 if (em->orig_start != em->start) {
671 test_err("wrong orig offset, want %llu, have %llu", em->start,
675 offset = em->start + em->len;
678 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
680 test_err("got an error when we shouldn't have");
683 if (em->block_start != disk_bytenr) {
684 test_err("block start does not match, want %llu got %llu",
685 disk_bytenr, em->block_start);
688 if (em->start != offset || em->len != 2 * sectorsize) {
690 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
691 offset, 2 * sectorsize, em->start, em->len);
694 if (em->flags != compressed_only) {
695 test_err("unexpected flags set, want %lu have %lu",
696 compressed_only, em->flags);
699 if (em->orig_start != orig_start) {
700 test_err("wrong orig offset, want %llu, have %llu",
701 em->start, orig_start);
704 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
705 test_err("unexpected compress type, wanted %d, got %d",
706 BTRFS_COMPRESS_ZLIB, em->compress_type);
709 offset = em->start + em->len;
712 /* A hole between regular extents but no hole extent */
713 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset + 6, sectorsize);
715 test_err("got an error when we shouldn't have");
718 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
719 test_err("expected a real extent, got %llu", em->block_start);
722 if (em->start != offset || em->len != sectorsize) {
724 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
725 offset, sectorsize, em->start, em->len);
728 if (em->flags != 0) {
729 test_err("unexpected flags set, want 0 have %lu", em->flags);
732 if (em->orig_start != em->start) {
733 test_err("wrong orig offset, want %llu, have %llu", em->start,
737 offset = em->start + em->len;
740 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, SZ_4M);
742 test_err("got an error when we shouldn't have");
745 if (em->block_start != EXTENT_MAP_HOLE) {
746 test_err("expected a hole extent, got %llu", em->block_start);
750 * Currently we just return a length that we requested rather than the
751 * length of the actual hole, if this changes we'll have to change this
754 if (em->start != offset || em->len != 3 * sectorsize) {
756 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
757 offset, 3 * sectorsize, em->start, em->len);
760 if (em->flags != vacancy_only) {
761 test_err("unexpected flags set, want %lu have %lu",
762 vacancy_only, em->flags);
765 if (em->orig_start != em->start) {
766 test_err("wrong orig offset, want %llu, have %llu", em->start,
770 offset = em->start + em->len;
773 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
775 test_err("got an error when we shouldn't have");
778 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
779 test_err("expected a real extent, got %llu", em->block_start);
782 if (em->start != offset || em->len != sectorsize) {
784 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
785 offset, sectorsize, em->start, em->len);
788 if (em->flags != 0) {
789 test_err("unexpected flags set, want 0 have %lu", em->flags);
792 if (em->orig_start != em->start) {
793 test_err("wrong orig offset, want %llu, have %llu", em->start,
802 btrfs_free_dummy_root(root);
803 btrfs_free_dummy_fs_info(fs_info);
807 static int test_hole_first(u32 sectorsize, u32 nodesize)
809 struct btrfs_fs_info *fs_info = NULL;
810 struct inode *inode = NULL;
811 struct btrfs_root *root = NULL;
812 struct extent_map *em = NULL;
815 test_msg("running hole first btrfs_get_extent test");
817 inode = btrfs_new_test_inode();
819 test_std_err(TEST_ALLOC_INODE);
823 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
825 test_std_err(TEST_ALLOC_FS_INFO);
829 root = btrfs_alloc_dummy_root(fs_info);
831 test_std_err(TEST_ALLOC_ROOT);
835 root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
837 test_std_err(TEST_ALLOC_ROOT);
841 btrfs_set_header_nritems(root->node, 0);
842 btrfs_set_header_level(root->node, 0);
843 BTRFS_I(inode)->root = root;
847 * Need a blank inode item here just so we don't confuse
850 insert_inode_item_key(root);
851 insert_extent(root, sectorsize, sectorsize, sectorsize, 0, sectorsize,
852 sectorsize, BTRFS_FILE_EXTENT_REG, 0, 1);
853 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, 2 * sectorsize);
855 test_err("got an error when we shouldn't have");
858 if (em->block_start != EXTENT_MAP_HOLE) {
859 test_err("expected a hole, got %llu", em->block_start);
862 if (em->start != 0 || em->len != sectorsize) {
864 "unexpected extent wanted start 0 len %u, got start %llu len %llu",
865 sectorsize, em->start, em->len);
868 if (em->flags != vacancy_only) {
869 test_err("wrong flags, wanted %lu, have %lu", vacancy_only,
875 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, sectorsize, 2 * sectorsize);
877 test_err("got an error when we shouldn't have");
880 if (em->block_start != sectorsize) {
881 test_err("expected a real extent, got %llu", em->block_start);
884 if (em->start != sectorsize || em->len != sectorsize) {
886 "unexpected extent wanted start %u len %u, got start %llu len %llu",
887 sectorsize, sectorsize, em->start, em->len);
890 if (em->flags != 0) {
891 test_err("unexpected flags set, wanted 0 got %lu",
900 btrfs_free_dummy_root(root);
901 btrfs_free_dummy_fs_info(fs_info);
905 static int test_extent_accounting(u32 sectorsize, u32 nodesize)
907 struct btrfs_fs_info *fs_info = NULL;
908 struct inode *inode = NULL;
909 struct btrfs_root *root = NULL;
912 test_msg("running outstanding_extents tests");
914 inode = btrfs_new_test_inode();
916 test_std_err(TEST_ALLOC_INODE);
920 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
922 test_std_err(TEST_ALLOC_FS_INFO);
926 root = btrfs_alloc_dummy_root(fs_info);
928 test_std_err(TEST_ALLOC_ROOT);
932 BTRFS_I(inode)->root = root;
934 /* [BTRFS_MAX_EXTENT_SIZE] */
935 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), 0,
936 BTRFS_MAX_EXTENT_SIZE - 1, 0, NULL);
938 test_err("btrfs_set_extent_delalloc returned %d", ret);
941 if (BTRFS_I(inode)->outstanding_extents != 1) {
943 test_err("miscount, wanted 1, got %u",
944 BTRFS_I(inode)->outstanding_extents);
948 /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
949 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), BTRFS_MAX_EXTENT_SIZE,
950 BTRFS_MAX_EXTENT_SIZE + sectorsize - 1,
953 test_err("btrfs_set_extent_delalloc returned %d", ret);
956 if (BTRFS_I(inode)->outstanding_extents != 2) {
958 test_err("miscount, wanted 2, got %u",
959 BTRFS_I(inode)->outstanding_extents);
963 /* [BTRFS_MAX_EXTENT_SIZE/2][sectorsize HOLE][the rest] */
964 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
965 BTRFS_MAX_EXTENT_SIZE >> 1,
966 (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1,
967 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
968 EXTENT_UPTODATE, NULL);
970 test_err("clear_extent_bit returned %d", ret);
973 if (BTRFS_I(inode)->outstanding_extents != 2) {
975 test_err("miscount, wanted 2, got %u",
976 BTRFS_I(inode)->outstanding_extents);
980 /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
981 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), BTRFS_MAX_EXTENT_SIZE >> 1,
982 (BTRFS_MAX_EXTENT_SIZE >> 1)
986 test_err("btrfs_set_extent_delalloc returned %d", ret);
989 if (BTRFS_I(inode)->outstanding_extents != 2) {
991 test_err("miscount, wanted 2, got %u",
992 BTRFS_I(inode)->outstanding_extents);
997 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize]
999 ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
1000 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize,
1001 (BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1,
1004 test_err("btrfs_set_extent_delalloc returned %d", ret);
1007 if (BTRFS_I(inode)->outstanding_extents != 4) {
1009 test_err("miscount, wanted 4, got %u",
1010 BTRFS_I(inode)->outstanding_extents);
1015 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize]
1017 ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
1018 BTRFS_MAX_EXTENT_SIZE + sectorsize,
1019 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL);
1021 test_err("btrfs_set_extent_delalloc returned %d", ret);
1024 if (BTRFS_I(inode)->outstanding_extents != 3) {
1026 test_err("miscount, wanted 3, got %u",
1027 BTRFS_I(inode)->outstanding_extents);
1031 /* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */
1032 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
1033 BTRFS_MAX_EXTENT_SIZE + sectorsize,
1034 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1,
1035 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1036 EXTENT_UPTODATE, NULL);
1038 test_err("clear_extent_bit returned %d", ret);
1041 if (BTRFS_I(inode)->outstanding_extents != 4) {
1043 test_err("miscount, wanted 4, got %u",
1044 BTRFS_I(inode)->outstanding_extents);
1049 * Refill the hole again just for good measure, because I thought it
1050 * might fail and I'd rather satisfy my paranoia at this point.
1052 ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
1053 BTRFS_MAX_EXTENT_SIZE + sectorsize,
1054 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL);
1056 test_err("btrfs_set_extent_delalloc returned %d", ret);
1059 if (BTRFS_I(inode)->outstanding_extents != 3) {
1061 test_err("miscount, wanted 3, got %u",
1062 BTRFS_I(inode)->outstanding_extents);
1067 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1068 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1069 EXTENT_UPTODATE, NULL);
1071 test_err("clear_extent_bit returned %d", ret);
1074 if (BTRFS_I(inode)->outstanding_extents) {
1076 test_err("miscount, wanted 0, got %u",
1077 BTRFS_I(inode)->outstanding_extents);
1083 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1084 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1085 EXTENT_UPTODATE, NULL);
1087 btrfs_free_dummy_root(root);
1088 btrfs_free_dummy_fs_info(fs_info);
1092 int btrfs_test_inodes(u32 sectorsize, u32 nodesize)
1096 test_msg("running inode tests");
1098 set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
1099 set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
1101 ret = test_btrfs_get_extent(sectorsize, nodesize);
1104 ret = test_hole_first(sectorsize, nodesize);
1107 return test_extent_accounting(sectorsize, nodesize);