xref: /openbmc/linux/fs/btrfs/tests/inode-tests.c (revision 7c0260ee)
1aaedb55bSJosef Bacik /*
2aaedb55bSJosef Bacik  * Copyright (C) 2013 Fusion IO.  All rights reserved.
3aaedb55bSJosef Bacik  *
4aaedb55bSJosef Bacik  * This program is free software; you can redistribute it and/or
5aaedb55bSJosef Bacik  * modify it under the terms of the GNU General Public
6aaedb55bSJosef Bacik  * License v2 as published by the Free Software Foundation.
7aaedb55bSJosef Bacik  *
8aaedb55bSJosef Bacik  * This program is distributed in the hope that it will be useful,
9aaedb55bSJosef Bacik  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10aaedb55bSJosef Bacik  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11aaedb55bSJosef Bacik  * General Public License for more details.
12aaedb55bSJosef Bacik  *
13aaedb55bSJosef Bacik  * You should have received a copy of the GNU General Public
14aaedb55bSJosef Bacik  * License along with this program; if not, write to the
15aaedb55bSJosef Bacik  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16aaedb55bSJosef Bacik  * Boston, MA 021110-1307, USA.
17aaedb55bSJosef Bacik  */
18aaedb55bSJosef Bacik 
19b9ef22deSFeifei Xu #include <linux/types.h>
20aaedb55bSJosef Bacik #include "btrfs-tests.h"
21aaedb55bSJosef Bacik #include "../ctree.h"
22aaedb55bSJosef Bacik #include "../btrfs_inode.h"
23aaedb55bSJosef Bacik #include "../disk-io.h"
24aaedb55bSJosef Bacik #include "../extent_io.h"
25aaedb55bSJosef Bacik #include "../volumes.h"
26ebb8765bSAnand Jain #include "../compression.h"
27aaedb55bSJosef Bacik 
28aaedb55bSJosef Bacik static void insert_extent(struct btrfs_root *root, u64 start, u64 len,
29aaedb55bSJosef Bacik 			  u64 ram_bytes, u64 offset, u64 disk_bytenr,
30aaedb55bSJosef Bacik 			  u64 disk_len, u32 type, u8 compression, int slot)
31aaedb55bSJosef Bacik {
32aaedb55bSJosef Bacik 	struct btrfs_path path;
33aaedb55bSJosef Bacik 	struct btrfs_file_extent_item *fi;
34aaedb55bSJosef Bacik 	struct extent_buffer *leaf = root->node;
35aaedb55bSJosef Bacik 	struct btrfs_key key;
36aaedb55bSJosef Bacik 	u32 value_len = sizeof(struct btrfs_file_extent_item);
37aaedb55bSJosef Bacik 
38aaedb55bSJosef Bacik 	if (type == BTRFS_FILE_EXTENT_INLINE)
39aaedb55bSJosef Bacik 		value_len += len;
40aaedb55bSJosef Bacik 	memset(&path, 0, sizeof(path));
41aaedb55bSJosef Bacik 
42aaedb55bSJosef Bacik 	path.nodes[0] = leaf;
43aaedb55bSJosef Bacik 	path.slots[0] = slot;
44aaedb55bSJosef Bacik 
45aaedb55bSJosef Bacik 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
46aaedb55bSJosef Bacik 	key.type = BTRFS_EXTENT_DATA_KEY;
47aaedb55bSJosef Bacik 	key.offset = start;
48aaedb55bSJosef Bacik 
49aaedb55bSJosef Bacik 	setup_items_for_insert(root, &path, &key, &value_len, value_len,
50aaedb55bSJosef Bacik 			       value_len + sizeof(struct btrfs_item), 1);
51aaedb55bSJosef Bacik 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
52aaedb55bSJosef Bacik 	btrfs_set_file_extent_generation(leaf, fi, 1);
53aaedb55bSJosef Bacik 	btrfs_set_file_extent_type(leaf, fi, type);
54aaedb55bSJosef Bacik 	btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
55aaedb55bSJosef Bacik 	btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_len);
56aaedb55bSJosef Bacik 	btrfs_set_file_extent_offset(leaf, fi, offset);
57aaedb55bSJosef Bacik 	btrfs_set_file_extent_num_bytes(leaf, fi, len);
58aaedb55bSJosef Bacik 	btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
59aaedb55bSJosef Bacik 	btrfs_set_file_extent_compression(leaf, fi, compression);
60aaedb55bSJosef Bacik 	btrfs_set_file_extent_encryption(leaf, fi, 0);
61aaedb55bSJosef Bacik 	btrfs_set_file_extent_other_encoding(leaf, fi, 0);
62aaedb55bSJosef Bacik }
63aaedb55bSJosef Bacik 
640e30db86SJosef Bacik static void insert_inode_item_key(struct btrfs_root *root)
650e30db86SJosef Bacik {
660e30db86SJosef Bacik 	struct btrfs_path path;
670e30db86SJosef Bacik 	struct extent_buffer *leaf = root->node;
680e30db86SJosef Bacik 	struct btrfs_key key;
690e30db86SJosef Bacik 	u32 value_len = 0;
700e30db86SJosef Bacik 
710e30db86SJosef Bacik 	memset(&path, 0, sizeof(path));
720e30db86SJosef Bacik 
730e30db86SJosef Bacik 	path.nodes[0] = leaf;
740e30db86SJosef Bacik 	path.slots[0] = 0;
750e30db86SJosef Bacik 
760e30db86SJosef Bacik 	key.objectid = BTRFS_INODE_ITEM_KEY;
770e30db86SJosef Bacik 	key.type = BTRFS_INODE_ITEM_KEY;
780e30db86SJosef Bacik 	key.offset = 0;
790e30db86SJosef Bacik 
800e30db86SJosef Bacik 	setup_items_for_insert(root, &path, &key, &value_len, value_len,
810e30db86SJosef Bacik 			       value_len + sizeof(struct btrfs_item), 1);
820e30db86SJosef Bacik }
830e30db86SJosef Bacik 
84aaedb55bSJosef Bacik /*
85aaedb55bSJosef Bacik  * Build the most complicated map of extents the earth has ever seen.  We want
86aaedb55bSJosef Bacik  * this so we can test all of the corner cases of btrfs_get_extent.  Here is a
87aaedb55bSJosef Bacik  * diagram of how the extents will look though this may not be possible we still
88aaedb55bSJosef Bacik  * want to make sure everything acts normally (the last number is not inclusive)
89aaedb55bSJosef Bacik  *
90b9ef22deSFeifei Xu  * [0 - 5][5 -  6][     6 - 4096     ][ 4096 - 4100][4100 - 8195][8195 - 12291]
91b9ef22deSFeifei Xu  * [hole ][inline][hole but no extent][  hole   ][   regular ][regular1 split]
92aaedb55bSJosef Bacik  *
93b9ef22deSFeifei Xu  * [12291 - 16387][16387 - 24579][24579 - 28675][ 28675 - 32771][32771 - 36867 ]
94b9ef22deSFeifei Xu  * [    hole    ][regular1 split][   prealloc ][   prealloc1  ][prealloc1 written]
95aaedb55bSJosef Bacik  *
96b9ef22deSFeifei Xu  * [36867 - 45059][45059 - 53251][53251 - 57347][57347 - 61443][61443- 69635]
97b9ef22deSFeifei Xu  * [  prealloc1  ][ compressed  ][ compressed1 ][    regular  ][ compressed1]
98aaedb55bSJosef Bacik  *
99b9ef22deSFeifei Xu  * [69635-73731][   73731 - 86019   ][86019-90115]
100b9ef22deSFeifei Xu  * [  regular  ][ hole but no extent][  regular  ]
101aaedb55bSJosef Bacik  */
102b9ef22deSFeifei Xu static void setup_file_extents(struct btrfs_root *root, u32 sectorsize)
103aaedb55bSJosef Bacik {
104aaedb55bSJosef Bacik 	int slot = 0;
105ee22184bSByongho Lee 	u64 disk_bytenr = SZ_1M;
106aaedb55bSJosef Bacik 	u64 offset = 0;
107aaedb55bSJosef Bacik 
108aaedb55bSJosef Bacik 	/* First we want a hole */
109aaedb55bSJosef Bacik 	insert_extent(root, offset, 5, 5, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
110aaedb55bSJosef Bacik 		      slot);
111aaedb55bSJosef Bacik 	slot++;
112aaedb55bSJosef Bacik 	offset += 5;
113aaedb55bSJosef Bacik 
114aaedb55bSJosef Bacik 	/*
115aaedb55bSJosef Bacik 	 * Now we want an inline extent, I don't think this is possible but hey
116aaedb55bSJosef Bacik 	 * why not?  Also keep in mind if we have an inline extent it counts as
117aaedb55bSJosef Bacik 	 * the whole first page.  If we were to expand it we would have to cow
118aaedb55bSJosef Bacik 	 * and we wouldn't have an inline extent anymore.
119aaedb55bSJosef Bacik 	 */
120aaedb55bSJosef Bacik 	insert_extent(root, offset, 1, 1, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE, 0,
121aaedb55bSJosef Bacik 		      slot);
122aaedb55bSJosef Bacik 	slot++;
123b9ef22deSFeifei Xu 	offset = sectorsize;
124aaedb55bSJosef Bacik 
125aaedb55bSJosef Bacik 	/* Now another hole */
126aaedb55bSJosef Bacik 	insert_extent(root, offset, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
127aaedb55bSJosef Bacik 		      slot);
128aaedb55bSJosef Bacik 	slot++;
129aaedb55bSJosef Bacik 	offset += 4;
130aaedb55bSJosef Bacik 
131aaedb55bSJosef Bacik 	/* Now for a regular extent */
132b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize - 1, sectorsize - 1, 0,
133b9ef22deSFeifei Xu 		      disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
134aaedb55bSJosef Bacik 	slot++;
135b9ef22deSFeifei Xu 	disk_bytenr += sectorsize;
136b9ef22deSFeifei Xu 	offset += sectorsize - 1;
137aaedb55bSJosef Bacik 
138aaedb55bSJosef Bacik 	/*
139aaedb55bSJosef Bacik 	 * Now for 3 extents that were split from a hole punch so we test
140aaedb55bSJosef Bacik 	 * offsets properly.
141aaedb55bSJosef Bacik 	 */
142b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
143b9ef22deSFeifei Xu 		      4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
144b9ef22deSFeifei Xu 	slot++;
145b9ef22deSFeifei Xu 	offset += sectorsize;
146b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0, 0, 0,
147aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
148aaedb55bSJosef Bacik 	slot++;
149b9ef22deSFeifei Xu 	offset += sectorsize;
150b9ef22deSFeifei Xu 	insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
151b9ef22deSFeifei Xu 		      2 * sectorsize, disk_bytenr, 4 * sectorsize,
152aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
153aaedb55bSJosef Bacik 	slot++;
154b9ef22deSFeifei Xu 	offset += 2 * sectorsize;
155b9ef22deSFeifei Xu 	disk_bytenr += 4 * sectorsize;
156aaedb55bSJosef Bacik 
157aaedb55bSJosef Bacik 	/* Now for a unwritten prealloc extent */
158b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
159b9ef22deSFeifei Xu 		sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
160aaedb55bSJosef Bacik 	slot++;
161b9ef22deSFeifei Xu 	offset += sectorsize;
162aaedb55bSJosef Bacik 
163aaedb55bSJosef Bacik 	/*
164aaedb55bSJosef Bacik 	 * We want to jack up disk_bytenr a little more so the em stuff doesn't
165aaedb55bSJosef Bacik 	 * merge our records.
166aaedb55bSJosef Bacik 	 */
167b9ef22deSFeifei Xu 	disk_bytenr += 2 * sectorsize;
168aaedb55bSJosef Bacik 
169aaedb55bSJosef Bacik 	/*
170aaedb55bSJosef Bacik 	 * Now for a partially written prealloc extent, basically the same as
171aaedb55bSJosef Bacik 	 * the hole punch example above.  Ram_bytes never changes when you mark
172aaedb55bSJosef Bacik 	 * extents written btw.
173aaedb55bSJosef Bacik 	 */
174b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
175b9ef22deSFeifei Xu 		      4 * sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
176b9ef22deSFeifei Xu 	slot++;
177b9ef22deSFeifei Xu 	offset += sectorsize;
178b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, 4 * sectorsize, sectorsize,
179b9ef22deSFeifei Xu 		      disk_bytenr, 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0,
180b9ef22deSFeifei Xu 		      slot);
181b9ef22deSFeifei Xu 	slot++;
182b9ef22deSFeifei Xu 	offset += sectorsize;
183b9ef22deSFeifei Xu 	insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
184b9ef22deSFeifei Xu 		      2 * sectorsize, disk_bytenr, 4 * sectorsize,
185aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
186aaedb55bSJosef Bacik 	slot++;
187b9ef22deSFeifei Xu 	offset += 2 * sectorsize;
188b9ef22deSFeifei Xu 	disk_bytenr += 4 * sectorsize;
189aaedb55bSJosef Bacik 
190aaedb55bSJosef Bacik 	/* Now a normal compressed extent */
191b9ef22deSFeifei Xu 	insert_extent(root, offset, 2 * sectorsize, 2 * sectorsize, 0,
192b9ef22deSFeifei Xu 		      disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG,
193b9ef22deSFeifei Xu 		      BTRFS_COMPRESS_ZLIB, slot);
194aaedb55bSJosef Bacik 	slot++;
195b9ef22deSFeifei Xu 	offset += 2 * sectorsize;
196aaedb55bSJosef Bacik 	/* No merges */
197b9ef22deSFeifei Xu 	disk_bytenr += 2 * sectorsize;
198aaedb55bSJosef Bacik 
199aaedb55bSJosef Bacik 	/* Now a split compressed extent */
200b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
201b9ef22deSFeifei Xu 		      sectorsize, BTRFS_FILE_EXTENT_REG,
202b9ef22deSFeifei Xu 		      BTRFS_COMPRESS_ZLIB, slot);
203aaedb55bSJosef Bacik 	slot++;
204b9ef22deSFeifei Xu 	offset += sectorsize;
205b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0,
206b9ef22deSFeifei Xu 		      disk_bytenr + sectorsize, sectorsize,
207aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
208aaedb55bSJosef Bacik 	slot++;
209b9ef22deSFeifei Xu 	offset += sectorsize;
210b9ef22deSFeifei Xu 	insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
211b9ef22deSFeifei Xu 		      2 * sectorsize, disk_bytenr, sectorsize,
212aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
213aaedb55bSJosef Bacik 	slot++;
214b9ef22deSFeifei Xu 	offset += 2 * sectorsize;
215b9ef22deSFeifei Xu 	disk_bytenr += 2 * sectorsize;
216aaedb55bSJosef Bacik 
217aaedb55bSJosef Bacik 	/* Now extents that have a hole but no hole extent */
218b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
219b9ef22deSFeifei Xu 		      sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
220aaedb55bSJosef Bacik 	slot++;
221b9ef22deSFeifei Xu 	offset += 4 * sectorsize;
222b9ef22deSFeifei Xu 	disk_bytenr += sectorsize;
223b9ef22deSFeifei Xu 	insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
224b9ef22deSFeifei Xu 		      sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
225aaedb55bSJosef Bacik }
226aaedb55bSJosef Bacik 
227aaedb55bSJosef Bacik static unsigned long prealloc_only = 0;
228aaedb55bSJosef Bacik static unsigned long compressed_only = 0;
229aaedb55bSJosef Bacik static unsigned long vacancy_only = 0;
230aaedb55bSJosef Bacik 
231b9ef22deSFeifei Xu static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize)
232aaedb55bSJosef Bacik {
2337c0260eeSJeff Mahoney 	struct btrfs_fs_info *fs_info = NULL;
234aaedb55bSJosef Bacik 	struct inode *inode = NULL;
235aaedb55bSJosef Bacik 	struct btrfs_root *root = NULL;
236aaedb55bSJosef Bacik 	struct extent_map *em = NULL;
237aaedb55bSJosef Bacik 	u64 orig_start;
238aaedb55bSJosef Bacik 	u64 disk_bytenr;
239aaedb55bSJosef Bacik 	u64 offset;
240aaedb55bSJosef Bacik 	int ret = -ENOMEM;
241aaedb55bSJosef Bacik 
242aaedb55bSJosef Bacik 	inode = btrfs_new_test_inode();
243aaedb55bSJosef Bacik 	if (!inode) {
244aaedb55bSJosef Bacik 		test_msg("Couldn't allocate inode\n");
245aaedb55bSJosef Bacik 		return ret;
246aaedb55bSJosef Bacik 	}
247aaedb55bSJosef Bacik 
248aaedb55bSJosef Bacik 	BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
249aaedb55bSJosef Bacik 	BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
250aaedb55bSJosef Bacik 	BTRFS_I(inode)->location.offset = 0;
251aaedb55bSJosef Bacik 
2527c0260eeSJeff Mahoney 	fs_info = btrfs_alloc_dummy_fs_info();
2537c0260eeSJeff Mahoney 	if (!fs_info) {
2547c0260eeSJeff Mahoney 		test_msg("Couldn't allocate dummy fs info\n");
255aaedb55bSJosef Bacik 		goto out;
256aaedb55bSJosef Bacik 	}
257aaedb55bSJosef Bacik 
2587c0260eeSJeff Mahoney 	root = btrfs_alloc_dummy_root(fs_info, sectorsize, nodesize);
2597c0260eeSJeff Mahoney 	if (IS_ERR(root)) {
2607c0260eeSJeff Mahoney 		test_msg("Couldn't allocate root\n");
261aaedb55bSJosef Bacik 		goto out;
262aaedb55bSJosef Bacik 	}
263aaedb55bSJosef Bacik 
264b9ef22deSFeifei Xu 	root->node = alloc_dummy_extent_buffer(NULL, nodesize, nodesize);
265aaedb55bSJosef Bacik 	if (!root->node) {
266aaedb55bSJosef Bacik 		test_msg("Couldn't allocate dummy buffer\n");
267aaedb55bSJosef Bacik 		goto out;
268aaedb55bSJosef Bacik 	}
269aaedb55bSJosef Bacik 
270aaedb55bSJosef Bacik 	/*
271aaedb55bSJosef Bacik 	 * We will just free a dummy node if it's ref count is 2 so we need an
27201327610SNicholas D Steeves 	 * extra ref so our searches don't accidentally release our page.
273aaedb55bSJosef Bacik 	 */
274aaedb55bSJosef Bacik 	extent_buffer_get(root->node);
275aaedb55bSJosef Bacik 	btrfs_set_header_nritems(root->node, 0);
276aaedb55bSJosef Bacik 	btrfs_set_header_level(root->node, 0);
277aaedb55bSJosef Bacik 	ret = -EINVAL;
278aaedb55bSJosef Bacik 
279aaedb55bSJosef Bacik 	/* First with no extents */
280aaedb55bSJosef Bacik 	BTRFS_I(inode)->root = root;
281b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, 0, sectorsize, 0);
282aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
283aaedb55bSJosef Bacik 		em = NULL;
284aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
285aaedb55bSJosef Bacik 		goto out;
286aaedb55bSJosef Bacik 	}
287aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
288aaedb55bSJosef Bacik 		test_msg("Expected a hole, got %llu\n", em->block_start);
289aaedb55bSJosef Bacik 		goto out;
290aaedb55bSJosef Bacik 	}
291aaedb55bSJosef Bacik 	if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
292aaedb55bSJosef Bacik 		test_msg("Vacancy flag wasn't set properly\n");
293aaedb55bSJosef Bacik 		goto out;
294aaedb55bSJosef Bacik 	}
295aaedb55bSJosef Bacik 	free_extent_map(em);
296aaedb55bSJosef Bacik 	btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
297aaedb55bSJosef Bacik 
298aaedb55bSJosef Bacik 	/*
299aaedb55bSJosef Bacik 	 * All of the magic numbers are based on the mapping setup in
300aaedb55bSJosef Bacik 	 * setup_file_extents, so if you change anything there you need to
301aaedb55bSJosef Bacik 	 * update the comment and update the expected values below.
302aaedb55bSJosef Bacik 	 */
303b9ef22deSFeifei Xu 	setup_file_extents(root, sectorsize);
304aaedb55bSJosef Bacik 
305aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, 0, (u64)-1, 0);
306aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
307aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
308aaedb55bSJosef Bacik 		goto out;
309aaedb55bSJosef Bacik 	}
310aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
311aaedb55bSJosef Bacik 		test_msg("Expected a hole, got %llu\n", em->block_start);
312aaedb55bSJosef Bacik 		goto out;
313aaedb55bSJosef Bacik 	}
314aaedb55bSJosef Bacik 	if (em->start != 0 || em->len != 5) {
315aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start 0 len 5, got start "
316aaedb55bSJosef Bacik 			 "%llu len %llu\n", em->start, em->len);
317aaedb55bSJosef Bacik 		goto out;
318aaedb55bSJosef Bacik 	}
319aaedb55bSJosef Bacik 	if (em->flags != 0) {
320aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
321aaedb55bSJosef Bacik 		goto out;
322aaedb55bSJosef Bacik 	}
323aaedb55bSJosef Bacik 	offset = em->start + em->len;
324aaedb55bSJosef Bacik 	free_extent_map(em);
325aaedb55bSJosef Bacik 
326b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
327aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
328aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
329aaedb55bSJosef Bacik 		goto out;
330aaedb55bSJosef Bacik 	}
331aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_INLINE) {
332aaedb55bSJosef Bacik 		test_msg("Expected an inline, got %llu\n", em->block_start);
333aaedb55bSJosef Bacik 		goto out;
334aaedb55bSJosef Bacik 	}
335b9ef22deSFeifei Xu 
336b9ef22deSFeifei Xu 	if (em->start != offset || em->len != (sectorsize - 5)) {
337aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 1, got start "
338aaedb55bSJosef Bacik 			 "%llu len %llu\n", offset, em->start, em->len);
339aaedb55bSJosef Bacik 		goto out;
340aaedb55bSJosef Bacik 	}
341aaedb55bSJosef Bacik 	if (em->flags != 0) {
342aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
343aaedb55bSJosef Bacik 		goto out;
344aaedb55bSJosef Bacik 	}
345aaedb55bSJosef Bacik 	/*
346aaedb55bSJosef Bacik 	 * We don't test anything else for inline since it doesn't get set
347aaedb55bSJosef Bacik 	 * unless we have a page for it to write into.  Maybe we should change
348aaedb55bSJosef Bacik 	 * this?
349aaedb55bSJosef Bacik 	 */
350aaedb55bSJosef Bacik 	offset = em->start + em->len;
351aaedb55bSJosef Bacik 	free_extent_map(em);
352aaedb55bSJosef Bacik 
353b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
354aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
355aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
356aaedb55bSJosef Bacik 		goto out;
357aaedb55bSJosef Bacik 	}
358aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
359aaedb55bSJosef Bacik 		test_msg("Expected a hole, got %llu\n", em->block_start);
360aaedb55bSJosef Bacik 		goto out;
361aaedb55bSJosef Bacik 	}
362aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 4) {
363aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 4, got start "
364aaedb55bSJosef Bacik 			 "%llu len %llu\n", offset, em->start, em->len);
365aaedb55bSJosef Bacik 		goto out;
366aaedb55bSJosef Bacik 	}
367aaedb55bSJosef Bacik 	if (em->flags != 0) {
368aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
369aaedb55bSJosef Bacik 		goto out;
370aaedb55bSJosef Bacik 	}
371aaedb55bSJosef Bacik 	offset = em->start + em->len;
372aaedb55bSJosef Bacik 	free_extent_map(em);
373aaedb55bSJosef Bacik 
374aaedb55bSJosef Bacik 	/* Regular extent */
375b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
376aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
377aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
378aaedb55bSJosef Bacik 		goto out;
379aaedb55bSJosef Bacik 	}
380aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
381aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
382aaedb55bSJosef Bacik 		goto out;
383aaedb55bSJosef Bacik 	}
384b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize - 1) {
385aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 4095, got "
386aaedb55bSJosef Bacik 			 "start %llu len %llu\n", offset, em->start, em->len);
387aaedb55bSJosef Bacik 		goto out;
388aaedb55bSJosef Bacik 	}
389aaedb55bSJosef Bacik 	if (em->flags != 0) {
390aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
391aaedb55bSJosef Bacik 		goto out;
392aaedb55bSJosef Bacik 	}
393aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
394aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
395aaedb55bSJosef Bacik 			 em->orig_start);
396aaedb55bSJosef Bacik 		goto out;
397aaedb55bSJosef Bacik 	}
398aaedb55bSJosef Bacik 	offset = em->start + em->len;
399aaedb55bSJosef Bacik 	free_extent_map(em);
400aaedb55bSJosef Bacik 
401aaedb55bSJosef Bacik 	/* The next 3 are split extents */
402b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
403aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
404aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
405aaedb55bSJosef Bacik 		goto out;
406aaedb55bSJosef Bacik 	}
407aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
408aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
409aaedb55bSJosef Bacik 		goto out;
410aaedb55bSJosef Bacik 	}
411b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
412b9ef22deSFeifei Xu 		test_msg("Unexpected extent start %llu len %u, "
413b9ef22deSFeifei Xu 			"got start %llu len %llu\n",
414b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
415aaedb55bSJosef Bacik 		goto out;
416aaedb55bSJosef Bacik 	}
417aaedb55bSJosef Bacik 	if (em->flags != 0) {
418aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
419aaedb55bSJosef Bacik 		goto out;
420aaedb55bSJosef Bacik 	}
421aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
422aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
423aaedb55bSJosef Bacik 			 em->orig_start);
424aaedb55bSJosef Bacik 		goto out;
425aaedb55bSJosef Bacik 	}
426aaedb55bSJosef Bacik 	disk_bytenr = em->block_start;
427aaedb55bSJosef Bacik 	orig_start = em->start;
428aaedb55bSJosef Bacik 	offset = em->start + em->len;
429aaedb55bSJosef Bacik 	free_extent_map(em);
430aaedb55bSJosef Bacik 
431b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
432aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
433aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
434aaedb55bSJosef Bacik 		goto out;
435aaedb55bSJosef Bacik 	}
436aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
437aaedb55bSJosef Bacik 		test_msg("Expected a hole, got %llu\n", em->block_start);
438aaedb55bSJosef Bacik 		goto out;
439aaedb55bSJosef Bacik 	}
440b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
441b9ef22deSFeifei Xu 		test_msg("Unexpected extent wanted start %llu len %u, "
442b9ef22deSFeifei Xu 			"got start %llu len %llu\n",
443b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
444aaedb55bSJosef Bacik 		goto out;
445aaedb55bSJosef Bacik 	}
446aaedb55bSJosef Bacik 	if (em->flags != 0) {
447aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
448aaedb55bSJosef Bacik 		goto out;
449aaedb55bSJosef Bacik 	}
450aaedb55bSJosef Bacik 	offset = em->start + em->len;
451aaedb55bSJosef Bacik 	free_extent_map(em);
452aaedb55bSJosef Bacik 
453b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
454aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
455aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
456aaedb55bSJosef Bacik 		goto out;
457aaedb55bSJosef Bacik 	}
458aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
459aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
460aaedb55bSJosef Bacik 		goto out;
461aaedb55bSJosef Bacik 	}
462b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 2 * sectorsize) {
463b9ef22deSFeifei Xu 		test_msg("Unexpected extent wanted start %llu len %u, "
464b9ef22deSFeifei Xu 			"got start %llu len %llu\n",
465b9ef22deSFeifei Xu 			offset, 2 * sectorsize, em->start, em->len);
466aaedb55bSJosef Bacik 		goto out;
467aaedb55bSJosef Bacik 	}
468aaedb55bSJosef Bacik 	if (em->flags != 0) {
469aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
470aaedb55bSJosef Bacik 		goto out;
471aaedb55bSJosef Bacik 	}
472aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
473aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n",
474aaedb55bSJosef Bacik 			 orig_start, em->orig_start);
475aaedb55bSJosef Bacik 		goto out;
476aaedb55bSJosef Bacik 	}
477aaedb55bSJosef Bacik 	disk_bytenr += (em->start - orig_start);
478aaedb55bSJosef Bacik 	if (em->block_start != disk_bytenr) {
479aaedb55bSJosef Bacik 		test_msg("Wrong block start, want %llu, have %llu\n",
480aaedb55bSJosef Bacik 			 disk_bytenr, em->block_start);
481aaedb55bSJosef Bacik 		goto out;
482aaedb55bSJosef Bacik 	}
483aaedb55bSJosef Bacik 	offset = em->start + em->len;
484aaedb55bSJosef Bacik 	free_extent_map(em);
485aaedb55bSJosef Bacik 
486aaedb55bSJosef Bacik 	/* Prealloc extent */
487b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
488aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
489aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
490aaedb55bSJosef Bacik 		goto out;
491aaedb55bSJosef Bacik 	}
492aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
493aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
494aaedb55bSJosef Bacik 		goto out;
495aaedb55bSJosef Bacik 	}
496b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
497b9ef22deSFeifei Xu 		test_msg("Unexpected extent wanted start %llu len %u, "
498b9ef22deSFeifei Xu 			"got start %llu len %llu\n",
499b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
500aaedb55bSJosef Bacik 		goto out;
501aaedb55bSJosef Bacik 	}
502aaedb55bSJosef Bacik 	if (em->flags != prealloc_only) {
503aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want %lu have %lu\n",
504aaedb55bSJosef Bacik 			 prealloc_only, em->flags);
505aaedb55bSJosef Bacik 		goto out;
506aaedb55bSJosef Bacik 	}
507aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
508aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
509aaedb55bSJosef Bacik 			 em->orig_start);
510aaedb55bSJosef Bacik 		goto out;
511aaedb55bSJosef Bacik 	}
512aaedb55bSJosef Bacik 	offset = em->start + em->len;
513aaedb55bSJosef Bacik 	free_extent_map(em);
514aaedb55bSJosef Bacik 
515aaedb55bSJosef Bacik 	/* The next 3 are a half written prealloc extent */
516b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
517aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
518aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
519aaedb55bSJosef Bacik 		goto out;
520aaedb55bSJosef Bacik 	}
521aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
522aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
523aaedb55bSJosef Bacik 		goto out;
524aaedb55bSJosef Bacik 	}
525b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
526b9ef22deSFeifei Xu 		test_msg("Unexpected extent wanted start %llu len %u, "
527b9ef22deSFeifei Xu 			"got start %llu len %llu\n",
528b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
529aaedb55bSJosef Bacik 		goto out;
530aaedb55bSJosef Bacik 	}
531aaedb55bSJosef Bacik 	if (em->flags != prealloc_only) {
532aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want %lu have %lu\n",
533aaedb55bSJosef Bacik 			 prealloc_only, em->flags);
534aaedb55bSJosef Bacik 		goto out;
535aaedb55bSJosef Bacik 	}
536aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
537aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
538aaedb55bSJosef Bacik 			 em->orig_start);
539aaedb55bSJosef Bacik 		goto out;
540aaedb55bSJosef Bacik 	}
541aaedb55bSJosef Bacik 	disk_bytenr = em->block_start;
542aaedb55bSJosef Bacik 	orig_start = em->start;
543aaedb55bSJosef Bacik 	offset = em->start + em->len;
544aaedb55bSJosef Bacik 	free_extent_map(em);
545aaedb55bSJosef Bacik 
546b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
547aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
548aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
549aaedb55bSJosef Bacik 		goto out;
550aaedb55bSJosef Bacik 	}
551aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_HOLE) {
552aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
553aaedb55bSJosef Bacik 		goto out;
554aaedb55bSJosef Bacik 	}
555b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
556b9ef22deSFeifei Xu 		test_msg("Unexpected extent wanted start %llu len %u, "
557b9ef22deSFeifei Xu 			"got start %llu len %llu\n",
558b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
559aaedb55bSJosef Bacik 		goto out;
560aaedb55bSJosef Bacik 	}
561aaedb55bSJosef Bacik 	if (em->flags != 0) {
562aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
563aaedb55bSJosef Bacik 		goto out;
564aaedb55bSJosef Bacik 	}
565aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
566aaedb55bSJosef Bacik 		test_msg("Unexpected orig offset, wanted %llu, have %llu\n",
567aaedb55bSJosef Bacik 			 orig_start, em->orig_start);
568aaedb55bSJosef Bacik 		goto out;
569aaedb55bSJosef Bacik 	}
570aaedb55bSJosef Bacik 	if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
571aaedb55bSJosef Bacik 		test_msg("Unexpected block start, wanted %llu, have %llu\n",
572aaedb55bSJosef Bacik 			 disk_bytenr + (em->start - em->orig_start),
573aaedb55bSJosef Bacik 			 em->block_start);
574aaedb55bSJosef Bacik 		goto out;
575aaedb55bSJosef Bacik 	}
576aaedb55bSJosef Bacik 	offset = em->start + em->len;
577aaedb55bSJosef Bacik 	free_extent_map(em);
578aaedb55bSJosef Bacik 
579b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
580aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
581aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
582aaedb55bSJosef Bacik 		goto out;
583aaedb55bSJosef Bacik 	}
584aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
585aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
586aaedb55bSJosef Bacik 		goto out;
587aaedb55bSJosef Bacik 	}
588b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 2 * sectorsize) {
589b9ef22deSFeifei Xu 		test_msg("Unexpected extent wanted start %llu len %u, "
590b9ef22deSFeifei Xu 			"got start %llu len %llu\n",
591b9ef22deSFeifei Xu 			offset, 2 * sectorsize, em->start, em->len);
592aaedb55bSJosef Bacik 		goto out;
593aaedb55bSJosef Bacik 	}
594aaedb55bSJosef Bacik 	if (em->flags != prealloc_only) {
595aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want %lu have %lu\n",
596aaedb55bSJosef Bacik 			 prealloc_only, em->flags);
597aaedb55bSJosef Bacik 		goto out;
598aaedb55bSJosef Bacik 	}
599aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
600aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", orig_start,
601aaedb55bSJosef Bacik 			 em->orig_start);
602aaedb55bSJosef Bacik 		goto out;
603aaedb55bSJosef Bacik 	}
604aaedb55bSJosef Bacik 	if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
605aaedb55bSJosef Bacik 		test_msg("Unexpected block start, wanted %llu, have %llu\n",
606aaedb55bSJosef Bacik 			 disk_bytenr + (em->start - em->orig_start),
607aaedb55bSJosef Bacik 			 em->block_start);
608aaedb55bSJosef Bacik 		goto out;
609aaedb55bSJosef Bacik 	}
610aaedb55bSJosef Bacik 	offset = em->start + em->len;
611aaedb55bSJosef Bacik 	free_extent_map(em);
612aaedb55bSJosef Bacik 
613aaedb55bSJosef Bacik 	/* Now for the compressed extent */
614b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
615aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
616aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
617aaedb55bSJosef Bacik 		goto out;
618aaedb55bSJosef Bacik 	}
619aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
620aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
621aaedb55bSJosef Bacik 		goto out;
622aaedb55bSJosef Bacik 	}
623b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 2 * sectorsize) {
624b9ef22deSFeifei Xu 		test_msg("Unexpected extent wanted start %llu len %u,"
625b9ef22deSFeifei Xu 			"got start %llu len %llu\n",
626b9ef22deSFeifei Xu 			offset, 2 * sectorsize, em->start, em->len);
627aaedb55bSJosef Bacik 		goto out;
628aaedb55bSJosef Bacik 	}
629aaedb55bSJosef Bacik 	if (em->flags != compressed_only) {
630aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want %lu have %lu\n",
631aaedb55bSJosef Bacik 			 compressed_only, em->flags);
632aaedb55bSJosef Bacik 		goto out;
633aaedb55bSJosef Bacik 	}
634aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
635aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n",
636aaedb55bSJosef Bacik 			 em->start, em->orig_start);
637aaedb55bSJosef Bacik 		goto out;
638aaedb55bSJosef Bacik 	}
639aaedb55bSJosef Bacik 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
640aaedb55bSJosef Bacik 		test_msg("Unexpected compress type, wanted %d, got %d\n",
641aaedb55bSJosef Bacik 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
642aaedb55bSJosef Bacik 		goto out;
643aaedb55bSJosef Bacik 	}
644aaedb55bSJosef Bacik 	offset = em->start + em->len;
645aaedb55bSJosef Bacik 	free_extent_map(em);
646aaedb55bSJosef Bacik 
647aaedb55bSJosef Bacik 	/* Split compressed extent */
648b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
649aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
650aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
651aaedb55bSJosef Bacik 		goto out;
652aaedb55bSJosef Bacik 	}
653aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
654aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
655aaedb55bSJosef Bacik 		goto out;
656aaedb55bSJosef Bacik 	}
657b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
658b9ef22deSFeifei Xu 		test_msg("Unexpected extent wanted start %llu len %u,"
659b9ef22deSFeifei Xu 			"got start %llu len %llu\n",
660b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
661aaedb55bSJosef Bacik 		goto out;
662aaedb55bSJosef Bacik 	}
663aaedb55bSJosef Bacik 	if (em->flags != compressed_only) {
664aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want %lu have %lu\n",
665aaedb55bSJosef Bacik 			 compressed_only, em->flags);
666aaedb55bSJosef Bacik 		goto out;
667aaedb55bSJosef Bacik 	}
668aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
669aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n",
670aaedb55bSJosef Bacik 			 em->start, em->orig_start);
671aaedb55bSJosef Bacik 		goto out;
672aaedb55bSJosef Bacik 	}
673aaedb55bSJosef Bacik 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
674aaedb55bSJosef Bacik 		test_msg("Unexpected compress type, wanted %d, got %d\n",
675aaedb55bSJosef Bacik 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
676aaedb55bSJosef Bacik 		goto out;
677aaedb55bSJosef Bacik 	}
678aaedb55bSJosef Bacik 	disk_bytenr = em->block_start;
679aaedb55bSJosef Bacik 	orig_start = em->start;
680aaedb55bSJosef Bacik 	offset = em->start + em->len;
681aaedb55bSJosef Bacik 	free_extent_map(em);
682aaedb55bSJosef Bacik 
683b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
684aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
685aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
686aaedb55bSJosef Bacik 		goto out;
687aaedb55bSJosef Bacik 	}
688aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
689aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
690aaedb55bSJosef Bacik 		goto out;
691aaedb55bSJosef Bacik 	}
692b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
693b9ef22deSFeifei Xu 		test_msg("Unexpected extent wanted start %llu len %u, "
694b9ef22deSFeifei Xu 			"got start %llu len %llu\n",
695b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
696aaedb55bSJosef Bacik 		goto out;
697aaedb55bSJosef Bacik 	}
698aaedb55bSJosef Bacik 	if (em->flags != 0) {
699aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
700aaedb55bSJosef Bacik 		goto out;
701aaedb55bSJosef Bacik 	}
702aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
703aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
704aaedb55bSJosef Bacik 			 em->orig_start);
705aaedb55bSJosef Bacik 		goto out;
706aaedb55bSJosef Bacik 	}
707aaedb55bSJosef Bacik 	offset = em->start + em->len;
708aaedb55bSJosef Bacik 	free_extent_map(em);
709aaedb55bSJosef Bacik 
710b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
711aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
712aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
713aaedb55bSJosef Bacik 		goto out;
714aaedb55bSJosef Bacik 	}
715aaedb55bSJosef Bacik 	if (em->block_start != disk_bytenr) {
716aaedb55bSJosef Bacik 		test_msg("Block start does not match, want %llu got %llu\n",
717aaedb55bSJosef Bacik 			 disk_bytenr, em->block_start);
718aaedb55bSJosef Bacik 		goto out;
719aaedb55bSJosef Bacik 	}
720b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 2 * sectorsize) {
721b9ef22deSFeifei Xu 		test_msg("Unexpected extent wanted start %llu len %u, "
722b9ef22deSFeifei Xu 			"got start %llu len %llu\n",
723b9ef22deSFeifei Xu 			offset, 2 * sectorsize, em->start, em->len);
724aaedb55bSJosef Bacik 		goto out;
725aaedb55bSJosef Bacik 	}
726aaedb55bSJosef Bacik 	if (em->flags != compressed_only) {
727aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want %lu have %lu\n",
728aaedb55bSJosef Bacik 			 compressed_only, em->flags);
729aaedb55bSJosef Bacik 		goto out;
730aaedb55bSJosef Bacik 	}
731aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
732aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n",
733aaedb55bSJosef Bacik 			 em->start, orig_start);
734aaedb55bSJosef Bacik 		goto out;
735aaedb55bSJosef Bacik 	}
736aaedb55bSJosef Bacik 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
737aaedb55bSJosef Bacik 		test_msg("Unexpected compress type, wanted %d, got %d\n",
738aaedb55bSJosef Bacik 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
739aaedb55bSJosef Bacik 		goto out;
740aaedb55bSJosef Bacik 	}
741aaedb55bSJosef Bacik 	offset = em->start + em->len;
742aaedb55bSJosef Bacik 	free_extent_map(em);
743aaedb55bSJosef Bacik 
744aaedb55bSJosef Bacik 	/* A hole between regular extents but no hole extent */
745b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, offset + 6, sectorsize, 0);
746aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
747aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
748aaedb55bSJosef Bacik 		goto out;
749aaedb55bSJosef Bacik 	}
750aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
751aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
752aaedb55bSJosef Bacik 		goto out;
753aaedb55bSJosef Bacik 	}
754b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
755b9ef22deSFeifei Xu 		test_msg("Unexpected extent wanted start %llu len %u, "
756b9ef22deSFeifei Xu 			"got start %llu len %llu\n",
757b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
758aaedb55bSJosef Bacik 		goto out;
759aaedb55bSJosef Bacik 	}
760aaedb55bSJosef Bacik 	if (em->flags != 0) {
761aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
762aaedb55bSJosef Bacik 		goto out;
763aaedb55bSJosef Bacik 	}
764aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
765aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
766aaedb55bSJosef Bacik 			 em->orig_start);
767aaedb55bSJosef Bacik 		goto out;
768aaedb55bSJosef Bacik 	}
769aaedb55bSJosef Bacik 	offset = em->start + em->len;
770aaedb55bSJosef Bacik 	free_extent_map(em);
771aaedb55bSJosef Bacik 
772aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset, 4096 * 1024, 0);
773aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
774aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
775aaedb55bSJosef Bacik 		goto out;
776aaedb55bSJosef Bacik 	}
777aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
778aaedb55bSJosef Bacik 		test_msg("Expected a hole extent, got %llu\n", em->block_start);
779aaedb55bSJosef Bacik 		goto out;
780aaedb55bSJosef Bacik 	}
781aaedb55bSJosef Bacik 	/*
782aaedb55bSJosef Bacik 	 * Currently we just return a length that we requested rather than the
783aaedb55bSJosef Bacik 	 * length of the actual hole, if this changes we'll have to change this
784aaedb55bSJosef Bacik 	 * test.
785aaedb55bSJosef Bacik 	 */
786b9ef22deSFeifei Xu 	if (em->start != offset || em->len != 3 * sectorsize) {
787b9ef22deSFeifei Xu 		test_msg("Unexpected extent wanted start %llu len %u, "
788b9ef22deSFeifei Xu 			"got start %llu len %llu\n",
789b9ef22deSFeifei Xu 			offset, 3 * sectorsize, em->start, em->len);
790aaedb55bSJosef Bacik 		goto out;
791aaedb55bSJosef Bacik 	}
792aaedb55bSJosef Bacik 	if (em->flags != vacancy_only) {
793aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want %lu have %lu\n",
794aaedb55bSJosef Bacik 			 vacancy_only, em->flags);
795aaedb55bSJosef Bacik 		goto out;
796aaedb55bSJosef Bacik 	}
797aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
798aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
799aaedb55bSJosef Bacik 			 em->orig_start);
800aaedb55bSJosef Bacik 		goto out;
801aaedb55bSJosef Bacik 	}
802aaedb55bSJosef Bacik 	offset = em->start + em->len;
803aaedb55bSJosef Bacik 	free_extent_map(em);
804aaedb55bSJosef Bacik 
805b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize, 0);
806aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
807aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
808aaedb55bSJosef Bacik 		goto out;
809aaedb55bSJosef Bacik 	}
810aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
811aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
812aaedb55bSJosef Bacik 		goto out;
813aaedb55bSJosef Bacik 	}
814b9ef22deSFeifei Xu 	if (em->start != offset || em->len != sectorsize) {
815b9ef22deSFeifei Xu 		test_msg("Unexpected extent wanted start %llu len %u,"
816b9ef22deSFeifei Xu 			"got start %llu len %llu\n",
817b9ef22deSFeifei Xu 			offset, sectorsize, em->start, em->len);
818aaedb55bSJosef Bacik 		goto out;
819aaedb55bSJosef Bacik 	}
820aaedb55bSJosef Bacik 	if (em->flags != 0) {
821aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
822aaedb55bSJosef Bacik 		goto out;
823aaedb55bSJosef Bacik 	}
824aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
825aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
826aaedb55bSJosef Bacik 			 em->orig_start);
827aaedb55bSJosef Bacik 		goto out;
828aaedb55bSJosef Bacik 	}
829aaedb55bSJosef Bacik 	ret = 0;
830aaedb55bSJosef Bacik out:
831aaedb55bSJosef Bacik 	if (!IS_ERR(em))
832aaedb55bSJosef Bacik 		free_extent_map(em);
833aaedb55bSJosef Bacik 	iput(inode);
834faa2dbf0SJosef Bacik 	btrfs_free_dummy_root(root);
8357c0260eeSJeff Mahoney 	btrfs_free_dummy_fs_info(fs_info);
836aaedb55bSJosef Bacik 	return ret;
837aaedb55bSJosef Bacik }
838aaedb55bSJosef Bacik 
839b9ef22deSFeifei Xu static int test_hole_first(u32 sectorsize, u32 nodesize)
8400e30db86SJosef Bacik {
8417c0260eeSJeff Mahoney 	struct btrfs_fs_info *fs_info = NULL;
8420e30db86SJosef Bacik 	struct inode *inode = NULL;
8430e30db86SJosef Bacik 	struct btrfs_root *root = NULL;
8440e30db86SJosef Bacik 	struct extent_map *em = NULL;
8450e30db86SJosef Bacik 	int ret = -ENOMEM;
8460e30db86SJosef Bacik 
8470e30db86SJosef Bacik 	inode = btrfs_new_test_inode();
8480e30db86SJosef Bacik 	if (!inode) {
8490e30db86SJosef Bacik 		test_msg("Couldn't allocate inode\n");
8500e30db86SJosef Bacik 		return ret;
8510e30db86SJosef Bacik 	}
8520e30db86SJosef Bacik 
8530e30db86SJosef Bacik 	BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
8540e30db86SJosef Bacik 	BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
8550e30db86SJosef Bacik 	BTRFS_I(inode)->location.offset = 0;
8560e30db86SJosef Bacik 
8577c0260eeSJeff Mahoney 	fs_info = btrfs_alloc_dummy_fs_info();
8587c0260eeSJeff Mahoney 	if (!fs_info) {
8597c0260eeSJeff Mahoney 		test_msg("Couldn't allocate dummy fs info\n");
8600e30db86SJosef Bacik 		goto out;
8610e30db86SJosef Bacik 	}
8620e30db86SJosef Bacik 
8637c0260eeSJeff Mahoney 	root = btrfs_alloc_dummy_root(fs_info, sectorsize, nodesize);
8647c0260eeSJeff Mahoney 	if (IS_ERR(root)) {
8657c0260eeSJeff Mahoney 		test_msg("Couldn't allocate root\n");
8660e30db86SJosef Bacik 		goto out;
8670e30db86SJosef Bacik 	}
8680e30db86SJosef Bacik 
869b9ef22deSFeifei Xu 	root->node = alloc_dummy_extent_buffer(NULL, nodesize, nodesize);
8700e30db86SJosef Bacik 	if (!root->node) {
8710e30db86SJosef Bacik 		test_msg("Couldn't allocate dummy buffer\n");
8720e30db86SJosef Bacik 		goto out;
8730e30db86SJosef Bacik 	}
8740e30db86SJosef Bacik 
8750e30db86SJosef Bacik 	extent_buffer_get(root->node);
8760e30db86SJosef Bacik 	btrfs_set_header_nritems(root->node, 0);
8770e30db86SJosef Bacik 	btrfs_set_header_level(root->node, 0);
8780e30db86SJosef Bacik 	BTRFS_I(inode)->root = root;
8790e30db86SJosef Bacik 	ret = -EINVAL;
8800e30db86SJosef Bacik 
8810e30db86SJosef Bacik 	/*
8820e30db86SJosef Bacik 	 * Need a blank inode item here just so we don't confuse
8830e30db86SJosef Bacik 	 * btrfs_get_extent.
8840e30db86SJosef Bacik 	 */
8850e30db86SJosef Bacik 	insert_inode_item_key(root);
886b9ef22deSFeifei Xu 	insert_extent(root, sectorsize, sectorsize, sectorsize, 0, sectorsize,
887b9ef22deSFeifei Xu 		      sectorsize, BTRFS_FILE_EXTENT_REG, 0, 1);
888b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, 0, 2 * sectorsize, 0);
8890e30db86SJosef Bacik 	if (IS_ERR(em)) {
8900e30db86SJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
8910e30db86SJosef Bacik 		goto out;
8920e30db86SJosef Bacik 	}
8930e30db86SJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
8940e30db86SJosef Bacik 		test_msg("Expected a hole, got %llu\n", em->block_start);
8950e30db86SJosef Bacik 		goto out;
8960e30db86SJosef Bacik 	}
897b9ef22deSFeifei Xu 	if (em->start != 0 || em->len != sectorsize) {
898b9ef22deSFeifei Xu 		test_msg("Unexpected extent wanted start 0 len %u, "
899b9ef22deSFeifei Xu 			"got start %llu len %llu\n",
900b9ef22deSFeifei Xu 			sectorsize, em->start, em->len);
9010e30db86SJosef Bacik 		goto out;
9020e30db86SJosef Bacik 	}
9030e30db86SJosef Bacik 	if (em->flags != vacancy_only) {
9040e30db86SJosef Bacik 		test_msg("Wrong flags, wanted %lu, have %lu\n", vacancy_only,
9050e30db86SJosef Bacik 			 em->flags);
9060e30db86SJosef Bacik 		goto out;
9070e30db86SJosef Bacik 	}
9080e30db86SJosef Bacik 	free_extent_map(em);
9090e30db86SJosef Bacik 
910b9ef22deSFeifei Xu 	em = btrfs_get_extent(inode, NULL, 0, sectorsize, 2 * sectorsize, 0);
9110e30db86SJosef Bacik 	if (IS_ERR(em)) {
9120e30db86SJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
9130e30db86SJosef Bacik 		goto out;
9140e30db86SJosef Bacik 	}
915b9ef22deSFeifei Xu 	if (em->block_start != sectorsize) {
9160e30db86SJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
9170e30db86SJosef Bacik 		goto out;
9180e30db86SJosef Bacik 	}
919b9ef22deSFeifei Xu 	if (em->start != sectorsize || em->len != sectorsize) {
920b9ef22deSFeifei Xu 		test_msg("Unexpected extent wanted start %u len %u, "
921b9ef22deSFeifei Xu 			"got start %llu len %llu\n",
922b9ef22deSFeifei Xu 			sectorsize, sectorsize, em->start, em->len);
9230e30db86SJosef Bacik 		goto out;
9240e30db86SJosef Bacik 	}
9250e30db86SJosef Bacik 	if (em->flags != 0) {
9260e30db86SJosef Bacik 		test_msg("Unexpected flags set, wanted 0 got %lu\n",
9270e30db86SJosef Bacik 			 em->flags);
9280e30db86SJosef Bacik 		goto out;
9290e30db86SJosef Bacik 	}
9300e30db86SJosef Bacik 	ret = 0;
9310e30db86SJosef Bacik out:
9320e30db86SJosef Bacik 	if (!IS_ERR(em))
9330e30db86SJosef Bacik 		free_extent_map(em);
9340e30db86SJosef Bacik 	iput(inode);
935faa2dbf0SJosef Bacik 	btrfs_free_dummy_root(root);
9367c0260eeSJeff Mahoney 	btrfs_free_dummy_fs_info(fs_info);
9370e30db86SJosef Bacik 	return ret;
9380e30db86SJosef Bacik }
9390e30db86SJosef Bacik 
940b9ef22deSFeifei Xu static int test_extent_accounting(u32 sectorsize, u32 nodesize)
9416a3891c5SJosef Bacik {
9427c0260eeSJeff Mahoney 	struct btrfs_fs_info *fs_info = NULL;
9436a3891c5SJosef Bacik 	struct inode *inode = NULL;
9446a3891c5SJosef Bacik 	struct btrfs_root *root = NULL;
9456a3891c5SJosef Bacik 	int ret = -ENOMEM;
9466a3891c5SJosef Bacik 
9476a3891c5SJosef Bacik 	inode = btrfs_new_test_inode();
9486a3891c5SJosef Bacik 	if (!inode) {
9496a3891c5SJosef Bacik 		test_msg("Couldn't allocate inode\n");
9506a3891c5SJosef Bacik 		return ret;
9516a3891c5SJosef Bacik 	}
9526a3891c5SJosef Bacik 
9537c0260eeSJeff Mahoney 	fs_info = btrfs_alloc_dummy_fs_info();
9547c0260eeSJeff Mahoney 	if (!fs_info) {
9557c0260eeSJeff Mahoney 		test_msg("Couldn't allocate dummy fs info\n");
9566a3891c5SJosef Bacik 		goto out;
9576a3891c5SJosef Bacik 	}
9586a3891c5SJosef Bacik 
9597c0260eeSJeff Mahoney 	root = btrfs_alloc_dummy_root(fs_info, sectorsize, nodesize);
9607c0260eeSJeff Mahoney 	if (IS_ERR(root)) {
9617c0260eeSJeff Mahoney 		test_msg("Couldn't allocate root\n");
9626a3891c5SJosef Bacik 		goto out;
9636a3891c5SJosef Bacik 	}
9646a3891c5SJosef Bacik 
9656a3891c5SJosef Bacik 	BTRFS_I(inode)->root = root;
9666a3891c5SJosef Bacik 	btrfs_test_inode_set_ops(inode);
9676a3891c5SJosef Bacik 
9686a3891c5SJosef Bacik 	/* [BTRFS_MAX_EXTENT_SIZE] */
9696a3891c5SJosef Bacik 	BTRFS_I(inode)->outstanding_extents++;
9706a3891c5SJosef Bacik 	ret = btrfs_set_extent_delalloc(inode, 0, BTRFS_MAX_EXTENT_SIZE - 1,
9716a3891c5SJosef Bacik 					NULL);
9726a3891c5SJosef Bacik 	if (ret) {
9736a3891c5SJosef Bacik 		test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
9746a3891c5SJosef Bacik 		goto out;
9756a3891c5SJosef Bacik 	}
9766a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 1) {
9776a3891c5SJosef Bacik 		ret = -EINVAL;
9786a3891c5SJosef Bacik 		test_msg("Miscount, wanted 1, got %u\n",
9796a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
9806a3891c5SJosef Bacik 		goto out;
9816a3891c5SJosef Bacik 	}
9826a3891c5SJosef Bacik 
983b9ef22deSFeifei Xu 	/* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
9846a3891c5SJosef Bacik 	BTRFS_I(inode)->outstanding_extents++;
9856a3891c5SJosef Bacik 	ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE,
986b9ef22deSFeifei Xu 					BTRFS_MAX_EXTENT_SIZE + sectorsize - 1,
987b9ef22deSFeifei Xu 					NULL);
9886a3891c5SJosef Bacik 	if (ret) {
9896a3891c5SJosef Bacik 		test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
9906a3891c5SJosef Bacik 		goto out;
9916a3891c5SJosef Bacik 	}
9926a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 2) {
9936a3891c5SJosef Bacik 		ret = -EINVAL;
9946a3891c5SJosef Bacik 		test_msg("Miscount, wanted 2, got %u\n",
9956a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
9966a3891c5SJosef Bacik 		goto out;
9976a3891c5SJosef Bacik 	}
9986a3891c5SJosef Bacik 
999b9ef22deSFeifei Xu 	/* [BTRFS_MAX_EXTENT_SIZE/2][sectorsize HOLE][the rest] */
10006a3891c5SJosef Bacik 	ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
10016a3891c5SJosef Bacik 			       BTRFS_MAX_EXTENT_SIZE >> 1,
1002b9ef22deSFeifei Xu 			       (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1,
10036a3891c5SJosef Bacik 			       EXTENT_DELALLOC | EXTENT_DIRTY |
10046a3891c5SJosef Bacik 			       EXTENT_UPTODATE | EXTENT_DO_ACCOUNTING, 0, 0,
10058cce83baSDavid Sterba 			       NULL, GFP_KERNEL);
10066a3891c5SJosef Bacik 	if (ret) {
10076a3891c5SJosef Bacik 		test_msg("clear_extent_bit returned %d\n", ret);
10086a3891c5SJosef Bacik 		goto out;
10096a3891c5SJosef Bacik 	}
10106a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 2) {
10116a3891c5SJosef Bacik 		ret = -EINVAL;
10126a3891c5SJosef Bacik 		test_msg("Miscount, wanted 2, got %u\n",
10136a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10146a3891c5SJosef Bacik 		goto out;
10156a3891c5SJosef Bacik 	}
10166a3891c5SJosef Bacik 
1017b9ef22deSFeifei Xu 	/* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
10186a3891c5SJosef Bacik 	BTRFS_I(inode)->outstanding_extents++;
10196a3891c5SJosef Bacik 	ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE >> 1,
1020b9ef22deSFeifei Xu 					(BTRFS_MAX_EXTENT_SIZE >> 1)
1021b9ef22deSFeifei Xu 					+ sectorsize - 1,
10226a3891c5SJosef Bacik 					NULL);
10236a3891c5SJosef Bacik 	if (ret) {
10246a3891c5SJosef Bacik 		test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
10256a3891c5SJosef Bacik 		goto out;
10266a3891c5SJosef Bacik 	}
10276a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 2) {
10286a3891c5SJosef Bacik 		ret = -EINVAL;
10296a3891c5SJosef Bacik 		test_msg("Miscount, wanted 2, got %u\n",
10306a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10316a3891c5SJosef Bacik 		goto out;
10326a3891c5SJosef Bacik 	}
10336a3891c5SJosef Bacik 
10346a3891c5SJosef Bacik 	/*
1035b9ef22deSFeifei Xu 	 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize]
10366a3891c5SJosef Bacik 	 *
10376a3891c5SJosef Bacik 	 * I'm artificially adding 2 to outstanding_extents because in the
10386a3891c5SJosef Bacik 	 * buffered IO case we'd add things up as we go, but I don't feel like
10396a3891c5SJosef Bacik 	 * doing that here, this isn't the interesting case we want to test.
10406a3891c5SJosef Bacik 	 */
10416a3891c5SJosef Bacik 	BTRFS_I(inode)->outstanding_extents += 2;
1042b9ef22deSFeifei Xu 	ret = btrfs_set_extent_delalloc(inode,
1043b9ef22deSFeifei Xu 			BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize,
1044b9ef22deSFeifei Xu 			(BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1,
10456a3891c5SJosef Bacik 			NULL);
10466a3891c5SJosef Bacik 	if (ret) {
10476a3891c5SJosef Bacik 		test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
10486a3891c5SJosef Bacik 		goto out;
10496a3891c5SJosef Bacik 	}
10506a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 4) {
10516a3891c5SJosef Bacik 		ret = -EINVAL;
10526a3891c5SJosef Bacik 		test_msg("Miscount, wanted 4, got %u\n",
10536a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10546a3891c5SJosef Bacik 		goto out;
10556a3891c5SJosef Bacik 	}
10566a3891c5SJosef Bacik 
1057b9ef22deSFeifei Xu 	/*
1058b9ef22deSFeifei Xu 	* [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize]
1059b9ef22deSFeifei Xu 	*/
10606a3891c5SJosef Bacik 	BTRFS_I(inode)->outstanding_extents++;
1061b9ef22deSFeifei Xu 	ret = btrfs_set_extent_delalloc(inode,
1062b9ef22deSFeifei Xu 			BTRFS_MAX_EXTENT_SIZE + sectorsize,
1063b9ef22deSFeifei Xu 			BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, NULL);
10646a3891c5SJosef Bacik 	if (ret) {
10656a3891c5SJosef Bacik 		test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
10666a3891c5SJosef Bacik 		goto out;
10676a3891c5SJosef Bacik 	}
10686a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 3) {
10696a3891c5SJosef Bacik 		ret = -EINVAL;
10706a3891c5SJosef Bacik 		test_msg("Miscount, wanted 3, got %u\n",
10716a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10726a3891c5SJosef Bacik 		goto out;
10736a3891c5SJosef Bacik 	}
10746a3891c5SJosef Bacik 
10756a3891c5SJosef Bacik 	/* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */
10766a3891c5SJosef Bacik 	ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
1077b9ef22deSFeifei Xu 			       BTRFS_MAX_EXTENT_SIZE + sectorsize,
1078b9ef22deSFeifei Xu 			       BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1,
10796a3891c5SJosef Bacik 			       EXTENT_DIRTY | EXTENT_DELALLOC |
10806a3891c5SJosef Bacik 			       EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0,
10818cce83baSDavid Sterba 			       NULL, GFP_KERNEL);
10826a3891c5SJosef Bacik 	if (ret) {
10836a3891c5SJosef Bacik 		test_msg("clear_extent_bit returned %d\n", ret);
10846a3891c5SJosef Bacik 		goto out;
10856a3891c5SJosef Bacik 	}
10866a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 4) {
10876a3891c5SJosef Bacik 		ret = -EINVAL;
10886a3891c5SJosef Bacik 		test_msg("Miscount, wanted 4, got %u\n",
10896a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
10906a3891c5SJosef Bacik 		goto out;
10916a3891c5SJosef Bacik 	}
10926a3891c5SJosef Bacik 
10936a3891c5SJosef Bacik 	/*
10946a3891c5SJosef Bacik 	 * Refill the hole again just for good measure, because I thought it
10956a3891c5SJosef Bacik 	 * might fail and I'd rather satisfy my paranoia at this point.
10966a3891c5SJosef Bacik 	 */
10976a3891c5SJosef Bacik 	BTRFS_I(inode)->outstanding_extents++;
1098b9ef22deSFeifei Xu 	ret = btrfs_set_extent_delalloc(inode,
1099b9ef22deSFeifei Xu 			BTRFS_MAX_EXTENT_SIZE + sectorsize,
1100b9ef22deSFeifei Xu 			BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, NULL);
11016a3891c5SJosef Bacik 	if (ret) {
11026a3891c5SJosef Bacik 		test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
11036a3891c5SJosef Bacik 		goto out;
11046a3891c5SJosef Bacik 	}
11056a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents != 3) {
11066a3891c5SJosef Bacik 		ret = -EINVAL;
11076a3891c5SJosef Bacik 		test_msg("Miscount, wanted 3, got %u\n",
11086a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
11096a3891c5SJosef Bacik 		goto out;
11106a3891c5SJosef Bacik 	}
11116a3891c5SJosef Bacik 
11126a3891c5SJosef Bacik 	/* Empty */
11136a3891c5SJosef Bacik 	ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
11146a3891c5SJosef Bacik 			       EXTENT_DIRTY | EXTENT_DELALLOC |
11156a3891c5SJosef Bacik 			       EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0,
11168cce83baSDavid Sterba 			       NULL, GFP_KERNEL);
11176a3891c5SJosef Bacik 	if (ret) {
11186a3891c5SJosef Bacik 		test_msg("clear_extent_bit returned %d\n", ret);
11196a3891c5SJosef Bacik 		goto out;
11206a3891c5SJosef Bacik 	}
11216a3891c5SJosef Bacik 	if (BTRFS_I(inode)->outstanding_extents) {
11226a3891c5SJosef Bacik 		ret = -EINVAL;
11236a3891c5SJosef Bacik 		test_msg("Miscount, wanted 0, got %u\n",
11246a3891c5SJosef Bacik 			 BTRFS_I(inode)->outstanding_extents);
11256a3891c5SJosef Bacik 		goto out;
11266a3891c5SJosef Bacik 	}
11276a3891c5SJosef Bacik 	ret = 0;
11286a3891c5SJosef Bacik out:
11296a3891c5SJosef Bacik 	if (ret)
11306a3891c5SJosef Bacik 		clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
11316a3891c5SJosef Bacik 				 EXTENT_DIRTY | EXTENT_DELALLOC |
11326a3891c5SJosef Bacik 				 EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0,
11338cce83baSDavid Sterba 				 NULL, GFP_KERNEL);
11346a3891c5SJosef Bacik 	iput(inode);
11356a3891c5SJosef Bacik 	btrfs_free_dummy_root(root);
11367c0260eeSJeff Mahoney 	btrfs_free_dummy_fs_info(fs_info);
11376a3891c5SJosef Bacik 	return ret;
11386a3891c5SJosef Bacik }
11396a3891c5SJosef Bacik 
1140b9ef22deSFeifei Xu int btrfs_test_inodes(u32 sectorsize, u32 nodesize)
1141aaedb55bSJosef Bacik {
11420e30db86SJosef Bacik 	int ret;
11430e30db86SJosef Bacik 
11440e30db86SJosef Bacik 	set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
11450e30db86SJosef Bacik 	set_bit(EXTENT_FLAG_VACANCY, &vacancy_only);
11460e30db86SJosef Bacik 	set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
11470e30db86SJosef Bacik 
1148aaedb55bSJosef Bacik 	test_msg("Running btrfs_get_extent tests\n");
1149b9ef22deSFeifei Xu 	ret = test_btrfs_get_extent(sectorsize, nodesize);
11500e30db86SJosef Bacik 	if (ret)
11510e30db86SJosef Bacik 		return ret;
11520e30db86SJosef Bacik 	test_msg("Running hole first btrfs_get_extent test\n");
1153b9ef22deSFeifei Xu 	ret = test_hole_first(sectorsize, nodesize);
11546a3891c5SJosef Bacik 	if (ret)
11556a3891c5SJosef Bacik 		return ret;
11566a3891c5SJosef Bacik 	test_msg("Running outstanding_extents tests\n");
1157b9ef22deSFeifei Xu 	return test_extent_accounting(sectorsize, nodesize);
1158aaedb55bSJosef Bacik }
1159