extent-io-tests.c 11 KB

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