blob: 5d00ce2b5339660d704c4a2c2f40b115c12ae0eb [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Sage Weilf24e9982009-10-06 11:31:10 -07002
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003#include <linux/ceph/ceph_debug.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09004
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Sage Weilf24e9982009-10-06 11:31:10 -07007
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07008#include <linux/ceph/libceph.h>
9#include <linux/ceph/osdmap.h>
10#include <linux/ceph/decode.h>
11#include <linux/crush/hash.h>
12#include <linux/crush/mapper.h>
Sage Weilf24e9982009-10-06 11:31:10 -070013
Ilya Dryomov0bb05da2017-06-22 19:44:06 +020014char *ceph_osdmap_state_str(char *str, int len, u32 state)
Sage Weilf24e9982009-10-06 11:31:10 -070015{
Sage Weilf24e9982009-10-06 11:31:10 -070016 if (!len)
Cong Ding1ec39112013-01-25 17:48:59 -060017 return str;
Sage Weilf24e9982009-10-06 11:31:10 -070018
Cong Ding1ec39112013-01-25 17:48:59 -060019 if ((state & CEPH_OSD_EXISTS) && (state & CEPH_OSD_UP))
20 snprintf(str, len, "exists, up");
21 else if (state & CEPH_OSD_EXISTS)
22 snprintf(str, len, "exists");
23 else if (state & CEPH_OSD_UP)
24 snprintf(str, len, "up");
25 else
Sage Weilf24e9982009-10-06 11:31:10 -070026 snprintf(str, len, "doesn't exist");
Cong Ding1ec39112013-01-25 17:48:59 -060027
Sage Weilf24e9982009-10-06 11:31:10 -070028 return str;
29}
30
31/* maps */
32
Eric Dumazet95c96172012-04-15 05:58:06 +000033static int calc_bits_of(unsigned int t)
Sage Weilf24e9982009-10-06 11:31:10 -070034{
35 int b = 0;
36 while (t) {
37 t = t >> 1;
38 b++;
39 }
40 return b;
41}
42
43/*
44 * the foo_mask is the smallest value 2^n-1 that is >= foo.
45 */
46static void calc_pg_masks(struct ceph_pg_pool_info *pi)
47{
Sage Weil4f6a7e52013-02-23 10:41:09 -080048 pi->pg_num_mask = (1 << calc_bits_of(pi->pg_num-1)) - 1;
49 pi->pgp_num_mask = (1 << calc_bits_of(pi->pgp_num-1)) - 1;
Sage Weilf24e9982009-10-06 11:31:10 -070050}
51
52/*
53 * decode crush map
54 */
55static int crush_decode_uniform_bucket(void **p, void *end,
56 struct crush_bucket_uniform *b)
57{
58 dout("crush_decode_uniform_bucket %p to %p\n", *p, end);
59 ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -070060 b->item_weight = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -070061 return 0;
62bad:
63 return -EINVAL;
64}
65
66static int crush_decode_list_bucket(void **p, void *end,
67 struct crush_bucket_list *b)
68{
69 int j;
70 dout("crush_decode_list_bucket %p to %p\n", *p, end);
71 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
72 if (b->item_weights == NULL)
73 return -ENOMEM;
74 b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
75 if (b->sum_weights == NULL)
76 return -ENOMEM;
77 ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
78 for (j = 0; j < b->h.size; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -070079 b->item_weights[j] = ceph_decode_32(p);
80 b->sum_weights[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -070081 }
82 return 0;
83bad:
84 return -EINVAL;
85}
86
87static int crush_decode_tree_bucket(void **p, void *end,
88 struct crush_bucket_tree *b)
89{
90 int j;
91 dout("crush_decode_tree_bucket %p to %p\n", *p, end);
Ilya Dryomov82cd0032015-06-29 19:30:23 +030092 ceph_decode_8_safe(p, end, b->num_nodes, bad);
Sage Weilf24e9982009-10-06 11:31:10 -070093 b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS);
94 if (b->node_weights == NULL)
95 return -ENOMEM;
96 ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad);
97 for (j = 0; j < b->num_nodes; j++)
Sage Weilc89136e2009-10-14 09:59:09 -070098 b->node_weights[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -070099 return 0;
100bad:
101 return -EINVAL;
102}
103
104static int crush_decode_straw_bucket(void **p, void *end,
105 struct crush_bucket_straw *b)
106{
107 int j;
108 dout("crush_decode_straw_bucket %p to %p\n", *p, end);
109 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
110 if (b->item_weights == NULL)
111 return -ENOMEM;
112 b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
113 if (b->straws == NULL)
114 return -ENOMEM;
115 ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
116 for (j = 0; j < b->h.size; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -0700117 b->item_weights[j] = ceph_decode_32(p);
118 b->straws[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700119 }
120 return 0;
121bad:
122 return -EINVAL;
123}
124
Ilya Dryomov958a2762015-04-14 16:54:52 +0300125static int crush_decode_straw2_bucket(void **p, void *end,
126 struct crush_bucket_straw2 *b)
127{
128 int j;
129 dout("crush_decode_straw2_bucket %p to %p\n", *p, end);
130 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
131 if (b->item_weights == NULL)
132 return -ENOMEM;
133 ceph_decode_need(p, end, b->h.size * sizeof(u32), bad);
134 for (j = 0; j < b->h.size; j++)
135 b->item_weights[j] = ceph_decode_32(p);
136 return 0;
137bad:
138 return -EINVAL;
139}
140
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +0200141static struct crush_choose_arg_map *alloc_choose_arg_map(void)
142{
143 struct crush_choose_arg_map *arg_map;
144
145 arg_map = kzalloc(sizeof(*arg_map), GFP_NOIO);
146 if (!arg_map)
147 return NULL;
148
149 RB_CLEAR_NODE(&arg_map->node);
150 return arg_map;
151}
152
153static void free_choose_arg_map(struct crush_choose_arg_map *arg_map)
154{
155 if (arg_map) {
156 int i, j;
157
158 WARN_ON(!RB_EMPTY_NODE(&arg_map->node));
159
160 for (i = 0; i < arg_map->size; i++) {
161 struct crush_choose_arg *arg = &arg_map->args[i];
162
163 for (j = 0; j < arg->weight_set_size; j++)
164 kfree(arg->weight_set[j].weights);
165 kfree(arg->weight_set);
166 kfree(arg->ids);
167 }
168 kfree(arg_map->args);
169 kfree(arg_map);
170 }
171}
172
173DEFINE_RB_FUNCS(choose_arg_map, struct crush_choose_arg_map, choose_args_index,
174 node);
175
176void clear_choose_args(struct crush_map *c)
177{
178 while (!RB_EMPTY_ROOT(&c->choose_args)) {
179 struct crush_choose_arg_map *arg_map =
180 rb_entry(rb_first(&c->choose_args),
181 struct crush_choose_arg_map, node);
182
183 erase_choose_arg_map(&c->choose_args, arg_map);
184 free_choose_arg_map(arg_map);
185 }
186}
187
188static u32 *decode_array_32_alloc(void **p, void *end, u32 *plen)
189{
190 u32 *a = NULL;
191 u32 len;
192 int ret;
193
194 ceph_decode_32_safe(p, end, len, e_inval);
195 if (len) {
196 u32 i;
197
198 a = kmalloc_array(len, sizeof(u32), GFP_NOIO);
199 if (!a) {
200 ret = -ENOMEM;
201 goto fail;
202 }
203
204 ceph_decode_need(p, end, len * sizeof(u32), e_inval);
205 for (i = 0; i < len; i++)
206 a[i] = ceph_decode_32(p);
207 }
208
209 *plen = len;
210 return a;
211
212e_inval:
213 ret = -EINVAL;
214fail:
215 kfree(a);
216 return ERR_PTR(ret);
217}
218
219/*
220 * Assumes @arg is zero-initialized.
221 */
222static int decode_choose_arg(void **p, void *end, struct crush_choose_arg *arg)
223{
224 int ret;
225
226 ceph_decode_32_safe(p, end, arg->weight_set_size, e_inval);
227 if (arg->weight_set_size) {
228 u32 i;
229
230 arg->weight_set = kmalloc_array(arg->weight_set_size,
231 sizeof(*arg->weight_set),
232 GFP_NOIO);
233 if (!arg->weight_set)
234 return -ENOMEM;
235
236 for (i = 0; i < arg->weight_set_size; i++) {
237 struct crush_weight_set *w = &arg->weight_set[i];
238
239 w->weights = decode_array_32_alloc(p, end, &w->size);
240 if (IS_ERR(w->weights)) {
241 ret = PTR_ERR(w->weights);
242 w->weights = NULL;
243 return ret;
244 }
245 }
246 }
247
248 arg->ids = decode_array_32_alloc(p, end, &arg->ids_size);
249 if (IS_ERR(arg->ids)) {
250 ret = PTR_ERR(arg->ids);
251 arg->ids = NULL;
252 return ret;
253 }
254
255 return 0;
256
257e_inval:
258 return -EINVAL;
259}
260
261static int decode_choose_args(void **p, void *end, struct crush_map *c)
262{
263 struct crush_choose_arg_map *arg_map = NULL;
264 u32 num_choose_arg_maps, num_buckets;
265 int ret;
266
267 ceph_decode_32_safe(p, end, num_choose_arg_maps, e_inval);
268 while (num_choose_arg_maps--) {
269 arg_map = alloc_choose_arg_map();
270 if (!arg_map) {
271 ret = -ENOMEM;
272 goto fail;
273 }
274
275 ceph_decode_64_safe(p, end, arg_map->choose_args_index,
276 e_inval);
277 arg_map->size = c->max_buckets;
278 arg_map->args = kcalloc(arg_map->size, sizeof(*arg_map->args),
279 GFP_NOIO);
280 if (!arg_map->args) {
281 ret = -ENOMEM;
282 goto fail;
283 }
284
285 ceph_decode_32_safe(p, end, num_buckets, e_inval);
286 while (num_buckets--) {
287 struct crush_choose_arg *arg;
288 u32 bucket_index;
289
290 ceph_decode_32_safe(p, end, bucket_index, e_inval);
291 if (bucket_index >= arg_map->size)
292 goto e_inval;
293
294 arg = &arg_map->args[bucket_index];
295 ret = decode_choose_arg(p, end, arg);
296 if (ret)
297 goto fail;
Ilya Dryomovc7ed1a42017-07-24 15:49:52 +0200298
299 if (arg->ids_size &&
300 arg->ids_size != c->buckets[bucket_index]->size)
301 goto e_inval;
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +0200302 }
303
304 insert_choose_arg_map(&c->choose_args, arg_map);
305 }
306
307 return 0;
308
309e_inval:
310 ret = -EINVAL;
311fail:
312 free_choose_arg_map(arg_map);
313 return ret;
314}
315
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100316static void crush_finalize(struct crush_map *c)
317{
318 __s32 b;
319
320 /* Space for the array of pointers to per-bucket workspace */
321 c->working_size = sizeof(struct crush_work) +
322 c->max_buckets * sizeof(struct crush_work_bucket *);
323
324 for (b = 0; b < c->max_buckets; b++) {
325 if (!c->buckets[b])
326 continue;
327
328 switch (c->buckets[b]->alg) {
329 default:
330 /*
331 * The base case, permutation variables and
332 * the pointer to the permutation array.
333 */
334 c->working_size += sizeof(struct crush_work_bucket);
335 break;
336 }
337 /* Every bucket has a permutation array. */
338 c->working_size += c->buckets[b]->size * sizeof(__u32);
339 }
340}
341
Sage Weilf24e9982009-10-06 11:31:10 -0700342static struct crush_map *crush_decode(void *pbyval, void *end)
343{
344 struct crush_map *c;
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200345 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700346 int i, j;
347 void **p = &pbyval;
348 void *start = pbyval;
349 u32 magic;
350
351 dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
352
353 c = kzalloc(sizeof(*c), GFP_NOFS);
354 if (c == NULL)
355 return ERR_PTR(-ENOMEM);
356
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +0200357 c->choose_args = RB_ROOT;
358
Sage Weil546f04e2012-07-30 18:15:23 -0700359 /* set tunables to default values */
360 c->choose_local_tries = 2;
361 c->choose_local_fallback_tries = 5;
362 c->choose_total_tries = 19;
Jim Schutt1604f482012-11-30 09:15:25 -0700363 c->chooseleaf_descend_once = 0;
Sage Weil546f04e2012-07-30 18:15:23 -0700364
Sage Weilf24e9982009-10-06 11:31:10 -0700365 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700366 magic = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700367 if (magic != CRUSH_MAGIC) {
368 pr_err("crush_decode magic %x != current %x\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000369 (unsigned int)magic, (unsigned int)CRUSH_MAGIC);
Sage Weilf24e9982009-10-06 11:31:10 -0700370 goto bad;
371 }
Sage Weilc89136e2009-10-14 09:59:09 -0700372 c->max_buckets = ceph_decode_32(p);
373 c->max_rules = ceph_decode_32(p);
374 c->max_devices = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700375
Sage Weilf24e9982009-10-06 11:31:10 -0700376 c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
377 if (c->buckets == NULL)
378 goto badmem;
379 c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
380 if (c->rules == NULL)
381 goto badmem;
382
383 /* buckets */
384 for (i = 0; i < c->max_buckets; i++) {
385 int size = 0;
386 u32 alg;
387 struct crush_bucket *b;
388
389 ceph_decode_32_safe(p, end, alg, bad);
390 if (alg == 0) {
391 c->buckets[i] = NULL;
392 continue;
393 }
394 dout("crush_decode bucket %d off %x %p to %p\n",
395 i, (int)(*p-start), *p, end);
396
397 switch (alg) {
398 case CRUSH_BUCKET_UNIFORM:
399 size = sizeof(struct crush_bucket_uniform);
400 break;
401 case CRUSH_BUCKET_LIST:
402 size = sizeof(struct crush_bucket_list);
403 break;
404 case CRUSH_BUCKET_TREE:
405 size = sizeof(struct crush_bucket_tree);
406 break;
407 case CRUSH_BUCKET_STRAW:
408 size = sizeof(struct crush_bucket_straw);
409 break;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300410 case CRUSH_BUCKET_STRAW2:
411 size = sizeof(struct crush_bucket_straw2);
412 break;
Sage Weilf24e9982009-10-06 11:31:10 -0700413 default:
414 goto bad;
415 }
416 BUG_ON(size == 0);
417 b = c->buckets[i] = kzalloc(size, GFP_NOFS);
418 if (b == NULL)
419 goto badmem;
420
421 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700422 b->id = ceph_decode_32(p);
423 b->type = ceph_decode_16(p);
Sage Weilfb690392009-11-07 20:18:22 -0800424 b->alg = ceph_decode_8(p);
425 b->hash = ceph_decode_8(p);
Sage Weilc89136e2009-10-14 09:59:09 -0700426 b->weight = ceph_decode_32(p);
427 b->size = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700428
429 dout("crush_decode bucket size %d off %x %p to %p\n",
430 b->size, (int)(*p-start), *p, end);
431
432 b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
433 if (b->items == NULL)
434 goto badmem;
Sage Weilf24e9982009-10-06 11:31:10 -0700435
436 ceph_decode_need(p, end, b->size*sizeof(u32), bad);
437 for (j = 0; j < b->size; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700438 b->items[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700439
440 switch (b->alg) {
441 case CRUSH_BUCKET_UNIFORM:
442 err = crush_decode_uniform_bucket(p, end,
443 (struct crush_bucket_uniform *)b);
444 if (err < 0)
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200445 goto fail;
Sage Weilf24e9982009-10-06 11:31:10 -0700446 break;
447 case CRUSH_BUCKET_LIST:
448 err = crush_decode_list_bucket(p, end,
449 (struct crush_bucket_list *)b);
450 if (err < 0)
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200451 goto fail;
Sage Weilf24e9982009-10-06 11:31:10 -0700452 break;
453 case CRUSH_BUCKET_TREE:
454 err = crush_decode_tree_bucket(p, end,
455 (struct crush_bucket_tree *)b);
456 if (err < 0)
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200457 goto fail;
Sage Weilf24e9982009-10-06 11:31:10 -0700458 break;
459 case CRUSH_BUCKET_STRAW:
460 err = crush_decode_straw_bucket(p, end,
461 (struct crush_bucket_straw *)b);
462 if (err < 0)
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200463 goto fail;
Sage Weilf24e9982009-10-06 11:31:10 -0700464 break;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300465 case CRUSH_BUCKET_STRAW2:
466 err = crush_decode_straw2_bucket(p, end,
467 (struct crush_bucket_straw2 *)b);
468 if (err < 0)
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200469 goto fail;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300470 break;
Sage Weilf24e9982009-10-06 11:31:10 -0700471 }
472 }
473
474 /* rules */
475 dout("rule vec is %p\n", c->rules);
476 for (i = 0; i < c->max_rules; i++) {
477 u32 yes;
478 struct crush_rule *r;
479
480 ceph_decode_32_safe(p, end, yes, bad);
481 if (!yes) {
482 dout("crush_decode NO rule %d off %x %p to %p\n",
483 i, (int)(*p-start), *p, end);
484 c->rules[i] = NULL;
485 continue;
486 }
487
488 dout("crush_decode rule %d off %x %p to %p\n",
489 i, (int)(*p-start), *p, end);
490
491 /* len */
492 ceph_decode_32_safe(p, end, yes, bad);
493#if BITS_PER_LONG == 32
Xi Wang64486692012-02-16 11:55:48 -0500494 if (yes > (ULONG_MAX - sizeof(*r))
495 / sizeof(struct crush_rule_step))
Sage Weilf24e9982009-10-06 11:31:10 -0700496 goto bad;
497#endif
Gustavo A. R. Silva6b41d4d2019-01-15 13:41:53 -0600498 r = kmalloc(struct_size(r, steps, yes), GFP_NOFS);
499 c->rules[i] = r;
Sage Weilf24e9982009-10-06 11:31:10 -0700500 if (r == NULL)
501 goto badmem;
502 dout(" rule %d is at %p\n", i, r);
503 r->len = yes;
504 ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
505 ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
506 for (j = 0; j < r->len; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -0700507 r->steps[j].op = ceph_decode_32(p);
508 r->steps[j].arg1 = ceph_decode_32(p);
509 r->steps[j].arg2 = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700510 }
511 }
512
Ilya Dryomov278b1d702017-06-21 17:27:17 +0200513 ceph_decode_skip_map(p, end, 32, string, bad); /* type_map */
514 ceph_decode_skip_map(p, end, 32, string, bad); /* name_map */
515 ceph_decode_skip_map(p, end, 32, string, bad); /* rule_name_map */
Sage Weilf24e9982009-10-06 11:31:10 -0700516
Sage Weil546f04e2012-07-30 18:15:23 -0700517 /* tunables */
518 ceph_decode_need(p, end, 3*sizeof(u32), done);
519 c->choose_local_tries = ceph_decode_32(p);
520 c->choose_local_fallback_tries = ceph_decode_32(p);
521 c->choose_total_tries = ceph_decode_32(p);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100522 dout("crush decode tunable choose_local_tries = %d\n",
Sage Weil546f04e2012-07-30 18:15:23 -0700523 c->choose_local_tries);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100524 dout("crush decode tunable choose_local_fallback_tries = %d\n",
Sage Weil546f04e2012-07-30 18:15:23 -0700525 c->choose_local_fallback_tries);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100526 dout("crush decode tunable choose_total_tries = %d\n",
Sage Weil546f04e2012-07-30 18:15:23 -0700527 c->choose_total_tries);
528
Jim Schutt1604f482012-11-30 09:15:25 -0700529 ceph_decode_need(p, end, sizeof(u32), done);
530 c->chooseleaf_descend_once = ceph_decode_32(p);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100531 dout("crush decode tunable chooseleaf_descend_once = %d\n",
Jim Schutt1604f482012-11-30 09:15:25 -0700532 c->chooseleaf_descend_once);
533
Ilya Dryomovf1406622014-05-09 18:27:34 +0400534 ceph_decode_need(p, end, sizeof(u8), done);
535 c->chooseleaf_vary_r = ceph_decode_8(p);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100536 dout("crush decode tunable chooseleaf_vary_r = %d\n",
Ilya Dryomovf1406622014-05-09 18:27:34 +0400537 c->chooseleaf_vary_r);
538
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100539 /* skip straw_calc_version, allowed_bucket_algs */
540 ceph_decode_need(p, end, sizeof(u8) + sizeof(u32), done);
541 *p += sizeof(u8) + sizeof(u32);
542
543 ceph_decode_need(p, end, sizeof(u8), done);
544 c->chooseleaf_stable = ceph_decode_8(p);
545 dout("crush decode tunable chooseleaf_stable = %d\n",
546 c->chooseleaf_stable);
547
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +0200548 if (*p != end) {
549 /* class_map */
550 ceph_decode_skip_map(p, end, 32, 32, bad);
551 /* class_name */
552 ceph_decode_skip_map(p, end, 32, string, bad);
553 /* class_bucket */
554 ceph_decode_skip_map_of_map(p, end, 32, 32, 32, bad);
555 }
556
557 if (*p != end) {
558 err = decode_choose_args(p, end, c);
559 if (err)
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200560 goto fail;
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +0200561 }
562
Sage Weil546f04e2012-07-30 18:15:23 -0700563done:
Ilya Dryomov9afd30d2017-02-28 18:53:53 +0100564 crush_finalize(c);
Sage Weilf24e9982009-10-06 11:31:10 -0700565 dout("crush_decode success\n");
566 return c;
567
568badmem:
569 err = -ENOMEM;
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200570fail:
Sage Weilf24e9982009-10-06 11:31:10 -0700571 dout("crush_decode fail %d\n", err);
572 crush_destroy(c);
573 return ERR_PTR(err);
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200574
575bad:
576 err = -EINVAL;
577 goto fail;
Sage Weilf24e9982009-10-06 11:31:10 -0700578}
579
Ilya Dryomovf984cb72016-04-28 16:07:23 +0200580int ceph_pg_compare(const struct ceph_pg *lhs, const struct ceph_pg *rhs)
581{
582 if (lhs->pool < rhs->pool)
583 return -1;
584 if (lhs->pool > rhs->pool)
585 return 1;
586 if (lhs->seed < rhs->seed)
587 return -1;
588 if (lhs->seed > rhs->seed)
589 return 1;
590
591 return 0;
592}
593
Ilya Dryomova02a9462017-06-19 12:18:05 +0200594int ceph_spg_compare(const struct ceph_spg *lhs, const struct ceph_spg *rhs)
595{
596 int ret;
597
598 ret = ceph_pg_compare(&lhs->pgid, &rhs->pgid);
599 if (ret)
600 return ret;
601
602 if (lhs->shard < rhs->shard)
603 return -1;
604 if (lhs->shard > rhs->shard)
605 return 1;
606
607 return 0;
608}
609
Ilya Dryomova303bb02017-06-21 17:27:17 +0200610static struct ceph_pg_mapping *alloc_pg_mapping(size_t payload_len)
611{
612 struct ceph_pg_mapping *pg;
613
614 pg = kmalloc(sizeof(*pg) + payload_len, GFP_NOIO);
615 if (!pg)
616 return NULL;
617
618 RB_CLEAR_NODE(&pg->node);
619 return pg;
620}
621
622static void free_pg_mapping(struct ceph_pg_mapping *pg)
623{
624 WARN_ON(!RB_EMPTY_NODE(&pg->node));
625
626 kfree(pg);
627}
628
Sage Weilf24e9982009-10-06 11:31:10 -0700629/*
Sage Weil9794b142010-02-16 15:53:32 -0800630 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
Ilya Dryomov9686f942014-03-21 19:05:29 +0200631 * to a set of osds) and primary_temp (explicit primary setting)
Sage Weilf24e9982009-10-06 11:31:10 -0700632 */
Ilya Dryomovab751442017-06-21 17:27:17 +0200633DEFINE_RB_FUNCS2(pg_mapping, struct ceph_pg_mapping, pgid, ceph_pg_compare,
634 RB_BYPTR, const struct ceph_pg *, node)
Sage Weil8adc8b32011-09-28 10:11:04 -0700635
Sage Weilf24e9982009-10-06 11:31:10 -0700636/*
Sage Weil4fc51be2010-02-16 15:55:03 -0800637 * rbtree of pg pool info
638 */
Ilya Dryomov8a4b8632020-05-19 16:46:47 +0200639DEFINE_RB_FUNCS(pg_pool, struct ceph_pg_pool_info, id, node)
Sage Weil4fc51be2010-02-16 15:55:03 -0800640
Ilya Dryomovce7f6a22014-01-27 17:40:19 +0200641struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id)
642{
Ilya Dryomov8a4b8632020-05-19 16:46:47 +0200643 return lookup_pg_pool(&map->pg_pools, id);
Ilya Dryomovce7f6a22014-01-27 17:40:19 +0200644}
645
Alex Elder72afc712012-10-30 19:40:33 -0500646const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id)
647{
648 struct ceph_pg_pool_info *pi;
649
650 if (id == CEPH_NOPOOL)
651 return NULL;
652
653 if (WARN_ON_ONCE(id > (u64) INT_MAX))
654 return NULL;
655
Ilya Dryomov8a4b8632020-05-19 16:46:47 +0200656 pi = lookup_pg_pool(&map->pg_pools, id);
Alex Elder72afc712012-10-30 19:40:33 -0500657 return pi ? pi->name : NULL;
658}
659EXPORT_SYMBOL(ceph_pg_pool_name_by_id);
660
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700661int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
662{
663 struct rb_node *rbp;
664
665 for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
666 struct ceph_pg_pool_info *pi =
667 rb_entry(rbp, struct ceph_pg_pool_info, node);
668 if (pi->name && strcmp(pi->name, name) == 0)
669 return pi->id;
670 }
671 return -ENOENT;
672}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700673EXPORT_SYMBOL(ceph_pg_poolid_by_name);
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700674
Ilya Dryomov76142092020-03-09 12:03:14 +0100675u64 ceph_pg_pool_flags(struct ceph_osdmap *map, u64 id)
676{
677 struct ceph_pg_pool_info *pi;
678
Ilya Dryomov8a4b8632020-05-19 16:46:47 +0200679 pi = lookup_pg_pool(&map->pg_pools, id);
Ilya Dryomov76142092020-03-09 12:03:14 +0100680 return pi ? pi->flags : 0;
681}
682EXPORT_SYMBOL(ceph_pg_pool_flags);
683
Sage Weil2844a762010-04-09 15:46:42 -0700684static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
685{
Ilya Dryomov8a4b8632020-05-19 16:46:47 +0200686 erase_pg_pool(root, pi);
Sage Weil2844a762010-04-09 15:46:42 -0700687 kfree(pi->name);
688 kfree(pi);
689}
690
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +0200691static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
Sage Weilefd75762010-03-17 10:05:28 -0700692{
Sage Weil4f6a7e52013-02-23 10:41:09 -0800693 u8 ev, cv;
694 unsigned len, num;
695 void *pool_end;
Sage Weil73a7e692010-08-02 11:00:55 -0700696
Sage Weil4f6a7e52013-02-23 10:41:09 -0800697 ceph_decode_need(p, end, 2 + 4, bad);
698 ev = ceph_decode_8(p); /* encoding version */
699 cv = ceph_decode_8(p); /* compat version */
700 if (ev < 5) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700701 pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800702 return -EINVAL;
703 }
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200704 if (cv > 9) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700705 pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800706 return -EINVAL;
707 }
708 len = ceph_decode_32(p);
709 ceph_decode_need(p, end, len, bad);
710 pool_end = *p + len;
Sage Weil73a7e692010-08-02 11:00:55 -0700711
Sage Weil4f6a7e52013-02-23 10:41:09 -0800712 pi->type = ceph_decode_8(p);
713 pi->size = ceph_decode_8(p);
714 pi->crush_ruleset = ceph_decode_8(p);
715 pi->object_hash = ceph_decode_8(p);
716
717 pi->pg_num = ceph_decode_32(p);
718 pi->pgp_num = ceph_decode_32(p);
719
720 *p += 4 + 4; /* skip lpg* */
721 *p += 4; /* skip last_change */
722 *p += 8 + 4; /* skip snap_seq, snap_epoch */
723
724 /* skip snaps */
725 num = ceph_decode_32(p);
726 while (num--) {
727 *p += 8; /* snapid key */
728 *p += 1 + 1; /* versions */
729 len = ceph_decode_32(p);
730 *p += len;
Sage Weil73a7e692010-08-02 11:00:55 -0700731 }
732
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200733 /* skip removed_snaps */
Sage Weil4f6a7e52013-02-23 10:41:09 -0800734 num = ceph_decode_32(p);
735 *p += num * (8 + 8);
736
737 *p += 8; /* skip auid */
738 pi->flags = ceph_decode_64(p);
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200739 *p += 4; /* skip crash_replay_interval */
740
741 if (ev >= 7)
Ilya Dryomov04812ac2016-04-28 16:07:23 +0200742 pi->min_size = ceph_decode_8(p);
743 else
744 pi->min_size = pi->size - pi->size / 2;
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200745
746 if (ev >= 8)
747 *p += 8 + 8; /* skip quota_max_* */
748
749 if (ev >= 9) {
750 /* skip tiers */
751 num = ceph_decode_32(p);
752 *p += num * 8;
753
754 *p += 8; /* skip tier_of */
755 *p += 1; /* skip cache_mode */
756
757 pi->read_tier = ceph_decode_64(p);
758 pi->write_tier = ceph_decode_64(p);
759 } else {
760 pi->read_tier = -1;
761 pi->write_tier = -1;
762 }
Sage Weil4f6a7e52013-02-23 10:41:09 -0800763
Ilya Dryomov04812ac2016-04-28 16:07:23 +0200764 if (ev >= 10) {
765 /* skip properties */
766 num = ceph_decode_32(p);
767 while (num--) {
768 len = ceph_decode_32(p);
769 *p += len; /* key */
770 len = ceph_decode_32(p);
771 *p += len; /* val */
772 }
773 }
774
775 if (ev >= 11) {
776 /* skip hit_set_params */
777 *p += 1 + 1; /* versions */
778 len = ceph_decode_32(p);
779 *p += len;
780
781 *p += 4; /* skip hit_set_period */
782 *p += 4; /* skip hit_set_count */
783 }
784
785 if (ev >= 12)
786 *p += 4; /* skip stripe_width */
787
788 if (ev >= 13) {
789 *p += 8; /* skip target_max_bytes */
790 *p += 8; /* skip target_max_objects */
791 *p += 4; /* skip cache_target_dirty_ratio_micro */
792 *p += 4; /* skip cache_target_full_ratio_micro */
793 *p += 4; /* skip cache_min_flush_age */
794 *p += 4; /* skip cache_min_evict_age */
795 }
796
797 if (ev >= 14) {
798 /* skip erasure_code_profile */
799 len = ceph_decode_32(p);
800 *p += len;
801 }
802
Ilya Dryomov8e48cf02017-06-05 14:45:00 +0200803 /*
804 * last_force_op_resend_preluminous, will be overridden if the
805 * map was encoded with RESEND_ON_SPLIT
806 */
Ilya Dryomov04812ac2016-04-28 16:07:23 +0200807 if (ev >= 15)
808 pi->last_force_request_resend = ceph_decode_32(p);
809 else
810 pi->last_force_request_resend = 0;
811
Ilya Dryomov8e48cf02017-06-05 14:45:00 +0200812 if (ev >= 16)
813 *p += 4; /* skip min_read_recency_for_promote */
814
815 if (ev >= 17)
816 *p += 8; /* skip expected_num_objects */
817
818 if (ev >= 19)
819 *p += 4; /* skip cache_target_dirty_high_ratio_micro */
820
821 if (ev >= 20)
822 *p += 4; /* skip min_write_recency_for_promote */
823
824 if (ev >= 21)
825 *p += 1; /* skip use_gmt_hitset */
826
827 if (ev >= 22)
828 *p += 1; /* skip fast_read */
829
830 if (ev >= 23) {
831 *p += 4; /* skip hit_set_grade_decay_rate */
832 *p += 4; /* skip hit_set_search_last_n */
833 }
834
835 if (ev >= 24) {
836 /* skip opts */
837 *p += 1 + 1; /* versions */
838 len = ceph_decode_32(p);
839 *p += len;
840 }
841
842 if (ev >= 25)
843 pi->last_force_request_resend = ceph_decode_32(p);
844
Sage Weil4f6a7e52013-02-23 10:41:09 -0800845 /* ignore the rest */
846
847 *p = pool_end;
848 calc_pg_masks(pi);
Sage Weil73a7e692010-08-02 11:00:55 -0700849 return 0;
850
851bad:
852 return -EINVAL;
Sage Weilefd75762010-03-17 10:05:28 -0700853}
854
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +0200855static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
Sage Weil2844a762010-04-09 15:46:42 -0700856{
857 struct ceph_pg_pool_info *pi;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800858 u32 num, len;
859 u64 pool;
Sage Weil2844a762010-04-09 15:46:42 -0700860
861 ceph_decode_32_safe(p, end, num, bad);
862 dout(" %d pool names\n", num);
863 while (num--) {
Sage Weil4f6a7e52013-02-23 10:41:09 -0800864 ceph_decode_64_safe(p, end, pool, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700865 ceph_decode_32_safe(p, end, len, bad);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800866 dout(" pool %llu len %d\n", pool, len);
Xi Wangad3b9042012-06-06 19:35:55 -0500867 ceph_decode_need(p, end, len, bad);
Ilya Dryomov8a4b8632020-05-19 16:46:47 +0200868 pi = lookup_pg_pool(&map->pg_pools, pool);
Sage Weil2844a762010-04-09 15:46:42 -0700869 if (pi) {
Xi Wangad3b9042012-06-06 19:35:55 -0500870 char *name = kstrndup(*p, len, GFP_NOFS);
871
872 if (!name)
873 return -ENOMEM;
Sage Weil2844a762010-04-09 15:46:42 -0700874 kfree(pi->name);
Xi Wangad3b9042012-06-06 19:35:55 -0500875 pi->name = name;
876 dout(" name is %s\n", pi->name);
Sage Weil2844a762010-04-09 15:46:42 -0700877 }
878 *p += len;
879 }
880 return 0;
881
882bad:
883 return -EINVAL;
884}
885
886/*
887 * osd map
888 */
Ilya Dryomove5253a72016-04-28 16:07:25 +0200889struct ceph_osdmap *ceph_osdmap_alloc(void)
890{
891 struct ceph_osdmap *map;
892
893 map = kzalloc(sizeof(*map), GFP_NOIO);
894 if (!map)
895 return NULL;
896
897 map->pg_pools = RB_ROOT;
898 map->pool_max = -1;
899 map->pg_temp = RB_ROOT;
900 map->primary_temp = RB_ROOT;
Ilya Dryomov6f428df2017-06-21 17:27:18 +0200901 map->pg_upmap = RB_ROOT;
902 map->pg_upmap_items = RB_ROOT;
Ilya Dryomov743efcf2017-01-31 15:55:06 +0100903 mutex_init(&map->crush_workspace_mutex);
Ilya Dryomove5253a72016-04-28 16:07:25 +0200904
905 return map;
906}
907
Sage Weil2844a762010-04-09 15:46:42 -0700908void ceph_osdmap_destroy(struct ceph_osdmap *map)
909{
910 dout("osdmap_destroy %p\n", map);
911 if (map->crush)
912 crush_destroy(map->crush);
913 while (!RB_EMPTY_ROOT(&map->pg_temp)) {
914 struct ceph_pg_mapping *pg =
915 rb_entry(rb_first(&map->pg_temp),
916 struct ceph_pg_mapping, node);
Ilya Dryomovab751442017-06-21 17:27:17 +0200917 erase_pg_mapping(&map->pg_temp, pg);
918 free_pg_mapping(pg);
Sage Weil2844a762010-04-09 15:46:42 -0700919 }
Ilya Dryomov9686f942014-03-21 19:05:29 +0200920 while (!RB_EMPTY_ROOT(&map->primary_temp)) {
921 struct ceph_pg_mapping *pg =
922 rb_entry(rb_first(&map->primary_temp),
923 struct ceph_pg_mapping, node);
Ilya Dryomovab751442017-06-21 17:27:17 +0200924 erase_pg_mapping(&map->primary_temp, pg);
925 free_pg_mapping(pg);
Ilya Dryomov9686f942014-03-21 19:05:29 +0200926 }
Ilya Dryomov6f428df2017-06-21 17:27:18 +0200927 while (!RB_EMPTY_ROOT(&map->pg_upmap)) {
928 struct ceph_pg_mapping *pg =
929 rb_entry(rb_first(&map->pg_upmap),
930 struct ceph_pg_mapping, node);
931 rb_erase(&pg->node, &map->pg_upmap);
932 kfree(pg);
933 }
934 while (!RB_EMPTY_ROOT(&map->pg_upmap_items)) {
935 struct ceph_pg_mapping *pg =
936 rb_entry(rb_first(&map->pg_upmap_items),
937 struct ceph_pg_mapping, node);
938 rb_erase(&pg->node, &map->pg_upmap_items);
939 kfree(pg);
940 }
Sage Weil2844a762010-04-09 15:46:42 -0700941 while (!RB_EMPTY_ROOT(&map->pg_pools)) {
942 struct ceph_pg_pool_info *pi =
943 rb_entry(rb_first(&map->pg_pools),
944 struct ceph_pg_pool_info, node);
945 __remove_pg_pool(&map->pg_pools, pi);
946 }
Ilya Dryomovcf73d882019-09-04 15:40:03 +0200947 kvfree(map->osd_state);
948 kvfree(map->osd_weight);
949 kvfree(map->osd_addr);
950 kvfree(map->osd_primary_affinity);
951 kvfree(map->crush_workspace);
Sage Weil2844a762010-04-09 15:46:42 -0700952 kfree(map);
953}
954
955/*
Ilya Dryomov4d603512014-03-21 19:05:28 +0200956 * Adjust max_osd value, (re)allocate arrays.
957 *
958 * The new elements are properly initialized.
Sage Weil2844a762010-04-09 15:46:42 -0700959 */
Ilya Dryomovcf73d882019-09-04 15:40:03 +0200960static int osdmap_set_max_osd(struct ceph_osdmap *map, u32 max)
Sage Weil2844a762010-04-09 15:46:42 -0700961{
Ilya Dryomov0bb05da2017-06-22 19:44:06 +0200962 u32 *state;
Sage Weil2844a762010-04-09 15:46:42 -0700963 u32 *weight;
Ilya Dryomov4d603512014-03-21 19:05:28 +0200964 struct ceph_entity_addr *addr;
Ilya Dryomovcf73d882019-09-04 15:40:03 +0200965 u32 to_copy;
Ilya Dryomov4d603512014-03-21 19:05:28 +0200966 int i;
Sage Weil2844a762010-04-09 15:46:42 -0700967
Ilya Dryomovcf73d882019-09-04 15:40:03 +0200968 dout("%s old %u new %u\n", __func__, map->max_osd, max);
969 if (max == map->max_osd)
970 return 0;
971
972 state = ceph_kvmalloc(array_size(max, sizeof(*state)), GFP_NOFS);
973 weight = ceph_kvmalloc(array_size(max, sizeof(*weight)), GFP_NOFS);
974 addr = ceph_kvmalloc(array_size(max, sizeof(*addr)), GFP_NOFS);
975 if (!state || !weight || !addr) {
976 kvfree(state);
977 kvfree(weight);
978 kvfree(addr);
Sage Weil2844a762010-04-09 15:46:42 -0700979 return -ENOMEM;
Ilya Dryomovcf73d882019-09-04 15:40:03 +0200980 }
981
982 to_copy = min(map->max_osd, max);
983 if (map->osd_state) {
984 memcpy(state, map->osd_state, to_copy * sizeof(*state));
985 memcpy(weight, map->osd_weight, to_copy * sizeof(*weight));
986 memcpy(addr, map->osd_addr, to_copy * sizeof(*addr));
987 kvfree(map->osd_state);
988 kvfree(map->osd_weight);
989 kvfree(map->osd_addr);
990 }
991
Li RongQing589506f2014-09-07 18:10:51 +0800992 map->osd_state = state;
Li RongQing589506f2014-09-07 18:10:51 +0800993 map->osd_weight = weight;
Li RongQing589506f2014-09-07 18:10:51 +0800994 map->osd_addr = addr;
Ilya Dryomov4d603512014-03-21 19:05:28 +0200995 for (i = map->max_osd; i < max; i++) {
Li RongQing589506f2014-09-07 18:10:51 +0800996 map->osd_state[i] = 0;
997 map->osd_weight[i] = CEPH_OSD_OUT;
998 memset(map->osd_addr + i, 0, sizeof(*map->osd_addr));
Sage Weil2844a762010-04-09 15:46:42 -0700999 }
1000
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +02001001 if (map->osd_primary_affinity) {
1002 u32 *affinity;
1003
Ilya Dryomovcf73d882019-09-04 15:40:03 +02001004 affinity = ceph_kvmalloc(array_size(max, sizeof(*affinity)),
1005 GFP_NOFS);
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +02001006 if (!affinity)
1007 return -ENOMEM;
1008
Ilya Dryomovcf73d882019-09-04 15:40:03 +02001009 memcpy(affinity, map->osd_primary_affinity,
1010 to_copy * sizeof(*affinity));
1011 kvfree(map->osd_primary_affinity);
1012
1013 map->osd_primary_affinity = affinity;
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +02001014 for (i = map->max_osd; i < max; i++)
Li RongQing589506f2014-09-07 18:10:51 +08001015 map->osd_primary_affinity[i] =
1016 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +02001017 }
1018
Sage Weil2844a762010-04-09 15:46:42 -07001019 map->max_osd = max;
Ilya Dryomov4d603512014-03-21 19:05:28 +02001020
Sage Weil2844a762010-04-09 15:46:42 -07001021 return 0;
1022}
1023
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001024static int osdmap_set_crush(struct ceph_osdmap *map, struct crush_map *crush)
1025{
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +01001026 void *workspace;
Ilya Dryomov743efcf2017-01-31 15:55:06 +01001027 size_t work_size;
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +01001028
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001029 if (IS_ERR(crush))
1030 return PTR_ERR(crush);
1031
Ilya Dryomov743efcf2017-01-31 15:55:06 +01001032 work_size = crush_work_size(crush, CEPH_PG_MAX_SIZE);
1033 dout("%s work_size %zu bytes\n", __func__, work_size);
Ilya Dryomovcf73d882019-09-04 15:40:03 +02001034 workspace = ceph_kvmalloc(work_size, GFP_NOIO);
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +01001035 if (!workspace) {
1036 crush_destroy(crush);
1037 return -ENOMEM;
1038 }
1039 crush_init_workspace(crush, workspace);
1040
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001041 if (map->crush)
1042 crush_destroy(map->crush);
Ilya Dryomovcf73d882019-09-04 15:40:03 +02001043 kvfree(map->crush_workspace);
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001044 map->crush = crush;
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +01001045 map->crush_workspace = workspace;
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001046 return 0;
1047}
1048
Ilya Dryomovec7af972014-03-21 19:05:29 +02001049#define OSDMAP_WRAPPER_COMPAT_VER 7
1050#define OSDMAP_CLIENT_DATA_COMPAT_VER 1
1051
1052/*
1053 * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps,
1054 * to struct_v of the client_data section for new (v7 and above)
1055 * osdmaps.
1056 */
1057static int get_osdmap_client_data_v(void **p, void *end,
1058 const char *prefix, u8 *v)
1059{
1060 u8 struct_v;
1061
1062 ceph_decode_8_safe(p, end, struct_v, e_inval);
1063 if (struct_v >= 7) {
1064 u8 struct_compat;
1065
1066 ceph_decode_8_safe(p, end, struct_compat, e_inval);
1067 if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001068 pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n",
1069 struct_v, struct_compat,
1070 OSDMAP_WRAPPER_COMPAT_VER, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +02001071 return -EINVAL;
1072 }
1073 *p += 4; /* ignore wrapper struct_len */
1074
1075 ceph_decode_8_safe(p, end, struct_v, e_inval);
1076 ceph_decode_8_safe(p, end, struct_compat, e_inval);
1077 if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001078 pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n",
1079 struct_v, struct_compat,
1080 OSDMAP_CLIENT_DATA_COMPAT_VER, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +02001081 return -EINVAL;
1082 }
1083 *p += 4; /* ignore client data struct_len */
1084 } else {
1085 u16 version;
1086
1087 *p -= 1;
1088 ceph_decode_16_safe(p, end, version, e_inval);
1089 if (version < 6) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001090 pr_warn("got v %d < 6 of %s ceph_osdmap\n",
1091 version, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +02001092 return -EINVAL;
1093 }
1094
1095 /* old osdmap enconding */
1096 struct_v = 0;
1097 }
1098
1099 *v = struct_v;
1100 return 0;
1101
1102e_inval:
1103 return -EINVAL;
1104}
1105
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001106static int __decode_pools(void **p, void *end, struct ceph_osdmap *map,
1107 bool incremental)
1108{
1109 u32 n;
1110
1111 ceph_decode_32_safe(p, end, n, e_inval);
1112 while (n--) {
1113 struct ceph_pg_pool_info *pi;
1114 u64 pool;
1115 int ret;
1116
1117 ceph_decode_64_safe(p, end, pool, e_inval);
1118
Ilya Dryomov8a4b8632020-05-19 16:46:47 +02001119 pi = lookup_pg_pool(&map->pg_pools, pool);
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001120 if (!incremental || !pi) {
1121 pi = kzalloc(sizeof(*pi), GFP_NOFS);
1122 if (!pi)
1123 return -ENOMEM;
1124
Ilya Dryomov8a4b8632020-05-19 16:46:47 +02001125 RB_CLEAR_NODE(&pi->node);
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001126 pi->id = pool;
1127
Ilya Dryomov8a4b8632020-05-19 16:46:47 +02001128 if (!__insert_pg_pool(&map->pg_pools, pi)) {
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001129 kfree(pi);
Ilya Dryomov8a4b8632020-05-19 16:46:47 +02001130 return -EEXIST;
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001131 }
1132 }
1133
1134 ret = decode_pool(p, end, pi);
1135 if (ret)
1136 return ret;
1137 }
1138
1139 return 0;
1140
1141e_inval:
1142 return -EINVAL;
1143}
1144
1145static int decode_pools(void **p, void *end, struct ceph_osdmap *map)
1146{
1147 return __decode_pools(p, end, map, false);
1148}
1149
1150static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map)
1151{
1152 return __decode_pools(p, end, map, true);
1153}
1154
Ilya Dryomova303bb02017-06-21 17:27:17 +02001155typedef struct ceph_pg_mapping *(*decode_mapping_fn_t)(void **, void *, bool);
1156
1157static int decode_pg_mapping(void **p, void *end, struct rb_root *mapping_root,
1158 decode_mapping_fn_t fn, bool incremental)
Ilya Dryomov10db6342014-03-21 19:05:28 +02001159{
1160 u32 n;
1161
Ilya Dryomova303bb02017-06-21 17:27:17 +02001162 WARN_ON(!incremental && !fn);
1163
Ilya Dryomov10db6342014-03-21 19:05:28 +02001164 ceph_decode_32_safe(p, end, n, e_inval);
1165 while (n--) {
Ilya Dryomova303bb02017-06-21 17:27:17 +02001166 struct ceph_pg_mapping *pg;
Ilya Dryomov10db6342014-03-21 19:05:28 +02001167 struct ceph_pg pgid;
Ilya Dryomov10db6342014-03-21 19:05:28 +02001168 int ret;
1169
1170 ret = ceph_decode_pgid(p, end, &pgid);
1171 if (ret)
1172 return ret;
1173
Ilya Dryomovab751442017-06-21 17:27:17 +02001174 pg = lookup_pg_mapping(mapping_root, &pgid);
1175 if (pg) {
1176 WARN_ON(!incremental);
1177 erase_pg_mapping(mapping_root, pg);
1178 free_pg_mapping(pg);
1179 }
Ilya Dryomov10db6342014-03-21 19:05:28 +02001180
Ilya Dryomova303bb02017-06-21 17:27:17 +02001181 if (fn) {
1182 pg = fn(p, end, incremental);
1183 if (IS_ERR(pg))
1184 return PTR_ERR(pg);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001185
Ilya Dryomova303bb02017-06-21 17:27:17 +02001186 if (pg) {
1187 pg->pgid = pgid; /* struct */
Ilya Dryomovab751442017-06-21 17:27:17 +02001188 insert_pg_mapping(mapping_root, pg);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001189 }
1190 }
1191 }
1192
1193 return 0;
1194
1195e_inval:
1196 return -EINVAL;
1197}
1198
Ilya Dryomova303bb02017-06-21 17:27:17 +02001199static struct ceph_pg_mapping *__decode_pg_temp(void **p, void *end,
1200 bool incremental)
1201{
1202 struct ceph_pg_mapping *pg;
1203 u32 len, i;
1204
1205 ceph_decode_32_safe(p, end, len, e_inval);
1206 if (len == 0 && incremental)
1207 return NULL; /* new_pg_temp: [] to remove */
1208 if (len > (SIZE_MAX - sizeof(*pg)) / sizeof(u32))
1209 return ERR_PTR(-EINVAL);
1210
1211 ceph_decode_need(p, end, len * sizeof(u32), e_inval);
1212 pg = alloc_pg_mapping(len * sizeof(u32));
1213 if (!pg)
1214 return ERR_PTR(-ENOMEM);
1215
1216 pg->pg_temp.len = len;
1217 for (i = 0; i < len; i++)
1218 pg->pg_temp.osds[i] = ceph_decode_32(p);
1219
1220 return pg;
1221
1222e_inval:
1223 return ERR_PTR(-EINVAL);
1224}
1225
Ilya Dryomov10db6342014-03-21 19:05:28 +02001226static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map)
1227{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001228 return decode_pg_mapping(p, end, &map->pg_temp, __decode_pg_temp,
1229 false);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001230}
1231
1232static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map)
1233{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001234 return decode_pg_mapping(p, end, &map->pg_temp, __decode_pg_temp,
1235 true);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001236}
1237
Ilya Dryomova303bb02017-06-21 17:27:17 +02001238static struct ceph_pg_mapping *__decode_primary_temp(void **p, void *end,
1239 bool incremental)
Ilya Dryomovd286de72014-03-21 19:05:30 +02001240{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001241 struct ceph_pg_mapping *pg;
1242 u32 osd;
Ilya Dryomovd286de72014-03-21 19:05:30 +02001243
Ilya Dryomova303bb02017-06-21 17:27:17 +02001244 ceph_decode_32_safe(p, end, osd, e_inval);
1245 if (osd == (u32)-1 && incremental)
1246 return NULL; /* new_primary_temp: -1 to remove */
Ilya Dryomovd286de72014-03-21 19:05:30 +02001247
Ilya Dryomova303bb02017-06-21 17:27:17 +02001248 pg = alloc_pg_mapping(0);
1249 if (!pg)
1250 return ERR_PTR(-ENOMEM);
Ilya Dryomovd286de72014-03-21 19:05:30 +02001251
Ilya Dryomova303bb02017-06-21 17:27:17 +02001252 pg->primary_temp.osd = osd;
1253 return pg;
Ilya Dryomovd286de72014-03-21 19:05:30 +02001254
1255e_inval:
Ilya Dryomova303bb02017-06-21 17:27:17 +02001256 return ERR_PTR(-EINVAL);
Ilya Dryomovd286de72014-03-21 19:05:30 +02001257}
1258
1259static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map)
1260{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001261 return decode_pg_mapping(p, end, &map->primary_temp,
1262 __decode_primary_temp, false);
Ilya Dryomovd286de72014-03-21 19:05:30 +02001263}
1264
1265static int decode_new_primary_temp(void **p, void *end,
1266 struct ceph_osdmap *map)
1267{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001268 return decode_pg_mapping(p, end, &map->primary_temp,
1269 __decode_primary_temp, true);
Ilya Dryomovd286de72014-03-21 19:05:30 +02001270}
1271
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +02001272u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd)
1273{
1274 BUG_ON(osd >= map->max_osd);
1275
1276 if (!map->osd_primary_affinity)
1277 return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
1278
1279 return map->osd_primary_affinity[osd];
1280}
1281
1282static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff)
1283{
1284 BUG_ON(osd >= map->max_osd);
1285
1286 if (!map->osd_primary_affinity) {
1287 int i;
1288
Ilya Dryomovcf73d882019-09-04 15:40:03 +02001289 map->osd_primary_affinity = ceph_kvmalloc(
1290 array_size(map->max_osd, sizeof(*map->osd_primary_affinity)),
1291 GFP_NOFS);
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +02001292 if (!map->osd_primary_affinity)
1293 return -ENOMEM;
1294
1295 for (i = 0; i < map->max_osd; i++)
1296 map->osd_primary_affinity[i] =
1297 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
1298 }
1299
1300 map->osd_primary_affinity[osd] = aff;
1301
1302 return 0;
1303}
1304
Ilya Dryomov63a69932014-03-21 19:05:30 +02001305static int decode_primary_affinity(void **p, void *end,
1306 struct ceph_osdmap *map)
1307{
1308 u32 len, i;
1309
1310 ceph_decode_32_safe(p, end, len, e_inval);
1311 if (len == 0) {
Ilya Dryomovcf73d882019-09-04 15:40:03 +02001312 kvfree(map->osd_primary_affinity);
Ilya Dryomov63a69932014-03-21 19:05:30 +02001313 map->osd_primary_affinity = NULL;
1314 return 0;
1315 }
1316 if (len != map->max_osd)
1317 goto e_inval;
1318
1319 ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval);
1320
1321 for (i = 0; i < map->max_osd; i++) {
1322 int ret;
1323
1324 ret = set_primary_affinity(map, i, ceph_decode_32(p));
1325 if (ret)
1326 return ret;
1327 }
1328
1329 return 0;
1330
1331e_inval:
1332 return -EINVAL;
1333}
1334
1335static int decode_new_primary_affinity(void **p, void *end,
1336 struct ceph_osdmap *map)
1337{
1338 u32 n;
1339
1340 ceph_decode_32_safe(p, end, n, e_inval);
1341 while (n--) {
1342 u32 osd, aff;
1343 int ret;
1344
1345 ceph_decode_32_safe(p, end, osd, e_inval);
1346 ceph_decode_32_safe(p, end, aff, e_inval);
1347
1348 ret = set_primary_affinity(map, osd, aff);
1349 if (ret)
1350 return ret;
Ilya Dryomovf31da0f2014-04-02 20:34:04 +04001351
1352 pr_info("osd%d primary-affinity 0x%x\n", osd, aff);
Ilya Dryomov63a69932014-03-21 19:05:30 +02001353 }
1354
1355 return 0;
1356
1357e_inval:
1358 return -EINVAL;
1359}
1360
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001361static struct ceph_pg_mapping *__decode_pg_upmap(void **p, void *end,
1362 bool __unused)
1363{
1364 return __decode_pg_temp(p, end, false);
1365}
1366
1367static int decode_pg_upmap(void **p, void *end, struct ceph_osdmap *map)
1368{
1369 return decode_pg_mapping(p, end, &map->pg_upmap, __decode_pg_upmap,
1370 false);
1371}
1372
1373static int decode_new_pg_upmap(void **p, void *end, struct ceph_osdmap *map)
1374{
1375 return decode_pg_mapping(p, end, &map->pg_upmap, __decode_pg_upmap,
1376 true);
1377}
1378
1379static int decode_old_pg_upmap(void **p, void *end, struct ceph_osdmap *map)
1380{
1381 return decode_pg_mapping(p, end, &map->pg_upmap, NULL, true);
1382}
1383
1384static struct ceph_pg_mapping *__decode_pg_upmap_items(void **p, void *end,
1385 bool __unused)
1386{
1387 struct ceph_pg_mapping *pg;
1388 u32 len, i;
1389
1390 ceph_decode_32_safe(p, end, len, e_inval);
1391 if (len > (SIZE_MAX - sizeof(*pg)) / (2 * sizeof(u32)))
1392 return ERR_PTR(-EINVAL);
1393
1394 ceph_decode_need(p, end, 2 * len * sizeof(u32), e_inval);
Ilya Dryomovf5cc6892017-07-07 16:14:45 +02001395 pg = alloc_pg_mapping(2 * len * sizeof(u32));
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001396 if (!pg)
1397 return ERR_PTR(-ENOMEM);
1398
1399 pg->pg_upmap_items.len = len;
1400 for (i = 0; i < len; i++) {
1401 pg->pg_upmap_items.from_to[i][0] = ceph_decode_32(p);
1402 pg->pg_upmap_items.from_to[i][1] = ceph_decode_32(p);
1403 }
1404
1405 return pg;
1406
1407e_inval:
1408 return ERR_PTR(-EINVAL);
1409}
1410
1411static int decode_pg_upmap_items(void **p, void *end, struct ceph_osdmap *map)
1412{
1413 return decode_pg_mapping(p, end, &map->pg_upmap_items,
1414 __decode_pg_upmap_items, false);
1415}
1416
1417static int decode_new_pg_upmap_items(void **p, void *end,
1418 struct ceph_osdmap *map)
1419{
1420 return decode_pg_mapping(p, end, &map->pg_upmap_items,
1421 __decode_pg_upmap_items, true);
1422}
1423
1424static int decode_old_pg_upmap_items(void **p, void *end,
1425 struct ceph_osdmap *map)
1426{
1427 return decode_pg_mapping(p, end, &map->pg_upmap_items, NULL, true);
1428}
1429
Sage Weil4fc51be2010-02-16 15:55:03 -08001430/*
Sage Weilf24e9982009-10-06 11:31:10 -07001431 * decode a full map.
1432 */
Ilya Dryomova2505d62014-03-13 16:36:13 +02001433static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
Sage Weilf24e9982009-10-06 11:31:10 -07001434{
Ilya Dryomovec7af972014-03-21 19:05:29 +02001435 u8 struct_v;
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001436 u32 epoch = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001437 void *start = *p;
Ilya Dryomov39770582014-03-13 16:36:14 +02001438 u32 max;
1439 u32 len, i;
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001440 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001441
Ilya Dryomova2505d62014-03-13 16:36:13 +02001442 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
Sage Weilf24e9982009-10-06 11:31:10 -07001443
Ilya Dryomovec7af972014-03-21 19:05:29 +02001444 err = get_osdmap_client_data_v(p, end, "full", &struct_v);
1445 if (err)
1446 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001447
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001448 /* fsid, epoch, created, modified */
1449 ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) +
1450 sizeof(map->created) + sizeof(map->modified), e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001451 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001452 epoch = map->epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001453 ceph_decode_copy(p, &map->created, sizeof(map->created));
1454 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
1455
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001456 /* pools */
1457 err = decode_pools(p, end, map);
1458 if (err)
1459 goto bad;
Sage Weil2844a762010-04-09 15:46:42 -07001460
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +02001461 /* pool_name */
1462 err = decode_pool_names(p, end, map);
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001463 if (err)
Sage Weil4f6a7e52013-02-23 10:41:09 -08001464 goto bad;
Sage Weil2844a762010-04-09 15:46:42 -07001465
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001466 ceph_decode_32_safe(p, end, map->pool_max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001467
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001468 ceph_decode_32_safe(p, end, map->flags, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001469
Ilya Dryomov39770582014-03-13 16:36:14 +02001470 /* max_osd */
1471 ceph_decode_32_safe(p, end, max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001472
1473 /* (re)alloc osd arrays */
1474 err = osdmap_set_max_osd(map, max);
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001475 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001476 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001477
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001478 /* osd_state, osd_weight, osd_addrs->client_addr */
Sage Weilf24e9982009-10-06 11:31:10 -07001479 ceph_decode_need(p, end, 3*sizeof(u32) +
Jeff Laytondcbc9192019-06-03 15:08:13 -04001480 map->max_osd*(struct_v >= 5 ? sizeof(u32) :
1481 sizeof(u8)) +
1482 sizeof(*map->osd_weight), e_inval);
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001483 if (ceph_decode_32(p) != map->max_osd)
1484 goto e_inval;
1485
Ilya Dryomov0bb05da2017-06-22 19:44:06 +02001486 if (struct_v >= 5) {
1487 for (i = 0; i < map->max_osd; i++)
1488 map->osd_state[i] = ceph_decode_32(p);
1489 } else {
1490 for (i = 0; i < map->max_osd; i++)
1491 map->osd_state[i] = ceph_decode_8(p);
1492 }
Sage Weilf24e9982009-10-06 11:31:10 -07001493
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001494 if (ceph_decode_32(p) != map->max_osd)
1495 goto e_inval;
1496
Sage Weilf24e9982009-10-06 11:31:10 -07001497 for (i = 0; i < map->max_osd; i++)
Sage Weilc89136e2009-10-14 09:59:09 -07001498 map->osd_weight[i] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001499
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001500 if (ceph_decode_32(p) != map->max_osd)
1501 goto e_inval;
1502
Jeff Laytondcbc9192019-06-03 15:08:13 -04001503 for (i = 0; i < map->max_osd; i++) {
1504 err = ceph_decode_entity_addr(p, end, &map->osd_addr[i]);
1505 if (err)
1506 goto bad;
1507 }
Sage Weilf24e9982009-10-06 11:31:10 -07001508
1509 /* pg_temp */
Ilya Dryomov10db6342014-03-21 19:05:28 +02001510 err = decode_pg_temp(p, end, map);
1511 if (err)
1512 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001513
Ilya Dryomovd286de72014-03-21 19:05:30 +02001514 /* primary_temp */
1515 if (struct_v >= 1) {
1516 err = decode_primary_temp(p, end, map);
1517 if (err)
1518 goto bad;
1519 }
1520
Ilya Dryomov63a69932014-03-21 19:05:30 +02001521 /* primary_affinity */
1522 if (struct_v >= 2) {
1523 err = decode_primary_affinity(p, end, map);
1524 if (err)
1525 goto bad;
1526 } else {
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001527 WARN_ON(map->osd_primary_affinity);
Ilya Dryomov63a69932014-03-21 19:05:30 +02001528 }
1529
Sage Weilf24e9982009-10-06 11:31:10 -07001530 /* crush */
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001531 ceph_decode_32_safe(p, end, len, e_inval);
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001532 err = osdmap_set_crush(map, crush_decode(*p, min(*p + len, end)));
1533 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001534 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001535
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001536 *p += len;
1537 if (struct_v >= 3) {
1538 /* erasure_code_profiles */
1539 ceph_decode_skip_map_of_map(p, end, string, string, string,
Dan Carpenter00c8ebb32017-07-13 10:45:17 +03001540 e_inval);
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001541 }
1542
1543 if (struct_v >= 4) {
1544 err = decode_pg_upmap(p, end, map);
1545 if (err)
1546 goto bad;
1547
1548 err = decode_pg_upmap_items(p, end, map);
1549 if (err)
1550 goto bad;
1551 } else {
1552 WARN_ON(!RB_EMPTY_ROOT(&map->pg_upmap));
1553 WARN_ON(!RB_EMPTY_ROOT(&map->pg_upmap_items));
1554 }
1555
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001556 /* ignore the rest */
Sage Weilf24e9982009-10-06 11:31:10 -07001557 *p = end;
1558
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001559 dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001560 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001561
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001562e_inval:
1563 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001564bad:
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001565 pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1566 err, epoch, (int)(*p - start), *p, start, end);
1567 print_hex_dump(KERN_DEBUG, "osdmap: ",
1568 DUMP_PREFIX_OFFSET, 16, 1,
1569 start, end - start, true);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001570 return err;
1571}
1572
1573/*
1574 * Allocate and decode a full map.
1575 */
1576struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end)
1577{
1578 struct ceph_osdmap *map;
1579 int ret;
1580
Ilya Dryomove5253a72016-04-28 16:07:25 +02001581 map = ceph_osdmap_alloc();
Ilya Dryomova2505d62014-03-13 16:36:13 +02001582 if (!map)
1583 return ERR_PTR(-ENOMEM);
1584
Ilya Dryomova2505d62014-03-13 16:36:13 +02001585 ret = osdmap_decode(p, end, map);
1586 if (ret) {
1587 ceph_osdmap_destroy(map);
1588 return ERR_PTR(ret);
1589 }
1590
1591 return map;
Sage Weilf24e9982009-10-06 11:31:10 -07001592}
1593
1594/*
Ilya Dryomov930c5322016-07-19 03:50:28 +02001595 * Encoding order is (new_up_client, new_state, new_weight). Need to
1596 * apply in the (new_weight, new_state, new_up_client) order, because
1597 * an incremental map may look like e.g.
1598 *
1599 * new_up_client: { osd=6, addr=... } # set osd_state and addr
1600 * new_state: { osd=6, xorstate=EXISTS } # clear osd_state
1601 */
Ilya Dryomov0bb05da2017-06-22 19:44:06 +02001602static int decode_new_up_state_weight(void **p, void *end, u8 struct_v,
Ilya Dryomov930c5322016-07-19 03:50:28 +02001603 struct ceph_osdmap *map)
1604{
1605 void *new_up_client;
1606 void *new_state;
1607 void *new_weight_end;
1608 u32 len;
Jeff Layton8cb5f2b2019-06-04 15:10:44 -04001609 int i;
Ilya Dryomov930c5322016-07-19 03:50:28 +02001610
1611 new_up_client = *p;
1612 ceph_decode_32_safe(p, end, len, e_inval);
Jeff Layton8cb5f2b2019-06-04 15:10:44 -04001613 for (i = 0; i < len; ++i) {
1614 struct ceph_entity_addr addr;
1615
1616 ceph_decode_skip_32(p, end, e_inval);
1617 if (ceph_decode_entity_addr(p, end, &addr))
1618 goto e_inval;
1619 }
Ilya Dryomov930c5322016-07-19 03:50:28 +02001620
1621 new_state = *p;
1622 ceph_decode_32_safe(p, end, len, e_inval);
Ilya Dryomov0bb05da2017-06-22 19:44:06 +02001623 len *= sizeof(u32) + (struct_v >= 5 ? sizeof(u32) : sizeof(u8));
Ilya Dryomov930c5322016-07-19 03:50:28 +02001624 ceph_decode_need(p, end, len, e_inval);
1625 *p += len;
1626
1627 /* new_weight */
1628 ceph_decode_32_safe(p, end, len, e_inval);
1629 while (len--) {
1630 s32 osd;
1631 u32 w;
1632
1633 ceph_decode_need(p, end, 2*sizeof(u32), e_inval);
1634 osd = ceph_decode_32(p);
1635 w = ceph_decode_32(p);
1636 BUG_ON(osd >= map->max_osd);
1637 pr_info("osd%d weight 0x%x %s\n", osd, w,
1638 w == CEPH_OSD_IN ? "(in)" :
1639 (w == CEPH_OSD_OUT ? "(out)" : ""));
1640 map->osd_weight[osd] = w;
1641
1642 /*
1643 * If we are marking in, set the EXISTS, and clear the
1644 * AUTOOUT and NEW bits.
1645 */
1646 if (w) {
1647 map->osd_state[osd] |= CEPH_OSD_EXISTS;
1648 map->osd_state[osd] &= ~(CEPH_OSD_AUTOOUT |
1649 CEPH_OSD_NEW);
1650 }
1651 }
1652 new_weight_end = *p;
1653
1654 /* new_state (up/down) */
1655 *p = new_state;
1656 len = ceph_decode_32(p);
1657 while (len--) {
1658 s32 osd;
Ilya Dryomov0bb05da2017-06-22 19:44:06 +02001659 u32 xorstate;
Ilya Dryomov930c5322016-07-19 03:50:28 +02001660 int ret;
1661
1662 osd = ceph_decode_32(p);
Ilya Dryomov0bb05da2017-06-22 19:44:06 +02001663 if (struct_v >= 5)
1664 xorstate = ceph_decode_32(p);
1665 else
1666 xorstate = ceph_decode_8(p);
Ilya Dryomov930c5322016-07-19 03:50:28 +02001667 if (xorstate == 0)
1668 xorstate = CEPH_OSD_UP;
1669 BUG_ON(osd >= map->max_osd);
1670 if ((map->osd_state[osd] & CEPH_OSD_UP) &&
1671 (xorstate & CEPH_OSD_UP))
1672 pr_info("osd%d down\n", osd);
1673 if ((map->osd_state[osd] & CEPH_OSD_EXISTS) &&
1674 (xorstate & CEPH_OSD_EXISTS)) {
1675 pr_info("osd%d does not exist\n", osd);
Ilya Dryomov930c5322016-07-19 03:50:28 +02001676 ret = set_primary_affinity(map, osd,
1677 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY);
1678 if (ret)
1679 return ret;
1680 memset(map->osd_addr + osd, 0, sizeof(*map->osd_addr));
1681 map->osd_state[osd] = 0;
1682 } else {
1683 map->osd_state[osd] ^= xorstate;
1684 }
1685 }
1686
1687 /* new_up_client */
1688 *p = new_up_client;
1689 len = ceph_decode_32(p);
1690 while (len--) {
1691 s32 osd;
1692 struct ceph_entity_addr addr;
1693
1694 osd = ceph_decode_32(p);
Ilya Dryomov930c5322016-07-19 03:50:28 +02001695 BUG_ON(osd >= map->max_osd);
Jeff Layton8cb5f2b2019-06-04 15:10:44 -04001696 if (ceph_decode_entity_addr(p, end, &addr))
1697 goto e_inval;
Ilya Dryomov930c5322016-07-19 03:50:28 +02001698 pr_info("osd%d up\n", osd);
1699 map->osd_state[osd] |= CEPH_OSD_EXISTS | CEPH_OSD_UP;
1700 map->osd_addr[osd] = addr;
1701 }
1702
1703 *p = new_weight_end;
1704 return 0;
1705
1706e_inval:
1707 return -EINVAL;
1708}
1709
1710/*
Sage Weilf24e9982009-10-06 11:31:10 -07001711 * decode and apply an incremental map update.
1712 */
1713struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
Ilya Dryomov0c0a8de2016-04-28 16:07:21 +02001714 struct ceph_osdmap *map)
Sage Weilf24e9982009-10-06 11:31:10 -07001715{
Sage Weilf24e9982009-10-06 11:31:10 -07001716 struct ceph_fsid fsid;
1717 u32 epoch = 0;
1718 struct ceph_timespec modified;
Sage Weil4f6a7e52013-02-23 10:41:09 -08001719 s32 len;
1720 u64 pool;
1721 __s64 new_pool_max;
1722 __s32 new_flags, max;
Sage Weilf24e9982009-10-06 11:31:10 -07001723 void *start = *p;
Ilya Dryomov86f17422014-03-13 16:36:15 +02001724 int err;
Ilya Dryomovec7af972014-03-21 19:05:29 +02001725 u8 struct_v;
Sage Weilf24e9982009-10-06 11:31:10 -07001726
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001727 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
1728
Ilya Dryomovec7af972014-03-21 19:05:29 +02001729 err = get_osdmap_client_data_v(p, end, "inc", &struct_v);
1730 if (err)
1731 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001732
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001733 /* fsid, epoch, modified, new_pool_max, new_flags */
1734 ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) +
1735 sizeof(u64) + sizeof(u32), e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001736 ceph_decode_copy(p, &fsid, sizeof(fsid));
Sage Weilc89136e2009-10-14 09:59:09 -07001737 epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001738 BUG_ON(epoch != map->epoch+1);
1739 ceph_decode_copy(p, &modified, sizeof(modified));
Sage Weil4f6a7e52013-02-23 10:41:09 -08001740 new_pool_max = ceph_decode_64(p);
Sage Weilc89136e2009-10-14 09:59:09 -07001741 new_flags = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001742
1743 /* full map? */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001744 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001745 if (len > 0) {
1746 dout("apply_incremental full map len %d, %p to %p\n",
1747 len, *p, end);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001748 return ceph_osdmap_decode(p, min(*p+len, end));
Sage Weilf24e9982009-10-06 11:31:10 -07001749 }
1750
1751 /* new crush? */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001752 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001753 if (len > 0) {
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001754 err = osdmap_set_crush(map,
1755 crush_decode(*p, min(*p + len, end)));
1756 if (err)
Ilya Dryomov86f17422014-03-13 16:36:15 +02001757 goto bad;
Sage Weilcebc5be2010-06-17 10:22:48 -07001758 *p += len;
Sage Weilf24e9982009-10-06 11:31:10 -07001759 }
1760
1761 /* new flags? */
1762 if (new_flags >= 0)
1763 map->flags = new_flags;
Sage Weil4fc51be2010-02-16 15:55:03 -08001764 if (new_pool_max >= 0)
1765 map->pool_max = new_pool_max;
Sage Weilf24e9982009-10-06 11:31:10 -07001766
Sage Weilf24e9982009-10-06 11:31:10 -07001767 /* new max? */
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001768 ceph_decode_32_safe(p, end, max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001769 if (max >= 0) {
1770 err = osdmap_set_max_osd(map, max);
Ilya Dryomov86f17422014-03-13 16:36:15 +02001771 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001772 goto bad;
1773 }
1774
1775 map->epoch++;
Sage Weil31456662011-05-12 15:18:43 -07001776 map->modified = modified;
Sage Weilf24e9982009-10-06 11:31:10 -07001777
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001778 /* new_pools */
1779 err = decode_new_pools(p, end, map);
1780 if (err)
1781 goto bad;
Ilya Dryomov9464d002014-03-13 16:36:16 +02001782
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +02001783 /* new_pool_names */
1784 err = decode_pool_names(p, end, map);
Ilya Dryomov9464d002014-03-13 16:36:16 +02001785 if (err)
1786 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001787
Sage Weil4fc51be2010-02-16 15:55:03 -08001788 /* old_pool */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001789 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weil4fc51be2010-02-16 15:55:03 -08001790 while (len--) {
1791 struct ceph_pg_pool_info *pi;
1792
Ilya Dryomov86f17422014-03-13 16:36:15 +02001793 ceph_decode_64_safe(p, end, pool, e_inval);
Ilya Dryomov8a4b8632020-05-19 16:46:47 +02001794 pi = lookup_pg_pool(&map->pg_pools, pool);
Sage Weil2844a762010-04-09 15:46:42 -07001795 if (pi)
1796 __remove_pg_pool(&map->pg_pools, pi);
Sage Weil4fc51be2010-02-16 15:55:03 -08001797 }
Sage Weilf24e9982009-10-06 11:31:10 -07001798
Ilya Dryomov930c5322016-07-19 03:50:28 +02001799 /* new_up_client, new_state, new_weight */
Ilya Dryomov0bb05da2017-06-22 19:44:06 +02001800 err = decode_new_up_state_weight(p, end, struct_v, map);
Ilya Dryomov930c5322016-07-19 03:50:28 +02001801 if (err)
1802 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001803
1804 /* new_pg_temp */
Ilya Dryomov10db6342014-03-21 19:05:28 +02001805 err = decode_new_pg_temp(p, end, map);
1806 if (err)
1807 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001808
Ilya Dryomovd286de72014-03-21 19:05:30 +02001809 /* new_primary_temp */
1810 if (struct_v >= 1) {
1811 err = decode_new_primary_temp(p, end, map);
1812 if (err)
1813 goto bad;
1814 }
1815
Ilya Dryomov63a69932014-03-21 19:05:30 +02001816 /* new_primary_affinity */
1817 if (struct_v >= 2) {
1818 err = decode_new_primary_affinity(p, end, map);
1819 if (err)
1820 goto bad;
1821 }
1822
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001823 if (struct_v >= 3) {
1824 /* new_erasure_code_profiles */
1825 ceph_decode_skip_map_of_map(p, end, string, string, string,
Dan Carpenter00c8ebb32017-07-13 10:45:17 +03001826 e_inval);
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001827 /* old_erasure_code_profiles */
Dan Carpenter00c8ebb32017-07-13 10:45:17 +03001828 ceph_decode_skip_set(p, end, string, e_inval);
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001829 }
1830
1831 if (struct_v >= 4) {
1832 err = decode_new_pg_upmap(p, end, map);
1833 if (err)
1834 goto bad;
1835
1836 err = decode_old_pg_upmap(p, end, map);
1837 if (err)
1838 goto bad;
1839
1840 err = decode_new_pg_upmap_items(p, end, map);
1841 if (err)
1842 goto bad;
1843
1844 err = decode_old_pg_upmap_items(p, end, map);
1845 if (err)
1846 goto bad;
1847 }
1848
Sage Weilf24e9982009-10-06 11:31:10 -07001849 /* ignore the rest */
1850 *p = end;
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001851
1852 dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001853 return map;
1854
Ilya Dryomov86f17422014-03-13 16:36:15 +02001855e_inval:
1856 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001857bad:
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001858 pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1859 err, epoch, (int)(*p - start), *p, start, end);
Sage Weil9ec7cab2009-12-14 15:13:47 -08001860 print_hex_dump(KERN_DEBUG, "osdmap: ",
1861 DUMP_PREFIX_OFFSET, 16, 1,
1862 start, end - start, true);
Sage Weilf24e9982009-10-06 11:31:10 -07001863 return ERR_PTR(err);
1864}
1865
Yan, Zheng30c156d2016-02-14 11:24:31 +08001866void ceph_oloc_copy(struct ceph_object_locator *dest,
1867 const struct ceph_object_locator *src)
1868{
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001869 ceph_oloc_destroy(dest);
Yan, Zheng30c156d2016-02-14 11:24:31 +08001870
1871 dest->pool = src->pool;
1872 if (src->pool_ns)
1873 dest->pool_ns = ceph_get_string(src->pool_ns);
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001874 else
1875 dest->pool_ns = NULL;
Yan, Zheng30c156d2016-02-14 11:24:31 +08001876}
1877EXPORT_SYMBOL(ceph_oloc_copy);
1878
1879void ceph_oloc_destroy(struct ceph_object_locator *oloc)
1880{
1881 ceph_put_string(oloc->pool_ns);
1882}
1883EXPORT_SYMBOL(ceph_oloc_destroy);
1884
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001885void ceph_oid_copy(struct ceph_object_id *dest,
1886 const struct ceph_object_id *src)
1887{
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001888 ceph_oid_destroy(dest);
Sage Weilf24e9982009-10-06 11:31:10 -07001889
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001890 if (src->name != src->inline_name) {
1891 /* very rare, see ceph_object_id definition */
1892 dest->name = kmalloc(src->name_len + 1,
1893 GFP_NOIO | __GFP_NOFAIL);
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001894 } else {
1895 dest->name = dest->inline_name;
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001896 }
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001897 memcpy(dest->name, src->name, src->name_len + 1);
1898 dest->name_len = src->name_len;
1899}
1900EXPORT_SYMBOL(ceph_oid_copy);
1901
1902static __printf(2, 0)
1903int oid_printf_vargs(struct ceph_object_id *oid, const char *fmt, va_list ap)
1904{
1905 int len;
1906
1907 WARN_ON(!ceph_oid_empty(oid));
1908
1909 len = vsnprintf(oid->inline_name, sizeof(oid->inline_name), fmt, ap);
1910 if (len >= sizeof(oid->inline_name))
1911 return len;
1912
1913 oid->name_len = len;
1914 return 0;
1915}
1916
1917/*
1918 * If oid doesn't fit into inline buffer, BUG.
1919 */
1920void ceph_oid_printf(struct ceph_object_id *oid, const char *fmt, ...)
1921{
1922 va_list ap;
1923
1924 va_start(ap, fmt);
1925 BUG_ON(oid_printf_vargs(oid, fmt, ap));
1926 va_end(ap);
1927}
1928EXPORT_SYMBOL(ceph_oid_printf);
1929
1930static __printf(3, 0)
1931int oid_aprintf_vargs(struct ceph_object_id *oid, gfp_t gfp,
1932 const char *fmt, va_list ap)
1933{
1934 va_list aq;
1935 int len;
1936
1937 va_copy(aq, ap);
1938 len = oid_printf_vargs(oid, fmt, aq);
1939 va_end(aq);
1940
1941 if (len) {
1942 char *external_name;
1943
1944 external_name = kmalloc(len + 1, gfp);
1945 if (!external_name)
1946 return -ENOMEM;
1947
1948 oid->name = external_name;
1949 WARN_ON(vsnprintf(oid->name, len + 1, fmt, ap) != len);
1950 oid->name_len = len;
1951 }
1952
1953 return 0;
1954}
1955
1956/*
1957 * If oid doesn't fit into inline buffer, allocate.
1958 */
1959int ceph_oid_aprintf(struct ceph_object_id *oid, gfp_t gfp,
1960 const char *fmt, ...)
1961{
1962 va_list ap;
1963 int ret;
1964
1965 va_start(ap, fmt);
1966 ret = oid_aprintf_vargs(oid, gfp, fmt, ap);
1967 va_end(ap);
1968
1969 return ret;
1970}
1971EXPORT_SYMBOL(ceph_oid_aprintf);
1972
1973void ceph_oid_destroy(struct ceph_object_id *oid)
1974{
1975 if (oid->name != oid->inline_name)
1976 kfree(oid->name);
1977}
1978EXPORT_SYMBOL(ceph_oid_destroy);
Sage Weilf24e9982009-10-06 11:31:10 -07001979
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001980/*
1981 * osds only
1982 */
1983static bool __osds_equal(const struct ceph_osds *lhs,
1984 const struct ceph_osds *rhs)
1985{
1986 if (lhs->size == rhs->size &&
1987 !memcmp(lhs->osds, rhs->osds, rhs->size * sizeof(rhs->osds[0])))
1988 return true;
1989
1990 return false;
1991}
1992
1993/*
1994 * osds + primary
1995 */
1996static bool osds_equal(const struct ceph_osds *lhs,
1997 const struct ceph_osds *rhs)
1998{
1999 if (__osds_equal(lhs, rhs) &&
2000 lhs->primary == rhs->primary)
2001 return true;
2002
2003 return false;
2004}
2005
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002006static bool osds_valid(const struct ceph_osds *set)
2007{
2008 /* non-empty set */
2009 if (set->size > 0 && set->primary >= 0)
2010 return true;
2011
2012 /* empty can_shift_osds set */
2013 if (!set->size && set->primary == -1)
2014 return true;
2015
2016 /* empty !can_shift_osds set - all NONE */
2017 if (set->size > 0 && set->primary == -1) {
2018 int i;
2019
2020 for (i = 0; i < set->size; i++) {
2021 if (set->osds[i] != CRUSH_ITEM_NONE)
2022 break;
2023 }
2024 if (i == set->size)
2025 return true;
2026 }
2027
2028 return false;
2029}
2030
2031void ceph_osds_copy(struct ceph_osds *dest, const struct ceph_osds *src)
2032{
2033 memcpy(dest->osds, src->osds, src->size * sizeof(src->osds[0]));
2034 dest->size = src->size;
2035 dest->primary = src->primary;
2036}
2037
Ilya Dryomov7de030d2017-06-15 16:30:54 +02002038bool ceph_pg_is_split(const struct ceph_pg *pgid, u32 old_pg_num,
2039 u32 new_pg_num)
Ilya Dryomov63244fa2016-04-28 16:07:23 +02002040{
2041 int old_bits = calc_bits_of(old_pg_num);
2042 int old_mask = (1 << old_bits) - 1;
2043 int n;
2044
2045 WARN_ON(pgid->seed >= old_pg_num);
2046 if (new_pg_num <= old_pg_num)
2047 return false;
2048
2049 for (n = 1; ; n++) {
2050 int next_bit = n << (old_bits - 1);
2051 u32 s = next_bit | pgid->seed;
2052
2053 if (s < old_pg_num || s == pgid->seed)
2054 continue;
2055 if (s >= new_pg_num)
2056 break;
2057
2058 s = ceph_stable_mod(s, old_pg_num, old_mask);
2059 if (s == pgid->seed)
2060 return true;
2061 }
2062
2063 return false;
2064}
2065
2066bool ceph_is_new_interval(const struct ceph_osds *old_acting,
2067 const struct ceph_osds *new_acting,
2068 const struct ceph_osds *old_up,
2069 const struct ceph_osds *new_up,
2070 int old_size,
2071 int new_size,
2072 int old_min_size,
2073 int new_min_size,
2074 u32 old_pg_num,
2075 u32 new_pg_num,
2076 bool old_sort_bitwise,
2077 bool new_sort_bitwise,
Ilya Dryomovae78dd82017-07-27 17:59:14 +02002078 bool old_recovery_deletes,
2079 bool new_recovery_deletes,
Ilya Dryomov63244fa2016-04-28 16:07:23 +02002080 const struct ceph_pg *pgid)
2081{
2082 return !osds_equal(old_acting, new_acting) ||
2083 !osds_equal(old_up, new_up) ||
2084 old_size != new_size ||
2085 old_min_size != new_min_size ||
Ilya Dryomov7de030d2017-06-15 16:30:54 +02002086 ceph_pg_is_split(pgid, old_pg_num, new_pg_num) ||
Ilya Dryomovae78dd82017-07-27 17:59:14 +02002087 old_sort_bitwise != new_sort_bitwise ||
2088 old_recovery_deletes != new_recovery_deletes;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02002089}
2090
2091static int calc_pg_rank(int osd, const struct ceph_osds *acting)
2092{
2093 int i;
2094
2095 for (i = 0; i < acting->size; i++) {
2096 if (acting->osds[i] == osd)
2097 return i;
2098 }
2099
2100 return -1;
2101}
2102
2103static bool primary_changed(const struct ceph_osds *old_acting,
2104 const struct ceph_osds *new_acting)
2105{
2106 if (!old_acting->size && !new_acting->size)
2107 return false; /* both still empty */
2108
2109 if (!old_acting->size ^ !new_acting->size)
2110 return true; /* was empty, now not, or vice versa */
2111
2112 if (old_acting->primary != new_acting->primary)
2113 return true; /* primary changed */
2114
2115 if (calc_pg_rank(old_acting->primary, old_acting) !=
2116 calc_pg_rank(new_acting->primary, new_acting))
2117 return true;
2118
2119 return false; /* same primary (tho replicas may have changed) */
2120}
2121
2122bool ceph_osds_changed(const struct ceph_osds *old_acting,
2123 const struct ceph_osds *new_acting,
2124 bool any_change)
2125{
2126 if (primary_changed(old_acting, new_acting))
2127 return true;
2128
2129 if (any_change && !__osds_equal(old_acting, new_acting))
2130 return true;
2131
2132 return false;
2133}
2134
Sage Weilf24e9982009-10-06 11:31:10 -07002135/*
Ilya Dryomovd9591f52016-04-28 16:07:22 +02002136 * Map an object into a PG.
2137 *
2138 * Should only be called with target_oid and target_oloc (as opposed to
2139 * base_oid and base_oloc), since tiering isn't taken into account.
Sage Weilf24e9982009-10-06 11:31:10 -07002140 */
Ilya Dryomova86f0092018-05-23 14:46:53 +02002141void __ceph_object_locator_to_pg(struct ceph_pg_pool_info *pi,
2142 const struct ceph_object_id *oid,
2143 const struct ceph_object_locator *oloc,
2144 struct ceph_pg *raw_pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07002145{
Ilya Dryomovdf281522017-06-15 16:30:56 +02002146 WARN_ON(pi->id != oloc->pool);
Sage Weilf24e9982009-10-06 11:31:10 -07002147
Yan, Zheng30c156d2016-02-14 11:24:31 +08002148 if (!oloc->pool_ns) {
2149 raw_pgid->pool = oloc->pool;
2150 raw_pgid->seed = ceph_str_hash(pi->object_hash, oid->name,
2151 oid->name_len);
2152 dout("%s %s -> raw_pgid %llu.%x\n", __func__, oid->name,
2153 raw_pgid->pool, raw_pgid->seed);
2154 } else {
2155 char stack_buf[256];
2156 char *buf = stack_buf;
2157 int nsl = oloc->pool_ns->len;
2158 size_t total = nsl + 1 + oid->name_len;
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02002159
Ilya Dryomova86f0092018-05-23 14:46:53 +02002160 if (total > sizeof(stack_buf))
2161 buf = kmalloc(total, GFP_NOIO | __GFP_NOFAIL);
Yan, Zheng30c156d2016-02-14 11:24:31 +08002162 memcpy(buf, oloc->pool_ns->str, nsl);
2163 buf[nsl] = '\037';
2164 memcpy(buf + nsl + 1, oid->name, oid->name_len);
2165 raw_pgid->pool = oloc->pool;
2166 raw_pgid->seed = ceph_str_hash(pi->object_hash, buf, total);
2167 if (buf != stack_buf)
2168 kfree(buf);
2169 dout("%s %s ns %.*s -> raw_pgid %llu.%x\n", __func__,
2170 oid->name, nsl, oloc->pool_ns->str,
2171 raw_pgid->pool, raw_pgid->seed);
2172 }
Sage Weilf24e9982009-10-06 11:31:10 -07002173}
Ilya Dryomovdf281522017-06-15 16:30:56 +02002174
2175int ceph_object_locator_to_pg(struct ceph_osdmap *osdmap,
2176 const struct ceph_object_id *oid,
2177 const struct ceph_object_locator *oloc,
2178 struct ceph_pg *raw_pgid)
2179{
2180 struct ceph_pg_pool_info *pi;
2181
2182 pi = ceph_pg_pool_by_id(osdmap, oloc->pool);
2183 if (!pi)
2184 return -ENOENT;
2185
Ilya Dryomova86f0092018-05-23 14:46:53 +02002186 __ceph_object_locator_to_pg(pi, oid, oloc, raw_pgid);
2187 return 0;
Ilya Dryomovdf281522017-06-15 16:30:56 +02002188}
Ilya Dryomovd9591f52016-04-28 16:07:22 +02002189EXPORT_SYMBOL(ceph_object_locator_to_pg);
Sage Weilf24e9982009-10-06 11:31:10 -07002190
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002191/*
2192 * Map a raw PG (full precision ps) into an actual PG.
2193 */
2194static void raw_pg_to_pg(struct ceph_pg_pool_info *pi,
2195 const struct ceph_pg *raw_pgid,
2196 struct ceph_pg *pgid)
2197{
2198 pgid->pool = raw_pgid->pool;
2199 pgid->seed = ceph_stable_mod(raw_pgid->seed, pi->pg_num,
2200 pi->pg_num_mask);
2201}
2202
2203/*
2204 * Map a raw PG (full precision ps) into a placement ps (placement
2205 * seed). Include pool id in that value so that different pools don't
2206 * use the same seeds.
2207 */
2208static u32 raw_pg_to_pps(struct ceph_pg_pool_info *pi,
2209 const struct ceph_pg *raw_pgid)
2210{
2211 if (pi->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
2212 /* hash pool id and seed so that pool PGs do not overlap */
2213 return crush_hash32_2(CRUSH_HASH_RJENKINS1,
2214 ceph_stable_mod(raw_pgid->seed,
2215 pi->pgp_num,
2216 pi->pgp_num_mask),
2217 raw_pgid->pool);
2218 } else {
2219 /*
2220 * legacy behavior: add ps and pool together. this is
2221 * not a great approach because the PGs from each pool
2222 * will overlap on top of each other: 0.5 == 1.4 ==
2223 * 2.3 == ...
2224 */
2225 return ceph_stable_mod(raw_pgid->seed, pi->pgp_num,
2226 pi->pgp_num_mask) +
2227 (unsigned)raw_pgid->pool;
2228 }
2229}
2230
Ilya Dryomove17e8962017-07-24 16:43:49 +02002231/*
2232 * Magic value used for a "default" fallback choose_args, used if the
2233 * crush_choose_arg_map passed to do_crush() does not exist. If this
2234 * also doesn't exist, fall back to canonical weights.
2235 */
2236#define CEPH_DEFAULT_CHOOSE_ARGS -1
2237
Ilya Dryomov9d521472014-01-31 17:54:26 +02002238static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
2239 int *result, int result_max,
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +02002240 const __u32 *weight, int weight_max,
Ilya Dryomove17e8962017-07-24 16:43:49 +02002241 s64 choose_args_index)
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02002242{
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +02002243 struct crush_choose_arg_map *arg_map;
Ilya Dryomov9d521472014-01-31 17:54:26 +02002244 int r;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02002245
Ilya Dryomov9d521472014-01-31 17:54:26 +02002246 BUG_ON(result_max > CEPH_PG_MAX_SIZE);
2247
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +02002248 arg_map = lookup_choose_arg_map(&map->crush->choose_args,
2249 choose_args_index);
Ilya Dryomove17e8962017-07-24 16:43:49 +02002250 if (!arg_map)
2251 arg_map = lookup_choose_arg_map(&map->crush->choose_args,
2252 CEPH_DEFAULT_CHOOSE_ARGS);
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +02002253
Ilya Dryomov743efcf2017-01-31 15:55:06 +01002254 mutex_lock(&map->crush_workspace_mutex);
Ilya Dryomov9d521472014-01-31 17:54:26 +02002255 r = crush_do_rule(map->crush, ruleno, x, result, result_max,
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +02002256 weight, weight_max, map->crush_workspace,
2257 arg_map ? arg_map->args : NULL);
Ilya Dryomov743efcf2017-01-31 15:55:06 +01002258 mutex_unlock(&map->crush_workspace_mutex);
Ilya Dryomov9d521472014-01-31 17:54:26 +02002259
2260 return r;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02002261}
2262
Ilya Dryomov1c2e7b42017-06-21 17:27:18 +02002263static void remove_nonexistent_osds(struct ceph_osdmap *osdmap,
2264 struct ceph_pg_pool_info *pi,
2265 struct ceph_osds *set)
2266{
2267 int i;
2268
2269 if (ceph_can_shift_osds(pi)) {
2270 int removed = 0;
2271
2272 /* shift left */
2273 for (i = 0; i < set->size; i++) {
2274 if (!ceph_osd_exists(osdmap, set->osds[i])) {
2275 removed++;
2276 continue;
2277 }
2278 if (removed)
2279 set->osds[i - removed] = set->osds[i];
2280 }
2281 set->size -= removed;
2282 } else {
2283 /* set dne devices to NONE */
2284 for (i = 0; i < set->size; i++) {
2285 if (!ceph_osd_exists(osdmap, set->osds[i]))
2286 set->osds[i] = CRUSH_ITEM_NONE;
2287 }
2288 }
2289}
2290
Sage Weilf24e9982009-10-06 11:31:10 -07002291/*
Ilya Dryomov1c2e7b42017-06-21 17:27:18 +02002292 * Calculate raw set (CRUSH output) for given PG and filter out
2293 * nonexistent OSDs. ->primary is undefined for a raw set.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002294 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002295 * Placement seed (CRUSH input) is returned through @ppps.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002296 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002297static void pg_to_raw_osds(struct ceph_osdmap *osdmap,
2298 struct ceph_pg_pool_info *pi,
2299 const struct ceph_pg *raw_pgid,
2300 struct ceph_osds *raw,
2301 u32 *ppps)
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002302{
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002303 u32 pps = raw_pg_to_pps(pi, raw_pgid);
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002304 int ruleno;
2305 int len;
2306
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002307 ceph_osds_init(raw);
2308 if (ppps)
2309 *ppps = pps;
2310
2311 ruleno = crush_find_rule(osdmap->crush, pi->crush_ruleset, pi->type,
2312 pi->size);
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002313 if (ruleno < 0) {
2314 pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n",
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002315 pi->id, pi->crush_ruleset, pi->type, pi->size);
2316 return;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002317 }
2318
Ilya Dryomovef9324bb2017-02-08 18:57:48 +01002319 if (pi->size > ARRAY_SIZE(raw->osds)) {
2320 pr_err_ratelimited("pool %lld ruleset %d type %d too wide: size %d > %zu\n",
2321 pi->id, pi->crush_ruleset, pi->type, pi->size,
2322 ARRAY_SIZE(raw->osds));
2323 return;
2324 }
2325
2326 len = do_crush(osdmap, ruleno, pps, raw->osds, pi->size,
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +02002327 osdmap->osd_weight, osdmap->max_osd, pi->id);
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002328 if (len < 0) {
2329 pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n",
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002330 len, ruleno, pi->id, pi->crush_ruleset, pi->type,
2331 pi->size);
2332 return;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002333 }
2334
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002335 raw->size = len;
Ilya Dryomov1c2e7b42017-06-21 17:27:18 +02002336 remove_nonexistent_osds(osdmap, pi, raw);
2337}
2338
2339/* apply pg_upmap[_items] mappings */
2340static void apply_upmap(struct ceph_osdmap *osdmap,
2341 const struct ceph_pg *pgid,
2342 struct ceph_osds *raw)
2343{
2344 struct ceph_pg_mapping *pg;
2345 int i, j;
2346
2347 pg = lookup_pg_mapping(&osdmap->pg_upmap, pgid);
2348 if (pg) {
2349 /* make sure targets aren't marked out */
2350 for (i = 0; i < pg->pg_upmap.len; i++) {
2351 int osd = pg->pg_upmap.osds[i];
2352
2353 if (osd != CRUSH_ITEM_NONE &&
2354 osd < osdmap->max_osd &&
2355 osdmap->osd_weight[osd] == 0) {
2356 /* reject/ignore explicit mapping */
2357 return;
2358 }
2359 }
2360 for (i = 0; i < pg->pg_upmap.len; i++)
2361 raw->osds[i] = pg->pg_upmap.osds[i];
2362 raw->size = pg->pg_upmap.len;
Ilya Dryomovf53b7662017-07-27 15:16:39 +02002363 /* check and apply pg_upmap_items, if any */
Ilya Dryomov1c2e7b42017-06-21 17:27:18 +02002364 }
2365
2366 pg = lookup_pg_mapping(&osdmap->pg_upmap_items, pgid);
2367 if (pg) {
Ilya Dryomov29a0cfb2017-09-18 12:21:37 +02002368 /*
2369 * Note: this approach does not allow a bidirectional swap,
2370 * e.g., [[1,2],[2,1]] applied to [0,1,2] -> [0,2,1].
2371 */
2372 for (i = 0; i < pg->pg_upmap_items.len; i++) {
2373 int from = pg->pg_upmap_items.from_to[i][0];
2374 int to = pg->pg_upmap_items.from_to[i][1];
2375 int pos = -1;
2376 bool exists = false;
Ilya Dryomov1c2e7b42017-06-21 17:27:18 +02002377
Ilya Dryomov29a0cfb2017-09-18 12:21:37 +02002378 /* make sure replacement doesn't already appear */
2379 for (j = 0; j < raw->size; j++) {
2380 int osd = raw->osds[j];
2381
2382 if (osd == to) {
2383 exists = true;
Ilya Dryomov1c2e7b42017-06-21 17:27:18 +02002384 break;
2385 }
Ilya Dryomov29a0cfb2017-09-18 12:21:37 +02002386 /* ignore mapping if target is marked out */
2387 if (osd == from && pos < 0 &&
2388 !(to != CRUSH_ITEM_NONE &&
2389 to < osdmap->max_osd &&
2390 osdmap->osd_weight[to] == 0)) {
2391 pos = j;
2392 }
Ilya Dryomov1c2e7b42017-06-21 17:27:18 +02002393 }
Ilya Dryomov29a0cfb2017-09-18 12:21:37 +02002394 if (!exists && pos >= 0)
2395 raw->osds[pos] = to;
Ilya Dryomov1c2e7b42017-06-21 17:27:18 +02002396 }
2397 }
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002398}
2399
2400/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002401 * Given raw set, calculate up set and up primary. By definition of an
2402 * up set, the result won't contain nonexistent or down OSDs.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002403 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002404 * This is done in-place - on return @set is the up set. If it's
2405 * empty, ->primary will remain undefined.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002406 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002407static void raw_to_up_osds(struct ceph_osdmap *osdmap,
2408 struct ceph_pg_pool_info *pi,
2409 struct ceph_osds *set)
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002410{
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002411 int i;
2412
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002413 /* ->primary is undefined for a raw set */
2414 BUG_ON(set->primary != -1);
2415
2416 if (ceph_can_shift_osds(pi)) {
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002417 int removed = 0;
2418
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002419 /* shift left */
2420 for (i = 0; i < set->size; i++) {
2421 if (ceph_osd_is_down(osdmap, set->osds[i])) {
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002422 removed++;
2423 continue;
2424 }
2425 if (removed)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002426 set->osds[i - removed] = set->osds[i];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002427 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002428 set->size -= removed;
2429 if (set->size > 0)
2430 set->primary = set->osds[0];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002431 } else {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002432 /* set down/dne devices to NONE */
2433 for (i = set->size - 1; i >= 0; i--) {
2434 if (ceph_osd_is_down(osdmap, set->osds[i]))
2435 set->osds[i] = CRUSH_ITEM_NONE;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002436 else
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002437 set->primary = set->osds[i];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002438 }
2439 }
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002440}
2441
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002442static void apply_primary_affinity(struct ceph_osdmap *osdmap,
2443 struct ceph_pg_pool_info *pi,
2444 u32 pps,
2445 struct ceph_osds *up)
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002446{
2447 int i;
2448 int pos = -1;
2449
2450 /*
2451 * Do we have any non-default primary_affinity values for these
2452 * osds?
2453 */
2454 if (!osdmap->osd_primary_affinity)
2455 return;
2456
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002457 for (i = 0; i < up->size; i++) {
2458 int osd = up->osds[i];
Ilya Dryomov92b2e752014-04-10 18:09:41 +04002459
2460 if (osd != CRUSH_ITEM_NONE &&
2461 osdmap->osd_primary_affinity[osd] !=
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002462 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) {
2463 break;
2464 }
2465 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002466 if (i == up->size)
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002467 return;
2468
2469 /*
2470 * Pick the primary. Feed both the seed (for the pg) and the
2471 * osd into the hash/rng so that a proportional fraction of an
2472 * osd's pgs get rejected as primary.
2473 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002474 for (i = 0; i < up->size; i++) {
2475 int osd = up->osds[i];
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002476 u32 aff;
2477
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002478 if (osd == CRUSH_ITEM_NONE)
2479 continue;
2480
2481 aff = osdmap->osd_primary_affinity[osd];
2482 if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY &&
2483 (crush_hash32_2(CRUSH_HASH_RJENKINS1,
2484 pps, osd) >> 16) >= aff) {
2485 /*
2486 * We chose not to use this primary. Note it
2487 * anyway as a fallback in case we don't pick
2488 * anyone else, but keep looking.
2489 */
2490 if (pos < 0)
2491 pos = i;
2492 } else {
2493 pos = i;
2494 break;
2495 }
2496 }
2497 if (pos < 0)
2498 return;
2499
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002500 up->primary = up->osds[pos];
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002501
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002502 if (ceph_can_shift_osds(pi) && pos > 0) {
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002503 /* move the new primary to the front */
2504 for (i = pos; i > 0; i--)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002505 up->osds[i] = up->osds[i - 1];
2506 up->osds[0] = up->primary;
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002507 }
2508}
2509
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002510/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002511 * Get pg_temp and primary_temp mappings for given PG.
Ilya Dryomov45966c32014-03-24 17:12:47 +02002512 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002513 * Note that a PG may have none, only pg_temp, only primary_temp or
2514 * both pg_temp and primary_temp mappings. This means @temp isn't
2515 * always a valid OSD set on return: in the "only primary_temp" case,
2516 * @temp will have its ->primary >= 0 but ->size == 0.
Ilya Dryomov45966c32014-03-24 17:12:47 +02002517 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002518static void get_temp_osds(struct ceph_osdmap *osdmap,
2519 struct ceph_pg_pool_info *pi,
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002520 const struct ceph_pg *pgid,
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002521 struct ceph_osds *temp)
Ilya Dryomov45966c32014-03-24 17:12:47 +02002522{
2523 struct ceph_pg_mapping *pg;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002524 int i;
2525
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002526 ceph_osds_init(temp);
Ilya Dryomov45966c32014-03-24 17:12:47 +02002527
2528 /* pg_temp? */
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002529 pg = lookup_pg_mapping(&osdmap->pg_temp, pgid);
Ilya Dryomov45966c32014-03-24 17:12:47 +02002530 if (pg) {
Ilya Dryomov45966c32014-03-24 17:12:47 +02002531 for (i = 0; i < pg->pg_temp.len; i++) {
2532 if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002533 if (ceph_can_shift_osds(pi))
Ilya Dryomov45966c32014-03-24 17:12:47 +02002534 continue;
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002535
2536 temp->osds[temp->size++] = CRUSH_ITEM_NONE;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002537 } else {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002538 temp->osds[temp->size++] = pg->pg_temp.osds[i];
Ilya Dryomov45966c32014-03-24 17:12:47 +02002539 }
2540 }
2541
2542 /* apply pg_temp's primary */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002543 for (i = 0; i < temp->size; i++) {
2544 if (temp->osds[i] != CRUSH_ITEM_NONE) {
2545 temp->primary = temp->osds[i];
Ilya Dryomov45966c32014-03-24 17:12:47 +02002546 break;
2547 }
2548 }
Ilya Dryomov45966c32014-03-24 17:12:47 +02002549 }
2550
Ilya Dryomov5e8d4d32014-03-24 17:12:48 +02002551 /* primary_temp? */
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002552 pg = lookup_pg_mapping(&osdmap->primary_temp, pgid);
Ilya Dryomov5e8d4d32014-03-24 17:12:48 +02002553 if (pg)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002554 temp->primary = pg->primary_temp.osd;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002555}
2556
2557/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002558 * Map a PG to its acting set as well as its up set.
Ilya Dryomovac972232014-03-24 17:12:48 +02002559 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002560 * Acting set is used for data mapping purposes, while up set can be
2561 * recorded for detecting interval changes and deciding whether to
2562 * resend a request.
Sage Weild85b7052010-05-10 10:24:48 -07002563 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002564void ceph_pg_to_up_acting_osds(struct ceph_osdmap *osdmap,
Ilya Dryomovdf281522017-06-15 16:30:56 +02002565 struct ceph_pg_pool_info *pi,
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002566 const struct ceph_pg *raw_pgid,
2567 struct ceph_osds *up,
2568 struct ceph_osds *acting)
Sage Weild85b7052010-05-10 10:24:48 -07002569{
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002570 struct ceph_pg pgid;
Ilya Dryomovac972232014-03-24 17:12:48 +02002571 u32 pps;
Sage Weild85b7052010-05-10 10:24:48 -07002572
Ilya Dryomovdf281522017-06-15 16:30:56 +02002573 WARN_ON(pi->id != raw_pgid->pool);
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002574 raw_pg_to_pg(pi, raw_pgid, &pgid);
Sage Weild85b7052010-05-10 10:24:48 -07002575
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002576 pg_to_raw_osds(osdmap, pi, raw_pgid, up, &pps);
Ilya Dryomov1c2e7b42017-06-21 17:27:18 +02002577 apply_upmap(osdmap, &pgid, up);
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002578 raw_to_up_osds(osdmap, pi, up);
2579 apply_primary_affinity(osdmap, pi, pps, up);
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002580 get_temp_osds(osdmap, pi, &pgid, acting);
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002581 if (!acting->size) {
2582 memcpy(acting->osds, up->osds, up->size * sizeof(up->osds[0]));
2583 acting->size = up->size;
2584 if (acting->primary == -1)
2585 acting->primary = up->primary;
Ilya Dryomovac972232014-03-24 17:12:48 +02002586 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002587 WARN_ON(!osds_valid(up) || !osds_valid(acting));
Sage Weild85b7052010-05-10 10:24:48 -07002588}
2589
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002590bool ceph_pg_to_primary_shard(struct ceph_osdmap *osdmap,
Ilya Dryomovdf281522017-06-15 16:30:56 +02002591 struct ceph_pg_pool_info *pi,
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002592 const struct ceph_pg *raw_pgid,
2593 struct ceph_spg *spgid)
2594{
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002595 struct ceph_pg pgid;
2596 struct ceph_osds up, acting;
2597 int i;
2598
Ilya Dryomovdf281522017-06-15 16:30:56 +02002599 WARN_ON(pi->id != raw_pgid->pool);
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002600 raw_pg_to_pg(pi, raw_pgid, &pgid);
2601
2602 if (ceph_can_shift_osds(pi)) {
2603 spgid->pgid = pgid; /* struct */
2604 spgid->shard = CEPH_SPG_NOSHARD;
2605 return true;
2606 }
2607
Ilya Dryomovdf281522017-06-15 16:30:56 +02002608 ceph_pg_to_up_acting_osds(osdmap, pi, &pgid, &up, &acting);
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002609 for (i = 0; i < acting.size; i++) {
2610 if (acting.osds[i] == acting.primary) {
2611 spgid->pgid = pgid; /* struct */
2612 spgid->shard = i;
2613 return true;
2614 }
2615 }
2616
2617 return false;
2618}
2619
Sage Weild85b7052010-05-10 10:24:48 -07002620/*
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002621 * Return acting primary for given PG, or -1 if none.
Sage Weilf24e9982009-10-06 11:31:10 -07002622 */
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002623int ceph_pg_to_acting_primary(struct ceph_osdmap *osdmap,
2624 const struct ceph_pg *raw_pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07002625{
Ilya Dryomovdf281522017-06-15 16:30:56 +02002626 struct ceph_pg_pool_info *pi;
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002627 struct ceph_osds up, acting;
Sage Weilf24e9982009-10-06 11:31:10 -07002628
Ilya Dryomovdf281522017-06-15 16:30:56 +02002629 pi = ceph_pg_pool_by_id(osdmap, raw_pgid->pool);
2630 if (!pi)
2631 return -1;
2632
2633 ceph_pg_to_up_acting_osds(osdmap, pi, raw_pgid, &up, &acting);
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002634 return acting.primary;
Sage Weilf24e9982009-10-06 11:31:10 -07002635}
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002636EXPORT_SYMBOL(ceph_pg_to_acting_primary);