extent-buffer-tests.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/slab.h>
  19. #include "btrfs-tests.h"
  20. #include "../ctree.h"
  21. #include "../extent_io.h"
  22. #include "../disk-io.h"
  23. static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
  24. {
  25. struct btrfs_fs_info *fs_info;
  26. struct btrfs_path *path = NULL;
  27. struct btrfs_root *root = NULL;
  28. struct extent_buffer *eb;
  29. struct btrfs_item *item;
  30. char *value = "mary had a little lamb";
  31. char *split1 = "mary had a little";
  32. char *split2 = " lamb";
  33. char *split3 = "mary";
  34. char *split4 = " had a little";
  35. char buf[32];
  36. struct btrfs_key key;
  37. u32 value_len = strlen(value);
  38. int ret = 0;
  39. test_msg("Running btrfs_split_item tests\n");
  40. fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
  41. if (!fs_info) {
  42. test_msg("Could not allocate fs_info\n");
  43. return -ENOMEM;
  44. }
  45. root = btrfs_alloc_dummy_root(fs_info);
  46. if (IS_ERR(root)) {
  47. test_msg("Could not allocate root\n");
  48. ret = PTR_ERR(root);
  49. goto out;
  50. }
  51. path = btrfs_alloc_path();
  52. if (!path) {
  53. test_msg("Could not allocate path\n");
  54. ret = -ENOMEM;
  55. goto out;
  56. }
  57. path->nodes[0] = eb = alloc_dummy_extent_buffer(fs_info, nodesize);
  58. if (!eb) {
  59. test_msg("Could not allocate dummy buffer\n");
  60. ret = -ENOMEM;
  61. goto out;
  62. }
  63. path->slots[0] = 0;
  64. key.objectid = 0;
  65. key.type = BTRFS_EXTENT_CSUM_KEY;
  66. key.offset = 0;
  67. setup_items_for_insert(root, path, &key, &value_len, value_len,
  68. value_len + sizeof(struct btrfs_item), 1);
  69. item = btrfs_item_nr(0);
  70. write_extent_buffer(eb, value, btrfs_item_ptr_offset(eb, 0),
  71. value_len);
  72. key.offset = 3;
  73. /*
  74. * Passing NULL trans here should be safe because we have plenty of
  75. * space in this leaf to split the item without having to split the
  76. * leaf.
  77. */
  78. ret = btrfs_split_item(NULL, root, path, &key, 17);
  79. if (ret) {
  80. test_msg("Split item failed %d\n", ret);
  81. goto out;
  82. }
  83. /*
  84. * Read the first slot, it should have the original key and contain only
  85. * 'mary had a little'
  86. */
  87. btrfs_item_key_to_cpu(eb, &key, 0);
  88. if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
  89. key.offset != 0) {
  90. test_msg("Invalid key at slot 0\n");
  91. ret = -EINVAL;
  92. goto out;
  93. }
  94. item = btrfs_item_nr(0);
  95. if (btrfs_item_size(eb, item) != strlen(split1)) {
  96. test_msg("Invalid len in the first split\n");
  97. ret = -EINVAL;
  98. goto out;
  99. }
  100. read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0),
  101. strlen(split1));
  102. if (memcmp(buf, split1, strlen(split1))) {
  103. test_msg("Data in the buffer doesn't match what it should "
  104. "in the first split have='%.*s' want '%s'\n",
  105. (int)strlen(split1), buf, split1);
  106. ret = -EINVAL;
  107. goto out;
  108. }
  109. btrfs_item_key_to_cpu(eb, &key, 1);
  110. if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
  111. key.offset != 3) {
  112. test_msg("Invalid key at slot 1\n");
  113. ret = -EINVAL;
  114. goto out;
  115. }
  116. item = btrfs_item_nr(1);
  117. if (btrfs_item_size(eb, item) != strlen(split2)) {
  118. test_msg("Invalid len in the second split\n");
  119. ret = -EINVAL;
  120. goto out;
  121. }
  122. read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1),
  123. strlen(split2));
  124. if (memcmp(buf, split2, strlen(split2))) {
  125. test_msg("Data in the buffer doesn't match what it should "
  126. "in the second split\n");
  127. ret = -EINVAL;
  128. goto out;
  129. }
  130. key.offset = 1;
  131. /* Do it again so we test memmoving the other items in the leaf */
  132. ret = btrfs_split_item(NULL, root, path, &key, 4);
  133. if (ret) {
  134. test_msg("Second split item failed %d\n", ret);
  135. goto out;
  136. }
  137. btrfs_item_key_to_cpu(eb, &key, 0);
  138. if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
  139. key.offset != 0) {
  140. test_msg("Invalid key at slot 0\n");
  141. ret = -EINVAL;
  142. goto out;
  143. }
  144. item = btrfs_item_nr(0);
  145. if (btrfs_item_size(eb, item) != strlen(split3)) {
  146. test_msg("Invalid len in the first split\n");
  147. ret = -EINVAL;
  148. goto out;
  149. }
  150. read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0),
  151. strlen(split3));
  152. if (memcmp(buf, split3, strlen(split3))) {
  153. test_msg("Data in the buffer doesn't match what it should "
  154. "in the third split");
  155. ret = -EINVAL;
  156. goto out;
  157. }
  158. btrfs_item_key_to_cpu(eb, &key, 1);
  159. if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
  160. key.offset != 1) {
  161. test_msg("Invalid key at slot 1\n");
  162. ret = -EINVAL;
  163. goto out;
  164. }
  165. item = btrfs_item_nr(1);
  166. if (btrfs_item_size(eb, item) != strlen(split4)) {
  167. test_msg("Invalid len in the second split\n");
  168. ret = -EINVAL;
  169. goto out;
  170. }
  171. read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1),
  172. strlen(split4));
  173. if (memcmp(buf, split4, strlen(split4))) {
  174. test_msg("Data in the buffer doesn't match what it should "
  175. "in the fourth split\n");
  176. ret = -EINVAL;
  177. goto out;
  178. }
  179. btrfs_item_key_to_cpu(eb, &key, 2);
  180. if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
  181. key.offset != 3) {
  182. test_msg("Invalid key at slot 2\n");
  183. ret = -EINVAL;
  184. goto out;
  185. }
  186. item = btrfs_item_nr(2);
  187. if (btrfs_item_size(eb, item) != strlen(split2)) {
  188. test_msg("Invalid len in the second split\n");
  189. ret = -EINVAL;
  190. goto out;
  191. }
  192. read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 2),
  193. strlen(split2));
  194. if (memcmp(buf, split2, strlen(split2))) {
  195. test_msg("Data in the buffer doesn't match what it should "
  196. "in the last chunk\n");
  197. ret = -EINVAL;
  198. goto out;
  199. }
  200. out:
  201. btrfs_free_path(path);
  202. btrfs_free_dummy_root(root);
  203. btrfs_free_dummy_fs_info(fs_info);
  204. return ret;
  205. }
  206. int btrfs_test_extent_buffer_operations(u32 sectorsize, u32 nodesize)
  207. {
  208. test_msg("Running extent buffer operation tests\n");
  209. return test_btrfs_split_item(sectorsize, nodesize);
  210. }