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