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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 229 int shadow_has_parent(struct shadow_spine *s) 230 { 231 return s->count >= 2; 232 } 233 234 dm_block_t shadow_root(struct shadow_spine *s) 235 { 236 return s->root; 237 } 238 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 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 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 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