Josef Bacik | 294e30f | 2013-10-09 12:00:56 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Fusion IO. All rights reserved. |
| 3 | * |
| 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. |
| 7 | * |
| 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. |
| 12 | * |
| 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. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/pagemap.h> |
| 20 | #include <linux/sched.h> |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame^] | 21 | #include <linux/slab.h> |
Josef Bacik | 294e30f | 2013-10-09 12:00:56 -0400 | [diff] [blame] | 22 | #include "btrfs-tests.h" |
| 23 | #include "../extent_io.h" |
| 24 | |
| 25 | #define PROCESS_UNLOCK (1 << 0) |
| 26 | #define PROCESS_RELEASE (1 << 1) |
| 27 | #define PROCESS_TEST_LOCKED (1 << 2) |
| 28 | |
| 29 | static noinline int process_page_range(struct inode *inode, u64 start, u64 end, |
| 30 | unsigned long flags) |
| 31 | { |
| 32 | int ret; |
| 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; |
| 37 | int i; |
| 38 | int count = 0; |
| 39 | int loops = 0; |
| 40 | |
| 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])) |
| 48 | count++; |
| 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]); |
| 54 | } |
| 55 | nr_pages -= ret; |
| 56 | index += ret; |
| 57 | cond_resched(); |
| 58 | loops++; |
| 59 | if (loops > 100000) { |
| 60 | printk(KERN_ERR "stuck in a loop, start %Lu, end %Lu, nr_pages %lu, ret %d\n", start, end, nr_pages, ret); |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | return count; |
| 65 | } |
| 66 | |
| 67 | static int test_find_delalloc(void) |
| 68 | { |
| 69 | struct inode *inode; |
| 70 | struct extent_io_tree tmp; |
| 71 | struct page *page; |
| 72 | struct page *locked_page = NULL; |
| 73 | unsigned long index = 0; |
| 74 | u64 total_dirty = 256 * 1024 * 1024; |
| 75 | u64 max_bytes = 128 * 1024 * 1024; |
| 76 | u64 start, end, test_start; |
| 77 | u64 found; |
| 78 | int ret = -EINVAL; |
| 79 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame^] | 80 | test_msg("Running find delalloc tests\n"); |
| 81 | |
Josef Bacik | 294e30f | 2013-10-09 12:00:56 -0400 | [diff] [blame] | 82 | inode = btrfs_new_test_inode(); |
| 83 | if (!inode) { |
| 84 | test_msg("Failed to allocate test inode\n"); |
| 85 | return -ENOMEM; |
| 86 | } |
| 87 | |
| 88 | extent_io_tree_init(&tmp, &inode->i_data); |
| 89 | |
| 90 | /* |
| 91 | * First go through and create and mark all of our pages dirty, we pin |
| 92 | * everything to make sure our pages don't get evicted and screw up our |
| 93 | * test. |
| 94 | */ |
| 95 | for (index = 0; index < (total_dirty >> PAGE_CACHE_SHIFT); index++) { |
| 96 | page = find_or_create_page(inode->i_mapping, index, GFP_NOFS); |
| 97 | if (!page) { |
| 98 | test_msg("Failed to allocate test page\n"); |
| 99 | ret = -ENOMEM; |
| 100 | goto out; |
| 101 | } |
| 102 | SetPageDirty(page); |
| 103 | if (index) { |
| 104 | unlock_page(page); |
| 105 | } else { |
| 106 | page_cache_get(page); |
| 107 | locked_page = page; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /* Test this scenario |
| 112 | * |--- delalloc ---| |
| 113 | * |--- search ---| |
| 114 | */ |
| 115 | set_extent_delalloc(&tmp, 0, 4095, NULL, GFP_NOFS); |
| 116 | start = 0; |
| 117 | end = 0; |
| 118 | found = find_lock_delalloc_range(inode, &tmp, locked_page, &start, |
| 119 | &end, max_bytes); |
| 120 | if (!found) { |
| 121 | test_msg("Should have found at least one delalloc\n"); |
| 122 | goto out_bits; |
| 123 | } |
| 124 | if (start != 0 || end != 4095) { |
| 125 | test_msg("Expected start 0 end 4095, got start %Lu end %Lu\n", |
| 126 | start, end); |
| 127 | goto out_bits; |
| 128 | } |
| 129 | unlock_extent(&tmp, start, end); |
| 130 | unlock_page(locked_page); |
| 131 | page_cache_release(locked_page); |
| 132 | |
| 133 | /* |
| 134 | * Test this scenario |
| 135 | * |
| 136 | * |--- delalloc ---| |
| 137 | * |--- search ---| |
| 138 | */ |
| 139 | test_start = 64 * 1024 * 1024; |
| 140 | locked_page = find_lock_page(inode->i_mapping, |
| 141 | test_start >> PAGE_CACHE_SHIFT); |
| 142 | if (!locked_page) { |
| 143 | test_msg("Couldn't find the locked page\n"); |
| 144 | goto out_bits; |
| 145 | } |
| 146 | set_extent_delalloc(&tmp, 4096, max_bytes - 1, NULL, GFP_NOFS); |
| 147 | start = test_start; |
| 148 | end = 0; |
| 149 | found = find_lock_delalloc_range(inode, &tmp, locked_page, &start, |
| 150 | &end, max_bytes); |
| 151 | if (!found) { |
| 152 | test_msg("Couldn't find delalloc in our range\n"); |
| 153 | goto out_bits; |
| 154 | } |
| 155 | if (start != test_start || end != max_bytes - 1) { |
| 156 | test_msg("Expected start %Lu end %Lu, got start %Lu, end " |
| 157 | "%Lu\n", test_start, max_bytes - 1, start, end); |
| 158 | goto out_bits; |
| 159 | } |
| 160 | if (process_page_range(inode, start, end, |
| 161 | PROCESS_TEST_LOCKED | PROCESS_UNLOCK)) { |
| 162 | test_msg("There were unlocked pages in the range\n"); |
| 163 | goto out_bits; |
| 164 | } |
| 165 | unlock_extent(&tmp, start, end); |
| 166 | /* locked_page was unlocked above */ |
| 167 | page_cache_release(locked_page); |
| 168 | |
| 169 | /* |
| 170 | * Test this scenario |
| 171 | * |--- delalloc ---| |
| 172 | * |--- search ---| |
| 173 | */ |
| 174 | test_start = max_bytes + 4096; |
| 175 | locked_page = find_lock_page(inode->i_mapping, test_start >> |
| 176 | PAGE_CACHE_SHIFT); |
| 177 | if (!locked_page) { |
| 178 | test_msg("Could'nt find the locked page\n"); |
| 179 | goto out_bits; |
| 180 | } |
| 181 | start = test_start; |
| 182 | end = 0; |
| 183 | found = find_lock_delalloc_range(inode, &tmp, locked_page, &start, |
| 184 | &end, max_bytes); |
| 185 | if (found) { |
| 186 | test_msg("Found range when we shouldn't have\n"); |
| 187 | goto out_bits; |
| 188 | } |
| 189 | if (end != (u64)-1) { |
| 190 | test_msg("Did not return the proper end offset\n"); |
| 191 | goto out_bits; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Test this scenario |
| 196 | * [------- delalloc -------| |
| 197 | * [max_bytes]|-- search--| |
| 198 | * |
| 199 | * We are re-using our test_start from above since it works out well. |
| 200 | */ |
| 201 | set_extent_delalloc(&tmp, max_bytes, total_dirty - 1, NULL, GFP_NOFS); |
| 202 | start = test_start; |
| 203 | end = 0; |
| 204 | found = find_lock_delalloc_range(inode, &tmp, locked_page, &start, |
| 205 | &end, max_bytes); |
| 206 | if (!found) { |
| 207 | test_msg("Didn't find our range\n"); |
| 208 | goto out_bits; |
| 209 | } |
| 210 | if (start != test_start || end != total_dirty - 1) { |
| 211 | test_msg("Expected start %Lu end %Lu, got start %Lu end %Lu\n", |
| 212 | test_start, total_dirty - 1, start, end); |
| 213 | goto out_bits; |
| 214 | } |
| 215 | if (process_page_range(inode, start, end, |
| 216 | PROCESS_TEST_LOCKED | PROCESS_UNLOCK)) { |
| 217 | test_msg("Pages in range were not all locked\n"); |
| 218 | goto out_bits; |
| 219 | } |
| 220 | unlock_extent(&tmp, start, end); |
| 221 | |
| 222 | /* |
| 223 | * Now to test where we run into a page that is no longer dirty in the |
| 224 | * range we want to find. |
| 225 | */ |
| 226 | page = find_get_page(inode->i_mapping, (max_bytes + (1 * 1024 * 1024)) |
| 227 | >> PAGE_CACHE_SHIFT); |
| 228 | if (!page) { |
| 229 | test_msg("Couldn't find our page\n"); |
| 230 | goto out_bits; |
| 231 | } |
| 232 | ClearPageDirty(page); |
| 233 | page_cache_release(page); |
| 234 | |
| 235 | /* We unlocked it in the previous test */ |
| 236 | lock_page(locked_page); |
| 237 | start = test_start; |
| 238 | end = 0; |
| 239 | /* |
| 240 | * Currently if we fail to find dirty pages in the delalloc range we |
| 241 | * will adjust max_bytes down to PAGE_CACHE_SIZE and then re-search. If |
| 242 | * this changes at any point in the future we will need to fix this |
| 243 | * tests expected behavior. |
| 244 | */ |
| 245 | found = find_lock_delalloc_range(inode, &tmp, locked_page, &start, |
| 246 | &end, max_bytes); |
| 247 | if (!found) { |
| 248 | test_msg("Didn't find our range\n"); |
| 249 | goto out_bits; |
| 250 | } |
| 251 | if (start != test_start && end != test_start + PAGE_CACHE_SIZE - 1) { |
| 252 | test_msg("Expected start %Lu end %Lu, got start %Lu end %Lu\n", |
| 253 | test_start, test_start + PAGE_CACHE_SIZE - 1, start, |
| 254 | end); |
| 255 | goto out_bits; |
| 256 | } |
| 257 | if (process_page_range(inode, start, end, PROCESS_TEST_LOCKED | |
| 258 | PROCESS_UNLOCK)) { |
| 259 | test_msg("Pages in range were not all locked\n"); |
| 260 | goto out_bits; |
| 261 | } |
| 262 | ret = 0; |
| 263 | out_bits: |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 264 | clear_extent_bits(&tmp, 0, total_dirty - 1, (unsigned)-1, GFP_NOFS); |
Josef Bacik | 294e30f | 2013-10-09 12:00:56 -0400 | [diff] [blame] | 265 | out: |
| 266 | if (locked_page) |
| 267 | page_cache_release(locked_page); |
| 268 | process_page_range(inode, 0, total_dirty - 1, |
| 269 | PROCESS_UNLOCK | PROCESS_RELEASE); |
| 270 | iput(inode); |
| 271 | return ret; |
| 272 | } |
| 273 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame^] | 274 | static int __test_eb_bitmaps(unsigned long *bitmap, struct extent_buffer *eb, |
| 275 | unsigned long len) |
| 276 | { |
| 277 | unsigned long i, x; |
| 278 | |
| 279 | memset(bitmap, 0, len); |
| 280 | memset_extent_buffer(eb, 0, 0, len); |
| 281 | if (memcmp_extent_buffer(eb, bitmap, 0, len) != 0) { |
| 282 | test_msg("Bitmap was not zeroed\n"); |
| 283 | return -EINVAL; |
| 284 | } |
| 285 | |
| 286 | bitmap_set(bitmap, 0, len * BITS_PER_BYTE); |
| 287 | extent_buffer_bitmap_set(eb, 0, 0, len * BITS_PER_BYTE); |
| 288 | if (memcmp_extent_buffer(eb, bitmap, 0, len) != 0) { |
| 289 | test_msg("Setting all bits failed\n"); |
| 290 | return -EINVAL; |
| 291 | } |
| 292 | |
| 293 | bitmap_clear(bitmap, 0, len * BITS_PER_BYTE); |
| 294 | extent_buffer_bitmap_clear(eb, 0, 0, len * BITS_PER_BYTE); |
| 295 | if (memcmp_extent_buffer(eb, bitmap, 0, len) != 0) { |
| 296 | test_msg("Clearing all bits failed\n"); |
| 297 | return -EINVAL; |
| 298 | } |
| 299 | |
| 300 | bitmap_set(bitmap, (PAGE_CACHE_SIZE - sizeof(long) / 2) * BITS_PER_BYTE, |
| 301 | sizeof(long) * BITS_PER_BYTE); |
| 302 | extent_buffer_bitmap_set(eb, PAGE_CACHE_SIZE - sizeof(long) / 2, 0, |
| 303 | sizeof(long) * BITS_PER_BYTE); |
| 304 | if (memcmp_extent_buffer(eb, bitmap, 0, len) != 0) { |
| 305 | test_msg("Setting straddling pages failed\n"); |
| 306 | return -EINVAL; |
| 307 | } |
| 308 | |
| 309 | bitmap_set(bitmap, 0, len * BITS_PER_BYTE); |
| 310 | bitmap_clear(bitmap, |
| 311 | (PAGE_CACHE_SIZE - sizeof(long) / 2) * BITS_PER_BYTE, |
| 312 | sizeof(long) * BITS_PER_BYTE); |
| 313 | extent_buffer_bitmap_set(eb, 0, 0, len * BITS_PER_BYTE); |
| 314 | extent_buffer_bitmap_clear(eb, PAGE_CACHE_SIZE - sizeof(long) / 2, 0, |
| 315 | sizeof(long) * BITS_PER_BYTE); |
| 316 | if (memcmp_extent_buffer(eb, bitmap, 0, len) != 0) { |
| 317 | test_msg("Clearing straddling pages failed\n"); |
| 318 | return -EINVAL; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Generate a wonky pseudo-random bit pattern for the sake of not using |
| 323 | * something repetitive that could miss some hypothetical off-by-n bug. |
| 324 | */ |
| 325 | x = 0; |
| 326 | for (i = 0; i < len / sizeof(long); i++) { |
| 327 | x = (0x19660dULL * (u64)x + 0x3c6ef35fULL) & 0xffffffffUL; |
| 328 | bitmap[i] = x; |
| 329 | } |
| 330 | write_extent_buffer(eb, bitmap, 0, len); |
| 331 | |
| 332 | for (i = 0; i < len * BITS_PER_BYTE; i++) { |
| 333 | int bit, bit1; |
| 334 | |
| 335 | bit = !!test_bit(i, bitmap); |
| 336 | bit1 = !!extent_buffer_test_bit(eb, 0, i); |
| 337 | if (bit1 != bit) { |
| 338 | test_msg("Testing bit pattern failed\n"); |
| 339 | return -EINVAL; |
| 340 | } |
| 341 | |
| 342 | bit1 = !!extent_buffer_test_bit(eb, i / BITS_PER_BYTE, |
| 343 | i % BITS_PER_BYTE); |
| 344 | if (bit1 != bit) { |
| 345 | test_msg("Testing bit pattern with offset failed\n"); |
| 346 | return -EINVAL; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static int test_eb_bitmaps(void) |
| 354 | { |
| 355 | unsigned long len = PAGE_CACHE_SIZE * 4; |
| 356 | unsigned long *bitmap; |
| 357 | struct extent_buffer *eb; |
| 358 | int ret; |
| 359 | |
| 360 | test_msg("Running extent buffer bitmap tests\n"); |
| 361 | |
| 362 | bitmap = kmalloc(len, GFP_NOFS); |
| 363 | if (!bitmap) { |
| 364 | test_msg("Couldn't allocate test bitmap\n"); |
| 365 | return -ENOMEM; |
| 366 | } |
| 367 | |
| 368 | eb = __alloc_dummy_extent_buffer(NULL, 0, len); |
| 369 | if (!eb) { |
| 370 | test_msg("Couldn't allocate test extent buffer\n"); |
| 371 | kfree(bitmap); |
| 372 | return -ENOMEM; |
| 373 | } |
| 374 | |
| 375 | ret = __test_eb_bitmaps(bitmap, eb, len); |
| 376 | if (ret) |
| 377 | goto out; |
| 378 | |
| 379 | /* Do it over again with an extent buffer which isn't page-aligned. */ |
| 380 | free_extent_buffer(eb); |
| 381 | eb = __alloc_dummy_extent_buffer(NULL, PAGE_CACHE_SIZE / 2, len); |
| 382 | if (!eb) { |
| 383 | test_msg("Couldn't allocate test extent buffer\n"); |
| 384 | kfree(bitmap); |
| 385 | return -ENOMEM; |
| 386 | } |
| 387 | |
| 388 | ret = __test_eb_bitmaps(bitmap, eb, len); |
| 389 | out: |
| 390 | free_extent_buffer(eb); |
| 391 | kfree(bitmap); |
| 392 | return ret; |
| 393 | } |
| 394 | |
Josef Bacik | 294e30f | 2013-10-09 12:00:56 -0400 | [diff] [blame] | 395 | int btrfs_test_extent_io(void) |
| 396 | { |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame^] | 397 | int ret; |
| 398 | |
| 399 | test_msg("Running extent I/O tests\n"); |
| 400 | |
| 401 | ret = test_find_delalloc(); |
| 402 | if (ret) |
| 403 | goto out; |
| 404 | |
| 405 | ret = test_eb_bitmaps(); |
| 406 | out: |
| 407 | test_msg("Extent I/O tests finished\n"); |
| 408 | return ret; |
Josef Bacik | 294e30f | 2013-10-09 12:00:56 -0400 | [diff] [blame] | 409 | } |