Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2013 Fusion IO. All rights reserved.
4 */
5
6#include <linux/pagemap.h>
7#include <linux/sched.h>
8#include <linux/slab.h>
9#include <linux/sizes.h>
10#include "btrfs-tests.h"
11#include "../ctree.h"
12#include "../extent_io.h"
13#include "../btrfs_inode.h"
14
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];
24 unsigned long index = start >> PAGE_SHIFT;
25 unsigned long end_index = end >> PAGE_SHIFT;
26 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]);
41 put_page(pages[i]);
42 if (flags & PROCESS_RELEASE)
43 put_page(pages[i]);
44 }
45 nr_pages -= ret;
46 index += ret;
47 cond_resched();
48 loops++;
49 if (loops > 100000) {
50 printk(KERN_ERR
51 "stuck in a loop, start %llu, end %llu, nr_pages %lu, ret %d\n",
52 start, end, nr_pages, ret);
53 break;
54 }
55 }
56 return count;
57}
58
59static int test_find_delalloc(u32 sectorsize)
60{
61 struct inode *inode;
62 struct extent_io_tree *tmp;
63 struct page *page;
64 struct page *locked_page = NULL;
65 unsigned long index = 0;
66 /* 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;
69 u64 start, end, test_start;
70 bool found;
71 int ret = -EINVAL;
72
73 test_msg("running find delalloc tests");
74
75 inode = btrfs_new_test_inode();
76 if (!inode) {
77 test_std_err(TEST_ALLOC_INODE);
78 return -ENOMEM;
79 }
80 tmp = &BTRFS_I(inode)->io_tree;
81
82 /*
83 * Passing NULL as we don't have fs_info but tracepoints are not used
84 * at this point
85 */
86 extent_io_tree_init(NULL, tmp, IO_TREE_SELFTEST, NULL);
87
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 */
93 for (index = 0; index < (total_dirty >> PAGE_SHIFT); index++) {
94 page = find_or_create_page(inode->i_mapping, index, GFP_KERNEL);
95 if (!page) {
96 test_err("failed to allocate test page");
97 ret = -ENOMEM;
98 goto out;
99 }
100 SetPageDirty(page);
101 if (index) {
102 unlock_page(page);
103 } else {
104 get_page(page);
105 locked_page = page;
106 }
107 }
108
109 /* Test this scenario
110 * |--- delalloc ---|
111 * |--- search ---|
112 */
113 set_extent_delalloc(tmp, 0, sectorsize - 1, 0, NULL);
114 start = 0;
115 end = 0;
116 found = find_lock_delalloc_range(inode, locked_page, &start,
117 &end);
118 if (!found) {
119 test_err("should have found at least one delalloc");
120 goto out_bits;
121 }
122 if (start != 0 || end != (sectorsize - 1)) {
123 test_err("expected start 0 end %u, got start %llu end %llu",
124 sectorsize - 1, start, end);
125 goto out_bits;
126 }
127 unlock_extent(tmp, start, end);
128 unlock_page(locked_page);
129 put_page(locked_page);
130
131 /*
132 * Test this scenario
133 *
134 * |--- delalloc ---|
135 * |--- search ---|
136 */
137 test_start = SZ_64M;
138 locked_page = find_lock_page(inode->i_mapping,
139 test_start >> PAGE_SHIFT);
140 if (!locked_page) {
141 test_err("couldn't find the locked page");
142 goto out_bits;
143 }
144 set_extent_delalloc(tmp, sectorsize, max_bytes - 1, 0, NULL);
145 start = test_start;
146 end = 0;
147 found = find_lock_delalloc_range(inode, locked_page, &start,
148 &end);
149 if (!found) {
150 test_err("couldn't find delalloc in our range");
151 goto out_bits;
152 }
153 if (start != test_start || end != max_bytes - 1) {
154 test_err("expected start %llu end %llu, got start %llu, end %llu",
155 test_start, max_bytes - 1, start, end);
156 goto out_bits;
157 }
158 if (process_page_range(inode, start, end,
159 PROCESS_TEST_LOCKED | PROCESS_UNLOCK)) {
160 test_err("there were unlocked pages in the range");
161 goto out_bits;
162 }
163 unlock_extent(tmp, start, end);
164 /* locked_page was unlocked above */
165 put_page(locked_page);
166
167 /*
168 * Test this scenario
169 * |--- delalloc ---|
170 * |--- search ---|
171 */
172 test_start = max_bytes + sectorsize;
173 locked_page = find_lock_page(inode->i_mapping, test_start >>
174 PAGE_SHIFT);
175 if (!locked_page) {
176 test_err("couldn't find the locked page");
177 goto out_bits;
178 }
179 start = test_start;
180 end = 0;
181 found = find_lock_delalloc_range(inode, locked_page, &start,
182 &end);
183 if (found) {
184 test_err("found range when we shouldn't have");
185 goto out_bits;
186 }
187 if (end != (u64)-1) {
188 test_err("did not return the proper end offset");
189 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 */
199 set_extent_delalloc(tmp, max_bytes, total_dirty - 1, 0, NULL);
200 start = test_start;
201 end = 0;
202 found = find_lock_delalloc_range(inode, locked_page, &start,
203 &end);
204 if (!found) {
205 test_err("didn't find our range");
206 goto out_bits;
207 }
208 if (start != test_start || end != total_dirty - 1) {
209 test_err("expected start %llu end %llu, got start %llu end %llu",
210 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)) {
215 test_err("pages in range were not all locked");
216 goto out_bits;
217 }
218 unlock_extent(tmp, start, end);
219
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 */
224 page = find_get_page(inode->i_mapping,
225 (max_bytes + SZ_1M) >> PAGE_SHIFT);
226 if (!page) {
227 test_err("couldn't find our page");
228 goto out_bits;
229 }
230 ClearPageDirty(page);
231 put_page(page);
232
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
239 * will adjust max_bytes down to PAGE_SIZE and then re-search. If
240 * this changes at any point in the future we will need to fix this
241 * tests expected behavior.
242 */
243 found = find_lock_delalloc_range(inode, locked_page, &start,
244 &end);
245 if (!found) {
246 test_err("didn't find our range");
247 goto out_bits;
248 }
249 if (start != test_start && end != test_start + PAGE_SIZE - 1) {
250 test_err("expected start %llu end %llu, got start %llu end %llu",
251 test_start, test_start + PAGE_SIZE - 1, start, end);
252 goto out_bits;
253 }
254 if (process_page_range(inode, start, end, PROCESS_TEST_LOCKED |
255 PROCESS_UNLOCK)) {
256 test_err("pages in range were not all locked");
257 goto out_bits;
258 }
259 ret = 0;
260out_bits:
261 clear_extent_bits(tmp, 0, total_dirty - 1, (unsigned)-1);
262out:
263 if (locked_page)
264 put_page(locked_page);
265 process_page_range(inode, 0, total_dirty - 1,
266 PROCESS_UNLOCK | PROCESS_RELEASE);
267 iput(inode);
268 return ret;
269}
270
271static int check_eb_bitmap(unsigned long *bitmap, struct extent_buffer *eb,
272 unsigned long len)
273{
274 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) {
282 test_err("bits do not match");
283 return -EINVAL;
284 }
285
286 bit1 = !!extent_buffer_test_bit(eb, i / BITS_PER_BYTE,
287 i % BITS_PER_BYTE);
288 if (bit1 != bit) {
289 test_err("offset bits do not match");
290 return -EINVAL;
291 }
292 }
293 return 0;
294}
295
296static int __test_eb_bitmaps(unsigned long *bitmap, struct extent_buffer *eb,
297 unsigned long len)
298{
299 unsigned long i, j;
300 u32 x;
301 int ret;
302
303 memset(bitmap, 0, len);
304 memzero_extent_buffer(eb, 0, len);
305 if (memcmp_extent_buffer(eb, bitmap, 0, len) != 0) {
306 test_err("bitmap was not zeroed");
307 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);
312 ret = check_eb_bitmap(bitmap, eb, len);
313 if (ret) {
314 test_err("setting all bits failed");
315 return ret;
316 }
317
318 bitmap_clear(bitmap, 0, len * BITS_PER_BYTE);
319 extent_buffer_bitmap_clear(eb, 0, 0, len * BITS_PER_BYTE);
320 ret = check_eb_bitmap(bitmap, eb, len);
321 if (ret) {
322 test_err("clearing all bits failed");
323 return ret;
324 }
325
326 /* 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);
333 ret = check_eb_bitmap(bitmap, eb, len);
334 if (ret) {
335 test_err("setting straddling pages failed");
336 return ret;
337 }
338
339 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);
346 ret = check_eb_bitmap(bitmap, eb, len);
347 if (ret) {
348 test_err("clearing straddling pages failed");
349 return ret;
350 }
351 }
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;
358 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 }
368 }
369
370 ret = check_eb_bitmap(bitmap, eb, len);
371 if (ret) {
372 test_err("random bit pattern failed");
373 return ret;
374 }
375
376 return 0;
377}
378
379static int test_eb_bitmaps(u32 sectorsize, u32 nodesize)
380{
381 struct btrfs_fs_info *fs_info;
382 unsigned long len;
383 unsigned long *bitmap = NULL;
384 struct extent_buffer *eb = NULL;
385 int ret;
386
387 test_msg("running extent buffer bitmap tests");
388
389 /*
390 * In ppc64, sectorsize can be 64K, thus 4 * 64K will be larger than
391 * BTRFS_MAX_METADATA_BLOCKSIZE.
392 */
393 len = (sectorsize < BTRFS_MAX_METADATA_BLOCKSIZE)
394 ? sectorsize * 4 : sectorsize;
395
396 fs_info = btrfs_alloc_dummy_fs_info(len, len);
397 if (!fs_info) {
398 test_std_err(TEST_ALLOC_FS_INFO);
399 return -ENOMEM;
400 }
401
402 bitmap = kmalloc(len, GFP_KERNEL);
403 if (!bitmap) {
404 test_err("couldn't allocate test bitmap");
405 ret = -ENOMEM;
406 goto out;
407 }
408
409 eb = __alloc_dummy_extent_buffer(fs_info, 0, len);
410 if (!eb) {
411 test_std_err(TEST_ALLOC_ROOT);
412 ret = -ENOMEM;
413 goto out;
414 }
415
416 ret = __test_eb_bitmaps(bitmap, eb, len);
417 if (ret)
418 goto out;
419
420 /* Do it over again with an extent buffer which isn't page-aligned. */
421 free_extent_buffer(eb);
422 eb = __alloc_dummy_extent_buffer(fs_info, nodesize / 2, len);
423 if (!eb) {
424 test_std_err(TEST_ALLOC_ROOT);
425 ret = -ENOMEM;
426 goto out;
427 }
428
429 ret = __test_eb_bitmaps(bitmap, eb, len);
430out:
431 free_extent_buffer(eb);
432 kfree(bitmap);
433 btrfs_free_dummy_fs_info(fs_info);
434 return ret;
435}
436
437static int test_find_first_clear_extent_bit(void)
438{
439 struct extent_io_tree tree;
440 u64 start, end;
441 int ret = -EINVAL;
442
443 test_msg("running find_first_clear_extent_bit test");
444 extent_io_tree_init(NULL, &tree, IO_TREE_SELFTEST, NULL);
445
446 /*
447 * Set 1M-4M alloc/discard and 32M-64M thus leaving a hole between
448 * 4M-32M
449 */
450 set_extent_bits(&tree, SZ_1M, SZ_4M - 1,
451 CHUNK_TRIMMED | CHUNK_ALLOCATED);
452
453 find_first_clear_extent_bit(&tree, SZ_512K, &start, &end,
454 CHUNK_TRIMMED | CHUNK_ALLOCATED);
455
456 if (start != 0 || end != SZ_1M - 1) {
457 test_err("error finding beginning range: start %llu end %llu",
458 start, end);
459 goto out;
460 }
461
462 /* Now add 32M-64M so that we have a hole between 4M-32M */
463 set_extent_bits(&tree, SZ_32M, SZ_64M - 1,
464 CHUNK_TRIMMED | CHUNK_ALLOCATED);
465
466 /*
467 * Request first hole starting at 12M, we should get 4M-32M
468 */
469 find_first_clear_extent_bit(&tree, 12 * SZ_1M, &start, &end,
470 CHUNK_TRIMMED | CHUNK_ALLOCATED);
471
472 if (start != SZ_4M || end != SZ_32M - 1) {
473 test_err("error finding trimmed range: start %llu end %llu",
474 start, end);
475 goto out;
476 }
477
478 /*
479 * Search in the middle of allocated range, should get the next one
480 * available, which happens to be unallocated -> 4M-32M
481 */
482 find_first_clear_extent_bit(&tree, SZ_2M, &start, &end,
483 CHUNK_TRIMMED | CHUNK_ALLOCATED);
484
485 if (start != SZ_4M || end != SZ_32M - 1) {
486 test_err("error finding next unalloc range: start %llu end %llu",
487 start, end);
488 goto out;
489 }
490
491 /*
492 * Set 64M-72M with CHUNK_ALLOC flag, then search for CHUNK_TRIMMED flag
493 * being unset in this range, we should get the entry in range 64M-72M
494 */
495 set_extent_bits(&tree, SZ_64M, SZ_64M + SZ_8M - 1, CHUNK_ALLOCATED);
496 find_first_clear_extent_bit(&tree, SZ_64M + SZ_1M, &start, &end,
497 CHUNK_TRIMMED);
498
499 if (start != SZ_64M || end != SZ_64M + SZ_8M - 1) {
500 test_err("error finding exact range: start %llu end %llu",
501 start, end);
502 goto out;
503 }
504
505 find_first_clear_extent_bit(&tree, SZ_64M - SZ_8M, &start, &end,
506 CHUNK_TRIMMED);
507
508 /*
509 * Search in the middle of set range whose immediate neighbour doesn't
510 * have the bits set so it must be returned
511 */
512 if (start != SZ_64M || end != SZ_64M + SZ_8M - 1) {
513 test_err("error finding next alloc range: start %llu end %llu",
514 start, end);
515 goto out;
516 }
517
518 /*
519 * Search beyond any known range, shall return after last known range
520 * and end should be -1
521 */
522 find_first_clear_extent_bit(&tree, -1, &start, &end, CHUNK_TRIMMED);
523 if (start != SZ_64M + SZ_8M || end != -1) {
524 test_err(
525 "error handling beyond end of range search: start %llu end %llu",
526 start, end);
527 goto out;
528 }
529
530 ret = 0;
531out:
532 clear_extent_bits(&tree, 0, (u64)-1, CHUNK_TRIMMED | CHUNK_ALLOCATED);
533
534 return ret;
535}
536
537int btrfs_test_extent_io(u32 sectorsize, u32 nodesize)
538{
539 int ret;
540
541 test_msg("running extent I/O tests");
542
543 ret = test_find_delalloc(sectorsize);
544 if (ret)
545 goto out;
546
547 ret = test_find_first_clear_extent_bit();
548 if (ret)
549 goto out;
550
551 ret = test_eb_bitmaps(sectorsize, nodesize);
552out:
553 return ret;
554}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2013 Fusion IO. All rights reserved.
4 */
5
6#include <linux/pagemap.h>
7#include <linux/sched.h>
8#include <linux/slab.h>
9#include <linux/sizes.h>
10#include "btrfs-tests.h"
11#include "../ctree.h"
12#include "../extent_io.h"
13
14#define PROCESS_UNLOCK (1 << 0)
15#define PROCESS_RELEASE (1 << 1)
16#define PROCESS_TEST_LOCKED (1 << 2)
17
18static noinline int process_page_range(struct inode *inode, u64 start, u64 end,
19 unsigned long flags)
20{
21 int ret;
22 struct page *pages[16];
23 unsigned long index = start >> PAGE_SHIFT;
24 unsigned long end_index = end >> PAGE_SHIFT;
25 unsigned long nr_pages = end_index - index + 1;
26 int i;
27 int count = 0;
28 int loops = 0;
29
30 while (nr_pages > 0) {
31 ret = find_get_pages_contig(inode->i_mapping, index,
32 min_t(unsigned long, nr_pages,
33 ARRAY_SIZE(pages)), pages);
34 for (i = 0; i < ret; i++) {
35 if (flags & PROCESS_TEST_LOCKED &&
36 !PageLocked(pages[i]))
37 count++;
38 if (flags & PROCESS_UNLOCK && PageLocked(pages[i]))
39 unlock_page(pages[i]);
40 put_page(pages[i]);
41 if (flags & PROCESS_RELEASE)
42 put_page(pages[i]);
43 }
44 nr_pages -= ret;
45 index += ret;
46 cond_resched();
47 loops++;
48 if (loops > 100000) {
49 printk(KERN_ERR "stuck in a loop, start %Lu, end %Lu, nr_pages %lu, ret %d\n", start, end, nr_pages, ret);
50 break;
51 }
52 }
53 return count;
54}
55
56static int test_find_delalloc(u32 sectorsize)
57{
58 struct inode *inode;
59 struct extent_io_tree tmp;
60 struct page *page;
61 struct page *locked_page = NULL;
62 unsigned long index = 0;
63 u64 total_dirty = SZ_256M;
64 u64 max_bytes = SZ_128M;
65 u64 start, end, test_start;
66 u64 found;
67 int ret = -EINVAL;
68
69 test_msg("Running find delalloc tests\n");
70
71 inode = btrfs_new_test_inode();
72 if (!inode) {
73 test_msg("Failed to allocate test inode\n");
74 return -ENOMEM;
75 }
76
77 extent_io_tree_init(&tmp, inode);
78
79 /*
80 * First go through and create and mark all of our pages dirty, we pin
81 * everything to make sure our pages don't get evicted and screw up our
82 * test.
83 */
84 for (index = 0; index < (total_dirty >> PAGE_SHIFT); index++) {
85 page = find_or_create_page(inode->i_mapping, index, GFP_KERNEL);
86 if (!page) {
87 test_msg("Failed to allocate test page\n");
88 ret = -ENOMEM;
89 goto out;
90 }
91 SetPageDirty(page);
92 if (index) {
93 unlock_page(page);
94 } else {
95 get_page(page);
96 locked_page = page;
97 }
98 }
99
100 /* Test this scenario
101 * |--- delalloc ---|
102 * |--- search ---|
103 */
104 set_extent_delalloc(&tmp, 0, sectorsize - 1, 0, NULL);
105 start = 0;
106 end = 0;
107 found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
108 &end, max_bytes);
109 if (!found) {
110 test_msg("Should have found at least one delalloc\n");
111 goto out_bits;
112 }
113 if (start != 0 || end != (sectorsize - 1)) {
114 test_msg("Expected start 0 end %u, got start %llu end %llu\n",
115 sectorsize - 1, start, end);
116 goto out_bits;
117 }
118 unlock_extent(&tmp, start, end);
119 unlock_page(locked_page);
120 put_page(locked_page);
121
122 /*
123 * Test this scenario
124 *
125 * |--- delalloc ---|
126 * |--- search ---|
127 */
128 test_start = SZ_64M;
129 locked_page = find_lock_page(inode->i_mapping,
130 test_start >> PAGE_SHIFT);
131 if (!locked_page) {
132 test_msg("Couldn't find the locked page\n");
133 goto out_bits;
134 }
135 set_extent_delalloc(&tmp, sectorsize, max_bytes - 1, 0, NULL);
136 start = test_start;
137 end = 0;
138 found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
139 &end, max_bytes);
140 if (!found) {
141 test_msg("Couldn't find delalloc in our range\n");
142 goto out_bits;
143 }
144 if (start != test_start || end != max_bytes - 1) {
145 test_msg("Expected start %Lu end %Lu, got start %Lu, end "
146 "%Lu\n", test_start, max_bytes - 1, start, end);
147 goto out_bits;
148 }
149 if (process_page_range(inode, start, end,
150 PROCESS_TEST_LOCKED | PROCESS_UNLOCK)) {
151 test_msg("There were unlocked pages in the range\n");
152 goto out_bits;
153 }
154 unlock_extent(&tmp, start, end);
155 /* locked_page was unlocked above */
156 put_page(locked_page);
157
158 /*
159 * Test this scenario
160 * |--- delalloc ---|
161 * |--- search ---|
162 */
163 test_start = max_bytes + sectorsize;
164 locked_page = find_lock_page(inode->i_mapping, test_start >>
165 PAGE_SHIFT);
166 if (!locked_page) {
167 test_msg("Couldn't find the locked page\n");
168 goto out_bits;
169 }
170 start = test_start;
171 end = 0;
172 found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
173 &end, max_bytes);
174 if (found) {
175 test_msg("Found range when we shouldn't have\n");
176 goto out_bits;
177 }
178 if (end != (u64)-1) {
179 test_msg("Did not return the proper end offset\n");
180 goto out_bits;
181 }
182
183 /*
184 * Test this scenario
185 * [------- delalloc -------|
186 * [max_bytes]|-- search--|
187 *
188 * We are re-using our test_start from above since it works out well.
189 */
190 set_extent_delalloc(&tmp, max_bytes, total_dirty - 1, 0, NULL);
191 start = test_start;
192 end = 0;
193 found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
194 &end, max_bytes);
195 if (!found) {
196 test_msg("Didn't find our range\n");
197 goto out_bits;
198 }
199 if (start != test_start || end != total_dirty - 1) {
200 test_msg("Expected start %Lu end %Lu, got start %Lu end %Lu\n",
201 test_start, total_dirty - 1, start, end);
202 goto out_bits;
203 }
204 if (process_page_range(inode, start, end,
205 PROCESS_TEST_LOCKED | PROCESS_UNLOCK)) {
206 test_msg("Pages in range were not all locked\n");
207 goto out_bits;
208 }
209 unlock_extent(&tmp, start, end);
210
211 /*
212 * Now to test where we run into a page that is no longer dirty in the
213 * range we want to find.
214 */
215 page = find_get_page(inode->i_mapping,
216 (max_bytes + SZ_1M) >> PAGE_SHIFT);
217 if (!page) {
218 test_msg("Couldn't find our page\n");
219 goto out_bits;
220 }
221 ClearPageDirty(page);
222 put_page(page);
223
224 /* We unlocked it in the previous test */
225 lock_page(locked_page);
226 start = test_start;
227 end = 0;
228 /*
229 * Currently if we fail to find dirty pages in the delalloc range we
230 * will adjust max_bytes down to PAGE_SIZE and then re-search. If
231 * this changes at any point in the future we will need to fix this
232 * tests expected behavior.
233 */
234 found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
235 &end, max_bytes);
236 if (!found) {
237 test_msg("Didn't find our range\n");
238 goto out_bits;
239 }
240 if (start != test_start && end != test_start + PAGE_SIZE - 1) {
241 test_msg("Expected start %Lu end %Lu, got start %Lu end %Lu\n",
242 test_start, test_start + PAGE_SIZE - 1, start,
243 end);
244 goto out_bits;
245 }
246 if (process_page_range(inode, start, end, PROCESS_TEST_LOCKED |
247 PROCESS_UNLOCK)) {
248 test_msg("Pages in range were not all locked\n");
249 goto out_bits;
250 }
251 ret = 0;
252out_bits:
253 clear_extent_bits(&tmp, 0, total_dirty - 1, (unsigned)-1);
254out:
255 if (locked_page)
256 put_page(locked_page);
257 process_page_range(inode, 0, total_dirty - 1,
258 PROCESS_UNLOCK | PROCESS_RELEASE);
259 iput(inode);
260 return ret;
261}
262
263static int check_eb_bitmap(unsigned long *bitmap, struct extent_buffer *eb,
264 unsigned long len)
265{
266 unsigned long i;
267
268 for (i = 0; i < len * BITS_PER_BYTE; i++) {
269 int bit, bit1;
270
271 bit = !!test_bit(i, bitmap);
272 bit1 = !!extent_buffer_test_bit(eb, 0, i);
273 if (bit1 != bit) {
274 test_msg("Bits do not match\n");
275 return -EINVAL;
276 }
277
278 bit1 = !!extent_buffer_test_bit(eb, i / BITS_PER_BYTE,
279 i % BITS_PER_BYTE);
280 if (bit1 != bit) {
281 test_msg("Offset bits do not match\n");
282 return -EINVAL;
283 }
284 }
285 return 0;
286}
287
288static int __test_eb_bitmaps(unsigned long *bitmap, struct extent_buffer *eb,
289 unsigned long len)
290{
291 unsigned long i, j;
292 u32 x;
293 int ret;
294
295 memset(bitmap, 0, len);
296 memzero_extent_buffer(eb, 0, len);
297 if (memcmp_extent_buffer(eb, bitmap, 0, len) != 0) {
298 test_msg("Bitmap was not zeroed\n");
299 return -EINVAL;
300 }
301
302 bitmap_set(bitmap, 0, len * BITS_PER_BYTE);
303 extent_buffer_bitmap_set(eb, 0, 0, len * BITS_PER_BYTE);
304 ret = check_eb_bitmap(bitmap, eb, len);
305 if (ret) {
306 test_msg("Setting all bits failed\n");
307 return ret;
308 }
309
310 bitmap_clear(bitmap, 0, len * BITS_PER_BYTE);
311 extent_buffer_bitmap_clear(eb, 0, 0, len * BITS_PER_BYTE);
312 ret = check_eb_bitmap(bitmap, eb, len);
313 if (ret) {
314 test_msg("Clearing all bits failed\n");
315 return ret;
316 }
317
318 /* Straddling pages test */
319 if (len > PAGE_SIZE) {
320 bitmap_set(bitmap,
321 (PAGE_SIZE - sizeof(long) / 2) * BITS_PER_BYTE,
322 sizeof(long) * BITS_PER_BYTE);
323 extent_buffer_bitmap_set(eb, PAGE_SIZE - sizeof(long) / 2, 0,
324 sizeof(long) * BITS_PER_BYTE);
325 ret = check_eb_bitmap(bitmap, eb, len);
326 if (ret) {
327 test_msg("Setting straddling pages failed\n");
328 return ret;
329 }
330
331 bitmap_set(bitmap, 0, len * BITS_PER_BYTE);
332 bitmap_clear(bitmap,
333 (PAGE_SIZE - sizeof(long) / 2) * BITS_PER_BYTE,
334 sizeof(long) * BITS_PER_BYTE);
335 extent_buffer_bitmap_set(eb, 0, 0, len * BITS_PER_BYTE);
336 extent_buffer_bitmap_clear(eb, PAGE_SIZE - sizeof(long) / 2, 0,
337 sizeof(long) * BITS_PER_BYTE);
338 ret = check_eb_bitmap(bitmap, eb, len);
339 if (ret) {
340 test_msg("Clearing straddling pages failed\n");
341 return ret;
342 }
343 }
344
345 /*
346 * Generate a wonky pseudo-random bit pattern for the sake of not using
347 * something repetitive that could miss some hypothetical off-by-n bug.
348 */
349 x = 0;
350 bitmap_clear(bitmap, 0, len * BITS_PER_BYTE);
351 extent_buffer_bitmap_clear(eb, 0, 0, len * BITS_PER_BYTE);
352 for (i = 0; i < len * BITS_PER_BYTE / 32; i++) {
353 x = (0x19660dULL * (u64)x + 0x3c6ef35fULL) & 0xffffffffU;
354 for (j = 0; j < 32; j++) {
355 if (x & (1U << j)) {
356 bitmap_set(bitmap, i * 32 + j, 1);
357 extent_buffer_bitmap_set(eb, 0, i * 32 + j, 1);
358 }
359 }
360 }
361
362 ret = check_eb_bitmap(bitmap, eb, len);
363 if (ret) {
364 test_msg("Random bit pattern failed\n");
365 return ret;
366 }
367
368 return 0;
369}
370
371static int test_eb_bitmaps(u32 sectorsize, u32 nodesize)
372{
373 struct btrfs_fs_info *fs_info;
374 unsigned long len;
375 unsigned long *bitmap;
376 struct extent_buffer *eb;
377 int ret;
378
379 test_msg("Running extent buffer bitmap tests\n");
380
381 /*
382 * In ppc64, sectorsize can be 64K, thus 4 * 64K will be larger than
383 * BTRFS_MAX_METADATA_BLOCKSIZE.
384 */
385 len = (sectorsize < BTRFS_MAX_METADATA_BLOCKSIZE)
386 ? sectorsize * 4 : sectorsize;
387
388 fs_info = btrfs_alloc_dummy_fs_info(len, len);
389
390 bitmap = kmalloc(len, GFP_KERNEL);
391 if (!bitmap) {
392 test_msg("Couldn't allocate test bitmap\n");
393 return -ENOMEM;
394 }
395
396 eb = __alloc_dummy_extent_buffer(fs_info, 0, len);
397 if (!eb) {
398 test_msg("Couldn't allocate test extent buffer\n");
399 kfree(bitmap);
400 return -ENOMEM;
401 }
402
403 ret = __test_eb_bitmaps(bitmap, eb, len);
404 if (ret)
405 goto out;
406
407 /* Do it over again with an extent buffer which isn't page-aligned. */
408 free_extent_buffer(eb);
409 eb = __alloc_dummy_extent_buffer(NULL, nodesize / 2, len);
410 if (!eb) {
411 test_msg("Couldn't allocate test extent buffer\n");
412 kfree(bitmap);
413 return -ENOMEM;
414 }
415
416 ret = __test_eb_bitmaps(bitmap, eb, len);
417out:
418 free_extent_buffer(eb);
419 kfree(bitmap);
420 return ret;
421}
422
423int btrfs_test_extent_io(u32 sectorsize, u32 nodesize)
424{
425 int ret;
426
427 test_msg("Running extent I/O tests\n");
428
429 ret = test_find_delalloc(sectorsize);
430 if (ret)
431 goto out;
432
433 ret = test_eb_bitmaps(sectorsize, nodesize);
434out:
435 test_msg("Extent I/O tests finished\n");
436 return ret;
437}