extent-buffer-tests.c 5.3 KB

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