extent-buffer-tests.c 6.1 KB

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