blob: b5cd8c21bfdfbf4d85bd93993f1f02807c5200be [file] [log] [blame]
Ilya Dryomovb459be72015-06-12 13:21:07 +03001/*
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 Weil5ecc0a02009-10-06 11:31:11 -070012
13#ifdef __KERNEL__
14# include <linux/string.h>
15# include <linux/slab.h>
16# include <linux/bug.h>
17# include <linux/kernel.h>
Ilya Dryomovb459be72015-06-12 13:21:07 +030018# include <linux/crush/crush.h>
19# include <linux/crush/hash.h>
Tobias Klauserf6c0d1a2016-10-28 13:23:24 +020020# include <linux/crush/mapper.h>
Sage Weil5ecc0a02009-10-06 11:31:11 -070021#else
Ilya Dryomovb459be72015-06-12 13:21:07 +030022# include "crush_compat.h"
23# include "crush.h"
24# include "hash.h"
Tobias Klauserf6c0d1a2016-10-28 13:23:24 +020025# include "mapper.h"
Sage Weil5ecc0a02009-10-06 11:31:11 -070026#endif
Ilya Dryomov958a2762015-04-14 16:54:52 +030027#include "crush_ln_table.h"
Sage Weil5ecc0a02009-10-06 11:31:11 -070028
Ilya Dryomovb459be72015-06-12 13:21:07 +030029#define dprintk(args...) /* printf(args) */
30
Sage Weil5ecc0a02009-10-06 11:31:11 -070031/*
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 */
Sage Weil8b12d472012-05-07 15:38:35 -070042int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
Sage Weil5ecc0a02009-10-06 11:31:11 -070043{
Sage Weil8b12d472012-05-07 15:38:35 -070044 __u32 i;
Sage Weil5ecc0a02009-10-06 11:31:11 -070045
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
Sage Weil5ecc0a02009-10-06 11:31:11 -070057/*
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 */
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +010074static int bucket_perm_choose(const struct crush_bucket *bucket,
75 struct crush_work_bucket *work,
Sage Weil5ecc0a02009-10-06 11:31:11 -070076 int x, int r)
77{
Eric Dumazet95c96172012-04-15 05:58:06 +000078 unsigned int pr = r % bucket->size;
79 unsigned int i, s;
Sage Weil5ecc0a02009-10-06 11:31:11 -070080
81 /* start a new permutation if @x has changed */
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +010082 if (work->perm_x != (__u32)x || work->perm_n == 0) {
Sage Weil5ecc0a02009-10-06 11:31:11 -070083 dprintk("bucket %d new x=%d\n", bucket->id, x);
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +010084 work->perm_x = x;
Sage Weil5ecc0a02009-10-06 11:31:11 -070085
86 /* optimize common r=0 case */
87 if (pr == 0) {
Sage Weilfb690392009-11-07 20:18:22 -080088 s = crush_hash32_3(bucket->hash, x, bucket->id, 0) %
Sage Weil5ecc0a02009-10-06 11:31:11 -070089 bucket->size;
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +010090 work->perm[0] = s;
91 work->perm_n = 0xffff; /* magic value, see below */
Sage Weil5ecc0a02009-10-06 11:31:11 -070092 goto out;
93 }
94
95 for (i = 0; i < bucket->size; i++)
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +010096 work->perm[i] = i;
97 work->perm_n = 0;
98 } else if (work->perm_n == 0xffff) {
Sage Weil5ecc0a02009-10-06 11:31:11 -070099 /* clean up after the r=0 case above */
100 for (i = 1; i < bucket->size; i++)
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100101 work->perm[i] = i;
102 work->perm[work->perm[0]] = 0;
103 work->perm_n = 1;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700104 }
105
106 /* calculate permutation up to pr */
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100107 for (i = 0; i < work->perm_n; i++)
Ilya Dryomov7ba04872017-02-16 15:38:05 +0100108 dprintk(" perm_choose have %d: %d\n", i, work->perm[i]);
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100109 while (work->perm_n <= pr) {
110 unsigned int p = work->perm_n;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700111 /* no point in swapping the final entry */
112 if (p < bucket->size - 1) {
Sage Weilfb690392009-11-07 20:18:22 -0800113 i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
Sage Weil5ecc0a02009-10-06 11:31:11 -0700114 (bucket->size - p);
115 if (i) {
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100116 unsigned int t = work->perm[p + i];
117 work->perm[p + i] = work->perm[p];
118 work->perm[p] = t;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700119 }
120 dprintk(" perm_choose swap %d with %d\n", p, p+i);
121 }
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100122 work->perm_n++;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700123 }
124 for (i = 0; i < bucket->size; i++)
Ilya Dryomov7ba04872017-02-16 15:38:05 +0100125 dprintk(" perm_choose %d: %d\n", i, work->perm[i]);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700126
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100127 s = work->perm[pr];
Sage Weil5ecc0a02009-10-06 11:31:11 -0700128out:
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 */
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100135static int bucket_uniform_choose(const struct crush_bucket_uniform *bucket,
136 struct crush_work_bucket *work, int x, int r)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700137{
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100138 return bucket_perm_choose(&bucket->h, work, x, r);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700139}
140
141/* list */
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100142static int bucket_list_choose(const struct crush_bucket_list *bucket,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700143 int x, int r)
144{
145 int i;
146
147 for (i = bucket->h.size-1; i >= 0; i--) {
Ilya Dryomovb459be72015-06-12 13:21:07 +0300148 __u64 w = crush_hash32_4(bucket->h.hash, x, bucket->h.items[i],
Sage Weilfb690392009-11-07 20:18:22 -0800149 r, bucket->h.id);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700150 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);*/
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100158 if (w < bucket->item_weights[i]) {
Sage Weil5ecc0a02009-10-06 11:31:11 -0700159 return bucket->h.items[i];
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100160 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700161 }
162
Sage Weila1f48952012-05-07 15:35:24 -0700163 dprintk("bad list sums for bucket %d\n", bucket->h.id);
164 return bucket->h.items[0];
Sage Weil5ecc0a02009-10-06 11:31:11 -0700165}
166
167
168/* (binary) tree */
169static 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
179static int left(int x)
180{
181 int h = height(x);
182 return x - (1 << (h-1));
183}
184
185static int right(int x)
186{
187 int h = height(x);
188 return x + (1 << (h-1));
189}
190
191static int terminal(int x)
192{
193 return x & 1;
194}
195
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100196static int bucket_tree_choose(const struct crush_bucket_tree *bucket,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700197 int x, int r)
198{
Ilya Dryomov8f99c852013-12-24 21:19:24 +0200199 int n;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700200 __u32 w;
201 __u64 t;
202
203 /* start at root */
204 n = bucket->num_nodes >> 1;
205
206 while (!terminal(n)) {
Ilya Dryomov8f99c852013-12-24 21:19:24 +0200207 int l;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700208 /* pick point in [0, w) */
209 w = bucket->node_weights[n];
Sage Weilfb690392009-11-07 20:18:22 -0800210 t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r,
211 bucket->h.id) * (__u64)w;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700212 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
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100228static int bucket_straw_choose(const struct crush_bucket_straw *bucket,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700229 int x, int r)
230{
Sage Weil8b12d472012-05-07 15:38:35 -0700231 __u32 i;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700232 int high = 0;
233 __u64 high_draw = 0;
234 __u64 draw;
235
236 for (i = 0; i < bucket->h.size; i++) {
Sage Weilfb690392009-11-07 20:18:22 -0800237 draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700238 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
Ilya Dryomovb459be72015-06-12 13:21:07 +0300248/* compute 2^44*log2(input+1) */
249static __u64 crush_ln(unsigned int xin)
Ilya Dryomov958a2762015-04-14 16:54:52 +0300250{
Ilya Dryomov64f77562016-09-27 12:35:55 +0200251 unsigned int x = xin;
Ilya Dryomovb459be72015-06-12 13:21:07 +0300252 int iexpon, index1, index2;
253 __u64 RH, LH, LL, xl64, result;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300254
Ilya Dryomovb459be72015-06-12 13:21:07 +0300255 x++;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300256
Ilya Dryomovb459be72015-06-12 13:21:07 +0300257 /* normalize input */
258 iexpon = 15;
Ilya Dryomov74a529382016-09-27 12:30:09 +0200259
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;
Ilya Dryomovb459be72015-06-12 13:21:07 +0300268 }
Ilya Dryomov958a2762015-04-14 16:54:52 +0300269
Ilya Dryomovb459be72015-06-12 13:21:07 +0300270 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];
Ilya Dryomov958a2762015-04-14 16:54:52 +0300275
Ilya Dryomovb459be72015-06-12 13:21:07 +0300276 /* RH*x ~ 2^48 * (2^15 + xf), xf<2^8 */
277 xl64 = (__s64)x * RH;
278 xl64 >>= 48;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300279
Ilya Dryomovb459be72015-06-12 13:21:07 +0300280 result = iexpon;
281 result <<= (12 + 32);
Ilya Dryomov958a2762015-04-14 16:54:52 +0300282
Ilya Dryomov64f77562016-09-27 12:35:55 +0200283 index2 = xl64 & 0xff;
Ilya Dryomovb459be72015-06-12 13:21:07 +0300284 /* LL ~ 2^48*log2(1.0+index2/2^15) */
285 LL = __LL_tbl[index2];
Ilya Dryomov958a2762015-04-14 16:54:52 +0300286
Ilya Dryomovb459be72015-06-12 13:21:07 +0300287 LH = LH + LL;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300288
Ilya Dryomovb459be72015-06-12 13:21:07 +0300289 LH >>= (48 - 12 - 32);
290 result += LH;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300291
Ilya Dryomovb459be72015-06-12 13:21:07 +0300292 return result;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300293}
294
295
296/*
297 * straw2
298 *
299 * for reference, see:
300 *
301 * http://en.wikipedia.org/wiki/Exponential_distribution#Distribution_of_the_minimum_of_exponential_random_variables
302 *
303 */
304
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100305static int bucket_straw2_choose(const struct crush_bucket_straw2 *bucket,
Ilya Dryomov958a2762015-04-14 16:54:52 +0300306 int x, int r)
307{
Ilya Dryomovb459be72015-06-12 13:21:07 +0300308 unsigned int i, high = 0;
309 unsigned int u;
310 unsigned int w;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300311 __s64 ln, draw, high_draw = 0;
312
313 for (i = 0; i < bucket->h.size; i++) {
314 w = bucket->item_weights[i];
315 if (w) {
316 u = crush_hash32_3(bucket->h.hash, x,
317 bucket->h.items[i], r);
318 u &= 0xffff;
319
320 /*
321 * for some reason slightly less than 0x10000 produces
322 * a slightly more accurate distribution... probably a
323 * rounding effect.
324 *
325 * the natural log lookup table maps [0,0xffff]
326 * (corresponding to real numbers [1/0x10000, 1] to
327 * [0, 0xffffffffffff] (corresponding to real numbers
328 * [-11.090355,0]).
329 */
330 ln = crush_ln(u) - 0x1000000000000ll;
331
332 /*
333 * divide by 16.16 fixed-point weight. note
334 * that the ln value is negative, so a larger
335 * weight means a larger (less negative) value
336 * for draw.
337 */
338 draw = div64_s64(ln, w);
339 } else {
340 draw = S64_MIN;
341 }
342
343 if (i == 0 || draw > high_draw) {
344 high = i;
345 high_draw = draw;
346 }
347 }
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100348
Ilya Dryomov958a2762015-04-14 16:54:52 +0300349 return bucket->h.items[high];
350}
351
352
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100353static int crush_bucket_choose(const struct crush_bucket *in,
354 struct crush_work_bucket *work,
355 int x, int r)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700356{
Sage Weila1a31e72010-06-24 12:58:14 -0700357 dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
Sage Weila1f48952012-05-07 15:35:24 -0700358 BUG_ON(in->size == 0);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700359 switch (in->alg) {
360 case CRUSH_BUCKET_UNIFORM:
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100361 return bucket_uniform_choose(
362 (const struct crush_bucket_uniform *)in,
363 work, x, r);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700364 case CRUSH_BUCKET_LIST:
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100365 return bucket_list_choose((const struct crush_bucket_list *)in,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700366 x, r);
367 case CRUSH_BUCKET_TREE:
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100368 return bucket_tree_choose((const struct crush_bucket_tree *)in,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700369 x, r);
370 case CRUSH_BUCKET_STRAW:
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100371 return bucket_straw_choose(
372 (const struct crush_bucket_straw *)in,
373 x, r);
Ilya Dryomov958a2762015-04-14 16:54:52 +0300374 case CRUSH_BUCKET_STRAW2:
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100375 return bucket_straw2_choose(
376 (const struct crush_bucket_straw2 *)in,
377 x, r);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700378 default:
Sage Weila1f48952012-05-07 15:35:24 -0700379 dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
Sage Weil50b885b2009-12-01 14:12:07 -0800380 return in->items[0];
Sage Weil5ecc0a02009-10-06 11:31:11 -0700381 }
382}
383
384/*
385 * true if device is marked "out" (failed, fully offloaded)
386 * of the cluster
387 */
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200388static int is_out(const struct crush_map *map,
389 const __u32 *weight, int weight_max,
390 int item, int x)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700391{
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200392 if (item >= weight_max)
393 return 1;
Sage Weil153a1092010-07-05 09:44:17 -0700394 if (weight[item] >= 0x10000)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700395 return 0;
396 if (weight[item] == 0)
397 return 1;
Sage Weilfb690392009-11-07 20:18:22 -0800398 if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
399 < weight[item])
Sage Weil5ecc0a02009-10-06 11:31:11 -0700400 return 0;
401 return 1;
402}
403
404/**
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200405 * crush_choose_firstn - choose numrep distinct items of given type
Sage Weil5ecc0a02009-10-06 11:31:11 -0700406 * @map: the crush_map
407 * @bucket: the bucket we are choose an item from
408 * @x: crush input value
409 * @numrep: the number of items to choose
410 * @type: the type of item to choose
411 * @out: pointer to output vector
412 * @outpos: our position in that vector
Ilya Dryomov45002262015-04-14 16:04:23 +0300413 * @out_size: size of the out vector
Ilya Dryomov0e32d712013-12-24 21:19:27 +0200414 * @tries: number of attempts to make
415 * @recurse_tries: number of attempts to have recursive chooseleaf make
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200416 * @local_retries: localized retries
417 * @local_fallback_retries: localized fallback retries
Ilya Dryomov0e32d712013-12-24 21:19:27 +0200418 * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose)
Ilya Dryomovdc6ae6d2016-01-31 14:36:07 +0100419 * @stable: stable mode starts rep=0 in the recursive call for all replicas
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200420 * @vary_r: pass r to recursive calls
Sage Weil5ecc0a02009-10-06 11:31:11 -0700421 * @out2: second output vector for leaf items (if @recurse_to_leaf)
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200422 * @parent_r: r value passed from the parent
Sage Weil5ecc0a02009-10-06 11:31:11 -0700423 */
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200424static int crush_choose_firstn(const struct crush_map *map,
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100425 struct crush_work *work,
426 const struct crush_bucket *bucket,
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200427 const __u32 *weight, int weight_max,
428 int x, int numrep, int type,
429 int *out, int outpos,
Ilya Dryomov45002262015-04-14 16:04:23 +0300430 int out_size,
Ilya Dryomov2d8be0b2013-12-24 21:19:27 +0200431 unsigned int tries,
432 unsigned int recurse_tries,
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200433 unsigned int local_retries,
434 unsigned int local_fallback_retries,
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200435 int recurse_to_leaf,
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200436 unsigned int vary_r,
Ilya Dryomovdc6ae6d2016-01-31 14:36:07 +0100437 unsigned int stable,
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200438 int *out2,
439 int parent_r)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700440{
441 int rep;
Sage Weil8b12d472012-05-07 15:38:35 -0700442 unsigned int ftotal, flocal;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700443 int retry_descent, retry_bucket, skip_rep;
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100444 const struct crush_bucket *in = bucket;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700445 int r;
446 int i;
Sage Weilb28813a2009-10-07 10:59:34 -0700447 int item = 0;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700448 int itemtype;
449 int collide, reject;
Ilya Dryomov45002262015-04-14 16:04:23 +0300450 int count = out_size;
Sage Weila1a31e72010-06-24 12:58:14 -0700451
Ilya Dryomovdc6ae6d2016-01-31 14:36:07 +0100452 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 Dryomove2b149c2014-03-19 16:58:37 +0200453 recurse_to_leaf ? "_LEAF" : "",
454 bucket->id, x, outpos, numrep,
455 tries, recurse_tries, local_retries, local_fallback_retries,
Ilya Dryomovdc6ae6d2016-01-31 14:36:07 +0100456 parent_r, stable);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700457
Ilya Dryomovdc6ae6d2016-01-31 14:36:07 +0100458 for (rep = stable ? 0 : outpos; rep < numrep && count > 0 ; rep++) {
Sage Weil5ecc0a02009-10-06 11:31:11 -0700459 /* keep trying until we get a non-out, non-colliding item */
460 ftotal = 0;
461 skip_rep = 0;
462 do {
463 retry_descent = 0;
464 in = bucket; /* initial bucket */
465
466 /* choose through intervening buckets */
467 flocal = 0;
468 do {
Sage Weilb28813a2009-10-07 10:59:34 -0700469 collide = 0;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700470 retry_bucket = 0;
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200471 r = rep + parent_r;
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200472 /* r' = r + f_total */
473 r += ftotal;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700474
475 /* bucket choose */
Sage Weilb28813a2009-10-07 10:59:34 -0700476 if (in->size == 0) {
477 reject = 1;
478 goto reject;
479 }
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200480 if (local_fallback_retries > 0 &&
Sage Weil546f04e2012-07-30 18:15:23 -0700481 flocal >= (in->size>>1) &&
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200482 flocal > local_fallback_retries)
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100483 item = bucket_perm_choose(
484 in, work->work[-1-in->id],
485 x, r);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700486 else
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100487 item = crush_bucket_choose(
488 in, work->work[-1-in->id],
489 x, r);
Sage Weila1f48952012-05-07 15:35:24 -0700490 if (item >= map->max_devices) {
491 dprintk(" bad item %d\n", item);
492 skip_rep = 1;
493 break;
494 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700495
496 /* desired type? */
497 if (item < 0)
498 itemtype = map->buckets[-1-item]->type;
499 else
500 itemtype = 0;
501 dprintk(" item %d type %d\n", item, itemtype);
502
503 /* keep going? */
504 if (itemtype != type) {
Sage Weila1f48952012-05-07 15:35:24 -0700505 if (item >= 0 ||
506 (-1-item) >= map->max_buckets) {
507 dprintk(" bad item type %d\n", type);
508 skip_rep = 1;
509 break;
510 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700511 in = map->buckets[-1-item];
Sage Weil55bda7a2010-06-24 12:55:48 -0700512 retry_bucket = 1;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700513 continue;
514 }
515
516 /* collision? */
Sage Weil5ecc0a02009-10-06 11:31:11 -0700517 for (i = 0; i < outpos; i++) {
518 if (out[i] == item) {
519 collide = 1;
520 break;
521 }
522 }
523
Sage Weila1a31e72010-06-24 12:58:14 -0700524 reject = 0;
Sage Weil7d7c1f62013-01-15 18:49:09 -0800525 if (!collide && recurse_to_leaf) {
Sage Weila1a31e72010-06-24 12:58:14 -0700526 if (item < 0) {
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200527 int sub_r;
528 if (vary_r)
529 sub_r = r >> (vary_r-1);
530 else
531 sub_r = 0;
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100532 if (crush_choose_firstn(
533 map,
534 work,
535 map->buckets[-1-item],
536 weight, weight_max,
537 x, stable ? 1 : outpos+1, 0,
538 out2, outpos, count,
539 recurse_tries, 0,
540 local_retries,
541 local_fallback_retries,
542 0,
543 vary_r,
544 stable,
545 NULL,
546 sub_r) <= outpos)
Sage Weila1a31e72010-06-24 12:58:14 -0700547 /* didn't get leaf */
548 reject = 1;
549 } else {
550 /* we already have a leaf! */
551 out2[outpos] = item;
552 }
553 }
554
Ilya Dryomov98ba6af2017-02-16 15:21:15 +0100555 if (!reject && !collide) {
Sage Weil5ecc0a02009-10-06 11:31:11 -0700556 /* out? */
557 if (itemtype == 0)
558 reject = is_out(map, weight,
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200559 weight_max,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700560 item, x);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700561 }
562
Sage Weilb28813a2009-10-07 10:59:34 -0700563reject:
Sage Weil5ecc0a02009-10-06 11:31:11 -0700564 if (reject || collide) {
565 ftotal++;
566 flocal++;
567
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200568 if (collide && flocal <= local_retries)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700569 /* retry locally a few times */
570 retry_bucket = 1;
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200571 else if (local_fallback_retries > 0 &&
572 flocal <= in->size + local_fallback_retries)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700573 /* exhaustive bucket search */
574 retry_bucket = 1;
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200575 else if (ftotal < tries)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700576 /* then retry descent */
577 retry_descent = 1;
578 else
579 /* else give up */
580 skip_rep = 1;
581 dprintk(" reject %d collide %d "
Sage Weil8b12d472012-05-07 15:38:35 -0700582 "ftotal %u flocal %u\n",
Sage Weil5ecc0a02009-10-06 11:31:11 -0700583 reject, collide, ftotal,
584 flocal);
585 }
586 } while (retry_bucket);
587 } while (retry_descent);
588
589 if (skip_rep) {
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200590 dprintk("skip rep\n");
591 continue;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700592 }
593
Sage Weila1a31e72010-06-24 12:58:14 -0700594 dprintk("CHOOSE got %d\n", item);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700595 out[outpos] = item;
596 outpos++;
Ilya Dryomov45002262015-04-14 16:04:23 +0300597 count--;
Ilya Dryomovb459be72015-06-12 13:21:07 +0300598#ifndef __KERNEL__
599 if (map->choose_tries && ftotal <= map->choose_total_tries)
600 map->choose_tries[ftotal]++;
601#endif
Sage Weil5ecc0a02009-10-06 11:31:11 -0700602 }
603
Sage Weila1a31e72010-06-24 12:58:14 -0700604 dprintk("CHOOSE returns %d\n", outpos);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700605 return outpos;
606}
607
608
609/**
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200610 * crush_choose_indep: alternative breadth-first positionally stable mapping
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200611 *
612 */
613static void crush_choose_indep(const struct crush_map *map,
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100614 struct crush_work *work,
615 const struct crush_bucket *bucket,
Ilya Dryomovbe3226a2013-12-24 21:19:26 +0200616 const __u32 *weight, int weight_max,
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200617 int x, int left, int numrep, int type,
Ilya Dryomovbe3226a2013-12-24 21:19:26 +0200618 int *out, int outpos,
Ilya Dryomov2d8be0b2013-12-24 21:19:27 +0200619 unsigned int tries,
620 unsigned int recurse_tries,
Ilya Dryomovbe3226a2013-12-24 21:19:26 +0200621 int recurse_to_leaf,
Ilya Dryomov41586082013-12-24 21:19:25 +0200622 int *out2,
623 int parent_r)
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200624{
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100625 const struct crush_bucket *in = bucket;
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200626 int endpos = outpos + left;
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200627 int rep;
628 unsigned int ftotal;
629 int r;
630 int i;
631 int item = 0;
632 int itemtype;
633 int collide;
634
635 dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
636 bucket->id, x, outpos, numrep);
637
638 /* initially my result is undefined */
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200639 for (rep = outpos; rep < endpos; rep++) {
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200640 out[rep] = CRUSH_ITEM_UNDEF;
641 if (out2)
642 out2[rep] = CRUSH_ITEM_UNDEF;
643 }
644
Ilya Dryomov2d8be0b2013-12-24 21:19:27 +0200645 for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) {
Ilya Dryomovb459be72015-06-12 13:21:07 +0300646#ifdef DEBUG_INDEP
647 if (out2 && ftotal) {
648 dprintk("%u %d a: ", ftotal, left);
649 for (rep = outpos; rep < endpos; rep++) {
650 dprintk(" %d", out[rep]);
651 }
652 dprintk("\n");
653 dprintk("%u %d b: ", ftotal, left);
654 for (rep = outpos; rep < endpos; rep++) {
655 dprintk(" %d", out2[rep]);
656 }
657 dprintk("\n");
658 }
659#endif
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200660 for (rep = outpos; rep < endpos; rep++) {
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200661 if (out[rep] != CRUSH_ITEM_UNDEF)
662 continue;
663
664 in = bucket; /* initial bucket */
665
666 /* choose through intervening buckets */
667 for (;;) {
Ilya Dryomov3102b0a2013-12-24 21:19:25 +0200668 /* note: we base the choice on the position
669 * even in the nested call. that means that
670 * if the first layer chooses the same bucket
671 * in a different position, we will tend to
672 * choose a different item in that bucket.
673 * this will involve more devices in data
674 * movement and tend to distribute the load.
675 */
Ilya Dryomov41586082013-12-24 21:19:25 +0200676 r = rep + parent_r;
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200677
678 /* be careful */
679 if (in->alg == CRUSH_BUCKET_UNIFORM &&
680 in->size % numrep == 0)
681 /* r'=r+(n+1)*f_total */
682 r += (numrep+1) * ftotal;
683 else
684 /* r' = r + n*f_total */
685 r += numrep * ftotal;
686
687 /* bucket choose */
688 if (in->size == 0) {
689 dprintk(" empty bucket\n");
690 break;
691 }
692
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100693 item = crush_bucket_choose(
694 in, work->work[-1-in->id],
695 x, r);
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200696 if (item >= map->max_devices) {
697 dprintk(" bad item %d\n", item);
698 out[rep] = CRUSH_ITEM_NONE;
699 if (out2)
700 out2[rep] = CRUSH_ITEM_NONE;
701 left--;
702 break;
703 }
704
705 /* desired type? */
706 if (item < 0)
707 itemtype = map->buckets[-1-item]->type;
708 else
709 itemtype = 0;
710 dprintk(" item %d type %d\n", item, itemtype);
711
712 /* keep going? */
713 if (itemtype != type) {
714 if (item >= 0 ||
715 (-1-item) >= map->max_buckets) {
716 dprintk(" bad item type %d\n", type);
717 out[rep] = CRUSH_ITEM_NONE;
718 if (out2)
719 out2[rep] =
720 CRUSH_ITEM_NONE;
721 left--;
722 break;
723 }
724 in = map->buckets[-1-item];
725 continue;
726 }
727
728 /* collision? */
729 collide = 0;
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200730 for (i = outpos; i < endpos; i++) {
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200731 if (out[i] == item) {
732 collide = 1;
733 break;
734 }
735 }
736 if (collide)
737 break;
738
739 if (recurse_to_leaf) {
740 if (item < 0) {
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100741 crush_choose_indep(
742 map,
743 work,
744 map->buckets[-1-item],
745 weight, weight_max,
746 x, 1, numrep, 0,
747 out2, rep,
748 recurse_tries, 0,
749 0, NULL, r);
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200750 if (out2[rep] == CRUSH_ITEM_NONE) {
751 /* placed nothing; no leaf */
752 break;
753 }
754 } else {
755 /* we already have a leaf! */
756 out2[rep] = item;
757 }
758 }
759
760 /* out? */
761 if (itemtype == 0 &&
762 is_out(map, weight, weight_max, item, x))
763 break;
764
765 /* yay! */
766 out[rep] = item;
767 left--;
768 break;
769 }
770 }
771 }
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200772 for (rep = outpos; rep < endpos; rep++) {
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200773 if (out[rep] == CRUSH_ITEM_UNDEF) {
774 out[rep] = CRUSH_ITEM_NONE;
775 }
776 if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) {
777 out2[rep] = CRUSH_ITEM_NONE;
778 }
779 }
Ilya Dryomovb459be72015-06-12 13:21:07 +0300780#ifndef __KERNEL__
781 if (map->choose_tries && ftotal <= map->choose_total_tries)
782 map->choose_tries[ftotal]++;
783#endif
784#ifdef DEBUG_INDEP
785 if (out2) {
786 dprintk("%u %d a: ", ftotal, left);
787 for (rep = outpos; rep < endpos; rep++) {
788 dprintk(" %d", out[rep]);
789 }
790 dprintk("\n");
791 dprintk("%u %d b: ", ftotal, left);
792 for (rep = outpos; rep < endpos; rep++) {
793 dprintk(" %d", out2[rep]);
794 }
795 dprintk("\n");
796 }
797#endif
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200798}
799
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100800
801/*
802 * This takes a chunk of memory and sets it up to be a shiny new
803 * working area for a CRUSH placement computation. It must be called
804 * on any newly allocated memory before passing it in to
805 * crush_do_rule. It may be used repeatedly after that, so long as the
806 * map has not changed. If the map /has/ changed, you must make sure
807 * the working size is no smaller than what was allocated and re-run
808 * crush_init_workspace.
809 *
810 * If you do retain the working space between calls to crush, make it
811 * thread-local.
812 */
813void crush_init_workspace(const struct crush_map *map, void *v)
814{
815 struct crush_work *w = v;
816 __s32 b;
817
818 /*
819 * We work by moving through the available space and setting
820 * values and pointers as we go.
821 *
822 * It's a bit like Forth's use of the 'allot' word since we
823 * set the pointer first and then reserve the space for it to
824 * point to by incrementing the point.
825 */
826 v += sizeof(struct crush_work *);
827 w->work = v;
828 v += map->max_buckets * sizeof(struct crush_work_bucket *);
829 for (b = 0; b < map->max_buckets; ++b) {
830 if (!map->buckets[b])
831 continue;
832
833 w->work[b] = v;
834 switch (map->buckets[b]->alg) {
835 default:
836 v += sizeof(struct crush_work_bucket);
837 break;
838 }
839 w->work[b]->perm_x = 0;
840 w->work[b]->perm_n = 0;
841 w->work[b]->perm = v;
842 v += map->buckets[b]->size * sizeof(__u32);
843 }
844 BUG_ON(v - (void *)w != map->working_size);
845}
846
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200847/**
Sage Weil5ecc0a02009-10-06 11:31:11 -0700848 * crush_do_rule - calculate a mapping with the given input and rule
849 * @map: the crush_map
850 * @ruleno: the rule id
851 * @x: hash input
852 * @result: pointer to result vector
853 * @result_max: maximum result size
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200854 * @weight: weight vector (for map leaves)
855 * @weight_max: size of weight vector
Ilya Dryomov743efcf2017-01-31 15:55:06 +0100856 * @cwin: pointer to at least crush_work_size() bytes of memory
Sage Weil5ecc0a02009-10-06 11:31:11 -0700857 */
Sage Weil8b12d472012-05-07 15:38:35 -0700858int crush_do_rule(const struct crush_map *map,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700859 int ruleno, int x, int *result, int result_max,
Ilya Dryomove8ef19c2013-12-24 21:19:24 +0200860 const __u32 *weight, int weight_max,
Ilya Dryomov743efcf2017-01-31 15:55:06 +0100861 void *cwin)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700862{
863 int result_len;
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100864 struct crush_work *cw = cwin;
Ilya Dryomov743efcf2017-01-31 15:55:06 +0100865 int *a = cwin + map->working_size;
866 int *b = a + result_max;
867 int *c = b + result_max;
868 int *w = a;
869 int *o = b;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700870 int recurse_to_leaf;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700871 int wsize = 0;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700872 int osize;
873 int *tmp;
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100874 const struct crush_rule *rule;
Sage Weil8b12d472012-05-07 15:38:35 -0700875 __u32 step;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700876 int i, j;
877 int numrep;
Ilya Dryomov45002262015-04-14 16:04:23 +0300878 int out_size;
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200879 /*
880 * the original choose_total_tries value was off by one (it
881 * counted "retries" and not "tries"). add one.
882 */
883 int choose_tries = map->choose_total_tries + 1;
Ilya Dryomovf18650a2013-12-24 21:19:26 +0200884 int choose_leaf_tries = 0;
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200885 /*
886 * the local tries values were counted as "retries", though,
887 * and need no adjustment
888 */
889 int choose_local_retries = map->choose_local_tries;
890 int choose_local_fallback_retries = map->choose_local_fallback_tries;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700891
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200892 int vary_r = map->chooseleaf_vary_r;
Ilya Dryomovdc6ae6d2016-01-31 14:36:07 +0100893 int stable = map->chooseleaf_stable;
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200894
Sage Weila1f48952012-05-07 15:35:24 -0700895 if ((__u32)ruleno >= map->max_rules) {
896 dprintk(" bad ruleno %d\n", ruleno);
897 return 0;
898 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700899
900 rule = map->rules[ruleno];
901 result_len = 0;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700902
Sage Weil5ecc0a02009-10-06 11:31:11 -0700903 for (step = 0; step < rule->len; step++) {
Ilya Dryomov8f99c852013-12-24 21:19:24 +0200904 int firstn = 0;
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100905 const struct crush_rule_step *curstep = &rule->steps[step];
Sage Weil06682162012-05-07 15:35:48 -0700906
Sage Weil06682162012-05-07 15:35:48 -0700907 switch (curstep->op) {
Sage Weil5ecc0a02009-10-06 11:31:11 -0700908 case CRUSH_RULE_TAKE:
Ilya Dryomov8f529792015-06-12 11:20:03 +0300909 if ((curstep->arg1 >= 0 &&
910 curstep->arg1 < map->max_devices) ||
Ilya Dryomov56a4f302016-01-31 14:36:05 +0100911 (-1-curstep->arg1 >= 0 &&
912 -1-curstep->arg1 < map->max_buckets &&
Ilya Dryomov8f529792015-06-12 11:20:03 +0300913 map->buckets[-1-curstep->arg1])) {
914 w[0] = curstep->arg1;
915 wsize = 1;
916 } else {
917 dprintk(" bad take value %d\n", curstep->arg1);
918 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700919 break;
920
Ilya Dryomovcc10df42013-12-24 21:19:26 +0200921 case CRUSH_RULE_SET_CHOOSE_TRIES:
922 if (curstep->arg1 > 0)
923 choose_tries = curstep->arg1;
924 break;
925
Ilya Dryomov917edad2013-12-24 21:19:26 +0200926 case CRUSH_RULE_SET_CHOOSELEAF_TRIES:
Ilya Dryomovbe3226a2013-12-24 21:19:26 +0200927 if (curstep->arg1 > 0)
928 choose_leaf_tries = curstep->arg1;
929 break;
930
Ilya Dryomovf046bf92013-12-24 21:19:27 +0200931 case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES:
Ilya Dryomov6ed10022014-03-19 16:58:37 +0200932 if (curstep->arg1 >= 0)
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200933 choose_local_retries = curstep->arg1;
Ilya Dryomovf046bf92013-12-24 21:19:27 +0200934 break;
935
936 case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES:
Ilya Dryomov6ed10022014-03-19 16:58:37 +0200937 if (curstep->arg1 >= 0)
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200938 choose_local_fallback_retries = curstep->arg1;
Ilya Dryomovf046bf92013-12-24 21:19:27 +0200939 break;
940
Ilya Dryomovd83ed852014-03-19 16:58:37 +0200941 case CRUSH_RULE_SET_CHOOSELEAF_VARY_R:
942 if (curstep->arg1 >= 0)
943 vary_r = curstep->arg1;
944 break;
945
Ilya Dryomovdc6ae6d2016-01-31 14:36:07 +0100946 case CRUSH_RULE_SET_CHOOSELEAF_STABLE:
947 if (curstep->arg1 >= 0)
948 stable = curstep->arg1;
949 break;
950
Ilya Dryomov917edad2013-12-24 21:19:26 +0200951 case CRUSH_RULE_CHOOSELEAF_FIRSTN:
Sage Weil5ecc0a02009-10-06 11:31:11 -0700952 case CRUSH_RULE_CHOOSE_FIRSTN:
953 firstn = 1;
Sage Weil06682162012-05-07 15:35:48 -0700954 /* fall through */
Ilya Dryomov917edad2013-12-24 21:19:26 +0200955 case CRUSH_RULE_CHOOSELEAF_INDEP:
Sage Weil5ecc0a02009-10-06 11:31:11 -0700956 case CRUSH_RULE_CHOOSE_INDEP:
Sage Weila1f48952012-05-07 15:35:24 -0700957 if (wsize == 0)
958 break;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700959
960 recurse_to_leaf =
Sage Weil06682162012-05-07 15:35:48 -0700961 curstep->op ==
Ilya Dryomov917edad2013-12-24 21:19:26 +0200962 CRUSH_RULE_CHOOSELEAF_FIRSTN ||
Sage Weil06682162012-05-07 15:35:48 -0700963 curstep->op ==
Ilya Dryomov917edad2013-12-24 21:19:26 +0200964 CRUSH_RULE_CHOOSELEAF_INDEP;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700965
966 /* reset output */
967 osize = 0;
968
969 for (i = 0; i < wsize; i++) {
Ilya Dryomovf224a692016-01-31 14:35:59 +0100970 int bno;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700971 /*
972 * see CRUSH_N, CRUSH_N_MINUS macros.
973 * basically, numrep <= 0 means relative to
974 * the provided result_max
975 */
Sage Weil06682162012-05-07 15:35:48 -0700976 numrep = curstep->arg1;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700977 if (numrep <= 0) {
978 numrep += result_max;
979 if (numrep <= 0)
980 continue;
981 }
982 j = 0;
Ilya Dryomovf224a692016-01-31 14:35:59 +0100983 /* make sure bucket id is valid */
984 bno = -1 - w[i];
985 if (bno < 0 || bno >= map->max_buckets) {
986 /* w[i] is probably CRUSH_ITEM_NONE */
987 dprintk(" bad w[i] %d\n", w[i]);
988 continue;
989 }
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200990 if (firstn) {
Ilya Dryomovd390bb22013-12-24 21:19:26 +0200991 int recurse_tries;
992 if (choose_leaf_tries)
993 recurse_tries =
994 choose_leaf_tries;
995 else if (map->chooseleaf_descend_once)
996 recurse_tries = 1;
997 else
998 recurse_tries = choose_tries;
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200999 osize += crush_choose_firstn(
1000 map,
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +01001001 cw,
Ilya Dryomovf224a692016-01-31 14:35:59 +01001002 map->buckets[bno],
Ilya Dryomov9fe07182013-12-24 21:19:25 +02001003 weight, weight_max,
1004 x, numrep,
1005 curstep->arg2,
1006 o+osize, j,
Ilya Dryomov45002262015-04-14 16:04:23 +03001007 result_max-osize,
Ilya Dryomovf18650a2013-12-24 21:19:26 +02001008 choose_tries,
Ilya Dryomovd390bb22013-12-24 21:19:26 +02001009 recurse_tries,
Ilya Dryomov48a163d2014-03-19 16:58:36 +02001010 choose_local_retries,
1011 choose_local_fallback_retries,
Ilya Dryomov9fe07182013-12-24 21:19:25 +02001012 recurse_to_leaf,
Ilya Dryomove2b149c2014-03-19 16:58:37 +02001013 vary_r,
Ilya Dryomovdc6ae6d2016-01-31 14:36:07 +01001014 stable,
Ilya Dryomove2b149c2014-03-19 16:58:37 +02001015 c+osize,
1016 0);
Ilya Dryomov9a3b4902013-12-24 21:19:25 +02001017 } else {
Ilya Dryomov45002262015-04-14 16:04:23 +03001018 out_size = ((numrep < (result_max-osize)) ?
Ilya Dryomovb459be72015-06-12 13:21:07 +03001019 numrep : (result_max-osize));
Ilya Dryomov9fe07182013-12-24 21:19:25 +02001020 crush_choose_indep(
1021 map,
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +01001022 cw,
Ilya Dryomovf224a692016-01-31 14:35:59 +01001023 map->buckets[bno],
Ilya Dryomov9fe07182013-12-24 21:19:25 +02001024 weight, weight_max,
Ilya Dryomov45002262015-04-14 16:04:23 +03001025 x, out_size, numrep,
Ilya Dryomov9fe07182013-12-24 21:19:25 +02001026 curstep->arg2,
1027 o+osize, j,
Ilya Dryomovf18650a2013-12-24 21:19:26 +02001028 choose_tries,
Ilya Dryomovd390bb22013-12-24 21:19:26 +02001029 choose_leaf_tries ?
1030 choose_leaf_tries : 1,
Ilya Dryomov9fe07182013-12-24 21:19:25 +02001031 recurse_to_leaf,
Ilya Dryomov41586082013-12-24 21:19:25 +02001032 c+osize,
1033 0);
Ilya Dryomov45002262015-04-14 16:04:23 +03001034 osize += out_size;
Ilya Dryomov9a3b4902013-12-24 21:19:25 +02001035 }
Sage Weil5ecc0a02009-10-06 11:31:11 -07001036 }
1037
1038 if (recurse_to_leaf)
1039 /* copy final _leaf_ values to output set */
1040 memcpy(o, c, osize*sizeof(*o));
1041
Ilya Dryomov2a4ba742013-12-24 21:19:24 +02001042 /* swap o and w arrays */
Sage Weil5ecc0a02009-10-06 11:31:11 -07001043 tmp = o;
1044 o = w;
1045 w = tmp;
1046 wsize = osize;
1047 break;
1048
1049
1050 case CRUSH_RULE_EMIT:
1051 for (i = 0; i < wsize && result_len < result_max; i++) {
1052 result[result_len] = w[i];
1053 result_len++;
1054 }
1055 wsize = 0;
1056 break;
1057
1058 default:
Sage Weila1f48952012-05-07 15:35:24 -07001059 dprintk(" unknown op %d at step %d\n",
1060 curstep->op, step);
1061 break;
Sage Weil5ecc0a02009-10-06 11:31:11 -07001062 }
1063 }
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +01001064
Sage Weilf1932fc2011-12-07 09:10:26 -08001065 return result_len;
Sage Weil5ecc0a02009-10-06 11:31:11 -07001066}