2 * Copyright (C) 2013 Fusion IO. All rights reserved.
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.
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.
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.
19 #include <linux/pagemap.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/sizes.h>
23 #include "btrfs-tests.h"
25 #include "../extent_io.h"
27 #define PROCESS_UNLOCK (1 << 0)
28 #define PROCESS_RELEASE (1 << 1)
29 #define PROCESS_TEST_LOCKED (1 << 2)
31 static noinline
int process_page_range(struct inode
*inode
, u64 start
, u64 end
,
35 struct page
*pages
[16];
36 unsigned long index
= start
>> PAGE_SHIFT
;
37 unsigned long end_index
= end
>> PAGE_SHIFT
;
38 unsigned long nr_pages
= end_index
- index
+ 1;
43 while (nr_pages
> 0) {
44 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
45 min_t(unsigned long, nr_pages
,
46 ARRAY_SIZE(pages
)), pages
);
47 for (i
= 0; i
< ret
; i
++) {
48 if (flags
& PROCESS_TEST_LOCKED
&&
49 !PageLocked(pages
[i
]))
51 if (flags
& PROCESS_UNLOCK
&& PageLocked(pages
[i
]))
52 unlock_page(pages
[i
]);
54 if (flags
& PROCESS_RELEASE
)
62 printk(KERN_ERR
"stuck in a loop, start %Lu, end %Lu, nr_pages %lu, ret %d\n", start
, end
, nr_pages
, ret
);
69 static int test_find_delalloc(u32 sectorsize
)
72 struct extent_io_tree tmp
;
74 struct page
*locked_page
= NULL
;
75 unsigned long index
= 0;
76 u64 total_dirty
= SZ_256M
;
77 u64 max_bytes
= SZ_128M
;
78 u64 start
, end
, test_start
;
82 test_msg("Running find delalloc tests\n");
84 inode
= btrfs_new_test_inode();
86 test_msg("Failed to allocate test inode\n");
90 extent_io_tree_init(&tmp
, inode
);
93 * First go through and create and mark all of our pages dirty, we pin
94 * everything to make sure our pages don't get evicted and screw up our
97 for (index
= 0; index
< (total_dirty
>> PAGE_SHIFT
); index
++) {
98 page
= find_or_create_page(inode
->i_mapping
, index
, GFP_KERNEL
);
100 test_msg("Failed to allocate test page\n");
113 /* Test this scenario
117 set_extent_delalloc(&tmp
, 0, sectorsize
- 1, 0, NULL
);
120 found
= find_lock_delalloc_range(inode
, &tmp
, locked_page
, &start
,
123 test_msg("Should have found at least one delalloc\n");
126 if (start
!= 0 || end
!= (sectorsize
- 1)) {
127 test_msg("Expected start 0 end %u, got start %llu end %llu\n",
128 sectorsize
- 1, start
, end
);
131 unlock_extent(&tmp
, start
, end
);
132 unlock_page(locked_page
);
133 put_page(locked_page
);
142 locked_page
= find_lock_page(inode
->i_mapping
,
143 test_start
>> PAGE_SHIFT
);
145 test_msg("Couldn't find the locked page\n");
148 set_extent_delalloc(&tmp
, sectorsize
, max_bytes
- 1, 0, NULL
);
151 found
= find_lock_delalloc_range(inode
, &tmp
, locked_page
, &start
,
154 test_msg("Couldn't find delalloc in our range\n");
157 if (start
!= test_start
|| end
!= max_bytes
- 1) {
158 test_msg("Expected start %Lu end %Lu, got start %Lu, end "
159 "%Lu\n", test_start
, max_bytes
- 1, start
, end
);
162 if (process_page_range(inode
, start
, end
,
163 PROCESS_TEST_LOCKED
| PROCESS_UNLOCK
)) {
164 test_msg("There were unlocked pages in the range\n");
167 unlock_extent(&tmp
, start
, end
);
168 /* locked_page was unlocked above */
169 put_page(locked_page
);
176 test_start
= max_bytes
+ sectorsize
;
177 locked_page
= find_lock_page(inode
->i_mapping
, test_start
>>
180 test_msg("Couldn't find the locked page\n");
185 found
= find_lock_delalloc_range(inode
, &tmp
, locked_page
, &start
,
188 test_msg("Found range when we shouldn't have\n");
191 if (end
!= (u64
)-1) {
192 test_msg("Did not return the proper end offset\n");
198 * [------- delalloc -------|
199 * [max_bytes]|-- search--|
201 * We are re-using our test_start from above since it works out well.
203 set_extent_delalloc(&tmp
, max_bytes
, total_dirty
- 1, 0, NULL
);
206 found
= find_lock_delalloc_range(inode
, &tmp
, locked_page
, &start
,
209 test_msg("Didn't find our range\n");
212 if (start
!= test_start
|| end
!= total_dirty
- 1) {
213 test_msg("Expected start %Lu end %Lu, got start %Lu end %Lu\n",
214 test_start
, total_dirty
- 1, start
, end
);
217 if (process_page_range(inode
, start
, end
,
218 PROCESS_TEST_LOCKED
| PROCESS_UNLOCK
)) {
219 test_msg("Pages in range were not all locked\n");
222 unlock_extent(&tmp
, start
, end
);
225 * Now to test where we run into a page that is no longer dirty in the
226 * range we want to find.
228 page
= find_get_page(inode
->i_mapping
,
229 (max_bytes
+ SZ_1M
) >> PAGE_SHIFT
);
231 test_msg("Couldn't find our page\n");
234 ClearPageDirty(page
);
237 /* We unlocked it in the previous test */
238 lock_page(locked_page
);
242 * Currently if we fail to find dirty pages in the delalloc range we
243 * will adjust max_bytes down to PAGE_SIZE and then re-search. If
244 * this changes at any point in the future we will need to fix this
245 * tests expected behavior.
247 found
= find_lock_delalloc_range(inode
, &tmp
, locked_page
, &start
,
250 test_msg("Didn't find our range\n");
253 if (start
!= test_start
&& end
!= test_start
+ PAGE_SIZE
- 1) {
254 test_msg("Expected start %Lu end %Lu, got start %Lu end %Lu\n",
255 test_start
, test_start
+ PAGE_SIZE
- 1, start
,
259 if (process_page_range(inode
, start
, end
, PROCESS_TEST_LOCKED
|
261 test_msg("Pages in range were not all locked\n");
266 clear_extent_bits(&tmp
, 0, total_dirty
- 1, (unsigned)-1);
269 put_page(locked_page
);
270 process_page_range(inode
, 0, total_dirty
- 1,
271 PROCESS_UNLOCK
| PROCESS_RELEASE
);
276 static int check_eb_bitmap(unsigned long *bitmap
, struct extent_buffer
*eb
,
281 for (i
= 0; i
< len
* BITS_PER_BYTE
; i
++) {
284 bit
= !!test_bit(i
, bitmap
);
285 bit1
= !!extent_buffer_test_bit(eb
, 0, i
);
287 test_msg("Bits do not match\n");
291 bit1
= !!extent_buffer_test_bit(eb
, i
/ BITS_PER_BYTE
,
294 test_msg("Offset bits do not match\n");
301 static int __test_eb_bitmaps(unsigned long *bitmap
, struct extent_buffer
*eb
,
308 memset(bitmap
, 0, len
);
309 memzero_extent_buffer(eb
, 0, len
);
310 if (memcmp_extent_buffer(eb
, bitmap
, 0, len
) != 0) {
311 test_msg("Bitmap was not zeroed\n");
315 bitmap_set(bitmap
, 0, len
* BITS_PER_BYTE
);
316 extent_buffer_bitmap_set(eb
, 0, 0, len
* BITS_PER_BYTE
);
317 ret
= check_eb_bitmap(bitmap
, eb
, len
);
319 test_msg("Setting all bits failed\n");
323 bitmap_clear(bitmap
, 0, len
* BITS_PER_BYTE
);
324 extent_buffer_bitmap_clear(eb
, 0, 0, len
* BITS_PER_BYTE
);
325 ret
= check_eb_bitmap(bitmap
, eb
, len
);
327 test_msg("Clearing all bits failed\n");
331 /* Straddling pages test */
332 if (len
> PAGE_SIZE
) {
334 (PAGE_SIZE
- sizeof(long) / 2) * BITS_PER_BYTE
,
335 sizeof(long) * BITS_PER_BYTE
);
336 extent_buffer_bitmap_set(eb
, PAGE_SIZE
- sizeof(long) / 2, 0,
337 sizeof(long) * BITS_PER_BYTE
);
338 ret
= check_eb_bitmap(bitmap
, eb
, len
);
340 test_msg("Setting straddling pages failed\n");
344 bitmap_set(bitmap
, 0, len
* BITS_PER_BYTE
);
346 (PAGE_SIZE
- sizeof(long) / 2) * BITS_PER_BYTE
,
347 sizeof(long) * BITS_PER_BYTE
);
348 extent_buffer_bitmap_set(eb
, 0, 0, len
* BITS_PER_BYTE
);
349 extent_buffer_bitmap_clear(eb
, PAGE_SIZE
- sizeof(long) / 2, 0,
350 sizeof(long) * BITS_PER_BYTE
);
351 ret
= check_eb_bitmap(bitmap
, eb
, len
);
353 test_msg("Clearing straddling pages failed\n");
359 * Generate a wonky pseudo-random bit pattern for the sake of not using
360 * something repetitive that could miss some hypothetical off-by-n bug.
363 bitmap_clear(bitmap
, 0, len
* BITS_PER_BYTE
);
364 extent_buffer_bitmap_clear(eb
, 0, 0, len
* BITS_PER_BYTE
);
365 for (i
= 0; i
< len
* BITS_PER_BYTE
/ 32; i
++) {
366 x
= (0x19660dULL
* (u64
)x
+ 0x3c6ef35fULL
) & 0xffffffffU
;
367 for (j
= 0; j
< 32; j
++) {
369 bitmap_set(bitmap
, i
* 32 + j
, 1);
370 extent_buffer_bitmap_set(eb
, 0, i
* 32 + j
, 1);
375 ret
= check_eb_bitmap(bitmap
, eb
, len
);
377 test_msg("Random bit pattern failed\n");
384 static int test_eb_bitmaps(u32 sectorsize
, u32 nodesize
)
386 struct btrfs_fs_info
*fs_info
;
388 unsigned long *bitmap
;
389 struct extent_buffer
*eb
;
392 test_msg("Running extent buffer bitmap tests\n");
395 * In ppc64, sectorsize can be 64K, thus 4 * 64K will be larger than
396 * BTRFS_MAX_METADATA_BLOCKSIZE.
398 len
= (sectorsize
< BTRFS_MAX_METADATA_BLOCKSIZE
)
399 ? sectorsize
* 4 : sectorsize
;
401 fs_info
= btrfs_alloc_dummy_fs_info(len
, len
);
403 bitmap
= kmalloc(len
, GFP_KERNEL
);
405 test_msg("Couldn't allocate test bitmap\n");
409 eb
= __alloc_dummy_extent_buffer(fs_info
, 0, len
);
411 test_msg("Couldn't allocate test extent buffer\n");
416 ret
= __test_eb_bitmaps(bitmap
, eb
, len
);
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(NULL
, nodesize
/ 2, len
);
424 test_msg("Couldn't allocate test extent buffer\n");
429 ret
= __test_eb_bitmaps(bitmap
, eb
, len
);
431 free_extent_buffer(eb
);
436 int btrfs_test_extent_io(u32 sectorsize
, u32 nodesize
)
440 test_msg("Running extent I/O tests\n");
442 ret
= test_find_delalloc(sectorsize
);
446 ret
= test_eb_bitmaps(sectorsize
, nodesize
);
448 test_msg("Extent I/O tests finished\n");