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