xref: /openbmc/linux/fs/btrfs/tests/inode-tests.c (revision faa2dbf0)
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 
19aaedb55bSJosef Bacik #include "btrfs-tests.h"
20aaedb55bSJosef Bacik #include "../ctree.h"
21aaedb55bSJosef Bacik #include "../btrfs_inode.h"
22aaedb55bSJosef Bacik #include "../disk-io.h"
23aaedb55bSJosef Bacik #include "../extent_io.h"
24aaedb55bSJosef Bacik #include "../volumes.h"
25aaedb55bSJosef Bacik 
26aaedb55bSJosef Bacik static void insert_extent(struct btrfs_root *root, u64 start, u64 len,
27aaedb55bSJosef Bacik 			  u64 ram_bytes, u64 offset, u64 disk_bytenr,
28aaedb55bSJosef Bacik 			  u64 disk_len, u32 type, u8 compression, int slot)
29aaedb55bSJosef Bacik {
30aaedb55bSJosef Bacik 	struct btrfs_path path;
31aaedb55bSJosef Bacik 	struct btrfs_file_extent_item *fi;
32aaedb55bSJosef Bacik 	struct extent_buffer *leaf = root->node;
33aaedb55bSJosef Bacik 	struct btrfs_key key;
34aaedb55bSJosef Bacik 	u32 value_len = sizeof(struct btrfs_file_extent_item);
35aaedb55bSJosef Bacik 
36aaedb55bSJosef Bacik 	if (type == BTRFS_FILE_EXTENT_INLINE)
37aaedb55bSJosef Bacik 		value_len += len;
38aaedb55bSJosef Bacik 	memset(&path, 0, sizeof(path));
39aaedb55bSJosef Bacik 
40aaedb55bSJosef Bacik 	path.nodes[0] = leaf;
41aaedb55bSJosef Bacik 	path.slots[0] = slot;
42aaedb55bSJosef Bacik 
43aaedb55bSJosef Bacik 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
44aaedb55bSJosef Bacik 	key.type = BTRFS_EXTENT_DATA_KEY;
45aaedb55bSJosef Bacik 	key.offset = start;
46aaedb55bSJosef Bacik 
47aaedb55bSJosef Bacik 	setup_items_for_insert(root, &path, &key, &value_len, value_len,
48aaedb55bSJosef Bacik 			       value_len + sizeof(struct btrfs_item), 1);
49aaedb55bSJosef Bacik 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
50aaedb55bSJosef Bacik 	btrfs_set_file_extent_generation(leaf, fi, 1);
51aaedb55bSJosef Bacik 	btrfs_set_file_extent_type(leaf, fi, type);
52aaedb55bSJosef Bacik 	btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
53aaedb55bSJosef Bacik 	btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_len);
54aaedb55bSJosef Bacik 	btrfs_set_file_extent_offset(leaf, fi, offset);
55aaedb55bSJosef Bacik 	btrfs_set_file_extent_num_bytes(leaf, fi, len);
56aaedb55bSJosef Bacik 	btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
57aaedb55bSJosef Bacik 	btrfs_set_file_extent_compression(leaf, fi, compression);
58aaedb55bSJosef Bacik 	btrfs_set_file_extent_encryption(leaf, fi, 0);
59aaedb55bSJosef Bacik 	btrfs_set_file_extent_other_encoding(leaf, fi, 0);
60aaedb55bSJosef Bacik }
61aaedb55bSJosef Bacik 
620e30db86SJosef Bacik static void insert_inode_item_key(struct btrfs_root *root)
630e30db86SJosef Bacik {
640e30db86SJosef Bacik 	struct btrfs_path path;
650e30db86SJosef Bacik 	struct extent_buffer *leaf = root->node;
660e30db86SJosef Bacik 	struct btrfs_key key;
670e30db86SJosef Bacik 	u32 value_len = 0;
680e30db86SJosef Bacik 
690e30db86SJosef Bacik 	memset(&path, 0, sizeof(path));
700e30db86SJosef Bacik 
710e30db86SJosef Bacik 	path.nodes[0] = leaf;
720e30db86SJosef Bacik 	path.slots[0] = 0;
730e30db86SJosef Bacik 
740e30db86SJosef Bacik 	key.objectid = BTRFS_INODE_ITEM_KEY;
750e30db86SJosef Bacik 	key.type = BTRFS_INODE_ITEM_KEY;
760e30db86SJosef Bacik 	key.offset = 0;
770e30db86SJosef Bacik 
780e30db86SJosef Bacik 	setup_items_for_insert(root, &path, &key, &value_len, value_len,
790e30db86SJosef Bacik 			       value_len + sizeof(struct btrfs_item), 1);
800e30db86SJosef Bacik }
810e30db86SJosef Bacik 
82aaedb55bSJosef Bacik /*
83aaedb55bSJosef Bacik  * Build the most complicated map of extents the earth has ever seen.  We want
84aaedb55bSJosef Bacik  * this so we can test all of the corner cases of btrfs_get_extent.  Here is a
85aaedb55bSJosef Bacik  * diagram of how the extents will look though this may not be possible we still
86aaedb55bSJosef Bacik  * want to make sure everything acts normally (the last number is not inclusive)
87aaedb55bSJosef Bacik  *
88aaedb55bSJosef Bacik  * [0 - 5][5 -  6][6 - 10][10 - 4096][  4096 - 8192 ][8192 - 12288]
89aaedb55bSJosef Bacik  * [hole ][inline][ hole ][ regular ][regular1 split][    hole    ]
90aaedb55bSJosef Bacik  *
91aaedb55bSJosef Bacik  * [ 12288 - 20480][20480 - 24576][  24576 - 28672  ][28672 - 36864][36864 - 45056]
92aaedb55bSJosef Bacik  * [regular1 split][   prealloc1 ][prealloc1 written][   prealloc1 ][ compressed  ]
93aaedb55bSJosef Bacik  *
94aaedb55bSJosef Bacik  * [45056 - 49152][49152-53248][53248-61440][61440-65536][     65536+81920   ]
95aaedb55bSJosef Bacik  * [ compressed1 ][  regular  ][compressed1][  regular  ][ hole but no extent]
96aaedb55bSJosef Bacik  *
97aaedb55bSJosef Bacik  * [81920-86016]
98aaedb55bSJosef Bacik  * [  regular  ]
99aaedb55bSJosef Bacik  */
100aaedb55bSJosef Bacik static void setup_file_extents(struct btrfs_root *root)
101aaedb55bSJosef Bacik {
102aaedb55bSJosef Bacik 	int slot = 0;
103aaedb55bSJosef Bacik 	u64 disk_bytenr = 1 * 1024 * 1024;
104aaedb55bSJosef Bacik 	u64 offset = 0;
105aaedb55bSJosef Bacik 
106aaedb55bSJosef Bacik 	/* First we want a hole */
107aaedb55bSJosef Bacik 	insert_extent(root, offset, 5, 5, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
108aaedb55bSJosef Bacik 		      slot);
109aaedb55bSJosef Bacik 	slot++;
110aaedb55bSJosef Bacik 	offset += 5;
111aaedb55bSJosef Bacik 
112aaedb55bSJosef Bacik 	/*
113aaedb55bSJosef Bacik 	 * Now we want an inline extent, I don't think this is possible but hey
114aaedb55bSJosef Bacik 	 * why not?  Also keep in mind if we have an inline extent it counts as
115aaedb55bSJosef Bacik 	 * the whole first page.  If we were to expand it we would have to cow
116aaedb55bSJosef Bacik 	 * and we wouldn't have an inline extent anymore.
117aaedb55bSJosef Bacik 	 */
118aaedb55bSJosef Bacik 	insert_extent(root, offset, 1, 1, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE, 0,
119aaedb55bSJosef Bacik 		      slot);
120aaedb55bSJosef Bacik 	slot++;
121aaedb55bSJosef Bacik 	offset = 4096;
122aaedb55bSJosef Bacik 
123aaedb55bSJosef Bacik 	/* Now another hole */
124aaedb55bSJosef Bacik 	insert_extent(root, offset, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
125aaedb55bSJosef Bacik 		      slot);
126aaedb55bSJosef Bacik 	slot++;
127aaedb55bSJosef Bacik 	offset += 4;
128aaedb55bSJosef Bacik 
129aaedb55bSJosef Bacik 	/* Now for a regular extent */
130aaedb55bSJosef Bacik 	insert_extent(root, offset, 4095, 4095, 0, disk_bytenr, 4096,
131aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
132aaedb55bSJosef Bacik 	slot++;
133aaedb55bSJosef Bacik 	disk_bytenr += 4096;
134aaedb55bSJosef Bacik 	offset += 4095;
135aaedb55bSJosef Bacik 
136aaedb55bSJosef Bacik 	/*
137aaedb55bSJosef Bacik 	 * Now for 3 extents that were split from a hole punch so we test
138aaedb55bSJosef Bacik 	 * offsets properly.
139aaedb55bSJosef Bacik 	 */
140aaedb55bSJosef Bacik 	insert_extent(root, offset, 4096, 16384, 0, disk_bytenr, 16384,
141aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
142aaedb55bSJosef Bacik 	slot++;
143aaedb55bSJosef Bacik 	offset += 4096;
144aaedb55bSJosef Bacik 	insert_extent(root, offset, 4096, 4096, 0, 0, 0, BTRFS_FILE_EXTENT_REG,
145aaedb55bSJosef Bacik 		      0, slot);
146aaedb55bSJosef Bacik 	slot++;
147aaedb55bSJosef Bacik 	offset += 4096;
148aaedb55bSJosef Bacik 	insert_extent(root, offset, 8192, 16384, 8192, disk_bytenr, 16384,
149aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
150aaedb55bSJosef Bacik 	slot++;
151aaedb55bSJosef Bacik 	offset += 8192;
152aaedb55bSJosef Bacik 	disk_bytenr += 16384;
153aaedb55bSJosef Bacik 
154aaedb55bSJosef Bacik 	/* Now for a unwritten prealloc extent */
155aaedb55bSJosef Bacik 	insert_extent(root, offset, 4096, 4096, 0, disk_bytenr, 4096,
156aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
157aaedb55bSJosef Bacik 	slot++;
158aaedb55bSJosef Bacik 	offset += 4096;
159aaedb55bSJosef Bacik 
160aaedb55bSJosef Bacik 	/*
161aaedb55bSJosef Bacik 	 * We want to jack up disk_bytenr a little more so the em stuff doesn't
162aaedb55bSJosef Bacik 	 * merge our records.
163aaedb55bSJosef Bacik 	 */
164aaedb55bSJosef Bacik 	disk_bytenr += 8192;
165aaedb55bSJosef Bacik 
166aaedb55bSJosef Bacik 	/*
167aaedb55bSJosef Bacik 	 * Now for a partially written prealloc extent, basically the same as
168aaedb55bSJosef Bacik 	 * the hole punch example above.  Ram_bytes never changes when you mark
169aaedb55bSJosef Bacik 	 * extents written btw.
170aaedb55bSJosef Bacik 	 */
171aaedb55bSJosef Bacik 	insert_extent(root, offset, 4096, 16384, 0, disk_bytenr, 16384,
172aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
173aaedb55bSJosef Bacik 	slot++;
174aaedb55bSJosef Bacik 	offset += 4096;
175aaedb55bSJosef Bacik 	insert_extent(root, offset, 4096, 16384, 4096, disk_bytenr, 16384,
176aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
177aaedb55bSJosef Bacik 	slot++;
178aaedb55bSJosef Bacik 	offset += 4096;
179aaedb55bSJosef Bacik 	insert_extent(root, offset, 8192, 16384, 8192, disk_bytenr, 16384,
180aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
181aaedb55bSJosef Bacik 	slot++;
182aaedb55bSJosef Bacik 	offset += 8192;
183aaedb55bSJosef Bacik 	disk_bytenr += 16384;
184aaedb55bSJosef Bacik 
185aaedb55bSJosef Bacik 	/* Now a normal compressed extent */
186aaedb55bSJosef Bacik 	insert_extent(root, offset, 8192, 8192, 0, disk_bytenr, 4096,
187aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
188aaedb55bSJosef Bacik 	slot++;
189aaedb55bSJosef Bacik 	offset += 8192;
190aaedb55bSJosef Bacik 	/* No merges */
191aaedb55bSJosef Bacik 	disk_bytenr += 8192;
192aaedb55bSJosef Bacik 
193aaedb55bSJosef Bacik 	/* Now a split compressed extent */
194aaedb55bSJosef Bacik 	insert_extent(root, offset, 4096, 16384, 0, disk_bytenr, 4096,
195aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
196aaedb55bSJosef Bacik 	slot++;
197aaedb55bSJosef Bacik 	offset += 4096;
198aaedb55bSJosef Bacik 	insert_extent(root, offset, 4096, 4096, 0, disk_bytenr + 4096, 4096,
199aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
200aaedb55bSJosef Bacik 	slot++;
201aaedb55bSJosef Bacik 	offset += 4096;
202aaedb55bSJosef Bacik 	insert_extent(root, offset, 8192, 16384, 8192, disk_bytenr, 4096,
203aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
204aaedb55bSJosef Bacik 	slot++;
205aaedb55bSJosef Bacik 	offset += 8192;
206aaedb55bSJosef Bacik 	disk_bytenr += 8192;
207aaedb55bSJosef Bacik 
208aaedb55bSJosef Bacik 	/* Now extents that have a hole but no hole extent */
209aaedb55bSJosef Bacik 	insert_extent(root, offset, 4096, 4096, 0, disk_bytenr, 4096,
210aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
211aaedb55bSJosef Bacik 	slot++;
212aaedb55bSJosef Bacik 	offset += 16384;
213aaedb55bSJosef Bacik 	disk_bytenr += 4096;
214aaedb55bSJosef Bacik 	insert_extent(root, offset, 4096, 4096, 0, disk_bytenr, 4096,
215aaedb55bSJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, slot);
216aaedb55bSJosef Bacik }
217aaedb55bSJosef Bacik 
218aaedb55bSJosef Bacik static unsigned long prealloc_only = 0;
219aaedb55bSJosef Bacik static unsigned long compressed_only = 0;
220aaedb55bSJosef Bacik static unsigned long vacancy_only = 0;
221aaedb55bSJosef Bacik 
222aaedb55bSJosef Bacik static noinline int test_btrfs_get_extent(void)
223aaedb55bSJosef Bacik {
224aaedb55bSJosef Bacik 	struct inode *inode = NULL;
225aaedb55bSJosef Bacik 	struct btrfs_root *root = NULL;
226aaedb55bSJosef Bacik 	struct extent_map *em = NULL;
227aaedb55bSJosef Bacik 	u64 orig_start;
228aaedb55bSJosef Bacik 	u64 disk_bytenr;
229aaedb55bSJosef Bacik 	u64 offset;
230aaedb55bSJosef Bacik 	int ret = -ENOMEM;
231aaedb55bSJosef Bacik 
232aaedb55bSJosef Bacik 	inode = btrfs_new_test_inode();
233aaedb55bSJosef Bacik 	if (!inode) {
234aaedb55bSJosef Bacik 		test_msg("Couldn't allocate inode\n");
235aaedb55bSJosef Bacik 		return ret;
236aaedb55bSJosef Bacik 	}
237aaedb55bSJosef Bacik 
238aaedb55bSJosef Bacik 	BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
239aaedb55bSJosef Bacik 	BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
240aaedb55bSJosef Bacik 	BTRFS_I(inode)->location.offset = 0;
241aaedb55bSJosef Bacik 
242aaedb55bSJosef Bacik 	root = btrfs_alloc_dummy_root();
243aaedb55bSJosef Bacik 	if (IS_ERR(root)) {
244aaedb55bSJosef Bacik 		test_msg("Couldn't allocate root\n");
245aaedb55bSJosef Bacik 		goto out;
246aaedb55bSJosef Bacik 	}
247aaedb55bSJosef Bacik 
248aaedb55bSJosef Bacik 	/*
249aaedb55bSJosef Bacik 	 * We do this since btrfs_get_extent wants to assign em->bdev to
250aaedb55bSJosef Bacik 	 * root->fs_info->fs_devices->latest_bdev.
251aaedb55bSJosef Bacik 	 */
252faa2dbf0SJosef Bacik 	root->fs_info = btrfs_alloc_dummy_fs_info();
253aaedb55bSJosef Bacik 	if (!root->fs_info) {
254aaedb55bSJosef Bacik 		test_msg("Couldn't allocate dummy fs info\n");
255aaedb55bSJosef Bacik 		goto out;
256aaedb55bSJosef Bacik 	}
257aaedb55bSJosef Bacik 
258aaedb55bSJosef Bacik 	root->node = alloc_dummy_extent_buffer(0, 4096);
259aaedb55bSJosef Bacik 	if (!root->node) {
260aaedb55bSJosef Bacik 		test_msg("Couldn't allocate dummy buffer\n");
261aaedb55bSJosef Bacik 		goto out;
262aaedb55bSJosef Bacik 	}
263aaedb55bSJosef Bacik 
264aaedb55bSJosef Bacik 	/*
265aaedb55bSJosef Bacik 	 * We will just free a dummy node if it's ref count is 2 so we need an
266aaedb55bSJosef Bacik 	 * extra ref so our searches don't accidently release our page.
267aaedb55bSJosef Bacik 	 */
268aaedb55bSJosef Bacik 	extent_buffer_get(root->node);
269aaedb55bSJosef Bacik 	btrfs_set_header_nritems(root->node, 0);
270aaedb55bSJosef Bacik 	btrfs_set_header_level(root->node, 0);
271aaedb55bSJosef Bacik 	ret = -EINVAL;
272aaedb55bSJosef Bacik 
273aaedb55bSJosef Bacik 	/* First with no extents */
274aaedb55bSJosef Bacik 	BTRFS_I(inode)->root = root;
275aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, 0, 4096, 0);
276aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
277aaedb55bSJosef Bacik 		em = NULL;
278aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
279aaedb55bSJosef Bacik 		goto out;
280aaedb55bSJosef Bacik 	}
281aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
282aaedb55bSJosef Bacik 		test_msg("Expected a hole, got %llu\n", em->block_start);
283aaedb55bSJosef Bacik 		goto out;
284aaedb55bSJosef Bacik 	}
285aaedb55bSJosef Bacik 	if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
286aaedb55bSJosef Bacik 		test_msg("Vacancy flag wasn't set properly\n");
287aaedb55bSJosef Bacik 		goto out;
288aaedb55bSJosef Bacik 	}
289aaedb55bSJosef Bacik 	free_extent_map(em);
290aaedb55bSJosef Bacik 	btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
291aaedb55bSJosef Bacik 
292aaedb55bSJosef Bacik 	/*
293aaedb55bSJosef Bacik 	 * All of the magic numbers are based on the mapping setup in
294aaedb55bSJosef Bacik 	 * setup_file_extents, so if you change anything there you need to
295aaedb55bSJosef Bacik 	 * update the comment and update the expected values below.
296aaedb55bSJosef Bacik 	 */
297aaedb55bSJosef Bacik 	setup_file_extents(root);
298aaedb55bSJosef Bacik 
299aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, 0, (u64)-1, 0);
300aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
301aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
302aaedb55bSJosef Bacik 		goto out;
303aaedb55bSJosef Bacik 	}
304aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
305aaedb55bSJosef Bacik 		test_msg("Expected a hole, got %llu\n", em->block_start);
306aaedb55bSJosef Bacik 		goto out;
307aaedb55bSJosef Bacik 	}
308aaedb55bSJosef Bacik 	if (em->start != 0 || em->len != 5) {
309aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start 0 len 5, got start "
310aaedb55bSJosef Bacik 			 "%llu len %llu\n", em->start, em->len);
311aaedb55bSJosef Bacik 		goto out;
312aaedb55bSJosef Bacik 	}
313aaedb55bSJosef Bacik 	if (em->flags != 0) {
314aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
315aaedb55bSJosef Bacik 		goto out;
316aaedb55bSJosef Bacik 	}
317aaedb55bSJosef Bacik 	offset = em->start + em->len;
318aaedb55bSJosef Bacik 	free_extent_map(em);
319aaedb55bSJosef Bacik 
320aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
321aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
322aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
323aaedb55bSJosef Bacik 		goto out;
324aaedb55bSJosef Bacik 	}
325aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_INLINE) {
326aaedb55bSJosef Bacik 		test_msg("Expected an inline, got %llu\n", em->block_start);
327aaedb55bSJosef Bacik 		goto out;
328aaedb55bSJosef Bacik 	}
329aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 4091) {
330aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 1, got start "
331aaedb55bSJosef Bacik 			 "%llu len %llu\n", offset, em->start, em->len);
332aaedb55bSJosef Bacik 		goto out;
333aaedb55bSJosef Bacik 	}
334aaedb55bSJosef Bacik 	if (em->flags != 0) {
335aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
336aaedb55bSJosef Bacik 		goto out;
337aaedb55bSJosef Bacik 	}
338aaedb55bSJosef Bacik 	/*
339aaedb55bSJosef Bacik 	 * We don't test anything else for inline since it doesn't get set
340aaedb55bSJosef Bacik 	 * unless we have a page for it to write into.  Maybe we should change
341aaedb55bSJosef Bacik 	 * this?
342aaedb55bSJosef Bacik 	 */
343aaedb55bSJosef Bacik 	offset = em->start + em->len;
344aaedb55bSJosef Bacik 	free_extent_map(em);
345aaedb55bSJosef Bacik 
346aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
347aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
348aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
349aaedb55bSJosef Bacik 		goto out;
350aaedb55bSJosef Bacik 	}
351aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
352aaedb55bSJosef Bacik 		test_msg("Expected a hole, got %llu\n", em->block_start);
353aaedb55bSJosef Bacik 		goto out;
354aaedb55bSJosef Bacik 	}
355aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 4) {
356aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 4, got start "
357aaedb55bSJosef Bacik 			 "%llu len %llu\n", offset, em->start, em->len);
358aaedb55bSJosef Bacik 		goto out;
359aaedb55bSJosef Bacik 	}
360aaedb55bSJosef Bacik 	if (em->flags != 0) {
361aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
362aaedb55bSJosef Bacik 		goto out;
363aaedb55bSJosef Bacik 	}
364aaedb55bSJosef Bacik 	offset = em->start + em->len;
365aaedb55bSJosef Bacik 	free_extent_map(em);
366aaedb55bSJosef Bacik 
367aaedb55bSJosef Bacik 	/* Regular extent */
368aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
369aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
370aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
371aaedb55bSJosef Bacik 		goto out;
372aaedb55bSJosef Bacik 	}
373aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
374aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
375aaedb55bSJosef Bacik 		goto out;
376aaedb55bSJosef Bacik 	}
377aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 4095) {
378aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 4095, got "
379aaedb55bSJosef Bacik 			 "start %llu len %llu\n", offset, em->start, em->len);
380aaedb55bSJosef Bacik 		goto out;
381aaedb55bSJosef Bacik 	}
382aaedb55bSJosef Bacik 	if (em->flags != 0) {
383aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
384aaedb55bSJosef Bacik 		goto out;
385aaedb55bSJosef Bacik 	}
386aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
387aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
388aaedb55bSJosef Bacik 			 em->orig_start);
389aaedb55bSJosef Bacik 		goto out;
390aaedb55bSJosef Bacik 	}
391aaedb55bSJosef Bacik 	offset = em->start + em->len;
392aaedb55bSJosef Bacik 	free_extent_map(em);
393aaedb55bSJosef Bacik 
394aaedb55bSJosef Bacik 	/* The next 3 are split extents */
395aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
396aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
397aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
398aaedb55bSJosef Bacik 		goto out;
399aaedb55bSJosef Bacik 	}
400aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
401aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
402aaedb55bSJosef Bacik 		goto out;
403aaedb55bSJosef Bacik 	}
404aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 4096) {
405aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 4096, got "
406aaedb55bSJosef Bacik 			 "start %llu len %llu\n", offset, em->start, em->len);
407aaedb55bSJosef Bacik 		goto out;
408aaedb55bSJosef Bacik 	}
409aaedb55bSJosef Bacik 	if (em->flags != 0) {
410aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
411aaedb55bSJosef Bacik 		goto out;
412aaedb55bSJosef Bacik 	}
413aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
414aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
415aaedb55bSJosef Bacik 			 em->orig_start);
416aaedb55bSJosef Bacik 		goto out;
417aaedb55bSJosef Bacik 	}
418aaedb55bSJosef Bacik 	disk_bytenr = em->block_start;
419aaedb55bSJosef Bacik 	orig_start = em->start;
420aaedb55bSJosef Bacik 	offset = em->start + em->len;
421aaedb55bSJosef Bacik 	free_extent_map(em);
422aaedb55bSJosef Bacik 
423aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
424aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
425aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
426aaedb55bSJosef Bacik 		goto out;
427aaedb55bSJosef Bacik 	}
428aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
429aaedb55bSJosef Bacik 		test_msg("Expected a hole, got %llu\n", em->block_start);
430aaedb55bSJosef Bacik 		goto out;
431aaedb55bSJosef Bacik 	}
432aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 4096) {
433aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 4096, got "
434aaedb55bSJosef Bacik 			 "start %llu len %llu\n", offset, em->start, em->len);
435aaedb55bSJosef Bacik 		goto out;
436aaedb55bSJosef Bacik 	}
437aaedb55bSJosef Bacik 	if (em->flags != 0) {
438aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
439aaedb55bSJosef Bacik 		goto out;
440aaedb55bSJosef Bacik 	}
441aaedb55bSJosef Bacik 	offset = em->start + em->len;
442aaedb55bSJosef Bacik 	free_extent_map(em);
443aaedb55bSJosef Bacik 
444aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
445aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
446aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
447aaedb55bSJosef Bacik 		goto out;
448aaedb55bSJosef Bacik 	}
449aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
450aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
451aaedb55bSJosef Bacik 		goto out;
452aaedb55bSJosef Bacik 	}
453aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 8192) {
454aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 8192, got "
455aaedb55bSJosef Bacik 			 "start %llu len %llu\n", offset, em->start, em->len);
456aaedb55bSJosef Bacik 		goto out;
457aaedb55bSJosef Bacik 	}
458aaedb55bSJosef Bacik 	if (em->flags != 0) {
459aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
460aaedb55bSJosef Bacik 		goto out;
461aaedb55bSJosef Bacik 	}
462aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
463aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n",
464aaedb55bSJosef Bacik 			 orig_start, em->orig_start);
465aaedb55bSJosef Bacik 		goto out;
466aaedb55bSJosef Bacik 	}
467aaedb55bSJosef Bacik 	disk_bytenr += (em->start - orig_start);
468aaedb55bSJosef Bacik 	if (em->block_start != disk_bytenr) {
469aaedb55bSJosef Bacik 		test_msg("Wrong block start, want %llu, have %llu\n",
470aaedb55bSJosef Bacik 			 disk_bytenr, em->block_start);
471aaedb55bSJosef Bacik 		goto out;
472aaedb55bSJosef Bacik 	}
473aaedb55bSJosef Bacik 	offset = em->start + em->len;
474aaedb55bSJosef Bacik 	free_extent_map(em);
475aaedb55bSJosef Bacik 
476aaedb55bSJosef Bacik 	/* Prealloc extent */
477aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
478aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
479aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
480aaedb55bSJosef Bacik 		goto out;
481aaedb55bSJosef Bacik 	}
482aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
483aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
484aaedb55bSJosef Bacik 		goto out;
485aaedb55bSJosef Bacik 	}
486aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 4096) {
487aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 4096, got "
488aaedb55bSJosef Bacik 			 "start %llu len %llu\n", offset, em->start, em->len);
489aaedb55bSJosef Bacik 		goto out;
490aaedb55bSJosef Bacik 	}
491aaedb55bSJosef Bacik 	if (em->flags != prealloc_only) {
492aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want %lu have %lu\n",
493aaedb55bSJosef Bacik 			 prealloc_only, em->flags);
494aaedb55bSJosef Bacik 		goto out;
495aaedb55bSJosef Bacik 	}
496aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
497aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
498aaedb55bSJosef Bacik 			 em->orig_start);
499aaedb55bSJosef Bacik 		goto out;
500aaedb55bSJosef Bacik 	}
501aaedb55bSJosef Bacik 	offset = em->start + em->len;
502aaedb55bSJosef Bacik 	free_extent_map(em);
503aaedb55bSJosef Bacik 
504aaedb55bSJosef Bacik 	/* The next 3 are a half written prealloc extent */
505aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
506aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
507aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
508aaedb55bSJosef Bacik 		goto out;
509aaedb55bSJosef Bacik 	}
510aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
511aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
512aaedb55bSJosef Bacik 		goto out;
513aaedb55bSJosef Bacik 	}
514aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 4096) {
515aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 4096, got "
516aaedb55bSJosef Bacik 			 "start %llu len %llu\n", offset, em->start, em->len);
517aaedb55bSJosef Bacik 		goto out;
518aaedb55bSJosef Bacik 	}
519aaedb55bSJosef Bacik 	if (em->flags != prealloc_only) {
520aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want %lu have %lu\n",
521aaedb55bSJosef Bacik 			 prealloc_only, em->flags);
522aaedb55bSJosef Bacik 		goto out;
523aaedb55bSJosef Bacik 	}
524aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
525aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
526aaedb55bSJosef Bacik 			 em->orig_start);
527aaedb55bSJosef Bacik 		goto out;
528aaedb55bSJosef Bacik 	}
529aaedb55bSJosef Bacik 	disk_bytenr = em->block_start;
530aaedb55bSJosef Bacik 	orig_start = em->start;
531aaedb55bSJosef Bacik 	offset = em->start + em->len;
532aaedb55bSJosef Bacik 	free_extent_map(em);
533aaedb55bSJosef Bacik 
534aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
535aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
536aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
537aaedb55bSJosef Bacik 		goto out;
538aaedb55bSJosef Bacik 	}
539aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_HOLE) {
540aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
541aaedb55bSJosef Bacik 		goto out;
542aaedb55bSJosef Bacik 	}
543aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 4096) {
544aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 4096, got "
545aaedb55bSJosef Bacik 			 "start %llu len %llu\n", offset, em->start, em->len);
546aaedb55bSJosef Bacik 		goto out;
547aaedb55bSJosef Bacik 	}
548aaedb55bSJosef Bacik 	if (em->flags != 0) {
549aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
550aaedb55bSJosef Bacik 		goto out;
551aaedb55bSJosef Bacik 	}
552aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
553aaedb55bSJosef Bacik 		test_msg("Unexpected orig offset, wanted %llu, have %llu\n",
554aaedb55bSJosef Bacik 			 orig_start, em->orig_start);
555aaedb55bSJosef Bacik 		goto out;
556aaedb55bSJosef Bacik 	}
557aaedb55bSJosef Bacik 	if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
558aaedb55bSJosef Bacik 		test_msg("Unexpected block start, wanted %llu, have %llu\n",
559aaedb55bSJosef Bacik 			 disk_bytenr + (em->start - em->orig_start),
560aaedb55bSJosef Bacik 			 em->block_start);
561aaedb55bSJosef Bacik 		goto out;
562aaedb55bSJosef Bacik 	}
563aaedb55bSJosef Bacik 	offset = em->start + em->len;
564aaedb55bSJosef Bacik 	free_extent_map(em);
565aaedb55bSJosef Bacik 
566aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
567aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
568aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
569aaedb55bSJosef Bacik 		goto out;
570aaedb55bSJosef Bacik 	}
571aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
572aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
573aaedb55bSJosef Bacik 		goto out;
574aaedb55bSJosef Bacik 	}
575aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 8192) {
576aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 8192, got "
577aaedb55bSJosef Bacik 			 "start %llu len %llu\n", offset, em->start, em->len);
578aaedb55bSJosef Bacik 		goto out;
579aaedb55bSJosef Bacik 	}
580aaedb55bSJosef Bacik 	if (em->flags != prealloc_only) {
581aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want %lu have %lu\n",
582aaedb55bSJosef Bacik 			 prealloc_only, em->flags);
583aaedb55bSJosef Bacik 		goto out;
584aaedb55bSJosef Bacik 	}
585aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
586aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", orig_start,
587aaedb55bSJosef Bacik 			 em->orig_start);
588aaedb55bSJosef Bacik 		goto out;
589aaedb55bSJosef Bacik 	}
590aaedb55bSJosef Bacik 	if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
591aaedb55bSJosef Bacik 		test_msg("Unexpected block start, wanted %llu, have %llu\n",
592aaedb55bSJosef Bacik 			 disk_bytenr + (em->start - em->orig_start),
593aaedb55bSJosef Bacik 			 em->block_start);
594aaedb55bSJosef Bacik 		goto out;
595aaedb55bSJosef Bacik 	}
596aaedb55bSJosef Bacik 	offset = em->start + em->len;
597aaedb55bSJosef Bacik 	free_extent_map(em);
598aaedb55bSJosef Bacik 
599aaedb55bSJosef Bacik 	/* Now for the compressed extent */
600aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
601aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
602aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
603aaedb55bSJosef Bacik 		goto out;
604aaedb55bSJosef Bacik 	}
605aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
606aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
607aaedb55bSJosef Bacik 		goto out;
608aaedb55bSJosef Bacik 	}
609aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 8192) {
610aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 8192, got "
611aaedb55bSJosef Bacik 			 "start %llu len %llu\n", offset, em->start, em->len);
612aaedb55bSJosef Bacik 		goto out;
613aaedb55bSJosef Bacik 	}
614aaedb55bSJosef Bacik 	if (em->flags != compressed_only) {
615aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want %lu have %lu\n",
616aaedb55bSJosef Bacik 			 compressed_only, em->flags);
617aaedb55bSJosef Bacik 		goto out;
618aaedb55bSJosef Bacik 	}
619aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
620aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n",
621aaedb55bSJosef Bacik 			 em->start, em->orig_start);
622aaedb55bSJosef Bacik 		goto out;
623aaedb55bSJosef Bacik 	}
624aaedb55bSJosef Bacik 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
625aaedb55bSJosef Bacik 		test_msg("Unexpected compress type, wanted %d, got %d\n",
626aaedb55bSJosef Bacik 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
627aaedb55bSJosef Bacik 		goto out;
628aaedb55bSJosef Bacik 	}
629aaedb55bSJosef Bacik 	offset = em->start + em->len;
630aaedb55bSJosef Bacik 	free_extent_map(em);
631aaedb55bSJosef Bacik 
632aaedb55bSJosef Bacik 	/* Split compressed extent */
633aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
634aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
635aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
636aaedb55bSJosef Bacik 		goto out;
637aaedb55bSJosef Bacik 	}
638aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
639aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
640aaedb55bSJosef Bacik 		goto out;
641aaedb55bSJosef Bacik 	}
642aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 4096) {
643aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 4096, got "
644aaedb55bSJosef Bacik 			 "start %llu len %llu\n", offset, em->start, em->len);
645aaedb55bSJosef Bacik 		goto out;
646aaedb55bSJosef Bacik 	}
647aaedb55bSJosef Bacik 	if (em->flags != compressed_only) {
648aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want %lu have %lu\n",
649aaedb55bSJosef Bacik 			 compressed_only, em->flags);
650aaedb55bSJosef Bacik 		goto out;
651aaedb55bSJosef Bacik 	}
652aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
653aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n",
654aaedb55bSJosef Bacik 			 em->start, em->orig_start);
655aaedb55bSJosef Bacik 		goto out;
656aaedb55bSJosef Bacik 	}
657aaedb55bSJosef Bacik 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
658aaedb55bSJosef Bacik 		test_msg("Unexpected compress type, wanted %d, got %d\n",
659aaedb55bSJosef Bacik 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
660aaedb55bSJosef Bacik 		goto out;
661aaedb55bSJosef Bacik 	}
662aaedb55bSJosef Bacik 	disk_bytenr = em->block_start;
663aaedb55bSJosef Bacik 	orig_start = em->start;
664aaedb55bSJosef Bacik 	offset = em->start + em->len;
665aaedb55bSJosef Bacik 	free_extent_map(em);
666aaedb55bSJosef Bacik 
667aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
668aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
669aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
670aaedb55bSJosef Bacik 		goto out;
671aaedb55bSJosef Bacik 	}
672aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
673aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
674aaedb55bSJosef Bacik 		goto out;
675aaedb55bSJosef Bacik 	}
676aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 4096) {
677aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 4096, got "
678aaedb55bSJosef Bacik 			 "start %llu len %llu\n", offset, em->start, em->len);
679aaedb55bSJosef Bacik 		goto out;
680aaedb55bSJosef Bacik 	}
681aaedb55bSJosef Bacik 	if (em->flags != 0) {
682aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
683aaedb55bSJosef Bacik 		goto out;
684aaedb55bSJosef Bacik 	}
685aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
686aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
687aaedb55bSJosef Bacik 			 em->orig_start);
688aaedb55bSJosef Bacik 		goto out;
689aaedb55bSJosef Bacik 	}
690aaedb55bSJosef Bacik 	offset = em->start + em->len;
691aaedb55bSJosef Bacik 	free_extent_map(em);
692aaedb55bSJosef Bacik 
693aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
694aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
695aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
696aaedb55bSJosef Bacik 		goto out;
697aaedb55bSJosef Bacik 	}
698aaedb55bSJosef Bacik 	if (em->block_start != disk_bytenr) {
699aaedb55bSJosef Bacik 		test_msg("Block start does not match, want %llu got %llu\n",
700aaedb55bSJosef Bacik 			 disk_bytenr, em->block_start);
701aaedb55bSJosef Bacik 		goto out;
702aaedb55bSJosef Bacik 	}
703aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 8192) {
704aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 8192, got "
705aaedb55bSJosef Bacik 			 "start %llu len %llu\n", offset, em->start, em->len);
706aaedb55bSJosef Bacik 		goto out;
707aaedb55bSJosef Bacik 	}
708aaedb55bSJosef Bacik 	if (em->flags != compressed_only) {
709aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want %lu have %lu\n",
710aaedb55bSJosef Bacik 			 compressed_only, em->flags);
711aaedb55bSJosef Bacik 		goto out;
712aaedb55bSJosef Bacik 	}
713aaedb55bSJosef Bacik 	if (em->orig_start != orig_start) {
714aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n",
715aaedb55bSJosef Bacik 			 em->start, orig_start);
716aaedb55bSJosef Bacik 		goto out;
717aaedb55bSJosef Bacik 	}
718aaedb55bSJosef Bacik 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
719aaedb55bSJosef Bacik 		test_msg("Unexpected compress type, wanted %d, got %d\n",
720aaedb55bSJosef Bacik 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
721aaedb55bSJosef Bacik 		goto out;
722aaedb55bSJosef Bacik 	}
723aaedb55bSJosef Bacik 	offset = em->start + em->len;
724aaedb55bSJosef Bacik 	free_extent_map(em);
725aaedb55bSJosef Bacik 
726aaedb55bSJosef Bacik 	/* A hole between regular extents but no hole extent */
727aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset + 6, 4096, 0);
728aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
729aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
730aaedb55bSJosef Bacik 		goto out;
731aaedb55bSJosef Bacik 	}
732aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
733aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
734aaedb55bSJosef Bacik 		goto out;
735aaedb55bSJosef Bacik 	}
736aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 4096) {
737aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 4096, got "
738aaedb55bSJosef Bacik 			 "start %llu len %llu\n", offset, em->start, em->len);
739aaedb55bSJosef Bacik 		goto out;
740aaedb55bSJosef Bacik 	}
741aaedb55bSJosef Bacik 	if (em->flags != 0) {
742aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
743aaedb55bSJosef Bacik 		goto out;
744aaedb55bSJosef Bacik 	}
745aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
746aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
747aaedb55bSJosef Bacik 			 em->orig_start);
748aaedb55bSJosef Bacik 		goto out;
749aaedb55bSJosef Bacik 	}
750aaedb55bSJosef Bacik 	offset = em->start + em->len;
751aaedb55bSJosef Bacik 	free_extent_map(em);
752aaedb55bSJosef Bacik 
753aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset, 4096 * 1024, 0);
754aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
755aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
756aaedb55bSJosef Bacik 		goto out;
757aaedb55bSJosef Bacik 	}
758aaedb55bSJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
759aaedb55bSJosef Bacik 		test_msg("Expected a hole extent, got %llu\n", em->block_start);
760aaedb55bSJosef Bacik 		goto out;
761aaedb55bSJosef Bacik 	}
762aaedb55bSJosef Bacik 	/*
763aaedb55bSJosef Bacik 	 * Currently we just return a length that we requested rather than the
764aaedb55bSJosef Bacik 	 * length of the actual hole, if this changes we'll have to change this
765aaedb55bSJosef Bacik 	 * test.
766aaedb55bSJosef Bacik 	 */
767aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 12288) {
768aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 12288, got "
769aaedb55bSJosef Bacik 			 "start %llu len %llu\n", offset, em->start, em->len);
770aaedb55bSJosef Bacik 		goto out;
771aaedb55bSJosef Bacik 	}
772aaedb55bSJosef Bacik 	if (em->flags != vacancy_only) {
773aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want %lu have %lu\n",
774aaedb55bSJosef Bacik 			 vacancy_only, em->flags);
775aaedb55bSJosef Bacik 		goto out;
776aaedb55bSJosef Bacik 	}
777aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
778aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
779aaedb55bSJosef Bacik 			 em->orig_start);
780aaedb55bSJosef Bacik 		goto out;
781aaedb55bSJosef Bacik 	}
782aaedb55bSJosef Bacik 	offset = em->start + em->len;
783aaedb55bSJosef Bacik 	free_extent_map(em);
784aaedb55bSJosef Bacik 
785aaedb55bSJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
786aaedb55bSJosef Bacik 	if (IS_ERR(em)) {
787aaedb55bSJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
788aaedb55bSJosef Bacik 		goto out;
789aaedb55bSJosef Bacik 	}
790aaedb55bSJosef Bacik 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
791aaedb55bSJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
792aaedb55bSJosef Bacik 		goto out;
793aaedb55bSJosef Bacik 	}
794aaedb55bSJosef Bacik 	if (em->start != offset || em->len != 4096) {
795aaedb55bSJosef Bacik 		test_msg("Unexpected extent wanted start %llu len 4096, got "
796aaedb55bSJosef Bacik 			 "start %llu len %llu\n", offset, em->start, em->len);
797aaedb55bSJosef Bacik 		goto out;
798aaedb55bSJosef Bacik 	}
799aaedb55bSJosef Bacik 	if (em->flags != 0) {
800aaedb55bSJosef Bacik 		test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
801aaedb55bSJosef Bacik 		goto out;
802aaedb55bSJosef Bacik 	}
803aaedb55bSJosef Bacik 	if (em->orig_start != em->start) {
804aaedb55bSJosef Bacik 		test_msg("Wrong orig offset, want %llu, have %llu\n", 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);
814aaedb55bSJosef Bacik 	return ret;
815aaedb55bSJosef Bacik }
816aaedb55bSJosef Bacik 
8170e30db86SJosef Bacik static int test_hole_first(void)
8180e30db86SJosef Bacik {
8190e30db86SJosef Bacik 	struct inode *inode = NULL;
8200e30db86SJosef Bacik 	struct btrfs_root *root = NULL;
8210e30db86SJosef Bacik 	struct extent_map *em = NULL;
8220e30db86SJosef Bacik 	int ret = -ENOMEM;
8230e30db86SJosef Bacik 
8240e30db86SJosef Bacik 	inode = btrfs_new_test_inode();
8250e30db86SJosef Bacik 	if (!inode) {
8260e30db86SJosef Bacik 		test_msg("Couldn't allocate inode\n");
8270e30db86SJosef Bacik 		return ret;
8280e30db86SJosef Bacik 	}
8290e30db86SJosef Bacik 
8300e30db86SJosef Bacik 	BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
8310e30db86SJosef Bacik 	BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
8320e30db86SJosef Bacik 	BTRFS_I(inode)->location.offset = 0;
8330e30db86SJosef Bacik 
8340e30db86SJosef Bacik 	root = btrfs_alloc_dummy_root();
8350e30db86SJosef Bacik 	if (IS_ERR(root)) {
8360e30db86SJosef Bacik 		test_msg("Couldn't allocate root\n");
8370e30db86SJosef Bacik 		goto out;
8380e30db86SJosef Bacik 	}
8390e30db86SJosef Bacik 
840faa2dbf0SJosef Bacik 	root->fs_info = btrfs_alloc_dummy_fs_info();
8410e30db86SJosef Bacik 	if (!root->fs_info) {
8420e30db86SJosef Bacik 		test_msg("Couldn't allocate dummy fs info\n");
8430e30db86SJosef Bacik 		goto out;
8440e30db86SJosef Bacik 	}
8450e30db86SJosef Bacik 
8460e30db86SJosef Bacik 	root->node = alloc_dummy_extent_buffer(0, 4096);
8470e30db86SJosef Bacik 	if (!root->node) {
8480e30db86SJosef Bacik 		test_msg("Couldn't allocate dummy buffer\n");
8490e30db86SJosef Bacik 		goto out;
8500e30db86SJosef Bacik 	}
8510e30db86SJosef Bacik 
8520e30db86SJosef Bacik 	extent_buffer_get(root->node);
8530e30db86SJosef Bacik 	btrfs_set_header_nritems(root->node, 0);
8540e30db86SJosef Bacik 	btrfs_set_header_level(root->node, 0);
8550e30db86SJosef Bacik 	BTRFS_I(inode)->root = root;
8560e30db86SJosef Bacik 	ret = -EINVAL;
8570e30db86SJosef Bacik 
8580e30db86SJosef Bacik 	/*
8590e30db86SJosef Bacik 	 * Need a blank inode item here just so we don't confuse
8600e30db86SJosef Bacik 	 * btrfs_get_extent.
8610e30db86SJosef Bacik 	 */
8620e30db86SJosef Bacik 	insert_inode_item_key(root);
8630e30db86SJosef Bacik 	insert_extent(root, 4096, 4096, 4096, 0, 4096, 4096,
8640e30db86SJosef Bacik 		      BTRFS_FILE_EXTENT_REG, 0, 1);
8650e30db86SJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, 0, 8192, 0);
8660e30db86SJosef Bacik 	if (IS_ERR(em)) {
8670e30db86SJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
8680e30db86SJosef Bacik 		goto out;
8690e30db86SJosef Bacik 	}
8700e30db86SJosef Bacik 	if (em->block_start != EXTENT_MAP_HOLE) {
8710e30db86SJosef Bacik 		test_msg("Expected a hole, got %llu\n", em->block_start);
8720e30db86SJosef Bacik 		goto out;
8730e30db86SJosef Bacik 	}
8740e30db86SJosef Bacik 	if (em->start != 0 || em->len != 4096) {
8750e30db86SJosef Bacik 		test_msg("Unexpected extent wanted start 0 len 4096, got start "
8760e30db86SJosef Bacik 			 "%llu len %llu\n", em->start, em->len);
8770e30db86SJosef Bacik 		goto out;
8780e30db86SJosef Bacik 	}
8790e30db86SJosef Bacik 	if (em->flags != vacancy_only) {
8800e30db86SJosef Bacik 		test_msg("Wrong flags, wanted %lu, have %lu\n", vacancy_only,
8810e30db86SJosef Bacik 			 em->flags);
8820e30db86SJosef Bacik 		goto out;
8830e30db86SJosef Bacik 	}
8840e30db86SJosef Bacik 	free_extent_map(em);
8850e30db86SJosef Bacik 
8860e30db86SJosef Bacik 	em = btrfs_get_extent(inode, NULL, 0, 4096, 8192, 0);
8870e30db86SJosef Bacik 	if (IS_ERR(em)) {
8880e30db86SJosef Bacik 		test_msg("Got an error when we shouldn't have\n");
8890e30db86SJosef Bacik 		goto out;
8900e30db86SJosef Bacik 	}
8910e30db86SJosef Bacik 	if (em->block_start != 4096) {
8920e30db86SJosef Bacik 		test_msg("Expected a real extent, got %llu\n", em->block_start);
8930e30db86SJosef Bacik 		goto out;
8940e30db86SJosef Bacik 	}
8950e30db86SJosef Bacik 	if (em->start != 4096 || em->len != 4096) {
8960e30db86SJosef Bacik 		test_msg("Unexpected extent wanted start 4096 len 4096, got "
8970e30db86SJosef Bacik 			 "start %llu len %llu\n", em->start, em->len);
8980e30db86SJosef Bacik 		goto out;
8990e30db86SJosef Bacik 	}
9000e30db86SJosef Bacik 	if (em->flags != 0) {
9010e30db86SJosef Bacik 		test_msg("Unexpected flags set, wanted 0 got %lu\n",
9020e30db86SJosef Bacik 			 em->flags);
9030e30db86SJosef Bacik 		goto out;
9040e30db86SJosef Bacik 	}
9050e30db86SJosef Bacik 	ret = 0;
9060e30db86SJosef Bacik out:
9070e30db86SJosef Bacik 	if (!IS_ERR(em))
9080e30db86SJosef Bacik 		free_extent_map(em);
9090e30db86SJosef Bacik 	iput(inode);
910faa2dbf0SJosef Bacik 	btrfs_free_dummy_root(root);
9110e30db86SJosef Bacik 	return ret;
9120e30db86SJosef Bacik }
9130e30db86SJosef Bacik 
914aaedb55bSJosef Bacik int btrfs_test_inodes(void)
915aaedb55bSJosef Bacik {
9160e30db86SJosef Bacik 	int ret;
9170e30db86SJosef Bacik 
9180e30db86SJosef Bacik 	set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
9190e30db86SJosef Bacik 	set_bit(EXTENT_FLAG_VACANCY, &vacancy_only);
9200e30db86SJosef Bacik 	set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
9210e30db86SJosef Bacik 
922aaedb55bSJosef Bacik 	test_msg("Running btrfs_get_extent tests\n");
9230e30db86SJosef Bacik 	ret = test_btrfs_get_extent();
9240e30db86SJosef Bacik 	if (ret)
9250e30db86SJosef Bacik 		return ret;
9260e30db86SJosef Bacik 	test_msg("Running hole first btrfs_get_extent test\n");
9270e30db86SJosef Bacik 	return test_hole_first();
928aaedb55bSJosef Bacik }
929