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"
25 #include "../compression.h"
27 static void insert_extent(struct btrfs_root *root, u64 start, u64 len,
28 u64 ram_bytes, u64 offset, u64 disk_bytenr,
29 u64 disk_len, u32 type, u8 compression, int slot)
31 struct btrfs_path path;
32 struct btrfs_file_extent_item *fi;
33 struct extent_buffer *leaf = root->node;
35 u32 value_len = sizeof(struct btrfs_file_extent_item);
37 if (type == BTRFS_FILE_EXTENT_INLINE)
39 memset(&path, 0, sizeof(path));
44 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
45 key.type = BTRFS_EXTENT_DATA_KEY;
48 setup_items_for_insert(root, &path, &key, &value_len, value_len,
49 value_len + sizeof(struct btrfs_item), 1);
50 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
51 btrfs_set_file_extent_generation(leaf, fi, 1);
52 btrfs_set_file_extent_type(leaf, fi, type);
53 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
54 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_len);
55 btrfs_set_file_extent_offset(leaf, fi, offset);
56 btrfs_set_file_extent_num_bytes(leaf, fi, len);
57 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
58 btrfs_set_file_extent_compression(leaf, fi, compression);
59 btrfs_set_file_extent_encryption(leaf, fi, 0);
60 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
63 static void insert_inode_item_key(struct btrfs_root *root)
65 struct btrfs_path path;
66 struct extent_buffer *leaf = root->node;
70 memset(&path, 0, sizeof(path));
75 key.objectid = BTRFS_INODE_ITEM_KEY;
76 key.type = BTRFS_INODE_ITEM_KEY;
79 setup_items_for_insert(root, &path, &key, &value_len, value_len,
80 value_len + sizeof(struct btrfs_item), 1);
84 * Build the most complicated map of extents the earth has ever seen. We want
85 * this so we can test all of the corner cases of btrfs_get_extent. Here is a
86 * diagram of how the extents will look though this may not be possible we still
87 * want to make sure everything acts normally (the last number is not inclusive)
89 * [0 - 5][5 - 6][6 - 10][10 - 4096][ 4096 - 8192 ][8192 - 12288]
90 * [hole ][inline][ hole ][ regular ][regular1 split][ hole ]
92 * [ 12288 - 20480][20480 - 24576][ 24576 - 28672 ][28672 - 36864][36864 - 45056]
93 * [regular1 split][ prealloc1 ][prealloc1 written][ prealloc1 ][ compressed ]
95 * [45056 - 49152][49152-53248][53248-61440][61440-65536][ 65536+81920 ]
96 * [ compressed1 ][ regular ][compressed1][ regular ][ hole but no extent]
101 static void setup_file_extents(struct btrfs_root *root)
104 u64 disk_bytenr = SZ_1M;
107 /* First we want a hole */
108 insert_extent(root, offset, 5, 5, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
114 * Now we want an inline extent, I don't think this is possible but hey
115 * why not? Also keep in mind if we have an inline extent it counts as
116 * the whole first page. If we were to expand it we would have to cow
117 * and we wouldn't have an inline extent anymore.
119 insert_extent(root, offset, 1, 1, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE, 0,
124 /* Now another hole */
125 insert_extent(root, offset, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
130 /* Now for a regular extent */
131 insert_extent(root, offset, 4095, 4095, 0, disk_bytenr, 4096,
132 BTRFS_FILE_EXTENT_REG, 0, slot);
138 * Now for 3 extents that were split from a hole punch so we test
141 insert_extent(root, offset, 4096, 16384, 0, disk_bytenr, 16384,
142 BTRFS_FILE_EXTENT_REG, 0, slot);
145 insert_extent(root, offset, 4096, 4096, 0, 0, 0, BTRFS_FILE_EXTENT_REG,
149 insert_extent(root, offset, 8192, 16384, 8192, disk_bytenr, 16384,
150 BTRFS_FILE_EXTENT_REG, 0, slot);
153 disk_bytenr += 16384;
155 /* Now for a unwritten prealloc extent */
156 insert_extent(root, offset, 4096, 4096, 0, disk_bytenr, 4096,
157 BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
162 * We want to jack up disk_bytenr a little more so the em stuff doesn't
168 * Now for a partially written prealloc extent, basically the same as
169 * the hole punch example above. Ram_bytes never changes when you mark
170 * extents written btw.
172 insert_extent(root, offset, 4096, 16384, 0, disk_bytenr, 16384,
173 BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
176 insert_extent(root, offset, 4096, 16384, 4096, disk_bytenr, 16384,
177 BTRFS_FILE_EXTENT_REG, 0, slot);
180 insert_extent(root, offset, 8192, 16384, 8192, disk_bytenr, 16384,
181 BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
184 disk_bytenr += 16384;
186 /* Now a normal compressed extent */
187 insert_extent(root, offset, 8192, 8192, 0, disk_bytenr, 4096,
188 BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
194 /* Now a split compressed extent */
195 insert_extent(root, offset, 4096, 16384, 0, disk_bytenr, 4096,
196 BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
199 insert_extent(root, offset, 4096, 4096, 0, disk_bytenr + 4096, 4096,
200 BTRFS_FILE_EXTENT_REG, 0, slot);
203 insert_extent(root, offset, 8192, 16384, 8192, disk_bytenr, 4096,
204 BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
209 /* Now extents that have a hole but no hole extent */
210 insert_extent(root, offset, 4096, 4096, 0, disk_bytenr, 4096,
211 BTRFS_FILE_EXTENT_REG, 0, slot);
215 insert_extent(root, offset, 4096, 4096, 0, disk_bytenr, 4096,
216 BTRFS_FILE_EXTENT_REG, 0, slot);
219 static unsigned long prealloc_only = 0;
220 static unsigned long compressed_only = 0;
221 static unsigned long vacancy_only = 0;
223 static noinline int test_btrfs_get_extent(void)
225 struct inode *inode = NULL;
226 struct btrfs_root *root = NULL;
227 struct extent_map *em = NULL;
233 inode = btrfs_new_test_inode();
235 test_msg("Couldn't allocate inode\n");
239 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
240 BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
241 BTRFS_I(inode)->location.offset = 0;
243 root = btrfs_alloc_dummy_root();
245 test_msg("Couldn't allocate root\n");
250 * We do this since btrfs_get_extent wants to assign em->bdev to
251 * root->fs_info->fs_devices->latest_bdev.
253 root->fs_info = btrfs_alloc_dummy_fs_info();
254 if (!root->fs_info) {
255 test_msg("Couldn't allocate dummy fs info\n");
259 root->node = alloc_dummy_extent_buffer(NULL, 4096);
261 test_msg("Couldn't allocate dummy buffer\n");
266 * We will just free a dummy node if it's ref count is 2 so we need an
267 * extra ref so our searches don't accidentally release our page.
269 extent_buffer_get(root->node);
270 btrfs_set_header_nritems(root->node, 0);
271 btrfs_set_header_level(root->node, 0);
274 /* First with no extents */
275 BTRFS_I(inode)->root = root;
276 em = btrfs_get_extent(inode, NULL, 0, 0, 4096, 0);
279 test_msg("Got an error when we shouldn't have\n");
282 if (em->block_start != EXTENT_MAP_HOLE) {
283 test_msg("Expected a hole, got %llu\n", em->block_start);
286 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
287 test_msg("Vacancy flag wasn't set properly\n");
291 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
294 * All of the magic numbers are based on the mapping setup in
295 * setup_file_extents, so if you change anything there you need to
296 * update the comment and update the expected values below.
298 setup_file_extents(root);
300 em = btrfs_get_extent(inode, NULL, 0, 0, (u64)-1, 0);
302 test_msg("Got an error when we shouldn't have\n");
305 if (em->block_start != EXTENT_MAP_HOLE) {
306 test_msg("Expected a hole, got %llu\n", em->block_start);
309 if (em->start != 0 || em->len != 5) {
310 test_msg("Unexpected extent wanted start 0 len 5, got start "
311 "%llu len %llu\n", em->start, em->len);
314 if (em->flags != 0) {
315 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
318 offset = em->start + em->len;
321 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
323 test_msg("Got an error when we shouldn't have\n");
326 if (em->block_start != EXTENT_MAP_INLINE) {
327 test_msg("Expected an inline, got %llu\n", em->block_start);
330 if (em->start != offset || em->len != 4091) {
331 test_msg("Unexpected extent wanted start %llu len 1, got start "
332 "%llu len %llu\n", offset, em->start, em->len);
335 if (em->flags != 0) {
336 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
340 * We don't test anything else for inline since it doesn't get set
341 * unless we have a page for it to write into. Maybe we should change
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_HOLE) {
353 test_msg("Expected a hole, got %llu\n", em->block_start);
356 if (em->start != offset || em->len != 4) {
357 test_msg("Unexpected extent wanted start %llu len 4, 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);
365 offset = em->start + em->len;
369 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
371 test_msg("Got an error when we shouldn't have\n");
374 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
375 test_msg("Expected a real extent, got %llu\n", em->block_start);
378 if (em->start != offset || em->len != 4095) {
379 test_msg("Unexpected extent wanted start %llu len 4095, got "
380 "start %llu len %llu\n", offset, em->start, em->len);
383 if (em->flags != 0) {
384 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
387 if (em->orig_start != em->start) {
388 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
392 offset = em->start + em->len;
395 /* The next 3 are split extents */
396 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
398 test_msg("Got an error when we shouldn't have\n");
401 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
402 test_msg("Expected a real extent, got %llu\n", em->block_start);
405 if (em->start != offset || em->len != 4096) {
406 test_msg("Unexpected extent wanted start %llu len 4096, got "
407 "start %llu len %llu\n", offset, em->start, em->len);
410 if (em->flags != 0) {
411 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
414 if (em->orig_start != em->start) {
415 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
419 disk_bytenr = em->block_start;
420 orig_start = em->start;
421 offset = em->start + em->len;
424 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
426 test_msg("Got an error when we shouldn't have\n");
429 if (em->block_start != EXTENT_MAP_HOLE) {
430 test_msg("Expected a hole, got %llu\n", em->block_start);
433 if (em->start != offset || em->len != 4096) {
434 test_msg("Unexpected extent wanted start %llu len 4096, got "
435 "start %llu len %llu\n", offset, em->start, em->len);
438 if (em->flags != 0) {
439 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
442 offset = em->start + em->len;
445 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
447 test_msg("Got an error when we shouldn't have\n");
450 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
451 test_msg("Expected a real extent, got %llu\n", em->block_start);
454 if (em->start != offset || em->len != 8192) {
455 test_msg("Unexpected extent wanted start %llu len 8192, got "
456 "start %llu len %llu\n", offset, em->start, em->len);
459 if (em->flags != 0) {
460 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
463 if (em->orig_start != orig_start) {
464 test_msg("Wrong orig offset, want %llu, have %llu\n",
465 orig_start, em->orig_start);
468 disk_bytenr += (em->start - orig_start);
469 if (em->block_start != disk_bytenr) {
470 test_msg("Wrong block start, want %llu, have %llu\n",
471 disk_bytenr, em->block_start);
474 offset = em->start + em->len;
477 /* Prealloc extent */
478 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
480 test_msg("Got an error when we shouldn't have\n");
483 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
484 test_msg("Expected a real extent, got %llu\n", em->block_start);
487 if (em->start != offset || em->len != 4096) {
488 test_msg("Unexpected extent wanted start %llu len 4096, got "
489 "start %llu len %llu\n", offset, em->start, em->len);
492 if (em->flags != prealloc_only) {
493 test_msg("Unexpected flags set, want %lu have %lu\n",
494 prealloc_only, em->flags);
497 if (em->orig_start != em->start) {
498 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
502 offset = em->start + em->len;
505 /* The next 3 are a half written prealloc extent */
506 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
508 test_msg("Got an error when we shouldn't have\n");
511 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
512 test_msg("Expected a real extent, got %llu\n", em->block_start);
515 if (em->start != offset || em->len != 4096) {
516 test_msg("Unexpected extent wanted start %llu len 4096, got "
517 "start %llu len %llu\n", offset, em->start, em->len);
520 if (em->flags != prealloc_only) {
521 test_msg("Unexpected flags set, want %lu have %lu\n",
522 prealloc_only, em->flags);
525 if (em->orig_start != em->start) {
526 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
530 disk_bytenr = em->block_start;
531 orig_start = em->start;
532 offset = em->start + em->len;
535 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
537 test_msg("Got an error when we shouldn't have\n");
540 if (em->block_start >= EXTENT_MAP_HOLE) {
541 test_msg("Expected a real extent, got %llu\n", em->block_start);
544 if (em->start != offset || em->len != 4096) {
545 test_msg("Unexpected extent wanted start %llu len 4096, got "
546 "start %llu len %llu\n", offset, em->start, em->len);
549 if (em->flags != 0) {
550 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
553 if (em->orig_start != orig_start) {
554 test_msg("Unexpected orig offset, wanted %llu, have %llu\n",
555 orig_start, em->orig_start);
558 if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
559 test_msg("Unexpected block start, wanted %llu, have %llu\n",
560 disk_bytenr + (em->start - em->orig_start),
564 offset = em->start + em->len;
567 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
569 test_msg("Got an error when we shouldn't have\n");
572 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
573 test_msg("Expected a real extent, got %llu\n", em->block_start);
576 if (em->start != offset || em->len != 8192) {
577 test_msg("Unexpected extent wanted start %llu len 8192, got "
578 "start %llu len %llu\n", offset, em->start, em->len);
581 if (em->flags != prealloc_only) {
582 test_msg("Unexpected flags set, want %lu have %lu\n",
583 prealloc_only, em->flags);
586 if (em->orig_start != orig_start) {
587 test_msg("Wrong orig offset, want %llu, have %llu\n", orig_start,
591 if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
592 test_msg("Unexpected block start, wanted %llu, have %llu\n",
593 disk_bytenr + (em->start - em->orig_start),
597 offset = em->start + em->len;
600 /* Now for the compressed extent */
601 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
603 test_msg("Got an error when we shouldn't have\n");
606 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
607 test_msg("Expected a real extent, got %llu\n", em->block_start);
610 if (em->start != offset || em->len != 8192) {
611 test_msg("Unexpected extent wanted start %llu len 8192, got "
612 "start %llu len %llu\n", offset, em->start, em->len);
615 if (em->flags != compressed_only) {
616 test_msg("Unexpected flags set, want %lu have %lu\n",
617 compressed_only, em->flags);
620 if (em->orig_start != em->start) {
621 test_msg("Wrong orig offset, want %llu, have %llu\n",
622 em->start, em->orig_start);
625 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
626 test_msg("Unexpected compress type, wanted %d, got %d\n",
627 BTRFS_COMPRESS_ZLIB, em->compress_type);
630 offset = em->start + em->len;
633 /* Split compressed extent */
634 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
636 test_msg("Got an error when we shouldn't have\n");
639 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
640 test_msg("Expected a real extent, got %llu\n", em->block_start);
643 if (em->start != offset || em->len != 4096) {
644 test_msg("Unexpected extent wanted start %llu len 4096, got "
645 "start %llu len %llu\n", offset, em->start, em->len);
648 if (em->flags != compressed_only) {
649 test_msg("Unexpected flags set, want %lu have %lu\n",
650 compressed_only, em->flags);
653 if (em->orig_start != em->start) {
654 test_msg("Wrong orig offset, want %llu, have %llu\n",
655 em->start, em->orig_start);
658 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
659 test_msg("Unexpected compress type, wanted %d, got %d\n",
660 BTRFS_COMPRESS_ZLIB, em->compress_type);
663 disk_bytenr = em->block_start;
664 orig_start = em->start;
665 offset = em->start + em->len;
668 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
670 test_msg("Got an error when we shouldn't have\n");
673 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
674 test_msg("Expected a real extent, got %llu\n", em->block_start);
677 if (em->start != offset || em->len != 4096) {
678 test_msg("Unexpected extent wanted start %llu len 4096, got "
679 "start %llu len %llu\n", offset, em->start, em->len);
682 if (em->flags != 0) {
683 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
686 if (em->orig_start != em->start) {
687 test_msg("Wrong orig offset, want %llu, have %llu\n", 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 != disk_bytenr) {
700 test_msg("Block start does not match, want %llu got %llu\n",
701 disk_bytenr, em->block_start);
704 if (em->start != offset || em->len != 8192) {
705 test_msg("Unexpected extent wanted start %llu len 8192, got "
706 "start %llu len %llu\n", offset, em->start, em->len);
709 if (em->flags != compressed_only) {
710 test_msg("Unexpected flags set, want %lu have %lu\n",
711 compressed_only, em->flags);
714 if (em->orig_start != orig_start) {
715 test_msg("Wrong orig offset, want %llu, have %llu\n",
716 em->start, orig_start);
719 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
720 test_msg("Unexpected compress type, wanted %d, got %d\n",
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(inode, NULL, 0, offset + 6, 4096, 0);
730 test_msg("Got an error when we shouldn't have\n");
733 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
734 test_msg("Expected a real extent, got %llu\n", em->block_start);
737 if (em->start != offset || em->len != 4096) {
738 test_msg("Unexpected extent wanted start %llu len 4096, got "
739 "start %llu len %llu\n", offset, em->start, em->len);
742 if (em->flags != 0) {
743 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
746 if (em->orig_start != em->start) {
747 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
751 offset = em->start + em->len;
754 em = btrfs_get_extent(inode, NULL, 0, offset, 4096 * 1024, 0);
756 test_msg("Got an error when we shouldn't have\n");
759 if (em->block_start != EXTENT_MAP_HOLE) {
760 test_msg("Expected a hole extent, got %llu\n", em->block_start);
764 * Currently we just return a length that we requested rather than the
765 * length of the actual hole, if this changes we'll have to change this
768 if (em->start != offset || em->len != 12288) {
769 test_msg("Unexpected extent wanted start %llu len 12288, got "
770 "start %llu len %llu\n", offset, em->start, em->len);
773 if (em->flags != vacancy_only) {
774 test_msg("Unexpected flags set, want %lu have %lu\n",
775 vacancy_only, em->flags);
778 if (em->orig_start != em->start) {
779 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
783 offset = em->start + em->len;
786 em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
788 test_msg("Got an error when we shouldn't have\n");
791 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
792 test_msg("Expected a real extent, got %llu\n", em->block_start);
795 if (em->start != offset || em->len != 4096) {
796 test_msg("Unexpected extent wanted start %llu len 4096, got "
797 "start %llu len %llu\n", offset, em->start, em->len);
800 if (em->flags != 0) {
801 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
804 if (em->orig_start != em->start) {
805 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
814 btrfs_free_dummy_root(root);
818 static int test_hole_first(void)
820 struct inode *inode = NULL;
821 struct btrfs_root *root = NULL;
822 struct extent_map *em = NULL;
825 inode = btrfs_new_test_inode();
827 test_msg("Couldn't allocate inode\n");
831 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
832 BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
833 BTRFS_I(inode)->location.offset = 0;
835 root = btrfs_alloc_dummy_root();
837 test_msg("Couldn't allocate root\n");
841 root->fs_info = btrfs_alloc_dummy_fs_info();
842 if (!root->fs_info) {
843 test_msg("Couldn't allocate dummy fs info\n");
847 root->node = alloc_dummy_extent_buffer(NULL, 4096);
849 test_msg("Couldn't allocate dummy buffer\n");
853 extent_buffer_get(root->node);
854 btrfs_set_header_nritems(root->node, 0);
855 btrfs_set_header_level(root->node, 0);
856 BTRFS_I(inode)->root = root;
860 * Need a blank inode item here just so we don't confuse
863 insert_inode_item_key(root);
864 insert_extent(root, 4096, 4096, 4096, 0, 4096, 4096,
865 BTRFS_FILE_EXTENT_REG, 0, 1);
866 em = btrfs_get_extent(inode, NULL, 0, 0, 8192, 0);
868 test_msg("Got an error when we shouldn't have\n");
871 if (em->block_start != EXTENT_MAP_HOLE) {
872 test_msg("Expected a hole, got %llu\n", em->block_start);
875 if (em->start != 0 || em->len != 4096) {
876 test_msg("Unexpected extent wanted start 0 len 4096, got start "
877 "%llu len %llu\n", em->start, em->len);
880 if (em->flags != vacancy_only) {
881 test_msg("Wrong flags, wanted %lu, have %lu\n", vacancy_only,
887 em = btrfs_get_extent(inode, NULL, 0, 4096, 8192, 0);
889 test_msg("Got an error when we shouldn't have\n");
892 if (em->block_start != 4096) {
893 test_msg("Expected a real extent, got %llu\n", em->block_start);
896 if (em->start != 4096 || em->len != 4096) {
897 test_msg("Unexpected extent wanted start 4096 len 4096, got "
898 "start %llu len %llu\n", em->start, em->len);
901 if (em->flags != 0) {
902 test_msg("Unexpected flags set, wanted 0 got %lu\n",
911 btrfs_free_dummy_root(root);
915 static int test_extent_accounting(void)
917 struct inode *inode = NULL;
918 struct btrfs_root *root = NULL;
921 inode = btrfs_new_test_inode();
923 test_msg("Couldn't allocate inode\n");
927 root = btrfs_alloc_dummy_root();
929 test_msg("Couldn't allocate root\n");
933 root->fs_info = btrfs_alloc_dummy_fs_info();
934 if (!root->fs_info) {
935 test_msg("Couldn't allocate dummy fs info\n");
939 BTRFS_I(inode)->root = root;
940 btrfs_test_inode_set_ops(inode);
942 /* [BTRFS_MAX_EXTENT_SIZE] */
943 BTRFS_I(inode)->outstanding_extents++;
944 ret = btrfs_set_extent_delalloc(inode, 0, BTRFS_MAX_EXTENT_SIZE - 1,
947 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
950 if (BTRFS_I(inode)->outstanding_extents != 1) {
952 test_msg("Miscount, wanted 1, got %u\n",
953 BTRFS_I(inode)->outstanding_extents);
957 /* [BTRFS_MAX_EXTENT_SIZE][4k] */
958 BTRFS_I(inode)->outstanding_extents++;
959 ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE,
960 BTRFS_MAX_EXTENT_SIZE + 4095, NULL);
962 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
965 if (BTRFS_I(inode)->outstanding_extents != 2) {
967 test_msg("Miscount, wanted 2, got %u\n",
968 BTRFS_I(inode)->outstanding_extents);
972 /* [BTRFS_MAX_EXTENT_SIZE/2][4K HOLE][the rest] */
973 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
974 BTRFS_MAX_EXTENT_SIZE >> 1,
975 (BTRFS_MAX_EXTENT_SIZE >> 1) + 4095,
976 EXTENT_DELALLOC | EXTENT_DIRTY |
977 EXTENT_UPTODATE | EXTENT_DO_ACCOUNTING, 0, 0,
980 test_msg("clear_extent_bit returned %d\n", ret);
983 if (BTRFS_I(inode)->outstanding_extents != 2) {
985 test_msg("Miscount, wanted 2, got %u\n",
986 BTRFS_I(inode)->outstanding_extents);
990 /* [BTRFS_MAX_EXTENT_SIZE][4K] */
991 BTRFS_I(inode)->outstanding_extents++;
992 ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE >> 1,
993 (BTRFS_MAX_EXTENT_SIZE >> 1) + 4095,
996 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
999 if (BTRFS_I(inode)->outstanding_extents != 2) {
1001 test_msg("Miscount, wanted 2, got %u\n",
1002 BTRFS_I(inode)->outstanding_extents);
1007 * [BTRFS_MAX_EXTENT_SIZE+4K][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4K]
1009 * I'm artificially adding 2 to outstanding_extents because in the
1010 * buffered IO case we'd add things up as we go, but I don't feel like
1011 * doing that here, this isn't the interesting case we want to test.
1013 BTRFS_I(inode)->outstanding_extents += 2;
1014 ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE + 8192,
1015 (BTRFS_MAX_EXTENT_SIZE << 1) + 12287,
1018 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
1021 if (BTRFS_I(inode)->outstanding_extents != 4) {
1023 test_msg("Miscount, wanted 4, got %u\n",
1024 BTRFS_I(inode)->outstanding_extents);
1028 /* [BTRFS_MAX_EXTENT_SIZE+4k][4k][BTRFS_MAX_EXTENT_SIZE+4k] */
1029 BTRFS_I(inode)->outstanding_extents++;
1030 ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE+4096,
1031 BTRFS_MAX_EXTENT_SIZE+8191, NULL);
1033 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
1036 if (BTRFS_I(inode)->outstanding_extents != 3) {
1038 test_msg("Miscount, wanted 3, got %u\n",
1039 BTRFS_I(inode)->outstanding_extents);
1043 /* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */
1044 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
1045 BTRFS_MAX_EXTENT_SIZE+4096,
1046 BTRFS_MAX_EXTENT_SIZE+8191,
1047 EXTENT_DIRTY | EXTENT_DELALLOC |
1048 EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0,
1051 test_msg("clear_extent_bit returned %d\n", ret);
1054 if (BTRFS_I(inode)->outstanding_extents != 4) {
1056 test_msg("Miscount, wanted 4, got %u\n",
1057 BTRFS_I(inode)->outstanding_extents);
1062 * Refill the hole again just for good measure, because I thought it
1063 * might fail and I'd rather satisfy my paranoia at this point.
1065 BTRFS_I(inode)->outstanding_extents++;
1066 ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE+4096,
1067 BTRFS_MAX_EXTENT_SIZE+8191, NULL);
1069 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
1072 if (BTRFS_I(inode)->outstanding_extents != 3) {
1074 test_msg("Miscount, wanted 3, got %u\n",
1075 BTRFS_I(inode)->outstanding_extents);
1080 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1081 EXTENT_DIRTY | EXTENT_DELALLOC |
1082 EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0,
1085 test_msg("clear_extent_bit returned %d\n", ret);
1088 if (BTRFS_I(inode)->outstanding_extents) {
1090 test_msg("Miscount, wanted 0, got %u\n",
1091 BTRFS_I(inode)->outstanding_extents);
1097 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1098 EXTENT_DIRTY | EXTENT_DELALLOC |
1099 EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0,
1102 btrfs_free_dummy_root(root);
1106 int btrfs_test_inodes(void)
1110 set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
1111 set_bit(EXTENT_FLAG_VACANCY, &vacancy_only);
1112 set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
1114 test_msg("Running btrfs_get_extent tests\n");
1115 ret = test_btrfs_get_extent();
1118 test_msg("Running hole first btrfs_get_extent test\n");
1119 ret = test_hole_first();
1122 test_msg("Running outstanding_extents tests\n");
1123 return test_extent_accounting();