extent-io-tests.c 11 KB

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