extent-buffer-tests.c 5.9 KB

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