1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2013 Fusion IO. All rights reserved.
6 #include <linux/types.h>
7 #include "btrfs-tests.h"
9 #include "../btrfs_inode.h"
10 #include "../disk-io.h"
11 #include "../extent_io.h"
12 #include "../volumes.h"
13 #include "../compression.h"
15 static void insert_extent(struct btrfs_root *root, u64 start, u64 len,
16 u64 ram_bytes, u64 offset, u64 disk_bytenr,
17 u64 disk_len, u32 type, u8 compression, int slot)
19 struct btrfs_path path;
20 struct btrfs_file_extent_item *fi;
21 struct extent_buffer *leaf = root->node;
23 u32 value_len = sizeof(struct btrfs_file_extent_item);
25 if (type == BTRFS_FILE_EXTENT_INLINE)
27 memset(&path, 0, sizeof(path));
32 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
33 key.type = BTRFS_EXTENT_DATA_KEY;
36 setup_items_for_insert(root, &path, &key, &value_len, 1);
37 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
38 btrfs_set_file_extent_generation(leaf, fi, 1);
39 btrfs_set_file_extent_type(leaf, fi, type);
40 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
41 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_len);
42 btrfs_set_file_extent_offset(leaf, fi, offset);
43 btrfs_set_file_extent_num_bytes(leaf, fi, len);
44 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
45 btrfs_set_file_extent_compression(leaf, fi, compression);
46 btrfs_set_file_extent_encryption(leaf, fi, 0);
47 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
50 static void insert_inode_item_key(struct btrfs_root *root)
52 struct btrfs_path path;
53 struct extent_buffer *leaf = root->node;
57 memset(&path, 0, sizeof(path));
62 key.objectid = BTRFS_INODE_ITEM_KEY;
63 key.type = BTRFS_INODE_ITEM_KEY;
66 setup_items_for_insert(root, &path, &key, &value_len, 1);
70 * Build the most complicated map of extents the earth has ever seen. We want
71 * this so we can test all of the corner cases of btrfs_get_extent. Here is a
72 * diagram of how the extents will look though this may not be possible we still
73 * want to make sure everything acts normally (the last number is not inclusive)
75 * [0 - 5][5 - 6][ 6 - 4096 ][ 4096 - 4100][4100 - 8195][8195 - 12291]
76 * [hole ][inline][hole but no extent][ hole ][ regular ][regular1 split]
78 * [12291 - 16387][16387 - 24579][24579 - 28675][ 28675 - 32771][32771 - 36867 ]
79 * [ hole ][regular1 split][ prealloc ][ prealloc1 ][prealloc1 written]
81 * [36867 - 45059][45059 - 53251][53251 - 57347][57347 - 61443][61443- 69635]
82 * [ prealloc1 ][ compressed ][ compressed1 ][ regular ][ compressed1]
84 * [69635-73731][ 73731 - 86019 ][86019-90115]
85 * [ regular ][ hole but no extent][ regular ]
87 static void setup_file_extents(struct btrfs_root *root, u32 sectorsize)
90 u64 disk_bytenr = SZ_1M;
93 /* First we want a hole */
94 insert_extent(root, offset, 5, 5, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
100 * Now we want an inline extent, I don't think this is possible but hey
101 * why not? Also keep in mind if we have an inline extent it counts as
102 * the whole first page. If we were to expand it we would have to cow
103 * and we wouldn't have an inline extent anymore.
105 insert_extent(root, offset, 1, 1, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE, 0,
110 /* Now another hole */
111 insert_extent(root, offset, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
116 /* Now for a regular extent */
117 insert_extent(root, offset, sectorsize - 1, sectorsize - 1, 0,
118 disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
120 disk_bytenr += sectorsize;
121 offset += sectorsize - 1;
124 * Now for 3 extents that were split from a hole punch so we test
127 insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
128 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
130 offset += sectorsize;
131 insert_extent(root, offset, sectorsize, sectorsize, 0, 0, 0,
132 BTRFS_FILE_EXTENT_REG, 0, slot);
134 offset += sectorsize;
135 insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
136 2 * sectorsize, disk_bytenr, 4 * sectorsize,
137 BTRFS_FILE_EXTENT_REG, 0, slot);
139 offset += 2 * sectorsize;
140 disk_bytenr += 4 * sectorsize;
142 /* Now for a unwritten prealloc extent */
143 insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
144 sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
146 offset += sectorsize;
149 * We want to jack up disk_bytenr a little more so the em stuff doesn't
152 disk_bytenr += 2 * sectorsize;
155 * Now for a partially written prealloc extent, basically the same as
156 * the hole punch example above. Ram_bytes never changes when you mark
157 * extents written btw.
159 insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
160 4 * sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
162 offset += sectorsize;
163 insert_extent(root, offset, sectorsize, 4 * sectorsize, sectorsize,
164 disk_bytenr, 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0,
167 offset += sectorsize;
168 insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
169 2 * sectorsize, disk_bytenr, 4 * sectorsize,
170 BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
172 offset += 2 * sectorsize;
173 disk_bytenr += 4 * sectorsize;
175 /* Now a normal compressed extent */
176 insert_extent(root, offset, 2 * sectorsize, 2 * sectorsize, 0,
177 disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG,
178 BTRFS_COMPRESS_ZLIB, slot);
180 offset += 2 * sectorsize;
182 disk_bytenr += 2 * sectorsize;
184 /* Now a split compressed extent */
185 insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
186 sectorsize, BTRFS_FILE_EXTENT_REG,
187 BTRFS_COMPRESS_ZLIB, slot);
189 offset += sectorsize;
190 insert_extent(root, offset, sectorsize, sectorsize, 0,
191 disk_bytenr + sectorsize, sectorsize,
192 BTRFS_FILE_EXTENT_REG, 0, slot);
194 offset += sectorsize;
195 insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
196 2 * sectorsize, disk_bytenr, sectorsize,
197 BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
199 offset += 2 * sectorsize;
200 disk_bytenr += 2 * sectorsize;
202 /* Now extents that have a hole but no hole extent */
203 insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
204 sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
206 offset += 4 * sectorsize;
207 disk_bytenr += sectorsize;
208 insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
209 sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
212 static unsigned long prealloc_only = 0;
213 static unsigned long compressed_only = 0;
214 static unsigned long vacancy_only = 0;
216 static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize)
218 struct btrfs_fs_info *fs_info = NULL;
219 struct inode *inode = NULL;
220 struct btrfs_root *root = NULL;
221 struct extent_map *em = NULL;
227 test_msg("running btrfs_get_extent tests");
229 inode = btrfs_new_test_inode();
231 test_std_err(TEST_ALLOC_INODE);
235 inode->i_mode = S_IFREG;
236 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
237 BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
238 BTRFS_I(inode)->location.offset = 0;
240 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
242 test_std_err(TEST_ALLOC_FS_INFO);
246 root = btrfs_alloc_dummy_root(fs_info);
248 test_std_err(TEST_ALLOC_ROOT);
252 root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
254 test_std_err(TEST_ALLOC_ROOT);
258 btrfs_set_header_nritems(root->node, 0);
259 btrfs_set_header_level(root->node, 0);
262 /* First with no extents */
263 BTRFS_I(inode)->root = root;
264 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, sectorsize);
267 test_err("got an error when we shouldn't have");
270 if (em->block_start != EXTENT_MAP_HOLE) {
271 test_err("expected a hole, got %llu", em->block_start);
275 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
278 * All of the magic numbers are based on the mapping setup in
279 * setup_file_extents, so if you change anything there you need to
280 * update the comment and update the expected values below.
282 setup_file_extents(root, sectorsize);
284 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, (u64)-1);
286 test_err("got an error when we shouldn't have");
289 if (em->block_start != EXTENT_MAP_HOLE) {
290 test_err("expected a hole, got %llu", em->block_start);
293 if (em->start != 0 || em->len != 5) {
295 "unexpected extent wanted start 0 len 5, got start %llu len %llu",
299 if (em->flags != 0) {
300 test_err("unexpected flags set, want 0 have %lu", em->flags);
303 offset = em->start + em->len;
306 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
308 test_err("got an error when we shouldn't have");
311 if (em->block_start != EXTENT_MAP_INLINE) {
312 test_err("expected an inline, got %llu", em->block_start);
316 if (em->start != offset || em->len != (sectorsize - 5)) {
318 "unexpected extent wanted start %llu len 1, got start %llu len %llu",
319 offset, em->start, em->len);
322 if (em->flags != 0) {
323 test_err("unexpected flags set, want 0 have %lu", em->flags);
327 * We don't test anything else for inline since it doesn't get set
328 * unless we have a page for it to write into. Maybe we should change
331 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_HOLE) {
340 test_err("expected a hole, got %llu", em->block_start);
343 if (em->start != offset || em->len != 4) {
345 "unexpected extent wanted start %llu len 4, 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 offset = em->start + em->len;
357 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
359 test_err("got an error when we shouldn't have");
362 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
363 test_err("expected a real extent, got %llu", em->block_start);
366 if (em->start != offset || em->len != sectorsize - 1) {
368 "unexpected extent wanted start %llu len 4095, got start %llu len %llu",
369 offset, em->start, em->len);
372 if (em->flags != 0) {
373 test_err("unexpected flags set, want 0 have %lu", em->flags);
376 if (em->orig_start != em->start) {
377 test_err("wrong orig offset, want %llu, have %llu", em->start,
381 offset = em->start + em->len;
384 /* The next 3 are split extents */
385 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
387 test_err("got an error when we shouldn't have");
390 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
391 test_err("expected a real extent, got %llu", em->block_start);
394 if (em->start != offset || em->len != sectorsize) {
396 "unexpected extent start %llu len %u, got start %llu len %llu",
397 offset, sectorsize, em->start, em->len);
400 if (em->flags != 0) {
401 test_err("unexpected flags set, want 0 have %lu", em->flags);
404 if (em->orig_start != em->start) {
405 test_err("wrong orig offset, want %llu, have %llu", em->start,
409 disk_bytenr = em->block_start;
410 orig_start = em->start;
411 offset = em->start + em->len;
414 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
416 test_err("got an error when we shouldn't have");
419 if (em->block_start != EXTENT_MAP_HOLE) {
420 test_err("expected a hole, got %llu", em->block_start);
423 if (em->start != offset || em->len != sectorsize) {
425 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
426 offset, sectorsize, em->start, em->len);
429 if (em->flags != 0) {
430 test_err("unexpected flags set, want 0 have %lu", em->flags);
433 offset = em->start + em->len;
436 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
438 test_err("got an error when we shouldn't have");
441 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
442 test_err("expected a real extent, got %llu", em->block_start);
445 if (em->start != offset || em->len != 2 * sectorsize) {
447 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
448 offset, 2 * sectorsize, em->start, em->len);
451 if (em->flags != 0) {
452 test_err("unexpected flags set, want 0 have %lu", em->flags);
455 if (em->orig_start != orig_start) {
456 test_err("wrong orig offset, want %llu, have %llu",
457 orig_start, em->orig_start);
460 disk_bytenr += (em->start - orig_start);
461 if (em->block_start != disk_bytenr) {
462 test_err("wrong block start, want %llu, have %llu",
463 disk_bytenr, em->block_start);
466 offset = em->start + em->len;
469 /* Prealloc extent */
470 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
472 test_err("got an error when we shouldn't have");
475 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
476 test_err("expected a real extent, got %llu", em->block_start);
479 if (em->start != offset || em->len != sectorsize) {
481 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
482 offset, sectorsize, em->start, em->len);
485 if (em->flags != prealloc_only) {
486 test_err("unexpected flags set, want %lu have %lu",
487 prealloc_only, em->flags);
490 if (em->orig_start != em->start) {
491 test_err("wrong orig offset, want %llu, have %llu", em->start,
495 offset = em->start + em->len;
498 /* The next 3 are a half written prealloc extent */
499 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
501 test_err("got an error when we shouldn't have");
504 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
505 test_err("expected a real extent, got %llu", em->block_start);
508 if (em->start != offset || em->len != sectorsize) {
510 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
511 offset, sectorsize, em->start, em->len);
514 if (em->flags != prealloc_only) {
515 test_err("unexpected flags set, want %lu have %lu",
516 prealloc_only, em->flags);
519 if (em->orig_start != em->start) {
520 test_err("wrong orig offset, want %llu, have %llu", em->start,
524 disk_bytenr = em->block_start;
525 orig_start = em->start;
526 offset = em->start + em->len;
529 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
531 test_err("got an error when we shouldn't have");
534 if (em->block_start >= EXTENT_MAP_HOLE) {
535 test_err("expected a real extent, got %llu", em->block_start);
538 if (em->start != offset || em->len != sectorsize) {
540 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
541 offset, sectorsize, em->start, em->len);
544 if (em->flags != 0) {
545 test_err("unexpected flags set, want 0 have %lu", em->flags);
548 if (em->orig_start != orig_start) {
549 test_err("unexpected orig offset, wanted %llu, have %llu",
550 orig_start, em->orig_start);
553 if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
554 test_err("unexpected block start, wanted %llu, have %llu",
555 disk_bytenr + (em->start - em->orig_start),
559 offset = em->start + em->len;
562 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
564 test_err("got an error when we shouldn't have");
567 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
568 test_err("expected a real extent, got %llu", em->block_start);
571 if (em->start != offset || em->len != 2 * sectorsize) {
573 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
574 offset, 2 * sectorsize, em->start, em->len);
577 if (em->flags != prealloc_only) {
578 test_err("unexpected flags set, want %lu have %lu",
579 prealloc_only, em->flags);
582 if (em->orig_start != orig_start) {
583 test_err("wrong orig offset, want %llu, have %llu", orig_start,
587 if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
588 test_err("unexpected block start, wanted %llu, have %llu",
589 disk_bytenr + (em->start - em->orig_start),
593 offset = em->start + em->len;
596 /* Now for the compressed extent */
597 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
599 test_err("got an error when we shouldn't have");
602 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
603 test_err("expected a real extent, got %llu", em->block_start);
606 if (em->start != offset || em->len != 2 * sectorsize) {
608 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
609 offset, 2 * sectorsize, em->start, em->len);
612 if (em->flags != compressed_only) {
613 test_err("unexpected flags set, want %lu have %lu",
614 compressed_only, em->flags);
617 if (em->orig_start != em->start) {
618 test_err("wrong orig offset, want %llu, have %llu",
619 em->start, em->orig_start);
622 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
623 test_err("unexpected compress type, wanted %d, got %d",
624 BTRFS_COMPRESS_ZLIB, em->compress_type);
627 offset = em->start + em->len;
630 /* Split compressed extent */
631 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
633 test_err("got an error when we shouldn't have");
636 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
637 test_err("expected a real extent, got %llu", em->block_start);
640 if (em->start != offset || em->len != sectorsize) {
642 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
643 offset, sectorsize, em->start, em->len);
646 if (em->flags != compressed_only) {
647 test_err("unexpected flags set, want %lu have %lu",
648 compressed_only, em->flags);
651 if (em->orig_start != em->start) {
652 test_err("wrong orig offset, want %llu, have %llu",
653 em->start, em->orig_start);
656 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
657 test_err("unexpected compress type, wanted %d, got %d",
658 BTRFS_COMPRESS_ZLIB, em->compress_type);
661 disk_bytenr = em->block_start;
662 orig_start = em->start;
663 offset = em->start + em->len;
666 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
668 test_err("got an error when we shouldn't have");
671 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
672 test_err("expected a real extent, got %llu", em->block_start);
675 if (em->start != offset || em->len != sectorsize) {
677 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
678 offset, sectorsize, em->start, em->len);
681 if (em->flags != 0) {
682 test_err("unexpected flags set, want 0 have %lu", em->flags);
685 if (em->orig_start != em->start) {
686 test_err("wrong orig offset, want %llu, have %llu", em->start,
690 offset = em->start + em->len;
693 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
695 test_err("got an error when we shouldn't have");
698 if (em->block_start != disk_bytenr) {
699 test_err("block start does not match, want %llu got %llu",
700 disk_bytenr, em->block_start);
703 if (em->start != offset || em->len != 2 * sectorsize) {
705 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
706 offset, 2 * sectorsize, em->start, em->len);
709 if (em->flags != compressed_only) {
710 test_err("unexpected flags set, want %lu have %lu",
711 compressed_only, em->flags);
714 if (em->orig_start != orig_start) {
715 test_err("wrong orig offset, want %llu, have %llu",
716 em->start, orig_start);
719 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
720 test_err("unexpected compress type, wanted %d, got %d",
721 BTRFS_COMPRESS_ZLIB, em->compress_type);
724 offset = em->start + em->len;
727 /* A hole between regular extents but no hole extent */
728 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset + 6, sectorsize);
730 test_err("got an error when we shouldn't have");
733 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
734 test_err("expected a real extent, got %llu", em->block_start);
737 if (em->start != offset || em->len != sectorsize) {
739 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
740 offset, sectorsize, em->start, em->len);
743 if (em->flags != 0) {
744 test_err("unexpected flags set, want 0 have %lu", em->flags);
747 if (em->orig_start != em->start) {
748 test_err("wrong orig offset, want %llu, have %llu", em->start,
752 offset = em->start + em->len;
755 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, SZ_4M);
757 test_err("got an error when we shouldn't have");
760 if (em->block_start != EXTENT_MAP_HOLE) {
761 test_err("expected a hole extent, got %llu", em->block_start);
765 * Currently we just return a length that we requested rather than the
766 * length of the actual hole, if this changes we'll have to change this
769 if (em->start != offset || em->len != 3 * sectorsize) {
771 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
772 offset, 3 * sectorsize, em->start, em->len);
775 if (em->flags != vacancy_only) {
776 test_err("unexpected flags set, want %lu have %lu",
777 vacancy_only, em->flags);
780 if (em->orig_start != em->start) {
781 test_err("wrong orig offset, want %llu, have %llu", em->start,
785 offset = em->start + em->len;
788 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
790 test_err("got an error when we shouldn't have");
793 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
794 test_err("expected a real extent, got %llu", em->block_start);
797 if (em->start != offset || em->len != sectorsize) {
799 "unexpected extent wanted start %llu len %u, got start %llu len %llu",
800 offset, sectorsize, em->start, em->len);
803 if (em->flags != 0) {
804 test_err("unexpected flags set, want 0 have %lu", em->flags);
807 if (em->orig_start != em->start) {
808 test_err("wrong orig offset, want %llu, have %llu", em->start,
817 btrfs_free_dummy_root(root);
818 btrfs_free_dummy_fs_info(fs_info);
822 static int test_hole_first(u32 sectorsize, u32 nodesize)
824 struct btrfs_fs_info *fs_info = NULL;
825 struct inode *inode = NULL;
826 struct btrfs_root *root = NULL;
827 struct extent_map *em = NULL;
830 test_msg("running hole first btrfs_get_extent test");
832 inode = btrfs_new_test_inode();
834 test_std_err(TEST_ALLOC_INODE);
838 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
839 BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
840 BTRFS_I(inode)->location.offset = 0;
842 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
844 test_std_err(TEST_ALLOC_FS_INFO);
848 root = btrfs_alloc_dummy_root(fs_info);
850 test_std_err(TEST_ALLOC_ROOT);
854 root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
856 test_std_err(TEST_ALLOC_ROOT);
860 btrfs_set_header_nritems(root->node, 0);
861 btrfs_set_header_level(root->node, 0);
862 BTRFS_I(inode)->root = root;
866 * Need a blank inode item here just so we don't confuse
869 insert_inode_item_key(root);
870 insert_extent(root, sectorsize, sectorsize, sectorsize, 0, sectorsize,
871 sectorsize, BTRFS_FILE_EXTENT_REG, 0, 1);
872 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, 2 * sectorsize);
874 test_err("got an error when we shouldn't have");
877 if (em->block_start != EXTENT_MAP_HOLE) {
878 test_err("expected a hole, got %llu", em->block_start);
881 if (em->start != 0 || em->len != sectorsize) {
883 "unexpected extent wanted start 0 len %u, got start %llu len %llu",
884 sectorsize, em->start, em->len);
887 if (em->flags != vacancy_only) {
888 test_err("wrong flags, wanted %lu, have %lu", vacancy_only,
894 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, sectorsize, 2 * sectorsize);
896 test_err("got an error when we shouldn't have");
899 if (em->block_start != sectorsize) {
900 test_err("expected a real extent, got %llu", em->block_start);
903 if (em->start != sectorsize || em->len != sectorsize) {
905 "unexpected extent wanted start %u len %u, got start %llu len %llu",
906 sectorsize, sectorsize, em->start, em->len);
909 if (em->flags != 0) {
910 test_err("unexpected flags set, wanted 0 got %lu",
919 btrfs_free_dummy_root(root);
920 btrfs_free_dummy_fs_info(fs_info);
924 static int test_extent_accounting(u32 sectorsize, u32 nodesize)
926 struct btrfs_fs_info *fs_info = NULL;
927 struct inode *inode = NULL;
928 struct btrfs_root *root = NULL;
931 test_msg("running outstanding_extents tests");
933 inode = btrfs_new_test_inode();
935 test_std_err(TEST_ALLOC_INODE);
939 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
941 test_std_err(TEST_ALLOC_FS_INFO);
945 root = btrfs_alloc_dummy_root(fs_info);
947 test_std_err(TEST_ALLOC_ROOT);
951 BTRFS_I(inode)->root = root;
953 /* [BTRFS_MAX_EXTENT_SIZE] */
954 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), 0,
955 BTRFS_MAX_EXTENT_SIZE - 1, 0, NULL);
957 test_err("btrfs_set_extent_delalloc returned %d", ret);
960 if (BTRFS_I(inode)->outstanding_extents != 1) {
962 test_err("miscount, wanted 1, got %u",
963 BTRFS_I(inode)->outstanding_extents);
967 /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
968 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), BTRFS_MAX_EXTENT_SIZE,
969 BTRFS_MAX_EXTENT_SIZE + sectorsize - 1,
972 test_err("btrfs_set_extent_delalloc returned %d", ret);
975 if (BTRFS_I(inode)->outstanding_extents != 2) {
977 test_err("miscount, wanted 2, got %u",
978 BTRFS_I(inode)->outstanding_extents);
982 /* [BTRFS_MAX_EXTENT_SIZE/2][sectorsize HOLE][the rest] */
983 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
984 BTRFS_MAX_EXTENT_SIZE >> 1,
985 (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1,
986 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
987 EXTENT_UPTODATE, 0, 0, NULL);
989 test_err("clear_extent_bit returned %d", ret);
992 if (BTRFS_I(inode)->outstanding_extents != 2) {
994 test_err("miscount, wanted 2, got %u",
995 BTRFS_I(inode)->outstanding_extents);
999 /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
1000 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), BTRFS_MAX_EXTENT_SIZE >> 1,
1001 (BTRFS_MAX_EXTENT_SIZE >> 1)
1005 test_err("btrfs_set_extent_delalloc returned %d", ret);
1008 if (BTRFS_I(inode)->outstanding_extents != 2) {
1010 test_err("miscount, wanted 2, got %u",
1011 BTRFS_I(inode)->outstanding_extents);
1016 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize]
1018 ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
1019 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize,
1020 (BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1,
1023 test_err("btrfs_set_extent_delalloc returned %d", ret);
1026 if (BTRFS_I(inode)->outstanding_extents != 4) {
1028 test_err("miscount, wanted 4, got %u",
1029 BTRFS_I(inode)->outstanding_extents);
1034 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize]
1036 ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
1037 BTRFS_MAX_EXTENT_SIZE + sectorsize,
1038 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL);
1040 test_err("btrfs_set_extent_delalloc returned %d", ret);
1043 if (BTRFS_I(inode)->outstanding_extents != 3) {
1045 test_err("miscount, wanted 3, got %u",
1046 BTRFS_I(inode)->outstanding_extents);
1050 /* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */
1051 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
1052 BTRFS_MAX_EXTENT_SIZE + sectorsize,
1053 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1,
1054 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1055 EXTENT_UPTODATE, 0, 0, NULL);
1057 test_err("clear_extent_bit returned %d", ret);
1060 if (BTRFS_I(inode)->outstanding_extents != 4) {
1062 test_err("miscount, wanted 4, got %u",
1063 BTRFS_I(inode)->outstanding_extents);
1068 * Refill the hole again just for good measure, because I thought it
1069 * might fail and I'd rather satisfy my paranoia at this point.
1071 ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
1072 BTRFS_MAX_EXTENT_SIZE + sectorsize,
1073 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL);
1075 test_err("btrfs_set_extent_delalloc returned %d", ret);
1078 if (BTRFS_I(inode)->outstanding_extents != 3) {
1080 test_err("miscount, wanted 3, got %u",
1081 BTRFS_I(inode)->outstanding_extents);
1086 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1087 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1088 EXTENT_UPTODATE, 0, 0, NULL);
1090 test_err("clear_extent_bit returned %d", ret);
1093 if (BTRFS_I(inode)->outstanding_extents) {
1095 test_err("miscount, wanted 0, got %u",
1096 BTRFS_I(inode)->outstanding_extents);
1102 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1103 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1104 EXTENT_UPTODATE, 0, 0, NULL);
1106 btrfs_free_dummy_root(root);
1107 btrfs_free_dummy_fs_info(fs_info);
1111 int btrfs_test_inodes(u32 sectorsize, u32 nodesize)
1115 test_msg("running inode tests");
1117 set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
1118 set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
1120 ret = test_btrfs_get_extent(sectorsize, nodesize);
1123 ret = test_hole_first(sectorsize, nodesize);
1126 return test_extent_accounting(sectorsize, nodesize);