xref: /openbmc/linux/fs/btrfs/tests/inode-tests.c (revision 07e81dc9)
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"
14*07e81dc9SJosef Bacik #include "../accessors.h"
15aaedb55bSJosef Bacik 
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 
37f0641656SFilipe Manana 	btrfs_setup_item_for_insert(root, &path, &key, value_len);
38aaedb55bSJosef Bacik 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
39aaedb55bSJosef Bacik 	btrfs_set_file_extent_generation(leaf, fi, 1);
40aaedb55bSJosef Bacik 	btrfs_set_file_extent_type(leaf, fi, type);
41aaedb55bSJosef Bacik 	btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
42aaedb55bSJosef Bacik 	btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_len);
43aaedb55bSJosef Bacik 	btrfs_set_file_extent_offset(leaf, fi, offset);
44aaedb55bSJosef Bacik 	btrfs_set_file_extent_num_bytes(leaf, fi, len);
45aaedb55bSJosef Bacik 	btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
46aaedb55bSJosef Bacik 	btrfs_set_file_extent_compression(leaf, fi, compression);
47aaedb55bSJosef Bacik 	btrfs_set_file_extent_encryption(leaf, fi, 0);
48aaedb55bSJosef Bacik 	btrfs_set_file_extent_other_encoding(leaf, fi, 0);
49aaedb55bSJosef Bacik }
50aaedb55bSJosef Bacik 
510e30db86SJosef Bacik static void insert_inode_item_key(struct btrfs_root *root)
520e30db86SJosef Bacik {
530e30db86SJosef Bacik 	struct btrfs_path path;
540e30db86SJosef Bacik 	struct extent_buffer *leaf = root->node;
550e30db86SJosef Bacik 	struct btrfs_key key;
560e30db86SJosef Bacik 	u32 value_len = 0;
570e30db86SJosef Bacik 
580e30db86SJosef Bacik 	memset(&path, 0, sizeof(path));
590e30db86SJosef Bacik 
600e30db86SJosef Bacik 	path.nodes[0] = leaf;
610e30db86SJosef Bacik 	path.slots[0] = 0;
620e30db86SJosef Bacik 
630e30db86SJosef Bacik 	key.objectid = BTRFS_INODE_ITEM_KEY;
640e30db86SJosef Bacik 	key.type = BTRFS_INODE_ITEM_KEY;
650e30db86SJosef Bacik 	key.offset = 0;
660e30db86SJosef Bacik 
67f0641656SFilipe Manana 	btrfs_setup_item_for_insert(root, &path, &key, value_len);
680e30db86SJosef Bacik }
690e30db86SJosef Bacik 
70aaedb55bSJosef Bacik /*
71aaedb55bSJosef Bacik  * Build the most complicated map of extents the earth has ever seen.  We want
72aaedb55bSJosef Bacik  * this so we can test all of the corner cases of btrfs_get_extent.  Here is a
73aaedb55bSJosef Bacik  * diagram of how the extents will look though this may not be possible we still
74aaedb55bSJosef Bacik  * want to make sure everything acts normally (the last number is not inclusive)
75aaedb55bSJosef Bacik  *
76b9ef22deSFeifei Xu  * [0 - 5][5 -  6][     6 - 4096     ][ 4096 - 4100][4100 - 8195][8195 - 12291]
77b9ef22deSFeifei Xu  * [hole ][inline][hole but no extent][  hole   ][   regular ][regular1 split]
78aaedb55bSJosef Bacik  *
79b9ef22deSFeifei Xu  * [12291 - 16387][16387 - 24579][24579 - 28675][ 28675 - 32771][32771 - 36867 ]
80b9ef22deSFeifei Xu  * [    hole    ][regular1 split][   prealloc ][   prealloc1  ][prealloc1 written]
81aaedb55bSJosef Bacik  *
82b9ef22deSFeifei Xu  * [36867 - 45059][45059 - 53251][53251 - 57347][57347 - 61443][61443- 69635]
83b9ef22deSFeifei Xu  * [  prealloc1  ][ compressed  ][ compressed1 ][    regular  ][ compressed1]
84aaedb55bSJosef Bacik  *
85b9ef22deSFeifei Xu  * [69635-73731][   73731 - 86019   ][86019-90115]
86b9ef22deSFeifei Xu  * [  regular  ][ hole but no extent][  regular  ]
87aaedb55bSJosef Bacik  */
88b9ef22deSFeifei Xu static void setup_file_extents(struct btrfs_root *root, u32 sectorsize)
89aaedb55bSJosef Bacik {
90aaedb55bSJosef Bacik 	int slot = 0;
91ee22184bSByongho Lee 	u64 disk_bytenr = SZ_1M;
92aaedb55bSJosef Bacik 	u64 offset = 0;
93aaedb55bSJosef Bacik 
94aaedb55bSJosef Bacik 	/* First we want a hole */
95aaedb55bSJosef Bacik 	insert_extent(root, offset, 5, 5, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
96aaedb55bSJosef Bacik 		      slot);
97aaedb55bSJosef Bacik 	slot++;
98aaedb55bSJosef Bacik 	offset += 5;
99aaedb55bSJosef Bacik 
100aaedb55bSJosef Bacik 	/*
101aaedb55bSJosef Bacik 	 * Now we want an inline extent, I don't think this is possible but hey
102aaedb55bSJosef Bacik 	 * why not?  Also keep in mind if we have an inline extent it counts as
103aaedb55bSJosef Bacik 	 * the whole first page.  If we were to expand it we would have to cow
104aaedb55bSJosef Bacik 	 * and we wouldn't have an inline extent anymore.
105aaedb55bSJosef Bacik 	 */
106aaedb55bSJosef Bacik 	insert_extent(root, offset, 1, 1, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE, 0,
107aaedb55bSJosef Bacik 		      slot);
108aaedb55bSJosef Bacik 	slot++;
109b9ef22deSFeifei Xu 	offset = sectorsize;
110aaedb55bSJosef Bacik 
111aaedb55bSJosef Bacik 	/* Now another hole */
112aaedb55bSJosef Bacik 	insert_extent(root, offset, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
113aaedb55bSJosef Bacik 		      slot);
114aaedb55bSJosef Bacik 	slot++;
115aaedb55bSJosef Bacik 	offset += 4;
116aaedb55bSJosef Bacik 
117aaedb55bSJosef Bacik 	/* Now for a regular extent */
118b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize - 1, sectorsize - 1, 0,
119b9ef22deSFeifei Xu 		      disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
120aaedb55bSJosef Bacik 	slot++;
121b9ef22deSFeifei Xu 	disk_bytenr += sectorsize;
122b9ef22deSFeifei Xu 	offset += sectorsize - 1;
123aaedb55bSJosef Bacik 
124aaedb55bSJosef Bacik 	/*
125aaedb55bSJosef Bacik 	 * Now for 3 extents that were split from a hole punch so we test
126aaedb55bSJosef Bacik 	 * offsets properly.
127aaedb55bSJosef Bacik 	 */
128b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
129b9ef22deSFeifei Xu 		      4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
130b9ef22deSFeifei Xu 	slot++;
131b9ef22deSFeifei Xu 	offset += sectorsize;
132b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0, 0, 0,
133aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
134aaedb55bSJosef Bacik 	slot++;
135b9ef22deSFeifei Xu 	offset += sectorsize;
136b9ef22deSFeifei Xu 	insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
137b9ef22deSFeifei Xu 		      2 * sectorsize, disk_bytenr, 4 * sectorsize,
138aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
139aaedb55bSJosef Bacik 	slot++;
140b9ef22deSFeifei Xu 	offset += 2 * sectorsize;
141b9ef22deSFeifei Xu 	disk_bytenr += 4 * sectorsize;
142aaedb55bSJosef Bacik 
143aaedb55bSJosef Bacik 	/* Now for a unwritten prealloc extent */
144b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
145b9ef22deSFeifei Xu 		sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
146aaedb55bSJosef Bacik 	slot++;
147b9ef22deSFeifei Xu 	offset += sectorsize;
148aaedb55bSJosef Bacik 
149aaedb55bSJosef Bacik 	/*
150aaedb55bSJosef Bacik 	 * We want to jack up disk_bytenr a little more so the em stuff doesn't
151aaedb55bSJosef Bacik 	 * merge our records.
152aaedb55bSJosef Bacik 	 */
153b9ef22deSFeifei Xu 	disk_bytenr += 2 * sectorsize;
154aaedb55bSJosef Bacik 
155aaedb55bSJosef Bacik 	/*
156aaedb55bSJosef Bacik 	 * Now for a partially written prealloc extent, basically the same as
157aaedb55bSJosef Bacik 	 * the hole punch example above.  Ram_bytes never changes when you mark
158aaedb55bSJosef Bacik 	 * extents written btw.
159aaedb55bSJosef Bacik 	 */
160b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
161b9ef22deSFeifei Xu 		      4 * sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
162b9ef22deSFeifei Xu 	slot++;
163b9ef22deSFeifei Xu 	offset += sectorsize;
164b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, 4 * sectorsize, sectorsize,
165b9ef22deSFeifei Xu 		      disk_bytenr, 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0,
166b9ef22deSFeifei Xu 		      slot);
167b9ef22deSFeifei Xu 	slot++;
168b9ef22deSFeifei Xu 	offset += sectorsize;
169b9ef22deSFeifei Xu 	insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
170b9ef22deSFeifei Xu 		      2 * sectorsize, disk_bytenr, 4 * sectorsize,
171aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
172aaedb55bSJosef Bacik 	slot++;
173b9ef22deSFeifei Xu 	offset += 2 * sectorsize;
174b9ef22deSFeifei Xu 	disk_bytenr += 4 * sectorsize;
175aaedb55bSJosef Bacik 
176aaedb55bSJosef Bacik 	/* Now a normal compressed extent */
177b9ef22deSFeifei Xu 	insert_extent(root, offset, 2 * sectorsize, 2 * sectorsize, 0,
178b9ef22deSFeifei Xu 		      disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG,
179b9ef22deSFeifei Xu 		      BTRFS_COMPRESS_ZLIB, slot);
180aaedb55bSJosef Bacik 	slot++;
181b9ef22deSFeifei Xu 	offset += 2 * sectorsize;
182aaedb55bSJosef Bacik 	/* No merges */
183b9ef22deSFeifei Xu 	disk_bytenr += 2 * sectorsize;
184aaedb55bSJosef Bacik 
185aaedb55bSJosef Bacik 	/* Now a split compressed extent */
186b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
187b9ef22deSFeifei Xu 		      sectorsize, BTRFS_FILE_EXTENT_REG,
188b9ef22deSFeifei Xu 		      BTRFS_COMPRESS_ZLIB, slot);
189aaedb55bSJosef Bacik 	slot++;
190b9ef22deSFeifei Xu 	offset += sectorsize;
191b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0,
192b9ef22deSFeifei Xu 		      disk_bytenr + sectorsize, sectorsize,
193aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
194aaedb55bSJosef Bacik 	slot++;
195b9ef22deSFeifei Xu 	offset += sectorsize;
196b9ef22deSFeifei Xu 	insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
197b9ef22deSFeifei Xu 		      2 * sectorsize, disk_bytenr, sectorsize,
198aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
199aaedb55bSJosef Bacik 	slot++;
200b9ef22deSFeifei Xu 	offset += 2 * sectorsize;
201b9ef22deSFeifei Xu 	disk_bytenr += 2 * sectorsize;
202aaedb55bSJosef Bacik 
203aaedb55bSJosef Bacik 	/* Now extents that have a hole but no hole extent */
204b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
205b9ef22deSFeifei Xu 		      sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
206aaedb55bSJosef Bacik 	slot++;
207b9ef22deSFeifei Xu 	offset += 4 * sectorsize;
208b9ef22deSFeifei Xu 	disk_bytenr += sectorsize;
209b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
210b9ef22deSFeifei Xu 		      sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
211aaedb55bSJosef Bacik }
212aaedb55bSJosef Bacik 
213aaedb55bSJosef Bacik static unsigned long prealloc_only = 0;
214aaedb55bSJosef Bacik static unsigned long compressed_only = 0;
215aaedb55bSJosef Bacik static unsigned long vacancy_only = 0;
216aaedb55bSJosef Bacik 
217b9ef22deSFeifei Xu static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize)
218aaedb55bSJosef Bacik {
2197c0260eeSJeff Mahoney 	struct btrfs_fs_info *fs_info = NULL;
220aaedb55bSJosef Bacik 	struct inode *inode = NULL;
221aaedb55bSJosef Bacik 	struct btrfs_root *root = NULL;
222aaedb55bSJosef Bacik 	struct extent_map *em = NULL;
223aaedb55bSJosef Bacik 	u64 orig_start;
224aaedb55bSJosef Bacik 	u64 disk_bytenr;
225aaedb55bSJosef Bacik 	u64 offset;
226aaedb55bSJosef Bacik 	int ret = -ENOMEM;
227aaedb55bSJosef Bacik 
228e4fa7469SDavid Sterba 	test_msg("running btrfs_get_extent tests");
229e4fa7469SDavid Sterba 
230aaedb55bSJosef Bacik 	inode = btrfs_new_test_inode();
231aaedb55bSJosef Bacik 	if (!inode) {
2326a060db8SDavid Sterba 		test_std_err(TEST_ALLOC_INODE);
233aaedb55bSJosef Bacik 		return ret;
234aaedb55bSJosef Bacik 	}
235aaedb55bSJosef Bacik 
236da17066cSJeff Mahoney 	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
2377c0260eeSJeff Mahoney 	if (!fs_info) {
23837b2a7bcSDavid Sterba 		test_std_err(TEST_ALLOC_FS_INFO);
239aaedb55bSJosef Bacik 		goto out;
240aaedb55bSJosef Bacik 	}
241aaedb55bSJosef Bacik 
242da17066cSJeff Mahoney 	root = btrfs_alloc_dummy_root(fs_info);
2437c0260eeSJeff Mahoney 	if (IS_ERR(root)) {
24452ab7bcaSDavid Sterba 		test_std_err(TEST_ALLOC_ROOT);
245aaedb55bSJosef Bacik 		goto out;
246aaedb55bSJosef Bacik 	}
247aaedb55bSJosef Bacik 
248da17066cSJeff Mahoney 	root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
249aaedb55bSJosef Bacik 	if (!root->node) {
2509e3d9f84SDavid Sterba 		test_std_err(TEST_ALLOC_ROOT);
251aaedb55bSJosef Bacik 		goto out;
252aaedb55bSJosef Bacik 	}
253aaedb55bSJosef Bacik 
254aaedb55bSJosef Bacik 	btrfs_set_header_nritems(root->node, 0);
255aaedb55bSJosef Bacik 	btrfs_set_header_level(root->node, 0);
256aaedb55bSJosef Bacik 	ret = -EINVAL;
257aaedb55bSJosef Bacik 
258aaedb55bSJosef Bacik 	/* First with no extents */
259aaedb55bSJosef Bacik 	BTRFS_I(inode)->root = root;
26039b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, sectorsize);
261aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
262aaedb55bSJosef Bacik 		em = NULL;
2633c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
264aaedb55bSJosef Bacik 		goto out;
265aaedb55bSJosef Bacik 	}
266aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
2673c7251f2SDavid Sterba 		test_err("expected a hole, got %llu", em->block_start);
268aaedb55bSJosef Bacik 		goto out;
269aaedb55bSJosef Bacik 	}
270aaedb55bSJosef Bacik 	free_extent_map(em);
2714c0c8cfcSFilipe Manana 	btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false);
272aaedb55bSJosef Bacik 
273aaedb55bSJosef Bacik 	/*
274aaedb55bSJosef Bacik 	 * All of the magic numbers are based on the mapping setup in
275aaedb55bSJosef Bacik 	 * setup_file_extents, so if you change anything there you need to
276aaedb55bSJosef Bacik 	 * update the comment and update the expected values below.
277aaedb55bSJosef Bacik 	 */
278b9ef22deSFeifei Xu 	setup_file_extents(root, sectorsize);
279aaedb55bSJosef Bacik 
28039b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, (u64)-1);
281aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
2823c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
283aaedb55bSJosef Bacik 		goto out;
284aaedb55bSJosef Bacik 	}
285aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
2863c7251f2SDavid Sterba 		test_err("expected a hole, got %llu", em->block_start);
287aaedb55bSJosef Bacik 		goto out;
288aaedb55bSJosef Bacik 	}
289aaedb55bSJosef Bacik 	if (em->start != 0 || em->len != 5) {
2903c7251f2SDavid Sterba 		test_err(
2913c7251f2SDavid Sterba 		"unexpected extent wanted start 0 len 5, got start %llu len %llu",
2923c7251f2SDavid Sterba 			em->start, em->len);
293aaedb55bSJosef Bacik 		goto out;
294aaedb55bSJosef Bacik 	}
295aaedb55bSJosef Bacik 	if (em->flags != 0) {
2963c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
297aaedb55bSJosef Bacik 		goto out;
298aaedb55bSJosef Bacik 	}
299aaedb55bSJosef Bacik 	offset = em->start + em->len;
300aaedb55bSJosef Bacik 	free_extent_map(em);
301aaedb55bSJosef Bacik 
30239b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
303aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
3043c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
305aaedb55bSJosef Bacik 		goto out;
306aaedb55bSJosef Bacik 	}
307aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_INLINE) {
3083c7251f2SDavid Sterba 		test_err("expected an inline, got %llu", em->block_start);
309aaedb55bSJosef Bacik 		goto out;
310aaedb55bSJosef Bacik 	}
311b9ef22deSFeifei Xu 
312b9ef22deSFeifei Xu 	if (em->start != offset || em->len != (sectorsize - 5)) {
3133c7251f2SDavid Sterba 		test_err(
3143c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len 1, got start %llu len %llu",
3153c7251f2SDavid Sterba 			offset, em->start, em->len);
316aaedb55bSJosef Bacik 		goto out;
317aaedb55bSJosef Bacik 	}
318aaedb55bSJosef Bacik 	if (em->flags != 0) {
3193c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
320aaedb55bSJosef Bacik 		goto out;
321aaedb55bSJosef Bacik 	}
322aaedb55bSJosef Bacik 	/*
323aaedb55bSJosef Bacik 	 * We don't test anything else for inline since it doesn't get set
324aaedb55bSJosef Bacik 	 * unless we have a page for it to write into.  Maybe we should change
325aaedb55bSJosef Bacik 	 * this?
326aaedb55bSJosef Bacik 	 */
327aaedb55bSJosef Bacik 	offset = em->start + em->len;
328aaedb55bSJosef Bacik 	free_extent_map(em);
329aaedb55bSJosef Bacik 
33039b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
331aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
3323c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
333aaedb55bSJosef Bacik 		goto out;
334aaedb55bSJosef Bacik 	}
335aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
3363c7251f2SDavid Sterba 		test_err("expected a hole, got %llu", em->block_start);
337aaedb55bSJosef Bacik 		goto out;
338aaedb55bSJosef Bacik 	}
339aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 4) {
3403c7251f2SDavid Sterba 		test_err(
3413c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len 4, got start %llu len %llu",
3423c7251f2SDavid Sterba 			offset, em->start, em->len);
343aaedb55bSJosef Bacik 		goto out;
344aaedb55bSJosef Bacik 	}
345aaedb55bSJosef Bacik 	if (em->flags != 0) {
3463c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
347aaedb55bSJosef Bacik 		goto out;
348aaedb55bSJosef Bacik 	}
349aaedb55bSJosef Bacik 	offset = em->start + em->len;
350aaedb55bSJosef Bacik 	free_extent_map(em);
351aaedb55bSJosef Bacik 
352aaedb55bSJosef Bacik 	/* Regular extent */
35339b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
354aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
3553c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
356aaedb55bSJosef Bacik 		goto out;
357aaedb55bSJosef Bacik 	}
358aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
3593c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
360aaedb55bSJosef Bacik 		goto out;
361aaedb55bSJosef Bacik 	}
362b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize - 1) {
3633c7251f2SDavid Sterba 		test_err(
3643c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len 4095, got start %llu len %llu",
3653c7251f2SDavid Sterba 			offset, em->start, em->len);
366aaedb55bSJosef Bacik 		goto out;
367aaedb55bSJosef Bacik 	}
368aaedb55bSJosef Bacik 	if (em->flags != 0) {
3693c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
370aaedb55bSJosef Bacik 		goto out;
371aaedb55bSJosef Bacik 	}
372aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
3733c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
374aaedb55bSJosef Bacik 			 em->orig_start);
375aaedb55bSJosef Bacik 		goto out;
376aaedb55bSJosef Bacik 	}
377aaedb55bSJosef Bacik 	offset = em->start + em->len;
378aaedb55bSJosef Bacik 	free_extent_map(em);
379aaedb55bSJosef Bacik 
380aaedb55bSJosef Bacik 	/* The next 3 are split extents */
38139b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
382aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
3833c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
384aaedb55bSJosef Bacik 		goto out;
385aaedb55bSJosef Bacik 	}
386aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
3873c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
388aaedb55bSJosef Bacik 		goto out;
389aaedb55bSJosef Bacik 	}
390b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
3913c7251f2SDavid Sterba 		test_err(
3923c7251f2SDavid Sterba 		"unexpected extent start %llu len %u, got start %llu len %llu",
393b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
394aaedb55bSJosef Bacik 		goto out;
395aaedb55bSJosef Bacik 	}
396aaedb55bSJosef Bacik 	if (em->flags != 0) {
3973c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
398aaedb55bSJosef Bacik 		goto out;
399aaedb55bSJosef Bacik 	}
400aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
4013c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
402aaedb55bSJosef Bacik 			 em->orig_start);
403aaedb55bSJosef Bacik 		goto out;
404aaedb55bSJosef Bacik 	}
405aaedb55bSJosef Bacik 	disk_bytenr = em->block_start;
406aaedb55bSJosef Bacik 	orig_start = em->start;
407aaedb55bSJosef Bacik 	offset = em->start + em->len;
408aaedb55bSJosef Bacik 	free_extent_map(em);
409aaedb55bSJosef Bacik 
41039b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
411aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
4123c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
413aaedb55bSJosef Bacik 		goto out;
414aaedb55bSJosef Bacik 	}
415aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
4163c7251f2SDavid Sterba 		test_err("expected a hole, got %llu", em->block_start);
417aaedb55bSJosef Bacik 		goto out;
418aaedb55bSJosef Bacik 	}
419b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
4203c7251f2SDavid Sterba 		test_err(
4213c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
422b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
423aaedb55bSJosef Bacik 		goto out;
424aaedb55bSJosef Bacik 	}
425aaedb55bSJosef Bacik 	if (em->flags != 0) {
4263c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
427aaedb55bSJosef Bacik 		goto out;
428aaedb55bSJosef Bacik 	}
429aaedb55bSJosef Bacik 	offset = em->start + em->len;
430aaedb55bSJosef Bacik 	free_extent_map(em);
431aaedb55bSJosef Bacik 
43239b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
433aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
4343c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
435aaedb55bSJosef Bacik 		goto out;
436aaedb55bSJosef Bacik 	}
437aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
4383c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
439aaedb55bSJosef Bacik 		goto out;
440aaedb55bSJosef Bacik 	}
441b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 2 * sectorsize) {
4423c7251f2SDavid Sterba 		test_err(
4433c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
444b9ef22deSFeifei Xu 			offset, 2 * sectorsize, em->start, em->len);
445aaedb55bSJosef Bacik 		goto out;
446aaedb55bSJosef Bacik 	}
447aaedb55bSJosef Bacik 	if (em->flags != 0) {
4483c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
449aaedb55bSJosef Bacik 		goto out;
450aaedb55bSJosef Bacik 	}
451aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
4523c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu",
453aaedb55bSJosef Bacik 			 orig_start, em->orig_start);
454aaedb55bSJosef Bacik 		goto out;
455aaedb55bSJosef Bacik 	}
456aaedb55bSJosef Bacik 	disk_bytenr += (em->start - orig_start);
457aaedb55bSJosef Bacik 	if (em->block_start != disk_bytenr) {
4583c7251f2SDavid Sterba 		test_err("wrong block start, want %llu, have %llu",
459aaedb55bSJosef Bacik 			 disk_bytenr, em->block_start);
460aaedb55bSJosef Bacik 		goto out;
461aaedb55bSJosef Bacik 	}
462aaedb55bSJosef Bacik 	offset = em->start + em->len;
463aaedb55bSJosef Bacik 	free_extent_map(em);
464aaedb55bSJosef Bacik 
465aaedb55bSJosef Bacik 	/* Prealloc extent */
46639b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
467aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
4683c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
469aaedb55bSJosef Bacik 		goto out;
470aaedb55bSJosef Bacik 	}
471aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
4723c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
473aaedb55bSJosef Bacik 		goto out;
474aaedb55bSJosef Bacik 	}
475b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
4763c7251f2SDavid Sterba 		test_err(
4773c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
478b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
479aaedb55bSJosef Bacik 		goto out;
480aaedb55bSJosef Bacik 	}
481aaedb55bSJosef Bacik 	if (em->flags != prealloc_only) {
4823c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
483aaedb55bSJosef Bacik 			 prealloc_only, em->flags);
484aaedb55bSJosef Bacik 		goto out;
485aaedb55bSJosef Bacik 	}
486aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
4873c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
488aaedb55bSJosef Bacik 			 em->orig_start);
489aaedb55bSJosef Bacik 		goto out;
490aaedb55bSJosef Bacik 	}
491aaedb55bSJosef Bacik 	offset = em->start + em->len;
492aaedb55bSJosef Bacik 	free_extent_map(em);
493aaedb55bSJosef Bacik 
494aaedb55bSJosef Bacik 	/* The next 3 are a half written prealloc extent */
49539b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
496aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
4973c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
498aaedb55bSJosef Bacik 		goto out;
499aaedb55bSJosef Bacik 	}
500aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
5013c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
502aaedb55bSJosef Bacik 		goto out;
503aaedb55bSJosef Bacik 	}
504b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
5053c7251f2SDavid Sterba 		test_err(
5063c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
507b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
508aaedb55bSJosef Bacik 		goto out;
509aaedb55bSJosef Bacik 	}
510aaedb55bSJosef Bacik 	if (em->flags != prealloc_only) {
5113c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
512aaedb55bSJosef Bacik 			 prealloc_only, em->flags);
513aaedb55bSJosef Bacik 		goto out;
514aaedb55bSJosef Bacik 	}
515aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
5163c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
517aaedb55bSJosef Bacik 			 em->orig_start);
518aaedb55bSJosef Bacik 		goto out;
519aaedb55bSJosef Bacik 	}
520aaedb55bSJosef Bacik 	disk_bytenr = em->block_start;
521aaedb55bSJosef Bacik 	orig_start = em->start;
522aaedb55bSJosef Bacik 	offset = em->start + em->len;
523aaedb55bSJosef Bacik 	free_extent_map(em);
524aaedb55bSJosef Bacik 
52539b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
526aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
5273c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
528aaedb55bSJosef Bacik 		goto out;
529aaedb55bSJosef Bacik 	}
530aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_HOLE) {
5313c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
532aaedb55bSJosef Bacik 		goto out;
533aaedb55bSJosef Bacik 	}
534b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
5353c7251f2SDavid Sterba 		test_err(
5363c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
537b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
538aaedb55bSJosef Bacik 		goto out;
539aaedb55bSJosef Bacik 	}
540aaedb55bSJosef Bacik 	if (em->flags != 0) {
5413c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
542aaedb55bSJosef Bacik 		goto out;
543aaedb55bSJosef Bacik 	}
544aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
5453c7251f2SDavid Sterba 		test_err("unexpected orig offset, wanted %llu, have %llu",
546aaedb55bSJosef Bacik 			 orig_start, em->orig_start);
547aaedb55bSJosef Bacik 		goto out;
548aaedb55bSJosef Bacik 	}
549aaedb55bSJosef Bacik 	if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
5503c7251f2SDavid Sterba 		test_err("unexpected block start, wanted %llu, have %llu",
551aaedb55bSJosef Bacik 			 disk_bytenr + (em->start - em->orig_start),
552aaedb55bSJosef Bacik 			 em->block_start);
553aaedb55bSJosef Bacik 		goto out;
554aaedb55bSJosef Bacik 	}
555aaedb55bSJosef Bacik 	offset = em->start + em->len;
556aaedb55bSJosef Bacik 	free_extent_map(em);
557aaedb55bSJosef Bacik 
55839b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
559aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
5603c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
561aaedb55bSJosef Bacik 		goto out;
562aaedb55bSJosef Bacik 	}
563aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
5643c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
565aaedb55bSJosef Bacik 		goto out;
566aaedb55bSJosef Bacik 	}
567b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 2 * sectorsize) {
5683c7251f2SDavid Sterba 		test_err(
5693c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
570b9ef22deSFeifei Xu 			offset, 2 * sectorsize, em->start, em->len);
571aaedb55bSJosef Bacik 		goto out;
572aaedb55bSJosef Bacik 	}
573aaedb55bSJosef Bacik 	if (em->flags != prealloc_only) {
5743c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
575aaedb55bSJosef Bacik 			 prealloc_only, em->flags);
576aaedb55bSJosef Bacik 		goto out;
577aaedb55bSJosef Bacik 	}
578aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
5793c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", orig_start,
580aaedb55bSJosef Bacik 			 em->orig_start);
581aaedb55bSJosef Bacik 		goto out;
582aaedb55bSJosef Bacik 	}
583aaedb55bSJosef Bacik 	if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
5843c7251f2SDavid Sterba 		test_err("unexpected block start, wanted %llu, have %llu",
585aaedb55bSJosef Bacik 			 disk_bytenr + (em->start - em->orig_start),
586aaedb55bSJosef Bacik 			 em->block_start);
587aaedb55bSJosef Bacik 		goto out;
588aaedb55bSJosef Bacik 	}
589aaedb55bSJosef Bacik 	offset = em->start + em->len;
590aaedb55bSJosef Bacik 	free_extent_map(em);
591aaedb55bSJosef Bacik 
592aaedb55bSJosef Bacik 	/* Now for the compressed extent */
59339b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
594aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
5953c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
596aaedb55bSJosef Bacik 		goto out;
597aaedb55bSJosef Bacik 	}
598aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
5993c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
600aaedb55bSJosef Bacik 		goto out;
601aaedb55bSJosef Bacik 	}
602b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 2 * sectorsize) {
6033c7251f2SDavid Sterba 		test_err(
6043c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
605b9ef22deSFeifei Xu 			offset, 2 * sectorsize, em->start, em->len);
606aaedb55bSJosef Bacik 		goto out;
607aaedb55bSJosef Bacik 	}
608aaedb55bSJosef Bacik 	if (em->flags != compressed_only) {
6093c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
610aaedb55bSJosef Bacik 			 compressed_only, em->flags);
611aaedb55bSJosef Bacik 		goto out;
612aaedb55bSJosef Bacik 	}
613aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
6143c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu",
615aaedb55bSJosef Bacik 			 em->start, em->orig_start);
616aaedb55bSJosef Bacik 		goto out;
617aaedb55bSJosef Bacik 	}
618aaedb55bSJosef Bacik 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
6193c7251f2SDavid Sterba 		test_err("unexpected compress type, wanted %d, got %d",
620aaedb55bSJosef Bacik 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
621aaedb55bSJosef Bacik 		goto out;
622aaedb55bSJosef Bacik 	}
623aaedb55bSJosef Bacik 	offset = em->start + em->len;
624aaedb55bSJosef Bacik 	free_extent_map(em);
625aaedb55bSJosef Bacik 
626aaedb55bSJosef Bacik 	/* Split compressed extent */
62739b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
628aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
6293c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
630aaedb55bSJosef Bacik 		goto out;
631aaedb55bSJosef Bacik 	}
632aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
6333c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
634aaedb55bSJosef Bacik 		goto out;
635aaedb55bSJosef Bacik 	}
636b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
6373c7251f2SDavid Sterba 		test_err(
6383c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
639b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
640aaedb55bSJosef Bacik 		goto out;
641aaedb55bSJosef Bacik 	}
642aaedb55bSJosef Bacik 	if (em->flags != compressed_only) {
6433c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
644aaedb55bSJosef Bacik 			 compressed_only, em->flags);
645aaedb55bSJosef Bacik 		goto out;
646aaedb55bSJosef Bacik 	}
647aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
6483c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu",
649aaedb55bSJosef Bacik 			 em->start, em->orig_start);
650aaedb55bSJosef Bacik 		goto out;
651aaedb55bSJosef Bacik 	}
652aaedb55bSJosef Bacik 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
6533c7251f2SDavid Sterba 		test_err("unexpected compress type, wanted %d, got %d",
654aaedb55bSJosef Bacik 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
655aaedb55bSJosef Bacik 		goto out;
656aaedb55bSJosef Bacik 	}
657aaedb55bSJosef Bacik 	disk_bytenr = em->block_start;
658aaedb55bSJosef Bacik 	orig_start = em->start;
659aaedb55bSJosef Bacik 	offset = em->start + em->len;
660aaedb55bSJosef Bacik 	free_extent_map(em);
661aaedb55bSJosef Bacik 
66239b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
663aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
6643c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
665aaedb55bSJosef Bacik 		goto out;
666aaedb55bSJosef Bacik 	}
667aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
6683c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
669aaedb55bSJosef Bacik 		goto out;
670aaedb55bSJosef Bacik 	}
671b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
6723c7251f2SDavid Sterba 		test_err(
6733c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
674b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
675aaedb55bSJosef Bacik 		goto out;
676aaedb55bSJosef Bacik 	}
677aaedb55bSJosef Bacik 	if (em->flags != 0) {
6783c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
679aaedb55bSJosef Bacik 		goto out;
680aaedb55bSJosef Bacik 	}
681aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
6823c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
683aaedb55bSJosef Bacik 			 em->orig_start);
684aaedb55bSJosef Bacik 		goto out;
685aaedb55bSJosef Bacik 	}
686aaedb55bSJosef Bacik 	offset = em->start + em->len;
687aaedb55bSJosef Bacik 	free_extent_map(em);
688aaedb55bSJosef Bacik 
68939b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
690aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
6913c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
692aaedb55bSJosef Bacik 		goto out;
693aaedb55bSJosef Bacik 	}
694aaedb55bSJosef Bacik 	if (em->block_start != disk_bytenr) {
6953c7251f2SDavid Sterba 		test_err("block start does not match, want %llu got %llu",
696aaedb55bSJosef Bacik 			 disk_bytenr, em->block_start);
697aaedb55bSJosef Bacik 		goto out;
698aaedb55bSJosef Bacik 	}
699b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 2 * sectorsize) {
7003c7251f2SDavid Sterba 		test_err(
7013c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
702b9ef22deSFeifei Xu 			offset, 2 * sectorsize, em->start, em->len);
703aaedb55bSJosef Bacik 		goto out;
704aaedb55bSJosef Bacik 	}
705aaedb55bSJosef Bacik 	if (em->flags != compressed_only) {
7063c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
707aaedb55bSJosef Bacik 			 compressed_only, em->flags);
708aaedb55bSJosef Bacik 		goto out;
709aaedb55bSJosef Bacik 	}
710aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
7113c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu",
712aaedb55bSJosef Bacik 			 em->start, orig_start);
713aaedb55bSJosef Bacik 		goto out;
714aaedb55bSJosef Bacik 	}
715aaedb55bSJosef Bacik 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
7163c7251f2SDavid Sterba 		test_err("unexpected compress type, wanted %d, got %d",
717aaedb55bSJosef Bacik 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
718aaedb55bSJosef Bacik 		goto out;
719aaedb55bSJosef Bacik 	}
720aaedb55bSJosef Bacik 	offset = em->start + em->len;
721aaedb55bSJosef Bacik 	free_extent_map(em);
722aaedb55bSJosef Bacik 
723aaedb55bSJosef Bacik 	/* A hole between regular extents but no hole extent */
72439b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset + 6, sectorsize);
725aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
7263c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
727aaedb55bSJosef Bacik 		goto out;
728aaedb55bSJosef Bacik 	}
729aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
7303c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
731aaedb55bSJosef Bacik 		goto out;
732aaedb55bSJosef Bacik 	}
733b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
7343c7251f2SDavid Sterba 		test_err(
7353c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
736b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
737aaedb55bSJosef Bacik 		goto out;
738aaedb55bSJosef Bacik 	}
739aaedb55bSJosef Bacik 	if (em->flags != 0) {
7403c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
741aaedb55bSJosef Bacik 		goto out;
742aaedb55bSJosef Bacik 	}
743aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
7443c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
745aaedb55bSJosef Bacik 			 em->orig_start);
746aaedb55bSJosef Bacik 		goto out;
747aaedb55bSJosef Bacik 	}
748aaedb55bSJosef Bacik 	offset = em->start + em->len;
749aaedb55bSJosef Bacik 	free_extent_map(em);
750aaedb55bSJosef Bacik 
75139b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, SZ_4M);
752aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
7533c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
754aaedb55bSJosef Bacik 		goto out;
755aaedb55bSJosef Bacik 	}
756aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
7573c7251f2SDavid Sterba 		test_err("expected a hole extent, got %llu", em->block_start);
758aaedb55bSJosef Bacik 		goto out;
759aaedb55bSJosef Bacik 	}
760aaedb55bSJosef Bacik 	/*
761aaedb55bSJosef Bacik 	 * Currently we just return a length that we requested rather than the
762aaedb55bSJosef Bacik 	 * length of the actual hole, if this changes we'll have to change this
763aaedb55bSJosef Bacik 	 * test.
764aaedb55bSJosef Bacik 	 */
765b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 3 * sectorsize) {
7663c7251f2SDavid Sterba 		test_err(
7673c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
768b9ef22deSFeifei Xu 			offset, 3 * sectorsize, em->start, em->len);
769aaedb55bSJosef Bacik 		goto out;
770aaedb55bSJosef Bacik 	}
771aaedb55bSJosef Bacik 	if (em->flags != vacancy_only) {
7723c7251f2SDavid Sterba 		test_err("unexpected flags set, want %lu have %lu",
773aaedb55bSJosef Bacik 			 vacancy_only, em->flags);
774aaedb55bSJosef Bacik 		goto out;
775aaedb55bSJosef Bacik 	}
776aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
7773c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
778aaedb55bSJosef Bacik 			 em->orig_start);
779aaedb55bSJosef Bacik 		goto out;
780aaedb55bSJosef Bacik 	}
781aaedb55bSJosef Bacik 	offset = em->start + em->len;
782aaedb55bSJosef Bacik 	free_extent_map(em);
783aaedb55bSJosef Bacik 
78439b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize);
785aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
7863c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
787aaedb55bSJosef Bacik 		goto out;
788aaedb55bSJosef Bacik 	}
789aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
7903c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
791aaedb55bSJosef Bacik 		goto out;
792aaedb55bSJosef Bacik 	}
793b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
7943c7251f2SDavid Sterba 		test_err(
7953c7251f2SDavid Sterba 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
796b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
797aaedb55bSJosef Bacik 		goto out;
798aaedb55bSJosef Bacik 	}
799aaedb55bSJosef Bacik 	if (em->flags != 0) {
8003c7251f2SDavid Sterba 		test_err("unexpected flags set, want 0 have %lu", em->flags);
801aaedb55bSJosef Bacik 		goto out;
802aaedb55bSJosef Bacik 	}
803aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
8043c7251f2SDavid Sterba 		test_err("wrong orig offset, want %llu, have %llu", em->start,
805aaedb55bSJosef Bacik 			 em->orig_start);
806aaedb55bSJosef Bacik 		goto out;
807aaedb55bSJosef Bacik 	}
808aaedb55bSJosef Bacik 	ret = 0;
809aaedb55bSJosef Bacik out:
810aaedb55bSJosef Bacik 	if (!IS_ERR(em))
811aaedb55bSJosef Bacik 		free_extent_map(em);
812aaedb55bSJosef Bacik 	iput(inode);
813faa2dbf0SJosef Bacik 	btrfs_free_dummy_root(root);
8147c0260eeSJeff Mahoney 	btrfs_free_dummy_fs_info(fs_info);
815aaedb55bSJosef Bacik 	return ret;
816aaedb55bSJosef Bacik }
817aaedb55bSJosef Bacik 
818b9ef22deSFeifei Xu static int test_hole_first(u32 sectorsize, u32 nodesize)
8190e30db86SJosef Bacik {
8207c0260eeSJeff Mahoney 	struct btrfs_fs_info *fs_info = NULL;
8210e30db86SJosef Bacik 	struct inode *inode = NULL;
8220e30db86SJosef Bacik 	struct btrfs_root *root = NULL;
8230e30db86SJosef Bacik 	struct extent_map *em = NULL;
8240e30db86SJosef Bacik 	int ret = -ENOMEM;
8250e30db86SJosef Bacik 
826e4fa7469SDavid Sterba 	test_msg("running hole first btrfs_get_extent test");
827e4fa7469SDavid Sterba 
8280e30db86SJosef Bacik 	inode = btrfs_new_test_inode();
8290e30db86SJosef Bacik 	if (!inode) {
8306a060db8SDavid Sterba 		test_std_err(TEST_ALLOC_INODE);
8310e30db86SJosef Bacik 		return ret;
8320e30db86SJosef Bacik 	}
8330e30db86SJosef Bacik 
834da17066cSJeff Mahoney 	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
8357c0260eeSJeff Mahoney 	if (!fs_info) {
83637b2a7bcSDavid Sterba 		test_std_err(TEST_ALLOC_FS_INFO);
8370e30db86SJosef Bacik 		goto out;
8380e30db86SJosef Bacik 	}
8390e30db86SJosef Bacik 
840da17066cSJeff Mahoney 	root = btrfs_alloc_dummy_root(fs_info);
8417c0260eeSJeff Mahoney 	if (IS_ERR(root)) {
84252ab7bcaSDavid Sterba 		test_std_err(TEST_ALLOC_ROOT);
8430e30db86SJosef Bacik 		goto out;
8440e30db86SJosef Bacik 	}
8450e30db86SJosef Bacik 
846da17066cSJeff Mahoney 	root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
8470e30db86SJosef Bacik 	if (!root->node) {
8489e3d9f84SDavid Sterba 		test_std_err(TEST_ALLOC_ROOT);
8490e30db86SJosef Bacik 		goto out;
8500e30db86SJosef Bacik 	}
8510e30db86SJosef Bacik 
8520e30db86SJosef Bacik 	btrfs_set_header_nritems(root->node, 0);
8530e30db86SJosef Bacik 	btrfs_set_header_level(root->node, 0);
8540e30db86SJosef Bacik 	BTRFS_I(inode)->root = root;
8550e30db86SJosef Bacik 	ret = -EINVAL;
8560e30db86SJosef Bacik 
8570e30db86SJosef Bacik 	/*
8580e30db86SJosef Bacik 	 * Need a blank inode item here just so we don't confuse
8590e30db86SJosef Bacik 	 * btrfs_get_extent.
8600e30db86SJosef Bacik 	 */
8610e30db86SJosef Bacik 	insert_inode_item_key(root);
862b9ef22deSFeifei Xu 	insert_extent(root, sectorsize, sectorsize, sectorsize, 0, sectorsize,
863b9ef22deSFeifei Xu 		      sectorsize, BTRFS_FILE_EXTENT_REG, 0, 1);
86439b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, 2 * sectorsize);
8650e30db86SJosef Bacik 	if (IS_ERR(em)) {
8663c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
8670e30db86SJosef Bacik 		goto out;
8680e30db86SJosef Bacik 	}
8690e30db86SJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
8703c7251f2SDavid Sterba 		test_err("expected a hole, got %llu", em->block_start);
8710e30db86SJosef Bacik 		goto out;
8720e30db86SJosef Bacik 	}
873b9ef22deSFeifei Xu 	if (em->start != 0 || em->len != sectorsize) {
8743c7251f2SDavid Sterba 		test_err(
8753c7251f2SDavid Sterba 	"unexpected extent wanted start 0 len %u, got start %llu len %llu",
876b9ef22deSFeifei Xu 			sectorsize, em->start, em->len);
8770e30db86SJosef Bacik 		goto out;
8780e30db86SJosef Bacik 	}
8790e30db86SJosef Bacik 	if (em->flags != vacancy_only) {
8803c7251f2SDavid Sterba 		test_err("wrong flags, wanted %lu, have %lu", vacancy_only,
8810e30db86SJosef Bacik 			 em->flags);
8820e30db86SJosef Bacik 		goto out;
8830e30db86SJosef Bacik 	}
8840e30db86SJosef Bacik 	free_extent_map(em);
8850e30db86SJosef Bacik 
88639b07b5dSOmar Sandoval 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, sectorsize, 2 * sectorsize);
8870e30db86SJosef Bacik 	if (IS_ERR(em)) {
8883c7251f2SDavid Sterba 		test_err("got an error when we shouldn't have");
8890e30db86SJosef Bacik 		goto out;
8900e30db86SJosef Bacik 	}
891b9ef22deSFeifei Xu 	if (em->block_start != sectorsize) {
8923c7251f2SDavid Sterba 		test_err("expected a real extent, got %llu", em->block_start);
8930e30db86SJosef Bacik 		goto out;
8940e30db86SJosef Bacik 	}
895b9ef22deSFeifei Xu 	if (em->start != sectorsize || em->len != sectorsize) {
8963c7251f2SDavid Sterba 		test_err(
8973c7251f2SDavid Sterba 	"unexpected extent wanted start %u len %u, got start %llu len %llu",
898b9ef22deSFeifei Xu 			sectorsize, sectorsize, em->start, em->len);
8990e30db86SJosef Bacik 		goto out;
9000e30db86SJosef Bacik 	}
9010e30db86SJosef Bacik 	if (em->flags != 0) {
9023c7251f2SDavid Sterba 		test_err("unexpected flags set, wanted 0 got %lu",
9030e30db86SJosef Bacik 			 em->flags);
9040e30db86SJosef Bacik 		goto out;
9050e30db86SJosef Bacik 	}
9060e30db86SJosef Bacik 	ret = 0;
9070e30db86SJosef Bacik out:
9080e30db86SJosef Bacik 	if (!IS_ERR(em))
9090e30db86SJosef Bacik 		free_extent_map(em);
9100e30db86SJosef Bacik 	iput(inode);
911faa2dbf0SJosef Bacik 	btrfs_free_dummy_root(root);
9127c0260eeSJeff Mahoney 	btrfs_free_dummy_fs_info(fs_info);
9130e30db86SJosef Bacik 	return ret;
9140e30db86SJosef Bacik }
9150e30db86SJosef Bacik 
916b9ef22deSFeifei Xu static int test_extent_accounting(u32 sectorsize, u32 nodesize)
9176a3891c5SJosef Bacik {
9187c0260eeSJeff Mahoney 	struct btrfs_fs_info *fs_info = NULL;
9196a3891c5SJosef Bacik 	struct inode *inode = NULL;
9206a3891c5SJosef Bacik 	struct btrfs_root *root = NULL;
9216a3891c5SJosef Bacik 	int ret = -ENOMEM;
9226a3891c5SJosef Bacik 
923e4fa7469SDavid Sterba 	test_msg("running outstanding_extents tests");
924e4fa7469SDavid Sterba 
9256a3891c5SJosef Bacik 	inode = btrfs_new_test_inode();
9266a3891c5SJosef Bacik 	if (!inode) {
9276a060db8SDavid Sterba 		test_std_err(TEST_ALLOC_INODE);
9286a3891c5SJosef Bacik 		return ret;
9296a3891c5SJosef Bacik 	}
9306a3891c5SJosef Bacik 
931da17066cSJeff Mahoney 	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
9327c0260eeSJeff Mahoney 	if (!fs_info) {
93337b2a7bcSDavid Sterba 		test_std_err(TEST_ALLOC_FS_INFO);
9346a3891c5SJosef Bacik 		goto out;
9356a3891c5SJosef Bacik 	}
9366a3891c5SJosef Bacik 
937da17066cSJeff Mahoney 	root = btrfs_alloc_dummy_root(fs_info);
9387c0260eeSJeff Mahoney 	if (IS_ERR(root)) {
93952ab7bcaSDavid Sterba 		test_std_err(TEST_ALLOC_ROOT);
9406a3891c5SJosef Bacik 		goto out;
9416a3891c5SJosef Bacik 	}
9426a3891c5SJosef Bacik 
9436a3891c5SJosef Bacik 	BTRFS_I(inode)->root = root;
9446a3891c5SJosef Bacik 
9456a3891c5SJosef Bacik 	/* [BTRFS_MAX_EXTENT_SIZE] */
946c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode), 0,
947c2566f22SNikolay Borisov 					BTRFS_MAX_EXTENT_SIZE - 1, 0, NULL);
9486a3891c5SJosef Bacik 	if (ret) {
9493c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
9506a3891c5SJosef Bacik 		goto out;
9516a3891c5SJosef Bacik 	}
9526a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 1) {
9536a3891c5SJosef Bacik 		ret = -EINVAL;
9543c7251f2SDavid Sterba 		test_err("miscount, wanted 1, got %u",
9556a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
9566a3891c5SJosef Bacik 		goto out;
9576a3891c5SJosef Bacik 	}
9586a3891c5SJosef Bacik 
959b9ef22deSFeifei Xu 	/* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
960c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode), BTRFS_MAX_EXTENT_SIZE,
961b9ef22deSFeifei Xu 					BTRFS_MAX_EXTENT_SIZE + sectorsize - 1,
962330a5827SNikolay Borisov 					0, NULL);
9636a3891c5SJosef Bacik 	if (ret) {
9643c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
9656a3891c5SJosef Bacik 		goto out;
9666a3891c5SJosef Bacik 	}
9676a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 2) {
9686a3891c5SJosef Bacik 		ret = -EINVAL;
9693c7251f2SDavid Sterba 		test_err("miscount, wanted 2, got %u",
9706a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
9716a3891c5SJosef Bacik 		goto out;
9726a3891c5SJosef Bacik 	}
9736a3891c5SJosef Bacik 
974b9ef22deSFeifei Xu 	/* [BTRFS_MAX_EXTENT_SIZE/2][sectorsize HOLE][the rest] */
9756a3891c5SJosef Bacik 	ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
9766a3891c5SJosef Bacik 			       BTRFS_MAX_EXTENT_SIZE >> 1,
977b9ef22deSFeifei Xu 			       (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1,
978c3347309SFilipe Manana 			       EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
979bd015294SJosef Bacik 			       EXTENT_UPTODATE, NULL);
9806a3891c5SJosef Bacik 	if (ret) {
9813c7251f2SDavid Sterba 		test_err("clear_extent_bit returned %d", ret);
9826a3891c5SJosef Bacik 		goto out;
9836a3891c5SJosef Bacik 	}
9846a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 2) {
9856a3891c5SJosef Bacik 		ret = -EINVAL;
9863c7251f2SDavid Sterba 		test_err("miscount, wanted 2, got %u",
9876a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
9886a3891c5SJosef Bacik 		goto out;
9896a3891c5SJosef Bacik 	}
9906a3891c5SJosef Bacik 
991b9ef22deSFeifei Xu 	/* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
992c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode), BTRFS_MAX_EXTENT_SIZE >> 1,
993b9ef22deSFeifei Xu 					(BTRFS_MAX_EXTENT_SIZE >> 1)
994b9ef22deSFeifei Xu 					+ sectorsize - 1,
995330a5827SNikolay Borisov 					0, NULL);
9966a3891c5SJosef Bacik 	if (ret) {
9973c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
9986a3891c5SJosef Bacik 		goto out;
9996a3891c5SJosef Bacik 	}
10006a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 2) {
10016a3891c5SJosef Bacik 		ret = -EINVAL;
10023c7251f2SDavid Sterba 		test_err("miscount, wanted 2, got %u",
10036a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10046a3891c5SJosef Bacik 		goto out;
10056a3891c5SJosef Bacik 	}
10066a3891c5SJosef Bacik 
10076a3891c5SJosef Bacik 	/*
1008b9ef22deSFeifei Xu 	 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize]
10096a3891c5SJosef Bacik 	 */
1010c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
1011b9ef22deSFeifei Xu 			BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize,
1012b9ef22deSFeifei Xu 			(BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1,
1013330a5827SNikolay Borisov 			0, NULL);
10146a3891c5SJosef Bacik 	if (ret) {
10153c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
10166a3891c5SJosef Bacik 		goto out;
10176a3891c5SJosef Bacik 	}
10186a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 4) {
10196a3891c5SJosef Bacik 		ret = -EINVAL;
10203c7251f2SDavid Sterba 		test_err("miscount, wanted 4, got %u",
10216a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10226a3891c5SJosef Bacik 		goto out;
10236a3891c5SJosef Bacik 	}
10246a3891c5SJosef Bacik 
1025b9ef22deSFeifei Xu 	/*
1026b9ef22deSFeifei Xu 	* [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize]
1027b9ef22deSFeifei Xu 	*/
1028c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
1029b9ef22deSFeifei Xu 			BTRFS_MAX_EXTENT_SIZE + sectorsize,
1030330a5827SNikolay Borisov 			BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL);
10316a3891c5SJosef Bacik 	if (ret) {
10323c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
10336a3891c5SJosef Bacik 		goto out;
10346a3891c5SJosef Bacik 	}
10356a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 3) {
10366a3891c5SJosef Bacik 		ret = -EINVAL;
10373c7251f2SDavid Sterba 		test_err("miscount, wanted 3, got %u",
10386a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10396a3891c5SJosef Bacik 		goto out;
10406a3891c5SJosef Bacik 	}
10416a3891c5SJosef Bacik 
10426a3891c5SJosef Bacik 	/* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */
10436a3891c5SJosef Bacik 	ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
1044b9ef22deSFeifei Xu 			       BTRFS_MAX_EXTENT_SIZE + sectorsize,
1045b9ef22deSFeifei Xu 			       BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1,
1046c3347309SFilipe Manana 			       EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1047bd015294SJosef Bacik 			       EXTENT_UPTODATE, NULL);
10486a3891c5SJosef Bacik 	if (ret) {
10493c7251f2SDavid Sterba 		test_err("clear_extent_bit returned %d", ret);
10506a3891c5SJosef Bacik 		goto out;
10516a3891c5SJosef Bacik 	}
10526a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 4) {
10536a3891c5SJosef Bacik 		ret = -EINVAL;
10543c7251f2SDavid Sterba 		test_err("miscount, wanted 4, got %u",
10556a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10566a3891c5SJosef Bacik 		goto out;
10576a3891c5SJosef Bacik 	}
10586a3891c5SJosef Bacik 
10596a3891c5SJosef Bacik 	/*
10606a3891c5SJosef Bacik 	 * Refill the hole again just for good measure, because I thought it
10616a3891c5SJosef Bacik 	 * might fail and I'd rather satisfy my paranoia at this point.
10626a3891c5SJosef Bacik 	 */
1063c2566f22SNikolay Borisov 	ret = btrfs_set_extent_delalloc(BTRFS_I(inode),
1064b9ef22deSFeifei Xu 			BTRFS_MAX_EXTENT_SIZE + sectorsize,
1065330a5827SNikolay Borisov 			BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL);
10666a3891c5SJosef Bacik 	if (ret) {
10673c7251f2SDavid Sterba 		test_err("btrfs_set_extent_delalloc returned %d", ret);
10686a3891c5SJosef Bacik 		goto out;
10696a3891c5SJosef Bacik 	}
10706a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 3) {
10716a3891c5SJosef Bacik 		ret = -EINVAL;
10723c7251f2SDavid Sterba 		test_err("miscount, wanted 3, got %u",
10736a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10746a3891c5SJosef Bacik 		goto out;
10756a3891c5SJosef Bacik 	}
10766a3891c5SJosef Bacik 
10776a3891c5SJosef Bacik 	/* Empty */
10786a3891c5SJosef Bacik 	ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1079c3347309SFilipe Manana 			       EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1080bd015294SJosef Bacik 			       EXTENT_UPTODATE, NULL);
10816a3891c5SJosef Bacik 	if (ret) {
10823c7251f2SDavid Sterba 		test_err("clear_extent_bit returned %d", ret);
10836a3891c5SJosef Bacik 		goto out;
10846a3891c5SJosef Bacik 	}
10856a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents) {
10866a3891c5SJosef Bacik 		ret = -EINVAL;
10873c7251f2SDavid Sterba 		test_err("miscount, wanted 0, got %u",
10886a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10896a3891c5SJosef Bacik 		goto out;
10906a3891c5SJosef Bacik 	}
10916a3891c5SJosef Bacik 	ret = 0;
10926a3891c5SJosef Bacik out:
10936a3891c5SJosef Bacik 	if (ret)
10946a3891c5SJosef Bacik 		clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1095c3347309SFilipe Manana 				 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1096bd015294SJosef Bacik 				 EXTENT_UPTODATE, NULL);
10976a3891c5SJosef Bacik 	iput(inode);
10986a3891c5SJosef Bacik 	btrfs_free_dummy_root(root);
10997c0260eeSJeff Mahoney 	btrfs_free_dummy_fs_info(fs_info);
11006a3891c5SJosef Bacik 	return ret;
11016a3891c5SJosef Bacik }
11026a3891c5SJosef Bacik 
1103b9ef22deSFeifei Xu int btrfs_test_inodes(u32 sectorsize, u32 nodesize)
1104aaedb55bSJosef Bacik {
11050e30db86SJosef Bacik 	int ret;
11060e30db86SJosef Bacik 
1107e4fa7469SDavid Sterba 	test_msg("running inode tests");
1108e4fa7469SDavid Sterba 
11090e30db86SJosef Bacik 	set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
11100e30db86SJosef Bacik 	set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
11110e30db86SJosef Bacik 
1112b9ef22deSFeifei Xu 	ret = test_btrfs_get_extent(sectorsize, nodesize);
11130e30db86SJosef Bacik 	if (ret)
11140e30db86SJosef Bacik 		return ret;
1115b9ef22deSFeifei Xu 	ret = test_hole_first(sectorsize, nodesize);
11166a3891c5SJosef Bacik 	if (ret)
11176a3891c5SJosef Bacik 		return ret;
1118b9ef22deSFeifei Xu 	return test_extent_accounting(sectorsize, nodesize);
1119aaedb55bSJosef Bacik }
1120