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