Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Ceph - scalable distributed file system |
| 3 | * |
| 4 | * Copyright (C) 2015 Intel Corporation All Rights Reserved |
| 5 | * |
| 6 | * This is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License version 2.1, as published by the Free Software |
| 9 | * Foundation. See file COPYING. |
| 10 | * |
| 11 | */ |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 12 | |
| 13 | #ifdef __KERNEL__ |
| 14 | # include <linux/string.h> |
| 15 | # include <linux/slab.h> |
| 16 | # include <linux/bug.h> |
| 17 | # include <linux/kernel.h> |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 18 | # include <linux/crush/crush.h> |
| 19 | # include <linux/crush/hash.h> |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 20 | #else |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 21 | # include "crush_compat.h" |
| 22 | # include "crush.h" |
| 23 | # include "hash.h" |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 24 | #endif |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 25 | #include "crush_ln_table.h" |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 26 | |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 27 | #define dprintk(args...) /* printf(args) */ |
| 28 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 29 | /* |
| 30 | * Implement the core CRUSH mapping algorithm. |
| 31 | */ |
| 32 | |
| 33 | /** |
| 34 | * crush_find_rule - find a crush_rule id for a given ruleset, type, and size. |
| 35 | * @map: the crush_map |
| 36 | * @ruleset: the storage ruleset id (user defined) |
| 37 | * @type: storage ruleset type (user defined) |
| 38 | * @size: output set size |
| 39 | */ |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 40 | int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 41 | { |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 42 | __u32 i; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 43 | |
| 44 | for (i = 0; i < map->max_rules; i++) { |
| 45 | if (map->rules[i] && |
| 46 | map->rules[i]->mask.ruleset == ruleset && |
| 47 | map->rules[i]->mask.type == type && |
| 48 | map->rules[i]->mask.min_size <= size && |
| 49 | map->rules[i]->mask.max_size >= size) |
| 50 | return i; |
| 51 | } |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /* |
| 57 | * bucket choose methods |
| 58 | * |
| 59 | * For each bucket algorithm, we have a "choose" method that, given a |
| 60 | * crush input @x and replica position (usually, position in output set) @r, |
| 61 | * will produce an item in the bucket. |
| 62 | */ |
| 63 | |
| 64 | /* |
| 65 | * Choose based on a random permutation of the bucket. |
| 66 | * |
| 67 | * We used to use some prime number arithmetic to do this, but it |
| 68 | * wasn't very random, and had some other bad behaviors. Instead, we |
| 69 | * calculate an actual random permutation of the bucket members. |
| 70 | * Since this is expensive, we optimize for the r=0 case, which |
| 71 | * captures the vast majority of calls. |
| 72 | */ |
| 73 | static int bucket_perm_choose(struct crush_bucket *bucket, |
| 74 | int x, int r) |
| 75 | { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 76 | unsigned int pr = r % bucket->size; |
| 77 | unsigned int i, s; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 78 | |
| 79 | /* start a new permutation if @x has changed */ |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 80 | if (bucket->perm_x != (__u32)x || bucket->perm_n == 0) { |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 81 | dprintk("bucket %d new x=%d\n", bucket->id, x); |
| 82 | bucket->perm_x = x; |
| 83 | |
| 84 | /* optimize common r=0 case */ |
| 85 | if (pr == 0) { |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 86 | s = crush_hash32_3(bucket->hash, x, bucket->id, 0) % |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 87 | bucket->size; |
| 88 | bucket->perm[0] = s; |
| 89 | bucket->perm_n = 0xffff; /* magic value, see below */ |
| 90 | goto out; |
| 91 | } |
| 92 | |
| 93 | for (i = 0; i < bucket->size; i++) |
| 94 | bucket->perm[i] = i; |
| 95 | bucket->perm_n = 0; |
| 96 | } else if (bucket->perm_n == 0xffff) { |
| 97 | /* clean up after the r=0 case above */ |
| 98 | for (i = 1; i < bucket->size; i++) |
| 99 | bucket->perm[i] = i; |
| 100 | bucket->perm[bucket->perm[0]] = 0; |
| 101 | bucket->perm_n = 1; |
| 102 | } |
| 103 | |
| 104 | /* calculate permutation up to pr */ |
| 105 | for (i = 0; i < bucket->perm_n; i++) |
| 106 | dprintk(" perm_choose have %d: %d\n", i, bucket->perm[i]); |
| 107 | while (bucket->perm_n <= pr) { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 108 | unsigned int p = bucket->perm_n; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 109 | /* no point in swapping the final entry */ |
| 110 | if (p < bucket->size - 1) { |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 111 | i = crush_hash32_3(bucket->hash, x, bucket->id, p) % |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 112 | (bucket->size - p); |
| 113 | if (i) { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 114 | unsigned int t = bucket->perm[p + i]; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 115 | bucket->perm[p + i] = bucket->perm[p]; |
| 116 | bucket->perm[p] = t; |
| 117 | } |
| 118 | dprintk(" perm_choose swap %d with %d\n", p, p+i); |
| 119 | } |
| 120 | bucket->perm_n++; |
| 121 | } |
| 122 | for (i = 0; i < bucket->size; i++) |
| 123 | dprintk(" perm_choose %d: %d\n", i, bucket->perm[i]); |
| 124 | |
| 125 | s = bucket->perm[pr]; |
| 126 | out: |
| 127 | dprintk(" perm_choose %d sz=%d x=%d r=%d (%d) s=%d\n", bucket->id, |
| 128 | bucket->size, x, r, pr, s); |
| 129 | return bucket->items[s]; |
| 130 | } |
| 131 | |
| 132 | /* uniform */ |
| 133 | static int bucket_uniform_choose(struct crush_bucket_uniform *bucket, |
| 134 | int x, int r) |
| 135 | { |
| 136 | return bucket_perm_choose(&bucket->h, x, r); |
| 137 | } |
| 138 | |
| 139 | /* list */ |
| 140 | static int bucket_list_choose(struct crush_bucket_list *bucket, |
| 141 | int x, int r) |
| 142 | { |
| 143 | int i; |
| 144 | |
| 145 | for (i = bucket->h.size-1; i >= 0; i--) { |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 146 | __u64 w = crush_hash32_4(bucket->h.hash, x, bucket->h.items[i], |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 147 | r, bucket->h.id); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 148 | w &= 0xffff; |
| 149 | dprintk("list_choose i=%d x=%d r=%d item %d weight %x " |
| 150 | "sw %x rand %llx", |
| 151 | i, x, r, bucket->h.items[i], bucket->item_weights[i], |
| 152 | bucket->sum_weights[i], w); |
| 153 | w *= bucket->sum_weights[i]; |
| 154 | w = w >> 16; |
| 155 | /*dprintk(" scaled %llx\n", w);*/ |
| 156 | if (w < bucket->item_weights[i]) |
| 157 | return bucket->h.items[i]; |
| 158 | } |
| 159 | |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 160 | dprintk("bad list sums for bucket %d\n", bucket->h.id); |
| 161 | return bucket->h.items[0]; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
| 165 | /* (binary) tree */ |
| 166 | static int height(int n) |
| 167 | { |
| 168 | int h = 0; |
| 169 | while ((n & 1) == 0) { |
| 170 | h++; |
| 171 | n = n >> 1; |
| 172 | } |
| 173 | return h; |
| 174 | } |
| 175 | |
| 176 | static int left(int x) |
| 177 | { |
| 178 | int h = height(x); |
| 179 | return x - (1 << (h-1)); |
| 180 | } |
| 181 | |
| 182 | static int right(int x) |
| 183 | { |
| 184 | int h = height(x); |
| 185 | return x + (1 << (h-1)); |
| 186 | } |
| 187 | |
| 188 | static int terminal(int x) |
| 189 | { |
| 190 | return x & 1; |
| 191 | } |
| 192 | |
| 193 | static int bucket_tree_choose(struct crush_bucket_tree *bucket, |
| 194 | int x, int r) |
| 195 | { |
Ilya Dryomov | 8f99c85 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 196 | int n; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 197 | __u32 w; |
| 198 | __u64 t; |
| 199 | |
| 200 | /* start at root */ |
| 201 | n = bucket->num_nodes >> 1; |
| 202 | |
| 203 | while (!terminal(n)) { |
Ilya Dryomov | 8f99c85 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 204 | int l; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 205 | /* pick point in [0, w) */ |
| 206 | w = bucket->node_weights[n]; |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 207 | t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r, |
| 208 | bucket->h.id) * (__u64)w; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 209 | t = t >> 32; |
| 210 | |
| 211 | /* descend to the left or right? */ |
| 212 | l = left(n); |
| 213 | if (t < bucket->node_weights[l]) |
| 214 | n = l; |
| 215 | else |
| 216 | n = right(n); |
| 217 | } |
| 218 | |
| 219 | return bucket->h.items[n >> 1]; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /* straw */ |
| 224 | |
| 225 | static int bucket_straw_choose(struct crush_bucket_straw *bucket, |
| 226 | int x, int r) |
| 227 | { |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 228 | __u32 i; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 229 | int high = 0; |
| 230 | __u64 high_draw = 0; |
| 231 | __u64 draw; |
| 232 | |
| 233 | for (i = 0; i < bucket->h.size; i++) { |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 234 | draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 235 | draw &= 0xffff; |
| 236 | draw *= bucket->straws[i]; |
| 237 | if (i == 0 || draw > high_draw) { |
| 238 | high = i; |
| 239 | high_draw = draw; |
| 240 | } |
| 241 | } |
| 242 | return bucket->h.items[high]; |
| 243 | } |
| 244 | |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 245 | /* compute 2^44*log2(input+1) */ |
| 246 | static __u64 crush_ln(unsigned int xin) |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 247 | { |
Ilya Dryomov | 64f7756 | 2016-09-27 12:35:55 +0200 | [diff] [blame] | 248 | unsigned int x = xin; |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 249 | int iexpon, index1, index2; |
| 250 | __u64 RH, LH, LL, xl64, result; |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 251 | |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 252 | x++; |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 253 | |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 254 | /* normalize input */ |
| 255 | iexpon = 15; |
Ilya Dryomov | 74a52938 | 2016-09-27 12:30:09 +0200 | [diff] [blame] | 256 | |
| 257 | /* |
| 258 | * figure out number of bits we need to shift and |
| 259 | * do it in one step instead of iteratively |
| 260 | */ |
| 261 | if (!(x & 0x18000)) { |
| 262 | int bits = __builtin_clz(x & 0x1FFFF) - 16; |
| 263 | x <<= bits; |
| 264 | iexpon = 15 - bits; |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 265 | } |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 266 | |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 267 | index1 = (x >> 8) << 1; |
| 268 | /* RH ~ 2^56/index1 */ |
| 269 | RH = __RH_LH_tbl[index1 - 256]; |
| 270 | /* LH ~ 2^48 * log2(index1/256) */ |
| 271 | LH = __RH_LH_tbl[index1 + 1 - 256]; |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 272 | |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 273 | /* RH*x ~ 2^48 * (2^15 + xf), xf<2^8 */ |
| 274 | xl64 = (__s64)x * RH; |
| 275 | xl64 >>= 48; |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 276 | |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 277 | result = iexpon; |
| 278 | result <<= (12 + 32); |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 279 | |
Ilya Dryomov | 64f7756 | 2016-09-27 12:35:55 +0200 | [diff] [blame] | 280 | index2 = xl64 & 0xff; |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 281 | /* LL ~ 2^48*log2(1.0+index2/2^15) */ |
| 282 | LL = __LL_tbl[index2]; |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 283 | |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 284 | LH = LH + LL; |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 285 | |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 286 | LH >>= (48 - 12 - 32); |
| 287 | result += LH; |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 288 | |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 289 | return result; |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | |
| 293 | /* |
| 294 | * straw2 |
| 295 | * |
| 296 | * for reference, see: |
| 297 | * |
| 298 | * http://en.wikipedia.org/wiki/Exponential_distribution#Distribution_of_the_minimum_of_exponential_random_variables |
| 299 | * |
| 300 | */ |
| 301 | |
| 302 | static int bucket_straw2_choose(struct crush_bucket_straw2 *bucket, |
| 303 | int x, int r) |
| 304 | { |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 305 | unsigned int i, high = 0; |
| 306 | unsigned int u; |
| 307 | unsigned int w; |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 308 | __s64 ln, draw, high_draw = 0; |
| 309 | |
| 310 | for (i = 0; i < bucket->h.size; i++) { |
| 311 | w = bucket->item_weights[i]; |
| 312 | if (w) { |
| 313 | u = crush_hash32_3(bucket->h.hash, x, |
| 314 | bucket->h.items[i], r); |
| 315 | u &= 0xffff; |
| 316 | |
| 317 | /* |
| 318 | * for some reason slightly less than 0x10000 produces |
| 319 | * a slightly more accurate distribution... probably a |
| 320 | * rounding effect. |
| 321 | * |
| 322 | * the natural log lookup table maps [0,0xffff] |
| 323 | * (corresponding to real numbers [1/0x10000, 1] to |
| 324 | * [0, 0xffffffffffff] (corresponding to real numbers |
| 325 | * [-11.090355,0]). |
| 326 | */ |
| 327 | ln = crush_ln(u) - 0x1000000000000ll; |
| 328 | |
| 329 | /* |
| 330 | * divide by 16.16 fixed-point weight. note |
| 331 | * that the ln value is negative, so a larger |
| 332 | * weight means a larger (less negative) value |
| 333 | * for draw. |
| 334 | */ |
| 335 | draw = div64_s64(ln, w); |
| 336 | } else { |
| 337 | draw = S64_MIN; |
| 338 | } |
| 339 | |
| 340 | if (i == 0 || draw > high_draw) { |
| 341 | high = i; |
| 342 | high_draw = draw; |
| 343 | } |
| 344 | } |
| 345 | return bucket->h.items[high]; |
| 346 | } |
| 347 | |
| 348 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 349 | static int crush_bucket_choose(struct crush_bucket *in, int x, int r) |
| 350 | { |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 351 | dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r); |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 352 | BUG_ON(in->size == 0); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 353 | switch (in->alg) { |
| 354 | case CRUSH_BUCKET_UNIFORM: |
| 355 | return bucket_uniform_choose((struct crush_bucket_uniform *)in, |
| 356 | x, r); |
| 357 | case CRUSH_BUCKET_LIST: |
| 358 | return bucket_list_choose((struct crush_bucket_list *)in, |
| 359 | x, r); |
| 360 | case CRUSH_BUCKET_TREE: |
| 361 | return bucket_tree_choose((struct crush_bucket_tree *)in, |
| 362 | x, r); |
| 363 | case CRUSH_BUCKET_STRAW: |
| 364 | return bucket_straw_choose((struct crush_bucket_straw *)in, |
| 365 | x, r); |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 366 | case CRUSH_BUCKET_STRAW2: |
| 367 | return bucket_straw2_choose((struct crush_bucket_straw2 *)in, |
| 368 | x, r); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 369 | default: |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 370 | dprintk("unknown bucket %d alg %d\n", in->id, in->alg); |
Sage Weil | 50b885b | 2009-12-01 14:12:07 -0800 | [diff] [blame] | 371 | return in->items[0]; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 375 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 376 | /* |
| 377 | * true if device is marked "out" (failed, fully offloaded) |
| 378 | * of the cluster |
| 379 | */ |
Ilya Dryomov | b3b33b0 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 380 | static int is_out(const struct crush_map *map, |
| 381 | const __u32 *weight, int weight_max, |
| 382 | int item, int x) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 383 | { |
Ilya Dryomov | b3b33b0 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 384 | if (item >= weight_max) |
| 385 | return 1; |
Sage Weil | 153a109 | 2010-07-05 09:44:17 -0700 | [diff] [blame] | 386 | if (weight[item] >= 0x10000) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 387 | return 0; |
| 388 | if (weight[item] == 0) |
| 389 | return 1; |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 390 | if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff) |
| 391 | < weight[item]) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 392 | return 0; |
| 393 | return 1; |
| 394 | } |
| 395 | |
| 396 | /** |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 397 | * crush_choose_firstn - choose numrep distinct items of given type |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 398 | * @map: the crush_map |
| 399 | * @bucket: the bucket we are choose an item from |
| 400 | * @x: crush input value |
| 401 | * @numrep: the number of items to choose |
| 402 | * @type: the type of item to choose |
| 403 | * @out: pointer to output vector |
| 404 | * @outpos: our position in that vector |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 405 | * @out_size: size of the out vector |
Ilya Dryomov | 0e32d71 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 406 | * @tries: number of attempts to make |
| 407 | * @recurse_tries: number of attempts to have recursive chooseleaf make |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 408 | * @local_retries: localized retries |
| 409 | * @local_fallback_retries: localized fallback retries |
Ilya Dryomov | 0e32d71 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 410 | * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose) |
Ilya Dryomov | dc6ae6d | 2016-01-31 14:36:07 +0100 | [diff] [blame] | 411 | * @stable: stable mode starts rep=0 in the recursive call for all replicas |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 412 | * @vary_r: pass r to recursive calls |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 413 | * @out2: second output vector for leaf items (if @recurse_to_leaf) |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 414 | * @parent_r: r value passed from the parent |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 415 | */ |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 416 | static int crush_choose_firstn(const struct crush_map *map, |
| 417 | struct crush_bucket *bucket, |
| 418 | const __u32 *weight, int weight_max, |
| 419 | int x, int numrep, int type, |
| 420 | int *out, int outpos, |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 421 | int out_size, |
Ilya Dryomov | 2d8be0b | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 422 | unsigned int tries, |
| 423 | unsigned int recurse_tries, |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 424 | unsigned int local_retries, |
| 425 | unsigned int local_fallback_retries, |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 426 | int recurse_to_leaf, |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 427 | unsigned int vary_r, |
Ilya Dryomov | dc6ae6d | 2016-01-31 14:36:07 +0100 | [diff] [blame] | 428 | unsigned int stable, |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 429 | int *out2, |
| 430 | int parent_r) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 431 | { |
| 432 | int rep; |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 433 | unsigned int ftotal, flocal; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 434 | int retry_descent, retry_bucket, skip_rep; |
| 435 | struct crush_bucket *in = bucket; |
| 436 | int r; |
| 437 | int i; |
Sage Weil | b28813a | 2009-10-07 10:59:34 -0700 | [diff] [blame] | 438 | int item = 0; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 439 | int itemtype; |
| 440 | int collide, reject; |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 441 | int count = out_size; |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 442 | |
Ilya Dryomov | dc6ae6d | 2016-01-31 14:36:07 +0100 | [diff] [blame] | 443 | dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d tries %d recurse_tries %d local_retries %d local_fallback_retries %d parent_r %d stable %d\n", |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 444 | recurse_to_leaf ? "_LEAF" : "", |
| 445 | bucket->id, x, outpos, numrep, |
| 446 | tries, recurse_tries, local_retries, local_fallback_retries, |
Ilya Dryomov | dc6ae6d | 2016-01-31 14:36:07 +0100 | [diff] [blame] | 447 | parent_r, stable); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 448 | |
Ilya Dryomov | dc6ae6d | 2016-01-31 14:36:07 +0100 | [diff] [blame] | 449 | for (rep = stable ? 0 : outpos; rep < numrep && count > 0 ; rep++) { |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 450 | /* keep trying until we get a non-out, non-colliding item */ |
| 451 | ftotal = 0; |
| 452 | skip_rep = 0; |
| 453 | do { |
| 454 | retry_descent = 0; |
| 455 | in = bucket; /* initial bucket */ |
| 456 | |
| 457 | /* choose through intervening buckets */ |
| 458 | flocal = 0; |
| 459 | do { |
Sage Weil | b28813a | 2009-10-07 10:59:34 -0700 | [diff] [blame] | 460 | collide = 0; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 461 | retry_bucket = 0; |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 462 | r = rep + parent_r; |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 463 | /* r' = r + f_total */ |
| 464 | r += ftotal; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 465 | |
| 466 | /* bucket choose */ |
Sage Weil | b28813a | 2009-10-07 10:59:34 -0700 | [diff] [blame] | 467 | if (in->size == 0) { |
| 468 | reject = 1; |
| 469 | goto reject; |
| 470 | } |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 471 | if (local_fallback_retries > 0 && |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 472 | flocal >= (in->size>>1) && |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 473 | flocal > local_fallback_retries) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 474 | item = bucket_perm_choose(in, x, r); |
| 475 | else |
| 476 | item = crush_bucket_choose(in, x, r); |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 477 | if (item >= map->max_devices) { |
| 478 | dprintk(" bad item %d\n", item); |
| 479 | skip_rep = 1; |
| 480 | break; |
| 481 | } |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 482 | |
| 483 | /* desired type? */ |
| 484 | if (item < 0) |
| 485 | itemtype = map->buckets[-1-item]->type; |
| 486 | else |
| 487 | itemtype = 0; |
| 488 | dprintk(" item %d type %d\n", item, itemtype); |
| 489 | |
| 490 | /* keep going? */ |
| 491 | if (itemtype != type) { |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 492 | if (item >= 0 || |
| 493 | (-1-item) >= map->max_buckets) { |
| 494 | dprintk(" bad item type %d\n", type); |
| 495 | skip_rep = 1; |
| 496 | break; |
| 497 | } |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 498 | in = map->buckets[-1-item]; |
Sage Weil | 55bda7a | 2010-06-24 12:55:48 -0700 | [diff] [blame] | 499 | retry_bucket = 1; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 500 | continue; |
| 501 | } |
| 502 | |
| 503 | /* collision? */ |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 504 | for (i = 0; i < outpos; i++) { |
| 505 | if (out[i] == item) { |
| 506 | collide = 1; |
| 507 | break; |
| 508 | } |
| 509 | } |
| 510 | |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 511 | reject = 0; |
Sage Weil | 7d7c1f6 | 2013-01-15 18:49:09 -0800 | [diff] [blame] | 512 | if (!collide && recurse_to_leaf) { |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 513 | if (item < 0) { |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 514 | int sub_r; |
| 515 | if (vary_r) |
| 516 | sub_r = r >> (vary_r-1); |
| 517 | else |
| 518 | sub_r = 0; |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 519 | if (crush_choose_firstn(map, |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 520 | map->buckets[-1-item], |
Ilya Dryomov | b3b33b0 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 521 | weight, weight_max, |
Ilya Dryomov | dc6ae6d | 2016-01-31 14:36:07 +0100 | [diff] [blame] | 522 | x, stable ? 1 : outpos+1, 0, |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 523 | out2, outpos, count, |
Ilya Dryomov | 2d8be0b | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 524 | recurse_tries, 0, |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 525 | local_retries, |
| 526 | local_fallback_retries, |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 527 | 0, |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 528 | vary_r, |
Ilya Dryomov | dc6ae6d | 2016-01-31 14:36:07 +0100 | [diff] [blame] | 529 | stable, |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 530 | NULL, |
| 531 | sub_r) <= outpos) |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 532 | /* didn't get leaf */ |
| 533 | reject = 1; |
| 534 | } else { |
| 535 | /* we already have a leaf! */ |
| 536 | out2[outpos] = item; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | if (!reject) { |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 541 | /* out? */ |
| 542 | if (itemtype == 0) |
| 543 | reject = is_out(map, weight, |
Ilya Dryomov | b3b33b0 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 544 | weight_max, |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 545 | item, x); |
| 546 | else |
| 547 | reject = 0; |
| 548 | } |
| 549 | |
Sage Weil | b28813a | 2009-10-07 10:59:34 -0700 | [diff] [blame] | 550 | reject: |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 551 | if (reject || collide) { |
| 552 | ftotal++; |
| 553 | flocal++; |
| 554 | |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 555 | if (collide && flocal <= local_retries) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 556 | /* retry locally a few times */ |
| 557 | retry_bucket = 1; |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 558 | else if (local_fallback_retries > 0 && |
| 559 | flocal <= in->size + local_fallback_retries) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 560 | /* exhaustive bucket search */ |
| 561 | retry_bucket = 1; |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 562 | else if (ftotal < tries) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 563 | /* then retry descent */ |
| 564 | retry_descent = 1; |
| 565 | else |
| 566 | /* else give up */ |
| 567 | skip_rep = 1; |
| 568 | dprintk(" reject %d collide %d " |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 569 | "ftotal %u flocal %u\n", |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 570 | reject, collide, ftotal, |
| 571 | flocal); |
| 572 | } |
| 573 | } while (retry_bucket); |
| 574 | } while (retry_descent); |
| 575 | |
| 576 | if (skip_rep) { |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 577 | dprintk("skip rep\n"); |
| 578 | continue; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 581 | dprintk("CHOOSE got %d\n", item); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 582 | out[outpos] = item; |
| 583 | outpos++; |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 584 | count--; |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 585 | #ifndef __KERNEL__ |
| 586 | if (map->choose_tries && ftotal <= map->choose_total_tries) |
| 587 | map->choose_tries[ftotal]++; |
| 588 | #endif |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 591 | dprintk("CHOOSE returns %d\n", outpos); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 592 | return outpos; |
| 593 | } |
| 594 | |
| 595 | |
| 596 | /** |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 597 | * crush_choose_indep: alternative breadth-first positionally stable mapping |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 598 | * |
| 599 | */ |
| 600 | static void crush_choose_indep(const struct crush_map *map, |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 601 | struct crush_bucket *bucket, |
| 602 | const __u32 *weight, int weight_max, |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 603 | int x, int left, int numrep, int type, |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 604 | int *out, int outpos, |
Ilya Dryomov | 2d8be0b | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 605 | unsigned int tries, |
| 606 | unsigned int recurse_tries, |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 607 | int recurse_to_leaf, |
Ilya Dryomov | 4158608 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 608 | int *out2, |
| 609 | int parent_r) |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 610 | { |
| 611 | struct crush_bucket *in = bucket; |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 612 | int endpos = outpos + left; |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 613 | int rep; |
| 614 | unsigned int ftotal; |
| 615 | int r; |
| 616 | int i; |
| 617 | int item = 0; |
| 618 | int itemtype; |
| 619 | int collide; |
| 620 | |
| 621 | dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "", |
| 622 | bucket->id, x, outpos, numrep); |
| 623 | |
| 624 | /* initially my result is undefined */ |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 625 | for (rep = outpos; rep < endpos; rep++) { |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 626 | out[rep] = CRUSH_ITEM_UNDEF; |
| 627 | if (out2) |
| 628 | out2[rep] = CRUSH_ITEM_UNDEF; |
| 629 | } |
| 630 | |
Ilya Dryomov | 2d8be0b | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 631 | for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) { |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 632 | #ifdef DEBUG_INDEP |
| 633 | if (out2 && ftotal) { |
| 634 | dprintk("%u %d a: ", ftotal, left); |
| 635 | for (rep = outpos; rep < endpos; rep++) { |
| 636 | dprintk(" %d", out[rep]); |
| 637 | } |
| 638 | dprintk("\n"); |
| 639 | dprintk("%u %d b: ", ftotal, left); |
| 640 | for (rep = outpos; rep < endpos; rep++) { |
| 641 | dprintk(" %d", out2[rep]); |
| 642 | } |
| 643 | dprintk("\n"); |
| 644 | } |
| 645 | #endif |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 646 | for (rep = outpos; rep < endpos; rep++) { |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 647 | if (out[rep] != CRUSH_ITEM_UNDEF) |
| 648 | continue; |
| 649 | |
| 650 | in = bucket; /* initial bucket */ |
| 651 | |
| 652 | /* choose through intervening buckets */ |
| 653 | for (;;) { |
Ilya Dryomov | 3102b0a | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 654 | /* note: we base the choice on the position |
| 655 | * even in the nested call. that means that |
| 656 | * if the first layer chooses the same bucket |
| 657 | * in a different position, we will tend to |
| 658 | * choose a different item in that bucket. |
| 659 | * this will involve more devices in data |
| 660 | * movement and tend to distribute the load. |
| 661 | */ |
Ilya Dryomov | 4158608 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 662 | r = rep + parent_r; |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 663 | |
| 664 | /* be careful */ |
| 665 | if (in->alg == CRUSH_BUCKET_UNIFORM && |
| 666 | in->size % numrep == 0) |
| 667 | /* r'=r+(n+1)*f_total */ |
| 668 | r += (numrep+1) * ftotal; |
| 669 | else |
| 670 | /* r' = r + n*f_total */ |
| 671 | r += numrep * ftotal; |
| 672 | |
| 673 | /* bucket choose */ |
| 674 | if (in->size == 0) { |
| 675 | dprintk(" empty bucket\n"); |
| 676 | break; |
| 677 | } |
| 678 | |
| 679 | item = crush_bucket_choose(in, x, r); |
| 680 | if (item >= map->max_devices) { |
| 681 | dprintk(" bad item %d\n", item); |
| 682 | out[rep] = CRUSH_ITEM_NONE; |
| 683 | if (out2) |
| 684 | out2[rep] = CRUSH_ITEM_NONE; |
| 685 | left--; |
| 686 | break; |
| 687 | } |
| 688 | |
| 689 | /* desired type? */ |
| 690 | if (item < 0) |
| 691 | itemtype = map->buckets[-1-item]->type; |
| 692 | else |
| 693 | itemtype = 0; |
| 694 | dprintk(" item %d type %d\n", item, itemtype); |
| 695 | |
| 696 | /* keep going? */ |
| 697 | if (itemtype != type) { |
| 698 | if (item >= 0 || |
| 699 | (-1-item) >= map->max_buckets) { |
| 700 | dprintk(" bad item type %d\n", type); |
| 701 | out[rep] = CRUSH_ITEM_NONE; |
| 702 | if (out2) |
| 703 | out2[rep] = |
| 704 | CRUSH_ITEM_NONE; |
| 705 | left--; |
| 706 | break; |
| 707 | } |
| 708 | in = map->buckets[-1-item]; |
| 709 | continue; |
| 710 | } |
| 711 | |
| 712 | /* collision? */ |
| 713 | collide = 0; |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 714 | for (i = outpos; i < endpos; i++) { |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 715 | if (out[i] == item) { |
| 716 | collide = 1; |
| 717 | break; |
| 718 | } |
| 719 | } |
| 720 | if (collide) |
| 721 | break; |
| 722 | |
| 723 | if (recurse_to_leaf) { |
| 724 | if (item < 0) { |
| 725 | crush_choose_indep(map, |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 726 | map->buckets[-1-item], |
| 727 | weight, weight_max, |
| 728 | x, 1, numrep, 0, |
| 729 | out2, rep, |
Ilya Dryomov | 2d8be0b | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 730 | recurse_tries, 0, |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 731 | 0, NULL, r); |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 732 | if (out2[rep] == CRUSH_ITEM_NONE) { |
| 733 | /* placed nothing; no leaf */ |
| 734 | break; |
| 735 | } |
| 736 | } else { |
| 737 | /* we already have a leaf! */ |
| 738 | out2[rep] = item; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | /* out? */ |
| 743 | if (itemtype == 0 && |
| 744 | is_out(map, weight, weight_max, item, x)) |
| 745 | break; |
| 746 | |
| 747 | /* yay! */ |
| 748 | out[rep] = item; |
| 749 | left--; |
| 750 | break; |
| 751 | } |
| 752 | } |
| 753 | } |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 754 | for (rep = outpos; rep < endpos; rep++) { |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 755 | if (out[rep] == CRUSH_ITEM_UNDEF) { |
| 756 | out[rep] = CRUSH_ITEM_NONE; |
| 757 | } |
| 758 | if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) { |
| 759 | out2[rep] = CRUSH_ITEM_NONE; |
| 760 | } |
| 761 | } |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 762 | #ifndef __KERNEL__ |
| 763 | if (map->choose_tries && ftotal <= map->choose_total_tries) |
| 764 | map->choose_tries[ftotal]++; |
| 765 | #endif |
| 766 | #ifdef DEBUG_INDEP |
| 767 | if (out2) { |
| 768 | dprintk("%u %d a: ", ftotal, left); |
| 769 | for (rep = outpos; rep < endpos; rep++) { |
| 770 | dprintk(" %d", out[rep]); |
| 771 | } |
| 772 | dprintk("\n"); |
| 773 | dprintk("%u %d b: ", ftotal, left); |
| 774 | for (rep = outpos; rep < endpos; rep++) { |
| 775 | dprintk(" %d", out2[rep]); |
| 776 | } |
| 777 | dprintk("\n"); |
| 778 | } |
| 779 | #endif |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /** |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 783 | * crush_do_rule - calculate a mapping with the given input and rule |
| 784 | * @map: the crush_map |
| 785 | * @ruleno: the rule id |
| 786 | * @x: hash input |
| 787 | * @result: pointer to result vector |
| 788 | * @result_max: maximum result size |
Ilya Dryomov | b3b33b0 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 789 | * @weight: weight vector (for map leaves) |
| 790 | * @weight_max: size of weight vector |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 791 | * @scratch: scratch vector for private use; must be >= 3 * result_max |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 792 | */ |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 793 | int crush_do_rule(const struct crush_map *map, |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 794 | int ruleno, int x, int *result, int result_max, |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 795 | const __u32 *weight, int weight_max, |
| 796 | int *scratch) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 797 | { |
| 798 | int result_len; |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 799 | int *a = scratch; |
| 800 | int *b = scratch + result_max; |
| 801 | int *c = scratch + result_max*2; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 802 | int recurse_to_leaf; |
| 803 | int *w; |
| 804 | int wsize = 0; |
| 805 | int *o; |
| 806 | int osize; |
| 807 | int *tmp; |
| 808 | struct crush_rule *rule; |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 809 | __u32 step; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 810 | int i, j; |
| 811 | int numrep; |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 812 | int out_size; |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 813 | /* |
| 814 | * the original choose_total_tries value was off by one (it |
| 815 | * counted "retries" and not "tries"). add one. |
| 816 | */ |
| 817 | int choose_tries = map->choose_total_tries + 1; |
Ilya Dryomov | f18650a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 818 | int choose_leaf_tries = 0; |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 819 | /* |
| 820 | * the local tries values were counted as "retries", though, |
| 821 | * and need no adjustment |
| 822 | */ |
| 823 | int choose_local_retries = map->choose_local_tries; |
| 824 | int choose_local_fallback_retries = map->choose_local_fallback_tries; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 825 | |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 826 | int vary_r = map->chooseleaf_vary_r; |
Ilya Dryomov | dc6ae6d | 2016-01-31 14:36:07 +0100 | [diff] [blame] | 827 | int stable = map->chooseleaf_stable; |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 828 | |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 829 | if ((__u32)ruleno >= map->max_rules) { |
| 830 | dprintk(" bad ruleno %d\n", ruleno); |
| 831 | return 0; |
| 832 | } |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 833 | |
| 834 | rule = map->rules[ruleno]; |
| 835 | result_len = 0; |
| 836 | w = a; |
| 837 | o = b; |
| 838 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 839 | for (step = 0; step < rule->len; step++) { |
Ilya Dryomov | 8f99c85 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 840 | int firstn = 0; |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 841 | struct crush_rule_step *curstep = &rule->steps[step]; |
| 842 | |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 843 | switch (curstep->op) { |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 844 | case CRUSH_RULE_TAKE: |
Ilya Dryomov | 8f52979 | 2015-06-12 11:20:03 +0300 | [diff] [blame] | 845 | if ((curstep->arg1 >= 0 && |
| 846 | curstep->arg1 < map->max_devices) || |
Ilya Dryomov | 56a4f30 | 2016-01-31 14:36:05 +0100 | [diff] [blame] | 847 | (-1-curstep->arg1 >= 0 && |
| 848 | -1-curstep->arg1 < map->max_buckets && |
Ilya Dryomov | 8f52979 | 2015-06-12 11:20:03 +0300 | [diff] [blame] | 849 | map->buckets[-1-curstep->arg1])) { |
| 850 | w[0] = curstep->arg1; |
| 851 | wsize = 1; |
| 852 | } else { |
| 853 | dprintk(" bad take value %d\n", curstep->arg1); |
| 854 | } |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 855 | break; |
| 856 | |
Ilya Dryomov | cc10df4 | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 857 | case CRUSH_RULE_SET_CHOOSE_TRIES: |
| 858 | if (curstep->arg1 > 0) |
| 859 | choose_tries = curstep->arg1; |
| 860 | break; |
| 861 | |
Ilya Dryomov | 917edad | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 862 | case CRUSH_RULE_SET_CHOOSELEAF_TRIES: |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 863 | if (curstep->arg1 > 0) |
| 864 | choose_leaf_tries = curstep->arg1; |
| 865 | break; |
| 866 | |
Ilya Dryomov | f046bf9 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 867 | case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES: |
Ilya Dryomov | 6ed1002 | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 868 | if (curstep->arg1 >= 0) |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 869 | choose_local_retries = curstep->arg1; |
Ilya Dryomov | f046bf9 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 870 | break; |
| 871 | |
| 872 | case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES: |
Ilya Dryomov | 6ed1002 | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 873 | if (curstep->arg1 >= 0) |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 874 | choose_local_fallback_retries = curstep->arg1; |
Ilya Dryomov | f046bf9 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 875 | break; |
| 876 | |
Ilya Dryomov | d83ed85 | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 877 | case CRUSH_RULE_SET_CHOOSELEAF_VARY_R: |
| 878 | if (curstep->arg1 >= 0) |
| 879 | vary_r = curstep->arg1; |
| 880 | break; |
| 881 | |
Ilya Dryomov | dc6ae6d | 2016-01-31 14:36:07 +0100 | [diff] [blame] | 882 | case CRUSH_RULE_SET_CHOOSELEAF_STABLE: |
| 883 | if (curstep->arg1 >= 0) |
| 884 | stable = curstep->arg1; |
| 885 | break; |
| 886 | |
Ilya Dryomov | 917edad | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 887 | case CRUSH_RULE_CHOOSELEAF_FIRSTN: |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 888 | case CRUSH_RULE_CHOOSE_FIRSTN: |
| 889 | firstn = 1; |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 890 | /* fall through */ |
Ilya Dryomov | 917edad | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 891 | case CRUSH_RULE_CHOOSELEAF_INDEP: |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 892 | case CRUSH_RULE_CHOOSE_INDEP: |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 893 | if (wsize == 0) |
| 894 | break; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 895 | |
| 896 | recurse_to_leaf = |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 897 | curstep->op == |
Ilya Dryomov | 917edad | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 898 | CRUSH_RULE_CHOOSELEAF_FIRSTN || |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 899 | curstep->op == |
Ilya Dryomov | 917edad | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 900 | CRUSH_RULE_CHOOSELEAF_INDEP; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 901 | |
| 902 | /* reset output */ |
| 903 | osize = 0; |
| 904 | |
| 905 | for (i = 0; i < wsize; i++) { |
Ilya Dryomov | f224a69 | 2016-01-31 14:35:59 +0100 | [diff] [blame] | 906 | int bno; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 907 | /* |
| 908 | * see CRUSH_N, CRUSH_N_MINUS macros. |
| 909 | * basically, numrep <= 0 means relative to |
| 910 | * the provided result_max |
| 911 | */ |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 912 | numrep = curstep->arg1; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 913 | if (numrep <= 0) { |
| 914 | numrep += result_max; |
| 915 | if (numrep <= 0) |
| 916 | continue; |
| 917 | } |
| 918 | j = 0; |
Ilya Dryomov | f224a69 | 2016-01-31 14:35:59 +0100 | [diff] [blame] | 919 | /* make sure bucket id is valid */ |
| 920 | bno = -1 - w[i]; |
| 921 | if (bno < 0 || bno >= map->max_buckets) { |
| 922 | /* w[i] is probably CRUSH_ITEM_NONE */ |
| 923 | dprintk(" bad w[i] %d\n", w[i]); |
| 924 | continue; |
| 925 | } |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 926 | if (firstn) { |
Ilya Dryomov | d390bb2 | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 927 | int recurse_tries; |
| 928 | if (choose_leaf_tries) |
| 929 | recurse_tries = |
| 930 | choose_leaf_tries; |
| 931 | else if (map->chooseleaf_descend_once) |
| 932 | recurse_tries = 1; |
| 933 | else |
| 934 | recurse_tries = choose_tries; |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 935 | osize += crush_choose_firstn( |
| 936 | map, |
Ilya Dryomov | f224a69 | 2016-01-31 14:35:59 +0100 | [diff] [blame] | 937 | map->buckets[bno], |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 938 | weight, weight_max, |
| 939 | x, numrep, |
| 940 | curstep->arg2, |
| 941 | o+osize, j, |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 942 | result_max-osize, |
Ilya Dryomov | f18650a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 943 | choose_tries, |
Ilya Dryomov | d390bb2 | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 944 | recurse_tries, |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 945 | choose_local_retries, |
| 946 | choose_local_fallback_retries, |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 947 | recurse_to_leaf, |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 948 | vary_r, |
Ilya Dryomov | dc6ae6d | 2016-01-31 14:36:07 +0100 | [diff] [blame] | 949 | stable, |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 950 | c+osize, |
| 951 | 0); |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 952 | } else { |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 953 | out_size = ((numrep < (result_max-osize)) ? |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 954 | numrep : (result_max-osize)); |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 955 | crush_choose_indep( |
| 956 | map, |
Ilya Dryomov | f224a69 | 2016-01-31 14:35:59 +0100 | [diff] [blame] | 957 | map->buckets[bno], |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 958 | weight, weight_max, |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 959 | x, out_size, numrep, |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 960 | curstep->arg2, |
| 961 | o+osize, j, |
Ilya Dryomov | f18650a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 962 | choose_tries, |
Ilya Dryomov | d390bb2 | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 963 | choose_leaf_tries ? |
| 964 | choose_leaf_tries : 1, |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 965 | recurse_to_leaf, |
Ilya Dryomov | 4158608 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 966 | c+osize, |
| 967 | 0); |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 968 | osize += out_size; |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 969 | } |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | if (recurse_to_leaf) |
| 973 | /* copy final _leaf_ values to output set */ |
| 974 | memcpy(o, c, osize*sizeof(*o)); |
| 975 | |
Ilya Dryomov | 2a4ba74 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 976 | /* swap o and w arrays */ |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 977 | tmp = o; |
| 978 | o = w; |
| 979 | w = tmp; |
| 980 | wsize = osize; |
| 981 | break; |
| 982 | |
| 983 | |
| 984 | case CRUSH_RULE_EMIT: |
| 985 | for (i = 0; i < wsize && result_len < result_max; i++) { |
| 986 | result[result_len] = w[i]; |
| 987 | result_len++; |
| 988 | } |
| 989 | wsize = 0; |
| 990 | break; |
| 991 | |
| 992 | default: |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 993 | dprintk(" unknown op %d at step %d\n", |
| 994 | curstep->op, step); |
| 995 | break; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 996 | } |
| 997 | } |
Sage Weil | f1932fc | 2011-12-07 09:10:26 -0800 | [diff] [blame] | 998 | return result_len; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 999 | } |