dm-btree-spine.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (C) 2011 Red Hat, Inc.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm-btree-internal.h"
  7. #include "dm-transaction-manager.h"
  8. #include <linux/device-mapper.h>
  9. #define DM_MSG_PREFIX "btree spine"
  10. /*----------------------------------------------------------------*/
  11. #define BTREE_CSUM_XOR 121107
  12. static void node_prepare_for_write(struct dm_block_validator *v,
  13. struct dm_block *b,
  14. size_t block_size)
  15. {
  16. struct btree_node *n = dm_block_data(b);
  17. struct node_header *h = &n->header;
  18. h->blocknr = cpu_to_le64(dm_block_location(b));
  19. h->csum = cpu_to_le32(dm_bm_checksum(&h->flags,
  20. block_size - sizeof(__le32),
  21. BTREE_CSUM_XOR));
  22. }
  23. static int node_check(struct dm_block_validator *v,
  24. struct dm_block *b,
  25. size_t block_size)
  26. {
  27. struct btree_node *n = dm_block_data(b);
  28. struct node_header *h = &n->header;
  29. size_t value_size;
  30. __le32 csum_disk;
  31. uint32_t flags, nr_entries, max_entries;
  32. if (dm_block_location(b) != le64_to_cpu(h->blocknr)) {
  33. DMERR_LIMIT("node_check failed: blocknr %llu != wanted %llu",
  34. le64_to_cpu(h->blocknr), dm_block_location(b));
  35. return -ENOTBLK;
  36. }
  37. csum_disk = cpu_to_le32(dm_bm_checksum(&h->flags,
  38. block_size - sizeof(__le32),
  39. BTREE_CSUM_XOR));
  40. if (csum_disk != h->csum) {
  41. DMERR_LIMIT("node_check failed: csum %u != wanted %u",
  42. le32_to_cpu(csum_disk), le32_to_cpu(h->csum));
  43. return -EILSEQ;
  44. }
  45. nr_entries = le32_to_cpu(h->nr_entries);
  46. max_entries = le32_to_cpu(h->max_entries);
  47. value_size = le32_to_cpu(h->value_size);
  48. if (sizeof(struct node_header) +
  49. (sizeof(__le64) + value_size) * max_entries > block_size) {
  50. DMERR_LIMIT("node_check failed: max_entries too large");
  51. return -EILSEQ;
  52. }
  53. if (nr_entries > max_entries) {
  54. DMERR_LIMIT("node_check failed: too many entries");
  55. return -EILSEQ;
  56. }
  57. /*
  58. * The node must be either INTERNAL or LEAF.
  59. */
  60. flags = le32_to_cpu(h->flags);
  61. if (!(flags & INTERNAL_NODE) && !(flags & LEAF_NODE)) {
  62. DMERR_LIMIT("node_check failed: node is neither INTERNAL or LEAF");
  63. return -EILSEQ;
  64. }
  65. return 0;
  66. }
  67. struct dm_block_validator btree_node_validator = {
  68. .name = "btree_node",
  69. .prepare_for_write = node_prepare_for_write,
  70. .check = node_check
  71. };
  72. /*----------------------------------------------------------------*/
  73. int bn_read_lock(struct dm_btree_info *info, dm_block_t b,
  74. struct dm_block **result)
  75. {
  76. return dm_tm_read_lock(info->tm, b, &btree_node_validator, result);
  77. }
  78. static int bn_shadow(struct dm_btree_info *info, dm_block_t orig,
  79. struct dm_btree_value_type *vt,
  80. struct dm_block **result)
  81. {
  82. int r, inc;
  83. r = dm_tm_shadow_block(info->tm, orig, &btree_node_validator,
  84. result, &inc);
  85. if (!r && inc)
  86. inc_children(info->tm, dm_block_data(*result), vt);
  87. return r;
  88. }
  89. int new_block(struct dm_btree_info *info, struct dm_block **result)
  90. {
  91. return dm_tm_new_block(info->tm, &btree_node_validator, result);
  92. }
  93. void unlock_block(struct dm_btree_info *info, struct dm_block *b)
  94. {
  95. dm_tm_unlock(info->tm, b);
  96. }
  97. /*----------------------------------------------------------------*/
  98. void init_ro_spine(struct ro_spine *s, struct dm_btree_info *info)
  99. {
  100. s->info = info;
  101. s->count = 0;
  102. s->nodes[0] = NULL;
  103. s->nodes[1] = NULL;
  104. }
  105. void exit_ro_spine(struct ro_spine *s)
  106. {
  107. int i;
  108. for (i = 0; i < s->count; i++) {
  109. unlock_block(s->info, s->nodes[i]);
  110. }
  111. }
  112. int ro_step(struct ro_spine *s, dm_block_t new_child)
  113. {
  114. int r;
  115. if (s->count == 2) {
  116. unlock_block(s->info, s->nodes[0]);
  117. s->nodes[0] = s->nodes[1];
  118. s->count--;
  119. }
  120. r = bn_read_lock(s->info, new_child, s->nodes + s->count);
  121. if (!r)
  122. s->count++;
  123. return r;
  124. }
  125. void ro_pop(struct ro_spine *s)
  126. {
  127. BUG_ON(!s->count);
  128. --s->count;
  129. unlock_block(s->info, s->nodes[s->count]);
  130. }
  131. struct btree_node *ro_node(struct ro_spine *s)
  132. {
  133. struct dm_block *block;
  134. BUG_ON(!s->count);
  135. block = s->nodes[s->count - 1];
  136. return dm_block_data(block);
  137. }
  138. /*----------------------------------------------------------------*/
  139. void init_shadow_spine(struct shadow_spine *s, struct dm_btree_info *info)
  140. {
  141. s->info = info;
  142. s->count = 0;
  143. }
  144. void exit_shadow_spine(struct shadow_spine *s)
  145. {
  146. int i;
  147. for (i = 0; i < s->count; i++) {
  148. unlock_block(s->info, s->nodes[i]);
  149. }
  150. }
  151. int shadow_step(struct shadow_spine *s, dm_block_t b,
  152. struct dm_btree_value_type *vt)
  153. {
  154. int r;
  155. if (s->count == 2) {
  156. unlock_block(s->info, s->nodes[0]);
  157. s->nodes[0] = s->nodes[1];
  158. s->count--;
  159. }
  160. r = bn_shadow(s->info, b, vt, s->nodes + s->count);
  161. if (!r) {
  162. if (!s->count)
  163. s->root = dm_block_location(s->nodes[0]);
  164. s->count++;
  165. }
  166. return r;
  167. }
  168. struct dm_block *shadow_current(struct shadow_spine *s)
  169. {
  170. BUG_ON(!s->count);
  171. return s->nodes[s->count - 1];
  172. }
  173. struct dm_block *shadow_parent(struct shadow_spine *s)
  174. {
  175. BUG_ON(s->count != 2);
  176. return s->count == 2 ? s->nodes[0] : NULL;
  177. }
  178. int shadow_has_parent(struct shadow_spine *s)
  179. {
  180. return s->count >= 2;
  181. }
  182. dm_block_t shadow_root(struct shadow_spine *s)
  183. {
  184. return s->root;
  185. }
  186. static void le64_inc(void *context, const void *value_le, unsigned int count)
  187. {
  188. dm_tm_with_runs(context, value_le, count, dm_tm_inc_range);
  189. }
  190. static void le64_dec(void *context, const void *value_le, unsigned int count)
  191. {
  192. dm_tm_with_runs(context, value_le, count, dm_tm_dec_range);
  193. }
  194. static int le64_equal(void *context, const void *value1_le, const void *value2_le)
  195. {
  196. __le64 v1_le, v2_le;
  197. memcpy(&v1_le, value1_le, sizeof(v1_le));
  198. memcpy(&v2_le, value2_le, sizeof(v2_le));
  199. return v1_le == v2_le;
  200. }
  201. void init_le64_type(struct dm_transaction_manager *tm,
  202. struct dm_btree_value_type *vt)
  203. {
  204. vt->context = tm;
  205. vt->size = sizeof(__le64);
  206. vt->inc = le64_inc;
  207. vt->dec = le64_dec;
  208. vt->equal = le64_equal;
  209. }