dm-btree-spine.c 5.7 KB

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