extent-io-tests.c 11 KB

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