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 <linux/types.h>
20 #include "btrfs-tests.h"
22 #include "../btrfs_inode.h"
23 #include "../disk-io.h"
24 #include "../extent_io.h"
25 #include "../volumes.h"
26 #include "../compression.h"
28 static void insert_extent(struct btrfs_root *root, u64 start, u64 len,
29 u64 ram_bytes, u64 offset, u64 disk_bytenr,
30 u64 disk_len, u32 type, u8 compression, int slot)
32 struct btrfs_path path;
33 struct btrfs_file_extent_item *fi;
34 struct extent_buffer *leaf = root->node;
36 u32 value_len = sizeof(struct btrfs_file_extent_item);
38 if (type == BTRFS_FILE_EXTENT_INLINE)
40 memset(&path, 0, sizeof(path));
45 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
46 key.type = BTRFS_EXTENT_DATA_KEY;
49 setup_items_for_insert(root, &path, &key, &value_len, value_len,
50 value_len + sizeof(struct btrfs_item), 1);
51 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
52 btrfs_set_file_extent_generation(leaf, fi, 1);
53 btrfs_set_file_extent_type(leaf, fi, type);
54 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
55 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_len);
56 btrfs_set_file_extent_offset(leaf, fi, offset);
57 btrfs_set_file_extent_num_bytes(leaf, fi, len);
58 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
59 btrfs_set_file_extent_compression(leaf, fi, compression);
60 btrfs_set_file_extent_encryption(leaf, fi, 0);
61 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
64 static void insert_inode_item_key(struct btrfs_root *root)
66 struct btrfs_path path;
67 struct extent_buffer *leaf = root->node;
71 memset(&path, 0, sizeof(path));
76 key.objectid = BTRFS_INODE_ITEM_KEY;
77 key.type = BTRFS_INODE_ITEM_KEY;
80 setup_items_for_insert(root, &path, &key, &value_len, value_len,
81 value_len + sizeof(struct btrfs_item), 1);
85 * Build the most complicated map of extents the earth has ever seen. We want
86 * this so we can test all of the corner cases of btrfs_get_extent. Here is a
87 * diagram of how the extents will look though this may not be possible we still
88 * want to make sure everything acts normally (the last number is not inclusive)
90 * [0 - 5][5 - 6][ 6 - 4096 ][ 4096 - 4100][4100 - 8195][8195 - 12291]
91 * [hole ][inline][hole but no extent][ hole ][ regular ][regular1 split]
93 * [12291 - 16387][16387 - 24579][24579 - 28675][ 28675 - 32771][32771 - 36867 ]
94 * [ hole ][regular1 split][ prealloc ][ prealloc1 ][prealloc1 written]
96 * [36867 - 45059][45059 - 53251][53251 - 57347][57347 - 61443][61443- 69635]
97 * [ prealloc1 ][ compressed ][ compressed1 ][ regular ][ compressed1]
99 * [69635-73731][ 73731 - 86019 ][86019-90115]
100 * [ regular ][ hole but no extent][ regular ]
102 static void setup_file_extents(struct btrfs_root *root, u32 sectorsize)
105 u64 disk_bytenr = SZ_1M;
108 /* First we want a hole */
109 insert_extent(root, offset, 5, 5, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
115 * Now we want an inline extent, I don't think this is possible but hey
116 * why not? Also keep in mind if we have an inline extent it counts as
117 * the whole first page. If we were to expand it we would have to cow
118 * and we wouldn't have an inline extent anymore.
120 insert_extent(root, offset, 1, 1, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE, 0,
125 /* Now another hole */
126 insert_extent(root, offset, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
131 /* Now for a regular extent */
132 insert_extent(root, offset, sectorsize - 1, sectorsize - 1, 0,
133 disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
135 disk_bytenr += sectorsize;
136 offset += sectorsize - 1;
139 * Now for 3 extents that were split from a hole punch so we test
142 insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
143 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
145 offset += sectorsize;
146 insert_extent(root, offset, sectorsize, sectorsize, 0, 0, 0,
147 BTRFS_FILE_EXTENT_REG, 0, slot);
149 offset += sectorsize;
150 insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
151 2 * sectorsize, disk_bytenr, 4 * sectorsize,
152 BTRFS_FILE_EXTENT_REG, 0, slot);
154 offset += 2 * sectorsize;
155 disk_bytenr += 4 * sectorsize;
157 /* Now for a unwritten prealloc extent */
158 insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
159 sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
161 offset += sectorsize;
164 * We want to jack up disk_bytenr a little more so the em stuff doesn't
167 disk_bytenr += 2 * sectorsize;
170 * Now for a partially written prealloc extent, basically the same as
171 * the hole punch example above. Ram_bytes never changes when you mark
172 * extents written btw.
174 insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
175 4 * sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
177 offset += sectorsize;
178 insert_extent(root, offset, sectorsize, 4 * sectorsize, sectorsize,
179 disk_bytenr, 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0,
182 offset += sectorsize;
183 insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
184 2 * sectorsize, disk_bytenr, 4 * sectorsize,
185 BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
187 offset += 2 * sectorsize;
188 disk_bytenr += 4 * sectorsize;
190 /* Now a normal compressed extent */
191 insert_extent(root, offset, 2 * sectorsize, 2 * sectorsize, 0,
192 disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG,
193 BTRFS_COMPRESS_ZLIB, slot);
195 offset += 2 * sectorsize;
197 disk_bytenr += 2 * sectorsize;
199 /* Now a split compressed extent */
200 insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
201 sectorsize, BTRFS_FILE_EXTENT_REG,
202 BTRFS_COMPRESS_ZLIB, slot);
204 offset += sectorsize;
205 insert_extent(root, offset, sectorsize, sectorsize, 0,
206 disk_bytenr + sectorsize, sectorsize,
207 BTRFS_FILE_EXTENT_REG, 0, slot);
209 offset += sectorsize;
210 insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
211 2 * sectorsize, disk_bytenr, sectorsize,
212 BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
214 offset += 2 * sectorsize;
215 disk_bytenr += 2 * sectorsize;
217 /* Now extents that have a hole but no hole extent */
218 insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
219 sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
221 offset += 4 * sectorsize;
222 disk_bytenr += sectorsize;
223 insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
224 sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
227 static unsigned long prealloc_only = 0;
228 static unsigned long compressed_only = 0;
229 static unsigned long vacancy_only = 0;
231 static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize)
233 struct btrfs_fs_info *fs_info = NULL;
234 struct inode *inode = NULL;
235 struct btrfs_root *root = NULL;
236 struct extent_map *em = NULL;
242 inode = btrfs_new_test_inode();
244 test_msg("Couldn't allocate inode\n");
248 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
249 BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
250 BTRFS_I(inode)->location.offset = 0;
252 fs_info = btrfs_alloc_dummy_fs_info();
254 test_msg("Couldn't allocate dummy fs info\n");
258 root = btrfs_alloc_dummy_root(fs_info, sectorsize, nodesize);
260 test_msg("Couldn't allocate root\n");
264 root->node = alloc_dummy_extent_buffer(NULL, nodesize, nodesize);
266 test_msg("Couldn't allocate dummy buffer\n");
271 * We will just free a dummy node if it's ref count is 2 so we need an
272 * extra ref so our searches don't accidentally release our page.
274 extent_buffer_get(root->node);
275 btrfs_set_header_nritems(root->node, 0);
276 btrfs_set_header_level(root->node, 0);
279 /* First with no extents */
280 BTRFS_I(inode)->root = root;
281 em = btrfs_get_extent(inode, NULL, 0, 0, sectorsize, 0);
284 test_msg("Got an error when we shouldn't have\n");
287 if (em->block_start != EXTENT_MAP_HOLE) {
288 test_msg("Expected a hole, got %llu\n", em->block_start);
291 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
292 test_msg("Vacancy flag wasn't set properly\n");
296 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
299 * All of the magic numbers are based on the mapping setup in
300 * setup_file_extents, so if you change anything there you need to
301 * update the comment and update the expected values below.
303 setup_file_extents(root, sectorsize);
305 em = btrfs_get_extent(inode, NULL, 0, 0, (u64)-1, 0);
307 test_msg("Got an error when we shouldn't have\n");
310 if (em->block_start != EXTENT_MAP_HOLE) {
311 test_msg("Expected a hole, got %llu\n", em->block_start);
314 if (em->start != 0 || em->len != 5) {
315 test_msg("Unexpected extent wanted start 0 len 5, got start "
316 "%llu len %llu\n", em->start, em->len);
319 if (em->flags != 0) {
320 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
323 offset = em->start + em->len;
326 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
328 test_msg("Got an error when we shouldn't have\n");
331 if (em->block_start != EXTENT_MAP_INLINE) {
332 test_msg("Expected an inline, got %llu\n", em->block_start);
336 if (em->start != offset || em->len != (sectorsize - 5)) {
337 test_msg("Unexpected extent wanted start %llu len 1, got start "
338 "%llu len %llu\n", offset, em->start, em->len);
341 if (em->flags != 0) {
342 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
346 * We don't test anything else for inline since it doesn't get set
347 * unless we have a page for it to write into. Maybe we should change
350 offset = em->start + em->len;
353 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
355 test_msg("Got an error when we shouldn't have\n");
358 if (em->block_start != EXTENT_MAP_HOLE) {
359 test_msg("Expected a hole, got %llu\n", em->block_start);
362 if (em->start != offset || em->len != 4) {
363 test_msg("Unexpected extent wanted start %llu len 4, got start "
364 "%llu len %llu\n", offset, em->start, em->len);
367 if (em->flags != 0) {
368 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
371 offset = em->start + em->len;
375 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
377 test_msg("Got an error when we shouldn't have\n");
380 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
381 test_msg("Expected a real extent, got %llu\n", em->block_start);
384 if (em->start != offset || em->len != sectorsize - 1) {
385 test_msg("Unexpected extent wanted start %llu len 4095, got "
386 "start %llu len %llu\n", offset, em->start, em->len);
389 if (em->flags != 0) {
390 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
393 if (em->orig_start != em->start) {
394 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
398 offset = em->start + em->len;
401 /* The next 3 are split extents */
402 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
404 test_msg("Got an error when we shouldn't have\n");
407 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
408 test_msg("Expected a real extent, got %llu\n", em->block_start);
411 if (em->start != offset || em->len != sectorsize) {
412 test_msg("Unexpected extent start %llu len %u, "
413 "got start %llu len %llu\n",
414 offset, sectorsize, em->start, em->len);
417 if (em->flags != 0) {
418 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
421 if (em->orig_start != em->start) {
422 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
426 disk_bytenr = em->block_start;
427 orig_start = em->start;
428 offset = em->start + em->len;
431 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
433 test_msg("Got an error when we shouldn't have\n");
436 if (em->block_start != EXTENT_MAP_HOLE) {
437 test_msg("Expected a hole, got %llu\n", em->block_start);
440 if (em->start != offset || em->len != sectorsize) {
441 test_msg("Unexpected extent wanted start %llu len %u, "
442 "got start %llu len %llu\n",
443 offset, sectorsize, em->start, em->len);
446 if (em->flags != 0) {
447 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
450 offset = em->start + em->len;
453 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
455 test_msg("Got an error when we shouldn't have\n");
458 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
459 test_msg("Expected a real extent, got %llu\n", em->block_start);
462 if (em->start != offset || em->len != 2 * sectorsize) {
463 test_msg("Unexpected extent wanted start %llu len %u, "
464 "got start %llu len %llu\n",
465 offset, 2 * sectorsize, em->start, em->len);
468 if (em->flags != 0) {
469 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
472 if (em->orig_start != orig_start) {
473 test_msg("Wrong orig offset, want %llu, have %llu\n",
474 orig_start, em->orig_start);
477 disk_bytenr += (em->start - orig_start);
478 if (em->block_start != disk_bytenr) {
479 test_msg("Wrong block start, want %llu, have %llu\n",
480 disk_bytenr, em->block_start);
483 offset = em->start + em->len;
486 /* Prealloc extent */
487 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
489 test_msg("Got an error when we shouldn't have\n");
492 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
493 test_msg("Expected a real extent, got %llu\n", em->block_start);
496 if (em->start != offset || em->len != sectorsize) {
497 test_msg("Unexpected extent wanted start %llu len %u, "
498 "got start %llu len %llu\n",
499 offset, sectorsize, em->start, em->len);
502 if (em->flags != prealloc_only) {
503 test_msg("Unexpected flags set, want %lu have %lu\n",
504 prealloc_only, em->flags);
507 if (em->orig_start != em->start) {
508 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
512 offset = em->start + em->len;
515 /* The next 3 are a half written prealloc extent */
516 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
518 test_msg("Got an error when we shouldn't have\n");
521 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
522 test_msg("Expected a real extent, got %llu\n", em->block_start);
525 if (em->start != offset || em->len != sectorsize) {
526 test_msg("Unexpected extent wanted start %llu len %u, "
527 "got start %llu len %llu\n",
528 offset, sectorsize, em->start, em->len);
531 if (em->flags != prealloc_only) {
532 test_msg("Unexpected flags set, want %lu have %lu\n",
533 prealloc_only, em->flags);
536 if (em->orig_start != em->start) {
537 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
541 disk_bytenr = em->block_start;
542 orig_start = em->start;
543 offset = em->start + em->len;
546 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
548 test_msg("Got an error when we shouldn't have\n");
551 if (em->block_start >= EXTENT_MAP_HOLE) {
552 test_msg("Expected a real extent, got %llu\n", em->block_start);
555 if (em->start != offset || em->len != sectorsize) {
556 test_msg("Unexpected extent wanted start %llu len %u, "
557 "got start %llu len %llu\n",
558 offset, sectorsize, em->start, em->len);
561 if (em->flags != 0) {
562 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
565 if (em->orig_start != orig_start) {
566 test_msg("Unexpected orig offset, wanted %llu, have %llu\n",
567 orig_start, em->orig_start);
570 if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
571 test_msg("Unexpected block start, wanted %llu, have %llu\n",
572 disk_bytenr + (em->start - em->orig_start),
576 offset = em->start + em->len;
579 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
581 test_msg("Got an error when we shouldn't have\n");
584 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
585 test_msg("Expected a real extent, got %llu\n", em->block_start);
588 if (em->start != offset || em->len != 2 * sectorsize) {
589 test_msg("Unexpected extent wanted start %llu len %u, "
590 "got start %llu len %llu\n",
591 offset, 2 * sectorsize, em->start, em->len);
594 if (em->flags != prealloc_only) {
595 test_msg("Unexpected flags set, want %lu have %lu\n",
596 prealloc_only, em->flags);
599 if (em->orig_start != orig_start) {
600 test_msg("Wrong orig offset, want %llu, have %llu\n", orig_start,
604 if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
605 test_msg("Unexpected block start, wanted %llu, have %llu\n",
606 disk_bytenr + (em->start - em->orig_start),
610 offset = em->start + em->len;
613 /* Now for the compressed extent */
614 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
616 test_msg("Got an error when we shouldn't have\n");
619 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
620 test_msg("Expected a real extent, got %llu\n", em->block_start);
623 if (em->start != offset || em->len != 2 * sectorsize) {
624 test_msg("Unexpected extent wanted start %llu len %u,"
625 "got start %llu len %llu\n",
626 offset, 2 * sectorsize, em->start, em->len);
629 if (em->flags != compressed_only) {
630 test_msg("Unexpected flags set, want %lu have %lu\n",
631 compressed_only, em->flags);
634 if (em->orig_start != em->start) {
635 test_msg("Wrong orig offset, want %llu, have %llu\n",
636 em->start, em->orig_start);
639 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
640 test_msg("Unexpected compress type, wanted %d, got %d\n",
641 BTRFS_COMPRESS_ZLIB, em->compress_type);
644 offset = em->start + em->len;
647 /* Split compressed extent */
648 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
650 test_msg("Got an error when we shouldn't have\n");
653 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
654 test_msg("Expected a real extent, got %llu\n", em->block_start);
657 if (em->start != offset || em->len != sectorsize) {
658 test_msg("Unexpected extent wanted start %llu len %u,"
659 "got start %llu len %llu\n",
660 offset, sectorsize, em->start, em->len);
663 if (em->flags != compressed_only) {
664 test_msg("Unexpected flags set, want %lu have %lu\n",
665 compressed_only, em->flags);
668 if (em->orig_start != em->start) {
669 test_msg("Wrong orig offset, want %llu, have %llu\n",
670 em->start, em->orig_start);
673 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
674 test_msg("Unexpected compress type, wanted %d, got %d\n",
675 BTRFS_COMPRESS_ZLIB, em->compress_type);
678 disk_bytenr = em->block_start;
679 orig_start = em->start;
680 offset = em->start + em->len;
683 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
685 test_msg("Got an error when we shouldn't have\n");
688 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
689 test_msg("Expected a real extent, got %llu\n", em->block_start);
692 if (em->start != offset || em->len != sectorsize) {
693 test_msg("Unexpected extent wanted start %llu len %u, "
694 "got start %llu len %llu\n",
695 offset, sectorsize, em->start, em->len);
698 if (em->flags != 0) {
699 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
702 if (em->orig_start != em->start) {
703 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
707 offset = em->start + em->len;
710 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
712 test_msg("Got an error when we shouldn't have\n");
715 if (em->block_start != disk_bytenr) {
716 test_msg("Block start does not match, want %llu got %llu\n",
717 disk_bytenr, em->block_start);
720 if (em->start != offset || em->len != 2 * sectorsize) {
721 test_msg("Unexpected extent wanted start %llu len %u, "
722 "got start %llu len %llu\n",
723 offset, 2 * sectorsize, em->start, em->len);
726 if (em->flags != compressed_only) {
727 test_msg("Unexpected flags set, want %lu have %lu\n",
728 compressed_only, em->flags);
731 if (em->orig_start != orig_start) {
732 test_msg("Wrong orig offset, want %llu, have %llu\n",
733 em->start, orig_start);
736 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
737 test_msg("Unexpected compress type, wanted %d, got %d\n",
738 BTRFS_COMPRESS_ZLIB, em->compress_type);
741 offset = em->start + em->len;
744 /* A hole between regular extents but no hole extent */
745 em = btrfs_get_extent(inode, NULL, 0, offset + 6, sectorsize, 0);
747 test_msg("Got an error when we shouldn't have\n");
750 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
751 test_msg("Expected a real extent, got %llu\n", em->block_start);
754 if (em->start != offset || em->len != sectorsize) {
755 test_msg("Unexpected extent wanted start %llu len %u, "
756 "got start %llu len %llu\n",
757 offset, sectorsize, em->start, em->len);
760 if (em->flags != 0) {
761 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
764 if (em->orig_start != em->start) {
765 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
769 offset = em->start + em->len;
772 em = btrfs_get_extent(inode, NULL, 0, offset, 4096 * 1024, 0);
774 test_msg("Got an error when we shouldn't have\n");
777 if (em->block_start != EXTENT_MAP_HOLE) {
778 test_msg("Expected a hole extent, got %llu\n", em->block_start);
782 * Currently we just return a length that we requested rather than the
783 * length of the actual hole, if this changes we'll have to change this
786 if (em->start != offset || em->len != 3 * sectorsize) {
787 test_msg("Unexpected extent wanted start %llu len %u, "
788 "got start %llu len %llu\n",
789 offset, 3 * sectorsize, em->start, em->len);
792 if (em->flags != vacancy_only) {
793 test_msg("Unexpected flags set, want %lu have %lu\n",
794 vacancy_only, em->flags);
797 if (em->orig_start != em->start) {
798 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
802 offset = em->start + em->len;
805 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
807 test_msg("Got an error when we shouldn't have\n");
810 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
811 test_msg("Expected a real extent, got %llu\n", em->block_start);
814 if (em->start != offset || em->len != sectorsize) {
815 test_msg("Unexpected extent wanted start %llu len %u,"
816 "got start %llu len %llu\n",
817 offset, sectorsize, em->start, em->len);
820 if (em->flags != 0) {
821 test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
824 if (em->orig_start != em->start) {
825 test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
834 btrfs_free_dummy_root(root);
835 btrfs_free_dummy_fs_info(fs_info);
839 static int test_hole_first(u32 sectorsize, u32 nodesize)
841 struct btrfs_fs_info *fs_info = NULL;
842 struct inode *inode = NULL;
843 struct btrfs_root *root = NULL;
844 struct extent_map *em = NULL;
847 inode = btrfs_new_test_inode();
849 test_msg("Couldn't allocate inode\n");
853 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
854 BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
855 BTRFS_I(inode)->location.offset = 0;
857 fs_info = btrfs_alloc_dummy_fs_info();
859 test_msg("Couldn't allocate dummy fs info\n");
863 root = btrfs_alloc_dummy_root(fs_info, sectorsize, nodesize);
865 test_msg("Couldn't allocate root\n");
869 root->node = alloc_dummy_extent_buffer(NULL, nodesize, nodesize);
871 test_msg("Couldn't allocate dummy buffer\n");
875 extent_buffer_get(root->node);
876 btrfs_set_header_nritems(root->node, 0);
877 btrfs_set_header_level(root->node, 0);
878 BTRFS_I(inode)->root = root;
882 * Need a blank inode item here just so we don't confuse
885 insert_inode_item_key(root);
886 insert_extent(root, sectorsize, sectorsize, sectorsize, 0, sectorsize,
887 sectorsize, BTRFS_FILE_EXTENT_REG, 0, 1);
888 em = btrfs_get_extent(inode, NULL, 0, 0, 2 * sectorsize, 0);
890 test_msg("Got an error when we shouldn't have\n");
893 if (em->block_start != EXTENT_MAP_HOLE) {
894 test_msg("Expected a hole, got %llu\n", em->block_start);
897 if (em->start != 0 || em->len != sectorsize) {
898 test_msg("Unexpected extent wanted start 0 len %u, "
899 "got start %llu len %llu\n",
900 sectorsize, em->start, em->len);
903 if (em->flags != vacancy_only) {
904 test_msg("Wrong flags, wanted %lu, have %lu\n", vacancy_only,
910 em = btrfs_get_extent(inode, NULL, 0, sectorsize, 2 * sectorsize, 0);
912 test_msg("Got an error when we shouldn't have\n");
915 if (em->block_start != sectorsize) {
916 test_msg("Expected a real extent, got %llu\n", em->block_start);
919 if (em->start != sectorsize || em->len != sectorsize) {
920 test_msg("Unexpected extent wanted start %u len %u, "
921 "got start %llu len %llu\n",
922 sectorsize, sectorsize, em->start, em->len);
925 if (em->flags != 0) {
926 test_msg("Unexpected flags set, wanted 0 got %lu\n",
935 btrfs_free_dummy_root(root);
936 btrfs_free_dummy_fs_info(fs_info);
940 static int test_extent_accounting(u32 sectorsize, u32 nodesize)
942 struct btrfs_fs_info *fs_info = NULL;
943 struct inode *inode = NULL;
944 struct btrfs_root *root = NULL;
947 inode = btrfs_new_test_inode();
949 test_msg("Couldn't allocate inode\n");
953 fs_info = btrfs_alloc_dummy_fs_info();
955 test_msg("Couldn't allocate dummy fs info\n");
959 root = btrfs_alloc_dummy_root(fs_info, sectorsize, nodesize);
961 test_msg("Couldn't allocate root\n");
965 BTRFS_I(inode)->root = root;
966 btrfs_test_inode_set_ops(inode);
968 /* [BTRFS_MAX_EXTENT_SIZE] */
969 BTRFS_I(inode)->outstanding_extents++;
970 ret = btrfs_set_extent_delalloc(inode, 0, BTRFS_MAX_EXTENT_SIZE - 1,
973 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
976 if (BTRFS_I(inode)->outstanding_extents != 1) {
978 test_msg("Miscount, wanted 1, got %u\n",
979 BTRFS_I(inode)->outstanding_extents);
983 /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
984 BTRFS_I(inode)->outstanding_extents++;
985 ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE,
986 BTRFS_MAX_EXTENT_SIZE + sectorsize - 1,
989 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
992 if (BTRFS_I(inode)->outstanding_extents != 2) {
994 test_msg("Miscount, wanted 2, got %u\n",
995 BTRFS_I(inode)->outstanding_extents);
999 /* [BTRFS_MAX_EXTENT_SIZE/2][sectorsize HOLE][the rest] */
1000 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
1001 BTRFS_MAX_EXTENT_SIZE >> 1,
1002 (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1,
1003 EXTENT_DELALLOC | EXTENT_DIRTY |
1004 EXTENT_UPTODATE | EXTENT_DO_ACCOUNTING, 0, 0,
1007 test_msg("clear_extent_bit returned %d\n", ret);
1010 if (BTRFS_I(inode)->outstanding_extents != 2) {
1012 test_msg("Miscount, wanted 2, got %u\n",
1013 BTRFS_I(inode)->outstanding_extents);
1017 /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
1018 BTRFS_I(inode)->outstanding_extents++;
1019 ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE >> 1,
1020 (BTRFS_MAX_EXTENT_SIZE >> 1)
1024 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
1027 if (BTRFS_I(inode)->outstanding_extents != 2) {
1029 test_msg("Miscount, wanted 2, got %u\n",
1030 BTRFS_I(inode)->outstanding_extents);
1035 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize]
1037 * I'm artificially adding 2 to outstanding_extents because in the
1038 * buffered IO case we'd add things up as we go, but I don't feel like
1039 * doing that here, this isn't the interesting case we want to test.
1041 BTRFS_I(inode)->outstanding_extents += 2;
1042 ret = btrfs_set_extent_delalloc(inode,
1043 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize,
1044 (BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1,
1047 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
1050 if (BTRFS_I(inode)->outstanding_extents != 4) {
1052 test_msg("Miscount, wanted 4, got %u\n",
1053 BTRFS_I(inode)->outstanding_extents);
1058 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize]
1060 BTRFS_I(inode)->outstanding_extents++;
1061 ret = btrfs_set_extent_delalloc(inode,
1062 BTRFS_MAX_EXTENT_SIZE + sectorsize,
1063 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, NULL);
1065 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
1068 if (BTRFS_I(inode)->outstanding_extents != 3) {
1070 test_msg("Miscount, wanted 3, got %u\n",
1071 BTRFS_I(inode)->outstanding_extents);
1075 /* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */
1076 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
1077 BTRFS_MAX_EXTENT_SIZE + sectorsize,
1078 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1,
1079 EXTENT_DIRTY | EXTENT_DELALLOC |
1080 EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0,
1083 test_msg("clear_extent_bit returned %d\n", ret);
1086 if (BTRFS_I(inode)->outstanding_extents != 4) {
1088 test_msg("Miscount, wanted 4, got %u\n",
1089 BTRFS_I(inode)->outstanding_extents);
1094 * Refill the hole again just for good measure, because I thought it
1095 * might fail and I'd rather satisfy my paranoia at this point.
1097 BTRFS_I(inode)->outstanding_extents++;
1098 ret = btrfs_set_extent_delalloc(inode,
1099 BTRFS_MAX_EXTENT_SIZE + sectorsize,
1100 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, NULL);
1102 test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
1105 if (BTRFS_I(inode)->outstanding_extents != 3) {
1107 test_msg("Miscount, wanted 3, got %u\n",
1108 BTRFS_I(inode)->outstanding_extents);
1113 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1114 EXTENT_DIRTY | EXTENT_DELALLOC |
1115 EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0,
1118 test_msg("clear_extent_bit returned %d\n", ret);
1121 if (BTRFS_I(inode)->outstanding_extents) {
1123 test_msg("Miscount, wanted 0, got %u\n",
1124 BTRFS_I(inode)->outstanding_extents);
1130 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1131 EXTENT_DIRTY | EXTENT_DELALLOC |
1132 EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0,
1135 btrfs_free_dummy_root(root);
1136 btrfs_free_dummy_fs_info(fs_info);
1140 int btrfs_test_inodes(u32 sectorsize, u32 nodesize)
1144 set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
1145 set_bit(EXTENT_FLAG_VACANCY, &vacancy_only);
1146 set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
1148 test_msg("Running btrfs_get_extent tests\n");
1149 ret = test_btrfs_get_extent(sectorsize, nodesize);
1152 test_msg("Running hole first btrfs_get_extent test\n");
1153 ret = test_hole_first(sectorsize, nodesize);
1156 test_msg("Running outstanding_extents tests\n");
1157 return test_extent_accounting(sectorsize, nodesize);