blob: 73e96d505f4f7d5d8f203fe5938593e77e47971c [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Josef Bacik294e30f2013-10-09 12:00:56 -04002/*
3 * Copyright (C) 2013 Fusion IO. All rights reserved.
Josef Bacik294e30f2013-10-09 12:00:56 -04004 */
5
6#include <linux/pagemap.h>
7#include <linux/sched.h>
Omar Sandoval0f331222015-09-29 20:50:31 -07008#include <linux/slab.h>
Byongho Leeee221842015-12-15 01:42:10 +09009#include <linux/sizes.h>
Josef Bacik294e30f2013-10-09 12:00:56 -040010#include "btrfs-tests.h"
Feifei Xued9e4af2016-06-01 19:18:26 +080011#include "../ctree.h"
Josef Bacik294e30f2013-10-09 12:00:56 -040012#include "../extent_io.h"
Goldwyn Rodrigues99780592019-06-21 10:02:54 -050013#include "../btrfs_inode.h"
Josef Bacik294e30f2013-10-09 12:00:56 -040014
15#define PROCESS_UNLOCK (1 << 0)
16#define PROCESS_RELEASE (1 << 1)
17#define PROCESS_TEST_LOCKED (1 << 2)
18
19static noinline int process_page_range(struct inode *inode, u64 start, u64 end,
20 unsigned long flags)
21{
22 int ret;
23 struct page *pages[16];
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030024 unsigned long index = start >> PAGE_SHIFT;
25 unsigned long end_index = end >> PAGE_SHIFT;
Josef Bacik294e30f2013-10-09 12:00:56 -040026 unsigned long nr_pages = end_index - index + 1;
27 int i;
28 int count = 0;
29 int loops = 0;
30
31 while (nr_pages > 0) {
32 ret = find_get_pages_contig(inode->i_mapping, index,
33 min_t(unsigned long, nr_pages,
34 ARRAY_SIZE(pages)), pages);
35 for (i = 0; i < ret; i++) {
36 if (flags & PROCESS_TEST_LOCKED &&
37 !PageLocked(pages[i]))
38 count++;
39 if (flags & PROCESS_UNLOCK && PageLocked(pages[i]))
40 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030041 put_page(pages[i]);
Josef Bacik294e30f2013-10-09 12:00:56 -040042 if (flags & PROCESS_RELEASE)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030043 put_page(pages[i]);
Josef Bacik294e30f2013-10-09 12:00:56 -040044 }
45 nr_pages -= ret;
46 index += ret;
47 cond_resched();
48 loops++;
49 if (loops > 100000) {
David Sterba3c7251f2018-05-17 00:00:42 +020050 printk(KERN_ERR
51 "stuck in a loop, start %llu, end %llu, nr_pages %lu, ret %d\n",
52 start, end, nr_pages, ret);
Josef Bacik294e30f2013-10-09 12:00:56 -040053 break;
54 }
55 }
56 return count;
57}
58
Feifei Xub9ef22d2016-06-01 19:18:25 +080059static int test_find_delalloc(u32 sectorsize)
Josef Bacik294e30f2013-10-09 12:00:56 -040060{
61 struct inode *inode;
Goldwyn Rodrigues99780592019-06-21 10:02:54 -050062 struct extent_io_tree *tmp;
Josef Bacik294e30f2013-10-09 12:00:56 -040063 struct page *page;
64 struct page *locked_page = NULL;
65 unsigned long index = 0;
Qu Wenruod9cb2452018-11-03 17:24:52 +080066 /* In this test we need at least 2 file extents at its maximum size */
67 u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
68 u64 total_dirty = 2 * max_bytes;
Josef Bacik294e30f2013-10-09 12:00:56 -040069 u64 start, end, test_start;
Lu Fengqi3522e902018-11-29 11:33:38 +080070 bool found;
Josef Bacik294e30f2013-10-09 12:00:56 -040071 int ret = -EINVAL;
72
David Sterba315b76b2018-05-17 00:00:44 +020073 test_msg("running find delalloc tests");
Omar Sandoval0f331222015-09-29 20:50:31 -070074
Josef Bacik294e30f2013-10-09 12:00:56 -040075 inode = btrfs_new_test_inode();
76 if (!inode) {
David Sterba6a060db2019-03-15 17:28:46 +010077 test_std_err(TEST_ALLOC_INODE);
Josef Bacik294e30f2013-10-09 12:00:56 -040078 return -ENOMEM;
79 }
Goldwyn Rodrigues99780592019-06-21 10:02:54 -050080 tmp = &BTRFS_I(inode)->io_tree;
Josef Bacik294e30f2013-10-09 12:00:56 -040081
Qu Wenruo43eb5f22019-03-01 10:47:59 +080082 /*
83 * Passing NULL as we don't have fs_info but tracepoints are not used
84 * at this point
85 */
Goldwyn Rodrigues99780592019-06-21 10:02:54 -050086 extent_io_tree_init(NULL, tmp, IO_TREE_SELFTEST, NULL);
Josef Bacik294e30f2013-10-09 12:00:56 -040087
88 /*
89 * First go through and create and mark all of our pages dirty, we pin
90 * everything to make sure our pages don't get evicted and screw up our
91 * test.
92 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030093 for (index = 0; index < (total_dirty >> PAGE_SHIFT); index++) {
David Sterba8cce83b2016-01-22 10:28:24 +010094 page = find_or_create_page(inode->i_mapping, index, GFP_KERNEL);
Josef Bacik294e30f2013-10-09 12:00:56 -040095 if (!page) {
David Sterba3c7251f2018-05-17 00:00:42 +020096 test_err("failed to allocate test page");
Josef Bacik294e30f2013-10-09 12:00:56 -040097 ret = -ENOMEM;
98 goto out;
99 }
100 SetPageDirty(page);
101 if (index) {
102 unlock_page(page);
103 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300104 get_page(page);
Josef Bacik294e30f2013-10-09 12:00:56 -0400105 locked_page = page;
106 }
107 }
108
109 /* Test this scenario
110 * |--- delalloc ---|
111 * |--- search ---|
112 */
Goldwyn Rodrigues99780592019-06-21 10:02:54 -0500113 set_extent_delalloc(tmp, 0, sectorsize - 1, 0, NULL);
Josef Bacik294e30f2013-10-09 12:00:56 -0400114 start = 0;
115 end = 0;
Goldwyn Rodrigues99780592019-06-21 10:02:54 -0500116 found = find_lock_delalloc_range(inode, locked_page, &start,
Nikolay Borisov917aace2018-10-26 14:43:20 +0300117 &end);
Josef Bacik294e30f2013-10-09 12:00:56 -0400118 if (!found) {
David Sterba3c7251f2018-05-17 00:00:42 +0200119 test_err("should have found at least one delalloc");
Josef Bacik294e30f2013-10-09 12:00:56 -0400120 goto out_bits;
121 }
Feifei Xub9ef22d2016-06-01 19:18:25 +0800122 if (start != 0 || end != (sectorsize - 1)) {
David Sterba3c7251f2018-05-17 00:00:42 +0200123 test_err("expected start 0 end %u, got start %llu end %llu",
Feifei Xub9ef22d2016-06-01 19:18:25 +0800124 sectorsize - 1, start, end);
Josef Bacik294e30f2013-10-09 12:00:56 -0400125 goto out_bits;
126 }
Goldwyn Rodrigues99780592019-06-21 10:02:54 -0500127 unlock_extent(tmp, start, end);
Josef Bacik294e30f2013-10-09 12:00:56 -0400128 unlock_page(locked_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300129 put_page(locked_page);
Josef Bacik294e30f2013-10-09 12:00:56 -0400130
131 /*
132 * Test this scenario
133 *
134 * |--- delalloc ---|
135 * |--- search ---|
136 */
Byongho Leeee221842015-12-15 01:42:10 +0900137 test_start = SZ_64M;
Josef Bacik294e30f2013-10-09 12:00:56 -0400138 locked_page = find_lock_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300139 test_start >> PAGE_SHIFT);
Josef Bacik294e30f2013-10-09 12:00:56 -0400140 if (!locked_page) {
David Sterba3c7251f2018-05-17 00:00:42 +0200141 test_err("couldn't find the locked page");
Josef Bacik294e30f2013-10-09 12:00:56 -0400142 goto out_bits;
143 }
Goldwyn Rodrigues99780592019-06-21 10:02:54 -0500144 set_extent_delalloc(tmp, sectorsize, max_bytes - 1, 0, NULL);
Josef Bacik294e30f2013-10-09 12:00:56 -0400145 start = test_start;
146 end = 0;
Goldwyn Rodrigues99780592019-06-21 10:02:54 -0500147 found = find_lock_delalloc_range(inode, locked_page, &start,
Nikolay Borisov917aace2018-10-26 14:43:20 +0300148 &end);
Josef Bacik294e30f2013-10-09 12:00:56 -0400149 if (!found) {
David Sterba3c7251f2018-05-17 00:00:42 +0200150 test_err("couldn't find delalloc in our range");
Josef Bacik294e30f2013-10-09 12:00:56 -0400151 goto out_bits;
152 }
153 if (start != test_start || end != max_bytes - 1) {
David Sterba3c7251f2018-05-17 00:00:42 +0200154 test_err("expected start %llu end %llu, got start %llu, end %llu",
155 test_start, max_bytes - 1, start, end);
Josef Bacik294e30f2013-10-09 12:00:56 -0400156 goto out_bits;
157 }
158 if (process_page_range(inode, start, end,
159 PROCESS_TEST_LOCKED | PROCESS_UNLOCK)) {
David Sterba3c7251f2018-05-17 00:00:42 +0200160 test_err("there were unlocked pages in the range");
Josef Bacik294e30f2013-10-09 12:00:56 -0400161 goto out_bits;
162 }
Goldwyn Rodrigues99780592019-06-21 10:02:54 -0500163 unlock_extent(tmp, start, end);
Josef Bacik294e30f2013-10-09 12:00:56 -0400164 /* locked_page was unlocked above */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300165 put_page(locked_page);
Josef Bacik294e30f2013-10-09 12:00:56 -0400166
167 /*
168 * Test this scenario
169 * |--- delalloc ---|
170 * |--- search ---|
171 */
Feifei Xub9ef22d2016-06-01 19:18:25 +0800172 test_start = max_bytes + sectorsize;
Josef Bacik294e30f2013-10-09 12:00:56 -0400173 locked_page = find_lock_page(inode->i_mapping, test_start >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300174 PAGE_SHIFT);
Josef Bacik294e30f2013-10-09 12:00:56 -0400175 if (!locked_page) {
David Sterba3c7251f2018-05-17 00:00:42 +0200176 test_err("couldn't find the locked page");
Josef Bacik294e30f2013-10-09 12:00:56 -0400177 goto out_bits;
178 }
179 start = test_start;
180 end = 0;
Goldwyn Rodrigues99780592019-06-21 10:02:54 -0500181 found = find_lock_delalloc_range(inode, locked_page, &start,
Nikolay Borisov917aace2018-10-26 14:43:20 +0300182 &end);
Josef Bacik294e30f2013-10-09 12:00:56 -0400183 if (found) {
David Sterba3c7251f2018-05-17 00:00:42 +0200184 test_err("found range when we shouldn't have");
Josef Bacik294e30f2013-10-09 12:00:56 -0400185 goto out_bits;
186 }
187 if (end != (u64)-1) {
David Sterba3c7251f2018-05-17 00:00:42 +0200188 test_err("did not return the proper end offset");
Josef Bacik294e30f2013-10-09 12:00:56 -0400189 goto out_bits;
190 }
191
192 /*
193 * Test this scenario
194 * [------- delalloc -------|
195 * [max_bytes]|-- search--|
196 *
197 * We are re-using our test_start from above since it works out well.
198 */
Goldwyn Rodrigues99780592019-06-21 10:02:54 -0500199 set_extent_delalloc(tmp, max_bytes, total_dirty - 1, 0, NULL);
Josef Bacik294e30f2013-10-09 12:00:56 -0400200 start = test_start;
201 end = 0;
Goldwyn Rodrigues99780592019-06-21 10:02:54 -0500202 found = find_lock_delalloc_range(inode, locked_page, &start,
Nikolay Borisov917aace2018-10-26 14:43:20 +0300203 &end);
Josef Bacik294e30f2013-10-09 12:00:56 -0400204 if (!found) {
David Sterba3c7251f2018-05-17 00:00:42 +0200205 test_err("didn't find our range");
Josef Bacik294e30f2013-10-09 12:00:56 -0400206 goto out_bits;
207 }
208 if (start != test_start || end != total_dirty - 1) {
David Sterba3c7251f2018-05-17 00:00:42 +0200209 test_err("expected start %llu end %llu, got start %llu end %llu",
Josef Bacik294e30f2013-10-09 12:00:56 -0400210 test_start, total_dirty - 1, start, end);
211 goto out_bits;
212 }
213 if (process_page_range(inode, start, end,
214 PROCESS_TEST_LOCKED | PROCESS_UNLOCK)) {
David Sterba3c7251f2018-05-17 00:00:42 +0200215 test_err("pages in range were not all locked");
Josef Bacik294e30f2013-10-09 12:00:56 -0400216 goto out_bits;
217 }
Goldwyn Rodrigues99780592019-06-21 10:02:54 -0500218 unlock_extent(tmp, start, end);
Josef Bacik294e30f2013-10-09 12:00:56 -0400219
220 /*
221 * Now to test where we run into a page that is no longer dirty in the
222 * range we want to find.
223 */
Byongho Leeee221842015-12-15 01:42:10 +0900224 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300225 (max_bytes + SZ_1M) >> PAGE_SHIFT);
Josef Bacik294e30f2013-10-09 12:00:56 -0400226 if (!page) {
David Sterba3c7251f2018-05-17 00:00:42 +0200227 test_err("couldn't find our page");
Josef Bacik294e30f2013-10-09 12:00:56 -0400228 goto out_bits;
229 }
230 ClearPageDirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300231 put_page(page);
Josef Bacik294e30f2013-10-09 12:00:56 -0400232
233 /* We unlocked it in the previous test */
234 lock_page(locked_page);
235 start = test_start;
236 end = 0;
237 /*
238 * Currently if we fail to find dirty pages in the delalloc range we
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300239 * will adjust max_bytes down to PAGE_SIZE and then re-search. If
Josef Bacik294e30f2013-10-09 12:00:56 -0400240 * this changes at any point in the future we will need to fix this
241 * tests expected behavior.
242 */
Goldwyn Rodrigues99780592019-06-21 10:02:54 -0500243 found = find_lock_delalloc_range(inode, locked_page, &start,
Nikolay Borisov917aace2018-10-26 14:43:20 +0300244 &end);
Josef Bacik294e30f2013-10-09 12:00:56 -0400245 if (!found) {
David Sterba3c7251f2018-05-17 00:00:42 +0200246 test_err("didn't find our range");
Josef Bacik294e30f2013-10-09 12:00:56 -0400247 goto out_bits;
248 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300249 if (start != test_start && end != test_start + PAGE_SIZE - 1) {
David Sterba3c7251f2018-05-17 00:00:42 +0200250 test_err("expected start %llu end %llu, got start %llu end %llu",
251 test_start, test_start + PAGE_SIZE - 1, start, end);
Josef Bacik294e30f2013-10-09 12:00:56 -0400252 goto out_bits;
253 }
254 if (process_page_range(inode, start, end, PROCESS_TEST_LOCKED |
255 PROCESS_UNLOCK)) {
David Sterba3c7251f2018-05-17 00:00:42 +0200256 test_err("pages in range were not all locked");
Josef Bacik294e30f2013-10-09 12:00:56 -0400257 goto out_bits;
258 }
259 ret = 0;
260out_bits:
Goldwyn Rodrigues99780592019-06-21 10:02:54 -0500261 clear_extent_bits(tmp, 0, total_dirty - 1, (unsigned)-1);
Josef Bacik294e30f2013-10-09 12:00:56 -0400262out:
263 if (locked_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300264 put_page(locked_page);
Josef Bacik294e30f2013-10-09 12:00:56 -0400265 process_page_range(inode, 0, total_dirty - 1,
266 PROCESS_UNLOCK | PROCESS_RELEASE);
267 iput(inode);
268 return ret;
269}
270
Omar Sandoval9426ce72016-09-22 17:24:23 -0700271static int check_eb_bitmap(unsigned long *bitmap, struct extent_buffer *eb,
272 unsigned long len)
Feifei Xu34b3e6c2016-06-01 19:18:30 +0800273{
Omar Sandoval9426ce72016-09-22 17:24:23 -0700274 unsigned long i;
275
276 for (i = 0; i < len * BITS_PER_BYTE; i++) {
277 int bit, bit1;
278
279 bit = !!test_bit(i, bitmap);
280 bit1 = !!extent_buffer_test_bit(eb, 0, i);
281 if (bit1 != bit) {
David Sterba3c7251f2018-05-17 00:00:42 +0200282 test_err("bits do not match");
Omar Sandoval9426ce72016-09-22 17:24:23 -0700283 return -EINVAL;
284 }
285
286 bit1 = !!extent_buffer_test_bit(eb, i / BITS_PER_BYTE,
287 i % BITS_PER_BYTE);
288 if (bit1 != bit) {
David Sterba3c7251f2018-05-17 00:00:42 +0200289 test_err("offset bits do not match");
Omar Sandoval9426ce72016-09-22 17:24:23 -0700290 return -EINVAL;
291 }
292 }
293 return 0;
Feifei Xu34b3e6c2016-06-01 19:18:30 +0800294}
295
Omar Sandoval0f331222015-09-29 20:50:31 -0700296static int __test_eb_bitmaps(unsigned long *bitmap, struct extent_buffer *eb,
297 unsigned long len)
298{
Omar Sandoval9426ce72016-09-22 17:24:23 -0700299 unsigned long i, j;
300 u32 x;
301 int ret;
Omar Sandoval0f331222015-09-29 20:50:31 -0700302
303 memset(bitmap, 0, len);
David Sterbab159fa22016-11-08 18:09:03 +0100304 memzero_extent_buffer(eb, 0, len);
Omar Sandoval0f331222015-09-29 20:50:31 -0700305 if (memcmp_extent_buffer(eb, bitmap, 0, len) != 0) {
David Sterba3c7251f2018-05-17 00:00:42 +0200306 test_err("bitmap was not zeroed");
Omar Sandoval0f331222015-09-29 20:50:31 -0700307 return -EINVAL;
308 }
309
310 bitmap_set(bitmap, 0, len * BITS_PER_BYTE);
311 extent_buffer_bitmap_set(eb, 0, 0, len * BITS_PER_BYTE);
Omar Sandoval9426ce72016-09-22 17:24:23 -0700312 ret = check_eb_bitmap(bitmap, eb, len);
313 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +0200314 test_err("setting all bits failed");
Omar Sandoval9426ce72016-09-22 17:24:23 -0700315 return ret;
Omar Sandoval0f331222015-09-29 20:50:31 -0700316 }
317
318 bitmap_clear(bitmap, 0, len * BITS_PER_BYTE);
319 extent_buffer_bitmap_clear(eb, 0, 0, len * BITS_PER_BYTE);
Omar Sandoval9426ce72016-09-22 17:24:23 -0700320 ret = check_eb_bitmap(bitmap, eb, len);
321 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +0200322 test_err("clearing all bits failed");
Omar Sandoval9426ce72016-09-22 17:24:23 -0700323 return ret;
Omar Sandoval0f331222015-09-29 20:50:31 -0700324 }
325
Feifei Xued9e4af2016-06-01 19:18:26 +0800326 /* Straddling pages test */
327 if (len > PAGE_SIZE) {
328 bitmap_set(bitmap,
329 (PAGE_SIZE - sizeof(long) / 2) * BITS_PER_BYTE,
330 sizeof(long) * BITS_PER_BYTE);
331 extent_buffer_bitmap_set(eb, PAGE_SIZE - sizeof(long) / 2, 0,
332 sizeof(long) * BITS_PER_BYTE);
Omar Sandoval9426ce72016-09-22 17:24:23 -0700333 ret = check_eb_bitmap(bitmap, eb, len);
334 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +0200335 test_err("setting straddling pages failed");
Omar Sandoval9426ce72016-09-22 17:24:23 -0700336 return ret;
Feifei Xued9e4af2016-06-01 19:18:26 +0800337 }
Omar Sandoval0f331222015-09-29 20:50:31 -0700338
Feifei Xued9e4af2016-06-01 19:18:26 +0800339 bitmap_set(bitmap, 0, len * BITS_PER_BYTE);
340 bitmap_clear(bitmap,
341 (PAGE_SIZE - sizeof(long) / 2) * BITS_PER_BYTE,
342 sizeof(long) * BITS_PER_BYTE);
343 extent_buffer_bitmap_set(eb, 0, 0, len * BITS_PER_BYTE);
344 extent_buffer_bitmap_clear(eb, PAGE_SIZE - sizeof(long) / 2, 0,
345 sizeof(long) * BITS_PER_BYTE);
Omar Sandoval9426ce72016-09-22 17:24:23 -0700346 ret = check_eb_bitmap(bitmap, eb, len);
347 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +0200348 test_err("clearing straddling pages failed");
Omar Sandoval9426ce72016-09-22 17:24:23 -0700349 return ret;
Feifei Xued9e4af2016-06-01 19:18:26 +0800350 }
Omar Sandoval0f331222015-09-29 20:50:31 -0700351 }
352
353 /*
354 * Generate a wonky pseudo-random bit pattern for the sake of not using
355 * something repetitive that could miss some hypothetical off-by-n bug.
356 */
357 x = 0;
Omar Sandoval9426ce72016-09-22 17:24:23 -0700358 bitmap_clear(bitmap, 0, len * BITS_PER_BYTE);
359 extent_buffer_bitmap_clear(eb, 0, 0, len * BITS_PER_BYTE);
360 for (i = 0; i < len * BITS_PER_BYTE / 32; i++) {
361 x = (0x19660dULL * (u64)x + 0x3c6ef35fULL) & 0xffffffffU;
362 for (j = 0; j < 32; j++) {
363 if (x & (1U << j)) {
364 bitmap_set(bitmap, i * 32 + j, 1);
365 extent_buffer_bitmap_set(eb, 0, i * 32 + j, 1);
366 }
367 }
Omar Sandoval0f331222015-09-29 20:50:31 -0700368 }
Omar Sandoval0f331222015-09-29 20:50:31 -0700369
Omar Sandoval9426ce72016-09-22 17:24:23 -0700370 ret = check_eb_bitmap(bitmap, eb, len);
371 if (ret) {
David Sterba3c7251f2018-05-17 00:00:42 +0200372 test_err("random bit pattern failed");
Omar Sandoval9426ce72016-09-22 17:24:23 -0700373 return ret;
Omar Sandoval0f331222015-09-29 20:50:31 -0700374 }
375
376 return 0;
377}
378
Feifei Xub9ef22d2016-06-01 19:18:25 +0800379static int test_eb_bitmaps(u32 sectorsize, u32 nodesize)
Omar Sandoval0f331222015-09-29 20:50:31 -0700380{
Jeff Mahoneyda170662016-06-15 09:22:56 -0400381 struct btrfs_fs_info *fs_info;
David Sterbad33d1052019-03-15 16:46:55 +0100382 unsigned long *bitmap = NULL;
383 struct extent_buffer *eb = NULL;
Omar Sandoval0f331222015-09-29 20:50:31 -0700384 int ret;
385
David Sterba315b76b2018-05-17 00:00:44 +0200386 test_msg("running extent buffer bitmap tests");
Feifei Xued9e4af2016-06-01 19:18:26 +0800387
Qu Wenruob1d51f62020-11-13 20:51:27 +0800388 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
David Sterbad46a05e2019-03-15 16:43:11 +0100389 if (!fs_info) {
David Sterba37b2a7b2019-03-15 17:28:46 +0100390 test_std_err(TEST_ALLOC_FS_INFO);
David Sterbad46a05e2019-03-15 16:43:11 +0100391 return -ENOMEM;
392 }
Jeff Mahoneyda170662016-06-15 09:22:56 -0400393
Qu Wenruob1d51f62020-11-13 20:51:27 +0800394 bitmap = kmalloc(nodesize, GFP_KERNEL);
Omar Sandoval0f331222015-09-29 20:50:31 -0700395 if (!bitmap) {
David Sterba3c7251f2018-05-17 00:00:42 +0200396 test_err("couldn't allocate test bitmap");
David Sterbad33d1052019-03-15 16:46:55 +0100397 ret = -ENOMEM;
398 goto out;
Omar Sandoval0f331222015-09-29 20:50:31 -0700399 }
400
Qu Wenruob1d51f62020-11-13 20:51:27 +0800401 eb = __alloc_dummy_extent_buffer(fs_info, 0, nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -0700402 if (!eb) {
David Sterba9e3d9f82019-03-15 17:28:46 +0100403 test_std_err(TEST_ALLOC_ROOT);
David Sterbad33d1052019-03-15 16:46:55 +0100404 ret = -ENOMEM;
405 goto out;
Omar Sandoval0f331222015-09-29 20:50:31 -0700406 }
407
Qu Wenruob1d51f62020-11-13 20:51:27 +0800408 ret = __test_eb_bitmaps(bitmap, eb, nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -0700409 if (ret)
410 goto out;
411
Omar Sandoval0f331222015-09-29 20:50:31 -0700412 free_extent_buffer(eb);
Qu Wenruob1d51f62020-11-13 20:51:27 +0800413
414 /*
415 * Test again for case where the tree block is sectorsize aligned but
416 * not nodesize aligned.
417 */
418 eb = __alloc_dummy_extent_buffer(fs_info, sectorsize, nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -0700419 if (!eb) {
David Sterba9e3d9f82019-03-15 17:28:46 +0100420 test_std_err(TEST_ALLOC_ROOT);
David Sterbad33d1052019-03-15 16:46:55 +0100421 ret = -ENOMEM;
422 goto out;
Omar Sandoval0f331222015-09-29 20:50:31 -0700423 }
424
Qu Wenruob1d51f62020-11-13 20:51:27 +0800425 ret = __test_eb_bitmaps(bitmap, eb, nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -0700426out:
427 free_extent_buffer(eb);
428 kfree(bitmap);
David Sterbad33d1052019-03-15 16:46:55 +0100429 btrfs_free_dummy_fs_info(fs_info);
Omar Sandoval0f331222015-09-29 20:50:31 -0700430 return ret;
431}
432
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300433static int test_find_first_clear_extent_bit(void)
434{
435 struct extent_io_tree tree;
436 u64 start, end;
Filipe Manana202f64e2019-08-05 10:57:41 +0100437 int ret = -EINVAL;
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300438
439 test_msg("running find_first_clear_extent_bit test");
Nikolay Borisov5750c372020-01-27 11:59:26 +0200440
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300441 extent_io_tree_init(NULL, &tree, IO_TREE_SELFTEST, NULL);
442
Nikolay Borisov5750c372020-01-27 11:59:26 +0200443 /* Test correct handling of empty tree */
444 find_first_clear_extent_bit(&tree, 0, &start, &end, CHUNK_TRIMMED);
445 if (start != 0 || end != -1) {
446 test_err(
447 "error getting a range from completely empty tree: start %llu end %llu",
448 start, end);
449 goto out;
450 }
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300451 /*
452 * Set 1M-4M alloc/discard and 32M-64M thus leaving a hole between
453 * 4M-32M
454 */
455 set_extent_bits(&tree, SZ_1M, SZ_4M - 1,
456 CHUNK_TRIMMED | CHUNK_ALLOCATED);
457
458 find_first_clear_extent_bit(&tree, SZ_512K, &start, &end,
459 CHUNK_TRIMMED | CHUNK_ALLOCATED);
460
Filipe Manana202f64e2019-08-05 10:57:41 +0100461 if (start != 0 || end != SZ_1M - 1) {
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300462 test_err("error finding beginning range: start %llu end %llu",
463 start, end);
Filipe Manana202f64e2019-08-05 10:57:41 +0100464 goto out;
465 }
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300466
467 /* Now add 32M-64M so that we have a hole between 4M-32M */
468 set_extent_bits(&tree, SZ_32M, SZ_64M - 1,
469 CHUNK_TRIMMED | CHUNK_ALLOCATED);
470
471 /*
472 * Request first hole starting at 12M, we should get 4M-32M
473 */
474 find_first_clear_extent_bit(&tree, 12 * SZ_1M, &start, &end,
475 CHUNK_TRIMMED | CHUNK_ALLOCATED);
476
Filipe Manana202f64e2019-08-05 10:57:41 +0100477 if (start != SZ_4M || end != SZ_32M - 1) {
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300478 test_err("error finding trimmed range: start %llu end %llu",
479 start, end);
Filipe Manana202f64e2019-08-05 10:57:41 +0100480 goto out;
481 }
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300482
483 /*
484 * Search in the middle of allocated range, should get the next one
485 * available, which happens to be unallocated -> 4M-32M
486 */
487 find_first_clear_extent_bit(&tree, SZ_2M, &start, &end,
488 CHUNK_TRIMMED | CHUNK_ALLOCATED);
489
Filipe Manana202f64e2019-08-05 10:57:41 +0100490 if (start != SZ_4M || end != SZ_32M - 1) {
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300491 test_err("error finding next unalloc range: start %llu end %llu",
492 start, end);
Filipe Manana202f64e2019-08-05 10:57:41 +0100493 goto out;
494 }
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300495
496 /*
497 * Set 64M-72M with CHUNK_ALLOC flag, then search for CHUNK_TRIMMED flag
498 * being unset in this range, we should get the entry in range 64M-72M
499 */
500 set_extent_bits(&tree, SZ_64M, SZ_64M + SZ_8M - 1, CHUNK_ALLOCATED);
501 find_first_clear_extent_bit(&tree, SZ_64M + SZ_1M, &start, &end,
502 CHUNK_TRIMMED);
503
Filipe Manana202f64e2019-08-05 10:57:41 +0100504 if (start != SZ_64M || end != SZ_64M + SZ_8M - 1) {
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300505 test_err("error finding exact range: start %llu end %llu",
506 start, end);
Filipe Manana202f64e2019-08-05 10:57:41 +0100507 goto out;
508 }
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300509
510 find_first_clear_extent_bit(&tree, SZ_64M - SZ_8M, &start, &end,
511 CHUNK_TRIMMED);
512
513 /*
514 * Search in the middle of set range whose immediate neighbour doesn't
515 * have the bits set so it must be returned
516 */
Filipe Manana202f64e2019-08-05 10:57:41 +0100517 if (start != SZ_64M || end != SZ_64M + SZ_8M - 1) {
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300518 test_err("error finding next alloc range: start %llu end %llu",
519 start, end);
Filipe Manana202f64e2019-08-05 10:57:41 +0100520 goto out;
521 }
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300522
523 /*
524 * Search beyond any known range, shall return after last known range
525 * and end should be -1
526 */
527 find_first_clear_extent_bit(&tree, -1, &start, &end, CHUNK_TRIMMED);
Filipe Manana202f64e2019-08-05 10:57:41 +0100528 if (start != SZ_64M + SZ_8M || end != -1) {
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300529 test_err(
530 "error handling beyond end of range search: start %llu end %llu",
531 start, end);
Filipe Manana202f64e2019-08-05 10:57:41 +0100532 goto out;
533 }
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300534
Filipe Manana202f64e2019-08-05 10:57:41 +0100535 ret = 0;
536out:
Filipe Mananacdf52bd2019-08-03 09:53:16 +0100537 clear_extent_bits(&tree, 0, (u64)-1, CHUNK_TRIMMED | CHUNK_ALLOCATED);
538
Filipe Manana202f64e2019-08-05 10:57:41 +0100539 return ret;
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300540}
541
Feifei Xub9ef22d2016-06-01 19:18:25 +0800542int btrfs_test_extent_io(u32 sectorsize, u32 nodesize)
Josef Bacik294e30f2013-10-09 12:00:56 -0400543{
Omar Sandoval0f331222015-09-29 20:50:31 -0700544 int ret;
545
David Sterba315b76b2018-05-17 00:00:44 +0200546 test_msg("running extent I/O tests");
Omar Sandoval0f331222015-09-29 20:50:31 -0700547
Feifei Xub9ef22d2016-06-01 19:18:25 +0800548 ret = test_find_delalloc(sectorsize);
Omar Sandoval0f331222015-09-29 20:50:31 -0700549 if (ret)
550 goto out;
551
Nikolay Borisov1eaebb32019-06-03 13:06:02 +0300552 ret = test_find_first_clear_extent_bit();
553 if (ret)
554 goto out;
555
Feifei Xub9ef22d2016-06-01 19:18:25 +0800556 ret = test_eb_bitmaps(sectorsize, nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -0700557out:
Omar Sandoval0f331222015-09-29 20:50:31 -0700558 return ret;
Josef Bacik294e30f2013-10-09 12:00:56 -0400559}