xref: /openbmc/linux/fs/btrfs/tests/inode-tests.c (revision ba61bb17)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2013 Fusion IO.  All rights reserved.
4  */
5 
6 #include <linux/types.h>
7 #include "btrfs-tests.h"
8 #include "../ctree.h"
9 #include "../btrfs_inode.h"
10 #include "../disk-io.h"
11 #include "../extent_io.h"
12 #include "../volumes.h"
13 #include "../compression.h"
14 
15 static void insert_extent(struct btrfs_root *root, u64 start, u64 len,
16 			  u64 ram_bytes, u64 offset, u64 disk_bytenr,
17 			  u64 disk_len, u32 type, u8 compression, int slot)
18 {
19 	struct btrfs_path path;
20 	struct btrfs_file_extent_item *fi;
21 	struct extent_buffer *leaf = root->node;
22 	struct btrfs_key key;
23 	u32 value_len = sizeof(struct btrfs_file_extent_item);
24 
25 	if (type == BTRFS_FILE_EXTENT_INLINE)
26 		value_len += len;
27 	memset(&path, 0, sizeof(path));
28 
29 	path.nodes[0] = leaf;
30 	path.slots[0] = slot;
31 
32 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
33 	key.type = BTRFS_EXTENT_DATA_KEY;
34 	key.offset = start;
35 
36 	setup_items_for_insert(root, &path, &key, &value_len, value_len,
37 			       value_len + sizeof(struct btrfs_item), 1);
38 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
39 	btrfs_set_file_extent_generation(leaf, fi, 1);
40 	btrfs_set_file_extent_type(leaf, fi, type);
41 	btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
42 	btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_len);
43 	btrfs_set_file_extent_offset(leaf, fi, offset);
44 	btrfs_set_file_extent_num_bytes(leaf, fi, len);
45 	btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
46 	btrfs_set_file_extent_compression(leaf, fi, compression);
47 	btrfs_set_file_extent_encryption(leaf, fi, 0);
48 	btrfs_set_file_extent_other_encoding(leaf, fi, 0);
49 }
50 
51 static void insert_inode_item_key(struct btrfs_root *root)
52 {
53 	struct btrfs_path path;
54 	struct extent_buffer *leaf = root->node;
55 	struct btrfs_key key;
56 	u32 value_len = 0;
57 
58 	memset(&path, 0, sizeof(path));
59 
60 	path.nodes[0] = leaf;
61 	path.slots[0] = 0;
62 
63 	key.objectid = BTRFS_INODE_ITEM_KEY;
64 	key.type = BTRFS_INODE_ITEM_KEY;
65 	key.offset = 0;
66 
67 	setup_items_for_insert(root, &path, &key, &value_len, value_len,
68 			       value_len + sizeof(struct btrfs_item), 1);
69 }
70 
71 /*
72  * Build the most complicated map of extents the earth has ever seen.  We want
73  * this so we can test all of the corner cases of btrfs_get_extent.  Here is a
74  * diagram of how the extents will look though this may not be possible we still
75  * want to make sure everything acts normally (the last number is not inclusive)
76  *
77  * [0 - 5][5 -  6][     6 - 4096     ][ 4096 - 4100][4100 - 8195][8195 - 12291]
78  * [hole ][inline][hole but no extent][  hole   ][   regular ][regular1 split]
79  *
80  * [12291 - 16387][16387 - 24579][24579 - 28675][ 28675 - 32771][32771 - 36867 ]
81  * [    hole    ][regular1 split][   prealloc ][   prealloc1  ][prealloc1 written]
82  *
83  * [36867 - 45059][45059 - 53251][53251 - 57347][57347 - 61443][61443- 69635]
84  * [  prealloc1  ][ compressed  ][ compressed1 ][    regular  ][ compressed1]
85  *
86  * [69635-73731][   73731 - 86019   ][86019-90115]
87  * [  regular  ][ hole but no extent][  regular  ]
88  */
89 static void setup_file_extents(struct btrfs_root *root, u32 sectorsize)
90 {
91 	int slot = 0;
92 	u64 disk_bytenr = SZ_1M;
93 	u64 offset = 0;
94 
95 	/* First we want a hole */
96 	insert_extent(root, offset, 5, 5, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
97 		      slot);
98 	slot++;
99 	offset += 5;
100 
101 	/*
102 	 * Now we want an inline extent, I don't think this is possible but hey
103 	 * why not?  Also keep in mind if we have an inline extent it counts as
104 	 * the whole first page.  If we were to expand it we would have to cow
105 	 * and we wouldn't have an inline extent anymore.
106 	 */
107 	insert_extent(root, offset, 1, 1, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE, 0,
108 		      slot);
109 	slot++;
110 	offset = sectorsize;
111 
112 	/* Now another hole */
113 	insert_extent(root, offset, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
114 		      slot);
115 	slot++;
116 	offset += 4;
117 
118 	/* Now for a regular extent */
119 	insert_extent(root, offset, sectorsize - 1, sectorsize - 1, 0,
120 		      disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
121 	slot++;
122 	disk_bytenr += sectorsize;
123 	offset += sectorsize - 1;
124 
125 	/*
126 	 * Now for 3 extents that were split from a hole punch so we test
127 	 * offsets properly.
128 	 */
129 	insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
130 		      4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
131 	slot++;
132 	offset += sectorsize;
133 	insert_extent(root, offset, sectorsize, sectorsize, 0, 0, 0,
134 		      BTRFS_FILE_EXTENT_REG, 0, slot);
135 	slot++;
136 	offset += sectorsize;
137 	insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
138 		      2 * sectorsize, disk_bytenr, 4 * sectorsize,
139 		      BTRFS_FILE_EXTENT_REG, 0, slot);
140 	slot++;
141 	offset += 2 * sectorsize;
142 	disk_bytenr += 4 * sectorsize;
143 
144 	/* Now for a unwritten prealloc extent */
145 	insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
146 		sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
147 	slot++;
148 	offset += sectorsize;
149 
150 	/*
151 	 * We want to jack up disk_bytenr a little more so the em stuff doesn't
152 	 * merge our records.
153 	 */
154 	disk_bytenr += 2 * sectorsize;
155 
156 	/*
157 	 * Now for a partially written prealloc extent, basically the same as
158 	 * the hole punch example above.  Ram_bytes never changes when you mark
159 	 * extents written btw.
160 	 */
161 	insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
162 		      4 * sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
163 	slot++;
164 	offset += sectorsize;
165 	insert_extent(root, offset, sectorsize, 4 * sectorsize, sectorsize,
166 		      disk_bytenr, 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0,
167 		      slot);
168 	slot++;
169 	offset += sectorsize;
170 	insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
171 		      2 * sectorsize, disk_bytenr, 4 * sectorsize,
172 		      BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
173 	slot++;
174 	offset += 2 * sectorsize;
175 	disk_bytenr += 4 * sectorsize;
176 
177 	/* Now a normal compressed extent */
178 	insert_extent(root, offset, 2 * sectorsize, 2 * sectorsize, 0,
179 		      disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG,
180 		      BTRFS_COMPRESS_ZLIB, slot);
181 	slot++;
182 	offset += 2 * sectorsize;
183 	/* No merges */
184 	disk_bytenr += 2 * sectorsize;
185 
186 	/* Now a split compressed extent */
187 	insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
188 		      sectorsize, BTRFS_FILE_EXTENT_REG,
189 		      BTRFS_COMPRESS_ZLIB, slot);
190 	slot++;
191 	offset += sectorsize;
192 	insert_extent(root, offset, sectorsize, sectorsize, 0,
193 		      disk_bytenr + sectorsize, sectorsize,
194 		      BTRFS_FILE_EXTENT_REG, 0, slot);
195 	slot++;
196 	offset += sectorsize;
197 	insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
198 		      2 * sectorsize, disk_bytenr, sectorsize,
199 		      BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
200 	slot++;
201 	offset += 2 * sectorsize;
202 	disk_bytenr += 2 * sectorsize;
203 
204 	/* Now extents that have a hole but no hole extent */
205 	insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
206 		      sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
207 	slot++;
208 	offset += 4 * sectorsize;
209 	disk_bytenr += sectorsize;
210 	insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
211 		      sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
212 }
213 
214 static unsigned long prealloc_only = 0;
215 static unsigned long compressed_only = 0;
216 static unsigned long vacancy_only = 0;
217 
218 static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize)
219 {
220 	struct btrfs_fs_info *fs_info = NULL;
221 	struct inode *inode = NULL;
222 	struct btrfs_root *root = NULL;
223 	struct extent_map *em = NULL;
224 	u64 orig_start;
225 	u64 disk_bytenr;
226 	u64 offset;
227 	int ret = -ENOMEM;
228 
229 	inode = btrfs_new_test_inode();
230 	if (!inode) {
231 		test_err("couldn't allocate inode");
232 		return ret;
233 	}
234 
235 	BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
236 	BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
237 	BTRFS_I(inode)->location.offset = 0;
238 
239 	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
240 	if (!fs_info) {
241 		test_err("couldn't allocate dummy fs info");
242 		goto out;
243 	}
244 
245 	root = btrfs_alloc_dummy_root(fs_info);
246 	if (IS_ERR(root)) {
247 		test_err("couldn't allocate root");
248 		goto out;
249 	}
250 
251 	root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
252 	if (!root->node) {
253 		test_err("couldn't allocate dummy buffer");
254 		goto out;
255 	}
256 
257 	/*
258 	 * We will just free a dummy node if it's ref count is 2 so we need an
259 	 * extra ref so our searches don't accidentally release our page.
260 	 */
261 	extent_buffer_get(root->node);
262 	btrfs_set_header_nritems(root->node, 0);
263 	btrfs_set_header_level(root->node, 0);
264 	ret = -EINVAL;
265 
266 	/* First with no extents */
267 	BTRFS_I(inode)->root = root;
268 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, sectorsize, 0);
269 	if (IS_ERR(em)) {
270 		em = NULL;
271 		test_err("got an error when we shouldn't have");
272 		goto out;
273 	}
274 	if (em->block_start != EXTENT_MAP_HOLE) {
275 		test_err("expected a hole, got %llu", em->block_start);
276 		goto out;
277 	}
278 	free_extent_map(em);
279 	btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
280 
281 	/*
282 	 * All of the magic numbers are based on the mapping setup in
283 	 * setup_file_extents, so if you change anything there you need to
284 	 * update the comment and update the expected values below.
285 	 */
286 	setup_file_extents(root, sectorsize);
287 
288 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, (u64)-1, 0);
289 	if (IS_ERR(em)) {
290 		test_err("got an error when we shouldn't have");
291 		goto out;
292 	}
293 	if (em->block_start != EXTENT_MAP_HOLE) {
294 		test_err("expected a hole, got %llu", em->block_start);
295 		goto out;
296 	}
297 	if (em->start != 0 || em->len != 5) {
298 		test_err(
299 		"unexpected extent wanted start 0 len 5, got start %llu len %llu",
300 			em->start, em->len);
301 		goto out;
302 	}
303 	if (em->flags != 0) {
304 		test_err("unexpected flags set, want 0 have %lu", em->flags);
305 		goto out;
306 	}
307 	offset = em->start + em->len;
308 	free_extent_map(em);
309 
310 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
311 	if (IS_ERR(em)) {
312 		test_err("got an error when we shouldn't have");
313 		goto out;
314 	}
315 	if (em->block_start != EXTENT_MAP_INLINE) {
316 		test_err("expected an inline, got %llu", em->block_start);
317 		goto out;
318 	}
319 
320 	if (em->start != offset || em->len != (sectorsize - 5)) {
321 		test_err(
322 	"unexpected extent wanted start %llu len 1, got start %llu len %llu",
323 			offset, em->start, em->len);
324 		goto out;
325 	}
326 	if (em->flags != 0) {
327 		test_err("unexpected flags set, want 0 have %lu", em->flags);
328 		goto out;
329 	}
330 	/*
331 	 * We don't test anything else for inline since it doesn't get set
332 	 * unless we have a page for it to write into.  Maybe we should change
333 	 * this?
334 	 */
335 	offset = em->start + em->len;
336 	free_extent_map(em);
337 
338 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
339 	if (IS_ERR(em)) {
340 		test_err("got an error when we shouldn't have");
341 		goto out;
342 	}
343 	if (em->block_start != EXTENT_MAP_HOLE) {
344 		test_err("expected a hole, got %llu", em->block_start);
345 		goto out;
346 	}
347 	if (em->start != offset || em->len != 4) {
348 		test_err(
349 	"unexpected extent wanted start %llu len 4, got start %llu len %llu",
350 			offset, em->start, em->len);
351 		goto out;
352 	}
353 	if (em->flags != 0) {
354 		test_err("unexpected flags set, want 0 have %lu", em->flags);
355 		goto out;
356 	}
357 	offset = em->start + em->len;
358 	free_extent_map(em);
359 
360 	/* Regular extent */
361 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
362 	if (IS_ERR(em)) {
363 		test_err("got an error when we shouldn't have");
364 		goto out;
365 	}
366 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
367 		test_err("expected a real extent, got %llu", em->block_start);
368 		goto out;
369 	}
370 	if (em->start != offset || em->len != sectorsize - 1) {
371 		test_err(
372 	"unexpected extent wanted start %llu len 4095, got start %llu len %llu",
373 			offset, em->start, em->len);
374 		goto out;
375 	}
376 	if (em->flags != 0) {
377 		test_err("unexpected flags set, want 0 have %lu", em->flags);
378 		goto out;
379 	}
380 	if (em->orig_start != em->start) {
381 		test_err("wrong orig offset, want %llu, have %llu", em->start,
382 			 em->orig_start);
383 		goto out;
384 	}
385 	offset = em->start + em->len;
386 	free_extent_map(em);
387 
388 	/* The next 3 are split extents */
389 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
390 	if (IS_ERR(em)) {
391 		test_err("got an error when we shouldn't have");
392 		goto out;
393 	}
394 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
395 		test_err("expected a real extent, got %llu", em->block_start);
396 		goto out;
397 	}
398 	if (em->start != offset || em->len != sectorsize) {
399 		test_err(
400 		"unexpected extent start %llu len %u, got start %llu len %llu",
401 			offset, sectorsize, em->start, em->len);
402 		goto out;
403 	}
404 	if (em->flags != 0) {
405 		test_err("unexpected flags set, want 0 have %lu", em->flags);
406 		goto out;
407 	}
408 	if (em->orig_start != em->start) {
409 		test_err("wrong orig offset, want %llu, have %llu", em->start,
410 			 em->orig_start);
411 		goto out;
412 	}
413 	disk_bytenr = em->block_start;
414 	orig_start = em->start;
415 	offset = em->start + em->len;
416 	free_extent_map(em);
417 
418 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
419 	if (IS_ERR(em)) {
420 		test_err("got an error when we shouldn't have");
421 		goto out;
422 	}
423 	if (em->block_start != EXTENT_MAP_HOLE) {
424 		test_err("expected a hole, got %llu", em->block_start);
425 		goto out;
426 	}
427 	if (em->start != offset || em->len != sectorsize) {
428 		test_err(
429 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
430 			offset, sectorsize, em->start, em->len);
431 		goto out;
432 	}
433 	if (em->flags != 0) {
434 		test_err("unexpected flags set, want 0 have %lu", em->flags);
435 		goto out;
436 	}
437 	offset = em->start + em->len;
438 	free_extent_map(em);
439 
440 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
441 	if (IS_ERR(em)) {
442 		test_err("got an error when we shouldn't have");
443 		goto out;
444 	}
445 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
446 		test_err("expected a real extent, got %llu", em->block_start);
447 		goto out;
448 	}
449 	if (em->start != offset || em->len != 2 * sectorsize) {
450 		test_err(
451 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
452 			offset, 2 * sectorsize, em->start, em->len);
453 		goto out;
454 	}
455 	if (em->flags != 0) {
456 		test_err("unexpected flags set, want 0 have %lu", em->flags);
457 		goto out;
458 	}
459 	if (em->orig_start != orig_start) {
460 		test_err("wrong orig offset, want %llu, have %llu",
461 			 orig_start, em->orig_start);
462 		goto out;
463 	}
464 	disk_bytenr += (em->start - orig_start);
465 	if (em->block_start != disk_bytenr) {
466 		test_err("wrong block start, want %llu, have %llu",
467 			 disk_bytenr, em->block_start);
468 		goto out;
469 	}
470 	offset = em->start + em->len;
471 	free_extent_map(em);
472 
473 	/* Prealloc extent */
474 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
475 	if (IS_ERR(em)) {
476 		test_err("got an error when we shouldn't have");
477 		goto out;
478 	}
479 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
480 		test_err("expected a real extent, got %llu", em->block_start);
481 		goto out;
482 	}
483 	if (em->start != offset || em->len != sectorsize) {
484 		test_err(
485 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
486 			offset, sectorsize, em->start, em->len);
487 		goto out;
488 	}
489 	if (em->flags != prealloc_only) {
490 		test_err("unexpected flags set, want %lu have %lu",
491 			 prealloc_only, em->flags);
492 		goto out;
493 	}
494 	if (em->orig_start != em->start) {
495 		test_err("wrong orig offset, want %llu, have %llu", em->start,
496 			 em->orig_start);
497 		goto out;
498 	}
499 	offset = em->start + em->len;
500 	free_extent_map(em);
501 
502 	/* The next 3 are a half written prealloc extent */
503 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
504 	if (IS_ERR(em)) {
505 		test_err("got an error when we shouldn't have");
506 		goto out;
507 	}
508 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
509 		test_err("expected a real extent, got %llu", em->block_start);
510 		goto out;
511 	}
512 	if (em->start != offset || em->len != sectorsize) {
513 		test_err(
514 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
515 			offset, sectorsize, em->start, em->len);
516 		goto out;
517 	}
518 	if (em->flags != prealloc_only) {
519 		test_err("unexpected flags set, want %lu have %lu",
520 			 prealloc_only, em->flags);
521 		goto out;
522 	}
523 	if (em->orig_start != em->start) {
524 		test_err("wrong orig offset, want %llu, have %llu", em->start,
525 			 em->orig_start);
526 		goto out;
527 	}
528 	disk_bytenr = em->block_start;
529 	orig_start = em->start;
530 	offset = em->start + em->len;
531 	free_extent_map(em);
532 
533 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
534 	if (IS_ERR(em)) {
535 		test_err("got an error when we shouldn't have");
536 		goto out;
537 	}
538 	if (em->block_start >= EXTENT_MAP_HOLE) {
539 		test_err("expected a real extent, got %llu", em->block_start);
540 		goto out;
541 	}
542 	if (em->start != offset || em->len != sectorsize) {
543 		test_err(
544 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
545 			offset, sectorsize, em->start, em->len);
546 		goto out;
547 	}
548 	if (em->flags != 0) {
549 		test_err("unexpected flags set, want 0 have %lu", em->flags);
550 		goto out;
551 	}
552 	if (em->orig_start != orig_start) {
553 		test_err("unexpected orig offset, wanted %llu, have %llu",
554 			 orig_start, em->orig_start);
555 		goto out;
556 	}
557 	if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
558 		test_err("unexpected block start, wanted %llu, have %llu",
559 			 disk_bytenr + (em->start - em->orig_start),
560 			 em->block_start);
561 		goto out;
562 	}
563 	offset = em->start + em->len;
564 	free_extent_map(em);
565 
566 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
567 	if (IS_ERR(em)) {
568 		test_err("got an error when we shouldn't have");
569 		goto out;
570 	}
571 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
572 		test_err("expected a real extent, got %llu", em->block_start);
573 		goto out;
574 	}
575 	if (em->start != offset || em->len != 2 * sectorsize) {
576 		test_err(
577 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
578 			offset, 2 * sectorsize, em->start, em->len);
579 		goto out;
580 	}
581 	if (em->flags != prealloc_only) {
582 		test_err("unexpected flags set, want %lu have %lu",
583 			 prealloc_only, em->flags);
584 		goto out;
585 	}
586 	if (em->orig_start != orig_start) {
587 		test_err("wrong orig offset, want %llu, have %llu", orig_start,
588 			 em->orig_start);
589 		goto out;
590 	}
591 	if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
592 		test_err("unexpected block start, wanted %llu, have %llu",
593 			 disk_bytenr + (em->start - em->orig_start),
594 			 em->block_start);
595 		goto out;
596 	}
597 	offset = em->start + em->len;
598 	free_extent_map(em);
599 
600 	/* Now for the compressed extent */
601 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
602 	if (IS_ERR(em)) {
603 		test_err("got an error when we shouldn't have");
604 		goto out;
605 	}
606 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
607 		test_err("expected a real extent, got %llu", em->block_start);
608 		goto out;
609 	}
610 	if (em->start != offset || em->len != 2 * sectorsize) {
611 		test_err(
612 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
613 			offset, 2 * sectorsize, em->start, em->len);
614 		goto out;
615 	}
616 	if (em->flags != compressed_only) {
617 		test_err("unexpected flags set, want %lu have %lu",
618 			 compressed_only, em->flags);
619 		goto out;
620 	}
621 	if (em->orig_start != em->start) {
622 		test_err("wrong orig offset, want %llu, have %llu",
623 			 em->start, em->orig_start);
624 		goto out;
625 	}
626 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
627 		test_err("unexpected compress type, wanted %d, got %d",
628 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
629 		goto out;
630 	}
631 	offset = em->start + em->len;
632 	free_extent_map(em);
633 
634 	/* Split compressed extent */
635 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
636 	if (IS_ERR(em)) {
637 		test_err("got an error when we shouldn't have");
638 		goto out;
639 	}
640 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
641 		test_err("expected a real extent, got %llu", em->block_start);
642 		goto out;
643 	}
644 	if (em->start != offset || em->len != sectorsize) {
645 		test_err(
646 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
647 			offset, sectorsize, em->start, em->len);
648 		goto out;
649 	}
650 	if (em->flags != compressed_only) {
651 		test_err("unexpected flags set, want %lu have %lu",
652 			 compressed_only, em->flags);
653 		goto out;
654 	}
655 	if (em->orig_start != em->start) {
656 		test_err("wrong orig offset, want %llu, have %llu",
657 			 em->start, em->orig_start);
658 		goto out;
659 	}
660 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
661 		test_err("unexpected compress type, wanted %d, got %d",
662 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
663 		goto out;
664 	}
665 	disk_bytenr = em->block_start;
666 	orig_start = em->start;
667 	offset = em->start + em->len;
668 	free_extent_map(em);
669 
670 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
671 	if (IS_ERR(em)) {
672 		test_err("got an error when we shouldn't have");
673 		goto out;
674 	}
675 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
676 		test_err("expected a real extent, got %llu", em->block_start);
677 		goto out;
678 	}
679 	if (em->start != offset || em->len != sectorsize) {
680 		test_err(
681 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
682 			offset, sectorsize, em->start, em->len);
683 		goto out;
684 	}
685 	if (em->flags != 0) {
686 		test_err("unexpected flags set, want 0 have %lu", em->flags);
687 		goto out;
688 	}
689 	if (em->orig_start != em->start) {
690 		test_err("wrong orig offset, want %llu, have %llu", em->start,
691 			 em->orig_start);
692 		goto out;
693 	}
694 	offset = em->start + em->len;
695 	free_extent_map(em);
696 
697 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
698 	if (IS_ERR(em)) {
699 		test_err("got an error when we shouldn't have");
700 		goto out;
701 	}
702 	if (em->block_start != disk_bytenr) {
703 		test_err("block start does not match, want %llu got %llu",
704 			 disk_bytenr, em->block_start);
705 		goto out;
706 	}
707 	if (em->start != offset || em->len != 2 * sectorsize) {
708 		test_err(
709 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
710 			offset, 2 * sectorsize, em->start, em->len);
711 		goto out;
712 	}
713 	if (em->flags != compressed_only) {
714 		test_err("unexpected flags set, want %lu have %lu",
715 			 compressed_only, em->flags);
716 		goto out;
717 	}
718 	if (em->orig_start != orig_start) {
719 		test_err("wrong orig offset, want %llu, have %llu",
720 			 em->start, orig_start);
721 		goto out;
722 	}
723 	if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
724 		test_err("unexpected compress type, wanted %d, got %d",
725 			 BTRFS_COMPRESS_ZLIB, em->compress_type);
726 		goto out;
727 	}
728 	offset = em->start + em->len;
729 	free_extent_map(em);
730 
731 	/* A hole between regular extents but no hole extent */
732 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset + 6,
733 			sectorsize, 0);
734 	if (IS_ERR(em)) {
735 		test_err("got an error when we shouldn't have");
736 		goto out;
737 	}
738 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
739 		test_err("expected a real extent, got %llu", em->block_start);
740 		goto out;
741 	}
742 	if (em->start != offset || em->len != sectorsize) {
743 		test_err(
744 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
745 			offset, sectorsize, em->start, em->len);
746 		goto out;
747 	}
748 	if (em->flags != 0) {
749 		test_err("unexpected flags set, want 0 have %lu", em->flags);
750 		goto out;
751 	}
752 	if (em->orig_start != em->start) {
753 		test_err("wrong orig offset, want %llu, have %llu", em->start,
754 			 em->orig_start);
755 		goto out;
756 	}
757 	offset = em->start + em->len;
758 	free_extent_map(em);
759 
760 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, SZ_4M, 0);
761 	if (IS_ERR(em)) {
762 		test_err("got an error when we shouldn't have");
763 		goto out;
764 	}
765 	if (em->block_start != EXTENT_MAP_HOLE) {
766 		test_err("expected a hole extent, got %llu", em->block_start);
767 		goto out;
768 	}
769 	/*
770 	 * Currently we just return a length that we requested rather than the
771 	 * length of the actual hole, if this changes we'll have to change this
772 	 * test.
773 	 */
774 	if (em->start != offset || em->len != 3 * sectorsize) {
775 		test_err(
776 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
777 			offset, 3 * sectorsize, em->start, em->len);
778 		goto out;
779 	}
780 	if (em->flags != vacancy_only) {
781 		test_err("unexpected flags set, want %lu have %lu",
782 			 vacancy_only, em->flags);
783 		goto out;
784 	}
785 	if (em->orig_start != em->start) {
786 		test_err("wrong orig offset, want %llu, have %llu", em->start,
787 			 em->orig_start);
788 		goto out;
789 	}
790 	offset = em->start + em->len;
791 	free_extent_map(em);
792 
793 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
794 	if (IS_ERR(em)) {
795 		test_err("got an error when we shouldn't have");
796 		goto out;
797 	}
798 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
799 		test_err("expected a real extent, got %llu", em->block_start);
800 		goto out;
801 	}
802 	if (em->start != offset || em->len != sectorsize) {
803 		test_err(
804 	"unexpected extent wanted start %llu len %u, got start %llu len %llu",
805 			offset, sectorsize, em->start, em->len);
806 		goto out;
807 	}
808 	if (em->flags != 0) {
809 		test_err("unexpected flags set, want 0 have %lu", em->flags);
810 		goto out;
811 	}
812 	if (em->orig_start != em->start) {
813 		test_err("wrong orig offset, want %llu, have %llu", em->start,
814 			 em->orig_start);
815 		goto out;
816 	}
817 	ret = 0;
818 out:
819 	if (!IS_ERR(em))
820 		free_extent_map(em);
821 	iput(inode);
822 	btrfs_free_dummy_root(root);
823 	btrfs_free_dummy_fs_info(fs_info);
824 	return ret;
825 }
826 
827 static int test_hole_first(u32 sectorsize, u32 nodesize)
828 {
829 	struct btrfs_fs_info *fs_info = NULL;
830 	struct inode *inode = NULL;
831 	struct btrfs_root *root = NULL;
832 	struct extent_map *em = NULL;
833 	int ret = -ENOMEM;
834 
835 	inode = btrfs_new_test_inode();
836 	if (!inode) {
837 		test_err("couldn't allocate inode");
838 		return ret;
839 	}
840 
841 	BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
842 	BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
843 	BTRFS_I(inode)->location.offset = 0;
844 
845 	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
846 	if (!fs_info) {
847 		test_err("couldn't allocate dummy fs info");
848 		goto out;
849 	}
850 
851 	root = btrfs_alloc_dummy_root(fs_info);
852 	if (IS_ERR(root)) {
853 		test_err("couldn't allocate root");
854 		goto out;
855 	}
856 
857 	root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
858 	if (!root->node) {
859 		test_err("couldn't allocate dummy buffer");
860 		goto out;
861 	}
862 
863 	extent_buffer_get(root->node);
864 	btrfs_set_header_nritems(root->node, 0);
865 	btrfs_set_header_level(root->node, 0);
866 	BTRFS_I(inode)->root = root;
867 	ret = -EINVAL;
868 
869 	/*
870 	 * Need a blank inode item here just so we don't confuse
871 	 * btrfs_get_extent.
872 	 */
873 	insert_inode_item_key(root);
874 	insert_extent(root, sectorsize, sectorsize, sectorsize, 0, sectorsize,
875 		      sectorsize, BTRFS_FILE_EXTENT_REG, 0, 1);
876 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, 2 * sectorsize, 0);
877 	if (IS_ERR(em)) {
878 		test_err("got an error when we shouldn't have");
879 		goto out;
880 	}
881 	if (em->block_start != EXTENT_MAP_HOLE) {
882 		test_err("expected a hole, got %llu", em->block_start);
883 		goto out;
884 	}
885 	if (em->start != 0 || em->len != sectorsize) {
886 		test_err(
887 	"unexpected extent wanted start 0 len %u, got start %llu len %llu",
888 			sectorsize, em->start, em->len);
889 		goto out;
890 	}
891 	if (em->flags != vacancy_only) {
892 		test_err("wrong flags, wanted %lu, have %lu", vacancy_only,
893 			 em->flags);
894 		goto out;
895 	}
896 	free_extent_map(em);
897 
898 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, sectorsize,
899 			2 * sectorsize, 0);
900 	if (IS_ERR(em)) {
901 		test_err("got an error when we shouldn't have");
902 		goto out;
903 	}
904 	if (em->block_start != sectorsize) {
905 		test_err("expected a real extent, got %llu", em->block_start);
906 		goto out;
907 	}
908 	if (em->start != sectorsize || em->len != sectorsize) {
909 		test_err(
910 	"unexpected extent wanted start %u len %u, got start %llu len %llu",
911 			sectorsize, sectorsize, em->start, em->len);
912 		goto out;
913 	}
914 	if (em->flags != 0) {
915 		test_err("unexpected flags set, wanted 0 got %lu",
916 			 em->flags);
917 		goto out;
918 	}
919 	ret = 0;
920 out:
921 	if (!IS_ERR(em))
922 		free_extent_map(em);
923 	iput(inode);
924 	btrfs_free_dummy_root(root);
925 	btrfs_free_dummy_fs_info(fs_info);
926 	return ret;
927 }
928 
929 static int test_extent_accounting(u32 sectorsize, u32 nodesize)
930 {
931 	struct btrfs_fs_info *fs_info = NULL;
932 	struct inode *inode = NULL;
933 	struct btrfs_root *root = NULL;
934 	int ret = -ENOMEM;
935 
936 	inode = btrfs_new_test_inode();
937 	if (!inode) {
938 		test_err("couldn't allocate inode");
939 		return ret;
940 	}
941 
942 	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
943 	if (!fs_info) {
944 		test_err("couldn't allocate dummy fs info");
945 		goto out;
946 	}
947 
948 	root = btrfs_alloc_dummy_root(fs_info);
949 	if (IS_ERR(root)) {
950 		test_err("couldn't allocate root");
951 		goto out;
952 	}
953 
954 	BTRFS_I(inode)->root = root;
955 	btrfs_test_inode_set_ops(inode);
956 
957 	/* [BTRFS_MAX_EXTENT_SIZE] */
958 	ret = btrfs_set_extent_delalloc(inode, 0, BTRFS_MAX_EXTENT_SIZE - 1, 0,
959 					NULL, 0);
960 	if (ret) {
961 		test_err("btrfs_set_extent_delalloc returned %d", ret);
962 		goto out;
963 	}
964 	if (BTRFS_I(inode)->outstanding_extents != 1) {
965 		ret = -EINVAL;
966 		test_err("miscount, wanted 1, got %u",
967 			 BTRFS_I(inode)->outstanding_extents);
968 		goto out;
969 	}
970 
971 	/* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
972 	ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE,
973 					BTRFS_MAX_EXTENT_SIZE + sectorsize - 1,
974 					0, NULL, 0);
975 	if (ret) {
976 		test_err("btrfs_set_extent_delalloc returned %d", ret);
977 		goto out;
978 	}
979 	if (BTRFS_I(inode)->outstanding_extents != 2) {
980 		ret = -EINVAL;
981 		test_err("miscount, wanted 2, got %u",
982 			 BTRFS_I(inode)->outstanding_extents);
983 		goto out;
984 	}
985 
986 	/* [BTRFS_MAX_EXTENT_SIZE/2][sectorsize HOLE][the rest] */
987 	ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
988 			       BTRFS_MAX_EXTENT_SIZE >> 1,
989 			       (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1,
990 			       EXTENT_DELALLOC | EXTENT_DIRTY |
991 			       EXTENT_UPTODATE, 0, 0, NULL);
992 	if (ret) {
993 		test_err("clear_extent_bit returned %d", ret);
994 		goto out;
995 	}
996 	if (BTRFS_I(inode)->outstanding_extents != 2) {
997 		ret = -EINVAL;
998 		test_err("miscount, wanted 2, got %u",
999 			 BTRFS_I(inode)->outstanding_extents);
1000 		goto out;
1001 	}
1002 
1003 	/* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
1004 	ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE >> 1,
1005 					(BTRFS_MAX_EXTENT_SIZE >> 1)
1006 					+ sectorsize - 1,
1007 					0, NULL, 0);
1008 	if (ret) {
1009 		test_err("btrfs_set_extent_delalloc returned %d", ret);
1010 		goto out;
1011 	}
1012 	if (BTRFS_I(inode)->outstanding_extents != 2) {
1013 		ret = -EINVAL;
1014 		test_err("miscount, wanted 2, got %u",
1015 			 BTRFS_I(inode)->outstanding_extents);
1016 		goto out;
1017 	}
1018 
1019 	/*
1020 	 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize]
1021 	 */
1022 	ret = btrfs_set_extent_delalloc(inode,
1023 			BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize,
1024 			(BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1,
1025 			0, NULL, 0);
1026 	if (ret) {
1027 		test_err("btrfs_set_extent_delalloc returned %d", ret);
1028 		goto out;
1029 	}
1030 	if (BTRFS_I(inode)->outstanding_extents != 4) {
1031 		ret = -EINVAL;
1032 		test_err("miscount, wanted 4, got %u",
1033 			 BTRFS_I(inode)->outstanding_extents);
1034 		goto out;
1035 	}
1036 
1037 	/*
1038 	* [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize]
1039 	*/
1040 	ret = btrfs_set_extent_delalloc(inode,
1041 			BTRFS_MAX_EXTENT_SIZE + sectorsize,
1042 			BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL, 0);
1043 	if (ret) {
1044 		test_err("btrfs_set_extent_delalloc returned %d", ret);
1045 		goto out;
1046 	}
1047 	if (BTRFS_I(inode)->outstanding_extents != 3) {
1048 		ret = -EINVAL;
1049 		test_err("miscount, wanted 3, got %u",
1050 			 BTRFS_I(inode)->outstanding_extents);
1051 		goto out;
1052 	}
1053 
1054 	/* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */
1055 	ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
1056 			       BTRFS_MAX_EXTENT_SIZE + sectorsize,
1057 			       BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1,
1058 			       EXTENT_DIRTY | EXTENT_DELALLOC |
1059 			       EXTENT_UPTODATE, 0, 0, NULL);
1060 	if (ret) {
1061 		test_err("clear_extent_bit returned %d", ret);
1062 		goto out;
1063 	}
1064 	if (BTRFS_I(inode)->outstanding_extents != 4) {
1065 		ret = -EINVAL;
1066 		test_err("miscount, wanted 4, got %u",
1067 			 BTRFS_I(inode)->outstanding_extents);
1068 		goto out;
1069 	}
1070 
1071 	/*
1072 	 * Refill the hole again just for good measure, because I thought it
1073 	 * might fail and I'd rather satisfy my paranoia at this point.
1074 	 */
1075 	ret = btrfs_set_extent_delalloc(inode,
1076 			BTRFS_MAX_EXTENT_SIZE + sectorsize,
1077 			BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL, 0);
1078 	if (ret) {
1079 		test_err("btrfs_set_extent_delalloc returned %d", ret);
1080 		goto out;
1081 	}
1082 	if (BTRFS_I(inode)->outstanding_extents != 3) {
1083 		ret = -EINVAL;
1084 		test_err("miscount, wanted 3, got %u",
1085 			 BTRFS_I(inode)->outstanding_extents);
1086 		goto out;
1087 	}
1088 
1089 	/* Empty */
1090 	ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1091 			       EXTENT_DIRTY | EXTENT_DELALLOC |
1092 			       EXTENT_UPTODATE, 0, 0, NULL);
1093 	if (ret) {
1094 		test_err("clear_extent_bit returned %d", ret);
1095 		goto out;
1096 	}
1097 	if (BTRFS_I(inode)->outstanding_extents) {
1098 		ret = -EINVAL;
1099 		test_err("miscount, wanted 0, got %u",
1100 			 BTRFS_I(inode)->outstanding_extents);
1101 		goto out;
1102 	}
1103 	ret = 0;
1104 out:
1105 	if (ret)
1106 		clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1107 				 EXTENT_DIRTY | EXTENT_DELALLOC |
1108 				 EXTENT_UPTODATE, 0, 0, NULL);
1109 	iput(inode);
1110 	btrfs_free_dummy_root(root);
1111 	btrfs_free_dummy_fs_info(fs_info);
1112 	return ret;
1113 }
1114 
1115 int btrfs_test_inodes(u32 sectorsize, u32 nodesize)
1116 {
1117 	int ret;
1118 
1119 	set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
1120 	set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
1121 
1122 	test_msg("running btrfs_get_extent tests");
1123 	ret = test_btrfs_get_extent(sectorsize, nodesize);
1124 	if (ret)
1125 		return ret;
1126 	test_msg("running hole first btrfs_get_extent test");
1127 	ret = test_hole_first(sectorsize, nodesize);
1128 	if (ret)
1129 		return ret;
1130 	test_msg("running outstanding_extents tests");
1131 	return test_extent_accounting(sectorsize, nodesize);
1132 }
1133