GNU Linux-libre 4.19.245-gnu1
[releases.git] / fs / btrfs / tests / inode-tests.c
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         inode->i_mode = S_IFREG;
236         BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
237         BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
238         BTRFS_I(inode)->location.offset = 0;
239
240         fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
241         if (!fs_info) {
242                 test_err("couldn't allocate dummy fs info");
243                 goto out;
244         }
245
246         root = btrfs_alloc_dummy_root(fs_info);
247         if (IS_ERR(root)) {
248                 test_err("couldn't allocate root");
249                 goto out;
250         }
251
252         root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
253         if (!root->node) {
254                 test_err("couldn't allocate dummy buffer");
255                 goto out;
256         }
257
258         /*
259          * We will just free a dummy node if it's ref count is 2 so we need an
260          * extra ref so our searches don't accidentally release our page.
261          */
262         extent_buffer_get(root->node);
263         btrfs_set_header_nritems(root->node, 0);
264         btrfs_set_header_level(root->node, 0);
265         ret = -EINVAL;
266
267         /* First with no extents */
268         BTRFS_I(inode)->root = root;
269         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, sectorsize, 0);
270         if (IS_ERR(em)) {
271                 em = NULL;
272                 test_err("got an error when we shouldn't have");
273                 goto out;
274         }
275         if (em->block_start != EXTENT_MAP_HOLE) {
276                 test_err("expected a hole, got %llu", em->block_start);
277                 goto out;
278         }
279         free_extent_map(em);
280         btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
281
282         /*
283          * All of the magic numbers are based on the mapping setup in
284          * setup_file_extents, so if you change anything there you need to
285          * update the comment and update the expected values below.
286          */
287         setup_file_extents(root, sectorsize);
288
289         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, (u64)-1, 0);
290         if (IS_ERR(em)) {
291                 test_err("got an error when we shouldn't have");
292                 goto out;
293         }
294         if (em->block_start != EXTENT_MAP_HOLE) {
295                 test_err("expected a hole, got %llu", em->block_start);
296                 goto out;
297         }
298         if (em->start != 0 || em->len != 5) {
299                 test_err(
300                 "unexpected extent wanted start 0 len 5, got start %llu len %llu",
301                         em->start, em->len);
302                 goto out;
303         }
304         if (em->flags != 0) {
305                 test_err("unexpected flags set, want 0 have %lu", em->flags);
306                 goto out;
307         }
308         offset = em->start + em->len;
309         free_extent_map(em);
310
311         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
312         if (IS_ERR(em)) {
313                 test_err("got an error when we shouldn't have");
314                 goto out;
315         }
316         if (em->block_start != EXTENT_MAP_INLINE) {
317                 test_err("expected an inline, got %llu", em->block_start);
318                 goto out;
319         }
320
321         if (em->start != offset || em->len != (sectorsize - 5)) {
322                 test_err(
323         "unexpected extent wanted start %llu len 1, got start %llu len %llu",
324                         offset, em->start, em->len);
325                 goto out;
326         }
327         if (em->flags != 0) {
328                 test_err("unexpected flags set, want 0 have %lu", em->flags);
329                 goto out;
330         }
331         /*
332          * We don't test anything else for inline since it doesn't get set
333          * unless we have a page for it to write into.  Maybe we should change
334          * this?
335          */
336         offset = em->start + em->len;
337         free_extent_map(em);
338
339         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
340         if (IS_ERR(em)) {
341                 test_err("got an error when we shouldn't have");
342                 goto out;
343         }
344         if (em->block_start != EXTENT_MAP_HOLE) {
345                 test_err("expected a hole, got %llu", em->block_start);
346                 goto out;
347         }
348         if (em->start != offset || em->len != 4) {
349                 test_err(
350         "unexpected extent wanted start %llu len 4, got start %llu len %llu",
351                         offset, em->start, em->len);
352                 goto out;
353         }
354         if (em->flags != 0) {
355                 test_err("unexpected flags set, want 0 have %lu", em->flags);
356                 goto out;
357         }
358         offset = em->start + em->len;
359         free_extent_map(em);
360
361         /* Regular extent */
362         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
363         if (IS_ERR(em)) {
364                 test_err("got an error when we shouldn't have");
365                 goto out;
366         }
367         if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
368                 test_err("expected a real extent, got %llu", em->block_start);
369                 goto out;
370         }
371         if (em->start != offset || em->len != sectorsize - 1) {
372                 test_err(
373         "unexpected extent wanted start %llu len 4095, got start %llu len %llu",
374                         offset, em->start, em->len);
375                 goto out;
376         }
377         if (em->flags != 0) {
378                 test_err("unexpected flags set, want 0 have %lu", em->flags);
379                 goto out;
380         }
381         if (em->orig_start != em->start) {
382                 test_err("wrong orig offset, want %llu, have %llu", em->start,
383                          em->orig_start);
384                 goto out;
385         }
386         offset = em->start + em->len;
387         free_extent_map(em);
388
389         /* The next 3 are split extents */
390         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
391         if (IS_ERR(em)) {
392                 test_err("got an error when we shouldn't have");
393                 goto out;
394         }
395         if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
396                 test_err("expected a real extent, got %llu", em->block_start);
397                 goto out;
398         }
399         if (em->start != offset || em->len != sectorsize) {
400                 test_err(
401                 "unexpected extent start %llu len %u, got start %llu len %llu",
402                         offset, sectorsize, em->start, em->len);
403                 goto out;
404         }
405         if (em->flags != 0) {
406                 test_err("unexpected flags set, want 0 have %lu", em->flags);
407                 goto out;
408         }
409         if (em->orig_start != em->start) {
410                 test_err("wrong orig offset, want %llu, have %llu", em->start,
411                          em->orig_start);
412                 goto out;
413         }
414         disk_bytenr = em->block_start;
415         orig_start = em->start;
416         offset = em->start + em->len;
417         free_extent_map(em);
418
419         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
420         if (IS_ERR(em)) {
421                 test_err("got an error when we shouldn't have");
422                 goto out;
423         }
424         if (em->block_start != EXTENT_MAP_HOLE) {
425                 test_err("expected a hole, got %llu", em->block_start);
426                 goto out;
427         }
428         if (em->start != offset || em->len != sectorsize) {
429                 test_err(
430         "unexpected extent wanted start %llu len %u, got start %llu len %llu",
431                         offset, sectorsize, em->start, em->len);
432                 goto out;
433         }
434         if (em->flags != 0) {
435                 test_err("unexpected flags set, want 0 have %lu", em->flags);
436                 goto out;
437         }
438         offset = em->start + em->len;
439         free_extent_map(em);
440
441         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
442         if (IS_ERR(em)) {
443                 test_err("got an error when we shouldn't have");
444                 goto out;
445         }
446         if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
447                 test_err("expected a real extent, got %llu", em->block_start);
448                 goto out;
449         }
450         if (em->start != offset || em->len != 2 * sectorsize) {
451                 test_err(
452         "unexpected extent wanted start %llu len %u, got start %llu len %llu",
453                         offset, 2 * sectorsize, em->start, em->len);
454                 goto out;
455         }
456         if (em->flags != 0) {
457                 test_err("unexpected flags set, want 0 have %lu", em->flags);
458                 goto out;
459         }
460         if (em->orig_start != orig_start) {
461                 test_err("wrong orig offset, want %llu, have %llu",
462                          orig_start, em->orig_start);
463                 goto out;
464         }
465         disk_bytenr += (em->start - orig_start);
466         if (em->block_start != disk_bytenr) {
467                 test_err("wrong block start, want %llu, have %llu",
468                          disk_bytenr, em->block_start);
469                 goto out;
470         }
471         offset = em->start + em->len;
472         free_extent_map(em);
473
474         /* Prealloc extent */
475         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
476         if (IS_ERR(em)) {
477                 test_err("got an error when we shouldn't have");
478                 goto out;
479         }
480         if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
481                 test_err("expected a real extent, got %llu", em->block_start);
482                 goto out;
483         }
484         if (em->start != offset || em->len != sectorsize) {
485                 test_err(
486         "unexpected extent wanted start %llu len %u, got start %llu len %llu",
487                         offset, sectorsize, em->start, em->len);
488                 goto out;
489         }
490         if (em->flags != prealloc_only) {
491                 test_err("unexpected flags set, want %lu have %lu",
492                          prealloc_only, em->flags);
493                 goto out;
494         }
495         if (em->orig_start != em->start) {
496                 test_err("wrong orig offset, want %llu, have %llu", em->start,
497                          em->orig_start);
498                 goto out;
499         }
500         offset = em->start + em->len;
501         free_extent_map(em);
502
503         /* The next 3 are a half written prealloc extent */
504         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
505         if (IS_ERR(em)) {
506                 test_err("got an error when we shouldn't have");
507                 goto out;
508         }
509         if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
510                 test_err("expected a real extent, got %llu", em->block_start);
511                 goto out;
512         }
513         if (em->start != offset || em->len != sectorsize) {
514                 test_err(
515         "unexpected extent wanted start %llu len %u, got start %llu len %llu",
516                         offset, sectorsize, em->start, em->len);
517                 goto out;
518         }
519         if (em->flags != prealloc_only) {
520                 test_err("unexpected flags set, want %lu have %lu",
521                          prealloc_only, em->flags);
522                 goto out;
523         }
524         if (em->orig_start != em->start) {
525                 test_err("wrong orig offset, want %llu, have %llu", em->start,
526                          em->orig_start);
527                 goto out;
528         }
529         disk_bytenr = em->block_start;
530         orig_start = em->start;
531         offset = em->start + em->len;
532         free_extent_map(em);
533
534         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
535         if (IS_ERR(em)) {
536                 test_err("got an error when we shouldn't have");
537                 goto out;
538         }
539         if (em->block_start >= EXTENT_MAP_HOLE) {
540                 test_err("expected a real extent, got %llu", em->block_start);
541                 goto out;
542         }
543         if (em->start != offset || em->len != sectorsize) {
544                 test_err(
545         "unexpected extent wanted start %llu len %u, got start %llu len %llu",
546                         offset, sectorsize, em->start, em->len);
547                 goto out;
548         }
549         if (em->flags != 0) {
550                 test_err("unexpected flags set, want 0 have %lu", em->flags);
551                 goto out;
552         }
553         if (em->orig_start != orig_start) {
554                 test_err("unexpected orig offset, wanted %llu, have %llu",
555                          orig_start, em->orig_start);
556                 goto out;
557         }
558         if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
559                 test_err("unexpected block start, wanted %llu, have %llu",
560                          disk_bytenr + (em->start - em->orig_start),
561                          em->block_start);
562                 goto out;
563         }
564         offset = em->start + em->len;
565         free_extent_map(em);
566
567         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
568         if (IS_ERR(em)) {
569                 test_err("got an error when we shouldn't have");
570                 goto out;
571         }
572         if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
573                 test_err("expected a real extent, got %llu", em->block_start);
574                 goto out;
575         }
576         if (em->start != offset || em->len != 2 * sectorsize) {
577                 test_err(
578         "unexpected extent wanted start %llu len %u, got start %llu len %llu",
579                         offset, 2 * sectorsize, em->start, em->len);
580                 goto out;
581         }
582         if (em->flags != prealloc_only) {
583                 test_err("unexpected flags set, want %lu have %lu",
584                          prealloc_only, em->flags);
585                 goto out;
586         }
587         if (em->orig_start != orig_start) {
588                 test_err("wrong orig offset, want %llu, have %llu", orig_start,
589                          em->orig_start);
590                 goto out;
591         }
592         if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
593                 test_err("unexpected block start, wanted %llu, have %llu",
594                          disk_bytenr + (em->start - em->orig_start),
595                          em->block_start);
596                 goto out;
597         }
598         offset = em->start + em->len;
599         free_extent_map(em);
600
601         /* Now for the compressed extent */
602         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
603         if (IS_ERR(em)) {
604                 test_err("got an error when we shouldn't have");
605                 goto out;
606         }
607         if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
608                 test_err("expected a real extent, got %llu", em->block_start);
609                 goto out;
610         }
611         if (em->start != offset || em->len != 2 * sectorsize) {
612                 test_err(
613         "unexpected extent wanted start %llu len %u, got start %llu len %llu",
614                         offset, 2 * sectorsize, em->start, em->len);
615                 goto out;
616         }
617         if (em->flags != compressed_only) {
618                 test_err("unexpected flags set, want %lu have %lu",
619                          compressed_only, em->flags);
620                 goto out;
621         }
622         if (em->orig_start != em->start) {
623                 test_err("wrong orig offset, want %llu, have %llu",
624                          em->start, em->orig_start);
625                 goto out;
626         }
627         if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
628                 test_err("unexpected compress type, wanted %d, got %d",
629                          BTRFS_COMPRESS_ZLIB, em->compress_type);
630                 goto out;
631         }
632         offset = em->start + em->len;
633         free_extent_map(em);
634
635         /* Split compressed extent */
636         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
637         if (IS_ERR(em)) {
638                 test_err("got an error when we shouldn't have");
639                 goto out;
640         }
641         if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
642                 test_err("expected a real extent, got %llu", em->block_start);
643                 goto out;
644         }
645         if (em->start != offset || em->len != sectorsize) {
646                 test_err(
647         "unexpected extent wanted start %llu len %u, got start %llu len %llu",
648                         offset, sectorsize, em->start, em->len);
649                 goto out;
650         }
651         if (em->flags != compressed_only) {
652                 test_err("unexpected flags set, want %lu have %lu",
653                          compressed_only, em->flags);
654                 goto out;
655         }
656         if (em->orig_start != em->start) {
657                 test_err("wrong orig offset, want %llu, have %llu",
658                          em->start, em->orig_start);
659                 goto out;
660         }
661         if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
662                 test_err("unexpected compress type, wanted %d, got %d",
663                          BTRFS_COMPRESS_ZLIB, em->compress_type);
664                 goto out;
665         }
666         disk_bytenr = em->block_start;
667         orig_start = em->start;
668         offset = em->start + em->len;
669         free_extent_map(em);
670
671         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
672         if (IS_ERR(em)) {
673                 test_err("got an error when we shouldn't have");
674                 goto out;
675         }
676         if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
677                 test_err("expected a real extent, got %llu", em->block_start);
678                 goto out;
679         }
680         if (em->start != offset || em->len != sectorsize) {
681                 test_err(
682         "unexpected extent wanted start %llu len %u, got start %llu len %llu",
683                         offset, sectorsize, em->start, em->len);
684                 goto out;
685         }
686         if (em->flags != 0) {
687                 test_err("unexpected flags set, want 0 have %lu", em->flags);
688                 goto out;
689         }
690         if (em->orig_start != em->start) {
691                 test_err("wrong orig offset, want %llu, have %llu", em->start,
692                          em->orig_start);
693                 goto out;
694         }
695         offset = em->start + em->len;
696         free_extent_map(em);
697
698         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
699         if (IS_ERR(em)) {
700                 test_err("got an error when we shouldn't have");
701                 goto out;
702         }
703         if (em->block_start != disk_bytenr) {
704                 test_err("block start does not match, want %llu got %llu",
705                          disk_bytenr, em->block_start);
706                 goto out;
707         }
708         if (em->start != offset || em->len != 2 * sectorsize) {
709                 test_err(
710         "unexpected extent wanted start %llu len %u, got start %llu len %llu",
711                         offset, 2 * sectorsize, em->start, em->len);
712                 goto out;
713         }
714         if (em->flags != compressed_only) {
715                 test_err("unexpected flags set, want %lu have %lu",
716                          compressed_only, em->flags);
717                 goto out;
718         }
719         if (em->orig_start != orig_start) {
720                 test_err("wrong orig offset, want %llu, have %llu",
721                          em->start, orig_start);
722                 goto out;
723         }
724         if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
725                 test_err("unexpected compress type, wanted %d, got %d",
726                          BTRFS_COMPRESS_ZLIB, em->compress_type);
727                 goto out;
728         }
729         offset = em->start + em->len;
730         free_extent_map(em);
731
732         /* A hole between regular extents but no hole extent */
733         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset + 6,
734                         sectorsize, 0);
735         if (IS_ERR(em)) {
736                 test_err("got an error when we shouldn't have");
737                 goto out;
738         }
739         if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
740                 test_err("expected a real extent, got %llu", em->block_start);
741                 goto out;
742         }
743         if (em->start != offset || em->len != sectorsize) {
744                 test_err(
745         "unexpected extent wanted start %llu len %u, got start %llu len %llu",
746                         offset, sectorsize, em->start, em->len);
747                 goto out;
748         }
749         if (em->flags != 0) {
750                 test_err("unexpected flags set, want 0 have %lu", em->flags);
751                 goto out;
752         }
753         if (em->orig_start != em->start) {
754                 test_err("wrong orig offset, want %llu, have %llu", em->start,
755                          em->orig_start);
756                 goto out;
757         }
758         offset = em->start + em->len;
759         free_extent_map(em);
760
761         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, SZ_4M, 0);
762         if (IS_ERR(em)) {
763                 test_err("got an error when we shouldn't have");
764                 goto out;
765         }
766         if (em->block_start != EXTENT_MAP_HOLE) {
767                 test_err("expected a hole extent, got %llu", em->block_start);
768                 goto out;
769         }
770         /*
771          * Currently we just return a length that we requested rather than the
772          * length of the actual hole, if this changes we'll have to change this
773          * test.
774          */
775         if (em->start != offset || em->len != 3 * sectorsize) {
776                 test_err(
777         "unexpected extent wanted start %llu len %u, got start %llu len %llu",
778                         offset, 3 * sectorsize, em->start, em->len);
779                 goto out;
780         }
781         if (em->flags != vacancy_only) {
782                 test_err("unexpected flags set, want %lu have %lu",
783                          vacancy_only, em->flags);
784                 goto out;
785         }
786         if (em->orig_start != em->start) {
787                 test_err("wrong orig offset, want %llu, have %llu", em->start,
788                          em->orig_start);
789                 goto out;
790         }
791         offset = em->start + em->len;
792         free_extent_map(em);
793
794         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
795         if (IS_ERR(em)) {
796                 test_err("got an error when we shouldn't have");
797                 goto out;
798         }
799         if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
800                 test_err("expected a real extent, got %llu", em->block_start);
801                 goto out;
802         }
803         if (em->start != offset || em->len != sectorsize) {
804                 test_err(
805         "unexpected extent wanted start %llu len %u, got start %llu len %llu",
806                         offset, sectorsize, em->start, em->len);
807                 goto out;
808         }
809         if (em->flags != 0) {
810                 test_err("unexpected flags set, want 0 have %lu", em->flags);
811                 goto out;
812         }
813         if (em->orig_start != em->start) {
814                 test_err("wrong orig offset, want %llu, have %llu", em->start,
815                          em->orig_start);
816                 goto out;
817         }
818         ret = 0;
819 out:
820         if (!IS_ERR(em))
821                 free_extent_map(em);
822         iput(inode);
823         btrfs_free_dummy_root(root);
824         btrfs_free_dummy_fs_info(fs_info);
825         return ret;
826 }
827
828 static int test_hole_first(u32 sectorsize, u32 nodesize)
829 {
830         struct btrfs_fs_info *fs_info = NULL;
831         struct inode *inode = NULL;
832         struct btrfs_root *root = NULL;
833         struct extent_map *em = NULL;
834         int ret = -ENOMEM;
835
836         inode = btrfs_new_test_inode();
837         if (!inode) {
838                 test_err("couldn't allocate inode");
839                 return ret;
840         }
841
842         BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
843         BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
844         BTRFS_I(inode)->location.offset = 0;
845
846         fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
847         if (!fs_info) {
848                 test_err("couldn't allocate dummy fs info");
849                 goto out;
850         }
851
852         root = btrfs_alloc_dummy_root(fs_info);
853         if (IS_ERR(root)) {
854                 test_err("couldn't allocate root");
855                 goto out;
856         }
857
858         root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
859         if (!root->node) {
860                 test_err("couldn't allocate dummy buffer");
861                 goto out;
862         }
863
864         extent_buffer_get(root->node);
865         btrfs_set_header_nritems(root->node, 0);
866         btrfs_set_header_level(root->node, 0);
867         BTRFS_I(inode)->root = root;
868         ret = -EINVAL;
869
870         /*
871          * Need a blank inode item here just so we don't confuse
872          * btrfs_get_extent.
873          */
874         insert_inode_item_key(root);
875         insert_extent(root, sectorsize, sectorsize, sectorsize, 0, sectorsize,
876                       sectorsize, BTRFS_FILE_EXTENT_REG, 0, 1);
877         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, 2 * sectorsize, 0);
878         if (IS_ERR(em)) {
879                 test_err("got an error when we shouldn't have");
880                 goto out;
881         }
882         if (em->block_start != EXTENT_MAP_HOLE) {
883                 test_err("expected a hole, got %llu", em->block_start);
884                 goto out;
885         }
886         if (em->start != 0 || em->len != sectorsize) {
887                 test_err(
888         "unexpected extent wanted start 0 len %u, got start %llu len %llu",
889                         sectorsize, em->start, em->len);
890                 goto out;
891         }
892         if (em->flags != vacancy_only) {
893                 test_err("wrong flags, wanted %lu, have %lu", vacancy_only,
894                          em->flags);
895                 goto out;
896         }
897         free_extent_map(em);
898
899         em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, sectorsize,
900                         2 * sectorsize, 0);
901         if (IS_ERR(em)) {
902                 test_err("got an error when we shouldn't have");
903                 goto out;
904         }
905         if (em->block_start != sectorsize) {
906                 test_err("expected a real extent, got %llu", em->block_start);
907                 goto out;
908         }
909         if (em->start != sectorsize || em->len != sectorsize) {
910                 test_err(
911         "unexpected extent wanted start %u len %u, got start %llu len %llu",
912                         sectorsize, sectorsize, em->start, em->len);
913                 goto out;
914         }
915         if (em->flags != 0) {
916                 test_err("unexpected flags set, wanted 0 got %lu",
917                          em->flags);
918                 goto out;
919         }
920         ret = 0;
921 out:
922         if (!IS_ERR(em))
923                 free_extent_map(em);
924         iput(inode);
925         btrfs_free_dummy_root(root);
926         btrfs_free_dummy_fs_info(fs_info);
927         return ret;
928 }
929
930 static int test_extent_accounting(u32 sectorsize, u32 nodesize)
931 {
932         struct btrfs_fs_info *fs_info = NULL;
933         struct inode *inode = NULL;
934         struct btrfs_root *root = NULL;
935         int ret = -ENOMEM;
936
937         inode = btrfs_new_test_inode();
938         if (!inode) {
939                 test_err("couldn't allocate inode");
940                 return ret;
941         }
942
943         fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
944         if (!fs_info) {
945                 test_err("couldn't allocate dummy fs info");
946                 goto out;
947         }
948
949         root = btrfs_alloc_dummy_root(fs_info);
950         if (IS_ERR(root)) {
951                 test_err("couldn't allocate root");
952                 goto out;
953         }
954
955         BTRFS_I(inode)->root = root;
956         btrfs_test_inode_set_ops(inode);
957
958         /* [BTRFS_MAX_EXTENT_SIZE] */
959         ret = btrfs_set_extent_delalloc(inode, 0, BTRFS_MAX_EXTENT_SIZE - 1, 0,
960                                         NULL, 0);
961         if (ret) {
962                 test_err("btrfs_set_extent_delalloc returned %d", ret);
963                 goto out;
964         }
965         if (BTRFS_I(inode)->outstanding_extents != 1) {
966                 ret = -EINVAL;
967                 test_err("miscount, wanted 1, got %u",
968                          BTRFS_I(inode)->outstanding_extents);
969                 goto out;
970         }
971
972         /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
973         ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE,
974                                         BTRFS_MAX_EXTENT_SIZE + sectorsize - 1,
975                                         0, NULL, 0);
976         if (ret) {
977                 test_err("btrfs_set_extent_delalloc returned %d", ret);
978                 goto out;
979         }
980         if (BTRFS_I(inode)->outstanding_extents != 2) {
981                 ret = -EINVAL;
982                 test_err("miscount, wanted 2, got %u",
983                          BTRFS_I(inode)->outstanding_extents);
984                 goto out;
985         }
986
987         /* [BTRFS_MAX_EXTENT_SIZE/2][sectorsize HOLE][the rest] */
988         ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
989                                BTRFS_MAX_EXTENT_SIZE >> 1,
990                                (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1,
991                                EXTENT_DELALLOC | EXTENT_DIRTY |
992                                EXTENT_UPTODATE, 0, 0, NULL);
993         if (ret) {
994                 test_err("clear_extent_bit returned %d", ret);
995                 goto out;
996         }
997         if (BTRFS_I(inode)->outstanding_extents != 2) {
998                 ret = -EINVAL;
999                 test_err("miscount, wanted 2, got %u",
1000                          BTRFS_I(inode)->outstanding_extents);
1001                 goto out;
1002         }
1003
1004         /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
1005         ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE >> 1,
1006                                         (BTRFS_MAX_EXTENT_SIZE >> 1)
1007                                         + sectorsize - 1,
1008                                         0, NULL, 0);
1009         if (ret) {
1010                 test_err("btrfs_set_extent_delalloc returned %d", ret);
1011                 goto out;
1012         }
1013         if (BTRFS_I(inode)->outstanding_extents != 2) {
1014                 ret = -EINVAL;
1015                 test_err("miscount, wanted 2, got %u",
1016                          BTRFS_I(inode)->outstanding_extents);
1017                 goto out;
1018         }
1019
1020         /*
1021          * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize]
1022          */
1023         ret = btrfs_set_extent_delalloc(inode,
1024                         BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize,
1025                         (BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1,
1026                         0, NULL, 0);
1027         if (ret) {
1028                 test_err("btrfs_set_extent_delalloc returned %d", ret);
1029                 goto out;
1030         }
1031         if (BTRFS_I(inode)->outstanding_extents != 4) {
1032                 ret = -EINVAL;
1033                 test_err("miscount, wanted 4, got %u",
1034                          BTRFS_I(inode)->outstanding_extents);
1035                 goto out;
1036         }
1037
1038         /*
1039         * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize]
1040         */
1041         ret = btrfs_set_extent_delalloc(inode,
1042                         BTRFS_MAX_EXTENT_SIZE + sectorsize,
1043                         BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL, 0);
1044         if (ret) {
1045                 test_err("btrfs_set_extent_delalloc returned %d", ret);
1046                 goto out;
1047         }
1048         if (BTRFS_I(inode)->outstanding_extents != 3) {
1049                 ret = -EINVAL;
1050                 test_err("miscount, wanted 3, got %u",
1051                          BTRFS_I(inode)->outstanding_extents);
1052                 goto out;
1053         }
1054
1055         /* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */
1056         ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
1057                                BTRFS_MAX_EXTENT_SIZE + sectorsize,
1058                                BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1,
1059                                EXTENT_DIRTY | EXTENT_DELALLOC |
1060                                EXTENT_UPTODATE, 0, 0, NULL);
1061         if (ret) {
1062                 test_err("clear_extent_bit returned %d", ret);
1063                 goto out;
1064         }
1065         if (BTRFS_I(inode)->outstanding_extents != 4) {
1066                 ret = -EINVAL;
1067                 test_err("miscount, wanted 4, got %u",
1068                          BTRFS_I(inode)->outstanding_extents);
1069                 goto out;
1070         }
1071
1072         /*
1073          * Refill the hole again just for good measure, because I thought it
1074          * might fail and I'd rather satisfy my paranoia at this point.
1075          */
1076         ret = btrfs_set_extent_delalloc(inode,
1077                         BTRFS_MAX_EXTENT_SIZE + sectorsize,
1078                         BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL, 0);
1079         if (ret) {
1080                 test_err("btrfs_set_extent_delalloc returned %d", ret);
1081                 goto out;
1082         }
1083         if (BTRFS_I(inode)->outstanding_extents != 3) {
1084                 ret = -EINVAL;
1085                 test_err("miscount, wanted 3, got %u",
1086                          BTRFS_I(inode)->outstanding_extents);
1087                 goto out;
1088         }
1089
1090         /* Empty */
1091         ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1092                                EXTENT_DIRTY | EXTENT_DELALLOC |
1093                                EXTENT_UPTODATE, 0, 0, NULL);
1094         if (ret) {
1095                 test_err("clear_extent_bit returned %d", ret);
1096                 goto out;
1097         }
1098         if (BTRFS_I(inode)->outstanding_extents) {
1099                 ret = -EINVAL;
1100                 test_err("miscount, wanted 0, got %u",
1101                          BTRFS_I(inode)->outstanding_extents);
1102                 goto out;
1103         }
1104         ret = 0;
1105 out:
1106         if (ret)
1107                 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
1108                                  EXTENT_DIRTY | EXTENT_DELALLOC |
1109                                  EXTENT_UPTODATE, 0, 0, NULL);
1110         iput(inode);
1111         btrfs_free_dummy_root(root);
1112         btrfs_free_dummy_fs_info(fs_info);
1113         return ret;
1114 }
1115
1116 int btrfs_test_inodes(u32 sectorsize, u32 nodesize)
1117 {
1118         int ret;
1119
1120         set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
1121         set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
1122
1123         test_msg("running btrfs_get_extent tests");
1124         ret = test_btrfs_get_extent(sectorsize, nodesize);
1125         if (ret)
1126                 return ret;
1127         test_msg("running hole first btrfs_get_extent test");
1128         ret = test_hole_first(sectorsize, nodesize);
1129         if (ret)
1130                 return ret;
1131         test_msg("running outstanding_extents tests");
1132         return test_extent_accounting(sectorsize, nodesize);
1133 }