xref: /openbmc/linux/fs/btrfs/tests/inode-tests.c (revision 4c0c8cfc)
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"
14aaedb55bSJosef Bacik 
15aaedb55bSJosef Bacik static void insert_extent(struct btrfs_root *root, u64 start, u64 len,
16aaedb55bSJosef Bacik 			  u64 ram_bytes, u64 offset, u64 disk_bytenr,
17aaedb55bSJosef Bacik 			  u64 disk_len, u32 type, u8 compression, int slot)
18aaedb55bSJosef Bacik {
19aaedb55bSJosef Bacik 	struct btrfs_path path;
20aaedb55bSJosef Bacik 	struct btrfs_file_extent_item *fi;
21aaedb55bSJosef Bacik 	struct extent_buffer *leaf = root->node;
22aaedb55bSJosef Bacik 	struct btrfs_key key;
23aaedb55bSJosef Bacik 	u32 value_len = sizeof(struct btrfs_file_extent_item);
24aaedb55bSJosef Bacik 
25aaedb55bSJosef Bacik 	if (type == BTRFS_FILE_EXTENT_INLINE)
26aaedb55bSJosef Bacik 		value_len += len;
27aaedb55bSJosef Bacik 	memset(&path, 0, sizeof(path));
28aaedb55bSJosef Bacik 
29aaedb55bSJosef Bacik 	path.nodes[0] = leaf;
30aaedb55bSJosef Bacik 	path.slots[0] = slot;
31aaedb55bSJosef Bacik 
32aaedb55bSJosef Bacik 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
33aaedb55bSJosef Bacik 	key.type = BTRFS_EXTENT_DATA_KEY;
34aaedb55bSJosef Bacik 	key.offset = start;
35aaedb55bSJosef Bacik 
36f0641656SFilipe Manana 	btrfs_setup_item_for_insert(root, &path, &key, value_len);
37aaedb55bSJosef Bacik 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
38aaedb55bSJosef Bacik 	btrfs_set_file_extent_generation(leaf, fi, 1);
39aaedb55bSJosef Bacik 	btrfs_set_file_extent_type(leaf, fi, type);
40aaedb55bSJosef Bacik 	btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
41aaedb55bSJosef Bacik 	btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_len);
42aaedb55bSJosef Bacik 	btrfs_set_file_extent_offset(leaf, fi, offset);
43aaedb55bSJosef Bacik 	btrfs_set_file_extent_num_bytes(leaf, fi, len);
44aaedb55bSJosef Bacik 	btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
45aaedb55bSJosef Bacik 	btrfs_set_file_extent_compression(leaf, fi, compression);
46aaedb55bSJosef Bacik 	btrfs_set_file_extent_encryption(leaf, fi, 0);
47aaedb55bSJosef Bacik 	btrfs_set_file_extent_other_encoding(leaf, fi, 0);
48aaedb55bSJosef Bacik }
49aaedb55bSJosef Bacik 
500e30db86SJosef Bacik static void insert_inode_item_key(struct btrfs_root *root)
510e30db86SJosef Bacik {
520e30db86SJosef Bacik 	struct btrfs_path path;
530e30db86SJosef Bacik 	struct extent_buffer *leaf = root->node;
540e30db86SJosef Bacik 	struct btrfs_key key;
550e30db86SJosef Bacik 	u32 value_len = 0;
560e30db86SJosef Bacik 
570e30db86SJosef Bacik 	memset(&path, 0, sizeof(path));
580e30db86SJosef Bacik 
590e30db86SJosef Bacik 	path.nodes[0] = leaf;
600e30db86SJosef Bacik 	path.slots[0] = 0;
610e30db86SJosef Bacik 
620e30db86SJosef Bacik 	key.objectid = BTRFS_INODE_ITEM_KEY;
630e30db86SJosef Bacik 	key.type = BTRFS_INODE_ITEM_KEY;
640e30db86SJosef Bacik 	key.offset = 0;
650e30db86SJosef Bacik 
66f0641656SFilipe Manana 	btrfs_setup_item_for_insert(root, &path, &key, value_len);
670e30db86SJosef Bacik }
680e30db86SJosef Bacik 
69aaedb55bSJosef Bacik /*
70aaedb55bSJosef Bacik  * Build the most complicated map of extents the earth has ever seen.  We want
71aaedb55bSJosef Bacik  * this so we can test all of the corner cases of btrfs_get_extent.  Here is a
72aaedb55bSJosef Bacik  * diagram of how the extents will look though this may not be possible we still
73aaedb55bSJosef Bacik  * want to make sure everything acts normally (the last number is not inclusive)
74aaedb55bSJosef Bacik  *
75b9ef22deSFeifei Xu  * [0 - 5][5 -  6][     6 - 4096     ][ 4096 - 4100][4100 - 8195][8195 - 12291]
76b9ef22deSFeifei Xu  * [hole ][inline][hole but no extent][  hole   ][   regular ][regular1 split]
77aaedb55bSJosef Bacik  *
78b9ef22deSFeifei Xu  * [12291 - 16387][16387 - 24579][24579 - 28675][ 28675 - 32771][32771 - 36867 ]
79b9ef22deSFeifei Xu  * [    hole    ][regular1 split][   prealloc ][   prealloc1  ][prealloc1 written]
80aaedb55bSJosef Bacik  *
81b9ef22deSFeifei Xu  * [36867 - 45059][45059 - 53251][53251 - 57347][57347 - 61443][61443- 69635]
82b9ef22deSFeifei Xu  * [  prealloc1  ][ compressed  ][ compressed1 ][    regular  ][ compressed1]
83aaedb55bSJosef Bacik  *
84b9ef22deSFeifei Xu  * [69635-73731][   73731 - 86019   ][86019-90115]
85b9ef22deSFeifei Xu  * [  regular  ][ hole but no extent][  regular  ]
86aaedb55bSJosef Bacik  */
87b9ef22deSFeifei Xu static void setup_file_extents(struct btrfs_root *root, u32 sectorsize)
88aaedb55bSJosef Bacik {
89aaedb55bSJosef Bacik 	int slot = 0;
90ee22184bSByongho Lee 	u64 disk_bytenr = SZ_1M;
91aaedb55bSJosef Bacik 	u64 offset = 0;
92aaedb55bSJosef Bacik 
93aaedb55bSJosef Bacik 	/* First we want a hole */
94aaedb55bSJosef Bacik 	insert_extent(root, offset, 5, 5, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
95aaedb55bSJosef Bacik 		      slot);
96aaedb55bSJosef Bacik 	slot++;
97aaedb55bSJosef Bacik 	offset += 5;
98aaedb55bSJosef Bacik 
99aaedb55bSJosef Bacik 	/*
100aaedb55bSJosef Bacik 	 * Now we want an inline extent, I don't think this is possible but hey
101aaedb55bSJosef Bacik 	 * why not?  Also keep in mind if we have an inline extent it counts as
102aaedb55bSJosef Bacik 	 * the whole first page.  If we were to expand it we would have to cow
103aaedb55bSJosef Bacik 	 * and we wouldn't have an inline extent anymore.
104aaedb55bSJosef Bacik 	 */
105aaedb55bSJosef Bacik 	insert_extent(root, offset, 1, 1, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE, 0,
106aaedb55bSJosef Bacik 		      slot);
107aaedb55bSJosef Bacik 	slot++;
108b9ef22deSFeifei Xu 	offset = sectorsize;
109aaedb55bSJosef Bacik 
110aaedb55bSJosef Bacik 	/* Now another hole */
111aaedb55bSJosef Bacik 	insert_extent(root, offset, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
112aaedb55bSJosef Bacik 		      slot);
113aaedb55bSJosef Bacik 	slot++;
114aaedb55bSJosef Bacik 	offset += 4;
115aaedb55bSJosef Bacik 
116aaedb55bSJosef Bacik 	/* Now for a regular extent */
117b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize - 1, sectorsize - 1, 0,
118b9ef22deSFeifei Xu 		      disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
119aaedb55bSJosef Bacik 	slot++;
120b9ef22deSFeifei Xu 	disk_bytenr += sectorsize;
121b9ef22deSFeifei Xu 	offset += sectorsize - 1;
122aaedb55bSJosef Bacik 
123aaedb55bSJosef Bacik 	/*
124aaedb55bSJosef Bacik 	 * Now for 3 extents that were split from a hole punch so we test
125aaedb55bSJosef Bacik 	 * offsets properly.
126aaedb55bSJosef Bacik 	 */
127b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
128b9ef22deSFeifei Xu 		      4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
129b9ef22deSFeifei Xu 	slot++;
130b9ef22deSFeifei Xu 	offset += sectorsize;
131b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0, 0, 0,
132aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
133aaedb55bSJosef Bacik 	slot++;
134b9ef22deSFeifei Xu 	offset += sectorsize;
135b9ef22deSFeifei Xu 	insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
136b9ef22deSFeifei Xu 		      2 * sectorsize, disk_bytenr, 4 * sectorsize,
137aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
138aaedb55bSJosef Bacik 	slot++;
139b9ef22deSFeifei Xu 	offset += 2 * sectorsize;
140b9ef22deSFeifei Xu 	disk_bytenr += 4 * sectorsize;
141aaedb55bSJosef Bacik 
142aaedb55bSJosef Bacik 	/* Now for a unwritten prealloc extent */
143b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
144b9ef22deSFeifei Xu 		sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
145aaedb55bSJosef Bacik 	slot++;
146b9ef22deSFeifei Xu 	offset += sectorsize;
147aaedb55bSJosef Bacik 
148aaedb55bSJosef Bacik 	/*
149aaedb55bSJosef Bacik 	 * We want to jack up disk_bytenr a little more so the em stuff doesn't
150aaedb55bSJosef Bacik 	 * merge our records.
151aaedb55bSJosef Bacik 	 */
152b9ef22deSFeifei Xu 	disk_bytenr += 2 * sectorsize;
153aaedb55bSJosef Bacik 
154aaedb55bSJosef Bacik 	/*
155aaedb55bSJosef Bacik 	 * Now for a partially written prealloc extent, basically the same as
156aaedb55bSJosef Bacik 	 * the hole punch example above.  Ram_bytes never changes when you mark
157aaedb55bSJosef Bacik 	 * extents written btw.
158aaedb55bSJosef Bacik 	 */
159b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
160b9ef22deSFeifei Xu 		      4 * sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
161b9ef22deSFeifei Xu 	slot++;
162b9ef22deSFeifei Xu 	offset += sectorsize;
163b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, 4 * sectorsize, sectorsize,
164b9ef22deSFeifei Xu 		      disk_bytenr, 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0,
165b9ef22deSFeifei Xu 		      slot);
166b9ef22deSFeifei Xu 	slot++;
167b9ef22deSFeifei Xu 	offset += sectorsize;
168b9ef22deSFeifei Xu 	insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
169b9ef22deSFeifei Xu 		      2 * sectorsize, disk_bytenr, 4 * sectorsize,
170aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
171aaedb55bSJosef Bacik 	slot++;
172b9ef22deSFeifei Xu 	offset += 2 * sectorsize;
173b9ef22deSFeifei Xu 	disk_bytenr += 4 * sectorsize;
174aaedb55bSJosef Bacik 
175aaedb55bSJosef Bacik 	/* Now a normal compressed extent */
176b9ef22deSFeifei Xu 	insert_extent(root, offset, 2 * sectorsize, 2 * sectorsize, 0,
177b9ef22deSFeifei Xu 		      disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG,
178b9ef22deSFeifei Xu 		      BTRFS_COMPRESS_ZLIB, slot);
179aaedb55bSJosef Bacik 	slot++;
180b9ef22deSFeifei Xu 	offset += 2 * sectorsize;
181aaedb55bSJosef Bacik 	/* No merges */
182b9ef22deSFeifei Xu 	disk_bytenr += 2 * sectorsize;
183aaedb55bSJosef Bacik 
184aaedb55bSJosef Bacik 	/* Now a split compressed extent */
185b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
186b9ef22deSFeifei Xu 		      sectorsize, BTRFS_FILE_EXTENT_REG,
187b9ef22deSFeifei Xu 		      BTRFS_COMPRESS_ZLIB, slot);
188aaedb55bSJosef Bacik 	slot++;
189b9ef22deSFeifei Xu 	offset += sectorsize;
190b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0,
191b9ef22deSFeifei Xu 		      disk_bytenr + sectorsize, sectorsize,
192aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
193aaedb55bSJosef Bacik 	slot++;
194b9ef22deSFeifei Xu 	offset += sectorsize;
195b9ef22deSFeifei Xu 	insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
196b9ef22deSFeifei Xu 		      2 * sectorsize, disk_bytenr, sectorsize,
197aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
198aaedb55bSJosef Bacik 	slot++;
199b9ef22deSFeifei Xu 	offset += 2 * sectorsize;
200b9ef22deSFeifei Xu 	disk_bytenr += 2 * sectorsize;
201aaedb55bSJosef Bacik 
202aaedb55bSJosef Bacik 	/* Now extents that have a hole but no hole extent */
203b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
204b9ef22deSFeifei Xu 		      sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
205aaedb55bSJosef Bacik 	slot++;
206b9ef22deSFeifei Xu 	offset += 4 * sectorsize;
207b9ef22deSFeifei Xu 	disk_bytenr += sectorsize;
208b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
209b9ef22deSFeifei Xu 		      sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
210aaedb55bSJosef Bacik }
211aaedb55bSJosef Bacik 
212aaedb55bSJosef Bacik static unsigned long prealloc_only = 0;
213aaedb55bSJosef Bacik static unsigned long compressed_only = 0;
214aaedb55bSJosef Bacik static unsigned long vacancy_only = 0;
215aaedb55bSJosef Bacik 
216b9ef22deSFeifei Xu static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize)
217aaedb55bSJosef Bacik {
2187c0260eeSJeff Mahoney 	struct btrfs_fs_info *fs_info = NULL;
219aaedb55bSJosef Bacik 	struct inode *inode = NULL;
220aaedb55bSJosef Bacik 	struct btrfs_root *root = NULL;
221aaedb55bSJosef Bacik 	struct extent_map *em = NULL;
222aaedb55bSJosef Bacik 	u64 orig_start;
223aaedb55bSJosef Bacik 	u64 disk_bytenr;
224aaedb55bSJosef Bacik 	u64 offset;
225aaedb55bSJosef Bacik 	int ret = -ENOMEM;
226aaedb55bSJosef Bacik 
227e4fa7469SDavid Sterba 	test_msg("running btrfs_get_extent tests");
228e4fa7469SDavid Sterba 
229aaedb55bSJosef Bacik 	inode = btrfs_new_test_inode();
230aaedb55bSJosef Bacik 	if (!inode) {
2316a060db8SDavid Sterba 		test_std_err(TEST_ALLOC_INODE);
232aaedb55bSJosef Bacik 		return ret;
233aaedb55bSJosef Bacik 	}
234aaedb55bSJosef Bacik 
235da17066cSJeff Mahoney 	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
2367c0260eeSJeff Mahoney 	if (!fs_info) {
23737b2a7bcSDavid Sterba 		test_std_err(TEST_ALLOC_FS_INFO);
238aaedb55bSJosef Bacik 		goto out;
239aaedb55bSJosef Bacik 	}
240aaedb55bSJosef Bacik 
241da17066cSJeff Mahoney 	root = btrfs_alloc_dummy_root(fs_info);
2427c0260eeSJeff Mahoney 	if (IS_ERR(root)) {
24352ab7bcaSDavid Sterba 		test_std_err(TEST_ALLOC_ROOT);
244aaedb55bSJosef Bacik 		goto out;
245aaedb55bSJosef Bacik 	}
246aaedb55bSJosef Bacik 
247da17066cSJeff Mahoney 	root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
248aaedb55bSJosef Bacik 	if (!root->node) {
2499e3d9f84SDavid Sterba 		test_std_err(TEST_ALLOC_ROOT);
250aaedb55bSJosef Bacik 		goto out;
251aaedb55bSJosef Bacik 	}
252aaedb55bSJosef Bacik 
253aaedb55bSJosef Bacik 	btrfs_set_header_nritems(root->node, 0);
254aaedb55bSJosef Bacik 	btrfs_set_header_level(root->node, 0);
255aaedb55bSJosef Bacik 	ret = -EINVAL;
256aaedb55bSJosef Bacik 
257aaedb55bSJosef Bacik 	/* First with no extents */
258aaedb55bSJosef Bacik 	BTRFS_I(inode)->root = root;
25939b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, sectorsize);
260aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
261aaedb55bSJosef Bacik 		em = NULL;
2623c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
263aaedb55bSJosef Bacik 		goto out;
264aaedb55bSJosef Bacik 	}
265aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
2663c7251f2SDavid Sterba 		test_err("expected a hole, got %llu", em->block_start);
267aaedb55bSJosef Bacik 		goto out;
268aaedb55bSJosef Bacik 	}
269aaedb55bSJosef Bacik 	free_extent_map(em);
270*4c0c8cfcSFilipe Manana 	btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false);
271aaedb55bSJosef Bacik 
272aaedb55bSJosef Bacik 	/*
273aaedb55bSJosef Bacik 	 * All of the magic numbers are based on the mapping setup in
274aaedb55bSJosef Bacik 	 * setup_file_extents, so if you change anything there you need to
275aaedb55bSJosef Bacik 	 * update the comment and update the expected values below.
276aaedb55bSJosef Bacik 	 */
277b9ef22deSFeifei Xu 	setup_file_extents(root, sectorsize);
278aaedb55bSJosef Bacik 
27939b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, (u64)-1);
280aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
2813c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
282aaedb55bSJosef Bacik 		goto out;
283aaedb55bSJosef Bacik 	}
284aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
2853c7251f2SDavid Sterba 		test_err("expected a hole, got %llu", em->block_start);
286aaedb55bSJosef Bacik 		goto out;
287aaedb55bSJosef Bacik 	}
288aaedb55bSJosef Bacik 	if (em->start != 0 || em->len != 5) {
2893c7251f2SDavid Sterba 		test_err(
2903c7251f2SDavid Sterba 		"unexpected extent wanted start 0 len 5, got start %llu len %llu",
2913c7251f2SDavid Sterba 			em->start, em->len);
292aaedb55bSJosef Bacik 		goto out;
293aaedb55bSJosef Bacik 	}
294aaedb55bSJosef Bacik 	if (em->flags != 0) {
2953c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
296aaedb55bSJosef Bacik 		goto out;
297aaedb55bSJosef Bacik 	}
298aaedb55bSJosef Bacik 	offset = em->start + em->len;
299aaedb55bSJosef Bacik 	free_extent_map(em);
300aaedb55bSJosef Bacik 
30139b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
302aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
3033c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
304aaedb55bSJosef Bacik 		goto out;
305aaedb55bSJosef Bacik 	}
306aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_INLINE) {
3073c7251f2SDavid Sterba 		test_err("expected an inline, got %llu", em->block_start);
308aaedb55bSJosef Bacik 		goto out;
309aaedb55bSJosef Bacik 	}
310b9ef22deSFeifei Xu 
311b9ef22deSFeifei Xu 	if (em->start != offset || em->len != (sectorsize - 5)) {
3123c7251f2SDavid Sterba 		test_err(
3133c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len 1, got start %llu len %llu",
3143c7251f2SDavid Sterba 			offset, em->start, em->len);
315aaedb55bSJosef Bacik 		goto out;
316aaedb55bSJosef Bacik 	}
317aaedb55bSJosef Bacik 	if (em->flags != 0) {
3183c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
319aaedb55bSJosef Bacik 		goto out;
320aaedb55bSJosef Bacik 	}
321aaedb55bSJosef Bacik 	/*
322aaedb55bSJosef Bacik 	 * We don't test anything else for inline since it doesn't get set
323aaedb55bSJosef Bacik 	 * unless we have a page for it to write into.  Maybe we should change
324aaedb55bSJosef Bacik 	 * this?
325aaedb55bSJosef Bacik 	 */
326aaedb55bSJosef Bacik 	offset = em->start + em->len;
327aaedb55bSJosef Bacik 	free_extent_map(em);
328aaedb55bSJosef Bacik 
32939b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
330aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
3313c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
332aaedb55bSJosef Bacik 		goto out;
333aaedb55bSJosef Bacik 	}
334aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
3353c7251f2SDavid Sterba 		test_err("expected a hole, got %llu", em->block_start);
336aaedb55bSJosef Bacik 		goto out;
337aaedb55bSJosef Bacik 	}
338aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 4) {
3393c7251f2SDavid Sterba 		test_err(
3403c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len 4, got start %llu len %llu",
3413c7251f2SDavid Sterba 			offset, em->start, em->len);
342aaedb55bSJosef Bacik 		goto out;
343aaedb55bSJosef Bacik 	}
344aaedb55bSJosef Bacik 	if (em->flags != 0) {
3453c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
346aaedb55bSJosef Bacik 		goto out;
347aaedb55bSJosef Bacik 	}
348aaedb55bSJosef Bacik 	offset = em->start + em->len;
349aaedb55bSJosef Bacik 	free_extent_map(em);
350aaedb55bSJosef Bacik 
351aaedb55bSJosef Bacik 	/* Regular extent */
35239b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
353aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
3543c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
355aaedb55bSJosef Bacik 		goto out;
356aaedb55bSJosef Bacik 	}
357aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
3583c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
359aaedb55bSJosef Bacik 		goto out;
360aaedb55bSJosef Bacik 	}
361b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize - 1) {
3623c7251f2SDavid Sterba 		test_err(
3633c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len 4095, got start %llu len %llu",
3643c7251f2SDavid Sterba 			offset, em->start, em->len);
365aaedb55bSJosef Bacik 		goto out;
366aaedb55bSJosef Bacik 	}
367aaedb55bSJosef Bacik 	if (em->flags != 0) {
3683c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
369aaedb55bSJosef Bacik 		goto out;
370aaedb55bSJosef Bacik 	}
371aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
3723c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
373aaedb55bSJosef Bacik 			 em->orig_start);
374aaedb55bSJosef Bacik 		goto out;
375aaedb55bSJosef Bacik 	}
376aaedb55bSJosef Bacik 	offset = em->start + em->len;
377aaedb55bSJosef Bacik 	free_extent_map(em);
378aaedb55bSJosef Bacik 
379aaedb55bSJosef Bacik 	/* The next 3 are split extents */
38039b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
381aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
3823c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
383aaedb55bSJosef Bacik 		goto out;
384aaedb55bSJosef Bacik 	}
385aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
3863c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
387aaedb55bSJosef Bacik 		goto out;
388aaedb55bSJosef Bacik 	}
389b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
3903c7251f2SDavid Sterba 		test_err(
3913c7251f2SDavid Sterba 		"unexpected extent start %llu len %u, got start %llu len %llu",
392b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
393aaedb55bSJosef Bacik 		goto out;
394aaedb55bSJosef Bacik 	}
395aaedb55bSJosef Bacik 	if (em->flags != 0) {
3963c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
397aaedb55bSJosef Bacik 		goto out;
398aaedb55bSJosef Bacik 	}
399aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
4003c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
401aaedb55bSJosef Bacik 			 em->orig_start);
402aaedb55bSJosef Bacik 		goto out;
403aaedb55bSJosef Bacik 	}
404aaedb55bSJosef Bacik 	disk_bytenr = em->block_start;
405aaedb55bSJosef Bacik 	orig_start = em->start;
406aaedb55bSJosef Bacik 	offset = em->start + em->len;
407aaedb55bSJosef Bacik 	free_extent_map(em);
408aaedb55bSJosef Bacik 
40939b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
410aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
4113c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
412aaedb55bSJosef Bacik 		goto out;
413aaedb55bSJosef Bacik 	}
414aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
4153c7251f2SDavid Sterba 		test_err("expected a hole, got %llu", em->block_start);
416aaedb55bSJosef Bacik 		goto out;
417aaedb55bSJosef Bacik 	}
418b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
4193c7251f2SDavid Sterba 		test_err(
4203c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
421b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
422aaedb55bSJosef Bacik 		goto out;
423aaedb55bSJosef Bacik 	}
424aaedb55bSJosef Bacik 	if (em->flags != 0) {
4253c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
426aaedb55bSJosef Bacik 		goto out;
427aaedb55bSJosef Bacik 	}
428aaedb55bSJosef Bacik 	offset = em->start + em->len;
429aaedb55bSJosef Bacik 	free_extent_map(em);
430aaedb55bSJosef Bacik 
43139b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
432aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
4333c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
434aaedb55bSJosef Bacik 		goto out;
435aaedb55bSJosef Bacik 	}
436aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
4373c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
438aaedb55bSJosef Bacik 		goto out;
439aaedb55bSJosef Bacik 	}
440b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 2 * sectorsize) {
4413c7251f2SDavid Sterba 		test_err(
4423c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
443b9ef22deSFeifei Xu 			offset, 2 * sectorsize, em->start, em->len);
444aaedb55bSJosef Bacik 		goto out;
445aaedb55bSJosef Bacik 	}
446aaedb55bSJosef Bacik 	if (em->flags != 0) {
4473c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
448aaedb55bSJosef Bacik 		goto out;
449aaedb55bSJosef Bacik 	}
450aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
4513c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu",
452aaedb55bSJosef Bacik 			 orig_start, em->orig_start);
453aaedb55bSJosef Bacik 		goto out;
454aaedb55bSJosef Bacik 	}
455aaedb55bSJosef Bacik 	disk_bytenr += (em->start - orig_start);
456aaedb55bSJosef Bacik 	if (em->block_start != disk_bytenr) {
4573c7251f2SDavid Sterba 		test_err("wrong block start, want %llu, have %llu",
458aaedb55bSJosef Bacik 			 disk_bytenr, em->block_start);
459aaedb55bSJosef Bacik 		goto out;
460aaedb55bSJosef Bacik 	}
461aaedb55bSJosef Bacik 	offset = em->start + em->len;
462aaedb55bSJosef Bacik 	free_extent_map(em);
463aaedb55bSJosef Bacik 
464aaedb55bSJosef Bacik 	/* Prealloc extent */
46539b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
466aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
4673c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
468aaedb55bSJosef Bacik 		goto out;
469aaedb55bSJosef Bacik 	}
470aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
4713c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
472aaedb55bSJosef Bacik 		goto out;
473aaedb55bSJosef Bacik 	}
474b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
4753c7251f2SDavid Sterba 		test_err(
4763c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
477b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
478aaedb55bSJosef Bacik 		goto out;
479aaedb55bSJosef Bacik 	}
480aaedb55bSJosef Bacik 	if (em->flags != prealloc_only) {
4813c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
482aaedb55bSJosef Bacik 			 prealloc_only, em->flags);
483aaedb55bSJosef Bacik 		goto out;
484aaedb55bSJosef Bacik 	}
485aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
4863c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
487aaedb55bSJosef Bacik 			 em->orig_start);
488aaedb55bSJosef Bacik 		goto out;
489aaedb55bSJosef Bacik 	}
490aaedb55bSJosef Bacik 	offset = em->start + em->len;
491aaedb55bSJosef Bacik 	free_extent_map(em);
492aaedb55bSJosef Bacik 
493aaedb55bSJosef Bacik 	/* The next 3 are a half written prealloc extent */
49439b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
495aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
4963c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
497aaedb55bSJosef Bacik 		goto out;
498aaedb55bSJosef Bacik 	}
499aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
5003c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
501aaedb55bSJosef Bacik 		goto out;
502aaedb55bSJosef Bacik 	}
503b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
5043c7251f2SDavid Sterba 		test_err(
5053c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
506b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
507aaedb55bSJosef Bacik 		goto out;
508aaedb55bSJosef Bacik 	}
509aaedb55bSJosef Bacik 	if (em->flags != prealloc_only) {
5103c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
511aaedb55bSJosef Bacik 			 prealloc_only, em->flags);
512aaedb55bSJosef Bacik 		goto out;
513aaedb55bSJosef Bacik 	}
514aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
5153c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
516aaedb55bSJosef Bacik 			 em->orig_start);
517aaedb55bSJosef Bacik 		goto out;
518aaedb55bSJosef Bacik 	}
519aaedb55bSJosef Bacik 	disk_bytenr = em->block_start;
520aaedb55bSJosef Bacik 	orig_start = em->start;
521aaedb55bSJosef Bacik 	offset = em->start + em->len;
522aaedb55bSJosef Bacik 	free_extent_map(em);
523aaedb55bSJosef Bacik 
52439b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
525aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
5263c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
527aaedb55bSJosef Bacik 		goto out;
528aaedb55bSJosef Bacik 	}
529aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_HOLE) {
5303c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
531aaedb55bSJosef Bacik 		goto out;
532aaedb55bSJosef Bacik 	}
533b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
5343c7251f2SDavid Sterba 		test_err(
5353c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
536b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
537aaedb55bSJosef Bacik 		goto out;
538aaedb55bSJosef Bacik 	}
539aaedb55bSJosef Bacik 	if (em->flags != 0) {
5403c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
541aaedb55bSJosef Bacik 		goto out;
542aaedb55bSJosef Bacik 	}
543aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
5443c7251f2SDavid Sterba 		test_err("unexpected orig offset, wanted %llu, have %llu",
545aaedb55bSJosef Bacik 			 orig_start, em->orig_start);
546aaedb55bSJosef Bacik 		goto out;
547aaedb55bSJosef Bacik 	}
548aaedb55bSJosef Bacik 	if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
5493c7251f2SDavid Sterba 		test_err("unexpected block start, wanted %llu, have %llu",
550aaedb55bSJosef Bacik 			 disk_bytenr + (em->start - em->orig_start),
551aaedb55bSJosef Bacik 			 em->block_start);
552aaedb55bSJosef Bacik 		goto out;
553aaedb55bSJosef Bacik 	}
554aaedb55bSJosef Bacik 	offset = em->start + em->len;
555aaedb55bSJosef Bacik 	free_extent_map(em);
556aaedb55bSJosef Bacik 
55739b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
558aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
5593c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
560aaedb55bSJosef Bacik 		goto out;
561aaedb55bSJosef Bacik 	}
562aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
5633c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
564aaedb55bSJosef Bacik 		goto out;
565aaedb55bSJosef Bacik 	}
566b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 2 * sectorsize) {
5673c7251f2SDavid Sterba 		test_err(
5683c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
569b9ef22deSFeifei Xu 			offset, 2 * sectorsize, em->start, em->len);
570aaedb55bSJosef Bacik 		goto out;
571aaedb55bSJosef Bacik 	}
572aaedb55bSJosef Bacik 	if (em->flags != prealloc_only) {
5733c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
574aaedb55bSJosef Bacik 			 prealloc_only, em->flags);
575aaedb55bSJosef Bacik 		goto out;
576aaedb55bSJosef Bacik 	}
577aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
5783c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", orig_start,
579aaedb55bSJosef Bacik 			 em->orig_start);
580aaedb55bSJosef Bacik 		goto out;
581aaedb55bSJosef Bacik 	}
582aaedb55bSJosef Bacik 	if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
5833c7251f2SDavid Sterba 		test_err("unexpected block start, wanted %llu, have %llu",
584aaedb55bSJosef Bacik 			 disk_bytenr + (em->start - em->orig_start),
585aaedb55bSJosef Bacik 			 em->block_start);
586aaedb55bSJosef Bacik 		goto out;
587aaedb55bSJosef Bacik 	}
588aaedb55bSJosef Bacik 	offset = em->start + em->len;
589aaedb55bSJosef Bacik 	free_extent_map(em);
590aaedb55bSJosef Bacik 
591aaedb55bSJosef Bacik 	/* Now for the compressed extent */
59239b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
593aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
5943c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
595aaedb55bSJosef Bacik 		goto out;
596aaedb55bSJosef Bacik 	}
597aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
5983c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
599aaedb55bSJosef Bacik 		goto out;
600aaedb55bSJosef Bacik 	}
601b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 2 * sectorsize) {
6023c7251f2SDavid Sterba 		test_err(
6033c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
604b9ef22deSFeifei Xu 			offset, 2 * sectorsize, em->start, em->len);
605aaedb55bSJosef Bacik 		goto out;
606aaedb55bSJosef Bacik 	}
607aaedb55bSJosef Bacik 	if (em->flags != compressed_only) {
6083c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
609aaedb55bSJosef Bacik 			 compressed_only, em->flags);
610aaedb55bSJosef Bacik 		goto out;
611aaedb55bSJosef Bacik 	}
612aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
6133c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu",
614aaedb55bSJosef Bacik 			 em->start, em->orig_start);
615aaedb55bSJosef Bacik 		goto out;
616aaedb55bSJosef Bacik 	}
617aaedb55bSJosef Bacik 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
6183c7251f2SDavid Sterba 		test_err("unexpected compress type, wanted %d, got %d",
619aaedb55bSJosef Bacik 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
620aaedb55bSJosef Bacik 		goto out;
621aaedb55bSJosef Bacik 	}
622aaedb55bSJosef Bacik 	offset = em->start + em->len;
623aaedb55bSJosef Bacik 	free_extent_map(em);
624aaedb55bSJosef Bacik 
625aaedb55bSJosef Bacik 	/* Split compressed extent */
62639b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
627aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
6283c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
629aaedb55bSJosef Bacik 		goto out;
630aaedb55bSJosef Bacik 	}
631aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
6323c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
633aaedb55bSJosef Bacik 		goto out;
634aaedb55bSJosef Bacik 	}
635b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
6363c7251f2SDavid Sterba 		test_err(
6373c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
638b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
639aaedb55bSJosef Bacik 		goto out;
640aaedb55bSJosef Bacik 	}
641aaedb55bSJosef Bacik 	if (em->flags != compressed_only) {
6423c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
643aaedb55bSJosef Bacik 			 compressed_only, em->flags);
644aaedb55bSJosef Bacik 		goto out;
645aaedb55bSJosef Bacik 	}
646aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
6473c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu",
648aaedb55bSJosef Bacik 			 em->start, em->orig_start);
649aaedb55bSJosef Bacik 		goto out;
650aaedb55bSJosef Bacik 	}
651aaedb55bSJosef Bacik 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
6523c7251f2SDavid Sterba 		test_err("unexpected compress type, wanted %d, got %d",
653aaedb55bSJosef Bacik 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
654aaedb55bSJosef Bacik 		goto out;
655aaedb55bSJosef Bacik 	}
656aaedb55bSJosef Bacik 	disk_bytenr = em->block_start;
657aaedb55bSJosef Bacik 	orig_start = em->start;
658aaedb55bSJosef Bacik 	offset = em->start + em->len;
659aaedb55bSJosef Bacik 	free_extent_map(em);
660aaedb55bSJosef Bacik 
66139b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
662aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
6633c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
664aaedb55bSJosef Bacik 		goto out;
665aaedb55bSJosef Bacik 	}
666aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
6673c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
668aaedb55bSJosef Bacik 		goto out;
669aaedb55bSJosef Bacik 	}
670b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
6713c7251f2SDavid Sterba 		test_err(
6723c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
673b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
674aaedb55bSJosef Bacik 		goto out;
675aaedb55bSJosef Bacik 	}
676aaedb55bSJosef Bacik 	if (em->flags != 0) {
6773c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
678aaedb55bSJosef Bacik 		goto out;
679aaedb55bSJosef Bacik 	}
680aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
6813c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
682aaedb55bSJosef Bacik 			 em->orig_start);
683aaedb55bSJosef Bacik 		goto out;
684aaedb55bSJosef Bacik 	}
685aaedb55bSJosef Bacik 	offset = em->start + em->len;
686aaedb55bSJosef Bacik 	free_extent_map(em);
687aaedb55bSJosef Bacik 
68839b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
689aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
6903c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
691aaedb55bSJosef Bacik 		goto out;
692aaedb55bSJosef Bacik 	}
693aaedb55bSJosef Bacik 	if (em->block_start != disk_bytenr) {
6943c7251f2SDavid Sterba 		test_err("block start does not match, want %llu got %llu",
695aaedb55bSJosef Bacik 			 disk_bytenr, em->block_start);
696aaedb55bSJosef Bacik 		goto out;
697aaedb55bSJosef Bacik 	}
698b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 2 * sectorsize) {
6993c7251f2SDavid Sterba 		test_err(
7003c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
701b9ef22deSFeifei Xu 			offset, 2 * sectorsize, em->start, em->len);
702aaedb55bSJosef Bacik 		goto out;
703aaedb55bSJosef Bacik 	}
704aaedb55bSJosef Bacik 	if (em->flags != compressed_only) {
7053c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
706aaedb55bSJosef Bacik 			 compressed_only, em->flags);
707aaedb55bSJosef Bacik 		goto out;
708aaedb55bSJosef Bacik 	}
709aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
7103c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu",
711aaedb55bSJosef Bacik 			 em->start, orig_start);
712aaedb55bSJosef Bacik 		goto out;
713aaedb55bSJosef Bacik 	}
714aaedb55bSJosef Bacik 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
7153c7251f2SDavid Sterba 		test_err("unexpected compress type, wanted %d, got %d",
716aaedb55bSJosef Bacik 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
717aaedb55bSJosef Bacik 		goto out;
718aaedb55bSJosef Bacik 	}
719aaedb55bSJosef Bacik 	offset = em->start + em->len;
720aaedb55bSJosef Bacik 	free_extent_map(em);
721aaedb55bSJosef Bacik 
722aaedb55bSJosef Bacik 	/* A hole between regular extents but no hole extent */
72339b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset + 6, sectorsize);
724aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
7253c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
726aaedb55bSJosef Bacik 		goto out;
727aaedb55bSJosef Bacik 	}
728aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
7293c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
730aaedb55bSJosef Bacik 		goto out;
731aaedb55bSJosef Bacik 	}
732b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
7333c7251f2SDavid Sterba 		test_err(
7343c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
735b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
736aaedb55bSJosef Bacik 		goto out;
737aaedb55bSJosef Bacik 	}
738aaedb55bSJosef Bacik 	if (em->flags != 0) {
7393c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
740aaedb55bSJosef Bacik 		goto out;
741aaedb55bSJosef Bacik 	}
742aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
7433c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
744aaedb55bSJosef Bacik 			 em->orig_start);
745aaedb55bSJosef Bacik 		goto out;
746aaedb55bSJosef Bacik 	}
747aaedb55bSJosef Bacik 	offset = em->start + em->len;
748aaedb55bSJosef Bacik 	free_extent_map(em);
749aaedb55bSJosef Bacik 
75039b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, SZ_4M);
751aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
7523c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
753aaedb55bSJosef Bacik 		goto out;
754aaedb55bSJosef Bacik 	}
755aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
7563c7251f2SDavid Sterba 		test_err("expected a hole extent, got %llu", em->block_start);
757aaedb55bSJosef Bacik 		goto out;
758aaedb55bSJosef Bacik 	}
759aaedb55bSJosef Bacik 	/*
760aaedb55bSJosef Bacik 	 * Currently we just return a length that we requested rather than the
761aaedb55bSJosef Bacik 	 * length of the actual hole, if this changes we'll have to change this
762aaedb55bSJosef Bacik 	 * test.
763aaedb55bSJosef Bacik 	 */
764b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 3 * sectorsize) {
7653c7251f2SDavid Sterba 		test_err(
7663c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
767b9ef22deSFeifei Xu 			offset, 3 * sectorsize, em->start, em->len);
768aaedb55bSJosef Bacik 		goto out;
769aaedb55bSJosef Bacik 	}
770aaedb55bSJosef Bacik 	if (em->flags != vacancy_only) {
7713c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
772aaedb55bSJosef Bacik 			 vacancy_only, em->flags);
773aaedb55bSJosef Bacik 		goto out;
774aaedb55bSJosef Bacik 	}
775aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
7763c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
777aaedb55bSJosef Bacik 			 em->orig_start);
778aaedb55bSJosef Bacik 		goto out;
779aaedb55bSJosef Bacik 	}
780aaedb55bSJosef Bacik 	offset = em->start + em->len;
781aaedb55bSJosef Bacik 	free_extent_map(em);
782aaedb55bSJosef Bacik 
78339b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
784aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
7853c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
786aaedb55bSJosef Bacik 		goto out;
787aaedb55bSJosef Bacik 	}
788aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
7893c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
790aaedb55bSJosef Bacik 		goto out;
791aaedb55bSJosef Bacik 	}
792b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
7933c7251f2SDavid Sterba 		test_err(
7943c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
795b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
796aaedb55bSJosef Bacik 		goto out;
797aaedb55bSJosef Bacik 	}
798aaedb55bSJosef Bacik 	if (em->flags != 0) {
7993c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
800aaedb55bSJosef Bacik 		goto out;
801aaedb55bSJosef Bacik 	}
802aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
8033c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
804aaedb55bSJosef Bacik 			 em->orig_start);
805aaedb55bSJosef Bacik 		goto out;
806aaedb55bSJosef Bacik 	}
807aaedb55bSJosef Bacik 	ret = 0;
808aaedb55bSJosef Bacik out:
809aaedb55bSJosef Bacik 	if (!IS_ERR(em))
810aaedb55bSJosef Bacik 		free_extent_map(em);
811aaedb55bSJosef Bacik 	iput(inode);
812faa2dbf0SJosef Bacik 	btrfs_free_dummy_root(root);
8137c0260eeSJeff Mahoney 	btrfs_free_dummy_fs_info(fs_info);
814aaedb55bSJosef Bacik 	return ret;
815aaedb55bSJosef Bacik }
816aaedb55bSJosef Bacik 
817b9ef22deSFeifei Xu static int test_hole_first(u32 sectorsize, u32 nodesize)
8180e30db86SJosef Bacik {
8197c0260eeSJeff Mahoney 	struct btrfs_fs_info *fs_info = NULL;
8200e30db86SJosef Bacik 	struct inode *inode = NULL;
8210e30db86SJosef Bacik 	struct btrfs_root *root = NULL;
8220e30db86SJosef Bacik 	struct extent_map *em = NULL;
8230e30db86SJosef Bacik 	int ret = -ENOMEM;
8240e30db86SJosef Bacik 
825e4fa7469SDavid Sterba 	test_msg("running hole first btrfs_get_extent test");
826e4fa7469SDavid Sterba 
8270e30db86SJosef Bacik 	inode = btrfs_new_test_inode();
8280e30db86SJosef Bacik 	if (!inode) {
8296a060db8SDavid Sterba 		test_std_err(TEST_ALLOC_INODE);
8300e30db86SJosef Bacik 		return ret;
8310e30db86SJosef Bacik 	}
8320e30db86SJosef Bacik 
833da17066cSJeff Mahoney 	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
8347c0260eeSJeff Mahoney 	if (!fs_info) {
83537b2a7bcSDavid Sterba 		test_std_err(TEST_ALLOC_FS_INFO);
8360e30db86SJosef Bacik 		goto out;
8370e30db86SJosef Bacik 	}
8380e30db86SJosef Bacik 
839da17066cSJeff Mahoney 	root = btrfs_alloc_dummy_root(fs_info);
8407c0260eeSJeff Mahoney 	if (IS_ERR(root)) {
84152ab7bcaSDavid Sterba 		test_std_err(TEST_ALLOC_ROOT);
8420e30db86SJosef Bacik 		goto out;
8430e30db86SJosef Bacik 	}
8440e30db86SJosef Bacik 
845da17066cSJeff Mahoney 	root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
8460e30db86SJosef Bacik 	if (!root->node) {
8479e3d9f84SDavid Sterba 		test_std_err(TEST_ALLOC_ROOT);
8480e30db86SJosef Bacik 		goto out;
8490e30db86SJosef Bacik 	}
8500e30db86SJosef Bacik 
8510e30db86SJosef Bacik 	btrfs_set_header_nritems(root->node, 0);
8520e30db86SJosef Bacik 	btrfs_set_header_level(root->node, 0);
8530e30db86SJosef Bacik 	BTRFS_I(inode)->root = root;
8540e30db86SJosef Bacik 	ret = -EINVAL;
8550e30db86SJosef Bacik 
8560e30db86SJosef Bacik 	/*
8570e30db86SJosef Bacik 	 * Need a blank inode item here just so we don't confuse
8580e30db86SJosef Bacik 	 * btrfs_get_extent.
8590e30db86SJosef Bacik 	 */
8600e30db86SJosef Bacik 	insert_inode_item_key(root);
861b9ef22deSFeifei Xu 	insert_extent(root, sectorsize, sectorsize, sectorsize, 0, sectorsize,
862b9ef22deSFeifei Xu 		      sectorsize, BTRFS_FILE_EXTENT_REG, 0, 1);
86339b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, 2 * sectorsize);
8640e30db86SJosef Bacik 	if (IS_ERR(em)) {
8653c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
8660e30db86SJosef Bacik 		goto out;
8670e30db86SJosef Bacik 	}
8680e30db86SJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
8693c7251f2SDavid Sterba 		test_err("expected a hole, got %llu", em->block_start);
8700e30db86SJosef Bacik 		goto out;
8710e30db86SJosef Bacik 	}
872b9ef22deSFeifei Xu 	if (em->start != 0 || em->len != sectorsize) {
8733c7251f2SDavid Sterba 		test_err(
8743c7251f2SDavid Sterba 	"unexpected extent wanted start 0 len %u, got start %llu len %llu",
875b9ef22deSFeifei Xu 			sectorsize, em->start, em->len);
8760e30db86SJosef Bacik 		goto out;
8770e30db86SJosef Bacik 	}
8780e30db86SJosef Bacik 	if (em->flags != vacancy_only) {
8793c7251f2SDavid Sterba 		test_err("wrong flags, wanted %lu, have %lu", vacancy_only,
8800e30db86SJosef Bacik 			 em->flags);
8810e30db86SJosef Bacik 		goto out;
8820e30db86SJosef Bacik 	}
8830e30db86SJosef Bacik 	free_extent_map(em);
8840e30db86SJosef Bacik 
88539b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, sectorsize, 2 * sectorsize);
8860e30db86SJosef Bacik 	if (IS_ERR(em)) {
8873c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
8880e30db86SJosef Bacik 		goto out;
8890e30db86SJosef Bacik 	}
890b9ef22deSFeifei Xu 	if (em->block_start != sectorsize) {
8913c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
8920e30db86SJosef Bacik 		goto out;
8930e30db86SJosef Bacik 	}
894b9ef22deSFeifei Xu 	if (em->start != sectorsize || em->len != sectorsize) {
8953c7251f2SDavid Sterba 		test_err(
8963c7251f2SDavid Sterba 	"unexpected extent wanted start %u len %u, got start %llu len %llu",
897b9ef22deSFeifei Xu 			sectorsize, sectorsize, em->start, em->len);
8980e30db86SJosef Bacik 		goto out;
8990e30db86SJosef Bacik 	}
9000e30db86SJosef Bacik 	if (em->flags != 0) {
9013c7251f2SDavid Sterba 		test_err("unexpected flags set, wanted 0 got %lu",
9020e30db86SJosef Bacik 			 em->flags);
9030e30db86SJosef Bacik 		goto out;
9040e30db86SJosef Bacik 	}
9050e30db86SJosef Bacik 	ret = 0;
9060e30db86SJosef Bacik out:
9070e30db86SJosef Bacik 	if (!IS_ERR(em))
9080e30db86SJosef Bacik 		free_extent_map(em);
9090e30db86SJosef Bacik 	iput(inode);
910faa2dbf0SJosef Bacik 	btrfs_free_dummy_root(root);
9117c0260eeSJeff Mahoney 	btrfs_free_dummy_fs_info(fs_info);
9120e30db86SJosef Bacik 	return ret;
9130e30db86SJosef Bacik }
9140e30db86SJosef Bacik 
915b9ef22deSFeifei Xu static int test_extent_accounting(u32 sectorsize, u32 nodesize)
9166a3891c5SJosef Bacik {
9177c0260eeSJeff Mahoney 	struct btrfs_fs_info *fs_info = NULL;
9186a3891c5SJosef Bacik 	struct inode *inode = NULL;
9196a3891c5SJosef Bacik 	struct btrfs_root *root = NULL;
9206a3891c5SJosef Bacik 	int ret = -ENOMEM;
9216a3891c5SJosef Bacik 
922e4fa7469SDavid Sterba 	test_msg("running outstanding_extents tests");
923e4fa7469SDavid Sterba 
9246a3891c5SJosef Bacik 	inode = btrfs_new_test_inode();
9256a3891c5SJosef Bacik 	if (!inode) {
9266a060db8SDavid Sterba 		test_std_err(TEST_ALLOC_INODE);
9276a3891c5SJosef Bacik 		return ret;
9286a3891c5SJosef Bacik 	}
9296a3891c5SJosef Bacik 
930da17066cSJeff Mahoney 	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
9317c0260eeSJeff Mahoney 	if (!fs_info) {
93237b2a7bcSDavid Sterba 		test_std_err(TEST_ALLOC_FS_INFO);
9336a3891c5SJosef Bacik 		goto out;
9346a3891c5SJosef Bacik 	}
9356a3891c5SJosef Bacik 
936da17066cSJeff Mahoney 	root = btrfs_alloc_dummy_root(fs_info);
9377c0260eeSJeff Mahoney 	if (IS_ERR(root)) {
93852ab7bcaSDavid Sterba 		test_std_err(TEST_ALLOC_ROOT);
9396a3891c5SJosef Bacik 		goto out;
9406a3891c5SJosef Bacik 	}
9416a3891c5SJosef Bacik 
9426a3891c5SJosef Bacik 	BTRFS_I(inode)->root = root;
9436a3891c5SJosef Bacik 
9446a3891c5SJosef Bacik 	/* [BTRFS_MAX_EXTENT_SIZE] */
945c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode), 0,
946c2566f22SNikolay Borisov 					BTRFS_MAX_EXTENT_SIZE - 1, 0, NULL);
9476a3891c5SJosef Bacik 	if (ret) {
9483c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
9496a3891c5SJosef Bacik 		goto out;
9506a3891c5SJosef Bacik 	}
9516a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 1) {
9526a3891c5SJosef Bacik 		ret = -EINVAL;
9533c7251f2SDavid Sterba 		test_err("miscount, wanted 1, got %u",
9546a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
9556a3891c5SJosef Bacik 		goto out;
9566a3891c5SJosef Bacik 	}
9576a3891c5SJosef Bacik 
958b9ef22deSFeifei Xu 	/* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
959c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode), BTRFS_MAX_EXTENT_SIZE,
960b9ef22deSFeifei Xu 					BTRFS_MAX_EXTENT_SIZE + sectorsize - 1,
961330a5827SNikolay Borisov 					0, NULL);
9626a3891c5SJosef Bacik 	if (ret) {
9633c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
9646a3891c5SJosef Bacik 		goto out;
9656a3891c5SJosef Bacik 	}
9666a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 2) {
9676a3891c5SJosef Bacik 		ret = -EINVAL;
9683c7251f2SDavid Sterba 		test_err("miscount, wanted 2, got %u",
9696a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
9706a3891c5SJosef Bacik 		goto out;
9716a3891c5SJosef Bacik 	}
9726a3891c5SJosef Bacik 
973b9ef22deSFeifei Xu 	/* [BTRFS_MAX_EXTENT_SIZE/2][sectorsize HOLE][the rest] */
9746a3891c5SJosef Bacik 	ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
9756a3891c5SJosef Bacik 			       BTRFS_MAX_EXTENT_SIZE >> 1,
976b9ef22deSFeifei Xu 			       (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1,
977c3347309SFilipe Manana 			       EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
978bd015294SJosef Bacik 			       EXTENT_UPTODATE, NULL);
9796a3891c5SJosef Bacik 	if (ret) {
9803c7251f2SDavid Sterba 		test_err("clear_extent_bit returned %d", ret);
9816a3891c5SJosef Bacik 		goto out;
9826a3891c5SJosef Bacik 	}
9836a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 2) {
9846a3891c5SJosef Bacik 		ret = -EINVAL;
9853c7251f2SDavid Sterba 		test_err("miscount, wanted 2, got %u",
9866a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
9876a3891c5SJosef Bacik 		goto out;
9886a3891c5SJosef Bacik 	}
9896a3891c5SJosef Bacik 
990b9ef22deSFeifei Xu 	/* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
991c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode), BTRFS_MAX_EXTENT_SIZE >> 1,
992b9ef22deSFeifei Xu 					(BTRFS_MAX_EXTENT_SIZE >> 1)
993b9ef22deSFeifei Xu 					+ sectorsize - 1,
994330a5827SNikolay Borisov 					0, NULL);
9956a3891c5SJosef Bacik 	if (ret) {
9963c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
9976a3891c5SJosef Bacik 		goto out;
9986a3891c5SJosef Bacik 	}
9996a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 2) {
10006a3891c5SJosef Bacik 		ret = -EINVAL;
10013c7251f2SDavid Sterba 		test_err("miscount, wanted 2, got %u",
10026a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10036a3891c5SJosef Bacik 		goto out;
10046a3891c5SJosef Bacik 	}
10056a3891c5SJosef Bacik 
10066a3891c5SJosef Bacik 	/*
1007b9ef22deSFeifei Xu 	 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize]
10086a3891c5SJosef Bacik 	 */
1009c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
1010b9ef22deSFeifei Xu 			BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize,
1011b9ef22deSFeifei Xu 			(BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1,
1012330a5827SNikolay Borisov 			0, NULL);
10136a3891c5SJosef Bacik 	if (ret) {
10143c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
10156a3891c5SJosef Bacik 		goto out;
10166a3891c5SJosef Bacik 	}
10176a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 4) {
10186a3891c5SJosef Bacik 		ret = -EINVAL;
10193c7251f2SDavid Sterba 		test_err("miscount, wanted 4, got %u",
10206a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10216a3891c5SJosef Bacik 		goto out;
10226a3891c5SJosef Bacik 	}
10236a3891c5SJosef Bacik 
1024b9ef22deSFeifei Xu 	/*
1025b9ef22deSFeifei Xu 	* [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize]
1026b9ef22deSFeifei Xu 	*/
1027c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
1028b9ef22deSFeifei Xu 			BTRFS_MAX_EXTENT_SIZE + sectorsize,
1029330a5827SNikolay Borisov 			BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL);
10306a3891c5SJosef Bacik 	if (ret) {
10313c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
10326a3891c5SJosef Bacik 		goto out;
10336a3891c5SJosef Bacik 	}
10346a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 3) {
10356a3891c5SJosef Bacik 		ret = -EINVAL;
10363c7251f2SDavid Sterba 		test_err("miscount, wanted 3, got %u",
10376a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10386a3891c5SJosef Bacik 		goto out;
10396a3891c5SJosef Bacik 	}
10406a3891c5SJosef Bacik 
10416a3891c5SJosef Bacik 	/* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */
10426a3891c5SJosef Bacik 	ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
1043b9ef22deSFeifei Xu 			       BTRFS_MAX_EXTENT_SIZE + sectorsize,
1044b9ef22deSFeifei Xu 			       BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1,
1045c3347309SFilipe Manana 			       EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1046bd015294SJosef Bacik 			       EXTENT_UPTODATE, NULL);
10476a3891c5SJosef Bacik 	if (ret) {
10483c7251f2SDavid Sterba 		test_err("clear_extent_bit returned %d", ret);
10496a3891c5SJosef Bacik 		goto out;
10506a3891c5SJosef Bacik 	}
10516a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 4) {
10526a3891c5SJosef Bacik 		ret = -EINVAL;
10533c7251f2SDavid Sterba 		test_err("miscount, wanted 4, got %u",
10546a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10556a3891c5SJosef Bacik 		goto out;
10566a3891c5SJosef Bacik 	}
10576a3891c5SJosef Bacik 
10586a3891c5SJosef Bacik 	/*
10596a3891c5SJosef Bacik 	 * Refill the hole again just for good measure, because I thought it
10606a3891c5SJosef Bacik 	 * might fail and I'd rather satisfy my paranoia at this point.
10616a3891c5SJosef Bacik 	 */
1062c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
1063b9ef22deSFeifei Xu 			BTRFS_MAX_EXTENT_SIZE + sectorsize,
1064330a5827SNikolay Borisov 			BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL);
10656a3891c5SJosef Bacik 	if (ret) {
10663c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
10676a3891c5SJosef Bacik 		goto out;
10686a3891c5SJosef Bacik 	}
10696a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 3) {
10706a3891c5SJosef Bacik 		ret = -EINVAL;
10713c7251f2SDavid Sterba 		test_err("miscount, wanted 3, got %u",
10726a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10736a3891c5SJosef Bacik 		goto out;
10746a3891c5SJosef Bacik 	}
10756a3891c5SJosef Bacik 
10766a3891c5SJosef Bacik 	/* Empty */
10776a3891c5SJosef Bacik 	ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1078c3347309SFilipe Manana 			       EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1079bd015294SJosef Bacik 			       EXTENT_UPTODATE, NULL);
10806a3891c5SJosef Bacik 	if (ret) {
10813c7251f2SDavid Sterba 		test_err("clear_extent_bit returned %d", ret);
10826a3891c5SJosef Bacik 		goto out;
10836a3891c5SJosef Bacik 	}
10846a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents) {
10856a3891c5SJosef Bacik 		ret = -EINVAL;
10863c7251f2SDavid Sterba 		test_err("miscount, wanted 0, got %u",
10876a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10886a3891c5SJosef Bacik 		goto out;
10896a3891c5SJosef Bacik 	}
10906a3891c5SJosef Bacik 	ret = 0;
10916a3891c5SJosef Bacik out:
10926a3891c5SJosef Bacik 	if (ret)
10936a3891c5SJosef Bacik 		clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1094c3347309SFilipe Manana 				 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1095bd015294SJosef Bacik 				 EXTENT_UPTODATE, NULL);
10966a3891c5SJosef Bacik 	iput(inode);
10976a3891c5SJosef Bacik 	btrfs_free_dummy_root(root);
10987c0260eeSJeff Mahoney 	btrfs_free_dummy_fs_info(fs_info);
10996a3891c5SJosef Bacik 	return ret;
11006a3891c5SJosef Bacik }
11016a3891c5SJosef Bacik 
1102b9ef22deSFeifei Xu int btrfs_test_inodes(u32 sectorsize, u32 nodesize)
1103aaedb55bSJosef Bacik {
11040e30db86SJosef Bacik 	int ret;
11050e30db86SJosef Bacik 
1106e4fa7469SDavid Sterba 	test_msg("running inode tests");
1107e4fa7469SDavid Sterba 
11080e30db86SJosef Bacik 	set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
11090e30db86SJosef Bacik 	set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
11100e30db86SJosef Bacik 
1111b9ef22deSFeifei Xu 	ret = test_btrfs_get_extent(sectorsize, nodesize);
11120e30db86SJosef Bacik 	if (ret)
11130e30db86SJosef Bacik 		return ret;
1114b9ef22deSFeifei Xu 	ret = test_hole_first(sectorsize, nodesize);
11156a3891c5SJosef Bacik 	if (ret)
11166a3891c5SJosef Bacik 		return ret;
1117b9ef22deSFeifei Xu 	return test_extent_accounting(sectorsize, nodesize);
1118aaedb55bSJosef Bacik }
1119