extent-buffer-tests.c 5.8 KB

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