2 * Copyright (C) 2011 Red Hat, Inc.
4 * This file is released under the GPL.
7 #include "dm-btree-internal.h"
8 #include "dm-transaction-manager.h"
10 #include <linux/device-mapper.h>
12 #define DM_MSG_PREFIX "btree spine"
14 /*----------------------------------------------------------------*/
16 #define BTREE_CSUM_XOR 121107
18 static void node_prepare_for_write(struct dm_block_validator *v,
22 struct btree_node *n = dm_block_data(b);
23 struct node_header *h = &n->header;
25 h->blocknr = cpu_to_le64(dm_block_location(b));
26 h->csum = cpu_to_le32(dm_bm_checksum(&h->flags,
27 block_size - sizeof(__le32),
31 static int node_check(struct dm_block_validator *v,
35 struct btree_node *n = dm_block_data(b);
36 struct node_header *h = &n->header;
39 uint32_t flags, nr_entries, max_entries;
41 if (dm_block_location(b) != le64_to_cpu(h->blocknr)) {
42 DMERR_LIMIT("node_check failed: blocknr %llu != wanted %llu",
43 le64_to_cpu(h->blocknr), dm_block_location(b));
47 csum_disk = cpu_to_le32(dm_bm_checksum(&h->flags,
48 block_size - sizeof(__le32),
50 if (csum_disk != h->csum) {
51 DMERR_LIMIT("node_check failed: csum %u != wanted %u",
52 le32_to_cpu(csum_disk), le32_to_cpu(h->csum));
56 nr_entries = le32_to_cpu(h->nr_entries);
57 max_entries = le32_to_cpu(h->max_entries);
58 value_size = le32_to_cpu(h->value_size);
60 if (sizeof(struct node_header) +
61 (sizeof(__le64) + value_size) * max_entries > block_size) {
62 DMERR_LIMIT("node_check failed: max_entries too large");
66 if (nr_entries > max_entries) {
67 DMERR_LIMIT("node_check failed: too many entries");
72 * The node must be either INTERNAL or LEAF.
74 flags = le32_to_cpu(h->flags);
75 if (!(flags & INTERNAL_NODE) && !(flags & LEAF_NODE)) {
76 DMERR_LIMIT("node_check failed: node is neither INTERNAL or LEAF");
83 struct dm_block_validator btree_node_validator = {
85 .prepare_for_write = node_prepare_for_write,
89 /*----------------------------------------------------------------*/
91 int bn_read_lock(struct dm_btree_info *info, dm_block_t b,
92 struct dm_block **result)
94 return dm_tm_read_lock(info->tm, b, &btree_node_validator, result);
97 static int bn_shadow(struct dm_btree_info *info, dm_block_t orig,
98 struct dm_btree_value_type *vt,
99 struct dm_block **result)
103 r = dm_tm_shadow_block(info->tm, orig, &btree_node_validator,
106 inc_children(info->tm, dm_block_data(*result), vt);
111 int new_block(struct dm_btree_info *info, struct dm_block **result)
113 return dm_tm_new_block(info->tm, &btree_node_validator, result);
116 void unlock_block(struct dm_btree_info *info, struct dm_block *b)
118 dm_tm_unlock(info->tm, b);
121 /*----------------------------------------------------------------*/
123 void init_ro_spine(struct ro_spine *s, struct dm_btree_info *info)
131 void exit_ro_spine(struct ro_spine *s)
135 for (i = 0; i < s->count; i++) {
136 unlock_block(s->info, s->nodes[i]);
140 int ro_step(struct ro_spine *s, dm_block_t new_child)
145 unlock_block(s->info, s->nodes[0]);
146 s->nodes[0] = s->nodes[1];
150 r = bn_read_lock(s->info, new_child, s->nodes + s->count);
157 void ro_pop(struct ro_spine *s)
161 unlock_block(s->info, s->nodes[s->count]);
164 struct btree_node *ro_node(struct ro_spine *s)
166 struct dm_block *block;
169 block = s->nodes[s->count - 1];
171 return dm_block_data(block);
174 /*----------------------------------------------------------------*/
176 void init_shadow_spine(struct shadow_spine *s, struct dm_btree_info *info)
182 void exit_shadow_spine(struct shadow_spine *s)
186 for (i = 0; i < s->count; i++) {
187 unlock_block(s->info, s->nodes[i]);
191 int shadow_step(struct shadow_spine *s, dm_block_t b,
192 struct dm_btree_value_type *vt)
197 unlock_block(s->info, s->nodes[0]);
198 s->nodes[0] = s->nodes[1];
202 r = bn_shadow(s->info, b, vt, s->nodes + s->count);
205 s->root = dm_block_location(s->nodes[0]);
213 struct dm_block *shadow_current(struct shadow_spine *s)
217 return s->nodes[s->count - 1];
220 struct dm_block *shadow_parent(struct shadow_spine *s)
222 BUG_ON(s->count != 2);
224 return s->count == 2 ? s->nodes[0] : NULL;
227 int shadow_has_parent(struct shadow_spine *s)
229 return s->count >= 2;
232 dm_block_t shadow_root(struct shadow_spine *s)
237 static void le64_inc(void *context, const void *value_le, unsigned count)
239 dm_tm_with_runs(context, value_le, count, dm_tm_inc_range);
242 static void le64_dec(void *context, const void *value_le, unsigned count)
244 dm_tm_with_runs(context, value_le, count, dm_tm_dec_range);
247 static int le64_equal(void *context, const void *value1_le, const void *value2_le)
251 memcpy(&v1_le, value1_le, sizeof(v1_le));
252 memcpy(&v2_le, value2_le, sizeof(v2_le));
253 return v1_le == v2_le;
256 void init_le64_type(struct dm_transaction_manager *tm,
257 struct dm_btree_value_type *vt)
260 vt->size = sizeof(__le64);
263 vt->equal = le64_equal;