extent-buffer-tests.c 5.4 KB

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