1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2013 Fusion IO. All rights reserved.
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"
12 #include "../extent_io.h"
14 #define PROCESS_UNLOCK (1 << 0)
15 #define PROCESS_RELEASE (1 << 1)
16 #define PROCESS_TEST_LOCKED (1 << 2)
18 static noinline int process_page_range(struct inode *inode, u64 start, u64 end,
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;
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]))
38 if (flags & PROCESS_UNLOCK && PageLocked(pages[i]))
39 unlock_page(pages[i]);
41 if (flags & PROCESS_RELEASE)
50 "stuck in a loop, start %llu, end %llu, nr_pages %lu, ret %d\n",
51 start, end, nr_pages, ret);
58 static int test_find_delalloc(u32 sectorsize)
61 struct extent_io_tree tmp;
63 struct page *locked_page = NULL;
64 unsigned long index = 0;
65 /* In this test we need at least 2 file extents at its maximum size */
66 u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
67 u64 total_dirty = 2 * max_bytes;
68 u64 start, end, test_start;
72 test_msg("running find delalloc tests");
74 inode = btrfs_new_test_inode();
76 test_std_err(TEST_ALLOC_INODE);
81 * Passing NULL as we don't have fs_info but tracepoints are not used
84 extent_io_tree_init(NULL, &tmp, IO_TREE_SELFTEST, NULL);
87 * First go through and create and mark all of our pages dirty, we pin
88 * everything to make sure our pages don't get evicted and screw up our
91 for (index = 0; index < (total_dirty >> PAGE_SHIFT); index++) {
92 page = find_or_create_page(inode->i_mapping, index, GFP_KERNEL);
94 test_err("failed to allocate test page");
107 /* Test this scenario
111 set_extent_delalloc(&tmp, 0, sectorsize - 1, 0, NULL);
114 found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
117 test_err("should have found at least one delalloc");
120 if (start != 0 || end != (sectorsize - 1)) {
121 test_err("expected start 0 end %u, got start %llu end %llu",
122 sectorsize - 1, start, end);
125 unlock_extent(&tmp, start, end);
126 unlock_page(locked_page);
127 put_page(locked_page);
136 locked_page = find_lock_page(inode->i_mapping,
137 test_start >> PAGE_SHIFT);
139 test_err("couldn't find the locked page");
142 set_extent_delalloc(&tmp, sectorsize, max_bytes - 1, 0, NULL);
145 found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
148 test_err("couldn't find delalloc in our range");
151 if (start != test_start || end != max_bytes - 1) {
152 test_err("expected start %llu end %llu, got start %llu, end %llu",
153 test_start, max_bytes - 1, start, end);
156 if (process_page_range(inode, start, end,
157 PROCESS_TEST_LOCKED | PROCESS_UNLOCK)) {
158 test_err("there were unlocked pages in the range");
161 unlock_extent(&tmp, start, end);
162 /* locked_page was unlocked above */
163 put_page(locked_page);
170 test_start = max_bytes + sectorsize;
171 locked_page = find_lock_page(inode->i_mapping, test_start >>
174 test_err("couldn't find the locked page");
179 found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
182 test_err("found range when we shouldn't have");
185 if (end != (u64)-1) {
186 test_err("did not return the proper end offset");
192 * [------- delalloc -------|
193 * [max_bytes]|-- search--|
195 * We are re-using our test_start from above since it works out well.
197 set_extent_delalloc(&tmp, max_bytes, total_dirty - 1, 0, NULL);
200 found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
203 test_err("didn't find our range");
206 if (start != test_start || end != total_dirty - 1) {
207 test_err("expected start %llu end %llu, got start %llu end %llu",
208 test_start, total_dirty - 1, start, end);
211 if (process_page_range(inode, start, end,
212 PROCESS_TEST_LOCKED | PROCESS_UNLOCK)) {
213 test_err("pages in range were not all locked");
216 unlock_extent(&tmp, start, end);
219 * Now to test where we run into a page that is no longer dirty in the
220 * range we want to find.
222 page = find_get_page(inode->i_mapping,
223 (max_bytes + SZ_1M) >> PAGE_SHIFT);
225 test_err("couldn't find our page");
228 ClearPageDirty(page);
231 /* We unlocked it in the previous test */
232 lock_page(locked_page);
236 * Currently if we fail to find dirty pages in the delalloc range we
237 * will adjust max_bytes down to PAGE_SIZE and then re-search. If
238 * this changes at any point in the future we will need to fix this
239 * tests expected behavior.
241 found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
244 test_err("didn't find our range");
247 if (start != test_start && end != test_start + PAGE_SIZE - 1) {
248 test_err("expected start %llu end %llu, got start %llu end %llu",
249 test_start, test_start + PAGE_SIZE - 1, start, end);
252 if (process_page_range(inode, start, end, PROCESS_TEST_LOCKED |
254 test_err("pages in range were not all locked");
259 clear_extent_bits(&tmp, 0, total_dirty - 1, (unsigned)-1);
262 put_page(locked_page);
263 process_page_range(inode, 0, total_dirty - 1,
264 PROCESS_UNLOCK | PROCESS_RELEASE);
269 static int check_eb_bitmap(unsigned long *bitmap, struct extent_buffer *eb,
274 for (i = 0; i < len * BITS_PER_BYTE; i++) {
277 bit = !!test_bit(i, bitmap);
278 bit1 = !!extent_buffer_test_bit(eb, 0, i);
280 test_err("bits do not match");
284 bit1 = !!extent_buffer_test_bit(eb, i / BITS_PER_BYTE,
287 test_err("offset bits do not match");
294 static int __test_eb_bitmaps(unsigned long *bitmap, struct extent_buffer *eb,
301 memset(bitmap, 0, len);
302 memzero_extent_buffer(eb, 0, len);
303 if (memcmp_extent_buffer(eb, bitmap, 0, len) != 0) {
304 test_err("bitmap was not zeroed");
308 bitmap_set(bitmap, 0, len * BITS_PER_BYTE);
309 extent_buffer_bitmap_set(eb, 0, 0, len * BITS_PER_BYTE);
310 ret = check_eb_bitmap(bitmap, eb, len);
312 test_err("setting all bits failed");
316 bitmap_clear(bitmap, 0, len * BITS_PER_BYTE);
317 extent_buffer_bitmap_clear(eb, 0, 0, len * BITS_PER_BYTE);
318 ret = check_eb_bitmap(bitmap, eb, len);
320 test_err("clearing all bits failed");
324 /* Straddling pages test */
325 if (len > PAGE_SIZE) {
327 (PAGE_SIZE - sizeof(long) / 2) * BITS_PER_BYTE,
328 sizeof(long) * BITS_PER_BYTE);
329 extent_buffer_bitmap_set(eb, PAGE_SIZE - sizeof(long) / 2, 0,
330 sizeof(long) * BITS_PER_BYTE);
331 ret = check_eb_bitmap(bitmap, eb, len);
333 test_err("setting straddling pages failed");
337 bitmap_set(bitmap, 0, len * BITS_PER_BYTE);
339 (PAGE_SIZE - sizeof(long) / 2) * BITS_PER_BYTE,
340 sizeof(long) * BITS_PER_BYTE);
341 extent_buffer_bitmap_set(eb, 0, 0, len * BITS_PER_BYTE);
342 extent_buffer_bitmap_clear(eb, PAGE_SIZE - sizeof(long) / 2, 0,
343 sizeof(long) * BITS_PER_BYTE);
344 ret = check_eb_bitmap(bitmap, eb, len);
346 test_err("clearing straddling pages failed");
352 * Generate a wonky pseudo-random bit pattern for the sake of not using
353 * something repetitive that could miss some hypothetical off-by-n bug.
356 bitmap_clear(bitmap, 0, len * BITS_PER_BYTE);
357 extent_buffer_bitmap_clear(eb, 0, 0, len * BITS_PER_BYTE);
358 for (i = 0; i < len * BITS_PER_BYTE / 32; i++) {
359 x = (0x19660dULL * (u64)x + 0x3c6ef35fULL) & 0xffffffffU;
360 for (j = 0; j < 32; j++) {
362 bitmap_set(bitmap, i * 32 + j, 1);
363 extent_buffer_bitmap_set(eb, 0, i * 32 + j, 1);
368 ret = check_eb_bitmap(bitmap, eb, len);
370 test_err("random bit pattern failed");
377 static int test_eb_bitmaps(u32 sectorsize, u32 nodesize)
379 struct btrfs_fs_info *fs_info;
381 unsigned long *bitmap = NULL;
382 struct extent_buffer *eb = NULL;
385 test_msg("running extent buffer bitmap tests");
388 * In ppc64, sectorsize can be 64K, thus 4 * 64K will be larger than
389 * BTRFS_MAX_METADATA_BLOCKSIZE.
391 len = (sectorsize < BTRFS_MAX_METADATA_BLOCKSIZE)
392 ? sectorsize * 4 : sectorsize;
394 fs_info = btrfs_alloc_dummy_fs_info(len, len);
396 test_std_err(TEST_ALLOC_FS_INFO);
400 bitmap = kmalloc(len, GFP_KERNEL);
402 test_err("couldn't allocate test bitmap");
407 eb = __alloc_dummy_extent_buffer(fs_info, 0, len);
409 test_std_err(TEST_ALLOC_ROOT);
414 ret = __test_eb_bitmaps(bitmap, eb, len);
418 /* Do it over again with an extent buffer which isn't page-aligned. */
419 free_extent_buffer(eb);
420 eb = __alloc_dummy_extent_buffer(fs_info, nodesize / 2, len);
422 test_std_err(TEST_ALLOC_ROOT);
427 ret = __test_eb_bitmaps(bitmap, eb, len);
429 free_extent_buffer(eb);
431 btrfs_free_dummy_fs_info(fs_info);
435 int btrfs_test_extent_io(u32 sectorsize, u32 nodesize)
439 test_msg("running extent I/O tests");
441 ret = test_find_delalloc(sectorsize);
445 ret = test_eb_bitmaps(sectorsize, nodesize);