extent-io-tests.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "btrfs-tests.h"
  21. #include "../extent_io.h"
  22. #define PROCESS_UNLOCK (1 << 0)
  23. #define PROCESS_RELEASE (1 << 1)
  24. #define PROCESS_TEST_LOCKED (1 << 2)
  25. static noinline int process_page_range(struct inode *inode, u64 start, u64 end,
  26. unsigned long flags)
  27. {
  28. int ret;
  29. struct page *pages[16];
  30. unsigned long index = start >> PAGE_CACHE_SHIFT;
  31. unsigned long end_index = end >> PAGE_CACHE_SHIFT;
  32. unsigned long nr_pages = end_index - index + 1;
  33. int i;
  34. int count = 0;
  35. int loops = 0;
  36. while (nr_pages > 0) {
  37. ret = find_get_pages_contig(inode->i_mapping, index,
  38. min_t(unsigned long, nr_pages,
  39. ARRAY_SIZE(pages)), pages);
  40. for (i = 0; i < ret; i++) {
  41. if (flags & PROCESS_TEST_LOCKED &&
  42. !PageLocked(pages[i]))
  43. count++;
  44. if (flags & PROCESS_UNLOCK && PageLocked(pages[i]))
  45. unlock_page(pages[i]);
  46. page_cache_release(pages[i]);
  47. if (flags & PROCESS_RELEASE)
  48. page_cache_release(pages[i]);
  49. }
  50. nr_pages -= ret;
  51. index += ret;
  52. cond_resched();
  53. loops++;
  54. if (loops > 100000) {
  55. printk(KERN_ERR "stuck in a loop, start %Lu, end %Lu, nr_pages %lu, ret %d\n", start, end, nr_pages, ret);
  56. break;
  57. }
  58. }
  59. return count;
  60. }
  61. static int test_find_delalloc(void)
  62. {
  63. struct inode *inode;
  64. struct extent_io_tree tmp;
  65. struct page *page;
  66. struct page *locked_page = NULL;
  67. unsigned long index = 0;
  68. u64 total_dirty = 256 * 1024 * 1024;
  69. u64 max_bytes = 128 * 1024 * 1024;
  70. u64 start, end, test_start;
  71. u64 found;
  72. int ret = -EINVAL;
  73. inode = btrfs_new_test_inode();
  74. if (!inode) {
  75. test_msg("Failed to allocate test inode\n");
  76. return -ENOMEM;
  77. }
  78. extent_io_tree_init(&tmp, &inode->i_data);
  79. /*
  80. * First go through and create and mark all of our pages dirty, we pin
  81. * everything to make sure our pages don't get evicted and screw up our
  82. * test.
  83. */
  84. for (index = 0; index < (total_dirty >> PAGE_CACHE_SHIFT); index++) {
  85. page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
  86. if (!page) {
  87. test_msg("Failed to allocate test page\n");
  88. ret = -ENOMEM;
  89. goto out;
  90. }
  91. SetPageDirty(page);
  92. if (index) {
  93. unlock_page(page);
  94. } else {
  95. page_cache_get(page);
  96. locked_page = page;
  97. }
  98. }
  99. /* Test this scenario
  100. * |--- delalloc ---|
  101. * |--- search ---|
  102. */
  103. set_extent_delalloc(&tmp, 0, 4095, NULL, GFP_NOFS);
  104. start = 0;
  105. end = 0;
  106. found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
  107. &end, max_bytes);
  108. if (!found) {
  109. test_msg("Should have found at least one delalloc\n");
  110. goto out_bits;
  111. }
  112. if (start != 0 || end != 4095) {
  113. test_msg("Expected start 0 end 4095, got start %Lu end %Lu\n",
  114. start, end);
  115. goto out_bits;
  116. }
  117. unlock_extent(&tmp, start, end);
  118. unlock_page(locked_page);
  119. page_cache_release(locked_page);
  120. /*
  121. * Test this scenario
  122. *
  123. * |--- delalloc ---|
  124. * |--- search ---|
  125. */
  126. test_start = 64 * 1024 * 1024;
  127. locked_page = find_lock_page(inode->i_mapping,
  128. test_start >> PAGE_CACHE_SHIFT);
  129. if (!locked_page) {
  130. test_msg("Couldn't find the locked page\n");
  131. goto out_bits;
  132. }
  133. set_extent_delalloc(&tmp, 4096, max_bytes - 1, NULL, GFP_NOFS);
  134. start = test_start;
  135. end = 0;
  136. found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
  137. &end, max_bytes);
  138. if (!found) {
  139. test_msg("Couldn't find delalloc in our range\n");
  140. goto out_bits;
  141. }
  142. if (start != test_start || end != max_bytes - 1) {
  143. test_msg("Expected start %Lu end %Lu, got start %Lu, end "
  144. "%Lu\n", test_start, max_bytes - 1, start, end);
  145. goto out_bits;
  146. }
  147. if (process_page_range(inode, start, end,
  148. PROCESS_TEST_LOCKED | PROCESS_UNLOCK)) {
  149. test_msg("There were unlocked pages in the range\n");
  150. goto out_bits;
  151. }
  152. unlock_extent(&tmp, start, end);
  153. /* locked_page was unlocked above */
  154. page_cache_release(locked_page);
  155. /*
  156. * Test this scenario
  157. * |--- delalloc ---|
  158. * |--- search ---|
  159. */
  160. test_start = max_bytes + 4096;
  161. locked_page = find_lock_page(inode->i_mapping, test_start >>
  162. PAGE_CACHE_SHIFT);
  163. if (!locked_page) {
  164. test_msg("Could'nt find the locked page\n");
  165. goto out_bits;
  166. }
  167. start = test_start;
  168. end = 0;
  169. found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
  170. &end, max_bytes);
  171. if (found) {
  172. test_msg("Found range when we shouldn't have\n");
  173. goto out_bits;
  174. }
  175. if (end != (u64)-1) {
  176. test_msg("Did not return the proper end offset\n");
  177. goto out_bits;
  178. }
  179. /*
  180. * Test this scenario
  181. * [------- delalloc -------|
  182. * [max_bytes]|-- search--|
  183. *
  184. * We are re-using our test_start from above since it works out well.
  185. */
  186. set_extent_delalloc(&tmp, max_bytes, total_dirty - 1, NULL, GFP_NOFS);
  187. start = test_start;
  188. end = 0;
  189. found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
  190. &end, max_bytes);
  191. if (!found) {
  192. test_msg("Didn't find our range\n");
  193. goto out_bits;
  194. }
  195. if (start != test_start || end != total_dirty - 1) {
  196. test_msg("Expected start %Lu end %Lu, got start %Lu end %Lu\n",
  197. test_start, total_dirty - 1, start, end);
  198. goto out_bits;
  199. }
  200. if (process_page_range(inode, start, end,
  201. PROCESS_TEST_LOCKED | PROCESS_UNLOCK)) {
  202. test_msg("Pages in range were not all locked\n");
  203. goto out_bits;
  204. }
  205. unlock_extent(&tmp, start, end);
  206. /*
  207. * Now to test where we run into a page that is no longer dirty in the
  208. * range we want to find.
  209. */
  210. page = find_get_page(inode->i_mapping, (max_bytes + (1 * 1024 * 1024))
  211. >> PAGE_CACHE_SHIFT);
  212. if (!page) {
  213. test_msg("Couldn't find our page\n");
  214. goto out_bits;
  215. }
  216. ClearPageDirty(page);
  217. page_cache_release(page);
  218. /* We unlocked it in the previous test */
  219. lock_page(locked_page);
  220. start = test_start;
  221. end = 0;
  222. /*
  223. * Currently if we fail to find dirty pages in the delalloc range we
  224. * will adjust max_bytes down to PAGE_CACHE_SIZE and then re-search. If
  225. * this changes at any point in the future we will need to fix this
  226. * tests expected behavior.
  227. */
  228. found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
  229. &end, max_bytes);
  230. if (!found) {
  231. test_msg("Didn't find our range\n");
  232. goto out_bits;
  233. }
  234. if (start != test_start && end != test_start + PAGE_CACHE_SIZE - 1) {
  235. test_msg("Expected start %Lu end %Lu, got start %Lu end %Lu\n",
  236. test_start, test_start + PAGE_CACHE_SIZE - 1, start,
  237. end);
  238. goto out_bits;
  239. }
  240. if (process_page_range(inode, start, end, PROCESS_TEST_LOCKED |
  241. PROCESS_UNLOCK)) {
  242. test_msg("Pages in range were not all locked\n");
  243. goto out_bits;
  244. }
  245. ret = 0;
  246. out_bits:
  247. clear_extent_bits(&tmp, 0, total_dirty - 1, (unsigned)-1, GFP_NOFS);
  248. out:
  249. if (locked_page)
  250. page_cache_release(locked_page);
  251. process_page_range(inode, 0, total_dirty - 1,
  252. PROCESS_UNLOCK | PROCESS_RELEASE);
  253. iput(inode);
  254. return ret;
  255. }
  256. int btrfs_test_extent_io(void)
  257. {
  258. test_msg("Running find delalloc tests\n");
  259. return test_find_delalloc();
  260. }