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