xref: /openbmc/linux/fs/btrfs/tests/inode-tests.c (revision d5e09e38)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
2aaedb55bSJosef Bacik /*
3aaedb55bSJosef Bacik  * Copyright (C) 2013 Fusion IO.  All rights reserved.
4aaedb55bSJosef Bacik  */
5aaedb55bSJosef Bacik 
6b9ef22deSFeifei Xu #include <linux/types.h>
7aaedb55bSJosef Bacik #include "btrfs-tests.h"
8aaedb55bSJosef Bacik #include "../ctree.h"
9aaedb55bSJosef Bacik #include "../btrfs_inode.h"
10aaedb55bSJosef Bacik #include "../disk-io.h"
11aaedb55bSJosef Bacik #include "../extent_io.h"
12aaedb55bSJosef Bacik #include "../volumes.h"
13ebb8765bSAnand Jain #include "../compression.h"
1407e81dc9SJosef Bacik #include "../accessors.h"
15aaedb55bSJosef Bacik 
insert_extent(struct btrfs_root * root,u64 start,u64 len,u64 ram_bytes,u64 offset,u64 disk_bytenr,u64 disk_len,u32 type,u8 compression,int slot)16aaedb55bSJosef Bacik static void insert_extent(struct btrfs_root *root, u64 start, u64 len,
17aaedb55bSJosef Bacik 			  u64 ram_bytes, u64 offset, u64 disk_bytenr,
18aaedb55bSJosef Bacik 			  u64 disk_len, u32 type, u8 compression, int slot)
19aaedb55bSJosef Bacik {
20aaedb55bSJosef Bacik 	struct btrfs_path path;
21aaedb55bSJosef Bacik 	struct btrfs_file_extent_item *fi;
22aaedb55bSJosef Bacik 	struct extent_buffer *leaf = root->node;
23aaedb55bSJosef Bacik 	struct btrfs_key key;
24aaedb55bSJosef Bacik 	u32 value_len = sizeof(struct btrfs_file_extent_item);
25aaedb55bSJosef Bacik 
26aaedb55bSJosef Bacik 	if (type == BTRFS_FILE_EXTENT_INLINE)
27aaedb55bSJosef Bacik 		value_len += len;
28aaedb55bSJosef Bacik 	memset(&path, 0, sizeof(path));
29aaedb55bSJosef Bacik 
30aaedb55bSJosef Bacik 	path.nodes[0] = leaf;
31aaedb55bSJosef Bacik 	path.slots[0] = slot;
32aaedb55bSJosef Bacik 
33aaedb55bSJosef Bacik 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
34aaedb55bSJosef Bacik 	key.type = BTRFS_EXTENT_DATA_KEY;
35aaedb55bSJosef Bacik 	key.offset = start;
36aaedb55bSJosef Bacik 
37*d5e09e38SFilipe Manana 	/*
38*d5e09e38SFilipe Manana 	 * Passing a NULL trans handle is fine here, we have a dummy root eb
39*d5e09e38SFilipe Manana 	 * and the tree is a single node (level 0).
40*d5e09e38SFilipe Manana 	 */
41*d5e09e38SFilipe Manana 	btrfs_setup_item_for_insert(NULL, root, &path, &key, value_len);
42aaedb55bSJosef Bacik 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
43aaedb55bSJosef Bacik 	btrfs_set_file_extent_generation(leaf, fi, 1);
44aaedb55bSJosef Bacik 	btrfs_set_file_extent_type(leaf, fi, type);
45aaedb55bSJosef Bacik 	btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
46aaedb55bSJosef Bacik 	btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_len);
47aaedb55bSJosef Bacik 	btrfs_set_file_extent_offset(leaf, fi, offset);
48aaedb55bSJosef Bacik 	btrfs_set_file_extent_num_bytes(leaf, fi, len);
49aaedb55bSJosef Bacik 	btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
50aaedb55bSJosef Bacik 	btrfs_set_file_extent_compression(leaf, fi, compression);
51aaedb55bSJosef Bacik 	btrfs_set_file_extent_encryption(leaf, fi, 0);
52aaedb55bSJosef Bacik 	btrfs_set_file_extent_other_encoding(leaf, fi, 0);
53aaedb55bSJosef Bacik }
54aaedb55bSJosef Bacik 
insert_inode_item_key(struct btrfs_root * root)550e30db86SJosef Bacik static void insert_inode_item_key(struct btrfs_root *root)
560e30db86SJosef Bacik {
570e30db86SJosef Bacik 	struct btrfs_path path;
580e30db86SJosef Bacik 	struct extent_buffer *leaf = root->node;
590e30db86SJosef Bacik 	struct btrfs_key key;
600e30db86SJosef Bacik 	u32 value_len = 0;
610e30db86SJosef Bacik 
620e30db86SJosef Bacik 	memset(&path, 0, sizeof(path));
630e30db86SJosef Bacik 
640e30db86SJosef Bacik 	path.nodes[0] = leaf;
650e30db86SJosef Bacik 	path.slots[0] = 0;
660e30db86SJosef Bacik 
670e30db86SJosef Bacik 	key.objectid = BTRFS_INODE_ITEM_KEY;
680e30db86SJosef Bacik 	key.type = BTRFS_INODE_ITEM_KEY;
690e30db86SJosef Bacik 	key.offset = 0;
700e30db86SJosef Bacik 
71*d5e09e38SFilipe Manana 	/*
72*d5e09e38SFilipe Manana 	 * Passing a NULL trans handle is fine here, we have a dummy root eb
73*d5e09e38SFilipe Manana 	 * and the tree is a single node (level 0).
74*d5e09e38SFilipe Manana 	 */
75*d5e09e38SFilipe Manana 	btrfs_setup_item_for_insert(NULL, root, &path, &key, value_len);
760e30db86SJosef Bacik }
770e30db86SJosef Bacik 
78aaedb55bSJosef Bacik /*
79aaedb55bSJosef Bacik  * Build the most complicated map of extents the earth has ever seen.  We want
80aaedb55bSJosef Bacik  * this so we can test all of the corner cases of btrfs_get_extent.  Here is a
81aaedb55bSJosef Bacik  * diagram of how the extents will look though this may not be possible we still
82aaedb55bSJosef Bacik  * want to make sure everything acts normally (the last number is not inclusive)
83aaedb55bSJosef Bacik  *
84d52a1365SQu Wenruo  * [0  - 6][     6 - 4096     ][ 4096 - 4100][4100 - 8195][8195  -  12291]
85d52a1365SQu Wenruo  * [inline][hole but no extent][    hole    ][   regular ][regular1 split]
86aaedb55bSJosef Bacik  *
87b9ef22deSFeifei Xu  * [12291 - 16387][16387 - 24579][24579 - 28675][ 28675 - 32771][32771 - 36867 ]
88b9ef22deSFeifei Xu  * [    hole    ][regular1 split][   prealloc ][   prealloc1  ][prealloc1 written]
89aaedb55bSJosef Bacik  *
90b9ef22deSFeifei Xu  * [36867 - 45059][45059 - 53251][53251 - 57347][57347 - 61443][61443- 69635]
91b9ef22deSFeifei Xu  * [  prealloc1  ][ compressed  ][ compressed1 ][    regular  ][ compressed1]
92aaedb55bSJosef Bacik  *
93b9ef22deSFeifei Xu  * [69635-73731][   73731 - 86019   ][86019-90115]
94b9ef22deSFeifei Xu  * [  regular  ][ hole but no extent][  regular  ]
95aaedb55bSJosef Bacik  */
setup_file_extents(struct btrfs_root * root,u32 sectorsize)96b9ef22deSFeifei Xu static void setup_file_extents(struct btrfs_root *root, u32 sectorsize)
97aaedb55bSJosef Bacik {
98aaedb55bSJosef Bacik 	int slot = 0;
99ee22184bSByongho Lee 	u64 disk_bytenr = SZ_1M;
100aaedb55bSJosef Bacik 	u64 offset = 0;
101aaedb55bSJosef Bacik 
102aaedb55bSJosef Bacik 	/*
103d52a1365SQu Wenruo 	 * Tree-checker has strict limits on inline extents that they can only
104d52a1365SQu Wenruo 	 * exist at file offset 0, thus we can only have one inline file extent
105d52a1365SQu Wenruo 	 * at most.
106aaedb55bSJosef Bacik 	 */
107d52a1365SQu Wenruo 	insert_extent(root, offset, 6, 6, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE, 0,
108aaedb55bSJosef Bacik 		      slot);
109aaedb55bSJosef Bacik 	slot++;
110b9ef22deSFeifei Xu 	offset = sectorsize;
111aaedb55bSJosef Bacik 
112aaedb55bSJosef Bacik 	/* Now another hole */
113aaedb55bSJosef Bacik 	insert_extent(root, offset, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
114aaedb55bSJosef Bacik 		      slot);
115aaedb55bSJosef Bacik 	slot++;
116aaedb55bSJosef Bacik 	offset += 4;
117aaedb55bSJosef Bacik 
118aaedb55bSJosef Bacik 	/* Now for a regular extent */
119b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize - 1, sectorsize - 1, 0,
120b9ef22deSFeifei Xu 		      disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
121aaedb55bSJosef Bacik 	slot++;
122b9ef22deSFeifei Xu 	disk_bytenr += sectorsize;
123b9ef22deSFeifei Xu 	offset += sectorsize - 1;
124aaedb55bSJosef Bacik 
125aaedb55bSJosef Bacik 	/*
126aaedb55bSJosef Bacik 	 * Now for 3 extents that were split from a hole punch so we test
127aaedb55bSJosef Bacik 	 * offsets properly.
128aaedb55bSJosef Bacik 	 */
129b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
130b9ef22deSFeifei Xu 		      4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
131b9ef22deSFeifei Xu 	slot++;
132b9ef22deSFeifei Xu 	offset += sectorsize;
133b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0, 0, 0,
134aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
135aaedb55bSJosef Bacik 	slot++;
136b9ef22deSFeifei Xu 	offset += sectorsize;
137b9ef22deSFeifei Xu 	insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
138b9ef22deSFeifei Xu 		      2 * sectorsize, disk_bytenr, 4 * sectorsize,
139aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
140aaedb55bSJosef Bacik 	slot++;
141b9ef22deSFeifei Xu 	offset += 2 * sectorsize;
142b9ef22deSFeifei Xu 	disk_bytenr += 4 * sectorsize;
143aaedb55bSJosef Bacik 
144aaedb55bSJosef Bacik 	/* Now for a unwritten prealloc extent */
145b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
146b9ef22deSFeifei Xu 		sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
147aaedb55bSJosef Bacik 	slot++;
148b9ef22deSFeifei Xu 	offset += sectorsize;
149aaedb55bSJosef Bacik 
150aaedb55bSJosef Bacik 	/*
151aaedb55bSJosef Bacik 	 * We want to jack up disk_bytenr a little more so the em stuff doesn't
152aaedb55bSJosef Bacik 	 * merge our records.
153aaedb55bSJosef Bacik 	 */
154b9ef22deSFeifei Xu 	disk_bytenr += 2 * sectorsize;
155aaedb55bSJosef Bacik 
156aaedb55bSJosef Bacik 	/*
157aaedb55bSJosef Bacik 	 * Now for a partially written prealloc extent, basically the same as
158aaedb55bSJosef Bacik 	 * the hole punch example above.  Ram_bytes never changes when you mark
159aaedb55bSJosef Bacik 	 * extents written btw.
160aaedb55bSJosef Bacik 	 */
161b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
162b9ef22deSFeifei Xu 		      4 * sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
163b9ef22deSFeifei Xu 	slot++;
164b9ef22deSFeifei Xu 	offset += sectorsize;
165b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, 4 * sectorsize, sectorsize,
166b9ef22deSFeifei Xu 		      disk_bytenr, 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0,
167b9ef22deSFeifei Xu 		      slot);
168b9ef22deSFeifei Xu 	slot++;
169b9ef22deSFeifei Xu 	offset += sectorsize;
170b9ef22deSFeifei Xu 	insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
171b9ef22deSFeifei Xu 		      2 * sectorsize, disk_bytenr, 4 * sectorsize,
172aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
173aaedb55bSJosef Bacik 	slot++;
174b9ef22deSFeifei Xu 	offset += 2 * sectorsize;
175b9ef22deSFeifei Xu 	disk_bytenr += 4 * sectorsize;
176aaedb55bSJosef Bacik 
177aaedb55bSJosef Bacik 	/* Now a normal compressed extent */
178b9ef22deSFeifei Xu 	insert_extent(root, offset, 2 * sectorsize, 2 * sectorsize, 0,
179b9ef22deSFeifei Xu 		      disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG,
180b9ef22deSFeifei Xu 		      BTRFS_COMPRESS_ZLIB, slot);
181aaedb55bSJosef Bacik 	slot++;
182b9ef22deSFeifei Xu 	offset += 2 * sectorsize;
183aaedb55bSJosef Bacik 	/* No merges */
184b9ef22deSFeifei Xu 	disk_bytenr += 2 * sectorsize;
185aaedb55bSJosef Bacik 
186aaedb55bSJosef Bacik 	/* Now a split compressed extent */
187b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
188b9ef22deSFeifei Xu 		      sectorsize, BTRFS_FILE_EXTENT_REG,
189b9ef22deSFeifei Xu 		      BTRFS_COMPRESS_ZLIB, slot);
190aaedb55bSJosef Bacik 	slot++;
191b9ef22deSFeifei Xu 	offset += sectorsize;
192b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0,
193b9ef22deSFeifei Xu 		      disk_bytenr + sectorsize, sectorsize,
194aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
195aaedb55bSJosef Bacik 	slot++;
196b9ef22deSFeifei Xu 	offset += sectorsize;
197b9ef22deSFeifei Xu 	insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
198b9ef22deSFeifei Xu 		      2 * sectorsize, disk_bytenr, sectorsize,
199aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
200aaedb55bSJosef Bacik 	slot++;
201b9ef22deSFeifei Xu 	offset += 2 * sectorsize;
202b9ef22deSFeifei Xu 	disk_bytenr += 2 * sectorsize;
203aaedb55bSJosef Bacik 
204aaedb55bSJosef Bacik 	/* Now extents that have a hole but no hole extent */
205b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
206b9ef22deSFeifei Xu 		      sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
207aaedb55bSJosef Bacik 	slot++;
208b9ef22deSFeifei Xu 	offset += 4 * sectorsize;
209b9ef22deSFeifei Xu 	disk_bytenr += sectorsize;
210b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
211b9ef22deSFeifei Xu 		      sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
212aaedb55bSJosef Bacik }
213aaedb55bSJosef Bacik 
214aaedb55bSJosef Bacik static unsigned long prealloc_only = 0;
215aaedb55bSJosef Bacik static unsigned long compressed_only = 0;
216aaedb55bSJosef Bacik static unsigned long vacancy_only = 0;
217aaedb55bSJosef Bacik 
test_btrfs_get_extent(u32 sectorsize,u32 nodesize)218b9ef22deSFeifei Xu static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize)
219aaedb55bSJosef Bacik {
2207c0260eeSJeff Mahoney 	struct btrfs_fs_info *fs_info = NULL;
221aaedb55bSJosef Bacik 	struct inode *inode = NULL;
222aaedb55bSJosef Bacik 	struct btrfs_root *root = NULL;
223aaedb55bSJosef Bacik 	struct extent_map *em = NULL;
224aaedb55bSJosef Bacik 	u64 orig_start;
225aaedb55bSJosef Bacik 	u64 disk_bytenr;
226aaedb55bSJosef Bacik 	u64 offset;
227aaedb55bSJosef Bacik 	int ret = -ENOMEM;
228aaedb55bSJosef Bacik 
229e4fa7469SDavid Sterba 	test_msg("running btrfs_get_extent tests");
230e4fa7469SDavid Sterba 
231aaedb55bSJosef Bacik 	inode = btrfs_new_test_inode();
232aaedb55bSJosef Bacik 	if (!inode) {
2336a060db8SDavid Sterba 		test_std_err(TEST_ALLOC_INODE);
234aaedb55bSJosef Bacik 		return ret;
235aaedb55bSJosef Bacik 	}
236aaedb55bSJosef Bacik 
237da17066cSJeff Mahoney 	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
2387c0260eeSJeff Mahoney 	if (!fs_info) {
23937b2a7bcSDavid Sterba 		test_std_err(TEST_ALLOC_FS_INFO);
240aaedb55bSJosef Bacik 		goto out;
241aaedb55bSJosef Bacik 	}
242aaedb55bSJosef Bacik 
243da17066cSJeff Mahoney 	root = btrfs_alloc_dummy_root(fs_info);
2447c0260eeSJeff Mahoney 	if (IS_ERR(root)) {
24552ab7bcaSDavid Sterba 		test_std_err(TEST_ALLOC_ROOT);
246aaedb55bSJosef Bacik 		goto out;
247aaedb55bSJosef Bacik 	}
248aaedb55bSJosef Bacik 
249da17066cSJeff Mahoney 	root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
250aaedb55bSJosef Bacik 	if (!root->node) {
2519e3d9f84SDavid Sterba 		test_std_err(TEST_ALLOC_ROOT);
252aaedb55bSJosef Bacik 		goto out;
253aaedb55bSJosef Bacik 	}
254aaedb55bSJosef Bacik 
255aaedb55bSJosef Bacik 	btrfs_set_header_nritems(root->node, 0);
256aaedb55bSJosef Bacik 	btrfs_set_header_level(root->node, 0);
257aaedb55bSJosef Bacik 	ret = -EINVAL;
258aaedb55bSJosef Bacik 
259aaedb55bSJosef Bacik 	/* First with no extents */
260aaedb55bSJosef Bacik 	BTRFS_I(inode)->root = root;
26139b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, sectorsize);
262aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
263aaedb55bSJosef Bacik 		em = NULL;
2643c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
265aaedb55bSJosef Bacik 		goto out;
266aaedb55bSJosef Bacik 	}
267aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
2683c7251f2SDavid Sterba 		test_err("expected a hole, got %llu", em->block_start);
269aaedb55bSJosef Bacik 		goto out;
270aaedb55bSJosef Bacik 	}
271aaedb55bSJosef Bacik 	free_extent_map(em);
2724c0c8cfcSFilipe Manana 	btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false);
273aaedb55bSJosef Bacik 
274aaedb55bSJosef Bacik 	/*
275aaedb55bSJosef Bacik 	 * All of the magic numbers are based on the mapping setup in
276aaedb55bSJosef Bacik 	 * setup_file_extents, so if you change anything there you need to
277aaedb55bSJosef Bacik 	 * update the comment and update the expected values below.
278aaedb55bSJosef Bacik 	 */
279b9ef22deSFeifei Xu 	setup_file_extents(root, sectorsize);
280aaedb55bSJosef Bacik 
28139b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, (u64)-1);
282aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
2833c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
284aaedb55bSJosef Bacik 		goto out;
285aaedb55bSJosef Bacik 	}
286aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_INLINE) {
2873c7251f2SDavid Sterba 		test_err("expected an inline, got %llu", em->block_start);
288aaedb55bSJosef Bacik 		goto out;
289aaedb55bSJosef Bacik 	}
290b9ef22deSFeifei Xu 
291d52a1365SQu Wenruo 	/*
292d52a1365SQu Wenruo 	 * For inline extent, we always round up the em to sectorsize, as
293d52a1365SQu Wenruo 	 * they are either:
294d52a1365SQu Wenruo 	 *
295d52a1365SQu Wenruo 	 * a) a hidden hole
296d52a1365SQu Wenruo 	 *    The range will be zeroed at inline extent read time.
297d52a1365SQu Wenruo 	 *
298d52a1365SQu Wenruo 	 * b) a file extent with unaligned bytenr
299d52a1365SQu Wenruo 	 *    Tree checker will reject it.
300d52a1365SQu Wenruo 	 */
301d52a1365SQu Wenruo 	if (em->start != 0 || em->len != sectorsize) {
3023c7251f2SDavid Sterba 		test_err(
303d52a1365SQu Wenruo 	"unexpected extent wanted start 0 len %u, got start %llu len %llu",
304d52a1365SQu Wenruo 			sectorsize, em->start, em->len);
305aaedb55bSJosef Bacik 		goto out;
306aaedb55bSJosef Bacik 	}
307aaedb55bSJosef Bacik 	if (em->flags != 0) {
3083c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
309aaedb55bSJosef Bacik 		goto out;
310aaedb55bSJosef Bacik 	}
311aaedb55bSJosef Bacik 	/*
312aaedb55bSJosef Bacik 	 * We don't test anything else for inline since it doesn't get set
313aaedb55bSJosef Bacik 	 * unless we have a page for it to write into.  Maybe we should change
314aaedb55bSJosef Bacik 	 * this?
315aaedb55bSJosef Bacik 	 */
316aaedb55bSJosef Bacik 	offset = em->start + em->len;
317aaedb55bSJosef Bacik 	free_extent_map(em);
318aaedb55bSJosef Bacik 
31939b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
320aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
3213c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
322aaedb55bSJosef Bacik 		goto out;
323aaedb55bSJosef Bacik 	}
324aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
3253c7251f2SDavid Sterba 		test_err("expected a hole, got %llu", em->block_start);
326aaedb55bSJosef Bacik 		goto out;
327aaedb55bSJosef Bacik 	}
328aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 4) {
3293c7251f2SDavid Sterba 		test_err(
3303c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len 4, got start %llu len %llu",
3313c7251f2SDavid Sterba 			offset, em->start, em->len);
332aaedb55bSJosef Bacik 		goto out;
333aaedb55bSJosef Bacik 	}
334aaedb55bSJosef Bacik 	if (em->flags != 0) {
3353c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
336aaedb55bSJosef Bacik 		goto out;
337aaedb55bSJosef Bacik 	}
338aaedb55bSJosef Bacik 	offset = em->start + em->len;
339aaedb55bSJosef Bacik 	free_extent_map(em);
340aaedb55bSJosef Bacik 
341aaedb55bSJosef Bacik 	/* Regular extent */
34239b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
343aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
3443c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
345aaedb55bSJosef Bacik 		goto out;
346aaedb55bSJosef Bacik 	}
347aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
3483c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
349aaedb55bSJosef Bacik 		goto out;
350aaedb55bSJosef Bacik 	}
351b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize - 1) {
3523c7251f2SDavid Sterba 		test_err(
3533c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len 4095, got start %llu len %llu",
3543c7251f2SDavid Sterba 			offset, em->start, em->len);
355aaedb55bSJosef Bacik 		goto out;
356aaedb55bSJosef Bacik 	}
357aaedb55bSJosef Bacik 	if (em->flags != 0) {
3583c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
359aaedb55bSJosef Bacik 		goto out;
360aaedb55bSJosef Bacik 	}
361aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
3623c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
363aaedb55bSJosef Bacik 			 em->orig_start);
364aaedb55bSJosef Bacik 		goto out;
365aaedb55bSJosef Bacik 	}
366aaedb55bSJosef Bacik 	offset = em->start + em->len;
367aaedb55bSJosef Bacik 	free_extent_map(em);
368aaedb55bSJosef Bacik 
369aaedb55bSJosef Bacik 	/* The next 3 are split extents */
37039b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
371aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
3723c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
373aaedb55bSJosef Bacik 		goto out;
374aaedb55bSJosef Bacik 	}
375aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
3763c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
377aaedb55bSJosef Bacik 		goto out;
378aaedb55bSJosef Bacik 	}
379b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
3803c7251f2SDavid Sterba 		test_err(
3813c7251f2SDavid Sterba 		"unexpected extent start %llu len %u, got start %llu len %llu",
382b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
383aaedb55bSJosef Bacik 		goto out;
384aaedb55bSJosef Bacik 	}
385aaedb55bSJosef Bacik 	if (em->flags != 0) {
3863c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
387aaedb55bSJosef Bacik 		goto out;
388aaedb55bSJosef Bacik 	}
389aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
3903c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
391aaedb55bSJosef Bacik 			 em->orig_start);
392aaedb55bSJosef Bacik 		goto out;
393aaedb55bSJosef Bacik 	}
394aaedb55bSJosef Bacik 	disk_bytenr = em->block_start;
395aaedb55bSJosef Bacik 	orig_start = em->start;
396aaedb55bSJosef Bacik 	offset = em->start + em->len;
397aaedb55bSJosef Bacik 	free_extent_map(em);
398aaedb55bSJosef Bacik 
39939b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
400aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
4013c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
402aaedb55bSJosef Bacik 		goto out;
403aaedb55bSJosef Bacik 	}
404aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
4053c7251f2SDavid Sterba 		test_err("expected a hole, got %llu", em->block_start);
406aaedb55bSJosef Bacik 		goto out;
407aaedb55bSJosef Bacik 	}
408b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
4093c7251f2SDavid Sterba 		test_err(
4103c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
411b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
412aaedb55bSJosef Bacik 		goto out;
413aaedb55bSJosef Bacik 	}
414aaedb55bSJosef Bacik 	if (em->flags != 0) {
4153c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
416aaedb55bSJosef Bacik 		goto out;
417aaedb55bSJosef Bacik 	}
418aaedb55bSJosef Bacik 	offset = em->start + em->len;
419aaedb55bSJosef Bacik 	free_extent_map(em);
420aaedb55bSJosef Bacik 
42139b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
422aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
4233c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
424aaedb55bSJosef Bacik 		goto out;
425aaedb55bSJosef Bacik 	}
426aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
4273c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
428aaedb55bSJosef Bacik 		goto out;
429aaedb55bSJosef Bacik 	}
430b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 2 * sectorsize) {
4313c7251f2SDavid Sterba 		test_err(
4323c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
433b9ef22deSFeifei Xu 			offset, 2 * sectorsize, em->start, em->len);
434aaedb55bSJosef Bacik 		goto out;
435aaedb55bSJosef Bacik 	}
436aaedb55bSJosef Bacik 	if (em->flags != 0) {
4373c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
438aaedb55bSJosef Bacik 		goto out;
439aaedb55bSJosef Bacik 	}
440aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
4413c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu",
442aaedb55bSJosef Bacik 			 orig_start, em->orig_start);
443aaedb55bSJosef Bacik 		goto out;
444aaedb55bSJosef Bacik 	}
445aaedb55bSJosef Bacik 	disk_bytenr += (em->start - orig_start);
446aaedb55bSJosef Bacik 	if (em->block_start != disk_bytenr) {
4473c7251f2SDavid Sterba 		test_err("wrong block start, want %llu, have %llu",
448aaedb55bSJosef Bacik 			 disk_bytenr, em->block_start);
449aaedb55bSJosef Bacik 		goto out;
450aaedb55bSJosef Bacik 	}
451aaedb55bSJosef Bacik 	offset = em->start + em->len;
452aaedb55bSJosef Bacik 	free_extent_map(em);
453aaedb55bSJosef Bacik 
454aaedb55bSJosef Bacik 	/* Prealloc extent */
45539b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
456aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
4573c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
458aaedb55bSJosef Bacik 		goto out;
459aaedb55bSJosef Bacik 	}
460aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
4613c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
462aaedb55bSJosef Bacik 		goto out;
463aaedb55bSJosef Bacik 	}
464b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
4653c7251f2SDavid Sterba 		test_err(
4663c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
467b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
468aaedb55bSJosef Bacik 		goto out;
469aaedb55bSJosef Bacik 	}
470aaedb55bSJosef Bacik 	if (em->flags != prealloc_only) {
4713c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
472aaedb55bSJosef Bacik 			 prealloc_only, em->flags);
473aaedb55bSJosef Bacik 		goto out;
474aaedb55bSJosef Bacik 	}
475aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
4763c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
477aaedb55bSJosef Bacik 			 em->orig_start);
478aaedb55bSJosef Bacik 		goto out;
479aaedb55bSJosef Bacik 	}
480aaedb55bSJosef Bacik 	offset = em->start + em->len;
481aaedb55bSJosef Bacik 	free_extent_map(em);
482aaedb55bSJosef Bacik 
483aaedb55bSJosef Bacik 	/* The next 3 are a half written prealloc extent */
48439b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
485aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
4863c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
487aaedb55bSJosef Bacik 		goto out;
488aaedb55bSJosef Bacik 	}
489aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
4903c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
491aaedb55bSJosef Bacik 		goto out;
492aaedb55bSJosef Bacik 	}
493b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
4943c7251f2SDavid Sterba 		test_err(
4953c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
496b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
497aaedb55bSJosef Bacik 		goto out;
498aaedb55bSJosef Bacik 	}
499aaedb55bSJosef Bacik 	if (em->flags != prealloc_only) {
5003c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
501aaedb55bSJosef Bacik 			 prealloc_only, em->flags);
502aaedb55bSJosef Bacik 		goto out;
503aaedb55bSJosef Bacik 	}
504aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
5053c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
506aaedb55bSJosef Bacik 			 em->orig_start);
507aaedb55bSJosef Bacik 		goto out;
508aaedb55bSJosef Bacik 	}
509aaedb55bSJosef Bacik 	disk_bytenr = em->block_start;
510aaedb55bSJosef Bacik 	orig_start = em->start;
511aaedb55bSJosef Bacik 	offset = em->start + em->len;
512aaedb55bSJosef Bacik 	free_extent_map(em);
513aaedb55bSJosef Bacik 
51439b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
515aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
5163c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
517aaedb55bSJosef Bacik 		goto out;
518aaedb55bSJosef Bacik 	}
519aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_HOLE) {
5203c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
521aaedb55bSJosef Bacik 		goto out;
522aaedb55bSJosef Bacik 	}
523b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
5243c7251f2SDavid Sterba 		test_err(
5253c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
526b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
527aaedb55bSJosef Bacik 		goto out;
528aaedb55bSJosef Bacik 	}
529aaedb55bSJosef Bacik 	if (em->flags != 0) {
5303c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
531aaedb55bSJosef Bacik 		goto out;
532aaedb55bSJosef Bacik 	}
533aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
5343c7251f2SDavid Sterba 		test_err("unexpected orig offset, wanted %llu, have %llu",
535aaedb55bSJosef Bacik 			 orig_start, em->orig_start);
536aaedb55bSJosef Bacik 		goto out;
537aaedb55bSJosef Bacik 	}
538aaedb55bSJosef Bacik 	if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
5393c7251f2SDavid Sterba 		test_err("unexpected block start, wanted %llu, have %llu",
540aaedb55bSJosef Bacik 			 disk_bytenr + (em->start - em->orig_start),
541aaedb55bSJosef Bacik 			 em->block_start);
542aaedb55bSJosef Bacik 		goto out;
543aaedb55bSJosef Bacik 	}
544aaedb55bSJosef Bacik 	offset = em->start + em->len;
545aaedb55bSJosef Bacik 	free_extent_map(em);
546aaedb55bSJosef Bacik 
54739b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
548aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
5493c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
550aaedb55bSJosef Bacik 		goto out;
551aaedb55bSJosef Bacik 	}
552aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
5533c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
554aaedb55bSJosef Bacik 		goto out;
555aaedb55bSJosef Bacik 	}
556b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 2 * sectorsize) {
5573c7251f2SDavid Sterba 		test_err(
5583c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
559b9ef22deSFeifei Xu 			offset, 2 * sectorsize, em->start, em->len);
560aaedb55bSJosef Bacik 		goto out;
561aaedb55bSJosef Bacik 	}
562aaedb55bSJosef Bacik 	if (em->flags != prealloc_only) {
5633c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
564aaedb55bSJosef Bacik 			 prealloc_only, em->flags);
565aaedb55bSJosef Bacik 		goto out;
566aaedb55bSJosef Bacik 	}
567aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
5683c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", orig_start,
569aaedb55bSJosef Bacik 			 em->orig_start);
570aaedb55bSJosef Bacik 		goto out;
571aaedb55bSJosef Bacik 	}
572aaedb55bSJosef Bacik 	if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
5733c7251f2SDavid Sterba 		test_err("unexpected block start, wanted %llu, have %llu",
574aaedb55bSJosef Bacik 			 disk_bytenr + (em->start - em->orig_start),
575aaedb55bSJosef Bacik 			 em->block_start);
576aaedb55bSJosef Bacik 		goto out;
577aaedb55bSJosef Bacik 	}
578aaedb55bSJosef Bacik 	offset = em->start + em->len;
579aaedb55bSJosef Bacik 	free_extent_map(em);
580aaedb55bSJosef Bacik 
581aaedb55bSJosef Bacik 	/* Now for the compressed extent */
58239b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
583aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
5843c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
585aaedb55bSJosef Bacik 		goto out;
586aaedb55bSJosef Bacik 	}
587aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
5883c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
589aaedb55bSJosef Bacik 		goto out;
590aaedb55bSJosef Bacik 	}
591b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 2 * sectorsize) {
5923c7251f2SDavid Sterba 		test_err(
5933c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
594b9ef22deSFeifei Xu 			offset, 2 * sectorsize, em->start, em->len);
595aaedb55bSJosef Bacik 		goto out;
596aaedb55bSJosef Bacik 	}
597aaedb55bSJosef Bacik 	if (em->flags != compressed_only) {
5983c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
599aaedb55bSJosef Bacik 			 compressed_only, em->flags);
600aaedb55bSJosef Bacik 		goto out;
601aaedb55bSJosef Bacik 	}
602aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
6033c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu",
604aaedb55bSJosef Bacik 			 em->start, em->orig_start);
605aaedb55bSJosef Bacik 		goto out;
606aaedb55bSJosef Bacik 	}
607aaedb55bSJosef Bacik 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
6083c7251f2SDavid Sterba 		test_err("unexpected compress type, wanted %d, got %d",
609aaedb55bSJosef Bacik 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
610aaedb55bSJosef Bacik 		goto out;
611aaedb55bSJosef Bacik 	}
612aaedb55bSJosef Bacik 	offset = em->start + em->len;
613aaedb55bSJosef Bacik 	free_extent_map(em);
614aaedb55bSJosef Bacik 
615aaedb55bSJosef Bacik 	/* Split compressed extent */
61639b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
617aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
6183c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
619aaedb55bSJosef Bacik 		goto out;
620aaedb55bSJosef Bacik 	}
621aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
6223c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
623aaedb55bSJosef Bacik 		goto out;
624aaedb55bSJosef Bacik 	}
625b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
6263c7251f2SDavid Sterba 		test_err(
6273c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
628b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
629aaedb55bSJosef Bacik 		goto out;
630aaedb55bSJosef Bacik 	}
631aaedb55bSJosef Bacik 	if (em->flags != compressed_only) {
6323c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
633aaedb55bSJosef Bacik 			 compressed_only, em->flags);
634aaedb55bSJosef Bacik 		goto out;
635aaedb55bSJosef Bacik 	}
636aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
6373c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu",
638aaedb55bSJosef Bacik 			 em->start, em->orig_start);
639aaedb55bSJosef Bacik 		goto out;
640aaedb55bSJosef Bacik 	}
641aaedb55bSJosef Bacik 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
6423c7251f2SDavid Sterba 		test_err("unexpected compress type, wanted %d, got %d",
643aaedb55bSJosef Bacik 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
644aaedb55bSJosef Bacik 		goto out;
645aaedb55bSJosef Bacik 	}
646aaedb55bSJosef Bacik 	disk_bytenr = em->block_start;
647aaedb55bSJosef Bacik 	orig_start = em->start;
648aaedb55bSJosef Bacik 	offset = em->start + em->len;
649aaedb55bSJosef Bacik 	free_extent_map(em);
650aaedb55bSJosef Bacik 
65139b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
652aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
6533c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
654aaedb55bSJosef Bacik 		goto out;
655aaedb55bSJosef Bacik 	}
656aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
6573c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
658aaedb55bSJosef Bacik 		goto out;
659aaedb55bSJosef Bacik 	}
660b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
6613c7251f2SDavid Sterba 		test_err(
6623c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
663b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
664aaedb55bSJosef Bacik 		goto out;
665aaedb55bSJosef Bacik 	}
666aaedb55bSJosef Bacik 	if (em->flags != 0) {
6673c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
668aaedb55bSJosef Bacik 		goto out;
669aaedb55bSJosef Bacik 	}
670aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
6713c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
672aaedb55bSJosef Bacik 			 em->orig_start);
673aaedb55bSJosef Bacik 		goto out;
674aaedb55bSJosef Bacik 	}
675aaedb55bSJosef Bacik 	offset = em->start + em->len;
676aaedb55bSJosef Bacik 	free_extent_map(em);
677aaedb55bSJosef Bacik 
67839b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
679aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
6803c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
681aaedb55bSJosef Bacik 		goto out;
682aaedb55bSJosef Bacik 	}
683aaedb55bSJosef Bacik 	if (em->block_start != disk_bytenr) {
6843c7251f2SDavid Sterba 		test_err("block start does not match, want %llu got %llu",
685aaedb55bSJosef Bacik 			 disk_bytenr, em->block_start);
686aaedb55bSJosef Bacik 		goto out;
687aaedb55bSJosef Bacik 	}
688b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 2 * sectorsize) {
6893c7251f2SDavid Sterba 		test_err(
6903c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
691b9ef22deSFeifei Xu 			offset, 2 * sectorsize, em->start, em->len);
692aaedb55bSJosef Bacik 		goto out;
693aaedb55bSJosef Bacik 	}
694aaedb55bSJosef Bacik 	if (em->flags != compressed_only) {
6953c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
696aaedb55bSJosef Bacik 			 compressed_only, em->flags);
697aaedb55bSJosef Bacik 		goto out;
698aaedb55bSJosef Bacik 	}
699aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
7003c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu",
701aaedb55bSJosef Bacik 			 em->start, orig_start);
702aaedb55bSJosef Bacik 		goto out;
703aaedb55bSJosef Bacik 	}
704aaedb55bSJosef Bacik 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
7053c7251f2SDavid Sterba 		test_err("unexpected compress type, wanted %d, got %d",
706aaedb55bSJosef Bacik 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
707aaedb55bSJosef Bacik 		goto out;
708aaedb55bSJosef Bacik 	}
709aaedb55bSJosef Bacik 	offset = em->start + em->len;
710aaedb55bSJosef Bacik 	free_extent_map(em);
711aaedb55bSJosef Bacik 
712aaedb55bSJosef Bacik 	/* A hole between regular extents but no hole extent */
71339b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset + 6, sectorsize);
714aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
7153c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
716aaedb55bSJosef Bacik 		goto out;
717aaedb55bSJosef Bacik 	}
718aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
7193c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
720aaedb55bSJosef Bacik 		goto out;
721aaedb55bSJosef Bacik 	}
722b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
7233c7251f2SDavid Sterba 		test_err(
7243c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
725b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
726aaedb55bSJosef Bacik 		goto out;
727aaedb55bSJosef Bacik 	}
728aaedb55bSJosef Bacik 	if (em->flags != 0) {
7293c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
730aaedb55bSJosef Bacik 		goto out;
731aaedb55bSJosef Bacik 	}
732aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
7333c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
734aaedb55bSJosef Bacik 			 em->orig_start);
735aaedb55bSJosef Bacik 		goto out;
736aaedb55bSJosef Bacik 	}
737aaedb55bSJosef Bacik 	offset = em->start + em->len;
738aaedb55bSJosef Bacik 	free_extent_map(em);
739aaedb55bSJosef Bacik 
74039b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, SZ_4M);
741aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
7423c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
743aaedb55bSJosef Bacik 		goto out;
744aaedb55bSJosef Bacik 	}
745aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
7463c7251f2SDavid Sterba 		test_err("expected a hole extent, got %llu", em->block_start);
747aaedb55bSJosef Bacik 		goto out;
748aaedb55bSJosef Bacik 	}
749aaedb55bSJosef Bacik 	/*
750aaedb55bSJosef Bacik 	 * Currently we just return a length that we requested rather than the
751aaedb55bSJosef Bacik 	 * length of the actual hole, if this changes we'll have to change this
752aaedb55bSJosef Bacik 	 * test.
753aaedb55bSJosef Bacik 	 */
754b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 3 * sectorsize) {
7553c7251f2SDavid Sterba 		test_err(
7563c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
757b9ef22deSFeifei Xu 			offset, 3 * sectorsize, em->start, em->len);
758aaedb55bSJosef Bacik 		goto out;
759aaedb55bSJosef Bacik 	}
760aaedb55bSJosef Bacik 	if (em->flags != vacancy_only) {
7613c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
762aaedb55bSJosef Bacik 			 vacancy_only, em->flags);
763aaedb55bSJosef Bacik 		goto out;
764aaedb55bSJosef Bacik 	}
765aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
7663c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
767aaedb55bSJosef Bacik 			 em->orig_start);
768aaedb55bSJosef Bacik 		goto out;
769aaedb55bSJosef Bacik 	}
770aaedb55bSJosef Bacik 	offset = em->start + em->len;
771aaedb55bSJosef Bacik 	free_extent_map(em);
772aaedb55bSJosef Bacik 
77339b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
774aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
7753c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
776aaedb55bSJosef Bacik 		goto out;
777aaedb55bSJosef Bacik 	}
778aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
7793c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
780aaedb55bSJosef Bacik 		goto out;
781aaedb55bSJosef Bacik 	}
782b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
7833c7251f2SDavid Sterba 		test_err(
7843c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
785b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
786aaedb55bSJosef Bacik 		goto out;
787aaedb55bSJosef Bacik 	}
788aaedb55bSJosef Bacik 	if (em->flags != 0) {
7893c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
790aaedb55bSJosef Bacik 		goto out;
791aaedb55bSJosef Bacik 	}
792aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
7933c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
794aaedb55bSJosef Bacik 			 em->orig_start);
795aaedb55bSJosef Bacik 		goto out;
796aaedb55bSJosef Bacik 	}
797aaedb55bSJosef Bacik 	ret = 0;
798aaedb55bSJosef Bacik out:
799aaedb55bSJosef Bacik 	if (!IS_ERR(em))
800aaedb55bSJosef Bacik 		free_extent_map(em);
801aaedb55bSJosef Bacik 	iput(inode);
802faa2dbf0SJosef Bacik 	btrfs_free_dummy_root(root);
8037c0260eeSJeff Mahoney 	btrfs_free_dummy_fs_info(fs_info);
804aaedb55bSJosef Bacik 	return ret;
805aaedb55bSJosef Bacik }
806aaedb55bSJosef Bacik 
test_hole_first(u32 sectorsize,u32 nodesize)807b9ef22deSFeifei Xu static int test_hole_first(u32 sectorsize, u32 nodesize)
8080e30db86SJosef Bacik {
8097c0260eeSJeff Mahoney 	struct btrfs_fs_info *fs_info = NULL;
8100e30db86SJosef Bacik 	struct inode *inode = NULL;
8110e30db86SJosef Bacik 	struct btrfs_root *root = NULL;
8120e30db86SJosef Bacik 	struct extent_map *em = NULL;
8130e30db86SJosef Bacik 	int ret = -ENOMEM;
8140e30db86SJosef Bacik 
815e4fa7469SDavid Sterba 	test_msg("running hole first btrfs_get_extent test");
816e4fa7469SDavid Sterba 
8170e30db86SJosef Bacik 	inode = btrfs_new_test_inode();
8180e30db86SJosef Bacik 	if (!inode) {
8196a060db8SDavid Sterba 		test_std_err(TEST_ALLOC_INODE);
8200e30db86SJosef Bacik 		return ret;
8210e30db86SJosef Bacik 	}
8220e30db86SJosef Bacik 
823da17066cSJeff Mahoney 	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
8247c0260eeSJeff Mahoney 	if (!fs_info) {
82537b2a7bcSDavid Sterba 		test_std_err(TEST_ALLOC_FS_INFO);
8260e30db86SJosef Bacik 		goto out;
8270e30db86SJosef Bacik 	}
8280e30db86SJosef Bacik 
829da17066cSJeff Mahoney 	root = btrfs_alloc_dummy_root(fs_info);
8307c0260eeSJeff Mahoney 	if (IS_ERR(root)) {
83152ab7bcaSDavid Sterba 		test_std_err(TEST_ALLOC_ROOT);
8320e30db86SJosef Bacik 		goto out;
8330e30db86SJosef Bacik 	}
8340e30db86SJosef Bacik 
835da17066cSJeff Mahoney 	root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
8360e30db86SJosef Bacik 	if (!root->node) {
8379e3d9f84SDavid Sterba 		test_std_err(TEST_ALLOC_ROOT);
8380e30db86SJosef Bacik 		goto out;
8390e30db86SJosef Bacik 	}
8400e30db86SJosef Bacik 
8410e30db86SJosef Bacik 	btrfs_set_header_nritems(root->node, 0);
8420e30db86SJosef Bacik 	btrfs_set_header_level(root->node, 0);
8430e30db86SJosef Bacik 	BTRFS_I(inode)->root = root;
8440e30db86SJosef Bacik 	ret = -EINVAL;
8450e30db86SJosef Bacik 
8460e30db86SJosef Bacik 	/*
8470e30db86SJosef Bacik 	 * Need a blank inode item here just so we don't confuse
8480e30db86SJosef Bacik 	 * btrfs_get_extent.
8490e30db86SJosef Bacik 	 */
8500e30db86SJosef Bacik 	insert_inode_item_key(root);
851b9ef22deSFeifei Xu 	insert_extent(root, sectorsize, sectorsize, sectorsize, 0, sectorsize,
852b9ef22deSFeifei Xu 		      sectorsize, BTRFS_FILE_EXTENT_REG, 0, 1);
85339b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, 2 * sectorsize);
8540e30db86SJosef Bacik 	if (IS_ERR(em)) {
8553c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
8560e30db86SJosef Bacik 		goto out;
8570e30db86SJosef Bacik 	}
8580e30db86SJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
8593c7251f2SDavid Sterba 		test_err("expected a hole, got %llu", em->block_start);
8600e30db86SJosef Bacik 		goto out;
8610e30db86SJosef Bacik 	}
862b9ef22deSFeifei Xu 	if (em->start != 0 || em->len != sectorsize) {
8633c7251f2SDavid Sterba 		test_err(
8643c7251f2SDavid Sterba 	"unexpected extent wanted start 0 len %u, got start %llu len %llu",
865b9ef22deSFeifei Xu 			sectorsize, em->start, em->len);
8660e30db86SJosef Bacik 		goto out;
8670e30db86SJosef Bacik 	}
8680e30db86SJosef Bacik 	if (em->flags != vacancy_only) {
8693c7251f2SDavid Sterba 		test_err("wrong flags, wanted %lu, have %lu", vacancy_only,
8700e30db86SJosef Bacik 			 em->flags);
8710e30db86SJosef Bacik 		goto out;
8720e30db86SJosef Bacik 	}
8730e30db86SJosef Bacik 	free_extent_map(em);
8740e30db86SJosef Bacik 
87539b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, sectorsize, 2 * sectorsize);
8760e30db86SJosef Bacik 	if (IS_ERR(em)) {
8773c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
8780e30db86SJosef Bacik 		goto out;
8790e30db86SJosef Bacik 	}
880b9ef22deSFeifei Xu 	if (em->block_start != sectorsize) {
8813c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
8820e30db86SJosef Bacik 		goto out;
8830e30db86SJosef Bacik 	}
884b9ef22deSFeifei Xu 	if (em->start != sectorsize || em->len != sectorsize) {
8853c7251f2SDavid Sterba 		test_err(
8863c7251f2SDavid Sterba 	"unexpected extent wanted start %u len %u, got start %llu len %llu",
887b9ef22deSFeifei Xu 			sectorsize, sectorsize, em->start, em->len);
8880e30db86SJosef Bacik 		goto out;
8890e30db86SJosef Bacik 	}
8900e30db86SJosef Bacik 	if (em->flags != 0) {
8913c7251f2SDavid Sterba 		test_err("unexpected flags set, wanted 0 got %lu",
8920e30db86SJosef Bacik 			 em->flags);
8930e30db86SJosef Bacik 		goto out;
8940e30db86SJosef Bacik 	}
8950e30db86SJosef Bacik 	ret = 0;
8960e30db86SJosef Bacik out:
8970e30db86SJosef Bacik 	if (!IS_ERR(em))
8980e30db86SJosef Bacik 		free_extent_map(em);
8990e30db86SJosef Bacik 	iput(inode);
900faa2dbf0SJosef Bacik 	btrfs_free_dummy_root(root);
9017c0260eeSJeff Mahoney 	btrfs_free_dummy_fs_info(fs_info);
9020e30db86SJosef Bacik 	return ret;
9030e30db86SJosef Bacik }
9040e30db86SJosef Bacik 
test_extent_accounting(u32 sectorsize,u32 nodesize)905b9ef22deSFeifei Xu static int test_extent_accounting(u32 sectorsize, u32 nodesize)
9066a3891c5SJosef Bacik {
9077c0260eeSJeff Mahoney 	struct btrfs_fs_info *fs_info = NULL;
9086a3891c5SJosef Bacik 	struct inode *inode = NULL;
9096a3891c5SJosef Bacik 	struct btrfs_root *root = NULL;
9106a3891c5SJosef Bacik 	int ret = -ENOMEM;
9116a3891c5SJosef Bacik 
912e4fa7469SDavid Sterba 	test_msg("running outstanding_extents tests");
913e4fa7469SDavid Sterba 
9146a3891c5SJosef Bacik 	inode = btrfs_new_test_inode();
9156a3891c5SJosef Bacik 	if (!inode) {
9166a060db8SDavid Sterba 		test_std_err(TEST_ALLOC_INODE);
9176a3891c5SJosef Bacik 		return ret;
9186a3891c5SJosef Bacik 	}
9196a3891c5SJosef Bacik 
920da17066cSJeff Mahoney 	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
9217c0260eeSJeff Mahoney 	if (!fs_info) {
92237b2a7bcSDavid Sterba 		test_std_err(TEST_ALLOC_FS_INFO);
9236a3891c5SJosef Bacik 		goto out;
9246a3891c5SJosef Bacik 	}
9256a3891c5SJosef Bacik 
926da17066cSJeff Mahoney 	root = btrfs_alloc_dummy_root(fs_info);
9277c0260eeSJeff Mahoney 	if (IS_ERR(root)) {
92852ab7bcaSDavid Sterba 		test_std_err(TEST_ALLOC_ROOT);
9296a3891c5SJosef Bacik 		goto out;
9306a3891c5SJosef Bacik 	}
9316a3891c5SJosef Bacik 
9326a3891c5SJosef Bacik 	BTRFS_I(inode)->root = root;
9336a3891c5SJosef Bacik 
9346a3891c5SJosef Bacik 	/* [BTRFS_MAX_EXTENT_SIZE] */
935c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode), 0,
936c2566f22SNikolay Borisov 					BTRFS_MAX_EXTENT_SIZE - 1, 0, NULL);
9376a3891c5SJosef Bacik 	if (ret) {
9383c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
9396a3891c5SJosef Bacik 		goto out;
9406a3891c5SJosef Bacik 	}
9416a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 1) {
9426a3891c5SJosef Bacik 		ret = -EINVAL;
9433c7251f2SDavid Sterba 		test_err("miscount, wanted 1, got %u",
9446a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
9456a3891c5SJosef Bacik 		goto out;
9466a3891c5SJosef Bacik 	}
9476a3891c5SJosef Bacik 
948b9ef22deSFeifei Xu 	/* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
949c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode), BTRFS_MAX_EXTENT_SIZE,
950b9ef22deSFeifei Xu 					BTRFS_MAX_EXTENT_SIZE + sectorsize - 1,
951330a5827SNikolay Borisov 					0, NULL);
9526a3891c5SJosef Bacik 	if (ret) {
9533c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
9546a3891c5SJosef Bacik 		goto out;
9556a3891c5SJosef Bacik 	}
9566a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 2) {
9576a3891c5SJosef Bacik 		ret = -EINVAL;
9583c7251f2SDavid Sterba 		test_err("miscount, wanted 2, got %u",
9596a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
9606a3891c5SJosef Bacik 		goto out;
9616a3891c5SJosef Bacik 	}
9626a3891c5SJosef Bacik 
963b9ef22deSFeifei Xu 	/* [BTRFS_MAX_EXTENT_SIZE/2][sectorsize HOLE][the rest] */
9646a3891c5SJosef Bacik 	ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
9656a3891c5SJosef Bacik 			       BTRFS_MAX_EXTENT_SIZE >> 1,
966b9ef22deSFeifei Xu 			       (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1,
967c3347309SFilipe Manana 			       EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
968bd015294SJosef Bacik 			       EXTENT_UPTODATE, NULL);
9696a3891c5SJosef Bacik 	if (ret) {
9703c7251f2SDavid Sterba 		test_err("clear_extent_bit returned %d", ret);
9716a3891c5SJosef Bacik 		goto out;
9726a3891c5SJosef Bacik 	}
9736a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 2) {
9746a3891c5SJosef Bacik 		ret = -EINVAL;
9753c7251f2SDavid Sterba 		test_err("miscount, wanted 2, got %u",
9766a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
9776a3891c5SJosef Bacik 		goto out;
9786a3891c5SJosef Bacik 	}
9796a3891c5SJosef Bacik 
980b9ef22deSFeifei Xu 	/* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
981c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode), BTRFS_MAX_EXTENT_SIZE >> 1,
982b9ef22deSFeifei Xu 					(BTRFS_MAX_EXTENT_SIZE >> 1)
983b9ef22deSFeifei Xu 					+ sectorsize - 1,
984330a5827SNikolay Borisov 					0, NULL);
9856a3891c5SJosef Bacik 	if (ret) {
9863c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
9876a3891c5SJosef Bacik 		goto out;
9886a3891c5SJosef Bacik 	}
9896a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 2) {
9906a3891c5SJosef Bacik 		ret = -EINVAL;
9913c7251f2SDavid Sterba 		test_err("miscount, wanted 2, got %u",
9926a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
9936a3891c5SJosef Bacik 		goto out;
9946a3891c5SJosef Bacik 	}
9956a3891c5SJosef Bacik 
9966a3891c5SJosef Bacik 	/*
997b9ef22deSFeifei Xu 	 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize]
9986a3891c5SJosef Bacik 	 */
999c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
1000b9ef22deSFeifei Xu 			BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize,
1001b9ef22deSFeifei Xu 			(BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1,
1002330a5827SNikolay Borisov 			0, NULL);
10036a3891c5SJosef Bacik 	if (ret) {
10043c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
10056a3891c5SJosef Bacik 		goto out;
10066a3891c5SJosef Bacik 	}
10076a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 4) {
10086a3891c5SJosef Bacik 		ret = -EINVAL;
10093c7251f2SDavid Sterba 		test_err("miscount, wanted 4, got %u",
10106a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10116a3891c5SJosef Bacik 		goto out;
10126a3891c5SJosef Bacik 	}
10136a3891c5SJosef Bacik 
1014b9ef22deSFeifei Xu 	/*
1015b9ef22deSFeifei Xu 	* [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize]
1016b9ef22deSFeifei Xu 	*/
1017c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
1018b9ef22deSFeifei Xu 			BTRFS_MAX_EXTENT_SIZE + sectorsize,
1019330a5827SNikolay Borisov 			BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL);
10206a3891c5SJosef Bacik 	if (ret) {
10213c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
10226a3891c5SJosef Bacik 		goto out;
10236a3891c5SJosef Bacik 	}
10246a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 3) {
10256a3891c5SJosef Bacik 		ret = -EINVAL;
10263c7251f2SDavid Sterba 		test_err("miscount, wanted 3, got %u",
10276a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10286a3891c5SJosef Bacik 		goto out;
10296a3891c5SJosef Bacik 	}
10306a3891c5SJosef Bacik 
10316a3891c5SJosef Bacik 	/* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */
10326a3891c5SJosef Bacik 	ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
1033b9ef22deSFeifei Xu 			       BTRFS_MAX_EXTENT_SIZE + sectorsize,
1034b9ef22deSFeifei Xu 			       BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1,
1035c3347309SFilipe Manana 			       EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1036bd015294SJosef Bacik 			       EXTENT_UPTODATE, NULL);
10376a3891c5SJosef Bacik 	if (ret) {
10383c7251f2SDavid Sterba 		test_err("clear_extent_bit returned %d", ret);
10396a3891c5SJosef Bacik 		goto out;
10406a3891c5SJosef Bacik 	}
10416a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 4) {
10426a3891c5SJosef Bacik 		ret = -EINVAL;
10433c7251f2SDavid Sterba 		test_err("miscount, wanted 4, got %u",
10446a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10456a3891c5SJosef Bacik 		goto out;
10466a3891c5SJosef Bacik 	}
10476a3891c5SJosef Bacik 
10486a3891c5SJosef Bacik 	/*
10496a3891c5SJosef Bacik 	 * Refill the hole again just for good measure, because I thought it
10506a3891c5SJosef Bacik 	 * might fail and I'd rather satisfy my paranoia at this point.
10516a3891c5SJosef Bacik 	 */
1052c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
1053b9ef22deSFeifei Xu 			BTRFS_MAX_EXTENT_SIZE + sectorsize,
1054330a5827SNikolay Borisov 			BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL);
10556a3891c5SJosef Bacik 	if (ret) {
10563c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
10576a3891c5SJosef Bacik 		goto out;
10586a3891c5SJosef Bacik 	}
10596a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 3) {
10606a3891c5SJosef Bacik 		ret = -EINVAL;
10613c7251f2SDavid Sterba 		test_err("miscount, wanted 3, got %u",
10626a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10636a3891c5SJosef Bacik 		goto out;
10646a3891c5SJosef Bacik 	}
10656a3891c5SJosef Bacik 
10666a3891c5SJosef Bacik 	/* Empty */
10676a3891c5SJosef Bacik 	ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1068c3347309SFilipe Manana 			       EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1069bd015294SJosef Bacik 			       EXTENT_UPTODATE, NULL);
10706a3891c5SJosef Bacik 	if (ret) {
10713c7251f2SDavid Sterba 		test_err("clear_extent_bit returned %d", ret);
10726a3891c5SJosef Bacik 		goto out;
10736a3891c5SJosef Bacik 	}
10746a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents) {
10756a3891c5SJosef Bacik 		ret = -EINVAL;
10763c7251f2SDavid Sterba 		test_err("miscount, wanted 0, got %u",
10776a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10786a3891c5SJosef Bacik 		goto out;
10796a3891c5SJosef Bacik 	}
10806a3891c5SJosef Bacik 	ret = 0;
10816a3891c5SJosef Bacik out:
10826a3891c5SJosef Bacik 	if (ret)
10836a3891c5SJosef Bacik 		clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1084c3347309SFilipe Manana 				 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1085bd015294SJosef Bacik 				 EXTENT_UPTODATE, NULL);
10866a3891c5SJosef Bacik 	iput(inode);
10876a3891c5SJosef Bacik 	btrfs_free_dummy_root(root);
10887c0260eeSJeff Mahoney 	btrfs_free_dummy_fs_info(fs_info);
10896a3891c5SJosef Bacik 	return ret;
10906a3891c5SJosef Bacik }
10916a3891c5SJosef Bacik 
btrfs_test_inodes(u32 sectorsize,u32 nodesize)1092b9ef22deSFeifei Xu int btrfs_test_inodes(u32 sectorsize, u32 nodesize)
1093aaedb55bSJosef Bacik {
10940e30db86SJosef Bacik 	int ret;
10950e30db86SJosef Bacik 
1096e4fa7469SDavid Sterba 	test_msg("running inode tests");
1097e4fa7469SDavid Sterba 
10980e30db86SJosef Bacik 	set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
10990e30db86SJosef Bacik 	set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
11000e30db86SJosef Bacik 
1101b9ef22deSFeifei Xu 	ret = test_btrfs_get_extent(sectorsize, nodesize);
11020e30db86SJosef Bacik 	if (ret)
11030e30db86SJosef Bacik 		return ret;
1104b9ef22deSFeifei Xu 	ret = test_hole_first(sectorsize, nodesize);
11056a3891c5SJosef Bacik 	if (ret)
11066a3891c5SJosef Bacik 		return ret;
1107b9ef22deSFeifei Xu 	return test_extent_accounting(sectorsize, nodesize);
1108aaedb55bSJosef Bacik }
1109