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/sizes.h>
22 #include "btrfs-tests.h"
23 #include "../extent_io.h"
25 #define PROCESS_UNLOCK (1 << 0)
26 #define PROCESS_RELEASE (1 << 1)
27 #define PROCESS_TEST_LOCKED (1 << 2)
29 static noinline
int process_page_range(struct inode
*inode
, u64 start
, u64 end
,
33 struct page
*pages
[16];
34 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
35 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
36 unsigned long nr_pages
= end_index
- index
+ 1;
41 while (nr_pages
> 0) {
42 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
43 min_t(unsigned long, nr_pages
,
44 ARRAY_SIZE(pages
)), pages
);
45 for (i
= 0; i
< ret
; i
++) {
46 if (flags
& PROCESS_TEST_LOCKED
&&
47 !PageLocked(pages
[i
]))
49 if (flags
& PROCESS_UNLOCK
&& PageLocked(pages
[i
]))
50 unlock_page(pages
[i
]);
51 page_cache_release(pages
[i
]);
52 if (flags
& PROCESS_RELEASE
)
53 page_cache_release(pages
[i
]);
60 printk(KERN_ERR
"stuck in a loop, start %Lu, end %Lu, nr_pages %lu, ret %d\n", start
, end
, nr_pages
, ret
);
67 static int test_find_delalloc(void)
70 struct extent_io_tree tmp
;
72 struct page
*locked_page
= NULL
;
73 unsigned long index
= 0;
74 u64 total_dirty
= SZ_256M
;
75 u64 max_bytes
= SZ_128M
;
76 u64 start
, end
, test_start
;
80 inode
= btrfs_new_test_inode();
82 test_msg("Failed to allocate test inode\n");
86 extent_io_tree_init(&tmp
, &inode
->i_data
);
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
93 for (index
= 0; index
< (total_dirty
>> PAGE_CACHE_SHIFT
); index
++) {
94 page
= find_or_create_page(inode
->i_mapping
, index
, GFP_NOFS
);
96 test_msg("Failed to allocate test page\n");
104 page_cache_get(page
);
109 /* Test this scenario
113 set_extent_delalloc(&tmp
, 0, 4095, NULL
, GFP_NOFS
);
116 found
= find_lock_delalloc_range(inode
, &tmp
, locked_page
, &start
,
119 test_msg("Should have found at least one delalloc\n");
122 if (start
!= 0 || end
!= 4095) {
123 test_msg("Expected start 0 end 4095, got start %Lu end %Lu\n",
127 unlock_extent(&tmp
, start
, end
);
128 unlock_page(locked_page
);
129 page_cache_release(locked_page
);
138 locked_page
= find_lock_page(inode
->i_mapping
,
139 test_start
>> PAGE_CACHE_SHIFT
);
141 test_msg("Couldn't find the locked page\n");
144 set_extent_delalloc(&tmp
, 4096, max_bytes
- 1, NULL
, GFP_NOFS
);
147 found
= find_lock_delalloc_range(inode
, &tmp
, locked_page
, &start
,
150 test_msg("Couldn't find delalloc in our range\n");
153 if (start
!= test_start
|| end
!= max_bytes
- 1) {
154 test_msg("Expected start %Lu end %Lu, got start %Lu, end "
155 "%Lu\n", test_start
, max_bytes
- 1, start
, end
);
158 if (process_page_range(inode
, start
, end
,
159 PROCESS_TEST_LOCKED
| PROCESS_UNLOCK
)) {
160 test_msg("There were unlocked pages in the range\n");
163 unlock_extent(&tmp
, start
, end
);
164 /* locked_page was unlocked above */
165 page_cache_release(locked_page
);
172 test_start
= max_bytes
+ 4096;
173 locked_page
= find_lock_page(inode
->i_mapping
, test_start
>>
176 test_msg("Could'nt find the locked page\n");
181 found
= find_lock_delalloc_range(inode
, &tmp
, locked_page
, &start
,
184 test_msg("Found range when we shouldn't have\n");
187 if (end
!= (u64
)-1) {
188 test_msg("Did not return the proper end offset\n");
194 * [------- delalloc -------|
195 * [max_bytes]|-- search--|
197 * We are re-using our test_start from above since it works out well.
199 set_extent_delalloc(&tmp
, max_bytes
, total_dirty
- 1, NULL
, GFP_NOFS
);
202 found
= find_lock_delalloc_range(inode
, &tmp
, locked_page
, &start
,
205 test_msg("Didn't find our range\n");
208 if (start
!= test_start
|| end
!= total_dirty
- 1) {
209 test_msg("Expected start %Lu end %Lu, got start %Lu end %Lu\n",
210 test_start
, total_dirty
- 1, start
, end
);
213 if (process_page_range(inode
, start
, end
,
214 PROCESS_TEST_LOCKED
| PROCESS_UNLOCK
)) {
215 test_msg("Pages in range were not all locked\n");
218 unlock_extent(&tmp
, start
, end
);
221 * Now to test where we run into a page that is no longer dirty in the
222 * range we want to find.
224 page
= find_get_page(inode
->i_mapping
,
225 (max_bytes
+ SZ_1M
) >> PAGE_CACHE_SHIFT
);
227 test_msg("Couldn't find our page\n");
230 ClearPageDirty(page
);
231 page_cache_release(page
);
233 /* We unlocked it in the previous test */
234 lock_page(locked_page
);
238 * Currently if we fail to find dirty pages in the delalloc range we
239 * will adjust max_bytes down to PAGE_CACHE_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.
243 found
= find_lock_delalloc_range(inode
, &tmp
, locked_page
, &start
,
246 test_msg("Didn't find our range\n");
249 if (start
!= test_start
&& end
!= test_start
+ PAGE_CACHE_SIZE
- 1) {
250 test_msg("Expected start %Lu end %Lu, got start %Lu end %Lu\n",
251 test_start
, test_start
+ PAGE_CACHE_SIZE
- 1, start
,
255 if (process_page_range(inode
, start
, end
, PROCESS_TEST_LOCKED
|
257 test_msg("Pages in range were not all locked\n");
262 clear_extent_bits(&tmp
, 0, total_dirty
- 1, (unsigned)-1, GFP_NOFS
);
265 page_cache_release(locked_page
);
266 process_page_range(inode
, 0, total_dirty
- 1,
267 PROCESS_UNLOCK
| PROCESS_RELEASE
);
272 int btrfs_test_extent_io(void)
274 test_msg("Running find delalloc tests\n");
275 return test_find_delalloc();
This page took 0.036778 seconds and 6 git commands to generate.